sidekick-shared 0.21.0 → 0.21.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.
@@ -1,4 +1,10 @@
1
1
  import type { QuotaState } from './quota';
2
2
  import type { AccountProviderId } from './accountRegistry';
3
- export declare function writeQuotaSnapshot(providerId: AccountProviderId, accountId: string, quota: QuotaState): void;
4
- export declare function readQuotaSnapshot(providerId: AccountProviderId, accountId: string): QuotaState | null;
3
+ /**
4
+ * Storage key for a quota snapshot. Extends `AccountProviderId` with `'zai'`
5
+ * because z.ai has no full account-management surface in v1, but the snapshot
6
+ * store still needs a stable key for the z.ai quota cache.
7
+ */
8
+ export type QuotaSnapshotProviderId = AccountProviderId | 'zai';
9
+ export declare function writeQuotaSnapshot(providerId: QuotaSnapshotProviderId, accountId: string, quota: QuotaState): void;
10
+ export declare function readQuotaSnapshot(providerId: QuotaSnapshotProviderId, accountId: string): QuotaState | null;
@@ -12,7 +12,7 @@
12
12
  */
13
13
  export { messageUsageSchema, sessionMessageSchema, sessionEventSchema, permissionModeSchema, extractSessionEvents, } from './sessionEvent';
14
14
  export type { MessageUsage, SessionMessage, SessionEvent, PermissionMode, } from '../types/sessionEvent';
15
- export { quotaWindowSchema, quotaStateSchema, quotaFailureKindSchema, quotaProviderIdSchema, quotaSourceSchema, peakHoursStateSchema, quotaFailureDescriptorSchema, runtimeQuotaProviderSchema, providerQuotaStateSchema, claudeProviderQuotaStateSchema, codexProviderQuotaStateSchema, providerQuotaMapSchema, } from './quota';
15
+ export { quotaWindowSchema, quotaStateSchema, quotaFailureKindSchema, quotaProviderIdSchema, quotaSourceSchema, peakHoursStateSchema, quotaFailureDescriptorSchema, runtimeQuotaProviderSchema, providerQuotaStateSchema, claudeProviderQuotaStateSchema, codexProviderQuotaStateSchema, zaiProviderQuotaStateSchema, providerQuotaMapSchema, } from './quota';
16
16
  export type { QuotaWindow, QuotaState } from '../quota';
17
17
  export type { PeakHoursState } from '../peakHours';
18
18
  export type { QuotaFailureDescriptor } from '../quotaPresentation';
@@ -12,7 +12,7 @@
12
12
  * subpath is self-sufficient for boundary-validation modules.
13
13
  */
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
- exports.assistantTurnToolRefSchema = exports.assistantTurnToolGroupStepSchema = exports.assistantTurnTimelineItemSchema = exports.assistantTurnSubagentStatusSchema = exports.assistantTurnSubagentSchema = exports.assistantTurnReasoningTimelineItemSchema = exports.assistantTurnProjectionSchema = exports.assistantTurnProcessStepSchema = exports.assistantTurnProcessSchema = exports.assistantTurnNarrationStepSchema = exports.assistantTurnEventTypeSchema = exports.assistantTurnEventSchema = exports.listAllAccountsResultSchema = exports.savedAccountProfileSchema = exports.accountEntrySchema = exports.accountLoginStatusSchema = exports.beginAccountLoginResultSchema = exports.accountManagerResultSchema = exports.accountProviderIdSchema = exports.activeAccountStatusSchema = exports.activeProviderAccountStatusSchema = exports.quotaHistoryDailyBucketSchema = exports.quotaHistorySampleSchema = exports.quotaHistoryRuntimeProviderSchema = exports.providerQuotaMapSchema = exports.codexProviderQuotaStateSchema = exports.claudeProviderQuotaStateSchema = exports.providerQuotaStateSchema = exports.runtimeQuotaProviderSchema = exports.quotaFailureDescriptorSchema = exports.peakHoursStateSchema = exports.quotaSourceSchema = exports.quotaProviderIdSchema = exports.quotaFailureKindSchema = exports.quotaStateSchema = exports.quotaWindowSchema = exports.extractSessionEvents = exports.permissionModeSchema = exports.sessionEventSchema = exports.sessionMessageSchema = exports.messageUsageSchema = void 0;
15
+ exports.assistantTurnToolRefSchema = exports.assistantTurnToolGroupStepSchema = exports.assistantTurnTimelineItemSchema = exports.assistantTurnSubagentStatusSchema = exports.assistantTurnSubagentSchema = exports.assistantTurnReasoningTimelineItemSchema = exports.assistantTurnProjectionSchema = exports.assistantTurnProcessStepSchema = exports.assistantTurnProcessSchema = exports.assistantTurnNarrationStepSchema = exports.assistantTurnEventTypeSchema = exports.assistantTurnEventSchema = exports.listAllAccountsResultSchema = exports.savedAccountProfileSchema = exports.accountEntrySchema = exports.accountLoginStatusSchema = exports.beginAccountLoginResultSchema = exports.accountManagerResultSchema = exports.accountProviderIdSchema = exports.activeAccountStatusSchema = exports.activeProviderAccountStatusSchema = exports.quotaHistoryDailyBucketSchema = exports.quotaHistorySampleSchema = exports.quotaHistoryRuntimeProviderSchema = exports.providerQuotaMapSchema = exports.zaiProviderQuotaStateSchema = exports.codexProviderQuotaStateSchema = exports.claudeProviderQuotaStateSchema = exports.providerQuotaStateSchema = exports.runtimeQuotaProviderSchema = exports.quotaFailureDescriptorSchema = exports.peakHoursStateSchema = exports.quotaSourceSchema = exports.quotaProviderIdSchema = exports.quotaFailureKindSchema = exports.quotaStateSchema = exports.quotaWindowSchema = exports.extractSessionEvents = exports.permissionModeSchema = exports.sessionEventSchema = exports.sessionMessageSchema = exports.messageUsageSchema = void 0;
16
16
  var sessionEvent_1 = require("./sessionEvent");
