pi-agy-acp 0.2.0 → 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 +17 -0
- package/README.md +4 -5
- package/docs/development.md +33 -10
- package/docs/reference.md +32 -29
- package/index.ts +9 -7
- package/package.json +1 -1
- package/src/acp-process.ts +33 -3
- package/src/catalog.ts +198 -40
- package/src/config.ts +1 -1
- package/src/conversation.ts +326 -32
- package/src/format.ts +12 -6
- package/src/host.ts +120 -18
- package/src/pi-stream.ts +2 -2
- package/src/preflight.ts +162 -40
- package/src/quota.ts +163 -16
- package/src/runtime.ts +178 -101
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,
|
|
@@ -68,25 +101,79 @@ function isValidContextWindow(value: unknown): value is number {
|
|
|
68
101
|
return typeof value === 'number' && Number.isInteger(value) && value > 0
|
|
69
102
|
}
|
|
70
103
|
|
|
71
|
-
/** Observed catalog with a disk cache. */
|
|
104
|
+
/** Observed catalog with a disk cache. Starts empty until loaded or observed from ACP. */
|
|
72
105
|
export class ModelCatalog {
|
|
73
106
|
private models: CatalogModel[]
|
|
74
107
|
private tierInfo: { id?: string; name: string } | undefined
|
|
75
|
-
private efforts: Effort[] = [
|
|
76
|
-
/** 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. */
|
|
77
110
|
private byModel = new Map<string, Effort[]>()
|
|
78
111
|
/** Server-reported `max_context_tokens` keyed by model id. */
|
|
79
112
|
private readonly contextWindows = new Map<string, number>()
|
|
113
|
+
/** Server-reported `agentCapabilities.promptCapabilities.image` from `initialize`. */
|
|
114
|
+
private supportsImageInput: boolean | undefined
|
|
80
115
|
/** Latest `availableModels` snapshot, kept for later option updates. */
|
|
81
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
|
|
82
121
|
/** Whether a live ACP session has reported the catalog. */
|
|
83
122
|
observed = false
|
|
84
123
|
private readonly cachePath: string
|
|
85
124
|
|
|
86
|
-
/**
|
|
87
|
-
|
|
125
|
+
/**
|
|
126
|
+
* @param cachePath - JSON cache file.
|
|
127
|
+
* @param binaryFingerprint - current `agy` binary fingerprint, if resolved.
|
|
128
|
+
*/
|
|
129
|
+
constructor(cachePath: string, binaryFingerprint?: string) {
|
|
88
130
|
this.cachePath = cachePath
|
|
89
|
-
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
|
|
90
177
|
}
|
|
91
178
|
|
|
92
179
|
/**
|
|
@@ -138,7 +225,7 @@ export class ModelCatalog {
|
|
|
138
225
|
|
|
139
226
|
/**
|
|
140
227
|
* Efforts one family actually offers. Empty means the family is a single
|
|
141
|
-
* fixed variant (for example `gpt-oss-120b-medium`).
|
|
228
|
+
* fixed variant (for example `gpt-oss-120b-medium`) or offers no selector.
|
|
142
229
|
* @param id - family id.
|
|
143
230
|
* @returns offered efforts.
|
|
144
231
|
*/
|
|
@@ -148,14 +235,23 @@ export class ModelCatalog {
|
|
|
148
235
|
return this.fixedEffort(id) === undefined ? this.efforts : []
|
|
149
236
|
}
|
|
150
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
|
+
|
|
151
248
|
/**
|
|
152
249
|
* Effort baked into a fixed-variant id.
|
|
153
250
|
* @param id - family id.
|
|
154
251
|
* @returns the effort, or undefined for a selectable family.
|
|
155
252
|
*/
|
|
156
253
|
fixedEffort(id: string): Effort | undefined {
|
|
157
|
-
|
|
158
|
-
return match === null ? undefined : (match[1] as Effort)
|
|
254
|
+
return splitEffortSuffix(id)?.effort
|
|
159
255
|
}
|
|
160
256
|
|
|
161
257
|
/**
|
|
@@ -190,37 +286,61 @@ export class ModelCatalog {
|
|
|
190
286
|
)
|
|
191
287
|
const beforeEfforts = JSON.stringify(this.efforts)
|
|
192
288
|
if (levelOption?.options !== undefined) {
|
|
193
|
-
const offered = levelOption.options
|
|
289
|
+
const offered = flattenConfigOptions(levelOption.options)
|
|
194
290
|
.map((o) => o.value)
|
|
195
291
|
.filter((v): v is Effort => (EFFORTS as readonly string[]).includes(v))
|
|
196
292
|
if (offered.length > 0)
|
|
197
293
|
this.efforts = EFFORTS.filter((e) => offered.includes(e))
|
|
198
294
|
}
|
|
199
|
-
const
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
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
|
|
205
303
|
this.observed = true
|
|
206
304
|
const changed = JSON.stringify(next) !== JSON.stringify(this.models)
|
|
207
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
|
+
}
|
|
208
316
|
const effortsChanged =
|
|
209
|
-
|
|
210
|
-
if (changed || effortsChanged || tierChanged
|
|
317
|
+
derivedChanged || beforeEfforts !== JSON.stringify(this.efforts)
|
|
318
|
+
if (changed || effortsChanged || tierChanged || this.fingerprintDirty)
|
|
319
|
+
this.save()
|
|
211
320
|
return changed || effortsChanged
|
|
212
321
|
}
|
|
213
322
|
|
|
214
323
|
/** Recompute per-family efforts from the latest inventory. */
|
|
215
324
|
private deriveEfforts(): boolean {
|
|
216
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
|
+
}
|
|
217
337
|
let changed = false
|
|
218
338
|
for (const model of this.models) {
|
|
219
|
-
const
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
339
|
+
const matched = byBase.get(model.id)
|
|
340
|
+
const offered =
|
|
341
|
+
matched !== undefined
|
|
342
|
+
? EFFORTS.filter((effort) => matched.has(effort))
|
|
343
|
+
: []
|
|
224
344
|
if (offered.length > 0) {
|
|
225
345
|
if (
|
|
226
346
|
JSON.stringify(this.byModel.get(model.id)) !== JSON.stringify(offered)
|
|
@@ -236,18 +356,25 @@ export class ModelCatalog {
|
|
|
236
356
|
entry.modelId.startsWith(`${model.id}-`),
|
|
237
357
|
)
|
|
238
358
|
if (!present) continue
|
|
239
|
-
|
|
359
|
+
const prevEfforts = this.effortsFor(model.id)
|
|
360
|
+
if (this.byModel.get(model.id)?.length !== 0) {
|
|
240
361
|
this.byModel.set(model.id, [])
|
|
241
|
-
changed = true
|
|
362
|
+
if (prevEfforts.length !== 0) changed = true
|
|
242
363
|
}
|
|
243
364
|
}
|
|
244
365
|
return changed
|
|
245
366
|
}
|
|
246
367
|
|
|
247
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
|
+
)
|
|
248
374
|
try {
|
|
375
|
+
mkdirSync(dir, { recursive: true })
|
|
249
376
|
writeFileSync(
|
|
250
|
-
|
|
377
|
+
tempPath,
|
|
251
378
|
`${JSON.stringify(
|
|
252
379
|
{
|
|
253
380
|
models: this.models,
|
|
@@ -255,17 +382,30 @@ export class ModelCatalog {
|
|
|
255
382
|
byModel: Object.fromEntries(this.byModel),
|
|
256
383
|
contextWindows: Object.fromEntries(this.contextWindows),
|
|
257
384
|
tier: this.tierInfo,
|
|
385
|
+
...(this.supportsImageInput !== undefined
|
|
386
|
+
? { supportsImage: this.supportsImageInput }
|
|
387
|
+
: {}),
|
|
388
|
+
...(this.binaryFingerprint !== undefined
|
|
389
|
+
? { binaryFingerprint: this.binaryFingerprint }
|
|
390
|
+
: {}),
|
|
258
391
|
},
|
|
259
392
|
null,
|
|
260
393
|
2,
|
|
261
394
|
)}\n`,
|
|
262
395
|
)
|
|
396
|
+
renameSync(tempPath, this.cachePath)
|
|
397
|
+
this.fingerprintDirty = false
|
|
263
398
|
} catch (_cacheWrite) {
|
|
399
|
+
try {
|
|
400
|
+
unlinkSync(tempPath)
|
|
401
|
+
} catch {
|
|
402
|
+
// Best-effort cleanup.
|
|
403
|
+
}
|
|
264
404
|
// The cache only speeds up the next cold start.
|
|
265
405
|
}
|
|
266
406
|
}
|
|
267
407
|
|
|
268
|
-
private load(): CatalogModel[] | undefined {
|
|
408
|
+
private load(expectedFingerprint?: string): CatalogModel[] | undefined {
|
|
269
409
|
if (!existsSync(this.cachePath)) return undefined
|
|
270
410
|
try {
|
|
271
411
|
const parsed = JSON.parse(readFileSync(this.cachePath, 'utf8')) as {
|
|
@@ -274,9 +414,26 @@ export class ModelCatalog {
|
|
|
274
414
|
byModel?: Record<string, string[]>
|
|
275
415
|
contextWindows?: Record<string, unknown>
|
|
276
416
|
tier?: AcpTierInfo
|
|
417
|
+
supportsImage?: unknown
|
|
418
|
+
binaryFingerprint?: unknown
|
|
277
419
|
} | null
|
|
278
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
|
|
279
433
|
this.tierInfo = normalizeTier(parsed.tier)
|
|
434
|
+
if (typeof parsed.supportsImage === 'boolean') {
|
|
435
|
+
this.supportsImageInput = parsed.supportsImage
|
|
436
|
+
}
|
|
280
437
|
if (Array.isArray(parsed.efforts)) {
|
|
281
438
|
const efforts = parsed.efforts.filter((v): v is Effort =>
|
|
282
439
|
(EFFORTS as readonly string[]).includes(v),
|
|
@@ -293,6 +450,7 @@ export class ModelCatalog {
|
|
|
293
450
|
}
|
|
294
451
|
}
|
|
295
452
|
if (
|
|
453
|
+
fingerprintMatches &&
|
|
296
454
|
parsed.contextWindows !== null &&
|
|
297
455
|
typeof parsed.contextWindows === 'object'
|
|
298
456
|
) {
|
|
@@ -308,7 +466,7 @@ export class ModelCatalog {
|
|
|
308
466
|
: []
|
|
309
467
|
return models.length > 0 ? models : undefined
|
|
310
468
|
} catch (_cacheRead) {
|
|
311
|
-
// A corrupt cache
|
|
469
|
+
// A corrupt cache starts empty until live ACP discovery populates it.
|
|
312
470
|
return undefined
|
|
313
471
|
}
|
|
314
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
|
|