effect-assistant 0.12.0 → 0.13.1
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/Assistant.d.ts +26 -5
- package/dist/Assistant.d.ts.map +1 -1
- package/dist/Assistant.js +7 -1
- package/dist/Assistant.js.map +1 -1
- package/dist/AssistantEngine.d.ts +6 -2
- package/dist/AssistantEngine.d.ts.map +1 -1
- package/dist/AssistantEngine.js +18 -10
- package/dist/AssistantEngine.js.map +1 -1
- package/dist/AssistantLoop.js +1 -1
- package/dist/AssistantTest.js +1 -1
- package/dist/BlockKit.js +1 -1
- package/dist/ChatSignal.d.ts +58 -0
- package/dist/ChatSignal.d.ts.map +1 -0
- package/dist/ChatSignal.js +73 -0
- package/dist/ChatSignal.js.map +1 -0
- package/dist/Compaction.js +1 -1
- package/dist/Memory.js +1 -1
- package/dist/StatusSummary.d.ts +70 -0
- package/dist/StatusSummary.d.ts.map +1 -0
- package/dist/StatusSummary.js +84 -0
- package/dist/StatusSummary.js.map +1 -0
- package/dist/Timeline.d.ts +10 -2
- package/dist/Timeline.d.ts.map +1 -1
- package/dist/Timeline.js +72 -18
- package/dist/Timeline.js.map +1 -1
- package/dist/ToolStatus.d.ts +73 -0
- package/dist/ToolStatus.d.ts.map +1 -0
- package/dist/ToolStatus.js +72 -0
- package/dist/ToolStatus.js.map +1 -0
- package/dist/index.d.ts +4 -1
- package/dist/index.js +4 -1
- package/dist/internal/chat.js +18 -10
- package/dist/internal/chat.js.map +1 -1
- package/dist/internal/events.d.ts +1 -1
- package/dist/internal/events.js +1 -1
- package/dist/internal/history.js +1 -1
- package/dist/internal/live.js +87 -0
- package/dist/internal/live.js.map +1 -0
- package/dist/internal/loop.d.ts +1 -1
- package/dist/internal/loop.d.ts.map +1 -1
- package/dist/internal/loop.js +42 -11
- package/dist/internal/loop.js.map +1 -1
- package/dist/internal/schema.js +1 -1
- package/dist/internal/specialists.js +3 -2
- package/dist/internal/specialists.js.map +1 -1
- package/dist/internal/stream.js +72 -0
- package/dist/internal/stream.js.map +1 -0
- package/dist/internal/summary.js +66 -0
- package/dist/internal/summary.js.map +1 -0
- package/dist/internal/telemetry.js +3 -2
- package/dist/internal/telemetry.js.map +1 -1
- package/dist/internal/toolkit.js +1 -1
- package/dist/internal/tools.js +1 -1
- package/dist/internal/turns.d.ts +3 -1
- package/dist/internal/turns.d.ts.map +1 -1
- package/dist/internal/turns.js +12 -3
- package/dist/internal/turns.js.map +1 -1
- package/dist/internal/writer.d.ts +2 -0
- package/dist/internal/writer.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/Assistant.ts +48 -13
- package/src/AssistantEngine.ts +25 -11
- package/src/ChatSignal.ts +71 -0
- package/src/StatusSummary.ts +126 -0
- package/src/Timeline.ts +82 -20
- package/src/ToolStatus.ts +91 -0
- package/src/index.ts +15 -0
- package/src/internal/chat.ts +40 -21
- package/src/internal/live.ts +133 -0
- package/src/internal/loop.ts +103 -25
- package/src/internal/specialists.ts +3 -0
- package/src/internal/stream.ts +98 -0
- package/src/internal/summary.ts +127 -0
- package/src/internal/telemetry.ts +5 -1
- package/src/internal/turns.ts +20 -1
- package/src/internal/writer.ts +3 -0
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import * as Clock from "effect/Clock"
|
|
2
|
+
import * as Context from "effect/Context"
|
|
3
|
+
import * as Effect from "effect/Effect"
|
|
4
|
+
import * as Option from "effect/Option"
|
|
5
|
+
import * as SynchronizedRef from "effect/SynchronizedRef"
|
|
6
|
+
import * as ChatSignal from "../ChatSignal.ts"
|
|
7
|
+
|
|
8
|
+
interface Labelled {
|
|
9
|
+
readonly callId: string
|
|
10
|
+
readonly text: string
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/** @internal */
|
|
14
|
+
export interface State {
|
|
15
|
+
/** The described calls still running, oldest first. */
|
|
16
|
+
readonly running: ReadonlyArray<Labelled>
|
|
17
|
+
/** The line the user sees; none before the first status and after typing started. */
|
|
18
|
+
readonly shown: string | undefined
|
|
19
|
+
/** When the line last changed, or the turn started, in epoch milliseconds. */
|
|
20
|
+
readonly shownAt: number
|
|
21
|
+
readonly typing: boolean
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** @internal */
|
|
25
|
+
export interface Live {
|
|
26
|
+
readonly callStarted: (callId: string, text: string) => Effect.Effect<void>
|
|
27
|
+
readonly callEnded: (callId: string) => Effect.Effect<void>
|
|
28
|
+
// A summary's line: shown unless the model started a message, or the line says it already.
|
|
29
|
+
readonly summarized: (text: string) => Effect.Effect<void>
|
|
30
|
+
readonly typing: (typing: boolean) => Effect.Effect<void>
|
|
31
|
+
readonly state: Effect.Effect<State>
|
|
32
|
+
// The turn ends: typing stops. Its end, in the log, clears the line.
|
|
33
|
+
readonly reset: Effect.Effect<void>
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// A turn's own: a specialist's run, or a loop outside a turn, shows nothing.
|
|
37
|
+
/** @internal */
|
|
38
|
+
export const TurnLive = Context.Reference<Option.Option<Live>>("effect-assistant/TurnLive", {
|
|
39
|
+
defaultValue: Option.none
|
|
40
|
+
})
|
|
41
|
+
|
|
42
|
+
/** @internal */
|
|
43
|
+
export const use = Effect.fnUntraced(function* (f: (live: Live) => Effect.Effect<void>) {
|
|
44
|
+
const live = yield* TurnLive
|
|
45
|
+
|
|
46
|
+
if (Option.isSome(live)) yield* f(live.value)
|
|
47
|
+
})
|
|
48
|
+
|
|
49
|
+
type Change = readonly [ReadonlyArray<ChatSignal.ChatSignal>, State] | undefined
|
|
50
|
+
|
|
51
|
+
/** @internal */
|
|
52
|
+
export const make = Effect.fnUntraced(function* (send: (signal: ChatSignal.ChatSignal) => Effect.Effect<void>) {
|
|
53
|
+
const state = yield* SynchronizedRef.make<State>({
|
|
54
|
+
running: [],
|
|
55
|
+
shown: undefined,
|
|
56
|
+
shownAt: yield* Clock.currentTimeMillis,
|
|
57
|
+
typing: false
|
|
58
|
+
})
|
|
59
|
+
|
|
60
|
+
// Under the ref's lock, so signals go out in the order the state changed.
|
|
61
|
+
const change = (f: (current: State, now: number) => Change) =>
|
|
62
|
+
SynchronizedRef.updateEffect(
|
|
63
|
+
state,
|
|
64
|
+
Effect.fnUntraced(function* (current) {
|
|
65
|
+
const changed = f(current, yield* Clock.currentTimeMillis)
|
|
66
|
+
|
|
67
|
+
if (changed === undefined) return current
|
|
68
|
+
|
|
69
|
+
const [signals, next] = changed
|
|
70
|
+
|
|
71
|
+
yield* Effect.forEach(signals, send, { discard: true })
|
|
72
|
+
|
|
73
|
+
return next
|
|
74
|
+
})
|
|
75
|
+
)
|
|
76
|
+
|
|
77
|
+
const show = (current: State, text: string, now: number): readonly [ReadonlyArray<ChatSignal.ChatSignal>, State] =>
|
|
78
|
+
current.shown === text
|
|
79
|
+
? [[], current]
|
|
80
|
+
: [[ChatSignal.Status.make({ text })], { ...current, shown: text, shownAt: now }]
|
|
81
|
+
|
|
82
|
+
const live: Live = {
|
|
83
|
+
callStarted: (callId, text) =>
|
|
84
|
+
change((current, now) => {
|
|
85
|
+
const running = { ...current, running: [...current.running, { callId, text }] }
|
|
86
|
+
|
|
87
|
+
return current.typing ? [[], running] : show(running, text, now)
|
|
88
|
+
}),
|
|
89
|
+
// The line stays when the last labelled call ends; while another runs, the line says that one.
|
|
90
|
+
callEnded: (callId) =>
|
|
91
|
+
change((current, now) => {
|
|
92
|
+
const ended = current.running.find((call) => call.callId === callId)
|
|
93
|
+
|
|
94
|
+
if (ended === undefined) return undefined
|
|
95
|
+
|
|
96
|
+
const next = { ...current, running: current.running.filter((call) => call.callId !== callId) }
|
|
97
|
+
const still = next.running.at(-1)
|
|
98
|
+
|
|
99
|
+
return still === undefined || current.typing || current.shown !== ended.text
|
|
100
|
+
? [[], next]
|
|
101
|
+
: show(next, still.text, now)
|
|
102
|
+
}),
|
|
103
|
+
summarized: (text) =>
|
|
104
|
+
change((current, now) =>
|
|
105
|
+
current.typing
|
|
106
|
+
? undefined
|
|
107
|
+
: current.shown === text
|
|
108
|
+
? [[], { ...current, shownAt: now }]
|
|
109
|
+
: show(current, text, now)
|
|
110
|
+
),
|
|
111
|
+
typing: (typing) =>
|
|
112
|
+
change((current, now) => {
|
|
113
|
+
if (current.typing === typing) return undefined
|
|
114
|
+
|
|
115
|
+
if (typing) {
|
|
116
|
+
return [[ChatSignal.TypingStarted.make({})], { ...current, typing, shown: undefined, shownAt: now }]
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
// A call that runs on after the message still says what it does.
|
|
120
|
+
const stopped = { ...current, typing, shownAt: now }
|
|
121
|
+
const still = current.running.at(-1)
|
|
122
|
+
const [signals, next] = still === undefined ? [[], stopped] : show(stopped, still.text, now)
|
|
123
|
+
|
|
124
|
+
return [[ChatSignal.TypingStopped.make({}), ...signals], next]
|
|
125
|
+
}),
|
|
126
|
+
state: SynchronizedRef.get(state),
|
|
127
|
+
reset: change((current) =>
|
|
128
|
+
current.typing ? [[ChatSignal.TypingStopped.make({})], { ...current, typing: false }] : undefined
|
|
129
|
+
)
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
return live
|
|
133
|
+
})
|
package/src/internal/loop.ts
CHANGED
|
@@ -12,13 +12,17 @@ import * as Effect from "effect/Effect"
|
|
|
12
12
|
import * as Option from "effect/Option"
|
|
13
13
|
import * as Predicate from "effect/Predicate"
|
|
14
14
|
import * as Schema from "effect/Schema"
|
|
15
|
+
import * as SchemaAST from "effect/SchemaAST"
|
|
15
16
|
import * as Stream from "effect/Stream"
|
|
16
17
|
import * as AssistantEvent from "../AssistantEvent.ts"
|
|
17
18
|
import type * as Instructions from "../Instructions.ts"
|
|
19
|
+
import * as ToolStatus from "../ToolStatus.ts"
|
|
18
20
|
import * as History from "./history.ts"
|
|
21
|
+
import * as Live from "./live.ts"
|
|
22
|
+
import * as Streamed from "./stream.ts"
|
|
19
23
|
import * as Telemetry from "./telemetry.ts"
|
|
20
24
|
import * as Toolkits from "./toolkit.ts"
|
|
21
|
-
import { isTurnEnding, isUserMessage } from "./tools.ts"
|
|
25
|
+
import { isTurnEnding, isUserMessage, sendUserMessageName } from "./tools.ts"
|
|
22
26
|
|
|
23
27
|
/** @internal */
|
|
24
28
|
export type Event =
|
|
@@ -87,41 +91,84 @@ export interface StepResult {
|
|
|
87
91
|
// every tool's params, `unknown` for a schema of unknown type, though deferred validation encodes none.
|
|
88
92
|
type RequestTool = Tool.Tool<string, any, any>
|
|
89
93
|
|
|
94
|
+
// Required in what the model reads, so it writes one with every call; a call without it still runs.
|
|
95
|
+
const statusParameter = Schema.String.annotate({ description: ToolStatus.parameterDescription })
|
|
96
|
+
|
|
97
|
+
// What the model reads of a described tool: its parameters with the status one. Only the request has it; the call
|
|
98
|
+
// runs without it (see `withoutStatus`). A record of no keys (a tool without parameters) becomes an object with it.
|
|
99
|
+
const withStatusParameter = (tool: Tool.Any): Tool.Any => {
|
|
100
|
+
const ast = tool.parametersSchema.ast
|
|
101
|
+
|
|
102
|
+
if (!ToolStatus.isDescribed(tool) || !SchemaAST.isObjects(ast)) return tool
|
|
103
|
+
|
|
104
|
+
if (ast.propertySignatures.some((property) => property.name === ToolStatus.parameterName)) {
|
|
105
|
+
throw new Error(
|
|
106
|
+
`${tool.name} has a parameter named ${ToolStatus.parameterName} already, so it can't show a status: rename it.`
|
|
107
|
+
)
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
const added = Schema.Struct({ [ToolStatus.parameterName]: statusParameter }).ast.propertySignatures
|
|
111
|
+
|
|
112
|
+
return tool.setParameters(
|
|
113
|
+
Schema.make<Schema.Top>(
|
|
114
|
+
new SchemaAST.Objects(
|
|
115
|
+
[...ast.propertySignatures, ...added],
|
|
116
|
+
ast.indexSignatures.filter((index) => !SchemaAST.isNever(index.type)),
|
|
117
|
+
ast.annotations
|
|
118
|
+
)
|
|
119
|
+
)
|
|
120
|
+
)
|
|
121
|
+
}
|
|
122
|
+
|
|
90
123
|
// The loop runs the calls itself, after storing each as started, so the model call runs none. It also decides which
|
|
91
124
|
// calls wait for approval: the model call would ask for approvals of its own.
|
|
92
125
|
const requestToolkit = (definitions: ReadonlyArray<Tool.Any>): Toolkit.WithHandler<Record<string, RequestTool>> => ({
|
|
93
126
|
tools: Object.fromEntries(
|
|
94
|
-
definitions.map((tool) =>
|
|
95
|
-
tool
|
|
96
|
-
|
|
97
|
-
|
|
127
|
+
definitions.map((tool) => {
|
|
128
|
+
const requested = withStatusParameter(tool)
|
|
129
|
+
|
|
130
|
+
return [
|
|
131
|
+
tool.name,
|
|
132
|
+
requested.needsApproval === undefined || requested.needsApproval === false
|
|
133
|
+
? requested
|
|
134
|
+
: requested.setNeedsApproval(false)
|
|
135
|
+
]
|
|
136
|
+
})
|
|
98
137
|
),
|
|
99
138
|
handle: () => Effect.succeed(Stream.empty)
|
|
100
139
|
})
|
|
101
140
|
|
|
141
|
+
// The model starts a message to the user: a `send_user_message` call, or text of its own, which the reply pass turns
|
|
142
|
+
// into one.
|
|
143
|
+
const startsMessage = (part: Response.AnyPart) =>
|
|
144
|
+
part.type === "text-start" || (part.type === "tool-params-start" && part.name === sendUserMessageName)
|
|
145
|
+
|
|
102
146
|
// Validation is deferred: a call to a tool that isn't in the toolkit (one in the history the turn no longer offers),
|
|
103
147
|
// or with params that don't fit its schema, would otherwise fail the whole model call. The loop runs it instead, and
|
|
104
148
|
// the model reads why it failed.
|
|
149
|
+
// Streamed, so the chat can show the user that a message is on its way while the model writes it.
|
|
105
150
|
const generate = (
|
|
106
151
|
prompt: Prompt.Prompt,
|
|
107
152
|
definitions: ReadonlyArray<Tool.Any>,
|
|
108
153
|
toolChoice: LanguageModel.ToolChoice<string> | undefined
|
|
109
|
-
): Effect.Effect<
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
154
|
+
): Effect.Effect<ReadonlyArray<Response.AnyPart>, AiError.AiError, LanguageModel.LanguageModel> =>
|
|
155
|
+
Stream.suspend(() =>
|
|
156
|
+
LanguageModel.streamText({
|
|
157
|
+
prompt,
|
|
158
|
+
toolkit: requestToolkit(definitions),
|
|
159
|
+
toolChoice,
|
|
160
|
+
disableToolCallResolution: true,
|
|
161
|
+
toolCallValidation: "deferred"
|
|
162
|
+
})
|
|
163
|
+
).pipe(
|
|
164
|
+
Stream.tap((part) => (startsMessage(part) ? Live.use((live) => live.typing(true)) : Effect.void)),
|
|
165
|
+
Stream.runCollect,
|
|
166
|
+
Effect.map(Streamed.collect)
|
|
167
|
+
)
|
|
121
168
|
|
|
122
169
|
const decodePart = Schema.decodeUnknownOption(AssistantEvent.ModelPart)
|
|
123
170
|
|
|
124
|
-
const modelParts = (content: ReadonlyArray<Response.
|
|
171
|
+
const modelParts = (content: ReadonlyArray<Response.AnyPart>) =>
|
|
125
172
|
content.flatMap((part) =>
|
|
126
173
|
Option.toArray(decodePart(part)).filter((decoded) => !(decoded.type === "tool-call" && decoded.providerExecuted))
|
|
127
174
|
)
|
|
@@ -129,14 +176,43 @@ const modelParts = (content: ReadonlyArray<Response.AllParts<Record<string, Tool
|
|
|
129
176
|
const toolCalls = (parts: ReadonlyArray<AssistantEvent.ModelPart>): ReadonlyArray<AssistantEvent.ToolCallPart> =>
|
|
130
177
|
parts.flatMap((part) => (part.type === "tool-call" ? [part] : []))
|
|
131
178
|
|
|
179
|
+
const decodeParams = Schema.decodeUnknownOption(Schema.Record(Schema.String, Schema.Json))
|
|
180
|
+
|
|
181
|
+
const isDescribedCall = <R>(tools: Toolkits.Tools<R>, call: AssistantEvent.ToolCallPart) =>
|
|
182
|
+
tools.definitions.some((tool) => tool.name === call.name && ToolStatus.isDescribed(tool))
|
|
183
|
+
|
|
184
|
+
// The status the model wrote for a described tool's call, and the call as the tool's handler gets it.
|
|
185
|
+
const splitStatus = (call: AssistantEvent.ToolCallPart): readonly [string | undefined, AssistantEvent.ToolCallPart] => {
|
|
186
|
+
const params = decodeParams(call.params)
|
|
187
|
+
|
|
188
|
+
if (Option.isNone(params) || !Object.hasOwn(params.value, ToolStatus.parameterName)) return [undefined, call]
|
|
189
|
+
|
|
190
|
+
const { [ToolStatus.parameterName]: written, ...rest } = params.value
|
|
191
|
+
const status = Predicate.isString(written) ? written.trim() : ""
|
|
192
|
+
|
|
193
|
+
return [status === "" ? undefined : status, { ...call, params: rest }]
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
// A call's status shows when it starts; a message's typing stops once the message is stored.
|
|
132
197
|
const runCall = Effect.fnUntraced(function* <R>(tools: Toolkits.Tools<R>, call: AssistantEvent.ToolCallPart) {
|
|
198
|
+
const [status, run] = isDescribedCall(tools, call) ? splitStatus(call) : [undefined, call]
|
|
199
|
+
|
|
133
200
|
const result = yield* Effect.gen(function* () {
|
|
134
|
-
|
|
201
|
+
if (status !== undefined) yield* Live.use((live) => live.callStarted(call.id, status))
|
|
202
|
+
|
|
203
|
+
const result = yield* tools.run(run)
|
|
135
204
|
|
|
136
205
|
yield* Telemetry.toolOutput(result.result)
|
|
137
206
|
|
|
138
207
|
return result
|
|
139
|
-
}).pipe(
|
|
208
|
+
}).pipe(
|
|
209
|
+
Telemetry.toolSpan(call),
|
|
210
|
+
Effect.ensuring(
|
|
211
|
+
Live.use((live) =>
|
|
212
|
+
Effect.andThen(live.callEnded(call.id), call.name === sendUserMessageName ? live.typing(false) : Effect.void)
|
|
213
|
+
)
|
|
214
|
+
)
|
|
215
|
+
)
|
|
140
216
|
|
|
141
217
|
return AssistantEvent.ToolResult.make({ callId: call.id, tool: call.name, ...result })
|
|
142
218
|
})
|
|
@@ -204,6 +280,7 @@ const runCalls = <R, S, E, R2>(
|
|
|
204
280
|
Effect.gen(function* () {
|
|
205
281
|
const explained = pending.some(({ call }) => isUserMessage(call))
|
|
206
282
|
|
|
283
|
+
// Text that became no message, or a message that failed, stops the typing too.
|
|
207
284
|
const done = yield* Effect.forEach(
|
|
208
285
|
pending,
|
|
209
286
|
({ call }, i): Effect.Effect<Event, never, R> =>
|
|
@@ -213,7 +290,7 @@ const runCalls = <R, S, E, R2>(
|
|
|
213
290
|
? awaitApproval(call)
|
|
214
291
|
: Effect.succeed(unexplained(call)),
|
|
215
292
|
{ concurrency: "unbounded" }
|
|
216
|
-
)
|
|
293
|
+
).pipe(Effect.ensuring(Live.use((live) => live.typing(false))))
|
|
217
294
|
|
|
218
295
|
return Stream.fromIterable(done.map(value)).pipe(
|
|
219
296
|
Stream.concat(
|
|
@@ -322,18 +399,19 @@ export const step = <R, S, E2 = never, R2 = never>(
|
|
|
322
399
|
const name = yield* Effect.serviceOption(Model.ModelName)
|
|
323
400
|
const started = yield* Clock.currentTimeMillis
|
|
324
401
|
|
|
325
|
-
const
|
|
402
|
+
const content = yield* generate(prompt, toolkit.definitions, options.toolChoice).pipe(
|
|
326
403
|
Telemetry.annotateModelCall(instructions.attributes)
|
|
327
404
|
)
|
|
328
405
|
|
|
329
|
-
const metadata =
|
|
406
|
+
const metadata = content.find((part) => part.type === "response-metadata")
|
|
407
|
+
const finish = content.find((part) => part.type === "finish")
|
|
330
408
|
|
|
331
409
|
const output = AssistantEvent.ModelOutput.make({
|
|
332
|
-
parts: modelParts(
|
|
410
|
+
parts: modelParts(content),
|
|
333
411
|
seenThroughSequence: throughSequence,
|
|
334
412
|
model: metadata?.modelId ?? Option.getOrUndefined(name),
|
|
335
413
|
responseId: metadata?.id,
|
|
336
|
-
usage:
|
|
414
|
+
usage: finish?.usage,
|
|
337
415
|
durationMillis: (yield* Clock.currentTimeMillis) - started
|
|
338
416
|
})
|
|
339
417
|
|
|
@@ -26,6 +26,7 @@ import * as RunId from "../RunId.ts"
|
|
|
26
26
|
import * as Specialist from "../Specialist.ts"
|
|
27
27
|
import * as ToolFailure from "../ToolFailure.ts"
|
|
28
28
|
import { toolMessage, userText } from "./history.ts"
|
|
29
|
+
import * as Live from "./live.ts"
|
|
29
30
|
import * as Telemetry from "./telemetry.ts"
|
|
30
31
|
import * as Toolkits from "./toolkit.ts"
|
|
31
32
|
import type { Writer } from "./writer.ts"
|
|
@@ -270,6 +271,8 @@ export const makeRunner = Effect.fnUntraced(function* (options: {
|
|
|
270
271
|
Effect.provideContext(Context.merge(options.model, services)),
|
|
271
272
|
// Spawned from a turn, a run would inherit the turn's own tools: a run can't message the user.
|
|
272
273
|
Effect.provideService(Loop.TurnTools, Option.none()),
|
|
274
|
+
// Nor show the user a status or typing: its turn may have ended.
|
|
275
|
+
Effect.provideService(Live.TurnLive, Option.none()),
|
|
273
276
|
Effect.matchCause({
|
|
274
277
|
onSuccess: (answer): Outcome => ({ status: "answered", report: answer }),
|
|
275
278
|
onFailure: unanswered
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import * as Response from "effect/ai/Response"
|
|
2
|
+
import * as Predicate from "effect/Predicate"
|
|
3
|
+
|
|
4
|
+
type Metadata = Response.ProviderMetadata
|
|
5
|
+
|
|
6
|
+
// Per provider, the later part's keys win.
|
|
7
|
+
const mergeMetadata = (left: Metadata, right: Metadata): Metadata => ({
|
|
8
|
+
...left,
|
|
9
|
+
...Object.fromEntries(
|
|
10
|
+
Object.entries(right).map(([provider, metadata]) => {
|
|
11
|
+
const previous = left[provider]
|
|
12
|
+
|
|
13
|
+
return [
|
|
14
|
+
provider,
|
|
15
|
+
Predicate.isObject(previous) && Predicate.isObject(metadata) ? Object.assign({}, previous, metadata) : metadata
|
|
16
|
+
]
|
|
17
|
+
})
|
|
18
|
+
)
|
|
19
|
+
})
|
|
20
|
+
|
|
21
|
+
interface Open {
|
|
22
|
+
readonly index: number
|
|
23
|
+
text: string
|
|
24
|
+
metadata: Metadata
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
// A streamed call's parts as the call would have returned them whole: each text and reasoning block joined into one
|
|
28
|
+
// part, with its provider metadata (a reasoning block's signature arrives on one of its deltas). Partial text is kept;
|
|
29
|
+
// reasoning that never ended is dropped, as a provider would refuse it back without its signature.
|
|
30
|
+
/** @internal */
|
|
31
|
+
export const collect = (parts: ReadonlyArray<Response.AnyPart>): ReadonlyArray<Response.AnyPart> => {
|
|
32
|
+
const collected: Array<Response.AnyPart | undefined> = []
|
|
33
|
+
const texts = new Map<string, Open>()
|
|
34
|
+
const reasonings = new Map<string, Open>()
|
|
35
|
+
|
|
36
|
+
const start = (open: Map<string, Open>, id: string, metadata: Metadata) => {
|
|
37
|
+
open.set(id, { index: collected.length, text: "", metadata })
|
|
38
|
+
collected.push(undefined)
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const delta = (open: Map<string, Open>, id: string, text: string, metadata: Metadata) => {
|
|
42
|
+
const active = open.get(id)
|
|
43
|
+
|
|
44
|
+
if (active === undefined) return
|
|
45
|
+
|
|
46
|
+
active.text += text
|
|
47
|
+
active.metadata = mergeMetadata(active.metadata, metadata)
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const end = (open: Map<string, Open>, id: string, metadata: Metadata, type: "text" | "reasoning") => {
|
|
51
|
+
const active = open.get(id)
|
|
52
|
+
|
|
53
|
+
if (active === undefined) return
|
|
54
|
+
|
|
55
|
+
open.delete(id)
|
|
56
|
+
collected[active.index] = Response.makePart(type, {
|
|
57
|
+
text: active.text,
|
|
58
|
+
metadata: mergeMetadata(active.metadata, metadata)
|
|
59
|
+
})
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
for (const part of parts) {
|
|
63
|
+
switch (part.type) {
|
|
64
|
+
case "text-start":
|
|
65
|
+
start(texts, part.id, part.metadata)
|
|
66
|
+
break
|
|
67
|
+
case "text-delta":
|
|
68
|
+
delta(texts, part.id, part.delta, part.metadata)
|
|
69
|
+
break
|
|
70
|
+
case "text-end":
|
|
71
|
+
end(texts, part.id, part.metadata, "text")
|
|
72
|
+
break
|
|
73
|
+
case "reasoning-start":
|
|
74
|
+
start(reasonings, part.id, part.metadata)
|
|
75
|
+
break
|
|
76
|
+
case "reasoning-delta":
|
|
77
|
+
delta(reasonings, part.id, part.delta, part.metadata)
|
|
78
|
+
break
|
|
79
|
+
case "reasoning-end":
|
|
80
|
+
end(reasonings, part.id, part.metadata, "reasoning")
|
|
81
|
+
break
|
|
82
|
+
case "tool-params-start":
|
|
83
|
+
case "tool-params-delta":
|
|
84
|
+
case "tool-params-end":
|
|
85
|
+
break
|
|
86
|
+
default:
|
|
87
|
+
collected.push(part)
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
for (const active of texts.values()) {
|
|
92
|
+
if (active.text.length > 0) {
|
|
93
|
+
collected[active.index] = Response.makePart("text", { text: active.text, metadata: active.metadata })
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
return collected.filter(Predicate.isNotUndefined)
|
|
98
|
+
}
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import type * as Prompt from "effect/ai/Prompt"
|
|
2
|
+
import * as Clock from "effect/Clock"
|
|
3
|
+
import * as Duration from "effect/Duration"
|
|
4
|
+
import * as Effect from "effect/Effect"
|
|
5
|
+
import * as Option from "effect/Option"
|
|
6
|
+
import * as Predicate from "effect/Predicate"
|
|
7
|
+
import * as Ref from "effect/Ref"
|
|
8
|
+
import * as Schedule from "effect/Schedule"
|
|
9
|
+
import * as AssistantEvent from "../AssistantEvent.ts"
|
|
10
|
+
import type * as StatusSummary from "../StatusSummary.ts"
|
|
11
|
+
import * as History from "./history.ts"
|
|
12
|
+
import * as Live from "./live.ts"
|
|
13
|
+
import { toJsonText } from "./schema.ts"
|
|
14
|
+
import type { Writer } from "./writer.ts"
|
|
15
|
+
|
|
16
|
+
const MAX_LINE = 300
|
|
17
|
+
|
|
18
|
+
const MAX_MESSAGES = 12
|
|
19
|
+
|
|
20
|
+
const clip = (text: string) => (text.length > MAX_LINE ? `${text.slice(0, MAX_LINE)}…` : text)
|
|
21
|
+
|
|
22
|
+
const assistantLines = (part: Prompt.AssistantMessagePart): ReadonlyArray<string> => {
|
|
23
|
+
switch (part.type) {
|
|
24
|
+
case "text":
|
|
25
|
+
return [`Assistant: ${clip(part.text)}`]
|
|
26
|
+
case "tool-call":
|
|
27
|
+
return [`Assistant runs ${part.name} ${clip(toJsonText(part.params))}`]
|
|
28
|
+
default:
|
|
29
|
+
return []
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const lines = (message: Prompt.Message): ReadonlyArray<string> => {
|
|
34
|
+
switch (message.role) {
|
|
35
|
+
case "system":
|
|
36
|
+
return []
|
|
37
|
+
case "user":
|
|
38
|
+
return message.content.flatMap((part) => (part.type === "text" ? [`User: ${clip(part.text)}`] : []))
|
|
39
|
+
case "assistant":
|
|
40
|
+
return message.content.flatMap(assistantLines)
|
|
41
|
+
case "tool":
|
|
42
|
+
return message.content.flatMap((part) =>
|
|
43
|
+
part.type === "tool-result"
|
|
44
|
+
? [`Result of ${part.name}: ${clip(Predicate.isString(part.result) ? part.result : toJsonText(part.result))}`]
|
|
45
|
+
: []
|
|
46
|
+
)
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// The calls started and not answered yet: what the assistant is doing right now.
|
|
51
|
+
const runningCalls = (entries: ReadonlyArray<AssistantEvent.AnyEntry>) => {
|
|
52
|
+
const events = entries.flatMap(({ event }) => (AssistantEvent.isLibraryEvent(event) ? [event] : []))
|
|
53
|
+
const answered = new Set(events.flatMap((event) => (Predicate.isTagged(event, "ToolResult") ? [event.callId] : [])))
|
|
54
|
+
|
|
55
|
+
return events.flatMap((event) =>
|
|
56
|
+
Predicate.isTagged(event, "ToolStarted") && !answered.has(event.callId) ? [event.tool] : []
|
|
57
|
+
)
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// The latest messages, as plain lines, and what still runs: the summary's model gets no tools, so no tool call parts.
|
|
61
|
+
/** @internal */
|
|
62
|
+
export const transcript = (entries: ReadonlyArray<AssistantEvent.AnyEntry>): string => {
|
|
63
|
+
const running = runningCalls(entries)
|
|
64
|
+
const lastLines = History.fromEntries(entries).slice(-MAX_MESSAGES).flatMap(lines)
|
|
65
|
+
|
|
66
|
+
return [...lastLines, ...(running.length === 0 ? [] : [`Running now: ${running.join(", ")}`])].join("\n")
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const lastSequence = (entries: ReadonlyArray<AssistantEvent.AnyEntry>) => entries.at(-1)?.sequence ?? 0
|
|
70
|
+
|
|
71
|
+
const isWork = (event: { readonly _tag: string }) =>
|
|
72
|
+
Predicate.isTagged(event, "TurnStarted") ||
|
|
73
|
+
Predicate.isTagged(event, "ModelOutput") ||
|
|
74
|
+
Predicate.isTagged(event, "ToolStarted")
|
|
75
|
+
|
|
76
|
+
// After a message the user is up to date: a summary shows again only for work the model started since, not for what a
|
|
77
|
+
// handler does after its reply (a compaction, say).
|
|
78
|
+
const workingSinceMessage = (entries: ReadonlyArray<AssistantEvent.AnyEntry>) => {
|
|
79
|
+
const message = entries.findLastIndex(({ event }) => Predicate.isTagged(event, "AssistantMessage"))
|
|
80
|
+
|
|
81
|
+
return message === -1 || entries.slice(message + 1).some(({ event }) => isWork(event))
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
const MAX_CHECK_MILLIS = 500
|
|
85
|
+
|
|
86
|
+
const MIN_CHECK_MILLIS = 100
|
|
87
|
+
|
|
88
|
+
// A line for a turn that shows nothing for a while, or whose line hasn't changed for longer: never while a described
|
|
89
|
+
// call says what runs, or the model writes to the user, and only after something new happened.
|
|
90
|
+
/** @internal */
|
|
91
|
+
export const run = Effect.fnUntraced(function* (
|
|
92
|
+
summary: StatusSummary.StatusSummary["Service"],
|
|
93
|
+
live: Live.Live,
|
|
94
|
+
chat: Writer
|
|
95
|
+
) {
|
|
96
|
+
const summarized = yield* Ref.make(-1)
|
|
97
|
+
const after = Duration.toMillis(summary.after)
|
|
98
|
+
const staleAfter = Duration.toMillis(summary.staleAfter)
|
|
99
|
+
|
|
100
|
+
const tick = Effect.gen(function* () {
|
|
101
|
+
const current = yield* live.state
|
|
102
|
+
const entries = yield* chat.entries
|
|
103
|
+
const through = lastSequence(entries)
|
|
104
|
+
const waited = (yield* Clock.currentTimeMillis) - current.shownAt
|
|
105
|
+
|
|
106
|
+
if (
|
|
107
|
+
current.typing ||
|
|
108
|
+
current.running.length > 0 ||
|
|
109
|
+
waited < (current.shown === undefined ? after : staleAfter) ||
|
|
110
|
+
through === (yield* Ref.get(summarized)) ||
|
|
111
|
+
!workingSinceMessage(entries)
|
|
112
|
+
) {
|
|
113
|
+
return
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
const written = yield* summary.summarize(transcript(entries))
|
|
117
|
+
|
|
118
|
+
yield* Ref.set(summarized, through)
|
|
119
|
+
|
|
120
|
+
if (Option.isSome(written)) yield* live.summarized(written.value)
|
|
121
|
+
})
|
|
122
|
+
|
|
123
|
+
// Often enough that a line is filled or refreshed close to when it's due.
|
|
124
|
+
yield* tick.pipe(
|
|
125
|
+
Effect.repeat(Schedule.spaced(Math.max(MIN_CHECK_MILLIS, Math.min(MAX_CHECK_MILLIS, after / 2, staleAfter / 2))))
|
|
126
|
+
)
|
|
127
|
+
})
|
|
@@ -10,6 +10,7 @@ import * as Tracer from "effect/Tracer"
|
|
|
10
10
|
import * as AssistantEvent from "../AssistantEvent.ts"
|
|
11
11
|
import type { SpanAttributes } from "../Instructions.ts"
|
|
12
12
|
import { toJsonText } from "./schema.ts"
|
|
13
|
+
import { collect } from "./stream.ts"
|
|
13
14
|
|
|
14
15
|
type Traced = string | ReadonlyArray<{ readonly _tag: string }>
|
|
15
16
|
|
|
@@ -65,8 +66,11 @@ const encodeParts = Schema.encodeOption(Schema.Array(AssistantEvent.ModelPart))
|
|
|
65
66
|
|
|
66
67
|
type ModelCall = Parameters<Telemetry.SpanTransformer>[0]
|
|
67
68
|
|
|
69
|
+
// A streamed call's response is its stream's parts: joined first, as the log stores them.
|
|
68
70
|
const completionOf = (response: ModelCall["response"]) =>
|
|
69
|
-
encodeParts(
|
|
71
|
+
encodeParts(
|
|
72
|
+
collect(response).flatMap((part) => Option.toArray(Schema.decodeUnknownOption(AssistantEvent.ModelPart)(part)))
|
|
73
|
+
)
|
|
70
74
|
|
|
71
75
|
const annotateModelSpan = (
|
|
72
76
|
{ span, prompt, response }: ModelCall,
|
package/src/internal/turns.ts
CHANGED
|
@@ -22,13 +22,16 @@ import * as Loop from "./loop.ts"
|
|
|
22
22
|
import * as BlockKit from "../BlockKit.ts"
|
|
23
23
|
import type { CurrentChat } from "../CurrentChat.ts"
|
|
24
24
|
import * as Instructions from "../Instructions.ts"
|
|
25
|
+
import { StatusSummary } from "../StatusSummary.ts"
|
|
25
26
|
import * as ToolApproval from "../ToolApproval.ts"
|
|
26
27
|
import type * as Specialist from "../Specialist.ts"
|
|
27
28
|
import * as AppEvents from "./events.ts"
|
|
28
29
|
import * as History from "./history.ts"
|
|
29
30
|
import { mapText } from "./instructions.ts"
|
|
31
|
+
import * as Live from "./live.ts"
|
|
30
32
|
import { toJsonText } from "./schema.ts"
|
|
31
33
|
import * as Specialists from "./specialists.ts"
|
|
34
|
+
import * as Summary from "./summary.ts"
|
|
32
35
|
import * as Telemetry from "./telemetry.ts"
|
|
33
36
|
import * as Toolkits from "./toolkit.ts"
|
|
34
37
|
import type { Writer } from "./writer.ts"
|
|
@@ -79,6 +82,8 @@ export interface Registration {
|
|
|
79
82
|
readonly built: Specialist.Built
|
|
80
83
|
}>
|
|
81
84
|
readonly builtins: Toolkits.Tools<Assistant.TurnServices>
|
|
85
|
+
// Writes a status for a turn that has been working a while with nothing to show.
|
|
86
|
+
readonly summary: Option.Option<StatusSummary["Service"]>
|
|
82
87
|
}
|
|
83
88
|
|
|
84
89
|
/** @internal */
|
|
@@ -115,7 +120,8 @@ export const register = Effect.fnUntraced(function* (
|
|
|
115
120
|
tracer,
|
|
116
121
|
open: options.open,
|
|
117
122
|
specialists,
|
|
118
|
-
builtins
|
|
123
|
+
builtins,
|
|
124
|
+
summary: yield* Effect.serviceOption(StatusSummary)
|
|
119
125
|
}
|
|
120
126
|
|
|
121
127
|
return registration
|
|
@@ -374,10 +380,20 @@ export const run = Effect.fnUntraced(
|
|
|
374
380
|
const turn = makeTurnApi(options.registration, chat, startSequence, options.checkOpen, ranLoop)
|
|
375
381
|
|
|
376
382
|
const record = (event: AssistantLoop.Event) => chat.append([event])
|
|
383
|
+
const live = yield* Live.make(chat.signal)
|
|
384
|
+
|
|
385
|
+
// The summary runs beside the turn's work and stops with it.
|
|
386
|
+
const summarizing = <A, E, R>(work: Effect.Effect<A, E, R>) =>
|
|
387
|
+
Option.match(options.registration.summary, {
|
|
388
|
+
onNone: () => work,
|
|
389
|
+
onSome: (summary) =>
|
|
390
|
+
Effect.forkScoped(Summary.run(summary, live, chat)).pipe(Effect.andThen(work), Effect.scoped)
|
|
391
|
+
})
|
|
377
392
|
|
|
378
393
|
yield* options.handler(turn).pipe(
|
|
379
394
|
Stream.runForEach(record),
|
|
380
395
|
Effect.onExit((exit) => (Exit.hasInterrupts(exit) ? Effect.void : closeOpenCalls(chat, startSequence))),
|
|
396
|
+
Effect.provideService(Live.TurnLive, Option.some(live)),
|
|
381
397
|
// A loop the app writes gets the assistant's own tools too, with this turn's services.
|
|
382
398
|
Effect.provideService(
|
|
383
399
|
Loop.TurnTools,
|
|
@@ -386,6 +402,9 @@ export const run = Effect.fnUntraced(
|
|
|
386
402
|
// A turn's loop waits for the user's approval, and runs the calls they approved.
|
|
387
403
|
Effect.provideService(Loop.TurnApprovals, Option.some({ approved: Effect.map(chat.entries, approvedCalls) })),
|
|
388
404
|
Effect.provideContext(options.services),
|
|
405
|
+
summarizing,
|
|
406
|
+
// Nothing the turn showed outlives it, also when it fails.
|
|
407
|
+
Effect.ensuring(live.reset),
|
|
389
408
|
Effect.matchCauseEffect({
|
|
390
409
|
onSuccess: Effect.fnUntraced(function* () {
|
|
391
410
|
const all = yield* chat.entries
|