From cc668708a476ab01f4e386f6d2d2df84684a53bf Mon Sep 17 00:00:00 2001 From: SoyWater Date: Mon, 7 Sep 2026 17:18:18 +0800 Subject: [PATCH] fix: register 'codebuddy-sdk' in pi-ai's apiProviderRegistry MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit pi.registerProvider alone only routes model-runtime-mediated streaming. Consumers that call pi-ai's raw stream/streamSimple (e.g. pi-observational-memory's observer/reflector/dropper agents) bypass the model runtime and failed with 'No API provider registered for api: codebuddy-sdk', crashing the whole process because pi-agent-core's agentLoop has no rejection handler. Registering the api id in pi-ai's registry — the same registry builtin apis live in — makes codebuddy models dispatchable from every entry point. Idempotent and a no-op on pi versions without the export. --- src/index.ts | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/src/index.ts b/src/index.ts index 4a8908d..b9b5632 100644 --- a/src/index.ts +++ b/src/index.ts @@ -27,6 +27,16 @@ const newAssistantMessageEventStream: () => AssistantMessageEventStream = ? _piAi.createAssistantMessageEventStream : () => new _piAi.AssistantMessageEventStream(); +// pi-ai ≥0.85 exports registerApiProvider. Registering "codebuddy-sdk" in +// pi-ai's own apiProviderRegistry makes the api id dispatchable by pi-ai's +// raw stream/streamSimple too — required for consumers that bypass the model +// runtime (e.g. pi-observational-memory's background agents, which call +// pi-agent-core agentLoop with pi-ai's raw streamSimple). pi.registerProvider +// alone only routes registry-mediated streaming; raw dispatch would otherwise +// throw "No API provider registered for api: codebuddy-sdk". +const registerApiProviderFn: ((provider: { api: string; stream: any; streamSimple: any }, sourceId?: string) => void) | undefined = + typeof _piAi.registerApiProvider === "function" ? _piAi.registerApiProvider.bind(piAi) : undefined; + // --- Debug logging --- // CODEBUDDY_SDK_DEBUG=1 enables local debug logs (metadata only; paths redacted; no prompt/tool bodies). @@ -1754,6 +1764,28 @@ export default async function (pi: ExtensionAPI) { // Cast: pi-ai AssistantMessageEventStream diamond dep between pi-coding-agent and pi-agent-core streamSimple: streamCodebuddySdk as any, }); + // Also register in pi-ai's apiProviderRegistry so raw pi-ai + // stream/streamSimple dispatch works for api:"codebuddy-sdk" models + // (see registerApiProviderFn comment above). Idempotent (Map.set). + if (registerApiProviderFn) { + try { + registerApiProviderFn( + { + api: "codebuddy-sdk", + // The SDK bridge implements the streamSimple contract; raw + // stream() callers get the same event shape rather than a crash. + stream: streamCodebuddySdk as any, + streamSimple: streamCodebuddySdk as any, + }, + "pi-codebuddy-sdk", + ); + debug("provider: registered pi-ai API provider for api 'codebuddy-sdk'"); + } catch (err) { + debug("provider: pi-ai API provider registration failed", err); + } + } else { + debug("provider: pi-ai registerApiProvider unavailable; raw streamSimple consumers cannot use codebuddy models"); + } } else { // Subsequent instance (subagent session): skip registration entirely. // The subagent already has access to codebuddy-sdk models via the shared