effect-assistant 0.11.1 → 0.13.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/Assistant.d.ts +49 -4
- package/dist/Assistant.d.ts.map +1 -1
- package/dist/Assistant.js +33 -3
- 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 +2 -2
- package/dist/Memory.js +1 -1
- package/dist/Specialist.d.ts.map +1 -1
- package/dist/Specialist.js +2 -1
- package/dist/Specialist.js.map +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 +74 -20
- package/dist/Timeline.js.map +1 -1
- package/dist/ToolStatus.d.ts +72 -0
- package/dist/ToolStatus.d.ts.map +1 -0
- package/dist/ToolStatus.js +71 -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 +19 -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 +44 -13
- package/dist/internal/loop.js.map +1 -1
- package/dist/internal/schema.js +1 -1
- package/dist/internal/specialists.js +4 -3
- 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 +65 -0
- package/dist/internal/summary.js.map +1 -0
- package/dist/internal/telemetry.js +16 -5
- 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 +5 -0
- package/dist/internal/turns.d.ts.map +1 -1
- package/dist/internal/turns.js +18 -7
- 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 +77 -13
- package/src/AssistantEngine.ts +25 -11
- package/src/ChatSignal.ts +71 -0
- package/src/Specialist.ts +5 -1
- package/src/StatusSummary.ts +126 -0
- package/src/Timeline.ts +82 -20
- package/src/ToolStatus.ts +90 -0
- package/src/index.ts +15 -0
- package/src/internal/chat.ts +41 -21
- package/src/internal/live.ts +133 -0
- package/src/internal/loop.ts +104 -25
- package/src/internal/specialists.ts +6 -0
- package/src/internal/stream.ts +98 -0
- package/src/internal/summary.ts +123 -0
- package/src/internal/telemetry.ts +25 -2
- package/src/internal/turns.ts +27 -3
- package/src/internal/writer.ts +3 -0
package/dist/internal/chat.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { ChatDeleted, isInput, isLibraryEvent } from "../AssistantEvent.js";
|
|
2
2
|
import { ChatLog } from "../ChatLog.js";
|
|
3
|
+
import { isChatSignal } from "../ChatSignal.js";
|
|
3
4
|
import { CurrentChat } from "../CurrentChat.js";
|
|
4
5
|
import { Chats } from "../Chats.js";
|
|
5
6
|
import { Runner } from "../Specialist.js";
|
|
@@ -39,12 +40,14 @@ const makeHolding = Effect.fnUntraced(function* (keepAlive) {
|
|
|
39
40
|
}));
|
|
40
41
|
return (effect) => Effect.acquireUseRelease(acquire, () => effect, (_, exit) => Exit.hasInterrupts(exit) ? Effect.void : release);
|
|
41
42
|
});
|
|
42
|
-
const isChatDeleted = (
|
|
43
|
+
const isChatDeleted = (item) => !isChatSignal(item) && Predicate.isTagged(item.event, "ChatDeleted");
|
|
44
|
+
const isEntry = (item) => !isChatSignal(item);
|
|
43
45
|
const deletedChat = (entries) => ({
|
|
44
46
|
submit: () => Effect.void,
|
|
45
47
|
submitInBackground: () => Effect.void,
|
|
46
48
|
resume: Effect.void,
|
|
47
49
|
stream: (after) => Stream.fromIterable(entries.filter((entry) => entry.sequence > after)),
|
|
50
|
+
live: (after) => Stream.fromIterable(entries.filter((entry) => entry.sequence > after)),
|
|
48
51
|
delete: Effect.void
|
|
49
52
|
});
|
|
50
53
|
const refused = (input, why) => Effect.logDebug(`assistant chat ${why}: ${input._tag} not stored`);
|
|
@@ -82,7 +85,10 @@ const make = Effect.fnUntraced(function* (options) {
|
|
|
82
85
|
const chat = {
|
|
83
86
|
chatId,
|
|
84
87
|
entries: Ref.get(entries),
|
|
85
|
-
append: (events) => Semaphore.withPermit(writes)(write(events))
|
|
88
|
+
append: (events) => Semaphore.withPermit(writes)(write(events)),
|
|
89
|
+
signal: (signal) => Effect.gen(function* () {
|
|
90
|
+
if (!(yield* isDeleted)) yield* PubSub.publish(channel, signal);
|
|
91
|
+
})
|
|
86
92
|
};
|
|
87
93
|
const toStored = (event) => isLibraryEvent(event) ? Effect.succeed(event) : isRegistered(app, event) ? encode(app, event) : Effect.die(`${event._tag} isn't one of the ${registration.definition.name} assistant's events.`);
|
|
88
94
|
const handle = {
|
|
@@ -130,6 +136,7 @@ const make = Effect.fnUntraced(function* (options) {
|
|
|
130
136
|
handle,
|
|
131
137
|
specialists,
|
|
132
138
|
model: registration.model,
|
|
139
|
+
tracer: registration.tracer,
|
|
133
140
|
holding,
|
|
134
141
|
report: Effect.fnUntraced(function* (finished) {
|
|
135
142
|
yield* store(finished);
|
|
@@ -150,6 +157,14 @@ const make = Effect.fnUntraced(function* (options) {
|
|
|
150
157
|
yield* Fiber.await(closing);
|
|
151
158
|
});
|
|
152
159
|
yield* checkAvailable;
|
|
160
|
+
const subscribe = (after) => Stream.unwrap(Effect.gen(function* () {
|
|
161
|
+
const subscription = yield* PubSub.subscribe(channel);
|
|
162
|
+
const all = yield* chat.entries;
|
|
163
|
+
const stored = all.filter((entry) => entry.sequence > after);
|
|
164
|
+
const last = stored.at(-1)?.sequence ?? after;
|
|
165
|
+
if (all.some(isChatDeleted)) return Stream.fromIterable(stored);
|
|
166
|
+
return Stream.concat(Stream.fromIterable(stored), Stream.fromSubscription(subscription).pipe(Stream.filter((item) => isChatSignal(item) || item.sequence > last))).pipe(Stream.takeUntil(isChatDeleted));
|
|
167
|
+
}));
|
|
153
168
|
return {
|
|
154
169
|
submit: Effect.fnUntraced(function* (input) {
|
|
155
170
|
yield* store(input);
|
|
@@ -162,14 +177,8 @@ const make = Effect.fnUntraced(function* (options) {
|
|
|
162
177
|
resume: Effect.gen(function* () {
|
|
163
178
|
if (needsTurn(yield* chat.entries)) yield* inBackground(drain);
|
|
164
179
|
}),
|
|
165
|
-
stream: (after) =>
|
|
166
|
-
|
|
167
|
-
const all = yield* chat.entries;
|
|
168
|
-
const stored = all.filter((entry) => entry.sequence > after);
|
|
169
|
-
const last = stored.at(-1)?.sequence ?? after;
|
|
170
|
-
if (all.some(isChatDeleted)) return Stream.fromIterable(stored);
|
|
171
|
-
return Stream.concat(Stream.fromIterable(stored), Stream.fromSubscription(subscription).pipe(Stream.filter((entry) => entry.sequence > last))).pipe(Stream.takeUntil(isChatDeleted));
|
|
172
|
-
})),
|
|
180
|
+
stream: (after) => subscribe(after).pipe(Stream.filter(isEntry)),
|
|
181
|
+
live: subscribe,
|
|
173
182
|
delete: remove
|
|
174
183
|
};
|
|
175
184
|
});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"chat.js","names":["AssistantEvent.isLibraryEvent","AssistantEvent.isInput","Chats.Chats","AppEvents.isRegistered","AppEvents.encode","AppEvents.decodeEntries","ToolApproval.fromEntries","AppEvents.markInput","History.needsTurn","Turns.run","Specialists.makeRunner","SpecialistService.Runner"],"sources":["../../src/internal/chat.ts"],"sourcesContent":["import * as Cause from \"effect/Cause\"\nimport * as Context from \"effect/Context\"\nimport * as Deferred from \"effect/Deferred\"\nimport * as Effect from \"effect/Effect\"\nimport * as Exit from \"effect/Exit\"\nimport * as Fiber from \"effect/Fiber\"\nimport * as FiberSet from \"effect/FiberSet\"\nimport * as Predicate from \"effect/Predicate\"\nimport * as PubSub from \"effect/PubSub\"\nimport * as Ref from \"effect/Ref\"\nimport * as Scope from \"effect/Scope\"\nimport * as Semaphore from \"effect/Semaphore\"\nimport * as Stream from \"effect/Stream\"\nimport * as SynchronizedRef from \"effect/SynchronizedRef\"\nimport * as AssistantEvent from \"../AssistantEvent.ts\"\nimport type { ChatId } from \"../ChatId.ts\"\nimport { ChatLog } from \"../ChatLog.ts\"\nimport { CurrentChat } from \"../CurrentChat.ts\"\nimport * as Chats from \"../Chats.ts\"\nimport * as SpecialistService from \"../Specialist.ts\"\nimport * as ToolApproval from \"../ToolApproval.ts\"\nimport * as AppEvents from \"./events.ts\"\nimport * as History from \"./history.ts\"\nimport * as Specialists from \"./specialists.ts\"\nimport * as Turns from \"./turns.ts\"\nimport type { Writer } from \"./writer.ts\"\n\nconst idOf = (event: { readonly _tag: string }): string | undefined =>\n AssistantEvent.isLibraryEvent(event)\n ? AssistantEvent.isInput(event)\n ? event.id\n : undefined\n : Predicate.hasProperty(event, \"id\") && Predicate.isString(event.id)\n ? event.id\n : undefined\n\nconst makeChannelPubSub = Effect.acquireRelease(PubSub.unbounded<AssistantEvent.Entry>(), PubSub.shutdown)\n\nconst makeHolding = Effect.fnUntraced(function* (keepAlive: (enabled: boolean) => Effect.Effect<void>) {\n const holds = yield* SynchronizedRef.make(0)\n\n const acquire = SynchronizedRef.updateEffect(\n holds,\n Effect.fnUntraced(function* (n: number) {\n if (n !== 0) return n + 1\n\n yield* keepAlive(true)\n\n return 1\n })\n )\n\n const release = SynchronizedRef.updateEffect(\n holds,\n Effect.fnUntraced(function* (n: number) {\n if (n !== 1) return n - 1\n\n yield* keepAlive(false)\n\n return 0\n })\n )\n\n return (effect: Effect.Effect<void>) =>\n Effect.acquireUseRelease(\n acquire,\n () => effect,\n // An interrupted effect (a shutdown) keeps its hold, so the chat starts again and picks up where it was cut off.\n (_, exit) => (Exit.hasInterrupts(exit) ? Effect.void : release)\n )\n})\n\n/** @internal */\nexport interface Chat {\n readonly submit: (input: AssistantEvent.Input | AssistantEvent.StoredAppEvent) => Effect.Effect<void>\n readonly submitInBackground: (input: AssistantEvent.Input | AssistantEvent.StoredAppEvent) => Effect.Effect<void>\n // For inputs no turn has read, like a report stored during a shutdown.\n readonly resume: Effect.Effect<void>\n readonly stream: (after: number) => Stream.Stream<AssistantEvent.Entry>\n // Stops what works in the chat and ends its subscribers' streams; its log stays.\n readonly delete: Effect.Effect<void>\n}\n\nconst isChatDeleted = (entry: AssistantEvent.Entry) => Predicate.isTagged(entry.event, \"ChatDeleted\")\n\nconst deletedChat = (entries: ReadonlyArray<AssistantEvent.Entry>): Chat => ({\n submit: () => Effect.void,\n submitInBackground: () => Effect.void,\n resume: Effect.void,\n stream: (after) => Stream.fromIterable(entries.filter((entry) => entry.sequence > after)),\n delete: Effect.void\n})\n\nconst refused = (input: { readonly _tag: string }, why: string) =>\n Effect.logDebug(`assistant chat ${why}: ${input._tag} not stored`)\n\n/** @internal */\nexport const make = Effect.fnUntraced(function* (options: {\n readonly registration: Turns.Registration\n readonly chatId: ChatId\n readonly keepAlive: (enabled: boolean) => Effect.Effect<void>\n}) {\n const { registration, chatId } = options\n const { app } = registration.definition\n const storage = yield* ChatLog\n const chats = yield* Chats.Chats\n const initial = yield* storage.read(chatId)\n\n if (initial.some(isChatDeleted)) return deletedChat(initial)\n\n const channel = yield* makeChannelPubSub\n const writes = yield* Semaphore.make(1)\n const turnsLock = yield* Semaphore.make(1)\n const holding = yield* makeHolding(options.keepAlive)\n const deleted = yield* Deferred.make<undefined>()\n // So deleting the chat stops all its work: its turns, runs, and the builds' resources.\n const working = yield* Scope.fork(yield* Effect.scope)\n const inWorking = Effect.provideService(Scope.Scope, working)\n const background = yield* inWorking(FiberSet.make())\n const requests = yield* inWorking(FiberSet.make())\n\n const isDeleted = Deferred.isDone(deleted)\n\n // The chat is its log's only writer, so it reads the log once and turns read memory.\n const entries = yield* Ref.make(initial)\n\n // Every writer goes through here, one at a time, so each append is numbered after the last. After a delete it\n // stores nothing.\n const write = Effect.fnUntraced(function* (\n events: ReadonlyArray<AssistantEvent.AssistantEvent | AssistantEvent.StoredAppEvent>\n ) {\n const unknown = events.find((event) => !AssistantEvent.isLibraryEvent(event) && !AppEvents.isRegistered(app, event))\n\n if (unknown !== undefined) {\n return yield* Effect.die(`${unknown._tag} isn't one of the ${registration.definition.name} assistant's events.`)\n }\n\n if (yield* isDeleted) return []\n\n return yield* stored(events)\n })\n\n // Uninterruptible: a stored append must reach memory and the subscribers too, or a turn misses it and replies twice.\n const stored = Effect.fnUntraced(function* (\n events: ReadonlyArray<AssistantEvent.AssistantEvent | AssistantEvent.StoredAppEvent>\n ) {\n const stored = yield* storage.append(chatId, events)\n\n yield* Ref.update(entries, (all) => [...all, ...stored])\n yield* PubSub.publishAll(channel, stored)\n\n return stored\n }, Effect.uninterruptible)\n\n const chat: Writer = {\n chatId,\n entries: Ref.get(entries),\n append: (events) => Semaphore.withPermit(writes)(write(events))\n }\n\n const toStored = (event: {\n readonly _tag: string\n }): Effect.Effect<AssistantEvent.AssistantEvent | AssistantEvent.StoredAppEvent> =>\n AssistantEvent.isLibraryEvent(event)\n ? Effect.succeed(event)\n : AppEvents.isRegistered(app, event)\n ? AppEvents.encode(app, event)\n : Effect.die(`${event._tag} isn't one of the ${registration.definition.name} assistant's events.`)\n\n // What handlers, tools, and runs see: the assistant's own events decoded, and encoded when appended.\n const handle: Turns.TypedChat = {\n chatId,\n entries: Effect.map(chat.entries, (all) => AppEvents.decodeEntries(app, all)),\n append: (...events) => Effect.asVoid(Effect.flatMap(Effect.forEach(events, toStored), chat.append))\n }\n\n // A chat the app deleted is deleted here too, which stops what works in it: a turn that checks stops there.\n const checkAvailable: Effect.Effect<boolean> = chats.get(chatId).pipe(\n Effect.as(true),\n Effect.catchTag(\"GetChatError\", (unavailable) =>\n unavailable.reason === \"Deleted\"\n ? Effect.as(\n Effect.suspend(() => remove),\n false\n )\n : Effect.succeed(false)\n )\n )\n\n const store = Effect.fnUntraced(function* (input: AssistantEvent.Input | AssistantEvent.StoredAppEvent) {\n if (yield* isDeleted) return yield* refused(input, \"is deleted\")\n\n if (!(yield* checkAvailable)) return yield* refused(input, \"is unavailable\")\n\n // Checked under the write lock: a retry that arrives while the first submit is storing is stored once.\n yield* Semaphore.withPermit(writes)(\n Effect.gen(function* () {\n if (yield* isDeleted) return yield* refused(input, \"is deleted\")\n\n const id = idOf(input)\n const all = yield* Ref.get(entries)\n\n if (id !== undefined && all.some(({ event }) => idOf(event) === id)) return\n\n // A decision counts only for a call that still waits: not one decided, or expired, or unknown.\n if (\n Predicate.isTagged(input, \"ApprovalDecision\") &&\n ToolApproval.fromEntries(all).get(input.callId)?.status !== \"pending\"\n ) {\n return yield* refused(input, \"has no call waiting for that decision\")\n }\n\n // Stored as an input: it came in from outside a turn, so it wakes one, also after a restart.\n yield* write([AssistantEvent.isLibraryEvent(input) ? input : AppEvents.markInput(input)])\n })\n )\n })\n\n // The handlers' builds run in the chat's working scope: what they set up lives until the chat closes or is deleted.\n const handler = yield* inWorking(Effect.provideService(registration.open(handle), CurrentChat, handle))\n\n const specialists = yield* Effect.forEach(\n registration.specialists,\n Effect.fnUntraced(function* ({ specialist, built }) {\n const handler = yield* inWorking(Effect.provideService(built.open, CurrentChat, handle))\n\n return { specialist, handler }\n })\n )\n\n const inBackground = (effect: Effect.Effect<void>) => FiberSet.run(background, holding(effect)).pipe(Effect.asVoid)\n\n // A request's turns run in the working scope too, so a delete stops them; the request ends quietly then.\n const inRequest = Effect.fnUntraced(function* (effect: Effect.Effect<void>) {\n const fiber = yield* FiberSet.run(requests, effect)\n const exit = yield* Fiber.await(fiber).pipe(Effect.onInterrupt(() => Fiber.interrupt(fiber)))\n\n if (Exit.isFailure(exit) && !Cause.hasInterruptsOnly(exit.cause)) {\n return yield* Effect.failCause(exit.cause)\n }\n }, Effect.orDie)\n\n const drain: Effect.Effect<void> = Semaphore.withPermit(turnsLock)(\n Effect.gen(function* () {\n while (!(yield* isDeleted) && History.needsTurn(yield* chat.entries)) {\n yield* Turns.run({ registration, handler, chat, services, checkOpen: Effect.asVoid(checkAvailable) })\n }\n })\n )\n\n const runner = yield* inWorking(\n Specialists.makeRunner({\n chat,\n handle,\n specialists,\n model: registration.model,\n holding,\n report: Effect.fnUntraced(function* (finished) {\n yield* store(finished)\n yield* inBackground(drain)\n })\n })\n )\n\n const services = Context.make(CurrentChat, handle).pipe(\n Context.add(SpecialistService.Runner, runner),\n Context.merge(registration.model)\n )\n\n const remove = Effect.gen(function* () {\n // Under the write lock: an append in flight is stored before the `ChatDeleted`; none comes after.\n const first = yield* Semaphore.withPermit(writes)(\n Effect.gen(function* () {\n if (yield* isDeleted) return false\n\n yield* stored([AssistantEvent.ChatDeleted.make({})])\n\n return yield* Deferred.succeed(deleted, undefined)\n })\n ).pipe(Effect.uninterruptible)\n\n if (!first) return\n\n // Its own fiber: a tool that deletes its own chat is in the working scope, and closing it interrupts that tool.\n // Interrupted work keeps its hold (see `makeHolding`), so the chat lets go of it here.\n const closing = yield* Effect.forkDetach(\n Effect.gen(function* () {\n yield* Scope.close(working, Exit.void)\n yield* options.keepAlive(false)\n })\n )\n\n yield* Fiber.await(closing)\n })\n\n // A chat the app deleted while it was closed is deleted now, so its subscribers hear it.\n yield* checkAvailable\n\n const chatApi: Chat = {\n submit: Effect.fnUntraced(function* (input) {\n yield* store(input)\n yield* inRequest(drain)\n }),\n submitInBackground: Effect.fnUntraced(function* (input) {\n yield* store(input)\n yield* inBackground(drain)\n }),\n resume: Effect.gen(function* () {\n if (History.needsTurn(yield* chat.entries)) yield* inBackground(drain)\n }),\n stream: (after) =>\n Stream.unwrap(\n Effect.gen(function* () {\n // Subscribes before it reads, so nothing appended in between is missed.\n const subscription = yield* PubSub.subscribe(channel)\n const all = yield* chat.entries\n const stored = all.filter((entry) => entry.sequence > after)\n const last = stored.at(-1)?.sequence ?? after\n\n // Deleted: what the reader hasn't seen, then the end. Otherwise the stream ends with the `ChatDeleted`.\n if (all.some(isChatDeleted)) return Stream.fromIterable(stored)\n\n return Stream.concat(\n Stream.fromIterable(stored),\n Stream.fromSubscription(subscription).pipe(Stream.filter((entry) => entry.sequence > last))\n ).pipe(Stream.takeUntil(isChatDeleted))\n })\n ),\n delete: remove\n }\n\n return chatApi\n})\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;AA2BA,MAAM,QAAQ,UACZA,eAA8B,KAAK,IAC/BC,QAAuB,KAAK,IAC1B,MAAM,KACN,KAAA,IACF,UAAU,YAAY,OAAO,IAAI,KAAK,UAAU,SAAS,MAAM,EAAE,IAC/D,MAAM,KACN,KAAA;AAER,MAAM,oBAAoB,OAAO,eAAe,OAAO,UAAgC,GAAG,OAAO,QAAQ;AAEzG,MAAM,cAAc,OAAO,WAAW,WAAW,WAAsD;CACrG,MAAM,QAAQ,OAAO,gBAAgB,KAAK,CAAC;CAE3C,MAAM,UAAU,gBAAgB,aAC9B,OACA,OAAO,WAAW,WAAW,GAAW;EACtC,IAAI,MAAM,GAAG,OAAO,IAAI;EAExB,OAAO,UAAU,IAAI;EAErB,OAAO;CACT,CAAC,CACH;CAEA,MAAM,UAAU,gBAAgB,aAC9B,OACA,OAAO,WAAW,WAAW,GAAW;EACtC,IAAI,MAAM,GAAG,OAAO,IAAI;EAExB,OAAO,UAAU,KAAK;EAEtB,OAAO;CACT,CAAC,CACH;CAEA,QAAQ,WACN,OAAO,kBACL,eACM,SAEL,GAAG,SAAU,KAAK,cAAc,I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ClB,OAAO,UAAU,KAAK;EACxB,CAAC;EACD,oBAAoB,OAAO,WAAW,WAAW,OAAO;GACtD,OAAO,MAAM,KAAK;GAClB,OAAO,aAAa,KAAK;EAC3B,CAAC;EACD,QAAQ,OAAO,IAAI,aAAa;GAC9B,IAAIH,UAAkB,OAAO,KAAK,OAAO,GAAG,OAAO,aAAa,KAAK;EACvE,CAAC;EACD,SAAS,UACP,OAAO,OACL,OAAO,IAAI,aAAa;GAEtB,MAAM,eAAe,OAAO,OAAO,UAAU,OAAO;GACpD,MAAM,MAAM,OAAO,KAAK;GACxB,MAAM,SAAS,IAAI,QAAQ,UAAU,MAAM,WAAW,KAAK;GAC3D,MAAM,OAAO,OAAO,GAAG,EAAE,CAAC,EAAE,YAAY;GAGxC,IAAI,IAAI,KAAK,aAAa,GAAG,OAAO,OAAO,aAAa,MAAM;GAE9D,OAAO,OAAO,OACZ,OAAO,aAAa,MAAM,GAC1B,OAAO,iBAAiB,YAAY,CAAC,CAAC,KAAK,OAAO,QAAQ,UAAU,MAAM,WAAW,IAAI,CAAC,CAC5F,CAAC,CAAC,KAAK,OAAO,UAAU,aAAa,CAAC;EACxC,CAAC,CACH;EACF,QAAQ;CAGG;AACf,CAAC"}
|
|
1
|
+
{"version":3,"file":"chat.js","names":["AssistantEvent.isLibraryEvent","AssistantEvent.isInput","ChatSignal.isChatSignal","Chats.Chats","AppEvents.isRegistered","AppEvents.encode","AppEvents.decodeEntries","ToolApproval.fromEntries","AppEvents.markInput","History.needsTurn","Turns.run","Specialists.makeRunner","SpecialistService.Runner"],"sources":["../../src/internal/chat.ts"],"sourcesContent":["import * as Cause from \"effect/Cause\"\nimport * as Context from \"effect/Context\"\nimport * as Deferred from \"effect/Deferred\"\nimport * as Effect from \"effect/Effect\"\nimport * as Exit from \"effect/Exit\"\nimport * as Fiber from \"effect/Fiber\"\nimport * as FiberSet from \"effect/FiberSet\"\nimport * as Predicate from \"effect/Predicate\"\nimport * as PubSub from \"effect/PubSub\"\nimport * as Ref from \"effect/Ref\"\nimport * as Scope from \"effect/Scope\"\nimport * as Semaphore from \"effect/Semaphore\"\nimport * as Stream from \"effect/Stream\"\nimport * as SynchronizedRef from \"effect/SynchronizedRef\"\nimport * as AssistantEvent from \"../AssistantEvent.ts\"\nimport type { ChatId } from \"../ChatId.ts\"\nimport { ChatLog } from \"../ChatLog.ts\"\nimport * as ChatSignal from \"../ChatSignal.ts\"\nimport { CurrentChat } from \"../CurrentChat.ts\"\nimport * as Chats from \"../Chats.ts\"\nimport * as SpecialistService from \"../Specialist.ts\"\nimport * as ToolApproval from \"../ToolApproval.ts\"\nimport * as AppEvents from \"./events.ts\"\nimport * as History from \"./history.ts\"\nimport * as Specialists from \"./specialists.ts\"\nimport * as Turns from \"./turns.ts\"\nimport type { Writer } from \"./writer.ts\"\n\nconst idOf = (event: { readonly _tag: string }): string | undefined =>\n AssistantEvent.isLibraryEvent(event)\n ? AssistantEvent.isInput(event)\n ? event.id\n : undefined\n : Predicate.hasProperty(event, \"id\") && Predicate.isString(event.id)\n ? event.id\n : undefined\n\n/** @internal */\nexport type Item = AssistantEvent.Entry | ChatSignal.ChatSignal\n\n// Entries and signals share it, so a live subscriber gets them in the order they happened.\nconst makeChannelPubSub = Effect.acquireRelease(PubSub.unbounded<Item>(), PubSub.shutdown)\n\nconst makeHolding = Effect.fnUntraced(function* (keepAlive: (enabled: boolean) => Effect.Effect<void>) {\n const holds = yield* SynchronizedRef.make(0)\n\n const acquire = SynchronizedRef.updateEffect(\n holds,\n Effect.fnUntraced(function* (n: number) {\n if (n !== 0) return n + 1\n\n yield* keepAlive(true)\n\n return 1\n })\n )\n\n const release = SynchronizedRef.updateEffect(\n holds,\n Effect.fnUntraced(function* (n: number) {\n if (n !== 1) return n - 1\n\n yield* keepAlive(false)\n\n return 0\n })\n )\n\n return (effect: Effect.Effect<void>) =>\n Effect.acquireUseRelease(\n acquire,\n () => effect,\n // An interrupted effect (a shutdown) keeps its hold, so the chat starts again and picks up where it was cut off.\n (_, exit) => (Exit.hasInterrupts(exit) ? Effect.void : release)\n )\n})\n\n/** @internal */\nexport interface Chat {\n readonly submit: (input: AssistantEvent.Input | AssistantEvent.StoredAppEvent) => Effect.Effect<void>\n readonly submitInBackground: (input: AssistantEvent.Input | AssistantEvent.StoredAppEvent) => Effect.Effect<void>\n // For inputs no turn has read, like a report stored during a shutdown.\n readonly resume: Effect.Effect<void>\n readonly stream: (after: number) => Stream.Stream<AssistantEvent.Entry>\n // With the signals sent while it streams; a signal sent before isn't sent again.\n readonly live: (after: number) => Stream.Stream<Item>\n // Stops what works in the chat and ends its subscribers' streams; its log stays.\n readonly delete: Effect.Effect<void>\n}\n\nconst isChatDeleted = (item: Item) => !ChatSignal.isChatSignal(item) && Predicate.isTagged(item.event, \"ChatDeleted\")\n\nconst isEntry = (item: Item): item is AssistantEvent.Entry => !ChatSignal.isChatSignal(item)\n\nconst deletedChat = (entries: ReadonlyArray<AssistantEvent.Entry>): Chat => ({\n submit: () => Effect.void,\n submitInBackground: () => Effect.void,\n resume: Effect.void,\n stream: (after) => Stream.fromIterable(entries.filter((entry) => entry.sequence > after)),\n live: (after) => Stream.fromIterable(entries.filter((entry) => entry.sequence > after)),\n delete: Effect.void\n})\n\nconst refused = (input: { readonly _tag: string }, why: string) =>\n Effect.logDebug(`assistant chat ${why}: ${input._tag} not stored`)\n\n/** @internal */\nexport const make = Effect.fnUntraced(function* (options: {\n readonly registration: Turns.Registration\n readonly chatId: ChatId\n readonly keepAlive: (enabled: boolean) => Effect.Effect<void>\n}) {\n const { registration, chatId } = options\n const { app } = registration.definition\n const storage = yield* ChatLog\n const chats = yield* Chats.Chats\n const initial = yield* storage.read(chatId)\n\n if (initial.some(isChatDeleted)) return deletedChat(initial)\n\n const channel = yield* makeChannelPubSub\n const writes = yield* Semaphore.make(1)\n const turnsLock = yield* Semaphore.make(1)\n const holding = yield* makeHolding(options.keepAlive)\n const deleted = yield* Deferred.make<undefined>()\n // So deleting the chat stops all its work: its turns, runs, and the builds' resources.\n const working = yield* Scope.fork(yield* Effect.scope)\n const inWorking = Effect.provideService(Scope.Scope, working)\n const background = yield* inWorking(FiberSet.make())\n const requests = yield* inWorking(FiberSet.make())\n\n const isDeleted = Deferred.isDone(deleted)\n\n // The chat is its log's only writer, so it reads the log once and turns read memory.\n const entries = yield* Ref.make(initial)\n\n // Every writer goes through here, one at a time, so each append is numbered after the last. After a delete it\n // stores nothing.\n const write = Effect.fnUntraced(function* (\n events: ReadonlyArray<AssistantEvent.AssistantEvent | AssistantEvent.StoredAppEvent>\n ) {\n const unknown = events.find((event) => !AssistantEvent.isLibraryEvent(event) && !AppEvents.isRegistered(app, event))\n\n if (unknown !== undefined) {\n return yield* Effect.die(`${unknown._tag} isn't one of the ${registration.definition.name} assistant's events.`)\n }\n\n if (yield* isDeleted) return []\n\n return yield* stored(events)\n })\n\n // Uninterruptible: a stored append must reach memory and the subscribers too, or a turn misses it and replies twice.\n const stored = Effect.fnUntraced(function* (\n events: ReadonlyArray<AssistantEvent.AssistantEvent | AssistantEvent.StoredAppEvent>\n ) {\n const stored = yield* storage.append(chatId, events)\n\n yield* Ref.update(entries, (all) => [...all, ...stored])\n yield* PubSub.publishAll(channel, stored)\n\n return stored\n }, Effect.uninterruptible)\n\n const chat: Writer = {\n chatId,\n entries: Ref.get(entries),\n append: (events) => Semaphore.withPermit(writes)(write(events)),\n signal: (signal) =>\n Effect.gen(function* () {\n if (!(yield* isDeleted)) yield* PubSub.publish(channel, signal)\n })\n }\n\n const toStored = (event: {\n readonly _tag: string\n }): Effect.Effect<AssistantEvent.AssistantEvent | AssistantEvent.StoredAppEvent> =>\n AssistantEvent.isLibraryEvent(event)\n ? Effect.succeed(event)\n : AppEvents.isRegistered(app, event)\n ? AppEvents.encode(app, event)\n : Effect.die(`${event._tag} isn't one of the ${registration.definition.name} assistant's events.`)\n\n // What handlers, tools, and runs see: the assistant's own events decoded, and encoded when appended.\n const handle: Turns.TypedChat = {\n chatId,\n entries: Effect.map(chat.entries, (all) => AppEvents.decodeEntries(app, all)),\n append: (...events) => Effect.asVoid(Effect.flatMap(Effect.forEach(events, toStored), chat.append))\n }\n\n // A chat the app deleted is deleted here too, which stops what works in it: a turn that checks stops there.\n const checkAvailable: Effect.Effect<boolean> = chats.get(chatId).pipe(\n Effect.as(true),\n Effect.catchTag(\"GetChatError\", (unavailable) =>\n unavailable.reason === \"Deleted\"\n ? Effect.as(\n Effect.suspend(() => remove),\n false\n )\n : Effect.succeed(false)\n )\n )\n\n const store = Effect.fnUntraced(function* (input: AssistantEvent.Input | AssistantEvent.StoredAppEvent) {\n if (yield* isDeleted) return yield* refused(input, \"is deleted\")\n\n if (!(yield* checkAvailable)) return yield* refused(input, \"is unavailable\")\n\n // Checked under the write lock: a retry that arrives while the first submit is storing is stored once.\n yield* Semaphore.withPermit(writes)(\n Effect.gen(function* () {\n if (yield* isDeleted) return yield* refused(input, \"is deleted\")\n\n const id = idOf(input)\n const all = yield* Ref.get(entries)\n\n if (id !== undefined && all.some(({ event }) => idOf(event) === id)) return\n\n // A decision counts only for a call that still waits: not one decided, or expired, or unknown.\n if (\n Predicate.isTagged(input, \"ApprovalDecision\") &&\n ToolApproval.fromEntries(all).get(input.callId)?.status !== \"pending\"\n ) {\n return yield* refused(input, \"has no call waiting for that decision\")\n }\n\n // Stored as an input: it came in from outside a turn, so it wakes one, also after a restart.\n yield* write([AssistantEvent.isLibraryEvent(input) ? input : AppEvents.markInput(input)])\n })\n )\n })\n\n // The handlers' builds run in the chat's working scope: what they set up lives until the chat closes or is deleted.\n const handler = yield* inWorking(Effect.provideService(registration.open(handle), CurrentChat, handle))\n\n const specialists = yield* Effect.forEach(\n registration.specialists,\n Effect.fnUntraced(function* ({ specialist, built }) {\n const handler = yield* inWorking(Effect.provideService(built.open, CurrentChat, handle))\n\n return { specialist, handler }\n })\n )\n\n const inBackground = (effect: Effect.Effect<void>) => FiberSet.run(background, holding(effect)).pipe(Effect.asVoid)\n\n // A request's turns run in the working scope too, so a delete stops them; the request ends quietly then.\n const inRequest = Effect.fnUntraced(function* (effect: Effect.Effect<void>) {\n const fiber = yield* FiberSet.run(requests, effect)\n const exit = yield* Fiber.await(fiber).pipe(Effect.onInterrupt(() => Fiber.interrupt(fiber)))\n\n if (Exit.isFailure(exit) && !Cause.hasInterruptsOnly(exit.cause)) {\n return yield* Effect.failCause(exit.cause)\n }\n }, Effect.orDie)\n\n const drain: Effect.Effect<void> = Semaphore.withPermit(turnsLock)(\n Effect.gen(function* () {\n while (!(yield* isDeleted) && History.needsTurn(yield* chat.entries)) {\n yield* Turns.run({ registration, handler, chat, services, checkOpen: Effect.asVoid(checkAvailable) })\n }\n })\n )\n\n const runner = yield* inWorking(\n Specialists.makeRunner({\n chat,\n handle,\n specialists,\n model: registration.model,\n tracer: registration.tracer,\n holding,\n report: Effect.fnUntraced(function* (finished) {\n yield* store(finished)\n yield* inBackground(drain)\n })\n })\n )\n\n const services = Context.make(CurrentChat, handle).pipe(\n Context.add(SpecialistService.Runner, runner),\n Context.merge(registration.model)\n )\n\n const remove = Effect.gen(function* () {\n // Under the write lock: an append in flight is stored before the `ChatDeleted`; none comes after.\n const first = yield* Semaphore.withPermit(writes)(\n Effect.gen(function* () {\n if (yield* isDeleted) return false\n\n yield* stored([AssistantEvent.ChatDeleted.make({})])\n\n return yield* Deferred.succeed(deleted, undefined)\n })\n ).pipe(Effect.uninterruptible)\n\n if (!first) return\n\n // Its own fiber: a tool that deletes its own chat is in the working scope, and closing it interrupts that tool.\n // Interrupted work keeps its hold (see `makeHolding`), so the chat lets go of it here.\n const closing = yield* Effect.forkDetach(\n Effect.gen(function* () {\n yield* Scope.close(working, Exit.void)\n yield* options.keepAlive(false)\n })\n )\n\n yield* Fiber.await(closing)\n })\n\n // A chat the app deleted while it was closed is deleted now, so its subscribers hear it.\n yield* checkAvailable\n\n const subscribe = (after: number): Stream.Stream<Item> =>\n Stream.unwrap(\n Effect.gen(function* () {\n // Subscribes before it reads, so nothing appended in between is missed.\n const subscription = yield* PubSub.subscribe(channel)\n const all = yield* chat.entries\n const stored = all.filter((entry) => entry.sequence > after)\n const last = stored.at(-1)?.sequence ?? after\n\n // Deleted: what the reader hasn't seen, then the end. Otherwise the stream ends with the `ChatDeleted`.\n if (all.some(isChatDeleted)) return Stream.fromIterable(stored)\n\n return Stream.concat(\n Stream.fromIterable<Item>(stored),\n Stream.fromSubscription(subscription).pipe(\n Stream.filter((item) => ChatSignal.isChatSignal(item) || item.sequence > last)\n )\n ).pipe(Stream.takeUntil(isChatDeleted))\n })\n )\n\n const chatApi: Chat = {\n submit: Effect.fnUntraced(function* (input) {\n yield* store(input)\n yield* inRequest(drain)\n }),\n submitInBackground: Effect.fnUntraced(function* (input) {\n yield* store(input)\n yield* inBackground(drain)\n }),\n resume: Effect.gen(function* () {\n if (History.needsTurn(yield* chat.entries)) yield* inBackground(drain)\n }),\n stream: (after) => subscribe(after).pipe(Stream.filter(isEntry)),\n live: subscribe,\n delete: remove\n }\n\n return chatApi\n})\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AA4BA,MAAM,QAAQ,UACZA,eAA8B,KAAK,IAC/BC,QAAuB,KAAK,IAC1B,MAAM,KACN,KAAA,IACF,UAAU,YAAY,OAAO,IAAI,KAAK,UAAU,SAAS,MAAM,EAAE,IAC/D,MAAM,KACN,KAAA;AAMR,MAAM,oBAAoB,OAAO,eAAe,OAAO,UAAgB,GAAG,OAAO,QAAQ;AAEzF,MAAM,cAAc,OAAO,WAAW,WAAW,WAAsD;CACrG,MAAM,QAAQ,OAAO,gBAAgB,KAAK,CAAC;CAE3C,MAAM,UAAU,gBAAgB,aAC9B,OACA,OAAO,WAAW,WAAW,GAAW;EACtC,IAAI,MAAM,GAAG,OAAO,IAAI;EAExB,OAAO,UAAU,IAAI;EAErB,OAAO;CACT,CAAC,CACH;CAEA,MAAM,UAAU,gBAAgB,aAC9B,OACA,OAAO,WAAW,WAAW,GAAW;EACtC,IAAI,MAAM,GAAG,OAAO,IAAI;EAExB,OAAO,UA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G;AACf,CAAC"}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { AnyEntry, AppEventSchema, AssistantEvent_d_exports, StoredAppEvent } from "../AssistantEvent.js";
|
|
2
2
|
import { Assistant_d_exports, Entry } from "../Assistant.js";
|
|
3
3
|
import * as Effect from "effect/Effect";
|
|
4
|
-
import * as Schema from "effect/Schema";
|
|
5
4
|
import * as Option from "effect/Option";
|
|
5
|
+
import * as Schema from "effect/Schema";
|
|
6
6
|
declare namespace events_d_exports {
|
|
7
7
|
export { AppEvents, DecodedEntries, decodeEntries, decodeEntry, decodeWith, encode, isRegistered, make, markInput, tagOf };
|
|
8
8
|
}
|
package/dist/internal/events.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { inputKey } from "./input.js";
|
|
2
2
|
import { StoredAppEvent, isLibraryEvent } from "../AssistantEvent.js";
|
|
3
3
|
import * as Effect from "effect/Effect";
|
|
4
|
+
import * as Option from "effect/Option";
|
|
4
5
|
import * as Predicate from "effect/Predicate";
|
|
5
6
|
import * as Schema from "effect/Schema";
|
|
6
|
-
import * as Option from "effect/Option";
|
|
7
7
|
import * as Record from "effect/Record";
|
|
8
8
|
import * as SchemaAST from "effect/SchemaAST";
|
|
9
9
|
//#region src/internal/events.ts
|
package/dist/internal/history.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { fromKey } from "./uuid.js";
|
|
2
2
|
import { isAppInput, isInput as isInput$1, isLibraryEvent, isTurnEnd, isUserInput, isWaking, textWithAttachments } from "../AssistantEvent.js";
|
|
3
3
|
import { toJson, toJsonText } from "./schema.js";
|
|
4
|
+
import * as Option from "effect/Option";
|
|
4
5
|
import * as Predicate from "effect/Predicate";
|
|
5
6
|
import * as Schema from "effect/Schema";
|
|
6
|
-
import * as Option from "effect/Option";
|
|
7
7
|
import * as Response from "effect/ai/Response";
|
|
8
8
|
import * as Prompt from "effect/ai/Prompt";
|
|
9
9
|
import * as Match from "effect/Match";
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { Status, TypingStarted, TypingStopped } from "../ChatSignal.js";
|
|
2
|
+
import * as Context from "effect/Context";
|
|
3
|
+
import * as Effect from "effect/Effect";
|
|
4
|
+
import * as Option from "effect/Option";
|
|
5
|
+
import * as Clock from "effect/Clock";
|
|
6
|
+
import * as SynchronizedRef from "effect/SynchronizedRef";
|
|
7
|
+
//#region src/internal/live.ts
|
|
8
|
+
/** @internal */
|
|
9
|
+
const TurnLive = Context.Reference("effect-assistant/TurnLive", { defaultValue: Option.none });
|
|
10
|
+
/** @internal */
|
|
11
|
+
const use = Effect.fnUntraced(function* (f) {
|
|
12
|
+
const live = yield* TurnLive;
|
|
13
|
+
if (Option.isSome(live)) yield* f(live.value);
|
|
14
|
+
});
|
|
15
|
+
/** @internal */
|
|
16
|
+
const make = Effect.fnUntraced(function* (send) {
|
|
17
|
+
const state = yield* SynchronizedRef.make({
|
|
18
|
+
running: [],
|
|
19
|
+
shown: void 0,
|
|
20
|
+
shownAt: yield* Clock.currentTimeMillis,
|
|
21
|
+
typing: false
|
|
22
|
+
});
|
|
23
|
+
const change = (f) => SynchronizedRef.updateEffect(state, Effect.fnUntraced(function* (current) {
|
|
24
|
+
const changed = f(current, yield* Clock.currentTimeMillis);
|
|
25
|
+
if (changed === void 0) return current;
|
|
26
|
+
const [signals, next] = changed;
|
|
27
|
+
yield* Effect.forEach(signals, send, { discard: true });
|
|
28
|
+
return next;
|
|
29
|
+
}));
|
|
30
|
+
const show = (current, text, now) => current.shown === text ? [[], current] : [[Status.make({ text })], {
|
|
31
|
+
...current,
|
|
32
|
+
shown: text,
|
|
33
|
+
shownAt: now
|
|
34
|
+
}];
|
|
35
|
+
return {
|
|
36
|
+
callStarted: (callId, text) => change((current, now) => {
|
|
37
|
+
const running = {
|
|
38
|
+
...current,
|
|
39
|
+
running: [...current.running, {
|
|
40
|
+
callId,
|
|
41
|
+
text
|
|
42
|
+
}]
|
|
43
|
+
};
|
|
44
|
+
return current.typing ? [[], running] : show(running, text, now);
|
|
45
|
+
}),
|
|
46
|
+
callEnded: (callId) => change((current, now) => {
|
|
47
|
+
const ended = current.running.find((call) => call.callId === callId);
|
|
48
|
+
if (ended === void 0) return void 0;
|
|
49
|
+
const next = {
|
|
50
|
+
...current,
|
|
51
|
+
running: current.running.filter((call) => call.callId !== callId)
|
|
52
|
+
};
|
|
53
|
+
const still = next.running.at(-1);
|
|
54
|
+
return still === void 0 || current.typing || current.shown !== ended.text ? [[], next] : show(next, still.text, now);
|
|
55
|
+
}),
|
|
56
|
+
summarized: (text) => change((current, now) => current.typing ? void 0 : current.shown === text ? [[], {
|
|
57
|
+
...current,
|
|
58
|
+
shownAt: now
|
|
59
|
+
}] : show(current, text, now)),
|
|
60
|
+
typing: (typing) => change((current, now) => {
|
|
61
|
+
if (current.typing === typing) return void 0;
|
|
62
|
+
if (typing) return [[TypingStarted.make({})], {
|
|
63
|
+
...current,
|
|
64
|
+
typing,
|
|
65
|
+
shown: void 0,
|
|
66
|
+
shownAt: now
|
|
67
|
+
}];
|
|
68
|
+
const stopped = {
|
|
69
|
+
...current,
|
|
70
|
+
typing,
|
|
71
|
+
shownAt: now
|
|
72
|
+
};
|
|
73
|
+
const still = current.running.at(-1);
|
|
74
|
+
const [signals, next] = still === void 0 ? [[], stopped] : show(stopped, still.text, now);
|
|
75
|
+
return [[TypingStopped.make({}), ...signals], next];
|
|
76
|
+
}),
|
|
77
|
+
state: SynchronizedRef.get(state),
|
|
78
|
+
reset: change((current) => current.typing ? [[TypingStopped.make({})], {
|
|
79
|
+
...current,
|
|
80
|
+
typing: false
|
|
81
|
+
}] : void 0)
|
|
82
|
+
};
|
|
83
|
+
});
|
|
84
|
+
//#endregion
|
|
85
|
+
export { TurnLive, make, use };
|
|
86
|
+
|
|
87
|
+
//# sourceMappingURL=live.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"live.js","names":[],"sources":["../../src/internal/live.ts"],"sourcesContent":["import * as Clock from \"effect/Clock\"\nimport * as Context from \"effect/Context\"\nimport * as Effect from \"effect/Effect\"\nimport * as Option from \"effect/Option\"\nimport * as SynchronizedRef from \"effect/SynchronizedRef\"\nimport * as ChatSignal from \"../ChatSignal.ts\"\n\ninterface Labelled {\n readonly callId: string\n readonly text: string\n}\n\n/** @internal */\nexport interface State {\n /** The described calls still running, oldest first. */\n readonly running: ReadonlyArray<Labelled>\n /** The line the user sees; none before the first status and after typing started. */\n readonly shown: string | undefined\n /** When the line last changed, or the turn started, in epoch milliseconds. */\n readonly shownAt: number\n readonly typing: boolean\n}\n\n/** @internal */\nexport interface Live {\n readonly callStarted: (callId: string, text: string) => Effect.Effect<void>\n readonly callEnded: (callId: string) => Effect.Effect<void>\n // A summary's line: shown unless the model started a message, or the line says it already.\n readonly summarized: (text: string) => Effect.Effect<void>\n readonly typing: (typing: boolean) => Effect.Effect<void>\n readonly state: Effect.Effect<State>\n // The turn ends: typing stops. Its end, in the log, clears the line.\n readonly reset: Effect.Effect<void>\n}\n\n// A turn's own: a specialist's run, or a loop outside a turn, shows nothing.\n/** @internal */\nexport const TurnLive = Context.Reference<Option.Option<Live>>(\"effect-assistant/TurnLive\", {\n defaultValue: Option.none\n})\n\n/** @internal */\nexport const use = Effect.fnUntraced(function* (f: (live: Live) => Effect.Effect<void>) {\n const live = yield* TurnLive\n\n if (Option.isSome(live)) yield* f(live.value)\n})\n\ntype Change = readonly [ReadonlyArray<ChatSignal.ChatSignal>, State] | undefined\n\n/** @internal */\nexport const make = Effect.fnUntraced(function* (send: (signal: ChatSignal.ChatSignal) => Effect.Effect<void>) {\n const state = yield* SynchronizedRef.make<State>({\n running: [],\n shown: undefined,\n shownAt: yield* Clock.currentTimeMillis,\n typing: false\n })\n\n // Under the ref's lock, so signals go out in the order the state changed.\n const change = (f: (current: State, now: number) => Change) =>\n SynchronizedRef.updateEffect(\n state,\n Effect.fnUntraced(function* (current) {\n const changed = f(current, yield* Clock.currentTimeMillis)\n\n if (changed === undefined) return current\n\n const [signals, next] = changed\n\n yield* Effect.forEach(signals, send, { discard: true })\n\n return next\n })\n )\n\n const show = (current: State, text: string, now: number): readonly [ReadonlyArray<ChatSignal.ChatSignal>, State] =>\n current.shown === text\n ? [[], current]\n : [[ChatSignal.Status.make({ text })], { ...current, shown: text, shownAt: now }]\n\n const live: Live = {\n callStarted: (callId, text) =>\n change((current, now) => {\n const running = { ...current, running: [...current.running, { callId, text }] }\n\n return current.typing ? [[], running] : show(running, text, now)\n }),\n // The line stays when the last labelled call ends; while another runs, the line says that one.\n callEnded: (callId) =>\n change((current, now) => {\n const ended = current.running.find((call) => call.callId === callId)\n\n if (ended === undefined) return undefined\n\n const next = { ...current, running: current.running.filter((call) => call.callId !== callId) }\n const still = next.running.at(-1)\n\n return still === undefined || current.typing || current.shown !== ended.text\n ? [[], next]\n : show(next, still.text, now)\n }),\n summarized: (text) =>\n change((current, now) =>\n current.typing\n ? undefined\n : current.shown === text\n ? [[], { ...current, shownAt: now }]\n : show(current, text, now)\n ),\n typing: (typing) =>\n change((current, now) => {\n if (current.typing === typing) return undefined\n\n if (typing) {\n return [[ChatSignal.TypingStarted.make({})], { ...current, typing, shown: undefined, shownAt: now }]\n }\n\n // A call that runs on after the message still says what it does.\n const stopped = { ...current, typing, shownAt: now }\n const still = current.running.at(-1)\n const [signals, next] = still === undefined ? [[], stopped] : show(stopped, still.text, now)\n\n return [[ChatSignal.TypingStopped.make({}), ...signals], next]\n }),\n state: SynchronizedRef.get(state),\n reset: change((current) =>\n current.typing ? [[ChatSignal.TypingStopped.make({})], { ...current, typing: false }] : undefined\n )\n }\n\n return live\n})\n"],"mappings":";;;;;;;;AAqCA,MAAa,WAAW,QAAQ,UAA+B,6BAA6B,EAC1F,cAAc,OAAO,KACvB,CAAC;;AAGD,MAAa,MAAM,OAAO,WAAW,WAAW,GAAwC;CACtF,MAAM,OAAO,OAAO;CAEpB,IAAI,OAAO,OAAO,IAAI,GAAG,OAAO,EAAE,KAAK,KAAK;AAC9C,CAAC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AAM,OACpE,CAAC,CAAC,GAAG,IAAI,IACT,KAAK,MAAM,MAAM,MAAM,GAAG;EAChC,CAAC;EACH,aAAa,SACX,QAAQ,SAAS,QACf,QAAQ,SACJ,KAAA,IACA,QAAQ,UAAU,OAChB,CAAC,CAAC,GAAG;GAAE,GAAG;GAAS,SAAS;EAAI,CAAC,IACjC,KAAK,SAAS,MAAM,GAAG,CAC/B;EACF,SAAS,WACP,QAAQ,SAAS,QAAQ;GACvB,IAAI,QAAQ,WAAW,QAAQ,OAAO,KAAA;GAEtC,IAAI,QACF,OAAO,CAAC,CAAA,cAA0B,KAAK,CAAC,CAAC,CAAC,GAAG;IAAE,GAAG;IAAS;IAAQ,OAAO,KAAA;IAAW,SAAS;GAAI,CAAC;GAIrG,MAAM,UAAU;IAAE,GAAG;IAAS;IAAQ,SAAS;GAAI;GACnD,MAAM,QAAQ,QAAQ,QAAQ,GAAG,EAAE;GACnC,MAAM,CAAC,SAAS,QAAQ,UAAU,KAAA,IAAY,CAAC,CAAC,GAAG,OAAO,IAAI,KAAK,SAAS,MAAM,MAAM,GAAG;GAE3F,OAAO,CAAC,CAAA,cAA0B,KAAK,CAAC,CAAC,GAAG,GAAG,OAAO,GAAG,IAAI;EAC/D,CAAC;EACH,OAAO,gBAAgB,IAAI,KAAK;EAChC,OAAO,QAAQ,YACb,QAAQ,SAAS,CAAC,CAAA,cAA0B,KAAK,CAAC,CAAC,CAAC,GAAG;GAAE,GAAG;GAAS,QAAQ;EAAM,CAAC,IAAI,KAAA,CAC1F;CAGQ;AACZ,CAAC"}
|
package/dist/internal/loop.d.ts
CHANGED
|
@@ -3,9 +3,9 @@ import { Instructions_d_exports, Resolved } from "../Instructions.js";
|
|
|
3
3
|
import { Tools, toolkit_d_exports } from "./toolkit.js";
|
|
4
4
|
import * as Context from "effect/Context";
|
|
5
5
|
import * as Effect from "effect/Effect";
|
|
6
|
+
import * as Option from "effect/Option";
|
|
6
7
|
import * as Stream from "effect/Stream";
|
|
7
8
|
import * as Data from "effect/Data";
|
|
8
|
-
import * as Option from "effect/Option";
|
|
9
9
|
import * as Prompt from "effect/ai/Prompt";
|
|
10
10
|
import * as LanguageModel from "effect/ai/LanguageModel";
|
|
11
11
|
import * as AiError from "effect/ai/AiError";
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"loop.d.ts","names":[],"sources":["../../src/internal/loop.ts"],"mappings":";;;;;;;;;;;;;;;
|
|
1
|
+
{"version":3,"file":"loop.d.ts","names":[],"sources":["../../src/internal/loop.ts"],"mappings":";;;;;;;;;;;;;;;YA2BY,QACR,cACA,cACA,aACA;;qBAKS,eAAa,QAAA,UAAA,OAAA;WACW,UAAA,OAAO,OAAO,cAAc;;;qBAMpD,WAAS,QAAA,UAAA,OAAA,OAAA;;iBAKL;WACN,UAAU,cAAc,OAAO;WAC/B;;;iBAUM,YAAY;WAClB,uBAAuB;WACvB,UAAU,MAAe;WACzB,SAAS,OAAO,OAAO,cAAc,OAAO,WAAW;WACvD,aAAa,cAAc;;;iBAIrB,QAAQ,mBAAmB,YAAY;WAC7C;;;iBAKM;;WAEN,QAAQ;;;;;;WAMR;;;;WAIA;;;YAmOC,KAAK,GAAG,KAAK,KAAK;EAC5B;aAAkB,OAAO;;EACzB;aAAiB,OAAO;;EACxB,MAAM;;;qBAUK,QAAS,GAAC,OAAS,MAAI,KAAK;;qBAG5B,OAAQ,GAAC,OAAS,MAAI,OAAO,OAAO,YAAY;;qBAIhD,OAAQ,gBAAc,OAAO,OAAO,YAAY;;qBAOhD,OAAQ,GAAG,GAAG,GAAG,GAAC,SACpB,GAAC,OACH,OAAO,MAAM,OAAO,OAAO,KAAK,GAAG,IAAI,GAAG,OAChD,OAAO,OAAO,GAAG,GAAG;;qBAmBV,OAAQ,GAAG,GAAG,YAAY,YAAU,SACtC,YAAY,IAAE,OAChB,QAAQ,eAAe,OAAO,OAAO,YAAY,IAAI,IAAI,QAC/D,OAAO,OAAO,KAAK,OAAO,IAAI,QAAQ,UAAU,IAAI,cAAc,gBAAgB,IAAI;;qBA8E5E,MAAO,WAAS,SAClB,QAAQ,OAChB,OAAO,OAAO,OAAO,QAAQ,SAAS,cAAc,gBAAgB"}
|
package/dist/internal/loop.js
CHANGED
|
@@ -1,19 +1,23 @@
|
|
|
1
1
|
import { ApprovalRequested, ModelOutput, ModelPart, ToolResult, ToolStarted } from "../AssistantEvent.js";
|
|
2
|
-
import {
|
|
2
|
+
import { collect } from "./stream.js";
|
|
3
3
|
import { annotateModelCall, toolOutput, toolSpan } from "./telemetry.js";
|
|
4
|
+
import { unansweredCalls } from "./history.js";
|
|
5
|
+
import { isDescribed, parameterDescription, parameterName } from "../ToolStatus.js";
|
|
6
|
+
import { use } from "./live.js";
|
|
4
7
|
import { empty, failed } from "./toolkit.js";
|
|
5
8
|
import { isTurnEnding, isUserMessage } from "./tools.js";
|
|
6
9
|
import * as Context from "effect/Context";
|
|
7
10
|
import * as Effect from "effect/Effect";
|
|
11
|
+
import * as Option from "effect/Option";
|
|
8
12
|
import * as Predicate from "effect/Predicate";
|
|
9
13
|
import * as Schema from "effect/Schema";
|
|
10
14
|
import * as Stream from "effect/Stream";
|
|
11
15
|
import * as Data from "effect/Data";
|
|
12
|
-
import * as Option from "effect/Option";
|
|
13
16
|
import * as Clock from "effect/Clock";
|
|
17
|
+
import * as Model from "effect/ai/Model";
|
|
14
18
|
import * as Prompt from "effect/ai/Prompt";
|
|
19
|
+
import * as SchemaAST from "effect/SchemaAST";
|
|
15
20
|
import * as LanguageModel from "effect/ai/LanguageModel";
|
|
16
|
-
import * as Model from "effect/ai/Model";
|
|
17
21
|
//#region src/internal/loop.ts
|
|
18
22
|
/** @internal */
|
|
19
23
|
const TurnApprovals = Context.Reference("effect-assistant/TurnApprovals", { defaultValue: Option.none });
|
|
@@ -26,26 +30,52 @@ const readHistory = (read) => "messages" in read ? {
|
|
|
26
30
|
messages: read,
|
|
27
31
|
throughSequence: void 0
|
|
28
32
|
};
|
|
33
|
+
const statusParameter = Schema.String.annotate({ description: parameterDescription }).pipe(Schema.optionalKey);
|
|
34
|
+
const withStatusParameter = (tool) => {
|
|
35
|
+
const ast = tool.parametersSchema.ast;
|
|
36
|
+
if (!isDescribed(tool) || !SchemaAST.isObjects(ast)) return tool;
|
|
37
|
+
if (ast.propertySignatures.some((property) => property.name === "description")) throw new Error(`${tool.name} has a parameter named ${parameterName} already, so it can't show a status: rename it.`);
|
|
38
|
+
const added = Schema.Struct({ [parameterName]: statusParameter }).ast.propertySignatures;
|
|
39
|
+
return tool.setParameters(Schema.make(new SchemaAST.Objects([...ast.propertySignatures, ...added], ast.indexSignatures.filter((index) => !SchemaAST.isNever(index.type)), ast.annotations)));
|
|
40
|
+
};
|
|
29
41
|
const requestToolkit = (definitions) => ({
|
|
30
|
-
tools: Object.fromEntries(definitions.map((tool) =>
|
|
42
|
+
tools: Object.fromEntries(definitions.map((tool) => {
|
|
43
|
+
const requested = withStatusParameter(tool);
|
|
44
|
+
return [tool.name, requested.needsApproval === void 0 || requested.needsApproval === false ? requested : requested.setNeedsApproval(false)];
|
|
45
|
+
})),
|
|
31
46
|
handle: () => Effect.succeed(Stream.empty)
|
|
32
47
|
});
|
|
33
|
-
const
|
|
48
|
+
const startsMessage = (part) => part.type === "text-start" || part.type === "tool-params-start" && part.name === "send_user_message";
|
|
49
|
+
const generate = (prompt, definitions, toolChoice) => Stream.suspend(() => LanguageModel.streamText({
|
|
34
50
|
prompt,
|
|
35
51
|
toolkit: requestToolkit(definitions),
|
|
36
52
|
toolChoice,
|
|
37
53
|
disableToolCallResolution: true,
|
|
38
54
|
toolCallValidation: "deferred"
|
|
39
|
-
});
|
|
55
|
+
})).pipe(Stream.tap((part) => startsMessage(part) ? use((live) => live.typing(true)) : Effect.void), Stream.runCollect, Effect.map(collect));
|
|
40
56
|
const decodePart = Schema.decodeUnknownOption(ModelPart);
|
|
41
57
|
const modelParts = (content) => content.flatMap((part) => Option.toArray(decodePart(part)).filter((decoded) => !(decoded.type === "tool-call" && decoded.providerExecuted)));
|
|
42
58
|
const toolCalls = (parts) => parts.flatMap((part) => part.type === "tool-call" ? [part] : []);
|
|
59
|
+
const decodeParams = Schema.decodeUnknownOption(Schema.Record(Schema.String, Schema.Json));
|
|
60
|
+
const isDescribedCall = (tools, call) => tools.definitions.some((tool) => tool.name === call.name && isDescribed(tool));
|
|
61
|
+
const splitStatus = (call) => {
|
|
62
|
+
const params = decodeParams(call.params);
|
|
63
|
+
if (Option.isNone(params) || !Object.hasOwn(params.value, "description")) return [void 0, call];
|
|
64
|
+
const { [parameterName]: written, ...rest } = params.value;
|
|
65
|
+
const status = Predicate.isString(written) ? written.trim() : "";
|
|
66
|
+
return [status === "" ? void 0 : status, {
|
|
67
|
+
...call,
|
|
68
|
+
params: rest
|
|
69
|
+
}];
|
|
70
|
+
};
|
|
43
71
|
const runCall = Effect.fnUntraced(function* (tools, call) {
|
|
72
|
+
const [status, run] = isDescribedCall(tools, call) ? splitStatus(call) : [void 0, call];
|
|
44
73
|
const result = yield* Effect.gen(function* () {
|
|
45
|
-
|
|
74
|
+
if (status !== void 0) yield* use((live) => live.callStarted(call.id, status));
|
|
75
|
+
const result = yield* tools.run(run);
|
|
46
76
|
yield* toolOutput(result.result);
|
|
47
77
|
return result;
|
|
48
|
-
}).pipe(toolSpan(call));
|
|
78
|
+
}).pipe(toolSpan(call), Effect.ensuring(use((live) => Effect.andThen(live.callEnded(call.id), call.name === "send_user_message" ? live.typing(false) : Effect.void))));
|
|
49
79
|
return ToolResult.make({
|
|
50
80
|
callId: call.id,
|
|
51
81
|
tool: call.name,
|
|
@@ -77,7 +107,7 @@ const runCalls = (tools, pending, history, then) => Stream.unwrap(Effect.gen(fun
|
|
|
77
107
|
}))]);
|
|
78
108
|
return Stream.fromIterable(started).pipe(Stream.concat(Stream.unwrap(Effect.gen(function* () {
|
|
79
109
|
const explained = pending.some(({ call }) => isUserMessage(call));
|
|
80
|
-
const done = yield* Effect.forEach(pending, ({ call }, i) => waits[i] !== true ? runCall(tools, call) : explained ? awaitApproval(call) : Effect.succeed(unexplained(call)), { concurrency: "unbounded" });
|
|
110
|
+
const done = yield* Effect.forEach(pending, ({ call }, i) => waits[i] !== true ? runCall(tools, call) : explained ? awaitApproval(call) : Effect.succeed(unexplained(call)), { concurrency: "unbounded" }).pipe(Effect.ensuring(use((live) => live.typing(false))));
|
|
81
111
|
return Stream.fromIterable(done.map(value)).pipe(Stream.concat(then({
|
|
82
112
|
failed: done.some(isFailure),
|
|
83
113
|
waiting: done.some((event) => Predicate.isTagged(event, "ApprovalRequested"))
|
|
@@ -131,14 +161,15 @@ const step = (options, then) => {
|
|
|
131
161
|
const prompt = Prompt.fromMessages([Prompt.systemMessage({ content: instructions.text }), ...history]);
|
|
132
162
|
const name = yield* Effect.serviceOption(Model.ModelName);
|
|
133
163
|
const started = yield* Clock.currentTimeMillis;
|
|
134
|
-
const
|
|
135
|
-
const metadata =
|
|
164
|
+
const content = yield* generate(prompt, toolkit.definitions, options.toolChoice).pipe(annotateModelCall(instructions.attributes));
|
|
165
|
+
const metadata = content.find((part) => part.type === "response-metadata");
|
|
166
|
+
const finish = content.find((part) => part.type === "finish");
|
|
136
167
|
const output = ModelOutput.make({
|
|
137
|
-
parts: modelParts(
|
|
168
|
+
parts: modelParts(content),
|
|
138
169
|
seenThroughSequence: throughSequence,
|
|
139
170
|
model: metadata?.modelId ?? Option.getOrUndefined(name),
|
|
140
171
|
responseId: metadata?.id,
|
|
141
|
-
usage:
|
|
172
|
+
usage: finish?.usage,
|
|
142
173
|
durationMillis: (yield* Clock.currentTimeMillis) - started
|
|
143
174
|
});
|
|
144
175
|
const calls = toolCalls(output.parts);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"loop.js","names":["AssistantEvent.ModelPart","Telemetry.toolOutput","Telemetry.toolSpan","Toolkits.failed","Toolkits.empty","History.unansweredCalls","Telemetry.annotateModelCall"],"sources":["../../src/internal/loop.ts"],"sourcesContent":["import type * as AiError from \"effect/ai/AiError\"\nimport * as LanguageModel from \"effect/ai/LanguageModel\"\nimport * as Model from \"effect/ai/Model\"\nimport * as Prompt from \"effect/ai/Prompt\"\nimport type * as Response from \"effect/ai/Response\"\nimport type * as Tool from \"effect/ai/Tool\"\nimport type * as Toolkit from \"effect/ai/Toolkit\"\nimport * as Clock from \"effect/Clock\"\nimport * as Context from \"effect/Context\"\nimport * as Data from \"effect/Data\"\nimport * as Effect from \"effect/Effect\"\nimport * as Option from \"effect/Option\"\nimport * as Predicate from \"effect/Predicate\"\nimport * as Schema from \"effect/Schema\"\nimport * as Stream from \"effect/Stream\"\nimport * as AssistantEvent from \"../AssistantEvent.ts\"\nimport type * as Instructions from \"../Instructions.ts\"\nimport * as History from \"./history.ts\"\nimport * as Telemetry from \"./telemetry.ts\"\nimport * as Toolkits from \"./toolkit.ts\"\nimport { isTurnEnding, isUserMessage } from \"./tools.ts\"\n\n/** @internal */\nexport type Event =\n | AssistantEvent.ModelOutput\n | AssistantEvent.ToolStarted\n | AssistantEvent.ToolResult\n | AssistantEvent.ApprovalRequested\n\n// Only a turn's loop can wait for the user's approval, and runs the calls they approved. A turn's specialists inherit\n// its context, so their runs set it back to none.\n/** @internal */\nexport const TurnApprovals = Context.Reference<\n Option.Option<{ readonly approved: Effect.Effect<ReadonlyArray<AssistantEvent.ToolCallPart>> }>\n>(\"effect-assistant/TurnApprovals\", { defaultValue: Option.none })\n\n// The assistant's own tools with the turn's services, so an app's own loop in a turn can message the user. A\n// specialist's run has none.\n/** @internal */\nexport const TurnTools = Context.Reference<Option.Option<Toolkits.Tools>>(\"effect-assistant/TurnTools\", {\n defaultValue: Option.none\n})\n\n/** @internal */\nexport interface ChatHistory {\n readonly messages: ReadonlyArray<Prompt.Message>\n readonly throughSequence: number\n}\n\nconst readHistory = (read: ReadonlyArray<Prompt.Message> | ChatHistory) =>\n \"messages\" in read\n ? { messages: read.messages, throughSequence: read.throughSequence }\n : { messages: read, throughSequence: undefined }\n\n// `AssistantLoop` takes native toolkits; turns and specialist runs come here with their tools already merged.\n/** @internal */\nexport interface StepOptions<R = never> {\n readonly instructions: string | Instructions.Resolved\n readonly toolkit?: Toolkits.Tools<R> | undefined\n readonly history: Effect.Effect<ReadonlyArray<Prompt.Message> | ChatHistory>\n readonly toolChoice?: LanguageModel.ToolChoice<string> | undefined\n}\n\n/** @internal */\nexport interface Options<R = never> extends StepOptions<R> {\n readonly maxSteps?: number | undefined\n}\n\n// Its member docs show through `AssistantLoop.StepResult`.\n/** @internal */\nexport interface StepResult {\n /** The model's output; unset when the step only ran calls a crash cut off. */\n readonly output: AssistantEvent.ModelOutput | undefined\n /**\n * Whether the model is done: it made no tool calls, one of its calls waits for the user's\n * approval, or it ended its turn (`wait`, or a message with `wait: true`) and none of the step's\n * calls failed.\n */\n readonly done: boolean\n /**\n * The history's length with this step's output and results: a longer one got something new meanwhile.\n */\n readonly historyLength: number\n}\n\n// A tool as `Tool.isUserDefined` narrows one. With `Tool.Any`, generateText's types ask for the services that encode\n// every tool's params, `unknown` for a schema of unknown type, though deferred validation encodes none.\ntype RequestTool = Tool.Tool<string, any, any>\n\n// The loop runs the calls itself, after storing each as started, so the model call runs none. It also decides which\n// calls wait for approval: the model call would ask for approvals of its own.\nconst requestToolkit = (definitions: ReadonlyArray<Tool.Any>): Toolkit.WithHandler<Record<string, RequestTool>> => ({\n tools: Object.fromEntries(\n definitions.map((tool) => [\n tool.name,\n tool.needsApproval === undefined || tool.needsApproval === false ? tool : tool.setNeedsApproval(false)\n ])\n ),\n handle: () => Effect.succeed(Stream.empty)\n})\n\n// Validation is deferred: a call to a tool that isn't in the toolkit (one in the history the turn no longer offers),\n// or with params that don't fit its schema, would otherwise fail the whole model call. The loop runs it instead, and\n// the model reads why it failed.\nconst generate = (\n prompt: Prompt.Prompt,\n definitions: ReadonlyArray<Tool.Any>,\n toolChoice: LanguageModel.ToolChoice<string> | undefined\n): Effect.Effect<\n LanguageModel.GenerateTextResponse<Record<string, RequestTool>, \"deferred\">,\n AiError.AiError,\n LanguageModel.LanguageModel\n> =>\n LanguageModel.generateText({\n prompt,\n toolkit: requestToolkit(definitions),\n toolChoice,\n disableToolCallResolution: true,\n toolCallValidation: \"deferred\"\n })\n\nconst decodePart = Schema.decodeUnknownOption(AssistantEvent.ModelPart)\n\nconst modelParts = (content: ReadonlyArray<Response.AllParts<Record<string, Tool.Any>>>) =>\n content.flatMap((part) =>\n Option.toArray(decodePart(part)).filter((decoded) => !(decoded.type === \"tool-call\" && decoded.providerExecuted))\n )\n\nconst toolCalls = (parts: ReadonlyArray<AssistantEvent.ModelPart>): ReadonlyArray<AssistantEvent.ToolCallPart> =>\n parts.flatMap((part) => (part.type === \"tool-call\" ? [part] : []))\n\nconst runCall = Effect.fnUntraced(function* <R>(tools: Toolkits.Tools<R>, call: AssistantEvent.ToolCallPart) {\n const result = yield* Effect.gen(function* () {\n const result = yield* tools.run(call)\n\n yield* Telemetry.toolOutput(result.result)\n\n return result\n }).pipe(Telemetry.toolSpan(call))\n\n return AssistantEvent.ToolResult.make({ callId: call.id, tool: call.name, ...result })\n})\n\n// A call that needs approval waits for the user in a turn. A specialist's run can't ask them, so its call fails.\nconst awaitApproval = Effect.fnUntraced(function* (call: AssistantEvent.ToolCallPart) {\n if (Option.isNone(yield* TurnApprovals)) {\n return AssistantEvent.ToolResult.make({\n callId: call.id,\n tool: call.name,\n ...Toolkits.failed(\n `${call.name} needs the user's approval, and only the assistant can ask for it. Don't run it: say in your answer what needs approving, with the details.`,\n \"NeedsApproval\"\n )\n })\n }\n\n return AssistantEvent.ApprovalRequested.make({ callId: call.id, tool: call.name, params: call.params })\n})\n\ninterface Pending {\n readonly call: AssistantEvent.ToolCallPart\n /** The user approved it: it runs without asking again. */\n readonly approved: boolean\n}\n\ninterface Ran {\n /** One of the calls failed, so the model has to read the results. */\n readonly failed: boolean\n /** One of the calls waits for the user's approval, so the turn ends. */\n readonly waiting: boolean\n}\n\n// The approval shows the call, not what it does: the user learns that from the assistant's message beside it.\nconst unexplained = (call: AssistantEvent.ToolCallPart) =>\n AssistantEvent.ToolResult.make({\n callId: call.id,\n tool: call.name,\n ...Toolkits.failed(\n `Not sent for approval: the user would see buttons without knowing what they approve. In one step, send them a message with send_user_message saying what ${call.name} will do (what, to whom, how many), and call ${call.name} again.`,\n \"ApprovalNeedsMessage\"\n )\n })\n\n// Every `ToolStarted` is stored before any call runs; a call that waits for approval doesn't start.\nconst runCalls = <R, S, E, R2>(\n tools: Toolkits.Tools<R>,\n pending: ReadonlyArray<Pending>,\n history: ReadonlyArray<Prompt.Message>,\n then: (ran: Ran) => Stream.Stream<Step<never, S>, E, R2>\n): Stream.Stream<Step<Event, S>, E, R | R2> =>\n Stream.unwrap(\n Effect.gen(function* () {\n const waits = yield* Effect.forEach(pending, ({ call, approved }) =>\n approved ? Effect.succeed(false) : tools.needsApproval(call, history)\n )\n\n const started = pending.flatMap(({ call }, i) =>\n waits[i] === true ? [] : [value(AssistantEvent.ToolStarted.make({ callId: call.id, tool: call.name }))]\n )\n\n return Stream.fromIterable(started).pipe(\n Stream.concat(\n Stream.unwrap(\n Effect.gen(function* () {\n const explained = pending.some(({ call }) => isUserMessage(call))\n\n const done = yield* Effect.forEach(\n pending,\n ({ call }, i): Effect.Effect<Event, never, R> =>\n waits[i] !== true\n ? runCall(tools, call)\n : explained\n ? awaitApproval(call)\n : Effect.succeed(unexplained(call)),\n { concurrency: \"unbounded\" }\n )\n\n return Stream.fromIterable(done.map(value)).pipe(\n Stream.concat(\n then({\n failed: done.some(isFailure),\n waiting: done.some((event) => Predicate.isTagged(event, \"ApprovalRequested\"))\n })\n )\n )\n })\n )\n )\n )\n })\n )\n\nconst isFailure = (event: Event) => Predicate.isTagged(event, \"ToolResult\") && event.outcome === \"failure\"\n\n/** @internal */\nexport type Step<A, S> = Data.TaggedEnum<{\n Value: { readonly value: A }\n Next: { readonly state: S }\n Stop: Record<never, never>\n}>\n\ninterface StepDefinition extends Data.TaggedEnum.WithGenerics<2> {\n readonly taggedEnum: Step<this[\"A\"], this[\"B\"]>\n}\n\nconst Steps = Data.taggedEnum<StepDefinition>()\n\n/** @internal */\nexport const value = <A>(value: A): Step<A, never> => Steps.Value({ value })\n\n/** @internal */\nexport const next = <S>(state: S): Stream.Stream<Step<never, S>> => Stream.succeed(Steps.Next({ state }))\n\n/** @internal */\n// oxlint-disable-next-line no-unnecessary-type-parameters -- `stop<number>()` types a loop's state where inference can't.\nexport const stop = <S = never>(): Stream.Stream<Step<never, S>> => Stream.succeed(Steps.Stop())\n\nconst isValue = <A, S>(step: Step<A, S>): step is Data.TaggedEnum.Value<Step<A, S>, \"Value\"> =>\n Predicate.isTagged(step, \"Value\")\n\n// A body that ends without `next` or `stop` stops too.\n/** @internal */\nexport const loop = <S, A, E, R>(\n initial: S,\n body: (state: S) => Stream.Stream<Step<A, S>, E, R>\n): Stream.Stream<A, E, R> => {\n const go = (state: S): Stream.Stream<A, E, R> =>\n Stream.suspend(() => {\n let after: Option.Option<S> = Option.none()\n\n return body(state).pipe(\n Stream.tap((step) =>\n Effect.sync(() => (after = Predicate.isTagged(step, \"Next\") ? Option.some(step.state) : after))\n ),\n Stream.filter(isValue),\n Stream.map((step) => step.value),\n Stream.concat(Stream.suspend(() => Option.match(after, { onNone: () => Stream.empty, onSome: go })))\n )\n })\n\n return go(initial)\n}\n\n/** @internal */\nexport const step = <R, S, E2 = never, R2 = never>(\n options: StepOptions<R>,\n then: (result: StepResult) => Stream.Stream<Step<never, S>, E2, R2>\n): Stream.Stream<Step<Event, S>, AiError.AiError | E2, LanguageModel.LanguageModel | R | R2> => {\n const toolkit = options.toolkit ?? Toolkits.empty\n const instructions = Predicate.isString(options.instructions) ? { text: options.instructions } : options.instructions\n\n const after = (result: StepResult) => Stream.suspend(() => then(result))\n\n return Stream.unwrap(\n Effect.gen(function* () {\n const { messages: history, throughSequence } = readHistory(yield* options.history)\n // Calls a crash cut off, with no result: they run again, with the same ids, before the model goes on.\n const cutOff = History.unansweredCalls(history)\n const approvals = yield* TurnApprovals\n\n // Calls the user approved: they run as the model made them, with the same ids, before the model goes on. Not in\n // a loop without their tools.\n const approved = Option.isNone(approvals)\n ? []\n : (yield* approvals.value.approved).filter(\n (call) =>\n toolkit.definitions.some((tool) => tool.name === call.name) && !cutOff.some((cut) => cut.id === call.id)\n )\n\n if (cutOff.length > 0 || approved.length > 0) {\n const pending = [\n ...cutOff.map((call) => ({ call, approved: false })),\n ...approved.map((call) => ({ call, approved: true }))\n ]\n\n return runCalls(toolkit, pending, history, ({ waiting }) =>\n after({ output: undefined, done: waiting, historyLength: history.length + pending.length })\n )\n }\n\n const prompt = Prompt.fromMessages([Prompt.systemMessage({ content: instructions.text }), ...history])\n const name = yield* Effect.serviceOption(Model.ModelName)\n const started = yield* Clock.currentTimeMillis\n\n const response = yield* generate(prompt, toolkit.definitions, options.toolChoice).pipe(\n Telemetry.annotateModelCall(instructions.attributes)\n )\n\n const metadata = response.content.find((part) => part.type === \"response-metadata\")\n\n const output = AssistantEvent.ModelOutput.make({\n parts: modelParts(response.content),\n seenThroughSequence: throughSequence,\n model: metadata?.modelId ?? Option.getOrUndefined(name),\n responseId: metadata?.id,\n usage: response.usage,\n durationMillis: (yield* Clock.currentTimeMillis) - started\n })\n\n const calls = toolCalls(output.parts)\n const historyLength = history.length + Prompt.fromResponseParts(output.parts).content.length + calls.length\n\n // A failed call doesn't end the turn: a message that failed never reached the user, so the model reads why. A call\n // that waits for approval does: the turn waits for the user.\n return Stream.make(value(output)).pipe(\n Stream.concat(\n runCalls(\n toolkit,\n calls.map((call) => ({ call, approved: false })),\n history,\n ({ failed, waiting }) =>\n after({\n output,\n done: calls.length === 0 || waiting || (!failed && calls.some(isTurnEnding)),\n historyLength\n })\n )\n )\n )\n })\n )\n}\n\n/** @internal */\nexport const run = <R = never>(\n options: Options<R>\n): Stream.Stream<Event, AiError.AiError, LanguageModel.LanguageModel | R> => {\n const maxSteps = options.maxSteps ?? 16\n\n // Done: stop, unless something arrived while the model was working; then it reads that first.\n const afterDone = (steps: number, historyLength: number) =>\n Stream.unwrap(\n Effect.gen(function* () {\n const read = yield* options.history\n\n return readHistory(read).messages.length > historyLength ? next(steps) : stop<number>()\n })\n )\n\n return loop(0, (steps: number) =>\n step(options, (result) => {\n const taken = result.output === undefined ? steps : steps + 1\n\n if (taken >= maxSteps) return stop<number>()\n\n return result.done ? afterDone(taken, result.historyLength) : next(taken)\n })\n )\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAgCA,MAAa,gBAAgB,QAAQ,UAEnC,kCAAkC,EAAE,cAAc,OAAO,KAAK,CAAC;;AAKjE,MAAa,YAAY,QAAQ,UAAyC,8BAA8B,EACtG,cAAc,OAAO,KACvB,CAAC;AAQD,MAAM,eAAe,SACnB,cAAc,OACV;CAAE,UAAU,KAAK;CAAU,iBAAiB,KAAK;AAAgB,IACjE;CAAE,UAAU;CAAM,iBAAiB,KAAA;AAAU;AAuCnD,MAAM,kBAAkB,iBAA4F;CAClH,OAAO,OAAO,YACZ,YAAY,KAAK,SAAS,CACxB,KAAK,MACL,KAAK,kBAAkB,KAAA,KAAa,KAAK,kBAAkB,QAAQ,OAAO,KAAK,iBAAiB,KAAK,CACvG,CAAC,CACH;CACA,cAAc,OAAO,QAAQ,OAAO,KAAK;AAC3C;AAKA,MAAM,YACJ,QACA,aACA,eAMA,cAAc,aAAa;CACzB;CACA,SAAS,eAAe,WAAW;CACnC;CACA,2BAA2B;CAC3B,oBAAoB;AACtB,CAAC;AAEH,MAAM,aAAa,OAAO,oBAAoBA,SAAwB;AAEtE,MAAM,cAAc,YAClB,QAAQ,SAAS,SACf,OAAO,QAAQ,WAAW,IAAI,CAAC,CAAC,CAAC,QAAQ,YAAY,EAAE,QAAQ,SAAS,eAAe,QAAQ,iBAAiB,CAClH;AAEF,MAAM,aAAa,UACjB,MAAM,SAAS,SAAU,KAAK,SAAS,cAAc,CAAC,IAAI,IAAI,CAAC,CAAE;AAEnE,MAAM,UAAU,OAAO,WAAW,WAAc,OAA0B,MAAmC;CAC3G,MAAM,SAAS,OAAO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streamed.collect","AssistantEvent.ModelPart","Telemetry.toolOutput","Telemetry.toolSpan","Toolkits.failed","Toolkits.empty","History.unansweredCalls","Telemetry.annotateModelCall"],"sources":["../../src/internal/loop.ts"],"sourcesContent":["import type * as AiError from \"effect/ai/AiError\"\nimport * as LanguageModel from \"effect/ai/LanguageModel\"\nimport * as Model from \"effect/ai/Model\"\nimport * as Prompt from \"effect/ai/Prompt\"\nimport type * as Response from \"effect/ai/Response\"\nimport type * as Tool from \"effect/ai/Tool\"\nimport type * as Toolkit from \"effect/ai/Toolkit\"\nimport * as Clock from \"effect/Clock\"\nimport * as Context from \"effect/Context\"\nimport * as Data from \"effect/Data\"\nimport * as Effect from \"effect/Effect\"\nimport * as Option from \"effect/Option\"\nimport * as Predicate from \"effect/Predicate\"\nimport * as Schema from \"effect/Schema\"\nimport * as SchemaAST from \"effect/SchemaAST\"\nimport * as Stream from \"effect/Stream\"\nimport * as AssistantEvent from \"../AssistantEvent.ts\"\nimport type * as Instructions from \"../Instructions.ts\"\nimport * as ToolStatus from \"../ToolStatus.ts\"\nimport * as History from \"./history.ts\"\nimport * as Live from \"./live.ts\"\nimport * as Streamed from \"./stream.ts\"\nimport * as Telemetry from \"./telemetry.ts\"\nimport * as Toolkits from \"./toolkit.ts\"\nimport { isTurnEnding, isUserMessage, sendUserMessageName } from \"./tools.ts\"\n\n/** @internal */\nexport type Event =\n | AssistantEvent.ModelOutput\n | AssistantEvent.ToolStarted\n | AssistantEvent.ToolResult\n | AssistantEvent.ApprovalRequested\n\n// Only a turn's loop can wait for the user's approval, and runs the calls they approved. A turn's specialists inherit\n// its context, so their runs set it back to none.\n/** @internal */\nexport const TurnApprovals = Context.Reference<\n Option.Option<{ readonly approved: Effect.Effect<ReadonlyArray<AssistantEvent.ToolCallPart>> }>\n>(\"effect-assistant/TurnApprovals\", { defaultValue: Option.none })\n\n// The assistant's own tools with the turn's services, so an app's own loop in a turn can message the user. A\n// specialist's run has none.\n/** @internal */\nexport const TurnTools = Context.Reference<Option.Option<Toolkits.Tools>>(\"effect-assistant/TurnTools\", {\n defaultValue: Option.none\n})\n\n/** @internal */\nexport interface ChatHistory {\n readonly messages: ReadonlyArray<Prompt.Message>\n readonly throughSequence: number\n}\n\nconst readHistory = (read: ReadonlyArray<Prompt.Message> | ChatHistory) =>\n \"messages\" in read\n ? { messages: read.messages, throughSequence: read.throughSequence }\n : { messages: read, throughSequence: undefined }\n\n// `AssistantLoop` takes native toolkits; turns and specialist runs come here with their tools already merged.\n/** @internal */\nexport interface StepOptions<R = never> {\n readonly instructions: string | Instructions.Resolved\n readonly toolkit?: Toolkits.Tools<R> | undefined\n readonly history: Effect.Effect<ReadonlyArray<Prompt.Message> | ChatHistory>\n readonly toolChoice?: LanguageModel.ToolChoice<string> | undefined\n}\n\n/** @internal */\nexport interface Options<R = never> extends StepOptions<R> {\n readonly maxSteps?: number | undefined\n}\n\n// Its member docs show through `AssistantLoop.StepResult`.\n/** @internal */\nexport interface StepResult {\n /** The model's output; unset when the step only ran calls a crash cut off. */\n readonly output: AssistantEvent.ModelOutput | undefined\n /**\n * Whether the model is done: it made no tool calls, one of its calls waits for the user's\n * approval, or it ended its turn (`wait`, or a message with `wait: true`) and none of the step's\n * calls failed.\n */\n readonly done: boolean\n /**\n * The history's length with this step's output and results: a longer one got something new meanwhile.\n */\n readonly historyLength: number\n}\n\n// A tool as `Tool.isUserDefined` narrows one. With `Tool.Any`, generateText's types ask for the services that encode\n// every tool's params, `unknown` for a schema of unknown type, though deferred validation encodes none.\ntype RequestTool = Tool.Tool<string, any, any>\n\nconst statusParameter = Schema.String.annotate({ description: ToolStatus.parameterDescription }).pipe(\n Schema.optionalKey\n)\n\n// What the model reads of a described tool: its parameters with the status one. Only the request has it; the call\n// runs without it (see `withoutStatus`). A record of no keys (a tool without parameters) becomes an object with it.\nconst withStatusParameter = (tool: Tool.Any): Tool.Any => {\n const ast = tool.parametersSchema.ast\n\n if (!ToolStatus.isDescribed(tool) || !SchemaAST.isObjects(ast)) return tool\n\n if (ast.propertySignatures.some((property) => property.name === ToolStatus.parameterName)) {\n throw new Error(\n `${tool.name} has a parameter named ${ToolStatus.parameterName} already, so it can't show a status: rename it.`\n )\n }\n\n const added = Schema.Struct({ [ToolStatus.parameterName]: statusParameter }).ast.propertySignatures\n\n return tool.setParameters(\n Schema.make<Schema.Top>(\n new SchemaAST.Objects(\n [...ast.propertySignatures, ...added],\n ast.indexSignatures.filter((index) => !SchemaAST.isNever(index.type)),\n ast.annotations\n )\n )\n )\n}\n\n// The loop runs the calls itself, after storing each as started, so the model call runs none. It also decides which\n// calls wait for approval: the model call would ask for approvals of its own.\nconst requestToolkit = (definitions: ReadonlyArray<Tool.Any>): Toolkit.WithHandler<Record<string, RequestTool>> => ({\n tools: Object.fromEntries(\n definitions.map((tool) => {\n const requested = withStatusParameter(tool)\n\n return [\n tool.name,\n requested.needsApproval === undefined || requested.needsApproval === false\n ? requested\n : requested.setNeedsApproval(false)\n ]\n })\n ),\n handle: () => Effect.succeed(Stream.empty)\n})\n\n// The model starts a message to the user: a `send_user_message` call, or text of its own, which the reply pass turns\n// into one.\nconst startsMessage = (part: Response.AnyPart) =>\n part.type === \"text-start\" || (part.type === \"tool-params-start\" && part.name === sendUserMessageName)\n\n// Validation is deferred: a call to a tool that isn't in the toolkit (one in the history the turn no longer offers),\n// or with params that don't fit its schema, would otherwise fail the whole model call. The loop runs it instead, and\n// the model reads why it failed.\n// Streamed, so the chat can show the user that a message is on its way while the model writes it.\nconst generate = (\n prompt: Prompt.Prompt,\n definitions: ReadonlyArray<Tool.Any>,\n toolChoice: LanguageModel.ToolChoice<string> | undefined\n): Effect.Effect<ReadonlyArray<Response.AnyPart>, AiError.AiError, LanguageModel.LanguageModel> =>\n Stream.suspend(() =>\n LanguageModel.streamText({\n prompt,\n toolkit: requestToolkit(definitions),\n toolChoice,\n disableToolCallResolution: true,\n toolCallValidation: \"deferred\"\n })\n ).pipe(\n Stream.tap((part) => (startsMessage(part) ? Live.use((live) => live.typing(true)) : Effect.void)),\n Stream.runCollect,\n Effect.map(Streamed.collect)\n )\n\nconst decodePart = Schema.decodeUnknownOption(AssistantEvent.ModelPart)\n\nconst modelParts = (content: ReadonlyArray<Response.AnyPart>) =>\n content.flatMap((part) =>\n Option.toArray(decodePart(part)).filter((decoded) => !(decoded.type === \"tool-call\" && decoded.providerExecuted))\n )\n\nconst toolCalls = (parts: ReadonlyArray<AssistantEvent.ModelPart>): ReadonlyArray<AssistantEvent.ToolCallPart> =>\n parts.flatMap((part) => (part.type === \"tool-call\" ? [part] : []))\n\nconst decodeParams = Schema.decodeUnknownOption(Schema.Record(Schema.String, Schema.Json))\n\nconst isDescribedCall = <R>(tools: Toolkits.Tools<R>, call: AssistantEvent.ToolCallPart) =>\n tools.definitions.some((tool) => tool.name === call.name && ToolStatus.isDescribed(tool))\n\n// The status the model wrote for a described tool's call, and the call as the tool's handler gets it.\nconst splitStatus = (call: AssistantEvent.ToolCallPart): readonly [string | undefined, AssistantEvent.ToolCallPart] => {\n const params = decodeParams(call.params)\n\n if (Option.isNone(params) || !Object.hasOwn(params.value, ToolStatus.parameterName)) return [undefined, call]\n\n const { [ToolStatus.parameterName]: written, ...rest } = params.value\n const status = Predicate.isString(written) ? written.trim() : \"\"\n\n return [status === \"\" ? undefined : status, { ...call, params: rest }]\n}\n\n// A call's status shows when it starts; a message's typing stops once the message is stored.\nconst runCall = Effect.fnUntraced(function* <R>(tools: Toolkits.Tools<R>, call: AssistantEvent.ToolCallPart) {\n const [status, run] = isDescribedCall(tools, call) ? splitStatus(call) : [undefined, call]\n\n const result = yield* Effect.gen(function* () {\n if (status !== undefined) yield* Live.use((live) => live.callStarted(call.id, status))\n\n const result = yield* tools.run(run)\n\n yield* Telemetry.toolOutput(result.result)\n\n return result\n }).pipe(\n Telemetry.toolSpan(call),\n Effect.ensuring(\n Live.use((live) =>\n Effect.andThen(live.callEnded(call.id), call.name === sendUserMessageName ? live.typing(false) : Effect.void)\n )\n )\n )\n\n return AssistantEvent.ToolResult.make({ callId: call.id, tool: call.name, ...result })\n})\n\n// A call that needs approval waits for the user in a turn. A specialist's run can't ask them, so its call fails.\nconst awaitApproval = Effect.fnUntraced(function* (call: AssistantEvent.ToolCallPart) {\n if (Option.isNone(yield* TurnApprovals)) {\n return AssistantEvent.ToolResult.make({\n callId: call.id,\n tool: call.name,\n ...Toolkits.failed(\n `${call.name} needs the user's approval, and only the assistant can ask for it. Don't run it: say in your answer what needs approving, with the details.`,\n \"NeedsApproval\"\n )\n })\n }\n\n return AssistantEvent.ApprovalRequested.make({ callId: call.id, tool: call.name, params: call.params })\n})\n\ninterface Pending {\n readonly call: AssistantEvent.ToolCallPart\n /** The user approved it: it runs without asking again. */\n readonly approved: boolean\n}\n\ninterface Ran {\n /** One of the calls failed, so the model has to read the results. */\n readonly failed: boolean\n /** One of the calls waits for the user's approval, so the turn ends. */\n readonly waiting: boolean\n}\n\n// The approval shows the call, not what it does: the user learns that from the assistant's message beside it.\nconst unexplained = (call: AssistantEvent.ToolCallPart) =>\n AssistantEvent.ToolResult.make({\n callId: call.id,\n tool: call.name,\n ...Toolkits.failed(\n `Not sent for approval: the user would see buttons without knowing what they approve. In one step, send them a message with send_user_message saying what ${call.name} will do (what, to whom, how many), and call ${call.name} again.`,\n \"ApprovalNeedsMessage\"\n )\n })\n\n// Every `ToolStarted` is stored before any call runs; a call that waits for approval doesn't start.\nconst runCalls = <R, S, E, R2>(\n tools: Toolkits.Tools<R>,\n pending: ReadonlyArray<Pending>,\n history: ReadonlyArray<Prompt.Message>,\n then: (ran: Ran) => Stream.Stream<Step<never, S>, E, R2>\n): Stream.Stream<Step<Event, S>, E, R | R2> =>\n Stream.unwrap(\n Effect.gen(function* () {\n const waits = yield* Effect.forEach(pending, ({ call, approved }) =>\n approved ? Effect.succeed(false) : tools.needsApproval(call, history)\n )\n\n const started = pending.flatMap(({ call }, i) =>\n waits[i] === true ? [] : [value(AssistantEvent.ToolStarted.make({ callId: call.id, tool: call.name }))]\n )\n\n return Stream.fromIterable(started).pipe(\n Stream.concat(\n Stream.unwrap(\n Effect.gen(function* () {\n const explained = pending.some(({ call }) => isUserMessage(call))\n\n // Text that became no message, or a message that failed, stops the typing too.\n const done = yield* Effect.forEach(\n pending,\n ({ call }, i): Effect.Effect<Event, never, R> =>\n waits[i] !== true\n ? runCall(tools, call)\n : explained\n ? awaitApproval(call)\n : Effect.succeed(unexplained(call)),\n { concurrency: \"unbounded\" }\n ).pipe(Effect.ensuring(Live.use((live) => live.typing(false))))\n\n return Stream.fromIterable(done.map(value)).pipe(\n Stream.concat(\n then({\n failed: done.some(isFailure),\n waiting: done.some((event) => Predicate.isTagged(event, \"ApprovalRequested\"))\n })\n )\n )\n })\n )\n )\n )\n })\n )\n\nconst isFailure = (event: Event) => Predicate.isTagged(event, \"ToolResult\") && event.outcome === \"failure\"\n\n/** @internal */\nexport type Step<A, S> = Data.TaggedEnum<{\n Value: { readonly value: A }\n Next: { readonly state: S }\n Stop: Record<never, never>\n}>\n\ninterface StepDefinition extends Data.TaggedEnum.WithGenerics<2> {\n readonly taggedEnum: Step<this[\"A\"], this[\"B\"]>\n}\n\nconst Steps = Data.taggedEnum<StepDefinition>()\n\n/** @internal */\nexport const value = <A>(value: A): Step<A, never> => Steps.Value({ value })\n\n/** @internal */\nexport const next = <S>(state: S): Stream.Stream<Step<never, S>> => Stream.succeed(Steps.Next({ state }))\n\n/** @internal */\n// oxlint-disable-next-line no-unnecessary-type-parameters -- `stop<number>()` types a loop's state where inference can't.\nexport const stop = <S = never>(): Stream.Stream<Step<never, S>> => Stream.succeed(Steps.Stop())\n\nconst isValue = <A, S>(step: Step<A, S>): step is Data.TaggedEnum.Value<Step<A, S>, \"Value\"> =>\n Predicate.isTagged(step, \"Value\")\n\n// A body that ends without `next` or `stop` stops too.\n/** @internal */\nexport const loop = <S, A, E, R>(\n initial: S,\n body: (state: S) => Stream.Stream<Step<A, S>, E, R>\n): Stream.Stream<A, E, R> => {\n const go = (state: S): Stream.Stream<A, E, R> =>\n Stream.suspend(() => {\n let after: Option.Option<S> = Option.none()\n\n return body(state).pipe(\n Stream.tap((step) =>\n Effect.sync(() => (after = Predicate.isTagged(step, \"Next\") ? Option.some(step.state) : after))\n ),\n Stream.filter(isValue),\n Stream.map((step) => step.value),\n Stream.concat(Stream.suspend(() => Option.match(after, { onNone: () => Stream.empty, onSome: go })))\n )\n })\n\n return go(initial)\n}\n\n/** @internal */\nexport const step = <R, S, E2 = never, R2 = never>(\n options: StepOptions<R>,\n then: (result: StepResult) => Stream.Stream<Step<never, S>, E2, R2>\n): Stream.Stream<Step<Event, S>, AiError.AiError | E2, LanguageModel.LanguageModel | R | R2> => {\n const toolkit = options.toolkit ?? Toolkits.empty\n const instructions = Predicate.isString(options.instructions) ? { text: options.instructions } : options.instructions\n\n const after = (result: StepResult) => Stream.suspend(() => then(result))\n\n return Stream.unwrap(\n Effect.gen(function* () {\n const { messages: history, throughSequence } = readHistory(yield* options.history)\n // Calls a crash cut off, with no result: they run again, with the same ids, before the model goes on.\n const cutOff = History.unansweredCalls(history)\n const approvals = yield* TurnApprovals\n\n // Calls the user approved: they run as the model made them, with the same ids, before the model goes on. Not in\n // a loop without their tools.\n const approved = Option.isNone(approvals)\n ? []\n : (yield* approvals.value.approved).filter(\n (call) =>\n toolkit.definitions.some((tool) => tool.name === call.name) && !cutOff.some((cut) => cut.id === call.id)\n )\n\n if (cutOff.length > 0 || approved.length > 0) {\n const pending = [\n ...cutOff.map((call) => ({ call, approved: false })),\n ...approved.map((call) => ({ call, approved: true }))\n ]\n\n return runCalls(toolkit, pending, history, ({ waiting }) =>\n after({ output: undefined, done: waiting, historyLength: history.length + pending.length })\n )\n }\n\n const prompt = Prompt.fromMessages([Prompt.systemMessage({ content: instructions.text }), ...history])\n const name = yield* Effect.serviceOption(Model.ModelName)\n const started = yield* Clock.currentTimeMillis\n\n const content = yield* generate(prompt, toolkit.definitions, options.toolChoice).pipe(\n Telemetry.annotateModelCall(instructions.attributes)\n )\n\n const metadata = content.find((part) => part.type === \"response-metadata\")\n const finish = content.find((part) => part.type === \"finish\")\n\n const output = AssistantEvent.ModelOutput.make({\n parts: modelParts(content),\n seenThroughSequence: throughSequence,\n model: metadata?.modelId ?? Option.getOrUndefined(name),\n responseId: metadata?.id,\n usage: finish?.usage,\n durationMillis: (yield* Clock.currentTimeMillis) - started\n })\n\n const calls = toolCalls(output.parts)\n const historyLength = history.length + Prompt.fromResponseParts(output.parts).content.length + calls.length\n\n // A failed call doesn't end the turn: a message that failed never reached the user, so the model reads why. A call\n // that waits for approval does: the turn waits for the user.\n return Stream.make(value(output)).pipe(\n Stream.concat(\n runCalls(\n toolkit,\n calls.map((call) => ({ call, approved: false })),\n history,\n ({ failed, waiting }) =>\n after({\n output,\n done: calls.length === 0 || waiting || (!failed && calls.some(isTurnEnding)),\n historyLength\n })\n )\n )\n )\n })\n )\n}\n\n/** @internal */\nexport const run = <R = never>(\n options: Options<R>\n): Stream.Stream<Event, AiError.AiError, LanguageModel.LanguageModel | R> => {\n const maxSteps = options.maxSteps ?? 16\n\n // Done: stop, unless something arrived while the model was working; then it reads that first.\n const afterDone = (steps: number, historyLength: number) =>\n Stream.unwrap(\n Effect.gen(function* () {\n const read = yield* options.history\n\n return readHistory(read).messages.length > historyLength ? next(steps) : stop<number>()\n })\n )\n\n return loop(0, (steps: number) =>\n step(options, (result) => {\n const taken = result.output === undefined ? steps : steps + 1\n\n if (taken >= maxSteps) return stop<number>()\n\n return result.done ? afterDone(taken, result.historyLength) : next(taken)\n })\n )\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;AAoCA,MAAa,gBAAgB,QAAQ,UAEnC,kCAAkC,EAAE,cAAc,OAAO,KAAK,CAAC;;AAKjE,MAAa,YAAY,QAAQ,UAAyC,8BAA8B,EACtG,cAAc,OAAO,KACvB,CAAC;AAQD,MAAM,eAAe,SACnB,cAAc,OACV;CAAE,UAAU,KAAK;CAAU,iBAAiB,KAAK;AAAgB,IACjE;CAAE,UAAU;CAAM,iBAAiB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AAa;EAE3C,OAAO,OAAO,OAAO,UAAU,OAAO,OAAO,aAAa,IAAI,KAAK,KAAK;CAC1E,CAAC,CACH;AACF"}
|
package/dist/internal/schema.js
CHANGED
|
@@ -2,19 +2,20 @@ import { fromKey } from "../InputId.js";
|
|
|
2
2
|
import { RunId, make } from "../RunId.js";
|
|
3
3
|
import { SpecialistFinished, SpecialistStarted } from "../AssistantEvent.js";
|
|
4
4
|
import { CurrentChat } from "../CurrentChat.js";
|
|
5
|
+
import { agentOutput, agentSpan, onTracer } from "./telemetry.js";
|
|
5
6
|
import { Brief, Runner, SpecialistError } from "../Specialist.js";
|
|
6
7
|
import { toolMessage, userText } from "./history.js";
|
|
7
|
-
import {
|
|
8
|
+
import { TurnLive } from "./live.js";
|
|
8
9
|
import { ToolFailure, fail } from "../ToolFailure.js";
|
|
9
10
|
import { empty, fromToolkitOrEmpty, fromWithHandler } from "./toolkit.js";
|
|
10
11
|
import { TurnApprovals, TurnTools, run } from "./loop.js";
|
|
11
12
|
import * as Context from "effect/Context";
|
|
12
13
|
import * as Effect from "effect/Effect";
|
|
14
|
+
import * as Option from "effect/Option";
|
|
13
15
|
import * as Predicate from "effect/Predicate";
|
|
14
16
|
import * as Schema from "effect/Schema";
|
|
15
17
|
import * as Stream from "effect/Stream";
|
|
16
18
|
import * as Exit from "effect/Exit";
|
|
17
|
-
import * as Option from "effect/Option";
|
|
18
19
|
import * as Ref from "effect/Ref";
|
|
19
20
|
import * as Cause from "effect/Cause";
|
|
20
21
|
import * as Fiber from "effect/Fiber";
|
|
@@ -169,7 +170,7 @@ const makeRunner = Effect.fnUntraced(function* (options) {
|
|
|
169
170
|
history: run.history,
|
|
170
171
|
inbox: run.inbox,
|
|
171
172
|
chatId: chat.chatId
|
|
172
|
-
}).pipe(Effect.provideContext(Context.merge(options.model, services)), Effect.provideService(TurnTools, Option.none()), Effect.matchCause({
|
|
173
|
+
}).pipe(onTracer(options.tracer), Effect.provideContext(Context.merge(options.model, services)), Effect.provideService(TurnTools, Option.none()), Effect.provideService(TurnLive, Option.none()), Effect.matchCause({
|
|
173
174
|
onSuccess: (answer) => ({
|
|
174
175
|
status: "answered",
|
|
175
176
|
report: answer
|