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