@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.
- package/dist/cache-policy.d.ts +3 -0
- package/dist/cache-policy.d.ts.map +1 -0
- package/dist/index.cjs +2956 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.ts +23 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +2831 -0
- package/dist/index.js.map +1 -0
- package/dist/llm.cjs +1718 -0
- package/dist/llm.cjs.map +1 -0
- package/dist/llm.d.ts +284 -0
- package/dist/llm.d.ts.map +1 -0
- package/dist/llm.js +1692 -0
- package/dist/llm.js.map +1 -0
- package/dist/protocols/openai-chat.cjs +2443 -0
- package/dist/protocols/openai-chat.cjs.map +1 -0
- package/dist/protocols/openai-chat.d.ts +357 -0
- package/dist/protocols/openai-chat.d.ts.map +1 -0
- package/dist/protocols/openai-chat.js +2414 -0
- package/dist/protocols/openai-chat.js.map +1 -0
- package/dist/protocols/openai-compatible-chat.cjs +2443 -0
- package/dist/protocols/openai-compatible-chat.cjs.map +1 -0
- package/dist/protocols/openai-compatible-chat.d.ts +77 -0
- package/dist/protocols/openai-compatible-chat.d.ts.map +1 -0
- package/dist/protocols/openai-compatible-chat.js +2419 -0
- package/dist/protocols/openai-compatible-chat.js.map +1 -0
- package/dist/protocols/shared.cjs +904 -0
- package/dist/protocols/shared.cjs.map +1 -0
- package/dist/protocols/shared.d.ts +51 -0
- package/dist/protocols/shared.d.ts.map +1 -0
- package/dist/protocols/shared.js +857 -0
- package/dist/protocols/shared.js.map +1 -0
- package/dist/protocols/utils/openai-options.d.ts +15 -0
- package/dist/protocols/utils/openai-options.d.ts.map +1 -0
- package/dist/protocols/utils/tool-stream.d.ts +78 -0
- package/dist/protocols/utils/tool-stream.d.ts.map +1 -0
- package/dist/provider.cjs +20 -0
- package/dist/provider.cjs.map +1 -0
- package/dist/provider.d.ts +19 -0
- package/dist/provider.d.ts.map +1 -0
- package/dist/provider.js +17 -0
- package/dist/provider.js.map +1 -0
- package/{src/providers/openai-compatible-profile.ts → dist/providers/openai-compatible-profile.cjs} +12 -12
- package/dist/providers/openai-compatible-profile.cjs.map +1 -0
- package/dist/providers/openai-compatible-profile.d.ts +52 -0
- package/dist/providers/openai-compatible-profile.d.ts.map +1 -0
- package/dist/providers/openai-compatible-profile.js +21 -0
- package/dist/providers/openai-compatible-profile.js.map +1 -0
- package/dist/providers/openai-compatible.cjs +2533 -0
- package/dist/providers/openai-compatible.cjs.map +1 -0
- package/dist/providers/openai-compatible.d.ts +128 -0
- package/dist/providers/openai-compatible.d.ts.map +1 -0
- package/dist/providers/openai-compatible.js +2495 -0
- package/dist/providers/openai-compatible.js.map +1 -0
- package/dist/providers/openai-options.cjs +797 -0
- package/dist/providers/openai-options.cjs.map +1 -0
- package/dist/providers/openai-options.d.ts +29 -0
- package/dist/providers/openai-options.d.ts.map +1 -0
- package/dist/providers/openai-options.js +792 -0
- package/dist/providers/openai-options.js.map +1 -0
- package/dist/providers/openai.cjs +2517 -0
- package/dist/providers/openai.cjs.map +1 -0
- package/dist/providers/openai.d.ts +87 -0
- package/dist/providers/openai.d.ts.map +1 -0
- package/dist/providers/openai.js +2489 -0
- package/dist/providers/openai.js.map +1 -0
- package/dist/route/auth-options.cjs +855 -0
- package/dist/route/auth-options.cjs.map +1 -0
- package/dist/route/auth-options.d.ts +25 -0
- package/dist/route/auth-options.d.ts.map +1 -0
- package/dist/route/auth-options.js +852 -0
- package/dist/route/auth-options.js.map +1 -0
- package/dist/route/auth.cjs +855 -0
- package/dist/route/auth.cjs.map +1 -0
- package/dist/route/auth.d.ts +51 -0
- package/dist/route/auth.d.ts.map +1 -0
- package/dist/route/auth.js +836 -0
- package/dist/route/auth.js.map +1 -0
- package/dist/route/client.cjs +1844 -0
- package/dist/route/client.cjs.map +1 -0
- package/dist/route/client.d.ts +268 -0
- package/dist/route/client.d.ts.map +1 -0
- package/dist/route/client.js +1830 -0
- package/dist/route/client.js.map +1 -0
- package/dist/route/endpoint.cjs +756 -0
- package/dist/route/endpoint.cjs.map +1 -0
- package/dist/route/endpoint.d.ts +13 -0
- package/dist/route/endpoint.d.ts.map +1 -0
- package/dist/route/endpoint.js +752 -0
- package/dist/route/endpoint.js.map +1 -0
- package/dist/route/executor.cjs +1077 -0
- package/dist/route/executor.cjs.map +1 -0
- package/dist/route/executor.d.ts +13 -0
- package/dist/route/executor.d.ts.map +1 -0
- package/dist/route/executor.js +1072 -0
- package/dist/route/executor.js.map +1 -0
- package/dist/route/framing.cjs +773 -0
- package/dist/route/framing.cjs.map +1 -0
- package/dist/route/framing.d.ts +9 -0
- package/dist/route/framing.d.ts.map +1 -0
- package/dist/route/framing.js +750 -0
- package/dist/route/framing.js.map +1 -0
- package/dist/route/overflow-detect.d.ts +18 -0
- package/dist/route/overflow-detect.d.ts.map +1 -0
- package/dist/route/protocol.cjs +25 -0
- package/dist/route/protocol.cjs.map +1 -0
- package/dist/route/protocol.d.ts +40 -0
- package/dist/route/protocol.d.ts.map +1 -0
- package/dist/route/protocol.js +21 -0
- package/dist/route/protocol.js.map +1 -0
- package/dist/route/transport/http.d.ts +48 -0
- package/dist/route/transport/http.d.ts.map +1 -0
- package/dist/route/transport/index.cjs +969 -0
- package/dist/route/transport/index.cjs.map +1 -0
- package/dist/route/transport/index.d.ts +13 -0
- package/dist/route/transport/index.d.ts.map +1 -0
- package/dist/route/transport/index.js +967 -0
- package/dist/route/transport/index.js.map +1 -0
- package/dist/schema/errors.d.ts +181 -0
- package/dist/schema/errors.d.ts.map +1 -0
- package/dist/schema/events.d.ts +3224 -0
- package/dist/schema/events.d.ts.map +1 -0
- package/dist/schema/ids.d.ts +31 -0
- package/dist/schema/ids.d.ts.map +1 -0
- package/dist/schema/index.cjs +847 -0
- package/dist/schema/index.cjs.map +1 -0
- package/dist/schema/index.d.ts +6 -0
- package/dist/schema/index.d.ts.map +1 -0
- package/dist/schema/index.js +777 -0
- package/dist/schema/index.js.map +1 -0
- package/dist/schema/messages.d.ts +292 -0
- package/dist/schema/messages.d.ts.map +1 -0
- package/dist/schema/options.d.ts +123 -0
- package/dist/schema/options.d.ts.map +1 -0
- package/dist/tool-runtime.cjs +907 -0
- package/dist/tool-runtime.cjs.map +1 -0
- package/dist/tool-runtime.d.ts +50 -0
- package/dist/tool-runtime.d.ts.map +1 -0
- package/dist/tool-runtime.js +901 -0
- package/dist/tool-runtime.js.map +1 -0
- package/dist/tool.cjs +802 -0
- package/dist/tool.cjs.map +1 -0
- package/dist/tool.d.ts +111 -0
- package/dist/tool.d.ts.map +1 -0
- package/dist/tool.js +796 -0
- package/dist/tool.js.map +1 -0
- package/package.json +112 -29
- package/src/cache-policy.ts +0 -92
- package/src/index.ts +0 -62
- package/src/llm.ts +0 -210
- package/src/protocols/openai-chat.ts +0 -422
- package/src/protocols/openai-compatible-chat.ts +0 -29
- package/src/protocols/shared.ts +0 -204
- package/src/protocols/utils/openai-options.ts +0 -56
- package/src/protocols/utils/tool-stream.ts +0 -135
- package/src/provider.ts +0 -32
- package/src/providers/openai-compatible.ts +0 -69
- package/src/providers/openai-options.ts +0 -71
- package/src/providers/openai.ts +0 -39
- package/src/route/auth-options.ts +0 -48
- package/src/route/auth.ts +0 -198
- package/src/route/client.ts +0 -459
- package/src/route/endpoint.ts +0 -28
- package/src/route/executor.ts +0 -396
- package/src/route/framing.ts +0 -13
- package/src/route/overflow-detect.ts +0 -74
- package/src/route/protocol.ts +0 -48
- package/src/route/transport/http.ts +0 -123
- package/src/route/transport/index.ts +0 -21
- package/src/schema/errors.ts +0 -225
- package/src/schema/events.ts +0 -333
- package/src/schema/ids.ts +0 -44
- package/src/schema/index.ts +0 -6
- package/src/schema/messages.ts +0 -240
- package/src/schema/options.ts +0 -231
- package/src/tool-runtime.ts +0 -263
- package/src/tool.ts +0 -212
package/src/schema/errors.ts
DELETED
|
@@ -1,225 +0,0 @@
|
|
|
1
|
-
// Adapted from opencode@v1.14.48 packages/llm/src/schema/errors.ts
|
|
2
|
-
import { Schema } from "effect"
|
|
3
|
-
import { ModelID, ProviderID, ProviderMetadata, RouteID } from "./ids"
|
|
4
|
-
|
|
5
|
-
export class HttpRequestDetails extends Schema.Class<HttpRequestDetails>("LLM.HttpRequestDetails")({
|
|
6
|
-
method: Schema.String,
|
|
7
|
-
url: Schema.String,
|
|
8
|
-
headers: Schema.Record(Schema.String, Schema.String),
|
|
9
|
-
}) {}
|
|
10
|
-
|
|
11
|
-
export class HttpResponseDetails extends Schema.Class<HttpResponseDetails>("LLM.HttpResponseDetails")({
|
|
12
|
-
status: Schema.Number,
|
|
13
|
-
headers: Schema.Record(Schema.String, Schema.String),
|
|
14
|
-
}) {}
|
|
15
|
-
|
|
16
|
-
export class HttpRateLimitDetails extends Schema.Class<HttpRateLimitDetails>("LLM.HttpRateLimitDetails")({
|
|
17
|
-
retryAfterMs: Schema.optional(Schema.Number),
|
|
18
|
-
limit: Schema.optional(Schema.Record(Schema.String, Schema.String)),
|
|
19
|
-
remaining: Schema.optional(Schema.Record(Schema.String, Schema.String)),
|
|
20
|
-
reset: Schema.optional(Schema.Record(Schema.String, Schema.String)),
|
|
21
|
-
}) {}
|
|
22
|
-
|
|
23
|
-
export class HttpContext extends Schema.Class<HttpContext>("LLM.HttpContext")({
|
|
24
|
-
request: HttpRequestDetails,
|
|
25
|
-
response: Schema.optional(HttpResponseDetails),
|
|
26
|
-
body: Schema.optional(Schema.String),
|
|
27
|
-
bodyTruncated: Schema.optional(Schema.Boolean),
|
|
28
|
-
requestId: Schema.optional(Schema.String),
|
|
29
|
-
rateLimit: Schema.optional(HttpRateLimitDetails),
|
|
30
|
-
}) {}
|
|
31
|
-
|
|
32
|
-
export class InvalidRequestReason extends Schema.Class<InvalidRequestReason>("LLM.Error.InvalidRequest")({
|
|
33
|
-
_tag: Schema.tag("InvalidRequest"),
|
|
34
|
-
message: Schema.String,
|
|
35
|
-
parameter: Schema.optional(Schema.String),
|
|
36
|
-
providerMetadata: Schema.optional(ProviderMetadata),
|
|
37
|
-
http: Schema.optional(HttpContext),
|
|
38
|
-
}) {
|
|
39
|
-
get retryable() {
|
|
40
|
-
return false
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
/**
|
|
45
|
-
* Provider rejected the request because input exceeds the model's context window.
|
|
46
|
-
* Surfaced for HTTP 413 or provider-specific overflow phrases such as Anthropic
|
|
47
|
-
* "prompt is too long", OpenAI "context_length_exceeded", Bedrock "input is too long
|
|
48
|
-
* for requested model". Consumers should compact the conversation history and retry.
|
|
49
|
-
*/
|
|
50
|
-
export class ContextOverflowReason extends Schema.Class<ContextOverflowReason>("LLM.Error.ContextOverflow")({
|
|
51
|
-
_tag: Schema.tag("ContextOverflow"),
|
|
52
|
-
message: Schema.String,
|
|
53
|
-
/** Provider-reported prompt token count when present in the error body. */
|
|
54
|
-
promptTokens: Schema.optional(Schema.Number),
|
|
55
|
-
/** Provider-reported context window size when present in the error body. */
|
|
56
|
-
contextWindow: Schema.optional(Schema.Number),
|
|
57
|
-
providerMetadata: Schema.optional(ProviderMetadata),
|
|
58
|
-
http: Schema.optional(HttpContext),
|
|
59
|
-
}) {
|
|
60
|
-
get retryable() {
|
|
61
|
-
return false
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
export class NoRouteReason extends Schema.Class<NoRouteReason>("LLM.Error.NoRoute")({
|
|
66
|
-
_tag: Schema.tag("NoRoute"),
|
|
67
|
-
route: RouteID,
|
|
68
|
-
provider: ProviderID,
|
|
69
|
-
model: ModelID,
|
|
70
|
-
}) {
|
|
71
|
-
get retryable() {
|
|
72
|
-
return false
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
get message() {
|
|
76
|
-
return `No LLM route for ${this.provider}/${this.model} using ${this.route}`
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
export class AuthenticationReason extends Schema.Class<AuthenticationReason>("LLM.Error.Authentication")({
|
|
81
|
-
_tag: Schema.tag("Authentication"),
|
|
82
|
-
message: Schema.String,
|
|
83
|
-
kind: Schema.Literals(["missing", "invalid", "expired", "insufficient-permissions", "unknown"]),
|
|
84
|
-
providerMetadata: Schema.optional(ProviderMetadata),
|
|
85
|
-
http: Schema.optional(HttpContext),
|
|
86
|
-
}) {
|
|
87
|
-
get retryable() {
|
|
88
|
-
return false
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
export class RateLimitReason extends Schema.Class<RateLimitReason>("LLM.Error.RateLimit")({
|
|
93
|
-
_tag: Schema.tag("RateLimit"),
|
|
94
|
-
message: Schema.String,
|
|
95
|
-
retryAfterMs: Schema.optional(Schema.Number),
|
|
96
|
-
rateLimit: Schema.optional(HttpRateLimitDetails),
|
|
97
|
-
providerMetadata: Schema.optional(ProviderMetadata),
|
|
98
|
-
http: Schema.optional(HttpContext),
|
|
99
|
-
}) {
|
|
100
|
-
get retryable() {
|
|
101
|
-
return true
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
export class QuotaExceededReason extends Schema.Class<QuotaExceededReason>("LLM.Error.QuotaExceeded")({
|
|
106
|
-
_tag: Schema.tag("QuotaExceeded"),
|
|
107
|
-
message: Schema.String,
|
|
108
|
-
providerMetadata: Schema.optional(ProviderMetadata),
|
|
109
|
-
http: Schema.optional(HttpContext),
|
|
110
|
-
}) {
|
|
111
|
-
get retryable() {
|
|
112
|
-
return false
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
export class ContentPolicyReason extends Schema.Class<ContentPolicyReason>("LLM.Error.ContentPolicy")({
|
|
117
|
-
_tag: Schema.tag("ContentPolicy"),
|
|
118
|
-
message: Schema.String,
|
|
119
|
-
providerMetadata: Schema.optional(ProviderMetadata),
|
|
120
|
-
http: Schema.optional(HttpContext),
|
|
121
|
-
}) {
|
|
122
|
-
get retryable() {
|
|
123
|
-
return false
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
export class ProviderInternalReason extends Schema.Class<ProviderInternalReason>("LLM.Error.ProviderInternal")({
|
|
128
|
-
_tag: Schema.tag("ProviderInternal"),
|
|
129
|
-
message: Schema.String,
|
|
130
|
-
status: Schema.Number,
|
|
131
|
-
retryAfterMs: Schema.optional(Schema.Number),
|
|
132
|
-
providerMetadata: Schema.optional(ProviderMetadata),
|
|
133
|
-
http: Schema.optional(HttpContext),
|
|
134
|
-
}) {
|
|
135
|
-
get retryable() {
|
|
136
|
-
return true
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
export class TransportReason extends Schema.Class<TransportReason>("LLM.Error.Transport")({
|
|
141
|
-
_tag: Schema.tag("Transport"),
|
|
142
|
-
message: Schema.String,
|
|
143
|
-
kind: Schema.optional(Schema.String),
|
|
144
|
-
url: Schema.optional(Schema.String),
|
|
145
|
-
http: Schema.optional(HttpContext),
|
|
146
|
-
}) {
|
|
147
|
-
get retryable() {
|
|
148
|
-
return false
|
|
149
|
-
}
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
export class InvalidProviderOutputReason extends Schema.Class<InvalidProviderOutputReason>(
|
|
153
|
-
"LLM.Error.InvalidProviderOutput",
|
|
154
|
-
)({
|
|
155
|
-
_tag: Schema.tag("InvalidProviderOutput"),
|
|
156
|
-
message: Schema.String,
|
|
157
|
-
route: Schema.optional(Schema.String),
|
|
158
|
-
raw: Schema.optional(Schema.String),
|
|
159
|
-
providerMetadata: Schema.optional(ProviderMetadata),
|
|
160
|
-
}) {
|
|
161
|
-
get retryable() {
|
|
162
|
-
return false
|
|
163
|
-
}
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
export class UnknownProviderReason extends Schema.Class<UnknownProviderReason>("LLM.Error.UnknownProvider")({
|
|
167
|
-
_tag: Schema.tag("UnknownProvider"),
|
|
168
|
-
message: Schema.String,
|
|
169
|
-
status: Schema.optional(Schema.Number),
|
|
170
|
-
providerMetadata: Schema.optional(ProviderMetadata),
|
|
171
|
-
http: Schema.optional(HttpContext),
|
|
172
|
-
}) {
|
|
173
|
-
get retryable() {
|
|
174
|
-
return false
|
|
175
|
-
}
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
export const LLMErrorReason = Schema.Union([
|
|
179
|
-
InvalidRequestReason,
|
|
180
|
-
ContextOverflowReason,
|
|
181
|
-
NoRouteReason,
|
|
182
|
-
AuthenticationReason,
|
|
183
|
-
RateLimitReason,
|
|
184
|
-
QuotaExceededReason,
|
|
185
|
-
ContentPolicyReason,
|
|
186
|
-
ProviderInternalReason,
|
|
187
|
-
TransportReason,
|
|
188
|
-
InvalidProviderOutputReason,
|
|
189
|
-
UnknownProviderReason,
|
|
190
|
-
]).pipe(Schema.toTaggedUnion("_tag"))
|
|
191
|
-
export type LLMErrorReason = Schema.Schema.Type<typeof LLMErrorReason>
|
|
192
|
-
|
|
193
|
-
export class LLMError extends Schema.TaggedErrorClass<LLMError>()("LLM.Error", {
|
|
194
|
-
module: Schema.String,
|
|
195
|
-
method: Schema.String,
|
|
196
|
-
reason: LLMErrorReason,
|
|
197
|
-
}) {
|
|
198
|
-
override readonly cause = this.reason
|
|
199
|
-
|
|
200
|
-
get retryable() {
|
|
201
|
-
return this.reason.retryable
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
get retryAfterMs() {
|
|
205
|
-
return "retryAfterMs" in this.reason ? this.reason.retryAfterMs : undefined
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
override get message() {
|
|
209
|
-
return `${this.module}.${this.method}: ${this.reason.message}`
|
|
210
|
-
}
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
/**
|
|
214
|
-
* Failure type for tool execute handlers. Handlers must map their internal
|
|
215
|
-
* errors to this shape; the runtime catches `ToolFailure`s and surfaces them
|
|
216
|
-
* as `tool-error` events plus a `tool-result` of `type: "error"` so the model
|
|
217
|
-
* can self-correct.
|
|
218
|
-
*
|
|
219
|
-
* Anything thrown or yielded by a handler that is not a `ToolFailure` is
|
|
220
|
-
* treated as a defect and fails the stream.
|
|
221
|
-
*/
|
|
222
|
-
export class ToolFailure extends Schema.TaggedErrorClass<ToolFailure>()("LLM.ToolFailure", {
|
|
223
|
-
message: Schema.String,
|
|
224
|
-
metadata: Schema.optional(Schema.Record(Schema.String, Schema.Unknown)),
|
|
225
|
-
}) {}
|
package/src/schema/events.ts
DELETED
|
@@ -1,333 +0,0 @@
|
|
|
1
|
-
// Adapted from opencode@v1.14.48 packages/llm/src/schema/events.ts
|
|
2
|
-
import { Schema } from "effect"
|
|
3
|
-
import { ContentBlockID, FinishReason, ProtocolID, ProviderMetadata, ResponseID, RouteID, ToolCallID } from "./ids"
|
|
4
|
-
import { ModelRef } from "./options"
|
|
5
|
-
import { ToolResultValue } from "./messages"
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* Token usage reported by an LLM provider.
|
|
9
|
-
*
|
|
10
|
-
* **Inclusive totals** (match AI SDK / OpenAI / LangChain convention — a
|
|
11
|
-
* reader from any of those ecosystems sees the number they expect):
|
|
12
|
-
*
|
|
13
|
-
* - `inputTokens` — total prompt tokens, *including* cached reads/writes.
|
|
14
|
-
* - `outputTokens` — total output tokens, *including* reasoning.
|
|
15
|
-
* - `totalTokens` — provider-supplied total, or `inputTokens + outputTokens`.
|
|
16
|
-
*
|
|
17
|
-
* **Non-overlapping breakdown** (every field is independently meaningful;
|
|
18
|
-
* consumers never have to subtract):
|
|
19
|
-
*
|
|
20
|
-
* - `nonCachedInputTokens` — the "fresh" portion of the prompt.
|
|
21
|
-
* - `cacheReadInputTokens` — input tokens served from cache.
|
|
22
|
-
* - `cacheWriteInputTokens` — input tokens written to cache.
|
|
23
|
-
* - `reasoningTokens` — subset of `outputTokens` spent on hidden reasoning.
|
|
24
|
-
*
|
|
25
|
-
* **Invariant**: `nonCachedInputTokens + cacheReadInputTokens +
|
|
26
|
-
* cacheWriteInputTokens = inputTokens`, and `reasoningTokens ≤ outputTokens`.
|
|
27
|
-
*/
|
|
28
|
-
export class Usage extends Schema.Class<Usage>("LLM.Usage")({
|
|
29
|
-
inputTokens: Schema.optional(Schema.Number),
|
|
30
|
-
outputTokens: Schema.optional(Schema.Number),
|
|
31
|
-
nonCachedInputTokens: Schema.optional(Schema.Number),
|
|
32
|
-
cacheReadInputTokens: Schema.optional(Schema.Number),
|
|
33
|
-
cacheWriteInputTokens: Schema.optional(Schema.Number),
|
|
34
|
-
reasoningTokens: Schema.optional(Schema.Number),
|
|
35
|
-
totalTokens: Schema.optional(Schema.Number),
|
|
36
|
-
providerMetadata: Schema.optional(ProviderMetadata),
|
|
37
|
-
}) {
|
|
38
|
-
/**
|
|
39
|
-
* Visible output tokens — `outputTokens` minus `reasoningTokens`, clamped
|
|
40
|
-
* to zero. The one place subtraction happens in this contract; the clamp
|
|
41
|
-
* means a provider reporting `reasoningTokens > outputTokens` produces a
|
|
42
|
-
* harmless zero rather than a negative that crashes downstream schemas.
|
|
43
|
-
*/
|
|
44
|
-
get visibleOutputTokens() {
|
|
45
|
-
return Math.max(0, (this.outputTokens ?? 0) - (this.reasoningTokens ?? 0))
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
export const RequestStart = Schema.Struct({
|
|
50
|
-
type: Schema.tag("request-start"),
|
|
51
|
-
id: ResponseID,
|
|
52
|
-
model: ModelRef,
|
|
53
|
-
}).annotate({ identifier: "LLM.Event.RequestStart" })
|
|
54
|
-
export type RequestStart = Schema.Schema.Type<typeof RequestStart>
|
|
55
|
-
|
|
56
|
-
export const StepStart = Schema.Struct({
|
|
57
|
-
type: Schema.tag("step-start"),
|
|
58
|
-
index: Schema.Number,
|
|
59
|
-
}).annotate({ identifier: "LLM.Event.StepStart" })
|
|
60
|
-
export type StepStart = Schema.Schema.Type<typeof StepStart>
|
|
61
|
-
|
|
62
|
-
export const TextStart = Schema.Struct({
|
|
63
|
-
type: Schema.tag("text-start"),
|
|
64
|
-
id: ContentBlockID,
|
|
65
|
-
providerMetadata: Schema.optional(ProviderMetadata),
|
|
66
|
-
}).annotate({ identifier: "LLM.Event.TextStart" })
|
|
67
|
-
export type TextStart = Schema.Schema.Type<typeof TextStart>
|
|
68
|
-
|
|
69
|
-
export const TextDelta = Schema.Struct({
|
|
70
|
-
type: Schema.tag("text-delta"),
|
|
71
|
-
id: ContentBlockID,
|
|
72
|
-
text: Schema.String,
|
|
73
|
-
}).annotate({ identifier: "LLM.Event.TextDelta" })
|
|
74
|
-
export type TextDelta = Schema.Schema.Type<typeof TextDelta>
|
|
75
|
-
|
|
76
|
-
export const TextEnd = Schema.Struct({
|
|
77
|
-
type: Schema.tag("text-end"),
|
|
78
|
-
id: ContentBlockID,
|
|
79
|
-
providerMetadata: Schema.optional(ProviderMetadata),
|
|
80
|
-
}).annotate({ identifier: "LLM.Event.TextEnd" })
|
|
81
|
-
export type TextEnd = Schema.Schema.Type<typeof TextEnd>
|
|
82
|
-
|
|
83
|
-
export const ReasoningStart = Schema.Struct({
|
|
84
|
-
type: Schema.tag("reasoning-start"),
|
|
85
|
-
id: ContentBlockID,
|
|
86
|
-
providerMetadata: Schema.optional(ProviderMetadata),
|
|
87
|
-
}).annotate({ identifier: "LLM.Event.ReasoningStart" })
|
|
88
|
-
export type ReasoningStart = Schema.Schema.Type<typeof ReasoningStart>
|
|
89
|
-
|
|
90
|
-
export const ReasoningDelta = Schema.Struct({
|
|
91
|
-
type: Schema.tag("reasoning-delta"),
|
|
92
|
-
id: ContentBlockID,
|
|
93
|
-
text: Schema.String,
|
|
94
|
-
}).annotate({ identifier: "LLM.Event.ReasoningDelta" })
|
|
95
|
-
export type ReasoningDelta = Schema.Schema.Type<typeof ReasoningDelta>
|
|
96
|
-
|
|
97
|
-
export const ReasoningEnd = Schema.Struct({
|
|
98
|
-
type: Schema.tag("reasoning-end"),
|
|
99
|
-
id: ContentBlockID,
|
|
100
|
-
providerMetadata: Schema.optional(ProviderMetadata),
|
|
101
|
-
}).annotate({ identifier: "LLM.Event.ReasoningEnd" })
|
|
102
|
-
export type ReasoningEnd = Schema.Schema.Type<typeof ReasoningEnd>
|
|
103
|
-
|
|
104
|
-
export const ToolInputStart = Schema.Struct({
|
|
105
|
-
type: Schema.tag("tool-input-start"),
|
|
106
|
-
id: ToolCallID,
|
|
107
|
-
name: Schema.String,
|
|
108
|
-
providerMetadata: Schema.optional(ProviderMetadata),
|
|
109
|
-
}).annotate({ identifier: "LLM.Event.ToolInputStart" })
|
|
110
|
-
export type ToolInputStart = Schema.Schema.Type<typeof ToolInputStart>
|
|
111
|
-
|
|
112
|
-
export const ToolInputDelta = Schema.Struct({
|
|
113
|
-
type: Schema.tag("tool-input-delta"),
|
|
114
|
-
id: ToolCallID,
|
|
115
|
-
name: Schema.String,
|
|
116
|
-
text: Schema.String,
|
|
117
|
-
}).annotate({ identifier: "LLM.Event.ToolInputDelta" })
|
|
118
|
-
export type ToolInputDelta = Schema.Schema.Type<typeof ToolInputDelta>
|
|
119
|
-
|
|
120
|
-
export const ToolInputEnd = Schema.Struct({
|
|
121
|
-
type: Schema.tag("tool-input-end"),
|
|
122
|
-
id: ToolCallID,
|
|
123
|
-
name: Schema.String,
|
|
124
|
-
providerMetadata: Schema.optional(ProviderMetadata),
|
|
125
|
-
}).annotate({ identifier: "LLM.Event.ToolInputEnd" })
|
|
126
|
-
export type ToolInputEnd = Schema.Schema.Type<typeof ToolInputEnd>
|
|
127
|
-
|
|
128
|
-
export const ToolCall = Schema.Struct({
|
|
129
|
-
type: Schema.tag("tool-call"),
|
|
130
|
-
id: ToolCallID,
|
|
131
|
-
name: Schema.String,
|
|
132
|
-
input: Schema.Unknown,
|
|
133
|
-
providerExecuted: Schema.optional(Schema.Boolean),
|
|
134
|
-
providerMetadata: Schema.optional(ProviderMetadata),
|
|
135
|
-
}).annotate({ identifier: "LLM.Event.ToolCall" })
|
|
136
|
-
export type ToolCall = Schema.Schema.Type<typeof ToolCall>
|
|
137
|
-
|
|
138
|
-
export const ToolResult = Schema.Struct({
|
|
139
|
-
type: Schema.tag("tool-result"),
|
|
140
|
-
id: ToolCallID,
|
|
141
|
-
name: Schema.String,
|
|
142
|
-
result: ToolResultValue,
|
|
143
|
-
providerExecuted: Schema.optional(Schema.Boolean),
|
|
144
|
-
providerMetadata: Schema.optional(ProviderMetadata),
|
|
145
|
-
}).annotate({ identifier: "LLM.Event.ToolResult" })
|
|
146
|
-
export type ToolResult = Schema.Schema.Type<typeof ToolResult>
|
|
147
|
-
|
|
148
|
-
export const ToolError = Schema.Struct({
|
|
149
|
-
type: Schema.tag("tool-error"),
|
|
150
|
-
id: ToolCallID,
|
|
151
|
-
name: Schema.String,
|
|
152
|
-
message: Schema.String,
|
|
153
|
-
providerMetadata: Schema.optional(ProviderMetadata),
|
|
154
|
-
}).annotate({ identifier: "LLM.Event.ToolError" })
|
|
155
|
-
export type ToolError = Schema.Schema.Type<typeof ToolError>
|
|
156
|
-
|
|
157
|
-
export const StepFinish = Schema.Struct({
|
|
158
|
-
type: Schema.tag("step-finish"),
|
|
159
|
-
index: Schema.Number,
|
|
160
|
-
reason: FinishReason,
|
|
161
|
-
usage: Schema.optional(Usage),
|
|
162
|
-
providerMetadata: Schema.optional(ProviderMetadata),
|
|
163
|
-
}).annotate({ identifier: "LLM.Event.StepFinish" })
|
|
164
|
-
export type StepFinish = Schema.Schema.Type<typeof StepFinish>
|
|
165
|
-
|
|
166
|
-
export const RequestFinish = Schema.Struct({
|
|
167
|
-
type: Schema.tag("request-finish"),
|
|
168
|
-
reason: FinishReason,
|
|
169
|
-
usage: Schema.optional(Usage),
|
|
170
|
-
providerMetadata: Schema.optional(ProviderMetadata),
|
|
171
|
-
}).annotate({ identifier: "LLM.Event.RequestFinish" })
|
|
172
|
-
export type RequestFinish = Schema.Schema.Type<typeof RequestFinish>
|
|
173
|
-
|
|
174
|
-
export const ProviderErrorEvent = Schema.Struct({
|
|
175
|
-
type: Schema.tag("provider-error"),
|
|
176
|
-
message: Schema.String,
|
|
177
|
-
retryable: Schema.optional(Schema.Boolean),
|
|
178
|
-
providerMetadata: Schema.optional(ProviderMetadata),
|
|
179
|
-
}).annotate({ identifier: "LLM.Event.ProviderError" })
|
|
180
|
-
export type ProviderErrorEvent = Schema.Schema.Type<typeof ProviderErrorEvent>
|
|
181
|
-
|
|
182
|
-
const llmEventTagged = Schema.Union([
|
|
183
|
-
RequestStart,
|
|
184
|
-
StepStart,
|
|
185
|
-
TextStart,
|
|
186
|
-
TextDelta,
|
|
187
|
-
TextEnd,
|
|
188
|
-
ReasoningStart,
|
|
189
|
-
ReasoningDelta,
|
|
190
|
-
ReasoningEnd,
|
|
191
|
-
ToolInputStart,
|
|
192
|
-
ToolInputDelta,
|
|
193
|
-
ToolInputEnd,
|
|
194
|
-
ToolCall,
|
|
195
|
-
ToolResult,
|
|
196
|
-
ToolError,
|
|
197
|
-
StepFinish,
|
|
198
|
-
RequestFinish,
|
|
199
|
-
ProviderErrorEvent,
|
|
200
|
-
]).pipe(Schema.toTaggedUnion("type"))
|
|
201
|
-
|
|
202
|
-
type WithID<Event extends { readonly id: unknown }, ID> = Omit<Event, "type" | "id"> & { readonly id: ID | string }
|
|
203
|
-
|
|
204
|
-
const responseID = (value: ResponseID | string) => ResponseID.make(value)
|
|
205
|
-
const contentBlockID = (value: ContentBlockID | string) => ContentBlockID.make(value)
|
|
206
|
-
const toolCallID = (value: ToolCallID | string) => ToolCallID.make(value)
|
|
207
|
-
|
|
208
|
-
/**
|
|
209
|
-
* camelCase aliases for `LLMEvent.guards` (provided by `Schema.toTaggedUnion`).
|
|
210
|
-
* Lets consumers write `events.filter(LLMEvent.is.toolCall)` instead of
|
|
211
|
-
* `events.filter(LLMEvent.guards["tool-call"])`.
|
|
212
|
-
*/
|
|
213
|
-
export const LLMEvent = Object.assign(llmEventTagged, {
|
|
214
|
-
requestStart: (input: WithID<RequestStart, ResponseID>) => RequestStart.make({ ...input, id: responseID(input.id) }),
|
|
215
|
-
stepStart: StepStart.make,
|
|
216
|
-
textStart: (input: WithID<TextStart, ContentBlockID>) => TextStart.make({ ...input, id: contentBlockID(input.id) }),
|
|
217
|
-
textDelta: (input: WithID<TextDelta, ContentBlockID>) => TextDelta.make({ ...input, id: contentBlockID(input.id) }),
|
|
218
|
-
textEnd: (input: WithID<TextEnd, ContentBlockID>) => TextEnd.make({ ...input, id: contentBlockID(input.id) }),
|
|
219
|
-
reasoningStart: (input: WithID<ReasoningStart, ContentBlockID>) =>
|
|
220
|
-
ReasoningStart.make({ ...input, id: contentBlockID(input.id) }),
|
|
221
|
-
reasoningDelta: (input: WithID<ReasoningDelta, ContentBlockID>) =>
|
|
222
|
-
ReasoningDelta.make({ ...input, id: contentBlockID(input.id) }),
|
|
223
|
-
reasoningEnd: (input: WithID<ReasoningEnd, ContentBlockID>) =>
|
|
224
|
-
ReasoningEnd.make({ ...input, id: contentBlockID(input.id) }),
|
|
225
|
-
toolInputStart: (input: WithID<ToolInputStart, ToolCallID>) =>
|
|
226
|
-
ToolInputStart.make({ ...input, id: toolCallID(input.id) }),
|
|
227
|
-
toolInputDelta: (input: WithID<ToolInputDelta, ToolCallID>) =>
|
|
228
|
-
ToolInputDelta.make({ ...input, id: toolCallID(input.id) }),
|
|
229
|
-
toolInputEnd: (input: WithID<ToolInputEnd, ToolCallID>) => ToolInputEnd.make({ ...input, id: toolCallID(input.id) }),
|
|
230
|
-
toolCall: (input: WithID<ToolCall, ToolCallID>) => ToolCall.make({ ...input, id: toolCallID(input.id) }),
|
|
231
|
-
toolResult: (input: WithID<ToolResult, ToolCallID>) => ToolResult.make({ ...input, id: toolCallID(input.id) }),
|
|
232
|
-
toolError: (input: WithID<ToolError, ToolCallID>) => ToolError.make({ ...input, id: toolCallID(input.id) }),
|
|
233
|
-
stepFinish: StepFinish.make,
|
|
234
|
-
requestFinish: RequestFinish.make,
|
|
235
|
-
providerError: ProviderErrorEvent.make,
|
|
236
|
-
is: {
|
|
237
|
-
requestStart: llmEventTagged.guards["request-start"],
|
|
238
|
-
stepStart: llmEventTagged.guards["step-start"],
|
|
239
|
-
textStart: llmEventTagged.guards["text-start"],
|
|
240
|
-
textDelta: llmEventTagged.guards["text-delta"],
|
|
241
|
-
textEnd: llmEventTagged.guards["text-end"],
|
|
242
|
-
reasoningStart: llmEventTagged.guards["reasoning-start"],
|
|
243
|
-
reasoningDelta: llmEventTagged.guards["reasoning-delta"],
|
|
244
|
-
reasoningEnd: llmEventTagged.guards["reasoning-end"],
|
|
245
|
-
toolInputStart: llmEventTagged.guards["tool-input-start"],
|
|
246
|
-
toolInputDelta: llmEventTagged.guards["tool-input-delta"],
|
|
247
|
-
toolInputEnd: llmEventTagged.guards["tool-input-end"],
|
|
248
|
-
toolCall: llmEventTagged.guards["tool-call"],
|
|
249
|
-
toolResult: llmEventTagged.guards["tool-result"],
|
|
250
|
-
toolError: llmEventTagged.guards["tool-error"],
|
|
251
|
-
stepFinish: llmEventTagged.guards["step-finish"],
|
|
252
|
-
requestFinish: llmEventTagged.guards["request-finish"],
|
|
253
|
-
providerError: llmEventTagged.guards["provider-error"],
|
|
254
|
-
},
|
|
255
|
-
})
|
|
256
|
-
export type LLMEvent = Schema.Schema.Type<typeof llmEventTagged>
|
|
257
|
-
|
|
258
|
-
export class PreparedRequest extends Schema.Class<PreparedRequest>("LLM.PreparedRequest")({
|
|
259
|
-
id: Schema.String,
|
|
260
|
-
route: RouteID,
|
|
261
|
-
protocol: ProtocolID,
|
|
262
|
-
model: ModelRef,
|
|
263
|
-
body: Schema.Unknown,
|
|
264
|
-
metadata: Schema.optional(Schema.Record(Schema.String, Schema.Unknown)),
|
|
265
|
-
}) {}
|
|
266
|
-
|
|
267
|
-
/**
|
|
268
|
-
* A `PreparedRequest` whose `body` is typed as `Body`. Use with the generic
|
|
269
|
-
* on `LLMClient.prepare<Body>(...)` when the caller knows which route their
|
|
270
|
-
* request will resolve to and wants its native shape statically exposed
|
|
271
|
-
* (debug UIs, request previews, plan rendering).
|
|
272
|
-
*
|
|
273
|
-
* The runtime body is identical — the route still emits `body: unknown` — so
|
|
274
|
-
* this is a type-level assertion the caller makes about what they expect to
|
|
275
|
-
* find. The prepare runtime does not validate the assertion.
|
|
276
|
-
*/
|
|
277
|
-
export type PreparedRequestOf<Body> = Omit<PreparedRequest, "body"> & {
|
|
278
|
-
readonly body: Body
|
|
279
|
-
}
|
|
280
|
-
|
|
281
|
-
const responseText = (events: ReadonlyArray<LLMEvent>) =>
|
|
282
|
-
events
|
|
283
|
-
.filter(LLMEvent.is.textDelta)
|
|
284
|
-
.map((event) => event.text)
|
|
285
|
-
.join("")
|
|
286
|
-
|
|
287
|
-
const responseReasoning = (events: ReadonlyArray<LLMEvent>) =>
|
|
288
|
-
events
|
|
289
|
-
.filter(LLMEvent.is.reasoningDelta)
|
|
290
|
-
.map((event) => event.text)
|
|
291
|
-
.join("")
|
|
292
|
-
|
|
293
|
-
const responseUsage = (events: ReadonlyArray<LLMEvent>) =>
|
|
294
|
-
events.reduce<Usage | undefined>(
|
|
295
|
-
(usage, event) => ("usage" in event && event.usage !== undefined ? event.usage : usage),
|
|
296
|
-
undefined,
|
|
297
|
-
)
|
|
298
|
-
|
|
299
|
-
export class LLMResponse extends Schema.Class<LLMResponse>("LLM.Response")({
|
|
300
|
-
events: Schema.Array(LLMEvent),
|
|
301
|
-
usage: Schema.optional(Usage),
|
|
302
|
-
}) {
|
|
303
|
-
/** Concatenated assistant text assembled from streamed `text-delta` events. */
|
|
304
|
-
get text() {
|
|
305
|
-
return responseText(this.events)
|
|
306
|
-
}
|
|
307
|
-
|
|
308
|
-
/** Concatenated reasoning text assembled from streamed `reasoning-delta` events. */
|
|
309
|
-
get reasoning() {
|
|
310
|
-
return responseReasoning(this.events)
|
|
311
|
-
}
|
|
312
|
-
|
|
313
|
-
/** Completed tool calls emitted by the provider. */
|
|
314
|
-
get toolCalls() {
|
|
315
|
-
return this.events.filter(LLMEvent.is.toolCall)
|
|
316
|
-
}
|
|
317
|
-
}
|
|
318
|
-
|
|
319
|
-
export namespace LLMResponse {
|
|
320
|
-
export type Output = LLMResponse | { readonly events: ReadonlyArray<LLMEvent>; readonly usage?: Usage }
|
|
321
|
-
|
|
322
|
-
/** Concatenate assistant text from a response or collected event list. */
|
|
323
|
-
export const text = (response: Output) => responseText(response.events)
|
|
324
|
-
|
|
325
|
-
/** Return response usage, falling back to the latest usage-bearing event. */
|
|
326
|
-
export const usage = (response: Output) => response.usage ?? responseUsage(response.events)
|
|
327
|
-
|
|
328
|
-
/** Return completed tool calls from a response or collected event list. */
|
|
329
|
-
export const toolCalls = (response: Output) => response.events.filter(LLMEvent.is.toolCall)
|
|
330
|
-
|
|
331
|
-
/** Concatenate reasoning text from a response or collected event list. */
|
|
332
|
-
export const reasoning = (response: Output) => responseReasoning(response.events)
|
|
333
|
-
}
|
package/src/schema/ids.ts
DELETED
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
// Adapted from opencode@v1.14.48 packages/llm/src/schema/ids.ts
|
|
2
|
-
import { Schema } from "effect"
|
|
3
|
-
|
|
4
|
-
/** Stable string identifier for a protocol implementation. */
|
|
5
|
-
export const ProtocolID = Schema.String
|
|
6
|
-
export type ProtocolID = Schema.Schema.Type<typeof ProtocolID>
|
|
7
|
-
|
|
8
|
-
/** Stable string identifier for the runnable route. */
|
|
9
|
-
export const RouteID = Schema.String
|
|
10
|
-
export type RouteID = Schema.Schema.Type<typeof RouteID>
|
|
11
|
-
|
|
12
|
-
export const ModelID = Schema.String.pipe(Schema.brand("LLM.ModelID"))
|
|
13
|
-
export type ModelID = typeof ModelID.Type
|
|
14
|
-
|
|
15
|
-
export const ProviderID = Schema.String.pipe(Schema.brand("LLM.ProviderID"))
|
|
16
|
-
export type ProviderID = typeof ProviderID.Type
|
|
17
|
-
|
|
18
|
-
export const ResponseID = Schema.String
|
|
19
|
-
export type ResponseID = Schema.Schema.Type<typeof ResponseID>
|
|
20
|
-
|
|
21
|
-
export const ContentBlockID = Schema.String
|
|
22
|
-
export type ContentBlockID = Schema.Schema.Type<typeof ContentBlockID>
|
|
23
|
-
|
|
24
|
-
export const ToolCallID = Schema.String
|
|
25
|
-
export type ToolCallID = Schema.Schema.Type<typeof ToolCallID>
|
|
26
|
-
|
|
27
|
-
export const ReasoningEfforts = ["none", "minimal", "low", "medium", "high", "xhigh", "max"] as const
|
|
28
|
-
export const ReasoningEffort = Schema.Literals(ReasoningEfforts)
|
|
29
|
-
export type ReasoningEffort = Schema.Schema.Type<typeof ReasoningEffort>
|
|
30
|
-
|
|
31
|
-
export const TextVerbosity = Schema.Literals(["low", "medium", "high"])
|
|
32
|
-
export type TextVerbosity = Schema.Schema.Type<typeof TextVerbosity>
|
|
33
|
-
|
|
34
|
-
export const MessageRole = Schema.Literals(["user", "assistant", "tool"])
|
|
35
|
-
export type MessageRole = Schema.Schema.Type<typeof MessageRole>
|
|
36
|
-
|
|
37
|
-
export const FinishReason = Schema.Literals(["stop", "length", "tool-calls", "content-filter", "error", "unknown"])
|
|
38
|
-
export type FinishReason = Schema.Schema.Type<typeof FinishReason>
|
|
39
|
-
|
|
40
|
-
export const JsonSchema = Schema.Record(Schema.String, Schema.Unknown)
|
|
41
|
-
export type JsonSchema = Schema.Schema.Type<typeof JsonSchema>
|
|
42
|
-
|
|
43
|
-
export const ProviderMetadata = Schema.Record(Schema.String, Schema.Record(Schema.String, Schema.Unknown))
|
|
44
|
-
export type ProviderMetadata = Schema.Schema.Type<typeof ProviderMetadata>
|