Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions packages/ai/src/api/anthropic-messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ import type {
import { appendAssistantMessageDiagnostic } from "../utils/diagnostics.ts";
import { AssistantMessageEventStream } from "../utils/event-stream.ts";
import { headersToRecord } from "../utils/headers.ts";
import { cacheJsonSerialization } from "../utils/json-body.ts";
import { parseJsonWithRepair, parseStreamingJsonWhileStreaming, parseToolCallArguments } from "../utils/json-parse.ts";
import { getPiUserAgent } from "../utils/pi-user-agent.ts";
import { getProviderEnvValue } from "../utils/provider-env.ts";
import { retryProviderRequest } from "../utils/provider-retry.ts";
import { cacheRequestBody } from "../utils/request-body.ts";
import { sanitizeSurrogates } from "../utils/sanitize-unicode.ts";
import { iterateSseMessages } from "../utils/sse.ts";
import { getSystemMessageText, renderSystemMessageUpdate } from "../utils/text.ts";
Expand Down Expand Up @@ -514,7 +514,8 @@ export const stream: StreamFunction<"anthropic-messages", AnthropicOptions> = (
if (nextParams !== undefined) {
params = { ...(nextParams as MessageCreateParamsStreaming), stream: true };
}
cacheJsonSerialization(params.messages);
// A client passed in by the caller is not wrapped.
cacheRequestBody(options?.client ? undefined : client, params.messages, "messages", options?.sessionId);
try {
response = await retryProviderRequest(
() => client.beta.messages.create(params, requestOptions).asResponse(),
Expand Down
4 changes: 2 additions & 2 deletions packages/ai/src/api/azure-openai-responses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import type { Api, AssistantMessage, Model, SimpleStreamOptions, StreamFunction,
import { formatProviderError, normalizeProviderError } from "../utils/error-body.ts";
import { AssistantMessageEventStream } from "../utils/event-stream.ts";
import { headersToRecord } from "../utils/headers.ts";
import { cacheJsonSerialization } from "../utils/json-body.ts";
import { getPiUserAgent } from "../utils/pi-user-agent.ts";
import { retryProviderRequest } from "../utils/provider-retry.ts";
import { cacheRequestBody } from "../utils/request-body.ts";
import { getDeclaredTools, resolveTranscript, resolveTranscriptTools } from "../utils/transcript.ts";
import { type AzureEndpointOptions, resolveAzureConfig, resolveDeploymentName } from "./azure-openai-config.ts";
import { createGrammarToolInputProperties } from "./constrained-sampling.ts";
Expand Down Expand Up @@ -80,7 +80,7 @@ export const stream: StreamFunction<"azure-openai-responses", AzureOpenAIRespons
if (nextParams !== undefined) {
params = nextParams as ResponseCreateParamsStreaming;
}
cacheJsonSerialization(params.input);
cacheRequestBody(client, params.input, "input", options?.sessionId);
const requestOptions = {
...(options?.signal ? { signal: options.signal } : {}),
...(options?.timeoutMs !== undefined ? { timeout: options.timeoutMs } : {}),
Expand Down
4 changes: 2 additions & 2 deletions packages/ai/src/api/openai-completions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ import { formatProviderError, normalizeProviderError } from "../utils/error-body
import { AssistantMessageEventStream } from "../utils/event-stream.ts";
import { shortHash } from "../utils/hash.ts";
import { headersToRecord } from "../utils/headers.ts";
import { cacheJsonSerialization } from "../utils/json-body.ts";
import { parseStreamingJsonWhileStreaming, parseToolCallArguments } from "../utils/json-parse.ts";
import { getPiUserAgent } from "../utils/pi-user-agent.ts";
import { getProviderEnvValue } from "../utils/provider-env.ts";
import { retryProviderRequest } from "../utils/provider-retry.ts";
import { cacheRequestBody } from "../utils/request-body.ts";
import { sanitizeSurrogates } from "../utils/sanitize-unicode.ts";
import { getSystemMessageText, renderSystemMessageUpdate } from "../utils/text.ts";
import {
Expand Down Expand Up @@ -370,7 +370,7 @@ export const stream: StreamFunction<"openai-completions", OpenAICompletionsOptio
if (nextParams !== undefined) {
params = nextParams as OpenAI.Chat.Completions.ChatCompletionCreateParamsStreaming;
}
cacheJsonSerialization(params.messages);
cacheRequestBody(client, params.messages, "messages", options?.sessionId);
const requestOptions = {
...(options?.signal ? { signal: options.signal } : {}),
...(options?.timeoutMs !== undefined ? { timeout: options.timeoutMs } : {}),
Expand Down
4 changes: 2 additions & 2 deletions packages/ai/src/api/openai-responses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ import type {
import { formatProviderError, normalizeProviderError } from "../utils/error-body.ts";
import { AssistantMessageEventStream } from "../utils/event-stream.ts";
import { headersToRecord } from "../utils/headers.ts";
import { cacheJsonSerialization } from "../utils/json-body.ts";
import { getPiUserAgent } from "../utils/pi-user-agent.ts";
import { getProviderEnvValue } from "../utils/provider-env.ts";
import { retryProviderRequest } from "../utils/provider-retry.ts";
import { cacheRequestBody } from "../utils/request-body.ts";
import { getDeclaredTools, resolveTranscript, resolveTranscriptTools } from "../utils/transcript.ts";
import { createGrammarToolInputProperties } from "./constrained-sampling.ts";
import { buildCopilotDynamicHeaders, hasCopilotVisionInput } from "./github-copilot-headers.ts";
Expand Down Expand Up @@ -206,7 +206,7 @@ export const stream: StreamFunction<"openai-responses", OpenAIResponsesOptions>
if (nextParams !== undefined) {
params = nextParams as ResponseCreateParamsStreaming;
}
cacheJsonSerialization(params.input);
cacheRequestBody(client, params.input, "input", options?.sessionId);
const requestOptions = {
...(options?.signal ? { signal: options.signal } : {}),
...(options?.timeoutMs !== undefined ? { timeout: options.timeoutMs } : {}),
Expand Down
28 changes: 17 additions & 11 deletions packages/ai/src/utils/json-body.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,15 +99,29 @@ function substituteItems(
return out;
}

/**
* The toJSON cacheJsonSerialization gives an array: a copy of it with long strings replaced by their cached raw JSON,
* which JSON.stringify serializes to the same text as the array.
*/
export function cachedArrayToJSON(this: unknown[]): unknown {
try {
if (!rawJSON) return this;
startGeneration();
return substituteItems(this, rawJSON, new Set());
} catch {
// JSON.stringify serializes what toJSON returns without calling it again: the array as it is.
return this;
}
}

/**
* Makes JSON.stringify of `items` (a request's messages) reuse the serialized form of long strings serialized before,
* by defining a non-enumerable toJSON on the array. Does nothing to anything but an extensible plain array without a
* toJSON (an onPayload hook may return a frozen one). Never throws.
*/
export function cacheJsonSerialization(items: unknown): void {
const toRaw = rawJSON;
if (
!toRaw ||
!rawJSON ||
!isRawJSON ||
!Array.isArray(items) ||
Object.getPrototypeOf(items) !== Array.prototype ||
Expand All @@ -118,15 +132,7 @@ export function cacheJsonSerialization(items: unknown): void {
}
try {
Object.defineProperty(items, "toJSON", {
value(this: unknown[]): unknown {
try {
startGeneration();
return substituteItems(this, toRaw, new Set());
} catch {
// JSON.stringify serializes what toJSON returns without calling it again: the array as it is.
return this;
}
},
value: cachedArrayToJSON,
configurable: true,
writable: true,
enumerable: false,
Expand Down
Loading
Loading