fix: custom fetch for all providers (#6538)

* fix: custom fetch for all providers

* fix: run in development should use built-in fetch
This commit is contained in:
Louis 2025-09-23 09:55:36 +07:00 committed by GitHub
parent 885da29f28
commit 568ee857d5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 19 additions and 10 deletions

View File

@ -6,7 +6,7 @@
"build": {
"frontendDist": "../web-app/dist",
"devUrl": "http://localhost:1420",
"beforeDevCommand": "cross-env IS_TAURI=true yarn dev:web",
"beforeDevCommand": "cross-env IS_TAURI=true IS_DEV=true yarn dev:web",
"beforeBuildCommand": "cross-env IS_TAURI=true yarn build:web"
},
"app": {

View File

@ -169,11 +169,12 @@ export const sendCompletion = async (
providerName = 'openai-compatible'
const tokenJS = new TokenJS({
apiKey: provider.api_key ?? (await getServiceHub().core().getAppToken()) ?? '',
apiKey:
provider.api_key ?? (await getServiceHub().core().getAppToken()) ?? '',
// TODO: Retrieve from extension settings
baseURL: provider.base_url,
// Use Tauri's fetch to avoid CORS issues only for openai-compatible provider
...(providerName === 'openai-compatible' && { fetch: getServiceHub().providers().fetch() }),
fetch: IS_DEV ? fetch : getServiceHub().providers().fetch(),
// OpenRouter identification headers for Jan
// ref: https://openrouter.ai/docs/api-reference/overview#headers
...(provider.provider === 'openrouter' && {
@ -183,10 +184,11 @@ export const sendCompletion = async (
},
}),
// Add Origin header for local providers to avoid CORS issues
...((provider.base_url?.includes('localhost:') || provider.base_url?.includes('127.0.0.1:')) && {
...((provider.base_url?.includes('localhost:') ||
provider.base_url?.includes('127.0.0.1:')) && {
fetch: getServiceHub().providers().fetch(),
defaultHeaders: {
'Origin': 'tauri://localhost',
Origin: 'tauri://localhost',
},
}),
} as ExtendedConfigOptions)
@ -402,7 +404,10 @@ export const postMessageProcessing = async (
console.log('Parsed tool parameters:', toolParameters)
} catch (error) {
console.error('Failed to parse tool arguments:', error)
console.error('Raw arguments that failed:', toolCall.function.arguments)
console.error(
'Raw arguments that failed:',
toolCall.function.arguments
)
}
}
const approved =
@ -416,10 +421,12 @@ export const postMessageProcessing = async (
)
: true)
const { promise, cancel } = getServiceHub().mcp().callToolWithCancellation({
toolName: toolCall.function.name,
arguments: toolCall.function.arguments.length ? toolParameters : {},
})
const { promise, cancel } = getServiceHub()
.mcp()
.callToolWithCancellation({
toolName: toolCall.function.name,
arguments: toolCall.function.arguments.length ? toolParameters : {},
})
useAppState.getState().setCancelToolCall(cancel)

View File

@ -22,6 +22,7 @@ declare global {
declare const MODEL_CATALOG_URL: string
declare const AUTO_UPDATER_DISABLED: boolean
declare const GA_MEASUREMENT_ID: string
declare const IS_DEV: boolean
interface Window {
core: AppCore | undefined
gtag?: (...args: unknown[]) => void

View File

@ -40,6 +40,7 @@ export default defineConfig(({ mode }) => {
},
define: {
IS_TAURI: JSON.stringify(process.env.IS_TAURI),
IS_DEV: JSON.stringify(process.env.IS_DEV),
IS_WEB_APP: JSON.stringify(false),
IS_MACOS: JSON.stringify(
process.env.TAURI_ENV_PLATFORM?.includes('darwin') ?? false