@tansr/protocol 0.2.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 (47) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +74 -0
  3. package/dist/canonical/common.d.ts +37 -0
  4. package/dist/canonical/common.js +149 -0
  5. package/dist/canonical/digest.d.ts +12 -0
  6. package/dist/canonical/digest.js +58 -0
  7. package/dist/canonical/encode.d.ts +15 -0
  8. package/dist/canonical/encode.js +157 -0
  9. package/dist/canonical/errors.d.ts +81 -0
  10. package/dist/canonical/errors.js +89 -0
  11. package/dist/canonical/index.d.ts +21 -0
  12. package/dist/canonical/index.js +21 -0
  13. package/dist/canonical/parse.d.ts +21 -0
  14. package/dist/canonical/parse.js +295 -0
  15. package/dist/client.d.ts +42 -0
  16. package/dist/client.js +1 -0
  17. package/dist/config.d.ts +4990 -0
  18. package/dist/config.js +657 -0
  19. package/dist/daemon.d.ts +70 -0
  20. package/dist/daemon.js +48 -0
  21. package/dist/events.d.ts +2152 -0
  22. package/dist/events.js +625 -0
  23. package/dist/governance.d.ts +445 -0
  24. package/dist/governance.js +183 -0
  25. package/dist/headless.d.ts +97 -0
  26. package/dist/headless.js +39 -0
  27. package/dist/identity.d.ts +96 -0
  28. package/dist/identity.js +45 -0
  29. package/dist/index.d.ts +525 -0
  30. package/dist/index.js +529 -0
  31. package/dist/ir.d.ts +1418 -0
  32. package/dist/ir.js +283 -0
  33. package/dist/journal.d.ts +297 -0
  34. package/dist/journal.js +150 -0
  35. package/dist/media.d.ts +563 -0
  36. package/dist/media.js +190 -0
  37. package/dist/metering.d.ts +225 -0
  38. package/dist/metering.js +90 -0
  39. package/dist/orchestration.d.ts +1551 -0
  40. package/dist/orchestration.js +333 -0
  41. package/dist/redact-patterns.d.ts +49 -0
  42. package/dist/redact-patterns.js +141 -0
  43. package/dist/storage.d.ts +507 -0
  44. package/dist/storage.js +110 -0
  45. package/dist/tool-result-artifact.d.ts +257 -0
  46. package/dist/tool-result-artifact.js +263 -0
  47. package/package.json +30 -0
