pi-agy-acp 0.2.1 → 0.2.2
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 +9 -0
- package/docs/development.md +15 -6
- package/docs/reference.md +15 -8
- package/index.ts +11 -2
- package/package.json +1 -1
- package/src/acp-process.ts +15 -1
- package/src/catalog.ts +29 -19
- package/src/conversation.ts +66 -16
- package/src/format.ts +68 -23
- package/src/host.ts +148 -13
- package/src/jsonrpc.ts +38 -4
- package/src/mcp-bridge.ts +26 -2
- package/src/preflight.ts +296 -371
- package/src/quota.ts +39 -20
- package/src/runtime.ts +305 -104
- package/src/session-db.ts +670 -0
package/src/quota.ts
CHANGED
|
@@ -194,6 +194,27 @@ export interface QuotaSnapshot {
|
|
|
194
194
|
account?: AccountInfo
|
|
195
195
|
}
|
|
196
196
|
|
|
197
|
+
function parseBucket(raw: unknown): QuotaBucket | undefined {
|
|
198
|
+
if (raw === null || typeof raw !== 'object' || Array.isArray(raw))
|
|
199
|
+
return undefined
|
|
200
|
+
const bucket = raw as Record<string, unknown>
|
|
201
|
+
if (bucket.disabled === true) return undefined
|
|
202
|
+
const id = String(bucket.id ?? '').trim()
|
|
203
|
+
const window = String(bucket.window ?? '').trim()
|
|
204
|
+
if (id.length === 0 || window.length === 0) return undefined
|
|
205
|
+
return {
|
|
206
|
+
id,
|
|
207
|
+
name: String(bucket.name ?? ''),
|
|
208
|
+
window,
|
|
209
|
+
remainingFraction:
|
|
210
|
+
typeof bucket.remaining_fraction === 'number' &&
|
|
211
|
+
Number.isFinite(bucket.remaining_fraction)
|
|
212
|
+
? bucket.remaining_fraction
|
|
213
|
+
: undefined,
|
|
214
|
+
resetTime: String(bucket.reset_time ?? ''),
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
|
|
197
218
|
/**
|
|
198
219
|
* Parse `agy -p /usage --output-format json` output.
|
|
199
220
|
* @param stdout - command stdout.
|
|
@@ -215,20 +236,9 @@ export function parseUsage(stdout: string): QuotaGroup[] {
|
|
|
215
236
|
return {
|
|
216
237
|
name: String(group.name ?? ''),
|
|
217
238
|
description: String(group.description ?? ''),
|
|
218
|
-
buckets: (Array.isArray(group.buckets) ? group.buckets : [])
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
id: String(bucket.id ?? ''),
|
|
222
|
-
name: String(bucket.name ?? ''),
|
|
223
|
-
window: String(bucket.window ?? ''),
|
|
224
|
-
remainingFraction:
|
|
225
|
-
typeof bucket.remaining_fraction === 'number' &&
|
|
226
|
-
Number.isFinite(bucket.remaining_fraction)
|
|
227
|
-
? bucket.remaining_fraction
|
|
228
|
-
: undefined,
|
|
229
|
-
resetTime: String(bucket.reset_time ?? ''),
|
|
230
|
-
}
|
|
231
|
-
}),
|
|
239
|
+
buckets: (Array.isArray(group.buckets) ? group.buckets : [])
|
|
240
|
+
.map(parseBucket)
|
|
241
|
+
.filter((bucket): bucket is QuotaBucket => bucket !== undefined),
|
|
232
242
|
}
|
|
233
243
|
})
|
|
234
244
|
}
|
|
@@ -236,27 +246,36 @@ export function parseUsage(stdout: string): QuotaGroup[] {
|
|
|
236
246
|
/** Cached quota reader with request coalescing. */
|
|
237
247
|
export class QuotaReader {
|
|
238
248
|
private cached: QuotaSnapshot | undefined
|
|
239
|
-
|
|
249
|
+
// Monotonic `performance.now()` starts near 0, so `-Infinity` guarantees an empty cache is never fresh.
|
|
250
|
+
private cachedAt = -Infinity
|
|
240
251
|
private inflight: Promise<QuotaSnapshot> | undefined
|
|
241
252
|
private readonly binary: () => string
|
|
242
253
|
private readonly run: CommandRunner
|
|
243
254
|
private readonly ttlMs: number
|
|
255
|
+
private readonly now: () => number
|
|
244
256
|
|
|
245
257
|
/**
|
|
246
258
|
* @param binary - returns the agy executable.
|
|
247
259
|
* @param run - helper command runner.
|
|
248
260
|
* @param ttlMs - cache lifetime.
|
|
261
|
+
* @param now - monotonic clock in milliseconds.
|
|
249
262
|
*/
|
|
250
|
-
constructor(
|
|
263
|
+
constructor(
|
|
264
|
+
binary: () => string,
|
|
265
|
+
run: CommandRunner,
|
|
266
|
+
ttlMs: number,
|
|
267
|
+
now: () => number = () => performance.now(),
|
|
268
|
+
) {
|
|
251
269
|
this.binary = binary
|
|
252
270
|
this.run = run
|
|
253
271
|
this.ttlMs = ttlMs
|
|
272
|
+
this.now = now
|
|
254
273
|
}
|
|
255
274
|
|
|
256
275
|
/** Fresh successful data only; never starts a command. */
|
|
257
276
|
peek(): QuotaSnapshot | undefined {
|
|
258
277
|
return this.cached?.status === 'ok' &&
|
|
259
|
-
|
|
278
|
+
this.now() - this.cachedAt < this.ttlMs
|
|
260
279
|
? this.cached
|
|
261
280
|
: undefined
|
|
262
281
|
}
|
|
@@ -274,7 +293,7 @@ export class QuotaReader {
|
|
|
274
293
|
if (
|
|
275
294
|
!force &&
|
|
276
295
|
this.cached !== undefined &&
|
|
277
|
-
|
|
296
|
+
this.now() - this.cachedAt < this.ttlMs
|
|
278
297
|
)
|
|
279
298
|
return this.cached
|
|
280
299
|
this.inflight ??= this.fetch().finally(() => {
|
|
@@ -317,7 +336,7 @@ export class QuotaReader {
|
|
|
317
336
|
groups: parseUsage(result.stdout),
|
|
318
337
|
}
|
|
319
338
|
this.cached = snapshot
|
|
320
|
-
this.cachedAt =
|
|
339
|
+
this.cachedAt = this.now()
|
|
321
340
|
return snapshot
|
|
322
341
|
} catch (error) {
|
|
323
342
|
const snapshot: QuotaSnapshot = {
|
|
@@ -328,7 +347,7 @@ export class QuotaReader {
|
|
|
328
347
|
}
|
|
329
348
|
// Apply the same TTL to failures so settled events cannot hammer an unavailable CLI.
|
|
330
349
|
this.cached = snapshot
|
|
331
|
-
this.cachedAt =
|
|
350
|
+
this.cachedAt = this.now()
|
|
332
351
|
return snapshot
|
|
333
352
|
}
|
|
334
353
|
}
|