( task: LLMTask, chatState: ChatStateType, db: TaskyonDatabase )
| 706 | } |
| 707 | |
| 708 | export async function getOpenAIAssistantResponse( |
| 709 | task: LLMTask, |
| 710 | chatState: ChatStateType, |
| 711 | db: TaskyonDatabase |
| 712 | ) { |
| 713 | if (task.content) { |
| 714 | const openai = getOpenai(chatState.openAIApiKey); |
| 715 | console.log('send task to assistant!', task); |
| 716 | |
| 717 | // get all messages from a chat |
| 718 | const taskIdChain = taskChain(task.id, chatState.Tasks); |
| 719 | const taskList = taskIdChain.map((t) => chatState.Tasks[t]); |
| 720 | |
| 721 | // Find all fileIDs in the task list |
| 722 | const fileIDs = findAllFilesInTasks(taskList); |
| 723 | |
| 724 | // Get all corresponding filemapping entries from our database |
| 725 | const currentTaskListfileMappings = ( |
| 726 | await Promise.all( |
| 727 | fileIDs.map(async (fuuid) => await getFileMappingByUuid(fuuid)) |
| 728 | ) |
| 729 | ).filter((fm) => fm !== null) as FileMappingDocType[]; |
| 730 | |
| 731 | // upload files to openai with that information one-by-one |
| 732 | const openAIFileMappings: Record<string, string> = {}; |
| 733 | for (const fm of currentTaskListfileMappings) { |
| 734 | if (fm?.opfs) { |
| 735 | const file = await openFile(fm.opfs); |
| 736 | if (file) { |
| 737 | // TODO: only upload file, if it doesn't have a openAI ID yet. |
| 738 | const fileIDopenAI = await uploadFileToOpenAI( |
| 739 | file, |
| 740 | chatState.openAIApiKey |
| 741 | ); |
| 742 | if (fileIDopenAI) { |
| 743 | fm.openAIFileId = fileIDopenAI; |
| 744 | openAIFileMappings[fm.uuid] = fileIDopenAI; |
| 745 | } |
| 746 | } |
| 747 | } |
| 748 | } |
| 749 | |
| 750 | // Update file mapping database |
| 751 | await db.filemappings.bulkUpsert(currentTaskListfileMappings); |
| 752 | |
| 753 | // Finally, Convert task list to OpenAI thread messages |
| 754 | const openAIConversationThread = convertTasksToOpenAIThread( |
| 755 | taskList, |
| 756 | openAIFileMappings |
| 757 | ); |
| 758 | |
| 759 | // then upload the taskThread and execute it. |
| 760 | const thread = await openai.beta.threads.create({ |
| 761 | messages: openAIConversationThread, |
| 762 | }); |
| 763 | const threadId = thread.id; |
| 764 | const assistantId = chatState.openAIAssistant; |
| 765 |
no test coverage detected