@opencode/ai 2.2.0 → 2.2.1
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 +66 -62
- package/dist/effect.d.ts +1 -1
- package/dist/effect.js +1 -1
- package/dist/image-client.d.ts +45 -0
- package/dist/index.d.ts +38 -20
- package/dist/index.js +14 -2
- package/dist/media-client.d.ts +1 -0
- package/dist/media-client.js +1 -0
- package/dist/protocols/alibaba-chat.d.ts +39 -4
- package/dist/protocols/alibaba-responses.d.ts +20 -3
- package/dist/protocols/google-images.js +3 -1
- package/dist/protocols/meta-responses.d.ts +40 -6
- package/dist/protocols/meta-responses.js +1 -1
- package/dist/protocols/minimax-images.d.ts +25 -0
- package/dist/protocols/minimax-images.js +150 -0
- package/dist/protocols/open-responses.d.ts +182 -27
- package/dist/protocols/open-responses.js +108 -36
- package/dist/protocols/openai-chat.d.ts +438 -46
- package/dist/protocols/openai-chat.js +121 -39
- package/dist/protocols/openai-compatible-chat.d.ts +35 -4
- package/dist/protocols/openai-compatible-responses.d.ts +20 -3
- package/dist/protocols/openai-freeform.d.ts +47 -0
- package/dist/protocols/openai-freeform.js +54 -0
- package/dist/protocols/openai-images.d.ts +16 -1
- package/dist/protocols/openai-images.js +49 -17
- package/dist/protocols/openai-responses.d.ts +120 -18
- package/dist/protocols/openai-responses.js +17 -8
- package/dist/protocols/venice-chat.d.ts +39 -4
- package/dist/protocols/venice-chat.js +1 -1
- package/dist/protocols/xai-images.d.ts +6 -0
- package/dist/protocols/xai-images.js +4 -5
- package/dist/protocols/xai-responses.d.ts +20 -3
- package/dist/protocols/xai-responses.js +2 -1
- package/dist/protocols/zai-chat.d.ts +39 -4
- package/dist/provider-error.js +5 -4
- package/dist/provider-package.d.ts +2 -0
- package/dist/providers/alibaba.d.ts +58 -10
- package/dist/providers/amazon-bedrock-mantle.d.ts +66 -11
- package/dist/providers/amazon-bedrock-mantle.js +1 -1
- package/dist/providers/amazon-bedrock.d.ts +1 -1
- package/dist/providers/amazon-bedrock.js +1 -1
- package/dist/providers/anthropic-compatible.d.ts +1 -1
- package/dist/providers/anthropic.d.ts +1 -1
- package/dist/providers/anthropic.js +1 -1
- package/dist/providers/azure.d.ts +79 -15
- package/dist/providers/baseten.d.ts +74 -12
- package/dist/providers/baseten.js +1 -1
- package/dist/providers/cerebras.d.ts +73 -11
- package/dist/providers/cerebras.js +1 -1
- package/dist/providers/cloudflare-ai-gateway.d.ts +113 -17
- package/dist/providers/cloudflare-workers-ai.d.ts +73 -11
- package/dist/providers/cohere/chat.d.ts +1 -1
- package/dist/providers/cohere/chat.js +1 -1
- package/dist/providers/cohere.d.ts +73 -11
- package/dist/providers/cohere.js +1 -1
- package/dist/providers/deepinfra.d.ts +73 -11
- package/dist/providers/deepinfra.js +1 -1
- package/dist/providers/deepseek.d.ts +73 -11
- package/dist/providers/deepseek.js +1 -1
- package/dist/providers/digitalocean/responses.d.ts +4 -0
- package/dist/providers/digitalocean/responses.js +1 -0
- package/dist/providers/digitalocean.d.ts +320 -21
- package/dist/providers/digitalocean.js +18 -7
- package/dist/providers/fireworks.d.ts +73 -11
- package/dist/providers/fireworks.js +1 -1
- package/dist/providers/google/interactions.d.ts +1 -1
- package/dist/providers/google/interactions.js +1 -1
- package/dist/providers/google-vertex-chat.d.ts +36 -5
- package/dist/providers/google-vertex-chat.js +1 -1
- package/dist/providers/google-vertex-interactions.d.ts +1 -1
- package/dist/providers/google-vertex-interactions.js +1 -1
- package/dist/providers/google-vertex-messages.d.ts +1 -1
- package/dist/providers/google-vertex-messages.js +1 -1
- package/dist/providers/google-vertex-mistral.d.ts +1 -1
- package/dist/providers/google-vertex-mistral.js +1 -1
- package/dist/providers/google-vertex-responses.d.ts +21 -4
- package/dist/providers/google-vertex-responses.js +1 -1
- package/dist/providers/google-vertex.d.ts +2 -2
- package/dist/providers/google-vertex.js +1 -1
- package/dist/providers/google.d.ts +2 -2
- package/dist/providers/google.js +1 -1
- package/dist/providers/groq.d.ts +77 -11
- package/dist/providers/groq.js +1 -1
- package/dist/providers/index.d.ts +0 -4
- package/dist/providers/index.js +0 -4
- package/dist/providers/meta/images.d.ts +2 -0
- package/dist/providers/meta/images.js +1 -0
- package/dist/providers/meta.d.ts +61 -12
- package/dist/providers/meta.js +2 -1
- package/dist/providers/minimax/chat.d.ts +1 -1
- package/dist/providers/minimax/chat.js +1 -1
- package/dist/providers/minimax/images.d.ts +3 -0
- package/dist/providers/minimax/images.js +1 -0
- package/dist/providers/minimax/responses.d.ts +1 -1
- package/dist/providers/minimax/responses.js +1 -1
- package/dist/providers/minimax.d.ts +63 -9
- package/dist/providers/minimax.js +22 -9
- package/dist/providers/mistral.d.ts +3 -3
- package/dist/providers/mistral.js +1 -1
- package/dist/providers/moonshot/messages.d.ts +1 -1
- package/dist/providers/moonshot/messages.js +1 -1
- package/dist/providers/moonshot/responses.d.ts +1 -1
- package/dist/providers/moonshot/responses.js +1 -1
- package/dist/providers/moonshot.d.ts +56 -8
- package/dist/providers/moonshot.js +1 -1
- package/dist/providers/openai-compatible-responses.d.ts +21 -4
- package/dist/providers/openai-compatible.d.ts +38 -7
- package/dist/providers/openai-compatible.js +9 -2
- package/dist/providers/openai.d.ts +59 -11
- package/dist/providers/openai.js +4 -2
- package/dist/providers/opencode.d.ts +3 -3
- package/dist/providers/opencode.js +1 -1
- package/dist/providers/openrouter/responses.d.ts +4 -0
- package/dist/providers/openrouter/responses.js +1 -0
- package/dist/providers/openrouter.d.ts +356 -26
- package/dist/providers/openrouter.js +42 -13
- package/dist/providers/togetherai.d.ts +73 -11
- package/dist/providers/togetherai.js +1 -1
- package/dist/providers/typesafe-ai.d.ts +3 -3
- package/dist/providers/typesafe-ai.js +1 -1
- package/dist/providers/venice/responses.d.ts +4 -0
- package/dist/providers/venice/responses.js +1 -0
- package/dist/providers/venice.d.ts +242 -9
- package/dist/providers/venice.js +33 -17
- package/dist/providers/vercel-ai-gateway.d.ts +8 -8
- package/dist/providers/vercel-ai-gateway.js +12 -2
- package/dist/providers/xai.d.ts +39 -8
- package/dist/providers/xai.js +1 -1
- package/dist/providers/zai-coding-plan/messages.d.ts +1 -1
- package/dist/providers/zai-coding-plan/messages.js +1 -1
- package/dist/providers/zai-coding-plan/responses.d.ts +1 -1
- package/dist/providers/zai-coding-plan/responses.js +1 -1
- package/dist/providers/zai-coding-plan.d.ts +56 -8
- package/dist/providers/zai-coding-plan.js +1 -1
- package/dist/providers/zai.d.ts +37 -6
- package/dist/providers/zai.js +1 -1
- package/dist/route/auth.d.ts +1 -1
- package/dist/route/media-protocol.d.ts +2 -4
- package/dist/route/media.d.ts +1 -1
- package/dist/route/media.js +2 -4
- package/dist/schema/messages.d.ts +35 -1
- package/dist/schema/messages.js +20 -0
- package/dist/speech-client.d.ts +48 -0
- package/dist/testing/decision.d.ts +16 -0
- package/dist/testing/decision.js +20 -0
- package/dist/testing/image.d.ts +14 -0
- package/dist/testing/image.js +12 -0
- package/dist/testing/media.d.ts +18 -0
- package/dist/testing/media.js +36 -0
- package/dist/testing/script.d.ts +28 -0
- package/dist/testing/script.js +60 -0
- package/dist/testing/speech.d.ts +14 -0
- package/dist/testing/speech.js +12 -0
- package/dist/testing/transcription.d.ts +14 -0
- package/dist/testing/transcription.js +12 -0
- package/dist/testing/video.d.ts +14 -0
- package/dist/testing/video.js +12 -0
- package/dist/testing.d.ts +10 -21
- package/dist/testing.js +11 -54
- package/dist/tool.d.ts +12 -1
- package/dist/tool.js +10 -2
- package/dist/transcription-client.d.ts +64 -0
- package/dist/video-client.d.ts +41 -0
- package/package.json +22 -38
- package/dist/protocols/bfl-images.d.ts +0 -47
- package/dist/protocols/bfl-images.js +0 -155
- package/dist/protocols/fal-images.d.ts +0 -34
- package/dist/protocols/fal-images.js +0 -95
- package/dist/protocols/fal-video.d.ts +0 -29
- package/dist/protocols/fal-video.js +0 -73
- package/dist/protocols/replicate-images.d.ts +0 -28
- package/dist/protocols/replicate-images.js +0 -127
- package/dist/protocols/stability-images.d.ts +0 -44
- package/dist/protocols/stability-images.js +0 -131
- package/dist/protocols/utils/fal-queue.d.ts +0 -26
- package/dist/protocols/utils/fal-queue.js +0 -67
- package/dist/providers/black-forest-labs.d.ts +0 -21
- package/dist/providers/black-forest-labs.js +0 -23
- package/dist/providers/fal.d.ts +0 -24
- package/dist/providers/fal.js +0 -26
- package/dist/providers/replicate.d.ts +0 -21
- package/dist/providers/replicate.js +0 -17
- package/dist/providers/stability.d.ts +0 -23
- package/dist/providers/stability.js +0 -18
|
@@ -15,6 +15,7 @@ import { resolveParallelToolCalls } from "./open-responses.js";
|
|
|
15
15
|
import { OpenResponsesOptions } from "./utils/open-responses-options.js";
|
|
16
16
|
import { Lifecycle } from "./utils/lifecycle.js";
|
|
17
17
|
import { ToolStream } from "./utils/tool-stream.js";
|
|
18
|
+
import { OpenAIFreeform } from "./openai-freeform.js";
|
|
18
19
|
const ADAPTER = "openai-chat";
|
|
19
20
|
const RESERVED_REASONING_FIELDS = new Set(["role", "content", "refusal", "tool_calls"]);
|
|
20
21
|
export const DEFAULT_BASE_URL = "https://api.openai.com/v1";
|
|
@@ -29,16 +30,33 @@ const OpenAIChatCacheControl = Schema.Struct({
|
|
|
29
30
|
type: Schema.Literal("ephemeral"),
|
|
30
31
|
ttl: Schema.optional(Schema.String),
|
|
31
32
|
});
|
|
32
|
-
const OpenAIChatTool = Schema.
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
33
|
+
const OpenAIChatTool = Schema.Union([
|
|
34
|
+
Schema.Struct({
|
|
35
|
+
type: Schema.tag("function"),
|
|
36
|
+
function: Schema.Struct({
|
|
37
|
+
name: Schema.String,
|
|
38
|
+
description: Schema.String,
|
|
39
|
+
parameters: JsonObject,
|
|
40
|
+
strict: Schema.optional(Schema.Boolean),
|
|
41
|
+
}),
|
|
42
|
+
cache_control: Schema.optional(OpenAIChatCacheControl),
|
|
39
43
|
}),
|
|
40
|
-
|
|
41
|
-
|
|
44
|
+
Schema.Struct({
|
|
45
|
+
type: Schema.tag("custom"),
|
|
46
|
+
custom: Schema.Struct({
|
|
47
|
+
name: Schema.String,
|
|
48
|
+
description: Schema.String,
|
|
49
|
+
format: Schema.Union([
|
|
50
|
+
Schema.Struct({ type: Schema.tag("text") }),
|
|
51
|
+
Schema.Struct({
|
|
52
|
+
type: Schema.tag("grammar"),
|
|
53
|
+
grammar: Schema.Struct({ syntax: Schema.Literals(["lark", "regex"]), definition: Schema.String }),
|
|
54
|
+
}),
|
|
55
|
+
]),
|
|
56
|
+
}),
|
|
57
|
+
cache_control: Schema.optional(OpenAIChatCacheControl),
|
|
58
|
+
}),
|
|
59
|
+
]);
|
|
42
60
|
// Gemini's OpenAI-compatible surface carries thought signatures in tool call
|
|
43
61
|
// `extra_content` and rejects replayed parallel calls without them:
|
|
44
62
|
// https://ai.google.dev/gemini-api/docs/thinking#signatures
|
|
@@ -55,6 +73,14 @@ export const OpenAIChatAssistantToolCall = Schema.Struct({
|
|
|
55
73
|
}),
|
|
56
74
|
extra_content: Schema.optional(ExtraContent),
|
|
57
75
|
});
|
|
76
|
+
export const OpenAIChatAssistantCustomToolCall = Schema.Struct({
|
|
77
|
+
id: Schema.String,
|
|
78
|
+
type: Schema.tag("custom"),
|
|
79
|
+
custom: Schema.Struct({
|
|
80
|
+
name: Schema.String,
|
|
81
|
+
input: Schema.String,
|
|
82
|
+
}),
|
|
83
|
+
});
|
|
58
84
|
// `reasoning_details` carries two dialects. OpenRouter's `reasoning.*` entries
|
|
59
85
|
// must be replayed unmodified (`index` included), so they keep every field they
|
|
60
86
|
// arrived with. Kimi's OpenAI-compatible surface streams preserved thinking as
|
|
@@ -113,7 +139,7 @@ export const OpenAIChatMessage = Schema.Union([
|
|
|
113
139
|
Schema.StructWithRest(Schema.Struct({
|
|
114
140
|
role: Schema.Literal("assistant"),
|
|
115
141
|
content: Schema.NullOr(Schema.Union([Schema.String, Schema.Array(OpenAIChatTextContent)])),
|
|
116
|
-
tool_calls: optionalArray(OpenAIChatAssistantToolCall),
|
|
142
|
+
tool_calls: optionalArray(Schema.Union([OpenAIChatAssistantToolCall, OpenAIChatAssistantCustomToolCall])),
|
|
117
143
|
reasoning_content: Schema.optional(Schema.String),
|
|
118
144
|
reasoning: Schema.optional(Schema.String),
|
|
119
145
|
reasoning_text: Schema.optional(Schema.String),
|
|
@@ -131,6 +157,10 @@ const OpenAIChatToolChoice = Schema.Union([
|
|
|
131
157
|
type: Schema.tag("function"),
|
|
132
158
|
function: Schema.Struct({ name: Schema.String }),
|
|
133
159
|
}),
|
|
160
|
+
Schema.Struct({
|
|
161
|
+
type: Schema.tag("custom"),
|
|
162
|
+
custom: Schema.Struct({ name: Schema.String }),
|
|
163
|
+
}),
|
|
134
164
|
]);
|
|
135
165
|
export const bodyFields = {
|
|
136
166
|
model: Schema.String,
|
|
@@ -193,6 +223,7 @@ export const OpenAIChatToolCallDelta = Schema.Struct({
|
|
|
193
223
|
index: optionalNull(Schema.Number),
|
|
194
224
|
id: optionalNull(Schema.String),
|
|
195
225
|
function: optionalNull(OpenAIChatToolCallDeltaFunction),
|
|
226
|
+
custom: optionalNull(Schema.Struct({ name: optionalNull(Schema.String), input: optionalNull(Schema.String) })),
|
|
196
227
|
extra_content: optionalNull(Schema.Unknown),
|
|
197
228
|
});
|
|
198
229
|
export const OpenAIChatDelta = Schema.StructWithRest(Schema.Struct({
|
|
@@ -222,31 +253,57 @@ export const OpenAIChatEvent = Schema.StructWithRest(Schema.Struct({
|
|
|
222
253
|
}), [Schema.Record(Schema.String, Schema.Unknown)]);
|
|
223
254
|
const DONE = "[DONE]";
|
|
224
255
|
const OpenAIChatStreamEvent = Schema.Union([Schema.Literal(DONE), Protocol.jsonEvent(OpenAIChatEvent)]);
|
|
225
|
-
const lowerTool = (tool, options, supportsStrictMode) =>
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
}
|
|
235
|
-
|
|
256
|
+
const lowerTool = (tool, options, supportsStrictMode, custom) => {
|
|
257
|
+
if (custom) {
|
|
258
|
+
const grammar = OpenAIFreeform.grammar(custom.format);
|
|
259
|
+
return {
|
|
260
|
+
type: "custom",
|
|
261
|
+
custom: {
|
|
262
|
+
name: tool.name,
|
|
263
|
+
description: tool.description,
|
|
264
|
+
format: grammar ? { type: "grammar", grammar } : { type: "text" },
|
|
265
|
+
},
|
|
266
|
+
cache_control: options.cacheControl?.(tool.cache),
|
|
267
|
+
};
|
|
268
|
+
}
|
|
269
|
+
return {
|
|
270
|
+
type: "function",
|
|
271
|
+
function: {
|
|
272
|
+
name: tool.name,
|
|
273
|
+
description: tool.description,
|
|
274
|
+
parameters: tool.inputSchema,
|
|
275
|
+
...(supportsStrictMode ? { strict: false } : {}),
|
|
276
|
+
},
|
|
277
|
+
cache_control: options.cacheControl?.(tool.cache),
|
|
278
|
+
};
|
|
279
|
+
};
|
|
280
|
+
const lowerToolChoice = (toolChoice, custom) => ProviderShared.matchToolChoice("OpenAI Chat", toolChoice, {
|
|
236
281
|
auto: () => "auto",
|
|
237
282
|
none: () => "none",
|
|
238
283
|
required: () => "required",
|
|
239
|
-
tool: (name) => (
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
id: options.toolCallID?.(part.id) ?? part.id,
|
|
243
|
-
type: "function",
|
|
244
|
-
function: {
|
|
245
|
-
name: part.name,
|
|
246
|
-
arguments: ProviderShared.encodeJson(part.input === undefined ? {} : part.input),
|
|
247
|
-
},
|
|
248
|
-
extra_content: decodeExtraContent(part.providerMetadata?.[options.providerMetadataKey]?.extraContent),
|
|
284
|
+
tool: (name) => custom.has(name)
|
|
285
|
+
? { type: "custom", custom: { name } }
|
|
286
|
+
: { type: "function", function: { name } },
|
|
249
287
|
});
|
|
288
|
+
// A call replays as the type its tool is declared with in this request.
|
|
289
|
+
const lowerToolCall = (part, options) => {
|
|
290
|
+
const custom = options.custom.get(part.name);
|
|
291
|
+
if (custom)
|
|
292
|
+
return {
|
|
293
|
+
id: options.toolCallID?.(part.id) ?? part.id,
|
|
294
|
+
type: "custom",
|
|
295
|
+
custom: { name: part.name, input: OpenAIFreeform.toRaw(custom, part.input) },
|
|
296
|
+
};
|
|
297
|
+
return {
|
|
298
|
+
id: options.toolCallID?.(part.id) ?? part.id,
|
|
299
|
+
type: "function",
|
|
300
|
+
function: {
|
|
301
|
+
name: part.name,
|
|
302
|
+
arguments: ProviderShared.encodeJson(part.input === undefined ? {} : part.input),
|
|
303
|
+
},
|
|
304
|
+
extra_content: decodeExtraContent(part.providerMetadata?.[options.providerMetadataKey]?.extraContent),
|
|
305
|
+
};
|
|
306
|
+
};
|
|
250
307
|
const lowerMedia = Effect.fnUntraced(function* (part) {
|
|
251
308
|
// Chat Completions accepts PDFs, and no other documents, as inline `file` parts; file URLs are not supported.
|
|
252
309
|
if (part.media.mediaType.toLowerCase() === "application/pdf")
|
|
@@ -640,7 +697,8 @@ export const fromRequest = Effect.fn("OpenAIChat.fromRequest")(function* (reques
|
|
|
640
697
|
const zaiToolStream = request.model.compatibility?.zaiToolStream ?? detectZaiToolStream(provider, baseURL, request.model.id);
|
|
641
698
|
const hasHistory = hasToolHistory(request.messages);
|
|
642
699
|
const hasActiveTools = flattened.tools.length > 0;
|
|
643
|
-
const
|
|
700
|
+
const custom = OpenAIFreeform.tools(request);
|
|
701
|
+
const lowering = { ...options, cacheControl: options.cacheControl ?? declaredCacheControl(request), custom };
|
|
644
702
|
return {
|
|
645
703
|
model: request.model.id,
|
|
646
704
|
messages: yield* lowerMessages(flattened.request, lowering),
|
|
@@ -648,8 +706,8 @@ export const fromRequest = Effect.fn("OpenAIChat.fromRequest")(function* (reques
|
|
|
648
706
|
? hasHistory
|
|
649
707
|
? []
|
|
650
708
|
: undefined
|
|
651
|
-
: flattened.tools.map((tool) => lowerTool(tool, lowering, supportsStrictMode)),
|
|
652
|
-
tool_choice: hasActiveTools && request.toolChoice ? yield* lowerToolChoice(request.toolChoice) : undefined,
|
|
709
|
+
: flattened.tools.map((tool) => lowerTool(tool, lowering, supportsStrictMode, custom.get(tool.name))),
|
|
710
|
+
tool_choice: hasActiveTools && request.toolChoice ? yield* lowerToolChoice(request.toolChoice, custom) : undefined,
|
|
653
711
|
stream: true,
|
|
654
712
|
...((request.model.compatibility?.streamUsage ?? true) ? { stream_options: { include_usage: true } } : {}),
|
|
655
713
|
...(zaiToolStream && hasActiveTools ? { tool_stream: true } : {}),
|
|
@@ -855,12 +913,16 @@ const step = (state, event) => Effect.gen(function* () {
|
|
|
855
913
|
const toolDeltas = delta?.tool_calls ?? [];
|
|
856
914
|
let tools = state.tools;
|
|
857
915
|
let pendingTools = state.pendingTools;
|
|
916
|
+
let customCalls = state.customCalls;
|
|
858
917
|
let latestToolIndex = state.latestToolIndex;
|
|
859
918
|
let nextToolIndex = state.nextToolIndex;
|
|
860
919
|
let lifecycle = state.lifecycle;
|
|
861
920
|
const reasoning = reasoningDelta(delta, state.reasoningField);
|
|
862
921
|
if (state.finishReason !== undefined &&
|
|
863
|
-
toolDeltas.some((tool) => Boolean(tool.id) ||
|
|
922
|
+
toolDeltas.some((tool) => Boolean(tool.id) ||
|
|
923
|
+
Boolean(tool.function?.name) ||
|
|
924
|
+
Boolean(tool.function?.arguments) ||
|
|
925
|
+
Boolean(tool.custom?.input)))
|
|
864
926
|
return yield* ProviderShared.eventError(ADAPTER, "OpenAI Chat received content after the finish reason", ProviderShared.encodeJson(event));
|
|
865
927
|
const reasoningField = state.reasoningField ?? reasoning?.field;
|
|
866
928
|
const reasoningTextObserved = state.reasoningTextObserved || reasoning !== undefined;
|
|
@@ -898,8 +960,18 @@ const step = (state, event) => Effect.gen(function* () {
|
|
|
898
960
|
const current = tools[index];
|
|
899
961
|
const pending = pendingTools[index];
|
|
900
962
|
const id = current?.id ?? pending?.id ?? (tool.id || undefined);
|
|
901
|
-
const name = current?.name ?? pending?.name ?? (tool.function?.name || undefined);
|
|
902
|
-
|
|
963
|
+
const name = current?.name ?? pending?.name ?? (tool.function?.name || tool.custom?.name || undefined);
|
|
964
|
+
// Custom calls stream raw text; re-encode it as JSON arguments for the call's field.
|
|
965
|
+
const opening = Boolean(tool.custom) && !customCalls.has(index);
|
|
966
|
+
if (opening)
|
|
967
|
+
customCalls = new Set([...customCalls, index]);
|
|
968
|
+
const raw = tool.custom?.input ?? "";
|
|
969
|
+
const fragment = tool.custom
|
|
970
|
+
? opening
|
|
971
|
+
? OpenAIFreeform.streamStart(state.custom.get(name ?? ""), raw)
|
|
972
|
+
: OpenAIFreeform.streamDelta(raw)
|
|
973
|
+
: (tool.function?.arguments ?? "");
|
|
974
|
+
const text = `${pending?.input ?? ""}${fragment}`;
|
|
903
975
|
const extraContent = pending?.extraContent ?? decodeExtraContent(tool.extra_content);
|
|
904
976
|
latestToolIndex = index;
|
|
905
977
|
nextToolIndex = Math.max(nextToolIndex, index + 1);
|
|
@@ -945,12 +1017,14 @@ const step = (state, event) => Effect.gen(function* () {
|
|
|
945
1017
|
!incompleteTools &&
|
|
946
1018
|
state.finishReason === undefined &&
|
|
947
1019
|
Object.keys(tools).length > 0
|
|
948
|
-
? yield*
|
|
1020
|
+
? yield* finishTools(tools, customCalls)
|
|
949
1021
|
: undefined;
|
|
950
1022
|
return [
|
|
951
1023
|
{
|
|
952
1024
|
providerMetadataKey: state.providerMetadataKey,
|
|
953
1025
|
tools: finished?.tools ?? tools,
|
|
1026
|
+
custom: state.custom,
|
|
1027
|
+
customCalls,
|
|
954
1028
|
pendingTools,
|
|
955
1029
|
toolCallEvents: finished?.events ?? state.toolCallEvents,
|
|
956
1030
|
usage,
|
|
@@ -968,6 +1042,12 @@ const step = (state, event) => Effect.gen(function* () {
|
|
|
968
1042
|
events,
|
|
969
1043
|
];
|
|
970
1044
|
});
|
|
1045
|
+
// Custom calls close their JSON fragment before every pending call finishes.
|
|
1046
|
+
const finishTools = Effect.fnUntraced(function* (tools, customCalls) {
|
|
1047
|
+
const closed = OpenAIFreeform.close(tools, customCalls);
|
|
1048
|
+
const finished = yield* ToolStream.finishAll(ADAPTER, closed.tools);
|
|
1049
|
+
return { tools: finished.tools, events: [...closed.events, ...finished.events] };
|
|
1050
|
+
});
|
|
971
1051
|
export const finishEvents = Effect.fnUntraced(function* (state) {
|
|
972
1052
|
if (state.finishReason === undefined && state.requireFinishReason)
|
|
973
1053
|
return yield* new AIError({
|
|
@@ -979,7 +1059,7 @@ export const finishEvents = Effect.fnUntraced(function* (state) {
|
|
|
979
1059
|
});
|
|
980
1060
|
const events = [];
|
|
981
1061
|
const toolCallEvents = state.finishReason === undefined && Object.keys(state.tools).length > 0
|
|
982
|
-
? (yield*
|
|
1062
|
+
? (yield* finishTools(state.tools, state.customCalls)).events
|
|
983
1063
|
: state.toolCallEvents;
|
|
984
1064
|
const hasToolCalls = toolCallEvents.length > 0;
|
|
985
1065
|
const reason = state.finishReason
|
|
@@ -1020,6 +1100,8 @@ export const protocol = Protocol.make({
|
|
|
1020
1100
|
initial: (request) => ({
|
|
1021
1101
|
providerMetadataKey: request.model.route.providerMetadataKey ?? request.model.provider,
|
|
1022
1102
|
tools: ToolStream.empty(),
|
|
1103
|
+
custom: OpenAIFreeform.tools(request),
|
|
1104
|
+
customCalls: new Set(),
|
|
1023
1105
|
pendingTools: {},
|
|
1024
1106
|
toolCallEvents: [],
|
|
1025
1107
|
lifecycle: Lifecycle.initial(),
|
|
@@ -82,7 +82,14 @@ export declare const route: Route<{
|
|
|
82
82
|
readonly ttl?: string | undefined;
|
|
83
83
|
} | undefined;
|
|
84
84
|
}[] | null;
|
|
85
|
-
readonly tool_calls?: readonly {
|
|
85
|
+
readonly tool_calls?: readonly ({
|
|
86
|
+
readonly id: string;
|
|
87
|
+
readonly type: "custom";
|
|
88
|
+
readonly custom: {
|
|
89
|
+
readonly name: string;
|
|
90
|
+
readonly input: string;
|
|
91
|
+
};
|
|
92
|
+
} | {
|
|
86
93
|
readonly id: string;
|
|
87
94
|
readonly type: "function";
|
|
88
95
|
readonly function: {
|
|
@@ -94,13 +101,13 @@ export declare const route: Route<{
|
|
|
94
101
|
readonly thought_signature: string;
|
|
95
102
|
};
|
|
96
103
|
} | undefined;
|
|
97
|
-
}[] | undefined;
|
|
104
|
+
})[] | undefined;
|
|
98
105
|
readonly reasoning_content?: string | undefined;
|
|
99
106
|
readonly reasoning?: string | undefined;
|
|
100
107
|
readonly reasoning_text?: string | undefined;
|
|
101
108
|
readonly reasoning_details?: unknown;
|
|
102
109
|
})[];
|
|
103
|
-
readonly tools?: readonly {
|
|
110
|
+
readonly tools?: readonly ({
|
|
104
111
|
readonly type: "function";
|
|
105
112
|
readonly function: {
|
|
106
113
|
readonly name: string;
|
|
@@ -114,12 +121,36 @@ export declare const route: Route<{
|
|
|
114
121
|
readonly type: "ephemeral";
|
|
115
122
|
readonly ttl?: string | undefined;
|
|
116
123
|
} | undefined;
|
|
117
|
-
}
|
|
124
|
+
} | {
|
|
125
|
+
readonly type: "custom";
|
|
126
|
+
readonly custom: {
|
|
127
|
+
readonly name: string;
|
|
128
|
+
readonly description: string;
|
|
129
|
+
readonly format: {
|
|
130
|
+
readonly type: "text";
|
|
131
|
+
} | {
|
|
132
|
+
readonly type: "grammar";
|
|
133
|
+
readonly grammar: {
|
|
134
|
+
readonly syntax: "lark" | "regex";
|
|
135
|
+
readonly definition: string;
|
|
136
|
+
};
|
|
137
|
+
};
|
|
138
|
+
};
|
|
139
|
+
readonly cache_control?: {
|
|
140
|
+
readonly type: "ephemeral";
|
|
141
|
+
readonly ttl?: string | undefined;
|
|
142
|
+
} | undefined;
|
|
143
|
+
})[] | undefined;
|
|
118
144
|
readonly tool_choice?: "auto" | "none" | "required" | {
|
|
119
145
|
readonly type: "function";
|
|
120
146
|
readonly function: {
|
|
121
147
|
readonly name: string;
|
|
122
148
|
};
|
|
149
|
+
} | {
|
|
150
|
+
readonly type: "custom";
|
|
151
|
+
readonly custom: {
|
|
152
|
+
readonly name: string;
|
|
153
|
+
};
|
|
123
154
|
} | undefined;
|
|
124
155
|
readonly stream: true;
|
|
125
156
|
readonly stream_options?: {
|
|
@@ -20,7 +20,7 @@ export declare const route: Route<{
|
|
|
20
20
|
readonly role: "developer";
|
|
21
21
|
readonly content: string;
|
|
22
22
|
} | {
|
|
23
|
-
readonly type: "function_call_output";
|
|
23
|
+
readonly type: "custom_tool_call_output" | "function_call_output";
|
|
24
24
|
readonly call_id: string;
|
|
25
25
|
readonly output: string | readonly ({
|
|
26
26
|
readonly type: "input_text";
|
|
@@ -90,6 +90,12 @@ export declare const route: Route<{
|
|
|
90
90
|
readonly name: string;
|
|
91
91
|
readonly namespace?: string | undefined;
|
|
92
92
|
readonly arguments: string;
|
|
93
|
+
} | {
|
|
94
|
+
readonly type: "custom_tool_call";
|
|
95
|
+
readonly id?: string | undefined;
|
|
96
|
+
readonly call_id: string;
|
|
97
|
+
readonly name: string;
|
|
98
|
+
readonly input: string;
|
|
93
99
|
} | {
|
|
94
100
|
readonly [x: string]: unknown;
|
|
95
101
|
readonly type: "web_search_call";
|
|
@@ -134,6 +140,17 @@ export declare const route: Route<{
|
|
|
134
140
|
})[];
|
|
135
141
|
readonly instructions?: string | undefined;
|
|
136
142
|
readonly tools?: readonly ({
|
|
143
|
+
readonly type: "custom";
|
|
144
|
+
readonly name: string;
|
|
145
|
+
readonly description: string;
|
|
146
|
+
readonly format: {
|
|
147
|
+
readonly type: "text";
|
|
148
|
+
} | {
|
|
149
|
+
readonly type: "grammar";
|
|
150
|
+
readonly syntax: "lark" | "regex";
|
|
151
|
+
readonly definition: string;
|
|
152
|
+
};
|
|
153
|
+
} | {
|
|
137
154
|
readonly type: "function";
|
|
138
155
|
readonly name: string;
|
|
139
156
|
readonly description: string;
|
|
@@ -146,13 +163,13 @@ export declare const route: Route<{
|
|
|
146
163
|
readonly type: string;
|
|
147
164
|
})[] | undefined;
|
|
148
165
|
readonly tool_choice?: "auto" | "none" | "required" | {
|
|
149
|
-
readonly type: "function";
|
|
166
|
+
readonly type: "custom" | "function";
|
|
150
167
|
readonly name: string;
|
|
151
168
|
} | {
|
|
152
169
|
readonly type: "allowed_tools";
|
|
153
170
|
readonly mode: "auto" | "none" | "required";
|
|
154
171
|
readonly tools: readonly {
|
|
155
|
-
readonly type: "function";
|
|
172
|
+
readonly type: "custom" | "function";
|
|
156
173
|
readonly name: string;
|
|
157
174
|
}[];
|
|
158
175
|
} | undefined;
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { ToolDefinition, type Freeform, type LLMEvent, type LLMRequest, type ProviderMetadata } from "../schema/index.js";
|
|
2
|
+
import { ToolStream } from "./utils/tool-stream.js";
|
|
3
|
+
export interface Custom {
|
|
4
|
+
/** The tool's one required string parameter, which the raw text fills. */
|
|
5
|
+
readonly field: string;
|
|
6
|
+
readonly format: Freeform;
|
|
7
|
+
}
|
|
8
|
+
/** How a tool goes out: a custom tool when it declares `freeform` with one required string parameter. */
|
|
9
|
+
export declare const resolve: (tool: ToolDefinition) => Custom | undefined;
|
|
10
|
+
/** Custom tools in this request, by flattened tool name. */
|
|
11
|
+
export declare const tools: (request: LLMRequest) => ReadonlyMap<string, Custom>;
|
|
12
|
+
/** The format's grammar, or `undefined` for unconstrained text. */
|
|
13
|
+
export declare const grammar: (format: Freeform) => {
|
|
14
|
+
syntax: "lark" | "regex";
|
|
15
|
+
definition: string;
|
|
16
|
+
} | undefined;
|
|
17
|
+
/** Raw text replayed for a past call: its field, or the whole input as JSON when the field is not a string. */
|
|
18
|
+
export declare const toRaw: (custom: Custom, input: unknown) => string;
|
|
19
|
+
/** JSON arguments for a raw call. A call to an undeclared custom tool fills `input`. */
|
|
20
|
+
export declare const fromRaw: (custom: Custom | undefined, raw: string) => string;
|
|
21
|
+
/** The opening fragment, `{"field":"`, followed by the escaped first text. */
|
|
22
|
+
export declare const streamStart: (custom: Custom | undefined, raw?: string) => string;
|
|
23
|
+
export declare const streamDelta: (raw: string) => string;
|
|
24
|
+
/**
|
|
25
|
+
* Close the JSON fragment of each open custom call. With a `final` raw input, a call only closes when its fragments
|
|
26
|
+
* decode to it, so an authoritative final input that disagrees replaces the streamed one instead.
|
|
27
|
+
*/
|
|
28
|
+
export declare const close: <K extends string | number>(tools: ToolStream.State<K>, keys: Iterable<K>, final?: {
|
|
29
|
+
readonly custom: Custom | undefined;
|
|
30
|
+
readonly raw: string;
|
|
31
|
+
}) => {
|
|
32
|
+
readonly tools: ToolStream.State<K>;
|
|
33
|
+
readonly events: ReadonlyArray<LLMEvent>;
|
|
34
|
+
};
|
|
35
|
+
/** Provider metadata marking a call that arrived as a custom tool call. */
|
|
36
|
+
export declare const metadata: (itemId: string, custom: boolean) => {
|
|
37
|
+
itemId: string;
|
|
38
|
+
custom: boolean;
|
|
39
|
+
} | {
|
|
40
|
+
itemId: string;
|
|
41
|
+
};
|
|
42
|
+
/**
|
|
43
|
+
* Whether a replayed call keeps its item id: only when it goes out as the type it arrived as, since OpenAI item ids
|
|
44
|
+
* name their type (`fc_`, `ctc_`). The `call_id` always survives.
|
|
45
|
+
*/
|
|
46
|
+
export declare const keepsItemID: (providerMetadata: ProviderMetadata | undefined, key: string, custom: boolean) => boolean;
|
|
47
|
+
export * as OpenAIFreeform from "./openai-freeform.js";
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { Option, Schema } from "effect";
|
|
2
|
+
import { ToolDefinition, } from "../schema/index.js";
|
|
3
|
+
import { encodeJson, flattenTools, isRecord } from "./shared.js";
|
|
4
|
+
import { ToolStream } from "./utils/tool-stream.js";
|
|
5
|
+
/** How a tool goes out: a custom tool when it declares `freeform` with one required string parameter. */
|
|
6
|
+
export const resolve = (tool) => {
|
|
7
|
+
const field = ToolDefinition.freeformField(tool.inputSchema);
|
|
8
|
+
if (tool.freeform === undefined || tool.native !== undefined || field === undefined)
|
|
9
|
+
return undefined;
|
|
10
|
+
return { field, format: tool.freeform };
|
|
11
|
+
};
|
|
12
|
+
/** Custom tools in this request, by flattened tool name. */
|
|
13
|
+
export const tools = (request) => new Map(flattenTools(request.tools).flatMap((tool) => {
|
|
14
|
+
const custom = resolve(tool);
|
|
15
|
+
return custom ? [[tool.name, custom]] : [];
|
|
16
|
+
}));
|
|
17
|
+
/** The format's grammar, or `undefined` for unconstrained text. */
|
|
18
|
+
export const grammar = (format) => format.type === "text"
|
|
19
|
+
? undefined
|
|
20
|
+
: { syntax: format.type, definition: format.type === "regex" ? format.pattern : format.grammar };
|
|
21
|
+
/** Raw text replayed for a past call: its field, or the whole input as JSON when the field is not a string. */
|
|
22
|
+
export const toRaw = (custom, input) => {
|
|
23
|
+
const value = isRecord(input) ? input[custom.field] : undefined;
|
|
24
|
+
return typeof value === "string" ? value : encodeJson(input ?? {});
|
|
25
|
+
};
|
|
26
|
+
/** JSON arguments for a raw call. A call to an undeclared custom tool fills `input`. */
|
|
27
|
+
export const fromRaw = (custom, raw) => encodeJson({ [field(custom)]: raw });
|
|
28
|
+
const field = (custom) => custom?.field ?? "input";
|
|
29
|
+
// Streamed raw text is re-encoded as JSON-argument fragments, so the tool-input events match a JSON tool's.
|
|
30
|
+
/** The opening fragment, `{"field":"`, followed by the escaped first text. */
|
|
31
|
+
export const streamStart = (custom, raw = "") => `{${encodeJson(field(custom))}:"${streamDelta(raw)}`;
|
|
32
|
+
export const streamDelta = (raw) => encodeJson(raw).slice(1, -1);
|
|
33
|
+
const STREAM_END = '"}';
|
|
34
|
+
const decodeArguments = Schema.decodeUnknownOption(Schema.fromJsonString(Schema.Record(Schema.String, Schema.Unknown)));
|
|
35
|
+
/**
|
|
36
|
+
* Close the JSON fragment of each open custom call. With a `final` raw input, a call only closes when its fragments
|
|
37
|
+
* decode to it, so an authoritative final input that disagrees replaces the streamed one instead.
|
|
38
|
+
*/
|
|
39
|
+
export const close = (tools, keys, final) => [...keys].reduce((result, key) => {
|
|
40
|
+
const pending = result.tools[key];
|
|
41
|
+
const streamed = pending && Option.getOrUndefined(decodeArguments(`${pending.input}${STREAM_END}`));
|
|
42
|
+
if (!pending || (final !== undefined && streamed?.[field(final.custom)] !== final.raw))
|
|
43
|
+
return result;
|
|
44
|
+
const appended = ToolStream.append(result.tools, key, STREAM_END);
|
|
45
|
+
return appended ? { tools: appended.tools, events: [...result.events, ...appended.events] } : result;
|
|
46
|
+
}, { tools, events: [] });
|
|
47
|
+
/** Provider metadata marking a call that arrived as a custom tool call. */
|
|
48
|
+
export const metadata = (itemId, custom) => (custom ? { itemId, custom: true } : { itemId });
|
|
49
|
+
/**
|
|
50
|
+
* Whether a replayed call keeps its item id: only when it goes out as the type it arrived as, since OpenAI item ids
|
|
51
|
+
* name their type (`fc_`, `ctc_`). The `call_id` always survives.
|
|
52
|
+
*/
|
|
53
|
+
export const keepsItemID = (providerMetadata, key, custom) => (providerMetadata?.[key]?.custom === true) === custom;
|
|
54
|
+
export * as OpenAIFreeform from "./openai-freeform.js";
|
|
@@ -41,6 +41,15 @@ interface State {
|
|
|
41
41
|
readonly background?: string;
|
|
42
42
|
readonly usage?: Schema.Schema.Type<typeof Usage>;
|
|
43
43
|
}
|
|
44
|
+
/** The body fields `aspect` and `resolution` lower to; gateways that read other fields than `size` pass their own. */
|
|
45
|
+
export type OpenAIImageSizing = (request: Request) => Record<string, unknown> | undefined;
|
|
46
|
+
/**
|
|
47
|
+
* How input images travel. `edits` is OpenAI's `/images/edits`, uploading owned bytes as multipart; `json-edits` sends
|
|
48
|
+
* every edit there as JSON with data URLs, for hosts that reject multipart (the ChatGPT backend); `references` keeps
|
|
49
|
+
* every request on `/images/generations` and lists them as `input_references`, for gateways without an edits endpoint
|
|
50
|
+
* (OpenRouter).
|
|
51
|
+
*/
|
|
52
|
+
export type OpenAIImageInput = "edits" | "json-edits" | "references";
|
|
44
53
|
export declare const protocol: MediaProtocol.Streamed<Request, {
|
|
45
54
|
readonly type: "image-partial";
|
|
46
55
|
readonly index: number;
|
|
@@ -86,9 +95,14 @@ export declare const protocol: MediaProtocol.Streamed<Request, {
|
|
|
86
95
|
};
|
|
87
96
|
} | undefined;
|
|
88
97
|
}, Frame, State>;
|
|
89
|
-
/**
|
|
98
|
+
/**
|
|
99
|
+
* `provider` attributes models, refs, and metadata to an OpenAI-compatible host; it defaults to `openai`. `sizing`
|
|
100
|
+
* replaces the `size` that `aspect` and `resolution` lower to, and `imageInput` how input images are sent.
|
|
101
|
+
*/
|
|
90
102
|
export declare const model: (input: MediaRoute.ModelInput & {
|
|
91
103
|
readonly provider?: string;
|
|
104
|
+
readonly sizing?: OpenAIImageSizing;
|
|
105
|
+
readonly imageInput?: OpenAIImageInput;
|
|
92
106
|
}) => ImageModel<OpenAIImageOptions>;
|
|
93
107
|
export declare const OpenAIImages: {
|
|
94
108
|
readonly protocol: MediaProtocol.Streamed<Request, {
|
|
@@ -137,5 +151,6 @@ export declare const OpenAIImages: {
|
|
|
137
151
|
} | undefined;
|
|
138
152
|
}, Frame, State>;
|
|
139
153
|
readonly model: typeof model;
|
|
154
|
+
readonly pixelSize: OpenAIImageSizing;
|
|
140
155
|
};
|
|
141
156
|
export {};
|