opencode-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 +55 -0
- package/README.md +112 -0
- package/dist/auth/credential.d.ts +44 -0
- package/dist/auth/credential.js +85 -0
- package/dist/auth/link.d.ts +14 -0
- package/dist/auth/link.js +120 -0
- package/dist/auth/state.d.ts +54 -0
- package/dist/auth/state.js +54 -0
- package/dist/bridge/agent.d.ts +17 -0
- package/dist/bridge/agent.js +39 -0
- package/dist/bridge/binding.d.ts +35 -0
- package/dist/bridge/binding.js +59 -0
- package/dist/bridge/bridge.d.ts +16 -0
- package/dist/bridge/bridge.js +21 -0
- package/dist/bridge/conversation.d.ts +64 -0
- package/dist/bridge/conversation.js +86 -0
- package/dist/bridge/correlation.d.ts +10 -0
- package/dist/bridge/correlation.js +52 -0
- package/dist/bridge/lock.d.ts +4 -0
- package/dist/bridge/lock.js +24 -0
- package/dist/bridge/response-journal.d.ts +7 -0
- package/dist/bridge/response-journal.js +49 -0
- package/dist/bridge/translate.d.ts +60 -0
- package/dist/bridge/translate.js +188 -0
- package/dist/bridge/turn.d.ts +29 -0
- package/dist/bridge/turn.js +219 -0
- package/dist/catalog/catalog.d.ts +20 -0
- package/dist/catalog/catalog.js +192 -0
- package/dist/catalog/source.d.ts +8 -0
- package/dist/catalog/source.js +30 -0
- package/dist/errors.d.ts +35 -0
- package/dist/errors.js +36 -0
- package/dist/ids.d.ts +33 -0
- package/dist/ids.js +52 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +3 -0
- package/dist/model/language-model.d.ts +10 -0
- package/dist/model/language-model.js +539 -0
- package/dist/model/provider-options.d.ts +19 -0
- package/dist/model/provider-options.js +84 -0
- package/dist/plugin.d.ts +2 -0
- package/dist/plugin.js +127 -0
- package/dist/runtime.d.ts +8 -0
- package/dist/runtime.js +25 -0
- package/package.json +33 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
|
|
5
|
+
http://www.apache.org/licenses/
|
|
6
|
+
|
|
7
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
8
|
+
|
|
9
|
+
1. Definitions.
|
|
10
|
+
|
|
11
|
+
"License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity.
|
|
16
|
+
|
|
17
|
+
"You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License.
|
|
18
|
+
|
|
19
|
+
"Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files.
|
|
20
|
+
|
|
21
|
+
"Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types.
|
|
22
|
+
|
|
23
|
+
"Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below).
|
|
24
|
+
|
|
25
|
+
"Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof.
|
|
26
|
+
|
|
27
|
+
"Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution."
|
|
28
|
+
|
|
29
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work.
|
|
30
|
+
|
|
31
|
+
2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form.
|
|
32
|
+
|
|
33
|
+
3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed.
|
|
34
|
+
|
|
35
|
+
4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions:
|
|
36
|
+
|
|
37
|
+
You must give any other recipients of the Work or Derivative Works a copy of this License; and
|
|
38
|
+
|
|
39
|
+
You must cause any modified files to carry prominent notices stating that You changed the files; and
|
|
40
|
+
|
|
41
|
+
You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and
|
|
42
|
+
|
|
43
|
+
If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License.
|
|
44
|
+
|
|
45
|
+
5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions.
|
|
46
|
+
|
|
47
|
+
6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file.
|
|
48
|
+
|
|
49
|
+
7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License.
|
|
50
|
+
|
|
51
|
+
8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages.
|
|
52
|
+
|
|
53
|
+
9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability.
|
|
54
|
+
|
|
55
|
+
END OF TERMS AND CONDITIONS
|
package/README.md
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
# opencode-cursor-provider
|
|
2
|
+
|
|
3
|
+
Use Cursor models and the Cursor coding agent in OpenCode 2. The plugin uses the official Cursor SDK.
|
|
4
|
+
|
|
5
|
+
The Cursor coding agent can read and change your workspace. OpenCode shows Cursor reasoning and tool activity in the session.
|
|
6
|
+
|
|
7
|
+
## Requirements
|
|
8
|
+
|
|
9
|
+
- OpenCode 2
|
|
10
|
+
- Node.js 22.13 or later
|
|
11
|
+
- A Cursor account or a Cursor API key
|
|
12
|
+
|
|
13
|
+
## Install the plugin
|
|
14
|
+
|
|
15
|
+
Run this command:
|
|
16
|
+
|
|
17
|
+
```sh
|
|
18
|
+
opencode2 plugin add opencode-cursor-provider
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
You can also add the plugin to `opencode.jsonc`:
|
|
22
|
+
|
|
23
|
+
```jsonc
|
|
24
|
+
{
|
|
25
|
+
"plugins": ["opencode-cursor-provider"]
|
|
26
|
+
}
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
Restart OpenCode after you change the plugin configuration.
|
|
30
|
+
|
|
31
|
+
## Connect Cursor
|
|
32
|
+
|
|
33
|
+
1. Start `opencode2`.
|
|
34
|
+
2. Run `/connect`.
|
|
35
|
+
3. Select **Cursor**.
|
|
36
|
+
4. Select browser login, API key, or `CURSOR_API_KEY`.
|
|
37
|
+
|
|
38
|
+
Browser login creates a user API key. Sign in again when the key expires.
|
|
39
|
+
|
|
40
|
+
## Select a model
|
|
41
|
+
|
|
42
|
+
1. Run `/models`.
|
|
43
|
+
2. Select a model under **Cursor**.
|
|
44
|
+
|
|
45
|
+
The plugin gets the model list and the model variants from Cursor. The fallback list includes `cursor/composer-2.5` and `cursor/auto`.
|
|
46
|
+
|
|
47
|
+
## How the plugin works
|
|
48
|
+
|
|
49
|
+
- Cursor runs its own tools and changes the workspace.
|
|
50
|
+
- OpenCode tools stay off for Cursor models.
|
|
51
|
+
- OpenCode shows Cursor tools as provider-executed tool calls.
|
|
52
|
+
- The plugin keeps one Cursor agent for each OpenCode session.
|
|
53
|
+
- The plugin starts a new Cursor agent after a model, directory, mode, conversation, or Cursor agent option change.
|
|
54
|
+
- OpenCode cancellation stops the active Cursor run.
|
|
55
|
+
- You can set Cursor plan mode with `providerOptions.cursor.mode`.
|
|
56
|
+
|
|
57
|
+
The provider supports these Cursor agent options:
|
|
58
|
+
|
|
59
|
+
```ts
|
|
60
|
+
providerOptions: {
|
|
61
|
+
cursor: {
|
|
62
|
+
mode: "plan",
|
|
63
|
+
tools: ["read", "grep"],
|
|
64
|
+
disallowedTools: ["shell"],
|
|
65
|
+
sandboxOptions: { enabled: true },
|
|
66
|
+
autoReview: true,
|
|
67
|
+
settingSources: ["project", "team"],
|
|
68
|
+
},
|
|
69
|
+
}
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
The `tools` and `disallowedTools` options apply only to Cursor tools. They do not add OpenCode tools.
|
|
73
|
+
|
|
74
|
+
Because Cursor settings can load MCP servers, select only settings layers that you trust.
|
|
75
|
+
|
|
76
|
+
## Current limits
|
|
77
|
+
|
|
78
|
+
- The plugin supports text and local image input. It supports text output.
|
|
79
|
+
- The plugin rejects image URLs and non-image file input. It does not discard the input.
|
|
80
|
+
- The plugin rejects structured output requests.
|
|
81
|
+
- The plugin rejects OpenCode tools and explicit tool choice.
|
|
82
|
+
- OpenCode sampling settings do not change Cursor model parameters. The plugin reports those settings as unsupported.
|
|
83
|
+
- Cursor controls tool access and tool approval for its tools.
|
|
84
|
+
- The plugin does not expose Cursor MCP servers, custom tools, extra workspace roots, or a replacement system prompt.
|
|
85
|
+
|
|
86
|
+
## Credentials
|
|
87
|
+
|
|
88
|
+
OpenCode stores the Cursor credentials. The plugin does not write `~/.cursor/sdk/auth.json`.
|
|
89
|
+
|
|
90
|
+
You can set `CURSOR_API_KEY` when you do not want to store a connection in OpenCode.
|
|
91
|
+
|
|
92
|
+
## Develop the plugin
|
|
93
|
+
|
|
94
|
+
Install the dependencies:
|
|
95
|
+
|
|
96
|
+
```sh
|
|
97
|
+
bun install
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
Run the checks:
|
|
101
|
+
|
|
102
|
+
```sh
|
|
103
|
+
bun test
|
|
104
|
+
bun run typecheck
|
|
105
|
+
bun run build
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
The source code is in `src/`. The build writes the package files to `dist/`.
|
|
109
|
+
|
|
110
|
+
## License
|
|
111
|
+
|
|
112
|
+
Apache License 2.0. See [`LICENSE`](LICENSE).
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { ENV_NAME, type CursorApiKey, type EpochMs, type Parsed } from "../ids.ts";
|
|
2
|
+
export type CursorCredential = {
|
|
3
|
+
readonly kind: "oauth";
|
|
4
|
+
readonly apiKey: CursorApiKey;
|
|
5
|
+
readonly expiresAt: EpochMs;
|
|
6
|
+
readonly email?: string;
|
|
7
|
+
} | {
|
|
8
|
+
readonly kind: "key";
|
|
9
|
+
readonly apiKey: CursorApiKey;
|
|
10
|
+
} | {
|
|
11
|
+
readonly kind: "env";
|
|
12
|
+
readonly apiKey: CursorApiKey;
|
|
13
|
+
readonly variable: typeof ENV_NAME;
|
|
14
|
+
};
|
|
15
|
+
export type RenewalPolicy = {
|
|
16
|
+
readonly kind: "none";
|
|
17
|
+
} | {
|
|
18
|
+
readonly kind: "reauth-required";
|
|
19
|
+
readonly at: EpochMs;
|
|
20
|
+
};
|
|
21
|
+
export declare function renewalPolicy(credential: CursorCredential): RenewalPolicy;
|
|
22
|
+
export declare function credentialKey(credential: CursorCredential): CursorApiKey;
|
|
23
|
+
export declare function fromHostCredential(stored: unknown, env?: NodeJS.ProcessEnv): Parsed<CursorCredential>;
|
|
24
|
+
export declare function envCredential(env?: NodeJS.ProcessEnv): Parsed<CursorCredential>;
|
|
25
|
+
export declare function oauthFromLogin(input: {
|
|
26
|
+
apiKey: string;
|
|
27
|
+
email?: string;
|
|
28
|
+
apiKeyExpiresAtMs: number;
|
|
29
|
+
}): Parsed<Extract<CursorCredential, {
|
|
30
|
+
kind: "oauth";
|
|
31
|
+
}>>;
|
|
32
|
+
export declare function keyFromPaste(raw: string): Parsed<CursorCredential>;
|
|
33
|
+
export declare function toHostOAuth(credential: Extract<CursorCredential, {
|
|
34
|
+
kind: "oauth";
|
|
35
|
+
}>): {
|
|
36
|
+
type: "oauth";
|
|
37
|
+
methodID: string;
|
|
38
|
+
refresh: "";
|
|
39
|
+
access: string;
|
|
40
|
+
expires: number;
|
|
41
|
+
metadata?: {
|
|
42
|
+
email: string;
|
|
43
|
+
};
|
|
44
|
+
};
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { ENV_NAME, asEpochMs, fail, ok, parseApiKey } from "../ids.js";
|
|
2
|
+
export function renewalPolicy(credential) {
|
|
3
|
+
if (credential.kind === "oauth")
|
|
4
|
+
return { kind: "reauth-required", at: credential.expiresAt };
|
|
5
|
+
return { kind: "none" };
|
|
6
|
+
}
|
|
7
|
+
export function credentialKey(credential) {
|
|
8
|
+
return credential.apiKey;
|
|
9
|
+
}
|
|
10
|
+
function isRecord(value) {
|
|
11
|
+
return typeof value === "object" && value !== null;
|
|
12
|
+
}
|
|
13
|
+
export function fromHostCredential(stored, env = process.env) {
|
|
14
|
+
if (!isRecord(stored))
|
|
15
|
+
return fail("stored credential is not an object");
|
|
16
|
+
if (stored.type === "oauth") {
|
|
17
|
+
const key = parseApiKey(stored.access);
|
|
18
|
+
if (!key.ok)
|
|
19
|
+
return key;
|
|
20
|
+
const email = isRecord(stored.metadata) && typeof stored.metadata.email === "string" ? stored.metadata.email : undefined;
|
|
21
|
+
const expiresAt = parseExpires(stored.expires);
|
|
22
|
+
if (expiresAt === undefined) {
|
|
23
|
+
return ok({ kind: "key", apiKey: key.value });
|
|
24
|
+
}
|
|
25
|
+
return ok({
|
|
26
|
+
kind: "oauth",
|
|
27
|
+
apiKey: key.value,
|
|
28
|
+
expiresAt,
|
|
29
|
+
...(email === undefined ? {} : { email }),
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
if (stored.type === "key") {
|
|
33
|
+
const key = parseApiKey(stored.key);
|
|
34
|
+
if (!key.ok)
|
|
35
|
+
return key;
|
|
36
|
+
return ok({ kind: "key", apiKey: key.value });
|
|
37
|
+
}
|
|
38
|
+
if (stored.type === "env") {
|
|
39
|
+
const name = stored.name === ENV_NAME ? ENV_NAME : ENV_NAME;
|
|
40
|
+
const key = parseApiKey(env[name]);
|
|
41
|
+
if (!key.ok)
|
|
42
|
+
return fail(`${name} is not set`);
|
|
43
|
+
return ok({ kind: "env", apiKey: key.value, variable: name });
|
|
44
|
+
}
|
|
45
|
+
return fail("unknown credential type");
|
|
46
|
+
}
|
|
47
|
+
export function envCredential(env = process.env) {
|
|
48
|
+
const key = parseApiKey(env[ENV_NAME]);
|
|
49
|
+
if (!key.ok)
|
|
50
|
+
return fail(`${ENV_NAME} is not set`);
|
|
51
|
+
return ok({ kind: "env", apiKey: key.value, variable: ENV_NAME });
|
|
52
|
+
}
|
|
53
|
+
export function oauthFromLogin(input) {
|
|
54
|
+
const key = parseApiKey(input.apiKey);
|
|
55
|
+
if (!key.ok)
|
|
56
|
+
return key;
|
|
57
|
+
return ok({
|
|
58
|
+
kind: "oauth",
|
|
59
|
+
apiKey: key.value,
|
|
60
|
+
expiresAt: asEpochMs(input.apiKeyExpiresAtMs),
|
|
61
|
+
...(input.email === undefined ? {} : { email: input.email }),
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
export function keyFromPaste(raw) {
|
|
65
|
+
const key = parseApiKey(raw);
|
|
66
|
+
if (!key.ok)
|
|
67
|
+
return key;
|
|
68
|
+
return ok({ kind: "key", apiKey: key.value });
|
|
69
|
+
}
|
|
70
|
+
function parseExpires(value) {
|
|
71
|
+
if (typeof value !== "number" || !Number.isFinite(value) || value <= 0)
|
|
72
|
+
return undefined;
|
|
73
|
+
const ms = value >= 1_000_000_000 && value < 1_000_000_000_000 ? value * 1000 : value;
|
|
74
|
+
return asEpochMs(ms);
|
|
75
|
+
}
|
|
76
|
+
export function toHostOAuth(credential) {
|
|
77
|
+
return {
|
|
78
|
+
type: "oauth",
|
|
79
|
+
methodID: "oauth",
|
|
80
|
+
refresh: "",
|
|
81
|
+
access: credential.apiKey,
|
|
82
|
+
expires: credential.expiresAt,
|
|
83
|
+
...(credential.email === undefined ? {} : { metadata: { email: credential.email } }),
|
|
84
|
+
};
|
|
85
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { Plugin } from "@opencode-ai/plugin";
|
|
2
|
+
import type { IntegrationEditor } from "@opencode-ai/plugin/promise/integration";
|
|
3
|
+
import { usableKey } from "./state.ts";
|
|
4
|
+
export interface CursorLink {
|
|
5
|
+
resolve(): Promise<ReturnType<typeof usableKey>>;
|
|
6
|
+
register(draft: IntegrationEditor): void;
|
|
7
|
+
restore(ctx: Plugin.Context): Promise<void>;
|
|
8
|
+
reject(): void;
|
|
9
|
+
onLinked(listener: () => void): () => void;
|
|
10
|
+
}
|
|
11
|
+
export declare function createCursorLink(input?: {
|
|
12
|
+
env?: NodeJS.ProcessEnv;
|
|
13
|
+
clock?: () => number;
|
|
14
|
+
}): CursorLink;
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import { Cursor } from "@cursor/sdk";
|
|
2
|
+
import { Credential, Integration, Plugin } from "@opencode-ai/plugin";
|
|
3
|
+
import { ENV_NAME, INTEGRATION_ID, OAUTH_METHOD_ID, nowMs } from "../ids.js";
|
|
4
|
+
import { envCredential, fromHostCredential, oauthFromLogin, toHostOAuth } from "./credential.js";
|
|
5
|
+
import { reduce, usableKey } from "./state.js";
|
|
6
|
+
export function createCursorLink(input = {}) {
|
|
7
|
+
const env = input.env ?? process.env;
|
|
8
|
+
const clock = input.clock ?? Date.now;
|
|
9
|
+
let state = { status: "unlinked" };
|
|
10
|
+
const listeners = new Set();
|
|
11
|
+
function emit() {
|
|
12
|
+
for (const listener of listeners)
|
|
13
|
+
listener();
|
|
14
|
+
}
|
|
15
|
+
function assign(next) {
|
|
16
|
+
state = next;
|
|
17
|
+
}
|
|
18
|
+
return {
|
|
19
|
+
async resolve() {
|
|
20
|
+
assign(reduce(state, { type: "tick", now: nowMs(clock) }));
|
|
21
|
+
return usableKey(state);
|
|
22
|
+
},
|
|
23
|
+
reject() {
|
|
24
|
+
assign(reduce(state, { type: "rejected-by-cursor" }));
|
|
25
|
+
},
|
|
26
|
+
async restore(ctx) {
|
|
27
|
+
assign(reduce(state, { type: "restore", stored: await loadStored(ctx, env), now: nowMs(clock) }));
|
|
28
|
+
},
|
|
29
|
+
onLinked(listener) {
|
|
30
|
+
listeners.add(listener);
|
|
31
|
+
return () => {
|
|
32
|
+
listeners.delete(listener);
|
|
33
|
+
};
|
|
34
|
+
},
|
|
35
|
+
register(draft) {
|
|
36
|
+
draft.update(INTEGRATION_ID, (integration) => {
|
|
37
|
+
integration.id = INTEGRATION_ID;
|
|
38
|
+
integration.name = "Cursor";
|
|
39
|
+
});
|
|
40
|
+
draft.method.update({
|
|
41
|
+
integrationID: INTEGRATION_ID,
|
|
42
|
+
method: {
|
|
43
|
+
id: OAUTH_METHOD_ID,
|
|
44
|
+
type: "oauth",
|
|
45
|
+
label: "Cursor account (browser login)",
|
|
46
|
+
},
|
|
47
|
+
authorize: async () => {
|
|
48
|
+
const abort = new AbortController();
|
|
49
|
+
let settleUrl = () => { };
|
|
50
|
+
const url = new Promise((resolve) => {
|
|
51
|
+
settleUrl = resolve;
|
|
52
|
+
});
|
|
53
|
+
const login = Cursor.auth.login({
|
|
54
|
+
openBrowser: false,
|
|
55
|
+
onLoginUrl: settleUrl,
|
|
56
|
+
store: null,
|
|
57
|
+
signal: abort.signal,
|
|
58
|
+
apiKeyName: "OpenCode",
|
|
59
|
+
});
|
|
60
|
+
const loginUrl = await url;
|
|
61
|
+
assign(reduce(state, {
|
|
62
|
+
type: "login-started",
|
|
63
|
+
url: loginUrl,
|
|
64
|
+
abort,
|
|
65
|
+
now: nowMs(clock),
|
|
66
|
+
}));
|
|
67
|
+
return {
|
|
68
|
+
url: loginUrl,
|
|
69
|
+
instructions: "Open this URL in a browser to sign in to Cursor.",
|
|
70
|
+
mode: "auto",
|
|
71
|
+
callback: login.then((result) => {
|
|
72
|
+
const parsed = oauthFromLogin(result);
|
|
73
|
+
if (!parsed.ok) {
|
|
74
|
+
assign(reduce(state, { type: "login-failed", detail: parsed.issue }));
|
|
75
|
+
throw new Error(parsed.issue);
|
|
76
|
+
}
|
|
77
|
+
assign(reduce(state, { type: "login-succeeded", credential: parsed.value, now: nowMs(clock) }));
|
|
78
|
+
emit();
|
|
79
|
+
const host = toHostOAuth(parsed.value);
|
|
80
|
+
return {
|
|
81
|
+
type: "oauth",
|
|
82
|
+
methodID: Integration.MethodID.make(OAUTH_METHOD_ID),
|
|
83
|
+
refresh: host.refresh,
|
|
84
|
+
access: host.access,
|
|
85
|
+
expires: host.expires,
|
|
86
|
+
...(host.metadata === undefined ? {} : { metadata: host.metadata }),
|
|
87
|
+
};
|
|
88
|
+
}),
|
|
89
|
+
};
|
|
90
|
+
},
|
|
91
|
+
label: (credential) => {
|
|
92
|
+
const email = credential.metadata && typeof credential.metadata.email === "string" ? credential.metadata.email : undefined;
|
|
93
|
+
return email ?? "Cursor account";
|
|
94
|
+
},
|
|
95
|
+
});
|
|
96
|
+
draft.method.update({
|
|
97
|
+
integrationID: INTEGRATION_ID,
|
|
98
|
+
method: { type: "key", label: "API key (cursor.com/settings)" },
|
|
99
|
+
});
|
|
100
|
+
draft.method.update({
|
|
101
|
+
integrationID: INTEGRATION_ID,
|
|
102
|
+
method: { type: "env", names: [ENV_NAME] },
|
|
103
|
+
});
|
|
104
|
+
},
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
async function loadStored(ctx, env) {
|
|
108
|
+
try {
|
|
109
|
+
const connection = await ctx.integration.connection.active(INTEGRATION_ID);
|
|
110
|
+
if (connection) {
|
|
111
|
+
const stored = await ctx.integration.connection.resolve(connection);
|
|
112
|
+
if (stored)
|
|
113
|
+
return fromHostCredential(stored, env);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
catch {
|
|
117
|
+
// Host lookup is best-effort. CURSOR_API_KEY is the fallback.
|
|
118
|
+
}
|
|
119
|
+
return envCredential(env);
|
|
120
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { type CursorApiKey, type EpochMs, type Parsed } from "../ids.ts";
|
|
2
|
+
import { type CursorCredential, type RenewalPolicy } from "./credential.ts";
|
|
3
|
+
export declare const WARN_WINDOW_MS: number;
|
|
4
|
+
export type AuthState = {
|
|
5
|
+
readonly status: "unlinked";
|
|
6
|
+
} | {
|
|
7
|
+
readonly status: "linking";
|
|
8
|
+
readonly url: string;
|
|
9
|
+
readonly startedAt: EpochMs;
|
|
10
|
+
readonly abort: AbortController;
|
|
11
|
+
} | {
|
|
12
|
+
readonly status: "linked";
|
|
13
|
+
readonly credential: CursorCredential;
|
|
14
|
+
readonly renewal: RenewalPolicy;
|
|
15
|
+
} | {
|
|
16
|
+
readonly status: "stale";
|
|
17
|
+
readonly credential: CursorCredential;
|
|
18
|
+
readonly expiresAt: EpochMs;
|
|
19
|
+
} | {
|
|
20
|
+
readonly status: "expired";
|
|
21
|
+
readonly expiredAt: EpochMs;
|
|
22
|
+
} | {
|
|
23
|
+
readonly status: "failed";
|
|
24
|
+
readonly detail: string;
|
|
25
|
+
};
|
|
26
|
+
export type AuthEvent = {
|
|
27
|
+
readonly type: "restore";
|
|
28
|
+
readonly stored: Parsed<CursorCredential>;
|
|
29
|
+
readonly now: EpochMs;
|
|
30
|
+
} | {
|
|
31
|
+
readonly type: "login-started";
|
|
32
|
+
readonly url: string;
|
|
33
|
+
readonly abort: AbortController;
|
|
34
|
+
readonly now: EpochMs;
|
|
35
|
+
} | {
|
|
36
|
+
readonly type: "login-succeeded";
|
|
37
|
+
readonly credential: CursorCredential;
|
|
38
|
+
readonly now: EpochMs;
|
|
39
|
+
} | {
|
|
40
|
+
readonly type: "login-failed";
|
|
41
|
+
readonly detail: string;
|
|
42
|
+
} | {
|
|
43
|
+
readonly type: "login-cancelled";
|
|
44
|
+
} | {
|
|
45
|
+
readonly type: "tick";
|
|
46
|
+
readonly now: EpochMs;
|
|
47
|
+
} | {
|
|
48
|
+
readonly type: "rejected-by-cursor";
|
|
49
|
+
} | {
|
|
50
|
+
readonly type: "unlink";
|
|
51
|
+
};
|
|
52
|
+
export declare function classify(credential: CursorCredential, now: EpochMs): AuthState;
|
|
53
|
+
export declare function reduce(state: AuthState, event: AuthEvent): AuthState;
|
|
54
|
+
export declare function usableKey(state: AuthState): CursorApiKey | undefined;
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { asEpochMs } from "../ids.js";
|
|
2
|
+
import { credentialKey, renewalPolicy } from "./credential.js";
|
|
3
|
+
export const WARN_WINDOW_MS = 7 * 24 * 60 * 60 * 1000;
|
|
4
|
+
export function classify(credential, now) {
|
|
5
|
+
if (credential.kind !== "oauth") {
|
|
6
|
+
return { status: "linked", credential, renewal: renewalPolicy(credential) };
|
|
7
|
+
}
|
|
8
|
+
if (now >= credential.expiresAt) {
|
|
9
|
+
return { status: "expired", expiredAt: credential.expiresAt };
|
|
10
|
+
}
|
|
11
|
+
if (now >= asEpochMs(credential.expiresAt - WARN_WINDOW_MS)) {
|
|
12
|
+
return { status: "stale", credential, expiresAt: credential.expiresAt };
|
|
13
|
+
}
|
|
14
|
+
return { status: "linked", credential, renewal: renewalPolicy(credential) };
|
|
15
|
+
}
|
|
16
|
+
export function reduce(state, event) {
|
|
17
|
+
switch (event.type) {
|
|
18
|
+
case "restore":
|
|
19
|
+
if (!event.stored.ok)
|
|
20
|
+
return { status: "unlinked" };
|
|
21
|
+
return classify(event.stored.value, event.now);
|
|
22
|
+
case "login-started":
|
|
23
|
+
return {
|
|
24
|
+
status: "linking",
|
|
25
|
+
url: event.url,
|
|
26
|
+
startedAt: event.now,
|
|
27
|
+
abort: event.abort,
|
|
28
|
+
};
|
|
29
|
+
case "login-succeeded":
|
|
30
|
+
return classify(event.credential, event.now);
|
|
31
|
+
case "login-failed":
|
|
32
|
+
return { status: "failed", detail: event.detail };
|
|
33
|
+
case "login-cancelled":
|
|
34
|
+
return { status: "unlinked" };
|
|
35
|
+
case "tick":
|
|
36
|
+
if (state.status === "linked" || state.status === "stale") {
|
|
37
|
+
return classify(state.credential, event.now);
|
|
38
|
+
}
|
|
39
|
+
return state;
|
|
40
|
+
case "rejected-by-cursor":
|
|
41
|
+
return { status: "unlinked" };
|
|
42
|
+
case "unlink":
|
|
43
|
+
return { status: "unlinked" };
|
|
44
|
+
default: {
|
|
45
|
+
const _exhaustive = event;
|
|
46
|
+
return _exhaustive;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
export function usableKey(state) {
|
|
51
|
+
if (state.status === "linked" || state.status === "stale")
|
|
52
|
+
return credentialKey(state.credential);
|
|
53
|
+
return undefined;
|
|
54
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { type ModelParameterValue, type Run, type SDKUserMessage, type SendOptions } from "@cursor/sdk";
|
|
2
|
+
import { type CursorAgentID, type CursorApiKey } from "../ids.ts";
|
|
3
|
+
import type { CursorAgentOptions } from "../model/provider-options.ts";
|
|
4
|
+
export interface CursorAgentSession {
|
|
5
|
+
readonly id: CursorAgentID;
|
|
6
|
+
send(prompt: string | SDKUserMessage, options?: SendOptions): Promise<Run>;
|
|
7
|
+
dispose(): Promise<void>;
|
|
8
|
+
}
|
|
9
|
+
export declare function openCursorAgent(input: {
|
|
10
|
+
apiKey: CursorApiKey;
|
|
11
|
+
wireID: string;
|
|
12
|
+
params: readonly ModelParameterValue[] | undefined;
|
|
13
|
+
cwd: string;
|
|
14
|
+
resume: CursorAgentID | undefined;
|
|
15
|
+
agentOptions: CursorAgentOptions | undefined;
|
|
16
|
+
}): Promise<CursorAgentSession>;
|
|
17
|
+
export declare function isAgentLost(error: unknown): boolean;
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { Agent, AgentNotFoundError, } from "@cursor/sdk";
|
|
2
|
+
import { asAgentID } from "../ids.js";
|
|
3
|
+
export async function openCursorAgent(input) {
|
|
4
|
+
const model = {
|
|
5
|
+
id: input.wireID,
|
|
6
|
+
...(input.params === undefined ? {} : { params: input.params.map((param) => ({ ...param })) }),
|
|
7
|
+
};
|
|
8
|
+
const options = {
|
|
9
|
+
apiKey: input.apiKey,
|
|
10
|
+
model,
|
|
11
|
+
...(input.agentOptions?.tools === undefined ? {} : { tools: [...input.agentOptions.tools] }),
|
|
12
|
+
...(input.agentOptions?.disallowedTools === undefined
|
|
13
|
+
? {}
|
|
14
|
+
: { disallowedTools: [...input.agentOptions.disallowedTools] }),
|
|
15
|
+
local: {
|
|
16
|
+
cwd: input.cwd,
|
|
17
|
+
...(input.agentOptions?.autoReview === undefined ? {} : { autoReview: input.agentOptions.autoReview }),
|
|
18
|
+
...(input.agentOptions?.settingSources === undefined
|
|
19
|
+
? {}
|
|
20
|
+
: { settingSources: [...input.agentOptions.settingSources] }),
|
|
21
|
+
...(input.agentOptions?.sandboxOptions === undefined
|
|
22
|
+
? {}
|
|
23
|
+
: { sandboxOptions: { ...input.agentOptions.sandboxOptions } }),
|
|
24
|
+
},
|
|
25
|
+
};
|
|
26
|
+
const agent = input.resume === undefined ? await Agent.create(options) : await Agent.resume(input.resume, options);
|
|
27
|
+
return {
|
|
28
|
+
id: asAgentID(agent.agentId),
|
|
29
|
+
send(prompt, sendOptions) {
|
|
30
|
+
return agent.send(prompt, sendOptions);
|
|
31
|
+
},
|
|
32
|
+
dispose() {
|
|
33
|
+
return agent[Symbol.asyncDispose]();
|
|
34
|
+
},
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
export function isAgentLost(error) {
|
|
38
|
+
return error instanceof AgentNotFoundError;
|
|
39
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import type { AgentModeOption, ModelParameterValue } from "@cursor/sdk";
|
|
2
|
+
import type { CatalogModelID, CursorAgentID, EpochMs, OpencodeSessionID } from "../ids.ts";
|
|
3
|
+
import type { CursorAgentOptions } from "../model/provider-options.ts";
|
|
4
|
+
import { type Conversation, type ConversationCheckpoint } from "./conversation.ts";
|
|
5
|
+
export interface SessionAgentBinding {
|
|
6
|
+
readonly sessionID: OpencodeSessionID;
|
|
7
|
+
readonly agentID: CursorAgentID;
|
|
8
|
+
readonly modelID: CatalogModelID;
|
|
9
|
+
readonly cwd: string;
|
|
10
|
+
readonly checkpoint: ConversationCheckpoint;
|
|
11
|
+
readonly params: readonly ModelParameterValue[] | undefined;
|
|
12
|
+
readonly mode: AgentModeOption | undefined;
|
|
13
|
+
readonly agentOptions: CursorAgentOptions | undefined;
|
|
14
|
+
readonly lastUsedAt: EpochMs;
|
|
15
|
+
}
|
|
16
|
+
export interface TurnScope {
|
|
17
|
+
readonly sessionID: OpencodeSessionID;
|
|
18
|
+
readonly cwd: string;
|
|
19
|
+
}
|
|
20
|
+
export type RouteKind = "ONE_SHOT" | "FRESH" | "RESUME";
|
|
21
|
+
export declare function route(input: {
|
|
22
|
+
readonly scope: TurnScope | undefined;
|
|
23
|
+
readonly binding: SessionAgentBinding | undefined;
|
|
24
|
+
readonly modelID: CatalogModelID;
|
|
25
|
+
readonly conversation: Conversation;
|
|
26
|
+
readonly params: readonly ModelParameterValue[] | undefined;
|
|
27
|
+
readonly mode: AgentModeOption | undefined;
|
|
28
|
+
readonly agentOptions: CursorAgentOptions | undefined;
|
|
29
|
+
}): RouteKind;
|
|
30
|
+
export interface BindingStore {
|
|
31
|
+
get(sessionID: OpencodeSessionID): SessionAgentBinding | undefined;
|
|
32
|
+
put(binding: SessionAgentBinding): void;
|
|
33
|
+
drop(sessionID: OpencodeSessionID): void;
|
|
34
|
+
}
|
|
35
|
+
export declare function createBindingStore(): BindingStore;
|