pi-agy-acp 0.1.4 → 0.2.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/CHANGELOG.md +24 -0
- package/README.md +4 -5
- package/docs/development.md +33 -10
- package/docs/reference.md +37 -29
- package/index.ts +23 -8
- package/package.json +1 -1
- package/src/acp-process.ts +33 -3
- package/src/catalog.ts +249 -41
- package/src/config.ts +1 -1
- package/src/conversation.ts +326 -32
- package/src/format.ts +16 -7
- package/src/host.ts +120 -18
- package/src/pi-stream.ts +2 -2
- package/src/preflight.ts +385 -34
- package/src/quota.ts +163 -16
- package/src/runtime.ts +244 -105
package/src/catalog.ts
CHANGED
|
@@ -7,8 +7,20 @@
|
|
|
7
7
|
* @module pi-agy-acp/catalog
|
|
8
8
|
*/
|
|
9
9
|
|
|
10
|
-
import {
|
|
11
|
-
|
|
10
|
+
import {
|
|
11
|
+
existsSync,
|
|
12
|
+
mkdirSync,
|
|
13
|
+
readFileSync,
|
|
14
|
+
renameSync,
|
|
15
|
+
unlinkSync,
|
|
16
|
+
writeFileSync,
|
|
17
|
+
} from 'node:fs'
|
|
18
|
+
import { basename, dirname, join } from 'node:path'
|
|
19
|
+
import type {
|
|
20
|
+
AcpConfigOption,
|
|
21
|
+
AcpConfigOptionValue,
|
|
22
|
+
AcpTierInfo,
|
|
23
|
+
} from './acp-process.ts'
|
|
12
24
|
import { EFFORTS, type Effort } from './config.ts'
|
|
13
25
|
|
|
14
26
|
/** One advertised model family. */
|
|
@@ -23,17 +35,38 @@ export interface AvailableModel {
|
|
|
23
35
|
name?: string
|
|
24
36
|
}
|
|
25
37
|
|
|
26
|
-
/**
|
|
27
|
-
export
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
38
|
+
/** Flatten flat or grouped ACP select option entries. */
|
|
39
|
+
export function flattenConfigOptions(
|
|
40
|
+
options: AcpConfigOption['options'],
|
|
41
|
+
): AcpConfigOptionValue[] {
|
|
42
|
+
if (!Array.isArray(options)) return []
|
|
43
|
+
return options.flatMap((entry) => {
|
|
44
|
+
if (entry === null || typeof entry !== 'object') return []
|
|
45
|
+
return 'group' in entry
|
|
46
|
+
? Array.isArray(entry.options)
|
|
47
|
+
? entry.options
|
|
48
|
+
: []
|
|
49
|
+
: [entry]
|
|
50
|
+
})
|
|
51
|
+
}
|
|
34
52
|
|
|
35
|
-
/**
|
|
36
|
-
const
|
|
53
|
+
/** Effort suffixes ordered longest-first so `-<effort>-effort` beats `-<effort>`. */
|
|
54
|
+
const EFFORT_SUFFIXES: readonly { suffix: string; effort: Effort }[] =
|
|
55
|
+
EFFORTS.flatMap((effort) => [
|
|
56
|
+
{ suffix: `-${effort}-effort`, effort },
|
|
57
|
+
{ suffix: `-${effort}`, effort },
|
|
58
|
+
]).sort((a, b) => b.suffix.length - a.suffix.length)
|
|
59
|
+
|
|
60
|
+
function splitEffortSuffix(
|
|
61
|
+
id: string,
|
|
62
|
+
): { base: string; effort: Effort } | undefined {
|
|
63
|
+
for (const { suffix, effort } of EFFORT_SUFFIXES) {
|
|
64
|
+
if (id.length > suffix.length && id.endsWith(suffix)) {
|
|
65
|
+
return { base: id.slice(0, -suffix.length), effort }
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
return undefined
|
|
69
|
+
}
|
|
37
70
|
|
|
38
71
|
function normalizeTier(
|
|
39
72
|
tier: AcpTierInfo | undefined,
|
|
@@ -64,23 +97,115 @@ export function nearestEffort(
|
|
|
64
97
|
return best ?? wanted
|
|
65
98
|
}
|
|
66
99
|
|
|
67
|
-
|
|
100
|
+
function isValidContextWindow(value: unknown): value is number {
|
|
101
|
+
return typeof value === 'number' && Number.isInteger(value) && value > 0
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** Observed catalog with a disk cache. Starts empty until loaded or observed from ACP. */
|
|
68
105
|
export class ModelCatalog {
|
|
69
106
|
private models: CatalogModel[]
|
|
70
107
|
private tierInfo: { id?: string; name: string } | undefined
|
|
71
|
-
private efforts: Effort[] = [
|
|
72
|
-
/** Efforts per model family; an empty array marks a fixed-effort model. */
|
|
108
|
+
private efforts: Effort[] = []
|
|
109
|
+
/** Efforts per model family; an empty array marks a fixed-effort or non-selectable model. */
|
|
73
110
|
private byModel = new Map<string, Effort[]>()
|
|
111
|
+
/** Server-reported `max_context_tokens` keyed by model id. */
|
|
112
|
+
private readonly contextWindows = new Map<string, number>()
|
|
113
|
+
/** Server-reported `agentCapabilities.promptCapabilities.image` from `initialize`. */
|
|
114
|
+
private supportsImageInput: boolean | undefined
|
|
74
115
|
/** Latest `availableModels` snapshot, kept for later option updates. */
|
|
75
116
|
private available: readonly AvailableModel[] | undefined
|
|
117
|
+
/** Subprocess-free fingerprint of the `agy` binary that produced the cached catalog. */
|
|
118
|
+
private binaryFingerprint: string | undefined
|
|
119
|
+
/** Set when `binaryFingerprint` differs from the value last persisted on disk. */
|
|
120
|
+
private fingerprintDirty = false
|
|
76
121
|
/** Whether a live ACP session has reported the catalog. */
|
|
77
122
|
observed = false
|
|
78
123
|
private readonly cachePath: string
|
|
79
124
|
|
|
80
|
-
/**
|
|
81
|
-
|
|
125
|
+
/**
|
|
126
|
+
* @param cachePath - JSON cache file.
|
|
127
|
+
* @param binaryFingerprint - current `agy` binary fingerprint, if resolved.
|
|
128
|
+
*/
|
|
129
|
+
constructor(cachePath: string, binaryFingerprint?: string) {
|
|
82
130
|
this.cachePath = cachePath
|
|
83
|
-
this.models = this.load() ?? [
|
|
131
|
+
this.models = this.load(binaryFingerprint) ?? []
|
|
132
|
+
if (binaryFingerprint && this.binaryFingerprint === undefined) {
|
|
133
|
+
this.binaryFingerprint = binaryFingerprint
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/** Input modalities advertised to Pi, derived from `initialize` `promptCapabilities.image`. */
|
|
138
|
+
inputModalities(): ('text' | 'image')[] {
|
|
139
|
+
return this.supportsImageInput === false ? ['text'] : ['text', 'image']
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Record `agentCapabilities.promptCapabilities.image` from ACP `initialize`.
|
|
144
|
+
* @returns whether the advertised input modalities changed.
|
|
145
|
+
*/
|
|
146
|
+
observeImageSupport(supported: boolean | undefined): boolean {
|
|
147
|
+
if (typeof supported !== 'boolean') return false
|
|
148
|
+
const before = this.supportsImageInput !== false
|
|
149
|
+
const changed = this.supportsImageInput !== supported
|
|
150
|
+
if (!changed) return false
|
|
151
|
+
this.supportsImageInput = supported
|
|
152
|
+
this.save()
|
|
153
|
+
return before !== supported
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* Reconcile cached state against the current `agy` binary fingerprint.
|
|
158
|
+
* Invalidates cached `contextWindows` and `observed` when the binary changed
|
|
159
|
+
* so the next warmup or session re-probes fresh models and context limits.
|
|
160
|
+
* @returns whether cached context windows were invalidated.
|
|
161
|
+
*/
|
|
162
|
+
syncBinaryFingerprint(fingerprint: string | undefined): boolean {
|
|
163
|
+
if (!fingerprint) return false
|
|
164
|
+
if (this.binaryFingerprint === fingerprint) return false
|
|
165
|
+
const hadContextWindows = this.contextWindows.size > 0
|
|
166
|
+
const hadState =
|
|
167
|
+
this.binaryFingerprint !== undefined || hadContextWindows || this.observed
|
|
168
|
+
this.binaryFingerprint = fingerprint
|
|
169
|
+
if (hadState) {
|
|
170
|
+
this.contextWindows.clear()
|
|
171
|
+
this.observed = false
|
|
172
|
+
this.fingerprintDirty = true
|
|
173
|
+
this.save()
|
|
174
|
+
return hadContextWindows
|
|
175
|
+
}
|
|
176
|
+
return false
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* Cached server-issued context window (`max_context_tokens`) for one model.
|
|
181
|
+
* @param modelId - family id.
|
|
182
|
+
* @returns positive token limit, or `undefined` when never observed.
|
|
183
|
+
*/
|
|
184
|
+
contextWindowFor(modelId: string): number | undefined {
|
|
185
|
+
return this.contextWindows.get(modelId)
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* Persist one observed context window if it is valid and changed.
|
|
190
|
+
* @param modelId - family id.
|
|
191
|
+
* @param contextWindow - observed `max_context_tokens`.
|
|
192
|
+
* @returns whether the stored window changed.
|
|
193
|
+
*/
|
|
194
|
+
observeContextWindow(
|
|
195
|
+
modelId: string,
|
|
196
|
+
contextWindow: number | undefined,
|
|
197
|
+
): boolean {
|
|
198
|
+
if (
|
|
199
|
+
typeof modelId !== 'string' ||
|
|
200
|
+
modelId.length === 0 ||
|
|
201
|
+
!isValidContextWindow(contextWindow)
|
|
202
|
+
) {
|
|
203
|
+
return false
|
|
204
|
+
}
|
|
205
|
+
if (this.contextWindows.get(modelId) === contextWindow) return false
|
|
206
|
+
this.contextWindows.set(modelId, contextWindow)
|
|
207
|
+
this.save()
|
|
208
|
+
return true
|
|
84
209
|
}
|
|
85
210
|
|
|
86
211
|
/** Current families. */
|
|
@@ -100,7 +225,7 @@ export class ModelCatalog {
|
|
|
100
225
|
|
|
101
226
|
/**
|
|
102
227
|
* Efforts one family actually offers. Empty means the family is a single
|
|
103
|
-
* fixed variant (for example `gpt-oss-120b-medium`).
|
|
228
|
+
* fixed variant (for example `gpt-oss-120b-medium`) or offers no selector.
|
|
104
229
|
* @param id - family id.
|
|
105
230
|
* @returns offered efforts.
|
|
106
231
|
*/
|
|
@@ -110,14 +235,23 @@ export class ModelCatalog {
|
|
|
110
235
|
return this.fixedEffort(id) === undefined ? this.efforts : []
|
|
111
236
|
}
|
|
112
237
|
|
|
238
|
+
/**
|
|
239
|
+
* Whether a model accepts `thought_level` writes on a live ACP session.
|
|
240
|
+
* Fixed-effort models and models observed with zero effort variants return false.
|
|
241
|
+
*/
|
|
242
|
+
supportsEffortSelection(id: string): boolean {
|
|
243
|
+
const observed = this.byModel.get(id)
|
|
244
|
+
if (observed !== undefined) return observed.length > 0
|
|
245
|
+
return this.fixedEffort(id) === undefined
|
|
246
|
+
}
|
|
247
|
+
|
|
113
248
|
/**
|
|
114
249
|
* Effort baked into a fixed-variant id.
|
|
115
250
|
* @param id - family id.
|
|
116
251
|
* @returns the effort, or undefined for a selectable family.
|
|
117
252
|
*/
|
|
118
253
|
fixedEffort(id: string): Effort | undefined {
|
|
119
|
-
|
|
120
|
-
return match === null ? undefined : (match[1] as Effort)
|
|
254
|
+
return splitEffortSuffix(id)?.effort
|
|
121
255
|
}
|
|
122
256
|
|
|
123
257
|
/**
|
|
@@ -152,37 +286,61 @@ export class ModelCatalog {
|
|
|
152
286
|
)
|
|
153
287
|
const beforeEfforts = JSON.stringify(this.efforts)
|
|
154
288
|
if (levelOption?.options !== undefined) {
|
|
155
|
-
const offered = levelOption.options
|
|
289
|
+
const offered = flattenConfigOptions(levelOption.options)
|
|
156
290
|
.map((o) => o.value)
|
|
157
291
|
.filter((v): v is Effort => (EFFORTS as readonly string[]).includes(v))
|
|
158
292
|
if (offered.length > 0)
|
|
159
293
|
this.efforts = EFFORTS.filter((e) => offered.includes(e))
|
|
160
294
|
}
|
|
161
|
-
const
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
295
|
+
const modelEntries = flattenConfigOptions(modelOption?.options)
|
|
296
|
+
const next =
|
|
297
|
+
modelEntries.length > 0
|
|
298
|
+
? modelEntries.map((o) => ({
|
|
299
|
+
id: o.value,
|
|
300
|
+
name: o.name ?? o.value,
|
|
301
|
+
}))
|
|
302
|
+
: this.models
|
|
167
303
|
this.observed = true
|
|
168
304
|
const changed = JSON.stringify(next) !== JSON.stringify(this.models)
|
|
169
305
|
if (changed) this.models = next
|
|
306
|
+
const derivedChanged = this.deriveEfforts()
|
|
307
|
+
if (this.efforts.length === 0 && this.byModel.size > 0) {
|
|
308
|
+
const union = new Set<Effort>()
|
|
309
|
+
for (const values of this.byModel.values()) {
|
|
310
|
+
for (const effort of values) union.add(effort)
|
|
311
|
+
}
|
|
312
|
+
if (union.size > 0) {
|
|
313
|
+
this.efforts = EFFORTS.filter((e) => union.has(e))
|
|
314
|
+
}
|
|
315
|
+
}
|
|
170
316
|
const effortsChanged =
|
|
171
|
-
|
|
172
|
-
if (changed || effortsChanged || tierChanged
|
|
317
|
+
derivedChanged || beforeEfforts !== JSON.stringify(this.efforts)
|
|
318
|
+
if (changed || effortsChanged || tierChanged || this.fingerprintDirty)
|
|
319
|
+
this.save()
|
|
173
320
|
return changed || effortsChanged
|
|
174
321
|
}
|
|
175
322
|
|
|
176
323
|
/** Recompute per-family efforts from the latest inventory. */
|
|
177
324
|
private deriveEfforts(): boolean {
|
|
178
325
|
if (this.available === undefined) return false
|
|
326
|
+
const byBase = new Map<string, Set<Effort>>()
|
|
327
|
+
for (const entry of this.available) {
|
|
328
|
+
const split = splitEffortSuffix(entry.modelId)
|
|
329
|
+
if (split === undefined) continue
|
|
330
|
+
let set = byBase.get(split.base)
|
|
331
|
+
if (set === undefined) {
|
|
332
|
+
set = new Set<Effort>()
|
|
333
|
+
byBase.set(split.base, set)
|
|
334
|
+
}
|
|
335
|
+
set.add(split.effort)
|
|
336
|
+
}
|
|
179
337
|
let changed = false
|
|
180
338
|
for (const model of this.models) {
|
|
181
|
-
const
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
339
|
+
const matched = byBase.get(model.id)
|
|
340
|
+
const offered =
|
|
341
|
+
matched !== undefined
|
|
342
|
+
? EFFORTS.filter((effort) => matched.has(effort))
|
|
343
|
+
: []
|
|
186
344
|
if (offered.length > 0) {
|
|
187
345
|
if (
|
|
188
346
|
JSON.stringify(this.byModel.get(model.id)) !== JSON.stringify(offered)
|
|
@@ -198,44 +356,84 @@ export class ModelCatalog {
|
|
|
198
356
|
entry.modelId.startsWith(`${model.id}-`),
|
|
199
357
|
)
|
|
200
358
|
if (!present) continue
|
|
201
|
-
|
|
359
|
+
const prevEfforts = this.effortsFor(model.id)
|
|
360
|
+
if (this.byModel.get(model.id)?.length !== 0) {
|
|
202
361
|
this.byModel.set(model.id, [])
|
|
203
|
-
changed = true
|
|
362
|
+
if (prevEfforts.length !== 0) changed = true
|
|
204
363
|
}
|
|
205
364
|
}
|
|
206
365
|
return changed
|
|
207
366
|
}
|
|
208
367
|
|
|
209
368
|
private save(): void {
|
|
369
|
+
const dir = dirname(this.cachePath)
|
|
370
|
+
const tempPath = join(
|
|
371
|
+
dir,
|
|
372
|
+
`.${basename(this.cachePath)}.${process.pid}.${Date.now()}.${Math.random().toString(16).slice(2, 8)}.tmp`,
|
|
373
|
+
)
|
|
210
374
|
try {
|
|
375
|
+
mkdirSync(dir, { recursive: true })
|
|
211
376
|
writeFileSync(
|
|
212
|
-
|
|
377
|
+
tempPath,
|
|
213
378
|
`${JSON.stringify(
|
|
214
379
|
{
|
|
215
380
|
models: this.models,
|
|
216
381
|
efforts: this.efforts,
|
|
217
382
|
byModel: Object.fromEntries(this.byModel),
|
|
383
|
+
contextWindows: Object.fromEntries(this.contextWindows),
|
|
218
384
|
tier: this.tierInfo,
|
|
385
|
+
...(this.supportsImageInput !== undefined
|
|
386
|
+
? { supportsImage: this.supportsImageInput }
|
|
387
|
+
: {}),
|
|
388
|
+
...(this.binaryFingerprint !== undefined
|
|
389
|
+
? { binaryFingerprint: this.binaryFingerprint }
|
|
390
|
+
: {}),
|
|
219
391
|
},
|
|
220
392
|
null,
|
|
221
393
|
2,
|
|
222
394
|
)}\n`,
|
|
223
395
|
)
|
|
396
|
+
renameSync(tempPath, this.cachePath)
|
|
397
|
+
this.fingerprintDirty = false
|
|
224
398
|
} catch (_cacheWrite) {
|
|
399
|
+
try {
|
|
400
|
+
unlinkSync(tempPath)
|
|
401
|
+
} catch {
|
|
402
|
+
// Best-effort cleanup.
|
|
403
|
+
}
|
|
225
404
|
// The cache only speeds up the next cold start.
|
|
226
405
|
}
|
|
227
406
|
}
|
|
228
407
|
|
|
229
|
-
private load(): CatalogModel[] | undefined {
|
|
408
|
+
private load(expectedFingerprint?: string): CatalogModel[] | undefined {
|
|
230
409
|
if (!existsSync(this.cachePath)) return undefined
|
|
231
410
|
try {
|
|
232
411
|
const parsed = JSON.parse(readFileSync(this.cachePath, 'utf8')) as {
|
|
233
412
|
models?: CatalogModel[]
|
|
234
413
|
efforts?: string[]
|
|
235
414
|
byModel?: Record<string, string[]>
|
|
415
|
+
contextWindows?: Record<string, unknown>
|
|
236
416
|
tier?: AcpTierInfo
|
|
237
|
-
|
|
417
|
+
supportsImage?: unknown
|
|
418
|
+
binaryFingerprint?: unknown
|
|
419
|
+
} | null
|
|
420
|
+
if (parsed === null || typeof parsed !== 'object') return undefined
|
|
421
|
+
const storedFingerprint =
|
|
422
|
+
typeof parsed.binaryFingerprint === 'string' &&
|
|
423
|
+
parsed.binaryFingerprint.length > 0
|
|
424
|
+
? parsed.binaryFingerprint
|
|
425
|
+
: undefined
|
|
426
|
+
const fingerprintMatches =
|
|
427
|
+
expectedFingerprint === undefined ||
|
|
428
|
+
storedFingerprint === expectedFingerprint
|
|
429
|
+
this.binaryFingerprint = fingerprintMatches
|
|
430
|
+
? (storedFingerprint ?? expectedFingerprint)
|
|
431
|
+
: expectedFingerprint
|
|
432
|
+
this.fingerprintDirty = !fingerprintMatches
|
|
238
433
|
this.tierInfo = normalizeTier(parsed.tier)
|
|
434
|
+
if (typeof parsed.supportsImage === 'boolean') {
|
|
435
|
+
this.supportsImageInput = parsed.supportsImage
|
|
436
|
+
}
|
|
239
437
|
if (Array.isArray(parsed.efforts)) {
|
|
240
438
|
const efforts = parsed.efforts.filter((v): v is Effort =>
|
|
241
439
|
(EFFORTS as readonly string[]).includes(v),
|
|
@@ -251,6 +449,16 @@ export class ModelCatalog {
|
|
|
251
449
|
this.byModel.set(id, efforts)
|
|
252
450
|
}
|
|
253
451
|
}
|
|
452
|
+
if (
|
|
453
|
+
fingerprintMatches &&
|
|
454
|
+
parsed.contextWindows !== null &&
|
|
455
|
+
typeof parsed.contextWindows === 'object'
|
|
456
|
+
) {
|
|
457
|
+
for (const [id, value] of Object.entries(parsed.contextWindows)) {
|
|
458
|
+
if (id.length > 0 && isValidContextWindow(value))
|
|
459
|
+
this.contextWindows.set(id, value)
|
|
460
|
+
}
|
|
461
|
+
}
|
|
254
462
|
const models = Array.isArray(parsed.models)
|
|
255
463
|
? parsed.models.filter(
|
|
256
464
|
(m) => typeof m?.id === 'string' && typeof m?.name === 'string',
|
|
@@ -258,7 +466,7 @@ export class ModelCatalog {
|
|
|
258
466
|
: []
|
|
259
467
|
return models.length > 0 ? models : undefined
|
|
260
468
|
} catch (_cacheRead) {
|
|
261
|
-
// A corrupt cache
|
|
469
|
+
// A corrupt cache starts empty until live ACP discovery populates it.
|
|
262
470
|
return undefined
|
|
263
471
|
}
|
|
264
472
|
}
|
package/src/config.ts
CHANGED
|
@@ -8,7 +8,7 @@ import {
|
|
|
8
8
|
import { join } from 'node:path'
|
|
9
9
|
|
|
10
10
|
/** Reasoning effort ids accepted by the Antigravity ACP `thought_level` option. */
|
|
11
|
-
export const EFFORTS = ['low', 'medium', 'high'] as const
|
|
11
|
+
export const EFFORTS = ['low', 'medium', 'high', 'xhigh', 'max'] as const
|
|
12
12
|
/** One Antigravity reasoning effort. */
|
|
13
13
|
export type Effort = (typeof EFFORTS)[number]
|
|
14
14
|
|