@defend-tech/opencode-optima 0.1.70 → 0.1.71

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 CHANGED
@@ -10654,20 +10654,25 @@ function appendDirectoryQuery(url, directory) {
10654
10654
  const separator = url.includes("?") ? "&" : "?";
10655
10655
  return `${url}${separator}directory=${encodeURIComponent(value)}`;
10656
10656
  }
10657
- async function sendOpenCodeSessionEventDirect({ baseUrl, sessionId, text, agent, directory, fetchImpl = globalThis.fetch, legacyOnly = false } = {}) {
10657
+ function normalizeOpenCodePromptDelivery(value, fallback = "queue") {
10658
+ const normalized = String(value || "").trim().toLowerCase();
10659
+ return normalized === "steer" || normalized === "queue" ? normalized : fallback;
10660
+ }
10661
+ async function sendOpenCodeSessionEventDirect({ baseUrl, sessionId, text, agent, directory, fetchImpl = globalThis.fetch, legacyOnly = false, delivery = "queue" } = {}) {
10658
10662
  if (typeof fetchImpl !== "function") throw new Error("OpenCode direct prompt delivery requires fetch.");
10659
10663
  const root = normalizeOpenCodeBaseUrl(baseUrl, "");
10660
10664
  if (!root) throw new Error("OpenCode direct prompt delivery requires a base URL.");
10661
10665
  const encodedSession = encodeURIComponent(sessionId);
10666
+ const promptDelivery = normalizeOpenCodePromptDelivery(delivery, "queue");
10662
10667
  const attempts = [
10663
10668
  legacyOnly ? null : {
10664
10669
  name: "v2 prompt",
10665
10670
  url: appendDirectoryQuery(`${root}/api/session/${encodedSession}/prompt`, directory),
10666
- body: { prompt: { text }, delivery: "queue", resume: true },
10671
+ body: { prompt: { text }, delivery: promptDelivery, resume: true },
10667
10672
  accept: (response, data) => {
10668
10673
  if (!response.ok) return null;
10669
10674
  if (!openCodePromptAdmissionVerification(data, sessionId)) throw new Error("OpenCode v2 prompt response did not include a valid prompt admission.");
10670
- return { ok: true, method: "http", endpoint: "/api/session/{sessionID}/prompt", status: response.status, data: data.data, response: data };
10675
+ return { ok: true, method: "http", endpoint: "/api/session/{sessionID}/prompt", status: response.status, delivery: promptDelivery, data: data.data, response: data };
10671
10676
  }
10672
10677
  },
10673
10678
  {
@@ -10723,7 +10728,7 @@ async function callOpenCodePromptWithFallbacks(method, sessionId, flatPayload, s
10723
10728
  }
10724
10729
  throw firstError;
10725
10730
  }
10726
- async function sendOpenCodeSessionEvent(client, { sessionId, agent, text, directory, opencodeBaseUrl, baseUrl, fetchImpl, direct = false, legacyOnly = false, allowDirectFallback = true } = {}) {
10731
+ async function sendOpenCodeSessionEvent(client, { sessionId, agent, text, directory, opencodeBaseUrl, baseUrl, fetchImpl, direct = false, legacyOnly = false, allowDirectFallback = true, directDelivery = "queue" } = {}) {
10727
10732
  const directBaseUrl = opencodeBaseUrl || baseUrl;
10728
10733
  const parts = [{ type: "text", text }];
10729
10734
  const flatPayload = { directory, agent, parts };
@@ -10747,7 +10752,7 @@ async function sendOpenCodeSessionEvent(client, { sessionId, agent, text, direct
10747
10752
  firstError ??= error;
10748
10753
  }
10749
10754
  }
10750
- if (allowDirectFallback && directBaseUrl) return sendOpenCodeSessionEventDirect({ baseUrl: directBaseUrl, sessionId, text, agent, directory, fetchImpl, legacyOnly });
10755
+ if (allowDirectFallback && directBaseUrl) return sendOpenCodeSessionEventDirect({ baseUrl: directBaseUrl, sessionId, text, agent, directory, fetchImpl, legacyOnly, delivery: directDelivery });
10751
10756
  if (firstError) throw firstError;
10752
10757
  throw new Error("OpenCode client does not expose session.prompt or session.promptAsync.");
10753
10758
  }
@@ -11120,11 +11125,11 @@ function openCodeBlockingPromptVerification(result, sessionId) {
11120
11125
  if (parts.length > 0 || messageId) return { ok: true, method: parts.length > 0 ? "blocking_prompt_parts" : "blocking_prompt_message", messageId: messageId ? String(messageId) : null, sessionId: deliveredSessionId ? String(deliveredSessionId) : String(sessionId), parts: parts.length };
11121
11126
  return null;
11122
11127
  }
11123
- async function deliverClickUpSessionEventWithVerification({ openCodeClient, sendSessionEvent, clickupClient, worktree, taskId, sessionId, agent, text, directory, opencodeBaseUrl, directPrompt = false, acceptPromptAdmission = false, eventMarkers = [], verifySessionEventDelivery = verifyOpenCodeSessionEventDelivery, applyBlockerOnFailure = true } = {}) {
11128
+ async function deliverClickUpSessionEventWithVerification({ openCodeClient, sendSessionEvent, clickupClient, worktree, taskId, sessionId, agent, text, directory, opencodeBaseUrl, directPrompt = false, directDelivery = "queue", acceptPromptAdmission = false, eventMarkers = [], verifySessionEventDelivery = verifyOpenCodeSessionEventDelivery, applyBlockerOnFailure = true } = {}) {
11124
11129
  const beforeMessages = await readOpenCodeSessionMessages(openCodeClient, { sessionId, directory, limit: 50 }).catch(() => null);
11125
11130
  let sendResult;
11126
11131
  try {
11127
- sendResult = await sendSessionEvent(openCodeClient, { sessionId, agent, text, directory, opencodeBaseUrl, direct: directPrompt, allowDirectFallback: directPrompt });
11132
+ sendResult = await sendSessionEvent(openCodeClient, { sessionId, agent, text, directory, opencodeBaseUrl, direct: directPrompt, directDelivery, allowDirectFallback: directPrompt });
11128
11133
  } catch (error) {
11129
11134
  const reason2 = error.message || "message_delivery_failed";
11130
11135
  appendClickUpWebhookLocalLog(worktree, { type: "message_delivery_failed", taskId, sessionId, reason: reason2, fallbackAttempted: false });
@@ -11147,7 +11152,13 @@ async function deliverClickUpSessionEventWithVerification({ openCodeClient, send
11147
11152
  }
11148
11153
  if (blockingPromptVerification) return { ok: true, verification: blockingPromptVerification, admissionVerification: null, fallback: false };
11149
11154
  if (admissionVerification && acceptPromptAdmission) {
11150
- return { ok: true, verification: admissionVerification, admissionVerification, fallback: false, admissionOnly: true };
11155
+ appendClickUpWebhookLocalLog(worktree, {
11156
+ type: "message_delivery_admission_not_sufficient",
11157
+ taskId,
11158
+ sessionId,
11159
+ admission: admissionVerification,
11160
+ policy: "clickup_routing_requires_visible_delivery"
11161
+ });
11151
11162
  }
11152
11163
  let verification = await verifySessionEventDelivery(openCodeClient, { sessionId, directory, beforeMessages, expectedText: text, markers: eventMarkers });
11153
11164
  if (verification?.ok) return { ok: true, verification, admissionVerification, fallback: false };
@@ -11550,10 +11561,9 @@ async function routeClickUpWebhookEventUnlocked({ payload, config, state = {}, w
11550
11561
  const sessionId = String(existingSessionId);
11551
11562
  if (await sessionExists(openCodeClient, sessionId, { directory: taskRoute.worktree })) {
11552
11563
  if (typeof clickupClient?.updateTaskMetadata === "function") await clickupClient.updateTaskMetadata({ taskId, fieldId: config.routing.metadataFieldId, value: metadataWithRouting });
11553
- const delivery = await deliverClickUpSessionEventWithVerification({ openCodeClient, sendSessionEvent, clickupClient, worktree, taskId, sessionId, agent: config.routing.targetAgent, text: prompt, directory: taskRoute.worktree, opencodeBaseUrl: config.opencode?.baseUrl, directPrompt: config.opencode?.promptDelivery === "http", acceptPromptAdmission: config.opencode?.acceptPromptAdmission === true, eventMarkers: [taskId, eventType], verifySessionEventDelivery, applyBlockerOnFailure: false });
11564
+ const delivery = await deliverClickUpSessionEventWithVerification({ openCodeClient, sendSessionEvent, clickupClient, worktree, taskId, sessionId, agent: config.routing.targetAgent, text: prompt, directory: taskRoute.worktree, opencodeBaseUrl: config.opencode?.baseUrl, directPrompt: config.opencode?.promptDelivery === "http", directDelivery: "steer", acceptPromptAdmission: config.opencode?.acceptPromptAdmission === true, eventMarkers: [taskId, eventType], verifySessionEventDelivery, applyBlockerOnFailure: false });
11554
11565
  if (!delivery.ok) {
11555
- const recovery2 = await recoverClickUpPmSession({ openCodeClient, sendSessionEvent, clickupClient, worktree, taskId, staleSessionId: sessionId, sessionTitle, taskRoute, metadataWithRouting, config, prompt, eventMarkers: [taskId, eventType], deliveryEvidencePath, evidencePath: routingMetadata.evidence_path, eventKey, createSession, verifySessionEventDelivery });
11556
- return finish(recovery2);
11566
+ return finish({ ...delivery, eventKey, branch: taskRoute.branch, worktree: taskRoute.worktree, deliveryEvidencePath, evidencePath: routingMetadata.evidence_path, replacementAttempted: false });
11557
11567
  }
11558
11568
  return finish({ ok: true, action: "sent_to_existing_session", taskId, sessionId, eventKey, branch: taskRoute.branch, worktree: taskRoute.worktree, deliveryEvidencePath, evidencePath: routingMetadata.evidence_path, deliveryVerification: delivery.verification, deliveryAdmission: delivery.admissionVerification, deliveryFallback: delivery.fallback, deliveryAttempts: delivery.fallback ? 2 : 1 });
11559
11569
  }
@@ -10661,20 +10661,25 @@ function appendDirectoryQuery(url, directory) {
10661
10661
  const separator = url.includes("?") ? "&" : "?";
10662
10662
  return `${url}${separator}directory=${encodeURIComponent(value)}`;
10663
10663
  }
10664
- async function sendOpenCodeSessionEventDirect({ baseUrl, sessionId, text, agent, directory, fetchImpl = globalThis.fetch, legacyOnly = false } = {}) {
10664
+ function normalizeOpenCodePromptDelivery(value, fallback = "queue") {
10665
+ const normalized = String(value || "").trim().toLowerCase();
10666
+ return normalized === "steer" || normalized === "queue" ? normalized : fallback;
10667
+ }
10668
+ async function sendOpenCodeSessionEventDirect({ baseUrl, sessionId, text, agent, directory, fetchImpl = globalThis.fetch, legacyOnly = false, delivery = "queue" } = {}) {
10665
10669
  if (typeof fetchImpl !== "function") throw new Error("OpenCode direct prompt delivery requires fetch.");
10666
10670
  const root = normalizeOpenCodeBaseUrl(baseUrl, "");
10667
10671
  if (!root) throw new Error("OpenCode direct prompt delivery requires a base URL.");
10668
10672
  const encodedSession = encodeURIComponent(sessionId);
10673
+ const promptDelivery = normalizeOpenCodePromptDelivery(delivery, "queue");
10669
10674
  const attempts = [
10670
10675
  legacyOnly ? null : {
10671
10676
  name: "v2 prompt",
10672
10677
  url: appendDirectoryQuery(`${root}/api/session/${encodedSession}/prompt`, directory),
10673
- body: { prompt: { text }, delivery: "queue", resume: true },
10678
+ body: { prompt: { text }, delivery: promptDelivery, resume: true },
10674
10679
  accept: (response, data) => {
10675
10680
  if (!response.ok) return null;
10676
10681
  if (!openCodePromptAdmissionVerification(data, sessionId)) throw new Error("OpenCode v2 prompt response did not include a valid prompt admission.");
10677
- return { ok: true, method: "http", endpoint: "/api/session/{sessionID}/prompt", status: response.status, data: data.data, response: data };
10682
+ return { ok: true, method: "http", endpoint: "/api/session/{sessionID}/prompt", status: response.status, delivery: promptDelivery, data: data.data, response: data };
10678
10683
  }
10679
10684
  },
10680
10685
  {
@@ -10730,7 +10735,7 @@ async function callOpenCodePromptWithFallbacks(method, sessionId, flatPayload, s
10730
10735
  }
10731
10736
  throw firstError;
10732
10737
  }
10733
- async function sendOpenCodeSessionEvent(client, { sessionId, agent, text, directory, opencodeBaseUrl, baseUrl, fetchImpl, direct = false, legacyOnly = false, allowDirectFallback = true } = {}) {
10738
+ async function sendOpenCodeSessionEvent(client, { sessionId, agent, text, directory, opencodeBaseUrl, baseUrl, fetchImpl, direct = false, legacyOnly = false, allowDirectFallback = true, directDelivery = "queue" } = {}) {
10734
10739
  const directBaseUrl = opencodeBaseUrl || baseUrl;
10735
10740
  const parts = [{ type: "text", text }];
10736
10741
  const flatPayload = { directory, agent, parts };
@@ -10754,7 +10759,7 @@ async function sendOpenCodeSessionEvent(client, { sessionId, agent, text, direct
10754
10759
  firstError ??= error;
10755
10760
  }
10756
10761
  }
10757
- if (allowDirectFallback && directBaseUrl) return sendOpenCodeSessionEventDirect({ baseUrl: directBaseUrl, sessionId, text, agent, directory, fetchImpl, legacyOnly });
10762
+ if (allowDirectFallback && directBaseUrl) return sendOpenCodeSessionEventDirect({ baseUrl: directBaseUrl, sessionId, text, agent, directory, fetchImpl, legacyOnly, delivery: directDelivery });
10758
10763
  if (firstError) throw firstError;
10759
10764
  throw new Error("OpenCode client does not expose session.prompt or session.promptAsync.");
10760
10765
  }
@@ -11127,11 +11132,11 @@ function openCodeBlockingPromptVerification(result, sessionId) {
11127
11132
  if (parts.length > 0 || messageId) return { ok: true, method: parts.length > 0 ? "blocking_prompt_parts" : "blocking_prompt_message", messageId: messageId ? String(messageId) : null, sessionId: deliveredSessionId ? String(deliveredSessionId) : String(sessionId), parts: parts.length };
11128
11133
  return null;
11129
11134
  }
11130
- async function deliverClickUpSessionEventWithVerification({ openCodeClient, sendSessionEvent, clickupClient, worktree, taskId, sessionId, agent, text, directory, opencodeBaseUrl, directPrompt = false, acceptPromptAdmission = false, eventMarkers = [], verifySessionEventDelivery = verifyOpenCodeSessionEventDelivery, applyBlockerOnFailure = true } = {}) {
11135
+ async function deliverClickUpSessionEventWithVerification({ openCodeClient, sendSessionEvent, clickupClient, worktree, taskId, sessionId, agent, text, directory, opencodeBaseUrl, directPrompt = false, directDelivery = "queue", acceptPromptAdmission = false, eventMarkers = [], verifySessionEventDelivery = verifyOpenCodeSessionEventDelivery, applyBlockerOnFailure = true } = {}) {
11131
11136
  const beforeMessages = await readOpenCodeSessionMessages(openCodeClient, { sessionId, directory, limit: 50 }).catch(() => null);
11132
11137
  let sendResult;
11133
11138
  try {
11134
- sendResult = await sendSessionEvent(openCodeClient, { sessionId, agent, text, directory, opencodeBaseUrl, direct: directPrompt, allowDirectFallback: directPrompt });
11139
+ sendResult = await sendSessionEvent(openCodeClient, { sessionId, agent, text, directory, opencodeBaseUrl, direct: directPrompt, directDelivery, allowDirectFallback: directPrompt });
11135
11140
  } catch (error) {
11136
11141
  const reason2 = error.message || "message_delivery_failed";
11137
11142
  appendClickUpWebhookLocalLog(worktree, { type: "message_delivery_failed", taskId, sessionId, reason: reason2, fallbackAttempted: false });
@@ -11154,7 +11159,13 @@ async function deliverClickUpSessionEventWithVerification({ openCodeClient, send
11154
11159
  }
11155
11160
  if (blockingPromptVerification) return { ok: true, verification: blockingPromptVerification, admissionVerification: null, fallback: false };
11156
11161
  if (admissionVerification && acceptPromptAdmission) {
11157
- return { ok: true, verification: admissionVerification, admissionVerification, fallback: false, admissionOnly: true };
11162
+ appendClickUpWebhookLocalLog(worktree, {
11163
+ type: "message_delivery_admission_not_sufficient",
11164
+ taskId,
11165
+ sessionId,
11166
+ admission: admissionVerification,
11167
+ policy: "clickup_routing_requires_visible_delivery"
11168
+ });
11158
11169
  }
11159
11170
  let verification = await verifySessionEventDelivery(openCodeClient, { sessionId, directory, beforeMessages, expectedText: text, markers: eventMarkers });
11160
11171
  if (verification?.ok) return { ok: true, verification, admissionVerification, fallback: false };
@@ -11557,10 +11568,9 @@ async function routeClickUpWebhookEventUnlocked({ payload, config, state = {}, w
11557
11568
  const sessionId = String(existingSessionId);
11558
11569
  if (await sessionExists(openCodeClient, sessionId, { directory: taskRoute.worktree })) {
11559
11570
  if (typeof clickupClient?.updateTaskMetadata === "function") await clickupClient.updateTaskMetadata({ taskId, fieldId: config.routing.metadataFieldId, value: metadataWithRouting });
11560
- const delivery = await deliverClickUpSessionEventWithVerification({ openCodeClient, sendSessionEvent, clickupClient, worktree, taskId, sessionId, agent: config.routing.targetAgent, text: prompt, directory: taskRoute.worktree, opencodeBaseUrl: config.opencode?.baseUrl, directPrompt: config.opencode?.promptDelivery === "http", acceptPromptAdmission: config.opencode?.acceptPromptAdmission === true, eventMarkers: [taskId, eventType], verifySessionEventDelivery, applyBlockerOnFailure: false });
11571
+ const delivery = await deliverClickUpSessionEventWithVerification({ openCodeClient, sendSessionEvent, clickupClient, worktree, taskId, sessionId, agent: config.routing.targetAgent, text: prompt, directory: taskRoute.worktree, opencodeBaseUrl: config.opencode?.baseUrl, directPrompt: config.opencode?.promptDelivery === "http", directDelivery: "steer", acceptPromptAdmission: config.opencode?.acceptPromptAdmission === true, eventMarkers: [taskId, eventType], verifySessionEventDelivery, applyBlockerOnFailure: false });
11561
11572
  if (!delivery.ok) {
11562
- const recovery2 = await recoverClickUpPmSession({ openCodeClient, sendSessionEvent, clickupClient, worktree, taskId, staleSessionId: sessionId, sessionTitle, taskRoute, metadataWithRouting, config, prompt, eventMarkers: [taskId, eventType], deliveryEvidencePath, evidencePath: routingMetadata.evidence_path, eventKey, createSession, verifySessionEventDelivery });
11563
- return finish(recovery2);
11573
+ return finish({ ...delivery, eventKey, branch: taskRoute.branch, worktree: taskRoute.worktree, deliveryEvidencePath, evidencePath: routingMetadata.evidence_path, replacementAttempted: false });
11564
11574
  }
11565
11575
  return finish({ ok: true, action: "sent_to_existing_session", taskId, sessionId, eventKey, branch: taskRoute.branch, worktree: taskRoute.worktree, deliveryEvidencePath, evidencePath: routingMetadata.evidence_path, deliveryVerification: delivery.verification, deliveryAdmission: delivery.admissionVerification, deliveryFallback: delivery.fallback, deliveryAttempts: delivery.fallback ? 2 : 1 });
11566
11576
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@defend-tech/opencode-optima",
3
- "version": "0.1.70",
3
+ "version": "0.1.71",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+ssh://git@github.com/defend-tech/opencode-optima.git"