illuma-agents 1.0.4 → 1.0.6

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 (46) hide show
  1. package/dist/cjs/graphs/Graph.cjs +80 -1
  2. package/dist/cjs/graphs/Graph.cjs.map +1 -1
  3. package/dist/cjs/llm/anthropic/utils/message_outputs.cjs.map +1 -1
  4. package/dist/cjs/main.cjs +4 -0
  5. package/dist/cjs/main.cjs.map +1 -1
  6. package/dist/cjs/messages/cache.cjs +87 -14
  7. package/dist/cjs/messages/cache.cjs.map +1 -1
  8. package/dist/cjs/messages/format.cjs +242 -6
  9. package/dist/cjs/messages/format.cjs.map +1 -1
  10. package/dist/cjs/stream.cjs +3 -2
  11. package/dist/cjs/stream.cjs.map +1 -1
  12. package/dist/cjs/tools/handlers.cjs +5 -5
  13. package/dist/cjs/tools/handlers.cjs.map +1 -1
  14. package/dist/esm/graphs/Graph.mjs +80 -1
  15. package/dist/esm/graphs/Graph.mjs.map +1 -1
  16. package/dist/esm/llm/anthropic/utils/message_outputs.mjs.map +1 -1
  17. package/dist/esm/main.mjs +2 -2
  18. package/dist/esm/messages/cache.mjs +86 -15
  19. package/dist/esm/messages/cache.mjs.map +1 -1
  20. package/dist/esm/messages/format.mjs +242 -8
  21. package/dist/esm/messages/format.mjs.map +1 -1
  22. package/dist/esm/stream.mjs +3 -2
  23. package/dist/esm/stream.mjs.map +1 -1
  24. package/dist/esm/tools/handlers.mjs +5 -5
  25. package/dist/esm/tools/handlers.mjs.map +1 -1
  26. package/dist/types/graphs/Graph.d.ts +19 -2
  27. package/dist/types/messages/cache.d.ts +16 -0
  28. package/dist/types/messages/format.d.ts +25 -1
  29. package/dist/types/tools/handlers.d.ts +2 -1
  30. package/dist/types/types/stream.d.ts +1 -0
  31. package/package.json +4 -1
  32. package/src/graphs/Graph.ts +99 -2
  33. package/src/llm/anthropic/utils/message_outputs.ts +289 -289
  34. package/src/messages/cache.test.ts +499 -3
  35. package/src/messages/cache.ts +115 -25
  36. package/src/messages/ensureThinkingBlock.test.ts +393 -0
  37. package/src/messages/format.ts +312 -6
  38. package/src/messages/labelContentByAgent.test.ts +887 -0
  39. package/src/scripts/test-multi-agent-list-handoff.ts +169 -13
  40. package/src/scripts/test-parallel-agent-labeling.ts +325 -0
  41. package/src/scripts/test-thinking-handoff-bedrock.ts +153 -0
  42. package/src/scripts/test-thinking-handoff.ts +147 -0
  43. package/src/specs/thinking-handoff.test.ts +620 -0
  44. package/src/stream.ts +19 -10
  45. package/src/tools/handlers.ts +36 -18
  46. package/src/types/stream.ts +1 -0
@@ -1,4 +1,4 @@
1
- import { AIMessage, ToolMessage, HumanMessage, SystemMessage } from '@langchain/core/messages';
1
+ import { AIMessage, ToolMessage, BaseMessage, HumanMessage, SystemMessage } from '@langchain/core/messages';
2
2
  import type { MessageContentImageUrl } from '@langchain/core/messages';
3
3
  import type { MessageContentComplex, TPayload } from '@/types';
4
4
  import { Providers } from '@/common';
