Compare commits

..

No commits in common. "main" and "v0.1.1" have entirely different histories.
main ... v0.1.1

161 changed files with 11814 additions and 39393 deletions

View File

@ -1,31 +0,0 @@
---
name: Bug Report
about: Create a report to help us improve
title: "[BUG] "
labels: bug
assignees: ''
---
**Describe the bug**
A clear and concise description of what the bug is.
**To Reproduce**
Steps to reproduce the behavior:
1. Go to '...'
2. Click on '....'
3. Scroll down to '....'
4. See error
**Expected behavior**
A clear and concise description of what you expected to happen.
**Screenshots**
If applicable, add screenshots to help explain your problem.
**Environment (please complete the following information):**
- OS: [e.g. macOS, Windows]
- App Version [e.g. 1.0.0]
**Additional context**
Add any other context about the problem here.

View File

@ -1,20 +0,0 @@
---
name: Feature Request
about: Suggest an idea for this project
title: "[FEAT] "
labels: feature
assignees: ''
---
**Is your feature request related to a problem? Please describe.**
A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]
**Describe the solution you'd like**
A clear and concise description of what you want to happen.
**Describe alternatives you've considered**
A clear and concise description of any alternative solutions or features you've considered.
**Additional context**
Add any other context or screenshots about the feature request here.

View File

@ -1,28 +0,0 @@
---
name: Pull Request
about: Propose a change to the codebase
---
## Summary of Changes
Please provide a brief, high-level summary of the changes in this pull request.
## Related Issue
- Closes #XXX
*Please replace `XXX` with the issue number that this pull request resolves. If it does not resolve a specific issue, please explain why this change is needed.*
## Contributor's Self-Review Checklist
Please check the boxes that apply. This is a reminder of what we look for in a good pull request.
- [ ] I have read the [CONTRIBUTING.md](https://github.com/your-org/your-repo/blob/main/CONTRIBUTING.md) document.
- [ ] My code follows the project's coding style and architectural patterns as described in [DESIGN_PATTERNS.md](https://github.com/your-org/your-repo/blob/main/docs/DESIGN_PATTERNS.md).
- [ ] I have added or updated relevant tests for my changes.
- [ ] I have updated the documentation to reflect my changes (if applicable).
- [ ] My changes have been tested locally and are working as expected.
## Additional Context (Optional)
Add any other context or screenshots about the pull request here.

View File

@ -1,62 +0,0 @@
name: Assign on Comment
on:
issue_comment:
types: [created]
jobs:
# Job 1: Any contributor can self-assign
self-assign:
# Only run if the comment is exactly '/assign'
if: startsWith(github.event.comment.body, '/assign') && !contains(github.event.comment.body, '@')
runs-on: ubuntu-latest
permissions:
issues: write
steps:
- name: Assign commenter to the issue
uses: actions/github-script@v7
with:
script: |
// Assign the commenter as the assignee
await github.rest.issues.addAssignees({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
assignees: [context.actor]
});
// Add a rocket (🚀) reaction to indicate success
await github.rest.reactions.createForIssueComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: context.payload.comment.id,
content: 'rocket'
});
# Job 2: Admin can assign others
assign-others:
# Only run if the comment starts with '/assign @' and the commenter is in the admin group
if: startsWith(github.event.comment.body, '/assign @') && contains(fromJson('["OWNER", "COLLABORATOR", "MEMBER"]'), github.event.comment.author_association)
runs-on: ubuntu-latest
permissions:
issues: write
steps:
- name: Assign mentioned user
uses: actions/github-script@v7
with:
script: |
const mention = context.payload.comment.body.split(' ')[1];
const assignee = mention.substring(1); // Remove '@'
// Assign the mentioned user as the assignee
await github.rest.issues.addAssignees({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
assignees: [assignee]
});
// Add a thumbs up (+1) reaction to indicate success
await github.rest.reactions.createForIssueComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: context.payload.comment.id,
content: '+1'
});

View File

@ -1,45 +0,0 @@
name: Build & Verify
on:
push:
branches: [ "main" ] # Runs on every push to main branch
jobs:
build:
# Currently runs on macOS only, can add windows-latest later
runs-on: macos-latest
steps:
- name: 🚚 Checkout code
uses: actions/checkout@v4
- name: ⚙️ Setup Node.js environment
uses: actions/setup-node@v4
with:
node-version: '20.x' # Node.js version compatible with project
cache: 'npm' # npm dependency caching for speed improvement
- name: 📦 Install root dependencies
run: npm install
- name: 🌐 Install and build web (Renderer) part
# Move to pickleglass_web directory and run commands
working-directory: ./pickleglass_web
run: |
npm install
npm run build
- name: 🖥️ Build Electron app
# Run Electron build script from root directory
run: npm run build
- name: 🚨 Send failure notification to Slack
if: failure()
uses: rtCamp/action-slack-notify@v2
env:
SLACK_CHANNEL: general
SLACK_TITLE: "🚨 Build Failed"
SLACK_MESSAGE: "😭 Build failed for `${{ github.repository }}` repo on main branch."
SLACK_COLOR: 'danger'
SLACK_WEBHOOK: ${{ secrets.SLACK_WEBHOOK_URL }}

1
.gitignore vendored
View File

@ -102,6 +102,7 @@ pickleglass_web/venv/
node_modules/ node_modules/
npm-debug.log npm-debug.log
yarn-error.log yarn-error.log
package-lock.json
# Database # Database
data/*.db data/*.db

3
.gitmodules vendored
View File

@ -1,3 +0,0 @@
[submodule "aec"]
path = aec
url = https://github.com/samtiz/aec.git

1
.npmrc
View File

@ -1,2 +1,3 @@
better-sqlite3:ignore-scripts=true better-sqlite3:ignore-scripts=true
electron-deeplink:ignore-scripts=true
sharp:ignore-scripts=true sharp:ignore-scripts=true

View File

@ -1,2 +1,2 @@
src/ui/assets src/assets
node_modules node_modules

View File

@ -1,93 +0,0 @@
# Contributing to Glass
Thank you for considering contributing to **Glass by Pickle**! Contributions make the open-source community vibrant, innovative, and collaborative. We appreciate every contribution you make—big or small.
This document guides you through the entire contribution process, from finding an issue to getting your pull request merged.
---
## 🚀 Contribution Workflow
To ensure a smooth and effective workflow, all contributions must go through the following process. Please follow these steps carefully.
### 1. Find or Create an Issue
All work begins with an issue. This is the central place to discuss new ideas and track progress.
- Browse our existing [**Issues**](https://github.com/pickle-com/glass/issues) to find something you'd like to work on. We recommend looking for issues labeled `good first issue` if you're new!
- If you have a new idea or find a bug that hasn't been reported, please **create a new issue** using our templates.
### 2. Claim the Issue
To avoid duplicate work, you must claim an issue before you start coding.
- On the issue you want to work on, leave a comment with the command:
```
/assign
```
- Our GitHub bot will automatically assign the issue to you. Once your profile appears in the **`Assignees`** section on the right, you are ready to start development.
### 3. Fork & Create a Branch
Now it's time to set up your local environment.
1. **Fork** the repository to your own GitHub account.
2. **Clone** your forked repository to your local machine.
3. **Create a new branch** from `main`. A clear branch name is recommended.
- For new features: `feat/short-description` (e.g., `feat/user-login-ui`)
- For bug fixes: `fix/short-description` (e.g., `fix/header-rendering-bug`)
### 4. Develop
Write your code! As you work, please adhere to our quality standards.
- **Code Style & Quality**: Our project uses `Prettier` and `ESLint` to maintain a consistent code style.
- **Architecture & Design Patterns**: All new code must be consistent with the project's architecture. Please read our **[Design Patterns Guide](https://github.com/pickle-com/glass/blob/main/docs/DESIGN_PATTERNS.md)** before making significant changes.
### 5. Create a Pull Request (PR)
Once your work is ready, create a Pull Request to the `main` branch of the original repository.
- **Fill out the PR Template**: Our template will appear automatically. Please provide a clear summary of your changes.
- **Link the Issue**: In the PR description, include the line `Closes #XXX` (e.g., `Closes #123`) to link it to the issue you resolved. This is mandatory.
- **Code Review**: A maintainer will review your code, provide feedback, and merge it.
---
# Developing
### Prerequisites
Ensure the following are installed:
- [Node.js v20.x.x](https://nodejs.org/en/download)
- [Python](https://www.python.org/downloads/)
- (Windows users) [Build Tools for Visual Studio](https://visualstudio.microsoft.com/downloads/)
Ensure you're using Node.js version 20.x.x to avoid build errors with native dependencies.
```bash
# Check your Node.js version
node --version
# If you need to install Node.js 20.x.x, we recommend using nvm:
# curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash
# nvm install 20
# nvm use 20
```
## Setup and Build
```bash
npm run setup
```
Please ensure that you can make a full production build before pushing code.
## Linting
```bash
npm run lint
```
If you get errors, be sure to fix them before committing.

View File

@ -62,14 +62,11 @@ npm run setup
<img width="100%" alt="booking-screen" src="./public/assets/01.gif"> <img width="100%" alt="booking-screen" src="./public/assets/01.gif">
### Use your own API key, or sign up to use ours (free) ### Use your own OpenAI API key, or sign up to use ours (free)
<img width="100%" alt="booking-screen" src="./public/assets/02.gif"> <img width="100%" alt="booking-screen" src="./public/assets/02.gif">
**Currently Supporting:** You can visit [here](https://platform.openai.com/api-keys) to get your OpenAI API Key.
- OpenAI API: Get OpenAI API Key [here](https://platform.openai.com/api-keys)
- Gemini API: Get Gemini API Key [here](https://aistudio.google.com/apikey)
- Local LLM Ollama & Whisper
### Liquid Glass Design (coming soon) ### Liquid Glass Design (coming soon)
@ -91,44 +88,15 @@ npm run setup
`Ctrl/Cmd + Arrows` : move main window position `Ctrl/Cmd + Arrows` : move main window position
## Repo Activity
![Alt](https://repobeats.axiom.co/api/embed/a23e342faafa84fa8797fa57762885d82fac1180.svg "Repobeats analytics image")
## Contributing ## Contributing
We love contributions! Feel free to open issues for bugs or feature requests. For detailed guide, please see our [contributing guide](/CONTRIBUTING.md). We love contributions! Feel free to open issues for bugs or feature requests.
> Currently, we're working on a full code refactor and modularization. Once that's completed, we'll jump into addressing the major issues.
### Contributors
<a href="https://github.com/pickle-com/glass/graphs/contributors">
<img src="https://contrib.rocks/image?repo=pickle-com/glass" />
</a>
### Help Wanted Issues
We have a list of [help wanted](https://github.com/pickle-com/glass/issues?q=is%3Aissue%20state%3Aopen%20label%3A%22%F0%9F%99%8B%E2%80%8D%E2%99%82%EF%B8%8Fhelp%20wanted%22) that contain small features and bugs which have a relatively limited scope. This is a great place to get started, gain experience, and get familiar with our contribution process.
### 🛠 Current Issues & Improvements
| Status | Issue | Description |
|--------|--------------------------------|---------------------------------------------------|
| 🚧 WIP | Liquid Glass | Liquid Glass UI for MacOS 26 |
### Changelog
- Jul 5: Now support Gemini, Intel Mac supported
- Jul 6: Full code refactoring has done.
- Jul 7: Now support Claude, LLM/STT model selection
- Jul 8: Now support Windows(beta), Improved AEC by Rust(to seperate mic/system audio), shortcut editing(beta)
- Jul 8: Now support Local LLM & STT, Firebase Data Storage
## About Pickle ## About Pickle
**Our mission is to build a living digital clone for everyone.** Glass is part of Step 1—a trusted pipeline that transforms your daily data into a scalable clone. Visit [pickle.com](https://pickle.com) to learn more. **Our mission is to build a living digital clone for everyone.** Glass is part of Step 1—a trusted pipeline that transforms your daily data into a scalable clone. Visit [pickle.com](https://pickle.com) to learn more.
## Star History ## Star History
[![Star History Chart](https://api.star-history.com/svg?repos=pickle-com/glass&type=Date)](https://www.star-history.com/#pickle-com/glass&Date)
<img src="./public/assets/star-history-202574.png">

1
aec

@ -1 +0,0 @@
Subproject commit 9e11f4f95707714464194bdfc9db0222ec5c6163

View File

@ -14,8 +14,8 @@ const baseConfig = {
}; };
const entryPoints = [ const entryPoints = [
{ in: 'src/ui/app/HeaderController.js', out: 'public/build/header' }, { in: 'src/app/HeaderController.js', out: 'public/build/header' },
{ in: 'src/ui/app/PickleGlassApp.js', out: 'public/build/content' }, { in: 'src/app/PickleGlassApp.js', out: 'public/build/content' },
]; ];
async function build() { async function build() {

View File

@ -1,126 +0,0 @@
# Glass: Design Patterns and Architectural Overview
Welcome to the Glass project! This document is the definitive guide to the architectural patterns, conventions, and design philosophy that guide our development. Adhering to these principles is essential for building new features, maintaining the quality of our codebase, and ensuring a stable, consistent developer experience.
The architecture is designed to be modular, robust, and clear, with a strict separation of concerns.
---
## Core Architectural Principles
These are the fundamental rules that govern the entire application.
1. **Centralized Data Logic**: All data persistence logic (reading from or writing to a database) is centralized within the **Electron Main Process**. The UI layers (both Electron's renderer and the web dashboard) are forbidden from accessing data sources directly.
2. **Feature-Based Modularity**: Code is organized by feature (`src/features`) to promote encapsulation and separation of concerns. A new feature should be self-contained within its own directory.
3. **Dual-Database Repositories**: The data access layer uses a **Repository Pattern** that abstracts away the underlying database. Every repository that handles user data **must** have two implementations: one for the local `SQLite` database and one for the cloud `Firebase` database. Both must expose an identical interface.
4. **AI Provider Abstraction**: AI model interactions are abstracted using a **Factory Pattern**. To add a new provider (e.g., a new LLM), you only need to create a new provider module that conforms to the base interface in `src/common/ai/providers/` and register it in the `factory.js`.
5. **Single Source of Truth for Schema**: The schema for the local SQLite database is defined in a single location: `src/common/config/schema.js`. Any change to the database structure **must** be updated here.
6. **Encryption by Default**: All sensitive user data **must** be encrypted before being persisted to Firebase. This includes, but is not limited to, API keys, conversation titles, transcription text, and AI-generated summaries. This is handled automatically by the `createEncryptedConverter` Firestore helper.
---
## I. Electron Application Architecture (`src/`)
This section details the architecture of the core desktop application.
### 1. Overall Pattern: Service-Repository
The Electron app's logic is primarily built on a **Service-Repository** pattern, with the Views being the HTML/JS files in the `src/app` and `src/features` directories.
- **Views** (`*.html`, `*View.js`): The UI layer. Views are responsible for rendering the interface and capturing user interactions. They are intentionally kept "dumb" and delegate all significant logic to a corresponding Service.
- **Services** (`*Service.js`): Services contain the application's business logic. They act as the intermediary between Views and Repositories. For example, `sttService` contains the logic for STT, while `summaryService` handles the logic for generating summaries.
- **Repositories** (`*.repository.js`): Repositories are responsible for all data access. They are the *only* part of the application that directly interacts with `sqliteClient` or `firebaseClient`.
**Location of Modules:**
- **Feature-Specific**: If a service or repository is used by only one feature, it should reside within that feature's directory (e.g., `src/features/listen/summary/summaryService.js`).
- **Common**: If a service or repository is shared across multiple features (like `authService` or `userRepository`), it must be placed in `src/common/services/` or `src/common/repositories/` respectively.
### 2. Data Persistence: The Dual Repository Factory
The application dynamically switches between using the local SQLite database and the cloud-based Firebase Firestore.
- **SQLite**: The default data store for all users, especially those not logged in. This ensures full offline functionality. The low-level client is `src/common/services/sqliteClient.js`.
- **Firebase**: Used exclusively for users who are authenticated. This enables data synchronization across devices and with the web dashboard.
The selection mechanism is a sophisticated **Factory and Adapter Pattern** located in the `index.js` file of each repository directory (e.g., `src/common/repositories/session/index.js`).
**How it works:**
1. **Service Call**: A service makes a call to a high-level repository function, like `sessionRepository.create('ask')`. The service is unaware of the user's state or the underlying database.
2. **Repository Selection (Factory)**: The `index.js` adapter logic first determines which underlying repository to use. It imports and calls `authService.getCurrentUser()` to check the login state. If the user is logged in, it selects `firebase.repository.js`; otherwise, it defaults to `sqlite.repository.js`.
3. **UID Injection (Adapter)**: The adapter then retrieves the current user's ID (`uid`) from `authService.getCurrentUserId()`. It injects this `uid` into the actual, low-level repository call (e.g., `firebaseRepository.create(uid, 'ask')`).
4. **Execution**: The selected repository (`sqlite` or `firebase`) executes the data operation.
This powerful pattern accomplishes two critical goals:
- It makes the services completely agnostic about the underlying data source.
- It frees the services from the responsibility of managing and passing user IDs for every database query.
**Visualizing the Data Flow**
```mermaid
graph TD
subgraph "Electron Main Process"
A -- User Action --> B[Service Layer];
B -- Data Request --> C[Repository Factory];
C -- Check Login Status --> D{Decision};
D -- No --> E[SQLite Repository];
D -- Yes --> F[Firebase Repository];
E -- Access Local DB --> G[(SQLite)];
F -- Access Cloud DB --> H[(Firebase)];
G -- Return Data --> B;
H -- Return Data --> B;
B -- Update UI --> A;
end
style A fill:#D6EAF8,stroke:#3498DB
style G fill:#E8DAEF,stroke:#8E44AD
style H fill:#FADBD8,stroke:#E74C3C
```
---
## II. Web Dashboard Architecture (`pickleglass_web/`)
This section details the architecture of the Next.js web application, which serves as the user-facing dashboard for account management and cloud data viewing.
### 1. Frontend, Backend, and Main Process Communication
The web dashboard has a more complex, three-part architecture:
1. **Next.js Frontend (`app/`):** The React-based user interface.
2. **Node.js Backend (`backend_node/`):** An Express.js server that acts as an intermediary.
3. **Electron Main Process (`src/`):** The ultimate authority for all local data access.
Crucially, **the web dashboard's backend cannot access the local SQLite database directly**. It must communicate with the Electron main process to request data.
### 2. The IPC Data Flow
When the web frontend needs data that resides in the local SQLite database (e.g., viewing a non-synced session), it follows this precise flow:
1. **HTTP Request**: The Next.js frontend makes a standard API call to its own Node.js backend (e.g., `GET /api/conversations`).
2. **IPC Request**: The Node.js backend receives the HTTP request. It **does not** contain any database logic. Instead, it uses the `ipcRequest` helper from `backend_node/ipcBridge.js`.
3. **IPC Emission**: `ipcRequest` sends an event to the Electron main process over an IPC channel (`web-data-request`). It passes three things: the desired action (e.g., `'get-sessions'`), a unique channel name for the response, and a payload.
4. **Main Process Listener**: The Electron main process has a listener (`ipcMain.on('web-data-request', ...)`) that receives this request. It identifies the action and calls the appropriate **Service** or **Repository** to fetch the data from the SQLite database.
5. **IPC Response**: Once the data is retrieved, the main process sends it back to the web backend using the unique response channel provided in the request.
6. **HTTP Response**: The web backend's `ipcRequest` promise resolves with the data, and the backend sends it back to the Next.js frontend as a standard JSON HTTP response.
This round-trip ensures our core principle of centralizing data logic in the main process is never violated.
**Visualizing the IPC Data Flow**
```mermaid
sequenceDiagram
participant FE as Next.js Frontend
participant BE as Node.js Backend
participant Main as Electron Main Process
FE->>+BE: 1. HTTP GET /api/local-data
Note over BE: Receives local data request
BE->>+Main: 2. ipcRequest('get-data', responseChannel)
Note over Main: Receives request, fetches data from SQLite<br/>via Service/Repository
Main-->>-BE: 3. ipcResponse on responseChannel (data)
Note over BE: Receives data, prepares HTTP response
BE-->>-FE: 4. HTTP 200 OK (JSON data)
```

View File

@ -1,19 +0,0 @@
# Refactor Plan: Non-Window Logic Migration from windowManager.js
## Goal
`windowManager.js`를 순수 창 관리 모듈로 만들기 위해 비즈니스 로직을 해당 서비스와 `featureBridge.js`로 이전.
## Steps (based on initial plan)
1. **Shortcuts**: Completed. Logic moved to `shortcutsService.js` and IPC to `featureBridge.js`. Used `internalBridge` for coordination.
2. **Screenshot**: Next. Move `captureScreenshot` function and related IPC handlers from `windowManager.js` to `askService.js` (since it's primarily used there). Update `askService.js` to use its own screenshot method. Add IPC handlers to `featureBridge.js` if needed.
3. **System Permissions**: Create new `permissionService.js` in `src/features/common/services/`. Move all permission-related logic (check, request, open preferences, mark completed, etc.) and IPC handlers from `windowManager.js` to the new service and `featureBridge.js`.
4. **API Key / Model State**: Completely remove from `windowManager.js` (e.g., `setupApiKeyIPC` and helpers). Ensure all usages (e.g., in `askService.js`) directly require and use `modelStateService.js` instead.
## Notes
- Maintain original logic without changes.
- Break circular dependencies if found.
- Use `internalBridge` for inter-module communication where appropriate.
- After each step, verify no errors and test functionality.

View File

@ -8,73 +8,29 @@ productName: Glass
# Publish configuration for GitHub releases # Publish configuration for GitHub releases
publish: publish:
provider: github provider: github
owner: pickle-com owner: pickle-com
repo: glass repo: glass
releaseType: draft releaseType: draft
# Protocols configuration for deep linking
protocols:
name: PickleGlass Protocol
schemes:
- pickleglass
# List of files to be included in the app package # List of files to be included in the app package
files: files:
- src/**/* - src/**/*
- package.json - package.json
- pickleglass_web/backend_node/**/* - pickleglass_web/backend_node/**/*
- '!**/node_modules/electron/**' - '!**/node_modules/electron/**'
- public/build/**/* - public/build/**/*
# Additional resources to be copied into the app's resources directory # Additional resources to be copied into the app's resources directory
extraResources: extraResources:
- from: pickleglass_web/out - from: src/assets/SystemAudioDump
to: out to: SystemAudioDump
- from: pickleglass_web/out
asarUnpack: to: out
- "src/ui/assets/SystemAudioDump"
- "**/node_modules/sharp/**/*"
- "**/node_modules/@img/**/*"
# Windows configuration
win:
icon: src/ui/assets/logo.ico
target:
- target: nsis
arch: x64
- target: portable
arch: x64
requestedExecutionLevel: asInvoker
signAndEditExecutable: true
cscLink: build\certs\glass-dev.pfx
cscKeyPassword: "${env.CSC_KEY_PASSWORD}"
signtoolOptions:
certificateSubjectName: "Glass Dev Code Signing"
# NSIS installer configuration for Windows
nsis:
oneClick: false
perMachine: false
allowToChangeInstallationDirectory: true
deleteAppDataOnUninstall: true
createDesktopShortcut: always
createStartMenuShortcut: true
shortcutName: Glass
# macOS specific configuration # macOS specific configuration
mac: mac:
# The application category type # The application category type
category: public.app-category.utilities category: public.app-category.utilities
# Path to the .icns icon file # Path to the .icns icon file
icon: src/ui/assets/logo.icns icon: src/assets/logo.icns
# Minimum macOS version (supports both Intel and Apple Silicon)
minimumSystemVersion: '11.0'
hardenedRuntime: true
entitlements: entitlements.plist
entitlementsInherit: entitlements.plist
target:
- target: dmg
arch: universal
- target: zip
arch: universal

86
forge.config.js Normal file
View File

@ -0,0 +1,86 @@
const { FusesPlugin } = require('@electron-forge/plugin-fuses');
const { FuseV1Options, FuseVersion } = require('@electron/fuses');
const { notarizeApp } = require('./notarize');
module.exports = {
packagerConfig: {
asar: {
unpack:
'**/*.node,**/*.dylib,' +
'**/node_modules/{sharp,@img}/**/*'
},
extraResource: ['./src/assets/SystemAudioDump', './pickleglass_web/out'],
name: 'Glass',
icon: 'src/assets/logo',
appBundleId: 'com.pickle.glass',
protocols: [
{
name: 'PickleGlass Protocol',
schemes: ['pickleglass']
}
],
asarUnpack: [
"**/*.node",
"**/*.dylib",
"node_modules/@img/sharp-darwin-arm64/**",
"node_modules/@img/sharp-libvips-darwin-arm64/**"
],
osxSign: {
identity: process.env.APPLE_SIGNING_IDENTITY,
'hardened-runtime': true,
entitlements: 'entitlements.plist',
'entitlements-inherit': 'entitlements.plist',
},
osxNotarize: {
tool: 'notarytool',
appleId: process.env.APPLE_ID,
appleIdPassword: process.env.APPLE_ID_PASSWORD,
teamId: process.env.APPLE_TEAM_ID
}
},
rebuildConfig: {},
makers: [
{
name: '@electron-forge/maker-squirrel',
config: {
name: 'pickle-glass',
productName: 'Glass',
shortcutName: 'Glass',
createDesktopShortcut: true,
createStartMenuShortcut: true,
},
},
{
name: '@electron-forge/maker-dmg',
platforms: ['darwin'],
},
{
name: '@electron-forge/maker-deb',
config: {},
},
{
name: '@electron-forge/maker-rpm',
config: {},
},
],
hooks: {
afterSign: async (context, forgeConfig, platform, arch, appPath) => {
await notarizeApp(context, forgeConfig, platform, arch, appPath);
},
},
plugins: [
{
name: '@electron-forge/plugin-auto-unpack-natives',
config: {},
},
new FusesPlugin({
version: FuseVersion.V1,
[FuseV1Options.RunAsNode]: false,
[FuseV1Options.EnableCookieEncryption]: true,
[FuseV1Options.EnableNodeOptionsEnvironmentVariable]: false,
[FuseV1Options.EnableNodeCliInspectArguments]: false,
[FuseV1Options.EnableEmbeddedAsarIntegrityValidation]: true,
[FuseV1Options.OnlyLoadAppFromAsar]: false,
}),
],
};

View File

@ -53,6 +53,7 @@ const authCallbackHandler = (request, response) => {
} }
const idToken = request.body.token; const idToken = request.body.token;
logger.info("Received token:", idToken.substring(0, 20) + "...");
const decodedToken = await admin.auth().verifyIdToken(idToken); const decodedToken = await admin.auth().verifyIdToken(idToken);
const uid = decodedToken.uid; const uid = decodedToken.uid;

7725
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -1,22 +1,19 @@
{ {
"name": "pickle-glass", "name": "pickle-glass",
"productName": "Glass", "productName": "Glass",
"version": "0.2.4", "version": "0.1.1",
"description": "Cl*ely for Free", "description": "Cl*ely for Free",
"main": "src/index.js", "main": "src/index.js",
"scripts": { "scripts": {
"setup": "npm install && cd pickleglass_web && npm install && npm run build && cd .. && npm start", "setup": "npm install && cd pickleglass_web && npm install && npm run build && cd .. && npm start",
"start": "npm run build:renderer && electron .", "start": "npm run build:renderer && electron-forge start",
"package": "npm run build:all && electron-builder --dir", "package": "npm run build:renderer && electron-forge package",
"make": "npm run build:renderer && electron-forge make", "make": "npm run build:renderer && electron-forge make",
"build": "npm run build:all && electron-builder --config electron-builder.yml --publish never", "build": "npm run build:renderer && electron-builder --config electron-builder.yml --publish never",
"build:win": "npm run build:all && electron-builder --win --x64 --publish never", "publish": "npm run build:renderer && electron-builder --config electron-builder.yml --publish always",
"publish": "npm run build:all && electron-builder --config electron-builder.yml --publish always",
"lint": "eslint --ext .ts,.tsx,.js .", "lint": "eslint --ext .ts,.tsx,.js .",
"postinstall": "electron-builder install-app-deps", "postinstall": "electron-builder install-app-deps",
"build:renderer": "node build.js", "build:renderer": "node build.js",
"build:web": "cd pickleglass_web && npm run build && cd ..",
"build:all": "npm run build:renderer && npm run build:web",
"watch:renderer": "node build.js --watch" "watch:renderer": "node build.js --watch"
}, },
"keywords": [ "keywords": [
@ -32,14 +29,11 @@
}, },
"license": "GPL-3.0", "license": "GPL-3.0",
"dependencies": { "dependencies": {
"@anthropic-ai/sdk": "^0.56.0",
"@deepgram/sdk": "^4.9.1",
"@google/genai": "^1.8.0",
"@google/generative-ai": "^0.24.1",
"axios": "^1.10.0", "axios": "^1.10.0",
"better-sqlite3": "^9.6.0", "better-sqlite3": "^9.4.3",
"cors": "^2.8.5", "cors": "^2.8.5",
"dotenv": "^17.0.0", "dotenv": "^17.0.0",
"electron-deeplink": "^1.0.10",
"electron-squirrel-startup": "^1.0.1", "electron-squirrel-startup": "^1.0.1",
"electron-store": "^8.2.0", "electron-store": "^8.2.0",
"electron-updater": "^6.6.2", "electron-updater": "^6.6.2",
@ -47,26 +41,29 @@
"firebase": "^11.10.0", "firebase": "^11.10.0",
"firebase-admin": "^13.4.0", "firebase-admin": "^13.4.0",
"jsonwebtoken": "^9.0.2", "jsonwebtoken": "^9.0.2",
"keytar": "^7.9.0",
"node-fetch": "^2.7.0", "node-fetch": "^2.7.0",
"openai": "^4.70.0", "openai": "^4.70.0",
"portkey-ai": "^1.10.1",
"react-hot-toast": "^2.5.2", "react-hot-toast": "^2.5.2",
"sharp": "^0.34.2", "sharp": "^0.34.2",
"sqlite3": "^5.1.7",
"validator": "^13.11.0", "validator": "^13.11.0",
"wait-on": "^8.0.3", "wait-on": "^8.0.3",
"ws": "^8.18.0" "ws": "^8.18.0"
}, },
"devDependencies": { "devDependencies": {
"@electron-forge/cli": "^7.8.1",
"@electron-forge/maker-deb": "^7.8.1",
"@electron-forge/maker-dmg": "^7.8.1",
"@electron-forge/maker-rpm": "^7.8.1",
"@electron-forge/maker-squirrel": "^7.8.1",
"@electron-forge/maker-zip": "^7.8.1",
"@electron-forge/plugin-auto-unpack-natives": "^7.8.1",
"@electron-forge/plugin-fuses": "^7.8.1",
"@electron/fuses": "^1.8.0", "@electron/fuses": "^1.8.0",
"@electron/notarize": "^2.5.0", "@electron/notarize": "^2.5.0",
"electron": "^30.5.1", "electron": "^30.5.1",
"electron-builder": "^26.0.12", "electron-builder": "^26.0.12",
"electron-reloader": "^1.2.3", "electron-reloader": "^1.2.3",
"esbuild": "^0.25.5", "esbuild": "^0.25.5"
"prettier": "^3.6.2"
},
"optionalDependencies": {
"electron-liquid-glass": "^1.0.1"
} }
} }

View File

