@linnlabs/linnkit 0.10.0 → 0.21.0

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.
Files changed (91) hide show
  1. package/CHANGELOG.md +156 -1
  2. package/dist/{agent-invocation-BHcNfrBV.d.cts → agent-invocation-CAlm9qOw.d.cts} +1 -1
  3. package/dist/{agent-invocation-BznDaXDs.d.ts → agent-invocation-DKoEDLkl.d.ts} +1 -1
  4. package/dist/{ai-engine.types-vZRnQcJa.d.ts → ai-engine.types-CeLoM624.d.cts} +9 -1
  5. package/dist/{ai-engine.types-BpeU_XQG.d.cts → ai-engine.types-LqmnfR2W.d.ts} +9 -1
  6. package/dist/{audit-CtcfART1.d.ts → audit-BuWHoMEr.d.ts} +4 -2
  7. package/dist/{audit-LeOrm2hX.d.cts → audit-DnzNQzwq.d.cts} +4 -2
  8. package/dist/cli.cjs +565 -194
  9. package/dist/cli.cjs.map +1 -1
  10. package/dist/cli.js +565 -194
  11. package/dist/cli.js.map +1 -1
  12. package/dist/context-manager.cjs +590 -26
  13. package/dist/context-manager.cjs.map +1 -1
  14. package/dist/context-manager.d.cts +84 -7
  15. package/dist/context-manager.d.ts +84 -7
  16. package/dist/context-manager.js +590 -26
  17. package/dist/context-manager.js.map +1 -1
  18. package/dist/{context-trace-BHKDS-eq.d.cts → context-trace-DrtvKac4.d.cts} +36 -3
  19. package/dist/{context-trace-CHbqHmyE.d.ts → context-trace-WV1HAZeB.d.ts} +36 -3
  20. package/dist/contracts.cjs +222 -1
  21. package/dist/contracts.cjs.map +1 -1
  22. package/dist/contracts.d.cts +5 -3
  23. package/dist/contracts.d.ts +5 -3
  24. package/dist/contracts.js +202 -2
  25. package/dist/contracts.js.map +1 -1
  26. package/dist/{defaultGraphExecutor-C2E59v_R.d.cts → defaultGraphExecutor-L1ri3oij.d.cts} +35 -7
  27. package/dist/{defaultGraphExecutor-B29_qTHy.d.ts → defaultGraphExecutor-QzzdudLQ.d.ts} +35 -7
  28. package/dist/{index-BAaUP9yU.d.cts → index-BeQO0rx2.d.cts} +328 -263
  29. package/dist/{index-Cvr23YCl.d.cts → index-CDofGr_4.d.ts} +24 -6
  30. package/dist/{index-CKQzzZ5Y.d.cts → index-Crl5E4eF.d.cts} +26 -3
  31. package/dist/{index-C0DAjsdX.d.ts → index-DWGKvssS.d.ts} +2 -2
  32. package/dist/{index-D0mKxTR5.d.ts → index-X5lYU1Lj.d.ts} +26 -3
  33. package/dist/{index-DDzuSb0n.d.ts → index-uCNCpJBg.d.cts} +24 -6
  34. package/dist/{index-BaVpVNi2.d.ts → index-wOZD-p2-.d.ts} +328 -263
  35. package/dist/{index-BnYCS8Zg.d.cts → index-wXVAHZ0D.d.cts} +2 -2
  36. package/dist/index.cjs +893 -312
  37. package/dist/index.cjs.map +1 -1
  38. package/dist/index.d.cts +17 -14
  39. package/dist/index.d.ts +17 -14
  40. package/dist/index.js +893 -312
  41. package/dist/index.js.map +1 -1
  42. package/dist/{messages-XthmnHZ3.d.cts → messages-Br5SiLD3.d.cts} +706 -1
  43. package/dist/{messages-XthmnHZ3.d.ts → messages-Br5SiLD3.d.ts} +706 -1
  44. package/dist/{ports-s-tSp3sB.d.cts → ports-Dugizzje.d.cts} +4 -0
  45. package/dist/{ports-DpPTFhSd.d.ts → ports-fhp84x_C.d.ts} +4 -0
  46. package/dist/ports.d.cts +8 -5
  47. package/dist/ports.d.ts +8 -5
  48. package/dist/quickstart.cjs +564 -193
  49. package/dist/quickstart.cjs.map +1 -1
  50. package/dist/quickstart.d.cts +9 -8
  51. package/dist/quickstart.d.ts +9 -8
  52. package/dist/quickstart.js +564 -193
  53. package/dist/quickstart.js.map +1 -1
  54. package/dist/{runAgent-C6F-399C.d.ts → runAgent-BmMSju8I.d.ts} +4 -4
  55. package/dist/{runAgent-ilEj66Ik.d.cts → runAgent-CKgcRSRH.d.cts} +4 -4
  56. package/dist/{runHandle-BNOqS-Bl.d.cts → runHandle-DH4VmQx6.d.cts} +4 -2
  57. package/dist/{runHandle-BdLXOFqF.d.ts → runHandle-o3p06Nva.d.ts} +4 -2
  58. package/dist/runtime-kernel/events.cjs +201 -1
  59. package/dist/runtime-kernel/events.cjs.map +1 -1
  60. package/dist/runtime-kernel/events.d.cts +3 -3
  61. package/dist/runtime-kernel/events.d.ts +3 -3
  62. package/dist/runtime-kernel/events.js +201 -1
  63. package/dist/runtime-kernel/events.js.map +1 -1
  64. package/dist/runtime-kernel.cjs +824 -264
  65. package/dist/runtime-kernel.cjs.map +1 -1
  66. package/dist/runtime-kernel.d.cts +14 -12
  67. package/dist/runtime-kernel.d.ts +14 -12
  68. package/dist/runtime-kernel.js +816 -265
  69. package/dist/runtime-kernel.js.map +1 -1
  70. package/dist/testkit.cjs +847 -264
  71. package/dist/testkit.cjs.map +1 -1
  72. package/dist/testkit.d.cts +14 -12
  73. package/dist/testkit.d.ts +14 -12
  74. package/dist/testkit.js +847 -264
  75. package/dist/testkit.js.map +1 -1
  76. package/dist/{agentSpec-Du4Iye0q.d.cts → token-accounting-CwpSPg4y.d.cts} +1265 -1
  77. package/dist/{agentSpec-Du4Iye0q.d.ts → token-accounting-CwpSPg4y.d.ts} +1265 -1
  78. package/dist/token-calibration-Cxv6dDYF.d.cts +327 -0
  79. package/dist/token-calibration-Cxv6dDYF.d.ts +327 -0
  80. package/dist/token-counter-CFfP8PQb.d.cts +26 -0
  81. package/dist/token-counter-SFyyTeaG.d.ts +26 -0
  82. package/dist/token-usage-CqwT7-5K.d.cts +133 -0
  83. package/dist/token-usage-CqwT7-5K.d.ts +133 -0
  84. package/dist/{tokenizer-DFL4I7-I.d.ts → tokenizer-BFJNCHFX.d.ts} +1 -1
  85. package/dist/{tokenizer-DH_JXv-H.d.cts → tokenizer-DoGsBE3A.d.cts} +1 -1
  86. package/docs/README.md +1 -1
  87. package/docs/integration/README.md +2 -2
  88. package/docs/integration/agent-registration-guide.md +2 -0
  89. package/docs/integration/context-engineering.md +53 -1
  90. package/docs/integration/context-fences.md +12 -0
  91. package/package.json +2 -2
