agents 0.32.0 → 0.34.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/dist/{agent-routing-vVMA7m8Y.d.ts → agent-routing-BSoZzNiP.d.ts} +537 -373
- package/dist/agent-routing.d.ts +1 -1
- package/dist/agent-tool-types.d.ts +1 -1
- package/dist/{agent-tools-gGXXkf1z.d.ts → agent-tools-Be-hpRpH.d.ts} +2 -2
- package/dist/agent-tools.d.ts +1 -1
- package/dist/{ai-sdk-Kon40i0q.js → ai-sdk-CRMlEBX7.js} +75 -44
- package/dist/ai-sdk-CRMlEBX7.js.map +1 -0
- package/dist/browser/ai.js +2 -3
- package/dist/browser/ai.js.map +1 -1
- package/dist/browser/index.d.ts +33 -21
- package/dist/browser/index.js +937 -4
- package/dist/browser/index.js.map +1 -0
- package/dist/browser/tools/ai-sdk.d.ts +4 -4
- package/dist/browser/tools/ai-sdk.js +9 -3
- package/dist/browser/tools/ai-sdk.js.map +1 -1
- package/dist/browser/tools/pi.d.ts +11 -8
- package/dist/browser/tools/pi.js +58 -11
- package/dist/browser/tools/pi.js.map +1 -1
- package/dist/browser/tools/tanstack-ai.d.ts +4 -4
- package/dist/browser/tools/tanstack-ai.js +3 -3
- package/dist/browser/tools/tanstack-ai.js.map +1 -1
- package/dist/{browser-D9RNpJuT.d.ts → browser-C9T74n4c.d.ts} +302 -95
- package/dist/browser-tool-DOqT1oSD.js +360 -0
- package/dist/browser-tool-DOqT1oSD.js.map +1 -0
- package/dist/{browser-tool-BhjCcNVE.d.ts → browser-tool-DxAYQY9T.d.ts} +2 -2
- package/dist/capability-DBAKc6UZ.js.map +1 -1
- package/dist/{capability-runner-BiVUtjqC.d.ts → capability-runner-uFOdSANC.d.ts} +3 -2
- package/dist/chat/index.d.ts +37 -6
- package/dist/chat/index.js +39 -3
- package/dist/chat/index.js.map +1 -1
- package/dist/chat/react.js +1 -0
- package/dist/chat/react.js.map +1 -1
- package/dist/chat-sdk/index.d.ts +3 -3
- package/dist/chat-sdk/index.js +1 -1
- package/dist/cli.js +62 -60
- package/dist/client-CkT8voRY.js +3740 -0
- package/dist/client-CkT8voRY.js.map +1 -0
- package/dist/{client-eSfgYvd3.d.ts → client-DqS6oequ.d.ts} +2 -2
- package/dist/client.d.ts +1 -1
- package/dist/{connector-CM-uuxKM.js → connector-Bphz6wIw.js} +414 -3
- package/dist/connector-Bphz6wIw.js.map +1 -0
- package/dist/{conversations-FjHDRZyD.d.ts → conversations-CoAYNW_q.d.ts} +4 -4
- package/dist/{current-agent-CKgT5d1O.d.ts → current-agent-DShbQHDO.d.ts} +24 -3
- package/dist/current-agent-DhoDkSnH.js.map +1 -1
- package/dist/{do-oauth-client-provider-DtNqv7oj.d.ts → do-oauth-client-provider-BsT8-vtd.d.ts} +22 -2
- package/dist/{durable-object-lifecycle-DOI_bzCH.d.ts → durable-object-lifecycle-CRCA6O0W.d.ts} +2 -2
- package/dist/experimental/channels/index.d.ts +2 -2
- package/dist/experimental/channels/web/index.d.ts +2 -2
- package/dist/experimental/channels/web/index.js +1 -1
- package/dist/{gateway-BFO6vRDn.d.ts → gateway-Dz8zyZBJ.d.ts} +2 -2
- package/dist/harness/ai-sdk/index.d.ts +2 -2
- package/dist/harness/container/index.d.ts +2 -2
- package/dist/harness/container/index.js +1 -1
- package/dist/harness/container/index.js.map +1 -1
- package/dist/harness/opencode/index.d.ts +1 -1
- package/dist/harness/pi/index.d.ts +200 -37
- package/dist/harness/pi/index.js +202 -100
- package/dist/harness/pi/index.js.map +1 -1
- package/dist/harness/think/index.d.ts +2 -2
- package/dist/{images-CfuRCjwq.js → images-D4gtE06D.js} +119 -27
- package/dist/images-D4gtE06D.js.map +1 -0
- package/dist/{index-8k4qsX1T.d.ts → index-BXVgv6tp.d.ts} +4 -4
- package/dist/{index-BEL3ZV-s.d.ts → index-Cs7Ed9la.d.ts} +2 -2
- package/dist/{index-CBt8oRJt.d.ts → index-XiZxauMU.d.ts} +3 -3
- package/dist/index.d.ts +21 -21
- package/dist/index.js +2 -2
- package/dist/{input-schema-BQykTEp_.js → input-schema-D6GilRXI.js} +3 -3
- package/dist/input-schema-D6GilRXI.js.map +1 -0
- package/dist/{input-schema-B0Etuo-o.js → input-schema-y8W5bOdM.js} +3 -3
- package/dist/input-schema-y8W5bOdM.js.map +1 -0
- package/dist/internal_context.d.ts +1 -1
- package/dist/lifecycle/index.d.ts +3 -3
- package/dist/lifecycle/index.js +1 -1
- package/dist/{lifecycle-v1rKHV2C.js → lifecycle-CB5Dmtrt.js} +126 -38
- package/dist/lifecycle-CB5Dmtrt.js.map +1 -0
- package/dist/mcp/client/do-oauth-client-provider.d.ts +1 -1
- package/dist/mcp/client/do-oauth-client-provider.js +45 -2
- package/dist/mcp/client/do-oauth-client-provider.js.map +1 -1
- package/dist/mcp/client/index.d.ts +42 -36
- package/dist/mcp/client/index.js +2 -2
- package/dist/mcp/index.d.ts +16 -16
- package/dist/mcp/index.js +2 -2
- package/dist/models/ai-sdk/index.d.ts +9 -5
- package/dist/models/ai-sdk/index.js +1 -1
- package/dist/models/opencode/index.d.ts +3 -3
- package/dist/models/opencode/index.js +2 -2
- package/dist/models/opencode/index.js.map +1 -1
- package/dist/models/pi-ai/index.d.ts +4 -4
- package/dist/models/pi-ai/index.js +64 -170
- package/dist/models/pi-ai/index.js.map +1 -1
- package/dist/{options-CiIsJHXR.d.ts → options-CiXaymmZ.d.ts} +2 -2
- package/dist/queue/index.d.ts +1 -1
- package/dist/queue/index.js +1 -1
- package/dist/{queue-CcIjVYTE.js → queue-B_wl6K0Z.js} +24 -5
- package/dist/queue-B_wl6K0Z.js.map +1 -0
- package/dist/react.d.ts +2 -2
- package/dist/routing/index.d.ts +2 -2
- package/dist/{scheduler-BCLESHSC.d.ts → scheduler-CTpR0Ct0.d.ts} +2 -2
- package/dist/{scheduler-FIHv2-kJ.js → scheduler-DGNEmxQR.js} +2 -1
- package/dist/scheduler-DGNEmxQR.js.map +1 -0
- package/dist/schedules/index.d.ts +1 -1
- package/dist/schedules/index.js +1 -1
- package/dist/serializable.d.ts +1 -1
- package/dist/{session-7CDgVE0z.js → session-CRY0bGiI.js} +423 -411
- package/dist/session-CRY0bGiI.js.map +1 -0
- package/dist/sessions/index.d.ts +1 -1
- package/dist/{settings-Dk2il2Hf.js → settings-D2F_qONv.js} +11 -2
- package/dist/settings-D2F_qONv.js.map +1 -0
- package/dist/{src-DoGNJ4Ud.js → src-BO1FdLK1.js} +145 -86
- package/dist/src-BO1FdLK1.js.map +1 -0
- package/dist/state/index.d.ts +1 -1
- package/dist/streams/index.d.ts +1 -1
- package/dist/{streams-vrCtbUIn.d.ts → streams-CAZxZBDg.d.ts} +2 -2
- package/dist/sub-routing.d.ts +12 -12
- package/dist/tasks/index.d.ts +1 -1
- package/dist/tasks/index.js +1 -1
- package/dist/{tasks-BN_5AWqI.js → tasks-Cf_UfEO4.js} +2 -1
- package/dist/{tasks-BN_5AWqI.js.map → tasks-Cf_UfEO4.js.map} +1 -1
- package/dist/{tasks-E89KdwkS.d.ts → tasks-Cs8ilctE.d.ts} +2 -2
- package/dist/{tool-DkT_DJBm.js → tool-CmAGY6Fo.js} +24 -3
- package/dist/tool-CmAGY6Fo.js.map +1 -0
- package/dist/{tool-CxKsvxjm.js → tool-Kxcj8_22.js} +24 -4
- package/dist/tool-Kxcj8_22.js.map +1 -0
- package/dist/{tool-helpers-BI7ifO2S.js → tool-helpers-F3_Wy2YX.js} +2 -2
- package/dist/{tool-helpers-BI7ifO2S.js.map → tool-helpers-F3_Wy2YX.js.map} +1 -1
- package/dist/{transport-5moD2pW5.d.ts → transport-hV8AW4Bv.d.ts} +45 -12
- package/dist/voice/index.d.ts +2 -2
- package/dist/web/fetch/index.js +1 -1
- package/dist/web/fetch/tools/ai-sdk.js +2 -2
- package/dist/web/fetch/tools/pi.d.ts +9 -7
- package/dist/web/fetch/tools/pi.js +31 -7
- package/dist/web/fetch/tools/pi.js.map +1 -1
- package/dist/web/fetch/tools/tanstack-ai.js +2 -2
- package/dist/web/search/index.js +1 -1
- package/dist/web/search/tools/ai-sdk.js +2 -2
- package/dist/web/search/tools/pi.d.ts +9 -4
- package/dist/web/search/tools/pi.js +27 -11
- package/dist/web/search/tools/pi.js.map +1 -1
- package/dist/web/search/tools/tanstack-ai.js +2 -2
- package/dist/websockets/index.d.ts +2 -2
- package/dist/websockets/index.js +1 -1
- package/dist/{websockets-C6SJtWCJ.js → websockets-W4uhRg4-.js} +68 -5
- package/dist/websockets-W4uhRg4-.js.map +1 -0
- package/dist/workflow-types.d.ts +1 -1
- package/dist/workflows.d.ts +1 -1
- package/docs/agent-class.md +30 -4
- package/docs/browse-the-web.md +50 -3
- package/docs/chat-agents.md +13 -10
- package/docs/client-tools-continuation.md +1 -1
- package/docs/codemode.md +1 -1
- package/docs/configuration.md +6 -3
- package/docs/cross-domain-authentication.md +1 -1
- package/docs/fetch-the-web.md +20 -0
- package/docs/harnesses/pi.md +242 -33
- package/docs/harnesses/think.md +1 -1
- package/docs/http-websockets.md +40 -32
- package/docs/human-in-the-loop.md +2 -2
- package/docs/lifecycle.md +56 -0
- package/docs/mcp-client.md +104 -7
- package/docs/models-pi-ai.md +24 -10
- package/docs/models.md +42 -17
- package/docs/resumable-streaming.md +1 -1
- package/docs/retries.md +3 -6
- package/docs/search-the-web.md +10 -0
- package/docs/voice.md +1 -1
- package/package.json +16 -9
- package/dist/ai-sdk-Kon40i0q.js.map +0 -1
- package/dist/browser-1eUbPvC2.js +0 -931
- package/dist/browser-1eUbPvC2.js.map +0 -1
- package/dist/browser-tool-DRan7Lk6.js +0 -497
- package/dist/browser-tool-DRan7Lk6.js.map +0 -1
- package/dist/client-DMWujQJ1.js +0 -2325
- package/dist/client-DMWujQJ1.js.map +0 -1
- package/dist/connector-CM-uuxKM.js.map +0 -1
- package/dist/images-CfuRCjwq.js.map +0 -1
- package/dist/input-schema-B0Etuo-o.js.map +0 -1
- package/dist/input-schema-BQykTEp_.js.map +0 -1
- package/dist/lifecycle-v1rKHV2C.js.map +0 -1
- package/dist/queue-CcIjVYTE.js.map +0 -1
- package/dist/scheduler-FIHv2-kJ.js.map +0 -1
- package/dist/session-7CDgVE0z.js.map +0 -1
- package/dist/settings-Dk2il2Hf.js.map +0 -1
- package/dist/src-DoGNJ4Ud.js.map +0 -1
- package/dist/tool-CxKsvxjm.js.map +0 -1
- package/dist/tool-DkT_DJBm.js.map +0 -1
- package/dist/websockets-C6SJtWCJ.js.map +0 -1
|
@@ -1,8 +1,6 @@
|
|
|
1
|
-
import { i as
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import { i as mergeModelOptions, o as resolveOptions, r as gatewayLayerOf } from "../../settings-Dk2il2Hf.js";
|
|
5
|
-
import { calculateCost, clampThinkingLevel, createAssistantMessageEventStream, createProvider, getCurrentTools, normalizeContext, parseStreamingJson } from "@earendil-works/pi-ai";
|
|
1
|
+
import { E as isRunPathLanguageModelId, S as isAbortError, _ as headersToObject, a as normalizeChatCompletionsStream, b as CloudflareAIError, c as GATEWAY_PROVIDER_NAMES, f as resolveGatewayProvider, g as errorFromResponse, h as errorFromGatewayEnvelope, i as sseDataStream, l as gatewaySlugForBaseUrl, n as imageMediaTypeOf, o as normalizeChatCompletion, p as createTransport, r as imageMultipartFields, s as prepareChatCompletionsRequest, t as buildImageInput, u as isGatewayShapedUrl, v as isGatewayErrorEnvelope, w as isCatalogModelId, y as unwrapEnvelope } from "../../images-D4gtE06D.js";
|
|
2
|
+
import { a as mergeModelOptions, i as isGatewayConfigured, r as gatewayLayerOf, s as resolveOptions } from "../../settings-D2F_qONv.js";
|
|
3
|
+
import { ToolCallArgumentsStream, calculateCost, clampThinkingLevel, createAssistantMessageEventStream, createProvider, getCurrentTools, normalizeContext } from "@earendil-works/pi-ai";
|
|
6
4
|
import { CLOUDFLARE_AI_GATEWAY_MODELS } from "@earendil-works/pi-ai/providers/cloudflare-ai-gateway.models";
|
|
7
5
|
import { CLOUDFLARE_WORKERS_AI_MODELS } from "@earendil-works/pi-ai/providers/cloudflare-workers-ai.models";
|
|
8
6
|
import { convertMessages } from "@earendil-works/pi-ai/api/openai-completions";
|
|
@@ -345,9 +343,10 @@ function usageFromOpenAI(model, raw) {
|
|
|
345
343
|
if (raw === void 0) return emptyUsage();
|
|
346
344
|
const prompt = raw.prompt_tokens ?? 0;
|
|
347
345
|
const completion = raw.completion_tokens ?? 0;
|
|
348
|
-
const cacheRead = raw.prompt_tokens_details?.cached_tokens ?? raw.prompt_cache_hit_tokens ?? 0;
|
|
346
|
+
const cacheRead = raw.prompt_tokens_details?.cached_tokens ?? raw.prompt_cache_hit_tokens ?? raw.cached_tokens ?? 0;
|
|
349
347
|
const cacheWrite = raw.prompt_tokens_details?.cache_write_tokens ?? 0;
|
|
350
|
-
const
|
|
348
|
+
const nestedReasoning = raw.completion_tokens_details?.reasoning_tokens;
|
|
349
|
+
const reasoning = nestedReasoning ? nestedReasoning : raw.reasoning_tokens ?? nestedReasoning;
|
|
351
350
|
const totalTokens = raw.total_tokens ?? prompt + completion;
|
|
352
351
|
const output = raw.total_tokens === void 0 ? completion : Math.max(completion, totalTokens - prompt);
|
|
353
352
|
const usage = {
|
|
@@ -668,124 +667,6 @@ async function assertOk(response, context) {
|
|
|
668
667
|
throw await errorFromResponse(response, context);
|
|
669
668
|
}
|
|
670
669
|
//#endregion
|
|
671
|
-
//#region src/models/pi-ai/wires/partial-arguments.ts
|
|
672
|
-
const VALID_ESCAPES = /* @__PURE__ */ new Set([
|
|
673
|
-
"\"",
|
|
674
|
-
"\\",
|
|
675
|
-
"/",
|
|
676
|
-
"b",
|
|
677
|
-
"f",
|
|
678
|
-
"n",
|
|
679
|
-
"r",
|
|
680
|
-
"t",
|
|
681
|
-
"u"
|
|
682
|
-
]);
|
|
683
|
-
const HEX = /^[0-9a-fA-F]$/;
|
|
684
|
-
var _text = /* @__PURE__ */ new WeakMap();
|
|
685
|
-
var _snapshot = /* @__PURE__ */ new WeakMap();
|
|
686
|
-
var _needsRepair = /* @__PURE__ */ new WeakMap();
|
|
687
|
-
var _containers = /* @__PURE__ */ new WeakMap();
|
|
688
|
-
var _inString = /* @__PURE__ */ new WeakMap();
|
|
689
|
-
var _escape = /* @__PURE__ */ new WeakMap();
|
|
690
|
-
var _PartialArguments_brand = /* @__PURE__ */ new WeakSet();
|
|
691
|
-
/**
|
|
692
|
-
* A streamed tool call's arguments, parsed as their text arrives.
|
|
693
|
-
*
|
|
694
|
-
* pi-ai's `parseStreamingJson` re-parses the whole text on every delta: a
|
|
695
|
-
* `JSON.parse` that throws, a character-by-character repair, a second throwing
|
|
696
|
-
* `JSON.parse`, then a partial parse. Over a long argument, such as a file
|
|
697
|
-
* passed to a write tool, that is quadratic in time and garbage.
|
|
698
|
-
*
|
|
699
|
-
* This keeps the lexer state (open containers, whether the text ends inside a
|
|
700
|
-
* string or an escape) across deltas, so each delta scans only its own
|
|
701
|
-
* characters. A snapshot closes the open string and containers and hands the
|
|
702
|
-
* result to the native `JSON.parse`, which still reads the whole text but
|
|
703
|
-
* throws nothing and builds no intermediate strings in JavaScript. When the text ends mid-token (a key, a
|
|
704
|
-
* number, `tru`) the closed text does not parse, and the previous snapshot
|
|
705
|
-
* stands until the next delta. Unlike `parseStreamingJson`, a partial string
|
|
706
|
-
* keeps its trailing whitespace. Text that needs pi-ai's repairs (raw control
|
|
707
|
-
* characters or invalid escapes inside a string) goes back to
|
|
708
|
-
* `parseStreamingJson` for the rest of the call.
|
|
709
|
-
*/
|
|
710
|
-
var PartialArguments = class {
|
|
711
|
-
constructor() {
|
|
712
|
-
_classPrivateMethodInitSpec(this, _PartialArguments_brand);
|
|
713
|
-
_classPrivateFieldInitSpec(this, _text, "");
|
|
714
|
-
_classPrivateFieldInitSpec(this, _snapshot, {});
|
|
715
|
-
_classPrivateFieldInitSpec(this, _needsRepair, false);
|
|
716
|
-
_classPrivateFieldInitSpec(this, _containers, []);
|
|
717
|
-
_classPrivateFieldInitSpec(this, _inString, false);
|
|
718
|
-
_classPrivateFieldInitSpec(this, _escape, void 0);
|
|
719
|
-
}
|
|
720
|
-
get text() {
|
|
721
|
-
return _classPrivateFieldGet2(_text, this);
|
|
722
|
-
}
|
|
723
|
-
/** Append `delta` and return the arguments parsed so far. */
|
|
724
|
-
push(delta) {
|
|
725
|
-
_classPrivateFieldSet2(_text, this, _classPrivateFieldGet2(_text, this) + delta);
|
|
726
|
-
if (!_classPrivateFieldGet2(_needsRepair, this)) _assertClassBrand(_PartialArguments_brand, this, _scan).call(this, delta);
|
|
727
|
-
if (_classPrivateFieldGet2(_needsRepair, this)) {
|
|
728
|
-
_classPrivateFieldSet2(_snapshot, this, parseStreamingJson(_classPrivateFieldGet2(_text, this)));
|
|
729
|
-
return _classPrivateFieldGet2(_snapshot, this);
|
|
730
|
-
}
|
|
731
|
-
const parsed = _assertClassBrand(_PartialArguments_brand, this, _parseClosed).call(this);
|
|
732
|
-
if (parsed !== void 0) _classPrivateFieldSet2(_snapshot, this, parsed);
|
|
733
|
-
return _classPrivateFieldGet2(_snapshot, this);
|
|
734
|
-
}
|
|
735
|
-
};
|
|
736
|
-
function _scan(delta) {
|
|
737
|
-
for (const char of delta) {
|
|
738
|
-
if (_classPrivateFieldGet2(_escape, this) !== void 0) {
|
|
739
|
-
_classPrivateFieldSet2(_escape, this, _classPrivateFieldGet2(_escape, this) + char);
|
|
740
|
-
if (_classPrivateFieldGet2(_escape, this).length === 1) {
|
|
741
|
-
if (!VALID_ESCAPES.has(char)) {
|
|
742
|
-
_classPrivateFieldSet2(_needsRepair, this, true);
|
|
743
|
-
return;
|
|
744
|
-
}
|
|
745
|
-
if (char !== "u") _classPrivateFieldSet2(_escape, this, void 0);
|
|
746
|
-
} else {
|
|
747
|
-
if (!HEX.test(char)) {
|
|
748
|
-
_classPrivateFieldSet2(_needsRepair, this, true);
|
|
749
|
-
return;
|
|
750
|
-
}
|
|
751
|
-
if (_classPrivateFieldGet2(_escape, this).length === 5) _classPrivateFieldSet2(_escape, this, void 0);
|
|
752
|
-
}
|
|
753
|
-
continue;
|
|
754
|
-
}
|
|
755
|
-
if (_classPrivateFieldGet2(_inString, this)) {
|
|
756
|
-
if (char === "\"") _classPrivateFieldSet2(_inString, this, false);
|
|
757
|
-
else if (char === "\\") _classPrivateFieldSet2(_escape, this, "");
|
|
758
|
-
else if (char < " ") {
|
|
759
|
-
_classPrivateFieldSet2(_needsRepair, this, true);
|
|
760
|
-
return;
|
|
761
|
-
}
|
|
762
|
-
continue;
|
|
763
|
-
}
|
|
764
|
-
if (char === "\"") _classPrivateFieldSet2(_inString, this, true);
|
|
765
|
-
else if (char === "{") _classPrivateFieldGet2(_containers, this).push("}");
|
|
766
|
-
else if (char === "[") _classPrivateFieldGet2(_containers, this).push("]");
|
|
767
|
-
else if (char === "}" || char === "]") _classPrivateFieldGet2(_containers, this).pop();
|
|
768
|
-
}
|
|
769
|
-
}
|
|
770
|
-
function _parseClosed() {
|
|
771
|
-
let text = _classPrivateFieldGet2(_text, this);
|
|
772
|
-
if (_classPrivateFieldGet2(_inString, this)) {
|
|
773
|
-
if (_classPrivateFieldGet2(_escape, this) !== void 0) text = text.slice(0, text.length - _classPrivateFieldGet2(_escape, this).length - 1);
|
|
774
|
-
text += "\"";
|
|
775
|
-
} else {
|
|
776
|
-
text = text.trimEnd();
|
|
777
|
-
if (text.endsWith(",")) text = text.slice(0, -1);
|
|
778
|
-
}
|
|
779
|
-
for (let i = _classPrivateFieldGet2(_containers, this).length - 1; i >= 0; i--) text += _classPrivateFieldGet2(_containers, this)[i];
|
|
780
|
-
if (text.trim() === "") return {};
|
|
781
|
-
try {
|
|
782
|
-
const value = JSON.parse(text);
|
|
783
|
-
return typeof value === "object" && value !== null && !Array.isArray(value) ? value : void 0;
|
|
784
|
-
} catch {
|
|
785
|
-
return;
|
|
786
|
-
}
|
|
787
|
-
}
|
|
788
|
-
//#endregion
|
|
789
670
|
//#region src/models/pi-ai/wires/chat-completions.ts
|
|
790
671
|
/**
|
|
791
672
|
* Chat-completions wire: Workers AI on the run path, and any pi-ai model whose
|
|
@@ -811,9 +692,11 @@ const STRICT_OPENAI_COMPAT = {
|
|
|
811
692
|
maxTokensField: "max_tokens",
|
|
812
693
|
openRouterRouting: {},
|
|
813
694
|
requiresAssistantAfterToolResult: false,
|
|
695
|
+
requiresNonNullAssistantContent: false,
|
|
814
696
|
requiresReasoningContentOnAssistantMessages: false,
|
|
815
697
|
requiresThinkingAsText: false,
|
|
816
698
|
requiresToolResultName: false,
|
|
699
|
+
requiresToolsWithToolHistory: false,
|
|
817
700
|
sendSessionAffinityHeaders: false,
|
|
818
701
|
sessionAffinityFormat: "openai",
|
|
819
702
|
supportsDeveloperRole: false,
|
|
@@ -875,16 +758,13 @@ var CompletionAssembler = class {
|
|
|
875
758
|
this.output = output;
|
|
876
759
|
this.stream = stream;
|
|
877
760
|
this.openToolCalls = /* @__PURE__ */ new Map();
|
|
761
|
+
this.toolCallArguments = /* @__PURE__ */ new Map();
|
|
878
762
|
this.closedToolCalls = /* @__PURE__ */ new Set();
|
|
879
|
-
this.toolArguments = /* @__PURE__ */ new Map();
|
|
880
763
|
this.blocks = output.content;
|
|
881
764
|
}
|
|
882
765
|
indexOf(block) {
|
|
883
766
|
return this.blocks.indexOf(block);
|
|
884
767
|
}
|
|
885
|
-
stripScratch() {
|
|
886
|
-
for (const block of this.blocks) if (block.type === "toolCall") delete block.partialArguments;
|
|
887
|
-
}
|
|
888
768
|
endText() {
|
|
889
769
|
if (this.textBlock === void 0) return;
|
|
890
770
|
this.stream.push({
|
|
@@ -956,9 +836,9 @@ var CompletionAssembler = class {
|
|
|
956
836
|
const block = this.openToolCalls.get(index);
|
|
957
837
|
if (block === void 0 || this.closedToolCalls.has(index)) return;
|
|
958
838
|
this.closedToolCalls.add(index);
|
|
959
|
-
|
|
960
|
-
delete
|
|
961
|
-
|
|
839
|
+
const args = this.toolCallArguments.get(index);
|
|
840
|
+
this.toolCallArguments.delete(index);
|
|
841
|
+
Object.assign(block, (args ?? new ToolCallArgumentsStream()).finish());
|
|
962
842
|
this.stream.push({
|
|
963
843
|
contentIndex: this.indexOf(block),
|
|
964
844
|
partial: this.output,
|
|
@@ -985,10 +865,10 @@ var CompletionAssembler = class {
|
|
|
985
865
|
arguments: {},
|
|
986
866
|
id: call.id ?? crypto.randomUUID(),
|
|
987
867
|
name: name ?? "",
|
|
988
|
-
partialArguments: "",
|
|
989
868
|
type: "toolCall"
|
|
990
869
|
};
|
|
991
870
|
this.openToolCalls.set(index, block);
|
|
871
|
+
this.toolCallArguments.set(index, new ToolCallArgumentsStream());
|
|
992
872
|
this.blocks.push(block);
|
|
993
873
|
this.stream.push({
|
|
994
874
|
contentIndex: this.indexOf(block),
|
|
@@ -1001,13 +881,12 @@ var CompletionAssembler = class {
|
|
|
1001
881
|
}
|
|
1002
882
|
this.lastToolIndex = index;
|
|
1003
883
|
if (args.length > 0) {
|
|
1004
|
-
let
|
|
1005
|
-
if (
|
|
1006
|
-
|
|
1007
|
-
this.
|
|
884
|
+
let stream = this.toolCallArguments.get(index);
|
|
885
|
+
if (stream === void 0) {
|
|
886
|
+
stream = new ToolCallArgumentsStream();
|
|
887
|
+
this.toolCallArguments.set(index, stream);
|
|
1008
888
|
}
|
|
1009
|
-
block.arguments =
|
|
1010
|
-
block.partialArguments = parser.text;
|
|
889
|
+
block.arguments = stream.append(args);
|
|
1011
890
|
this.stream.push({
|
|
1012
891
|
contentIndex: this.indexOf(block),
|
|
1013
892
|
delta: args,
|
|
@@ -1150,7 +1029,6 @@ function streamCompletions(request, transport) {
|
|
|
1150
1029
|
if (sampling !== void 0) Object.assign(strict, sampling);
|
|
1151
1030
|
const prepared = workersAI ? prepareChatCompletionsRequest(strict, model.id) : {
|
|
1152
1031
|
body: strict,
|
|
1153
|
-
reasoningOff: false,
|
|
1154
1032
|
warnings: []
|
|
1155
1033
|
};
|
|
1156
1034
|
recordWarnings(output, [...ignored, ...prepared.warnings]);
|
|
@@ -1180,7 +1058,7 @@ function streamCompletions(request, transport) {
|
|
|
1180
1058
|
let sawFinishReason = false;
|
|
1181
1059
|
let sawDone = false;
|
|
1182
1060
|
const decoded = withIdleDeadline(response.body, request.streamIdleTimeoutMs).pipeThrough(sseDataStream());
|
|
1183
|
-
const reader = (workersAI ? decoded.pipeThrough(normalizeChatCompletionsStream(model.id
|
|
1061
|
+
const reader = (workersAI ? decoded.pipeThrough(normalizeChatCompletionsStream(model.id)) : decoded).getReader();
|
|
1184
1062
|
try {
|
|
1185
1063
|
for (;;) {
|
|
1186
1064
|
const { done, value } = await reader.read();
|
|
@@ -1217,7 +1095,6 @@ function streamCompletions(request, transport) {
|
|
|
1217
1095
|
stream.end(output);
|
|
1218
1096
|
} catch (error) {
|
|
1219
1097
|
if (response?.body !== null && response?.body?.locked === false) response.body.cancel().catch(() => {});
|
|
1220
|
-
assembler.stripScratch();
|
|
1221
1098
|
failStream(stream, output, error, options.signal);
|
|
1222
1099
|
}
|
|
1223
1100
|
})();
|
|
@@ -1293,14 +1170,16 @@ async function readImage(response) {
|
|
|
1293
1170
|
/**
|
|
1294
1171
|
* Generates images with a Cloudflare catalog model. Text input parts join into
|
|
1295
1172
|
* the prompt; the first image part, when present, is passed as the source
|
|
1296
|
-
* image for image-to-image models.
|
|
1173
|
+
* image for image-to-image models, and a FLUX.2 model takes up to four as
|
|
1174
|
+
* reference images. Failures are reported through
|
|
1297
1175
|
* `stopReason`/`errorMessage`, as pi-ai's images contract expects.
|
|
1298
1176
|
*/
|
|
1299
1177
|
async function generateImages(config, model, context, options) {
|
|
1300
1178
|
const modelOptions = imageOptionsOf(model);
|
|
1301
1179
|
const resolved = resolveOptions(config.providerGateway, modelOptions, void 0);
|
|
1302
1180
|
const prompt = context.input.filter((part) => part.type === "text").map((part) => part.text).join("\n");
|
|
1303
|
-
const
|
|
1181
|
+
const images = context.input.filter((part) => part.type === "image");
|
|
1182
|
+
const source = images[0];
|
|
1304
1183
|
const setting = (key) => options?.[key] !== void 0 ? options[key] : modelOptions?.[key];
|
|
1305
1184
|
const built = buildImageInput(model.id, prompt, {
|
|
1306
1185
|
...setting("width") === void 0 ? {} : { width: setting("width") },
|
|
@@ -1313,7 +1192,21 @@ async function generateImages(config, model, context, options) {
|
|
|
1313
1192
|
});
|
|
1314
1193
|
const input = built.input;
|
|
1315
1194
|
const warnings = built.warnings;
|
|
1316
|
-
|
|
1195
|
+
let multipart;
|
|
1196
|
+
if (built.body === "multipart") {
|
|
1197
|
+
multipart = imageMultipartFields(input, images.map((image) => ({
|
|
1198
|
+
bytes: base64ToBytes(image.data),
|
|
1199
|
+
contentType: image.mimeType
|
|
1200
|
+
})));
|
|
1201
|
+
if (images.length > 4) warnings.push({
|
|
1202
|
+
feature: "images",
|
|
1203
|
+
message: `Only the first 4 images are used.`
|
|
1204
|
+
});
|
|
1205
|
+
if (isGatewayConfigured(resolved.gateway)) warnings.push({
|
|
1206
|
+
feature: "gateway",
|
|
1207
|
+
message: `${model.id} goes to Workers AI directly, so the gateway options do not apply.`
|
|
1208
|
+
});
|
|
1209
|
+
} else if (built.body === "json" && source !== void 0) input.image = Array.from(base64ToBytes(source.data));
|
|
1317
1210
|
const result = {
|
|
1318
1211
|
api: model.api,
|
|
1319
1212
|
model: model.id,
|
|
@@ -1329,6 +1222,7 @@ async function generateImages(config, model, context, options) {
|
|
|
1329
1222
|
headers: resolved.headers,
|
|
1330
1223
|
input,
|
|
1331
1224
|
model: model.id,
|
|
1225
|
+
...multipart === void 0 ? {} : { multipart },
|
|
1332
1226
|
signal: options?.signal
|
|
1333
1227
|
});
|
|
1334
1228
|
await assertOk(response, {
|
|
@@ -1456,15 +1350,16 @@ function buildRequest(model, context, options, simple, config) {
|
|
|
1456
1350
|
* Gateway — `/run` for a catalog model, the provider-native path otherwise.
|
|
1457
1351
|
*
|
|
1458
1352
|
* Everything about the wire — request shaping, thinking, tool use, stream
|
|
1459
|
-
* parsing, thinking-signature replay — is pi-ai's
|
|
1460
|
-
*
|
|
1461
|
-
*
|
|
1462
|
-
* SDK, and sets the vendor headers the SDK would otherwise have added.
|
|
1353
|
+
* parsing, thinking-signature replay, error fallbacks — is pi-ai's, on its own
|
|
1354
|
+
* Anthropic SDK client. The `fetch` given to that client sends the SDK's
|
|
1355
|
+
* request through the gateway instead of to Anthropic.
|
|
1463
1356
|
*
|
|
1464
1357
|
* The model object is never rewritten: `api`, `provider` and `id` reach pi's
|
|
1465
1358
|
* implementation exactly as they will be recorded on the assistant message, so
|
|
1466
1359
|
* a later turn replaying a thinking block is recognised as the same model.
|
|
1467
1360
|
*/
|
|
1361
|
+
/** The key pi-ai's SDK client is built with. The gateway holds the real one. */
|
|
1362
|
+
const GATEWAY_HELD_KEY = "held-by-ai-gateway";
|
|
1468
1363
|
/**
|
|
1469
1364
|
* Maps a pi thinking level onto an Anthropic effort the way pi-ai's own
|
|
1470
1365
|
* `streamSimple` does: the model's `thinkingLevelMap` first, then the level's
|
|
@@ -1506,36 +1401,40 @@ function streamAnthropic(request, transport) {
|
|
|
1506
1401
|
const placeholder = startMessage(model);
|
|
1507
1402
|
let correlation;
|
|
1508
1403
|
let transportError;
|
|
1509
|
-
const
|
|
1510
|
-
|
|
1404
|
+
const fetch = async (input, init) => {
|
|
1405
|
+
transportError = void 0;
|
|
1406
|
+
const sdkRequest = new Request(input, init);
|
|
1407
|
+
const { model: _sdkModel, ...vendorParams } = await sdkRequest.json();
|
|
1511
1408
|
const body = {
|
|
1512
1409
|
...vendorParams,
|
|
1513
1410
|
model: wireModelId(model)
|
|
1514
1411
|
};
|
|
1412
|
+
const betas = sdkRequest.headers.get("anthropic-beta");
|
|
1413
|
+
const context = {
|
|
1414
|
+
model: model.id,
|
|
1415
|
+
requestBodyValues: body,
|
|
1416
|
+
url: transport.url
|
|
1417
|
+
};
|
|
1515
1418
|
try {
|
|
1516
|
-
const response = await sendGateway(request, transport, body, anthropicVendorHeaders(
|
|
1419
|
+
const response = await sendGateway(request, transport, body, anthropicVendorHeaders(betas ? betas.split(",") : []), "messages", (raw) => {
|
|
1517
1420
|
correlation = correlationDetails(request, raw, transport);
|
|
1518
1421
|
});
|
|
1519
|
-
|
|
1520
|
-
|
|
1521
|
-
|
|
1522
|
-
|
|
1523
|
-
});
|
|
1422
|
+
if (!response.ok) {
|
|
1423
|
+
transportError = await errorFromResponse(response.clone(), context);
|
|
1424
|
+
return response;
|
|
1425
|
+
}
|
|
1524
1426
|
if (response.body === null) return response;
|
|
1525
1427
|
return new Response(withIdleDeadline(response.body, request.streamIdleTimeoutMs), response);
|
|
1526
1428
|
} catch (error) {
|
|
1527
1429
|
transportError = error;
|
|
1528
1430
|
throw error;
|
|
1529
1431
|
}
|
|
1530
|
-
} });
|
|
1531
|
-
const client = {
|
|
1532
|
-
beta: { messages: { create } },
|
|
1533
|
-
messages: { create }
|
|
1534
1432
|
};
|
|
1535
1433
|
const anthropicOptions = {
|
|
1536
1434
|
...options,
|
|
1537
1435
|
...request.simple ? thinkingOptions(model, options) : {},
|
|
1538
|
-
|
|
1436
|
+
apiKey: GATEWAY_HELD_KEY,
|
|
1437
|
+
fetch,
|
|
1539
1438
|
...request.toolChoice !== void 0 ? { toolChoice: typeof request.toolChoice === "string" ? request.toolChoice === "required" ? "any" : request.toolChoice : {
|
|
1540
1439
|
name: request.toolChoice.function.name,
|
|
1541
1440
|
type: "tool"
|
|
@@ -1668,11 +1567,6 @@ function streamResponses(request, transport) {
|
|
|
1668
1567
|
stream.end(output);
|
|
1669
1568
|
} catch (error) {
|
|
1670
1569
|
if (response?.body !== null && response?.body?.locked === false) response.body.cancel().catch(() => {});
|
|
1671
|
-
for (const block of output.content) {
|
|
1672
|
-
delete block.index;
|
|
1673
|
-
delete block.partialJson;
|
|
1674
|
-
delete block.customInput;
|
|
1675
|
-
}
|
|
1676
1570
|
failStream(stream, output, error, options.signal);
|
|
1677
1571
|
}
|
|
1678
1572
|
})();
|
|
@@ -1892,7 +1786,7 @@ function createStreams(config) {
|
|
|
1892
1786
|
*
|
|
1893
1787
|
* const ai = createAI({ binding: env.AI, id: "prod" });
|
|
1894
1788
|
*
|
|
1895
|
-
* ai("@cf/
|
|
1789
|
+
* ai("@cf/deepseek-ai/deepseek-v4.1-flash-1017"); // Workers AI: ours
|
|
1896
1790
|
* ai(getBuiltinModel("anthropic", "claude-opus-4-8")); // theirs, our transport
|
|
1897
1791
|
* ai("anthropic/claude-opus-4-8"); // same, via pi's catalog
|
|
1898
1792
|
* ```
|
|
@@ -1946,8 +1840,8 @@ function buildModel(target, options) {
|
|
|
1946
1840
|
*
|
|
1947
1841
|
* ```ts
|
|
1948
1842
|
* const ai = createAI({ binding: env.AI, id: "prod" });
|
|
1949
|
-
* ai("@cf/
|
|
1950
|
-
* ai(anthropicModel, { fallback: ["@cf/
|
|
1843
|
+
* ai("@cf/deepseek-ai/deepseek-v4.1-flash-1017");
|
|
1844
|
+
* ai(anthropicModel, { fallback: ["@cf/deepseek-ai/deepseek-v4.1-flash-1017"] });
|
|
1951
1845
|
* new Agent({ streamFn: ai.streamFn, initialState: { model: ai(openaiModel) } });
|
|
1952
1846
|
* ```
|
|
1953
1847
|
*/
|