Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion src/preload/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,19 @@
ElectronLlmRenderer,
RendererLoadFunction,
} from '../interfaces.js';

// Add Node.js process type declaration
declare const process: {
contextIsolated?: boolean;
[key: string]: any;
};

declare global {
interface Window {
electronAi: ElectronLlmRenderer;
}
}

import type {
LanguageModelCreateOptions,
LanguageModelPromptOptions,
Expand Down Expand Up @@ -158,7 +171,20 @@
};

export const loadElectronLlm: RendererLoadFunction = async () => {
contextBridge.exposeInMainWorld('electronAi', electronAi);
try {
// Check if contextBridge is available (context isolation is enabled)
if (process.contextIsolated) {
// Use contextBridge when context isolation is on
contextBridge.exposeInMainWorld('electronAi', electronAi);
} else {
// When context isolation is off, add directly to window global
window.electronAi = electronAi;
}
} catch (error) {
// Fallback in case of any errors
console.error('Error exposing electronAi API:', error);
window.electronAi = electronAi;

Check failure on line 186 in src/preload/index.ts

View workflow job for this annotation

GitHub Actions / Test (20.19, macos-latest)

Unhandled error

ReferenceError: window is not defined ❯ loadElectronLlm src/preload/index.ts:186:5 ❯ src/preload/index.ts:190:1 ❯ processTicksAndRejections node:internal/process/task_queues:95:5 ❯ VitestExecutor.runModule node_modules/vite-node/dist/client.mjs:401:5 ❯ VitestExecutor.directRequest node_modules/vite-node/dist/client.mjs:381:5 ❯ VitestExecutor.cachedRequest node_modules/vite-node/dist/client.mjs:206:14 ❯ VitestExecutor.dependencyRequest node_modules/vite-node/dist/client.mjs:259:12 ❯ __tests__/preload.test.ts:2:1 ❯ VitestExecutor.runModule node_modules/vite-node/dist/client.mjs:401:5 ❯ VitestExecutor.directRequest node_modules/vite-node/dist/client.mjs:381:5 This error originated in "__tests__/preload.test.ts" test file. It doesn't mean the error was thrown inside the file itself, but while it was running. The latest test that might've caused the error is "__tests__/preload.test.ts". It might mean one of the following: - The error was thrown, while Vitest was running this test. - If the error occurred after the test had been completed, this was the last documented test before it was thrown.

Check failure on line 186 in src/preload/index.ts

View workflow job for this annotation

GitHub Actions / Test (20.19, macos-latest)

__tests__/preload.test.ts > Preload Interface > should expose electronAi on globalThis

ReferenceError: window is not defined ❯ Module.loadElectronLlm src/preload/index.ts:186:5 ❯ __tests__/preload.test.ts:27:11

Check failure on line 186 in src/preload/index.ts

View workflow job for this annotation

GitHub Actions / Test (20.19, macos-latest)

__tests__/preload.test.ts > Preload Interface > create should invoke with correct ipcMessage and options

ReferenceError: window is not defined ❯ Module.loadElectronLlm src/preload/index.ts:186:5 ❯ __tests__/preload.test.ts:27:11

Check failure on line 186 in src/preload/index.ts

View workflow job for this annotation

GitHub Actions / Test (20.19, macos-latest)

__tests__/preload.test.ts > Preload Interface > destroy should invoke with correct ipcMessage

ReferenceError: window is not defined ❯ Module.loadElectronLlm src/preload/index.ts:186:5 ❯ __tests__/preload.test.ts:27:11

Check failure on line 186 in src/preload/index.ts

View workflow job for this annotation

GitHub Actions / Test (20.19, macos-latest)

__tests__/preload.test.ts > Preload Interface > prompt should invoke with correct params

ReferenceError: window is not defined ❯ Module.loadElectronLlm src/preload/index.ts:186:5 ❯ __tests__/preload.test.ts:27:11

Check failure on line 186 in src/preload/index.ts

View workflow job for this annotation

GitHub Actions / Test (20.19, macos-latest)

__tests__/preload.test.ts > Preload Interface > promptStreaming should invoke with correct params

ReferenceError: window is not defined ❯ Module.loadElectronLlm src/preload/index.ts:186:5 ❯ __tests__/preload.test.ts:27:11

Check failure on line 186 in src/preload/index.ts

View workflow job for this annotation

GitHub Actions / Test (20.19, ubuntu-latest)

Unhandled error

ReferenceError: window is not defined ❯ loadElectronLlm src/preload/index.ts:186:5 ❯ src/preload/index.ts:190:1 ❯ processTicksAndRejections node:internal/process/task_queues:95:5 ❯ VitestExecutor.runModule node_modules/vite-node/dist/client.mjs:401:5 ❯ VitestExecutor.directRequest node_modules/vite-node/dist/client.mjs:381:5 ❯ VitestExecutor.cachedRequest node_modules/vite-node/dist/client.mjs:206:14 ❯ VitestExecutor.dependencyRequest node_modules/vite-node/dist/client.mjs:259:12 ❯ __tests__/preload.test.ts:2:1 ❯ VitestExecutor.runModule node_modules/vite-node/dist/client.mjs:401:5 ❯ VitestExecutor.directRequest node_modules/vite-node/dist/client.mjs:381:5 This error originated in "__tests__/preload.test.ts" test file. It doesn't mean the error was thrown inside the file itself, but while it was running. The latest test that might've caused the error is "__tests__/preload.test.ts". It might mean one of the following: - The error was thrown, while Vitest was running this test. - If the error occurred after the test had been completed, this was the last documented test before it was thrown.

Check failure on line 186 in src/preload/index.ts

View workflow job for this annotation

GitHub Actions / Test (20.19, ubuntu-latest)

__tests__/preload.test.ts > Preload Interface > should expose electronAi on globalThis

ReferenceError: window is not defined ❯ Module.loadElectronLlm src/preload/index.ts:186:5 ❯ __tests__/preload.test.ts:27:11

Check failure on line 186 in src/preload/index.ts

View workflow job for this annotation

GitHub Actions / Test (20.19, ubuntu-latest)

__tests__/preload.test.ts > Preload Interface > create should invoke with correct ipcMessage and options

ReferenceError: window is not defined ❯ Module.loadElectronLlm src/preload/index.ts:186:5 ❯ __tests__/preload.test.ts:27:11

Check failure on line 186 in src/preload/index.ts

View workflow job for this annotation

GitHub Actions / Test (20.19, ubuntu-latest)

__tests__/preload.test.ts > Preload Interface > destroy should invoke with correct ipcMessage

ReferenceError: window is not defined ❯ Module.loadElectronLlm src/preload/index.ts:186:5 ❯ __tests__/preload.test.ts:27:11

Check failure on line 186 in src/preload/index.ts

View workflow job for this annotation

GitHub Actions / Test (20.19, ubuntu-latest)

__tests__/preload.test.ts > Preload Interface > prompt should invoke with correct params

ReferenceError: window is not defined ❯ Module.loadElectronLlm src/preload/index.ts:186:5 ❯ __tests__/preload.test.ts:27:11

Check failure on line 186 in src/preload/index.ts

View workflow job for this annotation

GitHub Actions / Test (20.19, ubuntu-latest)

__tests__/preload.test.ts > Preload Interface > promptStreaming should invoke with correct params

ReferenceError: window is not defined ❯ Module.loadElectronLlm src/preload/index.ts:186:5 ❯ __tests__/preload.test.ts:27:11

Check failure on line 186 in src/preload/index.ts

View workflow job for this annotation

GitHub Actions / Test (20.19, windows-latest)

Unhandled error

ReferenceError: window is not defined ❯ loadElectronLlm src/preload/index.ts:186:5 ❯ src/preload/index.ts:190:1 ❯ processTicksAndRejections node:internal/process/task_queues:95:5 ❯ VitestExecutor.runModule node_modules/vite-node/dist/client.mjs:401:5 ❯ VitestExecutor.directRequest node_modules/vite-node/dist/client.mjs:381:5 ❯ VitestExecutor.cachedRequest node_modules/vite-node/dist/client.mjs:206:14 ❯ VitestExecutor.dependencyRequest node_modules/vite-node/dist/client.mjs:259:12 ❯ __tests__/preload.test.ts:2:1 ❯ VitestExecutor.runModule node_modules/vite-node/dist/client.mjs:401:5 ❯ VitestExecutor.directRequest node_modules/vite-node/dist/client.mjs:381:5 This error originated in "__tests__/preload.test.ts" test file. It doesn't mean the error was thrown inside the file itself, but while it was running. The latest test that might've caused the error is "__tests__/preload.test.ts". It might mean one of the following: - The error was thrown, while Vitest was running this test. - If the error occurred after the test had been completed, this was the last documented test before it was thrown.

Check failure on line 186 in src/preload/index.ts

View workflow job for this annotation

GitHub Actions / Test (20.19, windows-latest)

__tests__/preload.test.ts > Preload Interface > should expose electronAi on globalThis

ReferenceError: window is not defined ❯ Module.loadElectronLlm src/preload/index.ts:186:5 ❯ __tests__/preload.test.ts:27:11

Check failure on line 186 in src/preload/index.ts

View workflow job for this annotation

GitHub Actions / Test (20.19, windows-latest)

__tests__/preload.test.ts > Preload Interface > create should invoke with correct ipcMessage and options

ReferenceError: window is not defined ❯ Module.loadElectronLlm src/preload/index.ts:186:5 ❯ __tests__/preload.test.ts:27:11

Check failure on line 186 in src/preload/index.ts

View workflow job for this annotation

GitHub Actions / Test (20.19, windows-latest)

__tests__/preload.test.ts > Preload Interface > destroy should invoke with correct ipcMessage

ReferenceError: window is not defined ❯ Module.loadElectronLlm src/preload/index.ts:186:5 ❯ __tests__/preload.test.ts:27:11

Check failure on line 186 in src/preload/index.ts

View workflow job for this annotation

GitHub Actions / Test (20.19, windows-latest)

__tests__/preload.test.ts > Preload Interface > prompt should invoke with correct params

ReferenceError: window is not defined ❯ Module.loadElectronLlm src/preload/index.ts:186:5 ❯ __tests__/preload.test.ts:27:11

Check failure on line 186 in src/preload/index.ts

View workflow job for this annotation

GitHub Actions / Test (20.19, windows-latest)

__tests__/preload.test.ts > Preload Interface > promptStreaming should invoke with correct params

ReferenceError: window is not defined ❯ Module.loadElectronLlm src/preload/index.ts:186:5 ❯ __tests__/preload.test.ts:27:11
}
};

loadElectronLlm();
Loading