@tinyweb_dev/agent-framework-llm 0.1.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (177) hide show
  1. package/dist/cache-policy.d.ts +3 -0
  2. package/dist/cache-policy.d.ts.map +1 -0
  3. package/dist/index.cjs +2956 -0
  4. package/dist/index.cjs.map +1 -0
  5. package/dist/index.d.ts +23 -0
  6. package/dist/index.d.ts.map +1 -0
  7. package/dist/index.js +2831 -0
  8. package/dist/index.js.map +1 -0
  9. package/dist/llm.cjs +1718 -0
  10. package/dist/llm.cjs.map +1 -0
  11. package/dist/llm.d.ts +284 -0
  12. package/dist/llm.d.ts.map +1 -0
  13. package/dist/llm.js +1692 -0
  14. package/dist/llm.js.map +1 -0
  15. package/dist/protocols/openai-chat.cjs +2443 -0
  16. package/dist/protocols/openai-chat.cjs.map +1 -0
  17. package/dist/protocols/openai-chat.d.ts +357 -0
  18. package/dist/protocols/openai-chat.d.ts.map +1 -0
  19. package/dist/protocols/openai-chat.js +2414 -0
  20. package/dist/protocols/openai-chat.js.map +1 -0
  21. package/dist/protocols/openai-compatible-chat.cjs +2443 -0
  22. package/dist/protocols/openai-compatible-chat.cjs.map +1 -0
  23. package/dist/protocols/openai-compatible-chat.d.ts +77 -0
  24. package/dist/protocols/openai-compatible-chat.d.ts.map +1 -0
  25. package/dist/protocols/openai-compatible-chat.js +2419 -0
  26. package/dist/protocols/openai-compatible-chat.js.map +1 -0
  27. package/dist/protocols/shared.cjs +904 -0
  28. package/dist/protocols/shared.cjs.map +1 -0
  29. package/dist/protocols/shared.d.ts +51 -0
  30. package/dist/protocols/shared.d.ts.map +1 -0
  31. package/dist/protocols/shared.js +857 -0
  32. package/dist/protocols/shared.js.map +1 -0
  33. package/dist/protocols/utils/openai-options.d.ts +15 -0
  34. package/dist/protocols/utils/openai-options.d.ts.map +1 -0
  35. package/dist/protocols/utils/tool-stream.d.ts +78 -0
  36. package/dist/protocols/utils/tool-stream.d.ts.map +1 -0
  37. package/dist/provider.cjs +20 -0
  38. package/dist/provider.cjs.map +1 -0
  39. package/dist/provider.d.ts +19 -0
  40. package/dist/provider.d.ts.map +1 -0
  41. package/dist/provider.js +17 -0
  42. package/dist/provider.js.map +1 -0
  43. package/{src/providers/openai-compatible-profile.ts → dist/providers/openai-compatible-profile.cjs} +12 -12
  44. package/dist/providers/openai-compatible-profile.cjs.map +1 -0
  45. package/dist/providers/openai-compatible-profile.d.ts +52 -0
  46. package/dist/providers/openai-compatible-profile.d.ts.map +1 -0
  47. package/dist/providers/openai-compatible-profile.js +21 -0
  48. package/dist/providers/openai-compatible-profile.js.map +1 -0
  49. package/dist/providers/openai-compatible.cjs +2533 -0
  50. package/dist/providers/openai-compatible.cjs.map +1 -0
  51. package/dist/providers/openai-compatible.d.ts +128 -0
  52. package/dist/providers/openai-compatible.d.ts.map +1 -0
  53. package/dist/providers/openai-compatible.js +2495 -0
  54. package/dist/providers/openai-compatible.js.map +1 -0
  55. package/dist/providers/openai-options.cjs +797 -0
  56. package/dist/providers/openai-options.cjs.map +1 -0
  57. package/dist/providers/openai-options.d.ts +29 -0
  58. package/dist/providers/openai-options.d.ts.map +1 -0
  59. package/dist/providers/openai-options.js +792 -0
  60. package/dist/providers/openai-options.js.map +1 -0
  61. package/dist/providers/openai.cjs +2517 -0
  62. package/dist/providers/openai.cjs.map +1 -0
  63. package/dist/providers/openai.d.ts +87 -0
  64. package/dist/providers/openai.d.ts.map +1 -0
  65. package/dist/providers/openai.js +2489 -0
  66. package/dist/providers/openai.js.map +1 -0
  67. package/dist/route/auth-options.cjs +855 -0
  68. package/dist/route/auth-options.cjs.map +1 -0
  69. package/dist/route/auth-options.d.ts +25 -0
  70. package/dist/route/auth-options.d.ts.map +1 -0
  71. package/dist/route/auth-options.js +852 -0
  72. package/dist/route/auth-options.js.map +1 -0
  73. package/dist/route/auth.cjs +855 -0
  74. package/dist/route/auth.cjs.map +1 -0
  75. package/dist/route/auth.d.ts +51 -0
  76. package/dist/route/auth.d.ts.map +1 -0
  77. package/dist/route/auth.js +836 -0
  78. package/dist/route/auth.js.map +1 -0
  79. package/dist/route/client.cjs +1844 -0
  80. package/dist/route/client.cjs.map +1 -0
  81. package/dist/route/client.d.ts +268 -0
  82. package/dist/route/client.d.ts.map +1 -0
  83. package/dist/route/client.js +1830 -0
  84. package/dist/route/client.js.map +1 -0
  85. package/dist/route/endpoint.cjs +756 -0
  86. package/dist/route/endpoint.cjs.map +1 -0
  87. package/dist/route/endpoint.d.ts +13 -0
  88. package/dist/route/endpoint.d.ts.map +1 -0
  89. package/dist/route/endpoint.js +752 -0
  90. package/dist/route/endpoint.js.map +1 -0
  91. package/dist/route/executor.cjs +1077 -0
  92. package/dist/route/executor.cjs.map +1 -0
  93. package/dist/route/executor.d.ts +13 -0
  94. package/dist/route/executor.d.ts.map +1 -0
  95. package/dist/route/executor.js +1072 -0
  96. package/dist/route/executor.js.map +1 -0
  97. package/dist/route/framing.cjs +773 -0
  98. package/dist/route/framing.cjs.map +1 -0
  99. package/dist/route/framing.d.ts +9 -0
  100. package/dist/route/framing.d.ts.map +1 -0
  101. package/dist/route/framing.js +750 -0
  102. package/dist/route/framing.js.map +1 -0
  103. package/dist/route/overflow-detect.d.ts +18 -0
  104. package/dist/route/overflow-detect.d.ts.map +1 -0
  105. package/dist/route/protocol.cjs +25 -0
  106. package/dist/route/protocol.cjs.map +1 -0
  107. package/dist/route/protocol.d.ts +40 -0
  108. package/dist/route/protocol.d.ts.map +1 -0
  109. package/dist/route/protocol.js +21 -0
  110. package/dist/route/protocol.js.map +1 -0
  111. package/dist/route/transport/http.d.ts +48 -0
  112. package/dist/route/transport/http.d.ts.map +1 -0
  113. package/dist/route/transport/index.cjs +969 -0
  114. package/dist/route/transport/index.cjs.map +1 -0
  115. package/dist/route/transport/index.d.ts +13 -0
  116. package/dist/route/transport/index.d.ts.map +1 -0
  117. package/dist/route/transport/index.js +967 -0
  118. package/dist/route/transport/index.js.map +1 -0
  119. package/dist/schema/errors.d.ts +181 -0
  120. package/dist/schema/errors.d.ts.map +1 -0
  121. package/dist/schema/events.d.ts +3224 -0
  122. package/dist/schema/events.d.ts.map +1 -0
  123. package/dist/schema/ids.d.ts +31 -0
  124. package/dist/schema/ids.d.ts.map +1 -0
  125. package/dist/schema/index.cjs +847 -0
  126. package/dist/schema/index.cjs.map +1 -0
  127. package/dist/schema/index.d.ts +6 -0
  128. package/dist/schema/index.d.ts.map +1 -0
  129. package/dist/schema/index.js +777 -0
  130. package/dist/schema/index.js.map +1 -0
  131. package/dist/schema/messages.d.ts +292 -0
  132. package/dist/schema/messages.d.ts.map +1 -0
  133. package/dist/schema/options.d.ts +123 -0
  134. package/dist/schema/options.d.ts.map +1 -0
  135. package/dist/tool-runtime.cjs +907 -0
  136. package/dist/tool-runtime.cjs.map +1 -0
  137. package/dist/tool-runtime.d.ts +50 -0
  138. package/dist/tool-runtime.d.ts.map +1 -0
  139. package/dist/tool-runtime.js +901 -0
  140. package/dist/tool-runtime.js.map +1 -0
  141. package/dist/tool.cjs +802 -0
  142. package/dist/tool.cjs.map +1 -0
  143. package/dist/tool.d.ts +111 -0
  144. package/dist/tool.d.ts.map +1 -0
  145. package/dist/tool.js +796 -0
  146. package/dist/tool.js.map +1 -0
  147. package/package.json +112 -29
  148. package/src/cache-policy.ts +0 -92
  149. package/src/index.ts +0 -62
  150. package/src/llm.ts +0 -210
  151. package/src/protocols/openai-chat.ts +0 -422
  152. package/src/protocols/openai-compatible-chat.ts +0 -29
  153. package/src/protocols/shared.ts +0 -204
  154. package/src/protocols/utils/openai-options.ts +0 -56
  155. package/src/protocols/utils/tool-stream.ts +0 -135
  156. package/src/provider.ts +0 -32
  157. package/src/providers/openai-compatible.ts +0 -69
  158. package/src/providers/openai-options.ts +0 -71
  159. package/src/providers/openai.ts +0 -39
  160. package/src/route/auth-options.ts +0 -48
  161. package/src/route/auth.ts +0 -198
  162. package/src/route/client.ts +0 -459
  163. package/src/route/endpoint.ts +0 -28
  164. package/src/route/executor.ts +0 -396
  165. package/src/route/framing.ts +0 -13
  166. package/src/route/overflow-detect.ts +0 -74
  167. package/src/route/protocol.ts +0 -48
  168. package/src/route/transport/http.ts +0 -123
  169. package/src/route/transport/index.ts +0 -21
  170. package/src/schema/errors.ts +0 -225
  171. package/src/schema/events.ts +0 -333
  172. package/src/schema/ids.ts +0 -44
  173. package/src/schema/index.ts +0 -6
  174. package/src/schema/messages.ts +0 -240
  175. package/src/schema/options.ts +0 -231
  176. package/src/tool-runtime.ts +0 -263
  177. package/src/tool.ts +0 -212
