@caph42/pi-cursor-provider 0.7.0 → 0.8.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/CHANGELOG.md CHANGED
@@ -2,6 +2,15 @@
2
2
 
3
3
  ## Unreleased
4
4
 
5
+ ## 0.8.0
6
+
7
+ - Selecting another Cursor model while fast is on carries it to that model when the response is finished and the model supports fast at the current thinking level. The previous model stays on. A busy response, a model without support, or a model that is already on does not record another change. Turning it off does not turn off the model being left.
8
+ - `/cursor.fast` now remembers the models left on for the session's working directory. The record is `cursor-fast.json` beside `cursor.json`, separate from that hand-edited file. A new session in the same directory inherits it. The first change on a branch records the inherited models too, so a reload keeps them. A branch that already chose, including one that turned fast off, keeps its own choice. A different directory, and a directory change inside an existing session, do not move the switch. A failed write still changes the current session and says the directory preference was not saved.
9
+
10
+ ## 0.7.1
11
+
12
+ - Republished after npm staged 0.7.0 without making it installable.
13
+
5
14
  ## 0.7.0
6
15
 
7
16
  - `/cursor.fast` with no argument toggles the current model between off and on. `/cursor.fast on`, `/cursor.fast off`, and `/cursor.fast status` are unchanged.
package/README.md CHANGED
@@ -109,7 +109,7 @@ Select a Cursor model, then use:
109
109
 
110
110
  With no argument, `/cursor.fast` toggles the current model: off becomes on, and on becomes off. `/cursor.fast on` and `/cursor.fast off` set it explicitly. In the TUI, type `/cursor.fast ` and press Tab to choose `on` or `off`; a unique prefix such as `of` completes to `off` without executing the command. `/cursor.fast status` only reports the current state. In the TUI, enabled fast shows `Fast` on the second footer row beside the model/thinking information. Off shows nothing there. Other extension statuses retain their own row. The same text is also published through `ctx.ui.setStatus`, so a footer that renders extension statuses — Pi's own footer, or pi-ccline's — still shows it when another extension owns the footer slot; Cursor's footer filters that key out, so it never appears twice. RPC clients continue receiving the `cursor-fast` status text. Enabling fast preserves the selected model ID, context size, and thinking level. It requires an account-available fast variant at the same context and effective thinking parameters; unsupported combinations are rejected.
111
111
 
112
- The setting defaults to off and belongs to the current model in the current session. Other models keep their own settings. Reloading or resuming that session restores the setting from its current branch; a new session starts with fast off. Changes apply to the next request and require the current response to finish first.
112
+ The setting defaults to off and belongs to the current model. Selecting another Cursor model while fast is on carries that choice when the response is finished and the new model supports fast at the current thinking level; a busy response, a model without fast, or a model that is already on leaves the recorded choices unchanged. Turning it off does not turn off the model being left. Turning it on or off is recorded in the current session branch and in `cursor-fast.json` beside `cursor.json`, keyed by the session's working directory. The first change on a branch also records the models that branch inherited, so a later reload keeps every model, not only the one just changed. A branch that already chose — including one that turned fast off — wins over the file. A new session with no choice of its own inherits the models last left on in that directory, so the same project starts with fast on again; a different directory does not. Changing directory inside a session does not move the switch. If the directory file cannot be written, the change still applies to this session and Pi says it was not saved. Changes apply to the next request and require the current response to finish first.
113
113
 
114
114
  If fast is enabled and a later thinking change or catalogue update removes support, the footer shows `Fast unavailable` and requests report an error until you select a supported thinking level or run `/cursor.fast off`. The command does not add duplicate fast models to `/model`.
115
115
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@caph42/pi-cursor-provider",
3
- "version": "0.7.0",
3
+ "version": "0.8.0",
4
4
  "description": "Cursor provider for Pi: one HTTP/2 AgentService/Run per turn (magpie-aligned).",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -0,0 +1,81 @@
