illuma-agents 1.0.38 → 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 (46) hide show
  1. package/dist/cjs/agents/AgentContext.cjs +45 -2
  2. package/dist/cjs/agents/AgentContext.cjs.map +1 -1
  3. package/dist/cjs/common/enum.cjs +2 -0
  4. package/dist/cjs/common/enum.cjs.map +1 -1
  5. package/dist/cjs/graphs/Graph.cjs +98 -0
  6. package/dist/cjs/graphs/Graph.cjs.map +1 -1
  7. package/dist/cjs/main.cjs +6 -0
  8. package/dist/cjs/main.cjs.map +1 -1
  9. package/dist/cjs/messages/cache.cjs +140 -47
  10. package/dist/cjs/messages/cache.cjs.map +1 -1
  11. package/dist/cjs/schemas/validate.cjs +173 -0
  12. package/dist/cjs/schemas/validate.cjs.map +1 -0
  13. package/dist/esm/agents/AgentContext.mjs +45 -2
  14. package/dist/esm/agents/AgentContext.mjs.map +1 -1
  15. package/dist/esm/common/enum.mjs +2 -0
  16. package/dist/esm/common/enum.mjs.map +1 -1
  17. package/dist/esm/graphs/Graph.mjs +98 -0
  18. package/dist/esm/graphs/Graph.mjs.map +1 -1
  19. package/dist/esm/main.mjs +1 -0
  20. package/dist/esm/main.mjs.map +1 -1
  21. package/dist/esm/messages/cache.mjs +140 -47
  22. package/dist/esm/messages/cache.mjs.map +1 -1
  23. package/dist/esm/schemas/validate.mjs +167 -0
  24. package/dist/esm/schemas/validate.mjs.map +1 -0
  25. package/dist/types/agents/AgentContext.d.ts +19 -1
  26. package/dist/types/common/enum.d.ts +2 -0
  27. package/dist/types/graphs/Graph.d.ts +6 -0
  28. package/dist/types/index.d.ts +1 -0
  29. package/dist/types/messages/cache.d.ts +4 -1
  30. package/dist/types/schemas/index.d.ts +1 -0
  31. package/dist/types/schemas/validate.d.ts +36 -0
  32. package/dist/types/types/graph.d.ts +69 -0
  33. package/package.json +2 -2
  34. package/src/agents/AgentContext.test.ts +312 -0
  35. package/src/agents/AgentContext.ts +56 -0
  36. package/src/common/enum.ts +2 -0
  37. package/src/graphs/Graph.ts +150 -0
  38. package/src/index.ts +3 -0
  39. package/src/messages/cache.test.ts +51 -6
  40. package/src/messages/cache.ts +149 -122
  41. package/src/schemas/index.ts +2 -0
  42. package/src/schemas/validate.test.ts +358 -0
  43. package/src/schemas/validate.ts +238 -0
  44. package/src/specs/cache.simple.test.ts +396 -0
  45. package/src/types/graph.test.ts +183 -0
  46. package/src/types/graph.ts +71 -0
@@ -1,6 +1,5 @@
1
1
  'use strict';
2
2
 
3
- require('@langchain/core/messages');
4
3
  var _enum = require('../common/enum.cjs');
5
4
 
6
5
  /** Debug logger for cache operations - set ILLUMA_DEBUG_CACHE=true to enable */
@@ -10,13 +9,97 @@ const debugCache = (message, data) => {
10
9
  console.log(`[Cache] ${message}`, data !== undefined ? JSON.stringify(data, null, 2) : '');
11
10
  }
12
11
  };
12
+ /**
13
+ * Deep clones a message's content to prevent mutation of the original.
14
+ */
15
+ function deepCloneContent(content) {
16
+ if (typeof content === 'string') {
17
+ return content;
18
+ }
19
+ if (Array.isArray(content)) {
20
+ return content.map((block) => ({ ...block }));
21
+ }
22
+ return content;
23
+ }
24
+ /**
25
+ * Clones a message with deep-cloned content, explicitly excluding LangChain
26
+ * serialization metadata to prevent coercion issues.
27
+ * Keeps lc_kwargs in sync with content to prevent LangChain serialization issues.
28
+ */
29
+ function cloneMessage(message, content) {
30
+ const { lc_kwargs: _lc_kwargs, lc_serializable: _lc_serializable, lc_namespace: _lc_namespace, ...rest } = message;
31
+ const cloned = { ...rest, content };
32
+ // Sync lc_kwargs.content with the new content to prevent LangChain coercion issues
33
+ const lcKwargs = message.lc_kwargs;
34
+ if (lcKwargs != null) {
35
+ cloned.lc_kwargs = {
36
+ ...lcKwargs,
37
+ content: content,
38
+ };
39
+ }
40
+ // LangChain messages don't have a direct 'role' property - derive it from getType()
41
+ if ('getType' in message &&
42
+ typeof message.getType === 'function' &&
43
+ !('role' in cloned)) {
44
+ const msgType = message.getType();
45
+ const roleMap = {
46
+ human: 'user',
47
+ ai: 'assistant',
48
+ system: 'system',
49
+ tool: 'tool',
50
+ };
51
+ cloned.role = roleMap[msgType] || msgType;
52
+ }
53
+ return cloned;
54
+ }
55
+ /**
56
+ * Checks if a content block is a cache point
57
+ */
58
+ function isCachePoint(block) {
59
+ return 'cachePoint' in block && !('type' in block);
60
+ }
61
+ /**
62
+ * Checks if a message's content needs cache control stripping.
63
+ * Returns true if content has cachePoint blocks or cache_control fields.
64
+ */
65
+ function needsCacheStripping(content) {
66
+ for (let i = 0; i < content.length; i++) {
67
+ const block = content[i];
68
+ if (isCachePoint(block))
69
+ return true;
70
+ if ('cache_control' in block)
71
+ return true;
72
+ }
73
+ return false;
74
+ }
75
+ /**
76
+ * Checks if a message's content has Anthropic cache_control fields.
77
+ */
78
+ function hasAnthropicCacheControl(content) {
79
+ for (let i = 0; i < content.length; i++) {
80
+ if ('cache_control' in content[i])
81
+ return true;
82
+ }
83
+ return false;
84
+ }
85
+ /**
86
+ * Checks if a message's content has Bedrock cachePoint blocks.
87
+ */
88
+ function hasBedrockCachePoint(content) {
89
+ for (let i = 0; i < content.length; i++) {
90
+ if (isCachePoint(content[i]))
91
+ return true;
92
+ }
93
+ return false;
94
+ }
13
95
  /**
14
96
  * Anthropic API: Adds cache control to the appropriate user messages in the payload.
15
97
  * Strips ALL existing cache control (both Anthropic and Bedrock formats) from all messages,
16
98
  * then adds fresh cache control to the last 2 user messages in a single backward pass.
17
99
  * This ensures we don't accumulate stale cache points across multiple turns.
100
+ * Returns a new array - only clones messages that require modification.
18
101
  * @param messages - The array of message objects.
19
- * @returns - The updated array of message objects with cache control added.
102
+ * @returns - A new array of message objects with cache control added.
20
103
  */
