@fractal-solutions/qflow 1.0.32 → 1.0.34
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/dist/agent/agent.d.ts +2 -6
- package/dist/agent/agent.js +87 -81
- package/package.json +1 -1
package/dist/agent/agent.d.ts
CHANGED
|
@@ -14,13 +14,9 @@ export class AgentNode extends AsyncNode {
|
|
|
14
14
|
getLLMAction(): Promise<any>;
|
|
15
15
|
manageConversationHistory(): Promise<void>;
|
|
16
16
|
parseLLMResponse(llmResponse: any): {
|
|
17
|
-
thought: string;
|
|
18
|
-
toolCalls: any;
|
|
19
|
-
parallel: boolean;
|
|
20
|
-
} | {
|
|
21
17
|
thought: any;
|
|
22
|
-
toolCalls:
|
|
23
|
-
parallel:
|
|
18
|
+
toolCalls: any;
|
|
19
|
+
parallel: any;
|
|
24
20
|
};
|
|
25
21
|
postAsync(shared: any, prepRes: any, execRes: any): Promise<string>;
|
|
26
22
|
}
|
package/dist/agent/agent.js
CHANGED
|
@@ -93,66 +93,72 @@ export class AgentNode extends AsyncNode {
|
|
|
93
93
|
logger.final(finalOutput);
|
|
94
94
|
break;
|
|
95
95
|
}
|
|
96
|
-
|
|
97
|
-
const
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
let toolOutput;
|
|
102
|
-
try {
|
|
103
|
-
logger.toolCall(toolCall.tool, toolCall.parameters);
|
|
104
|
-
// If the tool is a sub-flow or iterator, look up the flow in the registry
|
|
105
|
-
if (toolCall.tool === 'sub_flow' || toolCall.tool === 'iterator' || toolCall.tool === 'scheduler') {
|
|
106
|
-
const flowName = toolCall.parameters.flow;
|
|
107
|
-
if (!this.flowRegistry[flowName]) {
|
|
108
|
-
throw new Error(`Flow '${flowName}' not found in registry.`);
|
|
109
|
-
}
|
|
110
|
-
toolCall.parameters.flow = this.flowRegistry[flowName];
|
|
111
|
-
}
|
|
112
|
-
toolInstance.setParams(toolCall.parameters);
|
|
113
|
-
const ToolFlowClass = toolInstance instanceof AsyncNode ? AsyncFlow : Flow;
|
|
114
|
-
const toolFlow = new ToolFlowClass(toolInstance);
|
|
115
|
-
if (toolInstance instanceof AsyncNode) {
|
|
116
|
-
toolOutput = await toolFlow.runAsync({});
|
|
96
|
+
try {
|
|
97
|
+
const executeTool = async (toolCall) => {
|
|
98
|
+
const toolInstance = this.availableTools[toolCall.tool];
|
|
99
|
+
if (!toolInstance) {
|
|
100
|
+
return `Error: Tool '${toolCall.tool}' not found. Available tools: ${Object.keys(this.availableTools).join(', ')}.`;
|
|
117
101
|
}
|
|
118
|
-
|
|
119
|
-
|
|
102
|
+
let toolOutput;
|
|
103
|
+
try {
|
|
104
|
+
logger.toolCall(toolCall.tool, toolCall.parameters);
|
|
105
|
+
// If the tool is a sub-flow, iterator or scheduler, look up the flow in the registry
|
|
106
|
+
if (toolCall.tool === 'sub_flow' || toolCall.tool === 'iterator' || toolCall.tool === 'scheduler') {
|
|
107
|
+
const flowName = toolCall.parameters.flow;
|
|
108
|
+
if (!this.flowRegistry[flowName]) {
|
|
109
|
+
throw new Error(`Flow '${flowName}' not found in registry.`);
|
|
110
|
+
}
|
|
111
|
+
toolCall.parameters.flow = this.flowRegistry[flowName];
|
|
112
|
+
}
|
|
113
|
+
toolInstance.setParams(toolCall.parameters);
|
|
114
|
+
const ToolFlowClass = toolInstance instanceof AsyncNode ? AsyncFlow : Flow;
|
|
115
|
+
const toolFlow = new ToolFlowClass(toolInstance);
|
|
116
|
+
if (toolInstance instanceof AsyncNode) {
|
|
117
|
+
toolOutput = await toolFlow.runAsync({});
|
|
118
|
+
}
|
|
119
|
+
else {
|
|
120
|
+
toolOutput = toolFlow.run({});
|
|
121
|
+
}
|
|
122
|
+
logger.toolResult(toolCall.tool, toolOutput);
|
|
123
|
+
if (this.summarizeLLM && typeof toolOutput === 'string' && toolOutput.length > 1000) {
|
|
124
|
+
logger.info(`Summarizing large tool output (${toolOutput.length} chars)...`);
|
|
125
|
+
const summarizeNode = new SummarizeNode();
|
|
126
|
+
summarizeNode.setParams({ text: toolOutput, llmNode: this.summarizeLLM });
|
|
127
|
+
const summarizeFlow = new AsyncFlow(summarizeNode);
|
|
128
|
+
toolOutput = await summarizeFlow.runAsync({});
|
|
129
|
+
logger.info(`Summarized to ${toolOutput.length} chars.`);
|
|
130
|
+
}
|
|
131
|
+
return toolOutput;
|
|
120
132
|
}
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
logger.info(`Summarizing large tool output (${toolOutput.length} chars)...`);
|
|
124
|
-
const summarizeNode = new SummarizeNode();
|
|
125
|
-
summarizeNode.setParams({ text: toolOutput, llmNode: this.summarizeLLM });
|
|
126
|
-
const summarizeFlow = new AsyncFlow(summarizeNode);
|
|
127
|
-
toolOutput = await summarizeFlow.runAsync({});
|
|
128
|
-
logger.info(`Summarized to ${toolOutput.length} chars.`);
|
|
133
|
+
catch (e) {
|
|
134
|
+
return `Error executing tool '${toolCall.tool}': ${e.message}`;
|
|
129
135
|
}
|
|
130
|
-
|
|
136
|
+
};
|
|
137
|
+
let observations = [];
|
|
138
|
+
if (parallel) {
|
|
139
|
+
const results = await Promise.all(toolCalls.map(executeTool));
|
|
140
|
+
observations = results.map((result, index) => ({
|
|
141
|
+
tool: toolCalls[index].tool,
|
|
142
|
+
parameters: toolCalls[index].parameters,
|
|
143
|
+
output: result
|
|
144
|
+
}));
|
|
131
145
|
}
|
|
132
|
-
|
|
133
|
-
|
|
146
|
+
else {
|
|
147
|
+
for (const toolCall of toolCalls) {
|
|
148
|
+
const result = await executeTool(toolCall);
|
|
149
|
+
observations.push({
|
|
150
|
+
tool: toolCall.tool,
|
|
151
|
+
parameters: toolCall.parameters,
|
|
152
|
+
output: result
|
|
153
|
+
});
|
|
154
|
+
}
|
|
134
155
|
}
|
|
135
|
-
|
|
136
|
-
let observations = [];
|
|
137
|
-
if (parallel) {
|
|
138
|
-
const results = await Promise.all(toolCalls.map(executeTool));
|
|
139
|
-
observations = results.map((result, index) => ({
|
|
140
|
-
tool: toolCalls[index].tool,
|
|
141
|
-
parameters: toolCalls[index].parameters,
|
|
142
|
-
output: result
|
|
143
|
-
}));
|
|
156
|
+
this.conversationHistory.push({ role: "user", content: `Observation: ${JSON.stringify(observations)}` });
|
|
144
157
|
}
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
observations.push({
|
|
149
|
-
tool: toolCall.tool,
|
|
150
|
-
parameters: toolCall.parameters,
|
|
151
|
-
output: result
|
|
152
|
-
});
|
|
153
|
-
}
|
|
158
|
+
catch (e) {
|
|
159
|
+
logger.error(`Error during tool execution: ${e.message}`);
|
|
160
|
+
this.conversationHistory.push({ role: "user", content: `Error: Tool execution failed with message: ${e.message}. You should try a different approach.` });
|
|
154
161
|
}
|
|
155
|
-
this.conversationHistory.push({ role: "user", content: `Observation: ${JSON.stringify(observations)}` });
|
|
156
162
|
this.conversationHistory.push({ role: "system", content: "Reflect on the last observation(s) and update your plan if necessary. What is your next step?" });
|
|
157
163
|
}
|
|
158
164
|
if (step >= this.maxSteps && finalOutput === null) {
|
|
@@ -248,59 +254,59 @@ Parameters: ${params}`;
|
|
|
248
254
|
}
|
|
249
255
|
}
|
|
250
256
|
parseLLMResponse(llmResponse) {
|
|
251
|
-
let parsed;
|
|
252
|
-
let thought = '';
|
|
253
|
-
let toolCalls = [];
|
|
254
|
-
let parallel = false;
|
|
255
|
-
let rawData = llmResponse;
|
|
256
257
|
if (typeof llmResponse === 'string') {
|
|
258
|
+
let jsonString = llmResponse;
|
|
259
|
+
// Find the first '{' and the last '}' to isolate the JSON object
|
|
260
|
+
const firstBrace = jsonString.indexOf('{');
|
|
261
|
+
const lastBrace = jsonString.lastIndexOf('}');
|
|
262
|
+
if (firstBrace !== -1 && lastBrace > firstBrace) {
|
|
263
|
+
jsonString = jsonString.substring(firstBrace, lastBrace + 1);
|
|
264
|
+
}
|
|
257
265
|
try {
|
|
258
|
-
parsed = JSON.parse(
|
|
266
|
+
const parsed = JSON.parse(jsonString);
|
|
259
267
|
if (parsed.thought && Array.isArray(parsed.tool_calls)) {
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
268
|
+
return {
|
|
269
|
+
thought: parsed.thought,
|
|
270
|
+
toolCalls: parsed.tool_calls,
|
|
271
|
+
parallel: parsed.parallel || false,
|
|
272
|
+
};
|
|
264
273
|
}
|
|
265
|
-
throw new Error("Missing required keys or invalid format in LLM response (thought, tool_calls array).");
|
|
266
274
|
}
|
|
267
275
|
catch (e) {
|
|
268
|
-
//
|
|
269
|
-
// Treat the whole response as a "thought".
|
|
270
|
-
return { thought: llmResponse, toolCalls: [], parallel: false };
|
|
276
|
+
// Parsing failed, fall through to treat the original string as a thought.
|
|
271
277
|
}
|
|
278
|
+
// Fallback for plain text responses or malformed JSON.
|
|
279
|
+
return { thought: llmResponse, toolCalls: [], parallel: false };
|
|
272
280
|
}
|
|
273
|
-
//
|
|
281
|
+
// This part of the function handles the case where llmResponse is an OBJECT,
|
|
282
|
+
// typically from OpenAI's function calling API.
|
|
283
|
+
const rawData = llmResponse;
|
|
274
284
|
if (rawData.choices && rawData.choices.length > 0 && rawData.choices[0] && rawData.choices[0].message) {
|
|
275
285
|
const message = rawData.choices[0].message;
|
|
276
286
|
if (message.tool_calls && message.tool_calls.length > 0) {
|
|
277
|
-
toolCalls = message.tool_calls.map(tc => ({
|
|
287
|
+
const toolCalls = message.tool_calls.map(tc => ({
|
|
278
288
|
tool: tc.function.name,
|
|
279
289
|
parameters: JSON.parse(tc.function.arguments)
|
|
280
290
|
}));
|
|
281
|
-
thought = message.reasoning_content || `Calling tool(s): ${toolCalls.map(tc => tc.tool).join(', ')}`;
|
|
282
|
-
|
|
283
|
-
parallel = false;
|
|
284
|
-
return { thought, toolCalls, parallel };
|
|
291
|
+
const thought = message.reasoning_content || `Calling tool(s): ${toolCalls.map(tc => tc.tool).join(', ')}`;
|
|
292
|
+
return { thought, toolCalls, parallel: false };
|
|
285
293
|
}
|
|
286
294
|
else if (typeof message.content === 'string' && message.content.trim().startsWith('{')) {
|
|
287
|
-
// If no tool_calls, but content is JSON (for thought/tool_calls/finish)
|
|
288
295
|
try {
|
|
289
|
-
parsed = JSON.parse(message.content);
|
|
296
|
+
const parsed = JSON.parse(message.content);
|
|
290
297
|
if (parsed.thought && Array.isArray(parsed.tool_calls)) {
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
298
|
+
return {
|
|
299
|
+
thought: parsed.thought,
|
|
300
|
+
toolCalls: parsed.tool_calls,
|
|
301
|
+
parallel: parsed.parallel || false,
|
|
302
|
+
};
|
|
295
303
|
}
|
|
296
|
-
throw new Error("Missing required keys or invalid format in message.content (thought, tool_calls array).");
|
|
297
304
|
}
|
|
298
305
|
catch (e) {
|
|
299
306
|
throw new Error(`Invalid JSON or unexpected format in message.content: ${e.message}. Content: ${message.content}`);
|
|
300
307
|
}
|
|
301
308
|
}
|
|
302
309
|
else if (typeof message.content === 'string') {
|
|
303
|
-
// If it's a plain string, treat it as a thought and no tool calls
|
|
304
310
|
return { thought: message.content, toolCalls: [], parallel: false };
|
|
305
311
|
}
|
|
306
312
|
}
|
package/package.json
CHANGED