@bastani/pi-ai 0.9.43 → 0.9.44-alpha.2
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/CHANGELOG.md +23 -0
- package/dist/api/anthropic-messages.d.ts.map +1 -1
- package/dist/api/anthropic-messages.js +45 -58
- package/dist/api/anthropic-messages.js.map +1 -1
- package/dist/api/azure-openai-responses.d.ts.map +1 -1
- package/dist/api/azure-openai-responses.js +0 -6
- package/dist/api/azure-openai-responses.js.map +1 -1
- package/dist/api/bedrock-converse-stream.d.ts.map +1 -1
- package/dist/api/bedrock-converse-stream.js +71 -78
- package/dist/api/bedrock-converse-stream.js.map +1 -1
- package/dist/api/constrained-sampling.d.ts +11 -1
- package/dist/api/constrained-sampling.d.ts.map +1 -1
- package/dist/api/constrained-sampling.js +33 -18
- package/dist/api/constrained-sampling.js.map +1 -1
- package/dist/api/google-generative-ai.d.ts.map +1 -1
- package/dist/api/google-generative-ai.js +0 -6
- package/dist/api/google-generative-ai.js.map +1 -1
- package/dist/api/google-shared.d.ts +2 -1
- package/dist/api/google-shared.d.ts.map +1 -1
- package/dist/api/google-shared.js +57 -2
- package/dist/api/google-shared.js.map +1 -1
- package/dist/api/google-vertex.d.ts.map +1 -1
- package/dist/api/google-vertex.js +0 -6
- package/dist/api/google-vertex.js.map +1 -1
- package/dist/api/mistral-conversations.d.ts.map +1 -1
- package/dist/api/mistral-conversations.js +17 -44
- package/dist/api/mistral-conversations.js.map +1 -1
- package/dist/api/openai-codex-responses.d.ts.map +1 -1
- package/dist/api/openai-codex-responses.js +0 -5
- package/dist/api/openai-codex-responses.js.map +1 -1
- package/dist/api/openai-completions.d.ts.map +1 -1
- package/dist/api/openai-completions.js +34 -53
- package/dist/api/openai-completions.js.map +1 -1
- package/dist/api/openai-responses-shared.d.ts.map +1 -1
- package/dist/api/openai-responses-shared.js +58 -57
- package/dist/api/openai-responses-shared.js.map +1 -1
- package/dist/api/openai-responses.d.ts.map +1 -1
- package/dist/api/openai-responses.js +0 -6
- package/dist/api/openai-responses.js.map +1 -1
- package/dist/api/pi-messages.d.ts +2 -0
- package/dist/api/pi-messages.d.ts.map +1 -1
- package/dist/api/pi-messages.js +15 -9
- package/dist/api/pi-messages.js.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/providers/data/.manifest.json +1 -1
- package/dist/providers/data/kimi-coding.json +1 -1
- package/dist/providers/data/moonshotai-cn.json +1 -1
- package/dist/providers/data/moonshotai.json +1 -1
- package/dist/providers/data/opencode-go.json +1 -1
- package/dist/providers/data/openrouter.json +1 -1
- package/dist/providers/data/vercel-ai-gateway.json +1 -1
- package/dist/providers/faux.d.ts.map +1 -1
- package/dist/providers/faux.js +7 -2
- package/dist/providers/faux.js.map +1 -1
- package/dist/providers/radius-config.d.ts.map +1 -1
- package/dist/providers/radius-config.js +2 -1
- package/dist/providers/radius-config.js.map +1 -1
- package/dist/types.js.map +1 -1
- package/dist/utils/error-body.d.ts.map +1 -1
- package/dist/utils/error-body.js +2 -1
- package/dist/utils/error-body.js.map +1 -1
- package/dist/utils/streaming-json.d.ts +84 -0
- package/dist/utils/streaming-json.d.ts.map +1 -0
- package/dist/utils/streaming-json.js +513 -0
- package/dist/utils/streaming-json.js.map +1 -0
- package/dist/utils/text.d.ts +7 -0
- package/dist/utils/text.d.ts.map +1 -1
- package/dist/utils/text.js +9 -0
- package/dist/utils/text.js.map +1 -1
- package/dist/utils/version.d.ts +1 -1
- package/dist/utils/version.d.ts.map +1 -1
- package/dist/utils/version.js +1 -1
- package/dist/utils/version.js.map +1 -1
- package/package.json +1 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"azure-openai-responses.d.ts","sourceRoot":"","sources":["../../src/api/azure-openai-responses.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,6BAA6B,EAAE,MAAM,yCAAyC,CAAC;AAE7F,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 -93,12 +93,6 @@ export const stream = (model, context, options) => {
|
|
|
93
93
|
stream.end();
|
|
94
94
|
}
|
|
95
95
|
catch (error) {
|
|
96
|
-
for (const block of output.content) {
|
|
97
|
-
delete block.index;
|
|
98
|
-
// Streaming scratch buffers are only used during parsing; never persist them.
|
|
99
|
-
delete block.partialJson;
|
|
100
|
-
delete block.customInput;
|
|
101
|
-
}
|
|
102
96
|
output.stopReason = options?.signal?.aborted ? "aborted" : "error";
|
|
103
97
|
output.errorMessage = formatAzureOpenAIError(error);
|
|
104
98
|
stream.push({ type: "error", reason: output.stopReason, error: output });
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"azure-openai-responses.js","sourceRoot":"","sources":["../../src/api/azure-openai-responses.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,QAAQ,CAAC;AAErC,OAAO,EAAE,kBAAkB,EAAE,MAAM,cAAc,CAAC;AAElD,OAAO,EAAE,mBAAmB,EAAE,sBAAsB,EAAE,MAAM,wBAAwB,CAAC;AACrF,OAAO,EAAE,2BAA2B,EAAE,MAAM,0BAA0B,CAAC;AACvE,OAAO,EAAE,eAAe,EAAE,MAAM,qBAAqB,CAAC;AACtD,OAAO,EAAE,kBAAkB,EAAE,MAAM,sBAAsB,CAAC;AAC1D,OAAO,EAAE,oBAAoB,EAAE,MAAM,4BAA4B,CAAC;AAClE,OAAO,EAAE,mBAAmB,EAAE,iBAAiB,EAAE,MAAM,+BAA+B,CAAC;AACvF,OAAO,EAAE,gBAAgB,EAAE,iBAAiB,EAAE,sBAAsB,EAAE,MAAM,wBAAwB,CAAC;AACrG,OAAO,EAA6B,kBAAkB,EAAE,qBAAqB,EAAE,MAAM,0BAA0B,CAAC;AAChH,OAAO,EAAE,gCAAgC,EAAE,MAAM,2BAA2B,CAAC;AAC7E,OAAO,EAAE,yBAAyB,EAAE,MAAM,0BAA0B,CAAC;AACrE,OAAO,EACN,wBAAwB,EACxB,qBAAqB,EACrB,uB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vC,CAAC;IAED,OAAO,MAAM,CAAC;AACf,CAAC","sourcesContent":["import { AzureOpenAI } from \"openai\";\nimport type { ResponseCreateParamsStreaming } from \"openai/resources/responses/responses.js\";\nimport { clampThinkingLevel } from \"../models.ts\";\nimport type { Api, AssistantMessage, Model, SimpleStreamOptions, StreamFunction, TranscriptContext } from \"../types.ts\";\nimport { formatProviderError, normalizeProviderError } from \"../utils/error-body.ts\";\nimport { AssistantMessageEventStream } from \"../utils/event-stream.ts\";\nimport { headersToRecord } from \"../utils/headers.ts\";\nimport { getIdentityHeaders } from \"../utils/identity.ts\";\nimport { retryProviderRequest } from \"../utils/provider-retry.ts\";\nimport { admitOpenAIRequests, getFetchAdmission } from \"../utils/request-admission.js\";\nimport { getDeclaredTools, resolveTranscript, resolveTranscriptTools } from \"../utils/transcript.ts\";\nimport { type AzureEndpointOptions, resolveAzureConfig, resolveDeploymentName } from \"./azure-openai-config.ts\";\nimport { createGrammarToolInputProperties } from \"./constrained-sampling.ts\";\nimport { clampOpenAIPromptCacheKey } from \"./openai-prompt-cache.ts\";\nimport {\n\tconvertResponsesMessages,\n\tconvertResponsesTools,\n\tgetResponsesToolSupport,\n\tprocessResponsesStream,\n} from \"./openai-responses-shared.ts\";\nimport { buildBaseOptions, resolveSamplingParams } from \"./simple-options.ts\";\n\nconst AZURE_TOOL_CALL_PROVIDERS = new Set([\n\t\"openai\",\n\t\"openai-api\",\n\t\"openai-codex\",\n\t\"opencode\",\n\t\"azure\",\n\t\"azure-openai-responses\",\n]);\n// OpenAI Responses rejects max_output_tokens below 16: https://github.com/earendil-works/pi/issues/6265\nconst OPENAI_RESPONSES_MIN_OUTPUT_TOKENS = 16;\n\nfunction formatAzureOpenAIError(error: unknown): string {\n\treturn formatProviderError(normalizeProviderError(error), \"Azure OpenAI API error\");\n}\n\n// Azure OpenAI Responses-specific options\nexport interface AzureOpenAIResponsesOptions extends AzureEndpointOptions {\n\treasoningEffort?: \"minimal\" | \"low\" | \"medium\" | \"high\" | \"xhigh\" | \"max\";\n\ttoolChoice?: ResponseCreateParamsStreaming[\"tool_choice\"];\n\treasoningSummary?: \"auto\" | \"detailed\" | \"concise\" | null;\n}\n\n/**\n * Generate function for Azure OpenAI Responses API\n */\nexport const stream: StreamFunction<\"azure-openai-responses\", AzureOpenAIResponsesOptions> = (\n\tmodel: Model<\"azure-openai-responses\">,\n\tcontext: TranscriptContext,\n\toptions?: AzureOpenAIResponsesOptions,\n): AssistantMessageEventStream => {\n\tconst stream = new AssistantMessageEventStream();\n\tconst normalizedContext = resolveTranscript(context, model.compat?.supportsMidConvoSystemMessages);\n\n\t// Start async processing\n\t(async () => {\n\t\tconst deploymentName = resolveDeploymentName(model, options);\n\n\t\tconst output: AssistantMessage = {\n\t\t\trole: \"assistant\",\n\t\t\tcontent: [],\n\t\t\tapi: \"azure-openai-responses\" as Api,\n\t\t\tprovider: model.provider,\n\t\t\tmodel: model.id,\n\t\t\tusage: {\n\t\t\t\tinput: 0,\n\t\t\t\toutput: 0,\n\t\t\t\tcacheRead: 0,\n\t\t\t\tcacheWrite: 0,\n\t\t\t\ttotalTokens: 0,\n\t\t\t\tcost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },\n\t\t\t},\n\t\t\tstopReason: \"pending\",\n\t\t\ttimestamp: Date.now(),\n\t\t};\n\n\t\ttry {\n\t\t\t// Create Azure OpenAI client\n\t\t\tconst apiKey = options?.apiKey;\n\t\t\tif (!apiKey) {\n\t\t\t\tthrow new Error(`No API key for provider: ${model.provider}`);\n\t\t\t}\n\t\t\tconst client = createClient(model, apiKey, options);\n\t\t\tconst grammarToolInputProperties = createGrammarToolInputProperties(\n\t\t\t\tgetDeclaredTools(normalizedContext.messages),\n\t\t\t\tmodel.compat?.supportsOpenAIGrammarTools ?? false,\n\t\t\t);\n\t\t\tlet params = buildParams(model, normalizedContext, options, deploymentName, grammarToolInputProperties);\n\t\t\tconst nextParams = await options?.onPayload?.(params, model);\n\t\t\tif (nextParams !== undefined) {\n\t\t\t\tparams = nextParams as ResponseCreateParamsStreaming;\n\t\t\t}\n\t\t\tconst requestOptions = {\n\t\t\t\t...(options?.signal ? { signal: options.signal } : {}),\n\t\t\t\t...(options?.timeoutMs !== undefined ? { timeout: options.timeoutMs } : {}),\n\t\t\t\tmaxRetries: 0,\n\t\t\t};\n\t\t\tconst { data: openaiStream, response } = await retryProviderRequest(\n\t\t\t\t() => client.responses.create(params, requestOptions).withResponse(),\n\t\t\t\t{\n\t\t\t\t\tmaxRetries: options?.maxRetries,\n\t\t\t\t\tmaxRetryDelayMs: options?.maxRetryDelayMs,\n\t\t\t\t\tsignal: options?.signal,\n\t\t\t\t},\n\t\t\t);\n\t\t\tawait options?.onResponse?.({ status: response.status, headers: headersToRecord(response.headers) }, model);\n\t\t\tstream.push({ type: \"start\", partial: output });\n\n\t\t\tawait processResponsesStream(openaiStream, output, stream, model, {\n\t\t\t\tonProviderStreamEvent: options?.onProviderStreamEvent,\n\t\t\t\tgrammarToolInputProperties,\n\t\t\t});\n\n\t\t\tif (options?.signal?.aborted) {\n\t\t\t\tthrow new Error(\"Request was aborted\");\n\t\t\t}\n\n\t\t\tif (output.stopReason === \"pending\") {\n\t\t\t\tthrow new Error(\"Azure OpenAI Responses stream ended without a stop reason\");\n\t\t\t}\n\t\t\tif (output.stopReason === \"aborted\" || output.stopReason === \"error\") {\n\t\t\t\tthrow new Error(output.errorMessage || \"An unknown error occurred\");\n\t\t\t}\n\n\t\t\tstream.push({ type: \"done\", reason: output.stopReason, message: output });\n\t\t\tstream.end();\n\t\t} catch (error) {\n\t\t\tfor (const block of output.content) {\n\t\t\t\tdelete (block as { index?: number }).index;\n\t\t\t\t// Streaming scratch buffers are only used during parsing; never persist them.\n\t\t\t\tdelete (block as { partialJson?: string }).partialJson;\n\t\t\t\tdelete (block as { customInput?: unknown }).customInput;\n\t\t\t}\n\t\t\toutput.stopReason = options?.signal?.aborted ? \"aborted\" : \"error\";\n\t\t\toutput.errorMessage = formatAzureOpenAIError(error);\n\t\t\tstream.push({ type: \"error\", reason: output.stopReason, error: output });\n\t\t\tstream.end();\n\t\t}\n\t})();\n\n\treturn stream;\n};\n\nexport const streamSimple: StreamFunction<\"azure-openai-responses\", SimpleStreamOptions> = (\n\tmodel: Model<\"azure-openai-responses\">,\n\tcontext: TranscriptContext,\n\toptions?: SimpleStreamOptions,\n): AssistantMessageEventStream => {\n\tconst apiKey = options?.apiKey;\n\tif (!apiKey) {\n\t\tthrow new Error(`No API key for provider: ${model.provider}`);\n\t}\n\n\tconst base = {\n\t\t...buildBaseOptions(model, context, options, apiKey),\n\t\ttoolChoice: options?.toolChoice,\n\t} satisfies AzureOpenAIResponsesOptions;\n\tconst clampedReasoning = options?.reasoning ? clampThinkingLevel(model, options.reasoning) : undefined;\n\tconst reasoningEffort = clampedReasoning === \"off\" ? undefined : clampedReasoning;\n\n\treturn stream(model, context, {\n\t\t...base,\n\t\treasoningEffort,\n\t} satisfies AzureOpenAIResponsesOptions);\n};\n\nfunction createClient(model: Model<\"azure-openai-responses\">, apiKey: string, options?: AzureOpenAIResponsesOptions) {\n\tconst headers = { ...getIdentityHeaders(model, options?.identity), ...model.headers };\n\n\tif (options?.headers) {\n\t\tObject.assign(headers, options.headers);\n\t}\n\n\tconst { baseUrl, apiVersion } = resolveAzureConfig(model, options);\n\n\treturn admitOpenAIRequests(\n\t\tnew AzureOpenAI({\n\t\t\tapiKey,\n\t\t\tapiVersion,\n\t\t\tdangerouslyAllowBrowser: true,\n\t\t\tfetch: getFetchAdmission(options?.fetch)?.fetch ?? options?.fetch,\n\t\t\tdefaultHeaders: headers,\n\t\t\tbaseURL: baseUrl,\n\t\t}),\n\t\toptions?.fetch,\n\t);\n}\n\nfunction buildParams(\n\tmodel: Model<\"azure-openai-responses\">,\n\tcontext: TranscriptContext,\n\toptions: AzureOpenAIResponsesOptions | undefined,\n\tdeploymentName: string,\n\tgrammarToolInputProperties: ReadonlyMap<string, string> = createGrammarToolInputProperties(\n\t\tgetDeclaredTools(context.messages),\n\t\tmodel.compat?.supportsOpenAIGrammarTools ?? false,\n\t),\n) {\n\tconst supportsAdditionalTools = model.compat?.supportsAdditionalTools ?? false;\n\tconst supportsToolSearch = model.compat?.supportsToolSearch ?? false;\n\tconst transcriptTools = resolveTranscriptTools(\n\t\tcontext.messages,\n\t\tgetResponsesToolSupport({\n\t\t\tsupportsMidConvoSystemMessages: model.compat?.supportsMidConvoSystemMessages ?? false,\n\t\t\tsupportsAdditionalTools,\n\t\t\tsupportsToolSearch,\n\t\t}),\n\t);\n\tconst messages = convertResponsesMessages(model, context, AZURE_TOOL_CALL_PROVIDERS, {\n\t\tgrammarToolInputProperties,\n\t\tsupportsMidConvoSystemMessages: model.compat?.supportsMidConvoSystemMessages ?? false,\n\t\tsupportsAdditionalTools,\n\t\tsupportsToolSearch,\n\t\ttoolOptions: {\n\t\t\tsupportsStrictMode: model.compat?.supportsStrictMode ?? true,\n\t\t\tsupportsOpenAIGrammarTools: model.compat?.supportsOpenAIGrammarTools ?? false,\n\t\t},\n\t});\n\n\tconst params: ResponseCreateParamsStreaming = {\n\t\tmodel: deploymentName,\n\t\tinput: messages,\n\t\tstream: true,\n\t\tprompt_cache_key: clampOpenAIPromptCacheKey(options?.sessionId),\n\t\tstore: false,\n\t};\n\n\tif (options?.maxTokens) {\n\t\tparams.max_output_tokens = Math.max(options.maxTokens, OPENAI_RESPONSES_MIN_OUTPUT_TOKENS);\n\t}\n\n\tif (options?.temperature !== undefined) {\n\t\tparams.temperature = options?.temperature;\n\t}\n\n\tif (transcriptTools.requestTools.length > 0) {\n\t\tparams.tools = convertResponsesTools(transcriptTools.requestTools, {\n\t\t\tsupportsStrictMode: model.compat?.supportsStrictMode ?? true,\n\t\t\tsupportsOpenAIGrammarTools: model.compat?.supportsOpenAIGrammarTools ?? false,\n\t\t});\n\t}\n\tif (options?.toolChoice !== undefined) {\n\t\tparams.tool_choice = options.toolChoice;\n\t}\n\n\tconst reasoningEffort = options?.reasoningEffort ?? (options?.reasoningSummary ? \"medium\" : undefined);\n\tif (model.reasoning) {\n\t\tif (reasoningEffort) {\n\t\t\tconst effort = options?.reasoningEffort\n\t\t\t\t? (model.thinkingLevelMap?.[options.reasoningEffort] ?? options.reasoningEffort)\n\t\t\t\t: reasoningEffort;\n\t\t\tparams.reasoning = {\n\t\t\t\teffort: effort as NonNullable<typeof params.reasoning>[\"effort\"],\n\t\t\t\tsummary: options?.reasoningSummary || \"auto\",\n\t\t\t};\n\t\t\tparams.include = [\"reasoning.encrypted_content\"];\n\t\t} else if (model.thinkingLevelMap?.off !== null) {\n\t\t\tparams.reasoning = {\n\t\t\t\teffort: (model.thinkingLevelMap?.off ?? \"none\") as NonNullable<typeof params.reasoning>[\"effort\"],\n\t\t\t};\n\t\t}\n\t}\n\n\t// Last so model and request sampling parameters override named request fields.\n\tconst samplingParams = resolveSamplingParams(model, reasoningEffort ?? \"off\", options?.samplingParams);\n\tif (samplingParams) {\n\t\tObject.assign(params, samplingParams);\n\t}\n\n\treturn params;\n}\n"]}
|
|
1
|
+
{"version":3,"file":"azure-openai-responses.js","sourceRoot":"","sources":["../../src/api/azure-openai-responses.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,QAAQ,CAAC;AAErC,OAAO,EAAE,kBAAkB,EAAE,MAAM,cAAc,CAAC;AAElD,OAAO,EAAE,mBAAmB,EAAE,sBAAsB,EAAE,MAAM,wBAAwB,CAAC;AACrF,OAAO,EAAE,2BAA2B,EAAE,MAAM,0BAA0B,CAAC;AACvE,OAAO,EAAE,eAAe,EAAE,MAAM,qBAAqB,CAAC;AACtD,OAAO,EAAE,kBAAkB,EAAE,MAAM,sBAAsB,CAAC;AAC1D,OAAO,EAAE,oBAAoB,EAAE,MAAM,4BAA4B,CAAC;AAClE,OAAO,EAAE,mBAAmB,EAAE,iBAAiB,EAAE,MAAM,+BAA+B,CAAC;AACvF,OAAO,EAAE,gBAAgB,EAAE,iBAAiB,EAAE,sBAAsB,EAAE,MAAM,wBA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EAAE,cAAc,CAAC,CAAC;IACvC,CAAC;IAED,OAAO,MAAM,CAAC;AACf,CAAC","sourcesContent":["import { AzureOpenAI } from \"openai\";\nimport type { ResponseCreateParamsStreaming } from \"openai/resources/responses/responses.js\";\nimport { clampThinkingLevel } from \"../models.ts\";\nimport type { Api, AssistantMessage, Model, SimpleStreamOptions, StreamFunction, TranscriptContext } from \"../types.ts\";\nimport { formatProviderError, normalizeProviderError } from \"../utils/error-body.ts\";\nimport { AssistantMessageEventStream } from \"../utils/event-stream.ts\";\nimport { headersToRecord } from \"../utils/headers.ts\";\nimport { getIdentityHeaders } from \"../utils/identity.ts\";\nimport { retryProviderRequest } from \"../utils/provider-retry.ts\";\nimport { admitOpenAIRequests, getFetchAdmission } from \"../utils/request-admission.js\";\nimport { getDeclaredTools, resolveTranscript, resolveTranscriptTools } from \"../utils/transcript.ts\";\nimport { type AzureEndpointOptions, resolveAzureConfig, resolveDeploymentName } from \"./azure-openai-config.ts\";\nimport { createGrammarToolInputProperties } from \"./constrained-sampling.ts\";\nimport { clampOpenAIPromptCacheKey } from \"./openai-prompt-cache.ts\";\nimport {\n\tconvertResponsesMessages,\n\tconvertResponsesTools,\n\tgetResponsesToolSupport,\n\tprocessResponsesStream,\n} from \"./openai-responses-shared.ts\";\nimport { buildBaseOptions, resolveSamplingParams } from \"./simple-options.ts\";\n\nconst AZURE_TOOL_CALL_PROVIDERS = new Set([\n\t\"openai\",\n\t\"openai-api\",\n\t\"openai-codex\",\n\t\"opencode\",\n\t\"azure\",\n\t\"azure-openai-responses\",\n]);\n// OpenAI Responses rejects max_output_tokens below 16: https://github.com/earendil-works/pi/issues/6265\nconst OPENAI_RESPONSES_MIN_OUTPUT_TOKENS = 16;\n\nfunction formatAzureOpenAIError(error: unknown): string {\n\treturn formatProviderError(normalizeProviderError(error), \"Azure OpenAI API error\");\n}\n\n// Azure OpenAI Responses-specific options\nexport interface AzureOpenAIResponsesOptions extends AzureEndpointOptions {\n\treasoningEffort?: \"minimal\" | \"low\" | \"medium\" | \"high\" | \"xhigh\" | \"max\";\n\ttoolChoice?: ResponseCreateParamsStreaming[\"tool_choice\"];\n\treasoningSummary?: \"auto\" | \"detailed\" | \"concise\" | null;\n}\n\n/**\n * Generate function for Azure OpenAI Responses API\n */\nexport const stream: StreamFunction<\"azure-openai-responses\", AzureOpenAIResponsesOptions> = (\n\tmodel: Model<\"azure-openai-responses\">,\n\tcontext: TranscriptContext,\n\toptions?: AzureOpenAIResponsesOptions,\n): AssistantMessageEventStream => {\n\tconst stream = new AssistantMessageEventStream();\n\tconst normalizedContext = resolveTranscript(context, model.compat?.supportsMidConvoSystemMessages);\n\n\t// Start async processing\n\t(async () => {\n\t\tconst deploymentName = resolveDeploymentName(model, options);\n\n\t\tconst output: AssistantMessage = {\n\t\t\trole: \"assistant\",\n\t\t\tcontent: [],\n\t\t\tapi: \"azure-openai-responses\" as Api,\n\t\t\tprovider: model.provider,\n\t\t\tmodel: model.id,\n\t\t\tusage: {\n\t\t\t\tinput: 0,\n\t\t\t\toutput: 0,\n\t\t\t\tcacheRead: 0,\n\t\t\t\tcacheWrite: 0,\n\t\t\t\ttotalTokens: 0,\n\t\t\t\tcost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },\n\t\t\t},\n\t\t\tstopReason: \"pending\",\n\t\t\ttimestamp: Date.now(),\n\t\t};\n\n\t\ttry {\n\t\t\t// Create Azure OpenAI client\n\t\t\tconst apiKey = options?.apiKey;\n\t\t\tif (!apiKey) {\n\t\t\t\tthrow new Error(`No API key for provider: ${model.provider}`);\n\t\t\t}\n\t\t\tconst client = createClient(model, apiKey, options);\n\t\t\tconst grammarToolInputProperties = createGrammarToolInputProperties(\n\t\t\t\tgetDeclaredTools(normalizedContext.messages),\n\t\t\t\tmodel.compat?.supportsOpenAIGrammarTools ?? false,\n\t\t\t);\n\t\t\tlet params = buildParams(model, normalizedContext, options, deploymentName, grammarToolInputProperties);\n\t\t\tconst nextParams = await options?.onPayload?.(params, model);\n\t\t\tif (nextParams !== undefined) {\n\t\t\t\tparams = nextParams as ResponseCreateParamsStreaming;\n\t\t\t}\n\t\t\tconst requestOptions = {\n\t\t\t\t...(options?.signal ? { signal: options.signal } : {}),\n\t\t\t\t...(options?.timeoutMs !== undefined ? { timeout: options.timeoutMs } : {}),\n\t\t\t\tmaxRetries: 0,\n\t\t\t};\n\t\t\tconst { data: openaiStream, response } = await retryProviderRequest(\n\t\t\t\t() => client.responses.create(params, requestOptions).withResponse(),\n\t\t\t\t{\n\t\t\t\t\tmaxRetries: options?.maxRetries,\n\t\t\t\t\tmaxRetryDelayMs: options?.maxRetryDelayMs,\n\t\t\t\t\tsignal: options?.signal,\n\t\t\t\t},\n\t\t\t);\n\t\t\tawait options?.onResponse?.({ status: response.status, headers: headersToRecord(response.headers) }, model);\n\t\t\tstream.push({ type: \"start\", partial: output });\n\n\t\t\tawait processResponsesStream(openaiStream, output, stream, model, {\n\t\t\t\tonProviderStreamEvent: options?.onProviderStreamEvent,\n\t\t\t\tgrammarToolInputProperties,\n\t\t\t});\n\n\t\t\tif (options?.signal?.aborted) {\n\t\t\t\tthrow new Error(\"Request was aborted\");\n\t\t\t}\n\n\t\t\tif (output.stopReason === \"pending\") {\n\t\t\t\tthrow new Error(\"Azure OpenAI Responses stream ended without a stop reason\");\n\t\t\t}\n\t\t\tif (output.stopReason === \"aborted\" || output.stopReason === \"error\") {\n\t\t\t\tthrow new Error(output.errorMessage || \"An unknown error occurred\");\n\t\t\t}\n\n\t\t\tstream.push({ type: \"done\", reason: output.stopReason, message: output });\n\t\t\tstream.end();\n\t\t} catch (error) {\n\t\t\toutput.stopReason = options?.signal?.aborted ? \"aborted\" : \"error\";\n\t\t\toutput.errorMessage = formatAzureOpenAIError(error);\n\t\t\tstream.push({ type: \"error\", reason: output.stopReason, error: output });\n\t\t\tstream.end();\n\t\t}\n\t})();\n\n\treturn stream;\n};\n\nexport const streamSimple: StreamFunction<\"azure-openai-responses\", SimpleStreamOptions> = (\n\tmodel: Model<\"azure-openai-responses\">,\n\tcontext: TranscriptContext,\n\toptions?: SimpleStreamOptions,\n): AssistantMessageEventStream => {\n\tconst apiKey = options?.apiKey;\n\tif (!apiKey) {\n\t\tthrow new Error(`No API key for provider: ${model.provider}`);\n\t}\n\n\tconst base = {\n\t\t...buildBaseOptions(model, context, options, apiKey),\n\t\ttoolChoice: options?.toolChoice,\n\t} satisfies AzureOpenAIResponsesOptions;\n\tconst clampedReasoning = options?.reasoning ? clampThinkingLevel(model, options.reasoning) : undefined;\n\tconst reasoningEffort = clampedReasoning === \"off\" ? undefined : clampedReasoning;\n\n\treturn stream(model, context, {\n\t\t...base,\n\t\treasoningEffort,\n\t} satisfies AzureOpenAIResponsesOptions);\n};\n\nfunction createClient(model: Model<\"azure-openai-responses\">, apiKey: string, options?: AzureOpenAIResponsesOptions) {\n\tconst headers = { ...getIdentityHeaders(model, options?.identity), ...model.headers };\n\n\tif (options?.headers) {\n\t\tObject.assign(headers, options.headers);\n\t}\n\n\tconst { baseUrl, apiVersion } = resolveAzureConfig(model, options);\n\n\treturn admitOpenAIRequests(\n\t\tnew AzureOpenAI({\n\t\t\tapiKey,\n\t\t\tapiVersion,\n\t\t\tdangerouslyAllowBrowser: true,\n\t\t\tfetch: getFetchAdmission(options?.fetch)?.fetch ?? options?.fetch,\n\t\t\tdefaultHeaders: headers,\n\t\t\tbaseURL: baseUrl,\n\t\t}),\n\t\toptions?.fetch,\n\t);\n}\n\nfunction buildParams(\n\tmodel: Model<\"azure-openai-responses\">,\n\tcontext: TranscriptContext,\n\toptions: AzureOpenAIResponsesOptions | undefined,\n\tdeploymentName: string,\n\tgrammarToolInputProperties: ReadonlyMap<string, string> = createGrammarToolInputProperties(\n\t\tgetDeclaredTools(context.messages),\n\t\tmodel.compat?.supportsOpenAIGrammarTools ?? false,\n\t),\n) {\n\tconst supportsAdditionalTools = model.compat?.supportsAdditionalTools ?? false;\n\tconst supportsToolSearch = model.compat?.supportsToolSearch ?? false;\n\tconst transcriptTools = resolveTranscriptTools(\n\t\tcontext.messages,\n\t\tgetResponsesToolSupport({\n\t\t\tsupportsMidConvoSystemMessages: model.compat?.supportsMidConvoSystemMessages ?? false,\n\t\t\tsupportsAdditionalTools,\n\t\t\tsupportsToolSearch,\n\t\t}),\n\t);\n\tconst messages = convertResponsesMessages(model, context, AZURE_TOOL_CALL_PROVIDERS, {\n\t\tgrammarToolInputProperties,\n\t\tsupportsMidConvoSystemMessages: model.compat?.supportsMidConvoSystemMessages ?? false,\n\t\tsupportsAdditionalTools,\n\t\tsupportsToolSearch,\n\t\ttoolOptions: {\n\t\t\tsupportsStrictMode: model.compat?.supportsStrictMode ?? true,\n\t\t\tsupportsOpenAIGrammarTools: model.compat?.supportsOpenAIGrammarTools ?? false,\n\t\t},\n\t});\n\n\tconst params: ResponseCreateParamsStreaming = {\n\t\tmodel: deploymentName,\n\t\tinput: messages,\n\t\tstream: true,\n\t\tprompt_cache_key: clampOpenAIPromptCacheKey(options?.sessionId),\n\t\tstore: false,\n\t};\n\n\tif (options?.maxTokens) {\n\t\tparams.max_output_tokens = Math.max(options.maxTokens, OPENAI_RESPONSES_MIN_OUTPUT_TOKENS);\n\t}\n\n\tif (options?.temperature !== undefined) {\n\t\tparams.temperature = options?.temperature;\n\t}\n\n\tif (transcriptTools.requestTools.length > 0) {\n\t\tparams.tools = convertResponsesTools(transcriptTools.requestTools, {\n\t\t\tsupportsStrictMode: model.compat?.supportsStrictMode ?? true,\n\t\t\tsupportsOpenAIGrammarTools: model.compat?.supportsOpenAIGrammarTools ?? false,\n\t\t});\n\t}\n\tif (options?.toolChoice !== undefined) {\n\t\tparams.tool_choice = options.toolChoice;\n\t}\n\n\tconst reasoningEffort = options?.reasoningEffort ?? (options?.reasoningSummary ? \"medium\" : undefined);\n\tif (model.reasoning) {\n\t\tif (reasoningEffort) {\n\t\t\tconst effort = options?.reasoningEffort\n\t\t\t\t? (model.thinkingLevelMap?.[options.reasoningEffort] ?? options.reasoningEffort)\n\t\t\t\t: reasoningEffort;\n\t\t\tparams.reasoning = {\n\t\t\t\teffort: effort as NonNullable<typeof params.reasoning>[\"effort\"],\n\t\t\t\tsummary: options?.reasoningSummary || \"auto\",\n\t\t\t};\n\t\t\tparams.include = [\"reasoning.encrypted_content\"];\n\t\t} else if (model.thinkingLevelMap?.off !== null) {\n\t\t\tparams.reasoning = {\n\t\t\t\teffort: (model.thinkingLevelMap?.off ?? \"none\") as NonNullable<typeof params.reasoning>[\"effort\"],\n\t\t\t};\n\t\t}\n\t}\n\n\t// Last so model and request sampling parameters override named request fields.\n\tconst samplingParams = resolveSamplingParams(model, reasoningEffort ?? \"off\", options?.samplingParams);\n\tif (samplingParams) {\n\t\tObject.assign(params, samplingParams);\n\t}\n\n\treturn params;\n}\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"bedrock-converse-stream.d.ts","sourceRoot":"","sources":["../../src/api/bedrock-converse-stream.ts"],"names":[],"mappings":"AA8BA,OAAO,KAAK,EAWX,mBAAmB,EAEnB,cAAc,EACd,aAAa,EAEb,eAAe,
|
|
1
|
+
{"version":3,"file":"bedrock-converse-stream.d.ts","sourceRoot":"","sources":["../../src/api/bedrock-converse-stream.ts"],"names":[],"mappings":"AA8BA,OAAO,KAAK,EAWX,mBAAmB,EAEnB,cAAc,EACd,aAAa,EAEb,eAAe,EACf,aAAa,EAGb,MAAM,aAAa,CAAC;AAgCrB,MAAM,MAAM,sBAAsB,GAAG,YAAY,GAAG,SAAS,CAAC;AAE9D,MAAM,WAAW,cAAe,SAAQ,aAAa;IACpD,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,UAAU,CAAC,EAAE,MAAM,GAAG,KAAK,GAAG,MAAM,GAAG;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,IAAI,EAAE,MAAM,CAAA;KAAE,CAAC;IAEtE,SAAS,CAAC,EAAE,aAAa,CAAC;IAE1B,eAAe,CAAC,EAAE,eAAe,CAAC;IAElC,mBAAmB,CAAC,EAAE,OAAO,CAAC;IAC9B;;;;;;;;;OASG;IACH,eAAe,CAAC,EAAE,sBAAsB,CAAC;IACzC;;;sGAGkG;IAClG,eAAe,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IACzC;;;;yGAIqG;IACrG,WAAW,CAAC,EAAE,MAAM,CAAC;CACrB;AAsBD,eAAO,MAAM,MAAM,EAAE,cAAc,CAAC,yBAAyB,EAAE,cAAc,CAmP5E,CAAC;AAkLF,eAAO,MAAM,YAAY,EAAE,cAAc,CAAC,yBAAyB,EAAE,mBAAmB,CAiDvF,CAAC"}
|
|
@@ -9,10 +9,10 @@ import { normalizeProviderError } from "../utils/error-body.js";
|
|
|
9
9
|
import { AssistantMessageEventStream } from "../utils/event-stream.js";
|
|
10
10
|
import { providerHeadersToRecord } from "../utils/headers.js";
|
|
11
11
|
import { getClientVersion } from "../utils/identity.js";
|
|
12
|
-
import { parseStreamingJson, parseToolCallArguments } from "../utils/json-parse.js";
|
|
13
12
|
import { resolveHttpProxyUrlForTarget } from "../utils/node-http-proxy.js";
|
|
14
13
|
import { getProviderEnvValue } from "../utils/provider-env.js";
|
|
15
14
|
import { sanitizeSurrogates } from "../utils/sanitize-unicode.js";
|
|
15
|
+
import { ToolCallArgumentsStream } from "../utils/streaming-json.js";
|
|
16
16
|
import { getSystemMessageText } from "../utils/text.js";
|
|
17
17
|
import { collapseSystemMessages, getCurrentTools, getInitialSystemMessage, withoutInitialSystemMessage, } from "../utils/transcript.js";
|
|
18
18
|
import { ANTHROPIC_STRICT_JSON_SCHEMA_PROFILE, getJsonSchemaToolParameters, resolveJsonSchemaStrictSampling, } from "./constrained-sampling.js";
|
|
@@ -44,7 +44,7 @@ export const stream = (model, context, options = {}) => {
|
|
|
44
44
|
stopReason: "pending",
|
|
45
45
|
timestamp: Date.now(),
|
|
46
46
|
};
|
|
47
|
-
const
|
|
47
|
+
const openBlocks = new Map();
|
|
48
48
|
// A profile explicitly configured through pi's auth flow (the `profile`
|
|
49
49
|
// option or scoped `AWS_PROFILE` on the stored credential's env) must win
|
|
50
50
|
// over ambient AWS_ACCESS_KEY_ID/AWS_SECRET_ACCESS_KEY. The SDK default
|
|
@@ -188,13 +188,13 @@ export const stream = (model, context, options = {}) => {
|
|
|
188
188
|
stream.push({ type: "start", partial: output });
|
|
189
189
|
}
|
|
190
190
|
else if (item.contentBlockStart) {
|
|
191
|
-
handleContentBlockStart(item.contentBlockStart,
|
|
191
|
+
handleContentBlockStart(item.contentBlockStart, openBlocks, output, stream);
|
|
192
192
|
}
|
|
193
193
|
else if (item.contentBlockDelta) {
|
|
194
|
-
handleContentBlockDelta(item.contentBlockDelta,
|
|
194
|
+
handleContentBlockDelta(item.contentBlockDelta, openBlocks, output, stream);
|
|
195
195
|
}
|
|
196
196
|
else if (item.contentBlockStop) {
|
|
197
|
-
handleContentBlockStop(item.contentBlockStop,
|
|
197
|
+
handleContentBlockStop(item.contentBlockStop, openBlocks, output, stream);
|
|
198
198
|
}
|
|
199
199
|
else if (item.messageStop) {
|
|
200
200
|
output.rawStopReason = item.messageStop.stopReason;
|
|
@@ -233,15 +233,12 @@ export const stream = (model, context, options = {}) => {
|
|
|
233
233
|
throw new Error(output.errorMessage || "An unknown error occurred");
|
|
234
234
|
}
|
|
235
235
|
// A stream can settle without stopping every block, so finalize here too.
|
|
236
|
-
|
|
237
|
-
finalizeStreamingBlock(block);
|
|
236
|
+
flushOpenBlocks(openBlocks, output);
|
|
238
237
|
stream.push({ type: "done", reason: output.stopReason, message: output });
|
|
239
238
|
stream.end();
|
|
240
239
|
}
|
|
241
240
|
catch (error) {
|
|
242
|
-
|
|
243
|
-
finalizeStreamingBlock(block);
|
|
244
|
-
}
|
|
241
|
+
flushOpenBlocks(openBlocks, output);
|
|
245
242
|
output.stopReason = options.signal?.aborted ? "aborted" : "error";
|
|
246
243
|
output.errorMessage = formatBedrockError(error);
|
|
247
244
|
if (output.stopReason === "error") {
|
|
@@ -444,59 +441,60 @@ export const streamSimple = (model, context, options) => {
|
|
|
444
441
|
thinkingBudgets: options.thinkingBudgets,
|
|
445
442
|
});
|
|
446
443
|
};
|
|
447
|
-
function handleContentBlockStart(event,
|
|
448
|
-
const index = event.contentBlockIndex;
|
|
444
|
+
function handleContentBlockStart(event, openBlocks, output, stream) {
|
|
449
445
|
const start = event.start;
|
|
450
446
|
if (start?.toolUse) {
|
|
451
|
-
|
|
447
|
+
output.content.push({
|
|
452
448
|
type: "toolCall",
|
|
453
449
|
id: start.toolUse.toolUseId || "",
|
|
454
450
|
name: start.toolUse.name || "",
|
|
455
451
|
arguments: {},
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
};
|
|
459
|
-
|
|
460
|
-
stream.push({ type: "toolcall_start", contentIndex: blocks.length - 1, partial: output });
|
|
452
|
+
});
|
|
453
|
+
const contentIndex = output.content.length - 1;
|
|
454
|
+
openBlocks.set(event.contentBlockIndex, { contentIndex, arguments: new ToolCallArgumentsStream() });
|
|
455
|
+
stream.push({ type: "toolcall_start", contentIndex, partial: output });
|
|
461
456
|
}
|
|
462
457
|
}
|
|
463
|
-
function handleContentBlockDelta(event,
|
|
458
|
+
function handleContentBlockDelta(event, openBlocks, output, stream) {
|
|
464
459
|
const contentBlockIndex = event.contentBlockIndex;
|
|
465
460
|
const delta = event.delta;
|
|
466
|
-
let
|
|
467
|
-
let block =
|
|
461
|
+
let open = openBlocks.get(contentBlockIndex);
|
|
462
|
+
let block = open === undefined ? undefined : output.content[open.contentIndex];
|
|
468
463
|
if (delta?.text !== undefined) {
|
|
469
464
|
// If no text block exists yet, create one, as `handleContentBlockStart` is not sent for text blocks
|
|
470
|
-
if (!
|
|
471
|
-
|
|
472
|
-
output.content.
|
|
473
|
-
|
|
474
|
-
block =
|
|
475
|
-
stream.push({ type: "text_start", contentIndex:
|
|
465
|
+
if (!open) {
|
|
466
|
+
output.content.push({ type: "text", text: "" });
|
|
467
|
+
open = { contentIndex: output.content.length - 1 };
|
|
468
|
+
openBlocks.set(contentBlockIndex, open);
|
|
469
|
+
block = output.content[open.contentIndex];
|
|
470
|
+
stream.push({ type: "text_start", contentIndex: open.contentIndex, partial: output });
|
|
476
471
|
}
|
|
477
|
-
if (block
|
|
472
|
+
if (block?.type === "text") {
|
|
478
473
|
block.text += delta.text;
|
|
479
|
-
stream.push({ type: "text_delta", contentIndex:
|
|
474
|
+
stream.push({ type: "text_delta", contentIndex: open.contentIndex, delta: delta.text, partial: output });
|
|
480
475
|
}
|
|
481
476
|
}
|
|
482
|
-
else if (delta?.toolUse && block?.type === "toolCall") {
|
|
483
|
-
block.
|
|
484
|
-
|
|
485
|
-
|
|
477
|
+
else if (delta?.toolUse && block?.type === "toolCall" && open?.arguments !== undefined) {
|
|
478
|
+
block.arguments = open.arguments.append(delta.toolUse.input || "");
|
|
479
|
+
stream.push({
|
|
480
|
+
type: "toolcall_delta",
|
|
481
|
+
contentIndex: open.contentIndex,
|
|
482
|
+
delta: delta.toolUse.input || "",
|
|
483
|
+
partial: output,
|
|
484
|
+
});
|
|
486
485
|
}
|
|
487
486
|
else if (delta?.reasoningContent) {
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
output.content.
|
|
493
|
-
|
|
494
|
-
thinkingBlock = blocks[thinkingIndex];
|
|
495
|
-
stream.push({ type: "thinking_start", contentIndex: thinkingIndex, partial: output });
|
|
487
|
+
if (!open) {
|
|
488
|
+
output.content.push({ type: "thinking", thinking: "", thinkingSignature: "" });
|
|
489
|
+
open = { contentIndex: output.content.length - 1 };
|
|
490
|
+
openBlocks.set(contentBlockIndex, open);
|
|
491
|
+
block = output.content[open.contentIndex];
|
|
492
|
+
stream.push({ type: "thinking_start", contentIndex: open.contentIndex, partial: output });
|
|
496
493
|
}
|
|
497
|
-
if (
|
|
494
|
+
if (block?.type === "thinking") {
|
|
495
|
+
const thinkingIndex = open.contentIndex;
|
|
498
496
|
if (delta.reasoningContent.text) {
|
|
499
|
-
|
|
497
|
+
block.thinking += delta.reasoningContent.text;
|
|
500
498
|
stream.push({
|
|
501
499
|
type: "thinking_delta",
|
|
502
500
|
contentIndex: thinkingIndex,
|
|
@@ -506,18 +504,17 @@ function handleContentBlockDelta(event, blocks, output, stream) {
|
|
|
506
504
|
}
|
|
507
505
|
// `thinkingSignature` holds either an Anthropic signature or an opaque redacted
|
|
508
506
|
// payload, never both: mixing them would corrupt whichever arrived first.
|
|
509
|
-
if (delta.reasoningContent.signature && !
|
|
510
|
-
|
|
511
|
-
(thinkingBlock.thinkingSignature || "") + delta.reasoningContent.signature;
|
|
507
|
+
if (delta.reasoningContent.signature && !block.redacted) {
|
|
508
|
+
block.thinkingSignature = (block.thinkingSignature || "") + delta.reasoningContent.signature;
|
|
512
509
|
}
|
|
513
510
|
if (delta.reasoningContent.redactedContent?.length) {
|
|
514
511
|
// Encrypted reasoning from non-Anthropic models on Bedrock (e.g. OpenAI GPT-5.6).
|
|
515
512
|
// The payload is opaque, so keep it verbatim in `thinkingSignature` the way the
|
|
516
513
|
// Anthropic path stores redacted thinking, and replay it on the next turn.
|
|
517
|
-
if (!
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
514
|
+
if (!block.redacted) {
|
|
515
|
+
block.redacted = true;
|
|
516
|
+
block.thinkingSignature = "";
|
|
517
|
+
block.thinking += REDACTED_THINKING_PLACEHOLDER;
|
|
521
518
|
stream.push({
|
|
522
519
|
type: "thinking_delta",
|
|
523
520
|
contentIndex: thinkingIndex,
|
|
@@ -525,32 +522,29 @@ function handleContentBlockDelta(event, blocks, output, stream) {
|
|
|
525
522
|
partial: output,
|
|
526
523
|
});
|
|
527
524
|
}
|
|
528
|
-
|
|
529
|
-
|
|
525
|
+
open.redactedChunks ??= [];
|
|
526
|
+
open.redactedChunks.push(delta.reasoningContent.redactedContent);
|
|
530
527
|
}
|
|
531
528
|
}
|
|
532
529
|
}
|
|
533
530
|
}
|
|
534
531
|
/**
|
|
535
|
-
* Encodes buffered encrypted reasoning into `thinkingSignature
|
|
536
|
-
*
|
|
537
|
-
*
|
|
532
|
+
* Encodes buffered encrypted reasoning into `thinkingSignature`. The chunks stay in the stream state, never on the
|
|
533
|
+
* block: `Uint8Array` is not JSON and serializes to an index-keyed object roughly ten times the size of the base64
|
|
534
|
+
* payload.
|
|
538
535
|
*/
|
|
539
|
-
function flushRedactedContent(
|
|
540
|
-
|
|
536
|
+
function flushRedactedContent(open, output) {
|
|
537
|
+
const block = output.content[open.contentIndex];
|
|
538
|
+
if (block?.type !== "thinking" || !open.redactedChunks)
|
|
541
539
|
return;
|
|
542
|
-
block.thinkingSignature = bytesToBase64(
|
|
543
|
-
|
|
540
|
+
block.thinkingSignature = bytesToBase64(open.redactedChunks);
|
|
541
|
+
open.redactedChunks = undefined;
|
|
544
542
|
}
|
|
545
|
-
/**
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
delete block.index;
|
|
551
|
-
// partialJson is only a streaming scratch buffer; never persist it.
|
|
552
|
-
delete block.partialJson;
|
|
553
|
-
flushRedactedContent(block);
|
|
543
|
+
/** Ends the stream state of blocks a stream settled without stopping. */
|
|
544
|
+
function flushOpenBlocks(openBlocks, output) {
|
|
545
|
+
for (const open of openBlocks.values())
|
|
546
|
+
flushRedactedContent(open, output);
|
|
547
|
+
openBlocks.clear();
|
|
554
548
|
}
|
|
555
549
|
function handleMetadata(event, model, output) {
|
|
556
550
|
if (event.usage) {
|
|
@@ -563,25 +557,24 @@ function handleMetadata(event, model, output) {
|
|
|
563
557
|
calculateCost(model, output.usage);
|
|
564
558
|
}
|
|
565
559
|
}
|
|
566
|
-
function handleContentBlockStop(event,
|
|
567
|
-
const
|
|
568
|
-
|
|
569
|
-
if (!block)
|
|
560
|
+
function handleContentBlockStop(event, openBlocks, output, stream) {
|
|
561
|
+
const open = openBlocks.get(event.contentBlockIndex);
|
|
562
|
+
if (!open)
|
|
570
563
|
return;
|
|
571
|
-
delete
|
|
564
|
+
openBlocks.delete(event.contentBlockIndex);
|
|
565
|
+
const index = open.contentIndex;
|
|
566
|
+
const block = output.content[index];
|
|
572
567
|
switch (block.type) {
|
|
573
568
|
case "text":
|
|
574
569
|
stream.push({ type: "text_end", contentIndex: index, content: block.text, partial: output });
|
|
575
570
|
break;
|
|
576
571
|
case "thinking":
|
|
577
|
-
flushRedactedContent(
|
|
572
|
+
flushRedactedContent(open, output);
|
|
578
573
|
stream.push({ type: "thinking_end", contentIndex: index, content: block.thinking, partial: output });
|
|
579
574
|
break;
|
|
580
575
|
case "toolCall":
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
// carries parsed arguments.
|
|
584
|
-
delete block.partialJson;
|
|
576
|
+
if (open.arguments !== undefined)
|
|
577
|
+
Object.assign(block, open.arguments.finish());
|
|
585
578
|
stream.push({ type: "toolcall_end", contentIndex: index, toolCall: block, partial: output });
|
|
586
579
|
break;
|
|
587
580
|
}
|