Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
7 changes: 7 additions & 0 deletions web-app/src/lib/completion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,18 +71,18 @@

// Add attachments to content array
if (attachments) {
attachments.forEach((attachment) => {
if (attachment.type.startsWith('image/')) {
contentParts.push({
type: ContentType.Image,
image_url: {
url: `data:${attachment.type};base64,${attachment.base64}`,
detail: 'auto',
},
} as any)
}
})
}

Check warning on line 85 in web-app/src/lib/completion.ts

View workflow job for this annotation

GitHub Actions / coverage-check

74-85 lines are not covered with tests

return {
type: 'text',
Expand Down Expand Up @@ -153,47 +153,54 @@
* @returns
*/
export const sendCompletion = async (
thread: Thread,
provider: ModelProvider,
messages: ChatCompletionMessageParam[],
abortController: AbortController,
tools: MCPTool[] = [],
stream: boolean = true,
params: Record<string, object> = {}
): Promise<ChatCompletionResponse | undefined> => {
if (!thread?.model?.id || !provider) return undefined

Check warning on line 164 in web-app/src/lib/completion.ts

View workflow job for this annotation

GitHub Actions / coverage-check

156-164 lines are not covered with tests

let providerName = provider.provider as unknown as keyof typeof models

Check warning on line 166 in web-app/src/lib/completion.ts

View workflow job for this annotation

GitHub Actions / coverage-check

166 line is not covered with tests

if (!Object.keys(models).some((key) => key === providerName))
providerName = 'openai-compatible'

Check warning on line 169 in web-app/src/lib/completion.ts

View workflow job for this annotation

GitHub Actions / coverage-check

168-169 lines are not covered with tests

const tokenJS = new TokenJS({
apiKey: provider.api_key ?? (await getServiceHub().core().getAppToken()) ?? '',

Check warning on line 172 in web-app/src/lib/completion.ts

View workflow job for this annotation

GitHub Actions / coverage-check

171-172 lines are not covered with tests
// TODO: Retrieve from extension settings
baseURL: provider.base_url,

Check warning on line 174 in web-app/src/lib/completion.ts

View workflow job for this annotation

GitHub Actions / coverage-check

174 line is not covered with tests
// Use Tauri's fetch to avoid CORS issues only for openai-compatible provider
...(providerName === 'openai-compatible' && { fetch: getServiceHub().providers().fetch() }),

Check warning on line 176 in web-app/src/lib/completion.ts

View workflow job for this annotation

GitHub Actions / coverage-check

176 line is not covered with tests
// OpenRouter identification headers for Jan
// ref: https://openrouter.ai/docs/api-reference/overview#headers
...(provider.provider === 'openrouter' && {
defaultHeaders: {
'HTTP-Referer': 'https://jan.ai',
'X-Title': 'Jan',
},
}),

Check warning on line 184 in web-app/src/lib/completion.ts

View workflow job for this annotation

GitHub Actions / coverage-check

179-184 lines are not covered with tests
// Add Origin header for local providers to avoid CORS issues
...((provider.base_url?.includes('localhost:') || provider.base_url?.includes('127.0.0.1:')) && {
fetch: getServiceHub().providers().fetch(),
defaultHeaders: {
'Origin': 'tauri://localhost',
},
}),
} as ExtendedConfigOptions)

Check warning on line 192 in web-app/src/lib/completion.ts

View workflow job for this annotation

GitHub Actions / coverage-check

186-192 lines are not covered with tests

if (
thread.model.id &&
!Object.values(models[providerName]).flat().includes(thread.model.id) &&
!tokenJS.extendedModelExist(providerName as any, thread.model.id) &&
provider.provider !== 'llamacpp'
) {
try {
tokenJS.extendModelList(
providerName as any,
thread.model.id,

Check warning on line 203 in web-app/src/lib/completion.ts

View workflow job for this annotation

GitHub Actions / coverage-check

194-203 lines are not covered with tests
// This is to inherit the model capabilities from another built-in model
// Can be anything that support all model capabilities
models.anthropic.models[0]
Expand Down
6 changes: 6 additions & 0 deletions web-app/src/services/providers/tauri.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,12 @@ export class TauriProvidersService extends DefaultProvidersService {
'Content-Type': 'application/json',
}

// Add Origin header for local providers to avoid CORS issues
// Some local providers (like Ollama) require an Origin header
if (provider.base_url.includes('localhost:') || provider.base_url.includes('127.0.0.1:')) {
headers['Origin'] = 'tauri://localhost'
}

// Only add authentication headers if API key is provided
if (provider.api_key) {
headers['x-api-key'] = provider.api_key
Expand Down
Loading