@lov3kaizen/agentsea-embeddings 0.5.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.
@@ -0,0 +1,27 @@
1
+ import {
2
+ BaseCache,
3
+ MemoryCache,
4
+ RedisCache,
5
+ SQLiteCache,
6
+ TieredCache,
7
+ createCache,
8
+ createMemoryCache,
9
+ createRedisCache,
10
+ createSQLiteCache,
11
+ createStandardTieredCache,
12
+ createTieredCache
13
+ } from "../chunk-VPSMDBHH.mjs";
14
+ import "../chunk-3KM32UQK.mjs";
15
+ export {
16
+ BaseCache,
17
+ MemoryCache,
18
+ RedisCache,
19
+ SQLiteCache,
20
+ TieredCache,
21
+ createCache,
22
+ createMemoryCache,
23
+ createRedisCache,
24
+ createSQLiteCache,
25
+ createStandardTieredCache,
26
+ createTieredCache
27
+ };
@@ -0,0 +1,207 @@
1
+ // src/core/utils.ts
2
+ import { createHash } from "crypto";
3
+ function contentHash(text, algorithm = "sha256") {
4
+ return createHash(algorithm).update(text).digest("hex");
5
+ }
6
+ function cacheKey(text, model, prefix = "emb") {
7
+ const hash = contentHash(`${model}:${text}`);
8
+ return `${prefix}:${model}:${hash}`;
9
+ }
10
+ function estimateTokens(text) {
11
+ return Math.ceil(text.length / 4);
12
+ }
13
+ function splitByChars(text, maxChars, overlap = 0) {
14
+ const chunks = [];
15
+ let start = 0;
16
+ while (start < text.length) {
17
+ const end = Math.min(start + maxChars, text.length);
18
+ chunks.push(text.slice(start, end));
19
+ start = end - overlap;
20
+ if (start >= text.length) break;
21
+ }
22
+ return chunks;
23
+ }
24
+ function splitBySeparator(text, separator) {
25
+ return text.split(separator).filter((s) => s.trim().length > 0);
26
+ }
27
+ function batch(items, batchSize) {
28
+ const batches = [];
29
+ for (let i = 0; i < items.length; i += batchSize) {
30
+ batches.push(items.slice(i, i + batchSize));
31
+ }
32
+ return batches;
33
+ }
34
+ async function withConcurrency(items, fn, concurrency) {
35
+ const results = new Array(items.length);
36
+ let currentIndex = 0;
37
+ async function worker() {
38
+ while (currentIndex < items.length) {
39
+ const index = currentIndex++;
40
+ results[index] = await fn(items[index], index);
41
+ }
42
+ }
43
+ const workers = Array.from(
44
+ { length: Math.min(concurrency, items.length) },
45
+ () => worker()
46
+ );
47
+ await Promise.all(workers);
48
+ return results;
49
+ }
50
+ async function retry(fn, options = {}) {
51
+ const {
52
+ maxRetries = 3,
53
+ initialDelay = 1e3,
54
+ maxDelay = 3e4,
55
+ backoffMultiplier = 2,
56
+ retryCondition = () => true
57
+ } = options;
58
+ let lastError;
59
+ let delay = initialDelay;
60
+ for (let attempt = 0; attempt <= maxRetries; attempt++) {
61
+ try {
62
+ return await fn();
63
+ } catch (error) {
64
+ lastError = error;
65
+ if (attempt === maxRetries || !retryCondition(lastError)) {
66
+ throw lastError;
67
+ }
68
+ await sleep(delay);
69
+ delay = Math.min(delay * backoffMultiplier, maxDelay);
70
+ }
71
+ }
72
+ throw lastError;
73
+ }
74
+ function sleep(ms) {
75
+ return new Promise((resolve) => setTimeout(resolve, ms));
76
+ }
77
+ function deferred() {
78
+ let resolve;
79
+ let reject;
80
+ const promise = new Promise((res, rej) => {
81
+ resolve = res;
82
+ reject = rej;
83
+ });
84
+ return { promise, resolve, reject };
85
+ }
86
+ function percentile(values, p) {
87
+ if (values.length === 0) return 0;
88
+ if (p < 0 || p > 100) throw new Error("Percentile must be between 0 and 100");
89
+ const sorted = [...values].sort((a, b) => a - b);
90
+ const index = p / 100 * (sorted.length - 1);
91
+ const lower = Math.floor(index);
92
+ const upper = Math.ceil(index);
93
+ if (lower === upper) {
94
+ return sorted[lower];
95
+ }
96
+ const weight = index - lower;
97
+ return sorted[lower] * (1 - weight) + sorted[upper] * weight;
98
+ }
99
+ function mean(values) {
100
+ if (values.length === 0) return 0;
101
+ return values.reduce((sum, v) => sum + v, 0) / values.length;
102
+ }
103
+ function variance(values) {
104
+ if (values.length === 0) return 0;
105
+ const m = mean(values);
106
+ return values.reduce((sum, v) => sum + Math.pow(v - m, 2), 0) / values.length;
107
+ }
108
+ function stdDev(values) {
109
+ return Math.sqrt(variance(values));
110
+ }
111
+ function normalize(value, min, max) {
112
+ if (max === min) return 0;
113
+ return (value - min) / (max - min);
114
+ }
115
+ function clamp(value, min, max) {
116
+ return Math.min(Math.max(value, min), max);
117
+ }
118
+ function generateId(prefix = "") {
119
+ const timestamp = Date.now().toString(36);
120
+ const random = Math.random().toString(36).substring(2, 10);
121
+ return prefix ? `${prefix}_${timestamp}${random}` : `${timestamp}${random}`;
122
+ }
123
+ function deepClone(obj) {
124
+ return JSON.parse(JSON.stringify(obj));
125
+ }
126
+ async function measureTime(fn) {
127
+ const start = performance.now();
128
+ const result = await fn();
129
+ const durationMs = performance.now() - start;
130
+ return { result, durationMs };
131
+ }
132
+ function formatBytes(bytes) {
133
+ if (bytes === 0) return "0 B";
134
+ const k = 1024;
135
+ const sizes = ["B", "KB", "MB", "GB", "TB"];
136
+ const i = Math.floor(Math.log(bytes) / Math.log(k));
137
+ return `${parseFloat((bytes / Math.pow(k, i)).toFixed(2))} ${sizes[i]}`;
138
+ }
139
+ function formatDuration(ms) {
140
+ if (ms < 1e3) return `${Math.round(ms)}ms`;
141
+ if (ms < 6e4) return `${(ms / 1e3).toFixed(1)}s`;
142
+ if (ms < 36e5) return `${(ms / 6e4).toFixed(1)}m`;
143
+ return `${(ms / 36e5).toFixed(1)}h`;
144
+ }
145
+ function createEventEmitter() {
146
+ const listeners = {};
147
+ return {
148
+ on(event, listener) {
149
+ if (!listeners[event]) {
150
+ listeners[event] = [];
151
+ }
152
+ listeners[event].push(listener);
153
+ return () => this.off(event, listener);
154
+ },
155
+ off(event, listener) {
156
+ const list = listeners[event];
157
+ if (list) {
158
+ const index = list.indexOf(listener);
159
+ if (index !== -1) {
160
+ list.splice(index, 1);
161
+ }
162
+ }
163
+ },
164
+ emit(event, ...args) {
165
+ const list = listeners[event];
166
+ if (list) {
167
+ for (const listener of list) {
168
+ listener(...args);
169
+ }
170
+ }
171
+ },
172
+ removeAllListeners(event) {
173
+ if (event) {
174
+ delete listeners[event];
175
+ } else {
176
+ for (const key of Object.keys(listeners)) {
177
+ delete listeners[key];
178
+ }
179
+ }
180
+ }
181
+ };
182
+ }
183
+
184
+ export {
185
+ contentHash,
186
+ cacheKey,
187
+ estimateTokens,
188
+ splitByChars,
189
+ splitBySeparator,
190
+ batch,
191
+ withConcurrency,
192
+ retry,
193
+ sleep,
194
+ deferred,
195
+ percentile,
196
+ mean,
197
+ variance,
198
+ stdDev,
199
+ normalize,
200
+ clamp,
201
+ generateId,
202
+ deepClone,
203
+ measureTime,
204
+ formatBytes,
205
+ formatDuration,
206
+ createEventEmitter
207
+ };