@ -0,0 +1,17 @@
FROM python:3.11-slim
WORKDIR /app
RUN apt-get update && apt-get install -y \
gcc \
&& rm -rf /var/lib/apt/lists/*
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY backend/ .
EXPOSE 8000
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000", "--reload"]

View File

@ -0,0 +1,15 @@
FROM node:18-alpine
WORKDIR /app
COPY package*.json ./
RUN npm ci --only=production
COPY . .
RUN npm run build
EXPOSE 3000
CMD ["npm", "start"]

View File

@ -2,7 +2,7 @@
import { useState, useEffect, Suspense } from 'react' import { useState, useEffect, Suspense } from 'react'
import { useRedirectIfNotAuth } from '@/utils/auth' import { useRedirectIfNotAuth } from '@/utils/auth'
import { useSearchParams, useRouter } from 'next/navigation' import { useSearchParams } from 'next/navigation'
import Link from 'next/link' import Link from 'next/link'
import { import {
UserProfile, UserProfile,
@ -10,7 +10,6 @@ import {
Transcript, Transcript,
AiMessage, AiMessage,
getSessionDetails, getSessionDetails,
deleteSession,
} from '@/utils/api' } from '@/utils/api'
type ConversationItem = (Transcript & { type: 'transcript' }) | (AiMessage & { type: 'ai_message' }); type ConversationItem = (Transcript & { type: 'transcript' }) | (AiMessage & { type: 'ai_message' });
@ -30,8 +29,6 @@ function SessionDetailsContent() {
const [isLoading, setIsLoading] = useState(true); const [isLoading, setIsLoading] = useState(true);
const searchParams = useSearchParams(); const searchParams = useSearchParams();
const sessionId = searchParams.get('sessionId'); const sessionId = searchParams.get('sessionId');
const router = useRouter();
const [deleting, setDeleting] = useState(false);
useEffect(() => { useEffect(() => {
if (userInfo && sessionId) { if (userInfo && sessionId) {
@ -50,20 +47,6 @@ function SessionDetailsContent() {
} }
}, [userInfo, sessionId]); }, [userInfo, sessionId]);
const handleDelete = async () => {
if (!sessionId) return;
if (!window.confirm('Are you sure you want to delete this activity? This cannot be undone.')) return;
setDeleting(true);
try {
await deleteSession(sessionId);
router.push('/activity');
} catch (error) {
alert('Failed to delete activity.');
setDeleting(false);
console.error(error);
}
};
if (!userInfo || isLoading) { if (!userInfo || isLoading) {
return ( return (
<div className="min-h-screen bg-[#FDFCF9] flex items-center justify-center"> <div className="min-h-screen bg-[#FDFCF9] flex items-center justify-center">
@ -89,7 +72,12 @@ function SessionDetailsContent() {
) )
} }
const askMessages = sessionDetails.ai_messages || []; const combinedConversation = [
...sessionDetails.transcripts.map(t => ({ ...t, type: 'transcript' as const, created_at: t.start_at })),
...sessionDetails.ai_messages.map(m => ({ ...m, type: 'ai_message' as const, created_at: m.sent_at }))
].sort((a, b) => (a.created_at || 0) - (b.created_at || 0));
const audioTranscripts = sessionDetails.transcripts.filter(t => t.speaker !== 'Me');
return ( return (
<div className="min-h-screen bg-[#FDFCF9] text-gray-800"> <div className="min-h-screen bg-[#FDFCF9] text-gray-800">
@ -103,82 +91,42 @@ function SessionDetailsContent() {
</Link> </Link>
</div> </div>
<div className="bg-white p-8 rounded-xl shadow-md border border-gray-100"> <div className="bg-white p-8 rounded-xl">
<div className="mb-8 flex flex-col sm:flex-row sm:items-center sm:justify-between gap-4"> <div className="mb-6">
<div> <h1 className="text-2xl font-bold text-gray-900 mb-2">
<h1 className="text-2xl font-bold text-gray-900 mb-2"> {sessionDetails.session.title || `Conversation on ${new Date(sessionDetails.session.started_at * 1000).toLocaleDateString()}`}
{sessionDetails.session.title || `Conversation on ${new Date(sessionDetails.session.started_at * 1000).toLocaleDateString()}`} </h1>
</h1> <div className="flex items-center text-sm text-gray-500 space-x-4">
<div className="flex items-center text-sm text-gray-500 space-x-4"> <span>{new Date(sessionDetails.session.started_at * 1000).toLocaleDateString('en-US', { month: 'long', day: 'numeric', year: 'numeric' })}</span>
<span>{new Date(sessionDetails.session.started_at * 1000).toLocaleDateString('en-US', { month: 'long', day: 'numeric', year: 'numeric' })}</span> <span>{new Date(sessionDetails.session.started_at * 1000).toLocaleTimeString('en-US', { hour: 'numeric', minute: '2-digit', hour12: true })}</span>
<span>{new Date(sessionDetails.session.started_at * 1000).toLocaleTimeString('en-US', { hour: 'numeric', minute: '2-digit', hour12: true })}</span>
<span className={`capitalize px-2 py-0.5 rounded-full text-xs font-medium ${sessionDetails.session.session_type === 'listen' ? 'bg-blue-100 text-blue-800' : 'bg-green-100 text-green-800'}`}>
{sessionDetails.session.session_type}
</span>
</div>
</div> </div>
<button
onClick={handleDelete}
disabled={deleting}
className={`px-4 py-2 rounded text-sm font-medium border border-red-200 text-red-700 bg-red-50 hover:bg-red-100 transition-colors ${deleting ? 'opacity-50 cursor-not-allowed' : ''}`}
>
{deleting ? 'Deleting...' : 'Delete Activity'}
</button>
</div> </div>
{sessionDetails.summary && ( {sessionDetails.summary && (
<Section title="Summary"> <Section title="Summary">
<p className="text-lg italic text-gray-600 mb-4">"{sessionDetails.summary.tldr}"</p> <p className="italic">"{sessionDetails.summary.tldr}"</p>
{sessionDetails.summary.bullet_json && JSON.parse(sessionDetails.summary.bullet_json).length > 0 &&
<div className="mt-4">
<h3 className="font-semibold text-gray-700 mb-2">Key Points:</h3>
<ul className="list-disc list-inside space-y-1 text-gray-600">
{JSON.parse(sessionDetails.summary.bullet_json).map((point: string, index: number) => (
<li key={index}>{point}</li>
))}
</ul>
</div>
}
{sessionDetails.summary.action_json && JSON.parse(sessionDetails.summary.action_json).length > 0 &&
<div className="mt-4">
<h3 className="font-semibold text-gray-700 mb-2">Action Items:</h3>
<ul className="list-disc list-inside space-y-1 text-gray-600">
{JSON.parse(sessionDetails.summary.action_json).map((action: string, index: number) => (
<li key={index}>{action}</li>
))}
</ul>
</div>
}
</Section> </Section>
)} )}
{sessionDetails.transcripts && sessionDetails.transcripts.length > 0 && ( <Section title="Notes">
<Section title="Listen: Transcript"> {combinedConversation.map((item) => (
<div className="space-y-3"> <p key={item.id}>
{sessionDetails.transcripts.map((item) => ( <span className="font-semibold">{(item.type === 'transcript' && item.speaker === 'Me') || (item.type === 'ai_message' && item.role === 'user') ? 'You: ' : 'AI: '}</span>
<p key={item.id} className="text-gray-700"> {item.type === 'transcript' ? item.text : item.content}
<span className="font-semibold capitalize">{item.speaker}: </span> </p>
{item.text} ))}
</p> {combinedConversation.length === 0 && <p>No notes recorded for this session.</p>}
))} </Section>
</div>
</Section>
)}
{askMessages.length > 0 && ( <Section title="Audio transcript content">
<Section title="Ask: Q&A"> {audioTranscripts.length > 0 ? (
<div className="space-y-4"> <ul className="list-disc list-inside space-y-1">
{askMessages.map((item) => ( {audioTranscripts.map(t => <li key={t.id}>{t.text}</li>)}
<div key={item.id} className={`p-3 rounded-lg ${item.role === 'user' ? 'bg-gray-100' : 'bg-blue-50'}`}> </ul>
<p className="font-semibold capitalize text-sm text-gray-600 mb-1">{item.role === 'user' ? 'You' : 'AI'}</p> ) : (
<p className="text-gray-800 whitespace-pre-wrap">{item.content}</p> <p>No audio transcript available for this session.</p>
</div> )}
))} </Section>
</div>
</Section>
)}
</div> </div>
</div> </div>
</div> </div>

View File

@ -7,14 +7,12 @@ import {
UserProfile, UserProfile,
Session, Session,
getSessions, getSessions,
deleteSession,
} from '@/utils/api' } from '@/utils/api'
export default function ActivityPage() { export default function ActivityPage() {
const userInfo = useRedirectIfNotAuth() as UserProfile | null; const userInfo = useRedirectIfNotAuth() as UserProfile | null;
const [sessions, setSessions] = useState<Session[]>([]) const [sessions, setSessions] = useState<Session[]>([])
const [isLoading, setIsLoading] = useState(true) const [isLoading, setIsLoading] = useState(true)
const [deletingId, setDeletingId] = useState<string | null>(null)
const fetchSessions = async () => { const fetchSessions = async () => {
try { try {
@ -49,20 +47,6 @@ export default function ActivityPage() {
return 'Good evening' return 'Good evening'
} }
const handleDelete = async (sessionId: string) => {
if (!window.confirm('Are you sure you want to delete this activity? This cannot be undone.')) return;
setDeletingId(sessionId);
try {
await deleteSession(sessionId);
setSessions(sessions => sessions.filter(s => s.id !== sessionId));
} catch (error) {
alert('Failed to delete activity.');
console.error(error);
} finally {
setDeletingId(null);
}
}
return ( return (
<div className="min-h-screen bg-gray-50"> <div className="min-h-screen bg-gray-50">
<div className="max-w-4xl mx-auto px-8 py-12"> <div className="max-w-4xl mx-auto px-8 py-12">
@ -83,28 +67,17 @@ export default function ActivityPage() {
) : sessions.length > 0 ? ( ) : sessions.length > 0 ? (
<div className="space-y-4"> <div className="space-y-4">
{sessions.map((session) => ( {sessions.map((session) => (
<div key={session.id} className="block bg-white rounded-lg p-6 shadow-sm border border-gray-200 hover:shadow-md transition-shadow cursor-pointer"> <Link href={`/activity/details?sessionId=${session.id}`} key={session.id} className="block bg-white rounded-lg p-6 shadow-sm border border-gray-200 hover:shadow-md transition-shadow cursor-pointer">
<div className="flex justify-between items-start mb-3"> <div className="flex justify-between items-start mb-3">
<div> <h3 className="text-lg font-medium text-gray-900">{session.title || `Conversation - ${new Date(session.started_at * 1000).toLocaleDateString()}`}</h3>
<Link href={`/activity/details?sessionId=${session.id}`} className="text-lg font-medium text-gray-900 hover:underline"> <span className="text-sm text-gray-500">
{session.title || `Conversation - ${new Date(session.started_at * 1000).toLocaleDateString()}`} {new Date(session.started_at * 1000).toLocaleString()}
</Link> </span>
<div className="text-sm text-gray-500">
{new Date(session.started_at * 1000).toLocaleString()}
</div>
</div>
<button
onClick={() => handleDelete(session.id)}
disabled={deletingId === session.id}
className={`ml-4 px-3 py-1 rounded text-xs font-medium border border-red-200 text-red-700 bg-red-50 hover:bg-red-100 transition-colors ${deletingId === session.id ? 'opacity-50 cursor-not-allowed' : ''}`}
>
{deletingId === session.id ? 'Deleting...' : 'Delete'}
</button>
</div> </div>
<span className={`capitalize inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium ${session.session_type === 'listen' ? 'bg-blue-100 text-blue-800' : 'bg-green-100 text-green-800'}`}> <span className="inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium bg-blue-100 text-blue-800">
{session.session_type || 'ask'} Conversation
</span> </span>
</div> </Link>
))} ))}
</div> </div>
) : ( ) : (

View File

@ -43,10 +43,9 @@ export default function LoginPage() {
window.location.href = deepLinkUrl window.location.href = deepLinkUrl
// Maybe we don't need this setTimeout(() => {
// setTimeout(() => { alert('Login completed. Please return to Pickle Glass app.')
// alert('Login completed. Please return to Pickle Glass app.') }, 1000)
// }, 1000)
} catch (error) { } catch (error) {
console.error('❌ Deep link processing failed:', error) console.error('❌ Deep link processing failed:', error)

View File

@ -1,8 +1,8 @@
'use client' 'use client'
import { useState, useEffect } from 'react' import { useState, useEffect } from 'react'
import { ChevronDown, Plus, Copy } from 'lucide-react' import { ChevronDown } from 'lucide-react'
import { getPresets, updatePreset, createPreset, PromptPreset } from '@/utils/api' import { getPresets, updatePreset, PromptPreset } from '@/utils/api'
export default function PersonalizePage() { export default function PersonalizePage() {
const [allPresets, setAllPresets] = useState<PromptPreset[]>([]); const [allPresets, setAllPresets] = useState<PromptPreset[]>([]);
@ -72,6 +72,7 @@ export default function PersonalizePage() {
) )
); );
setIsDirty(false); setIsDirty(false);
console.log('Save completed!');
} catch (error) { } catch (error) {
console.error("Save failed:", error); console.error("Save failed:", error);
alert("Failed to save preset. See console for details."); alert("Failed to save preset. See console for details.");
@ -80,73 +81,6 @@ export default function PersonalizePage() {
} }
}; };
const handleCreateNewPreset = async () => {
const title = prompt("Enter a title for the new preset:");
if (!title) return;
try {
setSaving(true);
const { id } = await createPreset({
title,
prompt: "Enter your custom prompt here..."
});
const newPreset: PromptPreset = {
id,
uid: 'current_user',
title,
prompt: "Enter your custom prompt here...",
is_default: 0,
created_at: Date.now(),
sync_state: 'clean'
};
setAllPresets(prev => [...prev, newPreset]);
setSelectedPreset(newPreset);
setEditorContent(newPreset.prompt);
setIsDirty(false);
} catch (error) {
console.error("Failed to create preset:", error);
alert("Failed to create preset. See console for details.");
} finally {
setSaving(false);
}
};
const handleDuplicatePreset = async () => {
if (!selectedPreset) return;
const title = prompt("Enter a title for the duplicated preset:", `${selectedPreset.title} (Copy)`);
if (!title) return;
try {
setSaving(true);
const { id } = await createPreset({
title,
prompt: editorContent
});
const newPreset: PromptPreset = {
id,
uid: 'current_user',
title,
prompt: editorContent,
is_default: 0,
created_at: Date.now(),
sync_state: 'clean'
};
setAllPresets(prev => [...prev, newPreset]);
setSelectedPreset(newPreset);
setIsDirty(false);
} catch (error) {
console.error("Failed to duplicate preset:", error);
alert("Failed to duplicate preset. See console for details.");
} finally {
setSaving(false);
}
};
if (loading) { if (loading) {
return ( return (
<div className="flex items-center justify-center h-full"> <div className="flex items-center justify-center h-full">
@ -164,39 +98,19 @@ export default function PersonalizePage() {
<p className="text-sm text-gray-500 mb-2">Presets</p> <p className="text-sm text-gray-500 mb-2">Presets</p>
<h1 className="text-3xl font-bold text-gray-900">Personalize</h1> <h1 className="text-3xl font-bold text-gray-900">Personalize</h1>
</div> </div>
<div className="flex gap-2"> <button
<button onClick={handleSave}
onClick={handleCreateNewPreset} disabled={saving || !isDirty || selectedPreset?.is_default === 1}
disabled={saving} className={`px-4 py-2 rounded-md text-sm font-medium transition-all duration-200 ${
className="px-4 py-2 rounded-md text-sm font-medium transition-all duration-200 bg-blue-600 text-white hover:bg-blue-700 disabled:bg-gray-400 disabled:cursor-not-allowed flex items-center gap-2" !isDirty && !saving
> ? 'bg-gray-500 text-white cursor-default'
<Plus className="h-4 w-4" /> : saving
New Preset ? 'bg-gray-400 text-white cursor-not-allowed'
</button> : 'bg-gray-600 text-white hover:bg-gray-700'
{selectedPreset && ( }`}
<button >
onClick={handleDuplicatePreset} {!isDirty && !saving ? 'Saved' : saving ? 'Saving...' : 'Save'}
disabled={saving} </button>
className="px-4 py-2 rounded-md text-sm font-medium transition-all duration-200 bg-green-600 text-white hover:bg-green-700 disabled:bg-gray-400 disabled:cursor-not-allowed flex items-center gap-2"
>
<Copy className="h-4 w-4" />
Duplicate
</button>
)}
<button
onClick={handleSave}
disabled={saving || !isDirty || selectedPreset?.is_default === 1}
className={`px-4 py-2 rounded-md text-sm font-medium transition-all duration-200 ${
!isDirty && !saving
? 'bg-gray-500 text-white cursor-default'
: saving
? 'bg-gray-400 text-white cursor-not-allowed'
: 'bg-gray-600 text-white hover:bg-gray-700'
}`}
>
{!isDirty && !saving ? 'Saved' : saving ? 'Saving...' : 'Save'}
</button>
</div>
</div> </div>
</div> </div>
</div> </div>
@ -223,18 +137,13 @@ export default function PersonalizePage() {
onClick={() => handlePresetClick(preset)} onClick={() => handlePresetClick(preset)}
className={` className={`
p-4 rounded-lg cursor-pointer transition-all duration-200 bg-white p-4 rounded-lg cursor-pointer transition-all duration-200 bg-white
h-48 flex flex-col shadow-sm hover:shadow-md relative h-48 flex flex-col shadow-sm hover:shadow-md
${selectedPreset?.id === preset.id ${selectedPreset?.id === preset.id
? 'border-2 border-blue-500 shadow-md' ? 'border-2 border-blue-500 shadow-md'
: 'border border-gray-200 hover:border-gray-300' : 'border border-gray-200 hover:border-gray-300'
} }
`} `}
> >
{preset.is_default === 1 && (
<div className="absolute top-2 right-2 bg-yellow-100 text-yellow-800 text-xs px-2 py-1 rounded-full">
Default
</div>
)}
<h3 className="font-semibold text-gray-900 mb-3 text-center text-sm"> <h3 className="font-semibold text-gray-900 mb-3 text-center text-sm">
{preset.title} {preset.title}
</h3> </h3>
@ -249,24 +158,12 @@ export default function PersonalizePage() {
</div> </div>
<div className="flex-1 bg-white"> <div className="flex-1 bg-white">
<div className="h-full px-8 py-6 flex flex-col"> <div className="h-full px-8 py-6">
{selectedPreset?.is_default === 1 && (
<div className="mb-4 p-4 bg-yellow-50 border border-yellow-200 rounded-lg">
<div className="flex items-center gap-2">
<div className="w-4 h-4 bg-yellow-400 rounded-full"></div>
<p className="text-sm text-yellow-800">
<strong>This is a default preset and cannot be edited.</strong>
Use the "Duplicate" button above to create an editable copy, or create a new preset.
</p>
</div>
</div>
)}
<textarea <textarea
value={editorContent} value={editorContent}
onChange={handleEditorChange} onChange={handleEditorChange}
className="w-full flex-1 text-sm text-gray-900 border-0 resize-none focus:outline-none bg-transparent font-mono leading-relaxed" className="w-full h-full text-sm text-gray-900 border-0 resize-none focus:outline-none bg-transparent font-mono leading-relaxed"
placeholder="Select a preset or type directly..." placeholder="Select a preset or type directly..."
readOnly={selectedPreset?.is_default === 1}
/> />
</div> </div>
</div> </div>

View File

@ -0,0 +1,103 @@
const path = require('path');
const databaseInitializer = require('../../src/common/services/databaseInitializer');
const Database = require('better-sqlite3');
const dbPath = databaseInitializer.getDatabasePath();
const db = new Database(dbPath);
db.pragma('journal_mode = WAL');
db.exec(`
-- users
CREATE TABLE IF NOT EXISTS users (
uid TEXT PRIMARY KEY,
display_name TEXT NOT NULL,
email TEXT NOT NULL,
created_at INTEGER,
api_key TEXT
);
-- sessions
CREATE TABLE IF NOT EXISTS sessions (
id TEXT PRIMARY KEY,
uid TEXT NOT NULL,
title TEXT,
started_at INTEGER,
ended_at INTEGER,
sync_state TEXT DEFAULT 'clean',
updated_at INTEGER
);
-- transcripts
CREATE TABLE IF NOT EXISTS transcripts (
id TEXT PRIMARY KEY,
session_id TEXT NOT NULL,
start_at INTEGER,
end_at INTEGER,
speaker TEXT,
text TEXT,
lang TEXT,
created_at INTEGER,
sync_state TEXT DEFAULT 'clean'
);
-- ai_messages
CREATE TABLE IF NOT EXISTS ai_messages (
id TEXT PRIMARY KEY,
session_id TEXT NOT NULL,
sent_at INTEGER,
role TEXT,
content TEXT,
tokens INTEGER,
model TEXT,
created_at INTEGER,
sync_state TEXT DEFAULT 'clean'
);
-- summaries
CREATE TABLE IF NOT EXISTS summaries (
session_id TEXT PRIMARY KEY,
generated_at INTEGER,
model TEXT,
text TEXT,
tldr TEXT,
bullet_json TEXT,
action_json TEXT,
tokens_used INTEGER,
updated_at INTEGER,
sync_state TEXT DEFAULT 'clean'
);
-- prompt_presets
CREATE TABLE IF NOT EXISTS prompt_presets (
id TEXT PRIMARY KEY,
uid TEXT NOT NULL,
title TEXT NOT NULL,
prompt TEXT NOT NULL,
is_default INTEGER NOT NULL,
created_at INTEGER,
sync_state TEXT DEFAULT 'clean'
);
`);
const defaultPresets = [
['school', 'School', 'You are a school and lecture assistant. Your goal is to help the user, a student, understand academic material and answer questions.\n\nWhenever a question appears on the user\'s screen or is asked aloud, you provide a direct, step-by-step answer, showing all necessary reasoning or calculations.\n\nIf the user is watching a lecture or working through new material, you offer concise explanations of key concepts and clarify definitions as they come up.', 1],
['meetings', 'Meetings', 'You are a meeting assistant. Your goal is to help the user capture key information during meetings and follow up effectively.\n\nYou help capture meeting notes, track action items, identify key decisions, and summarize important points discussed during meetings.', 1],
['sales', 'Sales', 'You are a real-time AI sales assistant, and your goal is to help the user close deals during sales interactions.\n\nYou provide real-time sales support, suggest responses to objections, help identify customer needs, and recommend strategies to advance deals.', 1],
['recruiting', 'Recruiting', 'You are a recruiting assistant. Your goal is to help the user interview candidates and evaluate talent effectively.\n\nYou help evaluate candidates, suggest interview questions, analyze responses, and provide insights about candidate fit for positions.', 1],
['customer-support', 'Customer Support', 'You are a customer support assistant. Your goal is to help resolve customer issues efficiently and thoroughly.\n\nYou help diagnose customer problems, suggest solutions, provide step-by-step troubleshooting guidance, and ensure customer satisfaction.', 1],
];
const stmt = db.prepare(`
INSERT OR IGNORE INTO prompt_presets (id, uid, title, prompt, is_default, created_at)
VALUES (@id, 'default_user', @title, @prompt, @is_default, strftime('%s','now'));
`);
db.transaction(() => defaultPresets.forEach(([id, title, prompt, is_default]) => stmt.run({ id, title, prompt, is_default })))();
const defaultUserStmt = db.prepare(`
INSERT OR IGNORE INTO users (uid, display_name, email, created_at)
VALUES ('default_user', 'Default User', 'contact@pickle.com', strftime('%s','now'));
`);
defaultUserStmt.run();
module.exports = db;

View File

@ -1,9 +1,9 @@
const express = require('express'); const express = require('express');
const cors = require('cors'); const cors = require('cors');
// const db = require('./db'); // No longer needed const db = require('./db');
const { identifyUser } = require('./middleware/auth'); const { identifyUser } = require('./middleware/auth');
function createApp(eventBridge) { function createApp() {
const app = express(); const app = express();
const webUrl = process.env.pickleglass_WEB_URL || 'http://localhost:3000'; const webUrl = process.env.pickleglass_WEB_URL || 'http://localhost:3000';
@ -20,11 +20,6 @@ function createApp(eventBridge) {
res.json({ message: "pickleglass API is running" }); res.json({ message: "pickleglass API is running" });
}); });
app.use((req, res, next) => {
req.bridge = eventBridge;
next();
});
app.use('/api', identifyUser); app.use('/api', identifyUser);
app.use('/api/auth', require('./routes/auth')); app.use('/api/auth', require('./routes/auth'));

View File

@ -1,35 +0,0 @@
const crypto = require('crypto');
function ipcRequest(req, channel, payload) {
return new Promise((resolve, reject) => {
// Immediately check bridge status and fail if it's not available.
if (!req.bridge || typeof req.bridge.emit !== 'function') {
reject(new Error('IPC bridge is not available'));
return;
}
const responseChannel = `${channel}-${crypto.randomUUID()}`;
req.bridge.once(responseChannel, (response) => {
if (!response) {
reject(new Error(`No response received from ${channel}`));
return;
}
if (response.success) {
resolve(response.data);
} else {
reject(new Error(response.error || `IPC request to ${channel} failed`));
}
});
try {
req.bridge.emit('web-data-request', channel, responseChannel, payload);
} catch (error) {
req.bridge.removeAllListeners(responseChannel);
reject(new Error(`Failed to emit IPC request: ${error.message}`));
}
});
}
module.exports = { ipcRequest };

View File

@ -0,0 +1,13 @@
const jwt = require('jsonwebtoken');
const SECRET = process.env.JWT_SECRET_KEY || 'change-me';
const EXPIRE = 60 * 24; // minutes
exports.sign = (sub, extra = {}) => jwt.sign({ sub, ...extra }, SECRET, { algorithm: 'HS256', expiresIn: `${EXPIRE}m` });
exports.verify = token => {
try {
return jwt.verify(token, SECRET).sub;
} catch {
return null;
}
};

View File

@ -1,3 +1,5 @@
const { verify } = require('../jwt');
function identifyUser(req, res, next) { function identifyUser(req, res, next) {
const userId = req.get('X-User-ID'); const userId = req.get('X-User-ID');

View File

@ -1,24 +1,19 @@
const express = require('express'); const express = require('express');
const db = require('../db');
const router = express.Router(); const router = express.Router();
const { ipcRequest } = require('../ipcBridge');
router.get('/status', async (req, res) => { router.get('/status', (req, res) => {
try { const user = db.prepare('SELECT uid, display_name FROM users WHERE uid = ?').get('default_user');
const user = await ipcRequest(req, 'get-user-profile'); if (!user) {
if (!user) { return res.status(500).json({ error: 'Default user not initialized' });
return res.status(500).json({ error: 'Default user not initialized' });
}
res.json({
authenticated: true,
user: {
id: user.uid,
name: user.display_name
}
});
} catch (error) {
console.error('Failed to get auth status via IPC:', error);
res.status(500).json({ error: 'Failed to retrieve auth status' });
} }
res.json({
authenticated: true,
user: {
id: user.uid,
name: user.display_name
}
});
}); });
module.exports = router; module.exports = router;

View File

@ -1,54 +1,121 @@
const express = require('express'); const express = require('express');
const db = require('../db');
const router = express.Router(); const router = express.Router();
const { ipcRequest } = require('../ipcBridge'); const crypto = require('crypto');
const validator = require('validator');
router.get('/', async (req, res) => { router.get('/', (req, res) => {
try { try {
const sessions = await ipcRequest(req, 'get-sessions'); const sessions = db.prepare(
"SELECT id, uid, title, started_at, ended_at, sync_state, updated_at FROM sessions WHERE uid = ? ORDER BY started_at DESC"
).all(req.uid);
res.json(sessions); res.json(sessions);
} catch (error) { } catch (error) {
console.error('Failed to get sessions via IPC:', error); console.error('Failed to get sessions:', error);
res.status(500).json({ error: 'Failed to retrieve sessions' }); res.status(500).json({ error: 'Failed to retrieve sessions' });
} }
}); });
router.post('/', async (req, res) => { router.post('/', (req, res) => {
const { title } = req.body;
const sessionId = crypto.randomUUID();
const now = Math.floor(Date.now() / 1000);
try { try {
const result = await ipcRequest(req, 'create-session', req.body); db.prepare(
res.status(201).json({ ...result, message: 'Session created successfully' }); `INSERT INTO sessions (id, uid, title, started_at, updated_at)
VALUES (?, ?, ?, ?, ?)`
).run(sessionId, req.uid, title || 'New Conversation', now, now);
res.status(201).json({ id: sessionId, message: 'Session created successfully' });
} catch (error) { } catch (error) {
console.error('Failed to create session via IPC:', error); console.error('Failed to create session:', error);
res.status(500).json({ error: 'Failed to create session' }); res.status(500).json({ error: 'Failed to create session' });
} }
}); });
router.get('/:session_id', async (req, res) => { router.get('/:session_id', (req, res) => {
const { session_id } = req.params;
try { try {
const details = await ipcRequest(req, 'get-session-details', req.params.session_id); const session = db.prepare("SELECT * FROM sessions WHERE id = ?").get(session_id);
if (!details) { if (!session) {
return res.status(404).json({ error: 'Session not found' }); return res.status(404).json({ error: 'Session not found' });
} }
res.json(details);
const transcripts = db.prepare("SELECT * FROM transcripts WHERE session_id = ? ORDER BY start_at ASC").all(session_id);
const ai_messages = db.prepare("SELECT * FROM ai_messages WHERE session_id = ? ORDER BY sent_at ASC").all(session_id);
const summary = db.prepare("SELECT * FROM summaries WHERE session_id = ?").get(session_id);
res.json({
session,
transcripts,
ai_messages,
summary: summary || null
});
} catch (error) { } catch (error) {
console.error(`Failed to get session details via IPC for ${req.params.session_id}:`, error); console.error(`Failed to get session ${session_id}:`, error);
res.status(500).json({ error: 'Failed to retrieve session details' }); res.status(500).json({ error: 'Failed to retrieve session details' });
} }
}); });
router.delete('/:session_id', async (req, res) => { router.delete('/:session_id', (req, res) => {
const { session_id } = req.params;
const session = db.prepare("SELECT id FROM sessions WHERE id = ?").get(session_id);
if (!session) {
return res.status(404).json({ error: 'Session not found' });
}
try { try {
await ipcRequest(req, 'delete-session', req.params.session_id); db.transaction(() => {
db.prepare("DELETE FROM transcripts WHERE session_id = ?").run(session_id);
db.prepare("DELETE FROM ai_messages WHERE session_id = ?").run(session_id);
db.prepare("DELETE FROM summaries WHERE session_id = ?").run(session_id);
db.prepare("DELETE FROM sessions WHERE id = ?").run(session_id);
})();
res.status(200).json({ message: 'Session deleted successfully' }); res.status(200).json({ message: 'Session deleted successfully' });
} catch (error) { } catch (error) {
console.error(`Failed to delete session via IPC for ${req.params.session_id}:`, error); console.error(`Failed to delete session ${session_id}:`, error);
res.status(500).json({ error: 'Failed to delete session' }); res.status(500).json({ error: 'Failed to delete session' });
} }
}); });
// The search functionality will be more complex to move to IPC.
// For now, we can disable it or leave it as is, knowing it's a future task.
router.get('/search', (req, res) => { router.get('/search', (req, res) => {
res.status(501).json({ error: 'Search not implemented for IPC bridge yet.' }); const { q } = req.query;
if (!q || !validator.isLength(q, { min: 3 })) {
return res.status(400).json({ error: 'Query parameter "q" is required' });
}
// Sanitize and validate input
const sanitizedQuery = validator.escape(q.trim()); // Escapes HTML and special chars
if (sanitizedQuery.length === 0 || sanitizedQuery.length > 255) {
return res.status(400).json({ error: 'Query parameter "q" must be between 3 and 255 characters' });
}
try {
const searchQuery = `%${sanitizedQuery}%`;
const sessionIds = db.prepare(`
SELECT DISTINCT session_id FROM (
SELECT session_id FROM transcripts WHERE text LIKE ?
UNION
SELECT session_id FROM ai_messages WHERE content LIKE ?
UNION
SELECT session_id FROM summaries WHERE text LIKE ? OR tldr LIKE ?
)
`).all(searchQuery, searchQuery, searchQuery, searchQuery).map(row => row.session_id);
if (sessionIds.length === 0) {
return res.json([]);
}
const placeholders = sessionIds.map(() => '?').join(',');
const sessions = db.prepare(
`SELECT id, uid, title, started_at, ended_at, sync_state, updated_at FROM sessions WHERE id IN (${placeholders}) ORDER BY started_at DESC`
).all(sessionIds);
res.json(sessions);
} catch (error) {
console.error('Search failed:', error);
res.status(500).json({ error: 'Failed to perform search' });
}
}); });
module.exports = router; module.exports = router;

View File

@ -1,43 +1,85 @@
const express = require('express'); const express = require('express');
const crypto = require('crypto');
const db = require('../db');
const router = express.Router(); const router = express.Router();
const { ipcRequest } = require('../ipcBridge');
router.get('/', async (req, res) => { router.get('/', (req, res) => {
try { try {
const presets = await ipcRequest(req, 'get-presets'); const presets = db.prepare(
`SELECT * FROM prompt_presets
WHERE uid = ? OR is_default = 1
ORDER BY is_default DESC, title ASC`
).all(req.uid);
res.json(presets); res.json(presets);
} catch (error) { } catch (error) {
console.error('Failed to get presets via IPC:', error); console.error('Failed to get presets:', error);
res.status(500).json({ error: 'Failed to retrieve presets' }); res.status(500).json({ error: 'Failed to retrieve presets' });
} }
}); });
router.post('/', async (req, res) => { router.post('/', (req, res) => {
const { title, prompt } = req.body;
if (!title || !prompt) {
return res.status(400).json({ error: 'Title and prompt are required' });
}
const presetId = crypto.randomUUID();
const now = Math.floor(Date.now() / 1000);
try { try {
const result = await ipcRequest(req, 'create-preset', req.body); db.prepare(
res.status(201).json({ ...result, message: 'Preset created successfully' }); `INSERT INTO prompt_presets (id, uid, title, prompt, is_default, created_at, sync_state)
VALUES (?, ?, ?, ?, 0, ?, 'dirty')`
).run(presetId, req.uid, title, prompt, now);
res.status(201).json({ id: presetId, message: 'Preset created successfully' });
} catch (error) { } catch (error) {
console.error('Failed to create preset via IPC:', error); console.error('Failed to create preset:', error);
res.status(500).json({ error: 'Failed to create preset' }); res.status(500).json({ error: 'Failed to create preset' });
} }
}); });
router.put('/:id', async (req, res) => { router.put('/:id', (req, res) => {
const { id } = req.params;
const { title, prompt } = req.body;
if (!title || !prompt) {
return res.status(400).json({ error: 'Title and prompt are required' });
}
try { try {
await ipcRequest(req, 'update-preset', { id: req.params.id, data: req.body }); const result = db.prepare(
`UPDATE prompt_presets
SET title = ?, prompt = ?, sync_state = 'dirty'
WHERE id = ? AND uid = ? AND is_default = 0`
).run(title, prompt, id, req.uid);
if (result.changes === 0) {
return res.status(404).json({ error: "Preset not found or you don't have permission to edit it." });
}
res.json({ message: 'Preset updated successfully' }); res.json({ message: 'Preset updated successfully' });
} catch (error) { } catch (error) {
console.error('Failed to update preset via IPC:', error); console.error('Failed to update preset:', error);
res.status(500).json({ error: 'Failed to update preset' }); res.status(500).json({ error: 'Failed to update preset' });
} }
}); });
router.delete('/:id', async (req, res) => { router.delete('/:id', (req, res) => {
const { id } = req.params;
try { try {
await ipcRequest(req, 'delete-preset', req.params.id); const result = db.prepare(
`DELETE FROM prompt_presets
WHERE id = ? AND uid = ? AND is_default = 0`
).run(id, req.uid);
if (result.changes === 0) {
return res.status(404).json({ error: "Preset not found or you don't have permission to delete it." });
}
res.json({ message: 'Preset deleted successfully' }); res.json({ message: 'Preset deleted successfully' });
} catch (error) { } catch (error) {
console.error('Failed to delete preset via IPC:', error); console.error('Failed to delete preset:', error);
res.status(500).json({ error: 'Failed to delete preset' }); res.status(500).json({ error: 'Failed to delete preset' });
} }
}); });

View File

@ -1,88 +1,144 @@
const express = require('express'); const express = require('express');
const db = require('../db');
const router = express.Router(); const router = express.Router();
const { ipcRequest } = require('../ipcBridge');
router.put('/profile', async (req, res) => { router.put('/profile', (req, res) => {
const { displayName } = req.body;
if (!displayName) return res.status(400).json({ error: 'displayName is required' });
try { try {
await ipcRequest(req, 'update-user-profile', req.body); db.prepare("UPDATE users SET display_name = ? WHERE uid = ?").run(displayName, req.uid);
res.json({ message: 'Profile updated successfully' }); res.json({ message: 'Profile updated successfully' });
} catch (error) { } catch (error) {
console.error('Failed to update profile via IPC:', error); console.error('Failed to update profile:', error);
res.status(500).json({ error: 'Failed to update profile' }); res.status(500).json({ error: 'Failed to update profile' });
} }
}); });
router.get('/profile', async (req, res) => { router.get('/profile', (req, res) => {
try { try {
const user = await ipcRequest(req, 'get-user-profile'); const user = db.prepare('SELECT uid, display_name, email FROM users WHERE uid = ?').get(req.uid);
if (!user) return res.status(404).json({ error: 'User not found' }); if (!user) return res.status(404).json({ error: 'User not found' });
res.json(user); res.json(user);
} catch (error) { } catch (error) {
console.error('Failed to get profile via IPC:', error); console.error('Failed to get profile:', error);
res.status(500).json({ error: 'Failed to get profile' }); res.status(500).json({ error: 'Failed to get profile' });
} }
}); });
router.post('/find-or-create', async (req, res) => { router.post('/find-or-create', (req, res) => {
const { uid, displayName, email } = req.body;
if (!uid || !displayName || !email) {
return res.status(400).json({ error: 'uid, displayName, and email are required' });
}
try { try {
console.log('[API] find-or-create request received:', req.body); const now = Math.floor(Date.now() / 1000);
db.prepare(
`INSERT INTO users (uid, display_name, email, created_at)
VALUES (?, ?, ?, ?)
ON CONFLICT(uid) DO NOTHING`
).run(uid, displayName, email, now);
if (!req.body || !req.body.uid) { const user = db.prepare('SELECT * FROM users WHERE uid = ?').get(uid);
return res.status(400).json({ error: 'User data with uid is required' });
}
const user = await ipcRequest(req, 'find-or-create-user', req.body);
console.log('[API] find-or-create response:', user);
res.status(200).json(user); res.status(200).json(user);
} catch (error) { } catch (error) {
console.error('Failed to find or create user via IPC:', error); console.error('Failed to find or create user:', error);
console.error('Request body:', req.body); res.status(500).json({ error: 'Failed to find or create user' });
res.status(500).json({
error: 'Failed to find or create user',
details: error.message
});
} }
}); });
router.post('/api-key', async (req, res) => { router.post('/api-key', (req, res) => {
const { apiKey } = req.body;
if (typeof apiKey !== 'string') {
return res.status(400).json({ error: 'API key must be a string' });
}
try { try {
const { apiKey, provider = 'openai' } = req.body; db.prepare("UPDATE users SET api_key = ? WHERE uid = ?").run(apiKey, req.uid);
await ipcRequest(req, 'save-api-key', { apiKey, provider }); res.json({ message: 'API key saved successfully' });
res.json({ message: 'API key saved successfully' });
} catch (error) { } catch (error) {
console.error('Failed to save API key via IPC:', error); console.error('Failed to save API key:', error);
res.status(500).json({ error: 'Failed to save API key' }); res.status(500).json({ error: 'Failed to save API key' });
} }
}); });
router.get('/api-key-status', async (req, res) => { router.get('/api-key-status', (req, res) => {
try { try {
const status = await ipcRequest(req, 'check-api-key-status'); const row = db.prepare('SELECT api_key FROM users WHERE uid = ?').get(req.uid);
res.json(status); if (!row) {
return res.status(404).json({ error: 'User not found' });
}
res.json({ hasApiKey: !!row.api_key && row.api_key.length > 0 });
} catch (error) { } catch (error) {
console.error('Failed to get API key status via IPC:', error); console.error('Failed to get API key status:', error);
res.status(500).json({ error: 'Failed to get API key status' }); res.status(500).json({ error: 'Failed to get API key status' });
} }
}); });
router.delete('/profile', async (req, res) => { router.delete('/profile', (req, res) => {
try { try {
await ipcRequest(req, 'delete-account'); const user = db.prepare('SELECT uid FROM users WHERE uid = ?').get(req.uid);
if (!user) {
return res.status(404).json({ error: 'User not found' });
}
const userSessions = db.prepare('SELECT id FROM sessions WHERE uid = ?').all(user.uid);
const sessionIds = userSessions.map(s => s.id);
db.transaction(() => {
if (sessionIds.length > 0) {
const placeholders = sessionIds.map(() => '?').join(',');
db.prepare(`DELETE FROM transcripts WHERE session_id IN (${placeholders})`).run(...sessionIds);
db.prepare(`DELETE FROM ai_messages WHERE session_id IN (${placeholders})`).run(...sessionIds);
db.prepare(`DELETE FROM summaries WHERE session_id IN (${placeholders})`).run(...sessionIds);
db.prepare(`DELETE FROM sessions WHERE uid = ?`).run(user.uid);
}
db.prepare('DELETE FROM prompt_presets WHERE uid = ?').run(user.uid);
db.prepare('DELETE FROM users WHERE uid = ?').run(user.uid);
})();
res.status(200).json({ message: 'User account and all data deleted successfully.' }); res.status(200).json({ message: 'User account and all data deleted successfully.' });
} catch (error) { } catch (error) {
console.error('Failed to delete user account via IPC:', error); console.error('Failed to delete user account:', error);
res.status(500).json({ error: 'Failed to delete user account' }); res.status(500).json({ error: 'Failed to delete user account' });
} }
}); });
router.get('/batch', async (req, res) => { async function getUserBatchData(req, res) {
const { include = 'profile,presets,sessions' } = req.query;
try { try {
const result = await ipcRequest(req, 'get-batch-data', req.query.include); const includes = include.split(',').map(item => item.trim());
res.json(result); const result = {};
} catch(error) {
console.error('Failed to get batch data via IPC:', error); if (includes.includes('profile')) {
const user = db.prepare('SELECT uid, display_name, email FROM users WHERE uid = ?').get(req.uid);
result.profile = user || null;
}
if (includes.includes('presets')) {
const presets = db.prepare('SELECT * FROM prompt_presets WHERE uid = ? OR is_default = 1').all(req.uid);
result.presets = presets || [];
}
if (includes.includes('sessions')) {
const recent_sessions = db.prepare(
"SELECT id, title, started_at, updated_at FROM sessions WHERE uid = ? ORDER BY updated_at DESC LIMIT 10"
).all(req.uid);
result.sessions = recent_sessions || [];
}
res.json(result);
} catch (error) {
console.error('Failed to get batch data:', error);
res.status(500).json({ error: 'Failed to get batch data' }); res.status(500).json({ error: 'Failed to get batch data' });
} }
}); }
router.get('/batch', getUserBatchData);
module.exports = router; module.exports = router;

View File

@ -0,0 +1,35 @@
version: '3.8'
services:
backend:
build:
context: .
dockerfile: Dockerfile.backend
container_name: pickleglass-backend
restart: always
ports:
- "8000:8000"
environment:
- DATABASE_URL=/app/data/pickleglass.db
volumes:
- ./backend:/app
- ./data:/app/data
frontend:
build:
context: .
dockerfile: Dockerfile.frontend
container_name: pickleglass-frontend
restart: always
ports:
- "3000:3000"
environment:
- NEXT_PUBLIC_API_URL=http://localhost:8000
depends_on:
- backend
volumes:
- .:/app
- /app/node_modules
volumes:
mongodb_data:

File diff suppressed because it is too large Load Diff

View File

@ -24,7 +24,6 @@ export interface Session {
id: string; id: string;
uid: string; uid: string;
title: string; title: string;
session_type: string;
started_at: number; started_at: number;
ended_at?: number; ended_at?: number;
sync_state: 'clean' | 'dirty'; sync_state: 'clean' | 'dirty';
@ -87,11 +86,7 @@ export interface SessionDetails {
const isFirebaseMode = (): boolean => { const isFirebaseMode = (): boolean => {
// The web frontend can no longer directly access Firebase state, return firebaseAuth.currentUser !== null;
// so we assume communication always goes through the backend API.
// In the future, we can create an endpoint like /api/auth/status
// in the backend to retrieve the authentication state.
return false;
}; };
const timestampToUnix = (timestamp: Timestamp): number => { const timestampToUnix = (timestamp: Timestamp): number => {
@ -107,7 +102,6 @@ const convertFirestoreSession = (session: { id: string } & FirestoreSession, uid
id: session.id, id: session.id,
uid, uid,
title: session.title, title: session.title,
session_type: session.session_type,
started_at: timestampToUnix(session.startedAt), started_at: timestampToUnix(session.startedAt),
ended_at: session.endedAt ? timestampToUnix(session.endedAt) : undefined, ended_at: session.endedAt ? timestampToUnix(session.endedAt) : undefined,
sync_state: 'clean', sync_state: 'clean',
@ -189,13 +183,41 @@ const loadRuntimeConfig = async (): Promise<string | null> => {
return null; return null;
}; };
const getApiUrlFromElectron = (): string | null => {
if (typeof window !== 'undefined') {
try {
const { ipcRenderer } = window.require?.('electron') || {};
if (ipcRenderer) {
try {
const apiUrl = ipcRenderer.sendSync('get-api-url-sync');
if (apiUrl) {
console.log('✅ API URL from Electron IPC:', apiUrl);
return apiUrl;
}
} catch (error) {
console.log('⚠️ Electron IPC failed:', error);
}
}
} catch (error) {
console.log(' Not in Electron environment');
}
}
return null;
};
let apiUrlInitialized = false; let apiUrlInitialized = false;
let initializationPromise: Promise<void> | null = null; let initializationPromise: Promise<void> | null = null;
const initializeApiUrl = async () => { const initializeApiUrl = async () => {
if (apiUrlInitialized) return; if (apiUrlInitialized) return;
// Electron IPC 관련 코드를 모두 제거하고 runtime-config.json 또는 fallback에만 의존합니다. const electronUrl = getApiUrlFromElectron();
if (electronUrl) {
API_ORIGIN = electronUrl;
apiUrlInitialized = true;
return;
}
const runtimeUrl = await loadRuntimeConfig(); const runtimeUrl = await loadRuntimeConfig();
if (runtimeUrl) { if (runtimeUrl) {
API_ORIGIN = runtimeUrl; API_ORIGIN = runtimeUrl;
@ -365,7 +387,6 @@ export const createSession = async (title?: string): Promise<{ id: string }> =>
const uid = firebaseAuth.currentUser!.uid; const uid = firebaseAuth.currentUser!.uid;
const sessionId = await FirestoreSessionService.createSession(uid, { const sessionId = await FirestoreSessionService.createSession(uid, {
title: title || 'New Session', title: title || 'New Session',
session_type: 'ask',
endedAt: undefined endedAt: undefined
}); });
return { id: sessionId }; return { id: sessionId };
@ -521,10 +542,7 @@ export const updatePreset = async (id: string, data: { title: string, prompt: st
method: 'PUT', method: 'PUT',
body: JSON.stringify(data), body: JSON.stringify(data),
}); });
if (!response.ok) { if (!response.ok) throw new Error('Failed to update preset');
const errorText = await response.text();
throw new Error(`Failed to update preset: ${response.status} ${errorText}`);
}
} }
}; };

View File

@ -36,12 +36,21 @@ export const useAuth = () => {
setUser(profile); setUser(profile);
setUserInfo(profile); setUserInfo(profile);
if (window.ipcRenderer) {
window.ipcRenderer.send('set-current-user', profile.uid);
}
} else { } else {
console.log('🏠 Local mode activated'); console.log('🏠 Local mode activated');
setMode('local'); setMode('local');
setUser(defaultLocalUser); setUser(defaultLocalUser);
setUserInfo(defaultLocalUser); setUserInfo(defaultLocalUser);
if (window.ipcRenderer) {
window.ipcRenderer.send('set-current-user', defaultLocalUser.uid);
}
} }
setIsLoading(false); setIsLoading(false);
}); });

View File

@ -24,7 +24,6 @@ export interface FirestoreUserProfile {
export interface FirestoreSession { export interface FirestoreSession {
title: string; title: string;
session_type: string;
startedAt: Timestamp; startedAt: Timestamp;
endedAt?: Timestamp; endedAt?: Timestamp;
} }

View File

492
src/app/ApiKeyHeader.js Normal file
View File

@ -0,0 +1,492 @@
import { html, css, LitElement } from '../assets/lit-core-2.7.4.min.js';
export class ApiKeyHeader extends LitElement {
static properties = {
apiKey: { type: String },
isLoading: { type: Boolean },
errorMessage: { type: String },
};
static styles = css`
:host {
display: block;
transform: translate3d(0, 0, 0);
backface-visibility: hidden;
transition: opacity 0.25s ease-out;
}
:host(.sliding-out) {
animation: slideOutUp 0.3s ease-in forwards;
will-change: opacity, transform;
}
:host(.hidden) {
opacity: 0;
pointer-events: none;
}
@keyframes slideOutUp {
from {
opacity: 1;
transform: translateY(0);
}
to {
opacity: 0;
transform: translateY(-20px);
}
}
* {
font-family: 'Helvetica Neue', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
cursor: default;
user-select: none;
box-sizing: border-box;
}
.container {
width: 285px;
height: 220px;
padding: 18px 20px;
background: rgba(0, 0, 0, 0.3);
border-radius: 16px;
overflow: hidden;
position: relative;
display: flex;
flex-direction: column;
align-items: center;
}
.container::after {
content: '';
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
border-radius: 16px;
padding: 1px;
background: linear-gradient(169deg, rgba(255, 255, 255, 0.5) 0%, rgba(255, 255, 255, 0) 50%, rgba(255, 255, 255, 0.5) 100%);
-webkit-mask: linear-gradient(#fff 0 0) content-box, linear-gradient(#fff 0 0);
-webkit-mask-composite: destination-out;
mask-composite: exclude;
pointer-events: none;
}
.close-button {
position: absolute;
top: 10px;
right: 10px;
width: 14px;
height: 14px;
background: rgba(255, 255, 255, 0.1);
border: none;
border-radius: 3px;
color: rgba(255, 255, 255, 0.7);
cursor: pointer;
display: flex;
align-items: center;
justify-content: center;
transition: all 0.15s ease;
z-index: 10;
font-size: 14px;
line-height: 1;
padding: 0;
}
.close-button:hover {
background: rgba(255, 255, 255, 0.2);
color: rgba(255, 255, 255, 0.9);
}
.close-button:active {
transform: scale(0.95);
}
.title {
color: white;
font-size: 16px;
font-weight: 500; /* Medium */
margin: 0;
text-align: center;
flex-shrink: 0; /* 제목이 줄어들지 않도록 고정 */
}
.form-content {
display: flex;
flex-direction: column;
align-items: center;
width: 100%;
margin-top: auto; /* 이 속성이 제목과 폼 사이의 공간을 만듭니다. */
}
.error-message {
color: rgba(239, 68, 68, 0.9);
font-weight: 500;
font-size: 11px;
height: 14px; /* Reserve space to prevent layout shift */
text-align: center;
margin-bottom: 4px;
}
.api-input {
width: 100%;
height: 34px;
background: rgba(255, 255, 255, 0.1);
border-radius: 10px;
border: none;
padding: 0 10px;
color: white;
font-size: 12px;
font-weight: 400; /* Regular */
margin-bottom: 6px;
text-align: center;
user-select: text;
cursor: text;
}
.api-input::placeholder {
color: rgba(255, 255, 255, 0.6);
}
.api-input:focus {
outline: none;
}
.action-button {
width: 100%;
height: 34px;
background: rgba(255, 255, 255, 0.2);
border: none;
border-radius: 10px;
color: white;
font-size: 12px;
font-weight: 500; /* Medium */
cursor: pointer;
transition: background 0.15s ease;
position: relative;
overflow: hidden;
}
.action-button::after {
content: '';
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
border-radius: 10px;
padding: 1px;
background: linear-gradient(169deg, rgba(255, 255, 255, 0.5) 0%, rgba(255, 255, 255, 0) 50%, rgba(255, 255, 255, 0.5) 100%);
-webkit-mask: linear-gradient(#fff 0 0) content-box, linear-gradient(#fff 0 0);
-webkit-mask-composite: destination-out;
mask-composite: exclude;
pointer-events: none;
}
.action-button:hover {
background: rgba(255, 255, 255, 0.3);
}
.action-button:disabled {
opacity: 0.5;
cursor: not-allowed;
}
.or-text {
color: rgba(255, 255, 255, 0.5);
font-size: 12px;
font-weight: 500; /* Medium */
margin: 10px 0;
}
`;
constructor() {
super();
this.dragState = null;
this.wasJustDragged = false;
this.apiKey = '';
this.isLoading = false;
this.errorMessage = '';
this.validatedApiKey = null;
this.handleMouseMove = this.handleMouseMove.bind(this);
this.handleMouseUp = this.handleMouseUp.bind(this);
this.handleKeyPress = this.handleKeyPress.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
this.handleInput = this.handleInput.bind(this);
this.handleAnimationEnd = this.handleAnimationEnd.bind(this);
this.handleUsePicklesKey = this.handleUsePicklesKey.bind(this);
}
reset() {
this.apiKey = '';
this.isLoading = false;
this.errorMessage = '';
this.validatedApiKey = null;
this.requestUpdate();
}
async handleMouseDown(e) {
if (e.target.tagName === 'INPUT' || e.target.tagName === 'BUTTON') {
return;
}
e.preventDefault();
const { ipcRenderer } = window.require('electron');
const initialPosition = await ipcRenderer.invoke('get-header-position');
this.dragState = {
initialMouseX: e.screenX,
initialMouseY: e.screenY,
initialWindowX: initialPosition.x,
initialWindowY: initialPosition.y,
moved: false,
};
window.addEventListener('mousemove', this.handleMouseMove);
window.addEventListener('mouseup', this.handleMouseUp, { once: true });
}
handleMouseMove(e) {
if (!this.dragState) return;
const deltaX = Math.abs(e.screenX - this.dragState.initialMouseX);
const deltaY = Math.abs(e.screenY - this.dragState.initialMouseY);
if (deltaX > 3 || deltaY > 3) {
this.dragState.moved = true;
}
const newWindowX = this.dragState.initialWindowX + (e.screenX - this.dragState.initialMouseX);
const newWindowY = this.dragState.initialWindowY + (e.screenY - this.dragState.initialMouseY);
const { ipcRenderer } = window.require('electron');
ipcRenderer.invoke('move-header-to', newWindowX, newWindowY);
}
handleMouseUp(e) {
if (!this.dragState) return;
const wasDragged = this.dragState.moved;
window.removeEventListener('mousemove', this.handleMouseMove);
this.dragState = null;
if (wasDragged) {
this.wasJustDragged = true;
setTimeout(() => {
this.wasJustDragged = false;
}, 200);
}
}
handleInput(e) {
this.apiKey = e.target.value;
this.errorMessage = '';
console.log('Input changed:', this.apiKey?.length || 0, 'chars');
this.requestUpdate();
this.updateComplete.then(() => {
const inputField = this.shadowRoot?.querySelector('.apikey-input');
if (inputField && this.isInputFocused) {
inputField.focus();
}
});
}
handlePaste(e) {
e.preventDefault();
this.errorMessage = '';
const clipboardText = (e.clipboardData || window.clipboardData).getData('text');
console.log('Paste event detected:', clipboardText?.substring(0, 10) + '...');
if (clipboardText) {
this.apiKey = clipboardText.trim();
const inputElement = e.target;
inputElement.value = this.apiKey;
}
this.requestUpdate();
this.updateComplete.then(() => {
const inputField = this.shadowRoot?.querySelector('.apikey-input');
if (inputField) {
inputField.focus();
inputField.setSelectionRange(inputField.value.length, inputField.value.length);
}
});
}
handleKeyPress(e) {
if (e.key === 'Enter') {
e.preventDefault();
this.handleSubmit();
}
}
async handleSubmit() {
if (this.wasJustDragged || this.isLoading || !this.apiKey.trim()) {
console.log('Submit blocked:', {
wasJustDragged: this.wasJustDragged,
isLoading: this.isLoading,
hasApiKey: !!this.apiKey.trim(),
});
return;
}
console.log('Starting API key validation...');
this.isLoading = true;
this.errorMessage = '';
this.requestUpdate();
const apiKey = this.apiKey.trim();
let isValid = false;
try {
const isValid = await this.validateApiKey(this.apiKey.trim());
if (isValid) {
console.log('API key valid - starting slide out animation');
this.startSlideOutAnimation();
this.validatedApiKey = this.apiKey.trim();
} else {
this.errorMessage = 'Invalid API key - please check and try again';
console.log('API key validation failed');
}
} catch (error) {
console.error('API key validation error:', error);
this.errorMessage = 'Validation error - please try again';
} finally {
this.isLoading = false;
this.requestUpdate();
}
}
async validateApiKey(apiKey) {
if (!apiKey || apiKey.length < 15) return false;
if (!apiKey.match(/^[A-Za-z0-9_-]+$/)) return false;
try {
console.log('Validating API key with openai models endpoint...');
const response = await fetch('https://api.openai.com/v1/models', {
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${apiKey}`,
},
});
if (response.ok) {
const data = await response.json();
const hasGPTModels = data.data && data.data.some(m => m.id.startsWith('gpt-'));
if (hasGPTModels) {
console.log('API key validation successful - GPT models available');
return true;
} else {
console.log('API key valid but no GPT models available');
return false;
}
} else {
const errorData = await response.json().catch(() => ({}));
console.log('API key validation failed:', response.status, errorData.error?.message || 'Unknown error');
return false;
}
} catch (error) {
console.error('API key validation network error:', error);
return apiKey.length >= 20; // Fallback for network issues
}
}
startSlideOutAnimation() {
this.classList.add('sliding-out');
}
handleUsePicklesKey(e) {
e.preventDefault();
if (this.wasJustDragged) return;
console.log('Requesting Firebase authentication from main process...');
if (window.require) {
window.require('electron').ipcRenderer.invoke('start-firebase-auth');
}
}
handleClose() {
console.log('Close button clicked');
if (window.require) {
window.require('electron').ipcRenderer.invoke('quit-application');
}
}
handleAnimationEnd(e) {
if (e.target !== this) return;
if (this.classList.contains('sliding-out')) {
this.classList.remove('sliding-out');
this.classList.add('hidden');
if (this.validatedApiKey) {
if (window.require) {
window.require('electron').ipcRenderer.invoke('api-key-validated', this.validatedApiKey);
}
this.validatedApiKey = null;
}
}
}
connectedCallback() {
super.connectedCallback();
this.addEventListener('animationend', this.handleAnimationEnd);
}
disconnectedCallback() {
super.disconnectedCallback();
this.removeEventListener('animationend', this.handleAnimationEnd);
}
render() {
const isButtonDisabled = this.isLoading || !this.apiKey || !this.apiKey.trim();
return html`
<div class="container" @mousedown=${this.handleMouseDown}>
<button class="close-button" @click=${this.handleClose} title="Close application">
<svg width="8" height="8" viewBox="0 0 10 10" fill="currentColor">
<path d="M1 1L9 9M9 1L1 9" stroke="currentColor" stroke-width="1.2" />
</svg>
</button>
<h1 class="title">Choose how to power your AI</h1>
<div class="form-content">
<div class="error-message">${this.errorMessage}</div>
<input
type="password"
class="api-input"
placeholder="Enter your OpenAI API key"
.value=${this.apiKey || ''}
@input=${this.handleInput}
@keypress=${this.handleKeyPress}
@paste=${this.handlePaste}
@focus=${() => (this.errorMessage = '')}
?disabled=${this.isLoading}
autocomplete="off"
spellcheck="false"
tabindex="0"
/>
<button class="action-button" @click=${this.handleSubmit} ?disabled=${isButtonDisabled} tabindex="0">
${this.isLoading ? 'Validating...' : 'Confirm'}
</button>
<div class="or-text">or</div>
<button class="action-button" @click=${this.handleUsePicklesKey}>Use Pickle's API Key</button>
</div>
</div>
`;
}
}
customElements.define('apikey-header', ApiKeyHeader);

