nemo-fabric-adapters-opencode 0.0.0 → 0.4.0

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.
@@ -0,0 +1,27 @@
1
+ import type { AgentRunRequest, AgentRunResult, AgentUsage, RuntimeContext } from "nemo-fabric-adapter-contract";
2
+ import { type AdapterRuntime, type AdapterStartInput } from "nemo-fabric-adapters-common";
3
+ export interface OpenCodePromptOutcome {
4
+ text?: string;
5
+ errorMessage?: string;
6
+ patch?: string;
7
+ usage?: AgentUsage;
8
+ }
9
+ export interface OpenCodeSessionHandle {
10
+ id: string;
11
+ prompt(text: string): Promise<OpenCodePromptOutcome>;
12
+ stop(): Promise<void>;
13
+ }
14
+ export interface OpenCodeSessionFactory {
15
+ create(input: AdapterStartInput): Promise<OpenCodeSessionHandle>;
16
+ }
17
+ export declare class OpenCodeAdapterRuntime implements AdapterRuntime {
18
+ private readonly factory;
19
+ private session?;
20
+ private artifactCount;
21
+ constructor(factory: OpenCodeSessionFactory);
22
+ start(input: AdapterStartInput): Promise<void>;
23
+ private writePatch;
24
+ private invalidate;
25
+ invoke(request: AgentRunRequest, context: RuntimeContext): Promise<AgentRunResult>;
26
+ stop(): Promise<void>;
27
+ }
@@ -0,0 +1,106 @@
1
+ // SPDX-FileCopyrightText: Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+ // Fabric lifecycle state for one OpenCode session. The SDK boundary supplies
4
+ // the session implementation so lifecycle tests do not need an installed SDK.
5
+ import { createHash } from "node:crypto";
6
+ import { mkdir, writeFile } from "node:fs/promises";
7
+ import { join } from "node:path";
8
+ import { LifecycleError } from "nemo-fabric-adapters-common";
9
+ function failed(code, message, usage, artifacts) {
10
+ return {
11
+ status: "failed",
12
+ output: null,
13
+ error: { code, message, retryable: false },
14
+ ...(usage === undefined ? {} : { usage }),
15
+ ...(artifacts === undefined ? {} : { artifacts }),
16
+ };
17
+ }
18
+ function artifactSegment(identifier) {
19
+ return createHash("sha256").update(identifier).digest("hex").slice(0, 32);
20
+ }
21
+ export class OpenCodeAdapterRuntime {
22
+ factory;
23
+ session;
24
+ artifactCount = 0;
25
+ constructor(factory) {
26
+ this.factory = factory;
27
+ }
28
+ async start(input) {
29
+ if (this.session !== undefined) {
30
+ throw new LifecycleError("opencode_already_started", "OpenCode adapter runtime is already started");
31
+ }
32
+ this.session = await this.factory.create(input);
33
+ this.artifactCount = 0;
34
+ }
35
+ async writePatch(patch, context) {
36
+ const root = context.artifacts.root ?? context.environment.artifacts;
37
+ if (root === undefined || root === null || root.length === 0) {
38
+ return undefined;
39
+ }
40
+ const turn = this.artifactCount + 1;
41
+ const runtime = artifactSegment(context.runtime_id);
42
+ const invocation = artifactSegment(context.invocation_id);
43
+ const path = `opencode/${runtime}/${invocation}-turn-${turn}.patch`;
44
+ try {
45
+ await mkdir(join(root, "opencode", runtime), { recursive: true });
46
+ await writeFile(join(root, path), patch, { encoding: "utf8", flag: "wx" });
47
+ }
48
+ catch {
49
+ throw new LifecycleError("opencode_artifact_write_failed", "OpenCode could not write its session diff artifact");
50
+ }
51
+ this.artifactCount = turn;
52
+ return [{ kind: "patch", media_type: "text/x-diff", name: `opencode-diff-${turn}`, path }];
53
+ }
54
+ async invalidate(session) {
55
+ if (this.session !== session) {
56
+ return;
57
+ }
58
+ this.session = undefined;
59
+ try {
60
+ await session.stop();
61
+ }
62
+ catch {
63
+ // The runtime is already invalid and must not conceal the original failure.
64
+ }
65
+ }
66
+ async invoke(request, context) {
67
+ if (this.session === undefined) {
68
+ throw new LifecycleError("opencode_not_started", "OpenCode adapter runtime is not started");
69
+ }
70
+ if (typeof request.input !== "string") {
71
+ return failed("opencode_unsupported_input", "The OpenCode adapter accepts only plain-text input");
72
+ }
73
+ const session = this.session;
74
+ let outcome;
75
+ try {
76
+ outcome = await session.prompt(request.input);
77
+ }
78
+ catch (error) {
79
+ await this.invalidate(session);
80
+ if (error instanceof LifecycleError) {
81
+ throw error;
82
+ }
83
+ throw new LifecycleError("opencode_session_failed", "OpenCode session communication failed", { retryable: true });
84
+ }
85
+ const artifacts = outcome.patch === undefined ? undefined : await this.writePatch(outcome.patch, context);
86
+ if (outcome.errorMessage !== undefined) {
87
+ return failed("opencode_model_error", outcome.errorMessage, outcome.usage, artifacts);
88
+ }
89
+ if (outcome.text === undefined || outcome.text.length === 0) {
90
+ return failed("opencode_no_assistant_response", "OpenCode completed without a final assistant text response", outcome.usage, artifacts);
91
+ }
92
+ return {
93
+ status: "succeeded",
94
+ output: { response: outcome.text },
95
+ ...(outcome.usage === undefined ? {} : { usage: outcome.usage }),
96
+ ...(artifacts === undefined ? {} : { artifacts }),
97
+ };
98
+ }
99
+ async stop() {
100
+ const session = this.session;
101
+ this.session = undefined;
102
+ if (session !== undefined) {
103
+ await session.stop();
104
+ }
105
+ }
106
+ }
@@ -0,0 +1,59 @@
1
+ {
2
+ "contract_version": "fabric.adapter/v1alpha2",
3
+ "adapter_id": "nvidia.fabric.opencode",
4
+ "adapter_kind": "process",
5
+ "runner": {
6
+ "command": "bun",
7
+ "script": "dist/cli.js"
8
+ },
9
+ "requirements": {
10
+ "binaries": ["bun"]
11
+ },
12
+ "config": {
13
+ "accepts": [
14
+ "models",
15
+ "models.base_url",
16
+ "models.temperature",
17
+ "models.top_p",
18
+ "instructions.system",
19
+ "skills",
20
+ "mcp"
21
+ ],
22
+ "system_instruction_modes": ["replace"]
23
+ },
24
+ "model_schema": {
25
+ "type": "object",
26
+ "properties": {
27
+ "provider": {"type": "string", "minLength": 1},
28
+ "model": {"type": "string", "minLength": 1},
29
+ "api_key_env": {"type": "string", "minLength": 1, "pattern": "^[A-Za-z_][A-Za-z0-9_]*$"},
30
+ "base_url": {"type": ["string", "null"], "format": "uri", "pattern": "^https?://"},
31
+ "temperature": {"type": "number"},
32
+ "top_p": {"type": "number", "minimum": 0, "maximum": 1}
33
+ },
34
+ "required": ["provider", "model", "api_key_env"],
35
+ "additionalProperties": false,
36
+ "allOf": [
37
+ {
38
+ "if": {
39
+ "anyOf": [
40
+ {"required": ["temperature"]},
41
+ {"required": ["top_p"]}
42
+ ]
43
+ },
44
+ "then": {
45
+ "properties": {
46
+ "base_url": {"type": "string", "format": "uri", "pattern": "^https?://"}
47
+ },
48
+ "required": ["base_url"]
49
+ }
50
+ }
51
+ ]
52
+ },
53
+ "capabilities": {
54
+ "streaming": false,
55
+ "cancellation": false,
56
+ "updates": false,
57
+ "service": false
58
+ }
59
+ }
package/package.json CHANGED
@@ -1,17 +1,66 @@
1
1
  {
2
2
  "name": "nemo-fabric-adapters-opencode",
3
- "version": "0.0.0",
4
- "description": "Inert registry bootstrap for NVIDIA NeMo Fabric OpenCode adapter.",
5
- "main": "index.js",
3
+ "version": "0.4.0",
4
+ "description": "OpenCode v2 SDK harness adapter for NVIDIA NeMo Fabric.",
5
+ "license": "Apache-2.0",
6
+ "type": "module",
7
+ "sideEffects": false,
8
+ "exports": {
9
+ "./descriptor": "./opencode.fabric-adapter.json",
10
+ "./package.json": "./package.json"
11
+ },
12
+ "bin": {
13
+ "nemo-fabric-adapters-opencode": "dist/cli.js"
14
+ },
15
+ "files": [
16
+ "dist",
17
+ "opencode.fabric-adapter.json",
18
+ "README.md",
19
+ "LICENSE"
20
+ ],
6
21
  "scripts": {
7
- "test": "echo \"Error: no test specified\" && exit 1"
22
+ "build": "tsc -p tsconfig.build.json",
23
+ "clean": "rm -rf dist",
24
+ "pack:check": "npm run build && node ../scripts/check-package.mjs",
25
+ "prepack": "npm run build",
26
+ "test": "npm run clean && npm run build && node --test test/*.test.mjs"
27
+ },
28
+ "publishConfig": {
29
+ "access": "public",
30
+ "registry": "https://registry.npmjs.org"
8
31
  },
9
- "keywords": [],
10
- "author": "",
11
- "license": "Apache-2.0",
12
- "type": "commonjs",
13
32
  "repository": {
14
33
  "type": "git",
15
- "url": "git+https://github.com/NVIDIA/NeMo-Fabric.git"
34
+ "url": "git+https://github.com/NVIDIA/NeMo-Fabric.git",
35
+ "directory": "adapters/typescript/opencode"
36
+ },
37
+ "bugs": {
38
+ "url": "https://github.com/NVIDIA/NeMo-Fabric/issues"
39
+ },
40
+ "homepage": "https://github.com/NVIDIA/NeMo-Fabric/tree/main/adapters/typescript/opencode#readme",
41
+ "engines": {
42
+ "bun": ">=1.4.2"
43
+ },
44
+ "dependencies": {
45
+ "nemo-fabric-adapter-contract": "0.4.0",
46
+ "nemo-fabric-adapters-common": "0.4.0"
47
+ },
48
+ "peerDependencies": {
49
+ "@opencode/core": "2.0.3",
50
+ "@opencode/sdk": "2.0.3"
51
+ },
52
+ "peerDependenciesMeta": {
53
+ "@opencode/core": {
54
+ "optional": true
55
+ },
56
+ "@opencode/sdk": {
57
+ "optional": true
58
+ }
59
+ },
60
+ "devDependencies": {
61
+ "@opencode/core": "2.0.3",
62
+ "@opencode/sdk": "2.0.3",
63
+ "@types/node": "24.12.4",
64
+ "typescript": "5.9.3"
16
65
  }
17
66
  }