@obtoai/agent-bridge 0.1.0-beta.3 → 0.1.0-beta.4
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/package.json +3 -2
- package/src/bridge-http.js +8 -0
- package/src/capabilities.js +31 -0
- package/src/daemon.js +42 -3
- package/src/driver.js +6 -5
- package/src/opencode-driver.js +220 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@obtoai/agent-bridge",
|
|
3
|
-
"version": "0.1.0-beta.
|
|
3
|
+
"version": "0.1.0-beta.4",
|
|
4
4
|
"description": "Local consumer for the OBTO Agent Bridge. Receives bridge events over SSE and drives a coding agent (Claude Code or OpenAI Codex) on your machine.",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"author": "OBTO Inc.",
|
|
@@ -32,6 +32,7 @@
|
|
|
32
32
|
},
|
|
33
33
|
"dependencies": {
|
|
34
34
|
"@anthropic-ai/claude-agent-sdk": "^0.2.126",
|
|
35
|
-
"@openai/codex-sdk": "^0.130.0"
|
|
35
|
+
"@openai/codex-sdk": "^0.130.0",
|
|
36
|
+
"@opencode-ai/sdk": "^1.16.1"
|
|
36
37
|
}
|
|
37
38
|
}
|
package/src/bridge-http.js
CHANGED
|
@@ -78,10 +78,18 @@ const getMessages = (threadId, sinceCursor) => {
|
|
|
78
78
|
const postAgentActivity = (threadId, state) =>
|
|
79
79
|
postJson('/api/bridge/agent-activity', { threadId, state });
|
|
80
80
|
|
|
81
|
+
// Phase 2b — atomic first-touch claim. Called by the daemon when it sees a
|
|
82
|
+
// reply event for a thread whose `agentId` is null (unrouted). The bridge's
|
|
83
|
+
// claimThread does a conditional Mongo update — only one daemon wins.
|
|
84
|
+
// Returns { ok, won, winner }: `won` is the only thing the caller acts on.
|
|
85
|
+
const claimThread = (threadId, agentId) =>
|
|
86
|
+
postJson('/api/bridge/thread/claim', { threadId, agentId });
|
|
87
|
+
|
|
81
88
|
module.exports = {
|
|
82
89
|
getCfg,
|
|
83
90
|
buildHeaders,
|
|
84
91
|
postMessage,
|
|
85
92
|
getMessages,
|
|
86
93
|
postAgentActivity,
|
|
94
|
+
claimThread,
|
|
87
95
|
};
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// Phase 2b — what this machine can drive. The Claude Agent SDK is a hard
|
|
4
|
+
// dependency of the daemon (declared in package.json), so `claude` is always
|
|
5
|
+
// available. `codex` and `opencode` need their respective CLIs on PATH —
|
|
6
|
+
// we probe with `which` (POSIX) or `where` (Windows).
|
|
7
|
+
//
|
|
8
|
+
// Sent to the bridge as `?capabilities=claude,codex,...` on SSE connect; the
|
|
9
|
+
// bridge records them in `agent_bridge_daemons` so the UI picker can offer
|
|
10
|
+
// only what's actually installable across the account's machines.
|
|
11
|
+
|
|
12
|
+
const { spawnSync } = require('child_process');
|
|
13
|
+
|
|
14
|
+
const onPath = (cmd) => {
|
|
15
|
+
try {
|
|
16
|
+
const tool = process.platform === 'win32' ? 'where' : 'which';
|
|
17
|
+
const r = spawnSync(tool, [cmd], { stdio: 'ignore' });
|
|
18
|
+
return r.status === 0;
|
|
19
|
+
} catch (_) {
|
|
20
|
+
return false;
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
const detect = () => {
|
|
25
|
+
const out = ['claude']; // bundled SDK; always advertised
|
|
26
|
+
if (onPath('codex')) out.push('codex');
|
|
27
|
+
if (onPath('opencode')) out.push('opencode');
|
|
28
|
+
return out;
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
module.exports = { detect, onPath };
|
package/src/daemon.js
CHANGED
|
@@ -4,7 +4,8 @@ const { loadConfig } = require('./config');
|
|
|
4
4
|
const { startStream } = require('./stream-client');
|
|
5
5
|
const { loadState, saveState, getAgentSession, setAgentSession } = require('./state');
|
|
6
6
|
const { drive, tryResolvePermission, agentFor } = require('./driver');
|
|
7
|
-
const { postAgentActivity } = require('./bridge-http');
|
|
7
|
+
const { postAgentActivity, claimThread } = require('./bridge-http');
|
|
8
|
+
const { detect: detectCapabilities } = require('./capabilities');
|
|
8
9
|
|
|
9
10
|
const log = (level, msg, data) => {
|
|
10
11
|
const line = { ts: new Date().toISOString(), level, msg };
|
|
@@ -67,6 +68,37 @@ const handleEvent = async (sseEvent) => {
|
|
|
67
68
|
return;
|
|
68
69
|
}
|
|
69
70
|
|
|
71
|
+
// Phase 2b — multi-daemon race check. The thread's target machine is
|
|
72
|
+
// included on the event when known (postReply publishes it). If it's set
|
|
73
|
+
// and isn't us, skip. If null, attempt the atomic first-touch claim — only
|
|
74
|
+
// the winning daemon handles the event; the rest skip cleanly.
|
|
75
|
+
const targetAgentId = payload.agentId ? String(payload.agentId).trim() : null;
|
|
76
|
+
if (targetAgentId && targetAgentId !== cfg.agentId) {
|
|
77
|
+
log('event', 'skip — claimed by other daemon', {
|
|
78
|
+
threadId,
|
|
79
|
+
targetAgentId,
|
|
80
|
+
messageId: payload.messageId,
|
|
81
|
+
});
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
if (!targetAgentId) {
|
|
85
|
+
try {
|
|
86
|
+
const r = await claimThread(threadId, cfg.agentId);
|
|
87
|
+
if (!r || !r.ok || !r.data || !r.data.won) {
|
|
88
|
+
log('info', 'claim lost or failed', {
|
|
89
|
+
threadId,
|
|
90
|
+
winner: r && r.data && r.data.winner,
|
|
91
|
+
status: r && r.status,
|
|
92
|
+
});
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
log('info', 'claim won', { threadId, agentId: cfg.agentId });
|
|
96
|
+
} catch (e) {
|
|
97
|
+
log('error', 'claim threw', { threadId, error: e && e.message });
|
|
98
|
+
return; // conservative — skip on uncertainty rather than double-drive
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
70
102
|
// v1.1 — which agent this thread is bound to (server-set, on the event).
|
|
71
103
|
const agent = agentFor(payload);
|
|
72
104
|
const session = getAgentSession(state, threadId, agent);
|
|
@@ -137,16 +169,23 @@ const handleEvent = async (sseEvent) => {
|
|
|
137
169
|
};
|
|
138
170
|
|
|
139
171
|
const start = () => {
|
|
172
|
+
// Phase 2b — advertise capabilities to the bridge on connect so the UI
|
|
173
|
+
// picker can offer just the agents that are actually installable here.
|
|
174
|
+
const capabilities = detectCapabilities();
|
|
175
|
+
|
|
140
176
|
log('info', 'starting daemon', {
|
|
141
177
|
baseUrl: cfg.baseUrl,
|
|
142
178
|
accountId: cfg.accountId,
|
|
143
179
|
agentId: cfg.agentId,
|
|
144
|
-
|
|
180
|
+
capabilities,
|
|
145
181
|
projectDir: cfg.projectDir,
|
|
146
182
|
boundThreads: Object.keys(state.bindings || {}),
|
|
147
183
|
});
|
|
148
184
|
|
|
149
|
-
const url = cfg.baseUrl.replace(/\/$/, '') +
|
|
185
|
+
const url = cfg.baseUrl.replace(/\/$/, '') +
|
|
186
|
+
'/api/bridge/stream' +
|
|
187
|
+
'?agentId=' + encodeURIComponent(cfg.agentId) +
|
|
188
|
+
'&capabilities=' + encodeURIComponent(capabilities.join(','));
|
|
150
189
|
stream = startStream({
|
|
151
190
|
url,
|
|
152
191
|
// Re-read config on every (re)connect so a rotated token (via
|
package/src/driver.js
CHANGED
|
@@ -13,15 +13,16 @@
|
|
|
13
13
|
|
|
14
14
|
const { loadConfig } = require('./config');
|
|
15
15
|
|
|
16
|
-
const KNOWN_AGENTS = ['claude', 'codex'];
|
|
16
|
+
const KNOWN_AGENTS = ['claude', 'codex', 'opencode'];
|
|
17
17
|
|
|
18
18
|
const cache = {};
|
|
19
19
|
|
|
20
20
|
const loadDriver = (name) => {
|
|
21
21
|
if (cache[name]) return cache[name];
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
22
|
+
let mod;
|
|
23
|
+
if (name === 'codex') mod = require('./codex-driver');
|
|
24
|
+
else if (name === 'opencode') mod = require('./opencode-driver');
|
|
25
|
+
else mod = require('./claude-driver');
|
|
25
26
|
cache[name] = mod;
|
|
26
27
|
return mod;
|
|
27
28
|
};
|
|
@@ -38,7 +39,7 @@ const getFallbackAgent = () => {
|
|
|
38
39
|
} catch (_) {
|
|
39
40
|
// config unreadable — default to claude
|
|
40
41
|
}
|
|
41
|
-
fallbackAgent = a
|
|
42
|
+
fallbackAgent = KNOWN_AGENTS.indexOf(a) !== -1 ? a : 'claude';
|
|
42
43
|
return fallbackAgent;
|
|
43
44
|
};
|
|
44
45
|
|
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// Opencode driver — drives an opencode session per bridge thread, the
|
|
4
|
+
// opencode counterpart of codex-driver.js. Selected when payload.agent ===
|
|
5
|
+
// 'opencode'. Same capture-model shape as Codex: opencode runs the turn, this
|
|
6
|
+
// driver posts the agent's final response to the bridge on its behalf.
|
|
7
|
+
//
|
|
8
|
+
// Why this is shaped like the Codex driver (and not Claude):
|
|
9
|
+
//
|
|
10
|
+
// • No bridge MCP tool exposed to opencode. Easiest path is the SDK's
|
|
11
|
+
// session.prompt() and concatenating returned text parts as the answer.
|
|
12
|
+
//
|
|
13
|
+
// • No fine-grained permission relay. opencode's SDK gives a single
|
|
14
|
+
// prompt-in / parts-out call per turn. tryResolvePermission() is a no-op.
|
|
15
|
+
//
|
|
16
|
+
// SDK-specific calls are isolated in runOpencode(), verified against
|
|
17
|
+
// @opencode-ai/sdk@^1.16 (Node SDK docs as of 2026-05-21).
|
|
18
|
+
|
|
19
|
+
const { loadConfig } = require('./config');
|
|
20
|
+
const { buildEnvelope } = require('./claude-driver');
|
|
21
|
+
const bridgeHttp = require('./bridge-http');
|
|
22
|
+
|
|
23
|
+
// Per-thread promise queue — concurrent replies on one thread are serialized
|
|
24
|
+
// so first-touch completes before any resume. Mirrors codex-driver.
|
|
25
|
+
const queues = new Map();
|
|
26
|
+
|
|
27
|
+
// Defaults can be overridden per-machine via env. Anthropic Claude is the
|
|
28
|
+
// default because users running opencode usually already have Claude auth.
|
|
29
|
+
const DEFAULT_PROVIDER = process.env.BRIDGE_OPENCODE_PROVIDER || 'anthropic';
|
|
30
|
+
const DEFAULT_MODEL = process.env.BRIDGE_OPENCODE_MODEL || 'claude-sonnet-4-5';
|
|
31
|
+
|
|
32
|
+
const buildOpencodePrompt = (payload, isFirst) => {
|
|
33
|
+
const head = buildEnvelope(payload);
|
|
34
|
+
if (!isFirst) return head;
|
|
35
|
+
return head +
|
|
36
|
+
'\n\n---\n' +
|
|
37
|
+
'You are an opencode session spawned by the OBTO Agent Bridge to handle ' +
|
|
38
|
+
'thread "' + payload.threadId + '". The human who sent the message above ' +
|
|
39
|
+
'is on the OBTO bridge web UI — they do NOT see your terminal, your tool ' +
|
|
40
|
+
'calls, or any intermediate output. They see ONLY your final response, ' +
|
|
41
|
+
'delivered to them verbatim.\n\n' +
|
|
42
|
+
'Therefore: do the requested work, then make your final response a ' +
|
|
43
|
+
'complete, self-contained answer addressed to that human. Markdown is ' +
|
|
44
|
+
'supported. If you need information you do not have, make your final ' +
|
|
45
|
+
'response a single clear question. Now handle the message above.';
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
// Best-effort extraction of the assistant's final text from an opencode
|
|
49
|
+
// prompt result. The SDK returns `{ parts: [...] }` or `{ data: { parts } }`
|
|
50
|
+
// depending on the call; we tolerate both and concatenate every text part.
|
|
51
|
+
const extractFinalResponse = (result) => {
|
|
52
|
+
if (!result) return '';
|
|
53
|
+
const parts = (result && result.parts) ||
|
|
54
|
+
(result && result.data && result.data.parts) ||
|
|
55
|
+
[];
|
|
56
|
+
if (!Array.isArray(parts)) return '';
|
|
57
|
+
return parts
|
|
58
|
+
.filter((p) => p && (p.type === 'text' || typeof p.text === 'string'))
|
|
59
|
+
.map((p) => String(p.text || ''))
|
|
60
|
+
.join('\n')
|
|
61
|
+
.trim();
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
// ── SDK boundary ──────────────────────────────────────────────────────────
|
|
65
|
+
// All @opencode-ai/sdk calls. The SDK spawns a local opencode HTTP server;
|
|
66
|
+
// we tear it down at the end of every turn (cheap, simple, no shared state).
|
|
67
|
+
const runOpencode = async ({ prompt, projectDir, resumeId }) => {
|
|
68
|
+
const { createOpencode } = await import('@opencode-ai/sdk');
|
|
69
|
+
const handle = await createOpencode({ directory: projectDir });
|
|
70
|
+
const client = handle.client;
|
|
71
|
+
const closeHandle = handle.close || (handle.server && handle.server.close);
|
|
72
|
+
|
|
73
|
+
try {
|
|
74
|
+
let sessionId = resumeId;
|
|
75
|
+
if (!sessionId) {
|
|
76
|
+
const created = await client.session.create({
|
|
77
|
+
body: { title: 'obto-bridge' },
|
|
78
|
+
});
|
|
79
|
+
sessionId = (created && created.id) ||
|
|
80
|
+
(created && created.data && created.data.id) ||
|
|
81
|
+
null;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
const result = await client.session.prompt({
|
|
85
|
+
path: { id: sessionId },
|
|
86
|
+
body: {
|
|
87
|
+
model: { providerID: DEFAULT_PROVIDER, modelID: DEFAULT_MODEL },
|
|
88
|
+
parts: [{ type: 'text', text: prompt }],
|
|
89
|
+
},
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
return {
|
|
93
|
+
sessionId: sessionId || (result && result.sessionId) || null,
|
|
94
|
+
finalResponse: extractFinalResponse(result),
|
|
95
|
+
};
|
|
96
|
+
} finally {
|
|
97
|
+
try { if (typeof closeHandle === 'function') await closeHandle(); } catch (_) {}
|
|
98
|
+
}
|
|
99
|
+
};
|
|
100
|
+
// ──────────────────────────────────────────────────────────────────────────
|
|
101
|
+
|
|
102
|
+
const postToBridge = async ({ threadId, body, kind, log }) => {
|
|
103
|
+
try {
|
|
104
|
+
const r = await bridgeHttp.postMessage({
|
|
105
|
+
threadId,
|
|
106
|
+
body,
|
|
107
|
+
kind: kind || 'result',
|
|
108
|
+
author: 'opencode-bridge',
|
|
109
|
+
role: 'agent',
|
|
110
|
+
});
|
|
111
|
+
if (!r.ok) {
|
|
112
|
+
log('error', 'opencode bridge post failed', { threadId, status: r.status });
|
|
113
|
+
}
|
|
114
|
+
return !!r.ok;
|
|
115
|
+
} catch (e) {
|
|
116
|
+
log('error', 'opencode bridge post threw', {
|
|
117
|
+
threadId,
|
|
118
|
+
error: e && e.message ? e.message : String(e),
|
|
119
|
+
});
|
|
120
|
+
return false;
|
|
121
|
+
}
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
const driveTurn = async ({ threadId, projectDir, resumeId, payload, log }) => {
|
|
125
|
+
const isFirst = !resumeId;
|
|
126
|
+
log('info', isFirst ? 'opencode first-touch spawn' : 'opencode resume', {
|
|
127
|
+
threadId,
|
|
128
|
+
projectDir,
|
|
129
|
+
resumeId: resumeId || undefined,
|
|
130
|
+
provider: DEFAULT_PROVIDER,
|
|
131
|
+
model: DEFAULT_MODEL,
|
|
132
|
+
messageId: payload.messageId,
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
const startedAt = Date.now();
|
|
136
|
+
let sessionId = resumeId || null;
|
|
137
|
+
let finalResponse = '';
|
|
138
|
+
let failure = null;
|
|
139
|
+
|
|
140
|
+
try {
|
|
141
|
+
const res = await runOpencode({
|
|
142
|
+
prompt: buildOpencodePrompt(payload, isFirst),
|
|
143
|
+
projectDir,
|
|
144
|
+
resumeId,
|
|
145
|
+
});
|
|
146
|
+
sessionId = res.sessionId || sessionId;
|
|
147
|
+
finalResponse = res.finalResponse;
|
|
148
|
+
} catch (e) {
|
|
149
|
+
failure = e && e.message ? e.message : String(e);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
// Capture model — the driver delivers opencode's output.
|
|
153
|
+
if (failure) {
|
|
154
|
+
await postToBridge({ threadId, kind: 'error', body: 'Opencode run failed: ' + failure, log });
|
|
155
|
+
} else if (finalResponse) {
|
|
156
|
+
await postToBridge({ threadId, kind: 'result', body: finalResponse, log });
|
|
157
|
+
} else {
|
|
158
|
+
await postToBridge({
|
|
159
|
+
threadId,
|
|
160
|
+
kind: 'error',
|
|
161
|
+
body: 'Opencode completed the turn but produced no final response.',
|
|
162
|
+
log,
|
|
163
|
+
});
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
log('info', isFirst ? 'opencode first-touch done' : 'opencode resume done', {
|
|
167
|
+
threadId,
|
|
168
|
+
sessionId,
|
|
169
|
+
ok: !failure && !!finalResponse,
|
|
170
|
+
assistantTextChars: finalResponse.length,
|
|
171
|
+
durationMs: Date.now() - startedAt,
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
if (failure && !sessionId) {
|
|
175
|
+
throw new Error('opencode run failed before a session id was assigned: ' + failure);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
// jsonlPath/lastJsonlMtimeMs are Claude-specific — null keeps the binding
|
|
179
|
+
// shape consistent for daemon.js / state.js.
|
|
180
|
+
return {
|
|
181
|
+
sessionId,
|
|
182
|
+
projectDir,
|
|
183
|
+
jsonlPath: null,
|
|
184
|
+
lastJsonlMtimeMs: null,
|
|
185
|
+
stopReason: failure ? 'error' : 'done',
|
|
186
|
+
assistantTextChars: finalResponse.length,
|
|
187
|
+
};
|
|
188
|
+
};
|
|
189
|
+
|
|
190
|
+
const drive = (params) => {
|
|
191
|
+
const key = params.threadId;
|
|
192
|
+
const prev = queues.get(key) || Promise.resolve();
|
|
193
|
+
const next = prev
|
|
194
|
+
.then(() => {
|
|
195
|
+
const binding = params.binding;
|
|
196
|
+
const resuming = binding && binding.sessionId;
|
|
197
|
+
return driveTurn({
|
|
198
|
+
threadId: params.threadId,
|
|
199
|
+
projectDir: resuming ? binding.projectDir : params.projectDir,
|
|
200
|
+
resumeId: resuming ? binding.sessionId : null,
|
|
201
|
+
payload: params.payload,
|
|
202
|
+
log: params.log,
|
|
203
|
+
});
|
|
204
|
+
})
|
|
205
|
+
.catch((err) => {
|
|
206
|
+
params.log('error', 'opencode drive failed', {
|
|
207
|
+
threadId: params.threadId,
|
|
208
|
+
error: err && err.message ? err.message : String(err),
|
|
209
|
+
});
|
|
210
|
+
throw err;
|
|
211
|
+
});
|
|
212
|
+
queues.set(key, next);
|
|
213
|
+
return next;
|
|
214
|
+
};
|
|
215
|
+
|
|
216
|
+
// Opencode has no per-tool permission callback exposed by the SDK — there is
|
|
217
|
+
// nothing to relay, same shape as the Codex driver.
|
|
218
|
+
const tryResolvePermission = () => false;
|
|
219
|
+
|
|
220
|
+
module.exports = { drive, tryResolvePermission };
|