@codemation/core-nodes 0.10.2 → 0.13.0

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 (39) hide show
  1. package/CHANGELOG.md +122 -0
  2. package/dist/index.cjs +427 -102
  3. package/dist/index.cjs.map +1 -1
  4. package/dist/index.d.cts +205 -67
  5. package/dist/index.d.ts +206 -68
  6. package/dist/index.js +427 -99
  7. package/dist/index.js.map +1 -1
  8. package/dist/metadata.json +1 -1
  9. package/package.json +3 -2
  10. package/src/chatModels/CodemationChatModelConfig.ts +9 -21
  11. package/src/chatModels/CodemationChatModelFactory.ts +12 -9
  12. package/src/chatModels/OpenAIChatModelFactory.ts +3 -2
  13. package/src/index.ts +1 -1
  14. package/src/nodes/AIAgentConfig.ts +36 -0
  15. package/src/nodes/AIAgentNode.ts +81 -15
  16. package/src/nodes/AgentBinaryContentFactory.ts +74 -0
  17. package/src/nodes/AgentMessageFactory.ts +22 -6
  18. package/src/nodes/AgentToolResultContentFactory.ts +155 -0
  19. package/src/nodes/CallbackNodeFactory.ts +9 -6
  20. package/src/nodes/CronTriggerFactory.ts +6 -2
  21. package/src/nodes/DeferredMetaToolStrategy.ts +8 -2
  22. package/src/nodes/ManualTriggerFactory.ts +15 -11
  23. package/src/nodes/WebhookTriggerFactory.ts +9 -2
  24. package/src/nodes/aggregate.ts +9 -2
  25. package/src/nodes/assertion.ts +3 -0
  26. package/src/nodes/filter.ts +9 -2
  27. package/src/nodes/httpRequest.ts +6 -1
  28. package/src/nodes/if.ts +9 -2
  29. package/src/nodes/isTestRun.ts +6 -2
  30. package/src/nodes/mapData.ts +4 -2
  31. package/src/nodes/merge.ts +9 -2
  32. package/src/nodes/noOp.ts +9 -2
  33. package/src/nodes/nodeOptions.types.ts +12 -0
  34. package/src/nodes/split.ts +9 -2
  35. package/src/nodes/subWorkflow.ts +9 -2
  36. package/src/nodes/switch.ts +7 -1
  37. package/src/nodes/wait.ts +9 -2
  38. package/src/workflowAuthoring/WorkflowChatModelFactory.types.ts +8 -2
  39. package/src/chatModels/ManagedModelFetcher.ts +0 -23
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "schemaVersion": 1,
3
3
  "packageName": "@codemation/core-nodes",
4
- "packageVersion": "0.10.2",
4
+ "packageVersion": "0.13.0",
5
5
  "description": "",
6
6
  "kind": "nodes",
7
7
  "nodes": [
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@codemation/core-nodes",
3
- "version": "0.10.2",
3
+ "version": "0.13.0",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
@@ -28,11 +28,12 @@
28
28
  }
29
29
  },
30
30
  "dependencies": {
31
+ "@ai-sdk/anthropic": "^3.0.85",
31
32
  "@ai-sdk/openai": "^3.0.53",
32
33
  "@ai-sdk/provider": "^3.0.8",
33
34
  "ai": "^6.0.168",
34
35
  "croner": "^10.0.1",
35
- "@codemation/core": "0.13.2"
36
+ "@codemation/core": "0.14.0"
36
37
  },
