@ycodium-ai/agent-opencode 0.2.3122 → 0.2.3158
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 +274 -181
- package/package.json +4 -4
package/dist/index.js
CHANGED
|
@@ -380,6 +380,7 @@ function withLastError(session, lastError, failureCategory) {
|
|
|
380
380
|
return { ...rest, lastError, ...failureCategoryField(failureCategory) };
|
|
381
381
|
}
|
|
382
382
|
function emitEvent(events, context, type2, payload, extra) {
|
|
383
|
+
if (context.lifecycle?.canEmit === false) return;
|
|
383
384
|
const turnId = extra?.turnId ?? context.session.activeTurnId;
|
|
384
385
|
const event = {
|
|
385
386
|
eventId: randomUUID(),
|
|
@@ -2507,24 +2508,8 @@ async function handleOpenCodeToolEvent(input) {
|
|
|
2507
2508
|
}
|
|
2508
2509
|
}
|
|
2509
2510
|
|
|
2510
|
-
// src/
|
|
2511
|
-
|
|
2512
|
-
const value = data[key];
|
|
2513
|
-
return typeof value === "string" ? value : void 0;
|
|
2514
|
-
};
|
|
2515
|
-
var streamItemId = (data, kind) => {
|
|
2516
|
-
const messageId = text3(data, "assistantMessageID");
|
|
2517
|
-
const ordinal = data["ordinal"];
|
|
2518
|
-
if (messageId === void 0 || messageId.length === 0 || typeof ordinal !== "number") {
|
|
2519
|
-
return void 0;
|
|
2520
|
-
}
|
|
2521
|
-
return `${messageId}:${kind}:${ordinal}`;
|
|
2522
|
-
};
|
|
2523
|
-
function settleSession(events, context) {
|
|
2524
|
-
context.session = { ...context.session, status: "ready", updatedAt: nowIso() };
|
|
2525
|
-
emitEvent(events, context, "session.state.changed", { state: "ready" });
|
|
2526
|
-
}
|
|
2527
|
-
function finishTurn(events, context, outcome) {
|
|
2511
|
+
// src/turnCompletion.ts
|
|
2512
|
+
function finishOpenCodeTurn(events, context, outcome) {
|
|
2528
2513
|
const turnId = context.session.activeTurnId;
|
|
2529
2514
|
const usage = takeTurnUsage(context.usageByMessageId, context.sessionId);
|
|
2530
2515
|
context.abortedByDecline = false;
|
|
@@ -2535,11 +2520,7 @@ function finishTurn(events, context, outcome) {
|
|
|
2535
2520
|
if (turnId === void 0) return;
|
|
2536
2521
|
if (outcome.state === "interrupted") {
|
|
2537
2522
|
emitEvent(events, context, "turn.aborted", { reason: outcome.reason }, { turnId });
|
|
2538
|
-
context.session = {
|
|
2539
|
-
...withoutActiveTurn(context.session),
|
|
2540
|
-
status: "ready",
|
|
2541
|
-
updatedAt: nowIso()
|
|
2542
|
-
};
|
|
2523
|
+
context.session = { ...withoutActiveTurn(context.session), updatedAt: nowIso() };
|
|
2543
2524
|
return;
|
|
2544
2525
|
}
|
|
2545
2526
|
if (outcome.state === "completed") {
|
|
@@ -2550,15 +2531,10 @@ function finishTurn(events, context, outcome) {
|
|
|
2550
2531
|
{ state: "completed", ...usage ? { tokenUsage: usage } : {} },
|
|
2551
2532
|
{ turnId }
|
|
2552
2533
|
);
|
|
2553
|
-
context.session = {
|
|
2554
|
-
...withoutActiveTurn(context.session),
|
|
2555
|
-
status: "ready",
|
|
2556
|
-
updatedAt: nowIso()
|
|
2557
|
-
};
|
|
2534
|
+
context.session = { ...withoutActiveTurn(context.session), updatedAt: nowIso() };
|
|
2558
2535
|
return;
|
|
2559
2536
|
}
|
|
2560
|
-
const errorMessage =
|
|
2561
|
-
const failureCategory = openCodeFailureCategory(outcome.error);
|
|
2537
|
+
const { errorMessage, failureCategory } = outcome;
|
|
2562
2538
|
emitEvent(
|
|
2563
2539
|
events,
|
|
2564
2540
|
context,
|
|
@@ -2573,14 +2549,69 @@ function finishTurn(events, context, outcome) {
|
|
|
2573
2549
|
);
|
|
2574
2550
|
context.session = {
|
|
2575
2551
|
...withLastError(withoutActiveTurn(context.session), errorMessage, failureCategory),
|
|
2576
|
-
status: "error",
|
|
2577
2552
|
updatedAt: nowIso()
|
|
2578
2553
|
};
|
|
2579
|
-
|
|
2580
|
-
|
|
2581
|
-
|
|
2582
|
-
|
|
2583
|
-
|
|
2554
|
+
}
|
|
2555
|
+
function finishOpenCodeTurnEvent(events, context, event) {
|
|
2556
|
+
switch (event.type) {
|
|
2557
|
+
case "session.execution.succeeded":
|
|
2558
|
+
finishOpenCodeTurn(events, context, { state: "completed" });
|
|
2559
|
+
return true;
|
|
2560
|
+
case "session.execution.failed": {
|
|
2561
|
+
const errorMessage = openCodeFailureMessage(event.data["error"]);
|
|
2562
|
+
const failureCategory = openCodeFailureCategory(event.data["error"]);
|
|
2563
|
+
finishOpenCodeTurn(events, context, { state: "failed", errorMessage, failureCategory });
|
|
2564
|
+
emitEvent(events, context, "runtime.error", {
|
|
2565
|
+
message: errorMessage,
|
|
2566
|
+
class: "provider_error",
|
|
2567
|
+
...failureCategoryField(failureCategory)
|
|
2568
|
+
});
|
|
2569
|
+
return true;
|
|
2570
|
+
}
|
|
2571
|
+
case "session.execution.interrupted": {
|
|
2572
|
+
const value = event.data["reason"];
|
|
2573
|
+
const reason = typeof value === "string" ? value : void 0;
|
|
2574
|
+
finishOpenCodeTurn(events, context, {
|
|
2575
|
+
state: "interrupted",
|
|
2576
|
+
reason: reason === void 0 || reason === "user" ? "Interrupted by user." : reason === "shutdown" && context.abortedByDecline ? "Declined by user." : `Interrupted (${reason}).`
|
|
2577
|
+
});
|
|
2578
|
+
return true;
|
|
2579
|
+
}
|
|
2580
|
+
default:
|
|
2581
|
+
return false;
|
|
2582
|
+
}
|
|
2583
|
+
}
|
|
2584
|
+
function finishOpenCodeTurnOnExit(events, context, cause) {
|
|
2585
|
+
const message = cause instanceof Error && cause.message.trim().length > 0 ? cause.message.trim() : "OpenCode server exited unexpectedly.";
|
|
2586
|
+
const failureCategory = cause instanceof OpenCodeApiError && cause.kind !== "transport" ? void 0 : "transport";
|
|
2587
|
+
const turnId = context.session.activeTurnId;
|
|
2588
|
+
if (turnId) {
|
|
2589
|
+
emitEvent(events, context, "runtime.error", {
|
|
2590
|
+
message,
|
|
2591
|
+
class: "transport_error",
|
|
2592
|
+
...failureCategoryField(failureCategory)
|
|
2593
|
+
});
|
|
2594
|
+
}
|
|
2595
|
+
finishOpenCodeTurn(events, context, { state: "failed", errorMessage: message, failureCategory });
|
|
2596
|
+
return { message, failureCategory, turnId };
|
|
2597
|
+
}
|
|
2598
|
+
|
|
2599
|
+
// src/turnEvents.ts
|
|
2600
|
+
var text3 = (data, key) => {
|
|
2601
|
+
const value = data[key];
|
|
2602
|
+
return typeof value === "string" ? value : void 0;
|
|
2603
|
+
};
|
|
2604
|
+
var streamItemId = (data, kind) => {
|
|
2605
|
+
const messageId = text3(data, "assistantMessageID");
|
|
2606
|
+
const ordinal = data["ordinal"];
|
|
2607
|
+
if (messageId === void 0 || messageId.length === 0 || typeof ordinal !== "number") {
|
|
2608
|
+
return void 0;
|
|
2609
|
+
}
|
|
2610
|
+
return `${messageId}:${kind}:${ordinal}`;
|
|
2611
|
+
};
|
|
2612
|
+
function settleSession(events, context) {
|
|
2613
|
+
context.session = { ...context.session, status: "ready", updatedAt: nowIso() };
|
|
2614
|
+
emitEvent(events, context, "session.state.changed", { state: "ready" });
|
|
2584
2615
|
}
|
|
2585
2616
|
function remainderOf(context, itemId, full) {
|
|
2586
2617
|
const sent = context.assistantTextByPartId.get(itemId) ?? "";
|
|
@@ -2630,6 +2661,10 @@ function endStream(events, context, data, kind) {
|
|
|
2630
2661
|
}
|
|
2631
2662
|
function handleOpenCodeTurnEvent(input) {
|
|
2632
2663
|
const { events, context, event } = input;
|
|
2664
|
+
if (finishOpenCodeTurnEvent(events, context, event)) {
|
|
2665
|
+
settleSession(events, context);
|
|
2666
|
+
return true;
|
|
2667
|
+
}
|
|
2633
2668
|
const data = event.data;
|
|
2634
2669
|
switch (event.type) {
|
|
2635
2670
|
case "session.execution.started": {
|
|
@@ -2639,33 +2674,6 @@ function handleOpenCodeTurnEvent(input) {
|
|
|
2639
2674
|
}
|
|
2640
2675
|
return true;
|
|
2641
2676
|
}
|
|
2642
|
-
case "session.execution.succeeded": {
|
|
2643
|
-
finishTurn(events, context, { state: "completed" });
|
|
2644
|
-
settleSession(events, context);
|
|
2645
|
-
return true;
|
|
2646
|
-
}
|
|
2647
|
-
case "session.execution.failed": {
|
|
2648
|
-
const hadTurn = context.session.activeTurnId !== void 0;
|
|
2649
|
-
finishTurn(events, context, { state: "failed", error: data["error"] });
|
|
2650
|
-
if (!hadTurn) {
|
|
2651
|
-
emitEvent(events, context, "runtime.error", {
|
|
2652
|
-
message: openCodeFailureMessage(data["error"]),
|
|
2653
|
-
class: "provider_error",
|
|
2654
|
-
...failureCategoryField(openCodeFailureCategory(data["error"]))
|
|
2655
|
-
});
|
|
2656
|
-
}
|
|
2657
|
-
settleSession(events, context);
|
|
2658
|
-
return true;
|
|
2659
|
-
}
|
|
2660
|
-
case "session.execution.interrupted": {
|
|
2661
|
-
const reason = text3(data, "reason");
|
|
2662
|
-
finishTurn(events, context, {
|
|
2663
|
-
state: "interrupted",
|
|
2664
|
-
reason: reason === void 0 || reason === "user" ? "Interrupted by user." : reason === "shutdown" && context.abortedByDecline ? "Declined by user." : `Interrupted (${reason}).`
|
|
2665
|
-
});
|
|
2666
|
-
settleSession(events, context);
|
|
2667
|
-
return true;
|
|
2668
|
-
}
|
|
2669
2677
|
case "session.step.started": {
|
|
2670
2678
|
context.abortedByDecline = false;
|
|
2671
2679
|
const messageId = text3(data, "assistantMessageID");
|
|
@@ -2717,6 +2725,160 @@ function handleOpenCodeTurnEvent(input) {
|
|
|
2717
2725
|
}
|
|
2718
2726
|
}
|
|
2719
2727
|
|
|
2728
|
+
// src/sessionLifecycle.ts
|
|
2729
|
+
function emitUnexpectedExit(events, context, cause) {
|
|
2730
|
+
if (context.stopped) return;
|
|
2731
|
+
context.stopped = true;
|
|
2732
|
+
const { message, failureCategory, turnId } = finishOpenCodeTurnOnExit(events, context, cause);
|
|
2733
|
+
emitEvent(
|
|
2734
|
+
events,
|
|
2735
|
+
context,
|
|
2736
|
+
"session.exited",
|
|
2737
|
+
{ reason: message, recoverable: false, exitKind: "error" },
|
|
2738
|
+
turnId === void 0 ? void 0 : { turnId }
|
|
2739
|
+
);
|
|
2740
|
+
context.session = {
|
|
2741
|
+
...withLastError(context.session, message, failureCategory),
|
|
2742
|
+
status: "error",
|
|
2743
|
+
updatedAt: nowIso()
|
|
2744
|
+
};
|
|
2745
|
+
}
|
|
2746
|
+
async function interruptForStop(context) {
|
|
2747
|
+
if (context.session.activeTurnId === void 0) return;
|
|
2748
|
+
const abort = new AbortController();
|
|
2749
|
+
const timer = setTimeout(() => abort.abort(), 2e3);
|
|
2750
|
+
try {
|
|
2751
|
+
await context.api.interrupt(context.sessionId, abort.signal);
|
|
2752
|
+
} catch {
|
|
2753
|
+
} finally {
|
|
2754
|
+
clearTimeout(timer);
|
|
2755
|
+
}
|
|
2756
|
+
}
|
|
2757
|
+
function createOpenCodeSessionLifecycle(input) {
|
|
2758
|
+
const { events, context, log, options } = input;
|
|
2759
|
+
const pending = /* @__PURE__ */ new Set();
|
|
2760
|
+
const stopStarted = Promise.withResolvers();
|
|
2761
|
+
let stopping = false;
|
|
2762
|
+
let exiting = false;
|
|
2763
|
+
let sealed = false;
|
|
2764
|
+
let handlersClosed = false;
|
|
2765
|
+
let publishing = false;
|
|
2766
|
+
let closing;
|
|
2767
|
+
let cleanup;
|
|
2768
|
+
const drain = async () => {
|
|
2769
|
+
if (!stopping) await Promise.race([context.pump, stopStarted.promise]);
|
|
2770
|
+
await Promise.all([...pending].map(({ task }) => task));
|
|
2771
|
+
};
|
|
2772
|
+
const close = (cause, message = "Session stopped.") => {
|
|
2773
|
+
if (closing) return closing;
|
|
2774
|
+
const work = Promise.resolve().then(async () => {
|
|
2775
|
+
if (!sealed) {
|
|
2776
|
+
let timer;
|
|
2777
|
+
try {
|
|
2778
|
+
const timedOut = await Promise.race([
|
|
2779
|
+
drain().catch((error) => {
|
|
2780
|
+
log.warn(`OpenCode tail handler failed: ${String(error)}`);
|
|
2781
|
+
}).then(() => false),
|
|
2782
|
+
new Promise((resolve2) => {
|
|
2783
|
+
timer = setTimeout(() => resolve2(true), options?.tailTimeoutMs ?? 2e3);
|
|
2784
|
+
})
|
|
2785
|
+
]);
|
|
2786
|
+
if (timedOut) {
|
|
2787
|
+
log.warn("OpenCode session tail handling timed out; closing the event boundary.");
|
|
2788
|
+
for (const registered of pending) {
|
|
2789
|
+
if (registered.event.sessionId === context.sessionId && registered.turnId !== void 0 && registered.turnId === context.session.activeTurnId) {
|
|
2790
|
+
finishOpenCodeTurnEvent(events, context, registered.event);
|
|
2791
|
+
}
|
|
2792
|
+
}
|
|
2793
|
+
}
|
|
2794
|
+
} finally {
|
|
2795
|
+
clearTimeout(timer);
|
|
2796
|
+
handlersClosed = true;
|
|
2797
|
+
}
|
|
2798
|
+
}
|
|
2799
|
+
await cleanup;
|
|
2800
|
+
if (sealed) return;
|
|
2801
|
+
publishing = true;
|
|
2802
|
+
if (cause !== void 0 && !stopping) {
|
|
2803
|
+
emitUnexpectedExit(events, context, cause);
|
|
2804
|
+
} else {
|
|
2805
|
+
finishOpenCodeTurn(events, context, {
|
|
2806
|
+
state: "interrupted",
|
|
2807
|
+
reason: "Interrupted by user."
|
|
2808
|
+
});
|
|
2809
|
+
context.session = {
|
|
2810
|
+
...context.session,
|
|
2811
|
+
status: "stopped",
|
|
2812
|
+
updatedAt: nowIso()
|
|
2813
|
+
};
|
|
2814
|
+
emitEvent(events, context, "session.exited", {
|
|
2815
|
+
reason: message,
|
|
2816
|
+
recoverable: false,
|
|
2817
|
+
exitKind: "graceful"
|
|
2818
|
+
});
|
|
2819
|
+
}
|
|
2820
|
+
sealed = true;
|
|
2821
|
+
publishing = false;
|
|
2822
|
+
context.stopped = true;
|
|
2823
|
+
context.eventAbort.abort();
|
|
2824
|
+
});
|
|
2825
|
+
closing = work;
|
|
2826
|
+
void work.catch(() => {
|
|
2827
|
+
closing = void 0;
|
|
2828
|
+
cleanup = void 0;
|
|
2829
|
+
});
|
|
2830
|
+
return work;
|
|
2831
|
+
};
|
|
2832
|
+
return {
|
|
2833
|
+
get exiting() {
|
|
2834
|
+
return exiting;
|
|
2835
|
+
},
|
|
2836
|
+
get closing() {
|
|
2837
|
+
return closing !== void 0;
|
|
2838
|
+
},
|
|
2839
|
+
get sealed() {
|
|
2840
|
+
return sealed;
|
|
2841
|
+
},
|
|
2842
|
+
get canEmit() {
|
|
2843
|
+
return !sealed && (!handlersClosed || publishing);
|
|
2844
|
+
},
|
|
2845
|
+
consume(event, handle) {
|
|
2846
|
+
if (stopping || handlersClosed) return Promise.resolve();
|
|
2847
|
+
const turnId = context.session.activeTurnId;
|
|
2848
|
+
const task = Promise.resolve().then(async () => {
|
|
2849
|
+
await options?.beforeHandleEvent?.(event);
|
|
2850
|
+
if (handlersClosed) return;
|
|
2851
|
+
await handle();
|
|
2852
|
+
});
|
|
2853
|
+
const registered = { event, turnId, task };
|
|
2854
|
+
pending.add(registered);
|
|
2855
|
+
return task.finally(() => pending.delete(registered));
|
|
2856
|
+
},
|
|
2857
|
+
exit(cause, processExited = false) {
|
|
2858
|
+
if (processExited) exiting = true;
|
|
2859
|
+
void close(cause).catch((error) => {
|
|
2860
|
+
log.warn(`OpenCode exit handling failed: ${String(error)}`);
|
|
2861
|
+
});
|
|
2862
|
+
},
|
|
2863
|
+
stop(message) {
|
|
2864
|
+
stopping = true;
|
|
2865
|
+
context.stopped = true;
|
|
2866
|
+
stopStarted.resolve();
|
|
2867
|
+
context.eventAbort.abort();
|
|
2868
|
+
cleanup ??= (async () => {
|
|
2869
|
+
await interruptForStop(context);
|
|
2870
|
+
await closeOpenCodeServer(context.server);
|
|
2871
|
+
})();
|
|
2872
|
+
void cleanup.catch(() => void 0);
|
|
2873
|
+
const ownedCleanup = cleanup;
|
|
2874
|
+
return close(void 0, message).then(() => ownedCleanup).catch((cause) => {
|
|
2875
|
+
if (cleanup === ownedCleanup) cleanup = void 0;
|
|
2876
|
+
throw cause;
|
|
2877
|
+
});
|
|
2878
|
+
}
|
|
2879
|
+
};
|
|
2880
|
+
}
|
|
2881
|
+
|
|
2720
2882
|
// src/turnSelection.ts
|
|
2721
2883
|
var OPENCODE_PLAN_AGENT = "plan";
|
|
2722
2884
|
function openCodeSelectionOf(session) {
|
|
@@ -2760,60 +2922,13 @@ async function applyOpenCodeTurnSelection(input) {
|
|
|
2760
2922
|
}
|
|
2761
2923
|
|
|
2762
2924
|
// src/sessionEvents.ts
|
|
2763
|
-
function
|
|
2764
|
-
return cause instanceof OpenCodeApiError && cause.kind !== "transport" ? void 0 : "transport";
|
|
2765
|
-
}
|
|
2766
|
-
function emitUnexpectedExit(events, context, cause) {
|
|
2767
|
-
if (context.stopped) return;
|
|
2768
|
-
context.stopped = true;
|
|
2769
|
-
const message = cause instanceof Error && cause.message.trim().length > 0 ? cause.message.trim() : "OpenCode server exited unexpectedly.";
|
|
2770
|
-
const failureCategory = exitFailureCategory(cause);
|
|
2771
|
-
const category = failureCategoryField(failureCategory);
|
|
2772
|
-
const turnId = context.session.activeTurnId;
|
|
2773
|
-
if (turnId) {
|
|
2774
|
-
emitEvent(events, context, "runtime.error", {
|
|
2775
|
-
message,
|
|
2776
|
-
class: "transport_error",
|
|
2777
|
-
...category
|
|
2778
|
-
});
|
|
2779
|
-
}
|
|
2780
|
-
const usage = takeTurnUsage(context.usageByMessageId, context.sessionId);
|
|
2781
|
-
if (turnId) {
|
|
2782
|
-
emitEvent(
|
|
2783
|
-
events,
|
|
2784
|
-
context,
|
|
2785
|
-
"turn.completed",
|
|
2786
|
-
{
|
|
2787
|
-
state: "failed",
|
|
2788
|
-
...usage ? { tokenUsage: usage } : {},
|
|
2789
|
-
errorMessage: message,
|
|
2790
|
-
...category
|
|
2791
|
-
},
|
|
2792
|
-
{ turnId }
|
|
2793
|
-
);
|
|
2794
|
-
}
|
|
2795
|
-
emitEvent(events, context, "session.exited", {
|
|
2796
|
-
reason: message,
|
|
2797
|
-
recoverable: false,
|
|
2798
|
-
exitKind: "error"
|
|
2799
|
-
});
|
|
2800
|
-
context.session = {
|
|
2801
|
-
...withLastError(withoutActiveTurn(context.session), message, failureCategory),
|
|
2802
|
-
status: "error",
|
|
2803
|
-
updatedAt: nowIso()
|
|
2804
|
-
};
|
|
2805
|
-
}
|
|
2806
|
-
async function handleOtherSessionEvent(events, context, event) {
|
|
2925
|
+
async function handleOtherSessionEvent(events, context, event, discoveredChild) {
|
|
2807
2926
|
const sessionId = event.sessionId;
|
|
2808
2927
|
if (sessionId === void 0) return;
|
|
2809
2928
|
if (event.type === "session.created") {
|
|
2810
|
-
|
|
2811
|
-
if (typeof parentId === "string" && (parentId === context.sessionId || context.childSessionIds.has(parentId))) {
|
|
2812
|
-
context.childSessionIds.add(sessionId);
|
|
2813
|
-
}
|
|
2929
|
+
if (discoveredChild) context.childSessionIds.add(sessionId);
|
|
2814
2930
|
return;
|
|
2815
2931
|
}
|
|
2816
|
-
if (!context.childSessionIds.has(sessionId)) return;
|
|
2817
2932
|
if (event.type === "permission.asked" || event.type === "permission.replied") {
|
|
2818
2933
|
await handleOpenCodeToolEvent({ events, context, event });
|
|
2819
2934
|
return;
|
|
@@ -2871,6 +2986,7 @@ async function recoverAfterReconnect(events, context, log) {
|
|
|
2871
2986
|
});
|
|
2872
2987
|
}
|
|
2873
2988
|
const current = await context.api.getSession(context.sessionId);
|
|
2989
|
+
if (context.stopped || context.lifecycle?.sealed) return;
|
|
2874
2990
|
Object.assign(context, openCodeSelectionOf(current));
|
|
2875
2991
|
if (context.session.activeTurnId === void 0) return;
|
|
2876
2992
|
const outcome = current.settledOutcome;
|
|
@@ -2895,6 +3011,8 @@ async function recoverAfterReconnect(events, context, log) {
|
|
|
2895
3011
|
}
|
|
2896
3012
|
function startEventPump(input) {
|
|
2897
3013
|
const { events, context, log } = input;
|
|
3014
|
+
const lifecycle = createOpenCodeSessionLifecycle(input);
|
|
3015
|
+
context.lifecycle = lifecycle;
|
|
2898
3016
|
const onSkipped = (reason) => {
|
|
2899
3017
|
log.warn(`OpenCode event skipped: ${reason}`);
|
|
2900
3018
|
};
|
|
@@ -2912,7 +3030,7 @@ function startEventPump(input) {
|
|
|
2912
3030
|
}, OPENCODE_EVENT_READY_TIMEOUT_MS);
|
|
2913
3031
|
const fail = (cause) => {
|
|
2914
3032
|
if (connected) {
|
|
2915
|
-
|
|
3033
|
+
lifecycle.exit(cause);
|
|
2916
3034
|
return;
|
|
2917
3035
|
}
|
|
2918
3036
|
clearTimeout(readyTimer);
|
|
@@ -2934,18 +3052,28 @@ function startEventPump(input) {
|
|
|
2934
3052
|
if (first) {
|
|
2935
3053
|
first = false;
|
|
2936
3054
|
receivedOnThisConnection = true;
|
|
2937
|
-
if (reconnecting) await recoverAfterReconnect(events, context, log);
|
|
2938
3055
|
}
|
|
2939
3056
|
if (context.stopped) break;
|
|
2940
|
-
|
|
2941
|
-
|
|
3057
|
+
const sessionId = event.sessionId;
|
|
3058
|
+
const parentId = event.type === "session.created" ? event.data["parentID"] : void 0;
|
|
3059
|
+
const discoveredChild = typeof parentId === "string" && (parentId === context.sessionId || context.childSessionIds.has(parentId));
|
|
3060
|
+
if (sessionId !== void 0 && sessionId !== context.sessionId && !context.childSessionIds.has(sessionId) && !discoveredChild)
|
|
2942
3061
|
continue;
|
|
2943
|
-
|
|
2944
|
-
|
|
2945
|
-
|
|
2946
|
-
|
|
2947
|
-
|
|
2948
|
-
|
|
3062
|
+
const recover = reconnecting;
|
|
3063
|
+
reconnecting = false;
|
|
3064
|
+
await lifecycle.consume(event, async () => {
|
|
3065
|
+
if (recover) await recoverAfterReconnect(events, context, log);
|
|
3066
|
+
if (lifecycle.sealed) return;
|
|
3067
|
+
if (event.sessionId !== void 0 && event.sessionId !== context.sessionId) {
|
|
3068
|
+
await handleOtherSessionEvent(events, context, event, discoveredChild);
|
|
3069
|
+
return;
|
|
3070
|
+
}
|
|
3071
|
+
if (handleOpenCodeTurnEvent({ events, context, event })) return;
|
|
3072
|
+
if (noteOpenCodeSelectionEvent(context, event)) return;
|
|
3073
|
+
if (handleOpenCodeCompactionEvent({ events, context, event })) return;
|
|
3074
|
+
if (handleOpenCodeFormEvent({ events, context, event })) return;
|
|
3075
|
+
await handleOpenCodeToolEvent({ events, context, event });
|
|
3076
|
+
});
|
|
2949
3077
|
}
|
|
2950
3078
|
return new Error("OpenCode event stream ended.");
|
|
2951
3079
|
} catch (cause) {
|
|
@@ -2956,6 +3084,7 @@ function startEventPump(input) {
|
|
|
2956
3084
|
for (let attempt = 0; ; attempt += 1) {
|
|
2957
3085
|
receivedOnThisConnection = false;
|
|
2958
3086
|
const cause = await readOnce(attempt > 0);
|
|
3087
|
+
if (lifecycle.exiting) return;
|
|
2959
3088
|
if (context.stopped || context.eventAbort.signal.aborted) {
|
|
2960
3089
|
if (!connected) {
|
|
2961
3090
|
clearTimeout(readyTimer);
|
|
@@ -2971,15 +3100,15 @@ function startEventPump(input) {
|
|
|
2971
3100
|
}
|
|
2972
3101
|
log.warn(`OpenCode event stream dropped; reconnecting in ${delay} ms`);
|
|
2973
3102
|
await abortableDelay(delay, context.eventAbort.signal);
|
|
2974
|
-
if (context.stopped || context.eventAbort.signal.aborted) return;
|
|
3103
|
+
if (context.stopped || lifecycle.exiting || context.eventAbort.signal.aborted) return;
|
|
2975
3104
|
}
|
|
2976
3105
|
})();
|
|
2977
3106
|
if (!context.server.external && context.server.process) {
|
|
2978
|
-
|
|
3107
|
+
void context.server.process.exited.then((code) => {
|
|
2979
3108
|
if (context.stopped) return;
|
|
2980
3109
|
const cause = new Error(`OpenCode server exited with code ${code}.`);
|
|
2981
|
-
|
|
2982
|
-
fail(cause);
|
|
3110
|
+
if (connected) lifecycle.exit(cause, true);
|
|
3111
|
+
else fail(cause);
|
|
2983
3112
|
});
|
|
2984
3113
|
}
|
|
2985
3114
|
return ready;
|
|
@@ -3628,18 +3757,6 @@ function createOpenCodeTextGeneration(recipe, host) {
|
|
|
3628
3757
|
}
|
|
3629
3758
|
|
|
3630
3759
|
// src/opencodeInstance.ts
|
|
3631
|
-
var OPENCODE_STOP_INTERRUPT_TIMEOUT_MS = 2e3;
|
|
3632
|
-
async function interruptActiveTurnForStop(context) {
|
|
3633
|
-
if (context.session.activeTurnId === void 0) return;
|
|
3634
|
-
const abort = new AbortController();
|
|
3635
|
-
const timer = setTimeout(() => abort.abort(), OPENCODE_STOP_INTERRUPT_TIMEOUT_MS);
|
|
3636
|
-
try {
|
|
3637
|
-
await context.api.interrupt(context.sessionId, abort.signal);
|
|
3638
|
-
} catch {
|
|
3639
|
-
} finally {
|
|
3640
|
-
clearTimeout(timer);
|
|
3641
|
-
}
|
|
3642
|
-
}
|
|
3643
3760
|
function requireCwd(input, host) {
|
|
3644
3761
|
const fromInput = input.cwd?.trim() ?? "";
|
|
3645
3762
|
if (fromInput.length > 0) return fromInput;
|
|
@@ -3653,12 +3770,11 @@ function sessionFromContext(context) {
|
|
|
3653
3770
|
function requestedModelOf(modelSelection) {
|
|
3654
3771
|
return modelIdFromSelection(modelSelection) ?? readString2(modelSelection?.model);
|
|
3655
3772
|
}
|
|
3656
|
-
function createOpenCodeInstance(config, host, attachments) {
|
|
3773
|
+
function createOpenCodeInstance(config, host, attachments, lifecycleOptions) {
|
|
3657
3774
|
const recipe = parseOpenCodeInstanceConfig(config);
|
|
3658
3775
|
const events = new EventBus();
|
|
3659
3776
|
const sessions = /* @__PURE__ */ new Map();
|
|
3660
3777
|
const pendingServers = /* @__PURE__ */ new Map();
|
|
3661
|
-
const stops = /* @__PURE__ */ new WeakMap();
|
|
3662
3778
|
const rememberPendingServer = (threadId, server) => {
|
|
3663
3779
|
const pending = pendingServers.get(threadId) ?? /* @__PURE__ */ new Set();
|
|
3664
3780
|
pending.add(server);
|
|
@@ -3686,36 +3802,12 @@ function createOpenCodeInstance(config, host, attachments) {
|
|
|
3686
3802
|
const requireSession = (threadId) => {
|
|
3687
3803
|
const context = sessions.get(threadId);
|
|
3688
3804
|
if (!context) throw new Error(`unknown thread ${threadId}`);
|
|
3805
|
+
if (context.stopped || context.lifecycle?.closing)
|
|
3806
|
+
throw new Error("OpenCode session is closing.");
|
|
3689
3807
|
return context;
|
|
3690
3808
|
};
|
|
3691
|
-
const stopContext = async (context,
|
|
3692
|
-
|
|
3693
|
-
if (pending) return pending;
|
|
3694
|
-
const work = (async () => {
|
|
3695
|
-
context.stopped = true;
|
|
3696
|
-
context.eventAbort.abort();
|
|
3697
|
-
await interruptActiveTurnForStop(context);
|
|
3698
|
-
await closeOpenCodeServer(context.server);
|
|
3699
|
-
await context.pump?.catch(() => void 0);
|
|
3700
|
-
await context.serverWatch?.catch(() => void 0);
|
|
3701
|
-
if (reason === "graceful") {
|
|
3702
|
-
emitEvent(events, context, "session.exited", {
|
|
3703
|
-
reason: message ?? "Session stopped.",
|
|
3704
|
-
recoverable: false,
|
|
3705
|
-
exitKind: "graceful"
|
|
3706
|
-
});
|
|
3707
|
-
context.session = { ...context.session, status: "stopped", updatedAt: nowIso() };
|
|
3708
|
-
} else {
|
|
3709
|
-
emitUnexpectedExit(events, context, new Error(message ?? "OpenCode session failed."));
|
|
3710
|
-
}
|
|
3711
|
-
})();
|
|
3712
|
-
stops.set(context, work);
|
|
3713
|
-
try {
|
|
3714
|
-
await work;
|
|
3715
|
-
} catch (cause) {
|
|
3716
|
-
stops.delete(context);
|
|
3717
|
-
throw cause;
|
|
3718
|
-
}
|
|
3809
|
+
const stopContext = async (context, message) => {
|
|
3810
|
+
await context.lifecycle?.stop(message);
|
|
3719
3811
|
};
|
|
3720
3812
|
const instance = {
|
|
3721
3813
|
probe: (signal) => probeOpenCode({
|
|
@@ -3732,7 +3824,7 @@ function createOpenCodeInstance(config, host, attachments) {
|
|
|
3732
3824
|
if (disposed) throw new Error("OpenCode instance has been disposed.");
|
|
3733
3825
|
const existing = sessions.get(input.threadId);
|
|
3734
3826
|
if (existing) {
|
|
3735
|
-
await stopContext(existing, "
|
|
3827
|
+
await stopContext(existing, "Replaced by a new session.");
|
|
3736
3828
|
sessions.delete(input.threadId);
|
|
3737
3829
|
}
|
|
3738
3830
|
await closePendingServers(input.threadId);
|
|
@@ -3857,7 +3949,6 @@ function createOpenCodeInstance(config, host, attachments) {
|
|
|
3857
3949
|
server,
|
|
3858
3950
|
eventAbort: new AbortController(),
|
|
3859
3951
|
pump: void 0,
|
|
3860
|
-
serverWatch: void 0,
|
|
3861
3952
|
stopped: false,
|
|
3862
3953
|
usageByMessageId: /* @__PURE__ */ new Map(),
|
|
3863
3954
|
assistantTextByPartId: /* @__PURE__ */ new Map(),
|
|
@@ -3877,7 +3968,12 @@ function createOpenCodeInstance(config, host, attachments) {
|
|
|
3877
3968
|
...selection,
|
|
3878
3969
|
matchModel
|
|
3879
3970
|
};
|
|
3880
|
-
await startEventPump({
|
|
3971
|
+
await startEventPump({
|
|
3972
|
+
events,
|
|
3973
|
+
context,
|
|
3974
|
+
log: host.log,
|
|
3975
|
+
...lifecycleOptions ? { options: lifecycleOptions } : {}
|
|
3976
|
+
});
|
|
3881
3977
|
const winner = sessions.get(input.threadId);
|
|
3882
3978
|
if (winner) {
|
|
3883
3979
|
context.stopped = true;
|
|
@@ -3997,15 +4093,12 @@ function createOpenCodeInstance(config, host, attachments) {
|
|
|
3997
4093
|
);
|
|
3998
4094
|
const active = context.session.activeTurnId;
|
|
3999
4095
|
if (active && (turnId === void 0 || turnId === active)) {
|
|
4000
|
-
|
|
4001
|
-
|
|
4002
|
-
|
|
4003
|
-
|
|
4004
|
-
{ reason: "Interrupted by user." },
|
|
4005
|
-
{ turnId: active }
|
|
4006
|
-
);
|
|
4096
|
+
finishOpenCodeTurn(events, context, {
|
|
4097
|
+
state: "interrupted",
|
|
4098
|
+
reason: "Interrupted by user."
|
|
4099
|
+
});
|
|
4007
4100
|
context.session = {
|
|
4008
|
-
...
|
|
4101
|
+
...context.session,
|
|
4009
4102
|
status: "ready",
|
|
4010
4103
|
updatedAt: nowIso()
|
|
4011
4104
|
};
|
|
@@ -4040,7 +4133,7 @@ function createOpenCodeInstance(config, host, attachments) {
|
|
|
4040
4133
|
stopSession: async (threadId) => {
|
|
4041
4134
|
const context = sessions.get(threadId);
|
|
4042
4135
|
if (context) {
|
|
4043
|
-
await stopContext(context
|
|
4136
|
+
await stopContext(context);
|
|
4044
4137
|
if (sessions.get(threadId) === context) sessions.delete(threadId);
|
|
4045
4138
|
}
|
|
4046
4139
|
await closePendingServers(threadId);
|
|
@@ -4100,7 +4193,7 @@ var executors = [
|
|
|
4100
4193
|
];
|
|
4101
4194
|
var index_default = definePlugin({
|
|
4102
4195
|
id: "agent-opencode",
|
|
4103
|
-
apiVersion:
|
|
4196
|
+
apiVersion: 15,
|
|
4104
4197
|
executors,
|
|
4105
4198
|
activate(ctx) {
|
|
4106
4199
|
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.3158",
|
|
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.3122",
|
|
25
24
|
"ycodium-executor-testing": "0.0.0",
|
|
26
|
-
"ycodium-
|
|
27
|
-
"ycodium-
|
|
25
|
+
"ycodium-sse-stream": "0.0.0",
|
|
26
|
+
"@ycodium-ai/plugin-api": "0.2.3158",
|
|
27
|
+
"ycodium-fetch-ports": "0.0.0"
|
|
28
28
|
},
|
|
29
29
|
"ycodium": {
|
|
30
30
|
"plugin": "./dist/index.js",
|