@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,250 @@
|
|
|
1
|
+
import "./apiClient.js";
|
|
2
|
+
import { createQuery } from "./query.js";
|
|
3
|
+
import { useState } from "react";
|
|
4
|
+
import { useSWRConfig } from "swr";
|
|
5
|
+
// src/claudeApi.ts
|
|
6
|
+
var cache = {
|
|
7
|
+
dedupingInterval: 6e4,
|
|
8
|
+
revalidateOnFocus: false
|
|
9
|
+
};
|
|
10
|
+
var hostModelsPath = "/api/code/machines/{machine}/claude-code/models";
|
|
11
|
+
var sessionModelsPath = "/api/code/machines/{machine}/sessions/{id}/models";
|
|
12
|
+
var hostAccountsPath = "/api/code/machines/{machine}/claude-code/accounts";
|
|
13
|
+
var sessionAccountsPath = "/api/code/machines/{machine}/sessions/{id}/claude-code/accounts";
|
|
14
|
+
var loginsPath = "/api/code/machines/{machine}/claude-code/logins";
|
|
15
|
+
var loginPath = "/api/code/machines/{machine}/claude-code/logins/{loginId}";
|
|
16
|
+
function modelQuery(account, refresh) {
|
|
17
|
+
return {
|
|
18
|
+
...account ? { account } : {},
|
|
19
|
+
refresh
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
function hostModelOptions(machine, account, refresh) {
|
|
23
|
+
return { params: {
|
|
24
|
+
path: { machine },
|
|
25
|
+
query: modelQuery(account, refresh)
|
|
26
|
+
} };
|
|
27
|
+
}
|
|
28
|
+
function sessionModelOptions(machine, id, account, refresh) {
|
|
29
|
+
return { params: {
|
|
30
|
+
path: {
|
|
31
|
+
machine,
|
|
32
|
+
id
|
|
33
|
+
},
|
|
34
|
+
query: modelQuery(account, refresh)
|
|
35
|
+
} };
|
|
36
|
+
}
|
|
37
|
+
function hostAccountOptions(machine, namesOnly, refresh = false) {
|
|
38
|
+
return { params: {
|
|
39
|
+
path: { machine },
|
|
40
|
+
query: {
|
|
41
|
+
namesOnly,
|
|
42
|
+
...refresh && { refresh }
|
|
43
|
+
}
|
|
44
|
+
} };
|
|
45
|
+
}
|
|
46
|
+
function sessionAccountOptions(machine, id, namesOnly, refresh = false) {
|
|
47
|
+
return { params: {
|
|
48
|
+
path: {
|
|
49
|
+
machine,
|
|
50
|
+
id
|
|
51
|
+
},
|
|
52
|
+
query: {
|
|
53
|
+
namesOnly,
|
|
54
|
+
...refresh && { refresh }
|
|
55
|
+
}
|
|
56
|
+
} };
|
|
57
|
+
}
|
|
58
|
+
function claudeLoginFinished(status) {
|
|
59
|
+
return status !== "running" && status !== "checking";
|
|
60
|
+
}
|
|
61
|
+
/** Aborts after `ms`, or with `signal`. Built by hand where AbortSignal has no
|
|
62
|
+
* `timeout` or `any`, as React Native's polyfill has neither. */
|
|
63
|
+
function deadline(ms, signal) {
|
|
64
|
+
if (typeof AbortSignal.timeout === "function" && (!signal || typeof AbortSignal.any === "function")) {
|
|
65
|
+
const timeout = AbortSignal.timeout(ms);
|
|
66
|
+
return signal ? AbortSignal.any([signal, timeout]) : timeout;
|
|
67
|
+
}
|
|
68
|
+
const controller = new AbortController();
|
|
69
|
+
const timer = setTimeout(() => controller.abort(), ms);
|
|
70
|
+
const abort = () => {
|
|
71
|
+
clearTimeout(timer);
|
|
72
|
+
controller.abort(signal?.reason);
|
|
73
|
+
};
|
|
74
|
+
if (signal?.aborted) abort();
|
|
75
|
+
else signal?.addEventListener("abort", abort, { once: true });
|
|
76
|
+
return controller.signal;
|
|
77
|
+
}
|
|
78
|
+
function claudeApi(client) {
|
|
79
|
+
const useQuery = createQuery(client);
|
|
80
|
+
function useRefreshClaudeModels(machine, sessionId) {
|
|
81
|
+
const { mutate } = useSWRConfig();
|
|
82
|
+
return async (account) => {
|
|
83
|
+
if (sessionId) {
|
|
84
|
+
const { data, error } = await client.GET(sessionModelsPath, sessionModelOptions(machine, sessionId, account, true));
|
|
85
|
+
if (error) throw error;
|
|
86
|
+
for (const refresh of [false, true]) await mutate([
|
|
87
|
+
"api",
|
|
88
|
+
sessionModelsPath,
|
|
89
|
+
sessionModelOptions(machine, sessionId, account, refresh)
|
|
90
|
+
], data, { revalidate: false });
|
|
91
|
+
} else {
|
|
92
|
+
const { data, error } = await client.GET(hostModelsPath, hostModelOptions(machine, account, true));
|
|
93
|
+
if (error) throw error;
|
|
94
|
+
for (const refresh of [false, true]) await mutate([
|
|
95
|
+
"api",
|
|
96
|
+
hostModelsPath,
|
|
97
|
+
hostModelOptions(machine, account, refresh)
|
|
98
|
+
], data, { revalidate: false });
|
|
99
|
+
}
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
function useClaudeModels(machine, sessionId, account, enabled, refresh = false) {
|
|
103
|
+
const host = useQuery(hostModelsPath, enabled && machine && !sessionId ? hostModelOptions(machine, account, refresh) : null, cache);
|
|
104
|
+
const session = useQuery(sessionModelsPath, enabled && machine && sessionId ? sessionModelOptions(machine, sessionId, account, refresh) : null, cache);
|
|
105
|
+
return sessionId ? session : host;
|
|
106
|
+
}
|
|
107
|
+
function useClaudeAccountNames(machine, enabled) {
|
|
108
|
+
return useQuery(hostAccountsPath, enabled && machine ? hostAccountOptions(machine, true) : null, cache);
|
|
109
|
+
}
|
|
110
|
+
function useClaudeAccounts(machine, sessionId, enabled) {
|
|
111
|
+
const { mutate } = useSWRConfig();
|
|
112
|
+
const [refreshing, setRefreshing] = useState(false);
|
|
113
|
+
const hostNames = useClaudeAccountNames(machine, enabled && !sessionId);
|
|
114
|
+
const sessionNames = useQuery(sessionAccountsPath, enabled && machine && sessionId ? sessionAccountOptions(machine, sessionId, true) : null, cache);
|
|
115
|
+
const names = sessionId ? sessionNames : hostNames;
|
|
116
|
+
const hostUsage = useQuery(hostAccountsPath, enabled && machine && !sessionId && names.data ? hostAccountOptions(machine, false) : null, cache);
|
|
117
|
+
const sessionUsage = useQuery(sessionAccountsPath, enabled && machine && sessionId && names.data ? sessionAccountOptions(machine, sessionId, false) : null, cache);
|
|
118
|
+
const usage = sessionId ? sessionUsage : hostUsage;
|
|
119
|
+
const metadata = new Map(usage.data?.map((a) => [a.name, a]));
|
|
120
|
+
const refreshUsage = async () => {
|
|
121
|
+
const { data, error } = sessionId ? await client.GET(sessionAccountsPath, sessionAccountOptions(machine, sessionId, false, true)) : await client.GET(hostAccountsPath, hostAccountOptions(machine, false, true));
|
|
122
|
+
if (error) throw error;
|
|
123
|
+
return data;
|
|
124
|
+
};
|
|
125
|
+
const refresh = async () => {
|
|
126
|
+
setRefreshing(true);
|
|
127
|
+
try {
|
|
128
|
+
await Promise.all([names.mutate(), mutate([
|
|
129
|
+
"api",
|
|
130
|
+
sessionId ? sessionAccountsPath : hostAccountsPath,
|
|
131
|
+
sessionId ? sessionAccountOptions(machine, sessionId, false) : hostAccountOptions(machine, false)
|
|
132
|
+
], refreshUsage, { revalidate: false })]);
|
|
133
|
+
} finally {
|
|
134
|
+
setRefreshing(false);
|
|
135
|
+
}
|
|
136
|
+
};
|
|
137
|
+
return {
|
|
138
|
+
accounts: (names.data ?? []).map((a) => metadata.get(a.name) ?? a),
|
|
139
|
+
loadedUsage: !!usage.data,
|
|
140
|
+
loading: names.isLoading,
|
|
141
|
+
error: names.error ?? usage.error,
|
|
142
|
+
refreshing: refreshing || names.isValidating || usage.isValidating,
|
|
143
|
+
refresh
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
/** Drops the account from cached lists; only the cheap name lists reload. */
|
|
147
|
+
function useRemoveClaudeAccount(machine, sessionId) {
|
|
148
|
+
const { mutate } = useSWRConfig();
|
|
149
|
+
return async (name) => {
|
|
150
|
+
const { error } = await client.DELETE("/api/code/machines/{machine}/claude-code/accounts/{name}", {
|
|
151
|
+
params: { path: {
|
|
152
|
+
machine,
|
|
153
|
+
name
|
|
154
|
+
} },
|
|
155
|
+
signal: deadline(6e4)
|
|
156
|
+
});
|
|
157
|
+
if (error) throw error;
|
|
158
|
+
const drop = (accounts) => accounts?.filter((a) => a.name !== name);
|
|
159
|
+
await Promise.all([true, false].flatMap((namesOnly) => [mutate([
|
|
160
|
+
"api",
|
|
161
|
+
hostAccountsPath,
|
|
162
|
+
hostAccountOptions(machine, namesOnly)
|
|
163
|
+
], drop, { revalidate: namesOnly }), ...sessionId ? [mutate([
|
|
164
|
+
"api",
|
|
165
|
+
sessionAccountsPath,
|
|
166
|
+
sessionAccountOptions(machine, sessionId, namesOnly)
|
|
167
|
+
], drop, { revalidate: namesOnly })] : []]));
|
|
168
|
+
};
|
|
169
|
+
}
|
|
170
|
+
async function startClaudeLogin(machine, account, sessionId, columns, rows) {
|
|
171
|
+
const { data, error } = await client.POST(loginsPath, {
|
|
172
|
+
params: { path: { machine } },
|
|
173
|
+
body: {
|
|
174
|
+
account,
|
|
175
|
+
columns,
|
|
176
|
+
rows,
|
|
177
|
+
...sessionId ? { sessionId } : {}
|
|
178
|
+
},
|
|
179
|
+
signal: deadline(3e4)
|
|
180
|
+
});
|
|
181
|
+
if (error) throw error;
|
|
182
|
+
if (!data) throw new Error("Missing Claude Code sign-in response");
|
|
183
|
+
return data;
|
|
184
|
+
}
|
|
185
|
+
async function readClaudeLogin(machine, loginId, after, status, signal, wait = true) {
|
|
186
|
+
const { data, error } = await client.GET(loginPath, {
|
|
187
|
+
params: {
|
|
188
|
+
path: {
|
|
189
|
+
machine,
|
|
190
|
+
loginId
|
|
191
|
+
},
|
|
192
|
+
query: {
|
|
193
|
+
after,
|
|
194
|
+
status,
|
|
195
|
+
wait
|
|
196
|
+
}
|
|
197
|
+
},
|
|
198
|
+
signal: deadline(3e4, signal)
|
|
199
|
+
});
|
|
200
|
+
if (error) throw error;
|
|
201
|
+
if (!data) throw new Error("Missing Claude Code sign-in output");
|
|
202
|
+
return data;
|
|
203
|
+
}
|
|
204
|
+
async function writeClaudeLogin(machine, loginId, input) {
|
|
205
|
+
const { error } = await client.POST("/api/code/machines/{machine}/claude-code/logins/{loginId}/input", {
|
|
206
|
+
params: { path: {
|
|
207
|
+
machine,
|
|
208
|
+
loginId
|
|
209
|
+
} },
|
|
210
|
+
body: input,
|
|
211
|
+
signal: deadline(3e4)
|
|
212
|
+
});
|
|
213
|
+
if (error) throw error;
|
|
214
|
+
}
|
|
215
|
+
async function cancelClaudeLogin(machine, loginId) {
|
|
216
|
+
const { error } = await client.DELETE(loginPath, {
|
|
217
|
+
params: { path: {
|
|
218
|
+
machine,
|
|
219
|
+
loginId
|
|
220
|
+
} },
|
|
221
|
+
signal: deadline(3e4)
|
|
222
|
+
});
|
|
223
|
+
if (error) throw error;
|
|
224
|
+
}
|
|
225
|
+
async function followClaudeLogin(machine, login, after, signal, receive) {
|
|
226
|
+
let status = login.status;
|
|
227
|
+
let cursor = after;
|
|
228
|
+
let wait = false;
|
|
229
|
+
while (!signal.aborted && !claudeLoginFinished(status)) {
|
|
230
|
+
const event = await readClaudeLogin(machine, login.id, cursor, status, signal, wait);
|
|
231
|
+
if (signal.aborted) return;
|
|
232
|
+
receive(event);
|
|
233
|
+
cursor = event.next;
|
|
234
|
+
status = event.login.status;
|
|
235
|
+
wait = true;
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
return {
|
|
239
|
+
useClaudeModels,
|
|
240
|
+
useRefreshClaudeModels,
|
|
241
|
+
useClaudeAccountNames,
|
|
242
|
+
useClaudeAccounts,
|
|
243
|
+
useRemoveClaudeAccount,
|
|
244
|
+
startClaudeLogin,
|
|
245
|
+
writeClaudeLogin,
|
|
246
|
+
cancelClaudeLogin,
|
|
247
|
+
followClaudeLogin
|
|
248
|
+
};
|
|
249
|
+
}
|
|
250
|
+
export { claudeApi, claudeLoginFinished };
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { type ClaudeLogin, type claudeApi } from './claudeApi.js';
|
|
2
|
+
/** The sign-in calls of `createPwCodeApi`. */
|
|
3
|
+
export type ClaudeSignInCalls = Pick<ReturnType<typeof claudeApi>, 'startClaudeLogin' | 'followClaudeLogin' | 'writeClaudeLogin' | 'cancelClaudeLogin'>;
|
|
4
|
+
export interface ClaudeTerminalSize {
|
|
5
|
+
columns: number;
|
|
6
|
+
rows: number;
|
|
7
|
+
}
|
|
8
|
+
/** The host's terminal that Claude Code's sign-in prompt runs in. */
|
|
9
|
+
export interface ClaudeSignInTerminal {
|
|
10
|
+
/** Its size in cells, read as the sign-in starts and on `resized`. */
|
|
11
|
+
size: () => ClaudeTerminalSize;
|
|
12
|
+
/** Output as Claude Code printed it, escape sequences included. */
|
|
13
|
+
write?: (bytes: Uint8Array) => void;
|
|
14
|
+
/** Clears the screen: the output before this is gone. */
|
|
15
|
+
reset?: () => void;
|
|
16
|
+
/** Whether what is typed into it reaches Claude Code. */
|
|
17
|
+
setInputEnabled?: (enabled: boolean) => void;
|
|
18
|
+
/** Called once the sign-in has started and waits for input. */
|
|
19
|
+
focus?: () => void;
|
|
20
|
+
}
|
|
21
|
+
export interface ClaudeSignInState {
|
|
22
|
+
/** Bumped by `retry`, which starts a new sign-in; give each attempt a fresh terminal. */
|
|
23
|
+
attempt: number;
|
|
24
|
+
login: ClaudeLogin | null;
|
|
25
|
+
/** Why starting, following or writing failed. */
|
|
26
|
+
error: unknown;
|
|
27
|
+
/** Earlier output was dropped, so the screen was cleared. */
|
|
28
|
+
truncated: boolean;
|
|
29
|
+
/** The last input was refused for not fitting the queue. */
|
|
30
|
+
inputTooLong: boolean;
|
|
31
|
+
/** The sign-in page's address, as Claude Code last printed it. */
|
|
32
|
+
link: string | null;
|
|
33
|
+
/** The last 8192 characters of output since the screen was last cleared. */
|
|
34
|
+
output: string;
|
|
35
|
+
acceptsInput: boolean;
|
|
36
|
+
starting: boolean;
|
|
37
|
+
signedIn: boolean;
|
|
38
|
+
finished: boolean;
|
|
39
|
+
/** Following broke while the sign-in still runs. */
|
|
40
|
+
canReconnect: boolean;
|
|
41
|
+
/** The sign-in never started or ended without signing in. */
|
|
42
|
+
canRetry: boolean;
|
|
43
|
+
/** Where the sign-in stands, in words. */
|
|
44
|
+
status: string | null;
|
|
45
|
+
/** Queues text, such as a pasted code (Enter is `\r`); false when refused. */
|
|
46
|
+
input: (data: string) => boolean;
|
|
47
|
+
/** Sends the terminal's new size, if it changed while the sign-in runs. */
|
|
48
|
+
resized: () => void;
|
|
49
|
+
/** Follows the sign-in again from the last output read. */
|
|
50
|
+
reconnect: () => void;
|
|
51
|
+
retry: () => void;
|
|
52
|
+
}
|
|
53
|
+
/** Runs Claude Code's sign-in for `account` on a machine: starts it at the
|
|
54
|
+
* terminal's size, follows its output into the terminal, sends what is typed,
|
|
55
|
+
* and cancels it when the host leaves before it finishes. */
|
|
56
|
+
export declare function useClaudeSignIn(calls: ClaudeSignInCalls, { machine, sessionId, account, terminal, onSignedIn, }: {
|
|
57
|
+
machine: string;
|
|
58
|
+
sessionId: string | undefined;
|
|
59
|
+
account: string;
|
|
60
|
+
terminal: ClaudeSignInTerminal;
|
|
61
|
+
onSignedIn?: (() => void) | undefined;
|
|
62
|
+
}): ClaudeSignInState;
|
|
@@ -0,0 +1,239 @@
|
|
|
1
|
+
import { base64ToBytes, bytesToBase64 } from "./base64.js";
|
|
2
|
+
import { claudeLoginFinished } from "./claudeApi.js";
|
|
3
|
+
import { usePwCodeTranslations } from "./messages.js";
|
|
4
|
+
import { useCallback, useEffect, useRef, useState } from "react";
|
|
5
|
+
// src/claudeSignIn.ts
|
|
6
|
+
var pendingStops = /* @__PURE__ */ new Map();
|
|
7
|
+
var signInUrl = /https:\/\/claude\.(?:ai|com)\/[\w\-.~:/?#[\]@!$&'()*+,;=%]+(?=[^\w\-.~:/?#[\]@!$&'()*+,;=%])/g;
|
|
8
|
+
var maxQueuedInput = 16384;
|
|
9
|
+
var inputChunk = 4096;
|
|
10
|
+
var inputBatchMs = 30;
|
|
11
|
+
/** Runs Claude Code's sign-in for `account` on a machine: starts it at the
|
|
12
|
+
* terminal's size, follows its output into the terminal, sends what is typed,
|
|
13
|
+
* and cancels it when the host leaves before it finishes. */
|
|
14
|
+
function useClaudeSignIn(calls, { machine, sessionId, account, terminal, onSignedIn }) {
|
|
15
|
+
const t = usePwCodeTranslations("agents");
|
|
16
|
+
const [attempt, setAttempt] = useState(0);
|
|
17
|
+
const [login, setLogin] = useState(null);
|
|
18
|
+
const [error, setError] = useState(null);
|
|
19
|
+
const [truncated, setTruncated] = useState(false);
|
|
20
|
+
const [inputTooLong, setInputTooLong] = useState(false);
|
|
21
|
+
const [link, setLink] = useState(null);
|
|
22
|
+
const [output, setOutput] = useState("");
|
|
23
|
+
const [acceptsInput, setAcceptsInput] = useState(false);
|
|
24
|
+
const running = useRef(null);
|
|
25
|
+
const latest = useRef({
|
|
26
|
+
calls,
|
|
27
|
+
terminal,
|
|
28
|
+
onSignedIn
|
|
29
|
+
});
|
|
30
|
+
latest.current = {
|
|
31
|
+
calls,
|
|
32
|
+
terminal,
|
|
33
|
+
onSignedIn
|
|
34
|
+
};
|
|
35
|
+
useEffect(() => {
|
|
36
|
+
const { calls } = latest.current;
|
|
37
|
+
const screen = () => latest.current.terminal;
|
|
38
|
+
let disposed = false;
|
|
39
|
+
let current = null;
|
|
40
|
+
let cursor = 0;
|
|
41
|
+
let pending = /* @__PURE__ */ new Uint8Array(0);
|
|
42
|
+
let queuedBytes = 0;
|
|
43
|
+
let writeEpoch = 0;
|
|
44
|
+
let writeFailed = false;
|
|
45
|
+
let inputEnabled = false;
|
|
46
|
+
let flushTimer;
|
|
47
|
+
let writes = Promise.resolve();
|
|
48
|
+
let reading;
|
|
49
|
+
let tail = "";
|
|
50
|
+
let sentSize = "";
|
|
51
|
+
const decoder = new TextDecoder();
|
|
52
|
+
setLogin(null);
|
|
53
|
+
setError(null);
|
|
54
|
+
setTruncated(false);
|
|
55
|
+
setInputTooLong(false);
|
|
56
|
+
setLink(null);
|
|
57
|
+
setOutput("");
|
|
58
|
+
setAcceptsInput(false);
|
|
59
|
+
const allowInput = (enabled) => {
|
|
60
|
+
inputEnabled = enabled;
|
|
61
|
+
screen().setInputEnabled?.(enabled);
|
|
62
|
+
setAcceptsInput(enabled);
|
|
63
|
+
};
|
|
64
|
+
const findLink = (bytes) => {
|
|
65
|
+
tail = (tail + decoder.decode(bytes, { stream: true })).slice(-8192);
|
|
66
|
+
const found = tail.match(signInUrl)?.at(-1);
|
|
67
|
+
if (found) setLink(found);
|
|
68
|
+
};
|
|
69
|
+
const receive = (event) => {
|
|
70
|
+
if (disposed) return;
|
|
71
|
+
if (event.offset > cursor || cursor > event.next) {
|
|
72
|
+
screen().reset?.();
|
|
73
|
+
tail = "";
|
|
74
|
+
setTruncated(true);
|
|
75
|
+
}
|
|
76
|
+
current = event.login;
|
|
77
|
+
allowInput(!writeFailed && current.status === "running");
|
|
78
|
+
setLogin(current);
|
|
79
|
+
if (event.data) {
|
|
80
|
+
const bytes = base64ToBytes(event.data);
|
|
81
|
+
screen().write?.(bytes);
|
|
82
|
+
findLink(bytes);
|
|
83
|
+
}
|
|
84
|
+
setOutput(tail);
|
|
85
|
+
cursor = event.next;
|
|
86
|
+
if (current.status === "signed_in") latest.current.onSignedIn?.();
|
|
87
|
+
};
|
|
88
|
+
const read = async () => {
|
|
89
|
+
if (!current || disposed) return;
|
|
90
|
+
reading?.abort();
|
|
91
|
+
const controller = new AbortController();
|
|
92
|
+
reading = controller;
|
|
93
|
+
writeFailed = false;
|
|
94
|
+
setError(null);
|
|
95
|
+
try {
|
|
96
|
+
await calls.followClaudeLogin(machine, current, cursor, controller.signal, receive);
|
|
97
|
+
} catch (cause) {
|
|
98
|
+
if (controller.signal.aborted) return;
|
|
99
|
+
allowInput(false);
|
|
100
|
+
setError(cause);
|
|
101
|
+
}
|
|
102
|
+
};
|
|
103
|
+
const send = (input, bytes = 0) => {
|
|
104
|
+
const id = current?.id;
|
|
105
|
+
if (!id || disposed) return;
|
|
106
|
+
const epoch = writeEpoch;
|
|
107
|
+
writes = writes.then(async () => {
|
|
108
|
+
if (!disposed && !writeFailed && epoch === writeEpoch && current?.status === "running") await calls.writeClaudeLogin(machine, id, input);
|
|
109
|
+
else if (input.columns) sentSize = "";
|
|
110
|
+
}).catch((cause) => {
|
|
111
|
+
if (input.columns) sentSize = "";
|
|
112
|
+
if (!disposed) {
|
|
113
|
+
writeEpoch++;
|
|
114
|
+
writeFailed = true;
|
|
115
|
+
allowInput(false);
|
|
116
|
+
setError(cause);
|
|
117
|
+
}
|
|
118
|
+
}).finally(() => {
|
|
119
|
+
queuedBytes -= bytes;
|
|
120
|
+
});
|
|
121
|
+
};
|
|
122
|
+
const flush = () => {
|
|
123
|
+
flushTimer = void 0;
|
|
124
|
+
const bytes = pending;
|
|
125
|
+
pending = /* @__PURE__ */ new Uint8Array(0);
|
|
126
|
+
for (let offset = 0; offset < bytes.length; offset += inputChunk) {
|
|
127
|
+
const chunk = bytes.subarray(offset, offset + inputChunk);
|
|
128
|
+
send({ data: bytesToBase64(chunk) }, chunk.length);
|
|
129
|
+
}
|
|
130
|
+
};
|
|
131
|
+
const input = (data) => {
|
|
132
|
+
if (!inputEnabled || current?.status !== "running" || disposed || writeFailed) return false;
|
|
133
|
+
const bytes = new TextEncoder().encode(data);
|
|
134
|
+
if (queuedBytes + bytes.length > maxQueuedInput) {
|
|
135
|
+
setInputTooLong(true);
|
|
136
|
+
return false;
|
|
137
|
+
}
|
|
138
|
+
queuedBytes += bytes.length;
|
|
139
|
+
setInputTooLong(false);
|
|
140
|
+
const buffer = new Uint8Array(pending.length + bytes.length);
|
|
141
|
+
buffer.set(pending);
|
|
142
|
+
buffer.set(bytes, pending.length);
|
|
143
|
+
pending = buffer;
|
|
144
|
+
flushTimer ??= setTimeout(flush, inputBatchMs);
|
|
145
|
+
return true;
|
|
146
|
+
};
|
|
147
|
+
const size = () => {
|
|
148
|
+
const { columns, rows } = screen().size();
|
|
149
|
+
return {
|
|
150
|
+
columns: Math.min(500, Math.max(1, columns)),
|
|
151
|
+
rows: Math.min(200, Math.max(1, rows))
|
|
152
|
+
};
|
|
153
|
+
};
|
|
154
|
+
const resized = () => {
|
|
155
|
+
const next = size();
|
|
156
|
+
const key = `${next.columns}x${next.rows}`;
|
|
157
|
+
if (current?.status === "running" && key !== sentSize) {
|
|
158
|
+
sentSize = key;
|
|
159
|
+
send(next);
|
|
160
|
+
}
|
|
161
|
+
};
|
|
162
|
+
running.current = {
|
|
163
|
+
input,
|
|
164
|
+
resized,
|
|
165
|
+
reconnect: () => {
|
|
166
|
+
read();
|
|
167
|
+
resized();
|
|
168
|
+
}
|
|
169
|
+
};
|
|
170
|
+
const key = JSON.stringify([
|
|
171
|
+
machine,
|
|
172
|
+
sessionId,
|
|
173
|
+
account
|
|
174
|
+
]);
|
|
175
|
+
const previousStop = pendingStops.get(key);
|
|
176
|
+
const started = Promise.resolve().then(async () => {
|
|
177
|
+
await previousStop;
|
|
178
|
+
if (disposed) return;
|
|
179
|
+
const { columns, rows } = size();
|
|
180
|
+
sentSize = `${columns}x${rows}`;
|
|
181
|
+
return calls.startClaudeLogin(machine, account, sessionId, columns, rows);
|
|
182
|
+
});
|
|
183
|
+
started.then(async (event) => {
|
|
184
|
+
if (!event || disposed) return;
|
|
185
|
+
receive(event);
|
|
186
|
+
resized();
|
|
187
|
+
screen().focus?.();
|
|
188
|
+
await read();
|
|
189
|
+
}).catch((cause) => {
|
|
190
|
+
if (!disposed) setError(cause);
|
|
191
|
+
});
|
|
192
|
+
return () => {
|
|
193
|
+
disposed = true;
|
|
194
|
+
reading?.abort();
|
|
195
|
+
if (flushTimer) clearTimeout(flushTimer);
|
|
196
|
+
running.current = null;
|
|
197
|
+
const stopped = started.then(async (event) => {
|
|
198
|
+
const login = current ?? event?.login;
|
|
199
|
+
if (login && !claudeLoginFinished(login.status)) await calls.cancelClaudeLogin(machine, login.id);
|
|
200
|
+
}).catch(() => {});
|
|
201
|
+
pendingStops.set(key, stopped);
|
|
202
|
+
stopped.then(() => {
|
|
203
|
+
if (pendingStops.get(key) === stopped) pendingStops.delete(key);
|
|
204
|
+
});
|
|
205
|
+
};
|
|
206
|
+
}, [
|
|
207
|
+
machine,
|
|
208
|
+
account,
|
|
209
|
+
sessionId,
|
|
210
|
+
attempt
|
|
211
|
+
]);
|
|
212
|
+
const input = useCallback((data) => running.current?.input(data) ?? false, []);
|
|
213
|
+
const resized = useCallback(() => running.current?.resized(), []);
|
|
214
|
+
const reconnect = useCallback(() => running.current?.reconnect(), []);
|
|
215
|
+
const retry = useCallback(() => setAttempt((value) => value + 1), []);
|
|
216
|
+
const finished = login ? claudeLoginFinished(login.status) : false;
|
|
217
|
+
const signedIn = login?.status === "signed_in";
|
|
218
|
+
return {
|
|
219
|
+
attempt,
|
|
220
|
+
login,
|
|
221
|
+
error,
|
|
222
|
+
truncated,
|
|
223
|
+
inputTooLong,
|
|
224
|
+
link,
|
|
225
|
+
output,
|
|
226
|
+
acceptsInput,
|
|
227
|
+
starting: !login && !error,
|
|
228
|
+
signedIn,
|
|
229
|
+
finished,
|
|
230
|
+
canReconnect: Boolean(error) && !!login && !finished,
|
|
231
|
+
canRetry: Boolean(error) && !login || finished && !signedIn,
|
|
232
|
+
status: login ? t(`claude.signInStatus.${login.status}`, { email: login.email ?? account }) : null,
|
|
233
|
+
input,
|
|
234
|
+
resized,
|
|
235
|
+
reconnect,
|
|
236
|
+
retry
|
|
237
|
+
};
|
|
238
|
+
}
|
|
239
|
+
export { useClaudeSignIn };
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/** A command a session's palette lists. */
|
|
2
|
+
export interface PwCodeSlashCommand {
|
|
3
|
+
name: string;
|
|
4
|
+
description?: string | undefined;
|
|
5
|
+
usage?: string | undefined;
|
|
6
|
+
}
|
|
7
|
+
/** What a session can run from a slash, as far as typed text is dispatched by
|
|
8
|
+
* it. A host that types its API from a spec asserts its type extends this. */
|
|
9
|
+
export interface PwCodePalette {
|
|
10
|
+
commands?: readonly PwCodeSlashCommand[] | null | undefined;
|
|
11
|
+
mcpPrompts?: readonly PwCodeSlashCommand[] | null | undefined;
|
|
12
|
+
prompts?: readonly string[] | null | undefined;
|
|
13
|
+
}
|
|
14
|
+
export type SlashAction = {
|
|
15
|
+
kind: 'message';
|
|
16
|
+
} | {
|
|
17
|
+
kind: 'clear';
|
|
18
|
+
} | {
|
|
19
|
+
kind: 'compact';
|
|
20
|
+
focus: string;
|
|
21
|
+
} | {
|
|
22
|
+
kind: 'goal';
|
|
23
|
+
args: string;
|
|
24
|
+
} | {
|
|
25
|
+
kind: 'plan';
|
|
26
|
+
args: string;
|
|
27
|
+
} | {
|
|
28
|
+
kind: 'archive';
|
|
29
|
+
} | {
|
|
30
|
+
kind: 'command';
|
|
31
|
+
name: string;
|
|
32
|
+
args: string;
|
|
33
|
+
};
|
|
34
|
+
/** Where typed text goes, in the order the terminal dispatches it: skills,
|
|
35
|
+
* custom commands, and MCP prompts expand into a turn, so they are messages;
|
|
36
|
+
* built-ins run as commands, five of them on routes of their own. */
|
|
37
|
+
export declare function slashAction(text: string, palette: PwCodePalette | null): SlashAction;
|
|
38
|
+
export declare function paletteOptions(palette: PwCodePalette | null, claudeAccountDescription?: string, archiveDescription?: string): PwCodeSlashCommand[];
|
|
39
|
+
/** The command a `!` line runs, read as the terminal reads it: empty for a bare
|
|
40
|
+
* `!`, which runs nothing, and null for text that is not a shell escape. */
|
|
41
|
+
export declare function shellCommand(text: string): string | null;
|
|
42
|
+
/** A proxy's 502 or 504 is not a problem, so it has no type: like the platform's no answer, it may have landed. */
|
|
43
|
+
export declare function steerMayHaveLanded(error: {
|
|
44
|
+
code?: string | undefined;
|
|
45
|
+
type?: string | undefined;
|
|
46
|
+
status?: number | undefined;
|
|
47
|
+
}): boolean;
|
package/dist/command.js
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
// src/command.ts
|
|
2
|
+
var INVOCATION = /^\/([A-Za-z0-9_:-]+)(?:\s+([\s\S]*))?$/;
|
|
3
|
+
/** Where typed text goes, in the order the terminal dispatches it: skills,
|
|
4
|
+
* custom commands, and MCP prompts expand into a turn, so they are messages;
|
|
5
|
+
* built-ins run as commands, five of them on routes of their own. */
|
|
6
|
+
function slashAction(text, palette) {
|
|
7
|
+
const match = INVOCATION.exec(text.trim());
|
|
8
|
+
const name = match?.[1];
|
|
9
|
+
if (!match || !name || !palette) return { kind: "message" };
|
|
10
|
+
const args = (match[2] ?? "").trim();
|
|
11
|
+
if (name.startsWith("mcp__") || (palette.prompts ?? []).includes(name)) return { kind: "message" };
|
|
12
|
+
switch (name) {
|
|
13
|
+
case "clear": return { kind: "clear" };
|
|
14
|
+
case "compact": return {
|
|
15
|
+
kind: "compact",
|
|
16
|
+
focus: args
|
|
17
|
+
};
|
|
18
|
+
case "goal": return {
|
|
19
|
+
kind: "goal",
|
|
20
|
+
args
|
|
21
|
+
};
|
|
22
|
+
case "plan": return {
|
|
23
|
+
kind: "plan",
|
|
24
|
+
args
|
|
25
|
+
};
|
|
26
|
+
case "archive": return { kind: "archive" };
|
|
27
|
+
}
|
|
28
|
+
if ((palette.commands ?? []).some((c) => c.name === name)) return {
|
|
29
|
+
kind: "command",
|
|
30
|
+
name,
|
|
31
|
+
args
|
|
32
|
+
};
|
|
33
|
+
return { kind: "message" };
|
|
34
|
+
}
|
|
35
|
+
function paletteOptions(palette, claudeAccountDescription, archiveDescription) {
|
|
36
|
+
const commands = [...palette?.commands ?? [], ...palette?.mcpPrompts ?? []].filter((c) => c.name !== "claude-code").map((c) => ({
|
|
37
|
+
name: c.name,
|
|
38
|
+
description: c.description,
|
|
39
|
+
usage: c.usage
|
|
40
|
+
}));
|
|
41
|
+
if (archiveDescription !== void 0 && !commands.some((c) => c.name === "archive")) {
|
|
42
|
+
const at = commands.findIndex((c) => c.name > "archive");
|
|
43
|
+
commands.splice(at === -1 ? commands.length : at, 0, {
|
|
44
|
+
name: "archive",
|
|
45
|
+
description: archiveDescription,
|
|
46
|
+
usage: "/archive"
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
return claudeAccountDescription === void 0 ? commands : [{
|
|
50
|
+
name: "claude-code",
|
|
51
|
+
description: claudeAccountDescription
|
|
52
|
+
}, ...commands];
|
|
53
|
+
}
|
|
54
|
+
/** The command a `!` line runs, read as the terminal reads it: empty for a bare
|
|
55
|
+
* `!`, which runs nothing, and null for text that is not a shell escape. */
|
|
56
|
+
function shellCommand(text) {
|
|
57
|
+
const trimmed = text.trim();
|
|
58
|
+
return trimmed.startsWith("!") ? trimmed.slice(1).trim() : null;
|
|
59
|
+
}
|
|
60
|
+
/** A proxy's 502 or 504 is not a problem, so it has no type: like the platform's no answer, it may have landed. */
|
|
61
|
+
function steerMayHaveLanded(error) {
|
|
62
|
+
return error.code === "upstream_no_answer" || !error.code && !error.type && (error.status === 502 || error.status === 504);
|
|
63
|
+
}
|
|
64
|
+
export { paletteOptions, shellCommand, slashAction, steerMayHaveLanded };
|