@obtoai/agent-bridge 0.1.0-beta.22 → 0.1.0-beta.23

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@obtoai/agent-bridge",
3
- "version": "0.1.0-beta.22",
3
+ "version": "0.1.0-beta.23",
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.",
@@ -1,9 +1,14 @@
1
1
  'use strict';
2
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).
3
+ // Phase 2b — what this machine can drive.
4
+ //
5
+ // `claude` and `opencode` are bundled SDKs (declared in package.json) and
6
+ // self-contained: claude uses the Claude Agent SDK; opencode uses
7
+ // @opencode-ai/sdk's createOpencode() which spawns its own local HTTP server.
8
+ // Neither needs a CLI on PATH — they're always advertised.
9
+ //
10
+ // `codex` uses @openai/codex-sdk which delegates to the user's `codex` CLI
11
+ // for auth/config, so we still probe PATH for it.
7
12
  //
8
13
  // Sent to the bridge as `?capabilities=claude,codex,...` on SSE connect; the
9
14
  // bridge records them in `agent_bridge_daemons` so the UI picker can offer
@@ -22,9 +27,8 @@ const onPath = (cmd) => {
22
27
  };
23
28
 
24
29
  const detect = () => {
25
- const out = ['claude']; // bundled SDK; always advertised
30
+ const out = ['claude', 'opencode']; // bundled SDKs; always advertised
26
31
  if (onPath('codex')) out.push('codex');
27
- if (onPath('opencode')) out.push('opencode');
28
32
  return out;
29
33
  };
30
34
 
package/src/daemon.js CHANGED
@@ -7,6 +7,10 @@ const { drive, tryResolvePermission, agentFor } = require('./driver');
7
7
  const { postAgentActivity, claimThread, postExternalSync } = require('./bridge-http');
8
8
  const { detect: detectCapabilities } = require('./capabilities');
9
9
  const { scanAll: scanExternalSessions } = require('./external-scanner');
10
+ // Phase 6.5 — OpenCode desktop/CLI uses SQLite instead of JSONL; separate
11
+ // scanner reads ~/.local/share/opencode/opencode.db read-only via the
12
+ // sqlite3 CLI subprocess. Empty array when SQLite or the DB isn't present.
13
+ const { scanAll: scanOpencodeSessions } = require('./opencode-sqlite-scanner');
10
14
 
