@isdk/ai-tool 0.0.7 → 0.0.8

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 (82) hide show
  1. package/dist/chunk-B7IE5ATQ.mjs +1 -0
  2. package/dist/funcs.d.mts +1 -1
  3. package/dist/funcs.d.ts +1 -1
  4. package/dist/funcs.js +1 -1
  5. package/dist/funcs.mjs +1 -1
  6. package/dist/{index-japi6bEq.d.mts → index-Dgo3ZXPQ.d.mts} +28 -2
  7. package/dist/{index-japi6bEq.d.ts → index-Dgo3ZXPQ.d.ts} +28 -2
  8. package/dist/index.d.mts +361 -52
  9. package/dist/index.d.ts +361 -52
  10. package/dist/index.js +1 -1
  11. package/dist/index.mjs +1 -1
  12. package/docs/classes/AbortError.md +8 -8
  13. package/docs/classes/AlreadyExistsError.md +8 -8
  14. package/docs/classes/BaseError.md +8 -8
  15. package/docs/classes/CancelableAbility.md +431 -0
  16. package/docs/classes/ClientTools.md +101 -51
  17. package/docs/classes/CommonError.md +8 -8
  18. package/docs/classes/EventClient.md +123 -73
  19. package/docs/classes/EventServer.md +121 -71
  20. package/docs/classes/EventToolFunc.md +91 -41
  21. package/docs/classes/FStringPromptTemplate.md +18 -18
  22. package/docs/classes/FewShotPromptTemplate.md +22 -22
  23. package/docs/classes/GolangPromptTemplate.md +18 -18
  24. package/docs/classes/HFEnvironment.md +9 -9
  25. package/docs/classes/HFInterpreter.md +17 -17
  26. package/docs/classes/HFTemplate.md +4 -4
  27. package/docs/classes/HfPromptTemplate.md +18 -18
  28. package/docs/classes/IntSet.md +249 -0
  29. package/docs/classes/NotFoundError.md +8 -8
  30. package/docs/classes/NotImplementationError.md +8 -8
  31. package/docs/classes/PromptExampleSelector.md +8 -8
  32. package/docs/classes/PromptTemplate.md +17 -17
  33. package/docs/classes/ReadableStreamError.md +8 -8
  34. package/docs/classes/ResClientTools.md +107 -57
  35. package/docs/classes/ResServerTools.md +105 -55
  36. package/docs/classes/SSEChannel.md +15 -15
  37. package/docs/classes/Semaphore.md +273 -0
  38. package/docs/classes/ServerTools.md +98 -48
  39. package/docs/classes/TaskAbortController.md +157 -0
  40. package/docs/classes/ToolFunc.md +80 -38
  41. package/docs/enums/AsyncFeatureBits.md +41 -0
  42. package/docs/enums/AsyncFeatures.md +41 -0
  43. package/docs/enums/ErrorCode.md +41 -19
  44. package/docs/enums/XXHashAlgorithm.md +2 -2
  45. package/docs/interfaces/AIChatAssistantMessageParam.md +6 -6
  46. package/docs/interfaces/AIChatContentPartImage.md +2 -2
  47. package/docs/interfaces/AIChatContentPartText.md +2 -2
  48. package/docs/interfaces/AIChatMessageParamBase.md +1 -1
  49. package/docs/interfaces/AIChatMessageToolCall.md +3 -3
  50. package/docs/interfaces/AIChatSystemMessageParam.md +3 -3
  51. package/docs/interfaces/AIChatToolMessageParam.md +4 -4
  52. package/docs/interfaces/AIChatUserMessageParam.md +7 -7
  53. package/docs/interfaces/AIResult.md +16 -5
  54. package/docs/interfaces/AIStreamParser.md +1 -1
  55. package/docs/interfaces/AIStreamParserOptions.md +1 -1
  56. package/docs/interfaces/BaseFunc.md +7 -7
  57. package/docs/interfaces/BaseFuncItem.md +6 -6
  58. package/docs/interfaces/CancelableAbilityOptions.md +126 -0
  59. package/docs/interfaces/ClientFuncItem.md +10 -10
  60. package/docs/interfaces/DotenvExpandOptions.md +3 -3
  61. package/docs/interfaces/DotenvExpandOutput.md +2 -2
  62. package/docs/interfaces/EventClientFuncParams.md +4 -4
  63. package/docs/interfaces/EventServerFuncParams.md +5 -5
  64. package/docs/interfaces/FewShotPromptTemplateOptions.md +11 -11
  65. package/docs/interfaces/FuncItem.md +7 -7
  66. package/docs/interfaces/FuncParam.md +16 -5
  67. package/docs/interfaces/PromptExampleSelectorOptions.md +2 -2
  68. package/docs/interfaces/PromptTemplateOptions.md +6 -6
  69. package/docs/interfaces/RemoteFuncItem.md +10 -10
  70. package/docs/interfaces/ResClientFuncParams.md +2 -2
  71. package/docs/interfaces/ResServerFuncParams.md +5 -5
  72. package/docs/interfaces/SanitizeFilenameOptions.md +2 -2
  73. package/docs/interfaces/ServerFuncItem.md +11 -11
  74. package/docs/interfaces/ServerFuncParams.md +2 -2
  75. package/docs/interfaces/StreamCallbacksAndOptions.md +124 -0
  76. package/docs/interfaces/TaskAbortControllers.md +7 -0
  77. package/docs/interfaces/TaskPromise.md +151 -0
  78. package/docs/interfaces/ToolFuncPackage.md +63 -0
  79. package/docs/modules.md +1124 -120
  80. package/package.json +3 -2
  81. package/dist/chunk-PYM7DCYB.mjs +0 -1
  82. package/docs/interfaces/AIStreamCallbacksAndOptions.md +0 -158
