| 1 | import { ChatMessage, CompletionOptions, LLMOptions } from "../../index.js"; |
| 2 | import { BaseLLM } from "../index.js"; |
| 3 | |
| 4 | type MockMessage = |
| 5 | | ChatMessage |
| 6 | | "REPEAT_LAST_MSG" |
| 7 | | "REPEAT_SYSTEM_MSG" |
| 8 | | "ERROR"; |
| 9 | |
| 10 | class MockLLM extends BaseLLM { |
| 11 | public completion: string = "Test Completion"; |
| 12 | public chatStreams: MockMessage[][] | undefined; |
| 13 | static providerName = "mock"; |
| 14 | |
| 15 | constructor(options: LLMOptions) { |
| 16 | super(options); |
| 17 | this.templateMessages = undefined; |
| 18 | this.chatStreams = options.requestOptions?.extraBodyProperties?.chatStream; |
| 19 | } |
| 20 | |
| 21 | protected async *_streamComplete( |
| 22 | prompt: string, |
| 23 | signal: AbortSignal, |
| 24 | options: CompletionOptions, |
| 25 | ): AsyncGenerator<string> { |
| 26 | yield this.completion; |
| 27 | } |
| 28 | |
| 29 | protected async *_streamChat( |
| 30 | messages: ChatMessage[], |
| 31 | signal: AbortSignal, |
| 32 | options: CompletionOptions, |
| 33 | ): AsyncGenerator<ChatMessage> { |
| 34 | if (this.chatStreams) { |
| 35 | const chatStream = |
| 36 | this.chatStreams?.[ |
| 37 | messages.filter((m) => m.role === "user" || m.role === "tool") |
| 38 | .length - 1 |
| 39 | ]; |
| 40 | if (chatStream) { |
| 41 | for (const message of chatStream) { |
| 42 | switch (message) { |
| 43 | case "REPEAT_LAST_MSG": |
| 44 | yield { |
| 45 | role: "assistant", |
| 46 | content: messages[messages.length - 1].content, |
| 47 | }; |
| 48 | break; |
| 49 | case "REPEAT_SYSTEM_MSG": |
| 50 | yield { |
| 51 | role: "assistant", |
| 52 | content: |
| 53 | messages.find((m) => m.role === "system")?.content || "", |
| 54 | }; |
| 55 | break; |
| 56 | case "ERROR": |
| 57 | throw new Error("Intentional error"); |
| 58 | default: |
| 59 | yield message; |
| 60 | } |
| 61 | } |
| 62 | } |
| 63 | return; |
| 64 | } |
| 65 | |
| 66 | for (const char of this.completion) { |
| 67 | yield { |
| 68 | role: "assistant", |
| 69 | content: char, |
| 70 | }; |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | export default MockLLM; |