@@ -1,6 +1,8 @@
1
- import { A as AiMessage } from './messages-XthmnHZ3.cjs';
2
- import { d as AgentSpecContextTracePolicy, g as AgentSpecContextPolicy } from './agentSpec-Du4Iye0q.cjs';
1
+ import { A as AiMessage } from './messages-Br5SiLD3.cjs';
2
+ import { d as AgentSpecContextTracePolicy, g as AgentSpecContextPolicy, C as ContextTokenComponent } from './token-accounting-CwpSPg4y.cjs';
3
+ import { T as TokenUsageCalibrationTrace } from './token-calibration-Cxv6dDYF.cjs';
3
4
  import { R as RuntimeEvent } from './todo-Ca8llpRQ.cjs';
5
+ import { T as TokenRoute, b as TokenCountSource, a as TokenCountConfidence } from './token-usage-CqwT7-5K.cjs';
4
6
 
5
7
  type MessageType = AiMessage['type'];
6
8
  type MessageRole = AiMessage['role'];
@@ -63,11 +65,25 @@ interface GenerateResponse {
63
65
  generatedText: string;
64
66
  }
65
67
 
68
+ interface RemoteTokenCountTrace {
69
+ enabled: boolean;
70
+ attempted: boolean;
71
+ applied: boolean;
72
+ route?: TokenRoute;
73
+ inputTokens?: number;
74
+ localEstimateTokens?: number;
75
+ deltaTokens?: number;
76
+ source?: TokenCountSource;
77
+ confidence?: TokenCountConfidence;
78
+ failureBehavior?: 'use-local-estimate' | 'fail-fast';
79
+ failureReason?: string;
80
+ }
66
81
  interface MessageProcessingState {
67
82
  message: AiMessage;
68
83
  originalIndex: number;
69
84
  action: 'keep_core' | 'keep_working_memory' | 'summarize' | 'skip';
70
85
  tokens: number;
86
+ tokenCalibration?: TokenUsageCalibrationTrace;
71
87
  processedContent?: string;
72
88
  contentType?: 'full' | 'final_answer_only' | 'thinking_only';
73
89
  phase?: string;
@@ -88,6 +104,11 @@ interface ProviderContext<TConfig = unknown> {
88
104
  config: TConfig;
89
105
  debugMode: boolean;
90
106
  estimateTokens: (message: AiMessage) => number;
107
+ estimateTokensWithTrace?: (message: AiMessage) => {
108
+ tokens: number;
109
+ tokenCalibration?: TokenUsageCalibrationTrace;
110
+ };
111
+ remoteTokenCount?: RemoteTokenCountTrace;
91
112
  summarizationCallbacks?: SummarizationCallbacks;
92
113
  /**
93
114
  * Host 注入的注册式 AI 调用入口。
@@ -181,6 +202,7 @@ interface ContextTraceMessageDecisionEvent {
181
202
  kept: boolean;
182
203
  reason: string;
183
204
  replacementSourceIds?: string[];
205
+ tokenCalibration?: TokenUsageCalibrationTrace;
184
206
  }
185
207
  interface ContextTrace {
186
208
  enabled: true;
@@ -193,6 +215,10 @@ interface ContextTrace {
193
215
  originalCount: number;
194
216
  finalCount: number;
195
217
  finalTokens: number;
218
+ tokenLedgerEntryIds?: string[];
219
+ tokenComponents?: ContextTokenComponent[];
220
+ tokenCalibration?: TokenUsageCalibrationTrace;
221
+ remoteTokenCount?: RemoteTokenCountTrace;
196
222
  truncated: boolean;
197
223
  events: ContextTraceEvent[];
198
224
  }
@@ -201,6 +227,8 @@ interface ContextTraceCollectorOptions {
201
227
  effectivePolicy?: AgentSpecContextPolicy;
202
228
  totalBudget: number;
203
229
  originalCount: number;
230
+ tokenCalibration?: TokenUsageCalibrationTrace;
231
+ remoteTokenCount?: RemoteTokenCountTrace;
204
232
  }
205
233
  interface ProviderTraceInput {
206
234
  providerName: string;
@@ -226,14 +254,19 @@ declare class ContextTraceCollector {
226
254
  private readonly effectivePolicy?;
227
255
  private readonly totalBudget;
228
256
  private readonly originalCount;
257
+ private readonly tokenCalibration?;
258
+ private remoteTokenCount?;
259
+ private tokenComponents?;
229
260
  private readonly events;
230
261
  private overflowed;
231
262
  private constructor();
232
263
  static create(options: ContextTraceCollectorOptions): ContextTraceCollector | undefined;
233
264
  recordProvider(input: ProviderTraceInput): void;
234
265
  recordMessageDecisions(states: ReadonlyArray<MessageProcessingState>): void;
266
+ recordRemoteTokenCount(trace: RemoteTokenCountTrace): void;
267
+ recordTokenComponents(components: ReadonlyArray<ContextTokenComponent>): void;
235
268
  build(finalMessages: ReadonlyArray<AiMessage>, finalTokens: number): ContextTrace;
236
269
  private push;
237
270
  }
238
271
 
239
- export { BaseContextProvider as B, type ChatMessage as C, type GenerateRequest as G, type IContextProvider as I, type MessageProcessingState as M, type ProviderContext as P, type RecentRejection as R, type SummarizationCallbacks as S, TOOL_HISTORY_OVERFLOW_ERROR_CODE as T, type ProviderResult as a, ContextTraceCollector as b, type GenerateResponse as c, type ContextTrace as d, type ChatDocumentMetadata as e, type ChatProjectMetadata as f, type ChatUserQuote as g, ContextProviderError as h, type ContextProviderErrorCode as i, type ContextProviderErrorOptions as j, type ContextTraceEvent as k, type ContextTraceMessageDecisionEvent as l, type ContextTraceProviderEvent as m, type MessageRole as n, type MessageType as o, SUMMARIZATION_FAILED_ERROR_CODE as p, isContextProviderError as q };
272
+ export { BaseContextProvider as B, type ChatMessage as C, type GenerateRequest as G, type IContextProvider as I, type MessageProcessingState as M, type ProviderContext as P, type RemoteTokenCountTrace as R, type SummarizationCallbacks as S, TOOL_HISTORY_OVERFLOW_ERROR_CODE as T, type ProviderResult as a, ContextTraceCollector as b, type GenerateResponse as c, type ContextTrace as d, type ChatDocumentMetadata as e, type ChatProjectMetadata as f, type ChatUserQuote as g, ContextProviderError as h, type ContextProviderErrorCode as i, type ContextProviderErrorOptions as j, type ContextTraceEvent as k, type ContextTraceMessageDecisionEvent as l, type ContextTraceProviderEvent as m, type MessageRole as n, type MessageType as o, type RecentRejection as p, SUMMARIZATION_FAILED_ERROR_CODE as q, isContextProviderError as r };
@@ -1,6 +1,8 @@
1
- import { A as AiMessage } from './messages-XthmnHZ3.js';
2
- import { d as AgentSpecContextTracePolicy, g as AgentSpecContextPolicy } from './agentSpec-Du4Iye0q.js';
1
+ import { A as AiMessage } from './messages-Br5SiLD3.js';
2
+ import { d as AgentSpecContextTracePolicy, g as AgentSpecContextPolicy, C as ContextTokenComponent } from './token-accounting-CwpSPg4y.js';
3
+ import { T as TokenUsageCalibrationTrace } from './token-calibration-Cxv6dDYF.js';
3
4
  import { R as RuntimeEvent } from './todo-Ca8llpRQ.js';
5
+ import { T as TokenRoute, b as TokenCountSource, a as TokenCountConfidence } from './token-usage-CqwT7-5K.js';
4
6
 
5
7
  type MessageType = AiMessage['type'];
6
8
  type MessageRole = AiMessage['role'];
@@ -63,11 +65,25 @@ interface GenerateResponse {
63
65
  generatedText: string;
64
66
  }
65
67
 
68
+ interface RemoteTokenCountTrace {
69
+ enabled: boolean;
70
+ attempted: boolean;
71
+ applied: boolean;
72
+ route?: TokenRoute;
73
+ inputTokens?: number;
74
+ localEstimateTokens?: number;
75
+ deltaTokens?: number;
76
+ source?: TokenCountSource;
77
+ confidence?: TokenCountConfidence;
78
+ failureBehavior?: 'use-local-estimate' | 'fail-fast';
79
+ failureReason?: string;
80
+ }
66
81
  interface MessageProcessingState {
67
82
  message: AiMessage;
68
83
  originalIndex: number;
69
84
  action: 'keep_core' | 'keep_working_memory' | 'summarize' | 'skip';
70
85
  tokens: number;
86
+ tokenCalibration?: TokenUsageCalibrationTrace;
71
87
  processedContent?: string;
72
88
  contentType?: 'full' | 'final_answer_only' | 'thinking_only';
73
89
  phase?: string;
@@ -88,6 +104,11 @@ interface ProviderContext<TConfig = unknown> {
88
104
  config: TConfig;
89
105
  debugMode: boolean;
90
106
  estimateTokens: (message: AiMessage) => number;
107
+ estimateTokensWithTrace?: (message: AiMessage) => {
108
+ tokens: number;
109
+ tokenCalibration?: TokenUsageCalibrationTrace;
110
+ };
111
+ remoteTokenCount?: RemoteTokenCountTrace;
91
112
  summarizationCallbacks?: SummarizationCallbacks;
92
113
  /**
93
114
  * Host 注入的注册式 AI 调用入口。
@@ -181,6 +202,7 @@ interface ContextTraceMessageDecisionEvent {
181
202
  kept: boolean;
182
203
  reason: string;
183
204
  replacementSourceIds?: string[];
205
+ tokenCalibration?: TokenUsageCalibrationTrace;
184
206
  }
185
207
  interface ContextTrace {
186
208
  enabled: true;
@@ -193,6 +215,10 @@ interface ContextTrace {
193
215
  originalCount: number;
194
216
  finalCount: number;
195
217
  finalTokens: number;
218
+ tokenLedgerEntryIds?: string[];
219
+ tokenComponents?: ContextTokenComponent[];
220
+ tokenCalibration?: TokenUsageCalibrationTrace;
221
+ remoteTokenCount?: RemoteTokenCountTrace;
196
222
  truncated: boolean;
197
223
  events: ContextTraceEvent[];
198
224
  }
@@ -201,6 +227,8 @@ interface ContextTraceCollectorOptions {
201
227
  effectivePolicy?: AgentSpecContextPolicy;
202
228
  totalBudget: number;
203
229
  originalCount: number;
230
+ tokenCalibration?: TokenUsageCalibrationTrace;
231
+ remoteTokenCount?: RemoteTokenCountTrace;
204
232
  }
205
233
  interface ProviderTraceInput {
206
234
  providerName: string;
@@ -226,14 +254,19 @@ declare class ContextTraceCollector {
226
254
  private readonly effectivePolicy?;
227
255
  private readonly totalBudget;
228
256
  private readonly originalCount;
257
+ private readonly tokenCalibration?;
258
+ private remoteTokenCount?;
259
+ private tokenComponents?;
229
260
  private readonly events;
230
261
  private overflowed;
231
262
  private constructor();
232
263
  static create(options: ContextTraceCollectorOptions): ContextTraceCollector | undefined;
233
264
  recordProvider(input: ProviderTraceInput): void;
234
265
  recordMessageDecisions(states: ReadonlyArray<MessageProcessingState>): void;
266
+ recordRemoteTokenCount(trace: RemoteTokenCountTrace): void;
267
+ recordTokenComponents(components: ReadonlyArray<ContextTokenComponent>): void;
235
268
  build(finalMessages: ReadonlyArray<AiMessage>, finalTokens: number): ContextTrace;
236
269
  private push;
237
270
  }
238
271
 
239
- export { BaseContextProvider as B, type ChatMessage as C, type GenerateRequest as G, type IContextProvider as I, type MessageProcessingState as M, type ProviderContext as P, type RecentRejection as R, type SummarizationCallbacks as S, TOOL_HISTORY_OVERFLOW_ERROR_CODE as T, type ProviderResult as a, ContextTraceCollector as b, type GenerateResponse as c, type ContextTrace as d, type ChatDocumentMetadata as e, type ChatProjectMetadata as f, type ChatUserQuote as g, ContextProviderError as h, type ContextProviderErrorCode as i, type ContextProviderErrorOptions as j, type ContextTraceEvent as k, type ContextTraceMessageDecisionEvent as l, type ContextTraceProviderEvent as m, type MessageRole as n, type MessageType as o, SUMMARIZATION_FAILED_ERROR_CODE as p, isContextProviderError as q };
272
+ export { BaseContextProvider as B, type ChatMessage as C, type GenerateRequest as G, type IContextProvider as I, type MessageProcessingState as M, type ProviderContext as P, type RemoteTokenCountTrace as R, type SummarizationCallbacks as S, TOOL_HISTORY_OVERFLOW_ERROR_CODE as T, type ProviderResult as a, ContextTraceCollector as b, type GenerateResponse as c, type ContextTrace as d, type ChatDocumentMetadata as e, type ChatProjectMetadata as f, type ChatUserQuote as g, ContextProviderError as h, type ContextProviderErrorCode as i, type ContextProviderErrorOptions as j, type ContextTraceEvent as k, type ContextTraceMessageDecisionEvent as l, type ContextTraceProviderEvent as m, type MessageRole as n, type MessageType as o, type RecentRejection as p, SUMMARIZATION_FAILED_ERROR_CODE as q, isContextProviderError as r };
@@ -35,6 +35,12 @@ var ToolOutputMeta = zod.z.object({
35
35
  args: zod.z.record(zod.z.unknown()).optional(),
36
36
  tool_call_id: zod.z.string()
37
37
  });
38
+ var ObservationTruncationMeta = zod.z.object({
39
+ originalChars: zod.z.number().int().nonnegative(),
40
+ previewChars: zod.z.number().int().nonnegative(),
41
+ originalLines: zod.z.number().int().nonnegative().optional(),
42
+ previewLines: zod.z.number().int().nonnegative().optional()
43
+ });
38
44
  var ImageInfoMeta = zod.z.object({
39
45
  url: zod.z.string().optional(),
40
46
  width: zod.z.number().int().positive().optional(),
@@ -66,6 +72,7 @@ var PersistentMetadata = zod.z.object({
66
72
  tool_name: zod.z.string().optional(),
67
73
  args: zod.z.record(zod.z.unknown()).optional(),
68
74
  tool_call_id: zod.z.string().optional(),
75
+ observationTruncation: ObservationTruncationMeta.optional(),
69
76
  raw_output: zod.z.string().optional(),
70
77
  image_info: ImageInfoMeta.optional(),
71
78
  taskType: zod.z.string().optional(),
@@ -290,7 +297,17 @@ var AgentSpecReasoningRetentionPolicy = zod.z.object({
290
297
  var AgentSpecTokenEstimationPolicy = zod.z.object({
291
298
  encoding: zod.z.string().min(1).optional(),
292
299
  avgCharsPerToken: zod.z.number().positive().optional(),
293
- toolCallOverhead: zod.z.number().int().nonnegative().optional()
300
+ toolCallOverhead: zod.z.number().int().nonnegative().optional(),
301
+ calibration: zod.z.object({
302
+ enabled: zod.z.boolean().optional(),
303
+ minSamples: zod.z.number().int().positive().optional(),
304
+ minCoefficient: zod.z.number().positive().optional(),
305
+ maxCoefficient: zod.z.number().positive().optional()
306
+ }).optional(),
307
+ remoteCount: zod.z.object({
308
+ enabled: zod.z.boolean().optional(),
309
+ failureBehavior: zod.z.enum(["use-local-estimate", "fail-fast"]).optional()
310
+ }).optional()
294
311
  });
295
312
  var SYSTEM_REMINDER_BUILTIN_TRIGGER_KINDS = [
296
313
  "phase-equals",
@@ -1212,6 +1229,189 @@ var createSSEHistorySummaryEvent = (id, conversationId, turnId, summaryId, conte
1212
1229
  summary_seq: summarySeq,
1213
1230
  ...options
1214
1231
  });
1232
+ var TokenCountSource = zod.z.enum([
1233
+ "local-estimate",
1234
+ "provider-preflight-count",
1235
+ "provider-response-usage",
1236
+ "host-supplied",
1237
+ "test-fixture"
1238
+ ]);
1239
+ var TokenCountConfidence = zod.z.enum([
1240
+ "estimate",
1241
+ "provider-estimate",
1242
+ "actual"
1243
+ ]);
1244
+ var TokenRouteCapabilities = zod.z.object({
1245
+ supportsRemoteTokenCount: zod.z.boolean().optional(),
1246
+ supportsResponseUsage: zod.z.boolean().optional(),
1247
+ supportsImageInput: zod.z.boolean().optional(),
1248
+ supportsCachedInputBilling: zod.z.boolean().optional(),
1249
+ supportsReasoningTokens: zod.z.boolean().optional()
1250
+ });
1251
+ var TokenRoute = zod.z.object({
1252
+ providerId: zod.z.string().describe("host \u4FA7 provider \u6807\u8BC6\uFF1Aopenai / anthropic / openrouter / siliconflow / zai ..."),
1253
+ baseURL: zod.z.string().optional().describe("\u4E2D\u8F6C\u5E73\u53F0\u6216\u79C1\u6709 endpoint\uFF1B\u7528\u4E8E\u533A\u5206\u540C\u540D\u6A21\u578B\u7684\u4E0D\u540C route"),
1254
+ modelId: zod.z.string().describe("host \u4FA7\u6A21\u578B id"),
1255
+ providerModelId: zod.z.string().optional().describe("provider \u5B9E\u9645\u6A21\u578B\u540D\uFF08\u4E0E host modelId \u53EF\u80FD\u4E0D\u540C\uFF09"),
1256
+ capabilities: TokenRouteCapabilities.optional()
1257
+ });
1258
+ var tokenCount = zod.z.number().int().nonnegative();
1259
+ var CanonicalLlmUsage = zod.z.object({
1260
+ inputTokens: tokenCount.describe("\u975E\u7F13\u5B58\u666E\u901A\u8F93\u5165 token"),
1261
+ outputTokens: tokenCount.describe("\u8F93\u51FA token"),
1262
+ reasoningTokens: tokenCount.optional().describe("\u63A8\u7406 token\uFF1Bundefined=\u672A\u5355\u5217\uFF0C0=\u62A5\u544A\u4E3A 0"),
1263
+ cacheReadTokens: tokenCount.optional().describe("\u7F13\u5B58\u547D\u4E2D\u8BFB\u53D6 token"),
1264
+ cacheWriteTokens: tokenCount.optional().describe("\u7F13\u5B58\u5199\u5165 token"),
1265
+ totalTokens: tokenCount.optional().describe("provider \u62A5\u544A\u7684\u603B\u91CF\uFF1B\u53EF\u7F3A\u5931\uFF0C\u4E14\u4E0D\u4FDD\u8BC1\u7B49\u4E8E\u5404\u9879\u4E4B\u548C"),
1266
+ source: TokenCountSource,
1267
+ confidence: TokenCountConfidence,
1268
+ rawUsage: zod.z.unknown().optional().describe("\u539F\u59CB provider usage\uFF0C\u4EC5\u4F9B\u5BA1\u8BA1")
1269
+ });
1270
+ var tokenCount2 = zod.z.number().int().nonnegative();
1271
+ var tokenPrice = zod.z.number().finite().nonnegative();
1272
+ var TokenLedgerEntryKind = zod.z.enum([
1273
+ "llm-usage",
1274
+ "context-component"
1275
+ ]);
1276
+ var ContextTokenComponentKind = zod.z.enum([
1277
+ "system",
1278
+ "user",
1279
+ "assistant",
1280
+ "tool",
1281
+ "context-injection",
1282
+ "history-summary",
1283
+ "working-memory",
1284
+ "fence",
1285
+ "system-reminder",
1286
+ "other"
1287
+ ]);
1288
+ var TokenUsageTotals = zod.z.object({
1289
+ inputTokens: tokenCount2,
1290
+ outputTokens: tokenCount2,
1291
+ reasoningTokens: tokenCount2.optional(),
1292
+ cacheReadTokens: tokenCount2.optional(),
1293
+ cacheWriteTokens: tokenCount2.optional(),
1294
+ totalTokens: tokenCount2.optional(),
1295
+ usageCount: zod.z.number().int().nonnegative()
1296
+ });
1297
+ var ContextTokenComponent = zod.z.object({
1298
+ componentId: zod.z.string().min(1),
1299
+ kind: ContextTokenComponentKind,
1300
+ tokens: tokenCount2,
1301
+ source: TokenCountSource,
1302
+ confidence: TokenCountConfidence,
1303
+ label: zod.z.string().optional(),
1304
+ messageId: zod.z.string().optional(),
1305
+ role: zod.z.string().optional(),
1306
+ action: zod.z.string().optional(),
1307
+ kept: zod.z.boolean().optional(),
1308
+ truncatedAtExecution: zod.z.boolean().optional(),
1309
+ originalTokensEstimate: tokenCount2.optional(),
1310
+ droppedTokensEstimate: tokenCount2.optional()
1311
+ });
1312
+ var TokenLedgerEntryBase = zod.z.object({
1313
+ id: zod.z.string().min(1),
1314
+ runId: zod.z.string().optional(),
1315
+ parentRunId: zod.z.string().optional(),
1316
+ conversationId: zod.z.string().optional(),
1317
+ turnId: zod.z.string().optional(),
1318
+ stepId: zod.z.string().optional(),
1319
+ route: TokenRoute.optional(),
1320
+ createdAt: zod.z.number().int().nonnegative().optional()
1321
+ });
1322
+ var LlmUsageTokenLedgerEntry = TokenLedgerEntryBase.extend({
1323
+ kind: zod.z.literal("llm-usage"),
1324
+ modelId: zod.z.string().optional(),
1325
+ usage: CanonicalLlmUsage
1326
+ });
1327
+ var ContextComponentTokenLedgerEntry = TokenLedgerEntryBase.extend({
1328
+ kind: zod.z.literal("context-component"),
1329
+ components: zod.z.array(ContextTokenComponent),
1330
+ totalTokens: tokenCount2
1331
+ });
1332
+ var TokenLedgerEntry = zod.z.discriminatedUnion("kind", [
1333
+ LlmUsageTokenLedgerEntry,
1334
+ ContextComponentTokenLedgerEntry
1335
+ ]);
1336
+ var TokenLedgerAggregate = zod.z.object({
1337
+ llmUsage: TokenUsageTotals.optional(),
1338
+ contextTokens: tokenCount2,
1339
+ entryCount: zod.z.number().int().nonnegative(),
1340
+ llmCallCount: zod.z.number().int().nonnegative(),
1341
+ contextComponentCount: zod.z.number().int().nonnegative()
1342
+ });
1343
+ var RunTokenUsageAggregate = zod.z.object({
1344
+ /**
1345
+ * 父 run 自己产生的 usage;子 run 必须放到 children,避免展示或计费时重复相加。
1346
+ */
1347
+ own: TokenLedgerAggregate,
1348
+ children: TokenLedgerAggregate.optional()
1349
+ });
1350
+ var TokenCostComponent = zod.z.enum([
1351
+ "input",
1352
+ "output",
1353
+ "reasoning",
1354
+ "cacheRead",
1355
+ "cacheWrite"
1356
+ ]);
1357
+ var TokenPricing = zod.z.object({
1358
+ currency: zod.z.string().min(1),
1359
+ unit: zod.z.literal("per_1m_tokens"),
1360
+ input: tokenPrice.optional(),
1361
+ output: tokenPrice.optional(),
1362
+ reasoning: tokenPrice.optional(),
1363
+ cacheRead: tokenPrice.optional(),
1364
+ cacheWrite: tokenPrice.optional()
1365
+ });
1366
+ var CostBreakdown = zod.z.object({
1367
+ status: zod.z.enum(["computed", "unknown"]),
1368
+ currency: zod.z.string().optional(),
1369
+ inputCost: tokenPrice.optional(),
1370
+ outputCost: tokenPrice.optional(),
1371
+ reasoningCost: tokenPrice.optional(),
1372
+ cacheReadCost: tokenPrice.optional(),
1373
+ cacheWriteCost: tokenPrice.optional(),
1374
+ totalCost: tokenPrice.optional(),
1375
+ missingPriceComponents: zod.z.array(TokenCostComponent).optional()
1376
+ });
1377
+ var tokenCount3 = zod.z.number().int().nonnegative();
1378
+ var actualCalibrationSource = zod.z.enum([
1379
+ "provider-response-usage",
1380
+ "host-supplied",
1381
+ "test-fixture"
1382
+ ]);
1383
+ var TokenUsageCalibrationSample = zod.z.object({
1384
+ route: TokenRoute,
1385
+ localEstimateTokens: tokenCount3,
1386
+ actualInputTokens: tokenCount3,
1387
+ source: actualCalibrationSource,
1388
+ confidence: zod.z.literal("actual"),
1389
+ observedAt: zod.z.number().int().nonnegative().optional(),
1390
+ runId: zod.z.string().optional(),
1391
+ ledgerEntryId: zod.z.string().optional()
1392
+ });
1393
+ var TokenUsageCalibrationTrace = zod.z.object({
1394
+ enabled: zod.z.boolean(),
1395
+ applied: zod.z.boolean(),
1396
+ route: TokenRoute.optional(),
1397
+ sampleCount: zod.z.number().int().nonnegative(),
1398
+ coefficient: zod.z.number().positive().optional(),
1399
+ minSamples: zod.z.number().int().positive().optional(),
1400
+ minCoefficient: zod.z.number().positive().optional(),
1401
+ maxCoefficient: zod.z.number().positive().optional(),
1402
+ localEstimateTokens: tokenCount3.optional(),
1403
+ calibratedEstimateTokens: tokenCount3.optional(),
1404
+ deltaTokens: zod.z.number().int().optional(),
1405
+ sampleLedgerEntryIds: zod.z.array(zod.z.string()).optional()
1406
+ });
1407
+ var ContextBuildTokenEstimate = zod.z.object({
1408
+ route: TokenRoute.optional(),
1409
+ localEstimateTokens: tokenCount3,
1410
+ calibratedEstimateTokens: tokenCount3,
1411
+ finalTokens: tokenCount3,
1412
+ source: zod.z.literal("local-estimate"),
1413
+ confidence: zod.z.literal("estimate")
1414
+ });
1215
1415
 