1
+ /**
2
+ * Fast preferences remembered by the session's working directory.
3
+ *
4
+ * A session branch is still the source of truth once it has its own
5
+ * `cursor-fast` entries. A new session in a directory that was used before
6
+ * starts from the models last left on here, instead of the global default of
7
+ * off. The file sits beside `cursor.json` and is never merged into it:
8
+ * that file is hand-edited, and a bad write must not change what it hides.
9
+ *
10
+ * { "directories": { "/abs/project": ["grok-test@256k"] } }
11
+ *
12
+ * Keys are real paths. A missing, unreadable, or malformed file remembers
13
+ * nothing, and a failed write leaves the command's session entry in place.
14
+ */
15
+ import { readFileSync, realpathSync, renameSync, writeFileSync } from "node:fs"
16
+ import { homedir } from "node:os"
17
+ import { join, normalize, resolve } from "node:path"
18
+
19
+ export const FAST_MEMORY_FILE = "cursor-fast.json"
20
+
21
+ /** `cursor-fast.json` beside the rest of Pi's config. */
22
+ export function fastMemoryPath(env: NodeJS.ProcessEnv = process.env): string {
23
+ const dir = (env.PI_CODING_AGENT_DIR ?? "").trim()
24
+ return join(dir || join(homedir(), ".pi", "agent"), FAST_MEMORY_FILE)
25
+ }
26
+
27
+ /** One spelling for a directory: the real path when it exists, otherwise the absolute path without `.` or `..`. */
28
+ export function canonicalCwd(cwd: string): string | undefined {
29
+ const trimmed = cwd.trim()
30
+ if (!trimmed) return undefined
31
+ const absolute = normalize(resolve(trimmed))
32
+ try {
33
+ return realpathSync(absolute)
34
+ } catch {
35
+ return absolute
36
+ }
37
+ }
38
+
39
+ function modelIds(value: unknown): string[] {
40
+ if (!Array.isArray(value)) return []
41
+ return [...new Set(value.flatMap((id) => (typeof id === "string" && id.trim() ? [id.trim()] : [])))]
42
+ }
43
+
44
+ /** Models last left on in each directory. Anything that is not that map is ignored. */
45
+ export function readFastMemory(path: string = fastMemoryPath()): Map<string, string[]> {
46
+ let json: unknown
47
+ try {
48
+ json = JSON.parse(readFileSync(path, "utf8"))
49
+ } catch {
50
+ return new Map()
51
+ }
52
+ const directories = (json as { directories?: unknown } | null)?.directories
53
+ if (!directories || typeof directories !== "object" || Array.isArray(directories)) return new Map()
54
+ const memory = new Map<string, string[]>()
55
+ for (const [cwd, models] of Object.entries(directories)) {
56
+ const key = canonicalCwd(cwd)
57
+ const ids = modelIds(models)
58
+ if (key && ids.length) memory.set(key, ids)
59
+ }
60
+ return memory
61
+ }
62
+
63
+ /** Replace one directory's remembered models. An empty list forgets the directory.
64
+ * A failed write returns false and leaves the previous file alone. */
65
+ export function writeFastDirectory(cwd: string, models: Iterable<string>, path: string = fastMemoryPath()): boolean {
66
+ const key = canonicalCwd(cwd)
67
+ if (!key) return false
68
+ const memory = readFastMemory(path)
69
+ const ids = modelIds([...models])
70
+ if (ids.length) memory.set(key, ids)
71
+ else memory.delete(key)
72
+ const directories = Object.fromEntries([...memory].sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0)))
73
+ const next = path + ".tmp"
74
+ try {
75
+ writeFileSync(next, JSON.stringify({ directories }) + "\n")
76
+ renameSync(next, path)
77
+ return true
78
+ } catch {
79
+ return false
80
+ }
81
+ }
package/src/fast.ts CHANGED
@@ -3,6 +3,7 @@ import type { ExtensionAPI, ExtensionContext } from "@earendil-works/pi-coding-a
3
3
  import { streamCursor } from "./pi-stream.ts"
4
4
  import { createFastFooter, supportsInlineFastFooter } from "./fast-footer.ts"
5
5
  import { cursorFastAutocomplete } from "./fast-autocomplete.ts"
6
+ import { canonicalCwd, readFastMemory, writeFastDirectory } from "./fast-memory.ts"
6
7
 
7
8
  const ENTRY = "cursor-fast"
8
9
  const STATUS = "cursor-fast"
@@ -11,6 +12,7 @@ type FastModel = Model<any> & { cursorFastLevels?: string[] }
11
12
 
