@anvia/core 0.1.0 → 0.1.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.
Files changed (36) hide show
  1. package/README.md +64 -0
  2. package/dist/agent/index.d.ts +11 -4
  3. package/dist/agent/index.js +8 -1
  4. package/dist/{agent-C6h6YrRU.d.ts → agent-D5KKP9_z.d.ts} +38 -5
  5. package/dist/{chunk-IQBY2GCF.js → chunk-BIJ5ZBCO.js} +262 -27
  6. package/dist/chunk-BIJ5ZBCO.js.map +1 -0
  7. package/dist/{chunk-X6FBOU2P.js → chunk-J23SIK6Y.js} +7 -1
  8. package/dist/chunk-J23SIK6Y.js.map +1 -0
  9. package/dist/{chunk-KSIY7KJA.js → chunk-JDPPZCOY.js} +53 -42
  10. package/dist/chunk-JDPPZCOY.js.map +1 -0
  11. package/dist/{chunk-LMBOJMNB.js → chunk-U524PWC6.js} +2 -2
  12. package/dist/chunk-XXT2UCAR.js +11 -0
  13. package/dist/chunk-XXT2UCAR.js.map +1 -0
  14. package/dist/chunk-YK4WAAS4.js +18 -0
  15. package/dist/chunk-YK4WAAS4.js.map +1 -0
  16. package/dist/{chunk-IA76K5UX.js → chunk-ZCKIAMMR.js} +2 -2
  17. package/dist/evals/index.d.ts +3 -2
  18. package/dist/evals/index.js +6 -4
  19. package/dist/extractor/index.d.ts +3 -2
  20. package/dist/extractor/index.js +5 -3
  21. package/dist/index.d.ts +3 -2
  22. package/dist/index.js +18 -9
  23. package/dist/memory/index.d.ts +43 -0
  24. package/dist/memory/index.js +7 -0
  25. package/dist/memory/index.js.map +1 -0
  26. package/dist/pipeline/index.d.ts +3 -2
  27. package/dist/skills/index.js +3 -2
  28. package/dist/tool/index.d.ts +2 -2
  29. package/dist/tool/index.js +3 -1
  30. package/dist/{zod-schema-DJTEgQBq.d.ts → zod-schema-Cq_9zlmJ.d.ts} +15 -1
  31. package/package.json +6 -2
  32. package/dist/chunk-IQBY2GCF.js.map +0 -1
  33. package/dist/chunk-KSIY7KJA.js.map +0 -1
  34. package/dist/chunk-X6FBOU2P.js.map +0 -1
  35. /package/dist/{chunk-LMBOJMNB.js.map → chunk-U524PWC6.js.map} +0 -0
  36. /package/dist/{chunk-IA76K5UX.js.map → chunk-ZCKIAMMR.js.map} +0 -0
@@ -1,3 +1,9 @@
1
+ import {
2
+ resolveMemoryOptions
3
+ } from "./chunk-XXT2UCAR.js";
4
+ import {
5
+ isSkillTool
6
+ } from "./chunk-YK4WAAS4.js";
1
7
  import {
2
8
  toReadableStream
3
9
  } from "./chunk-XUUY2L2D.js";
