@x12i/ai-gateway 7.9.1

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 (179) hide show
  1. package/README.md +4259 -0
  2. package/config.defaults.json +31 -0
  3. package/dist/activity-manager.d.ts +206 -0
  4. package/dist/activity-manager.js +1051 -0
  5. package/dist/config/activity-tracking-config.d.ts +11 -0
  6. package/dist/config/activity-tracking-config.js +15 -0
  7. package/dist/config.defaults.json +31 -0
  8. package/dist/content-normalizer/content-normalizer.d.ts +46 -0
  9. package/dist/content-normalizer/content-normalizer.js +393 -0
  10. package/dist/content-normalizer/index.d.ts +7 -0
  11. package/dist/content-normalizer/index.js +6 -0
  12. package/dist/content-normalizer/types.d.ts +33 -0
  13. package/dist/content-normalizer/types.js +4 -0
  14. package/dist/defaults/instructions-blocks.json +61 -0
  15. package/dist/defaults/model-config.json +16 -0
  16. package/dist/defaults/template-rendering.json +6 -0
  17. package/dist/flex-md-loader.d.ts +109 -0
  18. package/dist/flex-md-loader.js +940 -0
  19. package/dist/gateway-config.d.ts +49 -0
  20. package/dist/gateway-config.js +292 -0
  21. package/dist/gateway-conversion.d.ts +29 -0
  22. package/dist/gateway-conversion.js +174 -0
  23. package/dist/gateway-instructions.d.ts +30 -0
  24. package/dist/gateway-instructions.js +62 -0
  25. package/dist/gateway-memory.d.ts +51 -0
  26. package/dist/gateway-memory.js +207 -0
  27. package/dist/gateway-messages.d.ts +23 -0
  28. package/dist/gateway-messages.js +83 -0
  29. package/dist/gateway-meta.d.ts +25 -0
  30. package/dist/gateway-meta.js +87 -0
  31. package/dist/gateway-provider-auto-register.d.ts +17 -0
  32. package/dist/gateway-provider-auto-register.js +159 -0
  33. package/dist/gateway-provider.d.ts +54 -0
  34. package/dist/gateway-provider.js +202 -0
  35. package/dist/gateway-rate-limiter-constants.d.ts +16 -0
  36. package/dist/gateway-rate-limiter-constants.js +16 -0
  37. package/dist/gateway-rate-limiter.d.ts +56 -0
  38. package/dist/gateway-rate-limiter.js +107 -0
  39. package/dist/gateway-retry.d.ts +49 -0
  40. package/dist/gateway-retry.js +204 -0
  41. package/dist/gateway-utils.d.ts +21 -0
  42. package/dist/gateway-utils.js +181 -0
  43. package/dist/gateway-validation.d.ts +13 -0
  44. package/dist/gateway-validation.js +50 -0
  45. package/dist/gateway.d.ts +39 -0
  46. package/dist/gateway.js +430 -0
  47. package/dist/index.d.ts +36 -0
  48. package/dist/index.js +55 -0
  49. package/dist/instruction-errors.d.ts +16 -0
  50. package/dist/instruction-errors.js +29 -0
  51. package/dist/instruction-optimizer.d.ts +113 -0
  52. package/dist/instruction-optimizer.js +293 -0
  53. package/dist/instructions-parser.d.ts +31 -0
  54. package/dist/instructions-parser.js +56 -0
  55. package/dist/logger-factory.d.ts +17 -0
  56. package/dist/logger-factory.js +42 -0
  57. package/dist/message-builder.d.ts +41 -0
  58. package/dist/message-builder.js +522 -0
  59. package/dist/object-types-library-integration.d.ts +22 -0
  60. package/dist/object-types-library-integration.js +27 -0
  61. package/dist/object-types-library.d.ts +351 -0
  62. package/dist/object-types-library.js +210 -0
  63. package/dist/output-auditor.d.ts +44 -0
  64. package/dist/output-auditor.js +49 -0
  65. package/dist/request-report-generator.d.ts +60 -0
  66. package/dist/request-report-generator.js +169 -0
  67. package/dist/response-analyzer/format-type-detector.d.ts +35 -0
  68. package/dist/response-analyzer/format-type-detector.js +115 -0
  69. package/dist/response-analyzer/index.d.ts +9 -0
  70. package/dist/response-analyzer/index.js +8 -0
  71. package/dist/response-analyzer/object-type-detector.d.ts +42 -0
  72. package/dist/response-analyzer/object-type-detector.js +95 -0
  73. package/dist/response-analyzer/response-analyzer.d.ts +38 -0
  74. package/dist/response-analyzer/response-analyzer.js +97 -0
  75. package/dist/response-analyzer/types.d.ts +97 -0
  76. package/dist/response-analyzer/types.js +4 -0
  77. package/dist/response-fallback-fixer.d.ts +11 -0
  78. package/dist/response-fallback-fixer.js +123 -0
  79. package/dist/runtime-objects.d.ts +52 -0
  80. package/dist/runtime-objects.js +46 -0
  81. package/dist/template-parser.d.ts +58 -0
  82. package/dist/template-parser.js +99 -0
  83. package/dist/template-render-merge.d.ts +9 -0
  84. package/dist/template-render-merge.js +40 -0
  85. package/dist/troubleshooting-helper.d.ts +123 -0
  86. package/dist/troubleshooting-helper.js +596 -0
  87. package/dist/types.d.ts +1173 -0
  88. package/dist/types.js +6 -0
  89. package/dist/usage-tracker.d.ts +78 -0
  90. package/dist/usage-tracker.js +79 -0
  91. package/dist-cjs/activity-manager.cjs +1056 -0
  92. package/dist-cjs/activity-manager.d.ts +206 -0
  93. package/dist-cjs/config/activity-tracking-config.cjs +18 -0
  94. package/dist-cjs/config/activity-tracking-config.d.ts +11 -0
  95. package/dist-cjs/config.defaults.json +31 -0
  96. package/dist-cjs/content-normalizer/content-normalizer.cjs +398 -0
  97. package/dist-cjs/content-normalizer/content-normalizer.d.ts +46 -0
  98. package/dist-cjs/content-normalizer/index.cjs +12 -0
  99. package/dist-cjs/content-normalizer/index.d.ts +7 -0
  100. package/dist-cjs/content-normalizer/types.cjs +5 -0
  101. package/dist-cjs/content-normalizer/types.d.ts +33 -0
  102. package/dist-cjs/defaults/instructions-blocks.json +61 -0
  103. package/dist-cjs/defaults/model-config.json +16 -0
  104. package/dist-cjs/defaults/template-rendering.json +6 -0
  105. package/dist-cjs/flex-md-loader.cjs +986 -0
  106. package/dist-cjs/flex-md-loader.d.ts +109 -0
  107. package/dist-cjs/gateway-config.cjs +331 -0
  108. package/dist-cjs/gateway-config.d.ts +49 -0
  109. package/dist-cjs/gateway-conversion.cjs +212 -0
  110. package/dist-cjs/gateway-conversion.d.ts +29 -0
  111. package/dist-cjs/gateway-instructions.cjs +67 -0
  112. package/dist-cjs/gateway-instructions.d.ts +30 -0
  113. package/dist-cjs/gateway-memory.cjs +211 -0
  114. package/dist-cjs/gateway-memory.d.ts +51 -0
  115. package/dist-cjs/gateway-messages.cjs +86 -0
  116. package/dist-cjs/gateway-messages.d.ts +23 -0
  117. package/dist-cjs/gateway-meta.cjs +90 -0
  118. package/dist-cjs/gateway-meta.d.ts +25 -0
  119. package/dist-cjs/gateway-provider-auto-register.cjs +195 -0
  120. package/dist-cjs/gateway-provider-auto-register.d.ts +17 -0
  121. package/dist-cjs/gateway-provider.cjs +214 -0
  122. package/dist-cjs/gateway-provider.d.ts +54 -0
  123. package/dist-cjs/gateway-rate-limiter-constants.cjs +19 -0
  124. package/dist-cjs/gateway-rate-limiter-constants.d.ts +16 -0
  125. package/dist-cjs/gateway-rate-limiter.cjs +111 -0
  126. package/dist-cjs/gateway-rate-limiter.d.ts +56 -0
  127. package/dist-cjs/gateway-retry.cjs +212 -0
  128. package/dist-cjs/gateway-retry.d.ts +49 -0
  129. package/dist-cjs/gateway-utils.cjs +219 -0
  130. package/dist-cjs/gateway-utils.d.ts +21 -0
  131. package/dist-cjs/gateway-validation.cjs +54 -0
  132. package/dist-cjs/gateway-validation.d.ts +13 -0
  133. package/dist-cjs/gateway.cjs +434 -0
  134. package/dist-cjs/gateway.d.ts +39 -0
  135. package/dist-cjs/index.cjs +108 -0
  136. package/dist-cjs/index.d.ts +36 -0
  137. package/dist-cjs/instruction-errors.cjs +34 -0
  138. package/dist-cjs/instruction-errors.d.ts +16 -0
  139. package/dist-cjs/instruction-optimizer.cjs +299 -0
  140. package/dist-cjs/instruction-optimizer.d.ts +113 -0
  141. package/dist-cjs/instructions-parser.cjs +61 -0
  142. package/dist-cjs/instructions-parser.d.ts +31 -0
  143. package/dist-cjs/logger-factory.cjs +45 -0
  144. package/dist-cjs/logger-factory.d.ts +17 -0
  145. package/dist-cjs/message-builder.cjs +558 -0
  146. package/dist-cjs/message-builder.d.ts +41 -0
  147. package/dist-cjs/object-types-library-integration.cjs +32 -0
  148. package/dist-cjs/object-types-library-integration.d.ts +22 -0
  149. package/dist-cjs/object-types-library.cjs +215 -0
  150. package/dist-cjs/object-types-library.d.ts +351 -0
  151. package/dist-cjs/output-auditor.cjs +52 -0
  152. package/dist-cjs/output-auditor.d.ts +44 -0
  153. package/dist-cjs/request-report-generator.cjs +172 -0
  154. package/dist-cjs/request-report-generator.d.ts +60 -0
  155. package/dist-cjs/response-analyzer/format-type-detector.cjs +119 -0
  156. package/dist-cjs/response-analyzer/format-type-detector.d.ts +35 -0
  157. package/dist-cjs/response-analyzer/index.cjs +14 -0
  158. package/dist-cjs/response-analyzer/index.d.ts +9 -0
  159. package/dist-cjs/response-analyzer/object-type-detector.cjs +99 -0
  160. package/dist-cjs/response-analyzer/object-type-detector.d.ts +42 -0
  161. package/dist-cjs/response-analyzer/response-analyzer.cjs +101 -0
  162. package/dist-cjs/response-analyzer/response-analyzer.d.ts +38 -0
  163. package/dist-cjs/response-analyzer/types.cjs +5 -0
  164. package/dist-cjs/response-analyzer/types.d.ts +97 -0
  165. package/dist-cjs/response-fallback-fixer.cjs +126 -0
  166. package/dist-cjs/response-fallback-fixer.d.ts +11 -0
  167. package/dist-cjs/runtime-objects.cjs +52 -0
  168. package/dist-cjs/runtime-objects.d.ts +52 -0
  169. package/dist-cjs/template-parser.cjs +136 -0
  170. package/dist-cjs/template-parser.d.ts +58 -0
  171. package/dist-cjs/template-render-merge.cjs +43 -0
  172. package/dist-cjs/template-render-merge.d.ts +9 -0
  173. package/dist-cjs/troubleshooting-helper.cjs +611 -0
  174. package/dist-cjs/troubleshooting-helper.d.ts +123 -0
  175. package/dist-cjs/types.cjs +7 -0
  176. package/dist-cjs/types.d.ts +1173 -0
  177. package/dist-cjs/usage-tracker.cjs +83 -0
  178. package/dist-cjs/usage-tracker.d.ts +78 -0
  179. package/package.json +91 -0