package/dist/ir.d.ts ADDED
@@ -0,0 +1,1418 @@
1
+ /**
2
+ * IR(Internal Representation)消息模型
3
+ *
4
+ * 双协议网关与内核共用的中间表示。设计要点:
5
+ * - 内容块 + 类型化流事件基底;
6
+ * - opaque thinking 密文透传对策:reasoning 块保留 provider 原样载荷;
7
+ * - visibility 显式化,不用难组合的布尔标志位。
8
+ *
9
+ * 铁律:providers 依赖本包类型,本包不得反向依赖 providers/kernel。
10
+ */
11
+ import { z } from 'zod';
12
+ /** 工具结果内容项(可多模态) */
13
+ export declare const IRToolContentSchema: z.ZodDiscriminatedUnion<"t", [z.ZodObject<{
14
+ t: z.ZodLiteral<"text">;
15
+ text: z.ZodString;
16
+ }, "strip", z.ZodTypeAny, {
17
+ t: "text";
18
+ text: string;
19
+ }, {
20
+ t: "text";
21
+ text: string;
22
+ }>, z.ZodObject<{
23
+ t: z.ZodLiteral<"image">;
24
+ mime: z.ZodString;
25
+ data: z.ZodString;
26
+ }, "strip", z.ZodTypeAny, {
27
+ t: "image";
28
+ mime: string;
29
+ data: string;
30
+ }, {
31
+ t: "image";
32
+ mime: string;
33
+ data: string;
34
+ }>]>;
35
+ /** 工具结果内容项:文本或图片(IRToolContentSchema 推导)。 */
36
+ export type IRToolContent = z.infer<typeof IRToolContentSchema>;
37
+ /**
38
+ * 内容块。`thinking` 携带可选 `signature`(Anthropic 多轮需原样回传);
39
+ * `opaque` 用于无法明文还原、必须整体透传的推理载荷(OpenAI Responses reasoning item、
40
+ * DeepSeek/Kimi 的 provider 私有块)——部分厂商要求原样回传 thinking 块,否则 400。
41
+ */
42
+ export declare const IRBlockSchema: z.ZodDiscriminatedUnion<"t", [z.ZodObject<{
43
+ t: z.ZodLiteral<"text">;
44
+ text: z.ZodString;
45
+ }, "strip", z.ZodTypeAny, {
46
+ t: "text";
47
+ text: string;
48
+ }, {
49
+ t: "text";
50
+ text: string;
51
+ }>, z.ZodObject<{
52
+ t: z.ZodLiteral<"thinking">;
53
+ text: z.ZodString;
54
+ signature: z.ZodOptional<z.ZodString>;
55
+ }, "strip", z.ZodTypeAny, {
56
+ t: "thinking";
57
+ text: string;
58
+ signature?: string | undefined;
59
+ }, {
60
+ t: "thinking";
61
+ text: string;
62
+ signature?: string | undefined;
63
+ }>, z.ZodObject<{
64
+ t: z.ZodLiteral<"opaque">;
65
+ /** 归属的 provider/协议,回传时据此判断能否透传 */
66
+ provider: z.ZodString;
67
+ /** provider 原样载荷,内核不得解读或改写 */
68
+ payload: z.ZodUnknown;
69
+ }, "strip", z.ZodTypeAny, {
70
+ t: "opaque";
71
+ provider: string;
72
+ payload?: unknown;
73
+ }, {
74
+ t: "opaque";
75
+ provider: string;
76
+ payload?: unknown;
77
+ }>, z.ZodObject<{
78
+ t: z.ZodLiteral<"tool_call">;
79
+ id: z.ZodString;
80
+ name: z.ZodString;
81
+ args: z.ZodUnknown;
82
+ }, "strip", z.ZodTypeAny, {
83
+ t: "tool_call";
84
+ id: string;
85
+ name: string;
86
+ args?: unknown;
87
+ }, {
88
+ t: "tool_call";
89
+ id: string;
90
+ name: string;
91
+ args?: unknown;
92
+ }>, z.ZodObject<{
93
+ t: z.ZodLiteral<"tool_result">;
94
+ callId: z.ZodString;
95
+ content: z.ZodArray<z.ZodDiscriminatedUnion<"t", [z.ZodObject<{
96
+ t: z.ZodLiteral<"text">;
97
+ text: z.ZodString;
98
+ }, "strip", z.ZodTypeAny, {
99
+ t: "text";
100
+ text: string;
101
+ }, {
102
+ t: "text";
103
+ text: string;
104
+ }>, z.ZodObject<{
105
+ t: z.ZodLiteral<"image">;
106
+ mime: z.ZodString;
107
+ data: z.ZodString;
108
+ }, "strip", z.ZodTypeAny, {
109
+ t: "image";
110
+ mime: string;
111
+ data: string;
112
+ }, {
113
+ t: "image";
114
+ mime: string;
115
+ data: string;
116
+ }>]>, "many">;
117
+ isError: z.ZodOptional<z.ZodBoolean>;
118
+ artifact: z.ZodOptional<z.ZodUnknown>;
119
+ }, "strip", z.ZodTypeAny, {
120
+ t: "tool_result";
121
+ callId: string;
122
+ content: ({
123
+ t: "text";
124
+ text: string;
125
+ } | {
126
+ t: "image";
127
+ mime: string;
128
+ data: string;
129
+ })[];
130
+ isError?: boolean | undefined;
131
+ artifact?: unknown;
132
+ }, {
133
+ t: "tool_result";
134
+ callId: string;
135
+ content: ({
136
+ t: "text";
137
+ text: string;
138
+ } | {
139
+ t: "image";
140
+ mime: string;
141
+ data: string;
142
+ })[];
143
+ isError?: boolean | undefined;
144
+ artifact?: unknown;
145
+ }>, z.ZodObject<{
146
+ t: z.ZodLiteral<"image">;
147
+ mime: z.ZodString;
148
+ data: z.ZodString;
149
+ }, "strip", z.ZodTypeAny, {
150
+ t: "image";
151
+ mime: string;
152
+ data: string;
153
+ }, {
154
+ t: "image";
155
+ mime: string;
156
+ data: string;
157
+ }>]>;
158
+ /** 消息内容块:text / thinking / opaque / tool_call / tool_result / image(IRBlockSchema 推导)。 */
159
+ export type IRBlock = z.infer<typeof IRBlockSchema>;
160
+ /** 消息角色词表:user / assistant(system 走 IRRequest.system 分段,不是消息角色)。 */
161
+ export declare const IRRoleSchema: z.ZodEnum<["user", "assistant"]>;
162
+ /** 消息角色(IRRoleSchema 推导)。 */
163
+ export type IRRole = z.infer<typeof IRRoleSchema>;
164
+ /**
165
+ * 消息元数据(contract-v0.15,RFC-CTX12-4)。封闭键 + RFC 扩容(否决
166
+ * z.record 自由袋,RFC-B3-4 quirks 同先例);全键可选、整位可选,缺省 =
167
+ * 普通消息。
168
+ *
169
+ * 兼容原则「透传不解释」:providers 适配器不消费本位——出站 wire 载荷按
170
+ * role + blocks 投影构造,meta 恒不出线(不进模型上下文);入站适配器产
171
+ * IRMessage 不落 meta(标记是 kernel 语义,provider 无从判断)。消费方 =
172
+ * kernel(锚点识别/压缩计划)与表面层(渲染折叠)。
173
+ *
174
+ * 双写过渡纪律:摘要消息 meta 位 + 防碰撞文本哨兵(T-CTX22)双标,直至
175
+ * contract-v1 才允许哨兵退役;读方识别恒为「meta 优先、哨兵回退」双判。
176
+ */
177
+ export declare const IRMessageMetaSchema: z.ZodObject<{
178
+ /**
179
+ * 消息种类标记。首值 'compaction_summary' = 压缩摘要消息(防碰撞
180
+ * 文本哨兵的结构化替代);候选扩值 'pinned'/'synthetic' 随
181
+ * 对应消费方另行契约评审(先冻结已验证形态)。
182
+ */
183
+ kind: z.ZodOptional<z.ZodEnum<["compaction_summary"]>>;
184
+ /** 产生本消息的压缩执行 id(RFC-CTX12-2 compactionId 同源;审计链互指) */
185
+ compactionId: z.ZodOptional<z.ZodString>;
186
+ }, "strip", z.ZodTypeAny, {
187
+ kind?: "compaction_summary" | undefined;
188
+ compactionId?: string | undefined;
189
+ }, {
190
+ kind?: "compaction_summary" | undefined;
191
+ compactionId?: string | undefined;
192
+ }>;
193
+ export type IRMessageMeta = z.infer<typeof IRMessageMetaSchema>;
194
+ export declare const IRMessageSchema: z.ZodObject<{
195
+ role: z.ZodEnum<["user", "assistant"]>;
196
+ blocks: z.ZodArray<z.ZodDiscriminatedUnion<"t", [z.ZodObject<{
197
+ t: z.ZodLiteral<"text">;
198
+ text: z.ZodString;
199
+ }, "strip", z.ZodTypeAny, {
200
+ t: "text";
201
+ text: string;
202
+ }, {
203
+ t: "text";
204
+ text: string;
205
+ }>, z.ZodObject<{
206
+ t: z.ZodLiteral<"thinking">;
207
+ text: z.ZodString;
208
+ signature: z.ZodOptional<z.ZodString>;
209
+ }, "strip", z.ZodTypeAny, {
210
+ t: "thinking";
211
+ text: string;
212
+ signature?: string | undefined;
213
+ }, {
214
+ t: "thinking";
215
+ text: string;
216
+ signature?: string | undefined;
217
+ }>, z.ZodObject<{
218
+ t: z.ZodLiteral<"opaque">;
219
+ /** 归属的 provider/协议,回传时据此判断能否透传 */
220
+ provider: z.ZodString;
221
+ /** provider 原样载荷,内核不得解读或改写 */
222
+ payload: z.ZodUnknown;
223
+ }, "strip", z.ZodTypeAny, {
224
+ t: "opaque";
225
+ provider: string;
226
+ payload?: unknown;
227
+ }, {
228
+ t: "opaque";
229
+ provider: string;
230
+ payload?: unknown;
231
+ }>, z.ZodObject<{
232
+ t: z.ZodLiteral<"tool_call">;
233
+ id: z.ZodString;
234
+ name: z.ZodString;
235
+ args: z.ZodUnknown;
236
+ }, "strip", z.ZodTypeAny, {
237
+ t: "tool_call";
238
+ id: string;
239
+ name: string;
240
+ args?: unknown;
241
+ }, {
242
+ t: "tool_call";
243
+ id: string;
244
+ name: string;
245
+ args?: unknown;
246
+ }>, z.ZodObject<{
247
+ t: z.ZodLiteral<"tool_result">;
248
+ callId: z.ZodString;
249
+ content: z.ZodArray<z.ZodDiscriminatedUnion<"t", [z.ZodObject<{
250
+ t: z.ZodLiteral<"text">;
251
+ text: z.ZodString;
252
+ }, "strip", z.ZodTypeAny, {
253
+ t: "text";
254
+ text: string;
255
+ }, {
256
+ t: "text";
257
+ text: string;
258
+ }>, z.ZodObject<{
259
+ t: z.ZodLiteral<"image">;
260
+ mime: z.ZodString;
261
+ data: z.ZodString;
262
+ }, "strip", z.ZodTypeAny, {
263
+ t: "image";
264
+ mime: string;
265
+ data: string;
266
+ }, {
267
+ t: "image";
268
+ mime: string;
269
+ data: string;
270
+ }>]>, "many">;
271
+ isError: z.ZodOptional<z.ZodBoolean>;
272
+ artifact: z.ZodOptional<z.ZodUnknown>;
273
+ }, "strip", z.ZodTypeAny, {
274
+ t: "tool_result";
275
+ callId: string;
276
+ content: ({
277
+ t: "text";
278
+ text: string;
279
+ } | {
280
+ t: "image";
281
+ mime: string;
282
+ data: string;
283
+ })[];
284
+ isError?: boolean | undefined;
285
+ artifact?: unknown;
286
+ }, {
287
+ t: "tool_result";
288
+ callId: string;
289
+ content: ({
290
+ t: "text";
291
+ text: string;
292
+ } | {
293
+ t: "image";
294
+ mime: string;
295
+ data: string;
296
+ })[];
297
+ isError?: boolean | undefined;
298
+ artifact?: unknown;
299
+ }>, z.ZodObject<{
300
+ t: z.ZodLiteral<"image">;
301
+ mime: z.ZodString;
302
+ data: z.ZodString;
303
+ }, "strip", z.ZodTypeAny, {
304
+ t: "image";
305
+ mime: string;
306
+ data: string;
307
+ }, {
308
+ t: "image";
309
+ mime: string;
310
+ data: string;
311
+ }>]>, "many">;
312
+ /** 结构化元数据(contract-v0.15,RFC-CTX12-4;语义见 IRMessageMetaSchema) */
313
+ meta: z.ZodOptional<z.ZodObject<{
314
+ /**
315
+ * 消息种类标记。首值 'compaction_summary' = 压缩摘要消息(防碰撞
316
+ * 文本哨兵的结构化替代);候选扩值 'pinned'/'synthetic' 随
317
+ * 对应消费方另行契约评审(先冻结已验证形态)。
318
+ */
319
+ kind: z.ZodOptional<z.ZodEnum<["compaction_summary"]>>;
320
+ /** 产生本消息的压缩执行 id(RFC-CTX12-2 compactionId 同源;审计链互指) */
321
+ compactionId: z.ZodOptional<z.ZodString>;
322
+ }, "strip", z.ZodTypeAny, {
323
+ kind?: "compaction_summary" | undefined;
324
+ compactionId?: string | undefined;
325
+ }, {
326
+ kind?: "compaction_summary" | undefined;
327
+ compactionId?: string | undefined;
328
+ }>>;
329
+ }, "strip", z.ZodTypeAny, {
330
+ role: "user" | "assistant";
331
+ blocks: ({
332
+ t: "text";
333
+ text: string;
334
+ } | {
335
+ t: "thinking";
336
+ text: string;
337
+ signature?: string | undefined;
338
+ } | {
339
+ t: "opaque";
340
+ provider: string;
341
+ payload?: unknown;
342
+ } | {
343
+ t: "tool_call";
344
+ id: string;
345
+ name: string;
346
+ args?: unknown;
347
+ } | {
348
+ t: "tool_result";
349
+ callId: string;
350
+ content: ({
351
+ t: "text";
352
+ text: string;
353
+ } | {
354
+ t: "image";
355
+ mime: string;
356
+ data: string;
357
+ })[];
358
+ isError?: boolean | undefined;
359
+ artifact?: unknown;
360
+ } | {
361
+ t: "image";
362
+ mime: string;
363
+ data: string;
364
+ })[];
365
+ meta?: {
366
+ kind?: "compaction_summary" | undefined;
367
+ compactionId?: string | undefined;
368
+ } | undefined;
369
+ }, {
370
+ role: "user" | "assistant";
371
+ blocks: ({
372
+ t: "text";
373
+ text: string;
374
+ } | {
375
+ t: "thinking";
376
+ text: string;
377
+ signature?: string | undefined;
378
+ } | {
379
+ t: "opaque";
380
+ provider: string;
381
+ payload?: unknown;
382
+ } | {
383
+ t: "tool_call";
384
+ id: string;
385
+ name: string;
386
+ args?: unknown;
387
+ } | {
388
+ t: "tool_result";
389
+ callId: string;
390
+ content: ({
391
+ t: "text";
392
+ text: string;
393
+ } | {
394
+ t: "image";
395
+ mime: string;
396
+ data: string;
397
+ })[];
398
+ isError?: boolean | undefined;
399
+ artifact?: unknown;
400
+ } | {
401
+ t: "image";
402
+ mime: string;
403
+ data: string;
404
+ })[];
405
+ meta?: {
406
+ kind?: "compaction_summary" | undefined;
407
+ compactionId?: string | undefined;
408
+ } | undefined;
409
+ }>;
410
+ /** IR 消息:角色 + 内容块数组 + 可选结构化元数据(IRMessageSchema 推导)。 */
411
+ export type IRMessage = z.infer<typeof IRMessageSchema>;
412
+ /** 系统提示分段:稳定前缀在前,便于两家 provider 的缓存机制利用 */
413
+ export declare const IRSystemSegmentSchema: z.ZodObject<{
414
+ text: z.ZodString;
415
+ /** 缓存断点提示——忽略方无害 */
416
+ cacheable: z.ZodOptional<z.ZodBoolean>;
417
+ /**
418
+ * 段语义标签(contract-v0.1):'base'/'tools'/'env'/'memory' 等,
419
+ * 供上下文压缩按语义识别可替换段;消费方忽略无害。
420
+ */
421
+ label: z.ZodOptional<z.ZodString>;
422
+ }, "strip", z.ZodTypeAny, {
423
+ text: string;
424
+ cacheable?: boolean | undefined;
425
+ label?: string | undefined;
426
+ }, {
427
+ text: string;
428
+ cacheable?: boolean | undefined;
429
+ label?: string | undefined;
430
+ }>;
431
+ /** 系统提示分段(IRSystemSegmentSchema 推导)。 */
432
+ export type IRSystemSegment = z.infer<typeof IRSystemSegmentSchema>;
433
+ /** 工具定义:内部统一 JSON Schema(各协议在适配器内映射到各自字段) */
434
+ export declare const IRToolDefSchema: z.ZodObject<{
435
+ name: z.ZodString;
436
+ description: z.ZodString;
437
+ inputSchema: z.ZodRecord<z.ZodString, z.ZodUnknown>;
438
+ }, "strip", z.ZodTypeAny, {
439
+ name: string;
440
+ description: string;
441
+ inputSchema: Record<string, unknown>;
442
+ }, {
443
+ name: string;
444
+ description: string;
445
+ inputSchema: Record<string, unknown>;
446
+ }>;
447
+ /** 工具定义(IRToolDefSchema 推导)。 */
448
+ export type IRToolDef = z.infer<typeof IRToolDefSchema>;
449
+ /** 已解析的模型定位:provider id + 该 provider 下的具体模型名(别名已解析)。 */
450
+ export declare const ResolvedModelSchema: z.ZodObject<{
451
+ /** provider registry 内的 id,如 "anthropic" / "aliyun-maas" */
452
+ provider: z.ZodString;
453
+ /** 该 provider 下的具体模型名(已解析别名后) */
454
+ model: z.ZodString;
455
+ }, "strip", z.ZodTypeAny, {
456
+ provider: string;
457
+ model: string;
458
+ }, {
459
+ provider: string;
460
+ model: string;
461
+ }>;
462
+ /** 已解析的模型定位(ResolvedModelSchema 推导)。 */
463
+ export type ResolvedModel = z.infer<typeof ResolvedModelSchema>;
464
+ /** 一次模型调用的完整请求:系统分段、消息、工具定义、目标模型与采样 / 思考 / 缓存控制位。 */
465
+ export declare const IRRequestSchema: z.ZodObject<{
466
+ system: z.ZodArray<z.ZodObject<{
467
+ text: z.ZodString;
468
+ /** 缓存断点提示——忽略方无害 */
469
+ cacheable: z.ZodOptional<z.ZodBoolean>;
470
+ /**
471
+ * 段语义标签(contract-v0.1):'base'/'tools'/'env'/'memory' 等,
472
+ * 供上下文压缩按语义识别可替换段;消费方忽略无害。
473
+ */
474
+ label: z.ZodOptional<z.ZodString>;
475
+ }, "strip", z.ZodTypeAny, {
476
+ text: string;
477
+ cacheable?: boolean | undefined;
478
+ label?: string | undefined;
479
+ }, {
480
+ text: string;
481
+ cacheable?: boolean | undefined;
482
+ label?: string | undefined;
483
+ }>, "many">;
484
+ messages: z.ZodArray<z.ZodObject<{
485
+ role: z.ZodEnum<["user", "assistant"]>;
486
+ blocks: z.ZodArray<z.ZodDiscriminatedUnion<"t", [z.ZodObject<{
487
+ t: z.ZodLiteral<"text">;
488
+ text: z.ZodString;
489
+ }, "strip", z.ZodTypeAny, {
490
+ t: "text";
491
+ text: string;
492
+ }, {
493
+ t: "text";
494
+ text: string;
495
+ }>, z.ZodObject<{
496
+ t: z.ZodLiteral<"thinking">;
497
+ text: z.ZodString;
498
+ signature: z.ZodOptional<z.ZodString>;
499
+ }, "strip", z.ZodTypeAny, {
500
+ t: "thinking";
501
+ text: string;
502
+ signature?: string | undefined;
503
+ }, {
504
+ t: "thinking";
505
+ text: string;
506
+ signature?: string | undefined;
507
+ }>, z.ZodObject<{
508
+ t: z.ZodLiteral<"opaque">;
509
+ /** 归属的 provider/协议,回传时据此判断能否透传 */
510
+ provider: z.ZodString;
511
+ /** provider 原样载荷,内核不得解读或改写 */
512
+ payload: z.ZodUnknown;
513
+ }, "strip", z.ZodTypeAny, {
514
+ t: "opaque";
515
+ provider: string;
516
+ payload?: unknown;
517
+ }, {
518
+ t: "opaque";
519
+ provider: string;
520
+ payload?: unknown;
521
+ }>, z.ZodObject<{
522
+ t: z.ZodLiteral<"tool_call">;
523
+ id: z.ZodString;
524
+ name: z.ZodString;
525
+ args: z.ZodUnknown;
526
+ }, "strip", z.ZodTypeAny, {
527
+ t: "tool_call";
528
+ id: string;
529
+ name: string;
530
+ args?: unknown;
531
+ }, {
532
+ t: "tool_call";
533
+ id: string;
534
+ name: string;
535
+ args?: unknown;
536
+ }>, z.ZodObject<{
537
+ t: z.ZodLiteral<"tool_result">;
538
+ callId: z.ZodString;
539
+ content: z.ZodArray<z.ZodDiscriminatedUnion<"t", [z.ZodObject<{
540
+ t: z.ZodLiteral<"text">;
541
+ text: z.ZodString;
542
+ }, "strip", z.ZodTypeAny, {
543
+ t: "text";
544
+ text: string;
545
+ }, {
546
+ t: "text";
547
+ text: string;
548
+ }>, z.ZodObject<{
549
+ t: z.ZodLiteral<"image">;
550
+ mime: z.ZodString;
551
+ data: z.ZodString;
552
+ }, "strip", z.ZodTypeAny, {
553
+ t: "image";
554
+ mime: string;
555
+ data: string;
556
+ }, {
557
+ t: "image";
558
+ mime: string;
559
+ data: string;
560
+ }>]>, "many">;
561
+ isError: z.ZodOptional<z.ZodBoolean>;
562
+ artifact: z.ZodOptional<z.ZodUnknown>;
563
+ }, "strip", z.ZodTypeAny, {
564
+ t: "tool_result";
565
+ callId: string;
566
+ content: ({
567
+ t: "text";
568
+ text: string;
569
+ } | {
570
+ t: "image";
571
+ mime: string;
572
+ data: string;
573
+ })[];
574
+ isError?: boolean | undefined;
575
+ artifact?: unknown;
576
+ }, {
577
+ t: "tool_result";
578
+ callId: string;
579
+ content: ({
580
+ t: "text";
581
+ text: string;
582
+ } | {
583
+ t: "image";
584
+ mime: string;
585
+ data: string;
586
+ })[];
587
+ isError?: boolean | undefined;
588
+ artifact?: unknown;
589
+ }>, z.ZodObject<{
590
+ t: z.ZodLiteral<"image">;
591
+ mime: z.ZodString;
592
+ data: z.ZodString;
593
+ }, "strip", z.ZodTypeAny, {
594
+ t: "image";
595
+ mime: string;
596
+ data: string;
597
+ }, {
598
+ t: "image";
599
+ mime: string;
600
+ data: string;
601
+ }>]>, "many">;
602
+ /** 结构化元数据(contract-v0.15,RFC-CTX12-4;语义见 IRMessageMetaSchema) */
603
+ meta: z.ZodOptional<z.ZodObject<{
604
+ /**
605
+ * 消息种类标记。首值 'compaction_summary' = 压缩摘要消息(防碰撞
606
+ * 文本哨兵的结构化替代);候选扩值 'pinned'/'synthetic' 随
607
+ * 对应消费方另行契约评审(先冻结已验证形态)。
608
+ */
609
+ kind: z.ZodOptional<z.ZodEnum<["compaction_summary"]>>;
610
+ /** 产生本消息的压缩执行 id(RFC-CTX12-2 compactionId 同源;审计链互指) */
611
+ compactionId: z.ZodOptional<z.ZodString>;
612
+ }, "strip", z.ZodTypeAny, {
613
+ kind?: "compaction_summary" | undefined;
614
+ compactionId?: string | undefined;
615
+ }, {
616
+ kind?: "compaction_summary" | undefined;
617
+ compactionId?: string | undefined;
618
+ }>>;
619
+ }, "strip", z.ZodTypeAny, {
620
+ role: "user" | "assistant";
621
+ blocks: ({
622
+ t: "text";
623
+ text: string;
624
+ } | {
625
+ t: "thinking";
626
+ text: string;
627
+ signature?: string | undefined;
628
+ } | {
629
+ t: "opaque";
630
+ provider: string;
631
+ payload?: unknown;
632
+ } | {
633
+ t: "tool_call";
634
+ id: string;
635
+ name: string;
636
+ args?: unknown;
637
+ } | {
638
+ t: "tool_result";
639
+ callId: string;
640
+ content: ({
641
+ t: "text";
642
+ text: string;
643
+ } | {
644
+ t: "image";
645
+ mime: string;
646
+ data: string;
647
+ })[];
648
+ isError?: boolean | undefined;
649
+ artifact?: unknown;
650
+ } | {
651
+ t: "image";
652
+ mime: string;
653
+ data: string;
654
+ })[];
655
+ meta?: {
656
+ kind?: "compaction_summary" | undefined;
657
+ compactionId?: string | undefined;
658
+ } | undefined;
659
+ }, {
660
+ role: "user" | "assistant";
661
+ blocks: ({
662
+ t: "text";
663
+ text: string;
664
+ } | {
665
+ t: "thinking";
666
+ text: string;
667
+ signature?: string | undefined;
668
+ } | {
669
+ t: "opaque";
670
+ provider: string;
671
+ payload?: unknown;
672
+ } | {
673
+ t: "tool_call";
674
+ id: string;
675
+ name: string;
676
+ args?: unknown;
677
+ } | {
678
+ t: "tool_result";
679
+ callId: string;
680
+ content: ({
681
+ t: "text";
682
+ text: string;
683
+ } | {
684
+ t: "image";
685
+ mime: string;
686
+ data: string;
687
+ })[];
688
+ isError?: boolean | undefined;
689
+ artifact?: unknown;
690
+ } | {
691
+ t: "image";
692
+ mime: string;
693
+ data: string;
694
+ })[];
695
+ meta?: {
696
+ kind?: "compaction_summary" | undefined;
697
+ compactionId?: string | undefined;
698
+ } | undefined;
699
+ }>, "many">;
700
+ tools: z.ZodArray<z.ZodObject<{
701
+ name: z.ZodString;
702
+ description: z.ZodString;
703
+ inputSchema: z.ZodRecord<z.ZodString, z.ZodUnknown>;
704
+ }, "strip", z.ZodTypeAny, {
705
+ name: string;
706
+ description: string;
707
+ inputSchema: Record<string, unknown>;
708
+ }, {
709
+ name: string;
710
+ description: string;
711
+ inputSchema: Record<string, unknown>;
712
+ }>, "many">;
713
+ model: z.ZodObject<{
714
+ /** provider registry 内的 id,如 "anthropic" / "aliyun-maas" */
715
+ provider: z.ZodString;
716
+ /** 该 provider 下的具体模型名(已解析别名后) */
717
+ model: z.ZodString;
718
+ }, "strip", z.ZodTypeAny, {
719
+ provider: string;
720
+ model: string;
721
+ }, {
722
+ provider: string;
723
+ model: string;
724
+ }>;
725
+ maxTokens: z.ZodNumber;
726
+ temperature: z.ZodOptional<z.ZodNumber>;
727
+ /**
728
+ * thinking 能力位控制;适配器按 provider capability 改写。
729
+ * `off` 位 = 显式思考关断(fast 形态载体)——
730
+ * 与「缺席」语义不同:缺席 = 上游缺省(openai 兼容面思考模型缺省仍开思考,
731
+ * UCloud 实证),off = 适配器按方言显式关(twp → params.reasoning='off';
732
+ * openai 直连 → enable_thinking:false;anthropic → 不发 thinking 参数)。
733
+ * off 在场时 budget 忽略。
734
+ */
735
+ thinking: z.ZodOptional<z.ZodObject<{
736
+ budget: z.ZodOptional<z.ZodNumber>;
737
+ off: z.ZodOptional<z.ZodBoolean>;
738
+ }, "strip", z.ZodTypeAny, {
739
+ budget?: number | undefined;
740
+ off?: boolean | undefined;
741
+ }, {
742
+ budget?: number | undefined;
743
+ off?: boolean | undefined;
744
+ }>>;
745
+ stopSequences: z.ZodOptional<z.ZodArray<z.ZodString, "many">>;
746
+ /**
747
+ * 请求级缓存写抑制(contract-v0.8):true 时显式缓存适配器
748
+ * 不在消息尾落 cache_control 断点(system/工具面断点不受影响)。签发方为
749
+ * 一次性辅助请求(上下文压缩/标题生成)——其消息尾不会被复读,
750
+ * 写缓存纯付费无收益。implicit/ignored 缓存模式的适配器忽略本键。
751
+ */
752
+ skipCacheWrite: z.ZodOptional<z.ZodBoolean>;
753
+ /**
754
+ * 端侧预估的 prompt 侧 token 总量(贴窗防御;加法可选)。
755
+ * 语义:请求面(system+messages+tools)装配定格后、发出前的估算值,
756
+ * 整数 ≥1(向上取整;api TwpParamsSchema.promptTokensEstimate 同下限)。
757
+ * 消费方 = twp 适配器(落 params.promptTokensEstimate,网关侧贴窗拦截,
758
+ * 恒不送上游);其余协议适配器忽略。缺席 = 端侧无估算可用(压缩与窗口
759
+ * 均未接线),服务端仅既有输出钳制,行为零变化。
760
+ */
761
+ promptTokensEstimate: z.ZodOptional<z.ZodNumber>;
762
+ }, "strip", z.ZodTypeAny, {
763
+ model: {
764
+ provider: string;
765
+ model: string;
766
+ };
767
+ system: {
768
+ text: string;
769
+ cacheable?: boolean | undefined;
770
+ label?: string | undefined;
771
+ }[];
772
+ messages: {
773
+ role: "user" | "assistant";
774
+ blocks: ({
775
+ t: "text";
776
+ text: string;
777
+ } | {
778
+ t: "thinking";
779
+ text: string;
780
+ signature?: string | undefined;
781
+ } | {
782
+ t: "opaque";
783
+ provider: string;
784
+ payload?: unknown;
785
+ } | {
786
+ t: "tool_call";
787
+ id: string;
788
+ name: string;
789
+ args?: unknown;
790
+ } | {
791
+ t: "tool_result";
792
+ callId: string;
793
+ content: ({
794
+ t: "text";
795
+ text: string;
796
+ } | {
797
+ t: "image";
798
+ mime: string;
799
+ data: string;
800
+ })[];
801
+ isError?: boolean | undefined;
802
+ artifact?: unknown;
803
+ } | {
804
+ t: "image";
805
+ mime: string;
806
+ data: string;
807
+ })[];
808
+ meta?: {
809
+ kind?: "compaction_summary" | undefined;
810
+ compactionId?: string | undefined;
811
+ } | undefined;
812
+ }[];
813
+ tools: {
814
+ name: string;
815
+ description: string;
816
+ inputSchema: Record<string, unknown>;
817
+ }[];
818
+ maxTokens: number;
819
+ thinking?: {
820
+ budget?: number | undefined;
821
+ off?: boolean | undefined;
822
+ } | undefined;
823
+ temperature?: number | undefined;
824
+ stopSequences?: string[] | undefined;
825
+ skipCacheWrite?: boolean | undefined;
826
+ promptTokensEstimate?: number | undefined;
827
+ }, {
828
+ model: {
829
+ provider: string;
830
+ model: string;
831
+ };
832
+ system: {
833
+ text: string;
834
+ cacheable?: boolean | undefined;
835
+ label?: string | undefined;
836
+ }[];
837
+ messages: {
838
+ role: "user" | "assistant";
839
+ blocks: ({
840
+ t: "text";
841
+ text: string;
842
+ } | {
843
+ t: "thinking";
844
+ text: string;
845
+ signature?: string | undefined;
846
+ } | {
847
+ t: "opaque";
848
+ provider: string;
849
+ payload?: unknown;
850
+ } | {
851
+ t: "tool_call";
852
+ id: string;
853
+ name: string;
854
+ args?: unknown;
855
+ } | {
856
+ t: "tool_result";
857
+ callId: string;
858
+ content: ({
859
+ t: "text";
860
+ text: string;
861
+ } | {
862
+ t: "image";
863
+ mime: string;
864
+ data: string;
865
+ })[];
866
+ isError?: boolean | undefined;
867
+ artifact?: unknown;
868
+ } | {
869
+ t: "image";
870
+ mime: string;
871
+ data: string;
872
+ })[];
873
+ meta?: {
874
+ kind?: "compaction_summary" | undefined;
875
+ compactionId?: string | undefined;
876
+ } | undefined;
877
+ }[];
878
+ tools: {
879
+ name: string;
880
+ description: string;
881
+ inputSchema: Record<string, unknown>;
882
+ }[];
883
+ maxTokens: number;
884
+ thinking?: {
885
+ budget?: number | undefined;
886
+ off?: boolean | undefined;
887
+ } | undefined;
888
+ temperature?: number | undefined;
889
+ stopSequences?: string[] | undefined;
890
+ skipCacheWrite?: boolean | undefined;
891
+ promptTokensEstimate?: number | undefined;
892
+ }>;
893
+ /** 模型调用请求(IRRequestSchema 推导)。 */
894
+ export type IRRequest = z.infer<typeof IRRequestSchema>;
895
+ /** 五路用量归一化(input/output + 缓存读/写细分 + 推理输出)。
896
+ * inputTokens 为 exclusive 口径(规范语义,见字段注释)。 */
897
+ export declare const IRUsageSchema: z.ZodObject<{
898
+ /**
899
+ * 主输入计量,**exclusive 口径**(规范语义,contract-v0.15 RFC-CTX12-5
900
+ * 会签):不含 cacheReadInputTokens 与 cacheCreationInputTokens——三路
901
+ * 相加 = 请求输入总量。适配器出口恒为本口径:inclusive 端点(OpenAI 系
902
+ * prompt_tokens,cached ⊆ prompt)在适配器内减法归一,包含关系以 compat
903
+ * 实录核证(见 providers openai/wire.ts exclusiveInputTokens
904
+ * 注释)。消费方(上下文账本/CostTracker/状态栏)三路直加,不得再做
905
+ * inclusive 换算(防双重矫正)。限界注记:端点的缓存写入量不可见时
906
+ * (如 OpenAI Chat 协议无 cache_write 字段),写入量仍含在本键内
907
+ * (cacheCreation 缺席,三路总量不失真)。
908
+ */
909
+ inputTokens: z.ZodNumber;
910
+ outputTokens: z.ZodNumber;
911
+ cacheReadInputTokens: z.ZodOptional<z.ZodNumber>;
912
+ cacheCreationInputTokens: z.ZodOptional<z.ZodNumber>;
913
+ /** 思考 / 推理输出量(单列上报者填;缺省不落键)。与 outputTokens 的包含关系不由本键自述,由能力位
914
+ * quirks.reasoningTokens 声明(RFC-RF-3;未声明按子集);适配器恒原样透传,消费方按能力位决定是否叠加 */
915
+ reasoningOutputTokens: z.ZodOptional<z.ZodNumber>;
916
+ /**
917
+ * 缓存删除量(contract-v0.4;cached microcompact):
918
+ * provider 报告的 cache_deleted_input_tokens——服务端缓存段被逐出/失效的
919
+ * 通知量,**非计费维度**(cost-tracker 不入账),上下文账本(context
920
+ * ledger)以此校正窗口水位。观测到该字段的适配器填入;缺省不落键。
921
+ */
922
+ cacheDeletedInputTokens: z.ZodOptional<z.ZodNumber>;
923
+ /**
924
+ * 长尾计量维度桶(CW8 B3 契约 RFC 会签、B7 落地;纯增量可选键,包络 v
925
+ * 不 bump——v0.4 cacheDeletedInputTokens 先例)。键词表与服务端维度
926
+ * 注册表同源(twp 帧 dims 直透):计费长尾排他桶(cache_w_5m/1h、
927
+ * reasoning_out、多模态等)+ `obs_` 前缀观测位(不参与计费,呈现冠
928
+ * 「(观测)」);值为非负整数量(token/次数),非零桶才携带。恒不含
929
+ * 四主键 in/out/cache_r/cache_w 的语义重复(双源漂移防御,服务端
930
+ * schema 拒;适配器入站防御性剔除)。与 reasoningOutputTokens 的关系:
931
+ * 后者是既有专位(v0.x 兼容),gemini 独立计费的 reasoning_out 桶两处
932
+ * 并存时值恒相等(适配器同源填写)。
933
+ */
934
+ dims: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodNumber>>;
935
+ /**
936
+ * 缓存净省费(CW8 B3 RFC,裁决 16;DECIMAL 串,= readSaved −
937
+ * writePremium,**可为负**——写溢超读省的会话如实呈负,防「只报喜」)。
938
+ * twp 帧 saved 直透,≠0 才携带(缺席 =「无省费故事可讲」,端侧整行
939
+ * 不出);恒服务端权威——本地估算径(server-cost 缺席时)不得拟造本键
940
+ * (恒不猜纪律)。三分量明细(readSaved/writePremium/netSaved)走读面,
941
+ * 不进本键。
942
+ */
943
+ saved: z.ZodOptional<z.ZodString>;
944
+ }, "strip", z.ZodTypeAny, {
945
+ inputTokens: number;
946
+ outputTokens: number;
947
+ cacheReadInputTokens?: number | undefined;
948
+ cacheCreationInputTokens?: number | undefined;
949
+ reasoningOutputTokens?: number | undefined;
950
+ cacheDeletedInputTokens?: number | undefined;
951
+ dims?: Record<string, number> | undefined;
952
+ saved?: string | undefined;
953
+ }, {
954
+ inputTokens: number;
955
+ outputTokens: number;
956
+ cacheReadInputTokens?: number | undefined;
957
+ cacheCreationInputTokens?: number | undefined;
958
+ reasoningOutputTokens?: number | undefined;
959
+ cacheDeletedInputTokens?: number | undefined;
960
+ dims?: Record<string, number> | undefined;
961
+ saved?: string | undefined;
962
+ }>;
963
+ /** 归一化用量(IRUsageSchema 推导)。 */
964
+ export type IRUsage = z.infer<typeof IRUsageSchema>;
965
+ /**
966
+ * 归一化错误类别。
967
+ * policy_denied:平台策略/账务拒绝(余额/配额/内容
968
+ * 安全/出网/许可证/围栏/价格护栏等用户可行动族)——fail-fast 零重试,
969
+ * 与 invalid_request(请求本身不合法)语义分离;原始码与 detail 经错误
970
+ * 文案(`code: message`)与 twp onPlatformError 旁路直达渲染层。
971
+ * 后增两类(contract 加法演进):
972
+ * - connect:连接建立失败/连接超时(本端建连闸、网关 upstream_connect/
973
+ * ttfb 归一码)——与 network(本地毫秒级失败)同路由「耐心档」重试,
974
+ * 分列是为观测与文案区分「等墙」与「本机网络故障」两种病灶;
975
+ * - stream_interrupted:已收到部分响应后断流(idle 闸、上游中途掐流、
976
+ * 无 message_stop 收尾)——常规档重试,呈现面可如实说「响应被截断」。
977
+ * 消费方(重试路由/呈现)对枚举外值须按 recoverable 兜底,不做穷举断言。
978
+ */
979
+ export declare const IRErrorKindSchema: z.ZodEnum<["rate_limited", "context_overflow", "auth", "server", "network", "connect", "stream_interrupted", "invalid_request", "policy_denied"]>;
980
+ /** 归一化错误类别(IRErrorKindSchema 推导)。 */
981
+ export type IRErrorKind = z.infer<typeof IRErrorKindSchema>;
982
+ /**
983
+ * 模型侧流事件,与内核 msg.* 事件同构。
984
+ * 适配器把各协议 SSE/delta 解析为本联合;内核只消费本联合。
985
+ */
986
+ export declare const IRStreamEventSchema: z.ZodDiscriminatedUnion<"t", [z.ZodObject<{
987
+ t: z.ZodLiteral<"message_start">;
988
+ model: z.ZodString;
989
+ }, "strip", z.ZodTypeAny, {
990
+ t: "message_start";
991
+ model: string;
992
+ }, {
993
+ t: "message_start";
994
+ model: string;
995
+ }>, z.ZodObject<{
996
+ t: z.ZodLiteral<"block_start">;
997
+ index: z.ZodNumber;
998
+ block: z.ZodDiscriminatedUnion<"t", [z.ZodObject<{
999
+ t: z.ZodLiteral<"text">;
1000
+ text: z.ZodString;
1001
+ }, "strip", z.ZodTypeAny, {
1002
+ t: "text";
1003
+ text: string;
1004
+ }, {
1005
+ t: "text";
1006
+ text: string;
1007
+ }>, z.ZodObject<{
1008
+ t: z.ZodLiteral<"thinking">;
1009
+ text: z.ZodString;
1010
+ signature: z.ZodOptional<z.ZodString>;
1011
+ }, "strip", z.ZodTypeAny, {
1012
+ t: "thinking";
1013
+ text: string;
1014
+ signature?: string | undefined;
1015
+ }, {
1016
+ t: "thinking";
1017
+ text: string;
1018
+ signature?: string | undefined;
1019
+ }>, z.ZodObject<{
1020
+ t: z.ZodLiteral<"opaque">;
1021
+ /** 归属的 provider/协议,回传时据此判断能否透传 */
1022
+ provider: z.ZodString;
1023
+ /** provider 原样载荷,内核不得解读或改写 */
1024
+ payload: z.ZodUnknown;
1025
+ }, "strip", z.ZodTypeAny, {
1026
+ t: "opaque";
1027
+ provider: string;
1028
+ payload?: unknown;
1029
+ }, {
1030
+ t: "opaque";
1031
+ provider: string;
1032
+ payload?: unknown;
1033
+ }>, z.ZodObject<{
1034
+ t: z.ZodLiteral<"tool_call">;
1035
+ id: z.ZodString;
1036
+ name: z.ZodString;
1037
+ args: z.ZodUnknown;
1038
+ }, "strip", z.ZodTypeAny, {
1039
+ t: "tool_call";
1040
+ id: string;
1041
+ name: string;
1042
+ args?: unknown;
1043
+ }, {
1044
+ t: "tool_call";
1045
+ id: string;
1046
+ name: string;
1047
+ args?: unknown;
1048
+ }>, z.ZodObject<{
1049
+ t: z.ZodLiteral<"tool_result">;
1050
+ callId: z.ZodString;
1051
+ content: z.ZodArray<z.ZodDiscriminatedUnion<"t", [z.ZodObject<{
1052
+ t: z.ZodLiteral<"text">;
1053
+ text: z.ZodString;
1054
+ }, "strip", z.ZodTypeAny, {
1055
+ t: "text";
1056
+ text: string;
1057
+ }, {
1058
+ t: "text";
1059
+ text: string;
1060
+ }>, z.ZodObject<{
1061
+ t: z.ZodLiteral<"image">;
1062
+ mime: z.ZodString;
1063
+ data: z.ZodString;
1064
+ }, "strip", z.ZodTypeAny, {
1065
+ t: "image";
1066
+ mime: string;
1067
+ data: string;
1068
+ }, {
1069
+ t: "image";
1070
+ mime: string;
1071
+ data: string;
1072
+ }>]>, "many">;
1073
+ isError: z.ZodOptional<z.ZodBoolean>;
1074
+ artifact: z.ZodOptional<z.ZodUnknown>;
1075
+ }, "strip", z.ZodTypeAny, {
1076
+ t: "tool_result";
1077
+ callId: string;
1078
+ content: ({
1079
+ t: "text";
1080
+ text: string;
1081
+ } | {
1082
+ t: "image";
1083
+ mime: string;
1084
+ data: string;
1085
+ })[];
1086
+ isError?: boolean | undefined;
1087
+ artifact?: unknown;
1088
+ }, {
1089
+ t: "tool_result";
1090
+ callId: string;
1091
+ content: ({
1092
+ t: "text";
1093
+ text: string;
1094
+ } | {
1095
+ t: "image";
1096
+ mime: string;
1097
+ data: string;
1098
+ })[];
1099
+ isError?: boolean | undefined;
1100
+ artifact?: unknown;
1101
+ }>, z.ZodObject<{
1102
+ t: z.ZodLiteral<"image">;
1103
+ mime: z.ZodString;
1104
+ data: z.ZodString;
1105
+ }, "strip", z.ZodTypeAny, {
1106
+ t: "image";
1107
+ mime: string;
1108
+ data: string;
1109
+ }, {
1110
+ t: "image";
1111
+ mime: string;
1112
+ data: string;
1113
+ }>]>;
1114
+ }, "strip", z.ZodTypeAny, {
1115
+ t: "block_start";
1116
+ index: number;
1117
+ block: {
1118
+ t: "text";
1119
+ text: string;
1120
+ } | {
1121
+ t: "thinking";
1122
+ text: string;
1123
+ signature?: string | undefined;
1124
+ } | {
1125
+ t: "opaque";
1126
+ provider: string;
1127
+ payload?: unknown;
1128
+ } | {
1129
+ t: "tool_call";
1130
+ id: string;
1131
+ name: string;
1132
+ args?: unknown;
1133
+ } | {
1134
+ t: "tool_result";
1135
+ callId: string;
1136
+ content: ({
1137
+ t: "text";
1138
+ text: string;
1139
+ } | {
1140
+ t: "image";
1141
+ mime: string;
1142
+ data: string;
1143
+ })[];
1144
+ isError?: boolean | undefined;
1145
+ artifact?: unknown;
1146
+ } | {
1147
+ t: "image";
1148
+ mime: string;
1149
+ data: string;
1150
+ };
1151
+ }, {
1152
+ t: "block_start";
1153
+ index: number;
1154
+ block: {
1155
+ t: "text";
1156
+ text: string;
1157
+ } | {
1158
+ t: "thinking";
1159
+ text: string;
1160
+ signature?: string | undefined;
1161
+ } | {
1162
+ t: "opaque";
1163
+ provider: string;
1164
+ payload?: unknown;
1165
+ } | {
1166
+ t: "tool_call";
1167
+ id: string;
1168
+ name: string;
1169
+ args?: unknown;
1170
+ } | {
1171
+ t: "tool_result";
1172
+ callId: string;
1173
+ content: ({
1174
+ t: "text";
1175
+ text: string;
1176
+ } | {
1177
+ t: "image";
1178
+ mime: string;
1179
+ data: string;
1180
+ })[];
1181
+ isError?: boolean | undefined;
1182
+ artifact?: unknown;
1183
+ } | {
1184
+ t: "image";
1185
+ mime: string;
1186
+ data: string;
1187
+ };
1188
+ }>, z.ZodObject<{
1189
+ t: z.ZodLiteral<"text_delta">;
1190
+ index: z.ZodNumber;
1191
+ text: z.ZodString;
1192
+ }, "strip", z.ZodTypeAny, {
1193
+ t: "text_delta";
1194
+ text: string;
1195
+ index: number;
1196
+ }, {
1197
+ t: "text_delta";
1198
+ text: string;
1199
+ index: number;
1200
+ }>, z.ZodObject<{
1201
+ t: z.ZodLiteral<"thinking_delta">;
1202
+ index: z.ZodNumber;
1203
+ text: z.ZodString;
1204
+ }, "strip", z.ZodTypeAny, {
1205
+ t: "thinking_delta";
1206
+ text: string;
1207
+ index: number;
1208
+ }, {
1209
+ t: "thinking_delta";
1210
+ text: string;
1211
+ index: number;
1212
+ }>, z.ZodObject<{
1213
+ t: z.ZodLiteral<"signature_delta">;
1214
+ index: z.ZodNumber;
1215
+ text: z.ZodString;
1216
+ }, "strip", z.ZodTypeAny, {
1217
+ t: "signature_delta";
1218
+ text: string;
1219
+ index: number;
1220
+ }, {
1221
+ t: "signature_delta";
1222
+ text: string;
1223
+ index: number;
1224
+ }>, z.ZodObject<{
1225
+ t: z.ZodLiteral<"tool_args_delta">;
1226
+ index: z.ZodNumber;
1227
+ json: z.ZodString;
1228
+ }, "strip", z.ZodTypeAny, {
1229
+ t: "tool_args_delta";
1230
+ index: number;
1231
+ json: string;
1232
+ }, {
1233
+ t: "tool_args_delta";
1234
+ index: number;
1235
+ json: string;
1236
+ }>, z.ZodObject<{
1237
+ t: z.ZodLiteral<"block_stop">;
1238
+ index: z.ZodNumber;
1239
+ }, "strip", z.ZodTypeAny, {
1240
+ t: "block_stop";
1241
+ index: number;
1242
+ }, {
1243
+ t: "block_stop";
1244
+ index: number;
1245
+ }>, z.ZodObject<{
1246
+ t: z.ZodLiteral<"message_stop">;
1247
+ /**
1248
+ * 归一化 stop 原因。'unknown' 为文档化取值,X3-2a P-2 起语义收窄:
1249
+ * **仅限「协议级语义终止帧缺席」(物理断流)的容错收尾留痕**
1250
+ * (现签发点:twp/openai-responses 的 finish() 与 responses 的
1251
+ * [DONE]-无-completed 分支);kernel 查询环按「无 message_stop」同律
1252
+ * 消费为可恢复错误(run-query consumeModelStream),半截产物不入史。
1253
+ * 终止帧在场、仅 stop 元数据缺失的形态(anthropic message_stop 无
1254
+ * message_delta / openai chat [DONE] 无 finish_reason)响应已完整,
1255
+ * 适配器兜底 'end_turn',不得用 'unknown'(会被当截断误重试)。
1256
+ */
1257
+ stopReason: z.ZodString;
1258
+ usage: z.ZodOptional<z.ZodObject<{
1259
+ /**
1260
+ * 主输入计量,**exclusive 口径**(规范语义,contract-v0.15 RFC-CTX12-5
1261
+ * 会签):不含 cacheReadInputTokens 与 cacheCreationInputTokens——三路
1262
+ * 相加 = 请求输入总量。适配器出口恒为本口径:inclusive 端点(OpenAI 系
1263
+ * prompt_tokens,cached ⊆ prompt)在适配器内减法归一,包含关系以 compat
1264
+ * 实录核证(见 providers openai/wire.ts exclusiveInputTokens
1265
+ * 注释)。消费方(上下文账本/CostTracker/状态栏)三路直加,不得再做
1266
+ * inclusive 换算(防双重矫正)。限界注记:端点的缓存写入量不可见时
1267
+ * (如 OpenAI Chat 协议无 cache_write 字段),写入量仍含在本键内
1268
+ * (cacheCreation 缺席,三路总量不失真)。
1269
+ */
1270
+ inputTokens: z.ZodNumber;
1271
+ outputTokens: z.ZodNumber;
1272
+ cacheReadInputTokens: z.ZodOptional<z.ZodNumber>;
1273
+ cacheCreationInputTokens: z.ZodOptional<z.ZodNumber>;
1274
+ /** 思考 / 推理输出量(单列上报者填;缺省不落键)。与 outputTokens 的包含关系不由本键自述,由能力位
1275
+ * quirks.reasoningTokens 声明(RFC-RF-3;未声明按子集);适配器恒原样透传,消费方按能力位决定是否叠加 */
1276
+ reasoningOutputTokens: z.ZodOptional<z.ZodNumber>;
1277
+ /**
1278
+ * 缓存删除量(contract-v0.4;cached microcompact):
1279
+ * provider 报告的 cache_deleted_input_tokens——服务端缓存段被逐出/失效的
1280
+ * 通知量,**非计费维度**(cost-tracker 不入账),上下文账本(context
1281
+ * ledger)以此校正窗口水位。观测到该字段的适配器填入;缺省不落键。
1282
+ */
1283
+ cacheDeletedInputTokens: z.ZodOptional<z.ZodNumber>;
1284
+ /**
1285
+ * 长尾计量维度桶(CW8 B3 契约 RFC 会签、B7 落地;纯增量可选键,包络 v
1286
+ * 不 bump——v0.4 cacheDeletedInputTokens 先例)。键词表与服务端维度
1287
+ * 注册表同源(twp 帧 dims 直透):计费长尾排他桶(cache_w_5m/1h、
1288
+ * reasoning_out、多模态等)+ `obs_` 前缀观测位(不参与计费,呈现冠
1289
+ * 「(观测)」);值为非负整数量(token/次数),非零桶才携带。恒不含
1290
+ * 四主键 in/out/cache_r/cache_w 的语义重复(双源漂移防御,服务端
1291
+ * schema 拒;适配器入站防御性剔除)。与 reasoningOutputTokens 的关系:
1292
+ * 后者是既有专位(v0.x 兼容),gemini 独立计费的 reasoning_out 桶两处
1293
+ * 并存时值恒相等(适配器同源填写)。
1294
+ */
1295
+ dims: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodNumber>>;
1296
+ /**
1297
+ * 缓存净省费(CW8 B3 RFC,裁决 16;DECIMAL 串,= readSaved −
1298
+ * writePremium,**可为负**——写溢超读省的会话如实呈负,防「只报喜」)。
1299
+ * twp 帧 saved 直透,≠0 才携带(缺席 =「无省费故事可讲」,端侧整行
1300
+ * 不出);恒服务端权威——本地估算径(server-cost 缺席时)不得拟造本键
1301
+ * (恒不猜纪律)。三分量明细(readSaved/writePremium/netSaved)走读面,
1302
+ * 不进本键。
1303
+ */
1304
+ saved: z.ZodOptional<z.ZodString>;
1305
+ }, "strip", z.ZodTypeAny, {
1306
+ inputTokens: number;
1307
+ outputTokens: number;
1308
+ cacheReadInputTokens?: number | undefined;
1309
+ cacheCreationInputTokens?: number | undefined;
1310
+ reasoningOutputTokens?: number | undefined;
1311
+ cacheDeletedInputTokens?: number | undefined;
1312
+ dims?: Record<string, number> | undefined;
1313
+ saved?: string | undefined;
1314
+ }, {
1315
+ inputTokens: number;
1316
+ outputTokens: number;
1317
+ cacheReadInputTokens?: number | undefined;
1318
+ cacheCreationInputTokens?: number | undefined;
1319
+ reasoningOutputTokens?: number | undefined;
1320
+ cacheDeletedInputTokens?: number | undefined;
1321
+ dims?: Record<string, number> | undefined;
1322
+ saved?: string | undefined;
1323
+ }>>;
1324
+ ttftMs: z.ZodOptional<z.ZodNumber>;
1325
+ }, "strip", z.ZodTypeAny, {
1326
+ t: "message_stop";
1327
+ stopReason: string;
1328
+ usage?: {
1329
+ inputTokens: number;
1330
+ outputTokens: number;
1331
+ cacheReadInputTokens?: number | undefined;
1332
+ cacheCreationInputTokens?: number | undefined;
1333
+ reasoningOutputTokens?: number | undefined;
1334
+ cacheDeletedInputTokens?: number | undefined;
1335
+ dims?: Record<string, number> | undefined;
1336
+ saved?: string | undefined;
1337
+ } | undefined;
1338
+ ttftMs?: number | undefined;
1339
+ }, {
1340
+ t: "message_stop";
1341
+ stopReason: string;
1342
+ usage?: {
1343
+ inputTokens: number;
1344
+ outputTokens: number;
1345
+ cacheReadInputTokens?: number | undefined;
1346
+ cacheCreationInputTokens?: number | undefined;
1347
+ reasoningOutputTokens?: number | undefined;
1348
+ cacheDeletedInputTokens?: number | undefined;
1349
+ dims?: Record<string, number> | undefined;
1350
+ saved?: string | undefined;
1351
+ } | undefined;
1352
+ ttftMs?: number | undefined;
1353
+ }>, z.ZodObject<{
1354
+ t: z.ZodLiteral<"error">;
1355
+ kind: z.ZodEnum<["rate_limited", "context_overflow", "auth", "server", "network", "connect", "stream_interrupted", "invalid_request", "policy_denied"]>;
1356
+ message: z.ZodString;
1357
+ recoverable: z.ZodBoolean;
1358
+ retryAfterMs: z.ZodOptional<z.ZodNumber>;
1359
+ /**
1360
+ * R-5(doc/77 §五 R6,contract 加法演进):重试层终局错误随行的重试
1361
+ * 摘要——attempts 为该逻辑请求的传输尝试总数(含首次),elapsedMs 为
1362
+ * 自首次尝试起的墙钟耗时。kernel 据此在最终失败文案组装「已自动重试
1363
+ * N 次(历时 T 秒)仍失败」;缺席 = 未经重试层/首次即终局。
1364
+ */
1365
+ retrySummary: z.ZodOptional<z.ZodObject<{
1366
+ attempts: z.ZodNumber;
1367
+ elapsedMs: z.ZodNumber;
1368
+ }, "strip", z.ZodTypeAny, {
1369
+ attempts: number;
1370
+ elapsedMs: number;
1371
+ }, {
1372
+ attempts: number;
1373
+ elapsedMs: number;
1374
+ }>>;
1375
+ /**
1376
+ * 结构化事实位(contract-v0.26;加法可选):适配器把服务端
1377
+ * 信封 `error.detail` 中**机器可判、不含身份 / 内部 id** 的事实原样带到归一错误上,
1378
+ * kernel 透传进 `turn.error.detail`。首个签发方 = providers twp 归一层对平台套餐码
1379
+ * (`plan_concurrency_exceeded` / `plan_end_users_exceeded` / `plan_required` /
1380
+ * `plan_tier_insufficient`)落 `{ scope: 'plan' }`——端侧据此把「开发者套餐配额」与
1381
+ * 「模型限流」区分(HTTP 面同键同值;事件流面此前分叉)。缺席 = 无附加事实。
1382
+ */
1383
+ detail: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
1384
+ /**
1385
+ * 传输幂等键已被本次失败尝试烧毁(2026-08-27 幂等 409 事故取证,契约
1386
+ * 加法演进):请求已抵达服务端且流式语义下服务端将/已按断链结算并记
1387
+ * 「完成不可重放」——同键重发注定 409 idempotency_conflict。重试层
1388
+ * 见此标记即换新键重试(= 作为新请求重新执行,计费如实);缺席 =
1389
+ * 键仍可复用(纯网络失败/服务端 failed 终态,同键重发是防双计正道)。
1390
+ */
1391
+ idempotencyConsumed: z.ZodOptional<z.ZodBoolean>;
1392
+ }, "strip", z.ZodTypeAny, {
1393
+ t: "error";
1394
+ message: string;
1395
+ kind: "rate_limited" | "context_overflow" | "auth" | "server" | "network" | "connect" | "stream_interrupted" | "invalid_request" | "policy_denied";
1396
+ recoverable: boolean;
1397
+ retryAfterMs?: number | undefined;
1398
+ retrySummary?: {
1399
+ attempts: number;
1400
+ elapsedMs: number;
1401
+ } | undefined;
1402
+ detail?: Record<string, unknown> | undefined;
1403
+ idempotencyConsumed?: boolean | undefined;
1404
+ }, {
1405
+ t: "error";
1406
+ message: string;
1407
+ kind: "rate_limited" | "context_overflow" | "auth" | "server" | "network" | "connect" | "stream_interrupted" | "invalid_request" | "policy_denied";
1408
+ recoverable: boolean;
1409
+ retryAfterMs?: number | undefined;
1410
+ retrySummary?: {
1411
+ attempts: number;
1412
+ elapsedMs: number;
1413
+ } | undefined;
1414
+ detail?: Record<string, unknown> | undefined;
1415
+ idempotencyConsumed?: boolean | undefined;
1416
+ }>]>;
1417
+ /** 类型化流事件(IRStreamEventSchema 推导)。 */
1418
+ export type IRStreamEvent = z.infer<typeof IRStreamEventSchema>;