@anvia/core 0.17.0 → 0.18.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.
Files changed (53) hide show
  1. package/dist/agent/index.d.ts +3 -3
  2. package/dist/agent/index.js +11 -9
  3. package/dist/{agent-Cg0UigNK.d.ts → agent-CBz3CRxa.d.ts} +2 -1
  4. package/dist/{chunk-54S4I3SX.js → chunk-3YJXLOGO.js} +5 -5
  5. package/dist/{chunk-OPAEENGD.js → chunk-7LCKIULT.js} +168 -11
  6. package/dist/chunk-7LCKIULT.js.map +1 -0
  7. package/dist/{chunk-77D7XBWL.js → chunk-DC2GJDPR.js} +2 -2
  8. package/dist/{chunk-RWAOHGRL.js → chunk-DPTZ2MK4.js} +11 -5
  9. package/dist/{chunk-RWAOHGRL.js.map → chunk-DPTZ2MK4.js.map} +1 -1
  10. package/dist/{chunk-GCED5ZNM.js → chunk-F56AMXMQ.js} +2 -131
  11. package/dist/chunk-F56AMXMQ.js.map +1 -0
  12. package/dist/{chunk-F3C5653H.js → chunk-I7TM7TKX.js} +4 -4
  13. package/dist/{chunk-SWUIBS4N.js → chunk-JNJBKSLA.js} +5 -5
  14. package/dist/chunk-MTGNA4OS.js +138 -0
  15. package/dist/chunk-MTGNA4OS.js.map +1 -0
  16. package/dist/{chunk-BQCTJSNL.js → chunk-QABIBFKE.js} +5 -5
  17. package/dist/chunk-TGLNXVII.js +220 -0
  18. package/dist/chunk-TGLNXVII.js.map +1 -0
  19. package/dist/chunk-XWUC7CIT.js +1 -0
  20. package/dist/chunk-XWUC7CIT.js.map +1 -0
  21. package/dist/chunk-ZNTIUOKL.js +33 -0
  22. package/dist/chunk-ZNTIUOKL.js.map +1 -0
  23. package/dist/completion/index.js +6 -4
  24. package/dist/errors-Bf661V89.d.ts +17 -0
  25. package/dist/evals/index.d.ts +2 -2
  26. package/dist/evals/index.js +12 -10
  27. package/dist/evals/index.js.map +1 -1
  28. package/dist/extractor/index.d.ts +2 -2
  29. package/dist/extractor/index.js +12 -10
  30. package/dist/index.d.ts +3 -2
  31. package/dist/index.js +22 -10
  32. package/dist/internal/agent.d.ts +2 -2
  33. package/dist/internal/agent.js +9 -7
  34. package/dist/memory/index.d.ts +3 -2
  35. package/dist/memory/index.js +15 -1
  36. package/dist/pipeline/index.d.ts +2 -2
  37. package/dist/request/index.d.ts +2 -2
  38. package/dist/request/index.js +8 -6
  39. package/dist/skills/index.js +5 -5
  40. package/dist/tool/index.js +4 -4
  41. package/dist/types-DBHzPsbx.d.ts +119 -0
  42. package/dist/vector-store/index.js +2 -2
  43. package/package.json +1 -1
  44. package/dist/chunk-GCED5ZNM.js.map +0 -1
  45. package/dist/chunk-M6RPFHG7.js +0 -11
  46. package/dist/chunk-M6RPFHG7.js.map +0 -1
  47. package/dist/chunk-OPAEENGD.js.map +0 -1
  48. package/dist/types-CBmhLT_r.d.ts +0 -72
  49. /package/dist/{chunk-54S4I3SX.js.map → chunk-3YJXLOGO.js.map} +0 -0
  50. /package/dist/{chunk-77D7XBWL.js.map → chunk-DC2GJDPR.js.map} +0 -0
  51. /package/dist/{chunk-F3C5653H.js.map → chunk-I7TM7TKX.js.map} +0 -0
  52. /package/dist/{chunk-SWUIBS4N.js.map → chunk-JNJBKSLA.js.map} +0 -0
  53. /package/dist/{chunk-BQCTJSNL.js.map → chunk-QABIBFKE.js.map} +0 -0