package/dist/index.d.ts CHANGED
@@ -1,7 +1,9 @@
1
- import { B as BaseError, A as AIModelNameRules } from './index-japi6bEq.js';
2
- export { d as AIModelNameRule, c as AIModelNameRuleFn, m as AbortError, i as AbortErrorCode, b as ActionName, a as ActionNames, l as AlreadyExistsError, h as AlreadyExistsErrorCode, u as BaseFunc, r as BaseFuncItem, y as ClientFuncItem, D as ClientToolFuncSchema, z as ClientTools, C as CommonError, f as ErrorCode, E as ErrorCodeType, W as EventClient, V as EventClientFuncParams, Z as EventServer, Y as EventServerFuncParams, M as EventToolFunc, s as FuncItem, p as FuncParam, F as FuncParamType, q as FuncParams, v as Funcs, I as InternalErrorCode, k as NotFoundError, g as NotFoundErrorCode, j as NotImplementationError, N as NotImplementedErrorCode, P as PASSING_SCORE, e as RemoteFuncItem, R as RemoteToolFuncSchema, a0 as ResClientFuncParams, a1 as ResClientTools, a2 as ResClientToolsSchema, a3 as ResServerFuncParams, a4 as ResServerTools, a5 as ResServerToolsSchema, o as SSEChannel, S as SSEChannelAlreadyClosedErrCode, H as ServerFuncItem, G as ServerFuncParams, K as ServerToolFuncSchema, J as ServerTools, T as TFunc, w as ToolFunc, x as ToolFuncSchema, _ as _lrucache, n as createError, Q as createLRUCache, O as event, X as eventClient, $ as eventServer, U as lrucache, L as registerCoreTools, t as throwError } from './index-japi6bEq.js';
3
- import { BaseFactory } from 'custom-factory';
4
1
  import * as custom_ability from 'custom-ability';
2
+ import { AbilityOptions } from 'custom-ability';
3
+ export { createAbilityInjector } from 'custom-ability';
4
+ import { A as AsyncFeatures, C as CommonError, T as ToolFunc, B as BaseError, a as AIModelNameRules } from './index-Dgo3ZXPQ.js';
5
+ export { e as AIModelNameRule, d as AIModelNameRuleFn, n as AbortError, j as AbortErrorCode, c as ActionName, b as ActionNames, m as AlreadyExistsError, i as AlreadyExistsErrorCode, u as AsyncFeatureBits, D as BaseFunc, y as BaseFuncItem, K as ClientFuncItem, M as ClientToolFuncSchema, L as ClientTools, g as ErrorCode, E as ErrorCodeType, a1 as EventClient, a0 as EventClientFuncParams, a4 as EventServer, a3 as EventServerFuncParams, X as EventToolFunc, z as FuncItem, v as FuncParam, F as FuncParamType, w as FuncParams, G as Funcs, I as InternalErrorCode, l as NotFoundError, h as NotFoundErrorCode, k as NotImplementationError, N as NotImplementedErrorCode, P as PASSING_SCORE, f as RemoteFuncItem, R as RemoteToolFuncSchema, a6 as ResClientFuncParams, a7 as ResClientTools, a8 as ResClientToolsSchema, a9 as ResServerFuncParams, aa as ResServerTools, ab as ResServerToolsSchema, p as SSEChannel, S as SSEChannelAlreadyClosedErrCode, Q as ServerFuncItem, O as ServerFuncParams, V as ServerToolFuncSchema, U as ServerTools, x as TFunc, r as ToolAsyncCancelableBit, q as ToolAsyncMultiTaskBit, s as ToolAsyncPriorityBit, H as ToolFuncPackage, J as ToolFuncSchema, _ as _lrucache, o as createError, Z as createLRUCache, Y as event, a2 as eventClient, a5 as eventServer, $ as lrucache, W as registerCoreTools, t as throwError } from './index-Dgo3ZXPQ.js';
6
+ import { BaseFactory } from 'custom-factory';
5
7
  import { Event } from 'events-ex';
6
8
  import * as _base32768 from 'base32768';
7
9
  export { NIL as uuidNIL, parse as uuidParse, stringify as uuidStringify, validate as uuidValidate, version as uuidVersion, v1 as uuidv1, v4 as uuidv4, v5 as uuidv5 } from 'uuid';
@@ -9,11 +11,227 @@ export * from 'json-canonicalize';
9
11
  export { stringify as stringifyYaml } from 'yaml';
