effect-assistant 0.1.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (131) hide show
  1. package/README.md +1 -1
  2. package/dist/Assistant.d.ts +74 -28
  3. package/dist/Assistant.d.ts.map +1 -1
  4. package/dist/Assistant.js +11 -4
  5. package/dist/Assistant.js.map +1 -1
  6. package/dist/AssistantActivity.d.ts +10 -16
  7. package/dist/AssistantActivity.d.ts.map +1 -1
  8. package/dist/AssistantActivity.js +11 -4
  9. package/dist/AssistantActivity.js.map +1 -1
  10. package/dist/AssistantEngine.d.ts +39 -1
  11. package/dist/AssistantEngine.d.ts.map +1 -1
  12. package/dist/AssistantEngine.js +81 -28
  13. package/dist/AssistantEngine.js.map +1 -1
  14. package/dist/AssistantEvent.d.ts +52 -70
  15. package/dist/AssistantEvent.d.ts.map +1 -1
  16. package/dist/AssistantEvent.js +28 -57
  17. package/dist/AssistantEvent.js.map +1 -1
  18. package/dist/AssistantLoop.d.ts +7 -5
  19. package/dist/AssistantLoop.d.ts.map +1 -1
  20. package/dist/AssistantLoop.js +6 -3
  21. package/dist/AssistantLoop.js.map +1 -1
  22. package/dist/Block.js.map +1 -1
  23. package/dist/BlockKit.d.ts +14 -18
  24. package/dist/BlockKit.d.ts.map +1 -1
  25. package/dist/BlockKit.js +4 -7
  26. package/dist/BlockKit.js.map +1 -1
  27. package/dist/ChatLog.js +1 -1
  28. package/dist/Compaction.d.ts +8 -4
  29. package/dist/Compaction.d.ts.map +1 -1
  30. package/dist/Compaction.js +12 -15
  31. package/dist/Compaction.js.map +1 -1
  32. package/dist/CurrentChat.d.ts +2 -1
  33. package/dist/CurrentChat.d.ts.map +1 -1
  34. package/dist/CurrentChat.js.map +1 -1
  35. package/dist/Instructions.js +1 -1
  36. package/dist/Skill.d.ts +33 -0
  37. package/dist/Skill.d.ts.map +1 -1
  38. package/dist/Skill.js +27 -1
  39. package/dist/Skill.js.map +1 -1
  40. package/dist/Specialist.d.ts +4 -1
  41. package/dist/Specialist.d.ts.map +1 -1
  42. package/dist/Specialist.js.map +1 -1
  43. package/dist/Timeline.d.ts +482 -0
  44. package/dist/Timeline.d.ts.map +1 -0
  45. package/dist/Timeline.js +572 -0
  46. package/dist/Timeline.js.map +1 -0
  47. package/dist/ToolDiscovery.d.ts +4 -5
  48. package/dist/ToolDiscovery.d.ts.map +1 -1
  49. package/dist/ToolDiscovery.js +5 -4
  50. package/dist/ToolDiscovery.js.map +1 -1
  51. package/dist/ToolFailure.d.ts +1 -1
  52. package/dist/ToolFailure.js +1 -1
  53. package/dist/ToolFailure.js.map +1 -1
  54. package/dist/index.d.ts +2 -5
  55. package/dist/index.js +4 -7
  56. package/dist/internal/chat.js +11 -37
  57. package/dist/internal/chat.js.map +1 -1
  58. package/dist/internal/events.d.ts +15 -16
  59. package/dist/internal/events.d.ts.map +1 -1
  60. package/dist/internal/events.js +29 -28
  61. package/dist/internal/events.js.map +1 -1
  62. package/dist/internal/history.js +26 -58
  63. package/dist/internal/history.js.map +1 -1
  64. package/dist/internal/input.js +7 -0
  65. package/dist/internal/input.js.map +1 -0
  66. package/dist/internal/loop.d.ts +2 -2
  67. package/dist/internal/loop.d.ts.map +1 -1
  68. package/dist/internal/loop.js +21 -56
  69. package/dist/internal/loop.js.map +1 -1
  70. package/dist/internal/skills.js +6 -1
  71. package/dist/internal/skills.js.map +1 -1
  72. package/dist/internal/specialists.js +7 -7
  73. package/dist/internal/specialists.js.map +1 -1
  74. package/dist/internal/toolkit.d.ts +6 -20
  75. package/dist/internal/toolkit.d.ts.map +1 -1
  76. package/dist/internal/toolkit.js +10 -31
  77. package/dist/internal/toolkit.js.map +1 -1
  78. package/dist/internal/tools.js +2 -21
  79. package/dist/internal/tools.js.map +1 -1
  80. package/dist/internal/turns.d.ts.map +1 -1
  81. package/dist/internal/turns.js +24 -43
  82. package/dist/internal/turns.js.map +1 -1
  83. package/package.json +3 -2
  84. package/src/Assistant.ts +75 -29
  85. package/src/AssistantActivity.ts +11 -18
  86. package/src/AssistantEngine.ts +110 -43
  87. package/src/AssistantEvent.ts +46 -74
  88. package/src/AssistantLoop.ts +7 -5
  89. package/src/Block.ts +1 -1
  90. package/src/BlockKit.ts +18 -31
  91. package/src/Compaction.ts +8 -21
  92. package/src/CurrentChat.ts +2 -1
  93. package/src/Skill.ts +34 -1
  94. package/src/Specialist.ts +4 -1
  95. package/src/Timeline.ts +861 -0
  96. package/src/ToolDiscovery.ts +5 -13
  97. package/src/ToolFailure.ts +1 -1
  98. package/src/index.ts +5 -20
  99. package/src/internal/chat.ts +8 -57
  100. package/src/internal/events.ts +43 -43
  101. package/src/internal/history.ts +36 -101
  102. package/src/internal/input.ts +3 -0
  103. package/src/internal/loop.ts +31 -101
  104. package/src/internal/skills.ts +11 -0
  105. package/src/internal/specialists.ts +6 -9
  106. package/src/internal/toolkit.ts +17 -60
  107. package/src/internal/tools.ts +2 -53
  108. package/src/internal/turns.ts +37 -79
  109. package/dist/AppEvent.d.ts +0 -111
  110. package/dist/AppEvent.d.ts.map +0 -1
  111. package/dist/AppEvent.js +0 -71
  112. package/dist/AppEvent.js.map +0 -1
  113. package/dist/ChatView.d.ts +0 -459
  114. package/dist/ChatView.d.ts.map +0 -1
  115. package/dist/ChatView.js +0 -496
  116. package/dist/ChatView.js.map +0 -1
  117. package/dist/Decision.d.ts +0 -145
  118. package/dist/Decision.d.ts.map +0 -1
  119. package/dist/Decision.js +0 -178
  120. package/dist/Decision.js.map +0 -1
  121. package/dist/DecisionId.d.ts +0 -37
  122. package/dist/DecisionId.d.ts.map +0 -1
  123. package/dist/DecisionId.js +0 -44
  124. package/dist/DecisionId.js.map +0 -1
  125. package/dist/internal/decisions.js +0 -66
  126. package/dist/internal/decisions.js.map +0 -1
  127. package/src/AppEvent.ts +0 -134
  128. package/src/ChatView.ts +0 -658
  129. package/src/Decision.ts +0 -241
  130. package/src/DecisionId.ts +0 -46
  131. package/src/internal/decisions.ts +0 -131