592
src/app/AppHeader.js Normal file
View File

@ -0,0 +1,592 @@
import { html, css, LitElement } from '../assets/lit-core-2.7.4.min.js';
export class AppHeader extends LitElement {
static properties = {
isSessionActive: { type: Boolean, state: true },
};
static styles = css`
:host {
display: block;
transform: translate3d(0, 0, 0);
backface-visibility: hidden;
transition: transform 0.25s cubic-bezier(0.23, 1, 0.32, 1), opacity 0.25s ease-out;
}
:host(.hiding) {
animation: slideUp 0.45s cubic-bezier(0.55, 0.085, 0.68, 0.53) forwards;
}
:host(.showing) {
animation: slideDown 0.5s cubic-bezier(0.25, 0.8, 0.25, 1) forwards;
}
:host(.sliding-in) {
animation: fadeIn 0.25s ease-out forwards;
will-change: opacity;
}
:host(.hidden) {
opacity: 0;
transform: translateY(-180%) scale(0.8);
pointer-events: none;
}
@keyframes slideUp {
0% {
opacity: 1;
transform: translateY(0) scale(1);
filter: blur(0px) brightness(1);
box-shadow: 0 8px 32px rgba(0, 0, 0, 0.3);
}
25% {
opacity: 0.85;
transform: translateY(-20%) scale(0.96);
filter: blur(0px) brightness(0.95);
box-shadow: 0 6px 28px rgba(0, 0, 0, 0.25);
}
50% {
opacity: 0.5;
transform: translateY(-60%) scale(0.9);
filter: blur(1px) brightness(0.85);
box-shadow: 0 3px 15px rgba(0, 0, 0, 0.15);
}
75% {
opacity: 0.15;
transform: translateY(-120%) scale(0.85);
filter: blur(2px) brightness(0.75);
box-shadow: 0 1px 8px rgba(0, 0, 0, 0.08);
}
100% {
opacity: 0;
transform: translateY(-180%) scale(0.8);
filter: blur(3px) brightness(0.7);
box-shadow: 0 0px 0px rgba(0, 0, 0, 0);
}
}
@keyframes slideDown {
0% {
opacity: 0;
transform: translateY(-180%) scale(0.8);
filter: blur(3px) brightness(0.7);
box-shadow: 0 0px 0px rgba(0, 0, 0, 0);
}
40% {
opacity: 0.6;
transform: translateY(-30%) scale(0.95);
filter: blur(1px) brightness(0.9);
box-shadow: 0 4px 20px rgba(0, 0, 0, 0.2);
}
70% {
opacity: 0.9;
transform: translateY(-5%) scale(1.01);
filter: blur(0.3px) brightness(1.02);
box-shadow: 0 7px 28px rgba(0, 0, 0, 0.28);
}
85% {
opacity: 0.98;
transform: translateY(1%) scale(0.995);
filter: blur(0.1px) brightness(1.01);
box-shadow: 0 8px 30px rgba(0, 0, 0, 0.31);
}
100% {
opacity: 1;
transform: translateY(0) scale(1);
filter: blur(0px) brightness(1);
box-shadow: 0 8px 32px rgba(0, 0, 0, 0.3);
}
}
@keyframes fadeIn {
0% {
opacity: 0;
}
100% {
opacity: 1;
}
}
* {
font-family: 'Helvetica Neue', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
cursor: default;
user-select: none;
}
.header {
width: 100%;
height: 47px;
padding: 2px 10px 2px 13px;
background: transparent;
overflow: hidden;
border-radius: 9000px;
/* backdrop-filter: blur(1px); */
justify-content: space-between;
align-items: center;
display: inline-flex;
box-sizing: border-box;
position: relative;
}
.header::before {
content: '';
position: absolute;
top: 0; left: 0; right: 0; bottom: 0;
width: 100%;
height: 100%;
background: rgba(0, 0, 0, 0.6);
border-radius: 9000px;
z-index: -1;
}
.header::after {
content: '';
position: absolute;
top: 0; left: 0; right: 0; bottom: 0;
border-radius: 9000px;
padding: 1px;
background: linear-gradient(169deg, rgba(255, 255, 255, 0.17) 0%, rgba(255, 255, 255, 0.08) 50%, rgba(255, 255, 255, 0.17) 100%);
-webkit-mask:
linear-gradient(#fff 0 0) content-box,
linear-gradient(#fff 0 0);
-webkit-mask-composite: destination-out;
mask-composite: exclude;
pointer-events: none;
}
.listen-button {
height: 26px;
padding: 0 13px;
background: transparent;
border-radius: 9000px;
justify-content: center;
width: 78px;
align-items: center;
gap: 6px;
display: flex;
border: none;
cursor: pointer;
position: relative;
}
.listen-button.active::before {
background: rgba(215, 0, 0, 0.5);
}
.listen-button.active:hover::before {
background: rgba(255, 20, 20, 0.6);
}
.listen-button:hover::before {
background: rgba(255, 255, 255, 0.18);
}
.listen-button::before {
content: '';
position: absolute;
top: 0; left: 0; right: 0; bottom: 0;
background: rgba(255, 255, 255, 0.14);
border-radius: 9000px;
z-index: -1;
transition: background 0.15s ease;
}
.listen-button::after {
content: '';
position: absolute;
top: 0; left: 0; right: 0; bottom: 0;
border-radius: 9000px;
padding: 1px;
background: linear-gradient(169deg, rgba(255, 255, 255, 0.17) 0%, rgba(255, 255, 255, 0.08) 50%, rgba(255, 255, 255, 0.17) 100%);
-webkit-mask:
linear-gradient(#fff 0 0) content-box,
linear-gradient(#fff 0 0);
-webkit-mask-composite: destination-out;
mask-composite: exclude;
pointer-events: none;
}
.header-actions {
height: 26px;
box-sizing: border-box;
justify-content: flex-start;
align-items: center;
gap: 9px;
display: flex;
padding: 0 8px;
border-radius: 6px;
transition: background 0.15s ease;
}
.header-actions:hover {
background: rgba(255, 255, 255, 0.1);
}
.ask-action {
margin-left: 4px;
}
.action-button,
.settings-button {
background: transparent;
color: white;
border: none;
cursor: pointer;
display: flex;
align-items: center;
gap: 6px;
}
.action-text {
padding-bottom: 1px;
justify-content: center;
align-items: center;
gap: 10px;
display: flex;
}
.action-text-content {
color: white;
font-size: 12px;
font-family: 'Helvetica Neue', sans-serif;
font-weight: 500; /* Medium */
word-wrap: break-word;
}
.icon-container {
justify-content: flex-start;
align-items: center;
gap: 4px;
display: flex;
}
.icon-container.ask-icons svg,
.icon-container.showhide-icons svg {
width: 12px;
height: 12px;
}
.listen-icon svg {
width: 12px;
height: 11px;
position: relative;
top: 1px;
}
.icon-box {
color: white;
font-size: 12px;
font-family: 'Helvetica Neue', sans-serif;
font-weight: 500;
background-color: rgba(255, 255, 255, 0.1);
border-radius: 13%;
width: 18px;
height: 18px;
display: flex;
align-items: center;
justify-content: center;
}
.settings-button {
padding: 5px;
border-radius: 50%;
transition: background 0.15s ease;
}
.settings-button:hover {
background: rgba(255, 255, 255, 0.1);
}
.settings-icon {
display: flex;
align-items: center;
justify-content: center;
}
.settings-icon svg {
width: 16px;
height: 16px;
}
`;
constructor() {
super();
this.dragState = null;
this.wasJustDragged = false;
this.isVisible = true;
this.isAnimating = false;
this.hasSlidIn = false;
this.settingsHideTimer = null;
this.isSessionActive = false;
if (window.require) {
const { ipcRenderer } = window.require('electron');
ipcRenderer.on('toggle-header-visibility', () => {
this.toggleVisibility();
});
ipcRenderer.on('cancel-hide-settings', () => {
this.cancelHideWindow('settings');
});
}
this.handleMouseMove = this.handleMouseMove.bind(this);
this.handleMouseUp = this.handleMouseUp.bind(this);
this.handleAnimationEnd = this.handleAnimationEnd.bind(this);
}
async handleMouseDown(e) {
e.preventDefault();
const { ipcRenderer } = window.require('electron');
const initialPosition = await ipcRenderer.invoke('get-header-position');
this.dragState = {
initialMouseX: e.screenX,
initialMouseY: e.screenY,
initialWindowX: initialPosition.x,
initialWindowY: initialPosition.y,
moved: false,
};
window.addEventListener('mousemove', this.handleMouseMove, { capture: true });
window.addEventListener('mouseup', this.handleMouseUp, { once: true, capture: true });
}
handleMouseMove(e) {
if (!this.dragState) return;
const deltaX = Math.abs(e.screenX - this.dragState.initialMouseX);
const deltaY = Math.abs(e.screenY - this.dragState.initialMouseY);
if (deltaX > 3 || deltaY > 3) {
this.dragState.moved = true;
}
const newWindowX = this.dragState.initialWindowX + (e.screenX - this.dragState.initialMouseX);
const newWindowY = this.dragState.initialWindowY + (e.screenY - this.dragState.initialMouseY);
const { ipcRenderer } = window.require('electron');
ipcRenderer.invoke('move-header-to', newWindowX, newWindowY);
}
handleMouseUp(e) {
if (!this.dragState) return;
const wasDragged = this.dragState.moved;
window.removeEventListener('mousemove', this.handleMouseMove, { capture: true });
this.dragState = null;
if (wasDragged) {
this.wasJustDragged = true;
setTimeout(() => {
this.wasJustDragged = false;
}, 0);
}
}
toggleVisibility() {
if (this.isAnimating) return;
this.isAnimating = true;
if (this.isVisible) {
this.hide();
} else {
this.show();
}
}
hide() {
this.classList.remove('showing', 'hidden');
this.classList.add('hiding');
this.isVisible = false;
}
show() {
this.classList.remove('hiding', 'hidden');
this.classList.add('showing');
this.isVisible = true;
}
handleAnimationEnd(e) {
if (e.target !== this) return;
this.isAnimating = false;
if (this.classList.contains('hiding')) {
this.classList.remove('hiding');
this.classList.add('hidden');
if (window.require) {
const { ipcRenderer } = window.require('electron');
ipcRenderer.send('header-animation-complete', 'hidden');
}
} else if (this.classList.contains('showing')) {
this.classList.remove('showing');
if (window.require) {
const { ipcRenderer } = window.require('electron');
ipcRenderer.send('header-animation-complete', 'visible');
}
} else if (this.classList.contains('sliding-in')) {
this.classList.remove('sliding-in');
this.hasSlidIn = true;
console.log('AppHeader slide-in animation completed');
}
}
startSlideInAnimation() {
if (this.hasSlidIn) return;
this.classList.add('sliding-in');
}
connectedCallback() {
super.connectedCallback();
this.addEventListener('animationend', this.handleAnimationEnd);
if (window.require) {
const { ipcRenderer } = window.require('electron');
this._sessionStateListener = (event, { isActive }) => {
this.isSessionActive = isActive;
};
ipcRenderer.on('session-state-changed', this._sessionStateListener);
}
}
disconnectedCallback() {
super.disconnectedCallback();
this.removeEventListener('animationend', this.handleAnimationEnd);
if (window.require) {
const { ipcRenderer } = window.require('electron');
ipcRenderer.removeAllListeners('toggle-header-visibility');
ipcRenderer.removeAllListeners('cancel-hide-settings');
if (this._sessionStateListener) {
ipcRenderer.removeListener('session-state-changed', this._sessionStateListener);
}
}
}
invoke(channel, ...args) {
if (this.wasJustDragged) {
return;
}
if (window.require) {
window.require('electron').ipcRenderer.invoke(channel, ...args);
}
}
showWindow(name, element) {
if (this.wasJustDragged) return;
if (window.require) {
const { ipcRenderer } = window.require('electron');
console.log(`[AppHeader] showWindow('${name}') called at ${Date.now()}`);
ipcRenderer.send('cancel-hide-window', name);
if (name === 'settings' && element) {
const rect = element.getBoundingClientRect();
ipcRenderer.send('show-window', {
name: 'settings',
bounds: {
x: rect.left,
y: rect.top,
width: rect.width,
height: rect.height
}
});
} else {
ipcRenderer.send('show-window', name);
}
}
}
hideWindow(name) {
if (this.wasJustDragged) return;
if (window.require) {
console.log(`[AppHeader] hideWindow('${name}') called at ${Date.now()}`);
window.require('electron').ipcRenderer.send('hide-window', name);
}
}
cancelHideWindow(name) {
}
render() {
return html`
<div class="header" @mousedown=${this.handleMouseDown}>
<button
class="listen-button ${this.isSessionActive ? 'active' : ''}"
@click=${() => this.invoke(this.isSessionActive ? 'close-session' : 'toggle-feature', 'listen')}
>
<div class="action-text">
<div class="action-text-content">${this.isSessionActive ? 'Stop' : 'Listen'}</div>
</div>
<div class="listen-icon">
${this.isSessionActive
? html`
<svg width="9" height="9" viewBox="0 0 9 9" fill="none" xmlns="http://www.w3.org/2000/svg">
<rect width="9" height="9" rx="1" fill="white"/>
</svg>
`
: html`
<svg width="12" height="11" viewBox="0 0 12 11" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M1.69922 2.7515C1.69922 2.37153 2.00725 2.0635 2.38722 2.0635H2.73122C3.11119 2.0635 3.41922 2.37153 3.41922 2.7515V8.2555C3.41922 8.63547 3.11119 8.9435 2.73122 8.9435H2.38722C2.00725 8.9435 1.69922 8.63547 1.69922 8.2555V2.7515Z" fill="white"/>
<path d="M5.13922 1.3755C5.13922 0.995528 5.44725 0.6875 5.82722 0.6875H6.17122C6.55119 0.6875 6.85922 0.995528 6.85922 1.3755V9.6315C6.85922 10.0115 6.55119 10.3195 6.17122 10.3195H5.82722C5.44725 10.3195 5.13922 10.0115 5.13922 9.6315V1.3755Z" fill="white"/>
<path d="M8.57922 3.0955C8.57922 2.71553 8.88725 2.4075 9.26722 2.4075H9.61122C9.99119 2.4075 10.2992 2.71553 10.2992 3.0955V7.9115C10.2992 8.29147 9.99119 8.5995 9.61122 8.5995H9.26722C8.88725 8.5995 8.57922 8.29147 8.57922 7.9115V3.0955Z" fill="white"/>
</svg>
`}
</div>
</button>
<div class="header-actions ask-action" @click=${() => this.invoke('toggle-feature', 'ask')}>
<div class="action-text">
<div class="action-text-content">Ask</div>
</div>
<div class="icon-container ask-icons">
<div class="icon-box"></div>
<div class="icon-box">
<svg viewBox="0 0 15 14" fill="none" xmlns="http://www.w3.org/2000/svg">
<path fill-rule="evenodd" clip-rule="evenodd" d="M2.41797 8.16406C2.41797 8.00935 2.47943 7.86098 2.58882 7.75158C2.69822 7.64219 2.84659 7.58073 3.0013 7.58073H10.0013C10.4654 7.58073 10.9106 7.39636 11.2387 7.06817C11.5669 6.73998 11.7513 6.29486 11.7513 5.83073V3.4974C11.7513 3.34269 11.8128 3.19431 11.9222 3.08492C12.0316 2.97552 12.1799 2.91406 12.3346 2.91406C12.4893 2.91406 12.6377 2.97552 12.7471 3.08492C12.8565 3.19431 12.918 3.34269 12.918 3.4974V5.83073C12.918 6.60428 12.6107 7.34614 12.0637 7.89312C11.5167 8.44011 10.7748 8.7474 10.0013 8.7474H3.0013C2.84659 8.7474 2.69822 8.68594 2.58882 8.57654C2.47943 8.46715 2.41797 8.31877 2.41797 8.16406Z" fill="white"/>
<path fill-rule="evenodd" clip-rule="evenodd" d="M2.58876 8.57973C2.4794 8.47034 2.41797 8.32199 2.41797 8.16731C2.41797 8.01263 2.4794 7.86429 2.58876 7.75489L4.92209 5.42156C5.03211 5.3153 5.17946 5.25651 5.33241 5.25783C5.48536 5.25916 5.63167 5.32051 5.73982 5.42867C5.84798 5.53682 5.90932 5.68313 5.91065 5.83608C5.91198 5.98903 5.85319 6.13638 5.74693 6.24639L3.82601 8.16731L5.74693 10.0882C5.80264 10.142 5.84708 10.2064 5.87765 10.2776C5.90823 10.3487 5.92432 10.4253 5.92499 10.5027C5.92566 10.5802 5.9109 10.657 5.88157 10.7287C5.85224 10.8004 5.80893 10.8655 5.75416 10.9203C5.69939 10.9751 5.63426 11.0184 5.56257 11.0477C5.49088 11.077 5.41406 11.0918 5.33661 11.0911C5.25916 11.0905 5.18261 11.0744 5.11144 11.0438C5.04027 11.0132 4.9759 10.9688 4.92209 10.9131L2.58876 8.57973Z" fill="white"/>
</svg>
</div>
</div>
</div>
<div class="header-actions" @click=${() => this.invoke('toggle-all-windows-visibility')}>
<div class="action-text">
<div class="action-text-content">Show/Hide</div>
</div>
<div class="icon-container showhide-icons">
<div class="icon-box"></div>
<div class="icon-box">
<svg viewBox="0 0 6 12" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M1.50391 1.32812L5.16391 10.673" stroke="white" stroke-width="1.4" stroke-linecap="round" stroke-linejoin="round"/>
</svg>
</div>
</div>
</div>
<button
class="settings-button"
@mouseenter=${(e) => this.showWindow('settings', e.currentTarget)}
@mouseleave=${() => this.hideWindow('settings')}
>
<div class="settings-icon">
<svg width="16" height="17" viewBox="0 0 16 17" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M8.0013 3.16406C7.82449 3.16406 7.65492 3.2343 7.5299 3.35932C7.40487 3.48435 7.33464 3.65392 7.33464 3.83073C7.33464 4.00754 7.40487 4.17711 7.5299 4.30213C7.65492 4.42716 7.82449 4.4974 8.0013 4.4974C8.17811 4.4974 8.34768 4.42716 8.47271 4.30213C8.59773 4.17711 8.66797 4.00754 8.66797 3.83073C8.66797 3.65392 8.59773 3.48435 8.47271 3.35932C8.34768 3.2343 8.17811 3.16406 8.0013 3.16406ZM8.0013 7.83073C7.82449 7.83073 7.65492 7.90097 7.5299 8.02599C7.40487 8.15102 7.33464 8.32058 7.33464 8.4974C7.33464 8.67421 7.40487 8.84378 7.5299 8.9688C7.65492 9.09382 7.82449 9.16406 8.0013 9.16406C8.17811 9.16406 8.34768 9.09382 8.47271 8.9688C8.59773 8.84378 8.66797 8.67421 8.66797 8.4974C8.66797 8.32058 8.59773 8.15102 8.47271 8.02599C8.34768 7.90097 8.17811 7.83073 8.0013 7.83073ZM8.0013 12.4974C7.82449 12.4974 7.65492 12.5676 7.5299 12.6927C7.40487 12.8177 7.33464 12.9873 7.33464 13.1641C7.33464 13.3409 7.40487 13.5104 7.5299 13.6355C7.65492 13.7605 7.82449 13.8307 8.0013 13.8307C8.17811 13.8307 8.34768 13.7605 8.47271 13.6355C8.59773 13.5104 8.66797 13.3409 8.66797 13.1641C8.66797 12.9873 8.59773 12.8177 8.47271 12.6927C8.34768 12.5676 8.17811 12.4974 8.0013 12.4974Z" fill="white" stroke="white" stroke-linecap="round" stroke-linejoin="round"/>
</svg>
</div>
</button>
</div>
`;
}
}
customElements.define('app-header', AppHeader);

