( task: LLMTask, chatState: ChatStateType, db: TaskyonDatabase )
| 62 | } |
| 63 | |
| 64 | export async function processChatTask( |
| 65 | task: LLMTask, |
| 66 | chatState: ChatStateType, |
| 67 | db: TaskyonDatabase |
| 68 | ) { |
| 69 | //TODO: merge this function with the assistants function |
| 70 | if (chatState.useOpenAIAssistants && openAIUsed(chatState.baseURL)) { |
| 71 | const messages = await getOpenAIAssistantResponse(task, chatState, db); |
| 72 | if (messages) { |
| 73 | task.result = { |
| 74 | type: 'AssistantAnswer', |
| 75 | assistantResponse: messages, |
| 76 | }; |
| 77 | } |
| 78 | } else { |
| 79 | console.log('execute chat task!', task); |
| 80 | //TODO: also do this, if we start the task "autonomously" in which we basically |
| 81 | // allow it to create new tasks... |
| 82 | //TODO: we can create more things here like giving it context form other tasks, lookup |
| 83 | // main objective, previous tasks etc.... |
| 84 | const useToolChat = |
| 85 | task.allowedTools?.length && !chatState.enableOpenAiTools; |
| 86 | |
| 87 | const { openAIConversationThread, tools } = prepareTasksForInference( |
| 88 | task, |
| 89 | chatState, |
| 90 | useToolChat ? 'toolchat' : 'chat' |
| 91 | ); |
| 92 | |
| 93 | if (openAIConversationThread.length > 0) { |
| 94 | const chatCompletion = await callLLM( |
| 95 | openAIConversationThread, |
| 96 | tools, |
| 97 | chatState, |
| 98 | getSelectedModel(chatState), |
| 99 | getAPIURLs(chatState.baseURL).chat, |
| 100 | // if the task runs in the "foreground", stream it :) |
| 101 | task.id == chatState.selectedTaskId ? true : false, |
| 102 | // this function receives chunks if we stream and "plants" them into |
| 103 | // our original task |
| 104 | (chunk) => { |
| 105 | if (chunk?.choices[0]?.delta?.tool_calls) { |
| 106 | chunk?.choices[0]?.delta?.tool_calls.forEach((t) => { |
| 107 | task.debugging.toolStreamArgsContent = |
| 108 | task.debugging.toolStreamArgsContent || {}; |
| 109 | if (t.function?.name) { |
| 110 | task.debugging.toolStreamArgsContent[t.function.name] = |
| 111 | (task.debugging.toolStreamArgsContent[t.function.name] || |
| 112 | '') + (t.function?.arguments || ''); |
| 113 | } |
| 114 | }); |
| 115 | } |
| 116 | if (chunk?.choices[0]?.delta?.content) { |
| 117 | task.debugging.streamContent = |
| 118 | (task.debugging.streamContent || '') + |
| 119 | chunk.choices[0].delta.content; |
| 120 | } |
| 121 | } |
no test coverage detected