@defend-tech/opencode-optima 0.1.42 → 0.1.43
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 +54 -21
- package/dist/sanitize_cli.js +54 -21
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -9592,31 +9592,64 @@ function assertOpenCodePromptAccepted(result) {
|
|
|
9592
9592
|
}
|
|
9593
9593
|
return result;
|
|
9594
9594
|
}
|
|
9595
|
-
|
|
9595
|
+
function responseLooksLikeHtml(contentType = "", raw = "") {
|
|
9596
|
+
return String(contentType || "").toLowerCase().includes("text/html") || /^\s*<!doctype\s+html\b/i.test(raw) || /^\s*<html\b/i.test(raw);
|
|
9597
|
+
}
|
|
9598
|
+
async function readOpenCodeJsonResponse(response, endpointName) {
|
|
9599
|
+
const contentType = response.headers?.get?.("content-type") || "";
|
|
9600
|
+
const raw = await response.text();
|
|
9601
|
+
if (responseLooksLikeHtml(contentType, raw)) {
|
|
9602
|
+
throw new Error(`OpenCode ${endpointName} returned the HTML app shell instead of an API response.`);
|
|
9603
|
+
}
|
|
9604
|
+
if (!raw) return null;
|
|
9605
|
+
try {
|
|
9606
|
+
return JSON.parse(raw);
|
|
9607
|
+
} catch (error) {
|
|
9608
|
+
if (response.ok) throw new Error(`OpenCode ${endpointName} response was not JSON: ${error.message}`);
|
|
9609
|
+
return { raw };
|
|
9610
|
+
}
|
|
9611
|
+
}
|
|
9612
|
+
async function sendOpenCodeSessionEventDirect({ baseUrl, sessionId, text, agent, fetchImpl = globalThis.fetch } = {}) {
|
|
9596
9613
|
if (typeof fetchImpl !== "function") throw new Error("OpenCode direct prompt delivery requires fetch.");
|
|
9597
9614
|
const root = normalizeOpenCodeBaseUrl(baseUrl, "");
|
|
9598
9615
|
if (!root) throw new Error("OpenCode direct prompt delivery requires a base URL.");
|
|
9599
|
-
const
|
|
9600
|
-
const
|
|
9601
|
-
|
|
9602
|
-
|
|
9603
|
-
|
|
9604
|
-
|
|
9605
|
-
|
|
9606
|
-
|
|
9607
|
-
|
|
9608
|
-
|
|
9609
|
-
|
|
9610
|
-
}
|
|
9611
|
-
|
|
9616
|
+
const encodedSession = encodeURIComponent(sessionId);
|
|
9617
|
+
const attempts = [
|
|
9618
|
+
{
|
|
9619
|
+
name: "v2 prompt",
|
|
9620
|
+
url: `${root}/api/session/${encodedSession}/prompt`,
|
|
9621
|
+
body: { prompt: { text }, delivery: "queue", resume: true },
|
|
9622
|
+
accept: (response, data) => {
|
|
9623
|
+
if (!response.ok) return null;
|
|
9624
|
+
if (!data?.data?.id) throw new Error("OpenCode v2 prompt response did not include data.id.");
|
|
9625
|
+
return { ok: true, method: "http", endpoint: "/api/session/{sessionID}/prompt", status: response.status, data: data.data, response: data };
|
|
9626
|
+
}
|
|
9627
|
+
},
|
|
9628
|
+
{
|
|
9629
|
+
name: "legacy async prompt",
|
|
9630
|
+
url: `${root}/session/${encodedSession}/prompt_async`,
|
|
9631
|
+
body: { agent, parts: [{ type: "text", text }] },
|
|
9632
|
+
accept: (response, data) => {
|
|
9633
|
+
if (response.status !== 204 && !response.ok) return null;
|
|
9634
|
+
return { ok: true, method: "http", endpoint: "/session/{sessionID}/prompt_async", status: response.status, data: data?.data || null, response: data };
|
|
9635
|
+
}
|
|
9612
9636
|
}
|
|
9637
|
+
];
|
|
9638
|
+
let firstError = null;
|
|
9639
|
+
for (const attempt of attempts) {
|
|
9640
|
+
const response = await fetchImpl(attempt.url, {
|
|
9641
|
+
method: "POST",
|
|
9642
|
+
headers: { "content-type": "application/json" },
|
|
9643
|
+
body: JSON.stringify(attempt.body)
|
|
9644
|
+
});
|
|
9645
|
+
const data = await readOpenCodeJsonResponse(response, attempt.name);
|
|
9646
|
+
const accepted = attempt.accept(response, data);
|
|
9647
|
+
if (accepted) return accepted;
|
|
9648
|
+
const message = data?.error?.message || data?.message || data?.raw || `OpenCode ${attempt.name} request failed with status ${response.status}.`;
|
|
9649
|
+
firstError ??= new Error(message);
|
|
9650
|
+
if (![404, 405].includes(Number(response.status))) break;
|
|
9613
9651
|
}
|
|
9614
|
-
|
|
9615
|
-
const message = data?.error?.message || data?.message || raw || `OpenCode prompt request failed with status ${response.status}.`;
|
|
9616
|
-
throw new Error(message);
|
|
9617
|
-
}
|
|
9618
|
-
if (!data?.data?.id) throw new Error("OpenCode prompt response did not include data.id.");
|
|
9619
|
-
return { ok: true, method: "http", status: response.status, data: data.data, response: data };
|
|
9652
|
+
throw firstError || new Error("OpenCode direct prompt delivery failed.");
|
|
9620
9653
|
}
|
|
9621
9654
|
async function sendOpenCodeSessionEvent(client, { sessionId, agent, text, directory, opencodeBaseUrl, baseUrl, fetchImpl, direct = false } = {}) {
|
|
9622
9655
|
const directBaseUrl = opencodeBaseUrl || baseUrl;
|
|
@@ -9632,7 +9665,7 @@ async function sendOpenCodeSessionEvent(client, { sessionId, agent, text, direct
|
|
|
9632
9665
|
if (!direct && typeof client?.session?.prompt === "function") {
|
|
9633
9666
|
return assertOpenCodePromptAccepted(await client.session.prompt(structuredPayload));
|
|
9634
9667
|
}
|
|
9635
|
-
if (directBaseUrl) return sendOpenCodeSessionEventDirect({ baseUrl: directBaseUrl, sessionId, text, fetchImpl });
|
|
9668
|
+
if (directBaseUrl) return sendOpenCodeSessionEventDirect({ baseUrl: directBaseUrl, sessionId, text, agent, fetchImpl });
|
|
9636
9669
|
throw new Error("OpenCode client does not expose session.prompt or session.promptAsync.");
|
|
9637
9670
|
}
|
|
9638
9671
|
function normalizeOpenCodeSessionMessages(result) {
|
package/dist/sanitize_cli.js
CHANGED
|
@@ -9599,31 +9599,64 @@ function assertOpenCodePromptAccepted(result) {
|
|
|
9599
9599
|
}
|
|
9600
9600
|
return result;
|
|
9601
9601
|
}
|
|
9602
|
-
|
|
9602
|
+
function responseLooksLikeHtml(contentType = "", raw = "") {
|
|
9603
|
+
return String(contentType || "").toLowerCase().includes("text/html") || /^\s*<!doctype\s+html\b/i.test(raw) || /^\s*<html\b/i.test(raw);
|
|
9604
|
+
}
|
|
9605
|
+
async function readOpenCodeJsonResponse(response, endpointName) {
|
|
9606
|
+
const contentType = response.headers?.get?.("content-type") || "";
|
|
9607
|
+
const raw = await response.text();
|
|
9608
|
+
if (responseLooksLikeHtml(contentType, raw)) {
|
|
9609
|
+
throw new Error(`OpenCode ${endpointName} returned the HTML app shell instead of an API response.`);
|
|
9610
|
+
}
|
|
9611
|
+
if (!raw) return null;
|
|
9612
|
+
try {
|
|
9613
|
+
return JSON.parse(raw);
|
|
9614
|
+
} catch (error) {
|
|
9615
|
+
if (response.ok) throw new Error(`OpenCode ${endpointName} response was not JSON: ${error.message}`);
|
|
9616
|
+
return { raw };
|
|
9617
|
+
}
|
|
9618
|
+
}
|
|
9619
|
+
async function sendOpenCodeSessionEventDirect({ baseUrl, sessionId, text, agent, fetchImpl = globalThis.fetch } = {}) {
|
|
9603
9620
|
if (typeof fetchImpl !== "function") throw new Error("OpenCode direct prompt delivery requires fetch.");
|
|
9604
9621
|
const root = normalizeOpenCodeBaseUrl(baseUrl, "");
|
|
9605
9622
|
if (!root) throw new Error("OpenCode direct prompt delivery requires a base URL.");
|
|
9606
|
-
const
|
|
9607
|
-
const
|
|
9608
|
-
|
|
9609
|
-
|
|
9610
|
-
|
|
9611
|
-
|
|
9612
|
-
|
|
9613
|
-
|
|
9614
|
-
|
|
9615
|
-
|
|
9616
|
-
|
|
9617
|
-
}
|
|
9618
|
-
|
|
9623
|
+
const encodedSession = encodeURIComponent(sessionId);
|
|
9624
|
+
const attempts = [
|
|
9625
|
+
{
|
|
9626
|
+
name: "v2 prompt",
|
|
9627
|
+
url: `${root}/api/session/${encodedSession}/prompt`,
|
|
9628
|
+
body: { prompt: { text }, delivery: "queue", resume: true },
|
|
9629
|
+
accept: (response, data) => {
|
|
9630
|
+
if (!response.ok) return null;
|
|
9631
|
+
if (!data?.data?.id) throw new Error("OpenCode v2 prompt response did not include data.id.");
|
|
9632
|
+
return { ok: true, method: "http", endpoint: "/api/session/{sessionID}/prompt", status: response.status, data: data.data, response: data };
|
|
9633
|
+
}
|
|
9634
|
+
},
|
|
9635
|
+
{
|
|
9636
|
+
name: "legacy async prompt",
|
|
9637
|
+
url: `${root}/session/${encodedSession}/prompt_async`,
|
|
9638
|
+
body: { agent, parts: [{ type: "text", text }] },
|
|
9639
|
+
accept: (response, data) => {
|
|
9640
|
+
if (response.status !== 204 && !response.ok) return null;
|
|
9641
|
+
return { ok: true, method: "http", endpoint: "/session/{sessionID}/prompt_async", status: response.status, data: data?.data || null, response: data };
|
|
9642
|
+
}
|
|
9619
9643
|
}
|
|
9644
|
+
];
|
|
9645
|
+
let firstError = null;
|
|
9646
|
+
for (const attempt of attempts) {
|
|
9647
|
+
const response = await fetchImpl(attempt.url, {
|
|
9648
|
+
method: "POST",
|
|
9649
|
+
headers: { "content-type": "application/json" },
|
|
9650
|
+
body: JSON.stringify(attempt.body)
|
|
9651
|
+
});
|
|
9652
|
+
const data = await readOpenCodeJsonResponse(response, attempt.name);
|
|
9653
|
+
const accepted = attempt.accept(response, data);
|
|
9654
|
+
if (accepted) return accepted;
|
|
9655
|
+
const message = data?.error?.message || data?.message || data?.raw || `OpenCode ${attempt.name} request failed with status ${response.status}.`;
|
|
9656
|
+
firstError ??= new Error(message);
|
|
9657
|
+
if (![404, 405].includes(Number(response.status))) break;
|
|
9620
9658
|
}
|
|
9621
|
-
|
|
9622
|
-
const message = data?.error?.message || data?.message || raw || `OpenCode prompt request failed with status ${response.status}.`;
|
|
9623
|
-
throw new Error(message);
|
|
9624
|
-
}
|
|
9625
|
-
if (!data?.data?.id) throw new Error("OpenCode prompt response did not include data.id.");
|
|
9626
|
-
return { ok: true, method: "http", status: response.status, data: data.data, response: data };
|
|
9659
|
+
throw firstError || new Error("OpenCode direct prompt delivery failed.");
|
|
9627
9660
|
}
|
|
9628
9661
|
async function sendOpenCodeSessionEvent(client, { sessionId, agent, text, directory, opencodeBaseUrl, baseUrl, fetchImpl, direct = false } = {}) {
|
|
9629
9662
|
const directBaseUrl = opencodeBaseUrl || baseUrl;
|
|
@@ -9639,7 +9672,7 @@ async function sendOpenCodeSessionEvent(client, { sessionId, agent, text, direct
|
|
|
9639
9672
|
if (!direct && typeof client?.session?.prompt === "function") {
|
|
9640
9673
|
return assertOpenCodePromptAccepted(await client.session.prompt(structuredPayload));
|
|
9641
9674
|
}
|
|
9642
|
-
if (directBaseUrl) return sendOpenCodeSessionEventDirect({ baseUrl: directBaseUrl, sessionId, text, fetchImpl });
|
|
9675
|
+
if (directBaseUrl) return sendOpenCodeSessionEventDirect({ baseUrl: directBaseUrl, sessionId, text, agent, fetchImpl });
|
|
9643
9676
|
throw new Error("OpenCode client does not expose session.prompt or session.promptAsync.");
|
|
9644
9677
|
}
|
|
9645
9678
|
function normalizeOpenCodeSessionMessages(result) {
|