Skip to content

Commit 3f8aabe

Browse files
feat(opencode): let the model actually see attached images
`opencode run` cannot put an image in front of the model. Its read tool returns images inside a tool *result*, which OpenAI-compatible transports flatten to text, and `--file` labels every attachment text/plain (anomalyco/opencode#16723). Upstream does the same thing we did — hand the model a list of paths and hope — so images were silently ignored on every custom provider. Verified against 1.18.18 with neutrally-named files: the "right" answers we used to get were read off the filename, not the picture. OpenCode's HTTP server does accept an image as a part of the user message, so a turn carrying one now runs there and every other turn stays on the CLI path. The server is booted once, lazily, and shared. Three things about that server were not obvious and each one silently ate a turn until it was found: - "listening on ..." is not readiness. For a second or two the model catalog is empty, and a prompt sent into that window is accepted and then dropped with `ModelUnavailableError` in opencode's own log — no event, no message, a chat that hangs forever. Boot now waits for `/api/model`. - `/event` carries only server-level frames; session activity is published on `/api/event`, with the payload under `data` rather than the `properties` the OpenAPI document names. - The server records the session row but none of the conversation, so a turn left to it answers once and is gone by the next page load — and `opencode run --session` then continues it with no memory. The exchange is therefore written to opencode.db in the shape the CLI writes, which is what the sidebar, the transcript and a CLI resume all read. A session moves between the two transports message by message: attach an image, then send text, and the follow-up still knows what it was shown. The server is unauthenticated by default and exposes the filesystem and a shell on loopback, so it is given a per-boot password and bound to 127.0.0.1.
1 parent 31419df commit 3f8aabe

7 files changed

Lines changed: 1394 additions & 64 deletions

‎server/opencode-cli.js‎

Lines changed: 18 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1-
import fsSync from 'node:fs';
21
import { mkdir, mkdtemp, rm, unlink, writeFile } from 'node:fs/promises';
32
import os from 'node:os';
43
import path from 'node:path';
54

65
import crossSpawn from 'cross-spawn';
7-
import Database from 'better-sqlite3';
86

9-
import { appendImagesInputTag } from './shared/image-attachments.js';
7+
import { appendImagesInputTag, normalizeImageDescriptors } from './shared/image-attachments.js';
8+
import { readOpenCodeTokenUsage } from './shared/opencode-token-usage.js';
9+
import { runOpenCodeHttpTurn } from './services/opencode-http-runner.js';
1010
import { sessionsService } from './modules/providers/services/sessions.service.js';
1111
import { providerAuthService } from './modules/providers/services/provider-auth.service.js';
1212
import { providerModelsService } from './modules/providers/services/provider-models.service.js';
1313
import { notifyRunFailed, notifyRunStopped } from './services/notification-orchestrator.js';
1414
import { scheduleSessionRecap } from './services/session-recap.service.js';
1515
import { broadcastSessionUpdate } from './modules/providers/index.js';
16-
import { createCompleteMessage, createNormalizedMessage, flattenPromptForWindowsShell, getOpenCodeDatabasePath, getOpenCodeHelperWorkspace, isDatabaseLockedError, stripAnsi } from './shared/utils.js';
16+
import { createCompleteMessage, createNormalizedMessage, flattenPromptForWindowsShell, getOpenCodeHelperWorkspace, isDatabaseLockedError, stripAnsi } from './shared/utils.js';
1717

1818
// Every `opencode` process on this machine writes to one WAL database, so a
1919
// second run — or a terminal opencode, or this server's own catalog probe —
@@ -79,66 +79,6 @@ function readOpenCodeSessionId(event) {
7979
return event.sessionID || event.sessionId || null;
8080
}
8181

82-
function readOpenCodeTokenUsage(sessionId) {
83-
const dbPath = getOpenCodeDatabasePath();
84-
if (!sessionId || !fsSync.existsSync(dbPath)) {
85-
return null;
86-
}
87-
88-
let db = null;
89-
try {
90-
db = new Database(dbPath, { readonly: true, fileMustExist: true });
91-
const columns = db.prepare('PRAGMA table_info(session)').all();
92-
const columnNames = new Set(columns.map((column) => column.name));
93-
const requiredColumns = ['tokens_input', 'tokens_output', 'tokens_reasoning', 'tokens_cache_read', 'tokens_cache_write'];
94-
if (!requiredColumns.every((column) => columnNames.has(column))) {
95-
return null;
96-
}
97-
98-
const row = db.prepare(`
99-
SELECT
100-
tokens_input AS inputTokens,
101-
tokens_output AS outputTokens,
102-
tokens_reasoning AS reasoningTokens,
103-
tokens_cache_read AS cacheReadTokens,
104-
tokens_cache_write AS cacheWriteTokens
105-
FROM session
106-
WHERE id = ?
107-
`).get(sessionId);
108-
109-
if (!row) {
110-
return null;
111-
}
112-
113-
const inputTokens = Number(row.inputTokens || 0) + Number(row.cacheReadTokens || 0);
114-
const outputTokens = Number(row.outputTokens || 0);
115-
const used = Number(row.inputTokens || 0)
116-
+ outputTokens
117-
+ Number(row.reasoningTokens || 0)
118-
+ Number(row.cacheReadTokens || 0)
119-
+ Number(row.cacheWriteTokens || 0);
120-
if (used <= 0) {
121-
return null;
122-
}
123-
124-
return {
125-
used,
126-
inputTokens,
127-
outputTokens,
128-
breakdown: {
129-
input: inputTokens,
130-
output: outputTokens,
131-
},
132-
};
133-
} catch {
134-
return null;
135-
} finally {
136-
if (db) {
137-
db.close();
138-
}
139-
}
140-
}
141-
14282
/**
14383
* Runs one throwaway OpenCode turn on behalf of the background title/recap
14484
* summariser.
@@ -256,6 +196,20 @@ async function spawnOpenCode(command, options = {}, ws) {
256196
options = { ...options, rewind: undefined };
257197
}
258198

199+
// Images are the one thing `opencode run` cannot carry: its read tool returns
200+
// them inside a tool *result*, which an OpenAI-compatible transport flattens
201+
// to text, and `--file` labels every attachment text/plain (upstream
202+
// anomalyco/opencode#16723). The HTTP server takes an image as a part of the
203+
// user message, which is the shape vision models actually read — so a turn
204+
// with an image goes that way and every other turn stays on the CLI.
205+
if (normalizeImageDescriptors(options.images).length > 0) {
206+
return runOpenCodeHttpTurn(command, options, ws, {
207+
registerHandle: (sessionId, handle) => activeOpenCodeProcesses.set(sessionId, handle),
208+
releaseHandle: (sessionId) => activeOpenCodeProcesses.delete(sessionId),
209+
onCompleted: ({ sessionId, cwd }) => queueOpenCodeRecap({ sessionId, cwd, ws }),
210+
});
211+
}
212+
259213
return new Promise((resolve, reject) => {
260214
const { sessionId, projectPath, cwd, model, effort, sessionSummary, images, permissionMode } = options;
261215
const workingDir = cwd || projectPath || process.cwd();
Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
import { randomBytes } from 'node:crypto';
2+
import fsSync from 'node:fs';
3+
4+
import Database from 'better-sqlite3';
5+
6+
import { appendImagesInputTag } from '../shared/image-attachments.js';
7+
import { getOpenCodeDatabasePath } from '../shared/utils.js';
8+
9+
/**
10+
* Writes a turn that ran over the HTTP server into opencode.db.
11+
*
12+
* `opencode run` records every exchange in that database; the standalone server
13+
* does not — it creates the session row and keeps the conversation to itself.
14+
* Nothing else reads the server's copy: the sidebar, the transcript view and
15+
* `opencode run --session` all read opencode.db, so a turn left unwritten is a
16+
* turn that answers the user once and is gone by the next page load, in a
17+
* conversation the CLI then continues with no memory of it.
18+
*
19+
* The rows mirror what the CLI writes for the same exchange, so a session stays
20+
* readable and resumable no matter which transport produced which message.
21+
* Deleting from these same tables is already how session removal and rewind
22+
* work, so this is the existing relationship with the store, not a new one.
23+
*/
24+
25+
const ID_ALPHABET = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
26+
27+
function generateId(prefix) {
28+
const bytes = randomBytes(24);
29+
let id = '';
30+
for (const byte of bytes) {
31+
id += ID_ALPHABET[byte % ID_ALPHABET.length];
32+
}
33+
return `${prefix}_${id}`;
34+
}
35+
36+
/**
37+
* Appends one turn to a session's history.
38+
*
39+
* Best effort by design: the answer has already been streamed to the user and
40+
* is not worth losing to a locked database, so a failure is logged and the turn
41+
* carries on.
42+
*/
43+
export function persistOpenCodeTurn(turn) {
44+
const {
45+
sessionId,
46+
cwd,
47+
providerId,
48+
modelId,
49+
agent = 'build',
50+
promptText,
51+
images,
52+
assistantMessageId,
53+
text,
54+
tools = [],
55+
tokens,
56+
finish = 'stop',
57+
startedAt,
58+
endedAt,
59+
} = turn ?? {};
60+
const databasePath = getOpenCodeDatabasePath();
61+
if (!sessionId || !fsSync.existsSync(databasePath)) {
62+
return;
63+
}
64+
65+
let db = null;
66+
try {
67+
db = new Database(databasePath, { fileMustExist: true });
68+
// The store is shared with every other opencode process on the machine, so
69+
// a short wait beats failing the write outright.
70+
db.pragma('busy_timeout = 5000');
71+
72+
const created = startedAt ?? Date.now();
73+
const completed = endedAt ?? Date.now();
74+
const userMessageId = generateId('msg');
75+
const assistantId = assistantMessageId ?? generateId('msg');
76+
const usage = tokens ?? { input: 0, output: 0, reasoning: 0, cache: { read: 0, write: 0 } };
77+
const total = Number(usage.input || 0)
78+
+ Number(usage.output || 0)
79+
+ Number(usage.reasoning || 0)
80+
+ Number(usage.cache?.read || 0)
81+
+ Number(usage.cache?.write || 0);
82+
83+
const insertMessage = db.prepare(
84+
'INSERT INTO message (id, session_id, time_created, time_updated, data) VALUES (?, ?, ?, ?, ?)',
85+
);
86+
const insertPart = db.prepare(
87+
'INSERT INTO part (id, message_id, session_id, time_created, time_updated, data) VALUES (?, ?, ?, ?, ?, ?)',
88+
);
89+
90+
db.transaction(() => {
91+
insertMessage.run(userMessageId, sessionId, created, created, JSON.stringify({
92+
role: 'user',
93+
time: { created },
94+
agent,
95+
model: { providerID: providerId, modelID: modelId },
96+
summary: { diffs: [] },
97+
}));
98+
99+
// Attachments are recorded in the prompt text the way the CLI records
100+
// them, because that `<images_input>` block is what the transcript reader
101+
// turns back into the thumbnails under the user's message.
102+
insertPart.run(generateId('prt'), userMessageId, sessionId, created, created, JSON.stringify({
103+
type: 'text',
104+
text: appendImagesInputTag(promptText ?? '', images),
105+
}));
106+
107+
insertMessage.run(assistantId, sessionId, completed, completed, JSON.stringify({
108+
parentID: userMessageId,
109+
role: 'assistant',
110+
mode: agent,
111+
agent,
112+
path: { cwd, root: '/' },
113+
cost: 0,
114+
tokens: {
115+
total,
116+
input: Number(usage.input || 0),
117+
output: Number(usage.output || 0),
118+
reasoning: Number(usage.reasoning || 0),
119+
cache: { write: Number(usage.cache?.write || 0), read: Number(usage.cache?.read || 0) },
120+
},
121+
modelID: modelId,
122+
providerID: providerId,
123+
time: { created, completed },
124+
finish,
125+
}));
126+
127+
let partTime = completed;
128+
const nextPartTime = () => (partTime += 1);
129+
130+
insertPart.run(generateId('prt'), assistantId, sessionId, nextPartTime(), partTime, JSON.stringify({
131+
type: 'step-start',
132+
}));
133+
134+
for (const tool of tools) {
135+
insertPart.run(generateId('prt'), assistantId, sessionId, nextPartTime(), partTime, JSON.stringify({
136+
type: 'tool',
137+
tool: tool.name,
138+
callID: tool.callId,
139+
state: {
140+
status: tool.isError ? 'error' : 'completed',
141+
input: tool.input ?? {},
142+
...(tool.isError ? { error: tool.output } : { output: tool.output }),
143+
},
144+
}));
145+
}
146+
147+
if (text && text.trim()) {
148+
insertPart.run(generateId('prt'), assistantId, sessionId, nextPartTime(), partTime, JSON.stringify({
149+
type: 'text',
150+
text,
151+
time: { start: created, end: completed },
152+
}));
153+
}
154+
155+
insertPart.run(generateId('prt'), assistantId, sessionId, nextPartTime(), partTime, JSON.stringify({
156+
type: 'step-finish',
157+
reason: finish,
158+
cost: 0,
159+
tokens: {
160+
total,
161+
input: Number(usage.input || 0),
162+
output: Number(usage.output || 0),
163+
reasoning: Number(usage.reasoning || 0),
164+
cache: { write: Number(usage.cache?.write || 0), read: Number(usage.cache?.read || 0) },
165+
},
166+
}));
167+
})();
168+
} catch (error) {
169+
console.warn('[OpenCode] Could not record the turn in opencode.db:', error?.message || error);
170+
} finally {
171+
db?.close();
172+
}
173+
}

0 commit comments

Comments
 (0)