@@ -1,11 +1,11 @@
1
- import { D as DynamicContextOptions, b as DynamicToolOptions, c as AgentEventStore, d as AgentEventStoreOptions, A as Agent } from '../agent-Cg0UigNK.js';
2
- export { e as AgentEventAppendInput, f as AgentEventRecord, g as AgentEventStoreInclude, h as AgentSession, i as AgentToolOptions } from '../agent-Cg0UigNK.js';
1
+ import { D as DynamicContextOptions, b as DynamicToolOptions, c as AgentEventStore, d as AgentEventStoreOptions, A as Agent } from '../agent-CBz3CRxa.js';
2
+ export { e as AgentEventAppendInput, f as AgentEventRecord, g as AgentEventStoreInclude, h as AgentSession, i as AgentToolOptions } from '../agent-CBz3CRxa.js';
3
3
  import { Z as ZodSchema } from '../zod-schema-C7F4clpm.js';
4
4
  import { C as CompletionModel, P as ProviderTool, i as JsonValue, z as ToolChoice } from '../types-XjIh933x.js';
5
5
  import { GuardrailPolicyInput } from '../guardrails/index.js';
6
6
  import { P as PromptHook } from '../types-BCA8p0sb.js';
7
7
  import { a as McpServer } from '../types-BBEf34DP.js';
8
- import { e as MemoryStore, f as MemoryOptions } from '../types-CBmhLT_r.js';
8
+ import { m as MemoryStore, n as MemoryOptions } from '../types-DBHzPsbx.js';
9
9
  import { i as AgentObserver, O as ObserveOptions } from '../index-XTUEMWhU.js';
10
10
  import { a as SkillSet } from '../types-fBv8mhvP.js';
11
11
  import { k as ToolSearchDocument, l as ToolSet, A as AgentMiddleware, e as ToolMiddleware } from '../middleware-Cp0tm6zE.js';
@@ -1,19 +1,21 @@
1
1
  import {
2
2
  AgentBuilder
3
- } from "../chunk-RWAOHGRL.js";
4
- import "../chunk-BQCTJSNL.js";
5
- import "../chunk-OPAEENGD.js";
3
+ } from "../chunk-DPTZ2MK4.js";
4
+ import "../chunk-QABIBFKE.js";
5
+ import "../chunk-7LCKIULT.js";
6
6
  import "../chunk-YK4WAAS4.js";
7
7
  import "../chunk-XUUY2L2D.js";
8
- import "../chunk-SWUIBS4N.js";
9
- import "../chunk-F3C5653H.js";
8
+ import "../chunk-JNJBKSLA.js";
9
+ import "../chunk-I7TM7TKX.js";
10
10
  import "../chunk-2ODTMRHP.js";
11
- import "../chunk-M6RPFHG7.js";
12
- import "../chunk-YBBD4HZ3.js";
13
- import "../chunk-OIMLU4SF.js";
14
- import "../chunk-GCED5ZNM.js";
11
+ import "../chunk-ZNTIUOKL.js";
12
+ import "../chunk-TGLNXVII.js";
13
+ import "../chunk-MTGNA4OS.js";
14
+ import "../chunk-F56AMXMQ.js";
15
15
  import "../chunk-PJDL5UT2.js";
16
16
  import "../chunk-WQKHFADH.js";
17
+ import "../chunk-YBBD4HZ3.js";
18
+ import "../chunk-OIMLU4SF.js";
17
19
  import "../chunk-CWUJUSOS.js";
