@ycodium-ai/agent-opencode 0.2.3935 → 0.2.3982
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 +120 -161
- package/package.json +4 -4
package/dist/index.js
CHANGED
|
@@ -4,6 +4,9 @@ const require = __ycodiumCreateRequire(import.meta.url);
|
|
|
4
4
|
// src/index.ts
|
|
5
5
|
import { definePlugin } from "@ycodium-ai/plugin-api";
|
|
6
6
|
|
|
7
|
+
// ../../packages/plugin-api/src/version.ts
|
|
8
|
+
var PLUGIN_API_VERSION = 76;
|
|
9
|
+
|
|
7
10
|
// src/card.ts
|
|
8
11
|
var OPENCODE_DRIVER_KIND = "opencode";
|
|
9
12
|
var OPENCODE_CARD = {
|
|
@@ -268,40 +271,6 @@ function hideGenericEffectText(message) {
|
|
|
268
271
|
return trimmed;
|
|
269
272
|
}
|
|
270
273
|
|
|
271
|
-
// src/events.ts
|
|
272
|
-
var EventBus = class {
|
|
273
|
-
#queued = [];
|
|
274
|
-
#waiters = [];
|
|
275
|
-
#closed = false;
|
|
276
|
-
push(event) {
|
|
277
|
-
const waiter = this.#waiters.shift();
|
|
278
|
-
if (waiter) {
|
|
279
|
-
waiter({ done: false, value: event });
|
|
280
|
-
return;
|
|
281
|
-
}
|
|
282
|
-
this.#queued.push(event);
|
|
283
|
-
}
|
|
284
|
-
close() {
|
|
285
|
-
if (this.#closed) return;
|
|
286
|
-
this.#closed = true;
|
|
287
|
-
while (this.#waiters.length > 0) {
|
|
288
|
-
this.#waiters.shift()?.({ done: true, value: void 0 });
|
|
289
|
-
}
|
|
290
|
-
}
|
|
291
|
-
[Symbol.asyncIterator]() {
|
|
292
|
-
return {
|
|
293
|
-
next: () => {
|
|
294
|
-
const queued = this.#queued.shift();
|
|
295
|
-
if (queued !== void 0) return Promise.resolve({ done: false, value: queued });
|
|
296
|
-
if (this.#closed) return Promise.resolve({ done: true, value: void 0 });
|
|
297
|
-
return new Promise((resolve2) => {
|
|
298
|
-
this.#waiters.push(resolve2);
|
|
299
|
-
});
|
|
300
|
-
}
|
|
301
|
-
};
|
|
302
|
-
}
|
|
303
|
-
};
|
|
304
|
-
|
|
305
274
|
// src/fileParts.ts
|
|
306
275
|
import * as NodeFSP from "node:fs/promises";
|
|
307
276
|
import * as NodeOS from "node:os";
|
|
@@ -390,7 +359,7 @@ function withLastError(session, lastError, failureCategory) {
|
|
|
390
359
|
function openCodeModelLabel(ref) {
|
|
391
360
|
return `${ref.providerID}/${ref.id}`;
|
|
392
361
|
}
|
|
393
|
-
function emitEvent(
|
|
362
|
+
function emitEvent(context, type2, payload, extra) {
|
|
394
363
|
if (context.lifecycle?.canEmit === false) return;
|
|
395
364
|
const turnId = extra?.turnId ?? context.session.activeTurnId;
|
|
396
365
|
const event = {
|
|
@@ -406,16 +375,16 @@ function emitEvent(events, context, type2, payload, extra) {
|
|
|
406
375
|
...extra?.requestId ? { requestId: extra.requestId } : {},
|
|
407
376
|
providerRefs: {}
|
|
408
377
|
};
|
|
409
|
-
events.
|
|
378
|
+
context.events.emit(event);
|
|
410
379
|
}
|
|
411
|
-
function reportOpenCodeModel(
|
|
380
|
+
function reportOpenCodeModel(context, ref) {
|
|
412
381
|
const turnId = context.session.activeTurnId;
|
|
413
382
|
if (turnId === void 0) return;
|
|
414
383
|
const model = openCodeModelLabel(ref);
|
|
415
384
|
const key = `${turnId}|${model}`;
|
|
416
385
|
if (context.reportedModelKey === key) return;
|
|
417
386
|
context.reportedModelKey = key;
|
|
418
|
-
emitEvent(
|
|
387
|
+
emitEvent(context, "thread.metadata.updated", { model, modelSource: "reported" });
|
|
419
388
|
}
|
|
420
389
|
|
|
421
390
|
// src/formEvents.ts
|
|
@@ -446,16 +415,16 @@ function readForm(form) {
|
|
|
446
415
|
}
|
|
447
416
|
return fields.length === 0 ? void 0 : { id, pending: { sessionId, fields } };
|
|
448
417
|
}
|
|
449
|
-
function settle(
|
|
418
|
+
function settle(context, formId, answers) {
|
|
450
419
|
if (!context.pendingForms.delete(formId)) return;
|
|
451
|
-
emitEvent(
|
|
420
|
+
emitEvent(context, "user-input.resolved", { answers }, { requestId: formId });
|
|
452
421
|
if (context.pendingForms.size === 0 && context.pendingPermissions.size === 0 && context.session.activeTurnId !== void 0) {
|
|
453
422
|
context.session = { ...context.session, status: "running", updatedAt: nowIso() };
|
|
454
|
-
emitEvent(
|
|
423
|
+
emitEvent(context, "session.state.changed", { state: "running" });
|
|
455
424
|
}
|
|
456
425
|
}
|
|
457
426
|
function handleOpenCodeFormEvent(input) {
|
|
458
|
-
const {
|
|
427
|
+
const { context, event } = input;
|
|
459
428
|
switch (event.type) {
|
|
460
429
|
case "form.created": {
|
|
461
430
|
const form = isRecord2(event.data["form"]) ? readForm(event.data["form"]) : void 0;
|
|
@@ -467,7 +436,6 @@ function handleOpenCodeFormEvent(input) {
|
|
|
467
436
|
if (context.pendingForms.has(id)) return true;
|
|
468
437
|
context.pendingForms.set(id, pending);
|
|
469
438
|
emitEvent(
|
|
470
|
-
events,
|
|
471
439
|
context,
|
|
472
440
|
"user-input.requested",
|
|
473
441
|
{
|
|
@@ -481,17 +449,17 @@ function handleOpenCodeFormEvent(input) {
|
|
|
481
449
|
{ requestId: id }
|
|
482
450
|
);
|
|
483
451
|
context.session = { ...context.session, status: "waiting", updatedAt: nowIso() };
|
|
484
|
-
emitEvent(
|
|
452
|
+
emitEvent(context, "session.state.changed", { state: "waiting" });
|
|
485
453
|
return true;
|
|
486
454
|
}
|
|
487
455
|
case "form.replied": {
|
|
488
456
|
const id = text(event.data, "id");
|
|
489
|
-
if (id !== void 0) settle(
|
|
457
|
+
if (id !== void 0) settle(context, id, event.data["answer"]);
|
|
490
458
|
return true;
|
|
491
459
|
}
|
|
492
460
|
case "form.cancelled": {
|
|
493
461
|
const id = text(event.data, "id");
|
|
494
|
-
if (id !== void 0) settle(
|
|
462
|
+
if (id !== void 0) settle(context, id, { rejected: true });
|
|
495
463
|
return true;
|
|
496
464
|
}
|
|
497
465
|
default:
|
|
@@ -2431,7 +2399,7 @@ function takeTurnUsage(usageByMessageId, sessionId) {
|
|
|
2431
2399
|
|
|
2432
2400
|
// src/compactionEvents.ts
|
|
2433
2401
|
function handleOpenCodeCompactionEvent(input) {
|
|
2434
|
-
const {
|
|
2402
|
+
const { context, event } = input;
|
|
2435
2403
|
switch (event.type) {
|
|
2436
2404
|
case "session.compaction.started": {
|
|
2437
2405
|
const compaction = {
|
|
@@ -2440,8 +2408,8 @@ function handleOpenCodeCompactionEvent(input) {
|
|
|
2440
2408
|
};
|
|
2441
2409
|
context.compaction = compaction;
|
|
2442
2410
|
context.session = { ...context.session, status: "compacting", updatedAt: nowIso() };
|
|
2443
|
-
emitEvent(
|
|
2444
|
-
emitEvent(
|
|
2411
|
+
emitEvent(context, "session.state.changed", { state: "compacting" });
|
|
2412
|
+
emitEvent(context, "thread.state.changed", { state: "compacting", compaction });
|
|
2445
2413
|
return true;
|
|
2446
2414
|
}
|
|
2447
2415
|
case "session.compaction.delta":
|
|
@@ -2456,10 +2424,10 @@ function handleOpenCodeCompactionEvent(input) {
|
|
|
2456
2424
|
typeof summary === "string" ? summary : void 0
|
|
2457
2425
|
);
|
|
2458
2426
|
context.compaction = void 0;
|
|
2459
|
-
emitEvent(
|
|
2427
|
+
emitEvent(context, "thread.state.changed", { state: "compacted", compaction });
|
|
2460
2428
|
if (context.session.activeTurnId !== void 0) {
|
|
2461
2429
|
context.session = { ...context.session, status: "running", updatedAt: nowIso() };
|
|
2462
|
-
emitEvent(
|
|
2430
|
+
emitEvent(context, "session.state.changed", { state: "running" });
|
|
2463
2431
|
}
|
|
2464
2432
|
return true;
|
|
2465
2433
|
}
|
|
@@ -2526,25 +2494,25 @@ function activate(context, childId, names, callId, background) {
|
|
|
2526
2494
|
context.childSessionIds.add(childId);
|
|
2527
2495
|
return activation;
|
|
2528
2496
|
}
|
|
2529
|
-
function settleActivation(
|
|
2497
|
+
function settleActivation(context, childId, status, boundary) {
|
|
2530
2498
|
const state = context.subagents;
|
|
2531
2499
|
const activation = state.children.get(childId);
|
|
2532
2500
|
if (activation === void 0 || activation.ended !== void 0) return;
|
|
2533
2501
|
activation.ended = status;
|
|
2534
2502
|
activation.endBoundary = boundary;
|
|
2535
2503
|
const identity = { taskId: childId, ...identityOf(childId, activation, activation.callId) };
|
|
2536
|
-
emitEvent(
|
|
2504
|
+
emitEvent(context, "task.completed", {
|
|
2537
2505
|
...identity,
|
|
2538
2506
|
status,
|
|
2539
2507
|
...activation.summary === void 0 ? {} : { summary: activation.summary }
|
|
2540
2508
|
});
|
|
2541
2509
|
}
|
|
2542
|
-
function attachResult(
|
|
2510
|
+
function attachResult(context, childId, summary) {
|
|
2543
2511
|
const activation = context.subagents.children.get(childId);
|
|
2544
2512
|
if (activation === void 0 || summary === void 0 || activation.summary !== void 0) return;
|
|
2545
2513
|
activation.summary = summary;
|
|
2546
2514
|
if (activation.ended === void 0) return;
|
|
2547
|
-
emitEvent(
|
|
2515
|
+
emitEvent(context, "task.completed", {
|
|
2548
2516
|
taskId: childId,
|
|
2549
2517
|
...identityOf(childId, activation, activation.callId),
|
|
2550
2518
|
status: activation.ended,
|
|
@@ -2569,7 +2537,7 @@ function noteSubagentInput(context, callId, input) {
|
|
|
2569
2537
|
if (resumeId !== void 0) call.resumeId = resumeId;
|
|
2570
2538
|
call.background = input["background"] === true;
|
|
2571
2539
|
}
|
|
2572
|
-
function attach(
|
|
2540
|
+
function attach(context, callId, call, childId) {
|
|
2573
2541
|
const state = context.subagents;
|
|
2574
2542
|
if (call.childId !== void 0) return;
|
|
2575
2543
|
const previous = state.children.get(childId);
|
|
@@ -2586,19 +2554,19 @@ function attach(events, context, callId, call, childId) {
|
|
|
2586
2554
|
};
|
|
2587
2555
|
activate(context, childId, names, callId, call.background);
|
|
2588
2556
|
const identity = { taskId: childId, ...identityOf(childId, names, callId) };
|
|
2589
|
-
emitEvent(
|
|
2557
|
+
emitEvent(context, "task.started", {
|
|
2590
2558
|
...identity,
|
|
2591
2559
|
...names.title === void 0 ? {} : { description: names.title }
|
|
2592
2560
|
});
|
|
2593
|
-
if (resumed) emitEvent(
|
|
2561
|
+
if (resumed) emitEvent(context, "task.updated", { ...identity, status: "running" });
|
|
2594
2562
|
}
|
|
2595
|
-
function attachSubagentCall(
|
|
2563
|
+
function attachSubagentCall(context, callId, metadata) {
|
|
2596
2564
|
const call = context.subagents.calls.get(callId);
|
|
2597
2565
|
const childId = readString2(metadata["sessionID"]);
|
|
2598
2566
|
if (call === void 0 || childId === void 0) return;
|
|
2599
|
-
attach(
|
|
2567
|
+
attach(context, callId, call, childId);
|
|
2600
2568
|
}
|
|
2601
|
-
function settleSubagentCall(
|
|
2569
|
+
function settleSubagentCall(context, callId, result) {
|
|
2602
2570
|
const state = context.subagents;
|
|
2603
2571
|
const metadata = isRecord2(result.data["metadata"]) ? result.data["metadata"] : {};
|
|
2604
2572
|
const call = state.calls.get(callId);
|
|
@@ -2611,7 +2579,7 @@ function settleSubagentCall(events, context, callId, result) {
|
|
|
2611
2579
|
let childId = call === void 0 ? linked : call.childId;
|
|
2612
2580
|
if (call !== void 0 && childId === void 0 && !result.failed) {
|
|
2613
2581
|
childId = readString2(metadata["sessionID"]);
|
|
2614
|
-
if (childId !== void 0) attach(
|
|
2582
|
+
if (childId !== void 0) attach(context, callId, call, childId);
|
|
2615
2583
|
}
|
|
2616
2584
|
const activation = childId === void 0 ? void 0 : state.children.get(childId);
|
|
2617
2585
|
if (childId === void 0 || activation === void 0) return;
|
|
@@ -2624,7 +2592,7 @@ function settleSubagentCall(events, context, callId, result) {
|
|
|
2624
2592
|
const message = isRecord2(error) ? readString2(error["message"]) : void 0;
|
|
2625
2593
|
const aborted = isRecord2(error) && readString2(error["type"]) === "aborted";
|
|
2626
2594
|
const text4 = result.failed ? aborted ? void 0 : message : result.output;
|
|
2627
|
-
attachResult(
|
|
2595
|
+
attachResult(context, childId, summaryOf(text4));
|
|
2628
2596
|
}
|
|
2629
2597
|
function announceSubagentSession(context, sessionId, data) {
|
|
2630
2598
|
const title = readString2(data["title"]);
|
|
@@ -2636,7 +2604,7 @@ function announceSubagentSession(context, sessionId, data) {
|
|
|
2636
2604
|
...model === void 0 ? {} : { model }
|
|
2637
2605
|
});
|
|
2638
2606
|
}
|
|
2639
|
-
function reportSubagentInbox(
|
|
2607
|
+
function reportSubagentInbox(context, event) {
|
|
2640
2608
|
if (event.type !== "session.inbox.enqueued") return false;
|
|
2641
2609
|
const item = event.data["item"];
|
|
2642
2610
|
const payload = isRecord2(item) && item["type"] === "synthetic" ? item["payload"] : void 0;
|
|
@@ -2657,7 +2625,7 @@ function reportSubagentInbox(events, context, event) {
|
|
|
2657
2625
|
const key = readString2(event.data["inboxID"]) ?? `${childId}:${summary ?? ""}`;
|
|
2658
2626
|
if (context.subagents.reports.has(key)) return true;
|
|
2659
2627
|
context.subagents.reports.add(key);
|
|
2660
|
-
attachResult(
|
|
2628
|
+
attachResult(context, childId, summary);
|
|
2661
2629
|
return true;
|
|
2662
2630
|
}
|
|
2663
2631
|
function subagentOutcome(settled) {
|
|
@@ -2674,7 +2642,7 @@ function subagentOutcome(settled) {
|
|
|
2674
2642
|
return void 0;
|
|
2675
2643
|
}
|
|
2676
2644
|
}
|
|
2677
|
-
function endSubagentFromExecution(
|
|
2645
|
+
function endSubagentFromExecution(context, childId, event) {
|
|
2678
2646
|
if (event.type === "session.execution.started") {
|
|
2679
2647
|
const state = context.subagents;
|
|
2680
2648
|
if (event.sequence !== void 0) {
|
|
@@ -2694,7 +2662,7 @@ function endSubagentFromExecution(events, context, childId, event) {
|
|
|
2694
2662
|
activation.endBoundary = event.sequence;
|
|
2695
2663
|
return;
|
|
2696
2664
|
}
|
|
2697
|
-
settleActivation(
|
|
2665
|
+
settleActivation(context, childId, status, event.sequence);
|
|
2698
2666
|
}
|
|
2699
2667
|
function noteChildSequence(context, event) {
|
|
2700
2668
|
const state = context.subagents;
|
|
@@ -2702,22 +2670,22 @@ function noteChildSequence(context, event) {
|
|
|
2702
2670
|
const seen = state.childSequence.get(event.sessionId);
|
|
2703
2671
|
state.childSequence.set(event.sessionId, Math.max(seen ?? event.sequence, event.sequence));
|
|
2704
2672
|
}
|
|
2705
|
-
function finishSubagentsForTurn(
|
|
2673
|
+
function finishSubagentsForTurn(context, outcome) {
|
|
2706
2674
|
if (outcome.sessionEnds) return;
|
|
2707
2675
|
for (const [childId, activation] of context.subagents.children) {
|
|
2708
2676
|
if (activation.ended !== void 0 || activation.background) continue;
|
|
2709
2677
|
activation.background = true;
|
|
2710
|
-
emitEvent(
|
|
2678
|
+
emitEvent(context, "task.updated", {
|
|
2711
2679
|
taskId: childId,
|
|
2712
2680
|
...identityOf(childId, activation, activation.callId),
|
|
2713
2681
|
status: "idle"
|
|
2714
2682
|
});
|
|
2715
2683
|
}
|
|
2716
2684
|
}
|
|
2717
|
-
function stopSubagents(
|
|
2685
|
+
function stopSubagents(context, confirmed) {
|
|
2718
2686
|
for (const childId of confirmed) {
|
|
2719
2687
|
const boundary = context.subagents.childSequence.get(childId);
|
|
2720
|
-
settleActivation(
|
|
2688
|
+
settleActivation(context, childId, "stopped", boundary);
|
|
2721
2689
|
}
|
|
2722
2690
|
}
|
|
2723
2691
|
async function registerChildSessions(context, log) {
|
|
@@ -2738,10 +2706,10 @@ async function registerChildSessions(context, log) {
|
|
|
2738
2706
|
);
|
|
2739
2707
|
}
|
|
2740
2708
|
}
|
|
2741
|
-
function markSubagentsUnknown(
|
|
2709
|
+
function markSubagentsUnknown(context) {
|
|
2742
2710
|
for (const [childId, activation] of context.subagents.children) {
|
|
2743
2711
|
if (activation.ended !== void 0) continue;
|
|
2744
|
-
emitEvent(
|
|
2712
|
+
emitEvent(context, "task.updated", {
|
|
2745
2713
|
taskId: childId,
|
|
2746
2714
|
...identityOf(childId, activation, activation.callId),
|
|
2747
2715
|
status: "idle",
|
|
@@ -2785,6 +2753,15 @@ function openCodeRequestType(action) {
|
|
|
2785
2753
|
return "unknown";
|
|
2786
2754
|
}
|
|
2787
2755
|
}
|
|
2756
|
+
var DATA_URI = /^data:([^;,]+);base64,(.*)$/su;
|
|
2757
|
+
var EXTRACTED_MEDIA = /^(?:image|audio|video)\/|^application\/pdf$/iu;
|
|
2758
|
+
function inlineMediaOf(part) {
|
|
2759
|
+
if (!isRecord2(part) || part["type"] !== "file" || typeof part["uri"] !== "string") return [];
|
|
2760
|
+
const match = DATA_URI.exec(part["uri"]);
|
|
2761
|
+
if (match === null) return [];
|
|
2762
|
+
const mimeType = typeof part["mime"] === "string" ? part["mime"] : match[1];
|
|
2763
|
+
return EXTRACTED_MEDIA.test(mimeType) ? [{ mimeType, data: match[2] }] : [];
|
|
2764
|
+
}
|
|
2788
2765
|
function contentText(content) {
|
|
2789
2766
|
if (!Array.isArray(content)) return void 0;
|
|
2790
2767
|
const joined = content.map(
|
|
@@ -2811,10 +2788,9 @@ function fileChangesOf(metadata) {
|
|
|
2811
2788
|
}
|
|
2812
2789
|
return changes.length > 0 ? changes : void 0;
|
|
2813
2790
|
}
|
|
2814
|
-
function emitTool(
|
|
2791
|
+
function emitTool(context, callId, phase, status, state, extra = {}) {
|
|
2815
2792
|
const tool = context.toolNames.get(callId) ?? "tool";
|
|
2816
2793
|
emitEvent(
|
|
2817
|
-
events,
|
|
2818
2794
|
context,
|
|
2819
2795
|
phase,
|
|
2820
2796
|
{
|
|
@@ -2827,7 +2803,7 @@ function emitTool(events, context, callId, phase, status, state, extra = {}) {
|
|
|
2827
2803
|
{ itemId: callId }
|
|
2828
2804
|
);
|
|
2829
2805
|
}
|
|
2830
|
-
async function openPermission(
|
|
2806
|
+
async function openPermission(context, data) {
|
|
2831
2807
|
const requestId = text2(data, "id");
|
|
2832
2808
|
const action = text2(data, "action");
|
|
2833
2809
|
const sessionId = text2(data, "sessionID") ?? context.sessionId;
|
|
@@ -2856,7 +2832,6 @@ async function openPermission(events, context, data) {
|
|
|
2856
2832
|
...toolCallId === void 0 ? {} : { toolCallId }
|
|
2857
2833
|
});
|
|
2858
2834
|
emitEvent(
|
|
2859
|
-
events,
|
|
2860
2835
|
context,
|
|
2861
2836
|
"request.opened",
|
|
2862
2837
|
{
|
|
@@ -2870,9 +2845,9 @@ async function openPermission(events, context, data) {
|
|
|
2870
2845
|
{ requestId }
|
|
2871
2846
|
);
|
|
2872
2847
|
context.session = { ...context.session, status: "waiting", updatedAt: nowIso() };
|
|
2873
|
-
emitEvent(
|
|
2848
|
+
emitEvent(context, "session.state.changed", { state: "waiting" });
|
|
2874
2849
|
}
|
|
2875
|
-
function resolvePermission(
|
|
2850
|
+
function resolvePermission(context, data) {
|
|
2876
2851
|
const requestId = text2(data, "requestID");
|
|
2877
2852
|
const reply = text2(data, "reply");
|
|
2878
2853
|
if (requestId === void 0 || reply === void 0) return;
|
|
@@ -2887,7 +2862,6 @@ function resolvePermission(events, context, data) {
|
|
|
2887
2862
|
const reason = context.declineReasons.get(requestId);
|
|
2888
2863
|
context.declineReasons.delete(requestId);
|
|
2889
2864
|
emitEvent(
|
|
2890
|
-
events,
|
|
2891
2865
|
context,
|
|
2892
2866
|
"request.resolved",
|
|
2893
2867
|
{
|
|
@@ -2899,18 +2873,18 @@ function resolvePermission(events, context, data) {
|
|
|
2899
2873
|
);
|
|
2900
2874
|
if (context.pendingPermissions.size === 0 && context.pendingForms.size === 0 && context.session.activeTurnId !== void 0) {
|
|
2901
2875
|
context.session = { ...context.session, status: "running", updatedAt: nowIso() };
|
|
2902
|
-
emitEvent(
|
|
2876
|
+
emitEvent(context, "session.state.changed", { state: "running" });
|
|
2903
2877
|
}
|
|
2904
2878
|
}
|
|
2905
2879
|
async function handleOpenCodeToolEvent(input) {
|
|
2906
|
-
const {
|
|
2880
|
+
const { context, event } = input;
|
|
2907
2881
|
const data = event.data;
|
|
2908
2882
|
switch (event.type) {
|
|
2909
2883
|
case "permission.asked":
|
|
2910
|
-
await openPermission(
|
|
2884
|
+
await openPermission(context, data);
|
|
2911
2885
|
return true;
|
|
2912
2886
|
case "permission.replied":
|
|
2913
|
-
resolvePermission(
|
|
2887
|
+
resolvePermission(context, data);
|
|
2914
2888
|
return true;
|
|
2915
2889
|
case "session.tool.input.started": {
|
|
2916
2890
|
const callId = text2(data, "id");
|
|
@@ -2918,7 +2892,7 @@ async function handleOpenCodeToolEvent(input) {
|
|
|
2918
2892
|
if (callId === void 0 || name === void 0) return true;
|
|
2919
2893
|
context.toolNames.set(callId, name);
|
|
2920
2894
|
if (name === OPENCODE_SUBAGENT_TOOL) beginSubagentCall(context, callId);
|
|
2921
|
-
emitTool(
|
|
2895
|
+
emitTool(context, callId, "item.started", "inProgress", "pending");
|
|
2922
2896
|
return true;
|
|
2923
2897
|
}
|
|
2924
2898
|
case "session.tool.input.delta":
|
|
@@ -2927,7 +2901,7 @@ async function handleOpenCodeToolEvent(input) {
|
|
|
2927
2901
|
const callId = text2(data, "id");
|
|
2928
2902
|
const argumentText = text2(data, "text");
|
|
2929
2903
|
if (callId === void 0 || argumentText === void 0) return true;
|
|
2930
|
-
emitTool(
|
|
2904
|
+
emitTool(context, callId, "item.updated", "inProgress", "pending", {
|
|
2931
2905
|
data: { argumentText }
|
|
2932
2906
|
});
|
|
2933
2907
|
return true;
|
|
@@ -2937,7 +2911,7 @@ async function handleOpenCodeToolEvent(input) {
|
|
|
2937
2911
|
if (callId === void 0) return true;
|
|
2938
2912
|
const toolInput = isRecord2(data["input"]) ? data["input"] : void 0;
|
|
2939
2913
|
if (toolInput !== void 0) context.toolInputs.set(callId, toolInput);
|
|
2940
|
-
emitTool(
|
|
2914
|
+
emitTool(context, callId, "item.updated", "inProgress", "running", {
|
|
2941
2915
|
data: toolInput === void 0 ? {} : { input: toolInput }
|
|
2942
2916
|
});
|
|
2943
2917
|
if (toolInput !== void 0) noteSubagentInput(context, callId, toolInput);
|
|
@@ -2951,13 +2925,13 @@ async function handleOpenCodeToolEvent(input) {
|
|
|
2951
2925
|
if (context.toolNames.get(callId) === "shell" && text2(data["metadata"], "shellID") !== void 0 && typeof command === "string" && !context.openShells.has(callId)) {
|
|
2952
2926
|
context.openShells.set(callId, { command, startedAtMs: Date.now() });
|
|
2953
2927
|
}
|
|
2954
|
-
emitTool(
|
|
2928
|
+
emitTool(context, callId, "item.updated", "inProgress", "running", {
|
|
2955
2929
|
data: {
|
|
2956
2930
|
...toolInput === void 0 ? {} : { input: toolInput },
|
|
2957
2931
|
metadata: data["metadata"]
|
|
2958
2932
|
}
|
|
2959
2933
|
});
|
|
2960
|
-
attachSubagentCall(
|
|
2934
|
+
attachSubagentCall(context, callId, data["metadata"]);
|
|
2961
2935
|
return true;
|
|
2962
2936
|
}
|
|
2963
2937
|
case "session.tool.success":
|
|
@@ -2972,8 +2946,8 @@ async function handleOpenCodeToolEvent(input) {
|
|
|
2972
2946
|
}
|
|
2973
2947
|
const output = contentText(data["content"]) ?? (failed && isRecord2(error) ? text2(error, "message") : void 0);
|
|
2974
2948
|
const toolInput = context.toolInputs.get(callId);
|
|
2949
|
+
const media = Array.isArray(data["content"]) ? data["content"].flatMap(inlineMediaOf) : [];
|
|
2975
2950
|
emitTool(
|
|
2976
|
-
events,
|
|
2977
2951
|
context,
|
|
2978
2952
|
callId,
|
|
2979
2953
|
"item.completed",
|
|
@@ -2984,11 +2958,12 @@ async function handleOpenCodeToolEvent(input) {
|
|
|
2984
2958
|
data: {
|
|
2985
2959
|
...toolInput === void 0 ? {} : { input: toolInput },
|
|
2986
2960
|
...output === void 0 ? {} : { output },
|
|
2961
|
+
...media.length === 0 ? {} : { content: media },
|
|
2987
2962
|
...isRecord2(data["metadata"]) ? { metadata: data["metadata"] } : {}
|
|
2988
2963
|
}
|
|
2989
2964
|
}
|
|
2990
2965
|
);
|
|
2991
|
-
settleSubagentCall(
|
|
2966
|
+
settleSubagentCall(context, callId, { failed, data, output });
|
|
2992
2967
|
context.toolNames.delete(callId);
|
|
2993
2968
|
context.toolInputs.delete(callId);
|
|
2994
2969
|
context.openShells.delete(callId);
|
|
@@ -3000,7 +2975,7 @@ async function handleOpenCodeToolEvent(input) {
|
|
|
3000
2975
|
}
|
|
3001
2976
|
|
|
3002
2977
|
// src/turnCompletion.ts
|
|
3003
|
-
function finishOpenCodeTurn(
|
|
2978
|
+
function finishOpenCodeTurn(context, outcome) {
|
|
3004
2979
|
const turnId = context.session.activeTurnId;
|
|
3005
2980
|
const usage = takeTurnUsage(context.usageByMessageId, context.sessionId);
|
|
3006
2981
|
context.abortedByDecline = false;
|
|
@@ -3009,18 +2984,17 @@ function finishOpenCodeTurn(events, context, outcome) {
|
|
|
3009
2984
|
context.stepModels.clear();
|
|
3010
2985
|
context.assistantTextByPartId.clear();
|
|
3011
2986
|
context.completedTextParts.clear();
|
|
3012
|
-
finishSubagentsForTurn(
|
|
2987
|
+
finishSubagentsForTurn(context, {
|
|
3013
2988
|
sessionEnds: outcome.state !== "completed" && outcome.sessionEnds === true
|
|
3014
2989
|
});
|
|
3015
2990
|
if (turnId === void 0) return;
|
|
3016
2991
|
if (outcome.state === "interrupted") {
|
|
3017
|
-
emitEvent(
|
|
2992
|
+
emitEvent(context, "turn.aborted", { reason: outcome.reason }, { turnId });
|
|
3018
2993
|
context.session = { ...withoutActiveTurn(context.session), updatedAt: nowIso() };
|
|
3019
2994
|
return;
|
|
3020
2995
|
}
|
|
3021
2996
|
if (outcome.state === "completed") {
|
|
3022
2997
|
emitEvent(
|
|
3023
|
-
events,
|
|
3024
2998
|
context,
|
|
3025
2999
|
"turn.completed",
|
|
3026
3000
|
{ state: "completed", ...usage ? { tokenUsage: usage } : {} },
|
|
@@ -3031,7 +3005,6 @@ function finishOpenCodeTurn(events, context, outcome) {
|
|
|
3031
3005
|
}
|
|
3032
3006
|
const { errorMessage, failureCategory } = outcome;
|
|
3033
3007
|
emitEvent(
|
|
3034
|
-
events,
|
|
3035
3008
|
context,
|
|
3036
3009
|
"turn.completed",
|
|
3037
3010
|
{
|
|
@@ -3047,16 +3020,16 @@ function finishOpenCodeTurn(events, context, outcome) {
|
|
|
3047
3020
|
updatedAt: nowIso()
|
|
3048
3021
|
};
|
|
3049
3022
|
}
|
|
3050
|
-
function finishOpenCodeTurnEvent(
|
|
3023
|
+
function finishOpenCodeTurnEvent(context, event) {
|
|
3051
3024
|
switch (event.type) {
|
|
3052
3025
|
case "session.execution.succeeded":
|
|
3053
|
-
finishOpenCodeTurn(
|
|
3026
|
+
finishOpenCodeTurn(context, { state: "completed" });
|
|
3054
3027
|
return true;
|
|
3055
3028
|
case "session.execution.failed": {
|
|
3056
3029
|
const errorMessage = openCodeFailureMessage(event.data["error"]);
|
|
3057
3030
|
const failureCategory = openCodeFailureCategory(event.data["error"]);
|
|
3058
|
-
finishOpenCodeTurn(
|
|
3059
|
-
emitEvent(
|
|
3031
|
+
finishOpenCodeTurn(context, { state: "failed", errorMessage, failureCategory });
|
|
3032
|
+
emitEvent(context, "runtime.error", {
|
|
3060
3033
|
message: errorMessage,
|
|
3061
3034
|
class: "provider_error",
|
|
3062
3035
|
...failureCategoryField(failureCategory)
|
|
@@ -3066,7 +3039,7 @@ function finishOpenCodeTurnEvent(events, context, event) {
|
|
|
3066
3039
|
case "session.execution.interrupted": {
|
|
3067
3040
|
const value = event.data["reason"];
|
|
3068
3041
|
const reason = typeof value === "string" ? value : void 0;
|
|
3069
|
-
finishOpenCodeTurn(
|
|
3042
|
+
finishOpenCodeTurn(context, {
|
|
3070
3043
|
state: "interrupted",
|
|
3071
3044
|
reason: reason === void 0 || reason === "user" ? "Interrupted by user." : reason === "shutdown" && context.abortedByDecline ? "Declined by user." : `Interrupted (${reason}).`
|
|
3072
3045
|
});
|
|
@@ -3076,18 +3049,18 @@ function finishOpenCodeTurnEvent(events, context, event) {
|
|
|
3076
3049
|
return false;
|
|
3077
3050
|
}
|
|
3078
3051
|
}
|
|
3079
|
-
function finishOpenCodeTurnOnExit(
|
|
3052
|
+
function finishOpenCodeTurnOnExit(context, cause) {
|
|
3080
3053
|
const message = cause instanceof Error && cause.message.trim().length > 0 ? cause.message.trim() : "OpenCode server exited unexpectedly.";
|
|
3081
3054
|
const failureCategory = cause instanceof OpenCodeApiError && cause.kind !== "transport" ? void 0 : "transport";
|
|
3082
3055
|
const turnId = context.session.activeTurnId;
|
|
3083
3056
|
if (turnId) {
|
|
3084
|
-
emitEvent(
|
|
3057
|
+
emitEvent(context, "runtime.error", {
|
|
3085
3058
|
message,
|
|
3086
3059
|
class: "transport_error",
|
|
3087
3060
|
...failureCategoryField(failureCategory)
|
|
3088
3061
|
});
|
|
3089
3062
|
}
|
|
3090
|
-
finishOpenCodeTurn(
|
|
3063
|
+
finishOpenCodeTurn(context, {
|
|
3091
3064
|
state: "failed",
|
|
3092
3065
|
errorMessage: message,
|
|
3093
3066
|
failureCategory,
|
|
@@ -3109,9 +3082,9 @@ var streamItemId = (data, kind) => {
|
|
|
3109
3082
|
}
|
|
3110
3083
|
return `${messageId}:${kind}:${ordinal}`;
|
|
3111
3084
|
};
|
|
3112
|
-
function settleSession(
|
|
3085
|
+
function settleSession(context) {
|
|
3113
3086
|
context.session = { ...context.session, status: "ready", updatedAt: nowIso() };
|
|
3114
|
-
emitEvent(
|
|
3087
|
+
emitEvent(context, "session.state.changed", { state: "ready" });
|
|
3115
3088
|
}
|
|
3116
3089
|
function remainderOf(context, itemId, full) {
|
|
3117
3090
|
const sent = context.assistantTextByPartId.get(itemId) ?? "";
|
|
@@ -3119,7 +3092,7 @@ function remainderOf(context, itemId, full) {
|
|
|
3119
3092
|
if (full.startsWith(sent)) return full.slice(sent.length);
|
|
3120
3093
|
return "";
|
|
3121
3094
|
}
|
|
3122
|
-
function appendDelta(
|
|
3095
|
+
function appendDelta(context, data, kind) {
|
|
3123
3096
|
const itemId = streamItemId(data, kind);
|
|
3124
3097
|
const delta = text3(data, "delta");
|
|
3125
3098
|
if (itemId === void 0 || delta === void 0 || delta.length === 0) return;
|
|
@@ -3128,21 +3101,19 @@ function appendDelta(events, context, data, kind) {
|
|
|
3128
3101
|
(context.assistantTextByPartId.get(itemId) ?? "") + delta
|
|
3129
3102
|
);
|
|
3130
3103
|
emitEvent(
|
|
3131
|
-
events,
|
|
3132
3104
|
context,
|
|
3133
3105
|
"content.delta",
|
|
3134
3106
|
{ streamKind: kind === "text" ? "assistant_text" : "reasoning_text", delta },
|
|
3135
3107
|
{ itemId }
|
|
3136
3108
|
);
|
|
3137
3109
|
}
|
|
3138
|
-
function endStream(
|
|
3110
|
+
function endStream(context, data, kind) {
|
|
3139
3111
|
const itemId = streamItemId(data, kind);
|
|
3140
3112
|
const full = text3(data, "text");
|
|
3141
3113
|
if (itemId === void 0 || full === void 0) return;
|
|
3142
3114
|
const delta = remainderOf(context, itemId, full);
|
|
3143
3115
|
if (delta.length > 0) {
|
|
3144
3116
|
emitEvent(
|
|
3145
|
-
events,
|
|
3146
3117
|
context,
|
|
3147
3118
|
"content.delta",
|
|
3148
3119
|
{ streamKind: kind === "text" ? "assistant_text" : "reasoning_text", delta },
|
|
@@ -3152,7 +3123,6 @@ function endStream(events, context, data, kind) {
|
|
|
3152
3123
|
if (kind !== "text" || full.length === 0 || context.completedTextParts.has(itemId)) return;
|
|
3153
3124
|
context.completedTextParts.add(itemId);
|
|
3154
3125
|
emitEvent(
|
|
3155
|
-
events,
|
|
3156
3126
|
context,
|
|
3157
3127
|
"item.completed",
|
|
3158
3128
|
{ itemType: "assistant_message", status: "completed", detail: full },
|
|
@@ -3160,9 +3130,9 @@ function endStream(events, context, data, kind) {
|
|
|
3160
3130
|
);
|
|
3161
3131
|
}
|
|
3162
3132
|
function handleOpenCodeTurnEvent(input) {
|
|
3163
|
-
const {
|
|
3164
|
-
if (finishOpenCodeTurnEvent(
|
|
3165
|
-
settleSession(
|
|
3133
|
+
const { context, event } = input;
|
|
3134
|
+
if (finishOpenCodeTurnEvent(context, event)) {
|
|
3135
|
+
settleSession(context);
|
|
3166
3136
|
return true;
|
|
3167
3137
|
}
|
|
3168
3138
|
const data = event.data;
|
|
@@ -3170,7 +3140,7 @@ function handleOpenCodeTurnEvent(input) {
|
|
|
3170
3140
|
case "session.execution.started": {
|
|
3171
3141
|
if (context.session.status !== "running") {
|
|
3172
3142
|
context.session = { ...context.session, status: "running", updatedAt: nowIso() };
|
|
3173
|
-
emitEvent(
|
|
3143
|
+
emitEvent(context, "session.state.changed", { state: "running" });
|
|
3174
3144
|
}
|
|
3175
3145
|
return true;
|
|
3176
3146
|
}
|
|
@@ -3179,7 +3149,7 @@ function handleOpenCodeTurnEvent(input) {
|
|
|
3179
3149
|
const messageId = text3(data, "assistantMessageID");
|
|
3180
3150
|
const model = data["model"];
|
|
3181
3151
|
const stepModel = decodeOpenCodeModelRef(model);
|
|
3182
|
-
if (stepModel !== void 0) reportOpenCodeModel(
|
|
3152
|
+
if (stepModel !== void 0) reportOpenCodeModel(context, stepModel);
|
|
3183
3153
|
if (messageId === void 0 || !isRecord2(model)) return true;
|
|
3184
3154
|
const providerId = model["providerID"];
|
|
3185
3155
|
const modelId = model["id"];
|
|
@@ -3204,21 +3174,21 @@ function handleOpenCodeTurnEvent(input) {
|
|
|
3204
3174
|
case "session.reasoning.started":
|
|
3205
3175
|
return true;
|
|
3206
3176
|
case "session.text.delta":
|
|
3207
|
-
appendDelta(
|
|
3177
|
+
appendDelta(context, data, "text");
|
|
3208
3178
|
return true;
|
|
3209
3179
|
case "session.reasoning.delta":
|
|
3210
|
-
appendDelta(
|
|
3180
|
+
appendDelta(context, data, "reasoning");
|
|
3211
3181
|
return true;
|
|
3212
3182
|
case "session.text.ended":
|
|
3213
|
-
endStream(
|
|
3183
|
+
endStream(context, data, "text");
|
|
3214
3184
|
return true;
|
|
3215
3185
|
case "session.reasoning.ended":
|
|
3216
|
-
endStream(
|
|
3186
|
+
endStream(context, data, "reasoning");
|
|
3217
3187
|
return true;
|
|
3218
3188
|
case "session.renamed": {
|
|
3219
3189
|
const title = text3(data, "title")?.trim() ?? "";
|
|
3220
3190
|
if (title.length > 0 && !isOpenCodeDefaultTitle(title)) {
|
|
3221
|
-
emitEvent(
|
|
3191
|
+
emitEvent(context, "thread.metadata.updated", { name: title });
|
|
3222
3192
|
}
|
|
3223
3193
|
return true;
|
|
3224
3194
|
}
|
|
@@ -3228,13 +3198,12 @@ function handleOpenCodeTurnEvent(input) {
|
|
|
3228
3198
|
}
|
|
3229
3199
|
|
|
3230
3200
|
// src/sessionLifecycle.ts
|
|
3231
|
-
function emitUnexpectedExit(
|
|
3201
|
+
function emitUnexpectedExit(context, cause) {
|
|
3232
3202
|
if (context.stopped) return;
|
|
3233
3203
|
context.stopped = true;
|
|
3234
|
-
const { message, failureCategory, turnId } = finishOpenCodeTurnOnExit(
|
|
3235
|
-
markSubagentsUnknown(
|
|
3204
|
+
const { message, failureCategory, turnId } = finishOpenCodeTurnOnExit(context, cause);
|
|
3205
|
+
markSubagentsUnknown(context);
|
|
3236
3206
|
emitEvent(
|
|
3237
|
-
events,
|
|
3238
3207
|
context,
|
|
3239
3208
|
"session.exited",
|
|
3240
3209
|
{ reason: message, recoverable: false, exitKind: "error" },
|
|
@@ -3258,7 +3227,7 @@ async function interruptForStop(context) {
|
|
|
3258
3227
|
}
|
|
3259
3228
|
}
|
|
3260
3229
|
function createOpenCodeSessionLifecycle(input) {
|
|
3261
|
-
const {
|
|
3230
|
+
const { context, log, options } = input;
|
|
3262
3231
|
const pending = /* @__PURE__ */ new Set();
|
|
3263
3232
|
const stopStarted = Promise.withResolvers();
|
|
3264
3233
|
let stopping = false;
|
|
@@ -3290,7 +3259,7 @@ function createOpenCodeSessionLifecycle(input) {
|
|
|
3290
3259
|
log.warn("OpenCode session tail handling timed out; closing the event boundary.");
|
|
3291
3260
|
for (const registered of pending) {
|
|
3292
3261
|
if (registered.event.sessionId === context.sessionId && registered.turnId !== void 0 && registered.turnId === context.session.activeTurnId) {
|
|
3293
|
-
finishOpenCodeTurnEvent(
|
|
3262
|
+
finishOpenCodeTurnEvent(context, registered.event);
|
|
3294
3263
|
}
|
|
3295
3264
|
}
|
|
3296
3265
|
}
|
|
@@ -3303,9 +3272,9 @@ function createOpenCodeSessionLifecycle(input) {
|
|
|
3303
3272
|
if (sealed) return;
|
|
3304
3273
|
publishing = true;
|
|
3305
3274
|
if (cause !== void 0 && !stopping) {
|
|
3306
|
-
emitUnexpectedExit(
|
|
3275
|
+
emitUnexpectedExit(context, cause);
|
|
3307
3276
|
} else {
|
|
3308
|
-
finishOpenCodeTurn(
|
|
3277
|
+
finishOpenCodeTurn(context, {
|
|
3309
3278
|
state: "interrupted",
|
|
3310
3279
|
reason: "Interrupted by user.",
|
|
3311
3280
|
sessionEnds: true
|
|
@@ -3315,8 +3284,8 @@ function createOpenCodeSessionLifecycle(input) {
|
|
|
3315
3284
|
status: "stopped",
|
|
3316
3285
|
updatedAt: nowIso()
|
|
3317
3286
|
};
|
|
3318
|
-
markSubagentsUnknown(
|
|
3319
|
-
emitEvent(
|
|
3287
|
+
markSubagentsUnknown(context);
|
|
3288
|
+
emitEvent(context, "session.exited", {
|
|
3320
3289
|
reason: message,
|
|
3321
3290
|
recoverable: false,
|
|
3322
3291
|
exitKind: "graceful"
|
|
@@ -3426,7 +3395,7 @@ async function applyOpenCodeTurnSelection(input) {
|
|
|
3426
3395
|
}
|
|
3427
3396
|
|
|
3428
3397
|
// src/sessionEvents.ts
|
|
3429
|
-
async function handleOtherSessionEvent(
|
|
3398
|
+
async function handleOtherSessionEvent(context, event, discoveredChild) {
|
|
3430
3399
|
const sessionId = event.sessionId;
|
|
3431
3400
|
if (sessionId === void 0) return;
|
|
3432
3401
|
noteChildSequence(context, event);
|
|
@@ -3438,11 +3407,11 @@ async function handleOtherSessionEvent(events, context, event, discoveredChild)
|
|
|
3438
3407
|
return;
|
|
3439
3408
|
}
|
|
3440
3409
|
if (event.type.startsWith("session.execution.")) {
|
|
3441
|
-
endSubagentFromExecution(
|
|
3410
|
+
endSubagentFromExecution(context, sessionId, event);
|
|
3442
3411
|
return;
|
|
3443
3412
|
}
|
|
3444
3413
|
if (event.type === "permission.asked" || event.type === "permission.replied") {
|
|
3445
|
-
await handleOpenCodeToolEvent({
|
|
3414
|
+
await handleOpenCodeToolEvent({ context, event });
|
|
3446
3415
|
return;
|
|
3447
3416
|
}
|
|
3448
3417
|
if (event.type === "session.step.started") {
|
|
@@ -3478,22 +3447,20 @@ var abortableDelay = (ms, signal) => new Promise((resolve2) => {
|
|
|
3478
3447
|
}
|
|
3479
3448
|
signal.addEventListener("abort", done, { once: true });
|
|
3480
3449
|
});
|
|
3481
|
-
async function recoverAfterReconnect(
|
|
3482
|
-
markSubagentsUnknown(
|
|
3450
|
+
async function recoverAfterReconnect(context, log) {
|
|
3451
|
+
markSubagentsUnknown(context);
|
|
3483
3452
|
const ours = (sessionId) => typeof sessionId === "string" && (sessionId === context.sessionId || context.childSessionIds.has(sessionId));
|
|
3484
3453
|
try {
|
|
3485
3454
|
for (const request of await context.api.pendingPermissions()) {
|
|
3486
3455
|
const sessionId = request["sessionID"];
|
|
3487
3456
|
if (!ours(sessionId)) continue;
|
|
3488
3457
|
await handleOpenCodeToolEvent({
|
|
3489
|
-
events,
|
|
3490
3458
|
context,
|
|
3491
3459
|
event: { type: "permission.asked", sessionId, data: request }
|
|
3492
3460
|
});
|
|
3493
3461
|
}
|
|
3494
3462
|
for (const form of await context.api.pendingForms()) {
|
|
3495
3463
|
handleOpenCodeFormEvent({
|
|
3496
|
-
events,
|
|
3497
3464
|
context,
|
|
3498
3465
|
event: { type: "form.created", sessionId: void 0, data: { form } }
|
|
3499
3466
|
});
|
|
@@ -3505,7 +3472,6 @@ async function recoverAfterReconnect(events, context, log) {
|
|
|
3505
3472
|
const outcome = current.settledOutcome;
|
|
3506
3473
|
if (outcome === void 0) return;
|
|
3507
3474
|
handleOpenCodeTurnEvent({
|
|
3508
|
-
events,
|
|
3509
3475
|
context,
|
|
3510
3476
|
event: {
|
|
3511
3477
|
type: outcome === "succeeded" ? "session.execution.succeeded" : outcome === "interrupted" ? "session.execution.interrupted" : "session.execution.failed",
|
|
@@ -3523,7 +3489,7 @@ async function recoverAfterReconnect(events, context, log) {
|
|
|
3523
3489
|
}
|
|
3524
3490
|
}
|
|
3525
3491
|
function startEventPump(input) {
|
|
3526
|
-
const {
|
|
3492
|
+
const { context, log } = input;
|
|
3527
3493
|
const lifecycle = createOpenCodeSessionLifecycle(input);
|
|
3528
3494
|
context.lifecycle = lifecycle;
|
|
3529
3495
|
const onSkipped = (reason) => {
|
|
@@ -3575,24 +3541,24 @@ function startEventPump(input) {
|
|
|
3575
3541
|
const recover = reconnecting;
|
|
3576
3542
|
reconnecting = false;
|
|
3577
3543
|
await lifecycle.consume(event, async () => {
|
|
3578
|
-
if (recover) await recoverAfterReconnect(
|
|
3544
|
+
if (recover) await recoverAfterReconnect(context, log);
|
|
3579
3545
|
if (lifecycle.sealed) return;
|
|
3580
3546
|
if (event.sessionId !== void 0 && event.sessionId !== context.sessionId) {
|
|
3581
|
-
await handleOtherSessionEvent(
|
|
3547
|
+
await handleOtherSessionEvent(context, event, discoveredChild);
|
|
3582
3548
|
return;
|
|
3583
3549
|
}
|
|
3584
3550
|
if (event.sessionId === context.sessionId) noteParentSequence(context, event);
|
|
3585
|
-
if (handleOpenCodeTurnEvent({
|
|
3551
|
+
if (handleOpenCodeTurnEvent({ context, event })) return;
|
|
3586
3552
|
if (noteOpenCodeSelectionEvent(context, event)) {
|
|
3587
3553
|
if (event.type === "session.model.selected" && context.selectedModel !== void 0) {
|
|
3588
|
-
reportOpenCodeModel(
|
|
3554
|
+
reportOpenCodeModel(context, context.selectedModel);
|
|
3589
3555
|
}
|
|
3590
3556
|
return;
|
|
3591
3557
|
}
|
|
3592
|
-
if (handleOpenCodeCompactionEvent({
|
|
3593
|
-
if (handleOpenCodeFormEvent({
|
|
3594
|
-
if (reportSubagentInbox(
|
|
3595
|
-
await handleOpenCodeToolEvent({
|
|
3558
|
+
if (handleOpenCodeCompactionEvent({ context, event })) return;
|
|
3559
|
+
if (handleOpenCodeFormEvent({ context, event })) return;
|
|
3560
|
+
if (reportSubagentInbox(context, event)) return;
|
|
3561
|
+
await handleOpenCodeToolEvent({ context, event });
|
|
3596
3562
|
});
|
|
3597
3563
|
}
|
|
3598
3564
|
return new Error("OpenCode event stream ended.");
|
|
@@ -4417,7 +4383,6 @@ function requestedModelOf(modelSelection) {
|
|
|
4417
4383
|
}
|
|
4418
4384
|
function createOpenCodeInstance(config, host, attachments, lifecycleOptions) {
|
|
4419
4385
|
const recipe = parseOpenCodeInstanceConfig(config);
|
|
4420
|
-
const events = new EventBus();
|
|
4421
4386
|
const sessions = /* @__PURE__ */ new Map();
|
|
4422
4387
|
const pendingServers = /* @__PURE__ */ new Map();
|
|
4423
4388
|
const rememberPendingServer = (threadId, server) => {
|
|
@@ -4593,6 +4558,7 @@ function createOpenCodeInstance(config, host, attachments, lifecycleOptions) {
|
|
|
4593
4558
|
} : {}
|
|
4594
4559
|
};
|
|
4595
4560
|
const context = {
|
|
4561
|
+
events: input.events,
|
|
4596
4562
|
threadId: input.threadId,
|
|
4597
4563
|
session,
|
|
4598
4564
|
sessionId,
|
|
@@ -4624,7 +4590,6 @@ function createOpenCodeInstance(config, host, attachments, lifecycleOptions) {
|
|
|
4624
4590
|
...selection
|
|
4625
4591
|
};
|
|
4626
4592
|
await startEventPump({
|
|
4627
|
-
events,
|
|
4628
4593
|
context,
|
|
4629
4594
|
log: host.log,
|
|
4630
4595
|
...lifecycleOptions ? { options: lifecycleOptions } : {}
|
|
@@ -4638,13 +4603,12 @@ function createOpenCodeInstance(config, host, attachments, lifecycleOptions) {
|
|
|
4638
4603
|
}
|
|
4639
4604
|
sessions.set(input.threadId, context);
|
|
4640
4605
|
emitEvent(
|
|
4641
|
-
events,
|
|
4642
4606
|
context,
|
|
4643
4607
|
"session.started",
|
|
4644
4608
|
input.resumeCursor !== void 0 ? { resume: input.resumeCursor } : {}
|
|
4645
4609
|
);
|
|
4646
|
-
emitEvent(
|
|
4647
|
-
emitEvent(
|
|
4610
|
+
emitEvent(context, "thread.started", { providerThreadId: sessionId });
|
|
4611
|
+
emitEvent(context, "session.state.changed", { state: "ready" });
|
|
4648
4612
|
if (resumedInPlace) await registerChildSessions(context, host.log);
|
|
4649
4613
|
return sessionFromContext(context);
|
|
4650
4614
|
} catch (cause) {
|
|
@@ -4693,7 +4657,6 @@ function createOpenCodeInstance(config, host, attachments, lifecycleOptions) {
|
|
|
4693
4657
|
updatedAt: nowIso()
|
|
4694
4658
|
};
|
|
4695
4659
|
emitEvent(
|
|
4696
|
-
events,
|
|
4697
4660
|
context,
|
|
4698
4661
|
"turn.started",
|
|
4699
4662
|
requestedModel ? {
|
|
@@ -4703,7 +4666,7 @@ function createOpenCodeInstance(config, host, attachments, lifecycleOptions) {
|
|
|
4703
4666
|
} : {},
|
|
4704
4667
|
{ turnId }
|
|
4705
4668
|
);
|
|
4706
|
-
emitEvent(
|
|
4669
|
+
emitEvent(context, "session.state.changed", { state: "running" });
|
|
4707
4670
|
}
|
|
4708
4671
|
try {
|
|
4709
4672
|
if (!steering) {
|
|
@@ -4728,7 +4691,6 @@ function createOpenCodeInstance(config, host, attachments, lifecycleOptions) {
|
|
|
4728
4691
|
updatedAt: nowIso()
|
|
4729
4692
|
};
|
|
4730
4693
|
emitEvent(
|
|
4731
|
-
events,
|
|
4732
4694
|
context,
|
|
4733
4695
|
"turn.aborted",
|
|
4734
4696
|
{ reason: openCodeRuntimeErrorDetail(cause) },
|
|
@@ -4765,13 +4727,12 @@ function createOpenCodeInstance(config, host, attachments, lifecycleOptions) {
|
|
|
4765
4727
|
)
|
|
4766
4728
|
);
|
|
4767
4729
|
stopSubagents(
|
|
4768
|
-
events,
|
|
4769
4730
|
context,
|
|
4770
4731
|
interrupted.filter((child) => child !== void 0)
|
|
4771
4732
|
);
|
|
4772
4733
|
const active = context.session.activeTurnId;
|
|
4773
4734
|
if (active && (turnId === void 0 || turnId === active)) {
|
|
4774
|
-
finishOpenCodeTurn(
|
|
4735
|
+
finishOpenCodeTurn(context, {
|
|
4775
4736
|
state: "interrupted",
|
|
4776
4737
|
reason: "Interrupted by user."
|
|
4777
4738
|
});
|
|
@@ -4828,7 +4789,6 @@ function createOpenCodeInstance(config, host, attachments, lifecycleOptions) {
|
|
|
4828
4789
|
}));
|
|
4829
4790
|
return { threadId, turns };
|
|
4830
4791
|
},
|
|
4831
|
-
events,
|
|
4832
4792
|
// 额度:OpenCode Go 套餐,读这个实例自己的 key(连外部服务器的实例不读本机);token 仍走编排账本。
|
|
4833
4793
|
usage: createOpenCodeUsage({
|
|
4834
4794
|
homePath: recipe.homePath,
|
|
@@ -4854,7 +4814,6 @@ function createOpenCodeInstance(config, host, attachments, lifecycleOptions) {
|
|
|
4854
4814
|
throw new AggregateError(failures, "OpenCode instance disposal failed.", {
|
|
4855
4815
|
cause: failures[0]
|
|
4856
4816
|
});
|
|
4857
|
-
events.close();
|
|
4858
4817
|
})();
|
|
4859
4818
|
disposal = work;
|
|
4860
4819
|
try {
|
|
@@ -4887,7 +4846,7 @@ var executors = [
|
|
|
4887
4846
|
];
|
|
4888
4847
|
var index_default = definePlugin({
|
|
4889
4848
|
id: "agent-opencode",
|
|
4890
|
-
apiVersion:
|
|
4849
|
+
apiVersion: PLUGIN_API_VERSION,
|
|
4891
4850
|
executors,
|
|
4892
4851
|
activate(ctx) {
|
|
4893
4852
|
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.3982",
|
|
4
4
|
"description": "OpenCode executor: OpenCode CLI (2.x) over its HTTP API.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"ycodium",
|
|
@@ -21,10 +21,10 @@
|
|
|
21
21
|
"@types/node": "24.12.4",
|
|
22
22
|
"esbuild": "0.28.1",
|
|
23
23
|
"vite-plus": "0.2.2",
|
|
24
|
-
"@ycodium-ai/plugin-api": "0.2.
|
|
24
|
+
"@ycodium-ai/plugin-api": "0.2.3982",
|
|
25
25
|
"ycodium-executor-testing": "0.0.0",
|
|
26
|
-
"ycodium-
|
|
27
|
-
"ycodium-
|
|
26
|
+
"ycodium-sse-stream": "0.0.0",
|
|
27
|
+
"ycodium-fetch-ports": "0.0.0"
|
|
28
28
|
},
|
|
29
29
|
"ycodium": {
|
|
30
30
|
"plugin": "./dist/index.js",
|