@alisio/plugin-opencode 0.1.0-alpha.2 → 0.1.0-alpha.6
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 +2 -1
- package/dist/provider.js +69 -6
- package/dist/version.d.ts +8 -0
- package/dist/version.js +28 -0
- package/package.json +3 -3
package/dist/index.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { definePlugin } from "@alisio/sdk";
|
|
2
2
|
import { OpenCodeProvider } from "./provider.js";
|
|
3
|
+
import { loadVersion } from "./version.js";
|
|
3
4
|
const profileString = (request, key) => typeof request.profile[key] === "string" ? String(request.profile[key]) : "";
|
|
4
5
|
export function createOpenCodePlugin() {
|
|
5
6
|
return definePlugin({
|
|
@@ -7,7 +8,7 @@ export function createOpenCodePlugin() {
|
|
|
7
8
|
name: "OpenCode Console (Zen)",
|
|
8
9
|
description: "OpenCode Console gateway for Responses, Chat, and Messages models",
|
|
9
10
|
categories: ["model-provider"],
|
|
10
|
-
version:
|
|
11
|
+
version: loadVersion(import.meta.url),
|
|
11
12
|
apiVersion: 1,
|
|
12
13
|
setup(api) {
|
|
13
14
|
api.providers.register({
|
package/dist/provider.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { loadVersion } from "./version.js";
|
|
1
2
|
const protocolMap = (groups) => new Map(Object.entries(groups).flatMap(([protocol, models]) => (models ?? []).map((model) => [model, protocol])));
|
|
2
3
|
const GO_MODELS = protocolMap({
|
|
3
4
|
responses: [
|
|
@@ -133,6 +134,8 @@ export function classifyOpenCodeModel(id) {
|
|
|
133
134
|
}
|
|
134
135
|
const dataUrl = (a) => `data:${a.mimeType};base64,${a.data}`;
|
|
135
136
|
const modelId = (id) => id.replace(/^opencode(?:-go)?\//, "");
|
|
137
|
+
/** Default user agent derives from the package manifest at runtime; never a hardcoded version. */
|
|
138
|
+
const DEFAULT_USER_AGENT = `alisio/${loadVersion(import.meta.url)}`;
|
|
136
139
|
async function* sse(response) {
|
|
137
140
|
if (!response.ok)
|
|
138
141
|
throw new Error(`OpenCode request failed: HTTP ${response.status}`);
|
|
@@ -219,7 +222,7 @@ export class OpenCodeGatewayProvider {
|
|
|
219
222
|
this.displayName = config.product === "opencode-go" ? "OpenCode Go" : "OpenCode Console";
|
|
220
223
|
this.classify = config.classify;
|
|
221
224
|
this.baseURL = config.baseURL.replace(/\/$/, "");
|
|
222
|
-
this.userAgent = config.userAgent ??
|
|
225
|
+
this.userAgent = config.userAgent ?? DEFAULT_USER_AGENT;
|
|
223
226
|
const url = new URL(this.baseURL);
|
|
224
227
|
if (url.protocol !== "https:" && url.hostname !== "127.0.0.1" && url.hostname !== "localhost")
|
|
225
228
|
throw new Error("OpenCode base URL must use HTTPS");
|
|
@@ -341,12 +344,23 @@ export class OpenCodeGatewayProvider {
|
|
|
341
344
|
if (choice.finish_reason)
|
|
342
345
|
finish = choice.finish_reason;
|
|
343
346
|
}
|
|
344
|
-
if (finish !== "stop" && finish !== "tool_calls")
|
|
347
|
+
if (finish !== "stop" && finish !== "tool_calls" && finish !== "length")
|
|
345
348
|
throw new Error(`OpenCode chat response incomplete: ${finish ?? "stream ended"}`);
|
|
346
349
|
const completed = [...calls.entries()].sort(([a], [b]) => a - b).map(([, call]) => call);
|
|
347
350
|
if (completed.some((call) => !call.id || !call.name))
|
|
348
351
|
throw new Error("Incomplete tool call");
|
|
349
|
-
|
|
352
|
+
if (finish === "length" && !text.trim())
|
|
353
|
+
throw new Error("OpenCode chat response cut off by max output tokens before any usable content; raise limits.maxOutputTokens");
|
|
354
|
+
yield {
|
|
355
|
+
type: "completed",
|
|
356
|
+
message: {
|
|
357
|
+
role: "assistant",
|
|
358
|
+
text,
|
|
359
|
+
calls: completed,
|
|
360
|
+
...(finish === "length" ? { truncated: true } : {}),
|
|
361
|
+
},
|
|
362
|
+
usage,
|
|
363
|
+
};
|
|
350
364
|
}
|
|
351
365
|
responsesInput(messages) {
|
|
352
366
|
const input = [];
|
|
@@ -397,8 +411,39 @@ export class OpenCodeGatewayProvider {
|
|
|
397
411
|
yield { type: "text_delta", delta: event.delta };
|
|
398
412
|
if (event.type === "response.reasoning_summary_text.delta")
|
|
399
413
|
yield { type: "reasoning_delta", delta: event.delta };
|
|
400
|
-
if (["response.failed", "
|
|
414
|
+
if (["response.failed", "error"].includes(event.type))
|
|
401
415
|
throw new Error(`OpenCode Responses request failed: ${event.type}`);
|
|
416
|
+
if (event.type === "response.incomplete") {
|
|
417
|
+
complete = true;
|
|
418
|
+
const output = event.response.output;
|
|
419
|
+
const calls = output
|
|
420
|
+
.filter((item) => item.type === "function_call")
|
|
421
|
+
.map((item) => ({ id: item.call_id, name: item.name, arguments: item.arguments }));
|
|
422
|
+
const text = output
|
|
423
|
+
.filter((item) => item.type === "message")
|
|
424
|
+
.flatMap((item) => item.content ?? [])
|
|
425
|
+
.filter((item) => item.type === "output_text")
|
|
426
|
+
.map((item) => item.text)
|
|
427
|
+
.join("");
|
|
428
|
+
if (!text.trim())
|
|
429
|
+
throw new Error("OpenCode Responses response cut off by max output tokens before any usable content; raise limits.maxOutputTokens");
|
|
430
|
+
if (calls.some((call) => !call.id || !call.name))
|
|
431
|
+
throw new Error("Incomplete tool call");
|
|
432
|
+
const u = event.response.usage;
|
|
433
|
+
yield {
|
|
434
|
+
type: "completed",
|
|
435
|
+
message: { role: "assistant", text, calls, providerData: output, truncated: true },
|
|
436
|
+
usage: u
|
|
437
|
+
? {
|
|
438
|
+
input: u.input_tokens ?? 0,
|
|
439
|
+
output: u.output_tokens ?? 0,
|
|
440
|
+
...(typeof u.input_tokens_details?.cached_tokens === "number"
|
|
441
|
+
? { cachedInput: u.input_tokens_details.cached_tokens }
|
|
442
|
+
: {}),
|
|
443
|
+
}
|
|
444
|
+
: undefined,
|
|
445
|
+
};
|
|
446
|
+
}
|
|
402
447
|
if (event.type === "response.completed") {
|
|
403
448
|
complete = true;
|
|
404
449
|
const output = event.response.output;
|
|
@@ -541,7 +586,8 @@ export class OpenCodeGatewayProvider {
|
|
|
541
586
|
stopReason = event.delta?.stop_reason ?? stopReason;
|
|
542
587
|
}
|
|
543
588
|
}
|
|
544
|
-
|
|
589
|
+
const truncated = stopReason === "max_tokens";
|
|
590
|
+
if (!truncated && stopReason !== "end_turn" && stopReason !== "tool_use")
|
|
545
591
|
throw new Error(`OpenCode Messages response incomplete: ${stopReason ?? "stream ended"}`);
|
|
546
592
|
const calls = [];
|
|
547
593
|
for (let index = 0; index < blocks.length; index++) {
|
|
@@ -550,7 +596,24 @@ export class OpenCodeGatewayProvider {
|
|
|
550
596
|
continue;
|
|
551
597
|
const args = argumentParts.get(index) || JSON.stringify(block.input ?? {});
|
|
552
598
|
calls.push({ id: block.id, name: block.name, arguments: args });
|
|
553
|
-
|
|
599
|
+
try {
|
|
600
|
+
block.input = JSON.parse(args || "{}");
|
|
601
|
+
}
|
|
602
|
+
catch {
|
|
603
|
+
if (!truncated)
|
|
604
|
+
throw new Error("Incomplete tool call");
|
|
605
|
+
throw new Error(`OpenCode Messages response incomplete: tool call arguments cut off for ${block.name}`);
|
|
606
|
+
}
|
|
607
|
+
}
|
|
608
|
+
if (truncated) {
|
|
609
|
+
if (!text.trim())
|
|
610
|
+
throw new Error("OpenCode Messages response cut off by max output tokens before any usable content; raise limits.maxOutputTokens");
|
|
611
|
+
yield {
|
|
612
|
+
type: "completed",
|
|
613
|
+
message: { role: "assistant", text, calls, providerData: blocks, truncated: true },
|
|
614
|
+
usage: { input, output },
|
|
615
|
+
};
|
|
616
|
+
return;
|
|
554
617
|
}
|
|
555
618
|
yield {
|
|
556
619
|
type: "completed",
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Reads this plugin package's version at runtime so plugin metadata, user-agent defaults and CLI
|
|
3
|
+
* reports stay in sync with the published package without a build-time constant to update.
|
|
4
|
+
* The manifest is resolved relative to the module (`src/version.ts` → `../package.json` in the
|
|
5
|
+
* source tree; `dist/version.js` → `../package.json` of the installed package). Standalone
|
|
6
|
+
* binaries inject the version at build time via ALISIO_PACKAGE_VERSION (scripts/binary-build.ts).
|
|
7
|
+
*/
|
|
8
|
+
export declare function loadVersion(fromHere: string): string;
|
package/dist/version.js
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { readFileSync } from "node:fs";
|
|
2
|
+
import { dirname, join } from "node:path";
|
|
3
|
+
import { fileURLToPath } from "node:url";
|
|
4
|
+
/**
|
|
5
|
+
* Fallback used when neither the build-time injection nor the package manifest is readable
|
|
6
|
+
* (e.g. an unpackaged embed). Keep it a development marker, never a publish literal, so it
|
|
7
|
+
* cannot silently desync from the published package.
|
|
8
|
+
*/
|
|
9
|
+
const FALLBACK = "dev";
|
|
10
|
+
/**
|
|
11
|
+
* Reads this plugin package's version at runtime so plugin metadata, user-agent defaults and CLI
|
|
12
|
+
* reports stay in sync with the published package without a build-time constant to update.
|
|
13
|
+
* The manifest is resolved relative to the module (`src/version.ts` → `../package.json` in the
|
|
14
|
+
* source tree; `dist/version.js` → `../package.json` of the installed package). Standalone
|
|
15
|
+
* binaries inject the version at build time via ALISIO_PACKAGE_VERSION (scripts/binary-build.ts).
|
|
16
|
+
*/
|
|
17
|
+
export function loadVersion(fromHere) {
|
|
18
|
+
if (process.env.ALISIO_PACKAGE_VERSION)
|
|
19
|
+
return process.env.ALISIO_PACKAGE_VERSION;
|
|
20
|
+
try {
|
|
21
|
+
const manifest = join(dirname(fileURLToPath(fromHere)), "..", "package.json");
|
|
22
|
+
const parsed = JSON.parse(readFileSync(manifest, "utf8"));
|
|
23
|
+
return typeof parsed.version === "string" && parsed.version ? parsed.version : FALLBACK;
|
|
24
|
+
}
|
|
25
|
+
catch {
|
|
26
|
+
return FALLBACK;
|
|
27
|
+
}
|
|
28
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@alisio/plugin-opencode",
|
|
3
|
-
"version": "0.1.0-alpha.
|
|
3
|
+
"version": "0.1.0-alpha.6",
|
|
4
4
|
"description": "Built-in OpenCode Console (Zen) model provider for Alisio.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -21,10 +21,10 @@
|
|
|
21
21
|
"node": ">=22.16"
|
|
22
22
|
},
|
|
23
23
|
"peerDependencies": {
|
|
24
|
-
"@alisio/sdk": "^0.1.0-alpha.
|
|
24
|
+
"@alisio/sdk": "^0.1.0-alpha.5"
|
|
25
25
|
},
|
|
26
26
|
"devDependencies": {
|
|
27
|
-
"@alisio/sdk": "0.1.0-alpha.
|
|
27
|
+
"@alisio/sdk": "0.1.0-alpha.5"
|
|
28
28
|
},
|
|
29
29
|
"publishConfig": {
|
|
30
30
|
"access": "public",
|