illuma-agents 1.0.37 → 1.0.39

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 (139) hide show
  1. package/dist/cjs/agents/AgentContext.cjs +112 -14
  2. package/dist/cjs/agents/AgentContext.cjs.map +1 -1
  3. package/dist/cjs/common/enum.cjs +5 -1
  4. package/dist/cjs/common/enum.cjs.map +1 -1
  5. package/dist/cjs/graphs/Graph.cjs +148 -8
  6. package/dist/cjs/graphs/Graph.cjs.map +1 -1
  7. package/dist/cjs/graphs/MultiAgentGraph.cjs +277 -11
  8. package/dist/cjs/graphs/MultiAgentGraph.cjs.map +1 -1
  9. package/dist/cjs/llm/bedrock/index.cjs +128 -61
  10. package/dist/cjs/llm/bedrock/index.cjs.map +1 -1
  11. package/dist/cjs/main.cjs +22 -7
  12. package/dist/cjs/main.cjs.map +1 -1
  13. package/dist/cjs/messages/cache.cjs +140 -46
  14. package/dist/cjs/messages/cache.cjs.map +1 -1
  15. package/dist/cjs/messages/core.cjs +1 -1
  16. package/dist/cjs/messages/core.cjs.map +1 -1
  17. package/dist/cjs/messages/tools.cjs +2 -2
  18. package/dist/cjs/messages/tools.cjs.map +1 -1
  19. package/dist/cjs/schemas/validate.cjs +173 -0
  20. package/dist/cjs/schemas/validate.cjs.map +1 -0
  21. package/dist/cjs/stream.cjs +4 -2
  22. package/dist/cjs/stream.cjs.map +1 -1
  23. package/dist/cjs/tools/BrowserTools.cjs.map +1 -1
  24. package/dist/cjs/tools/CodeExecutor.cjs +22 -21
  25. package/dist/cjs/tools/CodeExecutor.cjs.map +1 -1
  26. package/dist/cjs/tools/ProgrammaticToolCalling.cjs +14 -11
  27. package/dist/cjs/tools/ProgrammaticToolCalling.cjs.map +1 -1
  28. package/dist/cjs/tools/ToolNode.cjs +101 -2
  29. package/dist/cjs/tools/ToolNode.cjs.map +1 -1
  30. package/dist/cjs/tools/ToolSearch.cjs +862 -0
  31. package/dist/cjs/tools/ToolSearch.cjs.map +1 -0
  32. package/dist/esm/agents/AgentContext.mjs +112 -14
  33. package/dist/esm/agents/AgentContext.mjs.map +1 -1
  34. package/dist/esm/common/enum.mjs +5 -1
  35. package/dist/esm/common/enum.mjs.map +1 -1
  36. package/dist/esm/graphs/Graph.mjs +149 -9
  37. package/dist/esm/graphs/Graph.mjs.map +1 -1
  38. package/dist/esm/graphs/MultiAgentGraph.mjs +278 -12
  39. package/dist/esm/graphs/MultiAgentGraph.mjs.map +1 -1
  40. package/dist/esm/llm/bedrock/index.mjs +127 -60
  41. package/dist/esm/llm/bedrock/index.mjs.map +1 -1
  42. package/dist/esm/main.mjs +2 -1
  43. package/dist/esm/main.mjs.map +1 -1
  44. package/dist/esm/messages/cache.mjs +140 -46
  45. package/dist/esm/messages/cache.mjs.map +1 -1
  46. package/dist/esm/messages/core.mjs +1 -1
  47. package/dist/esm/messages/core.mjs.map +1 -1
  48. package/dist/esm/messages/tools.mjs +2 -2
  49. package/dist/esm/messages/tools.mjs.map +1 -1
  50. package/dist/esm/schemas/validate.mjs +167 -0
  51. package/dist/esm/schemas/validate.mjs.map +1 -0
  52. package/dist/esm/stream.mjs +4 -2
  53. package/dist/esm/stream.mjs.map +1 -1
  54. package/dist/esm/tools/BrowserTools.mjs.map +1 -1
  55. package/dist/esm/tools/CodeExecutor.mjs +22 -21
  56. package/dist/esm/tools/CodeExecutor.mjs.map +1 -1
  57. package/dist/esm/tools/ProgrammaticToolCalling.mjs +14 -11
  58. package/dist/esm/tools/ProgrammaticToolCalling.mjs.map +1 -1
  59. package/dist/esm/tools/ToolNode.mjs +102 -3
  60. package/dist/esm/tools/ToolNode.mjs.map +1 -1
  61. package/dist/esm/tools/ToolSearch.mjs +827 -0
  62. package/dist/esm/tools/ToolSearch.mjs.map +1 -0
  63. package/dist/types/agents/AgentContext.d.ts +51 -1
  64. package/dist/types/common/enum.d.ts +6 -2
  65. package/dist/types/graphs/Graph.d.ts +12 -0
  66. package/dist/types/graphs/MultiAgentGraph.d.ts +16 -0
  67. package/dist/types/index.d.ts +2 -1
  68. package/dist/types/llm/bedrock/index.d.ts +89 -11
  69. package/dist/types/llm/bedrock/types.d.ts +27 -0
  70. package/dist/types/llm/bedrock/utils/index.d.ts +5 -0
  71. package/dist/types/llm/bedrock/utils/message_inputs.d.ts +31 -0
  72. package/dist/types/llm/bedrock/utils/message_outputs.d.ts +33 -0
  73. package/dist/types/messages/cache.d.ts +4 -1
  74. package/dist/types/schemas/index.d.ts +1 -0
  75. package/dist/types/schemas/validate.d.ts +36 -0
  76. package/dist/types/tools/CodeExecutor.d.ts +0 -3
  77. package/dist/types/tools/ProgrammaticToolCalling.d.ts +0 -3
  78. package/dist/types/tools/ToolNode.d.ts +3 -1
  79. package/dist/types/tools/ToolSearch.d.ts +148 -0
  80. package/dist/types/types/graph.d.ts +71 -0
  81. package/dist/types/types/llm.d.ts +3 -1
  82. package/dist/types/types/tools.d.ts +42 -2
  83. package/package.json +13 -6
  84. package/src/agents/AgentContext.test.ts +312 -0
  85. package/src/agents/AgentContext.ts +144 -16
  86. package/src/common/enum.ts +5 -1
  87. package/src/graphs/Graph.ts +214 -13
  88. package/src/graphs/MultiAgentGraph.ts +350 -13
  89. package/src/index.ts +4 -1
  90. package/src/llm/bedrock/index.ts +221 -99
  91. package/src/llm/bedrock/llm.spec.ts +616 -0
  92. package/src/llm/bedrock/types.ts +51 -0
  93. package/src/llm/bedrock/utils/index.ts +18 -0
  94. package/src/llm/bedrock/utils/message_inputs.ts +563 -0
  95. package/src/llm/bedrock/utils/message_outputs.ts +310 -0
  96. package/src/messages/__tests__/tools.test.ts +21 -21
  97. package/src/messages/cache.test.ts +304 -0
  98. package/src/messages/cache.ts +183 -53
  99. package/src/messages/core.ts +1 -1
  100. package/src/messages/tools.ts +2 -2
  101. package/src/schemas/index.ts +2 -0
  102. package/src/schemas/validate.test.ts +358 -0
  103. package/src/schemas/validate.ts +238 -0
  104. package/src/scripts/caching.ts +27 -19
  105. package/src/scripts/code_exec_files.ts +58 -15
  106. package/src/scripts/code_exec_multi_session.ts +241 -0
  107. package/src/scripts/code_exec_session.ts +282 -0
  108. package/src/scripts/multi-agent-conditional.ts +1 -0
  109. package/src/scripts/multi-agent-supervisor.ts +1 -0
  110. package/src/scripts/programmatic_exec_agent.ts +4 -4
  111. package/src/scripts/test-handoff-preamble.ts +277 -0
  112. package/src/scripts/test-parallel-handoffs.ts +291 -0
  113. package/src/scripts/test-tools-before-handoff.ts +8 -4
  114. package/src/scripts/test_code_api.ts +361 -0
  115. package/src/scripts/thinking-bedrock.ts +159 -0
  116. package/src/scripts/thinking.ts +39 -18
  117. package/src/scripts/{tool_search_regex.ts → tool_search.ts} +5 -5
  118. package/src/scripts/tools.ts +7 -3
  119. package/src/specs/cache.simple.test.ts +396 -0
  120. package/src/stream.ts +4 -2
  121. package/src/tools/BrowserTools.ts +39 -17
  122. package/src/tools/CodeExecutor.ts +26 -23
  123. package/src/tools/ProgrammaticToolCalling.ts +18 -14
  124. package/src/tools/ToolNode.ts +114 -1
  125. package/src/tools/ToolSearch.ts +1041 -0
  126. package/src/tools/__tests__/ProgrammaticToolCalling.test.ts +0 -2
  127. package/src/tools/__tests__/{ToolSearchRegex.integration.test.ts → ToolSearch.integration.test.ts} +6 -6
  128. package/src/tools/__tests__/ToolSearch.test.ts +1003 -0
  129. package/src/types/graph.test.ts +183 -0
  130. package/src/types/graph.ts +73 -0
  131. package/src/types/llm.ts +3 -1
  132. package/src/types/tools.ts +51 -2
  133. package/dist/cjs/tools/ToolSearchRegex.cjs +0 -455
  134. package/dist/cjs/tools/ToolSearchRegex.cjs.map +0 -1
  135. package/dist/esm/tools/ToolSearchRegex.mjs +0 -448
  136. package/dist/esm/tools/ToolSearchRegex.mjs.map +0 -1
  137. package/dist/types/tools/ToolSearchRegex.d.ts +0 -80
  138. package/src/tools/ToolSearchRegex.ts +0 -535
  139. package/src/tools/__tests__/ToolSearchRegex.test.ts +0 -232
