@agentica/vector-selector 0.43.3 → 0.44.0-dev.20260313-2

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.
package/src/utils.ts CHANGED
@@ -1,123 +1,123 @@
1
- import type { AgenticaContext } from "@agentica/core";
2
- import type { GreaterThan, Integer } from "type-fest";
3
-
4
- import { sha256 } from "@noble/hashes/sha2";
5
- import { utf8ToBytes } from "@noble/hashes/utils";
6
-
7
- /** type function to check if a number is greater than 0 */
8
- type GreaterThanZeroInteger<T extends number> = GreaterThan<Integer<T>, 0> extends true ? T : never;
9
-
10
- /**
11
- * This function is used to get a retry function.
12
- *
13
- * It will throw an error if the count is not a number,
14
- * or if the count is not a finite number,
15
- * or if the count is not an integer,
16
- * or if the count is less than 1.
17
- *
18
- * @param count - The number of times to retry the function.
19
- * @returns A retry function.
20
- * @throws {TypeError} If the count is not a number, or if the count is not a finite number, or if the count is not an integer, or if the count is less than 1.
21
- * @throws {Error} If the function fails to return a value after the specified number of retries.
22
- */
23
- export function getRetry<TCount extends number>(count: GreaterThanZeroInteger<TCount>) {
24
- if (count < 1) {
25
- throw new Error("count should be greater than 0");
26
- }
27
-
28
- return async <T>(fn: () => Promise<T>) => {
29
- let lastError: unknown = null;
30
-
31
- for (let i = 0; i < count; i++) {
32
- try {
33
- return await fn();
34
- }
35
- catch (e: unknown) {
36
- lastError = e;
37
- if (i === count - 1) {
38
- throw e;
39
- }
40
- }
41
- }
42
- // count should be greater than 0.
43
- throw lastError;
44
- };
45
- }
46
-
47
- /**
48
- * This function is used to group an array into a 2-dimensional array.
49
- *
50
- * It will throw an error if the count is not a number,
51
- * or if the count is not a finite number,
52
- * or if the count is not an integer,
53
- * or if the count is less than 1.
54
- *
55
- * @param array - The array to group.
56
- * @param count - The number of elements in each group.
57
- * @returns A 2-dimensional array.
58
- */
59
- export function groupByArray<T, TCount extends number>(array: T[], count: GreaterThanZeroInteger<TCount>): T[][] {
60
- if (count < 1) {
61
- throw new Error("count should be greater than 0");
62
- }
63
-
64
- if (array.length === 0) {
65
- return [];
66
- }
67
-
68
- if (array.length < count) {
69
- return [array];
70
- }
71
-
72
- const grouped = [];
73
- for (let i = 0; i < array.length; i += count) {
74
- grouped.push(array.slice(i, i + count));
75
- }
76
- return grouped;
77
- }
78
-
79
- /**
80
- * Removes duplicates from an array.
81
- * You can specify which value to compare using a property selector function.
82
- *
83
- * @param array - Array to remove duplicates from
84
- * @param selector - Function that selects the value to compare
85
- * @returns New array with duplicates removed
86
- *
87
- * @example
88
- * ```typescript
89
- * const users = [
90
- * { id: 1, name: 'John' },
91
- * { id: 2, name: 'Jane' },
92
- * { id: 1, name: 'John' }
93
- * ];
94
- *
95
- * const uniqueUsers = uniqBy(users, user => user.id);
96
- * // Result: [{ id: 1, name: 'John' }, { id: 2, name: 'Jane' }]
97
- * ```
98
- */
99
- export function uniqBy<T, K>(array: T[], selector: (item: T) => K): T[] {
100
- const seen = new Set<K>();
101
- return array.filter((item) => {
102
- const key = selector(item);
103
- if (seen.has(key)) {
104
- return false;
105
- }
106
- seen.add(key);
107
- return true;
108
- });
109
- }
110
-
111
- /**
112
- * Generates a hash from an Agentica ctx.operations.array.
113
- *
114
- * @param ctx - The Agentica context to generate a hash from
115
- * @returns A hash of the Agentica context
116
- */
117
- export function generateHashFromCtx(ctx: AgenticaContext): string {
118
- const target = JSON.stringify(ctx.operations.array);
119
- const bytes = utf8ToBytes(target);
120
- const hash = sha256(bytes);
121
- const binary = String.fromCharCode(...hash);
122
- return btoa(binary);
123
- }
1
+ import type { AgenticaContext } from "@agentica/core";
2
+ import type { GreaterThan, Integer } from "type-fest";
3
+
4
+ import { sha256 } from "@noble/hashes/sha2";
5
+ import { utf8ToBytes } from "@noble/hashes/utils";
6
+
7
+ /** type function to check if a number is greater than 0 */
8
+ type GreaterThanZeroInteger<T extends number> = GreaterThan<Integer<T>, 0> extends true ? T : never;
9
+
10
+ /**
11
+ * This function is used to get a retry function.
12
+ *
13
+ * It will throw an error if the count is not a number,
14
+ * or if the count is not a finite number,
15
+ * or if the count is not an integer,
16
+ * or if the count is less than 1.
17
+ *
18
+ * @param count - The number of times to retry the function.
19
+ * @returns A retry function.
20
+ * @throws {TypeError} If the count is not a number, or if the count is not a finite number, or if the count is not an integer, or if the count is less than 1.
21
+ * @throws {Error} If the function fails to return a value after the specified number of retries.
22
+ */
23
+ export function getRetry<TCount extends number>(count: GreaterThanZeroInteger<TCount>) {
24
+ if (count < 1) {
25
+ throw new Error("count should be greater than 0");
26
+ }
27
+
28
+ return async <T>(fn: () => Promise<T>) => {
29
+ let lastError: unknown = null;
30
+
31
+ for (let i = 0; i < count; i++) {
32
+ try {
33
+ return await fn();
34
+ }
35
+ catch (e: unknown) {
36
+ lastError = e;
37
+ if (i === count - 1) {
38
+ throw e;
39
+ }
40
+ }
41
+ }
42
+ // count should be greater than 0.
43
+ throw lastError;
44
+ };
45
+ }
46
+
47
+ /**
48
+ * This function is used to group an array into a 2-dimensional array.
49
+ *
50
+ * It will throw an error if the count is not a number,
51
+ * or if the count is not a finite number,
52
+ * or if the count is not an integer,
53
+ * or if the count is less than 1.
54
+ *
55
+ * @param array - The array to group.
56
+ * @param count - The number of elements in each group.
57
+ * @returns A 2-dimensional array.
58
+ */
59
+ export function groupByArray<T, TCount extends number>(array: T[], count: GreaterThanZeroInteger<TCount>): T[][] {
60
+ if (count < 1) {
61
+ throw new Error("count should be greater than 0");
62
+ }
63
+
64
+ if (array.length === 0) {
65
+ return [];
66
+ }
67
+
68
+ if (array.length < count) {
69
+ return [array];
70
+ }
71
+
72
+ const grouped = [];
73
+ for (let i = 0; i < array.length; i += count) {
74
+ grouped.push(array.slice(i, i + count));
75
+ }
76
+ return grouped;
77
+ }
78
+
79
+ /**
80
+ * Removes duplicates from an array.
81
+ * You can specify which value to compare using a property selector function.
82
+ *
83
+ * @param array - Array to remove duplicates from
84
+ * @param selector - Function that selects the value to compare
85
+ * @returns New array with duplicates removed
86
+ *
87
+ * @example
88
+ * ```typescript
89
+ * const users = [
90
+ * { id: 1, name: 'John' },
91
+ * { id: 2, name: 'Jane' },
92
+ * { id: 1, name: 'John' }
93
+ * ];
94
+ *
95
+ * const uniqueUsers = uniqBy(users, user => user.id);
96
+ * // Result: [{ id: 1, name: 'John' }, { id: 2, name: 'Jane' }]
97
+ * ```
98
+ */
99
+ export function uniqBy<T, K>(array: T[], selector: (item: T) => K): T[] {
100
+ const seen = new Set<K>();
101
+ return array.filter((item) => {
102
+ const key = selector(item);
103
+ if (seen.has(key)) {
104
+ return false;
105
+ }
106
+ seen.add(key);
107
+ return true;
108
+ });
109
+ }
110
+
111
+ /**
112
+ * Generates a hash from an Agentica ctx.operations.array.
113
+ *
114
+ * @param ctx - The Agentica context to generate a hash from
115
+ * @returns A hash of the Agentica context
116
+ */
117
+ export function generateHashFromCtx(ctx: AgenticaContext): string {
118
+ const target = JSON.stringify(ctx.operations.array);
119
+ const bytes = utf8ToBytes(target);
120
+ const hash = sha256(bytes);
121
+ const binary = String.fromCharCode(...hash);
122
+ return btoa(binary);
123
+ }