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
137 changes: 57 additions & 80 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,11 @@
"rather than in app.asar."
],
"devDependencies": {
"@ai-sdk/anthropic": "^2.0.105",
"@ai-sdk/google": "^2.0.99",
"@ai-sdk/openai": "^2.0.89",
"@ai-sdk/openai-compatible": "^1.0.31",
"@ai-sdk/provider": "^2.0.5",
"@ai-sdk/anthropic": "^3.0.125",
"@ai-sdk/google": "^3.0.129",
"@ai-sdk/openai": "^3.0.121",
"@ai-sdk/openai-compatible": "^2.0.80",
"@ai-sdk/provider": "^3.0.18",
"@dagrejs/dagre": "^1.1.8",
"@dnd-kit/core": "^6.3.1",
"@dnd-kit/sortable": "^10.0.0",
Expand Down Expand Up @@ -162,7 +162,7 @@
"@types/turndown": "^5.0.6",
"@vitejs/plugin-react": "^5.1.2",
"@xyflow/react": "^12.10.0",
"ai": "^5.0.123",
"ai": "^6.0.297",
"autoprefixer": "^10.4.23",
"cheerio": "^1.2.0",
"class-variance-authority": "^0.7.1",
Expand Down
19 changes: 16 additions & 3 deletions src/main/models/ModelClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ const toTokenUsage = (usage: LanguageModelUsage): ChatTokenUsage => ({
})

/**
* 将 APIMessage 转换为 AI SDK 的 CoreMessage 格式
* 将 APIMessage 转换为 AI SDK 的 ModelMessage 格式
*/
function convertToCoreMessages(messages: APIMessage[]) {
function toModelMessages(messages: APIMessage[]) {
return messages.map((msg) => ({
role: msg.role,
content: msg.content
Expand Down Expand Up @@ -90,6 +90,19 @@ export class ModelClient {
return this.adapter.chatProviderOptions?.(this.connection)
}

/**
* providerOptions for structured-output calls (streamObject).
*
* AI SDK 6 flipped the OpenAI-family `strictJsonSchema` default to true, and the
* schemas these services generate use `.optional()` fields, which strict mode
* rejects because every property has to be listed in `required`. This returns the
* protocol's pre-v6 setting so the request body is unchanged; protocols whose
* structured output is unaffected return undefined.
*/
structuredOutputProviderOptions(): SharedV2ProviderOptions | undefined {
return this.adapter.structuredOutputProviderOptions?.(this.connection)
}

/**
* 流式发送消息
*
Expand All @@ -106,7 +119,7 @@ export class ModelClient {

const result = streamText({
model: this.getAIModel(),
messages: convertToCoreMessages(messages),
messages: toModelMessages(messages),
temperature: DEFAULT_TEMPERATURE,
// Only when the connection asks for a ceiling. Absent means the model's own
// default, which is what a reasoning model needs: its thinking shares this
Expand Down
2 changes: 1 addition & 1 deletion src/main/models/protocols/googleGenerativeAi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const googleGenerativeAiAdapter: ProtocolAdapter = {
},

createEmbeddingModel(connection): EmbeddingModel {
return createProvider(connection).textEmbedding(connection.modelId)
return createProvider(connection).embedding(connection.modelId)
},

embeddingProviderOptions(_connection, dimensions) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/models/protocols/openaiCompletions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export const openaiCompletionsAdapter: ProtocolAdapter = {
},

createEmbeddingModel(connection): EmbeddingModel {
return createProvider(connection).textEmbeddingModel(connection.modelId)
return createProvider(connection).embeddingModel(connection.modelId)
},

embeddingProviderOptions(_connection, dimensions) {
Expand Down
10 changes: 9 additions & 1 deletion src/main/models/protocols/openaiResponses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const openaiResponsesAdapter: ProtocolAdapter = {
},

createEmbeddingModel(connection): EmbeddingModel {
return createProvider(connection).textEmbeddingModel(connection.modelId)
return createProvider(connection).embeddingModel(connection.modelId)
},

embeddingProviderOptions(_connection, dimensions) {
Expand All @@ -41,6 +41,14 @@ export const openaiResponsesAdapter: ProtocolAdapter = {
: undefined
},

/**
* AI SDK 6 把 `strictJsonSchema` 的默认值改为 true,而结构化输出服务用的 schema
* 带 `.optional()` 字段,strict 模式会拒绝。显式关掉,保持与 v5 相同的请求体。
*/
structuredOutputProviderOptions() {
return { [PROVIDER_OPTIONS_KEY]: { strictJsonSchema: false } }
},

supportsModelListing: true,

listModels: listOpenAICompatibleModels
Expand Down
10 changes: 10 additions & 0 deletions src/main/models/protocols/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,16 @@ export interface ProtocolAdapter {
*/
chatProviderOptions?(connection: ModelConnection): SharedV2ProviderOptions | undefined

/**
* 把结构化输出(streamObject)的 providerOptions 翻译成该协议的设置。
*
* AI SDK 6 把 OpenAI 系的 `strictJsonSchema` 默认值从 false 改成 true。本项目生成
* 的结构化输出 schema 大量使用 `.optional()`,而 strict 模式要求每个属性都出现在
* `required` 里,服务端会直接拒绝。显式回传 `strictJsonSchema: false` 是为了让线上
* 请求体与 v5 完全一致。结构化输出不受该默认值影响的协议返回 undefined。
*/
structuredOutputProviderOptions?(connection: ModelConnection): SharedV2ProviderOptions | undefined

/**
* 该协议是否支持从端点拉取模型列表(/v1/models 等)。
*/
Expand Down
3 changes: 2 additions & 1 deletion src/main/services/AnkiCardService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,8 @@ export class AnkiCardService {
const { partialObjectStream, object } = streamObject({
model: model,
schema: AnkiSchema,
prompt: prompt
prompt: prompt,
providerOptions: client.structuredOutputProviderOptions()
})

// 监听流式更新 (用于显示进度)
Expand Down
Loading
Loading