@retab/node 0.0.0-reserved → 0.0.43

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 (270) hide show
  1. package/README.md +293 -2
  2. package/dist/api/client.d.ts +15 -0
  3. package/dist/api/client.d.ts.map +1 -0
  4. package/dist/api/client.js +16 -0
  5. package/dist/api/consensus/client.d.ts +7 -0
  6. package/dist/api/consensus/client.d.ts.map +1 -0
  7. package/dist/api/consensus/client.js +14 -0
  8. package/dist/api/deployments/client.d.ts +20 -0
  9. package/dist/api/deployments/client.d.ts.map +1 -0
  10. package/dist/api/deployments/client.js +23 -0
  11. package/dist/api/documents/client.d.ts +10 -0
  12. package/dist/api/documents/client.d.ts.map +1 -0
  13. package/dist/api/documents/client.js +35 -0
  14. package/dist/api/models/client.d.ts +17 -0
  15. package/dist/api/models/client.d.ts.map +1 -0
  16. package/dist/api/models/client.js +15 -0
  17. package/dist/api/schemas/client.d.ts +12 -0
  18. package/dist/api/schemas/client.d.ts.map +1 -0
  19. package/dist/api/schemas/client.js +14 -0
  20. package/dist/client.d.ts +50 -0
  21. package/dist/client.d.ts.map +1 -0
  22. package/dist/client.js +135 -0
  23. package/dist/errors.d.ts +34 -0
  24. package/dist/errors.d.ts.map +1 -0
  25. package/dist/errors.js +53 -0
  26. package/dist/generated_types.d.ts +64373 -0
  27. package/dist/generated_types.d.ts.map +1 -0
  28. package/dist/generated_types.js +2267 -0
  29. package/dist/index.d.ts +8 -0
  30. package/dist/index.d.ts.map +1 -0
  31. package/dist/index.js +9 -0
  32. package/dist/mime.d.ts +5 -0
  33. package/dist/mime.d.ts.map +1 -0
  34. package/dist/mime.js +66 -0
  35. package/dist/resource.d.ts +12 -0
  36. package/dist/resource.d.ts.map +1 -0
  37. package/dist/resource.js +19 -0
  38. package/dist/resources/consensus/completions.d.ts +66 -0
  39. package/dist/resources/consensus/completions.d.ts.map +1 -0
  40. package/dist/resources/consensus/completions.js +84 -0
  41. package/dist/resources/consensus/index.d.ts +72 -0
  42. package/dist/resources/consensus/index.d.ts.map +1 -0
  43. package/dist/resources/consensus/index.js +76 -0
  44. package/dist/resources/consensus/responses.d.ts +69 -0
  45. package/dist/resources/consensus/responses.d.ts.map +1 -0
  46. package/dist/resources/consensus/responses.js +99 -0
  47. package/dist/resources/documents/extractions.d.ts +74 -0
  48. package/dist/resources/documents/extractions.d.ts.map +1 -0
  49. package/dist/resources/documents/extractions.js +196 -0
  50. package/dist/resources/documents/index.d.ts +21 -0
  51. package/dist/resources/documents/index.d.ts.map +1 -0
  52. package/dist/resources/documents/index.js +55 -0
  53. package/dist/resources/evaluations/documents.d.ts +40 -0
  54. package/dist/resources/evaluations/documents.d.ts.map +1 -0
  55. package/dist/resources/evaluations/documents.js +123 -0
  56. package/dist/resources/evaluations/index.d.ts +14 -0
  57. package/dist/resources/evaluations/index.d.ts.map +1 -0
  58. package/dist/resources/evaluations/index.js +17 -0
  59. package/dist/resources/evaluations/iterations.d.ts +50 -0
  60. package/dist/resources/evaluations/iterations.d.ts.map +1 -0
  61. package/dist/resources/evaluations/iterations.js +156 -0
  62. package/dist/resources/files.d.ts +82 -0
  63. package/dist/resources/files.d.ts.map +1 -0
  64. package/dist/resources/files.js +150 -0
  65. package/dist/resources/finetuning.d.ts +105 -0
  66. package/dist/resources/finetuning.d.ts.map +1 -0
  67. package/dist/resources/finetuning.js +181 -0
  68. package/dist/resources/index.d.ts +11 -0
  69. package/dist/resources/index.d.ts.map +1 -0
  70. package/dist/resources/index.js +10 -0
  71. package/dist/resources/models.d.ts +57 -0
  72. package/dist/resources/models.d.ts.map +1 -0
  73. package/dist/resources/models.js +72 -0
  74. package/dist/resources/processors/automations/endpoints.d.ts +90 -0
  75. package/dist/resources/processors/automations/endpoints.d.ts.map +1 -0
  76. package/dist/resources/processors/automations/endpoints.js +145 -0
  77. package/dist/resources/processors/automations/index.d.ts +7 -0
  78. package/dist/resources/processors/automations/index.d.ts.map +1 -0
  79. package/dist/resources/processors/automations/index.js +6 -0
  80. package/dist/resources/processors/automations/links.d.ts +90 -0
  81. package/dist/resources/processors/automations/links.d.ts.map +1 -0
  82. package/dist/resources/processors/automations/links.js +149 -0
  83. package/dist/resources/processors/automations/logs.d.ts +35 -0
  84. package/dist/resources/processors/automations/logs.d.ts.map +1 -0
  85. package/dist/resources/processors/automations/logs.js +60 -0
  86. package/dist/resources/processors/automations/mailboxes.d.ts +102 -0
  87. package/dist/resources/processors/automations/mailboxes.d.ts.map +1 -0
  88. package/dist/resources/processors/automations/mailboxes.js +157 -0
  89. package/dist/resources/processors/automations/outlook.d.ts +114 -0
  90. package/dist/resources/processors/automations/outlook.d.ts.map +1 -0
  91. package/dist/resources/processors/automations/outlook.js +170 -0
  92. package/dist/resources/processors/automations/tests.d.ts +58 -0
  93. package/dist/resources/processors/automations/tests.d.ts.map +1 -0
  94. package/dist/resources/processors/automations/tests.js +90 -0
  95. package/dist/resources/processors/index.d.ts +303 -0
  96. package/dist/resources/processors/index.d.ts.map +1 -0
  97. package/dist/resources/processors/index.js +261 -0
  98. package/dist/resources/schemas.d.ts +63 -0
  99. package/dist/resources/schemas.d.ts.map +1 -0
  100. package/dist/resources/schemas.js +183 -0
  101. package/dist/resources/secrets/external_api_keys.d.ts +61 -0
  102. package/dist/resources/secrets/external_api_keys.d.ts.map +1 -0
  103. package/dist/resources/secrets/external_api_keys.js +120 -0
  104. package/dist/resources/secrets/index.d.ts +14 -0
  105. package/dist/resources/secrets/index.d.ts.map +1 -0
  106. package/dist/resources/secrets/index.js +17 -0
  107. package/dist/resources/secrets/webhooks.d.ts +73 -0
  108. package/dist/resources/secrets/webhooks.d.ts.map +1 -0
  109. package/dist/resources/secrets/webhooks.js +145 -0
  110. package/dist/resources/usage.d.ts +223 -0
  111. package/dist/resources/usage.d.ts.map +1 -0
  112. package/dist/resources/usage.js +310 -0
  113. package/dist/types/ai_models.d.ts +389 -0
  114. package/dist/types/ai_models.d.ts.map +1 -0
  115. package/dist/types/ai_models.js +145 -0
  116. package/dist/types/automations/cron.d.ts +28 -0
  117. package/dist/types/automations/cron.d.ts.map +1 -0
  118. package/dist/types/automations/cron.js +1 -0
  119. package/dist/types/automations/endpoints.d.ts +13 -0
  120. package/dist/types/automations/endpoints.d.ts.map +1 -0
  121. package/dist/types/automations/endpoints.js +1 -0
  122. package/dist/types/automations/index.d.ts +7 -0
  123. package/dist/types/automations/index.d.ts.map +1 -0
  124. package/dist/types/automations/index.js +6 -0
  125. package/dist/types/automations/links.d.ts +15 -0
  126. package/dist/types/automations/links.d.ts.map +1 -0
  127. package/dist/types/automations/links.js +1 -0
  128. package/dist/types/automations/mailboxes.d.ts +18 -0
  129. package/dist/types/automations/mailboxes.d.ts.map +1 -0
  130. package/dist/types/automations/mailboxes.js +1 -0
  131. package/dist/types/automations/outlook.d.ts +37 -0
  132. package/dist/types/automations/outlook.d.ts.map +1 -0
  133. package/dist/types/automations/outlook.js +1 -0
  134. package/dist/types/automations/webhooks.d.ts +13 -0
  135. package/dist/types/automations/webhooks.d.ts.map +1 -0
  136. package/dist/types/automations/webhooks.js +1 -0
  137. package/dist/types/browser_canvas.d.ts +4 -0
  138. package/dist/types/browser_canvas.d.ts.map +1 -0
  139. package/dist/types/browser_canvas.js +2 -0
  140. package/dist/types/chat.d.ts +99 -0
  141. package/dist/types/chat.d.ts.map +1 -0
  142. package/dist/types/chat.js +20 -0
  143. package/dist/types/consensus.d.ts +10 -0
  144. package/dist/types/consensus.d.ts.map +1 -0
  145. package/dist/types/consensus.js +1 -0
  146. package/dist/types/db/annotations.d.ts +108 -0
  147. package/dist/types/db/annotations.d.ts.map +1 -0
  148. package/dist/types/db/annotations.js +6 -0
  149. package/dist/types/db/files.d.ts +133 -0
  150. package/dist/types/db/files.d.ts.map +1 -0
  151. package/dist/types/db/files.js +5 -0
  152. package/dist/types/documents/extractions.d.ts +1849 -0
  153. package/dist/types/documents/extractions.d.ts.map +1 -0
  154. package/dist/types/documents/extractions.js +211 -0
  155. package/dist/types/documents/processing.d.ts +249 -0
  156. package/dist/types/documents/processing.d.ts.map +1 -0
  157. package/dist/types/documents/processing.js +6 -0
  158. package/dist/types/evaluations/iterations.d.ts +41 -0
  159. package/dist/types/evaluations/iterations.d.ts.map +1 -0
  160. package/dist/types/evaluations/iterations.js +1 -0
  161. package/dist/types/jobs/base.d.ts +162 -0
  162. package/dist/types/jobs/base.d.ts.map +1 -0
  163. package/dist/types/jobs/base.js +6 -0
  164. package/dist/types/jobs/specialized.d.ts +200 -0
  165. package/dist/types/jobs/specialized.d.ts.map +1 -0
  166. package/dist/types/jobs/specialized.js +37 -0
  167. package/dist/types/logs.d.ts +92 -0
  168. package/dist/types/logs.d.ts.map +1 -0
  169. package/dist/types/logs.js +1 -0
  170. package/dist/types/mime.d.ts +426 -0
  171. package/dist/types/mime.d.ts.map +1 -0
  172. package/dist/types/mime.js +48 -0
  173. package/dist/types/modalities.d.ts +31 -0
  174. package/dist/types/modalities.d.ts.map +1 -0
  175. package/dist/types/modalities.js +109 -0
  176. package/dist/types/pagination.d.ts +5 -0
  177. package/dist/types/pagination.d.ts.map +1 -0
  178. package/dist/types/pagination.js +1 -0
  179. package/dist/types/schemas/enhancement.d.ts +250 -0
  180. package/dist/types/schemas/enhancement.d.ts.map +1 -0
  181. package/dist/types/schemas/enhancement.js +6 -0
  182. package/dist/types/schemas/generate.d.ts +160 -0
  183. package/dist/types/schemas/generate.d.ts.map +1 -0
  184. package/dist/types/schemas/generate.js +19 -0
  185. package/dist/types/schemas/object.d.ts +116 -0
  186. package/dist/types/schemas/object.d.ts.map +1 -0
  187. package/dist/types/schemas/object.js +861 -0
  188. package/dist/types/secrets/external_api_keys.d.ts +27 -0
  189. package/dist/types/secrets/external_api_keys.d.ts.map +1 -0
  190. package/dist/types/secrets/external_api_keys.js +11 -0
  191. package/dist/types/secrets/index.d.ts +2 -0
  192. package/dist/types/secrets/index.d.ts.map +1 -0
  193. package/dist/types/secrets/index.js +1 -0
  194. package/dist/types/standards.d.ts +37 -0
  195. package/dist/types/standards.d.ts.map +1 -0
  196. package/dist/types/standards.js +1 -0
  197. package/dist/types.d.ts +276 -0
  198. package/dist/types.d.ts.map +1 -0
  199. package/dist/types.js +85 -0
  200. package/dist/utils/ai_models.d.ts +10 -0
  201. package/dist/utils/ai_models.d.ts.map +1 -0
  202. package/dist/utils/ai_models.js +183 -0
  203. package/dist/utils/batch_processing.d.ts +227 -0
  204. package/dist/utils/batch_processing.d.ts.map +1 -0
  205. package/dist/utils/batch_processing.js +268 -0
  206. package/dist/utils/benchmarking.d.ts +115 -0
  207. package/dist/utils/benchmarking.d.ts.map +1 -0
  208. package/dist/utils/benchmarking.js +355 -0
  209. package/dist/utils/chat.d.ts +70 -0
  210. package/dist/utils/chat.d.ts.map +1 -0
  211. package/dist/utils/chat.js +79 -0
  212. package/dist/utils/cost_calculation.d.ts +26 -0
  213. package/dist/utils/cost_calculation.d.ts.map +1 -0
  214. package/dist/utils/cost_calculation.js +99 -0
  215. package/dist/utils/datasets.d.ts +135 -0
  216. package/dist/utils/datasets.d.ts.map +1 -0
  217. package/dist/utils/datasets.js +359 -0
  218. package/dist/utils/display.d.ts +108 -0
  219. package/dist/utils/display.d.ts.map +1 -0
  220. package/dist/utils/display.js +244 -0
  221. package/dist/utils/hash.d.ts +18 -0
  222. package/dist/utils/hash.d.ts.map +1 -0
  223. package/dist/utils/hash.js +31 -0
  224. package/dist/utils/hashing.d.ts +18 -0
  225. package/dist/utils/hashing.d.ts.map +1 -0
  226. package/dist/utils/hashing.js +28 -0
  227. package/dist/utils/index.d.ts +8 -0
  228. package/dist/utils/index.d.ts.map +1 -0
  229. package/dist/utils/index.js +10 -0
  230. package/dist/utils/json_schema.d.ts +18 -0
  231. package/dist/utils/json_schema.d.ts.map +1 -0
  232. package/dist/utils/json_schema.js +334 -0
  233. package/dist/utils/json_schema_utils.d.ts +42 -0
  234. package/dist/utils/json_schema_utils.d.ts.map +1 -0
  235. package/dist/utils/json_schema_utils.js +212 -0
  236. package/dist/utils/jsonl.d.ts +60 -0
  237. package/dist/utils/jsonl.d.ts.map +1 -0
  238. package/dist/utils/jsonl.js +259 -0
  239. package/dist/utils/mime.d.ts +6 -0
  240. package/dist/utils/mime.d.ts.map +1 -0
  241. package/dist/utils/mime.js +129 -0
  242. package/dist/utils/model_cards.d.ts +219 -0
  243. package/dist/utils/model_cards.d.ts.map +1 -0
  244. package/dist/utils/model_cards.js +462 -0
  245. package/dist/utils/prompt_optimization.d.ts +96 -0
  246. package/dist/utils/prompt_optimization.d.ts.map +1 -0
  247. package/dist/utils/prompt_optimization.js +275 -0
  248. package/dist/utils/responses.d.ts +35 -0
  249. package/dist/utils/responses.d.ts.map +1 -0
  250. package/dist/utils/responses.js +37 -0
  251. package/dist/utils/stream.d.ts +13 -0
  252. package/dist/utils/stream.d.ts.map +1 -0
  253. package/dist/utils/stream.js +64 -0
  254. package/dist/utils/stream_context_managers.d.ts +147 -0
  255. package/dist/utils/stream_context_managers.d.ts.map +1 -0
  256. package/dist/utils/stream_context_managers.js +380 -0
  257. package/dist/utils/usage.d.ts +57 -0
  258. package/dist/utils/usage.d.ts.map +1 -0
  259. package/dist/utils/usage.js +97 -0
  260. package/dist/utils/webhook_secrets.d.ts +59 -0
  261. package/dist/utils/webhook_secrets.d.ts.map +1 -0
  262. package/dist/utils/webhook_secrets.js +107 -0
  263. package/dist/utils/zod_to_json_schema.d.ts +11 -0
  264. package/dist/utils/zod_to_json_schema.d.ts.map +1 -0
  265. package/dist/utils/zod_to_json_schema.js +123 -0
  266. package/dist/utils.d.ts +19 -0
  267. package/dist/utils.d.ts.map +1 -0
  268. package/dist/utils.js +1 -0
  269. package/package.json +62 -6
  270. package/index.js +0 -7
