pi-agy-acp 0.2.6 → 0.2.8
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 +14 -0
- package/docs/architecture.md +41 -22
- package/docs/development.md +3 -3
- package/index.ts +65 -45
- package/package.json +4 -4
- package/src/acp-process.ts +13 -27
- package/src/catalog.ts +59 -33
- package/src/config.ts +3 -5
- package/src/conversation.ts +172 -175
- package/src/format.ts +9 -355
- package/src/history.ts +142 -33
- package/src/host.ts +2 -2
- package/src/mcp-bridge.ts +53 -12
- package/src/pi-stream.ts +5 -8
- package/src/preflight.ts +1 -3
- package/src/protocol.ts +18 -3
- package/src/quota.ts +377 -41
- package/src/runtime.ts +321 -235
- package/src/session-db.ts +24 -5
- package/src/util.ts +5 -0
package/src/catalog.ts
CHANGED
|
@@ -15,7 +15,7 @@ import type {
|
|
|
15
15
|
AcpTierInfo,
|
|
16
16
|
} from './acp-process.ts'
|
|
17
17
|
import { asEffort, EFFORTS, type Effort } from './config.ts'
|
|
18
|
-
import { atomicWriteFile } from './util.ts'
|
|
18
|
+
import { atomicWriteFile, isRecord } from './util.ts'
|
|
19
19
|
|
|
20
20
|
/** One advertised model family. */
|
|
21
21
|
export interface CatalogModel {
|
|
@@ -35,7 +35,7 @@ export function flattenConfigOptions(
|
|
|
35
35
|
): AcpConfigOptionValue[] {
|
|
36
36
|
if (!Array.isArray(options)) return []
|
|
37
37
|
return options.flatMap((entry) => {
|
|
38
|
-
if (entry
|
|
38
|
+
if (!isRecord(entry)) return []
|
|
39
39
|
return 'group' in entry
|
|
40
40
|
? Array.isArray(entry.options)
|
|
41
41
|
? entry.options
|
|
@@ -113,6 +113,34 @@ function isValidContextWindow(value: unknown): value is number {
|
|
|
113
113
|
return typeof value === 'number' && Number.isInteger(value) && value > 0
|
|
114
114
|
}
|
|
115
115
|
|
|
116
|
+
function equalTier(
|
|
117
|
+
a: { id?: string; name: string } | undefined,
|
|
118
|
+
b: { id?: string; name: string } | undefined,
|
|
119
|
+
): boolean {
|
|
120
|
+
return a?.id === b?.id && a?.name === b?.name
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
function equalEfforts(
|
|
124
|
+
a: readonly Effort[] | undefined,
|
|
125
|
+
b: readonly Effort[] | undefined,
|
|
126
|
+
): boolean {
|
|
127
|
+
if (a === b) return true
|
|
128
|
+
if (a === undefined || b === undefined || a.length !== b.length) return false
|
|
129
|
+
return a.every((value, index) => value === b[index])
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
function equalModels(
|
|
133
|
+
a: readonly CatalogModel[],
|
|
134
|
+
b: readonly CatalogModel[],
|
|
135
|
+
): boolean {
|
|
136
|
+
if (a === b) return true
|
|
137
|
+
if (a.length !== b.length) return false
|
|
138
|
+
return a.every(
|
|
139
|
+
(model, index) =>
|
|
140
|
+
model.id === b[index]?.id && model.name === b[index]?.name,
|
|
141
|
+
)
|
|
142
|
+
}
|
|
143
|
+
|
|
116
144
|
/** Observed catalog with a disk cache. Starts empty until loaded or observed from ACP. */
|
|
117
145
|
export class ModelCatalog {
|
|
118
146
|
private models: CatalogModel[]
|
|
@@ -131,7 +159,7 @@ export class ModelCatalog {
|
|
|
131
159
|
/** Fingerprint of the `agy` binary that produced the cached catalog. */
|
|
132
160
|
private observedFingerprint: string | undefined
|
|
133
161
|
/** Whether a live ACP session has reported the catalog in this process. */
|
|
134
|
-
observed = false
|
|
162
|
+
private observed = false
|
|
135
163
|
private readonly cachePath: string
|
|
136
164
|
|
|
137
165
|
/**
|
|
@@ -144,6 +172,11 @@ export class ModelCatalog {
|
|
|
144
172
|
this.models = this.load(binaryFingerprint) ?? []
|
|
145
173
|
}
|
|
146
174
|
|
|
175
|
+
/** Whether a live ACP session has reported the catalog in this process. */
|
|
176
|
+
isObserved(): boolean {
|
|
177
|
+
return this.observed
|
|
178
|
+
}
|
|
179
|
+
|
|
147
180
|
/** Whether disk-cached models match the current `agy` binary fingerprint. */
|
|
148
181
|
hasFreshCache(): boolean {
|
|
149
182
|
return (
|
|
@@ -288,8 +321,7 @@ export class ModelCatalog {
|
|
|
288
321
|
): boolean {
|
|
289
322
|
const nextTier = normalizeTier(tier)
|
|
290
323
|
const tierChanged =
|
|
291
|
-
nextTier !== undefined &&
|
|
292
|
-
JSON.stringify(nextTier) !== JSON.stringify(this.tierInfo)
|
|
324
|
+
nextTier !== undefined && !equalTier(nextTier, this.tierInfo)
|
|
293
325
|
if (tierChanged) this.tierInfo = nextTier
|
|
294
326
|
if (available !== undefined && available.length > 0)
|
|
295
327
|
this.available = available
|
|
@@ -299,7 +331,7 @@ export class ModelCatalog {
|
|
|
299
331
|
}
|
|
300
332
|
const modelOption = findConfigOption(options, 'model')
|
|
301
333
|
const levelOption = findConfigOption(options, 'thought_level')
|
|
302
|
-
const beforeEfforts =
|
|
334
|
+
const beforeEfforts = this.efforts
|
|
303
335
|
const levelEfforts = offeredEffortsFrom(levelOption) ?? []
|
|
304
336
|
const modelEntries = flattenConfigOptions(modelOption?.options)
|
|
305
337
|
const next =
|
|
@@ -310,7 +342,7 @@ export class ModelCatalog {
|
|
|
310
342
|
}))
|
|
311
343
|
: this.models
|
|
312
344
|
this.observed = true
|
|
313
|
-
const changed =
|
|
345
|
+
const changed = !equalModels(next, this.models)
|
|
314
346
|
if (changed) this.models = next
|
|
315
347
|
const fingerprintChanged =
|
|
316
348
|
modelEntries.length > 0 &&
|
|
@@ -341,7 +373,7 @@ export class ModelCatalog {
|
|
|
341
373
|
const effortsChanged =
|
|
342
374
|
prunedChanged ||
|
|
343
375
|
derivedChanged ||
|
|
344
|
-
beforeEfforts
|
|
376
|
+
!equalEfforts(beforeEfforts, this.efforts)
|
|
345
377
|
if (changed || effortsChanged || tierChanged || fingerprintChanged)
|
|
346
378
|
this.save()
|
|
347
379
|
return changed || effortsChanged
|
|
@@ -369,9 +401,7 @@ export class ModelCatalog {
|
|
|
369
401
|
? EFFORTS.filter((effort) => matched.has(effort))
|
|
370
402
|
: []
|
|
371
403
|
if (offered.length > 0) {
|
|
372
|
-
if (
|
|
373
|
-
JSON.stringify(this.byModel.get(model.id)) !== JSON.stringify(offered)
|
|
374
|
-
) {
|
|
404
|
+
if (!equalEfforts(this.byModel.get(model.id), offered)) {
|
|
375
405
|
this.byModel.set(model.id, offered)
|
|
376
406
|
changed = true
|
|
377
407
|
}
|
|
@@ -419,16 +449,8 @@ export class ModelCatalog {
|
|
|
419
449
|
private load(expectedFingerprint?: string): CatalogModel[] | undefined {
|
|
420
450
|
if (!existsSync(this.cachePath)) return undefined
|
|
421
451
|
try {
|
|
422
|
-
const parsed = JSON.parse(readFileSync(this.cachePath, 'utf8'))
|
|
423
|
-
|
|
424
|
-
efforts?: string[]
|
|
425
|
-
byModel?: Record<string, string[]>
|
|
426
|
-
contextWindows?: Record<string, unknown>
|
|
427
|
-
tier?: AcpTierInfo
|
|
428
|
-
supportsImage?: unknown
|
|
429
|
-
binaryFingerprint?: unknown
|
|
430
|
-
} | null
|
|
431
|
-
if (parsed === null || typeof parsed !== 'object') return undefined
|
|
452
|
+
const parsed: unknown = JSON.parse(readFileSync(this.cachePath, 'utf8'))
|
|
453
|
+
if (!isRecord(parsed)) return undefined
|
|
432
454
|
const storedFingerprint =
|
|
433
455
|
typeof parsed.binaryFingerprint === 'string' &&
|
|
434
456
|
parsed.binaryFingerprint.length > 0
|
|
@@ -438,30 +460,31 @@ export class ModelCatalog {
|
|
|
438
460
|
expectedFingerprint === undefined ||
|
|
439
461
|
storedFingerprint === expectedFingerprint
|
|
440
462
|
this.observedFingerprint = storedFingerprint
|
|
441
|
-
this.tierInfo = normalizeTier(
|
|
463
|
+
this.tierInfo = normalizeTier(
|
|
464
|
+
isRecord(parsed.tier) ? (parsed.tier as AcpTierInfo) : undefined,
|
|
465
|
+
)
|
|
442
466
|
if (typeof parsed.supportsImage === 'boolean') {
|
|
443
467
|
this.supportsImageInput = parsed.supportsImage
|
|
444
468
|
}
|
|
445
469
|
if (Array.isArray(parsed.efforts)) {
|
|
446
|
-
const efforts = parsed.efforts.filter(
|
|
447
|
-
(
|
|
470
|
+
const efforts = parsed.efforts.filter(
|
|
471
|
+
(v): v is Effort =>
|
|
472
|
+
typeof v === 'string' && (EFFORTS as readonly string[]).includes(v),
|
|
448
473
|
)
|
|
449
474
|
if (efforts.length > 0) this.efforts = efforts
|
|
450
475
|
}
|
|
451
|
-
if (parsed.byModel
|
|
476
|
+
if (isRecord(parsed.byModel)) {
|
|
452
477
|
for (const [id, values] of Object.entries(parsed.byModel)) {
|
|
453
478
|
if (!Array.isArray(values)) continue
|
|
454
|
-
const efforts = values.filter(
|
|
455
|
-
(
|
|
479
|
+
const efforts = values.filter(
|
|
480
|
+
(v): v is Effort =>
|
|
481
|
+
typeof v === 'string' &&
|
|
482
|
+
(EFFORTS as readonly string[]).includes(v),
|
|
456
483
|
)
|
|
457
484
|
this.byModel.set(id, efforts)
|
|
458
485
|
}
|
|
459
486
|
}
|
|
460
|
-
if (
|
|
461
|
-
fingerprintMatches &&
|
|
462
|
-
parsed.contextWindows !== null &&
|
|
463
|
-
typeof parsed.contextWindows === 'object'
|
|
464
|
-
) {
|
|
487
|
+
if (fingerprintMatches && isRecord(parsed.contextWindows)) {
|
|
465
488
|
for (const [id, value] of Object.entries(parsed.contextWindows)) {
|
|
466
489
|
if (id.length > 0 && isValidContextWindow(value))
|
|
467
490
|
this.contextWindows.set(id, value)
|
|
@@ -469,7 +492,10 @@ export class ModelCatalog {
|
|
|
469
492
|
}
|
|
470
493
|
const models = Array.isArray(parsed.models)
|
|
471
494
|
? parsed.models.filter(
|
|
472
|
-
(m)
|
|
495
|
+
(m): m is CatalogModel =>
|
|
496
|
+
isRecord(m) &&
|
|
497
|
+
typeof m.id === 'string' &&
|
|
498
|
+
typeof m.name === 'string',
|
|
473
499
|
)
|
|
474
500
|
: []
|
|
475
501
|
return models.length > 0 ? models : undefined
|
package/src/config.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { mkdirSync, readFileSync } from 'node:fs'
|
|
2
2
|
import { join } from 'node:path'
|
|
3
|
-
import { atomicWriteFile } from './util.ts'
|
|
3
|
+
import { atomicWriteFile, isRecord } from './util.ts'
|
|
4
4
|
|
|
5
5
|
/** Reasoning effort ids accepted by the Antigravity ACP `thought_level` option. */
|
|
6
6
|
export const EFFORTS = ['low', 'medium', 'high', 'xhigh', 'max'] as const
|
|
@@ -42,7 +42,7 @@ export interface Config {
|
|
|
42
42
|
quotaTtlMs: number
|
|
43
43
|
}
|
|
44
44
|
|
|
45
|
-
export const
|
|
45
|
+
export const DEFAULT_CONFIG: Config = {
|
|
46
46
|
provider: 'antigravity-acp',
|
|
47
47
|
displayName: 'Antigravity ACP',
|
|
48
48
|
command: '',
|
|
@@ -76,9 +76,7 @@ const SETTINGS_FILE = 'settings.json'
|
|
|
76
76
|
export function readJson(path: string): Record<string, unknown> {
|
|
77
77
|
try {
|
|
78
78
|
const parsed: unknown = JSON.parse(readFileSync(path, 'utf8'))
|
|
79
|
-
return parsed
|
|
80
|
-
? (parsed as Record<string, unknown>)
|
|
81
|
-
: {}
|
|
79
|
+
return isRecord(parsed) ? parsed : {}
|
|
82
80
|
} catch {
|
|
83
81
|
return {}
|
|
84
82
|
}
|