37
38
  "devDependencies": {
38
39
  "@types/node": "^25.3.5",
@@ -4,25 +4,14 @@ import type { CanvasIconName } from "../canvasIconName";
4
4
  import { CodemationChatModelFactory } from "./CodemationChatModelFactory";
5
5
 
6
6
  /**
7
- * A platform-managed model entry as returned by GET /api/llm/managed-models.
7
+ * Complexity token sent to the managed LLM broker.
8
+ * The broker maps this to a concrete provider model and thinking effort.
9
+ * low = cheapest/fastest (short classification, simple extraction)
10
+ * medium = default for most extraction/agent work
11
+ * high = complex multi-step reasoning
12
+ * xhigh = hardest problems, most capable model
8
13
  */
9
- export interface ManagedModelDto {
10
- id: string;
11
- modelId: string;
12
- displayName: string;
13
- providerKey: string;
14
- inputCostPerMTok: number;
15
- outputCostPerMTok: number;
16
- contextWindow: number;
17
- tier: string;
18
- }
19
-
20
- /**
21
- * Bifrost-namespaced model ID. Kept as `string` so runtime-fetched model IDs
22
- * (from the CP allowlist) work without compile-time enumeration.
23
- * Story C replaced the prior hardcoded union with this open type.
24
- */
25
- export type CodemationManagedModel = string;
14
+ export type ManagedComplexity = "low" | "medium" | "high" | "xhigh";
26
15
 
27
16
  export class CodemationChatModelConfig implements ChatModelConfig {
28
17
  readonly type = CodemationChatModelFactory;
@@ -32,14 +21,13 @@ export class CodemationChatModelConfig implements ChatModelConfig {
32
21
 
33
22
  constructor(
34
23
  public readonly name: string,
35
- public readonly model: CodemationManagedModel,
24
+ public readonly complexity: ManagedComplexity,
36
25
  presentationIn?: AgentCanvasPresentation<CanvasIconName>,
37
26
  public readonly options?: Readonly<{
38
- temperature?: number;
39
27
  maxTokens?: number;
40
28
  }>,
41
29
  ) {
42
- this.modelName = model;
30
+ this.modelName = complexity;
43
31
  this.presentation = presentationIn ?? { icon: "lucide:bot", label: name };
44
32
  }
45
33
 
@@ -1,14 +1,12 @@
1
1
  import type { ChatLanguageModel, ChatModelFactory, NodeExecutionContext } from "@codemation/core";
2
2
  import { chatModel } from "@codemation/core";
3
3
 
4
- import { createOpenAI } from "@ai-sdk/openai";
5
-
6
4
  import type { CodemationChatModelConfig } from "./CodemationChatModelConfig";
7
5
  import { managedHmacFetchFactory } from "./ManagedHmacSignerFactory.types";
8
6
 
9
7
  @chatModel({ packageName: "@codemation/core-nodes" })
10
8
  export class CodemationChatModelFactory implements ChatModelFactory<CodemationChatModelConfig> {
11
- create(
9
+ async create(
12
10
  args: Readonly<{ config: CodemationChatModelConfig; ctx: NodeExecutionContext<any> }>,
13
11
  ): Promise<ChatLanguageModel> {
14
12
  // D5: read at session-create time so unpairing or misconfiguration surfaces at workflow run, not boot.
@@ -27,18 +25,23 @@ export class CodemationChatModelFactory implements ChatModelFactory<CodemationCh
27
25
  }
28
26
 
29
27
  const hmacFetch = managedHmacFetchFactory(workspaceId, pairingSecret);
28
+ // Lazy import: pulls @ai-sdk/anthropic + the `ai` SDK (~28MB RSS) only when a
29
+ // chat model is actually built. Non-AI workflows never load it.
30
+ // Using the Anthropic-native route so the broker's injected `thinking` /
31
+ // `output_config.effort` fields survive (they are stripped by the OpenAI-compat route).
32
+ const { createAnthropic } = await import("@ai-sdk/anthropic");
30
33
  // apiKey is required by the AI SDK but unused — authentication is handled by the HMAC-signed fetch wrapper.
31
- const provider = createOpenAI({ baseURL: `${gatewayUrl}/v1`, apiKey: "codemation-managed", fetch: hmacFetch });
32
- const languageModel = provider.chat(args.config.model);
34
+ // baseURL: the SDK appends /messages hits the broker's /v1/messages Anthropic-native route.
35
+ const provider = createAnthropic({ baseURL: `${gatewayUrl}/v1`, apiKey: "codemation-managed", fetch: hmacFetch });
36
+ const languageModel = provider(args.config.complexity);
33
37
 
34
- return Promise.resolve({
38
+ return {
35
39
  languageModel,
36
- modelName: args.config.model,
40
+ modelName: args.config.complexity,
37
41
  provider: "codemation-managed",
38
42
  defaultCallOptions: {
39
43
  maxOutputTokens: args.config.options?.maxTokens,
40
- temperature: args.config.options?.temperature,
41
44
  },
42
- });
45
+ };
43
46
  }
44
47
  }
@@ -1,8 +1,6 @@
1
1
  import type { ChatLanguageModel, ChatModelFactory, NodeExecutionContext } from "@codemation/core";
2
2
  import { chatModel } from "@codemation/core";
3
3
 
4
- import { createOpenAI } from "@ai-sdk/openai";
5
-
6
4
  import type { OpenAiCredentialSession } from "./OpenAiCredentialSession";
7
5
  import type { OpenAIChatModelConfig } from "./openAiChatModelConfig";
8
6
 
@@ -12,6 +10,9 @@ export class OpenAIChatModelFactory implements ChatModelFactory<OpenAIChatModelC
12
10
  args: Readonly<{ config: OpenAIChatModelConfig; ctx: NodeExecutionContext<any> }>,
13
11
  ): Promise<ChatLanguageModel> {
14
12
  const session = await args.ctx.getCredential<OpenAiCredentialSession>(args.config.credentialSlotKey);
13
+ // Lazy import: pulls @ai-sdk/openai + the `ai` SDK (~28MB RSS) only when a
14
+ // chat model is actually built. Non-AI workflows never load it.
15
+ const { createOpenAI } = await import("@ai-sdk/openai");
15
16
  const provider = createOpenAI({
16
17
  apiKey: session.apiKey,
17
18
  baseURL: session.baseUrl,
package/src/index.ts CHANGED
@@ -10,7 +10,6 @@ export * from "./chatModels/openAiChatModelConfig";
10
10
  export * from "./chatModels/OpenAiChatModelPresetsFactory";
11
11
  export * from "./chatModels/CodemationChatModelFactory";
12
12
  export * from "./chatModels/CodemationChatModelConfig";
13
- export * from "./chatModels/ManagedModelFetcher";
14
13
  export * from "./nodes/aiAgent";
15
14
  export * from "./nodes/assertion";
16
15
  export * from "./nodes/CallbackNodeFactory";
@@ -26,6 +25,7 @@ export * from "./nodes/CronTriggerNode";
26
25
  export * from "./nodes/ManualTriggerFactory";
27
26
  export * from "./nodes/mapData";
28
27
  export * from "./nodes/merge";
28
+ export * from "./nodes/nodeOptions.types";
29
29
  export * from "./nodes/noOp";
30
30
  export * from "./nodes/subWorkflow";
31
31
  export * from "./nodes/testTrigger";
@@ -1,8 +1,10 @@
1
1
  import {
2
2
  RetryPolicy,
3
3
  type AgentGuardrailConfig,
4
+ type AgentMessageBuildArgs,
4
5
  type AgentMessageConfig,
5
6
  type AgentNodeConfig,
7
+ type BinaryAttachment,
6
8
  type ChatModelConfig,
7
9
  type NodeInspectorSummaryRow,
8
10
  type RetryPolicySpec,
@@ -20,6 +22,7 @@ export interface AIAgentOptions<TInputJson = unknown, _TOutputJson = unknown> {
20
22
  readonly chatModel: ChatModelConfig;
21
23
  readonly tools?: ReadonlyArray<ToolConfig>;
22
24
  readonly id?: string;
25
+ readonly description?: string;
23
26
  readonly retryPolicy?: RetryPolicySpec;
24
27
  readonly guardrails?: AgentGuardrailConfig;
25
28
  /** Engine applies with {@link RunnableNodeConfig.inputSchema} before {@link AIAgentNode.execute}. */
@@ -49,6 +52,29 @@ export interface AIAgentOptions<TInputJson = unknown, _TOutputJson = unknown> {
49
52
  * Defaults to `["gmail", "ocr", "webhook"]` when unset.
50
53
  */
51
54
  readonly untrustedSources?: ReadonlyArray<string>;
55
+ /**
56
+ * Whether file binaries are automatically passed to the chat model as native inline
57
+ * multimodal blocks. Defaults to `true`. Set to `false` to skip the binary-passdown step
58
+ * entirely (the node then behaves as if no binaries were present).
59
+ */
60
+ readonly passBinariesToModel?: boolean;
61
+ /**
62
+ * Whether binaries returned by a tool (e.g. an MCP tool returning a PDF or image) are passed to
63
+ * the chat model as native multimodal tool-result blocks. Defaults to `true`. Set to `false` to
64
+ * keep tool results as inert JSON text (the model then never "sees" the document).
65
+ */
66
+ readonly passToolBinariesToModel?: boolean;
67
+ /**
68
+ * Explicit binaries to pass to the chat model, instead of the ones on the current item.
69
+ * Either a static array or a function resolved per item (so an author can forward binaries
70
+ * produced by an earlier node further back in the workflow). When provided, these replace
71
+ * `item.binary` as the passdown source. Ignored when {@link passBinariesToModel} is `false`.
72
+ * Every binary is passed (images as image blocks, all other types as file blocks); the
73
+ * provider surfaces an error at runtime if it doesn't support a given file type.
74
+ */
75
+ readonly binaries?:
76
+ | ReadonlyArray<BinaryAttachment>
77
+ | ((args: AgentMessageBuildArgs<TInputJson>) => ReadonlyArray<BinaryAttachment>);
52
78
  }
53
79
 
54
80
  /**
@@ -67,6 +93,7 @@ export class AIAgent<TInputJson = unknown, TOutputJson = unknown>
67
93
  readonly chatModel: ChatModelConfig;
68
94
  readonly tools: ReadonlyArray<ToolConfig>;
69
95
  readonly id?: string;
96
+ readonly description?: string;
70
97
  readonly retryPolicy: RetryPolicySpec;
71
98
  readonly guardrails?: AgentGuardrailConfig;
72
99
  readonly inputSchema?: ZodType<TInputJson>;
@@ -74,6 +101,11 @@ export class AIAgent<TInputJson = unknown, TOutputJson = unknown>
74
101
  readonly mcpServers?: ReadonlyArray<string>;
75
102
  readonly pinnedMcpTools?: readonly string[];
76
103
  readonly untrustedSources?: ReadonlyArray<string>;
104
+ readonly passBinariesToModel?: boolean;
105
+ readonly passToolBinariesToModel?: boolean;
106
+ readonly binaries?:
107
+ | ReadonlyArray<BinaryAttachment>
108
+ | ((args: AgentMessageBuildArgs<TInputJson>) => ReadonlyArray<BinaryAttachment>);
77
109
 
78
110
  constructor(options: AIAgentOptions<TInputJson, TOutputJson>) {
79
111
  this.name = options.name;
@@ -81,6 +113,7 @@ export class AIAgent<TInputJson = unknown, TOutputJson = unknown>
81
113
  this.chatModel = options.chatModel;
82
114
  this.tools = options.tools ?? [];
83
115
  this.id = options.id;
116
+ this.description = options.description;
84
117
  this.retryPolicy = options.retryPolicy ?? RetryPolicy.defaultForAiAgent;
85
118
  this.guardrails = options.guardrails;
86
119
  this.inputSchema = options.inputSchema;
@@ -88,6 +121,9 @@ export class AIAgent<TInputJson = unknown, TOutputJson = unknown>
88
121
  this.mcpServers = options.mcpServers;
89
122
  this.pinnedMcpTools = options.pinnedMcpTools;
90
123
  this.untrustedSources = options.untrustedSources;
124
+ this.passBinariesToModel = options.passBinariesToModel;
125
+ this.passToolBinariesToModel = options.passToolBinariesToModel;
126
+ this.binaries = options.binaries;
91
127
  }
92
128
 
93
129
  inspectorSummary(): ReadonlyArray<NodeInspectorSummaryRow> {
@@ -2,6 +2,7 @@ import type {
2
2
  AgentGuardrailConfig,
3
3
  AgentMessageDto,
4
4
  AgentToolCall,
5
+ BinaryAttachment,
5
6
  ChatLanguageModel,
6
7
  ChatLanguageModelCallOptions,
7
8
  ChatModelConfig,
@@ -36,7 +37,6 @@ import {
36
37
  } from "@codemation/core";
37
38
 
38
39
  import type { AssistantModelMessage, GenerateTextResult, LanguageModel, ModelMessage, ToolSet } from "ai";
39
- import { Output, generateText, jsonSchema } from "ai";
40
40
 
41
41
  /**
42
42
  * OUTPUT generic must extend AI SDK's `Output<OUTPUT, PARTIAL, ELEMENT>` which is parametric on
@@ -52,6 +52,7 @@ import { AIAgentExecutionHelpersFactory } from "./AIAgentExecutionHelpersFactory
52
52
  import { AgentToolExecutionCoordinator } from "./AgentToolExecutionCoordinator";
53
53
  import { ConnectionCredentialExecutionContextFactory } from "./ConnectionCredentialExecutionContextFactory";
54
54
  import { AgentMessageFactory } from "./AgentMessageFactory";
55
+ import { AgentBinaryContentFactory, type ResolvedAgentBinary } from "./AgentBinaryContentFactory";
55
56
  import { AgentOutputFactory } from "./AgentOutputFactory";
56
57
  import { AgentStructuredOutputRunner } from "./AgentStructuredOutputRunner";
57
58
  import { AgentToolCallPortMap } from "./AgentToolCallPortMapFactory";
@@ -110,6 +111,13 @@ export class AIAgentNode implements RunnableNode<AIAgent<any, any>> {
110
111
  NodeExecutionContext<AIAgent<any, any>>,
111
112
  Promise<PreparedAgentExecution>
112
113
  >();
114
+ /**
115
+ * The `ai` SDK, loaded lazily in {@link execute} so the SDK (~28MB RSS) stays
116
+ * off the boot path — non-AI workflows never load it. Every path runs through
117
+ * `execute` → `ensureAiSdk` before any sync helper touches `this.aiSdk`.
118
+ */
119
+ private aiSdk!: typeof import("ai");
120
+ private aiSdkPromise: Promise<typeof import("ai")> | null = null;
113
121
 
114
122
  constructor(
115
123
  @inject(CoreTokens.NodeResolver)
@@ -135,6 +143,7 @@ export class AIAgentNode implements RunnableNode<AIAgent<any, any>> {
135
143
 
136
144
  async execute(args: RunnableNodeExecuteArgs<AIAgent<any, any>>): Promise<unknown> {
137
145
  const { ctx } = args;
146
+ await this.ensureAiSdk();
138
147
 
139
148
  // HITL resume branch (story 10): the engine re-activates us after a human decision.
140
149
  if (ctx.resumeContext) {
@@ -147,6 +156,11 @@ export class AIAgentNode implements RunnableNode<AIAgent<any, any>> {
147
156
  return resultItem.json;
148
157
  }
149
158
 
159
+ /** Load the `ai` SDK once per node instance (cached promise guards concurrent items). */
160
+ private async ensureAiSdk(): Promise<void> {
161
+ this.aiSdk = await (this.aiSdkPromise ??= import("ai"));
162
+ }
163
+
150
164
  /**
151
165
  * Resume path: re-enters the agent loop after a HITL suspension.
152
166
  * Reconstructs the conversation from the checkpoint, injects the human decision
@@ -196,7 +210,7 @@ export class AIAgentNode implements RunnableNode<AIAgent<any, any>> {
196
210
  };
197
211
  const conversation: ModelMessage[] = [
198
212
  ...checkpoint.conversation,
199
- AgentMessageFactory.createToolResultsMessage([toolResultEntry]),
213
+ AgentMessageFactory.createToolResultsMessage([toolResultEntry], ctx.config.passToolBinariesToModel !== false),
200
214
  ];
201
215
 
202
216
  const loopResult = await this.runTurnLoopUntilFinalAnswer({
@@ -330,7 +344,7 @@ export class AIAgentNode implements RunnableNode<AIAgent<any, any>> {
330
344
  const { ctx } = prepared;
331
345
  const itemInputsByPort = AgentItemPortMap.fromItem(item);
332
346
  const itemScopedTools = this.createItemScopedTools(prepared.resolvedTools, ctx, item, itemIndex, items);
333
- const conversation: ModelMessage[] = [...this.createPromptMessages(item, itemIndex, items, ctx)];
347
+ const conversation: ModelMessage[] = [...(await this.createPromptMessages(item, itemIndex, items, ctx))];
334
348
  if (ctx.config.outputSchema && itemScopedTools.length === 0) {
335
349
  const structuredOutput = await this.structuredOutputRunner.resolve({
336
350
  model: prepared.model,
@@ -476,6 +490,7 @@ export class AIAgentNode implements RunnableNode<AIAgent<any, any>> {
476
490
  result.text,
477
491
  result.toolCalls,
478
492
  allExecutedCalls,
493
+ ctx.config.passToolBinariesToModel !== false,
479
494
  );
480
495
  }
481
496
 
@@ -508,10 +523,11 @@ export class AIAgentNode implements RunnableNode<AIAgent<any, any>> {
508
523
  text: string,
509
524
  toolCalls: ReadonlyArray<AgentToolCall>,
510
525
  executedToolCalls: ReadonlyArray<ExecutedToolCall>,
526
+ passToolBinariesToModel: boolean,
511
527
  ): void {
512
528
  conversation.push(
513
529
  assistantMessage ?? AgentMessageFactory.createAssistantWithToolCalls(text, toolCalls),
514
- AgentMessageFactory.createToolResultsMessage(executedToolCalls),
530
+ AgentMessageFactory.createToolResultsMessage(executedToolCalls, passToolBinariesToModel),
515
531
  );
516
532
  }
517
533
 
@@ -653,7 +669,8 @@ export class AIAgentNode implements RunnableNode<AIAgent<any, any>> {
653
669
  */
654
670
  private buildToolSetFromResolved(resolvedTools: ReadonlyArray<ResolvedTool>): ToolSet {
655
671
  if (resolvedTools.length === 0) return {};
656
- const toolSet: Record<string, { description?: string; inputSchema: ReturnType<typeof jsonSchema> }> = {};
672
+ const toolSet: Record<string, { description?: string; inputSchema: ReturnType<typeof import("ai").jsonSchema> }> =
673
+ {};
657
674
  for (const entry of resolvedTools) {
658
675
  const schemaRecord = this.executionHelpers.createJsonSchemaRecord(entry.runtime.inputSchema, {
659
676
  schemaName: entry.config.name,
@@ -664,7 +681,7 @@ export class AIAgentNode implements RunnableNode<AIAgent<any, any>> {
664
681
  const description = isHitl ? `${baseDescription} ${HITL_SOLO_CONSTRAINT_SENTENCE}` : baseDescription;
665
682
  toolSet[entry.config.name] = {
666
683
  description,
667
- inputSchema: jsonSchema(schemaRecord as Parameters<typeof jsonSchema>[0]),
684
+ inputSchema: this.aiSdk.jsonSchema(schemaRecord as Parameters<typeof import("ai").jsonSchema>[0]),
668
685
  };
669
686
  }
670
687
  return toolSet as unknown as ToolSet;
@@ -686,7 +703,8 @@ export class AIAgentNode implements RunnableNode<AIAgent<any, any>> {
686
703
  */
687
704
  private buildToolSet(itemScopedTools: ReadonlyArray<ItemScopedToolBinding>): ToolSet | undefined {
688
705
  if (itemScopedTools.length === 0) return undefined;
689
- const toolSet: Record<string, { description?: string; inputSchema: ReturnType<typeof jsonSchema> }> = {};
706
+ const toolSet: Record<string, { description?: string; inputSchema: ReturnType<typeof import("ai").jsonSchema> }> =
707
+ {};
690
708
  for (const entry of itemScopedTools) {
691
709
  const schemaRecord = this.executionHelpers.createJsonSchemaRecord(entry.inputSchema, {
692
710
  schemaName: entry.config.name,
@@ -701,7 +719,7 @@ export class AIAgentNode implements RunnableNode<AIAgent<any, any>> {
701
719
  : baseDescription;
702
720
  toolSet[entry.config.name] = {
703
721
  description,
704
- inputSchema: jsonSchema(schemaRecord as Parameters<typeof jsonSchema>[0]),
722
+ inputSchema: this.aiSdk.jsonSchema(schemaRecord as Parameters<typeof import("ai").jsonSchema>[0]),
705
723
  };
706
724
  }
707
725
  return toolSet as unknown as ToolSet;
@@ -759,7 +777,7 @@ export class AIAgentNode implements RunnableNode<AIAgent<any, any>> {
759
777
  });
760
778
  try {
761
779
  const callOptions = this.resolveCallOptions(model, guardrails.modelInvocationOptions);
762
- const result = await generateText({
780
+ const result = await this.aiSdk.generateText({
763
781
  model: model.languageModel as LanguageModel,
764
782
  messages: [...messages],
765
783
  tools,
@@ -881,10 +899,10 @@ export class AIAgentNode implements RunnableNode<AIAgent<any, any>> {
881
899
  requireObjectRoot: true,
882
900
  })
883
901
  : schema;
884
- const outputSchema = Output.object({
885
- schema: jsonSchema(schemaRecord as Parameters<typeof jsonSchema>[0]) as never,
902
+ const outputSchema = this.aiSdk.Output.object({
903
+ schema: this.aiSdk.jsonSchema(schemaRecord as Parameters<typeof import("ai").jsonSchema>[0]) as never,
886
904
  });
887
- const result = await generateText({
905
+ const result = await this.aiSdk.generateText({
888
906
  model: model.languageModel as LanguageModel,
889
907
  messages: [...messages],
890
908
  experimental_output: outputSchema,
@@ -1207,12 +1225,12 @@ export class AIAgentNode implements RunnableNode<AIAgent<any, any>> {
1207
1225
  return JSON.parse(json) as JsonValue;
1208
1226
  }
1209
1227
 
1210
- private createPromptMessages(
1228
+ private async createPromptMessages(
1211
1229
  item: Item,
1212
1230
  itemIndex: number,
1213
1231
  items: Items,
1214
1232
  ctx: NodeExecutionContext<AIAgent<any, any>>,
1215
- ): ReadonlyArray<ModelMessage> {
1233
+ ): Promise<ReadonlyArray<ModelMessage>> {
1216
1234
  const messages = AgentMessageConfigNormalizer.resolveFromInputOrConfig(item.json, ctx.config, {
1217
1235
  item,
1218
1236
  itemIndex,
@@ -1220,7 +1238,55 @@ export class AIAgentNode implements RunnableNode<AIAgent<any, any>> {
1220
1238
  ctx,
1221
1239
  });
1222
1240
  const wrapped = this.wrapUntrustedSourceMessages(messages, item, ctx.config);
1223
- return AgentMessageFactory.createPromptMessages(wrapped);
1241
+ const promptMessages = AgentMessageFactory.createPromptMessages(wrapped);
1242
+ // Skip the passdown step entirely when the author opted out (default is on).
1243
+ if (ctx.config.passBinariesToModel === false) return promptMessages;
1244
+ const attachments = this.selectBinaryAttachments(item, itemIndex, items, ctx);
1245
+ const binaries = await this.resolveInlineBinaries(attachments, ctx);
1246
+ return AgentBinaryContentFactory.withBinaries(promptMessages, binaries);
1247
+ }
1248
+
1249
+ /**
1250
+ * Picks which attachments feed the passdown. When the author supplies `config.binaries`
1251
+ * (a static array or a per-item function — e.g. to forward binaries from an earlier node),
1252
+ * those replace the current item's attachments; otherwise the current item's `item.binary`
1253
+ * is used.
1254
+ */
1255
+ private selectBinaryAttachments(
1256
+ item: Item,
1257
+ itemIndex: number,
1258
+ items: Items,
1259
+ ctx: NodeExecutionContext<AIAgent<any, any>>,
1260
+ ): ReadonlyArray<BinaryAttachment> {
1261
+ const manual = ctx.config.binaries;
1262
+ if (manual !== undefined) {
1263
+ return typeof manual === "function" ? manual({ item, itemIndex, items, ctx }) : manual;
1264
+ }
1265
+ return item.binary ? Object.values(item.binary) : [];
1266
+ }
1267
+
1268
+ /**
1269
+ * Reads every attachment through `ctx.binary` (storage-backed, by reference — never base64 on
1270
+ * `item.json`) and resolves it to inline base64 so the agent can pass it to the chat model as a
1271
+ * native multimodal block. Images become image blocks; every other type (PDF, office docs, CSV,
1272
+ * JSON, …) becomes a file block — we don't filter by media type, so any binary can be fed to the
1273
+ * model. If the provider rejects an unsupported type the error surfaces at runtime, and the
1274
+ * workflow can filter the binary upstream.
1275
+ */
1276
+ private async resolveInlineBinaries(
1277
+ attachments: ReadonlyArray<BinaryAttachment>,
1278
+ ctx: NodeExecutionContext<AIAgent<any, any>>,
1279
+ ): Promise<ReadonlyArray<ResolvedAgentBinary>> {
1280
+ const resolved: ResolvedAgentBinary[] = [];
1281
+ for (const attachment of attachments) {
1282
+ const bytes = await ctx.binary.getBytes(attachment);
1283
+ resolved.push({
1284
+ mediaType: attachment.mimeType,
1285
+ base64: Buffer.from(bytes).toString("base64"),
1286
+ ...(attachment.filename ? { filename: attachment.filename } : {}),
1287
+ });
1288
+ }
1289
+ return resolved;
1224
1290
  }
1225
1291
 
1226
1292
  /**
@@ -0,0 +1,74 @@
1
+ import type { FilePart, ImagePart, ModelMessage, TextPart, UserModelMessage } from "ai";
2
+
3
+ /** A binary attachment already resolved to inline bytes, ready to become an AI SDK content part. */
4
+ export type ResolvedAgentBinary = Readonly<{
5
+ mediaType: string;
6
+ /** Base64-encoded bytes of the attachment. */
7
+ base64: string;
8
+ filename?: string;
9
+ }>;
10
+
11
+ /**
12
+ * Turns resolved file binaries into native AI SDK multimodal content parts and merges them into the
13
+ * agent prompt. Images (`image/*`) become {@link ImagePart}s; every other type (PDFs, office docs,
14
+ * CSV, JSON, …) becomes a {@link FilePart}. The provider maps these to its wire-level `image` /
15
+ * `document` blocks; an unsupported file type surfaces as a provider error at runtime.
16
+ *
17
+ * Parts are appended to the LAST user message so the binary travels alongside the author's prompt
18
+ * text (preserving any untrusted-source preamble that already wrapped that text). When no user
19
+ * message exists, a new user message carrying only the binaries is appended.
20
+ */
21
+ export class AgentBinaryContentFactory {
22
+ static toContentPart(binary: ResolvedAgentBinary): ImagePart | FilePart {
23
+ if (binary.mediaType.startsWith("image/")) {
24
+ return { type: "image", image: binary.base64, mediaType: binary.mediaType };
25
+ }
26
+ return {
27
+ type: "file",
28
+ data: binary.base64,
29
+ mediaType: binary.mediaType,
30
+ ...(binary.filename ? { filename: binary.filename } : {}),
31
+ };
32
+ }
33
+
34
+ static withBinaries(
35
+ messages: ReadonlyArray<ModelMessage>,
36
+ binaries: ReadonlyArray<ResolvedAgentBinary>,
37
+ ): ReadonlyArray<ModelMessage> {
38
+ if (binaries.length === 0) return messages;
39
+ const parts = binaries.map((binary) => AgentBinaryContentFactory.toContentPart(binary));
40
+
41
+ const lastUserIndex = AgentBinaryContentFactory.lastUserMessageIndex(messages);
42
+ if (lastUserIndex === -1) {
43
+ const appended: UserModelMessage = { role: "user", content: parts };
44
+ return [...messages, appended];
45
+ }
46
+
47
+ const next = [...messages];
48
+ next[lastUserIndex] = AgentBinaryContentFactory.appendPartsToUserMessage(
49
+ messages[lastUserIndex] as UserModelMessage,
50
+ parts,
51
+ );
52
+ return next;
53
+ }
54
+
55
+ private static lastUserMessageIndex(messages: ReadonlyArray<ModelMessage>): number {
56
+ for (let index = messages.length - 1; index >= 0; index--) {
57
+ if (messages[index]?.role === "user") return index;
58
+ }
59
+ return -1;
60
+ }
61
+
62
+ private static appendPartsToUserMessage(
63
+ message: UserModelMessage,
64
+ parts: ReadonlyArray<ImagePart | FilePart>,
65
+ ): UserModelMessage {
66
+ const existing: ReadonlyArray<TextPart | ImagePart | FilePart> =
67
+ typeof message.content === "string"
68
+ ? message.content.length > 0
69
+ ? [{ type: "text", text: message.content }]
70
+ : []
71
+ : message.content;
72
+ return { ...message, content: [...existing, ...parts] };
73
+ }
74
+ }
@@ -1,7 +1,8 @@
1
1
  import type { AgentMessageDto, AgentToolCall } from "@codemation/core";
2
2
 
3
- import type { AssistantModelMessage, ModelMessage, ToolModelMessage } from "ai";
3
+ import type { AssistantModelMessage, ModelMessage, ToolModelMessage, ToolResultPart } from "ai";
4
4
 
5
+ import { AgentToolResultContentFactory } from "./AgentToolResultContentFactory";
5
6
  import type { ExecutedToolCall } from "./aiAgentSupport.types";
6
7
 
7
8
  /**
@@ -41,21 +42,36 @@ export class AgentMessageFactory {
41
42
  * Builds the `{ role: "tool", content: [{ type: "tool-result", ... }, ...] }` message returned
42
43
  * to the model after each tool round.
43
44
  */
44
- static createToolResultsMessage(executedToolCalls: ReadonlyArray<ExecutedToolCall>): ToolModelMessage {
45
+ static createToolResultsMessage(
46
+ executedToolCalls: ReadonlyArray<ExecutedToolCall>,
47
+ passToolBinariesToModel = true,
48
+ ): ToolModelMessage {
45
49
  return {
46
50
  role: "tool",
47
51
  content: executedToolCalls.map((executed) => ({
48
52
  type: "tool-result",
49
53
  toolCallId: executed.toolCallId,
50
54
  toolName: executed.toolName,
51
- output: {
52
- type: "json",
53
- value: AgentMessageFactory.toToolResultJson(executed.result),
54
- },
55
+ output: AgentMessageFactory.toToolResultOutput(executed.result, passToolBinariesToModel),
55
56
  })),
56
57
  };
57
58
  }
58
59
 
60
+ /**
61
+ * Routes a tool result to a native multimodal `{ type: "content" }` output when it is
62
+ * content-block-shaped (an MCP `CallToolResult`) and binary passdown is enabled; otherwise keeps
63
+ * the inert `{ type: "json" }` path.
64
+ */
65
+ private static toToolResultOutput(result: unknown, passToolBinariesToModel: boolean): ToolResultPart["output"] {
66
+ if (passToolBinariesToModel) {
67
+ const content = AgentToolResultContentFactory.tryMapToContentOutput(result);
68
+ if (content !== undefined) {
69
+ return { type: "content", value: content };
70
+ }
71
+ }
72
+ return { type: "json", value: AgentMessageFactory.toToolResultJson(result) };
73
+ }
74
+
59
75
  private static toToolResultJson(value: unknown): import("ai").JSONValue {
60
76
  if (value === undefined) return null;
61
77
  try {