273
src/app/HeaderController.js Normal file
View File

@ -0,0 +1,273 @@
import { initializeApp } from 'firebase/app';
import { getAuth, onAuthStateChanged, GoogleAuthProvider, signInWithCredential, signInWithCustomToken, signOut } from 'firebase/auth';
import './AppHeader.js';
import './ApiKeyHeader.js';
const firebaseConfig = {
apiKey: 'AIzaSyAgtJrmsFWG1C7m9S55HyT1laICEzuUS2g',
authDomain: 'pickle-3651a.firebaseapp.com',
projectId: 'pickle-3651a',
storageBucket: 'pickle-3651a.firebasestorage.app',
messagingSenderId: '904706892885',
appId: '1:904706892885:web:0e42b3dda796674ead20dc',
measurementId: 'G-SQ0WM6S28T',
};
const firebaseApp = initializeApp(firebaseConfig);
const auth = getAuth(firebaseApp);
class HeaderTransitionManager {
constructor() {
this.headerContainer = document.getElementById('header-container');
this.currentHeaderType = null; // 'apikey' | 'app'
this.apiKeyHeader = null;
this.appHeader = null;
/**
* only one header window is allowed
* @param {'apikey'|'app'} type
*/
this.ensureHeader = (type) => {
if (this.currentHeaderType === type) return;
if (this.apiKeyHeader) { this.apiKeyHeader.remove(); this.apiKeyHeader = null; }
if (this.appHeader) { this.appHeader.remove(); this.appHeader = null; }
if (type === 'apikey') {
this.apiKeyHeader = document.createElement('apikey-header');
this.headerContainer.appendChild(this.apiKeyHeader);
} else {
this.appHeader = document.createElement('app-header');
this.headerContainer.appendChild(this.appHeader);
this.appHeader.startSlideInAnimation?.();
}
this.currentHeaderType = type;
this.notifyHeaderState(type);
};
console.log('[HeaderController] Manager initialized');
if (window.require) {
const { ipcRenderer } = window.require('electron');
ipcRenderer
.invoke('get-current-api-key')
.then(storedKey => {
this.hasApiKey = !!storedKey;
})
.catch(() => {});
}
if (window.require) {
const { ipcRenderer } = window.require('electron');
ipcRenderer.on('login-successful', async (event, payload) => {
const { customToken, token, error } = payload || {};
try {
if (customToken) {
console.log('[HeaderController] Received custom token, signing in with custom token...');
await signInWithCustomToken(auth, customToken);
return;
}
if (token) {
console.log('[HeaderController] Received ID token, attempting Google credential sign-in...');
const credential = GoogleAuthProvider.credential(token);
await signInWithCredential(auth, credential);
return;
}
if (error) {
console.warn('[HeaderController] Login payload indicates verification failure. Proceeding to AppHeader UI only.');
this.transitionToAppHeader();
}
} catch (error) {
console.error('[HeaderController] Sign-in failed', error);
this.transitionToAppHeader();
}
});
ipcRenderer.on('request-firebase-logout', async () => {
console.log('[HeaderController] Received request to sign out.');
try {
await signOut(auth);
} catch (error) {
console.error('[HeaderController] Sign out failed', error);
}
});
ipcRenderer.on('api-key-validated', () => {
this.hasApiKey = true;
this.transitionToAppHeader();
});
ipcRenderer.on('api-key-removed', () => {
this.hasApiKey = false;
this.transitionToApiKeyHeader();
});
ipcRenderer.on('api-key-updated', () => {
this.hasApiKey = true;
if (!auth.currentUser) {
this.transitionToAppHeader();
}
});
ipcRenderer.on('firebase-auth-success', async (event, firebaseUser) => {
console.log('[HeaderController] Received firebase-auth-success:', firebaseUser.uid);
try {
if (firebaseUser.idToken) {
const credential = GoogleAuthProvider.credential(firebaseUser.idToken);
await signInWithCredential(auth, credential);
console.log('[HeaderController] Firebase sign-in successful via ID token');
} else {
console.warn('[HeaderController] No ID token received from deeplink, virtual key request may fail');
this.transitionToAppHeader();
}
} catch (error) {
console.error('[HeaderController] Firebase auth failed:', error);
this.transitionToAppHeader();
}
});
}
this._bootstrap();
onAuthStateChanged(auth, async user => {
console.log('[HeaderController] Auth state changed. User:', user ? user.email : 'null');
if (window.require) {
const { ipcRenderer } = window.require('electron');
let userDataWithToken = null;
if (user) {
try {
const idToken = await user.getIdToken();
userDataWithToken = {
uid: user.uid,
email: user.email,
name: user.displayName,
photoURL: user.photoURL,
idToken: idToken,
};
} catch (error) {
console.error('[HeaderController] Failed to get ID token:', error);
userDataWithToken = {
uid: user.uid,
email: user.email,
name: user.displayName,
photoURL: user.photoURL,
idToken: null,
};
}
}
ipcRenderer.invoke('firebase-auth-state-changed', userDataWithToken).catch(console.error);
}
if (!this.isInitialized) {
this.isInitialized = true;
}
if (user) {
console.log('[HeaderController] User is logged in, transitioning to AppHeader');
this.transitionToAppHeader(!this.hasApiKey);
} else if (this.hasApiKey) {
console.log('[HeaderController] No Firebase user but API key exists, showing AppHeader');
this.transitionToAppHeader(false);
} else {
console.log('[HeaderController] No auth & no API key — showing ApiKeyHeader');
this.transitionToApiKeyHeader();
}
});
}
notifyHeaderState(stateOverride) {
const state = stateOverride || this.currentHeaderType || 'apikey';
if (window.require) {
window.require('electron').ipcRenderer.send('header-state-changed', state);
}
}
async _bootstrap() {
let storedKey = null;
if (window.require) {
try {
storedKey = await window
.require('electron')
.ipcRenderer.invoke('get-current-api-key');
} catch (_) {}
}
this.hasApiKey = !!storedKey;
const user = await new Promise(resolve => {
const unsubscribe = onAuthStateChanged(auth, u => {
unsubscribe();
resolve(u);
});
});
if (user || this.hasApiKey) {
await this._resizeForApp();
this.ensureHeader('app');
} else {
await this._resizeForApiKey();
this.ensureHeader('apikey');
}
}
async transitionToAppHeader(animate = true) {
if (this.currentHeaderType === 'app') {
return this._resizeForApp();
}
const canAnimate =
animate &&
this.apiKeyHeader &&
!this.apiKeyHeader.classList.contains('hidden') &&
typeof this.apiKeyHeader.startSlideOutAnimation === 'function';
if (canAnimate) {
const old = this.apiKeyHeader;
const onEnd = () => {
clearTimeout(fallback);
this._resizeForApp().then(() => this.ensureHeader('app'));
};
old.addEventListener('animationend', onEnd, { once: true });
old.startSlideOutAnimation();
const fallback = setTimeout(onEnd, 450);
} else {
this.ensureHeader('app');
this._resizeForApp();
}
}
_resizeForApp() {
if (!window.require) return;
return window
.require('electron')
.ipcRenderer.invoke('resize-header-window', { width: 353, height: 60 })
.catch(() => {});
}
async transitionToApiKeyHeader() {
await window.require('electron')
.ipcRenderer.invoke('resize-header-window', { width: 285, height: 220 });
if (this.currentHeaderType !== 'apikey') {
this.ensureHeader('apikey');
}
if (this.apiKeyHeader) this.apiKeyHeader.reset();
}
}
window.addEventListener('DOMContentLoaded', () => {
new HeaderTransitionManager();
});

285
src/app/PickleGlassApp.js Normal file
View File

@ -0,0 +1,285 @@
import { html, css, LitElement } from '../assets/lit-core-2.7.4.min.js';
import { CustomizeView } from '../features/customize/CustomizeView.js';
import { AssistantView } from '../features/listen/AssistantView.js';
import { OnboardingView } from '../features/onboarding/OnboardingView.js';
import { AskView } from '../features/ask/AskView.js';
import '../features/listen/renderer.js';
export class PickleGlassApp extends LitElement {
static styles = css`
:host {
display: block;
width: 100%;
color: var(--text-color);
background: transparent;
border-radius: 7px;
}
assistant-view {
display: block;
width: 100%;
}
ask-view, customize-view, history-view, help-view, onboarding-view, setup-view {
display: block;
width: 100%;
}
`;
static properties = {
currentView: { type: String },
statusText: { type: String },
startTime: { type: Number },
currentResponseIndex: { type: Number },
isMainViewVisible: { type: Boolean },
selectedProfile: { type: String },
selectedLanguage: { type: String },
selectedScreenshotInterval: { type: String },
selectedImageQuality: { type: String },
isClickThrough: { type: Boolean, state: true },
layoutMode: { type: String },
_viewInstances: { type: Object, state: true },
_isClickThrough: { state: true },
structuredData: { type: Object },
};
constructor() {
super();
const urlParams = new URLSearchParams(window.location.search);
this.currentView = urlParams.get('view') || 'listen';
this.currentResponseIndex = -1;
this.selectedProfile = localStorage.getItem('selectedProfile') || 'interview';
this.selectedLanguage = localStorage.getItem('selectedLanguage') || 'en-US';
this.selectedScreenshotInterval = localStorage.getItem('selectedScreenshotInterval') || '5';
this.selectedImageQuality = localStorage.getItem('selectedImageQuality') || 'medium';
this._isClickThrough = false;
this.outlines = [];
this.analysisRequests = [];
window.pickleGlass.setStructuredData = data => {
this.updateStructuredData(data);
};
}
connectedCallback() {
super.connectedCallback();
if (window.require) {
const { ipcRenderer } = window.require('electron');
ipcRenderer.on('update-status', (_, status) => this.setStatus(status));
ipcRenderer.on('click-through-toggled', (_, isEnabled) => {
this._isClickThrough = isEnabled;
});
ipcRenderer.on('show-view', (_, view) => {
this.currentView = view;
this.isMainViewVisible = true;
});
ipcRenderer.on('start-listening-session', () => {
console.log('Received start-listening-session command, calling handleListenClick.');
this.handleListenClick();
});
}
}
disconnectedCallback() {
super.disconnectedCallback();
if (window.require) {
const { ipcRenderer } = window.require('electron');
ipcRenderer.removeAllListeners('update-status');
ipcRenderer.removeAllListeners('click-through-toggled');
ipcRenderer.removeAllListeners('show-view');
ipcRenderer.removeAllListeners('start-listening-session');
}
}
updated(changedProperties) {
if (changedProperties.has('isMainViewVisible') || changedProperties.has('currentView')) {
this.requestWindowResize();
}
if (changedProperties.has('currentView') && window.require) {
const { ipcRenderer } = window.require('electron');
ipcRenderer.send('view-changed', this.currentView);
const viewContainer = this.shadowRoot?.querySelector('.view-container');
if (viewContainer) {
viewContainer.classList.add('entering');
requestAnimationFrame(() => {
viewContainer.classList.remove('entering');
});
}
}
// Only update localStorage when these specific properties change
if (changedProperties.has('selectedProfile')) {
localStorage.setItem('selectedProfile', this.selectedProfile);
}
if (changedProperties.has('selectedLanguage')) {
localStorage.setItem('selectedLanguage', this.selectedLanguage);
}
if (changedProperties.has('selectedScreenshotInterval')) {
localStorage.setItem('selectedScreenshotInterval', this.selectedScreenshotInterval);
}
if (changedProperties.has('selectedImageQuality')) {
localStorage.setItem('selectedImageQuality', this.selectedImageQuality);
}
if (changedProperties.has('layoutMode')) {
this.updateLayoutMode();
}
}
requestWindowResize() {
if (window.require) {
const { ipcRenderer } = window.require('electron');
ipcRenderer.invoke('resize-window', {
isMainViewVisible: this.isMainViewVisible,
view: this.currentView,
});
}
}
setStatus(text) {
this.statusText = text;
}
async handleListenClick() {
if (window.require) {
const { ipcRenderer } = window.require('electron');
const isActive = await ipcRenderer.invoke('is-session-active');
if (isActive) {
console.log('Session is already active. No action needed.');
return;
}
}
if (window.pickleGlass) {
await window.pickleGlass.initializeopenai(this.selectedProfile, this.selectedLanguage);
window.pickleGlass.startCapture(this.selectedScreenshotInterval, this.selectedImageQuality);
}
// 🔄 Clear previous summary/analysis when a new listening session begins
this.structuredData = {
summary: [],
topic: { header: '', bullets: [] },
actions: [],
followUps: [],
};
this.currentResponseIndex = -1;
this.startTime = Date.now();
this.currentView = 'listen';
this.isMainViewVisible = true;
}
handleShowHideClick() {
this.isMainViewVisible = !this.isMainViewVisible;
}
handleCustomizeClick() {
this.currentView = 'customize';
this.isMainViewVisible = true;
}
handleHelpClick() {
this.currentView = 'help';
this.isMainViewVisible = true;
}
handleHistoryClick() {
this.currentView = 'history';
this.isMainViewVisible = true;
}
async handleClose() {
if (window.require) {
const { ipcRenderer } = window.require('electron');
await ipcRenderer.invoke('quit-application');
}
}
handleBackClick() {
this.currentView = 'listen';
}
async handleSendText(message) {
if (window.pickleGlass) {
const result = await window.pickleGlass.sendTextMessage(message);
if (!result.success) {
console.error('Failed to send message:', result.error);
this.setStatus('Error sending message: ' + result.error);
} else {
this.setStatus('Message sent...');
}
}
}
// updateOutline(outline) {
// console.log('📝 PickleGlassApp updateOutline:', outline);
// this.outlines = [...outline];
// this.requestUpdate();
// }
// updateAnalysisRequests(requests) {
// console.log('📝 PickleGlassApp updateAnalysisRequests:', requests);
// this.analysisRequests = [...requests];
// this.requestUpdate();
// }
updateStructuredData(data) {
console.log('📝 PickleGlassApp updateStructuredData:', data);
this.structuredData = data;
this.requestUpdate();
const assistantView = this.shadowRoot?.querySelector('assistant-view');
if (assistantView) {
assistantView.structuredData = data;
console.log('✅ Structured data passed to AssistantView');
}
}
handleResponseIndexChanged(e) {
this.currentResponseIndex = e.detail.index;
}
handleOnboardingComplete() {
this.currentView = 'main';
}
render() {
switch (this.currentView) {
case 'listen':
return html`<assistant-view
.currentResponseIndex=${this.currentResponseIndex}
.selectedProfile=${this.selectedProfile}
.structuredData=${this.structuredData}
.onSendText=${message => this.handleSendText(message)}
@response-index-changed=${e => (this.currentResponseIndex = e.detail.index)}
></assistant-view>`;
case 'ask':
return html`<ask-view></ask-view>`;
case 'customize':
return html`<customize-view
.selectedProfile=${this.selectedProfile}
.selectedLanguage=${this.selectedLanguage}
.onProfileChange=${profile => (this.selectedProfile = profile)}
.onLanguageChange=${lang => (this.selectedLanguage = lang)}
></customize-view>`;
case 'history':
return html`<history-view></history-view>`;
case 'help':
return html`<help-view></help-view>`;
case 'onboarding':
return html`<onboarding-view></onboarding-view>`;
case 'setup':
return html`<setup-view></setup-view>`;
default:
return html`<div>Unknown view: ${this.currentView}</div>`;
}
}
}
customElements.define('pickle-glass-app', PickleGlassApp);

316
src/app/content.html Normal file
View File

@ -0,0 +1,316 @@
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-security-policy" content="script-src 'self' 'unsafe-inline'" />
<title>Pickle Glass Content</title>
<style>
:root {
--background-transparent: transparent;
--text-color: #e5e5e7;
--border-color: rgba(255, 255, 255, 0.2);
--header-background: rgba(0, 0, 0, 0.8);
--header-actions-color: rgba(255, 255, 255, 0.6);
--main-content-background: rgba(0, 0, 0, 0.8);
--button-background: rgba(0, 0, 0, 0.5);
--button-border: rgba(255, 255, 255, 0.1);
--icon-button-color: rgb(229, 229, 231);
--hover-background: rgba(255, 255, 255, 0.1);
--input-background: rgba(0, 0, 0, 0.3);
--placeholder-color: rgba(255, 255, 255, 0.4);
--focus-border-color: #007aff;
--focus-box-shadow: rgba(0, 122, 255, 0.2);
--input-focus-background: rgba(0, 0, 0, 0.5);
--scrollbar-track: rgba(0, 0, 0, 0.2);
--scrollbar-thumb: rgba(255, 255, 255, 0.2);
--scrollbar-thumb-hover: rgba(255, 255, 255, 0.3);
--preview-video-background: rgba(0, 0, 0, 0.9);
--preview-video-border: rgba(255, 255, 255, 0.15);
--option-label-color: rgba(255, 255, 255, 0.8);
--screen-option-background: rgba(0, 0, 0, 0.4);
--screen-option-hover-background: rgba(0, 0, 0, 0.6);
--screen-option-selected-background: rgba(0, 122, 255, 0.15);
--screen-option-text: rgba(255, 255, 255, 0.7);
--description-color: rgba(255, 255, 255, 0.6);
--start-button-background: white;
--start-button-color: black;
--start-button-border: white;
--start-button-hover-background: rgba(255, 255, 255, 0.8);
--start-button-hover-border: rgba(0, 0, 0, 0.2);
--text-input-button-background: #007aff;
--text-input-button-hover: #0056b3;
--link-color: #007aff;
--key-background: rgba(255, 255, 255, 0.1);
--scrollbar-background: rgba(0, 0, 0, 0.4);
/* Layout-specific variables */
--header-padding: 10px 20px;
--header-font-size: 16px;
--header-gap: 12px;
--header-button-padding: 8px 16px;
--header-icon-padding: 8px;
--header-font-size-small: 13px;
--main-content-padding: 20px;
--main-content-margin-top: 10px;
--icon-size: 24px;
--border-radius: 7px;
--content-border-radius: 7px;
}
/* Compact layout styles */
:root.compact-layout {
--header-padding: 6px 12px;
--header-font-size: 13px;
--header-gap: 6px;
--header-button-padding: 4px 8px;
--header-icon-padding: 4px;
--header-font-size-small: 10px;
--main-content-padding: 10px;
--main-content-margin-top: 2px;
--icon-size: 16px;
--border-radius: 4px;
--content-border-radius: 4px;
}
html,
body {
margin: 0;
padding: 0;
min-height: 100%;
overflow: hidden;
background: transparent;
}
body {
font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
}
* {
box-sizing: border-box;
}
pickle-glass-app {
display: block;
width: 100%;
transform: translate3d(0, 0, 0);
backface-visibility: hidden;
perspective: 1000px;
transform-origin: center center;
contain: layout style paint;
transition: transform 0.25s cubic-bezier(0.23, 1, 0.32, 1), opacity 0.25s ease-out;
}
.window-sliding-down {
animation: slideDownFromHeader 0.25s cubic-bezier(0.23, 1, 0.32, 1) forwards;
will-change: transform, opacity;
transform-style: preserve-3d;
}
.window-sliding-up {
animation: slideUpToHeader 0.18s cubic-bezier(0.55, 0.085, 0.68, 0.53) forwards;
will-change: transform, opacity;
transform-style: preserve-3d;
}
.window-hidden {
opacity: 0;
transform: translate3d(0, -18px, 0) scale3d(0.96, 0.96, 1);
pointer-events: none;
will-change: auto;
contain: layout style paint;
}
.listen-window-moving {
transition: transform 0.35s cubic-bezier(0.25, 0.46, 0.45, 0.94);
will-change: transform;
}
.listen-window-center {
transform: translate3d(0, 0, 0);
}
.listen-window-left {
transform: translate3d(-110px, 0, 0);
}
@keyframes slideDownFromHeader {
0% {
opacity: 0;
transform: translate3d(0, -18px, 0) scale3d(0.96, 0.96, 1);
}
25% {
opacity: 0.4;
transform: translate3d(0, -10px, 0) scale3d(0.98, 0.98, 1);
}
50% {
opacity: 0.7;
transform: translate3d(0, -3px, 0) scale3d(1.01, 1.01, 1);
}
75% {
opacity: 0.9;
transform: translate3d(0, -0.5px, 0) scale3d(1.005, 1.005, 1);
}
100% {
opacity: 1;
transform: translate3d(0, 0, 0) scale3d(1, 1, 1);
}
}
.settings-window-show {
animation: settingsPopFromButton 0.22s cubic-bezier(0.23, 1, 0.32, 1) forwards;
transform-origin: 85% 0%;
will-change: transform, opacity;
transform-style: preserve-3d;
}
.settings-window-hide {
animation: settingsCollapseToButton 0.18s cubic-bezier(0.55, 0.085, 0.68, 0.53) forwards;
transform-origin: 85% 0%;
will-change: transform, opacity;
transform-style: preserve-3d;
}
@keyframes settingsPopFromButton {
0% {
opacity: 0;
transform: translate3d(0, -8px, 0) scale3d(0.5, 0.5, 1);
}
40% {
opacity: 0.8;
transform: translate3d(0, -2px, 0) scale3d(1.05, 1.05, 1);
}
70% {
opacity: 0.95;
transform: translate3d(0, 0, 0) scale3d(1.02, 1.02, 1);
}
100% {
opacity: 1;
transform: translate3d(0, 0, 0) scale3d(1, 1, 1);
}
}
@keyframes settingsCollapseToButton {
0% {
opacity: 1;
transform: translate3d(0, 0, 0) scale3d(1, 1, 1);
}
30% {
opacity: 0.8;
transform: translate3d(0, -1px, 0) scale3d(0.9, 0.9, 1);
}
70% {
opacity: 0.3;
transform: translate3d(0, -5px, 0) scale3d(0.7, 0.7, 1);
}
100% {
opacity: 0;
transform: translate3d(0, -8px, 0) scale3d(0.5, 0.5, 1);
}
}
@keyframes slideUpToHeader {
0% {
opacity: 1;
transform: translate3d(0, 0, 0) scale3d(1, 1, 1);
}
30% {
opacity: 0.6;
transform: translate3d(0, -6px, 0) scale3d(0.98, 0.98, 1);
}
65% {
opacity: 0.2;
transform: translate3d(0, -14px, 0) scale3d(0.95, 0.95, 1);
}
100% {
opacity: 0;
transform: translate3d(0, -18px, 0) scale3d(0.93, 0.93, 1);
}
}
</style>
</head>
<body>
<script src="../assets/marked-4.3.0.min.js"></script>
<script type="module" src="../../public/build/content.js"></script>
<pickle-glass-app id="pickle-glass"></pickle-glass-app>
<script>
window.addEventListener('DOMContentLoaded', () => {
const app = document.getElementById('pickle-glass');
let animationTimeout = null;
if (window.require) {
const { ipcRenderer } = window.require('electron');
ipcRenderer.on('window-show-animation', () => {
console.log('Starting window show animation');
app.classList.remove('window-hidden', 'window-sliding-up', 'settings-window-hide');
app.classList.add('window-sliding-down');
if (animationTimeout) clearTimeout(animationTimeout);
animationTimeout = setTimeout(() => {
app.classList.remove('window-sliding-down');
}, 250);
});
ipcRenderer.on('settings-window-show-animation', () => {
console.log('Starting settings window show animation');
app.classList.remove('window-hidden', 'window-sliding-up', 'settings-window-hide');
app.classList.add('settings-window-show');
if (animationTimeout) clearTimeout(animationTimeout);
animationTimeout = setTimeout(() => {
app.classList.remove('settings-window-show');
}, 220);
});
ipcRenderer.on('window-hide-animation', () => {
console.log('Starting window hide animation');
app.classList.remove('window-sliding-down', 'settings-window-show');
app.classList.add('window-sliding-up');
if (animationTimeout) clearTimeout(animationTimeout);
animationTimeout = setTimeout(() => {
app.classList.remove('window-sliding-up');
app.classList.add('window-hidden');
}, 180);
});
ipcRenderer.on('settings-window-hide-animation', () => {
console.log('Starting settings window hide animation');
app.classList.remove('window-sliding-down', 'settings-window-show');
app.classList.add('settings-window-hide');
if (animationTimeout) clearTimeout(animationTimeout);
animationTimeout = setTimeout(() => {
app.classList.remove('settings-window-hide');
app.classList.add('window-hidden');
}, 180);
});
ipcRenderer.on('listen-window-move-to-center', () => {
console.log('Moving listen window to center');
app.classList.add('listen-window-moving');
app.classList.remove('listen-window-left');
app.classList.add('listen-window-center');
setTimeout(() => {
app.classList.remove('listen-window-moving');
}, 350);
});
ipcRenderer.on('listen-window-move-to-left', () => {
console.log('Moving listen window to left');
app.classList.add('listen-window-moving');
app.classList.remove('listen-window-center');
app.classList.add('listen-window-left');
setTimeout(() => {
app.classList.remove('listen-window-moving');
}, 350);
});
}
});
</script>
</body>
</html>

View File

@ -1,7 +1,7 @@
<!DOCTYPE html> <!DOCTYPE html>
<html> <html>
<head> <head>
<meta http-equiv="content-security-policy" content="script-src 'self' 'unsafe-inline' 'unsafe-eval'" /> <meta http-equiv="content-security-policy" content="script-src 'self' 'unsafe-inline'" />
<title>Pickle Glass Header</title> <title>Pickle Glass Header</title>
<style> <style>
html, html,
@ -15,14 +15,10 @@
</head> </head>
<body> <body>
<div id="header-container" tabindex="0" style="outline: none;"> <div id="header-container" tabindex="0" style="outline: none;">
<!-- <apikey-header id="apikey-header" style="display: none;"></apikey-header>
<app-header id="app-header" style="display: none;"></app-header> -->
</div> </div>
<script type="module" src="../../../public/build/header.js"></script> <script type="module" src="../../public/build/header.js"></script>
<script>
const params = new URLSearchParams(window.location.search);
if (params.get('glass') === 'true') {
document.body.classList.add('has-glass');
}
</script>
</body> </body>
</html> </html>

View File

Before

Width:  |  Height:  |  Size: 877 B

After

Width:  |  Height:  |  Size: 877 B

View File

Before

Width:  |  Height:  |  Size: 226 B

After

Width:  |  Height:  |  Size: 226 B

View File

Before

Width:  |  Height:  |  Size: 3.5 KiB

After

Width:  |  Height:  |  Size: 3.5 KiB

View File

Before

Width:  |  Height:  |  Size: 1.6 KiB

After

Width:  |  Height:  |  Size: 1.6 KiB

View File

Before

Width:  |  Height:  |  Size: 125 KiB

After

Width:  |  Height:  |  Size: 125 KiB

View File

Before

Width:  |  Height:  |  Size: 68 KiB

