@tduyng/pi-cursor 0.0.0-stage → 0.0.1
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 +178 -0
- package/README.md +87 -2
- package/package.json +42 -4
- package/src/index.ts +165 -0
- package/src/json.ts +74 -0
- package/src/sdk.ts +14 -0
- package/src/session.ts +352 -0
- package/src/transcript.ts +212 -0
- package/src/turn.ts +336 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
Use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
Copyright 2026 tduyng
|
package/README.md
CHANGED
|
@@ -1,3 +1,88 @@
|
|
|
1
|
-
#
|
|
1
|
+
# pi-cursor
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Cursor models in Pi through the official Cursor SDK. Pi keeps its own system prompt, tools, approvals, compaction and session tree. Cursor is only the authenticated model transport.
|
|
4
|
+
|
|
5
|
+
No Cursor CLI is installed or called. Login is the SDK browser flow, which mints a stored Cursor API key under `~/.cursor/sdk/auth.json`.
|
|
6
|
+
|
|
7
|
+
The provider id is `cursor`, and model ids are Cursor's own ids. `/model cursor/gpt-5.5` and `/model cursor/claude-opus-4-8` work as written.
|
|
8
|
+
|
|
9
|
+
## Requirements
|
|
10
|
+
|
|
11
|
+
- Pi 1.4 or newer.
|
|
12
|
+
- Node 24 or newer.
|
|
13
|
+
- A Cursor account.
|
|
14
|
+
- The extension dependency `@cursor/sdk` 1.0.40, installed by the workspace.
|
|
15
|
+
|
|
16
|
+
## Install
|
|
17
|
+
|
|
18
|
+
```sh
|
|
19
|
+
pi install /path/to/extensions/pi-cursor
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
In this repository the workspace already registers the extension in the root `package.json`.
|
|
23
|
+
|
|
24
|
+
## Login
|
|
25
|
+
|
|
26
|
+
```text
|
|
27
|
+
/cursor login
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
The SDK opens the Cursor login page and mints a key when the browser flow finishes. If the browser does not open, the command prints the URL. `/cursor status` shows the account and `/cursor logout` removes the stored key.
|
|
31
|
+
|
|
32
|
+
`CURSOR_API_KEY` overrides the stored login. The key is read by the SDK, not copied into Pi's `auth.json`.
|
|
33
|
+
|
|
34
|
+
Cursor bills SDK traffic under its own terms. This extension cannot tell you whether a run is plan-included, so check the Cursor dashboard for the account's SDK usage.
|
|
35
|
+
|
|
36
|
+
## Use
|
|
37
|
+
|
|
38
|
+
```text
|
|
39
|
+
/model cursor/gpt-5.5
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
The first model list is a small fallback so `/model` is usable before login. After login, or on the next session start, the extension reads `Cursor.models.list()` and registers every model the account can reach. `/cursor models` prints them.
|
|
43
|
+
|
|
44
|
+
## How it works
|
|
45
|
+
|
|
46
|
+
| Pi | Cursor SDK |
|
|
47
|
+
| ------------------------- | ------------------------------------------------------------------------------------ |
|
|
48
|
+
| Pi system prompt | `AgentOptions.systemPrompt`, replaces the Cursor harness prompt |
|
|
49
|
+
| Pi tools | `LocalAgentOptions.customTools`, one in-process callback per Pi tool |
|
|
50
|
+
| Cursor built-ins | `AgentOptions.tools: ['mcp']`, so only Pi's custom tools exist |
|
|
51
|
+
| Cursor rules, skills, MCP | `local.settingSources: []` |
|
|
52
|
+
| Pi tool execution | the custom tool callback parks, Pi runs the tool, the next provider call resolves it |
|
|
53
|
+
|
|
54
|
+
A tool call does not end the Cursor run. The callback waits, Pi executes the named tool through its normal pipeline, and the next `streamSimple` call resolves the callback with the result. The same Cursor run continues, so the model sees the tool result in the same conversation.
|
|
55
|
+
|
|
56
|
+
One Cursor agent runs per Pi session. A normal turn sends only the new user message. A tool round resolves parked callbacks and sends nothing. The agent is only replaced when Pi's prompt, tool set or working directory changes, or when the Pi history diverges. That replacement replays the existing Pi conversation once as a single message.
|
|
57
|
+
|
|
58
|
+
There is no HTTP MCP server, no Hono server and no tool-name rewriting. Cursor's SDK invokes the callback directly.
|
|
59
|
+
|
|
60
|
+
## Token and cache behavior
|
|
61
|
+
|
|
62
|
+
Cursor's backend owns its own conversation and cache. Pi does not replay a linear conversation on every turn, and it does not run a second agent for tool rounds. The Pi system prompt is stable for the lifetime of an agent, and the tool list is stable too. A divergence is the only case that pays a replay.
|
|
63
|
+
|
|
64
|
+
Thinking levels map to Cursor model parameters. For Claude models the extension sets both `thinking` and `effort`. For GPT models it sets `reasoning`. For Grok it sets `effort`. The mapping comes from the live model catalog.
|
|
65
|
+
|
|
66
|
+
## Limitations
|
|
67
|
+
|
|
68
|
+
- Cursor owns the agent loop and its context management. Pi's compaction does not reach inside a live Cursor agent.
|
|
69
|
+
- `AgentOptions.systemPrompt` is server-gated. If Cursor rejects it, the extension recreates the agent without it and prepends Pi's prompt to the first message. That fallback works but spends more tokens because Cursor's harness prompt remains.
|
|
70
|
+
- Usage tokens come from Cursor's `turn-ended` events. Pi shows a zero money cost because Cursor bills outside Pi.
|
|
71
|
+
- Images work in new messages. Replayed history carries images without their original message labels.
|
|
72
|
+
- A model id that Cursor no longer serves fails at `send`, not at model selection.
|
|
73
|
+
- A long Pi tool run is held open by Cursor local runtime. The SDK does not document a timeout for custom tools.
|
|
74
|
+
- The Cursor SDK is a 25 MB dependency with optional platform packages.
|
|
75
|
+
|
|
76
|
+
## Configuration
|
|
77
|
+
|
|
78
|
+
| Variable | Meaning |
|
|
79
|
+
| ---------------- | ----------------------------------------------- |
|
|
80
|
+
| `CURSOR_API_KEY` | Cursor API key. Overrides the stored SDK login. |
|
|
81
|
+
|
|
82
|
+
## Tests
|
|
83
|
+
|
|
84
|
+
```sh
|
|
85
|
+
bunx vitest run extensions/pi-cursor
|
|
86
|
+
bun run typecheck
|
|
87
|
+
bun run lint:anti-slop
|
|
88
|
+
```
|
package/package.json
CHANGED
|
@@ -1,6 +1,44 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tduyng/pi-cursor",
|
|
3
|
-
"version": "0.0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "Use Cursor models in Pi through the official Cursor SDK, with Pi's own prompt and tools.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"pi",
|
|
7
|
+
"pi-package",
|
|
8
|
+
"pi-extension",
|
|
9
|
+
"pi-coding-agent",
|
|
10
|
+
"cursor",
|
|
11
|
+
"cursor-sdk",
|
|
12
|
+
"models"
|
|
13
|
+
],
|
|
14
|
+
"license": "Apache-2.0",
|
|
15
|
+
"author": "tduyng",
|
|
16
|
+
"type": "module",
|
|
17
|
+
"files": [
|
|
18
|
+
"src",
|
|
19
|
+
"!src/**/*.test.ts",
|
|
20
|
+
"README.md",
|
|
21
|
+
"LICENSE"
|
|
22
|
+
],
|
|
23
|
+
"dependencies": {
|
|
24
|
+
"@cursor/sdk": "1.0.40",
|
|
25
|
+
"ata-validator": "^1.62.1",
|
|
26
|
+
"zod": "^4.0.0"
|
|
27
|
+
},
|
|
28
|
+
"peerDependencies": {
|
|
29
|
+
"@earendil-works/pi-ai": "*",
|
|
30
|
+
"@earendil-works/pi-coding-agent": "*"
|
|
31
|
+
},
|
|
32
|
+
"pi": {
|
|
33
|
+
"extensions": [
|
|
34
|
+
"./src/index.ts"
|
|
35
|
+
]
|
|
36
|
+
},
|
|
37
|
+
"repository": {
|
|
38
|
+
"type": "git",
|
|
39
|
+
"url": "git+https://gitlab.com/tduyng/pi-agent.git",
|
|
40
|
+
"directory": "extensions/pi-cursor"
|
|
41
|
+
},
|
|
42
|
+
"homepage": "https://gitlab.com/tduyng/pi-agent/-/tree/main/extensions/pi-cursor#readme",
|
|
43
|
+
"bugs": "https://gitlab.com/tduyng/pi-agent/-/issues"
|
|
44
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
import type { SdkLoginOptions } from '@cursor/sdk'
|
|
2
|
+
import type { ExtensionAPI, ExtensionCommandContext } from '@earendil-works/pi-coding-agent'
|
|
3
|
+
|
|
4
|
+
import { fallbackModels, rememberModels, toPiModels, type PiModelEntry } from './models.js'
|
|
5
|
+
import { cursorApiKey, cursorSdk } from './sdk.js'
|
|
6
|
+
import { allSessions, dropSessions, sweep } from './session.js'
|
|
7
|
+
import { createRuntime, streamCursor } from './turn.js'
|
|
8
|
+
|
|
9
|
+
const PROVIDER_ID = 'cursor'
|
|
10
|
+
const API_ID = 'cursor'
|
|
11
|
+
const IDLE_MS = 20 * 60_000
|
|
12
|
+
|
|
13
|
+
export default function piCursor(pi: ExtensionAPI): void {
|
|
14
|
+
const runtime = createRuntime()
|
|
15
|
+
let models: PiModelEntry[] = fallbackModels()
|
|
16
|
+
let refreshing: Promise<PiModelEntry[]> | undefined
|
|
17
|
+
let refreshed = false
|
|
18
|
+
let sweeper: ReturnType<typeof setInterval> | undefined
|
|
19
|
+
|
|
20
|
+
function register(next: PiModelEntry[]): void {
|
|
21
|
+
models = next
|
|
22
|
+
pi.registerProvider(PROVIDER_ID, {
|
|
23
|
+
name: 'Cursor (SDK)',
|
|
24
|
+
baseUrl: 'cursor://local',
|
|
25
|
+
apiKey: 'cursor-sdk',
|
|
26
|
+
api: API_ID,
|
|
27
|
+
models: next,
|
|
28
|
+
streamSimple: (model, context, options) => streamCursor(runtime, model, context, options),
|
|
29
|
+
refreshModels: () => refresh()
|
|
30
|
+
})
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
async function refresh(): Promise<PiModelEntry[]> {
|
|
34
|
+
refreshing ??= (async () => {
|
|
35
|
+
try {
|
|
36
|
+
const { Cursor } = await cursorSdk()
|
|
37
|
+
const items = await Cursor.models.list({ apiKey: cursorApiKey() })
|
|
38
|
+
rememberModels(items)
|
|
39
|
+
const next = toPiModels(items)
|
|
40
|
+
if (next.length > 0) register(next)
|
|
41
|
+
return models
|
|
42
|
+
} catch {
|
|
43
|
+
return models
|
|
44
|
+
} finally {
|
|
45
|
+
refreshing = undefined
|
|
46
|
+
}
|
|
47
|
+
})()
|
|
48
|
+
return refreshing
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
register(models)
|
|
52
|
+
|
|
53
|
+
if (!sweeper) {
|
|
54
|
+
sweeper = setInterval(() => sweep(runtime.sessions, IDLE_MS), 60_000)
|
|
55
|
+
sweeper.unref()
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
pi.on('session_start', () => {
|
|
59
|
+
if (refreshed) return
|
|
60
|
+
refreshed = true
|
|
61
|
+
void refresh().catch(() => undefined)
|
|
62
|
+
})
|
|
63
|
+
|
|
64
|
+
pi.on('session_shutdown', () => {
|
|
65
|
+
dropSessions(runtime.sessions)
|
|
66
|
+
if (sweeper) clearInterval(sweeper)
|
|
67
|
+
sweeper = undefined
|
|
68
|
+
})
|
|
69
|
+
|
|
70
|
+
process.once('exit', () => dropSessions(runtime.sessions))
|
|
71
|
+
|
|
72
|
+
const show = (text: string) =>
|
|
73
|
+
pi.sendMessage({ customType: 'cursor', display: true, content: text }, { triggerTurn: false })
|
|
74
|
+
|
|
75
|
+
async function login(ctx: ExtensionCommandContext): Promise<void> {
|
|
76
|
+
const { Cursor } = await cursorSdk()
|
|
77
|
+
const options: SdkLoginOptions = {
|
|
78
|
+
apiKeyName: 'pi-cursor',
|
|
79
|
+
onLoginUrl: (url) => {
|
|
80
|
+
void show(`Cursor login. Open this URL if the browser did not:\n${url}`)
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
if (ctx.signal) options.signal = ctx.signal
|
|
84
|
+
const result = await Cursor.auth.login(options)
|
|
85
|
+
await refresh()
|
|
86
|
+
await show(
|
|
87
|
+
`Cursor: logged in${result.email ? ` as ${result.email}` : ''}. ${models.length} model(s) available.`
|
|
88
|
+
)
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
pi.registerCommand('cursor', {
|
|
92
|
+
description: 'Cursor SDK login, status and model list',
|
|
93
|
+
getArgumentCompletions: (prefix: string) =>
|
|
94
|
+
[
|
|
95
|
+
{ value: 'status', label: 'status', description: 'login state, models and live agents' },
|
|
96
|
+
{ value: 'login', label: 'login', description: 'browser login, no Cursor CLI' },
|
|
97
|
+
{ value: 'logout', label: 'logout', description: 'forget the SDK credential' },
|
|
98
|
+
{ value: 'models', label: 'models', description: 'every Cursor model the provider exposes' },
|
|
99
|
+
{ value: 'clear', label: 'clear', description: 'close every warm Cursor agent' },
|
|
100
|
+
{ value: 'help', label: 'help', description: 'usage' }
|
|
101
|
+
].filter((item) => item.value.startsWith(prefix.trim())),
|
|
102
|
+
handler: async (args: string, ctx: ExtensionCommandContext) => {
|
|
103
|
+
const sub = args.trim().split(/\s+/).filter(Boolean)[0] ?? 'status'
|
|
104
|
+
if (sub === 'login') {
|
|
105
|
+
await login(ctx)
|
|
106
|
+
return
|
|
107
|
+
}
|
|
108
|
+
if (sub === 'logout') {
|
|
109
|
+
const { Cursor } = await cursorSdk()
|
|
110
|
+
await Cursor.auth.logout()
|
|
111
|
+
rememberModels([])
|
|
112
|
+
register(fallbackModels())
|
|
113
|
+
await show('Cursor: logged out. The SDK credential is gone.')
|
|
114
|
+
return
|
|
115
|
+
}
|
|
116
|
+
if (sub === 'clear') {
|
|
117
|
+
dropSessions(runtime.sessions)
|
|
118
|
+
await show('Cursor: closed every warm agent.')
|
|
119
|
+
return
|
|
120
|
+
}
|
|
121
|
+
if (sub === 'models') {
|
|
122
|
+
await show(
|
|
123
|
+
[
|
|
124
|
+
`${models.length} model(s), provider id ${PROVIDER_ID}:`,
|
|
125
|
+
...models.map((model) => ` ${PROVIDER_ID}/${model.id}`)
|
|
126
|
+
].join('\n')
|
|
127
|
+
)
|
|
128
|
+
return
|
|
129
|
+
}
|
|
130
|
+
if (sub === 'help') {
|
|
131
|
+
await show(
|
|
132
|
+
[
|
|
133
|
+
'Usage: /cursor [status|login|logout|models|clear|help]',
|
|
134
|
+
'',
|
|
135
|
+
'Login uses the official Cursor SDK browser flow and mints a stored API key.',
|
|
136
|
+
'No Cursor CLI is installed or called.',
|
|
137
|
+
'Pick a model with /model, for example cursor/gpt-5.5.',
|
|
138
|
+
'CURSOR_API_KEY overrides the stored SDK login.'
|
|
139
|
+
].join('\n')
|
|
140
|
+
)
|
|
141
|
+
return
|
|
142
|
+
}
|
|
143
|
+
let authLine = 'unknown'
|
|
144
|
+
try {
|
|
145
|
+
const { Cursor } = await cursorSdk()
|
|
146
|
+
const auth = await Cursor.auth.status()
|
|
147
|
+
authLine =
|
|
148
|
+
auth.status === 'logged-in' ? `logged-in${auth.email ? ` (${auth.email})` : ''}` : auth.status
|
|
149
|
+
} catch (error) {
|
|
150
|
+
authLine = `unavailable: ${error instanceof Error ? error.message : String(error)}`
|
|
151
|
+
}
|
|
152
|
+
const sessions = allSessions(runtime.sessions)
|
|
153
|
+
const parked = sessions.reduce((count, session) => count + session.parked.size, 0)
|
|
154
|
+
await show(
|
|
155
|
+
[
|
|
156
|
+
`auth: ${authLine}`,
|
|
157
|
+
`credential: ${cursorApiKey() ? 'CURSOR_API_KEY' : 'stored SDK login'}`,
|
|
158
|
+
`models: ${models.length}`,
|
|
159
|
+
`live agents: ${sessions.length}, parked tool calls: ${parked}`,
|
|
160
|
+
'prompt: Pi system prompt and Pi tools only; Cursor built-in tools disabled'
|
|
161
|
+
].join('\n')
|
|
162
|
+
)
|
|
163
|
+
}
|
|
164
|
+
})
|
|
165
|
+
}
|
package/src/json.ts
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { createHash } from 'node:crypto'
|
|
2
|
+
|
|
3
|
+
import { Validator } from 'ata-validator'
|
|
4
|
+
import { t } from 'ata-validator/t'
|
|
5
|
+
|
|
6
|
+
const isStringArray = new Validator(t.array(t.string())).isValidObject
|
|
7
|
+
const isString = new Validator(t.string()).isValidObject
|
|
8
|
+
const isNumber = new Validator(t.number()).isValidObject
|
|
9
|
+
const isBoolean = new Validator(t.boolean()).isValidObject
|
|
10
|
+
|
|
11
|
+
export type JsonValue = null | boolean | number | string | JsonValue[] | JsonObject
|
|
12
|
+
export type JsonObject = { [key: string]: JsonValue }
|
|
13
|
+
|
|
14
|
+
const ObjectSchema = t.object({}, { additionalProperties: true })
|
|
15
|
+
const isObjectSchema = new Validator(ObjectSchema).isValidObject
|
|
16
|
+
const ArraySchema = t.array(t.any())
|
|
17
|
+
const isArraySchema = new Validator(ArraySchema).isValidObject
|
|
18
|
+
|
|
19
|
+
function isJsonObject<V>(value: V): value is V & JsonObject {
|
|
20
|
+
return isObjectSchema(value) && !Array.isArray(value)
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function jsonObject(value: JsonValue | undefined): JsonObject | undefined {
|
|
24
|
+
return isJsonObject(value) ? value : undefined
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function jsonArray(value: JsonValue | undefined): JsonValue[] | undefined {
|
|
28
|
+
if (!isArraySchema(value)) return undefined
|
|
29
|
+
return value
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function jsonStrings(value: JsonValue | undefined): string[] | undefined {
|
|
33
|
+
if (!isStringArray(value)) return undefined
|
|
34
|
+
return value
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export function jsonText(object: JsonObject, key: string): string | undefined {
|
|
38
|
+
const value = object[key]
|
|
39
|
+
return isString(value) ? value : undefined
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export function jsonNumber(object: JsonObject, key: string): number | undefined {
|
|
43
|
+
const value = object[key]
|
|
44
|
+
return isNumber(value) ? value : undefined
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export function jsonBoolean(object: JsonObject, key: string): boolean | undefined {
|
|
48
|
+
const value = object[key]
|
|
49
|
+
return isBoolean(value) ? value : undefined
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export function jsonFlag(object: JsonObject, key: string): boolean {
|
|
53
|
+
return jsonBoolean(object, key) === true
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export function stableStringify(value: JsonValue): string {
|
|
57
|
+
return JSON.stringify(sortValue(value))
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function sortValue(value: JsonValue): JsonValue {
|
|
61
|
+
if (Array.isArray(value)) return value.map(sortValue)
|
|
62
|
+
const object = jsonObject(value)
|
|
63
|
+
if (!object) return value
|
|
64
|
+
const sorted: JsonObject = {}
|
|
65
|
+
for (const key of Object.keys(object).toSorted()) {
|
|
66
|
+
const item = object[key]
|
|
67
|
+
if (item !== undefined) sorted[key] = sortValue(item)
|
|
68
|
+
}
|
|
69
|
+
return sorted
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export function sha(value: string): string {
|
|
73
|
+
return createHash('sha256').update(value).digest('base64url')
|
|
74
|
+
}
|
package/src/sdk.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
type CursorSdk = typeof import('@cursor/sdk')
|
|
2
|
+
|
|
3
|
+
let loading: Promise<CursorSdk> | undefined
|
|
4
|
+
|
|
5
|
+
export function cursorSdk(): Promise<CursorSdk> {
|
|
6
|
+
loading ??= import('@cursor/sdk')
|
|
7
|
+
return loading
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export function cursorApiKey(): string | undefined {
|
|
11
|
+
const value = process.env['CURSOR_API_KEY']
|
|
12
|
+
const trimmed = value?.trim()
|
|
13
|
+
return trimmed && trimmed.length > 0 ? trimmed : undefined
|
|
14
|
+
}
|