@agentshouse/mdruntime 0.1.0-cf281e8224a5d → 0.1.0-cf2df0bba5020
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 +21 -19
- package/dist/acceptance.d.ts +0 -3
- package/dist/acceptance.d.ts.map +1 -1
- package/dist/acceptance.js +72 -158
- package/dist/act-lanes.d.ts +30 -0
- package/dist/act-lanes.d.ts.map +1 -0
- package/dist/act-lanes.js +225 -0
- package/dist/actions.d.ts +8 -2
- package/dist/actions.d.ts.map +1 -1
- package/dist/actions.js +142 -72
- package/dist/adapter.d.ts +129 -63
- package/dist/adapter.d.ts.map +1 -1
- package/dist/attached-model.d.ts +0 -1
- package/dist/attached-model.d.ts.map +1 -1
- package/dist/attached-model.js +0 -5
- package/dist/authored.d.ts +88 -0
- package/dist/authored.d.ts.map +1 -0
- package/dist/authored.js +345 -0
- package/dist/binary.d.ts +4 -0
- package/dist/binary.d.ts.map +1 -1
- package/dist/binary.js +9 -2
- package/dist/bootstrap.d.ts +1 -1
- package/dist/bootstrap.d.ts.map +1 -1
- package/dist/bootstrap.js +6 -19
- package/dist/change-set.d.ts +4 -0
- package/dist/change-set.d.ts.map +1 -1
- package/dist/change-set.js +6 -0
- package/dist/change.d.ts +4 -0
- package/dist/change.d.ts.map +1 -1
- package/dist/change.js +3 -0
- package/dist/chat-types.d.ts +0 -2
- package/dist/chat-types.d.ts.map +1 -1
- package/dist/chat-types.js +2 -4
- package/dist/client.d.ts +21 -22
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +45 -60
- package/dist/collection.d.ts +26 -4
- package/dist/collection.d.ts.map +1 -1
- package/dist/collection.js +28 -10
- package/dist/column.d.ts +5 -0
- package/dist/column.d.ts.map +1 -0
- package/dist/column.js +9 -0
- package/dist/compile.d.ts +2 -0
- package/dist/compile.d.ts.map +1 -1
- package/dist/compile.js +20 -10
- package/dist/connection.d.ts +1 -1
- package/dist/connection.d.ts.map +1 -1
- package/dist/connection.js +35 -26
- package/dist/context.d.ts.map +1 -1
- package/dist/context.js +1 -1
- package/dist/conversation-live.d.ts +28 -15
- package/dist/conversation-live.d.ts.map +1 -1
- package/dist/conversation-live.js +25 -14
- package/dist/conversation-row.d.ts +8 -1
- package/dist/conversation-row.d.ts.map +1 -1
- package/dist/conversation-row.js +20 -7
- package/dist/conversation.d.ts +39 -16
- package/dist/conversation.d.ts.map +1 -1
- package/dist/conversation.js +340 -243
- package/dist/declaration.d.ts +10 -0
- package/dist/declaration.d.ts.map +1 -0
- package/dist/declaration.js +24 -0
- package/dist/discovery.d.ts +3 -1
- package/dist/discovery.d.ts.map +1 -1
- package/dist/discovery.js +47 -47
- package/dist/download.d.ts +6 -0
- package/dist/download.d.ts.map +1 -0
- package/dist/download.js +42 -0
- package/dist/durability.d.ts +5 -0
- package/dist/durability.d.ts.map +1 -1
- package/dist/durability.js +7 -0
- package/dist/entry.d.ts +14 -0
- package/dist/entry.d.ts.map +1 -1
- package/dist/entry.js +45 -16
- package/dist/expectation.d.ts +25 -14
- package/dist/expectation.d.ts.map +1 -1
- package/dist/expectation.js +51 -20
- package/dist/failure.d.ts +3 -0
- package/dist/failure.d.ts.map +1 -1
- package/dist/failure.js +8 -0
- package/dist/figures.d.ts.map +1 -1
- package/dist/figures.js +4 -2
- package/dist/file.d.ts +1 -1
- package/dist/file.d.ts.map +1 -1
- package/dist/file.js +3 -2
- package/dist/host.d.ts +24 -4
- package/dist/host.d.ts.map +1 -1
- package/dist/host.js +98 -22
- package/dist/house-query.d.ts +18 -0
- package/dist/house-query.d.ts.map +1 -0
- package/dist/house-query.js +54 -0
- package/dist/index.d.ts +21 -12
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +16 -8
- package/dist/install.d.ts +10 -6
- package/dist/install.d.ts.map +1 -1
- package/dist/install.js +92 -109
- package/dist/lifecycle.d.ts +2 -2
- package/dist/lifecycle.d.ts.map +1 -1
- package/dist/lifecycle.js +75 -53
- package/dist/listing.d.ts +29 -22
- package/dist/listing.d.ts.map +1 -1
- package/dist/listing.js +71 -79
- package/dist/live.d.ts +5 -3
- package/dist/live.d.ts.map +1 -1
- package/dist/live.js +74 -31
- package/dist/loaded.d.ts +7 -0
- package/dist/loaded.d.ts.map +1 -0
- package/dist/loaded.js +46 -0
- package/dist/locks.d.ts +2 -4
- package/dist/locks.d.ts.map +1 -1
- package/dist/locks.js +12 -39
- package/dist/lost.d.ts.map +1 -1
- package/dist/lost.js +11 -23
- package/dist/media.d.ts +19 -3
- package/dist/media.d.ts.map +1 -1
- package/dist/media.js +167 -33
- package/dist/memory.d.ts +9 -3
- package/dist/memory.d.ts.map +1 -1
- package/dist/memory.js +93 -32
- package/dist/messages.d.ts +17 -6
- package/dist/messages.d.ts.map +1 -1
- package/dist/messages.js +34 -27
- package/dist/modal.d.ts +2 -0
- package/dist/modal.d.ts.map +1 -0
- package/dist/modal.js +11 -0
- package/dist/model.d.ts +4 -3
- package/dist/model.d.ts.map +1 -1
- package/dist/model.js +6 -5
- package/dist/models.d.ts +4 -8
- package/dist/models.d.ts.map +1 -1
- package/dist/models.js +97 -139
- package/dist/name.d.ts +0 -1
- package/dist/name.d.ts.map +1 -1
- package/dist/name.js +3 -7
- package/dist/observation.d.ts +29 -12
- package/dist/observation.d.ts.map +1 -1
- package/dist/observation.js +206 -69
- package/dist/online.d.ts +7 -0
- package/dist/online.d.ts.map +1 -0
- package/dist/online.js +29 -0
- package/dist/opened.d.ts +1 -1
- package/dist/opened.d.ts.map +1 -1
- package/dist/opened.js +2 -2
- package/dist/opening.d.ts.map +1 -1
- package/dist/opening.js +22 -10
- package/dist/outcome.d.ts +24 -4
- package/dist/outcome.d.ts.map +1 -1
- package/dist/outcome.js +32 -2
- package/dist/overlay.d.ts +6 -2
- package/dist/overlay.d.ts.map +1 -1
- package/dist/overlay.js +37 -24
- package/dist/page-metadata.d.ts +8 -0
- package/dist/page-metadata.d.ts.map +1 -0
- package/dist/page-metadata.js +19 -0
- package/dist/parts.d.ts +5 -59
- package/dist/parts.d.ts.map +1 -1
- package/dist/parts.js +32 -122
- package/dist/preparation.d.ts +33 -7
- package/dist/preparation.d.ts.map +1 -1
- package/dist/preparation.js +210 -92
- package/dist/presence.d.ts +58 -0
- package/dist/presence.d.ts.map +1 -0
- package/dist/presence.js +231 -0
- package/dist/queries.d.ts +2 -2
- package/dist/queries.d.ts.map +1 -1
- package/dist/queries.js +85 -70
- package/dist/questions.d.ts +1 -5
- package/dist/questions.d.ts.map +1 -1
- package/dist/questions.js +3 -8
- package/dist/queue.d.ts +40 -0
- package/dist/queue.d.ts.map +1 -0
- package/dist/queue.js +480 -0
- package/dist/reach.d.ts +15 -6
- package/dist/reach.d.ts.map +1 -1
- package/dist/reach.js +177 -33
- package/dist/react.d.ts +11 -5
- package/dist/react.d.ts.map +1 -1
- package/dist/react.js +28 -5
- package/dist/readiness.d.ts +20 -4
- package/dist/readiness.d.ts.map +1 -1
- package/dist/readiness.js +17 -10
- package/dist/reads.d.ts +4 -2
- package/dist/reads.d.ts.map +1 -1
- package/dist/reads.js +11 -4
- package/dist/recovery.d.ts +4 -1
- package/dist/recovery.d.ts.map +1 -1
- package/dist/recovery.js +31 -8
- package/dist/refresh.js +2 -2
- package/dist/refusal.d.ts.map +1 -1
- package/dist/refusal.js +5 -5
- package/dist/replica.d.ts +5 -11
- package/dist/replica.d.ts.map +1 -1
- package/dist/replica.js +14 -16
- package/dist/retried.d.ts +30 -0
- package/dist/retried.d.ts.map +1 -0
- package/dist/retried.js +72 -0
- package/dist/room-draft.d.ts +30 -5
- package/dist/room-draft.d.ts.map +1 -1
- package/dist/room-draft.js +275 -65
- package/dist/room-drafts.d.ts.map +1 -1
- package/dist/room-drafts.js +14 -9
- package/dist/room-list.d.ts +10 -0
- package/dist/room-list.d.ts.map +1 -0
- package/dist/room-list.js +45 -0
- package/dist/rows.d.ts +2 -1
- package/dist/rows.d.ts.map +1 -1
- package/dist/rows.js +9 -10
- package/dist/running.d.ts +5 -0
- package/dist/running.d.ts.map +1 -0
- package/dist/running.js +10 -0
- package/dist/runtime.d.ts +11 -19
- package/dist/runtime.d.ts.map +1 -1
- package/dist/save.d.ts +43 -90
- package/dist/save.d.ts.map +1 -1
- package/dist/save.js +326 -223
- package/dist/saves.d.ts +4 -40
- package/dist/saves.d.ts.map +1 -1
- package/dist/saves.js +164 -712
- package/dist/seed.d.ts.map +1 -1
- package/dist/seed.js +4 -4
- package/dist/shown.d.ts +9 -5
- package/dist/shown.d.ts.map +1 -1
- package/dist/shown.js +94 -60
- package/dist/socket/lanes.d.ts +12 -4
- package/dist/socket/lanes.d.ts.map +1 -1
- package/dist/socket/lanes.js +34 -8
- package/dist/socket/session.d.ts +4 -1
- package/dist/socket/session.d.ts.map +1 -1
- package/dist/socket/session.js +3 -0
- package/dist/socket.d.ts +4 -6
- package/dist/socket.d.ts.map +1 -1
- package/dist/socket.js +4 -14
- package/dist/source.d.ts +7 -7
- package/dist/source.d.ts.map +1 -1
- package/dist/source.js +15 -2
- package/dist/store/installs.d.ts +2 -2
- package/dist/store/installs.d.ts.map +1 -1
- package/dist/store/installs.js +10 -4
- package/dist/store/readiness.d.ts +5 -4
- package/dist/store/readiness.d.ts.map +1 -1
- package/dist/store/readiness.js +28 -12
- package/dist/store/records.d.ts +21 -6
- package/dist/store/records.d.ts.map +1 -1
- package/dist/store/records.js +59 -22
- package/dist/store/rooms-shown.d.ts +8 -0
- package/dist/store/rooms-shown.d.ts.map +1 -0
- package/dist/store/rooms-shown.js +13 -0
- package/dist/store.d.ts +25 -15
- package/dist/store.d.ts.map +1 -1
- package/dist/store.js +50 -18
- package/dist/stored.d.ts +32 -14
- package/dist/stored.d.ts.map +1 -1
- package/dist/stored.js +187 -51
- package/dist/surface.d.ts +12 -6
- package/dist/surface.d.ts.map +1 -1
- package/dist/surface.js +66 -34
- package/dist/sync.d.ts +4 -2
- package/dist/sync.d.ts.map +1 -1
- package/dist/sync.js +126 -101
- package/dist/tables.d.ts +16 -56
- package/dist/tables.d.ts.map +1 -1
- package/dist/tables.js +86 -327
- package/dist/tallies.d.ts.map +1 -1
- package/dist/tallies.js +1 -2
- package/dist/validation.d.ts.map +1 -1
- package/dist/validation.js +4 -10
- package/dist/wire.d.ts +66 -7
- package/dist/wire.d.ts.map +1 -1
- package/dist/wire.js +25 -9
- package/package.json +3 -2
- package/dist/attempt.d.ts +0 -16
- package/dist/attempt.d.ts.map +0 -1
- package/dist/attempt.js +0 -70
- package/dist/job.d.ts +0 -33
- package/dist/job.d.ts.map +0 -1
- package/dist/job.js +0 -54
- package/dist/log.d.ts +0 -6
- package/dist/log.d.ts.map +0 -1
- package/dist/log.js +0 -14
- package/dist/socket/rooms.d.ts +0 -44
- package/dist/socket/rooms.d.ts.map +0 -1
- package/dist/socket/rooms.js +0 -90
- package/dist/store/fences.d.ts +0 -16
- package/dist/store/fences.d.ts.map +0 -1
- package/dist/store/fences.js +0 -26
package/dist/conversation.js
CHANGED
|
@@ -1,14 +1,17 @@
|
|
|
1
1
|
import { cursorOf, decodedCursor } from '@agentshouse/mdmodel';
|
|
2
|
+
import { applied, isTurn, standingState, } from '@agentshouse/session-content';
|
|
2
3
|
import { conversationChangeSet } from './change-set.js';
|
|
3
4
|
import { FACTS, QUIET, } from './conversation-live.js';
|
|
4
5
|
import { heldFacts } from './conversation-row.js';
|
|
5
|
-
import { runtimeError } from './failure.js';
|
|
6
|
+
import { runtimeError, uncaught } from './failure.js';
|
|
6
7
|
import { refusedForSpace } from './durability.js';
|
|
7
8
|
import { changeBatchIdentity, chatDraftRevision } from './identity.js';
|
|
8
|
-
import { OFFLINE } from './outcome.js';
|
|
9
|
-
import {
|
|
9
|
+
import { OFFLINE, } from './outcome.js';
|
|
10
|
+
import { runningLine } from './parts.js';
|
|
11
|
+
import { record, text } from './read.js';
|
|
10
12
|
import { openQuestion, questionAsked } from './questions.js';
|
|
11
13
|
import { untilAnswered } from './recovery.js';
|
|
14
|
+
import { retrying } from './retried.js';
|
|
12
15
|
import { capped, FROM_START, messageOrder, precedes } from './windows.js';
|
|
13
16
|
export function unheldView(conversation) {
|
|
14
17
|
return {
|
|
@@ -21,15 +24,18 @@ export function unheldView(conversation) {
|
|
|
21
24
|
commands: [],
|
|
22
25
|
options: [],
|
|
23
26
|
starting: null,
|
|
24
|
-
|
|
25
|
-
|
|
27
|
+
state: null,
|
|
28
|
+
deliveryWaiting: [],
|
|
26
29
|
deliveryUnknown: [],
|
|
27
30
|
chatDraft: null,
|
|
28
|
-
|
|
31
|
+
dismissedSuggestion: null,
|
|
32
|
+
pins: [],
|
|
29
33
|
};
|
|
30
34
|
}
|
|
31
35
|
export const SESSION_STARTED = 'Session started';
|
|
32
36
|
const NEW_CHAT = 'new:';
|
|
37
|
+
const LISTING = 'listing';
|
|
38
|
+
export const CATCHING_UP = 'catching-up';
|
|
33
39
|
export const HISTORY_CONVERSATIONS = 64;
|
|
34
40
|
export function newChat(agent) {
|
|
35
41
|
return `${NEW_CHAT}${agent}`;
|
|
@@ -42,52 +48,15 @@ function creationOf(input) {
|
|
|
42
48
|
}
|
|
43
49
|
function startedTurn(turn, operation) {
|
|
44
50
|
if (operation.op === 'begin')
|
|
45
|
-
return { turn, started: operation.at, parts: []
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
turn,
|
|
49
|
-
started: null,
|
|
50
|
-
parts: [],
|
|
51
|
-
context: { used: operation.used, window: operation.window },
|
|
52
|
-
};
|
|
53
|
-
}
|
|
54
|
-
return operation.op === 'start' && operation.index === 0
|
|
55
|
-
? { turn, started: null, parts: [structuredClone(operation.part)], context: null }
|
|
56
|
-
: null;
|
|
57
|
-
}
|
|
58
|
-
function changedFields(part, operation) {
|
|
59
|
-
if (operation.op === 'append') {
|
|
60
|
-
return { text: `${String(part.text ?? '')}${operation.text}` };
|
|
61
|
-
}
|
|
62
|
-
if (operation.op === 'status') {
|
|
63
|
-
return { status: operation.status, result: structuredClone(operation.result) };
|
|
64
|
-
}
|
|
65
|
-
if (operation.op === 'count')
|
|
66
|
-
return { count: operation.count };
|
|
67
|
-
return { entries: structuredClone(operation.entries) };
|
|
51
|
+
return { turn, started: operation.at, parts: [] };
|
|
52
|
+
const parts = applied([], operation);
|
|
53
|
+
return parts === null ? null : { turn, started: null, parts };
|
|
68
54
|
}
|
|
69
55
|
function appliedTurn(held, operation) {
|
|
70
56
|
if (operation.op === 'begin')
|
|
71
57
|
return { ...held, started: operation.at };
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
}
|
|
75
|
-
if (operation.op === 'start') {
|
|
76
|
-
if (operation.index !== 0 && operation.index !== held.parts.length)
|
|
77
|
-
return null;
|
|
78
|
-
return {
|
|
79
|
-
...held,
|
|
80
|
-
parts: [...held.parts.slice(0, operation.index), structuredClone(operation.part)],
|
|
81
|
-
};
|
|
82
|
-
}
|
|
83
|
-
const part = held.parts[operation.index];
|
|
84
|
-
if (typeof part !== 'object' || part === null)
|
|
85
|
-
return null;
|
|
86
|
-
const changed = { ...part, ...changedFields(part, operation) };
|
|
87
|
-
return {
|
|
88
|
-
...held,
|
|
89
|
-
parts: held.parts.map((kept, at) => (at === operation.index ? changed : kept)),
|
|
90
|
-
};
|
|
58
|
+
const parts = applied(held.parts, operation);
|
|
59
|
+
return parts === null ? null : { ...held, parts };
|
|
91
60
|
}
|
|
92
61
|
function windowOf(held, arriving, limit) {
|
|
93
62
|
const byIdentity = new Map(held.map((message) => [message.identity, message]));
|
|
@@ -110,7 +79,7 @@ function frameOf(asked, messages, more, total) {
|
|
|
110
79
|
};
|
|
111
80
|
}
|
|
112
81
|
function newestOf(arriving, from) {
|
|
113
|
-
return arriving.reduce((top, message) => (top === null || message.
|
|
82
|
+
return arriving.reduce((top, message) => (top === null || message.change > top ? message.change : top), from);
|
|
114
83
|
}
|
|
115
84
|
function acceptedMessage(value) {
|
|
116
85
|
if (typeof value !== 'object' || value === null)
|
|
@@ -119,6 +88,7 @@ function acceptedMessage(value) {
|
|
|
119
88
|
if (typeof record.identity !== 'string' ||
|
|
120
89
|
typeof record.turn !== 'string' ||
|
|
121
90
|
typeof record.position !== 'string' ||
|
|
91
|
+
typeof record.change !== 'string' ||
|
|
122
92
|
typeof record.text !== 'string') {
|
|
123
93
|
return null;
|
|
124
94
|
}
|
|
@@ -126,6 +96,7 @@ function acceptedMessage(value) {
|
|
|
126
96
|
identity: record.identity,
|
|
127
97
|
turn: record.turn,
|
|
128
98
|
position: record.position,
|
|
99
|
+
change: record.change,
|
|
129
100
|
text: record.text,
|
|
130
101
|
message: structuredClone(record.message),
|
|
131
102
|
};
|
|
@@ -227,17 +198,17 @@ function withSubmitted(draft, submitted) {
|
|
|
227
198
|
const { submitted: _held, ...unsent } = draft;
|
|
228
199
|
return submitted.length === 0 ? unsent : { ...unsent, submitted };
|
|
229
200
|
}
|
|
230
|
-
function
|
|
231
|
-
return
|
|
201
|
+
export function joinedDraft(first, second) {
|
|
202
|
+
return [first, second].filter((one) => one !== '').join('\n');
|
|
232
203
|
}
|
|
233
204
|
function returned(draft, submission, others) {
|
|
234
205
|
return withSubmitted({
|
|
235
206
|
conversation: draft.conversation,
|
|
236
|
-
|
|
207
|
+
text: joinedDraft(submission.text, draft.text),
|
|
237
208
|
revision: chatDraftRevision(),
|
|
238
209
|
}, others);
|
|
239
210
|
}
|
|
240
|
-
function viewOf(alive, conversation, held, frame, draft, ended,
|
|
211
|
+
function viewOf(alive, conversation, held, frame, { draft, ended, dismissed, pins }) {
|
|
241
212
|
const facts = alive.liveOf(conversation);
|
|
242
213
|
return {
|
|
243
214
|
conversation,
|
|
@@ -255,26 +226,45 @@ function viewOf(alive, conversation, held, frame, draft, ended, marked) {
|
|
|
255
226
|
commands: structuredClone(facts.commands),
|
|
256
227
|
options: structuredClone(facts.options),
|
|
257
228
|
starting: facts.starting,
|
|
258
|
-
|
|
259
|
-
|
|
229
|
+
state: sessionOf(facts, held, ended),
|
|
230
|
+
deliveryWaiting: [...facts.deliveryWaiting],
|
|
260
231
|
deliveryUnknown: [...facts.deliveryUnknown],
|
|
261
232
|
chatDraft: draft === null || unwritten(draft) ? null : { text: draft.text, revision: draft.revision },
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
: jobsOf(marked.filter(({ at }) => ended === null || at > ended), facts.turn),
|
|
233
|
+
dismissedSuggestion: dismissed,
|
|
234
|
+
pins: [...pins],
|
|
265
235
|
};
|
|
266
236
|
}
|
|
237
|
+
function standing(facts, saved, ended) {
|
|
238
|
+
if (facts.state !== null)
|
|
239
|
+
return structuredClone(facts.state);
|
|
240
|
+
if (saved === null)
|
|
241
|
+
return null;
|
|
242
|
+
const down = facts.process?.running === false;
|
|
243
|
+
return structuredClone(standingState(saved.state, saved.at, down ? saved.at : ended));
|
|
244
|
+
}
|
|
245
|
+
function sessionOf(facts, held, ended) {
|
|
246
|
+
const saved = [...held]
|
|
247
|
+
.reverse()
|
|
248
|
+
.map(({ message }) => record(message))
|
|
249
|
+
.find(({ content }) => isTurn(content));
|
|
250
|
+
const at = saved === undefined ? null : text(saved, 'accepted_at');
|
|
251
|
+
return standing(facts, saved === undefined || !isTurn(saved.content) || at === null
|
|
252
|
+
? null
|
|
253
|
+
: { state: saved.content.state, at }, ended);
|
|
254
|
+
}
|
|
267
255
|
function emit(alive, conversation) {
|
|
268
256
|
if (alive.deps.closed())
|
|
269
257
|
return;
|
|
270
258
|
for (const view of alive.views()) {
|
|
271
|
-
const
|
|
259
|
+
const held = view.held.now();
|
|
260
|
+
const { window } = held;
|
|
272
261
|
if (view.conversation !== conversation || window === null)
|
|
273
262
|
continue;
|
|
274
263
|
const ready = view.ready();
|
|
275
264
|
if (ready.length === 0)
|
|
276
265
|
continue;
|
|
277
|
-
const
|
|
266
|
+
const frame = sharedFrame(view, window);
|
|
267
|
+
const value = viewOf(alive, conversation, window, frame, held);
|
|
278
268
|
for (const watcher of ready)
|
|
279
269
|
watcher.notify(value);
|
|
280
270
|
}
|
|
@@ -285,23 +275,18 @@ function sharedFrame(view, window) {
|
|
|
285
275
|
}
|
|
286
276
|
async function slide(alive, local, conversation, arriving) {
|
|
287
277
|
let count;
|
|
288
|
-
let marked;
|
|
289
278
|
for (const view of alive.views()) {
|
|
290
279
|
const { held } = view;
|
|
291
280
|
const { window, total } = held.now();
|
|
292
281
|
if (view.conversation !== conversation || window === null)
|
|
293
282
|
continue;
|
|
294
283
|
if (arriving === null) {
|
|
295
|
-
held.set({ window: [],
|
|
284
|
+
held.set({ window: [], more: false, total: total === null ? null : 0 });
|
|
296
285
|
continue;
|
|
297
286
|
}
|
|
298
287
|
const joining = arriving.filter((message) => precedes(message, view.asked.before));
|
|
299
288
|
const next = windowOf(window, joining, view.asked.limit);
|
|
300
289
|
held.set({ window: next.messages, more: held.now().more || next.dropped });
|
|
301
|
-
if (arriving.length > 0) {
|
|
302
|
-
marked ??= await local.jobMarkers(conversation);
|
|
303
|
-
held.set({ marked });
|
|
304
|
-
}
|
|
305
290
|
if (held.now().total !== null && arriving.length > 0) {
|
|
306
291
|
count ??= await local.messageCount(conversation);
|
|
307
292
|
held.set({ total: count });
|
|
@@ -315,76 +300,103 @@ function stateOf(alive, conversation) {
|
|
|
315
300
|
return facts.turn === null ? 'idle' : 'working';
|
|
316
301
|
}
|
|
317
302
|
function rowOf(alive, conversation) {
|
|
318
|
-
const { turn, questions } = alive.liveOf(conversation);
|
|
303
|
+
const { turn, questions, deliveryWaiting } = alive.liveOf(conversation);
|
|
319
304
|
return {
|
|
320
305
|
state: stateOf(alive, conversation),
|
|
321
306
|
started: turn?.started ?? null,
|
|
322
307
|
line: turn === null ? null : runningLine(turn.parts),
|
|
323
308
|
question: openQuestion(questions),
|
|
324
309
|
asked: questionAsked(questions),
|
|
310
|
+
deliveryWaiting: [...deliveryWaiting],
|
|
325
311
|
};
|
|
326
312
|
}
|
|
327
313
|
async function entries(alive) {
|
|
328
|
-
const
|
|
329
|
-
if (
|
|
314
|
+
const listing = await heldListing(alive);
|
|
315
|
+
if (listing === null)
|
|
316
|
+
return null;
|
|
317
|
+
const list = listing.conversations;
|
|
318
|
+
const agents = new Map(listing.agents.map((agent) => [agent.id, agent]));
|
|
319
|
+
if (list.some((entry) => !agents.has(entry.agent)))
|
|
330
320
|
return null;
|
|
331
321
|
const held = list.length === 0
|
|
332
322
|
? []
|
|
333
323
|
: await alive.deps.durably((local) => local.conversationRows(list.map(({ conversation }) => conversation)));
|
|
334
324
|
const rows = new Map(held.map((one) => [one.conversation, one]));
|
|
335
|
-
return list.map((entry) =>
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
325
|
+
return list.map((entry) => {
|
|
326
|
+
const agent = agents.get(entry.agent);
|
|
327
|
+
return {
|
|
328
|
+
conversation: entry.conversation,
|
|
329
|
+
agent: { identity: agent.id, name: agent.name, retired: agent.retired_at !== null },
|
|
330
|
+
name: entry.name,
|
|
331
|
+
created: entry.created,
|
|
332
|
+
outcome: entry.outcome,
|
|
333
|
+
waiting: entry.waiting,
|
|
334
|
+
subject: entry.subject,
|
|
335
|
+
...rowOf(alive, entry.conversation),
|
|
336
|
+
...heldFacts(rows.get(entry.conversation), entry),
|
|
337
|
+
jobs: standing(alive.liveOf(entry.conversation), rows.get(entry.conversation)?.saved ?? null, entry.processEnded)?.running ?? [],
|
|
338
|
+
};
|
|
339
|
+
});
|
|
346
340
|
}
|
|
347
|
-
async function
|
|
348
|
-
const
|
|
349
|
-
if (
|
|
350
|
-
return
|
|
351
|
-
return !alive.known() && alive.deps.stream() !== undefined && alive.deps.online()
|
|
341
|
+
async function heldListing(alive) {
|
|
342
|
+
const { conversationList, agentList } = await (await alive.deps.local()).facts();
|
|
343
|
+
if (conversationList !== null && agentList !== null)
|
|
344
|
+
return { conversations: conversationList, agents: agentList };
|
|
345
|
+
return !alive.known() && alive.deps.stream() !== undefined && alive.deps.online()
|
|
346
|
+
? null
|
|
347
|
+
: { conversations: [], agents: [] };
|
|
352
348
|
}
|
|
353
349
|
async function endedIn(alive, conversation) {
|
|
354
|
-
const
|
|
355
|
-
return
|
|
350
|
+
const listing = await heldListing(alive);
|
|
351
|
+
return (listing?.conversations.find((entry) => entry.conversation === conversation)?.processEnded ??
|
|
352
|
+
null);
|
|
353
|
+
}
|
|
354
|
+
function upserted(kept, set, key) {
|
|
355
|
+
const held = new Map(kept.map((one) => [key(one), one]));
|
|
356
|
+
for (const one of set)
|
|
357
|
+
held.set(key(one), { ...one });
|
|
358
|
+
return held;
|
|
356
359
|
}
|
|
357
360
|
function listChanged(alive, change) {
|
|
358
361
|
void alive.deps
|
|
359
362
|
.durably(async (local) => {
|
|
360
|
-
const { conversationList, conversationCursor } = await local.facts();
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
363
|
+
const { conversationList, agentList, conversationCursor, readUntil } = await local.facts();
|
|
364
|
+
if (readUntil === null)
|
|
365
|
+
await local.fact('readUntil', new Date().toISOString());
|
|
366
|
+
const held = upserted(change.replacing ? [] : (conversationList ?? []), change.set, (entry) => entry.conversation);
|
|
367
|
+
for (const entry of change.set)
|
|
365
368
|
alive.created(entry.conversation);
|
|
366
|
-
}
|
|
367
369
|
if (change.gone !== null)
|
|
368
370
|
held.delete(change.gone);
|
|
369
371
|
const list = [...held.values()];
|
|
372
|
+
const agents = [
|
|
373
|
+
...upserted(change.replacing ? [] : (agentList ?? []), change.agents, (agent) => agent.id).values(),
|
|
374
|
+
];
|
|
370
375
|
if (JSON.stringify(list) !== JSON.stringify(conversationList)) {
|
|
371
376
|
await local.fact('conversationList', list);
|
|
372
377
|
}
|
|
378
|
+
if (JSON.stringify(agents) !== JSON.stringify(agentList)) {
|
|
379
|
+
await local.fact('agentList', agents);
|
|
380
|
+
}
|
|
373
381
|
if (change.cursor !== undefined) {
|
|
374
382
|
if (change.cursor !== conversationCursor) {
|
|
375
383
|
await local.fact('conversationCursor', change.cursor);
|
|
376
384
|
}
|
|
377
385
|
alive.deps.stream()?.hold(change.cursor);
|
|
378
386
|
}
|
|
379
|
-
return list;
|
|
387
|
+
return { conversations: list, agents };
|
|
388
|
+
})
|
|
389
|
+
.catch(async () => {
|
|
390
|
+
const { conversationList, agentList } = await (await alive.deps.local()).facts();
|
|
391
|
+
return { conversations: conversationList ?? [], agents: agentList ?? [] };
|
|
380
392
|
})
|
|
381
|
-
.
|
|
382
|
-
.then((list) => {
|
|
393
|
+
.then((listing) => {
|
|
383
394
|
if (alive.deps.closed() || change.paged === true)
|
|
384
395
|
return;
|
|
396
|
+
const list = listing.conversations;
|
|
385
397
|
endedAnew(alive, list);
|
|
386
398
|
if (change.cursor !== undefined) {
|
|
387
|
-
alive.held.set({ listKnown: true });
|
|
399
|
+
alive.held.set({ listKnown: true, listHeld: true, openingListed: true });
|
|
388
400
|
settleBehind(alive, list);
|
|
389
401
|
openingEnded(alive);
|
|
390
402
|
alive.deps.settled();
|
|
@@ -392,14 +404,15 @@ function listChanged(alive, change) {
|
|
|
392
404
|
emitList(alive);
|
|
393
405
|
if (!alive.known())
|
|
394
406
|
return;
|
|
395
|
-
retiredDrafts(alive,
|
|
396
|
-
const { replicated } = alive.held.now();
|
|
397
|
-
if (change.cursor === undefined
|
|
398
|
-
|
|
399
|
-
list.some(({ conversation }) => !replicated.has(conversation))
|
|
407
|
+
retiredDrafts(alive, listing);
|
|
408
|
+
const { replicated, openingRead } = alive.held.now();
|
|
409
|
+
if (change.cursor === undefined
|
|
410
|
+
? list.length !== replicated.size ||
|
|
411
|
+
list.some(({ conversation }) => !replicated.has(conversation))
|
|
412
|
+
: openingRead) {
|
|
400
413
|
alive.deps.again();
|
|
401
414
|
}
|
|
402
|
-
},
|
|
415
|
+
}, uncaught);
|
|
403
416
|
}
|
|
404
417
|
function endedAnew(alive, list) {
|
|
405
418
|
const entries = new Map(list.map((entry) => [entry.conversation, entry]));
|
|
@@ -412,20 +425,23 @@ function endedAnew(alive, list) {
|
|
|
412
425
|
emit(alive, view.conversation);
|
|
413
426
|
}
|
|
414
427
|
}
|
|
415
|
-
function
|
|
416
|
-
const reading =
|
|
428
|
+
function shownListing(alive, work) {
|
|
429
|
+
const reading = heldListing(alive);
|
|
417
430
|
alive.inOrder(async () => {
|
|
418
|
-
const
|
|
419
|
-
|
|
431
|
+
const listing = await reading.catch((error) => {
|
|
432
|
+
uncaught(error);
|
|
433
|
+
return null;
|
|
434
|
+
});
|
|
435
|
+
if (listing === null || alive.deps.closed())
|
|
420
436
|
return;
|
|
421
|
-
work(
|
|
437
|
+
work(listing);
|
|
422
438
|
});
|
|
423
439
|
}
|
|
424
440
|
async function told(alive) {
|
|
425
441
|
try {
|
|
426
442
|
while (alive.held.now().listOwed && !alive.deps.closed()) {
|
|
427
443
|
alive.held.set({ listOwed: false });
|
|
428
|
-
const value = await untilAnswered(() => entries(alive), alive.closing);
|
|
444
|
+
const value = await untilAnswered(retrying(alive.deps.retries, 'conversations', LISTING, () => entries(alive)), alive.closing, () => true, alive.deps.recovery);
|
|
429
445
|
if (alive.deps.closed())
|
|
430
446
|
return;
|
|
431
447
|
if (value === null)
|
|
@@ -463,75 +479,73 @@ export function conversationsLearned(alive) {
|
|
|
463
479
|
return;
|
|
464
480
|
if (alive.listeners.waiting())
|
|
465
481
|
tellList(alive);
|
|
466
|
-
if (!alive.agentListeners.waiting())
|
|
482
|
+
if (!alive.agentListeners.waiting() && !alive.listedListeners.waiting())
|
|
467
483
|
return;
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
484
|
+
shownListing(alive, (listing) => {
|
|
485
|
+
alive.agentListeners.welcome(agentsOf(listing));
|
|
486
|
+
alive.listedListeners.welcome(structuredClone(listing.agents));
|
|
487
|
+
});
|
|
488
|
+
}
|
|
489
|
+
function agentsOf(listing) {
|
|
490
|
+
return listing.agents
|
|
491
|
+
.filter((agent) => agent.retired_at === null)
|
|
492
|
+
.map((agent) => ({
|
|
493
|
+
identity: agent.id,
|
|
494
|
+
name: agent.name,
|
|
495
|
+
cli: agent.cli,
|
|
496
|
+
model: agent.model,
|
|
497
|
+
effort: agent.effort,
|
|
498
|
+
environment: {
|
|
499
|
+
identity: agent.environment,
|
|
500
|
+
name: agent.environment_name,
|
|
501
|
+
state: agent.environment_connected ? 'online' : 'offline',
|
|
502
|
+
},
|
|
503
|
+
}))
|
|
480
504
|
.sort((left, right) => left.name === right.name
|
|
481
505
|
? left.identity.localeCompare(right.identity)
|
|
482
506
|
: left.name.localeCompare(right.name));
|
|
483
507
|
}
|
|
484
|
-
function readAgents(alive) {
|
|
485
|
-
const stream = alive.deps.stream();
|
|
486
|
-
if (stream?.agents === undefined || !alive.deps.online() || alive.held.now().agentsReading)
|
|
487
|
-
return;
|
|
488
|
-
const { agents: read } = stream;
|
|
489
|
-
const { agentListeners } = alive;
|
|
490
|
-
alive.held.set({ agentsReading: true });
|
|
491
|
-
void alive.deps.recovery
|
|
492
|
-
.attempt('agents', async () => {
|
|
493
|
-
if (!alive.deps.online() || (!agentListeners.waiting() && !agentListeners.told())) {
|
|
494
|
-
return 'done';
|
|
495
|
-
}
|
|
496
|
-
const listed = await read();
|
|
497
|
-
if (listed === null)
|
|
498
|
-
return 'done';
|
|
499
|
-
alive.held.set({ listedAgents: listed });
|
|
500
|
-
emitAgents(alive);
|
|
501
|
-
return 'done';
|
|
502
|
-
})
|
|
503
|
-
.finally(() => {
|
|
504
|
-
alive.held.set({ agentsReading: false });
|
|
505
|
-
});
|
|
506
|
-
}
|
|
507
508
|
function emitAgents(alive) {
|
|
508
|
-
|
|
509
|
+
const { agentListeners, listedListeners } = alive;
|
|
510
|
+
if (alive.deps.closed() || (!agentListeners.told() && !listedListeners.told()))
|
|
509
511
|
return;
|
|
510
|
-
|
|
511
|
-
const value =
|
|
512
|
+
shownListing(alive, (listing) => {
|
|
513
|
+
const value = agentsOf(listing);
|
|
512
514
|
const shown = JSON.stringify(value);
|
|
513
|
-
if (shown
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
515
|
+
if (shown !== alive.held.now().agentsShown) {
|
|
516
|
+
alive.held.set({ agentsShown: shown });
|
|
517
|
+
agentListeners.tell(value);
|
|
518
|
+
}
|
|
519
|
+
const listed = JSON.stringify(listing.agents);
|
|
520
|
+
if (listed !== alive.held.now().listedShown) {
|
|
521
|
+
alive.held.set({ listedShown: listed });
|
|
522
|
+
listedListeners.tell(structuredClone(listing.agents));
|
|
523
|
+
}
|
|
517
524
|
});
|
|
518
525
|
}
|
|
526
|
+
function ran({ state, process }) {
|
|
527
|
+
return JSON.stringify([state?.running ?? null, process?.running ?? null]);
|
|
528
|
+
}
|
|
519
529
|
function setLive(alive, conversation, next) {
|
|
530
|
+
const was = ran(alive.liveOf(conversation));
|
|
520
531
|
const before = rowOf(alive, conversation);
|
|
521
532
|
alive.holdLive(conversation, next);
|
|
522
533
|
const after = rowOf(alive, conversation);
|
|
523
|
-
if (after.state !== before.state ||
|
|
534
|
+
if (after.state !== before.state ||
|
|
535
|
+
after.started !== before.started ||
|
|
536
|
+
ran(alive.liveOf(conversation)) !== was ||
|
|
537
|
+
after.deliveryWaiting.join() !== before.deliveryWaiting.join())
|
|
524
538
|
emitList(alive);
|
|
525
539
|
else if (after.line !== before.line ||
|
|
526
540
|
after.question !== before.question ||
|
|
527
541
|
after.asked !== before.asked)
|
|
528
542
|
tellSoon(alive);
|
|
529
543
|
}
|
|
530
|
-
function retiredChat(conversation,
|
|
544
|
+
function retiredChat(conversation, listing) {
|
|
531
545
|
if (!conversation.startsWith(NEW_CHAT))
|
|
532
546
|
return false;
|
|
533
547
|
const agent = conversation.slice(NEW_CHAT.length);
|
|
534
|
-
return
|
|
548
|
+
return listing.agents.some((one) => one.id === agent && one.retired_at !== null);
|
|
535
549
|
}
|
|
536
550
|
function live(alive, conversation, change) {
|
|
537
551
|
alive.factsHeard(conversation, FACTS.filter((one) => one in change));
|
|
@@ -550,8 +564,8 @@ function openingEnded(alive) {
|
|
|
550
564
|
commands: facts.has('commands') ? QUIET.commands : held.commands,
|
|
551
565
|
options: facts.has('options') ? QUIET.options : held.options,
|
|
552
566
|
starting: facts.has('starting') ? QUIET.starting : held.starting,
|
|
553
|
-
|
|
554
|
-
|
|
567
|
+
state: facts.has('state') ? QUIET.state : held.state,
|
|
568
|
+
deliveryWaiting: facts.has('deliveryWaiting') ? QUIET.deliveryWaiting : held.deliveryWaiting,
|
|
555
569
|
deliveryUnknown: facts.has('deliveryUnknown') ? QUIET.deliveryUnknown : held.deliveryUnknown,
|
|
556
570
|
});
|
|
557
571
|
emit(alive, conversation);
|
|
@@ -583,19 +597,13 @@ function handReplicated(alive) {
|
|
|
583
597
|
}
|
|
584
598
|
async function remove(alive, leaving) {
|
|
585
599
|
await alive.deps.durably(async (local) => {
|
|
586
|
-
const kept = new Set(
|
|
587
|
-
...(await local.conversations(leaving)).map(({ conversation }) => conversation),
|
|
588
|
-
...(await local.chatDrafts())
|
|
589
|
-
.filter(({ conversation }) => leaving.includes(conversation))
|
|
590
|
-
.map(({ conversation }) => conversation),
|
|
591
|
-
]);
|
|
592
|
-
await local.dropConversations(leaving);
|
|
600
|
+
const kept = new Set(await local.dropConversations(leaving));
|
|
593
601
|
const changed = [];
|
|
594
602
|
for (const conversation of leaving) {
|
|
595
603
|
if (kept.has(conversation) || alive.liveHeld(conversation) !== undefined) {
|
|
596
604
|
changed.push(conversation);
|
|
597
605
|
}
|
|
598
|
-
|
|
606
|
+
heldInViews(alive, conversation, { draft: null, dismissed: null });
|
|
599
607
|
alive.forget(conversation);
|
|
600
608
|
setLive(alive, conversation, null);
|
|
601
609
|
await slide(alive, local, conversation, null);
|
|
@@ -624,7 +632,7 @@ async function readsOf(local, conversations) {
|
|
|
624
632
|
return new Map((await local.conversations(conversations)).map((held) => [held.conversation, held]));
|
|
625
633
|
}
|
|
626
634
|
function retiredDrafts(alive, list) {
|
|
627
|
-
if (!list.some((
|
|
635
|
+
if (!list.agents.some((agent) => agent.retired_at !== null))
|
|
628
636
|
return;
|
|
629
637
|
void alive.deps
|
|
630
638
|
.local()
|
|
@@ -634,7 +642,7 @@ function retiredDrafts(alive, list) {
|
|
|
634
642
|
held.some(({ conversation }) => retiredChat(conversation, list))) {
|
|
635
643
|
alive.deps.again();
|
|
636
644
|
}
|
|
637
|
-
},
|
|
645
|
+
}, uncaught);
|
|
638
646
|
}
|
|
639
647
|
function settleBehind(alive, list) {
|
|
640
648
|
void alive.deps
|
|
@@ -660,21 +668,19 @@ function settleBehind(alive, list) {
|
|
|
660
668
|
alive.deps.settled();
|
|
661
669
|
if (owed)
|
|
662
670
|
alive.deps.again();
|
|
663
|
-
},
|
|
671
|
+
}, uncaught);
|
|
664
672
|
}
|
|
665
673
|
function keep(alive, conversation, message) {
|
|
666
674
|
return alive.deps.durably(async (local) => {
|
|
667
675
|
if (!alive.held.now().replicated.has(conversation))
|
|
668
676
|
return;
|
|
669
677
|
const read = (await readsOf(local, [conversation])).get(conversation)?.read;
|
|
670
|
-
const advances = read !== undefined &&
|
|
671
|
-
!alive.behind(conversation) &&
|
|
672
|
-
(read === null || message.position > read);
|
|
678
|
+
const advances = read !== undefined && !alive.behind(conversation) && (read === null || message.change > read);
|
|
673
679
|
await sealed(alive, local, conversation, new Set([message.turn]));
|
|
674
680
|
await local.keepMessages({
|
|
675
681
|
conversation,
|
|
676
682
|
messages: [message],
|
|
677
|
-
...(advances ? { read: { at: message.
|
|
683
|
+
...(advances ? { read: { at: message.change, partial: false } } : {}),
|
|
678
684
|
});
|
|
679
685
|
sealTurns(alive, conversation, new Set([message.turn]));
|
|
680
686
|
await slide(alive, local, conversation, [message]);
|
|
@@ -786,22 +792,21 @@ function heard(alive, event) {
|
|
|
786
792
|
live(alive, conversation, { starting: event.step });
|
|
787
793
|
return;
|
|
788
794
|
}
|
|
789
|
-
if (event.kind === '
|
|
790
|
-
live(alive, conversation, {
|
|
791
|
-
return;
|
|
792
|
-
}
|
|
793
|
-
if (event.kind === 'link') {
|
|
794
|
-
live(alive, conversation, { link: { connected: event.connected } });
|
|
795
|
+
if (event.kind === 'state') {
|
|
796
|
+
live(alive, conversation, { state: structuredClone(event.state) });
|
|
795
797
|
return;
|
|
796
798
|
}
|
|
797
799
|
if (event.kind === 'deliveries') {
|
|
798
|
-
live(alive, conversation, {
|
|
800
|
+
live(alive, conversation, {
|
|
801
|
+
deliveryWaiting: [...event.waiting],
|
|
802
|
+
deliveryUnknown: [...event.unknown],
|
|
803
|
+
});
|
|
799
804
|
return;
|
|
800
805
|
}
|
|
801
806
|
const arriving = acceptedMessage(event.message);
|
|
802
807
|
if (arriving === null)
|
|
803
808
|
return;
|
|
804
|
-
alive.heardAt(conversation, arriving.
|
|
809
|
+
alive.heardAt(conversation, arriving.change);
|
|
805
810
|
if (alive.held.now().replicated.has(conversation)) {
|
|
806
811
|
if (alive.deps.full()) {
|
|
807
812
|
alive.gapOpened(conversation);
|
|
@@ -810,6 +815,8 @@ function heard(alive, event) {
|
|
|
810
815
|
void keep(alive, conversation, arriving).catch((error) => {
|
|
811
816
|
if (refusedForSpace(error))
|
|
812
817
|
alive.gapOpened(conversation);
|
|
818
|
+
else
|
|
819
|
+
uncaught(error);
|
|
813
820
|
});
|
|
814
821
|
return;
|
|
815
822
|
}
|
|
@@ -817,19 +824,27 @@ function heard(alive, event) {
|
|
|
817
824
|
emit(alive, conversation);
|
|
818
825
|
}
|
|
819
826
|
async function kept(alive, local, conversation, following) {
|
|
820
|
-
const
|
|
821
|
-
if (
|
|
827
|
+
const draft = following === null || empty(following) ? null : following;
|
|
828
|
+
if (draft === null)
|
|
822
829
|
await local.dropChatDraft(conversation);
|
|
823
830
|
else
|
|
824
|
-
await local.keepChatDrafts([
|
|
825
|
-
|
|
831
|
+
await local.keepChatDrafts([draft]);
|
|
832
|
+
heldInViews(alive, conversation, { draft });
|
|
826
833
|
}
|
|
827
|
-
function
|
|
834
|
+
function heldInViews(alive, conversation, held) {
|
|
828
835
|
for (const view of alive.views()) {
|
|
829
836
|
if (view.conversation === conversation)
|
|
830
|
-
view.held.set(
|
|
837
|
+
view.held.set(held);
|
|
831
838
|
}
|
|
832
839
|
}
|
|
840
|
+
async function factOf(local, name) {
|
|
841
|
+
return Object.fromEntries((await local.conversationFacts())
|
|
842
|
+
.filter((fact) => fact.name === name)
|
|
843
|
+
.map(({ conversation, value }) => [conversation, value]));
|
|
844
|
+
}
|
|
845
|
+
async function dismissedIn(local, conversation) {
|
|
846
|
+
return (await factOf(local, 'dismissedSuggestion'))[conversation] ?? null;
|
|
847
|
+
}
|
|
833
848
|
async function draftAt(local, conversation) {
|
|
834
849
|
return (await local.chatDraft(conversation)) ?? undefined;
|
|
835
850
|
}
|
|
@@ -860,9 +875,9 @@ function moved(alive, agent, operation, to) {
|
|
|
860
875
|
: [{ ...unsent, conversation: to }];
|
|
861
876
|
const left = { ...(taken.length === 0 ? unsent : blank), moves: [...earlier, operation] };
|
|
862
877
|
await local.keepChatDrafts([left, ...taken]);
|
|
863
|
-
|
|
878
|
+
heldInViews(alive, from, { draft: left });
|
|
864
879
|
for (const one of taken)
|
|
865
|
-
|
|
880
|
+
heldInViews(alive, to, { draft: one });
|
|
866
881
|
alive.deps.publish(conversationChangeSet([from, to]));
|
|
867
882
|
});
|
|
868
883
|
}
|
|
@@ -893,13 +908,10 @@ async function submitted(alive, conversation, operation, draft) {
|
|
|
893
908
|
});
|
|
894
909
|
return submission;
|
|
895
910
|
}
|
|
896
|
-
async function dispatched(alive, conversation, submission) {
|
|
911
|
+
async function dispatched(alive, conversation, submission, resent = false) {
|
|
897
912
|
const dispatch = alive.sendingBegan(submission.operation);
|
|
898
913
|
try {
|
|
899
|
-
const result = await invoke(alive, 'conversations/send', conversation, {
|
|
900
|
-
operation_id: submission.operation,
|
|
901
|
-
text: submission.text,
|
|
902
|
-
});
|
|
914
|
+
const result = await invoke(alive, 'conversations/send', conversation, submission.operation, { text: submission.text }, { step: 'sending', instance: `message ${submission.operation}` });
|
|
903
915
|
dispatch.set({ answered: result.kind !== 'transport-uncertainty' });
|
|
904
916
|
if (result.kind === 'revoked-authorization')
|
|
905
917
|
return result;
|
|
@@ -907,6 +919,7 @@ async function dispatched(alive, conversation, submission) {
|
|
|
907
919
|
alive.deps.unanswered();
|
|
908
920
|
return result;
|
|
909
921
|
}
|
|
922
|
+
alive.deps.heard(submission.operation, resent);
|
|
910
923
|
const turn = answered(result, 'message_id');
|
|
911
924
|
await withDraft(alive, conversation, async (current, local) => {
|
|
912
925
|
const held = current?.submitted?.find((one) => one.operation === submission.operation);
|
|
@@ -926,11 +939,12 @@ async function dispatched(alive, conversation, submission) {
|
|
|
926
939
|
alive.sendingEnded(submission.operation, dispatch);
|
|
927
940
|
}
|
|
928
941
|
}
|
|
929
|
-
function invoke(alive, name, conversation, extra = {
|
|
930
|
-
return alive.deps.call(name, { conversation_id: conversation, ...extra });
|
|
942
|
+
function invoke(alive, name, conversation, operation, extra = {}, attempt) {
|
|
943
|
+
return alive.deps.call(name, { conversation_id: conversation, operation_id: operation, ...extra }, attempt);
|
|
931
944
|
}
|
|
932
|
-
export function conversationSurface(alive) {
|
|
945
|
+
export function conversationSurface(alive, files) {
|
|
933
946
|
return {
|
|
947
|
+
...files,
|
|
934
948
|
list() {
|
|
935
949
|
return new Promise((settle) => {
|
|
936
950
|
const answer = (listed) => {
|
|
@@ -948,8 +962,12 @@ export function conversationSurface(alive) {
|
|
|
948
962
|
return alive.deps.durably(async (local) => {
|
|
949
963
|
const { messages, more } = await local.messageWindow(conversation, asked.limit, asked.before, phrase);
|
|
950
964
|
const total = asked.total ? await local.messageCount(conversation, phrase) : null;
|
|
951
|
-
|
|
952
|
-
|
|
965
|
+
return structuredClone(viewOf(alive, conversation, messages, frameOf(asked, messages, more, total), {
|
|
966
|
+
draft: (await local.chatDraft(conversation)) ?? null,
|
|
967
|
+
ended: await endedIn(alive, conversation),
|
|
968
|
+
dismissed: await dismissedIn(local, conversation),
|
|
969
|
+
pins: (await local.facts()).chatPins,
|
|
970
|
+
}));
|
|
953
971
|
});
|
|
954
972
|
},
|
|
955
973
|
async create(input) {
|
|
@@ -957,7 +975,7 @@ export function conversationSurface(alive) {
|
|
|
957
975
|
const conversation = answered(result, 'conversation_id');
|
|
958
976
|
if (conversation === null)
|
|
959
977
|
return result;
|
|
960
|
-
const listed = (await
|
|
978
|
+
const listed = (await heldListing(alive))?.conversations ?? [];
|
|
961
979
|
if (!listed.some((entry) => entry.conversation === conversation)) {
|
|
962
980
|
alive.creating(conversation);
|
|
963
981
|
}
|
|
@@ -986,40 +1004,90 @@ export function conversationSurface(alive) {
|
|
|
986
1004
|
}
|
|
987
1005
|
return OFFLINE;
|
|
988
1006
|
},
|
|
989
|
-
answer(conversation, answer) {
|
|
990
|
-
return invoke(alive, 'conversations/interaction/answer', conversation,
|
|
991
|
-
|
|
992
|
-
|
|
1007
|
+
answer(conversation, answer, operation) {
|
|
1008
|
+
return invoke(alive, 'conversations/interaction/answer', conversation, operation, answer);
|
|
1009
|
+
},
|
|
1010
|
+
interrupt(conversation, operation) {
|
|
1011
|
+
return invoke(alive, 'conversations/interrupt', conversation, operation);
|
|
1012
|
+
},
|
|
1013
|
+
kill(conversation, operation) {
|
|
1014
|
+
return invoke(alive, 'conversations/kill', conversation, operation);
|
|
1015
|
+
},
|
|
1016
|
+
delete(conversation, operation) {
|
|
1017
|
+
return invoke(alive, 'conversations/delete', conversation, operation);
|
|
1018
|
+
},
|
|
1019
|
+
takeOut(conversation, message, operation) {
|
|
1020
|
+
return invoke(alive, 'conversations/take-out', conversation, operation, {
|
|
1021
|
+
message_id: message,
|
|
1022
|
+
});
|
|
1023
|
+
},
|
|
1024
|
+
decide({ request, room, message }, decision, operation) {
|
|
1025
|
+
return alive.deps.call('agent-functions/decide', {
|
|
1026
|
+
room_ref: room,
|
|
1027
|
+
request,
|
|
1028
|
+
...(message === undefined ? {} : { message }),
|
|
1029
|
+
decision,
|
|
1030
|
+
operation_id: operation,
|
|
1031
|
+
});
|
|
1032
|
+
},
|
|
1033
|
+
rename(conversation, name, operation) {
|
|
1034
|
+
return invoke(alive, 'conversations/rename', conversation, operation, { name });
|
|
1035
|
+
},
|
|
1036
|
+
option(conversation, option, value, operation) {
|
|
1037
|
+
return invoke(alive, 'conversations/option/set', conversation, operation, { option, value });
|
|
1038
|
+
},
|
|
1039
|
+
retry(conversation, message, operation) {
|
|
1040
|
+
return invoke(alive, 'conversations/retry', conversation, operation, {
|
|
1041
|
+
message_id: message,
|
|
993
1042
|
});
|
|
994
1043
|
},
|
|
995
|
-
|
|
996
|
-
|
|
1044
|
+
async pin(figures) {
|
|
1045
|
+
const pins = [...figures];
|
|
1046
|
+
await alive.deps.durably((local) => local.fact('chatPins', pins));
|
|
1047
|
+
const views = [...alive.views()];
|
|
1048
|
+
for (const view of views)
|
|
1049
|
+
view.held.set({ pins });
|
|
1050
|
+
for (const conversation of new Set(views.map((view) => view.conversation))) {
|
|
1051
|
+
emit(alive, conversation);
|
|
1052
|
+
}
|
|
997
1053
|
},
|
|
998
|
-
|
|
999
|
-
|
|
1054
|
+
async openTurn(conversation, turn) {
|
|
1055
|
+
await alive.deps.durably((local) => local.keepConversationFact({ conversation, name: 'openedTurn', value: turn }));
|
|
1056
|
+
tellList(alive);
|
|
1000
1057
|
},
|
|
1001
|
-
|
|
1002
|
-
return
|
|
1058
|
+
dismissSuggestion(conversation, suggestion) {
|
|
1059
|
+
return alive.deps.durably(async (local) => {
|
|
1060
|
+
if ((await dismissedIn(local, conversation)) === suggestion)
|
|
1061
|
+
return;
|
|
1062
|
+
await local.keepConversationFact({
|
|
1063
|
+
conversation,
|
|
1064
|
+
name: 'dismissedSuggestion',
|
|
1065
|
+
value: suggestion,
|
|
1066
|
+
});
|
|
1067
|
+
heldInViews(alive, conversation, { dismissed: suggestion });
|
|
1068
|
+
alive.deps.publish(conversationChangeSet([conversation]));
|
|
1069
|
+
});
|
|
1003
1070
|
},
|
|
1004
1071
|
};
|
|
1005
1072
|
}
|
|
1073
|
+
function rewritten(alive, conversation, text, revision = chatDraftRevision()) {
|
|
1074
|
+
return withDraft(alive, conversation, (current) => ({
|
|
1075
|
+
conversation,
|
|
1076
|
+
text: text(current?.text ?? ''),
|
|
1077
|
+
revision,
|
|
1078
|
+
...(current === undefined ? {} : pending(current)),
|
|
1079
|
+
}));
|
|
1080
|
+
}
|
|
1006
1081
|
export function chatDraftSurface(alive) {
|
|
1007
1082
|
return {
|
|
1008
|
-
write(conversation, draft, revision
|
|
1009
|
-
return
|
|
1010
|
-
conversation,
|
|
1011
|
-
text: draft.text,
|
|
1012
|
-
revision,
|
|
1013
|
-
...(current === undefined ? {} : pending(current)),
|
|
1014
|
-
}));
|
|
1083
|
+
write(conversation, draft, revision) {
|
|
1084
|
+
return rewritten(alive, conversation, () => draft.text, revision);
|
|
1015
1085
|
},
|
|
1016
1086
|
prepend(conversation, draft) {
|
|
1017
|
-
return
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
...(current === undefined ? {} : pending(current)),
|
|
1022
|
-
}));
|
|
1087
|
+
return rewritten(alive, conversation, (held) => joinedDraft(draft.text, held));
|
|
1088
|
+
},
|
|
1089
|
+
append(conversation, draft) {
|
|
1090
|
+
return rewritten(alive, conversation, (held) => joinedDraft(held, draft.text));
|
|
1023
1091
|
},
|
|
1024
1092
|
discard(conversation) {
|
|
1025
1093
|
return withDraft(alive, conversation, (current) => current === undefined
|
|
@@ -1029,31 +1097,37 @@ export function chatDraftSurface(alive) {
|
|
|
1029
1097
|
};
|
|
1030
1098
|
}
|
|
1031
1099
|
export async function conversationsOpened(alive, local) {
|
|
1032
|
-
const { conversationList, conversationCursor } = await local.facts();
|
|
1033
|
-
if (conversationList === null ||
|
|
1100
|
+
const { conversationList, agentList, conversationCursor } = await local.facts();
|
|
1101
|
+
if (conversationList === null || agentList === null)
|
|
1102
|
+
return;
|
|
1103
|
+
alive.held.set({ listHeld: true });
|
|
1104
|
+
if (alive.known())
|
|
1034
1105
|
return;
|
|
1035
1106
|
alive.deps.stream()?.hold(conversationCursor);
|
|
1036
1107
|
conversationsLearned(alive);
|
|
1037
1108
|
}
|
|
1109
|
+
export function reconcileOpening(alive) {
|
|
1110
|
+
alive.held.set({ openingRead: true });
|
|
1111
|
+
return reconcileConversations(alive);
|
|
1112
|
+
}
|
|
1038
1113
|
export async function reconcileConversations(alive) {
|
|
1039
1114
|
if (!alive.known())
|
|
1040
1115
|
return;
|
|
1041
1116
|
const local = await alive.deps.local();
|
|
1042
|
-
const [held,
|
|
1117
|
+
const [held, stored, { conversationList, agentList }] = await Promise.all([
|
|
1043
1118
|
local.conversations(),
|
|
1044
|
-
local.
|
|
1119
|
+
local.storedConversations(),
|
|
1045
1120
|
local.facts(),
|
|
1046
1121
|
]);
|
|
1047
1122
|
const list = conversationList ?? [];
|
|
1123
|
+
const listing = { conversations: list, agents: agentList ?? [] };
|
|
1048
1124
|
const entries = new Map(list.map((entry) => [entry.conversation, entry]));
|
|
1049
1125
|
const named = new Set(entries.keys());
|
|
1050
1126
|
const reads = new Map(held.map((one) => [one.conversation, one]));
|
|
1051
1127
|
const { replicated } = alive.held.now();
|
|
1052
|
-
const leaving = [
|
|
1053
|
-
...new Set([...replicated, ...reads.keys(), ...kept.map(({ conversation }) => conversation)]),
|
|
1054
|
-
].filter((conversation) => !named.has(conversation) &&
|
|
1128
|
+
const leaving = [...new Set([...replicated, ...stored])].filter((conversation) => !named.has(conversation) &&
|
|
1055
1129
|
!alive.isCreating(conversation) &&
|
|
1056
|
-
(!conversation.startsWith(NEW_CHAT) || retiredChat(conversation,
|
|
1130
|
+
(!conversation.startsWith(NEW_CHAT) || retiredChat(conversation, listing)));
|
|
1057
1131
|
for (const conversation of named) {
|
|
1058
1132
|
if (!replicated.has(conversation) &&
|
|
1059
1133
|
missed(alive, conversation, reads.get(conversation), entries.get(conversation))) {
|
|
@@ -1088,6 +1162,17 @@ export async function catchUpConversations(alive, progress) {
|
|
|
1088
1162
|
if (!alive.current())
|
|
1089
1163
|
progress.complete = false;
|
|
1090
1164
|
}
|
|
1165
|
+
export async function holdsSubmission(alive, operation) {
|
|
1166
|
+
if (alive.sending(operation))
|
|
1167
|
+
return true;
|
|
1168
|
+
const local = await alive.deps.local();
|
|
1169
|
+
for (const { conversation } of await local.chatDrafts()) {
|
|
1170
|
+
const held = (await local.chatDraft(conversation))?.submitted;
|
|
1171
|
+
if (held?.some((one) => one.operation === operation && one.turn === null) === true)
|
|
1172
|
+
return true;
|
|
1173
|
+
}
|
|
1174
|
+
return false;
|
|
1175
|
+
}
|
|
1091
1176
|
export async function resendConversations(alive, progress) {
|
|
1092
1177
|
const local = await alive.deps.local();
|
|
1093
1178
|
const tried = new Set();
|
|
@@ -1099,7 +1184,7 @@ export async function resendConversations(alive, progress) {
|
|
|
1099
1184
|
if (alive.deps.closed())
|
|
1100
1185
|
return;
|
|
1101
1186
|
tried.add(submission.operation);
|
|
1102
|
-
const result = await dispatched(alive, conversation, submission);
|
|
1187
|
+
const result = await dispatched(alive, conversation, submission, true);
|
|
1103
1188
|
if (result.kind === 'revoked-authorization')
|
|
1104
1189
|
return;
|
|
1105
1190
|
if (result.kind === 'transport-uncertainty') {
|
|
@@ -1119,23 +1204,25 @@ export function watchConversation(alive, conversation, options, deliver) {
|
|
|
1119
1204
|
const { view, watcher } = alive.viewJoined(key, conversation, asked, deliver);
|
|
1120
1205
|
const { held } = view;
|
|
1121
1206
|
const reading = new AbortController();
|
|
1122
|
-
void untilAnswered(() => alive.deps.durably(async (local) => {
|
|
1207
|
+
void untilAnswered(retrying(alive.deps.retries, 'reading', `window ${key}`, () => alive.deps.durably(async (local) => {
|
|
1123
1208
|
if (held.now().window !== null)
|
|
1124
1209
|
return;
|
|
1125
1210
|
const { messages, more } = await local.messageWindow(conversation, asked.limit, asked.before);
|
|
1126
1211
|
held.set({ draft: (await local.chatDraft(conversation)) ?? null });
|
|
1127
1212
|
held.set({ ended: await endedIn(alive, conversation) });
|
|
1213
|
+
held.set({ dismissed: await dismissedIn(local, conversation) });
|
|
1214
|
+
held.set({ pins: (await local.facts()).chatPins });
|
|
1128
1215
|
held.set({ window: messages });
|
|
1129
|
-
held.set({ marked: await local.jobMarkers(conversation) });
|
|
1130
1216
|
held.set({ more });
|
|
1131
1217
|
held.set({ total: asked.total ? await local.messageCount(conversation) : null });
|
|
1132
|
-
}), reading.signal)
|
|
1218
|
+
})), reading.signal)
|
|
1133
1219
|
.then(() => {
|
|
1134
1220
|
if (alive.deps.closed() || !view.admit(watcher))
|
|
1135
1221
|
return;
|
|
1136
|
-
const
|
|
1137
|
-
const shown = window ?? [];
|
|
1138
|
-
|
|
1222
|
+
const now = held.now();
|
|
1223
|
+
const shown = now.window ?? [];
|
|
1224
|
+
const frame = sharedFrame(view, shown);
|
|
1225
|
+
watcher.notify(viewOf(alive, conversation, shown, frame, now));
|
|
1139
1226
|
})
|
|
1140
1227
|
.catch(() => { });
|
|
1141
1228
|
return () => {
|
|
@@ -1151,9 +1238,13 @@ export function watchConversationList(alive, deliver) {
|
|
|
1151
1238
|
export function watchConversationAgents(alive, deliver) {
|
|
1152
1239
|
alive.agentListeners.join(deliver);
|
|
1153
1240
|
conversationsLearned(alive);
|
|
1154
|
-
readAgents(alive);
|
|
1155
1241
|
return () => alive.agentListeners.leave(deliver);
|
|
1156
1242
|
}
|
|
1243
|
+
export function watchListedAgents(alive, deliver) {
|
|
1244
|
+
alive.listedListeners.join(deliver);
|
|
1245
|
+
conversationsLearned(alive);
|
|
1246
|
+
return () => alive.listedListeners.leave(deliver);
|
|
1247
|
+
}
|
|
1157
1248
|
export function conversationsChanged(alive, set) {
|
|
1158
1249
|
for (const conversation of set.conversations)
|
|
1159
1250
|
emit(alive, conversation);
|
|
@@ -1165,26 +1256,32 @@ export function receiveConversationEvent(alive, event) {
|
|
|
1165
1256
|
if (event.whole)
|
|
1166
1257
|
alive.relistBegun();
|
|
1167
1258
|
if (alive.relisting())
|
|
1168
|
-
alive.relistHeard(event.entries);
|
|
1259
|
+
alive.relistHeard(event.entries, event.agents);
|
|
1169
1260
|
if (event.more && alive.relisting())
|
|
1170
1261
|
return;
|
|
1171
1262
|
const whole = alive.relistTaken();
|
|
1172
1263
|
listChanged(alive, {
|
|
1173
1264
|
replacing: whole !== null,
|
|
1174
|
-
set: whole === null ? event.entries : [...whole.values()],
|
|
1265
|
+
set: whole === null ? event.entries : [...whole.conversations.values()],
|
|
1266
|
+
agents: whole === null ? event.agents : [...whole.agents.values()],
|
|
1175
1267
|
gone: null,
|
|
1176
1268
|
...(event.more ? { paged: true } : { cursor: event.cursor }),
|
|
1177
1269
|
});
|
|
1178
1270
|
return;
|
|
1179
1271
|
}
|
|
1180
1272
|
if (event.kind === 'listed') {
|
|
1181
|
-
alive.relistHeard([event.entry]);
|
|
1182
|
-
listChanged(alive, { replacing: false, set: [event.entry], gone: null });
|
|
1273
|
+
alive.relistHeard([event.entry], []);
|
|
1274
|
+
listChanged(alive, { replacing: false, set: [event.entry], agents: [], gone: null });
|
|
1275
|
+
return;
|
|
1276
|
+
}
|
|
1277
|
+
if (event.kind === 'agent') {
|
|
1278
|
+
alive.relistHeard([], [event.agent]);
|
|
1279
|
+
listChanged(alive, { replacing: false, set: [], agents: [event.agent], gone: null });
|
|
1183
1280
|
return;
|
|
1184
1281
|
}
|
|
1185
1282
|
if (event.kind === 'unlisted') {
|
|
1186
1283
|
alive.relistDropped(event.conversation);
|
|
1187
|
-
listChanged(alive, { replacing: false, set: [], gone: event.conversation });
|
|
1284
|
+
listChanged(alive, { replacing: false, set: [], agents: [], gone: event.conversation });
|
|
1188
1285
|
return;
|
|
1189
1286
|
}
|
|
1190
1287
|
heard(alive, event);
|