After

Width:  |  Height:  |  Size: 68 KiB

View File

Before

Width:  |  Height:  |  Size: 1.3 KiB

After

Width:  |  Height:  |  Size: 1.3 KiB

View File

@ -1,239 +0,0 @@
// src/bridge/featureBridge.js
const { ipcMain, app, BrowserWindow } = require('electron');
const settingsService = require('../features/settings/settingsService');
const authService = require('../features/common/services/authService');
const whisperService = require('../features/common/services/whisperService');
const ollamaService = require('../features/common/services/ollamaService');
const modelStateService = require('../features/common/services/modelStateService');
const shortcutsService = require('../features/shortcuts/shortcutsService');
const presetRepository = require('../features/common/repositories/preset');
const localAIManager = require('../features/common/services/localAIManager');
const askService = require('../features/ask/askService');
const listenService = require('../features/listen/listenService');
const permissionService = require('../features/common/services/permissionService');
const encryptionService = require('../features/common/services/encryptionService');
module.exports = {
// Renderer로부터의 요청을 수신하고 서비스로 전달
initialize() {
// Settings Service
ipcMain.handle('settings:getPresets', async () => await settingsService.getPresets());
ipcMain.handle('settings:get-auto-update', async () => await settingsService.getAutoUpdateSetting());
ipcMain.handle('settings:set-auto-update', async (event, isEnabled) => await settingsService.setAutoUpdateSetting(isEnabled));
ipcMain.handle('settings:get-model-settings', async () => await settingsService.getModelSettings());
ipcMain.handle('settings:clear-api-key', async (e, { provider }) => await settingsService.clearApiKey(provider));
ipcMain.handle('settings:set-selected-model', async (e, { type, modelId }) => await settingsService.setSelectedModel(type, modelId));
ipcMain.handle('settings:get-ollama-status', async () => await settingsService.getOllamaStatus());
ipcMain.handle('settings:ensure-ollama-ready', async () => await settingsService.ensureOllamaReady());
ipcMain.handle('settings:shutdown-ollama', async () => await settingsService.shutdownOllama());
// Shortcuts
ipcMain.handle('settings:getCurrentShortcuts', async () => await shortcutsService.loadKeybinds());
ipcMain.handle('shortcut:getDefaultShortcuts', async () => await shortcutsService.handleRestoreDefaults());
ipcMain.handle('shortcut:closeShortcutSettingsWindow', async () => await shortcutsService.closeShortcutSettingsWindow());
ipcMain.handle('shortcut:openShortcutSettingsWindow', async () => await shortcutsService.openShortcutSettingsWindow());
ipcMain.handle('shortcut:saveShortcuts', async (event, newKeybinds) => await shortcutsService.handleSaveShortcuts(newKeybinds));
ipcMain.handle('shortcut:toggleAllWindowsVisibility', async () => await shortcutsService.toggleAllWindowsVisibility());
// Permissions
ipcMain.handle('check-system-permissions', async () => await permissionService.checkSystemPermissions());
ipcMain.handle('request-microphone-permission', async () => await permissionService.requestMicrophonePermission());
ipcMain.handle('open-system-preferences', async (event, section) => await permissionService.openSystemPreferences(section));
ipcMain.handle('mark-keychain-completed', async () => await permissionService.markKeychainCompleted());
ipcMain.handle('check-keychain-completed', async () => await permissionService.checkKeychainCompleted());
ipcMain.handle('initialize-encryption-key', async () => {
const userId = authService.getCurrentUserId();
await encryptionService.initializeKey(userId);
return { success: true };
});
// User/Auth
ipcMain.handle('get-current-user', () => authService.getCurrentUser());
ipcMain.handle('start-firebase-auth', async () => await authService.startFirebaseAuthFlow());
ipcMain.handle('firebase-logout', async () => await authService.signOut());
// App
ipcMain.handle('quit-application', () => app.quit());
// Whisper
ipcMain.handle('whisper:download-model', async (event, modelId) => await whisperService.handleDownloadModel(modelId));
ipcMain.handle('whisper:get-installed-models', async () => await whisperService.handleGetInstalledModels());
// General
ipcMain.handle('get-preset-templates', () => presetRepository.getPresetTemplates());
ipcMain.handle('get-web-url', () => process.env.pickleglass_WEB_URL || 'http://localhost:3000');
// Ollama
ipcMain.handle('ollama:get-status', async () => await ollamaService.handleGetStatus());
ipcMain.handle('ollama:install', async () => await ollamaService.handleInstall());
ipcMain.handle('ollama:start-service', async () => await ollamaService.handleStartService());
ipcMain.handle('ollama:ensure-ready', async () => await ollamaService.handleEnsureReady());
ipcMain.handle('ollama:get-models', async () => await ollamaService.handleGetModels());
ipcMain.handle('ollama:get-model-suggestions', async () => await ollamaService.handleGetModelSuggestions());
ipcMain.handle('ollama:pull-model', async (event, modelName) => await ollamaService.handlePullModel(modelName));
ipcMain.handle('ollama:is-model-installed', async (event, modelName) => await ollamaService.handleIsModelInstalled(modelName));
ipcMain.handle('ollama:warm-up-model', async (event, modelName) => await ollamaService.handleWarmUpModel(modelName));
ipcMain.handle('ollama:auto-warm-up', async () => await ollamaService.handleAutoWarmUp());
ipcMain.handle('ollama:get-warm-up-status', async () => await ollamaService.handleGetWarmUpStatus());
ipcMain.handle('ollama:shutdown', async (event, force = false) => await ollamaService.handleShutdown(force));
// Ask
ipcMain.handle('ask:sendQuestionFromAsk', async (event, userPrompt) => await askService.sendMessage(userPrompt));
ipcMain.handle('ask:sendQuestionFromSummary', async (event, userPrompt) => await askService.sendMessage(userPrompt));
ipcMain.handle('ask:toggleAskButton', async () => await askService.toggleAskButton());
ipcMain.handle('ask:closeAskWindow', async () => await askService.closeAskWindow());
// Listen
ipcMain.handle('listen:sendMicAudio', async (event, { data, mimeType }) => await listenService.handleSendMicAudioContent(data, mimeType));
ipcMain.handle('listen:sendSystemAudio', async (event, { data, mimeType }) => {
const result = await listenService.sttService.sendSystemAudioContent(data, mimeType);
if(result.success) {
listenService.sendToRenderer('system-audio-data', { data });
}
return result;
});
ipcMain.handle('listen:startMacosSystemAudio', async () => await listenService.handleStartMacosAudio());
ipcMain.handle('listen:stopMacosSystemAudio', async () => await listenService.handleStopMacosAudio());
ipcMain.handle('update-google-search-setting', async (event, enabled) => await listenService.handleUpdateGoogleSearchSetting(enabled));
ipcMain.handle('listen:isSessionActive', async () => await listenService.isSessionActive());
ipcMain.handle('listen:changeSession', async (event, listenButtonText) => {
console.log('[FeatureBridge] listen:changeSession from mainheader', listenButtonText);
try {
await listenService.handleListenRequest(listenButtonText);
return { success: true };
} catch (error) {
console.error('[FeatureBridge] listen:changeSession failed', error.message);
return { success: false, error: error.message };
}
});
// ModelStateService
ipcMain.handle('model:validate-key', async (e, { provider, key }) => await modelStateService.handleValidateKey(provider, key));
ipcMain.handle('model:get-all-keys', async () => await modelStateService.getAllApiKeys());
ipcMain.handle('model:set-api-key', async (e, { provider, key }) => await modelStateService.setApiKey(provider, key));
ipcMain.handle('model:remove-api-key', async (e, provider) => await modelStateService.handleRemoveApiKey(provider));
ipcMain.handle('model:get-selected-models', async () => await modelStateService.getSelectedModels());
ipcMain.handle('model:set-selected-model', async (e, { type, modelId }) => await modelStateService.handleSetSelectedModel(type, modelId));
ipcMain.handle('model:get-available-models', async (e, { type }) => await modelStateService.getAvailableModels(type));
ipcMain.handle('model:are-providers-configured', async () => await modelStateService.areProvidersConfigured());
ipcMain.handle('model:get-provider-config', () => modelStateService.getProviderConfig());
ipcMain.handle('model:re-initialize-state', async () => await modelStateService.initialize());
// LocalAIManager 이벤트를 모든 윈도우에 브로드캐스트
localAIManager.on('install-progress', (service, data) => {
const event = { service, ...data };
BrowserWindow.getAllWindows().forEach(win => {
if (win && !win.isDestroyed()) {
win.webContents.send('localai:install-progress', event);
}
});
});
localAIManager.on('installation-complete', (service) => {
BrowserWindow.getAllWindows().forEach(win => {
if (win && !win.isDestroyed()) {
win.webContents.send('localai:installation-complete', { service });
}
});
});
localAIManager.on('error', (error) => {
BrowserWindow.getAllWindows().forEach(win => {
if (win && !win.isDestroyed()) {
win.webContents.send('localai:error-occurred', error);
}
});
});
// Handle error-occurred events from LocalAIManager's error handling
localAIManager.on('error-occurred', (error) => {
BrowserWindow.getAllWindows().forEach(win => {
if (win && !win.isDestroyed()) {
win.webContents.send('localai:error-occurred', error);
}
});
});
localAIManager.on('model-ready', (data) => {
BrowserWindow.getAllWindows().forEach(win => {
if (win && !win.isDestroyed()) {
win.webContents.send('localai:model-ready', data);
}
});
});
localAIManager.on('state-changed', (service, state) => {
const event = { service, ...state };
BrowserWindow.getAllWindows().forEach(win => {
if (win && !win.isDestroyed()) {
win.webContents.send('localai:service-status-changed', event);
}
});
});
// 주기적 상태 동기화 시작
localAIManager.startPeriodicSync();
// ModelStateService 이벤트를 모든 윈도우에 브로드캐스트
modelStateService.on('state-updated', (state) => {
BrowserWindow.getAllWindows().forEach(win => {
if (win && !win.isDestroyed()) {
win.webContents.send('model-state:updated', state);
}
});
});
modelStateService.on('settings-updated', () => {
BrowserWindow.getAllWindows().forEach(win => {
if (win && !win.isDestroyed()) {
win.webContents.send('settings-updated');
}
});
});
modelStateService.on('force-show-apikey-header', () => {
BrowserWindow.getAllWindows().forEach(win => {
if (win && !win.isDestroyed()) {
win.webContents.send('force-show-apikey-header');
}
});
});
// LocalAI 통합 핸들러 추가
ipcMain.handle('localai:install', async (event, { service, options }) => {
return await localAIManager.installService(service, options);
});
ipcMain.handle('localai:get-status', async (event, service) => {
return await localAIManager.getServiceStatus(service);
});
ipcMain.handle('localai:start-service', async (event, service) => {
return await localAIManager.startService(service);
});
ipcMain.handle('localai:stop-service', async (event, service) => {
return await localAIManager.stopService(service);
});
ipcMain.handle('localai:install-model', async (event, { service, modelId, options }) => {
return await localAIManager.installModel(service, modelId, options);
});
ipcMain.handle('localai:get-installed-models', async (event, service) => {
return await localAIManager.getInstalledModels(service);
});
ipcMain.handle('localai:run-diagnostics', async (event, service) => {
return await localAIManager.runDiagnostics(service);
});
ipcMain.handle('localai:repair-service', async (event, service) => {
return await localAIManager.repairService(service);
});
// 에러 처리 핸들러
ipcMain.handle('localai:handle-error', async (event, { service, errorType, details }) => {
return await localAIManager.handleError(service, errorType, details);
});
// 전체 상태 조회
ipcMain.handle('localai:get-all-states', async (event) => {
return await localAIManager.getAllServiceStates();
});
console.log('[FeatureBridge] Initialized with all feature handlers.');
},
// Renderer로 상태를 전송
sendAskProgress(win, progress) {
win.webContents.send('feature:ask:progress', progress);
},
};

View File

@ -1,11 +0,0 @@
// src/bridge/internalBridge.js
const { EventEmitter } = require('events');
// FeatureCore와 WindowCore를 잇는 내부 이벤트 버스
const internalBridge = new EventEmitter();
module.exports = internalBridge;
// 예시 이벤트
// internalBridge.on('content-protection-changed', (enabled) => {
// // windowManager에서 처리
// });

View File

@ -1,34 +0,0 @@
// src/bridge/windowBridge.js
const { ipcMain, shell } = require('electron');
// Bridge는 단순히 IPC 핸들러를 등록하는 역할만 함 (비즈니스 로직 없음)
module.exports = {
initialize() {
// initialize 시점에 windowManager를 require하여 circular dependency 문제 해결
const windowManager = require('../window/windowManager');
// 기존 IPC 핸들러들
ipcMain.handle('toggle-content-protection', () => windowManager.toggleContentProtection());
ipcMain.handle('resize-header-window', (event, args) => windowManager.resizeHeaderWindow(args));
ipcMain.handle('get-content-protection-status', () => windowManager.getContentProtectionStatus());
ipcMain.on('show-settings-window', () => windowManager.showSettingsWindow());
ipcMain.on('hide-settings-window', () => windowManager.hideSettingsWindow());
ipcMain.on('cancel-hide-settings-window', () => windowManager.cancelHideSettingsWindow());
ipcMain.handle('open-login-page', () => windowManager.openLoginPage());
ipcMain.handle('open-personalize-page', () => windowManager.openLoginPage());
ipcMain.handle('move-window-step', (event, direction) => windowManager.moveWindowStep(direction));
ipcMain.handle('open-external', (event, url) => shell.openExternal(url));
// Newly moved handlers from windowManager
ipcMain.on('header-state-changed', (event, state) => windowManager.handleHeaderStateChanged(state));
ipcMain.on('header-animation-finished', (event, state) => windowManager.handleHeaderAnimationFinished(state));
ipcMain.handle('get-header-position', () => windowManager.getHeaderPosition());
ipcMain.handle('move-header-to', (event, newX, newY) => windowManager.moveHeaderTo(newX, newY));
ipcMain.handle('adjust-window-height', (event, { winName, height }) => windowManager.adjustWindowHeight(winName, height));
},
notifyFocusChange(win, isFocused) {
win.webContents.send('window:focus-change', isFocused);
}
};

View File

@ -0,0 +1,239 @@
const axios = require('axios');
const config = require('../config/config');
class APIClient {
constructor() {
this.baseURL = config.get('apiUrl');
this.client = axios.create({
baseURL: this.baseURL,
timeout: config.get('apiTimeout'),
headers: {
'Content-Type': 'application/json'
}
});
this.client.interceptors.response.use(
(response) => response,
(error) => {
console.error('API request failed:', error.message);
if (error.response) {
console.error('response status:', error.response.status);
console.error('response data:', error.response.data);
}
return Promise.reject(error);
}
);
}
async initialize() {
try {
const response = await this.client.get('/api/auth/status');
console.log('[APIClient] checked default user status:', response.data);
return true;
} catch (error) {
console.error('[APIClient] failed to initialize:', error);
return false;
}
}
async checkConnection() {
try {
const response = await this.client.get('/');
return response.status === 200;
} catch (error) {
return false;
}
}
async saveApiKey(apiKey) {
try {
const response = await this.client.post('/api/user/api-key', { apiKey });
return response.data;
} catch (error) {
console.error('failed to save api key:', error);
throw error;
}
}
async checkApiKey() {
try {
const response = await this.client.get('/api/user/api-key');
return response.data;
} catch (error) {
console.error('failed to check api key:', error);
return { hasApiKey: false };
}
}
async getUserBatchData(includes = ['profile', 'context', 'presets']) {
try {
const includeParam = includes.join(',');
const response = await this.client.get(`/api/user/batch?include=${includeParam}`);
return response.data;
} catch (error) {
console.error('failed to get user batch data:', error);
return null;
}
}
async getUserContext() {
try {
const response = await this.client.get('/api/user/context');
return response.data.context;
} catch (error) {
console.error('fail to get user context:', error);
return null;
}
}
async getUserProfile() {
try {
const response = await this.client.get('/api/user/profile');
return response.data;
} catch (error) {
console.error('failed to get user profile:', error);
return null;
}
}
async getUserPresets() {
try {
const response = await this.client.get('/api/user/presets');
return response.data;
} catch (error) {
console.error('failed to get user presets:', error);
return [];
}
}
async updateUserContext(context) {
try {
const response = await this.client.post('/api/user/context', context);
return response.data;
} catch (error) {
console.error('failed to update user context:', error);
throw error;
}
}
async addActivity(activity) {
try {
const response = await this.client.post('/api/user/activities', activity);
return response.data;
} catch (error) {
console.error('failed to add activity:', error);
throw error;
}
}
async getPresetTemplates() {
try {
const response = await this.client.get('/api/preset-templates');
return response.data;
} catch (error) {
console.error('failed to get preset templates:', error);
return [];
}
}
async updateUserProfile(profile) {
try {
const response = await this.client.post('/api/user/profile', profile);
return response.data;
} catch (error) {
console.error('failed to update user profile:', error);
throw error;
}
}
async searchUsers(name = '') {
try {
const response = await this.client.get('/api/users/search', {
params: { name }
});
return response.data;
} catch (error) {
console.error('failed to search users:', error);
return [];
}
}
async getUserProfileById(userId) {
try {
const response = await this.client.get(`/api/users/${userId}/profile`);
return response.data;
} catch (error) {
console.error('failed to get user profile by id:', error);
return null;
}
}
async saveConversationSession(sessionId, conversationHistory) {
try {
const payload = {
sessionId,
conversationHistory
};
const response = await this.client.post('/api/conversations', payload);
return response.data;
} catch (error) {
console.error('failed to save conversation session:', error);
throw error;
}
}
async getConversationSession(sessionId) {
try {
const response = await this.client.get(`/api/conversations/${sessionId}`);
return response.data;
} catch (error) {
console.error('failed to get conversation session:', error);
return null;
}
}
async getAllConversationSessions() {
try {
const response = await this.client.get('/api/conversations');
return response.data;
} catch (error) {
console.error('failed to get all conversation sessions:', error);
return [];
}
}
async deleteConversationSession(sessionId) {
try {
const response = await this.client.delete(`/api/conversations/${sessionId}`);
return response.data;
} catch (error) {
console.error('failed to delete conversation session:', error);
throw error;
}
}
async getSyncStatus() {
try {
const response = await this.client.get('/api/sync/status');
return response.data;
} catch (error) {
console.error('failed to get sync status:', error);
return null;
}
}
async getFullUserData() {
try {
const response = await this.client.get('/api/user/full');
return response.data;
} catch (error) {
console.error('failed to get full user data:', error);
return null;
}
}
}
const apiClient = new APIClient();
module.exports = apiClient;
module.exports = apiClient;

View File

@ -0,0 +1,158 @@
const config = require('../config/config');
class DataService {
constructor() {
this.cache = new Map();
this.cacheTimeout = config.get('cacheTimeout');
this.enableCaching = config.get('enableCaching');
this.sqliteClient = null;
this.currentUserId = 'default_user';
this.isInitialized = false;
if (config.get('enableSQLiteStorage')) {
try {
this.sqliteClient = require('./sqliteClient');
console.log('[DataService] SQLite storage enabled.');
} catch (error) {
console.error('[DataService] Failed to load SQLite client:', error);
}
}
}
async initialize() {
if (this.isInitialized || !this.sqliteClient) {
return;
}
try {
await this.sqliteClient.connect();
this.isInitialized = true;
console.log('[DataService] Initialized successfully');
} catch (error) {
console.error('[DataService] Failed to initialize:', error);
throw error;
}
}
setCurrentUser(uid) {
if (this.currentUserId !== uid) {
console.log(`[DataService] Current user switched to: ${uid}`);
this.currentUserId = uid;
this.clearCache();
}
}
getCacheKey(operation, params = '') {
return `${this.currentUserId}:${operation}:${params}`;
}
getFromCache(key) {
if (!this.enableCaching) return null;
const cached = this.cache.get(key);
if (cached && (Date.now() - cached.timestamp) < this.cacheTimeout) {
return cached.data;
}
return null;
}
setCache(key, data) {
if (!this.enableCaching) return;
this.cache.set(key, { data, timestamp: Date.now() });
}
clearCache() {
this.cache.clear();
}
async findOrCreateUser(firebaseUser) {
if (!this.sqliteClient) {
console.log('[DataService] SQLite client not available, skipping user creation');
return firebaseUser;
}
try {
await this.initialize();
const existingUser = await this.sqliteClient.getUser(firebaseUser.uid);
if (!existingUser) {
console.log(`[DataService] Creating new user in local DB: ${firebaseUser.uid}`);
await this.sqliteClient.findOrCreateUser({
uid: firebaseUser.uid,
display_name: firebaseUser.displayName || firebaseUser.display_name,
email: firebaseUser.email
});
}
this.clearCache();
return firebaseUser;
} catch (error) {
console.error('[DataService] Failed to sync Firebase user to local DB:', error);
return firebaseUser;
}
}
async saveApiKey(apiKey) {
if (!this.sqliteClient) {
throw new Error("SQLite client not available.");
}
try {
await this.initialize();
const result = await this.sqliteClient.saveApiKey(apiKey, this.currentUserId);
this.clearCache();
return result;
} catch (error) {
console.error('[DataService] Failed to save API key to SQLite:', error);
throw error;
}
}
async checkApiKey() {
if (!this.sqliteClient) return { hasApiKey: false };
try {
await this.initialize();
const user = await this.sqliteClient.getUser(this.currentUserId);
return { hasApiKey: !!user?.api_key && user.api_key.length > 0 };
} catch (error) {
console.error('[DataService] Failed to check API key from SQLite:', error);
return { hasApiKey: false };
}
}
async getUserPresets() {
const cacheKey = this.getCacheKey('presets');
const cached = this.getFromCache(cacheKey);
if (cached) return cached;
if (!this.sqliteClient) return [];
try {
await this.initialize();
const presets = await this.sqliteClient.getPresets(this.currentUserId);
this.setCache(cacheKey, presets);
return presets;
} catch (error) {
console.error('[DataService] Failed to get presets from SQLite:', error);
return [];
}
}
async getPresetTemplates() {
const cacheKey = this.getCacheKey('preset_templates');
const cached = this.getFromCache(cacheKey);
if (cached) return cached;
if (!this.sqliteClient) return [];
try {
await this.initialize();
const templates = await this.sqliteClient.getPresetTemplates();
this.setCache(cacheKey, templates);
return templates;
} catch (error) {
console.error('[DataService] Failed to get preset templates from SQLite:', error);
return [];
}
}
}
const dataService = new DataService();
module.exports = dataService;

View File

