@ycodium-ai/agent-opencode 0.2.3598 → 0.2.3608
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/index.js +372 -27
- package/package.json +4 -4
package/dist/index.js
CHANGED
|
@@ -50,6 +50,8 @@ var OPENCODE_CARD = {
|
|
|
50
50
|
// 拼出逐字相同的规则集)。
|
|
51
51
|
nativeModeSwitch: true,
|
|
52
52
|
contextCompaction: "native",
|
|
53
|
+
// 子 agent 经父会话的 `subagent` 工具事件报 `task.*`:身份用子会话 id,终态以子会话自己的状态为准,工具结果与收件箱回报只补正文。
|
|
54
|
+
reportsSpawnedAgents: true,
|
|
53
55
|
// 出处不是 provider 文档:OpenCode 背后接哪家模型由用户配,没有统一上限可查。这里沿用
|
|
54
56
|
// 产品此前统一施加的单图上限(10 MiB),它在这条内联路径上一直在用;换了更严的后端
|
|
55
57
|
// 导致拒图时,要在这里调小,而不是在宿主里按 provider 名写死。
|
|
@@ -1261,6 +1263,27 @@ var ServerSentEventParser = class {
|
|
|
1261
1263
|
}
|
|
1262
1264
|
};
|
|
1263
1265
|
|
|
1266
|
+
// src/openCodeApiError.ts
|
|
1267
|
+
var OpenCodeApiError = class extends Error {
|
|
1268
|
+
/**
|
|
1269
|
+
* `http`:server 回了非 2xx,`status` 是它的状态码。
|
|
1270
|
+
* `shape`:回了 2xx,但形状不是我们认得的。
|
|
1271
|
+
* `transport`:请求没发到或中途断了。
|
|
1272
|
+
*/
|
|
1273
|
+
kind;
|
|
1274
|
+
status;
|
|
1275
|
+
constructor(input) {
|
|
1276
|
+
const what = input.kind === "http" ? `failed with status ${input.status}` : input.kind === "shape" ? `returned an unexpected shape${input.detail ? ` (${input.detail})` : ""}` : `could not be sent${input.detail ? ` (${input.detail})` : ""}`;
|
|
1277
|
+
super(
|
|
1278
|
+
`OpenCode ${input.method} ${input.path} ${what}.`,
|
|
1279
|
+
input.cause === void 0 ? {} : { cause: input.cause }
|
|
1280
|
+
);
|
|
1281
|
+
this.name = "OpenCodeApiError";
|
|
1282
|
+
this.kind = input.kind;
|
|
1283
|
+
this.status = input.status;
|
|
1284
|
+
}
|
|
1285
|
+
};
|
|
1286
|
+
|
|
1264
1287
|
// src/server.ts
|
|
1265
1288
|
import * as NodeCrypto2 from "node:crypto";
|
|
1266
1289
|
import {
|
|
@@ -1682,25 +1705,6 @@ async function runOpenCodeCommand(input) {
|
|
|
1682
1705
|
|
|
1683
1706
|
// src/openCodeApi.ts
|
|
1684
1707
|
var OPENCODE_REQUEST_TIMEOUT_MS = 3e4;
|
|
1685
|
-
var OpenCodeApiError = class extends Error {
|
|
1686
|
-
/**
|
|
1687
|
-
* `http`:server 回了非 2xx,`status` 是它的状态码。
|
|
1688
|
-
* `shape`:回了 2xx,但形状不是我们认得的。
|
|
1689
|
-
* `transport`:请求没发到或中途断了。
|
|
1690
|
-
*/
|
|
1691
|
-
kind;
|
|
1692
|
-
status;
|
|
1693
|
-
constructor(input) {
|
|
1694
|
-
const what = input.kind === "http" ? `failed with status ${input.status}` : input.kind === "shape" ? `returned an unexpected shape${input.detail ? ` (${input.detail})` : ""}` : `could not be sent${input.detail ? ` (${input.detail})` : ""}`;
|
|
1695
|
-
super(
|
|
1696
|
-
`OpenCode ${input.method} ${input.path} ${what}.`,
|
|
1697
|
-
input.cause === void 0 ? {} : { cause: input.cause }
|
|
1698
|
-
);
|
|
1699
|
-
this.name = "OpenCodeApiError";
|
|
1700
|
-
this.kind = input.kind;
|
|
1701
|
-
this.status = input.status;
|
|
1702
|
-
}
|
|
1703
|
-
};
|
|
1704
1708
|
function isOpenCodeApiNotFound(cause) {
|
|
1705
1709
|
return cause instanceof OpenCodeApiError && cause.kind === "http" && cause.status === 404;
|
|
1706
1710
|
}
|
|
@@ -1789,11 +1793,14 @@ function decodeOpenCodeEvent(frame) {
|
|
|
1789
1793
|
const fields = isRecord2(data) ? data : {};
|
|
1790
1794
|
const location = parsed["location"];
|
|
1791
1795
|
const directory = isRecord2(location) ? stringField(location, "directory") : void 0;
|
|
1796
|
+
const durable = parsed["durable"];
|
|
1797
|
+
const sequence = isRecord2(durable) ? durable["seq"] : void 0;
|
|
1792
1798
|
return {
|
|
1793
1799
|
type: type2,
|
|
1794
1800
|
sessionId: stringField(fields, "sessionID"),
|
|
1795
1801
|
data: fields,
|
|
1796
|
-
...directory === void 0 ? {} : { directory }
|
|
1802
|
+
...directory === void 0 ? {} : { directory },
|
|
1803
|
+
...typeof sequence === "number" ? { sequence } : {}
|
|
1797
1804
|
};
|
|
1798
1805
|
}
|
|
1799
1806
|
function createOpenCodeApi(server) {
|
|
@@ -1934,6 +1941,23 @@ function createOpenCodeApi(server) {
|
|
|
1934
1941
|
await sendWithoutResult("PATCH", sessionPath(sessionId), { body: { permissions } });
|
|
1935
1942
|
},
|
|
1936
1943
|
getSession: (sessionId) => callSession("GET", sessionPath(sessionId), void 0),
|
|
1944
|
+
listChildSessions: async (parentId, input) => {
|
|
1945
|
+
const query = input?.cursor === void 0 ? `parentID=${encodeURIComponent(parentId)}` : `cursor=${encodeURIComponent(input.cursor)}`;
|
|
1946
|
+
const path = `/api/session?${query}&limit=100`;
|
|
1947
|
+
const body = await call("GET", path, {
|
|
1948
|
+
wrapped: false,
|
|
1949
|
+
...input?.signal === void 0 ? {} : { signal: input.signal }
|
|
1950
|
+
});
|
|
1951
|
+
const items = isRecord2(body) ? body["data"] : void 0;
|
|
1952
|
+
if (!Array.isArray(items)) {
|
|
1953
|
+
throw new OpenCodeApiError({ kind: "shape", method: "GET", path, detail: "not a list" });
|
|
1954
|
+
}
|
|
1955
|
+
const cursor = isRecord2(body) ? body["cursor"] : void 0;
|
|
1956
|
+
return {
|
|
1957
|
+
sessions: items.map(decodeSession).filter((session) => typeof session !== "string"),
|
|
1958
|
+
next: isRecord2(cursor) ? stringField(cursor, "next") : void 0
|
|
1959
|
+
};
|
|
1960
|
+
},
|
|
1937
1961
|
prompt: async (sessionId, input) => {
|
|
1938
1962
|
const path = sessionPath(sessionId, "/prompt");
|
|
1939
1963
|
const files = input.files ?? [];
|
|
@@ -1948,11 +1972,12 @@ function createOpenCodeApi(server) {
|
|
|
1948
1972
|
return id;
|
|
1949
1973
|
},
|
|
1950
1974
|
interrupt: async (sessionId, signal) => {
|
|
1951
|
-
await call("POST", sessionPath(sessionId, "/interrupt"), {
|
|
1975
|
+
const body = await call("POST", sessionPath(sessionId, "/interrupt"), {
|
|
1952
1976
|
wrapped: false,
|
|
1953
1977
|
body: {},
|
|
1954
1978
|
...signal === void 0 ? {} : { signal }
|
|
1955
1979
|
});
|
|
1980
|
+
return isRecord2(body) && body["interrupted"] === true;
|
|
1956
1981
|
},
|
|
1957
1982
|
replyPermission: async (sessionId, requestId, decision, message) => {
|
|
1958
1983
|
await sendWithoutResult(
|
|
@@ -2422,6 +2447,288 @@ function handleOpenCodeCompactionEvent(input) {
|
|
|
2422
2447
|
}
|
|
2423
2448
|
}
|
|
2424
2449
|
|
|
2450
|
+
// src/subagents.ts
|
|
2451
|
+
var OPENCODE_SUBAGENT_TOOL = "subagent";
|
|
2452
|
+
function createOpenCodeSubagentState() {
|
|
2453
|
+
return {
|
|
2454
|
+
calls: /* @__PURE__ */ new Map(),
|
|
2455
|
+
doneCalls: /* @__PURE__ */ new Set(),
|
|
2456
|
+
children: /* @__PURE__ */ new Map(),
|
|
2457
|
+
announced: /* @__PURE__ */ new Map(),
|
|
2458
|
+
reports: /* @__PURE__ */ new Set(),
|
|
2459
|
+
childSequence: /* @__PURE__ */ new Map(),
|
|
2460
|
+
startedSequence: /* @__PURE__ */ new Map(),
|
|
2461
|
+
lastSequence: void 0
|
|
2462
|
+
};
|
|
2463
|
+
}
|
|
2464
|
+
var SUMMARY_LIMIT = 8e3;
|
|
2465
|
+
var TRUNCATED_PREFIX = "[Earlier output truncated]\n\n";
|
|
2466
|
+
function boundSummary(text4) {
|
|
2467
|
+
return text4.length <= SUMMARY_LIMIT ? text4 : `${TRUNCATED_PREFIX}${text4.slice(-SUMMARY_LIMIT)}`;
|
|
2468
|
+
}
|
|
2469
|
+
function stripSubagentShell(text4) {
|
|
2470
|
+
return /^<subagent\b[^>]*>\n?([\s\S]*?)\n?<\/subagent>$/.exec(text4.trim())?.[1] ?? text4;
|
|
2471
|
+
}
|
|
2472
|
+
var summaryOf = (text4) => {
|
|
2473
|
+
const body = text4 === void 0 ? void 0 : stripSubagentShell(text4).trim();
|
|
2474
|
+
return body === void 0 || body.length === 0 ? void 0 : boundSummary(body);
|
|
2475
|
+
};
|
|
2476
|
+
function identityOf(childId, names, callId) {
|
|
2477
|
+
return {
|
|
2478
|
+
taskType: "subagent",
|
|
2479
|
+
rosterKind: "subagent",
|
|
2480
|
+
nativeSessionId: childId,
|
|
2481
|
+
...callId === void 0 ? {} : { toolUseId: callId },
|
|
2482
|
+
...names.title === void 0 ? {} : { title: names.title },
|
|
2483
|
+
...names.role === void 0 ? {} : { role: names.role },
|
|
2484
|
+
...names.model === void 0 ? {} : { model: names.model }
|
|
2485
|
+
};
|
|
2486
|
+
}
|
|
2487
|
+
var modelName = (ref) => ref === void 0 ? void 0 : `${ref.providerID}/${ref.id}`;
|
|
2488
|
+
function noteParentSequence(context, event) {
|
|
2489
|
+
const state = context.subagents;
|
|
2490
|
+
if (event.sequence === void 0) return;
|
|
2491
|
+
state.lastSequence = Math.max(state.lastSequence ?? event.sequence, event.sequence);
|
|
2492
|
+
}
|
|
2493
|
+
function activate(context, childId, names, callId, background) {
|
|
2494
|
+
const state = context.subagents;
|
|
2495
|
+
const activation = {
|
|
2496
|
+
...names,
|
|
2497
|
+
callId,
|
|
2498
|
+
background,
|
|
2499
|
+
fromSequence: state.lastSequence,
|
|
2500
|
+
ended: void 0,
|
|
2501
|
+
summary: void 0,
|
|
2502
|
+
fromChildSequence: state.childSequence.get(childId)
|
|
2503
|
+
};
|
|
2504
|
+
state.children.set(childId, activation);
|
|
2505
|
+
context.childSessionIds.add(childId);
|
|
2506
|
+
return activation;
|
|
2507
|
+
}
|
|
2508
|
+
function settleActivation(events, context, childId, status, boundary) {
|
|
2509
|
+
const state = context.subagents;
|
|
2510
|
+
const activation = state.children.get(childId);
|
|
2511
|
+
if (activation === void 0 || activation.ended !== void 0) return;
|
|
2512
|
+
activation.ended = status;
|
|
2513
|
+
activation.endBoundary = boundary;
|
|
2514
|
+
const identity = { taskId: childId, ...identityOf(childId, activation, activation.callId) };
|
|
2515
|
+
emitEvent(events, context, "task.completed", {
|
|
2516
|
+
...identity,
|
|
2517
|
+
status,
|
|
2518
|
+
...activation.summary === void 0 ? {} : { summary: activation.summary }
|
|
2519
|
+
});
|
|
2520
|
+
}
|
|
2521
|
+
function attachResult(events, context, childId, summary) {
|
|
2522
|
+
const activation = context.subagents.children.get(childId);
|
|
2523
|
+
if (activation === void 0 || summary === void 0 || activation.summary !== void 0) return;
|
|
2524
|
+
activation.summary = summary;
|
|
2525
|
+
if (activation.ended === void 0) return;
|
|
2526
|
+
emitEvent(events, context, "task.completed", {
|
|
2527
|
+
taskId: childId,
|
|
2528
|
+
...identityOf(childId, activation, activation.callId),
|
|
2529
|
+
status: activation.ended,
|
|
2530
|
+
summary
|
|
2531
|
+
});
|
|
2532
|
+
}
|
|
2533
|
+
function beginSubagentCall(context, callId) {
|
|
2534
|
+
const state = context.subagents;
|
|
2535
|
+
if (state.calls.has(callId) || state.doneCalls.has(callId)) return;
|
|
2536
|
+
state.calls.set(callId, { background: false });
|
|
2537
|
+
}
|
|
2538
|
+
function noteSubagentInput(context, callId, input) {
|
|
2539
|
+
const call = context.subagents.calls.get(callId);
|
|
2540
|
+
if (call === void 0) return;
|
|
2541
|
+
const description = readString2(input["description"]);
|
|
2542
|
+
const agent = readString2(input["agent"]);
|
|
2543
|
+
const model = readString2(input["model"])?.split("#")[0];
|
|
2544
|
+
const resumeId = readString2(input["sessionID"]);
|
|
2545
|
+
if (description !== void 0) call.description = description;
|
|
2546
|
+
if (agent !== void 0) call.agent = agent;
|
|
2547
|
+
if (model !== void 0 && model.length > 0) call.model = model;
|
|
2548
|
+
if (resumeId !== void 0) call.resumeId = resumeId;
|
|
2549
|
+
call.background = input["background"] === true;
|
|
2550
|
+
}
|
|
2551
|
+
function attach(events, context, callId, call, childId) {
|
|
2552
|
+
const state = context.subagents;
|
|
2553
|
+
if (call.childId !== void 0) return;
|
|
2554
|
+
const previous = state.children.get(childId);
|
|
2555
|
+
const resumed = call.resumeId === childId;
|
|
2556
|
+
if (previous !== void 0 && !resumed && (previous.ended !== void 0 || previous.callId))
|
|
2557
|
+
return;
|
|
2558
|
+
call.childId = childId;
|
|
2559
|
+
const announced = state.announced.get(childId);
|
|
2560
|
+
const names = {
|
|
2561
|
+
...announced,
|
|
2562
|
+
...call.description === void 0 ? {} : { title: call.description },
|
|
2563
|
+
...call.agent === void 0 ? {} : { role: call.agent },
|
|
2564
|
+
...call.model === void 0 ? {} : { model: call.model }
|
|
2565
|
+
};
|
|
2566
|
+
activate(context, childId, names, callId, call.background);
|
|
2567
|
+
const identity = { taskId: childId, ...identityOf(childId, names, callId) };
|
|
2568
|
+
emitEvent(events, context, "task.started", {
|
|
2569
|
+
...identity,
|
|
2570
|
+
...names.title === void 0 ? {} : { description: names.title }
|
|
2571
|
+
});
|
|
2572
|
+
if (resumed) emitEvent(events, context, "task.updated", { ...identity, status: "running" });
|
|
2573
|
+
}
|
|
2574
|
+
function attachSubagentCall(events, context, callId, metadata) {
|
|
2575
|
+
const call = context.subagents.calls.get(callId);
|
|
2576
|
+
const childId = readString2(metadata["sessionID"]);
|
|
2577
|
+
if (call === void 0 || childId === void 0) return;
|
|
2578
|
+
attach(events, context, callId, call, childId);
|
|
2579
|
+
}
|
|
2580
|
+
function settleSubagentCall(events, context, callId, result) {
|
|
2581
|
+
const state = context.subagents;
|
|
2582
|
+
const metadata = isRecord2(result.data["metadata"]) ? result.data["metadata"] : {};
|
|
2583
|
+
const call = state.calls.get(callId);
|
|
2584
|
+
const linked = readString2(metadata["sessionID"]);
|
|
2585
|
+
if (call === void 0 && (state.doneCalls.has(callId) || !state.children.has(linked ?? ""))) {
|
|
2586
|
+
return;
|
|
2587
|
+
}
|
|
2588
|
+
state.calls.delete(callId);
|
|
2589
|
+
state.doneCalls.add(callId);
|
|
2590
|
+
let childId = call === void 0 ? linked : call.childId;
|
|
2591
|
+
if (call !== void 0 && childId === void 0 && !result.failed) {
|
|
2592
|
+
childId = readString2(metadata["sessionID"]);
|
|
2593
|
+
if (childId !== void 0) attach(events, context, callId, call, childId);
|
|
2594
|
+
}
|
|
2595
|
+
const activation = childId === void 0 ? void 0 : state.children.get(childId);
|
|
2596
|
+
if (childId === void 0 || activation === void 0) return;
|
|
2597
|
+
if (activation.callId !== callId) return;
|
|
2598
|
+
if (!result.failed && readString2(metadata["status"]) === "running") {
|
|
2599
|
+
activation.background = true;
|
|
2600
|
+
return;
|
|
2601
|
+
}
|
|
2602
|
+
const error = result.data["error"];
|
|
2603
|
+
const message = isRecord2(error) ? readString2(error["message"]) : void 0;
|
|
2604
|
+
const aborted = isRecord2(error) && readString2(error["type"]) === "aborted";
|
|
2605
|
+
const text4 = result.failed ? aborted ? void 0 : message : result.output;
|
|
2606
|
+
attachResult(events, context, childId, summaryOf(text4));
|
|
2607
|
+
}
|
|
2608
|
+
function announceSubagentSession(context, sessionId, data) {
|
|
2609
|
+
const title = readString2(data["title"]);
|
|
2610
|
+
const role = readString2(data["agent"]);
|
|
2611
|
+
const model = modelName(decodeOpenCodeModelRef(data["model"]));
|
|
2612
|
+
context.subagents.announced.set(sessionId, {
|
|
2613
|
+
...title === void 0 ? {} : { title },
|
|
2614
|
+
...role === void 0 ? {} : { role },
|
|
2615
|
+
...model === void 0 ? {} : { model }
|
|
2616
|
+
});
|
|
2617
|
+
}
|
|
2618
|
+
function reportSubagentInbox(events, context, event) {
|
|
2619
|
+
if (event.type !== "session.inbox.enqueued") return false;
|
|
2620
|
+
const item = event.data["item"];
|
|
2621
|
+
const payload = isRecord2(item) && item["type"] === "synthetic" ? item["payload"] : void 0;
|
|
2622
|
+
const metadata = isRecord2(payload) ? payload["metadata"] : void 0;
|
|
2623
|
+
if (!isRecord2(payload) || !isRecord2(metadata) || metadata["source"] !== "subagent") return false;
|
|
2624
|
+
const childId = readString2(metadata["childID"]);
|
|
2625
|
+
const summary = summaryOf(readString2(payload["text"]));
|
|
2626
|
+
if (childId === void 0) return true;
|
|
2627
|
+
const fromSequence = context.subagents.children.get(childId)?.fromSequence;
|
|
2628
|
+
if (event.sequence !== void 0 && fromSequence !== void 0 && event.sequence <= fromSequence) {
|
|
2629
|
+
return true;
|
|
2630
|
+
}
|
|
2631
|
+
const current = context.subagents.children.get(childId);
|
|
2632
|
+
const started = context.subagents.startedSequence.get(childId);
|
|
2633
|
+
if (current?.ended !== void 0 && current.endBoundary !== void 0 && started !== void 0 && started > current.endBoundary) {
|
|
2634
|
+
return true;
|
|
2635
|
+
}
|
|
2636
|
+
const key = readString2(event.data["inboxID"]) ?? `${childId}:${summary ?? ""}`;
|
|
2637
|
+
if (context.subagents.reports.has(key)) return true;
|
|
2638
|
+
context.subagents.reports.add(key);
|
|
2639
|
+
attachResult(events, context, childId, summary);
|
|
2640
|
+
return true;
|
|
2641
|
+
}
|
|
2642
|
+
function subagentOutcome(settled) {
|
|
2643
|
+
switch (settled) {
|
|
2644
|
+
case void 0:
|
|
2645
|
+
return "running";
|
|
2646
|
+
case "succeeded":
|
|
2647
|
+
return "completed";
|
|
2648
|
+
case "failed":
|
|
2649
|
+
return "failed";
|
|
2650
|
+
case "interrupted":
|
|
2651
|
+
return "stopped";
|
|
2652
|
+
default:
|
|
2653
|
+
return void 0;
|
|
2654
|
+
}
|
|
2655
|
+
}
|
|
2656
|
+
function endSubagentFromExecution(events, context, childId, event) {
|
|
2657
|
+
if (event.type === "session.execution.started") {
|
|
2658
|
+
const state = context.subagents;
|
|
2659
|
+
if (event.sequence !== void 0) {
|
|
2660
|
+
state.startedSequence.set(
|
|
2661
|
+
childId,
|
|
2662
|
+
Math.max(state.startedSequence.get(childId) ?? event.sequence, event.sequence)
|
|
2663
|
+
);
|
|
2664
|
+
}
|
|
2665
|
+
return;
|
|
2666
|
+
}
|
|
2667
|
+
const status = subagentOutcome(event.type.replace("session.execution.", ""));
|
|
2668
|
+
if (status === void 0 || status === "running") return;
|
|
2669
|
+
const from = context.subagents.children.get(childId)?.fromChildSequence;
|
|
2670
|
+
if (event.sequence !== void 0 && from !== void 0 && event.sequence <= from) return;
|
|
2671
|
+
const activation = context.subagents.children.get(childId);
|
|
2672
|
+
if (activation?.ended !== void 0 && activation.endBoundary === void 0) {
|
|
2673
|
+
activation.endBoundary = event.sequence;
|
|
2674
|
+
return;
|
|
2675
|
+
}
|
|
2676
|
+
settleActivation(events, context, childId, status, event.sequence);
|
|
2677
|
+
}
|
|
2678
|
+
function noteChildSequence(context, event) {
|
|
2679
|
+
const state = context.subagents;
|
|
2680
|
+
if (event.sequence === void 0 || event.sessionId === void 0) return;
|
|
2681
|
+
const seen = state.childSequence.get(event.sessionId);
|
|
2682
|
+
state.childSequence.set(event.sessionId, Math.max(seen ?? event.sequence, event.sequence));
|
|
2683
|
+
}
|
|
2684
|
+
function finishSubagentsForTurn(events, context, outcome) {
|
|
2685
|
+
if (outcome.sessionEnds) return;
|
|
2686
|
+
for (const [childId, activation] of context.subagents.children) {
|
|
2687
|
+
if (activation.ended !== void 0 || activation.background) continue;
|
|
2688
|
+
activation.background = true;
|
|
2689
|
+
emitEvent(events, context, "task.updated", {
|
|
2690
|
+
taskId: childId,
|
|
2691
|
+
...identityOf(childId, activation, activation.callId),
|
|
2692
|
+
status: "idle"
|
|
2693
|
+
});
|
|
2694
|
+
}
|
|
2695
|
+
}
|
|
2696
|
+
function stopSubagents(events, context, confirmed) {
|
|
2697
|
+
for (const childId of confirmed) {
|
|
2698
|
+
const boundary = context.subagents.childSequence.get(childId);
|
|
2699
|
+
settleActivation(events, context, childId, "stopped", boundary);
|
|
2700
|
+
}
|
|
2701
|
+
}
|
|
2702
|
+
async function registerChildSessions(context, log) {
|
|
2703
|
+
try {
|
|
2704
|
+
let cursor;
|
|
2705
|
+
do {
|
|
2706
|
+
const page = await context.api.listChildSessions(context.sessionId, {
|
|
2707
|
+
...cursor === void 0 ? {} : { cursor },
|
|
2708
|
+
signal: context.eventAbort.signal
|
|
2709
|
+
});
|
|
2710
|
+
for (const child of page.sessions) context.childSessionIds.add(child.id);
|
|
2711
|
+
cursor = page.sessions.length === 0 ? void 0 : page.next;
|
|
2712
|
+
} while (cursor !== void 0);
|
|
2713
|
+
} catch (cause) {
|
|
2714
|
+
if (context.stopped || context.eventAbort.signal.aborted) return;
|
|
2715
|
+
log.warn(
|
|
2716
|
+
`OpenCode could not list the subagents of a resumed session: ${cause instanceof Error ? cause.message : String(cause)}`
|
|
2717
|
+
);
|
|
2718
|
+
}
|
|
2719
|
+
}
|
|
2720
|
+
function markSubagentsUnknown(events, context) {
|
|
2721
|
+
for (const [childId, activation] of context.subagents.children) {
|
|
2722
|
+
if (activation.ended !== void 0) continue;
|
|
2723
|
+
emitEvent(events, context, "task.updated", {
|
|
2724
|
+
taskId: childId,
|
|
2725
|
+
...identityOf(childId, activation, activation.callId),
|
|
2726
|
+
status: "idle",
|
|
2727
|
+
outcomeUnknown: true
|
|
2728
|
+
});
|
|
2729
|
+
}
|
|
2730
|
+
}
|
|
2731
|
+
|
|
2425
2732
|
// src/toolEvents.ts
|
|
2426
2733
|
var text2 = (data, key) => {
|
|
2427
2734
|
const value = data[key];
|
|
@@ -2589,6 +2896,7 @@ async function handleOpenCodeToolEvent(input) {
|
|
|
2589
2896
|
const name = text2(data, "name");
|
|
2590
2897
|
if (callId === void 0 || name === void 0) return true;
|
|
2591
2898
|
context.toolNames.set(callId, name);
|
|
2899
|
+
if (name === OPENCODE_SUBAGENT_TOOL) beginSubagentCall(context, callId);
|
|
2592
2900
|
emitTool(events, context, callId, "item.started", "inProgress", "pending");
|
|
2593
2901
|
return true;
|
|
2594
2902
|
}
|
|
@@ -2611,6 +2919,7 @@ async function handleOpenCodeToolEvent(input) {
|
|
|
2611
2919
|
emitTool(events, context, callId, "item.updated", "inProgress", "running", {
|
|
2612
2920
|
data: toolInput === void 0 ? {} : { input: toolInput }
|
|
2613
2921
|
});
|
|
2922
|
+
if (toolInput !== void 0) noteSubagentInput(context, callId, toolInput);
|
|
2614
2923
|
return true;
|
|
2615
2924
|
}
|
|
2616
2925
|
case "session.tool.progress": {
|
|
@@ -2627,6 +2936,7 @@ async function handleOpenCodeToolEvent(input) {
|
|
|
2627
2936
|
metadata: data["metadata"]
|
|
2628
2937
|
}
|
|
2629
2938
|
});
|
|
2939
|
+
attachSubagentCall(events, context, callId, data["metadata"]);
|
|
2630
2940
|
return true;
|
|
2631
2941
|
}
|
|
2632
2942
|
case "session.tool.success":
|
|
@@ -2657,6 +2967,7 @@ async function handleOpenCodeToolEvent(input) {
|
|
|
2657
2967
|
}
|
|
2658
2968
|
}
|
|
2659
2969
|
);
|
|
2970
|
+
settleSubagentCall(events, context, callId, { failed, data, output });
|
|
2660
2971
|
context.toolNames.delete(callId);
|
|
2661
2972
|
context.toolInputs.delete(callId);
|
|
2662
2973
|
context.openShells.delete(callId);
|
|
@@ -2677,6 +2988,9 @@ function finishOpenCodeTurn(events, context, outcome) {
|
|
|
2677
2988
|
context.stepModels.clear();
|
|
2678
2989
|
context.assistantTextByPartId.clear();
|
|
2679
2990
|
context.completedTextParts.clear();
|
|
2991
|
+
finishSubagentsForTurn(events, context, {
|
|
2992
|
+
sessionEnds: outcome.state !== "completed" && outcome.sessionEnds === true
|
|
2993
|
+
});
|
|
2680
2994
|
if (turnId === void 0) return;
|
|
2681
2995
|
if (outcome.state === "interrupted") {
|
|
2682
2996
|
emitEvent(events, context, "turn.aborted", { reason: outcome.reason }, { turnId });
|
|
@@ -2752,7 +3066,12 @@ function finishOpenCodeTurnOnExit(events, context, cause) {
|
|
|
2752
3066
|
...failureCategoryField(failureCategory)
|
|
2753
3067
|
});
|
|
2754
3068
|
}
|
|
2755
|
-
finishOpenCodeTurn(events, context, {
|
|
3069
|
+
finishOpenCodeTurn(events, context, {
|
|
3070
|
+
state: "failed",
|
|
3071
|
+
errorMessage: message,
|
|
3072
|
+
failureCategory,
|
|
3073
|
+
sessionEnds: true
|
|
3074
|
+
});
|
|
2756
3075
|
return { message, failureCategory, turnId };
|
|
2757
3076
|
}
|
|
2758
3077
|
|
|
@@ -2890,6 +3209,7 @@ function emitUnexpectedExit(events, context, cause) {
|
|
|
2890
3209
|
if (context.stopped) return;
|
|
2891
3210
|
context.stopped = true;
|
|
2892
3211
|
const { message, failureCategory, turnId } = finishOpenCodeTurnOnExit(events, context, cause);
|
|
3212
|
+
markSubagentsUnknown(events, context);
|
|
2893
3213
|
emitEvent(
|
|
2894
3214
|
events,
|
|
2895
3215
|
context,
|
|
@@ -2964,13 +3284,15 @@ function createOpenCodeSessionLifecycle(input) {
|
|
|
2964
3284
|
} else {
|
|
2965
3285
|
finishOpenCodeTurn(events, context, {
|
|
2966
3286
|
state: "interrupted",
|
|
2967
|
-
reason: "Interrupted by user."
|
|
3287
|
+
reason: "Interrupted by user.",
|
|
3288
|
+
sessionEnds: true
|
|
2968
3289
|
});
|
|
2969
3290
|
context.session = {
|
|
2970
3291
|
...context.session,
|
|
2971
3292
|
status: "stopped",
|
|
2972
3293
|
updatedAt: nowIso()
|
|
2973
3294
|
};
|
|
3295
|
+
markSubagentsUnknown(events, context);
|
|
2974
3296
|
emitEvent(events, context, "session.exited", {
|
|
2975
3297
|
reason: message,
|
|
2976
3298
|
recoverable: false,
|
|
@@ -3084,8 +3406,16 @@ async function applyOpenCodeTurnSelection(input) {
|
|
|
3084
3406
|
async function handleOtherSessionEvent(events, context, event, discoveredChild) {
|
|
3085
3407
|
const sessionId = event.sessionId;
|
|
3086
3408
|
if (sessionId === void 0) return;
|
|
3409
|
+
noteChildSequence(context, event);
|
|
3087
3410
|
if (event.type === "session.created") {
|
|
3088
|
-
if (discoveredChild)
|
|
3411
|
+
if (discoveredChild) {
|
|
3412
|
+
context.childSessionIds.add(sessionId);
|
|
3413
|
+
announceSubagentSession(context, sessionId, event.data);
|
|
3414
|
+
}
|
|
3415
|
+
return;
|
|
3416
|
+
}
|
|
3417
|
+
if (event.type.startsWith("session.execution.")) {
|
|
3418
|
+
endSubagentFromExecution(events, context, sessionId, event);
|
|
3089
3419
|
return;
|
|
3090
3420
|
}
|
|
3091
3421
|
if (event.type === "permission.asked" || event.type === "permission.replied") {
|
|
@@ -3126,6 +3456,7 @@ var abortableDelay = (ms, signal) => new Promise((resolve2) => {
|
|
|
3126
3456
|
signal.addEventListener("abort", done, { once: true });
|
|
3127
3457
|
});
|
|
3128
3458
|
async function recoverAfterReconnect(events, context, log) {
|
|
3459
|
+
markSubagentsUnknown(events, context);
|
|
3129
3460
|
const ours = (sessionId) => typeof sessionId === "string" && (sessionId === context.sessionId || context.childSessionIds.has(sessionId));
|
|
3130
3461
|
try {
|
|
3131
3462
|
for (const request of await context.api.pendingPermissions()) {
|
|
@@ -3227,10 +3558,12 @@ function startEventPump(input) {
|
|
|
3227
3558
|
await handleOtherSessionEvent(events, context, event, discoveredChild);
|
|
3228
3559
|
return;
|
|
3229
3560
|
}
|
|
3561
|
+
if (event.sessionId === context.sessionId) noteParentSequence(context, event);
|
|
3230
3562
|
if (handleOpenCodeTurnEvent({ events, context, event })) return;
|
|
3231
3563
|
if (noteOpenCodeSelectionEvent(context, event)) return;
|
|
3232
3564
|
if (handleOpenCodeCompactionEvent({ events, context, event })) return;
|
|
3233
3565
|
if (handleOpenCodeFormEvent({ events, context, event })) return;
|
|
3566
|
+
if (reportSubagentInbox(events, context, event)) return;
|
|
3234
3567
|
await handleOpenCodeToolEvent({ events, context, event });
|
|
3235
3568
|
});
|
|
3236
3569
|
}
|
|
@@ -4147,6 +4480,7 @@ function createOpenCodeInstance(config, host, attachments, lifecycleOptions) {
|
|
|
4147
4480
|
);
|
|
4148
4481
|
const resume = input.discardResumeCursor === true ? void 0 : parseOpenCodeResume(input.resumeCursor);
|
|
4149
4482
|
let sessionId;
|
|
4483
|
+
let resumedInPlace = false;
|
|
4150
4484
|
let selection = {
|
|
4151
4485
|
selectedModel: void 0,
|
|
4152
4486
|
selectedAgent: void 0
|
|
@@ -4204,6 +4538,7 @@ function createOpenCodeInstance(config, host, attachments, lifecycleOptions) {
|
|
|
4204
4538
|
await api.replacePermissions(existingSession.id, permissions);
|
|
4205
4539
|
sessionId = existingSession.id;
|
|
4206
4540
|
selection = openCodeSelectionOf(existingSession);
|
|
4541
|
+
resumedInPlace = true;
|
|
4207
4542
|
} else {
|
|
4208
4543
|
sessionId = await forkIntoDirectory(existingSession.id);
|
|
4209
4544
|
}
|
|
@@ -4245,6 +4580,7 @@ function createOpenCodeInstance(config, host, attachments, lifecycleOptions) {
|
|
|
4245
4580
|
pendingPermissions: /* @__PURE__ */ new Map(),
|
|
4246
4581
|
pendingForms: /* @__PURE__ */ new Map(),
|
|
4247
4582
|
childSessionIds: /* @__PURE__ */ new Set(),
|
|
4583
|
+
subagents: createOpenCodeSubagentState(),
|
|
4248
4584
|
settledRequestIds: /* @__PURE__ */ new Set(),
|
|
4249
4585
|
autoRepliedRequestIds: /* @__PURE__ */ new Set(),
|
|
4250
4586
|
declineReasons: /* @__PURE__ */ new Map(),
|
|
@@ -4275,6 +4611,7 @@ function createOpenCodeInstance(config, host, attachments, lifecycleOptions) {
|
|
|
4275
4611
|
);
|
|
4276
4612
|
emitEvent(events, context, "thread.started", { providerThreadId: sessionId });
|
|
4277
4613
|
emitEvent(events, context, "session.state.changed", { state: "ready" });
|
|
4614
|
+
if (resumedInPlace) await registerChildSessions(context, host.log);
|
|
4278
4615
|
return sessionFromContext(context);
|
|
4279
4616
|
} catch (cause) {
|
|
4280
4617
|
rememberPendingServer(input.threadId, server);
|
|
@@ -4381,11 +4718,19 @@ function createOpenCodeInstance(config, host, attachments, lifecycleOptions) {
|
|
|
4381
4718
|
context.pendingInterrupts += 1;
|
|
4382
4719
|
try {
|
|
4383
4720
|
await context.api.interrupt(context.sessionId);
|
|
4384
|
-
await Promise.all(
|
|
4721
|
+
const interrupted = await Promise.all(
|
|
4385
4722
|
[...context.childSessionIds].map(
|
|
4386
|
-
(child) => context.api.interrupt(child).
|
|
4723
|
+
(child) => context.api.interrupt(child).then(
|
|
4724
|
+
(confirmed) => confirmed ? child : void 0,
|
|
4725
|
+
() => void 0
|
|
4726
|
+
)
|
|
4387
4727
|
)
|
|
4388
4728
|
);
|
|
4729
|
+
stopSubagents(
|
|
4730
|
+
events,
|
|
4731
|
+
context,
|
|
4732
|
+
interrupted.filter((child) => child !== void 0)
|
|
4733
|
+
);
|
|
4389
4734
|
const active = context.session.activeTurnId;
|
|
4390
4735
|
if (active && (turnId === void 0 || turnId === active)) {
|
|
4391
4736
|
finishOpenCodeTurn(events, context, {
|
|
@@ -4503,7 +4848,7 @@ var executors = [
|
|
|
4503
4848
|
];
|
|
4504
4849
|
var index_default = definePlugin({
|
|
4505
4850
|
id: "agent-opencode",
|
|
4506
|
-
apiVersion:
|
|
4851
|
+
apiVersion: 49,
|
|
4507
4852
|
executors,
|
|
4508
4853
|
activate(ctx) {
|
|
4509
4854
|
capturedAttachments = ctx.attachments;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ycodium-ai/agent-opencode",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.3608",
|
|
4
4
|
"description": "OpenCode executor: OpenCode CLI (2.x) over its HTTP API.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"ycodium",
|
|
@@ -22,9 +22,9 @@
|
|
|
22
22
|
"esbuild": "0.28.1",
|
|
23
23
|
"vite-plus": "0.2.2",
|
|
24
24
|
"ycodium-executor-testing": "0.0.0",
|
|
25
|
-
"ycodium-
|
|
26
|
-
"
|
|
27
|
-
"ycodium-
|
|
25
|
+
"@ycodium-ai/plugin-api": "0.2.3608",
|
|
26
|
+
"ycodium-fetch-ports": "0.0.0",
|
|
27
|
+
"ycodium-sse-stream": "0.0.0"
|
|
28
28
|
},
|
|
29
29
|
"ycodium": {
|
|
30
30
|
"plugin": "./dist/index.js",
|