@defend-tech/opencode-optima 0.1.52 → 0.1.53

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
@@ -9648,23 +9648,16 @@ function extractOpenCodeSessionId(result) {
9648
9648
  return data?.id || data?.sessionID || data?.sessionId || result?.id || result?.sessionID || result?.sessionId;
9649
9649
  }
9650
9650
  async function createOpenCodeSession(client, { title, directory, agent } = {}) {
9651
- const flatPayload = { directory, title };
9652
- if (agent) flatPayload.agent = agent;
9653
9651
  const body = { title };
9654
9652
  if (agent) body.agent = agent;
9655
- const attempts = [{ query: { directory }, body }, flatPayload];
9656
- let firstError = null;
9657
- for (const attempt of attempts) {
9658
- try {
9659
- const result = await client.session.create(attempt);
9660
- const sessionId = extractOpenCodeSessionId(result);
9661
- if (sessionId) return sessionId;
9662
- firstError ??= new Error("OpenCode session create response did not include a session id.");
9663
- } catch (error) {
9664
- firstError ??= error;
9665
- }
9653
+ try {
9654
+ const result = await client.session.create({ query: { directory }, body });
9655
+ const sessionId = extractOpenCodeSessionId(result);
9656
+ if (sessionId) return sessionId;
9657
+ throw new Error("OpenCode session create response did not include a session id.");
9658
+ } catch (error) {
9659
+ throw error || new Error("OpenCode session create failed.");
9666
9660
  }
9667
- throw firstError || new Error("OpenCode session create failed.");
9668
9661
  }
9669
9662
  async function waitForOpenCodeReadiness(client, { worktree = process.cwd(), attempts = 10, delayMs = 500, now = () => /* @__PURE__ */ new Date() } = {}) {
9670
9663
  if (typeof client?.session?.create !== "function") return { ok: true, skipped: true, reason: "session_create_probe_unavailable" };
@@ -9778,8 +9771,8 @@ function tagOpenCodePromptResult(result, deliveryMethod) {
9778
9771
  return result;
9779
9772
  }
9780
9773
  async function callOpenCodePromptWithFallbacks(method, sessionId, flatPayload, structuredPayload, deliveryMethod) {
9781
- const attempts = [
9782
- { ...structuredPayload, path: { id: sessionId } },
9774
+ const attempts = deliveryMethod === "prompt" ? [structuredPayload] : [
9775
+ structuredPayload,
9783
9776
  { ...structuredPayload, path: { sessionID: sessionId } },
9784
9777
  { sessionID: sessionId, ...flatPayload },
9785
9778
  { id: sessionId, ...flatPayload }
@@ -9829,23 +9822,11 @@ function normalizeOpenCodeSessionMessages(result) {
9829
9822
  if (Array.isArray(data?.items)) return [...data.items];
9830
9823
  return [];
9831
9824
  }
9832
- async function readOpenCodeSessionMessages(client, { sessionId, limit = 20 } = {}) {
9825
+ async function readOpenCodeSessionMessages(client, { sessionId, directory, limit = 20 } = {}) {
9833
9826
  if (typeof client?.session?.messages !== "function") return null;
9834
- const attempts = [
9835
- { path: { id: sessionId }, query: { limit } },
9836
- { path: { sessionID: sessionId }, query: { limit } },
9837
- { sessionID: sessionId, limit },
9838
- { id: sessionId, limit }
9839
- ];
9840
- let firstError = null;
9841
- for (const attempt of attempts) {
9842
- try {
9843
- return normalizeOpenCodeSessionMessages(await client.session.messages(attempt));
9844
- } catch (error) {
9845
- firstError ??= error;
9846
- }
9847
- }
9848
- throw firstError;
9827
+ const query = { limit };
9828
+ if (directory) query.directory = directory;
9829
+ return normalizeOpenCodeSessionMessages(await client.session.messages({ path: { id: sessionId }, query }));
9849
9830
  }
9850
9831
  function openCodeMessageText(message) {
9851
9832
  const parts = [message?.text, message?.content, message?.message, message?.body?.text, message?.data?.text];
@@ -9858,11 +9839,11 @@ function openCodeMessageStableKey(message, index = 0) {
9858
9839
  if (id) return `id:${id}`;
9859
9840
  return `idx:${index}:text:${openCodeMessageText(message)}`;
9860
9841
  }
9861
- async function verifyOpenCodeSessionEventDelivery(client, { sessionId, beforeMessages = null, expectedText = "", markers = [], attempts = 8, delayMs = 250 } = {}) {
9842
+ async function verifyOpenCodeSessionEventDelivery(client, { sessionId, directory, beforeMessages = null, expectedText = "", markers = [], attempts = 8, delayMs = 250 } = {}) {
9862
9843
  let lastError = "message_verification_unavailable";
9863
9844
  for (let attempt = 0; attempt < Math.max(1, attempts); attempt += 1) {
9864
9845
  try {
9865
- const afterMessages = await readOpenCodeSessionMessages(client, { sessionId, limit: 50 });
9846
+ const afterMessages = await readOpenCodeSessionMessages(client, { sessionId, directory, limit: 50 });
9866
9847
  if (!afterMessages) return { ok: false, reason: "message_verification_unavailable" };
9867
9848
  const beforeCount = Array.isArray(beforeMessages) ? beforeMessages.length : null;
9868
9849
  const beforeKeys = Array.isArray(beforeMessages) ? new Set(beforeMessages.map(openCodeMessageStableKey)) : null;
@@ -9910,7 +9891,7 @@ function openCodeBlockingPromptVerification(result, sessionId) {
9910
9891
  return null;
9911
9892
  }
9912
9893
  async function deliverClickUpSessionEventWithVerification({ openCodeClient, sendSessionEvent, clickupClient, worktree, taskId, sessionId, agent, text, directory, opencodeBaseUrl, eventMarkers = [], verifySessionEventDelivery = verifyOpenCodeSessionEventDelivery, applyBlockerOnFailure = true } = {}) {
9913
- const beforeMessages = await readOpenCodeSessionMessages(openCodeClient, { sessionId, limit: 50 }).catch(() => null);
9894
+ const beforeMessages = await readOpenCodeSessionMessages(openCodeClient, { sessionId, directory, limit: 50 }).catch(() => null);
9914
9895
  const sendResult = await sendSessionEvent(openCodeClient, { sessionId, agent, text, directory, opencodeBaseUrl, allowDirectFallback: false });
9915
9896
  let blockingPromptVerification = null;
9916
9897
  let admissionVerification = null;
@@ -9924,7 +9905,7 @@ async function deliverClickUpSessionEventWithVerification({ openCodeClient, send
9924
9905
  return { ok: false, action: "message_delivery_failed", reason: error.message, taskId, sessionId, fallbackAttempted: false, blockerTag: blocker2 };
9925
9906
  }
9926
9907
  if (blockingPromptVerification) return { ok: true, verification: blockingPromptVerification, admissionVerification: null, fallback: false };
9927
- let verification = await verifySessionEventDelivery(openCodeClient, { sessionId, beforeMessages, expectedText: text, markers: eventMarkers });
9908
+ let verification = await verifySessionEventDelivery(openCodeClient, { sessionId, directory, beforeMessages, expectedText: text, markers: eventMarkers });
9928
9909
  if (verification?.ok) return { ok: true, verification, admissionVerification, fallback: false };
9929
9910
  if (admissionVerification) {
9930
9911
  appendClickUpWebhookLocalLog(worktree, { type: "message_delivery_admitted_but_invisible", taskId, sessionId, admission: admissionVerification, reason: verification?.reason || "message_not_visible" });
@@ -9655,23 +9655,16 @@ function extractOpenCodeSessionId(result) {
9655
9655
  return data?.id || data?.sessionID || data?.sessionId || result?.id || result?.sessionID || result?.sessionId;
9656
9656
  }
9657
9657
  async function createOpenCodeSession(client, { title, directory, agent } = {}) {
9658
- const flatPayload = { directory, title };
9659
- if (agent) flatPayload.agent = agent;
9660
9658
  const body = { title };
9661
9659
  if (agent) body.agent = agent;
9662
- const attempts = [{ query: { directory }, body }, flatPayload];
9663
- let firstError = null;
9664
- for (const attempt of attempts) {
9665
- try {
9666
- const result = await client.session.create(attempt);
9667
- const sessionId = extractOpenCodeSessionId(result);
9668
- if (sessionId) return sessionId;
9669
- firstError ??= new Error("OpenCode session create response did not include a session id.");
9670
- } catch (error) {
9671
- firstError ??= error;
9672
- }
9660
+ try {
9661
+ const result = await client.session.create({ query: { directory }, body });
9662
+ const sessionId = extractOpenCodeSessionId(result);
9663
+ if (sessionId) return sessionId;
9664
+ throw new Error("OpenCode session create response did not include a session id.");
9665
+ } catch (error) {
9666
+ throw error || new Error("OpenCode session create failed.");
9673
9667
  }
9674
- throw firstError || new Error("OpenCode session create failed.");
9675
9668
  }
9676
9669
  async function waitForOpenCodeReadiness(client, { worktree = process.cwd(), attempts = 10, delayMs = 500, now = () => /* @__PURE__ */ new Date() } = {}) {
9677
9670
  if (typeof client?.session?.create !== "function") return { ok: true, skipped: true, reason: "session_create_probe_unavailable" };
@@ -9785,8 +9778,8 @@ function tagOpenCodePromptResult(result, deliveryMethod) {
9785
9778
  return result;
9786
9779
  }
9787
9780
  async function callOpenCodePromptWithFallbacks(method, sessionId, flatPayload, structuredPayload, deliveryMethod) {
9788
- const attempts = [
9789
- { ...structuredPayload, path: { id: sessionId } },
9781
+ const attempts = deliveryMethod === "prompt" ? [structuredPayload] : [
9782
+ structuredPayload,
9790
9783
  { ...structuredPayload, path: { sessionID: sessionId } },
9791
9784
  { sessionID: sessionId, ...flatPayload },
9792
9785
  { id: sessionId, ...flatPayload }
@@ -9836,23 +9829,11 @@ function normalizeOpenCodeSessionMessages(result) {
9836
9829
  if (Array.isArray(data?.items)) return [...data.items];
9837
9830
  return [];
9838
9831
  }
9839
- async function readOpenCodeSessionMessages(client, { sessionId, limit = 20 } = {}) {
9832
+ async function readOpenCodeSessionMessages(client, { sessionId, directory, limit = 20 } = {}) {
9840
9833
  if (typeof client?.session?.messages !== "function") return null;
9841
- const attempts = [
9842
- { path: { id: sessionId }, query: { limit } },
9843
- { path: { sessionID: sessionId }, query: { limit } },
9844
- { sessionID: sessionId, limit },
9845
- { id: sessionId, limit }
9846
- ];
9847
- let firstError = null;
9848
- for (const attempt of attempts) {
9849
- try {
9850
- return normalizeOpenCodeSessionMessages(await client.session.messages(attempt));
9851
- } catch (error) {
9852
- firstError ??= error;
9853
- }
9854
- }
9855
- throw firstError;
9834
+ const query = { limit };
9835
+ if (directory) query.directory = directory;
9836
+ return normalizeOpenCodeSessionMessages(await client.session.messages({ path: { id: sessionId }, query }));
9856
9837
  }
9857
9838
  function openCodeMessageText(message) {
9858
9839
  const parts = [message?.text, message?.content, message?.message, message?.body?.text, message?.data?.text];
@@ -9865,11 +9846,11 @@ function openCodeMessageStableKey(message, index = 0) {
9865
9846
  if (id) return `id:${id}`;
9866
9847
  return `idx:${index}:text:${openCodeMessageText(message)}`;
9867
9848
  }
9868
- async function verifyOpenCodeSessionEventDelivery(client, { sessionId, beforeMessages = null, expectedText = "", markers = [], attempts = 8, delayMs = 250 } = {}) {
9849
+ async function verifyOpenCodeSessionEventDelivery(client, { sessionId, directory, beforeMessages = null, expectedText = "", markers = [], attempts = 8, delayMs = 250 } = {}) {
9869
9850
  let lastError = "message_verification_unavailable";
9870
9851
  for (let attempt = 0; attempt < Math.max(1, attempts); attempt += 1) {
9871
9852
  try {
9872
- const afterMessages = await readOpenCodeSessionMessages(client, { sessionId, limit: 50 });
9853
+ const afterMessages = await readOpenCodeSessionMessages(client, { sessionId, directory, limit: 50 });
9873
9854
  if (!afterMessages) return { ok: false, reason: "message_verification_unavailable" };
9874
9855
  const beforeCount = Array.isArray(beforeMessages) ? beforeMessages.length : null;
9875
9856
  const beforeKeys = Array.isArray(beforeMessages) ? new Set(beforeMessages.map(openCodeMessageStableKey)) : null;
@@ -9917,7 +9898,7 @@ function openCodeBlockingPromptVerification(result, sessionId) {
9917
9898
  return null;
9918
9899
  }
9919
9900
  async function deliverClickUpSessionEventWithVerification({ openCodeClient, sendSessionEvent, clickupClient, worktree, taskId, sessionId, agent, text, directory, opencodeBaseUrl, eventMarkers = [], verifySessionEventDelivery = verifyOpenCodeSessionEventDelivery, applyBlockerOnFailure = true } = {}) {
9920
- const beforeMessages = await readOpenCodeSessionMessages(openCodeClient, { sessionId, limit: 50 }).catch(() => null);
9901
+ const beforeMessages = await readOpenCodeSessionMessages(openCodeClient, { sessionId, directory, limit: 50 }).catch(() => null);
9921
9902
  const sendResult = await sendSessionEvent(openCodeClient, { sessionId, agent, text, directory, opencodeBaseUrl, allowDirectFallback: false });
9922
9903
  let blockingPromptVerification = null;
9923
9904
  let admissionVerification = null;
@@ -9931,7 +9912,7 @@ async function deliverClickUpSessionEventWithVerification({ openCodeClient, send
9931
9912
  return { ok: false, action: "message_delivery_failed", reason: error.message, taskId, sessionId, fallbackAttempted: false, blockerTag: blocker2 };
9932
9913
  }
9933
9914
  if (blockingPromptVerification) return { ok: true, verification: blockingPromptVerification, admissionVerification: null, fallback: false };
9934
- let verification = await verifySessionEventDelivery(openCodeClient, { sessionId, beforeMessages, expectedText: text, markers: eventMarkers });
9915
+ let verification = await verifySessionEventDelivery(openCodeClient, { sessionId, directory, beforeMessages, expectedText: text, markers: eventMarkers });
9935
9916
  if (verification?.ok) return { ok: true, verification, admissionVerification, fallback: false };
9936
9917
  if (admissionVerification) {
9937
9918
  appendClickUpWebhookLocalLog(worktree, { type: "message_delivery_admitted_but_invisible", taskId, sessionId, admission: admissionVerification, reason: verification?.reason || "message_not_visible" });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@defend-tech/opencode-optima",
3
- "version": "0.1.52",
3
+ "version": "0.1.53",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+ssh://git@github.com/defend-tech/opencode-optima.git"