( storedData: NormalizedData, requestBody: string | undefined, workDir: string, toolResultNormalizers: ToolResultNormalizer[], )
| 636 | } |
| 637 | |
| 638 | async function findSavedChatCompletionResponse( |
| 639 | storedData: NormalizedData, |
| 640 | requestBody: string | undefined, |
| 641 | workDir: string, |
| 642 | toolResultNormalizers: ToolResultNormalizer[], |
| 643 | ): Promise<ChatCompletion | undefined> { |
| 644 | // Normalize the incoming request the same way we normalize for caching |
| 645 | const normalized = await parseAndNormalizeRequest( |
| 646 | requestBody, |
| 647 | workDir, |
| 648 | toolResultNormalizers, |
| 649 | ); |
| 650 | const requestMessages = normalized.conversations[0]?.messages ?? []; |
| 651 | const requestModel = normalized.models[0]; |
| 652 | if (!requestModel) { |
| 653 | throw new Error("Unable to determine model from request"); |
| 654 | } |
| 655 | |
| 656 | // Now find a matching cached conversation (i.e., one for which this request is a prefix) |
| 657 | for (const conversation of storedData.conversations) { |
| 658 | const replyIndex = findAssistantIndexAfterPrefix( |
| 659 | requestMessages, |
| 660 | conversation.messages, |
| 661 | ); |
| 662 | if (replyIndex !== undefined) { |
| 663 | return createOpenAIResponse( |
| 664 | requestModel, |
| 665 | conversation.messages, |
| 666 | replyIndex, |
| 667 | workDir, |
| 668 | ); |
| 669 | } |
| 670 | } |
| 671 | |
| 672 | return undefined; |
| 673 | } |
| 674 | |
| 675 | async function findSavedChatCompletionError( |
| 676 | storedData: NormalizedData, |
no test coverage detected
searching dependent graphs…