@codemation/core-nodes 0.4.1 → 0.4.3
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.
- package/CHANGELOG.md +23 -0
- package/dist/index.cjs +665 -85
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +194 -5
- package/dist/index.d.ts +194 -5
- package/dist/index.js +648 -87
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
- package/src/chatModels/openAiChatModelConfig.ts +3 -0
- package/src/nodes/AIAgentNode.ts +294 -102
- package/src/nodes/AgentToolErrorClassifier.ts +106 -0
- package/src/nodes/AgentToolExecutionCoordinator.ts +364 -0
- package/src/nodes/AgentToolRepair.types.ts +26 -0
- package/src/nodes/AgentToolRepairExhaustedError.ts +51 -0
- package/src/nodes/AgentToolRepairPolicy.ts +21 -0
- package/src/nodes/aiAgent.ts +4 -0
|
@@ -0,0 +1,364 @@
|
|
|
1
|
+
import type { JsonValue, NodeExecutionContext } from "@codemation/core";
|
|
2
|
+
import { CodemationTelemetryAttributeNames, ConnectionInvocationIdFactory, inject, injectable } from "@codemation/core";
|
|
3
|
+
|
|
4
|
+
import type { AIAgent } from "./AIAgentConfig";
|
|
5
|
+
import { AgentOutputFactory } from "./AgentOutputFactory";
|
|
6
|
+
import { AgentToolCallPortMap } from "./AgentToolCallPortMapFactory";
|
|
7
|
+
import { AgentToolErrorClassifier } from "./AgentToolErrorClassifier";
|
|
8
|
+
import { AgentToolRepairExhaustedError } from "./AgentToolRepairExhaustedError";
|
|
9
|
+
import { AgentToolRepairPolicy } from "./AgentToolRepairPolicy";
|
|
10
|
+
import type { AgentToolRepairDecision, AgentToolValidationIssue } from "./AgentToolRepair.types";
|
|
11
|
+
import type { ExecutedToolCall, PlannedToolCall } from "./aiAgentSupport.types";
|
|
12
|
+
|
|
13
|
+
@injectable()
|
|
14
|
+
export class AgentToolExecutionCoordinator {
|
|
15
|
+
constructor(
|
|
16
|
+
@inject(AgentToolErrorClassifier)
|
|
17
|
+
private readonly errorClassifier: AgentToolErrorClassifier,
|
|
18
|
+
@inject(AgentToolRepairPolicy)
|
|
19
|
+
private readonly repairPolicy: AgentToolRepairPolicy,
|
|
20
|
+
) {}
|
|
21
|
+
|
|
22
|
+
async execute(
|
|
23
|
+
args: Readonly<{
|
|
24
|
+
plannedToolCalls: ReadonlyArray<PlannedToolCall>;
|
|
25
|
+
ctx: NodeExecutionContext<AIAgent<any, any>>;
|
|
26
|
+
agentName: string;
|
|
27
|
+
repairAttemptsByToolName: Map<string, number>;
|
|
28
|
+
}>,
|
|
29
|
+
): Promise<ReadonlyArray<ExecutedToolCall>> {
|
|
30
|
+
const results = await Promise.allSettled(
|
|
31
|
+
args.plannedToolCalls.map(
|
|
32
|
+
async (plannedToolCall) => await this.executePlannedToolCall({ ...args, plannedToolCall }),
|
|
33
|
+
),
|
|
34
|
+
);
|
|
35
|
+
|
|
36
|
+
const rejected = results.find((result) => result.status === "rejected");
|
|
37
|
+
if (rejected?.status === "rejected") {
|
|
38
|
+
throw rejected.reason instanceof Error ? rejected.reason : new Error(String(rejected.reason));
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
return results
|
|
42
|
+
.filter((result): result is PromiseFulfilledResult<ExecutedToolCall> => result.status === "fulfilled")
|
|
43
|
+
.map((result) => result.value);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
private async executePlannedToolCall(
|
|
47
|
+
args: Readonly<{
|
|
48
|
+
plannedToolCall: PlannedToolCall;
|
|
49
|
+
ctx: NodeExecutionContext<AIAgent<any, any>>;
|
|
50
|
+
agentName: string;
|
|
51
|
+
repairAttemptsByToolName: Map<string, number>;
|
|
52
|
+
}>,
|
|
53
|
+
): Promise<ExecutedToolCall> {
|
|
54
|
+
const { plannedToolCall, ctx } = args;
|
|
55
|
+
const toolCallInputsByPort = AgentToolCallPortMap.fromInput(plannedToolCall.toolCall.input ?? {});
|
|
56
|
+
const invocationId = ConnectionInvocationIdFactory.create();
|
|
57
|
+
const startedAt = new Date();
|
|
58
|
+
const span = ctx.telemetry.startChildSpan({
|
|
59
|
+
name: "agent.tool.call",
|
|
60
|
+
kind: "client",
|
|
61
|
+
startedAt,
|
|
62
|
+
attributes: {
|
|
63
|
+
[CodemationTelemetryAttributeNames.connectionInvocationId]: invocationId,
|
|
64
|
+
[CodemationTelemetryAttributeNames.toolName]: plannedToolCall.binding.config.name,
|
|
65
|
+
},
|
|
66
|
+
});
|
|
67
|
+
await ctx.nodeState?.markRunning({
|
|
68
|
+
nodeId: plannedToolCall.nodeId,
|
|
69
|
+
activationId: ctx.activationId,
|
|
70
|
+
inputsByPort: toolCallInputsByPort,
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
try {
|
|
74
|
+
const serialized = await plannedToolCall.binding.langChainTool.invoke(plannedToolCall.toolCall.input ?? {});
|
|
75
|
+
const result = this.parseToolOutput(serialized);
|
|
76
|
+
const finishedAt = new Date();
|
|
77
|
+
await ctx.nodeState?.markCompleted({
|
|
78
|
+
nodeId: plannedToolCall.nodeId,
|
|
79
|
+
activationId: ctx.activationId,
|
|
80
|
+
inputsByPort: toolCallInputsByPort,
|
|
81
|
+
outputs: AgentOutputFactory.fromUnknown(result),
|
|
82
|
+
});
|
|
83
|
+
await span.attachArtifact({
|
|
84
|
+
kind: "tool.input",
|
|
85
|
+
contentType: "application/json",
|
|
86
|
+
previewJson: this.toJsonValue(plannedToolCall.toolCall.input),
|
|
87
|
+
});
|
|
88
|
+
await span.attachArtifact({
|
|
89
|
+
kind: "tool.output",
|
|
90
|
+
contentType: "application/json",
|
|
91
|
+
previewJson: this.toJsonValue(result),
|
|
92
|
+
});
|
|
93
|
+
await span.end({ status: "ok", endedAt: finishedAt });
|
|
94
|
+
await ctx.nodeState?.appendConnectionInvocation({
|
|
95
|
+
invocationId,
|
|
96
|
+
connectionNodeId: plannedToolCall.nodeId,
|
|
97
|
+
parentAgentNodeId: ctx.nodeId,
|
|
98
|
+
parentAgentActivationId: ctx.activationId,
|
|
99
|
+
status: "completed",
|
|
100
|
+
managedInput: this.toJsonValue(plannedToolCall.toolCall.input),
|
|
101
|
+
managedOutput: this.toJsonValue(result),
|
|
102
|
+
queuedAt: startedAt.toISOString(),
|
|
103
|
+
startedAt: startedAt.toISOString(),
|
|
104
|
+
finishedAt: finishedAt.toISOString(),
|
|
105
|
+
});
|
|
106
|
+
return {
|
|
107
|
+
toolName: plannedToolCall.binding.config.name,
|
|
108
|
+
toolCallId: plannedToolCall.toolCall.id ?? plannedToolCall.binding.config.name,
|
|
109
|
+
serialized: typeof serialized === "string" ? serialized : JSON.stringify(serialized),
|
|
110
|
+
result,
|
|
111
|
+
} satisfies ExecutedToolCall;
|
|
112
|
+
} catch (error) {
|
|
113
|
+
const classification = this.errorClassifier.classify({
|
|
114
|
+
error,
|
|
115
|
+
toolName: plannedToolCall.binding.config.name,
|
|
116
|
+
schema: plannedToolCall.binding.langChainTool.schema,
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
if (classification.kind !== "repairable_validation_error") {
|
|
120
|
+
const effectiveError = classification.effectiveError;
|
|
121
|
+
await this.recordFailedInvocation({
|
|
122
|
+
invocationId,
|
|
123
|
+
plannedToolCall,
|
|
124
|
+
ctx,
|
|
125
|
+
startedAt,
|
|
126
|
+
inputsByPort: toolCallInputsByPort,
|
|
127
|
+
managedInput: this.toJsonValue(plannedToolCall.toolCall.input),
|
|
128
|
+
error: effectiveError,
|
|
129
|
+
});
|
|
130
|
+
await span.attachArtifact({
|
|
131
|
+
kind: "tool.input",
|
|
132
|
+
contentType: "application/json",
|
|
133
|
+
previewJson: this.toJsonValue(plannedToolCall.toolCall.input),
|
|
134
|
+
});
|
|
135
|
+
await span.end({
|
|
136
|
+
status: "error",
|
|
137
|
+
statusMessage: effectiveError.message,
|
|
138
|
+
endedAt: new Date(),
|
|
139
|
+
});
|
|
140
|
+
throw effectiveError;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
const repairDecision = this.repairPolicy.createDecision(
|
|
144
|
+
plannedToolCall.binding.config.name,
|
|
145
|
+
args.repairAttemptsByToolName,
|
|
146
|
+
);
|
|
147
|
+
|
|
148
|
+
if (repairDecision.nextAction === "fail_agent_run") {
|
|
149
|
+
const exhaustedError = new AgentToolRepairExhaustedError({
|
|
150
|
+
agentName: args.agentName,
|
|
151
|
+
nodeId: ctx.nodeId,
|
|
152
|
+
toolName: plannedToolCall.binding.config.name,
|
|
153
|
+
maxAttempts: repairDecision.maxAttempts,
|
|
154
|
+
lastManagedInput: this.toJsonValue(plannedToolCall.toolCall.input),
|
|
155
|
+
lastValidationIssues: classification.issues,
|
|
156
|
+
});
|
|
157
|
+
await this.recordFailedInvocation({
|
|
158
|
+
invocationId,
|
|
159
|
+
plannedToolCall,
|
|
160
|
+
ctx,
|
|
161
|
+
startedAt,
|
|
162
|
+
inputsByPort: toolCallInputsByPort,
|
|
163
|
+
managedInput: this.toJsonValue(plannedToolCall.toolCall.input),
|
|
164
|
+
error: exhaustedError,
|
|
165
|
+
errorDetails: exhaustedError.details,
|
|
166
|
+
});
|
|
167
|
+
await span.attachArtifact({
|
|
168
|
+
kind: "tool.input",
|
|
169
|
+
contentType: "application/json",
|
|
170
|
+
previewJson: this.toJsonValue(plannedToolCall.toolCall.input),
|
|
171
|
+
});
|
|
172
|
+
await span.attachArtifact({
|
|
173
|
+
kind: "tool.error",
|
|
174
|
+
contentType: "application/json",
|
|
175
|
+
previewJson: exhaustedError.details,
|
|
176
|
+
});
|
|
177
|
+
await span.end({
|
|
178
|
+
status: "error",
|
|
179
|
+
statusMessage: exhaustedError.message,
|
|
180
|
+
endedAt: new Date(),
|
|
181
|
+
});
|
|
182
|
+
throw exhaustedError;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
const repairPayload = this.createRepairPayload({
|
|
186
|
+
toolName: plannedToolCall.binding.config.name,
|
|
187
|
+
issues: classification.issues,
|
|
188
|
+
requiredSchemaReminder: classification.requiredSchemaReminder,
|
|
189
|
+
});
|
|
190
|
+
const repairDetails = this.createRepairDetails({
|
|
191
|
+
toolName: plannedToolCall.binding.config.name,
|
|
192
|
+
issues: classification.issues,
|
|
193
|
+
requiredSchemaReminder: classification.requiredSchemaReminder,
|
|
194
|
+
repairDecision,
|
|
195
|
+
});
|
|
196
|
+
await this.recordFailedInvocation({
|
|
197
|
+
invocationId,
|
|
198
|
+
plannedToolCall,
|
|
199
|
+
ctx,
|
|
200
|
+
startedAt,
|
|
201
|
+
inputsByPort: toolCallInputsByPort,
|
|
202
|
+
managedInput: this.toJsonValue(plannedToolCall.toolCall.input),
|
|
203
|
+
error: classification.effectiveError,
|
|
204
|
+
errorDetails: repairDetails,
|
|
205
|
+
});
|
|
206
|
+
await span.attachArtifact({
|
|
207
|
+
kind: "tool.input",
|
|
208
|
+
contentType: "application/json",
|
|
209
|
+
previewJson: this.toJsonValue(plannedToolCall.toolCall.input),
|
|
210
|
+
});
|
|
211
|
+
await span.attachArtifact({
|
|
212
|
+
kind: "tool.error",
|
|
213
|
+
contentType: "application/json",
|
|
214
|
+
previewJson: repairPayload,
|
|
215
|
+
});
|
|
216
|
+
await span.end({
|
|
217
|
+
status: "error",
|
|
218
|
+
statusMessage: classification.effectiveError.message,
|
|
219
|
+
endedAt: new Date(),
|
|
220
|
+
});
|
|
221
|
+
return {
|
|
222
|
+
toolName: plannedToolCall.binding.config.name,
|
|
223
|
+
toolCallId: plannedToolCall.toolCall.id ?? plannedToolCall.binding.config.name,
|
|
224
|
+
serialized: JSON.stringify(repairPayload),
|
|
225
|
+
result: repairPayload,
|
|
226
|
+
} satisfies ExecutedToolCall;
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
private async recordFailedInvocation(
|
|
231
|
+
args: Readonly<{
|
|
232
|
+
invocationId: string;
|
|
233
|
+
plannedToolCall: PlannedToolCall;
|
|
234
|
+
ctx: NodeExecutionContext<AIAgent<any, any>>;
|
|
235
|
+
startedAt: Date;
|
|
236
|
+
inputsByPort: ReturnType<typeof AgentToolCallPortMap.fromInput>;
|
|
237
|
+
managedInput?: JsonValue;
|
|
238
|
+
error: Error;
|
|
239
|
+
errorDetails?: JsonValue;
|
|
240
|
+
}>,
|
|
241
|
+
): Promise<void> {
|
|
242
|
+
const finishedAt = new Date();
|
|
243
|
+
await args.ctx.nodeState?.markFailed({
|
|
244
|
+
nodeId: args.plannedToolCall.nodeId,
|
|
245
|
+
activationId: args.ctx.activationId,
|
|
246
|
+
inputsByPort: args.inputsByPort,
|
|
247
|
+
error: args.error,
|
|
248
|
+
});
|
|
249
|
+
await args.ctx.nodeState?.appendConnectionInvocation({
|
|
250
|
+
invocationId: args.invocationId,
|
|
251
|
+
connectionNodeId: args.plannedToolCall.nodeId,
|
|
252
|
+
parentAgentNodeId: args.ctx.nodeId,
|
|
253
|
+
parentAgentActivationId: args.ctx.activationId,
|
|
254
|
+
status: "failed",
|
|
255
|
+
managedInput: args.managedInput,
|
|
256
|
+
error: {
|
|
257
|
+
message: args.error.message,
|
|
258
|
+
name: args.error.name,
|
|
259
|
+
stack: args.error.stack,
|
|
260
|
+
details: args.errorDetails ?? this.extractErrorDetails(args.error),
|
|
261
|
+
},
|
|
262
|
+
queuedAt: args.startedAt.toISOString(),
|
|
263
|
+
startedAt: args.startedAt.toISOString(),
|
|
264
|
+
finishedAt: finishedAt.toISOString(),
|
|
265
|
+
});
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
private createRepairPayload(
|
|
269
|
+
args: Readonly<{
|
|
270
|
+
toolName: string;
|
|
271
|
+
issues?: ReadonlyArray<AgentToolValidationIssue>;
|
|
272
|
+
requiredSchemaReminder?: JsonValue;
|
|
273
|
+
}>,
|
|
274
|
+
): JsonValue {
|
|
275
|
+
const payload: Record<string, JsonValue> = {
|
|
276
|
+
status: "error",
|
|
277
|
+
errorType: "validation",
|
|
278
|
+
toolName: args.toolName,
|
|
279
|
+
message: this.createValidationMessage(args.toolName, args.issues),
|
|
280
|
+
instruction: "Call the tool again with all required fields present and correctly typed.",
|
|
281
|
+
};
|
|
282
|
+
if (args.requiredSchemaReminder !== undefined) {
|
|
283
|
+
payload["requiredSchemaReminder"] = args.requiredSchemaReminder;
|
|
284
|
+
}
|
|
285
|
+
return payload;
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
private createRepairDetails(
|
|
289
|
+
args: Readonly<{
|
|
290
|
+
toolName: string;
|
|
291
|
+
issues?: ReadonlyArray<AgentToolValidationIssue>;
|
|
292
|
+
requiredSchemaReminder?: JsonValue;
|
|
293
|
+
repairDecision: AgentToolRepairDecision;
|
|
294
|
+
}>,
|
|
295
|
+
): JsonValue {
|
|
296
|
+
const details: Record<string, JsonValue> = {
|
|
297
|
+
errorType: "validation",
|
|
298
|
+
toolName: args.toolName,
|
|
299
|
+
recoveryHint: "Call the same tool again with every required field present and correctly typed.",
|
|
300
|
+
repair: {
|
|
301
|
+
attempt: args.repairDecision.attempt,
|
|
302
|
+
maxAttempts: args.repairDecision.maxAttempts,
|
|
303
|
+
nextAction: args.repairDecision.nextAction,
|
|
304
|
+
},
|
|
305
|
+
};
|
|
306
|
+
if (args.issues && args.issues.length > 0) {
|
|
307
|
+
details["issues"] = args.issues.map((issue) => this.serializeIssue(issue));
|
|
308
|
+
}
|
|
309
|
+
if (args.requiredSchemaReminder !== undefined) {
|
|
310
|
+
details["requiredSchemaReminder"] = args.requiredSchemaReminder;
|
|
311
|
+
}
|
|
312
|
+
return details;
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
private createValidationMessage(
|
|
316
|
+
toolName: string,
|
|
317
|
+
issues: ReadonlyArray<AgentToolValidationIssue> | undefined,
|
|
318
|
+
): string {
|
|
319
|
+
const firstIssue = issues?.[0];
|
|
320
|
+
if (!firstIssue) {
|
|
321
|
+
return `Your previous tool call for "${toolName}" was invalid and did not match the expected schema.`;
|
|
322
|
+
}
|
|
323
|
+
const fieldPath = firstIssue.path.length > 0 ? firstIssue.path.join(".") : "<root>";
|
|
324
|
+
return `Your previous tool call for "${toolName}" was invalid because field "${fieldPath}" failed validation: ${firstIssue.message}`;
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
private parseToolOutput(serialized: unknown): unknown {
|
|
328
|
+
if (typeof serialized !== "string") {
|
|
329
|
+
return serialized;
|
|
330
|
+
}
|
|
331
|
+
try {
|
|
332
|
+
return JSON.parse(serialized) as unknown;
|
|
333
|
+
} catch {
|
|
334
|
+
return serialized;
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
private toJsonValue(value: unknown): JsonValue | undefined {
|
|
339
|
+
if (value === undefined) {
|
|
340
|
+
return undefined;
|
|
341
|
+
}
|
|
342
|
+
return JSON.parse(JSON.stringify(value)) as JsonValue;
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
private extractErrorDetails(error: Error): JsonValue | undefined {
|
|
346
|
+
const candidate = error as Error & { details?: JsonValue };
|
|
347
|
+
return candidate.details;
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
private serializeIssue(issue: AgentToolValidationIssue): JsonValue {
|
|
351
|
+
const result: Record<string, JsonValue> = {
|
|
352
|
+
path: [...issue.path],
|
|
353
|
+
code: issue.code,
|
|
354
|
+
message: issue.message,
|
|
355
|
+
};
|
|
356
|
+
if (issue.expected !== undefined) {
|
|
357
|
+
result["expected"] = issue.expected;
|
|
358
|
+
}
|
|
359
|
+
if (issue.received !== undefined) {
|
|
360
|
+
result["received"] = issue.received;
|
|
361
|
+
}
|
|
362
|
+
return result;
|
|
363
|
+
}
|
|
364
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { JsonValue } from "@codemation/core";
|
|
2
|
+
|
|
3
|
+
export type AgentToolFailureKind = "repairable_validation_error" | "transient_execution_error" | "non_repairable_error";
|
|
4
|
+
|
|
5
|
+
export type AgentToolRepairNextAction = "model_retry_with_tool_error_message" | "fail_agent_run";
|
|
6
|
+
|
|
7
|
+
export interface AgentToolValidationIssue {
|
|
8
|
+
readonly path: ReadonlyArray<string | number>;
|
|
9
|
+
readonly code: string;
|
|
10
|
+
readonly message: string;
|
|
11
|
+
readonly expected?: string;
|
|
12
|
+
readonly received?: string;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export interface AgentToolRepairDecision {
|
|
16
|
+
readonly attempt: number;
|
|
17
|
+
readonly maxAttempts: number;
|
|
18
|
+
readonly nextAction: AgentToolRepairNextAction;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export interface AgentToolFailureClassification {
|
|
22
|
+
readonly kind: AgentToolFailureKind;
|
|
23
|
+
readonly effectiveError: Error;
|
|
24
|
+
readonly issues?: ReadonlyArray<AgentToolValidationIssue>;
|
|
25
|
+
readonly requiredSchemaReminder?: JsonValue;
|
|
26
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import type { JsonValue } from "@codemation/core";
|
|
2
|
+
|
|
3
|
+
import type { AgentToolValidationIssue } from "./AgentToolRepair.types";
|
|
4
|
+
|
|
5
|
+
export class AgentToolRepairExhaustedError extends Error {
|
|
6
|
+
readonly details: JsonValue;
|
|
7
|
+
|
|
8
|
+
constructor(
|
|
9
|
+
args: Readonly<{
|
|
10
|
+
agentName: string;
|
|
11
|
+
nodeId: string;
|
|
12
|
+
toolName: string;
|
|
13
|
+
maxAttempts: number;
|
|
14
|
+
lastManagedInput?: JsonValue;
|
|
15
|
+
lastValidationIssues?: ReadonlyArray<AgentToolValidationIssue>;
|
|
16
|
+
}>,
|
|
17
|
+
) {
|
|
18
|
+
super(
|
|
19
|
+
`AIAgent "${args.agentName}" (${args.nodeId}) could not recover from invalid tool calls for "${args.toolName}" after ${args.maxAttempts} repair attempt(s).`,
|
|
20
|
+
);
|
|
21
|
+
this.name = "AgentToolRepairExhaustedError";
|
|
22
|
+
const details: Record<string, JsonValue> = {
|
|
23
|
+
toolName: args.toolName,
|
|
24
|
+
maxAttempts: args.maxAttempts,
|
|
25
|
+
recommendation:
|
|
26
|
+
"Check tool schema, tool description, or inject known values in code instead of asking the model to infer them.",
|
|
27
|
+
};
|
|
28
|
+
if (args.lastManagedInput !== undefined) {
|
|
29
|
+
details["lastManagedInput"] = args.lastManagedInput;
|
|
30
|
+
}
|
|
31
|
+
if (args.lastValidationIssues && args.lastValidationIssues.length > 0) {
|
|
32
|
+
details["lastValidationIssues"] = args.lastValidationIssues.map((issue) => this.serializeIssue(issue));
|
|
33
|
+
}
|
|
34
|
+
this.details = details;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
private serializeIssue(issue: AgentToolValidationIssue): JsonValue {
|
|
38
|
+
const result: Record<string, JsonValue> = {
|
|
39
|
+
path: [...issue.path],
|
|
40
|
+
code: issue.code,
|
|
41
|
+
message: issue.message,
|
|
42
|
+
};
|
|
43
|
+
if (issue.expected !== undefined) {
|
|
44
|
+
result["expected"] = issue.expected;
|
|
45
|
+
}
|
|
46
|
+
if (issue.received !== undefined) {
|
|
47
|
+
result["received"] = issue.received;
|
|
48
|
+
}
|
|
49
|
+
return result;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { injectable } from "@codemation/core";
|
|
2
|
+
|
|
3
|
+
import type { AgentToolRepairDecision } from "./AgentToolRepair.types";
|
|
4
|
+
|
|
5
|
+
@injectable()
|
|
6
|
+
export class AgentToolRepairPolicy {
|
|
7
|
+
private static readonly maxRepairAttemptsPerTool = 2;
|
|
8
|
+
|
|
9
|
+
createDecision(toolName: string, attemptsByToolName: Map<string, number>): AgentToolRepairDecision {
|
|
10
|
+
const attempt = (attemptsByToolName.get(toolName) ?? 0) + 1;
|
|
11
|
+
attemptsByToolName.set(toolName, attempt);
|
|
12
|
+
return {
|
|
13
|
+
attempt,
|
|
14
|
+
maxAttempts: AgentToolRepairPolicy.maxRepairAttemptsPerTool,
|
|
15
|
+
nextAction:
|
|
16
|
+
attempt < AgentToolRepairPolicy.maxRepairAttemptsPerTool
|
|
17
|
+
? "model_retry_with_tool_error_message"
|
|
18
|
+
: "fail_agent_run",
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
}
|
package/src/nodes/aiAgent.ts
CHANGED
|
@@ -3,6 +3,10 @@ export { AgentOutputFactory } from "./AgentOutputFactory";
|
|
|
3
3
|
export { AgentStructuredOutputRepairPromptFactory } from "./AgentStructuredOutputRepairPromptFactory";
|
|
4
4
|
export { AgentStructuredOutputRunner } from "./AgentStructuredOutputRunner";
|
|
5
5
|
export { AgentToolCallPortMap } from "./AgentToolCallPortMapFactory";
|
|
6
|
+
export { AgentToolErrorClassifier } from "./AgentToolErrorClassifier";
|
|
7
|
+
export { AgentToolExecutionCoordinator } from "./AgentToolExecutionCoordinator";
|
|
8
|
+
export { AgentToolRepairExhaustedError } from "./AgentToolRepairExhaustedError";
|
|
9
|
+
export { AgentToolRepairPolicy } from "./AgentToolRepairPolicy";
|
|
6
10
|
export { AIAgent } from "./AIAgentConfig";
|
|
7
11
|
export { AIAgentExecutionHelpersFactory } from "./AIAgentExecutionHelpersFactory";
|
|
8
12
|
export { AIAgentNode } from "./AIAgentNode";
|