@linnlabs/linnkit 0.9.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 (102) hide show
  1. package/CHANGELOG.md +168 -0
  2. package/bin/linnkit.cjs +7 -0
  3. package/dist/{agent-invocation-BHcNfrBV.d.cts → agent-invocation-CAlm9qOw.d.cts} +1 -1
  4. package/dist/{agent-invocation-BznDaXDs.d.ts → agent-invocation-DKoEDLkl.d.ts} +1 -1
  5. package/dist/{ai-engine.types-vZRnQcJa.d.ts → ai-engine.types-CeLoM624.d.cts} +9 -1
  6. package/dist/{ai-engine.types-BpeU_XQG.d.cts → ai-engine.types-LqmnfR2W.d.ts} +9 -1
  7. package/dist/{audit-CtcfART1.d.ts → audit-BuWHoMEr.d.ts} +4 -2
  8. package/dist/{audit-LeOrm2hX.d.cts → audit-DnzNQzwq.d.cts} +4 -2
  9. package/dist/cli.cjs +682 -258
  10. package/dist/cli.cjs.map +1 -1
  11. package/dist/cli.js +682 -258
  12. package/dist/cli.js.map +1 -1
  13. package/dist/context-manager.cjs +823 -57
  14. package/dist/context-manager.cjs.map +1 -1
  15. package/dist/context-manager.d.cts +132 -18
  16. package/dist/context-manager.d.ts +132 -18
  17. package/dist/context-manager.js +823 -58
  18. package/dist/context-manager.js.map +1 -1
  19. package/dist/{context-trace-HE2qY5Q-.d.cts → context-trace-DrtvKac4.d.cts} +37 -4
  20. package/dist/{context-trace-DRi5M4lX.d.ts → context-trace-WV1HAZeB.d.ts} +37 -4
  21. package/dist/contracts.cjs +225 -2
  22. package/dist/contracts.cjs.map +1 -1
  23. package/dist/contracts.d.cts +6 -4
  24. package/dist/contracts.d.ts +6 -4
  25. package/dist/contracts.js +205 -3
  26. package/dist/contracts.js.map +1 -1
  27. package/dist/{defaultGraphExecutor-BIjJj7WF.d.cts → defaultGraphExecutor-L1ri3oij.d.cts} +48 -19
  28. package/dist/{defaultGraphExecutor-BBswR8wn.d.ts → defaultGraphExecutor-QzzdudLQ.d.ts} +48 -19
  29. package/dist/{index-BanRABEt.d.cts → index-BeQO0rx2.d.cts} +345 -268
  30. package/dist/{index-Cvr23YCl.d.cts → index-CDofGr_4.d.ts} +24 -6
  31. package/dist/{index-Dl5PLgAv.d.cts → index-Crl5E4eF.d.cts} +27 -4
  32. package/dist/{index-CJeWHopy.d.ts → index-DWGKvssS.d.ts} +3 -3
  33. package/dist/{index-CHqwkvGp.d.ts → index-X5lYU1Lj.d.ts} +27 -4
  34. package/dist/{index-DDzuSb0n.d.ts → index-uCNCpJBg.d.cts} +24 -6
  35. package/dist/{index-Z8NXKNwI.d.ts → index-wOZD-p2-.d.ts} +345 -268
  36. package/dist/{index-DO4dQgf2.d.cts → index-wXVAHZ0D.d.cts} +3 -3
  37. package/dist/index.cjs +1011 -329
  38. package/dist/index.cjs.map +1 -1
  39. package/dist/index.d.cts +19 -16
  40. package/dist/index.d.ts +19 -16
  41. package/dist/index.js +1011 -329
  42. package/dist/index.js.map +1 -1
  43. package/dist/{messages-XthmnHZ3.d.cts → messages-Br5SiLD3.d.cts} +706 -1
  44. package/dist/{messages-XthmnHZ3.d.ts → messages-Br5SiLD3.d.ts} +706 -1
  45. package/dist/{ports-DnLuKfpE.d.ts → ports-Dugizzje.d.cts} +6 -2
  46. package/dist/{ports-DaatKJXp.d.cts → ports-fhp84x_C.d.ts} +6 -2
  47. package/dist/ports.d.cts +8 -5
  48. package/dist/ports.d.ts +8 -5
  49. package/dist/quickstart.cjs +683 -258
  50. package/dist/quickstart.cjs.map +1 -1
  51. package/dist/quickstart.d.cts +11 -10
  52. package/dist/quickstart.d.ts +11 -10
  53. package/dist/quickstart.js +683 -258
  54. package/dist/quickstart.js.map +1 -1
  55. package/dist/{runAgent-CPj_9e58.d.ts → runAgent-BmMSju8I.d.ts} +6 -6
  56. package/dist/{runAgent-HYKlXbVr.d.cts → runAgent-CKgcRSRH.d.cts} +6 -6
  57. package/dist/{runHandle-D3gPsD7B.d.cts → runHandle-DH4VmQx6.d.cts} +6 -4
  58. package/dist/{runHandle-CyXvzgzk.d.ts → runHandle-o3p06Nva.d.ts} +6 -4
  59. package/dist/runtime-kernel/events.cjs +202 -1
  60. package/dist/runtime-kernel/events.cjs.map +1 -1
  61. package/dist/runtime-kernel/events.d.cts +5 -5
  62. package/dist/runtime-kernel/events.d.ts +5 -5
  63. package/dist/runtime-kernel/events.js +202 -1
  64. package/dist/runtime-kernel/events.js.map +1 -1
  65. package/dist/runtime-kernel.cjs +999 -340
  66. package/dist/runtime-kernel.cjs.map +1 -1
  67. package/dist/runtime-kernel.d.cts +16 -14
  68. package/dist/runtime-kernel.d.ts +16 -14
  69. package/dist/runtime-kernel.js +990 -341
  70. package/dist/runtime-kernel.js.map +1 -1
  71. package/dist/testkit.cjs +1003 -321
  72. package/dist/testkit.cjs.map +1 -1
  73. package/dist/testkit.d.cts +16 -14
  74. package/dist/testkit.d.ts +16 -14
  75. package/dist/testkit.js +1003 -321
  76. package/dist/testkit.js.map +1 -1
  77. package/dist/{todo-B1PmDlp3.d.cts → todo-Ca8llpRQ.d.cts} +1 -1
  78. package/dist/{todo-B1PmDlp3.d.ts → todo-Ca8llpRQ.d.ts} +1 -1
  79. package/dist/{agentSpec-EkmviZjy.d.cts → token-accounting-CwpSPg4y.d.cts} +1280 -1
  80. package/dist/{agentSpec-EkmviZjy.d.ts → token-accounting-CwpSPg4y.d.ts} +1280 -1
  81. package/dist/token-calibration-Cxv6dDYF.d.cts +327 -0
  82. package/dist/token-calibration-Cxv6dDYF.d.ts +327 -0
  83. package/dist/token-counter-CFfP8PQb.d.cts +26 -0
  84. package/dist/token-counter-SFyyTeaG.d.ts +26 -0
  85. package/dist/token-usage-CqwT7-5K.d.cts +133 -0
  86. package/dist/token-usage-CqwT7-5K.d.ts +133 -0
  87. package/dist/{tokenizer-DFL4I7-I.d.ts → tokenizer-BFJNCHFX.d.ts} +1 -1
  88. package/dist/{tokenizer-DH_JXv-H.d.cts → tokenizer-DoGsBE3A.d.cts} +1 -1
  89. package/dist/{toolContracts-CLkQmhTG.d.cts → toolContracts-Bm3EZ1UM.d.cts} +13 -2
  90. package/dist/{toolContracts-Blll0241.d.ts → toolContracts-f8lzZBNa.d.ts} +13 -2
  91. package/docs/README.md +1 -1
  92. package/docs/integration/README.md +3 -3
  93. package/docs/integration/agent-registration-guide.md +3 -1
  94. package/docs/integration/child-runs.md +4 -1
  95. package/docs/integration/context-engineering.md +83 -16
  96. package/docs/integration/context-fences.md +44 -3
  97. package/docs/integration/llm-provider.md +1 -1
  98. package/docs/integration/persistence.md +1 -0
  99. package/docs/integration/run-supervisor.md +3 -0
  100. package/docs/integration/tool-development-guide.md +7 -5
  101. package/docs/integration/tool-history.md +43 -17
  102. package/package.json +4 -3