@ -10,13 +10,10 @@ class DatabaseInitializer {
// 최종적으로 사용될 DB 경로 (쓰기 가능한 위치) // 최종적으로 사용될 DB 경로 (쓰기 가능한 위치)
const userDataPath = app.getPath('userData'); const userDataPath = app.getPath('userData');
// In both development and production mode, the database is stored in the userData directory:
// macOS: ~/Library/Application Support/Glass/pickleglass.db
// Windows: %APPDATA%\Glass\pickleglass.db
this.dbPath = path.join(userDataPath, 'pickleglass.db'); this.dbPath = path.join(userDataPath, 'pickleglass.db');
this.dataDir = userDataPath; this.dataDir = userDataPath;
// The original DB path (read-only location in the package) // 원본 DB 경로 (패키지 내 읽기 전용 위치)
this.sourceDbPath = app.isPackaged this.sourceDbPath = app.isPackaged
? path.join(process.resourcesPath, 'data', 'pickleglass.db') ? path.join(process.resourcesPath, 'data', 'pickleglass.db')
: path.join(app.getAppPath(), 'data', 'pickleglass.db'); : path.join(app.getAppPath(), 'data', 'pickleglass.db');
@ -55,13 +52,15 @@ class DatabaseInitializer {
try { try {
this.ensureDatabaseExists(); this.ensureDatabaseExists();
sqliteClient.connect(this.dbPath); // DB 경로를 인자로 전달 await sqliteClient.connect(this.dbPath); // DB 경로를 인자로 전달
// This single call will now synchronize the schema and then init default data. // 연결 후 테이블 및 기본 데이터 초기화
await sqliteClient.initTables(); await sqliteClient.initTables();
const user = await sqliteClient.getUser(sqliteClient.defaultUserId);
// Clean up any orphaned sessions from previous versions if (!user) {
await sqliteClient.cleanupEmptySessions(); await sqliteClient.initDefaultData();
console.log('[DB] Default data initialized.');
}
this.isInitialized = true; this.isInitialized = true;
console.log('[DB] Database initialized successfully'); console.log('[DB] Database initialized successfully');
@ -142,10 +141,6 @@ class DatabaseInitializer {
try { try {
console.log('[DatabaseInitializer] Validating database integrity...'); console.log('[DatabaseInitializer] Validating database integrity...');
// The synchronizeSchema function handles table and column creation now.
// We just need to ensure default data is present.
await sqliteClient.synchronizeSchema();
const defaultUser = await sqliteClient.getUser(sqliteClient.defaultUserId); const defaultUser = await sqliteClient.getUser(sqliteClient.defaultUserId);
if (!defaultUser) { if (!defaultUser) {
console.log('[DatabaseInitializer] Default user not found - creating...'); console.log('[DatabaseInitializer] Default user not found - creating...');

View File

@ -0,0 +1,177 @@
const OpenAI = require('openai');
const WebSocket = require('ws');
/**
* Creates and returns an OpenAI client instance for STT (Speech-to-Text).
* @param {string} apiKey - The API key for authentication.
* @returns {OpenAI} The initialized OpenAI client.
*/
function createOpenAiClient(apiKey) {
return new OpenAI({
apiKey: apiKey,
});
}
/**
* Creates and returns an OpenAI client instance for text/image generation.
* @param {string} apiKey - The API key for authentication.
* @returns {OpenAI} The initialized OpenAI client.
*/
function createOpenAiGenerativeClient(apiKey) {
return new OpenAI({
apiKey: apiKey,
});
}
/**
* Connects to an OpenAI Realtime WebSocket session for STT.
* @param {string} key - Portkey vKey or OpenAI apiKey.
* @param {object} config - The configuration object for the realtime session.
* @param {'apiKey'|'vKey'} keyType - key type ('apiKey' | 'vKey').
* @returns {Promise<object>} A promise that resolves to the session object with send and close methods.
*/
async function connectToOpenAiSession(key, config, keyType) {
if (keyType !== 'apiKey' && keyType !== 'vKey') {
throw new Error('keyType must be either "apiKey" or "vKey".');
}
const wsUrl = keyType === 'apiKey'
? 'wss://api.openai.com/v1/realtime?intent=transcription'
: 'wss://api.portkey.ai/v1/realtime?intent=transcription';
const headers = keyType === 'apiKey'
? {
'Authorization': `Bearer ${key}`,
'OpenAI-Beta' : 'realtime=v1',
}
: {
'x-portkey-api-key' : 'gRv2UGRMq6GGLJ8aVEB4e7adIewu',
'x-portkey-virtual-key': key,
'OpenAI-Beta' : 'realtime=v1',
};
const ws = new WebSocket(wsUrl, { headers });
return new Promise((resolve, reject) => {
ws.onopen = () => {
console.log("WebSocket session opened.");
const sessionConfig = {
type: 'transcription_session.update',
session: {
input_audio_format: 'pcm16',
input_audio_transcription: {
model: 'gpt-4o-mini-transcribe',
prompt: config.prompt || '',
language: config.language || 'en'
},
turn_detection: {
type: 'server_vad',
threshold: 0.5,
prefix_padding_ms: 50,
silence_duration_ms: 25,
},
input_audio_noise_reduction: {
type: 'near_field'
}
}
};
ws.send(JSON.stringify(sessionConfig));
resolve({
sendRealtimeInput: (audioData) => {
if (ws.readyState === WebSocket.OPEN) {
const message = {
type: 'input_audio_buffer.append',
audio: audioData
};
ws.send(JSON.stringify(message));
}
},
close: () => {
if (ws.readyState === WebSocket.OPEN) {
ws.send(JSON.stringify({ type: 'session.close' }));
ws.close(1000, 'Client initiated close.');
}
}
});
};
ws.onmessage = (event) => {
const message = JSON.parse(event.data);
if (config.callbacks && config.callbacks.onmessage) {
config.callbacks.onmessage(message);
}
};
ws.onerror = (error) => {
console.error('WebSocket error:', error.message);
if (config.callbacks && config.callbacks.onerror) {
config.callbacks.onerror(error);
}
reject(error);
};
ws.onclose = (event) => {
console.log(`WebSocket closed: ${event.code} ${event.reason}`);
if (config.callbacks && config.callbacks.onclose) {
config.callbacks.onclose(event);
}
};
});
}
/**
* Gets a GPT model for text/image generation.
* @param {OpenAI} client - The OpenAI client instance.
* @param {string} [model='gpt-4.1'] - The name for the text/vision model.
* @returns {object} Model object with generateContent method
*/
function getOpenAiGenerativeModel(client, model = 'gpt-4.1') {
return {
generateContent: async (parts) => {
const messages = [];
let systemPrompt = '';
let userContent = [];
for (const part of parts) {
if (typeof part === 'string') {
if (systemPrompt === '' && part.includes('You are')) {
systemPrompt = part;
} else {
userContent.push({ type: 'text', text: part });
}
} else if (part.inlineData) {
userContent.push({
type: 'image_url',
image_url: { url: `data:${part.inlineData.mimeType};base64,${part.inlineData.data}` }
});
}
}
if (systemPrompt) messages.push({ role: 'system', content: systemPrompt });
if (userContent.length > 0) messages.push({ role: 'user', content: userContent });
const response = await client.chat.completions.create({
model: model,
messages: messages,
temperature: 0.7,
max_tokens: 2048
});
return {
response: {
text: () => response.choices[0].message.content
}
};
}
};
}
module.exports = {
createOpenAiClient,
connectToOpenAiSession,
createOpenAiGenerativeClient,
getOpenAiGenerativeModel,
};

View File

@ -0,0 +1,337 @@
const sqlite3 = require('sqlite3').verbose();
const path = require('path');
class SQLiteClient {
constructor() {
this.db = null;
this.dbPath = null;
this.defaultUserId = 'default_user';
}
connect(dbPath) {
return new Promise((resolve, reject) => {
if (this.db) {
console.log('[SQLiteClient] Already connected.');
return resolve();
}
this.dbPath = dbPath;
this.db = new sqlite3.Database(this.dbPath, (err) => {
if (err) {
console.error('[SQLiteClient] Could not connect to database', err);
return reject(err);
}
console.log('[SQLiteClient] Connected successfully to:', this.dbPath);
this.db.run('PRAGMA journal_mode = WAL;', (err) => {
if (err) {
return reject(err);
}
resolve();
});
});
});
}
async initTables() {
return new Promise((resolve, reject) => {
const schema = `
PRAGMA journal_mode = WAL;
CREATE TABLE IF NOT EXISTS users (
uid TEXT PRIMARY KEY,
display_name TEXT NOT NULL,
email TEXT NOT NULL,
created_at INTEGER,
api_key TEXT
);
CREATE TABLE IF NOT EXISTS sessions (
id TEXT PRIMARY KEY,
uid TEXT NOT NULL,
title TEXT,
started_at INTEGER,
ended_at INTEGER,
sync_state TEXT DEFAULT 'clean',
updated_at INTEGER
);
CREATE TABLE IF NOT EXISTS transcripts (
id TEXT PRIMARY KEY,
session_id TEXT NOT NULL,
start_at INTEGER,
end_at INTEGER,
speaker TEXT,
text TEXT,
lang TEXT,
created_at INTEGER,
sync_state TEXT DEFAULT 'clean'
);
CREATE TABLE IF NOT EXISTS ai_messages (
id TEXT PRIMARY KEY,
session_id TEXT NOT NULL,
sent_at INTEGER,
role TEXT,
content TEXT,
tokens INTEGER,
model TEXT,
created_at INTEGER,
sync_state TEXT DEFAULT 'clean'
);
CREATE TABLE IF NOT EXISTS summaries (
session_id TEXT PRIMARY KEY,
generated_at INTEGER,
model TEXT,
text TEXT,
tldr TEXT,
bullet_json TEXT,
action_json TEXT,
tokens_used INTEGER,
updated_at INTEGER,
sync_state TEXT DEFAULT 'clean'
);
CREATE TABLE IF NOT EXISTS prompt_presets (
id TEXT PRIMARY KEY,
uid TEXT NOT NULL,
title TEXT NOT NULL,
prompt TEXT NOT NULL,
is_default INTEGER NOT NULL,
created_at INTEGER,
sync_state TEXT DEFAULT 'clean'
);
`;
this.db.exec(schema, (err) => {
if (err) {
console.error('Failed to create tables:', err);
return reject(err);
}
console.log('All tables are ready.');
this.initDefaultData().then(resolve).catch(reject);
});
});
}
async initDefaultData() {
return new Promise((resolve, reject) => {
const now = Math.floor(Date.now() / 1000);
const initUserQuery = `
INSERT OR IGNORE INTO users (uid, display_name, email, created_at)
VALUES (?, ?, ?, ?)
`;
this.db.run(initUserQuery, [this.defaultUserId, 'Default User', 'contact@pickle.com', now], (err) => {
if (err) {
console.error('Failed to initialize default user:', err);
return reject(err);
}
const defaultPresets = [
['school', 'School', 'You are a school and lecture assistant. Your goal is to help the user, a student, understand academic material and answer questions.\n\nWhenever a question appears on the user\'s screen or is asked aloud, you provide a direct, step-by-step answer, showing all necessary reasoning or calculations.\n\nIf the user is watching a lecture or working through new material, you offer concise explanations of key concepts and clarify definitions as they come up.', 1],
['meetings', 'Meetings', 'You are a meeting assistant. Your goal is to help the user capture key information during meetings and follow up effectively.\n\nYou help capture meeting notes, track action items, identify key decisions, and summarize important points discussed during meetings.', 1],
['sales', 'Sales', 'You are a real-time AI sales assistant, and your goal is to help the user close deals during sales interactions.\n\nYou provide real-time sales support, suggest responses to objections, help identify customer needs, and recommend strategies to advance deals.', 1],
['recruiting', 'Recruiting', 'You are a recruiting assistant. Your goal is to help the user interview candidates and evaluate talent effectively.\n\nYou help evaluate candidates, suggest interview questions, analyze responses, and provide insights about candidate fit for positions.', 1],
['customer-support', 'Customer Support', 'You are a customer support assistant. Your goal is to help resolve customer issues efficiently and thoroughly.\n\nYou help diagnose customer problems, suggest solutions, provide step-by-step troubleshooting guidance, and ensure customer satisfaction.', 1],
];
const stmt = this.db.prepare(`
INSERT OR IGNORE INTO prompt_presets (id, uid, title, prompt, is_default, created_at)
VALUES (?, ?, ?, ?, ?, ?)
`);
for (const preset of defaultPresets) {
stmt.run(preset[0], this.defaultUserId, preset[1], preset[2], preset[3], now);
}
stmt.finalize((err) => {
if (err) {
console.error('Failed to finalize preset statement:', err);
return reject(err);
}
console.log('Default data initialized.');
resolve();
});
});
});
}
async findOrCreateUser(user) {
return new Promise((resolve, reject) => {
const { uid, display_name, email } = user;
const now = Math.floor(Date.now() / 1000);
const query = `
INSERT INTO users (uid, display_name, email, created_at)
VALUES (?, ?, ?, ?)
ON CONFLICT(uid) DO UPDATE SET
display_name=excluded.display_name,
email=excluded.email
`;
this.db.run(query, [uid, display_name, email, now], (err) => {
if (err) {
console.error('Failed to find or create user in SQLite:', err);
return reject(err);
}
this.getUser(uid).then(resolve).catch(reject);
});
});
}
async getUser(uid) {
return new Promise((resolve, reject) => {
this.db.get('SELECT * FROM users WHERE uid = ?', [uid], (err, row) => {
if (err) reject(err);
else resolve(row);
});
});
}
async saveApiKey(apiKey, uid = this.defaultUserId) {
return new Promise((resolve, reject) => {
this.db.run(
'UPDATE users SET api_key = ? WHERE uid = ?',
[apiKey, uid],
function(err) {
if (err) {
console.error('SQLite: Failed to save API key:', err);
reject(err);
} else {
console.log(`SQLite: API key saved for user ${uid}.`);
resolve({ changes: this.changes });
}
}
);
});
}
async getPresets(uid) {
return new Promise((resolve, reject) => {
const query = `
SELECT * FROM prompt_presets
WHERE uid = ? OR is_default = 1
ORDER BY is_default DESC, title ASC
`;
this.db.all(query, [uid], (err, rows) => {
if (err) {
console.error('SQLite: Failed to get presets:', err);
reject(err);
} else {
resolve(rows);
}
});
});
}
async getPresetTemplates() {
return new Promise((resolve, reject) => {
const query = `
SELECT * FROM prompt_presets
WHERE is_default = 1
ORDER BY title ASC
`;
this.db.all(query, [], (err, rows) => {
if (err) {
console.error('SQLite: Failed to get preset templates:', err);
reject(err);
} else {
resolve(rows);
}
});
});
}
async createSession(uid) {
return new Promise((resolve, reject) => {
const sessionId = require('crypto').randomUUID();
const now = Math.floor(Date.now() / 1000);
const query = `INSERT INTO sessions (id, uid, title, started_at, updated_at) VALUES (?, ?, ?, ?, ?)`;
this.db.run(query, [sessionId, uid, `Session @ ${new Date().toLocaleTimeString()}`, now, now], function(err) {
if (err) {
console.error('SQLite: Failed to create session:', err);
reject(err);
} else {
console.log(`SQLite: Created session ${sessionId} for user ${uid}`);
resolve(sessionId);
}
});
});
}
async endSession(sessionId) {
return new Promise((resolve, reject) => {
const now = Math.floor(Date.now() / 1000);
const query = `UPDATE sessions SET ended_at = ?, updated_at = ? WHERE id = ?`;
this.db.run(query, [now, now, sessionId], function(err) {
if (err) reject(err);
else resolve({ changes: this.changes });
});
});
}
async addTranscript({ sessionId, speaker, text }) {
return new Promise((resolve, reject) => {
const transcriptId = require('crypto').randomUUID();
const now = Math.floor(Date.now() / 1000);
const query = `INSERT INTO transcripts (id, session_id, start_at, speaker, text, created_at) VALUES (?, ?, ?, ?, ?, ?)`;
this.db.run(query, [transcriptId, sessionId, now, speaker, text, now], function(err) {
if (err) reject(err);
else resolve({ id: transcriptId });
});
});
}
async addAiMessage({ sessionId, role, content, model = 'gpt-4.1' }) {
return new Promise((resolve, reject) => {
const messageId = require('crypto').randomUUID();
const now = Math.floor(Date.now() / 1000);
const query = `INSERT INTO ai_messages (id, session_id, sent_at, role, content, model, created_at) VALUES (?, ?, ?, ?, ?, ?, ?)`;
this.db.run(query, [messageId, sessionId, now, role, content, model, now], function(err) {
if (err) reject(err);
else resolve({ id: messageId });
});
});
}
async saveSummary({ sessionId, tldr, text, bullet_json, action_json, model = 'gpt-4.1' }) {
return new Promise((resolve, reject) => {
const now = Math.floor(Date.now() / 1000);
const query = `
INSERT INTO summaries (session_id, generated_at, model, text, tldr, bullet_json, action_json, updated_at)
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
ON CONFLICT(session_id) DO UPDATE SET
generated_at=excluded.generated_at,
model=excluded.model,
text=excluded.text,
tldr=excluded.tldr,
bullet_json=excluded.bullet_json,
action_json=excluded.action_json,
updated_at=excluded.updated_at
`;
this.db.run(query, [sessionId, now, model, text, tldr, bullet_json, action_json, now], function(err) {
if (err) reject(err);
else resolve({ changes: this.changes });
});
});
}
close() {
if (this.db) {
this.db.close((err) => {
if (err) {
console.error('SQLite connection close failed:', err);
} else {
console.log('SQLite connection closed.');
}
});
this.db = null;
}
}
}
const sqliteClient = new SQLiteClient();
module.exports = sqliteClient;

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -1,450 +0,0 @@
const { BrowserWindow } = require('electron');
const { createStreamingLLM } = require('../common/ai/factory');
// Lazy require helper to avoid circular dependency issues
const getWindowManager = () => require('../../window/windowManager');
const internalBridge = require('../../bridge/internalBridge');
const getWindowPool = () => {
try {
return getWindowManager().windowPool;
} catch {
return null;
}
};
const sessionRepository = require('../common/repositories/session');
const askRepository = require('./repositories');
const { getSystemPrompt } = require('../common/prompts/promptBuilder');
const path = require('node:path');
const fs = require('node:fs');
const os = require('os');
const util = require('util');
const execFile = util.promisify(require('child_process').execFile);
const { desktopCapturer } = require('electron');
const modelStateService = require('../common/services/modelStateService');
// Try to load sharp, but don't fail if it's not available
let sharp;
try {
sharp = require('sharp');
console.log('[AskService] Sharp module loaded successfully');
} catch (error) {
console.warn('[AskService] Sharp module not available:', error.message);
console.warn('[AskService] Screenshot functionality will work with reduced image processing capabilities');
sharp = null;
}
let lastScreenshot = null;
async function captureScreenshot(options = {}) {
if (process.platform === 'darwin') {
try {
const tempPath = path.join(os.tmpdir(), `screenshot-${Date.now()}.jpg`);
await execFile('screencapture', ['-x', '-t', 'jpg', tempPath]);
const imageBuffer = await fs.promises.readFile(tempPath);
await fs.promises.unlink(tempPath);
if (sharp) {
try {
// Try using sharp for optimal image processing
const resizedBuffer = await sharp(imageBuffer)
.resize({ height: 384 })
.jpeg({ quality: 80 })
.toBuffer();
const base64 = resizedBuffer.toString('base64');
const metadata = await sharp(resizedBuffer).metadata();
lastScreenshot = {
base64,
width: metadata.width,
height: metadata.height,
timestamp: Date.now(),
};
return { success: true, base64, width: metadata.width, height: metadata.height };
} catch (sharpError) {
console.warn('Sharp module failed, falling back to basic image processing:', sharpError.message);
}
}
// Fallback: Return the original image without resizing
console.log('[AskService] Using fallback image processing (no resize/compression)');
const base64 = imageBuffer.toString('base64');
lastScreenshot = {
base64,
width: null, // We don't have metadata without sharp
height: null,
timestamp: Date.now(),
};
return { success: true, base64, width: null, height: null };
} catch (error) {
console.error('Failed to capture screenshot:', error);
return { success: false, error: error.message };
}
}
try {
const sources = await desktopCapturer.getSources({
types: ['screen'],
thumbnailSize: {
width: 1920,
height: 1080,
},
});
if (sources.length === 0) {
throw new Error('No screen sources available');
}
const source = sources[0];
const buffer = source.thumbnail.toJPEG(70);
const base64 = buffer.toString('base64');
const size = source.thumbnail.getSize();
return {
success: true,
base64,
width: size.width,
height: size.height,
};
} catch (error) {
console.error('Failed to capture screenshot using desktopCapturer:', error);
return {
success: false,
error: error.message,
};
}
}
/**
* @class
* @description
*/
class AskService {
constructor() {
this.abortController = null;
this.state = {
isVisible: false,
isLoading: false,
isStreaming: false,
currentQuestion: '',
currentResponse: '',
showTextInput: true,
};
console.log('[AskService] Service instance created.');
}
_broadcastState() {
const askWindow = getWindowPool()?.get('ask');
if (askWindow && !askWindow.isDestroyed()) {
askWindow.webContents.send('ask:stateUpdate', this.state);
}
}
async toggleAskButton(inputScreenOnly = false) {
const askWindow = getWindowPool()?.get('ask');
let shouldSendScreenOnly = false;
if (inputScreenOnly && this.state.showTextInput && askWindow && askWindow.isVisible()) {
shouldSendScreenOnly = true;
await this.sendMessage('', []);
return;
}
const hasContent = this.state.isLoading || this.state.isStreaming || (this.state.currentResponse && this.state.currentResponse.length > 0);
if (askWindow && askWindow.isVisible() && hasContent) {
this.state.showTextInput = !this.state.showTextInput;
this._broadcastState();
} else {
if (askWindow && askWindow.isVisible()) {
internalBridge.emit('window:requestVisibility', { name: 'ask', visible: false });
this.state.isVisible = false;
} else {
console.log('[AskService] Showing hidden Ask window');
internalBridge.emit('window:requestVisibility', { name: 'ask', visible: true });
this.state.isVisible = true;
}
if (this.state.isVisible) {
this.state.showTextInput = true;
this._broadcastState();
}
}
}
async closeAskWindow () {
if (this.abortController) {
this.abortController.abort('Window closed by user');
this.abortController = null;
}
this.state = {
isVisible : false,
isLoading : false,
isStreaming : false,
currentQuestion: '',
currentResponse: '',
showTextInput : true,
};
this._broadcastState();
internalBridge.emit('window:requestVisibility', { name: 'ask', visible: false });
return { success: true };
}
/**
*
* @param {string[]} conversationTexts
* @returns {string}
* @private
*/
_formatConversationForPrompt(conversationTexts) {
if (!conversationTexts || conversationTexts.length === 0) {
return 'No conversation history available.';
}
return conversationTexts.slice(-30).join('\n');
}
/**
*
* @param {string} userPrompt
* @returns {Promise<{success: boolean, response?: string, error?: string}>}
*/
async sendMessage(userPrompt, conversationHistoryRaw=[]) {
internalBridge.emit('window:requestVisibility', { name: 'ask', visible: true });
this.state = {
...this.state,
isLoading: true,
isStreaming: false,
currentQuestion: userPrompt,
currentResponse: '',
showTextInput: false,
};
this._broadcastState();
if (this.abortController) {
this.abortController.abort('New request received.');
}
this.abortController = new AbortController();
const { signal } = this.abortController;
let sessionId;
try {
console.log(`[AskService] 🤖 Processing message: ${userPrompt.substring(0, 50)}...`);
sessionId = await sessionRepository.getOrCreateActive('ask');
await askRepository.addAiMessage({ sessionId, role: 'user', content: userPrompt.trim() });
console.log(`[AskService] DB: Saved user prompt to session ${sessionId}`);
const modelInfo = await modelStateService.getCurrentModelInfo('llm');
if (!modelInfo || !modelInfo.apiKey) {
throw new Error('AI model or API key not configured.');
}
console.log(`[AskService] Using model: ${modelInfo.model} for provider: ${modelInfo.provider}`);
const screenshotResult = await captureScreenshot({ quality: 'medium' });
const screenshotBase64 = screenshotResult.success ? screenshotResult.base64 : null;
const conversationHistory = this._formatConversationForPrompt(conversationHistoryRaw);
const systemPrompt = getSystemPrompt('pickle_glass_analysis', conversationHistory, false);
const messages = [
{ role: 'system', content: systemPrompt },
{
role: 'user',
content: [
{ type: 'text', text: `User Request: ${userPrompt.trim()}` },
],
},
];
if (screenshotBase64) {
messages[1].content.push({
type: 'image_url',
image_url: { url: `data:image/jpeg;base64,${screenshotBase64}` },
});
}
const streamingLLM = createStreamingLLM(modelInfo.provider, {
apiKey: modelInfo.apiKey,
model: modelInfo.model,
temperature: 0.7,
maxTokens: 2048,
usePortkey: modelInfo.provider === 'openai-glass',
portkeyVirtualKey: modelInfo.provider === 'openai-glass' ? modelInfo.apiKey : undefined,
});
try {
const response = await streamingLLM.streamChat(messages);
const askWin = getWindowPool()?.get('ask');
if (!askWin || askWin.isDestroyed()) {
console.error("[AskService] Ask window is not available to send stream to.");
response.body.getReader().cancel();
return { success: false, error: 'Ask window is not available.' };
}
const reader = response.body.getReader();
signal.addEventListener('abort', () => {
console.log(`[AskService] Aborting stream reader. Reason: ${signal.reason}`);
reader.cancel(signal.reason).catch(() => { /* 이미 취소된 경우의 오류는 무시 */ });
});
await this._processStream(reader, askWin, sessionId, signal);
return { success: true };
} catch (multimodalError) {
// 멀티모달 요청이 실패했고 스크린샷이 포함되어 있다면 텍스트만으로 재시도
if (screenshotBase64 && this._isMultimodalError(multimodalError)) {
console.log(`[AskService] Multimodal request failed, retrying with text-only: ${multimodalError.message}`);
// 텍스트만으로 메시지 재구성
const textOnlyMessages = [
{ role: 'system', content: systemPrompt },
{
role: 'user',
content: `User Request: ${userPrompt.trim()}`
}
];
const fallbackResponse = await streamingLLM.streamChat(textOnlyMessages);
const askWin = getWindowPool()?.get('ask');
if (!askWin || askWin.isDestroyed()) {
console.error("[AskService] Ask window is not available for fallback response.");
fallbackResponse.body.getReader().cancel();
return { success: false, error: 'Ask window is not available.' };
}
const fallbackReader = fallbackResponse.body.getReader();
signal.addEventListener('abort', () => {
console.log(`[AskService] Aborting fallback stream reader. Reason: ${signal.reason}`);
fallbackReader.cancel(signal.reason).catch(() => {});
});
await this._processStream(fallbackReader, askWin, sessionId, signal);
return { success: true };
} else {
// 다른 종류의 에러이거나 스크린샷이 없었다면 그대로 throw
throw multimodalError;
}
}
} catch (error) {
console.error('[AskService] Error during message processing:', error);
this.state = {
...this.state,
isLoading: false,
isStreaming: false,
showTextInput: true,
};
this._broadcastState();
const askWin = getWindowPool()?.get('ask');
if (askWin && !askWin.isDestroyed()) {
const streamError = error.message || 'Unknown error occurred';
askWin.webContents.send('ask-response-stream-error', { error: streamError });
}
return { success: false, error: error.message };
}
}
/**
*
* @param {ReadableStreamDefaultReader} reader
* @param {BrowserWindow} askWin
* @param {number} sessionId
* @param {AbortSignal} signal
* @returns {Promise<void>}
* @private
*/
async _processStream(reader, askWin, sessionId, signal) {
const decoder = new TextDecoder();
let fullResponse = '';
try {
this.state.isLoading = false;
this.state.isStreaming = true;
this._broadcastState();
while (true) {
const { done, value } = await reader.read();
if (done) break;
const chunk = decoder.decode(value);
const lines = chunk.split('\n').filter(line => line.trim() !== '');
for (const line of lines) {
if (line.startsWith('data: ')) {
const data = line.substring(6);
if (data === '[DONE]') {
return;
}
try {
const json = JSON.parse(data);
const token = json.choices[0]?.delta?.content || '';
if (token) {
fullResponse += token;
this.state.currentResponse = fullResponse;
this._broadcastState();
}
} catch (error) {
}
}
}
}
} catch (streamError) {
if (signal.aborted) {
console.log(`[AskService] Stream reading was intentionally cancelled. Reason: ${signal.reason}`);
} else {
console.error('[AskService] Error while processing stream:', streamError);
if (askWin && !askWin.isDestroyed()) {
askWin.webContents.send('ask-response-stream-error', { error: streamError.message });
}
}
} finally {
this.state.isStreaming = false;
this.state.currentResponse = fullResponse;
this._broadcastState();
if (fullResponse) {
try {
await askRepository.addAiMessage({ sessionId, role: 'assistant', content: fullResponse });
console.log(`[AskService] DB: Saved partial or full assistant response to session ${sessionId} after stream ended.`);
} catch(dbError) {
console.error("[AskService] DB: Failed to save assistant response after stream ended:", dbError);
}
}
}
}
/**
* 멀티모달 관련 에러인지 판단
* @private
*/
_isMultimodalError(error) {
const errorMessage = error.message?.toLowerCase() || '';
return (
errorMessage.includes('vision') ||
errorMessage.includes('image') ||
errorMessage.includes('multimodal') ||
errorMessage.includes('unsupported') ||
errorMessage.includes('image_url') ||
errorMessage.includes('400') || // Bad Request often for unsupported features
errorMessage.includes('invalid') ||
errorMessage.includes('not supported')
);
}
}
const askService = new AskService();
module.exports = askService;

View File

@ -1,38 +0,0 @@
const { collection, addDoc, query, getDocs, orderBy, Timestamp } = require('firebase/firestore');
const { getFirestoreInstance } = require('../../common/services/firebaseClient');
const { createEncryptedConverter } = require('../../common/repositories/firestoreConverter');
const aiMessageConverter = createEncryptedConverter(['content']);
function aiMessagesCol(sessionId) {
if (!sessionId) throw new Error("Session ID is required to access AI messages.");
const db = getFirestoreInstance();
return collection(db, `sessions/${sessionId}/ai_messages`).withConverter(aiMessageConverter);
}
async function addAiMessage({ uid, sessionId, role, content, model = 'unknown' }) {
const now = Timestamp.now();
const newMessage = {
uid, // To identify the author of the message
session_id: sessionId,
sent_at: now,
role,
content,
model,
created_at: now,
};
const docRef = await addDoc(aiMessagesCol(sessionId), newMessage);
return { id: docRef.id };
}
async function getAllAiMessagesBySessionId(sessionId) {
const q = query(aiMessagesCol(sessionId), orderBy('sent_at', 'asc'));
const querySnapshot = await getDocs(q);
return querySnapshot.docs.map(doc => doc.data());
}
module.exports = {
addAiMessage,
getAllAiMessagesBySessionId,
};

View File

@ -1,25 +0,0 @@
const sqliteRepository = require('./sqlite.repository');
const firebaseRepository = require('./firebase.repository');
const authService = require('../../common/services/authService');
function getBaseRepository() {
const user = authService.getCurrentUser();
if (user && user.isLoggedIn) {
return firebaseRepository;
}
return sqliteRepository;
}
// The adapter layer that injects the UID
const askRepositoryAdapter = {
addAiMessage: ({ sessionId, role, content, model }) => {
const uid = authService.getCurrentUserId();
return getBaseRepository().addAiMessage({ uid, sessionId, role, content, model });
},
getAllAiMessagesBySessionId: (sessionId) => {
// This function does not require a UID at the service level.
return getBaseRepository().getAllAiMessagesBySessionId(sessionId);
}
};
module.exports = askRepositoryAdapter;

View File

@ -1,28 +0,0 @@
const sqliteClient = require('../../common/services/sqliteClient');
function addAiMessage({ uid, sessionId, role, content, model = 'unknown' }) {
// uid is ignored in the SQLite implementation
const db = sqliteClient.getDb();
const messageId = require('crypto').randomUUID();
const now = Math.floor(Date.now() / 1000);
const query = `INSERT INTO ai_messages (id, session_id, sent_at, role, content, model, created_at) VALUES (?, ?, ?, ?, ?, ?, ?)`;
try {
db.prepare(query).run(messageId, sessionId, now, role, content, model, now);
return { id: messageId };
} catch (err) {
console.error('SQLite: Failed to add AI message:', err);
throw err;
}
}
function getAllAiMessagesBySessionId(sessionId) {
const db = sqliteClient.getDb();
const query = "SELECT * FROM ai_messages WHERE session_id = ? ORDER BY sent_at ASC";
return db.prepare(query).all(sessionId);
}
module.exports = {
addAiMessage,
getAllAiMessagesBySessionId
};

View File

@ -1,185 +0,0 @@
// factory.js
/**
* @typedef {object} ModelOption
* @property {string} id
* @property {string} name
*/
/**
* @typedef {object} Provider
* @property {string} name
* @property {() => any} handler
* @property {ModelOption[]} llmModels
* @property {ModelOption[]} sttModels
*/
/**
* @type {Object.<string, Provider>}
*/
const PROVIDERS = {
'openai': {
name: 'OpenAI',
handler: () => require("./providers/openai"),
llmModels: [
{ id: 'gpt-4.1', name: 'GPT-4.1' },
],
sttModels: [
{ id: 'gpt-4o-mini-transcribe', name: 'GPT-4o Mini Transcribe' }
],
},
'openai-glass': {
name: 'OpenAI (Glass)',
handler: () => require("./providers/openai"),
llmModels: [
{ id: 'gpt-4.1-glass', name: 'GPT-4.1 (glass)' },
],
sttModels: [
{ id: 'gpt-4o-mini-transcribe-glass', name: 'GPT-4o Mini Transcribe (glass)' }
],
},
'gemini': {
name: 'Gemini',
handler: () => require("./providers/gemini"),
llmModels: [
{ id: 'gemini-2.5-flash', name: 'Gemini 2.5 Flash' },
],
sttModels: [
{ id: 'gemini-live-2.5-flash-preview', name: 'Gemini Live 2.5 Flash' }
],
},
'anthropic': {
name: 'Anthropic',
handler: () => require("./providers/anthropic"),
llmModels: [
{ id: 'claude-3-5-sonnet-20241022', name: 'Claude 3.5 Sonnet' },
],
sttModels: [],
},
'deepgram': {
name: 'Deepgram',
handler: () => require("./providers/deepgram"),
llmModels: [],
sttModels: [
{ id: 'nova-3', name: 'Nova-3 (General)' },
],
},
'ollama': {
name: 'Ollama (Local)',
handler: () => require("./providers/ollama"),
llmModels: [], // Dynamic models populated from installed Ollama models
sttModels: [], // Ollama doesn't support STT yet
},
'whisper': {
name: 'Whisper (Local)',
handler: () => {
// This needs to remain a function due to its conditional logic for renderer/main process
if (typeof window === 'undefined') {
const { WhisperProvider } = require("./providers/whisper");
return new WhisperProvider();
}
// Return a dummy object for the renderer process
return {
validateApiKey: async () => ({ success: true }), // Mock validate for renderer
createSTT: () => { throw new Error('Whisper STT is only available in main process'); },
};
},
llmModels: [],
sttModels: [
{ id: 'whisper-tiny', name: 'Whisper Tiny (39M)' },
{ id: 'whisper-base', name: 'Whisper Base (74M)' },
{ id: 'whisper-small', name: 'Whisper Small (244M)' },
{ id: 'whisper-medium', name: 'Whisper Medium (769M)' },
],
},
};
function sanitizeModelId(model) {
return (typeof model === 'string') ? model.replace(/-glass$/, '') : model;
}
function createSTT(provider, opts) {
if (provider === 'openai-glass') provider = 'openai';
const handler = PROVIDERS[provider]?.handler();
if (!handler?.createSTT) {
throw new Error(`STT not supported for provider: ${provider}`);
}
if (opts && opts.model) {
opts = { ...opts, model: sanitizeModelId(opts.model) };
}
return handler.createSTT(opts);
}
function createLLM(provider, opts) {
if (provider === 'openai-glass') provider = 'openai';
const handler = PROVIDERS[provider]?.handler();
if (!handler?.createLLM) {
throw new Error(`LLM not supported for provider: ${provider}`);
}
if (opts && opts.model) {
opts = { ...opts, model: sanitizeModelId(opts.model) };
}
return handler.createLLM(opts);
}
function createStreamingLLM(provider, opts) {
if (provider === 'openai-glass') provider = 'openai';
const handler = PROVIDERS[provider]?.handler();
if (!handler?.createStreamingLLM) {
throw new Error(`Streaming LLM not supported for provider: ${provider}`);
}
if (opts && opts.model) {
opts = { ...opts, model: sanitizeModelId(opts.model) };
}
return handler.createStreamingLLM(opts);
}
function getProviderClass(providerId) {
const providerConfig = PROVIDERS[providerId];
if (!providerConfig) return null;
// Handle special cases for glass providers
let actualProviderId = providerId;
if (providerId === 'openai-glass') {
actualProviderId = 'openai';
}
// The handler function returns the module, from which we get the class.
const module = providerConfig.handler();
// Map provider IDs to their actual exported class names
const classNameMap = {
'openai': 'OpenAIProvider',
'anthropic': 'AnthropicProvider',
'gemini': 'GeminiProvider',
'deepgram': 'DeepgramProvider',
'ollama': 'OllamaProvider',
'whisper': 'WhisperProvider'
};
const className = classNameMap[actualProviderId];
return className ? module[className] : null;
}
function getAvailableProviders() {
const stt = [];
const llm = [];
for (const [id, provider] of Object.entries(PROVIDERS)) {
if (provider.sttModels.length > 0) stt.push(id);
if (provider.llmModels.length > 0) llm.push(id);
}
return { stt: [...new Set(stt)], llm: [...new Set(llm)] };
}
module.exports = {
PROVIDERS,
createSTT,
createLLM,
createStreamingLLM,
getProviderClass,
getAvailableProviders,
};

View File

@ -1,327 +0,0 @@
const { Anthropic } = require("@anthropic-ai/sdk")
class AnthropicProvider {
static async validateApiKey(key) {
if (!key || typeof key !== 'string' || !key.startsWith('sk-ant-')) {
return { success: false, error: 'Invalid Anthropic API key format.' };
}
try {
const response = await fetch("https://api.anthropic.com/v1/messages", {
method: "POST",
headers: {
"Content-Type": "application/json",
"x-api-key": key,
"anthropic-version": "2023-06-01",
},
body: JSON.stringify({
model: "claude-3-haiku-20240307",
max_tokens: 1,
messages: [{ role: "user", content: "Hi" }],
}),
});
if (response.ok || response.status === 400) { // 400 is a valid response for a bad request, not a bad key
return { success: true };
} else {
const errorData = await response.json().catch(() => ({}));
return { success: false, error: errorData.error?.message || `Validation failed with status: ${response.status}` };
}
} catch (error) {
console.error(`[AnthropicProvider] Network error during key validation:`, error);
return { success: false, error: 'A network error occurred during validation.' };
}
}
}
/**
* Creates an Anthropic STT session
* Note: Anthropic doesn't have native real-time STT, so this is a placeholder
* You might want to use a different STT service or implement a workaround
* @param {object} opts - Configuration options
* @param {string} opts.apiKey - Anthropic API key
* @param {string} [opts.language='en'] - Language code
* @param {object} [opts.callbacks] - Event callbacks
* @returns {Promise<object>} STT session placeholder
*/
async function createSTT({ apiKey, language = "en", callbacks = {}, ...config }) {
console.warn("[Anthropic] STT not natively supported. Consider using OpenAI or Gemini for STT.")
// Return a mock STT session that doesn't actually do anything
// You might want to fallback to another provider for STT
return {
sendRealtimeInput: async (audioData) => {
console.warn("[Anthropic] STT sendRealtimeInput called but not implemented")
},
close: async () => {
console.log("[Anthropic] STT session closed")
},
}
}
/**
* Creates an Anthropic LLM instance
* @param {object} opts - Configuration options
* @param {string} opts.apiKey - Anthropic API key
* @param {string} [opts.model='claude-3-5-sonnet-20241022'] - Model name
* @param {number} [opts.temperature=0.7] - Temperature
* @param {number} [opts.maxTokens=4096] - Max tokens
* @returns {object} LLM instance
*/
function createLLM({ apiKey, model = "claude-3-5-sonnet-20241022", temperature = 0.7, maxTokens = 4096, ...config }) {
const client = new Anthropic({ apiKey })
return {
generateContent: async (parts) => {
const messages = []
let systemPrompt = ""
const userContent = []
for (const part of parts) {
if (typeof part === "string") {
if (systemPrompt === "" && part.includes("You are")) {
systemPrompt = part
} else {
userContent.push({ type: "text", text: part })
}
} else if (part.inlineData) {
userContent.push({
type: "image",
source: {
type: "base64",
media_type: part.inlineData.mimeType,
data: part.inlineData.data,
},
})
}
}
if (userContent.length > 0) {
messages.push({ role: "user", content: userContent })
}
try {
const response = await client.messages.create({
model: model,
max_tokens: maxTokens,
temperature: temperature,
system: systemPrompt || undefined,
messages: messages,
})
return {
response: {
text: () => response.content[0].text,
},
raw: response,
}
} catch (error) {
console.error("Anthropic API error:", error)
throw error
}
},
// For compatibility with chat-style interfaces
chat: async (messages) => {
let systemPrompt = ""
const anthropicMessages = []
for (const msg of messages) {
if (msg.role === "system") {
systemPrompt = msg.content
} else {
// Handle multimodal content
let content
if (Array.isArray(msg.content)) {
content = []
for (const part of msg.content) {
if (typeof part === "string") {
content.push({ type: "text", text: part })
} else if (part.type === "text") {
content.push({ type: "text", text: part.text })
} else if (part.type === "image_url" && part.image_url) {
// Convert base64 image to Anthropic format
const imageUrl = part.image_url.url
const [mimeInfo, base64Data] = imageUrl.split(",")
// Extract the actual MIME type from the data URL
const mimeType = mimeInfo.match(/data:([^;]+)/)?.[1] || "image/jpeg"
content.push({
type: "image",
source: {
type: "base64",
media_type: mimeType,
data: base64Data,
},
})
}
}
} else {
content = [{ type: "text", text: msg.content }]
}
anthropicMessages.push({
role: msg.role === "user" ? "user" : "assistant",
content: content,
})
}
}
const response = await client.messages.create({
model: model,
max_tokens: maxTokens,
temperature: temperature,
system: systemPrompt || undefined,
messages: anthropicMessages,
})
return {
content: response.content[0].text,
raw: response,
}
},
}
}
/**
* Creates an Anthropic streaming LLM instance
* @param {object} opts - Configuration options
* @param {string} opts.apiKey - Anthropic API key
* @param {string} [opts.model='claude-3-5-sonnet-20241022'] - Model name
* @param {number} [opts.temperature=0.7] - Temperature
* @param {number} [opts.maxTokens=4096] - Max tokens
* @returns {object} Streaming LLM instance
*/
function createStreamingLLM({
apiKey,
model = "claude-3-5-sonnet-20241022",
temperature = 0.7,
maxTokens = 4096,
...config
}) {
const client = new Anthropic({ apiKey })
return {
streamChat: async (messages) => {
console.log("[Anthropic Provider] Starting streaming request")
let systemPrompt = ""
const anthropicMessages = []
for (const msg of messages) {
if (msg.role === "system") {
systemPrompt = msg.content
} else {
// Handle multimodal content
let content
if (Array.isArray(msg.content)) {
content = []
for (const part of msg.content) {
if (typeof part === "string") {
content.push({ type: "text", text: part })
} else if (part.type === "text") {
content.push({ type: "text", text: part.text })
} else if (part.type === "image_url" && part.image_url) {
// Convert base64 image to Anthropic format
const imageUrl = part.image_url.url
const [mimeInfo, base64Data] = imageUrl.split(",")
// Extract the actual MIME type from the data URL
const mimeType = mimeInfo.match(/data:([^;]+)/)?.[1] || "image/jpeg"
console.log(`[Anthropic] Processing image with MIME type: ${mimeType}`)
content.push({
type: "image",
source: {
type: "base64",
media_type: mimeType,
data: base64Data,
},
})
}
}
} else {
content = [{ type: "text", text: msg.content }]
}
anthropicMessages.push({
role: msg.role === "user" ? "user" : "assistant",
content: content,
})
}
}
// Create a ReadableStream to handle Anthropic's streaming
const stream = new ReadableStream({
async start(controller) {
try {
console.log("[Anthropic Provider] Processing messages:", anthropicMessages.length, "messages")
let chunkCount = 0
let totalContent = ""
// Stream the response
const stream = await client.messages.create({
model: model,
max_tokens: maxTokens,
temperature: temperature,
system: systemPrompt || undefined,
messages: anthropicMessages,
stream: true,
})
for await (const chunk of stream) {
if (chunk.type === "content_block_delta" && chunk.delta.type === "text_delta") {
chunkCount++
const chunkText = chunk.delta.text || ""
totalContent += chunkText
// Format as SSE data
const data = JSON.stringify({
choices: [
{
delta: {
content: chunkText,
},
},
],
})
controller.enqueue(new TextEncoder().encode(`data: ${data}\n\n`))
}
}
console.log(
`[Anthropic Provider] Streamed ${chunkCount} chunks, total length: ${totalContent.length} chars`,
)
// Send the final done message
controller.enqueue(new TextEncoder().encode("data: [DONE]\n\n"))
controller.close()
console.log("[Anthropic Provider] Streaming completed successfully")
} catch (error) {
console.error("[Anthropic Provider] Streaming error:", error)
controller.error(error)
}
},
})
// Create a Response object with the stream
return new Response(stream, {
headers: {
"Content-Type": "text/event-stream",
"Cache-Control": "no-cache",
Connection: "keep-alive",
},
})
},
}
}
module.exports = {
AnthropicProvider,
createSTT,
createLLM,
createStreamingLLM
};

View File

@ -1,111 +0,0 @@
// providers/deepgram.js
const { createClient, LiveTranscriptionEvents } = require('@deepgram/sdk');
const WebSocket = require('ws');
/**
* Deepgram Provider 클래스. API 유효성 검사를 담당합니다.
*/
class DeepgramProvider {
/**
* Deepgram API 키의 유효성을 검사합니다.
* @param {string} key - 검사할 Deepgram API
* @returns {Promise<{success: boolean, error?: string}>}
*/
static async validateApiKey(key) {
if (!key || typeof key !== 'string') {
return { success: false, error: 'Invalid Deepgram API key format.' };
}
try {
// ✨ 변경점: SDK 대신 직접 fetch로 API를 호출하여 안정성 확보 (openai.js 방식)
const response = await fetch('https://api.deepgram.com/v1/projects', {
headers: { 'Authorization': `Token ${key}` }
});
if (response.ok) {
return { success: true };
} else {
const errorData = await response.json().catch(() => ({}));
const message = errorData.err_msg || `Validation failed with status: ${response.status}`;
return { success: false, error: message };
}
} catch (error) {
console.error(`[DeepgramProvider] Network error during key validation:`, error);
return { success: false, error: error.message || 'A network error occurred during validation.' };
}
}
}
function createSTT({
apiKey,
language = 'en-US',
sampleRate = 24000,
callbacks = {},
}) {
const qs = new URLSearchParams({
model: 'nova-3',
encoding: 'linear16',
sample_rate: sampleRate.toString(),
language,
smart_format: 'true',
interim_results: 'true',
channels: '1',
});
const url = `wss://api.deepgram.com/v1/listen?${qs}`;
const ws = new WebSocket(url, {
headers: { Authorization: `Token ${apiKey}` },
});
ws.binaryType = 'arraybuffer';
return new Promise((resolve, reject) => {
const to = setTimeout(() => {
ws.terminate();
reject(new Error('DG open timeout (10s)'));
}, 10_000);
ws.on('open', () => {
clearTimeout(to);
resolve({
sendRealtimeInput: (buf) => ws.send(buf),
close: () => ws.close(1000, 'client'),
});
});
ws.on('message', raw => {
let msg;
try { msg = JSON.parse(raw.toString()); } catch { return; }
if (msg.channel?.alternatives?.[0]?.transcript !== undefined) {
callbacks.onmessage?.({ provider: 'deepgram', ...msg });
}
});
ws.on('close', (code, reason) =>
callbacks.onclose?.({ code, reason: reason.toString() })
);
ws.on('error', err => {
clearTimeout(to);
callbacks.onerror?.(err);
reject(err);
});
});
}
// ... (LLM 관련 Placeholder 함수들은 그대로 유지) ...
function createLLM(opts) {
console.warn("[Deepgram] LLM not supported.");
return { generateContent: async () => { throw new Error("Deepgram does not support LLM functionality."); } };
}
function createStreamingLLM(opts) {
console.warn("[Deepgram] Streaming LLM not supported.");
return { streamChat: async () => { throw new Error("Deepgram does not support Streaming LLM functionality."); } };
}
module.exports = {
DeepgramProvider,
createSTT,
createLLM,
createStreamingLLM
};

View File

@ -1,328 +0,0 @@
const { GoogleGenerativeAI } = require("@google/generative-ai")
const { GoogleGenAI } = require("@google/genai")
class GeminiProvider {
static async validateApiKey(key) {
if (!key || typeof key !== 'string') {
return { success: false, error: 'Invalid Gemini API key format.' };
}
try {
const validationUrl = `https://generativelanguage.googleapis.com/v1beta/models?key=${key}`;
const response = await fetch(validationUrl);
if (response.ok) {
return { success: true };
} else {
const errorData = await response.json().catch(() => ({}));
const message = errorData.error?.message || `Validation failed with status: ${response.status}`;
return { success: false, error: message };
}
} catch (error) {
console.error(`[GeminiProvider] Network error during key validation:`, error);
return { success: false, error: 'A network error occurred during validation.' };
}
}
}
/**
* Creates a Gemini STT session
* @param {object} opts - Configuration options
* @param {string} opts.apiKey - Gemini API key
* @param {string} [opts.language='en-US'] - Language code
* @param {object} [opts.callbacks] - Event callbacks
* @returns {Promise<object>} STT session
*/
async function createSTT({ apiKey, language = "en-US", callbacks = {}, ...config }) {
const liveClient = new GoogleGenAI({ vertexai: false, apiKey })
// Language code BCP-47 conversion
const lang = language.includes("-") ? language : `${language}-US`
const session = await liveClient.live.connect({
model: 'gemini-live-2.5-flash-preview',
callbacks: {
...callbacks,
onMessage: (msg) => {
if (!msg || typeof msg !== 'object') return;
msg.provider = 'gemini';
callbacks.onmessage?.(msg);
}
},
config: {
inputAudioTranscription: {},
speechConfig: { languageCode: lang },
},
})
return {
sendRealtimeInput: async (payload) => session.sendRealtimeInput(payload),
close: async () => session.close(),
}
}
/**
* Creates a Gemini LLM instance with proper text response handling
*/
function createLLM({ apiKey, model = "gemini-2.5-flash", temperature = 0.7, maxTokens = 8192, ...config }) {
const client = new GoogleGenerativeAI(apiKey)
return {
generateContent: async (parts) => {
const geminiModel = client.getGenerativeModel({
model: model,
generationConfig: {
temperature,
maxOutputTokens: maxTokens,
// Ensure we get text responses, not JSON
responseMimeType: "text/plain",
},
})
const systemPrompt = ""
const userContent = []
for (const part of parts) {
if (typeof part === "string") {
// Don't automatically assume strings starting with "You are" are system prompts
// Check if it's explicitly marked as a system instruction
userContent.push(part)
} else if (part.inlineData) {
userContent.push({
inlineData: {
mimeType: part.inlineData.mimeType,
data: part.inlineData.data,
},
})
}
}
try {
const result = await geminiModel.generateContent(userContent)
const response = await result.response
// Return plain text, not wrapped in JSON structure
return {
response: {
text: () => response.text(),
},
}
} catch (error) {
console.error("Gemini API error:", error)
throw error
}
},
chat: async (messages) => {
// Filter out any system prompts that might be causing JSON responses
let systemInstruction = ""
const history = []
let lastMessage
messages.forEach((msg, index) => {
if (msg.role === "system") {
// Clean system instruction - avoid JSON formatting requests
systemInstruction = msg.content
.replace(/respond in json/gi, "")
.replace(/format.*json/gi, "")
.replace(/return.*json/gi, "")
// Add explicit instruction for natural text
if (!systemInstruction.includes("respond naturally")) {
systemInstruction += "\n\nRespond naturally in plain text, not in JSON or structured format."
}
return
}
const role = msg.role === "user" ? "user" : "model"
if (index === messages.length - 1) {
lastMessage = msg
} else {
history.push({ role, parts: [{ text: msg.content }] })
}
})
const geminiModel = client.getGenerativeModel({
model: model,
systemInstruction:
systemInstruction ||
"Respond naturally in plain text format. Do not use JSON or structured responses unless specifically requested.",
generationConfig: {
temperature: temperature,
maxOutputTokens: maxTokens,
// Force plain text responses
responseMimeType: "text/plain",
},
})
const chat = geminiModel.startChat({
history: history,
})
let content = lastMessage.content
// Handle multimodal content
if (Array.isArray(content)) {
const geminiContent = []
for (const part of content) {
if (typeof part === "string") {
geminiContent.push(part)
} else if (part.type === "text") {
geminiContent.push(part.text)
} else if (part.type === "image_url" && part.image_url) {
const base64Data = part.image_url.url.split(",")[1]
geminiContent.push({
inlineData: {
mimeType: "image/png",
data: base64Data,
},
})
}
}
content = geminiContent
}
const result = await chat.sendMessage(content)
const response = await result.response
// Return plain text content
return {
content: response.text(),
raw: result,
}
},
}
}
/**
* Creates a Gemini streaming LLM instance with text response fix
*/
function createStreamingLLM({ apiKey, model = "gemini-2.5-flash", temperature = 0.7, maxTokens = 8192, ...config }) {
const client = new GoogleGenerativeAI(apiKey)
return {
streamChat: async (messages) => {
console.log("[Gemini Provider] Starting streaming request")
let systemInstruction = ""
const nonSystemMessages = []
for (const msg of messages) {
if (msg.role === "system") {
// Clean and modify system instruction
systemInstruction = msg.content
.replace(/respond in json/gi, "")
.replace(/format.*json/gi, "")
.replace(/return.*json/gi, "")
if (!systemInstruction.includes("respond naturally")) {
systemInstruction += "\n\nRespond naturally in plain text, not in JSON or structured format."
}
} else {
nonSystemMessages.push(msg)
}
}
const geminiModel = client.getGenerativeModel({
model: model,
systemInstruction:
systemInstruction ||
"Respond naturally in plain text format. Do not use JSON or structured responses unless specifically requested.",
generationConfig: {
temperature,
maxOutputTokens: maxTokens || 8192,
// Force plain text responses
responseMimeType: "text/plain",
},
})
const stream = new ReadableStream({
async start(controller) {
try {
const lastMessage = nonSystemMessages[nonSystemMessages.length - 1]
let geminiContent = []
if (Array.isArray(lastMessage.content)) {
for (const part of lastMessage.content) {
if (typeof part === "string") {
geminiContent.push(part)
} else if (part.type === "text") {
geminiContent.push(part.text)
} else if (part.type === "image_url" && part.image_url) {
const base64Data = part.image_url.url.split(",")[1]
geminiContent.push({
inlineData: {
mimeType: "image/png",
data: base64Data,
},
})
}
}
} else {
geminiContent = [lastMessage.content]
}
const contentParts = geminiContent.map((part) => {
if (typeof part === "string") {
return { text: part }
} else if (part.inlineData) {
return { inlineData: part.inlineData }
}
return part
})
const result = await geminiModel.generateContentStream({
contents: [
{
role: "user",
parts: contentParts,
},
],
})
for await (const chunk of result.stream) {
const chunkText = chunk.text() || ""
// Format as SSE data - this should now be plain text
const data = JSON.stringify({
choices: [
{
delta: {
content: chunkText,
},
},
],
})
controller.enqueue(new TextEncoder().encode(`data: ${data}\n\n`))
}
controller.enqueue(new TextEncoder().encode("data: [DONE]\n\n"))
controller.close()
} catch (error) {
console.error("[Gemini Provider] Streaming error:", error)
controller.error(error)
}
},
})
return new Response(stream, {
headers: {
"Content-Type": "text/event-stream",
"Cache-Control": "no-cache",
Connection: "keep-alive",
},
})
},
}
}
module.exports = {
GeminiProvider,
createSTT,
createLLM,
createStreamingLLM
};

View File

@ -1,342 +0,0 @@
const http = require('http');
const fetch = require('node-fetch');
// Request Queue System for Ollama API (only for non-streaming requests)
class RequestQueue {
constructor() {
this.queue = [];
this.processing = false;
this.streamingActive = false;
}
async addStreamingRequest(requestFn) {
// Streaming requests have priority - wait for current processing to finish
while (this.processing) {
await new Promise(resolve => setTimeout(resolve, 50));
}
this.streamingActive = true;
console.log('[Ollama Queue] Starting streaming request (priority)');
try {
const result = await requestFn();
return result;
} finally {
this.streamingActive = false;
console.log('[Ollama Queue] Streaming request completed');
}
}
async add(requestFn) {
return new Promise((resolve, reject) => {
this.queue.push({ requestFn, resolve, reject });
this.process();
});
}
async process() {
if (this.processing || this.queue.length === 0) {
return;
}
// Wait if streaming is active
if (this.streamingActive) {
setTimeout(() => this.process(), 100);
return;
}
this.processing = true;
while (this.queue.length > 0) {
// Check if streaming started while processing queue
if (this.streamingActive) {
this.processing = false;
setTimeout(() => this.process(), 100);
return;
}
const { requestFn, resolve, reject } = this.queue.shift();
try {
console.log(`[Ollama Queue] Processing queued request (${this.queue.length} remaining)`);
const result = await requestFn();
resolve(result);
} catch (error) {
console.error('[Ollama Queue] Request failed:', error);
reject(error);
}
}
this.processing = false;
}
}
// Global request queue instance
const requestQueue = new RequestQueue();
class OllamaProvider {
static async validateApiKey() {
try {
const response = await fetch('http://localhost:11434/api/tags');
if (response.ok) {
return { success: true };
} else {
return { success: false, error: 'Ollama service is not running. Please start Ollama first.' };
}
} catch (error) {
return { success: false, error: 'Cannot connect to Ollama. Please ensure Ollama is installed and running.' };
}
}
}
function convertMessagesToOllamaFormat(messages) {
return messages.map(msg => {
if (Array.isArray(msg.content)) {
let textContent = '';
const images = [];
for (const part of msg.content) {
if (part.type === 'text') {
textContent += part.text;
} else if (part.type === 'image_url') {
const base64 = part.image_url.url.replace(/^data:image\/[^;]+;base64,/, '');
images.push(base64);
}
}
return {
role: msg.role,
content: textContent,
...(images.length > 0 && { images })
};
} else {
return msg;
}
});
}
function createLLM({
model,
temperature = 0.7,
maxTokens = 2048,
baseUrl = 'http://localhost:11434',
...config
}) {
if (!model) {
throw new Error('Model parameter is required for Ollama LLM. Please specify a model name (e.g., "llama3.2:latest", "gemma3:4b")');
}
return {
generateContent: async (parts) => {
let systemPrompt = '';
const userContent = [];
for (const part of parts) {
if (typeof part === 'string') {
if (systemPrompt === '' && part.includes('You are')) {
systemPrompt = part;
} else {
userContent.push(part);
}
} else if (part.inlineData) {
userContent.push({
type: 'image',
image: `data:${part.inlineData.mimeType};base64,${part.inlineData.data}`
});
}
}
const messages = [];
if (systemPrompt) {
messages.push({ role: 'system', content: systemPrompt });
}
messages.push({ role: 'user', content: userContent.join('\n') });
// Use request queue to prevent concurrent API calls
return await requestQueue.add(async () => {
try {
const response = await fetch(`${baseUrl}/api/chat`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
model,
messages,
stream: false,
options: {
temperature,
num_predict: maxTokens,
}
})
});
if (!response.ok) {
throw new Error(`Ollama API error: ${response.status} ${response.statusText}`);
}
const result = await response.json();
return {
response: {
text: () => result.message.content
},
raw: result
};
} catch (error) {
console.error('Ollama LLM error:', error);
throw error;
}
});
},
chat: async (messages) => {
const ollamaMessages = convertMessagesToOllamaFormat(messages);
// Use request queue to prevent concurrent API calls
return await requestQueue.add(async () => {
try {
const response = await fetch(`${baseUrl}/api/chat`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
model,
messages: ollamaMessages,
stream: false,
options: {
temperature,
num_predict: maxTokens,
}
})
});
if (!response.ok) {
throw new Error(`Ollama API error: ${response.status} ${response.statusText}`);
}
const result = await response.json();
return {
content: result.message.content,
raw: result
};
} catch (error) {
console.error('Ollama chat error:', error);
throw error;
}
});
}
};
}
function createStreamingLLM({
model,
temperature = 0.7,
maxTokens = 2048,
baseUrl = 'http://localhost:11434',
...config
}) {
if (!model) {
throw new Error('Model parameter is required for Ollama streaming LLM. Please specify a model name (e.g., "llama3.2:latest", "gemma3:4b")');
}
return {
streamChat: async (messages) => {
console.log('[Ollama Provider] Starting streaming request');
const ollamaMessages = convertMessagesToOllamaFormat(messages);
console.log('[Ollama Provider] Converted messages for Ollama:', ollamaMessages);
// Streaming requests have priority over queued requests
return await requestQueue.addStreamingRequest(async () => {
try {
const response = await fetch(`${baseUrl}/api/chat`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
model,
messages: ollamaMessages,
stream: true,
options: {
temperature,
num_predict: maxTokens,
}
})
});
if (!response.ok) {
throw new Error(`Ollama API error: ${response.status} ${response.statusText}`);
}
console.log('[Ollama Provider] Got streaming response');
const stream = new ReadableStream({
async start(controller) {
let buffer = '';
try {
response.body.on('data', (chunk) => {
buffer += chunk.toString();
const lines = buffer.split('\n');
buffer = lines.pop() || '';
for (const line of lines) {
if (line.trim() === '') continue;
try {
const data = JSON.parse(line);
if (data.message?.content) {
const sseData = JSON.stringify({
choices: [{
delta: {
content: data.message.content
}
}]
});
controller.enqueue(new TextEncoder().encode(`data: ${sseData}\n\n`));
}
if (data.done) {
controller.enqueue(new TextEncoder().encode('data: [DONE]\n\n'));
}
} catch (e) {
console.error('[Ollama Provider] Failed to parse chunk:', e);
}
}
});
response.body.on('end', () => {
controller.close();
console.log('[Ollama Provider] Streaming completed');
});
response.body.on('error', (error) => {
console.error('[Ollama Provider] Streaming error:', error);
controller.error(error);
});
} catch (error) {
console.error('[Ollama Provider] Streaming setup error:', error);
controller.error(error);
}
}
});
return {
ok: true,
body: stream
};
} catch (error) {
console.error('[Ollama Provider] Request error:', error);
throw error;
}
});
}
};
}
module.exports = {
OllamaProvider,
createLLM,
createStreamingLLM,
convertMessagesToOllamaFormat
};

