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