@@ -1,240 +0,0 @@
1
- // Adapted from opencode@v1.14.48 packages/llm/src/schema/messages.ts
2
- import { Schema } from "effect"
3
- import { JsonSchema, MessageRole, ProviderMetadata } from "./ids"
4
- import { CacheHint, CachePolicy, GenerationOptions, HttpOptions, ModelRef, ProviderOptions } from "./options"
5
-
6
- const isRecord = (value: unknown): value is Record<string, unknown> =>
7
- typeof value === "object" && value !== null && !Array.isArray(value)
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
- export type SystemPart = Schema.Schema.Type<typeof systemPartSchema>
16
-
17
- const makeSystemPart = (text: string): SystemPart => ({ type: "text", text })
18
-
19
- export const SystemPart = Object.assign(systemPartSchema, {
20
- make: makeSystemPart,
21
- content: (input?: string | SystemPart | ReadonlyArray<SystemPart>) => {
22
- if (input === undefined) return []
23
- return typeof input === "string" ? [makeSystemPart(input)] : Array.isArray(input) ? [...input] : [input]
24
- },
25
- })
26
-
27
- export const TextPart = Schema.Struct({
28
- type: Schema.Literal("text"),
29
- text: Schema.String,
30
- cache: Schema.optional(CacheHint),
31
- metadata: Schema.optional(Schema.Record(Schema.String, Schema.Unknown)),
32
- providerMetadata: Schema.optional(ProviderMetadata),
33
- }).annotate({ identifier: "LLM.Content.Text" })
34
- export type TextPart = Schema.Schema.Type<typeof TextPart>
35
-
36
- export const MediaPart = Schema.Struct({
37
- type: Schema.Literal("media"),
38
- mediaType: Schema.String,
39
- data: Schema.Union([Schema.String, Schema.Uint8Array]),
40
- filename: Schema.optional(Schema.String),
41
- metadata: Schema.optional(Schema.Record(Schema.String, Schema.Unknown)),
42
- }).annotate({ identifier: "LLM.Content.Media" })
43
- export type MediaPart = Schema.Schema.Type<typeof MediaPart>
44
-
45
- const isToolResultValue = (value: unknown): value is ToolResultValue =>
46
- isRecord(value) && (value.type === "text" || value.type === "json" || value.type === "error") && "value" in value
47
-
48
- export const ToolResultValue = Object.assign(
49
- Schema.Struct({
50
- type: Schema.Literals(["json", "text", "error"]),
51
- value: Schema.Unknown,
52
- }).annotate({ identifier: "LLM.ToolResult" }),
53
- {
54
- make: (value: unknown, type: ToolResultValue["type"] = "json"): ToolResultValue =>
55
- isToolResultValue(value) ? value : { type, value },
56
- },
57
- )
58
- export type ToolResultValue = Schema.Schema.Type<typeof ToolResultValue>
59
-
60
- export const ToolCallPart = Object.assign(
61
- Schema.Struct({
62
- type: Schema.Literal("tool-call"),
63
- id: Schema.String,
64
- name: Schema.String,
65
- input: Schema.Unknown,
66
- providerExecuted: Schema.optional(Schema.Boolean),
67
- metadata: Schema.optional(Schema.Record(Schema.String, Schema.Unknown)),
68
- providerMetadata: Schema.optional(ProviderMetadata),
69
- }).annotate({ identifier: "LLM.Content.ToolCall" }),
70
- {
71
- make: (input: Omit<ToolCallPart, "type">): ToolCallPart => ({ type: "tool-call", ...input }),
72
- },
73
- )
74
- export type ToolCallPart = Schema.Schema.Type<typeof ToolCallPart>
75
-
76
- export const ToolResultPart = Object.assign(
77
- Schema.Struct({
78
- type: Schema.Literal("tool-result"),
79
- id: Schema.String,
80
- name: Schema.String,
81
- result: ToolResultValue,
82
- providerExecuted: Schema.optional(Schema.Boolean),
83
- cache: Schema.optional(CacheHint),
84
- metadata: Schema.optional(Schema.Record(Schema.String, Schema.Unknown)),
85
- providerMetadata: Schema.optional(ProviderMetadata),
86
- }).annotate({ identifier: "LLM.Content.ToolResult" }),
87
- {
88
- make: (
89
- input: Omit<ToolResultPart, "type" | "result"> & {
90
- readonly result: unknown
91
- readonly resultType?: ToolResultValue["type"]
92
- },
93
- ): ToolResultPart => ({
94
- type: "tool-result",
95
- id: input.id,
96
- name: input.name,
97
- result: ToolResultValue.make(input.result, input.resultType),
98
- providerExecuted: input.providerExecuted,
99
- cache: input.cache,
100
- metadata: input.metadata,
101
- providerMetadata: input.providerMetadata,
102
- }),
103
- },
104
- )
105
- export type ToolResultPart = Schema.Schema.Type<typeof ToolResultPart>
106
-
107
- export const ReasoningPart = Schema.Struct({
108
- type: Schema.Literal("reasoning"),
109
- text: Schema.String,
110
- encrypted: Schema.optional(Schema.String),
111
- metadata: Schema.optional(Schema.Record(Schema.String, Schema.Unknown)),
112
- providerMetadata: Schema.optional(ProviderMetadata),
113
- }).annotate({ identifier: "LLM.Content.Reasoning" })
114
- export type ReasoningPart = Schema.Schema.Type<typeof ReasoningPart>
115
-
116
- export const ContentPart = Schema.Union([TextPart, MediaPart, ToolCallPart, ToolResultPart, ReasoningPart]).pipe(
117
- Schema.toTaggedUnion("type"),
118
- )
119
- export type ContentPart = Schema.Schema.Type<typeof ContentPart>
120
-
121
- export class Message extends Schema.Class<Message>("LLM.Message")({
122
- id: Schema.optional(Schema.String),
123
- role: MessageRole,
124
- content: Schema.Array(ContentPart),
125
- metadata: Schema.optional(Schema.Record(Schema.String, Schema.Unknown)),
126
- native: Schema.optional(Schema.Record(Schema.String, Schema.Unknown)),
127
- }) {}
128
-
129
- export namespace Message {
130
- export type ContentInput = string | ContentPart | ReadonlyArray<ContentPart>
131
- export type Input = Omit<ConstructorParameters<typeof Message>[0], "content"> & {
132
- readonly content: ContentInput
133
- }
134
-
135
- export const text = (value: string): ContentPart => ({ type: "text", text: value })
136
-
137
- export const content = (input: ContentInput) =>
138
- typeof input === "string" ? [text(input)] : Array.isArray(input) ? [...input] : [input]
139
-
140
- export const make = (input: Message | Input) => {
141
- if (input instanceof Message) return input
142
- return new Message({ ...input, content: content(input.content) })
143
- }
144
-
145
- export const user = (content: ContentInput) => make({ role: "user", content })
146
-
147
- export const assistant = (content: ContentInput) => make({ role: "assistant", content })
148
-
149
- export const tool = (result: ToolResultPart | Parameters<typeof ToolResultPart.make>[0]) =>
150
- make({ role: "tool", content: ["type" in result ? result : ToolResultPart.make(result)] })
151
- }
152
-
153
- export class ToolDefinition extends Schema.Class<ToolDefinition>("LLM.ToolDefinition")({
154
- name: Schema.String,
155
- description: Schema.String,
156
- inputSchema: JsonSchema,
157
- cache: Schema.optional(CacheHint),
158
- metadata: Schema.optional(Schema.Record(Schema.String, Schema.Unknown)),
159
- native: Schema.optional(Schema.Record(Schema.String, Schema.Unknown)),
160
- }) {}
161
-
162
- export namespace ToolDefinition {
163
- export type Input = ToolDefinition | ConstructorParameters<typeof ToolDefinition>[0]
164
-
165
- /** Normalize tool definition input into the canonical `ToolDefinition` class. */
166
- export const make = (input: Input) => (input instanceof ToolDefinition ? input : new ToolDefinition(input))
167
- }
168
-
169
- export class ToolChoice extends Schema.Class<ToolChoice>("LLM.ToolChoice")({
170
- type: Schema.Literals(["auto", "none", "required", "tool"]),
171
- name: Schema.optional(Schema.String),
172
- }) {}
173
-
174
- export namespace ToolChoice {
175
- export type Mode = Exclude<ToolChoice["type"], "tool">
176
- export type Input = ToolChoice | ConstructorParameters<typeof ToolChoice>[0] | ToolDefinition | string
177
-
178
- const isMode = (value: string): value is Mode => value === "auto" || value === "none" || value === "required"
179
-
180
- /** Select a specific named tool. */
181
- export const named = (value: string) => new ToolChoice({ type: "tool", name: value })
182
-
183
- /** Normalize ergonomic tool-choice inputs into the canonical `ToolChoice` class. */
184
- export const make = (input: Input) => {
185
- if (input instanceof ToolChoice) return input
186
- if (input instanceof ToolDefinition) return named(input.name)
187
- if (typeof input === "string") return isMode(input) ? new ToolChoice({ type: input }) : named(input)
188
- return new ToolChoice(input)
189
- }
190
- }
191
-
192
- export const ResponseFormat = Schema.Union([
193
- Schema.Struct({ type: Schema.Literal("text") }),
194
- Schema.Struct({ type: Schema.Literal("json"), schema: JsonSchema }),
195
- Schema.Struct({ type: Schema.Literal("tool"), tool: ToolDefinition }),
196
- ]).pipe(Schema.toTaggedUnion("type"))
197
- export type ResponseFormat = Schema.Schema.Type<typeof ResponseFormat>
198
-
199
- export class LLMRequest extends Schema.Class<LLMRequest>("LLM.Request")({
200
- id: Schema.optional(Schema.String),
201
- model: ModelRef,
202
- system: Schema.Array(SystemPart),
203
- messages: Schema.Array(Message),
204
- tools: Schema.Array(ToolDefinition),
205
- toolChoice: Schema.optional(ToolChoice),
206
- generation: Schema.optional(GenerationOptions),
207
- providerOptions: Schema.optional(ProviderOptions),
208
- http: Schema.optional(HttpOptions),
209
- responseFormat: Schema.optional(ResponseFormat),
210
- cache: Schema.optional(CachePolicy),
211
- metadata: Schema.optional(Schema.Record(Schema.String, Schema.Unknown)),
212
- }) {}
213
-
214
- export namespace LLMRequest {
215
- export type Input = ConstructorParameters<typeof LLMRequest>[0]
216
-
217
- export const input = (request: LLMRequest): Input => ({
218
- id: request.id,
219
- model: request.model,
220
- system: request.system,
221
- messages: request.messages,
222
- tools: request.tools,
223
- toolChoice: request.toolChoice,
224
- generation: request.generation,
225
- providerOptions: request.providerOptions,
226
- http: request.http,
227
- responseFormat: request.responseFormat,
228
- cache: request.cache,
229
- metadata: request.metadata,
230
- })
231
-
232
- export const update = (request: LLMRequest, patch: Partial<Input>) => {
233
- if (Object.keys(patch).length === 0) return request
234
- return new LLMRequest({
235
- ...input(request),
236
- ...patch,
237
- model: patch.model ?? request.model,
238
- })
239
- }
240
- }
@@ -1,231 +0,0 @@
1
- // Adapted from opencode@v1.14.48 packages/llm/src/schema/options.ts
2
- import { Schema } from "effect"
3
- import { JsonSchema, ModelID, ProviderID, RouteID } from "./ids"
4
-
5
- const isRecord = (value: unknown): value is Record<string, unknown> =>
6
- typeof value === "object" && value !== null && !Array.isArray(value)
7
-
8
- export const mergeJsonRecords = (
9
- ...items: ReadonlyArray<Record<string, unknown> | undefined>
10
- ): Record<string, unknown> | undefined => {
11
- const defined = items.filter((item): item is Record<string, unknown> => item !== undefined)
12
- if (defined.length === 0) return undefined
13
- if (defined.length === 1 && Object.values(defined[0]).every((value) => value !== undefined)) return defined[0]
14
- const result: Record<string, unknown> = {}
15
- for (const item of defined) {
16
- for (const [key, value] of Object.entries(item)) {
17
- if (value === undefined) continue
18
- result[key] = isRecord(result[key]) && isRecord(value) ? mergeJsonRecords(result[key], value) : value
19
- }
20
- }
21
- return Object.keys(result).length === 0 ? undefined : result
22
- }
23
-
24
- const mergeStringRecords = (
25
- ...items: ReadonlyArray<Record<string, string> | undefined>
26
- ): Record<string, string> | undefined => {
27
- const defined = items.filter((item): item is Record<string, string> => item !== undefined)
28
- if (defined.length === 0) return undefined
29
- if (defined.length === 1) return defined[0]
30
- const result = Object.fromEntries(
31
- defined.flatMap((item) =>
32
- Object.entries(item).filter((entry): entry is [string, string] => entry[1] !== undefined),
33
- ),
34
- )
35
- return Object.keys(result).length === 0 ? undefined : result
36
- }
37
-
38
- export const ProviderOptions = Schema.Record(Schema.String, Schema.Record(Schema.String, Schema.Unknown))
39
- export type ProviderOptions = Schema.Schema.Type<typeof ProviderOptions>
40
-
41
- export const mergeProviderOptions = (
42
- ...items: ReadonlyArray<ProviderOptions | undefined>
43
- ): ProviderOptions | undefined => {
44
- const result: Record<string, Record<string, unknown>> = {}
45
- for (const item of items) {
46
- if (!item) continue
47
- for (const [provider, options] of Object.entries(item)) {
48
- const merged = mergeJsonRecords(result[provider], options)
49
- if (merged) result[provider] = merged
50
- }
51
- }
52
- return Object.keys(result).length === 0 ? undefined : result
53
- }
54
-
55
- export class HttpOptions extends Schema.Class<HttpOptions>("LLM.HttpOptions")({
56
- body: Schema.optional(JsonSchema),
57
- headers: Schema.optional(Schema.Record(Schema.String, Schema.String)),
58
- query: Schema.optional(Schema.Record(Schema.String, Schema.String)),
59
- }) {}
60
-
61
- export namespace HttpOptions {
62
- export type Input = HttpOptions | ConstructorParameters<typeof HttpOptions>[0]
63
-
64
- /** Normalize HTTP option input into the canonical `HttpOptions` class. */
65
- export const make = (input: Input) => (input instanceof HttpOptions ? input : new HttpOptions(input))
66
- }
67
-
68
- export const mergeHttpOptions = (...items: ReadonlyArray<HttpOptions | undefined>): HttpOptions | undefined => {
69
- const body = mergeJsonRecords(...items.map((item) => item?.body))
70
- const headers = mergeStringRecords(...items.map((item) => item?.headers))
71
- const query = mergeStringRecords(...items.map((item) => item?.query))
72
- if (!body && !headers && !query) return undefined
73
- return new HttpOptions({ body, headers, query })
74
- }
75
-
76
- export class GenerationOptions extends Schema.Class<GenerationOptions>("LLM.GenerationOptions")({
77
- maxTokens: Schema.optional(Schema.Number),
78
- temperature: Schema.optional(Schema.Number),
79
- topP: Schema.optional(Schema.Number),
80
- topK: Schema.optional(Schema.Number),
81
- frequencyPenalty: Schema.optional(Schema.Number),
82
- presencePenalty: Schema.optional(Schema.Number),
83
- seed: Schema.optional(Schema.Number),
84
- stop: Schema.optional(Schema.Array(Schema.String)),
85
- }) {}
86
-
87
- export namespace GenerationOptions {
88
- export type Input = GenerationOptions | ConstructorParameters<typeof GenerationOptions>[0]
89
-
90
- /** Normalize generation option input into the canonical `GenerationOptions` class. */
91
- export const make = (input: Input = {}) => (input instanceof GenerationOptions ? input : new GenerationOptions(input))
92
- }
93
-
94
- export type GenerationOptionsFields = {
95
- readonly maxTokens?: number
96
- readonly temperature?: number
97
- readonly topP?: number
98
- readonly topK?: number
99
- readonly frequencyPenalty?: number
100
- readonly presencePenalty?: number
101
- readonly seed?: number
102
- readonly stop?: ReadonlyArray<string>
103
- }
104
-
105
- export type GenerationOptionsInput = GenerationOptions | GenerationOptionsFields
106
-
107
- const latestGeneration = <Key extends keyof GenerationOptionsFields>(
108
- items: ReadonlyArray<GenerationOptionsInput | undefined>,
109
- key: Key,
110
- ) => items.findLast((item) => item?.[key] !== undefined)?.[key]
111
-
112
- export const mergeGenerationOptions = (...items: ReadonlyArray<GenerationOptionsInput | undefined>) => {
113
- const result = new GenerationOptions({
114
- maxTokens: latestGeneration(items, "maxTokens"),
115
- temperature: latestGeneration(items, "temperature"),
116
- topP: latestGeneration(items, "topP"),
117
- topK: latestGeneration(items, "topK"),
118
- frequencyPenalty: latestGeneration(items, "frequencyPenalty"),
119
- presencePenalty: latestGeneration(items, "presencePenalty"),
120
- seed: latestGeneration(items, "seed"),
121
- stop: latestGeneration(items, "stop"),
122
- })
123
- return Object.values(result).some((value) => value !== undefined) ? result : undefined
124
- }
125
-
126
- export class ModelLimits extends Schema.Class<ModelLimits>("LLM.ModelLimits")({
127
- context: Schema.optional(Schema.Number),
128
- output: Schema.optional(Schema.Number),
129
- }) {}
130
-
131
- export namespace ModelLimits {
132
- export type Input = ModelLimits | ConstructorParameters<typeof ModelLimits>[0]
133
-
134
- /** Normalize model limit input into the canonical `ModelLimits` class. */
135
- export const make = (input: Input | undefined) =>
136
- input instanceof ModelLimits ? input : new ModelLimits(input ?? {})
137
- }
138
-
139
- export class ModelRef extends Schema.Class<ModelRef>("LLM.ModelRef")({
140
- id: ModelID,
141
- provider: ProviderID,
142
- route: RouteID,
143
- baseURL: Schema.String,
144
- /** Provider-specific API key convenience. Provider helpers normalize this into `auth`. */
145
- apiKey: Schema.optional(Schema.String),
146
- /** Optional transport auth policy. Opaque because it may contain functions. */
147
- auth: Schema.optional(Schema.Any),
148
- headers: Schema.optional(Schema.Record(Schema.String, Schema.String)),
149
- /**
150
- * Query params appended to the request URL by `Endpoint.baseURL`. Used for
151
- * deployment-level URL-scoped settings such as Azure's `api-version` or any
152
- * provider that requires a per-request key in the URL. Generic concern, so
153
- * lives as a typed first-class field instead of `native`.
154
- */
155
- queryParams: Schema.optional(Schema.Record(Schema.String, Schema.String)),
156
- limits: ModelLimits,
157
- /** Provider-neutral generation defaults. Request-level values override them. */
158
- generation: Schema.optional(GenerationOptions),
159
- /** Provider-owned typed-at-the-facade options for non-portable knobs. */
160
- providerOptions: Schema.optional(ProviderOptions),
161
- /** Serializable raw HTTP overlays applied to the final outgoing request. */
162
- http: Schema.optional(HttpOptions),
163
- /**
164
- * Provider-specific opaque options. Reach for this only when the value is
165
- * genuinely provider-private and does not fit a typed axis (e.g. Bedrock's
166
- * `aws_credentials` / `aws_region` for SigV4). Anything used by more than
167
- * one route should grow into a typed field instead.
168
- */
169
- native: Schema.optional(Schema.Record(Schema.String, Schema.Unknown)),
170
- }) {}
171
-
172
- export namespace ModelRef {
173
- export type Input = ConstructorParameters<typeof ModelRef>[0]
174
-
175
- export const input = (model: ModelRef): Input => ({
176
- id: model.id,
177
- provider: model.provider,
178
- route: model.route,
179
- baseURL: model.baseURL,
180
- apiKey: model.apiKey,
181
- auth: model.auth,
182
- headers: model.headers,
183
- queryParams: model.queryParams,
184
- limits: model.limits,
185
- generation: model.generation,
186
- providerOptions: model.providerOptions,
187
- http: model.http,
188
- native: model.native,
189
- })
190
-
191
- export const update = (model: ModelRef, patch: Partial<Input>) => {
192
- if (Object.keys(patch).length === 0) return model
193
- return new ModelRef({
194
- ...input(model),
195
- ...patch,
196
- })
197
- }
198
- }
199
-
200
- export class CacheHint extends Schema.Class<CacheHint>("LLM.CacheHint")({
201
- type: Schema.Literals(["ephemeral", "persistent"]),
202
- ttlSeconds: Schema.optional(Schema.Number),
203
- }) {}
204
-
205
- // Auto-placement policy for prompt caching. The protocol-neutral lowering step
206
- // reads this and injects `CacheHint`s at the configured boundaries; the
207
- // per-protocol body builders then translate those hints into wire markers as
208
- // usual. `"auto"` is the recommended default for agent loops — it places one
209
- // breakpoint at the last tool definition, one at the last system part, and one
210
- // at the latest user message. The combination of provider invalidation
211
- // hierarchy (tools → system → messages) and Anthropic/Bedrock's 20-block
212
- // lookback means three trailing breakpoints reliably cover the static prefix.
213
- //
214
- // Pass `"none"` to opt out entirely (the legacy behavior). Pass the granular
215
- // object form to override individual choices.
216
- export const CachePolicyObject = Schema.Struct({
217
- tools: Schema.optional(Schema.Boolean),
218
- system: Schema.optional(Schema.Boolean),
219
- messages: Schema.optional(
220
- Schema.Union([
221
- Schema.Literal("latest-user-message"),
222
- Schema.Literal("latest-assistant"),
223
- Schema.Struct({ tail: Schema.Number }),
224
- ]),
225
- ),
226
- ttlSeconds: Schema.optional(Schema.Number),
227
- })
228
- export type CachePolicyObject = Schema.Schema.Type<typeof CachePolicyObject>
229
-
230
- export const CachePolicy = Schema.Union([Schema.Literal("auto"), Schema.Literal("none"), CachePolicyObject])
231
- export type CachePolicy = Schema.Schema.Type<typeof CachePolicy>