@ai-ide-bridge/cli 1.1.1 → 1.2.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.js +7 -4
- package/dist/core/index.d.ts +2 -0
- package/dist/core/index.js +1 -0
- package/dist/core/parser.d.ts +5 -5
- package/dist/core/sse/client.d.ts +8 -0
- package/dist/core/sse/client.js +14 -0
- package/dist/core/sse/index.d.ts +3 -0
- package/dist/core/sse/index.js +3 -0
- package/dist/core/sse/parser.d.ts +7 -0
- package/dist/core/sse/parser.js +102 -0
- package/dist/core/sse/transport.d.ts +16 -0
- package/dist/core/sse/transport.js +72 -0
- package/dist/core/types.d.ts +4 -3
- package/dist/core/types.js +1 -1
- package/dist/plugins/copilot/session.js +1 -1
- package/dist/plugins/cursor/plugin.js +38 -11
- package/dist/plugins/cursor/session.d.ts +1 -4
- package/dist/plugins/cursor/session.js +68 -54
- package/package.json +2 -2
- package/src/commands/daemon.ts +7 -9
- package/src/core/index.ts +2 -0
- package/src/core/sse/client.ts +24 -0
- package/src/core/sse/index.ts +3 -0
- package/src/core/sse/parser.ts +109 -0
- package/src/core/sse/transport.ts +111 -0
- package/src/core/types.ts +2 -1
- package/src/plugins/copilot/session.ts +1 -1
- package/src/plugins/cursor/plugin.ts +43 -11
- package/src/plugins/cursor/session.ts +80 -55
- package/dist/plugins/cursor/tools.d.ts +0 -11
- package/dist/plugins/cursor/tools.js +0 -13
- package/src/plugins/cursor/tools.ts +0 -25
package/.turbo/turbo-build.log
CHANGED
package/dist/commands/daemon.js
CHANGED
|
@@ -20,7 +20,8 @@ export async function installDaemonCommand() {
|
|
|
20
20
|
}
|
|
21
21
|
async function installMacOSDaemon() {
|
|
22
22
|
const plistPath = path.join(os.homedir(), 'Library', 'LaunchAgents', `${LABEL}.plist`);
|
|
23
|
-
const
|
|
23
|
+
const binaryPath = process.execPath;
|
|
24
|
+
const uid = os.userInfo().uid;
|
|
24
25
|
const plist = `<?xml version="1.0" encoding="UTF-8"?>
|
|
25
26
|
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
|
26
27
|
<plist version="1.0">
|
|
@@ -29,7 +30,8 @@ async function installMacOSDaemon() {
|
|
|
29
30
|
<string>${LABEL}</string>
|
|
30
31
|
<key>ProgramArguments</key>
|
|
31
32
|
<array>
|
|
32
|
-
<string>${
|
|
33
|
+
<string>${binaryPath}</string>
|
|
34
|
+
<string>start</string>
|
|
33
35
|
</array>
|
|
34
36
|
<key>RunAtLoad</key>
|
|
35
37
|
<true/>
|
|
@@ -52,7 +54,7 @@ async function installMacOSDaemon() {
|
|
|
52
54
|
process.exit(1);
|
|
53
55
|
}
|
|
54
56
|
try {
|
|
55
|
-
execSync(`launchctl bootstrap
|
|
57
|
+
execSync(`launchctl bootstrap gui/${uid} "${plistPath}"`, { stdio: 'inherit' });
|
|
56
58
|
console.log(`Installed LaunchAgent: ${plistPath}`);
|
|
57
59
|
console.log(`Logs: ~/Library/Logs/llm-bridge.{log,err.log}`);
|
|
58
60
|
}
|
|
@@ -112,8 +114,9 @@ export async function uninstallDaemonCommand() {
|
|
|
112
114
|
}
|
|
113
115
|
async function uninstallMacOSDaemon() {
|
|
114
116
|
const plistPath = path.join(os.homedir(), 'Library', 'LaunchAgents', `${LABEL}.plist`);
|
|
117
|
+
const uid = os.userInfo().uid;
|
|
115
118
|
try {
|
|
116
|
-
execSync(`launchctl bootout
|
|
119
|
+
execSync(`launchctl bootout gui/${uid} "${plistPath}" 2>/dev/null || true`, {
|
|
117
120
|
stdio: 'inherit',
|
|
118
121
|
});
|
|
119
122
|
}
|
package/dist/core/index.d.ts
CHANGED
|
@@ -7,3 +7,5 @@ export { PluginRegistry } from './registry.js';
|
|
|
7
7
|
export { loadConfig, saveConfig, configPath } from './config.js';
|
|
8
8
|
export { createDaemonManager, type DaemonManager } from './daemon.js';
|
|
9
9
|
export { DaemonBridgeSession } from './daemon-session.js';
|
|
10
|
+
export { parseSSE, createTransport, createStream } from './sse/index.js';
|
|
11
|
+
export type { SSEEvent, TransportOptions, Transport, StreamOptions } from './sse/index.js';
|
package/dist/core/index.js
CHANGED
|
@@ -7,3 +7,4 @@ export { PluginRegistry } from './registry.js';
|
|
|
7
7
|
export { loadConfig, saveConfig, configPath } from './config.js';
|
|
8
8
|
export { createDaemonManager } from './daemon.js';
|
|
9
9
|
export { DaemonBridgeSession } from './daemon-session.js';
|
|
10
|
+
export { parseSSE, createTransport, createStream } from './sse/index.js';
|
package/dist/core/parser.d.ts
CHANGED
|
@@ -4,7 +4,7 @@ declare const ChatRequestSchema: z.ZodObject<{
|
|
|
4
4
|
model: z.ZodString;
|
|
5
5
|
messages: z.ZodArray<z.ZodObject<{
|
|
6
6
|
role: z.ZodEnum<["system", "user", "assistant", "tool", "function"]>;
|
|
7
|
-
content: z.ZodOptional<z.ZodNullable<z.
|
|
7
|
+
content: z.ZodOptional<z.ZodNullable<z.ZodUnknown>>;
|
|
8
8
|
name: z.ZodOptional<z.ZodString>;
|
|
9
9
|
tool_call_id: z.ZodOptional<z.ZodString>;
|
|
10
10
|
tool_calls: z.ZodOptional<z.ZodArray<z.ZodObject<{
|
|
@@ -37,7 +37,7 @@ declare const ChatRequestSchema: z.ZodObject<{
|
|
|
37
37
|
}>, "many">>;
|
|
38
38
|
}, "strip", z.ZodTypeAny, {
|
|
39
39
|
role: "function" | "system" | "user" | "assistant" | "tool";
|
|
40
|
-
content?:
|
|
40
|
+
content?: unknown;
|
|
41
41
|
name?: string | undefined;
|
|
42
42
|
tool_call_id?: string | undefined;
|
|
43
43
|
tool_calls?: {
|
|
@@ -50,7 +50,7 @@ declare const ChatRequestSchema: z.ZodObject<{
|
|
|
50
50
|
}[] | undefined;
|
|
51
51
|
}, {
|
|
52
52
|
role: "function" | "system" | "user" | "assistant" | "tool";
|
|
53
|
-
content?:
|
|
53
|
+
content?: unknown;
|
|
54
54
|
name?: string | undefined;
|
|
55
55
|
tool_call_id?: string | undefined;
|
|
56
56
|
tool_calls?: {
|
|
@@ -98,7 +98,7 @@ declare const ChatRequestSchema: z.ZodObject<{
|
|
|
98
98
|
model: string;
|
|
99
99
|
messages: {
|
|
100
100
|
role: "function" | "system" | "user" | "assistant" | "tool";
|
|
101
|
-
content?:
|
|
101
|
+
content?: unknown;
|
|
102
102
|
name?: string | undefined;
|
|
103
103
|
tool_call_id?: string | undefined;
|
|
104
104
|
tool_calls?: {
|
|
@@ -124,7 +124,7 @@ declare const ChatRequestSchema: z.ZodObject<{
|
|
|
124
124
|
model: string;
|
|
125
125
|
messages: {
|
|
126
126
|
role: "function" | "system" | "user" | "assistant" | "tool";
|
|
127
|
-
content?:
|
|
127
|
+
content?: unknown;
|
|
128
128
|
name?: string | undefined;
|
|
129
129
|
tool_call_id?: string | undefined;
|
|
130
130
|
tool_calls?: {
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { StreamChunk } from '../types.js';
|
|
2
|
+
import { TransportOptions } from './transport.js';
|
|
3
|
+
import { SSEEvent } from './parser.js';
|
|
4
|
+
export interface StreamOptions extends Omit<TransportOptions, 'signal'> {
|
|
5
|
+
parseEvent(event: SSEEvent): StreamChunk | null;
|
|
6
|
+
signal?: AbortSignal;
|
|
7
|
+
}
|
|
8
|
+
export declare function createStream(opts: StreamOptions): AsyncIterable<StreamChunk>;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { createTransport } from './transport.js';
|
|
2
|
+
export async function* createStream(opts) {
|
|
3
|
+
const { parseEvent, signal, ...transportOpts } = opts;
|
|
4
|
+
const transport = createTransport({ ...transportOpts, signal });
|
|
5
|
+
if (signal) {
|
|
6
|
+
signal.addEventListener('abort', () => transport.abort());
|
|
7
|
+
}
|
|
8
|
+
for await (const event of transport.stream()) {
|
|
9
|
+
const chunk = parseEvent(event);
|
|
10
|
+
if (chunk) {
|
|
11
|
+
yield chunk;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
}
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
export async function* parseSSE(stream) {
|
|
2
|
+
const decoder = new TextDecoder();
|
|
3
|
+
const reader = stream.getReader();
|
|
4
|
+
let buffer = '';
|
|
5
|
+
let eventId;
|
|
6
|
+
let eventType;
|
|
7
|
+
let eventData = '';
|
|
8
|
+
let retryMs;
|
|
9
|
+
try {
|
|
10
|
+
while (true) {
|
|
11
|
+
const { done, value } = await reader.read();
|
|
12
|
+
if (done)
|
|
13
|
+
break;
|
|
14
|
+
buffer += decoder.decode(value, { stream: true });
|
|
15
|
+
let lineEnd = buffer.indexOf('\n');
|
|
16
|
+
while (lineEnd !== -1) {
|
|
17
|
+
const line = buffer.slice(0, lineEnd).trimEnd();
|
|
18
|
+
buffer = buffer.slice(lineEnd + 1);
|
|
19
|
+
if (line.startsWith(':')) {
|
|
20
|
+
// Comment line, skip
|
|
21
|
+
}
|
|
22
|
+
else if (line === '') {
|
|
23
|
+
// Empty line = event boundary
|
|
24
|
+
if (eventData || eventType || eventId) {
|
|
25
|
+
yield {
|
|
26
|
+
id: eventId,
|
|
27
|
+
event: eventType,
|
|
28
|
+
data: eventData,
|
|
29
|
+
retry: retryMs,
|
|
30
|
+
};
|
|
31
|
+
eventId = undefined;
|
|
32
|
+
eventType = undefined;
|
|
33
|
+
eventData = '';
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
else {
|
|
37
|
+
const colonIndex = line.indexOf(':');
|
|
38
|
+
const field = colonIndex === -1 ? line : line.slice(0, colonIndex);
|
|
39
|
+
const value = colonIndex === -1 ? '' : line.slice(colonIndex + 1).trimStart();
|
|
40
|
+
switch (field) {
|
|
41
|
+
case 'id':
|
|
42
|
+
eventId = value;
|
|
43
|
+
break;
|
|
44
|
+
case 'event':
|
|
45
|
+
eventType = value;
|
|
46
|
+
break;
|
|
47
|
+
case 'data':
|
|
48
|
+
eventData = eventData ? eventData + '\n' + value : value;
|
|
49
|
+
break;
|
|
50
|
+
case 'retry': {
|
|
51
|
+
const parsed = parseInt(value, 10);
|
|
52
|
+
if (!isNaN(parsed))
|
|
53
|
+
retryMs = parsed;
|
|
54
|
+
break;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
lineEnd = buffer.indexOf('\n');
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
// Flush remaining buffer
|
|
62
|
+
if (buffer.trim()) {
|
|
63
|
+
const line = buffer.trim();
|
|
64
|
+
if (line.startsWith(':')) {
|
|
65
|
+
// Comment, skip
|
|
66
|
+
}
|
|
67
|
+
else if (line === '') {
|
|
68
|
+
if (eventData || eventType || eventId) {
|
|
69
|
+
yield { id: eventId, event: eventType, data: eventData, retry: retryMs };
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
else {
|
|
73
|
+
const colonIndex = line.indexOf(':');
|
|
74
|
+
const field = colonIndex === -1 ? line : line.slice(0, colonIndex);
|
|
75
|
+
const value = colonIndex === -1 ? '' : line.slice(colonIndex + 1).trimStart();
|
|
76
|
+
switch (field) {
|
|
77
|
+
case 'id':
|
|
78
|
+
eventId = value;
|
|
79
|
+
break;
|
|
80
|
+
case 'event':
|
|
81
|
+
eventType = value;
|
|
82
|
+
break;
|
|
83
|
+
case 'data':
|
|
84
|
+
eventData = eventData ? eventData + '\n' + value : value;
|
|
85
|
+
break;
|
|
86
|
+
case 'retry': {
|
|
87
|
+
const parsed = parseInt(value, 10);
|
|
88
|
+
if (!isNaN(parsed))
|
|
89
|
+
retryMs = parsed;
|
|
90
|
+
break;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
if (eventData || eventType || eventId) {
|
|
94
|
+
yield { id: eventId, event: eventType, data: eventData, retry: retryMs };
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
finally {
|
|
100
|
+
reader.releaseLock();
|
|
101
|
+
}
|
|
102
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { SSEEvent } from './parser.js';
|
|
2
|
+
export interface TransportOptions {
|
|
3
|
+
url: string;
|
|
4
|
+
method?: string;
|
|
5
|
+
headers?: Record<string, string>;
|
|
6
|
+
body?: string;
|
|
7
|
+
signal?: AbortSignal;
|
|
8
|
+
timeout?: number;
|
|
9
|
+
maxRetries?: number;
|
|
10
|
+
retryDelay?: number;
|
|
11
|
+
}
|
|
12
|
+
export interface Transport {
|
|
13
|
+
stream(): AsyncIterable<SSEEvent>;
|
|
14
|
+
abort(): void;
|
|
15
|
+
}
|
|
16
|
+
export declare function createTransport(opts: TransportOptions): Transport;
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { parseSSE } from './parser.js';
|
|
2
|
+
export function createTransport(opts) {
|
|
3
|
+
const { url, method = 'POST', headers = {}, body, signal, timeout = 30000, maxRetries = 3, retryDelay = 1000, } = opts;
|
|
4
|
+
let aborted = false;
|
|
5
|
+
return {
|
|
6
|
+
stream() {
|
|
7
|
+
return streamWithRetry();
|
|
8
|
+
},
|
|
9
|
+
abort() {
|
|
10
|
+
aborted = true;
|
|
11
|
+
},
|
|
12
|
+
};
|
|
13
|
+
async function* streamWithRetry() {
|
|
14
|
+
let lastEventId;
|
|
15
|
+
let retries = 0;
|
|
16
|
+
while (!aborted && retries <= maxRetries) {
|
|
17
|
+
const requestHeaders = { ...headers };
|
|
18
|
+
if (lastEventId) {
|
|
19
|
+
requestHeaders['Last-Event-ID'] = lastEventId;
|
|
20
|
+
}
|
|
21
|
+
try {
|
|
22
|
+
const controller = new AbortController();
|
|
23
|
+
const timeoutId = setTimeout(() => controller.abort(), timeout);
|
|
24
|
+
if (signal) {
|
|
25
|
+
signal.addEventListener('abort', () => controller.abort());
|
|
26
|
+
}
|
|
27
|
+
const response = await fetch(url, {
|
|
28
|
+
method,
|
|
29
|
+
headers: requestHeaders,
|
|
30
|
+
body,
|
|
31
|
+
signal: controller.signal,
|
|
32
|
+
});
|
|
33
|
+
clearTimeout(timeoutId);
|
|
34
|
+
if (!response.ok) {
|
|
35
|
+
if (response.status >= 400 && response.status < 500) {
|
|
36
|
+
const errorText = await response.text().catch(() => '');
|
|
37
|
+
yield { event: 'error', data: `HTTP ${response.status}: ${errorText}` };
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
throw new Error(`HTTP ${response.status}`);
|
|
41
|
+
}
|
|
42
|
+
if (!response.body) {
|
|
43
|
+
yield { event: 'error', data: 'No response body' };
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
retries = 0;
|
|
47
|
+
for await (const event of parseSSE(response.body)) {
|
|
48
|
+
if (event.id)
|
|
49
|
+
lastEventId = event.id;
|
|
50
|
+
yield event;
|
|
51
|
+
}
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
catch (e) {
|
|
55
|
+
if (aborted)
|
|
56
|
+
return;
|
|
57
|
+
const isAbort = e instanceof DOMException && e.name === 'AbortError';
|
|
58
|
+
if (isAbort && retries >= maxRetries) {
|
|
59
|
+
yield { event: 'error', data: 'Request timed out' };
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
retries++;
|
|
63
|
+
if (retries > maxRetries) {
|
|
64
|
+
yield { event: 'error', data: e instanceof Error ? e.message : String(e) };
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
const delay = retryDelay * Math.pow(2, retries - 1);
|
|
68
|
+
await new Promise((resolve) => setTimeout(resolve, delay));
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
}
|
package/dist/core/types.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { z } from 'zod';
|
|
2
2
|
export declare const MessageSchema: z.ZodObject<{
|
|
3
3
|
role: z.ZodEnum<["system", "user", "assistant", "tool", "function"]>;
|
|
4
|
-
content: z.ZodOptional<z.ZodNullable<z.
|
|
4
|
+
content: z.ZodOptional<z.ZodNullable<z.ZodUnknown>>;
|
|
5
5
|
name: z.ZodOptional<z.ZodString>;
|
|
6
6
|
tool_call_id: z.ZodOptional<z.ZodString>;
|
|
7
7
|
tool_calls: z.ZodOptional<z.ZodArray<z.ZodObject<{
|
|
@@ -34,7 +34,7 @@ export declare const MessageSchema: z.ZodObject<{
|
|
|
34
34
|
}>, "many">>;
|
|
35
35
|
}, "strip", z.ZodTypeAny, {
|
|
36
36
|
role: "function" | "system" | "user" | "assistant" | "tool";
|
|
37
|
-
content?:
|
|
37
|
+
content?: unknown;
|
|
38
38
|
name?: string | undefined;
|
|
39
39
|
tool_call_id?: string | undefined;
|
|
40
40
|
tool_calls?: {
|
|
@@ -47,7 +47,7 @@ export declare const MessageSchema: z.ZodObject<{
|
|
|
47
47
|
}[] | undefined;
|
|
48
48
|
}, {
|
|
49
49
|
role: "function" | "system" | "user" | "assistant" | "tool";
|
|
50
|
-
content?:
|
|
50
|
+
content?: unknown;
|
|
51
51
|
name?: string | undefined;
|
|
52
52
|
tool_call_id?: string | undefined;
|
|
53
53
|
tool_calls?: {
|
|
@@ -140,6 +140,7 @@ export interface StreamChunk {
|
|
|
140
140
|
export interface BridgePlugin {
|
|
141
141
|
name: string;
|
|
142
142
|
version: string;
|
|
143
|
+
oauthProvider?: import('@ai-ide-bridge/oauth').OAuthProvider;
|
|
143
144
|
authenticate(config: Record<string, string>): Promise<boolean>;
|
|
144
145
|
listModels(config: Record<string, string>): Promise<ModelInfo[]>;
|
|
145
146
|
createSession(config: Record<string, string>, model: string): Promise<BridgeSession>;
|
package/dist/core/types.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { z } from 'zod';
|
|
2
2
|
export const MessageSchema = z.object({
|
|
3
3
|
role: z.enum(['system', 'user', 'assistant', 'tool', 'function']),
|
|
4
|
-
content: z.
|
|
4
|
+
content: z.unknown().nullable().optional(),
|
|
5
5
|
name: z.string().optional(),
|
|
6
6
|
tool_call_id: z.string().optional(),
|
|
7
7
|
tool_calls: z
|
|
@@ -12,7 +12,7 @@ export class CopilotBridgeSession {
|
|
|
12
12
|
model: this.modelId,
|
|
13
13
|
messages: messages.map((m) => ({
|
|
14
14
|
role: m.role,
|
|
15
|
-
content: m.content
|
|
15
|
+
content: m.content,
|
|
16
16
|
...(m.tool_calls && { tool_calls: m.tool_calls }),
|
|
17
17
|
...(m.tool_call_id && { tool_call_id: m.tool_call_id }),
|
|
18
18
|
})),
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { Cursor } from '@cursor/sdk';
|
|
2
1
|
import { CursorBridgeSession } from './session.js';
|
|
2
|
+
const CURSOR_API_BASE = 'https://api2.cursor.sh';
|
|
3
3
|
export class CursorBridgePlugin {
|
|
4
4
|
name = 'cursor';
|
|
5
5
|
version = '2.0.0';
|
|
@@ -8,8 +8,10 @@ export class CursorBridgePlugin {
|
|
|
8
8
|
if (!apiKey)
|
|
9
9
|
return false;
|
|
10
10
|
try {
|
|
11
|
-
await
|
|
12
|
-
|
|
11
|
+
const response = await fetch(`${CURSOR_API_BASE}/auth/whoami`, {
|
|
12
|
+
headers: { Authorization: `Bearer ${apiKey}` },
|
|
13
|
+
});
|
|
14
|
+
return response.ok;
|
|
13
15
|
}
|
|
14
16
|
catch {
|
|
15
17
|
return false;
|
|
@@ -19,18 +21,43 @@ export class CursorBridgePlugin {
|
|
|
19
21
|
const apiKey = config.CURSOR_API_KEY;
|
|
20
22
|
if (!apiKey)
|
|
21
23
|
throw new Error('Missing CURSOR_API_KEY');
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
24
|
+
try {
|
|
25
|
+
const response = await fetch(`${CURSOR_API_BASE}/models`, {
|
|
26
|
+
headers: { Authorization: `Bearer ${apiKey}` },
|
|
27
|
+
});
|
|
28
|
+
if (!response.ok) {
|
|
29
|
+
return defaultModels();
|
|
30
|
+
}
|
|
31
|
+
const data = await response.json();
|
|
32
|
+
if (Array.isArray(data)) {
|
|
33
|
+
return data.map((m) => ({
|
|
34
|
+
id: m.id ?? m.name ?? 'unknown',
|
|
35
|
+
name: m.name ?? m.id ?? 'unknown',
|
|
36
|
+
capabilities: { streaming: true, tools: true },
|
|
37
|
+
}));
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
catch {
|
|
41
|
+
// Fall through to defaults
|
|
42
|
+
}
|
|
43
|
+
return defaultModels();
|
|
28
44
|
}
|
|
29
45
|
async createSession(config, model) {
|
|
30
46
|
const apiKey = config.CURSOR_API_KEY;
|
|
31
47
|
if (!apiKey)
|
|
32
48
|
throw new Error('Missing CURSOR_API_KEY');
|
|
33
|
-
|
|
34
|
-
return new CursorBridgeSession(apiKey, model, cwd);
|
|
49
|
+
return new CursorBridgeSession(apiKey, model);
|
|
35
50
|
}
|
|
36
51
|
}
|
|
52
|
+
function defaultModels() {
|
|
53
|
+
return [
|
|
54
|
+
{ id: 'composer-2', name: 'Composer 2', capabilities: { streaming: true, tools: true } },
|
|
55
|
+
{ id: 'composer-2.5', name: 'Composer 2.5', capabilities: { streaming: true, tools: true } },
|
|
56
|
+
{ id: 'gpt-4o', name: 'GPT-4o', capabilities: { streaming: true, tools: true } },
|
|
57
|
+
{
|
|
58
|
+
id: 'claude-3.5-sonnet',
|
|
59
|
+
name: 'Claude 3.5 Sonnet',
|
|
60
|
+
capabilities: { streaming: true, tools: true },
|
|
61
|
+
},
|
|
62
|
+
];
|
|
63
|
+
}
|
|
@@ -1,11 +1,8 @@
|
|
|
1
1
|
import type { BridgeSession, Message, ToolDefinition, StreamChunk } from '../../core/index.js';
|
|
2
2
|
export declare class CursorBridgeSession implements BridgeSession {
|
|
3
|
-
private agent;
|
|
4
3
|
private apiKey;
|
|
5
4
|
private modelId;
|
|
6
|
-
|
|
7
|
-
constructor(apiKey: string, modelId: string, cwd?: string);
|
|
5
|
+
constructor(apiKey: string, modelId: string);
|
|
8
6
|
send(messages: Message[], tools?: ToolDefinition[]): AsyncIterable<StreamChunk>;
|
|
9
7
|
dispose(): Promise<void>;
|
|
10
|
-
private buildPrompt;
|
|
11
8
|
}
|
|
@@ -1,69 +1,83 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
1
|
+
import { createStream } from '../../core/index.js';
|
|
2
|
+
const CURSOR_API_BASE = 'https://api2.cursor.sh';
|
|
3
3
|
export class CursorBridgeSession {
|
|
4
|
-
agent = null;
|
|
5
4
|
apiKey;
|
|
6
5
|
modelId;
|
|
7
|
-
|
|
8
|
-
constructor(apiKey, modelId, cwd = process.cwd()) {
|
|
6
|
+
constructor(apiKey, modelId) {
|
|
9
7
|
this.apiKey = apiKey;
|
|
10
8
|
this.modelId = modelId;
|
|
11
|
-
this.cwd = cwd;
|
|
12
9
|
}
|
|
13
10
|
async *send(messages, tools) {
|
|
14
|
-
const
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
return;
|
|
39
|
-
}
|
|
40
|
-
yield { type: 'text', content: result.result ?? '', finishReason: 'stop' };
|
|
41
|
-
}
|
|
42
|
-
catch (e) {
|
|
43
|
-
const msg = e instanceof Error ? e.message : String(e);
|
|
44
|
-
yield { type: 'error', content: msg, finishReason: 'error' };
|
|
11
|
+
const body = JSON.stringify({
|
|
12
|
+
model: this.modelId,
|
|
13
|
+
messages: messages.map((m) => ({
|
|
14
|
+
role: m.role,
|
|
15
|
+
content: m.content,
|
|
16
|
+
...(m.tool_calls && { tool_calls: m.tool_calls }),
|
|
17
|
+
...(m.tool_call_id && { tool_call_id: m.tool_call_id }),
|
|
18
|
+
...(m.name && { name: m.name }),
|
|
19
|
+
})),
|
|
20
|
+
...(tools && { tools: tools.map((t) => t) }),
|
|
21
|
+
stream: true,
|
|
22
|
+
});
|
|
23
|
+
for await (const chunk of createStream({
|
|
24
|
+
url: `${CURSOR_API_BASE}/aiserver.v1.AiService/StreamChat`,
|
|
25
|
+
method: 'POST',
|
|
26
|
+
headers: {
|
|
27
|
+
'Content-Type': 'application/json',
|
|
28
|
+
Authorization: `Bearer ${this.apiKey}`,
|
|
29
|
+
Accept: 'text/event-stream',
|
|
30
|
+
},
|
|
31
|
+
body,
|
|
32
|
+
parseEvent: parseCursorEvent,
|
|
33
|
+
})) {
|
|
34
|
+
yield chunk;
|
|
45
35
|
}
|
|
46
36
|
}
|
|
47
37
|
async dispose() {
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
38
|
+
// No state to dispose with direct HTTP
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
function parseCursorEvent(event) {
|
|
42
|
+
if (event.event === 'error') {
|
|
43
|
+
return { type: 'error', content: event.data, finishReason: 'error' };
|
|
44
|
+
}
|
|
45
|
+
if (event.data === '' || event.data === '[DONE]') {
|
|
46
|
+
return { type: 'done', finishReason: 'stop' };
|
|
47
|
+
}
|
|
48
|
+
try {
|
|
49
|
+
const json = JSON.parse(event.data);
|
|
50
|
+
if (json.error) {
|
|
51
|
+
return {
|
|
52
|
+
type: 'error',
|
|
53
|
+
content: json.error.message ?? JSON.stringify(json.error),
|
|
54
|
+
finishReason: 'error',
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
if (json.choices?.[0]?.delta?.content) {
|
|
58
|
+
return { type: 'text', content: json.choices[0].delta.content };
|
|
59
|
+
}
|
|
60
|
+
if (json.choices?.[0]?.delta?.tool_calls) {
|
|
61
|
+
for (const tc of json.choices[0].delta.tool_calls) {
|
|
62
|
+
return {
|
|
63
|
+
type: 'tool_call',
|
|
64
|
+
toolCall: {
|
|
65
|
+
id: tc.id ?? `tc-${Date.now()}`,
|
|
66
|
+
name: tc.function?.name ?? '',
|
|
67
|
+
arguments: tc.function?.arguments ?? '',
|
|
68
|
+
},
|
|
69
|
+
};
|
|
54
70
|
}
|
|
55
|
-
this.agent = null;
|
|
56
71
|
}
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
if (!text)
|
|
63
|
-
continue;
|
|
64
|
-
const label = m.role === 'tool' ? `tool (${m.tool_call_id ?? m.name ?? 'result'})` : m.role;
|
|
65
|
-
blocks.push(`[${label}]\n${text}`);
|
|
72
|
+
if (json.choices?.[0]?.finish_reason) {
|
|
73
|
+
return {
|
|
74
|
+
type: 'done',
|
|
75
|
+
finishReason: json.choices[0].finish_reason === 'stop' ? 'stop' : 'tool_calls',
|
|
76
|
+
};
|
|
66
77
|
}
|
|
67
|
-
return `\nFollow this conversation transcript and reply as the assistant.\n\n${blocks.join('\n\n---\n\n')}\n`;
|
|
68
78
|
}
|
|
79
|
+
catch {
|
|
80
|
+
return null;
|
|
81
|
+
}
|
|
82
|
+
return null;
|
|
69
83
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ai-ide-bridge/cli",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.2.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"bin": {
|
|
6
6
|
"llm-bridge": "./dist/index.js"
|
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
"start": "node dist/index.js"
|
|
15
15
|
},
|
|
16
16
|
"dependencies": {
|
|
17
|
-
"@
|
|
17
|
+
"@ai-ide-bridge/oauth": "workspace:*",
|
|
18
18
|
"zod": "^3.25.76"
|
|
19
19
|
},
|
|
20
20
|
"devDependencies": {
|
package/src/commands/daemon.ts
CHANGED
|
@@ -21,12 +21,8 @@ export async function installDaemonCommand(): Promise<void> {
|
|
|
21
21
|
|
|
22
22
|
async function installMacOSDaemon(): Promise<void> {
|
|
23
23
|
const plistPath = path.join(os.homedir(), 'Library', 'LaunchAgents', `${LABEL}.plist`);
|
|
24
|
-
const
|
|
25
|
-
|
|
26
|
-
'..',
|
|
27
|
-
'scripts',
|
|
28
|
-
'llm-bridge-daemon.sh',
|
|
29
|
-
);
|
|
24
|
+
const binaryPath = process.execPath;
|
|
25
|
+
const uid = os.userInfo().uid;
|
|
30
26
|
|
|
31
27
|
const plist = `<?xml version="1.0" encoding="UTF-8"?>
|
|
32
28
|
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
|
@@ -36,7 +32,8 @@ async function installMacOSDaemon(): Promise<void> {
|
|
|
36
32
|
<string>${LABEL}</string>
|
|
37
33
|
<key>ProgramArguments</key>
|
|
38
34
|
<array>
|
|
39
|
-
<string>${
|
|
35
|
+
<string>${binaryPath}</string>
|
|
36
|
+
<string>start</string>
|
|
40
37
|
</array>
|
|
41
38
|
<key>RunAtLoad</key>
|
|
42
39
|
<true/>
|
|
@@ -60,7 +57,7 @@ async function installMacOSDaemon(): Promise<void> {
|
|
|
60
57
|
}
|
|
61
58
|
|
|
62
59
|
try {
|
|
63
|
-
execSync(`launchctl bootstrap
|
|
60
|
+
execSync(`launchctl bootstrap gui/${uid} "${plistPath}"`, { stdio: 'inherit' });
|
|
64
61
|
console.log(`Installed LaunchAgent: ${plistPath}`);
|
|
65
62
|
console.log(`Logs: ~/Library/Logs/llm-bridge.{log,err.log}`);
|
|
66
63
|
} catch (e) {
|
|
@@ -122,9 +119,10 @@ export async function uninstallDaemonCommand(): Promise<void> {
|
|
|
122
119
|
|
|
123
120
|
async function uninstallMacOSDaemon(): Promise<void> {
|
|
124
121
|
const plistPath = path.join(os.homedir(), 'Library', 'LaunchAgents', `${LABEL}.plist`);
|
|
122
|
+
const uid = os.userInfo().uid;
|
|
125
123
|
|
|
126
124
|
try {
|
|
127
|
-
execSync(`launchctl bootout
|
|
125
|
+
execSync(`launchctl bootout gui/${uid} "${plistPath}" 2>/dev/null || true`, {
|
|
128
126
|
stdio: 'inherit',
|
|
129
127
|
});
|
|
130
128
|
} catch {
|
package/src/core/index.ts
CHANGED
|
@@ -7,3 +7,5 @@ export { PluginRegistry } from './registry.js';
|
|
|
7
7
|
export { loadConfig, saveConfig, configPath } from './config.js';
|
|
8
8
|
export { createDaemonManager, type DaemonManager } from './daemon.js';
|
|
9
9
|
export { DaemonBridgeSession } from './daemon-session.js';
|
|
10
|
+
export { parseSSE, createTransport, createStream } from './sse/index.js';
|
|
11
|
+
export type { SSEEvent, TransportOptions, Transport, StreamOptions } from './sse/index.js';
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { StreamChunk } from '../types.js';
|
|
2
|
+
import { createTransport, TransportOptions } from './transport.js';
|
|
3
|
+
import { SSEEvent } from './parser.js';
|
|
4
|
+
|
|
5
|
+
export interface StreamOptions extends Omit<TransportOptions, 'signal'> {
|
|
6
|
+
parseEvent(event: SSEEvent): StreamChunk | null;
|
|
7
|
+
signal?: AbortSignal;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export async function* createStream(opts: StreamOptions): AsyncIterable<StreamChunk> {
|
|
11
|
+
const { parseEvent, signal, ...transportOpts } = opts;
|
|
12
|
+
const transport = createTransport({ ...transportOpts, signal });
|
|
13
|
+
|
|
14
|
+
if (signal) {
|
|
15
|
+
signal.addEventListener('abort', () => transport.abort());
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
for await (const event of transport.stream()) {
|
|
19
|
+
const chunk = parseEvent(event);
|
|
20
|
+
if (chunk) {
|
|
21
|
+
yield chunk;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
}
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
export interface SSEEvent {
|
|
2
|
+
id?: string;
|
|
3
|
+
event?: string;
|
|
4
|
+
data: string;
|
|
5
|
+
retry?: number;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export async function* parseSSE(stream: ReadableStream<Uint8Array>): AsyncIterable<SSEEvent> {
|
|
9
|
+
const decoder = new TextDecoder();
|
|
10
|
+
const reader = stream.getReader();
|
|
11
|
+
|
|
12
|
+
let buffer = '';
|
|
13
|
+
let eventId: string | undefined;
|
|
14
|
+
let eventType: string | undefined;
|
|
15
|
+
let eventData = '';
|
|
16
|
+
let retryMs: number | undefined;
|
|
17
|
+
|
|
18
|
+
try {
|
|
19
|
+
while (true) {
|
|
20
|
+
const { done, value } = await reader.read();
|
|
21
|
+
if (done) break;
|
|
22
|
+
|
|
23
|
+
buffer += decoder.decode(value, { stream: true });
|
|
24
|
+
|
|
25
|
+
let lineEnd = buffer.indexOf('\n');
|
|
26
|
+
while (lineEnd !== -1) {
|
|
27
|
+
const line = buffer.slice(0, lineEnd).trimEnd();
|
|
28
|
+
buffer = buffer.slice(lineEnd + 1);
|
|
29
|
+
|
|
30
|
+
if (line.startsWith(':')) {
|
|
31
|
+
// Comment line, skip
|
|
32
|
+
} else if (line === '') {
|
|
33
|
+
// Empty line = event boundary
|
|
34
|
+
if (eventData || eventType || eventId) {
|
|
35
|
+
yield {
|
|
36
|
+
id: eventId,
|
|
37
|
+
event: eventType,
|
|
38
|
+
data: eventData,
|
|
39
|
+
retry: retryMs,
|
|
40
|
+
};
|
|
41
|
+
eventId = undefined;
|
|
42
|
+
eventType = undefined;
|
|
43
|
+
eventData = '';
|
|
44
|
+
}
|
|
45
|
+
} else {
|
|
46
|
+
const colonIndex = line.indexOf(':');
|
|
47
|
+
const field = colonIndex === -1 ? line : line.slice(0, colonIndex);
|
|
48
|
+
const value = colonIndex === -1 ? '' : line.slice(colonIndex + 1).trimStart();
|
|
49
|
+
|
|
50
|
+
switch (field) {
|
|
51
|
+
case 'id':
|
|
52
|
+
eventId = value;
|
|
53
|
+
break;
|
|
54
|
+
case 'event':
|
|
55
|
+
eventType = value;
|
|
56
|
+
break;
|
|
57
|
+
case 'data':
|
|
58
|
+
eventData = eventData ? eventData + '\n' + value : value;
|
|
59
|
+
break;
|
|
60
|
+
case 'retry': {
|
|
61
|
+
const parsed = parseInt(value, 10);
|
|
62
|
+
if (!isNaN(parsed)) retryMs = parsed;
|
|
63
|
+
break;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
lineEnd = buffer.indexOf('\n');
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// Flush remaining buffer
|
|
73
|
+
if (buffer.trim()) {
|
|
74
|
+
const line = buffer.trim();
|
|
75
|
+
if (line.startsWith(':')) {
|
|
76
|
+
// Comment, skip
|
|
77
|
+
} else if (line === '') {
|
|
78
|
+
if (eventData || eventType || eventId) {
|
|
79
|
+
yield { id: eventId, event: eventType, data: eventData, retry: retryMs };
|
|
80
|
+
}
|
|
81
|
+
} else {
|
|
82
|
+
const colonIndex = line.indexOf(':');
|
|
83
|
+
const field = colonIndex === -1 ? line : line.slice(0, colonIndex);
|
|
84
|
+
const value = colonIndex === -1 ? '' : line.slice(colonIndex + 1).trimStart();
|
|
85
|
+
switch (field) {
|
|
86
|
+
case 'id':
|
|
87
|
+
eventId = value;
|
|
88
|
+
break;
|
|
89
|
+
case 'event':
|
|
90
|
+
eventType = value;
|
|
91
|
+
break;
|
|
92
|
+
case 'data':
|
|
93
|
+
eventData = eventData ? eventData + '\n' + value : value;
|
|
94
|
+
break;
|
|
95
|
+
case 'retry': {
|
|
96
|
+
const parsed = parseInt(value, 10);
|
|
97
|
+
if (!isNaN(parsed)) retryMs = parsed;
|
|
98
|
+
break;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
if (eventData || eventType || eventId) {
|
|
102
|
+
yield { id: eventId, event: eventType, data: eventData, retry: retryMs };
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
} finally {
|
|
107
|
+
reader.releaseLock();
|
|
108
|
+
}
|
|
109
|
+
}
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import { parseSSE, SSEEvent } from './parser.js';
|
|
2
|
+
|
|
3
|
+
export interface TransportOptions {
|
|
4
|
+
url: string;
|
|
5
|
+
method?: string;
|
|
6
|
+
headers?: Record<string, string>;
|
|
7
|
+
body?: string;
|
|
8
|
+
signal?: AbortSignal;
|
|
9
|
+
timeout?: number;
|
|
10
|
+
maxRetries?: number;
|
|
11
|
+
retryDelay?: number;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export interface Transport {
|
|
15
|
+
stream(): AsyncIterable<SSEEvent>;
|
|
16
|
+
abort(): void;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function createTransport(opts: TransportOptions): Transport {
|
|
20
|
+
const {
|
|
21
|
+
url,
|
|
22
|
+
method = 'POST',
|
|
23
|
+
headers = {},
|
|
24
|
+
body,
|
|
25
|
+
signal,
|
|
26
|
+
timeout = 30000,
|
|
27
|
+
maxRetries = 3,
|
|
28
|
+
retryDelay = 1000,
|
|
29
|
+
} = opts;
|
|
30
|
+
|
|
31
|
+
let aborted = false;
|
|
32
|
+
|
|
33
|
+
return {
|
|
34
|
+
stream(): AsyncIterable<SSEEvent> {
|
|
35
|
+
return streamWithRetry();
|
|
36
|
+
},
|
|
37
|
+
abort(): void {
|
|
38
|
+
aborted = true;
|
|
39
|
+
},
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
async function* streamWithRetry(): AsyncIterable<SSEEvent> {
|
|
43
|
+
let lastEventId: string | undefined;
|
|
44
|
+
let retries = 0;
|
|
45
|
+
|
|
46
|
+
while (!aborted && retries <= maxRetries) {
|
|
47
|
+
const requestHeaders: Record<string, string> = { ...headers };
|
|
48
|
+
if (lastEventId) {
|
|
49
|
+
requestHeaders['Last-Event-ID'] = lastEventId;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
try {
|
|
53
|
+
const controller = new AbortController();
|
|
54
|
+
const timeoutId = setTimeout(() => controller.abort(), timeout);
|
|
55
|
+
|
|
56
|
+
if (signal) {
|
|
57
|
+
signal.addEventListener('abort', () => controller.abort());
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const response = await fetch(url, {
|
|
61
|
+
method,
|
|
62
|
+
headers: requestHeaders,
|
|
63
|
+
body,
|
|
64
|
+
signal: controller.signal,
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
clearTimeout(timeoutId);
|
|
68
|
+
|
|
69
|
+
if (!response.ok) {
|
|
70
|
+
if (response.status >= 400 && response.status < 500) {
|
|
71
|
+
const errorText = await response.text().catch(() => '');
|
|
72
|
+
yield { event: 'error', data: `HTTP ${response.status}: ${errorText}` };
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
throw new Error(`HTTP ${response.status}`);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
if (!response.body) {
|
|
79
|
+
yield { event: 'error', data: 'No response body' };
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
retries = 0;
|
|
84
|
+
|
|
85
|
+
for await (const event of parseSSE(response.body)) {
|
|
86
|
+
if (event.id) lastEventId = event.id;
|
|
87
|
+
yield event;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
return;
|
|
91
|
+
} catch (e) {
|
|
92
|
+
if (aborted) return;
|
|
93
|
+
|
|
94
|
+
const isAbort = e instanceof DOMException && e.name === 'AbortError';
|
|
95
|
+
if (isAbort && retries >= maxRetries) {
|
|
96
|
+
yield { event: 'error', data: 'Request timed out' };
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
retries++;
|
|
101
|
+
if (retries > maxRetries) {
|
|
102
|
+
yield { event: 'error', data: e instanceof Error ? e.message : String(e) };
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
const delay = retryDelay * Math.pow(2, retries - 1);
|
|
107
|
+
await new Promise((resolve) => setTimeout(resolve, delay));
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
}
|
package/src/core/types.ts
CHANGED
|
@@ -2,7 +2,7 @@ import { z } from 'zod';
|
|
|
2
2
|
|
|
3
3
|
export const MessageSchema = z.object({
|
|
4
4
|
role: z.enum(['system', 'user', 'assistant', 'tool', 'function']),
|
|
5
|
-
content: z.
|
|
5
|
+
content: z.unknown().nullable().optional(),
|
|
6
6
|
name: z.string().optional(),
|
|
7
7
|
tool_call_id: z.string().optional(),
|
|
8
8
|
tool_calls: z
|
|
@@ -63,6 +63,7 @@ export interface StreamChunk {
|
|
|
63
63
|
export interface BridgePlugin {
|
|
64
64
|
name: string;
|
|
65
65
|
version: string;
|
|
66
|
+
oauthProvider?: import('@ai-ide-bridge/oauth').OAuthProvider;
|
|
66
67
|
authenticate(config: Record<string, string>): Promise<boolean>;
|
|
67
68
|
listModels(config: Record<string, string>): Promise<ModelInfo[]>;
|
|
68
69
|
createSession(config: Record<string, string>, model: string): Promise<BridgeSession>;
|
|
@@ -17,7 +17,7 @@ export class CopilotBridgeSession implements BridgeSession {
|
|
|
17
17
|
model: this.modelId,
|
|
18
18
|
messages: messages.map((m) => ({
|
|
19
19
|
role: m.role,
|
|
20
|
-
content: m.content
|
|
20
|
+
content: m.content,
|
|
21
21
|
...(m.tool_calls && { tool_calls: m.tool_calls }),
|
|
22
22
|
...(m.tool_call_id && { tool_call_id: m.tool_call_id }),
|
|
23
23
|
})),
|
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
import { Cursor } from '@cursor/sdk';
|
|
2
1
|
import type { BridgePlugin, BridgeSession, ModelInfo } from '../../core/index.js';
|
|
3
2
|
import { CursorBridgeSession } from './session.js';
|
|
4
3
|
|
|
4
|
+
const CURSOR_API_BASE = 'https://api2.cursor.sh';
|
|
5
|
+
|
|
5
6
|
export class CursorBridgePlugin implements BridgePlugin {
|
|
6
7
|
name = 'cursor';
|
|
7
8
|
version = '2.0.0';
|
|
@@ -10,8 +11,10 @@ export class CursorBridgePlugin implements BridgePlugin {
|
|
|
10
11
|
const apiKey = config.CURSOR_API_KEY;
|
|
11
12
|
if (!apiKey) return false;
|
|
12
13
|
try {
|
|
13
|
-
await
|
|
14
|
-
|
|
14
|
+
const response = await fetch(`${CURSOR_API_BASE}/auth/whoami`, {
|
|
15
|
+
headers: { Authorization: `Bearer ${apiKey}` },
|
|
16
|
+
});
|
|
17
|
+
return response.ok;
|
|
15
18
|
} catch {
|
|
16
19
|
return false;
|
|
17
20
|
}
|
|
@@ -20,18 +23,47 @@ export class CursorBridgePlugin implements BridgePlugin {
|
|
|
20
23
|
async listModels(config: Record<string, string>): Promise<ModelInfo[]> {
|
|
21
24
|
const apiKey = config.CURSOR_API_KEY;
|
|
22
25
|
if (!apiKey) throw new Error('Missing CURSOR_API_KEY');
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
26
|
+
|
|
27
|
+
try {
|
|
28
|
+
const response = await fetch(`${CURSOR_API_BASE}/models`, {
|
|
29
|
+
headers: { Authorization: `Bearer ${apiKey}` },
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
if (!response.ok) {
|
|
33
|
+
return defaultModels();
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const data = await response.json();
|
|
37
|
+
if (Array.isArray(data)) {
|
|
38
|
+
return data.map((m: { id?: string; name?: string }) => ({
|
|
39
|
+
id: m.id ?? m.name ?? 'unknown',
|
|
40
|
+
name: m.name ?? m.id ?? 'unknown',
|
|
41
|
+
capabilities: { streaming: true, tools: true },
|
|
42
|
+
}));
|
|
43
|
+
}
|
|
44
|
+
} catch {
|
|
45
|
+
// Fall through to defaults
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
return defaultModels();
|
|
29
49
|
}
|
|
30
50
|
|
|
31
51
|
async createSession(config: Record<string, string>, model: string): Promise<BridgeSession> {
|
|
32
52
|
const apiKey = config.CURSOR_API_KEY;
|
|
33
53
|
if (!apiKey) throw new Error('Missing CURSOR_API_KEY');
|
|
34
|
-
|
|
35
|
-
return new CursorBridgeSession(apiKey, model, cwd);
|
|
54
|
+
return new CursorBridgeSession(apiKey, model);
|
|
36
55
|
}
|
|
37
56
|
}
|
|
57
|
+
|
|
58
|
+
function defaultModels(): ModelInfo[] {
|
|
59
|
+
return [
|
|
60
|
+
{ id: 'composer-2', name: 'Composer 2', capabilities: { streaming: true, tools: true } },
|
|
61
|
+
{ id: 'composer-2.5', name: 'Composer 2.5', capabilities: { streaming: true, tools: true } },
|
|
62
|
+
{ id: 'gpt-4o', name: 'GPT-4o', capabilities: { streaming: true, tools: true } },
|
|
63
|
+
{
|
|
64
|
+
id: 'claude-3.5-sonnet',
|
|
65
|
+
name: 'Claude 3.5 Sonnet',
|
|
66
|
+
capabilities: { streaming: true, tools: true },
|
|
67
|
+
},
|
|
68
|
+
];
|
|
69
|
+
}
|
|
@@ -1,78 +1,103 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
import type {
|
|
2
|
+
BridgeSession,
|
|
3
|
+
Message,
|
|
4
|
+
ToolDefinition,
|
|
5
|
+
StreamChunk,
|
|
6
|
+
SSEEvent,
|
|
7
|
+
} from '../../core/index.js';
|
|
8
|
+
import { createStream } from '../../core/index.js';
|
|
9
|
+
|
|
10
|
+
const CURSOR_API_BASE = 'https://api2.cursor.sh';
|
|
5
11
|
|
|
6
12
|
export class CursorBridgeSession implements BridgeSession {
|
|
7
|
-
private agent: SDKAgent | null = null;
|
|
8
13
|
private apiKey: string;
|
|
9
14
|
private modelId: string;
|
|
10
|
-
private cwd: string;
|
|
11
15
|
|
|
12
|
-
constructor(apiKey: string, modelId: string
|
|
16
|
+
constructor(apiKey: string, modelId: string) {
|
|
13
17
|
this.apiKey = apiKey;
|
|
14
18
|
this.modelId = modelId;
|
|
15
|
-
this.cwd = cwd;
|
|
16
19
|
}
|
|
17
20
|
|
|
18
21
|
async *send(messages: Message[], tools?: ToolDefinition[]): AsyncIterable<StreamChunk> {
|
|
19
|
-
const
|
|
20
|
-
|
|
22
|
+
const body = JSON.stringify({
|
|
23
|
+
model: this.modelId,
|
|
24
|
+
messages: messages.map((m) => ({
|
|
25
|
+
role: m.role,
|
|
26
|
+
content: m.content,
|
|
27
|
+
...(m.tool_calls && { tool_calls: m.tool_calls }),
|
|
28
|
+
...(m.tool_call_id && { tool_call_id: m.tool_call_id }),
|
|
29
|
+
...(m.name && { name: m.name }),
|
|
30
|
+
})),
|
|
31
|
+
...(tools && { tools: tools.map((t) => t) }),
|
|
32
|
+
stream: true,
|
|
33
|
+
});
|
|
21
34
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
35
|
+
for await (const chunk of createStream({
|
|
36
|
+
url: `${CURSOR_API_BASE}/aiserver.v1.AiService/StreamChat`,
|
|
37
|
+
method: 'POST',
|
|
38
|
+
headers: {
|
|
39
|
+
'Content-Type': 'application/json',
|
|
40
|
+
Authorization: `Bearer ${this.apiKey}`,
|
|
41
|
+
Accept: 'text/event-stream',
|
|
42
|
+
},
|
|
43
|
+
body,
|
|
44
|
+
parseEvent: parseCursorEvent,
|
|
45
|
+
})) {
|
|
46
|
+
yield chunk;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
28
49
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
// onDelta is synchronous callback, we buffer and yield in the loop
|
|
34
|
-
}
|
|
35
|
-
},
|
|
36
|
-
};
|
|
50
|
+
async dispose(): Promise<void> {
|
|
51
|
+
// No state to dispose with direct HTTP
|
|
52
|
+
}
|
|
53
|
+
}
|
|
37
54
|
|
|
38
|
-
|
|
55
|
+
function parseCursorEvent(event: SSEEvent): StreamChunk | null {
|
|
56
|
+
if (event.event === 'error') {
|
|
57
|
+
return { type: 'error', content: event.data, finishReason: 'error' };
|
|
58
|
+
}
|
|
39
59
|
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
type: 'error',
|
|
44
|
-
content: `Agent run ${result.status}: ${result.result ?? 'no details'}`,
|
|
45
|
-
finishReason: 'error',
|
|
46
|
-
};
|
|
47
|
-
return;
|
|
48
|
-
}
|
|
60
|
+
if (event.data === '' || event.data === '[DONE]') {
|
|
61
|
+
return { type: 'done', finishReason: 'stop' };
|
|
62
|
+
}
|
|
49
63
|
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
64
|
+
try {
|
|
65
|
+
const json = JSON.parse(event.data);
|
|
66
|
+
|
|
67
|
+
if (json.error) {
|
|
68
|
+
return {
|
|
69
|
+
type: 'error',
|
|
70
|
+
content: json.error.message ?? JSON.stringify(json.error),
|
|
71
|
+
finishReason: 'error',
|
|
72
|
+
};
|
|
54
73
|
}
|
|
55
|
-
}
|
|
56
74
|
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
75
|
+
if (json.choices?.[0]?.delta?.content) {
|
|
76
|
+
return { type: 'text', content: json.choices[0].delta.content };
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
if (json.choices?.[0]?.delta?.tool_calls) {
|
|
80
|
+
for (const tc of json.choices[0].delta.tool_calls) {
|
|
81
|
+
return {
|
|
82
|
+
type: 'tool_call',
|
|
83
|
+
toolCall: {
|
|
84
|
+
id: tc.id ?? `tc-${Date.now()}`,
|
|
85
|
+
name: tc.function?.name ?? '',
|
|
86
|
+
arguments: tc.function?.arguments ?? '',
|
|
87
|
+
},
|
|
88
|
+
};
|
|
63
89
|
}
|
|
64
|
-
this.agent = null;
|
|
65
90
|
}
|
|
66
|
-
}
|
|
67
91
|
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
const label = m.role === 'tool' ? `tool (${m.tool_call_id ?? m.name ?? 'result'})` : m.role;
|
|
74
|
-
blocks.push(`[${label}]\n${text}`);
|
|
92
|
+
if (json.choices?.[0]?.finish_reason) {
|
|
93
|
+
return {
|
|
94
|
+
type: 'done',
|
|
95
|
+
finishReason: json.choices[0].finish_reason === 'stop' ? 'stop' : 'tool_calls',
|
|
96
|
+
};
|
|
75
97
|
}
|
|
76
|
-
|
|
98
|
+
} catch {
|
|
99
|
+
return null;
|
|
77
100
|
}
|
|
101
|
+
|
|
102
|
+
return null;
|
|
78
103
|
}
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import type { ToolDefinition } from '../../core/index.js';
|
|
2
|
-
export interface CursorTool {
|
|
3
|
-
type: 'function';
|
|
4
|
-
function: {
|
|
5
|
-
name: string;
|
|
6
|
-
description?: string;
|
|
7
|
-
parameters: Record<string, unknown>;
|
|
8
|
-
};
|
|
9
|
-
}
|
|
10
|
-
export declare function translateTools(tools: ToolDefinition[]): CursorTool[];
|
|
11
|
-
export declare function translateToolResult(toolCallId: string, result: string): string;
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
export function translateTools(tools) {
|
|
2
|
-
return tools.map((tool) => ({
|
|
3
|
-
type: 'function',
|
|
4
|
-
function: {
|
|
5
|
-
name: tool.function.name,
|
|
6
|
-
description: tool.function.description,
|
|
7
|
-
parameters: tool.function.parameters,
|
|
8
|
-
},
|
|
9
|
-
}));
|
|
10
|
-
}
|
|
11
|
-
export function translateToolResult(toolCallId, result) {
|
|
12
|
-
return `[tool result for ${toolCallId}]\n${result}`;
|
|
13
|
-
}
|
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
import type { ToolDefinition } from '../../core/index.js';
|
|
2
|
-
|
|
3
|
-
export interface CursorTool {
|
|
4
|
-
type: 'function';
|
|
5
|
-
function: {
|
|
6
|
-
name: string;
|
|
7
|
-
description?: string;
|
|
8
|
-
parameters: Record<string, unknown>;
|
|
9
|
-
};
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
export function translateTools(tools: ToolDefinition[]): CursorTool[] {
|
|
13
|
-
return tools.map((tool) => ({
|
|
14
|
-
type: 'function' as const,
|
|
15
|
-
function: {
|
|
16
|
-
name: tool.function.name,
|
|
17
|
-
description: tool.function.description,
|
|
18
|
-
parameters: tool.function.parameters as Record<string, unknown>,
|
|
19
|
-
},
|
|
20
|
-
}));
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
export function translateToolResult(toolCallId: string, result: string): string {
|
|
24
|
-
return `[tool result for ${toolCallId}]\n${result}`;
|
|
25
|
-
}
|