12
13
  export function registerCursorFast(pi: ExtensionAPI) {
13
14
  const enabled = new Set<string>()
15
+ let activeModel: string | undefined
14
16
  let sessionId: string | undefined
15
17
  let currentContext: ExtensionContext | undefined
16
18
  let badge = ""
@@ -76,17 +78,65 @@ export function registerCursorFast(pi: ExtensionAPI) {
76
78
  display(ctx, supported(model, pi.getThinkingLevel()) ? "Fast" : "Fast unavailable")
77
79
  }
78
80
 
81
+ // A branch that already chose is left alone, including a branch that turned
82
+ // fast off. Only a session with no choice of its own inherits the directory.
83
+ function directoryOf(ctx: ExtensionContext): string | undefined {
84
+ const cwd = ctx.sessionManager.getCwd?.()
85
+ return typeof cwd === "string" ? canonicalCwd(cwd) : undefined
86
+ }
87
+
88
+ function chosen(ctx: ExtensionContext): Array<{ model: string; enabled: boolean }> {
89
+ return ctx.sessionManager.getBranch().flatMap((entry) => {
90
+ if (entry.type !== "custom" || entry.customType !== ENTRY) return []
91
+ const data = entry.data as { model?: unknown; enabled?: unknown } | undefined
92
+ return typeof data?.model === "string" && typeof data.enabled === "boolean" ? [{ model: data.model, enabled: data.enabled }] : []
93
+ })
94
+ }
95
+
96
+ function remember(ctx: ExtensionContext): boolean {
97
+ const cwd = directoryOf(ctx)
98
+ return cwd ? writeFastDirectory(cwd, enabled) : true
99
+ }
100
+
101
+ function record(ctx: ExtensionContext, model: string, on: boolean): boolean {
102
+ if (!chosen(ctx).length) {
103
+ for (const id of enabled) {
104
+ if (id !== model) pi.appendEntry(ENTRY, { model: id, enabled: true })
105
+ }
106
+ }
107
+ pi.appendEntry(ENTRY, { model, enabled: on })
108
+ if (on) enabled.add(model)
109
+ else enabled.delete(model)
110
+ return remember(ctx)
111
+ }
112
+
113
+ function carry(ctx: ExtensionContext) {
114
+ const next = modelOf(ctx)
115
+ const previous = activeModel
116
+ if (next) activeModel = next.id
117
+ if (!next || !previous || previous === next.id || !enabled.has(previous)) return
118
+ if (!ctx.isIdle()) return
119
+ if (enabled.has(next.id) || !supported(next, pi.getThinkingLevel())) return
120
+ if (!record(ctx, next.id, true)) {
121
+ ctx.ui.notify("Cursor fast followed the model, but the directory preference could not be saved.", "warning")
122
+ }
123
+ }
124
+
79
125
  function restore(ctx: ExtensionContext) {
80
126
  enabled.clear()
81
127
  sessionId = ctx.sessionManager.getSessionId()
82
- for (const entry of ctx.sessionManager.getBranch()) {
83
- if (entry.type !== "custom" || entry.customType !== ENTRY) continue
84
- const data = entry.data as { model?: unknown; enabled?: unknown } | undefined
85
- if (typeof data?.model !== "string" || typeof data.enabled !== "boolean") continue
86
- if (data.enabled) enabled.add(data.model)
87
- else enabled.delete(data.model)
128
+ const entries = chosen(ctx)
129
+ if (entries.length) {
130
+ for (const entry of entries) {
131
+ if (entry.enabled) enabled.add(entry.model)
132
+ else enabled.delete(entry.model)
133
+ }
134
+ } else {
135
+ const cwd = directoryOf(ctx)
136
+ for (const model of (cwd ? readFastMemory().get(cwd) : undefined) ?? []) enabled.add(model)
88
137
  }
89
138
  updateStatus(ctx)
139
+ activeModel = modelOf(ctx)?.id
90
140
  }
91
141
 
92
142
  pi.on("session_start", (_event, ctx) => {
@@ -105,7 +155,7 @@ export function registerCursorFast(pi: ExtensionAPI) {
105
155
  autocompleteInstalled = false
106
156
  enabled.clear()
107
157
  })
108
- pi.on("model_select", (_event, ctx) => updateStatus(ctx))
158
+ pi.on("model_select", (_event, ctx) => { carry(ctx); updateStatus(ctx) })
109
159
  pi.on("thinking_level_select", (_event, ctx) => updateStatus(ctx))
110
160
  pi.on("before_agent_start", (_event, ctx) => updateStatus(ctx))
111
161
 
@@ -136,9 +186,11 @@ export function registerCursorFast(pi: ExtensionAPI) {
136
186
  }
137
187
  if (action !== "status") {
138
188
  const on = action === "on"
139
- pi.appendEntry(ENTRY, { model: model.id, enabled: on })
140
- if (on) enabled.add(model.id)
141
- else enabled.delete(model.id)
189
+ // The first choice on a branch has to carry the models it inherited.
190
+ // Recording only the model just changed would drop the others on reload.
191
+ if (!record(ctx, model.id, on)) {
192
+ ctx.ui.notify("Cursor fast changed for this session, but the directory preference could not be saved.", "warning")
193
+ }
142
194
  }
143
195
  updateStatus(ctx)
144
196
  const on = enabled.has(model.id)