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.
- package/README.md +1 -1
- package/dist/Assistant.d.ts +74 -28
- package/dist/Assistant.d.ts.map +1 -1
- package/dist/Assistant.js +11 -4
- package/dist/Assistant.js.map +1 -1
- package/dist/AssistantActivity.d.ts +10 -16
- package/dist/AssistantActivity.d.ts.map +1 -1
- package/dist/AssistantActivity.js +11 -4
- package/dist/AssistantActivity.js.map +1 -1
- package/dist/AssistantEngine.d.ts +39 -1
- package/dist/AssistantEngine.d.ts.map +1 -1
- package/dist/AssistantEngine.js +81 -28
- package/dist/AssistantEngine.js.map +1 -1
- package/dist/AssistantEvent.d.ts +52 -70
- package/dist/AssistantEvent.d.ts.map +1 -1
- package/dist/AssistantEvent.js +28 -57
- package/dist/AssistantEvent.js.map +1 -1
- package/dist/AssistantLoop.d.ts +7 -5
- package/dist/AssistantLoop.d.ts.map +1 -1
- package/dist/AssistantLoop.js +6 -3
- package/dist/AssistantLoop.js.map +1 -1
- package/dist/Block.js.map +1 -1
- package/dist/BlockKit.d.ts +14 -18
- package/dist/BlockKit.d.ts.map +1 -1
- package/dist/BlockKit.js +4 -7
- package/dist/BlockKit.js.map +1 -1
- package/dist/ChatLog.js +1 -1
- package/dist/Compaction.d.ts +8 -4
- package/dist/Compaction.d.ts.map +1 -1
- package/dist/Compaction.js +12 -15
- package/dist/Compaction.js.map +1 -1
- package/dist/CurrentChat.d.ts +2 -1
- package/dist/CurrentChat.d.ts.map +1 -1
- package/dist/CurrentChat.js.map +1 -1
- package/dist/Instructions.js +1 -1
- package/dist/Skill.d.ts +33 -0
- package/dist/Skill.d.ts.map +1 -1
- package/dist/Skill.js +27 -1
- package/dist/Skill.js.map +1 -1
- package/dist/Specialist.d.ts +4 -1
- package/dist/Specialist.d.ts.map +1 -1
- package/dist/Specialist.js.map +1 -1
- package/dist/Timeline.d.ts +482 -0
- package/dist/Timeline.d.ts.map +1 -0
- package/dist/Timeline.js +572 -0
- package/dist/Timeline.js.map +1 -0
- package/dist/ToolDiscovery.d.ts +4 -5
- package/dist/ToolDiscovery.d.ts.map +1 -1
- package/dist/ToolDiscovery.js +5 -4
- package/dist/ToolDiscovery.js.map +1 -1
- package/dist/ToolFailure.d.ts +1 -1
- package/dist/ToolFailure.js +1 -1
- package/dist/ToolFailure.js.map +1 -1
- package/dist/index.d.ts +2 -5
- package/dist/index.js +4 -7
- package/dist/internal/chat.js +11 -37
- package/dist/internal/chat.js.map +1 -1
- package/dist/internal/events.d.ts +15 -16
- package/dist/internal/events.d.ts.map +1 -1
- package/dist/internal/events.js +29 -28
- package/dist/internal/events.js.map +1 -1
- package/dist/internal/history.js +26 -58
- package/dist/internal/history.js.map +1 -1
- package/dist/internal/input.js +7 -0
- package/dist/internal/input.js.map +1 -0
- package/dist/internal/loop.d.ts +2 -2
- package/dist/internal/loop.d.ts.map +1 -1
- package/dist/internal/loop.js +21 -56
- package/dist/internal/loop.js.map +1 -1
- package/dist/internal/skills.js +6 -1
- package/dist/internal/skills.js.map +1 -1
- package/dist/internal/specialists.js +7 -7
- package/dist/internal/specialists.js.map +1 -1
- package/dist/internal/toolkit.d.ts +6 -20
- package/dist/internal/toolkit.d.ts.map +1 -1
- package/dist/internal/toolkit.js +10 -31
- package/dist/internal/toolkit.js.map +1 -1
- package/dist/internal/tools.js +2 -21
- package/dist/internal/tools.js.map +1 -1
- package/dist/internal/turns.d.ts.map +1 -1
- package/dist/internal/turns.js +24 -43
- package/dist/internal/turns.js.map +1 -1
- package/package.json +3 -2
- package/src/Assistant.ts +75 -29
- package/src/AssistantActivity.ts +11 -18
- package/src/AssistantEngine.ts +110 -43
- package/src/AssistantEvent.ts +46 -74
- package/src/AssistantLoop.ts +7 -5
- package/src/Block.ts +1 -1
- package/src/BlockKit.ts +18 -31
- package/src/Compaction.ts +8 -21
- package/src/CurrentChat.ts +2 -1
- package/src/Skill.ts +34 -1
- package/src/Specialist.ts +4 -1
- package/src/Timeline.ts +861 -0
- package/src/ToolDiscovery.ts +5 -13
- package/src/ToolFailure.ts +1 -1
- package/src/index.ts +5 -20
- package/src/internal/chat.ts +8 -57
- package/src/internal/events.ts +43 -43
- package/src/internal/history.ts +36 -101
- package/src/internal/input.ts +3 -0
- package/src/internal/loop.ts +31 -101
- package/src/internal/skills.ts +11 -0
- package/src/internal/specialists.ts +6 -9
- package/src/internal/toolkit.ts +17 -60
- package/src/internal/tools.ts +2 -53
- package/src/internal/turns.ts +37 -79
- package/dist/AppEvent.d.ts +0 -111
- package/dist/AppEvent.d.ts.map +0 -1
- package/dist/AppEvent.js +0 -71
- package/dist/AppEvent.js.map +0 -1
- package/dist/ChatView.d.ts +0 -459
- package/dist/ChatView.d.ts.map +0 -1
- package/dist/ChatView.js +0 -496
- package/dist/ChatView.js.map +0 -1
- package/dist/Decision.d.ts +0 -145
- package/dist/Decision.d.ts.map +0 -1
- package/dist/Decision.js +0 -178
- package/dist/Decision.js.map +0 -1
- package/dist/DecisionId.d.ts +0 -37
- package/dist/DecisionId.d.ts.map +0 -1
- package/dist/DecisionId.js +0 -44
- package/dist/DecisionId.js.map +0 -1
- package/dist/internal/decisions.js +0 -66
- package/dist/internal/decisions.js.map +0 -1
- package/src/AppEvent.ts +0 -134
- package/src/ChatView.ts +0 -658
- package/src/Decision.ts +0 -241
- package/src/DecisionId.ts +0 -46
- package/src/internal/decisions.ts +0 -131
package/dist/ChatView.js
DELETED
|
@@ -1,496 +0,0 @@
|
|
|
1
|
-
import { __exportAll } from "./_virtual/_rolldown/runtime.js";
|
|
2
|
-
import { DecisionId } from "./DecisionId.js";
|
|
3
|
-
import { InputId } from "./InputId.js";
|
|
4
|
-
import { Attachment, DecisionOutcome, isLibraryEvent } from "./AssistantEvent.js";
|
|
5
|
-
import { Answer, Props, apply as apply$1 } from "./Decision.js";
|
|
6
|
-
import { apply as apply$2 } from "./BlockKit.js";
|
|
7
|
-
import { toJson } from "./internal/schema.js";
|
|
8
|
-
import { apply as apply$3, empty as empty$1 } from "./AssistantActivity.js";
|
|
9
|
-
import * as Schema from "effect/Schema";
|
|
10
|
-
import * as Stream from "effect/Stream";
|
|
11
|
-
import * as Option from "effect/Option";
|
|
12
|
-
import { dual } from "effect/Function";
|
|
13
|
-
import * as Match from "effect/Match";
|
|
14
|
-
//#region src/ChatView.ts
|
|
15
|
-
/**
|
|
16
|
-
* This module provides the view of a chat as its user sees it. A chat client receives this view
|
|
17
|
-
* and nothing else.
|
|
18
|
-
*
|
|
19
|
-
* A {@link View} holds the chat's messages with their blocks and decisions, the chat's
|
|
20
|
-
* {@link Activity}, and whether the last turn failed or the chat was deleted. It never contains
|
|
21
|
-
* the model's output, tool calls or results, what the model read, the app's own events, or why a
|
|
22
|
-
* turn failed. The server streams {@link Update}s by piping the assistant's `subscribe` through
|
|
23
|
-
* {@link updates}, and the client folds them with {@link apply}, starting from {@link empty}.
|
|
24
|
-
* On the server, {@link fromEntries} builds a whole view from a chat's log.
|
|
25
|
-
*
|
|
26
|
-
* @since 0.1.0
|
|
27
|
-
*/
|
|
28
|
-
var ChatView_exports = /* @__PURE__ */ __exportAll({
|
|
29
|
-
Activity: () => Activity,
|
|
30
|
-
AssistantMessage: () => AssistantMessage,
|
|
31
|
-
Block: () => Block,
|
|
32
|
-
BlockStatus: () => BlockStatus,
|
|
33
|
-
Message: () => Message,
|
|
34
|
-
PendingDecision: () => PendingDecision,
|
|
35
|
-
SpecialistWorking: () => SpecialistWorking,
|
|
36
|
-
Update: () => Update,
|
|
37
|
-
UserMessage: () => UserMessage,
|
|
38
|
-
View: () => View,
|
|
39
|
-
apply: () => apply,
|
|
40
|
-
empty: () => empty,
|
|
41
|
-
fromEntries: () => fromEntries,
|
|
42
|
-
isIdle: () => isIdle,
|
|
43
|
-
updates: () => updates
|
|
44
|
-
});
|
|
45
|
-
/**
|
|
46
|
-
* Schema for a decision as the user sees it on a decision block. It holds the call the decision is
|
|
47
|
-
* for (its tool and params, which the app can display as it likes) and, once the user has
|
|
48
|
-
* decided, the outcome.
|
|
49
|
-
*
|
|
50
|
-
* @category schemas
|
|
51
|
-
* @since 0.1.0
|
|
52
|
-
*/
|
|
53
|
-
const PendingDecision = Schema.Struct({
|
|
54
|
-
id: DecisionId,
|
|
55
|
-
tool: Schema.String,
|
|
56
|
-
params: Schema.Json,
|
|
57
|
-
outcome: Schema.optional(DecisionOutcome)
|
|
58
|
-
});
|
|
59
|
-
/**
|
|
60
|
-
* Schema for the status of a block. A block is `"open"` while it accepts an answer, `"answered"`
|
|
61
|
-
* once the user has used it, and `"closed"` when it closed without an answer because the user
|
|
62
|
-
* answered in words or moved on.
|
|
63
|
-
*
|
|
64
|
-
* @category schemas
|
|
65
|
-
* @since 0.1.0
|
|
66
|
-
*/
|
|
67
|
-
const BlockStatus = Schema.Literals([
|
|
68
|
-
"open",
|
|
69
|
-
"answered",
|
|
70
|
-
"closed"
|
|
71
|
-
]);
|
|
72
|
-
/**
|
|
73
|
-
* Schema for a block on an assistant's message. It holds the block's kind, its props (JSON, as
|
|
74
|
-
* the block's props schema encodes them), its status, the user's answer (JSON, as the block's
|
|
75
|
-
* answer schema encodes it), and, for a decision block, its decisions.
|
|
76
|
-
*
|
|
77
|
-
* @category schemas
|
|
78
|
-
* @since 0.1.0
|
|
79
|
-
*/
|
|
80
|
-
const Block = Schema.Struct({
|
|
81
|
-
kind: Schema.String,
|
|
82
|
-
props: Schema.Json,
|
|
83
|
-
status: BlockStatus,
|
|
84
|
-
answer: Schema.optional(Schema.Json),
|
|
85
|
-
decisions: Schema.optional(Schema.Array(PendingDecision))
|
|
86
|
-
});
|
|
87
|
-
/**
|
|
88
|
-
* Schema for a message the user sent. `at` is when it was stored, in epoch
|
|
89
|
-
* milliseconds.
|
|
90
|
-
*
|
|
91
|
-
* @category schemas
|
|
92
|
-
* @since 0.1.0
|
|
93
|
-
*/
|
|
94
|
-
const UserMessage = Schema.TaggedStruct("UserMessage", {
|
|
95
|
-
id: InputId,
|
|
96
|
-
text: Schema.String,
|
|
97
|
-
attachments: Schema.optional(Schema.Array(Attachment)),
|
|
98
|
-
at: Schema.Finite
|
|
99
|
-
});
|
|
100
|
-
/**
|
|
101
|
-
* Schema for a message the assistant sent the user, including its block if it has one.
|
|
102
|
-
*
|
|
103
|
-
* @category schemas
|
|
104
|
-
* @since 0.1.0
|
|
105
|
-
*/
|
|
106
|
-
const AssistantMessage = Schema.TaggedStruct("AssistantMessage", {
|
|
107
|
-
id: Schema.String,
|
|
108
|
-
text: Schema.String,
|
|
109
|
-
at: Schema.Finite,
|
|
110
|
-
block: Schema.optional(Block)
|
|
111
|
-
});
|
|
112
|
-
/**
|
|
113
|
-
* Schema for a message in the chat, sent by either the user or the assistant.
|
|
114
|
-
*
|
|
115
|
-
* @category schemas
|
|
116
|
-
* @since 0.1.0
|
|
117
|
-
*/
|
|
118
|
-
const Message = Schema.Union([UserMessage, AssistantMessage]);
|
|
119
|
-
/**
|
|
120
|
-
* Schema for a specialist that is working for the assistant. It holds the specialist's name and
|
|
121
|
-
* when it started, in epoch milliseconds.
|
|
122
|
-
*
|
|
123
|
-
* @category schemas
|
|
124
|
-
* @since 0.1.0
|
|
125
|
-
*/
|
|
126
|
-
const SpecialistWorking = Schema.Struct({
|
|
127
|
-
specialist: Schema.String,
|
|
128
|
-
since: Schema.Finite
|
|
129
|
-
});
|
|
130
|
-
/**
|
|
131
|
-
* Schema for what is working in the chat.
|
|
132
|
-
*
|
|
133
|
-
* `working` is true while the assistant runs a turn, or when a turn is about to start for
|
|
134
|
-
* something that arrived. `specialists` lists the specialists that are working. When the chat is
|
|
135
|
-
* idle, nothing is working, even if a workflow is waiting on a timer or a decision.
|
|
136
|
-
*
|
|
137
|
-
* @category schemas
|
|
138
|
-
* @since 0.1.0
|
|
139
|
-
*/
|
|
140
|
-
const Activity = Schema.Struct({
|
|
141
|
-
working: Schema.Boolean,
|
|
142
|
-
specialists: Schema.Array(SpecialistWorking)
|
|
143
|
-
});
|
|
144
|
-
/**
|
|
145
|
-
* Schema for a chat as its user sees it. It holds the chat's messages, what is working in it,
|
|
146
|
-
* whether the last turn failed, and whether the chat was deleted. `seq` is the position in the
|
|
147
|
-
* chat's log that the view is up to.
|
|
148
|
-
*
|
|
149
|
-
* @category schemas
|
|
150
|
-
* @since 0.1.0
|
|
151
|
-
*/
|
|
152
|
-
const View = Schema.Struct({
|
|
153
|
-
seq: Schema.Int,
|
|
154
|
-
messages: Schema.Array(Message),
|
|
155
|
-
activity: Activity,
|
|
156
|
-
/** Whether the last turn failed with no message after it. */
|
|
157
|
-
failed: Schema.Boolean,
|
|
158
|
-
/** Whether the chat was deleted. A deleted chat accepts nothing more, and its stream ends. */
|
|
159
|
-
deleted: Schema.Boolean
|
|
160
|
-
});
|
|
161
|
-
/**
|
|
162
|
-
* Schema for a change to a chat's view.
|
|
163
|
-
*
|
|
164
|
-
* An update holds each message that is new or changed (for example, a block answered or closed,
|
|
165
|
-
* or a decision made), in full. It also holds the current activity, whether the last turn failed,
|
|
166
|
-
* and whether the chat was deleted. `seq` is the position in the chat's log that the update is up
|
|
167
|
-
* to.
|
|
168
|
-
*
|
|
169
|
-
* @category schemas
|
|
170
|
-
* @since 0.1.0
|
|
171
|
-
*/
|
|
172
|
-
const Update = Schema.Struct({
|
|
173
|
-
seq: Schema.Int,
|
|
174
|
-
messages: Schema.Array(Message),
|
|
175
|
-
activity: Activity,
|
|
176
|
-
failed: Schema.Boolean,
|
|
177
|
-
deleted: Schema.Boolean
|
|
178
|
-
});
|
|
179
|
-
/**
|
|
180
|
-
* The view of a chat before anything has happened in it, with no messages and nothing working.
|
|
181
|
-
* Use it as the starting point when folding updates with {@link apply}.
|
|
182
|
-
*
|
|
183
|
-
* @category constructors
|
|
184
|
-
* @since 0.1.0
|
|
185
|
-
*/
|
|
186
|
-
const empty = {
|
|
187
|
-
seq: 0,
|
|
188
|
-
messages: [],
|
|
189
|
-
activity: {
|
|
190
|
-
working: false,
|
|
191
|
-
specialists: []
|
|
192
|
-
},
|
|
193
|
-
failed: false,
|
|
194
|
-
deleted: false
|
|
195
|
-
};
|
|
196
|
-
/**
|
|
197
|
-
* Returns the view after applying an update.
|
|
198
|
-
*
|
|
199
|
-
* **Details**
|
|
200
|
-
*
|
|
201
|
-
* Each message in the update replaces the message with the same id, in place, and new messages
|
|
202
|
-
* are added after the rest. The update's activity, `failed`, and `deleted` replace the view's.
|
|
203
|
-
* Applying an update twice changes nothing, so a client that resubscribes from the start can fold
|
|
204
|
-
* again and get the same view.
|
|
205
|
-
*
|
|
206
|
-
* @category combinators
|
|
207
|
-
* @since 0.1.0
|
|
208
|
-
*/
|
|
209
|
-
const apply = (view, update) => {
|
|
210
|
-
const changed = new Map(update.messages.map((message) => [message.id, message]));
|
|
211
|
-
const known = new Set(view.messages.map((message) => message.id));
|
|
212
|
-
return {
|
|
213
|
-
seq: Math.max(view.seq, update.seq),
|
|
214
|
-
messages: [...view.messages.map((message) => changed.get(message.id) ?? message), ...update.messages.filter((message) => !known.has(message.id))],
|
|
215
|
-
activity: update.activity,
|
|
216
|
-
failed: update.failed,
|
|
217
|
-
deleted: update.deleted
|
|
218
|
-
};
|
|
219
|
-
};
|
|
220
|
-
/**
|
|
221
|
-
* Returns whether nothing is working in the chat: no turn is running or about to start, and no
|
|
222
|
-
* specialist is working.
|
|
223
|
-
*
|
|
224
|
-
* @category guards
|
|
225
|
-
* @since 0.1.0
|
|
226
|
-
*/
|
|
227
|
-
const isIdle = (view) => !view.activity.working && view.activity.specialists.length === 0;
|
|
228
|
-
/**
|
|
229
|
-
* Returns a chat's view, built from the entries of its log.
|
|
230
|
-
*
|
|
231
|
-
* **When to use**
|
|
232
|
-
*
|
|
233
|
-
* Use it in server-side code, for example to return a whole chat at once. A client gets the view
|
|
234
|
-
* as a stream from {@link updates} instead.
|
|
235
|
-
*
|
|
236
|
-
* **Example** (Building a chat's view from its log)
|
|
237
|
-
*
|
|
238
|
-
* ```ts
|
|
239
|
-
* import { type ChatId, ChatLog, ChatView } from "effect-assistant"
|
|
240
|
-
* import * as Effect from "effect/Effect"
|
|
241
|
-
*
|
|
242
|
-
* const viewOf = (chatId: ChatId.ChatId) =>
|
|
243
|
-
* ChatLog.ChatLog.use((log) => log.read(chatId)).pipe(
|
|
244
|
-
* Effect.map((entries) => ChatView.fromEntries(entries))
|
|
245
|
-
* )
|
|
246
|
-
* ```
|
|
247
|
-
*
|
|
248
|
-
* @see {@link updates} for a stream of changes instead of a whole view
|
|
249
|
-
*
|
|
250
|
-
* @category constructors
|
|
251
|
-
* @since 0.1.0
|
|
252
|
-
*/
|
|
253
|
-
const fromEntries = (entries, options) => {
|
|
254
|
-
const fold = folding(options?.wakes ?? /* @__PURE__ */ new Set());
|
|
255
|
-
for (const entry of entries) fold.step(entry);
|
|
256
|
-
return fold.view;
|
|
257
|
-
};
|
|
258
|
-
const decodeProps = Schema.decodeUnknownOption(Props);
|
|
259
|
-
const decodeAnswer = Schema.decodeUnknownOption(Answer);
|
|
260
|
-
const activityOf = (activity) => ({
|
|
261
|
-
working: activity.turnSince !== void 0 || activity.queued,
|
|
262
|
-
specialists: [...activity.specialists.values()].map((run) => ({
|
|
263
|
-
specialist: run.specialist,
|
|
264
|
-
since: run.since
|
|
265
|
-
}))
|
|
266
|
-
});
|
|
267
|
-
const sameActivity = (a, b) => a.working === b.working && a.specialists.length === b.specialists.length && a.specialists.every((run, i) => {
|
|
268
|
-
const other = b.specialists[i];
|
|
269
|
-
return other !== void 0 && run.specialist === other.specialist && run.since === other.since;
|
|
270
|
-
});
|
|
271
|
-
const decisionIds = (block) => block.kind === "decision" ? Option.match(decodeProps(block.props), {
|
|
272
|
-
onNone: () => [],
|
|
273
|
-
onSome: (props) => props.decisionIds
|
|
274
|
-
}) : [];
|
|
275
|
-
const folding = (wakes) => {
|
|
276
|
-
let seq = 0;
|
|
277
|
-
let failed = false;
|
|
278
|
-
let deleted = false;
|
|
279
|
-
let activity = empty$1;
|
|
280
|
-
let blocks = /* @__PURE__ */ new Map();
|
|
281
|
-
let decisions = /* @__PURE__ */ new Map();
|
|
282
|
-
const messages = /* @__PURE__ */ new Map();
|
|
283
|
-
const sent = /* @__PURE__ */ new Map();
|
|
284
|
-
const asking = /* @__PURE__ */ new Map();
|
|
285
|
-
const blockOf = (message) => {
|
|
286
|
-
if (message.block === void 0) return void 0;
|
|
287
|
-
const state = blocks.get(message.id);
|
|
288
|
-
const status = state === void 0 || state.open ? "open" : state.answer === void 0 ? "closed" : "answered";
|
|
289
|
-
const shown = decisionIds(message.block).flatMap((id) => {
|
|
290
|
-
const decision = decisions.get(id);
|
|
291
|
-
if (decision === void 0) return [];
|
|
292
|
-
const { tool, params } = decision.request.origin;
|
|
293
|
-
const shown = {
|
|
294
|
-
id: decision.request.id,
|
|
295
|
-
tool,
|
|
296
|
-
params
|
|
297
|
-
};
|
|
298
|
-
return [decision.outcome === void 0 ? shown : {
|
|
299
|
-
...shown,
|
|
300
|
-
outcome: decision.outcome
|
|
301
|
-
}];
|
|
302
|
-
});
|
|
303
|
-
const block = {
|
|
304
|
-
kind: message.block.kind,
|
|
305
|
-
props: toJson(message.block.props),
|
|
306
|
-
status
|
|
307
|
-
};
|
|
308
|
-
const answered = state?.answer === void 0 ? block : {
|
|
309
|
-
...block,
|
|
310
|
-
answer: toJson(state.answer.value)
|
|
311
|
-
};
|
|
312
|
-
return message.block.kind === "decision" ? {
|
|
313
|
-
...answered,
|
|
314
|
-
decisions: shown
|
|
315
|
-
} : answered;
|
|
316
|
-
};
|
|
317
|
-
const viewOf = (message, at) => {
|
|
318
|
-
const block = blockOf(message);
|
|
319
|
-
return block === void 0 ? AssistantMessage.make({
|
|
320
|
-
id: message.id,
|
|
321
|
-
text: message.text,
|
|
322
|
-
at
|
|
323
|
-
}) : AssistantMessage.make({
|
|
324
|
-
id: message.id,
|
|
325
|
-
text: message.text,
|
|
326
|
-
at,
|
|
327
|
-
block
|
|
328
|
-
});
|
|
329
|
-
};
|
|
330
|
-
const touched = (event, at) => Match.value(event).pipe(Match.tagsExhaustive({
|
|
331
|
-
UserMessage: (message) => {
|
|
332
|
-
messages.set(message.id, message.attachments === void 0 ? UserMessage.make({
|
|
333
|
-
id: message.id,
|
|
334
|
-
text: message.text,
|
|
335
|
-
at
|
|
336
|
-
}) : UserMessage.make({
|
|
337
|
-
id: message.id,
|
|
338
|
-
text: message.text,
|
|
339
|
-
attachments: message.attachments,
|
|
340
|
-
at
|
|
341
|
-
}));
|
|
342
|
-
failed = false;
|
|
343
|
-
return [message.id];
|
|
344
|
-
},
|
|
345
|
-
AssistantMessage: (message) => {
|
|
346
|
-
sent.set(message.id, message);
|
|
347
|
-
messages.set(message.id, viewOf(message, at));
|
|
348
|
-
failed = false;
|
|
349
|
-
if (message.block !== void 0) for (const id of decisionIds(message.block)) asking.set(id, [...asking.get(id) ?? [], message.id]);
|
|
350
|
-
return [message.id, ...message.closes ?? []];
|
|
351
|
-
},
|
|
352
|
-
BlockAnswer: (answer) => {
|
|
353
|
-
failed = false;
|
|
354
|
-
const decided = answer.kind === "decision" ? Option.match(decodeAnswer(answer.value), {
|
|
355
|
-
onNone: () => [],
|
|
356
|
-
onSome: ({ decisions }) => Object.keys(decisions).flatMap((id) => asking.get(id) ?? [])
|
|
357
|
-
}) : [];
|
|
358
|
-
return [answer.messageId, ...decided];
|
|
359
|
-
},
|
|
360
|
-
BlocksClosed: (closed) => closed.messageIds,
|
|
361
|
-
DecisionResolved: (resolved) => asking.get(resolved.id) ?? [],
|
|
362
|
-
TurnFailed: () => {
|
|
363
|
-
failed = true;
|
|
364
|
-
return [];
|
|
365
|
-
},
|
|
366
|
-
ModelOutput: () => [],
|
|
367
|
-
ToolStarted: () => [],
|
|
368
|
-
ToolResult: () => [],
|
|
369
|
-
TurnStarted: () => [],
|
|
370
|
-
TurnCompleted: () => [],
|
|
371
|
-
SpecialistStarted: () => [],
|
|
372
|
-
SpecialistFinished: () => [],
|
|
373
|
-
DecisionRequested: () => [],
|
|
374
|
-
ChatDeleted: () => {
|
|
375
|
-
deleted = true;
|
|
376
|
-
return [];
|
|
377
|
-
}
|
|
378
|
-
}));
|
|
379
|
-
const step = (entry) => {
|
|
380
|
-
const before = {
|
|
381
|
-
activity: activityOf(activity),
|
|
382
|
-
failed,
|
|
383
|
-
deleted
|
|
384
|
-
};
|
|
385
|
-
const { event } = entry;
|
|
386
|
-
seq = entry.seq;
|
|
387
|
-
activity = apply$3(activity, entry, { wakes });
|
|
388
|
-
if (!isLibraryEvent(event)) {
|
|
389
|
-
const now = activityOf(activity);
|
|
390
|
-
return sameActivity(before.activity, now) ? void 0 : {
|
|
391
|
-
seq,
|
|
392
|
-
messages: [],
|
|
393
|
-
activity: now,
|
|
394
|
-
failed,
|
|
395
|
-
deleted
|
|
396
|
-
};
|
|
397
|
-
}
|
|
398
|
-
blocks = apply$2(blocks, event);
|
|
399
|
-
decisions = apply$1(decisions, event);
|
|
400
|
-
const changed = [...new Set(touched(event, entry.at))].flatMap((id) => {
|
|
401
|
-
const message = sent.get(id);
|
|
402
|
-
const current = messages.get(id);
|
|
403
|
-
if (message === void 0 || current === void 0) return current === void 0 ? [] : [current];
|
|
404
|
-
const next = viewOf(message, current.at);
|
|
405
|
-
messages.set(id, next);
|
|
406
|
-
return [next];
|
|
407
|
-
});
|
|
408
|
-
const now = activityOf(activity);
|
|
409
|
-
return changed.length === 0 && failed === before.failed && deleted === before.deleted && sameActivity(before.activity, now) ? void 0 : {
|
|
410
|
-
seq,
|
|
411
|
-
messages: changed,
|
|
412
|
-
activity: now,
|
|
413
|
-
failed,
|
|
414
|
-
deleted
|
|
415
|
-
};
|
|
416
|
-
};
|
|
417
|
-
return {
|
|
418
|
-
step,
|
|
419
|
-
get view() {
|
|
420
|
-
return {
|
|
421
|
-
seq,
|
|
422
|
-
messages: [...messages.values()],
|
|
423
|
-
activity: activityOf(activity),
|
|
424
|
-
failed,
|
|
425
|
-
deleted
|
|
426
|
-
};
|
|
427
|
-
}
|
|
428
|
-
};
|
|
429
|
-
};
|
|
430
|
-
const merge = (updates) => {
|
|
431
|
-
const last = updates.at(-1);
|
|
432
|
-
if (last === void 0) return void 0;
|
|
433
|
-
const messages = /* @__PURE__ */ new Map();
|
|
434
|
-
for (const update of updates) for (const message of update.messages) messages.set(message.id, message);
|
|
435
|
-
return {
|
|
436
|
-
seq: last.seq,
|
|
437
|
-
messages: [...messages.values()],
|
|
438
|
-
activity: last.activity,
|
|
439
|
-
failed: last.failed,
|
|
440
|
-
deleted: last.deleted
|
|
441
|
-
};
|
|
442
|
-
};
|
|
443
|
-
const wakesOf = (definition) => new Set(definition.events.flatMap((event) => event.wakes ? [event.tag] : []));
|
|
444
|
-
/**
|
|
445
|
-
* Returns the stream of view updates that a chat's entries produce, for a chat client.
|
|
446
|
-
*
|
|
447
|
-
* **Details**
|
|
448
|
-
*
|
|
449
|
-
* The entries must stream from the start of the chat's log, as `assistant.subscribe(chatId)` does
|
|
450
|
-
* without `after`. The fold reads every entry, because a message's view depends on earlier
|
|
451
|
-
* entries: a block is answered or closed later, and a decision is made later. `after` only skips
|
|
452
|
-
* emitting the updates up to that `seq`, so a client that has the view up to a `seq` receives only
|
|
453
|
-
* what changed since. Pass `after` here, not to `subscribe`: a subscription that starts after a
|
|
454
|
-
* `seq` gives the fold an incomplete log, and the updates it produces are wrong.
|
|
455
|
-
*
|
|
456
|
-
* The definition tells the fold which of the app's events wake a turn, so the view shows a turn as
|
|
457
|
-
* about to start after one. Each chunk of entries becomes one update; the stored entries, for
|
|
458
|
-
* example, arrive as one chunk.
|
|
459
|
-
*
|
|
460
|
-
* **Example** (Streaming a chat's view to a client)
|
|
461
|
-
*
|
|
462
|
-
* ```ts
|
|
463
|
-
* import { Assistant, type ChatId, ChatView } from "effect-assistant"
|
|
464
|
-
* import * as Stream from "effect/Stream"
|
|
465
|
-
*
|
|
466
|
-
* const MyAssistant = Assistant.make("my-assistant")
|
|
467
|
-
*
|
|
468
|
-
* const watch = (chatId: ChatId.ChatId, after: number) =>
|
|
469
|
-
* Stream.unwrap(
|
|
470
|
-
* MyAssistant.useSync((assistant) =>
|
|
471
|
-
* assistant.subscribe(chatId).pipe(ChatView.updates(MyAssistant, { after }))
|
|
472
|
-
* )
|
|
473
|
-
* )
|
|
474
|
-
* ```
|
|
475
|
-
*
|
|
476
|
-
* @see {@link apply} for folding the updates into a view on the client
|
|
477
|
-
*
|
|
478
|
-
* @category utils
|
|
479
|
-
* @since 0.1.0
|
|
480
|
-
*/
|
|
481
|
-
const updates = dual((args) => Stream.isStream(args[0]), (entries, definition, options) => {
|
|
482
|
-
const after = options?.after ?? 0;
|
|
483
|
-
const wakes = wakesOf(definition);
|
|
484
|
-
return entries.pipe(Stream.mapAccumArray(() => folding(wakes), (fold, chunk) => {
|
|
485
|
-
const changed = chunk.flatMap((entry) => {
|
|
486
|
-
const update = fold.step(entry);
|
|
487
|
-
return update === void 0 || entry.seq <= after ? [] : [update];
|
|
488
|
-
});
|
|
489
|
-
const merged = merge(changed);
|
|
490
|
-
return [fold, merged === void 0 ? [] : [merged]];
|
|
491
|
-
}));
|
|
492
|
-
});
|
|
493
|
-
//#endregion
|
|
494
|
-
export { Activity, AssistantMessage, Block, BlockStatus, ChatView_exports, Message, PendingDecision, SpecialistWorking, Update, UserMessage, View, apply, empty, fromEntries, isIdle, updates };
|
|
495
|
-
|
|
496
|
-
//# sourceMappingURL=ChatView.js.map
|
package/dist/ChatView.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"ChatView.js","names":["AssistantEvent.DecisionOutcome","AssistantEvent.Attachment","Decisions.Props","Decisions.Answer","AssistantActivity.empty","AssistantActivity.apply","AssistantEvent.isLibraryEvent","BlockKit.apply","Decisions.apply"],"sources":["../src/ChatView.ts"],"sourcesContent":["/**\n * This module provides the view of a chat as its user sees it. A chat client receives this view\n * and nothing else.\n *\n * A {@link View} holds the chat's messages with their blocks and decisions, the chat's\n * {@link Activity}, and whether the last turn failed or the chat was deleted. It never contains\n * the model's output, tool calls or results, what the model read, the app's own events, or why a\n * turn failed. The server streams {@link Update}s by piping the assistant's `subscribe` through\n * {@link updates}, and the client folds them with {@link apply}, starting from {@link empty}.\n * On the server, {@link fromEntries} builds a whole view from a chat's log.\n *\n * @since 0.1.0\n */\nimport { dual } from \"effect/Function\"\nimport * as Match from \"effect/Match\"\nimport * as Option from \"effect/Option\"\nimport * as Schema from \"effect/Schema\"\nimport * as Stream from \"effect/Stream\"\nimport * as AssistantActivity from \"./AssistantActivity.ts\"\nimport * as AssistantEvent from \"./AssistantEvent.ts\"\nimport * as BlockKit from \"./BlockKit.ts\"\nimport * as Decisions from \"./Decision.ts\"\nimport { DecisionId } from \"./DecisionId.ts\"\nimport { InputId } from \"./InputId.ts\"\nimport { toJson } from \"./internal/schema.ts\"\n\n/**\n * Schema for a decision as the user sees it on a decision block. It holds the call the decision is\n * for (its tool and params, which the app can display as it likes) and, once the user has\n * decided, the outcome.\n *\n * @category schemas\n * @since 0.1.0\n */\nexport const PendingDecision = Schema.Struct({\n id: DecisionId,\n tool: Schema.String,\n params: Schema.Json,\n outcome: Schema.optional(AssistantEvent.DecisionOutcome)\n})\n\n/**\n * Represents a decision as the user sees it.\n *\n * @category models\n * @since 0.1.0\n */\nexport type PendingDecision = typeof PendingDecision.Type\n\n/**\n * Schema for the status of a block. A block is `\"open\"` while it accepts an answer, `\"answered\"`\n * once the user has used it, and `\"closed\"` when it closed without an answer because the user\n * answered in words or moved on.\n *\n * @category schemas\n * @since 0.1.0\n */\nexport const BlockStatus = Schema.Literals([\"open\", \"answered\", \"closed\"])\n\n/**\n * The status of a block.\n *\n * @category models\n * @since 0.1.0\n */\nexport type BlockStatus = typeof BlockStatus.Type\n\n/**\n * Schema for a block on an assistant's message. It holds the block's kind, its props (JSON, as\n * the block's props schema encodes them), its status, the user's answer (JSON, as the block's\n * answer schema encodes it), and, for a decision block, its decisions.\n *\n * @category schemas\n * @since 0.1.0\n */\nexport const Block = Schema.Struct({\n kind: Schema.String,\n props: Schema.Json,\n status: BlockStatus,\n answer: Schema.optional(Schema.Json),\n decisions: Schema.optional(Schema.Array(PendingDecision))\n})\n\n/**\n * Represents a block on an assistant's message.\n *\n * @category models\n * @since 0.1.0\n */\nexport type Block = typeof Block.Type\n\n/**\n * Schema for a message the user sent. `at` is when it was stored, in epoch\n * milliseconds.\n *\n * @category schemas\n * @since 0.1.0\n */\nexport const UserMessage = Schema.TaggedStruct(\"UserMessage\", {\n id: InputId,\n text: Schema.String,\n attachments: Schema.optional(Schema.Array(AssistantEvent.Attachment)),\n at: Schema.Finite\n})\n\n/**\n * Represents a message the user sent.\n *\n * @category models\n * @since 0.1.0\n */\nexport type UserMessage = typeof UserMessage.Type\n\n/**\n * Schema for a message the assistant sent the user, including its block if it has one.\n *\n * @category schemas\n * @since 0.1.0\n */\nexport const AssistantMessage = Schema.TaggedStruct(\"AssistantMessage\", {\n id: Schema.String,\n text: Schema.String,\n at: Schema.Finite,\n block: Schema.optional(Block)\n})\n\n/**\n * Represents a message the assistant sent the user.\n *\n * @category models\n * @since 0.1.0\n */\nexport type AssistantMessage = typeof AssistantMessage.Type\n\n/**\n * Schema for a message in the chat, sent by either the user or the assistant.\n *\n * @category schemas\n * @since 0.1.0\n */\nexport const Message = Schema.Union([UserMessage, AssistantMessage])\n\n/**\n * Represents a message in the chat.\n *\n * @category models\n * @since 0.1.0\n */\nexport type Message = typeof Message.Type\n\n/**\n * Schema for a specialist that is working for the assistant. It holds the specialist's name and\n * when it started, in epoch milliseconds.\n *\n * @category schemas\n * @since 0.1.0\n */\nexport const SpecialistWorking = Schema.Struct({\n specialist: Schema.String,\n since: Schema.Finite\n})\n\n/**\n * Represents a specialist that is working for the assistant.\n *\n * @category models\n * @since 0.1.0\n */\nexport type SpecialistWorking = typeof SpecialistWorking.Type\n\n/**\n * Schema for what is working in the chat.\n *\n * `working` is true while the assistant runs a turn, or when a turn is about to start for\n * something that arrived. `specialists` lists the specialists that are working. When the chat is\n * idle, nothing is working, even if a workflow is waiting on a timer or a decision.\n *\n * @category schemas\n * @since 0.1.0\n */\nexport const Activity = Schema.Struct({\n working: Schema.Boolean,\n specialists: Schema.Array(SpecialistWorking)\n})\n\n/**\n * Represents what is working in the chat.\n *\n * @category models\n * @since 0.1.0\n */\nexport type Activity = typeof Activity.Type\n\n/**\n * Schema for a chat as its user sees it. It holds the chat's messages, what is working in it,\n * whether the last turn failed, and whether the chat was deleted. `seq` is the position in the\n * chat's log that the view is up to.\n *\n * @category schemas\n * @since 0.1.0\n */\nexport const View = Schema.Struct({\n seq: Schema.Int,\n messages: Schema.Array(Message),\n activity: Activity,\n /** Whether the last turn failed with no message after it. */\n failed: Schema.Boolean,\n /** Whether the chat was deleted. A deleted chat accepts nothing more, and its stream ends. */\n deleted: Schema.Boolean\n})\n\n/**\n * Represents a chat as its user sees it.\n *\n * @category models\n * @since 0.1.0\n */\nexport type View = typeof View.Type\n\n/**\n * Schema for a change to a chat's view.\n *\n * An update holds each message that is new or changed (for example, a block answered or closed,\n * or a decision made), in full. It also holds the current activity, whether the last turn failed,\n * and whether the chat was deleted. `seq` is the position in the chat's log that the update is up\n * to.\n *\n * @category schemas\n * @since 0.1.0\n */\nexport const Update = Schema.Struct({\n seq: Schema.Int,\n messages: Schema.Array(Message),\n activity: Activity,\n failed: Schema.Boolean,\n deleted: Schema.Boolean\n})\n\n/**\n * Represents a change to a chat's view.\n *\n * @category models\n * @since 0.1.0\n */\nexport type Update = typeof Update.Type\n\n/**\n * The view of a chat before anything has happened in it, with no messages and nothing working.\n * Use it as the starting point when folding updates with {@link apply}.\n *\n * @category constructors\n * @since 0.1.0\n */\nexport const empty: View = {\n seq: 0,\n messages: [],\n activity: { working: false, specialists: [] },\n failed: false,\n deleted: false\n}\n\n/**\n * Returns the view after applying an update.\n *\n * **Details**\n *\n * Each message in the update replaces the message with the same id, in place, and new messages\n * are added after the rest. The update's activity, `failed`, and `deleted` replace the view's.\n * Applying an update twice changes nothing, so a client that resubscribes from the start can fold\n * again and get the same view.\n *\n * @category combinators\n * @since 0.1.0\n */\nexport const apply = (view: View, update: Update): View => {\n const changed = new Map(update.messages.map((message) => [message.id, message]))\n const known = new Set(view.messages.map((message) => message.id))\n\n return {\n seq: Math.max(view.seq, update.seq),\n messages: [\n ...view.messages.map((message) => changed.get(message.id) ?? message),\n ...update.messages.filter((message) => !known.has(message.id))\n ],\n activity: update.activity,\n failed: update.failed,\n deleted: update.deleted\n }\n}\n\n/**\n * Returns whether nothing is working in the chat: no turn is running or about to start, and no\n * specialist is working.\n *\n * @category guards\n * @since 0.1.0\n */\nexport const isIdle = (view: View): boolean => !view.activity.working && view.activity.specialists.length === 0\n\n/**\n * Represents the options for building a view from a chat's log.\n *\n * @category models\n * @since 0.1.0\n */\nexport interface Options {\n /**\n * The tags of the app's events that wake a turn. After such an event, the view shows a turn as\n * about to start. Defaults to none.\n */\n readonly wakes?: ReadonlySet<string> | undefined\n}\n\n/**\n * Returns a chat's view, built from the entries of its log.\n *\n * **When to use**\n *\n * Use it in server-side code, for example to return a whole chat at once. A client gets the view\n * as a stream from {@link updates} instead.\n *\n * **Example** (Building a chat's view from its log)\n *\n * ```ts\n * import { type ChatId, ChatLog, ChatView } from \"effect-assistant\"\n * import * as Effect from \"effect/Effect\"\n *\n * const viewOf = (chatId: ChatId.ChatId) =>\n * ChatLog.ChatLog.use((log) => log.read(chatId)).pipe(\n * Effect.map((entries) => ChatView.fromEntries(entries))\n * )\n * ```\n *\n * @see {@link updates} for a stream of changes instead of a whole view\n *\n * @category constructors\n * @since 0.1.0\n */\nexport const fromEntries = (entries: ReadonlyArray<AssistantEvent.AnyEntry>, options?: Options): View => {\n const fold = folding(options?.wakes ?? new Set())\n\n for (const entry of entries) fold.step(entry)\n\n return fold.view\n}\n\ninterface Folding {\n readonly step: (entry: AssistantEvent.AnyEntry) => Update | undefined\n readonly view: View\n}\n\nconst decodeProps = Schema.decodeUnknownOption(Decisions.Props)\n\nconst decodeAnswer = Schema.decodeUnknownOption(Decisions.Answer)\n\nconst activityOf = (activity: AssistantActivity.State): Activity => ({\n working: activity.turnSince !== undefined || activity.queued,\n specialists: [...activity.specialists.values()].map((run) => ({\n specialist: run.specialist,\n since: run.since\n }))\n})\n\nconst sameActivity = (a: Activity, b: Activity) =>\n a.working === b.working &&\n a.specialists.length === b.specialists.length &&\n a.specialists.every((run, i) => {\n const other = b.specialists[i]\n\n return other !== undefined && run.specialist === other.specialist && run.since === other.since\n })\n\nconst decisionIds = (block: AssistantEvent.MessageBlock): ReadonlyArray<string> =>\n block.kind === Decisions.kind\n ? Option.match(decodeProps(block.props), {\n onNone: () => [],\n onSome: (props) => props.decisionIds\n })\n : []\n\nconst folding = (wakes: ReadonlySet<string>): Folding => {\n let seq = 0\n let failed = false\n let deleted = false\n let activity = AssistantActivity.empty\n let blocks: ReadonlyMap<string, BlockKit.State> = new Map()\n let decisions: ReadonlyMap<string, Decisions.State> = new Map()\n // The messages in order, and the assistant's as stored: their blocks change as the chat goes on.\n const messages = new Map<string, Message>()\n const sent = new Map<string, AssistantEvent.AssistantMessage>()\n // Which messages ask for a decision, so a decision's outcome updates the block that asked for it.\n const asking = new Map<string, Array<string>>()\n\n const blockOf = (message: AssistantEvent.AssistantMessage): Block | undefined => {\n if (message.block === undefined) return undefined\n\n const state = blocks.get(message.id)\n const status = state === undefined || state.open ? \"open\" : state.answer === undefined ? \"closed\" : \"answered\"\n const ids = decisionIds(message.block)\n\n const shown = ids.flatMap((id) => {\n const decision = decisions.get(id)\n\n if (decision === undefined) return []\n\n const { tool, params } = decision.request.origin\n const shown = { id: decision.request.id, tool, params }\n\n return [decision.outcome === undefined ? shown : { ...shown, outcome: decision.outcome }]\n })\n\n const block: Block = { kind: message.block.kind, props: toJson(message.block.props), status }\n const answered = state?.answer === undefined ? block : { ...block, answer: toJson(state.answer.value) }\n\n return message.block.kind === Decisions.kind ? { ...answered, decisions: shown } : answered\n }\n\n const viewOf = (message: AssistantEvent.AssistantMessage, at: number): AssistantMessage => {\n const block = blockOf(message)\n\n return block === undefined\n ? AssistantMessage.make({ id: message.id, text: message.text, at })\n : AssistantMessage.make({ id: message.id, text: message.text, at, block })\n }\n\n const touched = (event: AssistantEvent.AssistantEvent, at: number): ReadonlyArray<string> =>\n Match.value(event).pipe(\n Match.tagsExhaustive({\n UserMessage: (message) => {\n messages.set(\n message.id,\n message.attachments === undefined\n ? UserMessage.make({ id: message.id, text: message.text, at })\n : UserMessage.make({\n id: message.id,\n text: message.text,\n attachments: message.attachments,\n at\n })\n )\n failed = false\n\n return [message.id]\n },\n AssistantMessage: (message) => {\n sent.set(message.id, message)\n messages.set(message.id, viewOf(message, at))\n failed = false\n\n if (message.block !== undefined) {\n for (const id of decisionIds(message.block)) asking.set(id, [...(asking.get(id) ?? []), message.id])\n }\n\n return [message.id, ...(message.closes ?? [])]\n },\n BlockAnswer: (answer) => {\n failed = false\n\n const decided =\n answer.kind === Decisions.kind\n ? Option.match(decodeAnswer(answer.value), {\n onNone: () => [],\n onSome: ({ decisions }) => Object.keys(decisions).flatMap((id) => asking.get(id) ?? [])\n })\n : []\n\n return [answer.messageId, ...decided]\n },\n BlocksClosed: (closed) => closed.messageIds,\n DecisionResolved: (resolved) => asking.get(resolved.id) ?? [],\n TurnFailed: () => {\n failed = true\n\n return []\n },\n ModelOutput: () => [],\n ToolStarted: () => [],\n ToolResult: () => [],\n TurnStarted: () => [],\n TurnCompleted: () => [],\n SpecialistStarted: () => [],\n SpecialistFinished: () => [],\n DecisionRequested: () => [],\n ChatDeleted: () => {\n deleted = true\n\n return []\n }\n })\n )\n\n const step = (entry: AssistantEvent.AnyEntry): Update | undefined => {\n const before = { activity: activityOf(activity), failed, deleted }\n const { event } = entry\n\n seq = entry.seq\n activity = AssistantActivity.apply(activity, entry, { wakes })\n\n if (!AssistantEvent.isLibraryEvent(event)) {\n const now = activityOf(activity)\n\n return sameActivity(before.activity, now) ? undefined : { seq, messages: [], activity: now, failed, deleted }\n }\n\n blocks = BlockKit.apply(blocks, event)\n decisions = Decisions.apply(decisions, event)\n\n const changed = [...new Set(touched(event, entry.at))].flatMap((id) => {\n const message = sent.get(id)\n const current = messages.get(id)\n\n if (message === undefined || current === undefined) return current === undefined ? [] : [current]\n\n const next = viewOf(message, current.at)\n messages.set(id, next)\n\n return [next]\n })\n\n const now = activityOf(activity)\n\n return changed.length === 0 &&\n failed === before.failed &&\n deleted === before.deleted &&\n sameActivity(before.activity, now)\n ? undefined\n : { seq, messages: changed, activity: now, failed, deleted }\n }\n\n return {\n step,\n get view() {\n return { seq, messages: [...messages.values()], activity: activityOf(activity), failed, deleted }\n }\n }\n}\n\nconst merge = (updates: ReadonlyArray<Update>): Update | undefined => {\n const last = updates.at(-1)\n\n if (last === undefined) return undefined\n\n const messages = new Map<string, Message>()\n\n for (const update of updates) for (const message of update.messages) messages.set(message.id, message)\n\n return {\n seq: last.seq,\n messages: [...messages.values()],\n activity: last.activity,\n failed: last.failed,\n deleted: last.deleted\n }\n}\n\n/**\n * Represents an assistant's definition as {@link updates} reads it: the app's events it\n * registered, and whether each one wakes a turn. An assistant made with `Assistant.make` fits it.\n *\n * @category models\n * @since 0.1.0\n */\nexport interface Definition {\n readonly events: ReadonlyArray<{ readonly tag: string; readonly wakes: boolean }>\n}\n\n/**\n * Represents the options accepted by {@link updates}.\n *\n * @category models\n * @since 0.1.0\n */\nexport interface UpdatesOptions {\n /**\n * The `seq` up to which the client already has the view. The result contains only the updates\n * after it. Defaults to `0`, every update.\n */\n readonly after?: number | undefined\n}\n\nconst wakesOf = (definition: Definition): ReadonlySet<string> =>\n new Set(definition.events.flatMap((event) => (event.wakes ? [event.tag] : [])))\n\n/**\n * Returns the stream of view updates that a chat's entries produce, for a chat client.\n *\n * **Details**\n *\n * The entries must stream from the start of the chat's log, as `assistant.subscribe(chatId)` does\n * without `after`. The fold reads every entry, because a message's view depends on earlier\n * entries: a block is answered or closed later, and a decision is made later. `after` only skips\n * emitting the updates up to that `seq`, so a client that has the view up to a `seq` receives only\n * what changed since. Pass `after` here, not to `subscribe`: a subscription that starts after a\n * `seq` gives the fold an incomplete log, and the updates it produces are wrong.\n *\n * The definition tells the fold which of the app's events wake a turn, so the view shows a turn as\n * about to start after one. Each chunk of entries becomes one update; the stored entries, for\n * example, arrive as one chunk.\n *\n * **Example** (Streaming a chat's view to a client)\n *\n * ```ts\n * import { Assistant, type ChatId, ChatView } from \"effect-assistant\"\n * import * as Stream from \"effect/Stream\"\n *\n * const MyAssistant = Assistant.make(\"my-assistant\")\n *\n * const watch = (chatId: ChatId.ChatId, after: number) =>\n * Stream.unwrap(\n * MyAssistant.useSync((assistant) =>\n * assistant.subscribe(chatId).pipe(ChatView.updates(MyAssistant, { after }))\n * )\n * )\n * ```\n *\n * @see {@link apply} for folding the updates into a view on the client\n *\n * @category utils\n * @since 0.1.0\n */\nexport const updates: {\n (\n definition: Definition,\n options?: UpdatesOptions\n ): <E, R>(entries: Stream.Stream<AssistantEvent.AnyEntry, E, R>) => Stream.Stream<Update, E, R>\n <E, R>(\n entries: Stream.Stream<AssistantEvent.AnyEntry, E, R>,\n definition: Definition,\n options?: UpdatesOptions\n ): Stream.Stream<Update, E, R>\n} = dual(\n (args) => Stream.isStream(args[0]),\n <E, R>(\n entries: Stream.Stream<AssistantEvent.AnyEntry, E, R>,\n definition: Definition,\n options?: UpdatesOptions\n ): Stream.Stream<Update, E, R> => {\n const after = options?.after ?? 0\n const wakes = wakesOf(definition)\n\n return entries.pipe(\n Stream.mapAccumArray(\n () => folding(wakes),\n (fold, chunk) => {\n const changed = chunk.flatMap((entry) => {\n const update = fold.step(entry)\n\n return update === undefined || entry.seq <= after ? [] : [update]\n })\n\n const merged = merge(changed)\n\n return [fold, merged === undefined ? [] : [merged]]\n }\n )\n )\n }\n)\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAkCA,MAAa,kBAAkB,OAAO,OAAO;CAC3C,IAAI;CACJ,MAAM,OAAO;CACb,QAAQ,OAAO;CACf,SAAS,OAAO,SAASA,eAA8B;AACzD,CAAC;;;;;;;;;AAkBD,MAAa,c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