@anvia/core 0.17.0 → 0.20.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/dist/agent/index.d.ts +6 -14
- package/dist/agent/index.js +11 -9
- package/dist/{agent-Cg0UigNK.d.ts → agent-Q7FT1xmG.d.ts} +4 -23
- package/dist/{chunk-54S4I3SX.js → chunk-47UWISJU.js} +5 -5
- package/dist/{chunk-F3C5653H.js → chunk-4BGN6PYF.js} +5 -5
- package/dist/{chunk-BQCTJSNL.js → chunk-6GIREB6Z.js} +6 -11
- package/dist/chunk-6GIREB6Z.js.map +1 -0
- package/dist/{chunk-77D7XBWL.js → chunk-BCA4VZ5W.js} +2 -2
- package/dist/{chunk-RWAOHGRL.js → chunk-EOIQXFGK.js} +11 -17
- package/dist/chunk-EOIQXFGK.js.map +1 -0
- package/dist/{chunk-GCED5ZNM.js → chunk-F56AMXMQ.js} +2 -131
- package/dist/chunk-F56AMXMQ.js.map +1 -0
- package/dist/{chunk-OPAEENGD.js → chunk-GKPJ6FUL.js} +168 -39
- package/dist/chunk-GKPJ6FUL.js.map +1 -0
- package/dist/{chunk-SWUIBS4N.js → chunk-MB55EXYB.js} +5 -9
- package/dist/{chunk-SWUIBS4N.js.map → chunk-MB55EXYB.js.map} +1 -1
- package/dist/{chunk-YBBD4HZ3.js → chunk-MNWK2USR.js} +119 -15
- package/dist/chunk-MNWK2USR.js.map +1 -0
- package/dist/chunk-MTGNA4OS.js +138 -0
- package/dist/chunk-MTGNA4OS.js.map +1 -0
- package/dist/chunk-TGLNXVII.js +220 -0
- package/dist/chunk-TGLNXVII.js.map +1 -0
- package/dist/chunk-XWUC7CIT.js +1 -0
- package/dist/chunk-XWUC7CIT.js.map +1 -0
- package/dist/chunk-ZNTIUOKL.js +33 -0
- package/dist/chunk-ZNTIUOKL.js.map +1 -0
- package/dist/completion/index.js +6 -4
- package/dist/embeddings/index.d.ts +6 -3
- package/dist/embeddings/index.js +7 -1
- package/dist/errors-Bf661V89.d.ts +17 -0
- package/dist/evals/index.d.ts +5 -5
- package/dist/evals/index.js +13 -11
- package/dist/evals/index.js.map +1 -1
- package/dist/extractor/index.d.ts +5 -5
- package/dist/extractor/index.js +12 -10
- package/dist/index.d.ts +6 -5
- package/dist/index.js +23 -13
- package/dist/internal/agent.d.ts +5 -5
- package/dist/internal/agent.js +9 -7
- package/dist/memory/index.d.ts +3 -2
- package/dist/memory/index.js +15 -1
- package/dist/{middleware-Cp0tm6zE.d.ts → middleware-DfMc30in.d.ts} +3 -15
- package/dist/pipeline/index.d.ts +5 -5
- package/dist/request/index.d.ts +5 -5
- package/dist/request/index.js +8 -6
- package/dist/skills/index.js +5 -5
- package/dist/tool/index.d.ts +3 -3
- package/dist/tool/index.js +4 -6
- package/dist/types-BCTRUGex.d.ts +47 -0
- package/dist/{types-DMGvEFRf.d.ts → types-BrxxAtcJ.d.ts} +1 -1
- package/dist/types-DBHzPsbx.d.ts +119 -0
- package/dist/vector-store/index.d.ts +3 -3
- package/dist/vector-store/index.js +3 -3
- package/package.json +1 -1
- package/dist/chunk-BQCTJSNL.js.map +0 -1
- package/dist/chunk-GCED5ZNM.js.map +0 -1
- package/dist/chunk-M6RPFHG7.js +0 -11
- package/dist/chunk-M6RPFHG7.js.map +0 -1
- package/dist/chunk-OPAEENGD.js.map +0 -1
- package/dist/chunk-RWAOHGRL.js.map +0 -1
- package/dist/chunk-YBBD4HZ3.js.map +0 -1
- package/dist/types-CBmhLT_r.d.ts +0 -72
- package/dist/types-DHHZztGO.d.ts +0 -25
- /package/dist/{chunk-54S4I3SX.js.map → chunk-47UWISJU.js.map} +0 -0
- /package/dist/{chunk-F3C5653H.js.map → chunk-4BGN6PYF.js.map} +0 -0
- /package/dist/{chunk-77D7XBWL.js.map → chunk-BCA4VZ5W.js.map} +0 -0
|
@@ -4,18 +4,23 @@ import {
|
|
|
4
4
|
import {
|
|
5
5
|
parseToolArgs,
|
|
6
6
|
toolResultContentToText
|
|
7
|
-
} from "./chunk-
|
|
7
|
+
} from "./chunk-MB55EXYB.js";
|
|
8
8
|
import {
|
|
9
9
|
runControl,
|
|
10
10
|
toolCallControl
|
|
11
11
|
} from "./chunk-2ODTMRHP.js";
|
|
12
12
|
import {
|
|
13
|
-
|
|
14
|
-
|
|
13
|
+
MemoryCompactionConflictError,
|
|
14
|
+
MemoryCompactionError,
|
|
15
|
+
createMemoryCompactionSummary,
|
|
16
|
+
cumulativeCompactedMessageCount
|
|
17
|
+
} from "./chunk-TGLNXVII.js";
|
|
18
|
+
import {
|
|
19
|
+
CompletionRequestBuilder
|
|
20
|
+
} from "./chunk-MTGNA4OS.js";
|
|
15
21
|
import {
|
|
16
|
-
CompletionRequestBuilder,
|
|
17
22
|
isStreamingCompletionModel
|
|
18
|
-
} from "./chunk-
|
|
23
|
+
} from "./chunk-F56AMXMQ.js";
|
|
19
24
|
import {
|
|
20
25
|
AssistantContent,
|
|
21
26
|
Message,
|
|
@@ -25,6 +30,9 @@ import {
|
|
|
25
30
|
getAssistantGenerationMetadata,
|
|
26
31
|
textFromAssistantContent
|
|
27
32
|
} from "./chunk-PJDL5UT2.js";
|
|
33
|
+
import {
|
|
34
|
+
mapWithConcurrency
|
|
35
|
+
} from "./chunk-OIMLU4SF.js";
|
|
28
36
|
import {
|
|
29
37
|
appendGuardrailPolicies,
|
|
30
38
|
hasEnforcedOutputGuardrails,
|
|
@@ -151,9 +159,13 @@ var PromptRequestMemory = class {
|
|
|
151
159
|
async prepareRun(runId, newMessages) {
|
|
152
160
|
const memory = this.memory();
|
|
153
161
|
if (memory === void 0 || this.memoryContext === void 0) {
|
|
154
|
-
return
|
|
162
|
+
return {
|
|
163
|
+
history: this.initialHistory,
|
|
164
|
+
usage: Usage.empty()
|
|
165
|
+
};
|
|
155
166
|
}
|
|
156
|
-
const
|
|
167
|
+
const preparation = await this.prepareStoredHistory(memory, runId, newMessages.length);
|
|
168
|
+
const memoryHistory = preparation.history;
|
|
157
169
|
const chatHistory = [...memoryHistory, ...this.initialHistory];
|
|
158
170
|
if (memory.options.savePolicy === "message") {
|
|
159
171
|
await memory.store.append({
|
|
@@ -163,7 +175,10 @@ var PromptRequestMemory = class {
|
|
|
163
175
|
messages: newMessages
|
|
164
176
|
});
|
|
165
177
|
}
|
|
166
|
-
return
|
|
178
|
+
return {
|
|
179
|
+
...preparation,
|
|
180
|
+
history: chatHistory
|
|
181
|
+
};
|
|
167
182
|
}
|
|
168
183
|
async commitMessages(runId, turn, messages, pendingTurnMessages) {
|
|
169
184
|
const memory = this.memory();
|
|
@@ -222,7 +237,119 @@ var PromptRequestMemory = class {
|
|
|
222
237
|
memory() {
|
|
223
238
|
return this.memoryContext === void 0 ? void 0 : this.agent.memory;
|
|
224
239
|
}
|
|
240
|
+
async prepareStoredHistory(memory, runId, incomingMessageCount) {
|
|
241
|
+
const context = this.memoryContext;
|
|
242
|
+
if (context === void 0) {
|
|
243
|
+
return { history: [], usage: Usage.empty() };
|
|
244
|
+
}
|
|
245
|
+
const options = memory.options.compaction;
|
|
246
|
+
const capability = memory.store.compaction;
|
|
247
|
+
if (options === void 0 || capability === void 0) {
|
|
248
|
+
return {
|
|
249
|
+
history: await memory.store.load(context),
|
|
250
|
+
usage: Usage.empty()
|
|
251
|
+
};
|
|
252
|
+
}
|
|
253
|
+
let usage = Usage.empty();
|
|
254
|
+
let conflictRetries = 0;
|
|
255
|
+
for (let attempt = 0; attempt <= options.conflictRetries; attempt += 1) {
|
|
256
|
+
const snapshot = await capability.load(context);
|
|
257
|
+
const compactedMessageCount = compactedPrefixLength(
|
|
258
|
+
snapshot.messages,
|
|
259
|
+
incomingMessageCount,
|
|
260
|
+
options.maxMessages,
|
|
261
|
+
options.keepRecentUserTurns
|
|
262
|
+
);
|
|
263
|
+
if (compactedMessageCount === 0) {
|
|
264
|
+
return { history: snapshot.messages, usage };
|
|
265
|
+
}
|
|
266
|
+
const prefix = snapshot.messages.slice(0, compactedMessageCount);
|
|
267
|
+
let result;
|
|
268
|
+
try {
|
|
269
|
+
result = await options.compactor({
|
|
270
|
+
context,
|
|
271
|
+
messages: prefix
|
|
272
|
+
});
|
|
273
|
+
} catch (error) {
|
|
274
|
+
throw remappedCompactorError(error, usage);
|
|
275
|
+
}
|
|
276
|
+
if (result.usage !== void 0) {
|
|
277
|
+
usage = Usage.add(usage, result.usage);
|
|
278
|
+
}
|
|
279
|
+
const summaryText = result.summary.trim();
|
|
280
|
+
if (summaryText.length === 0) {
|
|
281
|
+
throw new MemoryCompactionError("Memory compactor returned an empty summary.", {
|
|
282
|
+
usage
|
|
283
|
+
});
|
|
284
|
+
}
|
|
285
|
+
const summary = createMemoryCompactionSummary(
|
|
286
|
+
summaryText,
|
|
287
|
+
cumulativeCompactedMessageCount(prefix)
|
|
288
|
+
);
|
|
289
|
+
let commit;
|
|
290
|
+
try {
|
|
291
|
+
commit = await capability.commit({
|
|
292
|
+
context,
|
|
293
|
+
revision: snapshot.revision,
|
|
294
|
+
compactedMessageCount,
|
|
295
|
+
summary,
|
|
296
|
+
runId: `memory-compaction:${runId}:${attempt + 1}`
|
|
297
|
+
});
|
|
298
|
+
} catch (error) {
|
|
299
|
+
throw new MemoryCompactionError("Memory compaction store commit failed.", {
|
|
300
|
+
cause: error,
|
|
301
|
+
usage
|
|
302
|
+
});
|
|
303
|
+
}
|
|
304
|
+
if (commit === "committed") {
|
|
305
|
+
const retained = snapshot.messages.slice(compactedMessageCount);
|
|
306
|
+
return {
|
|
307
|
+
history: [summary, ...retained],
|
|
308
|
+
usage,
|
|
309
|
+
compaction: {
|
|
310
|
+
originalMessageCount: snapshot.messages.length,
|
|
311
|
+
compactedMessageCount,
|
|
312
|
+
retainedMessageCount: retained.length,
|
|
313
|
+
conflictRetries
|
|
314
|
+
}
|
|
315
|
+
};
|
|
316
|
+
}
|
|
317
|
+
conflictRetries += 1;
|
|
318
|
+
}
|
|
319
|
+
throw new MemoryCompactionConflictError(options.conflictRetries + 1, usage);
|
|
320
|
+
}
|
|
225
321
|
};
|
|
322
|
+
function compactedPrefixLength(messages, incomingMessageCount, maxMessages, keepRecentUserTurns) {
|
|
323
|
+
if (messages.length + incomingMessageCount <= maxMessages) {
|
|
324
|
+
return 0;
|
|
325
|
+
}
|
|
326
|
+
const userMessageIndexes = messages.flatMap(
|
|
327
|
+
(message, index) => message.role === "user" ? [index] : []
|
|
328
|
+
);
|
|
329
|
+
if (userMessageIndexes.length <= keepRecentUserTurns) {
|
|
330
|
+
return 0;
|
|
331
|
+
}
|
|
332
|
+
return userMessageIndexes[userMessageIndexes.length - keepRecentUserTurns] ?? 0;
|
|
333
|
+
}
|
|
334
|
+
function remappedCompactorError(error, usage) {
|
|
335
|
+
if (error instanceof MemoryCompactionError) {
|
|
336
|
+
const mergedUsage = error.usage === void 0 ? usage : isEmptyUsage(usage) ? error.usage : Usage.add(usage, error.usage);
|
|
337
|
+
if (mergedUsage === error.usage || error.usage === void 0 && isEmptyUsage(usage)) {
|
|
338
|
+
return error;
|
|
339
|
+
}
|
|
340
|
+
return new MemoryCompactionError(error.message, {
|
|
341
|
+
cause: error,
|
|
342
|
+
usage: mergedUsage
|
|
343
|
+
});
|
|
344
|
+
}
|
|
345
|
+
return new MemoryCompactionError("Memory compactor failed.", {
|
|
346
|
+
cause: error,
|
|
347
|
+
usage
|
|
348
|
+
});
|
|
349
|
+
}
|
|
350
|
+
function isEmptyUsage(usage) {
|
|
351
|
+
return usage.inputTokens === 0 && usage.outputTokens === 0 && usage.totalTokens === 0 && usage.cachedInputTokens === 0 && usage.cacheCreationInputTokens === 0;
|
|
352
|
+
}
|
|
226
353
|
|
|
227
354
|
// src/internal/prompt-runtime/retrieval.ts
|
|
228
355
|
async function fetchDynamicContext(agent, ragText) {
|
|
@@ -969,16 +1096,6 @@ var ToolCallExecutor = class {
|
|
|
969
1096
|
}
|
|
970
1097
|
replaced = true;
|
|
971
1098
|
}
|
|
972
|
-
const legacyReplacement = await middleware.onResult?.({
|
|
973
|
-
...args,
|
|
974
|
-
result,
|
|
975
|
-
structuredResult
|
|
976
|
-
});
|
|
977
|
-
if (legacyReplacement !== void 0) {
|
|
978
|
-
result = legacyReplacement;
|
|
979
|
-
structuredResult = void 0;
|
|
980
|
-
replaced = true;
|
|
981
|
-
}
|
|
982
1099
|
}
|
|
983
1100
|
return replaced ? structuredResult ?? result : void 0;
|
|
984
1101
|
}
|
|
@@ -1555,12 +1672,6 @@ var PromptRequest = class _PromptRequest {
|
|
|
1555
1672
|
this.guardrailPolicies = appendGuardrailPolicies(this.guardrailPolicies, policies);
|
|
1556
1673
|
return this;
|
|
1557
1674
|
}
|
|
1558
|
-
/**
|
|
1559
|
-
* @deprecated Use `withHook` instead.
|
|
1560
|
-
*/
|
|
1561
|
-
requestHook(hook) {
|
|
1562
|
-
return this.withHook(hook);
|
|
1563
|
-
}
|
|
1564
1675
|
withToolConcurrency(concurrency) {
|
|
1565
1676
|
this.concurrency = Math.max(1, concurrency);
|
|
1566
1677
|
return this;
|
|
@@ -1573,18 +1684,6 @@ var PromptRequest = class _PromptRequest {
|
|
|
1573
1684
|
this.requestMiddlewares.push(...middlewares);
|
|
1574
1685
|
return this;
|
|
1575
1686
|
}
|
|
1576
|
-
/**
|
|
1577
|
-
* @deprecated Use `withMiddleware` instead.
|
|
1578
|
-
*/
|
|
1579
|
-
withToolMiddleware(middleware) {
|
|
1580
|
-
return this.withMiddleware(middleware);
|
|
1581
|
-
}
|
|
1582
|
-
/**
|
|
1583
|
-
* @deprecated Use `withMiddlewares` instead.
|
|
1584
|
-
*/
|
|
1585
|
-
withToolMiddlewares(middlewares) {
|
|
1586
|
-
return this.withMiddlewares(middlewares);
|
|
1587
|
-
}
|
|
1588
1687
|
withTrace(trace) {
|
|
1589
1688
|
this.traceOptions = trace;
|
|
1590
1689
|
return this;
|
|
@@ -1628,7 +1727,10 @@ var PromptRequest = class _PromptRequest {
|
|
|
1628
1727
|
return result;
|
|
1629
1728
|
}
|
|
1630
1729
|
newMessages = [this.promptMessage];
|
|
1631
|
-
|
|
1730
|
+
const memoryPreparation = await this.memoryRecorder.prepareRun(runId, newMessages);
|
|
1731
|
+
this.chatHistory = memoryPreparation.history;
|
|
1732
|
+
usage = Usage.add(usage, memoryPreparation.usage);
|
|
1733
|
+
await this.recordMemoryCompaction(memoryPreparation, runObservers);
|
|
1632
1734
|
const pendingTurnMessages = this.memoryRecorder.pendingTurnMessages(newMessages);
|
|
1633
1735
|
await this.runRunStartHook(newMessages);
|
|
1634
1736
|
while (currentTurns <= this.maxTurnCount + 1) {
|
|
@@ -1744,6 +1846,9 @@ var PromptRequest = class _PromptRequest {
|
|
|
1744
1846
|
}
|
|
1745
1847
|
throw new MaxTurnsError(this.maxTurnCount, [...this.chatHistory, ...newMessages], lastPrompt);
|
|
1746
1848
|
} catch (error) {
|
|
1849
|
+
if (error instanceof MemoryCompactionError && error.usage !== void 0) {
|
|
1850
|
+
usage = Usage.add(usage, error.usage);
|
|
1851
|
+
}
|
|
1747
1852
|
const finalError = await this.runRunErrorHook(error, usage, newMessages);
|
|
1748
1853
|
this.runState = finalError instanceof PromptCancelledError ? "cancelled" : "errored";
|
|
1749
1854
|
await runObservers.error({ error: finalError, usage, messages: [...newMessages] });
|
|
@@ -1801,7 +1906,10 @@ var PromptRequest = class _PromptRequest {
|
|
|
1801
1906
|
return;
|
|
1802
1907
|
}
|
|
1803
1908
|
newMessages = [this.promptMessage];
|
|
1804
|
-
|
|
1909
|
+
const memoryPreparation = await this.memoryRecorder.prepareRun(runId, newMessages);
|
|
1910
|
+
this.chatHistory = memoryPreparation.history;
|
|
1911
|
+
usage = Usage.add(usage, memoryPreparation.usage);
|
|
1912
|
+
await this.recordMemoryCompaction(memoryPreparation, runObservers);
|
|
1805
1913
|
const pendingTurnMessages = this.memoryRecorder.pendingTurnMessages(newMessages);
|
|
1806
1914
|
await this.runRunStartHook(newMessages);
|
|
1807
1915
|
while (currentTurns <= this.maxTurnCount + 1) {
|
|
@@ -2073,6 +2181,9 @@ var PromptRequest = class _PromptRequest {
|
|
|
2073
2181
|
}
|
|
2074
2182
|
throw new MaxTurnsError(this.maxTurnCount, [...this.chatHistory, ...newMessages], lastPrompt);
|
|
2075
2183
|
} catch (error) {
|
|
2184
|
+
if (error instanceof MemoryCompactionError && error.usage !== void 0) {
|
|
2185
|
+
usage = Usage.add(usage, error.usage);
|
|
2186
|
+
}
|
|
2076
2187
|
const finalError = await this.runRunErrorHook(error, usage, newMessages);
|
|
2077
2188
|
this.runState = finalError instanceof PromptCancelledError ? "cancelled" : "errored";
|
|
2078
2189
|
const finalUsage = usage;
|
|
@@ -2244,6 +2355,24 @@ var PromptRequest = class _PromptRequest {
|
|
|
2244
2355
|
attributes: guardrailDecisionAttributes(decision)
|
|
2245
2356
|
});
|
|
2246
2357
|
}
|
|
2358
|
+
async recordMemoryCompaction(preparation, runObservers) {
|
|
2359
|
+
const compaction = preparation.compaction;
|
|
2360
|
+
if (compaction === void 0) {
|
|
2361
|
+
return;
|
|
2362
|
+
}
|
|
2363
|
+
await runObservers.event({
|
|
2364
|
+
name: "memory.compaction",
|
|
2365
|
+
attributes: {
|
|
2366
|
+
originalMessageCount: compaction.originalMessageCount,
|
|
2367
|
+
compactedMessageCount: compaction.compactedMessageCount,
|
|
2368
|
+
retainedMessageCount: compaction.retainedMessageCount,
|
|
2369
|
+
conflictRetries: compaction.conflictRetries,
|
|
2370
|
+
inputTokens: preparation.usage.inputTokens,
|
|
2371
|
+
outputTokens: preparation.usage.outputTokens,
|
|
2372
|
+
totalTokens: preparation.usage.totalTokens
|
|
2373
|
+
}
|
|
2374
|
+
});
|
|
2375
|
+
}
|
|
2247
2376
|
guardrailRunContext(runId) {
|
|
2248
2377
|
const context = {
|
|
2249
2378
|
agentId: this.agent.id,
|
|
@@ -2593,4 +2722,4 @@ export {
|
|
|
2593
2722
|
extractRagText,
|
|
2594
2723
|
PromptRequest
|
|
2595
2724
|
};
|
|
2596
|
-
//# sourceMappingURL=chunk-
|
|
2725
|
+
//# sourceMappingURL=chunk-GKPJ6FUL.js.map
|