@ycodium-ai/agent-opencode 0.2.3056 → 0.2.3068

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.
Files changed (2) hide show
  1. package/dist/index.js +74 -54
  2. package/package.json +4 -4
package/dist/index.js CHANGED
@@ -350,9 +350,9 @@ var CATEGORY_BY_STATUS = {
350
350
  };
351
351
  function openCodeFailureCategory(error) {
352
352
  if (!isRecord2(error)) return void 0;
353
- const type3 = error["type"];
354
- if (typeof type3 === "string" && Object.hasOwn(CATEGORY_BY_TYPE, type3)) {
355
- return CATEGORY_BY_TYPE[type3];
353
+ const type2 = error["type"];
354
+ if (typeof type2 === "string" && Object.hasOwn(CATEGORY_BY_TYPE, type2)) {
355
+ return CATEGORY_BY_TYPE[type2];
356
356
  }
357
357
  const status = error["status"];
358
358
  if (typeof status === "number" && Object.hasOwn(CATEGORY_BY_STATUS, status)) {
@@ -379,7 +379,7 @@ function withLastError(session, lastError, failureCategory) {
379
379
  void previous;
380
380
  return { ...rest, lastError, ...failureCategoryField(failureCategory) };
381
381
  }
382
- function emitEvent(events, context, type3, payload, extra) {
382
+ function emitEvent(events, context, type2, payload, extra) {
383
383
  const turnId = extra?.turnId ?? context.session.activeTurnId;
384
384
  const event = {
385
385
  eventId: randomUUID(),
@@ -387,7 +387,7 @@ function emitEvent(events, context, type3, payload, extra) {
387
387
  ...context.session.providerInstanceId ? { providerInstanceId: context.session.providerInstanceId } : {},
388
388
  threadId: context.threadId,
389
389
  createdAt: nowIso(),
390
- type: type3,
390
+ type: type2,
391
391
  payload,
392
392
  ...turnId ? { turnId } : {},
393
393
  ...extra?.itemId ? { itemId: extra.itemId } : {},
@@ -577,7 +577,7 @@ function watchListLoad(api, directory, signal) {
577
577
  })();
578
578
  return { connected, loaded };
579
579
  }
580
- async function listOpenCodeModelsWhenLoaded(api, directory, timeoutMs = OPENCODE_LIST_WARMUP_MS) {
580
+ async function readWhenLoaded(api, directory, read, complete, timeoutMs) {
581
581
  const abort = new AbortController();
582
582
  let timer;
583
583
  const deadline = new Promise((resolve2) => {
@@ -586,18 +586,34 @@ async function listOpenCodeModelsWhenLoaded(api, directory, timeoutMs = OPENCODE
586
586
  try {
587
587
  const watch = watchListLoad(api, directory, abort.signal);
588
588
  await Promise.race([watch.connected, deadline]);
589
- const models = await api.listModels(directory);
590
- if (models.length > 0) return models;
589
+ const first = await read();
590
+ if (complete(first)) return first;
591
591
  await Promise.race([watch.loaded, deadline]);
592
- return await api.listModels(directory);
592
+ return await read();
593
593
  } finally {
594
594
  clearTimeout(timer);
595
595
  abort.abort();
596
596
  }
597
597
  }
598
- async function listOpenCodeAgentsWhenLoaded(api, directory) {
599
- await listOpenCodeModelsWhenLoaded(api, directory);
600
- return api.listAgents(directory);
598
+ var listOpenCodeModelsWhenLoaded = (api, directory, timeoutMs = OPENCODE_LIST_WARMUP_MS) => readWhenLoaded(
599
+ api,
600
+ directory,
601
+ () => api.listModels(directory),
602
+ (models) => models.length > 0,
603
+ timeoutMs
604
+ );
605
+ async function listOpenCodeAgentsForSelection(api, directory, chosen) {
606
+ if (chosen === void 0) {
607
+ await listOpenCodeModelsWhenLoaded(api, directory);
608
+ return api.listAgents(directory);
609
+ }
610
+ return readWhenLoaded(
611
+ api,
612
+ directory,
613
+ () => api.listAgents(directory),
614
+ (agents) => agents.some((entry) => entry.id === chosen),
615
+ OPENCODE_LIST_WARMUP_MS
616
+ );
601
617
  }
602
618
 
603
619
  // src/slug.ts
@@ -1181,7 +1197,6 @@ var ServerSentEventParser = class {
1181
1197
 
1182
1198
  // src/server.ts
1183
1199
  import * as NodeCrypto2 from "node:crypto";
1184
- import * as NodeOS4 from "node:os";
1185
1200
  import {
1186
1201
  mergeLaunchEnv as mergeLaunchEnv2
1187
1202
  } from "@ycodium-ai/plugin-api/executor";
@@ -1383,12 +1398,6 @@ async function terminateOwnedProcess(handle) {
1383
1398
  await handle.kill("SIGKILL");
1384
1399
  } catch {
1385
1400
  }
1386
- if (NodeOS4.type() !== "Windows_NT" && handle.pid > 0) {
1387
- try {
1388
- process.kill(-handle.pid, "SIGKILL");
1389
- } catch {
1390
- }
1391
- }
1392
1401
  await handle.exited;
1393
1402
  }
1394
1403
  }
@@ -1405,7 +1414,6 @@ async function spawnOpenCodeServer(input) {
1405
1414
  owner: { bindingId: input.host.bindingId }
1406
1415
  });
1407
1416
  const processHandle = {
1408
- pid: spawned.pid,
1409
1417
  kill: spawned.kill,
1410
1418
  exited: spawned.exited
1411
1419
  };
@@ -1687,14 +1695,14 @@ function decodeOpenCodeEvent(frame) {
1687
1695
  return "not JSON";
1688
1696
  }
1689
1697
  if (!isRecord2(parsed)) return "not an object";
1690
- const type3 = stringField(parsed, "type");
1691
- if (type3 === void 0) return "no type";
1698
+ const type2 = stringField(parsed, "type");
1699
+ if (type2 === void 0) return "no type";
1692
1700
  const data = parsed["data"];
1693
1701
  const fields = isRecord2(data) ? data : {};
1694
1702
  const location = parsed["location"];
1695
1703
  const directory = isRecord2(location) ? stringField(location, "directory") : void 0;
1696
1704
  return {
1697
- type: type3,
1705
+ type: type2,
1698
1706
  sessionId: stringField(fields, "sessionID"),
1699
1707
  data: fields,
1700
1708
  ...directory === void 0 ? {} : { directory }
@@ -1832,8 +1840,12 @@ function createOpenCodeApi(server) {
1832
1840
  }
1833
1841
  return id;
1834
1842
  },
1835
- interrupt: async (sessionId) => {
1836
- await call("POST", sessionPath(sessionId, "/interrupt"), { wrapped: false, body: {} });
1843
+ interrupt: async (sessionId, signal) => {
1844
+ await call("POST", sessionPath(sessionId, "/interrupt"), {
1845
+ wrapped: false,
1846
+ body: {},
1847
+ ...signal === void 0 ? {} : { signal }
1848
+ });
1837
1849
  },
1838
1850
  replyPermission: async (sessionId, requestId, decision, message) => {
1839
1851
  await sendWithoutResult(
@@ -1949,7 +1961,7 @@ async function addOpenCodeMcpServers(input) {
1949
1961
  }
1950
1962
 
1951
1963
  // src/probe.ts
1952
- import * as NodeOS5 from "node:os";
1964
+ import * as NodeOS4 from "node:os";
1953
1965
  import {
1954
1966
  ExecutorSpawnRefusedError
1955
1967
  } from "@ycodium-ai/plugin-api/executor";
@@ -2017,7 +2029,7 @@ ${result.stderr}`);
2017
2029
  const handle = await connectToOpenCodeServer({
2018
2030
  host: input.host,
2019
2031
  recipe: input.recipe,
2020
- directory: isExternal ? "." : NodeOS5.homedir(),
2032
+ directory: isExternal ? "." : NodeOS4.homedir(),
2021
2033
  program
2022
2034
  });
2023
2035
  try {
@@ -2042,7 +2054,7 @@ ${result.stderr}`);
2042
2054
  }
2043
2055
  }
2044
2056
  const api = createOpenCodeApi({ url: handle.url, password: handle.password });
2045
- const inventory = await loadOpenCodeInventory(api, NodeOS5.homedir());
2057
+ const inventory = await loadOpenCodeInventory(api, NodeOS4.homedir());
2046
2058
  const skills = flattenOpenCodeSkills(inventory.skills);
2047
2059
  const models = openCodeProbeModels(inventory);
2048
2060
  if (models.length > 0) {
@@ -2737,7 +2749,7 @@ async function applyOpenCodeTurnSelection(input) {
2737
2749
  const chosen = getModelSelectionStringOptionValue(input.modelSelection, "agent") ?? (input.interactionMode === "plan" ? OPENCODE_PLAN_AGENT : void 0);
2738
2750
  if (chosen === void 0 && context.selectedAgent === null) return;
2739
2751
  if (chosen !== void 0 && chosen === context.selectedAgent) return;
2740
- const known = await listOpenCodeAgentsWhenLoaded(context.api, context.directory);
2752
+ const known = await listOpenCodeAgentsForSelection(context.api, context.directory, chosen);
2741
2753
  const agent = chosen ?? inferDefaultAgent(selectableOpenCodeAgents(known));
2742
2754
  if (agent === void 0 || agent === context.selectedAgent) return;
2743
2755
  if (!known.some((entry) => entry.id === agent)) {
@@ -2829,6 +2841,7 @@ async function handleOtherSessionEvent(events, context, event) {
2829
2841
  }
2830
2842
  }
2831
2843
  var OPENCODE_EVENT_RECONNECT_DELAYS_MS = [200, 1e3, 3e3];
2844
+ var OPENCODE_EVENT_READY_TIMEOUT_MS = 1e4;
2832
2845
  var abortableDelay = (ms, signal) => new Promise((resolve2) => {
2833
2846
  const timer = setTimeout(done, ms);
2834
2847
  function done() {
@@ -2890,12 +2903,21 @@ function startEventPump(input) {
2890
2903
  const ready = new Promise((resolve2, reject) => {
2891
2904
  settleConnected = { resolve: resolve2, reject };
2892
2905
  });
2906
+ const readyTimer = setTimeout(() => {
2907
+ fail(
2908
+ new Error(
2909
+ `OpenCode event stream timed out after ${OPENCODE_EVENT_READY_TIMEOUT_MS}ms waiting for its first event.`
2910
+ )
2911
+ );
2912
+ }, OPENCODE_EVENT_READY_TIMEOUT_MS);
2893
2913
  const fail = (cause) => {
2894
2914
  if (connected) {
2895
2915
  emitUnexpectedExit(events, context, cause);
2896
2916
  return;
2897
2917
  }
2918
+ clearTimeout(readyTimer);
2898
2919
  context.stopped = true;
2920
+ context.eventAbort.abort();
2899
2921
  settleConnected?.reject(cause);
2900
2922
  };
2901
2923
  let receivedOnThisConnection = false;
@@ -2904,7 +2926,9 @@ function startEventPump(input) {
2904
2926
  try {
2905
2927
  for await (const event of context.api.events(context.eventAbort.signal, onSkipped)) {
2906
2928
  if (!connected) {
2929
+ if (context.stopped) break;
2907
2930
  connected = true;
2931
+ clearTimeout(readyTimer);
2908
2932
  settleConnected?.resolve();
2909
2933
  }
2910
2934
  if (first) {
@@ -2933,7 +2957,10 @@ function startEventPump(input) {
2933
2957
  receivedOnThisConnection = false;
2934
2958
  const cause = await readOnce(attempt > 0);
2935
2959
  if (context.stopped || context.eventAbort.signal.aborted) {
2936
- if (!connected) settleConnected?.reject(cause);
2960
+ if (!connected) {
2961
+ clearTimeout(readyTimer);
2962
+ settleConnected?.reject(cause);
2963
+ }
2937
2964
  return;
2938
2965
  }
2939
2966
  if (receivedOnThisConnection) attempt = 0;
@@ -3447,7 +3474,7 @@ function createOpenCodeTextGeneration(recipe, host) {
3447
3474
  const api = await acquireClient({ cwd: input.cwd });
3448
3475
  try {
3449
3476
  const model = toOpenCodeModelRef({
3450
- requested: modelIdFromSelection(input.modelSelection) ?? OPENCODE_CARD.defaultModel,
3477
+ requested: modelIdFromSelection(input.modelSelection),
3451
3478
  modelSelection: input.modelSelection,
3452
3479
  match: matchModel
3453
3480
  });
@@ -3601,6 +3628,18 @@ function createOpenCodeTextGeneration(recipe, host) {
3601
3628
  }
3602
3629
 
3603
3630
  // 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
+ }
3604
3643
  function requireCwd(input, host) {
3605
3644
  const fromInput = input.cwd?.trim() ?? "";
3606
3645
  if (fromInput.length > 0) return fromInput;
@@ -3649,25 +3688,13 @@ function createOpenCodeInstance(config, host, attachments) {
3649
3688
  if (!context) throw new Error(`unknown thread ${threadId}`);
3650
3689
  return context;
3651
3690
  };
3652
- const abortCreatedSession = async (context) => {
3653
- context.stopped = true;
3654
- context.eventAbort.abort();
3655
- try {
3656
- await context.api.interrupt(context.sessionId);
3657
- } catch {
3658
- }
3659
- await closeOpenCodeServer(context.server);
3660
- };
3661
3691
  const stopContext = async (context, reason, message) => {
3662
3692
  const pending = stops.get(context);
3663
3693
  if (pending) return pending;
3664
3694
  const work = (async () => {
3665
3695
  context.stopped = true;
3666
3696
  context.eventAbort.abort();
3667
- try {
3668
- await context.api.interrupt(context.sessionId);
3669
- } catch {
3670
- }
3697
+ await interruptActiveTurnForStop(context);
3671
3698
  await closeOpenCodeServer(context.server);
3672
3699
  await context.pump?.catch(() => void 0);
3673
3700
  await context.serverWatch?.catch(() => void 0);
@@ -3743,7 +3770,6 @@ function createOpenCodeInstance(config, host, attachments) {
3743
3770
  );
3744
3771
  const resume = input.discardResumeCursor === true ? void 0 : parseOpenCodeResume(input.resumeCursor);
3745
3772
  let sessionId;
3746
- let createdSession = false;
3747
3773
  let selection = {
3748
3774
  selectedModel: void 0,
3749
3775
  selectedAgent: void 0
@@ -3762,7 +3788,6 @@ function createOpenCodeInstance(config, host, attachments) {
3762
3788
  ...requestedTitle ? { title: requestedTitle } : {},
3763
3789
  permissions
3764
3790
  });
3765
- createdSession = true;
3766
3791
  selection = openCodeSelectionOf(created);
3767
3792
  return created.id;
3768
3793
  };
@@ -3781,7 +3806,6 @@ function createOpenCodeInstance(config, host, attachments) {
3781
3806
  await api.deleteSession(forked.id).catch(() => void 0);
3782
3807
  throw cause;
3783
3808
  }
3784
- createdSession = true;
3785
3809
  selection = openCodeSelectionOf(forked);
3786
3810
  return forked.id;
3787
3811
  };
@@ -3831,7 +3855,6 @@ function createOpenCodeInstance(config, host, attachments) {
3831
3855
  directory,
3832
3856
  api,
3833
3857
  server,
3834
- createdSession,
3835
3858
  eventAbort: new AbortController(),
3836
3859
  pump: void 0,
3837
3860
  serverWatch: void 0,
@@ -3857,12 +3880,9 @@ function createOpenCodeInstance(config, host, attachments) {
3857
3880
  await startEventPump({ events, context, log: host.log });
3858
3881
  const winner = sessions.get(input.threadId);
3859
3882
  if (winner) {
3860
- if (createdSession) {
3861
- await abortCreatedSession(context);
3862
- } else {
3863
- context.eventAbort.abort();
3864
- await closeOpenCodeServer(server);
3865
- }
3883
+ context.stopped = true;
3884
+ context.eventAbort.abort();
3885
+ await closeOpenCodeServer(server);
3866
3886
  return sessionFromContext(winner);
3867
3887
  }
3868
3888
  sessions.set(input.threadId, context);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ycodium-ai/agent-opencode",
3
- "version": "0.2.3056",
3
+ "version": "0.2.3068",
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.3056",
24
+ "@ycodium-ai/plugin-api": "0.2.0",
25
+ "ycodium-sse-stream": "0.0.0",
25
26
  "ycodium-executor-testing": "0.0.0",
26
- "ycodium-fetch-ports": "0.0.0",
27
- "ycodium-sse-stream": "0.0.0"
27
+ "ycodium-fetch-ports": "0.0.0"
28
28
  },
29
29
  "ycodium": {
30
30
  "plugin": "./dist/index.js",