@tinyweb_dev/agent-framework-llm 0.2.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 -464
- 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 -335
- package/src/schema/ids.ts +0 -44
- package/src/schema/index.ts +0 -6
- package/src/schema/messages.ts +0 -250
- package/src/schema/options.ts +0 -231
- package/src/tool-runtime.ts +0 -301
- package/src/tool.ts +0 -212
package/src/tool-runtime.ts
DELETED
|
@@ -1,301 +0,0 @@
|
|
|
1
|
-
// Adapted from opencode@v1.14.48 packages/llm/src/tool-runtime.ts
|
|
2
|
-
import { Effect, Stream } from "effect"
|
|
3
|
-
import type { Concurrency } from "effect/Types"
|
|
4
|
-
import {
|
|
5
|
-
type ContentPart,
|
|
6
|
-
type FinishReason,
|
|
7
|
-
type LLMError,
|
|
8
|
-
LLMEvent,
|
|
9
|
-
LLMRequest,
|
|
10
|
-
Message,
|
|
11
|
-
type ProviderMetadata,
|
|
12
|
-
ToolCallPart,
|
|
13
|
-
ToolFailure,
|
|
14
|
-
ToolResultPart,
|
|
15
|
-
type ToolResultValue,
|
|
16
|
-
} from "./schema"
|
|
17
|
-
import { type AnyTool, type ExecutableTools, type Tools, toDefinitions } from "./tool"
|
|
18
|
-
|
|
19
|
-
export interface RuntimeState {
|
|
20
|
-
readonly step: number
|
|
21
|
-
readonly request: LLMRequest
|
|
22
|
-
/** Tool calls emitted by the model in the step that just finished (before dispatch follow-up). */
|
|
23
|
-
readonly toolCalls: ReadonlyArray<ToolCallPart>
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
export type StopCondition = (state: RuntimeState) => boolean
|
|
27
|
-
|
|
28
|
-
/** Stop the loop once the model emits a tool call with the given name. */
|
|
29
|
-
export const hasToolCall =
|
|
30
|
-
(name: string): StopCondition =>
|
|
31
|
-
(state) =>
|
|
32
|
-
state.toolCalls.some((call) => call.name === name)
|
|
33
|
-
|
|
34
|
-
/** Combine multiple stop conditions with OR semantics. */
|
|
35
|
-
export const stopWhenAny =
|
|
36
|
-
(...conditions: ReadonlyArray<StopCondition>): StopCondition =>
|
|
37
|
-
(state) =>
|
|
38
|
-
conditions.some((condition) => condition(state))
|
|
39
|
-
|
|
40
|
-
export type ToolExecution = "auto" | "none"
|
|
41
|
-
|
|
42
|
-
interface RunOptionsBase {
|
|
43
|
-
readonly request: LLMRequest
|
|
44
|
-
readonly concurrency?: Concurrency
|
|
45
|
-
readonly stopWhen?: StopCondition
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
export type RunOptions<T extends Tools> = RunOptionsAuto<T & ExecutableTools> | RunOptionsNone<T>
|
|
49
|
-
|
|
50
|
-
export interface RunOptionsAuto<T extends ExecutableTools> extends RunOptionsBase {
|
|
51
|
-
readonly request: LLMRequest
|
|
52
|
-
readonly tools: T
|
|
53
|
-
readonly toolExecution?: "auto"
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
export interface RunOptionsNone<T extends Tools> extends RunOptionsBase {
|
|
57
|
-
readonly request: LLMRequest
|
|
58
|
-
readonly tools: T
|
|
59
|
-
/** Advertise tool schemas but leave model-emitted tool calls for the caller. */
|
|
60
|
-
readonly toolExecution: "none"
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
export type StreamOptions<T extends Tools> = RunOptions<T> & {
|
|
64
|
-
readonly stream: (request: LLMRequest) => Stream.Stream<LLMEvent, LLMError>
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
export const stepCountIs =
|
|
68
|
-
(count: number): StopCondition =>
|
|
69
|
-
(state) =>
|
|
70
|
-
state.step + 1 >= count
|
|
71
|
-
|
|
72
|
-
/**
|
|
73
|
-
* Run a model with typed tools. This helper owns tool orchestration, while the
|
|
74
|
-
* caller supplies the actual model stream function. It can advertise schemas
|
|
75
|
-
* only (`toolExecution: "none"`), execute one step, or continue model rounds
|
|
76
|
-
* when `stopWhen` is provided.
|
|
77
|
-
*/
|
|
78
|
-
export const stream = <T extends Tools>(options: StreamOptions<T>): Stream.Stream<LLMEvent, LLMError> => {
|
|
79
|
-
const concurrency = options.concurrency ?? 10
|
|
80
|
-
const tools = options.tools as Tools
|
|
81
|
-
const runtimeTools = toDefinitions(tools)
|
|
82
|
-
const runtimeToolNames = new Set(runtimeTools.map((tool) => tool.name))
|
|
83
|
-
const initialRequest =
|
|
84
|
-
runtimeTools.length === 0
|
|
85
|
-
? options.request
|
|
86
|
-
: LLMRequest.update(options.request, {
|
|
87
|
-
tools: [...options.request.tools.filter((tool) => !runtimeToolNames.has(tool.name)), ...runtimeTools],
|
|
88
|
-
})
|
|
89
|
-
|
|
90
|
-
const loop = (request: LLMRequest, step: number): Stream.Stream<LLMEvent, LLMError> =>
|
|
91
|
-
Stream.unwrap(
|
|
92
|
-
Effect.gen(function* () {
|
|
93
|
-
const state: StepState = { assistantContent: [], toolCalls: [], finishReason: undefined }
|
|
94
|
-
|
|
95
|
-
const modelStream = options
|
|
96
|
-
.stream(request)
|
|
97
|
-
.pipe(Stream.tap((event) => Effect.sync(() => accumulate(state, event))))
|
|
98
|
-
|
|
99
|
-
const continuation = Stream.unwrap(
|
|
100
|
-
Effect.gen(function* () {
|
|
101
|
-
if (state.finishReason !== "tool-calls" || state.toolCalls.length === 0) return Stream.empty
|
|
102
|
-
if (options.toolExecution === "none") return Stream.empty
|
|
103
|
-
|
|
104
|
-
const dispatched = yield* Effect.forEach(
|
|
105
|
-
state.toolCalls,
|
|
106
|
-
(call) => dispatch(tools, call).pipe(Effect.map((result) => [call, result] as const)),
|
|
107
|
-
{ concurrency },
|
|
108
|
-
)
|
|
109
|
-
const resultStream = Stream.fromIterable(dispatched.flatMap(([call, result]) => emitEvents(call, result)))
|
|
110
|
-
|
|
111
|
-
// Tool-interrupt (permission ask/interrupt mode): stop the agentic
|
|
112
|
-
// loop so the host can collect user approval and later resume.
|
|
113
|
-
// Do NOT feed interrupt errors back to the model as a follow-up turn.
|
|
114
|
-
if (dispatched.some(([, result]) => isInterruptResult(result))) return resultStream
|
|
115
|
-
|
|
116
|
-
if (!options.stopWhen) return resultStream
|
|
117
|
-
if (options.stopWhen({ step, request, toolCalls: state.toolCalls })) return resultStream
|
|
118
|
-
|
|
119
|
-
// Only completed (non-interrupt) results participate in the follow-up.
|
|
120
|
-
const followUp = dispatched.filter(([, result]) => !isInterruptResult(result))
|
|
121
|
-
if (followUp.length === 0) return resultStream
|
|
122
|
-
|
|
123
|
-
return resultStream.pipe(Stream.concat(loop(followUpRequest(request, state, followUp), step + 1)))
|
|
124
|
-
}),
|
|
125
|
-
)
|
|
126
|
-
|
|
127
|
-
return modelStream.pipe(Stream.concat(continuation))
|
|
128
|
-
}),
|
|
129
|
-
)
|
|
130
|
-
|
|
131
|
-
return loop(initialRequest, 0)
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
interface StepState {
|
|
135
|
-
assistantContent: ContentPart[]
|
|
136
|
-
toolCalls: ToolCallPart[]
|
|
137
|
-
finishReason: FinishReason | undefined
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
const accumulate = (state: StepState, event: LLMEvent) => {
|
|
141
|
-
if (event.type === "text-delta") {
|
|
142
|
-
appendStreamingText(state, "text", event.text, undefined)
|
|
143
|
-
return
|
|
144
|
-
}
|
|
145
|
-
if (event.type === "reasoning-delta") {
|
|
146
|
-
appendStreamingText(state, "reasoning", event.text, undefined)
|
|
147
|
-
return
|
|
148
|
-
}
|
|
149
|
-
if (event.type === "reasoning-end") {
|
|
150
|
-
appendStreamingText(state, "reasoning", "", event.providerMetadata)
|
|
151
|
-
return
|
|
152
|
-
}
|
|
153
|
-
if (event.type === "text-end") {
|
|
154
|
-
appendStreamingText(state, "text", "", event.providerMetadata)
|
|
155
|
-
return
|
|
156
|
-
}
|
|
157
|
-
if (event.type === "tool-call") {
|
|
158
|
-
const part = ToolCallPart.make({
|
|
159
|
-
id: event.id,
|
|
160
|
-
name: event.name,
|
|
161
|
-
input: event.input,
|
|
162
|
-
providerExecuted: event.providerExecuted,
|
|
163
|
-
providerMetadata: event.providerMetadata,
|
|
164
|
-
})
|
|
165
|
-
state.assistantContent.push(part)
|
|
166
|
-
if (!event.providerExecuted) state.toolCalls.push(part)
|
|
167
|
-
return
|
|
168
|
-
}
|
|
169
|
-
if (event.type === "tool-result" && event.providerExecuted) {
|
|
170
|
-
state.assistantContent.push(
|
|
171
|
-
ToolResultPart.make({
|
|
172
|
-
id: event.id,
|
|
173
|
-
name: event.name,
|
|
174
|
-
result: event.result,
|
|
175
|
-
providerExecuted: true,
|
|
176
|
-
providerMetadata: event.providerMetadata,
|
|
177
|
-
}),
|
|
178
|
-
)
|
|
179
|
-
return
|
|
180
|
-
}
|
|
181
|
-
if (event.type === "request-finish") {
|
|
182
|
-
state.finishReason = event.reason
|
|
183
|
-
}
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
const sameProviderMetadata = (left: ProviderMetadata | undefined, right: ProviderMetadata | undefined) =>
|
|
187
|
-
left === right || JSON.stringify(left) === JSON.stringify(right)
|
|
188
|
-
|
|
189
|
-
const mergeProviderMetadata = (left: ProviderMetadata | undefined, right: ProviderMetadata | undefined) => {
|
|
190
|
-
if (!left) return right
|
|
191
|
-
if (!right) return left
|
|
192
|
-
return Object.fromEntries(
|
|
193
|
-
Array.from(new Set([...Object.keys(left), ...Object.keys(right)])).map((provider) => [
|
|
194
|
-
provider,
|
|
195
|
-
{ ...left[provider], ...right[provider] },
|
|
196
|
-
]),
|
|
197
|
-
)
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
const appendStreamingText = (
|
|
201
|
-
state: StepState,
|
|
202
|
-
type: "text" | "reasoning",
|
|
203
|
-
text: string,
|
|
204
|
-
providerMetadata: ProviderMetadata | undefined,
|
|
205
|
-
) => {
|
|
206
|
-
const last = state.assistantContent.at(-1)
|
|
207
|
-
if (last?.type === type && text.length === 0) {
|
|
208
|
-
state.assistantContent[state.assistantContent.length - 1] = {
|
|
209
|
-
...last,
|
|
210
|
-
providerMetadata: mergeProviderMetadata(last.providerMetadata, providerMetadata),
|
|
211
|
-
}
|
|
212
|
-
return
|
|
213
|
-
}
|
|
214
|
-
if (last?.type === type && sameProviderMetadata(last.providerMetadata, providerMetadata)) {
|
|
215
|
-
state.assistantContent[state.assistantContent.length - 1] = { ...last, text: `${last.text}${text}` }
|
|
216
|
-
return
|
|
217
|
-
}
|
|
218
|
-
state.assistantContent.push({ type, text, providerMetadata })
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
const isInterruptResult = (result: ToolResultValue): boolean =>
|
|
222
|
-
result.type === "error" &&
|
|
223
|
-
typeof result.metadata === "object" &&
|
|
224
|
-
result.metadata !== null &&
|
|
225
|
-
(result.metadata as { kind?: unknown }).kind === "tool-interrupt"
|
|
226
|
-
|
|
227
|
-
const dispatch = (tools: Tools, call: ToolCallPart): Effect.Effect<ToolResultValue> => {
|
|
228
|
-
const tool = tools[call.name]
|
|
229
|
-
if (!tool) return Effect.succeed({ type: "error" as const, value: `Unknown tool: ${call.name}` })
|
|
230
|
-
if (!tool.execute)
|
|
231
|
-
return Effect.succeed({ type: "error" as const, value: `Tool has no execute handler: ${call.name}` })
|
|
232
|
-
|
|
233
|
-
return decodeAndExecute(tool, call.input).pipe(
|
|
234
|
-
Effect.catchTag("LLM.ToolFailure", (failure) =>
|
|
235
|
-
Effect.succeed({
|
|
236
|
-
type: "error" as const,
|
|
237
|
-
value: failure.message,
|
|
238
|
-
...(failure.metadata ? { metadata: failure.metadata as Record<string, unknown> } : {}),
|
|
239
|
-
} satisfies ToolResultValue),
|
|
240
|
-
),
|
|
241
|
-
)
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
const decodeAndExecute = (tool: AnyTool, input: unknown): Effect.Effect<ToolResultValue, ToolFailure> =>
|
|
245
|
-
tool._decode(input).pipe(
|
|
246
|
-
Effect.mapError((error) => new ToolFailure({ message: `Invalid tool input: ${error.message}` })),
|
|
247
|
-
Effect.flatMap((decoded) => tool.execute!(decoded)),
|
|
248
|
-
Effect.flatMap((value) =>
|
|
249
|
-
tool._encode(value).pipe(
|
|
250
|
-
Effect.mapError(
|
|
251
|
-
(error) =>
|
|
252
|
-
new ToolFailure({
|
|
253
|
-
message: `Tool returned an invalid value for its success schema: ${error.message}`,
|
|
254
|
-
}),
|
|
255
|
-
),
|
|
256
|
-
),
|
|
257
|
-
),
|
|
258
|
-
Effect.map((encoded): ToolResultValue => ({ type: "json", value: encoded })),
|
|
259
|
-
)
|
|
260
|
-
|
|
261
|
-
const emitEvents = (call: ToolCallPart, result: ToolResultValue): ReadonlyArray<LLMEvent> => {
|
|
262
|
-
if (result.type === "error") {
|
|
263
|
-
// Interrupt: emit tool-error only (no tool-result) so agent processors can
|
|
264
|
-
// map it to pending-approval without a follow-up result overwriting state.
|
|
265
|
-
if (isInterruptResult(result)) {
|
|
266
|
-
return [
|
|
267
|
-
LLMEvent.toolError({
|
|
268
|
-
id: call.id,
|
|
269
|
-
name: call.name,
|
|
270
|
-
message: String(result.value),
|
|
271
|
-
metadata: result.metadata,
|
|
272
|
-
}),
|
|
273
|
-
]
|
|
274
|
-
}
|
|
275
|
-
return [
|
|
276
|
-
LLMEvent.toolError({
|
|
277
|
-
id: call.id,
|
|
278
|
-
name: call.name,
|
|
279
|
-
message: String(result.value),
|
|
280
|
-
metadata: result.metadata,
|
|
281
|
-
}),
|
|
282
|
-
LLMEvent.toolResult({ id: call.id, name: call.name, result }),
|
|
283
|
-
]
|
|
284
|
-
}
|
|
285
|
-
return [LLMEvent.toolResult({ id: call.id, name: call.name, result })]
|
|
286
|
-
}
|
|
287
|
-
|
|
288
|
-
const followUpRequest = (
|
|
289
|
-
request: LLMRequest,
|
|
290
|
-
state: StepState,
|
|
291
|
-
dispatched: ReadonlyArray<readonly [ToolCallPart, ToolResultValue]>,
|
|
292
|
-
) =>
|
|
293
|
-
LLMRequest.update(request, {
|
|
294
|
-
messages: [
|
|
295
|
-
...request.messages,
|
|
296
|
-
Message.assistant(state.assistantContent),
|
|
297
|
-
...dispatched.map(([call, result]) => Message.tool({ id: call.id, name: call.name, result })),
|
|
298
|
-
],
|
|
299
|
-
})
|
|
300
|
-
|
|
301
|
-
export const ToolRuntime = { stream, stepCountIs } as const
|
package/src/tool.ts
DELETED
|
@@ -1,212 +0,0 @@
|
|
|
1
|
-
// Adapted from opencode@v1.14.48 packages/llm/src/tool.ts
|
|
2
|
-
import { Effect, JsonSchema, Schema } from "effect"
|
|
3
|
-
import type { ToolDefinition as ToolDefinitionClass } from "./schema"
|
|
4
|
-
import { ToolDefinition, ToolFailure } from "./schema"
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Schema constraint for tool parameters / success values: no decoding or
|
|
8
|
-
* encoding services are allowed. Tools should be self-contained — anything
|
|
9
|
-
* beyond pure data conversion belongs in the handler closure.
|
|
10
|
-
*/
|
|
11
|
-
export type ToolSchema<T> = Schema.Codec<T, any, never, never>
|
|
12
|
-
|
|
13
|
-
export type ToolExecute<Parameters extends ToolSchema<any>, Success extends ToolSchema<any>> = (
|
|
14
|
-
params: Schema.Schema.Type<Parameters>,
|
|
15
|
-
) => Effect.Effect<Schema.Schema.Type<Success>, ToolFailure>
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* A type-safe LLM tool. Each tool bundles its own description, parameter
|
|
19
|
-
* Schema and success Schema. The execute handler is optional: omit it when you
|
|
20
|
-
* only want to expose a tool schema to the model and handle tool calls outside
|
|
21
|
-
* this package.
|
|
22
|
-
*
|
|
23
|
-
* Errors must be expressed as `ToolFailure`. Unmapped errors and defects fail
|
|
24
|
-
* the stream.
|
|
25
|
-
*
|
|
26
|
-
* Internally each tool also carries memoized codecs and a precomputed
|
|
27
|
-
* `ToolDefinition` so the runtime doesn't rebuild them per invocation.
|
|
28
|
-
*/
|
|
29
|
-
export interface Tool<Parameters extends ToolSchema<any>, Success extends ToolSchema<any>> {
|
|
30
|
-
readonly description: string
|
|
31
|
-
readonly parameters: Parameters
|
|
32
|
-
readonly success: Success
|
|
33
|
-
readonly execute?: ToolExecute<Parameters, Success>
|
|
34
|
-
/** @internal */
|
|
35
|
-
readonly _decode: (input: unknown) => Effect.Effect<Schema.Schema.Type<Parameters>, Schema.SchemaError>
|
|
36
|
-
/** @internal */
|
|
37
|
-
readonly _encode: (value: Schema.Schema.Type<Success>) => Effect.Effect<unknown, Schema.SchemaError>
|
|
38
|
-
/** @internal */
|
|
39
|
-
readonly _definition: ToolDefinitionClass
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
export type AnyTool = Tool<ToolSchema<any>, ToolSchema<any>>
|
|
43
|
-
|
|
44
|
-
export type ExecutableTool<Parameters extends ToolSchema<any>, Success extends ToolSchema<any>> = Tool<
|
|
45
|
-
Parameters,
|
|
46
|
-
Success
|
|
47
|
-
> & {
|
|
48
|
-
readonly execute: ToolExecute<Parameters, Success>
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
export type AnyExecutableTool = ExecutableTool<ToolSchema<any>, ToolSchema<any>>
|
|
52
|
-
|
|
53
|
-
export type ExecutableTools = Record<string, AnyExecutableTool>
|
|
54
|
-
|
|
55
|
-
type TypedToolConfig = {
|
|
56
|
-
readonly description: string
|
|
57
|
-
readonly parameters: ToolSchema<any>
|
|
58
|
-
readonly success: ToolSchema<any>
|
|
59
|
-
readonly execute?: ToolExecute<ToolSchema<any>, ToolSchema<any>>
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
type DynamicToolConfig = {
|
|
63
|
-
readonly description: string
|
|
64
|
-
readonly jsonSchema: JsonSchema.JsonSchema
|
|
65
|
-
readonly execute?: (params: unknown) => Effect.Effect<unknown, ToolFailure>
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
/**
|
|
69
|
-
* Constructs a tool. Two input modes:
|
|
70
|
-
*
|
|
71
|
-
* 1. **Typed** — pass Effect `parameters` and `success` Schemas; inputs and
|
|
72
|
-
* outputs are statically typed and decoded/encoded automatically.
|
|
73
|
-
*
|
|
74
|
-
* ```ts
|
|
75
|
-
* Tool.make({
|
|
76
|
-
* description: "Get current weather",
|
|
77
|
-
* parameters: Schema.Struct({ city: Schema.String }),
|
|
78
|
-
* success: Schema.Struct({ temperature: Schema.Number }),
|
|
79
|
-
* execute: ({ city }) => Effect.succeed({ temperature: 22 }),
|
|
80
|
-
* })
|
|
81
|
-
* ```
|
|
82
|
-
*
|
|
83
|
-
* 2. **Dynamic** — pass raw JSON Schema as `jsonSchema`. Use this when the
|
|
84
|
-
* schema comes from an external source (MCP server, plugin manifest,
|
|
85
|
-
* dynamic config) and is not known at compile time. Inputs are typed as
|
|
86
|
-
* `unknown`; the handler is responsible for any validation it needs.
|
|
87
|
-
*
|
|
88
|
-
* ```ts
|
|
89
|
-
* Tool.make({
|
|
90
|
-
* description: "Look something up",
|
|
91
|
-
* jsonSchema: { type: "object", properties: { ... } },
|
|
92
|
-
* execute: (params) => Effect.succeed(...),
|
|
93
|
-
* })
|
|
94
|
-
* ```
|
|
95
|
-
*
|
|
96
|
-
* In both modes the produced tool flows through `toDefinitions(...)` and the
|
|
97
|
-
* runtime identically.
|
|
98
|
-
*/
|
|
99
|
-
export function make<Parameters extends ToolSchema<any>, Success extends ToolSchema<any>>(config: {
|
|
100
|
-
readonly description: string
|
|
101
|
-
readonly parameters: Parameters
|
|
102
|
-
readonly success: Success
|
|
103
|
-
readonly execute: ToolExecute<Parameters, Success>
|
|
104
|
-
}): ExecutableTool<Parameters, Success>
|
|
105
|
-
export function make<Parameters extends ToolSchema<any>, Success extends ToolSchema<any>>(config: {
|
|
106
|
-
readonly description: string
|
|
107
|
-
readonly parameters: Parameters
|
|
108
|
-
readonly success: Success
|
|
109
|
-
readonly execute?: undefined
|
|
110
|
-
}): Tool<Parameters, Success>
|
|
111
|
-
export function make(config: {
|
|
112
|
-
readonly description: string
|
|
113
|
-
readonly jsonSchema: JsonSchema.JsonSchema
|
|
114
|
-
readonly execute: (params: unknown) => Effect.Effect<unknown, ToolFailure>
|
|
115
|
-
}): AnyExecutableTool
|
|
116
|
-
export function make(config: {
|
|
117
|
-
readonly description: string
|
|
118
|
-
readonly jsonSchema: JsonSchema.JsonSchema
|
|
119
|
-
readonly execute?: undefined
|
|
120
|
-
}): AnyTool
|
|
121
|
-
export function make(config: TypedToolConfig | DynamicToolConfig): AnyTool {
|
|
122
|
-
if ("jsonSchema" in config) {
|
|
123
|
-
return {
|
|
124
|
-
description: config.description,
|
|
125
|
-
parameters: Schema.Unknown as ToolSchema<unknown>,
|
|
126
|
-
success: Schema.Unknown as ToolSchema<unknown>,
|
|
127
|
-
execute: config.execute,
|
|
128
|
-
_decode: Effect.succeed,
|
|
129
|
-
_encode: Effect.succeed,
|
|
130
|
-
_definition: new ToolDefinition({
|
|
131
|
-
name: "",
|
|
132
|
-
description: config.description,
|
|
133
|
-
inputSchema: config.jsonSchema,
|
|
134
|
-
}),
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
return {
|
|
138
|
-
description: config.description,
|
|
139
|
-
parameters: config.parameters,
|
|
140
|
-
success: config.success,
|
|
141
|
-
execute: config.execute,
|
|
142
|
-
_decode: Schema.decodeUnknownEffect(config.parameters),
|
|
143
|
-
_encode: Schema.encodeEffect(config.success),
|
|
144
|
-
_definition: new ToolDefinition({
|
|
145
|
-
name: "",
|
|
146
|
-
description: config.description,
|
|
147
|
-
inputSchema: toJsonSchema(config.parameters),
|
|
148
|
-
}),
|
|
149
|
-
}
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
export const tool = make
|
|
153
|
-
|
|
154
|
-
/**
|
|
155
|
-
* A record of named tools. The record key becomes the tool name on the wire.
|
|
156
|
-
*/
|
|
157
|
-
export type Tools = Record<string, AnyTool>
|
|
158
|
-
|
|
159
|
-
/**
|
|
160
|
-
* Convert a tools record into the `ToolDefinition[]` shape that
|
|
161
|
-
* `LLMRequest.tools` expects. The runtime calls this internally; consumers
|
|
162
|
-
* that build `LLMRequest` themselves can use it too.
|
|
163
|
-
*
|
|
164
|
-
* Tool names come from the record keys, so the per-tool cached
|
|
165
|
-
* `_definition` is rebuilt with the correct name here. The JSON Schema body
|
|
166
|
-
* is reused.
|
|
167
|
-
*/
|
|
168
|
-
export const toDefinitions = (tools: Tools): ReadonlyArray<ToolDefinitionClass> =>
|
|
169
|
-
Object.entries(tools).map(
|
|
170
|
-
([name, item]) =>
|
|
171
|
-
new ToolDefinition({
|
|
172
|
-
name,
|
|
173
|
-
description: item._definition.description,
|
|
174
|
-
inputSchema: item._definition.inputSchema,
|
|
175
|
-
}),
|
|
176
|
-
)
|
|
177
|
-
|
|
178
|
-
const toJsonSchema = (schema: Schema.Top): JsonSchema.JsonSchema => {
|
|
179
|
-
const document = Schema.toJsonSchemaDocument(schema)
|
|
180
|
-
const base =
|
|
181
|
-
Object.keys(document.definitions).length === 0
|
|
182
|
-
? document.schema
|
|
183
|
-
: { ...document.schema, $defs: document.definitions }
|
|
184
|
-
// OpenAI/Anthropic both require the top-level tool parameters schema to be
|
|
185
|
-
// `type: "object"`. Effect Schema emits `anyOf: [{object},{array}]` for
|
|
186
|
-
// empty structs, which OpenAI rejects. Normalize here.
|
|
187
|
-
return normalizeTopLevel(base)
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
const normalizeTopLevel = (s: JsonSchema.JsonSchema): JsonSchema.JsonSchema => {
|
|
191
|
-
if (!s || typeof s !== "object") {
|
|
192
|
-
return { type: "object", properties: {}, additionalProperties: false } as JsonSchema.JsonSchema
|
|
193
|
-
}
|
|
194
|
-
const obj = s as { type?: unknown; anyOf?: ReadonlyArray<JsonSchema.JsonSchema> }
|
|
195
|
-
if (obj.type === "object") return s
|
|
196
|
-
if (Array.isArray(obj.anyOf)) {
|
|
197
|
-
const objectVariant = obj.anyOf.find(
|
|
198
|
-
(v): v is JsonSchema.JsonSchema =>
|
|
199
|
-
v != null && typeof v === "object" && (v as { type?: unknown }).type === "object",
|
|
200
|
-
)
|
|
201
|
-
if (objectVariant) {
|
|
202
|
-
const { anyOf: _omit, ...rest } = obj
|
|
203
|
-
void _omit
|
|
204
|
-
return { ...rest, ...(objectVariant as object) } as JsonSchema.JsonSchema
|
|
205
|
-
}
|
|
206
|
-
}
|
|
207
|
-
return { type: "object", properties: {}, additionalProperties: false } as JsonSchema.JsonSchema
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
export { ToolFailure }
|
|
211
|
-
|
|
212
|
-
export * as Tool from "./tool"
|