18
20
  export {
19
21
  AgentBuilder
@@ -1,7 +1,7 @@
1
1
  import { C as CompletionModel, M as Message, D as Document, i as JsonValue, P as ProviderTool, z as ToolChoice, J as JsonObject } from './types-XjIh933x.js';
2
2
  import { GuardrailPolicyInput, GuardrailPolicy } from './guardrails/index.js';
3
3
  import { P as PromptHook } from './types-BCA8p0sb.js';
4
- import { h as MemoryContext, j as MemoryRegistration, S as SessionOptions } from './types-CBmhLT_r.js';
4
+ import { q as MemoryContext, s as MemoryRegistration, u as SessionOptions } from './types-DBHzPsbx.js';
5
5
  import { j as AgentTraceOptions, P as PromptResponse, d as AgentStreamEvent, f as AgentStreamEventWithoutToolCallDeltas, g as AgentStreamOptions, k as AgentObserverRegistration } from './index-XTUEMWhU.js';
6
6
  import { e as ToolApprovalsOptions, T as Tool, A as AnyTool, f as ToolCallContext, N as NormalizedToolOutput } from './tool-Dhwg__1L.js';
7
7
  import { A as AgentMiddleware, e as ToolMiddleware, k as ToolSearchDocument, l as ToolSet } from './middleware-Cp0tm6zE.js';
@@ -83,6 +83,7 @@ declare class PromptRequest<M extends CompletionModel = CompletionModel> {
83
83
  private executeToolCalls;
84
84
  private runOutputGuardrailsForResponse;
85
85
  private recordGuardrailDecision;
86
+ private recordMemoryCompaction;
86
87
  private guardrailRunContext;
87
88
  private startRunObservers;
88
89
  private recordAgentEvent;
@@ -1,15 +1,15 @@
1
1
  import {
2
2
  AgentBuilder
3
- } from "./chunk-RWAOHGRL.js";
3
+ } from "./chunk-DPTZ2MK4.js";
4
4
  import {
5
5
  extractRagText
6
- } from "./chunk-OPAEENGD.js";
6
+ } from "./chunk-7LCKIULT.js";
7
7
  import {
8
8
  createTool
9
- } from "./chunk-F3C5653H.js";
9
+ } from "./chunk-I7TM7TKX.js";
10
10
  import {
11
11
  CompletionRequestBuilder
12
- } from "./chunk-GCED5ZNM.js";
12
+ } from "./chunk-MTGNA4OS.js";
13
13
  import {
14
14
  CompletionCapabilityError,
15
15
  Message,
@@ -138,4 +138,4 @@ export {
138
138
  Extractor,
139
139
  ExtractorBuilder
140
140
  };
141
- //# sourceMappingURL=chunk-54S4I3SX.js.map
141
+ //# sourceMappingURL=chunk-3YJXLOGO.js.map
@@ -4,18 +4,23 @@ import {
4
4
  import {
5
5
  parseToolArgs,
6
6
  toolResultContentToText
7
- } from "./chunk-SWUIBS4N.js";
7
+ } from "./chunk-JNJBKSLA.js";
8
8
  import {
9
9
  runControl,
10
10
  toolCallControl
11
11
  } from "./chunk-2ODTMRHP.js";
12
12
  import {
13
- mapWithConcurrency
14
- } from "./chunk-OIMLU4SF.js";
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-GCED5ZNM.js";
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 this.initialHistory;
162
+ return {
163
+ history: this.initialHistory,
164
+ usage: Usage.empty()
165
+ };
155
166
  }
