@irogane/kaji 0.2.0-beta.11

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 (136) hide show
  1. package/LICENSE +105 -0
  2. package/README.md +598 -0
  3. package/contracts/README.md +14 -0
  4. package/contracts/beta-core-v1.json +52 -0
  5. package/contracts/cli/init-cases-v1.json +27 -0
  6. package/contracts/errors/error-codes.json +48 -0
  7. package/contracts/errors/integration-recovery-v1.json +127 -0
  8. package/contracts/errors/provider-normalization.json +111 -0
  9. package/contracts/events/conformance-invalid.json +20 -0
  10. package/contracts/events/conformance.json +511 -0
  11. package/contracts/events/new-kaji-event-v1.schema.json +1021 -0
  12. package/contracts/events/stored-kaji-event-v1.schema.json +1025 -0
  13. package/contracts/feature-tiers-v1.json +488 -0
  14. package/contracts/integrations/abi-index-v1.json +8 -0
  15. package/contracts/integrations/conformance-invalid.json +443 -0
  16. package/contracts/integrations/conformance-valid.json +121 -0
  17. package/contracts/integrations/copy-provenance-v1.schema.json +61 -0
  18. package/contracts/integrations/echo-tool-abi-v1.json +37 -0
  19. package/contracts/integrations/github-api-conformance-v1.json +644 -0
  20. package/contracts/integrations/github-tool-abi-typescript-v1.json +369 -0
  21. package/contracts/integrations/github-tool-abi-v1.json +146 -0
  22. package/contracts/integrations/gmail-api-conformance-v1.json +750 -0
  23. package/contracts/integrations/gmail-tool-abi-v1.json +62 -0
  24. package/contracts/integrations/index.schema.json +37 -0
  25. package/contracts/integrations/manifest.schema.json +119 -0
  26. package/contracts/parity/expected-normalized.json +4906 -0
  27. package/contracts/parity/scenarios.json +100 -0
  28. package/contracts/parity/scenarios.schema.json +214 -0
  29. package/contracts/providers/cost-conformance.json +112 -0
  30. package/contracts/release/github-proof-v1.schema.json +138 -0
  31. package/contracts/release/gmail-proof-v1.schema.json +138 -0
  32. package/contracts/release/kaji-ts-consumer-handoff-v1.schema.json +1289 -0
  33. package/contracts/release/publisher-identity-receipt-v1.schema.json +319 -0
  34. package/contracts/release/typescript-onboarding-evidence-v1.schema.json +740 -0
  35. package/contracts/tools/conformance-invalid.json +271 -0
  36. package/contracts/tools/conformance-valid.json +78 -0
  37. package/contracts/tools/tool-schema-v1.schema.json +18 -0
  38. package/dist/anthropic.cjs +1231 -0
  39. package/dist/anthropic.cjs.map +1 -0
  40. package/dist/anthropic.d.cts +26 -0
  41. package/dist/anthropic.d.ts +26 -0
  42. package/dist/anthropic.js +270 -0
  43. package/dist/anthropic.js.map +1 -0
  44. package/dist/auth.cjs +1507 -0
  45. package/dist/auth.cjs.map +1 -0
  46. package/dist/auth.d.cts +129 -0
  47. package/dist/auth.d.ts +129 -0
  48. package/dist/auth.js +1039 -0
  49. package/dist/auth.js.map +1 -0
  50. package/dist/base-B9FRMcP8.d.cts +140 -0
  51. package/dist/base-nHQd1VtS.d.ts +140 -0
  52. package/dist/chunk-AAM33KAO.js +4367 -0
  53. package/dist/chunk-AAM33KAO.js.map +1 -0
  54. package/dist/chunk-KAJ6BM64.js +153 -0
  55. package/dist/chunk-KAJ6BM64.js.map +1 -0
  56. package/dist/chunk-KCAXIOZS.js +308 -0
  57. package/dist/chunk-KCAXIOZS.js.map +1 -0
  58. package/dist/chunk-LSJ4AVO2.js +243 -0
  59. package/dist/chunk-LSJ4AVO2.js.map +1 -0
  60. package/dist/chunk-TM7ZGOJX.js +716 -0
  61. package/dist/chunk-TM7ZGOJX.js.map +1 -0
  62. package/dist/cli/bin.d.ts +2 -0
  63. package/dist/cli/bin.js +13 -0
  64. package/dist/cli/bin.js.map +1 -0
  65. package/dist/cli/chunk-2RCWPRVY.js +6277 -0
  66. package/dist/cli/chunk-2RCWPRVY.js.map +1 -0
  67. package/dist/cli/chunk-SEBX54TR.js +681 -0
  68. package/dist/cli/chunk-SEBX54TR.js.map +1 -0
  69. package/dist/cli/index.d.ts +232 -0
  70. package/dist/cli/index.js +11 -0
  71. package/dist/cli/index.js.map +1 -0
  72. package/dist/cli/init-worker.d.ts +2 -0
  73. package/dist/cli/init-worker.js +18 -0
  74. package/dist/cli/init-worker.js.map +1 -0
  75. package/dist/cli/integration-copy-worker.js +48 -0
  76. package/dist/cli/integration-copy-worker.js.map +1 -0
  77. package/dist/cli/package-entry-cjs.cjs +21 -0
  78. package/dist/cli/package-entry-cjs.cjs.map +1 -0
  79. package/dist/cli/package-entry-cjs.d.cts +2 -0
  80. package/dist/cli/package-entry.d.ts +2 -0
  81. package/dist/cli/package-entry.js +13 -0
  82. package/dist/cli/package-entry.js.map +1 -0
  83. package/dist/context-BaFHrQHv.d.cts +21 -0
  84. package/dist/context-BaFHrQHv.d.ts +21 -0
  85. package/dist/context-C-YPY-GS.d.cts +1538 -0
  86. package/dist/context-C-YPY-GS.d.ts +1538 -0
  87. package/dist/index.cjs +12346 -0
  88. package/dist/index.cjs.map +1 -0
  89. package/dist/index.d.cts +1560 -0
  90. package/dist/index.d.ts +1560 -0
  91. package/dist/index.js +7852 -0
  92. package/dist/index.js.map +1 -0
  93. package/dist/integrations/github.cjs +2092 -0
  94. package/dist/integrations/github.cjs.map +1 -0
  95. package/dist/integrations/github.d.cts +21 -0
  96. package/dist/integrations/github.d.ts +21 -0
  97. package/dist/integrations/github.js +2088 -0
  98. package/dist/integrations/github.js.map +1 -0
  99. package/dist/integrations.cjs +3370 -0
  100. package/dist/integrations.cjs.map +1 -0
  101. package/dist/integrations.d.cts +202 -0
  102. package/dist/integrations.d.ts +202 -0
  103. package/dist/integrations.js +2650 -0
  104. package/dist/integrations.js.map +1 -0
  105. package/dist/observability-Cj--OkME.d.cts +96 -0
  106. package/dist/observability-Cj--OkME.d.ts +96 -0
  107. package/dist/openai.cjs +1235 -0
  108. package/dist/openai.cjs.map +1 -0
  109. package/dist/openai.d.cts +32 -0
  110. package/dist/openai.d.ts +32 -0
  111. package/dist/openai.js +272 -0
  112. package/dist/openai.js.map +1 -0
  113. package/dist/testing.cjs +554 -0
  114. package/dist/testing.cjs.map +1 -0
  115. package/dist/testing.d.cts +43 -0
  116. package/dist/testing.d.ts +43 -0
  117. package/dist/testing.js +118 -0
  118. package/dist/testing.js.map +1 -0
  119. package/package.json +188 -0
  120. package/registry/echo/index.ts +53 -0
  121. package/registry/echo/manifest.json +53 -0
  122. package/registry/github/LICENSE +105 -0
  123. package/registry/github/client.ts +1727 -0
  124. package/registry/github/index.ts +263 -0
  125. package/registry/github/manifest.json +227 -0
  126. package/registry/github/owner-fixtures.json +10 -0
  127. package/registry/github/tests/github.test.ts +32 -0
  128. package/registry/gmail/LICENSE +105 -0
  129. package/registry/gmail/client.ts +548 -0
  130. package/registry/gmail/index.ts +165 -0
  131. package/registry/gmail/manifest.json +102 -0
  132. package/registry/gmail/owner-fixtures.json +10 -0
  133. package/registry/gmail/tests/gmail.test.ts +32 -0
  134. package/registry/index.json +21 -0
  135. package/registry/index.schema.json +37 -0
  136. package/registry/schema.json +119 -0
