@parallelworks/code 0.2.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 +43 -10
- package/dist/ago.d.ts +8 -0
- package/dist/ago.js +26 -0
- package/dist/api.d.ts +78 -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/claudeSignIn.d.ts +62 -0
- package/dist/claudeSignIn.js +239 -0
- package/dist/contextRow.d.ts +25 -0
- package/dist/contextRow.js +16 -0
- package/dist/createPwCodeApi.d.ts +8 -0
- package/dist/createPwCodeApi.js +2 -0
- package/dist/howSessionsAppear.d.ts +12 -0
- package/dist/howSessionsAppear.js +24 -0
- package/dist/index.d.ts +24 -1
- package/dist/index.js +24 -2
- package/dist/locales/en/agentTranscript.js +2 -10
- package/dist/locales/en/agents.js +5 -5
- package/dist/locales/en.d.ts +5 -13
- package/dist/locales/es/agentTranscript.js +2 -10
- package/dist/locales/es/agents.js +5 -5
- package/dist/locales/ja/agentTranscript.js +2 -10
- package/dist/locales/ja/agents.js +5 -5
- package/dist/locales/ko/agentTranscript.js +2 -10
- package/dist/locales/ko/agents.js +5 -5
- package/dist/locales/zh/agentTranscript.js +2 -10
- package/dist/locales/zh/agents.js +5 -5
- package/dist/machine.d.ts +2 -0
- package/dist/models.d.ts +47 -0
- package/dist/models.js +49 -0
- package/dist/notices.d.ts +2 -1
- package/dist/notices.js +29 -3
- package/dist/pullRequestList.d.ts +52 -0
- package/dist/pullRequestList.js +106 -0
- package/dist/session.d.ts +5 -0
- package/dist/sessionActions.d.ts +105 -0
- package/dist/sessionActions.js +178 -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.js +2 -4
- 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/transcript.d.ts +2 -0
- package/dist/transcript.js +15 -12
- 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/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 +2 -10
- package/locales/en/agents.json +5 -5
- package/locales/es/agentTranscript.json +2 -10
- package/locales/es/agents.json +5 -5
- package/locales/ja/agentTranscript.json +2 -10
- package/locales/ja/agents.json +5 -5
- package/locales/ko/agentTranscript.json +2 -10
- package/locales/ko/agents.json +5 -5
- package/locales/zh/agentTranscript.json +2 -10
- package/locales/zh/agents.json +5 -5
- package/package.json +3 -3
|
@@ -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 };
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { PwCodeApi } from './createPwCodeApi.js';
|
|
2
|
+
import type { PwCodeAddedDirectory, PwCodeAllowedDirectory } from './types.js';
|
|
3
|
+
/** Where a path's last segment starts, at its last separator. */
|
|
4
|
+
export declare const lastSegmentAt: (path: string) => number;
|
|
5
|
+
/** The path's segments, each with the separator that ends it. */
|
|
6
|
+
export declare function pathSegments(path: string): string[];
|
|
7
|
+
/** The monospace `head`, `full` pixels wide, cut to the whole characters and ellipsis that fit in `room`; null when it fits whole. */
|
|
8
|
+
export declare function cutToFit(head: string, full: number, room: number): string | null;
|
|
9
|
+
/** The letter spacing that stretches `cut`, from the monospace `head` `full` pixels wide, across `room`, so the sliver a whole-character cut leaves is spread between its characters rather than left blank beside them; none for a single character. */
|
|
10
|
+
export declare function cutSpacing(head: string, full: number, room: number, cut: string): number;
|
|
11
|
+
/** A path cut as far as it goes: the prefix, an ellipsis for what leads to the last segment, and that segment. */
|
|
12
|
+
export declare function shortestPath(path: string, prefix?: string): string;
|
|
13
|
+
declare const scopes: readonly ['local', 'project', 'user'];
|
|
14
|
+
/** Where pw code saves a directory it adds for every session. */
|
|
15
|
+
export type PwCodeDirectoryScope = (typeof scopes)[number];
|
|
16
|
+
/** A directory saved in settings outlives the session, which cannot remove it. */
|
|
17
|
+
export declare const isSavedDirectory: (dir: PwCodeAddedDirectory) => boolean;
|
|
18
|
+
/** The settings a directory is saved in, of those this package knows. */
|
|
19
|
+
export declare function directoryScopes(dir: PwCodeAddedDirectory): PwCodeDirectoryScope[];
|
|
20
|
+
/** What an approval let the session do in a directory, and whether that
|
|
21
|
+
* reaches below it. */
|
|
22
|
+
export declare function allowedAccess({ edit, subtree, }: PwCodeAllowedDirectory): 'edit' | 'read' | 'editFolder' | 'readFolder';
|
|
23
|
+
/** Removing the directories added for a session only, one request per path at
|
|
24
|
+
* a time. A refusal puts the path back, so it can be tried again. */
|
|
25
|
+
export declare function useRemoveSessionDirectory(api: Pick<PwCodeApi, 'removeSessionDirectory'>, machine: string, sessionId: string): {
|
|
26
|
+
/** The paths whose removal is under way. */
|
|
27
|
+
pending: ReadonlySet<string>;
|
|
28
|
+
/** Why the last removal failed: the machine's refusal, or what was thrown. */
|
|
29
|
+
error: unknown;
|
|
30
|
+
remove: (path: string) => Promise<void>;
|
|
31
|
+
};
|
|
32
|
+
export {};
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { useState } from "react";
|
|
2
|
+
// src/workingDirectories.ts
|
|
3
|
+
/** Where a path's last segment starts, at its last separator. */
|
|
4
|
+
var lastSegmentAt = (path) => Math.max(path.lastIndexOf("/"), path.lastIndexOf("\\"), 0);
|
|
5
|
+
/** The path's segments, each with the separator that ends it. */
|
|
6
|
+
function pathSegments(path) {
|
|
7
|
+
return path.match(/[^/\\]*[/\\]|[^/\\]+$/g) ?? [];
|
|
8
|
+
}
|
|
9
|
+
/** The monospace `head`, `full` pixels wide, cut to the whole characters and ellipsis that fit in `room`; null when it fits whole. */
|
|
10
|
+
function cutToFit(head, full, room) {
|
|
11
|
+
const chars = [...head];
|
|
12
|
+
if (full <= room + .5 || chars.length === 0) return null;
|
|
13
|
+
const fit = Math.floor(room / (full / chars.length) + .01);
|
|
14
|
+
return fit > 0 ? `${chars.slice(0, fit - 1).join("")}…` : "";
|
|
15
|
+
}
|
|
16
|
+
/** The letter spacing that stretches `cut`, from the monospace `head` `full` pixels wide, across `room`, so the sliver a whole-character cut leaves is spread between its characters rather than left blank beside them; none for a single character. */
|
|
17
|
+
function cutSpacing(head, full, room, cut) {
|
|
18
|
+
const chars = [...head].length;
|
|
19
|
+
const shown = [...cut].length;
|
|
20
|
+
if (shown < 2 || chars === 0) return 0;
|
|
21
|
+
return Math.max(room - shown * (full / chars), 0) / (shown - 1);
|
|
22
|
+
}
|
|
23
|
+
/** A path cut as far as it goes: the prefix, an ellipsis for what leads to the last segment, and that segment. */
|
|
24
|
+
function shortestPath(path, prefix = "") {
|
|
25
|
+
const cut = lastSegmentAt(path);
|
|
26
|
+
return `${prefix}${cut > 0 ? "…" : ""}${path.slice(cut)}`;
|
|
27
|
+
}
|
|
28
|
+
var scopes = [
|
|
29
|
+
"local",
|
|
30
|
+
"project",
|
|
31
|
+
"user"
|
|
32
|
+
];
|
|
33
|
+
var isScope = (scope) => scopes.includes(scope);
|
|
34
|
+
/** A directory saved in settings outlives the session, which cannot remove it. */
|
|
35
|
+
var isSavedDirectory = (dir) => (dir.scopes ?? []).length > 0;
|
|
36
|
+
/** The settings a directory is saved in, of those this package knows. */
|
|
37
|
+
function directoryScopes(dir) {
|
|
38
|
+
return (dir.scopes ?? []).filter(isScope);
|
|
39
|
+
}
|
|
40
|
+
/** What an approval let the session do in a directory, and whether that
|
|
41
|
+
* reaches below it. */
|
|
42
|
+
function allowedAccess({ edit, subtree }) {
|
|
43
|
+
if (!subtree) return edit ? "editFolder" : "readFolder";
|
|
44
|
+
return edit ? "edit" : "read";
|
|
45
|
+
}
|
|
46
|
+
/** Removing the directories added for a session only, one request per path at
|
|
47
|
+
* a time. A refusal puts the path back, so it can be tried again. */
|
|
48
|
+
function useRemoveSessionDirectory(api, machine, sessionId) {
|
|
49
|
+
const [pending, setPending] = useState(/* @__PURE__ */ new Set());
|
|
50
|
+
const [error, setError] = useState(null);
|
|
51
|
+
const remove = async (path) => {
|
|
52
|
+
if (pending.has(path)) return;
|
|
53
|
+
setPending((current) => new Set(current).add(path));
|
|
54
|
+
setError(null);
|
|
55
|
+
const refusal = await api.removeSessionDirectory(machine, sessionId, path).catch((thrown) => thrown);
|
|
56
|
+
if (refusal) {
|
|
57
|
+
setError(refusal);
|
|
58
|
+
setPending((current) => {
|
|
59
|
+
const next = new Set(current);
|
|
60
|
+
next.delete(path);
|
|
61
|
+
return next;
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
};
|
|
65
|
+
return {
|
|
66
|
+
pending,
|
|
67
|
+
error,
|
|
68
|
+
remove
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
export { allowedAccess, cutSpacing, cutToFit, directoryScopes, isSavedDirectory, lastSegmentAt, pathSegments, shortestPath, useRemoveSessionDirectory };
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { Schemas } from './apiClient.js';
|
|
2
|
+
import type { PwCodeApi } from './createPwCodeApi.js';
|
|
3
|
+
type Worktree = Schemas['AgentSessionWorktree'];
|
|
4
|
+
/** Some of what removing a worktree would delete: the lines its machine
|
|
5
|
+
* sent, and how many more there are. */
|
|
6
|
+
export interface PwCodeWorktreeLines {
|
|
7
|
+
total: number;
|
|
8
|
+
lines: string[];
|
|
9
|
+
more: number;
|
|
10
|
+
}
|
|
11
|
+
/** What removing a worktree would delete, as git lists it. */
|
|
12
|
+
export interface PwCodeWorktreeWork {
|
|
13
|
+
/** git could not say what the worktree holds. */
|
|
14
|
+
unchecked: boolean;
|
|
15
|
+
/** Files changed or not tracked; null for none. */
|
|
16
|
+
files: PwCodeWorktreeLines | null;
|
|
17
|
+
/** Commits no other branch holds; null for none. */
|
|
18
|
+
commits: PwCodeWorktreeLines | null;
|
|
19
|
+
}
|
|
20
|
+
/** Null when removing the worktree would delete nothing git knows of. */
|
|
21
|
+
export declare function worktreeWork(worktree: Worktree): PwCodeWorktreeWork | null;
|
|
22
|
+
/** The worktree a session works in, for deleting the session, with the
|
|
23
|
+
* choice to remove it too. It is kept unless removing it is chosen;
|
|
24
|
+
* `onChange` hears the worktree's root then, and undefined once it is kept. */
|
|
25
|
+
export declare function useWorktreeChoice(api: Pick<PwCodeApi, 'useSessionWorktree'>, session: Schemas['AgentSession'] | undefined, onChange: (removeRoot: string | undefined) => void): {
|
|
26
|
+
/** The worktree, while the session works in one. */
|
|
27
|
+
worktree: Worktree | undefined;
|
|
28
|
+
remove: boolean;
|
|
29
|
+
choose: (remove: boolean) => void;
|
|
30
|
+
};
|
|
31
|
+
export {};
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { useState } from "react";
|
|
2
|
+
// src/worktreeChoice.ts
|
|
3
|
+
function workLines(lines, total) {
|
|
4
|
+
if (total === 0) return null;
|
|
5
|
+
const shown = lines ?? [];
|
|
6
|
+
return {
|
|
7
|
+
total,
|
|
8
|
+
lines: shown,
|
|
9
|
+
more: Math.max(total - shown.length, 0)
|
|
10
|
+
};
|
|
11
|
+
}
|
|
12
|
+
/** Null when removing the worktree would delete nothing git knows of. */
|
|
13
|
+
function worktreeWork(worktree) {
|
|
14
|
+
const { fileCount = 0, commitCount = 0, unchecked } = worktree;
|
|
15
|
+
if (!unchecked && fileCount === 0 && commitCount === 0) return null;
|
|
16
|
+
return {
|
|
17
|
+
unchecked: !!unchecked,
|
|
18
|
+
files: workLines(worktree.files, fileCount),
|
|
19
|
+
commits: workLines(worktree.commits, commitCount)
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
/** The worktree a session works in, for deleting the session, with the
|
|
23
|
+
* choice to remove it too. It is kept unless removing it is chosen;
|
|
24
|
+
* `onChange` hears the worktree's root then, and undefined once it is kept. */
|
|
25
|
+
function useWorktreeChoice(api, session, onChange) {
|
|
26
|
+
const { data } = api.useSessionWorktree(session);
|
|
27
|
+
const [remove, setRemove] = useState(false);
|
|
28
|
+
const root = data?.root;
|
|
29
|
+
return {
|
|
30
|
+
worktree: root ? data : void 0,
|
|
31
|
+
remove,
|
|
32
|
+
choose: (next) => {
|
|
33
|
+
setRemove(next);
|
|
34
|
+
onChange(next ? root : void 0);
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
export { useWorktreeChoice, worktreeWork };
|