10
12
  export { mimeType } from 'mime-type/with-db';
11
13
  export { Config as ConfigFile } from 'load-config-file';
14
+ export { formatISO, parse as parseDateFormat, parseISO, toDate } from 'date-fns';
12
15
  import 'property-manager';
13
16
  import 'secondary-cache';
14
17
  import 'abstract-error';
15
18
  import 'http';
16
19
 
20
+ declare const DefaultAsyncSemaphoreCapacity = 32;
21
+ declare class Deque {
22
+ private _capacity;
23
+ private _length;
24
+ private _front;
25
+ private arr;
26
+ constructor(capacity: number);
27
+ push(item: any): number;
28
+ pop(): any;
29
+ shift(): any;
30
+ get length(): number;
31
+ private checkCapacity;
32
+ private resizeTo;
33
+ }
34
+ /**
35
+ * A Semaphore implementation for managing concurrency in asynchronous operations.
36
+ * Semaphores allow a fixed number of resources to be accessed concurrently.
37
+ *
38
+ * Example usage:
39
+ *
40
+ * ```typescript
41
+ * const semaphore = new Semaphore(5); // Allows 5 concurrent operations.
42
+ *
43
+ * const semaphore = new Semaphore(
44
+ * 4, // Allow 4 concurrent async calls
45
+ * {
46
+ * capacity: 100 // Prealloc space for 100 tokens
47
+ * }
48
+ * );
49
+ *
50
+ * async function fetchData(x) {
51
+ * await semaphore.acquire()
52
+ * try {
53
+ * console.log(semaphore.pendingCount() + ' calls to fetch are waiting')
54
+ * // ... do some async stuff with x
55
+ * } finally {
56
+ * semaphore.release();
57
+ * }
58
+ * }
59
+ *
60
+ * const data = await Promise.all(array.map(fetchData));
61
+ * ```
62
+ */
63
+ declare class Semaphore {
64
+ readonly maxConcurrency: number;
65
+ readonly waiting: Deque;
66
+ private free;
67
+ private releaseEmitter;
68
+ private useDefaultTokens;
69
+ private pauseFn?;
70
+ private resumeFn?;
71
+ private paused;
72
+ /**
73
+ * Creates a semaphore object. The first argument is the maximum concurrently number and the second argument is optional.
74
+ *
75
+ * @param maxConcurrency The maximum number of callers allowed to acquire the semaphore concurrently.
76
+ * @param options.initFn Function that is used to initialize the tokens used to manage the semaphore. The default is () => '1'.
77
+ * @param options.pauseFn An optional fuction that is called to opportunistically request pausing the the incoming stream of data,
78
+ * instead of piling up waiting promises and possibly running out of memory. See examples/pausing.js.
79
+ * @param options.resumeFn An optional function that is called when there is room again to accept new waiters on the semaphore.
80
+ * This function must be declared if a pauseFn is declared.
81
+ * @param options.capacity Sets the size of the preallocated waiting list inside the semaphore.
82
+ * This is typically used by high performance where the developer can make a rough estimate of the number of concurrent users of a semaphore.
83
+ *
84
+ * ```ts
85
+ * const readline = require('readline');
86
+ *
87
+ * const rl = readline.createInterface({
88
+ * input: process.stdin,
89
+ * output: process.stdout,
90
+ * terminal: false
91
+ * });
92
+ *
93
+ * function pause() {
94
+ * console.log('Pausing the stream');
95
+ * rl.pause();
96
+ * }
97
+ *
98
+ * function resume() {
99
+ * console.log('Resuming the stream');
100
+ * rl.resume();
101
+ * }
102
+ *
103
+ * const s = new Semaphore(5, { pauseFn: pause, resumeFn: resume });
104
+ *
105
+ * async function parse(line) {
106
+ * await s.acquire();
107
+ *
108
+ * console.log(line);
109
+ *
110
+ * s.release();
111
+ * }
112
+ *
113
+ * rl.on('line', line => {
114
+ * parse(line).catch(console.error);
115
+ * });
116
+ * ```
117
+ *
118
+ */
119
+ constructor(maxConcurrency: number, { initFn, pauseFn, resumeFn, capacity, }?: {
120
+ initFn?: () => any;
121
+ pauseFn?: () => void;
122
+ resumeFn?: () => void;
123
+ capacity?: number;
124
+ });
125
+ /**
126
+ * Attempt to acquire a token from the semaphore, if one is available immediately. Otherwise, return undefined.
127
+ */
128
+ tryAcquire(): any | undefined;
129
+ /**
130
+ * Acquire a token from the semaphore, thus decrement the number of available execution slots. If initFn is not used then the return value of the function can be discarded.
131
+ */
132
+ acquire(signal?: AbortSignal): Promise<any>;
133
+ /**
134
+ * Release the semaphore, thus increment the number of free execution slots. If initFn is used then the token returned by acquire() should be given as an argument when calling this function.
135
+ */
136
+ release(token?: any): void;
137
+ /**
138
+ * Drains the semaphore and returns all the initialized tokens in an array. Draining is an ideal way to ensure there are no pending async tasks, for example before a process will terminate.
139
+ */
140
+ drain(): Promise<any[]>;
141
+ /**
142
+ * Returns the number of callers waiting on the semaphore, i.e. the number of pending promises.
143
+ *
144
+ * @returns The number of waiters in the waiting list.
145
+ */
146
+ pendingCount(): number;
147
+ }
148
+ /**
149
+ * Creates a rate limiter function that blocks with a promise whenever the rate limit is hit and resolves the promise once the call rate is within the limit set by rps. The second argument is optional.
150
+ *
151
+ * @param rps
152
+ * @param options.timeUnit The `timeUnit` is an optional argument setting the width of the rate limiting window in milliseconds.
153
+ * The default `timeUnit` is 1000 ms, therefore making the rps argument act as requests per second limit.
154
+ * @param options.uniformDistribution The `uniformDistribution` argument enforces a discrete uniform distribution over time,
155
+ * instead of the default that allows hitting the function rps time and then pausing for timeWindow milliseconds. Setting
156
+ * the `uniformDistribution` option is mainly useful in a situation where the flow of rate limit function calls is continuous
157
+ * and and occuring faster than timeUnit (e.g. reading a file) and not enabling it would cause the maximum number of calls to
158
+ * resolve immediately (thus exhaust the limit immediately) and therefore the next bunch calls would need to wait for timeWindow
159
+ * milliseconds. However if the flow is sparse then this option may make the code run slower with no advantages.
160
+ *
161
+ * Examples:
162
+ *
163
+ * ```ts
164
+ * async function f() {
165
+ * const lim = RateLimit(5); // rps
166
+ *
167
+ * for (let i = 0; i < n; i++) {
168
+ * await lim();
169
+ * // ... do something async
170
+ * }
171
+ * }
172
+ * ```
173
+ *
174
+ *
175
+ */
176
+ declare function RateLimit(rps: number, { timeUnit, uniformDistribution, }?: {
177
+ timeUnit?: number;
178
+ uniformDistribution?: boolean;
179
+ }): () => Promise<void>;
180
+
181
+ type AsyncTaskId = string | number;
182
+ interface CancelableAbilityOptions extends AbilityOptions {
183
+ asyncFeatures?: AsyncFeatures;
184
+ maxTaskConcurrency?: number;
185
+ }
186
+ declare class TaskAbortController extends AbortController {
187
+ id?: AsyncTaskId;
188
+ timeoutId?: any;
189
+ streamController?: ReadableStreamDefaultController;
190
+ parent: CancelableAbility;
191
+ constructor(parent: CancelableAbility);
192
+ abort(reason?: string | Error | CommonError, data?: any): void;
193
+ throwRejected(alreadyRejected?: boolean): true | undefined;
194
+ }
195
+ interface TaskAbortControllers {
196
+ [k: AsyncTaskId]: TaskAbortController | undefined;
197
+ }
198
+ interface TaskPromise<T = any> extends Promise<T> {
199
+ task?: TaskAbortController;
200
+ }
201
+ declare interface CancelableAbility {
202
+ _asyncFeatures?: number;
203
+ _maxTaskConcurrency: number | undefined;
204
+ [name: string]: any;
205
+ }
206
+ declare class CancelableAbility {
207
+ generateAsyncTaskId: (taskId?: AsyncTaskId, aborters?: TaskAbortControllers) => AsyncTaskId;
208
+ cleanMultiTaskAborter: (id: AsyncTaskId, aborters: TaskAbortControllers) => void;
209
+ __task_aborter: TaskAbortController | TaskAbortControllers | undefined;
210
+ __task_semaphore: Semaphore | undefined;
211
+ get maxTaskConcurrency(): number | undefined;
212
+ get semaphore(): Semaphore | undefined;
213
+ isAborted(taskId?: AsyncTaskId): boolean;
214
+ getRunningTask(taskId?: AsyncTaskId): TaskAbortController | undefined;
215
+ getRunningTaskCount(): number;
216
+ _generateAsyncTaskId(taskId?: AsyncTaskId, aborters?: TaskAbortControllers): AsyncTaskId;
217
+ $generateAsyncTaskId(taskId?: AsyncTaskId, aborters?: TaskAbortControllers): AsyncTaskId | undefined;
218
+ createAborter(params?: any, taskId?: AsyncTaskId, raiseError?: boolean): TaskAbortController;
219
+ $cleanMultiTaskAborter(id: AsyncTaskId, aborters: TaskAbortControllers): void;
220
+ cleanTaskAborter(aborter: TaskAbortController): void;
221
+ _cleanMultiTaskAborter(id: AsyncTaskId, aborters: TaskAbortControllers): void;
222
+ createTaskPromise<Output = any>(runTask: (params: Record<string, any>, aborter: TaskAbortController) => Promise<Output>, params: Record<string, any>, options?: {
223
+ taskId?: AsyncTaskId;
224
+ raiseError?: boolean;
225
+ }): TaskPromise<Output>;
226
+ runAsyncCancelableTask<Output = any>(params: Record<string, any> | undefined, runTask: (params: Record<string, any>, aborter: TaskAbortController) => Promise<Output>, options?: {
227
+ taskId?: AsyncTaskId;
228
+ raiseError?: boolean;
229
+ }): TaskPromise<Output>;
230
+ abort(reason?: string, data?: any): void;
231
+ }
232
+ type ToolFuncCancelableFn = (Tool: typeof ToolFunc, options?: CancelableAbilityOptions) => typeof ToolFunc;
233
+ declare const makeToolFuncCancelable: ToolFuncCancelableFn;
234
+
17
235
  declare const AITextGenerationFinishReasons: readonly ["stop", "length", "content-filter", "tool-calls", "abort", "error", "other", null];