@@ -0,0 +1,380 @@
1
+ /**
2
+ * Stream context management utilities for proper resource cleanup
3
+ * Equivalent to Python's stream_context_managers.py
4
+ */
5
+ /**
6
+ * AsyncGenerator context manager for proper cleanup
7
+ */
8
+ export class AsyncGeneratorContextManager {
9
+ constructor(generator) {
10
+ this.isFinalized = false;
11
+ this.generator = generator;
12
+ }
13
+ async *[Symbol.asyncIterator]() {
14
+ try {
15
+ yield* this.generator;
16
+ }
17
+ finally {
18
+ await this.cleanup();
19
+ }
20
+ }
21
+ async next() {
22
+ try {
23
+ return await this.generator.next();
24
+ }
25
+ catch (error) {
26
+ await this.cleanup();
27
+ throw error;
28
+ }
29
+ }
30
+ async return(value) {
31
+ try {
32
+ return await this.generator.return(value);
33
+ }
34
+ finally {
35
+ await this.cleanup();
36
+ }
37
+ }
38
+ async throw(error) {
39
+ try {
40
+ return await this.generator.throw(error);
41
+ }
42
+ finally {
43
+ await this.cleanup();
44
+ }
45
+ }
46
+ async cleanup() {
47
+ if (!this.isFinalized) {
48
+ this.isFinalized = true;
49
+ try {
50
+ // Ensure generator is properly closed
51
+ if (typeof this.generator.return === 'function') {
52
+ await this.generator.return();
53
+ }
54
+ }
55
+ catch (error) {
56
+ // Ignore cleanup errors
57
+ }
58
+ }
59
+ }
60
+ async [Symbol.asyncDispose]() {
61
+ await this.cleanup();
62
+ }
63
+ }
64
+ /**
65
+ * Synchronous generator context manager
66
+ */
67
+ export class GeneratorContextManager {
68
+ constructor(generator) {
69
+ this.isFinalized = false;
70
+ this.generator = generator;
71
+ }
72
+ *[Symbol.iterator]() {
73
+ try {
74
+ yield* this.generator;
75
+ }
76
+ finally {
77
+ this.cleanup();
78
+ }
79
+ }
80
+ next() {
81
+ try {
82
+ return this.generator.next();
83
+ }
84
+ catch (error) {
85
+ this.cleanup();
86
+ throw error;
87
+ }
88
+ }
89
+ return(value) {
90
+ try {
91
+ return this.generator.return(value);
92
+ }
93
+ finally {
94
+ this.cleanup();
95
+ }
96
+ }
97
+ throw(error) {
98
+ try {
99
+ return this.generator.throw(error);
100
+ }
101
+ finally {
102
+ this.cleanup();
103
+ }
104
+ }
105
+ cleanup() {
106
+ if (!this.isFinalized) {
107
+ this.isFinalized = true;
108
+ try {
109
+ // Ensure generator is properly closed
110
+ if (typeof this.generator.return === 'function') {
111
+ this.generator.return();
112
+ }
113
+ }
114
+ catch (error) {
115
+ // Ignore cleanup errors
116
+ }
117
+ }
118
+ }
119
+ [Symbol.dispose]() {
120
+ this.cleanup();
121
+ }
122
+ }
123
+ /**
124
+ * Decorator to wrap async generator functions with context management
125
+ */
126
+ export function asAsyncContextManager(generatorFunction) {
127
+ return (...args) => {
128
+ const generator = generatorFunction(...args);
129
+ return new AsyncGeneratorContextManager(generator);
130
+ };
131
+ }
132
+ /**
133
+ * Decorator to wrap sync generator functions with context management
134
+ */
135
+ export function asContextManager(generatorFunction) {
136
+ return (...args) => {
137
+ const generator = generatorFunction(...args);
138
+ return new GeneratorContextManager(generator);
139
+ };
140
+ }
141
+ /**
142
+ * Stream wrapper that provides automatic cleanup and error handling
143
+ */
144
+ export class StreamWrapper {
145
+ constructor(stream, cleanup) {
146
+ this.isActive = true;
147
+ this.cleanup = null;
148
+ this.stream = stream;
149
+ this.cleanup = cleanup || null;
150
+ }
151
+ async *[Symbol.asyncIterator]() {
152
+ try {
153
+ while (this.isActive) {
154
+ const result = await this.stream.next();
155
+ if (result.done) {
156
+ break;
157
+ }
158
+ yield result.value;
159
+ }
160
+ }
161
+ finally {
162
+ await this.close();
163
+ }
164
+ }
165
+ async close() {
166
+ if (this.isActive) {
167
+ this.isActive = false;
168
+ try {
169
+ // Close the stream
170
+ if (typeof this.stream.return === 'function') {
171
+ await this.stream.return();
172
+ }
173
+ }
174
+ catch (error) {
175
+ // Ignore stream closure errors
176
+ }
177
+ // Run custom cleanup
178
+ if (this.cleanup) {
179
+ try {
180
+ await this.cleanup();
181
+ }
182
+ catch (error) {
183
+ console.warn('Stream cleanup failed:', error);
184
+ }
185
+ }
186
+ }
187
+ }
188
+ async [Symbol.asyncDispose]() {
189
+ await this.close();
190
+ }
191
+ }
192
+ /**
193
+ * Timeout wrapper for streams
194
+ */
195
+ export class StreamTimeoutWrapper extends StreamWrapper {
196
+ constructor(stream, timeoutMs, cleanup) {
197
+ super(stream, cleanup);
198
+ this.timeoutId = null;
199
+ this.timeout = timeoutMs;
200
+ this.lastActivity = Date.now();
201
+ }
202
+ async *[Symbol.asyncIterator]() {
203
+ try {
204
+ this.startTimeout();
205
+ while (this.isActive) {
206
+ const result = await this.stream.next();
207
+ this.updateActivity();
208
+ if (result.done) {
209
+ break;
210
+ }
211
+ yield result.value;
212
+ }
213
+ }
214
+ finally {
215
+ this.clearTimeout();
216
+ await this.close();
217
+ }
218
+ }
219
+ startTimeout() {
220
+ this.timeoutId = setTimeout(() => {
221
+ if (Date.now() - this.lastActivity > this.timeout) {
222
+ this.close().catch(console.error);
223
+ }
224
+ }, this.timeout);
225
+ }
226
+ clearTimeout() {
227
+ if (this.timeoutId) {
228
+ clearTimeout(this.timeoutId);
229
+ this.timeoutId = null;
230
+ }
231
+ }
232
+ updateActivity() {
233
+ this.lastActivity = Date.now();
234
+ }
235
+ async close() {
236
+ this.clearTimeout();
237
+ await super.close();
238
+ }
239
+ }
240
+ /**
241
+ * Batch processing wrapper for streams
242
+ */
243
+ export class StreamBatchProcessor {
244
+ constructor(stream, batchSize, cleanup) {
245
+ this.currentBatch = [];
246
+ this.isActive = true;
247
+ this.cleanup = null;
248
+ this.stream = stream;
249
+ this.batchSize = batchSize;
250
+ this.cleanup = cleanup || null;
251
+ }
252
+ async *[Symbol.asyncIterator]() {
253
+ try {
254
+ while (this.isActive) {
255
+ const result = await this.stream.next();
256
+ if (result.done) {
257
+ // Yield remaining items in batch
258
+ if (this.currentBatch.length > 0) {
259
+ yield [...this.currentBatch];
260
+ this.currentBatch = [];
261
+ }
262
+ break;
263
+ }
264
+ this.currentBatch.push(result.value);
265
+ if (this.currentBatch.length >= this.batchSize) {
266
+ yield [...this.currentBatch];
267
+ this.currentBatch = [];
268
+ }
269
+ }
270
+ }
271
+ finally {
272
+ await this.close();
273
+ }
274
+ }
275
+ async close() {
276
+ if (this.isActive) {
277
+ this.isActive = false;
278
+ try {
279
+ if (typeof this.stream.return === 'function') {
280
+ await this.stream.return();
281
+ }
282
+ }
283
+ catch (error) {
284
+ // Ignore stream closure errors
285
+ }
286
+ if (this.cleanup) {
287
+ try {
288
+ await this.cleanup();
289
+ }
290
+ catch (error) {
291
+ console.warn('Stream cleanup failed:', error);
292
+ }
293
+ }
294
+ }
295
+ }
296
+ async [Symbol.asyncDispose]() {
297
+ await this.close();
298
+ }
299
+ }
300
+ /**
301
+ * Error recovery wrapper for streams
302
+ */
303
+ export class StreamErrorRecovery extends StreamWrapper {
304
+ constructor(stream, maxRetries = 3, errorHandler, cleanup) {
305
+ super(stream, cleanup);
306
+ this.retryCount = 0;
307
+ this.errorHandler = null;
308
+ this.maxRetries = maxRetries;
309
+ this.errorHandler = errorHandler || null;
310
+ }
311
+ async *[Symbol.asyncIterator]() {
312
+ try {
313
+ while (this.isActive && this.retryCount <= this.maxRetries) {
314
+ try {
315
+ const result = await this.stream.next();
316
+ if (result.done) {
317
+ break;
318
+ }
319
+ // Reset retry count on successful read
320
+ this.retryCount = 0;
321
+ yield result.value;
322
+ }
323
+ catch (error) {
324
+ this.retryCount++;
325
+ const shouldRetry = this.errorHandler ?
326
+ this.errorHandler(error) :
327
+ this.retryCount <= this.maxRetries;
328
+ if (!shouldRetry) {
329
+ throw error;
330
+ }
331
+ // Wait before retry
332
+ await new Promise(resolve => setTimeout(resolve, Math.pow(2, this.retryCount) * 1000));
333
+ }
334
+ }
335
+ }
336
+ finally {
337
+ await this.close();
338
+ }
339
+ }
340
+ }
341
+ /**
342
+ * Utility functions for creating context-managed streams
343
+ */
344
+ export const streamUtils = {
345
+ /**
346
+ * Wrap an async generator with automatic cleanup
347
+ */
348
+ wrap: (stream, cleanup) => {
349
+ return new StreamWrapper(stream, cleanup);
350
+ },
351
+ /**
352
+ * Add timeout handling to a stream
353
+ */
354
+ withTimeout: (stream, timeoutMs, cleanup) => {
355
+ return new StreamTimeoutWrapper(stream, timeoutMs, cleanup);
356
+ },
357
+ /**
358
+ * Add batch processing to a stream
359
+ */
360
+ withBatching: (stream, batchSize, cleanup) => {
361
+ return new StreamBatchProcessor(stream, batchSize, cleanup);
362
+ },
363
+ /**
364
+ * Add error recovery to a stream
365
+ */
366
+ withErrorRecovery: (stream, maxRetries = 3, errorHandler, cleanup) => {
367
+ return new StreamErrorRecovery(stream, maxRetries, errorHandler, cleanup);
368
+ },
369
+ };
370
+ export default {
371
+ AsyncGeneratorContextManager,
372
+ GeneratorContextManager,
373
+ StreamWrapper,
374
+ StreamTimeoutWrapper,
375
+ StreamBatchProcessor,
376
+ StreamErrorRecovery,
377
+ asAsyncContextManager,
378
+ asContextManager,
379
+ streamUtils,
380
+ };
@@ -0,0 +1,57 @@
1
+ /**
2
+ * Usage tracking and analytics utilities
3
+ * Equivalent to Python's utils/usage/usage.py
4
+ */
5
+ export interface UsageMetrics {
6
+ timestamp: Date;
7
+ organization_id: string;
8
+ user_id?: string;
9
+ operation_type: 'extraction' | 'generation' | 'analysis' | 'batch' | 'streaming';
10
+ model_used: string;
11
+ provider: string;
12
+ tokens_used: {
13
+ input: number;
14
+ output: number;
15
+ total: number;
16
+ };
17
+ cost_usd: number;
18
+ latency_ms: number;
19
+ success: boolean;
20
+ error_type?: string;
21
+ request_id: string;
22
+ metadata?: Record<string, any>;
23
+ }
24
+ export interface UsageAggregation {
25
+ period: 'hour' | 'day' | 'week' | 'month';
26
+ start_time: Date;
27
+ end_time: Date;
28
+ total_requests: number;
29
+ successful_requests: number;
30
+ failed_requests: number;
31
+ total_tokens: number;
32
+ total_cost_usd: number;
33
+ average_latency_ms: number;
34
+ operations_by_type: Record<string, number>;
35
+ models_by_usage: Record<string, {
36
+ requests: number;
37
+ tokens: number;
38
+ cost: number;
39
+ }>;
40
+ providers_by_usage: Record<string, {
41
+ requests: number;
42
+ tokens: number;
43
+ cost: number;
44
+ }>;
45
+ error_breakdown: Record<string, number>;
46
+ }
47
+ export declare class UsageTracker {
48
+ private metrics;
49
+ recordUsage(metrics: Omit<UsageMetrics, 'timestamp'>): void;
50
+ getUsageAggregation(organizationId: string, period: 'hour' | 'day' | 'week' | 'month', startTime?: Date, endTime?: Date): UsageAggregation;
51
+ private getPeriodStart;
52
+ }
53
+ declare const _default: {
54
+ UsageTracker: typeof UsageTracker;
55
+ };
56
+ export default _default;
57
+ //# sourceMappingURL=usage.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"usage.d.ts","sourceRoot":"","sources":["../../src/utils/usage.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,MAAM,WAAW,YAAY;IAC3B,SAAS,EAAE,IAAI,CAAC;IAChB,eAAe,EAAE,MAAM,CAAC;IACxB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,cAAc,EAAE,YAAY,GAAG,YAAY,GAAG,UAAU,GAAG,OAAO,GAAG,WAAW,CAAC;IACjF,UAAU,EAAE,MAAM,CAAC;IACnB,QAAQ,EAAE,MAAM,CAAC;IACjB,WAAW,EAAE;QACX,KAAK,EAAE,MAAM,CAAC;QACd,MAAM,EAAE,MAAM,CAAC;QACf,KAAK,EAAE,MAAM,CAAC;KACf,CAAC;IACF,QAAQ,EAAE,MAAM,CAAC;IACjB,UAAU,EAAE,MAAM,CAAC;IACnB,OAAO,EAAE,OAAO,CAAC;IACjB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,UAAU,EAAE,MAAM,CAAC;IACnB,QAAQ,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;CAChC;AAED,MAAM,WAAW,gBAAgB;IAC/B,MAAM,EAAE,MAAM,GAAG,KAAK,GAAG,MAAM,GAAG,OAAO,CAAC;IAC1C,UAAU,EAAE,IAAI,CAAC;IACjB,QAAQ,EAAE,IAAI,CAAC;IACf,cAAc,EAAE,MAAM,CAAC;IACvB,mBAAmB,EAAE,MAAM,CAAC;IAC5B,eAAe,EAAE,MAAM,CAAC;IACxB,YAAY,EAAE,MAAM,CAAC;IACrB,cAAc,EAAE,MAAM,CAAC;IACvB,kBAAkB,EAAE,MAAM,CAAC;IAC3B,kBAAkB,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAC3C,eAAe,EAAE,MAAM,CAAC,MAAM,EAAE;QAC9B,QAAQ,EAAE,MAAM,CAAC;QACjB,MAAM,EAAE,MAAM,CAAC;QACf,IAAI,EAAE,MAAM,CAAC;KACd,CAAC,CAAC;IACH,kBAAkB,EAAE,MAAM,CAAC,MAAM,EAAE;QACjC,QAAQ,EAAE,MAAM,CAAC;QACjB,MAAM,EAAE,MAAM,CAAC;QACf,IAAI,EAAE,MAAM,CAAC;KACd,CAAC,CAAC;IACH,eAAe,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;CACzC;AAED,qBAAa,YAAY;IACvB,OAAO,CAAC,OAAO,CAAsB;IAErC,WAAW,CAAC,OAAO,EAAE,IAAI,CAAC,YAAY,EAAE,WAAW,CAAC,GAAG,IAAI;IAS3D,mBAAmB,CACjB,cAAc,EAAE,MAAM,EACtB,MAAM,EAAE,MAAM,GAAG,KAAK,GAAG,MAAM,GAAG,OAAO,EACzC,SAAS,CAAC,EAAE,IAAI,EAChB,OAAO,CAAC,EAAE,IAAI,GACb,gBAAgB;IAoEnB,OAAO,CAAC,cAAc;CAsBvB;;;;AAGD,wBAEE"}
@@ -0,0 +1,97 @@
1
+ /**
2
+ * Usage tracking and analytics utilities
3
+ * Equivalent to Python's utils/usage/usage.py
4
+ */
5
+ export class UsageTracker {
6
+ constructor() {
7
+ this.metrics = [];
8
+ }
9
+ recordUsage(metrics) {
10
+ const usage = {
11
+ ...metrics,
12
+ timestamp: new Date(),
13
+ };
14
+ this.metrics.push(usage);
15
+ }
16
+ getUsageAggregation(organizationId, period, startTime, endTime) {
17
+ const now = new Date();
18
+ const start = startTime || this.getPeriodStart(period, now);
19
+ const end = endTime || now;
20
+ const relevantMetrics = this.metrics.filter(m => m.organization_id === organizationId &&
21
+ m.timestamp >= start &&
22
+ m.timestamp <= end);
23
+ const totalRequests = relevantMetrics.length;
24
+ const successfulRequests = relevantMetrics.filter(m => m.success).length;
25
+ const failedRequests = totalRequests - successfulRequests;
26
+ const totalTokens = relevantMetrics.reduce((sum, m) => sum + m.tokens_used.total, 0);
27
+ const totalCost = relevantMetrics.reduce((sum, m) => sum + m.cost_usd, 0);
28
+ const averageLatency = totalRequests > 0 ?
29
+ relevantMetrics.reduce((sum, m) => sum + m.latency_ms, 0) / totalRequests : 0;
30
+ const operationsByType = {};
31
+ relevantMetrics.forEach(m => {
32
+ operationsByType[m.operation_type] = (operationsByType[m.operation_type] || 0) + 1;
33
+ });
34
+ const modelsByUsage = {};
35
+ relevantMetrics.forEach(m => {
36
+ if (!modelsByUsage[m.model_used]) {
37
+ modelsByUsage[m.model_used] = { requests: 0, tokens: 0, cost: 0 };
38
+ }
39
+ modelsByUsage[m.model_used].requests++;
40
+ modelsByUsage[m.model_used].tokens += m.tokens_used.total;
41
+ modelsByUsage[m.model_used].cost += m.cost_usd;
42
+ });
43
+ const providersByUsage = {};
44
+ relevantMetrics.forEach(m => {
45
+ if (!providersByUsage[m.provider]) {
46
+ providersByUsage[m.provider] = { requests: 0, tokens: 0, cost: 0 };
47
+ }
48
+ providersByUsage[m.provider].requests++;
49
+ providersByUsage[m.provider].tokens += m.tokens_used.total;
50
+ providersByUsage[m.provider].cost += m.cost_usd;
51
+ });
52
+ const errorBreakdown = {};
53
+ relevantMetrics.filter(m => !m.success).forEach(m => {
54
+ const errorType = m.error_type || 'unknown';
55
+ errorBreakdown[errorType] = (errorBreakdown[errorType] || 0) + 1;
56
+ });
57
+ return {
58
+ period,
59
+ start_time: start,
60
+ end_time: end,
61
+ total_requests: totalRequests,
62
+ successful_requests: successfulRequests,
63
+ failed_requests: failedRequests,
64
+ total_tokens: totalTokens,
65
+ total_cost_usd: totalCost,
66
+ average_latency_ms: averageLatency,
67
+ operations_by_type: operationsByType,
68
+ models_by_usage: modelsByUsage,
69
+ providers_by_usage: providersByUsage,
70
+ error_breakdown: errorBreakdown,
71
+ };
72
+ }
73
+ getPeriodStart(period, from) {
74
+ const start = new Date(from);
75
+ switch (period) {
76
+ case 'hour':
77
+ start.setMinutes(0, 0, 0);
78
+ break;
79
+ case 'day':
80
+ start.setHours(0, 0, 0, 0);
81
+ break;
82
+ case 'week':
83
+ start.setDate(start.getDate() - start.getDay());
84
+ start.setHours(0, 0, 0, 0);
85
+ break;
86
+ case 'month':
87
+ start.setDate(1);
88
+ start.setHours(0, 0, 0, 0);
89
+ break;
90
+ }
91
+ return start;
92
+ }
93
+ }
94
+ // Export class but not types in default export
95
+ export default {
96
+ UsageTracker,
97
+ };
@@ -0,0 +1,59 @@
1
+ /**
2
+ * Webhook secret management utilities for secure webhook verification
3
+ * Equivalent to Python's webhook_secrets.py
4
+ */
5
+ export interface WebhookConfig {
6
+ secret: string;
7
+ tolerance?: number;
8
+ encoding?: 'hex' | 'base64';
9
+ }
10
+ export interface WebhookHeaders {
11
+ signature?: string;
12
+ timestamp?: string;
13
+ [key: string]: string | undefined;
14
+ }
15
+ /**
16
+ * Generate webhook signature for payload
17
+ */
18
+ export declare function generateSignature(payload: string | Buffer, secret: string, timestamp?: number, encoding?: 'hex' | 'base64'): string;
19
+ /**
20
+ * Verify webhook signature
21
+ */
22
+ export declare function verifySignature(payload: string | Buffer, signature: string, secret: string, timestamp?: number, config?: Partial<WebhookConfig>): boolean;
23
+ /**
24
+ * Parse webhook headers for signature and timestamp
25
+ */
26
+ export declare function parseWebhookHeaders(headers: WebhookHeaders): {
27
+ signature?: string;
28
+ timestamp?: number;
29
+ };
30
+ /**
31
+ * Verify webhook request
32
+ */
33
+ export declare function verifyWebhookRequest(payload: string | Buffer, headers: WebhookHeaders, secret: string, config?: Partial<WebhookConfig>): boolean;
34
+ /**
35
+ * Generate webhook secret
36
+ */
37
+ export declare function generateWebhookSecret(length?: number): string;
38
+ /**
39
+ * Create webhook signature header
40
+ */
41
+ export declare function createSignatureHeader(payload: string | Buffer, secret: string, encoding?: 'hex' | 'base64'): {
42
+ signature: string;
43
+ timestamp: number;
44
+ };
45
+ /**
46
+ * Webhook middleware for Express-like frameworks
47
+ */
48
+ export declare function webhookMiddleware(secret: string, config?: Partial<WebhookConfig>): (req: any, res: any, next: any) => any;
49
+ declare const _default: {
50
+ generateSignature: typeof generateSignature;
51
+ verifySignature: typeof verifySignature;
52
+ parseWebhookHeaders: typeof parseWebhookHeaders;
53
+ verifyWebhookRequest: typeof verifyWebhookRequest;
54
+ generateWebhookSecret: typeof generateWebhookSecret;
55
+ createSignatureHeader: typeof createSignatureHeader;
56
+ webhookMiddleware: typeof webhookMiddleware;
57
+ };
58
+ export default _default;
59
+ //# sourceMappingURL=webhook_secrets.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"webhook_secrets.d.ts","sourceRoot":"","sources":["../../src/utils/webhook_secrets.ts"],"names":[],"mappings":"AAEA;;;GAGG;AAEH,MAAM,WAAW,aAAa;IAC5B,MAAM,EAAE,MAAM,CAAC;IACf,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,QAAQ,CAAC,EAAE,KAAK,GAAG,QAAQ,CAAC;CAC7B;AAED,MAAM,WAAW,cAAc;IAC7B,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CAAC;CACnC;AAED;;GAEG;AACH,wBAAgB,iBAAiB,CAC/B,OAAO,EAAE,MAAM,GAAG,MAAM,EACxB,MAAM,EAAE,MAAM,EACd,SAAS,CAAC,EAAE,MAAM,EAClB,QAAQ,GAAE,KAAK,GAAG,QAAgB,GACjC,MAAM,CASR;AAED;;GAEG;AACH,wBAAgB,eAAe,CAC7B,OAAO,EAAE,MAAM,GAAG,MAAM,EACxB,SAAS,EAAE,MAAM,EACjB,MAAM,EAAE,MAAM,EACd,SAAS,CAAC,EAAE,MAAM,EAClB,MAAM,GAAE,OAAO,CAAC,aAAa,CAAM,GAClC,OAAO,CA4BT;AAED;;GAEG;AACH,wBAAgB,mBAAmB,CAAC,OAAO,EAAE,cAAc,GAAG;IAC5D,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB,CAYA;AAED;;GAEG;AACH,wBAAgB,oBAAoB,CAClC,OAAO,EAAE,MAAM,GAAG,MAAM,EACxB,OAAO,EAAE,cAAc,EACvB,MAAM,EAAE,MAAM,EACd,MAAM,GAAE,OAAO,CAAC,aAAa,CAAM,GAClC,OAAO,CAQT;AAED;;GAEG;AACH,wBAAgB,qBAAqB,CAAC,MAAM,GAAE,MAAW,GAAG,MAAM,CAMjE;AAED;;GAEG;AACH,wBAAgB,qBAAqB,CACnC,OAAO,EAAE,MAAM,GAAG,MAAM,EACxB,MAAM,EAAE,MAAM,EACd,QAAQ,GAAE,KAAK,GAAG,QAAgB,GACjC;IAAE,SAAS,EAAE,MAAM,CAAC;IAAC,SAAS,EAAE,MAAM,CAAA;CAAE,CAK1C;AAED;;GAEG;AACH,wBAAgB,iBAAiB,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,GAAE,OAAO,CAAC,aAAa,CAAM,IAC3E,KAAK,GAAG,EAAE,KAAK,GAAG,EAAE,MAAM,GAAG,SActC;;;;;;;;;;AAED,wBAQE"}