@ai-hero/sandcastle 0.13.0 → 0.14.1
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 +3 -3
- package/dist/{SandboxProvider-YCL97qco.d.ts → SandboxProvider-CwRLxIXZ.d.ts} +5 -41
- package/dist/{chunk-XCYK7I45.js → chunk-4BB7DNPW.js} +3 -3
- package/dist/chunk-4BB7DNPW.js.map +1 -0
- package/dist/chunk-4GZMUHVO.js +60 -0
- package/dist/chunk-4GZMUHVO.js.map +1 -0
- package/dist/{chunk-A4C2FG74.js → chunk-7JUTKCHL.js} +43 -42
- package/dist/chunk-7JUTKCHL.js.map +1 -0
- package/dist/chunk-7PHPLDVS.js +284 -0
- package/dist/chunk-7PHPLDVS.js.map +1 -0
- package/dist/{chunk-NMQ5Z6YG.js → chunk-BXLS4HRY.js} +15 -4
- package/dist/chunk-BXLS4HRY.js.map +1 -0
- package/dist/chunk-EM4QNWKF.js +3161 -0
- package/dist/chunk-EM4QNWKF.js.map +1 -0
- package/dist/chunk-EXYXBRCX.js +60 -0
- package/dist/chunk-EXYXBRCX.js.map +1 -0
- package/dist/{chunk-LWNC4UTZ.js → chunk-JEQ4IVQM.js} +1520 -5375
- package/dist/chunk-JEQ4IVQM.js.map +1 -0
- package/dist/{chunk-XCWKLDH4.js → chunk-L3BU47F6.js} +3 -3
- package/dist/{chunk-XCWKLDH4.js.map → chunk-L3BU47F6.js.map} +1 -1
- package/dist/{chunk-7N7AMUYA.js → chunk-PTJEKJPQ.js} +7 -5
- package/dist/chunk-PTJEKJPQ.js.map +1 -0
- package/dist/chunk-SAQXFLO2.js +1268 -0
- package/dist/chunk-SAQXFLO2.js.map +1 -0
- package/dist/{chunk-767EAJVT.js → chunk-TBVRAXYF.js} +3 -3
- package/dist/{chunk-767EAJVT.js.map → chunk-TBVRAXYF.js.map} +1 -1
- package/dist/{chunk-J5J5YJ5S.js → chunk-VUOXFCWO.js} +3 -3
- package/dist/{chunk-J5J5YJ5S.js.map → chunk-VUOXFCWO.js.map} +1 -1
- package/dist/{chunk-U42XXICG.js → chunk-WYOGXC72.js} +47 -82
- package/dist/chunk-WYOGXC72.js.map +1 -0
- package/dist/chunk-YDQW5IOY.js +799 -0
- package/dist/chunk-YDQW5IOY.js.map +1 -0
- package/dist/index.d.ts +51 -4
- package/dist/index.js +892 -2018
- package/dist/index.js.map +1 -1
- package/dist/main.js +322 -251
- package/dist/main.js.map +1 -1
- package/dist/sandboxes/bwrap.d.ts +1 -1
- package/dist/sandboxes/bwrap.js +9 -14
- package/dist/sandboxes/bwrap.js.map +1 -1
- package/dist/sandboxes/daytona.d.ts +1 -1
- package/dist/sandboxes/daytona.js +183 -136
- package/dist/sandboxes/daytona.js.map +1 -1
- package/dist/sandboxes/docker.d.ts +23 -1
- package/dist/sandboxes/docker.js +7 -3
- package/dist/sandboxes/e2b.d.ts +41 -0
- package/dist/sandboxes/e2b.js +202 -0
- package/dist/sandboxes/e2b.js.map +1 -0
- package/dist/sandboxes/modal.d.ts +78 -0
- package/dist/sandboxes/modal.js +171 -0
- package/dist/sandboxes/modal.js.map +1 -0
- package/dist/sandboxes/no-sandbox.d.ts +1 -1
- package/dist/sandboxes/no-sandbox.js +1 -1
- package/dist/sandboxes/podman.d.ts +27 -4
- package/dist/sandboxes/podman.js +19 -43
- package/dist/sandboxes/podman.js.map +1 -1
- package/dist/sandboxes/seatbelt.d.ts +2 -2
- package/dist/sandboxes/seatbelt.js +10 -7
- package/dist/sandboxes/seatbelt.js.map +1 -1
- package/dist/sandboxes/sprites.d.ts +54 -0
- package/dist/sandboxes/sprites.js +219 -0
- package/dist/sandboxes/sprites.js.map +1 -0
- package/dist/sandboxes/vercel.d.ts +10 -1
- package/dist/sandboxes/vercel.js +74 -46
- package/dist/sandboxes/vercel.js.map +1 -1
- package/dist/templates/parallel-planner/main.mts +43 -21
- package/dist/templates/parallel-planner-with-review/main.mts +55 -27
- package/dist/templates/sequential-reviewer/main.mts +1 -1
- package/package.json +36 -4
- package/dist/chunk-4EIVTSOZ.js +0 -93
- package/dist/chunk-4EIVTSOZ.js.map +0 -1
- package/dist/chunk-7N7AMUYA.js.map +0 -1
- package/dist/chunk-A4C2FG74.js.map +0 -1
- package/dist/chunk-LWNC4UTZ.js.map +0 -1
- package/dist/chunk-NMQ5Z6YG.js.map +0 -1
- package/dist/chunk-U42XXICG.js.map +0 -1
- package/dist/chunk-XCYK7I45.js.map +0 -1
|
@@ -0,0 +1,1268 @@
|
|
|
1
|
+
import { createRequire } from 'node:module';
|
|
2
|
+
import { ForkNotSupportedError } from './chunk-YDQW5IOY.js';
|
|
3
|
+
import { createHash } from 'crypto';
|
|
4
|
+
import { readdir, mkdir, writeFile, readFile, access, rm } from 'fs/promises';
|
|
5
|
+
import { join, posix, dirname, relative } from 'path';
|
|
6
|
+
import { tmpdir } from 'os';
|
|
7
|
+
|
|
8
|
+
createRequire(import.meta.url);
|
|
9
|
+
var homeDir = () => process.env.HOME ?? "~";
|
|
10
|
+
var firstExistingPath = async (paths) => {
|
|
11
|
+
const [head, ...rest] = paths;
|
|
12
|
+
if (head === void 0) {
|
|
13
|
+
return void 0;
|
|
14
|
+
}
|
|
15
|
+
return await pathExists(head) ? head : firstExistingPath(rest);
|
|
16
|
+
};
|
|
17
|
+
var pathExists = async (path) => {
|
|
18
|
+
try {
|
|
19
|
+
await access(path);
|
|
20
|
+
return true;
|
|
21
|
+
} catch {
|
|
22
|
+
return false;
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
var MAX_PROJECT_DIR_LENGTH = 200;
|
|
26
|
+
var claudePathHash = (value) => {
|
|
27
|
+
const hash = value.split("").reduce((acc, unit) => Math.imul(31, acc) + (unit.codePointAt(0) ?? 0), 0);
|
|
28
|
+
return Math.abs(Math.imul(hash, 1)).toString(36);
|
|
29
|
+
};
|
|
30
|
+
var encodeProjectPath = (cwd) => {
|
|
31
|
+
const isRoot = cwd === "/" || /^[A-Za-z]:[\\/]?$/u.test(cwd);
|
|
32
|
+
const normalized = isRoot ? cwd : cwd.replace(/[\\/]+$/u, "");
|
|
33
|
+
const encoded = normalized.replaceAll(/[^A-Za-z0-9]/gu, "-");
|
|
34
|
+
if (encoded.length <= MAX_PROJECT_DIR_LENGTH) {
|
|
35
|
+
return encoded;
|
|
36
|
+
}
|
|
37
|
+
return `${encoded.slice(0, MAX_PROJECT_DIR_LENGTH)}-${claudePathHash(normalized)}`;
|
|
38
|
+
};
|
|
39
|
+
var DEFAULT_SANDBOX_DIRS = { home: "/home/agent", tmp: "/tmp" };
|
|
40
|
+
var sandboxDirsOf = (handle) => ({
|
|
41
|
+
home: handle.sandboxHomedir ?? DEFAULT_SANDBOX_DIRS.home,
|
|
42
|
+
tmp: handle.sandboxTmpdir ?? DEFAULT_SANDBOX_DIRS.tmp
|
|
43
|
+
});
|
|
44
|
+
var claudeHostSessionPath = (cwd, id, projectsDir) => {
|
|
45
|
+
const base = projectsDir ?? join(homeDir(), ".claude", "projects");
|
|
46
|
+
return join(base, encodeProjectPath(cwd), `${id}.jsonl`);
|
|
47
|
+
};
|
|
48
|
+
var claudeSandboxSessionPath = (cwd, id, projectsDir) => posix.join(projectsDir, encodeProjectPath(cwd), `${id}.jsonl`);
|
|
49
|
+
var claudeSubagentsDirInSandbox = (cwd, id, projectsDir) => posix.join(projectsDir, encodeProjectPath(cwd), id, "subagents");
|
|
50
|
+
var claudeSubagentsDirOnHost = (cwd, id, projectsDir) => {
|
|
51
|
+
const base = projectsDir ?? join(homeDir(), ".claude", "projects");
|
|
52
|
+
return join(base, encodeProjectPath(cwd), id, "subagents");
|
|
53
|
+
};
|
|
54
|
+
var listClaudeSubagentSessionsInSandbox = async (cwd, id, handle, sandboxProjectsDir) => {
|
|
55
|
+
const dir = claudeSubagentsDirInSandbox(cwd, id, sandboxProjectsDir);
|
|
56
|
+
const result = await handle.exec(
|
|
57
|
+
`find ${JSON.stringify(dir)} -type f -name ${JSON.stringify("agent-*.jsonl")} 2>/dev/null`
|
|
58
|
+
);
|
|
59
|
+
if (result.exitCode !== 0) {
|
|
60
|
+
return [];
|
|
61
|
+
}
|
|
62
|
+
const stdout = result.stdout.trim();
|
|
63
|
+
if (stdout === "") {
|
|
64
|
+
return [];
|
|
65
|
+
}
|
|
66
|
+
return stdout.split("\n").filter((line) => line !== "");
|
|
67
|
+
};
|
|
68
|
+
var findClaudeSessionOnHost = async (id, projectsDir) => {
|
|
69
|
+
const root = projectsDir ?? join(homeDir(), ".claude", "projects");
|
|
70
|
+
let entries;
|
|
71
|
+
try {
|
|
72
|
+
entries = await readdir(root, { withFileTypes: true });
|
|
73
|
+
} catch {
|
|
74
|
+
return { path: void 0, searchedRoot: root };
|
|
75
|
+
}
|
|
76
|
+
const candidates = entries.filter((entry) => entry.isDirectory()).map((entry) => join(root, entry.name, `${id}.jsonl`));
|
|
77
|
+
return { path: await firstExistingPath(candidates), searchedRoot: root };
|
|
78
|
+
};
|
|
79
|
+
var rewriteEntryCwd = (entry, fromCwd, toCwd) => {
|
|
80
|
+
if (typeof entry.cwd === "string" && entry.cwd === fromCwd) {
|
|
81
|
+
entry.cwd = toCwd;
|
|
82
|
+
}
|
|
83
|
+
if (entry.type === "session_meta" && isRecord(entry.payload) && typeof entry.payload.cwd === "string" && entry.payload.cwd === fromCwd) {
|
|
84
|
+
entry.payload.cwd = toCwd;
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
var rewriteSessionCwd = (content, fromCwd, toCwd) => {
|
|
88
|
+
if (content === "") {
|
|
89
|
+
return "";
|
|
90
|
+
}
|
|
91
|
+
return content.split("\n").map((line) => {
|
|
92
|
+
if (line === "") {
|
|
93
|
+
return line;
|
|
94
|
+
}
|
|
95
|
+
try {
|
|
96
|
+
const entry = JSON.parse(line);
|
|
97
|
+
if (!isRecord(entry)) {
|
|
98
|
+
return line;
|
|
99
|
+
}
|
|
100
|
+
rewriteEntryCwd(entry, fromCwd, toCwd);
|
|
101
|
+
return JSON.stringify(entry);
|
|
102
|
+
} catch {
|
|
103
|
+
return line;
|
|
104
|
+
}
|
|
105
|
+
}).join("\n");
|
|
106
|
+
};
|
|
107
|
+
var transferClaudeSession = (jsonl, fromCwd, toCwd) => rewriteSessionCwd(jsonl, fromCwd, toCwd);
|
|
108
|
+
var isCodexSessionFilename = (filename, id) => filename.startsWith("rollout-") && filename.endsWith(`-${id}.jsonl`);
|
|
109
|
+
var findCodexSessionOnHost = async (id, sessionsDir) => {
|
|
110
|
+
const root = sessionsDir ?? join(homeDir(), ".codex", "sessions");
|
|
111
|
+
let entries;
|
|
112
|
+
try {
|
|
113
|
+
entries = await readdir(root, { withFileTypes: true });
|
|
114
|
+
} catch {
|
|
115
|
+
return { path: void 0, searchedRoot: root };
|
|
116
|
+
}
|
|
117
|
+
for (const entry of entries) {
|
|
118
|
+
const child = join(root, entry.name);
|
|
119
|
+
if (entry.isFile() && isCodexSessionFilename(entry.name, id)) {
|
|
120
|
+
return { path: child, searchedRoot: root };
|
|
121
|
+
}
|
|
122
|
+
if (entry.isDirectory()) {
|
|
123
|
+
const { path } = await findCodexSessionOnHost(id, child);
|
|
124
|
+
if (path !== void 0) {
|
|
125
|
+
return { path, searchedRoot: root };
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
return { path: void 0, searchedRoot: root };
|
|
130
|
+
};
|
|
131
|
+
var requireHostSessionPath = (found, id) => {
|
|
132
|
+
if (found.path === void 0) {
|
|
133
|
+
throw new Error(`session ${id} not found in ${found.searchedRoot}`);
|
|
134
|
+
}
|
|
135
|
+
return found.path;
|
|
136
|
+
};
|
|
137
|
+
var locateSandboxSessionFile = async (handle, sessionsDir, nameGlob) => {
|
|
138
|
+
const result = await handle.exec(
|
|
139
|
+
`find ${JSON.stringify(sessionsDir)} -type f -name ${JSON.stringify(nameGlob)} -print -quit`
|
|
140
|
+
);
|
|
141
|
+
const [path = ""] = result.stdout.trim().split("\n");
|
|
142
|
+
if (result.exitCode !== 0 || path === "") {
|
|
143
|
+
throw new Error(`session ${nameGlob} not found in ${sessionsDir}`);
|
|
144
|
+
}
|
|
145
|
+
return { path, relativePath: posix.relative(sessionsDir, path) };
|
|
146
|
+
};
|
|
147
|
+
var transferCodexSession = (jsonl, fromCwd, toCwd) => rewriteSessionCwd(jsonl, fromCwd, toCwd);
|
|
148
|
+
var encodePiSessionDir = (cwd) => {
|
|
149
|
+
const stripped = cwd.replace(/^[/\\]/u, "");
|
|
150
|
+
const replaced = stripped.replaceAll(/[/\\:]/gu, "-");
|
|
151
|
+
return `--${replaced}--`;
|
|
152
|
+
};
|
|
153
|
+
var piSessionDirPath = (cwd, sessionsDir) => {
|
|
154
|
+
const base = sessionsDir ?? join(homeDir(), ".pi", "agent", "sessions");
|
|
155
|
+
return join(base, encodePiSessionDir(cwd));
|
|
156
|
+
};
|
|
157
|
+
var isPiSessionFilename = (filename, id) => filename.endsWith(`_${id}.jsonl`);
|
|
158
|
+
var findPiSessionOnHost = async (id, sessionsDir) => {
|
|
159
|
+
const root = sessionsDir ?? join(homeDir(), ".pi", "agent", "sessions");
|
|
160
|
+
let entries;
|
|
161
|
+
try {
|
|
162
|
+
entries = await readdir(root, { withFileTypes: true });
|
|
163
|
+
} catch {
|
|
164
|
+
return { path: void 0, searchedRoot: root };
|
|
165
|
+
}
|
|
166
|
+
for (const entry of entries) {
|
|
167
|
+
if (!entry.isDirectory()) {
|
|
168
|
+
continue;
|
|
169
|
+
}
|
|
170
|
+
const dirAbs = join(root, entry.name);
|
|
171
|
+
let files;
|
|
172
|
+
try {
|
|
173
|
+
files = await readdir(dirAbs);
|
|
174
|
+
} catch {
|
|
175
|
+
continue;
|
|
176
|
+
}
|
|
177
|
+
const match = files.find((name) => isPiSessionFilename(name, id));
|
|
178
|
+
if (match !== void 0) {
|
|
179
|
+
return { path: join(dirAbs, match), searchedRoot: root };
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
return { path: void 0, searchedRoot: root };
|
|
183
|
+
};
|
|
184
|
+
var transferPiSession = (jsonl, fromCwd, toCwd) => {
|
|
185
|
+
if (jsonl === "") {
|
|
186
|
+
return "";
|
|
187
|
+
}
|
|
188
|
+
return jsonl.split("\n").map((line) => {
|
|
189
|
+
if (line === "") {
|
|
190
|
+
return line;
|
|
191
|
+
}
|
|
192
|
+
try {
|
|
193
|
+
const entry = JSON.parse(line);
|
|
194
|
+
if (isRecord(entry) && entry.type === "session" && typeof entry.cwd === "string" && entry.cwd === fromCwd) {
|
|
195
|
+
entry.cwd = toCwd;
|
|
196
|
+
return JSON.stringify(entry);
|
|
197
|
+
}
|
|
198
|
+
return line;
|
|
199
|
+
} catch {
|
|
200
|
+
return line;
|
|
201
|
+
}
|
|
202
|
+
}).join("\n");
|
|
203
|
+
};
|
|
204
|
+
var isRecord = (value) => typeof value === "object" && value !== null;
|
|
205
|
+
var copilotSessionPath = (sessionsDir, id) => posix.join(sessionsDir, id, "events.jsonl");
|
|
206
|
+
var copilotHostSessionsDir = () => join(homeDir(), ".copilot", "session-state");
|
|
207
|
+
var rewriteCopilotEvent = (line, fromCwd, toCwd) => {
|
|
208
|
+
let entry;
|
|
209
|
+
try {
|
|
210
|
+
entry = JSON.parse(line);
|
|
211
|
+
} catch {
|
|
212
|
+
return line;
|
|
213
|
+
}
|
|
214
|
+
const data = isRecord(entry) ? entry["data"] : void 0;
|
|
215
|
+
const context = isRecord(data) ? data["context"] : void 0;
|
|
216
|
+
if (!isRecord(context)) {
|
|
217
|
+
return line;
|
|
218
|
+
}
|
|
219
|
+
const keys = ["cwd", "gitRoot"].filter((key) => context[key] === fromCwd);
|
|
220
|
+
for (const key of keys) {
|
|
221
|
+
context[key] = toCwd;
|
|
222
|
+
}
|
|
223
|
+
return keys.length > 0 ? JSON.stringify(entry) : line;
|
|
224
|
+
};
|
|
225
|
+
var transferCopilotSession = (jsonl, fromCwd, toCwd) => jsonl.split("\n").map(
|
|
226
|
+
(line) => line === "" ? line : rewriteCopilotEvent(line, fromCwd, toCwd)
|
|
227
|
+
).join("\n");
|
|
228
|
+
var cursorChatDirName = (cwd) => createHash("md5").update(cwd).digest("hex");
|
|
229
|
+
var cursorHostChatsDir = () => join(homeDir(), ".cursor", "chats");
|
|
230
|
+
var opencodeHostSessionsDir = () => join(homeDir(), ".local", "share", "opencode", "sandcastle-sessions");
|
|
231
|
+
var opencodeHostDatabase = () => join(homeDir(), ".local", "share", "opencode", "opencode.db");
|
|
232
|
+
var sandboxCommandError = (command, result) => new Error(
|
|
233
|
+
`\`${command}\` exited with code ${result.exitCode}: ${result.stderr.trim()}`
|
|
234
|
+
);
|
|
235
|
+
var captureSessionFile = (layout) => async (transfer) => {
|
|
236
|
+
const { hostCwd, sandboxCwd, sessionId, handle } = transfer;
|
|
237
|
+
const source = layout.sandboxPath(
|
|
238
|
+
sandboxCwd,
|
|
239
|
+
sessionId,
|
|
240
|
+
sandboxDirsOf(handle)
|
|
241
|
+
);
|
|
242
|
+
if (layout.sandboxExport !== void 0) {
|
|
243
|
+
const command = layout.sandboxExport(sessionId, source);
|
|
244
|
+
const result = await handle.exec(command, { cwd: sandboxCwd });
|
|
245
|
+
if (result.exitCode !== 0) {
|
|
246
|
+
throw sandboxCommandError(command, result);
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
const target = layout.hostPath(hostCwd, sessionId);
|
|
250
|
+
await mkdir(dirname(target), { recursive: true });
|
|
251
|
+
await handle.copyFileOut(source, target);
|
|
252
|
+
if (layout.rewrite !== void 0) {
|
|
253
|
+
const content = await readFile(target, "utf8");
|
|
254
|
+
await writeFile(target, layout.rewrite(content, sandboxCwd, hostCwd));
|
|
255
|
+
}
|
|
256
|
+
};
|
|
257
|
+
var resumeSessionFile = (layout) => async (transfer) => {
|
|
258
|
+
const { hostCwd, sandboxCwd, sessionId, handle } = transfer;
|
|
259
|
+
const source = layout.hostPath(hostCwd, sessionId);
|
|
260
|
+
const target = layout.sandboxPath(
|
|
261
|
+
sandboxCwd,
|
|
262
|
+
sessionId,
|
|
263
|
+
sandboxDirsOf(handle)
|
|
264
|
+
);
|
|
265
|
+
const dir = JSON.stringify(posix.dirname(target));
|
|
266
|
+
if (layout.rewrite === void 0) {
|
|
267
|
+
await handle.exec(`mkdir -p ${dir}`);
|
|
268
|
+
await handle.copyFileIn(source, target);
|
|
269
|
+
if (layout.sandboxImport !== void 0) {
|
|
270
|
+
const command = layout.sandboxImport(sessionId, target);
|
|
271
|
+
const result2 = await handle.exec(command, { cwd: sandboxCwd });
|
|
272
|
+
if (result2.exitCode !== 0) {
|
|
273
|
+
throw sandboxCommandError(command, result2);
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
return;
|
|
277
|
+
}
|
|
278
|
+
const content = await readFile(source, "utf8");
|
|
279
|
+
const result = await handle.exec(
|
|
280
|
+
`mkdir -p ${dir} && cat > ${JSON.stringify(target)}`,
|
|
281
|
+
{ stdin: layout.rewrite(content, hostCwd, sandboxCwd) }
|
|
282
|
+
);
|
|
283
|
+
if (result.exitCode !== 0) {
|
|
284
|
+
throw new Error(`writing ${target} failed: ${result.stderr}`);
|
|
285
|
+
}
|
|
286
|
+
};
|
|
287
|
+
var makeSessionFileStorage = (layout) => ({
|
|
288
|
+
hostSessionFilePath: (cwd, id) => layout.hostPath(cwd, id),
|
|
289
|
+
existsOnHost: async (cwd, id) => pathExists(layout.hostPath(cwd, id)),
|
|
290
|
+
readHostSession: async (cwd, id) => {
|
|
291
|
+
const path = layout.hostPath(cwd, id);
|
|
292
|
+
return await pathExists(path) ? readFile(path, "utf8") : void 0;
|
|
293
|
+
},
|
|
294
|
+
findByIdOnHost: async (id) => {
|
|
295
|
+
let subdirs = [];
|
|
296
|
+
try {
|
|
297
|
+
subdirs = await readdir(layout.hostRoot);
|
|
298
|
+
} catch {
|
|
299
|
+
}
|
|
300
|
+
return {
|
|
301
|
+
path: await firstExistingPath(layout.hostPathsById(id, subdirs)),
|
|
302
|
+
searchedRoot: layout.hostRoot
|
|
303
|
+
};
|
|
304
|
+
},
|
|
305
|
+
captureToHost: captureSessionFile(layout),
|
|
306
|
+
resumeIntoSandbox: resumeSessionFile(layout)
|
|
307
|
+
});
|
|
308
|
+
var shellEscape = (s) => `'${s.replaceAll("'", String.raw`'\''`)}'`;
|
|
309
|
+
var contextWindowEvent = (input, ...cacheBuckets) => {
|
|
310
|
+
if (typeof input !== "number") return [];
|
|
311
|
+
let tokens = input;
|
|
312
|
+
for (const n of cacheBuckets) if (typeof n === "number") tokens += n;
|
|
313
|
+
return tokens > 0 ? [{ type: "context_window", contextWindow: { tokens } }] : [];
|
|
314
|
+
};
|
|
315
|
+
var parseClaudeContextWindow = (line) => {
|
|
316
|
+
if (!line.startsWith("{")) return [];
|
|
317
|
+
try {
|
|
318
|
+
const obj = JSON.parse(line);
|
|
319
|
+
if (obj.type !== "assistant" || obj.parent_tool_use_id) return [];
|
|
320
|
+
const u = obj.message?.usage;
|
|
321
|
+
return contextWindowEvent(
|
|
322
|
+
u?.input_tokens,
|
|
323
|
+
u?.cache_creation_input_tokens,
|
|
324
|
+
u?.cache_read_input_tokens
|
|
325
|
+
);
|
|
326
|
+
} catch {
|
|
327
|
+
return [];
|
|
328
|
+
}
|
|
329
|
+
};
|
|
330
|
+
var TOOL_ARG_FIELDS = {
|
|
331
|
+
Bash: "command",
|
|
332
|
+
WebSearch: "query",
|
|
333
|
+
WebFetch: "url",
|
|
334
|
+
Agent: "description",
|
|
335
|
+
Skill: "skill"
|
|
336
|
+
};
|
|
337
|
+
var messageField = (value) => typeof value === "object" && value !== null && "message" in value && typeof value.message === "string" ? value.message : void 0;
|
|
338
|
+
var extractErrorMessage = (obj) => {
|
|
339
|
+
if (typeof obj !== "object" || obj === null) {
|
|
340
|
+
return void 0;
|
|
341
|
+
}
|
|
342
|
+
const err = "error" in obj ? obj.error : void 0;
|
|
343
|
+
if (typeof err === "string") {
|
|
344
|
+
return err;
|
|
345
|
+
}
|
|
346
|
+
if (typeof err === "object" && err !== null) {
|
|
347
|
+
const nested = messageField(err) ?? ("data" in err ? messageField(err.data) : void 0);
|
|
348
|
+
if (nested !== void 0) {
|
|
349
|
+
return nested;
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
return messageField(obj);
|
|
353
|
+
};
|
|
354
|
+
var toolCallFromClaudeBlock = (block) => {
|
|
355
|
+
if (block.type !== "tool_use" || typeof block.name !== "string" || block.input === void 0) {
|
|
356
|
+
return void 0;
|
|
357
|
+
}
|
|
358
|
+
const argField = TOOL_ARG_FIELDS[block.name];
|
|
359
|
+
if (argField === void 0) {
|
|
360
|
+
return void 0;
|
|
361
|
+
}
|
|
362
|
+
const argValue = block.input[argField];
|
|
363
|
+
if (typeof argValue !== "string") {
|
|
364
|
+
return void 0;
|
|
365
|
+
}
|
|
366
|
+
return { type: "tool_call", name: block.name, args: argValue };
|
|
367
|
+
};
|
|
368
|
+
var mergeAdjacentText = (events) => {
|
|
369
|
+
const merged = [];
|
|
370
|
+
for (const event of events) {
|
|
371
|
+
const last = merged.at(-1);
|
|
372
|
+
if (event.type === "text" && last?.type === "text") {
|
|
373
|
+
merged[merged.length - 1] = {
|
|
374
|
+
type: "text",
|
|
375
|
+
text: last.text + event.text
|
|
376
|
+
};
|
|
377
|
+
} else {
|
|
378
|
+
merged.push(event);
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
return merged;
|
|
382
|
+
};
|
|
383
|
+
var parseClaudeAssistantContent = (content) => mergeAdjacentText(
|
|
384
|
+
content.flatMap((block) => {
|
|
385
|
+
if (block.type === "text" && typeof block.text === "string") {
|
|
386
|
+
return [{ type: "text", text: block.text }];
|
|
387
|
+
}
|
|
388
|
+
const toolCall = toolCallFromClaudeBlock(block);
|
|
389
|
+
return toolCall === void 0 ? [] : [toolCall];
|
|
390
|
+
})
|
|
391
|
+
);
|
|
392
|
+
var parseStreamJsonLine = (line) => {
|
|
393
|
+
if (!line.startsWith("{")) return [];
|
|
394
|
+
try {
|
|
395
|
+
const obj = JSON.parse(line);
|
|
396
|
+
if (obj.type === "assistant" && Array.isArray(obj.message?.content)) {
|
|
397
|
+
return parseClaudeAssistantContent(
|
|
398
|
+
obj.message.content
|
|
399
|
+
);
|
|
400
|
+
}
|
|
401
|
+
if (obj.type === "result" && typeof obj.result === "string") {
|
|
402
|
+
if (obj.is_error === true) {
|
|
403
|
+
return [{ type: "error", message: obj.result }];
|
|
404
|
+
}
|
|
405
|
+
return [{ type: "result", result: obj.result }];
|
|
406
|
+
}
|
|
407
|
+
if (obj.type === "system" && obj.subtype === "init" && typeof obj.session_id === "string") {
|
|
408
|
+
return [{ type: "session_id", sessionId: obj.session_id }];
|
|
409
|
+
}
|
|
410
|
+
} catch {
|
|
411
|
+
}
|
|
412
|
+
return [];
|
|
413
|
+
};
|
|
414
|
+
var CURSOR_PRINT_PROMPT_MAX_BYTES = 120 * 1024;
|
|
415
|
+
var assertCursorPrintPromptFitsArgv = (prompt) => {
|
|
416
|
+
const n = Buffer.byteLength(prompt, "utf8");
|
|
417
|
+
if (n > CURSOR_PRINT_PROMPT_MAX_BYTES) {
|
|
418
|
+
throw new Error(
|
|
419
|
+
`Cursor print-mode prompt is ${n} bytes (max ${CURSOR_PRINT_PROMPT_MAX_BYTES} bytes). The Cursor CLI accepts the prompt only as a command-line argument; shorten the prompt or split the work. Other Sandcastle providers use stdin for large prompts.`
|
|
420
|
+
);
|
|
421
|
+
}
|
|
422
|
+
};
|
|
423
|
+
var cursorToolCallArg = (toolCall, field) => {
|
|
424
|
+
if (typeof toolCall !== "object" || toolCall === null) {
|
|
425
|
+
return void 0;
|
|
426
|
+
}
|
|
427
|
+
const args = Reflect.get(toolCall, "args");
|
|
428
|
+
if (typeof args !== "object" || args === null) {
|
|
429
|
+
return void 0;
|
|
430
|
+
}
|
|
431
|
+
const value = Reflect.get(args, field);
|
|
432
|
+
return typeof value === "string" ? value : void 0;
|
|
433
|
+
};
|
|
434
|
+
var CURSOR_TYPED_TOOL_CALLS = [
|
|
435
|
+
{ kind: "readToolCall", name: "Read", field: "path" },
|
|
436
|
+
{ kind: "writeToolCall", name: "Write", field: "path" },
|
|
437
|
+
{ kind: "editToolCall", name: "Edit", field: "path" },
|
|
438
|
+
{ kind: "shellToolCall", name: "Bash", field: "command" }
|
|
439
|
+
];
|
|
440
|
+
var parseCursorFunctionToolCall = (fn) => {
|
|
441
|
+
if (typeof fn !== "object" || fn === null) {
|
|
442
|
+
return [];
|
|
443
|
+
}
|
|
444
|
+
const name = Reflect.get(fn, "name");
|
|
445
|
+
if (typeof name !== "string") {
|
|
446
|
+
return [];
|
|
447
|
+
}
|
|
448
|
+
const rawArguments = Reflect.get(fn, "arguments");
|
|
449
|
+
const rawArgs = typeof rawArguments === "string" ? rawArguments : "";
|
|
450
|
+
if (rawArgs === "") {
|
|
451
|
+
return [{ type: "tool_call", name, args: "" }];
|
|
452
|
+
}
|
|
453
|
+
try {
|
|
454
|
+
const parsedArgs = JSON.parse(rawArgs);
|
|
455
|
+
const command = typeof parsedArgs === "object" && parsedArgs !== null ? Reflect.get(parsedArgs, "command") : void 0;
|
|
456
|
+
if (typeof command === "string") {
|
|
457
|
+
return [{ type: "tool_call", name: "Bash", args: command }];
|
|
458
|
+
}
|
|
459
|
+
} catch {
|
|
460
|
+
}
|
|
461
|
+
return [{ type: "tool_call", name, args: rawArgs }];
|
|
462
|
+
};
|
|
463
|
+
var parseCursorToolCallStarted = (obj) => {
|
|
464
|
+
if (obj.type !== "tool_call" || obj.subtype !== "started") {
|
|
465
|
+
return [];
|
|
466
|
+
}
|
|
467
|
+
const toolCall = obj.tool_call;
|
|
468
|
+
if (typeof toolCall !== "object" || toolCall === null) {
|
|
469
|
+
return [];
|
|
470
|
+
}
|
|
471
|
+
for (const { kind, name, field } of CURSOR_TYPED_TOOL_CALLS) {
|
|
472
|
+
const args = cursorToolCallArg(Reflect.get(toolCall, kind), field);
|
|
473
|
+
if (args !== void 0) {
|
|
474
|
+
return [{ type: "tool_call", name, args }];
|
|
475
|
+
}
|
|
476
|
+
}
|
|
477
|
+
return parseCursorFunctionToolCall(Reflect.get(toolCall, "function"));
|
|
478
|
+
};
|
|
479
|
+
var parseCursorStreamLine = (line) => {
|
|
480
|
+
if (!line.startsWith("{")) return [];
|
|
481
|
+
let obj;
|
|
482
|
+
try {
|
|
483
|
+
obj = JSON.parse(line);
|
|
484
|
+
} catch {
|
|
485
|
+
return [];
|
|
486
|
+
}
|
|
487
|
+
if (obj.type === "tool_call") {
|
|
488
|
+
return parseCursorToolCallStarted(obj);
|
|
489
|
+
}
|
|
490
|
+
return parseStreamJsonLine(line);
|
|
491
|
+
};
|
|
492
|
+
var readSandboxFile = async (handle, sandboxPath, tag) => {
|
|
493
|
+
const tmpPath = join(
|
|
494
|
+
tmpdir(),
|
|
495
|
+
`sandcastle-${tag}-${Date.now()}-${Math.random().toString(36).slice(2)}.jsonl`
|
|
496
|
+
);
|
|
497
|
+
await handle.copyFileOut(sandboxPath, tmpPath);
|
|
498
|
+
try {
|
|
499
|
+
return await readFile(tmpPath, "utf8");
|
|
500
|
+
} finally {
|
|
501
|
+
await rm(tmpPath, { force: true }).catch(() => {
|
|
502
|
+
});
|
|
503
|
+
}
|
|
504
|
+
};
|
|
505
|
+
var writeSandboxFile = async (handle, { sandboxPath, content, tag }) => {
|
|
506
|
+
const tmpPath = join(
|
|
507
|
+
tmpdir(),
|
|
508
|
+
`sandcastle-${tag}-${Date.now()}-${Math.random().toString(36).slice(2)}.jsonl`
|
|
509
|
+
);
|
|
510
|
+
await writeFile(tmpPath, content);
|
|
511
|
+
try {
|
|
512
|
+
await handle.exec(`mkdir -p ${JSON.stringify(posix.dirname(sandboxPath))}`);
|
|
513
|
+
await handle.copyFileIn(tmpPath, sandboxPath);
|
|
514
|
+
} finally {
|
|
515
|
+
await rm(tmpPath, { force: true }).catch(() => {
|
|
516
|
+
});
|
|
517
|
+
}
|
|
518
|
+
};
|
|
519
|
+
var copyClaudeSessionFile = async ({
|
|
520
|
+
handle,
|
|
521
|
+
sourcePath,
|
|
522
|
+
fromCwd,
|
|
523
|
+
toCwd,
|
|
524
|
+
destPath,
|
|
525
|
+
tag
|
|
526
|
+
}) => {
|
|
527
|
+
const jsonl = await readSandboxFile(handle, sourcePath, tag);
|
|
528
|
+
const rewritten = transferClaudeSession(jsonl, fromCwd, toCwd);
|
|
529
|
+
await mkdir(dirname(destPath), { recursive: true });
|
|
530
|
+
await writeFile(destPath, rewritten);
|
|
531
|
+
};
|
|
532
|
+
var captureClaudeSubagents = async ({
|
|
533
|
+
handle,
|
|
534
|
+
hostCwd,
|
|
535
|
+
sandboxCwd,
|
|
536
|
+
sessionId,
|
|
537
|
+
sandboxProjectsDir,
|
|
538
|
+
hostProjectsDir
|
|
539
|
+
}) => {
|
|
540
|
+
const subagentSandboxPaths = await listClaudeSubagentSessionsInSandbox(
|
|
541
|
+
sandboxCwd,
|
|
542
|
+
sessionId,
|
|
543
|
+
handle,
|
|
544
|
+
sandboxProjectsDir
|
|
545
|
+
);
|
|
546
|
+
const hostSubagentsDir = claudeSubagentsDirOnHost(
|
|
547
|
+
hostCwd,
|
|
548
|
+
sessionId,
|
|
549
|
+
hostProjectsDir
|
|
550
|
+
);
|
|
551
|
+
for (const sandboxSubagentPath of subagentSandboxPaths) {
|
|
552
|
+
try {
|
|
553
|
+
await copyClaudeSessionFile({
|
|
554
|
+
handle,
|
|
555
|
+
sourcePath: sandboxSubagentPath,
|
|
556
|
+
fromCwd: sandboxCwd,
|
|
557
|
+
toCwd: hostCwd,
|
|
558
|
+
destPath: join(hostSubagentsDir, posix.basename(sandboxSubagentPath)),
|
|
559
|
+
tag: "claude-sub"
|
|
560
|
+
});
|
|
561
|
+
} catch (error) {
|
|
562
|
+
console.error(
|
|
563
|
+
`sandcastle: failed to capture Claude subagent transcript ${sandboxSubagentPath}: ${error instanceof Error ? error.message : String(error)}`
|
|
564
|
+
);
|
|
565
|
+
}
|
|
566
|
+
}
|
|
567
|
+
};
|
|
568
|
+
var resumeClaudeIntoSandbox = async ({ hostCwd, sandboxCwd, sessionId, handle }, dirs) => {
|
|
569
|
+
const hostPath = claudeHostSessionPath(
|
|
570
|
+
hostCwd,
|
|
571
|
+
sessionId,
|
|
572
|
+
dirs.hostProjectsDir
|
|
573
|
+
);
|
|
574
|
+
const jsonl = await readFile(hostPath, "utf8");
|
|
575
|
+
await writeSandboxFile(handle, {
|
|
576
|
+
sandboxPath: claudeSandboxSessionPath(
|
|
577
|
+
sandboxCwd,
|
|
578
|
+
sessionId,
|
|
579
|
+
dirs.sandboxProjectsDir
|
|
580
|
+
),
|
|
581
|
+
content: transferClaudeSession(jsonl, hostCwd, sandboxCwd),
|
|
582
|
+
tag: "claude-res"
|
|
583
|
+
});
|
|
584
|
+
};
|
|
585
|
+
var captureClaudeSession = (capture) => (
|
|
586
|
+
// Main session: failure is fatal — the user expects their session.
|
|
587
|
+
copyClaudeSessionFile({
|
|
588
|
+
handle: capture.handle,
|
|
589
|
+
sourcePath: claudeSandboxSessionPath(
|
|
590
|
+
capture.sandboxCwd,
|
|
591
|
+
capture.sessionId,
|
|
592
|
+
capture.sandboxProjectsDir
|
|
593
|
+
),
|
|
594
|
+
fromCwd: capture.sandboxCwd,
|
|
595
|
+
toCwd: capture.hostCwd,
|
|
596
|
+
destPath: claudeHostSessionPath(
|
|
597
|
+
capture.hostCwd,
|
|
598
|
+
capture.sessionId,
|
|
599
|
+
capture.hostProjectsDir
|
|
600
|
+
),
|
|
601
|
+
tag: "claude-cap"
|
|
602
|
+
}).then(() => captureClaudeSubagents(capture))
|
|
603
|
+
);
|
|
604
|
+
var makeClaudeSessionStorage = (options) => {
|
|
605
|
+
const hostProjectsDir = options?.sessionStorage?.hostProjectsDir;
|
|
606
|
+
const sandboxProjectsDirOf = (handle) => options?.sessionStorage?.sandboxProjectsDir ?? posix.join(sandboxDirsOf(handle).home, ".claude", "projects");
|
|
607
|
+
return {
|
|
608
|
+
hostSessionFilePath: (cwd, id) => claudeHostSessionPath(cwd, id, hostProjectsDir),
|
|
609
|
+
existsOnHost: (cwd, id) => pathExists(claudeHostSessionPath(cwd, id, hostProjectsDir)),
|
|
610
|
+
readHostSession: async (cwd, id) => {
|
|
611
|
+
const path = claudeHostSessionPath(cwd, id, hostProjectsDir);
|
|
612
|
+
if (!await pathExists(path)) return void 0;
|
|
613
|
+
return readFile(path, "utf8");
|
|
614
|
+
},
|
|
615
|
+
captureToHost: ({ hostCwd, sandboxCwd, sessionId, handle }) => captureClaudeSession({
|
|
616
|
+
handle,
|
|
617
|
+
hostCwd,
|
|
618
|
+
sandboxCwd,
|
|
619
|
+
sessionId,
|
|
620
|
+
sandboxProjectsDir: sandboxProjectsDirOf(handle),
|
|
621
|
+
hostProjectsDir
|
|
622
|
+
}),
|
|
623
|
+
resumeIntoSandbox: (transfer) => resumeClaudeIntoSandbox(transfer, {
|
|
624
|
+
hostProjectsDir,
|
|
625
|
+
sandboxProjectsDir: sandboxProjectsDirOf(transfer.handle)
|
|
626
|
+
}),
|
|
627
|
+
findByIdOnHost: (id) => findClaudeSessionOnHost(id, hostProjectsDir)
|
|
628
|
+
};
|
|
629
|
+
};
|
|
630
|
+
var resumeCodexIntoSandbox = async ({ hostCwd, sandboxCwd, sessionId, handle }, dirs) => {
|
|
631
|
+
const found = await findCodexSessionOnHost(sessionId, dirs.hostSessionsDir);
|
|
632
|
+
const path = requireHostSessionPath(found, sessionId);
|
|
633
|
+
const jsonl = await readFile(path, "utf8");
|
|
634
|
+
await writeSandboxFile(handle, {
|
|
635
|
+
sandboxPath: posix.join(
|
|
636
|
+
dirs.sandboxSessionsDir,
|
|
637
|
+
...relative(found.searchedRoot, path).split(/[\\/]/u)
|
|
638
|
+
),
|
|
639
|
+
content: transferCodexSession(jsonl, hostCwd, sandboxCwd),
|
|
640
|
+
tag: "codex-res"
|
|
641
|
+
});
|
|
642
|
+
};
|
|
643
|
+
var makeCodexSessionStorage = (options) => {
|
|
644
|
+
const hostSessionsDir = options?.sessionStorage?.hostSessionsDir;
|
|
645
|
+
const sandboxSessionsDirOf = (handle) => options?.sessionStorage?.sandboxSessionsDir ?? posix.join(sandboxDirsOf(handle).home, ".codex", "sessions");
|
|
646
|
+
const capturedPaths = /* @__PURE__ */ new Map();
|
|
647
|
+
return {
|
|
648
|
+
hostSessionFilePath: (_cwd, id) => capturedPaths.get(id),
|
|
649
|
+
existsOnHost: async (_cwd, id) => {
|
|
650
|
+
const found = await findCodexSessionOnHost(id, hostSessionsDir);
|
|
651
|
+
return found.path !== void 0;
|
|
652
|
+
},
|
|
653
|
+
readHostSession: async (_cwd, id) => {
|
|
654
|
+
const found = await findCodexSessionOnHost(id, hostSessionsDir);
|
|
655
|
+
if (!found.path) return void 0;
|
|
656
|
+
return readFile(found.path, "utf8");
|
|
657
|
+
},
|
|
658
|
+
captureToHost: async ({ hostCwd, sandboxCwd, sessionId, handle }) => {
|
|
659
|
+
const located = await locateSandboxSessionFile(
|
|
660
|
+
handle,
|
|
661
|
+
sandboxSessionsDirOf(handle),
|
|
662
|
+
`rollout-*-${sessionId}.jsonl`
|
|
663
|
+
);
|
|
664
|
+
const jsonl = await readSandboxFile(handle, located.path, "codex-cap");
|
|
665
|
+
const rewritten = transferCodexSession(jsonl, sandboxCwd, hostCwd);
|
|
666
|
+
const root = hostSessionsDir ?? join(homeDir(), ".codex", "sessions");
|
|
667
|
+
const target = join(root, located.relativePath);
|
|
668
|
+
await mkdir(dirname(target), { recursive: true });
|
|
669
|
+
await writeFile(target, rewritten);
|
|
670
|
+
capturedPaths.set(sessionId, target);
|
|
671
|
+
},
|
|
672
|
+
resumeIntoSandbox: (transfer) => resumeCodexIntoSandbox(transfer, {
|
|
673
|
+
hostSessionsDir,
|
|
674
|
+
sandboxSessionsDir: sandboxSessionsDirOf(transfer.handle)
|
|
675
|
+
}),
|
|
676
|
+
findByIdOnHost: (id) => findCodexSessionOnHost(id, hostSessionsDir)
|
|
677
|
+
};
|
|
678
|
+
};
|
|
679
|
+
var piHostCaptureTarget = (hostCwd, sandboxPath, hostSessionsDir) => join(piSessionDirPath(hostCwd, hostSessionsDir), posix.basename(sandboxPath));
|
|
680
|
+
var resumePiIntoSandbox = async ({ hostCwd, sandboxCwd, sessionId, handle }, dirs) => {
|
|
681
|
+
const found = await findPiSessionOnHost(sessionId, dirs.hostSessionsDir);
|
|
682
|
+
const path = requireHostSessionPath(found, sessionId);
|
|
683
|
+
const jsonl = await readFile(path, "utf8");
|
|
684
|
+
const filename = posix.basename(path.replaceAll("\\", "/"));
|
|
685
|
+
await writeSandboxFile(handle, {
|
|
686
|
+
sandboxPath: posix.join(
|
|
687
|
+
dirs.sandboxSessionsDir,
|
|
688
|
+
encodePiSessionDir(sandboxCwd),
|
|
689
|
+
filename
|
|
690
|
+
),
|
|
691
|
+
content: transferPiSession(jsonl, hostCwd, sandboxCwd),
|
|
692
|
+
tag: "pi-res"
|
|
693
|
+
});
|
|
694
|
+
};
|
|
695
|
+
var makePiSessionStorage = (options) => {
|
|
696
|
+
const hostSessionsDir = options?.sessionStorage?.hostSessionsDir;
|
|
697
|
+
const sandboxSessionsDirOf = (handle) => options?.sessionStorage?.sandboxSessionsDir ?? posix.join(sandboxDirsOf(handle).home, ".pi", "agent", "sessions");
|
|
698
|
+
const capturedPaths = /* @__PURE__ */ new Map();
|
|
699
|
+
return {
|
|
700
|
+
hostSessionFilePath: (cwd, id) => capturedPaths.get(id) ?? piSessionDirPath(cwd, hostSessionsDir),
|
|
701
|
+
existsOnHost: async (_cwd, id) => {
|
|
702
|
+
const found = await findPiSessionOnHost(id, hostSessionsDir);
|
|
703
|
+
return found.path !== void 0;
|
|
704
|
+
},
|
|
705
|
+
readHostSession: async (_cwd, id) => {
|
|
706
|
+
const { path } = await findPiSessionOnHost(id, hostSessionsDir);
|
|
707
|
+
return path === void 0 ? void 0 : readFile(path, "utf8");
|
|
708
|
+
},
|
|
709
|
+
captureToHost: async ({ hostCwd, sandboxCwd, sessionId, handle }) => {
|
|
710
|
+
const located = await locateSandboxSessionFile(
|
|
711
|
+
handle,
|
|
712
|
+
sandboxSessionsDirOf(handle),
|
|
713
|
+
`*_${sessionId}.jsonl`
|
|
714
|
+
);
|
|
715
|
+
const jsonl = await readSandboxFile(handle, located.path, "pi-cap");
|
|
716
|
+
const rewritten = transferPiSession(jsonl, sandboxCwd, hostCwd);
|
|
717
|
+
const target = piHostCaptureTarget(
|
|
718
|
+
hostCwd,
|
|
719
|
+
located.path,
|
|
720
|
+
hostSessionsDir
|
|
721
|
+
);
|
|
722
|
+
await mkdir(dirname(target), { recursive: true });
|
|
723
|
+
await writeFile(target, rewritten);
|
|
724
|
+
capturedPaths.set(sessionId, target);
|
|
725
|
+
},
|
|
726
|
+
resumeIntoSandbox: (transfer) => resumePiIntoSandbox(transfer, {
|
|
727
|
+
hostSessionsDir,
|
|
728
|
+
sandboxSessionsDir: sandboxSessionsDirOf(transfer.handle)
|
|
729
|
+
}),
|
|
730
|
+
findByIdOnHost: (id) => findPiSessionOnHost(id, hostSessionsDir)
|
|
731
|
+
};
|
|
732
|
+
};
|
|
733
|
+
var lastAssistantMessage = (messages) => {
|
|
734
|
+
for (let i = messages.length - 1; i >= 0; i--) {
|
|
735
|
+
const msg = messages[i];
|
|
736
|
+
if (msg?.role === "assistant") {
|
|
737
|
+
return msg;
|
|
738
|
+
}
|
|
739
|
+
}
|
|
740
|
+
return null;
|
|
741
|
+
};
|
|
742
|
+
var parsePiAgentEnd = (messages) => {
|
|
743
|
+
const msg = lastAssistantMessage(messages);
|
|
744
|
+
if (msg === null) {
|
|
745
|
+
return [];
|
|
746
|
+
}
|
|
747
|
+
if (msg.stopReason === "error") {
|
|
748
|
+
return [
|
|
749
|
+
{
|
|
750
|
+
type: "error",
|
|
751
|
+
message: typeof msg.errorMessage === "string" && msg.errorMessage ? msg.errorMessage : 'pi stopped with stopReason "error"'
|
|
752
|
+
}
|
|
753
|
+
];
|
|
754
|
+
}
|
|
755
|
+
const texts = msg.content.flatMap(
|
|
756
|
+
(block) => block.type === "text" && typeof block.text === "string" ? [block.text] : []
|
|
757
|
+
);
|
|
758
|
+
return texts.length > 0 ? [{ type: "result", result: texts.join("") }] : [];
|
|
759
|
+
};
|
|
760
|
+
var parsePiUsage = (u) => {
|
|
761
|
+
if (typeof u !== "object" || u === null) {
|
|
762
|
+
return void 0;
|
|
763
|
+
}
|
|
764
|
+
const field = (key) => Reflect.get(u, key);
|
|
765
|
+
const input = field("input");
|
|
766
|
+
const output = field("output");
|
|
767
|
+
const cacheRead = field("cacheRead");
|
|
768
|
+
const cacheWrite = field("cacheWrite");
|
|
769
|
+
if (typeof input !== "number" || typeof output !== "number" || typeof cacheRead !== "number" || typeof cacheWrite !== "number") {
|
|
770
|
+
return void 0;
|
|
771
|
+
}
|
|
772
|
+
return {
|
|
773
|
+
inputTokens: input,
|
|
774
|
+
cacheCreationInputTokens: cacheWrite,
|
|
775
|
+
cacheReadInputTokens: cacheRead,
|
|
776
|
+
outputTokens: output
|
|
777
|
+
};
|
|
778
|
+
};
|
|
779
|
+
var parsePiStreamLine = (line) => {
|
|
780
|
+
if (!line.startsWith("{")) return [];
|
|
781
|
+
try {
|
|
782
|
+
const obj = JSON.parse(line);
|
|
783
|
+
if (obj.type === "session" && typeof obj.id === "string") {
|
|
784
|
+
return [{ type: "session_id", sessionId: obj.id }];
|
|
785
|
+
}
|
|
786
|
+
if (obj.type === "message_end" && obj.message?.role === "assistant") {
|
|
787
|
+
const u = obj.message.usage;
|
|
788
|
+
const usage = parsePiUsage(u);
|
|
789
|
+
return [
|
|
790
|
+
...contextWindowEvent(u?.input, u?.cacheRead, u?.cacheWrite),
|
|
791
|
+
...usage ? [{ type: "usage", usage }] : []
|
|
792
|
+
];
|
|
793
|
+
}
|
|
794
|
+
if (obj.type === "message_update" && obj.assistantMessageEvent) {
|
|
795
|
+
const evt = obj.assistantMessageEvent;
|
|
796
|
+
if (evt.type === "text_delta" && typeof evt.delta === "string") {
|
|
797
|
+
return [{ type: "text", text: evt.delta }];
|
|
798
|
+
}
|
|
799
|
+
return [];
|
|
800
|
+
}
|
|
801
|
+
if (obj.type === "tool_execution_start") {
|
|
802
|
+
const rawName = obj.toolName;
|
|
803
|
+
if (typeof rawName !== "string") return [];
|
|
804
|
+
const toolName = Object.keys(TOOL_ARG_FIELDS).find(
|
|
805
|
+
(name) => name.toLowerCase() === rawName.toLowerCase()
|
|
806
|
+
);
|
|
807
|
+
if (toolName === void 0) return [];
|
|
808
|
+
const argField = TOOL_ARG_FIELDS[toolName];
|
|
809
|
+
if (argField === void 0) return [];
|
|
810
|
+
const args = obj.args;
|
|
811
|
+
if (!args) return [];
|
|
812
|
+
const argValue = args[argField];
|
|
813
|
+
if (typeof argValue !== "string") return [];
|
|
814
|
+
return [{ type: "tool_call", name: toolName, args: argValue }];
|
|
815
|
+
}
|
|
816
|
+
if (obj.type === "agent_error" || obj.type === "error") {
|
|
817
|
+
const msg = extractErrorMessage(obj);
|
|
818
|
+
return msg ? [{ type: "error", message: msg }] : [];
|
|
819
|
+
}
|
|
820
|
+
if (obj.type === "agent_end" && Array.isArray(obj.messages)) {
|
|
821
|
+
return parsePiAgentEnd(obj.messages);
|
|
822
|
+
}
|
|
823
|
+
} catch {
|
|
824
|
+
}
|
|
825
|
+
return [];
|
|
826
|
+
};
|
|
827
|
+
var pi = (model, options) => ({
|
|
828
|
+
name: "pi",
|
|
829
|
+
env: options?.env ?? {},
|
|
830
|
+
captureSessions: options?.captureSessions ?? true,
|
|
831
|
+
sessionStorage: makePiSessionStorage(options),
|
|
832
|
+
buildPrintCommand({
|
|
833
|
+
prompt,
|
|
834
|
+
resumeSession,
|
|
835
|
+
forkSession
|
|
836
|
+
}) {
|
|
837
|
+
const thinkingFlag = options?.thinking ? ` --thinking ${options.thinking}` : "";
|
|
838
|
+
const sessionFlag = resumeSession ? ` --${forkSession ? "fork" : "session"} ${shellEscape(resumeSession)}` : "";
|
|
839
|
+
return {
|
|
840
|
+
command: `pi -p --mode json --model ${shellEscape(model)}${thinkingFlag}${sessionFlag}`,
|
|
841
|
+
stdin: prompt
|
|
842
|
+
};
|
|
843
|
+
},
|
|
844
|
+
buildInteractiveArgs({ prompt }) {
|
|
845
|
+
const args = ["pi", "--model", model];
|
|
846
|
+
if (prompt) args.push(prompt);
|
|
847
|
+
return args;
|
|
848
|
+
},
|
|
849
|
+
parseStreamLine(line) {
|
|
850
|
+
return parsePiStreamLine(line);
|
|
851
|
+
}
|
|
852
|
+
});
|
|
853
|
+
var parseCodexUsage = (usage) => {
|
|
854
|
+
if (typeof usage !== "object" || usage === null) return void 0;
|
|
855
|
+
const u = usage;
|
|
856
|
+
if (typeof u.input_tokens !== "number" || typeof u.cached_input_tokens !== "number" || typeof u.output_tokens !== "number") {
|
|
857
|
+
return void 0;
|
|
858
|
+
}
|
|
859
|
+
return {
|
|
860
|
+
inputTokens: u.input_tokens - u.cached_input_tokens,
|
|
861
|
+
cacheCreationInputTokens: 0,
|
|
862
|
+
cacheReadInputTokens: u.cached_input_tokens,
|
|
863
|
+
outputTokens: u.output_tokens
|
|
864
|
+
};
|
|
865
|
+
};
|
|
866
|
+
var parseCodexSessionContextWindow = (content) => {
|
|
867
|
+
const lines = content.split("\n");
|
|
868
|
+
for (let i = lines.length - 1; i >= 0; i--) {
|
|
869
|
+
const line = lines[i];
|
|
870
|
+
if (!line.startsWith("{")) continue;
|
|
871
|
+
try {
|
|
872
|
+
const obj = JSON.parse(line);
|
|
873
|
+
if (obj.type !== "event_msg" || obj.payload?.type !== "token_count") {
|
|
874
|
+
continue;
|
|
875
|
+
}
|
|
876
|
+
const { info } = obj.payload;
|
|
877
|
+
const tokens = info?.last_token_usage?.input_tokens;
|
|
878
|
+
if (typeof tokens !== "number" || tokens <= 0) continue;
|
|
879
|
+
const limit = info.model_context_window;
|
|
880
|
+
return typeof limit === "number" && limit > 0 ? { tokens, limit } : { tokens };
|
|
881
|
+
} catch {
|
|
882
|
+
}
|
|
883
|
+
}
|
|
884
|
+
return void 0;
|
|
885
|
+
};
|
|
886
|
+
var parseCodexStreamLine = (line) => {
|
|
887
|
+
if (!line.startsWith("{")) return [];
|
|
888
|
+
try {
|
|
889
|
+
const obj = JSON.parse(line);
|
|
890
|
+
if (obj.type === "thread.started" && typeof obj.thread_id === "string") {
|
|
891
|
+
return [{ type: "session_id", sessionId: obj.thread_id }];
|
|
892
|
+
}
|
|
893
|
+
if (obj.type === "item.completed" && obj.item?.type === "agent_message" && typeof obj.item.text === "string") {
|
|
894
|
+
const { text } = obj.item;
|
|
895
|
+
return [
|
|
896
|
+
{ type: "text", text },
|
|
897
|
+
{ type: "result", result: text }
|
|
898
|
+
];
|
|
899
|
+
}
|
|
900
|
+
if (obj.type === "item.started" && obj.item?.type === "command_execution" && typeof obj.item.command === "string") {
|
|
901
|
+
return [{ type: "tool_call", name: "Bash", args: obj.item.command }];
|
|
902
|
+
}
|
|
903
|
+
if (obj.type === "error") {
|
|
904
|
+
const msg = extractErrorMessage(obj);
|
|
905
|
+
return msg ? [{ type: "result", result: msg }] : [];
|
|
906
|
+
}
|
|
907
|
+
if (obj.type === "turn.failed") {
|
|
908
|
+
const msg = extractErrorMessage(obj);
|
|
909
|
+
return [{ type: "error", message: msg ?? "codex turn failed" }];
|
|
910
|
+
}
|
|
911
|
+
if (obj.type === "turn.completed") {
|
|
912
|
+
const usage = parseCodexUsage(obj.usage);
|
|
913
|
+
return usage ? [{ type: "usage", usage }] : [];
|
|
914
|
+
}
|
|
915
|
+
} catch {
|
|
916
|
+
}
|
|
917
|
+
return [];
|
|
918
|
+
};
|
|
919
|
+
var buildCodexPrintCommand = (model, options, { prompt, resumeSession, forkSession }) => {
|
|
920
|
+
const effortFlag = options?.effort ? ` -c ${shellEscape(`model_reasoning_effort="${options.effort}"`)}` : "";
|
|
921
|
+
const approvalsFlags = options?.approvalsReviewer === "auto_review" ? ` -a on-request -s danger-full-access -c ${shellEscape(`approvals_reviewer="auto_review"`)}` : " --dangerously-bypass-approvals-and-sandbox";
|
|
922
|
+
let base;
|
|
923
|
+
if (resumeSession && forkSession) {
|
|
924
|
+
base = `codex exec fork ${shellEscape(resumeSession)}`;
|
|
925
|
+
} else if (resumeSession) {
|
|
926
|
+
base = `codex exec resume ${shellEscape(resumeSession)}`;
|
|
927
|
+
} else {
|
|
928
|
+
base = "codex exec";
|
|
929
|
+
}
|
|
930
|
+
const stdinArg = resumeSession ? " -" : "";
|
|
931
|
+
return {
|
|
932
|
+
command: `${base} --json${approvalsFlags} -m ${shellEscape(model)}${effortFlag}${stdinArg}`,
|
|
933
|
+
stdin: prompt
|
|
934
|
+
};
|
|
935
|
+
};
|
|
936
|
+
var codex = (model, options) => ({
|
|
937
|
+
name: "codex",
|
|
938
|
+
env: options?.env ?? {},
|
|
939
|
+
captureSessions: options?.captureSessions ?? true,
|
|
940
|
+
sessionStorage: makeCodexSessionStorage(options),
|
|
941
|
+
buildPrintCommand(commandOptions) {
|
|
942
|
+
return buildCodexPrintCommand(model, options, commandOptions);
|
|
943
|
+
},
|
|
944
|
+
buildInteractiveArgs({ prompt }) {
|
|
945
|
+
const args = ["codex", "--model", model];
|
|
946
|
+
if (prompt) args.push(prompt);
|
|
947
|
+
return args;
|
|
948
|
+
},
|
|
949
|
+
parseStreamLine(line) {
|
|
950
|
+
return parseCodexStreamLine(line);
|
|
951
|
+
},
|
|
952
|
+
parseSessionContextWindow(content) {
|
|
953
|
+
return parseCodexSessionContextWindow(content);
|
|
954
|
+
}
|
|
955
|
+
});
|
|
956
|
+
var makeCursorSessionStorage = (options) => {
|
|
957
|
+
const hostDir = options?.sessionStorage?.hostChatsDir ?? cursorHostChatsDir();
|
|
958
|
+
const sandboxDirOverride = options?.sessionStorage?.sandboxChatsDir;
|
|
959
|
+
return {
|
|
960
|
+
...makeSessionFileStorage({
|
|
961
|
+
hostRoot: hostDir,
|
|
962
|
+
hostPath: (cwd, id) => join(hostDir, cursorChatDirName(cwd), id, "store.db"),
|
|
963
|
+
hostPathsById: (id, chatDirs) => chatDirs.map((chatDir) => join(hostDir, chatDir, id, "store.db")),
|
|
964
|
+
sandboxPath: (cwd, id, dirs) => posix.join(
|
|
965
|
+
sandboxDirOverride ?? posix.join(dirs.home, ".cursor", "chats"),
|
|
966
|
+
cursorChatDirName(cwd),
|
|
967
|
+
id,
|
|
968
|
+
"store.db"
|
|
969
|
+
)
|
|
970
|
+
}),
|
|
971
|
+
canFork: false
|
|
972
|
+
};
|
|
973
|
+
};
|
|
974
|
+
var cursor = (model, options) => ({
|
|
975
|
+
name: "cursor",
|
|
976
|
+
env: options?.env ?? {},
|
|
977
|
+
captureSessions: options?.captureSessions ?? true,
|
|
978
|
+
sessionStorage: makeCursorSessionStorage(options),
|
|
979
|
+
buildPrintCommand({
|
|
980
|
+
prompt,
|
|
981
|
+
dangerouslySkipPermissions,
|
|
982
|
+
resumeSession,
|
|
983
|
+
forkSession
|
|
984
|
+
}) {
|
|
985
|
+
if (resumeSession !== void 0 && forkSession === true) {
|
|
986
|
+
throw new ForkNotSupportedError({
|
|
987
|
+
message: "cursor cannot fork a session: its CLI resumes but has no fork. Use resume() to continue the session in place.",
|
|
988
|
+
agentProvider: "cursor"
|
|
989
|
+
});
|
|
990
|
+
}
|
|
991
|
+
assertCursorPrintPromptFitsArgv(prompt);
|
|
992
|
+
const forceFlag = dangerouslySkipPermissions ? " --force" : "";
|
|
993
|
+
const resumeFlag = resumeSession === void 0 ? "" : ` --resume=${shellEscape(resumeSession)}`;
|
|
994
|
+
return {
|
|
995
|
+
command: `agent --print --output-format stream-json --model ${shellEscape(model)} ${forceFlag}${resumeFlag} ${shellEscape(prompt)}`
|
|
996
|
+
};
|
|
997
|
+
},
|
|
998
|
+
buildInteractiveArgs({
|
|
999
|
+
prompt,
|
|
1000
|
+
dangerouslySkipPermissions
|
|
1001
|
+
}) {
|
|
1002
|
+
const args = ["agent", "--model", model];
|
|
1003
|
+
if (dangerouslySkipPermissions) args.push("--force");
|
|
1004
|
+
if (prompt) args.push(prompt);
|
|
1005
|
+
return args;
|
|
1006
|
+
},
|
|
1007
|
+
parseStreamLine(line) {
|
|
1008
|
+
return parseCursorStreamLine(line);
|
|
1009
|
+
}
|
|
1010
|
+
});
|
|
1011
|
+
var OPENCODE_TOOL_ARG_FIELDS = {
|
|
1012
|
+
bash: "command",
|
|
1013
|
+
webfetch: "url",
|
|
1014
|
+
task: "description"
|
|
1015
|
+
};
|
|
1016
|
+
var parseOpenCodeStreamLine = (line) => {
|
|
1017
|
+
if (!line.startsWith("{")) return [];
|
|
1018
|
+
try {
|
|
1019
|
+
const obj = JSON.parse(line);
|
|
1020
|
+
const { part } = obj;
|
|
1021
|
+
if (obj.type === "step_start" && typeof obj.sessionID === "string") {
|
|
1022
|
+
return [{ type: "session_id", sessionId: obj.sessionID }];
|
|
1023
|
+
}
|
|
1024
|
+
if (obj.type === "step_finish" && part?.tokens) {
|
|
1025
|
+
const t = part.tokens;
|
|
1026
|
+
return contextWindowEvent(t.input, t.cache?.read, t.cache?.write);
|
|
1027
|
+
}
|
|
1028
|
+
if (obj.type === "text" && part?.type === "text" && typeof part.text === "string") {
|
|
1029
|
+
return [
|
|
1030
|
+
{ type: "text", text: part.text },
|
|
1031
|
+
{ type: "result", result: part.text }
|
|
1032
|
+
];
|
|
1033
|
+
}
|
|
1034
|
+
if (obj.type === "tool_use" && part?.type === "tool") {
|
|
1035
|
+
if (typeof part.tool !== "string") return [];
|
|
1036
|
+
const state = part.state;
|
|
1037
|
+
if (state?.status !== "completed") return [];
|
|
1038
|
+
const { input } = state;
|
|
1039
|
+
if (!input) return [];
|
|
1040
|
+
const argField = OPENCODE_TOOL_ARG_FIELDS[part.tool];
|
|
1041
|
+
const argValue = argField === void 0 ? void 0 : input[argField];
|
|
1042
|
+
const args = typeof argValue === "string" ? argValue : JSON.stringify(input);
|
|
1043
|
+
return [{ type: "tool_call", name: part.tool, args }];
|
|
1044
|
+
}
|
|
1045
|
+
if (obj.type === "error") {
|
|
1046
|
+
const msg = extractErrorMessage(obj);
|
|
1047
|
+
return msg ? [{ type: "error", message: msg }] : [];
|
|
1048
|
+
}
|
|
1049
|
+
} catch {
|
|
1050
|
+
}
|
|
1051
|
+
return [];
|
|
1052
|
+
};
|
|
1053
|
+
var makeOpenCodeSessionStorage = (options) => {
|
|
1054
|
+
const hostDir = options?.sessionStorage?.hostSessionsDir ?? opencodeHostSessionsDir();
|
|
1055
|
+
const exportDirOverride = options?.sessionStorage?.sandboxExportDir;
|
|
1056
|
+
const hostPath = (id) => join(hostDir, `${id}.json`);
|
|
1057
|
+
return makeSessionFileStorage({
|
|
1058
|
+
hostRoot: hostDir,
|
|
1059
|
+
hostPath: (_cwd, id) => hostPath(id),
|
|
1060
|
+
// A no-sandbox run resumes from OpenCode's own database, which can't be
|
|
1061
|
+
// queried by id from here; OpenCode reports an unknown id itself.
|
|
1062
|
+
hostPathsById: (id) => [hostPath(id), opencodeHostDatabase()],
|
|
1063
|
+
sandboxPath: (_cwd, id, dirs) => posix.join(exportDirOverride ?? dirs.tmp, `${id}.json`),
|
|
1064
|
+
sandboxExport: (id, path) => `opencode export ${shellEscape(id)} > ${shellEscape(path)}`,
|
|
1065
|
+
sandboxImport: (_id, path) => `opencode import ${shellEscape(path)}`
|
|
1066
|
+
});
|
|
1067
|
+
};
|
|
1068
|
+
var opencode = (model, options) => ({
|
|
1069
|
+
name: "opencode",
|
|
1070
|
+
env: options?.env ?? {},
|
|
1071
|
+
captureSessions: options?.captureSessions ?? true,
|
|
1072
|
+
sessionStorage: makeOpenCodeSessionStorage(options),
|
|
1073
|
+
buildPrintCommand({
|
|
1074
|
+
prompt,
|
|
1075
|
+
dangerouslySkipPermissions,
|
|
1076
|
+
resumeSession,
|
|
1077
|
+
forkSession
|
|
1078
|
+
}) {
|
|
1079
|
+
const variantFlag = options?.variant ? ` --variant ${shellEscape(options.variant)}` : "";
|
|
1080
|
+
const agentFlag = options?.agent ? ` --agent ${shellEscape(options.agent)}` : "";
|
|
1081
|
+
const permissionsFlag = dangerouslySkipPermissions ? " --dangerously-skip-permissions" : "";
|
|
1082
|
+
const sessionFlags = resumeSession === void 0 ? "" : ` --session ${shellEscape(resumeSession)}${forkSession === true ? " --fork" : ""}`;
|
|
1083
|
+
return {
|
|
1084
|
+
command: `opencode run --format json --model ${shellEscape(model)}${variantFlag}${agentFlag}${permissionsFlag}${sessionFlags} ${shellEscape(prompt)}`
|
|
1085
|
+
};
|
|
1086
|
+
},
|
|
1087
|
+
buildInteractiveArgs({ prompt }) {
|
|
1088
|
+
const args = ["opencode", "--model", model];
|
|
1089
|
+
if (options?.agent) args.push("--agent", options.agent);
|
|
1090
|
+
if (prompt) args.push("--prompt", prompt);
|
|
1091
|
+
return args;
|
|
1092
|
+
},
|
|
1093
|
+
parseStreamLine(line) {
|
|
1094
|
+
return parseOpenCodeStreamLine(line);
|
|
1095
|
+
}
|
|
1096
|
+
});
|
|
1097
|
+
var COPILOT_PRINT_PROMPT_MAX_BYTES = 120 * 1024;
|
|
1098
|
+
var assertCopilotPrintPromptFitsArgv = (prompt) => {
|
|
1099
|
+
const n = Buffer.byteLength(prompt, "utf8");
|
|
1100
|
+
if (n > COPILOT_PRINT_PROMPT_MAX_BYTES) {
|
|
1101
|
+
throw new Error(
|
|
1102
|
+
`Copilot print-mode prompt is ${n} bytes (max ${COPILOT_PRINT_PROMPT_MAX_BYTES} bytes). This provider passes the prompt as a command-line argument; shorten the prompt or split the work. Other Sandcastle providers use stdin for large prompts.`
|
|
1103
|
+
);
|
|
1104
|
+
}
|
|
1105
|
+
};
|
|
1106
|
+
var parseCopilotStreamLine = (line) => {
|
|
1107
|
+
if (!line.startsWith("{")) return [];
|
|
1108
|
+
try {
|
|
1109
|
+
const obj = JSON.parse(line);
|
|
1110
|
+
if (obj.type === "assistant.message_delta" && typeof obj.data?.deltaContent === "string") {
|
|
1111
|
+
return [{ type: "text", text: obj.data.deltaContent }];
|
|
1112
|
+
}
|
|
1113
|
+
if (obj.type === "tool.execution_start") {
|
|
1114
|
+
const rawName = obj.data?.toolName;
|
|
1115
|
+
if (typeof rawName !== "string") return [];
|
|
1116
|
+
const toolName = rawName === "bash" ? "Bash" : rawName;
|
|
1117
|
+
const argField = TOOL_ARG_FIELDS[toolName];
|
|
1118
|
+
if (argField === void 0) return [];
|
|
1119
|
+
const args = obj.data?.arguments;
|
|
1120
|
+
if (!args) return [];
|
|
1121
|
+
const argValue = args[argField];
|
|
1122
|
+
if (typeof argValue !== "string") return [];
|
|
1123
|
+
return [{ type: "tool_call", name: toolName, args: argValue }];
|
|
1124
|
+
}
|
|
1125
|
+
if (obj.type === "assistant.message" && typeof obj.data?.content === "string" && obj.data.content.length > 0) {
|
|
1126
|
+
return [{ type: "result", result: obj.data.content }];
|
|
1127
|
+
}
|
|
1128
|
+
if (obj.type === "session.usage_checkpoint") {
|
|
1129
|
+
return contextWindowEvent(
|
|
1130
|
+
obj.data?.accountingSnapshot?.lastCallInputTokens
|
|
1131
|
+
);
|
|
1132
|
+
}
|
|
1133
|
+
if (obj.type === "result" && typeof obj.sessionId === "string") {
|
|
1134
|
+
return [{ type: "session_id", sessionId: obj.sessionId }];
|
|
1135
|
+
}
|
|
1136
|
+
if (obj.type === "error" || obj.type === "agent_error") {
|
|
1137
|
+
const msg = extractErrorMessage(obj);
|
|
1138
|
+
return msg ? [{ type: "error", message: msg }] : [];
|
|
1139
|
+
}
|
|
1140
|
+
} catch {
|
|
1141
|
+
}
|
|
1142
|
+
return [];
|
|
1143
|
+
};
|
|
1144
|
+
var makeCopilotSessionStorage = (options) => {
|
|
1145
|
+
const hostDir = options?.sessionStorage?.hostSessionsDir ?? copilotHostSessionsDir();
|
|
1146
|
+
const sandboxDirOverride = options?.sessionStorage?.sandboxSessionsDir;
|
|
1147
|
+
const hostPath = (id) => join(hostDir, id, "events.jsonl");
|
|
1148
|
+
return {
|
|
1149
|
+
...makeSessionFileStorage({
|
|
1150
|
+
hostRoot: hostDir,
|
|
1151
|
+
hostPath: (_cwd, id) => hostPath(id),
|
|
1152
|
+
hostPathsById: (id) => [hostPath(id)],
|
|
1153
|
+
sandboxPath: (_cwd, id, dirs) => copilotSessionPath(
|
|
1154
|
+
sandboxDirOverride ?? posix.join(dirs.home, ".copilot", "session-state"),
|
|
1155
|
+
id
|
|
1156
|
+
),
|
|
1157
|
+
rewrite: transferCopilotSession
|
|
1158
|
+
}),
|
|
1159
|
+
canFork: false
|
|
1160
|
+
};
|
|
1161
|
+
};
|
|
1162
|
+
var copilot = (model, options) => ({
|
|
1163
|
+
name: "copilot",
|
|
1164
|
+
env: options?.env ?? {},
|
|
1165
|
+
captureSessions: options?.captureSessions ?? true,
|
|
1166
|
+
sessionStorage: makeCopilotSessionStorage(options),
|
|
1167
|
+
buildPrintCommand({
|
|
1168
|
+
prompt,
|
|
1169
|
+
dangerouslySkipPermissions,
|
|
1170
|
+
resumeSession,
|
|
1171
|
+
forkSession
|
|
1172
|
+
}) {
|
|
1173
|
+
if (resumeSession !== void 0 && forkSession === true) {
|
|
1174
|
+
throw new ForkNotSupportedError({
|
|
1175
|
+
message: "copilot cannot fork a session: its CLI resumes but has no fork. Use resume() to continue the session in place.",
|
|
1176
|
+
agentProvider: "copilot"
|
|
1177
|
+
});
|
|
1178
|
+
}
|
|
1179
|
+
assertCopilotPrintPromptFitsArgv(prompt);
|
|
1180
|
+
const allowAll = dangerouslySkipPermissions ? " --allow-all-tools" : "";
|
|
1181
|
+
const effortFlag = options?.effort ? ` --effort ${options.effort}` : "";
|
|
1182
|
+
const resumeFlag = resumeSession === void 0 ? "" : ` --resume=${shellEscape(resumeSession)}`;
|
|
1183
|
+
return {
|
|
1184
|
+
command: `copilot -p ${shellEscape(prompt)} --output-format json --model ${shellEscape(model)}${allowAll}${effortFlag}${resumeFlag}`
|
|
1185
|
+
};
|
|
1186
|
+
},
|
|
1187
|
+
buildInteractiveArgs({ prompt }) {
|
|
1188
|
+
const args = ["copilot", "--model", model];
|
|
1189
|
+
if (prompt) args.push("-i", prompt);
|
|
1190
|
+
return args;
|
|
1191
|
+
},
|
|
1192
|
+
parseStreamLine(line) {
|
|
1193
|
+
return parseCopilotStreamLine(line);
|
|
1194
|
+
}
|
|
1195
|
+
});
|
|
1196
|
+
var buildClaudePrintCommand = (model, options, {
|
|
1197
|
+
prompt,
|
|
1198
|
+
dangerouslySkipPermissions,
|
|
1199
|
+
resumeSession,
|
|
1200
|
+
forkSession
|
|
1201
|
+
}) => {
|
|
1202
|
+
const skipPermissionsFlag = dangerouslySkipPermissions ? " --dangerously-skip-permissions" : "";
|
|
1203
|
+
const permissionFlag = options?.permissionMode ? ` --permission-mode ${options.permissionMode}` : skipPermissionsFlag;
|
|
1204
|
+
const effortFlag = options?.effort ? ` --effort ${options.effort}` : "";
|
|
1205
|
+
const resumeFlag = resumeSession ? ` --resume ${shellEscape(resumeSession)}` : "";
|
|
1206
|
+
const forkFlag = resumeSession && forkSession ? " --fork-session" : "";
|
|
1207
|
+
return {
|
|
1208
|
+
command: `claude --print --verbose${permissionFlag} --output-format stream-json --model ${shellEscape(model)}${effortFlag}${resumeFlag}${forkFlag}`,
|
|
1209
|
+
stdin: prompt
|
|
1210
|
+
};
|
|
1211
|
+
};
|
|
1212
|
+
var parseClaudeSessionUsage = (content) => {
|
|
1213
|
+
const lines = content.split("\n");
|
|
1214
|
+
for (let i = lines.length - 1; i >= 0; i--) {
|
|
1215
|
+
const line = lines[i];
|
|
1216
|
+
if (!line.startsWith("{")) continue;
|
|
1217
|
+
try {
|
|
1218
|
+
const obj = JSON.parse(line);
|
|
1219
|
+
if (obj.type === "assistant" && obj.message?.usage) {
|
|
1220
|
+
const u = obj.message.usage;
|
|
1221
|
+
if (typeof u.input_tokens === "number" && typeof u.cache_creation_input_tokens === "number" && typeof u.cache_read_input_tokens === "number" && typeof u.output_tokens === "number") {
|
|
1222
|
+
return {
|
|
1223
|
+
inputTokens: u.input_tokens,
|
|
1224
|
+
cacheCreationInputTokens: u.cache_creation_input_tokens,
|
|
1225
|
+
cacheReadInputTokens: u.cache_read_input_tokens,
|
|
1226
|
+
outputTokens: u.output_tokens
|
|
1227
|
+
};
|
|
1228
|
+
}
|
|
1229
|
+
}
|
|
1230
|
+
} catch {
|
|
1231
|
+
}
|
|
1232
|
+
}
|
|
1233
|
+
return void 0;
|
|
1234
|
+
};
|
|
1235
|
+
var claudeCode = (model, options) => ({
|
|
1236
|
+
name: "claude-code",
|
|
1237
|
+
env: options?.env ?? {},
|
|
1238
|
+
captureSessions: options?.captureSessions ?? true,
|
|
1239
|
+
sessionStorage: makeClaudeSessionStorage(options),
|
|
1240
|
+
buildPrintCommand(commandOptions) {
|
|
1241
|
+
return buildClaudePrintCommand(model, options, commandOptions);
|
|
1242
|
+
},
|
|
1243
|
+
buildInteractiveArgs({
|
|
1244
|
+
prompt,
|
|
1245
|
+
dangerouslySkipPermissions
|
|
1246
|
+
}) {
|
|
1247
|
+
const args = ["claude"];
|
|
1248
|
+
if (options?.permissionMode) {
|
|
1249
|
+
args.push("--permission-mode", options.permissionMode);
|
|
1250
|
+
} else if (dangerouslySkipPermissions) {
|
|
1251
|
+
args.push("--dangerously-skip-permissions");
|
|
1252
|
+
}
|
|
1253
|
+
args.push("--model", model);
|
|
1254
|
+
if (options?.effort) args.push("--effort", options.effort);
|
|
1255
|
+
if (prompt) args.push(prompt);
|
|
1256
|
+
return args;
|
|
1257
|
+
},
|
|
1258
|
+
parseStreamLine(line) {
|
|
1259
|
+
return [...parseStreamJsonLine(line), ...parseClaudeContextWindow(line)];
|
|
1260
|
+
},
|
|
1261
|
+
parseSessionUsage(content) {
|
|
1262
|
+
return parseClaudeSessionUsage(content);
|
|
1263
|
+
}
|
|
1264
|
+
});
|
|
1265
|
+
|
|
1266
|
+
export { claudeCode, claudeHostSessionPath, claudeSandboxSessionPath, codex, copilot, cursor, encodeProjectPath, findClaudeSessionOnHost, findCodexSessionOnHost, opencode, pi, shellEscape, transferClaudeSession, transferCodexSession };
|
|
1267
|
+
//# sourceMappingURL=chunk-SAQXFLO2.js.map
|
|
1268
|
+
//# sourceMappingURL=chunk-SAQXFLO2.js.map
|