17
17
  Object.defineProperty(exports, "messageUsageSchema", { enumerable: true, get: function () { return sessionEvent_1.messageUsageSchema; } });
18
18
  Object.defineProperty(exports, "sessionMessageSchema", { enumerable: true, get: function () { return sessionEvent_1.sessionMessageSchema; } });
@@ -31,6 +31,7 @@ Object.defineProperty(exports, "runtimeQuotaProviderSchema", { enumerable: true,
31
31
  Object.defineProperty(exports, "providerQuotaStateSchema", { enumerable: true, get: function () { return quota_1.providerQuotaStateSchema; } });
32
32
  Object.defineProperty(exports, "claudeProviderQuotaStateSchema", { enumerable: true, get: function () { return quota_1.claudeProviderQuotaStateSchema; } });
33
33
  Object.defineProperty(exports, "codexProviderQuotaStateSchema", { enumerable: true, get: function () { return quota_1.codexProviderQuotaStateSchema; } });
34
+ Object.defineProperty(exports, "zaiProviderQuotaStateSchema", { enumerable: true, get: function () { return quota_1.zaiProviderQuotaStateSchema; } });
34
35
  Object.defineProperty(exports, "providerQuotaMapSchema", { enumerable: true, get: function () { return quota_1.providerQuotaMapSchema; } });
35
36
  var quotaHistory_1 = require("./quotaHistory");
36
37
  Object.defineProperty(exports, "quotaHistoryRuntimeProviderSchema", { enumerable: true, get: function () { return quotaHistory_1.quotaHistoryRuntimeProviderSchema; } });
@@ -23,6 +23,7 @@ export declare const quotaFailureKindSchema: z.ZodEnum<{
23
23
  export declare const quotaProviderIdSchema: z.ZodEnum<{
24
24
  "claude-code": "claude-code";
25
25
  codex: "codex";
26
+ zai: "zai";
26
27
  }>;
27
28
  export declare const quotaSourceSchema: z.ZodEnum<{
28
29
  api: "api";
@@ -54,6 +55,7 @@ export declare const quotaStateSchema: z.ZodObject<{
54
55
  providerId: z.ZodOptional<z.ZodEnum<{
55
56
  "claude-code": "claude-code";
56
57
  codex: "codex";
58
+ zai: "zai";
57
59
  }>>;
58
60
  source: z.ZodOptional<z.ZodEnum<{
59
61
  api: "api";
@@ -106,6 +108,7 @@ export declare const quotaFailureDescriptorSchema: z.ZodObject<{
106
108
  export declare const runtimeQuotaProviderSchema: z.ZodEnum<{
107
109
  codex: "codex";
108
110
  claude: "claude";
111
+ zai: "zai";
109
112
  }>;
110
113
  export declare const providerQuotaStateSchema: z.ZodObject<{
111
114
  fiveHour: z.ZodObject<{
@@ -132,6 +135,7 @@ export declare const providerQuotaStateSchema: z.ZodObject<{
132
135
  providerId: z.ZodOptional<z.ZodEnum<{
133
136
  "claude-code": "claude-code";
134
137
  codex: "codex";
138
+ zai: "zai";
135
139
  }>>;
136
140
  source: z.ZodOptional<z.ZodEnum<{
137
141
  api: "api";
@@ -185,6 +189,7 @@ export declare const providerQuotaStateSchema: z.ZodObject<{
185
189
  runtimeProvider: z.ZodEnum<{
186
190
  codex: "codex";
187
191
  claude: "claude";
192
+ zai: "zai";
188
193
  }>;
189
194
  }, z.core.$strip>;
190
195
  export declare const claudeProviderQuotaStateSchema: z.ZodObject<{
@@ -212,6 +217,7 @@ export declare const claudeProviderQuotaStateSchema: z.ZodObject<{
212
217
  providerId: z.ZodOptional<z.ZodEnum<{
213
218
  "claude-code": "claude-code";
214
219
  codex: "codex";
220
+ zai: "zai";
215
221
  }>>;
216
222
  source: z.ZodOptional<z.ZodEnum<{
217
223
  api: "api";
@@ -289,6 +295,7 @@ export declare const codexProviderQuotaStateSchema: z.ZodObject<{
289
295
  providerId: z.ZodOptional<z.ZodEnum<{
290
296
  "claude-code": "claude-code";
291
297
  codex: "codex";
298
+ zai: "zai";
292
299
  }>>;
293
300
  source: z.ZodOptional<z.ZodEnum<{
294
301
  api: "api";
@@ -341,6 +348,84 @@ export declare const codexProviderQuotaStateSchema: z.ZodObject<{
341
348
  }, z.core.$strip>>>;
342
349
  runtimeProvider: z.ZodLiteral<"codex">;
343
350
  }, z.core.$strip>;
351
+ export declare const zaiProviderQuotaStateSchema: z.ZodObject<{
352
+ fiveHour: z.ZodObject<{
353
+ utilization: z.ZodNumber;
354
+ resetsAt: z.ZodString;
355
+ }, z.core.$strip>;
356
+ sevenDay: z.ZodObject<{
357
+ utilization: z.ZodNumber;
358
+ resetsAt: z.ZodString;
359
+ }, z.core.$strip>;
360
+ available: z.ZodBoolean;
361
+ error: z.ZodOptional<z.ZodString>;
362
+ failureKind: z.ZodOptional<z.ZodEnum<{
363
+ unknown: "unknown";
364
+ auth: "auth";
365
+ network: "network";
366
+ rate_limit: "rate_limit";
367
+ server: "server";
368
+ }>>;
369
+ httpStatus: z.ZodOptional<z.ZodNumber>;
370
+ retryAfterMs: z.ZodOptional<z.ZodNumber>;
371
+ projectedFiveHour: z.ZodOptional<z.ZodNumber>;
372
+ projectedSevenDay: z.ZodOptional<z.ZodNumber>;
373
+ providerId: z.ZodOptional<z.ZodEnum<{
374
+ "claude-code": "claude-code";
375
+ codex: "codex";
376
+ zai: "zai";
377
+ }>>;
378
+ source: z.ZodOptional<z.ZodEnum<{
379
+ api: "api";
380
+ session: "session";
381
+ cache: "cache";
382
+ }>>;
383
+ capturedAt: z.ZodOptional<z.ZodString>;
384
+ stale: z.ZodOptional<z.ZodBoolean>;
385
+ fiveHourLabel: z.ZodOptional<z.ZodString>;
386
+ sevenDayLabel: z.ZodOptional<z.ZodString>;
387
+ limitId: z.ZodOptional<z.ZodString>;
388
+ limitName: z.ZodOptional<z.ZodString>;
389
+ credits: z.ZodOptional<z.ZodUnknown>;
390
+ planType: z.ZodOptional<z.ZodString>;
391
+ rateLimitReachedType: z.ZodOptional<z.ZodString>;
392
+ accountLabel: z.ZodOptional<z.ZodString>;
393
+ accountDetail: z.ZodOptional<z.ZodString>;
394
+ peakHours: z.ZodOptional<z.ZodNullable<z.ZodObject<{
395
+ status: z.ZodEnum<{
396
+ unknown: "unknown";
397
+ peak: "peak";
398
+ off_peak: "off_peak";
399
+ }>;
400
+ isPeak: z.ZodBoolean;
401
+ sessionLimitSpeed: z.ZodEnum<{
402
+ unknown: "unknown";
403
+ normal: "normal";
404
+ faster: "faster";
405
+ }>;
406
+ label: z.ZodString;
407
+ peakHoursDescription: z.ZodString;
408
+ nextChange: z.ZodNullable<z.ZodString>;
409
+ minutesUntilChange: z.ZodNullable<z.ZodNumber>;
410
+ note: z.ZodString;
411
+ updatedAt: z.ZodString;
412
+ unavailable: z.ZodBoolean;
413
+ notApplicable: z.ZodOptional<z.ZodBoolean>;
414
+ }, z.core.$strip>>>;
415
+ failure: z.ZodOptional<z.ZodNullable<z.ZodObject<{
416
+ severity: z.ZodEnum<{
417
+ error: "error";
418
+ warning: "warning";
419
+ info: "info";
420
+ }>;
421
+ title: z.ZodString;
422
+ message: z.ZodString;
423
+ detail: z.ZodOptional<z.ZodString>;
424
+ alertKey: z.ZodString;
425
+ isRetryable: z.ZodBoolean;
426
+ }, z.core.$strip>>>;
427
+ runtimeProvider: z.ZodLiteral<"zai">;
428
+ }, z.core.$strip>;
344
429
  export declare const providerQuotaMapSchema: z.ZodObject<{
345
430
  claude: z.ZodOptional<z.ZodObject<{
346
431
  fiveHour: z.ZodObject<{
@@ -367,6 +452,7 @@ export declare const providerQuotaMapSchema: z.ZodObject<{
367
452
  providerId: z.ZodOptional<z.ZodEnum<{
368
453
  "claude-code": "claude-code";
369
454
  codex: "codex";
455
+ zai: "zai";
370
456
  }>>;
371
457
  source: z.ZodOptional<z.ZodEnum<{
372
458
  api: "api";
@@ -444,6 +530,7 @@ export declare const providerQuotaMapSchema: z.ZodObject<{
444
530
  providerId: z.ZodOptional<z.ZodEnum<{
445
531
  "claude-code": "claude-code";
446
532
  codex: "codex";
533
+ zai: "zai";
447
534
  }>>;
448
535
  source: z.ZodOptional<z.ZodEnum<{
449
536
  api: "api";
@@ -496,4 +583,82 @@ export declare const providerQuotaMapSchema: z.ZodObject<{
496
583
  }, z.core.$strip>>>;
497
584
  runtimeProvider: z.ZodLiteral<"codex">;
498
585
  }, z.core.$strip>>;
586
+ zai: z.ZodOptional<z.ZodObject<{
587
+ fiveHour: z.ZodObject<{
588
+ utilization: z.ZodNumber;
589
+ resetsAt: z.ZodString;
590
+ }, z.core.$strip>;
591
+ sevenDay: z.ZodObject<{
592
+ utilization: z.ZodNumber;
593
+ resetsAt: z.ZodString;
594
+ }, z.core.$strip>;
595
+ available: z.ZodBoolean;
596
+ error: z.ZodOptional<z.ZodString>;
597
+ failureKind: z.ZodOptional<z.ZodEnum<{
598
+ unknown: "unknown";
599
+ auth: "auth";
600
+ network: "network";
601
+ rate_limit: "rate_limit";
602
+ server: "server";
603
+ }>>;
604
+ httpStatus: z.ZodOptional<z.ZodNumber>;
605
+ retryAfterMs: z.ZodOptional<z.ZodNumber>;
606
+ projectedFiveHour: z.ZodOptional<z.ZodNumber>;
607
+ projectedSevenDay: z.ZodOptional<z.ZodNumber>;
608
+ providerId: z.ZodOptional<z.ZodEnum<{
609
+ "claude-code": "claude-code";
610
+ codex: "codex";
611
+ zai: "zai";
612
+ }>>;
613
+ source: z.ZodOptional<z.ZodEnum<{
614
+ api: "api";
615
+ session: "session";
616
+ cache: "cache";
617
+ }>>;
618
+ capturedAt: z.ZodOptional<z.ZodString>;
619
+ stale: z.ZodOptional<z.ZodBoolean>;
620
+ fiveHourLabel: z.ZodOptional<z.ZodString>;
621
+ sevenDayLabel: z.ZodOptional<z.ZodString>;
622
+ limitId: z.ZodOptional<z.ZodString>;
623
+ limitName: z.ZodOptional<z.ZodString>;
624
+ credits: z.ZodOptional<z.ZodUnknown>;
625
+ planType: z.ZodOptional<z.ZodString>;
626
+ rateLimitReachedType: z.ZodOptional<z.ZodString>;
627
+ accountLabel: z.ZodOptional<z.ZodString>;
628
+ accountDetail: z.ZodOptional<z.ZodString>;
629
+ peakHours: z.ZodOptional<z.ZodNullable<z.ZodObject<{
630
+ status: z.ZodEnum<{
631
+ unknown: "unknown";
632
+ peak: "peak";
633
+ off_peak: "off_peak";
634
+ }>;
635
+ isPeak: z.ZodBoolean;
636
+ sessionLimitSpeed: z.ZodEnum<{
637
+ unknown: "unknown";
638
+ normal: "normal";
639
+ faster: "faster";
640
+ }>;
641
+ label: z.ZodString;
642
+ peakHoursDescription: z.ZodString;
643
+ nextChange: z.ZodNullable<z.ZodString>;
644
+ minutesUntilChange: z.ZodNullable<z.ZodNumber>;
645
+ note: z.ZodString;
646
+ updatedAt: z.ZodString;
647
+ unavailable: z.ZodBoolean;
648
+ notApplicable: z.ZodOptional<z.ZodBoolean>;
649
+ }, z.core.$strip>>>;
650
+ failure: z.ZodOptional<z.ZodNullable<z.ZodObject<{
651
+ severity: z.ZodEnum<{
652
+ error: "error";
653
+ warning: "warning";
654
+ info: "info";
655
+ }>;
656
+ title: z.ZodString;
657
+ message: z.ZodString;
658
+ detail: z.ZodOptional<z.ZodString>;
659
+ alertKey: z.ZodString;
660
+ isRetryable: z.ZodBoolean;
661
+ }, z.core.$strip>>>;
662
+ runtimeProvider: z.ZodLiteral<"zai">;
663
+ }, z.core.$strip>>;
499
664
  }, z.core.$strip>;
@@ -10,7 +10,7 @@
10
10
  * @module schemas/quota
11
11
  */
12
12
  Object.defineProperty(exports, "__esModule", { value: true });
13
- exports.providerQuotaMapSchema = exports.codexProviderQuotaStateSchema = exports.claudeProviderQuotaStateSchema = exports.providerQuotaStateSchema = exports.runtimeQuotaProviderSchema = exports.quotaFailureDescriptorSchema = exports.peakHoursStateSchema = exports.quotaStateSchema = exports.quotaSourceSchema = exports.quotaProviderIdSchema = exports.quotaFailureKindSchema = exports.quotaWindowSchema = void 0;
13
+ exports.providerQuotaMapSchema = exports.zaiProviderQuotaStateSchema = exports.codexProviderQuotaStateSchema = exports.claudeProviderQuotaStateSchema = exports.providerQuotaStateSchema = exports.runtimeQuotaProviderSchema = exports.quotaFailureDescriptorSchema = exports.peakHoursStateSchema = exports.quotaStateSchema = exports.quotaSourceSchema = exports.quotaProviderIdSchema = exports.quotaFailureKindSchema = exports.quotaWindowSchema = void 0;
14
14
  const zod_1 = require("zod");
15
15
  // ── QuotaWindow ──
16
16
  exports.quotaWindowSchema = zod_1.z.object({
@@ -28,6 +28,7 @@ exports.quotaFailureKindSchema = zod_1.z.enum([
28
28
  exports.quotaProviderIdSchema = zod_1.z.enum([
29
29
  'claude-code',
30
30
  'codex',
31
+ 'zai',
31
32
  ]);
32
33
  exports.quotaSourceSchema = zod_1.z.enum([
33
34
  'api',
@@ -83,6 +84,7 @@ exports.quotaFailureDescriptorSchema = zod_1.z.object({
83
84
  exports.runtimeQuotaProviderSchema = zod_1.z.enum([
84
85
  'claude',
85
86
  'codex',
87
+ 'zai',
86
88
  ]);
87
89
  /** Fields ProviderQuotaState adds on top of QuotaState, minus the provider tag. */
88
90
  const providerQuotaExtensionShape = {
@@ -103,7 +105,12 @@ exports.codexProviderQuotaStateSchema = exports.quotaStateSchema.extend({
103
105
  runtimeProvider: zod_1.z.literal('codex'),
104
106
  ...providerQuotaExtensionShape,
105
107
  });
108
+ exports.zaiProviderQuotaStateSchema = exports.quotaStateSchema.extend({
109
+ runtimeProvider: zod_1.z.literal('zai'),
110
+ ...providerQuotaExtensionShape,
111
+ });
106
112
  exports.providerQuotaMapSchema = zod_1.z.object({
107
113
  claude: exports.claudeProviderQuotaStateSchema.optional(),
108
114
  codex: exports.codexProviderQuotaStateSchema.optional(),
115
+ zai: exports.zaiProviderQuotaStateSchema.optional(),
109
116
  });
@@ -12,12 +12,14 @@ import { z } from 'zod';
12
12
  export declare const quotaHistoryRuntimeProviderSchema: z.ZodEnum<{
13
13
  codex: "codex";
14
14
  claude: "claude";
15
+ zai: "zai";
15
16
  }>;
16
17
  export declare const quotaHistorySampleSchema: z.ZodObject<{
17
18
  timestamp: z.ZodString;
18
19
  runtimeProvider: z.ZodEnum<{
19
20
  codex: "codex";
20
21
  claude: "claude";
22
+ zai: "zai";
21
23
  }>;
22
24
  providerId: z.ZodString;
23
25
  workspaceId: z.ZodString;
@@ -16,6 +16,7 @@ const zod_1 = require("zod");
16
16
  exports.quotaHistoryRuntimeProviderSchema = zod_1.z.enum([
17
17
  'claude',
18
18
  'codex',
19
+ 'zai',
19
20
  ]);
20
21
  // ── QuotaHistorySample ──
21
22
  exports.quotaHistorySampleSchema = zod_1.z.object({
@@ -0,0 +1,209 @@
1
+ /**
2
+ * Deprecated stateless quota accumulator for z.ai coding plans.
3
+ *
4
+ * This estimator is kept for compatibility with consumers that imported the
5
+ * old helpers directly. Product code should use `zaiQuotaApi.ts`, which calls
6
+ * z.ai's authoritative quota endpoint. The legacy estimator only uses:
7
+ *
8
+ * 1. Per-call token `usage` returned inline on each chat completion —
9
+ * which OpenCode persists into `opencode.db` message rows tagged with
10
+ * `providerID ∈ {"zai", "zai-coding-plan"}`.
11
+ * 2. Business error codes (`1308` / `1310` / `1313` / `1309`) returned
12
+ * when a limit is actually hit, with `${next_flush_time}` embedded
13
+ * in the human-readable message text.
14
+ *
15
+ * This module is therefore a **derivation** rather than a fetcher: callers
16
+ * feed it observed assistant turns (and any error events), and it computes a
17
+ * compatibility `QuotaState` keyed against the published per-tier budgets.
18
+ *
19
+ * Polling / eventing / persistence are the caller's responsibility —
20
+ * see `ZaiQuotaWatcher` for the bundled watcher.
21
+ */
22
+ import type { QuotaState } from './quota';
23
+ /**
24
+ * Published z.ai coding-plan prompt budgets.
25
+ *
26
+ * Source: https://docs.z.ai/devpack/overview & /devpack/faq.
27
+ * "One prompt ≈ one query; each prompt is estimated to invoke the model
28
+ * 15–20 times." We use the midpoint (17.5) when converting observed
29
+ * assistant turns into prompt equivalents.
30
+ */
31
+ export declare const ZAI_TIER_BUDGETS: {
32
+ readonly lite: {
33
+ readonly fiveHour: 80;
34
+ readonly weekly: 400;
35
+ };
36
+ readonly pro: {
37
+ readonly fiveHour: 400;
38
+ readonly weekly: 2000;
39
+ };
40
+ readonly max: {
41
+ readonly fiveHour: 1600;
42
+ readonly weekly: 8000;
43
+ };
44
+ };
45
+ export type ZaiTier = 'lite' | 'pro' | 'max';
46
+ /**
47
+ * Midpoint of z.ai's documented "15–20 model invocations per prompt" range.
48
+ * Used to convert accumulated assistant turns into prompt equivalents.
49
+ * Calibration note: this constant may need per-tier adjustment after
50
+ * real-world validation against the z.ai web UI.
51
+ */
52
+ export declare const ZAI_PROMPT_INVOCATIONS = 17.5;
53
+ /**
54
+ * Provider IDs that OpenCode tags on z.ai-routed messages.
55
+ * `zai-coding-plan` is the Coding Plan key; `zai` is the bare API key.
56
+ */
57
+ export declare const ZAI_PROVIDER_IDS: readonly ["zai", "zai-coding-plan"];
58
+ export type ZaiProviderId = (typeof ZAI_PROVIDER_IDS)[number];
59
+ export declare function isZaiProviderId(value: unknown): value is ZaiProviderId;
60
+ /**
61
+ * Minimal description of a z.ai-routed assistant turn needed to accumulate
62
+ * usage. Mirrors the fields OpenCode already extracts via its parser
63
+ * (`openCodeParser.ts:346-357`). Tokens are Anthropic-style for cross-
64
+ * provider comparability; cost is omitted (always 0 for z.ai).
65
+ *
66
+ * `timestampMs` is when the assistant turn completed (epoch milliseconds).
67
+ */
68
+ export interface ZaiAssistantTurn {
69
+ timestampMs: number;
70
+ model?: string;
71
+ inputTokens: number;
72
+ outputTokens: number;
73
+ cacheReadTokens: number;
74
+ cacheWriteTokens: number;
75
+ reasoningTokens: number;
76
+ }
77
+ export interface ZaiAccumulatedUsage {
78
+ /** Number of z.ai assistant turns inside the 5-hour window. */
79
+ fiveHourTurns: number;
80
+ /** Number of z.ai assistant turns inside the 7-day window. */
81
+ weeklyTurns: number;
82
+ /** Sum of token fields across the 5-hour window. */
83
+ fiveHourTokens: number;
84
+ /** Sum of token fields across the 7-day window. */
85
+ weeklyTokens: number;
86
+ /** Estimated prompts consumed in the 5-hour window (turns / 17.5). */
87
+ fiveHourPrompts: number;
88
+ /** Estimated prompts consumed in the 7-day window (turns / 17.5). */
89
+ weeklyPrompts: number;
90
+ /** Earliest turn timestamp inside the 5-hour window (epoch ms), or null. */
91
+ fiveHourStartedAtMs: number | null;
92
+ /** Earliest turn timestamp inside the 7-day window (epoch ms), or null. */
93
+ weeklyStartedAtMs: number | null;
94
+ }
95
+ /**
96
+ * Sums the token fields of a single turn into a scalar.
97
+ * Cache reads are weighted at 0.1× (Anthropic convention) to avoid
98
+ * over-counting repeated context. Output and reasoning tokens are weighted 1×.
99
+ */
100
+ export declare function turnTokenWeight(turn: Pick<ZaiAssistantTurn, 'inputTokens' | 'outputTokens' | 'cacheReadTokens' | 'cacheWriteTokens' | 'reasoningTokens'>): number;
101
+ /**
102
+ * Reduces a stream of z.ai-routed assistant turns into rolling-window usage.
103
+ *
104
+ * Pure & stateless: the caller is responsible for passing the relevant turns.
105
+ * Turns outside the 7-day window are ignored; turns inside the 7-day window
106
+ * but outside the 5-hour window still count toward weekly usage.
107
+ */
108
+ export declare function accumulateZaiUsage(turns: readonly ZaiAssistantTurn[], nowMs?: number): ZaiAccumulatedUsage;
109
+ /**
110
+ * Resolves the effective z.ai tier when the user has selected `'auto'`.
111
+ *
112
+ * Heuristic: if accumulated weekly prompts already exceed a tier's budget,
113
+ * the user must be on a higher tier. This under-detects at the start of a
114
+ * weekly cycle (a Max user looks identical to a Lite user until they cross
115
+ * 400 prompts/week), so callers should still expose an explicit override.
116
+ */
117
+ export declare function resolveZaiTier(configured: ZaiTier | 'auto', accumulated: ZaiAccumulatedUsage): ZaiTier;
118
+ export interface InferZaiQuotaStateOptions {
119
+ /** ISO timestamp to stamp on the sample. Default: now. */
120
+ capturedAt?: string;
121
+ /** Mark the sample stale when the windows aren't fully observed yet. */
122
+ stale?: boolean;
123
+ /** Authoritative reset timestamp from a trapped 1308/1310 error. */
124
+ authoritativeFiveHourResetAt?: string;
125
+ authoritativeWeeklyResetAt?: string;
126
+ }
127
+ /**
128
+ * Converts accumulated usage into a shared `QuotaState`.
129
+ *
130
+ * The two windows reuse the `fiveHour` / `sevenDay` field names for cross-
131
+ * provider compatibility, but are labeled "5-Hour" and "Weekly" via
132
+ * `fiveHourLabel` / `sevenDayLabel` — mirroring how Codex relabels its
133
+ * generic primary/secondary windows.
134
+ *
135
+ * Reset timestamps are best-effort because z.ai does not expose them:
136
+ * - `fiveHour.resetsAt` ← first turn in window + 5h, unless an error
137
+ * event supplied an authoritative reset.
138
+ * - `sevenDay.resetsAt` ← first turn in window + 7d (documented as
139
+ * "7 days from order time" — order time is unknown, so this is a
140
+ * lower bound at best).
141
+ */
142
+ export declare function inferZaiQuotaState(accumulated: ZaiAccumulatedUsage, tier: ZaiTier, options?: InferZaiQuotaStateOptions): QuotaState;
143
+ export type ZaiQuotaErrorKind = 'exhausted' | 'fup' | 'expired' | 'invalid';
144
+ export interface ZaiQuotaError {
145
+ kind: ZaiQuotaErrorKind;
146
+ /** ISO timestamp parsed from `${next_flush_time}` in the error message, if present. */
147
+ resetsAt?: string;
148
+ /** The raw business error code from z.ai. */
149
+ code: string;
150
+ /** Human-readable message. */
151
+ message: string;
152
+ }
153
+ /**
154
+ * Extracts a z.ai quota error from a message-error payload, if present.
155
+ *
156
+ * The reset timestamp is embedded inside the human-readable `message` text
157
+ * as `${next_flush_time}`. z.ai's docs interpolate the value into the
158
+ * message string; we parse ISO datetimes, epoch seconds, and the literal
159
+ * `YYYY-MM-DD HH:mm:ss` form defensively.
160
+ *
161
+ * Returns `null` when the code is not a quota-related business error.
162
+ */
163
+ export declare function parseZaiQuotaError(error: {
164
+ code?: string | number;
165
+ message?: string;
166
+ type?: string;
167
+ }): ZaiQuotaError | null;
168
+ /**
169
+ * Parses `${next_flush_time}` out of a z.ai error message.
170
+ *
171
+ * Handles ISO 8601 (`2025-01-31T14:00:00Z`), space-separated
172
+ * (`2025-01-31 14:00:00`), and epoch-seconds (1234567890) forms.
173
+ * Returns undefined when no parseable timestamp is found.
174
+ */
175
+ export declare function extractNextFlushTime(message: string): string | undefined;
176
+ /**
177
+ * Builds an unavailable z.ai `QuotaState` for the cold-start case where
178
+ * no z.ai traffic has been observed yet.
179
+ */
180
+ export declare function makeUnavailableZaiQuotaState(error?: string, tier?: ZaiTier, capturedAt?: string): QuotaState;
181
+ /**
182
+ * Shape of a row returned by `OpenCodeDatabase.getAssistantMessagesByProviderId`.
183
+ * Defined here to keep the sidekick-shared helper self-contained without
184
+ * leaking the OpenCode db module into the public quota surface.
185
+ */
186
+ export interface ZaiOpenCodeRow {
187
+ timeCreated: number;
188
+ modelId: string;
189
+ providerId: string;
190
+ inputTokens: number;
191
+ outputTokens: number;
192
+ cacheReadTokens: number;
193
+ cacheWriteTokens: number;
194
+ reasoningTokens: number;
195
+ errorMessage: string | null;
196
+ errorCode: string | null;
197
+ }
198
+ /**
199
+ * Converts raw OpenCode DB rows (filtered to z.ai providerIDs by the caller)
200
+ * into the watcher's `ZaiAssistantTurn` shape, and surfaces any trapped
201
+ * business errors.
202
+ *
203
+ * Returns `{ turns, errors }` so callers can replay them through
204
+ * `ZaiQuotaWatcher.ingestAssistantTurns` and `ingestError`.
205
+ */
206
+ export declare function rowsToZaiTurnsAndErrors(rows: readonly ZaiOpenCodeRow[]): {
207
+ turns: ZaiAssistantTurn[];
208
+ errors: ZaiQuotaError[];
209
+ };