toc-ocra 0.2.33 → 0.2.35
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/dist/cli/cli.mjs +368 -173
- package/dist/service/agent.mjs +362 -167
- package/dist/src/providers/cli/kiro-session-log.d.ts +53 -0
- package/dist/src/providers/cli/kiro-session-log.js +155 -0
- package/dist/src/providers/cli/kiro-session-log.js.map +1 -0
- package/dist/src/providers/cli/kiro.d.ts +14 -1
- package/dist/src/providers/cli/kiro.js +165 -123
- package/dist/src/providers/cli/kiro.js.map +1 -1
- package/dist/src/providers/provider.d.ts +23 -0
- package/dist/src/providers/provider.js.map +1 -1
- package/dist/src/runs/run-supervisor.d.ts +1 -0
- package/dist/src/runs/run-supervisor.js +37 -6
- package/dist/src/runs/run-supervisor.js.map +1 -1
- package/package.json +1 -1
package/dist/service/agent.mjs
CHANGED
|
@@ -7,7 +7,7 @@ var __export = (target, all) => {
|
|
|
7
7
|
|
|
8
8
|
// src/plugin-service.ts
|
|
9
9
|
import { existsSync as existsSync6 } from "node:fs";
|
|
10
|
-
import { delimiter as delimiter5, join as
|
|
10
|
+
import { delimiter as delimiter5, join as join13 } from "node:path";
|
|
11
11
|
|
|
12
12
|
// src/config.ts
|
|
13
13
|
import { randomUUID } from "node:crypto";
|
|
@@ -20928,6 +20928,145 @@ var grokProvider = {
|
|
|
20928
20928
|
// src/providers/cli/kiro.ts
|
|
20929
20929
|
import { execFile } from "node:child_process";
|
|
20930
20930
|
import { promisify } from "node:util";
|
|
20931
|
+
|
|
20932
|
+
// src/providers/cli/kiro-session-log.ts
|
|
20933
|
+
import { open as open2, readdir, readFile as readFile3, stat } from "node:fs/promises";
|
|
20934
|
+
import { homedir as homedir5 } from "node:os";
|
|
20935
|
+
import { join as join6 } from "node:path";
|
|
20936
|
+
var SHELL_TOOLS = /* @__PURE__ */ new Set(["shell", "execute_bash", "bash"]);
|
|
20937
|
+
function kiroSessionsDirectory() {
|
|
20938
|
+
return process.env.TOC_OCRA_KIRO_SESSIONS_DIR || join6(homedir5(), ".kiro", "sessions", "cli");
|
|
20939
|
+
}
|
|
20940
|
+
var KiroSessionLog = class {
|
|
20941
|
+
constructor(options) {
|
|
20942
|
+
this.options = options;
|
|
20943
|
+
}
|
|
20944
|
+
options;
|
|
20945
|
+
path;
|
|
20946
|
+
offset = 0;
|
|
20947
|
+
partial = "";
|
|
20948
|
+
steps = [];
|
|
20949
|
+
results = /* @__PURE__ */ new Map();
|
|
20950
|
+
claimed = /* @__PURE__ */ new Set();
|
|
20951
|
+
get directory() {
|
|
20952
|
+
return this.options.directory ?? kiroSessionsDirectory();
|
|
20953
|
+
}
|
|
20954
|
+
/** Called before the CLI starts: a resumed log is followed from its current end. */
|
|
20955
|
+
async begin() {
|
|
20956
|
+
if (!this.options.sessionId) return;
|
|
20957
|
+
this.path = join6(this.directory, `${this.options.sessionId}.jsonl`);
|
|
20958
|
+
this.offset = await stat(this.path).then((info) => info.size, () => 0);
|
|
20959
|
+
}
|
|
20960
|
+
/**
|
|
20961
|
+
* A fresh turn's chat has no known id: the one log for this folder created
|
|
20962
|
+
* after the turn started. Several candidates (another turn starting a chat
|
|
20963
|
+
* in the same folder) leave it unknown.
|
|
20964
|
+
*/
|
|
20965
|
+
async findFreshLog() {
|
|
20966
|
+
const names = await readdir(this.directory).catch(() => []);
|
|
20967
|
+
const found = [];
|
|
20968
|
+
for (const name of names) {
|
|
20969
|
+
if (!name.endsWith(".json")) continue;
|
|
20970
|
+
const meta3 = await readFile3(join6(this.directory, name), "utf8").then((text) => JSON.parse(text), () => void 0);
|
|
20971
|
+
const created = meta3?.created_at ? Date.parse(meta3.created_at) : NaN;
|
|
20972
|
+
if (meta3?.cwd && sameFolder(meta3.cwd, this.options.workdir) && created >= this.options.startedAt - 2e3) found.push(name.slice(0, -5));
|
|
20973
|
+
}
|
|
20974
|
+
if (found.length === 1) this.path = join6(this.directory, `${found[0]}.jsonl`);
|
|
20975
|
+
}
|
|
20976
|
+
/** Reads entries appended since the last call. */
|
|
20977
|
+
async refresh() {
|
|
20978
|
+
if (!this.path) await this.findFreshLog();
|
|
20979
|
+
if (!this.path) return;
|
|
20980
|
+
const file2 = await open2(this.path, "r").catch(() => void 0);
|
|
20981
|
+
if (!file2) return;
|
|
20982
|
+
try {
|
|
20983
|
+
const { size } = await file2.stat();
|
|
20984
|
+
if (size <= this.offset) return;
|
|
20985
|
+
const buffer = Buffer.alloc(size - this.offset);
|
|
20986
|
+
await file2.read(buffer, 0, buffer.length, this.offset);
|
|
20987
|
+
this.offset = size;
|
|
20988
|
+
const lines = (this.partial + buffer.toString("utf8")).split("\n");
|
|
20989
|
+
this.partial = lines.pop() ?? "";
|
|
20990
|
+
for (const line of lines) this.take(line);
|
|
20991
|
+
} finally {
|
|
20992
|
+
await file2.close();
|
|
20993
|
+
}
|
|
20994
|
+
}
|
|
20995
|
+
take(line) {
|
|
20996
|
+
let entry;
|
|
20997
|
+
try {
|
|
20998
|
+
entry = JSON.parse(line);
|
|
20999
|
+
} catch {
|
|
21000
|
+
return;
|
|
21001
|
+
}
|
|
21002
|
+
for (const part of entry.data?.content ?? []) {
|
|
21003
|
+
const data = part.data ?? {};
|
|
21004
|
+
if (part.kind === "toolUse" && SHELL_TOOLS.has(String(data.name))) {
|
|
21005
|
+
const command = data.input?.command;
|
|
21006
|
+
if (typeof data.toolUseId === "string" && typeof command === "string") this.steps.push({ id: data.toolUseId, command });
|
|
21007
|
+
} else if (part.kind === "toolResult" && typeof data.toolUseId === "string") {
|
|
21008
|
+
this.results.set(data.toolUseId, resultOf(data));
|
|
21009
|
+
}
|
|
21010
|
+
}
|
|
21011
|
+
}
|
|
21012
|
+
/** The shell step a `[tool] Running:` row announced (its text may be cut short). */
|
|
21013
|
+
async step(shown, waitMs = 2e3) {
|
|
21014
|
+
const prefix = shown.replace(/\.\.\.$|…$/, "").trim();
|
|
21015
|
+
const deadline = Date.now() + waitMs;
|
|
21016
|
+
for (; ; ) {
|
|
21017
|
+
await this.refresh();
|
|
21018
|
+
const step = this.steps.find((item) => !this.claimed.has(item.id) && item.command.trim().startsWith(prefix));
|
|
21019
|
+
if (step) {
|
|
21020
|
+
this.claimed.add(step.id);
|
|
21021
|
+
return step;
|
|
21022
|
+
}
|
|
21023
|
+
if (Date.now() >= deadline) return void 0;
|
|
21024
|
+
await new Promise((resolve7) => setTimeout(resolve7, 150));
|
|
21025
|
+
}
|
|
21026
|
+
}
|
|
21027
|
+
/** The first of `ids` whose result is in the log, waiting up to `waitMs`. */
|
|
21028
|
+
async firstFinished(ids, waitMs) {
|
|
21029
|
+
const deadline = Date.now() + waitMs;
|
|
21030
|
+
for (; ; ) {
|
|
21031
|
+
await this.refresh();
|
|
21032
|
+
const id = ids.find((item) => this.results.has(item));
|
|
21033
|
+
if (id || Date.now() >= deadline) return id;
|
|
21034
|
+
await new Promise((resolve7) => setTimeout(resolve7, 150));
|
|
21035
|
+
}
|
|
21036
|
+
}
|
|
21037
|
+
async result(id, waitMs = 3e3) {
|
|
21038
|
+
const deadline = Date.now() + waitMs;
|
|
21039
|
+
for (; ; ) {
|
|
21040
|
+
await this.refresh();
|
|
21041
|
+
const result = this.results.get(id);
|
|
21042
|
+
if (result || Date.now() >= deadline) return result;
|
|
21043
|
+
await new Promise((resolve7) => setTimeout(resolve7, 200));
|
|
21044
|
+
}
|
|
21045
|
+
}
|
|
21046
|
+
};
|
|
21047
|
+
function resultOf(data) {
|
|
21048
|
+
const parts = Array.isArray(data.content) ? data.content : [];
|
|
21049
|
+
const texts = [];
|
|
21050
|
+
let exitCode;
|
|
21051
|
+
for (const part of parts) {
|
|
21052
|
+
if (part.kind === "json" && part.data && typeof part.data === "object") {
|
|
21053
|
+
const value = part.data;
|
|
21054
|
+
const code = /(-?\d+)/.exec(String(value.exit_status ?? ""))?.[1];
|
|
21055
|
+
if (code !== void 0) exitCode = Number(code);
|
|
21056
|
+
for (const text of [value.stdout, value.stderr]) if (typeof text === "string" && text.trim()) texts.push(text.replace(/\s+$/, ""));
|
|
21057
|
+
} else if (part.kind === "text" && typeof part.data === "string" && part.data.trim()) {
|
|
21058
|
+
texts.push(part.data.replace(/\s+$/, ""));
|
|
21059
|
+
}
|
|
21060
|
+
}
|
|
21061
|
+
if (exitCode === void 0 && data.status === "error") exitCode = 1;
|
|
21062
|
+
return { ...exitCode === void 0 ? {} : { exitCode }, output: texts.join("\n") };
|
|
21063
|
+
}
|
|
21064
|
+
function sameFolder(a, b) {
|
|
21065
|
+
const normal = (path) => path.replace(/\/+$/, "").replace(/^\/private(?=\/)/, "");
|
|
21066
|
+
return normal(a) === normal(b);
|
|
21067
|
+
}
|
|
21068
|
+
|
|
21069
|
+
// src/providers/cli/kiro.ts
|
|
20931
21070
|
var execFileAsync = promisify(execFile);
|
|
20932
21071
|
var ANSI = /\u001b(?:\][^\u0007]*?(?:\u0007|\u001b\\)|\[[0-?]*[ -/]*[@-~]|[@-Z\\-_])/g;
|
|
20933
21072
|
function kiroLaunch(args) {
|
|
@@ -20951,7 +21090,7 @@ function describeListSessionsError(error62, timeoutMs) {
|
|
|
20951
21090
|
return (failure2?.message || String(error62)).split("\n")[0].slice(0, 160);
|
|
20952
21091
|
}
|
|
20953
21092
|
async function listKiroSessions(workdir, options = {}) {
|
|
20954
|
-
const timeoutMs = options.timeoutMs ?? KIRO_LIST_SESSIONS_TIMEOUT_MS;
|
|
21093
|
+
const timeoutMs = options.timeoutMs ?? (Number(process.env.TOC_OCRA_KIRO_LIST_TIMEOUT_MS) || KIRO_LIST_SESSIONS_TIMEOUT_MS);
|
|
20955
21094
|
const { executable, args, env } = kiroLaunch(LIST_SESSIONS);
|
|
20956
21095
|
let lastError;
|
|
20957
21096
|
for (let attempt = 0; attempt < 2; attempt++) {
|
|
@@ -20969,90 +21108,124 @@ async function listKiroSessions(workdir, options = {}) {
|
|
|
20969
21108
|
function isKiroMissingSession(stderr) {
|
|
20970
21109
|
return stderr.some((line) => /\bSession '[^']*' not found\b/i.test(line.replace(ANSI, "")));
|
|
20971
21110
|
}
|
|
20972
|
-
function
|
|
20973
|
-
|
|
20974
|
-
|
|
20975
|
-
|
|
20976
|
-
|
|
20977
|
-
|
|
20978
|
-
|
|
20979
|
-
|
|
20980
|
-
|
|
20981
|
-
|
|
20982
|
-
|
|
20983
|
-
|
|
20984
|
-
|
|
20985
|
-
|
|
20986
|
-
|
|
20987
|
-
|
|
20988
|
-
|
|
20989
|
-
|
|
20990
|
-
|
|
20991
|
-
|
|
20992
|
-
|
|
20993
|
-
|
|
20994
|
-
|
|
20995
|
-
|
|
20996
|
-
|
|
20997
|
-
|
|
20998
|
-
|
|
20999
|
-
|
|
21000
|
-
|
|
21001
|
-
|
|
21002
|
-
|
|
21003
|
-
|
|
21004
|
-
|
|
21005
|
-
|
|
21006
|
-
|
|
21007
|
-
|
|
21008
|
-
|
|
21009
|
-
|
|
21111
|
+
function kiroToolRows(text) {
|
|
21112
|
+
const step = /^\[tool\]\s*(.*)$/.exec(text.trim())?.[1];
|
|
21113
|
+
if (step === void 0) return void 0;
|
|
21114
|
+
const status = /^status:\s*(\w+)/i.exec(step)?.[1]?.toLowerCase();
|
|
21115
|
+
if (status) return status === "failed" ? [runLog.error("\u4E0A\u4E00\u6B65\u6267\u884C\u5931\u8D25")] : [];
|
|
21116
|
+
const running = /^Running:\s*(.+)$/.exec(step)?.[1]?.trim();
|
|
21117
|
+
if (running) return running.startsWith("@") ? [runLog.tool("mcp_tool_call")] : [runLog.command(running)];
|
|
21118
|
+
const changed = /^(?:Creating|Editing|Writing|Replacing|Appending to|Deleting)\s+(.+)$/i.exec(step)?.[1];
|
|
21119
|
+
if (changed) return [runLog.fileChange(changed.trim())];
|
|
21120
|
+
if (/^Searching the web\b|^Fetching\b/i.test(step)) return [runLog.tool("web_search")];
|
|
21121
|
+
if (/^(Reading|Searching|Finding|Listing)\b/i.test(step)) return [];
|
|
21122
|
+
return [runLog.tool("tool_call")];
|
|
21123
|
+
}
|
|
21124
|
+
function isKiroNoise(text) {
|
|
21125
|
+
return /^\[(INFO|DEBUG|WARN|TRACE)\]/.test(text) || /^\[ERROR\] \(node:\d+\) ExperimentalWarning/.test(text) || /^\(Use `node --trace-warnings/.test(text);
|
|
21126
|
+
}
|
|
21127
|
+
function kiroRunParser(context) {
|
|
21128
|
+
let pending = "";
|
|
21129
|
+
let started = false;
|
|
21130
|
+
let segment = [];
|
|
21131
|
+
const log = context ? new KiroSessionLog({ sessionId: context.sessionId, workdir: context.workdir, startedAt: context.startedAt }) : void 0;
|
|
21132
|
+
const ready = log?.begin().catch(() => void 0);
|
|
21133
|
+
let queue = Promise.resolve();
|
|
21134
|
+
const openSteps = [];
|
|
21135
|
+
let lastHeader;
|
|
21136
|
+
const out = (parsed) => {
|
|
21137
|
+
if (!context) return typeof parsed === "function" ? { display: [] } : parsed;
|
|
21138
|
+
queue = queue.then(async () => {
|
|
21139
|
+
const value = typeof parsed === "function" ? await parsed().catch(() => ({ display: [] })) : parsed;
|
|
21140
|
+
if (value.display.length && typeof parsed !== "function") lastHeader = void 0;
|
|
21141
|
+
context.emit(value);
|
|
21142
|
+
});
|
|
21143
|
+
return { display: [] };
|
|
21144
|
+
};
|
|
21145
|
+
const finishShell = async (step, status) => {
|
|
21146
|
+
const found = await step.shell.lookup;
|
|
21147
|
+
const result = found ? await log.result(found.id, 1e3) : void 0;
|
|
21148
|
+
const header = step.shell.headerShown && lastHeader === step ? [] : [runLog.command(found?.command ?? step.shell.shown)];
|
|
21149
|
+
lastHeader = void 0;
|
|
21150
|
+
if (!result) return { display: [...header, ...status === "failed" ? [runLog.error("\u4E0A\u4E00\u6B65\u6267\u884C\u5931\u8D25")] : []] };
|
|
21151
|
+
return { display: [...header, ...runLog.commandOutput(result.output), runLog.commandExit(result.exitCode ?? (status === "failed" ? 1 : 0))] };
|
|
21152
|
+
};
|
|
21153
|
+
const replyRows = (lines) => {
|
|
21154
|
+
const display = [];
|
|
21155
|
+
for (const raw of lines) {
|
|
21156
|
+
const line = raw.replace(ANSI, "").replace(/\s+$/, "");
|
|
21157
|
+
if (!started) {
|
|
21158
|
+
if (!line.trim()) continue;
|
|
21159
|
+
started = true;
|
|
21160
|
+
segment = [line];
|
|
21161
|
+
display.push(...runLog.reply(line));
|
|
21010
21162
|
} else {
|
|
21011
|
-
|
|
21012
|
-
|
|
21013
|
-
|
|
21014
|
-
|
|
21015
|
-
|
|
21016
|
-
|
|
21017
|
-
|
|
21018
|
-
|
|
21019
|
-
|
|
21020
|
-
|
|
21021
|
-
|
|
21022
|
-
|
|
21023
|
-
|
|
21024
|
-
const done = command && commandLast ? [runLog.commandExit(0)] : [];
|
|
21025
|
-
if (command && commandLast) {
|
|
21026
|
-
command = "";
|
|
21027
|
-
commandLast = false;
|
|
21028
|
-
}
|
|
21029
|
-
mode = "none";
|
|
21030
|
-
return { display: done };
|
|
21031
|
-
}
|
|
21032
|
-
if (isChrome(trimmed)) return { display: [] };
|
|
21033
|
-
if (/^✓/.test(trimmed)) return { display: [] };
|
|
21034
|
-
if (/^[✗❗]/.test(trimmed) && mode !== "reply" && !command) return { display: [runLog.error(trimmed.replace(/^[✗❗]\s*/, ""))] };
|
|
21035
|
-
if (mode === "reply") {
|
|
21036
|
-
reply.push(text);
|
|
21037
|
-
return { message: reply.join("\n").trim(), display: runLog.replyMore(text) };
|
|
21038
|
-
}
|
|
21039
|
-
if (command) return { display: commandRows(text) };
|
|
21040
|
-
if (mode === "tool") return { display: [] };
|
|
21041
|
-
return { display: [text] };
|
|
21042
|
-
};
|
|
21043
|
-
return parse4;
|
|
21044
|
-
}
|
|
21045
|
-
function jsonLine(event, line) {
|
|
21046
|
-
const sessionId = str(event.session_id) ?? str(event.sessionId);
|
|
21047
|
-
const message = str(event.response) ?? str(event.message) ?? str(event.text);
|
|
21048
|
-
const error62 = typeof event.error === "string" ? event.error : event.error && typeof event.error === "object" ? str(event.error.message) : void 0;
|
|
21049
|
-
const type = str(event.type) ?? "";
|
|
21163
|
+
segment.push(line);
|
|
21164
|
+
display.push(...runLog.replyMore(line));
|
|
21165
|
+
}
|
|
21166
|
+
}
|
|
21167
|
+
return started ? { message: segment.join("\n").trim(), display } : { display };
|
|
21168
|
+
};
|
|
21169
|
+
const closeSegment = () => {
|
|
21170
|
+
const rest = pending;
|
|
21171
|
+
pending = "";
|
|
21172
|
+
const parsed = rest ? replyRows([rest]) : { display: [] };
|
|
21173
|
+
started = false;
|
|
21174
|
+
return parsed;
|
|
21175
|
+
};
|
|
21050
21176
|
return {
|
|
21051
|
-
|
|
21052
|
-
|
|
21053
|
-
|
|
21054
|
-
|
|
21055
|
-
|
|
21177
|
+
stdout(chunk) {
|
|
21178
|
+
const lines = (pending + chunk).split(/\r?\n/);
|
|
21179
|
+
pending = lines.pop() ?? "";
|
|
21180
|
+
return out(replyRows(lines));
|
|
21181
|
+
},
|
|
21182
|
+
stderr(raw) {
|
|
21183
|
+
const text = raw.replace(ANSI, "").trim();
|
|
21184
|
+
if (!text || isKiroNoise(text)) return { display: [] };
|
|
21185
|
+
const closed = closeSegment();
|
|
21186
|
+
const status = /^\[tool\]\s*status:\s*(\w+)/i.exec(text)?.[1]?.toLowerCase();
|
|
21187
|
+
if (!log) {
|
|
21188
|
+
const rows2 = kiroToolRows(text) ?? [runLog.error(text)];
|
|
21189
|
+
return out({ ...closed, display: [...closed.display, ...rows2] });
|
|
21190
|
+
}
|
|
21191
|
+
out(closed);
|
|
21192
|
+
const running = /^\[tool\]\s*Running:\s*(.+)$/.exec(text)?.[1]?.trim();
|
|
21193
|
+
if (running && !running.startsWith("@")) {
|
|
21194
|
+
const step = { shell: { shown: running, lookup: (async () => {
|
|
21195
|
+
await ready;
|
|
21196
|
+
return log.step(running);
|
|
21197
|
+
})(), headerShown: !openSteps.some((item) => item.shell) } };
|
|
21198
|
+
openSteps.push(step);
|
|
21199
|
+
if (!step.shell.headerShown) return { display: [] };
|
|
21200
|
+
return out(async () => {
|
|
21201
|
+
const found = await step.shell.lookup;
|
|
21202
|
+
lastHeader = step;
|
|
21203
|
+
return { display: [runLog.command(found?.command ?? running)] };
|
|
21204
|
+
});
|
|
21205
|
+
}
|
|
21206
|
+
if (status) {
|
|
21207
|
+
return out(async () => {
|
|
21208
|
+
const shells = openSteps.filter((item) => item.shell);
|
|
21209
|
+
const ids = (await Promise.all(shells.map((item) => item.shell.lookup))).map((found) => found?.id ?? "");
|
|
21210
|
+
const others = openSteps.some((item) => !item.shell);
|
|
21211
|
+
const id = shells.length ? await log.firstFinished(ids.filter(Boolean), others ? 1200 : 3e3) : void 0;
|
|
21212
|
+
const step = id ? shells[ids.indexOf(id)] : openSteps.find((item) => !item.shell) ?? openSteps[0];
|
|
21213
|
+
if (step) openSteps.splice(openSteps.indexOf(step), 1);
|
|
21214
|
+
if (step?.shell) return finishShell(step, status);
|
|
21215
|
+
lastHeader = void 0;
|
|
21216
|
+
return { display: status === "failed" ? [runLog.error("\u4E0A\u4E00\u6B65\u6267\u884C\u5931\u8D25")] : [] };
|
|
21217
|
+
});
|
|
21218
|
+
}
|
|
21219
|
+
if (/^\[tool\]/.test(text)) openSteps.push({});
|
|
21220
|
+
const rows = kiroToolRows(text) ?? [runLog.error(text)];
|
|
21221
|
+
return out({ display: rows });
|
|
21222
|
+
},
|
|
21223
|
+
end: async () => {
|
|
21224
|
+
const rest = out(closeSegment());
|
|
21225
|
+
if (!context) return rest;
|
|
21226
|
+
await queue;
|
|
21227
|
+
return { display: [] };
|
|
21228
|
+
}
|
|
21056
21229
|
};
|
|
21057
21230
|
}
|
|
21058
21231
|
var kiroProvider = {
|
|
@@ -21071,19 +21244,14 @@ var kiroProvider = {
|
|
|
21071
21244
|
isMissingSession: (stderr) => isKiroMissingSession(stderr),
|
|
21072
21245
|
inspectExit(context) {
|
|
21073
21246
|
const businessError = kiroStderrError(context.stderr);
|
|
21074
|
-
|
|
21075
|
-
const credits = context.stderr.map((line) => /^\s*▸\s*(Credits:.*)$/.exec(line.replace(ANSI, ""))?.[1]).find(Boolean);
|
|
21076
|
-
return credits ? { display: [runLog.event(`\u672C\u8F6E\u5B8C\u6210\uFF08${credits.trim().replace(/\s*•\s*/g, " \xB7 ")}\uFF09`)] } : {};
|
|
21247
|
+
return businessError ? { turnError: businessError } : {};
|
|
21077
21248
|
},
|
|
21249
|
+
/** A single stdout line read as a complete reply (the supervisor uses `createRunParser`). */
|
|
21078
21250
|
parseLine(line) {
|
|
21079
|
-
return
|
|
21251
|
+
return kiroRunParser().stdout(`${line}
|
|
21252
|
+
`);
|
|
21080
21253
|
},
|
|
21081
|
-
|
|
21082
|
-
stderrDisplay(line) {
|
|
21083
|
-
const text = line.replace(ANSI, "").trim();
|
|
21084
|
-
if (!text || isChrome(text)) return [];
|
|
21085
|
-
return [line];
|
|
21086
|
-
}
|
|
21254
|
+
createRunParser: (context) => kiroRunParser(context)
|
|
21087
21255
|
};
|
|
21088
21256
|
function kiroStderrError(stderrLines) {
|
|
21089
21257
|
const text = stderrLines.join("\n");
|
|
@@ -21259,7 +21427,7 @@ var cliDetection = new CliDetectionService();
|
|
|
21259
21427
|
|
|
21260
21428
|
// src/runtime/agent-runtime.ts
|
|
21261
21429
|
import { randomUUID as randomUUID3 } from "node:crypto";
|
|
21262
|
-
import { join as
|
|
21430
|
+
import { join as join12 } from "node:path";
|
|
21263
21431
|
|
|
21264
21432
|
// src/exec/script-runner.ts
|
|
21265
21433
|
import { spawn as spawn3 } from "node:child_process";
|
|
@@ -21664,13 +21832,13 @@ function resolveChatProvider(type, request, attachments) {
|
|
|
21664
21832
|
|
|
21665
21833
|
// src/providers/model-catalog-collector.ts
|
|
21666
21834
|
import { createHash } from "node:crypto";
|
|
21667
|
-
import { stat } from "node:fs/promises";
|
|
21835
|
+
import { stat as stat2 } from "node:fs/promises";
|
|
21668
21836
|
|
|
21669
21837
|
// src/providers/model-catalog.ts
|
|
21670
21838
|
import { spawn as spawn5 } from "node:child_process";
|
|
21671
|
-
import { readFile as
|
|
21672
|
-
import { homedir as
|
|
21673
|
-
import { isAbsolute, join as
|
|
21839
|
+
import { readFile as readFile4 } from "node:fs/promises";
|
|
21840
|
+
import { homedir as homedir6 } from "node:os";
|
|
21841
|
+
import { isAbsolute, join as join7, resolve as resolve2 } from "node:path";
|
|
21674
21842
|
|
|
21675
21843
|
// src/providers/kiro-models.ts
|
|
21676
21844
|
import { spawn as spawn4 } from "node:child_process";
|
|
@@ -21792,15 +21960,15 @@ function parseClaudeModelCatalog(settings, environment = process.env, fetchedAt
|
|
|
21792
21960
|
if (defaultModel) catalog.default_model = defaultModel;
|
|
21793
21961
|
return catalog;
|
|
21794
21962
|
}
|
|
21795
|
-
var claudeHome = () => process.env.CLAUDE_CONFIG_DIR?.trim() ||
|
|
21796
|
-
var codexHome = () => process.env.CODEX_HOME?.trim() ||
|
|
21797
|
-
var geminiHome = () => process.env.GEMINI_CLI_HOME?.trim() ||
|
|
21798
|
-
var grokHome = () => process.env.GROK_HOME?.trim() ||
|
|
21799
|
-
var opencodeHome = () =>
|
|
21800
|
-
var kiroHome = () =>
|
|
21963
|
+
var claudeHome = () => process.env.CLAUDE_CONFIG_DIR?.trim() || join7(homedir6(), ".claude");
|
|
21964
|
+
var codexHome = () => process.env.CODEX_HOME?.trim() || join7(homedir6(), ".codex");
|
|
21965
|
+
var geminiHome = () => process.env.GEMINI_CLI_HOME?.trim() || join7(homedir6(), ".gemini");
|
|
21966
|
+
var grokHome = () => process.env.GROK_HOME?.trim() || join7(homedir6(), ".grok");
|
|
21967
|
+
var opencodeHome = () => join7(process.env.XDG_CONFIG_HOME?.trim() || join7(homedir6(), ".config"), "opencode");
|
|
21968
|
+
var kiroHome = () => join7(homedir6(), ".kiro");
|
|
21801
21969
|
async function readClaudeModelCatalog() {
|
|
21802
21970
|
const home = claudeHome();
|
|
21803
|
-
const content = await
|
|
21971
|
+
const content = await readFile4(join7(home, "settings.json"), "utf8").catch(() => "");
|
|
21804
21972
|
let settings = {};
|
|
21805
21973
|
if (content) {
|
|
21806
21974
|
try {
|
|
@@ -21856,16 +22024,16 @@ async function readCodexModels(codexHome2, config2) {
|
|
|
21856
22024
|
const configuredCatalog = configValue(config2, "model_catalog_json");
|
|
21857
22025
|
if (configuredCatalog) {
|
|
21858
22026
|
const catalogPath = isAbsolute(configuredCatalog) ? configuredCatalog : resolve2(codexHome2, configuredCatalog);
|
|
21859
|
-
const configuredModels = parseCodexModels(await
|
|
22027
|
+
const configuredModels = parseCodexModels(await readFile4(catalogPath, "utf8").catch(() => ""));
|
|
21860
22028
|
if (configuredModels?.length) return configuredModels;
|
|
21861
22029
|
}
|
|
21862
22030
|
return parseCodexModels(
|
|
21863
|
-
await
|
|
22031
|
+
await readFile4(join7(codexHome2, "models_cache.json"), "utf8").catch(() => "")
|
|
21864
22032
|
) ?? [];
|
|
21865
22033
|
}
|
|
21866
22034
|
async function readCodexModelCatalog() {
|
|
21867
22035
|
const home = codexHome();
|
|
21868
|
-
const config2 = await
|
|
22036
|
+
const config2 = await readFile4(join7(home, "config.toml"), "utf8").catch(() => "");
|
|
21869
22037
|
const models = await readCodexModels(home, config2);
|
|
21870
22038
|
const defaultModel = configValue(config2, "model");
|
|
21871
22039
|
const defaultReasoningEffort = configValue(config2, "model_reasoning_effort");
|
|
@@ -21955,8 +22123,8 @@ async function readKiroModelCatalog() {
|
|
|
21955
22123
|
};
|
|
21956
22124
|
const launch = resolveExecutable("kiro-cli", ["acp", "--trust-tools="], environment, override);
|
|
21957
22125
|
const [payload, content] = await Promise.all([
|
|
21958
|
-
readKiroNativeModels(launch,
|
|
21959
|
-
|
|
22126
|
+
readKiroNativeModels(launch, homedir6(), { catalog: base }),
|
|
22127
|
+
readFile4(join7(kiroHome(), "settings", "cli.json"), "utf8").catch(() => "{}")
|
|
21960
22128
|
]);
|
|
21961
22129
|
let settings;
|
|
21962
22130
|
try {
|
|
@@ -22013,7 +22181,7 @@ function geminiModelsUrl(baseUrl) {
|
|
|
22013
22181
|
}
|
|
22014
22182
|
async function readGeminiModelCatalog() {
|
|
22015
22183
|
const fileEnvironment = parseEnvFile(
|
|
22016
|
-
await
|
|
22184
|
+
await readFile4(join7(geminiHome(), ".env"), "utf8").catch(() => "")
|
|
22017
22185
|
);
|
|
22018
22186
|
const environment = { ...fileEnvironment, ...process.env };
|
|
22019
22187
|
const baseUrl = environment.GOOGLE_GEMINI_BASE_URL?.trim();
|
|
@@ -22058,7 +22226,7 @@ function parseGrokModelCatalog(output2, config2 = "", fetchedAt = (/* @__PURE__
|
|
|
22058
22226
|
return catalog;
|
|
22059
22227
|
}
|
|
22060
22228
|
async function readGrokModelCatalog() {
|
|
22061
|
-
const config2 = await
|
|
22229
|
+
const config2 = await readFile4(join7(grokHome(), "config.toml"), "utf8").catch(() => "");
|
|
22062
22230
|
const output2 = await runModelListCommand(resolveCliCommand("grok"), ["models"], "grok");
|
|
22063
22231
|
const catalog = parseGrokModelCatalog(output2, config2);
|
|
22064
22232
|
if (catalog.models.length === 0) throw new Error("Grok \u672C\u673A\u672A\u8FD4\u56DE\u53EF\u7528\u6A21\u578B");
|
|
@@ -22077,7 +22245,7 @@ function parseOpencodeModelCatalog(output2, config2 = "", fetchedAt = (/* @__PUR
|
|
|
22077
22245
|
return catalog;
|
|
22078
22246
|
}
|
|
22079
22247
|
async function readOpencodeModelCatalog() {
|
|
22080
|
-
const config2 = await
|
|
22248
|
+
const config2 = await readFile4(join7(opencodeHome(), "opencode.json"), "utf8").catch(() => "") || await readFile4(join7(opencodeHome(), "opencode.jsonc"), "utf8").catch(() => "");
|
|
22081
22249
|
const output2 = await runModelListCommand(resolveCliCommand("opencode"), ["models"], "opencode");
|
|
22082
22250
|
const catalog = parseOpencodeModelCatalog(output2, config2);
|
|
22083
22251
|
if (catalog.models.length === 0) throw new Error("OpenCode \u672C\u673A\u672A\u8FD4\u56DE\u53EF\u7528\u6A21\u578B");
|
|
@@ -22085,22 +22253,22 @@ async function readOpencodeModelCatalog() {
|
|
|
22085
22253
|
}
|
|
22086
22254
|
var TEN_MINUTES = 10 * 60 * 1e3;
|
|
22087
22255
|
var MODEL_SOURCES = {
|
|
22088
|
-
claude: { read: readClaudeModelCatalog, watchFiles: () => [
|
|
22256
|
+
claude: { read: readClaudeModelCatalog, watchFiles: () => [join7(claudeHome(), "settings.json")] },
|
|
22089
22257
|
codex: {
|
|
22090
22258
|
read: readCodexModelCatalog,
|
|
22091
22259
|
watchFiles: () => {
|
|
22092
22260
|
const home = codexHome();
|
|
22093
|
-
return [
|
|
22261
|
+
return [join7(home, "config.toml"), join7(home, "models_cache.json")];
|
|
22094
22262
|
},
|
|
22095
22263
|
// model_catalog_json may point anywhere; a periodic re-read covers it.
|
|
22096
22264
|
refreshEveryMs: TEN_MINUTES
|
|
22097
22265
|
},
|
|
22098
|
-
gemini: { read: readGeminiModelCatalog, watchFiles: () => [
|
|
22099
|
-
grok: { read: readGrokModelCatalog, watchFiles: () => [
|
|
22100
|
-
kiro: { read: readKiroModelCatalog, watchFiles: () => [
|
|
22266
|
+
gemini: { read: readGeminiModelCatalog, watchFiles: () => [join7(geminiHome(), ".env")], refreshEveryMs: TEN_MINUTES },
|
|
22267
|
+
grok: { read: readGrokModelCatalog, watchFiles: () => [join7(grokHome(), "config.toml")], refreshEveryMs: TEN_MINUTES },
|
|
22268
|
+
kiro: { read: readKiroModelCatalog, watchFiles: () => [join7(kiroHome(), "settings", "cli.json")], refreshEveryMs: TEN_MINUTES },
|
|
22101
22269
|
opencode: {
|
|
22102
22270
|
read: readOpencodeModelCatalog,
|
|
22103
|
-
watchFiles: () => [
|
|
22271
|
+
watchFiles: () => [join7(opencodeHome(), "opencode.json"), join7(opencodeHome(), "opencode.jsonc")],
|
|
22104
22272
|
refreshEveryMs: TEN_MINUTES
|
|
22105
22273
|
}
|
|
22106
22274
|
};
|
|
@@ -22108,7 +22276,7 @@ var MODEL_SOURCES = {
|
|
|
22108
22276
|
// src/providers/model-catalog-collector.ts
|
|
22109
22277
|
async function fileStamp(path) {
|
|
22110
22278
|
try {
|
|
22111
|
-
const info = await
|
|
22279
|
+
const info = await stat2(path);
|
|
22112
22280
|
return `${path}:${info.mtimeMs}:${info.size}`;
|
|
22113
22281
|
} catch {
|
|
22114
22282
|
return `${path}:-`;
|
|
@@ -22343,16 +22511,16 @@ async function readCLIQuotaReport(agentType) {
|
|
|
22343
22511
|
}
|
|
22344
22512
|
|
|
22345
22513
|
// src/providers/tb-switch-control.ts
|
|
22346
|
-
import { readFile as
|
|
22347
|
-
import { homedir as
|
|
22348
|
-
import { join as
|
|
22514
|
+
import { readFile as readFile5 } from "node:fs/promises";
|
|
22515
|
+
import { homedir as homedir7 } from "node:os";
|
|
22516
|
+
import { join as join8 } from "node:path";
|
|
22349
22517
|
function dataDir() {
|
|
22350
|
-
return process.env.TB_SWITCH_HOME?.trim() ||
|
|
22518
|
+
return process.env.TB_SWITCH_HOME?.trim() || join8(homedir7(), ".tb-switch");
|
|
22351
22519
|
}
|
|
22352
22520
|
async function readLocalService() {
|
|
22353
22521
|
let discovery;
|
|
22354
22522
|
try {
|
|
22355
|
-
discovery = JSON.parse(await
|
|
22523
|
+
discovery = JSON.parse(await readFile5(join8(dataDir(), "api.json"), "utf8"));
|
|
22356
22524
|
} catch {
|
|
22357
22525
|
throw new Error("\u8FD9\u53F0\u8BBE\u5907\u4E0A\u7684 TB Switch \u670D\u52A1\u672A\u8FD0\u884C\uFF0C\u8BF7\u6253\u5F00\u684C\u9762\u7AEF\u63D2\u4EF6\u540E\u91CD\u8BD5\u3002");
|
|
22358
22526
|
}
|
|
@@ -22368,7 +22536,7 @@ async function readLocalService() {
|
|
|
22368
22536
|
}
|
|
22369
22537
|
let token = process.env.TB_SWITCH_API_TOKEN?.trim() ?? "";
|
|
22370
22538
|
try {
|
|
22371
|
-
token = (await
|
|
22539
|
+
token = (await readFile5(join8(dataDir(), "api-token"), "utf8")).trim();
|
|
22372
22540
|
} catch {
|
|
22373
22541
|
}
|
|
22374
22542
|
if (!token) throw new Error("\u65E0\u6CD5\u8BFB\u53D6 TB Switch \u672C\u673A\u670D\u52A1\u51ED\u636E\uFF0C\u8BF7\u91CD\u542F\u684C\u9762\u7AEF\u63D2\u4EF6\u540E\u91CD\u8BD5\u3002");
|
|
@@ -22459,7 +22627,7 @@ async function controlTbSwitch(payload) {
|
|
|
22459
22627
|
|
|
22460
22628
|
// src/runs/orphan-registry.ts
|
|
22461
22629
|
import { execFile as execFile4 } from "node:child_process";
|
|
22462
|
-
import { mkdir as mkdir3, readFile as
|
|
22630
|
+
import { mkdir as mkdir3, readFile as readFile6, rename as rename3, writeFile as writeFile3 } from "node:fs/promises";
|
|
22463
22631
|
import { basename, dirname as dirname5 } from "node:path";
|
|
22464
22632
|
|
|
22465
22633
|
// src/runs/process-tree.ts
|
|
@@ -22555,7 +22723,7 @@ var OrphanRegistry = class {
|
|
|
22555
22723
|
async reapPrevious() {
|
|
22556
22724
|
let previous = [];
|
|
22557
22725
|
try {
|
|
22558
|
-
previous = JSON.parse(await
|
|
22726
|
+
previous = JSON.parse(await readFile6(this.file, "utf8"));
|
|
22559
22727
|
} catch {
|
|
22560
22728
|
return [];
|
|
22561
22729
|
}
|
|
@@ -22585,8 +22753,8 @@ function commandOf(pid) {
|
|
|
22585
22753
|
|
|
22586
22754
|
// src/runs/run-supervisor.ts
|
|
22587
22755
|
import { spawn as spawn8 } from "node:child_process";
|
|
22588
|
-
import { mkdir as mkdir5, realpath as realpath2, stat as
|
|
22589
|
-
import { basename as basename3, isAbsolute as isAbsolute3, join as
|
|
22756
|
+
import { mkdir as mkdir5, realpath as realpath2, stat as stat3, writeFile as writeFile5 } from "node:fs/promises";
|
|
22757
|
+
import { basename as basename3, isAbsolute as isAbsolute3, join as join10, relative as relative2, resolve as resolve3 } from "node:path";
|
|
22590
22758
|
import { createInterface as createInterface3 } from "node:readline";
|
|
22591
22759
|
|
|
22592
22760
|
// src/session/terminal-line-sanitizer.ts
|
|
@@ -22712,8 +22880,8 @@ function clipToCharacterBudget(rows, budget) {
|
|
|
22712
22880
|
// src/runs/task-attachments.ts
|
|
22713
22881
|
import { createHash as createHash2, randomUUID as randomUUID2 } from "node:crypto";
|
|
22714
22882
|
import { constants as constants2 } from "node:fs";
|
|
22715
|
-
import { lstat, mkdir as mkdir4, open as
|
|
22716
|
-
import { basename as basename2, isAbsolute as isAbsolute2, join as
|
|
22883
|
+
import { lstat, mkdir as mkdir4, open as open3, realpath, rename as rename4, rm as rm2, writeFile as writeFile4 } from "node:fs/promises";
|
|
22884
|
+
import { basename as basename2, isAbsolute as isAbsolute2, join as join9, relative } from "node:path";
|
|
22717
22885
|
function within(root, path) {
|
|
22718
22886
|
const segment = relative(root, path);
|
|
22719
22887
|
return segment === "" || !segment.startsWith("..") && !isAbsolute2(segment);
|
|
@@ -22721,7 +22889,7 @@ function within(root, path) {
|
|
|
22721
22889
|
async function attachmentDirectory(workdir, messageId) {
|
|
22722
22890
|
let directory = workdir;
|
|
22723
22891
|
for (const segment of [".echo", "attachments", `message-${encodeURIComponent(messageId).slice(0, 180)}`]) {
|
|
22724
|
-
directory =
|
|
22892
|
+
directory = join9(directory, segment);
|
|
22725
22893
|
await mkdir4(directory, { mode: 448 }).catch((error62) => {
|
|
22726
22894
|
if (error62.code !== "EEXIST") throw error62;
|
|
22727
22895
|
});
|
|
@@ -22729,7 +22897,7 @@ async function attachmentDirectory(workdir, messageId) {
|
|
|
22729
22897
|
if (metadata.isSymbolicLink() || !metadata.isDirectory() || !within(workdir, await realpath(directory))) {
|
|
22730
22898
|
throw new Error("\u9644\u4EF6\u76EE\u5F55\u5FC5\u987B\u4F4D\u4E8E CLI \u5DE5\u4F5C\u76EE\u5F55\u5185\uFF0C\u4E14\u4E0D\u80FD\u662F\u7B26\u53F7\u94FE\u63A5");
|
|
22731
22899
|
}
|
|
22732
|
-
if (segment === ".echo") await writeFile4(
|
|
22900
|
+
if (segment === ".echo") await writeFile4(join9(directory, ".gitignore"), "*\n", { flag: "wx", mode: 384 }).catch((error62) => {
|
|
22733
22901
|
if (error62.code !== "EEXIST") throw error62;
|
|
22734
22902
|
});
|
|
22735
22903
|
}
|
|
@@ -22738,7 +22906,7 @@ async function attachmentDirectory(workdir, messageId) {
|
|
|
22738
22906
|
async function matches(path, size, sha256, signal2) {
|
|
22739
22907
|
const metadata = await lstat(path).catch(() => void 0);
|
|
22740
22908
|
if (!metadata?.isFile() || metadata.isSymbolicLink() || metadata.size !== size) return false;
|
|
22741
|
-
const file2 = await
|
|
22909
|
+
const file2 = await open3(path, constants2.O_RDONLY | (constants2.O_NOFOLLOW ?? 0));
|
|
22742
22910
|
try {
|
|
22743
22911
|
const hash2 = createHash2("sha256");
|
|
22744
22912
|
let read = 0;
|
|
@@ -22766,9 +22934,9 @@ async function prepareTaskAttachments(payload, workdir, download, signal2) {
|
|
|
22766
22934
|
const characters = Array.from(basename2(file2.name.replace(/\\/g, "/")).replace(/[^\p{L}\p{N}._-]/gu, "_").replace(/\.+$/u, ""));
|
|
22767
22935
|
while (Buffer.byteLength(characters.join("")) > 160) characters.shift();
|
|
22768
22936
|
const name = characters.join("") || "attachment";
|
|
22769
|
-
const path =
|
|
22937
|
+
const path = join9(directory, `${file2.file_id}-${name}`);
|
|
22770
22938
|
if (!await matches(path, file2.size, file2.sha256, signal2)) {
|
|
22771
|
-
const temporary =
|
|
22939
|
+
const temporary = join9(directory, `.download-${file2.file_id}-${randomUUID2()}.part`);
|
|
22772
22940
|
try {
|
|
22773
22941
|
await download(payload.run_id, file2.file_id, temporary, file2.size, file2.sha256, signal2);
|
|
22774
22942
|
signal2.throwIfAborted();
|
|
@@ -23031,6 +23199,16 @@ var RunSupervisor = class {
|
|
|
23031
23199
|
lastLine: "",
|
|
23032
23200
|
lastOutputAt: Date.now()
|
|
23033
23201
|
};
|
|
23202
|
+
if (provider.createRunParser) {
|
|
23203
|
+
run.stream = provider.createRunParser({
|
|
23204
|
+
workdir,
|
|
23205
|
+
sessionId,
|
|
23206
|
+
startedAt: run.startedAt,
|
|
23207
|
+
emit: (parsed) => {
|
|
23208
|
+
if (this.active.has(run.runId)) this.applyParsed(run, parsed, "");
|
|
23209
|
+
}
|
|
23210
|
+
});
|
|
23211
|
+
}
|
|
23034
23212
|
this.active.set(run.runId, run);
|
|
23035
23213
|
this.preparing.delete(run.runId);
|
|
23036
23214
|
this.unreported.add(run.runId);
|
|
@@ -23046,12 +23224,21 @@ var RunSupervisor = class {
|
|
|
23046
23224
|
child.once("error", (error62) => {
|
|
23047
23225
|
spawnError = error62.message;
|
|
23048
23226
|
});
|
|
23049
|
-
|
|
23227
|
+
if (run.stream) {
|
|
23228
|
+
child.stdout.setEncoding("utf8");
|
|
23229
|
+
child.stdout.on("data", (chunk) => this.applyParsed(run, run.stream.stdout(chunk), chunk));
|
|
23230
|
+
} else {
|
|
23231
|
+
createInterface3({ input: child.stdout }).on("line", (line) => this.onStdout(run, line));
|
|
23232
|
+
}
|
|
23050
23233
|
createInterface3({ input: child.stderr }).on("line", (line) => {
|
|
23051
23234
|
const clean = sanitizeTerminalLine(line);
|
|
23052
23235
|
if (!clean.trim()) return;
|
|
23053
23236
|
run.stderr.push(clean);
|
|
23054
23237
|
if (run.stderr.length > MAX_ERROR_ROWS) run.stderr.shift();
|
|
23238
|
+
if (run.stream) {
|
|
23239
|
+
this.applyParsed(run, run.stream.stderr(line), line);
|
|
23240
|
+
return;
|
|
23241
|
+
}
|
|
23055
23242
|
for (const row of provider.stderrDisplay ? provider.stderrDisplay(line) : [clean]) {
|
|
23056
23243
|
const shown = sanitizeTerminalLine(row);
|
|
23057
23244
|
if (shown.trim()) {
|
|
@@ -23097,7 +23284,9 @@ var RunSupervisor = class {
|
|
|
23097
23284
|
});
|
|
23098
23285
|
}
|
|
23099
23286
|
onStdout(run, rawLine) {
|
|
23100
|
-
|
|
23287
|
+
this.applyParsed(run, run.parse(rawLine), rawLine);
|
|
23288
|
+
}
|
|
23289
|
+
applyParsed(run, parsed, rawLine) {
|
|
23101
23290
|
if (run.chat && !run.forwardingFailed) {
|
|
23102
23291
|
for (const event of parsed.chatEvents ?? []) this.queueChatEvent(run, sanitizeChatEvent(event));
|
|
23103
23292
|
if (parsed.sessionId) run.chatReady = true;
|
|
@@ -23135,6 +23324,7 @@ var RunSupervisor = class {
|
|
|
23135
23324
|
}
|
|
23136
23325
|
async onExit(run, code, signal2, spawnError) {
|
|
23137
23326
|
for (const timer of run.timers) clearTimeout(timer);
|
|
23327
|
+
if (run.stream) this.applyParsed(run, await run.stream.end(), "");
|
|
23138
23328
|
this.active.delete(run.runId);
|
|
23139
23329
|
this.orphans?.remove(run.runId);
|
|
23140
23330
|
if (run.eventTimer) clearTimeout(run.eventTimer);
|
|
@@ -23270,13 +23460,13 @@ var RunSupervisor = class {
|
|
|
23270
23460
|
async prepareChatAttachments(payload) {
|
|
23271
23461
|
if (!payload.attachments?.length) return [];
|
|
23272
23462
|
if (!this.reporter.chatMedia) throw new Error("\u9644\u4EF6\u8F6C\u53D1\u672A\u914D\u7F6E");
|
|
23273
|
-
const directory =
|
|
23463
|
+
const directory = join10(this.config.dataDir, "chat-media", encodeURIComponent(payload.chat_session_id ?? payload.run_id));
|
|
23274
23464
|
await mkdir5(directory, { recursive: true, mode: 448 });
|
|
23275
23465
|
const files = [];
|
|
23276
23466
|
for (const attachment of payload.attachments) {
|
|
23277
23467
|
const name = basename3(attachment.name).replace(/[^\p{L}\p{N}._-]/gu, "_").slice(-120) || "attachment";
|
|
23278
|
-
const path =
|
|
23279
|
-
const existing = await
|
|
23468
|
+
const path = join10(directory, `${encodeURIComponent(attachment.media_id)}-${name}`);
|
|
23469
|
+
const existing = await stat3(path).catch(() => void 0);
|
|
23280
23470
|
if (!existing?.isFile()) {
|
|
23281
23471
|
const data = await this.reporter.chatMedia(payload.run_id, attachment.media_id);
|
|
23282
23472
|
await writeFile5(path, data, { mode: 384 });
|
|
@@ -23314,10 +23504,15 @@ var RunSupervisor = class {
|
|
|
23314
23504
|
if (live) return live;
|
|
23315
23505
|
return [...this.recent.values()].filter((run) => run.workstationId === id).sort((a, b) => b.finishedAt - a.finishedAt)[0];
|
|
23316
23506
|
};
|
|
23317
|
-
const
|
|
23318
|
-
if (
|
|
23319
|
-
|
|
23320
|
-
|
|
23507
|
+
const empty = { lines: [], startCursor: 0, nextCursor: 0, oldestCursor: 0, latestCursor: 0, hasMore: false, truncated: false, limited: false, idleMs: void 0 };
|
|
23508
|
+
if (query.runId) {
|
|
23509
|
+
const run = byId(query.runId);
|
|
23510
|
+
if (!run) return { ...empty, status: this.preparing.has(query.runId) ? "running" : "idle", run_id: query.runId };
|
|
23511
|
+
const read2 = run.tail.read(beforeCursor === void 0 ? { limit } : { limit, beforeCursor });
|
|
23512
|
+
return { ...read2, status: this.active.has(run.runId) ? "running" : "finished", run_id: run.runId };
|
|
23513
|
+
}
|
|
23514
|
+
const target = byWorkstation(query.workstationId);
|
|
23515
|
+
if (!target) return { ...empty, status: "idle" };
|
|
23321
23516
|
const read = target.tail.read(beforeCursor === void 0 ? { limit } : { limit, beforeCursor });
|
|
23322
23517
|
return { ...read, status: this.active.has(target.runId) ? "running" : "finished", run_id: target.runId };
|
|
23323
23518
|
}
|
|
@@ -23507,17 +23702,17 @@ var GatewayClient = class {
|
|
|
23507
23702
|
};
|
|
23508
23703
|
|
|
23509
23704
|
// src/workspace/directory-browser.ts
|
|
23510
|
-
import { readdir, realpath as realpath3, stat as
|
|
23511
|
-
import { homedir as
|
|
23705
|
+
import { readdir as readdir2, realpath as realpath3, stat as stat4 } from "node:fs/promises";
|
|
23706
|
+
import { homedir as homedir8 } from "node:os";
|
|
23512
23707
|
import { dirname as dirname6, parse as parse3, resolve as resolve4 } from "node:path";
|
|
23513
23708
|
var MAX_ENTRIES = 1e3;
|
|
23514
23709
|
async function listDirectory(requested, allowedRoots) {
|
|
23515
|
-
const raw = requested.trim().replace(/^~(?=$|[\\/])/,
|
|
23710
|
+
const raw = requested.trim().replace(/^~(?=$|[\\/])/, homedir8()) || homedir8();
|
|
23516
23711
|
const directory = await realpath3(resolve4(raw)).catch(() => {
|
|
23517
23712
|
throw new Error(`\u76EE\u5F55\u4E0D\u5B58\u5728\uFF1A${requested}`);
|
|
23518
23713
|
});
|
|
23519
|
-
if (!(await
|
|
23520
|
-
const entries = await
|
|
23714
|
+
if (!(await stat4(directory)).isDirectory()) throw new Error("\u8FD9\u4E0D\u662F\u4E00\u4E2A\u76EE\u5F55");
|
|
23715
|
+
const entries = await readdir2(directory, { withFileTypes: true }).catch((error62) => {
|
|
23521
23716
|
throw new Error(error62.code === "EACCES" || error62.code === "EPERM" ? "\u6CA1\u6709\u6743\u9650\u8BFB\u53D6\u8FD9\u4E2A\u76EE\u5F55" : error62.message);
|
|
23522
23717
|
});
|
|
23523
23718
|
const byName = (a, b) => a.name.localeCompare(b.name, void 0, { sensitivity: "base", numeric: true });
|
|
@@ -23530,8 +23725,8 @@ async function listDirectory(requested, allowedRoots) {
|
|
|
23530
23725
|
else if (entry.isFile()) files.push(item);
|
|
23531
23726
|
}
|
|
23532
23727
|
const parent = dirname6(directory);
|
|
23533
|
-
const places = [{ label: "\u4E3B\u76EE\u5F55", path:
|
|
23534
|
-
for (const root of allowedRoots) if (root !==
|
|
23728
|
+
const places = [{ label: "\u4E3B\u76EE\u5F55", path: homedir8() }];
|
|
23729
|
+
for (const root of allowedRoots) if (root !== homedir8()) places.push({ label: "\u5141\u8BB8\u7684\u76EE\u5F55", path: root });
|
|
23535
23730
|
places.push({ label: "\u6839\u76EE\u5F55", path: process.platform === "win32" ? parse3(directory).root : "/" });
|
|
23536
23731
|
return {
|
|
23537
23732
|
path: directory,
|
|
@@ -23545,9 +23740,9 @@ async function listDirectory(requested, allowedRoots) {
|
|
|
23545
23740
|
|
|
23546
23741
|
// src/workspace/workspace-manager.ts
|
|
23547
23742
|
import { spawn as spawn9 } from "node:child_process";
|
|
23548
|
-
import { chmod as chmod3, mkdir as mkdir6, mkdtemp, opendir, realpath as realpath4, rm as rm3, stat as
|
|
23549
|
-
import { homedir as
|
|
23550
|
-
import { isAbsolute as isAbsolute4, join as
|
|
23743
|
+
import { chmod as chmod3, mkdir as mkdir6, mkdtemp, opendir, realpath as realpath4, rm as rm3, stat as stat5, writeFile as writeFile6 } from "node:fs/promises";
|
|
23744
|
+
import { homedir as homedir9, tmpdir as tmpdir2 } from "node:os";
|
|
23745
|
+
import { isAbsolute as isAbsolute4, join as join11, relative as relative3, resolve as resolve5 } from "node:path";
|
|
23551
23746
|
var MAX_NODES = 2e3;
|
|
23552
23747
|
var MAX_DIRECTORY_ENTRIES = 12e3;
|
|
23553
23748
|
var IGNORED_DIRECTORIES = /* @__PURE__ */ new Set([
|
|
@@ -23595,7 +23790,7 @@ var WorkspaceManager = class {
|
|
|
23595
23790
|
};
|
|
23596
23791
|
}
|
|
23597
23792
|
validateRootPath(value) {
|
|
23598
|
-
const raw = value.trim().replace(/^~(?=$|[\\/])/,
|
|
23793
|
+
const raw = value.trim().replace(/^~(?=$|[\\/])/, homedir9());
|
|
23599
23794
|
const root = resolve5(raw);
|
|
23600
23795
|
if (!isAbsolute4(raw)) throw new Error("\u6839\u76EE\u5F55\u5FC5\u987B\u662F\u7EDD\u5BF9\u8DEF\u5F84");
|
|
23601
23796
|
if (relative3(root, resolve5(root, "..")) === "") throw new Error("\u4E0D\u80FD\u628A\u6587\u4EF6\u7CFB\u7EDF\u6839\u76EE\u5F55\u4F5C\u4E3A\u76EE\u5F55\u6839");
|
|
@@ -23612,7 +23807,7 @@ var WorkspaceManager = class {
|
|
|
23612
23807
|
requestedPaths.add(relativePath);
|
|
23613
23808
|
const target = this.resolveWithinRoot(rootPath, relativePath);
|
|
23614
23809
|
try {
|
|
23615
|
-
const metadata = await
|
|
23810
|
+
const metadata = await stat5(target).catch(() => void 0);
|
|
23616
23811
|
if (metadata) {
|
|
23617
23812
|
if (!metadata.isDirectory()) {
|
|
23618
23813
|
throw new Error("The existing workspace path is not a directory");
|
|
@@ -23670,7 +23865,7 @@ var WorkspaceManager = class {
|
|
|
23670
23865
|
let created = false;
|
|
23671
23866
|
let metadata;
|
|
23672
23867
|
try {
|
|
23673
|
-
metadata = await
|
|
23868
|
+
metadata = await stat5(target);
|
|
23674
23869
|
} catch {
|
|
23675
23870
|
}
|
|
23676
23871
|
if (!metadata) {
|
|
@@ -23770,7 +23965,7 @@ var WorkspaceManager = class {
|
|
|
23770
23965
|
}
|
|
23771
23966
|
async scanTree(rootPath, maxDepth, scanRoots) {
|
|
23772
23967
|
const resolvedRoot = await realpath4(rootPath);
|
|
23773
|
-
const rootMetadata = await
|
|
23968
|
+
const rootMetadata = await stat5(resolvedRoot);
|
|
23774
23969
|
if (!rootMetadata.isDirectory()) throw new Error("The workspace root is not a directory");
|
|
23775
23970
|
const nodes = [];
|
|
23776
23971
|
let entriesRead = 0;
|
|
@@ -23825,7 +24020,7 @@ var WorkspaceManager = class {
|
|
|
23825
24020
|
const target = this.resolveWithinRoot(resolvedRoot, relativePath);
|
|
23826
24021
|
let metadata;
|
|
23827
24022
|
try {
|
|
23828
|
-
metadata = await
|
|
24023
|
+
metadata = await stat5(target);
|
|
23829
24024
|
} catch (error62) {
|
|
23830
24025
|
this.logger.warn("workspace-scan-root-missing", {
|
|
23831
24026
|
rootPath: target,
|
|
@@ -23922,12 +24117,12 @@ var WorkspaceManager = class {
|
|
|
23922
24117
|
}
|
|
23923
24118
|
async createGitAuthEnvironment(auth) {
|
|
23924
24119
|
if (!auth) return void 0;
|
|
23925
|
-
const directory = await mkdtemp(
|
|
24120
|
+
const directory = await mkdtemp(join11(tmpdir2(), "toc-ocra-git-auth-"));
|
|
23926
24121
|
const isWindows = process.platform === "win32";
|
|
23927
|
-
const scriptPath =
|
|
24122
|
+
const scriptPath = join11(directory, isWindows ? "askpass.cmd" : "askpass.sh");
|
|
23928
24123
|
let script;
|
|
23929
24124
|
if (isWindows) {
|
|
23930
|
-
const helperPath =
|
|
24125
|
+
const helperPath = join11(directory, "askpass.cjs");
|
|
23931
24126
|
await writeFile6(
|
|
23932
24127
|
helperPath,
|
|
23933
24128
|
'const prompt = process.argv.slice(2).join(" ");\nprocess.stdout.write(/username/i.test(prompt) ? process.env.ECHO_GIT_USERNAME ?? "" : process.env.ECHO_GIT_PASSWORD ?? "");\n',
|
|
@@ -24077,7 +24272,7 @@ var AgentRuntime = class {
|
|
|
24077
24272
|
}
|
|
24078
24273
|
};
|
|
24079
24274
|
this.workspaces = new WorkspaceManager(config2, this.logger);
|
|
24080
|
-
this.orphans = new OrphanRegistry(
|
|
24275
|
+
this.orphans = new OrphanRegistry(join12(config2.dataDir, "active-runs.json"));
|
|
24081
24276
|
this.runs = new RunSupervisor(config2, this.logger, reporter, this.orphans);
|
|
24082
24277
|
}
|
|
24083
24278
|
config;
|
|
@@ -24795,7 +24990,7 @@ function preferSystemNode() {
|
|
|
24795
24990
|
const names = process.platform === "win32" ? ["node.exe"] : ["node"];
|
|
24796
24991
|
for (const directory of (process.env.PATH ?? "").split(delimiter5)) {
|
|
24797
24992
|
for (const name of names) {
|
|
24798
|
-
const candidate = directory &&
|
|
24993
|
+
const candidate = directory && join13(directory, name);
|
|
24799
24994
|
if (candidate && existsSync6(candidate)) {
|
|
24800
24995
|
process.env.TOC_OCRA_NODE_PATH = candidate;
|
|
24801
24996
|
return;
|