@fgv/ts-extras 5.1.0-55 → 5.1.0-56

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 (156) hide show
  1. package/CAPABILITIES.md +95 -0
  2. package/dist/packlets/ai-assist/cacheRequest.js +118 -0
  3. package/dist/packlets/ai-assist/cacheRequest.js.map +1 -0
  4. package/dist/packlets/ai-assist/chatRequestBuilders.js +70 -5
  5. package/dist/packlets/ai-assist/chatRequestBuilders.js.map +1 -1
  6. package/dist/packlets/ai-assist/completionClient.js +122 -45
  7. package/dist/packlets/ai-assist/completionClient.js.map +1 -1
  8. package/dist/packlets/ai-assist/imageGenerationClient.js +1 -1
  9. package/dist/packlets/ai-assist/imageGenerationClient.js.map +1 -1
  10. package/dist/packlets/ai-assist/index.js +3 -1
  11. package/dist/packlets/ai-assist/index.js.map +1 -1
  12. package/dist/packlets/ai-assist/model.js.map +1 -1
  13. package/dist/packlets/ai-assist/promptCacheAnthropicBreakpointHarness.js +200 -0
  14. package/dist/packlets/ai-assist/promptCacheAnthropicBreakpointHarness.js.map +1 -0
  15. package/dist/packlets/ai-assist/promptCacheObservabilityHarness.js +137 -0
  16. package/dist/packlets/ai-assist/promptCacheObservabilityHarness.js.map +1 -0
  17. package/dist/packlets/ai-assist/promptCacheRoutingAbHarness.js +156 -0
  18. package/dist/packlets/ai-assist/promptCacheRoutingAbHarness.js.map +1 -0
  19. package/dist/packlets/ai-assist/promptCacheUsageMath.js +124 -0
  20. package/dist/packlets/ai-assist/promptCacheUsageMath.js.map +1 -0
  21. package/dist/packlets/ai-assist/registry.js +9 -11
  22. package/dist/packlets/ai-assist/registry.js.map +1 -1
  23. package/dist/packlets/ai-assist/streamUsageCapability.js +124 -0
  24. package/dist/packlets/ai-assist/streamUsageCapability.js.map +1 -0
  25. package/dist/packlets/ai-assist/streamingAdapters/anthropic.js +24 -5
  26. package/dist/packlets/ai-assist/streamingAdapters/anthropic.js.map +1 -1
  27. package/dist/packlets/ai-assist/streamingAdapters/clientToolContinuationBuilder.js +64 -33
  28. package/dist/packlets/ai-assist/streamingAdapters/clientToolContinuationBuilder.js.map +1 -1
  29. package/dist/packlets/ai-assist/streamingAdapters/common.js +25 -1
  30. package/dist/packlets/ai-assist/streamingAdapters/common.js.map +1 -1
  31. package/dist/packlets/ai-assist/streamingAdapters/gemini.js +12 -5
  32. package/dist/packlets/ai-assist/streamingAdapters/gemini.js.map +1 -1
  33. package/dist/packlets/ai-assist/streamingAdapters/openaiChat.js +34 -8
  34. package/dist/packlets/ai-assist/streamingAdapters/openaiChat.js.map +1 -1
  35. package/dist/packlets/ai-assist/streamingAdapters/openaiResponses.js +22 -14
  36. package/dist/packlets/ai-assist/streamingAdapters/openaiResponses.js.map +1 -1
  37. package/dist/packlets/ai-assist/streamingClient.js +3 -2
  38. package/dist/packlets/ai-assist/streamingClient.js.map +1 -1
  39. package/dist/packlets/ai-assist/thinkingOptionsResolver.js +18 -3
  40. package/dist/packlets/ai-assist/thinkingOptionsResolver.js.map +1 -1
  41. package/dist/packlets/ai-assist/toolFormats.js +80 -0
  42. package/dist/packlets/ai-assist/toolFormats.js.map +1 -1
  43. package/dist/packlets/ai-assist/toolTypes.js +21 -0
  44. package/dist/packlets/ai-assist/toolTypes.js.map +1 -0
  45. package/dist/packlets/ai-assist/usageNormalization.js +178 -0
  46. package/dist/packlets/ai-assist/usageNormalization.js.map +1 -0
  47. package/dist/packlets/ai-assist/usageTypes.js +21 -0
  48. package/dist/packlets/ai-assist/usageTypes.js.map +1 -0
  49. package/dist/packlets/mustache/index.js.map +1 -1
  50. package/dist/packlets/mustache/interfaces.js.map +1 -1
  51. package/dist/packlets/mustache/mustacheTemplate.js +91 -0
  52. package/dist/packlets/mustache/mustacheTemplate.js.map +1 -1
  53. package/dist/ts-extras.d.ts +547 -30
  54. package/lib/packlets/ai-assist/cacheRequest.d.ts +100 -0
  55. package/lib/packlets/ai-assist/cacheRequest.d.ts.map +1 -0
  56. package/lib/packlets/ai-assist/cacheRequest.js +123 -0
  57. package/lib/packlets/ai-assist/cacheRequest.js.map +1 -0
  58. package/lib/packlets/ai-assist/chatRequestBuilders.d.ts +34 -4
  59. package/lib/packlets/ai-assist/chatRequestBuilders.d.ts.map +1 -1
  60. package/lib/packlets/ai-assist/chatRequestBuilders.js +73 -5
  61. package/lib/packlets/ai-assist/chatRequestBuilders.js.map +1 -1
  62. package/lib/packlets/ai-assist/completionClient.d.ts +6 -0
  63. package/lib/packlets/ai-assist/completionClient.d.ts.map +1 -1
  64. package/lib/packlets/ai-assist/completionClient.js +121 -44
  65. package/lib/packlets/ai-assist/completionClient.js.map +1 -1
  66. package/lib/packlets/ai-assist/imageGenerationClient.d.ts.map +1 -1
  67. package/lib/packlets/ai-assist/imageGenerationClient.js +1 -1
  68. package/lib/packlets/ai-assist/imageGenerationClient.js.map +1 -1
  69. package/lib/packlets/ai-assist/index.d.ts +7 -2
  70. package/lib/packlets/ai-assist/index.d.ts.map +1 -1
  71. package/lib/packlets/ai-assist/index.js +11 -1
  72. package/lib/packlets/ai-assist/index.js.map +1 -1
  73. package/lib/packlets/ai-assist/model.d.ts +59 -288
  74. package/lib/packlets/ai-assist/model.d.ts.map +1 -1
  75. package/lib/packlets/ai-assist/model.js.map +1 -1
  76. package/lib/packlets/ai-assist/promptCacheAnthropicBreakpointHarness.d.ts +124 -0
  77. package/lib/packlets/ai-assist/promptCacheAnthropicBreakpointHarness.d.ts.map +1 -0
  78. package/lib/packlets/ai-assist/promptCacheAnthropicBreakpointHarness.js +208 -0
  79. package/lib/packlets/ai-assist/promptCacheAnthropicBreakpointHarness.js.map +1 -0
  80. package/lib/packlets/ai-assist/promptCacheObservabilityHarness.d.ts +77 -0
  81. package/lib/packlets/ai-assist/promptCacheObservabilityHarness.d.ts.map +1 -0
  82. package/lib/packlets/ai-assist/promptCacheObservabilityHarness.js +143 -0
  83. package/lib/packlets/ai-assist/promptCacheObservabilityHarness.js.map +1 -0
  84. package/lib/packlets/ai-assist/promptCacheRoutingAbHarness.d.ts +84 -0
  85. package/lib/packlets/ai-assist/promptCacheRoutingAbHarness.d.ts.map +1 -0
  86. package/lib/packlets/ai-assist/promptCacheRoutingAbHarness.js +163 -0
  87. package/lib/packlets/ai-assist/promptCacheRoutingAbHarness.js.map +1 -0
  88. package/lib/packlets/ai-assist/promptCacheUsageMath.d.ts +105 -0
  89. package/lib/packlets/ai-assist/promptCacheUsageMath.d.ts.map +1 -0
  90. package/lib/packlets/ai-assist/promptCacheUsageMath.js +130 -0
  91. package/lib/packlets/ai-assist/promptCacheUsageMath.js.map +1 -0
  92. package/lib/packlets/ai-assist/registry.d.ts.map +1 -1
  93. package/lib/packlets/ai-assist/registry.js +9 -11
  94. package/lib/packlets/ai-assist/registry.js.map +1 -1
  95. package/lib/packlets/ai-assist/streamUsageCapability.d.ts +119 -0
  96. package/lib/packlets/ai-assist/streamUsageCapability.d.ts.map +1 -0
  97. package/lib/packlets/ai-assist/streamUsageCapability.js +130 -0
  98. package/lib/packlets/ai-assist/streamUsageCapability.js.map +1 -0
  99. package/lib/packlets/ai-assist/streamingAdapters/anthropic.d.ts.map +1 -1
  100. package/lib/packlets/ai-assist/streamingAdapters/anthropic.js +23 -4
  101. package/lib/packlets/ai-assist/streamingAdapters/anthropic.js.map +1 -1
  102. package/lib/packlets/ai-assist/streamingAdapters/clientToolContinuationBuilder.d.ts +21 -12
  103. package/lib/packlets/ai-assist/streamingAdapters/clientToolContinuationBuilder.d.ts.map +1 -1
  104. package/lib/packlets/ai-assist/streamingAdapters/clientToolContinuationBuilder.js +64 -34
  105. package/lib/packlets/ai-assist/streamingAdapters/clientToolContinuationBuilder.js.map +1 -1
  106. package/lib/packlets/ai-assist/streamingAdapters/common.d.ts +21 -0
  107. package/lib/packlets/ai-assist/streamingAdapters/common.d.ts.map +1 -1
  108. package/lib/packlets/ai-assist/streamingAdapters/common.js +25 -1
  109. package/lib/packlets/ai-assist/streamingAdapters/common.js.map +1 -1
  110. package/lib/packlets/ai-assist/streamingAdapters/gemini.d.ts.map +1 -1
  111. package/lib/packlets/ai-assist/streamingAdapters/gemini.js +12 -5
  112. package/lib/packlets/ai-assist/streamingAdapters/gemini.js.map +1 -1
  113. package/lib/packlets/ai-assist/streamingAdapters/openaiChat.d.ts +1 -1
  114. package/lib/packlets/ai-assist/streamingAdapters/openaiChat.d.ts.map +1 -1
  115. package/lib/packlets/ai-assist/streamingAdapters/openaiChat.js +33 -7
  116. package/lib/packlets/ai-assist/streamingAdapters/openaiChat.js.map +1 -1
  117. package/lib/packlets/ai-assist/streamingAdapters/openaiResponses.d.ts +1 -1
  118. package/lib/packlets/ai-assist/streamingAdapters/openaiResponses.d.ts.map +1 -1
  119. package/lib/packlets/ai-assist/streamingAdapters/openaiResponses.js +21 -13
  120. package/lib/packlets/ai-assist/streamingAdapters/openaiResponses.js.map +1 -1
  121. package/lib/packlets/ai-assist/streamingClient.d.ts.map +1 -1
  122. package/lib/packlets/ai-assist/streamingClient.js +3 -2
  123. package/lib/packlets/ai-assist/streamingClient.js.map +1 -1
  124. package/lib/packlets/ai-assist/thinkingOptionsResolver.d.ts.map +1 -1
  125. package/lib/packlets/ai-assist/thinkingOptionsResolver.js +18 -3
  126. package/lib/packlets/ai-assist/thinkingOptionsResolver.js.map +1 -1
  127. package/lib/packlets/ai-assist/toolFormats.d.ts +55 -1
  128. package/lib/packlets/ai-assist/toolFormats.d.ts.map +1 -1
  129. package/lib/packlets/ai-assist/toolFormats.js +82 -0
  130. package/lib/packlets/ai-assist/toolFormats.js.map +1 -1
  131. package/lib/packlets/ai-assist/toolTypes.d.ts +328 -0
  132. package/lib/packlets/ai-assist/toolTypes.d.ts.map +1 -0
  133. package/lib/packlets/ai-assist/toolTypes.js +22 -0
  134. package/lib/packlets/ai-assist/toolTypes.js.map +1 -0
  135. package/lib/packlets/ai-assist/usageNormalization.d.ts +58 -0
  136. package/lib/packlets/ai-assist/usageNormalization.d.ts.map +1 -0
  137. package/lib/packlets/ai-assist/usageNormalization.js +184 -0
  138. package/lib/packlets/ai-assist/usageNormalization.js.map +1 -0
  139. package/lib/packlets/ai-assist/usageTypes.d.ts +84 -0
  140. package/lib/packlets/ai-assist/usageTypes.d.ts.map +1 -0
  141. package/lib/packlets/ai-assist/usageTypes.js +22 -0
  142. package/lib/packlets/ai-assist/usageTypes.js.map +1 -0
  143. package/lib/packlets/mustache/index.d.ts +1 -1
  144. package/lib/packlets/mustache/index.d.ts.map +1 -1
  145. package/lib/packlets/mustache/index.js.map +1 -1
  146. package/lib/packlets/mustache/interfaces.d.ts +30 -0
  147. package/lib/packlets/mustache/interfaces.d.ts.map +1 -1
  148. package/lib/packlets/mustache/interfaces.js.map +1 -1
  149. package/lib/packlets/mustache/mustacheTemplate.d.ts +56 -1
  150. package/lib/packlets/mustache/mustacheTemplate.d.ts.map +1 -1
  151. package/lib/packlets/mustache/mustacheTemplate.js +91 -0
  152. package/lib/packlets/mustache/mustacheTemplate.js.map +1 -1
  153. package/package.json +16 -16
  154. package/perf/promptCacheAnthropicBreakpoint.js +135 -0
  155. package/perf/promptCacheObservability.js +107 -0
  156. package/perf/promptCacheRoutingAb.js +105 -0
