@agentshouse/mdruntime 0.1.0-cd3486bd400ed → 0.1.0-cd37f80239af0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (201) hide show
  1. package/README.md +21 -9
  2. package/dist/adapter.d.ts +149 -46
  3. package/dist/adapter.d.ts.map +1 -1
  4. package/dist/attached-model.d.ts +5 -1
  5. package/dist/attached-model.d.ts.map +1 -1
  6. package/dist/attached-model.js +38 -4
  7. package/dist/attempt.d.ts.map +1 -1
  8. package/dist/attempt.js +14 -1
  9. package/dist/binary.d.ts +18 -0
  10. package/dist/binary.d.ts.map +1 -0
  11. package/dist/binary.js +8 -0
  12. package/dist/bootstrap.d.ts +27 -11
  13. package/dist/bootstrap.d.ts.map +1 -1
  14. package/dist/bootstrap.js +184 -84
  15. package/dist/chain.d.ts +11 -0
  16. package/dist/chain.d.ts.map +1 -0
  17. package/dist/chain.js +64 -0
  18. package/dist/change-set.d.ts +9 -14
  19. package/dist/change-set.d.ts.map +1 -1
  20. package/dist/change-set.js +11 -62
  21. package/dist/change.d.ts +41 -0
  22. package/dist/change.d.ts.map +1 -1
  23. package/dist/change.js +34 -0
  24. package/dist/client.d.ts +31 -13
  25. package/dist/client.d.ts.map +1 -1
  26. package/dist/client.js +103 -7
  27. package/dist/collection.d.ts +10 -7
  28. package/dist/collection.d.ts.map +1 -1
  29. package/dist/collection.js +38 -79
  30. package/dist/compile.d.ts +16 -0
  31. package/dist/compile.d.ts.map +1 -0
  32. package/dist/compile.js +786 -0
  33. package/dist/connection.d.ts +50 -16
  34. package/dist/connection.d.ts.map +1 -1
  35. package/dist/connection.js +3786 -931
  36. package/dist/conversation.d.ts +37 -16
  37. package/dist/conversation.d.ts.map +1 -1
  38. package/dist/conversation.js +700 -257
  39. package/dist/digest.d.ts +7 -0
  40. package/dist/digest.d.ts.map +1 -0
  41. package/dist/digest.js +113 -0
  42. package/dist/durability.d.ts +4 -1
  43. package/dist/durability.d.ts.map +1 -1
  44. package/dist/durability.js +5 -0
  45. package/dist/entry.d.ts +20 -0
  46. package/dist/entry.d.ts.map +1 -0
  47. package/dist/entry.js +151 -0
  48. package/dist/fence.d.ts +6 -0
  49. package/dist/fence.d.ts.map +1 -0
  50. package/dist/fence.js +10 -0
  51. package/dist/file.d.ts +2 -19
  52. package/dist/file.d.ts.map +1 -1
  53. package/dist/file.js +7 -122
  54. package/dist/fulltext.d.ts +27 -0
  55. package/dist/fulltext.d.ts.map +1 -0
  56. package/dist/fulltext.js +370 -0
  57. package/dist/host.d.ts +13 -3
  58. package/dist/host.d.ts.map +1 -1
  59. package/dist/host.js +130 -17
  60. package/dist/identity.d.ts +2 -0
  61. package/dist/identity.d.ts.map +1 -1
  62. package/dist/identity.js +6 -0
  63. package/dist/index.d.ts +17 -9
  64. package/dist/index.d.ts.map +1 -1
  65. package/dist/index.js +7 -0
  66. package/dist/live.d.ts +22 -0
  67. package/dist/live.d.ts.map +1 -0
  68. package/dist/live.js +123 -0
  69. package/dist/media.d.ts +8 -0
  70. package/dist/media.d.ts.map +1 -0
  71. package/dist/media.js +218 -0
  72. package/dist/memory.d.ts +17 -7
  73. package/dist/memory.d.ts.map +1 -1
  74. package/dist/memory.js +231 -91
  75. package/dist/messages.d.ts +28 -0
  76. package/dist/messages.d.ts.map +1 -0
  77. package/dist/messages.js +90 -0
  78. package/dist/model.d.ts +9 -2
  79. package/dist/model.d.ts.map +1 -1
  80. package/dist/model.js +8 -2
  81. package/dist/name.d.ts +4 -0
  82. package/dist/name.d.ts.map +1 -0
  83. package/dist/name.js +122 -0
  84. package/dist/observation.d.ts +20 -2
  85. package/dist/observation.d.ts.map +1 -1
  86. package/dist/observation.js +113 -12
  87. package/dist/opened.d.ts +22 -0
  88. package/dist/opened.d.ts.map +1 -0
  89. package/dist/opened.js +107 -0
  90. package/dist/outcome.d.ts +52 -4
  91. package/dist/outcome.d.ts.map +1 -1
  92. package/dist/outcome.js +46 -0
  93. package/dist/overlay.d.ts +15 -5
  94. package/dist/overlay.d.ts.map +1 -1
  95. package/dist/overlay.js +129 -177
  96. package/dist/paths.d.ts +1 -0
  97. package/dist/paths.d.ts.map +1 -1
  98. package/dist/paths.js +4 -0
  99. package/dist/query.d.ts +68 -4
  100. package/dist/query.d.ts.map +1 -1
  101. package/dist/query.js +48 -7
  102. package/dist/reach.d.ts +18 -4
  103. package/dist/reach.d.ts.map +1 -1
  104. package/dist/reach.js +224 -55
  105. package/dist/react.d.ts +9 -6
  106. package/dist/react.d.ts.map +1 -1
  107. package/dist/react.js +7 -17
  108. package/dist/readiness.d.ts +52 -0
  109. package/dist/readiness.d.ts.map +1 -0
  110. package/dist/readiness.js +27 -0
  111. package/dist/recovery.d.ts +26 -0
  112. package/dist/recovery.d.ts.map +1 -0
  113. package/dist/recovery.js +224 -0
  114. package/dist/refresh.d.ts +17 -14
  115. package/dist/refresh.d.ts.map +1 -1
  116. package/dist/refresh.js +136 -56
  117. package/dist/replica.d.ts +58 -33
  118. package/dist/replica.d.ts.map +1 -1
  119. package/dist/replica.js +307 -159
  120. package/dist/room-draft.d.ts +51 -4
  121. package/dist/room-draft.d.ts.map +1 -1
  122. package/dist/room-draft.js +210 -18
  123. package/dist/rows.d.ts +37 -35
  124. package/dist/rows.d.ts.map +1 -1
  125. package/dist/rows.js +173 -102
  126. package/dist/save.d.ts +70 -3
  127. package/dist/save.d.ts.map +1 -1
  128. package/dist/save.js +805 -147
  129. package/dist/search.d.ts +39 -28
  130. package/dist/search.d.ts.map +1 -1
  131. package/dist/search.js +37 -105
  132. package/dist/seed.d.ts +16 -0
  133. package/dist/seed.d.ts.map +1 -0
  134. package/dist/seed.js +75 -0
  135. package/dist/shared.d.ts +15 -0
  136. package/dist/shared.d.ts.map +1 -0
  137. package/dist/shared.js +421 -0
  138. package/dist/status.d.ts +5 -0
  139. package/dist/status.d.ts.map +1 -1
  140. package/dist/status.js +3 -1
  141. package/dist/store.d.ts +107 -80
  142. package/dist/store.d.ts.map +1 -1
  143. package/dist/store.js +325 -461
  144. package/dist/stored.d.ts +130 -0
  145. package/dist/stored.d.ts.map +1 -0
  146. package/dist/stored.js +560 -0
  147. package/dist/tables.d.ts +325 -0
  148. package/dist/tables.d.ts.map +1 -0
  149. package/dist/tables.js +578 -0
  150. package/dist/transfer.d.ts +0 -5
  151. package/dist/transfer.d.ts.map +1 -1
  152. package/dist/turn.d.ts +9 -0
  153. package/dist/turn.d.ts.map +1 -0
  154. package/dist/turn.js +75 -0
  155. package/dist/validation.d.ts +29 -0
  156. package/dist/validation.d.ts.map +1 -0
  157. package/dist/validation.js +101 -0
  158. package/dist/windows.d.ts +22 -0
  159. package/dist/windows.d.ts.map +1 -0
  160. package/dist/windows.js +89 -0
  161. package/dist/wire.d.ts +165 -30
  162. package/dist/wire.d.ts.map +1 -1
  163. package/dist/wire.js +56 -29
  164. package/dist/words.d.ts +19 -0
  165. package/dist/words.d.ts.map +1 -0
  166. package/dist/words.js +156 -0
  167. package/package.json +26 -15
  168. package/AGENTS.md +0 -290
  169. package/dist/attachment.d.ts +0 -3
  170. package/dist/attachment.d.ts.map +0 -1
  171. package/dist/attachment.js +0 -8
  172. package/dist/chat/acts.d.ts +0 -34
  173. package/dist/chat/acts.d.ts.map +0 -1
  174. package/dist/chat/acts.js +0 -36
  175. package/dist/chat/messages.d.ts +0 -21
  176. package/dist/chat/messages.d.ts.map +0 -1
  177. package/dist/chat/messages.js +0 -151
  178. package/dist/chat/overlay.d.ts +0 -31
  179. package/dist/chat/overlay.d.ts.map +0 -1
  180. package/dist/chat/overlay.js +0 -97
  181. package/dist/chat/parts.d.ts +0 -57
  182. package/dist/chat/parts.d.ts.map +0 -1
  183. package/dist/chat/parts.js +0 -119
  184. package/dist/chat/process.d.ts +0 -20
  185. package/dist/chat/process.d.ts.map +0 -1
  186. package/dist/chat/process.js +0 -32
  187. package/dist/chat/provider.d.ts +0 -57
  188. package/dist/chat/provider.d.ts.map +0 -1
  189. package/dist/chat/provider.js +0 -637
  190. package/dist/chat/questions.d.ts +0 -31
  191. package/dist/chat/questions.d.ts.map +0 -1
  192. package/dist/chat/questions.js +0 -111
  193. package/dist/chat/read.d.ts +0 -5
  194. package/dist/chat/read.d.ts.map +0 -1
  195. package/dist/chat/read.js +0 -13
  196. package/dist/chat.d.ts +0 -7
  197. package/dist/chat.d.ts.map +0 -1
  198. package/dist/chat.js +0 -2
  199. package/dist/read-side.d.ts +0 -18
  200. package/dist/read-side.d.ts.map +0 -1
  201. package/dist/read-side.js +0 -433