View File

@ -1,294 +0,0 @@
const OpenAI = require('openai');
const WebSocket = require('ws');
const { Portkey } = require('portkey-ai');
const { Readable } = require('stream');
const { getProviderForModel } = require('../factory.js');
class OpenAIProvider {
static async validateApiKey(key) {
if (!key || typeof key !== 'string' || !key.startsWith('sk-')) {
return { success: false, error: 'Invalid OpenAI API key format.' };
}
try {
const response = await fetch('https://api.openai.com/v1/models', {
headers: { 'Authorization': `Bearer ${key}` }
});
if (response.ok) {
return { success: true };
} else {
const errorData = await response.json().catch(() => ({}));
const message = errorData.error?.message || `Validation failed with status: ${response.status}`;
return { success: false, error: message };
}
} catch (error) {
console.error(`[OpenAIProvider] Network error during key validation:`, error);
return { success: false, error: 'A network error occurred during validation.' };
}
}
}
/**
* Creates an OpenAI STT session
* @param {object} opts - Configuration options
* @param {string} opts.apiKey - OpenAI API key
* @param {string} [opts.language='en'] - Language code
* @param {object} [opts.callbacks] - Event callbacks
* @param {boolean} [opts.usePortkey=false] - Whether to use Portkey
* @param {string} [opts.portkeyVirtualKey] - Portkey virtual key
* @returns {Promise<object>} STT session
*/
async function createSTT({ apiKey, language = 'en', callbacks = {}, usePortkey = false, portkeyVirtualKey, ...config }) {
const keyType = usePortkey ? 'vKey' : 'apiKey';
const key = usePortkey ? (portkeyVirtualKey || apiKey) : apiKey;
const wsUrl = keyType === 'apiKey'
? 'wss://api.openai.com/v1/realtime?intent=transcription'
: 'wss://api.portkey.ai/v1/realtime?intent=transcription';
const headers = keyType === 'apiKey'
? {
'Authorization': `Bearer ${key}`,
'OpenAI-Beta': 'realtime=v1',
}
: {
'x-portkey-api-key': 'gRv2UGRMq6GGLJ8aVEB4e7adIewu',
'x-portkey-virtual-key': key,
'OpenAI-Beta': 'realtime=v1',
};
const ws = new WebSocket(wsUrl, { headers });
return new Promise((resolve, reject) => {
ws.onopen = () => {
console.log("WebSocket session opened.");
const sessionConfig = {
type: 'transcription_session.update',
session: {
input_audio_format: 'pcm16',
input_audio_transcription: {
model: 'gpt-4o-mini-transcribe',
prompt: config.prompt || '',
language: language || 'en'
},
turn_detection: {
type: 'server_vad',
threshold: 0.5,
prefix_padding_ms: 200,
silence_duration_ms: 100,
},
input_audio_noise_reduction: {
type: 'near_field'
}
}
};
ws.send(JSON.stringify(sessionConfig));
resolve({
sendRealtimeInput: (audioData) => {
if (ws.readyState === WebSocket.OPEN) {
const message = {
type: 'input_audio_buffer.append',
audio: audioData
};
ws.send(JSON.stringify(message));
}
},
close: () => {
if (ws.readyState === WebSocket.OPEN) {
ws.send(JSON.stringify({ type: 'session.close' }));
ws.onmessage = ws.onerror = () => {}; // 핸들러 제거
ws.close(1000, 'Client initiated close.');
}
}
});
};
ws.onmessage = (event) => {
// ── 종료·하트비트 패킷 필터링 ──────────────────────────────
if (!event.data || event.data === 'null' || event.data === '[DONE]') return;
let msg;
try { msg = JSON.parse(event.data); }
catch { return; } // JSON 파싱 실패 무시
if (!msg || typeof msg !== 'object') return;
msg.provider = 'openai'; // ← 항상 명시
callbacks.onmessage?.(msg);
};
ws.onerror = (error) => {
console.error('WebSocket error:', error.message);
if (callbacks && callbacks.onerror) {
callbacks.onerror(error);
}
reject(error);
};
ws.onclose = (event) => {
console.log(`WebSocket closed: ${event.code} ${event.reason}`);
if (callbacks && callbacks.onclose) {
callbacks.onclose(event);
}
};
});
}
/**
* Creates an OpenAI LLM instance
* @param {object} opts - Configuration options
* @param {string} opts.apiKey - OpenAI API key
* @param {string} [opts.model='gpt-4.1'] - Model name
* @param {number} [opts.temperature=0.7] - Temperature
* @param {number} [opts.maxTokens=2048] - Max tokens
* @param {boolean} [opts.usePortkey=false] - Whether to use Portkey
* @param {string} [opts.portkeyVirtualKey] - Portkey virtual key
* @returns {object} LLM instance
*/
function createLLM({ apiKey, model = 'gpt-4.1', temperature = 0.7, maxTokens = 2048, usePortkey = false, portkeyVirtualKey, ...config }) {
const client = new OpenAI({ apiKey });
const callApi = async (messages) => {
if (!usePortkey) {
const response = await client.chat.completions.create({
model: model,
messages: messages,
temperature: temperature,
max_tokens: maxTokens
});
return {
content: response.choices[0].message.content.trim(),
raw: response
};
} else {
const fetchUrl = 'https://api.portkey.ai/v1/chat/completions';
const response = await fetch(fetchUrl, {
method: 'POST',
headers: {
'x-portkey-api-key': 'gRv2UGRMq6GGLJ8aVEB4e7adIewu',
'x-portkey-virtual-key': portkeyVirtualKey || apiKey,
'Content-Type': 'application/json',
},
body: JSON.stringify({
model: model,
messages,
temperature,
max_tokens: maxTokens,
}),
});
if (!response.ok) {
throw new Error(`Portkey API error: ${response.status} ${response.statusText}`);
}
const result = await response.json();
return {
content: result.choices[0].message.content.trim(),
raw: result
};
}
};
return {
generateContent: async (parts) => {
const messages = [];
let systemPrompt = '';
let userContent = [];
for (const part of parts) {
if (typeof part === 'string') {
if (systemPrompt === '' && part.includes('You are')) {
systemPrompt = part;
} else {
userContent.push({ type: 'text', text: part });
}
} else if (part.inlineData) {
userContent.push({
type: 'image_url',
image_url: { url: `data:${part.inlineData.mimeType};base64,${part.inlineData.data}` }
});
}
}
if (systemPrompt) messages.push({ role: 'system', content: systemPrompt });
if (userContent.length > 0) messages.push({ role: 'user', content: userContent });
const result = await callApi(messages);
return {
response: {
text: () => result.content
},
raw: result.raw
};
},
// For compatibility with chat-style interfaces
chat: async (messages) => {
return await callApi(messages);
}
};
}
/**
* Creates an OpenAI streaming LLM instance
* @param {object} opts - Configuration options
* @param {string} opts.apiKey - OpenAI API key
* @param {string} [opts.model='gpt-4.1'] - Model name
* @param {number} [opts.temperature=0.7] - Temperature
* @param {number} [opts.maxTokens=2048] - Max tokens
* @param {boolean} [opts.usePortkey=false] - Whether to use Portkey
* @param {string} [opts.portkeyVirtualKey] - Portkey virtual key
* @returns {object} Streaming LLM instance
*/
function createStreamingLLM({ apiKey, model = 'gpt-4.1', temperature = 0.7, maxTokens = 2048, usePortkey = false, portkeyVirtualKey, ...config }) {
return {
streamChat: async (messages) => {
const fetchUrl = usePortkey
? 'https://api.portkey.ai/v1/chat/completions'
: 'https://api.openai.com/v1/chat/completions';
const headers = usePortkey
? {
'x-portkey-api-key': 'gRv2UGRMq6GGLJ8aVEB4e7adIewu',
'x-portkey-virtual-key': portkeyVirtualKey || apiKey,
'Content-Type': 'application/json',
}
: {
Authorization: `Bearer ${apiKey}`,
'Content-Type': 'application/json',
};
const response = await fetch(fetchUrl, {
method: 'POST',
headers,
body: JSON.stringify({
model: model,
messages,
temperature,
max_tokens: maxTokens,
stream: true,
}),
});
if (!response.ok) {
throw new Error(`OpenAI API error: ${response.status} ${response.statusText}`);
}
return response;
}
};
}
module.exports = {
OpenAIProvider,
createSTT,
createLLM,
createStreamingLLM
};

View File

@ -1,241 +0,0 @@
let spawn, path, EventEmitter;
if (typeof window === 'undefined') {
spawn = require('child_process').spawn;
path = require('path');
EventEmitter = require('events').EventEmitter;
} else {
class DummyEventEmitter {
on() {}
emit() {}
removeAllListeners() {}
}
EventEmitter = DummyEventEmitter;
}
class WhisperSTTSession extends EventEmitter {
constructor(model, whisperService, sessionId) {
super();
this.model = model;
this.whisperService = whisperService;
this.sessionId = sessionId || `session_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`;
this.process = null;
this.isRunning = false;
this.audioBuffer = Buffer.alloc(0);
this.processingInterval = null;
this.lastTranscription = '';
}
async initialize() {
try {
await this.whisperService.ensureModelAvailable(this.model);
this.isRunning = true;
this.startProcessingLoop();
return true;
} catch (error) {
console.error('[WhisperSTT] Initialization error:', error);
this.emit('error', error);
return false;
}
}
startProcessingLoop() {
this.processingInterval = setInterval(async () => {
const minBufferSize = 16000 * 2 * 0.15;
if (this.audioBuffer.length >= minBufferSize && !this.process) {
console.log(`[WhisperSTT-${this.sessionId}] Processing audio chunk, buffer size: ${this.audioBuffer.length}`);
await this.processAudioChunk();
}
}, 1500);
}
async processAudioChunk() {
if (!this.isRunning || this.audioBuffer.length === 0) return;
const audioData = this.audioBuffer;
this.audioBuffer = Buffer.alloc(0);
try {
const tempFile = await this.whisperService.saveAudioToTemp(audioData, this.sessionId);
if (!tempFile || typeof tempFile !== 'string') {
console.error('[WhisperSTT] Invalid temp file path:', tempFile);
return;
}
const whisperPath = await this.whisperService.getWhisperPath();
const modelPath = await this.whisperService.getModelPath(this.model);
if (!whisperPath || !modelPath) {
console.error('[WhisperSTT] Invalid whisper or model path:', { whisperPath, modelPath });
return;
}
this.process = spawn(whisperPath, [
'-m', modelPath,
'-f', tempFile,
'--no-timestamps',
'--output-txt',
'--output-json',
'--language', 'auto',
'--threads', '4',
'--print-progress', 'false'
]);
let output = '';
let errorOutput = '';
this.process.stdout.on('data', (data) => {
output += data.toString();
});
this.process.stderr.on('data', (data) => {
errorOutput += data.toString();
});
this.process.on('close', async (code) => {
this.process = null;
if (code === 0 && output.trim()) {
const transcription = output.trim();
if (transcription && transcription !== this.lastTranscription) {
this.lastTranscription = transcription;
console.log(`[WhisperSTT-${this.sessionId}] Transcription: "${transcription}"`);
this.emit('transcription', {
text: transcription,
timestamp: Date.now(),
confidence: 1.0,
sessionId: this.sessionId
});
}
} else if (errorOutput) {
console.error(`[WhisperSTT-${this.sessionId}] Process error:`, errorOutput);
}
await this.whisperService.cleanupTempFile(tempFile);
});
} catch (error) {
console.error('[WhisperSTT] Processing error:', error);
this.emit('error', error);
}
}
sendRealtimeInput(audioData) {
if (!this.isRunning) {
console.warn(`[WhisperSTT-${this.sessionId}] Session not running, cannot accept audio`);
return;
}
if (typeof audioData === 'string') {
try {
audioData = Buffer.from(audioData, 'base64');
} catch (error) {
console.error('[WhisperSTT] Failed to decode base64 audio data:', error);
return;
}
} else if (audioData instanceof ArrayBuffer) {
audioData = Buffer.from(audioData);
} else if (!Buffer.isBuffer(audioData) && !(audioData instanceof Uint8Array)) {
console.error('[WhisperSTT] Invalid audio data type:', typeof audioData);
return;
}
if (!Buffer.isBuffer(audioData)) {
audioData = Buffer.from(audioData);
}
if (audioData.length > 0) {
this.audioBuffer = Buffer.concat([this.audioBuffer, audioData]);
// Log every 10th audio chunk to avoid spam
if (Math.random() < 0.1) {
console.log(`[WhisperSTT-${this.sessionId}] Received audio chunk: ${audioData.length} bytes, total buffer: ${this.audioBuffer.length} bytes`);
}
}
}
async close() {
console.log(`[WhisperSTT-${this.sessionId}] Closing session`);
this.isRunning = false;
if (this.processingInterval) {
clearInterval(this.processingInterval);
this.processingInterval = null;
}
if (this.process) {
this.process.kill('SIGTERM');
this.process = null;
}
this.removeAllListeners();
}
}
class WhisperProvider {
static async validateApiKey() {
// Whisper is a local service, no API key validation needed.
return { success: true };
}
constructor() {
this.whisperService = null;
}
async initialize() {
if (!this.whisperService) {
this.whisperService = require('../../services/whisperService');
if (!this.whisperService.isInitialized) {
await this.whisperService.initialize();
}
}
}
async createSTT(config) {
await this.initialize();
const model = config.model || 'whisper-tiny';
const sessionType = config.sessionType || 'unknown';
console.log(`[WhisperProvider] Creating ${sessionType} STT session with model: ${model}`);
// Create unique session ID based on type
const sessionId = `${sessionType}_${Date.now()}_${Math.random().toString(36).substr(2, 6)}`;
const session = new WhisperSTTSession(model, this.whisperService, sessionId);
// Log session creation
console.log(`[WhisperProvider] Created session: ${sessionId}`);
const initialized = await session.initialize();
if (!initialized) {
throw new Error('Failed to initialize Whisper STT session');
}
if (config.callbacks) {
if (config.callbacks.onmessage) {
session.on('transcription', config.callbacks.onmessage);
}
if (config.callbacks.onerror) {
session.on('error', config.callbacks.onerror);
}
if (config.callbacks.onclose) {
session.on('close', config.callbacks.onclose);
}
}
return session;
}
async createLLM() {
throw new Error('Whisper provider does not support LLM functionality');
}
async createStreamingLLM() {
console.warn('[WhisperProvider] Streaming LLM is not supported by Whisper.');
throw new Error('Whisper does not support LLM.');
}
}
module.exports = {
WhisperProvider,
WhisperSTTSession
};

View File

@ -1,54 +0,0 @@
const DOWNLOAD_CHECKSUMS = {
ollama: {
dmg: {
url: 'https://ollama.com/download/Ollama.dmg',
sha256: null // TODO: 실제 체크섬 추가 필요 - null일 경우 체크섬 검증 스킵됨
},
exe: {
url: 'https://ollama.com/download/OllamaSetup.exe',
sha256: null // TODO: 실제 체크섬 추가 필요 - null일 경우 체크섬 검증 스킵됨
},
linux: {
url: 'curl -fsSL https://ollama.com/install.sh | sh',
sha256: null // TODO: 실제 체크섬 추가 필요 - null일 경우 체크섬 검증 스킵됨
}
},
whisper: {
models: {
'whisper-tiny': {
url: 'https://huggingface.co/ggml-org/whisper.cpp/resolve/main/ggml-tiny.bin',
sha256: 'be07e048e1e599ad46341c8d2a135645097a538221678b7acdd1b1919c6e1b21'
},
'whisper-base': {
url: 'https://huggingface.co/ggml-org/whisper.cpp/resolve/main/ggml-base.bin',
sha256: '60ed5bc3dd14eea856493d334349b405782ddcaf0028d4b5df4088345fba2efe'
},
'whisper-small': {
url: 'https://huggingface.co/ggml-org/whisper.cpp/resolve/main/ggml-small.bin',
sha256: '1be3a9b2063867b937e64e2ec7483364a79917e157fa98c5d94b5c1fffea987b'
},
'whisper-medium': {
url: 'https://huggingface.co/ggml-org/whisper.cpp/resolve/main/ggml-medium.bin',
sha256: '6c14d5adee5f86394037b4e4e8b59f1673b6cee10e3cf0b11bbdbee79c156208'
}
},
binaries: {
'v1.7.6': {
mac: {
url: 'https://github.com/ggml-org/whisper.cpp/releases/download/v1.7.6/whisper-cpp-v1.7.6-mac-x64.zip',
sha256: null // TODO: 실제 체크섬 추가 필요 - null일 경우 체크섬 검증 스킵됨
},
windows: {
url: 'https://github.com/ggml-org/whisper.cpp/releases/download/v1.7.6/whisper-cpp-v1.7.6-win-x64.zip',
sha256: null // TODO: 실제 체크섬 추가 필요 - null일 경우 체크섬 검증 스킵됨
},
linux: {
url: 'https://github.com/ggml-org/whisper.cpp/releases/download/v1.7.6/whisper-cpp-v1.7.6-linux-x64.tar.gz',
sha256: null // TODO: 실제 체크섬 추가 필요 - null일 경우 체크섬 검증 스킵됨
}
}
}
}
};
module.exports = { DOWNLOAD_CHECKSUMS };

View File

