effect-assistant 0.2.0 → 0.4.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 +77 -37
- package/dist/Assistant.d.ts.map +1 -1
- package/dist/Assistant.js +11 -4
- package/dist/Assistant.js.map +1 -1
- package/dist/AssistantActivity.d.ts +10 -16
- package/dist/AssistantActivity.d.ts.map +1 -1
- package/dist/AssistantActivity.js +11 -4
- package/dist/AssistantActivity.js.map +1 -1
- package/dist/AssistantEngine.d.ts +39 -1
- package/dist/AssistantEngine.d.ts.map +1 -1
- package/dist/AssistantEngine.js +81 -28
- package/dist/AssistantEngine.js.map +1 -1
- package/dist/AssistantEvent.d.ts +104 -11
- package/dist/AssistantEvent.d.ts.map +1 -1
- package/dist/AssistantEvent.js +88 -13
- package/dist/AssistantEvent.js.map +1 -1
- package/dist/AssistantLoop.d.ts +4 -2
- package/dist/AssistantLoop.d.ts.map +1 -1
- package/dist/AssistantLoop.js +4 -2
- package/dist/AssistantLoop.js.map +1 -1
- package/dist/BlockKit.d.ts +4 -4
- package/dist/BlockKit.js +1 -1
- package/dist/BlockKit.js.map +1 -1
- package/dist/ChatLog.js +1 -1
- package/dist/Compaction.d.ts +7 -3
- package/dist/Compaction.d.ts.map +1 -1
- package/dist/Compaction.js +19 -8
- package/dist/Compaction.js.map +1 -1
- package/dist/CurrentChat.d.ts +2 -1
- package/dist/CurrentChat.d.ts.map +1 -1
- package/dist/CurrentChat.js.map +1 -1
- package/dist/Instructions.js +1 -1
- package/dist/Skill.d.ts +86 -11
- package/dist/Skill.d.ts.map +1 -1
- package/dist/Skill.js +93 -18
- package/dist/Skill.js.map +1 -1
- package/dist/Specialist.d.ts +7 -7
- package/dist/Specialist.d.ts.map +1 -1
- package/dist/Specialist.js.map +1 -1
- package/dist/Timeline.d.ts +516 -0
- package/dist/Timeline.d.ts.map +1 -0
- package/dist/Timeline.js +631 -0
- package/dist/Timeline.js.map +1 -0
- package/dist/ToolApproval.d.ts +146 -0
- package/dist/ToolApproval.d.ts.map +1 -0
- package/dist/ToolApproval.js +201 -0
- package/dist/ToolApproval.js.map +1 -0
- package/dist/ToolDiscovery.d.ts +6 -4
- package/dist/ToolDiscovery.d.ts.map +1 -1
- package/dist/ToolDiscovery.js +5 -4
- package/dist/ToolDiscovery.js.map +1 -1
- package/dist/index.d.ts +4 -4
- package/dist/index.js +4 -4
- package/dist/internal/chat.js +9 -7
- package/dist/internal/chat.js.map +1 -1
- package/dist/internal/events.d.ts +15 -16
- package/dist/internal/events.d.ts.map +1 -1
- package/dist/internal/events.js +29 -28
- package/dist/internal/events.js.map +1 -1
- package/dist/internal/history.js +53 -22
- package/dist/internal/history.js.map +1 -1
- package/dist/internal/input.js +7 -0
- package/dist/internal/input.js.map +1 -0
- package/dist/internal/loop.d.ts +10 -5
- package/dist/internal/loop.d.ts.map +1 -1
- package/dist/internal/loop.js +55 -22
- package/dist/internal/loop.js.map +1 -1
- package/dist/internal/specialists.js +10 -16
- package/dist/internal/specialists.js.map +1 -1
- package/dist/internal/toolkit.d.ts +5 -7
- package/dist/internal/toolkit.d.ts.map +1 -1
- package/dist/internal/toolkit.js +28 -18
- package/dist/internal/toolkit.js.map +1 -1
- package/dist/internal/tools.js +1 -1
- package/dist/internal/turns.d.ts.map +1 -1
- package/dist/internal/turns.js +35 -42
- package/dist/internal/turns.js.map +1 -1
- package/package.json +1 -1
- package/src/Assistant.ts +80 -40
- package/src/AssistantActivity.ts +11 -18
- package/src/AssistantEngine.ts +110 -43
- package/src/AssistantEvent.ts +106 -14
- package/src/AssistantLoop.ts +4 -2
- package/src/BlockKit.ts +6 -6
- package/src/Compaction.ts +25 -4
- package/src/CurrentChat.ts +2 -1
- package/src/Skill.ts +133 -23
- package/src/Specialist.ts +9 -9
- package/src/Timeline.ts +947 -0
- package/src/ToolApproval.ts +249 -0
- package/src/ToolDiscovery.ts +8 -3
- package/src/index.ts +10 -10
- package/src/internal/chat.ts +15 -5
- package/src/internal/events.ts +43 -43
- package/src/internal/history.ts +94 -32
- package/src/internal/input.ts +3 -0
- package/src/internal/loop.ts +120 -34
- package/src/internal/specialists.ts +7 -12
- package/src/internal/toolkit.ts +47 -30
- package/src/internal/turns.ts +52 -58
- package/dist/AppEvent.d.ts +0 -111
- package/dist/AppEvent.d.ts.map +0 -1
- package/dist/AppEvent.js +0 -71
- package/dist/AppEvent.js.map +0 -1
- package/dist/ChatView.d.ts +0 -408
- package/dist/ChatView.d.ts.map +0 -1
- package/dist/ChatView.js +0 -443
- package/dist/ChatView.js.map +0 -1
- package/dist/internal/skills.js +0 -28
- package/dist/internal/skills.js.map +0 -1
- package/src/AppEvent.ts +0 -134
- package/src/ChatView.ts +0 -587
- package/src/internal/skills.ts +0 -37
package/dist/Assistant.d.ts
CHANGED
|
@@ -1,12 +1,10 @@
|
|
|
1
|
-
import { Any as Any$1, AppEvent_d_exports, Type } from "./AppEvent.js";
|
|
2
1
|
import { ChatId } from "./ChatId.js";
|
|
3
|
-
import { AssistantEvent, AssistantEvent_d_exports, Input } from "./AssistantEvent.js";
|
|
2
|
+
import { AppEventSchema, AssistantEvent, AssistantEvent_d_exports, Input } from "./AssistantEvent.js";
|
|
4
3
|
import { Instructions_d_exports, Resolved } from "./Instructions.js";
|
|
5
4
|
import { AssistantLoop_d_exports, ChatHistory, Event, ToolkitServices } from "./AssistantLoop.js";
|
|
6
5
|
import { CurrentChat } from "./CurrentChat.js";
|
|
7
6
|
import { BlockKit, BlockKit_d_exports } from "./BlockKit.js";
|
|
8
|
-
import {
|
|
9
|
-
import { Any as Any$2, CheckServices, HandlerOf, Runner, Specialist_d_exports, ToolHandlers } from "./Specialist.js";
|
|
7
|
+
import { Any as Any$1, CheckServices, HandlerOf, Runner, Specialist_d_exports, ToolHandlers } from "./Specialist.js";
|
|
10
8
|
import { AssistantEngine } from "./AssistantEngine.js";
|
|
11
9
|
import { Chats_d_exports, GetChatError } from "./Chats.js";
|
|
12
10
|
import * as Context from "effect/Context";
|
|
@@ -38,27 +36,24 @@ export type TurnServices = CurrentChat | Runner;
|
|
|
38
36
|
* @category models
|
|
39
37
|
* @since 0.1.0
|
|
40
38
|
*/
|
|
41
|
-
export interface LoopOptions<T extends Record<string, Tool.Any> = Record<never, never>,
|
|
39
|
+
export interface LoopOptions<T extends Record<string, Tool.Any> = Record<never, never>, Events = never> {
|
|
42
40
|
/** The assistant's role and rules. The loop places `Instructions.protocol` before them. */
|
|
43
41
|
readonly instructions: string | Resolved;
|
|
44
42
|
/**
|
|
45
43
|
* Tools the model can use in addition to the assistant's own. This is an `effect/ai` toolkit,
|
|
46
44
|
* and its handlers come from the layer. Combine several toolkits with `Toolkit.merge`, for
|
|
47
|
-
* example `Memory.Tools` or a `ToolDiscovery.make(...)` toolkit together
|
|
45
|
+
* example `Memory.Tools`, a `Skill.toolkit(...)`, or a `ToolDiscovery.make(...)` toolkit together
|
|
46
|
+
* with your own.
|
|
48
47
|
*/
|
|
49
48
|
readonly toolkit?: Toolkit.Toolkit<T> | undefined;
|
|
50
|
-
/**
|
|
51
|
-
* Playbooks the model can follow. The prompt lists them as a catalog, and the model loads one
|
|
52
|
-
* with `load_skill` when a task matches it.
|
|
53
|
-
*/
|
|
54
|
-
readonly skills?: ReadonlyArray<Skill<RS>> | undefined;
|
|
55
49
|
/** The maximum number of model calls per turn. Defaults to 16. */
|
|
56
50
|
readonly maxSteps?: number | undefined;
|
|
57
51
|
/** The message the reply pass gives the model when the turn would end without a reply. */
|
|
58
52
|
readonly replyNudge?: string | undefined;
|
|
59
53
|
/**
|
|
60
54
|
* Selects which of the chat's entries the model reads, for example only those since the last
|
|
61
|
-
* summary. Defaults to all
|
|
55
|
+
* summary, and maps the app's own events it should read into prompt messages. Defaults to all
|
|
56
|
+
* entries, without the app's events.
|
|
62
57
|
*/
|
|
63
58
|
readonly history?: HistoryView<Events> | undefined;
|
|
64
59
|
}
|
|
@@ -68,9 +63,43 @@ export interface LoopOptions<T extends Record<string, Tool.Any> = Record<never,
|
|
|
68
63
|
*
|
|
69
64
|
* **Details**
|
|
70
65
|
*
|
|
71
|
-
* The function receives the chat's entries
|
|
72
|
-
* `Prompt.Message`s. The model reads the returned entries the same way
|
|
73
|
-
* and reads each prompt message unchanged.
|
|
66
|
+
* The function receives the chat's entries, the app's own events among them, and returns, in
|
|
67
|
+
* order, a mix of entries and `Prompt.Message`s. The model reads the returned entries the same way
|
|
68
|
+
* it reads the full history, and reads each prompt message unchanged, where the view put it. The
|
|
69
|
+
* model reads none of the app's events by itself: map those it should read to a message, and an
|
|
70
|
+
* app event's entry left in the result is dropped.
|
|
71
|
+
*
|
|
72
|
+
* A message stands right after the entry before it: one that came after the model's last read, or
|
|
73
|
+
* between a tool call and its result, moves after them, as an input does.
|
|
74
|
+
*
|
|
75
|
+
* **Example** (Telling the model about an order the user placed)
|
|
76
|
+
*
|
|
77
|
+
* ```ts
|
|
78
|
+
* import { Assistant, InputId } from "effect-assistant"
|
|
79
|
+
* import * as Prompt from "effect/ai/Prompt"
|
|
80
|
+
* import * as Schema from "effect/Schema"
|
|
81
|
+
*
|
|
82
|
+
* class OrderPlaced extends Schema.TaggedClass<OrderPlaced>()("OrderPlaced", {
|
|
83
|
+
* id: InputId.InputId,
|
|
84
|
+
* reference: Schema.String
|
|
85
|
+
* }) {}
|
|
86
|
+
*
|
|
87
|
+
* const MyAssistant = Assistant.make("my-assistant", { events: [OrderPlaced] })
|
|
88
|
+
*
|
|
89
|
+
* const withOrders: Assistant.HistoryView<OrderPlaced> = (entries) =>
|
|
90
|
+
* entries.map((entry) =>
|
|
91
|
+
* entry.event instanceof OrderPlaced
|
|
92
|
+
* ? Prompt.userMessage({
|
|
93
|
+
* content: [Prompt.textPart({ text: `The user placed order ${entry.event.reference}.` })]
|
|
94
|
+
* })
|
|
95
|
+
* : entry
|
|
96
|
+
* )
|
|
97
|
+
*
|
|
98
|
+
* // An `OrderPlaced` that the API submits wakes a turn, which reads it as this note.
|
|
99
|
+
* const MyAssistantLive = MyAssistant.toLayer((turn) =>
|
|
100
|
+
* turn.loop({ instructions: "Be brief.", history: withOrders })
|
|
101
|
+
* )
|
|
102
|
+
* ```
|
|
74
103
|
*
|
|
75
104
|
* @category models
|
|
76
105
|
* @since 0.1.0
|
|
@@ -130,10 +159,10 @@ export interface Turn<Events = never> {
|
|
|
130
159
|
readonly instructions: (instructions: string | Resolved) => Resolved;
|
|
131
160
|
/**
|
|
132
161
|
* Runs the agent loop over the chat. The loop uses `Instructions.protocol`, the assistant's own
|
|
133
|
-
* tools, and the instructions
|
|
162
|
+
* tools, and the instructions and tools in `options`. Every step is a tool call, and
|
|
134
163
|
* the loop ends with `replyIfOwed`.
|
|
135
164
|
*/
|
|
136
|
-
readonly loop: <T extends Record<string, Tool.Any> = Record<never, never
|
|
165
|
+
readonly loop: <T extends Record<string, Tool.Any> = Record<never, never>>(options: LoopOptions<T, Events>) => Stream.Stream<Event, AiError.AiError, LanguageModel.LanguageModel | TurnServices | ToolkitServices<T>>;
|
|
137
166
|
/**
|
|
138
167
|
* Runs one more pass when the turn would otherwise end without a reply to the user's latest
|
|
139
168
|
* message. In this pass the model can only reply.
|
|
@@ -203,8 +232,8 @@ export interface Chat<Events = never> {
|
|
|
203
232
|
readonly entries: Effect.Effect<ReadonlyArray<Entry<Events>>>;
|
|
204
233
|
/**
|
|
205
234
|
* Appends events after the chat's last entry and sends them to the chat's subscribers. The
|
|
206
|
-
* events can be the library's or the assistant's own, such as a summary.
|
|
207
|
-
* turn
|
|
235
|
+
* events can be the library's or the assistant's own, such as a summary. None of them wakes a
|
|
236
|
+
* turn: only what comes in through the assistant's `submit` does.
|
|
208
237
|
*/
|
|
209
238
|
append(...events: ReadonlyArray<AssistantEvent | Events>): Effect.Effect<void>;
|
|
210
239
|
}
|
|
@@ -219,8 +248,11 @@ export interface Chat<Events = never> {
|
|
|
219
248
|
export interface Service<Events = never> {
|
|
220
249
|
/**
|
|
221
250
|
* Stores an input or one of the assistant's own events, then returns. Examples are a user's
|
|
222
|
-
* message or block answer, a workflow's progress, or the
|
|
223
|
-
*
|
|
251
|
+
* message or block answer, a workflow's progress, or the order the user placed. Each one is an
|
|
252
|
+
* input: storing it wakes a turn, which the assistant runs in the background. The handler decides
|
|
253
|
+
* what the turn does with it, and an app event the model should read needs a history view that
|
|
254
|
+
* maps it (see {@link HistoryView}). To store an app event without waking a turn, append it from
|
|
255
|
+
* within the chat instead (`yield* MyAssistant.Chat`).
|
|
224
256
|
*
|
|
225
257
|
* Fails with `GetChatError` when the app's `Chats` service reports that the chat can't be used,
|
|
226
258
|
* because the app doesn't know the chat or has deleted it.
|
|
@@ -231,9 +263,10 @@ export interface Service<Events = never> {
|
|
|
231
263
|
* A reader that already has the earlier entries passes `after` and receives only the entries
|
|
232
264
|
* after that `seq`.
|
|
233
265
|
*
|
|
234
|
-
* For a chat client, pipe the whole log into `
|
|
235
|
-
* streams the chat as its user sees it. Its `after` goes there, not here:
|
|
236
|
-
* depends on earlier entries.
|
|
266
|
+
* For a chat client, pipe the whole log into `Timeline.changes(MyAssistant, { after })`, which
|
|
267
|
+
* streams the chat's timeline, as its user sees it. Its `after` goes there, not here: a message
|
|
268
|
+
* on the timeline depends on earlier entries. The timeline shows only the app events named in its
|
|
269
|
+
* `show`, none by default (see `Timeline.changes`).
|
|
237
270
|
*/
|
|
238
271
|
readonly subscribe: (chatId: ChatId, options?: {
|
|
239
272
|
readonly after?: number | undefined;
|
|
@@ -263,14 +296,14 @@ export interface Service<Events = never> {
|
|
|
263
296
|
* @category models
|
|
264
297
|
* @since 0.1.0
|
|
265
298
|
*/
|
|
266
|
-
export interface Assistant<Name extends string = string, Specs extends Any$
|
|
299
|
+
export interface Assistant<Name extends string = string, Specs extends Any$1 = never, Events = never> extends Context.Service<Identifier<Name>, Service<Events>> {
|
|
267
300
|
readonly [TypeId]: typeof TypeId;
|
|
268
301
|
readonly name: Name;
|
|
269
302
|
readonly blocks: BlockKit | undefined;
|
|
270
303
|
/** The specialists the assistant can hand work to. */
|
|
271
304
|
readonly specialists: ReadonlyArray<Specs>;
|
|
272
|
-
/** The app's own events the assistant
|
|
273
|
-
readonly events: ReadonlyArray<
|
|
305
|
+
/** The schemas of the app's own events the assistant stores. */
|
|
306
|
+
readonly events: ReadonlyArray<AppEventSchema>;
|
|
274
307
|
/**
|
|
275
308
|
* Creates a layer that registers the assistant's handler with the engine and provides the
|
|
276
309
|
* assistant's service.
|
|
@@ -322,9 +355,9 @@ export interface Assistant<Name extends string = string, Specs extends Any$2 = n
|
|
|
322
355
|
*
|
|
323
356
|
* export const MyAssistant = Assistant.make("my-assistant")
|
|
324
357
|
*
|
|
325
|
-
* // Requires `
|
|
358
|
+
* // Requires `Sharding`. The cluster's runners run the chats, with `AssistantEngine.layerCluster`.
|
|
326
359
|
* const MyAssistantClient = MyAssistant.layerClient.pipe(
|
|
327
|
-
* Layer.provide(AssistantEngine.
|
|
360
|
+
* Layer.provide(AssistantEngine.layerClusterClient)
|
|
328
361
|
* )
|
|
329
362
|
* ```
|
|
330
363
|
*/
|
|
@@ -352,14 +385,21 @@ export type Any = Assistant<any, any, any>;
|
|
|
352
385
|
*/
|
|
353
386
|
export declare const isAssistant: (u: unknown) => u is Any;
|
|
354
387
|
/**
|
|
355
|
-
* Creates an assistant definition from its name, its UI blocks, its specialists, and the
|
|
356
|
-
* events it stores
|
|
388
|
+
* Creates an assistant definition from its name, its UI blocks, its specialists, and the schemas of
|
|
389
|
+
* the app's own events it stores.
|
|
357
390
|
*
|
|
358
391
|
* **Details**
|
|
359
392
|
*
|
|
360
393
|
* The definition holds no code that runs turns. Provide a handler with its `toLayer`, then yield
|
|
361
394
|
* the definition to get the assistant's {@link Service}.
|
|
362
395
|
*
|
|
396
|
+
* The definition only says which of the app's events a chat stores. What happens with one is up
|
|
397
|
+
* to the places that use it. One that comes in through the service's `submit`, from outside a
|
|
398
|
+
* turn, is an input and wakes a turn, like a user's message; one a turn appends to its chat never
|
|
399
|
+
* does. The handler decides what a turn does, a history view says how the model reads an event
|
|
400
|
+
* ({@link HistoryView}), and the chat's timeline says which its user sees
|
|
401
|
+
* (`Timeline.changes`).
|
|
402
|
+
*
|
|
363
403
|
* **Example** (Defining an assistant and submitting a message)
|
|
364
404
|
*
|
|
365
405
|
* ```ts
|
|
@@ -382,7 +422,7 @@ export declare const isAssistant: (u: unknown) => u is Any;
|
|
|
382
422
|
* @category constructors
|
|
383
423
|
* @since 0.1.0
|
|
384
424
|
*/
|
|
385
|
-
export declare const make: <const Name extends string, const Specs extends ReadonlyArray<Any$
|
|
425
|
+
export declare const make: <const Name extends string, const Specs extends ReadonlyArray<Any$1> = readonly [], const Events extends ReadonlyArray<AppEventSchema> = readonly []>(name: Name, options?: {
|
|
386
426
|
/** UI blocks the assistant can add to its messages. */
|
|
387
427
|
readonly blocks?: BlockKit | undefined;
|
|
388
428
|
/**
|
|
@@ -391,13 +431,13 @@ export declare const make: <const Name extends string, const Specs extends Reado
|
|
|
391
431
|
*/
|
|
392
432
|
readonly specialists?: Specs | undefined;
|
|
393
433
|
/**
|
|
394
|
-
* The app's own events, each
|
|
395
|
-
* `
|
|
396
|
-
* decoded.
|
|
397
|
-
*
|
|
434
|
+
* The schemas of the app's own events, each a tagged struct or class. `submit` and the chat's
|
|
435
|
+
* `append` store them, in their JSON form, and the chat's `entries` and `subscribe` read them
|
|
436
|
+
* back, decoded. Each needs a tag of its own that isn't one of the library's: `make` throws
|
|
437
|
+
* otherwise.
|
|
398
438
|
*/
|
|
399
439
|
readonly events?: Events | undefined;
|
|
400
|
-
}) => Assistant<Name, Specs[number],
|
|
440
|
+
}) => Assistant<Name, Specs[number], Events[number]["Type"]>;
|
|
401
441
|
//#endregion
|
|
402
442
|
export { Assistant_d_exports };
|
|
403
443
|
//# sourceMappingURL=Assistant.d.ts.map
|
package/dist/Assistant.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Assistant.d.ts","names":[],"sources":["../src/Assistant.ts"],"mappings":"
|
|
1
|
+
{"version":3,"file":"Assist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6
|
import { register } from "./internal/turns.js";
|
|
7
7
|
import { AssistantEngine } from "./AssistantEngine.js";
|
|
8
|
-
import * as Predicate from "effect/Predicate";
|
|
9
8
|
import * as Context from "effect/Context";
|
|
10
9
|
import * as Effect from "effect/Effect";
|
|
11
10
|
import * as Filter from "effect/Filter";
|
|
12
11
|
import * as Layer from "effect/Layer";
|
|
12
|
+
import * as Predicate from "effect/Predicate";
|
|
13
13
|
import * as Schema from "effect/Schema";
|
|
14
14
|
import * as Stream from "effect/Stream";
|
|
15
15
|
//#region src/Assistant.ts
|
|
@@ -31,18 +31,25 @@ function entrySchema(events) {
|
|
|
31
31
|
chatId: ChatId,
|
|
32
32
|
seq: Schema.Int,
|
|
33
33
|
at: Schema.Finite,
|
|
34
|
-
event: Schema.Union([AssistantEvent, ...events
|
|
34
|
+
event: Schema.Union([AssistantEvent, ...events])
|
|
35
35
|
}));
|
|
36
36
|
}
|
|
37
37
|
/**
|
|
38
|
-
* Creates an assistant definition from its name, its UI blocks, its specialists, and the
|
|
39
|
-
* events it stores
|
|
38
|
+
* Creates an assistant definition from its name, its UI blocks, its specialists, and the schemas of
|
|
39
|
+
* the app's own events it stores.
|
|
40
40
|
*
|
|
41
41
|
* **Details**
|
|
42
42
|
*
|
|
43
43
|
* The definition holds no code that runs turns. Provide a handler with its `toLayer`, then yield
|
|
44
44
|
* the definition to get the assistant's {@link Service}.
|
|
45
45
|
*
|
|
46
|
+
* The definition only says which of the app's events a chat stores. What happens with one is up
|
|
47
|
+
* to the places that use it. One that comes in through the service's `submit`, from outside a
|
|
48
|
+
* turn, is an input and wakes a turn, like a user's message; one a turn appends to its chat never
|
|
49
|
+
* does. The handler decides what a turn does, a history view says how the model reads an event
|
|
50
|
+
* ({@link HistoryView}), and the chat's timeline says which its user sees
|
|
51
|
+
* (`Timeline.changes`).
|
|
52
|
+
*
|
|
46
53
|
* **Example** (Defining an assistant and submitting a message)
|
|
47
54
|
*
|
|
48
55
|
* ```ts
|
package/dist/Assistant.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Assistant.js","names":["AssistantEvent.AssistantEvent","AppEvents.make","AssistantEvent.isInput","AppEvents.encode","Chats.Chats","AppEvents.decodeEntry","Turns.register"],"sources":["../src/Assistant.ts"],"sourcesContent":["/**\n * This module provides `Assistant.make`, which defines an assistant that works in a chat the way a\n * colleague does: the user messages it, it works in the background with the app's tools, and it\n * messages the user back.\n *\n * A definition is what the rest of an app imports. Yielding it (`yield* MyAssistant`) returns the\n * assistant's {@link Service}, which submits inputs to chats and streams them back. Its `toLayer`\n * registers the handler that runs each {@link Turn} with the `AssistantEngine`, and its\n * `layerClient` provides the service without a handler. Code that runs inside a chat reads and\n * extends that chat through the definition's {@link Chat} key.\n *\n * @since 0.1.0\n */\nimport type * as AiError from \"effect/ai/AiError\"\nimport type * as LanguageModel from \"effect/ai/LanguageModel\"\nimport type * as Prompt from \"effect/ai/Prompt\"\nimport type * as Tool from \"effect/ai/Tool\"\nimport type * as Toolkit from \"effect/ai/Toolkit\"\nimport * as Context from \"effect/Context\"\nimport * as Effect from \"effect/Effect\"\nimport * as Filter from \"effect/Filter\"\nimport * as Layer from \"effect/Layer\"\nimport * as Predicate from \"effect/Predicate\"\nimport * as Schema from \"effect/Schema\"\nimport type * as Scope from \"effect/Scope\"\nimport * as Stream from \"effect/Stream\"\nimport type * as AppEvent from \"./AppEvent.ts\"\nimport { AssistantEngine } from \"./AssistantEngine.ts\"\nimport * as AssistantEvent from \"./AssistantEvent.ts\"\nimport type * as AssistantLoop from \"./AssistantLoop.ts\"\nimport type * as BlockKit from \"./BlockKit.ts\"\nimport { ChatId } from \"./ChatId.ts\"\nimport * as Chats from \"./Chats.ts\"\nimport type { CurrentChat } from \"./CurrentChat.ts\"\nimport type * as Instructions from \"./Instructions.ts\"\nimport * as AppEvents from \"./internal/events.ts\"\nimport * as Turns from \"./internal/turns.ts\"\nimport type { Skill } from \"./Skill.ts\"\nimport type * as Specialist from \"./Specialist.ts\"\n\nconst TypeId = \"~effect-assistant/Assistant\" as const\n\n/**\n * Represents the services that a turn provides to its tools, its loop, and its specialists. These\n * are the turn's chat (`CurrentChat`) and the chat's specialist runner (`Specialist.Runner`).\n *\n * @category utility types\n * @since 0.1.0\n */\nexport type TurnServices = CurrentChat | Specialist.Runner\n\n/**\n * Represents the options accepted by `turn.loop`.\n *\n * @category models\n * @since 0.1.0\n */\nexport interface LoopOptions<T extends Record<string, Tool.Any> = Record<never, never>, RS = never, Events = never> {\n /** The assistant's role and rules. The loop places `Instructions.protocol` before them. */\n readonly instructions: string | Instructions.Resolved\n /**\n * Tools the model can use in addition to the assistant's own. This is an `effect/ai` toolkit,\n * and its handlers come from the layer. Combine several toolkits with `Toolkit.merge`, for\n * example `Memory.Tools` or a `ToolDiscovery.make(...)` toolkit together with your own.\n */\n readonly toolkit?: Toolkit.Toolkit<T> | undefined\n /**\n * Playbooks the model can follow. The prompt lists them as a catalog, and the model loads one\n * with `load_skill` when a task matches it.\n */\n readonly skills?: ReadonlyArray<Skill<RS>> | undefined\n /** The maximum number of model calls per turn. Defaults to 16. */\n readonly maxSteps?: number | undefined\n /** The message the reply pass gives the model when the turn would end without a reply. */\n readonly replyNudge?: string | undefined\n /**\n * Selects which of the chat's entries the model reads, for example only those since the last\n * summary. Defaults to all entries.\n */\n readonly history?: HistoryView<Events> | undefined\n}\n\n/**\n * Represents a function that selects which of a chat's entries the model reads, and what else it\n * reads with them.\n *\n * **Details**\n *\n * The function receives the chat's entries and returns, in order, a mix of entries and\n * `Prompt.Message`s. The model reads the returned entries the same way it reads the full history,\n * and reads each prompt message unchanged.\n *\n * @category models\n * @since 0.1.0\n */\nexport type HistoryView<Events = never> = (\n entries: ReadonlyArray<Entry<Events>>\n) => ReadonlyArray<Entry<Events> | Prompt.Message>\n\n/**\n * Represents an entry in a chat's log, with the assistant's own events decoded to their types.\n *\n * @category models\n * @since 0.1.0\n */\nexport interface Entry<Events = never> {\n readonly chatId: ChatId\n readonly seq: number\n readonly at: number\n readonly event: AssistantEvent.AssistantEvent | Events\n}\n\n/**\n * Represents the options accepted by `turn.replyIfOwed`.\n *\n * @category models\n * @since 0.1.0\n */\nexport interface ReplyOptions {\n /** The assistant's instructions, as passed to `turn.loop`. `Instructions.protocol` is added. */\n readonly instructions: string | Instructions.Resolved\n /**\n * The message given to the model after the history. Defaults to a message saying that it\n * hasn't replied yet and should reply now.\n */\n readonly nudge?: string | undefined\n}\n\n/**\n * Represents one turn, as the assistant's handler receives it.\n *\n * `Events` is the type of the app's own events that the assistant registered with its `events`\n * option.\n *\n * @category models\n * @since 0.1.0\n */\nexport interface Turn<Events = never> {\n readonly chatId: ChatId\n /**\n * Returns the model's history, built from the chat's entries (all of them, or those a view\n * keeps), together with how far into the chat it reaches.\n *\n * Read it before every model call, as the loop does. Each step records how far its model call\n * read, and the turn ends once it has handled what the model saw. An input that arrived after\n * that is handled by the next turn.\n */\n readonly history: (view?: HistoryView<Events>) => Effect.Effect<AssistantLoop.ChatHistory>\n /**\n * Returns the full system prompt for the given instructions. It contains\n * `Instructions.protocol`, the instructions, and the specialist runs that are working now.\n */\n readonly instructions: (instructions: string | Instructions.Resolved) => Instructions.Resolved\n /**\n * Runs the agent loop over the chat. The loop uses `Instructions.protocol`, the assistant's own\n * tools, and the instructions, tools, and skills in `options`. Every step is a tool call, and\n * the loop ends with `replyIfOwed`.\n */\n readonly loop: <T extends Record<string, Tool.Any> = Record<never, never>, RS = never>(\n options: LoopOptions<T, RS, Events>\n ) => Stream.Stream<\n AssistantLoop.Event,\n AiError.AiError,\n LanguageModel.LanguageModel | TurnServices | AssistantLoop.ToolkitServices<T> | RS\n >\n /**\n * Runs one more pass when the turn would otherwise end without a reply to the user's latest\n * message. In this pass the model can only reply.\n *\n * The stream is empty when no reply is owed, or when an input arrived that the next turn will\n * read. `loop` runs this pass with its own prompt and tools, so the pass reuses the cached\n * prompt.\n */\n readonly replyIfOwed: (\n options: ReplyOptions\n ) => Stream.Stream<AssistantLoop.Event, AiError.AiError, LanguageModel.LanguageModel | TurnServices>\n}\n\n/**\n * Represents an assistant's handler, which runs a turn and returns it as a stream of the loop's\n * events.\n *\n * The handler may return the stream directly or from an Effect. `R` is the services the stream\n * requires, and `RE` is the services the Effect requires.\n *\n * @category models\n * @since 0.1.0\n */\nexport type Handler<R = never, RE = R, Events = never> = (\n turn: Turn<Events>\n) =>\n | Stream.Stream<AssistantLoop.Event, unknown, R>\n | Effect.Effect<Stream.Stream<AssistantLoop.Event, unknown, R>, unknown, RE>\n\n/**\n * Computes the services an assistant's handler needs beyond those its turns provide (the turn\n * services and `LanguageModel`) and its tools' handlers (which its layer requires). These are the\n * services that its build Effect must require.\n *\n * @category utility types\n * @since 0.1.0\n */\nexport type HandlerServices<R> = Exclude<R, TurnServices | LanguageModel.LanguageModel | Tool.Handler<string>>\n\nconst toStream = <R, RE>(\n result:\n | Stream.Stream<AssistantLoop.Event, unknown, R>\n | Effect.Effect<Stream.Stream<AssistantLoop.Event, unknown, R>, unknown, RE>\n): Stream.Stream<AssistantLoop.Event, unknown, R | RE> => (Effect.isEffect(result) ? Stream.unwrap(result) : result)\n\n/**\n * Identifies an assistant's service in a context. Each assistant has its own identifier, keyed by\n * its name.\n *\n * @category models\n * @since 0.1.0\n */\nexport interface Identifier<Name extends string> {\n readonly \"~effect-assistant/Assistant\": Name\n}\n\n/**\n * Identifies, in a context, the chat that an assistant's turns run in.\n *\n * @category models\n * @since 0.1.0\n */\nexport interface ChatIdentifier<Name extends string> {\n readonly \"~effect-assistant/Assistant/Chat\": Name\n}\n\n/**\n * Represents the chat that a turn of this assistant runs in, typed with the assistant's events.\n *\n * Use `yield* MyAssistant.Chat` in the handler, in the handler's build, or in a tool that its\n * turns run. Code that doesn't know the assistant uses `CurrentChat` instead.\n *\n * @see {@link CurrentChat} for the same chat without the assistant's event types\n *\n * @category models\n * @since 0.1.0\n */\nexport interface Chat<Events = never> {\n readonly chatId: ChatId\n /**\n * The chat's entries so far, in order. They include the library's events and the assistant's\n * own events, typed.\n */\n readonly entries: Effect.Effect<ReadonlyArray<Entry<Events>>>\n /**\n * Appends events after the chat's last entry and sends them to the chat's subscribers. The\n * events can be the library's or the assistant's own, such as a summary. An event that wakes a\n * turn is handled by the next turn.\n */\n append(...events: ReadonlyArray<AssistantEvent.AssistantEvent | Events>): Effect.Effect<void>\n}\n\n/**\n * Represents the service that `yield* MyAssistant` returns. It gives code outside a chat, such as\n * a server or a workflow, access to the assistant's chats. Code that runs inside a chat uses\n * `CurrentChat` instead.\n *\n * @category models\n * @since 0.1.0\n */\nexport interface Service<Events = never> {\n /**\n * Stores an input or one of the assistant's own events, then returns. Examples are a user's\n * message or block answer, a workflow's progress, or the draft the user opened. The assistant\n * works on events that wake a turn in the background.\n *\n * Fails with `GetChatError` when the app's `Chats` service reports that the chat can't be used,\n * because the app doesn't know the chat or has deleted it.\n */\n // Method syntax: a service for these events is one for any (`Service<any>`), for helpers over assistants.\n submit(chatId: ChatId, input: AssistantEvent.Input | Events): Effect.Effect<void, Chats.GetChatError>\n /**\n * Streams the chat's stored entries, then live ones, with the assistant's own events decoded.\n * A reader that already has the earlier entries passes `after` and receives only the entries\n * after that `seq`.\n *\n * For a chat client, pipe the whole log into `ChatView.updates(MyAssistant, { after })`, which\n * streams the chat as its user sees it. Its `after` goes there, not here: the view of a message\n * depends on earlier entries.\n */\n readonly subscribe: (\n chatId: ChatId,\n options?: { readonly after?: number | undefined }\n ) => Stream.Stream<Entry<Events>>\n /**\n * Deletes a chat by storing a `ChatDeleted` event and stopping everything that works in it.\n *\n * **Details**\n *\n * A turn or specialist run working in the chat stops, and subscribers' streams end with the\n * `ChatDeleted` event. After that the chat stores nothing more: a later input, a run's report,\n * or a workflow's event for it is dropped without an error. The log is kept, and removing it is\n * up to the app. Deleting a chat a second time does nothing.\n *\n * When the app's `Chats` service reports a chat as deleted, the assistant deletes it in the same\n * way the next time it checks. It checks before an input is stored and before each model call.\n */\n readonly delete: (chatId: ChatId) => Effect.Effect<void>\n}\n\n/**\n * Represents an assistant's definition.\n *\n * The definition holds what callers and the UI need to know about the assistant, builds its\n * handler's layer, and is the key of its service (`yield* MyAssistant`). `Events` is the type of\n * the app's own events that the assistant registered.\n *\n * @category models\n * @since 0.1.0\n */\nexport interface Assistant<\n Name extends string = string,\n Specs extends Specialist.Any = never,\n Events = never\n> extends Context.Service<Identifier<Name>, Service<Events>> {\n readonly [TypeId]: typeof TypeId\n readonly name: Name\n readonly blocks: BlockKit.BlockKit | undefined\n /** The specialists the assistant can hand work to. */\n readonly specialists: ReadonlyArray<Specs>\n /** The app's own events the assistant registered. */\n readonly events: ReadonlyArray<AppEvent.Any>\n /**\n * Creates a layer that registers the assistant's handler with the engine and provides the\n * assistant's service.\n *\n * **Details**\n *\n * `build` is the handler, or an Effect that returns it. The handler may use the turn services,\n * `LanguageModel`, its tools' handlers (which the layer then requires), and services that its\n * build Effect required (see `Specialist.CheckServices`). The layer requires the\n * `AssistantEngine`, the `LanguageModel`, and each specialist's handler\n * (`Research.toLayer(...)`).\n *\n * **Example** (Providing an assistant's handler)\n *\n * ```ts\n * import { Assistant, AssistantEngine, ChatLog } from \"effect-assistant\"\n * import type * as LanguageModel from \"effect/ai/LanguageModel\"\n * import * as Layer from \"effect/Layer\"\n *\n * declare const ModelLive: Layer.Layer<LanguageModel.LanguageModel>\n *\n * export const MyAssistant = Assistant.make(\"my-assistant\")\n *\n * const MyAssistantLive = MyAssistant.toLayer((turn) =>\n * turn.loop({ instructions: \"You help a small team. Be brief.\" })\n * )\n *\n * const AppLive = MyAssistantLive.pipe(\n * Layer.provide(ModelLive),\n * Layer.provideMerge(AssistantEngine.layerMemory),\n * Layer.provideMerge(ChatLog.layerMemory)\n * )\n * ```\n */\n readonly toLayer: <R = never, RE = never, E = never, RX = never>(\n build: (Handler<R, RE, Events> | Effect.Effect<Handler<R, RE, Events>, E, RX>) &\n Specialist.CheckServices<Exclude<HandlerServices<R | RE>, RX | ChatIdentifier<Name>>>\n ) => Layer.Layer<\n Identifier<Name>,\n never,\n | AssistantEngine\n | LanguageModel.LanguageModel\n | Specialist.HandlerOf<Specs>\n | Exclude<RX, Scope.Scope | CurrentChat | ChatIdentifier<Name>>\n | Specialist.ToolHandlers<R | RE>\n >\n /**\n * A layer that provides the assistant's service without its handler.\n *\n * **When to use**\n *\n * Use it in a process that submits to chats that another process runs, such as a web server in\n * front of a cluster.\n *\n * **Example** (Serving clients in front of a cluster)\n *\n * ```ts\n * import { Assistant, AssistantEngine } from \"effect-assistant\"\n * import * as Layer from \"effect/Layer\"\n *\n * export const MyAssistant = Assistant.make(\"my-assistant\")\n *\n * // Requires `ChatLog` and `Sharding`. The cluster's runners run the chats.\n * const MyAssistantClient = MyAssistant.layerClient.pipe(\n * Layer.provide(AssistantEngine.layerCluster)\n * )\n * ```\n */\n readonly layerClient: Layer.Layer<Identifier<Name>, never, AssistantEngine>\n /**\n * The key of the chat that the assistant's turns run in, typed with its events. See\n * {@link Chat}.\n */\n readonly Chat: Context.Service<ChatIdentifier<Name>, Chat<Events>>\n /** A schema that encodes the chat's entries to JSON, including the assistant's own events. */\n readonly Entry: Schema.Codec<Entry<Events>, Schema.Json>\n}\n\n/**\n * Represents any assistant, for use in generic constraints.\n *\n * @category utility types\n * @since 0.1.0\n */\nexport type Any = Assistant<any, any, any>\n\n/**\n * Returns `true` when the value is an assistant's definition, created with {@link make}.\n *\n * @category guards\n * @since 0.1.0\n */\nexport const isAssistant = (u: unknown): u is Any => Predicate.hasProperty(u, TypeId)\n\nfunction entrySchema<E>(events: ReadonlyArray<AppEvent.Any>): Schema.Codec<Entry<E>, Schema.Json>\n// The overload states the decoded type: the library's events, or one of these schemas' types.\nfunction entrySchema(events: ReadonlyArray<AppEvent.Any>): Schema.Top {\n return Schema.toCodecJson(\n Schema.Struct({\n chatId: ChatId,\n seq: Schema.Int,\n at: Schema.Finite,\n event: Schema.Union([AssistantEvent.AssistantEvent, ...events.map((event) => event.schema)])\n })\n )\n}\n\n/**\n * Creates an assistant definition from its name, its UI blocks, its specialists, and the app's own\n * events it stores (see `AppEvent`).\n *\n * **Details**\n *\n * The definition holds no code that runs turns. Provide a handler with its `toLayer`, then yield\n * the definition to get the assistant's {@link Service}.\n *\n * **Example** (Defining an assistant and submitting a message)\n *\n * ```ts\n * import { Assistant, AssistantEvent, ChatId } from \"effect-assistant\"\n * import * as Effect from \"effect/Effect\"\n *\n * export const MyAssistant = Assistant.make(\"my-assistant\")\n *\n * const program = Effect.gen(function* () {\n * const assistant = yield* MyAssistant\n * const chatId = yield* ChatId.make()\n *\n * yield* assistant.submit(chatId, AssistantEvent.UserMessage.make({ text: \"Weather in Oslo?\" }))\n * })\n * ```\n *\n * @see {@link Assistant} for the definition's members\n * @see {@link Service} for what `yield* MyAssistant` returns\n *\n * @category constructors\n * @since 0.1.0\n */\nexport const make = <\n const Name extends string,\n const Specs extends ReadonlyArray<Specialist.Any> = readonly [],\n const Events extends ReadonlyArray<AppEvent.Any> = readonly []\n>(\n name: Name,\n options?: {\n /** UI blocks the assistant can add to its messages. */\n readonly blocks?: BlockKit.BlockKit | undefined\n /**\n * The specialists the assistant can hand work to. Each specialist's `toLayer` must be\n * provided to the assistant's layer.\n */\n readonly specialists?: Specs | undefined\n /**\n * The app's own events, each created with `AppEvent.make(schema, { wakes, render })`.\n * `submit` and the chat's `append` store them, and the chat's `entries` read them back,\n * decoded. Events that wake a turn are inputs, and the model reads each event that has a\n * `render`.\n */\n readonly events?: Events | undefined\n }\n): Assistant<Name, Specs[number], AppEvent.Type<Events[number]>> => {\n const events = options?.events ?? []\n\n const definition: Turns.Definition = {\n name,\n blocks: options?.blocks,\n specialists: options?.specialists ?? [],\n app: AppEvents.make(events)\n }\n\n type OwnEvents = AppEvent.Type<Events[number]>\n\n const Key = Context.Service<Identifier<Name>, Service<OwnEvents>>(`effect-assistant/Assistant/${name}`)\n\n // The app's own events go to the engine encoded, as they're stored; the library's go unchanged.\n const stored = (\n input: AssistantEvent.Input | OwnEvents\n ): Effect.Effect<AssistantEvent.Input | AssistantEvent.StoredAppEvent> =>\n AssistantEvent.isInput(input) ? Effect.succeed(input) : AppEvents.encode(definition.app, input)\n\n const ChatKey = Context.Service<ChatIdentifier<Name>, Chat<OwnEvents>>(`effect-assistant/Assistant/${name}/Chat`)\n\n // On the engine and the app's `Chats` the layer was given, so every caller sees the same ones.\n const makeService = Effect.gen(function* () {\n const engine = yield* AssistantEngine\n const chats = yield* Chats.Chats\n\n return Key.of({\n submit: Effect.fn(\"Assistant.submit\")(function* (chatId: ChatId, input: AssistantEvent.Input | OwnEvents) {\n // Checked here, where the caller can handle it: the chat checks again when it stores the input.\n yield* chats.get(chatId)\n yield* engine.submit(name, chatId, yield* stored(input))\n }),\n subscribe: (chatId, options) =>\n engine\n .subscribe(name, chatId, options?.after ?? 0)\n .pipe(\n Stream.filterMap(\n Filter.fromPredicateOption((entry: AssistantEvent.Entry) => AppEvents.decodeEntry(definition.app, entry))\n )\n ),\n delete: (chatId) => engine.delete(name, chatId)\n })\n })\n\n // The signature checks what the handler uses (`CheckServices`); the layer's services, which the build\n // required, are provided to it and to every turn's stream, so what's left is the turn services.\n function toLayer<R = never, RE = never, E = never, RX = never>(\n build: (Handler<R, RE, OwnEvents> | Effect.Effect<Handler<R, RE, OwnEvents>, E, RX>) &\n Specialist.CheckServices<Exclude<HandlerServices<R | RE>, RX | ChatIdentifier<Name>>>\n ): Layer.Layer<\n Identifier<Name>,\n never,\n | AssistantEngine\n | LanguageModel.LanguageModel\n | Specialist.HandlerOf<Specs[number]>\n | Exclude<RX, Scope.Scope | CurrentChat | ChatIdentifier<Name>>\n | Specialist.ToolHandlers<R | RE>\n >\n function toLayer(\n build: Handler<any, any, any> | Effect.Effect<Handler<any, any, any>, unknown, any>\n ): Layer.Layer<Identifier<Name>, never, any> {\n return Layer.effect(\n Key,\n Effect.gen(function* () {\n const context = yield* Effect.context()\n\n const registration = yield* Turns.register(definition, {\n // The build runs per chat activation, in the chat; here it only keeps the layer's services for it.\n open: (typed) =>\n Effect.gen(function* () {\n // The chat's entries are already decoded with this assistant's events.\n const chat = ChatKey.of({ chatId: typed.chatId, entries: typed.entries, append: typed.append })\n const handler = Effect.isEffect(build) ? yield* Effect.provideService(build, ChatKey, chat) : build\n\n return (turn: Turn<any>) =>\n Stream.provideContext(toStream(handler(turn)), context).pipe(Stream.provideService(ChatKey, chat))\n }).pipe(Effect.provideContext(context), Effect.orDie)\n })\n\n yield* (yield* AssistantEngine).register(registration)\n\n return yield* makeService\n })\n )\n }\n\n // The key is a function, whose own `name` is read-only: the assistant's name replaces it.\n const named = Object.defineProperty(Key, \"name\", { value: name, writable: true })\n\n return Object.assign(named, {\n [TypeId]: TypeId,\n name,\n blocks: options?.blocks,\n specialists: options?.specialists ?? [],\n events,\n toLayer,\n layerClient: Layer.effect(Key, makeService),\n Chat: ChatKey,\n Entry: entrySchema<OwnEvents>(events)\n })\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;AAwCA,MAAM,SAAS;AAoKf,MAAM,YACJ,WAGyD,OAAO,SAAS,MAAM,IAAI,OAAO,OAAO,MAAM,IAAI;;;;;;;AAmN7G,MAAa,eAAe,MAAyB,UAAU,YAAY,GAAG,MAAM;AAIpF,SAAS,YAAY,QAAiD;CACpE,OAAO,OAAO,YACZ,OAAO,OAAO;EACZ,QAAQ;EACR,KAAK,OAAO;EACZ,IAAI,OAAO;EACX,OAAO,OAAO,MAAM,CAACA,gBAA+B,GAAG,OAAO,KAAK,UAAU,MAAM,MAAM,CAAC,CAAC;CAC7F,CAAC,CACH;AACF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAiCA,MAAa,QAKX,MACA,YAgBkE;CAClE,MAAM,SAAS,SAAS,UAAU,CAAC;CAEnC,MAAM,aAA+B;EACnC;EACA,QAAQ,SAAS;EACjB,aAAa,SAAS,eAAe,CAAC;EACtC,KAAKC,OAAe,MAAM;CAC5B;CAIA,MAAM,MAAM,QAAQ,QAA8C,8BAA8B,MAAM;CAGtG,MAAM,UACJ,UAEAC,QAAuB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YAAY;GAErD,OAAO,OAAO;EAChB,CAAC,CACH;CACF;CAGA,MAAM,QAAQ,OAAO,eAAe,KAAK,QAAQ;EAAE,OAAO;EAAM,UAAU;CAAK,CAAC;CAEhF,OAAO,OAAO,OAAO,OAAO;GACzB,SAAS;EACV;EACA,QAAQ,SAAS;EACjB,aAAa,SAAS,eAAe,CAAC;EACtC;EACA;EACA,aAAa,MAAM,OAAO,KAAK,WAAW;EAC1C,MAAM;EACN,OAAO,YAAuB,MAAM;CACtC,CAAC;AACH"}
|
|
1
|
+
{"version":3,"file":"Assistant.js","names":["AssistantEvent.AssistantEvent","AppEvents.make","AssistantEvent.isInput","AppEvents.encode","Chats.Chats","AppEvents.decodeEntry","Turns.register"],"sources":["../src/Assistant.ts"],"sourcesContent":["/**\n * This module provides `Assistant.make`, which defines an assistant that works in a chat the way a\n * colleague does: the user messages it, it works in the background with the app's tools, and it\n * messages the user back.\n *\n * A definition is what the rest of an app imports. Yielding it (`yield* MyAssistant`) returns the\n * assistant's {@link Service}, which submits inputs to chats and streams them back. Its `toLayer`\n * registers the handler that runs each {@link Turn} with the `AssistantEngine`, and its\n * `layerClient` provides the service without a handler. Code that runs inside a chat reads and\n * extends that chat through the definition's {@link Chat} key.\n *\n * @since 0.1.0\n */\nimport type * as AiError from \"effect/ai/AiError\"\nimport type * as LanguageModel from \"effect/ai/LanguageModel\"\nimport type * as Prompt from \"effect/ai/Prompt\"\nimport type * as Tool from \"effect/ai/Tool\"\nimport type * as Toolkit from \"effect/ai/Toolkit\"\nimport * as Context from \"effect/Context\"\nimport * as Effect from \"effect/Effect\"\nimport * as Filter from \"effect/Filter\"\nimport * as Layer from \"effect/Layer\"\nimport * as Predicate from \"effect/Predicate\"\nimport * as Schema from \"effect/Schema\"\nimport type * as Scope from \"effect/Scope\"\nimport * as Stream from \"effect/Stream\"\nimport { AssistantEngine } from \"./AssistantEngine.ts\"\nimport * as AssistantEvent from \"./AssistantEvent.ts\"\nimport type * as AssistantLoop from \"./AssistantLoop.ts\"\nimport type * as BlockKit from \"./BlockKit.ts\"\nimport { ChatId } from \"./ChatId.ts\"\nimport * as Chats from \"./Chats.ts\"\nimport type { CurrentChat } from \"./CurrentChat.ts\"\nimport type * as Instructions from \"./Instructions.ts\"\nimport * as AppEvents from \"./internal/events.ts\"\nimport * as Turns from \"./internal/turns.ts\"\nimport type * as Specialist from \"./Specialist.ts\"\n\nconst TypeId = \"~effect-assistant/Assistant\" as const\n\n/**\n * Represents the services that a turn provides to its tools, its loop, and its specialists. These\n * are the turn's chat (`CurrentChat`) and the chat's specialist runner (`Specialist.Runner`).\n *\n * @category utility types\n * @since 0.1.0\n */\nexport type TurnServices = CurrentChat | Specialist.Runner\n\n/**\n * Represents the options accepted by `turn.loop`.\n *\n * @category models\n * @since 0.1.0\n */\nexport interface LoopOptions<T extends Record<string, Tool.Any> = Record<never, never>, Events = never> {\n /** The assistant's role and rules. The loop places `Instructions.protocol` before them. */\n readonly instructions: string | Instructions.Resolved\n /**\n * Tools the model can use in addition to the assistant's own. This is an `effect/ai` toolkit,\n * and its handlers come from the layer. Combine several toolkits with `Toolkit.merge`, for\n * example `Memory.Tools`, a `Skill.toolkit(...)`, or a `ToolDiscovery.make(...)` toolkit together\n * with your own.\n */\n readonly toolkit?: Toolkit.Toolkit<T> | undefined\n /** The maximum number of model calls per turn. Defaults to 16. */\n readonly maxSteps?: number | undefined\n /** The message the reply pass gives the model when the turn would end without a reply. */\n readonly replyNudge?: string | undefined\n /**\n * Selects which of the chat's entries the model reads, for example only those since the last\n * summary, and maps the app's own events it should read into prompt messages. Defaults to all\n * entries, without the app's events.\n */\n readonly history?: HistoryView<Events> | undefined\n}\n\n/**\n * Represents a function that selects which of a chat's entries the model reads, and what else it\n * reads with them.\n *\n * **Details**\n *\n * The function receives the chat's entries, the app's own events among them, and returns, in\n * order, a mix of entries and `Prompt.Message`s. The model reads the returned entries the same way\n * it reads the full history, and reads each prompt message unchanged, where the view put it. The\n * model reads none of the app's events by itself: map those it should read to a message, and an\n * app event's entry left in the result is dropped.\n *\n * A message stands right after the entry before it: one that came after the model's last read, or\n * between a tool call and its result, moves after them, as an input does.\n *\n * **Example** (Telling the model about an order the user placed)\n *\n * ```ts\n * import { Assistant, InputId } from \"effect-assistant\"\n * import * as Prompt from \"effect/ai/Prompt\"\n * import * as Schema from \"effect/Schema\"\n *\n * class OrderPlaced extends Schema.TaggedClass<OrderPlaced>()(\"OrderPlaced\", {\n * id: InputId.InputId,\n * reference: Schema.String\n * }) {}\n *\n * const MyAssistant = Assistant.make(\"my-assistant\", { events: [OrderPlaced] })\n *\n * const withOrders: Assistant.HistoryView<OrderPlaced> = (entries) =>\n * entries.map((entry) =>\n * entry.event instanceof OrderPlaced\n * ? Prompt.userMessage({\n * content: [Prompt.textPart({ text: `The user placed order ${entry.event.reference}.` })]\n * })\n * : entry\n * )\n *\n * // An `OrderPlaced` that the API submits wakes a turn, which reads it as this note.\n * const MyAssistantLive = MyAssistant.toLayer((turn) =>\n * turn.loop({ instructions: \"Be brief.\", history: withOrders })\n * )\n * ```\n *\n * @category models\n * @since 0.1.0\n */\nexport type HistoryView<Events = never> = (\n entries: ReadonlyArray<Entry<Events>>\n) => ReadonlyArray<Entry<Events> | Prompt.Message>\n\n/**\n * Represents an entry in a chat's log, with the assistant's own events decoded to their types.\n *\n * @category models\n * @since 0.1.0\n */\nexport interface Entry<Events = never> {\n readonly chatId: ChatId\n readonly seq: number\n readonly at: number\n readonly event: AssistantEvent.AssistantEvent | Events\n}\n\n/**\n * Represents the options accepted by `turn.replyIfOwed`.\n *\n * @category models\n * @since 0.1.0\n */\nexport interface ReplyOptions {\n /** The assistant's instructions, as passed to `turn.loop`. `Instructions.protocol` is added. */\n readonly instructions: string | Instructions.Resolved\n /**\n * The message given to the model after the history. Defaults to a message saying that it\n * hasn't replied yet and should reply now.\n */\n readonly nudge?: string | undefined\n}\n\n/**\n * Represents one turn, as the assistant's handler receives it.\n *\n * `Events` is the type of the app's own events that the assistant registered with its `events`\n * option.\n *\n * @category models\n * @since 0.1.0\n */\nexport interface Turn<Events = never> {\n readonly chatId: ChatId\n /**\n * Returns the model's history, built from the chat's entries (all of them, or those a view\n * keeps), together with how far into the chat it reaches.\n *\n * Read it before every model call, as the loop does. Each step records how far its model call\n * read, and the turn ends once it has handled what the model saw. An input that arrived after\n * that is handled by the next turn.\n */\n readonly history: (view?: HistoryView<Events>) => Effect.Effect<AssistantLoop.ChatHistory>\n /**\n * Returns the full system prompt for the given instructions. It contains\n * `Instructions.protocol`, the instructions, and the specialist runs that are working now.\n */\n readonly instructions: (instructions: string | Instructions.Resolved) => Instructions.Resolved\n /**\n * Runs the agent loop over the chat. The loop uses `Instructions.protocol`, the assistant's own\n * tools, and the instructions and tools in `options`. Every step is a tool call, and\n * the loop ends with `replyIfOwed`.\n */\n readonly loop: <T extends Record<string, Tool.Any> = Record<never, never>>(\n options: LoopOptions<T, Events>\n ) => Stream.Stream<\n AssistantLoop.Event,\n AiError.AiError,\n LanguageModel.LanguageModel | TurnServices | AssistantLoop.ToolkitServices<T>\n >\n /**\n * Runs one more pass when the turn would otherwise end without a reply to the user's latest\n * message. In this pass the model can only reply.\n *\n * The stream is empty when no reply is owed, or when an input arrived that the next turn will\n * read. `loop` runs this pass with its own prompt and tools, so the pass reuses the cached\n * prompt.\n */\n readonly replyIfOwed: (\n options: ReplyOptions\n ) => Stream.Stream<AssistantLoop.Event, AiError.AiError, LanguageModel.LanguageModel | TurnServices>\n}\n\n/**\n * Represents an assistant's handler, which runs a turn and returns it as a stream of the loop's\n * events.\n *\n * The handler may return the stream directly or from an Effect. `R` is the services the stream\n * requires, and `RE` is the services the Effect requires.\n *\n * @category models\n * @since 0.1.0\n */\nexport type Handler<R = never, RE = R, Events = never> = (\n turn: Turn<Events>\n) =>\n | Stream.Stream<AssistantLoop.Event, unknown, R>\n | Effect.Effect<Stream.Stream<AssistantLoop.Event, unknown, R>, unknown, RE>\n\n/**\n * Computes the services an assistant's handler needs beyond those its turns provide (the turn\n * services and `LanguageModel`) and its tools' handlers (which its layer requires). These are the\n * services that its build Effect must require.\n *\n * @category utility types\n * @since 0.1.0\n */\nexport type HandlerServices<R> = Exclude<R, TurnServices | LanguageModel.LanguageModel | Tool.Handler<string>>\n\nconst toStream = <R, RE>(\n result:\n | Stream.Stream<AssistantLoop.Event, unknown, R>\n | Effect.Effect<Stream.Stream<AssistantLoop.Event, unknown, R>, unknown, RE>\n): Stream.Stream<AssistantLoop.Event, unknown, R | RE> => (Effect.isEffect(result) ? Stream.unwrap(result) : result)\n\n/**\n * Identifies an assistant's service in a context. Each assistant has its own identifier, keyed by\n * its name.\n *\n * @category models\n * @since 0.1.0\n */\nexport interface Identifier<Name extends string> {\n readonly \"~effect-assistant/Assistant\": Name\n}\n\n/**\n * Identifies, in a context, the chat that an assistant's turns run in.\n *\n * @category models\n * @since 0.1.0\n */\nexport interface ChatIdentifier<Name extends string> {\n readonly \"~effect-assistant/Assistant/Chat\": Name\n}\n\n/**\n * Represents the chat that a turn of this assistant runs in, typed with the assistant's events.\n *\n * Use `yield* MyAssistant.Chat` in the handler, in the handler's build, or in a tool that its\n * turns run. Code that doesn't know the assistant uses `CurrentChat` instead.\n *\n * @see {@link CurrentChat} for the same chat without the assistant's event types\n *\n * @category models\n * @since 0.1.0\n */\nexport interface Chat<Events = never> {\n readonly chatId: ChatId\n /**\n * The chat's entries so far, in order. They include the library's events and the assistant's\n * own events, typed.\n */\n readonly entries: Effect.Effect<ReadonlyArray<Entry<Events>>>\n /**\n * Appends events after the chat's last entry and sends them to the chat's subscribers. The\n * events can be the library's or the assistant's own, such as a summary. None of them wakes a\n * turn: only what comes in through the assistant's `submit` does.\n */\n append(...events: ReadonlyArray<AssistantEvent.AssistantEvent | Events>): Effect.Effect<void>\n}\n\n/**\n * Represents the service that `yield* MyAssistant` returns. It gives code outside a chat, such as\n * a server or a workflow, access to the assistant's chats. Code that runs inside a chat uses\n * `CurrentChat` instead.\n *\n * @category models\n * @since 0.1.0\n */\nexport interface Service<Events = never> {\n /**\n * Stores an input or one of the assistant's own events, then returns. Examples are a user's\n * message or block answer, a workflow's progress, or the order the user placed. Each one is an\n * input: storing it wakes a turn, which the assistant runs in the background. The handler decides\n * what the turn does with it, and an app event the model should read needs a history view that\n * maps it (see {@link HistoryView}). To store an app event without waking a turn, append it from\n * within the chat instead (`yield* MyAssistant.Chat`).\n *\n * Fails with `GetChatError` when the app's `Chats` service reports that the chat can't be used,\n * because the app doesn't know the chat or has deleted it.\n */\n // Method syntax: a service for these events is one for any (`Service<any>`), for helpers over assistants.\n submit(chatId: ChatId, input: AssistantEvent.Input | Events): Effect.Effect<void, Chats.GetChatError>\n /**\n * Streams the chat's stored entries, then live ones, with the assistant's own events decoded.\n * A reader that already has the earlier entries passes `after` and receives only the entries\n * after that `seq`.\n *\n * For a chat client, pipe the whole log into `Timeline.changes(MyAssistant, { after })`, which\n * streams the chat's timeline, as its user sees it. Its `after` goes there, not here: a message\n * on the timeline depends on earlier entries. The timeline shows only the app events named in its\n * `show`, none by default (see `Timeline.changes`).\n */\n readonly subscribe: (\n chatId: ChatId,\n options?: { readonly after?: number | undefined }\n ) => Stream.Stream<Entry<Events>>\n /**\n * Deletes a chat by storing a `ChatDeleted` event and stopping everything that works in it.\n *\n * **Details**\n *\n * A turn or specialist run working in the chat stops, and subscribers' streams end with the\n * `ChatDeleted` event. After that the chat stores nothing more: a later input, a run's report,\n * or a workflow's event for it is dropped without an error. The log is kept, and removing it is\n * up to the app. Deleting a chat a second time does nothing.\n *\n * When the app's `Chats` service reports a chat as deleted, the assistant deletes it in the same\n * way the next time it checks. It checks before an input is stored and before each model call.\n */\n readonly delete: (chatId: ChatId) => Effect.Effect<void>\n}\n\n/**\n * Represents an assistant's definition.\n *\n * The definition holds what callers and the UI need to know about the assistant, builds its\n * handler's layer, and is the key of its service (`yield* MyAssistant`). `Events` is the type of\n * the app's own events that the assistant registered.\n *\n * @category models\n * @since 0.1.0\n */\nexport interface Assistant<\n Name extends string = string,\n Specs extends Specialist.Any = never,\n Events = never\n> extends Context.Service<Identifier<Name>, Service<Events>> {\n readonly [TypeId]: typeof TypeId\n readonly name: Name\n readonly blocks: BlockKit.BlockKit | undefined\n /** The specialists the assistant can hand work to. */\n readonly specialists: ReadonlyArray<Specs>\n /** The schemas of the app's own events the assistant stores. */\n readonly events: ReadonlyArray<AssistantEvent.AppEventSchema>\n /**\n * Creates a layer that registers the assistant's handler with the engine and provides the\n * assistant's service.\n *\n * **Details**\n *\n * `build` is the handler, or an Effect that returns it. The handler may use the turn services,\n * `LanguageModel`, its tools' handlers (which the layer then requires), and services that its\n * build Effect required (see `Specialist.CheckServices`). The layer requires the\n * `AssistantEngine`, the `LanguageModel`, and each specialist's handler\n * (`Research.toLayer(...)`).\n *\n * **Example** (Providing an assistant's handler)\n *\n * ```ts\n * import { Assistant, AssistantEngine, ChatLog } from \"effect-assistant\"\n * import type * as LanguageModel from \"effect/ai/LanguageModel\"\n * import * as Layer from \"effect/Layer\"\n *\n * declare const ModelLive: Layer.Layer<LanguageModel.LanguageModel>\n *\n * export const MyAssistant = Assistant.make(\"my-assistant\")\n *\n * const MyAssistantLive = MyAssistant.toLayer((turn) =>\n * turn.loop({ instructions: \"You help a small team. Be brief.\" })\n * )\n *\n * const AppLive = MyAssistantLive.pipe(\n * Layer.provide(ModelLive),\n * Layer.provideMerge(AssistantEngine.layerMemory),\n * Layer.provideMerge(ChatLog.layerMemory)\n * )\n * ```\n */\n readonly toLayer: <R = never, RE = never, E = never, RX = never>(\n build: (Handler<R, RE, Events> | Effect.Effect<Handler<R, RE, Events>, E, RX>) &\n Specialist.CheckServices<Exclude<HandlerServices<R | RE>, RX | ChatIdentifier<Name>>>\n ) => Layer.Layer<\n Identifier<Name>,\n never,\n | AssistantEngine\n | LanguageModel.LanguageModel\n | Specialist.HandlerOf<Specs>\n | Exclude<RX, Scope.Scope | CurrentChat | ChatIdentifier<Name>>\n | Specialist.ToolHandlers<R | RE>\n >\n /**\n * A layer that provides the assistant's service without its handler.\n *\n * **When to use**\n *\n * Use it in a process that submits to chats that another process runs, such as a web server in\n * front of a cluster.\n *\n * **Example** (Serving clients in front of a cluster)\n *\n * ```ts\n * import { Assistant, AssistantEngine } from \"effect-assistant\"\n * import * as Layer from \"effect/Layer\"\n *\n * export const MyAssistant = Assistant.make(\"my-assistant\")\n *\n * // Requires `Sharding`. The cluster's runners run the chats, with `AssistantEngine.layerCluster`.\n * const MyAssistantClient = MyAssistant.layerClient.pipe(\n * Layer.provide(AssistantEngine.layerClusterClient)\n * )\n * ```\n */\n readonly layerClient: Layer.Layer<Identifier<Name>, never, AssistantEngine>\n /**\n * The key of the chat that the assistant's turns run in, typed with its events. See\n * {@link Chat}.\n */\n readonly Chat: Context.Service<ChatIdentifier<Name>, Chat<Events>>\n /** A schema that encodes the chat's entries to JSON, including the assistant's own events. */\n readonly Entry: Schema.Codec<Entry<Events>, Schema.Json>\n}\n\n/**\n * Represents any assistant, for use in generic constraints.\n *\n * @category utility types\n * @since 0.1.0\n */\nexport type Any = Assistant<any, any, any>\n\n/**\n * Returns `true` when the value is an assistant's definition, created with {@link make}.\n *\n * @category guards\n * @since 0.1.0\n */\nexport const isAssistant = (u: unknown): u is Any => Predicate.hasProperty(u, TypeId)\n\nfunction entrySchema<E>(events: ReadonlyArray<AssistantEvent.AppEventSchema>): Schema.Codec<Entry<E>, Schema.Json>\n// The overload states the decoded type: the library's events, or one of these schemas' types.\nfunction entrySchema(events: ReadonlyArray<AssistantEvent.AppEventSchema>): Schema.Top {\n return Schema.toCodecJson(\n Schema.Struct({\n chatId: ChatId,\n seq: Schema.Int,\n at: Schema.Finite,\n event: Schema.Union([AssistantEvent.AssistantEvent, ...events])\n })\n )\n}\n\n/**\n * Creates an assistant definition from its name, its UI blocks, its specialists, and the schemas of\n * the app's own events it stores.\n *\n * **Details**\n *\n * The definition holds no code that runs turns. Provide a handler with its `toLayer`, then yield\n * the definition to get the assistant's {@link Service}.\n *\n * The definition only says which of the app's events a chat stores. What happens with one is up\n * to the places that use it. One that comes in through the service's `submit`, from outside a\n * turn, is an input and wakes a turn, like a user's message; one a turn appends to its chat never\n * does. The handler decides what a turn does, a history view says how the model reads an event\n * ({@link HistoryView}), and the chat's timeline says which its user sees\n * (`Timeline.changes`).\n *\n * **Example** (Defining an assistant and submitting a message)\n *\n * ```ts\n * import { Assistant, AssistantEvent, ChatId } from \"effect-assistant\"\n * import * as Effect from \"effect/Effect\"\n *\n * export const MyAssistant = Assistant.make(\"my-assistant\")\n *\n * const program = Effect.gen(function* () {\n * const assistant = yield* MyAssistant\n * const chatId = yield* ChatId.make()\n *\n * yield* assistant.submit(chatId, AssistantEvent.UserMessage.make({ text: \"Weather in Oslo?\" }))\n * })\n * ```\n *\n * @see {@link Assistant} for the definition's members\n * @see {@link Service} for what `yield* MyAssistant` returns\n *\n * @category constructors\n * @since 0.1.0\n */\nexport const make = <\n const Name extends string,\n const Specs extends ReadonlyArray<Specialist.Any> = readonly [],\n const Events extends ReadonlyArray<AssistantEvent.AppEventSchema> = readonly []\n>(\n name: Name,\n options?: {\n /** UI blocks the assistant can add to its messages. */\n readonly blocks?: BlockKit.BlockKit | undefined\n /**\n * The specialists the assistant can hand work to. Each specialist's `toLayer` must be\n * provided to the assistant's layer.\n */\n readonly specialists?: Specs | undefined\n /**\n * The schemas of the app's own events, each a tagged struct or class. `submit` and the chat's\n * `append` store them, in their JSON form, and the chat's `entries` and `subscribe` read them\n * back, decoded. Each needs a tag of its own that isn't one of the library's: `make` throws\n * otherwise.\n */\n readonly events?: Events | undefined\n }\n): Assistant<Name, Specs[number], Events[number][\"Type\"]> => {\n const events = options?.events ?? []\n\n const definition: Turns.Definition = {\n name,\n blocks: options?.blocks,\n specialists: options?.specialists ?? [],\n app: AppEvents.make(events)\n }\n\n type OwnEvents = Events[number][\"Type\"]\n\n const Key = Context.Service<Identifier<Name>, Service<OwnEvents>>(`effect-assistant/Assistant/${name}`)\n\n // The app's own events go to the engine encoded, as they're stored; the library's go unchanged.\n const stored = (\n input: AssistantEvent.Input | OwnEvents\n ): Effect.Effect<AssistantEvent.Input | AssistantEvent.StoredAppEvent> =>\n AssistantEvent.isInput(input) ? Effect.succeed(input) : AppEvents.encode(definition.app, input)\n\n const ChatKey = Context.Service<ChatIdentifier<Name>, Chat<OwnEvents>>(`effect-assistant/Assistant/${name}/Chat`)\n\n // On the engine and the app's `Chats` the layer was given, so every caller sees the same ones.\n const makeService = Effect.gen(function* () {\n const engine = yield* AssistantEngine\n const chats = yield* Chats.Chats\n\n return Key.of({\n submit: Effect.fn(\"Assistant.submit\")(function* (chatId: ChatId, input: AssistantEvent.Input | OwnEvents) {\n // Checked here, where the caller can handle it: the chat checks again when it stores the input.\n yield* chats.get(chatId)\n yield* engine.submit(name, chatId, yield* stored(input))\n }),\n subscribe: (chatId, options) =>\n engine\n .subscribe(name, chatId, options?.after ?? 0)\n .pipe(\n Stream.filterMap(\n Filter.fromPredicateOption((entry: AssistantEvent.Entry) => AppEvents.decodeEntry(definition.app, entry))\n )\n ),\n delete: (chatId) => engine.delete(name, chatId)\n })\n })\n\n // The signature checks what the handler uses (`CheckServices`); the layer's services, which the build\n // required, are provided to it and to every turn's stream, so what's left is the turn services.\n function toLayer<R = never, RE = never, E = never, RX = never>(\n build: (Handler<R, RE, OwnEvents> | Effect.Effect<Handler<R, RE, OwnEvents>, E, RX>) &\n Specialist.CheckServices<Exclude<HandlerServices<R | RE>, RX | ChatIdentifier<Name>>>\n ): Layer.Layer<\n Identifier<Name>,\n never,\n | AssistantEngine\n | LanguageModel.LanguageModel\n | Specialist.HandlerOf<Specs[number]>\n | Exclude<RX, Scope.Scope | CurrentChat | ChatIdentifier<Name>>\n | Specialist.ToolHandlers<R | RE>\n >\n function toLayer(\n build: Handler<any, any, any> | Effect.Effect<Handler<any, any, any>, unknown, any>\n ): Layer.Layer<Identifier<Name>, never, any> {\n return Layer.effect(\n Key,\n Effect.gen(function* () {\n const context = yield* Effect.context()\n\n const registration = yield* Turns.register(definition, {\n // The build runs per chat activation, in the chat; here it only keeps the layer's services for it.\n open: (typed) =>\n Effect.gen(function* () {\n // The chat's entries are already decoded with this assistant's events.\n const chat = ChatKey.of({ chatId: typed.chatId, entries: typed.entries, append: typed.append })\n const handler = Effect.isEffect(build) ? yield* Effect.provideService(build, ChatKey, chat) : build\n\n return (turn: Turn<any>) =>\n Stream.provideContext(toStream(handler(turn)), context).pipe(Stream.provideService(ChatKey, chat))\n }).pipe(Effect.provideContext(context), Effect.orDie)\n })\n\n yield* (yield* AssistantEngine).register(registration)\n\n return yield* makeService\n })\n )\n }\n\n // The key is a function, whose own `name` is read-only: the assistant's name replaces it.\n const named = Object.defineProperty(Key, \"name\", { value: name, writable: true })\n\n return Object.assign(named, {\n [TypeId]: TypeId,\n name,\n blocks: options?.blocks,\n specialists: options?.specialists ?? [],\n events,\n toLayer,\n layerClient: Layer.effect(Key, makeService),\n Chat: ChatKey,\n Entry: entrySchema<OwnEvents>(events)\n })\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;AAsCA,MAAM,SAAS;AAmMf,MAAM,YACJ,WAGyD,OAAO,SAAS,MAAM,IAAI,OAAO,OAAO,MAAM,IAAI;;;;;;;AAuN7G,MAAa,eAAe,MAAyB,UAAU,YAAY,GAAG,MAAM;AAIpF,SAAS,YAAY,QAAkE;CACrF,OAAO,OAAO,YACZ,OAAO,OAAO;EACZ,QAAQ;EACR,KAAK,OAAO;EACZ,IAAI,OAAO;EACX,OAAO,OAAO,MAAM,CAACA,gBAA+B,GAAG,MAAM,CAAC;CAChE,CAAC,CACH;AACF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAwCA,MAAa,QAKX,MACA,YAgB2D;CAC3D,MAAM,SAAS,SAAS,UAAU,CAAC;CAEnC,MAAM,aAA+B;EACnC;EACA,QAAQ,SAAS;EACjB,aAAa,SAAS,eAAe,CAAC;EACtC,KAAKC,OAAe,MAAM;CAC5B;CAIA,MAAM,MAAM,QAAQ,QAA8C,8BAA8B,MAAM;CAGtG,MAA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,gBAAA,CAAiB,SAAS,YAAY;GAErD,OAAO,OAAO;EAChB,CAAC,CACH;CACF;CAGA,MAAM,QAAQ,OAAO,eAAe,KAAK,QAAQ;EAAE,OAAO;EAAM,UAAU;CAAK,CAAC;CAEhF,OAAO,OAAO,OAAO,OAAO;GACzB,SAAS;EACV;EACA,QAAQ,SAAS;EACjB,aAAa,SAAS,eAAe,CAAC;EACtC;EACA;EACA,aAAa,MAAM,OAAO,KAAK,WAAW;EAC1C,MAAM;EACN,OAAO,YAAuB,MAAM;CACtC,CAAC;AACH"}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { AnyEntry, AssistantEvent_d_exports } from "./AssistantEvent.js";
|
|
2
2
|
import { RunId } from "./RunId.js";
|
|
3
3
|
declare namespace AssistantActivity_d_exports {
|
|
4
|
-
export {
|
|
4
|
+
export { SpecialistRun, State, apply, empty, fromEntries, isWorking };
|
|
5
5
|
}
|
|
6
6
|
/**
|
|
7
7
|
* Represents a specialist run that is working.
|
|
@@ -43,27 +43,21 @@ export interface State {
|
|
|
43
43
|
* @since 0.1.0
|
|
44
44
|
*/
|
|
45
45
|
export declare const empty: State;
|
|
46
|
-
/**
|
|
47
|
-
* Represents the options for folding a chat's entries into its activity.
|
|
48
|
-
*
|
|
49
|
-
* @category models
|
|
50
|
-
* @since 0.1.0
|
|
51
|
-
*/
|
|
52
|
-
export interface Options {
|
|
53
|
-
/**
|
|
54
|
-
* The tags of the app's events that wake a turn (see `AppEvent`). These events count as inputs
|
|
55
|
-
* too. Defaults to none.
|
|
56
|
-
*/
|
|
57
|
-
readonly wakes?: ReadonlySet<string> | undefined;
|
|
58
|
-
}
|
|
59
46
|
/**
|
|
60
47
|
* Returns the activity after one more entry. Fold a chat's entries in order, or use
|
|
61
48
|
* {@link fromEntries}.
|
|
62
49
|
*
|
|
50
|
+
* **Details**
|
|
51
|
+
*
|
|
52
|
+
* One of the app's events counts as an input, and a turn as about to start, when it came in
|
|
53
|
+
* through the assistant's `submit`. The log marks it so (`AssistantEvent.isAppInput`), as
|
|
54
|
+
* `ChatLog.read` returns it; `subscribe` streams it decoded, without the mark, so there the turn
|
|
55
|
+
* shows from its `TurnStarted`.
|
|
56
|
+
*
|
|
63
57
|
* @category combinators
|
|
64
58
|
* @since 0.1.0
|
|
65
59
|
*/
|
|
66
|
-
export declare const apply: (activity: State, entry: AnyEntry
|
|
60
|
+
export declare const apply: (activity: State, entry: AnyEntry) => State;
|
|
67
61
|
/**
|
|
68
62
|
* Returns what is working in a chat after the given entries, folded in order from
|
|
69
63
|
* {@link empty}.
|
|
@@ -71,7 +65,7 @@ export declare const apply: (activity: State, entry: AnyEntry, options?: Options
|
|
|
71
65
|
* @category constructors
|
|
72
66
|
* @since 0.1.0
|
|
73
67
|
*/
|
|
74
|
-
export declare const fromEntries: (entries: ReadonlyArray<AnyEntry
|
|
68
|
+
export declare const fromEntries: (entries: ReadonlyArray<AnyEntry>) => State;
|
|
75
69
|
/**
|
|
76
70
|
* Returns whether anything is working: a turn that is running or about to start, or a specialist
|
|
77
71
|
* run.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AssistantActivity.d.ts","names":[],"sources":["../src/AssistantActivity.ts"],"mappings":";;;;;;;;;;;iBAsBiB;WACN,OAAO;WACP;;WAEA;;WAEA;;;;;;;;iBASM;;WAEN;;;;;WAKA;;WAEA;;WAEA,aAAa,YAAY,OAAO;;;;;;;;qBAS9B,OAAO
|
|
1
|
+
{"version":3,"file":"AssistantActivity.d.ts","names":[],"sources":["../src/AssistantActivity.ts"],"mappings":";;;;;;;;;;;iBAsBiB;WACN,OAAO;WACP;;WAEA;;WAEA;;;;;;;;iBASM;;WAEN;;;;;WAKA;;WAEA;;WAEA,aAAa,YAAY,OAAO;;;;;;;;qBAS9B,OAAO;;;;;;;;;;;;;;;qBA8BP,QAAK,UAAc,OAAK,OAAS,aAA0B;;;;;;;;qBA4C3D,cAAW,SAAa,cAAc,cAA2B;;;;;;;;qBAUjE,YAAS,UAAc"}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { __exportAll } from "./_virtual/_rolldown/runtime.js";
|
|
2
|
-
import { isInput, isLibraryEvent, isWaking } from "./AssistantEvent.js";
|
|
2
|
+
import { isAppInput, isInput, isLibraryEvent, isWaking } from "./AssistantEvent.js";
|
|
3
3
|
import * as Predicate from "effect/Predicate";
|
|
4
4
|
import * as Match from "effect/Match";
|
|
5
5
|
//#region src/AssistantActivity.ts
|
|
@@ -45,12 +45,19 @@ const withoutRun = (activity, runId) => {
|
|
|
45
45
|
* Returns the activity after one more entry. Fold a chat's entries in order, or use
|
|
46
46
|
* {@link fromEntries}.
|
|
47
47
|
*
|
|
48
|
+
* **Details**
|
|
49
|
+
*
|
|
50
|
+
* One of the app's events counts as an input, and a turn as about to start, when it came in
|
|
51
|
+
* through the assistant's `submit`. The log marks it so (`AssistantEvent.isAppInput`), as
|
|
52
|
+
* `ChatLog.read` returns it; `subscribe` streams it decoded, without the mark, so there the turn
|
|
53
|
+
* shows from its `TurnStarted`.
|
|
54
|
+
*
|
|
48
55
|
* @category combinators
|
|
49
56
|
* @since 0.1.0
|
|
50
57
|
*/
|
|
51
|
-
const apply = (activity, entry
|
|
58
|
+
const apply = (activity, entry) => {
|
|
52
59
|
const event = entry.event;
|
|
53
|
-
if (!isLibraryEvent(event)) return
|
|
60
|
+
if (!isLibraryEvent(event)) return isAppInput(event) ? {
|
|
54
61
|
...activity,
|
|
55
62
|
queued: true,
|
|
56
63
|
lastInputSeq: entry.seq
|
|
@@ -90,7 +97,7 @@ const apply = (activity, entry, options) => {
|
|
|
90
97
|
* @category constructors
|
|
91
98
|
* @since 0.1.0
|
|
92
99
|
*/
|
|
93
|
-
const fromEntries = (entries
|
|
100
|
+
const fromEntries = (entries) => entries.reduce((activity, entry) => apply(activity, entry), empty);
|
|
94
101
|
/**
|
|
95
102
|
* Returns whether anything is working: a turn that is running or about to start, or a specialist
|
|
96
103
|
* run.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AssistantActivity.js","names":["AssistantEvent.isLibraryEvent","AssistantEvent.isInput","AssistantEvent.isWaking"],"sources":["../src/AssistantActivity.ts"],"sourcesContent":["/**\n * This module provides functions that determine what is working in a chat right now, read from\n * the chat's log.\n *\n * A {@link State} records whether the assistant is running a turn or is about to start one,\n * and which specialist runs are working. Fold a chat's entries into it with {@link apply}, or\n * build it from a list of entries with {@link fromEntries}, then check it with\n * {@link isWorking}.\n *\n * @since 0.1.0\n */\nimport * as Match from \"effect/Match\"\nimport * as Predicate from \"effect/Predicate\"\nimport * as AssistantEvent from \"./AssistantEvent.ts\"\nimport type { RunId } from \"./RunId.ts\"\n\n/**\n * Represents a specialist run that is working.\n *\n * @category models\n * @since 0.1.0\n */\nexport interface SpecialistRun {\n readonly runId: RunId\n readonly specialist: string\n /** The goal the run is working on. */\n readonly goal: string\n /** When the run started, in epoch milliseconds. */\n readonly since: number\n}\n\n/**\n * Represents what is working in a chat.\n *\n * @category models\n * @since 0.1.0\n */\nexport interface State {\n /** When the running turn started, in epoch milliseconds, or `undefined` when no turn runs. */\n readonly turnSince: number | undefined\n /**\n * Whether an input arrived that no finished turn has read. When this is true, a turn is about\n * to start.\n */\n readonly queued: boolean\n /** The `seq` of the newest input. */\n readonly lastInputSeq: number\n /** Specialist runs that are working, by run id. */\n readonly specialists: ReadonlyMap<RunId, SpecialistRun>\n}\n\n/**\n * The activity of a chat with an empty log, where nothing is working.\n *\n * @category constructors\n * @since 0.1.0\n */\nexport const empty: State = {\n turnSince: undefined,\n queued: false,\n lastInputSeq: 0,\n specialists: new Map()\n}\n\nconst withoutRun = (activity: State, runId: RunId): State => {\n if (!activity.specialists.has(runId)) return activity\n\n const specialists = new Map(activity.specialists)\n specialists.delete(runId)\n\n return { ...activity, specialists }\n}\n\n/**\n *
|
|
1
|
+
{"version":3,"file":"AssistantActivity.js","names":["AssistantEvent.isLibraryEvent","AssistantEvent.isAppInput","AssistantEvent.isInput","AssistantEvent.isWaking"],"sources":["../src/AssistantActivity.ts"],"sourcesContent":["/**\n * This module provides functions that determine what is working in a chat right now, read from\n * the chat's log.\n *\n * A {@link State} records whether the assistant is running a turn or is about to start one,\n * and which specialist runs are working. Fold a chat's entries into it with {@link apply}, or\n * build it from a list of entries with {@link fromEntries}, then check it with\n * {@link isWorking}.\n *\n * @since 0.1.0\n */\nimport * as Match from \"effect/Match\"\nimport * as Predicate from \"effect/Predicate\"\nimport * as AssistantEvent from \"./AssistantEvent.ts\"\nimport type { RunId } from \"./RunId.ts\"\n\n/**\n * Represents a specialist run that is working.\n *\n * @category models\n * @since 0.1.0\n */\nexport interface SpecialistRun {\n readonly runId: RunId\n readonly specialist: string\n /** The goal the run is working on. */\n readonly goal: string\n /** When the run started, in epoch milliseconds. */\n readonly since: number\n}\n\n/**\n * Represents what is working in a chat.\n *\n * @category models\n * @since 0.1.0\n */\nexport interface State {\n /** When the running turn started, in epoch milliseconds, or `undefined` when no turn runs. */\n readonly turnSince: number | undefined\n /**\n * Whether an input arrived that no finished turn has read. When this is true, a turn is about\n * to start.\n */\n readonly queued: boolean\n /** The `seq` of the newest input. */\n readonly lastInputSeq: number\n /** Specialist runs that are working, by run id. */\n readonly specialists: ReadonlyMap<RunId, SpecialistRun>\n}\n\n/**\n * The activity of a chat with an empty log, where nothing is working.\n *\n * @category constructors\n * @since 0.1.0\n */\nexport const empty: State = {\n turnSince: undefined,\n queued: false,\n lastInputSeq: 0,\n specialists: new Map()\n}\n\nconst withoutRun = (activity: State, runId: RunId): State => {\n if (!activity.specialists.has(runId)) return activity\n\n const specialists = new Map(activity.specialists)\n specialists.delete(runId)\n\n return { ...activity, specialists }\n}\n\n/**\n * Returns the activity after one more entry. Fold a chat's entries in order, or use\n * {@link fromEntries}.\n *\n * **Details**\n *\n * One of the app's events counts as an input, and a turn as about to start, when it came in\n * through the assistant's `submit`. The log marks it so (`AssistantEvent.isAppInput`), as\n * `ChatLog.read` returns it; `subscribe` streams it decoded, without the mark, so there the turn\n * shows from its `TurnStarted`.\n *\n * @category combinators\n * @since 0.1.0\n */\nexport const apply = (activity: State, entry: AssistantEvent.AnyEntry): State => {\n const event = entry.event\n\n if (!AssistantEvent.isLibraryEvent(event)) {\n return AssistantEvent.isAppInput(event) ? { ...activity, queued: true, lastInputSeq: entry.seq } : activity\n }\n\n if (AssistantEvent.isInput(event)) {\n // A finished run's report is an input, and ends the run.\n const current = Predicate.isTagged(event, \"SpecialistFinished\") ? withoutRun(activity, event.runId) : activity\n\n return AssistantEvent.isWaking(event) ? { ...current, queued: true, lastInputSeq: entry.seq } : current\n }\n\n return Match.value(event).pipe(\n Match.tag(\"TurnStarted\", (): State => ({ ...activity, turnSince: entry.at })),\n // Turns run one at a time, so an end closes whichever turn is open, also one a restart cut off.\n Match.tag(\"TurnCompleted\", \"TurnFailed\", (ended): State => ({\n ...activity,\n turnSince: undefined,\n queued: activity.lastInputSeq > ended.throughSeq\n })),\n Match.tag(\"SpecialistStarted\", (started): State => ({\n ...activity,\n specialists: new Map(activity.specialists).set(started.runId, {\n runId: started.runId,\n specialist: started.specialist,\n goal: started.goal,\n since: entry.at\n })\n })),\n // Deleting a chat stops everything in it, and nothing starts again.\n Match.tag(\"ChatDeleted\", (): State => ({ ...empty, lastInputSeq: activity.lastInputSeq })),\n Match.orElse(() => activity)\n )\n}\n\n/**\n * Returns what is working in a chat after the given entries, folded in order from\n * {@link empty}.\n *\n * @category constructors\n * @since 0.1.0\n */\nexport const fromEntries = (entries: ReadonlyArray<AssistantEvent.AnyEntry>): State =>\n entries.reduce((activity, entry) => apply(activity, entry), empty)\n\n/**\n * Returns whether anything is working: a turn that is running or about to start, or a specialist\n * run.\n *\n * @category guards\n * @since 0.1.0\n */\nexport const isWorking = (activity: State): boolean =>\n activity.turnSince !== undefined || activity.queued || activity.specialists.size > 0\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAyDA,MAAa,QAAe;CAC1B,WAAW,KAAA;CACX,QAAQ;CACR,cAAc;CACd,6B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AU,KAAK,GAAG,KAAK;;;;;;;;AASnE,MAAa,aAAa,aACxB,SAAS,cAAc,KAAA,KAAa,SAAS,UAAU,SAAS,YAAY,OAAO"}
|
|
@@ -9,7 +9,7 @@ import * as Stream from "effect/Stream";
|
|
|
9
9
|
import * as Sharding from "effect/cluster/Sharding";
|
|
10
10
|
import * as Scope from "effect/Scope";
|
|
11
11
|
declare namespace AssistantEngine_d_exports {
|
|
12
|
-
export { AssistantEngine, layerCluster, layerMemory };
|
|
12
|
+
export { AssistantEngine, layerCluster, layerClusterClient, layerMemory };
|
|
13
13
|
}
|
|
14
14
|
declare const AssistantEngine_base: Context.ServiceClass<AssistantEngine, "effect-assistant/AssistantEngine", {
|
|
15
15
|
/** Registers an assistant for as long as the scope is open. */
|
|
@@ -106,11 +106,49 @@ export declare const layerMemory: Layer.Layer<AssistantEngine, never, ChatLog>;
|
|
|
106
106
|
* ```
|
|
107
107
|
*
|
|
108
108
|
* @see {@link layerMemory} for running chats in one process
|
|
109
|
+
* @see {@link layerClusterClient} for a process that only reaches chats the cluster runs
|
|
109
110
|
*
|
|
110
111
|
* @category layers
|
|
111
112
|
* @since 0.1.0
|
|
112
113
|
*/
|
|
113
114
|
export declare const layerCluster: Layer.Layer<AssistantEngine, never, ChatLog | Sharding.Sharding>;
|
|
115
|
+
/**
|
|
116
|
+
* Layer that reaches chats running as cluster entities elsewhere, and runs none itself.
|
|
117
|
+
*
|
|
118
|
+
* **When to use**
|
|
119
|
+
*
|
|
120
|
+
* Use it in a process that only submits to, subscribes to, and deletes chats that the cluster's
|
|
121
|
+
* runners run with {@link layerCluster}, such as a web server that provides an assistant's
|
|
122
|
+
* `layerClient`. It needs no `ChatLog`, since the runners store the chats.
|
|
123
|
+
*
|
|
124
|
+
* **Details**
|
|
125
|
+
*
|
|
126
|
+
* The layer requires `Sharding`. An assistant's `toLayer` can't register with it: it dies, since
|
|
127
|
+
* no chats run in this process.
|
|
128
|
+
*
|
|
129
|
+
* **Example** (A web server in front of the cluster)
|
|
130
|
+
*
|
|
131
|
+
* ```ts
|
|
132
|
+
* import { Assistant, AssistantEngine } from "effect-assistant"
|
|
133
|
+
* import type * as Sharding from "effect/cluster/Sharding"
|
|
134
|
+
* import * as Layer from "effect/Layer"
|
|
135
|
+
*
|
|
136
|
+
* declare const ShardingLive: Layer.Layer<Sharding.Sharding>
|
|
137
|
+
*
|
|
138
|
+
* export const MyAssistant = Assistant.make("my-assistant")
|
|
139
|
+
*
|
|
140
|
+
* const MyAssistantClient = MyAssistant.layerClient.pipe(
|
|
141
|
+
* Layer.provide(AssistantEngine.layerClusterClient),
|
|
142
|
+
* Layer.provide(ShardingLive)
|
|
143
|
+
* )
|
|
144
|
+
* ```
|
|
145
|
+
*
|
|
146
|
+
* @see {@link layerCluster} for the runners that run the chats
|
|
147
|
+
*
|
|
148
|
+
* @category layers
|
|
149
|
+
* @since 0.3.0
|
|
150
|
+
*/
|
|
151
|
+
export declare const layerClusterClient: Layer.Layer<AssistantEngine, never, Sharding.Sharding>;
|
|
114
152
|
//#endregion
|
|
115
153
|
export { AssistantEngine_d_exports };
|
|
116
154
|
//# sourceMappingURL=AssistantEngine.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AssistantEngine.d.ts","names":[],"sources":["../src/AssistantEngine.ts"],"mappings":";;;;;;;;;;;;;;;
|
|
1
|
+
{"version":3,"file":"AssistantEngine.d.ts","names":[],"sources":["../src/AssistantEngine.ts"],"mappings":";;;;;;;;;;;;;;;WA8CuB,WAAC,cAAc,iBAAuB,OAAO,oBAAoB,MAAM;;;;;WAKzE,SACf,mBACA,QAAQ,QACR,OAAO,QAAuB,mBAC3B,OAAO;;WAEQ,YAAC,mBAAmB,QAAQ,QAAQ,kBAAkB,OAAO,OAAO;;;;;WAKvE,SAAC,mBAAmB,QAAQ,WAAW,OAAO;;;;;;;;;qBApBtD,wBAAwB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;qBA8DxB,aAAa,MAAM,MAAM,wBAAwB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;qBA8MjD,cAAc,MAAM,MAAM,wBAAwB,UAAU,SAAS;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;qBAsFrE,oBAAoB,MAAM,MAAM,wBAAwB,SAAS"}
|