@tangle-network/agent-provider-cli-bridge 0.2.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.
- package/LICENSE +21 -0
- package/README.md +13 -0
- package/dist/index.d.ts +21 -0
- package/dist/index.js +233 -0
- package/package.json +45 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Tangle Network
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
# @tangle-network/agent-provider-cli-bridge
|
|
2
|
+
|
|
3
|
+
Wraps a running `cli-bridge` server as an `AgentEnvironmentProvider`.
|
|
4
|
+
|
|
5
|
+
```ts
|
|
6
|
+
import { createCliBridgeProvider } from '@tangle-network/agent-provider-cli-bridge'
|
|
7
|
+
|
|
8
|
+
const provider = createCliBridgeProvider({
|
|
9
|
+
baseUrl: 'http://127.0.0.1:8787',
|
|
10
|
+
bearerToken: process.env.CLI_BRIDGE_TOKEN,
|
|
11
|
+
defaultModel: 'codex',
|
|
12
|
+
})
|
|
13
|
+
```
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { AgentEnvironmentCapabilities, AgentEnvironmentProvider } from "@tangle-network/agent-interface/environment-provider";
|
|
2
|
+
export interface CliBridgeProviderOptions {
|
|
3
|
+
baseUrl: string;
|
|
4
|
+
bearerToken?: string;
|
|
5
|
+
defaultModel?: string;
|
|
6
|
+
defaultMode?: "byob" | "hosted-safe" | "hosted-sandboxed";
|
|
7
|
+
defaultExecution?: {
|
|
8
|
+
kind: "host";
|
|
9
|
+
} | {
|
|
10
|
+
kind: "sandbox";
|
|
11
|
+
repoUrl?: string;
|
|
12
|
+
gitRef?: string;
|
|
13
|
+
capability?: string;
|
|
14
|
+
ttlSeconds?: number;
|
|
15
|
+
};
|
|
16
|
+
fetch?: typeof fetch;
|
|
17
|
+
name?: string;
|
|
18
|
+
capabilities?: AgentEnvironmentCapabilities;
|
|
19
|
+
}
|
|
20
|
+
export declare function createCliBridgeProvider(options: CliBridgeProviderOptions): AgentEnvironmentProvider;
|
|
21
|
+
export declare function defaultCliBridgeCapabilities(): AgentEnvironmentCapabilities;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,233 @@
|
|
|
1
|
+
export function createCliBridgeProvider(options) {
|
|
2
|
+
const fetchImpl = options.fetch ?? globalThis.fetch;
|
|
3
|
+
if (!fetchImpl)
|
|
4
|
+
throw new Error("createCliBridgeProvider requires fetch");
|
|
5
|
+
const name = options.name ?? "cli-bridge";
|
|
6
|
+
return {
|
|
7
|
+
name,
|
|
8
|
+
capabilities: () => options.capabilities ?? defaultCliBridgeCapabilities(),
|
|
9
|
+
async create(input) {
|
|
10
|
+
return {
|
|
11
|
+
id: input.idempotencyKey ?? input.name ?? crypto.randomUUID(),
|
|
12
|
+
provider: name,
|
|
13
|
+
...(input.name ? { name: input.name } : {}),
|
|
14
|
+
status: async () => "running",
|
|
15
|
+
stream: (turn) => streamCliBridgeTurn(fetchImpl, options, input, turn),
|
|
16
|
+
placement: async () => ({
|
|
17
|
+
kind: options.defaultExecution?.kind === "sandbox" ? "sandbox" : "local",
|
|
18
|
+
providerMetadata: { baseUrl: options.baseUrl },
|
|
19
|
+
}),
|
|
20
|
+
destroy: async () => { },
|
|
21
|
+
};
|
|
22
|
+
},
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
async function* streamCliBridgeTurn(fetchImpl, options, environmentInput, turn) {
|
|
26
|
+
const response = await fetchImpl(`${trimSlash(options.baseUrl)}/v1/chat/completions`, {
|
|
27
|
+
method: "POST",
|
|
28
|
+
headers: {
|
|
29
|
+
"content-type": "application/json",
|
|
30
|
+
accept: "text/event-stream",
|
|
31
|
+
...(options.bearerToken ? { authorization: `Bearer ${options.bearerToken}` } : {}),
|
|
32
|
+
...(turn.sessionId ? { "x-session-id": turn.sessionId } : {}),
|
|
33
|
+
},
|
|
34
|
+
body: JSON.stringify(toChatCompletionsBody(options, environmentInput, turn)),
|
|
35
|
+
signal: turn.signal ?? environmentInput.signal,
|
|
36
|
+
});
|
|
37
|
+
if (!response.ok) {
|
|
38
|
+
throw new Error(`cli-bridge ${response.status}: ${await response.text()}`);
|
|
39
|
+
}
|
|
40
|
+
if (!response.body)
|
|
41
|
+
throw new Error("cli-bridge response body is empty");
|
|
42
|
+
let text = "";
|
|
43
|
+
let usage;
|
|
44
|
+
for await (const event of parseSse(response.body)) {
|
|
45
|
+
if (event === "[DONE]")
|
|
46
|
+
break;
|
|
47
|
+
const parsed = safeJson(event);
|
|
48
|
+
if (!parsed)
|
|
49
|
+
continue;
|
|
50
|
+
if (parsed.error && typeof parsed.error === "object") {
|
|
51
|
+
const error = parsed.error;
|
|
52
|
+
const message = typeof error.message === "string" ? error.message : "cli-bridge error";
|
|
53
|
+
yield { type: "status", data: { status: "failed", error: message } };
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
const choice = Array.isArray(parsed.choices) ? parsed.choices[0] : undefined;
|
|
57
|
+
const delta = choice?.delta;
|
|
58
|
+
const chunk = delta && typeof delta.content === "string" ? delta.content : "";
|
|
59
|
+
const nextUsage = usageFromOpenAi(parsed.usage);
|
|
60
|
+
if (nextUsage) {
|
|
61
|
+
usage = mergeUsage(usage, nextUsage);
|
|
62
|
+
yield { type: "usage", data: { usage: nextUsage }, usage: nextUsage };
|
|
63
|
+
}
|
|
64
|
+
if (chunk) {
|
|
65
|
+
text += chunk;
|
|
66
|
+
yield {
|
|
67
|
+
type: "message.part.updated",
|
|
68
|
+
data: { delta: chunk },
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
if (choice?.finish_reason) {
|
|
72
|
+
yield {
|
|
73
|
+
type: "result",
|
|
74
|
+
data: {
|
|
75
|
+
finalText: text,
|
|
76
|
+
finishReason: choice.finish_reason,
|
|
77
|
+
status: choice.finish_reason === "error" ? "failed" : "completed",
|
|
78
|
+
},
|
|
79
|
+
...(usage ? { usage } : {}),
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
function toChatCompletionsBody(options, environmentInput, turn) {
|
|
85
|
+
const profile = inlineProfile(environmentInput.profile);
|
|
86
|
+
return {
|
|
87
|
+
model: turn.model ?? environmentInput.backend ?? options.defaultModel ?? "opencode",
|
|
88
|
+
messages: messagesFromTurn(turn, profile),
|
|
89
|
+
stream: true,
|
|
90
|
+
...(turn.sessionId ? { session_id: turn.sessionId } : {}),
|
|
91
|
+
...(options.defaultMode ? { mode: options.defaultMode } : {}),
|
|
92
|
+
...(profile ? { agent_profile: profile } : {}),
|
|
93
|
+
...(environmentInput.env ? { env: environmentInput.env } : {}),
|
|
94
|
+
...(environmentInput.workspace?.cwd ? { cwd: environmentInput.workspace.cwd } : {}),
|
|
95
|
+
...(executionFromInput(options, environmentInput) ? { execution: executionFromInput(options, environmentInput) } : {}),
|
|
96
|
+
metadata: {
|
|
97
|
+
...(environmentInput.metadata ?? {}),
|
|
98
|
+
...(turn.context ?? {}),
|
|
99
|
+
...(turn.providerOptions ?? {}),
|
|
100
|
+
},
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
function messagesFromTurn(turn, profile) {
|
|
104
|
+
const messages = [];
|
|
105
|
+
const systemPrompt = profile?.prompt?.systemPrompt;
|
|
106
|
+
if (systemPrompt)
|
|
107
|
+
messages.push({ role: "system", content: systemPrompt });
|
|
108
|
+
messages.push({ role: "user", content: contentFromTurn(turn) });
|
|
109
|
+
return messages;
|
|
110
|
+
}
|
|
111
|
+
function contentFromTurn(turn) {
|
|
112
|
+
if (turn.parts)
|
|
113
|
+
return turn.parts;
|
|
114
|
+
return turn.prompt ?? "";
|
|
115
|
+
}
|
|
116
|
+
function inlineProfile(profile) {
|
|
117
|
+
return typeof profile === "string" ? undefined : profile;
|
|
118
|
+
}
|
|
119
|
+
function executionFromInput(options, input) {
|
|
120
|
+
if (options.defaultExecution)
|
|
121
|
+
return options.defaultExecution;
|
|
122
|
+
if (!input.workspace?.repoUrl)
|
|
123
|
+
return undefined;
|
|
124
|
+
return {
|
|
125
|
+
kind: "sandbox",
|
|
126
|
+
repoUrl: input.workspace.repoUrl,
|
|
127
|
+
...(input.workspace.gitRef ? { gitRef: input.workspace.gitRef } : {}),
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
async function* parseSse(body) {
|
|
131
|
+
const reader = body.getReader();
|
|
132
|
+
const decoder = new TextDecoder();
|
|
133
|
+
let buffer = "";
|
|
134
|
+
try {
|
|
135
|
+
while (true) {
|
|
136
|
+
const { done, value } = await reader.read();
|
|
137
|
+
if (done)
|
|
138
|
+
break;
|
|
139
|
+
buffer += decoder.decode(value, { stream: true });
|
|
140
|
+
let boundary = buffer.indexOf("\n\n");
|
|
141
|
+
while (boundary !== -1) {
|
|
142
|
+
const frame = buffer.slice(0, boundary);
|
|
143
|
+
buffer = buffer.slice(boundary + 2);
|
|
144
|
+
const data = dataFromFrame(frame);
|
|
145
|
+
if (data !== undefined)
|
|
146
|
+
yield data;
|
|
147
|
+
boundary = buffer.indexOf("\n\n");
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
if (buffer) {
|
|
151
|
+
const data = dataFromFrame(buffer);
|
|
152
|
+
if (data !== undefined)
|
|
153
|
+
yield data;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
finally {
|
|
157
|
+
reader.releaseLock();
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
function dataFromFrame(frame) {
|
|
161
|
+
const lines = frame.split(/\r?\n/);
|
|
162
|
+
const data = lines
|
|
163
|
+
.filter((line) => line.startsWith("data:"))
|
|
164
|
+
.map((line) => line.slice("data:".length).trimStart())
|
|
165
|
+
.join("\n");
|
|
166
|
+
return data || undefined;
|
|
167
|
+
}
|
|
168
|
+
function safeJson(value) {
|
|
169
|
+
try {
|
|
170
|
+
const parsed = JSON.parse(value);
|
|
171
|
+
return parsed && typeof parsed === "object" ? parsed : null;
|
|
172
|
+
}
|
|
173
|
+
catch {
|
|
174
|
+
return null;
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
function usageFromOpenAi(value) {
|
|
178
|
+
if (!value || typeof value !== "object")
|
|
179
|
+
return undefined;
|
|
180
|
+
const record = value;
|
|
181
|
+
const inputTokens = number(record.prompt_tokens) ?? number(record.input_tokens);
|
|
182
|
+
const outputTokens = number(record.completion_tokens) ?? number(record.output_tokens);
|
|
183
|
+
if (inputTokens === undefined && outputTokens === undefined)
|
|
184
|
+
return undefined;
|
|
185
|
+
return { inputTokens: inputTokens ?? 0, outputTokens: outputTokens ?? 0 };
|
|
186
|
+
}
|
|
187
|
+
function mergeUsage(left, right) {
|
|
188
|
+
if (!left)
|
|
189
|
+
return right;
|
|
190
|
+
return {
|
|
191
|
+
inputTokens: left.inputTokens + right.inputTokens,
|
|
192
|
+
outputTokens: left.outputTokens + right.outputTokens,
|
|
193
|
+
...(left.cost !== undefined || right.cost !== undefined ? { cost: (left.cost ?? 0) + (right.cost ?? 0) } : {}),
|
|
194
|
+
};
|
|
195
|
+
}
|
|
196
|
+
function number(value) {
|
|
197
|
+
return typeof value === "number" && Number.isFinite(value) ? value : undefined;
|
|
198
|
+
}
|
|
199
|
+
function trimSlash(value) {
|
|
200
|
+
return value.replace(/\/+$/, "");
|
|
201
|
+
}
|
|
202
|
+
export function defaultCliBridgeCapabilities() {
|
|
203
|
+
return {
|
|
204
|
+
profile: {
|
|
205
|
+
namedProfiles: false,
|
|
206
|
+
systemPrompt: true,
|
|
207
|
+
instructions: true,
|
|
208
|
+
tools: true,
|
|
209
|
+
permissions: true,
|
|
210
|
+
mcp: true,
|
|
211
|
+
subagents: true,
|
|
212
|
+
resources: {
|
|
213
|
+
files: true,
|
|
214
|
+
instructions: true,
|
|
215
|
+
tools: true,
|
|
216
|
+
skills: true,
|
|
217
|
+
agents: true,
|
|
218
|
+
commands: true,
|
|
219
|
+
},
|
|
220
|
+
hooks: false,
|
|
221
|
+
modes: true,
|
|
222
|
+
runtimeUpdate: false,
|
|
223
|
+
validation: false,
|
|
224
|
+
},
|
|
225
|
+
streaming: { live: true, replay: false, detach: false, turnIdempotency: true },
|
|
226
|
+
sessions: { continue: true, list: false, messages: false },
|
|
227
|
+
workspace: { read: false, write: false, exec: false, git: false, upload: false, download: false },
|
|
228
|
+
branching: { checkpoint: false, fork: false },
|
|
229
|
+
placement: true,
|
|
230
|
+
usage: true,
|
|
231
|
+
confidential: false,
|
|
232
|
+
};
|
|
233
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@tangle-network/agent-provider-cli-bridge",
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "AgentEnvironmentProvider adapter for cli-bridge /v1/chat/completions",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"main": "./dist/index.js",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"import": "./dist/index.js",
|
|
12
|
+
"types": "./dist/index.d.ts",
|
|
13
|
+
"default": "./dist/index.js"
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
"repository": {
|
|
17
|
+
"type": "git",
|
|
18
|
+
"url": "git+https://github.com/tangle-network/agent-sdk.git",
|
|
19
|
+
"directory": "packages/agent-provider-cli-bridge"
|
|
20
|
+
},
|
|
21
|
+
"publishConfig": {
|
|
22
|
+
"access": "public",
|
|
23
|
+
"registry": "https://registry.npmjs.org"
|
|
24
|
+
},
|
|
25
|
+
"files": [
|
|
26
|
+
"dist/index.d.ts",
|
|
27
|
+
"dist/index.js",
|
|
28
|
+
"README.md",
|
|
29
|
+
"LICENSE"
|
|
30
|
+
],
|
|
31
|
+
"dependencies": {
|
|
32
|
+
"@tangle-network/agent-interface": "0.14.0"
|
|
33
|
+
},
|
|
34
|
+
"devDependencies": {
|
|
35
|
+
"@types/node": "25.6.0",
|
|
36
|
+
"typescript": "^6.0.3",
|
|
37
|
+
"vitest": "^4.1.5"
|
|
38
|
+
},
|
|
39
|
+
"scripts": {
|
|
40
|
+
"build": "tsc -p tsconfig.json",
|
|
41
|
+
"check-types": "tsc --noEmit",
|
|
42
|
+
"clean": "rm -rf dist",
|
|
43
|
+
"test": "vitest run"
|
|
44
|
+
}
|
|
45
|
+
}
|