18
236
  type AITextGenerationFinishReason = typeof AITextGenerationFinishReasons[number];
19
237
  interface AIResult<TValue = any, TOptions = any> {
@@ -30,6 +248,7 @@ interface AIResult<TValue = any, TOptions = any> {
30
248
  * for stream mode
31
249
  */
32
250
  stop?: boolean;
251
+ taskId?: AsyncTaskId;
33
252
  }
34
253
  declare const AIMessageTypes: readonly ["human", "ai", "generic", "system", "tool"];
35
254
  type AIMessageType = typeof AIMessageTypes[number];
@@ -87,6 +306,7 @@ interface AIChatToolMessageParam extends AIChatMessageParamBase {
87
306
  tool_call_id: string;
88
307
  templateFormat?: string;
89
308
  }
309
+ declare function messagesToText(messages: AIChatMessageParamBase[]): string;
90
310
 
91
311
  /**
92
312
  * Prompt Type
@@ -764,6 +984,8 @@ type EventListenerFn = (this: Event, name: string, ...args: any) => any;
764
984
  type EventErrorListenerFn = (this: Event, err: Error, name: string, ...args: any) => any;
765
985
  declare const backendEventable: custom_ability.AbilityFn;
766
986
 
987
+ declare function isWebStream(val: any): boolean;
988
+
767
989
  declare const RStreamErrCode = 600;
768
990
  declare const ResponseRStreamErrCode = 601;
769
991
  declare class ReadableStreamError extends BaseError {
@@ -780,29 +1002,6 @@ declare function getResponseErrorReadableStream(body?: ReadableStream<Uint8Array
780
1002
  */
781
1003
  declare function createEmptyReadableStream(): ReadableStream;
782
1004
 
783
- /**
784
- * Configuration options and helper callback methods for AIStream stream lifecycle events.
785
- * @interface
786
- */
787
- interface AIStreamCallbacksAndOptions {
788
- /** `onStart`: Called once when the stream is initialized. */
789
- onStart?: () => Promise<void> | void;
790
- /** `onCompletion`: Called for each tokenized message. */
791
- onCompletion?: (completion: string) => Promise<void> | void;
792
- /** `onFinal`: Called once when the stream is closed with the final completion message. */
793
- onFinal?: (completion: string) => Promise<void> | void;
794
- /** `onToken`: Called for each tokenized message. */
795
- onToken?: (token: string) => Promise<void> | void;
796
- /** `onText`: Called for each text chunk. */
797
- onText?: (text: string) => Promise<void> | void;
798
- /**
799
- * A flag for enabling the experimental_StreamData class and the new protocol.
800
- * @see https://github.com/vercel-labs/ai/pull/425
801
- *
802
- * When StreamData is rolled out, this will be removed and the new protocol will be used by default.
803
- */
804
- experimental_streamData?: boolean;
805
- }
806
1005
  /**
807
1006
  * Options for the AIStreamParser.
808
1007
  * @interface
@@ -827,29 +1026,6 @@ interface AIStreamParser<T = any, TOptions = any> {
827
1026
  * @returns TransformStream parsing events.
828
1027
  */
829
1028
  declare function createEventStreamTransformer<TValue = any, TOptions = any>(customParser?: AIStreamParser): TransformStream<Uint8Array, AIResult<TValue, TOptions>>;
830
- /**
831
- * Creates a transform stream that encodes input messages and invokes optional callback functions.
832
- * The transform stream uses the provided callbacks to execute custom logic at different stages of the stream's lifecycle.
833
- * - `onStart`: Called once when the stream is initialized.
834
- * - `onToken`: Called for each tokenized message.
835
- * - `onCompletion`: Called every time an AIStream completion message is received. This can occur multiple times when using e.g. OpenAI functions
836
- * - `onFinal`: Called once when the stream is closed with the final completion message.
837
- *
838
- * This function is useful when you want to process a stream of messages and perform specific actions during the stream's lifecycle.
839
- *
840
- * @param {AIStreamCallbacksAndOptions} [callbacks] - An object containing the callback functions.
841
- * @return {TransformStream<string, Uint8Array>} A transform stream that encodes input messages as Uint8Array and allows the execution of custom logic through callbacks.
842
- *
843
- * @example
844
- * const callbacks = {
845
- * onStart: async () => console.log('Stream started'),
846
- * onToken: async (token) => console.log(`Token: ${token}`),
847
- * onCompletion: async (completion) => console.log(`Completion: ${completion}`)
848
- * onFinal: async () => data.close()
849
- * };
850
- * const transformer = createCallbacksTransformer(callbacks);
851
- */
852
- declare function createCallbacksTransformer(cb: AIStreamCallbacksAndOptions | undefined): TransformStream<AIResult, Uint8Array>;
853
1029
  /**
854
1030
  * Returns a stateful function that, when invoked, trims leading whitespace
855
1031
  * from the input text. The trimming only occurs on the first invocation, ensuring that
@@ -891,6 +1067,42 @@ declare function AIStream<T = any, TOptions = any>(response: Response, customPar
891
1067
  */
892
1068
  declare function readableFromAsyncIterable<T>(iterable: AsyncIterable<T>): ReadableStream<T>;
893
1069
 
1070
+ /**
1071
+ * Configuration options and helper callback methods for stream lifecycle events.
1072
+ * @interface
1073
+ */
1074
+ interface StreamCallbacksAndOptions<I = any, O = any> {
1075
+ /** `onStart`: Called once when the stream is initialized. */
1076
+ onStart?: (controller: TransformStreamDefaultController) => Promise<void> | void;
1077
+ /** `onToken`: Called for each tokenized message. */
1078
+ onTransform?: (chunk: I, controller: TransformStreamDefaultController) => Promise<O | void> | O | void;
1079
+ /** `onFinal`: Called once when the stream is closed with the final completion message. */
1080
+ onFinal?: (controller: TransformStreamDefaultController) => Promise<void> | void;
1081
+ onError?: (error: Error) => Promise<void> | void;
1082
+ }
1083
+ /**
1084
+ * Creates a transform stream that invokes optional callback functions.
1085
+ * The transform stream uses the provided callbacks to execute custom logic at different stages of the stream's lifecycle.
1086
+ * - `onStart`: Called once when the stream is initialized.
1087
+ * - `onTransform`: Called for each tokenized message.
1088
+ * - `onCompletion`: Called every time an AIStream completion message is received. This can occur multiple times when using e.g. OpenAI functions
1089
+ * - `onFinal`: Called once when the stream is closed with the final completion message.
1090
+ *
1091
+ * This function is useful when you want to process a stream of messages and perform specific actions during the stream's lifecycle.
1092
+ *
1093
+ * @param [callbacks] - An object containing the callback functions.
1094
+ * @return A transform stream that encodes input messages as Uint8Array and allows the execution of custom logic through callbacks.
1095
+ *
1096
+ * @example
1097
+ * const callbacks = {
1098
+ * onStart: async () => console.log('Stream started'),
1099
+ * onTransform: async (chunk) => console.log('Token:',chunk),
1100
+ * onFinal: async () => data.close()
1101
+ * };
1102
+ * const transformer = createCallbacksTransformer(callbacks);
1103
+ */
1104
+ declare function createCallbacksTransformer<I = any, O = any>(cb: StreamCallbacksAndOptions<I, O> | undefined): TransformStream<I, O>;
1105
+
894
1106
  declare function uuid(ver?: 1 | 4 | 5, encode?: boolean): string;
895
1107
 
896
1108
  declare const base32768: typeof _base32768;
@@ -1044,7 +1256,7 @@ declare function parseJsJson(input: string, scope?: Record<string, any>): any;
1044
1256
  */
1045
1257
  declare function getKeysPath<TValue extends object>(value: TValue): string[];
1046
1258
 
1047
- declare function createEndWithRepetitionDetector(repetitionThreshold: number): (value: string) => boolean;
1259
+ declare function createEndWithRepetitionDetector(repetitionThreshold: number): (value: string) => RegExpExecArray | null;
1048
1260
 
1049
1261
  /**
1050
1262
  * Loads a file from given paths, optionally searching for specific extensions.
@@ -1064,11 +1276,40 @@ declare function createEndWithRepetitionDetector(repetitionThreshold: number): (
1064
1276
  */
1065
1277
  declare function loadFileFromPaths(filename: string, searchPaths?: string[], extNames?: string[], options?: {
1066
1278
  filepath?: string;
1279
+ exclude?: string[] | string;
1067
1280
  }): Buffer;
1068
1281
  declare function loadTextFromPaths(filename: string, searchPaths?: string[], extNames?: string[], options?: {
1069
1282
  encoding?: BufferEncoding;
1070
1283
  filepath?: string;
1071
- } | BufferEncoding): string;
1284
+ } | BufferEncoding, exclude?: string[] | string): string;
1285
+ /**
1286
+ * Recursively reads all filenames in the given directory or directories.
1287
+ * Optionally filters the results using a file matching callback.
1288
+ *
1289
+ * @param dir - A single directory path or an array of directory paths to start the search from.
1290
+ * @param isFileMatched - An optional callback that determines whether a file should be included in the result.
1291
+ * @returns An array of file paths that match the criteria.
1292
+ *
1293
+ * @example
1294
+ * ```typescript
1295
+ * const files = readFilenamesRecursiveSync('/path/to/directory', (filepath) => filepath.endsWith('.js'));
1296
+ * console.log(files); // Outputs an array of JavaScript file paths.
1297
+ * ```
1298
+ */
1299
+ declare function readFilenamesRecursiveSync(dir: string | string[], isFileMatched?: (filepath: string) => boolean): string[];
1300
+ /**
1301
+ * Resolves the real file path, handling symbolic links.
1302
+ *
1303
+ * @param filepath - The file path to resolve.
1304
+ * @returns The real file path.
1305
+ *
1306
+ * @example
1307
+ * ```typescript
1308
+ * const realPath = getRealFilepath('/path/to/symbolic/link');
1309
+ * console.log(realPath); // Outputs the resolved path.
1310
+ * ```
1311
+ */
1312
+ declare function getRealFilepath(filepath: string): string;
1072
1313
 