@ -1,120 +0,0 @@
const LATEST_SCHEMA = {
users: {
columns: [
{ name: 'uid', type: 'TEXT PRIMARY KEY' },
{ name: 'display_name', type: 'TEXT NOT NULL' },
{ name: 'email', type: 'TEXT NOT NULL' },
{ name: 'created_at', type: 'INTEGER' },
{ name: 'auto_update_enabled', type: 'INTEGER DEFAULT 1' },
{ name: 'has_migrated_to_firebase', type: 'INTEGER DEFAULT 0' }
]
},
sessions: {
columns: [
{ name: 'id', type: 'TEXT PRIMARY KEY' },
{ name: 'uid', type: 'TEXT NOT NULL' },
{ name: 'title', type: 'TEXT' },
{ name: 'session_type', type: 'TEXT DEFAULT \'ask\'' },
{ name: 'started_at', type: 'INTEGER' },
{ name: 'ended_at', type: 'INTEGER' },
{ name: 'sync_state', type: 'TEXT DEFAULT \'clean\'' },
{ name: 'updated_at', type: 'INTEGER' }
]
},
transcripts: {
columns: [
{ name: 'id', type: 'TEXT PRIMARY KEY' },
{ name: 'session_id', type: 'TEXT NOT NULL' },
{ name: 'start_at', type: 'INTEGER' },
{ name: 'end_at', type: 'INTEGER' },
{ name: 'speaker', type: 'TEXT' },
{ name: 'text', type: 'TEXT' },
{ name: 'lang', type: 'TEXT' },
{ name: 'created_at', type: 'INTEGER' },
{ name: 'sync_state', type: 'TEXT DEFAULT \'clean\'' }
]
},
ai_messages: {
columns: [
{ name: 'id', type: 'TEXT PRIMARY KEY' },
{ name: 'session_id', type: 'TEXT NOT NULL' },
{ name: 'sent_at', type: 'INTEGER' },
{ name: 'role', type: 'TEXT' },
{ name: 'content', type: 'TEXT' },
{ name: 'tokens', type: 'INTEGER' },
{ name: 'model', type: 'TEXT' },
{ name: 'created_at', type: 'INTEGER' },
{ name: 'sync_state', type: 'TEXT DEFAULT \'clean\'' }
]
},
summaries: {
columns: [
{ name: 'session_id', type: 'TEXT PRIMARY KEY' },
{ name: 'generated_at', type: 'INTEGER' },
{ name: 'model', type: 'TEXT' },
{ name: 'text', type: 'TEXT' },
{ name: 'tldr', type: 'TEXT' },
{ name: 'bullet_json', type: 'TEXT' },
{ name: 'action_json', type: 'TEXT' },
{ name: 'tokens_used', type: 'INTEGER' },
{ name: 'updated_at', type: 'INTEGER' },
{ name: 'sync_state', type: 'TEXT DEFAULT \'clean\'' }
]
},
prompt_presets: {
columns: [
{ name: 'id', type: 'TEXT PRIMARY KEY' },
{ name: 'uid', type: 'TEXT NOT NULL' },
{ name: 'title', type: 'TEXT NOT NULL' },
{ name: 'prompt', type: 'TEXT NOT NULL' },
{ name: 'is_default', type: 'INTEGER NOT NULL' },
{ name: 'created_at', type: 'INTEGER' },
{ name: 'sync_state', type: 'TEXT DEFAULT \'clean\'' }
]
},
ollama_models: {
columns: [
{ name: 'name', type: 'TEXT PRIMARY KEY' },
{ name: 'size', type: 'TEXT NOT NULL' },
{ name: 'installed', type: 'INTEGER DEFAULT 0' },
{ name: 'installing', type: 'INTEGER DEFAULT 0' }
]
},
whisper_models: {
columns: [
{ name: 'id', type: 'TEXT PRIMARY KEY' },
{ name: 'name', type: 'TEXT NOT NULL' },
{ name: 'size', type: 'TEXT NOT NULL' },
{ name: 'installed', type: 'INTEGER DEFAULT 0' },
{ name: 'installing', type: 'INTEGER DEFAULT 0' }
]
},
provider_settings: {
columns: [
{ name: 'provider', type: 'TEXT NOT NULL' },
{ name: 'api_key', type: 'TEXT' },
{ name: 'selected_llm_model', type: 'TEXT' },
{ name: 'selected_stt_model', type: 'TEXT' },
{ name: 'is_active_llm', type: 'INTEGER DEFAULT 0' },
{ name: 'is_active_stt', type: 'INTEGER DEFAULT 0' },
{ name: 'created_at', type: 'INTEGER' },
{ name: 'updated_at', type: 'INTEGER' }
],
constraints: ['PRIMARY KEY (provider)']
},
shortcuts: {
columns: [
{ name: 'action', type: 'TEXT PRIMARY KEY' },
{ name: 'accelerator', type: 'TEXT NOT NULL' },
{ name: 'created_at', type: 'INTEGER' }
]
},
permissions: {
columns: [
{ name: 'uid', type: 'TEXT PRIMARY KEY' },
{ name: 'keychain_completed', type: 'INTEGER DEFAULT 0' }
]
}
};
module.exports = LATEST_SCHEMA;

View File

@ -1,60 +0,0 @@
const encryptionService = require('../services/encryptionService');
const { Timestamp } = require('firebase/firestore');
/**
* Creates a Firestore converter that automatically encrypts and decrypts specified fields.
* @param {string[]} fieldsToEncrypt - An array of field names to encrypt.
* @returns {import('@firebase/firestore').FirestoreDataConverter<T>} A Firestore converter.
* @template T
*/
function createEncryptedConverter(fieldsToEncrypt = []) {
return {
/**
* @param {import('@firebase/firestore').DocumentData} appObject
*/
toFirestore: (appObject) => {
const firestoreData = { ...appObject };
for (const field of fieldsToEncrypt) {
if (Object.prototype.hasOwnProperty.call(firestoreData, field) && firestoreData[field] != null) {
firestoreData[field] = encryptionService.encrypt(firestoreData[field]);
}
}
// Ensure there's a timestamp for the last modification
firestoreData.updated_at = Timestamp.now();
return firestoreData;
},
/**
* @param {import('@firebase/firestore').QueryDocumentSnapshot} snapshot
* @param {import('@firebase/firestore').SnapshotOptions} options
*/
fromFirestore: (snapshot, options) => {
const firestoreData = snapshot.data(options);
const appObject = { ...firestoreData, id: snapshot.id }; // include the document ID
for (const field of fieldsToEncrypt) {
if (Object.prototype.hasOwnProperty.call(appObject, field) && appObject[field] != null) {
try {
appObject[field] = encryptionService.decrypt(appObject[field]);
} catch (error) {
console.warn(`[FirestoreConverter] Failed to decrypt field '${field}' (possibly plaintext or key mismatch):`, error.message);
// Keep the original value instead of failing
// appObject[field] remains as is
}
}
}
// Convert Firestore Timestamps back to Unix timestamps (seconds) for app-wide consistency
for (const key in appObject) {
if (appObject[key] instanceof Timestamp) {
appObject[key] = appObject[key].seconds;
}
}
return appObject;
}
};
}
module.exports = {
createEncryptedConverter,
};

View File

@ -1,20 +0,0 @@
const sqliteRepository = require('./sqlite.repository');
// For now, we only use SQLite repository
// In the future, we could add cloud sync support
function getRepository() {
return sqliteRepository;
}
// Export all repository methods
module.exports = {
getAllModels: (...args) => getRepository().getAllModels(...args),
getModel: (...args) => getRepository().getModel(...args),
upsertModel: (...args) => getRepository().upsertModel(...args),
updateInstallStatus: (...args) => getRepository().updateInstallStatus(...args),
initializeDefaultModels: (...args) => getRepository().initializeDefaultModels(...args),
deleteModel: (...args) => getRepository().deleteModel(...args),
getInstalledModels: (...args) => getRepository().getInstalledModels(...args),
getInstallingModels: (...args) => getRepository().getInstallingModels(...args)
};

View File

@ -1,137 +0,0 @@
const sqliteClient = require('../../services/sqliteClient');
/**
* Get all Ollama models
*/
function getAllModels() {
const db = sqliteClient.getDb();
const query = 'SELECT * FROM ollama_models ORDER BY name';
try {
return db.prepare(query).all() || [];
} catch (err) {
console.error('[OllamaModel Repository] Failed to get models:', err);
throw err;
}
}
/**
* Get a specific model by name
*/
function getModel(name) {
const db = sqliteClient.getDb();
const query = 'SELECT * FROM ollama_models WHERE name = ?';
try {
return db.prepare(query).get(name);
} catch (err) {
console.error('[OllamaModel Repository] Failed to get model:', err);
throw err;
}
}
/**
* Create or update a model entry
*/
function upsertModel({ name, size, installed = false, installing = false }) {
const db = sqliteClient.getDb();
const query = `
INSERT INTO ollama_models (name, size, installed, installing)
VALUES (?, ?, ?, ?)
ON CONFLICT(name) DO UPDATE SET
size = excluded.size,
installed = excluded.installed,
installing = excluded.installing
`;
try {
db.prepare(query).run(name, size, installed ? 1 : 0, installing ? 1 : 0);
return { success: true };
} catch (err) {
console.error('[OllamaModel Repository] Failed to upsert model:', err);
throw err;
}
}
/**
* Update installation status for a model
*/
function updateInstallStatus(name, installed, installing = false) {
const db = sqliteClient.getDb();
const query = 'UPDATE ollama_models SET installed = ?, installing = ? WHERE name = ?';
try {
const result = db.prepare(query).run(installed ? 1 : 0, installing ? 1 : 0, name);
return { success: true, changes: result.changes };
} catch (err) {
console.error('[OllamaModel Repository] Failed to update install status:', err);
throw err;
}
}
/**
* Initialize default models - now done dynamically based on installed models
*/
function initializeDefaultModels() {
// Default models are now detected dynamically from Ollama installation
// This function maintains compatibility but doesn't hardcode any models
console.log('[OllamaModel Repository] Default models initialization skipped - using dynamic detection');
return { success: true };
}
/**
* Delete a model entry
*/
function deleteModel(name) {
const db = sqliteClient.getDb();
const query = 'DELETE FROM ollama_models WHERE name = ?';
try {
const result = db.prepare(query).run(name);
return { success: true, changes: result.changes };
} catch (err) {
console.error('[OllamaModel Repository] Failed to delete model:', err);
throw err;
}
}
/**
* Get installed models
*/
function getInstalledModels() {
const db = sqliteClient.getDb();
const query = 'SELECT * FROM ollama_models WHERE installed = 1 ORDER BY name';
try {
return db.prepare(query).all() || [];
} catch (err) {
console.error('[OllamaModel Repository] Failed to get installed models:', err);
throw err;
}
}
/**
* Get models currently being installed
*/
function getInstallingModels() {
const db = sqliteClient.getDb();
const query = 'SELECT * FROM ollama_models WHERE installing = 1 ORDER BY name';
try {
return db.prepare(query).all() || [];
} catch (err) {
console.error('[OllamaModel Repository] Failed to get installing models:', err);
throw err;
}
}
module.exports = {
getAllModels,
getModel,
upsertModel,
updateInstallStatus,
initializeDefaultModels,
deleteModel,
getInstalledModels,
getInstallingModels
};

View File

@ -1,11 +0,0 @@
const sqliteRepository = require('./sqlite.repository');
// This repository is not user-specific, so we always return sqlite.
function getRepository() {
return sqliteRepository;
}
module.exports = {
markKeychainCompleted: (...args) => getRepository().markKeychainCompleted(...args),
checkKeychainCompleted: (...args) => getRepository().checkKeychainCompleted(...args),
};

View File

@ -1,18 +0,0 @@
const sqliteClient = require('../../services/sqliteClient');
function markKeychainCompleted(uid) {
return sqliteClient.query(
'INSERT OR REPLACE INTO permissions (uid, keychain_completed) VALUES (?, 1)',
[uid]
);
}
function checkKeychainCompleted(uid) {
const row = sqliteClient.query('SELECT keychain_completed FROM permissions WHERE uid = ?', [uid]);
return row.length > 0 && row[0].keychain_completed === 1;
}
module.exports = {
markKeychainCompleted,
checkKeychainCompleted
};

View File

@ -1,107 +0,0 @@
const { collection, doc, addDoc, getDoc, getDocs, updateDoc, deleteDoc, query, where, orderBy, Timestamp } = require('firebase/firestore');
const { getFirestoreInstance } = require('../../services/firebaseClient');
const { createEncryptedConverter } = require('../firestoreConverter');
const encryptionService = require('../../services/encryptionService');
const userPresetConverter = createEncryptedConverter(['prompt', 'title']);
const defaultPresetConverter = {
toFirestore: (data) => data,
fromFirestore: (snapshot, options) => {
const data = snapshot.data(options);
return { ...data, id: snapshot.id };
}
};
function userPresetsCol() {
const db = getFirestoreInstance();
return collection(db, 'prompt_presets').withConverter(userPresetConverter);
}
function defaultPresetsCol() {
const db = getFirestoreInstance();
// Path must have an odd number of segments. 'v1' is a placeholder document.
return collection(db, 'defaults/v1/prompt_presets').withConverter(defaultPresetConverter);
}
async function getPresets(uid) {
const userPresetsQuery = query(userPresetsCol(), where('uid', '==', uid));
const defaultPresetsQuery = query(defaultPresetsCol()); // Defaults have no owner
const [userSnapshot, defaultSnapshot] = await Promise.all([
getDocs(userPresetsQuery),
getDocs(defaultPresetsQuery)
]);
const presets = [
...defaultSnapshot.docs.map(d => d.data()),
...userSnapshot.docs.map(d => d.data())
];
return presets.sort((a, b) => {
if (a.is_default && !b.is_default) return -1;
if (!a.is_default && b.is_default) return 1;
return a.title.localeCompare(b.title);
});
}
async function getPresetTemplates() {
const q = query(defaultPresetsCol(), orderBy('title', 'asc'));
const snapshot = await getDocs(q);
return snapshot.docs.map(doc => doc.data());
}
async function create({ uid, title, prompt }) {
const now = Timestamp.now();
const newPreset = {
uid: uid,
title,
prompt,
is_default: 0,
created_at: now,
};
const docRef = await addDoc(userPresetsCol(), newPreset);
return { id: docRef.id };
}
async function update(id, { title, prompt }, uid) {
const docRef = doc(userPresetsCol(), id);
const docSnap = await getDoc(docRef);
if (!docSnap.exists() || docSnap.data().uid !== uid || docSnap.data().is_default) {
throw new Error("Preset not found or permission denied to update.");
}
// Encrypt sensitive fields before sending to Firestore because `updateDoc` bypasses converters.
const updates = {};
if (title !== undefined) {
updates.title = encryptionService.encrypt(title);
}
if (prompt !== undefined) {
updates.prompt = encryptionService.encrypt(prompt);
}
updates.updated_at = Timestamp.now();
await updateDoc(docRef, updates);
return { changes: 1 };
}
async function del(id, uid) {
const docRef = doc(userPresetsCol(), id);
const docSnap = await getDoc(docRef);
if (!docSnap.exists() || docSnap.data().uid !== uid || docSnap.data().is_default) {
throw new Error("Preset not found or permission denied to delete.");
}
await deleteDoc(docRef);
return { changes: 1 };
}
module.exports = {
getPresets,
getPresetTemplates,
create,
update,
delete: del,
};

View File

@ -1,39 +0,0 @@
const sqliteRepository = require('./sqlite.repository');
const firebaseRepository = require('./firebase.repository');
const authService = require('../../services/authService');
function getBaseRepository() {
const user = authService.getCurrentUser();
if (user && user.isLoggedIn) {
return firebaseRepository;
}
return sqliteRepository;
}
const presetRepositoryAdapter = {
getPresets: () => {
const uid = authService.getCurrentUserId();
return getBaseRepository().getPresets(uid);
},
getPresetTemplates: () => {
return getBaseRepository().getPresetTemplates();
},
create: (options) => {
const uid = authService.getCurrentUserId();
return getBaseRepository().create({ uid, ...options });
},
update: (id, options) => {
const uid = authService.getCurrentUserId();
return getBaseRepository().update(id, options, uid);
},
delete: (id) => {
const uid = authService.getCurrentUserId();
return getBaseRepository().delete(id, uid);
},
};
module.exports = presetRepositoryAdapter;

View File

@ -1,85 +0,0 @@
const sqliteClient = require('../../services/sqliteClient');
function getPresets(uid) {
const db = sqliteClient.getDb();
const query = `
SELECT * FROM prompt_presets
WHERE uid = ? OR is_default = 1
ORDER BY is_default DESC, title ASC
`;
try {
return db.prepare(query).all(uid);
} catch (err) {
console.error('SQLite: Failed to get presets:', err);
throw err;
}
}
function getPresetTemplates() {
const db = sqliteClient.getDb();
const query = `
SELECT * FROM prompt_presets
WHERE is_default = 1
ORDER BY title ASC
`;
try {
return db.prepare(query).all();
} catch (err) {
console.error('SQLite: Failed to get preset templates:', err);
throw err;
}
}
function create({ uid, title, prompt }) {
const db = sqliteClient.getDb();
const presetId = require('crypto').randomUUID();
const now = Math.floor(Date.now() / 1000);
const query = `INSERT INTO prompt_presets (id, uid, title, prompt, is_default, created_at, sync_state) VALUES (?, ?, ?, ?, 0, ?, 'dirty')`;
try {
db.prepare(query).run(presetId, uid, title, prompt, now);
return { id: presetId };
} catch (err) {
throw err;
}
}
function update(id, { title, prompt }, uid) {
const db = sqliteClient.getDb();
const query = `UPDATE prompt_presets SET title = ?, prompt = ?, sync_state = 'dirty' WHERE id = ? AND uid = ? AND is_default = 0`;
try {
const result = db.prepare(query).run(title, prompt, id, uid);
if (result.changes === 0) {
throw new Error("Preset not found or permission denied.");
}
return { changes: result.changes };
} catch (err) {
throw err;
}
}
function del(id, uid) {
const db = sqliteClient.getDb();
const query = `DELETE FROM prompt_presets WHERE id = ? AND uid = ? AND is_default = 0`;
try {
const result = db.prepare(query).run(id, uid);
if (result.changes === 0) {
throw new Error("Preset not found or permission denied.");
}
return { changes: result.changes };
} catch (err) {
throw err;
}
}
module.exports = {
getPresets,
getPresetTemplates,
create,
update,
delete: del
};

View File

@ -1,68 +0,0 @@
const sqliteRepository = require('./sqlite.repository');
function getBaseRepository() {
// For now, we only have sqlite. This could be expanded later.
return sqliteRepository;
}
const providerSettingsRepositoryAdapter = {
// Core CRUD operations
async getByProvider(provider) {
const repo = getBaseRepository();
return await repo.getByProvider(provider);
},
async getAll() {
const repo = getBaseRepository();
return await repo.getAll();
},
async upsert(provider, settings) {
const repo = getBaseRepository();
const now = Date.now();
const settingsWithMeta = {
...settings,
provider,
updated_at: now,
created_at: settings.created_at || now
};
return await repo.upsert(provider, settingsWithMeta);
},
async remove(provider) {
const repo = getBaseRepository();
return await repo.remove(provider);
},
async removeAll() {
const repo = getBaseRepository();
return await repo.removeAll();
},
async getRawApiKeys() {
// This function should always target the local sqlite DB,
// as it's part of the local-first boot sequence.
return await sqliteRepository.getRawApiKeys();
},
async getActiveProvider(type) {
const repo = getBaseRepository();
return await repo.getActiveProvider(type);
},
async setActiveProvider(provider, type) {
const repo = getBaseRepository();
return await repo.setActiveProvider(provider, type);
},
async getActiveSettings() {
const repo = getBaseRepository();
return await repo.getActiveSettings();
}
};
module.exports = {
...providerSettingsRepositoryAdapter
};

View File

@ -1,160 +0,0 @@
const sqliteClient = require('../../services/sqliteClient');
const encryptionService = require('../../services/encryptionService');
function getByProvider(provider) {
const db = sqliteClient.getDb();
const stmt = db.prepare('SELECT * FROM provider_settings WHERE provider = ?');
const result = stmt.get(provider) || null;
if (result && result.api_key && encryptionService.looksEncrypted(result.api_key)) {
result.api_key = encryptionService.decrypt(result.api_key);
}
return result;
}
function getAll() {
const db = sqliteClient.getDb();
const stmt = db.prepare('SELECT * FROM provider_settings ORDER BY provider');
const results = stmt.all();
return results.map(result => {
if (result.api_key && encryptionService.looksEncrypted(result.api_key)) {
result.api_key = encryptionService.decrypt(result.api_key);
}
return result;
});
}
function upsert(provider, settings) {
// Validate: prevent direct setting of active status
if (settings.is_active_llm || settings.is_active_stt) {
console.warn('[ProviderSettings] Warning: is_active_llm/is_active_stt should not be set directly. Use setActiveProvider() instead.');
}
const db = sqliteClient.getDb();
// Use SQLite's UPSERT syntax (INSERT ... ON CONFLICT ... DO UPDATE)
const stmt = db.prepare(`
INSERT INTO provider_settings (provider, api_key, selected_llm_model, selected_stt_model, is_active_llm, is_active_stt, created_at, updated_at)
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
ON CONFLICT(provider) DO UPDATE SET
api_key = excluded.api_key,
selected_llm_model = excluded.selected_llm_model,
selected_stt_model = excluded.selected_stt_model,
-- is_active_llm and is_active_stt are NOT updated here
-- Use setActiveProvider() to change active status
updated_at = excluded.updated_at
`);
const result = stmt.run(
provider,
settings.api_key || null,
settings.selected_llm_model || null,
settings.selected_stt_model || null,
0, // is_active_llm - always 0, use setActiveProvider to activate
0, // is_active_stt - always 0, use setActiveProvider to activate
settings.created_at || Date.now(),
settings.updated_at
);
return { changes: result.changes };
}
function remove(provider) {
const db = sqliteClient.getDb();
const stmt = db.prepare('DELETE FROM provider_settings WHERE provider = ?');
const result = stmt.run(provider);
return { changes: result.changes };
}
function removeAll() {
const db = sqliteClient.getDb();
const stmt = db.prepare('DELETE FROM provider_settings');
const result = stmt.run();
return { changes: result.changes };
}
function getRawApiKeys() {
const db = sqliteClient.getDb();
const stmt = db.prepare('SELECT api_key FROM provider_settings');
return stmt.all();
}
// Get active provider for a specific type (llm or stt)
function getActiveProvider(type) {
const db = sqliteClient.getDb();
const column = type === 'llm' ? 'is_active_llm' : 'is_active_stt';
const stmt = db.prepare(`SELECT * FROM provider_settings WHERE ${column} = 1`);
const result = stmt.get() || null;
if (result && result.api_key && encryptionService.looksEncrypted(result.api_key)) {
result.api_key = encryptionService.decrypt(result.api_key);
}
return result;
}
// Set active provider for a specific type
function setActiveProvider(provider, type) {
const db = sqliteClient.getDb();
const column = type === 'llm' ? 'is_active_llm' : 'is_active_stt';
// Start transaction to ensure only one provider is active
db.transaction(() => {
// First, deactivate all providers for this type
const deactivateStmt = db.prepare(`UPDATE provider_settings SET ${column} = 0`);
deactivateStmt.run();
// Then activate the specified provider
if (provider) {
const activateStmt = db.prepare(`UPDATE provider_settings SET ${column} = 1 WHERE provider = ?`);
activateStmt.run(provider);
}
})();
return { success: true };
}
// Get all active settings (both llm and stt)
function getActiveSettings() {
const db = sqliteClient.getDb();
const stmt = db.prepare(`
SELECT * FROM provider_settings
WHERE (is_active_llm = 1 OR is_active_stt = 1)
ORDER BY provider
`);
const results = stmt.all();
// Decrypt API keys and organize by type
const activeSettings = {
llm: null,
stt: null
};
results.forEach(result => {
if (result.api_key && encryptionService.looksEncrypted(result.api_key)) {
result.api_key = encryptionService.decrypt(result.api_key);
}
if (result.is_active_llm) {
activeSettings.llm = result;
}
if (result.is_active_stt) {
activeSettings.stt = result;
}
});
return activeSettings;
}
module.exports = {
getByProvider,
getAll,
upsert,
remove,
removeAll,
getRawApiKeys,
getActiveProvider,
setActiveProvider,
getActiveSettings
};

View File

@ -1,161 +0,0 @@
const { doc, getDoc, collection, addDoc, query, where, getDocs, writeBatch, orderBy, limit, updateDoc, Timestamp } = require('firebase/firestore');
const { getFirestoreInstance } = require('../../services/firebaseClient');
const { createEncryptedConverter } = require('../firestoreConverter');
const encryptionService = require('../../services/encryptionService');
const sessionConverter = createEncryptedConverter(['title']);
function sessionsCol() {
const db = getFirestoreInstance();
return collection(db, 'sessions').withConverter(sessionConverter);
}
// Sub-collection references are now built from the top-level
function subCollections(sessionId) {
const db = getFirestoreInstance();
const sessionPath = `sessions/${sessionId}`;
return {
transcripts: collection(db, `${sessionPath}/transcripts`),
ai_messages: collection(db, `${sessionPath}/ai_messages`),
summary: collection(db, `${sessionPath}/summary`),
}
}
async function getById(id) {
const docRef = doc(sessionsCol(), id);
const docSnap = await getDoc(docRef);
return docSnap.exists() ? docSnap.data() : null;
}
async function create(uid, type = 'ask') {
const now = Timestamp.now();
const newSession = {
uid: uid,
members: [uid], // For future sharing functionality
title: `Session @ ${new Date().toLocaleTimeString()}`,
session_type: type,
started_at: now,
updated_at: now,
ended_at: null,
};
const docRef = await addDoc(sessionsCol(), newSession);
console.log(`Firebase: Created session ${docRef.id} for user ${uid}`);
return docRef.id;
}
async function getAllByUserId(uid) {
const q = query(sessionsCol(), where('members', 'array-contains', uid), orderBy('started_at', 'desc'));
const querySnapshot = await getDocs(q);
return querySnapshot.docs.map(doc => doc.data());
}
async function updateTitle(id, title) {
const docRef = doc(sessionsCol(), id);
await updateDoc(docRef, {
title: encryptionService.encrypt(title),
updated_at: Timestamp.now()
});
return { changes: 1 };
}
async function deleteWithRelatedData(id) {
const db = getFirestoreInstance();
const batch = writeBatch(db);
const { transcripts, ai_messages, summary } = subCollections(id);
const [transcriptsSnap, aiMessagesSnap, summarySnap] = await Promise.all([
getDocs(query(transcripts)),
getDocs(query(ai_messages)),
getDocs(query(summary)),
]);
transcriptsSnap.forEach(d => batch.delete(d.ref));
aiMessagesSnap.forEach(d => batch.delete(d.ref));
summarySnap.forEach(d => batch.delete(d.ref));
const sessionRef = doc(sessionsCol(), id);
batch.delete(sessionRef);
await batch.commit();
return { success: true };
}
async function end(id) {
const docRef = doc(sessionsCol(), id);
await updateDoc(docRef, { ended_at: Timestamp.now() });
return { changes: 1 };
}
async function updateType(id, type) {
const docRef = doc(sessionsCol(), id);
await updateDoc(docRef, { session_type: type });
return { changes: 1 };
}
async function touch(id) {
const docRef = doc(sessionsCol(), id);
await updateDoc(docRef, { updated_at: Timestamp.now() });
return { changes: 1 };
}
async function getOrCreateActive(uid, requestedType = 'ask') {
const findQuery = query(
sessionsCol(),
where('uid', '==', uid),
where('ended_at', '==', null),
orderBy('session_type', 'desc'),
limit(1)
);
const activeSessionSnap = await getDocs(findQuery);
if (!activeSessionSnap.empty) {
const activeSessionDoc = activeSessionSnap.docs[0];
const sessionRef = doc(sessionsCol(), activeSessionDoc.id);
const activeSession = activeSessionDoc.data();
console.log(`[Repo] Found active Firebase session ${activeSession.id}`);
const updates = { updated_at: Timestamp.now() };
if (activeSession.session_type === 'ask' && requestedType === 'listen') {
updates.session_type = 'listen';
console.log(`[Repo] Promoted Firebase session ${activeSession.id} to 'listen' type.`);
}
await updateDoc(sessionRef, updates);
return activeSessionDoc.id;
} else {
console.log(`[Repo] No active Firebase session for user ${uid}. Creating new.`);
return create(uid, requestedType);
}
}
async function endAllActiveSessions(uid) {
const q = query(sessionsCol(), where('uid', '==', uid), where('ended_at', '==', null));
const snapshot = await getDocs(q);
if (snapshot.empty) return { changes: 0 };
const batch = writeBatch(getFirestoreInstance());
const now = Timestamp.now();
snapshot.forEach(d => {
batch.update(d.ref, { ended_at: now });
});
await batch.commit();
console.log(`[Repo] Ended ${snapshot.size} active session(s) for user ${uid}.`);
return { changes: snapshot.size };
}
module.exports = {
getById,
create,
getAllByUserId,
updateTitle,
deleteWithRelatedData,
end,
updateType,
touch,
getOrCreateActive,
endAllActiveSessions,
};

View File

@ -1,60 +0,0 @@
const sqliteRepository = require('./sqlite.repository');
const firebaseRepository = require('./firebase.repository');
let authService = null;
function setAuthService(service) {
authService = service;
}
function getBaseRepository() {
if (!authService) {
// Fallback or error if authService is not set, to prevent crashes.
// During initial load, it might not be set, so we default to sqlite.
return sqliteRepository;
}
const user = authService.getCurrentUser();
if (user && user.isLoggedIn) {
return firebaseRepository;
}
return sqliteRepository;
}
// The adapter layer that injects the UID
const sessionRepositoryAdapter = {
setAuthService, // Expose the setter
getById: (id) => getBaseRepository().getById(id),
create: (type = 'ask') => {
const uid = authService.getCurrentUserId();
return getBaseRepository().create(uid, type);
},
getAllByUserId: () => {
const uid = authService.getCurrentUserId();
return getBaseRepository().getAllByUserId(uid);
},
updateTitle: (id, title) => getBaseRepository().updateTitle(id, title),
deleteWithRelatedData: (id) => getBaseRepository().deleteWithRelatedData(id),
end: (id) => getBaseRepository().end(id),
updateType: (id, type) => getBaseRepository().updateType(id, type),
touch: (id) => getBaseRepository().touch(id),
getOrCreateActive: (requestedType = 'ask') => {
const uid = authService.getCurrentUserId();
return getBaseRepository().getOrCreateActive(uid, requestedType);
},
endAllActiveSessions: () => {
const uid = authService.getCurrentUserId();
return getBaseRepository().endAllActiveSessions(uid);
},
};
module.exports = sessionRepositoryAdapter;

View File

@ -1,138 +0,0 @@
const sqliteClient = require('../../services/sqliteClient');
function getById(id) {
const db = sqliteClient.getDb();
return db.prepare('SELECT * FROM sessions WHERE id = ?').get(id);
}
function create(uid, type = 'ask') {
const db = sqliteClient.getDb();
const sessionId = require('crypto').randomUUID();
const now = Math.floor(Date.now() / 1000);
const query = `INSERT INTO sessions (id, uid, title, session_type, started_at, updated_at) VALUES (?, ?, ?, ?, ?, ?)`;
try {
db.prepare(query).run(sessionId, uid, `Session @ ${new Date().toLocaleTimeString()}`, type, now, now);
console.log(`SQLite: Created session ${sessionId} for user ${uid} (type: ${type})`);
return sessionId;
} catch (err) {
console.error('SQLite: Failed to create session:', err);
throw err;
}
}
function getAllByUserId(uid) {
const db = sqliteClient.getDb();
const query = "SELECT id, uid, title, session_type, started_at, ended_at, sync_state, updated_at FROM sessions WHERE uid = ? ORDER BY started_at DESC";
return db.prepare(query).all(uid);
}
function updateTitle(id, title) {
const db = sqliteClient.getDb();
const result = db.prepare('UPDATE sessions SET title = ? WHERE id = ?').run(title, id);
return { changes: result.changes };
}
function deleteWithRelatedData(id) {
const db = sqliteClient.getDb();
const transaction = db.transaction(() => {
db.prepare("DELETE FROM transcripts WHERE session_id = ?").run(id);
db.prepare("DELETE FROM ai_messages WHERE session_id = ?").run(id);
db.prepare("DELETE FROM summaries WHERE session_id = ?").run(id);
db.prepare("DELETE FROM sessions WHERE id = ?").run(id);
});
try {
transaction();
return { success: true };
} catch (err) {
throw err;
}
}
function end(id) {
const db = sqliteClient.getDb();
const now = Math.floor(Date.now() / 1000);
const query = `UPDATE sessions SET ended_at = ?, updated_at = ? WHERE id = ?`;
const result = db.prepare(query).run(now, now, id);
return { changes: result.changes };
}
function updateType(id, type) {
const db = sqliteClient.getDb();
const now = Math.floor(Date.now() / 1000);
const query = 'UPDATE sessions SET session_type = ?, updated_at = ? WHERE id = ?';
const result = db.prepare(query).run(type, now, id);
return { changes: result.changes };
}
function touch(id) {
const db = sqliteClient.getDb();
const now = Math.floor(Date.now() / 1000);
const query = 'UPDATE sessions SET updated_at = ? WHERE id = ?';
const result = db.prepare(query).run(now, id);
return { changes: result.changes };
}
function getOrCreateActive(uid, requestedType = 'ask') {
const db = sqliteClient.getDb();
// 1. Look for ANY active session for the user (ended_at IS NULL).
// Prefer 'listen' sessions over 'ask' sessions to ensure continuity.
const findQuery = `
SELECT id, session_type FROM sessions
WHERE uid = ? AND ended_at IS NULL
ORDER BY CASE session_type WHEN 'listen' THEN 1 WHEN 'ask' THEN 2 ELSE 3 END
LIMIT 1
`;
const activeSession = db.prepare(findQuery).get(uid);
if (activeSession) {
// An active session exists.
console.log(`[Repo] Found active session ${activeSession.id} of type ${activeSession.session_type}`);
// 2. Promotion Logic: If it's an 'ask' session and we need 'listen', promote it.
if (activeSession.session_type === 'ask' && requestedType === 'listen') {
updateType(activeSession.id, 'listen');
console.log(`[Repo] Promoted session ${activeSession.id} to 'listen' type.`);
}
// 3. Touch the session and return its ID.
touch(activeSession.id);
return activeSession.id;
} else {
// 4. No active session found, create a new one.
console.log(`[Repo] No active session for user ${uid}. Creating new '${requestedType}' session.`);
return create(uid, requestedType);
}
}
function endAllActiveSessions(uid) {
const db = sqliteClient.getDb();
const now = Math.floor(Date.now() / 1000);
// Filter by uid to match the Firebase repository's behavior.
const query = `UPDATE sessions SET ended_at = ?, updated_at = ? WHERE ended_at IS NULL AND uid = ?`;
try {
const result = db.prepare(query).run(now, now, uid);
console.log(`[Repo] Ended ${result.changes} active SQLite session(s) for user ${uid}.`);
return { changes: result.changes };
} catch (err) {
console.error('SQLite: Failed to end all active sessions:', err);
throw err;
}
}
module.exports = {
getById,
create,
getAllByUserId,
updateTitle,
deleteWithRelatedData,
end,
updateType,
touch,
getOrCreateActive,
endAllActiveSessions,
};

Some files were not shown because too many files have changed in this diff Show More