illuma-agents 1.0.60 → 1.0.61

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.
@@ -766,8 +766,15 @@ class StandardGraph extends Graph {
766
766
  analytics: contextAnalytics$1,
767
767
  }, config);
768
768
  // Check if structured output mode is enabled
769
- if (agentContext.isStructuredOutputMode &&
770
- agentContext.structuredOutput) {
769
+ // IMPORTANT: If tools are available, we need to let the model use them first.
770
+ // Only use structured output when:
771
+ // 1. No tools are configured, OR
772
+ // 2. The model has already used tools and is ready to give final response
773
+ const hasTools = agentContext.tools && agentContext.tools.length > 0;
774
+ const shouldUseStructuredOutputNow = agentContext.isStructuredOutputMode &&
775
+ agentContext.structuredOutput &&
776
+ !hasTools; // Only use structured output immediately if no tools
777
+ if (shouldUseStructuredOutputNow) {
771
778
  const schema = agentContext.getStructuredOutputSchema();
772
779
  if (!schema) {
773
780
  throw new Error('Structured output schema is not configured');
@@ -1017,6 +1024,87 @@ If I seem to be missing something we discussed earlier, just give me a quick rem
1017
1024
  if (!result) {
1018
1025
  throw new Error('No result after model invocation');
1019
1026
  }
1027
+ // Check if we need to apply structured output for the final response
1028
+ // This handles the case where tools were used, and now the model is giving its final answer
1029
+ const resultMessage = result.messages?.[0];
1030
+ const hasToolCalls = (resultMessage?.tool_calls?.length ?? 0) > 0;
1031
+ if (agentContext.isStructuredOutputMode &&
1032
+ agentContext.structuredOutput &&
1033
+ !hasToolCalls && // Model is giving final response (no tool calls)
1034
+ hasTools // We skipped structured output earlier because tools were available
1035
+ ) {
1036
+ const schema = agentContext.getStructuredOutputSchema();
1037
+ if (schema) {
1038
+ try {
1039
+ console.log('[Graph] Applying structured output for final response after tool execution');
1040
+ // Get a fresh model for structured output
1041
+ const structuredClientOptions = { ...agentContext.clientOptions };
1042
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
1043
+ structuredClientOptions.streaming = false;
1044
+ // Remove thinking configuration
1045
+ if (agentContext.provider === _enum.Providers.BEDROCK) {
1046
+ const bedrockOpts = structuredClientOptions;
1047
+ if (bedrockOpts.additionalModelRequestFields) {
1048
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
1049
+ const additionalFields = Object.assign({}, bedrockOpts.additionalModelRequestFields);
1050
+ delete additionalFields.thinking;
1051
+ delete additionalFields.budgetTokens;
1052
+ bedrockOpts.additionalModelRequestFields = additionalFields;
1053
+ }
1054
+ }
1055
+ if (agentContext.provider === _enum.Providers.ANTHROPIC) {
1056
+ const anthropicOpts = structuredClientOptions;
1057
+ if (anthropicOpts.thinking) {
1058
+ delete anthropicOpts.thinking;
1059
+ }
1060
+ }
1061
+ const structuredModel = this.getNewModel({
1062
+ provider: agentContext.provider,
1063
+ clientOptions: structuredClientOptions,
1064
+ });
1065
+ // Include the current result in the messages so the model knows what it just said
1066
+ const messagesWithResult = [...finalMessages];
1067
+ if (resultMessage) {
1068
+ messagesWithResult.push(resultMessage);
1069
+ }
1070
+ const { structuredResponse, rawMessage } = await this.attemptStructuredInvoke({
1071
+ currentModel: structuredModel,
1072
+ finalMessages: messagesWithResult,
1073
+ schema,
1074
+ structuredOutputConfig: agentContext.structuredOutput,
1075
+ provider: agentContext.provider,
1076
+ }, config);
1077
+ // Emit structured output event
1078
+ await events.safeDispatchCustomEvent(_enum.GraphEvents.ON_STRUCTURED_OUTPUT, {
1079
+ structuredResponse,
1080
+ schema,
1081
+ raw: rawMessage,
1082
+ }, config);
1083
+ agentContext.currentUsage = rawMessage
1084
+ ? this.getUsageMetadata(rawMessage)
1085
+ : undefined;
1086
+ this.cleanupSignalListener();
1087
+ // Return clean message without tool_calls
1088
+ let cleanMessage;
1089
+ if (rawMessage) {
1090
+ cleanMessage = new messages.AIMessageChunk({
1091
+ content: JSON.stringify(structuredResponse, null, 2),
1092
+ id: rawMessage.id,
1093
+ response_metadata: rawMessage.response_metadata,
1094
+ usage_metadata: rawMessage.usage_metadata,
1095
+ });
1096
+ }
1097
+ return {
1098
+ messages: cleanMessage ? [cleanMessage] : [],
1099
+ structuredResponse,
1100
+ };
1101
+ }
1102
+ catch (structuredError) {
1103
+ console.error('[Graph] Structured output failed after tool execution:', structuredError);
1104
+ // Fall through to return normal result
1105
+ }
1106
+ }
1107
+ }
1020
1108
  agentContext.currentUsage = this.getUsageMetadata(result.messages?.[0]);
1021
1109
  this.cleanupSignalListener();
1022
1110
  return result;