1073
1314
  declare function registerYamlTag(tags: any): void;
1074
1315
  declare function parseYaml(content: string): any;
@@ -1130,4 +1371,72 @@ declare function jsonFilterToWhere(filter: JsonFilter | JsonFilter[], wrapKey?:
1130
1371
  */
1131
1372
  declare function matchUrlProtocol(name: string): string | undefined;
1132
1373
 
1133
- export { type AIChatAssistantMessageParam, type AIChatContentPart, type AIChatContentPartImage, type AIChatContentPartText, type AIChatMessageParam, type AIChatMessageParamBase, type AIChatMessageToolCall, type AIChatRole, AIChatRoles, type AIChatSystemMessageParam, type AIChatToolMessageParam, type AIChatUserMessageParam, type AIMessageType, AIMessageTypes, AIModelNameRules, type AIResult, AIStream, type AIStreamCallbacksAndOptions, type AIStreamParser, type AIStreamParserOptions, type AITextGenerationFinishReason, AITextGenerationFinishReasons, BaseError, type DotenvExpandOptions, type DotenvExpandOutput, type DotenvParseInput, type DotenvParseOutput, type DotenvPopulateInput, EventBusName, type EventErrorListenerFn, type EventListenerFn, EventName, FStringPromptTemplate, type FStringPromptTemplateNode, FewShotPromptTemplate, type FewShotPromptTemplateOptions, FilenameReservedRegex, GolangPromptTemplate, builtins as HFBuiltins, EnvironmentEx as HFEnvironment, Interpreter as HFInterpreter, Template as HFTemplate, HfPromptTemplate, type JsonFilter, PromptExampleSelector, type PromptExampleSelectorOptions, type PromptExamples, PromptTemplate, type PromptTemplateOptions, type PromptTemplateType, PromptTemplateTypes, type PromptType, PromptTypes, RStreamErrCode, ReadableStreamError, ResponseRStreamErrCode, type SanitizeFilenameOptions, WindowsReservedNameRegex, XXHashAlgorithm, backendEventable, base32768, createCallbacksTransformer, createEmptyReadableStream, createEndWithRepetitionDetector, createEventStreamTransformer, createHfValueFunc, defaultTemplateFormat, expandEnv, expandObjEnv, extNameLevel, filenameReservedRegex, getAllEnumKeys, getConfigFileNames, getConfigs, getKeysPath, getMultiLevelExtname, getResponseErrorReadableStream, parse as hfParse, tokenize as hfTokenize, interpolateEnv, interpolateFString, interpolateGolangTemplate, isModelNameMatched, isValidFilename, isValidFilepath, jsonFilterToWhere, loadFileFromPaths, loadTextFromPaths, matchUrlProtocol, parseFString, parseJsJson, parseYaml, reControlCharsRegex, readableFromAsyncIterable, registerYamlTag, sanitizeFilename, sanitizeFilepath, saveConfigFile, sortedValues, splitSentence, trimStartOfStreamHelper, truncTo, uuid, wait, xxhash, xxhash32, xxhash64, xxhashAsStr };
1374
+ /**
1375
+ * Formats a parameter size in bytes into a string with an appropriate scale (T, B, M, or K),
1376
+ * optionally specifying the number of fractional digits to display.
1377
+ *
1378
+ * @param paramsSize The parameter size in bytes.
1379
+ * @param fractionDigits The number of fractional digits to include in the formatted output. Default is 0 (no fractional digits).
1380
+ * @returns A string representing the formatted parameter size with the appropriate scale unit and the specified number of fractional digits.
1381
+ */
1382
+ declare function paramsSizeToScaleStr(paramsSize: number, fractionDigits?: number): string;
1383
+ /**
1384
+ * Converts a formatted parameters size string (with units T, B, M, or K) back to its byte value.
1385
+ *
1386
+ * @param scale The formatted parameter size string (e.g., "1.23B", "456M", "789K", or "123").
1387
+ * @returns The parameter size in bytes.
1388
+ */
1389
+ declare function scaleStrToParamsSize(scale: string): number;
1390
+
1391
+ declare function jsonToMarkdownStr(mem: any, options?: {
1392
+ level?: number;
1393
+ visiting?: Set<any>;
1394
+ objectTag?: string;
1395
+ arrayTag?: string;
1396
+ }): string;
1397
+
1398
+ declare function toDateTime(date: Date | string | number): Date | undefined;
1399
+ declare function textToDate(date: string, format?: string): Date;
1400
+ declare function dateToText(date: Date, format?: string): string;
1401
+
1402
+ /**
1403
+ * Represents a set of integers using a bit field.
1404
+ * Each bit in the bit field represents an integer starting from 0,
1405
+ * where the flag value 0 represents the 0th bit, 1 represents the 1st bit, and so on.
1406
+ */
1407
+ declare class IntSet {
1408
+ private bitField;
1409
+ static has(bitField: number, flag: number): boolean;
1410
+ static add(bitField: number, flag: number): number;
1411
+ static delete(bitField: number, flag: number): number;
1412
+ constructor(bitField?: number);
1413
+ /**
1414
+ * Adds an element to the set.
1415
+ *
1416
+ * @param flag - The flag value representing the bit position to set.
1417
+ * Note: the flag value 0 represents the 0th bit, and so on.
1418
+ */
1419
+ add(flag: number): this;
1420
+ /**
1421
+ * Removes an element from the set.
1422
+ *
1423
+ * @param flag - The flag value representing the bit position to set. 0 represents the 0th bit
1424
+ */
1425
+ delete(flag: number): this;
1426
+ /**
1427
+ * Determines whether an element is in the set.
1428
+ *
1429
+ * @param flag - The flag value representing the bit position to set. 0 represents the 0th bit
1430
+ * @returns true if the element is in the set; otherwise, false.
1431
+ */
1432
+ has(flag: number): boolean;
1433
+ /**
1434
+ * Clears all elements from the set.
1435
+ */
1436
+ clear(): this;
1437
+ valueOf(): number;
1438
+ toString(): string;
1439
+ toJSON(): number;
1440
+ }
1441
+
1442
+ export { type AIChatAssistantMessageParam, type AIChatContentPart, type AIChatContentPartImage, type AIChatContentPartText, type AIChatMessageParam, type AIChatMessageParamBase, type AIChatMessageToolCall, type AIChatRole, AIChatRoles, type AIChatSystemMessageParam, type AIChatToolMessageParam, type AIChatUserMessageParam, type AIMessageType, AIMessageTypes, AIModelNameRules, type AIResult, AIStream, type AIStreamParser, type AIStreamParserOptions, type AITextGenerationFinishReason, AITextGenerationFinishReasons, AsyncFeatures, type AsyncTaskId, BaseError, CancelableAbility, type CancelableAbilityOptions, CommonError, DefaultAsyncSemaphoreCapacity, type DotenvExpandOptions, type DotenvExpandOutput, type DotenvParseInput, type DotenvParseOutput, type DotenvPopulateInput, EventBusName, type EventErrorListenerFn, type EventListenerFn, EventName, FStringPromptTemplate, type FStringPromptTemplateNode, FewShotPromptTemplate, type FewShotPromptTemplateOptions, FilenameReservedRegex, GolangPromptTemplate, builtins as HFBuiltins, EnvironmentEx as HFEnvironment, Interpreter as HFInterpreter, Template as HFTemplate, HfPromptTemplate, IntSet, type JsonFilter, PromptExampleSelector, type PromptExampleSelectorOptions, type PromptExamples, PromptTemplate, type PromptTemplateOptions, type PromptTemplateType, PromptTemplateTypes, type PromptType, PromptTypes, RStreamErrCode, RateLimit, ReadableStreamError, ResponseRStreamErrCode, type SanitizeFilenameOptions, Semaphore, type StreamCallbacksAndOptions, TaskAbortController, type TaskAbortControllers, type TaskPromise, ToolFunc, WindowsReservedNameRegex, XXHashAlgorithm, backendEventable, base32768, createCallbacksTransformer, createEmptyReadableStream, createEndWithRepetitionDetector, createEventStreamTransformer, createHfValueFunc, dateToText, defaultTemplateFormat, expandEnv, expandObjEnv, extNameLevel, filenameReservedRegex, getAllEnumKeys, getConfigFileNames, getConfigs, getKeysPath, getMultiLevelExtname, getRealFilepath, getResponseErrorReadableStream, parse as hfParse, tokenize as hfTokenize, interpolateEnv, interpolateFString, interpolateGolangTemplate, isModelNameMatched, isValidFilename, isValidFilepath, isWebStream, jsonFilterToWhere, jsonToMarkdownStr, loadFileFromPaths, loadTextFromPaths, makeToolFuncCancelable, matchUrlProtocol, messagesToText, paramsSizeToScaleStr, parseFString, parseJsJson, parseYaml, reControlCharsRegex, readFilenamesRecursiveSync, readableFromAsyncIterable, registerYamlTag, sanitizeFilename, sanitizeFilepath, saveConfigFile, scaleStrToParamsSize, sortedValues, splitSentence, textToDate, toDateTime, trimStartOfStreamHelper, truncTo, uuid, wait, xxhash, xxhash32, xxhash64, xxhashAsStr };