( command: FunctionCall, nonSystemMessages: GPTMessageInclSummary[], )
| 9 | import _ from "lodash"; |
| 10 | |
| 11 | export function updatePastAssistantMessage( |
| 12 | command: FunctionCall, |
| 13 | nonSystemMessages: GPTMessageInclSummary[], |
| 14 | ) { |
| 15 | /** Updates the history of the assistant message to include the filled-in parameters in the command |
| 16 | * |
| 17 | * E.g. from |
| 18 | * test(a=b) |
| 19 | * |
| 20 | * where requiredParam was missing to |
| 21 | * test(a=b, requiredParam=value) |
| 22 | * **/ |
| 23 | console.log("Updating past assistant message with command:", command); |
| 24 | const newCommandLine = `${command.name}(${Object.entries(command.args) |
| 25 | .map(([key, value]) => `${key}=${JSON.stringify(value)}`) |
| 26 | .join(", ")})`; |
| 27 | |
| 28 | // Update the last assistant message in the nonSystemMessages array |
| 29 | const lastAssistantMessageIndex = |
| 30 | nonSystemMessages.length - |
| 31 | 1 - |
| 32 | nonSystemMessages.reverse().findIndex((m) => m.role === "assistant"); |
| 33 | nonSystemMessages.reverse(); // Reverse back to original order |
| 34 | |
| 35 | nonSystemMessages[lastAssistantMessageIndex].content = nonSystemMessages[ |
| 36 | lastAssistantMessageIndex |
| 37 | ].content |
| 38 | .split("\n") // Split into lines |
| 39 | .map((line) => { |
| 40 | // Below checks if it's the same function call as the one we're updating |
| 41 | if (line.startsWith(command.name + "(") && line.trim().endsWith(")")) { |
| 42 | return newCommandLine; |
| 43 | } |
| 44 | return line; |
| 45 | }) |
| 46 | .join("\n"); // Re-join lines |
| 47 | } |
| 48 | |
| 49 | export async function streamResponseToUser( |
| 50 | res: ReadableStream, |
no outgoing calls
no test coverage detected