@@ -37,6 +37,7 @@ import {
37
37
  Providers,
38
38
  StepTypes,
39
39
  MessageTypes,
40
+ Constants,
40
41
  } from '@/common';
41
42
  import {
42
43
  formatAnthropicArtifactContent,
@@ -141,6 +142,12 @@ export abstract class Graph<
141
142
  /** Set of invoked tool call IDs from non-message run steps completed mid-run, if any */
142
143
  invokedToolIds?: Set<string>;
143
144
  handlerRegistry: HandlerRegistry | undefined;
145
+ /**
146
+ * Tool session contexts for automatic state persistence across tool invocations.
147
+ * Keyed by tool name (e.g., Constants.EXECUTE_CODE).
148
+ * Currently supports code execution session tracking (session_id, files).
149
+ */
150
+ sessions: t.ToolSessionMap = new Map();
144
151
  }
145
152
 
146
153
  export class StandardGraph extends Graph<t.BaseGraphState, t.GraphNode> {
@@ -487,11 +494,7 @@ export class StandardGraph extends Graph<t.BaseGraphState, t.GraphNode> {
487
494
  finalInstructions != null &&
488
495
  finalInstructions &&
489
496
  provider === Providers.ANTHROPIC &&
490
- ((
491
- (clientOptions as t.AnthropicClientOptions).clientOptions
492
- ?.defaultHeaders as Record<string, string> | undefined
493
- )?.['anthropic-beta']?.includes('prompt-caching') ??
494
- false)
497
+ (clientOptions as t.AnthropicClientOptions).promptCache === true
495
498
  ) {
496
499
  finalInstructions = {
497
500
  content: [
@@ -528,6 +531,7 @@ export class StandardGraph extends Graph<t.BaseGraphState, t.GraphNode> {
528
531
  errorHandler: (data, metadata) =>
529
532
  StandardGraph.handleToolCallErrorStatic(this, data, metadata),
530
533
  toolRegistry: agentContext?.toolRegistry,
534
+ sessions: this.sessions,
531
535
  });
532
536
  }
533
537
 
@@ -662,6 +666,106 @@ export class StandardGraph extends Graph<t.BaseGraphState, t.GraphNode> {
662
666
  }
663
667
  }
664
668
 
669
+ /**
670
+ * Execute model invocation with structured output.
671
+ * Uses withStructuredOutput to force the model to return JSON conforming to the schema.
672
+ * Disables streaming and returns a validated JSON response.
673
+ */
674
+ private async attemptStructuredInvoke(
675
+ {
676
+ currentModel,
677
+ finalMessages,
678
+ schema,
679
+ structuredOutputConfig,
680
+ }: {
681
+ currentModel: t.ChatModelInstance;
682
+ finalMessages: BaseMessage[];
683
+ schema: Record<string, unknown>;
684
+ structuredOutputConfig: t.StructuredOutputConfig;
685
+ },
686
+ config?: RunnableConfig
687
+ ): Promise<{
688
+ structuredResponse: Record<string, unknown>;
689
+ rawMessage?: AIMessageChunk;
690
+ }> {
691
+ const model = this.overrideModel ?? currentModel;
692
+ if (!model) {
693
+ throw new Error('No model found');
694
+ }
695
+
696
+ const {
697
+ name = 'StructuredResponse',
698
+ includeRaw = false,
699
+ handleErrors = true,
700
+ maxRetries = 2,
701
+ } = structuredOutputConfig;
702
+
703
+ // Use withStructuredOutput to bind the schema
704
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
705
+ const structuredModel = (model as any).withStructuredOutput(schema, {
706
+ name,
707
+ includeRaw,
708
+ strict: structuredOutputConfig.strict !== false,
709
+ });
710
+
711
+ let lastError: Error | undefined;
712
+ let attempts = 0;
713
+
714
+ while (attempts <= maxRetries) {
715
+ try {
716
+ const result = await structuredModel.invoke(finalMessages, config);
717
+
718
+ // Handle includeRaw response format
719
+ if (includeRaw && result.raw && result.parsed) {
720
+ return {
721
+ structuredResponse: result.parsed as Record<string, unknown>,
722
+ rawMessage: result.raw as AIMessageChunk,
723
+ };
724
+ }
725
+
726
+ // Direct response
727
+ return {
728
+ structuredResponse: result as Record<string, unknown>,
729
+ };
730
+ } catch (error) {
731
+ lastError = error as Error;
732
+ attempts++;
733
+
734
+ // If error handling is disabled, throw immediately
735
+ if (handleErrors === false) {
736
+ throw error;
737
+ }
738
+
739
+ // If we've exhausted retries, throw
740
+ if (attempts > maxRetries) {
741
+ throw new Error(
742
+ `Structured output failed after ${maxRetries + 1} attempts: ${lastError.message}`
743
+ );
744
+ }
745
+
746
+ // Add error message to conversation for retry
747
+ const errorMessage =
748
+ typeof handleErrors === 'string'
749
+ ? handleErrors
750
+ : `The response did not match the expected schema. Error: ${lastError.message}. Please try again with a valid response.`;
751
+
752
+ console.warn(
753
+ `[Graph] Structured output attempt ${attempts} failed: ${lastError.message}. Retrying...`
754
+ );
755
+
756
+ // Add the error as a human message for context
757
+ finalMessages = [
758
+ ...finalMessages,
759
+ new HumanMessage({
760
+ content: `[VALIDATION ERROR]\n${errorMessage}`,
761
+ }),
762
+ ];
763
+ }
764
+ }
765
+
766
+ throw lastError ?? new Error('Structured output failed');
767
+ }
768
+
665
769
  cleanupSignalListener(currentModel?: t.ChatModel): void {
666
770
  if (!this.signal) {
667
771
  return;
@@ -860,14 +964,7 @@ export class StandardGraph extends Graph<t.BaseGraphState, t.GraphNode> {
860
964
  const anthropicOptions = agentContext.clientOptions as
861
965
  | t.AnthropicClientOptions
862
966
  | undefined;
863
- const defaultHeaders = anthropicOptions?.clientOptions
864
- ?.defaultHeaders as Record<string, string> | undefined;
865
- const anthropicBeta = defaultHeaders?.['anthropic-beta'];
866
-
867
- if (
868
- typeof anthropicBeta === 'string' &&
869
- anthropicBeta.includes('prompt-caching')
870
- ) {
967
+ if (anthropicOptions?.promptCache === true) {
871
968
  finalMessages = addCacheControl<BaseMessage>(finalMessages);
872
969
  }
873
970
  } else if (agentContext.provider === Providers.BEDROCK) {
@@ -951,6 +1048,56 @@ export class StandardGraph extends Graph<t.BaseGraphState, t.GraphNode> {
951
1048
  config
952
1049
  );
953
1050
 
1051
+ // Check if structured output mode is enabled
1052
+ if (
1053
+ agentContext.isStructuredOutputMode &&
1054
+ agentContext.structuredOutput
1055
+ ) {
1056
+ const schema = agentContext.getStructuredOutputSchema();
1057
+ if (!schema) {
1058
+ throw new Error('Structured output schema is not configured');
1059
+ }
1060
+
1061
+ try {
1062
+ // Use structured output invocation (non-streaming)
1063
+ const { structuredResponse, rawMessage } =
1064
+ await this.attemptStructuredInvoke(
1065
+ {
1066
+ currentModel: model as t.ChatModelInstance,
1067
+ finalMessages,
1068
+ schema,
1069
+ structuredOutputConfig: agentContext.structuredOutput,
1070
+ },
1071
+ config
1072
+ );
1073
+
1074
+ // Emit structured output event
1075
+ await safeDispatchCustomEvent(
1076
+ GraphEvents.ON_STRUCTURED_OUTPUT,
1077
+ {
1078
+ structuredResponse,
1079
+ schema,
1080
+ raw: rawMessage,
1081
+ },
1082
+ config
1083
+ );
1084
+
1085
+ agentContext.currentUsage = rawMessage
1086
+ ? this.getUsageMetadata(rawMessage)
1087
+ : undefined;
1088
+ this.cleanupSignalListener();
1089
+
1090
+ // Return both the structured response and the raw message
1091
+ return {
1092
+ messages: rawMessage ? [rawMessage] : [],
1093
+ structuredResponse,
1094
+ };
1095
+ } catch (structuredError) {
1096
+ console.error('[Graph] Structured output failed:', structuredError);
1097
+ throw structuredError;
1098
+ }
1099
+ }
1100
+
954
1101
  try {
955
1102
  result = await this.attemptInvoke(
956
1103
  {
@@ -1366,6 +1513,60 @@ If I seem to be missing something we discussed earlier, just give me a quick rem
1366
1513
  throw new Error(`No run step found for stepId ${stepId}`);
1367
1514
  }
1368
1515
 
1516
+ /**
1517
+ * Extract and store code execution session context from artifacts.
1518
+ * Each file is stamped with its source session_id to support multi-session file tracking.
1519
+ * When the same filename appears in a later execution, the newer version replaces the old.
1520
+ */
1521
+ const toolName = output.name;
1522
+ if (
1523
+ toolName === Constants.EXECUTE_CODE ||
1524
+ toolName === Constants.PROGRAMMATIC_TOOL_CALLING
1525
+ ) {
1526
+ const artifact = output.artifact as t.CodeExecutionArtifact | undefined;
1527
+ const newFiles = artifact?.files ?? [];
1528
+ const hasNewFiles = newFiles.length > 0;
1529
+
1530
+ if (
1531
+ hasNewFiles &&
1532
+ artifact?.session_id != null &&
1533
+ artifact.session_id !== ''
1534
+ ) {
1535
+ /**
1536
+ * Stamp each new file with its source session_id.
1537
+ * This enables files from different executions (parallel or sequential)
1538
+ * to be tracked and passed to subsequent calls.
1539
+ */
1540
+ const filesWithSession: t.FileRefs = newFiles.map((file) => ({
1541
+ ...file,
1542
+ session_id: artifact.session_id,
1543
+ }));
1544
+
1545
+ const existingSession = this.sessions.get(Constants.EXECUTE_CODE) as
1546
+ | t.CodeSessionContext
1547
+ | undefined;
1548
+ const existingFiles = existingSession?.files ?? [];
1549
+
1550
+ /**
1551
+ * Merge files, preferring latest versions by name.
1552
+ * If a file with the same name exists, replace it with the new version.
1553
+ * This handles cases where files are edited/recreated in subsequent executions.
1554
+ */
1555
+ const newFileNames = new Set(filesWithSession.map((f) => f.name));
1556
+ const filteredExisting = existingFiles.filter(
1557
+ (f) => !newFileNames.has(f.name)
1558
+ );
1559
+
1560
+ this.sessions.set(Constants.EXECUTE_CODE, {
1561
+ /** Keep latest session_id for reference/fallback */
1562
+ session_id: artifact.session_id,
1563
+ /** Accumulated files with latest versions preferred */
1564
+ files: [...filteredExisting, ...filesWithSession],
1565
+ lastUpdated: Date.now(),
1566
+ });
1567
+ }
1568
+ }
1569
+
1369
1570
  const dispatchedOutput =
1370
1571
  typeof output.content === 'string'
1371
1572
  ? output.content