@pixelbyte-software/pixcode 1.33.10 → 1.33.11
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/dist/api-docs.html +395 -395
- package/dist/assets/{index-B_dU5AHA.js → index-oLYHJ2X5.js} +134 -134
- package/dist/favicon.svg +8 -8
- package/dist/icons/icon-128x128.svg +9 -9
- package/dist/icons/icon-144x144.svg +9 -9
- package/dist/icons/icon-152x152.svg +9 -9
- package/dist/icons/icon-192x192.svg +9 -9
- package/dist/icons/icon-384x384.svg +9 -9
- package/dist/icons/icon-512x512.svg +9 -9
- package/dist/icons/icon-72x72.svg +9 -9
- package/dist/icons/icon-96x96.svg +9 -9
- package/dist/icons/icon-template.svg +9 -9
- package/dist/index.html +1 -1
- package/dist/logo.svg +12 -12
- package/dist/openapi.yaml +1311 -1311
- package/dist-server/server/opencode-cli.js +4 -1
- package/dist-server/server/opencode-cli.js.map +1 -1
- package/package.json +178 -178
- package/server/database/db.js +794 -794
- package/server/database/json-store.js +194 -194
- package/server/modules/providers/list/opencode/opencode-auth.provider.ts +130 -130
- package/server/modules/providers/list/opencode/opencode-mcp.provider.ts +126 -126
- package/server/modules/providers/list/opencode/opencode-sessions.provider.ts +232 -232
- package/server/modules/providers/list/opencode/opencode.provider.ts +29 -29
- package/server/modules/providers/list/qwen/qwen-auth.provider.ts +145 -145
- package/server/modules/providers/list/qwen/qwen-mcp.provider.ts +114 -114
- package/server/modules/providers/list/qwen/qwen-sessions.provider.ts +265 -265
- package/server/modules/providers/list/qwen/qwen.provider.ts +21 -21
- package/server/modules/providers/shared/provider-configs.ts +142 -142
- package/server/opencode-cli.js +4 -1
- package/server/opencode-response-handler.js +107 -107
- package/server/qwen-code-cli.js +395 -395
- package/server/qwen-response-handler.js +73 -73
- package/server/routes/qwen.js +27 -27
- package/server/services/external-access.js +171 -171
- package/server/services/provider-credentials.js +189 -189
- package/server/services/provider-models.js +381 -381
- package/server/services/telegram/telegram-http-client.js +130 -130
- package/server/services/vapid-keys.js +36 -36
- package/server/utils/port-access.js +209 -209
- package/scripts/rest-sweep.mjs +0 -93
|
@@ -1,142 +1,142 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Registry of user-editable config files per provider CLI.
|
|
3
|
-
*
|
|
4
|
-
* This is the single source of truth for the Settings → Agents →
|
|
5
|
-
* Configuration tab. Adding a new provider? Append a row here and the
|
|
6
|
-
* UI + API pick it up — no component changes required.
|
|
7
|
-
*
|
|
8
|
-
* Rules:
|
|
9
|
-
* - `relativePath` is relative to the user's home directory.
|
|
10
|
-
* We never accept absolute paths from the client; the server
|
|
11
|
-
* resolves these explicitly so path traversal is impossible.
|
|
12
|
-
* - `format` drives the CodeMirror language extension on the client.
|
|
13
|
-
* - `readonly: true` hides the Save button and the server rejects
|
|
14
|
-
* writes. Use it for files the CLI owns (e.g. OAuth tokens).
|
|
15
|
-
* - `description` is shown as a subtle caption under the editor.
|
|
16
|
-
*/
|
|
17
|
-
|
|
18
|
-
export type ProviderConfigFormat = 'json' | 'toml' | 'env' | 'text';
|
|
19
|
-
|
|
20
|
-
export type ProviderConfigFile = {
|
|
21
|
-
id: string;
|
|
22
|
-
label: string;
|
|
23
|
-
relativePath: string;
|
|
24
|
-
format: ProviderConfigFormat;
|
|
25
|
-
readonly?: boolean;
|
|
26
|
-
description?: string;
|
|
27
|
-
};
|
|
28
|
-
|
|
29
|
-
export const PROVIDER_CONFIG_FILES: Record<string, ProviderConfigFile[]> = {
|
|
30
|
-
claude: [
|
|
31
|
-
{
|
|
32
|
-
id: 'settings',
|
|
33
|
-
label: 'settings.json',
|
|
34
|
-
relativePath: '.claude/settings.json',
|
|
35
|
-
format: 'json',
|
|
36
|
-
description: 'Main Claude Code settings — default model, system prompt, tool policy.',
|
|
37
|
-
},
|
|
38
|
-
{
|
|
39
|
-
id: 'env',
|
|
40
|
-
label: '.env',
|
|
41
|
-
relativePath: '.claude/.env',
|
|
42
|
-
format: 'env',
|
|
43
|
-
description: 'Environment variables loaded when Claude runs (e.g. ANTHROPIC_API_KEY).',
|
|
44
|
-
},
|
|
45
|
-
],
|
|
46
|
-
codex: [
|
|
47
|
-
{
|
|
48
|
-
id: 'config',
|
|
49
|
-
label: 'config.toml',
|
|
50
|
-
relativePath: '.codex/config.toml',
|
|
51
|
-
format: 'toml',
|
|
52
|
-
description: 'Main Codex CLI config — models, MCP servers, approval policy, sandbox mode.',
|
|
53
|
-
},
|
|
54
|
-
{
|
|
55
|
-
id: 'env',
|
|
56
|
-
label: '.env',
|
|
57
|
-
relativePath: '.codex/.env',
|
|
58
|
-
format: 'env',
|
|
59
|
-
description: 'Environment variables (OPENAI_API_KEY, OPENAI_BASE_URL, …).',
|
|
60
|
-
},
|
|
61
|
-
{
|
|
62
|
-
id: 'auth',
|
|
63
|
-
label: 'auth.json',
|
|
64
|
-
relativePath: '.codex/auth.json',
|
|
65
|
-
format: 'json',
|
|
66
|
-
readonly: true,
|
|
67
|
-
description: 'OAuth tokens managed by `codex login`. Read-only; editing here would corrupt the session.',
|
|
68
|
-
},
|
|
69
|
-
],
|
|
70
|
-
cursor: [
|
|
71
|
-
{
|
|
72
|
-
id: 'env',
|
|
73
|
-
label: '.env',
|
|
74
|
-
relativePath: '.cursor/.env',
|
|
75
|
-
format: 'env',
|
|
76
|
-
description: 'Cursor CLI environment variables.',
|
|
77
|
-
},
|
|
78
|
-
],
|
|
79
|
-
gemini: [
|
|
80
|
-
{
|
|
81
|
-
id: 'settings',
|
|
82
|
-
label: 'settings.json',
|
|
83
|
-
relativePath: '.gemini/settings.json',
|
|
84
|
-
format: 'json',
|
|
85
|
-
description: 'Main Gemini CLI settings — selected model, MCP servers, tool approval mode.',
|
|
86
|
-
},
|
|
87
|
-
{
|
|
88
|
-
id: 'env',
|
|
89
|
-
label: '.env',
|
|
90
|
-
relativePath: '.gemini/.env',
|
|
91
|
-
format: 'env',
|
|
92
|
-
description: 'Environment variables (GOOGLE_API_KEY, GEMINI_API_KEY, …).',
|
|
93
|
-
},
|
|
94
|
-
],
|
|
95
|
-
qwen: [
|
|
96
|
-
{
|
|
97
|
-
id: 'settings',
|
|
98
|
-
label: 'settings.json',
|
|
99
|
-
relativePath: '.qwen/settings.json',
|
|
100
|
-
format: 'json',
|
|
101
|
-
description: 'Main Qwen Code settings — selected model, MCP servers, approval mode.',
|
|
102
|
-
},
|
|
103
|
-
{
|
|
104
|
-
id: 'env',
|
|
105
|
-
label: '.env',
|
|
106
|
-
relativePath: '.qwen/.env',
|
|
107
|
-
format: 'env',
|
|
108
|
-
description: 'Environment variables (DASHSCOPE_API_KEY, OPENAI_API_KEY for OpenAI-compatible routes, …).',
|
|
109
|
-
},
|
|
110
|
-
],
|
|
111
|
-
opencode: [
|
|
112
|
-
{
|
|
113
|
-
id: 'config',
|
|
114
|
-
label: 'opencode.json',
|
|
115
|
-
relativePath: '.config/opencode/opencode.json',
|
|
116
|
-
format: 'json',
|
|
117
|
-
description: 'Main OpenCode config — provider, model, MCP servers, permission rules, agents.',
|
|
118
|
-
},
|
|
119
|
-
{
|
|
120
|
-
id: 'tui',
|
|
121
|
-
label: 'tui.json',
|
|
122
|
-
relativePath: '.config/opencode/tui.json',
|
|
123
|
-
format: 'json',
|
|
124
|
-
description: 'Terminal UI preferences (theme, keybinds). Separate from the main config since 2026-02.',
|
|
125
|
-
},
|
|
126
|
-
{
|
|
127
|
-
id: 'auth',
|
|
128
|
-
label: 'auth.json',
|
|
129
|
-
relativePath: '.local/share/opencode/auth.json',
|
|
130
|
-
format: 'json',
|
|
131
|
-
readonly: true,
|
|
132
|
-
description: 'Provider credentials managed by `opencode auth login`. Read-only here; editing would corrupt stored OAuth tokens.',
|
|
133
|
-
},
|
|
134
|
-
],
|
|
135
|
-
};
|
|
136
|
-
|
|
137
|
-
export const SUPPORTED_CONFIG_PROVIDERS = Object.keys(PROVIDER_CONFIG_FILES);
|
|
138
|
-
|
|
139
|
-
// Hard cap — no config file we care about is remotely this big, but we
|
|
140
|
-
// want to refuse reads and writes that would swell memory. Editing a 1 MB
|
|
141
|
-
// settings.json is already a smell.
|
|
142
|
-
export const MAX_CONFIG_FILE_SIZE_BYTES = 1_048_576; // 1 MB
|
|
1
|
+
/**
|
|
2
|
+
* Registry of user-editable config files per provider CLI.
|
|
3
|
+
*
|
|
4
|
+
* This is the single source of truth for the Settings → Agents →
|
|
5
|
+
* Configuration tab. Adding a new provider? Append a row here and the
|
|
6
|
+
* UI + API pick it up — no component changes required.
|
|
7
|
+
*
|
|
8
|
+
* Rules:
|
|
9
|
+
* - `relativePath` is relative to the user's home directory.
|
|
10
|
+
* We never accept absolute paths from the client; the server
|
|
11
|
+
* resolves these explicitly so path traversal is impossible.
|
|
12
|
+
* - `format` drives the CodeMirror language extension on the client.
|
|
13
|
+
* - `readonly: true` hides the Save button and the server rejects
|
|
14
|
+
* writes. Use it for files the CLI owns (e.g. OAuth tokens).
|
|
15
|
+
* - `description` is shown as a subtle caption under the editor.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
export type ProviderConfigFormat = 'json' | 'toml' | 'env' | 'text';
|
|
19
|
+
|
|
20
|
+
export type ProviderConfigFile = {
|
|
21
|
+
id: string;
|
|
22
|
+
label: string;
|
|
23
|
+
relativePath: string;
|
|
24
|
+
format: ProviderConfigFormat;
|
|
25
|
+
readonly?: boolean;
|
|
26
|
+
description?: string;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
export const PROVIDER_CONFIG_FILES: Record<string, ProviderConfigFile[]> = {
|
|
30
|
+
claude: [
|
|
31
|
+
{
|
|
32
|
+
id: 'settings',
|
|
33
|
+
label: 'settings.json',
|
|
34
|
+
relativePath: '.claude/settings.json',
|
|
35
|
+
format: 'json',
|
|
36
|
+
description: 'Main Claude Code settings — default model, system prompt, tool policy.',
|
|
37
|
+
},
|
|
38
|
+
{
|
|
39
|
+
id: 'env',
|
|
40
|
+
label: '.env',
|
|
41
|
+
relativePath: '.claude/.env',
|
|
42
|
+
format: 'env',
|
|
43
|
+
description: 'Environment variables loaded when Claude runs (e.g. ANTHROPIC_API_KEY).',
|
|
44
|
+
},
|
|
45
|
+
],
|
|
46
|
+
codex: [
|
|
47
|
+
{
|
|
48
|
+
id: 'config',
|
|
49
|
+
label: 'config.toml',
|
|
50
|
+
relativePath: '.codex/config.toml',
|
|
51
|
+
format: 'toml',
|
|
52
|
+
description: 'Main Codex CLI config — models, MCP servers, approval policy, sandbox mode.',
|
|
53
|
+
},
|
|
54
|
+
{
|
|
55
|
+
id: 'env',
|
|
56
|
+
label: '.env',
|
|
57
|
+
relativePath: '.codex/.env',
|
|
58
|
+
format: 'env',
|
|
59
|
+
description: 'Environment variables (OPENAI_API_KEY, OPENAI_BASE_URL, …).',
|
|
60
|
+
},
|
|
61
|
+
{
|
|
62
|
+
id: 'auth',
|
|
63
|
+
label: 'auth.json',
|
|
64
|
+
relativePath: '.codex/auth.json',
|
|
65
|
+
format: 'json',
|
|
66
|
+
readonly: true,
|
|
67
|
+
description: 'OAuth tokens managed by `codex login`. Read-only; editing here would corrupt the session.',
|
|
68
|
+
},
|
|
69
|
+
],
|
|
70
|
+
cursor: [
|
|
71
|
+
{
|
|
72
|
+
id: 'env',
|
|
73
|
+
label: '.env',
|
|
74
|
+
relativePath: '.cursor/.env',
|
|
75
|
+
format: 'env',
|
|
76
|
+
description: 'Cursor CLI environment variables.',
|
|
77
|
+
},
|
|
78
|
+
],
|
|
79
|
+
gemini: [
|
|
80
|
+
{
|
|
81
|
+
id: 'settings',
|
|
82
|
+
label: 'settings.json',
|
|
83
|
+
relativePath: '.gemini/settings.json',
|
|
84
|
+
format: 'json',
|
|
85
|
+
description: 'Main Gemini CLI settings — selected model, MCP servers, tool approval mode.',
|
|
86
|
+
},
|
|
87
|
+
{
|
|
88
|
+
id: 'env',
|
|
89
|
+
label: '.env',
|
|
90
|
+
relativePath: '.gemini/.env',
|
|
91
|
+
format: 'env',
|
|
92
|
+
description: 'Environment variables (GOOGLE_API_KEY, GEMINI_API_KEY, …).',
|
|
93
|
+
},
|
|
94
|
+
],
|
|
95
|
+
qwen: [
|
|
96
|
+
{
|
|
97
|
+
id: 'settings',
|
|
98
|
+
label: 'settings.json',
|
|
99
|
+
relativePath: '.qwen/settings.json',
|
|
100
|
+
format: 'json',
|
|
101
|
+
description: 'Main Qwen Code settings — selected model, MCP servers, approval mode.',
|
|
102
|
+
},
|
|
103
|
+
{
|
|
104
|
+
id: 'env',
|
|
105
|
+
label: '.env',
|
|
106
|
+
relativePath: '.qwen/.env',
|
|
107
|
+
format: 'env',
|
|
108
|
+
description: 'Environment variables (DASHSCOPE_API_KEY, OPENAI_API_KEY for OpenAI-compatible routes, …).',
|
|
109
|
+
},
|
|
110
|
+
],
|
|
111
|
+
opencode: [
|
|
112
|
+
{
|
|
113
|
+
id: 'config',
|
|
114
|
+
label: 'opencode.json',
|
|
115
|
+
relativePath: '.config/opencode/opencode.json',
|
|
116
|
+
format: 'json',
|
|
117
|
+
description: 'Main OpenCode config — provider, model, MCP servers, permission rules, agents.',
|
|
118
|
+
},
|
|
119
|
+
{
|
|
120
|
+
id: 'tui',
|
|
121
|
+
label: 'tui.json',
|
|
122
|
+
relativePath: '.config/opencode/tui.json',
|
|
123
|
+
format: 'json',
|
|
124
|
+
description: 'Terminal UI preferences (theme, keybinds). Separate from the main config since 2026-02.',
|
|
125
|
+
},
|
|
126
|
+
{
|
|
127
|
+
id: 'auth',
|
|
128
|
+
label: 'auth.json',
|
|
129
|
+
relativePath: '.local/share/opencode/auth.json',
|
|
130
|
+
format: 'json',
|
|
131
|
+
readonly: true,
|
|
132
|
+
description: 'Provider credentials managed by `opencode auth login`. Read-only here; editing would corrupt stored OAuth tokens.',
|
|
133
|
+
},
|
|
134
|
+
],
|
|
135
|
+
};
|
|
136
|
+
|
|
137
|
+
export const SUPPORTED_CONFIG_PROVIDERS = Object.keys(PROVIDER_CONFIG_FILES);
|
|
138
|
+
|
|
139
|
+
// Hard cap — no config file we care about is remotely this big, but we
|
|
140
|
+
// want to refuse reads and writes that would swell memory. Editing a 1 MB
|
|
141
|
+
// settings.json is already a smell.
|
|
142
|
+
export const MAX_CONFIG_FILE_SIZE_BYTES = 1_048_576; // 1 MB
|
package/server/opencode-cli.js
CHANGED
|
@@ -94,7 +94,10 @@ async function spawnOpencode(command, options = {}, ws) {
|
|
|
94
94
|
if (sessionId) {
|
|
95
95
|
const session = sessionManager.getSession(sessionId);
|
|
96
96
|
const cliId = session?.cliSessionId || sessionId;
|
|
97
|
-
|
|
97
|
+
// OpenCode CLI requires session IDs to start with 'ses' (e.g. ses_22d6…).
|
|
98
|
+
// Pixcode's internal IDs (opencode_xxxxx) must NOT be passed to -s,
|
|
99
|
+
// otherwise the CLI exits with "Invalid session ID: must start with 'ses'".
|
|
100
|
+
if (cliId && safeSessionIdPattern.test(cliId) && cliId.startsWith('ses')) {
|
|
98
101
|
args.push('-s', cliId);
|
|
99
102
|
}
|
|
100
103
|
}
|
|
@@ -1,107 +1,107 @@
|
|
|
1
|
-
// OpenCode Response Handler — `opencode run --format json` parser.
|
|
2
|
-
//
|
|
3
|
-
// The JSON format streams one event per line (NDJSON). Verified shapes
|
|
4
|
-
// (opencode-ai 0.x, see opencode.ai/docs/cli):
|
|
5
|
-
//
|
|
6
|
-
// { type:"step_start", timestamp, sessionID, part:{ type:"step-start", id, messageID, sessionID } }
|
|
7
|
-
// { type:"text", timestamp, sessionID, part:{ type:"text", id, messageID, sessionID, text:"…", time:{…}, metadata:{…} } }
|
|
8
|
-
// { type:"tool_use", timestamp, sessionID, part:{ type:"tool-use", callID, tool, state:{ input } } }
|
|
9
|
-
// { type:"tool_result", timestamp, sessionID, part:{ type:"tool-result", callID, state:{ output, status } } }
|
|
10
|
-
// { type:"step_finish", timestamp, sessionID, part:{ type:"step-finish", reason, tokens, cost } }
|
|
11
|
-
// { type:"error", timestamp, sessionID, error:{ name, data:{ message, statusCode? } } }
|
|
12
|
-
//
|
|
13
|
-
// Important: field names are camelCase — `sessionID`, `callID`, `messageID`.
|
|
14
|
-
// Earlier integration code assumed snake_case (`session_id`, `tool_id`) which
|
|
15
|
-
// is wrong; nothing in `opencode run --format json` uses snake_case.
|
|
16
|
-
//
|
|
17
|
-
// Lines that don't parse as JSON are surfaced as plain text deltas (covers
|
|
18
|
-
// the CLI's pre-stream banner output, "Shell cwd was reset to …" notices on
|
|
19
|
-
// Windows, and any debug noise).
|
|
20
|
-
import { sessionsService } from './modules/providers/services/sessions.service.js';
|
|
21
|
-
|
|
22
|
-
class OpencodeResponseHandler {
|
|
23
|
-
constructor(ws, options = {}) {
|
|
24
|
-
this.ws = ws;
|
|
25
|
-
this.buffer = '';
|
|
26
|
-
this.onContentFragment = options.onContentFragment || null;
|
|
27
|
-
this.onInit = options.onInit || null;
|
|
28
|
-
this.onToolUse = options.onToolUse || null;
|
|
29
|
-
this.onToolResult = options.onToolResult || null;
|
|
30
|
-
this.capturedCliSessionId = null;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
processData(data) {
|
|
34
|
-
this.buffer += data;
|
|
35
|
-
|
|
36
|
-
const lines = this.buffer.split('\n');
|
|
37
|
-
this.buffer = lines.pop() || '';
|
|
38
|
-
|
|
39
|
-
for (const line of lines) {
|
|
40
|
-
const trimmed = line.trim();
|
|
41
|
-
if (!trimmed) continue;
|
|
42
|
-
try {
|
|
43
|
-
const event = JSON.parse(trimmed);
|
|
44
|
-
this.handleEvent(event);
|
|
45
|
-
} catch {
|
|
46
|
-
// Non-JSON line — surface as plain text delta so the user sees CLI
|
|
47
|
-
// banners / status messages instead of swallowing them silently.
|
|
48
|
-
if (this.onContentFragment) this.onContentFragment(trimmed + '\n');
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
handleEvent(event) {
|
|
54
|
-
const sid = typeof this.ws.getSessionId === 'function' ? this.ws.getSessionId() : null;
|
|
55
|
-
|
|
56
|
-
// Capture OpenCode's real session ID (e.g. `ses_22d6…`) the first time we
|
|
57
|
-
// see one. Every event carries `sessionID` at the top level — we don't
|
|
58
|
-
// wait for an `init`/`session.start` event because OpenCode `run --format
|
|
59
|
-
// json` never emits those (the first event is always `step_start`).
|
|
60
|
-
if (!this.capturedCliSessionId && typeof event.sessionID === 'string' && event.sessionID) {
|
|
61
|
-
this.capturedCliSessionId = event.sessionID;
|
|
62
|
-
if (this.onInit) this.onInit({ session_id: event.sessionID, sessionID: event.sessionID });
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
const part = event.part && typeof event.part === 'object' ? event.part : null;
|
|
66
|
-
|
|
67
|
-
if (event.type === 'text' && part && typeof part.text === 'string') {
|
|
68
|
-
if (this.onContentFragment && part.text) this.onContentFragment(part.text);
|
|
69
|
-
} else if (event.type === 'tool_use' || event.type === 'tool-use' || event.type === 'tool.start') {
|
|
70
|
-
const state = part?.state && typeof part.state === 'object' ? part.state : {};
|
|
71
|
-
if (this.onToolUse) this.onToolUse({
|
|
72
|
-
tool_id: part?.callID || part?.id || event.tool_id || '',
|
|
73
|
-
tool_name: part?.tool || part?.name || event.tool_name || '',
|
|
74
|
-
parameters: state.input || part?.input || event.parameters || {},
|
|
75
|
-
});
|
|
76
|
-
} else if (event.type === 'tool_result' || event.type === 'tool-result' || event.type === 'tool.end') {
|
|
77
|
-
const state = part?.state && typeof part.state === 'object' ? part.state : {};
|
|
78
|
-
if (this.onToolResult) this.onToolResult({
|
|
79
|
-
tool_id: part?.callID || part?.id || event.tool_id || '',
|
|
80
|
-
output: state.output ?? part?.output ?? event.output ?? event.result ?? '',
|
|
81
|
-
status: state.status || event.status || (event.isError ? 'error' : 'ok'),
|
|
82
|
-
});
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
const normalized = sessionsService.normalizeMessage('opencode', event, sid);
|
|
86
|
-
for (const msg of normalized) {
|
|
87
|
-
this.ws.send(msg);
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
forceFlush() {
|
|
92
|
-
if (this.buffer.trim()) {
|
|
93
|
-
try {
|
|
94
|
-
const event = JSON.parse(this.buffer);
|
|
95
|
-
this.handleEvent(event);
|
|
96
|
-
} catch {
|
|
97
|
-
if (this.onContentFragment) this.onContentFragment(this.buffer);
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
destroy() {
|
|
103
|
-
this.buffer = '';
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
export default OpencodeResponseHandler;
|
|
1
|
+
// OpenCode Response Handler — `opencode run --format json` parser.
|
|
2
|
+
//
|
|
3
|
+
// The JSON format streams one event per line (NDJSON). Verified shapes
|
|
4
|
+
// (opencode-ai 0.x, see opencode.ai/docs/cli):
|
|
5
|
+
//
|
|
6
|
+
// { type:"step_start", timestamp, sessionID, part:{ type:"step-start", id, messageID, sessionID } }
|
|
7
|
+
// { type:"text", timestamp, sessionID, part:{ type:"text", id, messageID, sessionID, text:"…", time:{…}, metadata:{…} } }
|
|
8
|
+
// { type:"tool_use", timestamp, sessionID, part:{ type:"tool-use", callID, tool, state:{ input } } }
|
|
9
|
+
// { type:"tool_result", timestamp, sessionID, part:{ type:"tool-result", callID, state:{ output, status } } }
|
|
10
|
+
// { type:"step_finish", timestamp, sessionID, part:{ type:"step-finish", reason, tokens, cost } }
|
|
11
|
+
// { type:"error", timestamp, sessionID, error:{ name, data:{ message, statusCode? } } }
|
|
12
|
+
//
|
|
13
|
+
// Important: field names are camelCase — `sessionID`, `callID`, `messageID`.
|
|
14
|
+
// Earlier integration code assumed snake_case (`session_id`, `tool_id`) which
|
|
15
|
+
// is wrong; nothing in `opencode run --format json` uses snake_case.
|
|
16
|
+
//
|
|
17
|
+
// Lines that don't parse as JSON are surfaced as plain text deltas (covers
|
|
18
|
+
// the CLI's pre-stream banner output, "Shell cwd was reset to …" notices on
|
|
19
|
+
// Windows, and any debug noise).
|
|
20
|
+
import { sessionsService } from './modules/providers/services/sessions.service.js';
|
|
21
|
+
|
|
22
|
+
class OpencodeResponseHandler {
|
|
23
|
+
constructor(ws, options = {}) {
|
|
24
|
+
this.ws = ws;
|
|
25
|
+
this.buffer = '';
|
|
26
|
+
this.onContentFragment = options.onContentFragment || null;
|
|
27
|
+
this.onInit = options.onInit || null;
|
|
28
|
+
this.onToolUse = options.onToolUse || null;
|
|
29
|
+
this.onToolResult = options.onToolResult || null;
|
|
30
|
+
this.capturedCliSessionId = null;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
processData(data) {
|
|
34
|
+
this.buffer += data;
|
|
35
|
+
|
|
36
|
+
const lines = this.buffer.split('\n');
|
|
37
|
+
this.buffer = lines.pop() || '';
|
|
38
|
+
|
|
39
|
+
for (const line of lines) {
|
|
40
|
+
const trimmed = line.trim();
|
|
41
|
+
if (!trimmed) continue;
|
|
42
|
+
try {
|
|
43
|
+
const event = JSON.parse(trimmed);
|
|
44
|
+
this.handleEvent(event);
|
|
45
|
+
} catch {
|
|
46
|
+
// Non-JSON line — surface as plain text delta so the user sees CLI
|
|
47
|
+
// banners / status messages instead of swallowing them silently.
|
|
48
|
+
if (this.onContentFragment) this.onContentFragment(trimmed + '\n');
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
handleEvent(event) {
|
|
54
|
+
const sid = typeof this.ws.getSessionId === 'function' ? this.ws.getSessionId() : null;
|
|
55
|
+
|
|
56
|
+
// Capture OpenCode's real session ID (e.g. `ses_22d6…`) the first time we
|
|
57
|
+
// see one. Every event carries `sessionID` at the top level — we don't
|
|
58
|
+
// wait for an `init`/`session.start` event because OpenCode `run --format
|
|
59
|
+
// json` never emits those (the first event is always `step_start`).
|
|
60
|
+
if (!this.capturedCliSessionId && typeof event.sessionID === 'string' && event.sessionID) {
|
|
61
|
+
this.capturedCliSessionId = event.sessionID;
|
|
62
|
+
if (this.onInit) this.onInit({ session_id: event.sessionID, sessionID: event.sessionID });
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const part = event.part && typeof event.part === 'object' ? event.part : null;
|
|
66
|
+
|
|
67
|
+
if (event.type === 'text' && part && typeof part.text === 'string') {
|
|
68
|
+
if (this.onContentFragment && part.text) this.onContentFragment(part.text);
|
|
69
|
+
} else if (event.type === 'tool_use' || event.type === 'tool-use' || event.type === 'tool.start') {
|
|
70
|
+
const state = part?.state && typeof part.state === 'object' ? part.state : {};
|
|
71
|
+
if (this.onToolUse) this.onToolUse({
|
|
72
|
+
tool_id: part?.callID || part?.id || event.tool_id || '',
|
|
73
|
+
tool_name: part?.tool || part?.name || event.tool_name || '',
|
|
74
|
+
parameters: state.input || part?.input || event.parameters || {},
|
|
75
|
+
});
|
|
76
|
+
} else if (event.type === 'tool_result' || event.type === 'tool-result' || event.type === 'tool.end') {
|
|
77
|
+
const state = part?.state && typeof part.state === 'object' ? part.state : {};
|
|
78
|
+
if (this.onToolResult) this.onToolResult({
|
|
79
|
+
tool_id: part?.callID || part?.id || event.tool_id || '',
|
|
80
|
+
output: state.output ?? part?.output ?? event.output ?? event.result ?? '',
|
|
81
|
+
status: state.status || event.status || (event.isError ? 'error' : 'ok'),
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
const normalized = sessionsService.normalizeMessage('opencode', event, sid);
|
|
86
|
+
for (const msg of normalized) {
|
|
87
|
+
this.ws.send(msg);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
forceFlush() {
|
|
92
|
+
if (this.buffer.trim()) {
|
|
93
|
+
try {
|
|
94
|
+
const event = JSON.parse(this.buffer);
|
|
95
|
+
this.handleEvent(event);
|
|
96
|
+
} catch {
|
|
97
|
+
if (this.onContentFragment) this.onContentFragment(this.buffer);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
destroy() {
|
|
103
|
+
this.buffer = '';
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
export default OpencodeResponseHandler;
|