156
- const memoryHistory = await memory.store.load(this.memoryContext);
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 chatHistory;
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) {
@@ -1628,7 +1755,10 @@ var PromptRequest = class _PromptRequest {
1628
1755
  return result;
1629
1756
  }
1630
1757
  newMessages = [this.promptMessage];
1631
- this.chatHistory = await this.memoryRecorder.prepareRun(runId, newMessages);
1758
+ const memoryPreparation = await this.memoryRecorder.prepareRun(runId, newMessages);
1759
+ this.chatHistory = memoryPreparation.history;
1760
+ usage = Usage.add(usage, memoryPreparation.usage);
1761
+ await this.recordMemoryCompaction(memoryPreparation, runObservers);
1632
1762
  const pendingTurnMessages = this.memoryRecorder.pendingTurnMessages(newMessages);
1633
1763
  await this.runRunStartHook(newMessages);
1634
1764
  while (currentTurns <= this.maxTurnCount + 1) {
@@ -1744,6 +1874,9 @@ var PromptRequest = class _PromptRequest {
1744
1874
  }
1745
1875
  throw new MaxTurnsError(this.maxTurnCount, [...this.chatHistory, ...newMessages], lastPrompt);
1746
1876
  } catch (error) {
1877
+ if (error instanceof MemoryCompactionError && error.usage !== void 0) {
1878
+ usage = Usage.add(usage, error.usage);
1879
+ }
1747
1880
  const finalError = await this.runRunErrorHook(error, usage, newMessages);
1748
1881
  this.runState = finalError instanceof PromptCancelledError ? "cancelled" : "errored";
1749
1882
  await runObservers.error({ error: finalError, usage, messages: [...newMessages] });
@@ -1801,7 +1934,10 @@ var PromptRequest = class _PromptRequest {
1801
1934
  return;
1802
1935
  }
1803
1936
  newMessages = [this.promptMessage];
1804
- this.chatHistory = await this.memoryRecorder.prepareRun(runId, newMessages);
1937
+ const memoryPreparation = await this.memoryRecorder.prepareRun(runId, newMessages);
1938
+ this.chatHistory = memoryPreparation.history;
1939
+ usage = Usage.add(usage, memoryPreparation.usage);
1940
+ await this.recordMemoryCompaction(memoryPreparation, runObservers);
1805
1941
  const pendingTurnMessages = this.memoryRecorder.pendingTurnMessages(newMessages);
1806
1942
  await this.runRunStartHook(newMessages);
1807
1943
  while (currentTurns <= this.maxTurnCount + 1) {
@@ -2073,6 +2209,9 @@ var PromptRequest = class _PromptRequest {
2073
2209
  }
2074
2210
  throw new MaxTurnsError(this.maxTurnCount, [...this.chatHistory, ...newMessages], lastPrompt);
2075
2211
  } catch (error) {
2212
+ if (error instanceof MemoryCompactionError && error.usage !== void 0) {
2213
+ usage = Usage.add(usage, error.usage);
2214
+ }
2076
2215
  const finalError = await this.runRunErrorHook(error, usage, newMessages);
2077
2216
  this.runState = finalError instanceof PromptCancelledError ? "cancelled" : "errored";
2078
2217
  const finalUsage = usage;
@@ -2244,6 +2383,24 @@ var PromptRequest = class _PromptRequest {
2244
2383
  attributes: guardrailDecisionAttributes(decision)
2245
2384
  });
2246
2385
  }
2386
+ async recordMemoryCompaction(preparation, runObservers) {
2387
+ const compaction = preparation.compaction;
2388
+ if (compaction === void 0) {
2389
+ return;
2390
+ }
2391
+ await runObservers.event({
2392
+ name: "memory.compaction",
2393
+ attributes: {
2394
+ originalMessageCount: compaction.originalMessageCount,
2395
+ compactedMessageCount: compaction.compactedMessageCount,
2396
+ retainedMessageCount: compaction.retainedMessageCount,
2397
+ conflictRetries: compaction.conflictRetries,
2398
+ inputTokens: preparation.usage.inputTokens,
2399
+ outputTokens: preparation.usage.outputTokens,
2400
+ totalTokens: preparation.usage.totalTokens
2401
+ }
2402
+ });
2403
+ }
2247
2404
  guardrailRunContext(runId) {
2248
2405
  const context = {
2249
2406
  agentId: this.agent.id,
@@ -2593,4 +2750,4 @@ export {
2593
2750
  extractRagText,
2594
2751
  PromptRequest
2595
2752
  };
2596
- //# sourceMappingURL=chunk-OPAEENGD.js.map
2753
+ //# sourceMappingURL=chunk-7LCKIULT.js.map