@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,31 @@
1
+ {
2
+ "contentRegistry": {
3
+ "mode": "dev",
4
+ "localRoot": ".metadata",
5
+ "github": {
6
+ "token": null,
7
+ "repo": null
8
+ },
9
+ "s3": {
10
+ "bucket": null,
11
+ "region": null
12
+ },
13
+ "redis": {
14
+ "host": null,
15
+ "port": null
16
+ },
17
+ "cache": {
18
+ "ttl": 3600000
19
+ }
20
+ },
21
+ "logging": {
22
+ "level": "info",
23
+ "enabled": true
24
+ },
25
+ "templateRendering": {
26
+ "subPathSearch": {
27
+ "enabled": false,
28
+ "roots": ["execution", "input", "inputs"]
29
+ }
30
+ }
31
+ }
@@ -0,0 +1,206 @@
1
+ /**
2
+ * Activity Manager
3
+ *
4
+ * Manages activity tracking for LLM requests.
5
+ * Wraps the ActivityTracker and provides convenience methods.
6
+ */
7
+ import { Activix } from '@x12i/activix';
8
+ import type { Logxer } from '@x12i/logxer';
9
+ import type { ActivityIdentity, ChatRequest, AIRequest, FailureType, LLMResponseFailureSubtype, ResponseParsingFailureSubtype } from './types.js';
10
+ type Request = ChatRequest | AIRequest;
11
+ /**
12
+ * Normalizes and attaches the gateway `identity` envelope on the request (Activix + router correlation).
13
+ * Call at the API boundary before the router when activity tracking is off, and used internally when it is on.
14
+ *
15
+ * **Execution context:** treats `request.identity` as received from upstream. Root fields (`sessionId`,
16
+ * `instance`, and any keys already set on that object) are not replaced by gateway defaults; the gateway
17
+ * only fills missing required pieces and adds local context (e.g. graph linkage) where absent.
18
+ */
19
+ export declare function ensureGatewayRequestIdentity(request: ChatRequest | AIRequest, extras?: Partial<ActivityIdentity>): ActivityIdentity;
20
+ type ActivityMetadata = Record<string, any> & {
21
+ activityId?: string;
22
+ recordId?: string;
23
+ aiRequestId: string;
24
+ runContext?: ActivityIdentity;
25
+ status?: string;
26
+ activityType?: string;
27
+ };
28
+ export interface ActivityManagerConfig {
29
+ enableActivityTracking: boolean;
30
+ customTracker?: Activix;
31
+ logger: Logxer;
32
+ }
33
+ /**
34
+ * Manages activity tracking lifecycle
35
+ */
36
+ export declare class ActivityManager {
37
+ private activix?;
38
+ private initPromise?;
39
+ private mainCollectionName?;
40
+ private skillExecutionsCollectionName;
41
+ private badRequestsCollectionName?;
42
+ private logger;
43
+ constructor(config: ActivityManagerConfig);
44
+ /**
45
+ * Gets upstream-provided AI request ID
46
+ *
47
+ * @param request - Enhanced LLM request
48
+ * @returns AI request ID string
49
+ */
50
+ generateJobId(request: Request): string;
51
+ /**
52
+ * Starts activity tracking for a request
53
+ *
54
+ * IMPORTANT: This method ONLY sends REQUEST data (no response).
55
+ * The same record will be updated later via logSuccess() or logFailure()
56
+ * with response/timing data.
57
+ *
58
+ * KEY CONCEPT: jobId is NOT the unique identifier!
59
+ * - jobId = entire job (can have 100+ activities sharing the same jobId)
60
+ * - taskId = task within a job (can have multiple activities)
61
+ * - Each activity gets its own unique identifier (_id/recordId) from ActivityTracker
62
+ * - The returned ActivityMetadata contains this unique identifier for later updates
63
+ *
64
+ * This ensures:
65
+ * - On start: Only request data is stored → creates NEW record with unique _id
66
+ * - On finish: Only response data is added → updates SAME record by _id/recordId
67
+ * - No duplication: Request data sent once, response data sent once
68
+ * - Multiple activities per job: Each activity is a separate record, grouped by jobId
69
+ *
70
+ * @param request - Enhanced LLM request
71
+ * @param startTime - Request start timestamp
72
+ * @returns Activity metadata (contains unique _id/recordId) or undefined if tracking is disabled
73
+ */
74
+ startActivity(request: Request, startTime: number): Promise<ActivityMetadata | undefined>;
75
+ /**
76
+ * Starts skill execution activity tracking
77
+ *
78
+ * Similar to startActivity() but creates a skill-execution activity record instead of gateway-invocation.
79
+ * Skill executions are tracked separately and stored in the 'skill-executions' collection.
80
+ *
81
+ * @param request - Enhanced LLM request (must be a skill execution)
82
+ * @param instructionMetadata - Optional audit fields when caller tracks instruction catalog info
83
+ * @param startTime - Request start timestamp
84
+ * @returns Activity metadata (contains unique _id/recordId) or undefined if tracking is disabled
85
+ */
86
+ startSkillExecution(request: Request, instructionMetadata: {
87
+ key?: string;
88
+ version?: string;
89
+ rawContent?: string;
90
+ } | undefined, startTime: number): Promise<ActivityMetadata | undefined>;
91
+ /**
92
+ * Logs successful activity completion
93
+ *
94
+ * IMPORTANT: This method ONLY sends RESPONSE/TIMING data (no request data).
95
+ * It updates the SAME record created by startActivity() using the unique record identifier.
96
+ *
97
+ * KEY CONCEPT: jobId is NOT the unique identifier!
98
+ * - jobId = entire job (can have 100+ activities sharing the same jobId)
99
+ * - taskId = task within a job (can have multiple activities)
100
+ * - Each activity has its own unique identifier (_id/recordId) returned by startActivity()
101
+ *
102
+ * This ensures:
103
+ * - On start: Only request data is stored (via startActivity) → creates new record with unique _id
104
+ * - On finish: Only response data is added (via logSuccess) → updates same record by _id/recordId
105
+ * - No duplication: Request data sent once, response data sent once
106
+ * - Same record: Updated by unique identifier (_id/recordId), NOT by jobId
107
+ *
108
+ * Structure sent to ActivityTracker:
109
+ * - response: { content, metadata } (response object)
110
+ * - endTime, duration (timing completion)
111
+ * - cost (cost calculation)
112
+ * - status: 'success' (updated by ActivityTracker)
113
+ * - NO request data (already stored in startActivity)
114
+ * - NO config data (already stored in startActivity)
115
+ *
116
+ * @param activity - Activity metadata from startActivity() (contains unique _id/recordId for record lookup)
117
+ * @param details - Success details (cost, response, timing)
118
+ */
119
+ logSuccess(activity: ActivityMetadata | undefined, details: {
120
+ cost?: number;
121
+ response: any;
122
+ endTime: number;
123
+ duration: number;
124
+ }): Promise<void>;
125
+ /**
126
+ * Logs failed activity
127
+ *
128
+ * IMPORTANT: This method ONLY sends ERROR/TIMING data (no request/response data).
129
+ * It updates the SAME record created by startActivity() using the unique record identifier.
130
+ *
131
+ * KEY CONCEPT: jobId is NOT the unique identifier!
132
+ * - jobId = entire job (can have 100+ activities sharing the same jobId)
133
+ * - taskId = task within a job (can have multiple activities)
134
+ * - Each activity has its own unique identifier (_id/recordId) returned by startActivity()
135
+ *
136
+ * Structure sent to ActivityTracker:
137
+ * - error (error message)
138
+ * - endTime, duration (timing completion)
139
+ * - status: 'failed' (updated by ActivityTracker)
140
+ * - NO request data (already stored in startActivity)
141
+ * - NO response data (request failed)
142
+ *
143
+ * @param activity - Activity metadata from startActivity() (contains unique _id/recordId for record lookup)
144
+ * @param error - Error that occurred
145
+ * @param details - Failure details (timing, error message)
146
+ */
147
+ logFailure(activity: ActivityMetadata | undefined, error: Error, details: {
148
+ endTime: number;
149
+ duration: number;
150
+ error: string;
151
+ failureType?: FailureType;
152
+ failureSubtype?: LLMResponseFailureSubtype | ResponseParsingFailureSubtype;
153
+ response?: any;
154
+ }): Promise<void>;
155
+ /**
156
+ * Logs a bad request (error that occurred before startActivity)
157
+ *
158
+ * This method creates a new activity record specifically for tracking validation
159
+ * errors, format extraction failures, and other errors that occur before the
160
+ * normal activity lifecycle begins.
161
+ *
162
+ * Uses native ActivityTracker support for bad requests collection via the
163
+ * `collectionName` option in `startActivity()` and `logFailure()` methods.
164
+ * The bad requests collection is configured via `badRequestsCollectionName` in
165
+ * the ActivityTracker constructor (defaults to 'ai-bad-requests').
166
+ *
167
+ * Bad requests are automatically routed to the separate bad requests collection
168
+ * and can be filtered by `failureType` field:
169
+ * - 'validation-failure': Request validation errors
170
+ * - 'format-extraction-failure': Format extraction errors
171
+ * - 'configuration-failure': Configuration errors
172
+ *
173
+ * @param request - The request that failed
174
+ * @param error - The error that occurred
175
+ * @param details - Additional error details
176
+ * @param startTime - When the request started
177
+ */
178
+ logBadRequest(request: Request, error: Error, details: {
179
+ endTime: number;
180
+ duration: number;
181
+ error: string;
182
+ errorType?: string;
183
+ diagnosticInfo?: Record<string, any>;
184
+ failureType?: 'validation-failure' | 'format-extraction-failure' | 'configuration-failure';
185
+ }, startTime: number): Promise<void>;
186
+ /**
187
+ * Gets the underlying activity tracker instance
188
+ *
189
+ * @returns Activix instance or undefined if not enabled
190
+ */
191
+ getTracker(): Activix | undefined;
192
+ /**
193
+ * Get status of activity tracker
194
+ */
195
+ getStatus(): {
196
+ activityTracking: {
197
+ enabled: boolean;
198
+ tracker?: any;
199
+ };
200
+ };
201
+ /**
202
+ * Shutdown tracker
203
+ */
204
+ shutdown(): Promise<void>;
205
+ }
206
+ export {};