effect-assistant 0.0.0-stage → 0.1.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 (185) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +22 -2
  3. package/dist/AppEvent.d.ts +111 -0
  4. package/dist/AppEvent.d.ts.map +1 -0
  5. package/dist/AppEvent.js +71 -0
  6. package/dist/AppEvent.js.map +1 -0
  7. package/dist/Assistant.d.ts +403 -0
  8. package/dist/Assistant.d.ts.map +1 -0
  9. package/dist/Assistant.js +126 -0
  10. package/dist/Assistant.js.map +1 -0
  11. package/dist/AssistantActivity.d.ts +85 -0
  12. package/dist/AssistantActivity.d.ts.map +1 -0
  13. package/dist/AssistantActivity.js +105 -0
  14. package/dist/AssistantActivity.js.map +1 -0
  15. package/dist/AssistantEngine.d.ts +116 -0
  16. package/dist/AssistantEngine.d.ts.map +1 -0
  17. package/dist/AssistantEngine.js +255 -0
  18. package/dist/AssistantEngine.js.map +1 -0
  19. package/dist/AssistantEvent.d.ts +614 -0
  20. package/dist/AssistantEvent.d.ts.map +1 -0
  21. package/dist/AssistantEvent.js +523 -0
  22. package/dist/AssistantEvent.js.map +1 -0
  23. package/dist/AssistantLoop.d.ts +171 -0
  24. package/dist/AssistantLoop.d.ts.map +1 -0
  25. package/dist/AssistantLoop.js +108 -0
  26. package/dist/AssistantLoop.js.map +1 -0
  27. package/dist/AssistantToolCall.d.ts +32 -0
  28. package/dist/AssistantToolCall.d.ts.map +1 -0
  29. package/dist/AssistantToolCall.js +37 -0
  30. package/dist/AssistantToolCall.js.map +1 -0
  31. package/dist/Block.d.ts +69 -0
  32. package/dist/Block.d.ts.map +1 -0
  33. package/dist/Block.js +39 -0
  34. package/dist/Block.js.map +1 -0
  35. package/dist/BlockKit.d.ts +184 -0
  36. package/dist/BlockKit.d.ts.map +1 -0
  37. package/dist/BlockKit.js +151 -0
  38. package/dist/BlockKit.js.map +1 -0
  39. package/dist/ChatId.d.ts +31 -0
  40. package/dist/ChatId.d.ts.map +1 -0
  41. package/dist/ChatId.js +36 -0
  42. package/dist/ChatId.js.map +1 -0
  43. package/dist/ChatLog.d.ts +109 -0
  44. package/dist/ChatLog.d.ts.map +1 -0
  45. package/dist/ChatLog.js +177 -0
  46. package/dist/ChatLog.js.map +1 -0
  47. package/dist/ChatView.d.ts +459 -0
  48. package/dist/ChatView.d.ts.map +1 -0
  49. package/dist/ChatView.js +496 -0
  50. package/dist/ChatView.js.map +1 -0
  51. package/dist/Chats.d.ts +110 -0
  52. package/dist/Chats.d.ts.map +1 -0
  53. package/dist/Chats.js +98 -0
  54. package/dist/Chats.js.map +1 -0
  55. package/dist/Compaction.d.ts +105 -0
  56. package/dist/Compaction.d.ts.map +1 -0
  57. package/dist/Compaction.js +133 -0
  58. package/dist/Compaction.js.map +1 -0
  59. package/dist/CurrentChat.d.ts +40 -0
  60. package/dist/CurrentChat.d.ts.map +1 -0
  61. package/dist/CurrentChat.js +33 -0
  62. package/dist/CurrentChat.js.map +1 -0
  63. package/dist/Decision.d.ts +145 -0
  64. package/dist/Decision.d.ts.map +1 -0
  65. package/dist/Decision.js +178 -0
  66. package/dist/Decision.js.map +1 -0
  67. package/dist/DecisionId.d.ts +37 -0
  68. package/dist/DecisionId.d.ts.map +1 -0
  69. package/dist/DecisionId.js +44 -0
  70. package/dist/DecisionId.js.map +1 -0
  71. package/dist/InputId.d.ts +38 -0
  72. package/dist/InputId.d.ts.map +1 -0
  73. package/dist/InputId.js +43 -0
  74. package/dist/InputId.js.map +1 -0
  75. package/dist/Instructions.d.ts +56 -0
  76. package/dist/Instructions.d.ts.map +1 -0
  77. package/dist/Instructions.js +50 -0
  78. package/dist/Instructions.js.map +1 -0
  79. package/dist/Memory.d.ts +190 -0
  80. package/dist/Memory.d.ts.map +1 -0
  81. package/dist/Memory.js +216 -0
  82. package/dist/Memory.js.map +1 -0
  83. package/dist/RunId.d.ts +41 -0
  84. package/dist/RunId.d.ts.map +1 -0
  85. package/dist/RunId.js +46 -0
  86. package/dist/RunId.js.map +1 -0
  87. package/dist/Skill.d.ts +72 -0
  88. package/dist/Skill.d.ts.map +1 -0
  89. package/dist/Skill.js +58 -0
  90. package/dist/Skill.js.map +1 -0
  91. package/dist/Specialist.d.ts +267 -0
  92. package/dist/Specialist.d.ts.map +1 -0
  93. package/dist/Specialist.js +119 -0
  94. package/dist/Specialist.js.map +1 -0
  95. package/dist/ToolDiscovery.d.ts +108 -0
  96. package/dist/ToolDiscovery.d.ts.map +1 -0
  97. package/dist/ToolDiscovery.js +139 -0
  98. package/dist/ToolDiscovery.js.map +1 -0
  99. package/dist/ToolFailure.d.ts +66 -0
  100. package/dist/ToolFailure.d.ts.map +1 -0
  101. package/dist/ToolFailure.js +79 -0
  102. package/dist/ToolFailure.js.map +1 -0
  103. package/dist/_virtual/_rolldown/runtime.js +13 -0
  104. package/dist/index.d.ts +26 -0
  105. package/dist/index.js +26 -0
  106. package/dist/internal/chat.js +202 -0
  107. package/dist/internal/chat.js.map +1 -0
  108. package/dist/internal/decisions.js +66 -0
  109. package/dist/internal/decisions.js.map +1 -0
  110. package/dist/internal/events.d.ts +46 -0
  111. package/dist/internal/events.d.ts.map +1 -0
  112. package/dist/internal/events.js +65 -0
  113. package/dist/internal/events.js.map +1 -0
  114. package/dist/internal/history.js +185 -0
  115. package/dist/internal/history.js.map +1 -0
  116. package/dist/internal/instructions.js +11 -0
  117. package/dist/internal/instructions.js.map +1 -0
  118. package/dist/internal/loop.d.ts +73 -0
  119. package/dist/internal/loop.d.ts.map +1 -0
  120. package/dist/internal/loop.js +166 -0
  121. package/dist/internal/loop.js.map +1 -0
  122. package/dist/internal/schema.js +14 -0
  123. package/dist/internal/schema.js.map +1 -0
  124. package/dist/internal/skills.js +28 -0
  125. package/dist/internal/skills.js.map +1 -0
  126. package/dist/internal/specialists.js +273 -0
  127. package/dist/internal/specialists.js.map +1 -0
  128. package/dist/internal/telemetry.js +85 -0
  129. package/dist/internal/telemetry.js.map +1 -0
  130. package/dist/internal/toolkit.d.ts +76 -0
  131. package/dist/internal/toolkit.d.ts.map +1 -0
  132. package/dist/internal/toolkit.js +129 -0
  133. package/dist/internal/toolkit.js.map +1 -0
  134. package/dist/internal/tools.js +132 -0
  135. package/dist/internal/tools.js.map +1 -0
  136. package/dist/internal/turns.d.ts +64 -0
  137. package/dist/internal/turns.d.ts.map +1 -0
  138. package/dist/internal/turns.js +221 -0
  139. package/dist/internal/turns.js.map +1 -0
  140. package/dist/internal/uuid.js +45 -0
  141. package/dist/internal/uuid.js.map +1 -0
  142. package/dist/internal/writer.d.ts +12 -0
  143. package/dist/internal/writer.d.ts.map +1 -0
  144. package/package.json +51 -3
  145. package/src/AppEvent.ts +134 -0
  146. package/src/Assistant.ts +589 -0
  147. package/src/AssistantActivity.ts +150 -0
  148. package/src/AssistantEngine.ts +343 -0
  149. package/src/AssistantEvent.ts +635 -0
  150. package/src/AssistantLoop.ts +207 -0
  151. package/src/AssistantToolCall.ts +40 -0
  152. package/src/Block.ts +84 -0
  153. package/src/BlockKit.ts +311 -0
  154. package/src/ChatId.ts +36 -0
  155. package/src/ChatLog.ts +247 -0
  156. package/src/ChatView.ts +658 -0
  157. package/src/Chats.ts +122 -0
  158. package/src/Compaction.ts +224 -0
  159. package/src/CurrentChat.ts +47 -0
  160. package/src/Decision.ts +241 -0
  161. package/src/DecisionId.ts +46 -0
  162. package/src/InputId.ts +43 -0
  163. package/src/Instructions.ts +72 -0
  164. package/src/Memory.ts +276 -0
  165. package/src/RunId.ts +46 -0
  166. package/src/Skill.ts +91 -0
  167. package/src/Specialist.ts +355 -0
  168. package/src/ToolDiscovery.ts +187 -0
  169. package/src/ToolFailure.ts +74 -0
  170. package/src/index.ts +130 -0
  171. package/src/internal/chat.ts +372 -0
  172. package/src/internal/decisions.ts +131 -0
  173. package/src/internal/events.ts +100 -0
  174. package/src/internal/history.ts +366 -0
  175. package/src/internal/instructions.ts +9 -0
  176. package/src/internal/loop.ts +344 -0
  177. package/src/internal/schema.ts +25 -0
  178. package/src/internal/skills.ts +37 -0
  179. package/src/internal/specialists.ts +414 -0
  180. package/src/internal/telemetry.ts +125 -0
  181. package/src/internal/toolkit.ts +231 -0
  182. package/src/internal/tools.ts +210 -0
  183. package/src/internal/turns.ts +429 -0
  184. package/src/internal/uuid.ts +50 -0
  185. package/src/internal/writer.ts +12 -0
