@scenemesh/entity-engine-aimodule 1.0.0
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/LICENSE +21 -0
- package/README.md +854 -0
- package/dist/ai-core-LBGYFGOK.mjs +17 -0
- package/dist/ai-core-LBGYFGOK.mjs.map +1 -0
- package/dist/ai-core-UGJWSCQN.js +17 -0
- package/dist/ai-core-UGJWSCQN.js.map +1 -0
- package/dist/ai-core-manager-B3Z34RHA.mjs +9 -0
- package/dist/ai-core-manager-B3Z34RHA.mjs.map +1 -0
- package/dist/ai-core-manager-W7SSDCG5.js +9 -0
- package/dist/ai-core-manager-W7SSDCG5.js.map +1 -0
- package/dist/ai-embeddings-5ED5LDXX.mjs +17 -0
- package/dist/ai-embeddings-5ED5LDXX.mjs.map +1 -0
- package/dist/ai-embeddings-WCXZMMTZ.js +17 -0
- package/dist/ai-embeddings-WCXZMMTZ.js.map +1 -0
- package/dist/ai-form-renderer-24IWNMX5.js +233 -0
- package/dist/ai-form-renderer-24IWNMX5.js.map +1 -0
- package/dist/ai-form-renderer-BORQABF2.mjs +233 -0
- package/dist/ai-form-renderer-BORQABF2.mjs.map +1 -0
- package/dist/ai-provider-3PSCVEEN.mjs +17 -0
- package/dist/ai-provider-3PSCVEEN.mjs.map +1 -0
- package/dist/ai-provider-WMPMVZFL.js +17 -0
- package/dist/ai-provider-WMPMVZFL.js.map +1 -0
- package/dist/ai-renderer-7WGGWH5D.mjs +134 -0
- package/dist/ai-renderer-7WGGWH5D.mjs.map +1 -0
- package/dist/ai-renderer-OILYWAJV.js +134 -0
- package/dist/ai-renderer-OILYWAJV.js.map +1 -0
- package/dist/ai-settings-DGCFPK3U.js +15 -0
- package/dist/ai-settings-DGCFPK3U.js.map +1 -0
- package/dist/ai-settings-DTXEAB64.mjs +15 -0
- package/dist/ai-settings-DTXEAB64.mjs.map +1 -0
- package/dist/ai-structured-EGZ26ZS4.mjs +13 -0
- package/dist/ai-structured-EGZ26ZS4.mjs.map +1 -0
- package/dist/ai-structured-N2FZLO4A.js +13 -0
- package/dist/ai-structured-N2FZLO4A.js.map +1 -0
- package/dist/ai-tools-B3R77HZ3.js +19 -0
- package/dist/ai-tools-B3R77HZ3.js.map +1 -0
- package/dist/ai-tools-JAPVYQGE.mjs +19 -0
- package/dist/ai-tools-JAPVYQGE.mjs.map +1 -0
- package/dist/ai.module-GAHVCBTP.js +7 -0
- package/dist/ai.module-GAHVCBTP.js.map +1 -0
- package/dist/ai.module-TTPMTPB3.mjs +7 -0
- package/dist/ai.module-TTPMTPB3.mjs.map +1 -0
- package/dist/chunk-25C2NRSD.mjs +611 -0
- package/dist/chunk-25C2NRSD.mjs.map +1 -0
- package/dist/chunk-4JQ7UOXH.js +427 -0
- package/dist/chunk-4JQ7UOXH.js.map +1 -0
- package/dist/chunk-6IUKES2L.js +290 -0
- package/dist/chunk-6IUKES2L.js.map +1 -0
- package/dist/chunk-COWPK7XN.mjs +834 -0
- package/dist/chunk-COWPK7XN.mjs.map +1 -0
- package/dist/chunk-CTEXPMVZ.js +512 -0
- package/dist/chunk-CTEXPMVZ.js.map +1 -0
- package/dist/chunk-DXQTHA75.js +573 -0
- package/dist/chunk-DXQTHA75.js.map +1 -0
- package/dist/chunk-DZFQ6I23.js +72 -0
- package/dist/chunk-DZFQ6I23.js.map +1 -0
- package/dist/chunk-J323UTPE.mjs +650 -0
- package/dist/chunk-J323UTPE.mjs.map +1 -0
- package/dist/chunk-LHNNALVF.js +834 -0
- package/dist/chunk-LHNNALVF.js.map +1 -0
- package/dist/chunk-O7SZSMXV.js +1621 -0
- package/dist/chunk-O7SZSMXV.js.map +1 -0
- package/dist/chunk-OTNOFOVW.js +650 -0
- package/dist/chunk-OTNOFOVW.js.map +1 -0
- package/dist/chunk-PRIGZEI4.mjs +72 -0
- package/dist/chunk-PRIGZEI4.mjs.map +1 -0
- package/dist/chunk-SBSZ3IPB.mjs +573 -0
- package/dist/chunk-SBSZ3IPB.mjs.map +1 -0
- package/dist/chunk-SXPA6SSD.mjs +1621 -0
- package/dist/chunk-SXPA6SSD.mjs.map +1 -0
- package/dist/chunk-T5A4KAVS.mjs +512 -0
- package/dist/chunk-T5A4KAVS.mjs.map +1 -0
- package/dist/chunk-TDRKKUNT.mjs +357 -0
- package/dist/chunk-TDRKKUNT.mjs.map +1 -0
- package/dist/chunk-TJFNODPE.js +357 -0
- package/dist/chunk-TJFNODPE.js.map +1 -0
- package/dist/chunk-V2SSI3SL.mjs +427 -0
- package/dist/chunk-V2SSI3SL.mjs.map +1 -0
- package/dist/chunk-X42L6MTY.mjs +290 -0
- package/dist/chunk-X42L6MTY.mjs.map +1 -0
- package/dist/chunk-YSVMY77H.js +611 -0
- package/dist/chunk-YSVMY77H.js.map +1 -0
- package/dist/core-ANYRS6EF.mjs +73 -0
- package/dist/core-ANYRS6EF.mjs.map +1 -0
- package/dist/core-K5K34DCS.js +73 -0
- package/dist/core-K5K34DCS.js.map +1 -0
- package/dist/core-index.d.mts +1668 -0
- package/dist/core-index.d.ts +1668 -0
- package/dist/core-index.js +101 -0
- package/dist/core-index.js.map +1 -0
- package/dist/core-index.mjs +101 -0
- package/dist/core-index.mjs.map +1 -0
- package/dist/index.d.mts +2911 -0
- package/dist/index.d.ts +2911 -0
- package/dist/index.js +1177 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +1177 -0
- package/dist/index.mjs.map +1 -0
- package/dist/tools-352X7A6X.mjs +366 -0
- package/dist/tools-352X7A6X.mjs.map +1 -0
- package/dist/tools-YLEX6GNO.js +366 -0
- package/dist/tools-YLEX6GNO.js.map +1 -0
- package/dist/ui-index.d.mts +627 -0
- package/dist/ui-index.d.ts +627 -0
- package/dist/ui-index.js +2354 -0
- package/dist/ui-index.js.map +1 -0
- package/dist/ui-index.mjs +2353 -0
- package/dist/ui-index.mjs.map +1 -0
- package/package.json +105 -0
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,1177 @@
|
|
|
1
|
+
import {
|
|
2
|
+
CORE_VERSION,
|
|
3
|
+
InvalidToolInputError,
|
|
4
|
+
NoSuchToolError,
|
|
5
|
+
cosineSimilarity,
|
|
6
|
+
createAICore,
|
|
7
|
+
createProviderRegistry,
|
|
8
|
+
customProvider,
|
|
9
|
+
defaultSettingsMiddleware,
|
|
10
|
+
embed,
|
|
11
|
+
embedMany,
|
|
12
|
+
generateObject,
|
|
13
|
+
generateText,
|
|
14
|
+
smoothStream,
|
|
15
|
+
streamObject,
|
|
16
|
+
streamText,
|
|
17
|
+
tool,
|
|
18
|
+
wrapLanguageModel
|
|
19
|
+
} from "./chunk-PRIGZEI4.mjs";
|
|
20
|
+
import {
|
|
21
|
+
EntityAIModule
|
|
22
|
+
} from "./chunk-SBSZ3IPB.mjs";
|
|
23
|
+
import {
|
|
24
|
+
AISDKIntegration
|
|
25
|
+
} from "./chunk-X42L6MTY.mjs";
|
|
26
|
+
import {
|
|
27
|
+
AIToolsIntegration
|
|
28
|
+
} from "./chunk-25C2NRSD.mjs";
|
|
29
|
+
import {
|
|
30
|
+
AIEmbeddingsIntegration,
|
|
31
|
+
SupportedEmbeddingModels
|
|
32
|
+
} from "./chunk-TDRKKUNT.mjs";
|
|
33
|
+
import {
|
|
34
|
+
AISettingsManagement,
|
|
35
|
+
BuiltinSettingsPresets
|
|
36
|
+
} from "./chunk-COWPK7XN.mjs";
|
|
37
|
+
import {
|
|
38
|
+
AIProviderManagement
|
|
39
|
+
} from "./chunk-J323UTPE.mjs";
|
|
40
|
+
import {
|
|
41
|
+
AIStructuredDataIntegration
|
|
42
|
+
} from "./chunk-V2SSI3SL.mjs";
|
|
43
|
+
import {
|
|
44
|
+
AICoreManager
|
|
45
|
+
} from "./chunk-T5A4KAVS.mjs";
|
|
46
|
+
|
|
47
|
+
// src/utils/warnings.ts
|
|
48
|
+
var WarningManager = class {
|
|
49
|
+
constructor() {
|
|
50
|
+
this.warnings = [];
|
|
51
|
+
this.handler = null;
|
|
52
|
+
this.maxWarnings = 100;
|
|
53
|
+
}
|
|
54
|
+
// Maximum warnings to prevent memory leaks
|
|
55
|
+
/**
|
|
56
|
+
* Initialize warning system
|
|
57
|
+
*/
|
|
58
|
+
initialize() {
|
|
59
|
+
if (typeof globalThis !== "undefined") {
|
|
60
|
+
const globalHandler = globalThis.AI_SDK_LOG_WARNINGS;
|
|
61
|
+
if (globalHandler !== void 0) {
|
|
62
|
+
this.handler = globalHandler;
|
|
63
|
+
} else {
|
|
64
|
+
this.handler = true;
|
|
65
|
+
}
|
|
66
|
+
} else {
|
|
67
|
+
this.handler = true;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Set warning handler
|
|
72
|
+
*/
|
|
73
|
+
setHandler(handler) {
|
|
74
|
+
this.handler = handler;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Add warning
|
|
78
|
+
*/
|
|
79
|
+
warn(type, message, details, source) {
|
|
80
|
+
const warning = {
|
|
81
|
+
type,
|
|
82
|
+
message,
|
|
83
|
+
details,
|
|
84
|
+
timestamp: Date.now(),
|
|
85
|
+
source
|
|
86
|
+
};
|
|
87
|
+
this.warnings.push(warning);
|
|
88
|
+
if (this.warnings.length > this.maxWarnings) {
|
|
89
|
+
this.warnings = this.warnings.slice(-this.maxWarnings);
|
|
90
|
+
}
|
|
91
|
+
this.processWarning(warning);
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Process warning
|
|
95
|
+
*/
|
|
96
|
+
processWarning(warning) {
|
|
97
|
+
if (this.handler === false) {
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
if (typeof this.handler === "function") {
|
|
101
|
+
try {
|
|
102
|
+
this.handler([warning]);
|
|
103
|
+
} catch (error) {
|
|
104
|
+
this.defaultHandler([warning]);
|
|
105
|
+
}
|
|
106
|
+
} else {
|
|
107
|
+
this.defaultHandler([warning]);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Default warning handler
|
|
112
|
+
*/
|
|
113
|
+
defaultHandler(warnings) {
|
|
114
|
+
warnings.forEach((warning) => {
|
|
115
|
+
const prefix = "AI SDK Warning:";
|
|
116
|
+
const message = `${prefix} ${warning.message}`;
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Get all warnings
|
|
121
|
+
*/
|
|
122
|
+
getWarnings() {
|
|
123
|
+
return [...this.warnings];
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Clear warnings
|
|
127
|
+
*/
|
|
128
|
+
clearWarnings() {
|
|
129
|
+
this.warnings = [];
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* Get warnings by type
|
|
133
|
+
*/
|
|
134
|
+
getWarningsByType(type) {
|
|
135
|
+
return this.warnings.filter((w) => w.type === type);
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Get warning statistics
|
|
139
|
+
*/
|
|
140
|
+
getWarningStats() {
|
|
141
|
+
const stats = {};
|
|
142
|
+
this.warnings.forEach((warning) => {
|
|
143
|
+
stats[warning.type] = (stats[warning.type] || 0) + 1;
|
|
144
|
+
});
|
|
145
|
+
return stats;
|
|
146
|
+
}
|
|
147
|
+
};
|
|
148
|
+
var warningManager = new WarningManager();
|
|
149
|
+
if (typeof globalThis !== "undefined") {
|
|
150
|
+
warningManager.initialize();
|
|
151
|
+
}
|
|
152
|
+
function warnUnsupportedSetting(setting, model) {
|
|
153
|
+
warningManager.warn(
|
|
154
|
+
"unsupported-setting",
|
|
155
|
+
`The "${setting}" setting is not supported${model ? ` by the ${model} model` : ""}`,
|
|
156
|
+
{ setting, model },
|
|
157
|
+
"entity-engine-aiui"
|
|
158
|
+
);
|
|
159
|
+
}
|
|
160
|
+
function warnUnsupportedTool(toolName, model) {
|
|
161
|
+
warningManager.warn(
|
|
162
|
+
"unsupported-tool",
|
|
163
|
+
`The tool "${toolName}" is not supported${model ? ` by the ${model} model` : ""}`,
|
|
164
|
+
{ toolName, model },
|
|
165
|
+
"entity-engine-aiui"
|
|
166
|
+
);
|
|
167
|
+
}
|
|
168
|
+
function warnDeprecatedFeature(feature, replacement) {
|
|
169
|
+
const message = `The "${feature}" feature is deprecated${replacement ? ` and will be replaced with "${replacement}"` : ""}`;
|
|
170
|
+
warningManager.warn(
|
|
171
|
+
"deprecated-feature",
|
|
172
|
+
message,
|
|
173
|
+
{ feature, replacement },
|
|
174
|
+
"entity-engine-aiui"
|
|
175
|
+
);
|
|
176
|
+
}
|
|
177
|
+
function warnValidationError(message, details) {
|
|
178
|
+
warningManager.warn(
|
|
179
|
+
"validation-error",
|
|
180
|
+
message,
|
|
181
|
+
details,
|
|
182
|
+
"entity-engine-aiui"
|
|
183
|
+
);
|
|
184
|
+
}
|
|
185
|
+
function warnPerformance(message, details) {
|
|
186
|
+
warningManager.warn(
|
|
187
|
+
"performance-warning",
|
|
188
|
+
message,
|
|
189
|
+
details,
|
|
190
|
+
"entity-engine-aiui"
|
|
191
|
+
);
|
|
192
|
+
}
|
|
193
|
+
function warn(message, details) {
|
|
194
|
+
warningManager.warn(
|
|
195
|
+
"unknown",
|
|
196
|
+
message,
|
|
197
|
+
details,
|
|
198
|
+
"entity-engine-aiui"
|
|
199
|
+
);
|
|
200
|
+
}
|
|
201
|
+
function setWarningHandler(handler) {
|
|
202
|
+
warningManager.setHandler(handler);
|
|
203
|
+
}
|
|
204
|
+
function getWarnings() {
|
|
205
|
+
return warningManager.getWarnings();
|
|
206
|
+
}
|
|
207
|
+
function clearWarnings() {
|
|
208
|
+
warningManager.clearWarnings();
|
|
209
|
+
}
|
|
210
|
+
function getWarningStats() {
|
|
211
|
+
return warningManager.getWarningStats();
|
|
212
|
+
}
|
|
213
|
+
if (process.env.NODE_ENV === "development") {
|
|
214
|
+
if (typeof globalThis !== "undefined") {
|
|
215
|
+
globalThis.__ENTITY_ENGINE_AIUI_WARNINGS__ = {
|
|
216
|
+
manager: warningManager,
|
|
217
|
+
getWarnings,
|
|
218
|
+
clearWarnings,
|
|
219
|
+
getWarningStats,
|
|
220
|
+
setWarningHandler
|
|
221
|
+
};
|
|
222
|
+
}
|
|
223
|
+
const originalWarn = warningManager.warn.bind(warningManager);
|
|
224
|
+
warningManager.warn = (type, message, details, source) => {
|
|
225
|
+
const stack = new Error().stack;
|
|
226
|
+
const enhancedDetails = {
|
|
227
|
+
...details,
|
|
228
|
+
stack: stack?.split("\n").slice(2, 5).join("\n")
|
|
229
|
+
// Keep only first 3 stack levels
|
|
230
|
+
};
|
|
231
|
+
originalWarn(type, message, enhancedDetails, source);
|
|
232
|
+
};
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
// src/utils/streaming.ts
|
|
236
|
+
var UIMessageStreamReader = class {
|
|
237
|
+
constructor(options) {
|
|
238
|
+
this.options = options;
|
|
239
|
+
this.reader = null;
|
|
240
|
+
this.currentMessage = null;
|
|
241
|
+
this.isReading = false;
|
|
242
|
+
this.currentMessage = options.message || null;
|
|
243
|
+
}
|
|
244
|
+
/**
|
|
245
|
+
* Start reading stream
|
|
246
|
+
*/
|
|
247
|
+
async *read() {
|
|
248
|
+
if (this.isReading) {
|
|
249
|
+
throw new Error("Stream is already being read");
|
|
250
|
+
}
|
|
251
|
+
this.isReading = true;
|
|
252
|
+
this.reader = this.options.stream.getReader();
|
|
253
|
+
try {
|
|
254
|
+
let done = false;
|
|
255
|
+
while (!done) {
|
|
256
|
+
const { value, done: streamDone } = await this.reader.read();
|
|
257
|
+
done = streamDone;
|
|
258
|
+
if (value) {
|
|
259
|
+
const message = this.processChunk(value);
|
|
260
|
+
if (message) {
|
|
261
|
+
this.options.onMessage?.(message);
|
|
262
|
+
yield message;
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
if (this.currentMessage) {
|
|
267
|
+
this.options.onComplete?.(this.currentMessage);
|
|
268
|
+
}
|
|
269
|
+
} catch (error) {
|
|
270
|
+
const err = error instanceof Error ? error : new Error(String(error));
|
|
271
|
+
this.options.onError?.(err);
|
|
272
|
+
throw err;
|
|
273
|
+
} finally {
|
|
274
|
+
this.cleanup();
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
/**
|
|
278
|
+
* Process stream chunk
|
|
279
|
+
*/
|
|
280
|
+
processChunk(chunk) {
|
|
281
|
+
try {
|
|
282
|
+
if (typeof chunk === "string") {
|
|
283
|
+
chunk = JSON.parse(chunk);
|
|
284
|
+
}
|
|
285
|
+
if (chunk instanceof Uint8Array) {
|
|
286
|
+
const decoder = new TextDecoder();
|
|
287
|
+
chunk = JSON.parse(decoder.decode(chunk));
|
|
288
|
+
}
|
|
289
|
+
if (chunk.type) {
|
|
290
|
+
return this.handleStreamData(chunk);
|
|
291
|
+
}
|
|
292
|
+
return null;
|
|
293
|
+
} catch (error) {
|
|
294
|
+
return null;
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
/**
|
|
298
|
+
* Handle stream data
|
|
299
|
+
*/
|
|
300
|
+
handleStreamData(data) {
|
|
301
|
+
switch (data.type) {
|
|
302
|
+
case "message-start":
|
|
303
|
+
return this.handleMessageStart(data);
|
|
304
|
+
case "message-delta":
|
|
305
|
+
return this.handleMessageDelta(data);
|
|
306
|
+
case "message-stop":
|
|
307
|
+
return this.handleMessageStop(data);
|
|
308
|
+
case "text-delta":
|
|
309
|
+
return this.handleTextDelta(data);
|
|
310
|
+
case "tool-call":
|
|
311
|
+
return this.handleToolCall(data);
|
|
312
|
+
case "tool-result":
|
|
313
|
+
return this.handleToolResult(data);
|
|
314
|
+
default:
|
|
315
|
+
return null;
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
/**
|
|
319
|
+
* Handle message start
|
|
320
|
+
*/
|
|
321
|
+
handleMessageStart(data) {
|
|
322
|
+
this.currentMessage = {
|
|
323
|
+
id: data.messageId || this.generateId(),
|
|
324
|
+
role: data.role || "assistant",
|
|
325
|
+
parts: [],
|
|
326
|
+
metadata: data.metadata || {},
|
|
327
|
+
...data
|
|
328
|
+
};
|
|
329
|
+
return { ...this.currentMessage };
|
|
330
|
+
}
|
|
331
|
+
/**
|
|
332
|
+
* Handle message delta
|
|
333
|
+
*/
|
|
334
|
+
handleMessageDelta(data) {
|
|
335
|
+
if (!this.currentMessage) {
|
|
336
|
+
return null;
|
|
337
|
+
}
|
|
338
|
+
if (data.content) {
|
|
339
|
+
let textPart = this.currentMessage.parts.find((p) => p.type === "text");
|
|
340
|
+
if (!textPart) {
|
|
341
|
+
textPart = { type: "text", text: "" };
|
|
342
|
+
this.currentMessage.parts.push(textPart);
|
|
343
|
+
}
|
|
344
|
+
textPart.text += data.content;
|
|
345
|
+
}
|
|
346
|
+
return { ...this.currentMessage };
|
|
347
|
+
}
|
|
348
|
+
/**
|
|
349
|
+
* Handle message stop
|
|
350
|
+
*/
|
|
351
|
+
handleMessageStop(data) {
|
|
352
|
+
if (!this.currentMessage) {
|
|
353
|
+
return null;
|
|
354
|
+
}
|
|
355
|
+
if (data.finishReason) {
|
|
356
|
+
this.currentMessage.metadata = {
|
|
357
|
+
...this.currentMessage.metadata || {},
|
|
358
|
+
finishReason: data.finishReason
|
|
359
|
+
};
|
|
360
|
+
}
|
|
361
|
+
return { ...this.currentMessage };
|
|
362
|
+
}
|
|
363
|
+
/**
|
|
364
|
+
* Handle text delta
|
|
365
|
+
*/
|
|
366
|
+
handleTextDelta(data) {
|
|
367
|
+
if (!this.currentMessage) {
|
|
368
|
+
this.currentMessage = {
|
|
369
|
+
id: this.generateId(),
|
|
370
|
+
role: "assistant",
|
|
371
|
+
parts: [{ type: "text", text: data.text || "" }]
|
|
372
|
+
};
|
|
373
|
+
} else {
|
|
374
|
+
let textPart = this.currentMessage.parts.find((p) => p.type === "text");
|
|
375
|
+
if (!textPart) {
|
|
376
|
+
textPart = { type: "text", text: "" };
|
|
377
|
+
this.currentMessage.parts.push(textPart);
|
|
378
|
+
}
|
|
379
|
+
textPart.text += data.text || "";
|
|
380
|
+
}
|
|
381
|
+
return { ...this.currentMessage };
|
|
382
|
+
}
|
|
383
|
+
/**
|
|
384
|
+
* Handle tool call
|
|
385
|
+
*/
|
|
386
|
+
handleToolCall(data) {
|
|
387
|
+
if (!this.currentMessage) {
|
|
388
|
+
this.currentMessage = {
|
|
389
|
+
id: this.generateId(),
|
|
390
|
+
role: "assistant",
|
|
391
|
+
parts: []
|
|
392
|
+
};
|
|
393
|
+
}
|
|
394
|
+
const toolPart = {
|
|
395
|
+
type: "dynamic-tool",
|
|
396
|
+
toolCallId: data.toolCallId,
|
|
397
|
+
toolName: data.toolName,
|
|
398
|
+
input: data.input,
|
|
399
|
+
state: "input-available"
|
|
400
|
+
};
|
|
401
|
+
this.currentMessage.parts.push(toolPart);
|
|
402
|
+
return { ...this.currentMessage };
|
|
403
|
+
}
|
|
404
|
+
/**
|
|
405
|
+
* Handle tool result
|
|
406
|
+
*/
|
|
407
|
+
handleToolResult(data) {
|
|
408
|
+
if (!this.currentMessage) {
|
|
409
|
+
return null;
|
|
410
|
+
}
|
|
411
|
+
const toolPart = this.currentMessage.parts.find(
|
|
412
|
+
(p) => p.type.startsWith("tool-") && p.toolCallId === data.toolCallId
|
|
413
|
+
);
|
|
414
|
+
if (toolPart) {
|
|
415
|
+
toolPart.output = data.result;
|
|
416
|
+
toolPart.state = data.error ? "output-error" : "output-available";
|
|
417
|
+
if (data.error) {
|
|
418
|
+
toolPart.errorText = data.error;
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
return { ...this.currentMessage };
|
|
422
|
+
}
|
|
423
|
+
/**
|
|
424
|
+
* Generate ID
|
|
425
|
+
*/
|
|
426
|
+
generateId() {
|
|
427
|
+
return Math.random().toString(36).substr(2, 9);
|
|
428
|
+
}
|
|
429
|
+
/**
|
|
430
|
+
* Stop reading
|
|
431
|
+
*/
|
|
432
|
+
async stop() {
|
|
433
|
+
if (this.reader) {
|
|
434
|
+
await this.reader.cancel();
|
|
435
|
+
}
|
|
436
|
+
this.cleanup();
|
|
437
|
+
}
|
|
438
|
+
/**
|
|
439
|
+
* Cleanup resources
|
|
440
|
+
*/
|
|
441
|
+
cleanup() {
|
|
442
|
+
this.isReading = false;
|
|
443
|
+
if (this.reader) {
|
|
444
|
+
this.reader.releaseLock();
|
|
445
|
+
this.reader = null;
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
};
|
|
449
|
+
async function* readUIMessageStream(options) {
|
|
450
|
+
const reader = new UIMessageStreamReader(options);
|
|
451
|
+
yield* reader.read();
|
|
452
|
+
}
|
|
453
|
+
async function resumeStream({
|
|
454
|
+
chatId,
|
|
455
|
+
messageId,
|
|
456
|
+
endpoint = "/api/chat/resume",
|
|
457
|
+
headers = {}
|
|
458
|
+
}) {
|
|
459
|
+
const response = await fetch(endpoint, {
|
|
460
|
+
method: "POST",
|
|
461
|
+
headers: {
|
|
462
|
+
"Content-Type": "application/json",
|
|
463
|
+
...headers
|
|
464
|
+
},
|
|
465
|
+
body: JSON.stringify({
|
|
466
|
+
chatId,
|
|
467
|
+
messageId
|
|
468
|
+
})
|
|
469
|
+
});
|
|
470
|
+
if (!response.ok) {
|
|
471
|
+
throw new Error(`Resume request failed: ${response.status}`);
|
|
472
|
+
}
|
|
473
|
+
if (!response.body) {
|
|
474
|
+
throw new Error("No response body received");
|
|
475
|
+
}
|
|
476
|
+
return response.body;
|
|
477
|
+
}
|
|
478
|
+
var StreamStateManager = class {
|
|
479
|
+
constructor() {
|
|
480
|
+
this.state = "idle";
|
|
481
|
+
this.listeners = /* @__PURE__ */ new Set();
|
|
482
|
+
this.abortController = null;
|
|
483
|
+
}
|
|
484
|
+
/**
|
|
485
|
+
* Get current state
|
|
486
|
+
*/
|
|
487
|
+
getState() {
|
|
488
|
+
return this.state;
|
|
489
|
+
}
|
|
490
|
+
/**
|
|
491
|
+
* Set state
|
|
492
|
+
*/
|
|
493
|
+
setState(newState) {
|
|
494
|
+
if (this.state !== newState) {
|
|
495
|
+
this.state = newState;
|
|
496
|
+
this.notifyListeners();
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
/**
|
|
500
|
+
* Listen to state changes
|
|
501
|
+
*/
|
|
502
|
+
onStateChange(listener) {
|
|
503
|
+
this.listeners.add(listener);
|
|
504
|
+
return () => {
|
|
505
|
+
this.listeners.delete(listener);
|
|
506
|
+
};
|
|
507
|
+
}
|
|
508
|
+
/**
|
|
509
|
+
* Start stream
|
|
510
|
+
*/
|
|
511
|
+
startStream() {
|
|
512
|
+
this.abortController = new AbortController();
|
|
513
|
+
this.setState("connecting");
|
|
514
|
+
return this.abortController;
|
|
515
|
+
}
|
|
516
|
+
/**
|
|
517
|
+
* Abort stream
|
|
518
|
+
*/
|
|
519
|
+
abortStream() {
|
|
520
|
+
if (this.abortController) {
|
|
521
|
+
this.abortController.abort();
|
|
522
|
+
this.abortController = null;
|
|
523
|
+
}
|
|
524
|
+
this.setState("aborted");
|
|
525
|
+
}
|
|
526
|
+
/**
|
|
527
|
+
* Complete stream
|
|
528
|
+
*/
|
|
529
|
+
completeStream() {
|
|
530
|
+
this.abortController = null;
|
|
531
|
+
this.setState("complete");
|
|
532
|
+
}
|
|
533
|
+
/**
|
|
534
|
+
* Stream error
|
|
535
|
+
*/
|
|
536
|
+
errorStream() {
|
|
537
|
+
this.abortController = null;
|
|
538
|
+
this.setState("error");
|
|
539
|
+
}
|
|
540
|
+
/**
|
|
541
|
+
* Reset state
|
|
542
|
+
*/
|
|
543
|
+
reset() {
|
|
544
|
+
this.abortController = null;
|
|
545
|
+
this.setState("idle");
|
|
546
|
+
}
|
|
547
|
+
/**
|
|
548
|
+
* Notify listeners
|
|
549
|
+
*/
|
|
550
|
+
notifyListeners() {
|
|
551
|
+
this.listeners.forEach((listener) => {
|
|
552
|
+
try {
|
|
553
|
+
listener(this.state);
|
|
554
|
+
} catch (error) {
|
|
555
|
+
}
|
|
556
|
+
});
|
|
557
|
+
}
|
|
558
|
+
/**
|
|
559
|
+
* Dispose
|
|
560
|
+
*/
|
|
561
|
+
dispose() {
|
|
562
|
+
this.abortController = null;
|
|
563
|
+
this.listeners.clear();
|
|
564
|
+
this.setState("idle");
|
|
565
|
+
}
|
|
566
|
+
};
|
|
567
|
+
|
|
568
|
+
// src/utils/persistence.ts
|
|
569
|
+
function createIdGenerator(options = {}) {
|
|
570
|
+
const { prefix = "", size = 12 } = options;
|
|
571
|
+
return () => {
|
|
572
|
+
const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
|
|
573
|
+
let result = "";
|
|
574
|
+
for (let i = 0; i < size; i++) {
|
|
575
|
+
result += chars.charAt(Math.floor(Math.random() * chars.length));
|
|
576
|
+
}
|
|
577
|
+
return prefix ? `${prefix}-${result}` : result;
|
|
578
|
+
};
|
|
579
|
+
}
|
|
580
|
+
function generateId() {
|
|
581
|
+
return createIdGenerator()();
|
|
582
|
+
}
|
|
583
|
+
async function validateUIMessages({
|
|
584
|
+
messages,
|
|
585
|
+
tools,
|
|
586
|
+
dataPartsSchema,
|
|
587
|
+
metadataSchema
|
|
588
|
+
}) {
|
|
589
|
+
const validatedMessages = [];
|
|
590
|
+
for (const message of messages) {
|
|
591
|
+
try {
|
|
592
|
+
const validatedMessage = await validateSingleMessage(message, {
|
|
593
|
+
tools,
|
|
594
|
+
dataPartsSchema,
|
|
595
|
+
metadataSchema
|
|
596
|
+
});
|
|
597
|
+
validatedMessages.push(validatedMessage);
|
|
598
|
+
} catch (error) {
|
|
599
|
+
validatedMessages.push(sanitizeMessage(message));
|
|
600
|
+
}
|
|
601
|
+
}
|
|
602
|
+
return validatedMessages;
|
|
603
|
+
}
|
|
604
|
+
async function validateSingleMessage(message, options) {
|
|
605
|
+
const { tools, dataPartsSchema, metadataSchema } = options;
|
|
606
|
+
if (!message.id || !message.role || !Array.isArray(message.parts)) {
|
|
607
|
+
throw new Error("Invalid message structure");
|
|
608
|
+
}
|
|
609
|
+
const validatedParts = [];
|
|
610
|
+
for (const part of message.parts) {
|
|
611
|
+
const validatedPart = await validateMessagePart(part, tools);
|
|
612
|
+
validatedParts.push(validatedPart);
|
|
613
|
+
}
|
|
614
|
+
let validatedMetadata = message.metadata;
|
|
615
|
+
if (metadataSchema && message.metadata) {
|
|
616
|
+
try {
|
|
617
|
+
validatedMetadata = metadataSchema.parse(message.metadata);
|
|
618
|
+
} catch (error) {
|
|
619
|
+
validatedMetadata = void 0;
|
|
620
|
+
}
|
|
621
|
+
}
|
|
622
|
+
return {
|
|
623
|
+
...message,
|
|
624
|
+
parts: validatedParts,
|
|
625
|
+
metadata: validatedMetadata
|
|
626
|
+
};
|
|
627
|
+
}
|
|
628
|
+
async function validateMessagePart(part, tools) {
|
|
629
|
+
if (part.type === "text") {
|
|
630
|
+
if (typeof part.text !== "string") {
|
|
631
|
+
throw new Error("Text part must have a string text property");
|
|
632
|
+
}
|
|
633
|
+
return part;
|
|
634
|
+
}
|
|
635
|
+
if (part.type === "file") {
|
|
636
|
+
if (!part.filename || !part.url) {
|
|
637
|
+
throw new Error("File part must have filename and url properties");
|
|
638
|
+
}
|
|
639
|
+
return part;
|
|
640
|
+
}
|
|
641
|
+
if (part.type.startsWith("tool-") || part.type === "dynamic-tool") {
|
|
642
|
+
const toolName = part.type === "dynamic-tool" ? part.toolName : part.type.replace("tool-", "");
|
|
643
|
+
if (tools && toolName && !tools[toolName]) {
|
|
644
|
+
console.warn(`Tool ${toolName} not found in provided tools`);
|
|
645
|
+
}
|
|
646
|
+
return part;
|
|
647
|
+
}
|
|
648
|
+
if (part.type.startsWith("data-")) {
|
|
649
|
+
if (!part.data) {
|
|
650
|
+
throw new Error("Data part must have a data property");
|
|
651
|
+
}
|
|
652
|
+
return part;
|
|
653
|
+
}
|
|
654
|
+
return part;
|
|
655
|
+
}
|
|
656
|
+
function sanitizeMessage(message) {
|
|
657
|
+
const sanitizedParts = message.parts.filter(
|
|
658
|
+
(part) => (
|
|
659
|
+
// 保留有效的部分类型
|
|
660
|
+
part && typeof part === "object" && part.type
|
|
661
|
+
)
|
|
662
|
+
);
|
|
663
|
+
return {
|
|
664
|
+
...message,
|
|
665
|
+
parts: sanitizedParts.length > 0 ? sanitizedParts : [
|
|
666
|
+
{ type: "text", text: "[Message content could not be validated]" }
|
|
667
|
+
]
|
|
668
|
+
};
|
|
669
|
+
}
|
|
670
|
+
var TypeValidationError = class extends Error {
|
|
671
|
+
constructor(message, details) {
|
|
672
|
+
super(message);
|
|
673
|
+
this.details = details;
|
|
674
|
+
this.name = "TypeValidationError";
|
|
675
|
+
}
|
|
676
|
+
};
|
|
677
|
+
var InMemoryMessageStore = class {
|
|
678
|
+
constructor() {
|
|
679
|
+
this.chats = /* @__PURE__ */ new Map();
|
|
680
|
+
this.metadata = /* @__PURE__ */ new Map();
|
|
681
|
+
}
|
|
682
|
+
async createChat() {
|
|
683
|
+
const id = generateId();
|
|
684
|
+
this.chats.set(id, []);
|
|
685
|
+
this.metadata.set(id, { updatedAt: /* @__PURE__ */ new Date() });
|
|
686
|
+
return id;
|
|
687
|
+
}
|
|
688
|
+
async loadChat(id) {
|
|
689
|
+
const messages = this.chats.get(id);
|
|
690
|
+
if (!messages) {
|
|
691
|
+
throw new Error(`Chat ${id} not found`);
|
|
692
|
+
}
|
|
693
|
+
return [...messages];
|
|
694
|
+
}
|
|
695
|
+
async saveChat({ chatId, messages }) {
|
|
696
|
+
this.chats.set(chatId, [...messages]);
|
|
697
|
+
const existing = this.metadata.get(chatId) || {};
|
|
698
|
+
this.metadata.set(chatId, {
|
|
699
|
+
...existing,
|
|
700
|
+
updatedAt: /* @__PURE__ */ new Date(),
|
|
701
|
+
title: existing.title || this.extractTitle(messages)
|
|
702
|
+
});
|
|
703
|
+
}
|
|
704
|
+
async deleteChat(id) {
|
|
705
|
+
this.chats.delete(id);
|
|
706
|
+
this.metadata.delete(id);
|
|
707
|
+
}
|
|
708
|
+
async listChats() {
|
|
709
|
+
return Array.from(this.metadata.entries()).map(([id, meta]) => ({
|
|
710
|
+
id,
|
|
711
|
+
...meta
|
|
712
|
+
}));
|
|
713
|
+
}
|
|
714
|
+
extractTitle(messages) {
|
|
715
|
+
const firstUserMessage = messages.find((m) => m.role === "user");
|
|
716
|
+
if (firstUserMessage) {
|
|
717
|
+
const textPart = firstUserMessage.parts.find((p) => p.type === "text");
|
|
718
|
+
if (textPart?.text) {
|
|
719
|
+
return textPart.text.slice(0, 50) + (textPart.text.length > 50 ? "..." : "");
|
|
720
|
+
}
|
|
721
|
+
}
|
|
722
|
+
return "Untitled Chat";
|
|
723
|
+
}
|
|
724
|
+
};
|
|
725
|
+
var LocalStorageMessageStore = class {
|
|
726
|
+
constructor() {
|
|
727
|
+
this.keyPrefix = "entity-engine-aiui-chat-";
|
|
728
|
+
}
|
|
729
|
+
async createChat() {
|
|
730
|
+
const id = generateId();
|
|
731
|
+
const key = this.keyPrefix + id;
|
|
732
|
+
const chatData = {
|
|
733
|
+
id,
|
|
734
|
+
messages: [],
|
|
735
|
+
createdAt: (/* @__PURE__ */ new Date()).toISOString(),
|
|
736
|
+
updatedAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
737
|
+
};
|
|
738
|
+
localStorage.setItem(key, JSON.stringify(chatData));
|
|
739
|
+
return id;
|
|
740
|
+
}
|
|
741
|
+
async loadChat(id) {
|
|
742
|
+
const key = this.keyPrefix + id;
|
|
743
|
+
const data = localStorage.getItem(key);
|
|
744
|
+
if (!data) {
|
|
745
|
+
throw new Error(`Chat ${id} not found`);
|
|
746
|
+
}
|
|
747
|
+
try {
|
|
748
|
+
const chatData = JSON.parse(data);
|
|
749
|
+
return chatData.messages || [];
|
|
750
|
+
} catch (error) {
|
|
751
|
+
throw new Error(`Failed to parse chat data: ${error}`);
|
|
752
|
+
}
|
|
753
|
+
}
|
|
754
|
+
async saveChat({ chatId, messages }) {
|
|
755
|
+
const key = this.keyPrefix + chatId;
|
|
756
|
+
const existing = localStorage.getItem(key);
|
|
757
|
+
const chatData = {
|
|
758
|
+
id: chatId,
|
|
759
|
+
messages,
|
|
760
|
+
createdAt: (/* @__PURE__ */ new Date()).toISOString(),
|
|
761
|
+
updatedAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
762
|
+
};
|
|
763
|
+
if (existing) {
|
|
764
|
+
try {
|
|
765
|
+
const parsedExisting = JSON.parse(existing);
|
|
766
|
+
chatData.createdAt = parsedExisting.createdAt || chatData.createdAt;
|
|
767
|
+
} catch {
|
|
768
|
+
}
|
|
769
|
+
}
|
|
770
|
+
localStorage.setItem(key, JSON.stringify(chatData));
|
|
771
|
+
}
|
|
772
|
+
async deleteChat(id) {
|
|
773
|
+
const key = this.keyPrefix + id;
|
|
774
|
+
localStorage.removeItem(key);
|
|
775
|
+
}
|
|
776
|
+
async listChats() {
|
|
777
|
+
const chats = [];
|
|
778
|
+
for (let i = 0; i < localStorage.length; i++) {
|
|
779
|
+
const key = localStorage.key(i);
|
|
780
|
+
if (key?.startsWith(this.keyPrefix)) {
|
|
781
|
+
try {
|
|
782
|
+
const data = localStorage.getItem(key);
|
|
783
|
+
if (data) {
|
|
784
|
+
const chatData = JSON.parse(data);
|
|
785
|
+
chats.push({
|
|
786
|
+
id: chatData.id,
|
|
787
|
+
title: this.extractTitle(chatData.messages || []),
|
|
788
|
+
updatedAt: new Date(chatData.updatedAt || chatData.createdAt)
|
|
789
|
+
});
|
|
790
|
+
}
|
|
791
|
+
} catch {
|
|
792
|
+
}
|
|
793
|
+
}
|
|
794
|
+
}
|
|
795
|
+
return chats.sort((a, b) => b.updatedAt.getTime() - a.updatedAt.getTime());
|
|
796
|
+
}
|
|
797
|
+
extractTitle(messages) {
|
|
798
|
+
const firstUserMessage = messages.find((m) => m.role === "user");
|
|
799
|
+
if (firstUserMessage) {
|
|
800
|
+
const textPart = firstUserMessage.parts.find((p) => p.type === "text");
|
|
801
|
+
if (textPart?.text) {
|
|
802
|
+
return textPart.text.slice(0, 50) + (textPart.text.length > 50 ? "..." : "");
|
|
803
|
+
}
|
|
804
|
+
}
|
|
805
|
+
return "Untitled Chat";
|
|
806
|
+
}
|
|
807
|
+
};
|
|
808
|
+
|
|
809
|
+
// src/utils/index.ts
|
|
810
|
+
function extractMessageText(message) {
|
|
811
|
+
return message.parts.filter((part) => part.type === "text").map((part) => part.text).join("\n");
|
|
812
|
+
}
|
|
813
|
+
function hasToolCalls(message) {
|
|
814
|
+
return message.parts.some(
|
|
815
|
+
(part) => part.type.startsWith("tool-") || part.type === "dynamic-tool"
|
|
816
|
+
);
|
|
817
|
+
}
|
|
818
|
+
function getToolCalls(message) {
|
|
819
|
+
return message.parts.filter((part) => part.type.startsWith("tool-") || part.type === "dynamic-tool").map((part) => ({
|
|
820
|
+
toolName: part.type === "dynamic-tool" ? part.toolName : part.type.replace("tool-", ""),
|
|
821
|
+
state: part.state || "unknown",
|
|
822
|
+
input: part.input,
|
|
823
|
+
output: part.output
|
|
824
|
+
}));
|
|
825
|
+
}
|
|
826
|
+
function hasFileAttachments(message) {
|
|
827
|
+
return message.parts.some((part) => part.type === "file");
|
|
828
|
+
}
|
|
829
|
+
function getFileAttachments(message) {
|
|
830
|
+
return message.parts.filter((part) => part.type === "file").map((part) => part);
|
|
831
|
+
}
|
|
832
|
+
function formatTimestamp(timestamp, options) {
|
|
833
|
+
if (!timestamp) return "";
|
|
834
|
+
const date = new Date(timestamp);
|
|
835
|
+
const { format = "short", includeDate = false } = options || {};
|
|
836
|
+
switch (format) {
|
|
837
|
+
case "short":
|
|
838
|
+
return date.toLocaleTimeString([], {
|
|
839
|
+
hour: "2-digit",
|
|
840
|
+
minute: "2-digit",
|
|
841
|
+
...includeDate && {
|
|
842
|
+
month: "short",
|
|
843
|
+
day: "numeric"
|
|
844
|
+
}
|
|
845
|
+
});
|
|
846
|
+
case "long":
|
|
847
|
+
return date.toLocaleString();
|
|
848
|
+
case "relative":
|
|
849
|
+
return formatRelativeTime(timestamp);
|
|
850
|
+
default:
|
|
851
|
+
return date.toLocaleTimeString();
|
|
852
|
+
}
|
|
853
|
+
}
|
|
854
|
+
function formatRelativeTime(timestamp) {
|
|
855
|
+
const now = Date.now();
|
|
856
|
+
const diff = now - timestamp;
|
|
857
|
+
const minute = 60 * 1e3;
|
|
858
|
+
const hour = minute * 60;
|
|
859
|
+
const day = hour * 24;
|
|
860
|
+
const week = day * 7;
|
|
861
|
+
const month = day * 30;
|
|
862
|
+
const year = day * 365;
|
|
863
|
+
if (diff < minute) {
|
|
864
|
+
return "Just now";
|
|
865
|
+
} else if (diff < hour) {
|
|
866
|
+
const minutes = Math.floor(diff / minute);
|
|
867
|
+
return `${minutes}m ago`;
|
|
868
|
+
} else if (diff < day) {
|
|
869
|
+
const hours = Math.floor(diff / hour);
|
|
870
|
+
return `${hours}h ago`;
|
|
871
|
+
} else if (diff < week) {
|
|
872
|
+
const days = Math.floor(diff / day);
|
|
873
|
+
return `${days}d ago`;
|
|
874
|
+
} else if (diff < month) {
|
|
875
|
+
const weeks = Math.floor(diff / week);
|
|
876
|
+
return `${weeks}w ago`;
|
|
877
|
+
} else if (diff < year) {
|
|
878
|
+
const months = Math.floor(diff / month);
|
|
879
|
+
return `${months}mo ago`;
|
|
880
|
+
} else {
|
|
881
|
+
const years = Math.floor(diff / year);
|
|
882
|
+
return `${years}y ago`;
|
|
883
|
+
}
|
|
884
|
+
}
|
|
885
|
+
function formatFileSize(bytes) {
|
|
886
|
+
if (bytes === 0) return "0 B";
|
|
887
|
+
const k = 1024;
|
|
888
|
+
const sizes = ["B", "KB", "MB", "GB", "TB"];
|
|
889
|
+
const i = Math.floor(Math.log(bytes) / Math.log(k));
|
|
890
|
+
return parseFloat((bytes / Math.pow(k, i)).toFixed(1)) + " " + sizes[i];
|
|
891
|
+
}
|
|
892
|
+
function getFileExtension(filename) {
|
|
893
|
+
return filename.split(".").pop()?.toLowerCase() || "";
|
|
894
|
+
}
|
|
895
|
+
function isFileTypeAccepted(file, acceptedTypes) {
|
|
896
|
+
return acceptedTypes.some((type) => {
|
|
897
|
+
if (type.endsWith("/*")) {
|
|
898
|
+
return file.type.startsWith(type.slice(0, -1));
|
|
899
|
+
}
|
|
900
|
+
return file.type === type;
|
|
901
|
+
});
|
|
902
|
+
}
|
|
903
|
+
function createFileUrl(file) {
|
|
904
|
+
return URL.createObjectURL(file);
|
|
905
|
+
}
|
|
906
|
+
function revokeFileUrl(url) {
|
|
907
|
+
URL.revokeObjectURL(url);
|
|
908
|
+
}
|
|
909
|
+
function truncateText(text, maxLength) {
|
|
910
|
+
if (text.length <= maxLength) return text;
|
|
911
|
+
return text.slice(0, maxLength - 3) + "...";
|
|
912
|
+
}
|
|
913
|
+
function highlightSearchTerms(text, searchTerms) {
|
|
914
|
+
if (!searchTerms.length) return text;
|
|
915
|
+
let highlightedText = text;
|
|
916
|
+
searchTerms.forEach((term) => {
|
|
917
|
+
const regex = new RegExp(`(${term})`, "gi");
|
|
918
|
+
highlightedText = highlightedText.replace(regex, "<mark>$1</mark>");
|
|
919
|
+
});
|
|
920
|
+
return highlightedText;
|
|
921
|
+
}
|
|
922
|
+
function camelToSpaced(str) {
|
|
923
|
+
return str.replace(/([A-Z])/g, " $1").trim();
|
|
924
|
+
}
|
|
925
|
+
function isValidUrl(string) {
|
|
926
|
+
try {
|
|
927
|
+
new URL(string);
|
|
928
|
+
return true;
|
|
929
|
+
} catch {
|
|
930
|
+
return false;
|
|
931
|
+
}
|
|
932
|
+
}
|
|
933
|
+
function isValidJson(string) {
|
|
934
|
+
try {
|
|
935
|
+
JSON.parse(string);
|
|
936
|
+
return true;
|
|
937
|
+
} catch {
|
|
938
|
+
return false;
|
|
939
|
+
}
|
|
940
|
+
}
|
|
941
|
+
function safeJsonParse(string, fallback) {
|
|
942
|
+
try {
|
|
943
|
+
return JSON.parse(string);
|
|
944
|
+
} catch {
|
|
945
|
+
return fallback;
|
|
946
|
+
}
|
|
947
|
+
}
|
|
948
|
+
function cn(...classes) {
|
|
949
|
+
return classes.filter(Boolean).join(" ");
|
|
950
|
+
}
|
|
951
|
+
async function safeAsync(fn, fallback) {
|
|
952
|
+
try {
|
|
953
|
+
const data = await fn();
|
|
954
|
+
return { data };
|
|
955
|
+
} catch (error) {
|
|
956
|
+
return {
|
|
957
|
+
error: error instanceof Error ? error : new Error(String(error)),
|
|
958
|
+
...fallback !== void 0 && { data: fallback }
|
|
959
|
+
};
|
|
960
|
+
}
|
|
961
|
+
}
|
|
962
|
+
function createErrorMessage(error) {
|
|
963
|
+
if (error instanceof Error) {
|
|
964
|
+
return error.message;
|
|
965
|
+
}
|
|
966
|
+
if (typeof error === "string") {
|
|
967
|
+
return error;
|
|
968
|
+
}
|
|
969
|
+
return "An unknown error occurred";
|
|
970
|
+
}
|
|
971
|
+
function debounce(func, delay) {
|
|
972
|
+
let timeoutId;
|
|
973
|
+
return function debouncedFunction(...args) {
|
|
974
|
+
clearTimeout(timeoutId);
|
|
975
|
+
timeoutId = setTimeout(() => func.apply(this, args), delay);
|
|
976
|
+
};
|
|
977
|
+
}
|
|
978
|
+
function throttle(func, delay) {
|
|
979
|
+
let inThrottle;
|
|
980
|
+
return function throttledFunction(...args) {
|
|
981
|
+
if (!inThrottle) {
|
|
982
|
+
func.apply(this, args);
|
|
983
|
+
inThrottle = true;
|
|
984
|
+
setTimeout(() => inThrottle = false, delay);
|
|
985
|
+
}
|
|
986
|
+
};
|
|
987
|
+
}
|
|
988
|
+
var storage = {
|
|
989
|
+
get(key, fallback) {
|
|
990
|
+
if (typeof window === "undefined") return fallback || null;
|
|
991
|
+
try {
|
|
992
|
+
const item = localStorage.getItem(key);
|
|
993
|
+
return item ? JSON.parse(item) : fallback || null;
|
|
994
|
+
} catch {
|
|
995
|
+
return fallback || null;
|
|
996
|
+
}
|
|
997
|
+
},
|
|
998
|
+
set(key, value) {
|
|
999
|
+
if (typeof window === "undefined") return false;
|
|
1000
|
+
try {
|
|
1001
|
+
localStorage.setItem(key, JSON.stringify(value));
|
|
1002
|
+
return true;
|
|
1003
|
+
} catch {
|
|
1004
|
+
return false;
|
|
1005
|
+
}
|
|
1006
|
+
},
|
|
1007
|
+
remove(key) {
|
|
1008
|
+
if (typeof window === "undefined") return false;
|
|
1009
|
+
try {
|
|
1010
|
+
localStorage.removeItem(key);
|
|
1011
|
+
return true;
|
|
1012
|
+
} catch {
|
|
1013
|
+
return false;
|
|
1014
|
+
}
|
|
1015
|
+
},
|
|
1016
|
+
clear() {
|
|
1017
|
+
if (typeof window === "undefined") return false;
|
|
1018
|
+
try {
|
|
1019
|
+
localStorage.clear();
|
|
1020
|
+
return true;
|
|
1021
|
+
} catch {
|
|
1022
|
+
return false;
|
|
1023
|
+
}
|
|
1024
|
+
}
|
|
1025
|
+
};
|
|
1026
|
+
|
|
1027
|
+
// src/types/index.ts
|
|
1028
|
+
var aimoduleError = class extends Error {
|
|
1029
|
+
constructor(message, code, details) {
|
|
1030
|
+
super(message);
|
|
1031
|
+
this.code = code;
|
|
1032
|
+
this.details = details;
|
|
1033
|
+
this.name = "aimoduleError";
|
|
1034
|
+
}
|
|
1035
|
+
};
|
|
1036
|
+
var aimoduleRuntimeError = class extends aimoduleError {
|
|
1037
|
+
constructor(message, details) {
|
|
1038
|
+
super(message, "RUNTIME_ERROR", details);
|
|
1039
|
+
this.name = "aimoduleRuntimeError";
|
|
1040
|
+
}
|
|
1041
|
+
};
|
|
1042
|
+
|
|
1043
|
+
// src/index.ts
|
|
1044
|
+
async function createEntityAIModule() {
|
|
1045
|
+
const { EntityAIModule: EntityAIModule2 } = await import("./ai.module-TTPMTPB3.mjs");
|
|
1046
|
+
return new EntityAIModule2();
|
|
1047
|
+
}
|
|
1048
|
+
var VERSION = "1.0.0";
|
|
1049
|
+
var PACKAGE_NAME = "@scenemesh/entity-engine-aimodule";
|
|
1050
|
+
var AI_SDK_COMPATIBLE_VERSION = "^5.0.10";
|
|
1051
|
+
async function createaimodule(config) {
|
|
1052
|
+
const { createAICore: createAICore2 } = await import("./core-ANYRS6EF.mjs");
|
|
1053
|
+
const core = await createAICore2({
|
|
1054
|
+
providers: config?.providers,
|
|
1055
|
+
settings: config?.settings,
|
|
1056
|
+
enableEmbeddings: config?.enableEmbeddings,
|
|
1057
|
+
enableTools: config?.enableTools
|
|
1058
|
+
});
|
|
1059
|
+
const api = {
|
|
1060
|
+
generateText: (options) => core.aiSDK.generateText(options),
|
|
1061
|
+
streamText: (options) => core.aiSDK.streamText(options),
|
|
1062
|
+
generateObject: (options) => core.structuredData.generateObject(options),
|
|
1063
|
+
streamObject: (options) => core.structuredData.streamObject(options)
|
|
1064
|
+
};
|
|
1065
|
+
return {
|
|
1066
|
+
core,
|
|
1067
|
+
api
|
|
1068
|
+
};
|
|
1069
|
+
}
|
|
1070
|
+
async function createQuickAI(providers) {
|
|
1071
|
+
return createaimodule({
|
|
1072
|
+
providers,
|
|
1073
|
+
enableEmbeddings: true,
|
|
1074
|
+
enableTools: true
|
|
1075
|
+
});
|
|
1076
|
+
}
|
|
1077
|
+
var EntityEngineAIModule = {
|
|
1078
|
+
// 工厂函数
|
|
1079
|
+
create: createaimodule,
|
|
1080
|
+
createQuick: createQuickAI,
|
|
1081
|
+
// 版本信息
|
|
1082
|
+
VERSION,
|
|
1083
|
+
PACKAGE_NAME,
|
|
1084
|
+
AI_SDK_COMPATIBLE_VERSION,
|
|
1085
|
+
// 核心类导出(用于高级使用)
|
|
1086
|
+
Core: {
|
|
1087
|
+
AIProviderManagement: import("./ai-provider-3PSCVEEN.mjs").then((m) => m.AIProviderManagement),
|
|
1088
|
+
AISDKIntegration: import("./ai-core-LBGYFGOK.mjs").then((m) => m.AISDKIntegration),
|
|
1089
|
+
AIToolsIntegration: import("./ai-tools-JAPVYQGE.mjs").then((m) => m.AIToolsIntegration),
|
|
1090
|
+
AIEmbeddingsIntegration: import("./ai-embeddings-5ED5LDXX.mjs").then((m) => m.AIEmbeddingsIntegration),
|
|
1091
|
+
AISettingsManagement: import("./ai-settings-DTXEAB64.mjs").then((m) => m.AISettingsManagement),
|
|
1092
|
+
AIStructuredDataIntegration: import("./ai-structured-EGZ26ZS4.mjs").then((m) => m.AIStructuredDataIntegration)
|
|
1093
|
+
}
|
|
1094
|
+
};
|
|
1095
|
+
export {
|
|
1096
|
+
AICoreManager,
|
|
1097
|
+
AIEmbeddingsIntegration,
|
|
1098
|
+
AIProviderManagement,
|
|
1099
|
+
AISDKIntegration,
|
|
1100
|
+
AISettingsManagement,
|
|
1101
|
+
AIStructuredDataIntegration,
|
|
1102
|
+
AIToolsIntegration,
|
|
1103
|
+
AI_SDK_COMPATIBLE_VERSION,
|
|
1104
|
+
BuiltinSettingsPresets,
|
|
1105
|
+
CORE_VERSION,
|
|
1106
|
+
EntityAIModule,
|
|
1107
|
+
EntityEngineAIModule,
|
|
1108
|
+
InMemoryMessageStore,
|
|
1109
|
+
InvalidToolInputError,
|
|
1110
|
+
LocalStorageMessageStore,
|
|
1111
|
+
NoSuchToolError,
|
|
1112
|
+
PACKAGE_NAME,
|
|
1113
|
+
StreamStateManager,
|
|
1114
|
+
SupportedEmbeddingModels,
|
|
1115
|
+
TypeValidationError,
|
|
1116
|
+
UIMessageStreamReader,
|
|
1117
|
+
VERSION,
|
|
1118
|
+
aimoduleError,
|
|
1119
|
+
aimoduleRuntimeError,
|
|
1120
|
+
camelToSpaced,
|
|
1121
|
+
clearWarnings,
|
|
1122
|
+
cn,
|
|
1123
|
+
cosineSimilarity,
|
|
1124
|
+
createAICore,
|
|
1125
|
+
createEntityAIModule,
|
|
1126
|
+
createErrorMessage,
|
|
1127
|
+
createFileUrl,
|
|
1128
|
+
createIdGenerator,
|
|
1129
|
+
createProviderRegistry,
|
|
1130
|
+
createQuickAI,
|
|
1131
|
+
createaimodule,
|
|
1132
|
+
customProvider,
|
|
1133
|
+
debounce,
|
|
1134
|
+
defaultSettingsMiddleware,
|
|
1135
|
+
embed,
|
|
1136
|
+
embedMany,
|
|
1137
|
+
extractMessageText,
|
|
1138
|
+
formatFileSize,
|
|
1139
|
+
formatRelativeTime,
|
|
1140
|
+
formatTimestamp,
|
|
1141
|
+
generateId,
|
|
1142
|
+
generateObject,
|
|
1143
|
+
generateText,
|
|
1144
|
+
getFileAttachments,
|
|
1145
|
+
getFileExtension,
|
|
1146
|
+
getToolCalls,
|
|
1147
|
+
getWarningStats,
|
|
1148
|
+
getWarnings,
|
|
1149
|
+
hasFileAttachments,
|
|
1150
|
+
hasToolCalls,
|
|
1151
|
+
highlightSearchTerms,
|
|
1152
|
+
isFileTypeAccepted,
|
|
1153
|
+
isValidJson,
|
|
1154
|
+
isValidUrl,
|
|
1155
|
+
readUIMessageStream,
|
|
1156
|
+
resumeStream,
|
|
1157
|
+
revokeFileUrl,
|
|
1158
|
+
safeAsync,
|
|
1159
|
+
safeJsonParse,
|
|
1160
|
+
setWarningHandler,
|
|
1161
|
+
smoothStream,
|
|
1162
|
+
storage,
|
|
1163
|
+
streamObject,
|
|
1164
|
+
streamText,
|
|
1165
|
+
throttle,
|
|
1166
|
+
tool,
|
|
1167
|
+
truncateText,
|
|
1168
|
+
validateUIMessages,
|
|
1169
|
+
warn,
|
|
1170
|
+
warnDeprecatedFeature,
|
|
1171
|
+
warnPerformance,
|
|
1172
|
+
warnUnsupportedSetting,
|
|
1173
|
+
warnUnsupportedTool,
|
|
1174
|
+
warnValidationError,
|
|
1175
|
+
wrapLanguageModel
|
|
1176
|
+
};
|
|
1177
|
+
//# sourceMappingURL=index.mjs.map
|