@@ -453,25 +459,28 @@ function reasoningDeltaEvent(event) {
453
459
 
454
460
  // src/agent/request.ts
455
461
  var PromptRequest = class _PromptRequest {
456
- constructor(agent, promptMessage) {
462
+ constructor(agent, promptMessage, initialHistory = [], memoryContext = void 0) {
457
463
  this.agent = agent;
458
464
  this.promptMessage = promptMessage;
465
+ this.initialHistory = initialHistory;
466
+ this.memoryContext = memoryContext;
467
+ this.chatHistory = initialHistory;
459
468
  this.maxTurnCount = agent.defaultMaxTurns ?? 0;
460
469
  this.activeHook = agent.hook;
461
470
  }
462
471
  agent;
463
472
  promptMessage;
473
+ initialHistory;
474
+ memoryContext;
464
475
  chatHistory;
465
476
  maxTurnCount;
466
477
  activeHook;
467
478
  concurrency = 1;
468
479
  traceOptions;
469
- static fromAgent(agent, prompt) {
470
- return new _PromptRequest(agent, typeof prompt === "string" ? Message.user(prompt) : prompt);
471
- }
472
- withHistory(history) {
473
- this.chatHistory = history;
474
- return this;
480
+ requestToolMiddlewares = [];
481
+ static fromAgent(agent, prompt, options = {}) {
482
+ const normalized = normalizePromptInput(prompt);
483
+ return new _PromptRequest(agent, normalized.prompt, normalized.history, options.memoryContext);
475
484
  }
476
485
  maxTurns(maxTurns) {
477
486
  this.maxTurnCount = maxTurns;
@@ -485,12 +494,23 @@ var PromptRequest = class _PromptRequest {
485
494
  this.concurrency = Math.max(1, concurrency);
486
495
  return this;
487
496
  }
497
+ withToolMiddleware(middleware) {
498
+ this.requestToolMiddlewares.push(middleware);
499
+ return this;
500
+ }
501
+ withToolMiddlewares(middlewares) {
502
+ this.requestToolMiddlewares.push(...middlewares);
503
+ return this;
504
+ }
488
505
  withTrace(trace) {
489
506
  this.traceOptions = trace;
490
507
  return this;
491
508
  }
492
509
  async send() {
510
+ const runId = globalThis.crypto.randomUUID();
493
511
  const newMessages = [this.promptMessage];
512
+ await this.prepareMemoryRun(runId, newMessages);
513
+ const pendingTurnMessages = this.memoryPolicy() === "turn" ? [...newMessages] : [];
494
514
  let usage = Usage.empty();
495
515
  let currentTurns = 0;
496
516
  let lastPrompt = this.promptMessage;
@@ -503,7 +523,7 @@ var PromptRequest = class _PromptRequest {
503
523
  }
504
524
  lastPrompt = prompt;
505
525
  currentTurns += 1;
506
- const historyForRequest = [...this.chatHistory ?? [], ...newMessages.slice(0, -1)];
526
+ const historyForRequest = [...this.chatHistory, ...newMessages.slice(0, -1)];
507
527
  await this.runCompletionCallHook(prompt, historyForRequest, newMessages);
508
528
  const ragText = extractRagText(prompt);
509
529
  const dynamicContext = await this.fetchDynamicContext(ragText);
@@ -512,11 +532,24 @@ var PromptRequest = class _PromptRequest {
512
532
  const response = await this.runCompletion(request, currentTurns, runObservers);
513
533
  usage = Usage.add(usage, response.usage);
514
534
  await this.runCompletionResponseHook(prompt, response, newMessages);
515
- newMessages.push(Message.assistant(response.choice, response.messageId));
535
+ const assistantMessage = Message.assistant(response.choice, response.messageId);
536
+ newMessages.push(assistantMessage);
537
+ await this.commitMemoryMessages(
538
+ runId,
539
+ currentTurns,
540
+ [assistantMessage],
541
+ pendingTurnMessages
542
+ );
516
543
  const toolCalls = response.choice.filter(
517
544
  (item) => item.type === "tool_call"
518
545
  );
519
546
  if (toolCalls.length === 0) {
547
+ await this.commitCompletedMemoryRun(
548
+ runId,
549
+ currentTurns,
550
+ newMessages,
551
+ pendingTurnMessages
552
+ );
520
553
  const result = {
521
554
  output: textFromAssistantContent(response.choice),
522
555
  usage,
@@ -530,15 +563,15 @@ var PromptRequest = class _PromptRequest {
530
563
  turn: currentTurns,
531
564
  runObservers
532
565
  });
533
- newMessages.push(Message.tool(toolResults));
566
+ const toolMessage = Message.tool(toolResults);
567
+ newMessages.push(toolMessage);
568
+ await this.commitMemoryMessages(runId, currentTurns, [toolMessage], pendingTurnMessages);
569
+ await this.commitCompletedMemoryTurn(runId, currentTurns, pendingTurnMessages);
534
570
  }
535
- throw new MaxTurnsError(
536
- this.maxTurnCount,
537
- [...this.chatHistory ?? [], ...newMessages],
538
- lastPrompt
539
- );
571
+ throw new MaxTurnsError(this.maxTurnCount, [...this.chatHistory, ...newMessages], lastPrompt);
540
572
  } catch (error) {
541
573
  await runObservers.error({ error, usage, messages: [...newMessages] });
574
+ await this.recordMemoryError(runId, error, newMessages);
542
575
  throw error;
543
576
  }
544
577
  }
@@ -546,7 +579,10 @@ var PromptRequest = class _PromptRequest {
546
579
  if (!this.agent.model.capabilities.streaming || !isStreamingCompletionModel(this.agent.model)) {
547
580
  throw new Error("This completion model does not support streaming");
548
581
  }
582
+ const runId = globalThis.crypto.randomUUID();
549
583
  const newMessages = [this.promptMessage];
584
+ await this.prepareMemoryRun(runId, newMessages);
585
+ const pendingTurnMessages = this.memoryPolicy() === "turn" ? [...newMessages] : [];
550
586
  let usage = Usage.empty();
551
587
  let currentTurns = 0;
552
588
  let lastPrompt = this.promptMessage;
@@ -559,7 +595,7 @@ var PromptRequest = class _PromptRequest {
559
595
  }
560
596
  lastPrompt = prompt;
561
597
  currentTurns += 1;
562
- const historyForRequest = [...this.chatHistory ?? [], ...newMessages.slice(0, -1)];
598
+ const historyForRequest = [...this.chatHistory, ...newMessages.slice(0, -1)];
563
599
  yield {
564
600
  type: "turn_start",
565
601
  turn: currentTurns,
@@ -604,7 +640,14 @@ var PromptRequest = class _PromptRequest {
604
640
  });
605
641
  usage = Usage.add(usage, response.usage);
606
642
  await this.runCompletionResponseHook(prompt, response, newMessages);
607
- newMessages.push(Message.assistant(response.choice, response.messageId));
643
+ const assistantMessage = Message.assistant(response.choice, response.messageId);
644
+ newMessages.push(assistantMessage);
645
+ await this.commitMemoryMessages(
646
+ runId,
647
+ currentTurns,
648
+ [assistantMessage],
649
+ pendingTurnMessages
650
+ );
608
651
  const toolCalls = response.choice.filter(
609
652
  (item) => item.type === "tool_call"
610
653
  );
@@ -614,6 +657,12 @@ var PromptRequest = class _PromptRequest {
614
657
  yield { type: "turn_end", turn: currentTurns, response };
615
658
  if (toolCalls.length === 0) {
616
659
  const output = textFromAssistantContent(response.choice);
660
+ await this.commitCompletedMemoryRun(
661
+ runId,
662
+ currentTurns,
663
+ newMessages,
664
+ pendingTurnMessages
665
+ );
617
666
  yield {
618
667
  type: "final",
619
668
  output,
@@ -644,15 +693,15 @@ var PromptRequest = class _PromptRequest {
644
693
  yield { type: "tool_result", turn: currentTurns, ...result };
645
694
  }
646
695
  const toolResults = await toolResultsPromise;
647
- newMessages.push(Message.tool(toolResults));
696
+ const toolMessage = Message.tool(toolResults);
697
+ newMessages.push(toolMessage);
698
+ await this.commitMemoryMessages(runId, currentTurns, [toolMessage], pendingTurnMessages);
699
+ await this.commitCompletedMemoryTurn(runId, currentTurns, pendingTurnMessages);
648
700
  }
649
- throw new MaxTurnsError(
650
- this.maxTurnCount,
651
- [...this.chatHistory ?? [], ...newMessages],
652
- lastPrompt
653
- );
701
+ throw new MaxTurnsError(this.maxTurnCount, [...this.chatHistory, ...newMessages], lastPrompt);
654
702
  } catch (error) {
655
703
  await runObservers.error({ error, usage, messages: [...newMessages] });
704
+ await this.recordMemoryError(runId, error, newMessages);
656
705
  yield { type: "error", error };
657
706
  throw error;
658
707
  }
@@ -719,6 +768,14 @@ var PromptRequest = class _PromptRequest {
719
768
  output = error instanceof Error ? error.toString() : String(error);
720
769
  }
721
770
  }
771
+ if (this.agent.shouldApplyToolMiddleware(toolCall.function.name)) {
772
+ output = await this.runToolResultMiddlewares({
773
+ ...hookArgs,
774
+ result: output,
775
+ originalResult: output,
776
+ turn: observation?.turn ?? 0
777
+ });
778
+ }
722
779
  const resultAction = await this.activeHook?.onToolResult?.({
723
780
  ...hookArgs,
724
781
  result: output,
@@ -750,6 +807,19 @@ var PromptRequest = class _PromptRequest {
750
807
  return ToolContent.toolResult(toolCall.id, output, toolCall.callId);
751
808
  });
752
809
  }
810
+ async runToolResultMiddlewares(args) {
811
+ let result = args.result;
812
+ for (const middleware of [...this.agent.toolMiddlewares, ...this.requestToolMiddlewares]) {
813
+ const replacement = await middleware.onResult?.({
814
+ ...args,
815
+ result
816
+ });
817
+ if (replacement !== void 0) {
818
+ result = replacement;
819
+ }
820
+ }
821
+ return result;
822
+ }
753
823
  async startRunObservers() {
754
824
  const failOnObserverError = this.traceOptions?.failOnObserverError === true || this.agent.observers.some((registration) => registration.failOnObserverError === true);
755
825
  return startAgentRunObservers(
@@ -760,7 +830,7 @@ var PromptRequest = class _PromptRequest {
760
830
  instructions: this.agent.instructions,
761
831
  trace: this.traceOptions,
762
832
  prompt: this.promptMessage,
763
- history: this.chatHistory ?? [],
833
+ history: this.chatHistory,
764
834
  maxTurns: this.maxTurnCount
765
835
  },
766
836
  failOnObserverError
@@ -850,9 +920,105 @@ var PromptRequest = class _PromptRequest {
850
920
  }
851
921
  }
852
922
  cancelled(newMessages, reason) {
853
- return new PromptCancelledError([...this.chatHistory ?? [], ...newMessages], reason);
923
+ return new PromptCancelledError([...this.chatHistory, ...newMessages], reason);
924
+ }
925
+ memory() {
926
+ return this.memoryContext === void 0 ? void 0 : this.agent.memory;
927
+ }
928
+ memoryPolicy() {
929
+ return this.memory()?.options.savePolicy;
930
+ }
931
+ async prepareMemoryRun(runId, newMessages) {
932
+ const memory = this.memory();
933
+ if (memory === void 0 || this.memoryContext === void 0) {
934
+ this.chatHistory = this.initialHistory;
935
+ return;
936
+ }
937
+ const memoryHistory = await memory.store.load(this.memoryContext);
938
+ this.chatHistory = [...memoryHistory, ...this.initialHistory];
939
+ if (memory.options.savePolicy === "message") {
940
+ await memory.store.append({
941
+ context: this.memoryContext,
942
+ runId,
943
+ turn: 1,
944
+ messages: newMessages
945
+ });
946
+ }
947
+ }
948
+ async commitMemoryMessages(runId, turn, messages, pendingTurnMessages) {
949
+ const memory = this.memory();
950
+ if (memory === void 0 || this.memoryContext === void 0 || messages.length === 0) {
951
+ return;
952
+ }
953
+ if (memory.options.savePolicy === "message") {
954
+ await memory.store.append({
955
+ context: this.memoryContext,
956
+ runId,
957
+ turn,
958
+ messages
959
+ });
960
+ } else if (memory.options.savePolicy === "turn") {
961
+ pendingTurnMessages.push(...messages);
962
+ }
963
+ }
964
+ async commitCompletedMemoryTurn(runId, turn, pendingTurnMessages) {
965
+ const memory = this.memory();
966
+ if (memory === void 0 || this.memoryContext === void 0 || memory.options.savePolicy !== "turn" || pendingTurnMessages.length === 0) {
967
+ return;
968
+ }
969
+ await memory.store.append({
970
+ context: this.memoryContext,
971
+ runId,
972
+ turn,
973
+ messages: [...pendingTurnMessages]
974
+ });
975
+ pendingTurnMessages.length = 0;
976
+ }
977
+ async commitCompletedMemoryRun(runId, turn, newMessages, pendingTurnMessages) {
978
+ await this.commitCompletedMemoryTurn(runId, turn, pendingTurnMessages);
979
+ const memory = this.memory();
980
+ if (memory === void 0 || this.memoryContext === void 0 || memory.options.savePolicy !== "run") {
981
+ return;
982
+ }
983
+ await memory.store.append({
984
+ context: this.memoryContext,
985
+ runId,
986
+ turn,
987
+ messages: [...newMessages]
988
+ });
989
+ }
990
+ async recordMemoryError(runId, error, newMessages) {
991
+ const memory = this.memory();
992
+ if (memory === void 0 || this.memoryContext === void 0) {
993
+ return;
994
+ }
995
+ await memory.store.recordError?.({
996
+ context: this.memoryContext,
997
+ runId,
998
+ error,
999
+ messages: [...newMessages]
1000
+ });
854
1001
  }
855
1002
  };
1003
+ function normalizePromptInput(prompt) {
1004
+ if (typeof prompt === "string") {
1005
+ return { prompt: Message.user(prompt), history: [] };
1006
+ }
1007
+ if (!Array.isArray(prompt)) {
1008
+ return { prompt, history: [] };
1009
+ }
1010
+ if (prompt.length === 0) {
1011
+ throw new TypeError("Prompt transcript must contain at least one message.");
1012
+ }
1013
+ const activePrompt = prompt.at(-1);
1014
+ if (activePrompt === void 0) {
1015
+ throw new TypeError("Prompt transcript must contain at least one message.");
1016
+ }
1017
+ return {
1018
+ prompt: activePrompt,
1019
+ history: prompt.slice(0, -1)
1020
+ };
1021
+ }
856
1022
  function createAsyncQueue() {
857
1023
  const values = [];
858
1024
  const waiters = [];
@@ -962,6 +1128,8 @@ var Agent = class {
962
1128
  observers;
963
1129
  dynamicContexts;
964
1130
  dynamicTools;
1131
+ toolMiddlewares;
1132
+ memory;
965
1133
  constructor(options) {
966
1134
  this.id = normalizeAgentId(options.id);
967
1135
  this.name = options.name;
@@ -980,10 +1148,26 @@ var Agent = class {
980
1148
  this.observers = options.observers ?? [];
981
1149
  this.dynamicContexts = options.dynamicContexts ?? [];
982
1150
  this.dynamicTools = options.dynamicTools ?? [];
1151
+ this.toolMiddlewares = options.toolMiddlewares ?? [];
1152
+ this.memory = options.memory;
983
1153
  }
984
1154
  prompt(prompt) {
985
1155
  return PromptRequest.fromAgent(this, prompt);
986
1156
  }
1157
+ session(sessionId, options = {}) {
1158
+ if (this.memory === void 0) {
1159
+ throw new Error(`Agent "${this.id}" has no memory store configured.`);
1160
+ }
1161
+ const normalized = sessionId.trim();
1162
+ if (normalized.length === 0) {
1163
+ throw new TypeError("Session id must be a non-empty string.");
1164
+ }
1165
+ return new AgentSession(this, {
1166
+ sessionId: normalized,
1167
+ ...options.userId === void 0 ? {} : { userId: options.userId },
1168
+ ...options.metadata === void 0 ? {} : { metadata: options.metadata }
1169
+ });
1170
+ }
987
1171
  asTool(options) {
988
1172
  const description = options.description ?? this.description ?? `Prompt the ${options.name} agent.`;
989
1173
  return createTool({
@@ -1025,6 +1209,37 @@ var Agent = class {
1025
1209
  }
1026
1210
  return this.toolSet.call(toolName, args);
1027
1211
  }
1212
+ shouldApplyToolMiddleware(toolName) {
1213
+ return !isSkillTool(this.getTool(toolName));
1214
+ }
1215
+ };
1216
+ var AgentSession = class {
1217
+ constructor(agent, context) {
1218
+ this.agent = agent;
1219
+ this.context = context;
1220
+ }
1221
+ agent;
1222
+ context;
1223
+ prompt(prompt) {
1224
+ if (Array.isArray(prompt)) {
1225
+ throw new TypeError("AgentSession.prompt does not accept Message[] transcripts.");
1226
+ }
1227
+ return PromptRequest.fromAgent(this.agent, prompt, { memoryContext: this.context });
1228
+ }
1229
+ async messages() {
1230
+ const memory = this.agent.memory;
1231
+ if (memory === void 0) {
1232
+ throw new Error(`Agent "${this.agent.id}" has no memory store configured.`);
1233
+ }
1234
+ return memory.store.load(this.context);
1235
+ }
1236
+ async clear() {
1237
+ const memory = this.agent.memory;
1238
+ if (memory === void 0) {
1239
+ throw new Error(`Agent "${this.agent.id}" has no memory store configured.`);
1240
+ }
1241
+ await memory.store.clear(this.context);
1242
+ }
1028
1243
  };
1029
1244
  function dynamicToolSetFromIndex(index) {
1030
1245
  const maybeIndex = index;
@@ -1064,6 +1279,8 @@ var AgentBuilder = class {
1064
1279
  observerRegistrations = [];
1065
1280
  dynamicContextRegistrations = [];
1066
1281
  dynamicToolRegistrations = [];
1282
+ middlewareRegistrations = [];
1283
+ memoryRegistration;
1067
1284
  activeToolSet = new ToolSet();
1068
1285
  name(name) {
1069
1286
  this.agentName = name;
@@ -1141,6 +1358,14 @@ var AgentBuilder = class {
1141
1358
  this.requestHook = hook;
1142
1359
  return this;
1143
1360
  }
1361
+ toolMiddleware(middleware) {
1362
+ this.middlewareRegistrations.push(middleware);
1363
+ return this;
1364
+ }
1365
+ toolMiddlewares(middlewares) {
1366
+ this.middlewareRegistrations.push(...middlewares);
1367
+ return this;
1368
+ }
1144
1369
  observe(observer, options = {}) {
1145
1370
  this.observerRegistrations.push({
1146
1371
  observer,
@@ -1148,6 +1373,13 @@ var AgentBuilder = class {
1148
1373
  });
1149
1374
  return this;
1150
1375
  }
1376
+ memory(store, options = {}) {
1377
+ this.memoryRegistration = {
1378
+ store,
1379
+ options: resolveMemoryOptions(options)
1380
+ };
1381
+ return this;
1382
+ }
1151
1383
  outputSchema(schema) {
1152
1384
  this.schema = toProviderJsonSchema(schema);
1153
1385
  return this;
@@ -1170,7 +1402,9 @@ var AgentBuilder = class {
1170
1402
  outputSchema: this.schema,
1171
1403
  observers: this.observerRegistrations,
1172
1404
  dynamicContexts: this.dynamicContextRegistrations,
1173
- dynamicTools: this.dynamicToolRegistrations
1405
+ dynamicTools: this.dynamicToolRegistrations,
1406
+ toolMiddlewares: this.middlewareRegistrations,
1407
+ memory: this.memoryRegistration
1174
1408
  });
1175
1409
  }
1176
1410
  buildInstructions() {
@@ -1202,6 +1436,7 @@ export {
1202
1436
  PromptRequest,
1203
1437
  DEFAULT_MAX_TURNS,
1204
1438
  Agent,
1439
+ AgentSession,
1205
1440
  AgentBuilder
1206
1441
  };
1207
- //# sourceMappingURL=chunk-IQBY2GCF.js.map
1442
+ //# sourceMappingURL=chunk-BIJ5ZBCO.js.map