@parallelworks/code 0.0.0-stage → 0.2.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.
Files changed (73) hide show
  1. package/LICENSE +202 -0
  2. package/README.md +205 -2
  3. package/dist/api.d.ts +3469 -0
  4. package/dist/apiClient.d.ts +26 -0
  5. package/dist/apiClient.js +5 -0
  6. package/dist/claudeApi.d.ts +27 -0
  7. package/dist/claudeApi.js +250 -0
  8. package/dist/command.d.ts +47 -0
  9. package/dist/command.js +64 -0
  10. package/dist/commandsApi.d.ts +64 -0
  11. package/dist/commandsApi.js +267 -0
  12. package/dist/createPwCodeApi.d.ts +688 -0
  13. package/dist/createPwCodeApi.js +31 -0
  14. package/dist/images.d.ts +7 -0
  15. package/dist/images.js +22 -0
  16. package/dist/index.d.ts +18 -0
  17. package/dist/index.js +18 -0
  18. package/dist/locales/en/agentTranscript.js +170 -0
  19. package/dist/locales/en/agents.js +380 -0
  20. package/dist/locales/en.d.ts +531 -0
  21. package/dist/locales/en.js +8 -0
  22. package/dist/locales/es/agentTranscript.js +170 -0
  23. package/dist/locales/es/agents.js +380 -0
  24. package/dist/locales/es.d.ts +3 -0
  25. package/dist/locales/es.js +8 -0
  26. package/dist/locales/ja/agentTranscript.js +170 -0
  27. package/dist/locales/ja/agents.js +380 -0
  28. package/dist/locales/ja.d.ts +3 -0
  29. package/dist/locales/ja.js +8 -0
  30. package/dist/locales/ko/agentTranscript.js +170 -0
  31. package/dist/locales/ko/agents.js +380 -0
  32. package/dist/locales/ko.d.ts +3 -0
  33. package/dist/locales/ko.js +8 -0
  34. package/dist/locales/zh/agentTranscript.js +170 -0
  35. package/dist/locales/zh/agents.js +380 -0
  36. package/dist/locales/zh.d.ts +3 -0
  37. package/dist/locales/zh.js +8 -0
  38. package/dist/machine.d.ts +49 -0
  39. package/dist/machine.js +60 -0
  40. package/dist/messages.d.ts +17 -0
  41. package/dist/messages.js +31 -0
  42. package/dist/notices.d.ts +11 -0
  43. package/dist/notices.js +94 -0
  44. package/dist/pullRequest.d.ts +8 -0
  45. package/dist/pullRequest.js +29 -0
  46. package/dist/query.d.ts +28 -0
  47. package/dist/query.js +25 -0
  48. package/dist/session.d.ts +72 -0
  49. package/dist/session.js +111 -0
  50. package/dist/sessionEvents.d.ts +27 -0
  51. package/dist/sessionEvents.js +212 -0
  52. package/dist/sessionsApi.d.ts +105 -0
  53. package/dist/sessionsApi.js +513 -0
  54. package/dist/toolStrings.d.ts +4 -0
  55. package/dist/toolStrings.js +95 -0
  56. package/dist/transcript.d.ts +151 -0
  57. package/dist/transcript.js +1315 -0
  58. package/dist/types.d.ts +234 -0
  59. package/dist/useTranscript.d.ts +63 -0
  60. package/dist/useTranscript.js +243 -0
  61. package/dist/wording.d.ts +43 -0
  62. package/dist/wording.js +101 -0
  63. package/locales/en/agentTranscript.json +162 -0
  64. package/locales/en/agents.json +366 -0
  65. package/locales/es/agentTranscript.json +162 -0
  66. package/locales/es/agents.json +366 -0
  67. package/locales/ja/agentTranscript.json +162 -0
  68. package/locales/ja/agents.json +366 -0
  69. package/locales/ko/agentTranscript.json +162 -0
  70. package/locales/ko/agents.json +366 -0
  71. package/locales/zh/agentTranscript.json +162 -0
  72. package/locales/zh/agents.json +366 -0
  73. package/package.json +68 -3
