Repository navigation
Expand file tree
/
Copy pathloop.ts
More file actions
214 lines (204 loc) · 8.03 KB
/
Copy pathloop.ts
File metadata and controls
214 lines (204 loc) · 8.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import { Effect, Stream } from "effect"
import {
AIError,
AssistantMessage,
InvalidProviderOutputError,
LLMResponse,
ResponseFold,
Message,
Usage,
LLMEvent,
ToolCallPart,
ToolChoice,
type Finish,
type FinishReasonDetails,
type LLMRequest,
type ToolInputError,
} from "./schema/index.js"
import type { DispatchResult, PendingCall } from "./tool-runtime.js"
/**
* Client calls to answer before the next step. A call whose input was not valid JSON carries the error the model
* gets back, so it can retry. A step is final when it calls `answerTool`, even alongside other tools, or when it hits
* the output token limit, which may have cut its calls off.
*/
export const pendingCalls = (response: LLMResponse, answerTool?: string): ReadonlyArray<PendingCall> => {
if (response.finishReason.normalized === "length") return []
if (answerTool !== undefined && response.toolCalls.some((call) => call.name === answerTool)) return []
return [
...response.toolCalls.filter((call) => call.providerExecuted !== true).map((call) => ({ call })),
...response.events.filter(LLMEvent.is.toolInputError).map((event) => ({
call: invalidCall(event),
error: `Invalid tool arguments: the input is not valid JSON. Received: ${event.raw}`,
})),
]
}
/** The call to replay for input that was not valid JSON. Providers require an object input, so it is empty. */
const invalidCall = (event: ToolInputError) =>
ToolCallPart.make({ id: event.id, name: event.name, namespace: event.namespace, input: {} })
/** Add the replayed calls for invalid input to the assistant message, so each tool result has its call. */
const withInvalidCalls = (message: AssistantMessage, calls: ReadonlyArray<ToolCallPart>) =>
calls.length === 0 ? message : new AssistantMessage({ ...message, content: [...message.content, ...calls] })
/** The step's assistant message followed by one tool message per call, in call order. */
export const stepMessages = (
response: LLMResponse,
calls: ReadonlyArray<PendingCall>,
results: ReadonlyArray<DispatchResult>,
): ReadonlyArray<Message> => [
withInvalidCalls(
response.message,
calls.flatMap((item) => (item.error === undefined ? [] : [item.call])),
),
...calls.map((item, i) =>
Message.tool({
id: item.call.id,
name: item.call.name,
namespace: item.call.namespace,
result: results[i].result,
providerMetadata: item.call.providerMetadata,
}),
),
]
/**
* The request for the step after `response`, with its messages and tool results appended. A caller's forced
* `toolChoice` applies to the first step only; with `keepForcedChoice` (the answer tool's) it stays.
*/
export const nextRequest = (
request: LLMRequest,
response: LLMResponse,
calls: ReadonlyArray<PendingCall>,
results: ReadonlyArray<DispatchResult>,
keepForcedChoice: boolean,
): LLMRequest => ({
...request,
toolChoice:
!keepForcedChoice && (request.toolChoice?.type === "required" || request.toolChoice?.type === "tool")
? new ToolChoice({ type: "auto", disableParallelToolUse: request.toolChoice.disableParallelToolUse })
: request.toolChoice,
messages: [...request.messages, ...stepMessages(response, calls, results)],
})
/** Sum usage across steps. */
export const addUsage = (total: Usage | undefined, step: Usage | undefined) => {
if (total === undefined || step === undefined) return total ?? step
const sum = (
key:
| "inputTokens"
| "outputTokens"
| "nonCachedInputTokens"
| "cacheReadInputTokens"
| "cacheWriteInputTokens"
| "reasoningTokens"
| "totalTokens"
| "cost",
) => (total[key] === undefined && step[key] === undefined ? undefined : (total[key] ?? 0) + (step[key] ?? 0))
return new Usage({
// Context size describes the latest call, not a running total.
contextTokens: step.contextTokens ?? total.contextTokens,
inputTokens: sum("inputTokens"),
outputTokens: sum("outputTokens"),
nonCachedInputTokens: sum("nonCachedInputTokens"),
cacheReadInputTokens: sum("cacheReadInputTokens"),
cacheWriteInputTokens: sum("cacheWriteInputTokens"),
reasoningTokens: sum("reasoningTokens"),
totalTokens: sum("totalTokens"),
cost: sum("cost"),
})
}
/** The result of `collect`: every step of a stream from `stream` or `loop`, folded into one value. */
export interface Collected {
/** Text of the final step. */
readonly text: string
/** One response per model call, in order. */
readonly steps: ReadonlyArray<LLMResponse>
/** Each step's assistant message followed by its tool results, ready to append to the request's messages. */
readonly messages: ReadonlyArray<Message>
/** Usage from the terminal `finish`, summed across steps for a loop. */
readonly usage?: Usage
readonly finishReason: FinishReasonDetails
}
type FinishOf<Event> = Extract<Event, { readonly type: "finish" }>
/** `Collected`, plus `object` when the stream was started with `schema`. */
export type CollectResult<Event> = "object" extends keyof FinishOf<Event>
? Collected & Pick<FinishOf<Event>, "object">
: Collected
/** Run a stream from `stream` or `loop` to the end and fold its events into the final result. */
export function collect<Event extends LLMEvent, E, R>(
events: Stream.Stream<Event, E, R>,
): Effect.Effect<CollectResult<Event>, E | AIError, R>
export function collect<E, R>(
events: Stream.Stream<LLMEvent, E, R>,
): Effect.Effect<Collected & { readonly object?: unknown }, E | AIError, R> {
return Effect.suspend(() => {
const steps: LLMResponse[] = []
const messages: Message[] = []
let step = ResponseFold.empty()
// Between a `step-finish` and the next `step-start`, tool results come from the loop running tools.
let betweenSteps = false
// The last step's assistant message, and its calls with invalid input that the loop may answer.
let assistant = -1
let invalid: ReadonlyArray<ToolCallPart> = []
let finish: Finish | undefined
const close = (reason: FinishReasonDetails, usage: Usage | undefined) => {
if (step.events.length === 0) return
const response = new LLMResponse({
message: step.message,
events: step.events,
usage: usage ?? step.usage,
finishReason: reason,
})
steps.push(response)
assistant = messages.push(response.message) - 1
invalid = step.events.filter(LLMEvent.is.toolInputError).map(invalidCall)
step = ResponseFold.empty()
}
return Stream.runForEach(events, (event) =>
Effect.sync(() => {
if (event.type === "finish") {
close(event.reason, event.usage)
finish = event
return
}
if (event.type === "tool-result" && betweenSteps) {
const call = invalid.find((item) => item.id === event.id)
const previous = messages[assistant]
if (call && previous.role === "assistant") messages[assistant] = withInvalidCalls(previous, [call])
messages.push(
Message.tool({
id: event.id,
name: event.name,
namespace: event.namespace,
result: event.result,
providerMetadata: event.providerMetadata,
}),
)
return
}
if (event.type === "step-start") betweenSteps = false
step = ResponseFold.reduce(step, event)
if (event.type !== "step-finish") return
close(event.reason, event.usage)
betweenSteps = true
}),
).pipe(
Effect.flatMap(() => {
if (finish === undefined)
return Effect.fail(
new AIError({
reason: new InvalidProviderOutputError({
message: "The stream ended without a finish event.",
classification: "incomplete-stream",
}),
}),
)
const result = {
text: steps.at(-1)?.text ?? "",
steps,
messages,
usage: finish.usage,
finishReason: finish.reason,
...("object" in finish ? { object: finish.object } : {}),
}
return Effect.succeed(result)
}),
)
})
}