@@ -83,6 +83,20 @@ interface LangChainMessage {
83
83
  * @returns - The formatted LangChain message.
84
84
  */
85
85
  export declare const formatFromLangChain: (message: LangChainMessage) => Record<string, any>;
86
+ /**
87
+ * Groups content parts by agent and formats them with agent labels
88
+ * This preprocesses multi-agent content to prevent identity confusion
89
+ *
90
+ * @param contentParts - The content parts from a run
91
+ * @param agentIdMap - Map of content part index to agent ID
92
+ * @param agentNames - Optional map of agent ID to display name
93
+ * @param options - Configuration options
94
+ * @param options.labelNonTransferContent - If true, labels all agent transitions (for parallel patterns)
95
+ * @returns Modified content parts with agent labels where appropriate
96
+ */
97
+ export declare const labelContentByAgent: (contentParts: MessageContentComplex[], agentIdMap?: Record<number, string>, agentNames?: Record<string, string>, options?: {
98
+ labelNonTransferContent?: boolean;
99
+ }) => MessageContentComplex[];
86
100
  /**
87
101
  * Formats an array of messages for LangChain, handling tool calls and creating ToolMessage instances.
88
102
  *
@@ -104,4 +118,14 @@ export declare const formatAgentMessages: (payload: TPayload, indexTokenCountMap
104
118
  * @returns A new map with the system message at index 0 and all other indices shifted by 1
105
119
  */
106
120
  export declare function shiftIndexTokenCountMap(indexTokenCountMap: Record<number, number>, instructionsTokenCount: number): Record<number, number>;
121
+ /**
122
+ * Ensures compatibility when switching from a non-thinking agent to a thinking-enabled agent.
123
+ * Converts AI messages with tool calls (that lack thinking blocks) into buffer strings,
124
+ * avoiding the thinking block signature requirement.
125
+ *
126
+ * @param messages - Array of messages to process
127
+ * @param provider - The provider being used (unused but kept for future compatibility)
128
+ * @returns The messages array with tool sequences converted to buffer strings if necessary
129
+ */
130
+ export declare function ensureThinkingBlockInMessages(messages: BaseMessage[], _provider: Providers): BaseMessage[];
107
131
  export {};
@@ -2,10 +2,11 @@ import type { ToolCall, ToolCallChunk } from '@langchain/core/messages/tool';
2
2
  import type { MultiAgentGraph, StandardGraph } from '@/graphs';
3
3
  import type { AgentContext } from '@/agents/AgentContext';
4
4
  import type * as t from '@/types';
5
- export declare function handleToolCallChunks({ graph, stepKey, toolCallChunks, }: {
5
+ export declare function handleToolCallChunks({ graph, stepKey, toolCallChunks, metadata, }: {
6
6
  graph: StandardGraph | MultiAgentGraph;
7
7
  stepKey: string;
8
8
  toolCallChunks: ToolCallChunk[];
9
+ metadata?: Record<string, unknown>;
9
10
  }): Promise<void>;
10
11
  export declare const handleToolCalls: (toolCalls?: ToolCall[], metadata?: Record<string, unknown>, graph?: StandardGraph | MultiAgentGraph) => Promise<void>;
11
12
  export declare const toolResultTypes: Set<string>;
@@ -37,6 +37,7 @@ export type RunStep = {
37
37
  type: StepTypes;
38
38
  id: string;
39
39
  runId?: string;
40
+ agentId?: string;
40
41
  index: number;
41
42
  stepIndex?: number;
42
43
  stepDetails: StepDetails;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "illuma-agents",
3
- "version": "1.0.4",
3
+ "version": "1.0.6",
4
4
  "main": "./dist/cjs/main.cjs",
5
5
  "module": "./dist/esm/main.mjs",
6
6
  "types": "./dist/types/index.d.ts",
@@ -58,6 +58,9 @@
58
58
  "multi-agent-conditional": "node -r dotenv/config --loader ./tsconfig-paths-bootstrap.mjs --experimental-specifier-resolution=node ./src/scripts/multi-agent-conditional.ts",
59
59
  "multi-agent-supervisor": "node -r dotenv/config --loader ./tsconfig-paths-bootstrap.mjs --experimental-specifier-resolution=node ./src/scripts/multi-agent-supervisor.ts",
60
60
  "multi-agent-list-handoff": "node -r dotenv/config --loader ./tsconfig-paths-bootstrap.mjs --experimental-specifier-resolution=node ./src/scripts/test-multi-agent-list-handoff.ts",
61
+ "test-parallel-agent-labeling": "node -r dotenv/config --loader ./tsconfig-paths-bootstrap.mjs --experimental-specifier-resolution=node ./src/scripts/test-parallel-agent-labeling.ts",
62
+ "test-thinking-handoff": "node -r dotenv/config --loader ./tsconfig-paths-bootstrap.mjs --experimental-specifier-resolution=node ./src/scripts/test-thinking-handoff.ts",
63
+ "test-thinking-handoff-bedrock": "node -r dotenv/config --loader ./tsconfig-paths-bootstrap.mjs --experimental-specifier-resolution=node ./src/scripts/test-thinking-handoff-bedrock.ts",
61
64
  "multi-agent-hybrid-flow": "node -r dotenv/config --loader ./tsconfig-paths-bootstrap.mjs --experimental-specifier-resolution=node ./src/scripts/multi-agent-hybrid-flow.ts",
62
65
  "test-handoff-input": "node -r dotenv/config --loader ./tsconfig-paths-bootstrap.mjs --experimental-specifier-resolution=node ./src/scripts/test-handoff-input.ts",
63
66
  "test-custom-prompt-key": "node -r dotenv/config --loader ./tsconfig-paths-bootstrap.mjs --experimental-specifier-resolution=node ./src/scripts/test-custom-prompt-key.ts",
@@ -38,6 +38,7 @@ import {
38
38
  } from '@/common';
39
39
  import {
40
40
  formatAnthropicArtifactContent,
41
+ ensureThinkingBlockInMessages,
41
42
  convertMessagesToContent,
42
43
  addBedrockCacheControl,
43
44
  modifyDeltaProperties,
@@ -96,7 +97,8 @@ export abstract class Graph<
96
97
  abstract getRunStep(stepId: string): t.RunStep | undefined;
97
98
  abstract dispatchRunStep(
98
99
  stepKey: string,
99
- stepDetails: t.StepDetails
100
+ stepDetails: t.StepDetails,
101
+ metadata?: Record<string, unknown>
100
102
  ): Promise<string>;
101
103
  abstract dispatchRunStepDelta(
102
104
  id: string,
@@ -327,6 +329,66 @@ export class StandardGraph extends Graph<t.BaseGraphState, t.GraphNode> {
327
329
  return convertMessagesToContent(this.messages.slice(this.startIndex));
328
330
  }
329
331
 
332
+ /**
333
+ * Get all run steps, optionally filtered by agent ID
334
+ */
335
+ getRunSteps(agentId?: string): t.RunStep[] {
336
+ if (agentId == null || agentId === '') {
337
+ return [...this.contentData];
338
+ }
339
+ return this.contentData.filter((step) => step.agentId === agentId);
340
+ }
341
+
342
+ /**
343
+ * Get run steps grouped by agent ID
344
+ */
345
+ getRunStepsByAgent(): Map<string, t.RunStep[]> {
346
+ const stepsByAgent = new Map<string, t.RunStep[]>();
347
+
348
+ for (const step of this.contentData) {
349
+ if (step.agentId == null || step.agentId === '') continue;
350
+
351
+ const steps = stepsByAgent.get(step.agentId) ?? [];
352
+ steps.push(step);
353
+ stepsByAgent.set(step.agentId, steps);
354
+ }
355
+
356
+ return stepsByAgent;
357
+ }
358
+
359
+ /**
360
+ * Get agent IDs that participated in this run
361
+ */
362
+ getActiveAgentIds(): string[] {
363
+ const agentIds = new Set<string>();
364
+ for (const step of this.contentData) {
365
+ if (step.agentId != null && step.agentId !== '') {
366
+ agentIds.add(step.agentId);
367
+ }
368
+ }
369
+ return Array.from(agentIds);
370
+ }
371
+
372
+ /**
373
+ * Maps contentPart indices to agent IDs for post-run analysis
374
+ * Returns a map where key is the contentPart index and value is the agentId
375
+ */
376
+ getContentPartAgentMap(): Map<number, string> {
377
+ const contentPartAgentMap = new Map<number, string>();
378
+
379
+ for (const step of this.contentData) {
380
+ if (
381
+ step.agentId != null &&
382
+ step.agentId !== '' &&
383
+ Number.isFinite(step.index)
384
+ ) {
385
+ contentPartAgentMap.set(step.index, step.agentId);
386
+ }
387
+ }
388
+
389
+ return contentPartAgentMap;
390
+ }
391
+
330
392
  /* Graph */
331
393
 
332
394
  createSystemRunnable({
@@ -672,6 +734,26 @@ export class StandardGraph extends Graph<t.BaseGraphState, t.GraphNode> {
672
734
  }
673
735
  }
674
736
 
737
+ /**
738
+ * Handle edge case: when switching from a non-thinking agent to a thinking-enabled agent,
739
+ * convert AI messages with tool calls to HumanMessages to avoid thinking block requirements.
740
+ * This is required by Anthropic/Bedrock when thinking is enabled.
741
+ */
742
+ const isAnthropicWithThinking =
743
+ (agentContext.provider === Providers.ANTHROPIC &&
744
+ (agentContext.clientOptions as t.AnthropicClientOptions).thinking !=
745
+ null) ||
746
+ (agentContext.provider === Providers.BEDROCK &&
747
+ (agentContext.clientOptions as t.BedrockAnthropicInput)
748
+ .additionalModelRequestFields?.['thinking'] != null);
749
+
750
+ if (isAnthropicWithThinking) {
751
+ finalMessages = ensureThinkingBlockInMessages(
752
+ finalMessages,
753
+ agentContext.provider
754
+ );
755
+ }
756
+
675
757
  if (
676
758
  agentContext.lastStreamCall != null &&
677
759
  agentContext.streamBuffer != null
@@ -837,7 +919,8 @@ export class StandardGraph extends Graph<t.BaseGraphState, t.GraphNode> {
837
919
  */
838
920
  async dispatchRunStep(
839
921
  stepKey: string,
840
- stepDetails: t.StepDetails
922
+ stepDetails: t.StepDetails,
923
+ metadata?: Record<string, unknown>
841
924
  ): Promise<string> {
842
925
  if (!this.config) {
843
926
  throw new Error('No config provided');
@@ -868,6 +951,20 @@ export class StandardGraph extends Graph<t.BaseGraphState, t.GraphNode> {
868
951
  runStep.runId = runId;
869
952
  }
870
953
 
954
+ /**
955
+ * Extract and store agentId from metadata
956
+ */
957
+ if (metadata) {
958
+ try {
959
+ const agentContext = this.getAgentContext(metadata);
960
+ if (agentContext.agentId) {
961
+ runStep.agentId = agentContext.agentId;
962
+ }
963
+ } catch (_e) {
964
+ /** If we can't get agent context, that's okay - agentId remains undefined */
965
+ }
966
+ }
967
+
871
968
  this.contentData.push(runStep);
872
969
  this.contentIndexMap.set(stepId, runStep.index);
873
970
  await safeDispatchCustomEvent(