@opencode/ai 0.0.0-beta-19275
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 +682 -0
- package/dist/cache-policy.d.ts +2 -0
- package/dist/cache-policy.js +129 -0
- package/dist/image-client.d.ts +19 -0
- package/dist/image-client.js +28 -0
- package/dist/image.d.ts +110 -0
- package/dist/image.js +103 -0
- package/dist/index.d.ts +18 -0
- package/dist/index.js +12 -0
- package/dist/llm.d.ts +252 -0
- package/dist/llm.js +74 -0
- package/dist/protocols/anthropic-messages.d.ts +957 -0
- package/dist/protocols/anthropic-messages.js +1414 -0
- package/dist/protocols/bedrock-converse.d.ts +499 -0
- package/dist/protocols/bedrock-converse.js +643 -0
- package/dist/protocols/bedrock-event-stream.d.ts +9 -0
- package/dist/protocols/bedrock-event-stream.js +97 -0
- package/dist/protocols/gemini.d.ts +285 -0
- package/dist/protocols/gemini.js +671 -0
- package/dist/protocols/google-images.d.ts +30 -0
- package/dist/protocols/google-images.js +181 -0
- package/dist/protocols/index.d.ts +12 -0
- package/dist/protocols/index.js +12 -0
- package/dist/protocols/meta-images.d.ts +26 -0
- package/dist/protocols/meta-images.js +89 -0
- package/dist/protocols/meta-messages.d.ts +350 -0
- package/dist/protocols/meta-messages.js +41 -0
- package/dist/protocols/meta-responses.d.ts +473 -0
- package/dist/protocols/meta-responses.js +173 -0
- package/dist/protocols/mistral-chat.d.ts +540 -0
- package/dist/protocols/mistral-chat.js +624 -0
- package/dist/protocols/open-responses-channel.d.ts +23 -0
- package/dist/protocols/open-responses-channel.js +141 -0
- package/dist/protocols/open-responses-continuation.d.ts +12 -0
- package/dist/protocols/open-responses-continuation.js +161 -0
- package/dist/protocols/open-responses.d.ts +1583 -0
- package/dist/protocols/open-responses.js +1206 -0
- package/dist/protocols/openai-chat.d.ts +860 -0
- package/dist/protocols/openai-chat.js +966 -0
- package/dist/protocols/openai-compatible-chat.d.ts +109 -0
- package/dist/protocols/openai-compatible-chat.js +19 -0
- package/dist/protocols/openai-compatible-responses.d.ts +172 -0
- package/dist/protocols/openai-compatible-responses.js +18 -0
- package/dist/protocols/openai-images.d.ts +32 -0
- package/dist/protocols/openai-images.js +183 -0
- package/dist/protocols/openai-responses.d.ts +1389 -0
- package/dist/protocols/openai-responses.js +260 -0
- package/dist/protocols/shared.d.ts +193 -0
- package/dist/protocols/shared.js +290 -0
- package/dist/protocols/utils/bedrock-auth.d.ts +60 -0
- package/dist/protocols/utils/bedrock-auth.js +85 -0
- package/dist/protocols/utils/bedrock-cache.d.ts +19 -0
- package/dist/protocols/utils/bedrock-cache.js +49 -0
- package/dist/protocols/utils/bedrock-media.d.ts +55 -0
- package/dist/protocols/utils/bedrock-media.js +73 -0
- package/dist/protocols/utils/cache.d.ts +6 -0
- package/dist/protocols/utils/cache.js +5 -0
- package/dist/protocols/utils/gemini-tool-schema.d.ts +2 -0
- package/dist/protocols/utils/gemini-tool-schema.js +103 -0
- package/dist/protocols/utils/image-input.d.ts +21 -0
- package/dist/protocols/utils/image-input.js +20 -0
- package/dist/protocols/utils/lifecycle.d.ts +24 -0
- package/dist/protocols/utils/lifecycle.js +72 -0
- package/dist/protocols/utils/meta-image.d.ts +2 -0
- package/dist/protocols/utils/meta-image.js +13 -0
- package/dist/protocols/utils/open-responses-options.d.ts +56 -0
- package/dist/protocols/utils/open-responses-options.js +61 -0
- package/dist/protocols/utils/openai-image.d.ts +5 -0
- package/dist/protocols/utils/openai-image.js +18 -0
- package/dist/protocols/utils/openai-options.d.ts +16 -0
- package/dist/protocols/utils/openai-options.js +14 -0
- package/dist/protocols/utils/partial-json-options.d.ts +62 -0
- package/dist/protocols/utils/partial-json-options.js +52 -0
- package/dist/protocols/utils/partial-json.d.ts +8 -0
- package/dist/protocols/utils/partial-json.js +279 -0
- package/dist/protocols/utils/responses-checkpoint.d.ts +4 -0
- package/dist/protocols/utils/responses-checkpoint.js +107 -0
- package/dist/protocols/utils/responses-compaction.d.ts +4 -0
- package/dist/protocols/utils/responses-compaction.js +129 -0
- package/dist/protocols/utils/responses-hosted-tools.d.ts +26 -0
- package/dist/protocols/utils/responses-hosted-tools.js +32 -0
- package/dist/protocols/utils/tool-schema.d.ts +8 -0
- package/dist/protocols/utils/tool-schema.js +54 -0
- package/dist/protocols/utils/tool-stream.d.ts +710 -0
- package/dist/protocols/utils/tool-stream.js +155 -0
- package/dist/protocols/xai-images.d.ts +26 -0
- package/dist/protocols/xai-images.js +106 -0
- package/dist/protocols/xai-responses.d.ts +250 -0
- package/dist/protocols/xai-responses.js +81 -0
- package/dist/protocols/zai-images.d.ts +22 -0
- package/dist/protocols/zai-images.js +79 -0
- package/dist/protocols.d.ts +1 -0
- package/dist/protocols.js +1 -0
- package/dist/provider-error.d.ts +15 -0
- package/dist/provider-error.js +125 -0
- package/dist/provider-package.d.ts +11 -0
- package/dist/provider-package.js +1 -0
- package/dist/provider.d.ts +23 -0
- package/dist/provider.js +2 -0
- package/dist/providers/amazon-bedrock/mantle/chat.d.ts +2 -0
- package/dist/providers/amazon-bedrock/mantle/chat.js +1 -0
- package/dist/providers/amazon-bedrock/mantle/responses.d.ts +2 -0
- package/dist/providers/amazon-bedrock/mantle/responses.js +1 -0
- package/dist/providers/amazon-bedrock/mantle.d.ts +2 -0
- package/dist/providers/amazon-bedrock/mantle.js +1 -0
- package/dist/providers/amazon-bedrock-mantle.d.ts +371 -0
- package/dist/providers/amazon-bedrock-mantle.js +76 -0
- package/dist/providers/amazon-bedrock.d.ts +173 -0
- package/dist/providers/amazon-bedrock.js +41 -0
- package/dist/providers/anthropic-compatible.d.ts +304 -0
- package/dist/providers/anthropic-compatible.js +44 -0
- package/dist/providers/anthropic.d.ts +302 -0
- package/dist/providers/anthropic.js +39 -0
- package/dist/providers/azure/chat.d.ts +2 -0
- package/dist/providers/azure/chat.js +1 -0
- package/dist/providers/azure/responses.d.ts +2 -0
- package/dist/providers/azure/responses.js +1 -0
- package/dist/providers/azure.d.ts +599 -0
- package/dist/providers/azure.js +115 -0
- package/dist/providers/baseten.d.ts +229 -0
- package/dist/providers/baseten.js +38 -0
- package/dist/providers/cerebras.d.ts +228 -0
- package/dist/providers/cerebras.js +40 -0
- package/dist/providers/cloudflare-ai-gateway.d.ts +238 -0
- package/dist/providers/cloudflare-ai-gateway.js +59 -0
- package/dist/providers/cloudflare-workers-ai.d.ts +232 -0
- package/dist/providers/cloudflare-workers-ai.js +45 -0
- package/dist/providers/deepinfra.d.ts +228 -0
- package/dist/providers/deepinfra.js +43 -0
- package/dist/providers/deepseek.d.ts +229 -0
- package/dist/providers/deepseek.js +41 -0
- package/dist/providers/fireworks.d.ts +229 -0
- package/dist/providers/fireworks.js +38 -0
- package/dist/providers/google-vertex/chat.d.ts +2 -0
- package/dist/providers/google-vertex/chat.js +1 -0
- package/dist/providers/google-vertex/gemini.d.ts +2 -0
- package/dist/providers/google-vertex/gemini.js +1 -0
- package/dist/providers/google-vertex/messages.d.ts +2 -0
- package/dist/providers/google-vertex/messages.js +1 -0
- package/dist/providers/google-vertex/responses.d.ts +2 -0
- package/dist/providers/google-vertex/responses.js +1 -0
- package/dist/providers/google-vertex-chat.d.ts +133 -0
- package/dist/providers/google-vertex-chat.js +55 -0
- package/dist/providers/google-vertex-messages.d.ts +301 -0
- package/dist/providers/google-vertex-messages.js +75 -0
- package/dist/providers/google-vertex-responses.d.ts +198 -0
- package/dist/providers/google-vertex-responses.js +56 -0
- package/dist/providers/google-vertex-shared.d.ts +22 -0
- package/dist/providers/google-vertex-shared.js +61 -0
- package/dist/providers/google-vertex.d.ts +120 -0
- package/dist/providers/google-vertex.js +92 -0
- package/dist/providers/google.d.ts +115 -0
- package/dist/providers/google.js +42 -0
- package/dist/providers/groq.d.ts +315 -0
- package/dist/providers/groq.js +81 -0
- package/dist/providers/index.d.ts +28 -0
- package/dist/providers/index.js +28 -0
- package/dist/providers/meta/chat.d.ts +2 -0
- package/dist/providers/meta/chat.js +1 -0
- package/dist/providers/meta/messages.d.ts +2 -0
- package/dist/providers/meta/messages.js +1 -0
- package/dist/providers/meta/responses.d.ts +2 -0
- package/dist/providers/meta/responses.js +1 -0
- package/dist/providers/meta.d.ts +631 -0
- package/dist/providers/meta.js +113 -0
- package/dist/providers/minimax/chat.d.ts +4 -0
- package/dist/providers/minimax/chat.js +8 -0
- package/dist/providers/minimax/messages.d.ts +1 -0
- package/dist/providers/minimax/messages.js +1 -0
- package/dist/providers/minimax/responses.d.ts +4 -0
- package/dist/providers/minimax/responses.js +8 -0
- package/dist/providers/minimax.d.ts +593 -0
- package/dist/providers/minimax.js +89 -0
- package/dist/providers/mistral.d.ts +208 -0
- package/dist/providers/mistral.js +28 -0
- package/dist/providers/open-responses-options.d.ts +6 -0
- package/dist/providers/open-responses-options.js +1 -0
- package/dist/providers/openai/chat.d.ts +2 -0
- package/dist/providers/openai/chat.js +1 -0
- package/dist/providers/openai/responses.d.ts +2 -0
- package/dist/providers/openai/responses.js +1 -0
- package/dist/providers/openai-compatible/responses.d.ts +1 -0
- package/dist/providers/openai-compatible/responses.js +1 -0
- package/dist/providers/openai-compatible-responses.d.ts +196 -0
- package/dist/providers/openai-compatible-responses.js +32 -0
- package/dist/providers/openai-compatible.d.ts +131 -0
- package/dist/providers/openai-compatible.js +33 -0
- package/dist/providers/openai-options.d.ts +26 -0
- package/dist/providers/openai-options.js +41 -0
- package/dist/providers/openai.d.ts +405 -0
- package/dist/providers/openai.js +82 -0
- package/dist/providers/openrouter.d.ts +564 -0
- package/dist/providers/openrouter.js +114 -0
- package/dist/providers/togetherai.d.ts +228 -0
- package/dist/providers/togetherai.js +40 -0
- package/dist/providers/xai.d.ts +164 -0
- package/dist/providers/xai.js +84 -0
- package/dist/providers/zai.d.ts +24 -0
- package/dist/providers/zai.js +21 -0
- package/dist/providers.d.ts +1 -0
- package/dist/providers.js +1 -0
- package/dist/route/auth-options.d.ts +34 -0
- package/dist/route/auth-options.js +14 -0
- package/dist/route/auth.d.ts +51 -0
- package/dist/route/auth.js +87 -0
- package/dist/route/client.d.ts +430 -0
- package/dist/route/client.js +304 -0
- package/dist/route/endpoint.d.ts +28 -0
- package/dist/route/endpoint.js +21 -0
- package/dist/route/executor.d.ts +27 -0
- package/dist/route/executor.js +183 -0
- package/dist/route/framing.d.ts +29 -0
- package/dist/route/framing.js +14 -0
- package/dist/route/index.d.ts +16 -0
- package/dist/route/index.js +9 -0
- package/dist/route/protocol.d.ts +76 -0
- package/dist/route/protocol.js +16 -0
- package/dist/route/transport/http.d.ts +34 -0
- package/dist/route/transport/http.js +68 -0
- package/dist/route/transport/index.d.ts +41 -0
- package/dist/route/transport/index.js +4 -0
- package/dist/route/transport/websocket-channel.d.ts +57 -0
- package/dist/route/transport/websocket-channel.js +1 -0
- package/dist/route/transport/websocket.d.ts +56 -0
- package/dist/route/transport/websocket.js +366 -0
- package/dist/route.d.ts +1 -0
- package/dist/route.js +1 -0
- package/dist/schema/errors.d.ts +158 -0
- package/dist/schema/errors.js +120 -0
- package/dist/schema/events.d.ts +8123 -0
- package/dist/schema/events.js +550 -0
- package/dist/schema/ids.d.ts +17 -0
- package/dist/schema/ids.js +10 -0
- package/dist/schema/index.d.ts +5 -0
- package/dist/schema/index.js +5 -0
- package/dist/schema/messages.d.ts +646 -0
- package/dist/schema/messages.js +301 -0
- package/dist/schema/options.d.ts +157 -0
- package/dist/schema/options.js +187 -0
- package/dist/testing.d.ts +1693 -0
- package/dist/testing.js +129 -0
- package/dist/tool-history.d.ts +2 -0
- package/dist/tool-history.js +84 -0
- package/dist/tool-runtime.d.ts +15 -0
- package/dist/tool-runtime.js +62 -0
- package/dist/tool.d.ts +136 -0
- package/dist/tool.js +66 -0
- package/dist/utils/record.d.ts +2 -0
- package/dist/utils/record.js +2 -0
- package/dist/utils/sanitize.d.ts +1 -0
- package/dist/utils/sanitize.js +12 -0
- package/package.json +48 -0
|
@@ -0,0 +1,301 @@
|
|
|
1
|
+
import { Schema } from "effect";
|
|
2
|
+
import { Tool } from "@opencode/schema/tool";
|
|
3
|
+
import { CacheHint, CachePolicy, GenerationOptions, HttpOptions, JsonSchema, LanguageModelSchema, ProviderOptions, } from "./options.js";
|
|
4
|
+
import { ProviderID } from "./ids.js";
|
|
5
|
+
export const MessageRole = Schema.Literals(["system", "user", "assistant", "tool"]);
|
|
6
|
+
export const ProviderMetadata = Schema.Record(Schema.String, Schema.Record(Schema.String, Schema.Unknown)).annotate({
|
|
7
|
+
identifier: "LLM.ProviderMetadata",
|
|
8
|
+
});
|
|
9
|
+
const systemPartSchema = Schema.Struct({
|
|
10
|
+
type: Schema.Literal("text"),
|
|
11
|
+
text: Schema.String,
|
|
12
|
+
cache: Schema.optional(CacheHint),
|
|
13
|
+
metadata: Schema.optional(Schema.Record(Schema.String, Schema.Unknown)),
|
|
14
|
+
}).annotate({ identifier: "LLM.SystemPart" });
|
|
15
|
+
const makeSystemPart = (text) => ({ type: "text", text });
|
|
16
|
+
export const SystemPart = Object.assign(systemPartSchema, {
|
|
17
|
+
make: makeSystemPart,
|
|
18
|
+
content: (input) => {
|
|
19
|
+
if (input === undefined)
|
|
20
|
+
return [];
|
|
21
|
+
return typeof input === "string" ? [makeSystemPart(input)] : Array.isArray(input) ? [...input] : [input];
|
|
22
|
+
},
|
|
23
|
+
});
|
|
24
|
+
export const TextPart = Schema.Struct({
|
|
25
|
+
type: Schema.Literal("text"),
|
|
26
|
+
text: Schema.String,
|
|
27
|
+
cache: Schema.optional(CacheHint),
|
|
28
|
+
metadata: Schema.optional(Schema.Record(Schema.String, Schema.Unknown)),
|
|
29
|
+
providerMetadata: Schema.optional(ProviderMetadata),
|
|
30
|
+
}).annotate({ identifier: "LLM.Content.Text" });
|
|
31
|
+
export const MediaPart = Schema.Struct({
|
|
32
|
+
type: Schema.Literal("media"),
|
|
33
|
+
mediaType: Schema.String,
|
|
34
|
+
data: Schema.Union([Schema.String, Schema.Uint8Array]),
|
|
35
|
+
filename: Schema.optional(Schema.String),
|
|
36
|
+
cache: Schema.optional(CacheHint),
|
|
37
|
+
metadata: Schema.optional(Schema.Record(Schema.String, Schema.Unknown)),
|
|
38
|
+
providerMetadata: Schema.optional(ProviderMetadata),
|
|
39
|
+
}).annotate({ identifier: "LLM.Content.Media" });
|
|
40
|
+
const toolResultValueSchema = Schema.Union([
|
|
41
|
+
Schema.Struct({
|
|
42
|
+
type: Schema.Literal("json"),
|
|
43
|
+
value: Schema.Unknown,
|
|
44
|
+
}),
|
|
45
|
+
Schema.Struct({
|
|
46
|
+
type: Schema.Literal("text"),
|
|
47
|
+
value: Schema.Unknown,
|
|
48
|
+
}),
|
|
49
|
+
Schema.Struct({
|
|
50
|
+
type: Schema.Literal("error"),
|
|
51
|
+
value: Schema.Unknown,
|
|
52
|
+
}),
|
|
53
|
+
Schema.Struct({
|
|
54
|
+
type: Schema.Literal("content"),
|
|
55
|
+
value: Schema.Array(Tool.Content),
|
|
56
|
+
}),
|
|
57
|
+
]).annotate({ identifier: "LLM.ToolResult" });
|
|
58
|
+
const isToolResultValue = Schema.is(toolResultValueSchema);
|
|
59
|
+
export const ToolResultValue = Object.assign(toolResultValueSchema, {
|
|
60
|
+
is: isToolResultValue,
|
|
61
|
+
make: (value, type = "json") => {
|
|
62
|
+
if (isToolResultValue(value))
|
|
63
|
+
return value;
|
|
64
|
+
if (type === "content")
|
|
65
|
+
return { type, value: Array.isArray(value) ? value : [] };
|
|
66
|
+
return { type, value };
|
|
67
|
+
},
|
|
68
|
+
});
|
|
69
|
+
export const ToolOutput = Object.assign(Schema.Struct({
|
|
70
|
+
structured: Schema.Unknown,
|
|
71
|
+
content: Schema.Array(Tool.Content),
|
|
72
|
+
}).annotate({ identifier: "LLM.ToolOutput" }), {
|
|
73
|
+
make: (structured, content = []) => ({ structured, content }),
|
|
74
|
+
fromResultValue: (result) => {
|
|
75
|
+
switch (result.type) {
|
|
76
|
+
case "json":
|
|
77
|
+
return { structured: result.value, content: [] };
|
|
78
|
+
case "text":
|
|
79
|
+
return { structured: {}, content: [{ type: "text", text: toolResultText(result.value) }] };
|
|
80
|
+
case "content":
|
|
81
|
+
return { structured: {}, content: result.value };
|
|
82
|
+
case "error":
|
|
83
|
+
return undefined;
|
|
84
|
+
}
|
|
85
|
+
},
|
|
86
|
+
toResultValue: (output) => {
|
|
87
|
+
if (output.content.length === 0)
|
|
88
|
+
return { type: "json", value: output.structured };
|
|
89
|
+
if (output.content.length === 1 && output.content[0]?.type === "text")
|
|
90
|
+
return { type: "text", value: output.content[0].text };
|
|
91
|
+
return { type: "content", value: output.content };
|
|
92
|
+
},
|
|
93
|
+
});
|
|
94
|
+
const toolResultText = (value) => {
|
|
95
|
+
if (typeof value === "string")
|
|
96
|
+
return value;
|
|
97
|
+
try {
|
|
98
|
+
return JSON.stringify(value) ?? String(value);
|
|
99
|
+
}
|
|
100
|
+
catch {
|
|
101
|
+
return String(value);
|
|
102
|
+
}
|
|
103
|
+
};
|
|
104
|
+
export const ToolCallPart = Object.assign(Schema.Struct({
|
|
105
|
+
type: Schema.Literal("tool-call"),
|
|
106
|
+
id: Schema.String,
|
|
107
|
+
name: Schema.String,
|
|
108
|
+
namespace: Schema.optional(Schema.String),
|
|
109
|
+
input: Schema.Unknown,
|
|
110
|
+
providerExecuted: Schema.optional(Schema.Boolean),
|
|
111
|
+
cache: Schema.optional(CacheHint),
|
|
112
|
+
metadata: Schema.optional(Schema.Record(Schema.String, Schema.Unknown)),
|
|
113
|
+
providerMetadata: Schema.optional(ProviderMetadata),
|
|
114
|
+
}).annotate({ identifier: "LLM.Content.ToolCall" }), {
|
|
115
|
+
make: (input) => ({ type: "tool-call", ...input }),
|
|
116
|
+
});
|
|
117
|
+
export const ToolResultPart = Object.assign(Schema.Struct({
|
|
118
|
+
type: Schema.Literal("tool-result"),
|
|
119
|
+
id: Schema.String,
|
|
120
|
+
name: Schema.String,
|
|
121
|
+
namespace: Schema.optional(Schema.String),
|
|
122
|
+
result: ToolResultValue,
|
|
123
|
+
providerExecuted: Schema.optional(Schema.Boolean),
|
|
124
|
+
cache: Schema.optional(CacheHint),
|
|
125
|
+
metadata: Schema.optional(Schema.Record(Schema.String, Schema.Unknown)),
|
|
126
|
+
providerMetadata: Schema.optional(ProviderMetadata),
|
|
127
|
+
}).annotate({ identifier: "LLM.Content.ToolResult" }), {
|
|
128
|
+
make: (input) => ({
|
|
129
|
+
type: "tool-result",
|
|
130
|
+
id: input.id,
|
|
131
|
+
name: input.name,
|
|
132
|
+
namespace: input.namespace,
|
|
133
|
+
result: ToolResultValue.make(input.result, input.resultType),
|
|
134
|
+
providerExecuted: input.providerExecuted,
|
|
135
|
+
cache: input.cache,
|
|
136
|
+
metadata: input.metadata,
|
|
137
|
+
providerMetadata: input.providerMetadata,
|
|
138
|
+
}),
|
|
139
|
+
});
|
|
140
|
+
export const ReasoningPart = Schema.Struct({
|
|
141
|
+
type: Schema.Literal("reasoning"),
|
|
142
|
+
text: Schema.String,
|
|
143
|
+
encrypted: Schema.optional(Schema.String),
|
|
144
|
+
cache: Schema.optional(CacheHint),
|
|
145
|
+
metadata: Schema.optional(Schema.Record(Schema.String, Schema.Unknown)),
|
|
146
|
+
providerMetadata: Schema.optional(ProviderMetadata),
|
|
147
|
+
}).annotate({ identifier: "LLM.Content.Reasoning" });
|
|
148
|
+
const compactionPartSchema = Schema.Struct({
|
|
149
|
+
type: Schema.Literal("compaction"),
|
|
150
|
+
provider: ProviderID,
|
|
151
|
+
id: Schema.optional(Schema.String),
|
|
152
|
+
encrypted: Schema.optional(Schema.String),
|
|
153
|
+
/** Null means the provider failed to produce a summary; prior history must be retained. */
|
|
154
|
+
text: Schema.optional(Schema.NullOr(Schema.String)),
|
|
155
|
+
})
|
|
156
|
+
.pipe(Schema.refine((part) => (part.encrypted !== undefined) !== (part.text !== undefined), { message: "Compaction requires either encrypted content or a summary" }))
|
|
157
|
+
.annotate({ identifier: "LLM.Content.Compaction" });
|
|
158
|
+
export const CompactionPart = Object.assign(compactionPartSchema, {
|
|
159
|
+
make: (input) => Schema.decodeUnknownSync(compactionPartSchema)({ type: "compaction", ...input }),
|
|
160
|
+
});
|
|
161
|
+
export const ContentPart = Schema.Union([
|
|
162
|
+
TextPart,
|
|
163
|
+
MediaPart,
|
|
164
|
+
ToolCallPart,
|
|
165
|
+
ToolResultPart,
|
|
166
|
+
ReasoningPart,
|
|
167
|
+
CompactionPart,
|
|
168
|
+
]).pipe(Schema.toTaggedUnion("type"));
|
|
169
|
+
export class Message extends Schema.Class("LLM.Message")({
|
|
170
|
+
id: Schema.optional(Schema.String),
|
|
171
|
+
role: MessageRole,
|
|
172
|
+
content: Schema.Array(ContentPart),
|
|
173
|
+
metadata: Schema.optional(Schema.Record(Schema.String, Schema.Unknown)),
|
|
174
|
+
providerMetadata: Schema.optional(ProviderMetadata),
|
|
175
|
+
native: Schema.optional(Schema.Record(Schema.String, Schema.Unknown)),
|
|
176
|
+
}) {
|
|
177
|
+
}
|
|
178
|
+
(function (Message) {
|
|
179
|
+
Message.text = (value) => ({ type: "text", text: value });
|
|
180
|
+
Message.content = (input) => typeof input === "string" ? [Message.text(input)] : Array.isArray(input) ? [...input] : [input];
|
|
181
|
+
Message.make = (input) => {
|
|
182
|
+
if (input instanceof Message)
|
|
183
|
+
return input;
|
|
184
|
+
return new Message({ ...input, content: Message.content(input.content) });
|
|
185
|
+
};
|
|
186
|
+
Message.user = (content) => Message.make({ role: "user", content });
|
|
187
|
+
Message.assistant = (content) => Message.make({ role: "assistant", content });
|
|
188
|
+
/**
|
|
189
|
+
* Add an operator-authored instruction at this chronological point in the
|
|
190
|
+
* conversation. This is distinct from the initial `LLMRequest.system`
|
|
191
|
+
* prompt. Keep raw retrieved, tool, and web content out of privileged system
|
|
192
|
+
* updates; pass that untrusted content through ordinary user/tool channels.
|
|
193
|
+
*/
|
|
194
|
+
Message.system = (content) => Message.make({ role: "system", content });
|
|
195
|
+
Message.tool = (result) => Message.make({ role: "tool", content: ["type" in result ? result : ToolResultPart.make(result)] });
|
|
196
|
+
})(Message || (Message = {}));
|
|
197
|
+
const toolDefinitionFields = {
|
|
198
|
+
name: Schema.String,
|
|
199
|
+
description: Schema.String,
|
|
200
|
+
inputSchema: JsonSchema,
|
|
201
|
+
outputSchema: Schema.optional(JsonSchema),
|
|
202
|
+
cache: Schema.optional(CacheHint),
|
|
203
|
+
metadata: Schema.optional(Schema.Record(Schema.String, Schema.Unknown)),
|
|
204
|
+
native: Schema.optional(Schema.Record(Schema.String, Schema.Unknown)),
|
|
205
|
+
};
|
|
206
|
+
export class ToolDefinition extends Schema.Class("LLM.ToolDefinition")({
|
|
207
|
+
type: Schema.Literal("tool"),
|
|
208
|
+
...toolDefinitionFields,
|
|
209
|
+
}) {
|
|
210
|
+
constructor(input) {
|
|
211
|
+
super({ ...input, type: "tool" });
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
(function (ToolDefinition) {
|
|
215
|
+
/** Normalize tool definition input into the canonical `ToolDefinition` class. */
|
|
216
|
+
ToolDefinition.make = (input) => (input instanceof ToolDefinition ? input : new ToolDefinition(input));
|
|
217
|
+
})(ToolDefinition || (ToolDefinition = {}));
|
|
218
|
+
export const ToolNamespace = Object.assign(Schema.Struct({
|
|
219
|
+
type: Schema.Literal("namespace"),
|
|
220
|
+
name: Schema.String,
|
|
221
|
+
description: Schema.optional(Schema.UndefinedOr(Schema.String)),
|
|
222
|
+
tools: Schema.Array(Schema.suspend(() => ToolEntry)),
|
|
223
|
+
}).annotate({ identifier: "LLM.ToolNamespace" }), {
|
|
224
|
+
make: (input) => ({
|
|
225
|
+
...input,
|
|
226
|
+
type: "namespace",
|
|
227
|
+
tools: input.tools.map(ToolEntry.make),
|
|
228
|
+
}),
|
|
229
|
+
});
|
|
230
|
+
export const ToolEntry = Object.assign(Schema.Union([ToolDefinition, ToolNamespace]).pipe(Schema.toTaggedUnion("type"), Schema.annotate({ identifier: "LLM.ToolEntry" })), {
|
|
231
|
+
make: (input) => "type" in input && input.type === "namespace" ? ToolNamespace.make(input) : ToolDefinition.make(input),
|
|
232
|
+
});
|
|
233
|
+
export class ToolChoice extends Schema.Class("LLM.ToolChoice")({
|
|
234
|
+
type: Schema.Literals(["auto", "none", "required", "tool"]),
|
|
235
|
+
name: Schema.optional(Schema.String),
|
|
236
|
+
disableParallelToolUse: Schema.optional(Schema.Boolean),
|
|
237
|
+
}) {
|
|
238
|
+
}
|
|
239
|
+
(function (ToolChoice) {
|
|
240
|
+
const isMode = (value) => value === "auto" || value === "none" || value === "required";
|
|
241
|
+
/** Select a specific named tool. */
|
|
242
|
+
ToolChoice.named = (value) => new ToolChoice({ type: "tool", name: value });
|
|
243
|
+
/** Normalize ergonomic tool-choice inputs into the canonical `ToolChoice` class. */
|
|
244
|
+
ToolChoice.make = (input) => {
|
|
245
|
+
if (input instanceof ToolChoice)
|
|
246
|
+
return input;
|
|
247
|
+
if (input instanceof ToolDefinition)
|
|
248
|
+
return ToolChoice.named(input.name);
|
|
249
|
+
if (typeof input === "string")
|
|
250
|
+
return isMode(input) ? new ToolChoice({ type: input }) : ToolChoice.named(input);
|
|
251
|
+
return new ToolChoice(input);
|
|
252
|
+
};
|
|
253
|
+
})(ToolChoice || (ToolChoice = {}));
|
|
254
|
+
const requestSchema = Schema.Struct({
|
|
255
|
+
id: Schema.optional(Schema.String),
|
|
256
|
+
model: LanguageModelSchema,
|
|
257
|
+
system: Schema.Array(SystemPart),
|
|
258
|
+
messages: Schema.Array(Message),
|
|
259
|
+
tools: Schema.Array(ToolEntry),
|
|
260
|
+
toolChoice: Schema.optional(ToolChoice),
|
|
261
|
+
generation: Schema.optional(GenerationOptions),
|
|
262
|
+
providerOptions: Schema.optional(ProviderOptions),
|
|
263
|
+
http: Schema.optional(HttpOptions),
|
|
264
|
+
cache: Schema.optional(CachePolicy),
|
|
265
|
+
// Stable cache affinity for protocols that support provider-managed prompt caching.
|
|
266
|
+
promptCacheKey: Schema.optional(Schema.String),
|
|
267
|
+
metadata: Schema.optional(Schema.Record(Schema.String, Schema.Unknown)),
|
|
268
|
+
});
|
|
269
|
+
export class LLMRequest extends Schema.Class("LLM.Request")(requestSchema.fields) {
|
|
270
|
+
// Preserve model inference instead of inheriting the schema's erased constructor signature.
|
|
271
|
+
// oxlint-disable-next-line no-useless-constructor
|
|
272
|
+
constructor(input) {
|
|
273
|
+
super(input);
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
(function (LLMRequest) {
|
|
277
|
+
LLMRequest.input = (request) => ({
|
|
278
|
+
id: request.id,
|
|
279
|
+
model: request.model,
|
|
280
|
+
system: request.system,
|
|
281
|
+
messages: request.messages,
|
|
282
|
+
tools: request.tools,
|
|
283
|
+
toolChoice: request.toolChoice,
|
|
284
|
+
generation: request.generation,
|
|
285
|
+
providerOptions: request.providerOptions,
|
|
286
|
+
http: request.http,
|
|
287
|
+
cache: request.cache,
|
|
288
|
+
promptCacheKey: request.promptCacheKey,
|
|
289
|
+
metadata: request.metadata,
|
|
290
|
+
});
|
|
291
|
+
function update(request, patch) {
|
|
292
|
+
if (Object.keys(patch).length === 0)
|
|
293
|
+
return request;
|
|
294
|
+
return new LLMRequest({
|
|
295
|
+
...LLMRequest.input(request),
|
|
296
|
+
...patch,
|
|
297
|
+
model: patch.model ?? request.model,
|
|
298
|
+
});
|
|
299
|
+
}
|
|
300
|
+
LLMRequest.update = update;
|
|
301
|
+
})(LLMRequest || (LLMRequest = {}));
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
import { Schema } from "effect";
|
|
2
|
+
import { ModelID, ProviderID } from "./ids.js";
|
|
3
|
+
import type { AnyRoute, CompactionOperations } from "../route/client.js";
|
|
4
|
+
export declare const JsonSchema: Schema.$Record<Schema.String, Schema.Unknown>;
|
|
5
|
+
export type JsonSchema = Schema.Schema.Type<typeof JsonSchema>;
|
|
6
|
+
export declare const mergeJsonRecords: (...items: ReadonlyArray<Record<string, unknown> | undefined>) => Record<string, unknown> | undefined;
|
|
7
|
+
export declare const ProviderOptions: Schema.$Record<Schema.String, Schema.Unknown>;
|
|
8
|
+
export type ProviderOptions = Schema.Schema.Type<typeof ProviderOptions>;
|
|
9
|
+
export declare const mergeProviderOptions: (...items: ReadonlyArray<ProviderOptions | undefined>) => ProviderOptions | undefined;
|
|
10
|
+
declare const HttpOptions_base: Schema.Class<HttpOptions, Schema.Struct<{
|
|
11
|
+
readonly body: Schema.optional<Schema.$Record<Schema.String, Schema.Unknown>>;
|
|
12
|
+
readonly headers: Schema.optional<Schema.$Record<Schema.String, Schema.String>>;
|
|
13
|
+
readonly query: Schema.optional<Schema.$Record<Schema.String, Schema.String>>;
|
|
14
|
+
}>, {}>;
|
|
15
|
+
export declare class HttpOptions extends HttpOptions_base {
|
|
16
|
+
}
|
|
17
|
+
export declare namespace HttpOptions {
|
|
18
|
+
type Input = HttpOptions | ConstructorParameters<typeof HttpOptions>[0];
|
|
19
|
+
/** Normalize HTTP option input into the canonical `HttpOptions` class. */
|
|
20
|
+
const make: (input: Input) => HttpOptions;
|
|
21
|
+
}
|
|
22
|
+
export declare const mergeHttpOptions: (...items: ReadonlyArray<HttpOptions | undefined>) => HttpOptions | undefined;
|
|
23
|
+
declare const GenerationOptions_base: Schema.Class<GenerationOptions, Schema.Struct<{
|
|
24
|
+
readonly maxTokens: Schema.optional<Schema.Number>;
|
|
25
|
+
readonly temperature: Schema.optional<Schema.Number>;
|
|
26
|
+
readonly topP: Schema.optional<Schema.Number>;
|
|
27
|
+
readonly topK: Schema.optional<Schema.Number>;
|
|
28
|
+
readonly frequencyPenalty: Schema.optional<Schema.Number>;
|
|
29
|
+
readonly presencePenalty: Schema.optional<Schema.Number>;
|
|
30
|
+
readonly seed: Schema.optional<Schema.Number>;
|
|
31
|
+
readonly stop: Schema.optional<Schema.$Array<Schema.String>>;
|
|
32
|
+
}>, {}>;
|
|
33
|
+
export declare class GenerationOptions extends GenerationOptions_base {
|
|
34
|
+
}
|
|
35
|
+
export declare namespace GenerationOptions {
|
|
36
|
+
type Input = GenerationOptions | ConstructorParameters<typeof GenerationOptions>[0];
|
|
37
|
+
/** Normalize generation option input into the canonical `GenerationOptions` class. */
|
|
38
|
+
const make: (input?: Input) => GenerationOptions;
|
|
39
|
+
}
|
|
40
|
+
export type GenerationOptionsFields = {
|
|
41
|
+
readonly maxTokens?: number;
|
|
42
|
+
readonly temperature?: number;
|
|
43
|
+
readonly topP?: number;
|
|
44
|
+
readonly topK?: number;
|
|
45
|
+
readonly frequencyPenalty?: number;
|
|
46
|
+
readonly presencePenalty?: number;
|
|
47
|
+
readonly seed?: number;
|
|
48
|
+
readonly stop?: ReadonlyArray<string>;
|
|
49
|
+
};
|
|
50
|
+
export type GenerationOptionsInput = GenerationOptions | GenerationOptionsFields;
|
|
51
|
+
export declare const mergeGenerationOptions: (...items: ReadonlyArray<GenerationOptionsInput | undefined>) => GenerationOptions | undefined;
|
|
52
|
+
declare const LanguageModelDefaults_base: Schema.Class<LanguageModelDefaults, Schema.Struct<{
|
|
53
|
+
readonly generation: Schema.optional<typeof GenerationOptions>;
|
|
54
|
+
readonly providerOptions: Schema.optional<Schema.$Record<Schema.String, Schema.Unknown>>;
|
|
55
|
+
readonly http: Schema.optional<typeof HttpOptions>;
|
|
56
|
+
}>, {}>;
|
|
57
|
+
export declare class LanguageModelDefaults extends LanguageModelDefaults_base {
|
|
58
|
+
}
|
|
59
|
+
export declare namespace LanguageModelDefaults {
|
|
60
|
+
type Input = LanguageModelDefaults | {
|
|
61
|
+
readonly generation?: GenerationOptions.Input;
|
|
62
|
+
readonly providerOptions?: ProviderOptions;
|
|
63
|
+
readonly http?: HttpOptions.Input;
|
|
64
|
+
};
|
|
65
|
+
/** Normalize selected-model request defaults without applying precedence. */
|
|
66
|
+
const make: (input: Input) => LanguageModelDefaults;
|
|
67
|
+
}
|
|
68
|
+
export declare const LanguageModelToolSchemaCompatibility: Schema.Literals<readonly ["gemini", "moonshot"]>;
|
|
69
|
+
export type LanguageModelToolSchemaCompatibility = Schema.Schema.Type<typeof LanguageModelToolSchemaCompatibility>;
|
|
70
|
+
export declare const LanguageModelMaxTokensFieldCompatibility: Schema.Literals<readonly ["max_completion_tokens", "max_tokens"]>;
|
|
71
|
+
export type LanguageModelMaxTokensFieldCompatibility = Schema.Schema.Type<typeof LanguageModelMaxTokensFieldCompatibility>;
|
|
72
|
+
declare const LanguageModelCompatibility_base: Schema.Class<LanguageModelCompatibility, Schema.Struct<{
|
|
73
|
+
readonly toolSchema: Schema.optional<Schema.Literals<readonly ["gemini", "moonshot"]>>;
|
|
74
|
+
readonly reasoningField: Schema.optional<Schema.String>;
|
|
75
|
+
/** Require every assistant message to include its reasoning field, even when empty. */
|
|
76
|
+
readonly requireReasoning: Schema.optional<Schema.Boolean>;
|
|
77
|
+
readonly maxTokensField: Schema.optional<Schema.Literals<readonly ["max_completion_tokens", "max_tokens"]>>;
|
|
78
|
+
readonly requireFinishReason: Schema.optional<Schema.Boolean>;
|
|
79
|
+
readonly requireAssistantAfterTool: Schema.optional<Schema.Boolean>;
|
|
80
|
+
readonly supportsStore: Schema.optional<Schema.Boolean>;
|
|
81
|
+
readonly supportsUsageInStreaming: Schema.optional<Schema.Boolean>;
|
|
82
|
+
readonly supportsStrictMode: Schema.optional<Schema.Boolean>;
|
|
83
|
+
readonly zaiToolStream: Schema.optional<Schema.Boolean>;
|
|
84
|
+
readonly requireSignature: Schema.optional<Schema.Boolean>;
|
|
85
|
+
}>, {}>;
|
|
86
|
+
export declare class LanguageModelCompatibility extends LanguageModelCompatibility_base {
|
|
87
|
+
}
|
|
88
|
+
export declare namespace LanguageModelCompatibility {
|
|
89
|
+
type Input = LanguageModelCompatibility | ConstructorParameters<typeof LanguageModelCompatibility>[0];
|
|
90
|
+
/** Normalize model/upstream compatibility metadata without projecting requests. */
|
|
91
|
+
const make: (input: Input) => LanguageModelCompatibility;
|
|
92
|
+
}
|
|
93
|
+
export declare class LanguageModel<Options extends ProviderOptions = ProviderOptions, Compact extends CompactionOperations | undefined = CompactionOperations | undefined> {
|
|
94
|
+
protected readonly _ProviderOptions: Options;
|
|
95
|
+
readonly id: ModelID;
|
|
96
|
+
readonly provider: ProviderID;
|
|
97
|
+
readonly route: AnyRoute<Compact>;
|
|
98
|
+
readonly defaults?: LanguageModelDefaults;
|
|
99
|
+
readonly compatibility?: LanguageModelCompatibility;
|
|
100
|
+
constructor(input: LanguageModel.ConstructorInput<Compact>);
|
|
101
|
+
static make<Options extends ProviderOptions = ProviderOptions, Compact extends CompactionOperations | undefined = CompactionOperations | undefined>(input: LanguageModel.Input<Compact>): LanguageModel<Options, Compact>;
|
|
102
|
+
static input<Options extends ProviderOptions, Compact extends CompactionOperations | undefined>(model: LanguageModel<Options, Compact>): LanguageModel.ConstructorInput<Compact>;
|
|
103
|
+
static update<Options extends ProviderOptions, Compact extends CompactionOperations | undefined>(model: LanguageModel<Options>, patch: Partial<LanguageModel.Input<Compact>> & {
|
|
104
|
+
readonly route: AnyRoute<Compact>;
|
|
105
|
+
}): LanguageModel<Options, Compact>;
|
|
106
|
+
static update<Options extends ProviderOptions, Compact extends CompactionOperations | undefined>(model: LanguageModel<Options, Compact>, patch: Partial<Omit<LanguageModel.Input, "route">> & {
|
|
107
|
+
readonly route?: undefined;
|
|
108
|
+
}): LanguageModel<Options, Compact>;
|
|
109
|
+
static update<Options extends ProviderOptions>(model: LanguageModel<Options>, patch: Partial<LanguageModel.Input>): LanguageModel<Options>;
|
|
110
|
+
}
|
|
111
|
+
export declare namespace LanguageModel {
|
|
112
|
+
type ConstructorInput<Compact extends CompactionOperations | undefined = CompactionOperations | undefined> = {
|
|
113
|
+
readonly id: ModelID;
|
|
114
|
+
readonly provider: ProviderID;
|
|
115
|
+
readonly route: AnyRoute<Compact>;
|
|
116
|
+
readonly defaults?: LanguageModelDefaults;
|
|
117
|
+
readonly compatibility?: LanguageModelCompatibility;
|
|
118
|
+
};
|
|
119
|
+
type Input<Compact extends CompactionOperations | undefined = CompactionOperations | undefined> = Omit<ConstructorInput<Compact>, "id" | "provider" | "defaults" | "compatibility"> & {
|
|
120
|
+
readonly id: string | ModelID;
|
|
121
|
+
readonly provider: string | ProviderID;
|
|
122
|
+
readonly defaults?: LanguageModelDefaults.Input;
|
|
123
|
+
readonly compatibility?: LanguageModelCompatibility.Input;
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
export type LanguageModelInput = LanguageModel.Input;
|
|
127
|
+
export type LanguageModelProviderOptions<SelectedModel> = SelectedModel extends LanguageModel<infer Options> ? Options : never;
|
|
128
|
+
export declare const LanguageModelSchema: Schema.declare<LanguageModel<{
|
|
129
|
+
readonly [x: string]: unknown;
|
|
130
|
+
}, CompactionOperations | undefined>, LanguageModel<{
|
|
131
|
+
readonly [x: string]: unknown;
|
|
132
|
+
}, CompactionOperations | undefined>>;
|
|
133
|
+
declare const CacheHint_base: Schema.Class<CacheHint, Schema.Struct<{
|
|
134
|
+
readonly type: Schema.Literals<readonly ["ephemeral", "persistent"]>;
|
|
135
|
+
readonly ttlSeconds: Schema.optional<Schema.Number>;
|
|
136
|
+
}>, {}>;
|
|
137
|
+
export declare class CacheHint extends CacheHint_base {
|
|
138
|
+
}
|
|
139
|
+
export declare const CachePolicyObject: Schema.Struct<{
|
|
140
|
+
readonly tools: Schema.optional<Schema.Boolean>;
|
|
141
|
+
readonly system: Schema.optional<Schema.Boolean>;
|
|
142
|
+
readonly messages: Schema.optional<Schema.Union<readonly [Schema.Literal<"latest-user-message">, Schema.Literal<"latest-assistant">, Schema.Struct<{
|
|
143
|
+
readonly tail: Schema.Natural;
|
|
144
|
+
}>]>>;
|
|
145
|
+
readonly ttlSeconds: Schema.optional<Schema.Number>;
|
|
146
|
+
}>;
|
|
147
|
+
export type CachePolicyObject = Schema.Schema.Type<typeof CachePolicyObject>;
|
|
148
|
+
export declare const CachePolicy: Schema.Union<readonly [Schema.Literal<"auto">, Schema.Literal<"none">, Schema.Struct<{
|
|
149
|
+
readonly tools: Schema.optional<Schema.Boolean>;
|
|
150
|
+
readonly system: Schema.optional<Schema.Boolean>;
|
|
151
|
+
readonly messages: Schema.optional<Schema.Union<readonly [Schema.Literal<"latest-user-message">, Schema.Literal<"latest-assistant">, Schema.Struct<{
|
|
152
|
+
readonly tail: Schema.Natural;
|
|
153
|
+
}>]>>;
|
|
154
|
+
readonly ttlSeconds: Schema.optional<Schema.Number>;
|
|
155
|
+
}>]>;
|
|
156
|
+
export type CachePolicy = Schema.Schema.Type<typeof CachePolicy>;
|
|
157
|
+
export {};
|
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
import { Schema } from "effect";
|
|
2
|
+
import { ModelID, ProviderID } from "./ids.js";
|
|
3
|
+
import { isRecord } from "../utils/record.js";
|
|
4
|
+
export const JsonSchema = Schema.Record(Schema.String, Schema.Unknown);
|
|
5
|
+
export const mergeJsonRecords = (...items) => {
|
|
6
|
+
const defined = items.filter((item) => item !== undefined);
|
|
7
|
+
if (defined.length === 0)
|
|
8
|
+
return undefined;
|
|
9
|
+
if (defined.length === 1 && Object.values(defined[0]).every((value) => value !== undefined))
|
|
10
|
+
return defined[0];
|
|
11
|
+
const result = {};
|
|
12
|
+
for (const item of defined) {
|
|
13
|
+
for (const [key, value] of Object.entries(item)) {
|
|
14
|
+
if (value === undefined)
|
|
15
|
+
continue;
|
|
16
|
+
result[key] = isRecord(result[key]) && isRecord(value) ? mergeJsonRecords(result[key], value) : value;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
return Object.keys(result).length === 0 ? undefined : result;
|
|
20
|
+
};
|
|
21
|
+
const mergeStringRecords = (...items) => {
|
|
22
|
+
const defined = items.filter((item) => item !== undefined);
|
|
23
|
+
if (defined.length === 0)
|
|
24
|
+
return undefined;
|
|
25
|
+
if (defined.length === 1)
|
|
26
|
+
return defined[0];
|
|
27
|
+
const result = Object.fromEntries(defined.flatMap((item) => Object.entries(item).filter((entry) => entry[1] !== undefined)));
|
|
28
|
+
return Object.keys(result).length === 0 ? undefined : result;
|
|
29
|
+
};
|
|
30
|
+
export const ProviderOptions = Schema.Record(Schema.String, Schema.Unknown);
|
|
31
|
+
export const mergeProviderOptions = (...items) => mergeJsonRecords(...items);
|
|
32
|
+
export class HttpOptions extends Schema.Class("AI.HttpOptions")({
|
|
33
|
+
body: Schema.optional(JsonSchema),
|
|
34
|
+
headers: Schema.optional(Schema.Record(Schema.String, Schema.String)),
|
|
35
|
+
query: Schema.optional(Schema.Record(Schema.String, Schema.String)),
|
|
36
|
+
}) {
|
|
37
|
+
}
|
|
38
|
+
(function (HttpOptions) {
|
|
39
|
+
/** Normalize HTTP option input into the canonical `HttpOptions` class. */
|
|
40
|
+
HttpOptions.make = (input) => (input instanceof HttpOptions ? input : new HttpOptions(input));
|
|
41
|
+
})(HttpOptions || (HttpOptions = {}));
|
|
42
|
+
export const mergeHttpOptions = (...items) => {
|
|
43
|
+
const body = mergeJsonRecords(...items.map((item) => item?.body));
|
|
44
|
+
const headers = mergeStringRecords(...items.map((item) => item?.headers));
|
|
45
|
+
const query = mergeStringRecords(...items.map((item) => item?.query));
|
|
46
|
+
if (!body && !headers && !query)
|
|
47
|
+
return undefined;
|
|
48
|
+
return new HttpOptions({ body, headers, query });
|
|
49
|
+
};
|
|
50
|
+
export class GenerationOptions extends Schema.Class("LLM.GenerationOptions")({
|
|
51
|
+
maxTokens: Schema.optional(Schema.Number),
|
|
52
|
+
temperature: Schema.optional(Schema.Number),
|
|
53
|
+
topP: Schema.optional(Schema.Number),
|
|
54
|
+
topK: Schema.optional(Schema.Number),
|
|
55
|
+
frequencyPenalty: Schema.optional(Schema.Number),
|
|
56
|
+
presencePenalty: Schema.optional(Schema.Number),
|
|
57
|
+
seed: Schema.optional(Schema.Number),
|
|
58
|
+
stop: Schema.optional(Schema.Array(Schema.String)),
|
|
59
|
+
}) {
|
|
60
|
+
}
|
|
61
|
+
(function (GenerationOptions) {
|
|
62
|
+
/** Normalize generation option input into the canonical `GenerationOptions` class. */
|
|
63
|
+
GenerationOptions.make = (input = {}) => (input instanceof GenerationOptions ? input : new GenerationOptions(input));
|
|
64
|
+
})(GenerationOptions || (GenerationOptions = {}));
|
|
65
|
+
const latestGeneration = (items, key) => items.findLast((item) => item?.[key] !== undefined)?.[key];
|
|
66
|
+
export const mergeGenerationOptions = (...items) => {
|
|
67
|
+
const result = new GenerationOptions({
|
|
68
|
+
maxTokens: latestGeneration(items, "maxTokens"),
|
|
69
|
+
temperature: latestGeneration(items, "temperature"),
|
|
70
|
+
topP: latestGeneration(items, "topP"),
|
|
71
|
+
topK: latestGeneration(items, "topK"),
|
|
72
|
+
frequencyPenalty: latestGeneration(items, "frequencyPenalty"),
|
|
73
|
+
presencePenalty: latestGeneration(items, "presencePenalty"),
|
|
74
|
+
seed: latestGeneration(items, "seed"),
|
|
75
|
+
stop: latestGeneration(items, "stop"),
|
|
76
|
+
});
|
|
77
|
+
return Object.values(result).some((value) => value !== undefined) ? result : undefined;
|
|
78
|
+
};
|
|
79
|
+
export class LanguageModelDefaults extends Schema.Class("LLM.LanguageModelDefaults")({
|
|
80
|
+
generation: Schema.optional(GenerationOptions),
|
|
81
|
+
providerOptions: Schema.optional(ProviderOptions),
|
|
82
|
+
http: Schema.optional(HttpOptions),
|
|
83
|
+
}) {
|
|
84
|
+
}
|
|
85
|
+
(function (LanguageModelDefaults) {
|
|
86
|
+
/** Normalize selected-model request defaults without applying precedence. */
|
|
87
|
+
LanguageModelDefaults.make = (input) => {
|
|
88
|
+
if (input instanceof LanguageModelDefaults)
|
|
89
|
+
return input;
|
|
90
|
+
return new LanguageModelDefaults({
|
|
91
|
+
generation: input.generation === undefined ? undefined : GenerationOptions.make(input.generation),
|
|
92
|
+
providerOptions: input.providerOptions,
|
|
93
|
+
http: input.http === undefined ? undefined : HttpOptions.make(input.http),
|
|
94
|
+
});
|
|
95
|
+
};
|
|
96
|
+
})(LanguageModelDefaults || (LanguageModelDefaults = {}));
|
|
97
|
+
export const LanguageModelToolSchemaCompatibility = Schema.Literals(["gemini", "moonshot"]);
|
|
98
|
+
export const LanguageModelMaxTokensFieldCompatibility = Schema.Literals(["max_completion_tokens", "max_tokens"]);
|
|
99
|
+
export class LanguageModelCompatibility extends Schema.Class("LLM.LanguageModelCompatibility")({
|
|
100
|
+
toolSchema: Schema.optional(LanguageModelToolSchemaCompatibility),
|
|
101
|
+
reasoningField: Schema.optional(Schema.String),
|
|
102
|
+
/** Require every assistant message to include its reasoning field, even when empty. */
|
|
103
|
+
requireReasoning: Schema.optional(Schema.Boolean),
|
|
104
|
+
maxTokensField: Schema.optional(LanguageModelMaxTokensFieldCompatibility),
|
|
105
|
+
requireFinishReason: Schema.optional(Schema.Boolean),
|
|
106
|
+
requireAssistantAfterTool: Schema.optional(Schema.Boolean),
|
|
107
|
+
supportsStore: Schema.optional(Schema.Boolean),
|
|
108
|
+
supportsUsageInStreaming: Schema.optional(Schema.Boolean),
|
|
109
|
+
supportsStrictMode: Schema.optional(Schema.Boolean),
|
|
110
|
+
zaiToolStream: Schema.optional(Schema.Boolean),
|
|
111
|
+
requireSignature: Schema.optional(Schema.Boolean),
|
|
112
|
+
}) {
|
|
113
|
+
}
|
|
114
|
+
(function (LanguageModelCompatibility) {
|
|
115
|
+
/** Normalize model/upstream compatibility metadata without projecting requests. */
|
|
116
|
+
LanguageModelCompatibility.make = (input) => input instanceof LanguageModelCompatibility ? input : new LanguageModelCompatibility(input);
|
|
117
|
+
})(LanguageModelCompatibility || (LanguageModelCompatibility = {}));
|
|
118
|
+
export class LanguageModel {
|
|
119
|
+
id;
|
|
120
|
+
provider;
|
|
121
|
+
route;
|
|
122
|
+
defaults;
|
|
123
|
+
compatibility;
|
|
124
|
+
constructor(input) {
|
|
125
|
+
this.id = input.id;
|
|
126
|
+
this.provider = input.provider;
|
|
127
|
+
this.route = input.route;
|
|
128
|
+
this.defaults = input.defaults;
|
|
129
|
+
this.compatibility = input.compatibility;
|
|
130
|
+
}
|
|
131
|
+
static make(input) {
|
|
132
|
+
return new LanguageModel({
|
|
133
|
+
id: ModelID.make(input.id),
|
|
134
|
+
provider: ProviderID.make(input.provider),
|
|
135
|
+
route: input.route,
|
|
136
|
+
defaults: input.defaults === undefined ? undefined : LanguageModelDefaults.make(input.defaults),
|
|
137
|
+
compatibility: input.compatibility === undefined ? undefined : LanguageModelCompatibility.make(input.compatibility),
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
static input(model) {
|
|
141
|
+
return {
|
|
142
|
+
id: model.id,
|
|
143
|
+
provider: model.provider,
|
|
144
|
+
route: model.route,
|
|
145
|
+
defaults: model.defaults,
|
|
146
|
+
compatibility: model.compatibility,
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
static update(model, patch) {
|
|
150
|
+
if (Object.keys(patch).length === 0)
|
|
151
|
+
return model;
|
|
152
|
+
return LanguageModel.make({
|
|
153
|
+
...LanguageModel.input(model),
|
|
154
|
+
...patch,
|
|
155
|
+
route: patch.route ?? model.route,
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
export const LanguageModelSchema = Schema.declare((value) => value instanceof LanguageModel, {
|
|
160
|
+
expected: "LLM.LanguageModel",
|
|
161
|
+
});
|
|
162
|
+
export class CacheHint extends Schema.Class("LLM.CacheHint")({
|
|
163
|
+
type: Schema.Literals(["ephemeral", "persistent"]),
|
|
164
|
+
ttlSeconds: Schema.optional(Schema.Number),
|
|
165
|
+
}) {
|
|
166
|
+
}
|
|
167
|
+
// Auto-placement policy for prompt caching. The protocol-neutral lowering step
|
|
168
|
+
// reads this and injects `CacheHint`s at the configured boundaries; the
|
|
169
|
+
// per-protocol body builders then translate those hints into wire markers as
|
|
170
|
+
// usual. `"auto"` is the default for agent loops — it places
|
|
171
|
+
// breakpoints at the last tool definition, the first and last distinct system
|
|
172
|
+
// parts, and the conversation tail so recent prefixes remain reusable during
|
|
173
|
+
// tool loops.
|
|
174
|
+
//
|
|
175
|
+
// Pass `"none"` to opt out entirely (the legacy behavior). Pass the granular
|
|
176
|
+
// object form to override individual choices.
|
|
177
|
+
export const CachePolicyObject = Schema.Struct({
|
|
178
|
+
tools: Schema.optional(Schema.Boolean),
|
|
179
|
+
system: Schema.optional(Schema.Boolean),
|
|
180
|
+
messages: Schema.optional(Schema.Union([
|
|
181
|
+
Schema.Literal("latest-user-message"),
|
|
182
|
+
Schema.Literal("latest-assistant"),
|
|
183
|
+
Schema.Struct({ tail: Schema.Natural }),
|
|
184
|
+
])),
|
|
185
|
+
ttlSeconds: Schema.optional(Schema.Number),
|
|
186
|
+
});
|
|
187
|
+
export const CachePolicy = Schema.Union([Schema.Literal("auto"), Schema.Literal("none"), CachePolicyObject]);
|