11
15
  const log = (level, msg, data) => {
12
16
  const line = { ts: new Date().toISOString(), level, msg };
@@ -295,7 +299,17 @@ const ownedSessionIdsFromState = () => {
295
299
  };
296
300
  const externalScanTick = async () => {
297
301
  try {
298
- const all = scanExternalSessions();
302
+ // Phase 6.5 — fold opencode SQLite sessions into the same external sync
303
+ // payload. Both scanners are best-effort and return [] on failure, so a
304
+ // dead SQLite CLI or missing DB never breaks the JSONL path.
305
+ const fromJsonl = scanExternalSessions();
306
+ let fromOpencode = [];
307
+ try {
308
+ fromOpencode = scanOpencodeSessions();
309
+ } catch (e) {
310
+ log('warn', 'opencode sqlite scan failed', { error: e && e.message ? e.message : String(e) });
311
+ }
312
+ const all = fromJsonl.concat(fromOpencode);
299
313
  const owned = ownedSessionIdsFromState();
300
314
  const external = all.filter((s) => s && s.sessionId && !owned.has(String(s.sessionId)));
301
315
  if (external.length === 0) return;
@@ -0,0 +1,178 @@
1
+ 'use strict';
2
+
3
+ // Phase 6.5 — surface OpenCode desktop/CLI conversations to the bridge.
4
+ //
5
+ // OpenCode stores sessions in SQLite at ~/.local/share/opencode/opencode.db
6
+ // (shared between the CLI and the Electron desktop app). Schema (relevant
7
+ // subset, captured 2026-06-07):
8
+ //
9
+ // session(id, project_id, parent_id, directory, title, time_created,
10
+ // time_updated, agent, model, ...)
11
+ // message(id, session_id, time_created, data JSON) -- data.role: user|assistant|...
12
+ // part(id, message_id, session_id, time_created, data JSON) -- data.type: text|reasoning|step-start|...
13
+ // project(id, worktree, name, ...)
14
+ //
15
+ // We read via the `sqlite3` CLI subprocess (ships on macOS, standard on
16
+ // Linux) rather than adding a native dependency (better-sqlite3) to the
17
+ // daemon's install footprint. Opens in `-readonly` mode so live writes from
18
+ // the desktop app are safe — SQLite WAL allows concurrent reads.
19
+
20
+ const fs = require('fs');
21
+ const os = require('os');
22
+ const path = require('path');
23
+ const { spawnSync, execFileSync } = require('child_process');
24
+
25
+ const OPENCODE_DB = path.join(os.homedir(), '.local', 'share', 'opencode', 'opencode.db');
26
+
27
+ // Match the limits the Claude/Codex scanners use so the bridge UI's
28
+ // preview/title rendering looks consistent across sources.
29
+ const SESSION_LIMIT = 500;
30
+ const RECENT_TURN_COUNT = 10;
31
+ const RECENT_MESSAGE_BODY_MAX = 4000;
32
+ const PREVIEW_MAX_CHARS = 240;
33
+ const QUERY_TIMEOUT_MS = 8000;
34
+
35
+ let sqliteAvailableCached = null;
36
+ const sqliteAvailable = () => {
37
+ if (sqliteAvailableCached !== null) return sqliteAvailableCached;
38
+ try {
39
+ const r = spawnSync('sqlite3', ['-version'], { encoding: 'utf8' });
40
+ sqliteAvailableCached = r.status === 0;
41
+ } catch (_) {
42
+ sqliteAvailableCached = false;
43
+ }
44
+ return sqliteAvailableCached;
45
+ };
46
+
47
+ const dbExists = () => {
48
+ try { return fs.existsSync(OPENCODE_DB); } catch (_) { return false; }
49
+ };
50
+
51
+ // Run a single SQL query against the OpenCode DB and parse `-json` output.
52
+ // Returns [] on any failure (missing CLI, locked DB beyond timeout, bad SQL).
53
+ // The daemon's external scan is fire-and-forget and runs every 30s, so we
54
+ // MUST NOT throw out — at worst we miss this tick.
55
+ const queryJson = (sql) => {
56
+ try {
57
+ const out = execFileSync('sqlite3', ['-readonly', '-json', OPENCODE_DB, sql], {
58
+ encoding: 'utf8',
59
+ timeout: QUERY_TIMEOUT_MS,
60
+ maxBuffer: 32 * 1024 * 1024,
61
+ stdio: ['ignore', 'pipe', 'ignore'],
62
+ });
63
+ if (!out || !out.trim()) return [];
64
+ return JSON.parse(out);
65
+ } catch (_) {
66
+ return [];
67
+ }
68
+ };
69
+
70
+ // Take a string and slice/trim to PREVIEW_MAX_CHARS for the sidebar preview.
71
+ const previewOf = (s) => {
72
+ const t = String(s || '').replace(/\s+/g, ' ').trim();
73
+ return t.length > PREVIEW_MAX_CHARS ? t.slice(0, PREVIEW_MAX_CHARS) : t;
74
+ };
75
+
76
+ // Trim a recentMessages body to the same RECENT_MESSAGE_BODY_MAX cap as the
77
+ // other scanners so the bridge's per-row payload stays bounded.
78
+ const bodyOf = (s) => {
79
+ const t = String(s || '');
80
+ return t.length > RECENT_MESSAGE_BODY_MAX ? t.slice(0, RECENT_MESSAGE_BODY_MAX) : t;
81
+ };
82
+
83
+ // Pull the last N text-bearing turns for one session. Skip control rows
84
+ // (step-start, reasoning, tool-use) so the preview matches what the human
85
+ // actually said and what the assistant actually replied.
86
+ const recentMessagesFor = (sessionId) => {
87
+ const safeId = String(sessionId).replace(/'/g, "''");
88
+ // Last N text parts in order. We take 2*N from the tail then sort because
89
+ // SQLite's LIMIT is fastest with DESC + ascending re-sort in JS.
90
+ const rows = queryJson(
91
+ "SELECT p.time_created AS ts, " +
92
+ "json_extract(m.data, '$.role') AS role, " +
93
+ "json_extract(p.data, '$.text') AS text " +
94
+ "FROM part p JOIN message m ON p.message_id = m.id " +
95
+ "WHERE p.session_id = '" + safeId + "' " +
96
+ "AND json_extract(p.data, '$.type') = 'text' " +
97
+ "ORDER BY p.time_created DESC LIMIT " + (RECENT_TURN_COUNT * 2),
98
+ );
99
+ rows.reverse();
100
+ // Coalesce consecutive same-role rows (assistant turn can be split across
101
+ // parts) and tail to N.
102
+ const coalesced = [];
103
+ for (const r of rows) {
104
+ if (!r || !r.text) continue;
105
+ const role = r.role === 'user' ? 'user' : 'assistant';
106
+ const last = coalesced[coalesced.length - 1];
107
+ if (last && last.role === role) {
108
+ last.text += '\n\n' + r.text;
109
+ last.ts = r.ts;
110
+ } else {
111
+ coalesced.push({ role, text: r.text, ts: r.ts });
112
+ }
113
+ }
114
+ const sliced = coalesced.slice(-RECENT_TURN_COUNT);
115
+ return sliced.map((m) => ({ role: m.role, body: bodyOf(m.text), ts: m.ts }));
116
+ };
117
+
118
+ const lastMessageFor = (sessionId) => {
119
+ const safeId = String(sessionId).replace(/'/g, "''");
120
+ const rows = queryJson(
121
+ "SELECT json_extract(m.data, '$.role') AS role, json_extract(p.data, '$.text') AS text " +
122
+ "FROM part p JOIN message m ON p.message_id = m.id " +
123
+ "WHERE p.session_id = '" + safeId + "' " +
124
+ "AND json_extract(p.data, '$.type') = 'text' " +
125
+ "ORDER BY p.time_created DESC LIMIT 1",
126
+ );
127
+ if (!rows.length) return null;
128
+ const r = rows[0];
129
+ return {
130
+ author: r.role === 'user' ? 'user' : 'assistant',
131
+ preview: previewOf(r.text),
132
+ };
133
+ };
134
+
135
+ // Public API. Returns the same shape that postExternalSync expects (same as
136
+ // claude/codex rows). Best-effort: returns [] if SQLite/DB unavailable.
137
+ const scanAll = () => {
138
+ if (!dbExists() || !sqliteAvailable()) return [];
139
+
140
+ // Top-level sessions only — skip sub-sessions (parent_id non-null), they
141
+ // belong to a parent and showing them as standalone rows would clutter
142
+ // the sidebar with duplicate-looking conversations.
143
+ const sessions = queryJson(
144
+ "SELECT s.id AS sessionId, s.title, s.directory, " +
145
+ "s.time_created AS createdMs, s.time_updated AS updatedMs, " +
146
+ "s.agent, s.model, " +
147
+ "p.name AS projectName, p.worktree AS projectWorktree " +
148
+ "FROM session s LEFT JOIN project p ON s.project_id = p.id " +
149
+ "WHERE (s.parent_id IS NULL OR s.parent_id = '') " +
150
+ "ORDER BY s.time_updated DESC LIMIT " + SESSION_LIMIT,
151
+ );
152
+
153
+ const out = [];
154
+ for (const s of sessions) {
155
+ if (!s || !s.sessionId) continue;
156
+ const dir = s.directory || s.projectWorktree || '';
157
+ const recentMessages = recentMessagesFor(s.sessionId);
158
+ const lastMsg = lastMessageFor(s.sessionId);
159
+ out.push({
160
+ source: 'opencode',
161
+ sessionId: String(s.sessionId),
162
+ // The bridge's adoption path uses projectDir for resume cwd. OpenCode
163
+ // stores the absolute path in `directory` (or project.worktree as
164
+ // backup); pass it through as both projectDir and projectName so the
165
+ // daemon's "looksAbsolute" guard in daemon.js handleEvent accepts it.
166
+ projectDir: dir,
167
+ projectName: dir,
168
+ title: String(s.title || '').trim() || null,
169
+ recentMessages: recentMessages,
170
+ lastActivityAt: typeof s.updatedMs === 'number' ? s.updatedMs : Number(s.updatedMs) || 0,
171
+ lastMessagePreview: lastMsg ? lastMsg.preview : '',
172
+ lastMessageAuthor: lastMsg ? lastMsg.author : null,
173
+ });
174
+ }
175
+ return out;
176
+ };
177
+
178
+ module.exports = { scanAll, OPENCODE_DB };