@agentshouse/mdruntime 0.1.0-c112fd663ab59 → 0.1.0-c11fdcbfb7fe2
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 +101 -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 +165 -75
- package/dist/adapter.d.ts +209 -73
- 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 +2 -7
- package/dist/{binary.d.ts → attachment.d.ts} +8 -4
- package/dist/attachment.d.ts.map +1 -0
- package/dist/attachment.js +15 -0
- package/dist/authored.d.ts +88 -0
- package/dist/authored.d.ts.map +1 -0
- package/dist/authored.js +345 -0
- package/dist/bootstrap.d.ts +1 -1
- package/dist/bootstrap.d.ts.map +1 -1
- package/dist/bootstrap.js +6 -19
- package/dist/chain.d.ts +1 -1
- package/dist/chain.d.ts.map +1 -1
- package/dist/chain.js +5 -2
- 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 +6 -2
- 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 +29 -24
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +51 -66
- package/dist/collection.d.ts +27 -4
- package/dist/collection.d.ts.map +1 -1
- package/dist/collection.js +34 -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 +4 -1
- package/dist/compile.d.ts.map +1 -1
- package/dist/compile.js +88 -44
- package/dist/connection.d.ts +1 -1
- package/dist/connection.d.ts.map +1 -1
- package/dist/connection.js +36 -27
- package/dist/context.d.ts.map +1 -1
- package/dist/context.js +1 -1
- package/dist/conversation-live.d.ts +29 -15
- package/dist/conversation-live.d.ts.map +1 -1
- package/dist/conversation-live.js +26 -14
- package/dist/conversation-row.d.ts +20 -1
- package/dist/conversation-row.d.ts.map +1 -1
- package/dist/conversation-row.js +30 -7
- package/dist/conversation.d.ts +47 -16
- package/dist/conversation.d.ts.map +1 -1
- package/dist/conversation.js +358 -258
- 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 +25 -1
- package/dist/entry.d.ts.map +1 -1
- package/dist/entry.js +111 -34
- 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 +2 -0
- package/dist/failure.d.ts.map +1 -1
- package/dist/failure.js +6 -1
- package/dist/figures.d.ts +1 -1
- package/dist/figures.d.ts.map +1 -1
- package/dist/figures.js +6 -4
- 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 +108 -32
- package/dist/house-query.d.ts +24 -0
- package/dist/house-query.d.ts.map +1 -0
- package/dist/house-query.js +102 -0
- package/dist/identity.d.ts +1 -1
- package/dist/identity.d.ts.map +1 -1
- package/dist/identity.js +1 -1
- package/dist/index.d.ts +29 -16
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +19 -9
- package/dist/install.d.ts +10 -6
- package/dist/install.d.ts.map +1 -1
- package/dist/install.js +89 -108
- package/dist/lifecycle.d.ts +2 -2
- package/dist/lifecycle.d.ts.map +1 -1
- package/dist/lifecycle.js +76 -54
- 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 +19 -3
- package/dist/live.d.ts.map +1 -1
- package/dist/live.js +94 -36
- 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 +10 -38
- 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 +181 -34
- package/dist/memory.d.ts +10 -4
- package/dist/memory.d.ts.map +1 -1
- package/dist/memory.js +101 -40
- package/dist/messages.d.ts +21 -6
- package/dist/messages.d.ts.map +1 -1
- package/dist/messages.js +48 -30
- 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 +10 -8
- 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 +94 -138
- 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 +6 -6
- package/dist/opened.d.ts.map +1 -1
- package/dist/opened.js +4 -4
- package/dist/opening.d.ts.map +1 -1
- package/dist/opening.js +23 -12
- package/dist/outcome.d.ts +33 -13
- package/dist/outcome.d.ts.map +1 -1
- package/dist/outcome.js +36 -6
- package/dist/overlay.d.ts +23 -8
- package/dist/overlay.d.ts.map +1 -1
- package/dist/overlay.js +132 -48
- 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/paths.d.ts +1 -1
- package/dist/paths.js +1 -1
- package/dist/preparation.d.ts +33 -7
- package/dist/preparation.d.ts.map +1 -1
- package/dist/preparation.js +207 -96
- 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 +137 -79
- package/dist/query.d.ts +2 -2
- package/dist/query.d.ts.map +1 -1
- 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 +198 -38
- package/dist/react.d.ts +17 -5
- package/dist/react.d.ts.map +1 -1
- package/dist/react.js +28 -5
- package/dist/readiness.d.ts +23 -4
- package/dist/readiness.d.ts.map +1 -1
- package/dist/readiness.js +18 -10
- package/dist/reads.d.ts +4 -2
- package/dist/reads.d.ts.map +1 -1
- package/dist/reads.js +22 -7
- package/dist/recovery.d.ts +4 -1
- package/dist/recovery.d.ts.map +1 -1
- package/dist/recovery.js +31 -8
- package/dist/refresh.d.ts +1 -1
- package/dist/refresh.js +6 -6
- package/dist/refusal.d.ts.map +1 -1
- package/dist/refusal.js +2 -4
- package/dist/replica.d.ts +6 -17
- package/dist/replica.d.ts.map +1 -1
- package/dist/replica.js +17 -19
- 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 +31 -6
- package/dist/room-draft.d.ts.map +1 -1
- package/dist/room-draft.js +279 -69
- package/dist/room-drafts.d.ts.map +1 -1
- package/dist/room-drafts.js +12 -7
- package/dist/room-list.d.ts +10 -0
- package/dist/room-list.d.ts.map +1 -0
- package/dist/room-list.js +47 -0
- package/dist/rows.d.ts +11 -3
- package/dist/rows.d.ts.map +1 -1
- package/dist/rows.js +28 -15
- package/dist/run-tables.d.ts +25 -0
- package/dist/run-tables.d.ts.map +1 -0
- package/dist/run-tables.js +98 -0
- package/dist/running.d.ts +5 -0
- package/dist/running.d.ts.map +1 -0
- package/dist/running.js +10 -0
- package/dist/runs.d.ts +27 -0
- package/dist/runs.d.ts.map +1 -0
- package/dist/runs.js +92 -0
- package/dist/runtime.d.ts +13 -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 +314 -223
- package/dist/saves.d.ts +9 -45
- package/dist/saves.d.ts.map +1 -1
- package/dist/saves.js +206 -748
- package/dist/seed.d.ts.map +1 -1
- package/dist/seed.js +4 -4
- package/dist/selector.d.ts +3 -0
- package/dist/selector.d.ts.map +1 -0
- package/dist/selector.js +22 -0
- package/dist/shown.d.ts +9 -5
- package/dist/shown.d.ts.map +1 -1
- package/dist/shown.js +93 -57
- 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 +7 -7
- package/dist/socket.d.ts.map +1 -1
- package/dist/socket.js +11 -14
- package/dist/source.d.ts +24 -7
- package/dist/source.d.ts.map +1 -1
- package/dist/source.js +48 -9
- 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 +31 -9
- package/dist/store/records.d.ts.map +1 -1
- package/dist/store/records.js +69 -26
- 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 +41 -16
- package/dist/stored.d.ts.map +1 -1
- package/dist/stored.js +217 -59
- package/dist/surface.d.ts +20 -8
- package/dist/surface.d.ts.map +1 -1
- package/dist/surface.js +68 -34
- package/dist/sync.d.ts +4 -2
- package/dist/sync.d.ts.map +1 -1
- package/dist/sync.js +127 -102
- package/dist/tables.d.ts +40 -56
- package/dist/tables.d.ts.map +1 -1
- package/dist/tables.js +109 -329
- package/dist/tallies.d.ts.map +1 -1
- package/dist/tallies.js +3 -4
- package/dist/validation.d.ts +2 -0
- package/dist/validation.d.ts.map +1 -1
- package/dist/validation.js +51 -11
- package/dist/wire.d.ts +72 -12
- package/dist/wire.d.ts.map +1 -1
- package/dist/wire.js +31 -14
- package/package.json +4 -3
- package/dist/attempt.d.ts +0 -16
- package/dist/attempt.d.ts.map +0 -1
- package/dist/attempt.js +0 -70
- package/dist/binary.d.ts.map +0 -1
- package/dist/binary.js +0 -8
- 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
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,106 +275,136 @@ 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 });
|
|
308
293
|
}
|
|
309
294
|
}
|
|
310
295
|
}
|
|
311
|
-
function stateOf(
|
|
312
|
-
|
|
313
|
-
|
|
296
|
+
function stateOf({ activity }) {
|
|
297
|
+
if (activity === null)
|
|
298
|
+
return 'idle';
|
|
299
|
+
if (activity.turn === 'asking')
|
|
314
300
|
return 'asking';
|
|
315
|
-
return
|
|
301
|
+
return activity.turn === 'running' || activity.background !== false ? 'working' : 'idle';
|
|
316
302
|
}
|
|
317
303
|
function rowOf(alive, conversation) {
|
|
318
|
-
const { turn, questions } = alive.liveOf(conversation);
|
|
304
|
+
const { turn, questions, deliveryWaiting } = alive.liveOf(conversation);
|
|
319
305
|
return {
|
|
320
|
-
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
|
+
room: entry.room,
|
|
336
|
+
model: entry.model,
|
|
337
|
+
effort: entry.effort,
|
|
338
|
+
state: stateOf(entry),
|
|
339
|
+
...rowOf(alive, entry.conversation),
|
|
340
|
+
...heldFacts(rows.get(entry.conversation), entry),
|
|
341
|
+
jobs: standing(alive.liveOf(entry.conversation), rows.get(entry.conversation)?.saved ?? null, entry.processEnded)?.running ?? [],
|
|
342
|
+
};
|
|
343
|
+
});
|
|
346
344
|
}
|
|
347
|
-
async function
|
|
348
|
-
const
|
|
349
|
-
if (
|
|
350
|
-
return
|
|
351
|
-
return !alive.known() && alive.deps.stream() !== undefined && alive.deps.online()
|
|
345
|
+
async function heldListing(alive) {
|
|
346
|
+
const { conversationList, agentList } = await (await alive.deps.local()).facts();
|
|
347
|
+
if (conversationList !== null && agentList !== null)
|
|
348
|
+
return { conversations: conversationList, agents: agentList };
|
|
349
|
+
return !alive.known() && alive.deps.stream() !== undefined && alive.deps.online()
|
|
350
|
+
? null
|
|
351
|
+
: { conversations: [], agents: [] };
|
|
352
352
|
}
|
|
353
353
|
async function endedIn(alive, conversation) {
|
|
354
|
-
const
|
|
355
|
-
return
|
|
354
|
+
const listing = await heldListing(alive);
|
|
355
|
+
return (listing?.conversations.find((entry) => entry.conversation === conversation)?.processEnded ??
|
|
356
|
+
null);
|
|
357
|
+
}
|
|
358
|
+
function upserted(kept, set, key) {
|
|
359
|
+
const held = new Map(kept.map((one) => [key(one), one]));
|
|
360
|
+
for (const one of set)
|
|
361
|
+
held.set(key(one), { ...one });
|
|
362
|
+
return held;
|
|
363
|
+
}
|
|
364
|
+
async function listStored(alive, local, change) {
|
|
365
|
+
const { conversationList, agentList, conversationCursor, readUntil } = await local.facts();
|
|
366
|
+
if (readUntil === null)
|
|
367
|
+
await local.fact('readUntil', new Date().toISOString());
|
|
368
|
+
const held = upserted(change.replacing ? [] : (conversationList ?? []), change.set, (entry) => entry.conversation);
|
|
369
|
+
for (const entry of change.set)
|
|
370
|
+
alive.created(entry.conversation);
|
|
371
|
+
if (change.gone !== null)
|
|
372
|
+
held.delete(change.gone);
|
|
373
|
+
const list = [...held.values()];
|
|
374
|
+
const agents = [
|
|
375
|
+
...upserted(change.replacing ? [] : (agentList ?? []), change.agents, (agent) => agent.id).values(),
|
|
376
|
+
];
|
|
377
|
+
if (JSON.stringify(list) !== JSON.stringify(conversationList)) {
|
|
378
|
+
await local.fact('conversationList', list);
|
|
379
|
+
}
|
|
380
|
+
if (JSON.stringify(agents) !== JSON.stringify(agentList)) {
|
|
381
|
+
await local.fact('agentList', agents);
|
|
382
|
+
}
|
|
383
|
+
if (change.cursor !== undefined && !alive.held.now().listRefused) {
|
|
384
|
+
if (change.cursor !== conversationCursor) {
|
|
385
|
+
await local.fact('conversationCursor', change.cursor);
|
|
386
|
+
}
|
|
387
|
+
alive.deps.stream()?.hold(change.cursor);
|
|
388
|
+
}
|
|
389
|
+
return { conversations: list, agents };
|
|
356
390
|
}
|
|
357
391
|
function listChanged(alive, change) {
|
|
358
392
|
void alive.deps
|
|
359
|
-
.durably(
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
}
|
|
367
|
-
if (change.gone !== null)
|
|
368
|
-
held.delete(change.gone);
|
|
369
|
-
const list = [...held.values()];
|
|
370
|
-
if (JSON.stringify(list) !== JSON.stringify(conversationList)) {
|
|
371
|
-
await local.fact('conversationList', list);
|
|
372
|
-
}
|
|
373
|
-
if (change.cursor !== undefined) {
|
|
374
|
-
if (change.cursor !== conversationCursor) {
|
|
375
|
-
await local.fact('conversationCursor', change.cursor);
|
|
376
|
-
}
|
|
377
|
-
alive.deps.stream()?.hold(change.cursor);
|
|
378
|
-
}
|
|
379
|
-
return list;
|
|
393
|
+
.durably((local) => listStored(alive, local, change).catch((error) => {
|
|
394
|
+
alive.held.set({ listRefused: true });
|
|
395
|
+
throw error;
|
|
396
|
+
}))
|
|
397
|
+
.catch(async () => {
|
|
398
|
+
const { conversationList, agentList } = await (await alive.deps.local()).facts();
|
|
399
|
+
return { conversations: conversationList ?? [], agents: agentList ?? [] };
|
|
380
400
|
})
|
|
381
|
-
.
|
|
382
|
-
.then((list) => {
|
|
401
|
+
.then((listing) => {
|
|
383
402
|
if (alive.deps.closed() || change.paged === true)
|
|
384
403
|
return;
|
|
404
|
+
const list = listing.conversations;
|
|
385
405
|
endedAnew(alive, list);
|
|
386
406
|
if (change.cursor !== undefined) {
|
|
387
|
-
alive.held.set({ listKnown: true });
|
|
407
|
+
alive.held.set({ listKnown: true, listHeld: true, openingListed: true });
|
|
388
408
|
settleBehind(alive, list);
|
|
389
409
|
openingEnded(alive);
|
|
390
410
|
alive.deps.settled();
|
|
@@ -392,11 +412,12 @@ function listChanged(alive, change) {
|
|
|
392
412
|
emitList(alive);
|
|
393
413
|
if (!alive.known())
|
|
394
414
|
return;
|
|
395
|
-
retiredDrafts(alive,
|
|
396
|
-
const { replicated } = alive.held.now();
|
|
397
|
-
if (change.cursor === undefined
|
|
398
|
-
|
|
399
|
-
list.some(({ conversation }) => !replicated.has(conversation))
|
|
415
|
+
retiredDrafts(alive, listing);
|
|
416
|
+
const { replicated, openingRead } = alive.held.now();
|
|
417
|
+
if (change.cursor === undefined
|
|
418
|
+
? list.length !== replicated.size ||
|
|
419
|
+
list.some(({ conversation }) => !replicated.has(conversation))
|
|
420
|
+
: openingRead) {
|
|
400
421
|
alive.deps.again();
|
|
401
422
|
}
|
|
402
423
|
}, uncaught);
|
|
@@ -412,23 +433,23 @@ function endedAnew(alive, list) {
|
|
|
412
433
|
emit(alive, view.conversation);
|
|
413
434
|
}
|
|
414
435
|
}
|
|
415
|
-
function
|
|
416
|
-
const reading =
|
|
436
|
+
function shownListing(alive, work) {
|
|
437
|
+
const reading = heldListing(alive);
|
|
417
438
|
alive.inOrder(async () => {
|
|
418
|
-
const
|
|
439
|
+
const listing = await reading.catch((error) => {
|
|
419
440
|
uncaught(error);
|
|
420
441
|
return null;
|
|
421
442
|
});
|
|
422
|
-
if (
|
|
443
|
+
if (listing === null || alive.deps.closed())
|
|
423
444
|
return;
|
|
424
|
-
work(
|
|
445
|
+
work(listing);
|
|
425
446
|
});
|
|
426
447
|
}
|
|
427
448
|
async function told(alive) {
|
|
428
449
|
try {
|
|
429
450
|
while (alive.held.now().listOwed && !alive.deps.closed()) {
|
|
430
451
|
alive.held.set({ listOwed: false });
|
|
431
|
-
const value = await untilAnswered(() => entries(alive), alive.closing);
|
|
452
|
+
const value = await untilAnswered(retrying(alive.deps.retries, 'conversations', LISTING, () => entries(alive)), alive.closing, () => true, alive.deps.recovery);
|
|
432
453
|
if (alive.deps.closed())
|
|
433
454
|
return;
|
|
434
455
|
if (value === null)
|
|
@@ -466,75 +487,73 @@ export function conversationsLearned(alive) {
|
|
|
466
487
|
return;
|
|
467
488
|
if (alive.listeners.waiting())
|
|
468
489
|
tellList(alive);
|
|
469
|
-
if (!alive.agentListeners.waiting())
|
|
490
|
+
if (!alive.agentListeners.waiting() && !alive.listedListeners.waiting())
|
|
470
491
|
return;
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
492
|
+
shownListing(alive, (listing) => {
|
|
493
|
+
alive.agentListeners.welcome(agentsOf(listing));
|
|
494
|
+
alive.listedListeners.welcome(structuredClone(listing.agents));
|
|
495
|
+
});
|
|
496
|
+
}
|
|
497
|
+
function agentsOf(listing) {
|
|
498
|
+
return listing.agents
|
|
499
|
+
.filter((agent) => agent.retired_at === null)
|
|
500
|
+
.map((agent) => ({
|
|
501
|
+
identity: agent.id,
|
|
502
|
+
name: agent.name,
|
|
503
|
+
cli: agent.cli,
|
|
504
|
+
model: agent.model,
|
|
505
|
+
effort: agent.effort,
|
|
506
|
+
model_fixed: agent.model_fixed,
|
|
507
|
+
environment: {
|
|
508
|
+
identity: agent.environment,
|
|
509
|
+
name: agent.environment_name,
|
|
510
|
+
state: agent.environment_connected ? 'online' : 'offline',
|
|
511
|
+
},
|
|
512
|
+
}))
|
|
483
513
|
.sort((left, right) => left.name === right.name
|
|
484
514
|
? left.identity.localeCompare(right.identity)
|
|
485
515
|
: left.name.localeCompare(right.name));
|
|
486
516
|
}
|
|
487
|
-
function readAgents(alive) {
|
|
488
|
-
const stream = alive.deps.stream();
|
|
489
|
-
if (stream?.agents === undefined || !alive.deps.online() || alive.held.now().agentsReading)
|
|
490
|
-
return;
|
|
491
|
-
const { agents: read } = stream;
|
|
492
|
-
const { agentListeners } = alive;
|
|
493
|
-
alive.held.set({ agentsReading: true });
|
|
494
|
-
void alive.deps.recovery
|
|
495
|
-
.attempt('agents', async () => {
|
|
496
|
-
if (!alive.deps.online() || (!agentListeners.waiting() && !agentListeners.told())) {
|
|
497
|
-
return 'done';
|
|
498
|
-
}
|
|
499
|
-
const listed = await read();
|
|
500
|
-
if (listed === null)
|
|
501
|
-
return 'done';
|
|
502
|
-
alive.held.set({ listedAgents: listed });
|
|
503
|
-
emitAgents(alive);
|
|
504
|
-
return 'done';
|
|
505
|
-
})
|
|
506
|
-
.finally(() => {
|
|
507
|
-
alive.held.set({ agentsReading: false });
|
|
508
|
-
});
|
|
509
|
-
}
|
|
510
517
|
function emitAgents(alive) {
|
|
511
|
-
|
|
518
|
+
const { agentListeners, listedListeners } = alive;
|
|
519
|
+
if (alive.deps.closed() || (!agentListeners.told() && !listedListeners.told()))
|
|
512
520
|
return;
|
|
513
|
-
|
|
514
|
-
const value =
|
|
521
|
+
shownListing(alive, (listing) => {
|
|
522
|
+
const value = agentsOf(listing);
|
|
515
523
|
const shown = JSON.stringify(value);
|
|
516
|
-
if (shown
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
524
|
+
if (shown !== alive.held.now().agentsShown) {
|
|
525
|
+
alive.held.set({ agentsShown: shown });
|
|
526
|
+
agentListeners.tell(value);
|
|
527
|
+
}
|
|
528
|
+
const listed = JSON.stringify(listing.agents);
|
|
529
|
+
if (listed !== alive.held.now().listedShown) {
|
|
530
|
+
alive.held.set({ listedShown: listed });
|
|
531
|
+
listedListeners.tell(structuredClone(listing.agents));
|
|
532
|
+
}
|
|
520
533
|
});
|
|
521
534
|
}
|
|
535
|
+
function ran({ state, process }) {
|
|
536
|
+
return JSON.stringify([state?.running ?? null, process?.running ?? null]);
|
|
537
|
+
}
|
|
522
538
|
function setLive(alive, conversation, next) {
|
|
539
|
+
const was = ran(alive.liveOf(conversation));
|
|
523
540
|
const before = rowOf(alive, conversation);
|
|
524
541
|
alive.holdLive(conversation, next);
|
|
525
542
|
const after = rowOf(alive, conversation);
|
|
526
|
-
if (after.
|
|
543
|
+
if (after.started !== before.started ||
|
|
544
|
+
ran(alive.liveOf(conversation)) !== was ||
|
|
545
|
+
after.deliveryWaiting.join() !== before.deliveryWaiting.join())
|
|
527
546
|
emitList(alive);
|
|
528
547
|
else if (after.line !== before.line ||
|
|
529
548
|
after.question !== before.question ||
|
|
530
549
|
after.asked !== before.asked)
|
|
531
550
|
tellSoon(alive);
|
|
532
551
|
}
|
|
533
|
-
function retiredChat(conversation,
|
|
552
|
+
function retiredChat(conversation, listing) {
|
|
534
553
|
if (!conversation.startsWith(NEW_CHAT))
|
|
535
554
|
return false;
|
|
536
555
|
const agent = conversation.slice(NEW_CHAT.length);
|
|
537
|
-
return
|
|
556
|
+
return listing.agents.some((one) => one.id === agent && one.retired_at !== null);
|
|
538
557
|
}
|
|
539
558
|
function live(alive, conversation, change) {
|
|
540
559
|
alive.factsHeard(conversation, FACTS.filter((one) => one in change));
|
|
@@ -553,8 +572,8 @@ function openingEnded(alive) {
|
|
|
553
572
|
commands: facts.has('commands') ? QUIET.commands : held.commands,
|
|
554
573
|
options: facts.has('options') ? QUIET.options : held.options,
|
|
555
574
|
starting: facts.has('starting') ? QUIET.starting : held.starting,
|
|
556
|
-
|
|
557
|
-
|
|
575
|
+
state: facts.has('state') ? QUIET.state : held.state,
|
|
576
|
+
deliveryWaiting: facts.has('deliveryWaiting') ? QUIET.deliveryWaiting : held.deliveryWaiting,
|
|
558
577
|
deliveryUnknown: facts.has('deliveryUnknown') ? QUIET.deliveryUnknown : held.deliveryUnknown,
|
|
559
578
|
});
|
|
560
579
|
emit(alive, conversation);
|
|
@@ -586,19 +605,13 @@ function handReplicated(alive) {
|
|
|
586
605
|
}
|
|
587
606
|
async function remove(alive, leaving) {
|
|
588
607
|
await alive.deps.durably(async (local) => {
|
|
589
|
-
const kept = new Set(
|
|
590
|
-
...(await local.conversations(leaving)).map(({ conversation }) => conversation),
|
|
591
|
-
...(await local.chatDrafts())
|
|
592
|
-
.filter(({ conversation }) => leaving.includes(conversation))
|
|
593
|
-
.map(({ conversation }) => conversation),
|
|
594
|
-
]);
|
|
595
|
-
await local.dropConversations(leaving);
|
|
608
|
+
const kept = new Set(await local.dropConversations(leaving));
|
|
596
609
|
const changed = [];
|
|
597
610
|
for (const conversation of leaving) {
|
|
598
611
|
if (kept.has(conversation) || alive.liveHeld(conversation) !== undefined) {
|
|
599
612
|
changed.push(conversation);
|
|
600
613
|
}
|
|
601
|
-
|
|
614
|
+
heldInViews(alive, conversation, { draft: null, dismissed: null });
|
|
602
615
|
alive.forget(conversation);
|
|
603
616
|
setLive(alive, conversation, null);
|
|
604
617
|
await slide(alive, local, conversation, null);
|
|
@@ -627,7 +640,7 @@ async function readsOf(local, conversations) {
|
|
|
627
640
|
return new Map((await local.conversations(conversations)).map((held) => [held.conversation, held]));
|
|
628
641
|
}
|
|
629
642
|
function retiredDrafts(alive, list) {
|
|
630
|
-
if (!list.some((
|
|
643
|
+
if (!list.agents.some((agent) => agent.retired_at !== null))
|
|
631
644
|
return;
|
|
632
645
|
void alive.deps
|
|
633
646
|
.local()
|
|
@@ -670,14 +683,12 @@ function keep(alive, conversation, message) {
|
|
|
670
683
|
if (!alive.held.now().replicated.has(conversation))
|
|
671
684
|
return;
|
|
672
685
|
const read = (await readsOf(local, [conversation])).get(conversation)?.read;
|
|
673
|
-
const advances = read !== undefined &&
|
|
674
|
-
!alive.behind(conversation) &&
|
|
675
|
-
(read === null || message.position > read);
|
|
686
|
+
const advances = read !== undefined && !alive.behind(conversation) && (read === null || message.change > read);
|
|
676
687
|
await sealed(alive, local, conversation, new Set([message.turn]));
|
|
677
688
|
await local.keepMessages({
|
|
678
689
|
conversation,
|
|
679
690
|
messages: [message],
|
|
680
|
-
...(advances ? { read: { at: message.
|
|
691
|
+
...(advances ? { read: { at: message.change, partial: false } } : {}),
|
|
681
692
|
});
|
|
682
693
|
sealTurns(alive, conversation, new Set([message.turn]));
|
|
683
694
|
await slide(alive, local, conversation, [message]);
|
|
@@ -789,22 +800,21 @@ function heard(alive, event) {
|
|
|
789
800
|
live(alive, conversation, { starting: event.step });
|
|
790
801
|
return;
|
|
791
802
|
}
|
|
792
|
-
if (event.kind === '
|
|
793
|
-
live(alive, conversation, {
|
|
794
|
-
return;
|
|
795
|
-
}
|
|
796
|
-
if (event.kind === 'link') {
|
|
797
|
-
live(alive, conversation, { link: { connected: event.connected } });
|
|
803
|
+
if (event.kind === 'state') {
|
|
804
|
+
live(alive, conversation, { state: structuredClone(event.state) });
|
|
798
805
|
return;
|
|
799
806
|
}
|
|
800
807
|
if (event.kind === 'deliveries') {
|
|
801
|
-
live(alive, conversation, {
|
|
808
|
+
live(alive, conversation, {
|
|
809
|
+
deliveryWaiting: [...event.waiting],
|
|
810
|
+
deliveryUnknown: [...event.unknown],
|
|
811
|
+
});
|
|
802
812
|
return;
|
|
803
813
|
}
|
|
804
814
|
const arriving = acceptedMessage(event.message);
|
|
805
815
|
if (arriving === null)
|
|
806
816
|
return;
|
|
807
|
-
alive.heardAt(conversation, arriving.
|
|
817
|
+
alive.heardAt(conversation, arriving.change);
|
|
808
818
|
if (alive.held.now().replicated.has(conversation)) {
|
|
809
819
|
if (alive.deps.full()) {
|
|
810
820
|
alive.gapOpened(conversation);
|
|
@@ -822,19 +832,27 @@ function heard(alive, event) {
|
|
|
822
832
|
emit(alive, conversation);
|
|
823
833
|
}
|
|
824
834
|
async function kept(alive, local, conversation, following) {
|
|
825
|
-
const
|
|
826
|
-
if (
|
|
835
|
+
const draft = following === null || empty(following) ? null : following;
|
|
836
|
+
if (draft === null)
|
|
827
837
|
await local.dropChatDraft(conversation);
|
|
828
838
|
else
|
|
829
|
-
await local.keepChatDrafts([
|
|
830
|
-
|
|
839
|
+
await local.keepChatDrafts([draft]);
|
|
840
|
+
heldInViews(alive, conversation, { draft });
|
|
831
841
|
}
|
|
832
|
-
function
|
|
842
|
+
function heldInViews(alive, conversation, held) {
|
|
833
843
|
for (const view of alive.views()) {
|
|
834
844
|
if (view.conversation === conversation)
|
|
835
|
-
view.held.set(
|
|
845
|
+
view.held.set(held);
|
|
836
846
|
}
|
|
837
847
|
}
|
|
848
|
+
async function factOf(local, name) {
|
|
849
|
+
return Object.fromEntries((await local.conversationFacts())
|
|
850
|
+
.filter((fact) => fact.name === name)
|
|
851
|
+
.map(({ conversation, value }) => [conversation, value]));
|
|
852
|
+
}
|
|
853
|
+
async function dismissedIn(local, conversation) {
|
|
854
|
+
return (await factOf(local, 'dismissedSuggestion'))[conversation] ?? null;
|
|
855
|
+
}
|
|
838
856
|
async function draftAt(local, conversation) {
|
|
839
857
|
return (await local.chatDraft(conversation)) ?? undefined;
|
|
840
858
|
}
|
|
@@ -865,9 +883,9 @@ function moved(alive, agent, operation, to) {
|
|
|
865
883
|
: [{ ...unsent, conversation: to }];
|
|
866
884
|
const left = { ...(taken.length === 0 ? unsent : blank), moves: [...earlier, operation] };
|
|
867
885
|
await local.keepChatDrafts([left, ...taken]);
|
|
868
|
-
|
|
886
|
+
heldInViews(alive, from, { draft: left });
|
|
869
887
|
for (const one of taken)
|
|
870
|
-
|
|
888
|
+
heldInViews(alive, to, { draft: one });
|
|
871
889
|
alive.deps.publish(conversationChangeSet([from, to]));
|
|
872
890
|
});
|
|
873
891
|
}
|
|
@@ -898,13 +916,10 @@ async function submitted(alive, conversation, operation, draft) {
|
|
|
898
916
|
});
|
|
899
917
|
return submission;
|
|
900
918
|
}
|
|
901
|
-
async function dispatched(alive, conversation, submission) {
|
|
919
|
+
async function dispatched(alive, conversation, submission, resent = false) {
|
|
902
920
|
const dispatch = alive.sendingBegan(submission.operation);
|
|
903
921
|
try {
|
|
904
|
-
const result = await invoke(alive, 'conversations/send', conversation, {
|
|
905
|
-
operation_id: submission.operation,
|
|
906
|
-
text: submission.text,
|
|
907
|
-
});
|
|
922
|
+
const result = await invoke(alive, 'conversations/send', conversation, submission.operation, { text: submission.text }, { step: 'sending', instance: `message ${submission.operation}` });
|
|
908
923
|
dispatch.set({ answered: result.kind !== 'transport-uncertainty' });
|
|
909
924
|
if (result.kind === 'revoked-authorization')
|
|
910
925
|
return result;
|
|
@@ -912,6 +927,7 @@ async function dispatched(alive, conversation, submission) {
|
|
|
912
927
|
alive.deps.unanswered();
|
|
913
928
|
return result;
|
|
914
929
|
}
|
|
930
|
+
alive.deps.heard(submission.operation, resent);
|
|
915
931
|
const turn = answered(result, 'message_id');
|
|
916
932
|
await withDraft(alive, conversation, async (current, local) => {
|
|
917
933
|
const held = current?.submitted?.find((one) => one.operation === submission.operation);
|
|
@@ -931,11 +947,12 @@ async function dispatched(alive, conversation, submission) {
|
|
|
931
947
|
alive.sendingEnded(submission.operation, dispatch);
|
|
932
948
|
}
|
|
933
949
|
}
|
|
934
|
-
function invoke(alive, name, conversation, extra = {
|
|
935
|
-
return alive.deps.call(name, { conversation_id: conversation, ...extra });
|
|
950
|
+
function invoke(alive, name, conversation, operation, extra = {}, attempt) {
|
|
951
|
+
return alive.deps.call(name, { conversation_id: conversation, operation_id: operation, ...extra }, attempt);
|
|
936
952
|
}
|
|
937
|
-
export function conversationSurface(alive) {
|
|
953
|
+
export function conversationSurface(alive, files) {
|
|
938
954
|
return {
|
|
955
|
+
...files,
|
|
939
956
|
list() {
|
|
940
957
|
return new Promise((settle) => {
|
|
941
958
|
const answer = (listed) => {
|
|
@@ -953,8 +970,12 @@ export function conversationSurface(alive) {
|
|
|
953
970
|
return alive.deps.durably(async (local) => {
|
|
954
971
|
const { messages, more } = await local.messageWindow(conversation, asked.limit, asked.before, phrase);
|
|
955
972
|
const total = asked.total ? await local.messageCount(conversation, phrase) : null;
|
|
956
|
-
|
|
957
|
-
|
|
973
|
+
return structuredClone(viewOf(alive, conversation, messages, frameOf(asked, messages, more, total), {
|
|
974
|
+
draft: (await local.chatDraft(conversation)) ?? null,
|
|
975
|
+
ended: await endedIn(alive, conversation),
|
|
976
|
+
dismissed: await dismissedIn(local, conversation),
|
|
977
|
+
pins: (await local.facts()).chatPins,
|
|
978
|
+
}));
|
|
958
979
|
});
|
|
959
980
|
},
|
|
960
981
|
async create(input) {
|
|
@@ -962,7 +983,7 @@ export function conversationSurface(alive) {
|
|
|
962
983
|
const conversation = answered(result, 'conversation_id');
|
|
963
984
|
if (conversation === null)
|
|
964
985
|
return result;
|
|
965
|
-
const listed = (await
|
|
986
|
+
const listed = (await heldListing(alive))?.conversations ?? [];
|
|
966
987
|
if (!listed.some((entry) => entry.conversation === conversation)) {
|
|
967
988
|
alive.creating(conversation);
|
|
968
989
|
}
|
|
@@ -991,40 +1012,90 @@ export function conversationSurface(alive) {
|
|
|
991
1012
|
}
|
|
992
1013
|
return OFFLINE;
|
|
993
1014
|
},
|
|
994
|
-
answer(conversation, answer) {
|
|
995
|
-
return invoke(alive, 'conversations/interaction/answer', conversation,
|
|
996
|
-
|
|
997
|
-
|
|
1015
|
+
answer(conversation, answer, operation) {
|
|
1016
|
+
return invoke(alive, 'conversations/interaction/answer', conversation, operation, answer);
|
|
1017
|
+
},
|
|
1018
|
+
interrupt(conversation, operation) {
|
|
1019
|
+
return invoke(alive, 'conversations/interrupt', conversation, operation);
|
|
1020
|
+
},
|
|
1021
|
+
kill(conversation, operation) {
|
|
1022
|
+
return invoke(alive, 'conversations/kill', conversation, operation);
|
|
1023
|
+
},
|
|
1024
|
+
delete(conversation, operation) {
|
|
1025
|
+
return invoke(alive, 'conversations/delete', conversation, operation);
|
|
1026
|
+
},
|
|
1027
|
+
takeOut(conversation, message, operation) {
|
|
1028
|
+
return invoke(alive, 'conversations/take-out', conversation, operation, {
|
|
1029
|
+
message_id: message,
|
|
998
1030
|
});
|
|
999
1031
|
},
|
|
1000
|
-
|
|
1001
|
-
return
|
|
1032
|
+
decide({ request, room, message }, decision, operation) {
|
|
1033
|
+
return alive.deps.call('agent-functions/decide', {
|
|
1034
|
+
room_ref: room,
|
|
1035
|
+
request,
|
|
1036
|
+
...(message === undefined ? {} : { message }),
|
|
1037
|
+
decision,
|
|
1038
|
+
operation_id: operation,
|
|
1039
|
+
});
|
|
1040
|
+
},
|
|
1041
|
+
rename(conversation, name, operation) {
|
|
1042
|
+
return invoke(alive, 'conversations/rename', conversation, operation, { name });
|
|
1002
1043
|
},
|
|
1003
|
-
|
|
1004
|
-
return invoke(alive, 'conversations/
|
|
1044
|
+
set(conversation, settings, operation) {
|
|
1045
|
+
return invoke(alive, 'conversations/set', conversation, operation, settings);
|
|
1005
1046
|
},
|
|
1006
|
-
|
|
1007
|
-
return invoke(alive, 'conversations/
|
|
1047
|
+
retry(conversation, message, operation) {
|
|
1048
|
+
return invoke(alive, 'conversations/retry', conversation, operation, {
|
|
1049
|
+
message_id: message,
|
|
1050
|
+
});
|
|
1051
|
+
},
|
|
1052
|
+
async pin(figures) {
|
|
1053
|
+
const pins = [...figures];
|
|
1054
|
+
await alive.deps.durably((local) => local.fact('chatPins', pins));
|
|
1055
|
+
const views = [...alive.views()];
|
|
1056
|
+
for (const view of views)
|
|
1057
|
+
view.held.set({ pins });
|
|
1058
|
+
for (const conversation of new Set(views.map((view) => view.conversation))) {
|
|
1059
|
+
emit(alive, conversation);
|
|
1060
|
+
}
|
|
1061
|
+
},
|
|
1062
|
+
async openTurn(conversation, turn) {
|
|
1063
|
+
await alive.deps.durably((local) => local.keepConversationFact({ conversation, name: 'openedTurn', value: turn }));
|
|
1064
|
+
tellList(alive);
|
|
1065
|
+
},
|
|
1066
|
+
dismissSuggestion(conversation, suggestion) {
|
|
1067
|
+
return alive.deps.durably(async (local) => {
|
|
1068
|
+
if ((await dismissedIn(local, conversation)) === suggestion)
|
|
1069
|
+
return;
|
|
1070
|
+
await local.keepConversationFact({
|
|
1071
|
+
conversation,
|
|
1072
|
+
name: 'dismissedSuggestion',
|
|
1073
|
+
value: suggestion,
|
|
1074
|
+
});
|
|
1075
|
+
heldInViews(alive, conversation, { dismissed: suggestion });
|
|
1076
|
+
alive.deps.publish(conversationChangeSet([conversation]));
|
|
1077
|
+
});
|
|
1008
1078
|
},
|
|
1009
1079
|
};
|
|
1010
1080
|
}
|
|
1081
|
+
function rewritten(alive, conversation, text, revision = chatDraftRevision()) {
|
|
1082
|
+
return withDraft(alive, conversation, (current) => ({
|
|
1083
|
+
conversation,
|
|
1084
|
+
text: text(current?.text ?? ''),
|
|
1085
|
+
revision,
|
|
1086
|
+
...(current === undefined ? {} : pending(current)),
|
|
1087
|
+
}));
|
|
1088
|
+
}
|
|
1011
1089
|
export function chatDraftSurface(alive) {
|
|
1012
1090
|
return {
|
|
1013
|
-
write(conversation, draft, revision
|
|
1014
|
-
return
|
|
1015
|
-
conversation,
|
|
1016
|
-
text: draft.text,
|
|
1017
|
-
revision,
|
|
1018
|
-
...(current === undefined ? {} : pending(current)),
|
|
1019
|
-
}));
|
|
1091
|
+
write(conversation, draft, revision) {
|
|
1092
|
+
return rewritten(alive, conversation, () => draft.text, revision);
|
|
1020
1093
|
},
|
|
1021
1094
|
prepend(conversation, draft) {
|
|
1022
|
-
return
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
...(current === undefined ? {} : pending(current)),
|
|
1027
|
-
}));
|
|
1095
|
+
return rewritten(alive, conversation, (held) => joinedDraft(draft.text, held));
|
|
1096
|
+
},
|
|
1097
|
+
append(conversation, draft) {
|
|
1098
|
+
return rewritten(alive, conversation, (held) => joinedDraft(held, draft.text));
|
|
1028
1099
|
},
|
|
1029
1100
|
discard(conversation) {
|
|
1030
1101
|
return withDraft(alive, conversation, (current) => current === undefined
|
|
@@ -1034,31 +1105,37 @@ export function chatDraftSurface(alive) {
|
|
|
1034
1105
|
};
|
|
1035
1106
|
}
|
|
1036
1107
|
export async function conversationsOpened(alive, local) {
|
|
1037
|
-
const { conversationList, conversationCursor } = await local.facts();
|
|
1038
|
-
if (conversationList === null ||
|
|
1108
|
+
const { conversationList, agentList, conversationCursor } = await local.facts();
|
|
1109
|
+
if (conversationList === null || agentList === null)
|
|
1110
|
+
return;
|
|
1111
|
+
alive.held.set({ listHeld: true });
|
|
1112
|
+
if (alive.known())
|
|
1039
1113
|
return;
|
|
1040
1114
|
alive.deps.stream()?.hold(conversationCursor);
|
|
1041
1115
|
conversationsLearned(alive);
|
|
1042
1116
|
}
|
|
1117
|
+
export function reconcileOpening(alive) {
|
|
1118
|
+
alive.held.set({ openingRead: true });
|
|
1119
|
+
return reconcileConversations(alive);
|
|
1120
|
+
}
|
|
1043
1121
|
export async function reconcileConversations(alive) {
|
|
1044
1122
|
if (!alive.known())
|
|
1045
1123
|
return;
|
|
1046
1124
|
const local = await alive.deps.local();
|
|
1047
|
-
const [held,
|
|
1125
|
+
const [held, stored, { conversationList, agentList }] = await Promise.all([
|
|
1048
1126
|
local.conversations(),
|
|
1049
|
-
local.
|
|
1127
|
+
local.storedConversations(),
|
|
1050
1128
|
local.facts(),
|
|
1051
1129
|
]);
|
|
1052
1130
|
const list = conversationList ?? [];
|
|
1131
|
+
const listing = { conversations: list, agents: agentList ?? [] };
|
|
1053
1132
|
const entries = new Map(list.map((entry) => [entry.conversation, entry]));
|
|
1054
1133
|
const named = new Set(entries.keys());
|
|
1055
1134
|
const reads = new Map(held.map((one) => [one.conversation, one]));
|
|
1056
1135
|
const { replicated } = alive.held.now();
|
|
1057
|
-
const leaving = [
|
|
1058
|
-
...new Set([...replicated, ...reads.keys(), ...kept.map(({ conversation }) => conversation)]),
|
|
1059
|
-
].filter((conversation) => !named.has(conversation) &&
|
|
1136
|
+
const leaving = [...new Set([...replicated, ...stored])].filter((conversation) => !named.has(conversation) &&
|
|
1060
1137
|
!alive.isCreating(conversation) &&
|
|
1061
|
-
(!conversation.startsWith(NEW_CHAT) || retiredChat(conversation,
|
|
1138
|
+
(!conversation.startsWith(NEW_CHAT) || retiredChat(conversation, listing)));
|
|
1062
1139
|
for (const conversation of named) {
|
|
1063
1140
|
if (!replicated.has(conversation) &&
|
|
1064
1141
|
missed(alive, conversation, reads.get(conversation), entries.get(conversation))) {
|
|
@@ -1093,6 +1170,17 @@ export async function catchUpConversations(alive, progress) {
|
|
|
1093
1170
|
if (!alive.current())
|
|
1094
1171
|
progress.complete = false;
|
|
1095
1172
|
}
|
|
1173
|
+
export async function holdsSubmission(alive, operation) {
|
|
1174
|
+
if (alive.sending(operation))
|
|
1175
|
+
return true;
|
|
1176
|
+
const local = await alive.deps.local();
|
|
1177
|
+
for (const { conversation } of await local.chatDrafts()) {
|
|
1178
|
+
const held = (await local.chatDraft(conversation))?.submitted;
|
|
1179
|
+
if (held?.some((one) => one.operation === operation && one.turn === null) === true)
|
|
1180
|
+
return true;
|
|
1181
|
+
}
|
|
1182
|
+
return false;
|
|
1183
|
+
}
|
|
1096
1184
|
export async function resendConversations(alive, progress) {
|
|
1097
1185
|
const local = await alive.deps.local();
|
|
1098
1186
|
const tried = new Set();
|
|
@@ -1104,7 +1192,7 @@ export async function resendConversations(alive, progress) {
|
|
|
1104
1192
|
if (alive.deps.closed())
|
|
1105
1193
|
return;
|
|
1106
1194
|
tried.add(submission.operation);
|
|
1107
|
-
const result = await dispatched(alive, conversation, submission);
|
|
1195
|
+
const result = await dispatched(alive, conversation, submission, true);
|
|
1108
1196
|
if (result.kind === 'revoked-authorization')
|
|
1109
1197
|
return;
|
|
1110
1198
|
if (result.kind === 'transport-uncertainty') {
|
|
@@ -1124,23 +1212,25 @@ export function watchConversation(alive, conversation, options, deliver) {
|
|
|
1124
1212
|
const { view, watcher } = alive.viewJoined(key, conversation, asked, deliver);
|
|
1125
1213
|
const { held } = view;
|
|
1126
1214
|
const reading = new AbortController();
|
|
1127
|
-
void untilAnswered(() => alive.deps.durably(async (local) => {
|
|
1215
|
+
void untilAnswered(retrying(alive.deps.retries, 'reading', `window ${key}`, () => alive.deps.durably(async (local) => {
|
|
1128
1216
|
if (held.now().window !== null)
|
|
1129
1217
|
return;
|
|
1130
1218
|
const { messages, more } = await local.messageWindow(conversation, asked.limit, asked.before);
|
|
1131
1219
|
held.set({ draft: (await local.chatDraft(conversation)) ?? null });
|
|
1132
1220
|
held.set({ ended: await endedIn(alive, conversation) });
|
|
1221
|
+
held.set({ dismissed: await dismissedIn(local, conversation) });
|
|
1222
|
+
held.set({ pins: (await local.facts()).chatPins });
|
|
1133
1223
|
held.set({ window: messages });
|
|
1134
|
-
held.set({ marked: await local.jobMarkers(conversation) });
|
|
1135
1224
|
held.set({ more });
|
|
1136
1225
|
held.set({ total: asked.total ? await local.messageCount(conversation) : null });
|
|
1137
|
-
}), reading.signal)
|
|
1226
|
+
})), reading.signal)
|
|
1138
1227
|
.then(() => {
|
|
1139
1228
|
if (alive.deps.closed() || !view.admit(watcher))
|
|
1140
1229
|
return;
|
|
1141
|
-
const
|
|
1142
|
-
const shown = window ?? [];
|
|
1143
|
-
|
|
1230
|
+
const now = held.now();
|
|
1231
|
+
const shown = now.window ?? [];
|
|
1232
|
+
const frame = sharedFrame(view, shown);
|
|
1233
|
+
watcher.notify(viewOf(alive, conversation, shown, frame, now));
|
|
1144
1234
|
})
|
|
1145
1235
|
.catch(() => { });
|
|
1146
1236
|
return () => {
|
|
@@ -1156,9 +1246,13 @@ export function watchConversationList(alive, deliver) {
|
|
|
1156
1246
|
export function watchConversationAgents(alive, deliver) {
|
|
1157
1247
|
alive.agentListeners.join(deliver);
|
|
1158
1248
|
conversationsLearned(alive);
|
|
1159
|
-
readAgents(alive);
|
|
1160
1249
|
return () => alive.agentListeners.leave(deliver);
|
|
1161
1250
|
}
|
|
1251
|
+
export function watchListedAgents(alive, deliver) {
|
|
1252
|
+
alive.listedListeners.join(deliver);
|
|
1253
|
+
conversationsLearned(alive);
|
|
1254
|
+
return () => alive.listedListeners.leave(deliver);
|
|
1255
|
+
}
|
|
1162
1256
|
export function conversationsChanged(alive, set) {
|
|
1163
1257
|
for (const conversation of set.conversations)
|
|
1164
1258
|
emit(alive, conversation);
|
|
@@ -1170,26 +1264,32 @@ export function receiveConversationEvent(alive, event) {
|
|
|
1170
1264
|
if (event.whole)
|
|
1171
1265
|
alive.relistBegun();
|
|
1172
1266
|
if (alive.relisting())
|
|
1173
|
-
alive.relistHeard(event.entries);
|
|
1267
|
+
alive.relistHeard(event.entries, event.agents);
|
|
1174
1268
|
if (event.more && alive.relisting())
|
|
1175
1269
|
return;
|
|
1176
1270
|
const whole = alive.relistTaken();
|
|
1177
1271
|
listChanged(alive, {
|
|
1178
1272
|
replacing: whole !== null,
|
|
1179
|
-
set: whole === null ? event.entries : [...whole.values()],
|
|
1273
|
+
set: whole === null ? event.entries : [...whole.conversations.values()],
|
|
1274
|
+
agents: whole === null ? event.agents : [...whole.agents.values()],
|
|
1180
1275
|
gone: null,
|
|
1181
1276
|
...(event.more ? { paged: true } : { cursor: event.cursor }),
|
|
1182
1277
|
});
|
|
1183
1278
|
return;
|
|
1184
1279
|
}
|
|
1185
1280
|
if (event.kind === 'listed') {
|
|
1186
|
-
alive.relistHeard([event.entry]);
|
|
1187
|
-
listChanged(alive, { replacing: false, set: [event.entry], gone: null });
|
|
1281
|
+
alive.relistHeard([event.entry], []);
|
|
1282
|
+
listChanged(alive, { replacing: false, set: [event.entry], agents: [], gone: null });
|
|
1283
|
+
return;
|
|
1284
|
+
}
|
|
1285
|
+
if (event.kind === 'agent') {
|
|
1286
|
+
alive.relistHeard([], [event.agent]);
|
|
1287
|
+
listChanged(alive, { replacing: false, set: [], agents: [event.agent], gone: null });
|
|
1188
1288
|
return;
|
|
1189
1289
|
}
|
|
1190
1290
|
if (event.kind === 'unlisted') {
|
|
1191
1291
|
alive.relistDropped(event.conversation);
|
|
1192
|
-
listChanged(alive, { replacing: false, set: [], gone: event.conversation });
|
|
1292
|
+
listChanged(alive, { replacing: false, set: [], agents: [], gone: event.conversation });
|
|
1193
1293
|
return;
|
|
1194
1294
|
}
|
|
1195
1295
|
heard(alive, event);
|