@parallelworks/code 0.1.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +171 -24
- package/dist/ago.d.ts +8 -0
- package/dist/ago.js +26 -0
- package/dist/api.d.ts +3547 -0
- package/dist/apiClient.d.ts +26 -0
- package/dist/apiClient.js +5 -0
- package/dist/base64.d.ts +3 -0
- package/dist/base64.js +15 -0
- package/dist/chatHandoff.d.ts +12 -0
- package/dist/chatHandoff.js +27 -0
- package/dist/claudeAccounts.d.ts +112 -0
- package/dist/claudeAccounts.js +234 -0
- package/dist/claudeApi.d.ts +27 -0
- package/dist/claudeApi.js +250 -0
- package/dist/claudeSignIn.d.ts +62 -0
- package/dist/claudeSignIn.js +239 -0
- package/dist/command.d.ts +47 -0
- package/dist/command.js +64 -0
- package/dist/commandsApi.d.ts +64 -0
- package/dist/commandsApi.js +267 -0
- package/dist/contextRow.d.ts +25 -0
- package/dist/contextRow.js +16 -0
- package/dist/createPwCodeApi.d.ts +696 -0
- package/dist/createPwCodeApi.js +33 -0
- package/dist/howSessionsAppear.d.ts +12 -0
- package/dist/howSessionsAppear.js +24 -0
- package/dist/index.d.ts +39 -3
- package/dist/index.js +38 -3
- package/dist/locales/en/agentTranscript.js +162 -0
- package/dist/locales/en/agents.js +380 -0
- package/dist/locales/en.d.ts +523 -0
- package/dist/locales/en.js +8 -0
- package/dist/locales/es/agentTranscript.js +162 -0
- package/dist/locales/es/agents.js +380 -0
- package/dist/locales/es.d.ts +3 -0
- package/dist/locales/es.js +8 -0
- package/dist/locales/ja/agentTranscript.js +162 -0
- package/dist/locales/ja/agents.js +380 -0
- package/dist/locales/ja.d.ts +3 -0
- package/dist/locales/ja.js +8 -0
- package/dist/locales/ko/agentTranscript.js +162 -0
- package/dist/locales/ko/agents.js +380 -0
- package/dist/locales/ko.d.ts +3 -0
- package/dist/locales/ko.js +8 -0
- package/dist/locales/zh/agentTranscript.js +162 -0
- package/dist/locales/zh/agents.js +380 -0
- package/dist/locales/zh.d.ts +3 -0
- package/dist/locales/zh.js +8 -0
- package/dist/machine.d.ts +51 -0
- package/dist/machine.js +60 -0
- package/dist/messages.d.ts +17 -0
- package/dist/messages.js +31 -0
- package/dist/models.d.ts +47 -0
- package/dist/models.js +49 -0
- package/dist/notices.d.ts +12 -0
- package/dist/notices.js +120 -0
- package/dist/pullRequest.d.ts +8 -0
- package/dist/pullRequest.js +29 -0
- package/dist/pullRequestList.d.ts +52 -0
- package/dist/pullRequestList.js +106 -0
- package/dist/query.d.ts +28 -0
- package/dist/query.js +25 -0
- package/dist/session.d.ts +75 -1
- package/dist/session.js +101 -1
- package/dist/sessionActions.d.ts +105 -0
- package/dist/sessionActions.js +178 -0
- package/dist/sessionEvents.d.ts +27 -0
- package/dist/sessionEvents.js +212 -0
- package/dist/sessionGroups.d.ts +49 -0
- package/dist/sessionGroups.js +72 -0
- package/dist/sessionHeader.d.ts +14 -0
- package/dist/sessionHeader.js +16 -0
- package/dist/sessionList.d.ts +134 -0
- package/dist/sessionList.js +271 -0
- package/dist/sessionsApi.d.ts +105 -0
- package/dist/sessionsApi.js +511 -0
- package/dist/settingsApi.d.ts +36 -0
- package/dist/settingsApi.js +151 -0
- package/dist/startSession.d.ts +138 -0
- package/dist/startSession.js +234 -0
- package/dist/terminalHandoffPanel.d.ts +30 -0
- package/dist/terminalHandoffPanel.js +33 -0
- package/dist/toolStrings.d.ts +4 -0
- package/dist/toolStrings.js +95 -0
- package/dist/transcript.d.ts +14 -1
- package/dist/transcript.js +55 -14
- package/dist/types.d.ts +20 -1
- package/dist/usageLimit.d.ts +8 -0
- package/dist/usageLimit.js +13 -0
- package/dist/useAwayRecap.d.ts +30 -0
- package/dist/useAwayRecap.js +75 -0
- package/dist/useComposer.d.ts +121 -0
- package/dist/useComposer.js +244 -0
- package/dist/useComposerImages.d.ts +66 -0
- package/dist/useComposerImages.js +195 -0
- package/dist/useConversation.d.ts +190 -0
- package/dist/useConversation.js +287 -0
- package/dist/useSubagent.d.ts +58 -0
- package/dist/useSubagent.js +149 -0
- package/dist/useTranscript.d.ts +4 -1
- package/dist/useTranscript.js +54 -11
- package/dist/wording.d.ts +43 -0
- package/dist/wording.js +101 -0
- package/dist/workingDirectories.d.ts +32 -0
- package/dist/workingDirectories.js +71 -0
- package/dist/worktreeChoice.d.ts +31 -0
- package/dist/worktreeChoice.js +38 -0
- package/locales/en/agentTranscript.json +154 -0
- package/locales/en/agents.json +366 -0
- package/locales/es/agentTranscript.json +154 -0
- package/locales/es/agents.json +366 -0
- package/locales/ja/agentTranscript.json +154 -0
- package/locales/ja/agents.json +366 -0
- package/locales/ko/agentTranscript.json +154 -0
- package/locales/ko/agents.json +366 -0
- package/locales/zh/agentTranscript.json +154 -0
- package/locales/zh/agents.json +366 -0
- package/package.json +24 -9
|
@@ -0,0 +1,287 @@
|
|
|
1
|
+
import { steerMayHaveLanded } from "./command.js";
|
|
2
|
+
import { sessionImageUrl } from "./images.js";
|
|
3
|
+
import { isUnreachableError } from "./machine.js";
|
|
4
|
+
import { usePwCodeTranslations } from "./messages.js";
|
|
5
|
+
import { useCompactionNotice, useRecapNotice, useTranscriptWords } from "./notices.js";
|
|
6
|
+
import { isSession, sharedStoreCopy } from "./session.js";
|
|
7
|
+
import { changesSubagents } from "./sessionEvents.js";
|
|
8
|
+
import { pasteRefs, patchSessionNames } from "./sessionsApi.js";
|
|
9
|
+
import { pendingApprovals, pendingMessages } from "./transcript.js";
|
|
10
|
+
import { usePwCodeTranscript } from "./useTranscript.js";
|
|
11
|
+
import { useCallback, useEffect, useRef, useState } from "react";
|
|
12
|
+
// src/useConversation.ts
|
|
13
|
+
/** A transcript's messages as a view draws them: those it holds, then the one
|
|
14
|
+
* streaming, then those still waiting to be taken. */
|
|
15
|
+
function transcriptMessages(transcript) {
|
|
16
|
+
const messages = transcript?.messages ?? [];
|
|
17
|
+
const queued = transcript ? pendingMessages(transcript) : [];
|
|
18
|
+
return {
|
|
19
|
+
messages,
|
|
20
|
+
queued,
|
|
21
|
+
all: transcript ? [
|
|
22
|
+
...messages,
|
|
23
|
+
...transcript.streaming ? [transcript.streaming] : [],
|
|
24
|
+
...queued
|
|
25
|
+
] : messages
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
/** A direction that got no answer may have landed, so it reads as lost, never as refused. */
|
|
29
|
+
async function directionOutcome(request, lost, describeError) {
|
|
30
|
+
try {
|
|
31
|
+
const { data, error } = await request();
|
|
32
|
+
if (!error) return { delivered: !!data?.delivered };
|
|
33
|
+
return steerMayHaveLanded(error) ? { lost } : { error: describeError(error) };
|
|
34
|
+
} catch {
|
|
35
|
+
return { lost };
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
/** What a frame says of the main conversation's own turn; null leaves it to the refetched detail, undefined says nothing. */
|
|
39
|
+
function mainTurnAfter(ev) {
|
|
40
|
+
if (ev.type === "replay_truncated") return null;
|
|
41
|
+
if (ev.id) return;
|
|
42
|
+
if (ev.type === "user_message" || ev.type === "turn_start") return true;
|
|
43
|
+
if (ev.turnId && (ev.type === "done" || ev.type === "error")) return false;
|
|
44
|
+
}
|
|
45
|
+
/** An open session's conversation: its live transcript, sends routed to the
|
|
46
|
+
* machine, approvals, the notice above the composer, and its subagents. The
|
|
47
|
+
* host reads the listing and the session (`api.useSessions`,
|
|
48
|
+
* `api.useSessionDetail`) and opens the feed with `api.useSessionEvents`,
|
|
49
|
+
* handing it `onEvent` and, once the detail is this visit's own, `cursor`. */
|
|
50
|
+
function usePwCodeConversation({ api, machine, sessionId, sessions, detail: read, describeError, notify, onCleared, onMoved, goalNotice }) {
|
|
51
|
+
const t = usePwCodeTranslations("agents");
|
|
52
|
+
const { data: list, mutate: mutateSessions } = sessions;
|
|
53
|
+
const { data: detail, mutate } = read;
|
|
54
|
+
const session = list?.sessions?.find((s) => isSession(s, machine, sessionId)) ?? detail;
|
|
55
|
+
const moved = sharedStoreCopy(list, machine, sessionId);
|
|
56
|
+
const onMovedRef = useRef(onMoved);
|
|
57
|
+
onMovedRef.current = onMoved;
|
|
58
|
+
useEffect(() => {
|
|
59
|
+
if (moved) onMovedRef.current(moved);
|
|
60
|
+
}, [moved]);
|
|
61
|
+
const [liveState, setLiveState] = useState(null);
|
|
62
|
+
const [notice, setNotice] = useState(null);
|
|
63
|
+
const showError = useCallback((error) => setNotice({
|
|
64
|
+
text: describeError(error),
|
|
65
|
+
warn: true,
|
|
66
|
+
unreachable: isUnreachableError(error)
|
|
67
|
+
}), [describeError]);
|
|
68
|
+
const warn = useCallback((text, unreachable) => setNotice(unreachable === void 0 ? {
|
|
69
|
+
text,
|
|
70
|
+
warn: true
|
|
71
|
+
} : {
|
|
72
|
+
text,
|
|
73
|
+
warn: true,
|
|
74
|
+
unreachable
|
|
75
|
+
}), []);
|
|
76
|
+
const dismissNotice = useCallback(() => setNotice(null), []);
|
|
77
|
+
const reached = useCallback(() => setNotice((current) => current?.unreachable ? null : current), []);
|
|
78
|
+
const sendUnreachable = useRef(false);
|
|
79
|
+
const runEffect = useCallback((effect) => {
|
|
80
|
+
switch (effect.type) {
|
|
81
|
+
case "refetch":
|
|
82
|
+
mutate();
|
|
83
|
+
break;
|
|
84
|
+
case "cleared":
|
|
85
|
+
notify(t("detail.cleared"));
|
|
86
|
+
onCleared(effect.newId);
|
|
87
|
+
break;
|
|
88
|
+
case "state": {
|
|
89
|
+
setLiveState(effect.state);
|
|
90
|
+
const names = {
|
|
91
|
+
name: effect.state.name ?? "",
|
|
92
|
+
autoName: effect.state.autoName ?? ""
|
|
93
|
+
};
|
|
94
|
+
mutateSessions((current) => patchSessionNames(current, machine, sessionId, names), { revalidate: false });
|
|
95
|
+
break;
|
|
96
|
+
}
|
|
97
|
+
case "notice": setNotice({
|
|
98
|
+
text: effect.text,
|
|
99
|
+
warn: effect.warn
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
}, [
|
|
103
|
+
machine,
|
|
104
|
+
sessionId,
|
|
105
|
+
mutate,
|
|
106
|
+
mutateSessions,
|
|
107
|
+
notify,
|
|
108
|
+
onCleared,
|
|
109
|
+
t
|
|
110
|
+
]);
|
|
111
|
+
const postMessage = useCallback(async (text, pastes, images) => {
|
|
112
|
+
try {
|
|
113
|
+
const { data, error } = await api.sendMessage(machine, sessionId, {
|
|
114
|
+
text,
|
|
115
|
+
...pasteRefs(pastes),
|
|
116
|
+
...images ? { images } : {}
|
|
117
|
+
});
|
|
118
|
+
if (error) {
|
|
119
|
+
sendUnreachable.current = isUnreachableError(error);
|
|
120
|
+
return { error: describeError(error) };
|
|
121
|
+
}
|
|
122
|
+
return {
|
|
123
|
+
turnId: data?.turnId,
|
|
124
|
+
queued: data?.queued
|
|
125
|
+
};
|
|
126
|
+
} catch (error) {
|
|
127
|
+
return { error: describeError(error) };
|
|
128
|
+
}
|
|
129
|
+
}, [
|
|
130
|
+
api,
|
|
131
|
+
describeError,
|
|
132
|
+
machine,
|
|
133
|
+
sessionId
|
|
134
|
+
]);
|
|
135
|
+
const withdrawQueued = useCallback(async (turnId) => {
|
|
136
|
+
const { error } = await api.withdrawQueued(machine, sessionId, turnId);
|
|
137
|
+
return error ? { error: describeError(error) } : {};
|
|
138
|
+
}, [
|
|
139
|
+
api,
|
|
140
|
+
describeError,
|
|
141
|
+
machine,
|
|
142
|
+
sessionId
|
|
143
|
+
]);
|
|
144
|
+
const postDirection = useCallback((text, pastes, images) => directionOutcome(() => api.steer(machine, sessionId, {
|
|
145
|
+
text,
|
|
146
|
+
...pasteRefs(pastes),
|
|
147
|
+
...images ? { images } : {}
|
|
148
|
+
}), t("detail.steerUncertain"), describeError), [
|
|
149
|
+
api,
|
|
150
|
+
describeError,
|
|
151
|
+
machine,
|
|
152
|
+
sessionId,
|
|
153
|
+
t
|
|
154
|
+
]);
|
|
155
|
+
const compactionNotice = useCompactionNotice();
|
|
156
|
+
const recapNotice = useRecapNotice();
|
|
157
|
+
const words = useTranscriptWords();
|
|
158
|
+
const imageUrl = useCallback((ref) => sessionImageUrl(machine, sessionId, ref), [machine, sessionId]);
|
|
159
|
+
const { transcript, sending, onEvent: applyEvent, sendMessage: sendToTranscript, resolveApproval, noteShellRun, canRemoveQueued, removeQueued: removeFromTranscript, cursor } = usePwCodeTranscript({
|
|
160
|
+
sessionId,
|
|
161
|
+
detail,
|
|
162
|
+
send: postMessage,
|
|
163
|
+
steer: postDirection,
|
|
164
|
+
withdraw: withdrawQueued,
|
|
165
|
+
onEffect: runEffect,
|
|
166
|
+
...goalNotice ? { goalNotice } : {},
|
|
167
|
+
compactionNotice,
|
|
168
|
+
recapNotice,
|
|
169
|
+
words,
|
|
170
|
+
imageUrl
|
|
171
|
+
});
|
|
172
|
+
/** Resolves to the refusal and whether it was the machine not answering, or null once the session took the message. */
|
|
173
|
+
const deliver = useCallback(async (text, options) => {
|
|
174
|
+
sendUnreachable.current = false;
|
|
175
|
+
const refusal = await sendToTranscript(text, options);
|
|
176
|
+
return refusal === null ? null : {
|
|
177
|
+
text: refusal,
|
|
178
|
+
unreachable: sendUnreachable.current
|
|
179
|
+
};
|
|
180
|
+
}, [sendToTranscript]);
|
|
181
|
+
const removeQueued = useCallback(async (id) => {
|
|
182
|
+
const refusal = await removeFromTranscript(id);
|
|
183
|
+
if (refusal) setNotice({
|
|
184
|
+
text: refusal,
|
|
185
|
+
warn: true
|
|
186
|
+
});
|
|
187
|
+
}, [removeFromTranscript]);
|
|
188
|
+
const answerApproval = useCallback(async (approvalId, answer) => {
|
|
189
|
+
const refusal = await api.answerApproval(machine, sessionId, approvalId, answer);
|
|
190
|
+
if (refusal) {
|
|
191
|
+
showError(refusal);
|
|
192
|
+
return;
|
|
193
|
+
}
|
|
194
|
+
setNotice(null);
|
|
195
|
+
resolveApproval(approvalId);
|
|
196
|
+
}, [
|
|
197
|
+
api,
|
|
198
|
+
showError,
|
|
199
|
+
resolveApproval,
|
|
200
|
+
machine,
|
|
201
|
+
sessionId
|
|
202
|
+
]);
|
|
203
|
+
const { subagents, refresh: refreshSubagents } = api.useSubagents(machine, sessionId, !!detail && detail.status !== "live_elsewhere");
|
|
204
|
+
const feedListeners = useRef(/* @__PURE__ */ new Set());
|
|
205
|
+
const subscribe = useCallback((listener) => {
|
|
206
|
+
feedListeners.current.add(listener);
|
|
207
|
+
return () => {
|
|
208
|
+
feedListeners.current.delete(listener);
|
|
209
|
+
};
|
|
210
|
+
}, []);
|
|
211
|
+
const [mainTurn, setMainTurn] = useState(null);
|
|
212
|
+
const childTurns = useRef(/* @__PURE__ */ new Set());
|
|
213
|
+
const onEvent = useCallback((ev) => {
|
|
214
|
+
applyEvent(ev);
|
|
215
|
+
for (const listener of feedListeners.current) listener(ev);
|
|
216
|
+
const said = mainTurnAfter(ev);
|
|
217
|
+
if (said !== void 0) setMainTurn(said);
|
|
218
|
+
const childTurn = ev.id ? ev.turnId : void 0;
|
|
219
|
+
const started = !!childTurn && !childTurns.current.has(childTurn);
|
|
220
|
+
if (childTurn) childTurns.current.add(childTurn);
|
|
221
|
+
if (started || changesSubagents(ev)) refreshSubagents();
|
|
222
|
+
}, [applyEvent, refreshSubagents]);
|
|
223
|
+
const stopSubagent = useCallback(async (id) => {
|
|
224
|
+
const refusal = await api.stopSubagent(machine, sessionId, id);
|
|
225
|
+
if (refusal) showError(refusal);
|
|
226
|
+
}, [
|
|
227
|
+
api,
|
|
228
|
+
machine,
|
|
229
|
+
sessionId,
|
|
230
|
+
showError
|
|
231
|
+
]);
|
|
232
|
+
const stopAllSubagents = useCallback(async () => {
|
|
233
|
+
const refusal = await api.stopAllSubagents(machine, sessionId);
|
|
234
|
+
if (refusal) showError(refusal);
|
|
235
|
+
}, [
|
|
236
|
+
api,
|
|
237
|
+
machine,
|
|
238
|
+
sessionId,
|
|
239
|
+
showError
|
|
240
|
+
]);
|
|
241
|
+
const goal = liveState ? liveState.goal : detail?.goal;
|
|
242
|
+
const addDirs = liveState ? liveState.addDirs : detail?.addDirs;
|
|
243
|
+
const allowedDirs = liveState ? liveState.allowedDirs : detail?.allowedDirs;
|
|
244
|
+
const steered = new Set(transcript ? [...transcript.notes, ...transcript.steered].map((m) => m.id) : []);
|
|
245
|
+
const mainWaits = !!transcript && pendingApprovals(transcript).some((p) => !p.agent);
|
|
246
|
+
const mainRuns = mainTurn ?? !!detail?.turnId;
|
|
247
|
+
const mainState = mainWaits ? "approval" : mainRuns ? "running" : "idle";
|
|
248
|
+
return {
|
|
249
|
+
/** The session as the listing has it, or as its fetch does until then. */
|
|
250
|
+
session,
|
|
251
|
+
detail,
|
|
252
|
+
/** The session's state as its last state frame said, until which the detail stands. */
|
|
253
|
+
liveState,
|
|
254
|
+
goal,
|
|
255
|
+
addDirs,
|
|
256
|
+
allowedDirs,
|
|
257
|
+
refetch: mutate,
|
|
258
|
+
transcript,
|
|
259
|
+
...transcriptMessages(transcript),
|
|
260
|
+
sending,
|
|
261
|
+
/** Hands each frame of the session's feed to the transcript and the subagent views. */
|
|
262
|
+
onEvent,
|
|
263
|
+
/** Where the feed resumes from. */
|
|
264
|
+
cursor,
|
|
265
|
+
/** Hands every frame of the feed to `listener` until the returned call, as a subagent's view reads it. */
|
|
266
|
+
subscribe,
|
|
267
|
+
notice,
|
|
268
|
+
dismissNotice,
|
|
269
|
+
warn,
|
|
270
|
+
showError,
|
|
271
|
+
/** Takes back a notice that the machine could not be reached, once it answers. */
|
|
272
|
+
reached,
|
|
273
|
+
deliver,
|
|
274
|
+
noteShellRun,
|
|
275
|
+
canRemoveQueued,
|
|
276
|
+
removeQueued,
|
|
277
|
+
/** Whether a queued message goes into the running turn's next step or waits for its reply. */
|
|
278
|
+
queueDelivery: (id) => steered.has(id) ? "nextStep" : "afterReply",
|
|
279
|
+
answerApproval,
|
|
280
|
+
subagents,
|
|
281
|
+
refreshSubagents,
|
|
282
|
+
stopSubagent,
|
|
283
|
+
stopAllSubagents,
|
|
284
|
+
mainState
|
|
285
|
+
};
|
|
286
|
+
}
|
|
287
|
+
export { directionOutcome, transcriptMessages, usePwCodeConversation };
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import type { ApprovalAnswerValue, MessagePaste } from '@parallelworks/ui/ai/headless';
|
|
2
|
+
import type { Schemas } from './apiClient.js';
|
|
3
|
+
import type { PwCodeApi } from './createPwCodeApi.js';
|
|
4
|
+
/** One subagent's conversation in place of the main one: its transcript, kept
|
|
5
|
+
* current from the session's feed, and a composer that talks to it. Commands
|
|
6
|
+
* and `!` stay with the main conversation's composer. The host reads the
|
|
7
|
+
* subagent with `api.useSubagentDetail`. */
|
|
8
|
+
export declare function usePwCodeSubagent({ api, machine, sessionId, subId, subagent, subscribe, loadPalette, describeError, sessionLock, name, onSent, }: {
|
|
9
|
+
api: PwCodeApi;
|
|
10
|
+
machine: string;
|
|
11
|
+
sessionId: string;
|
|
12
|
+
subId: string;
|
|
13
|
+
/** The subagent, as `api.useSubagentDetail` reads it. */
|
|
14
|
+
subagent: {
|
|
15
|
+
data: Schemas['SubagentDetail'] | undefined;
|
|
16
|
+
refresh: () => void;
|
|
17
|
+
};
|
|
18
|
+
/** Hands over each frame of the session's feed until the returned call: the main conversation's `subscribe`. */
|
|
19
|
+
subscribe: (listener: (ev: Schemas['AgentStreamEvent']) => void) => () => void;
|
|
20
|
+
/** The session's palette, which says what typed text is a command. */
|
|
21
|
+
loadPalette: () => Promise<Schemas['AgentPalette'] | null>;
|
|
22
|
+
/** Words a refusal or failure for the reader. */
|
|
23
|
+
describeError: (error: unknown) => string;
|
|
24
|
+
/** Why nothing can be sent to the session at all, such as an unreachable machine. */
|
|
25
|
+
sessionLock: {
|
|
26
|
+
placeholder: string;
|
|
27
|
+
hint: string;
|
|
28
|
+
} | null;
|
|
29
|
+
/** What the composer calls the subagent. */
|
|
30
|
+
name: string;
|
|
31
|
+
/** The subagent took a message, so the session's list of them reads differently. */
|
|
32
|
+
onSent: () => void;
|
|
33
|
+
}): {
|
|
34
|
+
messages: import("@parallelworks/ui/ai/headless").ChatMessage[];
|
|
35
|
+
queued: import("@parallelworks/ui/ai/headless").ChatMessage[];
|
|
36
|
+
all: import("@parallelworks/ui/ai/headless").ChatMessage[];
|
|
37
|
+
transcript: import("./transcript.js").TranscriptState | null;
|
|
38
|
+
sending: boolean;
|
|
39
|
+
running: boolean;
|
|
40
|
+
approvals: import("@parallelworks/ui/ai/headless").ApprovalPart[];
|
|
41
|
+
notice: string | null;
|
|
42
|
+
dismissNotice: () => void;
|
|
43
|
+
/** Sends what was typed; false hands it back to the composer. */
|
|
44
|
+
send: (typed: string, sent?: MessagePaste[]) => Promise<boolean>;
|
|
45
|
+
turn: {
|
|
46
|
+
running: boolean;
|
|
47
|
+
stop: () => Promise<void>;
|
|
48
|
+
stopping: boolean;
|
|
49
|
+
};
|
|
50
|
+
answerApproval: (approvalId: string, answer: ApprovalAnswerValue) => Promise<void>;
|
|
51
|
+
composer: {
|
|
52
|
+
placeholder: string;
|
|
53
|
+
hint: string;
|
|
54
|
+
locked: boolean;
|
|
55
|
+
disabled: boolean;
|
|
56
|
+
};
|
|
57
|
+
};
|
|
58
|
+
export type PwCodeSubagent = ReturnType<typeof usePwCodeSubagent>;
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
import { shellCommand, slashAction } from "./command.js";
|
|
2
|
+
import { sessionImageUrl } from "./images.js";
|
|
3
|
+
import { usePwCodeTranslations } from "./messages.js";
|
|
4
|
+
import { useTranscriptWords } from "./notices.js";
|
|
5
|
+
import { pendingApprovals, subagentSessionDetail, turnRunning } from "./transcript.js";
|
|
6
|
+
import { directionOutcome, transcriptMessages } from "./useConversation.js";
|
|
7
|
+
import { usePwCodeTranscript } from "./useTranscript.js";
|
|
8
|
+
import { pasteRefs } from "./sessionsApi.js";
|
|
9
|
+
import { composerState } from "./wording.js";
|
|
10
|
+
import { useCallback, useEffect, useMemo, useState } from "react";
|
|
11
|
+
// src/useSubagent.ts
|
|
12
|
+
/** One subagent's conversation in place of the main one: its transcript, kept
|
|
13
|
+
* current from the session's feed, and a composer that talks to it. Commands
|
|
14
|
+
* and `!` stay with the main conversation's composer. The host reads the
|
|
15
|
+
* subagent with `api.useSubagentDetail`. */
|
|
16
|
+
function usePwCodeSubagent({ api, machine, sessionId, subId, subagent, subscribe, loadPalette, describeError, sessionLock, name, onSent }) {
|
|
17
|
+
const t = usePwCodeTranslations("agents");
|
|
18
|
+
const { data, refresh } = subagent;
|
|
19
|
+
const detail = useMemo(() => data ? subagentSessionDetail(data) : void 0, [data]);
|
|
20
|
+
const [notice, setNotice] = useState(null);
|
|
21
|
+
const postMessage = useCallback(async (text, sent) => {
|
|
22
|
+
try {
|
|
23
|
+
const { data: accepted, error: refusal } = await api.messageSubagent(machine, sessionId, subId, {
|
|
24
|
+
text,
|
|
25
|
+
...pasteRefs(sent)
|
|
26
|
+
});
|
|
27
|
+
return refusal ? { error: describeError(refusal) } : { turnId: accepted?.turnId };
|
|
28
|
+
} catch (thrown) {
|
|
29
|
+
return { error: describeError(thrown) };
|
|
30
|
+
}
|
|
31
|
+
}, [
|
|
32
|
+
api,
|
|
33
|
+
describeError,
|
|
34
|
+
machine,
|
|
35
|
+
sessionId,
|
|
36
|
+
subId
|
|
37
|
+
]);
|
|
38
|
+
const postDirection = useCallback((text, sent) => directionOutcome(() => api.steerSubagent(machine, sessionId, subId, {
|
|
39
|
+
text,
|
|
40
|
+
...pasteRefs(sent)
|
|
41
|
+
}), t("detail.steerUncertain"), describeError), [
|
|
42
|
+
api,
|
|
43
|
+
describeError,
|
|
44
|
+
machine,
|
|
45
|
+
sessionId,
|
|
46
|
+
subId,
|
|
47
|
+
t
|
|
48
|
+
]);
|
|
49
|
+
const runEffect = useCallback((effect) => {
|
|
50
|
+
if (effect.type === "refetch") refresh();
|
|
51
|
+
}, [refresh]);
|
|
52
|
+
const words = useTranscriptWords();
|
|
53
|
+
const imageUrl = useCallback((ref) => sessionImageUrl(machine, sessionId, ref), [machine, sessionId]);
|
|
54
|
+
const { transcript, sending, onEvent, sendMessage: sendToTranscript, resolveApproval } = usePwCodeTranscript({
|
|
55
|
+
subagent: subId,
|
|
56
|
+
detail,
|
|
57
|
+
send: postMessage,
|
|
58
|
+
steer: postDirection,
|
|
59
|
+
onEffect: runEffect,
|
|
60
|
+
words,
|
|
61
|
+
imageUrl
|
|
62
|
+
});
|
|
63
|
+
useEffect(() => subscribe(onEvent), [subscribe, onEvent]);
|
|
64
|
+
const send = useCallback(async (typed, sent) => {
|
|
65
|
+
if (shellCommand(typed) !== null || typed.trimStart().startsWith("/") && slashAction(typed, await loadPalette().catch(() => null)).kind !== "message") {
|
|
66
|
+
setNotice(t("switcher.commandsOnMain"));
|
|
67
|
+
return false;
|
|
68
|
+
}
|
|
69
|
+
const refusal = await sendToTranscript(typed, { pastes: sent });
|
|
70
|
+
setNotice(refusal);
|
|
71
|
+
if (!refusal) onSent();
|
|
72
|
+
return !refusal;
|
|
73
|
+
}, [
|
|
74
|
+
loadPalette,
|
|
75
|
+
onSent,
|
|
76
|
+
sendToTranscript,
|
|
77
|
+
t
|
|
78
|
+
]);
|
|
79
|
+
const [stopping, setStopping] = useState(false);
|
|
80
|
+
const stop = useCallback(async () => {
|
|
81
|
+
setStopping(true);
|
|
82
|
+
const refusal = await api.stopSubagent(machine, sessionId, subId);
|
|
83
|
+
setStopping(false);
|
|
84
|
+
setNotice(refusal ? describeError(refusal) : null);
|
|
85
|
+
}, [
|
|
86
|
+
api,
|
|
87
|
+
describeError,
|
|
88
|
+
machine,
|
|
89
|
+
sessionId,
|
|
90
|
+
subId
|
|
91
|
+
]);
|
|
92
|
+
const answerApproval = useCallback(async (approvalId, answer) => {
|
|
93
|
+
const refusal = await api.answerApproval(machine, sessionId, approvalId, answer);
|
|
94
|
+
if (refusal) {
|
|
95
|
+
setNotice(describeError(refusal));
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
setNotice(null);
|
|
99
|
+
resolveApproval(approvalId);
|
|
100
|
+
}, [
|
|
101
|
+
api,
|
|
102
|
+
describeError,
|
|
103
|
+
machine,
|
|
104
|
+
resolveApproval,
|
|
105
|
+
sessionId
|
|
106
|
+
]);
|
|
107
|
+
const dismissNotice = useCallback(() => setNotice(null), []);
|
|
108
|
+
const running = turnRunning(transcript);
|
|
109
|
+
const approvals = transcript ? pendingApprovals(transcript) : [];
|
|
110
|
+
const composer = sessionLock ? {
|
|
111
|
+
...sessionLock,
|
|
112
|
+
locked: true
|
|
113
|
+
} : approvals.length > 0 ? composerState({
|
|
114
|
+
unreachable: false,
|
|
115
|
+
archived: false,
|
|
116
|
+
machineName: "",
|
|
117
|
+
staleFor: "",
|
|
118
|
+
status: "awaiting_approval",
|
|
119
|
+
liveHost: void 0,
|
|
120
|
+
approvals: approvals.map((p) => ({ kind: p.approvalKind })),
|
|
121
|
+
t
|
|
122
|
+
}) : {
|
|
123
|
+
placeholder: t("switcher.composer", { name }),
|
|
124
|
+
hint: running ? t("switcher.steerHint") : "",
|
|
125
|
+
locked: false
|
|
126
|
+
};
|
|
127
|
+
return {
|
|
128
|
+
transcript,
|
|
129
|
+
...transcriptMessages(transcript),
|
|
130
|
+
sending,
|
|
131
|
+
running,
|
|
132
|
+
approvals,
|
|
133
|
+
notice,
|
|
134
|
+
dismissNotice,
|
|
135
|
+
/** Sends what was typed; false hands it back to the composer. */
|
|
136
|
+
send,
|
|
137
|
+
turn: {
|
|
138
|
+
running: running && !sessionLock,
|
|
139
|
+
stop,
|
|
140
|
+
stopping
|
|
141
|
+
},
|
|
142
|
+
answerApproval,
|
|
143
|
+
composer: {
|
|
144
|
+
...composer,
|
|
145
|
+
disabled: sending || composer.locked || !data
|
|
146
|
+
}
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
export { usePwCodeSubagent };
|
package/dist/useTranscript.d.ts
CHANGED
|
@@ -27,9 +27,12 @@ interface SendOptions {
|
|
|
27
27
|
}
|
|
28
28
|
/** Holds the live transcript for one session. The host still owns the socket
|
|
29
29
|
* and the fetch: it feeds events to `onEvent` and posts through `send`. */
|
|
30
|
-
export declare function usePwCodeTranscript({ sessionId, detail, send, steer, withdraw, onEffect, goalNotice, compactionNotice, recapNotice, words, imageUrl, }: {
|
|
30
|
+
export declare function usePwCodeTranscript({ sessionId, subagent, detail, send, steer, withdraw, onEffect, goalNotice, compactionNotice, recapNotice, words, imageUrl, }: {
|
|
31
31
|
/** The session shown, so frames from the conversation it cleared are dropped. */
|
|
32
32
|
sessionId?: string | undefined;
|
|
33
|
+
/** Shows this subagent's conversation instead of the main one: the frames
|
|
34
|
+
* are still the session's, and `detail`, `send` and `steer` are the subagent's. */
|
|
35
|
+
subagent?: string | undefined;
|
|
33
36
|
detail: PwCodeSessionDetail | undefined;
|
|
34
37
|
send: (text: string, pastes?: MessagePaste[], images?: PwCodeImageRef[]) => Promise<PwCodeSendResult>;
|
|
35
38
|
/** Hands text to the running turn, as typing into a busy terminal does. */
|
package/dist/useTranscript.js
CHANGED
|
@@ -1,7 +1,11 @@
|
|
|
1
|
-
import { appendOwnMessage, appendShellRun, appendSteeredMessage, applyStreamEvent, dropMessage, initialTranscript, markQueued, pendingMessages, resolveApprovalEverywhere, turnRunning } from "./transcript.js";
|
|
1
|
+
import { appendOwnMessage, appendShellRun, appendSteeredMessage, appendTurnMessage, applyStreamEvent, dropMessage, initialTranscript, markQueued, pendingMessages, resolveApprovalEverywhere, subagentFrame, turnRunning } from "./transcript.js";
|
|
2
2
|
import { useCallback, useEffect, useRef, useState } from "react";
|
|
3
3
|
// src/useTranscript.ts
|
|
4
4
|
var MAX_REPLAY_FRAMES = 5e3;
|
|
5
|
+
function keepForSeed(frames, ev) {
|
|
6
|
+
frames.push(ev);
|
|
7
|
+
if (frames.length > MAX_REPLAY_FRAMES) frames.shift();
|
|
8
|
+
}
|
|
5
9
|
/** Matches the way the echo is recognised: by turn once the send has named it,
|
|
6
10
|
* by text before then. */
|
|
7
11
|
function echoedSend(unechoed, ev) {
|
|
@@ -19,9 +23,18 @@ function forgetSettled(unechoed, before, after) {
|
|
|
19
23
|
const still = new Set(pendingMessages(after).map((m) => m.id));
|
|
20
24
|
for (const m of pendingMessages(before)) if (!still.has(m.id)) unechoed.delete(m.id);
|
|
21
25
|
}
|
|
26
|
+
/** A turn prompted on a subagent echoes no user_message, so its history places
|
|
27
|
+
* the message: once it reaches the turn's first frame, or ends on the same words. */
|
|
28
|
+
function forgetPlaced(unechoed, detail) {
|
|
29
|
+
const last = detail.history?.findLast((h) => h.kind === "user" && !h.fromMain)?.text;
|
|
30
|
+
for (const [localId, sent] of unechoed) {
|
|
31
|
+
const reached = sent.placedSeq !== void 0 && (detail.lastSeq ?? 0) >= sent.placedSeq;
|
|
32
|
+
if (!sent.steered && (reached || sent.text === last)) unechoed.delete(localId);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
22
35
|
/** Holds the live transcript for one session. The host still owns the socket
|
|
23
36
|
* and the fetch: it feeds events to `onEvent` and posts through `send`. */
|
|
24
|
-
function usePwCodeTranscript({ sessionId, detail, send, steer, withdraw, onEffect, goalNotice, compactionNotice, recapNotice, words, imageUrl }) {
|
|
37
|
+
function usePwCodeTranscript({ sessionId, subagent, detail, send, steer, withdraw, onEffect, goalNotice, compactionNotice, recapNotice, words, imageUrl }) {
|
|
25
38
|
const [transcript, setTranscript] = useState(null);
|
|
26
39
|
const transcriptRef = useRef(null);
|
|
27
40
|
const [sending, setSending] = useState(false);
|
|
@@ -56,6 +69,7 @@ function usePwCodeTranscript({ sessionId, detail, send, steer, withdraw, onEffec
|
|
|
56
69
|
onEffectRef.current({ type: "refetch" });
|
|
57
70
|
return;
|
|
58
71
|
}
|
|
72
|
+
if (subagent) forgetPlaced(unechoed.current, detail);
|
|
59
73
|
let next = initialTranscript(detail, {
|
|
60
74
|
goalNotice: goalNoticeRef.current,
|
|
61
75
|
compactionNotice: compactionNoticeRef.current,
|
|
@@ -63,6 +77,12 @@ function usePwCodeTranscript({ sessionId, detail, send, steer, withdraw, onEffec
|
|
|
63
77
|
words: wordsRef.current,
|
|
64
78
|
imageUrl: imageUrlRef.current
|
|
65
79
|
});
|
|
80
|
+
if (subagent) {
|
|
81
|
+
for (const [localId, sent] of unechoed.current) if (!sent.steered) {
|
|
82
|
+
const { text, pastes, previews } = sent;
|
|
83
|
+
next = appendTurnMessage(next, text, localId, pastes, previews);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
66
86
|
const replay = appliedSinceSeed.current.filter((ev) => ev.seq > (lastSeq ?? 0));
|
|
67
87
|
appliedSinceSeed.current = replay;
|
|
68
88
|
for (const ev of replay) next = applyStreamEvent(next, ev, {
|
|
@@ -73,7 +93,8 @@ function usePwCodeTranscript({ sessionId, detail, send, steer, withdraw, onEffec
|
|
|
73
93
|
}).state;
|
|
74
94
|
for (const [localId, sent] of unechoed.current) {
|
|
75
95
|
const { text, pastes, previews, steered } = sent;
|
|
76
|
-
|
|
96
|
+
if (steered) next = appendSteeredMessage(next, text, localId, pastes, previews);
|
|
97
|
+
else if (!subagent) next = appendOwnMessage(next, text, localId, pastes, previews);
|
|
77
98
|
}
|
|
78
99
|
const cursor = Math.max(next.cursor, transcriptRef.current?.cursor ?? 0);
|
|
79
100
|
next = cursor === next.cursor ? next : {
|
|
@@ -82,7 +103,7 @@ function usePwCodeTranscript({ sessionId, detail, send, steer, withdraw, onEffec
|
|
|
82
103
|
};
|
|
83
104
|
transcriptRef.current = next;
|
|
84
105
|
setTranscript(next);
|
|
85
|
-
}, [detail]);
|
|
106
|
+
}, [detail, subagent]);
|
|
86
107
|
const apply = useCallback((change) => {
|
|
87
108
|
const current = transcriptRef.current;
|
|
88
109
|
if (!current) return;
|
|
@@ -90,9 +111,21 @@ function usePwCodeTranscript({ sessionId, detail, send, steer, withdraw, onEffec
|
|
|
90
111
|
transcriptRef.current = next;
|
|
91
112
|
setTranscript(next);
|
|
92
113
|
}, []);
|
|
93
|
-
const onEvent = useCallback((
|
|
114
|
+
const onEvent = useCallback((frame) => {
|
|
115
|
+
const ev = subagent ? subagentFrame(frame, subagent) : frame;
|
|
116
|
+
if (ev === "refetch") {
|
|
117
|
+
onEffectRef.current({ type: "refetch" });
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
if (!ev) return;
|
|
94
121
|
const current = transcriptRef.current;
|
|
95
|
-
if (!current)
|
|
122
|
+
if (!current) {
|
|
123
|
+
if (subagent) keepForSeed(appliedSinceSeed.current, ev);
|
|
124
|
+
return;
|
|
125
|
+
}
|
|
126
|
+
if (subagent && ev.turnId) {
|
|
127
|
+
for (const sent of unechoed.current.values()) if (sent.turnId === ev.turnId) sent.placedSeq ??= ev.seq;
|
|
128
|
+
}
|
|
96
129
|
const echoOf = echoedSend(unechoed.current, ev);
|
|
97
130
|
const { state, effects } = applyStreamEvent(current, ev, {
|
|
98
131
|
ownTurnIds: ownTurnIds.current,
|
|
@@ -106,14 +139,18 @@ function usePwCodeTranscript({ sessionId, detail, send, steer, withdraw, onEffec
|
|
|
106
139
|
});
|
|
107
140
|
if (ev.type === "replay_truncated") appliedSinceSeed.current = [];
|
|
108
141
|
else if (state.cursor > current.cursor) {
|
|
109
|
-
appliedSinceSeed.current
|
|
110
|
-
if (appliedSinceSeed.current.length > MAX_REPLAY_FRAMES) appliedSinceSeed.current.shift();
|
|
142
|
+
keepForSeed(appliedSinceSeed.current, ev);
|
|
111
143
|
if (echoOf) unechoed.current.delete(echoOf);
|
|
112
144
|
forgetSettled(unechoed.current, current, state);
|
|
113
145
|
}
|
|
114
146
|
apply(() => state);
|
|
115
147
|
for (const effect of effects) onEffectRef.current(effect);
|
|
116
|
-
|
|
148
|
+
if (subagent && frame.type === "subagent_user_note") onEffectRef.current({ type: "refetch" });
|
|
149
|
+
}, [
|
|
150
|
+
apply,
|
|
151
|
+
sessionId,
|
|
152
|
+
subagent
|
|
153
|
+
]);
|
|
117
154
|
/** True once the running turn took the text, or the refusal if it may have.
|
|
118
155
|
* Commands and shell escapes change the next turn, so like the TUI they wait. */
|
|
119
156
|
const trySteer = useCallback(async (text, { pastes, images, previews }) => {
|
|
@@ -150,13 +187,15 @@ function usePwCodeTranscript({ sessionId, detail, send, steer, withdraw, onEffec
|
|
|
150
187
|
pastes,
|
|
151
188
|
previews
|
|
152
189
|
});
|
|
153
|
-
|
|
190
|
+
const append = subagent ? appendTurnMessage : appendOwnMessage;
|
|
191
|
+
apply((state) => append(state, text, localId, pastes, previews));
|
|
154
192
|
const result = await sendRef.current(text, pastes, images);
|
|
155
193
|
setSending(false);
|
|
156
194
|
if ("error" in result) {
|
|
157
195
|
ownTexts.current.delete(text);
|
|
158
196
|
unechoed.current.delete(localId);
|
|
159
197
|
apply((state) => dropMessage(state, localId));
|
|
198
|
+
if (subagent) onEffectRef.current({ type: "refetch" });
|
|
160
199
|
return result.error;
|
|
161
200
|
}
|
|
162
201
|
if (result.queued && unechoed.current.has(localId)) apply((state) => markQueued(state, localId));
|
|
@@ -167,7 +206,11 @@ function usePwCodeTranscript({ sessionId, detail, send, steer, withdraw, onEffec
|
|
|
167
206
|
if (pending) pending.turnId = result.turnId;
|
|
168
207
|
}
|
|
169
208
|
return null;
|
|
170
|
-
}, [
|
|
209
|
+
}, [
|
|
210
|
+
apply,
|
|
211
|
+
trySteer,
|
|
212
|
+
subagent
|
|
213
|
+
]);
|
|
171
214
|
const resolveApproval = useCallback((approvalId) => apply((state) => resolveApprovalEverywhere(state, approvalId)), [apply]);
|
|
172
215
|
const noteShellRun = useCallback((command, output) => apply((state) => appendShellRun(state, command, output)), [apply]);
|
|
173
216
|
const queuedTurn = (id) => {
|