@@ -0,0 +1,1173 @@
1
+ /**
2
+ * Type definitions for AI Gateway
3
+ *
4
+ * Extends router types with gateway-specific enhancements
5
+ */
6
+ import { AIResponse, LLMProviderRouter, type RouterConfig } from '@x12i/ai-providers-router';
7
+ type AIProvider = string;
8
+ type AIModel = string;
9
+ export type UsageTier = string;
10
+ import type { Activix } from '@x12i/activix';
11
+ import type { TemplateRenderOptions } from '@x12i/rendrix';
12
+ import type { Logxer } from '@x12i/logxer';
13
+ /**
14
+ * Identity object used for activity linkage.
15
+ * On gateway requests/responses it lives on `identity`. When activity tracking persists via Activix v5+,
16
+ * the same envelope is written under the BSON field `runContext` (see `@x12i/activix` changelog).
17
+ */
18
+ export type ActivityIdentity = {
19
+ /**
20
+ * Run/session correlation id for Activix (one logical session or job-scoped flow).
21
+ * Distinct from `aiRequestId` when multiple gateway invocations share one run.
22
+ */
23
+ sessionId: string;
24
+ /**
25
+ * Required runtime identity descriptor for the execution instance.
26
+ * `instanceId` and `type` are always set (with gateway fallbacks); additional keys are preserved for Activix.
27
+ */
28
+ instance: {
29
+ instanceId: string;
30
+ type: string;
31
+ [key: string]: unknown;
32
+ };
33
+ /** Gateway-level request correlation ID. */
34
+ aiRequestId: string;
35
+ /** @deprecated Use aiRequestId. Kept only for backward compatibility. */
36
+ jobId?: string;
37
+ jobTypeId?: string;
38
+ agentId: string;
39
+ taskId?: string;
40
+ taskTypeId?: string;
41
+ /** Graph/node linkage (optional, used by activity identity grouping). */
42
+ graphId?: string;
43
+ nodeId?: string;
44
+ /** Alternative identifiers (optional, forwarded for compatibility with upstream). */
45
+ coreSkillId?: string;
46
+ masterSkillId?: string;
47
+ masterSkillActivityId?: string;
48
+ };
49
+ /** Re-export parser template options for gateway consumers (MUST/optional protocol, subPathSearch, etc.). */
50
+ export type { TemplateRenderOptions } from '@x12i/rendrix';
51
+ type LLMRequest = Parameters<LLMProviderRouter['invoke']>[0];
52
+ type LLMResponse = Awaited<ReturnType<LLMProviderRouter['invoke']>>;
53
+ /**
54
+ * Gateway configuration extending router configuration
55
+ *
56
+ * RouterConfig already includes:
57
+ * - defaultProvider?: LLMProvider (from llm-provider-interface)
58
+ * - fallbackChain?: LLMProvider[]
59
+ * - autoDiscover?: boolean
60
+ * - usageTracker?: UsageTracker
61
+ *
62
+ * We don't need to redeclare these - they're inherited from RouterConfig
63
+ */
64
+ /**
65
+ * Provider + model reference used for defaults and fallbacks
66
+ */
67
+ export interface ProviderModelRef {
68
+ engine: AIProvider;
69
+ model: AIModel;
70
+ }
71
+ /**
72
+ * Retry configuration for network and server errors
73
+ */
74
+ export interface RetryConfig {
75
+ /**
76
+ * Maximum number of retry attempts
77
+ * @default 3
78
+ */
79
+ maxRetries?: number;
80
+ /**
81
+ * Initial delay in milliseconds before first retry
82
+ * @default 1000
83
+ */
84
+ initialDelay?: number;
85
+ /**
86
+ * Maximum delay cap in milliseconds
87
+ * @default 30000
88
+ */
89
+ maxDelay?: number;
90
+ /**
91
+ * Enable exponential backoff multiplier
92
+ * @default 2
93
+ */
94
+ backoffMultiplier?: number;
95
+ /**
96
+ * Enable jitter (randomization) to prevent thundering herd
97
+ * @default true
98
+ */
99
+ enableJitter?: boolean;
100
+ /**
101
+ * Additional delay for 429 (throttling) errors in milliseconds
102
+ * @default 5000
103
+ */
104
+ throttlingDelay?: number;
105
+ }
106
+ /**
107
+ * Model configuration for request execution.
108
+ *
109
+ * Controls which model is used and how it generates responses.
110
+ * This configuration is passed through to the gateway for model selection and parameter control.
111
+ *
112
+ * Priority: modelConfig overrides request.config, which overrides gateway defaults.
113
+ */
114
+ export interface ModelConfig {
115
+ /**
116
+ * Model identifier (e.g., "gpt-4-turbo", "claude-3-opus", "gpt-3.5-turbo")
117
+ * If not specified, uses gateway/router default
118
+ */
119
+ model?: string;
120
+ /**
121
+ * Model ID (alternative to model name, for provider-specific model IDs)
122
+ * If both model and modelId are provided, model takes precedence
123
+ */
124
+ modelId?: string;
125
+ /**
126
+ * Provider name (e.g., "openai", "anthropic")
127
+ * If not specified, uses client default provider
128
+ */
129
+ provider?: string;
130
+ /**
131
+ * Temperature for generation (0.0 to 2.0)
132
+ * Controls randomness in responses
133
+ */
134
+ temperature?: number;
135
+ /**
136
+ * Maximum tokens to generate
137
+ */
138
+ maxTokens?: number;
139
+ /**
140
+ * Top-p (nucleus) sampling parameter (0.0 to 1.0)
141
+ */
142
+ topP?: number;
143
+ /**
144
+ * Frequency penalty (-2.0 to 2.0)
145
+ */
146
+ frequencyPenalty?: number;
147
+ /**
148
+ * Presence penalty (-2.0 to 2.0)
149
+ */
150
+ presencePenalty?: number;
151
+ /**
152
+ * Stop sequences (array of strings)
153
+ * Generation stops when any stop sequence is encountered
154
+ */
155
+ stop?: string[];
156
+ /**
157
+ * Additional provider-specific parameters
158
+ */
159
+ [key: string]: any;
160
+ }
161
+ export interface GatewayConfig extends Omit<RouterConfig, 'defaultEngine' | 'logger'> {
162
+ /**
163
+ * Default target (engine + model) for routing
164
+ */
165
+ defaultTarget?: ProviderModelRef;
166
+ /**
167
+ * Fallback chain for routing (engine + model pairs)
168
+ */
169
+ fallbackChain?: ProviderModelRef[];
170
+ /**
171
+ * Usage tier for rate limiting (RPM/TPM)
172
+ * @default 'tier-3'
173
+ */
174
+ usageTier?: UsageTier;
175
+ /**
176
+ * Enable activity tracking
177
+ * @default true
178
+ */
179
+ enableActivityTracking?: boolean;
180
+ /**
181
+ * Enable usage tier tracking
182
+ * @default true
183
+ */
184
+ enableUsageTracking?: boolean;
185
+ /**
186
+ * Enable logging
187
+ * @default true
188
+ */
189
+ enableLogging?: boolean;
190
+ /**
191
+ * Custom logger instance (optional)
192
+ */
193
+ logger?: Logxer;
194
+ /**
195
+ * Custom activity tracker instance (optional)
196
+ */
197
+ activityTracker?: Activix;
198
+ /**
199
+ * Package name for logging
200
+ * @default 'AI_GATEWAY'
201
+ */
202
+ packageName?: string;
203
+ /**
204
+ * Default engine (e.g., 'openai', 'anthropic')
205
+ * Used only for provider routing when request doesn't specify provider
206
+ */
207
+ defaultEngine?: string;
208
+ /**
209
+ * OpenRouter API key passed explicitly (avoids env-loading order when the gateway is created before .env is loaded).
210
+ * When set, the router receives this key at creation so OpenRouter can be registered even if process.env is not yet available.
211
+ */
212
+ openrouter?: {
213
+ apiKey: string;
214
+ };
215
+ /**
216
+ * Enable OpenRouter mode. When omitted, the gateway uses process.env.USE_OPENROUTER or enables when a key is set.
217
+ */
218
+ openRouter?: {
219
+ enabled?: boolean;
220
+ };
221
+ /**
222
+ * InstructionsBlocks overrides
223
+ * Key: block name, Value: block content
224
+ */
225
+ instructionsBlocks?: Record<string, string>;
226
+ /**
227
+ * Default temperature for LLM requests
228
+ */
229
+ temperature?: number;
230
+ /**
231
+ * Other LLM config options
232
+ */
233
+ maxTokens?: number;
234
+ topP?: number;
235
+ frequencyPenalty?: number;
236
+ presencePenalty?: number;
237
+ /**
238
+ * Retry configuration for network and server errors
239
+ */
240
+ retry?: RetryConfig;
241
+ /**
242
+ * Rate limiting configuration
243
+ * Smart rate limiting that tracks when the last API call was made
244
+ * and only waits if necessary to maintain minimum intervals between calls.
245
+ * Applied automatically to all provider calls via router interceptors.
246
+ */
247
+ rateLimit?: {
248
+ /**
249
+ * Enable rate limiting
250
+ * @default true
251
+ */
252
+ enabled?: boolean;
253
+ /**
254
+ * Default minimum interval in milliseconds between API calls (used if provider-specific not set)
255
+ * @default 500
256
+ */
257
+ defaultMinIntervalMs?: number;
258
+ /**
259
+ * Per-provider minimum intervals in milliseconds
260
+ * Key: provider name (e.g., 'openai', 'grok')
261
+ * Value: minimum milliseconds between calls for that provider
262
+ *
263
+ * @example
264
+ * {
265
+ * openai: 500, // 500ms between OpenAI calls
266
+ * grok: 1000, // 1 second between Grok calls
267
+ * anthropic: 300 // 300ms between Anthropic calls
268
+ * }
269
+ */
270
+ providerIntervals?: Record<string, number>;
271
+ };
272
+ /**
273
+ * Default task configuration for template rendering
274
+ * @deprecated taskConfig is no longer used by Rendrix 3.0.0+
275
+ * Kept for backward compatibility only - will be ignored
276
+ */
277
+ defaultTaskConfig?: {
278
+ includeThoughts?: boolean;
279
+ };
280
+ /**
281
+ * Default options passed to @x12i/rendrix `render` (v4+).
282
+ * Merged with `src/defaults/template-rendering.json` (gateway `templateRendering` overrides packaged JSON defaults).
283
+ * Per-request `templateRenderOptions` on the request overrides this.
284
+ *
285
+ * **Sub-path search root priority:** when `subPathSearch.enabled` is true, the **`roots` array order is the priority**
286
+ * (first root wins; ISSUE-005). Omit `roots` to use the parser package defaults. Example:
287
+ * `subPathSearch: { enabled: true, roots: ['execution', 'input', 'inputs'] }`.
288
+ */
289
+ templateRendering?: TemplateRenderOptions;
290
+ /**
291
+ * Memory manager instance for unified memory management
292
+ * When provided, enables fetching and merging memories from persistent storage
293
+ * Type: @x12i/ai-memory MemoryManager (placeholder until component is ready)
294
+ */
295
+ memoryManager?: unknown;
296
+ /**
297
+ * Memory resolution configuration
298
+ * Controls which memory types to fetch and how to resolve them
299
+ */
300
+ memoryResolution?: {
301
+ /**
302
+ * Whether to fetch workingMemory from the component
303
+ * @default true
304
+ */
305
+ fetchWorkingMemory?: boolean;
306
+ /**
307
+ * Whether to fetch shortTermMemory from the component
308
+ * @default true
309
+ */
310
+ fetchShortTermMemory?: boolean;
311
+ /**
312
+ * Whether to fetch experienceMemory from the component
313
+ * @default true
314
+ */
315
+ fetchExperienceMemory?: boolean;
316
+ /**
317
+ * Whether to fetch knowledgeMemory from the component
318
+ * @default true
319
+ */
320
+ fetchKnowledgeMemory?: boolean;
321
+ /**
322
+ * Whether to update existing memories in storage with merged results
323
+ * When true, the final merged memory (from input + fetched) will be saved back to storage
324
+ * @default false
325
+ */
326
+ updateExistingMemories?: boolean;
327
+ /**
328
+ * Scope of memory updates - which memory types can be updated
329
+ * Only applies when updateExistingMemories is true
330
+ * @default ['shortTermMemory', 'experienceMemory', 'knowledgeMemory']
331
+ */
332
+ updateScope?: Array<'workingMemory' | 'shortTermMemory' | 'experienceMemory' | 'knowledgeMemory'>;
333
+ /**
334
+ * Enrichment configuration for experience and knowledge memories
335
+ * @default { experience: 'none', knowledge: 'none' }
336
+ */
337
+ enrichment?: {
338
+ experience?: 'none' | 'force' | 'ttl' | 'lacking';
339
+ knowledge?: 'none' | 'force' | 'ttl' | 'lacking';
340
+ };
341
+ /**
342
+ * Strategy for memory resolution
343
+ * @default 'fetch-on-demand'
344
+ */
345
+ resolutionStrategy?: 'cache-only' | 'fetch-on-demand' | 'enrich';
346
+ };
347
+ /**
348
+ * Object types library cache TTL in milliseconds
349
+ * @default 3600000 (1 hour)
350
+ */
351
+ objectTypesCacheTTL?: number;
352
+ /**
353
+ * Internal system actions configuration
354
+ * Configures models and engines for internal AI operations (two-step conversion, instruction optimization, etc.)
355
+ * These are separate from user requests and use dedicated models/engines
356
+ */
357
+ internalSystemActions?: {
358
+ /**
359
+ * Model and engine for two-step conversion (second step - converting structured text to JSON)
360
+ * @default { engine: 'openai', model: 'gpt-5-nano' }
361
+ */
362
+ twoStepConversion?: {
363
+ engine?: string;
364
+ model: string;
365
+ temperature?: number;
366
+ maxTokens?: number;
367
+ };
368
+ /**
369
+ * Model and engine for instruction optimization
370
+ * @default { engine: 'openai', model: 'gpt-5-nano' }
371
+ */
372
+ instructionOptimization?: {
373
+ engine?: string;
374
+ model: string;
375
+ temperature?: number;
376
+ maxTokens?: number;
377
+ };
378
+ /**
379
+ * Model and engine for instruction auditing/testing
380
+ * @default { engine: 'openai', model: 'gpt-5-nano' }
381
+ */
382
+ instructionAudit?: {
383
+ engine?: string;
384
+ model: string;
385
+ temperature?: number;
386
+ maxTokens?: number;
387
+ };
388
+ /**
389
+ * Model and engine for default skill execution
390
+ * @default { engine: 'openai', model: 'gpt-5-nano' }
391
+ */
392
+ internalSkill?: {
393
+ engine?: string;
394
+ model: string;
395
+ temperature?: number;
396
+ maxTokens?: number;
397
+ };
398
+ /**
399
+ * Model and engine for default skill audit
400
+ * @default { engine: 'openai', model: 'gpt-5-nano' }
401
+ */
402
+ skillAudit?: {
403
+ engine?: string;
404
+ model: string;
405
+ temperature?: number;
406
+ maxTokens?: number;
407
+ };
408
+ };
409
+ }
410
+ /**
411
+ * Internal base request type (not exported)
412
+ * Contains all common properties shared by ChatRequest and AIRequest
413
+ * Extends LLMRequest but omits 'messages' and 'input' to allow custom message handling
414
+ * and make input optional (router may require it, but gateway allows it to be optional)
415
+ */
416
+ interface BaseLLMRequest extends Omit<LLMRequest, 'messages' | 'input'> {
417
+ /**
418
+ * AI request ID - Required for all invocations and activity tracking
419
+ */
420
+ aiRequestId: string;
421
+ /** @deprecated Use aiRequestId. Kept only for backward compatibility. */
422
+ jobId?: string;
423
+ /**
424
+ * Optional top-level run/session id for Activix. Prefer `identity.sessionId`; if both are set they must match.
425
+ * When omitted, `ActivityManager` resolves session from `identity.sessionId`, then `jobId`, then `aiRequestId`.
426
+ */
427
+ sessionId?: string;
428
+ /**
429
+ * Optional identity object used for upstream linkage (forwarded through the request).
430
+ * May be partial; may include product-specific keys beyond `ActivityIdentity`. When omitted,
431
+ * the gateway normalizes and attaches a full envelope (`ensureGatewayRequestIdentity` / activity tracking).
432
+ */
433
+ identity?: ActivityIdentity & Record<string, unknown>;
434
+ /**
435
+ * Job type ID (optional) - Short ID or hash to identify recurring job types
436
+ * Best practice: Use MD5 hash of the job type string for consistent job identification
437
+ * Example: MD5("data-processing-job") = "abc123..." for all jobs of the same type
438
+ * Works like taskTypeId but for job-level aggregation
439
+ */
440
+ jobTypeId?: string;
441
+ /**
442
+ * Agent ID - Required to identify the agent
443
+ */
444
+ agentId: string;
445
+ /**
446
+ * Agent type - Identifies the type of agent making the request
447
+ * - 'chat': Simple chat agent (uses invokeChat)
448
+ * - 'ai-reasoner': AI reasoning agent (uses invoke with objectTypes)
449
+ * - 'ai-auditor': AI auditing agent
450
+ * - 'ai-scoper': AI scoping agent
451
+ * - 'ai-discovery': AI discovery agent
452
+ * - 'ai-planner': AI planning agent
453
+ * - 'ai-executor': AI execution agent
454
+ * @default 'chat' for ChatRequest, 'ai-reasoner' for AIRequest
455
+ */
456
+ agentType?: 'chat' | 'ai-reasoner' | 'ai-auditor' | 'ai-scoper' | 'ai-discovery' | 'ai-planner' | 'ai-executor';
457
+ /**
458
+ * Instructions - Required template text (parsed with workingMemory / tier memories via Rendrix).
459
+ */
460
+ instructions: string;
461
+ /**
462
+ * Task ID (optional)
463
+ */
464
+ taskId?: string;
465
+ /**
466
+ * Task type ID (optional) - Short ID or hash to identify recurring tasks
467
+ * If not provided, will be auto-generated as MD5 hash of the pre-parsed instructions
468
+ * Best practice: Use MD5 hash of the question/instruction text for consistent task identification
469
+ * Example: MD5("What is the capital of France?") = "abc123..." for all requests with same question
470
+ */
471
+ taskTypeId?: string;
472
+ /**
473
+ * Graph ID (optional) - Identifier for graph execution context
474
+ * Used when executing nodes within a graph to track which graph contains the node
475
+ * Example: 'graph-123' or 'workflow-data-processing'
476
+ * When provided, enables smarter activity identity and grouping by graph
477
+ */
478
+ graphId?: string;
479
+ /**
480
+ * Node ID (optional) - Identifier for the specific node being executed
481
+ * Used when executing nodes within a graph to track which node is being executed
482
+ * Example: 'node-456' or 'extract-emails'
483
+ * When provided along with graphId, enables precise activity identity and tracking
484
+ * Mapped to identity.nodeId in activity tracking
485
+ */
486
+ nodeId?: string;
487
+ /**
488
+ * Core Skill ID (optional) - Alternative node identifier for graph execution
489
+ * Used when executing nodes within a graph (alternative to nodeId or skillId)
490
+ * Example: 'q0' or 'extract-emails-node'
491
+ * Mapped to identity.nodeId in activity tracking (alternative to skillId/nodeId)
492
+ */
493
+ coreSkillId?: string;
494
+ /**
495
+ * Prompt template text (optional) — parsed with workingMemory and tier memories.
496
+ * Use variables such as {{input}} resolved from workingMemory.input.
497
+ */
498
+ prompt?: string;
499
+ /**
500
+ * Context text (optional) - Template that can use workingMemory
501
+ * Added as system message between instructions and prompt
502
+ */
503
+ context?: string;
504
+ /**
505
+ * Task configuration (optional) - Control template behavior and conditional rendering
506
+ * @deprecated taskConfig is no longer used by Rendrix 3.0.0+
507
+ * Kept for backward compatibility only - will be ignored
508
+ */
509
+ taskConfig?: {
510
+ includeThoughts?: boolean;
511
+ };
512
+ /**
513
+ * Working memory (optional) - Affects all template parsing
514
+ * Passed to Rendrix (v4+) for template variable substitution.
515
+ * Plain {{path}} tokens are MUST paths: undefined after merge throws TemplateResolutionError (rethrown by the gateway).
516
+ */
517
+ workingMemory?: unknown;
518
+ /**
519
+ * Per-request overrides for Rendrix `TemplateRenderOptions` (v4+).
520
+ * Merged on top of gateway `templateRendering` and packaged defaults.
521
+ * For `subPathSearch.roots`, array order is search priority (first match wins); a request can override
522
+ * the full list or only set `enabled` and inherit `roots` from the gateway default merge.
523
+ */
524
+ templateRenderOptions?: TemplateRenderOptions;
525
+ /**
526
+ * Template tokens (optional) - Tier 1 (highest priority) token values
527
+ * These tokens are passed directly as arguments and override all other memory sources
528
+ * Common tokens: taskDescription, input (from workingMemory.input), role
529
+ * Example: { taskDescription: "Extract emails", input: "user@example.com", role: "user" }
530
+ * Note: The `input` field has been removed. Use workingMemory.input instead.
531
+ */
532
+ templateTokens?: {
533
+ [key: string]: string | number | boolean | object | object[];
534
+ };
535
+ /**
536
+ * Messages array - Optional, can be used instead of instructions/prompt
537
+ * If provided, will be appended as-is after built messages; instructions template text is still parsed for the system message when present
538
+ */
539
+ messages?: any;
540
+ /**
541
+ * Config - Can override gateway defaults
542
+ *
543
+ * For reasoning-capable models (OpenRouter unified reasoning API), can include:
544
+ * ```typescript
545
+ * reasoning?: {
546
+ * effort?: 'none' | 'low' | 'medium' | 'high' | 'xhigh'; // xhigh normalized to high
547
+ * visibility?: 'none' | 'summary' | 'trace'; // What reasoning info to return
548
+ * onUnsupported?: 'error' | 'downgrade' | 'ignore'; // Error handling
549
+ * }
550
+ * ```
551
+ */
552
+ config?: any;
553
+ /**
554
+ * Model configuration for this request execution.
555
+ *
556
+ * Overrides default model settings from gateway initialization.
557
+ * Merged with request.config (modelConfig takes precedence).
558
+ *
559
+ * Use this field for structured model configuration from upstream clients (e.g., @woroces/ai-tasks).
560
+ * The gateway will merge modelConfig into request.config before passing to the router.
561
+ *
562
+ * @example
563
+ * ```typescript
564
+ * {
565
+ * modelConfig: {
566
+ * model: "gpt-4-turbo",
567
+ * provider: "openai",
568
+ * temperature: 0.7,
569
+ * maxTokens: 2000
570
+ * }
571
+ * }
572
+ * ```
573
+ */
574
+ modelConfig?: ModelConfig;
575
+ /**
576
+ * Inference type for outputs library parsing (optional)
577
+ * When provided, response will be parsed into typed inference output
578
+ * Can be a single type (string) or multiple types (array)
579
+ */
580
+ inferenceType?: string | string[];
581
+ /**
582
+ * Enable output schema validation (optional)
583
+ * When true, validates parsed output against schema from instruction metadata or outputs library
584
+ * @default false
585
+ */
586
+ validateOutputSchema?: boolean;
587
+ /**
588
+ * Enable strict validation mode (optional)
589
+ * When true, validation errors will throw exceptions instead of warnings
590
+ * @default false
591
+ */
592
+ strictValidation?: boolean;
593
+ /**
594
+ * Response transformation hooks (optional)
595
+ * Allows transforming responses at different stages of processing
596
+ */
597
+ transformations?: ResponseTransformationConfig;
598
+ /**
599
+ * Parse options for outputs library (optional)
600
+ * Used when inferenceType is provided for parsing inference outputs
601
+ */
602
+ parseOptions?: Record<string, unknown>;
603
+ }
604
+ /**
605
+ * Chat request for conversational use cases
606
+ *
607
+ * Mandatory:
608
+ * - aiRequestId: Required for activity tracking
609
+ * - agentId: Required to identify the agent
610
+ * - instructions: Required (template text)
611
+ *
612
+ * Minimum: instructions + prompt (prompt is required for user message)
613
+ *
614
+ * Note: objectTypes is NOT supported in ChatRequest.
615
+ * Use AIRequest with invoke() method for structured output requests.
616
+ */
617
+ export interface ChatRequest extends BaseLLMRequest {
618
+ /**
619
+ * Optional expected schema for contract output validation
620
+ * When provided, AI response will be parsed against this schema and stored in activity records
621
+ */
622
+ expectedSchema?: StructuredTextSpec;
623
+ }
624
+ /**
625
+ * Structured text format specification
626
+ * Free-form description of desired output structure
627
+ *
628
+ * NOTE: Text mode now uses flex-md format exclusively.
629
+ * The LLM should return valid flex-md that can be parsed to JSON.
630
+ */
631
+ export interface StructuredTextSpec {
632
+ /**
633
+ * Free-form description of the output structure
634
+ * Examples: "a story with character, setting, and conflict",
635
+ * "a detailed explanation with introduction, body, and conclusion"
636
+ *
637
+ * The LLM will format this as flex-md text.
638
+ */
639
+ description: string;
640
+ /**
641
+ * Optional format hints (flex-md specific hints)
642
+ * If not provided, LLM should use standard flex-md format
643
+ */
644
+ formatHint?: string;
645
+ }
646
+ /**
647
+ * AI request for structured output requests
648
+ * Used exclusively with invoke() method for requests requiring structured output
649
+ *
650
+ * Mandatory:
651
+ * - aiRequestId: Required for activity tracking
652
+ * - agentId: Required to identify the agent
653
+ * - instructions: Required (template text)
654
+ * - primaryObjectType OR flexMdFormat: Required for structured output
655
+ * * primaryObjectType: Standard/custom object type (requires outputs library)
656
+ * * flexMdFormat: Direct flex-md format specification (no outputs library needed)
657
+ */
658
+ export interface AIRequest extends BaseLLMRequest {
659
+ /**
660
+ * Encrypted reasoning items for conversation continuity
661
+ * Only include reasoning.encrypted items from previous responses
662
+ * Must be preserved and replayed verbatim in subsequent requests
663
+ */
664
+ reasoningEncrypted?: Array<{
665
+ id: string;
666
+ type: string;
667
+ format?: string;
668
+ index?: number;
669
+ data?: string;
670
+ summary?: any[];
671
+ }>;
672
+ /**
673
+ * Skill execution tracking fields
674
+ * Used when executing skills (instruction keys starting with 'skills/')
675
+ */
676
+ /**
677
+ * Master skill activity ID - The parent skill activity's ID (when a skill calls another skill)
678
+ * When a skill executes and calls another skill, the parent skill's activityId becomes
679
+ * the child skill's masterSkillActivityId. This allows tracking all sub-activities of a skill execution.
680
+ */
681
+ masterSkillActivityId?: string;
682
+ /**
683
+ * Skill ID - The skill identifier (e.g., 'skills/professional-answer')
684
+ * Auto-detected from instruction key if not provided. Used for skill execution tracking.
685
+ */
686
+ skillId?: string;
687
+ /**
688
+ * Master skill ID - The parent skill's identifier (when a skill calls another skill)
689
+ * When a skill executes and calls another skill, the parent skill's skillId becomes
690
+ * the child skill's masterSkillId. This allows tracking skill hierarchies.
691
+ */
692
+ masterSkillId?: string;
693
+ }
694
+ /**
695
+ * Enhanced response with comprehensive metadata and generic type support
696
+ *
697
+ * @template TContent - Type of parsedContent (defaults to unknown)
698
+ */
699
+ /**
700
+ * Failure types for activity tracking
701
+ *
702
+ * - pre-call-validation: Request validation failed before attempting LLM call (missing/invalid prefix, schema, etc.)
703
+ * - api-call-failure: API call failed (network error, API error, timeout) - no response from LLM
704
+ * - llm-response-failure: LLM responded but couldn't answer (bad input, bad instructions, bad context, missing context, missing input)
705
+ * - response-parsing-failure: LLM responded but response failed to parse (non-JSON, prose instead of JSON, invalid format)
706
+ */
707
+ export type FailureType = 'pre-call-validation' | 'api-call-failure' | 'llm-response-failure' | 'response-parsing-failure';
708
+ /**
709
+ * LLM response failure subtypes (when failureType is 'llm-response-failure')
710
+ */
711
+ export type LLMResponseFailureSubtype = 'bad-input' | 'bad-instructions' | 'bad-context' | 'missing-context' | 'missing-input';
712
+ /**
713
+ * Response parsing failure subtypes (when failureType is 'response-parsing-failure')
714
+ */
715
+ export type ResponseParsingFailureSubtype = 'invalid-flex-md' | 'flex-md-parse-failed' | 'invalid-json-format' | 'missing-parsed-content' | 'schema-validation-failed';
716
+ export interface EnhancedLLMResponse<TContent = unknown> extends Omit<AIResponse, 'content' | 'metadata' | 'reasoning'> {
717
+ /**
718
+ * Normalized content string (always present)
719
+ * - If original was string: returns the string
720
+ * - If original was object/array: returns JSON.stringify()
721
+ * - If original was null: returns empty string
722
+ */
723
+ content: string;
724
+ /**
725
+ * Original raw text from provider (before any parsing)
726
+ * Available if router preserved rawText
727
+ */
728
+ rawText?: string;
729
+ /**
730
+ * Parsed content (object or array) if content was JSON
731
+ * Typed based on generic TContent
732
+ */
733
+ parsedContent?: TContent;
734
+ /**
735
+ * Unified reasoning response from router (OpenRouter Reasoning API)
736
+ * Provides normalized reasoning across all providers
737
+ */
738
+ reasoning?: {
739
+ requested: {
740
+ effort?: 'none' | 'low' | 'medium' | 'high' | 'xhigh';
741
+ visibility?: 'none' | 'summary' | 'trace';
742
+ };
743
+ applied: {
744
+ effort?: 'none' | 'low' | 'medium' | 'high';
745
+ visibility?: 'none' | 'summary' | 'trace';
746
+ };
747
+ artifacts?: {
748
+ summary?: {
749
+ text: string;
750
+ format: string;
751
+ };
752
+ trace?: {
753
+ text: string;
754
+ format: string;
755
+ };
756
+ encrypted?: Array<{
757
+ id: string;
758
+ format: string;
759
+ type?: string;
760
+ [key: string]: any;
761
+ }>;
762
+ };
763
+ availability?: {
764
+ supportsEffort?: boolean;
765
+ supportsSummary?: boolean;
766
+ supportsTrace?: boolean;
767
+ supportsEncrypted?: boolean;
768
+ };
769
+ warnings?: Array<{
770
+ code: 'EFFORT_NORMALIZED' | 'VISIBILITY_DOWNGRADED' | 'EFFORT_IGNORED' | 'REASONING_UNSUPPORTED';
771
+ message: string;
772
+ }>;
773
+ };
774
+ /**
775
+ * Encrypted reasoning items for conversation continuity
776
+ * Must be preserved and replayed verbatim in subsequent requests
777
+ * @deprecated Use reasoning.artifacts.encrypted instead (unified reasoning API)
778
+ */
779
+ reasoningEncrypted?: Array<{
780
+ id: string;
781
+ type: string;
782
+ format?: string;
783
+ index?: number;
784
+ data?: string;
785
+ summary?: any[];
786
+ }>;
787
+ /**
788
+ * Readable reasoning summary (human-readable)
789
+ * Not replayed - only for display/audit purposes
790
+ * @deprecated Use reasoning.artifacts.summary instead (unified reasoning API)
791
+ */
792
+ reasoningSummary?: Array<{
793
+ id: string;
794
+ type: string;
795
+ format?: string;
796
+ index?: number;
797
+ content?: any;
798
+ }>;
799
+ /**
800
+ * Enhanced metadata including latency, tokens, model, and aiRequestId
801
+ * Merges with existing metadata from router response
802
+ */
803
+ metadata: LLMResponse['metadata'] & {
804
+ /**
805
+ * AI request ID that triggered this execution
806
+ */
807
+ aiRequestId?: string;
808
+ /**
809
+ * Execution latency in milliseconds
810
+ */
811
+ latencyMs: number;
812
+ /**
813
+ * Detailed token usage breakdown
814
+ */
815
+ tokens: {
816
+ prompt: number;
817
+ completion: number;
818
+ total: number;
819
+ };
820
+ /**
821
+ * Model ID used (e.g., 'gpt-4o', 'claude-sonnet-4')
822
+ */
823
+ model?: string;
824
+ /**
825
+ * Provider used (e.g., 'openai', 'anthropic')
826
+ */
827
+ provider?: string;
828
+ /**
829
+ * Cost in USD (if available)
830
+ */
831
+ cost?: number;
832
+ /**
833
+ * Content type classification
834
+ * Indicates whether content is 'string', 'object', 'array', or 'null'
835
+ */
836
+ contentType?: 'string' | 'object' | 'array' | 'null';
837
+ /**
838
+ * Gateway normalization data (optional - tracks content normalization process)
839
+ * Available when content normalization was performed
840
+ */
841
+ normalization?: {
842
+ /**
843
+ * Original raw text from provider (before any parsing)
844
+ */
845
+ rawText?: string;
846
+ /**
847
+ * Parsed content (object or array) if content was JSON
848
+ */
849
+ parsedContent?: any;
850
+ /**
851
+ * Content type classification from normalization
852
+ */
853
+ contentType: 'string' | 'object' | 'array' | 'null';
854
+ /**
855
+ * Normalized content string
856
+ */
857
+ normalizedContent: string;
858
+ };
859
+ /**
860
+ * Output format information (optional - extracted and validated from instructions)
861
+ * Available when output format is extracted from instruction templates
862
+ */
863
+ outputFormat?: {
864
+ /**
865
+ * The extracted/validated format specification
866
+ */
867
+ spec?: string;
868
+ /**
869
+ * Detected compliance level (L0, L1, L2, L3)
870
+ */
871
+ level?: 'L0' | 'L1' | 'L2' | 'L3';
872
+ /**
873
+ * Whether validation passed
874
+ */
875
+ validated?: boolean;
876
+ /**
877
+ * Validation errors if any
878
+ */
879
+ validationErrors?: string[];
880
+ };
881
+ /**
882
+ * Provider normalization metadata (optional - tracks provider-level normalization)
883
+ * Available when provider normalized the request (messages format, config parameters, etc.)
884
+ * This comes from router response metadata.normalization
885
+ */
886
+ providerNormalization?: {
887
+ /**
888
+ * Whether the request messages/format were normalized
889
+ * For example, converting gateway messages array to API-specific format
890
+ */
891
+ messagesNormalized?: boolean;
892
+ /**
893
+ * Whether configuration parameters were normalized
894
+ * This includes parameter name conversions, removals, or value adjustments
895
+ */
896
+ configNormalized?: boolean;
897
+ /**
898
+ * The normalized configuration that was actually sent to the API
899
+ * This allows consumers to see exactly what parameters were used
900
+ */
901
+ normalizedConfig?: Record<string, unknown>;
902
+ /**
903
+ * The normalized messages that were actually sent to the API
904
+ * This allows consumers to see the exact message format used
905
+ */
906
+ normalizedMessages?: any;
907
+ /**
908
+ * The normalized request that was actually sent to the API
909
+ * This allows consumers to see the exact request format used
910
+ */
911
+ normalizedRequest?: any;
912
+ /**
913
+ * Warnings about parameter modifications during normalization
914
+ * Examples:
915
+ * - "Omitted temperature (not supported by gpt-5-nano)"
916
+ * - "Converted maxTokens -> max_output_tokens"
917
+ */
918
+ warnings?: string[];
919
+ };
920
+ /**
921
+ * Parsed inference output (if inferenceType was provided in request)
922
+ * Typed based on the inference type
923
+ */
924
+ parsedOutput?: unknown;
925
+ /**
926
+ * Inference type used for parsing (if provided)
927
+ */
928
+ inferenceType?: string;
929
+ /**
930
+ * Validation errors from schema validation (if enabled)
931
+ */
932
+ outputValidationErrors?: string[];
933
+ /**
934
+ * Whether output validation passed (if validation was enabled)
935
+ */
936
+ outputValidationPassed?: boolean;
937
+ /**
938
+ * Schema used for validation (if validation was enabled)
939
+ */
940
+ outputSchema?: Record<string, unknown>;
941
+ /**
942
+ * Whether outputs library was available and used for parsing
943
+ */
944
+ outputsLibraryAvailable?: boolean;
945
+ /**
946
+ * Method used for parsing (outputs-library, json-parse, or raw)
947
+ */
948
+ parsingMethod?: 'outputs-library' | 'json-parse' | 'raw';
949
+ /**
950
+ * Whether fallback structure was used (parsing failed but structure guaranteed)
951
+ * When true, parsedOutput contains a fallback structure with _fallback: true flag
952
+ */
953
+ isFallback?: boolean;
954
+ /**
955
+ * Selected object type when multiple object types were provided (v2.1.2+)
956
+ * Indicates which object type from the objectTypes array was used in the response
957
+ */
958
+ selectedObjectType?: string;
959
+ /**
960
+ * Index of the selected object type in the objectTypes array (v2.1.2+)
961
+ */
962
+ selectedObjectTypeIndex?: number;
963
+ /**
964
+ * All available object types that were provided in the request (v2.1.2+)
965
+ */
966
+ availableObjectTypes?: string[];
967
+ /**
968
+ * Confidence score for object type selection (0-1) (v2.1.2+)
969
+ * Higher values indicate better match between response and selected schema
970
+ */
971
+ objectTypeConfidence?: number;
972
+ /**
973
+ * Whether instructions-error object was detected in the response (v2.1.2+)
974
+ */
975
+ hasInstructionsError?: boolean;
976
+ /**
977
+ * Whether prompt-error object was detected in the response (v2.1.2+)
978
+ */
979
+ hasPromptError?: boolean;
980
+ structuredTextStep?: 'first' | 'second';
981
+ /**
982
+ * Output structure audit results (v2.1.1+)
983
+ * Automatically generated when outputSchema is available
984
+ * Provides detailed analysis of the response structure against the expected schema
985
+ */
986
+ outputAudit?: {
987
+ /**
988
+ * Whether all required fields from schema are present
989
+ */
990
+ hasAllRequiredFields: boolean;
991
+ /**
992
+ * Missing required fields (if any)
993
+ */
994
+ missingRequiredFields?: string[];
995
+ /**
996
+ * Fields present in response but not in schema (extra fields)
997
+ */
998
+ extraFields?: string[];
999
+ /**
1000
+ * Fields present in both response and schema
1001
+ */
1002
+ matchingFields?: string[];
1003
+ /**
1004
+ * Total number of fields in the response
1005
+ */
1006
+ responseFieldCount: number;
1007
+ /**
1008
+ * Total number of fields expected in schema
1009
+ */
1010
+ schemaFieldCount: number;
1011
+ /**
1012
+ * Whether the structure matches the schema exactly (all required fields present, no extra fields)
1013
+ */
1014
+ structureMatches: boolean;
1015
+ };
1016
+ /**
1017
+ * Retry information (if retries occurred)
1018
+ */
1019
+ retries?: {
1020
+ /**
1021
+ * Number of retry attempts
1022
+ */
1023
+ count: number;
1024
+ /**
1025
+ * Details of each retry attempt
1026
+ */
1027
+ attempts: Array<{
1028
+ /**
1029
+ * Attempt number (1-based)
1030
+ */
1031
+ attempt: number;
1032
+ /**
1033
+ * Timestamp when retry occurred
1034
+ */
1035
+ timestamp: number;
1036
+ /**
1037
+ * Error message that triggered the retry
1038
+ */
1039
+ error: string;
1040
+ /**
1041
+ * Error type classification (network, http-429, http-5xx, timeout)
1042
+ */
1043
+ errorType: string;
1044
+ /**
1045
+ * Delay in milliseconds before this retry
1046
+ */
1047
+ delayMs: number;
1048
+ }>;
1049
+ };
1050
+ /**
1051
+ * Response repair metadata (when gateway fallback repair was applied)
1052
+ * Indicates that the response was repaired by the gateway before parsing
1053
+ */
1054
+ responseWasFixed?: boolean;
1055
+ /**
1056
+ * Which fix strategy was applied (e.g., 'unwrap-json', 'generate-empty-response')
1057
+ */
1058
+ responseFixApplied?: string;
1059
+ /**
1060
+ * Confidence level of the fix (0-1)
1061
+ */
1062
+ responseFixConfidence?: number;
1063
+ /**
1064
+ * Warnings about the fix (if any)
1065
+ */
1066
+ responseFixWarnings?: string[];
1067
+ };
1068
+ }
1069
+ /**
1070
+ * Instruction metadata for metadata-driven inference (e.g. output schema hints).
1071
+ */
1072
+ export interface InstructionMetadata {
1073
+ /**
1074
+ * Stable instruction identifier when known (e.g. skill or catalog id).
1075
+ */
1076
+ instructionKey: string;
1077
+ /**
1078
+ * Output type (inference type) for parsing responses
1079
+ * Maps to InferenceType from @x12i/outputs-library
1080
+ */
1081
+ outputType?: string;
1082
+ /**
1083
+ * JSON schema for output validation (JSONSchema7 format)
1084
+ */
1085
+ outputSchema?: Record<string, unknown>;
1086
+ /**
1087
+ * Required template variables for this instruction
1088
+ */
1089
+ requiredVariables?: string[];
1090
+ /**
1091
+ * Optional template variables for this instruction
1092
+ */
1093
+ optionalVariables?: string[];
1094
+ /**
1095
+ * Validation rules for the instruction
1096
+ */
1097
+ validationRules?: ValidationRule[];
1098
+ /**
1099
+ * Default output field mapping
1100
+ * Maps output fields to standardized names
1101
+ */
1102
+ defaultOutputMapping?: Record<string, string>;
1103
+ /**
1104
+ * Default parse options for outputs library
1105
+ * Used when outputType is provided
1106
+ */
1107
+ parseOptions?: {
1108
+ question?: string;
1109
+ classes?: string[];
1110
+ [key: string]: unknown;
1111
+ };
1112
+ /**
1113
+ * Version of the instruction
1114
+ */
1115
+ version?: string;
1116
+ [key: string]: unknown;
1117
+ }
1118
+ /**
1119
+ * Validation rule for instruction metadata
1120
+ */
1121
+ export interface ValidationRule {
1122
+ /**
1123
+ * Rule name/identifier
1124
+ */
1125
+ name: string;
1126
+ /**
1127
+ * Rule type (e.g., 'required', 'format', 'range')
1128
+ */
1129
+ type: string;
1130
+ /**
1131
+ * Rule configuration/parameters
1132
+ */
1133
+ config?: Record<string, unknown>;
1134
+ /**
1135
+ * Error message if validation fails
1136
+ */
1137
+ message?: string;
1138
+ }
1139
+ /**
1140
+ * Response transformation hooks configuration
1141
+ * Allows transforming responses at different stages of processing
1142
+ */
1143
+ export interface ResponseTransformationConfig {
1144
+ /**
1145
+ * Transform raw text before parsing
1146
+ * @param rawText - Raw text from LLM response
1147
+ * @param metadata - Request metadata (jobId, agentId, etc.)
1148
+ * @returns Transformed raw text
1149
+ */
1150
+ preParse?: (rawText: string, metadata: any) => string;
1151
+ /**
1152
+ * Transform parsed content after parsing
1153
+ * @param parsed - Parsed content (object/array)
1154
+ * @param metadata - Request metadata
1155
+ * @returns Transformed parsed content
1156
+ */
1157
+ postParse?: (parsed: any, metadata: any) => any;
1158
+ /**
1159
+ * Transform parsed content before validation
1160
+ * @param parsed - Parsed content
1161
+ * @param schema - JSON schema (if available)
1162
+ * @returns Transformed parsed content
1163
+ */
1164
+ preValidate?: (parsed: any, schema: any) => any;
1165
+ /**
1166
+ * Transform validated content after validation
1167
+ * @param validated - Validated content
1168
+ * @param metadata - Request metadata
1169
+ * @returns Transformed validated content
1170
+ */
1171
+ postValidate?: (validated: any, metadata: any) => any;
1172
+ }
1173
+ export type { LLMRequest, LLMResponse };