@@ -1,637 +0,0 @@
1
- import { createContext, createElement, useCallback, useContext, useEffect, useMemo, useReducer, useRef, useState, useSyncExternalStore, } from 'react';
2
- import { AssistantRuntimeProvider, createMessageQueue, generateId, useExternalStoreRuntime, } from '@assistant-ui/react';
3
- import { attempts } from '../attempt.js';
4
- import { expectedList } from '../expectation.js';
5
- import { chatDraftRevision } from '../identity.js';
6
- import { useObserve } from '../react.js';
7
- import { accepted, answeredOf, outcomeOf, sender, unanswered, } from './acts.js';
8
- import { askedBefore, chatMessages, contextOf } from './messages.js';
9
- import { conversationChanged, conversationGone, conversationListed, optionChosen, overlay, questionAnswered, sentMessage, } from './overlay.js';
10
- import { commandIn, commandsOf, optionsOf } from './process.js';
11
- import { loneOf, questionsOf } from './questions.js';
12
- import { record, text } from './read.js';
13
- const SHARED = new WeakMap();
14
- function sharedOf(client) {
15
- const held = SHARED.get(client);
16
- if (held !== undefined)
17
- return held;
18
- const fresh = { overlay: overlay(), created: new Map(), sends: 0 };
19
- SHARED.set(client, fresh);
20
- return fresh;
21
- }
22
- function newChat(agent) {
23
- return `new:${agent}`;
24
- }
25
- function ready(observed) {
26
- return observed.state === 'ready' ? observed.value : null;
27
- }
28
- async function alike(one, other) {
29
- if (one.type !== other.type || one.size !== other.size)
30
- return false;
31
- const [mine, theirs] = await Promise.all([one.arrayBuffer(), other.arrayBuffer()]);
32
- const bytes = new Uint8Array(theirs);
33
- return new Uint8Array(mine).every((byte, at) => byte === bytes[at]);
34
- }
35
- async function same(one, other) {
36
- if (one.text !== other.text || one.files.length !== other.files.length)
37
- return false;
38
- const files = await Promise.all(one.files.map(async (file, at) => {
39
- const twin = other.files[at];
40
- return twin !== undefined && file.name === twin.name && (await alike(file.blob, twin.blob));
41
- }));
42
- return files.every(Boolean);
43
- }
44
- function turnOf(result) {
45
- return accepted(result) ? text(record(result.result), 'turn') : null;
46
- }
47
- async function deliver(client, send, conversation, said, key, refused, noted) {
48
- const typed = (await client.conversation.read(conversation)).chatDraft;
49
- const kept = chatDraftRevision();
50
- noted(conversation, kept);
51
- await client.chatDraft.write(conversation, said, kept);
52
- const result = await send.carried('conversation.send', { conversation, key }, () => {
53
- noted(conversation, null);
54
- noted(conversation, kept);
55
- return client.conversation.send(conversation);
56
- });
57
- if (typed !== null && !(await same(typed, said))) {
58
- const now = (await client.conversation.read(conversation)).chatDraft;
59
- if (now === null || now.revision === kept) {
60
- const back = chatDraftRevision();
61
- noted(conversation, back);
62
- await client.chatDraft.write(conversation, typed, back);
63
- }
64
- }
65
- if (accepted(result))
66
- send.expect(sentMessage(key, [conversation], said, turnOf(result)));
67
- else if (!unanswered(result))
68
- refused();
69
- return result;
70
- }
71
- const IDLE = { busy: false, outcome: null };
72
- const SENDING = { state: 'sending' };
73
- function useActs(overlay, online) {
74
- const [running, setRunning] = useState(IDLE);
75
- const [tried] = useState(() => attempts());
76
- const latest = useRef(0);
77
- const mounted = useRef(true);
78
- useEffect(() => {
79
- mounted.current = true;
80
- return () => {
81
- mounted.current = false;
82
- };
83
- }, []);
84
- const clear = useCallback(() => {
85
- latest.current += 1;
86
- setRunning(IDLE);
87
- }, []);
88
- const call = useCallback((steps, expected = null, then) => {
89
- latest.current += 1;
90
- const mine = latest.current;
91
- const act = overlay.begin();
92
- const drawn = typeof expected === 'function' ? null : expected;
93
- overlay.expect(act, drawn);
94
- setRunning({ busy: true, outcome: expectedList(drawn).length > 0 ? null : SENDING });
95
- const attempt = tried.begin(act);
96
- const send = sender({
97
- online,
98
- identity: attempt.identity,
99
- expect: (expecting) => overlay.expect(overlay.begin(), expecting),
100
- draw: (expecting) => overlay.expect(act, expecting),
101
- });
102
- return steps(send)
103
- .catch(() => ({
104
- kind: 'transport-uncertainty',
105
- code: 'transport-uncertainty',
106
- fields: {},
107
- retryable: true,
108
- }))
109
- .then((result) => {
110
- if (accepted(result) && typeof expected === 'function') {
111
- overlay.expect(act, expected(result.result));
112
- }
113
- attempt.settle(answeredOf(result), (one) => overlay.drop(one));
114
- if (!mounted.current || latest.current !== mine)
115
- return accepted(result);
116
- if (accepted(result))
117
- then?.(result.result);
118
- if (latest.current === mine)
119
- setRunning({ busy: false, outcome: outcomeOf(result) });
120
- return accepted(result);
121
- });
122
- }, [overlay, online, tried]);
123
- return { ...running, call, clear };
124
- }
125
- const ATTACHMENTS = {
126
- accept: '*',
127
- add: ({ file }) => Promise.resolve({
128
- id: generateId(),
129
- type: 'file',
130
- name: file.name,
131
- contentType: file.type,
132
- file,
133
- status: { type: 'requires-action', reason: 'composer-send' },
134
- }),
135
- send: (attachment) => Promise.resolve({ ...attachment, status: { type: 'complete' }, content: [] }),
136
- remove: () => Promise.resolve(),
137
- };
138
- function filesOf(attachments) {
139
- return attachments.flatMap((one) => one.file === undefined ? [] : [{ name: one.name, blob: one.file }]);
140
- }
141
- function fileOf(file) {
142
- return file.blob instanceof File && file.blob.name === file.name
143
- ? file.blob
144
- : new File([file.blob], file.name, { type: file.blob.type });
145
- }
146
- function textOf(parts) {
147
- return parts.map((part) => (part.type === 'text' ? (part.text ?? '') : '')).join('');
148
- }
149
- function draftOf(message) {
150
- return { text: textOf(message.content), files: filesOf(message.attachments ?? []) };
151
- }
152
- function appended(draft) {
153
- return {
154
- role: 'user',
155
- content: [{ type: 'text', text: draft.text }],
156
- attachments: draft.files.map((file) => ({
157
- id: generateId(),
158
- type: 'file',
159
- name: file.name,
160
- contentType: file.blob.type,
161
- file: fileOf(file),
162
- status: { type: 'complete' },
163
- content: [],
164
- })),
165
- metadata: { custom: {} },
166
- createdAt: new Date(),
167
- parentId: null,
168
- sourceId: null,
169
- runConfig: undefined,
170
- };
171
- }
172
- function composed(state) {
173
- return JSON.stringify([state.text, state.attachments.map((one) => one.id)]);
174
- }
175
- const ChatContext = createContext(null);
176
- export function useChat() {
177
- const chat = useContext(ChatContext);
178
- if (chat === null)
179
- throw new Error('useChat is called outside a ChatProvider');
180
- return chat;
181
- }
182
- export function ChatProvider({ client, conversation = null, agent = null, onOpen, children, }) {
183
- const shared = sharedOf(client);
184
- const asked = JSON.stringify([conversation, agent]);
185
- const [followed, setFollowed] = useState(asked);
186
- const [open, setOpen] = useState({ conversation, agent });
187
- if (followed !== asked) {
188
- setFollowed(asked);
189
- setOpen({ conversation, agent });
190
- }
191
- const opened = useRef(open);
192
- opened.current = open;
193
- const told = useRef(onOpen);
194
- told.current = onOpen;
195
- const go = useCallback((next) => {
196
- opened.current = next;
197
- setOpen(next);
198
- told.current?.(next);
199
- }, []);
200
- const listedHeld = ready(useObserve(client, 'conversations'));
201
- const agentsHeld = ready(useObserve(client, 'agents'));
202
- const sync = ready(useObserve(client, 'sync-status'));
203
- const refusal = ready(useObserve(client, 'connection-refusal'));
204
- const online = sync !== null && !sync.providerUnreachable && refusal === null;
205
- const thread = open.conversation ?? (open.agent === null ? null : newChat(open.agent));
206
- const held = ready(useObserve(thread === null ? null : client, 'conversation', thread ?? '', {}));
207
- const version = useSyncExternalStore(shared.overlay.subscribe, shared.overlay.version);
208
- useEffect(() => {
209
- if (listedHeld !== null)
210
- shared.overlay.heardList(listedHeld);
211
- }, [shared, listedHeld, version]);
212
- useEffect(() => {
213
- if (held !== null)
214
- shared.overlay.heard(held);
215
- }, [shared, held, version]);
216
- const conversations = useMemo(() => shared.overlay.conversations(listedHeld ?? []), [shared, listedHeld, version]);
217
- const view = useMemo(() => held === null || held.conversation !== thread ? null : shared.overlay.conversation(held), [shared, held, thread, version]);
218
- const agents = agentsHeld ?? [];
219
- const known = useRef(agents);
220
- known.current = agents;
221
- const entry = conversations.find((one) => one.conversation === open.conversation);
222
- const running = entry?.state === 'working' || view?.turn != null;
223
- const questions = useMemo(() => questionsOf(view), [view]);
224
- const lone = loneOf(questions);
225
- const commands = useMemo(() => commandsOf(view), [view]);
226
- const messages = useMemo(() => chatMessages(view), [view]);
227
- const sending = useActs(shared.overlay, online);
228
- const behind = useActs(shared.overlay, online);
229
- const acting = useActs(shared.overlay, online);
230
- const [again, setAgain] = useState(null);
231
- const current = useRef(thread);
232
- current.current = thread;
233
- const seeding = useRef(false);
234
- const heard = useRef('');
235
- const shown = useRef('');
236
- const own = useRef([]);
237
- const noted = useCallback((to, revision) => {
238
- own.current = [...own.current, `${to}\u0000${revision ?? ''}`];
239
- }, []);
240
- const keep = useCallback((to, draft) => {
241
- const revision = draft.text === '' && draft.files.length === 0 ? null : chatDraftRevision();
242
- noted(to, revision);
243
- void (revision === null
244
- ? client.chatDraft.discard(to)
245
- : client.chatDraft.write(to, draft, revision));
246
- }, [client, noted]);
247
- useEffect(() => {
248
- heard.current = '';
249
- shown.current = '';
250
- own.current = own.current.filter((one) => one.startsWith(`${thread}\u0000`));
251
- sending.clear();
252
- acting.clear();
253
- setAgain(null);
254
- }, [thread, sending.clear, acting.clear]);
255
- const sendTo = (to, said, key, refused, acts = sending) => acts.call((send) => deliver(client, send, to, said, key, refused, noted), sentMessage(key, [to], said, null));
256
- const first = (to, said, key, refused) => {
257
- const drafted = [newChat(to)];
258
- let made = null;
259
- return sending.call(async (send) => {
260
- let conversation = shared.created.get(to);
261
- if (conversation === undefined) {
262
- const answer = await send.carried('conversation.create', { agent: to }, (operation) => client.conversation.create({ agent_id: to, operation_id: operation }));
263
- if (!accepted(answer)) {
264
- if (!unanswered(answer))
265
- refused();
266
- return answer;
267
- }
268
- const named = text(record(answer.result), 'conversation');
269
- if (named === null)
270
- return answer;
271
- conversation = named;
272
- shared.created.set(to, conversation);
273
- const name = known.current.find((one) => one.identity === to)?.name ?? null;
274
- send.expect(conversationListed({
275
- conversation,
276
- agent: { identity: to, name },
277
- name: name ?? '',
278
- created: new Date().toISOString(),
279
- finished: null,
280
- outcome: null,
281
- state: 'idle',
282
- started: null,
283
- }));
284
- }
285
- made = conversation;
286
- send.draw(sentMessage(key, [...drafted, conversation], said, null));
287
- const result = await deliver(client, send, conversation, said, key, refused, noted);
288
- if (accepted(result))
289
- shared.created.delete(to);
290
- return result;
291
- }, sentMessage(key, drafted, said, null), () => {
292
- if (made === null)
293
- return;
294
- void client.chatDraft.discard(newChat(to));
295
- const now = opened.current;
296
- if (now.conversation === null && now.agent === to)
297
- go({ conversation: made, agent: to });
298
- });
299
- };
300
- const returned = async (to, one) => {
301
- const now = (await client.conversation.read(to)).chatDraft;
302
- await client.chatDraft.write(to, {
303
- text: now === null || now.text === '' ? one.text : `${one.text}\n${now.text}`,
304
- files: [...one.files, ...(now?.files ?? [])],
305
- });
306
- };
307
- const sent = () => {
308
- shared.sends += 1;
309
- return `sent ${shared.sends}`;
310
- };
311
- const deliverDraft = (said, refused) => {
312
- const key = sent();
313
- const { conversation: to, agent: by } = opened.current;
314
- const attempt = to !== null
315
- ? () => void sendTo(to, said, key, refused)
316
- : by !== null
317
- ? () => void first(by, said, key, refused)
318
- : null;
319
- if (attempt === null)
320
- return;
321
- setAgain(() => attempt);
322
- attempt();
323
- };
324
- const control = (name, input, expected) => acting.call((send) => send.carried(name, input, (operation) => client.call(name, { ...input, operation_id: operation })), expected);
325
- const carried = (name, act) => {
326
- const to = opened.current.conversation;
327
- if (to === null)
328
- return Promise.resolve(false);
329
- return acting.call((send) => send.carried(name, { conversation: to }, () => act(to)));
330
- };
331
- const stop = () => carried('conversation.interrupt', (to) => client.conversation.interrupt(to));
332
- const kill = () => carried('conversation.kill', (to) => client.conversation.kill(to));
333
- const answer = (question, response) => {
334
- const to = opened.current.conversation;
335
- if (to === null || question.secret)
336
- return Promise.resolve(false);
337
- return acting.call((send) => send.carried('conversation.answer', { conversation: to, question: question.interaction }, () => client.conversation.answer(to, { interaction_id: question.interaction, response })), () => questionAnswered(to, question.interaction));
338
- };
339
- const choose = (option, value) => {
340
- const to = opened.current.conversation;
341
- if (to === null)
342
- return Promise.resolve(false);
343
- return control('conversation.setOption', { conversation_id: to, option, value }, optionChosen(to, option, value));
344
- };
345
- const done = (to = opened.current.conversation) => to === null
346
- ? Promise.resolve(false)
347
- : control('conversation.markDone', { conversation_id: to }, conversationChanged(to, { finished: new Date().toISOString() }, (one) => one.finished !== null));
348
- const rename = (name, to = opened.current.conversation) => to === null
349
- ? Promise.resolve(false)
350
- : control('conversation.rename', { conversation_id: to, name }, conversationChanged(to, { name }, (one) => one.name === name));
351
- const remove = (to = opened.current.conversation) => to === null
352
- ? Promise.resolve(false)
353
- : acting.call((send) => send.carried('conversation.delete', { conversation: to }, () => client.conversation.delete(to)), conversationGone(to), () => {
354
- const now = opened.current;
355
- if (now.conversation === to)
356
- go({ conversation: null, agent: now.agent });
357
- });
358
- const openConversation = (id) => go({
359
- conversation: id,
360
- agent: conversations.find((one) => one.conversation === id)?.agent.identity ?? null,
361
- });
362
- const start = (by) => go({ conversation: null, agent: by });
363
- const dispatch = useRef(() => undefined);
364
- const queues = useRef(new Map());
365
- const into = thread ?? '';
366
- const laneOf = (key) => {
367
- const held = queues.current.get(key);
368
- if (held !== undefined)
369
- return held;
370
- const fresh = {
371
- queue: createMessageQueue({ run: (message) => dispatch.current(message, key) }),
372
- running: false,
373
- };
374
- queues.current.set(key, fresh);
375
- return fresh;
376
- };
377
- const lane = laneOf(into);
378
- const queue = lane.queue;
379
- const [, moved] = useReducer((count) => count + 1, 0);
380
- useEffect(() => queue.subscribe(moved), [queue]);
381
- useEffect(() => {
382
- for (const [key, one] of queues.current) {
383
- const now = key === into
384
- ? running
385
- : conversations.some((entry) => entry.conversation === key && entry.state !== 'idle');
386
- const empty = one.queue.adapter.items.length + one.queue.adapter.steerItems.length === 0;
387
- if (key !== into && !now && empty) {
388
- queues.current.delete(key);
389
- continue;
390
- }
391
- if (now === one.running)
392
- continue;
393
- one.running = now;
394
- if (now)
395
- one.queue.notifyBusy();
396
- else
397
- one.queue.notifyIdle();
398
- }
399
- }, [into, running, conversations]);
400
- const waiting = useRef(new Map());
401
- const lanes = () => [...queue.adapter.steerItems, ...queue.adapter.items];
402
- const threads = useMemo(() => {
403
- const item = (one) => ({
404
- id: one.conversation,
405
- title: one.name,
406
- custom: { agent: one.agent.identity, state: one.state, finished: one.finished },
407
- });
408
- return {
409
- regular: conversations
410
- .filter((one) => one.finished === null)
411
- .map((one) => ({ ...item(one), status: 'regular' })),
412
- archived: conversations
413
- .filter((one) => one.finished !== null)
414
- .map((one) => ({ ...item(one), status: 'archived' })),
415
- };
416
- }, [conversations]);
417
- const aui = useExternalStoreRuntime({
418
- messages,
419
- isRunning: running,
420
- convertMessage: (message) => message,
421
- queue: lined(),
422
- onNew: async (message) => lined().enqueue(message),
423
- onCancel: async () => {
424
- void stop();
425
- },
426
- onReload: thread === null
427
- ? undefined
428
- : async (parent) => {
429
- const said = askedBefore(view, parent);
430
- if (said === null)
431
- return;
432
- const draft = { text: said, files: [] };
433
- const into = thread;
434
- deliverDraft(draft, () => back(draft, into));
435
- },
436
- adapters: {
437
- attachments: ATTACHMENTS,
438
- threadList: {
439
- ...(thread === null ? {} : { threadId: thread }),
440
- threads: threads.regular,
441
- archivedThreads: threads.archived,
442
- onSwitchToThread: (id) => openConversation(id),
443
- onSwitchToNewThread: () => {
444
- const by = open.agent ?? known.current[0]?.identity;
445
- if (by !== undefined)
446
- start(by);
447
- },
448
- onRename: (id, name) => {
449
- void rename(name, id);
450
- },
451
- onArchive: (id) => {
452
- void done(id);
453
- },
454
- onDelete: (id) => {
455
- void remove(id);
456
- },
457
- },
458
- },
459
- });
460
- function back(one, into) {
461
- if (current.current !== into) {
462
- void returned(into, one);
463
- return;
464
- }
465
- const composer = aui.thread.composer;
466
- const now = composer.getState().text;
467
- composer.setText(now === '' ? one.text : `${one.text}\n${now}`);
468
- for (const file of one.files)
469
- void composer.addAttachment(fileOf(file));
470
- }
471
- function taken(message) {
472
- const said = textOf(message.content);
473
- if (lone === null || commandIn(said, commands))
474
- return false;
475
- void answer(lone.question, lone.question.accept({ [lone.field.name]: said }));
476
- return true;
477
- }
478
- function lined() {
479
- const holding = (put) => (message) => {
480
- if (taken(message))
481
- return;
482
- const known = new Set(lanes().map((one) => one.id));
483
- if (!lane.running)
484
- queue.notifyIdle();
485
- put(message);
486
- const added = lanes().find((one) => !known.has(one.id));
487
- if (added !== undefined)
488
- waiting.current.set(added.id, message);
489
- };
490
- return {
491
- get items() {
492
- return queue.adapter.items;
493
- },
494
- get steerItems() {
495
- return queue.adapter.steerItems;
496
- },
497
- enqueue: holding(queue.adapter.enqueue),
498
- steer: holding(queue.adapter.steer),
499
- move: queue.adapter.move,
500
- edit: queue.adapter.edit,
501
- remove: queue.adapter.remove,
502
- __internal_setDispatchTransform: queue.adapter.__internal_setDispatchTransform,
503
- };
504
- }
505
- dispatch.current = (message, key) => {
506
- const draft = draftOf(message);
507
- const idle = () => queues.current.get(key)?.queue.notifyIdle();
508
- if (key !== (current.current ?? '')) {
509
- void sendTo(key, draft, sent(), () => {
510
- back(draft, key);
511
- idle();
512
- }, behind);
513
- return;
514
- }
515
- const into = current.current;
516
- deliverDraft(draft, () => {
517
- if (into !== null)
518
- back(draft, into);
519
- idle();
520
- });
521
- };
522
- const unqueue = (id) => {
523
- const message = waiting.current.get(id);
524
- waiting.current.delete(id);
525
- queue.adapter.remove(id);
526
- return message;
527
- };
528
- const edit = (id) => {
529
- const item = lanes().find((one) => one.id === id);
530
- const message = unqueue(id);
531
- if (thread === null || item === undefined)
532
- return;
533
- back(message === undefined ? { text: textOf(item.parts), files: [] } : draftOf(message), thread);
534
- };
535
- const reconcile = useRef(() => undefined);
536
- reconcile.current = () => {
537
- if (thread === null || view === null || seeding.current)
538
- return;
539
- const at = `${thread}\u0000`;
540
- const now = `${at}${view.chatDraft?.revision ?? ''}`;
541
- if (now === heard.current)
542
- return;
543
- const echo = own.current.indexOf(now);
544
- const known = heard.current.startsWith(at);
545
- heard.current = now;
546
- if (known && echo >= 0) {
547
- own.current = own.current.slice(echo);
548
- return;
549
- }
550
- own.current = own.current.filter((one) => !one.startsWith(at));
551
- const composer = aui.thread.composer;
552
- const state = composer.getState();
553
- const stored = view.chatDraft;
554
- const typed = { text: state.text, files: filesOf(state.attachments) };
555
- const kept = !known && (typed.text !== '' || typed.files.length > 0);
556
- if (kept && stored === null) {
557
- shown.current = `${at}${composed(state)}`;
558
- keep(thread, typed);
559
- return;
560
- }
561
- const draft = kept
562
- ? {
563
- text: [stored?.text ?? '', typed.text].filter((one) => one !== '').join('\n'),
564
- files: [...(stored?.files ?? []), ...typed.files],
565
- }
566
- : stored;
567
- if (state.text === (draft?.text ?? '') &&
568
- state.attachments.length === 0 &&
569
- (draft?.files.length ?? 0) === 0) {
570
- shown.current = `${at}${composed(state)}`;
571
- return;
572
- }
573
- seeding.current = true;
574
- composer.setText(draft?.text ?? '');
575
- void composer
576
- .clearAttachments()
577
- .then(() => Promise.all((draft?.files ?? []).map((file) => composer.addAttachment(fileOf(file)))))
578
- .finally(() => {
579
- seeding.current = false;
580
- if (current.current !== thread)
581
- return;
582
- const seeded = composer.getState();
583
- shown.current = `${at}${composed(seeded)}`;
584
- if (kept)
585
- keep(thread, { text: seeded.text, files: filesOf(seeded.attachments) });
586
- reconcile.current();
587
- });
588
- };
589
- useEffect(() => reconcile.current(), [aui, thread, view]);
590
- useEffect(() => {
591
- if (thread === null)
592
- return;
593
- const at = `${thread}\u0000`;
594
- const composer = aui.thread.composer;
595
- return composer.subscribe(() => {
596
- if (seeding.current || !heard.current.startsWith(at))
597
- return;
598
- const state = composer.getState();
599
- const next = `${at}${composed(state)}`;
600
- if (next === shown.current)
601
- return;
602
- shown.current = next;
603
- keep(thread, { text: state.text, files: filesOf(state.attachments) });
604
- });
605
- }, [aui, thread, keep]);
606
- const queued = lanes().map((item) => ({ id: item.id, text: textOf(item.parts) }));
607
- const chat = {
608
- ...open,
609
- conversations,
610
- agents,
611
- view,
612
- running,
613
- questions,
614
- lone,
615
- options: optionsOf(view),
616
- commands,
617
- context: contextOf(view),
618
- queued,
619
- sending: { busy: sending.busy, outcome: sending.outcome, again },
620
- acting: { busy: acting.busy, outcome: acting.outcome },
621
- open: openConversation,
622
- start,
623
- send: (draft) => lined().enqueue(appended(draft)),
624
- answer,
625
- choose,
626
- stop,
627
- kill,
628
- done: () => done(),
629
- rename: (name) => rename(name),
630
- remove: () => remove(),
631
- edit,
632
- unqueue: (id) => {
633
- unqueue(id);
634
- },
635
- };
636
- return createElement(AssistantRuntimeProvider, { runtime: aui }, createElement(ChatContext.Provider, { value: chat }, children));
637
- }
@@ -1,31 +0,0 @@
1
- import type { ConversationView } from '../conversation.js';
2
- export type ChatChoice = {
3
- readonly value: string;
4
- readonly label: string;
5
- readonly about: string | null;
6
- };
7
- export type ChatField = {
8
- readonly name: string;
9
- readonly kind: 'string' | 'number' | 'integer' | 'boolean' | 'multi-select';
10
- readonly prompt: string;
11
- readonly about: string | null;
12
- readonly choices: readonly ChatChoice[];
13
- };
14
- export type ChatAnswer = Readonly<Record<string, unknown>>;
15
- export type ChatQuestion = {
16
- readonly interaction: string;
17
- readonly secret: boolean;
18
- readonly message: string;
19
- readonly fields: readonly ChatField[];
20
- readonly undelivered: boolean;
21
- accept(answer: ChatAnswer): unknown;
22
- readonly decline: unknown;
23
- };
24
- export declare function entered(field: ChatField): boolean;
25
- export declare function questionsOf(view: ConversationView | null): readonly ChatQuestion[];
26
- export type ChatLone = {
27
- readonly question: ChatQuestion;
28
- readonly field: ChatField;
29
- };
30
- export declare function loneOf(questions: readonly ChatQuestion[]): ChatLone | null;
31
- //# sourceMappingURL=questions.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"questions.d.ts","sourceRoot":"","sources":["../../src/chat/questions.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAC;AAG3D,MAAM,MAAM,UAAU,GAAG;IACvB,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,KAAK,EAAE,MAAM,GAAG,IAAI,CAAC;CAC/B,CAAC;AAEF,MAAM,MAAM,SAAS,GAAG;IACtB,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,IAAI,EAAE,QAAQ,GAAG,QAAQ,GAAG,SAAS,GAAG,SAAS,GAAG,cAAc,CAAC;IAC5E,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,QAAQ,CAAC,KAAK,EAAE,MAAM,GAAG,IAAI,CAAC;IAC9B,QAAQ,CAAC,OAAO,EAAE,SAAS,UAAU,EAAE,CAAC;CACzC,CAAC;AAEF,MAAM,MAAM,UAAU,GAAG,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CAAC;AAE3D,MAAM,MAAM,YAAY,GAAG;IACzB,QAAQ,CAAC,WAAW,EAAE,MAAM,CAAC;IAC7B,QAAQ,CAAC,MAAM,EAAE,OAAO,CAAC;IACzB,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IACzB,QAAQ,CAAC,MAAM,EAAE,SAAS,SAAS,EAAE,CAAC;IACtC,QAAQ,CAAC,WAAW,EAAE,OAAO,CAAC;IAC9B,MAAM,CAAC,MAAM,EAAE,UAAU,GAAG,OAAO,CAAC;IACpC,QAAQ,CAAC,OAAO,EAAE,OAAO,CAAC;CAC3B,CAAC;AAuCF,wBAAgB,OAAO,CAAC,KAAK,EAAE,SAAS,GAAG,OAAO,CAEjD;AA2DD,wBAAgB,WAAW,CAAC,IAAI,EAAE,gBAAgB,GAAG,IAAI,GAAG,SAAS,YAAY,EAAE,CAgBlF;AAED,MAAM,MAAM,QAAQ,GAAG;IAAE,QAAQ,CAAC,QAAQ,EAAE,YAAY,CAAC;IAAC,QAAQ,CAAC,KAAK,EAAE,SAAS,CAAA;CAAE,CAAC;AAEtF,wBAAgB,MAAM,CAAC,SAAS,EAAE,SAAS,YAAY,EAAE,GAAG,QAAQ,GAAG,IAAI,CAM1E"}