@caph42/pi-cursor-provider 0.3.0 → 0.4.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/README.md +14 -0
- package/package.json +1 -1
- package/src/config.ts +76 -0
- package/src/index.ts +25 -13
package/README.md
CHANGED
|
@@ -41,6 +41,20 @@ Then select a model:
|
|
|
41
41
|
/model cursor/<id>
|
|
42
42
|
```
|
|
43
43
|
|
|
44
|
+
## Hide models
|
|
45
|
+
|
|
46
|
+
`/model` lists every model Cursor offers the account. To shorten it, put `cursor.json` beside Pi's config (`$PI_CODING_AGENT_DIR/cursor.json`, else `~/.pi/agent/cursor.json`):
|
|
47
|
+
|
|
48
|
+
```json
|
|
49
|
+
{ "hiddenModels": ["claude-*", "gpt-5", "cursor/gpt-5-mini"] }
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
A pattern matches the whole id `/model` shows (the part after `cursor/`), ignoring case. `*` stands for any run of characters; every other character, `?` included, is literal. A pattern without the `@300k` size suffix hides every context size of that model. A missing or malformed file hides nothing, and `/cursor.doctor` prints the patterns in force. Reopen `/model` to see the new list.
|
|
53
|
+
|
|
54
|
+
Pi resolves models through the provider's list, so a hidden model is also unselectable by id, and a session resumed on one falls back to another model with Pi's "model no longer exists" warning. A session already running on a hidden model keeps it until the model changes. `/cursor.usage` still classifies usage by every model the account has.
|
|
55
|
+
|
|
56
|
+
If resume must keep working for the scoped-away models, Pi's own `enabledModels` setting scopes `/model` without removing anything from the registry: `{ "enabledModels": ["cursor/*sonnet*"] }` opens the picker on that scope, and Tab still reaches the rest.
|
|
57
|
+
|
|
44
58
|
## How this relates to other Cursor packages
|
|
45
59
|
|
|
46
60
|
| Package | Role |
|
package/package.json
CHANGED
package/src/config.ts
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Which Cursor models Pi's picker offers.
|
|
3
|
+
*
|
|
4
|
+
* Cursor lists every model (and every context size and effort of it) a
|
|
5
|
+
* subscription may use; that is long enough that the useful ones get lost in
|
|
6
|
+
* `/model`. `cursor.json` in Pi's config directory (`PI_CODING_AGENT_DIR`,
|
|
7
|
+
* else `~/.pi/agent`) names what to hide:
|
|
8
|
+
*
|
|
9
|
+
* { "hiddenModels": ["claude-*", "gpt-5", "cursor/gpt-5-mini"] }
|
|
10
|
+
*
|
|
11
|
+
* A pattern globs the id `/model` shows (the part after `cursor/`): `*` is any
|
|
12
|
+
* run of characters, everything else is literal, and the match ignores case.
|
|
13
|
+
* Hiding is a picker filter only: a session already on that model keeps it. A
|
|
14
|
+
* missing, unreadable or malformed file hides nothing.
|
|
15
|
+
*/
|
|
16
|
+
import { readFileSync } from "node:fs"
|
|
17
|
+
import { homedir } from "node:os"
|
|
18
|
+
import { join } from "node:path"
|
|
19
|
+
|
|
20
|
+
export const CONFIG_FILE = "cursor.json"
|
|
21
|
+
|
|
22
|
+
/** `cursor.json` beside the rest of Pi's config. */
|
|
23
|
+
export function configPath(env: NodeJS.ProcessEnv = process.env): string {
|
|
24
|
+
const dir = (env.PI_CODING_AGENT_DIR ?? "").trim()
|
|
25
|
+
return join(dir || join(homedir(), ".pi", "agent"), CONFIG_FILE)
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** `hiddenModels` as patterns: strings only, trimmed, empty ones dropped. */
|
|
29
|
+
function patternsOf(value: unknown): string[] {
|
|
30
|
+
if (!Array.isArray(value)) return []
|
|
31
|
+
return value.flatMap((v) => (typeof v === "string" && v.trim() ? [v.trim()] : []))
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** The patterns in `cursor.json`. A file that is absent or not JSON hides nothing. */
|
|
35
|
+
export function hiddenPatterns(path: string = configPath()): string[] {
|
|
36
|
+
let text: string
|
|
37
|
+
try {
|
|
38
|
+
text = readFileSync(path, "utf8")
|
|
39
|
+
} catch {
|
|
40
|
+
return []
|
|
41
|
+
}
|
|
42
|
+
try {
|
|
43
|
+
const json: unknown = JSON.parse(text)
|
|
44
|
+
return patternsOf((json as { hiddenModels?: unknown } | null)?.hiddenModels)
|
|
45
|
+
} catch {
|
|
46
|
+
return []
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** A pattern as a regex: `*` any run, the rest literal (a `?` is a question mark, not a wildcard),
|
|
51
|
+
* the whole id, ignoring case. */
|
|
52
|
+
function globRe(pattern: string): RegExp {
|
|
53
|
+
const body = pattern.replace(/[.+?^${}()|[\]\\]/g, "\\$&").replace(/\*/g, ".*")
|
|
54
|
+
return new RegExp(`^${body}$`, "i")
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** A model id as /model spells it: no `cursor/` prefix. */
|
|
58
|
+
const bare = (id: string) => (id.startsWith("cursor/") ? id.slice("cursor/".length) : id)
|
|
59
|
+
|
|
60
|
+
/** Whether any pattern hides this model id. Both the id and the pattern may carry `cursor/`, and a
|
|
61
|
+
* pattern without the `@size` suffix hides every context size of that model. */
|
|
62
|
+
export function hidden(patterns: string[], id: string): boolean {
|
|
63
|
+
const name = bare(id)
|
|
64
|
+
const at = name.indexOf("@")
|
|
65
|
+
const family = at < 0 ? name : name.slice(0, at)
|
|
66
|
+
return patterns.some((p) => {
|
|
67
|
+
const re = globRe(bare(p))
|
|
68
|
+
return re.test(name) || re.test(family)
|
|
69
|
+
})
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** The models a pattern list keeps, in order. No patterns keeps all of them. */
|
|
73
|
+
export function visible<T extends { id: string }>(models: T[], patterns: string[]): T[] {
|
|
74
|
+
if (!patterns.length) return models
|
|
75
|
+
return models.filter((m) => !hidden(patterns, m.id))
|
|
76
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -10,26 +10,35 @@ import type { RefreshModelsContext } from "@earendil-works/pi-ai"
|
|
|
10
10
|
import type { ExtensionAPI, ProviderModelConfig } from "@earendil-works/pi-coding-agent"
|
|
11
11
|
import { login, refresh, resolveAccessToken } from "./auth.ts"
|
|
12
12
|
import { AGENT, clientVersion } from "./client.ts"
|
|
13
|
+
import { configPath, hiddenPatterns, visible } from "./config.ts"
|
|
13
14
|
import { piModelConfigs, usable } from "./models.ts"
|
|
14
15
|
import { streamCursor } from "./pi-stream.ts"
|
|
15
16
|
import { fetchUsage, formatUsage } from "./usage.ts"
|
|
16
17
|
|
|
17
18
|
export { fetchUsage, formatUsage, onDemandOf } from "./usage.ts"
|
|
18
19
|
|
|
20
|
+
// offered is every model Cursor offers the account; models is what cursor.json keeps
|
|
21
|
+
let offered: ProviderModelConfig[] = []
|
|
19
22
|
let models: ProviderModelConfig[] = []
|
|
20
23
|
|
|
21
24
|
async function refreshModels(ctx: RefreshModelsContext): Promise<ProviderModelConfig[]> {
|
|
22
|
-
if (ctx.signal.aborted
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
25
|
+
if (ctx.signal.aborted) return models
|
|
26
|
+
if (ctx.allowNetwork) {
|
|
27
|
+
const fromCred =
|
|
28
|
+
ctx.credential?.type === "oauth" ? ctx.credential.access : ctx.credential?.type === "api_key" ? ctx.credential.key : undefined
|
|
29
|
+
const auth = await resolveAccessToken({ apiKey: fromCred })
|
|
30
|
+
if (auth) {
|
|
31
|
+
try {
|
|
32
|
+
offered = piModelConfigs(await usable(auth.token, ctx.force === true)) as ProviderModelConfig[]
|
|
33
|
+
} catch {
|
|
34
|
+
// keep the list Cursor gave last; only the hide list below changes
|
|
35
|
+
}
|
|
36
|
+
}
|
|
32
37
|
}
|
|
38
|
+
// cursor.json is read on every refresh, network or not, so a hidden model leaves
|
|
39
|
+
// the picker on the next list refresh
|
|
40
|
+
models = visible(offered, hiddenPatterns())
|
|
41
|
+
return models
|
|
33
42
|
}
|
|
34
43
|
|
|
35
44
|
export default function (pi: ExtensionAPI) {
|
|
@@ -56,11 +65,13 @@ export default function (pi: ExtensionAPI) {
|
|
|
56
65
|
const extra = args.trim() ? " This command does not take arguments." : ""
|
|
57
66
|
const auth = await resolveAccessToken()
|
|
58
67
|
const version = await clientVersion()
|
|
68
|
+
const patterns = hiddenPatterns()
|
|
69
|
+
const hiddenText = patterns.length ? `Hidden from /model: ${patterns.join(", ")} (${configPath()}).` : `No models hidden (${configPath()}).`
|
|
59
70
|
if (!auth) {
|
|
60
|
-
ctx.ui.notify(`Cursor is not signed in (${version}). Run /login cursor or set CURSOR_ACCESS_TOKEN
|
|
71
|
+
ctx.ui.notify(`Cursor is not signed in (${version}). Run /login cursor or set CURSOR_ACCESS_TOKEN. ${hiddenText}${extra}`, "warning")
|
|
61
72
|
return
|
|
62
73
|
}
|
|
63
|
-
ctx.ui.notify(`Cursor auth source: ${auth.source}. Client ${version}. Token not shown.${extra}`, "info")
|
|
74
|
+
ctx.ui.notify(`Cursor auth source: ${auth.source}. Client ${version}. ${hiddenText} Token not shown.${extra}`, "info")
|
|
64
75
|
},
|
|
65
76
|
})
|
|
66
77
|
|
|
@@ -74,7 +85,8 @@ export default function (pi: ExtensionAPI) {
|
|
|
74
85
|
ctx.ui.notify(`Cursor is not signed in (${version}). Run /login cursor or set CURSOR_ACCESS_TOKEN.${extra}`, "warning")
|
|
75
86
|
return
|
|
76
87
|
}
|
|
77
|
-
|
|
88
|
+
// every model the account has: usage windows classify events by these, hidden or not
|
|
89
|
+
let ids = offered.map((m) => m.id)
|
|
78
90
|
if (!ids.length) {
|
|
79
91
|
try {
|
|
80
92
|
ids = (await usable(auth.token)).map((m: { id: string }) => m.id)
|