@@ -1,6 +1,8 @@
1
- import { A as AiMessage } from './messages-XthmnHZ3.cjs';
2
- import { g as AgentSpecContextTracePolicy, e as AgentSpecContextPolicy } from './agentSpec-EkmviZjy.cjs';
3
- import { e as RuntimeEvent } from './todo-B1PmDlp3.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';
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 { g as AgentSpecContextTracePolicy, e as AgentSpecContextPolicy } from './agentSpec-EkmviZjy.js';
3
- import { e as RuntimeEvent } from './todo-B1PmDlp3.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';
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(),
@@ -237,6 +244,7 @@ var AgentSpecBudgetPolicy = zod.z.object({
237
244
  });
238
245
  var AgentSpecToolHistoryPolicy = zod.z.object({
239
246
  strategy: zod.z.enum(["per-pair", "per-run", "none"]).optional(),
247
+ retentionMode: zod.z.enum(["drop", "compress"]).optional(),
240
248
  keepLatestToolPairs: zod.z.number().int().nonnegative().optional(),
241
249
  keepLatestRuns: zod.z.number().int().nonnegative().optional(),
242
250
  maxInteractionGroups: zod.z.number().int().nonnegative().optional(),
@@ -289,7 +297,17 @@ var AgentSpecReasoningRetentionPolicy = zod.z.object({
289
297
  var AgentSpecTokenEstimationPolicy = zod.z.object({
290
298
  encoding: zod.z.string().min(1).optional(),
291
299
  avgCharsPerToken: zod.z.number().positive().optional(),
292
- 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()
293
311
  });
294
312
  var SYSTEM_REMINDER_BUILTIN_TRIGGER_KINDS = [
295
313
  "phase-equals",
@@ -359,12 +377,13 @@ var AgentSpecContextPolicy = zod.z.object({
359
377
  var DEFAULT_CONTEXT_POLICY = {
360
378
  profileId: "agent",
361
379
  budget: {
362
- maxTokens: 12e4,
380
+ maxTokens: 232e3,
363
381
  reservedForResponse: 2400,
364
382
  workingMemoryBudgetPercentage: 0.7
365
383
  },
366
384
  toolHistory: {
367
385
  strategy: "per-run",
386
+ retentionMode: "drop",
368
387
  keepLatestToolPairs: 2,
369
388
  keepLatestRuns: 1,
370
389
  maxInteractionGroups: 12,
@@ -1210,6 +1229,189 @@ var createSSEHistorySummaryEvent = (id, conversationId, turnId, summaryId, conte
1210
1229
  summary_seq: summarySeq,
1211
1230
  ...options
1212
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
+ });
1213
1415
 
1214
1416
  exports.AUDIT_ACTIONS = AUDIT_ACTIONS;
1215
1417
  exports.AUDIT_DECISION_OUTCOMES = AUDIT_DECISION_OUTCOMES;
@@ -1247,14 +1449,23 @@ exports.AuditEvidence = AuditEvidence;
1247
1449
  exports.AuditScope = AuditScope;
1248
1450
  exports.BaseEvent = BaseEvent;
1249
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;
1250
1458
  exports.DEFAULT_MAX_STEPS = DEFAULT_MAX_STEPS;
1251
1459
  exports.EventEnvelope = EventEnvelope;
1252
1460
  exports.ExecutionTraceContext = ExecutionTraceContext;
1253
1461
  exports.HistorySummaryMeta = HistorySummaryMeta;
1254
1462
  exports.ImageInfoMeta = ImageInfoMeta;
1463
+ exports.LlmUsageTokenLedgerEntry = LlmUsageTokenLedgerEntry;
1464
+ exports.ObservationTruncationMeta = ObservationTruncationMeta;
1255
1465
  exports.PersistentMetadata = PersistentMetadata;
1256
1466
  exports.ProviderReasoningDetails = ProviderReasoningDetails;
1257
1467
  exports.ProviderReasoningDetailsPayload = ProviderReasoningDetailsPayload;
1468
+ exports.RunTokenUsageAggregate = RunTokenUsageAggregate;
1258
1469
  exports.RuntimeEvent = RuntimeEvent;
1259
1470
  exports.SSEErrorEvent = SSEErrorEvent;
1260
1471
  exports.SSEEvent = SSEEvent;
@@ -1277,6 +1488,18 @@ exports.SYSTEM_REMINDER_BUILTIN_TRIGGER_KINDS = SYSTEM_REMINDER_BUILTIN_TRIGGER_
1277
1488
  exports.Status = Status;
1278
1489
  exports.SystemMessage = SystemMessage;
1279
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;
1280
1503
  exports.ToolBindingSpec = ToolBindingSpec;
1281
1504
  exports.ToolCallDecisionPayload = ToolCallDecisionPayload;
1282
1505
  exports.ToolCallExtraContent = ToolCallExtraContent;