@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,271 @@
|
|
|
1
|
+
import { isConnected, isMachineDown } from "./machine.js";
|
|
2
|
+
import { usePwCodeTranslations } from "./messages.js";
|
|
3
|
+
import { pullRequestLabel } from "./pullRequest.js";
|
|
4
|
+
import { isActiveSession, isArchived, isSession, needsYou, newestFirst, sessionKey, sessionVariant, spansMachines } from "./session.js";
|
|
5
|
+
import { menuCommand } from "./sessionActions.js";
|
|
6
|
+
import { groupOf, inFlightFirst, isWorking, listGroups } from "./sessionGroups.js";
|
|
7
|
+
import { sessionLabel, sessionStateText } from "./wording.js";
|
|
8
|
+
import { useEffect, useMemo, useRef } from "react";
|
|
9
|
+
// src/sessionList.ts
|
|
10
|
+
var PINNED = { pinned: true };
|
|
11
|
+
var UNGROUPED = { groupId: null };
|
|
12
|
+
var ARCHIVED = { archived: true };
|
|
13
|
+
var groupSectionKey = (id) => `group:${id}`;
|
|
14
|
+
function archiveSection(sessions, t) {
|
|
15
|
+
return {
|
|
16
|
+
key: "archived",
|
|
17
|
+
kind: "archived",
|
|
18
|
+
label: t("sidebar.groupArchived"),
|
|
19
|
+
sessions,
|
|
20
|
+
target: ARCHIVED
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
/** A reader scanning for what needs them, then what is live, should not have
|
|
24
|
+
* to read every mark. */
|
|
25
|
+
function statusSections(sessions, waiting, t) {
|
|
26
|
+
const needsYou = new Set(waiting);
|
|
27
|
+
const rest = sessions.filter((s) => !needsYou.has(s) && !isArchived(s));
|
|
28
|
+
return [
|
|
29
|
+
{
|
|
30
|
+
key: "needsYou",
|
|
31
|
+
kind: "needsYou",
|
|
32
|
+
label: t("sidebar.groupNeedsYou"),
|
|
33
|
+
sessions: waiting,
|
|
34
|
+
target: UNGROUPED
|
|
35
|
+
},
|
|
36
|
+
{
|
|
37
|
+
key: "active",
|
|
38
|
+
kind: "active",
|
|
39
|
+
label: t("sidebar.groupActive"),
|
|
40
|
+
sessions: rest.filter(isActiveSession),
|
|
41
|
+
target: UNGROUPED
|
|
42
|
+
},
|
|
43
|
+
{
|
|
44
|
+
key: "inactive",
|
|
45
|
+
kind: "inactive",
|
|
46
|
+
label: t("sidebar.groupInactive"),
|
|
47
|
+
sessions: rest.filter((s) => !isActiveSession(s)),
|
|
48
|
+
target: UNGROUPED
|
|
49
|
+
},
|
|
50
|
+
archiveSection(sessions.filter(isArchived), t)
|
|
51
|
+
].filter((g) => g.sessions.length > 0);
|
|
52
|
+
}
|
|
53
|
+
/** The sections a list of sessions reads in: the pinned ones, each of the
|
|
54
|
+
* reader's groups, then the rest by whether they need the reader, are live,
|
|
55
|
+
* are at rest or are archived. A pinned session is listed with the pinned
|
|
56
|
+
* ones alone; it keeps its group and goes back to it once it is unpinned. */
|
|
57
|
+
function sessionSections(sessions, groups, machineById, t) {
|
|
58
|
+
const visible = newestFirst(sessions);
|
|
59
|
+
const waiting = sessions.filter((s) => needsYou(s, machineById.get(s.machineId)));
|
|
60
|
+
const isPinned = (s) => !!s.pinnedAt && !isArchived(s);
|
|
61
|
+
const pinned = inFlightFirst(visible.filter(isPinned), machineById);
|
|
62
|
+
const { listed, ungrouped } = listGroups(groups, visible.filter((s) => !isPinned(s)), machineById);
|
|
63
|
+
return [
|
|
64
|
+
...pinned.sessions.length > 0 ? [{
|
|
65
|
+
key: "pinned",
|
|
66
|
+
kind: "pinned",
|
|
67
|
+
label: t("sidebar.pinned"),
|
|
68
|
+
sessions: pinned.sessions,
|
|
69
|
+
target: PINNED,
|
|
70
|
+
rollup: {
|
|
71
|
+
waiting: pinned.waiting,
|
|
72
|
+
running: pinned.running
|
|
73
|
+
}
|
|
74
|
+
}] : [],
|
|
75
|
+
...(visible.length > 0 ? listed : []).map((l) => ({
|
|
76
|
+
key: groupSectionKey(l.group.id),
|
|
77
|
+
kind: "group",
|
|
78
|
+
label: l.group.name,
|
|
79
|
+
sessions: l.sessions,
|
|
80
|
+
target: { groupId: l.group.id },
|
|
81
|
+
rollup: {
|
|
82
|
+
waiting: l.waiting,
|
|
83
|
+
running: l.running
|
|
84
|
+
},
|
|
85
|
+
group: l.group
|
|
86
|
+
})),
|
|
87
|
+
...statusSections(ungrouped, waiting.filter((s) => ungrouped.includes(s)), t)
|
|
88
|
+
];
|
|
89
|
+
}
|
|
90
|
+
/** The archive is listed only while it holds a session, so a dragged one
|
|
91
|
+
* would otherwise have nowhere to be dropped until the first is archived. */
|
|
92
|
+
function withArchiveSection(sections, t) {
|
|
93
|
+
return sections.some((s) => s.kind === "archived") ? sections : [...sections, archiveSection([], t)];
|
|
94
|
+
}
|
|
95
|
+
function isUngroupedTarget(target) {
|
|
96
|
+
return "groupId" in target && target.groupId === null;
|
|
97
|
+
}
|
|
98
|
+
/** The sessions a search lists: archived ones stay out until a search reaches for them. */
|
|
99
|
+
function searchedSessions(sessions, search) {
|
|
100
|
+
const current = sessions.filter((s) => !isArchived(s));
|
|
101
|
+
return search ? [...current, ...sessions.filter(isArchived)] : current;
|
|
102
|
+
}
|
|
103
|
+
/** What a search finds a session by: `value` its label, machine, directory,
|
|
104
|
+
* id and pull requests, and `keywords` the name it was given or gave itself. */
|
|
105
|
+
function sessionSearchText(session, machine, t) {
|
|
106
|
+
const pullRequests = (session.pullRequests ?? []).flatMap((url) => pullRequestLabel(url) ?? []);
|
|
107
|
+
return {
|
|
108
|
+
value: `${sessionLabel(session, t)} ${machine?.name ?? ""} ${session.workspace ?? ""} ${session.id} ${pullRequests.join(" ")}`,
|
|
109
|
+
keywords: [session.name || session.autoName || session.preview || ""]
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
/** A second line costs a row its height, so the state takes one only when the
|
|
113
|
+
* mark cannot express it: not "idle" or "running", but "waiting on you" and a
|
|
114
|
+
* dead machine. */
|
|
115
|
+
function sessionRowState(session, machine, t) {
|
|
116
|
+
const degraded = isMachineDown(machine);
|
|
117
|
+
return {
|
|
118
|
+
stateText: sessionStateText(session, machine, t),
|
|
119
|
+
showSecondLine: degraded || session.status !== "idle" && session.status !== "running",
|
|
120
|
+
live: isActiveSession(session) && !degraded
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
/** A pinned session is under the pinned ones' heading, which says so; only an
|
|
124
|
+
* archived one, listed with the archive, needs the mark. */
|
|
125
|
+
function showsPinMark(session) {
|
|
126
|
+
return !!session.pinnedAt && isArchived(session);
|
|
127
|
+
}
|
|
128
|
+
/** A session at work shows a turning mark, which tells it from one resting
|
|
129
|
+
* between turns by its shape and so holds with reduced motion. Any other
|
|
130
|
+
* shows a dot in its state's tone, pulsing while it waits on the reader. */
|
|
131
|
+
function sessionMark(session, machine) {
|
|
132
|
+
if (isWorking(session, machine)) return { working: true };
|
|
133
|
+
return {
|
|
134
|
+
working: false,
|
|
135
|
+
tone: sessionVariant(session.status, machine, "success"),
|
|
136
|
+
pulse: session.status === "awaiting_approval"
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
/** The server stores nothing, so a dropped machine contributes no sessions to
|
|
140
|
+
* the next poll; without this its sessions vanish instead of explaining themselves. */
|
|
141
|
+
function useRetainedSessions(machines, sessions) {
|
|
142
|
+
const lastSeen = useRef(/* @__PURE__ */ new Map());
|
|
143
|
+
useEffect(() => {
|
|
144
|
+
const byMachine = /* @__PURE__ */ new Map();
|
|
145
|
+
for (const s of sessions) {
|
|
146
|
+
const found = byMachine.get(s.machineId);
|
|
147
|
+
if (found) found.push(s);
|
|
148
|
+
else byMachine.set(s.machineId, [s]);
|
|
149
|
+
}
|
|
150
|
+
const known = new Set(machines.map((m) => m.id));
|
|
151
|
+
for (const id of [...lastSeen.current.keys()]) if (!known.has(id)) lastSeen.current.delete(id);
|
|
152
|
+
for (const machine of machines) if (isConnected(machine)) lastSeen.current.set(machine.id, byMachine.get(machine.id) ?? []);
|
|
153
|
+
}, [machines, sessions]);
|
|
154
|
+
return useMemo(() => {
|
|
155
|
+
const retained = [];
|
|
156
|
+
for (const machine of machines) if (!isConnected(machine)) retained.push(...lastSeen.current.get(machine.id) ?? []);
|
|
157
|
+
return retained.length > 0 ? [...sessions, ...retained] : sessions;
|
|
158
|
+
}, [machines, sessions]);
|
|
159
|
+
}
|
|
160
|
+
/** The sessions as a list shows them, in sections, with what the reader can
|
|
161
|
+
* do to a row, a group or the list. */
|
|
162
|
+
function useSessionList({ api, machineById, groups, folded: [storedFolded, setFolded], open, actions, newSession, promptGroupName, confirmGroupDelete, onBlocked }) {
|
|
163
|
+
const t = usePwCodeTranslations("agents");
|
|
164
|
+
const { data, error, isLoading } = api.useSessions();
|
|
165
|
+
const sessions = useRetainedSessions(useMemo(() => data?.machines ?? [], [data]), useMemo(() => data?.sessions ?? [], [data]));
|
|
166
|
+
const folded = Array.isArray(storedFolded) ? storedFolded : [];
|
|
167
|
+
const sections = sessionSections(sessions, groups.groups, machineById, t);
|
|
168
|
+
const listedSessions = sections.flatMap((s) => s.sessions);
|
|
169
|
+
const openSession = listedSessions.find((s) => isSession(s, open.machine, open.sessionId));
|
|
170
|
+
const loading = isLoading && !data;
|
|
171
|
+
const newGroup = () => {
|
|
172
|
+
const group = groups.create(t("sidebar.newGroupName"));
|
|
173
|
+
promptGroupName(group);
|
|
174
|
+
return group;
|
|
175
|
+
};
|
|
176
|
+
const deleteGroup = (group) => {
|
|
177
|
+
groups.remove(group.id);
|
|
178
|
+
const key = groupSectionKey(group.id);
|
|
179
|
+
if (folded.includes(key)) setFolded(folded.filter((k) => k !== key));
|
|
180
|
+
for (const s of sessions) if (s.group === group.id && !actions.groupBlocked(s)) actions.moveToGroup(s, null);
|
|
181
|
+
};
|
|
182
|
+
const dropOnArchive = (session) => {
|
|
183
|
+
const blocked = actions.archiveBlocked(session);
|
|
184
|
+
if (blocked) {
|
|
185
|
+
onBlocked(blocked);
|
|
186
|
+
return;
|
|
187
|
+
}
|
|
188
|
+
actions.requestArchive(session);
|
|
189
|
+
};
|
|
190
|
+
const dropSession = (key, target) => {
|
|
191
|
+
const session = sessions.find((s) => sessionKey(s) === key);
|
|
192
|
+
if (!session) return;
|
|
193
|
+
if ("archived" in target) {
|
|
194
|
+
dropOnArchive(session);
|
|
195
|
+
return;
|
|
196
|
+
}
|
|
197
|
+
const pin = "pinned" in target;
|
|
198
|
+
if (pin !== !!session.pinnedAt) actions.togglePin(session);
|
|
199
|
+
if (!pin && (groupOf(groups.groups, session)?.id ?? null) !== target.groupId) {
|
|
200
|
+
const blocked = actions.groupBlocked(session);
|
|
201
|
+
if (blocked) {
|
|
202
|
+
onBlocked(blocked);
|
|
203
|
+
return;
|
|
204
|
+
}
|
|
205
|
+
actions.moveToGroup(session, target.groupId);
|
|
206
|
+
}
|
|
207
|
+
};
|
|
208
|
+
const menu = (session, row) => {
|
|
209
|
+
const items = actions.menu(session, row);
|
|
210
|
+
if (isArchived(session)) return items;
|
|
211
|
+
const current = groupOf(groups.groups, session);
|
|
212
|
+
const blocked = actions.groupBlocked(session);
|
|
213
|
+
const choices = [
|
|
214
|
+
...groups.groups.map((g) => ({
|
|
215
|
+
action: "joinGroup",
|
|
216
|
+
label: g.name,
|
|
217
|
+
checked: g === current,
|
|
218
|
+
run: () => actions.moveToGroup(session, g === current ? null : g.id)
|
|
219
|
+
})),
|
|
220
|
+
...groups.groups.length > 0 ? ["divider"] : [],
|
|
221
|
+
menuCommand("moveToNewGroup", t("sidebar.newGroup"), () => actions.moveToGroup(session, newGroup().id))
|
|
222
|
+
];
|
|
223
|
+
const move = {
|
|
224
|
+
action: "moveToGroup",
|
|
225
|
+
label: t("sidebar.moveToGroup"),
|
|
226
|
+
...blocked ? { disabledReason: blocked } : {},
|
|
227
|
+
items: choices
|
|
228
|
+
};
|
|
229
|
+
const at = items.lastIndexOf("divider");
|
|
230
|
+
return at < 0 ? [...items, move] : [
|
|
231
|
+
...items.slice(0, at),
|
|
232
|
+
move,
|
|
233
|
+
...items.slice(at)
|
|
234
|
+
];
|
|
235
|
+
};
|
|
236
|
+
return {
|
|
237
|
+
data,
|
|
238
|
+
error,
|
|
239
|
+
loading,
|
|
240
|
+
sessions,
|
|
241
|
+
sections,
|
|
242
|
+
searchable: loading || sections.length > 0,
|
|
243
|
+
multiMachine: spansMachines(sessions),
|
|
244
|
+
openSession,
|
|
245
|
+
rail: listedSessions.filter((s) => isActiveSession(s) && isConnected(machineById.get(s.machineId)) || s === openSession),
|
|
246
|
+
folded,
|
|
247
|
+
toggleFold: (key) => setFolded(folded.includes(key) ? folded.filter((k) => k !== key) : [...folded, key]),
|
|
248
|
+
holdsOpen: (section) => folded.includes(section.key) && !!openSession && section.sessions.includes(openSession),
|
|
249
|
+
dropSession,
|
|
250
|
+
newGroup,
|
|
251
|
+
renameGroup: (group, name) => groups.patch(group.id, { name }),
|
|
252
|
+
deleteGroup,
|
|
253
|
+
menu,
|
|
254
|
+
groupMenu: (group) => [
|
|
255
|
+
menuCommand("renameGroup", t("sidebar.renameGroup"), () => promptGroupName(group)),
|
|
256
|
+
"divider",
|
|
257
|
+
{
|
|
258
|
+
...menuCommand("deleteGroup", t("sidebar.deleteGroup"), () => confirmGroupDelete(group)),
|
|
259
|
+
destructive: true
|
|
260
|
+
}
|
|
261
|
+
],
|
|
262
|
+
listMenu: [menuCommand("newSession", t("sidebar.newSession"), newSession), ...sessions.length > 0 ? [menuCommand("newGroup", t("sidebar.newGroup"), () => {
|
|
263
|
+
newGroup();
|
|
264
|
+
})] : []],
|
|
265
|
+
shortcuts: (session) => [...actions.shortcuts(session), {
|
|
266
|
+
keys: "C",
|
|
267
|
+
run: newSession
|
|
268
|
+
}]
|
|
269
|
+
};
|
|
270
|
+
}
|
|
271
|
+
export { groupSectionKey, isUngroupedTarget, searchedSessions, sessionMark, sessionRowState, sessionSearchText, sessionSections, showsPinMark, useRetainedSessions, useSessionList, withArchiveSection };
|
package/dist/sessionsApi.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { bytesToBase64 } from "./base64.js";
|
|
1
2
|
import { createQuery } from "./query.js";
|
|
2
3
|
import { machinesById } from "./machine.js";
|
|
3
4
|
import { isSession, triageSessions } from "./session.js";
|
|
@@ -128,10 +129,7 @@ function pasteRefs(pastes) {
|
|
|
128
129
|
})) } : {};
|
|
129
130
|
}
|
|
130
131
|
function utf8ToBase64(text) {
|
|
131
|
-
|
|
132
|
-
let binary = "";
|
|
133
|
-
for (let i = 0; i < bytes.length; i += 32768) binary += String.fromCharCode(...bytes.subarray(i, i + 32768));
|
|
134
|
-
return btoa(binary);
|
|
132
|
+
return bytesToBase64(new TextEncoder().encode(text));
|
|
135
133
|
}
|
|
136
134
|
function sameSession(a, b) {
|
|
137
135
|
return isSession(a, b.machineId, b.id);
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import type { BoundSetting, PermissionMode } from '@parallelworks/ui/ai/headless';
|
|
2
|
+
import type { PwCodeClient, PwCodeResult, Schemas } from './apiClient.js';
|
|
3
|
+
type SettingsBody = Schemas['AgentSessionSettingsBody'];
|
|
4
|
+
export interface PwCodeSessionSettingsOptions {
|
|
5
|
+
machine: string;
|
|
6
|
+
sessionId: string;
|
|
7
|
+
model: string | undefined;
|
|
8
|
+
effort?: string | undefined;
|
|
9
|
+
allocation: string | undefined;
|
|
10
|
+
claudeAccount?: string | undefined;
|
|
11
|
+
/** Billed by a switch to a model the platform bills, while the session has had no allocation of its own. */
|
|
12
|
+
nativeAllocation?: string | null | undefined;
|
|
13
|
+
locked: boolean;
|
|
14
|
+
live: boolean;
|
|
15
|
+
onPatched: () => void;
|
|
16
|
+
onError: (error: unknown) => void;
|
|
17
|
+
/** Called as a mode is picked, before the machine answers. */
|
|
18
|
+
onModePicked?: ((mode: PermissionMode) => void) | undefined;
|
|
19
|
+
/** Called as an effort level is picked, before the machine answers. */
|
|
20
|
+
onEffortPicked?: ((effort: string) => void) | undefined;
|
|
21
|
+
/** Called once the machine takes an account; it refuses one it cannot use, such as one signed out. */
|
|
22
|
+
onAccountSaved?: ((account: string) => void) | undefined;
|
|
23
|
+
}
|
|
24
|
+
export interface PwCodeSessionSettings {
|
|
25
|
+
model: BoundSetting;
|
|
26
|
+
effort: BoundSetting;
|
|
27
|
+
allocation: BoundSetting;
|
|
28
|
+
account: BoundSetting;
|
|
29
|
+
setMode: (mode: PermissionMode) => void;
|
|
30
|
+
saving: boolean;
|
|
31
|
+
}
|
|
32
|
+
export declare function settingsApi(client: PwCodeClient): {
|
|
33
|
+
updateSessionSettings: (machine: string, id: string, body: SettingsBody) => Promise<PwCodeResult<Schemas['SessionState']>>;
|
|
34
|
+
useSessionSettings: ({ machine, sessionId, model, effort, allocation, claudeAccount, nativeAllocation, locked, live, onPatched, onError, onModePicked, onEffortPicked, onAccountSaved, }: PwCodeSessionSettingsOptions) => PwCodeSessionSettings;
|
|
35
|
+
};
|
|
36
|
+
export {};
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
import { useCallback, useRef, useState } from "react";
|
|
2
|
+
// src/settingsApi.ts
|
|
3
|
+
var isClaude = (model) => !!model?.startsWith("claude-code/");
|
|
4
|
+
function settingsApi(client) {
|
|
5
|
+
/** Changes what a live session runs as, answering with its new state. */
|
|
6
|
+
function updateSessionSettings(machine, id, body) {
|
|
7
|
+
return client.PATCH("/api/code/machines/{machine}/sessions/{id}/settings", {
|
|
8
|
+
params: { path: {
|
|
9
|
+
machine,
|
|
10
|
+
id
|
|
11
|
+
} },
|
|
12
|
+
body
|
|
13
|
+
});
|
|
14
|
+
}
|
|
15
|
+
/** The session's own model, effort, allocation, account and permission
|
|
16
|
+
* mode, for pickers that show and change them. A pick shows at once and
|
|
17
|
+
* goes back if the machine refuses it. A session that cannot take a change
|
|
18
|
+
* (not live yet, or unreachable) shows its pickers disabled rather than
|
|
19
|
+
* dropping a pick. */
|
|
20
|
+
function useSessionSettings({ machine, sessionId, model, effort, allocation, claudeAccount, nativeAllocation, locked, live, onPatched, onError, onModePicked, onEffortPicked, onAccountSaved }) {
|
|
21
|
+
const [inFlight, setInFlight] = useState(0);
|
|
22
|
+
const saving = inFlight > 0;
|
|
23
|
+
const platformAllocation = useRef(allocation || "");
|
|
24
|
+
if (!isClaude(model) && allocation) platformAllocation.current = allocation;
|
|
25
|
+
const [picked, setPicked] = useState(null);
|
|
26
|
+
const reported = [
|
|
27
|
+
model,
|
|
28
|
+
effort,
|
|
29
|
+
allocation,
|
|
30
|
+
claudeAccount
|
|
31
|
+
].join("|");
|
|
32
|
+
const pick = picked?.over === reported ? picked : null;
|
|
33
|
+
const pickNext = (change) => setPicked((previous) => ({
|
|
34
|
+
...previous?.over === reported ? previous : {},
|
|
35
|
+
...change,
|
|
36
|
+
over: reported
|
|
37
|
+
}));
|
|
38
|
+
const patch = useCallback(async (body) => {
|
|
39
|
+
setInFlight((count) => count + 1);
|
|
40
|
+
let error;
|
|
41
|
+
try {
|
|
42
|
+
error = (await updateSessionSettings(machine, sessionId, body)).error;
|
|
43
|
+
} catch (thrown) {
|
|
44
|
+
error = thrown;
|
|
45
|
+
}
|
|
46
|
+
setInFlight((count) => count - 1);
|
|
47
|
+
if (error) {
|
|
48
|
+
setPicked(null);
|
|
49
|
+
onError(error);
|
|
50
|
+
return false;
|
|
51
|
+
}
|
|
52
|
+
onPatched();
|
|
53
|
+
return true;
|
|
54
|
+
}, [
|
|
55
|
+
machine,
|
|
56
|
+
sessionId,
|
|
57
|
+
onError,
|
|
58
|
+
onPatched
|
|
59
|
+
]);
|
|
60
|
+
const shownModel = pick?.model ?? model;
|
|
61
|
+
const shownAllocation = pick?.allocation ?? allocation;
|
|
62
|
+
const disabled = !live || locked || saving;
|
|
63
|
+
const modelSetting = {
|
|
64
|
+
value: shownModel,
|
|
65
|
+
disabled,
|
|
66
|
+
onChange: (next) => {
|
|
67
|
+
const nextAllocation = isClaude(next) ? "" : shownAllocation || platformAllocation.current || nativeAllocation || "";
|
|
68
|
+
pickNext({
|
|
69
|
+
model: next,
|
|
70
|
+
allocation: nextAllocation
|
|
71
|
+
});
|
|
72
|
+
patch({
|
|
73
|
+
model: next,
|
|
74
|
+
...nextAllocation || isClaude(next) ? { allocation: nextAllocation } : {}
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
const allocationSetting = {
|
|
79
|
+
value: shownAllocation,
|
|
80
|
+
disabled,
|
|
81
|
+
onChange: (next) => {
|
|
82
|
+
if (!shownModel) return;
|
|
83
|
+
pickNext({
|
|
84
|
+
model: shownModel,
|
|
85
|
+
allocation: next
|
|
86
|
+
});
|
|
87
|
+
patch({
|
|
88
|
+
model: shownModel,
|
|
89
|
+
allocation: next
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
};
|
|
93
|
+
const effortQueue = useRef({
|
|
94
|
+
sending: false,
|
|
95
|
+
next: null
|
|
96
|
+
});
|
|
97
|
+
const [sendingEffort, setSendingEffort] = useState(null);
|
|
98
|
+
const sendEffort = async (level) => {
|
|
99
|
+
const queue = effortQueue.current;
|
|
100
|
+
queue.next = level;
|
|
101
|
+
if (queue.sending) return;
|
|
102
|
+
queue.sending = true;
|
|
103
|
+
try {
|
|
104
|
+
while (queue.next !== null) {
|
|
105
|
+
const sending = queue.next;
|
|
106
|
+
queue.next = null;
|
|
107
|
+
await patch({ effort: sending });
|
|
108
|
+
}
|
|
109
|
+
} finally {
|
|
110
|
+
queue.sending = false;
|
|
111
|
+
setSendingEffort(null);
|
|
112
|
+
}
|
|
113
|
+
};
|
|
114
|
+
const effortSetting = {
|
|
115
|
+
value: sendingEffort ?? pick?.effort ?? effort ?? "",
|
|
116
|
+
disabled: !live || locked,
|
|
117
|
+
onChange: (next) => {
|
|
118
|
+
pickNext({ effort: next });
|
|
119
|
+
setSendingEffort(next);
|
|
120
|
+
onEffortPicked?.(next);
|
|
121
|
+
sendEffort(next);
|
|
122
|
+
}
|
|
123
|
+
};
|
|
124
|
+
const setMode = useCallback((mode) => {
|
|
125
|
+
onModePicked?.(mode);
|
|
126
|
+
patch({ permissionMode: mode });
|
|
127
|
+
}, [patch, onModePicked]);
|
|
128
|
+
return {
|
|
129
|
+
model: modelSetting,
|
|
130
|
+
effort: effortSetting,
|
|
131
|
+
allocation: allocationSetting,
|
|
132
|
+
account: {
|
|
133
|
+
value: pick?.claudeAccount ?? claudeAccount,
|
|
134
|
+
disabled,
|
|
135
|
+
onChange: (next) => {
|
|
136
|
+
pickNext({ claudeAccount: next });
|
|
137
|
+
patch({ claudeAccount: next }).then((saved) => {
|
|
138
|
+
if (saved) onAccountSaved?.(next);
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
},
|
|
142
|
+
setMode,
|
|
143
|
+
saving
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
return {
|
|
147
|
+
updateSessionSettings,
|
|
148
|
+
useSessionSettings
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
export { settingsApi };
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import { type EffortLevel, type PermissionMode } from '@parallelworks/ui/ai/headless';
|
|
2
|
+
import type { PwCodeApi } from './createPwCodeApi.js';
|
|
3
|
+
import { type PwCodeMachine } from './machine.js';
|
|
4
|
+
import { type PwCodeSession, type RecentDirectory } from './session.js';
|
|
5
|
+
import type { PwCodeImageRef } from './types.js';
|
|
6
|
+
/** A machine as far as starting a session on it reads it. */
|
|
7
|
+
export interface PwCodeStartMachine extends PwCodeMachine {
|
|
8
|
+
/** The widest mode its owner lets a session started from another client run in. */
|
|
9
|
+
remoteMaxPermissionMode?: string | undefined;
|
|
10
|
+
/** The modes its pw code takes; one that lists none predates auto. */
|
|
11
|
+
permissionModes?: readonly string[] | null | undefined;
|
|
12
|
+
claudeInstalled?: boolean | undefined;
|
|
13
|
+
claudeCodeAllowed?: boolean | undefined;
|
|
14
|
+
}
|
|
15
|
+
/** The machines a session can start on, then those listed but not answering,
|
|
16
|
+
* each with computers before resources. A resource with no agent on it is
|
|
17
|
+
* one the reader has not touched, so it is not counted as down. */
|
|
18
|
+
export declare function startMachines<M extends PwCodeStartMachine>(machines: readonly M[]): {
|
|
19
|
+
startable: M[];
|
|
20
|
+
down: M[];
|
|
21
|
+
};
|
|
22
|
+
/** clampPermissionMode falls to a machine's widest mode, which would turn auto
|
|
23
|
+
* into bypass on a machine too old for auto; capping at the mode itself first
|
|
24
|
+
* keeps the result from ever being wider than what was asked for. */
|
|
25
|
+
export declare function startingMode(mode: PermissionMode, machine: Pick<PwCodeStartMachine, 'remoteMaxPermissionMode' | 'permissionModes'> | undefined): PermissionMode;
|
|
26
|
+
/** Where a session starts: the machine, and the directory on it. */
|
|
27
|
+
export interface PwCodeStartLocation<M> {
|
|
28
|
+
/** The machines a session can start on, computers first. */
|
|
29
|
+
startable: M[];
|
|
30
|
+
/** The machines listed but not answering, computers first, so a missing one can be traced. */
|
|
31
|
+
down: M[];
|
|
32
|
+
/** The machine picked, or the only one a session can start on. */
|
|
33
|
+
machine: M | undefined;
|
|
34
|
+
pickMachine: (id: string) => void;
|
|
35
|
+
/** The directories the machine's sessions worked in, newest first. */
|
|
36
|
+
directories: RecentDirectory[];
|
|
37
|
+
/** The directory picked or typed, or else the newest one. */
|
|
38
|
+
directory: string;
|
|
39
|
+
/** The directory is typed rather than picked from the recent ones. */
|
|
40
|
+
typing: boolean;
|
|
41
|
+
pickDirectory: (path: string) => void;
|
|
42
|
+
typeDirectory: (path: string) => void;
|
|
43
|
+
/** Turns to typing a directory, keeping what was typed so far. */
|
|
44
|
+
typeOtherDirectory: () => void;
|
|
45
|
+
}
|
|
46
|
+
export declare function useStartLocation<M extends PwCodeStartMachine>(machines: readonly M[], sessions: readonly Pick<PwCodeSession, 'machineId' | 'workspace' | 'updatedAt'>[]): PwCodeStartLocation<M>;
|
|
47
|
+
/** The mode a session starts in: the one saved, or one picked on this visit,
|
|
48
|
+
* within what the machine allows. A pick holds until the visit ends, so one
|
|
49
|
+
* the host declines to save, such as bypass, still applies to this start. */
|
|
50
|
+
export declare function useStartPermissionMode(preference: {
|
|
51
|
+
saved: PermissionMode;
|
|
52
|
+
remember: (mode: PermissionMode) => void;
|
|
53
|
+
}, machine: Pick<PwCodeStartMachine, 'remoteMaxPermissionMode' | 'permissionModes'> | undefined): {
|
|
54
|
+
mode: PermissionMode;
|
|
55
|
+
pick: (mode: PermissionMode) => void;
|
|
56
|
+
};
|
|
57
|
+
type ClaudeMachine = Pick<PwCodeStartMachine, 'id' | 'claudeInstalled' | 'claudeCodeAllowed'>;
|
|
58
|
+
type StartApi = Pick<PwCodeApi, 'useClaudeModels' | 'useClaudeAccountNames' | 'createSession' | 'sendMessage' | 'useSessions'>;
|
|
59
|
+
/** A model last picked for a session, and the allocation that billed it. */
|
|
60
|
+
export interface PwCodeLastModel {
|
|
61
|
+
model?: string | undefined;
|
|
62
|
+
allocation?: string | undefined;
|
|
63
|
+
}
|
|
64
|
+
/** The model, allocation and Claude Code account a session starts on. */
|
|
65
|
+
export interface PwCodeStartModel {
|
|
66
|
+
model: string | null | undefined;
|
|
67
|
+
allocation: string | null | undefined;
|
|
68
|
+
account: string | undefined;
|
|
69
|
+
/** The account picked on this visit, rather than one remembered. */
|
|
70
|
+
pickedAccount: string | undefined;
|
|
71
|
+
pickModel: (model: string) => void;
|
|
72
|
+
pickAllocation: (allocation: string) => void;
|
|
73
|
+
/** Picks the account and remembers it for the machine. */
|
|
74
|
+
pickAccount: (account: string) => void;
|
|
75
|
+
}
|
|
76
|
+
/** Picks hold for the machine they were made on. Until then a session starts
|
|
77
|
+
* on the model last picked where the machine and the platform still offer
|
|
78
|
+
* it, else on the host's selected one, and on the account saved for the
|
|
79
|
+
* machine where the machine still lists it. */
|
|
80
|
+
export declare function useStartModel(api: Pick<StartApi, 'useClaudeModels' | 'useClaudeAccountNames'>, { machine, platform, last, account: accountPreference, handedOff, }: {
|
|
81
|
+
machine: ClaudeMachine | undefined;
|
|
82
|
+
platform: {
|
|
83
|
+
/** The platform's models: undefined while they load, or when they failed to. */
|
|
84
|
+
models: readonly {
|
|
85
|
+
id: string;
|
|
86
|
+
}[] | undefined;
|
|
87
|
+
/** The host's selected model and allocation. */
|
|
88
|
+
model: string | null | undefined;
|
|
89
|
+
allocation: string | null | undefined;
|
|
90
|
+
};
|
|
91
|
+
last: PwCodeLastModel;
|
|
92
|
+
/** The Claude Code account saved for this machine. */
|
|
93
|
+
account: {
|
|
94
|
+
saved: string | undefined;
|
|
95
|
+
remember: (account: string) => void;
|
|
96
|
+
};
|
|
97
|
+
/** A chat handed off to the session, which keeps the host's selected model. */
|
|
98
|
+
handedOff: boolean;
|
|
99
|
+
}): PwCodeStartModel;
|
|
100
|
+
/** Images that wait to go with a session's first prompt. */
|
|
101
|
+
export interface PwCodeStartImages {
|
|
102
|
+
count: number;
|
|
103
|
+
/** Saves them with the session; null when one could not be saved, which
|
|
104
|
+
* the host reports. */
|
|
105
|
+
saveTo: (machine: string, sessionId: string) => Promise<PwCodeImageRef[] | null>;
|
|
106
|
+
}
|
|
107
|
+
export interface PwCodeStartSession {
|
|
108
|
+
/** A machine and an absolute directory are chosen, and nothing holds the start back. */
|
|
109
|
+
ready: boolean;
|
|
110
|
+
starting: boolean;
|
|
111
|
+
/** Why the last start failed: the machine's refusal, or what was thrown. */
|
|
112
|
+
error: unknown;
|
|
113
|
+
/** Starts the session with `prompt` as its first message. False hands the
|
|
114
|
+
* prompt back to the composer. */
|
|
115
|
+
start: (prompt: string) => Promise<boolean>;
|
|
116
|
+
}
|
|
117
|
+
/** Starts a session, reads the listing again so it shows there, and hands it
|
|
118
|
+
* to `onStarted` once its first prompt is sent. With images, the session is
|
|
119
|
+
* created first and they are saved with it; a retry after they fail reuses
|
|
120
|
+
* that session while nothing it started with has changed. */
|
|
121
|
+
export declare function useStartSession(api: Pick<StartApi, 'createSession' | 'sendMessage' | 'useSessions'>, options: {
|
|
122
|
+
machine: Pick<PwCodeStartMachine, 'id' | 'claudeCodeAllowed'> | undefined;
|
|
123
|
+
directory: string;
|
|
124
|
+
mode: PermissionMode;
|
|
125
|
+
model: string | null | undefined;
|
|
126
|
+
/** Empty starts at the machine's own level. */
|
|
127
|
+
effort: EffortLevel | '';
|
|
128
|
+
allocation: string | null | undefined;
|
|
129
|
+
account: string | undefined;
|
|
130
|
+
images?: PwCodeStartImages | undefined;
|
|
131
|
+
/** The first message to send for what the reader typed. */
|
|
132
|
+
firstPrompt?: ((typed: string) => string) | undefined;
|
|
133
|
+
/** Something the first message needs has not arrived yet. */
|
|
134
|
+
waiting?: boolean | undefined;
|
|
135
|
+
/** The host opens the session. */
|
|
136
|
+
onStarted: (machine: string, sessionId: string) => void;
|
|
137
|
+
}): PwCodeStartSession;
|
|
138
|
+
export {};
|