vercel-ai-cursor-provider 0.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/LICENSE +21 -0
- package/README.md +61 -0
- package/dist/index.d.ts +28 -0
- package/dist/index.js +114 -0
- package/package.json +30 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
# vercel-ai-cursor-provider
|
|
2
|
+
|
|
3
|
+
A [Vercel AI SDK](https://sdk.vercel.ai/) custom provider for **Cursor**, leveraging the `cursor-agent` CLI.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- Use any model supported by your Cursor subscription (Claude 3.5 Sonnet, GPT-4o, etc.)
|
|
8
|
+
- Supports streaming via `streamText`
|
|
9
|
+
- References logic from `opencode-cursor` for robust Prompt building
|
|
10
|
+
|
|
11
|
+
## Prerequisites
|
|
12
|
+
|
|
13
|
+
- **Cursor** installed on your machine.
|
|
14
|
+
- `cursor-agent` CLI available in your PATH.
|
|
15
|
+
- You can usually install it via: `curl -fsS https://cursor.com/install | bash`
|
|
16
|
+
- You must be logged into `cursor-agent`: `cursor-agent login`
|
|
17
|
+
|
|
18
|
+
## Installation
|
|
19
|
+
|
|
20
|
+
```bash
|
|
21
|
+
npm install vercel-ai-cursor-provider @ai-sdk/provider ai
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
## Usage
|
|
25
|
+
|
|
26
|
+
```typescript
|
|
27
|
+
import { streamText } from 'ai';
|
|
28
|
+
import { createCursor } from 'vercel-ai-cursor-provider';
|
|
29
|
+
|
|
30
|
+
// Create the provider
|
|
31
|
+
const cursor = createCursor({
|
|
32
|
+
workspace: process.cwd(), // Optional: project workspace for cursor-agent context
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
// Use it with Vercel AI SDK
|
|
36
|
+
const { textStream } = await streamText({
|
|
37
|
+
model: cursor('claude-3.5-sonnet'),
|
|
38
|
+
prompt: 'Write a TypeScript function to calculate fibonacci numbers.',
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
for await (const text of textStream) {
|
|
42
|
+
process.stdout.write(text);
|
|
43
|
+
}
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
## Configuration
|
|
47
|
+
|
|
48
|
+
`createCursor` accepts an optional options object:
|
|
49
|
+
|
|
50
|
+
| Option | Type | Description |
|
|
51
|
+
| --- | --- | --- |
|
|
52
|
+
| `command` | `string` | Path to `cursor-agent` binary. Defaults to `cursor-agent`. |
|
|
53
|
+
| `workspace` | `string` | The directory `cursor-agent` should treat as the project workspace. |
|
|
54
|
+
|
|
55
|
+
## Credits
|
|
56
|
+
|
|
57
|
+
Inspired by the [opencode-cursor](https://github.com/Nomadcxx/opencode-cursor) project by Nomadcxx.
|
|
58
|
+
|
|
59
|
+
## License
|
|
60
|
+
|
|
61
|
+
MIT
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { LanguageModelV1, LanguageModelV1StreamPart } from '@ai-sdk/provider';
|
|
2
|
+
export interface CursorOptions {
|
|
3
|
+
/**
|
|
4
|
+
* Path to cursor-agent. Defaults to 'cursor-agent' (searched in PATH).
|
|
5
|
+
*/
|
|
6
|
+
command?: string;
|
|
7
|
+
/**
|
|
8
|
+
* Workspace path for cursor-agent.
|
|
9
|
+
*/
|
|
10
|
+
workspace?: string;
|
|
11
|
+
}
|
|
12
|
+
export declare class CursorLanguageModel implements LanguageModelV1 {
|
|
13
|
+
readonly modelId: string;
|
|
14
|
+
private options;
|
|
15
|
+
readonly specificationVersion = "v1";
|
|
16
|
+
readonly defaultObjectGenerationMode: undefined;
|
|
17
|
+
constructor(modelId: string, options?: CursorOptions);
|
|
18
|
+
get provider(): string;
|
|
19
|
+
doGenerate(options: any): Promise<any>;
|
|
20
|
+
doStream(options: any): Promise<{
|
|
21
|
+
stream: ReadableStream<LanguageModelV1StreamPart>;
|
|
22
|
+
rawCall: {
|
|
23
|
+
rawPrompt: string;
|
|
24
|
+
rawSettings: any;
|
|
25
|
+
};
|
|
26
|
+
}>;
|
|
27
|
+
}
|
|
28
|
+
export declare function createCursor(options?: CursorOptions): (modelId: string) => CursorLanguageModel;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import { spawn } from 'child_process';
|
|
2
|
+
import { Readable } from 'stream';
|
|
3
|
+
/**
|
|
4
|
+
* Builds a prompt string compatible with cursor-agent from Vercel AI SDK messages.
|
|
5
|
+
*/
|
|
6
|
+
function buildCursorPrompt(messages, tools) {
|
|
7
|
+
const lines = [];
|
|
8
|
+
if (tools && tools.length > 0) {
|
|
9
|
+
const toolDescs = tools.map((t) => {
|
|
10
|
+
const fn = t.function || t;
|
|
11
|
+
return `- ${fn.name}: ${fn.description || ''}\n Parameters: ${JSON.stringify(fn.parameters || {})}`;
|
|
12
|
+
}).join('\n');
|
|
13
|
+
lines.push(`SYSTEM: You have access to the following tools:\n${toolDescs}`);
|
|
14
|
+
}
|
|
15
|
+
for (const message of messages) {
|
|
16
|
+
const role = message.role.toUpperCase();
|
|
17
|
+
let content = '';
|
|
18
|
+
if (Array.isArray(message.content)) {
|
|
19
|
+
content = message.content
|
|
20
|
+
.filter((c) => c.type === 'text')
|
|
21
|
+
.map((c) => c.text)
|
|
22
|
+
.join('\n');
|
|
23
|
+
}
|
|
24
|
+
else {
|
|
25
|
+
content = message.content || '';
|
|
26
|
+
}
|
|
27
|
+
if (message.role === 'tool') {
|
|
28
|
+
lines.push(`TOOL_RESULT (call_id: ${message.toolCallId}): ${content}`);
|
|
29
|
+
}
|
|
30
|
+
else if (message.role === 'assistant' && message.toolCalls) {
|
|
31
|
+
const tcs = message.toolCalls.map((tc) => `tool_call(id: ${tc.toolCallId}, name: ${tc.toolName}, args: ${JSON.stringify(tc.args)})`).join('\n');
|
|
32
|
+
lines.push(`ASSISTANT: ${content ? content + '\n' : ''}${tcs}`);
|
|
33
|
+
}
|
|
34
|
+
else {
|
|
35
|
+
lines.push(`${role}: ${content}`);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
return lines.join('\n\n');
|
|
39
|
+
}
|
|
40
|
+
export class CursorLanguageModel {
|
|
41
|
+
modelId;
|
|
42
|
+
options;
|
|
43
|
+
specificationVersion = 'v1';
|
|
44
|
+
defaultObjectGenerationMode = undefined;
|
|
45
|
+
constructor(modelId, options = {}) {
|
|
46
|
+
this.modelId = modelId;
|
|
47
|
+
this.options = options;
|
|
48
|
+
}
|
|
49
|
+
get provider() { return 'cursor'; }
|
|
50
|
+
async doGenerate(options) {
|
|
51
|
+
throw new Error('doGenerate not implemented, please use streamText');
|
|
52
|
+
}
|
|
53
|
+
async doStream(options) {
|
|
54
|
+
const prompt = buildCursorPrompt(options.prompt, options.tools || []);
|
|
55
|
+
const cmd = this.options.command || 'cursor-agent';
|
|
56
|
+
const workspace = this.options.workspace || process.cwd();
|
|
57
|
+
const args = [
|
|
58
|
+
'--print',
|
|
59
|
+
'--output-format', 'stream-json',
|
|
60
|
+
'--stream-partial-output',
|
|
61
|
+
'--workspace', workspace,
|
|
62
|
+
'--model', this.modelId,
|
|
63
|
+
];
|
|
64
|
+
const child = spawn(cmd, args, { stdio: ['pipe', 'pipe', 'pipe'] });
|
|
65
|
+
child.stdin.write(prompt);
|
|
66
|
+
child.stdin.end();
|
|
67
|
+
const stream = new ReadableStream({
|
|
68
|
+
async start(controller) {
|
|
69
|
+
const reader = Readable.toWeb(child.stdout);
|
|
70
|
+
const decoder = new TextDecoder();
|
|
71
|
+
let buffer = '';
|
|
72
|
+
try {
|
|
73
|
+
for await (const chunk of reader) {
|
|
74
|
+
buffer += decoder.decode(chunk, { stream: true });
|
|
75
|
+
const lines = buffer.split('\n');
|
|
76
|
+
buffer = lines.pop() || '';
|
|
77
|
+
for (const line of lines) {
|
|
78
|
+
if (!line.trim())
|
|
79
|
+
continue;
|
|
80
|
+
try {
|
|
81
|
+
const event = JSON.parse(line);
|
|
82
|
+
if (event.type === 'assistant' || event.type === 'thinking') {
|
|
83
|
+
const text = event.type === 'thinking' ? event.text :
|
|
84
|
+
event.message?.content?.find((c) => c.type === 'text')?.text;
|
|
85
|
+
if (text) {
|
|
86
|
+
controller.enqueue({ type: 'text-delta', textDelta: text });
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
catch (e) {
|
|
91
|
+
// Ignore parse errors for partial lines
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
finally {
|
|
97
|
+
controller.enqueue({
|
|
98
|
+
type: 'finish',
|
|
99
|
+
finishReason: 'stop',
|
|
100
|
+
usage: { promptTokens: 0, completionTokens: 0 }
|
|
101
|
+
});
|
|
102
|
+
controller.close();
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
});
|
|
106
|
+
return {
|
|
107
|
+
stream,
|
|
108
|
+
rawCall: { rawPrompt: prompt, rawSettings: options },
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
export function createCursor(options = {}) {
|
|
113
|
+
return (modelId) => new CursorLanguageModel(modelId, options);
|
|
114
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "vercel-ai-cursor-provider",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Vercel AI SDK provider for Cursor (using cursor-agent)",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"files": [
|
|
9
|
+
"dist"
|
|
10
|
+
],
|
|
11
|
+
"scripts": {
|
|
12
|
+
"build": "tsc",
|
|
13
|
+
"prepublishOnly": "npm run build"
|
|
14
|
+
},
|
|
15
|
+
"keywords": [
|
|
16
|
+
"vercel-ai-sdk",
|
|
17
|
+
"cursor",
|
|
18
|
+
"cursor-agent",
|
|
19
|
+
"ai-provider"
|
|
20
|
+
],
|
|
21
|
+
"author": "",
|
|
22
|
+
"license": "MIT",
|
|
23
|
+
"dependencies": {
|
|
24
|
+
"@ai-sdk/provider": "^1.1.0"
|
|
25
|
+
},
|
|
26
|
+
"devDependencies": {
|
|
27
|
+
"typescript": "^5.0.0",
|
|
28
|
+
"@types/node": "^20.0.0"
|
|
29
|
+
}
|
|
30
|
+
}
|