1216
1416
  exports.AUDIT_ACTIONS = AUDIT_ACTIONS;
1217
1417
  exports.AUDIT_DECISION_OUTCOMES = AUDIT_DECISION_OUTCOMES;
@@ -1249,14 +1449,23 @@ exports.AuditEvidence = AuditEvidence;
1249
1449
  exports.AuditScope = AuditScope;
1250
1450
  exports.BaseEvent = BaseEvent;
1251
1451
  exports.BaseSSEEvent = BaseSSEEvent;
1452
+ exports.CanonicalLlmUsage = CanonicalLlmUsage;
1453
+ exports.ContextBuildTokenEstimate = ContextBuildTokenEstimate;
1454
+ exports.ContextComponentTokenLedgerEntry = ContextComponentTokenLedgerEntry;
1455
+ exports.ContextTokenComponent = ContextTokenComponent;
1456
+ exports.ContextTokenComponentKind = ContextTokenComponentKind;
1457
+ exports.CostBreakdown = CostBreakdown;
1252
1458
  exports.DEFAULT_MAX_STEPS = DEFAULT_MAX_STEPS;
1253
1459
  exports.EventEnvelope = EventEnvelope;
1254
1460
  exports.ExecutionTraceContext = ExecutionTraceContext;
1255
1461
  exports.HistorySummaryMeta = HistorySummaryMeta;