21
104
  function addCacheControl(messages) {
22
105
  if (!Array.isArray(messages) || messages.length < 2) {
@@ -25,55 +108,59 @@ function addCacheControl(messages) {
25
108
  const updatedMessages = [...messages];
26
109
  let userMessagesModified = 0;
27
110
  for (let i = updatedMessages.length - 1; i >= 0; i--) {
28
- const message = updatedMessages[i];
29
- const isUserMessage = ('getType' in message && message.getType() === 'human') ||
30
- ('role' in message && message.role === 'user');
31
- if (Array.isArray(message.content)) {
32
- message.content = message.content.filter((block) => !isCachePoint(block));
33
- for (let j = 0; j < message.content.length; j++) {
34
- const block = message.content[j];
111
+ const originalMessage = updatedMessages[i];
112
+ const content = originalMessage.content;
113
+ const isUserMessage = ('getType' in originalMessage && originalMessage.getType() === 'human') ||
114
+ ('role' in originalMessage && originalMessage.role === 'user');
115
+ const hasArrayContent = Array.isArray(content);
116
+ const needsStripping = hasArrayContent &&
117
+ needsCacheStripping(content);
118
+ const needsCacheAdd = userMessagesModified < 2 &&
119
+ isUserMessage &&
120
+ (typeof content === 'string' || hasArrayContent);
121
+ if (!needsStripping && !needsCacheAdd) {
122
+ continue;
123
+ }
124
+ let workingContent;
125
+ if (hasArrayContent) {
126
+ workingContent = deepCloneContent(content).filter((block) => !isCachePoint(block));
127
+ for (let j = 0; j < workingContent.length; j++) {
128
+ const block = workingContent[j];
35
129
  if ('cache_control' in block) {
36
130
  delete block.cache_control;
37
131
  }
38
132
  }
39
133
  }
134
+ else if (typeof content === 'string') {
135
+ workingContent = [
136
+ { type: 'text', text: content },
137
+ ];
138
+ }
139
+ else {
140
+ workingContent = [];
141
+ }
40
142
  if (userMessagesModified >= 2 || !isUserMessage) {
143
+ updatedMessages[i] = cloneMessage(originalMessage, workingContent);
41
144
  continue;
42
145
  }
43
- if (typeof message.content === 'string') {
44
- message.content = [
45
- {
46
- type: 'text',
47
- text: message.content,
48
- cache_control: { type: 'ephemeral' },
49
- },
50
- ];
51
- userMessagesModified++;
52
- }
53
- else if (Array.isArray(message.content)) {
54
- for (let j = message.content.length - 1; j >= 0; j--) {
55
- const contentPart = message.content[j];
56
- if ('type' in contentPart && contentPart.type === 'text') {
57
- contentPart.cache_control = {
58
- type: 'ephemeral',
59
- };
60
- userMessagesModified++;
61
- break;
62
- }
146
+ for (let j = workingContent.length - 1; j >= 0; j--) {
147
+ const contentPart = workingContent[j];
148
+ if ('type' in contentPart && contentPart.type === 'text') {
149
+ contentPart.cache_control = {
150
+ type: 'ephemeral',
151
+ };
152
+ userMessagesModified++;
153
+ break;
63
154
  }
64
155
  }
156
+ updatedMessages[i] = cloneMessage(originalMessage, workingContent);
65
157
  }
66
158
  return updatedMessages;
67
159
  }
68
- /**
69
- * Checks if a content block is a cache point
70
- */
71
- function isCachePoint(block) {
72
- return 'cachePoint' in block && !('type' in block);
73
- }
74
160
  /**
75
161
  * Removes all Anthropic cache_control fields from messages
76
162
  * Used when switching from Anthropic to Bedrock provider
163
+ * Returns a new array - only clones messages that require modification.
77
164
  */
78
165
  function stripAnthropicCacheControl(messages) {
79
166
  if (!Array.isArray(messages)) {
@@ -81,22 +168,26 @@ function stripAnthropicCacheControl(messages) {
81
168
  }
82
169
  const updatedMessages = [...messages];
83
170
  for (let i = 0; i < updatedMessages.length; i++) {
84
- const message = updatedMessages[i];
85
- const content = message.content;
86
- if (Array.isArray(content)) {
87
- for (let j = 0; j < content.length; j++) {
88
- const block = content[j];
89
- if ('cache_control' in block) {
90
- delete block.cache_control;
91
- }
171
+ const originalMessage = updatedMessages[i];
172
+ const content = originalMessage.content;
173
+ if (!Array.isArray(content) || !hasAnthropicCacheControl(content)) {
174
+ continue;
175
+ }
176
+ const clonedContent = deepCloneContent(content);
177
+ for (let j = 0; j < clonedContent.length; j++) {
178
+ const block = clonedContent[j];
179
+ if ('cache_control' in block) {
180
+ delete block.cache_control;
92
181
  }
93
182
  }
183
+ updatedMessages[i] = cloneMessage(originalMessage, clonedContent);
94
184
  }
95
185
  return updatedMessages;
96
186
  }
97
187
  /**
98
188
  * Removes all Bedrock cachePoint blocks from messages
99
189
  * Used when switching from Bedrock to Anthropic provider
190
+ * Returns a new array - only clones messages that require modification.
100
191
  */
101
192
  function stripBedrockCacheControl(messages) {
102
193
  if (!Array.isArray(messages)) {
@@ -104,11 +195,13 @@ function stripBedrockCacheControl(messages) {
104
195
  }
105
196
  const updatedMessages = [...messages];
106
197
  for (let i = 0; i < updatedMessages.length; i++) {
107
- const message = updatedMessages[i];
108
- const content = message.content;
109
- if (Array.isArray(content)) {
110
- message.content = content.filter((block) => !isCachePoint(block));
198
+ const originalMessage = updatedMessages[i];
199
+ const content = originalMessage.content;
200
+ if (!Array.isArray(content) || !hasBedrockCachePoint(content)) {
201
+ continue;
111
202
  }
203
+ const clonedContent = deepCloneContent(content).filter((block) => !isCachePoint(block));
204
+ updatedMessages[i] = cloneMessage(originalMessage, clonedContent);
112
205
  }
113
206
  return updatedMessages;
114
207
  }
@@ -1 +1 @@
1
- {"version":3,"file":"cache.cjs","sources":["../../../src/messages/cache.ts"],"sourcesContent":["import {\n BaseMessage,\n MessageContentComplex,\n AIMessage,\n HumanMessage,\n SystemMessage,\n ToolMessage,\n} from '@langchain/core/messages';\nimport type { AnthropicMessage } from '@/types/messages';\nimport type Anthropic from '@anthropic-ai/sdk';\nimport { ContentTypes } from '@/common/enum';\n\ntype MessageWithContent = {\n content?: string | MessageContentComplex[];\n};\n\n/** Debug logger for cache operations - set ILLUMA_DEBUG_CACHE=true to enable */\nconst debugCache = (message: string, data?: unknown): void => {\n if (process.env.ILLUMA_DEBUG_CACHE === 'true') {\n // eslint-disable-next-line no-console\n console.log(\n `[Cache] ${message}`,\n data !== undefined ? JSON.stringify(data, null, 2) : ''\n );\n }\n};\n\n/**\n * Deep clones a message's content to prevent mutation of the original.\n */\nfunction deepCloneContent<T extends string | MessageContentComplex[]>(\n content: T\n): T {\n if (typeof content === 'string') {\n return content;\n }\n if (Array.isArray(content)) {\n return content.map((block) => ({ ...block })) as T;\n }\n return content;\n}\n\n/**\n * Simple shallow clone with deep-cloned content.\n * Used for stripping cache control where we don't need proper LangChain instances.\n */\nfunction _shallowCloneMessage<T extends MessageWithContent>(message: T): T {\n const cloned = {\n ...message,\n content: deepCloneContent(message.content ?? ''),\n } as T;\n const lcKwargs = (cloned as Record<string, unknown>).lc_kwargs as\n | Record<string, unknown>\n | undefined;\n if (lcKwargs != null) {\n (cloned as Record<string, unknown>).lc_kwargs = {\n ...lcKwargs,\n content: cloned.content,\n };\n }\n return cloned;\n}\n\n/**\n * Creates a new LangChain message instance with the given content.\n * Required when adding cache points to ensure proper serialization.\n */\nfunction _createNewMessage<T extends MessageWithContent>(\n message: T,\n content: MessageContentComplex[]\n): T {\n if ('getType' in message && typeof message.getType === 'function') {\n const baseMsg = message as unknown as BaseMessage;\n const msgType = baseMsg.getType();\n\n const baseFields = {\n content,\n name: baseMsg.name,\n additional_kwargs: { ...baseMsg.additional_kwargs },\n response_metadata: { ...baseMsg.response_metadata },\n id: baseMsg.id,\n };\n\n switch (msgType) {\n case 'human':\n return new HumanMessage(baseFields) as unknown as T;\n case 'ai': {\n const aiMsg = baseMsg as AIMessage;\n return new AIMessage({\n ...baseFields,\n tool_calls: aiMsg.tool_calls ? [...aiMsg.tool_calls] : [],\n invalid_tool_calls: aiMsg.invalid_tool_calls\n ? [...aiMsg.invalid_tool_calls]\n : [],\n usage_metadata: aiMsg.usage_metadata,\n }) as unknown as T;\n }\n case 'system':\n return new SystemMessage(baseFields) as unknown as T;\n case 'tool': {\n const toolMsg = baseMsg as ToolMessage;\n return new ToolMessage({\n ...baseFields,\n tool_call_id: toolMsg.tool_call_id,\n status: toolMsg.status,\n artifact: toolMsg.artifact,\n }) as unknown as T;\n }\n default:\n break;\n }\n }\n\n const cloned = { ...message, content } as T;\n const lcKwargs = (cloned as Record<string, unknown>).lc_kwargs as\n | Record<string, unknown>\n | undefined;\n if (lcKwargs != null) {\n (cloned as Record<string, unknown>).lc_kwargs = { ...lcKwargs, content };\n }\n return cloned;\n}\n\n/**\n * Anthropic API: Adds cache control to the appropriate user messages in the payload.\n * Strips ALL existing cache control (both Anthropic and Bedrock formats) from all messages,\n * then adds fresh cache control to the last 2 user messages in a single backward pass.\n * This ensures we don't accumulate stale cache points across multiple turns.\n * @param messages - The array of message objects.\n * @returns - The updated array of message objects with cache control added.\n */\nexport function addCacheControl<T extends AnthropicMessage | BaseMessage>(\n messages: T[]\n): T[] {\n if (!Array.isArray(messages) || messages.length < 2) {\n return messages;\n }\n\n const updatedMessages = [...messages];\n let userMessagesModified = 0;\n\n for (let i = updatedMessages.length - 1; i >= 0; i--) {\n const message = updatedMessages[i];\n const isUserMessage =\n ('getType' in message && message.getType() === 'human') ||\n ('role' in message && message.role === 'user');\n\n if (Array.isArray(message.content)) {\n message.content = message.content.filter(\n (block) => !isCachePoint(block as MessageContentComplex)\n ) as typeof message.content;\n\n for (let j = 0; j < message.content.length; j++) {\n const block = message.content[j] as Record<string, unknown>;\n if ('cache_control' in block) {\n delete block.cache_control;\n }\n }\n }\n\n if (userMessagesModified >= 2 || !isUserMessage) {\n continue;\n }\n\n if (typeof message.content === 'string') {\n message.content = [\n {\n type: 'text',\n text: message.content,\n cache_control: { type: 'ephemeral' },\n },\n ];\n userMessagesModified++;\n } else if (Array.isArray(message.content)) {\n for (let j = message.content.length - 1; j >= 0; j--) {\n const contentPart = message.content[j];\n if ('type' in contentPart && contentPart.type === 'text') {\n (contentPart as Anthropic.TextBlockParam).cache_control = {\n type: 'ephemeral',\n };\n userMessagesModified++;\n break;\n }\n }\n }\n }\n\n return updatedMessages;\n}\n\n/**\n * Checks if a content block is a cache point\n */\nfunction isCachePoint(block: MessageContentComplex): boolean {\n return 'cachePoint' in block && !('type' in block);\n}\n\n/**\n * Removes all Anthropic cache_control fields from messages\n * Used when switching from Anthropic to Bedrock provider\n */\nexport function stripAnthropicCacheControl<T extends MessageWithContent>(\n messages: T[]\n): T[] {\n if (!Array.isArray(messages)) {\n return messages;\n }\n\n const updatedMessages = [...messages];\n\n for (let i = 0; i < updatedMessages.length; i++) {\n const message = updatedMessages[i];\n const content = message.content;\n\n if (Array.isArray(content)) {\n for (let j = 0; j < content.length; j++) {\n const block = content[j] as Record<string, unknown>;\n if ('cache_control' in block) {\n delete block.cache_control;\n }\n }\n }\n }\n\n return updatedMessages;\n}\n\n/**\n * Removes all Bedrock cachePoint blocks from messages\n * Used when switching from Bedrock to Anthropic provider\n */\nexport function stripBedrockCacheControl<T extends MessageWithContent>(\n messages: T[]\n): T[] {\n if (!Array.isArray(messages)) {\n return messages;\n }\n\n const updatedMessages = [...messages];\n\n for (let i = 0; i < updatedMessages.length; i++) {\n const message = updatedMessages[i];\n const content = message.content;\n\n if (Array.isArray(content)) {\n message.content = content.filter(\n (block) => !isCachePoint(block as MessageContentComplex)\n ) as typeof content;\n }\n }\n\n return updatedMessages;\n}\n\n/**\n * Adds Bedrock Converse API cache points using \"Stable Prefix Caching\" strategy.\n *\n * STRATEGY: Place cache point after the LAST ASSISTANT message only.\n * This ensures the prefix (everything before the cache point) remains STABLE\n * as the conversation grows, maximizing cache hits.\n *\n * Why this works:\n * - System message has its own cachePoint (added in AgentContext)\n * - Tools have their own cachePoint (added in CustomChatBedrockConverse)\n * - Conversation history grows, but the PREFIX stays the same\n * - Only the NEW user message is uncached (it's always different)\n *\n * Example conversation flow:\n * Request 1: [System+cachePoint][Tools+cachePoint][User1] → No conversation cache yet\n * Request 2: [System][Tools][User1][Assistant1+cachePoint][User2] → Cache User1+Assistant1\n * Request 3: [System][Tools][User1][Assistant1][User2][Assistant2+cachePoint][User3]\n * → Cache reads User1+A1+User2+A2, cache writes new portion\n *\n * Claude's \"Simplified Cache Management\" automatically looks back up to 20 content\n * blocks from the cache checkpoint to find the longest matching prefix.\n *\n * @param messages - The array of message objects (excluding system message).\n * @returns - The updated array with a single cache point after the last assistant message.\n */\nexport function addBedrockCacheControl<\n T extends Partial<BaseMessage> & MessageWithContent,\n>(messages: T[]): T[] {\n if (!Array.isArray(messages) || messages.length < 1) {\n debugCache('addBedrockCacheControl: Skipping - no messages', {\n count: messages.length,\n });\n return messages;\n }\n\n debugCache(\n 'addBedrockCacheControl: Processing messages with stable prefix strategy',\n {\n count: messages.length,\n }\n );\n\n const updatedMessages: T[] = messages.slice();\n\n // First pass: Remove ALL existing cache points to ensure clean state\n // This prevents accumulation of stale cache points\n for (const message of updatedMessages) {\n const content = message.content;\n if (Array.isArray(content)) {\n message.content = content.filter(\n (block) => !isCachePoint(block)\n ) as typeof content;\n\n // Also remove Anthropic-style cache_control\n for (let j = 0; j < message.content.length; j++) {\n const block = message.content[j] as Record<string, unknown>;\n if ('cache_control' in block) {\n delete block.cache_control;\n }\n }\n }\n }\n\n // Helper function to check if a message contains reasoning/thinking blocks\n const hasReasoningBlock = (message: T): boolean => {\n const content = message.content;\n if (!Array.isArray(content)) {\n return false;\n }\n for (const block of content) {\n const type = (block as { type?: string }).type;\n // Check for all reasoning/thinking block types:\n // - reasoning_content: Bedrock Anthropic extended thinking\n // - reasoning: Generic reasoning format\n // - thinking: Anthropic direct API thinking\n // - redacted_thinking: Anthropic redacted thinking blocks\n if (\n type === 'reasoning_content' ||\n type === 'reasoning' ||\n type === 'thinking' ||\n type === 'redacted_thinking'\n ) {\n return true;\n }\n }\n return false;\n };\n\n // Second pass: Find the LAST assistant message WITHOUT reasoning blocks and add a cache point there\n // Messages with reasoning/thinking blocks cannot have cache points after them (Bedrock limitation)\n let lastAssistantIndex = -1;\n let skippedWithReasoning = 0;\n\n // Count message types for logging\n const messageTypes: Record<string, number> = {};\n for (const message of updatedMessages) {\n const msgType =\n 'getType' in message && typeof message.getType === 'function'\n ? message.getType()\n : 'unknown';\n messageTypes[msgType] = (messageTypes[msgType] || 0) + 1;\n }\n\n for (let i = updatedMessages.length - 1; i >= 0; i--) {\n const message = updatedMessages[i];\n const messageType =\n 'getType' in message && typeof message.getType === 'function'\n ? message.getType()\n : 'unknown';\n\n if (messageType === 'ai') {\n // Skip assistant messages with reasoning blocks - cache points not allowed after them\n if (hasReasoningBlock(message)) {\n skippedWithReasoning++;\n debugCache(\n 'addBedrockCacheControl: Skipping assistant message with reasoning block',\n { index: i }\n );\n continue;\n }\n lastAssistantIndex = i;\n break;\n }\n }\n\n // Log message summary\n debugCache(\n `📨 Messages | total=${updatedMessages.length} | ${Object.entries(\n messageTypes\n )\n .map(([k, v]) => `${k}:${v}`)\n .join(' ')} | skippedReasoning=${skippedWithReasoning}`\n );\n\n // If no suitable assistant message found, skip conversation caching\n // (System and Tools caching are still handled separately)\n if (lastAssistantIndex === -1) {\n debugCache(\n '📨 Messages | No suitable assistant message for cachePoint (first turn or all have reasoning)'\n );\n return updatedMessages;\n }\n\n // Add cache point to the last assistant message (without reasoning blocks)\n const assistantMessage = updatedMessages[lastAssistantIndex];\n const content = assistantMessage.content;\n\n if (typeof content === 'string' && content !== '') {\n assistantMessage.content = [\n { type: ContentTypes.TEXT, text: content },\n { cachePoint: { type: 'default' } },\n ] as MessageContentComplex[];\n debugCache(\n `📍 Message cachePoint at index ${lastAssistantIndex} (string, ${content.length} chars)`\n );\n debugCache(\n 'addBedrockCacheControl: Added cachePoint to assistant message (string content)',\n {\n index: lastAssistantIndex,\n contentLength: content.length,\n }\n );\n } else if (\n Array.isArray(assistantMessage.content) &&\n assistantMessage.content.length > 0\n ) {\n // Double-check: If this message has reasoning blocks, skip adding cache point entirely\n // This handles edge cases where the initial skip check might have missed it\n if (hasReasoningBlock(assistantMessage)) {\n debugCache(\n `⚠️ Message cachePoint SKIPPED at index ${lastAssistantIndex} (has reasoning blocks)`\n );\n debugCache(\n 'addBedrockCacheControl: Skipping - assistant message has reasoning blocks (safety check)',\n {\n index: lastAssistantIndex,\n }\n );\n return updatedMessages;\n }\n\n // Find the last text block and insert cache point after it\n let inserted = false;\n for (let j = assistantMessage.content.length - 1; j >= 0; j--) {\n const block = assistantMessage.content[j] as MessageContentComplex;\n const type = (block as { type?: string }).type;\n if (type === ContentTypes.TEXT || type === 'text') {\n const text = (block as { text?: string }).text;\n if (text != null && text !== '') {\n assistantMessage.content.splice(j + 1, 0, {\n cachePoint: { type: 'default' },\n } as MessageContentComplex);\n inserted = true;\n debugCache(\n `📍 Message cachePoint at index ${lastAssistantIndex} (array, block ${j}, ${text.length} chars)`\n );\n debugCache(\n 'addBedrockCacheControl: Added cachePoint after text block in assistant message',\n {\n index: lastAssistantIndex,\n textBlockIndex: j,\n contentLength: text.length,\n }\n );\n break;\n }\n }\n }\n\n // If no text block found, don't append cache point as the message structure is unexpected\n if (!inserted) {\n const contentTypes = assistantMessage.content.map(\n (b) => (b as { type?: string }).type\n );\n debugCache(\n `⚠️ Message cachePoint SKIPPED at index ${lastAssistantIndex} (no text block, types: ${contentTypes.join(',')})`\n );\n debugCache(\n 'addBedrockCacheControl: No suitable text block found, skipping cache point',\n {\n index: lastAssistantIndex,\n contentTypes,\n }\n );\n }\n }\n\n debugCache(\n 'addBedrockCacheControl: Complete - stable prefix caching applied',\n {\n lastAssistantIndex,\n totalMessages: updatedMessages.length,\n }\n );\n\n return updatedMessages;\n}\n"],"names":["ContentTypes"],"mappings":";;;;;AAgBA;AACA,MAAM,UAAU,GAAG,CAAC,OAAe,EAAE,IAAc,KAAU;IAC3D,IAAI,OAAO,CAAC,GAAG,CAAC,kBAAkB,KAAK,MAAM,EAAE;;AAE7C,QAAA,OAAO,CAAC,GAAG,CACT,CAAA,QAAA,EAAW,OAAO,CAAA,CAAE,EACpB,IAAI,KAAK,SAAS,GAAG,IAAI,CAAC,SAAS,CAAC,IAAI,EAAE,IAAI,EAAE,CAAC,CAAC,GAAG,EAAE,CACxD;;AAEL,CAAC;AAkGD;;;;;;;AAOG;AACG,SAAU,eAAe,CAC7B,QAAa,EAAA;AAEb,IAAA,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,QAAQ,CAAC,IAAI,QAAQ,CAAC,MAAM,GAAG,CAAC,EAAE;AACnD,QAAA,OAAO,QAAQ;;AAGjB,IAAA,MAAM,eAAe,GAAG,CAAC,GAAG,QAAQ,CAAC;IACrC,IAAI,oBAAoB,GAAG,CAAC;AAE5B,IAAA,KAAK,IAAI,CAAC,GAAG,eAAe,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC,IAAI,CAAC,EAAE,CAAC,EAAE,EAAE;AACpD,QAAA,MAAM,OAAO,GAAG,eAAe,CAAC,CAA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,gFAAgF,EAChF;AACE,wBAAA,KAAK,EAAE,kBAAkB;AACzB,wBAAA,cAAc,EAAE,CAAC;wBACjB,aAAa,EAAE,IAAI,CAAC,MAAM;AAC3B,qBAAA,CACF;oBACD;;;;;QAMN,IAAI,CAAC,QAAQ,EAAE;AACb,YAAA,MAAM,YAAY,GAAG,gBAAgB,CAAC,OAAO,CAAC,GAAG,CAC/C,CAAC,CAAC,KAAM,CAAuB,CAAC,IAAI,CACrC;AACD,YAAA,UAAU,CACR,CAAA,uCAAA,EAA0C,kBAAkB,CAAA,wBAAA,EAA2B,YAAY,CAAC,IAAI,CAAC,GAAG,CAAC,CAAG,CAAA,CAAA,CACjH;YACD,UAAU,CACR,4EAA4E,EAC5E;AACE,gBAAA,KAAK,EAAE,kBAAkB;gBACzB,YAAY;AACb,aAAA,CACF;;;IAIL,UAAU,CACR,kEAAkE,EAClE;QACE,kBAAkB;QAClB,aAAa,EAAE,eAAe,CAAC,MAAM;AACtC,KAAA,CACF;AAED,IAAA,OAAO,eAAe;AACxB;;;;;;;"}
1
+ {"version":3,"file":"cache.cjs","sources":["../../../src/messages/cache.ts"],"sourcesContent":["import { BaseMessage, MessageContentComplex } from '@langchain/core/messages';\nimport type { AnthropicMessage } from '@/types/messages';\nimport type Anthropic from '@anthropic-ai/sdk';\nimport { ContentTypes } from '@/common/enum';\n\ntype MessageWithContent = {\n content?: string | MessageContentComplex[];\n};\n\n/** Debug logger for cache operations - set ILLUMA_DEBUG_CACHE=true to enable */\nconst debugCache = (message: string, data?: unknown): void => {\n if (process.env.ILLUMA_DEBUG_CACHE === 'true') {\n // eslint-disable-next-line no-console\n console.log(\n `[Cache] ${message}`,\n data !== undefined ? JSON.stringify(data, null, 2) : ''\n );\n }\n};\n\n/**\n * Deep clones a message's content to prevent mutation of the original.\n */\nfunction deepCloneContent<T extends string | MessageContentComplex[]>(\n content: T\n): T {\n if (typeof content === 'string') {\n return content;\n }\n if (Array.isArray(content)) {\n return content.map((block) => ({ ...block })) as T;\n }\n return content;\n}\n\n/**\n * Clones a message with deep-cloned content, explicitly excluding LangChain\n * serialization metadata to prevent coercion issues.\n * Keeps lc_kwargs in sync with content to prevent LangChain serialization issues.\n */\nfunction cloneMessage<T extends MessageWithContent>(\n message: T,\n content: string | MessageContentComplex[]\n): T {\n const {\n lc_kwargs: _lc_kwargs,\n lc_serializable: _lc_serializable,\n lc_namespace: _lc_namespace,\n ...rest\n } = message as T & {\n lc_kwargs?: unknown;\n lc_serializable?: unknown;\n lc_namespace?: unknown;\n };\n\n const cloned = { ...rest, content } as T;\n\n // Sync lc_kwargs.content with the new content to prevent LangChain coercion issues\n const lcKwargs = (message as Record<string, unknown>).lc_kwargs as\n | Record<string, unknown>\n | undefined;\n if (lcKwargs != null) {\n (cloned as Record<string, unknown>).lc_kwargs = {\n ...lcKwargs,\n content: content,\n };\n }\n\n // LangChain messages don't have a direct 'role' property - derive it from getType()\n if (\n 'getType' in message &&\n typeof message.getType === 'function' &&\n !('role' in cloned)\n ) {\n const msgType = (message as unknown as BaseMessage).getType();\n const roleMap: Record<string, string> = {\n human: 'user',\n ai: 'assistant',\n system: 'system',\n tool: 'tool',\n };\n (cloned as Record<string, unknown>).role = roleMap[msgType] || msgType;\n }\n\n return cloned;\n}\n\n/**\n * Checks if a content block is a cache point\n */\nfunction isCachePoint(block: MessageContentComplex): boolean {\n return 'cachePoint' in block && !('type' in block);\n}\n\n/**\n * Checks if a message's content needs cache control stripping.\n * Returns true if content has cachePoint blocks or cache_control fields.\n */\nfunction needsCacheStripping(content: MessageContentComplex[]): boolean {\n for (let i = 0; i < content.length; i++) {\n const block = content[i];\n if (isCachePoint(block)) return true;\n if ('cache_control' in block) return true;\n }\n return false;\n}\n\n/**\n * Checks if a message's content has Anthropic cache_control fields.\n */\nfunction hasAnthropicCacheControl(content: MessageContentComplex[]): boolean {\n for (let i = 0; i < content.length; i++) {\n if ('cache_control' in content[i]) return true;\n }\n return false;\n}\n\n/**\n * Checks if a message's content has Bedrock cachePoint blocks.\n */\nfunction hasBedrockCachePoint(content: MessageContentComplex[]): boolean {\n for (let i = 0; i < content.length; i++) {\n if (isCachePoint(content[i])) return true;\n }\n return false;\n}\n\n/**\n * Anthropic API: Adds cache control to the appropriate user messages in the payload.\n * Strips ALL existing cache control (both Anthropic and Bedrock formats) from all messages,\n * then adds fresh cache control to the last 2 user messages in a single backward pass.\n * This ensures we don't accumulate stale cache points across multiple turns.\n * Returns a new array - only clones messages that require modification.\n * @param messages - The array of message objects.\n * @returns - A new array of message objects with cache control added.\n */\nexport function addCacheControl<T extends AnthropicMessage | BaseMessage>(\n messages: T[]\n): T[] {\n if (!Array.isArray(messages) || messages.length < 2) {\n return messages;\n }\n\n const updatedMessages: T[] = [...messages];\n let userMessagesModified = 0;\n\n for (let i = updatedMessages.length - 1; i >= 0; i--) {\n const originalMessage = updatedMessages[i];\n const content = originalMessage.content;\n const isUserMessage =\n ('getType' in originalMessage && originalMessage.getType() === 'human') ||\n ('role' in originalMessage && originalMessage.role === 'user');\n\n const hasArrayContent = Array.isArray(content);\n const needsStripping =\n hasArrayContent &&\n needsCacheStripping(content as MessageContentComplex[]);\n const needsCacheAdd =\n userMessagesModified < 2 &&\n isUserMessage &&\n (typeof content === 'string' || hasArrayContent);\n\n if (!needsStripping && !needsCacheAdd) {\n continue;\n }\n\n let workingContent: MessageContentComplex[];\n\n if (hasArrayContent) {\n workingContent = deepCloneContent(\n content as MessageContentComplex[]\n ).filter((block) => !isCachePoint(block as MessageContentComplex));\n\n for (let j = 0; j < workingContent.length; j++) {\n const block = workingContent[j] as Record<string, unknown>;\n if ('cache_control' in block) {\n delete block.cache_control;\n }\n }\n } else if (typeof content === 'string') {\n workingContent = [\n { type: 'text', text: content },\n ] as MessageContentComplex[];\n } else {\n workingContent = [];\n }\n\n if (userMessagesModified >= 2 || !isUserMessage) {\n updatedMessages[i] = cloneMessage(\n originalMessage as MessageWithContent,\n workingContent\n ) as T;\n continue;\n }\n\n for (let j = workingContent.length - 1; j >= 0; j--) {\n const contentPart = workingContent[j];\n if ('type' in contentPart && contentPart.type === 'text') {\n (contentPart as Anthropic.TextBlockParam).cache_control = {\n type: 'ephemeral',\n };\n userMessagesModified++;\n break;\n }\n }\n\n updatedMessages[i] = cloneMessage(\n originalMessage as MessageWithContent,\n workingContent\n ) as T;\n }\n\n return updatedMessages;\n}\n\n/**\n * Removes all Anthropic cache_control fields from messages\n * Used when switching from Anthropic to Bedrock provider\n * Returns a new array - only clones messages that require modification.\n */\nexport function stripAnthropicCacheControl<T extends MessageWithContent>(\n messages: T[]\n): T[] {\n if (!Array.isArray(messages)) {\n return messages;\n }\n\n const updatedMessages: T[] = [...messages];\n\n for (let i = 0; i < updatedMessages.length; i++) {\n const originalMessage = updatedMessages[i];\n const content = originalMessage.content;\n\n if (!Array.isArray(content) || !hasAnthropicCacheControl(content)) {\n continue;\n }\n\n const clonedContent = deepCloneContent(content);\n for (let j = 0; j < clonedContent.length; j++) {\n const block = clonedContent[j] as Record<string, unknown>;\n if ('cache_control' in block) {\n delete block.cache_control;\n }\n }\n updatedMessages[i] = cloneMessage(originalMessage, clonedContent) as T;\n }\n\n return updatedMessages;\n}\n\n/**\n * Removes all Bedrock cachePoint blocks from messages\n * Used when switching from Bedrock to Anthropic provider\n * Returns a new array - only clones messages that require modification.\n */\nexport function stripBedrockCacheControl<T extends MessageWithContent>(\n messages: T[]\n): T[] {\n if (!Array.isArray(messages)) {\n return messages;\n }\n\n const updatedMessages: T[] = [...messages];\n\n for (let i = 0; i < updatedMessages.length; i++) {\n const originalMessage = updatedMessages[i];\n const content = originalMessage.content;\n\n if (!Array.isArray(content) || !hasBedrockCachePoint(content)) {\n continue;\n }\n\n const clonedContent = deepCloneContent(content).filter(\n (block) => !isCachePoint(block as MessageContentComplex)\n );\n updatedMessages[i] = cloneMessage(originalMessage, clonedContent) as T;\n }\n\n return updatedMessages;\n}\n\n/**\n * Adds Bedrock Converse API cache points using \"Stable Prefix Caching\" strategy.\n *\n * STRATEGY: Place cache point after the LAST ASSISTANT message only.\n * This ensures the prefix (everything before the cache point) remains STABLE\n * as the conversation grows, maximizing cache hits.\n *\n * Why this works:\n * - System message has its own cachePoint (added in AgentContext)\n * - Tools have their own cachePoint (added in CustomChatBedrockConverse)\n * - Conversation history grows, but the PREFIX stays the same\n * - Only the NEW user message is uncached (it's always different)\n *\n * Example conversation flow:\n * Request 1: [System+cachePoint][Tools+cachePoint][User1] → No conversation cache yet\n * Request 2: [System][Tools][User1][Assistant1+cachePoint][User2] → Cache User1+Assistant1\n * Request 3: [System][Tools][User1][Assistant1][User2][Assistant2+cachePoint][User3]\n * → Cache reads User1+A1+User2+A2, cache writes new portion\n *\n * Claude's \"Simplified Cache Management\" automatically looks back up to 20 content\n * blocks from the cache checkpoint to find the longest matching prefix.\n *\n * @param messages - The array of message objects (excluding system message).\n * @returns - The updated array with a single cache point after the last assistant message.\n */\nexport function addBedrockCacheControl<\n T extends Partial<BaseMessage> & MessageWithContent,\n>(messages: T[]): T[] {\n if (!Array.isArray(messages) || messages.length < 1) {\n debugCache('addBedrockCacheControl: Skipping - no messages', {\n count: messages.length,\n });\n return messages;\n }\n\n debugCache(\n 'addBedrockCacheControl: Processing messages with stable prefix strategy',\n {\n count: messages.length,\n }\n );\n\n const updatedMessages: T[] = messages.slice();\n\n // First pass: Remove ALL existing cache points to ensure clean state\n // This prevents accumulation of stale cache points\n for (const message of updatedMessages) {\n const content = message.content;\n if (Array.isArray(content)) {\n message.content = content.filter(\n (block) => !isCachePoint(block)\n ) as typeof content;\n\n // Also remove Anthropic-style cache_control\n for (let j = 0; j < message.content.length; j++) {\n const block = message.content[j] as Record<string, unknown>;\n if ('cache_control' in block) {\n delete block.cache_control;\n }\n }\n }\n }\n\n // Helper function to check if a message contains reasoning/thinking blocks\n const hasReasoningBlock = (message: T): boolean => {\n const content = message.content;\n if (!Array.isArray(content)) {\n return false;\n }\n for (const block of content) {\n const type = (block as { type?: string }).type;\n // Check for all reasoning/thinking block types:\n // - reasoning_content: Bedrock Anthropic extended thinking\n // - reasoning: Generic reasoning format\n // - thinking: Anthropic direct API thinking\n // - redacted_thinking: Anthropic redacted thinking blocks\n if (\n type === 'reasoning_content' ||\n type === 'reasoning' ||\n type === 'thinking' ||\n type === 'redacted_thinking'\n ) {\n return true;\n }\n }\n return false;\n };\n\n // Second pass: Find the LAST assistant message WITHOUT reasoning blocks and add a cache point there\n // Messages with reasoning/thinking blocks cannot have cache points after them (Bedrock limitation)\n let lastAssistantIndex = -1;\n let skippedWithReasoning = 0;\n\n // Count message types for logging\n const messageTypes: Record<string, number> = {};\n for (const message of updatedMessages) {\n const msgType =\n 'getType' in message && typeof message.getType === 'function'\n ? message.getType()\n : 'unknown';\n messageTypes[msgType] = (messageTypes[msgType] || 0) + 1;\n }\n\n for (let i = updatedMessages.length - 1; i >= 0; i--) {\n const message = updatedMessages[i];\n const messageType =\n 'getType' in message && typeof message.getType === 'function'\n ? message.getType()\n : 'unknown';\n\n if (messageType === 'ai') {\n // Skip assistant messages with reasoning blocks - cache points not allowed after them\n if (hasReasoningBlock(message)) {\n skippedWithReasoning++;\n debugCache(\n 'addBedrockCacheControl: Skipping assistant message with reasoning block',\n { index: i }\n );\n continue;\n }\n lastAssistantIndex = i;\n break;\n }\n }\n\n // Log message summary\n debugCache(\n `📨 Messages | total=${updatedMessages.length} | ${Object.entries(\n messageTypes\n )\n .map(([k, v]) => `${k}:${v}`)\n .join(' ')} | skippedReasoning=${skippedWithReasoning}`\n );\n\n // If no suitable assistant message found, skip conversation caching\n // (System and Tools caching are still handled separately)\n if (lastAssistantIndex === -1) {\n debugCache(\n '📨 Messages | No suitable assistant message for cachePoint (first turn or all have reasoning)'\n );\n return updatedMessages;\n }\n\n // Add cache point to the last assistant message (without reasoning blocks)\n const assistantMessage = updatedMessages[lastAssistantIndex];\n const content = assistantMessage.content;\n\n if (typeof content === 'string' && content !== '') {\n assistantMessage.content = [\n { type: ContentTypes.TEXT, text: content },\n { cachePoint: { type: 'default' } },\n ] as MessageContentComplex[];\n debugCache(\n `📍 Message cachePoint at index ${lastAssistantIndex} (string, ${content.length} chars)`\n );\n debugCache(\n 'addBedrockCacheControl: Added cachePoint to assistant message (string content)',\n {\n index: lastAssistantIndex,\n contentLength: content.length,\n }\n );\n } else if (\n Array.isArray(assistantMessage.content) &&\n assistantMessage.content.length > 0\n ) {\n // Double-check: If this message has reasoning blocks, skip adding cache point entirely\n // This handles edge cases where the initial skip check might have missed it\n if (hasReasoningBlock(assistantMessage)) {\n debugCache(\n `⚠️ Message cachePoint SKIPPED at index ${lastAssistantIndex} (has reasoning blocks)`\n );\n debugCache(\n 'addBedrockCacheControl: Skipping - assistant message has reasoning blocks (safety check)',\n {\n index: lastAssistantIndex,\n }\n );\n return updatedMessages;\n }\n\n // Find the last text block and insert cache point after it\n let inserted = false;\n for (let j = assistantMessage.content.length - 1; j >= 0; j--) {\n const block = assistantMessage.content[j] as MessageContentComplex;\n const type = (block as { type?: string }).type;\n if (type === ContentTypes.TEXT || type === 'text') {\n const text = (block as { text?: string }).text;\n if (text != null && text !== '') {\n assistantMessage.content.splice(j + 1, 0, {\n cachePoint: { type: 'default' },\n } as MessageContentComplex);\n inserted = true;\n debugCache(\n `📍 Message cachePoint at index ${lastAssistantIndex} (array, block ${j}, ${text.length} chars)`\n );\n debugCache(\n 'addBedrockCacheControl: Added cachePoint after text block in assistant message',\n {\n index: lastAssistantIndex,\n textBlockIndex: j,\n contentLength: text.length,\n }\n );\n break;\n }\n }\n }\n\n // If no text block found, don't append cache point as the message structure is unexpected\n if (!inserted) {\n const contentTypes = assistantMessage.content.map(\n (b) => (b as { type?: string }).type\n );\n debugCache(\n `⚠️ Message cachePoint SKIPPED at index ${lastAssistantIndex} (no text block, types: ${contentTypes.join(',')})`\n );\n debugCache(\n 'addBedrockCacheControl: No suitable text block found, skipping cache point',\n {\n index: lastAssistantIndex,\n contentTypes,\n }\n );\n }\n }\n\n debugCache(\n 'addBedrockCacheControl: Complete - stable prefix caching applied',\n {\n lastAssistantIndex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AACb,YAAA,MAAM,YAAY,GAAG,gBAAgB,CAAC,OAAO,CAAC,GAAG,CAC/C,CAAC,CAAC,KAAM,CAAuB,CAAC,IAAI,CACrC;AACD,YAAA,UAAU,CACR,CAAA,uCAAA,EAA0C,kBAAkB,CAAA,wBAAA,EAA2B,YAAY,CAAC,IAAI,CAAC,GAAG,CAAC,CAAG,CAAA,CAAA,CACjH;YACD,UAAU,CACR,4EAA4E,EAC5E;AACE,gBAAA,KAAK,EAAE,kBAAkB;gBACzB,YAAY;AACb,aAAA,CACF;;;IAIL,UAAU,CACR,kEAAkE,EAClE;QACE,kBAAkB;QAClB,aAAa,EAAE,eAAe,CAAC,MAAM;AACtC,KAAA,CACF;AAED,IAAA,OAAO,eAAe;AACxB;;;;;;;"}
@@ -0,0 +1,173 @@
1
+ 'use strict';
2
+
3
+ /**
4
+ * Validates structured output against a JSON Schema.
5
+ *
6
+ * @param output - The output to validate
7
+ * @param schema - JSON Schema to validate against
8
+ * @returns Validation result with success flag and data or error
9
+ */
10
+ function validateStructuredOutput(output, schema) {
11
+ try {
12
+ // Parse output if it's a string
13
+ const data = typeof output === 'string' ? JSON.parse(output) : output;
14
+ // Basic JSON Schema validation
15
+ const errors = validateAgainstJsonSchema(data, schema);
16
+ if (errors.length > 0) {
17
+ return {
18
+ success: false,
19
+ error: `Validation failed: ${errors.join('; ')}`,
20
+ raw: data,
21
+ };
22
+ }
23
+ return {
24
+ success: true,
25
+ data: data,
26
+ };
27
+ }
28
+ catch (e) {
29
+ return {
30
+ success: false,
31
+ error: `Failed to parse output: ${e.message}`,
32
+ raw: output,
33
+ };
34
+ }
35
+ }
36
+ /**
37
+ * Validates data against a JSON Schema (simplified implementation).
38
+ * For complex schemas, consider using ajv or similar library.
39
+ */
40
+ function validateAgainstJsonSchema(data, schema, path = '') {
41
+ const errors = [];
42
+ if (schema.type === 'object' && typeof data !== 'object') {
43
+ errors.push(`${path || 'root'}: expected object, got ${typeof data}`);
44
+ return errors;
45
+ }
46
+ if (schema.type === 'array' && !Array.isArray(data)) {
47
+ errors.push(`${path || 'root'}: expected array, got ${typeof data}`);
48
+ return errors;
49
+ }
50
+ if (schema.type === 'string' && typeof data !== 'string') {
51
+ errors.push(`${path || 'root'}: expected string, got ${typeof data}`);
52
+ return errors;
53
+ }
54
+ if (schema.type === 'number' && typeof data !== 'number') {
55
+ errors.push(`${path || 'root'}: expected number, got ${typeof data}`);
56
+ return errors;
57
+ }
58
+ if (schema.type === 'boolean' && typeof data !== 'boolean') {
59
+ errors.push(`${path || 'root'}: expected boolean, got ${typeof data}`);
60
+ return errors;
61
+ }
62
+ // Validate required properties
63
+ if (schema.type === 'object' &&
64
+ Array.isArray(schema.required) &&
65
+ typeof data === 'object' &&
66
+ data !== null) {
67
+ for (const requiredProp of schema.required) {
68
+ if (!(requiredProp in data)) {
69
+ errors.push(`${path || 'root'}: missing required property '${requiredProp}'`);
70
+ }
71
+ }
72
+ }
73
+ // Validate nested properties
74
+ if (schema.type === 'object' &&
75
+ schema.properties &&
76
+ typeof data === 'object' &&
77
+ data !== null) {
78
+ const properties = schema.properties;
79
+ for (const [key, propSchema] of Object.entries(properties)) {
80
+ if (key in data) {
81
+ const nestedErrors = validateAgainstJsonSchema(data[key], propSchema, path ? `${path}.${key}` : key);
82
+ errors.push(...nestedErrors);
83
+ }
84
+ }
85
+ }
86
+ // Validate array items
87
+ if (schema.type === 'array' && schema.items && Array.isArray(data)) {
88
+ const itemSchema = schema.items;
89
+ for (let i = 0; i < data.length; i++) {
90
+ const nestedErrors = validateAgainstJsonSchema(data[i], itemSchema, `${path || 'root'}[${i}]`);
91
+ errors.push(...nestedErrors);
92
+ }
93
+ }
94
+ // Validate enum values
95
+ if (schema.enum && Array.isArray(schema.enum)) {
96
+ if (!schema.enum.includes(data)) {
97
+ errors.push(`${path || 'root'}: value '${data}' not in enum [${schema.enum.join(', ')}]`);
98
+ }
99
+ }
100
+ return errors;
101
+ }
102
+ /**
103
+ * Converts a Zod schema to JSON Schema format.
104
+ * This is a simplified converter for common types.
105
+ */
106
+ function zodToJsonSchema(zodSchema) {
107
+ // Use the zod-to-json-schema library if available
108
+ // For now, provide a basic implementation
109
+ try {
110
+ // eslint-disable-next-line @typescript-eslint/no-require-imports
111
+ const { zodToJsonSchema: convert } = require('zod-to-json-schema');
112
+ return convert(zodSchema);
113
+ }
114
+ catch {
115
+ // Fallback: return a generic object schema
116
+ return {
117
+ type: 'object',
118
+ additionalProperties: true,
119
+ };
120
+ }
121
+ }
122
+ /**
123
+ * Creates a structured output error message for retry.
124
+ */
125
+ function createValidationErrorMessage(result, customMessage) {
126
+ if (customMessage) {
127
+ return customMessage;
128
+ }
129
+ return `The response did not match the expected schema. Error: ${result.error}. Please try again and ensure your response exactly matches the required format.`;
130
+ }
131
+ /**
132
+ * Checks if a value is a valid JSON Schema object.
133
+ */
134
+ function isValidJsonSchema(value) {
135
+ if (typeof value !== 'object' || value === null) {
136
+ return false;
137
+ }
138
+ const schema = value;
139
+ // Basic check: must have a type or properties
140
+ return (typeof schema.type === 'string' ||
141
+ typeof schema.properties === 'object' ||
142
+ typeof schema.$schema === 'string');
143
+ }
144
+ /**
145
+ * Normalizes a JSON Schema by adding defaults and cleaning up.
146
+ */
147
+ function normalizeJsonSchema(schema, config) {
148
+ const normalized = { ...schema };
149
+ // Ensure type is set
150
+ if (!normalized.type && normalized.properties) {
151
+ normalized.type = 'object';
152
+ }
153
+ // Add title from config name
154
+ if (config?.name && !normalized.title) {
155
+ normalized.title = config.name;
156
+ }
157
+ // Add description from config
158
+ if (config?.description && !normalized.description) {
159
+ normalized.description = config.description;
160
+ }
161
+ // Enable additionalProperties: false for strict mode
162
+ if (config?.strict !== false && normalized.type === 'object') {
163
+ normalized.additionalProperties = normalized.additionalProperties ?? false;
164
+ }
165
+ return normalized;
166
+ }
167
+
168
+ exports.createValidationErrorMessage = createValidationErrorMessage;
169
+ exports.isValidJsonSchema = isValidJsonSchema;
170
+ exports.normalizeJsonSchema = normalizeJsonSchema;
171
+ exports.validateStructuredOutput = validateStructuredOutput;
172
+ exports.zodToJsonSchema = zodToJsonSchema;
173
+ //# sourceMappingURL=validate.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"validate.cjs","sources":["../../../src/schemas/validate.ts"],"sourcesContent":["// src/schemas/validate.ts\nimport { z } from 'zod';\nimport type * as t from '@/types';\n\n/**\n * Validation result from structured output\n */\nexport interface ValidationResult<T = Record<string, unknown>> {\n success: boolean;\n data?: T;\n error?: string;\n raw?: unknown;\n}\n\n/**\n * Validates structured output against a JSON Schema.\n *\n * @param output - The output to validate\n * @param schema - JSON Schema to validate against\n * @returns Validation result with success flag and data or error\n */\nexport function validateStructuredOutput(\n output: unknown,\n schema: Record<string, unknown>\n): ValidationResult {\n try {\n // Parse output if it's a string\n const data = typeof output === 'string' ? JSON.parse(output) : output;\n\n // Basic JSON Schema validation\n const errors = validateAgainstJsonSchema(data, schema);\n\n if (errors.length > 0) {\n return {\n success: false,\n error: `Validation failed: ${errors.join('; ')}`,\n raw: data,\n };\n }\n\n return {\n success: true,\n data: data as Record<string, unknown>,\n };\n } catch (e) {\n return {\n success: false,\n error: `Failed to parse output: ${(e as Error).message}`,\n raw: output,\n };\n }\n}\n\n/**\n * Validates data against a JSON Schema (simplified implementation).\n * For complex schemas, consider using ajv or similar library.\n */\nfunction validateAgainstJsonSchema(\n data: unknown,\n schema: Record<string, unknown>,\n path = ''\n): string[] {\n const errors: string[] = [];\n\n if (schema.type === 'object' && typeof data !== 'object') {\n errors.push(`${path || 'root'}: expected object, got ${typeof data}`);\n return errors;\n }\n\n if (schema.type === 'array' && !Array.isArray(data)) {\n errors.push(`${path || 'root'}: expected array, got ${typeof data}`);\n return errors;\n }\n\n if (schema.type === 'string' && typeof data !== 'string') {\n errors.push(`${path || 'root'}: expected string, got ${typeof data}`);\n return errors;\n }\n\n if (schema.type === 'number' && typeof data !== 'number') {\n errors.push(`${path || 'root'}: expected number, got ${typeof data}`);\n return errors;\n }\n\n if (schema.type === 'boolean' && typeof data !== 'boolean') {\n errors.push(`${path || 'root'}: expected boolean, got ${typeof data}`);\n return errors;\n }\n\n // Validate required properties\n if (\n schema.type === 'object' &&\n Array.isArray(schema.required) &&\n typeof data === 'object' &&\n data !== null\n ) {\n for (const requiredProp of schema.required as string[]) {\n if (!(requiredProp in data)) {\n errors.push(\n `${path || 'root'}: missing required property '${requiredProp}'`\n );\n }\n }\n }\n\n // Validate nested properties\n if (\n schema.type === 'object' &&\n schema.properties &&\n typeof data === 'object' &&\n data !== null\n ) {\n const properties = schema.properties as Record<\n string,\n Record<string, unknown>\n >;\n for (const [key, propSchema] of Object.entries(properties)) {\n if (key in data) {\n const nestedErrors = validateAgainstJsonSchema(\n (data as Record<string, unknown>)[key],\n propSchema,\n path ? `${path}.${key}` : key\n );\n errors.push(...nestedErrors);\n }\n }\n }\n\n // Validate array items\n if (schema.type === 'array' && schema.items && Array.isArray(data)) {\n const itemSchema = schema.items as Record<string, unknown>;\n for (let i = 0; i < data.length; i++) {\n const nestedErrors = validateAgainstJsonSchema(\n data[i],\n itemSchema,\n `${path || 'root'}[${i}]`\n );\n errors.push(...nestedErrors);\n }\n }\n\n // Validate enum values\n if (schema.enum && Array.isArray(schema.enum)) {\n if (!schema.enum.includes(data)) {\n errors.push(\n `${path || 'root'}: value '${data}' not in enum [${(schema.enum as unknown[]).join(', ')}]`\n );\n }\n }\n\n return errors;\n}\n\n/**\n * Converts a Zod schema to JSON Schema format.\n * This is a simplified converter for common types.\n */\nexport function zodToJsonSchema(zodSchema: z.ZodType): Record<string, unknown> {\n // Use the zod-to-json-schema library if available\n // For now, provide a basic implementation\n try {\n // eslint-disable-next-line @typescript-eslint/no-require-imports\n const { zodToJsonSchema: convert } = require('zod-to-json-schema');\n return convert(zodSchema) as Record<string, unknown>;\n } catch {\n // Fallback: return a generic object schema\n return {\n type: 'object',\n additionalProperties: true,\n };\n }\n}\n\n/**\n * Creates a structured output error message for retry.\n */\nexport function createValidationErrorMessage(\n result: ValidationResult,\n customMessage?: string\n): string {\n if (customMessage) {\n return customMessage;\n }\n\n return `The response did not match the expected schema. Error: ${result.error}. Please try again and ensure your response exactly matches the required format.`;\n}\n\n/**\n * Checks if a value is a valid JSON Schema object.\n */\nexport function isValidJsonSchema(\n value: unknown\n): value is Record<string, unknown> {\n if (typeof value !== 'object' || value === null) {\n return false;\n }\n\n const schema = value as Record<string, unknown>;\n\n // Basic check: must have a type or properties\n return (\n typeof schema.type === 'string' ||\n typeof schema.properties === 'object' ||\n typeof schema.$schema === 'string'\n );\n}\n\n/**\n * Normalizes a JSON Schema by adding defaults and cleaning up.\n */\nexport function normalizeJsonSchema(\n schema: Record<string, unknown>,\n config?: t.StructuredOutputConfig\n): Record<string, unknown> {\n const normalized = { ...schema };\n\n // Ensure type is set\n if (!normalized.type && normalized.properties) {\n normalized.type = 'object';\n }\n\n // Add title from config name\n if (config?.name && !normalized.title) {\n normalized.title = config.name;\n }\n\n // Add description from config\n if (config?.description && !normalized.description) {\n normalized.description = config.description;\n }\n\n // Enable additionalProperties: false for strict mode\n if (config?.strict !== false && normalized.type === 'object') {\n normalized.additionalProperties = normalized.additionalProperties ?? false;\n }\n\n return normalized;\n}\n"],"names":[],"mappings":";;AAcA;;;;;;AAMG;AACa,SAAA,wBAAwB,CACtC,MAAe,EACf,MAA+B,EAAA;AAE/B,IAAA,IAAI;;AAEF,QAAA,MAAM,IAAI,GAAG,OAAO,MAAM,KAAK,QAAQ,GAAG,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,GAAG,MAAM;;QAGrE,MAAM,MAAM,GAAG,yBAAyB,CAAC,IAAI,EAAE,MAAM,CAAC;AAEtD,QAAA,IAAI,MAAM,CAAC,MAAM,GAAG,CAAC,EAAE;YACrB,OAAO;AACL,gBAAA,OAAO,EAAE,KAAK;gBACd,KAAK,EAAE,sBAAsB,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,CAAE,CAAA;AAChD,gBAAA,GAAG,EAAE,IAAI;aACV;;QAGH,OAAO;AAC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AE;AACjB,QAAA,OAAO,aAAa;;AAGtB,IAAA,OAAO,CAA0D,uDAAA,EAAA,MAAM,CAAC,KAAK,kFAAkF;AACjK;AAEA;;AAEG;AACG,SAAU,iBAAiB,CAC/B,KAAc,EAAA;IAEd,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,IAAI,EAAE;AAC/C,QAAA,OAAO,KAAK;;IAGd,MAAM,MAAM,GAAG,KAAgC;;AAG/C,IAAA,QACE,OAAO,MAAM,CAAC,IAAI,KAAK,QAAQ;AAC/B,QAAA,OAAO,MAAM,CAAC,UAAU,KAAK,QAAQ;AACrC,QAAA,OAAO,MAAM,CAAC,OAAO,KAAK,QAAQ;AAEtC;AAEA;;AAEG;AACa,SAAA,mBAAmB,CACjC,MAA+B,EAC/B,MAAiC,EAAA;AAEjC,IAAA,MAAM,UAAU,GAAG,EAAE,GAAG,MAAM,EAAE;;IAGhC,IAAI,CAAC,UAAU,CAAC,IAAI,IAAI,UAAU,CAAC,UAAU,EAAE;AAC7C,QAAA,UAAU,CAAC,IAAI,GAAG,QAAQ;;;IAI5B,IAAI,MAAM,EAAE,IAAI,IAAI,CAAC,UAAU,CAAC,KAAK,EAAE;AACrC,QAAA,UAAU,CAAC,KAAK,GAAG,MAAM,CAAC,IAAI;;;IAIhC,IAAI,MAAM,EAAE,WAAW,IAAI,CAAC,UAAU,CAAC,WAAW,EAAE;AAClD,QAAA,UAAU,CAAC,WAAW,GAAG,MAAM,CAAC,WAAW;;;AAI7C,IAAA,IAAI,MAAM,EAAE,MAAM,KAAK,KAAK,IAAI,UAAU,CAAC,IAAI,KAAK,QAAQ,EAAE;QAC5D,UAAU,CAAC,oBAAoB,GAAG,UAAU,CAAC,oBAAoB,IAAI,KAAK;;AAG5E,IAAA,OAAO,UAAU;AACnB;;;;;;;;"}
@@ -13,7 +13,7 @@ class AgentContext {
13
13
  * Create an AgentContext from configuration with token accounting initialization
14
14
  */
15
15
  static fromConfig(agentConfig, tokenCounter, indexTokenCountMap) {
16
- const { agentId, name, provider, clientOptions, tools, toolMap, toolEnd, toolRegistry, instructions, additional_instructions, streamBuffer, maxContextTokens, reasoningKey, useLegacyContent, dynamicContext, } = agentConfig;
16
+ const { agentId, name, provider, clientOptions, tools, toolMap, toolEnd, toolRegistry, instructions, additional_instructions, streamBuffer, maxContextTokens, reasoningKey, useLegacyContent, dynamicContext, structuredOutput, } = agentConfig;
17
17
  const agentContext = new AgentContext({
18
18
  agentId,
19
19
  name: name ?? agentId,
@@ -32,6 +32,7 @@ class AgentContext {
32
32
  tokenCounter,
33
33
  useLegacyContent,
34
34
  dynamicContext,
35
+ structuredOutput,
35
36
  });
36
37
  if (tokenCounter) {
37
38
  // Initialize system runnable BEFORE async tool token calculation
@@ -138,7 +139,13 @@ class AgentContext {
138
139
  * Contains source and parallel execution info for system message context.
139
140
  */
140
141
  handoffContext;
141
- constructor({ agentId, name, provider, clientOptions, maxContextTokens, streamBuffer, tokenCounter, tools, toolMap, toolRegistry, instructions, additionalInstructions, dynamicContext, reasoningKey, toolEnd, instructionTokens, useLegacyContent, }) {
142
+ /**
143
+ * Structured output configuration.
144
+ * When set, the agent will return a validated JSON response
145
+ * instead of streaming text.
146
+ */
147
+ structuredOutput;
148
+ constructor({ agentId, name, provider, clientOptions, maxContextTokens, streamBuffer, tokenCounter, tools, toolMap, toolRegistry, instructions, additionalInstructions, dynamicContext, reasoningKey, toolEnd, instructionTokens, useLegacyContent, structuredOutput, }) {
142
149
  this.agentId = agentId;
143
150
  this.name = name;
144
151
  this.provider = provider;
@@ -152,6 +159,7 @@ class AgentContext {
152
159
  this.instructions = instructions;
153
160
  this.additionalInstructions = additionalInstructions;
154
161
  this.dynamicContext = dynamicContext;
162
+ this.structuredOutput = structuredOutput;
155
163
  if (reasoningKey) {
156
164
  this.reasoningKey = reasoningKey;
157
165
  }
@@ -163,6 +171,41 @@ class AgentContext {
163
171
  }
164
172
  this.useLegacyContent = useLegacyContent ?? false;
165
173
  }
174
+ /**
175
+ * Checks if structured output mode is enabled for this agent.
176
+ * When enabled, the agent will use model.invoke() instead of streaming
177
+ * and return a validated JSON response.
178
+ */
179
+ get isStructuredOutputMode() {
180
+ return (this.structuredOutput != null && this.structuredOutput.schema != null);
181
+ }
182
+ /**
183
+ * Gets the structured output schema with normalized defaults.
184
+ * Returns undefined if structured output is not configured.
185
+ */
186
+ getStructuredOutputSchema() {
187
+ if (!this.structuredOutput?.schema) {
188
+ return undefined;
189
+ }
190
+ const schema = { ...this.structuredOutput.schema };
191
+ // Ensure type is set
192
+ if (!schema.type && schema.properties) {
193
+ schema.type = 'object';
194
+ }
195
+ // Add title from config name
196
+ if (this.structuredOutput.name && !schema.title) {
197
+ schema.title = this.structuredOutput.name;
198
+ }
199
+ // Add description from config
200
+ if (this.structuredOutput.description && !schema.description) {
201
+ schema.description = this.structuredOutput.description;
202
+ }
203
+ // Enable strict mode by default
204
+ if (this.structuredOutput.strict !== false && schema.type === 'object') {
205
+ schema.additionalProperties = schema.additionalProperties ?? false;
206
+ }
207
+ return schema;
208
+ }
166
209
  /**
167
210
  * Builds instructions text for tools that are ONLY callable via programmatic code execution.
168
211
  * These tools cannot be called directly by the LLM but are available through the