@@ -0,0 +1,267 @@
1
+ import { createQuery } from "./query.js";
2
+ import { shellCommand, slashAction } from "./command.js";
3
+ import { useCallback, useEffect, useRef, useState } from "react";
4
+ // src/commandsApi.ts
5
+ var backgroundOfferDelayMs = 2e3;
6
+ function planOutcome(plan, args, planOnMachine) {
7
+ switch (plan.action) {
8
+ case "answer": return {
9
+ kind: "output",
10
+ text: plan.output ?? ""
11
+ };
12
+ case "show": return {
13
+ kind: "output",
14
+ text: plan.planBody ?? ""
15
+ };
16
+ case "turn": return {
17
+ kind: "send",
18
+ text: args
19
+ };
20
+ case "open": return {
21
+ kind: "output",
22
+ text: planOnMachine(plan.planPath ?? "")
23
+ };
24
+ default: return { kind: "none" };
25
+ }
26
+ }
27
+ function settle(outcome, command, kind, deps) {
28
+ switch (outcome.kind) {
29
+ case "send": return outcome.text;
30
+ case "output":
31
+ if (outcome.text && kind !== "goal") deps.onOutput(command, outcome.text);
32
+ return null;
33
+ case "compacted":
34
+ deps.onCompacted();
35
+ return null;
36
+ case "cleared":
37
+ deps.onCleared(outcome.newId);
38
+ return null;
39
+ case "refused":
40
+ deps.onRefused(outcome.error);
41
+ return null;
42
+ default: return null;
43
+ }
44
+ }
45
+ function commandsApi(client) {
46
+ const useQuery = createQuery(client);
47
+ const palettes = /* @__PURE__ */ new Map();
48
+ /** Runs a command against the session's daemon, answering with what the
49
+ * host owes next. /plan's actions follow the terminal's, except that opening
50
+ * the plan file needs the machine's own editor. */
51
+ async function runSlashAction(machine, id, action, planOnMachine) {
52
+ const path = { params: { path: {
53
+ machine,
54
+ id
55
+ } } };
56
+ switch (action.kind) {
57
+ case "clear": {
58
+ const { data, error } = await client.POST("/api/code/machines/{machine}/sessions/{id}/clear", path);
59
+ return error ? {
60
+ kind: "refused",
61
+ error
62
+ } : {
63
+ kind: "cleared",
64
+ newId: data.id
65
+ };
66
+ }
67
+ case "compact": {
68
+ const { data, error } = await client.POST("/api/code/machines/{machine}/sessions/{id}/compact", {
69
+ ...path,
70
+ body: { focus: action.focus }
71
+ });
72
+ if (error) return {
73
+ kind: "refused",
74
+ error
75
+ };
76
+ return data.compacted ? { kind: "compacted" } : {
77
+ kind: "output",
78
+ text: data.output
79
+ };
80
+ }
81
+ case "goal": {
82
+ const { data, error } = await client.POST("/api/code/machines/{machine}/sessions/{id}/goal", {
83
+ ...path,
84
+ body: { args: action.args }
85
+ });
86
+ return error ? {
87
+ kind: "refused",
88
+ error
89
+ } : {
90
+ kind: "output",
91
+ text: data.output
92
+ };
93
+ }
94
+ case "plan": {
95
+ const { data, error } = await client.POST("/api/code/machines/{machine}/sessions/{id}/plan", {
96
+ ...path,
97
+ body: { args: action.args }
98
+ });
99
+ return error ? {
100
+ kind: "refused",
101
+ error
102
+ } : planOutcome(data, action.args, planOnMachine);
103
+ }
104
+ case "command": {
105
+ const { data, error } = await client.POST("/api/code/machines/{machine}/sessions/{id}/commands", {
106
+ ...path,
107
+ body: {
108
+ name: action.name,
109
+ args: action.args
110
+ }
111
+ });
112
+ return error ? {
113
+ kind: "refused",
114
+ error
115
+ } : {
116
+ kind: "output",
117
+ text: [data.output, data.note].filter(Boolean).join("\n\n")
118
+ };
119
+ }
120
+ }
121
+ }
122
+ async function runShell(command, deps) {
123
+ const { data, error } = await client.POST("/api/code/machines/{machine}/sessions/{id}/shell", {
124
+ params: { path: {
125
+ machine: deps.machine,
126
+ id: deps.sessionId
127
+ } },
128
+ body: { command }
129
+ });
130
+ if (error) {
131
+ deps.onRefused(error);
132
+ return;
133
+ }
134
+ await deps.onShellRun(command, data.output, data.stop);
135
+ }
136
+ /** Runs typed text that is a command or a `!` shell escape, answering with
137
+ * what still goes to the session as a message, or null once it was taken. */
138
+ async function resolveTyped(text, deps) {
139
+ const command = shellCommand(text);
140
+ if (command !== null) {
141
+ if (command) await runShell(command, deps);
142
+ return null;
143
+ }
144
+ if (!text.trimStart().startsWith("/")) return text;
145
+ const action = slashAction(text, deps.palette ?? await deps.loadPalette());
146
+ if (action.kind === "message") return text;
147
+ if (action.kind === "archive") {
148
+ await deps.onArchive();
149
+ return null;
150
+ }
151
+ return settle(await runSlashAction(deps.machine, deps.sessionId, action, deps.planOnMachine), text.trim(), action.kind, deps);
152
+ }
153
+ /** The session's palette, loaded the first time the composer reads as a
154
+ * command: loading attaches a saved session, which viewing must not do. */
155
+ function usePalette(machine, sessionId) {
156
+ const key = `${machine}/${sessionId}`;
157
+ const [palette, setPalette] = useState(() => palettes.get(key) ?? null);
158
+ const [fetching, setFetching] = useState(false);
159
+ const loading = useRef(null);
160
+ useEffect(() => {
161
+ setPalette(palettes.get(key) ?? null);
162
+ setFetching(false);
163
+ loading.current = null;
164
+ }, [key]);
165
+ const load = useCallback(() => {
166
+ if (!loading.current) {
167
+ setFetching(true);
168
+ const request = client.GET("/api/code/machines/{machine}/sessions/{id}/palette", { params: { path: {
169
+ machine,
170
+ id: sessionId
171
+ } } }).then(({ data }) => {
172
+ if (data) palettes.set(key, data);
173
+ if (loading.current === request) {
174
+ setFetching(false);
175
+ if (data) setPalette(data);
176
+ else loading.current = null;
177
+ }
178
+ return data ?? null;
179
+ }, (error) => {
180
+ if (loading.current === request) {
181
+ setFetching(false);
182
+ loading.current = null;
183
+ }
184
+ throw error;
185
+ });
186
+ loading.current = request;
187
+ }
188
+ return loading.current;
189
+ }, [
190
+ key,
191
+ machine,
192
+ sessionId
193
+ ]);
194
+ return {
195
+ palette,
196
+ loading: fetching && !palette,
197
+ load
198
+ };
199
+ }
200
+ /** What a session started in workspace could run first, loaded once the composer reads as a command. */
201
+ function useWorkspacePalette(machine, workspace, model) {
202
+ const [wanted, setWanted] = useState(false);
203
+ const load = useCallback(() => setWanted(true), []);
204
+ const { data, isLoading } = useQuery("/api/code/machines/{machine}/palette", wanted && machine && workspace.startsWith("/") ? { params: {
205
+ path: { machine },
206
+ query: {
207
+ workspace,
208
+ ...model ? { model } : {}
209
+ }
210
+ } } : null, { shouldRetryOnError: false });
211
+ return {
212
+ palette: data ?? null,
213
+ loading: isLoading,
214
+ load
215
+ };
216
+ }
217
+ /** Offers to move the Bash commands a turn waits on to the background, as
218
+ * ctrl+b does in the terminal. */
219
+ function useBackgroundCommands({ machine, sessionId, running, onError }) {
220
+ const [offered, setOffered] = useState(false);
221
+ const [moving, setMoving] = useState(false);
222
+ useEffect(() => {
223
+ setOffered(false);
224
+ if (!running) return;
225
+ const timer = setTimeout(() => setOffered(true), backgroundOfferDelayMs);
226
+ return () => clearTimeout(timer);
227
+ }, [running]);
228
+ const move = useCallback(async () => {
229
+ setMoving(true);
230
+ const { error } = await client.POST("/api/code/machines/{machine}/sessions/{id}/background", { params: { path: {
231
+ machine,
232
+ id: sessionId
233
+ } } });
234
+ setMoving(false);
235
+ if (error) onError(error);
236
+ }, [
237
+ machine,
238
+ onError,
239
+ sessionId
240
+ ]);
241
+ return {
242
+ offered: running && offered,
243
+ moving,
244
+ move
245
+ };
246
+ }
247
+ /** Asks the session for a recap of where it stands, which arrives on its
248
+ * event feed. The machine declines while its recaps are off. */
249
+ function requestRecap(machine, id, signal) {
250
+ return client.POST("/api/code/machines/{machine}/sessions/{id}/recap", {
251
+ params: { path: {
252
+ machine,
253
+ id
254
+ } },
255
+ ...signal ? { signal } : {}
256
+ });
257
+ }
258
+ return {
259
+ runSlashAction,
260
+ resolveTyped,
261
+ usePalette,
262
+ useWorkspacePalette,
263
+ useBackgroundCommands,
264
+ requestRecap
265
+ };
266
+ }
267
+ export { backgroundOfferDelayMs, commandsApi };