@@ -0,0 +1,328 @@
1
+ /**
2
+ * Tool types for AI assist: server-side tool configs, client-defined ("function")
3
+ * tools, the streaming events a tool call produces, the provider-exclusion vocabulary,
4
+ * and the client-tool round-trip result types.
5
+ *
6
+ * @remarks
7
+ * Split out of `model.ts` when the provider-exclusion types pushed that file past the
8
+ * 2000-line lint ceiling. The cut is along a real seam rather than an arbitrary one:
9
+ * everything here is about tools and nothing here depends on the rest of `model.ts`,
10
+ * so the module is a leaf. `model.ts` re-exports all of it, which is why no consumer
11
+ * import changed.
12
+ * @packageDocumentation
13
+ */
14
+ import { type Result } from '@fgv/ts-utils';
15
+ import { type JsonObject, type JsonSchema } from '@fgv/ts-json-base';
16
+ /**
17
+ * Built-in server-side tool types supported across providers.
18
+ * @public
19
+ */
20
+ export type AiServerToolType = 'web_search';
21
+ /**
22
+ * Configuration specific to web search tools.
23
+ * @public
24
+ */
25
+ export interface IAiWebSearchToolConfig {
26
+ readonly type: 'web_search';
27
+ /** Optional: restrict search to these domains. */
28
+ readonly allowedDomains?: ReadonlyArray<string>;
29
+ /** Optional: exclude these domains from search. */
30
+ readonly blockedDomains?: ReadonlyArray<string>;
31
+ /** Optional: max number of searches per request. */
32
+ readonly maxUses?: number;
33
+ /**
34
+ * Optional: enable image understanding during web search.
35
+ * When true, the model can view and analyze images found during search.
36
+ * Currently supported by xAI only; ignored by other providers.
37
+ */
38
+ readonly enableImageUnderstanding?: boolean;
39
+ }
40
+ /**
41
+ * Union of all server-side tool configurations. Discriminated on `type`.
42
+ * @public
43
+ */
44
+ export type AiServerToolConfig = IAiWebSearchToolConfig;
45
+ /**
46
+ * Declares a tool as enabled/disabled in provider settings.
47
+ * Tools are disabled by default — consuming apps must opt in explicitly.
48
+ * @public
49
+ */
50
+ export interface IAiToolEnablement {
51
+ /** Which tool type. */
52
+ readonly type: AiServerToolType;
53
+ /** Whether this tool is enabled by default for this provider. */
54
+ readonly enabled: boolean;
55
+ /** Optional tool-specific configuration. */
56
+ readonly config?: AiServerToolConfig;
57
+ }
58
+ /**
59
+ * Behavior annotations for a client-defined tool.
60
+ *
61
+ * @remarks
62
+ * These are **host-advisory-only hints** describing a tool's side-effect profile.
63
+ * They are consumed by the host's tool loop (e.g. a before-execute gate) and are
64
+ * **never serialized to the model** — the provider wire tool-schemas whitelist
65
+ * `{name, description, parameters}` and ignore this field.
66
+ *
67
+ * Field names mirror MCP's `ToolAnnotations` (`@modelcontextprotocol/sdk`) 1:1 so
68
+ * an MCP tool's annotations pass through unchanged. Per the MCP spec, all fields
69
+ * are hints — a host should never make tool-use decisions based on annotations
70
+ * received from an untrusted server without its own validation.
71
+ *
72
+ * @public
73
+ */
74
+ export interface IAiToolAnnotations {
75
+ /** Optional human-readable display title for the tool. */
76
+ readonly title?: string;
77
+ /** Hint: the tool does not modify its environment (read-only). */
78
+ readonly readOnlyHint?: boolean;
79
+ /** Hint: the tool may perform destructive updates (only meaningful when not read-only). */
80
+ readonly destructiveHint?: boolean;
81
+ /** Hint: repeated calls with the same arguments have no additional effect. */
82
+ readonly idempotentHint?: boolean;
83
+ /** Hint: the tool interacts with an open world of external entities. */
84
+ readonly openWorldHint?: boolean;
85
+ }
86
+ /**
87
+ * Configuration for a client-defined (harness-supplied) tool.
88
+ *
89
+ * @remarks
90
+ * The `parametersSchema` is the single source of truth for both the wire-format
91
+ * JSON Schema sent to the provider (via `.toJson()`) and the runtime argument
92
+ * validation (via `.validate(rawArgs)`). Use `JsonSchema.object(...)` from
93
+ * `@fgv/ts-json-base` to author the schema as a const (e.g. `const mySchema = JsonSchema.object({...})`);
94
+ * the static type `TParams` is then derived via `JsonSchema.Static<typeof mySchema>` —
95
+ * no drift between wire schema and runtime validation.
96
+ *
97
+ * @public
98
+ */
99
+ export interface IAiClientToolConfig<TParams = unknown> {
100
+ /** Discriminator — always `'client_tool'`. */
101
+ readonly type: 'client_tool';
102
+ /** Tool name sent to the model (must be unique within a call). */
103
+ readonly name: string;
104
+ /** Human-readable description of what the tool does, shown to the model. */
105
+ readonly description: string;
106
+ /**
107
+ * JSON Schema validator for the tool's parameters. Emits wire format via
108
+ * `.toJson()` and validates model-returned args via `.validate(rawArgs)`.
109
+ */
110
+ readonly parametersSchema: JsonSchema.ISchemaValidator<TParams>;
111
+ /**
112
+ * Optional host-advisory behavior annotations (read-only / destructive /
113
+ * idempotent / open-world hints + display title). Consumed by the host's
114
+ * tool loop; never serialized to the model. See {@link AiAssist.IAiToolAnnotations}.
115
+ */
116
+ readonly annotations?: IAiToolAnnotations;
117
+ }
118
+ /**
119
+ * A client-defined tool: configuration + execution callback pair.
120
+ *
121
+ * @remarks
122
+ * The `execute` callback receives typed `TParams` (already validated by
123
+ * `config.parametersSchema.validate()`) and returns a `Promise<Result<unknown>>`.
124
+ * Thrown errors are caught via `captureAsyncResult` in the round-trip helper.
125
+ *
126
+ * @public
127
+ */
128
+ export interface IAiClientTool<TParams = unknown> {
129
+ /** The tool's configuration (name, description, parameters schema). */
130
+ readonly config: IAiClientToolConfig<TParams>;
131
+ /**
132
+ * Execute the tool with validated parameters.
133
+ * @param args - Typed arguments, already validated against `config.parametersSchema`.
134
+ * @returns A `Promise<Result<unknown>>` — the result is stringified and sent back to the model.
135
+ */
136
+ readonly execute: (args: TParams) => Promise<Result<unknown>>;
137
+ }
138
+ /**
139
+ * Union of all tool configurations: server-side or client-defined.
140
+ * Discriminated on `type`.
141
+ * @public
142
+ */
143
+ export type AiToolConfig = AiServerToolConfig | IAiClientToolConfig;
144
+ /**
145
+ * Emitted when a client-defined tool call begins streaming. Carries the tool name
146
+ * and optional provider-assigned call ID (Anthropic / OpenAI Responses API; absent
147
+ * for Gemini which does not assign call IDs).
148
+ * @public
149
+ */
150
+ export interface IAiStreamToolUseStart {
151
+ readonly type: 'client-tool-call-start';
152
+ /** The name of the client tool being called. */
153
+ readonly toolName: string;
154
+ /**
155
+ * Provider-assigned call identifier (Anthropic: `toolu_*`; OpenAI: `call_*`).
156
+ * Absent for Gemini (correlation by name).
157
+ */
158
+ readonly callId?: string;
159
+ }
160
+ /**
161
+ * Emitted when a client-defined tool call is complete and its arguments are fully
162
+ * accumulated. The `args` object is the fully parsed JSON object — no further
163
+ * streaming deltas follow for this call.
164
+ * @public
165
+ */
166
+ export interface IAiStreamToolUseDelta {
167
+ readonly type: 'client-tool-call-done';
168
+ /** The name of the client tool being called. */
169
+ readonly toolName: string;
170
+ /**
171
+ * Provider-assigned call identifier. Absent for Gemini.
172
+ */
173
+ readonly callId?: string;
174
+ /** The fully accumulated and parsed tool arguments. */
175
+ readonly args: JsonObject;
176
+ }
177
+ /**
178
+ * Emitted after a client-defined tool has been executed and the result is ready
179
+ * to be fed back to the model in the round-trip continuation.
180
+ * @public
181
+ */
182
+ export interface IAiStreamToolUseComplete {
183
+ readonly type: 'client-tool-result';
184
+ /** The name of the client tool that was executed. */
185
+ readonly toolName: string;
186
+ /**
187
+ * Provider-assigned call identifier. Absent for Gemini.
188
+ */
189
+ readonly callId?: string;
190
+ /** The stringified result returned by the tool's execute callback. */
191
+ readonly result: string;
192
+ /** Whether the tool execution failed (schema validation failure, execute error, or unknown tool). */
193
+ readonly isError: boolean;
194
+ }
195
+ /**
196
+ * What to do when a request would combine a server tool with client (function)
197
+ * tools on a provider that declares the two mutually exclusive (see
198
+ * `IAiProviderDescriptor.serverToolsExclusiveWithClientTools`).
199
+ *
200
+ * @remarks
201
+ * - `'drop-server-tools'` (the default) removes the offending server tools and
202
+ * keeps every client tool. Client tools are what the host registered
203
+ * explicitly for this turn; a built-in like grounding is a per-provider
204
+ * nicety, and dropping it degrades the turn rather than losing it.
205
+ * - `'prefer-server-tools'` is the mirror image: keep the server tools and drop
206
+ * every client tool. For hosts whose turn is *about* the grounded answer.
207
+ * - `'fail'` refuses the request up front with a message naming the conflict —
208
+ * the behavior before this policy existed. For hosts that would rather
209
+ * choose themselves than have either half silently disappear.
210
+ *
211
+ * Whichever is chosen, what actually happened is reported, never inferred: see
212
+ * {@link AiAssist.IAiToolConflictReport}.
213
+ * @public
214
+ */
215
+ export type AiToolConflictPolicy = 'drop-server-tools' | 'prefer-server-tools' | 'fail';
216
+ /**
217
+ * What the conflict resolution did to a turn's tools.
218
+ *
219
+ * @remarks
220
+ * Required on {@link AiAssist.IAiClientToolTurnResult}, not optional, for the same reason
221
+ * `structuredOutput` is required on a completion response: an absent report is
222
+ * three-ways ambiguous — nothing conflicted / this build predates the feature /
223
+ * the resolution ran and said nothing — and disambiguating exactly that is what
224
+ * it is for. A turn where nothing conflicted reports the policy that was in
225
+ * force and two empty arrays.
226
+ * @public
227
+ */
228
+ export interface IAiToolConflictReport {
229
+ /** The policy that was in force for this turn (the default when none was passed). */
230
+ readonly policy: AiToolConflictPolicy;
231
+ /** Server tool types removed from the request. Empty when nothing was dropped. */
232
+ readonly droppedServerTools: ReadonlyArray<AiServerToolType>;
233
+ /** Names of client tools removed from the request. Empty when nothing was dropped. */
234
+ readonly droppedClientTools: ReadonlyArray<string>;
235
+ }
236
+ /**
237
+ * Summary of a single client tool call within a turn: the tool name, call ID,
238
+ * raw arguments, execution result, and whether the execution was an error.
239
+ * @public
240
+ */
241
+ export interface IAiClientToolCallSummary {
242
+ /** The name of the tool that was called. */
243
+ readonly toolName: string;
244
+ /** Provider-assigned call identifier (absent for Gemini). */
245
+ readonly callId?: string;
246
+ /** The fully accumulated raw arguments object as parsed JSON. */
247
+ readonly args: JsonObject;
248
+ /** The stringified result (success value or error message). */
249
+ readonly result: string;
250
+ /** Whether execution failed (schema validation failure, execute error, or unknown tool). */
251
+ readonly isError: boolean;
252
+ }
253
+ /**
254
+ * The provider-specific continuation data needed to build the follow-up request
255
+ * for the next round of the conversation.
256
+ *
257
+ * @remarks
258
+ * `messages` are provider-native request objects (Anthropic: content-block arrays,
259
+ * OpenAI Responses API: input items, Gemini: content parts). The continuation
260
+ * builder in `clientToolContinuationBuilder.ts` populates this.
261
+ *
262
+ * @public
263
+ */
264
+ export interface IAiClientToolContinuation {
265
+ /**
266
+ * **Cumulative** provider-native wire-format message objects covering all
267
+ * tool rounds so far. On each turn, `executeClientToolTurn` prepends the
268
+ * inbound `continuationMessages` so that this array always contains the
269
+ * complete wire tail from round 1 through the current round.
270
+ *
271
+ * To drive a multi-round loop, simply **replace** `continuationMessages`
272
+ * with this value — do not manually concatenate:
273
+ *
274
+ * ```ts
275
+ * let tail: JsonObject[] | undefined;
276
+ * while (true) {
277
+ * const { events, nextTurn } = executeClientToolTurn({
278
+ * ..., continuationMessages: tail
279
+ * }).orThrow();
280
+ * for await (const e of events) { /* observe *\/ }
281
+ * const outcome = (await nextTurn).orThrow();
282
+ * if (!outcome.continuation) break;
283
+ * tail = [...outcome.continuation.messages]; // replace — already cumulative
284
+ * }
285
+ * ```
286
+ *
287
+ * The exact shape is provider-native and may include provider-specific
288
+ * blocks (e.g. Anthropic thinking/redacted_thinking/tool_use, OpenAI
289
+ * function_call/function_call_output items, Gemini functionCall/functionResponse
290
+ * parts). These are NOT `IChatMessage[]` and must NOT be placed in the
291
+ * `messages` parameter — the normalized-message path strips provider-native
292
+ * fields (thinking signatures, redacted_thinking data) that the server
293
+ * requires for continuation validation.
294
+ *
295
+ * `toolCallsSummary` is per-round only (the calls executed in the current
296
+ * turn). Only `messages` is cumulative.
297
+ */
298
+ readonly messages: ReadonlyArray<JsonObject>;
299
+ /** Summary of each tool call executed in this turn (per-round, not cumulative). */
300
+ readonly toolCallsSummary: ReadonlyArray<IAiClientToolCallSummary>;
301
+ }
302
+ /**
303
+ * The result of a single client-tool turn: the optional continuation for the next
304
+ * call (absent when no tool calls occurred) and whether the stream was truncated.
305
+ * @public
306
+ */
307
+ export interface IAiClientToolTurnResult {
308
+ /**
309
+ * The continuation data for the next round-trip. `undefined` when the model
310
+ * completed without invoking any client tools.
311
+ */
312
+ readonly continuation: IAiClientToolContinuation | undefined;
313
+ /** Whether the stream was truncated (token limit or stop reason). */
314
+ readonly truncated: boolean;
315
+ /** The full concatenated text from all `text-delta` events in this turn. */
316
+ readonly fullText: string;
317
+ /**
318
+ * What the provider tool-conflict resolution did to this turn's tools.
319
+ * Always present; empty arrays mean nothing conflicted.
320
+ *
321
+ * @remarks
322
+ * Read this to tell the person (or the model, on a later turn) that a
323
+ * capability they expected was unavailable — e.g. that web search was dropped
324
+ * because the pinned model cannot ground and call functions in one request.
325
+ */
326
+ readonly toolConflicts: IAiToolConflictReport;
327
+ }
328
+ //# sourceMappingURL=toolTypes.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"toolTypes.d.ts","sourceRoot":"","sources":["../../../src/packlets/ai-assist/toolTypes.ts"],"names":[],"mappings":"AAoBA;;;;;;;;;;;;GAYG;AAEH,OAAO,EAAE,KAAK,MAAM,EAAE,MAAM,eAAe,CAAC;AAC5C,OAAO,EAAE,KAAK,UAAU,EAAE,KAAK,UAAU,EAAE,MAAM,mBAAmB,CAAC;AAMrE;;;GAGG;AACH,MAAM,MAAM,gBA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tC;;;OAGG;IACH,QAAQ,CAAC,YAAY,EAAE,yBAAyB,GAAG,SAAS,CAAC;IAC7D,qEAAqE;IACrE,QAAQ,CAAC,SAAS,EAAE,OAAO,CAAC;IAC5B,4EAA4E;IAC5E,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;IAC1B;;;;;;;;OAQG;IACH,QAAQ,CAAC,aAAa,EAAE,qBAAqB,CAAC;CAC/C"}
@@ -0,0 +1,22 @@
1
+ "use strict";
2
+ // Copyright (c) 2026 Erik Fortune
3
+ //
4
+ // Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ // of this software and associated documentation files (the "Software"), to deal
6
+ // in the Software without restriction, including without limitation the rights
7
+ // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ // copies of the Software, and to permit persons to whom the Software is
9
+ // furnished to do so, subject to the following conditions:
10
+ //
11
+ // The above copyright notice and this permission notice shall be included in all
12
+ // copies or substantial portions of the Software.
13
+ //
14
+ // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
+ // SOFTWARE.
21
+ Object.defineProperty(exports, "__esModule", { value: true });
22
+ //# sourceMappingURL=toolTypes.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"toolTypes.js","sourceRoot":"","sources":["../../../src/packlets/ai-assist/toolTypes.ts"],"names":[],"mappings":";AAAA,kCAAkC;AAClC,EAAE;AACF,+EAA+E;AAC/E,gFAAgF;AAChF,+EAA+E;AAC/E,4EAA4E;AAC5E,wEAAwE;AACxE,2DAA2D;AAC3D,EAAE;AACF,iFAAiF;AACjF,kDAAkD;AAClD,EAAE;AACF,6EAA6E;AAC7E,2EAA2E;AAC3E,8EAA8E;AAC9E,yEAAyE;AACzE,gFAAgF;AAChF,gFAAgF;AAChF,YAAY","sourcesContent":["// Copyright (c) 2026 Erik Fortune\n//\n// Permission is hereby granted, free of charge, to any person obtaining a copy\n// of this software and associated documentation files (the \"Software\"), to deal\n// in the Software without restriction, including without limitation the rights\n// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n// copies of the Software, and to permit persons to whom the Software is\n// furnished to do so, subject to the following conditions:\n//\n// The above copyright notice and this permission notice shall be included in all\n// copies or substantial portions of the Software.\n//\n// THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n// SOFTWARE.\n\n/**\n * Tool types for AI assist: server-side tool configs, client-defined (\"function\")\n * tools, the streaming events a tool call produces, the provider-exclusion vocabulary,\n * and the client-tool round-trip result types.\n *\n * @remarks\n * Split out of `model.ts` when the provider-exclusion types pushed that file past the\n * 2000-line lint ceiling. The cut is along a real seam rather than an arbitrary one:\n * everything here is about tools and nothing here depends on the rest of `model.ts`,\n * so the module is a leaf. `model.ts` re-exports all of it, which is why no consumer\n * import changed.\n * @packageDocumentation\n */\n\nimport { type Result } from '@fgv/ts-utils';\nimport { type JsonObject, type JsonSchema } from '@fgv/ts-json-base';\n\n// ============================================================================\n// Server-Side Tools\n// ============================================================================\n\n/**\n * Built-in server-side tool types supported across providers.\n * @public\n */\nexport type AiServerToolType = 'web_search';\n\n/**\n * Configuration specific to web search tools.\n * @public\n */\nexport interface IAiWebSearchToolConfig {\n readonly type: 'web_search';\n /** Optional: restrict search to these domains. */\n readonly allowedDomains?: ReadonlyArray<string>;\n /** Optional: exclude these domains from search. */\n readonly blockedDomains?: ReadonlyArray<string>;\n /** Optional: max number of searches per request. */\n readonly maxUses?: number;\n /**\n * Optional: enable image understanding during web search.\n * When true, the model can view and analyze images found during search.\n * Currently supported by xAI only; ignored by other providers.\n */\n readonly enableImageUnderstanding?: boolean;\n}\n\n/**\n * Union of all server-side tool configurations. Discriminated on `type`.\n * @public\n */\nexport type AiServerToolConfig = IAiWebSearchToolConfig;\n\n/**\n * Declares a tool as enabled/disabled in provider settings.\n * Tools are disabled by default — consuming apps must opt in explicitly.\n * @public\n */\nexport interface IAiToolEnablement {\n /** Which tool type. */\n readonly type: AiServerToolType;\n /** Whether this tool is enabled by default for this provider. */\n readonly enabled: boolean;\n /** Optional tool-specific configuration. */\n readonly config?: AiServerToolConfig;\n}\n\n// ============================================================================\n// Client-Defined Tools\n// ============================================================================\n\n/**\n * Behavior annotations for a client-defined tool.\n *\n * @remarks\n * These are **host-advisory-only hints** describing a tool's side-effect profile.\n * They are consumed by the host's tool loop (e.g. a before-execute gate) and are\n * **never serialized to the model** — the provider wire tool-schemas whitelist\n * `{name, description, parameters}` and ignore this field.\n *\n * Field names mirror MCP's `ToolAnnotations` (`@modelcontextprotocol/sdk`) 1:1 so\n * an MCP tool's annotations pass through unchanged. Per the MCP spec, all fields\n * are hints — a host should never make tool-use decisions based on annotations\n * received from an untrusted server without its own validation.\n *\n * @public\n */\nexport interface IAiToolAnnotations {\n /** Optional human-readable display title for the tool. */\n readonly title?: string;\n /** Hint: the tool does not modify its environment (read-only). */\n readonly readOnlyHint?: boolean;\n /** Hint: the tool may perform destructive updates (only meaningful when not read-only). */\n readonly destructiveHint?: boolean;\n /** Hint: repeated calls with the same arguments have no additional effect. */\n readonly idempotentHint?: boolean;\n /** Hint: the tool interacts with an open world of external entities. */\n readonly openWorldHint?: boolean;\n}\n\n/**\n * Configuration for a client-defined (harness-supplied) tool.\n *\n * @remarks\n * The `parametersSchema` is the single source of truth for both the wire-format\n * JSON Schema sent to the provider (via `.toJson()`) and the runtime argument\n * validation (via `.validate(rawArgs)`). Use `JsonSchema.object(...)` from\n * `@fgv/ts-json-base` to author the schema as a const (e.g. `const mySchema = JsonSchema.object({...})`);\n * the static type `TParams` is then derived via `JsonSchema.Static<typeof mySchema>` —\n * no drift between wire schema and runtime validation.\n *\n * @public\n */\nexport interface IAiClientToolConfig<TParams = unknown> {\n /** Discriminator — always `'client_tool'`. */\n readonly type: 'client_tool';\n /** Tool name sent to the model (must be unique within a call). */\n readonly name: string;\n /** Human-readable description of what the tool does, shown to the model. */\n readonly description: string;\n /**\n * JSON Schema validator for the tool's parameters. Emits wire format via\n * `.toJson()` and validates model-returned args via `.validate(rawArgs)`.\n */\n readonly parametersSchema: JsonSchema.ISchemaValidator<TParams>;\n /**\n * Optional host-advisory behavior annotations (read-only / destructive /\n * idempotent / open-world hints + display title). Consumed by the host's\n * tool loop; never serialized to the model. See {@link AiAssist.IAiToolAnnotations}.\n */\n readonly annotations?: IAiToolAnnotations;\n}\n\n/**\n * A client-defined tool: configuration + execution callback pair.\n *\n * @remarks\n * The `execute` callback receives typed `TParams` (already validated by\n * `config.parametersSchema.validate()`) and returns a `Promise<Result<unknown>>`.\n * Thrown errors are caught via `captureAsyncResult` in the round-trip helper.\n *\n * @public\n */\nexport interface IAiClientTool<TParams = unknown> {\n /** The tool's configuration (name, description, parameters schema). */\n readonly config: IAiClientToolConfig<TParams>;\n /**\n * Execute the tool with validated parameters.\n * @param args - Typed arguments, already validated against `config.parametersSchema`.\n * @returns A `Promise<Result<unknown>>` — the result is stringified and sent back to the model.\n */\n readonly execute: (args: TParams) => Promise<Result<unknown>>;\n}\n\n/**\n * Union of all tool configurations: server-side or client-defined.\n * Discriminated on `type`.\n * @public\n */\nexport type AiToolConfig = AiServerToolConfig | IAiClientToolConfig;\n\n// ============================================================================\n// Client Tool Streaming Events\n// ============================================================================\n\n/**\n * Emitted when a client-defined tool call begins streaming. Carries the tool name\n * and optional provider-assigned call ID (Anthropic / OpenAI Responses API; absent\n * for Gemini which does not assign call IDs).\n * @public\n */\nexport interface IAiStreamToolUseStart {\n readonly type: 'client-tool-call-start';\n /** The name of the client tool being called. */\n readonly toolName: string;\n /**\n * Provider-assigned call identifier (Anthropic: `toolu_*`; OpenAI: `call_*`).\n * Absent for Gemini (correlation by name).\n */\n readonly callId?: string;\n}\n\n/**\n * Emitted when a client-defined tool call is complete and its arguments are fully\n * accumulated. The `args` object is the fully parsed JSON object — no further\n * streaming deltas follow for this call.\n * @public\n */\nexport interface IAiStreamToolUseDelta {\n readonly type: 'client-tool-call-done';\n /** The name of the client tool being called. */\n readonly toolName: string;\n /**\n * Provider-assigned call identifier. Absent for Gemini.\n */\n readonly callId?: string;\n /** The fully accumulated and parsed tool arguments. */\n readonly args: JsonObject;\n}\n\n/**\n * Emitted after a client-defined tool has been executed and the result is ready\n * to be fed back to the model in the round-trip continuation.\n * @public\n */\nexport interface IAiStreamToolUseComplete {\n readonly type: 'client-tool-result';\n /** The name of the client tool that was executed. */\n readonly toolName: string;\n /**\n * Provider-assigned call identifier. Absent for Gemini.\n */\n readonly callId?: string;\n /** The stringified result returned by the tool's execute callback. */\n readonly result: string;\n /** Whether the tool execution failed (schema validation failure, execute error, or unknown tool). */\n readonly isError: boolean;\n}\n\n// ============================================================================\n// Server/Client Tool Conflicts\n// ============================================================================\n\n/**\n * What to do when a request would combine a server tool with client (function)\n * tools on a provider that declares the two mutually exclusive (see\n * `IAiProviderDescriptor.serverToolsExclusiveWithClientTools`).\n *\n * @remarks\n * - `'drop-server-tools'` (the default) removes the offending server tools and\n * keeps every client tool. Client tools are what the host registered\n * explicitly for this turn; a built-in like grounding is a per-provider\n * nicety, and dropping it degrades the turn rather than losing it.\n * - `'prefer-server-tools'` is the mirror image: keep the server tools and drop\n * every client tool. For hosts whose turn is *about* the grounded answer.\n * - `'fail'` refuses the request up front with a message naming the conflict —\n * the behavior before this policy existed. For hosts that would rather\n * choose themselves than have either half silently disappear.\n *\n * Whichever is chosen, what actually happened is reported, never inferred: see\n * {@link AiAssist.IAiToolConflictReport}.\n * @public\n */\nexport type AiToolConflictPolicy = 'drop-server-tools' | 'prefer-server-tools' | 'fail';\n\n/**\n * What the conflict resolution did to a turn's tools.\n *\n * @remarks\n * Required on {@link AiAssist.IAiClientToolTurnResult}, not optional, for the same reason\n * `structuredOutput` is required on a completion response: an absent report is\n * three-ways ambiguous — nothing conflicted / this build predates the feature /\n * the resolution ran and said nothing — and disambiguating exactly that is what\n * it is for. A turn where nothing conflicted reports the policy that was in\n * force and two empty arrays.\n * @public\n */\nexport interface IAiToolConflictReport {\n /** The policy that was in force for this turn (the default when none was passed). */\n readonly policy: AiToolConflictPolicy;\n /** Server tool types removed from the request. Empty when nothing was dropped. */\n readonly droppedServerTools: ReadonlyArray<AiServerToolType>;\n /** Names of client tools removed from the request. Empty when nothing was dropped. */\n readonly droppedClientTools: ReadonlyArray<string>;\n}\n\n// ============================================================================\n// Client Tool Round-Trip Types\n// ============================================================================\n\n/**\n * Summary of a single client tool call within a turn: the tool name, call ID,\n * raw arguments, execution result, and whether the execution was an error.\n * @public\n */\nexport interface IAiClientToolCallSummary {\n /** The name of the tool that was called. */\n readonly toolName: string;\n /** Provider-assigned call identifier (absent for Gemini). */\n readonly callId?: string;\n /** The fully accumulated raw arguments object as parsed JSON. */\n readonly args: JsonObject;\n /** The stringified result (success value or error message). */\n readonly result: string;\n /** Whether execution failed (schema validation failure, execute error, or unknown tool). */\n readonly isError: boolean;\n}\n\n/**\n * The provider-specific continuation data needed to build the follow-up request\n * for the next round of the conversation.\n *\n * @remarks\n * `messages` are provider-native request objects (Anthropic: content-block arrays,\n * OpenAI Responses API: input items, Gemini: content parts). The continuation\n * builder in `clientToolContinuationBuilder.ts` populates this.\n *\n * @public\n */\nexport interface IAiClientToolContinuation {\n /**\n * **Cumulative** provider-native wire-format message objects covering all\n * tool rounds so far. On each turn, `executeClientToolTurn` prepends the\n * inbound `continuationMessages` so that this array always contains the\n * complete wire tail from round 1 through the current round.\n *\n * To drive a multi-round loop, simply **replace** `continuationMessages`\n * with this value — do not manually concatenate:\n *\n * ```ts\n * let tail: JsonObject[] | undefined;\n * while (true) {\n * const { events, nextTurn } = executeClientToolTurn({\n * ..., continuationMessages: tail\n * }).orThrow();\n * for await (const e of events) { /* observe *\\/ }\n * const outcome = (await nextTurn).orThrow();\n * if (!outcome.continuation) break;\n * tail = [...outcome.continuation.messages]; // replace — already cumulative\n * }\n * ```\n *\n * The exact shape is provider-native and may include provider-specific\n * blocks (e.g. Anthropic thinking/redacted_thinking/tool_use, OpenAI\n * function_call/function_call_output items, Gemini functionCall/functionResponse\n * parts). These are NOT `IChatMessage[]` and must NOT be placed in the\n * `messages` parameter — the normalized-message path strips provider-native\n * fields (thinking signatures, redacted_thinking data) that the server\n * requires for continuation validation.\n *\n * `toolCallsSummary` is per-round only (the calls executed in the current\n * turn). Only `messages` is cumulative.\n */\n readonly messages: ReadonlyArray<JsonObject>;\n /** Summary of each tool call executed in this turn (per-round, not cumulative). */\n readonly toolCallsSummary: ReadonlyArray<IAiClientToolCallSummary>;\n}\n\n/**\n * The result of a single client-tool turn: the optional continuation for the next\n * call (absent when no tool calls occurred) and whether the stream was truncated.\n * @public\n */\nexport interface IAiClientToolTurnResult {\n /**\n * The continuation data for the next round-trip. `undefined` when the model\n * completed without invoking any client tools.\n */\n readonly continuation: IAiClientToolContinuation | undefined;\n /** Whether the stream was truncated (token limit or stop reason). */\n readonly truncated: boolean;\n /** The full concatenated text from all `text-delta` events in this turn. */\n readonly fullText: string;\n /**\n * What the provider tool-conflict resolution did to this turn's tools.\n * Always present; empty arrays mean nothing conflicted.\n *\n * @remarks\n * Read this to tell the person (or the model, on a later turn) that a\n * capability they expected was unavailable — e.g. that web search was dropped\n * because the pinned model cannot ground and call functions in one request.\n */\n readonly toolConflicts: IAiToolConflictReport;\n}\n"]}
@@ -0,0 +1,58 @@
1
+ /**
2
+ * Per-wire-shape `usage` block normalization, shared by the non-streaming
3
+ * adapters (`completionClient.ts`) and the streaming adapters
4
+ * (`streamingAdapters/*.ts`) so the same JSON layout is parsed once.
5
+ *
6
+ * @remarks
7
+ * Every function here follows one rule throughout: a field that is absent
8
+ * from the wire response stays absent on {@link AiAssist.IAiCompletionUsage} —
9
+ * never defaulted to `0`, and never derived from a sibling field unless the
10
+ * value that sibling would need is itself known. See `IAiCompletionUsage`
11
+ * for why.
12
+ * @packageDocumentation
13
+ */
14
+ import { type JsonObject } from '@fgv/ts-json-base';
15
+ import { type IAiCompletionUsage } from './usageTypes';
16
+ /**
17
+ * Normalizes an Anthropic Messages API `usage` block. Anthropic always
18
+ * reports both reads and writes, so `reports` is unconditionally
19
+ * `'reads-and-writes'` — an absent `cache_creation_input_tokens` genuinely
20
+ * means zero tokens were written this request.
21
+ * @internal
22
+ */
23
+ export declare function normalizeAnthropicUsage(raw: JsonObject | undefined): IAiCompletionUsage | undefined;
24
+ /**
25
+ * Normalizes an OpenAI/xAI Chat Completions `usage` block. `reports` is
26
+ * unconditionally `'reads'`: `cache_write_tokens` does not exist on this API
27
+ * for any provider reached through it, so `cacheWriteTokens` is never set —
28
+ * not even to `0`.
29
+ * @internal
30
+ */
31
+ export declare function normalizeOpenAiChatUsage(raw: JsonObject | undefined): IAiCompletionUsage | undefined;
32
+ /**
33
+ * Normalizes an OpenAI/xAI Responses API `usage` block. This route is shared
34
+ * by two providers that disagree about write reporting — OpenAI always sends
35
+ * `cache_write_tokens` (even when `0`), xAI never does — so `reports` is
36
+ * derived from field **presence**, not from provider identity: a wire that
37
+ * sends the field, however small its value, is `'reads-and-writes'`; a wire
38
+ * that omits it entirely is `'reads'`.
39
+ * @internal
40
+ */
41
+ export declare function normalizeOpenAiResponsesUsage(raw: JsonObject | undefined): IAiCompletionUsage | undefined;
42
+ /**
43
+ * Normalizes a Gemini `usageMetadata` block. `reports` is unconditionally
44
+ * `'reads'` — Gemini has no cache-write concept in a `generateContent` call;
45
+ * writes happen out-of-band via the explicit `cachedContents` resource,
46
+ * out of scope per the design (§10).
47
+ *
48
+ * @remarks
49
+ * The Gemini-specific footgun: `promptTokenCount` already **includes**
50
+ * `cachedContentTokenCount` (it is the total effective prompt size), unlike
51
+ * OpenAI where the cached figure sits in a sub-object alongside a separate
52
+ * total. `uncachedInputTokens` subtracts to correct for it — but only when
53
+ * `cachedContentTokenCount` is itself present, per the module's absent-means-
54
+ * absent rule.
55
+ * @internal
56
+ */
57
+ export declare function normalizeGeminiUsage(raw: JsonObject | undefined): IAiCompletionUsage | undefined;
58
+ //# sourceMappingURL=usageNormalization.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"usageNormalization.d.ts","sourceRoot":"","sources":["../../../src/packlets/ai-assist/usageNormalization.ts"],"names":[],"mappings":"AAoBA;;;;;;;;;;;;GAYG;AAIH,OAAO,EAAE,KAAK,UAAU,EAAE,MAAM,mBAAmB,CAAC;AAGpD,OAAO,EAA8B,KAAK,kBAAkB,EAAE,MAAM,cAAc,CAAC;AAqBnF;;;;;;GAMG;AACH,wBAAgB,uBAAuB,CAAC,GAAG,EAAE,UAAU,GAAG,SAAS,GAAG,kBAAkB,GAAG,SAAS,CAuBnG;AA2BD;;;;;;GAMG;AACH,wBAAgB,wBAAwB,CAAC,GAAG,EAAE,UAAU,GAAG,SAAS,GAAG,kBAAkB,GAAG,SAAS,CA6BpG;AAgCD;;;;;;;;GAQG;AACH,wBAAgB,6BAA6B,CAAC,GAAG,EAAE,UAAU,GAAG,SAAS,GAAG,kBAAkB,GAAG,SAAS,CA4BzG;AAmBD;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,oBAAoB,CAAC,GAAG,EAAE,UAAU,GAAG,SAAS,GAAG,kBAAkB,GAAG,SAAS,CA4BhG"}
@@ -0,0 +1,184 @@
1
+ "use strict";
2
+ // Copyright (c) 2026 Erik Fortune
3
+ //
4
+ // Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ // of this software and associated documentation files (the "Software"), to deal
6
+ // in the Software without restriction, including without limitation the rights
7
+ // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ // copies of the Software, and to permit persons to whom the Software is
9
+ // furnished to do so, subject to the following conditions:
10
+ //
11
+ // The above copyright notice and this permission notice shall be included in all
12
+ // copies or substantial portions of the Software.
13
+ //
14
+ // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
+ // SOFTWARE.
21
+ Object.defineProperty(exports, "__esModule", { value: true });
22
+ exports.normalizeAnthropicUsage = normalizeAnthropicUsage;
23
+ exports.normalizeOpenAiChatUsage = normalizeOpenAiChatUsage;
24
+ exports.normalizeOpenAiResponsesUsage = normalizeOpenAiResponsesUsage;
25
+ exports.normalizeGeminiUsage = normalizeGeminiUsage;
26
+ const ts_utils_1 = require("@fgv/ts-utils");
27
+ const anthropicRawUsage = ts_utils_1.Validators.object({
28
+ input_tokens: ts_utils_1.Validators.number,
29
+ output_tokens: ts_utils_1.Validators.number.optional(),
30
+ cache_creation_input_tokens: ts_utils_1.Validators.number.optional(),
31
+ cache_read_input_tokens: ts_utils_1.Validators.number.optional()
32
+ });
33
+ /**
34
+ * Normalizes an Anthropic Messages API `usage` block. Anthropic always
35
+ * reports both reads and writes, so `reports` is unconditionally
36
+ * `'reads-and-writes'` — an absent `cache_creation_input_tokens` genuinely
37
+ * means zero tokens were written this request.
38
+ * @internal
39
+ */
40
+ function normalizeAnthropicUsage(raw) {
41
+ if (raw === undefined) {
42
+ return undefined;
43
+ }
44
+ const parsed = anthropicRawUsage.validate(raw);
45
+ if (parsed.isFailure()) {
46
+ return undefined;
47
+ }
48
+ const { input_tokens: uncachedInputTokens, output_tokens: outputTokens, cache_creation_input_tokens: cacheWriteTokens, cache_read_input_tokens: cachedInputTokens } = parsed.value;
49
+ return {
50
+ reports: 'reads-and-writes',
51
+ uncachedInputTokens,
52
+ cachedInputTokens,
53
+ cacheWriteTokens,
54
+ outputTokens,
55
+ totalInputTokens: cachedInputTokens !== undefined ? uncachedInputTokens + cachedInputTokens : undefined,
56
+ raw
57
+ };
58
+ }
59
+ const openAiChatPromptTokensDetails = ts_utils_1.Validators.object({ cached_tokens: ts_utils_1.Validators.number.optional() });
60
+ const openAiChatRawUsage = ts_utils_1.Validators.object({
61
+ prompt_tokens: ts_utils_1.Validators.number.optional(),
62
+ completion_tokens: ts_utils_1.Validators.number.optional(),
63
+ prompt_tokens_details: openAiChatPromptTokensDetails.optional()
64
+ });
65
+ /**
66
+ * Normalizes an OpenAI/xAI Chat Completions `usage` block. `reports` is
67
+ * unconditionally `'reads'`: `cache_write_tokens` does not exist on this API
68
+ * for any provider reached through it, so `cacheWriteTokens` is never set —
69
+ * not even to `0`.
70
+ * @internal
71
+ */
72
+ function normalizeOpenAiChatUsage(raw) {
73
+ if (raw === undefined) {
74
+ return undefined;
75
+ }
76
+ const parsed = openAiChatRawUsage.validate(raw);
77
+ if (parsed.isFailure()) {
78
+ return undefined;
79
+ }
80
+ const { prompt_tokens: promptTokens, completion_tokens: outputTokens, prompt_tokens_details } = parsed.value;
81
+ const cachedInputTokens = prompt_tokens_details === null || prompt_tokens_details === void 0 ? void 0 : prompt_tokens_details.cached_tokens;
82
+ const uncachedInputTokens = cachedInputTokens !== undefined && promptTokens !== undefined
83
+ ? promptTokens - cachedInputTokens
84
+ : undefined;
85
+ return {
86
+ reports: 'reads',
87
+ uncachedInputTokens,
88
+ cachedInputTokens,
89
+ outputTokens,
90
+ totalInputTokens: uncachedInputTokens !== undefined && cachedInputTokens !== undefined
91
+ ? uncachedInputTokens + cachedInputTokens
92
+ : undefined,
93
+ raw
94
+ };
95
+ }
96
+ const openAiResponsesInputTokensDetails = ts_utils_1.Validators.object({
97
+ cached_tokens: ts_utils_1.Validators.number.optional(),
98
+ cache_write_tokens: ts_utils_1.Validators.number.optional()
99
+ });
100
+ const openAiResponsesRawUsage = ts_utils_1.Validators.object({
101
+ input_tokens: ts_utils_1.Validators.number.optional(),
102
+ output_tokens: ts_utils_1.Validators.number.optional(),
103
+ input_tokens_details: openAiResponsesInputTokensDetails.optional()
104
+ });
105
+ /**
106
+ * Normalizes an OpenAI/xAI Responses API `usage` block. This route is shared
107
+ * by two providers that disagree about write reporting — OpenAI always sends
108
+ * `cache_write_tokens` (even when `0`), xAI never does — so `reports` is
109
+ * derived from field **presence**, not from provider identity: a wire that
110
+ * sends the field, however small its value, is `'reads-and-writes'`; a wire
111
+ * that omits it entirely is `'reads'`.
112
+ * @internal
113
+ */
114
+ function normalizeOpenAiResponsesUsage(raw) {
115
+ if (raw === undefined) {
116
+ return undefined;
117
+ }
118
+ const parsed = openAiResponsesRawUsage.validate(raw);
119
+ if (parsed.isFailure()) {
120
+ return undefined;
121
+ }
122
+ const { input_tokens: inputTokens, output_tokens: outputTokens, input_tokens_details } = parsed.value;
123
+ const cachedInputTokens = input_tokens_details === null || input_tokens_details === void 0 ? void 0 : input_tokens_details.cached_tokens;
124
+ const cacheWriteTokens = input_tokens_details === null || input_tokens_details === void 0 ? void 0 : input_tokens_details.cache_write_tokens;
125
+ const reports = cacheWriteTokens !== undefined ? 'reads-and-writes' : 'reads';
126
+ const uncachedInputTokens = cachedInputTokens !== undefined && inputTokens !== undefined
127
+ ? inputTokens - cachedInputTokens
128
+ : undefined;
129
+ return {
130
+ reports,
131
+ uncachedInputTokens,
132
+ cachedInputTokens,
133
+ cacheWriteTokens,
134
+ outputTokens,
135
+ totalInputTokens: uncachedInputTokens !== undefined && cachedInputTokens !== undefined
136
+ ? uncachedInputTokens + cachedInputTokens
137
+ : undefined,
138
+ raw
139
+ };
140
+ }
141
+ const geminiRawUsage = ts_utils_1.Validators.object({
142
+ promptTokenCount: ts_utils_1.Validators.number.optional(),
143
+ candidatesTokenCount: ts_utils_1.Validators.number.optional(),
144
+ cachedContentTokenCount: ts_utils_1.Validators.number.optional()
145
+ });
146
+ /**
147
+ * Normalizes a Gemini `usageMetadata` block. `reports` is unconditionally
148
+ * `'reads'` — Gemini has no cache-write concept in a `generateContent` call;
149
+ * writes happen out-of-band via the explicit `cachedContents` resource,
150
+ * out of scope per the design (§10).
151
+ *
152
+ * @remarks
153
+ * The Gemini-specific footgun: `promptTokenCount` already **includes**
154
+ * `cachedContentTokenCount` (it is the total effective prompt size), unlike
155
+ * OpenAI where the cached figure sits in a sub-object alongside a separate
156
+ * total. `uncachedInputTokens` subtracts to correct for it — but only when
157
+ * `cachedContentTokenCount` is itself present, per the module's absent-means-
158
+ * absent rule.
159
+ * @internal
160
+ */
161
+ function normalizeGeminiUsage(raw) {
162
+ if (raw === undefined) {
163
+ return undefined;
164
+ }
165
+ const parsed = geminiRawUsage.validate(raw);
166
+ if (parsed.isFailure()) {
167
+ return undefined;
168
+ }
169
+ const { promptTokenCount, candidatesTokenCount: outputTokens, cachedContentTokenCount: cachedInputTokens } = parsed.value;
170
+ const uncachedInputTokens = cachedInputTokens !== undefined && promptTokenCount !== undefined
171
+ ? promptTokenCount - cachedInputTokens
172
+ : undefined;
173
+ return {
174
+ reports: 'reads',
175
+ uncachedInputTokens,
176
+ cachedInputTokens,
177
+ outputTokens,
178
+ totalInputTokens: uncachedInputTokens !== undefined && cachedInputTokens !== undefined
179
+ ? uncachedInputTokens + cachedInputTokens
180
+ : undefined,
181
+ raw
182
+ };
183
+ }
184
+ //# sourceMappingURL=usageNormalization.js.map