1256
1462
  exports.ImageInfoMeta = ImageInfoMeta;
1463
+ exports.LlmUsageTokenLedgerEntry = LlmUsageTokenLedgerEntry;
1464
+ exports.ObservationTruncationMeta = ObservationTruncationMeta;
1257
1465
  exports.PersistentMetadata = PersistentMetadata;
1258
1466
  exports.ProviderReasoningDetails = ProviderReasoningDetails;
1259
1467
  exports.ProviderReasoningDetailsPayload = ProviderReasoningDetailsPayload;
1468
+ exports.RunTokenUsageAggregate = RunTokenUsageAggregate;
1260
1469
  exports.RuntimeEvent = RuntimeEvent;
1261
1470
  exports.SSEErrorEvent = SSEErrorEvent;
1262
1471
  exports.SSEEvent = SSEEvent;
@@ -1279,6 +1488,18 @@ exports.SYSTEM_REMINDER_BUILTIN_TRIGGER_KINDS = SYSTEM_REMINDER_BUILTIN_TRIGGER_
1279
1488
  exports.Status = Status;
1280
1489
  exports.SystemMessage = SystemMessage;
1281
1490
  exports.TaskTrackingMeta = TaskTrackingMeta;
1491
+ exports.TokenCostComponent = TokenCostComponent;
1492
+ exports.TokenCountConfidence = TokenCountConfidence;
1493
+ exports.TokenCountSource = TokenCountSource;
1494
+ exports.TokenLedgerAggregate = TokenLedgerAggregate;
1495
+ exports.TokenLedgerEntry = TokenLedgerEntry;
1496
+ exports.TokenLedgerEntryKind = TokenLedgerEntryKind;
1497
+ exports.TokenPricing = TokenPricing;
1498
+ exports.TokenRoute = TokenRoute;
1499
+ exports.TokenRouteCapabilities = TokenRouteCapabilities;
1500
+ exports.TokenUsageCalibrationSample = TokenUsageCalibrationSample;
1501
+ exports.TokenUsageCalibrationTrace = TokenUsageCalibrationTrace;
1502
+ exports.TokenUsageTotals = TokenUsageTotals;
1282
1503
  exports.ToolBindingSpec = ToolBindingSpec;
1283
1504
  exports.ToolCallDecisionPayload = ToolCallDecisionPayload;
1284
1505
  exports.ToolCallExtraContent = ToolCallExtraContent;