@@ -0,0 +1,1538 @@
1
+ import * as z from 'zod';
2
+ import { ErrorObject } from 'ajv/dist/2020.js';
3
+
4
+ type DeepReadonly<T> = T extends (...args: never[]) => unknown ? T : T extends readonly (infer TItem)[] ? readonly DeepReadonly<TItem>[] : T extends object ? {
5
+ readonly [TKey in keyof T]: DeepReadonly<T[TKey]>;
6
+ } : T;
7
+
8
+ /**
9
+ * Zod schemas for all Kaji events, mirroring
10
+ * `kaji.infra.events.schemas`. Field names stay snake_case because they are
11
+ * the shared wire format with the Python SDK.
12
+ *
13
+ * `BaseEvent` is `.strict()` to match Pydantic's `extra="forbid"`. The
14
+ * id/version/timestamp/metadata defaults mirror the Python `default_factory`
15
+ * fields, so constructing an event needs only its own payload plus session_id.
16
+ */
17
+
18
+ declare const SessionCreated: z.ZodObject<{
19
+ id: z.ZodDefault<z.ZodString>;
20
+ version: z.ZodDefault<z.ZodLiteral<"1.0">>;
21
+ timestamp: z.ZodDefault<z.ZodNumber>;
22
+ session_id: z.ZodString;
23
+ turn_id: z.ZodOptional<z.ZodString>;
24
+ metadata: z.ZodDefault<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
25
+ type: z.ZodLiteral<"session.created">;
26
+ }, z.core.$strict>;
27
+ declare const AgentTurnFailed: z.ZodObject<{
28
+ id: z.ZodDefault<z.ZodString>;
29
+ version: z.ZodDefault<z.ZodLiteral<"1.0">>;
30
+ timestamp: z.ZodDefault<z.ZodNumber>;
31
+ session_id: z.ZodString;
32
+ metadata: z.ZodDefault<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
33
+ type: z.ZodLiteral<"agent.turn.failed">;
34
+ turn_id: z.ZodString;
35
+ error: z.ZodString;
36
+ error_code: z.ZodOptional<z.ZodString>;
37
+ phase: z.ZodOptional<z.ZodEnum<{
38
+ queue: "queue";
39
+ provider_open: "provider_open";
40
+ provider_stream: "provider_stream";
41
+ approval: "approval";
42
+ tool: "tool";
43
+ }>>;
44
+ retryable: z.ZodOptional<z.ZodBoolean>;
45
+ outcome: z.ZodOptional<z.ZodEnum<{
46
+ not_started: "not_started";
47
+ failed: "failed";
48
+ unknown: "unknown";
49
+ }>>;
50
+ }, z.core.$strict>;
51
+ /** Discriminated union over every event, keyed on `type`. */
52
+ declare const KajiEvent: z.ZodDiscriminatedUnion<[z.ZodObject<{
53
+ id: z.ZodDefault<z.ZodString>;
54
+ version: z.ZodDefault<z.ZodLiteral<"1.0">>;
55
+ timestamp: z.ZodDefault<z.ZodNumber>;
56
+ session_id: z.ZodString;
57
+ turn_id: z.ZodOptional<z.ZodString>;
58
+ metadata: z.ZodDefault<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
59
+ type: z.ZodLiteral<"session.created">;
60
+ }, z.core.$strict>, z.ZodObject<{
61
+ id: z.ZodDefault<z.ZodString>;
62
+ version: z.ZodDefault<z.ZodLiteral<"1.0">>;
63
+ timestamp: z.ZodDefault<z.ZodNumber>;
64
+ session_id: z.ZodString;
65
+ turn_id: z.ZodOptional<z.ZodString>;
66
+ metadata: z.ZodDefault<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
67
+ type: z.ZodLiteral<"session.closed">;
68
+ reason: z.ZodOptional<z.ZodNullable<z.ZodString>>;
69
+ }, z.core.$strict>, z.ZodObject<{
70
+ id: z.ZodDefault<z.ZodString>;
71
+ version: z.ZodDefault<z.ZodLiteral<"1.0">>;
72
+ timestamp: z.ZodDefault<z.ZodNumber>;
73
+ session_id: z.ZodString;
74
+ turn_id: z.ZodOptional<z.ZodString>;
75
+ metadata: z.ZodDefault<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
76
+ type: z.ZodLiteral<"user.message">;
77
+ content: z.ZodString;
78
+ }, z.core.$strict>, z.ZodObject<{
79
+ id: z.ZodDefault<z.ZodString>;
80
+ version: z.ZodDefault<z.ZodLiteral<"1.0">>;
81
+ timestamp: z.ZodDefault<z.ZodNumber>;
82
+ session_id: z.ZodString;
83
+ turn_id: z.ZodOptional<z.ZodString>;
84
+ metadata: z.ZodDefault<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
85
+ type: z.ZodLiteral<"user.audio.chunk">;
86
+ chunk_size_bytes: z.ZodNumber;
87
+ }, z.core.$strict>, z.ZodObject<{
88
+ id: z.ZodDefault<z.ZodString>;
89
+ version: z.ZodDefault<z.ZodLiteral<"1.0">>;
90
+ timestamp: z.ZodDefault<z.ZodNumber>;
91
+ session_id: z.ZodString;
92
+ turn_id: z.ZodOptional<z.ZodString>;
93
+ metadata: z.ZodDefault<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
94
+ type: z.ZodLiteral<"transcript.partial">;
95
+ text: z.ZodString;
96
+ }, z.core.$strict>, z.ZodObject<{
97
+ id: z.ZodDefault<z.ZodString>;
98
+ version: z.ZodDefault<z.ZodLiteral<"1.0">>;
99
+ timestamp: z.ZodDefault<z.ZodNumber>;
100
+ session_id: z.ZodString;
101
+ turn_id: z.ZodOptional<z.ZodString>;
102
+ metadata: z.ZodDefault<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
103
+ type: z.ZodLiteral<"transcript.final">;
104
+ text: z.ZodString;
105
+ }, z.core.$strict>, z.ZodObject<{
106
+ id: z.ZodDefault<z.ZodString>;
107
+ version: z.ZodDefault<z.ZodLiteral<"1.0">>;
108
+ timestamp: z.ZodDefault<z.ZodNumber>;
109
+ session_id: z.ZodString;
110
+ turn_id: z.ZodOptional<z.ZodString>;
111
+ metadata: z.ZodDefault<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
112
+ type: z.ZodLiteral<"memory.retrieval.started">;
113
+ query: z.ZodString;
114
+ }, z.core.$strict>, z.ZodObject<{
115
+ id: z.ZodDefault<z.ZodString>;
116
+ version: z.ZodDefault<z.ZodLiteral<"1.0">>;
117
+ timestamp: z.ZodDefault<z.ZodNumber>;
118
+ session_id: z.ZodString;
119
+ turn_id: z.ZodOptional<z.ZodString>;
120
+ metadata: z.ZodDefault<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
121
+ type: z.ZodLiteral<"memory.retrieval.completed">;
122
+ query: z.ZodString;
123
+ documents: z.ZodArray<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
124
+ }, z.core.$strict>, z.ZodObject<{
125
+ id: z.ZodDefault<z.ZodString>;
126
+ version: z.ZodDefault<z.ZodLiteral<"1.0">>;
127
+ timestamp: z.ZodDefault<z.ZodNumber>;
128
+ session_id: z.ZodString;
129
+ turn_id: z.ZodOptional<z.ZodString>;
130
+ metadata: z.ZodDefault<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
131
+ type: z.ZodLiteral<"agent.reasoning.started">;
132
+ }, z.core.$strict>, z.ZodObject<{
133
+ id: z.ZodDefault<z.ZodString>;
134
+ version: z.ZodDefault<z.ZodLiteral<"1.0">>;
135
+ timestamp: z.ZodDefault<z.ZodNumber>;
136
+ session_id: z.ZodString;
137
+ turn_id: z.ZodOptional<z.ZodString>;
138
+ metadata: z.ZodDefault<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
139
+ type: z.ZodLiteral<"agent.message.delta">;
140
+ delta: z.ZodString;
141
+ }, z.core.$strict>, z.ZodObject<{
142
+ id: z.ZodDefault<z.ZodString>;
143
+ version: z.ZodDefault<z.ZodLiteral<"1.0">>;
144
+ timestamp: z.ZodDefault<z.ZodNumber>;
145
+ session_id: z.ZodString;
146
+ turn_id: z.ZodOptional<z.ZodString>;
147
+ metadata: z.ZodDefault<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
148
+ type: z.ZodLiteral<"agent.message.completed">;
149
+ content: z.ZodString;
150
+ tokens: z.ZodOptional<z.ZodNullable<z.ZodObject<{
151
+ input: z.ZodNumber;
152
+ output: z.ZodNumber;
153
+ }, z.core.$strict>>>;
154
+ cost_usd: z.ZodOptional<z.ZodNullable<z.ZodNumber>>;
155
+ }, z.core.$strict>, z.ZodObject<{
156
+ id: z.ZodDefault<z.ZodString>;
157
+ version: z.ZodDefault<z.ZodLiteral<"1.0">>;
158
+ timestamp: z.ZodDefault<z.ZodNumber>;
159
+ session_id: z.ZodString;
160
+ turn_id: z.ZodOptional<z.ZodString>;
161
+ metadata: z.ZodDefault<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
162
+ type: z.ZodLiteral<"agent.turn.exhausted">;
163
+ max_iterations: z.ZodNumber;
164
+ pending_tool_calls: z.ZodArray<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
165
+ reason: z.ZodOptional<z.ZodNullable<z.ZodString>>;
166
+ }, z.core.$strict>, z.ZodObject<{
167
+ id: z.ZodDefault<z.ZodString>;
168
+ version: z.ZodDefault<z.ZodLiteral<"1.0">>;
169
+ timestamp: z.ZodDefault<z.ZodNumber>;
170
+ session_id: z.ZodString;
171
+ metadata: z.ZodDefault<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
172
+ type: z.ZodLiteral<"agent.turn.failed">;
173
+ turn_id: z.ZodString;
174
+ error: z.ZodString;
175
+ error_code: z.ZodOptional<z.ZodString>;
176
+ phase: z.ZodOptional<z.ZodEnum<{
177
+ queue: "queue";
178
+ provider_open: "provider_open";
179
+ provider_stream: "provider_stream";
180
+ approval: "approval";
181
+ tool: "tool";
182
+ }>>;
183
+ retryable: z.ZodOptional<z.ZodBoolean>;
184
+ outcome: z.ZodOptional<z.ZodEnum<{
185
+ not_started: "not_started";
186
+ failed: "failed";
187
+ unknown: "unknown";
188
+ }>>;
189
+ }, z.core.$strict>, z.ZodObject<{
190
+ id: z.ZodDefault<z.ZodString>;
191
+ version: z.ZodDefault<z.ZodLiteral<"1.0">>;
192
+ timestamp: z.ZodDefault<z.ZodNumber>;
193
+ session_id: z.ZodString;
194
+ metadata: z.ZodDefault<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
195
+ type: z.ZodLiteral<"tool.call.requested">;
196
+ turn_id: z.ZodString;
197
+ tool_name: z.ZodString;
198
+ tool_args: z.ZodRecord<z.ZodString, z.ZodUnknown>;
199
+ tool_call_id: z.ZodString;
200
+ }, z.core.$strict>, z.ZodObject<{
201
+ id: z.ZodDefault<z.ZodString>;
202
+ version: z.ZodDefault<z.ZodLiteral<"1.0">>;
203
+ timestamp: z.ZodDefault<z.ZodNumber>;
204
+ session_id: z.ZodString;
205
+ metadata: z.ZodDefault<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
206
+ type: z.ZodLiteral<"tool.call.started">;
207
+ turn_id: z.ZodString;
208
+ tool_name: z.ZodString;
209
+ tool_call_id: z.ZodString;
210
+ }, z.core.$strict>, z.ZodObject<{
211
+ id: z.ZodDefault<z.ZodString>;
212
+ version: z.ZodDefault<z.ZodLiteral<"1.0">>;
213
+ timestamp: z.ZodDefault<z.ZodNumber>;
214
+ session_id: z.ZodString;
215
+ metadata: z.ZodDefault<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
216
+ type: z.ZodLiteral<"tool.call.completed">;
217
+ turn_id: z.ZodString;
218
+ tool_name: z.ZodString;
219
+ tool_call_id: z.ZodString;
220
+ result: z.ZodUnknown;
221
+ tokens: z.ZodOptional<z.ZodNullable<z.ZodObject<{
222
+ input: z.ZodNumber;
223
+ output: z.ZodNumber;
224
+ }, z.core.$strict>>>;
225
+ cost_usd: z.ZodOptional<z.ZodNullable<z.ZodNumber>>;
226
+ }, z.core.$strict>, z.ZodObject<{
227
+ id: z.ZodDefault<z.ZodString>;
228
+ version: z.ZodDefault<z.ZodLiteral<"1.0">>;
229
+ timestamp: z.ZodDefault<z.ZodNumber>;
230
+ session_id: z.ZodString;
231
+ metadata: z.ZodDefault<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
232
+ type: z.ZodLiteral<"tool.call.failed">;
233
+ turn_id: z.ZodString;
234
+ tool_name: z.ZodString;
235
+ tool_call_id: z.ZodString;
236
+ error: z.ZodString;
237
+ error_code: z.ZodOptional<z.ZodString>;
238
+ error_path: z.ZodOptional<z.ZodString>;
239
+ retryable: z.ZodOptional<z.ZodBoolean>;
240
+ outcome: z.ZodOptional<z.ZodEnum<{
241
+ not_started: "not_started";
242
+ failed: "failed";
243
+ unknown: "unknown";
244
+ }>>;
245
+ reason_code: z.ZodOptional<z.ZodString>;
246
+ recovery_code: z.ZodOptional<z.ZodString>;
247
+ doc_url: z.ZodOptional<z.ZodString>;
248
+ }, z.core.$strict>, z.ZodObject<{
249
+ id: z.ZodDefault<z.ZodString>;
250
+ version: z.ZodDefault<z.ZodLiteral<"1.0">>;
251
+ timestamp: z.ZodDefault<z.ZodNumber>;
252
+ session_id: z.ZodString;
253
+ metadata: z.ZodDefault<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
254
+ type: z.ZodLiteral<"tool.approval.requested">;
255
+ turn_id: z.ZodString;
256
+ tool_name: z.ZodString;
257
+ tool_call_id: z.ZodString;
258
+ tool_args: z.ZodRecord<z.ZodString, z.ZodUnknown>;
259
+ risk: z.ZodEnum<{
260
+ read: "read";
261
+ write: "write";
262
+ external_effect: "external_effect";
263
+ destructive: "destructive";
264
+ admin: "admin";
265
+ }>;
266
+ }, z.core.$strict>, z.ZodObject<{
267
+ id: z.ZodDefault<z.ZodString>;
268
+ version: z.ZodDefault<z.ZodLiteral<"1.0">>;
269
+ timestamp: z.ZodDefault<z.ZodNumber>;
270
+ session_id: z.ZodString;
271
+ metadata: z.ZodDefault<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
272
+ type: z.ZodLiteral<"tool.approval.approved">;
273
+ turn_id: z.ZodString;
274
+ tool_name: z.ZodString;
275
+ tool_call_id: z.ZodString;
276
+ }, z.core.$strict>, z.ZodObject<{
277
+ id: z.ZodDefault<z.ZodString>;
278
+ version: z.ZodDefault<z.ZodLiteral<"1.0">>;
279
+ timestamp: z.ZodDefault<z.ZodNumber>;
280
+ session_id: z.ZodString;
281
+ metadata: z.ZodDefault<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
282
+ type: z.ZodLiteral<"tool.approval.rejected">;
283
+ turn_id: z.ZodString;
284
+ tool_name: z.ZodString;
285
+ tool_call_id: z.ZodString;
286
+ error_code: z.ZodEnum<{
287
+ APPROVAL_REJECTED: "APPROVAL_REJECTED";
288
+ APPROVAL_TIMEOUT: "APPROVAL_TIMEOUT";
289
+ TURN_TIMEOUT: "TURN_TIMEOUT";
290
+ TOOL_CANCELLED: "TOOL_CANCELLED";
291
+ APPROVAL_UNAVAILABLE: "APPROVAL_UNAVAILABLE";
292
+ }>;
293
+ reason: z.ZodString;
294
+ }, z.core.$strict>, z.ZodObject<{
295
+ id: z.ZodDefault<z.ZodString>;
296
+ version: z.ZodDefault<z.ZodLiteral<"1.0">>;
297
+ timestamp: z.ZodDefault<z.ZodNumber>;
298
+ session_id: z.ZodString;
299
+ turn_id: z.ZodOptional<z.ZodString>;
300
+ metadata: z.ZodDefault<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
301
+ type: z.ZodLiteral<"workflow.started">;
302
+ workflow_name: z.ZodString;
303
+ }, z.core.$strict>, z.ZodObject<{
304
+ id: z.ZodDefault<z.ZodString>;
305
+ version: z.ZodDefault<z.ZodLiteral<"1.0">>;
306
+ timestamp: z.ZodDefault<z.ZodNumber>;
307
+ session_id: z.ZodString;
308
+ turn_id: z.ZodOptional<z.ZodString>;
309
+ metadata: z.ZodDefault<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
310
+ type: z.ZodLiteral<"workflow.completed">;
311
+ workflow_name: z.ZodString;
312
+ result: z.ZodUnknown;
313
+ }, z.core.$strict>, z.ZodObject<{
314
+ id: z.ZodDefault<z.ZodString>;
315
+ version: z.ZodDefault<z.ZodLiteral<"1.0">>;
316
+ timestamp: z.ZodDefault<z.ZodNumber>;
317
+ session_id: z.ZodString;
318
+ turn_id: z.ZodOptional<z.ZodString>;
319
+ metadata: z.ZodDefault<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
320
+ type: z.ZodLiteral<"workflow.failed">;
321
+ workflow_name: z.ZodString;
322
+ error: z.ZodString;
323
+ }, z.core.$strict>, z.ZodObject<{
324
+ id: z.ZodDefault<z.ZodString>;
325
+ version: z.ZodDefault<z.ZodLiteral<"1.0">>;
326
+ timestamp: z.ZodDefault<z.ZodNumber>;
327
+ session_id: z.ZodString;
328
+ turn_id: z.ZodOptional<z.ZodString>;
329
+ metadata: z.ZodDefault<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
330
+ type: z.ZodLiteral<"cancellation.requested">;
331
+ reason: z.ZodString;
332
+ }, z.core.$strict>, z.ZodObject<{
333
+ id: z.ZodDefault<z.ZodString>;
334
+ version: z.ZodDefault<z.ZodLiteral<"1.0">>;
335
+ timestamp: z.ZodDefault<z.ZodNumber>;
336
+ session_id: z.ZodString;
337
+ turn_id: z.ZodOptional<z.ZodString>;
338
+ metadata: z.ZodDefault<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
339
+ type: z.ZodLiteral<"cancellation.completed">;
340
+ }, z.core.$strict>], "type">;
341
+ /** A validated Kaji event (output type, with defaults applied). */
342
+ type KajiEvent = z.infer<typeof KajiEvent>;
343
+ /** The input type accepted by `KajiEvent.parse` (defaults optional). */
344
+ type KajiEventInput = z.input<typeof KajiEvent>;
345
+ /** Base event fields, useful for typing helpers that touch any event. */
346
+ type BaseEvent = z.infer<typeof SessionCreated>;
347
+ /** An event draft accepted by stores and committers. Drafts never carry sequence. */
348
+ declare const NewKajiEvent: z.ZodDiscriminatedUnion<[z.ZodObject<{
349
+ id: z.ZodDefault<z.ZodString>;
350
+ version: z.ZodDefault<z.ZodLiteral<"1.0">>;
351
+ timestamp: z.ZodDefault<z.ZodNumber>;
352
+ session_id: z.ZodString;
353
+ turn_id: z.ZodOptional<z.ZodString>;
354
+ metadata: z.ZodDefault<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
355
+ type: z.ZodLiteral<"session.created">;
356
+ }, z.core.$strict>, z.ZodObject<{
357
+ id: z.ZodDefault<z.ZodString>;
358
+ version: z.ZodDefault<z.ZodLiteral<"1.0">>;
359
+ timestamp: z.ZodDefault<z.ZodNumber>;
360
+ session_id: z.ZodString;
361
+ turn_id: z.ZodOptional<z.ZodString>;
362
+ metadata: z.ZodDefault<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
363
+ type: z.ZodLiteral<"session.closed">;
364
+ reason: z.ZodOptional<z.ZodNullable<z.ZodString>>;
365
+ }, z.core.$strict>, z.ZodObject<{
366
+ id: z.ZodDefault<z.ZodString>;
367
+ version: z.ZodDefault<z.ZodLiteral<"1.0">>;
368
+ timestamp: z.ZodDefault<z.ZodNumber>;
369
+ session_id: z.ZodString;
370
+ turn_id: z.ZodOptional<z.ZodString>;
371
+ metadata: z.ZodDefault<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
372
+ type: z.ZodLiteral<"user.message">;
373
+ content: z.ZodString;
374
+ }, z.core.$strict>, z.ZodObject<{
375
+ id: z.ZodDefault<z.ZodString>;
376
+ version: z.ZodDefault<z.ZodLiteral<"1.0">>;
377
+ timestamp: z.ZodDefault<z.ZodNumber>;
378
+ session_id: z.ZodString;
379
+ turn_id: z.ZodOptional<z.ZodString>;
380
+ metadata: z.ZodDefault<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
381
+ type: z.ZodLiteral<"user.audio.chunk">;
382
+ chunk_size_bytes: z.ZodNumber;
383
+ }, z.core.$strict>, z.ZodObject<{
384
+ id: z.ZodDefault<z.ZodString>;
385
+ version: z.ZodDefault<z.ZodLiteral<"1.0">>;
386
+ timestamp: z.ZodDefault<z.ZodNumber>;
387
+ session_id: z.ZodString;
388
+ turn_id: z.ZodOptional<z.ZodString>;
389
+ metadata: z.ZodDefault<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
390
+ type: z.ZodLiteral<"transcript.partial">;
391
+ text: z.ZodString;
392
+ }, z.core.$strict>, z.ZodObject<{
393
+ id: z.ZodDefault<z.ZodString>;
394
+ version: z.ZodDefault<z.ZodLiteral<"1.0">>;
395
+ timestamp: z.ZodDefault<z.ZodNumber>;
396
+ session_id: z.ZodString;
397
+ turn_id: z.ZodOptional<z.ZodString>;
398
+ metadata: z.ZodDefault<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
399
+ type: z.ZodLiteral<"transcript.final">;
400
+ text: z.ZodString;
401
+ }, z.core.$strict>, z.ZodObject<{
402
+ id: z.ZodDefault<z.ZodString>;
403
+ version: z.ZodDefault<z.ZodLiteral<"1.0">>;
404
+ timestamp: z.ZodDefault<z.ZodNumber>;
405
+ session_id: z.ZodString;
406
+ turn_id: z.ZodOptional<z.ZodString>;
407
+ metadata: z.ZodDefault<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
408
+ type: z.ZodLiteral<"memory.retrieval.started">;
409
+ query: z.ZodString;
410
+ }, z.core.$strict>, z.ZodObject<{
411
+ id: z.ZodDefault<z.ZodString>;
412
+ version: z.ZodDefault<z.ZodLiteral<"1.0">>;
413
+ timestamp: z.ZodDefault<z.ZodNumber>;
414
+ session_id: z.ZodString;
415
+ turn_id: z.ZodOptional<z.ZodString>;
416
+ metadata: z.ZodDefault<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
417
+ type: z.ZodLiteral<"memory.retrieval.completed">;
418
+ query: z.ZodString;
419
+ documents: z.ZodArray<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
420
+ }, z.core.$strict>, z.ZodObject<{
421
+ id: z.ZodDefault<z.ZodString>;
422
+ version: z.ZodDefault<z.ZodLiteral<"1.0">>;
423
+ timestamp: z.ZodDefault<z.ZodNumber>;
424
+ session_id: z.ZodString;
425
+ turn_id: z.ZodOptional<z.ZodString>;
426
+ metadata: z.ZodDefault<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
427
+ type: z.ZodLiteral<"agent.reasoning.started">;
428
+ }, z.core.$strict>, z.ZodObject<{
429
+ id: z.ZodDefault<z.ZodString>;
430
+ version: z.ZodDefault<z.ZodLiteral<"1.0">>;
431
+ timestamp: z.ZodDefault<z.ZodNumber>;
432
+ session_id: z.ZodString;
433
+ turn_id: z.ZodOptional<z.ZodString>;
434
+ metadata: z.ZodDefault<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
435
+ type: z.ZodLiteral<"agent.message.delta">;
436
+ delta: z.ZodString;
437
+ }, z.core.$strict>, z.ZodObject<{
438
+ id: z.ZodDefault<z.ZodString>;
439
+ version: z.ZodDefault<z.ZodLiteral<"1.0">>;
440
+ timestamp: z.ZodDefault<z.ZodNumber>;
441
+ session_id: z.ZodString;
442
+ turn_id: z.ZodOptional<z.ZodString>;
443
+ metadata: z.ZodDefault<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
444
+ type: z.ZodLiteral<"agent.message.completed">;
445
+ content: z.ZodString;
446
+ tokens: z.ZodOptional<z.ZodNullable<z.ZodObject<{
447
+ input: z.ZodNumber;
448
+ output: z.ZodNumber;
449
+ }, z.core.$strict>>>;
450
+ cost_usd: z.ZodOptional<z.ZodNullable<z.ZodNumber>>;
451
+ }, z.core.$strict>, z.ZodObject<{
452
+ id: z.ZodDefault<z.ZodString>;
453
+ version: z.ZodDefault<z.ZodLiteral<"1.0">>;
454
+ timestamp: z.ZodDefault<z.ZodNumber>;
455
+ session_id: z.ZodString;
456
+ turn_id: z.ZodOptional<z.ZodString>;
457
+ metadata: z.ZodDefault<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
458
+ type: z.ZodLiteral<"agent.turn.exhausted">;
459
+ max_iterations: z.ZodNumber;
460
+ pending_tool_calls: z.ZodArray<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
461
+ reason: z.ZodOptional<z.ZodNullable<z.ZodString>>;
462
+ }, z.core.$strict>, z.ZodObject<{
463
+ id: z.ZodDefault<z.ZodString>;
464
+ version: z.ZodDefault<z.ZodLiteral<"1.0">>;
465
+ timestamp: z.ZodDefault<z.ZodNumber>;
466
+ session_id: z.ZodString;
467
+ metadata: z.ZodDefault<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
468
+ type: z.ZodLiteral<"agent.turn.failed">;
469
+ turn_id: z.ZodString;
470
+ error: z.ZodString;
471
+ error_code: z.ZodOptional<z.ZodString>;
472
+ phase: z.ZodOptional<z.ZodEnum<{
473
+ queue: "queue";
474
+ provider_open: "provider_open";
475
+ provider_stream: "provider_stream";
476
+ approval: "approval";
477
+ tool: "tool";
478
+ }>>;
479
+ retryable: z.ZodOptional<z.ZodBoolean>;
480
+ outcome: z.ZodOptional<z.ZodEnum<{
481
+ not_started: "not_started";
482
+ failed: "failed";
483
+ unknown: "unknown";
484
+ }>>;
485
+ }, z.core.$strict>, z.ZodObject<{
486
+ id: z.ZodDefault<z.ZodString>;
487
+ version: z.ZodDefault<z.ZodLiteral<"1.0">>;
488
+ timestamp: z.ZodDefault<z.ZodNumber>;
489
+ session_id: z.ZodString;
490
+ metadata: z.ZodDefault<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
491
+ type: z.ZodLiteral<"tool.call.requested">;
492
+ turn_id: z.ZodString;
493
+ tool_name: z.ZodString;
494
+ tool_args: z.ZodRecord<z.ZodString, z.ZodUnknown>;
495
+ tool_call_id: z.ZodString;
496
+ }, z.core.$strict>, z.ZodObject<{
497
+ id: z.ZodDefault<z.ZodString>;
498
+ version: z.ZodDefault<z.ZodLiteral<"1.0">>;
499
+ timestamp: z.ZodDefault<z.ZodNumber>;
500
+ session_id: z.ZodString;
501
+ metadata: z.ZodDefault<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
502
+ type: z.ZodLiteral<"tool.call.started">;
503
+ turn_id: z.ZodString;
504
+ tool_name: z.ZodString;
505
+ tool_call_id: z.ZodString;
506
+ }, z.core.$strict>, z.ZodObject<{
507
+ id: z.ZodDefault<z.ZodString>;
508
+ version: z.ZodDefault<z.ZodLiteral<"1.0">>;
509
+ timestamp: z.ZodDefault<z.ZodNumber>;
510
+ session_id: z.ZodString;
511
+ metadata: z.ZodDefault<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
512
+ type: z.ZodLiteral<"tool.call.completed">;
513
+ turn_id: z.ZodString;
514
+ tool_name: z.ZodString;
515
+ tool_call_id: z.ZodString;
516
+ result: z.ZodUnknown;
517
+ tokens: z.ZodOptional<z.ZodNullable<z.ZodObject<{
518
+ input: z.ZodNumber;
519
+ output: z.ZodNumber;
520
+ }, z.core.$strict>>>;
521
+ cost_usd: z.ZodOptional<z.ZodNullable<z.ZodNumber>>;
522
+ }, z.core.$strict>, z.ZodObject<{
523
+ id: z.ZodDefault<z.ZodString>;
524
+ version: z.ZodDefault<z.ZodLiteral<"1.0">>;
525
+ timestamp: z.ZodDefault<z.ZodNumber>;
526
+ session_id: z.ZodString;
527
+ metadata: z.ZodDefault<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
528
+ type: z.ZodLiteral<"tool.call.failed">;
529
+ turn_id: z.ZodString;
530
+ tool_name: z.ZodString;
531
+ tool_call_id: z.ZodString;
532
+ error: z.ZodString;
533
+ error_code: z.ZodOptional<z.ZodString>;
534
+ error_path: z.ZodOptional<z.ZodString>;
535
+ retryable: z.ZodOptional<z.ZodBoolean>;
536
+ outcome: z.ZodOptional<z.ZodEnum<{
537
+ not_started: "not_started";
538
+ failed: "failed";
539
+ unknown: "unknown";
540
+ }>>;
541
+ reason_code: z.ZodOptional<z.ZodString>;
542
+ recovery_code: z.ZodOptional<z.ZodString>;
543
+ doc_url: z.ZodOptional<z.ZodString>;
544
+ }, z.core.$strict>, z.ZodObject<{
545
+ id: z.ZodDefault<z.ZodString>;
546
+ version: z.ZodDefault<z.ZodLiteral<"1.0">>;
547
+ timestamp: z.ZodDefault<z.ZodNumber>;
548
+ session_id: z.ZodString;
549
+ metadata: z.ZodDefault<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
550
+ type: z.ZodLiteral<"tool.approval.requested">;
551
+ turn_id: z.ZodString;
552
+ tool_name: z.ZodString;
553
+ tool_call_id: z.ZodString;
554
+ tool_args: z.ZodRecord<z.ZodString, z.ZodUnknown>;
555
+ risk: z.ZodEnum<{
556
+ read: "read";
557
+ write: "write";
558
+ external_effect: "external_effect";
559
+ destructive: "destructive";
560
+ admin: "admin";
561
+ }>;
562
+ }, z.core.$strict>, z.ZodObject<{
563
+ id: z.ZodDefault<z.ZodString>;
564
+ version: z.ZodDefault<z.ZodLiteral<"1.0">>;
565
+ timestamp: z.ZodDefault<z.ZodNumber>;
566
+ session_id: z.ZodString;
567
+ metadata: z.ZodDefault<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
568
+ type: z.ZodLiteral<"tool.approval.approved">;
569
+ turn_id: z.ZodString;
570
+ tool_name: z.ZodString;
571
+ tool_call_id: z.ZodString;
572
+ }, z.core.$strict>, z.ZodObject<{
573
+ id: z.ZodDefault<z.ZodString>;
574
+ version: z.ZodDefault<z.ZodLiteral<"1.0">>;
575
+ timestamp: z.ZodDefault<z.ZodNumber>;
576
+ session_id: z.ZodString;
577
+ metadata: z.ZodDefault<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
578
+ type: z.ZodLiteral<"tool.approval.rejected">;
579
+ turn_id: z.ZodString;
580
+ tool_name: z.ZodString;
581
+ tool_call_id: z.ZodString;
582
+ error_code: z.ZodEnum<{
583
+ APPROVAL_REJECTED: "APPROVAL_REJECTED";
584
+ APPROVAL_TIMEOUT: "APPROVAL_TIMEOUT";
585
+ TURN_TIMEOUT: "TURN_TIMEOUT";
586
+ TOOL_CANCELLED: "TOOL_CANCELLED";
587
+ APPROVAL_UNAVAILABLE: "APPROVAL_UNAVAILABLE";
588
+ }>;
589
+ reason: z.ZodString;
590
+ }, z.core.$strict>, z.ZodObject<{
591
+ id: z.ZodDefault<z.ZodString>;
592
+ version: z.ZodDefault<z.ZodLiteral<"1.0">>;
593
+ timestamp: z.ZodDefault<z.ZodNumber>;
594
+ session_id: z.ZodString;
595
+ turn_id: z.ZodOptional<z.ZodString>;
596
+ metadata: z.ZodDefault<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
597
+ type: z.ZodLiteral<"workflow.started">;
598
+ workflow_name: z.ZodString;
599
+ }, z.core.$strict>, z.ZodObject<{
600
+ id: z.ZodDefault<z.ZodString>;
601
+ version: z.ZodDefault<z.ZodLiteral<"1.0">>;
602
+ timestamp: z.ZodDefault<z.ZodNumber>;
603
+ session_id: z.ZodString;
604
+ turn_id: z.ZodOptional<z.ZodString>;
605
+ metadata: z.ZodDefault<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
606
+ type: z.ZodLiteral<"workflow.completed">;
607
+ workflow_name: z.ZodString;
608
+ result: z.ZodUnknown;
609
+ }, z.core.$strict>, z.ZodObject<{
610
+ id: z.ZodDefault<z.ZodString>;
611
+ version: z.ZodDefault<z.ZodLiteral<"1.0">>;
612
+ timestamp: z.ZodDefault<z.ZodNumber>;
613
+ session_id: z.ZodString;
614
+ turn_id: z.ZodOptional<z.ZodString>;
615
+ metadata: z.ZodDefault<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
616
+ type: z.ZodLiteral<"workflow.failed">;
617
+ workflow_name: z.ZodString;
618
+ error: z.ZodString;
619
+ }, z.core.$strict>, z.ZodObject<{
620
+ id: z.ZodDefault<z.ZodString>;
621
+ version: z.ZodDefault<z.ZodLiteral<"1.0">>;
622
+ timestamp: z.ZodDefault<z.ZodNumber>;
623
+ session_id: z.ZodString;
624
+ turn_id: z.ZodOptional<z.ZodString>;
625
+ metadata: z.ZodDefault<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
626
+ type: z.ZodLiteral<"cancellation.requested">;
627
+ reason: z.ZodString;
628
+ }, z.core.$strict>, z.ZodObject<{
629
+ id: z.ZodDefault<z.ZodString>;
630
+ version: z.ZodDefault<z.ZodLiteral<"1.0">>;
631
+ timestamp: z.ZodDefault<z.ZodNumber>;
632
+ session_id: z.ZodString;
633
+ turn_id: z.ZodOptional<z.ZodString>;
634
+ metadata: z.ZodDefault<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
635
+ type: z.ZodLiteral<"cancellation.completed">;
636
+ }, z.core.$strict>], "type">;
637
+ type NewKajiEvent = KajiEvent;
638
+ /** A persisted event. Stores assign a positive session-local sequence. */
639
+ type StoredKajiEvent = DeepReadonly<KajiEvent & {
640
+ sequence: number;
641
+ }>;
642
+ declare const StoredKajiEvent: z.ZodPipe<z.ZodObject<{
643
+ sequence: z.ZodNumber;
644
+ }, z.core.$loose>, z.ZodTransform<{
645
+ readonly id: string;
646
+ readonly version: "1.0";
647
+ readonly timestamp: number;
648
+ readonly session_id: string;
649
+ readonly metadata: {
650
+ readonly [x: string]: unknown;
651
+ };
652
+ readonly type: "tool.call.failed";
653
+ readonly turn_id: string;
654
+ readonly tool_name: string;
655
+ readonly tool_call_id: string;
656
+ readonly error: string;
657
+ readonly error_code?: string | undefined;
658
+ readonly error_path?: string | undefined;
659
+ readonly retryable?: boolean | undefined;
660
+ readonly outcome?: "not_started" | "failed" | "unknown" | undefined;
661
+ readonly reason_code?: string | undefined;
662
+ readonly recovery_code?: string | undefined;
663
+ readonly doc_url?: string | undefined;
664
+ readonly sequence: number;
665
+ } | {
666
+ readonly id: string;
667
+ readonly version: "1.0";
668
+ readonly timestamp: number;
669
+ readonly session_id: string;
670
+ readonly metadata: {
671
+ readonly [x: string]: unknown;
672
+ };
673
+ readonly type: "session.created";
674
+ readonly turn_id?: string | undefined;
675
+ readonly sequence: number;
676
+ } | {
677
+ readonly id: string;
678
+ readonly version: "1.0";
679
+ readonly timestamp: number;
680
+ readonly session_id: string;
681
+ readonly metadata: {
682
+ readonly [x: string]: unknown;
683
+ };
684
+ readonly type: "session.closed";
685
+ readonly turn_id?: string | undefined;
686
+ readonly reason?: string | null | undefined;
687
+ readonly sequence: number;
688
+ } | {
689
+ readonly id: string;
690
+ readonly version: "1.0";
691
+ readonly timestamp: number;
692
+ readonly session_id: string;
693
+ readonly metadata: {
694
+ readonly [x: string]: unknown;
695
+ };
696
+ readonly type: "user.message";
697
+ readonly content: string;
698
+ readonly turn_id?: string | undefined;
699
+ readonly sequence: number;
700
+ } | {
701
+ readonly id: string;
702
+ readonly version: "1.0";
703
+ readonly timestamp: number;
704
+ readonly session_id: string;
705
+ readonly metadata: {
706
+ readonly [x: string]: unknown;
707
+ };
708
+ readonly type: "user.audio.chunk";
709
+ readonly chunk_size_bytes: number;
710
+ readonly turn_id?: string | undefined;
711
+ readonly sequence: number;
712
+ } | {
713
+ readonly id: string;
714
+ readonly version: "1.0";
715
+ readonly timestamp: number;
716
+ readonly session_id: string;
717
+ readonly metadata: {
718
+ readonly [x: string]: unknown;
719
+ };
720
+ readonly type: "transcript.partial";
721
+ readonly text: string;
722
+ readonly turn_id?: string | undefined;
723
+ readonly sequence: number;
724
+ } | {
725
+ readonly id: string;
726
+ readonly version: "1.0";
727
+ readonly timestamp: number;
728
+ readonly session_id: string;
729
+ readonly metadata: {
730
+ readonly [x: string]: unknown;
731
+ };
732
+ readonly type: "transcript.final";
733
+ readonly text: string;
734
+ readonly turn_id?: string | undefined;
735
+ readonly sequence: number;
736
+ } | {
737
+ readonly id: string;
738
+ readonly version: "1.0";
739
+ readonly timestamp: number;
740
+ readonly session_id: string;
741
+ readonly metadata: {
742
+ readonly [x: string]: unknown;
743
+ };
744
+ readonly type: "memory.retrieval.started";
745
+ readonly query: string;
746
+ readonly turn_id?: string | undefined;
747
+ readonly sequence: number;
748
+ } | {
749
+ readonly id: string;
750
+ readonly version: "1.0";
751
+ readonly timestamp: number;
752
+ readonly session_id: string;
753
+ readonly metadata: {
754
+ readonly [x: string]: unknown;
755
+ };
756
+ readonly type: "memory.retrieval.completed";
757
+ readonly query: string;
758
+ readonly documents: readonly {
759
+ readonly [x: string]: unknown;
760
+ }[];
761
+ readonly turn_id?: string | undefined;
762
+ readonly sequence: number;
763
+ } | {
764
+ readonly id: string;
765
+ readonly version: "1.0";
766
+ readonly timestamp: number;
767
+ readonly session_id: string;
768
+ readonly metadata: {
769
+ readonly [x: string]: unknown;
770
+ };
771
+ readonly type: "agent.reasoning.started";
772
+ readonly turn_id?: string | undefined;
773
+ readonly sequence: number;
774
+ } | {
775
+ readonly id: string;
776
+ readonly version: "1.0";
777
+ readonly timestamp: number;
778
+ readonly session_id: string;
779
+ readonly metadata: {
780
+ readonly [x: string]: unknown;
781
+ };
782
+ readonly type: "agent.message.delta";
783
+ readonly delta: string;
784
+ readonly turn_id?: string | undefined;
785
+ readonly sequence: number;
786
+ } | {
787
+ readonly id: string;
788
+ readonly version: "1.0";
789
+ readonly timestamp: number;
790
+ readonly session_id: string;
791
+ readonly metadata: {
792
+ readonly [x: string]: unknown;
793
+ };
794
+ readonly type: "agent.message.completed";
795
+ readonly content: string;
796
+ readonly turn_id?: string | undefined;
797
+ readonly tokens?: {
798
+ readonly input: number;
799
+ readonly output: number;
800
+ } | null | undefined;
801
+ readonly cost_usd?: number | null | undefined;
802
+ readonly sequence: number;
803
+ } | {
804
+ readonly id: string;
805
+ readonly version: "1.0";
806
+ readonly timestamp: number;
807
+ readonly session_id: string;
808
+ readonly metadata: {
809
+ readonly [x: string]: unknown;
810
+ };
811
+ readonly type: "agent.turn.exhausted";
812
+ readonly max_iterations: number;
813
+ readonly pending_tool_calls: readonly {
814
+ readonly [x: string]: unknown;
815
+ }[];
816
+ readonly turn_id?: string | undefined;
817
+ readonly reason?: string | null | undefined;
818
+ readonly sequence: number;
819
+ } | {
820
+ readonly id: string;
821
+ readonly version: "1.0";
822
+ readonly timestamp: number;
823
+ readonly session_id: string;
824
+ readonly metadata: {
825
+ readonly [x: string]: unknown;
826
+ };
827
+ readonly type: "agent.turn.failed";
828
+ readonly turn_id: string;
829
+ readonly error: string;
830
+ readonly error_code?: string | undefined;
831
+ readonly phase?: "queue" | "provider_open" | "provider_stream" | "approval" | "tool" | undefined;
832
+ readonly retryable?: boolean | undefined;
833
+ readonly outcome?: "not_started" | "failed" | "unknown" | undefined;
834
+ readonly sequence: number;
835
+ } | {
836
+ readonly id: string;
837
+ readonly version: "1.0";
838
+ readonly timestamp: number;
839
+ readonly session_id: string;
840
+ readonly metadata: {
841
+ readonly [x: string]: unknown;
842
+ };
843
+ readonly type: "tool.call.requested";
844
+ readonly turn_id: string;
845
+ readonly tool_name: string;
846
+ readonly tool_args: {
847
+ readonly [x: string]: unknown;
848
+ };
849
+ readonly tool_call_id: string;
850
+ readonly sequence: number;
851
+ } | {
852
+ readonly id: string;
853
+ readonly version: "1.0";
854
+ readonly timestamp: number;
855
+ readonly session_id: string;
856
+ readonly metadata: {
857
+ readonly [x: string]: unknown;
858
+ };
859
+ readonly type: "tool.call.started";
860
+ readonly turn_id: string;
861
+ readonly tool_name: string;
862
+ readonly tool_call_id: string;
863
+ readonly sequence: number;
864
+ } | {
865
+ readonly id: string;
866
+ readonly version: "1.0";
867
+ readonly timestamp: number;
868
+ readonly session_id: string;
869
+ readonly metadata: {
870
+ readonly [x: string]: unknown;
871
+ };
872
+ readonly type: "tool.call.completed";
873
+ readonly turn_id: string;
874
+ readonly tool_name: string;
875
+ readonly tool_call_id: string;
876
+ readonly result: unknown;
877
+ readonly tokens?: {
878
+ readonly input: number;
879
+ readonly output: number;
880
+ } | null | undefined;
881
+ readonly cost_usd?: number | null | undefined;
882
+ readonly sequence: number;
883
+ } | {
884
+ readonly id: string;
885
+ readonly version: "1.0";
886
+ readonly timestamp: number;
887
+ readonly session_id: string;
888
+ readonly metadata: {
889
+ readonly [x: string]: unknown;
890
+ };
891
+ readonly type: "tool.approval.requested";
892
+ readonly turn_id: string;
893
+ readonly tool_name: string;
894
+ readonly tool_call_id: string;
895
+ readonly tool_args: {
896
+ readonly [x: string]: unknown;
897
+ };
898
+ readonly risk: "read" | "write" | "external_effect" | "destructive" | "admin";
899
+ readonly sequence: number;
900
+ } | {
901
+ readonly id: string;
902
+ readonly version: "1.0";
903
+ readonly timestamp: number;
904
+ readonly session_id: string;
905
+ readonly metadata: {
906
+ readonly [x: string]: unknown;
907
+ };
908
+ readonly type: "tool.approval.approved";
909
+ readonly turn_id: string;
910
+ readonly tool_name: string;
911
+ readonly tool_call_id: string;
912
+ readonly sequence: number;
913
+ } | {
914
+ readonly id: string;
915
+ readonly version: "1.0";
916
+ readonly timestamp: number;
917
+ readonly session_id: string;
918
+ readonly metadata: {
919
+ readonly [x: string]: unknown;
920
+ };
921
+ readonly type: "tool.approval.rejected";
922
+ readonly turn_id: string;
923
+ readonly tool_name: string;
924
+ readonly tool_call_id: string;
925
+ readonly error_code: "APPROVAL_REJECTED" | "APPROVAL_TIMEOUT" | "TURN_TIMEOUT" | "TOOL_CANCELLED" | "APPROVAL_UNAVAILABLE";
926
+ readonly reason: string;
927
+ readonly sequence: number;
928
+ } | {
929
+ readonly id: string;
930
+ readonly version: "1.0";
931
+ readonly timestamp: number;
932
+ readonly session_id: string;
933
+ readonly metadata: {
934
+ readonly [x: string]: unknown;
935
+ };
936
+ readonly type: "workflow.started";
937
+ readonly workflow_name: string;
938
+ readonly turn_id?: string | undefined;
939
+ readonly sequence: number;
940
+ } | {
941
+ readonly id: string;
942
+ readonly version: "1.0";
943
+ readonly timestamp: number;
944
+ readonly session_id: string;
945
+ readonly metadata: {
946
+ readonly [x: string]: unknown;
947
+ };
948
+ readonly type: "workflow.completed";
949
+ readonly workflow_name: string;
950
+ readonly result: unknown;
951
+ readonly turn_id?: string | undefined;
952
+ readonly sequence: number;
953
+ } | {
954
+ readonly id: string;
955
+ readonly version: "1.0";
956
+ readonly timestamp: number;
957
+ readonly session_id: string;
958
+ readonly metadata: {
959
+ readonly [x: string]: unknown;
960
+ };
961
+ readonly type: "workflow.failed";
962
+ readonly workflow_name: string;
963
+ readonly error: string;
964
+ readonly turn_id?: string | undefined;
965
+ readonly sequence: number;
966
+ } | {
967
+ readonly id: string;
968
+ readonly version: "1.0";
969
+ readonly timestamp: number;
970
+ readonly session_id: string;
971
+ readonly metadata: {
972
+ readonly [x: string]: unknown;
973
+ };
974
+ readonly type: "cancellation.requested";
975
+ readonly reason: string;
976
+ readonly turn_id?: string | undefined;
977
+ readonly sequence: number;
978
+ } | {
979
+ readonly id: string;
980
+ readonly version: "1.0";
981
+ readonly timestamp: number;
982
+ readonly session_id: string;
983
+ readonly metadata: {
984
+ readonly [x: string]: unknown;
985
+ };
986
+ readonly type: "cancellation.completed";
987
+ readonly turn_id?: string | undefined;
988
+ readonly sequence: number;
989
+ }, {
990
+ [x: string]: unknown;
991
+ sequence: number;
992
+ }>>;
993
+ /** Validate an untouched new-event mapping before constructor defaults can run. */
994
+ declare function validateNewEvent(value: unknown): NewKajiEvent;
995
+ /** Validate an untouched stored-event mapping before constructor defaults can run. */
996
+ declare function validateStoredEvent(value: unknown): StoredKajiEvent;
997
+
998
+ /** Dependency-free, low-cardinality observability contracts for the stable runtime. */
999
+ type ProviderFamily = "openai" | "anthropic" | "custom";
1000
+ type SpanName = "kaji.turn" | "kaji.provider" | "kaji.tool" | "kaji.integration.auth" | "kaji.integration.request";
1001
+ type ProviderStatus = "success" | "error" | "cancelled";
1002
+ type TurnOutcome = "completed" | "failed" | "cancelled";
1003
+ type ToolMetricOutcome = "acquired" | "completed" | "failed" | "cancelled" | "timeout" | "not_started" | "unknown";
1004
+ type JournalStage = "append" | "publish";
1005
+ type SubscriberStage = "lag" | "overflow";
1006
+ interface MetricLabelsByName {
1007
+ "kaji.turn.queue_wait_ms": Record<never, never>;
1008
+ "kaji.turn.duration_ms": {
1009
+ outcome: TurnOutcome;
1010
+ };
1011
+ "kaji.turn.iterations": {
1012
+ outcome: TurnOutcome;
1013
+ };
1014
+ "kaji.provider.duration_ms": {
1015
+ provider_family: ProviderFamily;
1016
+ status: ProviderStatus;
1017
+ };
1018
+ "kaji.provider.retries": {
1019
+ provider_family: ProviderFamily;
1020
+ };
1021
+ "kaji.replay.input_events": Record<never, never>;
1022
+ "kaji.context.messages": Record<never, never>;
1023
+ "kaji.context.characters": Record<never, never>;
1024
+ "kaji.tool.queue_wait_ms": {
1025
+ outcome: ToolMetricOutcome;
1026
+ };
1027
+ "kaji.tool.active": Record<never, never>;
1028
+ "kaji.tool.duration_ms": {
1029
+ outcome: ToolMetricOutcome;
1030
+ error_code: string;
1031
+ };
1032
+ "kaji.journal.failures": {
1033
+ stage: JournalStage;
1034
+ };
1035
+ "kaji.subscriber.lag_events": Record<never, never>;
1036
+ "kaji.subscriber.overflow": {
1037
+ stage: SubscriberStage;
1038
+ };
1039
+ "kaji.integration.auth_ms": {
1040
+ integration: "github" | "gmail";
1041
+ operation: "token";
1042
+ outcome: ProviderStatus;
1043
+ };
1044
+ "kaji.integration.request_ms": {
1045
+ integration: "github" | "gmail";
1046
+ operation: "read" | "mutation";
1047
+ outcome: ProviderStatus;
1048
+ };
1049
+ }
1050
+ interface MetricUnitByName {
1051
+ "kaji.turn.queue_wait_ms": "ms";
1052
+ "kaji.turn.duration_ms": "ms";
1053
+ "kaji.turn.iterations": "count";
1054
+ "kaji.provider.duration_ms": "ms";
1055
+ "kaji.provider.retries": "count";
1056
+ "kaji.replay.input_events": "count";
1057
+ "kaji.context.messages": "count";
1058
+ "kaji.context.characters": "count";
1059
+ "kaji.tool.queue_wait_ms": "ms";
1060
+ "kaji.tool.active": "gauge";
1061
+ "kaji.tool.duration_ms": "ms";
1062
+ "kaji.journal.failures": "count";
1063
+ "kaji.subscriber.lag_events": "count";
1064
+ "kaji.subscriber.overflow": "count";
1065
+ "kaji.integration.auth_ms": "ms";
1066
+ "kaji.integration.request_ms": "ms";
1067
+ }
1068
+ type MetricName = keyof MetricLabelsByName;
1069
+ type MetricLabels<TName extends MetricName> = Readonly<MetricLabelsByName[TName]>;
1070
+ type MetricMeasurement = {
1071
+ [TName in MetricName]: Readonly<{
1072
+ name: TName;
1073
+ value: number;
1074
+ unit: MetricUnitByName[TName];
1075
+ labels: MetricLabels<TName>;
1076
+ }>;
1077
+ }[MetricName];
1078
+ interface MetricsSink {
1079
+ record(measurement: MetricMeasurement): void | Promise<void>;
1080
+ }
1081
+ type TraceAttributeName = "session.id" | "turn.id" | "request.id" | "trace.id" | "tool.call_id" | "provider.family" | "integration.name" | "integration.operation" | "http.status_family";
1082
+ type TraceAttributeValue = string;
1083
+ type TraceAttributes = Readonly<Partial<Record<TraceAttributeName, TraceAttributeValue>>>;
1084
+ interface TraceSpan {
1085
+ setAttribute(name: TraceAttributeName, value: TraceAttributeValue): void;
1086
+ recordError(error: unknown): void;
1087
+ end(): void;
1088
+ }
1089
+ interface TraceSink {
1090
+ startSpan(name: SpanName, attributes?: TraceAttributes): TraceSpan;
1091
+ }
1092
+ declare const METRIC_NAMES: readonly MetricName[];
1093
+ /** Record a validated measurement. Invalid tuples fail closed; sink failures stay observational. */
1094
+ declare function recordMetric<TName extends MetricName>(sink: MetricsSink, name: TName, value: number, labels: MetricLabels<TName>): void;
1095
+ declare const NOOP_METRICS: MetricsSink;
1096
+ declare const NOOP_TRACE: TraceSink;
1097
+ /** Start a span protected from throwing third-party sinks and span handles. */
1098
+ declare function startSpan(sink: TraceSink, name: SpanName, attributes?: TraceAttributes): TraceSpan;
1099
+ declare function providerFamily(provider: object): ProviderFamily;
1100
+
1101
+ /** A single conversation turn in the projected state. */
1102
+ interface MessageToolCall {
1103
+ id: string;
1104
+ name: string;
1105
+ args: Record<string, unknown>;
1106
+ }
1107
+ interface Message {
1108
+ role: "user" | "assistant" | "tool";
1109
+ content: string;
1110
+ /** Set only for tool messages. */
1111
+ name?: string;
1112
+ /** Set only for tool messages: the id from the originating tool call request. */
1113
+ toolCallId?: string;
1114
+ /** Set only for assistant messages that requested tools. */
1115
+ toolCalls?: MessageToolCall[];
1116
+ }
1117
+ /** Accumulated token counts across all turns in the session. */
1118
+ interface SessionTokens {
1119
+ input: number;
1120
+ output: number;
1121
+ }
1122
+ declare const APPROVAL_KEY: unique symbol;
1123
+ /** Stable value key for the approval security/correlation tuple. */
1124
+ type ApprovalKey = string & {
1125
+ readonly [APPROVAL_KEY]: true;
1126
+ };
1127
+ type ApprovalFailureCode = "APPROVAL_REJECTED" | "APPROVAL_TIMEOUT" | "TURN_TIMEOUT" | "TOOL_CANCELLED" | "APPROVAL_UNAVAILABLE";
1128
+ declare function approvalKey(turnId: string, toolCallId: string, toolName: string): ApprovalKey;
1129
+ /** A projection of the event log into current session state. */
1130
+ interface SessionState {
1131
+ sessionId: string;
1132
+ isActive: boolean;
1133
+ messages: Message[];
1134
+ /**
1135
+ * Correlation triples that emitted TOOL_APPROVAL_REQUESTED but have not yet
1136
+ * been approved or rejected.
1137
+ */
1138
+ pendingApprovals: Set<ApprovalKey>;
1139
+ approvedApprovals: Set<ApprovalKey>;
1140
+ rejectedApprovals: Map<ApprovalKey, ApprovalFailureCode>;
1141
+ /** Total tokens consumed in this session (summed from AGENT_MESSAGE_COMPLETED events). */
1142
+ totalTokens: SessionTokens;
1143
+ /** Estimated total cost in USD for this session. */
1144
+ totalCostUsd: number;
1145
+ }
1146
+ /** Create an empty projection for incremental application. */
1147
+ declare function createSessionState(sessionId: string): SessionState;
1148
+ /**
1149
+ * Reconstruct session state from stored events. Sequences must already be
1150
+ * strictly monotonic.
1151
+ *
1152
+ * TOOL_CALL_REQUESTED events are attached to the most recent assistant message
1153
+ * so provider history includes the assistant-side tool call before the matching
1154
+ * role:tool result. OpenAI and Anthropic reject orphan tool-result messages.
1155
+ */
1156
+ declare function replaySession(events: readonly StoredKajiEvent[]): SessionState;
1157
+ /** Apply one persisted event to an existing session projection in place. */
1158
+ declare function applyEvent(state: SessionState, event: StoredKajiEvent): number | null;
1159
+
1160
+ type UuidFactory = () => string;
1161
+ type IdScope = "event" | "session" | "turn" | "request" | "trace" | "tool_call";
1162
+ interface IdFactory {
1163
+ next(scope: IdScope): string;
1164
+ }
1165
+ interface Clock {
1166
+ nowWallSeconds(): number;
1167
+ /** Monotonic milliseconds, matching `performance.now()`. */
1168
+ nowMonotonic(): number;
1169
+ }
1170
+ interface TimerHandle {
1171
+ cancel(): void;
1172
+ }
1173
+ /** Minimal one-shot timer seam used by deadline races. */
1174
+ interface TimerScheduler {
1175
+ schedule(delayMs: number, callback: () => void): TimerHandle;
1176
+ }
1177
+
1178
+ /**
1179
+ * Cancellation token for the agent loop, mirroring the Python
1180
+ * `CancellationToken`. Carries a backing `AbortController` so the same
1181
+ * cancel signal can be plumbed into platform APIs that accept an
1182
+ * `AbortSignal` (the `openai` / `@anthropic-ai/sdk` clients, `fetch`,
1183
+ * `EventTarget` listeners, etc.). The boolean `isCancelled` flag remains
1184
+ * for the polling style the runtime already uses; both fire together.
1185
+ */
1186
+ declare class CancellationError extends Error {
1187
+ constructor(message?: string);
1188
+ }
1189
+ interface CancellationTokenLike {
1190
+ isCancelled: boolean;
1191
+ signal?: AbortSignal;
1192
+ throwIfCancelled?: () => void;
1193
+ }
1194
+ declare function throwIfCancellationRequested(token?: CancellationTokenLike): void;
1195
+ declare class CancellationToken {
1196
+ private readonly controller;
1197
+ /** Whether `cancel()` has been called. */
1198
+ get isCancelled(): boolean;
1199
+ /**
1200
+ * The underlying `AbortSignal`. Pass this directly to APIs that accept
1201
+ * one (the OpenAI / Anthropic SDKs, `fetch`, etc.) so the network call
1202
+ * aborts on cancel instead of just polling out at the next yield point.
1203
+ */
1204
+ get signal(): AbortSignal;
1205
+ cancel(): void;
1206
+ throwIfCancelled(): void;
1207
+ }
1208
+
1209
+ /** Compiled Draft 2020-12 validation for provider-supplied tool arguments. */
1210
+
1211
+ type ToolValidationCode = "INVALID_TOOL_SCHEMA" | "INVALID_TOOL_ARGUMENTS" | "UNCLASSIFIED_TOOL_RISK";
1212
+ type ToolExecutionOutcome = "not_started";
1213
+ type ToolArgumentValidator = (toolName: string, args: unknown) => Promise<void>;
1214
+ declare const TOOL_ARGUMENT_VALIDATOR: unique symbol;
1215
+ declare abstract class ToolValidationError extends Error {
1216
+ readonly code: ToolValidationCode;
1217
+ readonly toolName: string;
1218
+ readonly path: string;
1219
+ readonly retryable = false;
1220
+ readonly outcome: ToolExecutionOutcome;
1221
+ protected constructor(code: ToolValidationCode, toolName: string, path: string, message: string);
1222
+ normalized(): {
1223
+ code: ToolValidationCode;
1224
+ path: string;
1225
+ message: string;
1226
+ };
1227
+ }
1228
+ declare class ToolArgumentValidationError extends ToolValidationError {
1229
+ private constructor();
1230
+ static fromAjv(toolName: string, errors: readonly ErrorObject[]): ToolArgumentValidationError;
1231
+ static nonObject(toolName: string): ToolArgumentValidationError;
1232
+ static parseError(toolName: string): ToolArgumentValidationError;
1233
+ static fromValidationIssue(toolName: string, pathParts: readonly PropertyKey[], keyword: string): ToolArgumentValidationError;
1234
+ static jsonUnsafe(toolName: string, path: string): ToolArgumentValidationError;
1235
+ static oversize(toolName: string): ToolArgumentValidationError;
1236
+ static changedDuringValidation(toolName: string): ToolArgumentValidationError;
1237
+ }
1238
+ declare class ToolSchemaValidationError extends ToolValidationError {
1239
+ private constructor();
1240
+ static fromAjv(toolName: string, errors: readonly ErrorObject[]): ToolSchemaValidationError;
1241
+ static compilation(toolName: string): ToolSchemaValidationError;
1242
+ static jsonUnsafe(toolName: string, path: string): ToolSchemaValidationError;
1243
+ static invalidRisk(toolName: string): ToolSchemaValidationError;
1244
+ }
1245
+ /** Compile each tool schema once and validate calls without exposing receipts. */
1246
+ declare class ToolSchemaValidator {
1247
+ constructor(specs?: ReadonlyMap<string, ToolSpec>);
1248
+ /** Validate a private clone; Zod parse results are intentionally discarded. */
1249
+ validate(name: string, args: unknown): Promise<void>;
1250
+ }
1251
+
1252
+ /**
1253
+ * Tool registry for LLM-callable functions, mirroring
1254
+ * `kaji.runtime.tools.registry`. A process-level registry holds specs and
1255
+ * handlers; tools run with a `ToolExecutionContext` and need no infra by default.
1256
+ */
1257
+
1258
+ /** A JSON Schema object describing a tool's parameters. */
1259
+ type JSONSchema = Readonly<Record<string, unknown>>;
1260
+ /** Parameter schema accepted at tool authoring boundaries. */
1261
+ type ToolParameters = JSONSchema | z.ZodType;
1262
+ /** Risk level for a tool, used by ToolPolicy to gate execution. */
1263
+ type ToolRisk = "read" | "write" | "external_effect" | "destructive" | "admin";
1264
+ declare class UnclassifiedToolRiskError extends Error {
1265
+ readonly toolName: string;
1266
+ readonly code: "UNCLASSIFIED_TOOL_RISK";
1267
+ readonly retryable = false;
1268
+ readonly outcome: "not_started";
1269
+ constructor(toolName: string);
1270
+ }
1271
+ /** Definition of a tool exposed to the LLM. */
1272
+ interface ToolSpec {
1273
+ readonly name: string;
1274
+ readonly description: string;
1275
+ readonly parameters: JSONSchema;
1276
+ readonly catalogName?: string;
1277
+ readonly tags?: readonly string[];
1278
+ readonly enabled?: boolean;
1279
+ /** Risk classification for policy enforcement and approval routing. */
1280
+ readonly risk: ToolRisk;
1281
+ /** Explicit opt-in for bounded overlap with adjacent parallel-safe tools. */
1282
+ readonly parallel_safe?: boolean;
1283
+ /** Per-tool execution deadline. Must be a positive integer when present. */
1284
+ readonly timeout_ms?: number;
1285
+ readonly [TOOL_ARGUMENT_VALIDATOR]?: ToolArgumentValidator;
1286
+ }
1287
+ /** A tool handler: receives validated args and canonical execution context. */
1288
+ type ToolHandler = (args: Record<string, unknown>, context: ToolExecutionContext) => Promise<Record<string, unknown>>;
1289
+ /** Metadata attached to a handler by `tool(meta, fn)`. */
1290
+ interface ToolMeta {
1291
+ description: string;
1292
+ parameters: ToolParameters;
1293
+ risk: ToolRisk;
1294
+ tags?: string[];
1295
+ enabled?: boolean;
1296
+ parallel_safe?: boolean;
1297
+ timeout_ms?: number;
1298
+ }
1299
+ interface ListToolSpecsOptions {
1300
+ tags?: string[];
1301
+ enabledOnly?: boolean;
1302
+ }
1303
+ /**
1304
+ * Build a tool spec from a Zod schema while preserving its complete validation
1305
+ * schema, including nested constraints and references.
1306
+ */
1307
+ declare function toolSpecFromSchema(name: string, description: string, schema: z.ZodType, risk: ToolRisk): ToolSpec;
1308
+ /**
1309
+ * Scoped tool registry for per-agent or per-tenant isolation.
1310
+ *
1311
+ * The module-level `registerTool`, `listToolSpecs`, and `executeTool`
1312
+ * functions delegate to a single default `ToolRegistry` instance, which is
1313
+ * sufficient for simple setups. Construct your own `ToolRegistry` when you
1314
+ * need multiple isolated registries.
1315
+ *
1316
+ * @example
1317
+ * ```ts
1318
+ * const registry = new ToolRegistry();
1319
+ * registry.register(
1320
+ * { name: "ping", description: "...", parameters: {}, risk: "read" },
1321
+ * async (_args, context) => ({ pong: true, principalId: context.principalId }),
1322
+ * );
1323
+ * await registry.execute("ping", {}, {
1324
+ * principalId: "user-1",
1325
+ * sessionId: "session-1",
1326
+ * turnId: "turn-1",
1327
+ * requestId: "request-1",
1328
+ * traceId: "trace-1",
1329
+ * toolCallId: "call-1",
1330
+ * idempotencyKey: "session-1:call-1",
1331
+ * signal: new AbortController().signal,
1332
+ * metadata: {},
1333
+ * });
1334
+ * ```
1335
+ */
1336
+ declare class ToolRegistry {
1337
+ private readonly specs;
1338
+ private readonly handlers;
1339
+ private readonly schemaValidator;
1340
+ constructor(schemaValidator?: ToolSchemaValidator);
1341
+ register(spec: ToolSpec, handler: ToolHandler): this;
1342
+ register(name: string, handler: ToolHandler): this;
1343
+ listSpecs(options?: ListToolSpecsOptions): ToolSpec[];
1344
+ execute(toolName: string, toolArgs: Record<string, unknown>, context: ToolExecutionContext): Promise<Record<string, unknown>>;
1345
+ clear(): void;
1346
+ }
1347
+ /** Thrown when a tool is requested by name but not registered. */
1348
+ declare class UnknownToolError extends Error {
1349
+ readonly toolName: string;
1350
+ constructor(toolName: string);
1351
+ }
1352
+ /** Register a tool handler on the process-default registry. Accepts either a
1353
+ * full ToolSpec + handler, or a pre-tagged handler (from `tool(meta, fn)`)
1354
+ * with just a name string. */
1355
+ declare function registerTool(spec: ToolSpec, handler: ToolHandler): void;
1356
+ declare function registerTool(name: string, handler: ToolHandler): void;
1357
+ /** Return registered tool specs from the process-default registry. */
1358
+ declare function listToolSpecs(options?: ListToolSpecsOptions): ToolSpec[];
1359
+ /** Execute a registered tool call from the process-default registry. */
1360
+ declare function executeTool(toolName: string, toolArgs: Record<string, unknown>, context: ToolExecutionContext): Promise<Record<string, unknown>>;
1361
+
1362
+ /**
1363
+ * Provider interface for LLM backends, mirroring
1364
+ * `kaji.runtime.providers.base.ModelProvider`.
1365
+ *
1366
+ * Each provider translates the neutral message + tool format to its own API at
1367
+ * its boundary. The runtime never imports provider-specific types.
1368
+ */
1369
+
1370
+ /** A message in the conversation history passed to the provider. */
1371
+ interface ProviderMessage {
1372
+ role: "system" | "user" | "assistant" | "tool";
1373
+ content: string;
1374
+ /** Set only for tool-result messages. */
1375
+ name?: string;
1376
+ /** Set only for tool-result messages: id from the originating tool call. */
1377
+ tool_call_id?: string;
1378
+ /** Set only for assistant messages that requested tools. */
1379
+ toolCalls?: ToolCall[];
1380
+ }
1381
+ /** A tool call the model wants to make. */
1382
+ interface ToolCall {
1383
+ id: string;
1384
+ name: string;
1385
+ args: Record<string, unknown>;
1386
+ }
1387
+ /** A streaming chunk from the provider. */
1388
+ interface ModelResponseChunk {
1389
+ delta: string;
1390
+ toolCalls: ToolCall[];
1391
+ /** Token usage, if the provider reported it during streaming. */
1392
+ usage?: TokenUsage;
1393
+ /** Estimated cost in USD, if the model is in the cost table. */
1394
+ costUsd?: number;
1395
+ }
1396
+ /** Token usage from a provider response. */
1397
+ interface TokenUsage {
1398
+ /** Prompt / input tokens consumed. */
1399
+ input: number;
1400
+ /** Completion / output tokens generated. */
1401
+ output: number;
1402
+ }
1403
+ /** A complete non-streaming response. */
1404
+ interface ModelResponse {
1405
+ content: string;
1406
+ toolCalls: ToolCall[];
1407
+ /** Token usage, if the provider reported it. */
1408
+ usage?: TokenUsage;
1409
+ /** Estimated cost in USD, if the model is in the cost table. */
1410
+ costUsd?: number;
1411
+ }
1412
+ /** Immutable per-call provider response bounds. */
1413
+ interface ProviderResponseLimits {
1414
+ readonly textMaxBytes: number;
1415
+ readonly toolArgumentsMaxBytes: number;
1416
+ readonly responseMaxBytes: number;
1417
+ readonly toolCallsMax: number;
1418
+ }
1419
+ /** Linear-assembly evidence captured for one provider response. */
1420
+ interface ProviderResponseDiagnostics {
1421
+ readonly rawFragments: number;
1422
+ readonly toolArgumentJoinOperations: number;
1423
+ }
1424
+ /** Internal per-call sink used by the runtime without changing stream chunks. */
1425
+ interface ProviderResponseDiagnosticsSink {
1426
+ record(diagnostics: Readonly<ProviderResponseDiagnostics>): void;
1427
+ }
1428
+ /** @internal Attach runtime-owned diagnostics without exposing a public option. */
1429
+ declare function withProviderResponseDiagnostics(options: ModelProviderOptions, sink: ProviderResponseDiagnosticsSink): ModelProviderOptions;
1430
+ declare const DEFAULT_PROVIDER_RESPONSE_LIMITS: Readonly<ProviderResponseLimits>;
1431
+ declare function resolveProviderResponseLimits(limits: Readonly<ProviderResponseLimits> | undefined): Readonly<ProviderResponseLimits>;
1432
+ /**
1433
+ * Per-call options that callers may override from the constructor defaults.
1434
+ *
1435
+ * `cancellationToken` is structurally typed: any object with an
1436
+ * `isCancelled` boolean is accepted. Pass a full `CancellationToken`
1437
+ * (which also carries an `AbortSignal` under `.signal`) to let the
1438
+ * provider abort the underlying HTTP call, not just poll out at the next
1439
+ * yield point.
1440
+ */
1441
+ interface ModelProviderOptions {
1442
+ temperature?: number;
1443
+ maxTokens?: number;
1444
+ cancellationToken?: CancellationTokenLike;
1445
+ /** Runtime-injected metrics sink. Provider implementations must keep labels low-cardinality. */
1446
+ metricsSink?: MetricsSink;
1447
+ /** Runtime-injected immutable response limits. */
1448
+ responseLimits?: Readonly<ProviderResponseLimits>;
1449
+ }
1450
+ /** Common interface every LLM provider implements. */
1451
+ interface ModelProvider {
1452
+ /**
1453
+ * Custom providers are cooperative cancellation boundaries. Both methods
1454
+ * must observe `options.cancellationToken` while opening and streaming,
1455
+ * stop their underlying request, and let the returned iterator settle when
1456
+ * cancellation is requested. A provider that misses the configured grace
1457
+ * causes a typed contract violation and quarantines that session until a
1458
+ * successful `drainProviders()`. Custom adapters must pass the SDK's
1459
+ * cancellation-contract suite before being described as production-safe.
1460
+ */
1461
+ /** Stable low-cardinality family identifier for observability. */
1462
+ readonly providerFamily?: ProviderFamily;
1463
+ generate(messages: ProviderMessage[], tools: ToolSpec[], options?: ModelProviderOptions): Promise<ModelResponse>;
1464
+ generateStream(messages: ProviderMessage[], tools: ToolSpec[], options?: ModelProviderOptions): AsyncGenerator<ModelResponseChunk>;
1465
+ }
1466
+ /** Rate-limit retry configuration shared by HTTP-backed providers. */
1467
+ interface RetryOptions {
1468
+ /** Maximum retry attempts on 429 rate-limit responses. Defaults to 3. */
1469
+ maxAttempts?: number;
1470
+ /** Base delay in ms before first retry. Doubles each attempt. Defaults to 1000. */
1471
+ baseDelayMs?: number;
1472
+ }
1473
+
1474
+ /**
1475
+ * Build the provider message list from replayed session state. Mirrors the
1476
+ * message construction in `kaji.runtime.agents.runtime`.
1477
+ */
1478
+
1479
+ /** Caller-owned context applied to one agent turn. */
1480
+ interface TurnContext {
1481
+ readonly principalId?: string;
1482
+ readonly requestId?: string;
1483
+ readonly traceId?: string;
1484
+ /**
1485
+ * Absolute Unix epoch deadline in milliseconds. An earlier value tightens,
1486
+ * but never extends, the runtime's configured whole-turn maximum.
1487
+ * Use `deadlineAfter()` when starting from a duration.
1488
+ */
1489
+ readonly deadlineAtMs?: number;
1490
+ readonly db?: unknown;
1491
+ readonly metadata?: Readonly<Record<string, unknown>>;
1492
+ }
1493
+ /** Fully resolved authorization and correlation context for one tool call. */
1494
+ interface ToolExecutionContext {
1495
+ readonly principalId: string;
1496
+ readonly sessionId: string;
1497
+ readonly turnId: string;
1498
+ readonly requestId: string;
1499
+ readonly traceId: string;
1500
+ readonly toolCallId: string;
1501
+ readonly idempotencyKey: string;
1502
+ readonly deadlineMonotonicMs?: number;
1503
+ readonly signal: AbortSignal;
1504
+ readonly db?: unknown;
1505
+ readonly metadata: Readonly<Record<string, unknown>>;
1506
+ }
1507
+ declare class MissingToolIdentityError extends Error {
1508
+ readonly code: "MISSING_TOOL_IDENTITY";
1509
+ readonly retryable = false;
1510
+ readonly outcome: "not_started";
1511
+ constructor();
1512
+ }
1513
+ /** Convert a caller duration to an absolute Unix epoch deadline for `TurnContext`. */
1514
+ declare function deadlineAfter(timeoutMs: number, clock?: Clock): number;
1515
+ interface ContextWindow {
1516
+ maxTurns: number | null;
1517
+ maxCharacters: number | null;
1518
+ }
1519
+ interface ContextDiagnostics {
1520
+ readonly droppedTurns: number;
1521
+ readonly droppedMessages: number;
1522
+ readonly droppedCharacters: number;
1523
+ }
1524
+ interface ContextBuildResult {
1525
+ messages: ProviderMessage[];
1526
+ diagnostics: ContextDiagnostics;
1527
+ }
1528
+ declare const DEFAULT_CONTEXT_WINDOW: Readonly<ContextWindow>;
1529
+ declare class ContextWindowOverflowError extends Error {
1530
+ readonly currentTurnCharacters: number;
1531
+ readonly maxCharacters: number;
1532
+ constructor(currentTurnCharacters: number, maxCharacters: number);
1533
+ }
1534
+ declare class ContextIntegrityError extends Error {
1535
+ constructor(message: string);
1536
+ }
1537
+
1538
+ export { type ProviderStatus as $, AgentTurnFailed as A, type BaseEvent as B, type ContextWindow as C, DEFAULT_CONTEXT_WINDOW as D, type IdScope as E, type JournalStage as F, type KajiEventInput as G, METRIC_NAMES as H, type IdFactory as I, type JSONSchema as J, KajiEvent as K, type ListToolSpecsOptions as L, type MetricsSink as M, NewKajiEvent as N, type Message as O, type ProviderMessage as P, type MetricLabels as Q, type RetryOptions as R, StoredKajiEvent as S, type ToolRisk as T, type MetricMeasurement as U, type MetricName as V, MissingToolIdentityError as W, NOOP_METRICS as X, NOOP_TRACE as Y, type ProviderFamily as Z, type ProviderResponseLimits as _, type SessionState as a, SessionCreated as a0, type SessionTokens as a1, type SpanName as a2, type SubscriberStage as a3, type TimerHandle as a4, ToolArgumentValidationError as a5, type ToolExecutionOutcome as a6, type ToolMetricOutcome as a7, type ToolParameters as a8, ToolSchemaValidationError as a9, type ProviderResponseDiagnostics as aA, createSessionState as aB, withProviderResponseDiagnostics as aC, ToolSchemaValidator as aa, type ToolValidationCode as ab, ToolValidationError as ac, type TraceAttributeName as ad, type TraceAttributeValue as ae, type TraceAttributes as af, type TraceSpan as ag, type TurnOutcome as ah, UnclassifiedToolRiskError as ai, UnknownToolError as aj, type UuidFactory as ak, applyEvent as al, approvalKey as am, deadlineAfter as an, executeTool as ao, listToolSpecs as ap, providerFamily as aq, recordMetric as ar, registerTool as as, replaySession as at, resolveProviderResponseLimits as au, startSpan as av, throwIfCancellationRequested as aw, toolSpecFromSchema as ax, validateNewEvent as ay, validateStoredEvent as az, type ContextBuildResult as b, type ToolCall as c, type ToolExecutionContext as d, type TimerScheduler as e, type TraceSink as f, type ToolSpec as g, type Clock as h, type ModelProvider as i, type ModelProviderOptions as j, type ModelResponse as k, type ModelResponseChunk as l, type ToolHandler as m, ToolRegistry as n, type ToolMeta as o, type CancellationTokenLike as p, type TurnContext as q, type ContextDiagnostics as r, CancellationToken as s, type TokenUsage as t, type ApprovalFailureCode as u, type ApprovalKey as v, CancellationError as w, ContextIntegrityError as x, ContextWindowOverflowError as y, DEFAULT_PROVIDER_RESPONSE_LIMITS as z };