(config: Record<string, any>, question: string, options: ICommonObject)
| 298 | } |
| 299 | |
| 300 | const _generateSelectedTools = async (config: Record<string, any>, question: string, options: ICommonObject) => { |
| 301 | try { |
| 302 | const chatModelComponent = config.componentNodes[config.selectedChatModel?.name] |
| 303 | if (!chatModelComponent) { |
| 304 | throw new Error('Chat model component not found') |
| 305 | } |
| 306 | const nodeInstanceFilePath = chatModelComponent.filePath as string |
| 307 | const nodeModule = await import(nodeInstanceFilePath) |
| 308 | const newToolNodeInstance = new nodeModule.nodeClass() |
| 309 | const model = (await newToolNodeInstance.init(config.selectedChatModel, '', options)) as BaseChatModel |
| 310 | |
| 311 | // Create a parser to validate the output |
| 312 | const parser = StructuredOutputParser.fromZodSchema(ToolType as any) |
| 313 | |
| 314 | // Generate JSON schema from our Zod schema |
| 315 | const formatInstructions = parser.getFormatInstructions() |
| 316 | |
| 317 | // Full conversation with system prompt and instructions |
| 318 | const messages = [ |
| 319 | { |
| 320 | role: 'system', |
| 321 | content: `${config.prompt}\n\n${formatInstructions}\n\nMake sure to follow the exact JSON schema structure.` |
| 322 | }, |
| 323 | { |
| 324 | role: 'user', |
| 325 | content: question |
| 326 | } |
| 327 | ] |
| 328 | |
| 329 | // Standard completion without structured output |
| 330 | const response = await model.invoke(messages) |
| 331 | |
| 332 | // Try to extract JSON from the response |
| 333 | const responseContent = extractResponseContent(response) |
| 334 | const jsonMatch = responseContent.match(/```json\n([\s\S]*?)\n```/) || responseContent.match(/{[\s\S]*?}/) |
| 335 | |
| 336 | if (jsonMatch) { |
| 337 | const jsonStr = jsonMatch[1] || jsonMatch[0] |
| 338 | try { |
| 339 | const parsedJSON = JSON.parse(jsonStr) |
| 340 | // Validate with our schema |
| 341 | return ToolType.parse(parsedJSON) |
| 342 | } catch (parseError) { |
| 343 | console.error('Error parsing JSON from response:', parseError) |
| 344 | return { error: 'Failed to parse JSON from response', content: responseContent } |
| 345 | } |
| 346 | } else { |
| 347 | console.error('No JSON found in response:', responseContent) |
| 348 | return { error: 'No JSON found in response', content: responseContent } |
| 349 | } |
| 350 | } catch (error) { |
| 351 | console.error('Error generating AgentflowV2:', error) |
| 352 | return { error: error.message || 'Unknown error occurred' } |
| 353 | } |
| 354 | } |
| 355 | |
| 356 | const generateNodesEdges = async (config: Record<string, any>, question: string, options?: ICommonObject) => { |
| 357 | try { |
no test coverage detected