@chatpanel/bridge 0.2.0 → 0.2.2
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 +1 -1
- package/src/engines/claude.js +32 -0
- package/src/env.js +59 -0
- package/src/server.js +44 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@chatpanel/bridge",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.2",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Local bridge that exposes the AI coding agents installed on your machine — Claude Code (Agent SDK), Codex (CLI), and Gemini CLI — to the ChatPanel Chrome extension over a localhost SSE endpoint. Bring your own agent.",
|
|
6
6
|
"keywords": [
|
package/src/engines/claude.js
CHANGED
|
@@ -122,6 +122,38 @@ export async function chat({ messages, system, options }, emit) {
|
|
|
122
122
|
emit({ type: 'done', text: streamedAny ? '' : resultText });
|
|
123
123
|
}
|
|
124
124
|
|
|
125
|
+
// A fast, tool-free single-shot completion — used for prompt autocomplete. No
|
|
126
|
+
// claude_code preset, no tools, no local config: just a quick text continuation
|
|
127
|
+
// from a fast model (Haiku by default). Returns the completion string.
|
|
128
|
+
export async function complete({ prompt, system, model }) {
|
|
129
|
+
const sdk = await loadSdk();
|
|
130
|
+
if (!sdk) throw new Error('Claude Agent SDK not installed.');
|
|
131
|
+
const { query } = sdk;
|
|
132
|
+
let text = '';
|
|
133
|
+
const iterator = query({
|
|
134
|
+
prompt,
|
|
135
|
+
options: {
|
|
136
|
+
cwd: os.homedir(),
|
|
137
|
+
permissionMode: 'default',
|
|
138
|
+
allowedTools: [], // no tools — pure text completion
|
|
139
|
+
maxTurns: 1,
|
|
140
|
+
settingSources: [], // skip CLAUDE.md / MCP for a tiny completion
|
|
141
|
+
systemPrompt: system || "Continue the user's text briefly. Reply with only the continuation.",
|
|
142
|
+
model: model || 'haiku',
|
|
143
|
+
},
|
|
144
|
+
});
|
|
145
|
+
for await (const message of iterator) {
|
|
146
|
+
if (message.type === 'assistant') {
|
|
147
|
+
for (const block of message.message.content) {
|
|
148
|
+
if (block.type === 'text') text += block.text;
|
|
149
|
+
}
|
|
150
|
+
} else if (message.type === 'result' && message.subtype === 'success' && !text) {
|
|
151
|
+
text = message.result || '';
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
return text.trim();
|
|
155
|
+
}
|
|
156
|
+
|
|
125
157
|
function toolSummary(block) {
|
|
126
158
|
const i = block.input || {};
|
|
127
159
|
if (i.command) return String(i.command).slice(0, 60);
|
package/src/env.js
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
// Resolve a usable PATH for spawning the agent CLIs (codex, gemini).
|
|
2
|
+
//
|
|
3
|
+
// When the bridge runs as a login service (LaunchAgent / Scheduled Task) — or as
|
|
4
|
+
// a double-clicked app — it inherits a MINIMAL PATH, not your interactive shell's.
|
|
5
|
+
// So CLIs installed via Homebrew, npm-global, nvm, etc. aren't found. We fix that
|
|
6
|
+
// by (1) asking your login shell for its PATH and (2) adding common bin dirs.
|
|
7
|
+
|
|
8
|
+
import os from 'node:os';
|
|
9
|
+
import path from 'node:path';
|
|
10
|
+
import { spawnSync } from 'node:child_process';
|
|
11
|
+
|
|
12
|
+
let enriched = false;
|
|
13
|
+
|
|
14
|
+
export function enrichPath() {
|
|
15
|
+
if (enriched || process.platform === 'win32') {
|
|
16
|
+
enriched = true;
|
|
17
|
+
return; // Windows scheduled tasks run as the user and inherit a fuller PATH.
|
|
18
|
+
}
|
|
19
|
+
enriched = true;
|
|
20
|
+
|
|
21
|
+
const home = os.homedir();
|
|
22
|
+
const common = [
|
|
23
|
+
'/opt/homebrew/bin',
|
|
24
|
+
'/opt/homebrew/sbin',
|
|
25
|
+
'/usr/local/bin',
|
|
26
|
+
'/usr/bin',
|
|
27
|
+
'/bin',
|
|
28
|
+
'/usr/sbin',
|
|
29
|
+
'/sbin',
|
|
30
|
+
path.join(home, '.local', 'bin'),
|
|
31
|
+
path.join(home, 'bin'),
|
|
32
|
+
path.join(home, '.npm-global', 'bin'),
|
|
33
|
+
path.join(home, '.cargo', 'bin'),
|
|
34
|
+
path.join(home, '.deno', 'bin'),
|
|
35
|
+
path.join(home, '.bun', 'bin'),
|
|
36
|
+
];
|
|
37
|
+
|
|
38
|
+
// Ask the user's login shell for its PATH — captures nvm / Homebrew / asdf, etc.
|
|
39
|
+
let shellPath = '';
|
|
40
|
+
try {
|
|
41
|
+
const shell = process.env.SHELL || '/bin/zsh';
|
|
42
|
+
const r = spawnSync(shell, ['-ilc', 'command -p echo "$PATH"'], {
|
|
43
|
+
encoding: 'utf8',
|
|
44
|
+
timeout: 4000,
|
|
45
|
+
});
|
|
46
|
+
if (r.status === 0) shellPath = (r.stdout || '').trim();
|
|
47
|
+
} catch {
|
|
48
|
+
/* fall back to common dirs below */
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const seen = new Set();
|
|
52
|
+
const merged = [
|
|
53
|
+
...(shellPath ? shellPath.split(':') : []),
|
|
54
|
+
...(process.env.PATH ? process.env.PATH.split(path.delimiter) : []),
|
|
55
|
+
...common,
|
|
56
|
+
].filter((p) => p && !seen.has(p) && (seen.add(p), true));
|
|
57
|
+
|
|
58
|
+
process.env.PATH = merged.join(path.delimiter);
|
|
59
|
+
}
|
package/src/server.js
CHANGED
|
@@ -19,8 +19,9 @@ import * as claude from './engines/claude.js';
|
|
|
19
19
|
import * as codex from './engines/codex.js';
|
|
20
20
|
import * as gemini from './engines/gemini.js';
|
|
21
21
|
import { installService, uninstallService, serviceStatus } from './service.js';
|
|
22
|
+
import { enrichPath } from './env.js';
|
|
22
23
|
|
|
23
|
-
const VERSION = '0.2.
|
|
24
|
+
const VERSION = '0.2.2';
|
|
24
25
|
const HOST = process.env.CHATPANEL_BRIDGE_HOST || '127.0.0.1';
|
|
25
26
|
const PORT = Number(process.env.CHATPANEL_BRIDGE_PORT) || 4319;
|
|
26
27
|
|
|
@@ -125,6 +126,46 @@ async function handleChat(req, res) {
|
|
|
125
126
|
}
|
|
126
127
|
}
|
|
127
128
|
|
|
129
|
+
// POST /complete → { agent, prompt, model? } → { text } — a fast, single-shot
|
|
130
|
+
// completion for prompt autocomplete. Uses the engine's complete() if it has one
|
|
131
|
+
// (Claude: Haiku, no tools), else a one-shot chat collected into text.
|
|
132
|
+
async function handleComplete(req, res) {
|
|
133
|
+
let body;
|
|
134
|
+
try {
|
|
135
|
+
body = await readBody(req);
|
|
136
|
+
} catch (e) {
|
|
137
|
+
return json(res, 400, { error: 'Bad JSON: ' + e.message });
|
|
138
|
+
}
|
|
139
|
+
const target = ENGINES[body.agent];
|
|
140
|
+
if (!target) return json(res, 404, { error: `Unknown agent "${body.agent}"` });
|
|
141
|
+
const prompt = String(body.prompt || '').slice(0, 6000);
|
|
142
|
+
if (!prompt) return json(res, 400, { error: 'Empty prompt' });
|
|
143
|
+
const model = body.model || '';
|
|
144
|
+
// The extension sends a strict "continue, don't answer" system prompt (with any
|
|
145
|
+
// page context already in `prompt`); fall back to a sensible default.
|
|
146
|
+
const system =
|
|
147
|
+
String(body.system || '').slice(0, 2000) ||
|
|
148
|
+
'You autocomplete an unfinished message the user is typing. Output ONLY the ' +
|
|
149
|
+
'few words that come next. Do not answer it. No quotes, no repetition.';
|
|
150
|
+
try {
|
|
151
|
+
let text = '';
|
|
152
|
+
if (typeof target.engine.complete === 'function') {
|
|
153
|
+
text = await target.engine.complete({ prompt, system, model });
|
|
154
|
+
} else {
|
|
155
|
+
await target.engine.chat(
|
|
156
|
+
{ messages: [{ role: 'user', content: prompt }], system, options: { model } },
|
|
157
|
+
(obj) => {
|
|
158
|
+
if (obj.type === 'delta') text += obj.text || '';
|
|
159
|
+
else if (obj.type === 'done' && obj.text) text += obj.text;
|
|
160
|
+
},
|
|
161
|
+
);
|
|
162
|
+
}
|
|
163
|
+
return json(res, 200, { text: (text || '').trim() });
|
|
164
|
+
} catch (e) {
|
|
165
|
+
return json(res, 502, { error: e?.message || String(e) });
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
128
169
|
const server = createServer(async (req, res) => {
|
|
129
170
|
cors(req, res);
|
|
130
171
|
if (req.method === 'OPTIONS') {
|
|
@@ -135,6 +176,7 @@ const server = createServer(async (req, res) => {
|
|
|
135
176
|
try {
|
|
136
177
|
if (req.method === 'GET' && url.pathname === '/health') return handleHealth(res);
|
|
137
178
|
if (req.method === 'POST' && url.pathname === '/chat') return handleChat(req, res);
|
|
179
|
+
if (req.method === 'POST' && url.pathname === '/complete') return handleComplete(req, res);
|
|
138
180
|
json(res, 404, { error: 'Not found' });
|
|
139
181
|
} catch (e) {
|
|
140
182
|
json(res, 500, { error: e?.message || String(e) });
|
|
@@ -147,6 +189,7 @@ function log(level, msg) {
|
|
|
147
189
|
}
|
|
148
190
|
|
|
149
191
|
function startServer() {
|
|
192
|
+
enrichPath(); // so codex/gemini are found even under a minimal service PATH
|
|
150
193
|
server.listen(PORT, HOST, async () => {
|
|
151
194
|
log('info', `listening on http://${HOST}:${PORT}`);
|
|
152
195
|
for (const [, { engine, label }] of Object.entries(ENGINES)) {
|