( task: LLMTask, chatState: ChatStateType, method: 'toolchat' | 'chat' | 'taskAgent' )
| 520 | } |
| 521 | |
| 522 | export function prepareTasksForInference( |
| 523 | task: LLMTask, |
| 524 | chatState: ChatStateType, |
| 525 | method: 'toolchat' | 'chat' | 'taskAgent' |
| 526 | ): { |
| 527 | openAIConversationThread: OpenAI.Chat.Completions.ChatCompletionMessageParam[]; |
| 528 | tools: OpenAI.ChatCompletionTool[]; |
| 529 | } { |
| 530 | console.log('Prepare task tree for inference'); |
| 531 | let openAIConversationThread = buildChatFromTask(task, chatState); |
| 532 | |
| 533 | let tools: Tool[] = []; |
| 534 | |
| 535 | // Check if task has tools and OpenAI tools are not enabled |
| 536 | if ( |
| 537 | task.allowedTools?.length && |
| 538 | !chatState.enableOpenAiTools && |
| 539 | method === 'toolchat' |
| 540 | ) { |
| 541 | console.log('Creating chat task messages'); |
| 542 | // Prepare the variables for createTaskChatMessages |
| 543 | let variables = {}; |
| 544 | if (task.role === 'user') { |
| 545 | const yamlRepr = zodToYamlString(yamlToolChatType); |
| 546 | |
| 547 | variables = { |
| 548 | taskContent: task.content || '', |
| 549 | schema: yamlRepr, |
| 550 | format: 'yaml', |
| 551 | tools: summarizeTools(task.allowedTools), |
| 552 | toolList: JSON.stringify(task.allowedTools), |
| 553 | }; |
| 554 | } else { |
| 555 | const yamlRepr = zodToYamlString(toolResultChat); |
| 556 | |
| 557 | variables = { |
| 558 | toolResult: dump({ |
| 559 | successfullExecution: task.result?.toolResult?.error ? 'no' : 'yes', |
| 560 | ...task.result?.toolResult, |
| 561 | }), |
| 562 | resultSchema: yamlRepr, |
| 563 | format: 'yaml', |
| 564 | tools: summarizeTools(task.allowedTools), |
| 565 | toolList: JSON.stringify(task.allowedTools), |
| 566 | }; |
| 567 | } |
| 568 | |
| 569 | // Create additional messages using createTaskChatMessages |
| 570 | const filledTemplates = createTaskChatMessages( |
| 571 | chatState.taskChatTemplates, |
| 572 | variables |
| 573 | ); |
| 574 | |
| 575 | const taskPrompt = ( |
| 576 | task.role === 'user' |
| 577 | ? [filledTemplates['task']] |
| 578 | : [filledTemplates['toolResult']] |
| 579 | ) |
no test coverage detected