@@ -0,0 +1,482 @@
1
+ import { AnyEntry, AppEventSchema, AssistantEvent_d_exports } from "./AssistantEvent.js";
2
+ import { Assistant_d_exports, Entry } from "./Assistant.js";
3
+ import * as Schema from "effect/Schema";
4
+ import * as Stream from "effect/Stream";
5
+ declare namespace Timeline_d_exports {
6
+ export { Activity, AssistantMessage, Block, BlockStatus, Change, ChangesOptions, Definition, EventItem, Item, ShowOptions, Shown, SpecialistWorking, Timeline, UserMessage, apply, changes, empty, fold, fromEntries, isIdle, snapshot };
7
+ }
8
+ /**
9
+ * Schema for the status of a block. A block is `"open"` while it accepts an answer, `"answered"`
10
+ * once the user has used it, and `"closed"` when it closed without an answer because the user
11
+ * answered in words or moved on.
12
+ *
13
+ * @category schemas
14
+ * @since 0.1.0
15
+ */
16
+ export declare const BlockStatus: Schema.Literals<readonly ["open", "answered", "closed"]>;
17
+ /**
18
+ * The status of a block.
19
+ *
20
+ * @category models
21
+ * @since 0.1.0
22
+ */
23
+ export type BlockStatus = typeof BlockStatus.Type;
24
+ /**
25
+ * Schema for a block on an assistant's message. It holds the block's kind, its props (JSON, as
26
+ * the block's props schema encodes them), its status, and the user's answer (JSON, as the block's
27
+ * answer schema encodes it).
28
+ *
29
+ * @category schemas
30
+ * @since 0.1.0
31
+ */
32
+ export declare const Block: Schema.Struct<{
33
+ readonly kind: Schema.String;
34
+ readonly props: Schema.Codec<Schema.Json, Schema.Json, never, never>;
35
+ readonly status: Schema.Literals<readonly ["open", "answered", "closed"]>;
36
+ readonly answer: Schema.optional<Schema.Codec<Schema.Json, Schema.Json, never, never>>;
37
+ }>;
38
+ /**
39
+ * Represents a block on an assistant's message.
40
+ *
41
+ * @category models
42
+ * @since 0.1.0
43
+ */
44
+ export type Block = typeof Block.Type;
45
+ /**
46
+ * Schema for a message the user sent. `at` is when it was stored, in epoch
47
+ * milliseconds.
48
+ *
49
+ * @category schemas
50
+ * @since 0.1.0
51
+ */
52
+ export declare const UserMessage: Schema.TaggedStruct<"UserMessage", {
53
+ readonly id: Schema.brand<Schema.String, "~effect-assistant/InputId">;
54
+ readonly text: Schema.String;
55
+ readonly attachments: Schema.optional<Schema.$Array<Schema.Struct<{
56
+ readonly fileId: Schema.String;
57
+ readonly name: Schema.String;
58
+ readonly mediaType: Schema.String;
59
+ readonly size: Schema.Finite;
60
+ }>>>;
61
+ readonly at: Schema.Finite;
62
+ }>;
63
+ /**
64
+ * Represents a message the user sent.
65
+ *
66
+ * @category models
67
+ * @since 0.1.0
68
+ */
69
+ export type UserMessage = typeof UserMessage.Type;
70
+ /**
71
+ * Schema for a message the assistant sent the user, including its block if it has one.
72
+ *
73
+ * @category schemas
74
+ * @since 0.1.0
75
+ */
76
+ export declare const AssistantMessage: Schema.TaggedStruct<"AssistantMessage", {
77
+ readonly id: Schema.String;
78
+ readonly text: Schema.String;
79
+ readonly at: Schema.Finite;
80
+ readonly block: Schema.optional<Schema.Struct<{
81
+ readonly kind: Schema.String;
82
+ readonly props: Schema.Codec<Schema.Json, Schema.Json, never, never>;
83
+ readonly status: Schema.Literals<readonly ["open", "answered", "closed"]>;
84
+ readonly answer: Schema.optional<Schema.Codec<Schema.Json, Schema.Json, never, never>>;
85
+ }>>;
86
+ }>;
87
+ /**
88
+ * Represents a message the assistant sent the user.
89
+ *
90
+ * @category models
91
+ * @since 0.1.0
92
+ */
93
+ export type AssistantMessage = typeof AssistantMessage.Type;
94
+ /**
95
+ * Represents one of the app's own events in the timeline: an item between the messages, in the
96
+ * order the chat stored it.
97
+ *
98
+ * **Details**
99
+ *
100
+ * The item's `_tag` is the event's, so a match over a timeline's items takes a handler per event
101
+ * beside `UserMessage` and `AssistantMessage`. The event, decoded with its schema, is in `event`,
102
+ * and the item's own fields sit beside it rather than spread into it: an event may have an `id` or
103
+ * an `at` of its own, and a class instance keeps its prototype. `id` is fixed per entry of the
104
+ * chat's log, and `at` is when the event was stored, in epoch milliseconds.
105
+ *
106
+ * @category models
107
+ * @since 0.3.0
108
+ */
109
+ export type EventItem<E> = E extends {
110
+ readonly _tag: infer Tag extends string;
111
+ } ? {
112
+ readonly _tag: Tag;
113
+ readonly id: string;
114
+ readonly at: number;
115
+ readonly event: E;
116
+ } : never;
117
+ /**
118
+ * Represents an item in the chat's timeline: a message the user or the assistant sent, or one of
119
+ * the app's own events it shows (see {@link EventItem}).
120
+ *
121
+ * **Example** (Showing each item of a chat)
122
+ *
123
+ * ```ts
124
+ * import { Timeline } from "effect-assistant"
125
+ * import * as Match from "effect/Match"
126
+ * import * as Schema from "effect/Schema"
127
+ *
128
+ * class DailyBudgetReached extends Schema.TaggedClass<DailyBudgetReached>()("DailyBudgetReached", {}) {}
129
+ *
130
+ * const line = (item: Timeline.Item<DailyBudgetReached>) =>
131
+ * Match.valueTags(item, {
132
+ * UserMessage: (sent) => `You: ${sent.text}`,
133
+ * AssistantMessage: (sent) => `Assistant: ${sent.text}`,
134
+ * DailyBudgetReached: () => "You've reached today's budget."
135
+ * })
136
+ * ```
137
+ *
138
+ * @category models
139
+ * @since 0.3.0
140
+ */
141
+ export type Item<Events = never> = UserMessage | AssistantMessage | EventItem<Events>;
142
+ /**
143
+ * Schema for a specialist that is working for the assistant. It holds the specialist's name and
144
+ * when it started, in epoch milliseconds.
145
+ *
146
+ * @category schemas
147
+ * @since 0.1.0
148
+ */
149
+ export declare const SpecialistWorking: Schema.Struct<{
150
+ readonly specialist: Schema.String;
151
+ readonly since: Schema.Finite;
152
+ }>;
153
+ /**
154
+ * Represents a specialist that is working for the assistant.
155
+ *
156
+ * @category models
157
+ * @since 0.1.0
158
+ */
159
+ export type SpecialistWorking = typeof SpecialistWorking.Type;
160
+ /**
161
+ * Schema for what is working in the chat.
162
+ *
163
+ * `working` is true while the assistant runs a turn, or when a turn is about to start for
164
+ * something that arrived. `specialists` lists the specialists that are working. When the chat is
165
+ * idle, nothing is working, even if a workflow is waiting on a timer or on the user.
166
+ *
167
+ * @category schemas
168
+ * @since 0.1.0
169
+ */
170
+ export declare const Activity: Schema.Struct<{
171
+ readonly working: Schema.Boolean;
172
+ readonly specialists: Schema.$Array<Schema.Struct<{
173
+ readonly specialist: Schema.String;
174
+ readonly since: Schema.Finite;
175
+ }>>;
176
+ }>;
177
+ /**
178
+ * Represents what is working in the chat.
179
+ *
180
+ * @category models
181
+ * @since 0.1.0
182
+ */
183
+ export type Activity = typeof Activity.Type;
184
+ /**
185
+ * Represents a chat's timeline: the chat as its user sees it, typed with the app's events it shows.
186
+ * It holds the chat's items in order, what is working in it, whether the last turn failed, and
187
+ * whether the chat was deleted. `seq` is the position in the chat's log that the timeline is up to.
188
+ *
189
+ * @category models
190
+ * @since 0.3.0
191
+ */
192
+ export interface Timeline<Events = never> {
193
+ readonly seq: number;
194
+ readonly items: ReadonlyArray<Item<Events>>;
195
+ readonly activity: Activity;
196
+ /** Whether the last turn failed with no message after it. */
197
+ readonly failed: boolean;
198
+ /** Whether the chat was deleted. A deleted chat accepts nothing more, and its stream ends. */
199
+ readonly deleted: boolean;
200
+ }
201
+ /**
202
+ * Represents a change to a chat's timeline, typed with the app's events it shows.
203
+ *
204
+ * **Details**
205
+ *
206
+ * A change holds each item that is new or changed (for example, a message whose block was
207
+ * answered or closed), in full. It also holds the current activity, whether the last turn failed,
208
+ * and whether the chat was deleted. `seq` is the position in the chat's log that the change is up
209
+ * to.
210
+ *
211
+ * @category models
212
+ * @since 0.3.0
213
+ */
214
+ export interface Change<Events = never> {
215
+ readonly seq: number;
216
+ readonly items: ReadonlyArray<Item<Events>>;
217
+ readonly activity: Activity;
218
+ readonly failed: boolean;
219
+ readonly deleted: boolean;
220
+ }
221
+ /**
222
+ * Creates the schema for the changes to a chat's timeline, with items for the given app events,
223
+ * in JSON.
224
+ *
225
+ * **Details**
226
+ *
227
+ * It is the success schema of the RPC that streams a chat's timeline: name the events the chat's
228
+ * users see, the same ones the handler passes to {@link changes} as `show`. Each item decodes its
229
+ * event with that event's schema, so the client gets `Change<Events>` typed with them and folds it
230
+ * with {@link fold}, no definition needed. `Change()` carries no app events.
231
+ *
232
+ * A handler whose changes show an event the schema doesn't name doesn't compile.
233
+ *
234
+ * **Example** (An RPC that streams a chat's timeline, with the budget notice)
235
+ *
236
+ * ```ts
237
+ * import { ChatId, Timeline } from "effect-assistant"
238
+ * import * as Rpc from "effect/rpc/Rpc"
239
+ * import * as Schema from "effect/Schema"
240
+ *
241
+ * class DailyBudgetReached extends Schema.TaggedClass<DailyBudgetReached>()("DailyBudgetReached", {}) {}
242
+ *
243
+ * const Subscribe = Rpc.make("Subscribe", {
244
+ * payload: { chatId: ChatId.ChatId },
245
+ * success: Timeline.Change([DailyBudgetReached]),
246
+ * stream: true
247
+ * })
248
+ * ```
249
+ *
250
+ * @category schemas
251
+ * @since 0.3.0
252
+ */
253
+ export declare const Change: <const Events extends ReadonlyArray<AppEventSchema> = readonly []>(events?: Events) => Schema.Codec<Change<Events[number]["Type"]>, Schema.Json>;
254
+ /**
255
+ * The timeline of a chat before anything has happened in it, with no items and nothing working.
256
+ * Use it as the starting point when folding changes with {@link apply}.
257
+ *
258
+ * @category constructors
259
+ * @since 0.1.0
260
+ */
261
+ export declare const empty: Timeline;
262
+ /**
263
+ * Returns the timeline after a change.
264
+ *
265
+ * **Details**
266
+ *
267
+ * Each item in the change replaces the item with the same id, in place, and new items are added
268
+ * after the rest. The change's activity, `failed`, and `deleted` replace the timeline's. Applying a
269
+ * change twice changes nothing, so a client that resubscribes from the start can fold again and get
270
+ * the same timeline.
271
+ *
272
+ * The timeline is typed with the change's events, as its RPC's schema decoded them (see
273
+ * {@link Change}). To fold a stream of changes, use {@link fold}: in
274
+ * `Stream.scan(() => Timeline.empty, Timeline.apply)`, TypeScript fixes the state's type from
275
+ * `empty` before it sees the changes' events, so that form needs the timeline's type written out.
276
+ *
277
+ * **Example** (Folding changes into your own state)
278
+ *
279
+ * ```ts
280
+ * import { Timeline } from "effect-assistant"
281
+ *
282
+ * interface State<Events> {
283
+ * readonly timeline: Timeline.Timeline<Events>
284
+ * readonly unread: number
285
+ * }
286
+ *
287
+ * const next = <Events>(state: State<Events>, change: Timeline.Change<Events>): State<Events> => ({
288
+ * timeline: Timeline.apply(state.timeline, change),
289
+ * unread: state.unread + change.items.length
290
+ * })
291
+ * ```
292
+ *
293
+ * @category combinators
294
+ * @since 0.1.0
295
+ */
296
+ export declare const apply: <Events>(timeline: Timeline<Events>, change: Change<Events>) => Timeline<Events>;
297
+ /**
298
+ * Returns the timelines a stream of changes folds to, from {@link empty}, typed with the changes'
299
+ * events.
300
+ *
301
+ * **Example** (A chat's timeline on the client)
302
+ *
303
+ * ```ts
304
+ * import { Timeline } from "effect-assistant"
305
+ * import * as Schema from "effect/Schema"
306
+ * import * as Stream from "effect/Stream"
307
+ *
308
+ * class DailyBudgetReached extends Schema.TaggedClass<DailyBudgetReached>()("DailyBudgetReached", {}) {}
309
+ *
310
+ * // The stream of an RPC whose success schema is `Timeline.Change([DailyBudgetReached])`.
311
+ * declare const changes: Stream.Stream<Timeline.Change<DailyBudgetReached>>
312
+ *
313
+ * const timelines = changes.pipe(Timeline.fold) // Stream<Timeline.Timeline<DailyBudgetReached>>
314
+ * ```
315
+ *
316
+ * @category combinators
317
+ * @since 0.3.0
318
+ */
319
+ export declare const fold: <Events, E, R>(changes: Stream.Stream<Change<Events>, E, R>) => Stream.Stream<Timeline<Events>, E, R>;
320
+ /**
321
+ * Returns whether nothing is working in the chat: no turn is running or about to start, and no
322
+ * specialist is working.
323
+ *
324
+ * @category guards
325
+ * @since 0.1.0
326
+ */
327
+ export declare const isIdle: <Events>(timeline: Timeline<Events>) => boolean;
328
+ /**
329
+ * Represents the options for building a chat's timeline from its log.
330
+ *
331
+ * @category models
332
+ * @since 0.3.0
333
+ */
334
+ export interface ShowOptions<Show extends ReadonlyArray<AppEventSchema>> {
335
+ /**
336
+ * The schemas of the app's events the chat's users see. Each becomes an item in the timeline;
337
+ * the app's other events are left out. Defaults to none.
338
+ */
339
+ readonly show?: Show | undefined;
340
+ }
341
+ /**
342
+ * Represents the schemas an assistant's timeline may show: some of its own events.
343
+ *
344
+ * @category utility types
345
+ * @since 0.3.0
346
+ */
347
+ export type Shown<Events> = ReadonlyArray<AppEventSchema & {
348
+ readonly Type: Events;
349
+ }>;
350
+ /**
351
+ * Returns a chat's whole timeline as one change, built from the entries of its log.
352
+ *
353
+ * **When to use**
354
+ *
355
+ * Use it on the server for an RPC that returns a whole chat at once: its success schema is
356
+ * {@link Change} with the same events as `show`, and the client applies it to {@link empty}.
357
+ *
358
+ * **Details**
359
+ *
360
+ * Only the app's events in `show` become items; without it, the timeline has none.
361
+ *
362
+ * @category constructors
363
+ * @since 0.3.0
364
+ */
365
+ export declare const snapshot: {
366
+ (entries: ReadonlyArray<AnyEntry>): Change;
367
+ <Events, const Show extends Shown<Events> = readonly []>(entries: ReadonlyArray<AnyEntry>, definition: Definition<Events>, options?: ShowOptions<Show>): Change<Show[number]["Type"]>;
368
+ };
369
+ /**
370
+ * Returns a chat's timeline, built from the entries of its log.
371
+ *
372
+ * **When to use**
373
+ *
374
+ * Use it in server-side code that reads a whole chat. A client gets the timeline as a stream from
375
+ * {@link changes} instead, and an RPC that returns a whole chat sends a {@link snapshot}.
376
+ *
377
+ * **Details**
378
+ *
379
+ * Only the app's events in `show` become items, typed with their schemas; without it, the timeline
380
+ * has none.
381
+ *
382
+ * **Example** (Building a chat's timeline from its log)
383
+ *
384
+ * ```ts
385
+ * import { type ChatId, ChatLog, Timeline } from "effect-assistant"
386
+ * import * as Effect from "effect/Effect"
387
+ *
388
+ * const timelineOf = (chatId: ChatId.ChatId) =>
389
+ * ChatLog.ChatLog.use((log) => log.read(chatId)).pipe(
390
+ * Effect.map((entries) => Timeline.fromEntries(entries))
391
+ * )
392
+ * ```
393
+ *
394
+ * @see {@link changes} for a stream of changes instead of a whole timeline
395
+ *
396
+ * @category constructors
397
+ * @since 0.1.0
398
+ */
399
+ export declare const fromEntries: {
400
+ (entries: ReadonlyArray<AnyEntry>): Timeline;
401
+ <Events, const Show extends Shown<Events> = readonly []>(entries: ReadonlyArray<AnyEntry>, definition: Definition<Events>, options?: ShowOptions<Show>): Timeline<Show[number]["Type"]>;
402
+ };
403
+ /**
404
+ * Represents an assistant's definition as the timeline reads it. An assistant made with
405
+ * `Assistant.make` fits it.
406
+ *
407
+ * @category models
408
+ * @since 0.1.0
409
+ */
410
+ export interface Definition<Events = never> {
411
+ /** The schemas of the assistant's events. */
412
+ readonly events: ReadonlyArray<AppEventSchema>;
413
+ /** The schema of the chat's entries, typed with the assistant's events: it types `show`. */
414
+ readonly Entry: Schema.Codec<Entry<Events>, Schema.Json>;
415
+ }
416
+ /**
417
+ * Represents the options accepted by {@link changes}.
418
+ *
419
+ * @category models
420
+ * @since 0.3.0
421
+ */
422
+ export interface ChangesOptions<Show extends ReadonlyArray<AppEventSchema> = readonly []> extends ShowOptions<Show> {
423
+ /**
424
+ * The `seq` up to which the client already has the timeline. The result contains only the
425
+ * changes after it. Defaults to `0`, every change.
426
+ */
427
+ readonly after?: number | undefined;
428
+ }
429
+ /**
430
+ * Returns the stream of changes to a chat's timeline that its entries produce, for a chat client.
431
+ *
432
+ * **Details**
433
+ *
434
+ * The entries must stream from the start of the chat's log, as `assistant.subscribe(chatId)` does
435
+ * without `after`. The fold reads every entry, because a message depends on earlier entries: its
436
+ * block is answered or closed later. `after` only skips emitting the changes up to that `seq`, so a
437
+ * client that has the timeline up to a `seq` receives only what changed since. Pass `after` here,
438
+ * not to `subscribe`: a subscription that starts after a `seq` gives the fold an incomplete log,
439
+ * and the changes it produces are wrong.
440
+ *
441
+ * Each chunk of entries becomes one change; the stored entries, for example, arrive as one chunk.
442
+ *
443
+ * **Only the app's events in `show` reach the client.** Each one becomes an item between the
444
+ * messages (see {@link EventItem}); the app's other events, like a summary of the chat for the
445
+ * model, are left out. Without `show`, none are shown. The changes are typed with the events shown,
446
+ * so the RPC's success schema names the same ones (`Timeline.Change([...])`), and a handler that
447
+ * shows one the schema doesn't name doesn't compile. A timeline shows a turn as working once it
448
+ * starts, or from the user's input.
449
+ *
450
+ * **Example** (Streaming a chat's timeline to a client, with the one app event its user sees)
451
+ *
452
+ * ```ts
453
+ * import { Assistant, type ChatId, Timeline } from "effect-assistant"
454
+ * import * as Schema from "effect/Schema"
455
+ * import * as Stream from "effect/Stream"
456
+ *
457
+ * class Summary extends Schema.TaggedClass<Summary>()("Summary", { text: Schema.String, throughSeq: Schema.Int }) {}
458
+ * class DailyBudgetReached extends Schema.TaggedClass<DailyBudgetReached>()("DailyBudgetReached", {}) {}
459
+ *
460
+ * const MyAssistant = Assistant.make("my-assistant", { events: [Summary, DailyBudgetReached] })
461
+ *
462
+ * // The summary is for the model: only the budget notice reaches the user.
463
+ * const watch = (chatId: ChatId.ChatId, after: number) =>
464
+ * Stream.unwrap(
465
+ * MyAssistant.useSync((assistant) =>
466
+ * assistant.subscribe(chatId).pipe(Timeline.changes(MyAssistant, { show: [DailyBudgetReached], after }))
467
+ * )
468
+ * )
469
+ * ```
470
+ *
471
+ * @see {@link fold} for folding the changes into a timeline on the client
472
+ *
473
+ * @category utils
474
+ * @since 0.3.0
475
+ */
476
+ export declare const changes: {
477
+ <Events, const Show extends Shown<Events> = readonly []>(definition: Definition<Events>, options?: ChangesOptions<Show>): <E, R>(entries: Stream.Stream<AnyEntry, E, R>) => Stream.Stream<Change<Show[number]["Type"]>, E, R>;
478
+ <E, R, Events, const Show extends Shown<Events> = readonly []>(entries: Stream.Stream<AnyEntry, E, R>, definition: Definition<Events>, options?: ChangesOptions<Show>): Stream.Stream<Change<Show[number]["Type"]>, E, R>;
479
+ };
480
+ //#endregion
481
+ export { Timeline_d_exports };
482
+ //# sourceMappingURL=Timeline.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Timeline.d.ts","names":[],"sources":["../src/Timeline.ts"],"mappings":";;;;;;;;;;;;;;;qBAyCa,aAAW,OAAA;;;;;;;YAQZ,qBAAqB,YAAY;;;;;;;;;qBAUhC,OAAK,OAAA;;;;;;;;;;;;YAaN,eAAe,MAAM;;;;;;;;qBASpB,aAAW,OAAA;;;;;;;;;;;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