Skip to content

armgit / jan

Public Imported from GitHub
0

Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.

fix(web-app): avoid Tauri invoke crash for remote provider chat (#7815)

dataCenter430fae006b46b3e
diff --git a/web-app/src/lib/model-factory.ts b/web-app/src/lib/model-factory.ts
index c6f166293..963d83f30 100644
--- a/web-app/src/lib/model-factory.ts
+++ b/web-app/src/lib/model-factory.ts
@@ -55,6 +55,7 @@ import { createXai } from '@ai-sdk/xai'
import { invoke } from '@tauri-apps/api/core'
import { SessionInfo } from '@janhq/core'
import { fetch as httpFetch } from '@tauri-apps/plugin-http'
+import { isPlatformTauri } from '@/lib/platform/utils'
/**
* Llama.cpp timings structure from the response
@@ -121,9 +122,9 @@ const providerMetadataExtractor: MetadataExtractor = {
* Create a custom fetch function that injects additional parameters into the request body
*/
function createCustomFetch(
- baseFetch: typeof httpFetch,
+ baseFetch: typeof globalThis.fetch,
parameters: Record<string, unknown>
-): typeof httpFetch {
+): typeof globalThis.fetch {
return async (input: RequestInfo | URL, init?: RequestInit): Promise<Response> => {
// Only transform POST requests with JSON body
if (init?.method === 'POST' || !init?.method) {
@@ -142,6 +143,20 @@ function createCustomFetch(
}
}
+function getRuntimeFetch(): typeof globalThis.fetch {
+ const maybeWindow = globalThis as typeof globalThis & {
+ __TAURI__?: unknown
+ __TAURI_INTERNALS__?: unknown
+ }
+ const hasTauriRuntime =
+ typeof maybeWindow.__TAURI__ !== 'undefined' ||
+ typeof maybeWindow.__TAURI_INTERNALS__ !== 'undefined'
+
+ return isPlatformTauri() && hasTauriRuntime
+ ? (httpFetch as typeof globalThis.fetch)
+ : globalThis.fetch
+}
+
/**
* Custom fetch for Foundation Models that routes through Tauri IPC
* instead of HTTP, emulating an OpenAI-compatible fetch interface.
@@ -529,7 +544,7 @@ export class ModelFactory {
apiKey: provider.api_key,
baseURL: provider.base_url,
headers: Object.keys(headers).length > 0 ? headers : undefined,
- fetch: createCustomFetch(httpFetch, parameters),
+ fetch: createCustomFetch(getRuntimeFetch(), parameters),
})
return anthropic(modelId)
@@ -556,7 +571,7 @@ export class ModelFactory {
apiKey: provider.api_key,
baseURL: provider.base_url,
headers: Object.keys(headers).length > 0 ? headers : undefined,
- fetch: createCustomFetch(httpFetch, parameters),
+ fetch: createCustomFetch(getRuntimeFetch(), parameters),
})
return openai(modelId)
@@ -583,7 +598,7 @@ export class ModelFactory {
apiKey: provider.api_key,
baseURL: provider.base_url,
headers: Object.keys(headers).length > 0 ? headers : undefined,
- fetch: createCustomFetch(httpFetch, parameters),
+ fetch: createCustomFetch(getRuntimeFetch(), parameters),
})
return xai(modelId)
@@ -616,7 +631,7 @@ export class ModelFactory {
baseURL: provider.base_url || 'https://api.openai.com/v1',
headers,
includeUsage: true,
- fetch: createCustomFetch(httpFetch, parameters),
+ fetch: createCustomFetch(getRuntimeFetch(), parameters),
})
return openAICompatible.languageModel(modelId)