@ai-ide-bridge/cli 1.0.5 → 1.1.0
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/.turbo/turbo-build.log +1 -1
- package/dist/commands/daemon.d.ts +1 -0
- package/dist/commands/daemon.js +107 -13
- package/dist/commands/doctor.js +1 -1
- package/dist/commands/init.js +30 -4
- package/dist/commands/login.d.ts +1 -0
- package/dist/commands/login.js +62 -0
- package/dist/commands/logout.d.ts +1 -0
- package/dist/commands/logout.js +12 -0
- package/dist/commands/start.js +4 -4
- package/dist/core/config.d.ts +4 -0
- package/dist/core/config.js +43 -0
- package/dist/core/daemon-session.d.ts +14 -0
- package/dist/core/daemon-session.js +179 -0
- package/dist/core/daemon.d.ts +16 -0
- package/dist/core/daemon.js +168 -0
- package/dist/core/formatter.d.ts +3 -0
- package/dist/core/formatter.js +44 -0
- package/dist/core/index.d.ts +9 -0
- package/dist/core/index.js +9 -0
- package/dist/core/parser.d.ts +164 -0
- package/dist/core/parser.js +37 -0
- package/dist/core/registry.d.ts +16 -0
- package/dist/core/registry.js +53 -0
- package/dist/core/server.d.ts +19 -0
- package/dist/core/server.js +185 -0
- package/dist/core/session.d.ts +11 -0
- package/dist/core/session.js +39 -0
- package/dist/core/types.d.ts +166 -0
- package/dist/core/types.js +44 -0
- package/dist/index.js +22 -5
- package/dist/oauth/device-flow.d.ts +12 -0
- package/dist/oauth/device-flow.js +93 -0
- package/dist/oauth/flow.d.ts +11 -0
- package/dist/oauth/flow.js +75 -0
- package/dist/oauth/index.d.ts +6 -0
- package/dist/oauth/index.js +5 -0
- package/dist/oauth/lifecycle.d.ts +13 -0
- package/dist/oauth/lifecycle.js +56 -0
- package/dist/oauth/providers.d.ts +2 -0
- package/dist/oauth/providers.js +19 -0
- package/dist/oauth/storage-file.d.ts +2 -0
- package/dist/oauth/storage-file.js +68 -0
- package/dist/oauth/storage.d.ts +2 -0
- package/dist/oauth/storage.js +4 -0
- package/dist/oauth/types.d.ts +44 -0
- package/dist/oauth/types.js +1 -0
- package/dist/plugins/copilot/auth.d.ts +7 -0
- package/dist/plugins/copilot/auth.js +30 -0
- package/dist/plugins/copilot/index.d.ts +5 -0
- package/dist/plugins/copilot/index.js +4 -0
- package/dist/plugins/copilot/plugin.d.ts +8 -0
- package/dist/plugins/copilot/plugin.js +29 -0
- package/dist/plugins/copilot/session.d.ts +8 -0
- package/dist/plugins/copilot/session.js +115 -0
- package/dist/plugins/copilot/tools.d.ts +10 -0
- package/dist/plugins/copilot/tools.js +10 -0
- package/dist/plugins/copilot/types.d.ts +15 -0
- package/dist/plugins/copilot/types.js +27 -0
- package/dist/plugins/cursor/index.d.ts +2 -0
- package/dist/plugins/cursor/index.js +2 -0
- package/dist/plugins/cursor/plugin.d.ts +8 -0
- package/dist/plugins/cursor/plugin.js +36 -0
- package/dist/plugins/cursor/session.d.ts +11 -0
- package/dist/plugins/cursor/session.js +69 -0
- package/dist/plugins/cursor/tools.d.ts +11 -0
- package/dist/plugins/cursor/tools.js +13 -0
- package/dist/plugins/windsurf/auth.d.ts +3 -0
- package/dist/plugins/windsurf/auth.js +20 -0
- package/dist/plugins/windsurf/daemon.d.ts +6 -0
- package/dist/plugins/windsurf/daemon.js +16 -0
- package/dist/plugins/windsurf/index.d.ts +5 -0
- package/dist/plugins/windsurf/index.js +4 -0
- package/dist/plugins/windsurf/models.d.ts +2 -0
- package/dist/plugins/windsurf/models.js +42 -0
- package/dist/plugins/windsurf/plugin.d.ts +8 -0
- package/dist/plugins/windsurf/plugin.js +31 -0
- package/dist/plugins/windsurf/session.d.ts +5 -0
- package/dist/plugins/windsurf/session.js +6 -0
- package/dist/plugins/windsurf/tools.d.ts +3 -0
- package/dist/plugins/windsurf/tools.js +10 -0
- package/dist/plugins/windsurf/types.d.ts +22 -0
- package/dist/plugins/windsurf/types.js +1 -0
- package/dist/utils/config.d.ts +1 -1
- package/dist/utils/config.js +1 -1
- package/dist/utils/platform.d.ts +1 -0
- package/dist/utils/platform.js +3 -0
- package/package.json +3 -5
- package/src/commands/daemon.ts +112 -13
- package/src/commands/doctor.ts +1 -1
- package/src/commands/init.ts +29 -4
- package/src/commands/login.ts +98 -0
- package/src/commands/logout.ts +15 -0
- package/src/commands/start.ts +4 -4
- package/src/core/config.ts +45 -0
- package/src/core/daemon-session.ts +199 -0
- package/src/core/daemon.ts +206 -0
- package/src/core/formatter.ts +56 -0
- package/src/core/index.ts +9 -0
- package/src/core/parser.ts +47 -0
- package/src/core/registry.ts +62 -0
- package/src/core/server.ts +211 -0
- package/src/core/session.ts +54 -0
- package/src/core/types.ts +100 -0
- package/src/index.ts +22 -4
- package/src/oauth/device-flow.ts +111 -0
- package/src/oauth/flow.ts +94 -0
- package/src/oauth/index.ts +6 -0
- package/src/oauth/lifecycle.ts +77 -0
- package/src/oauth/providers.ts +21 -0
- package/src/oauth/storage-file.ts +77 -0
- package/src/oauth/storage.ts +6 -0
- package/src/oauth/types.ts +50 -0
- package/src/plugins/copilot/auth.ts +39 -0
- package/src/plugins/copilot/index.ts +5 -0
- package/src/plugins/copilot/plugin.ts +31 -0
- package/src/plugins/copilot/session.ts +130 -0
- package/src/plugins/copilot/tools.ts +21 -0
- package/src/plugins/copilot/types.ts +43 -0
- package/src/plugins/cursor/index.ts +2 -0
- package/src/plugins/cursor/plugin.ts +37 -0
- package/src/plugins/cursor/session.ts +78 -0
- package/src/plugins/cursor/tools.ts +25 -0
- package/src/plugins/windsurf/auth.ts +23 -0
- package/src/plugins/windsurf/daemon.ts +24 -0
- package/src/plugins/windsurf/index.ts +5 -0
- package/src/plugins/windsurf/models.ts +44 -0
- package/src/plugins/windsurf/plugin.ts +34 -0
- package/src/plugins/windsurf/session.ts +8 -0
- package/src/plugins/windsurf/tools.ts +13 -0
- package/src/plugins/windsurf/types.ts +24 -0
- package/src/utils/config.ts +1 -1
- package/src/utils/platform.ts +3 -0
- package/test/daemon.test.ts +224 -0
package/.turbo/turbo-build.log
CHANGED
|
@@ -3,3 +3,4 @@ export declare function uninstallDaemonCommand(): Promise<void>;
|
|
|
3
3
|
export declare function daemonStatusCommand(): Promise<void>;
|
|
4
4
|
export declare function daemonDownloadCommand(): Promise<void>;
|
|
5
5
|
export declare function daemonLocateCommand(): Promise<void>;
|
|
6
|
+
export declare function daemonReloadCommand(): Promise<void>;
|
package/dist/commands/daemon.js
CHANGED
|
@@ -2,17 +2,25 @@ import fs from 'node:fs';
|
|
|
2
2
|
import path from 'node:path';
|
|
3
3
|
import os from 'node:os';
|
|
4
4
|
import { execSync } from 'node:child_process';
|
|
5
|
-
import {
|
|
6
|
-
|
|
7
|
-
const __dirname = path.dirname(__filename);
|
|
5
|
+
import { createWindsurfDaemon } from '../plugins/windsurf/daemon.js';
|
|
6
|
+
import { getPlatform } from '../utils/platform.js';
|
|
8
7
|
const LABEL = 'com.llm-bridge.daemon';
|
|
9
8
|
export async function installDaemonCommand() {
|
|
10
|
-
|
|
11
|
-
|
|
9
|
+
const platform = getPlatform();
|
|
10
|
+
if (platform === 'darwin') {
|
|
11
|
+
await installMacOSDaemon();
|
|
12
|
+
}
|
|
13
|
+
else if (platform === 'linux') {
|
|
14
|
+
await installLinuxDaemon();
|
|
15
|
+
}
|
|
16
|
+
else {
|
|
17
|
+
console.error('Daemon installation is only supported on macOS and Linux.');
|
|
12
18
|
process.exit(1);
|
|
13
19
|
}
|
|
20
|
+
}
|
|
21
|
+
async function installMacOSDaemon() {
|
|
14
22
|
const plistPath = path.join(os.homedir(), 'Library', 'LaunchAgents', `${LABEL}.plist`);
|
|
15
|
-
const wrapperPath = path.join(
|
|
23
|
+
const wrapperPath = path.join(path.dirname(process.execPath), '..', 'scripts', 'llm-bridge-daemon.sh');
|
|
16
24
|
const plist = `<?xml version="1.0" encoding="UTF-8"?>
|
|
17
25
|
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
|
18
26
|
<plist version="1.0">
|
|
@@ -35,8 +43,14 @@ export async function installDaemonCommand() {
|
|
|
35
43
|
<string>${os.homedir()}/Library/Logs/llm-bridge.err.log</string>
|
|
36
44
|
</dict>
|
|
37
45
|
</plist>`;
|
|
38
|
-
|
|
39
|
-
|
|
46
|
+
try {
|
|
47
|
+
fs.mkdirSync(path.dirname(plistPath), { recursive: true });
|
|
48
|
+
fs.writeFileSync(plistPath, plist);
|
|
49
|
+
}
|
|
50
|
+
catch (e) {
|
|
51
|
+
console.error('Failed to write plist file:', e);
|
|
52
|
+
process.exit(1);
|
|
53
|
+
}
|
|
40
54
|
try {
|
|
41
55
|
execSync(`launchctl bootstrap "gui/$(id -u)" "${plistPath}"`, { stdio: 'inherit' });
|
|
42
56
|
console.log(`Installed LaunchAgent: ${plistPath}`);
|
|
@@ -47,11 +61,56 @@ export async function installDaemonCommand() {
|
|
|
47
61
|
process.exit(1);
|
|
48
62
|
}
|
|
49
63
|
}
|
|
64
|
+
async function installLinuxDaemon() {
|
|
65
|
+
const serviceDir = path.join(os.homedir(), '.config', 'systemd', 'user');
|
|
66
|
+
const servicePath = path.join(serviceDir, 'llm-bridge.service');
|
|
67
|
+
const binaryPath = process.execPath;
|
|
68
|
+
const unit = `[Unit]
|
|
69
|
+
Description=llm-bridge daemon
|
|
70
|
+
After=network.target
|
|
71
|
+
|
|
72
|
+
[Service]
|
|
73
|
+
Type=simple
|
|
74
|
+
ExecStart=${binaryPath} start
|
|
75
|
+
Restart=on-failure
|
|
76
|
+
RestartSec=5
|
|
77
|
+
|
|
78
|
+
[Install]
|
|
79
|
+
WantedBy=default.target
|
|
80
|
+
`;
|
|
81
|
+
try {
|
|
82
|
+
fs.mkdirSync(serviceDir, { recursive: true });
|
|
83
|
+
fs.writeFileSync(servicePath, unit);
|
|
84
|
+
}
|
|
85
|
+
catch (e) {
|
|
86
|
+
console.error('Failed to write service file:', e);
|
|
87
|
+
process.exit(1);
|
|
88
|
+
}
|
|
89
|
+
try {
|
|
90
|
+
execSync('systemctl --user daemon-reload', { stdio: 'inherit' });
|
|
91
|
+
execSync('systemctl --user enable --now llm-bridge', { stdio: 'inherit' });
|
|
92
|
+
console.log(`Installed systemd user service: ${servicePath}`);
|
|
93
|
+
console.log('Logs: journalctl --user -u llm-bridge -f');
|
|
94
|
+
}
|
|
95
|
+
catch (e) {
|
|
96
|
+
console.error('Failed to enable systemd service:', e);
|
|
97
|
+
process.exit(1);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
50
100
|
export async function uninstallDaemonCommand() {
|
|
51
|
-
|
|
52
|
-
|
|
101
|
+
const platform = getPlatform();
|
|
102
|
+
if (platform === 'darwin') {
|
|
103
|
+
await uninstallMacOSDaemon();
|
|
104
|
+
}
|
|
105
|
+
else if (platform === 'linux') {
|
|
106
|
+
await uninstallLinuxDaemon();
|
|
107
|
+
}
|
|
108
|
+
else {
|
|
109
|
+
console.error('Daemon uninstallation is only supported on macOS and Linux.');
|
|
53
110
|
process.exit(1);
|
|
54
111
|
}
|
|
112
|
+
}
|
|
113
|
+
async function uninstallMacOSDaemon() {
|
|
55
114
|
const plistPath = path.join(os.homedir(), 'Library', 'LaunchAgents', `${LABEL}.plist`);
|
|
56
115
|
try {
|
|
57
116
|
execSync(`launchctl bootout "gui/$(id -u)" "${plistPath}" 2>/dev/null || true`, {
|
|
@@ -69,8 +128,31 @@ export async function uninstallDaemonCommand() {
|
|
|
69
128
|
console.log('No LaunchAgent found.');
|
|
70
129
|
}
|
|
71
130
|
}
|
|
131
|
+
async function uninstallLinuxDaemon() {
|
|
132
|
+
const servicePath = path.join(os.homedir(), '.config', 'systemd', 'user', 'llm-bridge.service');
|
|
133
|
+
try {
|
|
134
|
+
execSync('systemctl --user disable --now llm-bridge 2>/dev/null || true', {
|
|
135
|
+
stdio: 'inherit',
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
catch {
|
|
139
|
+
// Ignore errors during disable
|
|
140
|
+
}
|
|
141
|
+
if (fs.existsSync(servicePath)) {
|
|
142
|
+
fs.unlinkSync(servicePath);
|
|
143
|
+
console.log(`Removed systemd service: ${servicePath}`);
|
|
144
|
+
}
|
|
145
|
+
else {
|
|
146
|
+
console.log('No systemd service found.');
|
|
147
|
+
}
|
|
148
|
+
try {
|
|
149
|
+
execSync('systemctl --user daemon-reload', { stdio: 'inherit' });
|
|
150
|
+
}
|
|
151
|
+
catch {
|
|
152
|
+
// Ignore errors during reload
|
|
153
|
+
}
|
|
154
|
+
}
|
|
72
155
|
export async function daemonStatusCommand() {
|
|
73
|
-
const { createWindsurfDaemon } = await import('@ai-ide-bridge/windsurf/daemon.js');
|
|
74
156
|
const daemon = createWindsurfDaemon();
|
|
75
157
|
const path = await daemon.locate();
|
|
76
158
|
if (path) {
|
|
@@ -84,7 +166,6 @@ export async function daemonStatusCommand() {
|
|
|
84
166
|
}
|
|
85
167
|
}
|
|
86
168
|
export async function daemonDownloadCommand() {
|
|
87
|
-
const { createWindsurfDaemon } = await import('@ai-ide-bridge/windsurf/daemon.js');
|
|
88
169
|
const daemon = createWindsurfDaemon();
|
|
89
170
|
console.log('Downloading Windsurf language server...');
|
|
90
171
|
try {
|
|
@@ -97,7 +178,6 @@ export async function daemonDownloadCommand() {
|
|
|
97
178
|
}
|
|
98
179
|
}
|
|
99
180
|
export async function daemonLocateCommand() {
|
|
100
|
-
const { createWindsurfDaemon } = await import('@ai-ide-bridge/windsurf/daemon.js');
|
|
101
181
|
const daemon = createWindsurfDaemon();
|
|
102
182
|
const path = await daemon.locate();
|
|
103
183
|
if (path) {
|
|
@@ -108,3 +188,17 @@ export async function daemonLocateCommand() {
|
|
|
108
188
|
process.exit(1);
|
|
109
189
|
}
|
|
110
190
|
}
|
|
191
|
+
export async function daemonReloadCommand() {
|
|
192
|
+
if (getPlatform() !== 'linux') {
|
|
193
|
+
console.error('Daemon reload is only supported on Linux.');
|
|
194
|
+
process.exit(1);
|
|
195
|
+
}
|
|
196
|
+
try {
|
|
197
|
+
execSync('systemctl --user reload-or-restart llm-bridge', { stdio: 'inherit' });
|
|
198
|
+
console.log('Daemon reloaded.');
|
|
199
|
+
}
|
|
200
|
+
catch (e) {
|
|
201
|
+
console.error('Failed to reload daemon:', e);
|
|
202
|
+
process.exit(1);
|
|
203
|
+
}
|
|
204
|
+
}
|
package/dist/commands/doctor.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import http from 'node:http';
|
|
2
2
|
import { readConfig } from '../utils/config.js';
|
|
3
3
|
import fs from 'node:fs';
|
|
4
|
-
import { configPath } from '
|
|
4
|
+
import { configPath } from '../core/index.js';
|
|
5
5
|
export async function doctorCommand() {
|
|
6
6
|
const config = readConfig();
|
|
7
7
|
console.log('llm-bridge diagnostics\n');
|
package/dist/commands/init.js
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import { setPluginConfig, writeConfig, readConfig } from '../utils/config.js';
|
|
2
|
-
import { CursorBridgePlugin } from '
|
|
3
|
-
import { CopilotBridgePlugin } from '
|
|
4
|
-
import { WindsurfBridgePlugin } from '
|
|
2
|
+
import { CursorBridgePlugin } from '../plugins/cursor/index.js';
|
|
3
|
+
import { CopilotBridgePlugin } from '../plugins/copilot/index.js';
|
|
4
|
+
import { WindsurfBridgePlugin } from '../plugins/windsurf/index.js';
|
|
5
5
|
import { createInterface } from 'node:readline';
|
|
6
|
+
import { loginCommand } from './login.js';
|
|
6
7
|
const PROVIDERS = ['cursor', 'copilot', 'windsurf'];
|
|
7
8
|
async function getProviderPlugin(provider) {
|
|
8
9
|
switch (provider) {
|
|
@@ -19,7 +20,7 @@ function getCredentialPrompt(provider) {
|
|
|
19
20
|
case 'cursor':
|
|
20
21
|
return 'Enter your CURSOR_API_KEY: ';
|
|
21
22
|
case 'copilot':
|
|
22
|
-
return 'Enter your GITHUB_TOKEN: ';
|
|
23
|
+
return 'Enter your GITHUB_TOKEN (or leave empty for OAuth): ';
|
|
23
24
|
case 'windsurf':
|
|
24
25
|
return 'Enter your WINDSURF_TOKEN: ';
|
|
25
26
|
}
|
|
@@ -34,6 +35,14 @@ function getEnvVar(provider) {
|
|
|
34
35
|
return 'WINDSURF_TOKEN';
|
|
35
36
|
}
|
|
36
37
|
}
|
|
38
|
+
async function authenticateWithOAuth(provider) {
|
|
39
|
+
if (provider === 'copilot') {
|
|
40
|
+
await loginCommand('copilot');
|
|
41
|
+
return true;
|
|
42
|
+
}
|
|
43
|
+
console.log(`OAuth not available for ${provider}. Use token authentication instead.`);
|
|
44
|
+
return false;
|
|
45
|
+
}
|
|
37
46
|
export async function initCommand() {
|
|
38
47
|
const rl = createInterface({ input: process.stdin, output: process.stdout });
|
|
39
48
|
const ask = (q) => new Promise((resolve) => rl.question(q, resolve));
|
|
@@ -55,6 +64,23 @@ export async function initCommand() {
|
|
|
55
64
|
break;
|
|
56
65
|
}
|
|
57
66
|
const provider = input;
|
|
67
|
+
if (provider === 'copilot') {
|
|
68
|
+
const method = await ask('Auth method (token/oauth): ');
|
|
69
|
+
if (method.toLowerCase() === 'oauth') {
|
|
70
|
+
const ok = await authenticateWithOAuth(provider);
|
|
71
|
+
if (!ok)
|
|
72
|
+
continue;
|
|
73
|
+
console.log(`Authentication successful for ${provider}.`);
|
|
74
|
+
setPluginConfig(provider, { COPILOT_OAUTH: 'true' });
|
|
75
|
+
configuredProviders.push(provider);
|
|
76
|
+
if (!firstProvider)
|
|
77
|
+
firstProvider = provider;
|
|
78
|
+
const more = await ask('Configure another provider? (y/n): ');
|
|
79
|
+
if (more.toLowerCase() !== 'y')
|
|
80
|
+
break;
|
|
81
|
+
continue;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
58
84
|
const envVar = getEnvVar(provider);
|
|
59
85
|
const credential = await ask(getCredentialPrompt(provider));
|
|
60
86
|
if (!credential) {
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function loginCommand(provider?: string): Promise<void>;
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { createInterface } from 'node:readline';
|
|
2
|
+
import { createTokenStore, DeviceFlow, providers } from '../oauth/index.js';
|
|
3
|
+
const SUPPORTED_PROVIDERS = ['copilot', 'cursor'];
|
|
4
|
+
export async function loginCommand(provider) {
|
|
5
|
+
const rl = createInterface({ input: process.stdin, output: process.stdout });
|
|
6
|
+
const ask = (q) => new Promise((resolve) => rl.question(q, resolve));
|
|
7
|
+
let providerId;
|
|
8
|
+
if (provider && SUPPORTED_PROVIDERS.includes(provider)) {
|
|
9
|
+
providerId = provider;
|
|
10
|
+
}
|
|
11
|
+
else {
|
|
12
|
+
const input = await ask(`Provider to login (${SUPPORTED_PROVIDERS.join(', ')}): `);
|
|
13
|
+
if (!SUPPORTED_PROVIDERS.includes(input)) {
|
|
14
|
+
console.error(`Unknown provider: ${input}`);
|
|
15
|
+
rl.close();
|
|
16
|
+
process.exit(1);
|
|
17
|
+
}
|
|
18
|
+
providerId = input;
|
|
19
|
+
}
|
|
20
|
+
const oauthProvider = providers[providerId];
|
|
21
|
+
if (!oauthProvider) {
|
|
22
|
+
console.error(`OAuth not configured for provider: ${providerId}`);
|
|
23
|
+
rl.close();
|
|
24
|
+
process.exit(1);
|
|
25
|
+
}
|
|
26
|
+
const store = createTokenStore();
|
|
27
|
+
if (oauthProvider.deviceFlow) {
|
|
28
|
+
await loginWithDeviceFlow(rl, oauthProvider, store);
|
|
29
|
+
}
|
|
30
|
+
else {
|
|
31
|
+
await loginWithPKCE(rl, oauthProvider, store);
|
|
32
|
+
}
|
|
33
|
+
rl.close();
|
|
34
|
+
}
|
|
35
|
+
async function loginWithDeviceFlow(rl, provider, store) {
|
|
36
|
+
const config = {
|
|
37
|
+
provider,
|
|
38
|
+
store,
|
|
39
|
+
};
|
|
40
|
+
const deviceFlow = new DeviceFlow(config);
|
|
41
|
+
console.log(`\nAuthenticating with ${provider.name}...`);
|
|
42
|
+
console.log('Requesting device code...');
|
|
43
|
+
try {
|
|
44
|
+
const deviceCode = await deviceFlow.start();
|
|
45
|
+
console.log(`\nOpen this URL in your browser: ${deviceCode.verificationUri}`);
|
|
46
|
+
console.log(`Enter this code: ${deviceCode.userCode}\n`);
|
|
47
|
+
console.log('Waiting for authorization...');
|
|
48
|
+
const token = await deviceFlow.poll();
|
|
49
|
+
const expiresIn = Math.round((token.expiresAt - Date.now()) / 1000);
|
|
50
|
+
console.log(`\nAuthentication successful!`);
|
|
51
|
+
console.log(`Token stored securely. Expires in ${expiresIn}s.`);
|
|
52
|
+
}
|
|
53
|
+
catch (err) {
|
|
54
|
+
console.error(`\nAuthentication failed: ${err.message}`);
|
|
55
|
+
process.exit(1);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
async function loginWithPKCE(rl, provider, store) {
|
|
59
|
+
console.log(`\nOAuth with PKCE is not yet supported for ${provider.name}.`);
|
|
60
|
+
console.log('Use a personal access token instead: llm-bridge init');
|
|
61
|
+
process.exit(1);
|
|
62
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function logoutCommand(provider?: string): Promise<void>;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { createTokenStore } from '../oauth/index.js';
|
|
2
|
+
export async function logoutCommand(provider) {
|
|
3
|
+
const store = createTokenStore();
|
|
4
|
+
if (provider) {
|
|
5
|
+
await store.delete(provider);
|
|
6
|
+
console.log(`Logged out from ${provider}.`);
|
|
7
|
+
return;
|
|
8
|
+
}
|
|
9
|
+
await store.delete('copilot');
|
|
10
|
+
await store.delete('cursor');
|
|
11
|
+
console.log('Logged out from all providers.');
|
|
12
|
+
}
|
package/dist/commands/start.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { BridgeServer, loadConfig } from '
|
|
2
|
-
import { CursorBridgePlugin } from '
|
|
3
|
-
import { CopilotBridgePlugin } from '
|
|
4
|
-
import { WindsurfBridgePlugin } from '
|
|
1
|
+
import { BridgeServer, loadConfig } from '../core/index.js';
|
|
2
|
+
import { CursorBridgePlugin } from '../plugins/cursor/index.js';
|
|
3
|
+
import { CopilotBridgePlugin } from '../plugins/copilot/index.js';
|
|
4
|
+
import { WindsurfBridgePlugin } from '../plugins/windsurf/index.js';
|
|
5
5
|
async function getPlugin(name) {
|
|
6
6
|
switch (name) {
|
|
7
7
|
case 'cursor':
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import os from 'node:os';
|
|
4
|
+
import { DefaultConfig } from './types.js';
|
|
5
|
+
export function configPath() {
|
|
6
|
+
const home = os.homedir();
|
|
7
|
+
return path.join(home, '.config', 'llm-bridge', 'config.json');
|
|
8
|
+
}
|
|
9
|
+
export function loadConfig() {
|
|
10
|
+
const envPort = process.env.LLM_BRIDGE_PORT;
|
|
11
|
+
const envHost = process.env.LLM_BRIDGE_HOST;
|
|
12
|
+
try {
|
|
13
|
+
const filePath = process.env.LLM_BRIDGE_CONFIG ?? configPath();
|
|
14
|
+
if (fs.existsSync(filePath)) {
|
|
15
|
+
const raw = fs.readFileSync(filePath, 'utf8');
|
|
16
|
+
const fileConfig = JSON.parse(raw);
|
|
17
|
+
const config = { ...DefaultConfig, ...fileConfig };
|
|
18
|
+
if (fileConfig.activePlugin && !fileConfig.defaultPlugin) {
|
|
19
|
+
config.defaultPlugin = fileConfig.activePlugin;
|
|
20
|
+
}
|
|
21
|
+
if (envPort)
|
|
22
|
+
config.port = parseInt(envPort, 10);
|
|
23
|
+
if (envHost)
|
|
24
|
+
config.host = envHost;
|
|
25
|
+
return config;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
catch (err) {
|
|
29
|
+
console.warn('[llm-bridge] failed to load config file, using defaults:', err);
|
|
30
|
+
}
|
|
31
|
+
const config = { ...DefaultConfig };
|
|
32
|
+
if (envPort)
|
|
33
|
+
config.port = parseInt(envPort, 10);
|
|
34
|
+
if (envHost)
|
|
35
|
+
config.host = envHost;
|
|
36
|
+
return config;
|
|
37
|
+
}
|
|
38
|
+
export function saveConfig(config) {
|
|
39
|
+
const filePath = configPath();
|
|
40
|
+
const dir = path.dirname(filePath);
|
|
41
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
42
|
+
fs.writeFileSync(filePath, JSON.stringify(config, null, 2));
|
|
43
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { BridgeSession, Message, ToolDefinition, StreamChunk } from './types.js';
|
|
2
|
+
import type { DaemonManager } from './daemon.js';
|
|
3
|
+
export declare class DaemonBridgeSession implements BridgeSession {
|
|
4
|
+
private daemon;
|
|
5
|
+
private token;
|
|
6
|
+
private model;
|
|
7
|
+
private cwd;
|
|
8
|
+
private proc;
|
|
9
|
+
private requestId;
|
|
10
|
+
private busy;
|
|
11
|
+
constructor(daemon: DaemonManager, token: string, model: string, cwd: string);
|
|
12
|
+
send(messages: Message[], tools?: ToolDefinition[]): AsyncIterable<StreamChunk>;
|
|
13
|
+
dispose(): Promise<void>;
|
|
14
|
+
}
|
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
const MAX_BUFFER_SIZE = 1024 * 1024;
|
|
2
|
+
export class DaemonBridgeSession {
|
|
3
|
+
daemon;
|
|
4
|
+
token;
|
|
5
|
+
model;
|
|
6
|
+
cwd;
|
|
7
|
+
proc = null;
|
|
8
|
+
requestId = 0;
|
|
9
|
+
busy = false;
|
|
10
|
+
constructor(daemon, token, model, cwd) {
|
|
11
|
+
this.daemon = daemon;
|
|
12
|
+
this.token = token;
|
|
13
|
+
this.model = model;
|
|
14
|
+
this.cwd = cwd;
|
|
15
|
+
}
|
|
16
|
+
async *send(messages, tools) {
|
|
17
|
+
if (this.busy) {
|
|
18
|
+
yield {
|
|
19
|
+
type: 'error',
|
|
20
|
+
content: 'Session is busy — concurrent send() calls are not supported',
|
|
21
|
+
finishReason: 'error',
|
|
22
|
+
};
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
this.busy = true;
|
|
26
|
+
if (!this.proc) {
|
|
27
|
+
const binaryPath = await this.daemon.locate();
|
|
28
|
+
if (!binaryPath) {
|
|
29
|
+
this.busy = false;
|
|
30
|
+
throw new Error(`Daemon binary '${this.daemon.binaryName}' not found`);
|
|
31
|
+
}
|
|
32
|
+
this.proc = this.daemon.spawn(binaryPath, []);
|
|
33
|
+
}
|
|
34
|
+
const id = ++this.requestId;
|
|
35
|
+
const request = {
|
|
36
|
+
jsonrpc: '2.0',
|
|
37
|
+
id,
|
|
38
|
+
method: 'chat/completions',
|
|
39
|
+
params: {
|
|
40
|
+
token: this.token,
|
|
41
|
+
model: this.model,
|
|
42
|
+
messages,
|
|
43
|
+
stream: true,
|
|
44
|
+
tools: tools?.map((t) => ({
|
|
45
|
+
type: 'function',
|
|
46
|
+
function: {
|
|
47
|
+
name: t.function.name,
|
|
48
|
+
description: t.function.description,
|
|
49
|
+
parameters: t.function.parameters,
|
|
50
|
+
},
|
|
51
|
+
})),
|
|
52
|
+
},
|
|
53
|
+
};
|
|
54
|
+
this.proc.stdin.write(JSON.stringify(request) + '\n');
|
|
55
|
+
let buffer = '';
|
|
56
|
+
let stderrBuffer = '';
|
|
57
|
+
let finished = false;
|
|
58
|
+
let capturedError = null;
|
|
59
|
+
let onDataResolve = null;
|
|
60
|
+
const onStderr = (data) => {
|
|
61
|
+
stderrBuffer += data.toString();
|
|
62
|
+
};
|
|
63
|
+
this.proc.stderr?.on('data', onStderr);
|
|
64
|
+
const onData = (data) => {
|
|
65
|
+
if (buffer.length <= MAX_BUFFER_SIZE) {
|
|
66
|
+
buffer += data.toString();
|
|
67
|
+
}
|
|
68
|
+
if (buffer.length > MAX_BUFFER_SIZE && !capturedError) {
|
|
69
|
+
capturedError = new Error(`stdout buffer exceeded max size of ${MAX_BUFFER_SIZE} bytes`);
|
|
70
|
+
onDataResolve?.();
|
|
71
|
+
}
|
|
72
|
+
onDataResolve?.();
|
|
73
|
+
};
|
|
74
|
+
const onError = (err) => {
|
|
75
|
+
if (!capturedError) {
|
|
76
|
+
capturedError = err;
|
|
77
|
+
onDataResolve?.();
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
const onExit = (code) => {
|
|
81
|
+
if (!finished && !capturedError) {
|
|
82
|
+
capturedError = new Error(`Process exited with code ${code ?? 'unknown'}`);
|
|
83
|
+
onDataResolve?.();
|
|
84
|
+
}
|
|
85
|
+
};
|
|
86
|
+
this.proc.stdout.on('data', onData);
|
|
87
|
+
this.proc.on('error', onError);
|
|
88
|
+
this.proc.on('exit', onExit);
|
|
89
|
+
try {
|
|
90
|
+
while (!finished) {
|
|
91
|
+
if (capturedError) {
|
|
92
|
+
const stderrInfo = stderrBuffer.trim() ? ` (stderr: ${stderrBuffer.slice(0, 500)})` : '';
|
|
93
|
+
yield {
|
|
94
|
+
type: 'error',
|
|
95
|
+
content: `Process error: ${capturedError.message}${stderrInfo}`,
|
|
96
|
+
finishReason: 'error',
|
|
97
|
+
};
|
|
98
|
+
break;
|
|
99
|
+
}
|
|
100
|
+
const newlineIndex = buffer.indexOf('\n');
|
|
101
|
+
if (newlineIndex === -1) {
|
|
102
|
+
const waitForData = new Promise((resolve) => {
|
|
103
|
+
onDataResolve = resolve;
|
|
104
|
+
});
|
|
105
|
+
await waitForData;
|
|
106
|
+
continue;
|
|
107
|
+
}
|
|
108
|
+
const line = buffer.slice(0, newlineIndex);
|
|
109
|
+
buffer = buffer.slice(newlineIndex + 1);
|
|
110
|
+
if (!line.trim())
|
|
111
|
+
continue;
|
|
112
|
+
let parsed;
|
|
113
|
+
try {
|
|
114
|
+
parsed = JSON.parse(line);
|
|
115
|
+
}
|
|
116
|
+
catch {
|
|
117
|
+
continue;
|
|
118
|
+
}
|
|
119
|
+
if (parsed.error) {
|
|
120
|
+
yield {
|
|
121
|
+
type: 'error',
|
|
122
|
+
content: `JSON-RPC error: ${parsed.error.message}`,
|
|
123
|
+
finishReason: 'error',
|
|
124
|
+
};
|
|
125
|
+
finished = true;
|
|
126
|
+
continue;
|
|
127
|
+
}
|
|
128
|
+
if (parsed.method === 'chat/chunk' && parsed.params) {
|
|
129
|
+
const delta = parsed.params.delta;
|
|
130
|
+
if (delta?.content) {
|
|
131
|
+
yield { type: 'text', content: delta.content };
|
|
132
|
+
}
|
|
133
|
+
if (delta?.tool_calls) {
|
|
134
|
+
for (const tc of delta.tool_calls) {
|
|
135
|
+
yield {
|
|
136
|
+
type: 'tool_call',
|
|
137
|
+
toolCall: {
|
|
138
|
+
id: tc.id ?? '',
|
|
139
|
+
name: tc.name ?? '',
|
|
140
|
+
arguments: tc.arguments ?? '',
|
|
141
|
+
},
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
if (parsed.method === 'chat/done' && parsed.params) {
|
|
147
|
+
const reason = parsed.params.finishReason;
|
|
148
|
+
yield {
|
|
149
|
+
type: 'done',
|
|
150
|
+
finishReason: reason === 'stop'
|
|
151
|
+
? 'stop'
|
|
152
|
+
: reason === 'tool_calls'
|
|
153
|
+
? 'tool_calls'
|
|
154
|
+
: reason === 'length'
|
|
155
|
+
? 'length'
|
|
156
|
+
: 'error',
|
|
157
|
+
};
|
|
158
|
+
finished = true;
|
|
159
|
+
}
|
|
160
|
+
if (parsed.id === id && parsed.result) {
|
|
161
|
+
finished = true;
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
finally {
|
|
166
|
+
this.proc?.stdout?.removeListener('data', onData);
|
|
167
|
+
this.proc?.removeListener('error', onError);
|
|
168
|
+
this.proc?.removeListener('exit', onExit);
|
|
169
|
+
this.proc?.stderr?.removeListener('data', onStderr);
|
|
170
|
+
this.busy = false;
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
async dispose() {
|
|
174
|
+
if (this.proc) {
|
|
175
|
+
this.proc.kill();
|
|
176
|
+
this.proc = null;
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { type ChildProcess } from 'node:child_process';
|
|
2
|
+
export interface DaemonManager {
|
|
3
|
+
binaryName: string;
|
|
4
|
+
locate(): Promise<string | null>;
|
|
5
|
+
download(): Promise<string>;
|
|
6
|
+
spawn(binaryPath: string, args: string[]): ChildProcess;
|
|
7
|
+
healthCheck(port?: number): Promise<boolean>;
|
|
8
|
+
}
|
|
9
|
+
export declare function createDaemonManager(config: {
|
|
10
|
+
binaryName: string;
|
|
11
|
+
downloadUrl: string;
|
|
12
|
+
checksum: string;
|
|
13
|
+
knownPaths: string[];
|
|
14
|
+
envVar?: string;
|
|
15
|
+
daemonsDir?: string;
|
|
16
|
+
}): DaemonManager;
|