pi-agy-acp 0.2.7 → 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 +7 -0
- package/docs/architecture.md +34 -21
- package/docs/development.md +3 -3
- package/index.ts +65 -45
- package/package.json +1 -1
- 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 +59 -48
- 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 +270 -242
- package/src/session-db.ts +24 -5
- package/src/util.ts +5 -0
package/src/session-db.ts
CHANGED
|
@@ -13,6 +13,7 @@ import {
|
|
|
13
13
|
} from 'node:fs'
|
|
14
14
|
import { isAbsolute, join, relative } from 'node:path'
|
|
15
15
|
import { DatabaseSync } from 'node:sqlite'
|
|
16
|
+
import type { LlmErrorCode } from './protocol.ts'
|
|
16
17
|
|
|
17
18
|
const EMPTY_BYTES = new Uint8Array(0)
|
|
18
19
|
const MAX_VARINT_SHIFT = 63n
|
|
@@ -47,13 +48,15 @@ export type HiddenTurnErrorKind =
|
|
|
47
48
|
|
|
48
49
|
export interface HiddenTurnError {
|
|
49
50
|
kind: HiddenTurnErrorKind
|
|
50
|
-
code:
|
|
51
|
+
code: Extract<
|
|
52
|
+
LlmErrorCode,
|
|
51
53
|
| 'QUOTA'
|
|
52
54
|
| 'PROVIDER_ERROR'
|
|
53
55
|
| 'RATE_LIMIT'
|
|
54
56
|
| 'SERVER'
|
|
55
57
|
| 'UNKNOWN_MODEL'
|
|
56
58
|
| 'INVALID_REQUEST'
|
|
59
|
+
>
|
|
57
60
|
message: string
|
|
58
61
|
dirtySession?: boolean
|
|
59
62
|
resetTimestamp?: string
|
|
@@ -395,12 +398,28 @@ function extractPayloadField(
|
|
|
395
398
|
return undefined
|
|
396
399
|
}
|
|
397
400
|
|
|
398
|
-
|
|
399
|
-
|
|
401
|
+
/** Context-window overflow phrases shared with runtime error classification. */
|
|
402
|
+
export const CONTEXT_WINDOW_RE =
|
|
403
|
+
/context.*(length|window|limit|overflow|exceeded)|too long|token limit|maximum.*tokens/i
|
|
404
|
+
const HARD_QUOTA_RE =
|
|
405
|
+
/\bquota_exhausted\b|quota|prepaid\s+balance|spend\s+cap|insufficient\s+(?:funds|credits?|balance)|billing\s+(?:limit|exhausted|disabled|required)/i
|
|
406
|
+
const SOFT_QUOTA_RE = /\bexceeded your\b|\blimit reached\b/i
|
|
407
|
+
const RATE_LIMIT_PHRASE_RE = /\brate[\s-]+limit\b/i
|
|
408
|
+
|
|
409
|
+
/**
|
|
410
|
+
* Whether an error payload or message explicitly indicates quota/billing
|
|
411
|
+
* exhaustion rather than a rate limit or context-window overflow.
|
|
412
|
+
*/
|
|
413
|
+
export function isExplicitQuotaError(text: string): boolean {
|
|
414
|
+
if (HARD_QUOTA_RE.test(text)) return true
|
|
415
|
+
if (!SOFT_QUOTA_RE.test(text)) return false
|
|
416
|
+
return !RATE_LIMIT_PHRASE_RE.test(text) && !CONTEXT_WINDOW_RE.test(text)
|
|
417
|
+
}
|
|
418
|
+
|
|
400
419
|
const INVALID_THINKING_SIG_RE =
|
|
401
420
|
/\bmessages\.\d+\.content\.\d+(?:\.thinking\.signature: Field required|: Invalid `signature` in `thinking` block)/
|
|
402
421
|
const RATE_LIMIT_RE =
|
|
403
|
-
/(?:HTTP\s+429|\(code\s+429\)|"code"\s*:\s*429|\bRESOURCE_EXHAUSTED\b)/
|
|
422
|
+
/(?:HTTP\s+429|\(code\s+429\)|"code"\s*:\s*429|\bRESOURCE_EXHAUSTED\b|\brate[\s-]+limit\b|\btoo many requests\b|\boverloaded\b|\bcapacity\b)/i
|
|
404
423
|
const NOT_FOUND_RE =
|
|
405
424
|
/(?:HTTP\s+404|\(code\s+404\)|"code"\s*:\s*404|\bNOT_FOUND\b)/
|
|
406
425
|
const INVALID_ARGUMENT_RE =
|
|
@@ -416,7 +435,7 @@ export function classifyStepErrorPayload(
|
|
|
416
435
|
): HiddenTurnError | undefined {
|
|
417
436
|
if (typeof payload !== 'string' || payload.length === 0) return undefined
|
|
418
437
|
|
|
419
|
-
if (
|
|
438
|
+
if (isExplicitQuotaError(payload)) {
|
|
420
439
|
const resetTimestamp = extractPayloadField(payload, 'quotaResetTimeStamp')
|
|
421
440
|
const rawDelay =
|
|
422
441
|
extractPayloadField(payload, 'quotaResetDelay') ??
|
package/src/util.ts
CHANGED
|
@@ -18,6 +18,11 @@ import {
|
|
|
18
18
|
} from 'node:fs'
|
|
19
19
|
import { basename, dirname, join } from 'node:path'
|
|
20
20
|
|
|
21
|
+
/** Whether `value` is a non-null, non-array object. */
|
|
22
|
+
export function isRecord(value: unknown): value is Record<string, unknown> {
|
|
23
|
+
return value !== null && typeof value === 'object' && !Array.isArray(value)
|
|
24
|
+
}
|
|
25
|
+
|
|
21
26
|
/** A promise with externally exposed settlement. */
|
|
22
27
|
export interface Deferred<T> {
|
|
23
28
|
promise: Promise<T>
|