@@ -0,0 +1,635 @@
1
+ /**
2
+ * Defines the events that can happen in a chat.
3
+ *
4
+ * A chat is an append-only log of these events, and the log is the source of truth. A chat client
5
+ * receives the part of the log its user sees (`ChatView`), and the model's history is rebuilt
6
+ * from the log before every model call. Besides the library's events ({@link AssistantEvent}), a
7
+ * log holds the application's own events ({@link StoredAppEvent}).
8
+ *
9
+ * @since 0.1.0
10
+ */
11
+ import * as Response from "effect/ai/Response"
12
+ import * as Predicate from "effect/Predicate"
13
+ import * as Schema from "effect/Schema"
14
+ import { ChatId } from "./ChatId.ts"
15
+ import { DecisionId } from "./DecisionId.ts"
16
+ import { InputId, make as makeInputId } from "./InputId.ts"
17
+ import { RunId } from "./RunId.ts"
18
+
19
+ /**
20
+ * Schema for a file the user attached to a message, kept by reference.
21
+ *
22
+ * The file itself is stored elsewhere, so the log never holds its bytes.
23
+ *
24
+ * @category schemas
25
+ * @since 0.1.0
26
+ */
27
+ export const Attachment = Schema.Struct({
28
+ fileId: Schema.String.check(Schema.isMaxLength(64)),
29
+ name: Schema.String.check(Schema.isMaxLength(255)),
30
+ mediaType: Schema.String.check(Schema.isMaxLength(127)),
31
+ /** In bytes. */
32
+ size: Schema.Finite.check(Schema.isGreaterThanOrEqualTo(0))
33
+ })
34
+
35
+ /**
36
+ * The maximum number of files one message can carry.
37
+ *
38
+ * Each attached file is a line in every prompt after the message.
39
+ *
40
+ * @category utils
41
+ * @since 0.1.0
42
+ */
43
+ export const maxAttachments = 10
44
+
45
+ /**
46
+ * Represents a file the user attached to a message.
47
+ *
48
+ * @category models
49
+ * @since 0.1.0
50
+ */
51
+ export type Attachment = typeof Attachment.Type
52
+
53
+ /**
54
+ * Formats a byte count for people and the model, such as `2.1 MB`.
55
+ *
56
+ * @category utils
57
+ * @since 0.1.0
58
+ */
59
+ export const formatSize = (bytes: number): string =>
60
+ bytes < 1024
61
+ ? `${bytes} B`
62
+ : bytes < 1024 * 1024
63
+ ? `${(bytes / 1024).toFixed(1)} KB`
64
+ : bytes < 1024 * 1024 * 1024
65
+ ? `${(bytes / (1024 * 1024)).toFixed(1)} MB`
66
+ : `${(bytes / (1024 * 1024 * 1024)).toFixed(1)} GB`
67
+
68
+ /**
69
+ * Returns the line the model reads for an attachment.
70
+ *
71
+ * The line names the file and never holds its content.
72
+ *
73
+ * @category utils
74
+ * @since 0.1.0
75
+ */
76
+ export const attachmentLine = (attachment: Attachment): string =>
77
+ `[attached: ${attachment.name}, ${attachment.mediaType}, ${formatSize(attachment.size)}, id ${attachment.fileId}]`
78
+
79
+ /**
80
+ * Returns a message's text followed by a line for each attached file.
81
+ *
82
+ * @category utils
83
+ * @since 0.1.0
84
+ */
85
+ export const textWithAttachments = (text: string, attachments: ReadonlyArray<Attachment> | undefined): string =>
86
+ attachments === undefined || attachments.length === 0
87
+ ? text
88
+ : [text, ...attachments.map(attachmentLine)].filter((part) => part.length > 0).join("\n")
89
+
90
+ /**
91
+ * Represents text the user typed, and the files they attached.
92
+ *
93
+ * `id` defaults to a new one. Pass the client's own id so that a retried send stores the message
94
+ * once.
95
+ *
96
+ * @category schemas
97
+ * @since 0.1.0
98
+ */
99
+ export class UserMessage extends Schema.TaggedClass<UserMessage>()("UserMessage", {
100
+ id: InputId.pipe(Schema.withConstructorDefault(makeInputId())),
101
+ text: Schema.String,
102
+ attachments: Schema.optional(Schema.Array(Attachment).check(Schema.isMaxLength(maxAttachments)))
103
+ }) {}
104
+
105
+ /**
106
+ * Represents the user's answer to a block.
107
+ *
108
+ * It holds the id of the message the block was on, and the value in the block's answer schema
109
+ * (in its JSON form). A chat UI shows it inside the block rather than as a message.
110
+ *
111
+ * @category schemas
112
+ * @since 0.1.0
113
+ */
114
+ export class BlockAnswer extends Schema.TaggedClass<BlockAnswer>()("BlockAnswer", {
115
+ id: InputId,
116
+ messageId: Schema.String,
117
+ kind: Schema.String,
118
+ value: Schema.Unknown
119
+ }) {}
120
+
121
+ /**
122
+ * Represents a specialist run that finished working on a brief.
123
+ *
124
+ * Unless the assistant stopped the run, it is an input, and a turn reads its report and decides
125
+ * what to tell the user. Its id is fixed per run and brief, so it is stored once.
126
+ *
127
+ * @category schemas
128
+ * @since 0.1.0
129
+ */
130
+ export class SpecialistFinished extends Schema.TaggedClass<SpecialistFinished>()("SpecialistFinished", {
131
+ id: InputId,
132
+ runId: RunId,
133
+ specialist: Schema.String,
134
+ /** Which brief this answers: 1 for the first, then one more per follow-up. */
135
+ brief: Schema.Int,
136
+ goal: Schema.String,
137
+ status: Schema.Literals(["answered", "failed", "stopped", "cut_off"]),
138
+ report: Schema.optional(Schema.String),
139
+ error: Schema.optional(Schema.String)
140
+ }) {}
141
+
142
+ /**
143
+ * Schema for how the user decided, either `"approved"` or `"declined"`.
144
+ *
145
+ * @category schemas
146
+ * @since 0.1.0
147
+ */
148
+ export const DecisionOutcome = Schema.Literals(["approved", "declined"])
149
+
150
+ /**
151
+ * Represents how the user decided.
152
+ *
153
+ * @category models
154
+ * @since 0.1.0
155
+ */
156
+ export type DecisionOutcome = typeof DecisionOutcome.Type
157
+
158
+ /**
159
+ * Schema for the origin of a decision that a tool call waits for.
160
+ *
161
+ * It holds the call as the model wrote it. Approving the decision runs exactly this call.
162
+ *
163
+ * @category schemas
164
+ * @since 0.1.0
165
+ */
166
+ export const CallOrigin = Schema.TaggedStruct("Call", {
167
+ callId: Schema.String,
168
+ tool: Schema.String,
169
+ params: Schema.Json
170
+ })
171
+
172
+ /**
173
+ * Represents the origin of a decision that a tool call waits for.
174
+ *
175
+ * @category models
176
+ * @since 0.1.0
177
+ */
178
+ export type CallOrigin = typeof CallOrigin.Type
179
+
180
+ /**
181
+ * Represents a request for the user to decide something, by approving or declining it.
182
+ *
183
+ * @category schemas
184
+ * @since 0.1.0
185
+ */
186
+ export class DecisionRequested extends Schema.TaggedClass<DecisionRequested>()("DecisionRequested", {
187
+ id: DecisionId,
188
+ origin: CallOrigin
189
+ }) {}
190
+
191
+ /**
192
+ * Represents a decision the user made by answering a decision block for it.
193
+ *
194
+ * `answer` is the id of that `BlockAnswer`. An approved call runs at the turn's next step.
195
+ *
196
+ * @category schemas
197
+ * @since 0.1.0
198
+ */
199
+ export class DecisionResolved extends Schema.TaggedClass<DecisionResolved>()("DecisionResolved", {
200
+ id: DecisionId,
201
+ outcome: DecisionOutcome,
202
+ answer: InputId
203
+ }) {}
204
+
205
+ /**
206
+ * Schema for every input `Assistant.submit` accepts.
207
+ *
208
+ * @category schemas
209
+ * @since 0.1.0
210
+ */
211
+ export const Input = Schema.Union([UserMessage, BlockAnswer, SpecialistFinished])
212
+
213
+ /**
214
+ * Represents an input to an assistant.
215
+ *
216
+ * @category models
217
+ * @since 0.1.0
218
+ */
219
+ export type Input = typeof Input.Type
220
+
221
+ /**
222
+ * Schema for the inputs from the chat's user, which are messages and block answers.
223
+ *
224
+ * A turn owes each one a reply. It is also what a chat client usually may submit, since the app's
225
+ * events come from the server.
226
+ *
227
+ * @category schemas
228
+ * @since 0.1.0
229
+ */
230
+ export const UserInput = Schema.Union([UserMessage, BlockAnswer])
231
+
232
+ /**
233
+ * Represents an input from the chat's user.
234
+ *
235
+ * @category models
236
+ * @since 0.1.0
237
+ */
238
+ export type UserInput = typeof UserInput.Type
239
+
240
+ /**
241
+ * Represents the UI shown with a message, as a block kind from the assistant's `BlockKit` and its
242
+ * props.
243
+ *
244
+ * @category schemas
245
+ * @since 0.1.0
246
+ */
247
+ export class MessageBlock extends Schema.Class<MessageBlock>("effect-assistant/MessageBlock")({
248
+ kind: Schema.String,
249
+ props: Schema.Unknown
250
+ }) {}
251
+
252
+ /**
253
+ * Represents a message the assistant sent the user with `send_user_message`, optionally with a
254
+ * block.
255
+ *
256
+ * @category schemas
257
+ * @since 0.1.0
258
+ */
259
+ export class AssistantMessage extends Schema.TaggedClass<AssistantMessage>()("AssistantMessage", {
260
+ id: Schema.String,
261
+ text: Schema.String,
262
+ block: Schema.optional(MessageBlock),
263
+ /** Earlier messages whose block the user answered by typing instead. Their blocks close. */
264
+ closes: Schema.optional(Schema.Array(Schema.String))
265
+ }) {}
266
+
267
+ /**
268
+ * Schema for a tool call the model made.
269
+ *
270
+ * It is `effect/ai`'s tool call part (`Response.ToolCallPart`) for any tool, with its params in
271
+ * JSON, as the model wrote them.
272
+ *
273
+ * @category schemas
274
+ * @since 0.1.0
275
+ */
276
+ export const ToolCallPart = Schema.Struct({
277
+ ...Response.ToolCallPart("", Schema.Json).fields,
278
+ name: Schema.String
279
+ }).annotate({ identifier: "ToolCallPart" })
280
+
281
+ /**
282
+ * Represents a tool call the model made.
283
+ *
284
+ * @category models
285
+ * @since 0.1.0
286
+ */
287
+ export type ToolCallPart = typeof ToolCallPart.Type
288
+
289
+ /**
290
+ * Schema for what the log keeps of a model call.
291
+ *
292
+ * It uses `effect/ai`'s response parts for text, reasoning (with the provider's metadata, such as
293
+ * a thinking block's signature), and tool calls.
294
+ *
295
+ * @category schemas
296
+ * @since 0.1.0
297
+ */
298
+ export const ModelPart = Schema.Union([Response.TextPart, Response.ReasoningPart, ToolCallPart])
299
+
300
+ /**
301
+ * Represents a part of a model call's output.
302
+ *
303
+ * @category models
304
+ * @since 0.1.0
305
+ */
306
+ export type ModelPart = typeof ModelPart.Type
307
+
308
+ /**
309
+ * Represents what one model call wrote, as response parts for text, reasoning, and tool calls.
310
+ *
311
+ * `Prompt.fromResponseParts` turns the parts into history.
312
+ *
313
+ * @category schemas
314
+ * @since 0.1.0
315
+ */
316
+ export class ModelOutput extends Schema.TaggedClass<ModelOutput>()("ModelOutput", {
317
+ parts: Schema.Array(ModelPart),
318
+ /**
319
+ * The last entry the model had read when it wrote this. Inputs after it
320
+ * arrived while the model was working. A turn has handled what its
321
+ * outputs had read.
322
+ */
323
+ seenThroughSeq: Schema.optional(Schema.Int),
324
+ /** The model that wrote it, as the provider's id for it or the name the context gave it. */
325
+ model: Schema.optional(Schema.String),
326
+ /** The provider's id for the response, when it gives one. */
327
+ responseId: Schema.optional(Schema.String),
328
+ /** The call's token usage, with input (including the cache's reads and writes) and output. */
329
+ usage: Schema.optional(Response.Usage),
330
+ /** How long the call took, in milliseconds. */
331
+ durationMillis: Schema.optional(Schema.Finite)
332
+ }) {}
333
+
334
+ /**
335
+ * Represents a tool call that is about to run.
336
+ *
337
+ * It is stored before the tool runs, so after a crash the log says which calls may have done
338
+ * something. Those calls run again with the same call id (see `AssistantToolCall`).
339
+ *
340
+ * @category schemas
341
+ * @since 0.1.0
342
+ */
343
+ export class ToolStarted extends Schema.TaggedClass<ToolStarted>()("ToolStarted", {
344
+ callId: Schema.String,
345
+ tool: Schema.String
346
+ }) {}
347
+
348
+ /**
349
+ * Schema for how a tool call went, either `"success"` or `"failure"`, which the model reads as an
350
+ * error.
351
+ *
352
+ * @category schemas
353
+ * @since 0.1.0
354
+ */
355
+ export const ToolOutcome = Schema.Literals(["success", "failure"])
356
+
357
+ /**
358
+ * Represents how a tool call went.
359
+ *
360
+ * @category models
361
+ * @since 0.1.0
362
+ */
363
+ export type ToolOutcome = typeof ToolOutcome.Type
364
+
365
+ /**
366
+ * Represents the result of one tool call, as the model reads it.
367
+ *
368
+ * It holds the tool's success or failure in its encoded (JSON) form, and which of the two it is. A
369
+ * failure includes its reason when it has one, which is a `ToolFailure`'s `reason` or the
370
+ * failure's tag.
371
+ *
372
+ * @category schemas
373
+ * @since 0.1.0
374
+ */
375
+ export class ToolResult extends Schema.TaggedClass<ToolResult>()("ToolResult", {
376
+ callId: Schema.String,
377
+ tool: Schema.String,
378
+ outcome: ToolOutcome,
379
+ result: Schema.Json,
380
+ reason: Schema.optional(Schema.String)
381
+ }) {}
382
+
383
+ /**
384
+ * Represents the start of a turn.
385
+ *
386
+ * @category schemas
387
+ * @since 0.1.0
388
+ */
389
+ export class TurnStarted extends Schema.TaggedClass<TurnStarted>()("TurnStarted", {
390
+ /** The assistant that runs it. */
391
+ assistant: Schema.String,
392
+ /**
393
+ * The id of the turn's trace, when a tracer records it (not Effect's own, which exports nothing).
394
+ */
395
+ traceId: Schema.optional(Schema.String)
396
+ }) {}
397
+
398
+ /**
399
+ * Represents a turn that finished.
400
+ *
401
+ * `throughSeq` is the last entry the turn read. Inputs after it still need a turn.
402
+ *
403
+ * @category schemas
404
+ * @since 0.1.0
405
+ */
406
+ export class TurnCompleted extends Schema.TaggedClass<TurnCompleted>()("TurnCompleted", {
407
+ throughSeq: Schema.Int
408
+ }) {}
409
+
410
+ /**
411
+ * Represents a turn that failed.
412
+ *
413
+ * `throughSeq` covers everything stored when the turn failed, so a failing input isn't retried
414
+ * forever.
415
+ *
416
+ * @category schemas
417
+ * @since 0.1.0
418
+ */
419
+ export class TurnFailed extends Schema.TaggedClass<TurnFailed>()("TurnFailed", {
420
+ throughSeq: Schema.Int,
421
+ message: Schema.String
422
+ }) {}
423
+
424
+ /**
425
+ * Represents a specialist run that started working, either spawned or continued by a follow-up
426
+ * message.
427
+ *
428
+ * The model doesn't see it, since the call that started the run says so, and the run's report
429
+ * arrives as a {@link SpecialistFinished}.
430
+ *
431
+ * @category schemas
432
+ * @since 0.1.0
433
+ */
434
+ export class SpecialistStarted extends Schema.TaggedClass<SpecialistStarted>()("SpecialistStarted", {
435
+ runId: RunId,
436
+ specialist: Schema.String,
437
+ /** The goal it's working on. */
438
+ goal: Schema.String
439
+ }) {}
440
+
441
+ /**
442
+ * Represents the deletion of a chat.
443
+ *
444
+ * The chat stores nothing after it, the work running in the chat stopped, and its subscribers'
445
+ * streams end here. Its log stays.
446
+ *
447
+ * @category schemas
448
+ * @since 0.1.0
449
+ */
450
+ export class ChatDeleted extends Schema.TaggedClass<ChatDeleted>()("ChatDeleted", {}) {}
451
+
452
+ /**
453
+ * Represents blocks that closed without an answer, like the ones `BlockKit.closeStale` closes
454
+ * from a turn once the user moved on.
455
+ *
456
+ * The model doesn't see it.
457
+ *
458
+ * @category schemas
459
+ * @since 0.1.0
460
+ */
461
+ export class BlocksClosed extends Schema.TaggedClass<BlocksClosed>()("BlocksClosed", {
462
+ messageIds: Schema.Array(Schema.String)
463
+ }) {}
464
+
465
+ /**
466
+ * Schema for every event the library defines.
467
+ *
468
+ * @category schemas
469
+ * @since 0.1.0
470
+ */
471
+ export const AssistantEvent = Schema.Union([
472
+ UserMessage,
473
+ BlockAnswer,
474
+ AssistantMessage,
475
+ ModelOutput,
476
+ ToolStarted,
477
+ ToolResult,
478
+ TurnStarted,
479
+ TurnCompleted,
480
+ TurnFailed,
481
+ SpecialistStarted,
482
+ SpecialistFinished,
483
+ BlocksClosed,
484
+ DecisionRequested,
485
+ DecisionResolved,
486
+ ChatDeleted
487
+ ])
488
+
489
+ /**
490
+ * Represents an event the library defines.
491
+ *
492
+ * @category models
493
+ * @since 0.1.0
494
+ */
495
+ export type AssistantEvent = typeof AssistantEvent.Type
496
+
497
+ const libraryTags: ReadonlySet<string> = new Set(
498
+ AssistantEvent.members.map((member) => member.fields._tag.schema.literal)
499
+ )
500
+
501
+ /**
502
+ * Schema for the tag of one of the application's own events.
503
+ *
504
+ * It accepts any tag but the library's, and it is branded, so narrowing an entry's event to one
505
+ * of the library's events (`Predicate.isTagged(event, "UserMessage")`) never leaves an app event
506
+ * in.
507
+ *
508
+ * @category schemas
509
+ * @since 0.1.0
510
+ */
511
+ export const AppTag = Schema.String.pipe(Schema.brand("~effect-assistant/AppTag"))
512
+
513
+ /**
514
+ * Schema for one of the application's own events as it is stored, with its `_tag` and its fields
515
+ * in JSON.
516
+ *
517
+ * An assistant made with `events: [AppEvent.make(Summary)]` decodes it to a `Summary` in
518
+ * `CurrentChat`'s `entries` and in its `subscribe`.
519
+ *
520
+ * @category schemas
521
+ * @since 0.1.0
522
+ */
523
+ export const StoredAppEvent = Schema.StructWithRest(Schema.Struct({ _tag: AppTag }), [
524
+ Schema.Record(Schema.String, Schema.Json)
525
+ ]).check(Schema.makeFilter((event) => !libraryTags.has(event._tag) || `${event._tag} is one of the library's events`))
526
+
527
+ /**
528
+ * Represents one of the application's own events, as it is stored.
529
+ *
530
+ * @category models
531
+ * @since 0.1.0
532
+ */
533
+ export type StoredAppEvent = typeof StoredAppEvent.Type
534
+
535
+ /**
536
+ * Returns whether an event is one of the library's, not one of the app's own.
537
+ *
538
+ * @category guards
539
+ * @since 0.1.0
540
+ */
541
+ export const isLibraryEvent = (event: { readonly _tag: string }): event is AssistantEvent => libraryTags.has(event._tag)
542
+
543
+ /**
544
+ * Represents a chat's entry with any event in it, either the library's or the application's own
545
+ * (decoded or not).
546
+ *
547
+ * The folds over a chat take this type, so a client's typed entries fit too.
548
+ *
549
+ * @category models
550
+ * @since 0.1.0
551
+ */
552
+ export interface AnyEntry {
553
+ readonly chatId: ChatId
554
+ readonly seq: number
555
+ readonly at: number
556
+ readonly event: { readonly _tag: string }
557
+ }
558
+
559
+ /**
560
+ * Schema for a stored event with its position in the chat.
561
+ *
562
+ * `seq` starts at 1 and has no gaps. `at` is the time the event was stored, in epoch
563
+ * milliseconds.
564
+ *
565
+ * @category schemas
566
+ * @since 0.1.0
567
+ */
568
+ export const Entry = Schema.Struct({
569
+ chatId: ChatId,
570
+ seq: Schema.Int,
571
+ at: Schema.Finite,
572
+ event: Schema.Union([AssistantEvent, StoredAppEvent])
573
+ })
574
+
575
+ /**
576
+ * Represents a stored event with its position in the chat.
577
+ *
578
+ * @category models
579
+ * @since 0.1.0
580
+ */
581
+ export type Entry = typeof Entry.Type
582
+
583
+ /**
584
+ * Returns whether an event is one of the library's inputs, which are a user message, a block
585
+ * answer, or a finished specialist run.
586
+ *
587
+ * The application's events that wake a turn are inputs too, but only their assistant knows which
588
+ * (see `AppEvent`).
589
+ *
590
+ * @category guards
591
+ * @since 0.1.0
592
+ */
593
+ export const isInput = (event: { readonly _tag: string }): event is Input =>
594
+ Predicate.isTagged(event, "UserMessage") ||
595
+ Predicate.isTagged(event, "BlockAnswer") ||
596
+ Predicate.isTagged(event, "SpecialistFinished")
597
+
598
+ /**
599
+ * Returns whether an input starts a turn, which every input does except a run the assistant
600
+ * stopped itself.
601
+ *
602
+ * @category guards
603
+ * @since 0.1.0
604
+ */
605
+ export const isWaking = (input: Input): boolean =>
606
+ !(Predicate.isTagged(input, "SpecialistFinished") && input.status === "stopped")
607
+
608
+ /**
609
+ * Returns whether an input came from the chat's user, and so is owed a reply.
610
+ *
611
+ * @category guards
612
+ * @since 0.1.0
613
+ */
614
+ export const isUserInput = (input: Input): input is UserInput =>
615
+ Predicate.isTagged(input, "UserMessage") || Predicate.isTagged(input, "BlockAnswer")
616
+
617
+ /**
618
+ * Returns whether an event ends a turn, and so says how far the assistant has read.
619
+ *
620
+ * @category guards
621
+ * @since 0.1.0
622
+ */
623
+ export const isTurnEnd = (event: { readonly _tag: string }): event is TurnCompleted | TurnFailed =>
624
+ Predicate.isTagged(event, "TurnCompleted") || Predicate.isTagged(event, "TurnFailed")
625
+
626
+ /**
627
+ * Returns the text of every message the user was shown, in order.
628
+ *
629
+ * @category utils
630
+ * @since 0.1.0
631
+ */
632
+ export const assistantTexts = (entries: ReadonlyArray<AnyEntry>): ReadonlyArray<string> =>
633
+ entries.flatMap(({ event }) =>
634
+ isLibraryEvent(event) && Predicate.isTagged(event, "AssistantMessage") ? [event.text] : []
635
+ )