From 1ad1f46ab3f0957750b1331aff7f94049f4e23ae Mon Sep 17 00:00:00 2001 From: shanyuhai123 <864299347@qq.com> Date: Thu, 12 Nov 2026 18:25:30 +0800 Subject: [PATCH] =?UTF-8?q?feat(buddy):=20=E6=94=AF=E6=8C=81=E6=8F=92?= =?UTF-8?q?=E4=BB=B6=E6=A8=A1=E5=9E=8B=E8=83=BD=E5=8A=9B=E5=A3=B0=E6=98=8E?= =?UTF-8?q?=E4=B8=8E=E7=BB=9F=E4=B8=80=E8=A7=A3=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../platform/extensions/ExtensionInference.ts | 43 ++++++++-- .../platform/extensions/ExtensionService.ts | 2 +- .../__tests__/ExtensionService.spec.ts | 36 +++++++++ .../__tests__/InferenceObservation.spec.ts | 79 ++++++++++++++++++- .../skills/plugin-creator/references/api.d.ts | 15 +++- .../plugin-creator/references/inference.md | 10 ++- .../src/providers/PluginModelRegistry.ts | 7 +- .../src/providers/ProviderCatalogMatcher.ts | 34 +++++--- .../src/providers/ProviderModelCatalog.ts | 31 ++++---- .../src/providers/ProviderModelDiscovery.ts | 2 + .../service/src/providers/ProviderService.ts | 8 +- .../__tests__/PluginModelProvider.spec.ts | 20 +++++ .../__tests__/PluginModelRegistry.spec.ts | 62 +++++++++++++-- .../__tests__/ProviderCatalogMatcher.spec.ts | 18 +++++ .../__tests__/ProviderModelDiscovery.spec.ts | 6 +- .../__tests__/ProviderServiceMetadata.spec.ts | 13 +-- .../src/providers/createPluginProvider.ts | 32 +++++++- .../shared/diagnostics/inferenceDiagnostic.ts | 3 + .../shared/extensions/extensionInference.ts | 4 + .../extensions/extensionInferenceClient.ts | 2 +- .../shared/providers/providerDiscovery.ts | 3 +- 21 files changed, 363 insertions(+), 67 deletions(-) diff --git a/apps/buddy/platform/extensions/ExtensionInference.ts b/apps/buddy/platform/extensions/ExtensionInference.ts index 66c56b737..ca5f49917 100644 --- a/apps/buddy/platform/extensions/ExtensionInference.ts +++ b/apps/buddy/platform/extensions/ExtensionInference.ts @@ -1,4 +1,5 @@ import type { InferenceBinding, InferenceCapability, InferenceHardware, InferenceObservation, InferenceRuntimeReference } from '../../shared/extensions/extensionInference' +import type { DiscoveredModelMetadata } from '../../shared/providers/providerDiscovery' import type { ExtensionInferenceCredentials } from './ExtensionInferenceCredentials' import type { InferenceResourceLease, InferenceResourceSource } from './ExtensionInferenceResources' import type { ExtensionPackage, ExtensionPackageStore } from './ExtensionPackageStore' @@ -38,6 +39,8 @@ interface RuntimeLifetime { } export class ExtensionInference { readonly #bindings = new Map() + readonly #modelMetadata = new WeakMap>() + readonly #modelsChanged: () => void readonly #instances = new Map() readonly #calls = new Set>() readonly #rebinding = new Set() @@ -52,12 +55,13 @@ export class ExtensionInference { readonly ports: ExtensionInferencePorts readonly source: InferenceResourceSource readonly assertCurrent: () => void - constructor(store: ExtensionPackageStore, pkg: ExtensionPackage, ports: ExtensionInferencePorts, source: InferenceResourceSource, assertCurrent: () => void) { + constructor(store: ExtensionPackageStore, pkg: ExtensionPackage, ports: ExtensionInferencePorts, source: InferenceResourceSource, assertCurrent: () => void, modelsChanged: () => void = () => {}) { this.store = store this.pkg = pkg this.ports = ports this.source = source this.assertCurrent = assertCurrent + this.#modelsChanged = modelsChanged } async prepareResources(...[files, options, signal, event]: Parameters>): Promise { @@ -155,7 +159,34 @@ export class ExtensionInference { } async models() { - return (await this.#availableBindings()).flatMap(binding => binding.models.map(model => ({ ...model, bindingRevision: createHash('sha256').update(JSON.stringify(binding)).digest('hex') }))) + return (await this.#availableBindings()).flatMap(binding => binding.models.map((model) => { + const declared = this.#modelMetadata.get(binding)?.get(model.id) + const metadata = model.metadata || declared ? { ...model.metadata, ...declared } : undefined + if (metadata?.contextWindow !== undefined && metadata.maxTokens !== undefined) + metadata.maxTokens = Math.min(metadata.maxTokens, metadata.contextWindow) + return { ...model, ...(metadata ? { metadata: structuredClone(metadata) } : {}), bindingRevision: createHash('sha256').update(JSON.stringify(binding)).digest('hex') } + })) + } + + #acceptModelMetadata(binding: InferenceBinding, observation: InferenceObservation): void { + const model = observation.modelId === undefined + ? binding.models.length === 1 ? binding.models[0] : undefined + : binding.models.find(model => model.id === observation.modelId) + if (!model || !observation.modelMetadata) + return + this.assertCurrent() + const models = this.#modelMetadata.get(binding) ?? new Map() + const previous = models.get(model.id) + const metadata = { ...previous } + for (const [key, value] of Object.entries(observation.modelMetadata)) { + if (value !== undefined) + Object.assign(metadata, { [key]: value }) + } + if (!Object.keys(metadata).length || JSON.stringify(previous) === JSON.stringify(metadata)) + return + models.set(model.id, metadata) + this.#modelMetadata.set(binding, models) + this.#modelsChanged() } async imageModels() { @@ -281,13 +312,13 @@ export class ExtensionInference { const leases: InferenceResourceLease[] = [] const observation: Instance['observation'] = {} const loading = AbortSignal.any([abort.signal, this.#stop.signal, AbortSignal.timeout(inferenceLimits.startTimeoutMs)]) - const ready = (async () => { + const ready = Promise.resolve().then(async () => { for (const resource of binding.resources) leases.push(await this.store.inference.acquire(this.pkg.manifest.id, resource, this.source, loading)) this.assertCurrent() return NativeInferenceProcess.start({ ...this.ports, record: (event) => { safeDiagnosticReporter(this.ports.record)(event) - if (abort.signal.aborted || event.component !== 'models.inference' || event.extensionId !== this.pkg.manifest.id || !inferenceStderrDiagnosticSchema.shape.event.safeParse(event.event).success) + if (abort.signal.aborted || this.#bindings.get(binding.runtime) !== binding || this.#instances.get(key)?.abort !== abort || event.component !== 'models.inference' || event.extensionId !== this.pkg.manifest.id || !inferenceStderrDiagnosticSchema.shape.event.safeParse(event.event).success) return const error = inferenceDiagnosticCodeSchema.safeParse(event.errorCode) if (event.event === 'chat.gpu.exit') { @@ -301,6 +332,8 @@ export class ExtensionInference { const parsed = inferenceObservationSchema.safeParse({ ...event.inference, ...(error.success ? { error: error.data } : {}), observedAt: Date.now() }) if (!parsed.success || Object.keys(parsed.data).length === 1) return + if (event.event === 'chat.gpu.ready') + this.#acceptModelMetadata(binding, parsed.data) const previous = observation.current observation.current = { ...previous, @@ -326,7 +359,7 @@ export class ExtensionInference { this.assertCurrent() return this.ports.request(endpoint.url, { method: 'POST', headers, body, signal }) } }, this.store, this.pkg, runtime, binding, leases, loading) - })() + }) const created: Instance = { abort, runtime: runtime.id, ready, observation, capacity: new ExtensionInvocationQueue({ concurrent: runtime.capacity, concurrentPerExtension: runtime.capacity, queued: 1024 }), waiters: 0, active: 0, leases } instance = created this.#instances.set(key, created) diff --git a/apps/buddy/platform/extensions/ExtensionService.ts b/apps/buddy/platform/extensions/ExtensionService.ts index 45e836839..ee36bf4d4 100644 --- a/apps/buddy/platform/extensions/ExtensionService.ts +++ b/apps/buddy/platform/extensions/ExtensionService.ts @@ -1961,7 +1961,7 @@ export class ExtensionService { running = { package: pkg, generation, abort, host, ready: Promise.resolve(), active: false, inFlight: 0, inferencePreparations: new Set(), drained: new Set() } if (pkg.manifest.contributes.inference && this.#ports.inference) { const owner = running - running.inference = new ExtensionInference(this.store, pkg, this.#ports.inference, this.#inferenceSource(owner), () => this.#assertCurrent(owner)) + running.inference = new ExtensionInference(this.store, pkg, this.#ports.inference, this.#inferenceSource(owner), () => this.#assertCurrent(owner), () => this.#publish({ kind: 'inference-models', extensionId: pkg.manifest.id })) } this.#running.set(dependency, running) const start = performance.now() diff --git a/apps/buddy/platform/extensions/__tests__/ExtensionService.spec.ts b/apps/buddy/platform/extensions/__tests__/ExtensionService.spec.ts index 8c9c2c944..983073136 100644 --- a/apps/buddy/platform/extensions/__tests__/ExtensionService.spec.ts +++ b/apps/buddy/platform/extensions/__tests__/ExtensionService.spec.ts @@ -911,6 +911,42 @@ it.each(['ready', 'checksum'] as const)('delivers resource progress before %s se expect(progress).toHaveLength(count) }) +it('publishes deployment metadata without replacing the active model binding or rejecting its original invocation identity', async () => { + const f = await fixture({ inference: { bootstrap: '', executable: '', admit: async () => async () => {} } }, manifest({ + id: 'fixture.inference', + apiVersion: 3, + permissions: { inference: true }, + contributes: { inference: { runtimes: [{ id: 'local', entry: 'native/fixture.mjs', platforms: ['linux-x64'], idleTimeoutMs: 60000 }], capabilities: [{ id: 'chat', title: 'Chat', runtime: 'local', kind: 'chat.completion' }] } }, + }), { 'native/fixture.mjs': 'export function initialize() {}' }) + await f.service.inferenceModels() + const host = f.hosts[0]! + const client = createExtensionInferenceClient(host.broker) + expect(client.capabilities.chatModelMetadata).toBe(true) + await client.bind({ runtime: 'local', resources: [], libraryPaths: [], endpoints: [], configuration: {}, imageModels: [], imageReaders: [], models: [{ id: 'model', title: 'Model', capability: 'chat', tools: 'auto', contextWindow: 8192, maxTokens: 512, metadata: { toolCall: true, reasoningOptions: ['off', 'medium'] } }] }) + const before = (await f.service.inferenceModels())[0]! + const native: ReturnType[] = [] + vi.spyOn(NativeInferenceProcess, 'start').mockImplementation(async (options) => { + const current = memoryInference({ record: options.record }) + native.push(current) + return current.worker + }) + cleanup.push(async () => { + for (const current of native) current.close() + await Promise.all(native.map(current => current.reading)) + }) + const invoke = () => f.service.invokeInference(before.extensionId, before.revision, invocation({ id: randomUUID(), capability: before.capability, generation: before.generation, bindingRevision: before.bindingRevision, input: { model: before.id } })) + await invoke() + const updates: unknown[] = [] + const subscription = f.service.onDidChange(change => updates.push(change)) + cleanup.push(async () => subscription.dispose()) + native[0]!.stderr.write(`${JSON.stringify({ event: 'chat.gpu.ready', modelId: 'model', modelMetadata: { toolCall: false } })}\n`) + expect(updates).toContainEqual(expect.objectContaining({ kind: 'inference-models', extensionId: before.extensionId })) + expect(await f.service.inferenceModels()).toEqual([{ ...before, metadata: { ...before.metadata, toolCall: false } }]) + await invoke() + expect(native).toHaveLength(1) + expect(host.disposed).toBe(false) +}) + it.each(['host', 'view'] as const)('keeps downloads, inference and original grants alive across concurrent private releases from %s', async (owner) => { const downloadStarted = deferred() const finishDownload = deferred() diff --git a/apps/buddy/platform/extensions/__tests__/InferenceObservation.spec.ts b/apps/buddy/platform/extensions/__tests__/InferenceObservation.spec.ts index 2fa30aa1c..f6aeeedf2 100644 --- a/apps/buddy/platform/extensions/__tests__/InferenceObservation.spec.ts +++ b/apps/buddy/platform/extensions/__tests__/InferenceObservation.spec.ts @@ -23,7 +23,10 @@ async function fixture(record?: ApplicationDiagnosticReporter) { ]))).token) const pkg = store.installed[owner]!.current const native: ReturnType[] = [] + const reports: ApplicationDiagnosticReporter[] = [] + const changes: unknown[] = [] vi.spyOn(NativeInferenceProcess, 'start').mockImplementation(async (options) => { + reports.push(options.record!) const current = memoryInference({ record: options.record }) native.push(current) return current.worker @@ -31,7 +34,7 @@ async function fixture(record?: ApplicationDiagnosticReporter) { const runtime = new ExtensionInference(store, pkg, { bootstrap: '', executable: '', admit: async () => async () => {}, record }, { response: async () => { throw new Error('Unexpected download') }, openReference: async () => { throw new Error('Unexpected resource') }, - }, () => {}) + }, () => {}, () => changes.push('models-changed')) onTestFinished(async () => { await runtime.dispose() await Promise.all(native.map(item => item.reading)) @@ -42,9 +45,81 @@ async function fixture(record?: ApplicationDiagnosticReporter) { const invoke = (id = 'first') => runtime.invoke(pkg.manifest.contributes.inference!.capabilities.find(item => item.id === id)!, invocation({ capability: id })) const observation = () => runtime.status()[0]!.instances[0]?.observation const emit = (data: unknown, index = 0) => native[index]!.stderr.write(`${JSON.stringify(data)}\n`) - return { runtime, native, invoke, observation, emit } + return { runtime, native, invoke, observation, emit, reports, changes } } +it('projects successful deployment declarations without changing bindings, and retains sparse fields until the binding changes', async () => { + const f = await fixture() + const binding = inferenceBindingSchema.parse({ runtime: 'first', resources: [], models: [{ + id: 'local', + title: 'Local', + capability: 'first', + tools: 'auto', + reasoning: 'auto', + contextWindow: 8192, + maxTokens: 512, + metadata: { toolCall: true, reasoningOptions: ['off', 'medium'], input: ['text', 'image'], contextWindow: 32768, maxTokens: 1024 }, + }] }) + await f.runtime.bind(binding) + const before = (await f.runtime.models())[0]! + await f.invoke() + f.emit({ event: 'chat.gpu.ready', context: { actual: 16384 }, modelMetadata: { toolCall: false, contextWindow: 4096 } }) + const expected = { ...before, metadata: { ...before.metadata, toolCall: false, contextWindow: 4096 } } + expect(await f.runtime.models()).toEqual([expected]) + expect(f.changes).toEqual(['models-changed']) + f.emit({ event: 'chat.gpu.ready', modelMetadata: { reasoningOptions: [] } }) + expected.metadata.reasoningOptions = [] + expect(await f.runtime.models()).toEqual([expected]) + expect(f.changes).toHaveLength(2) + f.emit({ event: 'chat.gpu.ready', modelMetadata: { reasoningOptions: [] } }) + f.emit({ event: 'chat.gpu.error', code: 'EXTENSION_INFERENCE_PROCESS_FAILED', modelMetadata: { toolCall: true } }) + f.emit({ event: 'chat.gpu.ready', modelMetadata: { toolCall: 'unknown' } }) + f.emit({ event: 'chat.gpu.ready', context: { actual: 65536, train: 262144 } }) + expect(await f.runtime.models()).toEqual([expected]) + expect(f.changes).toHaveLength(2) + expect(f.native).toHaveLength(1) + expect(binding.models[0]!.metadata?.toolCall).toBe(true) + expect((await f.runtime.models())[0]!.bindingRevision).toBe(before.bindingRevision) + await f.runtime.bind(binding) + await f.invoke() + expect(await f.runtime.models()).toEqual([expected]) + expect(f.native).toHaveLength(1) + await f.runtime.stop({ runtime: 'first' }) + expect(await f.runtime.models()).toEqual([expected]) + await f.invoke() + f.reports[0]!({ component: 'models.inference', extensionId: 'fixture.inference', event: 'chat.gpu.ready', level: 'info', inference: { modelMetadata: { toolCall: true } } }) + expect(await f.runtime.models()).toEqual([expected]) + f.emit({ event: 'chat.gpu.ready', modelId: 'local', modelMetadata: { input: ['text'] } }, 1) + expected.metadata.input = ['text'] + expect(await f.runtime.models()).toEqual([expected]) + await f.runtime.bind({ ...binding, configuration: { engine: 'replacement' } }) + const replaced = (await f.runtime.models())[0]! + expect(replaced.metadata).toEqual(before.metadata) + expect(replaced.bindingRevision).not.toBe(before.bindingRevision) + f.reports[1]!({ component: 'models.inference', extensionId: 'fixture.inference', event: 'chat.gpu.ready', level: 'info', inference: { modelId: 'local', modelMetadata: { toolCall: false } } }) + expect((await f.runtime.models())[0]).toEqual(replaced) + await f.invoke() + f.emit({ event: 'chat.gpu.ready', context: { actual: 4096 } }, 2) + expect((await f.runtime.models())[0]).toEqual(replaced) +}) + +it('requires an exact model ID for multi-model deployment declarations and never changes sibling capabilities', async () => { + const f = await fixture() + const binding = inferenceBindingSchema.parse({ runtime: 'first', resources: [], models: ['one', 'two'].map(id => ({ id, title: id, capability: 'first', contextWindow: 8192, maxTokens: 512, tools: false })) }) + await f.runtime.bind(binding) + const before = await f.runtime.models() + await f.invoke() + f.emit({ event: 'chat.gpu.ready', modelMetadata: { toolCall: true } }) + f.emit({ event: 'chat.gpu.ready', modelId: 'unknown', modelMetadata: { toolCall: true } }) + expect(await f.runtime.models()).toEqual(before) + expect(f.changes).toEqual([]) + f.emit({ event: 'chat.gpu.ready', modelId: 'two', modelMetadata: { toolCall: true } }) + expect(await f.runtime.models()).toEqual([before[0], { ...before[1], metadata: { toolCall: true } }]) + await f.runtime.unbind({ runtime: 'first' }) + await f.runtime.bind(binding) + expect(await f.runtime.models()).toEqual(before) +}) + it('projects validated incremental diagnostics per instance and forwards the original report', async () => { const records: unknown[] = [] const f = await fixture(event => records.push(event)) diff --git a/apps/buddy/service/resources/skills/plugin-creator/references/api.d.ts b/apps/buddy/service/resources/skills/plugin-creator/references/api.d.ts index 3d35b37eb..15728be5e 100644 --- a/apps/buddy/service/resources/skills/plugin-creator/references/api.d.ts +++ b/apps/buddy/service/resources/skills/plugin-creator/references/api.d.ts @@ -645,6 +645,8 @@ export interface InferenceHardware { export interface InferenceTiming { tokens: number, ms: number, tokensPerSecond?: number } export interface InferenceObservation { observedAt: number + modelId?: string + modelMetadata?: DiscoveredModelMetadata engine?: { name: 'llama.cpp', version?: string } backend?: { requested: 'cpu' | 'vulkan' | 'cuda' | 'metal' | 'unknown', observed: ('cpu' | 'vulkan' | 'cuda' | 'metal')[] } device?: { backend: 'cpu' | 'vulkan' | 'cuda' | 'metal', index: number }[] @@ -695,7 +697,16 @@ export interface InferenceResourceProgress { export interface InferenceResourceEvents { 'inference:resources:progress': { readonly id: string, readonly progress: InferenceResourceProgress } } -export interface InferenceModel { id: string, title: string, capability: string, contextWindow: number, maxTokens: number, tools: boolean | 'auto', reasoning?: boolean | 'auto', catalogModels?: { providerId: string, modelId: string }[], input?: ('text' | 'image')[] } +export interface DiscoveredModelMetadata { + type?: 'chat' | 'transcription' | 'speech' | 'image-generation' + name?: string + input?: ('text' | 'image' | 'pdf' | 'audio' | 'video')[] + toolCall?: boolean + reasoningOptions?: string[] + contextWindow?: number + maxTokens?: number +} +export interface InferenceModel { id: string, title: string, capability: string, contextWindow: number, maxTokens: number, tools: boolean | 'auto', reasoning?: boolean | 'auto', metadata?: DiscoveredModelMetadata, catalogModels?: { providerId: string, modelId: string }[], input?: ('text' | 'image')[] } export interface InferenceImageModel { id: string title: string @@ -719,7 +730,7 @@ export interface InferenceBinding { configuration?: Record } export interface InferenceApi { - readonly capabilities: { readonly nativeRuntime: true, readonly runtimeIsolation: true, readonly chatImageInput: true, readonly chatToolMatching: true, readonly chatReasoning: true, readonly resumableDownloads: true } + readonly capabilities: { readonly nativeRuntime: true, readonly runtimeIsolation: true, readonly chatImageInput: true, readonly chatToolMatching: true, readonly chatReasoning: true, readonly chatModelMetadata: true, readonly resumableDownloads: true } environment: () => Promise status: () => Promise<{ runtime: string, configured: boolean, platforms: string[], instances: { id: string | null, state: 'loading' | 'ready' | 'stopping' | 'stopped', active: number, pending: number, cancelling: number, observation?: InferenceObservation }[] }[]> stop: (input?: { runtime: string }) => Promise diff --git a/apps/buddy/service/resources/skills/plugin-creator/references/inference.md b/apps/buddy/service/resources/skills/plugin-creator/references/inference.md index aca5f7393..20ffe2c7a 100644 --- a/apps/buddy/service/resources/skills/plugin-creator/references/inference.md +++ b/apps/buddy/service/resources/skills/plugin-creator/references/inference.md @@ -82,7 +82,9 @@ models: [{ 对话会话的 `input` 为 `{ model, contentType: "application/json" }`。随后 `write(bytes)` 接收 UTF-8 JSON 分块,累计最多 32 MiB;`finish()` 时才解析完整的 `InferenceChatRequest`,类型见 [api.d.ts](api.d.ts)。请求包含完整消息、当前工具、最大输出、可选温度和可选 `reasoning`(`off / minimal / low / medium / high / xhigh / max`)。省略 `reasoning` 时保持后端默认;本地模板后端应把显式 `off` 映射到关闭思考的原生参数,不用提示词模拟。消息保留 system/user/assistant/tool 角色、工具调用 ID 与工具错误标志;适配后端时保留这些语义,包括 JSON Schema 的可选参数含义。纯文本 user/tool 消息继续使用 `text`;图文消息改用有序 `content`,与 `text` 二选一,包含 `{ type: 'text', text }` 和 `{ type: 'image', mimeType, data }`。图片只接受 PNG/JPEG/WebP/GIF 的 Base64 字节,不接受路径或远端 URL。工具结果图片同样保留其原始顺序、工具身份和错误状态。图片计入整个请求的 32 MiB 上限;文本模型收到图片必须明确拒绝,不隐式转录或换模型。 -`reasoning: true` 声明后端支持思考开关,`false` 或省略保持不支持;也可声明 `reasoning: 'auto'`,复用 `catalogModels` 精确匹配,只有全部来源明确支持时开启。使用前检查 `context.inference.capabilities.chatReasoning === true`,旧宿主不发送新增字段。当前声明是开关,宿主默认提供 `off / medium`,不把目录的云端思考强度直接套给本地后端。用户的能力覆盖和本次请求的思考选项分别保留;标题等短文本请求的 `off` 不改变模型的能力声明。开启思考时通过独立的 `{ type: 'thinking', text }` 事件输出,不能拼进正文;关闭或未请求思考时不发送该事件。 +`context.inference.capabilities.chatModelMetadata === true` 时,可在模型绑定中提供 `metadata`,复用统一的 `DiscoveredModelMetadata`:`toolCall`、`reasoningOptions`、`input`、`contextWindow`、`maxTokens` 等字段均可省略。只提交可核对的来源声明;缺字段表示未知,`false` 和空思考列表表示明确不支持。无需在线仓库身份或公共目录收录。`metadata` 按字段优先于旧 `tools` / `reasoning` / `input` 声明,缺字段才回退;声明的训练窗口不能扩大绑定的实际加载窗口和输出预算,媒体能力不能超出宿主协议。该声明表示模型与适配器能力,不是推理质量保证。 + +`reasoning: true` 声明后端支持思考开关,`false` 或省略保持旧版的不支持默认;也可声明 `reasoning: 'auto'`,复用 `catalogModels` 候选的一致能力。使用前检查 `context.inference.capabilities.chatReasoning === true`,旧宿主不发送新增字段。当前声明是开关,宿主默认提供 `off / medium`,不把目录的云端思考强度直接套给本地后端。用户的能力覆盖和本次请求的思考选项分别保留;标题等短文本请求的 `off` 不改变模型的能力声明。开启思考时通过独立的 `{ type: 'thinking', text }` 事件输出,不能拼进正文;关闭或未请求思考时不发送该事件。 输出不携带二进制,按顺序发送: @@ -94,9 +96,11 @@ call.emit({ type: 'done', reason: 'toolUse', usage: { input: 120, output: 18 } } 普通结束使用 `stop`,输出上限使用 `length`,产生工具调用使用 `toolUse`。每次必须且只能发送一个 `done`,随后结束会话;缺少终态不会认作成功。`usage` 使用后端实际 token 数,缓存读取/写入可用 `cacheRead`/`cacheWrite` 单列,`input` 不重复计入缓存。累计输出最多 16 MiB、最多 64 个工具调用;只能返回本次请求提供的工具名,ID 不得重复。 -`tools: false` 声明来源默认关闭工具调用,不影响已声明的图片输入;`tools: true` 声明来源默认开启,必须由后端结构化工具输出和实际任务往返验证。布尔声明保持原义,不被目录覆盖。后端能传递工具协议时,也可使用 `tools: 'auto'`,同时提供 `catalogModels: [{ providerId: 'huggingface', modelId: 'Qwen/Qwen3.5-9B' }]`。来源最多 8 条、不得重复,必须是可核对的显式来源 ID,不从显示名称或 GGUF 文件名推断。宿主使用已有模型目录精确查询,只有所有来源均明确支持工具时才启用;来源缺失、未知、未声明能力或相互冲突均关闭,不任选第一个。目录更新会重算来源默认能力,不改变插件模型 ID、绑定修订、资源、参数或用户启用状态。用户可在宿主「模型服务」中覆盖图片输入和工具能力,覆盖值独立保存并优先于插件声明与自动匹配;刷新、重启或插件更新不清空覆盖,恢复默认后重新跟随来源。插件不另存同一能力开关,也不把原始 `tools`/`input` 声明当作拒绝宿主请求的终态。后端实际不支持所请求协议时应明确报错,不丢弃图片或伪造工具调用。 +`tools: false` 声明来源默认关闭工具调用,不影响已声明的图片输入;`tools: true` 声明来源默认开启,必须由后端结构化工具输出和实际任务往返验证。布尔声明保持原义,不被目录覆盖。后端能传递工具协议时,也可使用 `tools: 'auto'`,同时提供 `catalogModels: [{ providerId: 'huggingface', modelId: 'Qwen/Qwen3.5-9B' }]`。来源最多 8 条、不得重复,必须是可核对的显式来源 ID,不从显示名称或 GGUF 文件名推断。宿主复用已有模型目录匹配和同级候选合并;未匹配的引用不构成反对证据,同级已知声明冲突的字段保持未决,不按第一个、数量或最大窗口选源。没有来源信息时才使用默认值;仓库名、标签和基座关系本身不是能力声明,不能无条件继承微调前的能力。目录更新会重算来源默认能力,不改变插件模型 ID、绑定修订、资源、参数或用户启用状态。用户可在宿主「模型服务」中覆盖图片输入和工具能力,覆盖值独立保存并优先于插件声明与自动匹配;刷新、重启或插件更新不清空覆盖,恢复默认后重新跟随来源。插件不另存同一能力开关,也不把原始 `tools`/`input` 声明当作拒绝宿主请求的终态。后端实际不支持所请求协议时应明确报错,不丢弃图片或伪造工具调用。 + +使用自动模式前检查 `context.inference.capabilities.chatToolMatching === true`;旧宿主应提示升级,不把字符串当作布尔启用。`tools: 'auto'` 不代表开启所有能力;输入声明优先使用 `metadata.input`,其次绑定 `input`,用户可显式覆盖,后端仍须实际支持;其他媒体不能超出宿主传输协议。能力覆盖不增加工具执行权限。工具交由宿主执行;插件不能自己执行任务工具或另建一份对话历史。宿主继续管理审批、用量、任务模型选择和取消。来自插件动作的模型调用复用外层调用的准入,不再次占用相同的插件队列名额。 -使用自动模式前检查 `context.inference.capabilities.chatToolMatching === true`;旧宿主应提示升级,不把字符串当作布尔启用。`tools: 'auto'` 只解析工具能力,不因目录声明自动开启图片、PDF、音频或视频输入;图片来源默认来自绑定 `input`,用户可显式覆盖,后端仍须实际支持;其他媒体不能超出宿主传输协议。能力覆盖不增加工具执行权限。工具交由宿主执行;插件不能自己执行任务工具或另建一份对话历史。宿主继续管理审批、用量、任务模型选择和取消。来自插件动作的模型调用复用外层调用的准入,不再次占用相同的插件队列名额。 +原生 llama.cpp 适配器可以在正常加载后的 `chat.gpu.ready` 诊断中报告 `modelMetadata` 和 `modelId`,由当前绑定接收增量部署声明,不需要重新绑定或重新加载。多模型绑定必须提供精确 `modelId`;单模型可省略。宿主保留同一绑定最后成功的字段,缺字段、读取失败和实例退出不制造否定声明;更换资源、模板或加载配置后旧部署声明失效。部署声明优先于绑定中的工件声明,用户显式覆盖仍优先,最终受协议和绑定预算限制。查询模型目录和状态本身不加载权重,也不发起模型问答。 ## 资源 diff --git a/apps/buddy/service/src/providers/PluginModelRegistry.ts b/apps/buddy/service/src/providers/PluginModelRegistry.ts index 923e0672b..f77a86e13 100644 --- a/apps/buddy/service/src/providers/PluginModelRegistry.ts +++ b/apps/buddy/service/src/providers/PluginModelRegistry.ts @@ -63,12 +63,11 @@ export class PluginModelRegistry { } for (const id of new Set([...this.providers.pluginProviderIds(), ...groups.keys()])) { const models = groups.get(id) - const toolCalling = new Map(models?.map(model => [model.id, this.providers.resolvePluginModelTools(model)])) - const reasoning = new Map(models?.map(model => [model.id, this.providers.resolvePluginModelReasoning(model)])) - const revision = JSON.stringify([models ?? [], [...toolCalling], [...reasoning]]) + const resolved = new Map(models?.map(model => [model.id, this.providers.resolvePluginModel(model)])) + const revision = JSON.stringify([models ?? [], [...resolved]]) if (revision === this.#revisions.get(id)) continue - const provider = models?.length ? createPluginProvider(models, (...args) => this.#invoke(...args), model => toolCalling.get(model.id) === true, model => reasoning.get(model.id) === true) : undefined + const provider = models?.length ? createPluginProvider(models, (...args) => this.#invoke(...args), model => resolved.get(model.id)!) : undefined await this.providers.applyPluginProvider(id, provider, models) this.#revisions.set(id, revision) } diff --git a/apps/buddy/service/src/providers/ProviderCatalogMatcher.ts b/apps/buddy/service/src/providers/ProviderCatalogMatcher.ts index bc1592085..4b285e6e3 100644 --- a/apps/buddy/service/src/providers/ProviderCatalogMatcher.ts +++ b/apps/buddy/service/src/providers/ProviderCatalogMatcher.ts @@ -1,7 +1,7 @@ import type { ModelCatalogReference } from '../../../shared/providers/providerCatalog' import type { CatalogModel, ModelMetadataCatalog } from './ModelsDevCatalog' import { serializeModelMetadata } from './modelMetadata' -import { modelCatalogFingerprint } from './modelResolution' +import { modelCatalogFingerprint, modelResolutionFingerprint } from './modelResolution' export type CatalogModelDefaults = Partial> @@ -57,7 +57,7 @@ export class ProviderCatalogMatcher { } getFingerprint(): string { - return this.#runtime.getFingerprint?.() ?? modelCatalogFingerprint(this.#runtime) + return modelResolutionFingerprint({ version: 2, catalog: this.#runtime.getFingerprint?.() ?? modelCatalogFingerprint(this.#runtime) }) } #index(): void { @@ -144,11 +144,14 @@ export class ProviderCatalogMatcher { const ranks = new Map(matches.map(match => [match.model, match.rank])) const highestRank = Math.max(...preferred.map(model => ranks.get(model)!)) preferred = preferred.filter(model => ranks.get(model) === highestRank) - const first = preferred[0]! - const fingerprint = catalogCapabilityFingerprint(first) - if (preferred.every(model => catalogCapabilityFingerprint(model) === fingerprint)) - return { candidates, model: first, status: 'matched' } - return { candidates, defaults: commonMetadata(preferred), status: 'ambiguous' } + return resolveCandidates(candidates, preferred) + } + + matchReferences(references: readonly ModelCatalogReference[]): ProviderCatalogMatch { + this.#index() + const candidates = [...new Set(references.flatMap(reference => this.find(reference) ?? []))] + .sort((left, right) => left.provider.localeCompare(right.provider) || left.id.localeCompare(right.id)) + return resolveCandidates(candidates) } find(reference: ModelCatalogReference): CatalogModel | undefined { @@ -157,6 +160,16 @@ export class ProviderCatalogMatcher { } } +function resolveCandidates(candidates: readonly CatalogModel[], preferred = candidates): ProviderCatalogMatch { + const first = preferred[0] + if (!first) + return { candidates, status: 'unmatched' } + const fingerprint = catalogCapabilityFingerprint(first) + if (preferred.every(model => catalogCapabilityFingerprint(model) === fingerprint)) + return { candidates, model: first, status: 'matched' } + return { candidates, defaults: commonMetadata(preferred), status: 'ambiguous' } +} + function catalogModelKey(value: string): string { const normalized = value.trim().toLowerCase() return normalized.match(/^[a-z0-9_-]+\/([^/]+)$/u)?.[1] ?? normalized @@ -188,8 +201,9 @@ function commonMetadata(models: readonly CatalogModel[]): CatalogModelDefaults { const common = (key: Key, value = first[key]) => models.every(model => serializeModelMetadata(model[key]) === serializeModelMetadata(value)) const defaults: CatalogModelDefaults = {} for (const key of ['contextWindow', 'maxTokens', 'pdfInput', 'audioInput', 'videoInput', 'toolCall'] as const) { - if (common(key)) - Object.assign(defaults, { [key]: first[key] }) + const values = models.map(model => model[key]).filter(value => value !== undefined) + if (values.length && values.every(value => value === values[0])) + Object.assign(defaults, { [key]: values[0] }) } if (models.every(model => serializeModelMetadata([...new Set(model.input)].sort()) === serializeModelMetadata([...new Set(first.input)].sort()))) defaults.input = [...first.input] @@ -212,7 +226,7 @@ function catalogCapabilityFingerprint(model: CatalogModel): string { pdfInput: model.pdfInput ?? false, audioInput: model.audioInput ?? false, videoInput: model.videoInput ?? false, - toolCall: model.toolCall !== false, + toolCall: model.toolCall, }) } diff --git a/apps/buddy/service/src/providers/ProviderModelCatalog.ts b/apps/buddy/service/src/providers/ProviderModelCatalog.ts index e538e9a64..8ac7df7b5 100644 --- a/apps/buddy/service/src/providers/ProviderModelCatalog.ts +++ b/apps/buddy/service/src/providers/ProviderModelCatalog.ts @@ -27,6 +27,7 @@ import { modelCapabilityOverridesSchema } from '../../../shared/providers/provid import { modelDiscoveryBaseUrl, serviceModelMetadataSchema } from '../../../shared/providers/providerDiscovery' import { providerModelInputSchema, storedCustomProviderModelSchema } from '../../../shared/providers/providerInput' import { providerModelTypeSchema } from '../../../shared/providers/providerModelType' +import { createPluginModel } from './createPluginProvider' import { applyModelCapabilities, getModelFileInputMimeTypes, readModelCapabilities, supportsModelAudioInput, supportsModelPdfInput, supportsModelVideoInput } from './modelCapabilities' import { serializeModelMetadata } from './modelMetadata' import { MODEL_RESOLUTION_VERSION, modelResolutionFingerprint } from './modelResolution' @@ -203,18 +204,10 @@ export class ProviderModelCatalog { } } - resolvePluginModelTools(model: Pick): boolean { - if (typeof model.tools === 'boolean') - return model.tools - return model.catalogModels?.length ? model.catalogModels.every(reference => this.#catalogMatcher.find(reference)?.toolCall === true) : false - } - - resolvePluginModelReasoning(model: Pick): boolean { - if (typeof model.reasoning === 'boolean') - return model.reasoning - if (model.reasoning !== 'auto' || !model.catalogModels?.length) - return false - return model.catalogModels.every(reference => this.#catalogMatcher.find(reference)?.reasoning === true) + resolvePluginModel(model: ExtensionModel): InputModel { + const match = this.#catalogMatcher.matchReferences(model.catalogModels ?? []) + const defaults = match.status === 'matched' ? match.model : match.status === 'ambiguous' ? match.defaults : undefined + return createPluginModel(model, defaults) } reconcilePluginModels(providerId: string, models: readonly Model[]): void { @@ -707,12 +700,22 @@ export class ProviderModelCatalog { pdfInput: supportsPdfInputApi(api) && (serviceInput ? serviceInput.includes('pdf') : metadata?.pdfInput === true && (sameEndpoint || codexPdf)), audioInput: serviceInput ? serviceInput.includes('audio') : metadata?.audioInput === true && sameEndpoint, videoInput: serviceInput ? serviceInput.includes('video') : metadata?.videoInput === true && sameEndpoint, - toolCall: metadata?.toolCall, + toolCall: serviceMetadata?.metadata.toolCall ?? metadata?.toolCall, } } #effectiveModel(model: ProviderModelStateRecord, sourceModel: InputModel): InputModel { - return applyModelCapabilities({ ...sourceModel, ...effectiveParameters(model) }, model.capabilityOverrides) + const effective = applyModelCapabilities({ ...sourceModel, ...effectiveParameters(model) }, model.capabilityOverrides) + if (effective.api !== pluginChatApi) + return effective + const contextWindow = Math.min(effective.contextWindow, sourceModel.contextWindow) + return { + ...effective, + contextWindow, + maxTokens: Math.min(effective.maxTokens, sourceModel.maxTokens, contextWindow), + reasoning: effective.reasoning && getSupportedThinkingLevels(effective).includes('medium'), + thinkingLevelMap: { ...effective.thinkingLevelMap, off: 'none', minimal: null, low: null, high: null, xhigh: null, max: null }, + } } #available(model: ProviderModelStateRecord): boolean { diff --git a/apps/buddy/service/src/providers/ProviderModelDiscovery.ts b/apps/buddy/service/src/providers/ProviderModelDiscovery.ts index 4c0a47d40..68a4dc846 100644 --- a/apps/buddy/service/src/providers/ProviderModelDiscovery.ts +++ b/apps/buddy/service/src/providers/ProviderModelDiscovery.ts @@ -177,6 +177,7 @@ const serviceModelMetadataSchema = z.object({ context_window: z.number().int().positive().optional(), max_tokens: z.number().int().positive().optional(), input_modalities: discoveredModelMetadataSchema.shape.input, + tool_call: discoveredModelMetadataSchema.shape.toolCall, supported_reasoning_levels: z.array(z.object({ effort: z.string().min(1).max(32).regex(/^[a-z0-9_-]+$/u), })).optional(), @@ -206,6 +207,7 @@ function parseServiceMetadata(value: Record): DiscoveredModelMe ...(model.supported_reasoning_levels ? { reasoningOptions: [...new Set(model.supported_reasoning_levels.map(level => level.effort === 'none' ? 'off' : level.effort))] } : {}), + ...(model.tool_call !== undefined ? { toolCall: model.tool_call } : {}), ...(model.context_window !== undefined ? { contextWindow: model.context_window } : {}), ...(model.max_tokens !== undefined ? { maxTokens: model.max_tokens } : {}), }) diff --git a/apps/buddy/service/src/providers/ProviderService.ts b/apps/buddy/service/src/providers/ProviderService.ts index 1069dfd70..0d2eae12b 100644 --- a/apps/buddy/service/src/providers/ProviderService.ts +++ b/apps/buddy/service/src/providers/ProviderService.ts @@ -241,12 +241,8 @@ export class ProviderService { }) } - resolvePluginModelTools(model: Pick): boolean { - return this.#modelCatalog.resolvePluginModelTools(model) - } - - resolvePluginModelReasoning(model: Pick): boolean { - return this.#modelCatalog.resolvePluginModelReasoning(model) + resolvePluginModel(model: ExtensionModel) { + return this.#modelCatalog.resolvePluginModel(model) } pluginProviderIds(): string[] { diff --git a/apps/buddy/service/src/providers/__tests__/PluginModelProvider.spec.ts b/apps/buddy/service/src/providers/__tests__/PluginModelProvider.spec.ts index e5008dfa4..c976e48db 100644 --- a/apps/buddy/service/src/providers/__tests__/PluginModelProvider.spec.ts +++ b/apps/buddy/service/src/providers/__tests__/PluginModelProvider.spec.ts @@ -44,6 +44,26 @@ async function fixture(invoke: Parameters[1], model } describe('plugin model provider', () => { + it('keeps local metadata identity, the loaded output budget and Pi disabled thinking at the plugin boundary', async () => { + let request: ExtensionChatRequest | undefined + let target: ExtensionModel | undefined + const bound: ExtensionModel = { ...descriptor, tools: false, metadata: { toolCall: true, reasoningOptions: ['off', 'medium'], contextWindow: 262144, maxTokens: 128 } } + const f = await fixture(async (current, input, _signal, emit) => { + target = current + request = input + emit({ type: 'text', text: 'Answer' }) + emit(done) + }, bound) + const response = await f.runtime.completeSimple(f.model, { messages: [ + { role: 'system', content: 'Use tools if needed.', toolsAdded: [{ name: 'read_note', description: 'Read a note', parameters: { type: 'object', properties: {} } }], timestamp: 0 }, + { role: 'user', content: 'Hello', timestamp: 1 }, + ] }, { maxTokens: 4096 }) + expect(response.stopReason).toBe('stop') + expect(f.model).toMatchObject({ id: bound.id, contextWindow: bound.contextWindow, maxTokens: 128, reasoning: true }) + expect(request).toMatchObject({ model: bound.id, maxTokens: 128, reasoning: 'off', tools: [{ name: 'read_note' }] }) + expect(target).toEqual(bound) + }) + it('preserves resolved instructions, current tools, streamed text and actual usage', async () => { let request: ExtensionChatRequest | undefined const f = await fixture(async (_target, input, _signal, emit) => { diff --git a/apps/buddy/service/src/providers/__tests__/PluginModelRegistry.spec.ts b/apps/buddy/service/src/providers/__tests__/PluginModelRegistry.spec.ts index 114b176ca..bd011fd0f 100644 --- a/apps/buddy/service/src/providers/__tests__/PluginModelRegistry.spec.ts +++ b/apps/buddy/service/src/providers/__tests__/PluginModelRegistry.spec.ts @@ -134,6 +134,56 @@ function catalogData(supported: boolean | undefined, reasoning?: boolean) { } describe('plugin model registry', () => { + it('merges plugin metadata by field without requiring HF identity and caps capabilities to the loaded backend', async () => { + const data = { vendor: catalogData(true, true).huggingface } + const f = await fixture(new ProviderModelSnapshotService({ builtin: { version: 1, updatedAt: '2026-01-01T00:00:00.000Z', data } })) + const reference = { providerId: 'vendor', modelId: qwen.modelId } + const cases: Array<{ model: ExtensionModel, tools: boolean, reasoning: boolean, contextWindow: number, maxTokens: number }> = [ + { model: { ...descriptor, id: 'local', reasoning: false, metadata: { toolCall: true, reasoningOptions: ['off', 'medium', 'high', 'future'], input: ['text'], contextWindow: 262144, maxTokens: 8192 } }, tools: true, reasoning: true, contextWindow: 8192, maxTokens: 512 }, + { model: { ...descriptor, id: 'explicit-false', tools: true, reasoning: true, catalogModels: [reference], metadata: { toolCall: false, reasoningOptions: [], input: ['text'] } }, tools: false, reasoning: false, contextWindow: 8192, maxTokens: 512 }, + { model: { ...descriptor, id: 'sparse', tools: 'auto', reasoning: 'auto', catalogModels: [reference], metadata: { input: ['text'] } }, tools: true, reasoning: true, contextWindow: 8192, maxTokens: 512 }, + { model: { ...descriptor, id: 'unsupported-effort', metadata: { input: ['text'], reasoningOptions: ['high'] } }, tools: false, reasoning: false, contextWindow: 8192, maxTokens: 512 }, + { model: { ...descriptor, id: 'smaller-budget', metadata: { input: ['text'], contextWindow: 4096, maxTokens: 128 } }, tools: false, reasoning: false, contextWindow: 4096, maxTokens: 128 }, + { model: { ...descriptor, id: 'unsupported-inputs', metadata: { input: ['text', 'pdf', 'audio', 'video'] } }, tools: false, reasoning: false, contextWindow: 8192, maxTokens: 512 }, + ] + f.setModels(cases.map(item => item.model)) + await f.registry.refresh() + const listed = await f.service.listModels(providerId) + for (const { model, tools, reasoning, contextWindow, maxTokens } of cases) { + const effective = await f.service.executionModels.resolveAvailable({ ...selection, modelId: model.id }) + expect(effective).toMatchObject({ id: model.id, provider: providerId, toolCall: tools, reasoning, input: ['text'], contextWindow, maxTokens }) + expect(listed.find(item => item.id === model.id)).toMatchObject({ toolCalling: tools, capabilities: reasoning ? ['text', 'reasoning'] : ['text'], reasoningOptions: reasoning ? ['off', 'medium'] : [], contextWindow, maxTokens, sourceCapabilities: { toolCall: tools, image: false, pdf: false, audio: false, video: false } }) + } + expect(await f.inference.models()).toEqual(cases.map(item => item.model)) + await expect(f.service.executionModels.resolveAvailable({ ...selection, modelId: 'local', contextWindow: 262144, maxTokens: 8192 })).rejects.toMatchObject({ code: 'PROVIDER_UNAVAILABLE' }) + }) + + it('preserves metadata bindings, sparse overrides and an off selection across rebinding, failure and restart', async () => { + const f = await fixture() + const bound: ExtensionModel = { ...descriptor, metadata: { toolCall: true, reasoningOptions: ['off', 'medium'], input: ['text'] } } + f.setModels([bound]) + await f.registry.refresh() + const off = { ...selected, reasoning: 'off' as const } + await f.service.setDefaultModel(off) + const overrides = { toolCall: false, reasoningOptions: ['off', 'medium', 'high'] as ('off' | 'medium' | 'high')[] } + await f.service.setModelCapabilities(providerId, descriptor.id, overrides) + const updated: ExtensionModel = { ...bound, bindingRevision: 'c'.repeat(64), metadata: { toolCall: false, reasoningOptions: [], input: ['text', 'image'] } } + f.setModels([updated]) + await f.registry.refresh() + const restarted = await f.restart() + await restarted.registry.refresh() + expect(await restarted.service.listModels(providerId)).toMatchObject([{ capabilityOverrides: overrides, toolCalling: false, capabilities: ['text', 'image', 'reasoning'], reasoningOptions: ['off', 'medium'], sourceCapabilities: { toolCall: false, reasoningOptions: ['off'] } }]) + expect(await restarted.service.executionModels.resolveAvailable(selection)).toMatchObject({ toolCall: false, reasoning: true, thinkingLevelMap: { low: null, high: null }, contextWindow: descriptor.contextWindow, maxTokens: descriptor.maxTokens }) + expect(await restarted.service.getDefaultModel()).toEqual(off) + expect(await f.inference.models()).toEqual([updated]) + f.failListing() + await expect(restarted.registry.refresh()).rejects.toThrow() + expect(await restarted.service.listModels(providerId)).toMatchObject([{ capabilityOverrides: overrides, reasoningOptions: ['off', 'medium'] }]) + await restarted.service.setModelCapabilities(providerId, descriptor.id, { toolCall: true }) + expect(await restarted.service.executionModels.resolveAvailable(selection)).toMatchObject({ toolCall: true, reasoning: false }) + expect(await restarted.service.getDefaultModel()).toEqual(off) + }) + it('uses exact bundled Qwen sources without interpreting GGUF names or overriding boolean declarations', async () => { const f = await fixture(new ProviderModelSnapshotService()) const cases = [ @@ -141,8 +191,8 @@ describe('plugin model registry', () => { { id: 'base', tools: 'auto', catalogModels: [base], expected: false }, { id: 'qwen3.5-9b-gguf', tools: 'auto', expected: false }, { id: 'unknown', tools: 'auto', catalogModels: [{ ...qwen, modelId: 'Qwen/Qwen3.5-9B-GGUF' }], expected: false }, - { id: 'mixed', tools: 'auto', catalogModels: [qwen, base], expected: false }, - { id: 'unknown-mixed', tools: 'auto', catalogModels: [qwen, { ...qwen, modelId: 'unknown' }], expected: false }, + { id: 'mixed', tools: 'auto', catalogModels: [qwen, base], expected: true }, + { id: 'unknown-mixed', tools: 'auto', catalogModels: [qwen, { ...qwen, modelId: 'unknown' }], expected: true }, { id: 'explicit-off', tools: false, catalogModels: [qwen], expected: false }, { id: 'legacy-on', tools: true, expected: true }, { id: 'legacy-off', tools: false, expected: false }, @@ -152,12 +202,12 @@ describe('plugin model registry', () => { await f.registry.refresh() const models = await f.service.listModels(providerId) for (const model of cases) { - expect(models.find(candidate => candidate.id === model.id)).toMatchObject({ toolCalling: model.expected, capabilities: ['text', 'image'], contextWindow: descriptor.contextWindow, maxTokens: descriptor.maxTokens }) + expect(models.find(candidate => candidate.id === model.id), model.id).toMatchObject({ toolCalling: model.expected, capabilities: ['text', 'image'], contextWindow: descriptor.contextWindow, maxTokens: descriptor.maxTokens }) expect(await f.service.executionModels.resolveAvailable({ ...selection, modelId: model.id })).toMatchObject({ id: model.id, provider: providerId, toolCall: model.expected }) } }) - it('resolves reasoning from explicit declarations or unanimous exact catalog references without opting legacy models in', async () => { + it('resolves reasoning from explicit declarations or consistent known catalog references without opting legacy models in', async () => { const data = catalogData(true, true) const alias = { ...qwen, modelId: 'Qwen/Qwen3.5-9B-Alias' } data.huggingface.models[alias.modelId] = { ...data.huggingface.models[qwen.modelId]! } @@ -171,7 +221,7 @@ describe('plugin model registry', () => { { id: 'unanimous', reasoning: 'auto', catalogModels: [qwen, alias], expected: true }, { id: 'conflict', reasoning: 'auto', catalogModels: [qwen, base], expected: false }, { id: 'unknown', reasoning: 'auto', catalogModels: [{ ...qwen, modelId: 'unknown' }], expected: false }, - { id: 'unknown-mixed', reasoning: 'auto', catalogModels: [qwen, { ...qwen, modelId: 'unknown' }], expected: false }, + { id: 'unknown-mixed', reasoning: 'auto', catalogModels: [qwen, { ...qwen, modelId: 'unknown' }], expected: true }, { id: 'wrong-provider', reasoning: 'auto', catalogModels: [{ ...qwen, providerId: 'unknown' }], expected: false }, { id: 'qwen3.5-9b-gguf', reasoning: 'auto', expected: false }, { id: 'legacy', expected: false }, @@ -189,7 +239,7 @@ describe('plugin model registry', () => { expect(runtime.thinkingLevelMap).toMatchObject({ minimal: null, low: null, high: null, xhigh: null, max: null }) expect(f.repository.models.find(providerId, id)).toMatchObject({ reasoning: expected, thinkingLevelMap: runtime.thinkingLevelMap ?? null }) } - expect(f.service.resolvePluginModelReasoning({ reasoning: 'auto', catalogModels: [] })).toBe(false) + expect(f.service.resolvePluginModel({ ...descriptor, reasoning: 'auto', catalogModels: [] }).reasoning).toBe(false) }) it('re-registers reasoning-only catalog changes and persists source maps without losing overrides or active selections', async () => { diff --git a/apps/buddy/service/src/providers/__tests__/ProviderCatalogMatcher.spec.ts b/apps/buddy/service/src/providers/__tests__/ProviderCatalogMatcher.spec.ts index 9303a0863..02edf81a2 100644 --- a/apps/buddy/service/src/providers/__tests__/ProviderCatalogMatcher.spec.ts +++ b/apps/buddy/service/src/providers/__tests__/ProviderCatalogMatcher.spec.ts @@ -4,6 +4,24 @@ import { ModelsDevCatalog } from '../ModelsDevCatalog' import { ProviderCatalogMatcher } from '../ProviderCatalogMatcher' describe('providerCatalogMatcher', () => { + it('merges exact reference candidates by field while distinguishing unknown support from explicit conflicts', () => { + const first = { ...model('vendor', 'instruct', 'https://vendor.test'), toolCall: true } + const unknown = { ...first, id: 'unknown', toolCall: undefined } + const conflicting = { ...first, id: 'base', toolCall: false } + const matcher = new ProviderCatalogMatcher(catalogRuntime([first, unknown, conflicting])) + const references = (ids: string[]) => ids.map(modelId => ({ providerId: 'vendor', modelId })) + expect(matcher.matchReferences(references(['missing']))).toEqual({ status: 'unmatched', candidates: [] }) + expect(matcher.matchReferences(references(['missing', 'instruct']))).toMatchObject({ status: 'matched', model: { toolCall: true } }) + for (const ids of [['unknown', 'instruct'], ['instruct', 'unknown']]) + expect(matcher.matchReferences(references(ids))).toMatchObject({ status: 'ambiguous', defaults: { toolCall: true, contextWindow: first.contextWindow } }) + const conflict = matcher.matchReferences(references(['instruct', 'base'])) + expect(conflict).toMatchObject({ status: 'ambiguous', defaults: { contextWindow: first.contextWindow } }) + if (conflict.status === 'ambiguous') + expect(conflict.defaults.toolCall).toBeUndefined() + expect(matcher.matchReferences(references(['unknown']))).toMatchObject({ status: 'matched', model: { toolCall: undefined } }) + expect(matcher.matchReferences(references(['base']))).toMatchObject({ status: 'matched', model: { toolCall: false } }) + }) + it('reuses content-identical catalogs and replaces indexed candidates when metadata changes', () => { const raw = (context: number) => ({ vendor: { name: 'Vendor', api: 'https://vendor.example.test', models: { shared: { name: 'Shared', modalities: { input: ['text'], output: ['text'] }, limit: { context, output: 4096 } }, diff --git a/apps/buddy/service/src/providers/__tests__/ProviderModelDiscovery.spec.ts b/apps/buddy/service/src/providers/__tests__/ProviderModelDiscovery.spec.ts index d2de0683e..5ae9e3952 100644 --- a/apps/buddy/service/src/providers/__tests__/ProviderModelDiscovery.spec.ts +++ b/apps/buddy/service/src/providers/__tests__/ProviderModelDiscovery.spec.ts @@ -57,6 +57,7 @@ describe('providerModelDiscovery', () => { context_window: 200_000 + index, max_tokens: 32_000 + index, input_modalities: ['text', 'image'], + tool_call: index === 0, supported_reasoning_levels: [{ effort: 'low' }, { effort: 'high' }], })) const discovery = new OpenAiCompatibleModelDiscovery({ @@ -68,7 +69,7 @@ describe('providerModelDiscovery', () => { models: models.map(model => ({ id: model.slug, name: model.display_name, - metadata: { name: model.display_name, contextWindow: model.context_window, maxTokens: model.max_tokens, input: model.input_modalities, reasoningOptions: ['low', 'high'] }, + metadata: { name: model.display_name, contextWindow: model.context_window, maxTokens: model.max_tokens, input: model.input_modalities, toolCall: model.tool_call, reasoningOptions: ['low', 'high'] }, })), }) }) @@ -81,6 +82,7 @@ describe('providerModelDiscovery', () => { input_modalities: ['text', 'image', 'image'], context_window: 128_000, max_tokens: 8_000, + tool_call: false, supported_reasoning_levels: [{ effort: 'none' }, { effort: 'high' }, { effort: 'ultra' }], base_instructions: 'untrusted', headers: { authorization: 'untrusted' }, @@ -90,7 +92,7 @@ describe('providerModelDiscovery', () => { complete: true, models: [{ id: 'gemini-3.8-flash-high', - metadata: { input: ['text', 'image'], contextWindow: 128_000, maxTokens: 8_000, reasoningOptions: ['off', 'high', 'ultra'] }, + metadata: { input: ['text', 'image'], contextWindow: 128_000, maxTokens: 8_000, toolCall: false, reasoningOptions: ['off', 'high', 'ultra'] }, }], }) }) diff --git a/apps/buddy/service/src/providers/__tests__/ProviderServiceMetadata.spec.ts b/apps/buddy/service/src/providers/__tests__/ProviderServiceMetadata.spec.ts index 9b785630c..8af6cf2bf 100644 --- a/apps/buddy/service/src/providers/__tests__/ProviderServiceMetadata.spec.ts +++ b/apps/buddy/service/src/providers/__tests__/ProviderServiceMetadata.spec.ts @@ -143,21 +143,22 @@ describe('service model metadata', () => { const f = await fixture() const id = 'gemini-3.8-flash-high' const catalog = { source: { providerId: 'google', modelId: 'gemini-3.8-flash' } } - f.setBody({ data: [{ id, input_modalities: ['text'], supported_reasoning_levels: [], context_window: 64_000, max_tokens: 8_000 }] }) - const expected = { id, catalogMatch: 'matched', capabilities: ['text'], reasoningOptions: [], contextWindow: 64_000, maxTokens: 8_000, catalog } + f.setBody({ data: [{ id, input_modalities: ['text'], tool_call: false, supported_reasoning_levels: [], context_window: 64_000, max_tokens: 8_000 }] }) + const expected = { id, catalogMatch: 'matched', toolCalling: false, sourceCapabilities: { toolCall: false }, capabilities: ['text'], reasoningOptions: [], contextWindow: 64_000, maxTokens: 8_000, catalog } expect((await f.service.syncModels(connection.id))[0]).toMatchObject(expected) await f.service.refreshModelSnapshot() await f.service.dispose() const { service } = await f.setup() expect((await service.listModels(connection.id))[0]).toMatchObject(expected) - await service.setModelCapabilities(connection.id, id, { image: true, reasoningOptions: ['high'] }) + await service.setModelCapabilities(connection.id, id, { image: true, toolCall: true, reasoningOptions: ['high'] }) await service.refreshModelSnapshot() - expect((await service.listModels(connection.id))[0]).toMatchObject({ capabilities: ['text', 'image', 'reasoning'], contextWindow: 64_000, maxTokens: 8_000, catalog }) + expect((await service.listModels(connection.id))[0]).toMatchObject({ toolCalling: true, sourceCapabilities: { toolCall: false }, capabilities: ['text', 'image', 'reasoning'], contextWindow: 64_000, maxTokens: 8_000, catalog }) + expect(service.executionModels.resolve({ providerId: connection.id, modelId: id, contextWindow: null, maxTokens: null })).toMatchObject({ toolCall: true }) await service.setModelCapabilities(connection.id, id, null) - expect((await service.listModels(connection.id))[0]?.capabilities).toEqual(['text']) + expect((await service.listModels(connection.id))[0]).toMatchObject({ toolCalling: false, capabilities: ['text'] }) f.setBody({ data: [{ id }] }) await service.syncModels(connection.id) - expect((await service.listModels(connection.id))[0]).toMatchObject({ contextWindow: 1_048_576, maxTokens: 65_536, catalog }) + expect((await service.listModels(connection.id))[0]).toMatchObject({ toolCalling: true, contextWindow: 1_048_576, maxTokens: 65_536, catalog }) }) it('preserves omitted states, overrides, manual models and defaults through partial pages, restart and recovery', async () => { diff --git a/apps/buddy/service/src/providers/createPluginProvider.ts b/apps/buddy/service/src/providers/createPluginProvider.ts index 496aa3a4e..1cb11810e 100644 --- a/apps/buddy/service/src/providers/createPluginProvider.ts +++ b/apps/buddy/service/src/providers/createPluginProvider.ts @@ -1,13 +1,37 @@ import type { AssistantMessage, ImageContent, ProviderStreams, TextContent } from '@earendil-works/pi-ai' import type { ExtensionChatCapabilities, ExtensionChatEvent, ExtensionChatRequest, ExtensionModel } from '../../../shared/extensions/extensionChat' import type { InputModel } from './modelCapabilities' +import type { CatalogModelDefaults } from './ProviderCatalogMatcher' import { Buffer } from 'node:buffer' import { createAssistantMessageEventStream, createProvider, getCurrentTools, getSystemMessageText, normalizeContext, resolveTranscript } from '@earendil-works/pi-ai' import { extensionChatMaxBytes, extensionChatRequestSchema, pluginChatApi, pluginProviderId } from '../../../shared/extensions/extensionChat' const cost = { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 } -export function createPluginProvider(models: ExtensionModel[], invoke: (target: ExtensionModel, input: ExtensionChatRequest, signal: AbortSignal, emit: (event: ExtensionChatEvent) => void, capabilities: ExtensionChatCapabilities) => Promise, resolveTools = (model: ExtensionModel) => model.tools === true, resolveReasoning = (model: ExtensionModel) => model.reasoning === true) { - const toolCalling = new Map(models.map(model => [model.id, resolveTools(model)])) +export function createPluginModel(model: ExtensionModel, defaults: CatalogModelDefaults = {}): InputModel { + const declared = model.metadata + const reasoning = declared?.reasoningOptions !== undefined + ? declared.reasoningOptions.includes('medium') + : typeof model.reasoning === 'boolean' ? model.reasoning : model.reasoning === 'auto' && defaults.reasoning === true + const contextWindow = Math.min(model.contextWindow, declared?.contextWindow ?? model.contextWindow) + const input = (declared?.input ?? model.input ?? defaults.input ?? ['text']) + .filter((value): value is 'text' | 'image' => value === 'text' || value === 'image') + return { + id: model.id, + name: declared?.name ?? model.title, + provider: pluginProviderId(model.extensionId), + api: pluginChatApi, + baseUrl: '', + input: [...new Set(['text' as const, ...input])], + reasoning, + thinkingLevelMap: { off: 'none', minimal: null, low: null, high: null, xhigh: null, max: null }, + contextWindow, + maxTokens: Math.min(model.maxTokens, declared?.maxTokens ?? model.maxTokens, contextWindow), + toolCall: declared?.toolCall ?? (typeof model.tools === 'boolean' ? model.tools : defaults.toolCall) ?? false, + cost, + } +} + +export function createPluginProvider(models: ExtensionModel[], invoke: (target: ExtensionModel, input: ExtensionChatRequest, signal: AbortSignal, emit: (event: ExtensionChatEvent) => void, capabilities: ExtensionChatCapabilities) => Promise, resolveModel = createPluginModel) { const first = models[0]! const providerId = pluginProviderId(first.extensionId) const stream: ProviderStreams['streamSimple'] = (model, context, options) => { @@ -55,7 +79,7 @@ export function createPluginProvider(models: ExtensionModel[], invoke: (target: return { role: 'tool', ...inputContent(entry.content), toolCallId: entry.toolCallId, name: entry.toolName, isError: entry.isError } }), tools: tools.map(tool => ({ name: tool.name, description: tool.description, parameters: tool.parameters })), - maxTokens: Math.min(options?.maxTokens ?? model.maxTokens, target.maxTokens), + maxTokens: Math.min(options?.maxTokens ?? model.maxTokens, model.maxTokens, target.maxTokens), ...(options?.temperature !== undefined ? { temperature: options.temperature } : {}), reasoning: options?.reasoning ?? 'off', }) @@ -147,7 +171,7 @@ export function createPluginProvider(models: ExtensionModel[], invoke: (target: id: providerId, name: first.providerName ?? first.extensionName, auth: { apiKey: { name: first.providerName ?? first.extensionName, resolve: async () => ({ auth: {}, source: 'plugin runtime' }) } }, - models: models.map(model => ({ id: model.id, name: model.title, provider: providerId, api: pluginChatApi, baseUrl: '', input: model.input ?? ['text'], reasoning: resolveReasoning(model), thinkingLevelMap: { minimal: null, low: null, high: null, xhigh: null, max: null }, contextWindow: model.contextWindow, maxTokens: model.maxTokens, toolCall: toolCalling.get(model.id) === true, cost })), + models: models.map(model => resolveModel(model)), api: { stream, streamSimple: stream }, }) } diff --git a/apps/buddy/shared/diagnostics/inferenceDiagnostic.ts b/apps/buddy/shared/diagnostics/inferenceDiagnostic.ts index 6b61f06f5..59e999b34 100644 --- a/apps/buddy/shared/diagnostics/inferenceDiagnostic.ts +++ b/apps/buddy/shared/diagnostics/inferenceDiagnostic.ts @@ -1,4 +1,5 @@ import { z } from 'zod' +import { discoveredModelMetadataSchema } from '../providers/providerDiscovery.ts' const count = z.number().int().nonnegative().max(Number.MAX_SAFE_INTEGER) const contextCount = z.number().int().positive().max(2147483647) @@ -8,6 +9,8 @@ const timing = z.object({ tokens: count, ms: duration, tokensPerSecond: duration export const inferenceDiagnosticCodeSchema = z.string().regex(/^EXTENSION_INFERENCE_[A-Z_]{1,80}$/) export const inferenceDiagnosticSchema = z.object({ + modelId: z.string().min(1).max(80).regex(/^[a-z][a-z0-9.-]*$/).optional(), + modelMetadata: discoveredModelMetadataSchema.optional(), engine: z.object({ name: z.literal('llama.cpp'), version: z.string().regex(/^b\d{1,12}$/).optional() }).strip().optional(), backend: z.object({ requested: z.enum(['cpu', 'vulkan', 'cuda', 'metal', 'unknown']), observed: z.array(backend).max(4) }).strip().optional(), device: z.array(z.object({ backend, index: count }).strip()).max(16).optional(), diff --git a/apps/buddy/shared/extensions/extensionInference.ts b/apps/buddy/shared/extensions/extensionInference.ts index ad9f79d60..a4b8e7675 100644 --- a/apps/buddy/shared/extensions/extensionInference.ts +++ b/apps/buddy/shared/extensions/extensionInference.ts @@ -2,6 +2,7 @@ import { z } from 'zod' import { inferenceDiagnosticSchema } from '../diagnostics/inferenceDiagnostic.ts' import { imageReadingSupportSchema, imageSupportSchema } from '../images/imagePreferences.ts' import { modelCatalogReferenceSchema } from '../providers/providerCatalog.ts' +import { discoveredModelMetadataSchema } from '../providers/providerDiscovery.ts' import { extensionPathSchema } from './extensionPath.ts' export const inferencePlatformSchema = z.enum(['linux-x64', 'windows-x64', 'macos-arm64']) @@ -27,6 +28,8 @@ export const inferenceHardwareSchema = z.object({ }).strip() export type InferenceHardware = z.infer export const inferenceObservationSchema = inferenceDiagnosticSchema.pick({ + modelId: true, + modelMetadata: true, engine: true, backend: true, device: true, @@ -156,6 +159,7 @@ export const inferenceModelSchema = z.object({ contextWindow: z.number().int().min(256).max(2_000_000), maxTokens: z.number().int().positive(), tools: z.union([z.boolean(), z.literal('auto')]), + metadata: discoveredModelMetadataSchema.optional(), reasoning: z.union([z.boolean(), z.literal('auto')]).optional(), catalogModels: z.array(modelCatalogReferenceSchema).max(8).refine(value => new Set(value.map(model => JSON.stringify([model.providerId, model.modelId]))).size === value.length, 'Duplicate catalog model').optional(), input: z.array(z.enum(['text', 'image'])).min(1).max(2).refine(value => value.includes('text') && new Set(value).size === value.length, 'Expected unique inputs including text').optional(), diff --git a/apps/buddy/shared/extensions/extensionInferenceClient.ts b/apps/buddy/shared/extensions/extensionInferenceClient.ts index 6bce9dc75..2e7b5fc13 100644 --- a/apps/buddy/shared/extensions/extensionInferenceClient.ts +++ b/apps/buddy/shared/extensions/extensionInferenceClient.ts @@ -5,7 +5,7 @@ import type { InferenceBinding, InferenceCredential, InferenceCredentialReferenc export function createExtensionInferenceClient(request: (method: string, params?: JsonValue) => Promise, events?: EventSubscriber) { return Object.freeze({ - capabilities: Object.freeze({ nativeRuntime: true as const, runtimeIsolation: true as const, chatImageInput: true as const, chatToolMatching: true as const, chatReasoning: true as const, resumableDownloads: true as const }), + capabilities: Object.freeze({ nativeRuntime: true as const, runtimeIsolation: true as const, chatImageInput: true as const, chatToolMatching: true as const, chatReasoning: true as const, chatModelMetadata: true as const, resumableDownloads: true as const }), environment: async (): Promise => await request('inference.environment') as InferenceEnvironment, status: () => request('inference.status'), stop: async (input?: InferenceRuntimeReference): Promise => { await request('inference.stop', input) }, diff --git a/apps/buddy/shared/providers/providerDiscovery.ts b/apps/buddy/shared/providers/providerDiscovery.ts index b1c0ffc64..b223fd08e 100644 --- a/apps/buddy/shared/providers/providerDiscovery.ts +++ b/apps/buddy/shared/providers/providerDiscovery.ts @@ -1,10 +1,11 @@ import { z } from 'zod' -import { providerModelTypeSchema } from './providerModelType' +import { providerModelTypeSchema } from './providerModelType.ts' export const discoveredModelMetadataSchema = z.object({ type: providerModelTypeSchema.optional(), name: z.string().trim().min(1).max(200).optional(), input: z.array(z.enum(['text', 'image', 'pdf', 'audio', 'video'])).min(1).optional(), + toolCall: z.boolean().optional(), reasoningOptions: z.array(z.string().min(1).max(32).regex(/^[a-z0-9_-]+$/u)).optional(), contextWindow: z.number().int().positive().optional(), maxTokens: z.number().int().positive().optional(),