| 1 | import { IDE, PromptLog } from "core"; |
| 2 | import { |
| 3 | FromWebviewProtocol, |
| 4 | ToCoreProtocol, |
| 5 | ToWebviewProtocol, |
| 6 | } from "core/protocol"; |
| 7 | import { MessageIde } from "core/protocol/messenger/messageIde"; |
| 8 | import { |
| 9 | GeneratorReturnType, |
| 10 | GeneratorYieldType, |
| 11 | WebviewSingleProtocolMessage, |
| 12 | } from "core/protocol/util"; |
| 13 | import { ChatMessage } from "../redux/store"; |
| 14 | import { IIdeMessenger } from "./IdeMessenger"; |
| 15 | |
| 16 | async function defaultMockHandleMessage<T extends keyof FromWebviewProtocol>( |
| 17 | messageType: T, |
| 18 | data: FromWebviewProtocol[T][0], |
| 19 | ): Promise<FromWebviewProtocol[T][1]> { |
| 20 | function returnFor<K extends keyof FromWebviewProtocol>( |
| 21 | _: K, |
| 22 | value: FromWebviewProtocol[K][1], |
| 23 | ): FromWebviewProtocol[T][1] { |
| 24 | return value as unknown as FromWebviewProtocol[T][1]; |
| 25 | } |
| 26 | |
| 27 | switch (messageType) { |
| 28 | case "history/list": |
| 29 | return returnFor("history/list", [ |
| 30 | { |
| 31 | title: "Session 1", |
| 32 | sessionId: "session-1", |
| 33 | dateCreated: new Date().toString(), |
| 34 | workspaceDirectory: "/tmp", |
| 35 | }, |
| 36 | ]); |
| 37 | case "getControlPlaneSessionInfo": |
| 38 | return returnFor("getControlPlaneSessionInfo", { |
| 39 | accessToken: "", |
| 40 | account: { |
| 41 | label: "", |
| 42 | id: "", |
| 43 | }, |
| 44 | }); |
| 45 | case "config/getSerializedProfileInfo": |
| 46 | return returnFor("config/getSerializedProfileInfo", { |
| 47 | organizations: [], |
| 48 | profileId: "test-profile", |
| 49 | result: { |
| 50 | config: undefined, |
| 51 | errors: [], |
| 52 | configLoadInterrupted: false, |
| 53 | }, |
| 54 | selectedOrgId: "local", |
| 55 | }); |
| 56 | default: |
| 57 | throw new Error(`Unknown message type ${messageType}`); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | export class MockIdeMessenger implements IIdeMessenger { |
| 62 | ide: IDE; |
| 63 | |
| 64 | constructor() { |
| 65 | this.ide = new MessageIde( |
| 66 | (messageType, data) => { |
| 67 | throw new Error("Not implemented"); |
| 68 | }, |
| 69 | (messageType, callback) => {}, |
| 70 | ); |
| 71 | } |
| 72 | |
| 73 | async *llmStreamChat( |
| 74 | msg: ToCoreProtocol["llm/streamChat"][0], |
| 75 | cancelToken: AbortSignal, |
| 76 | ): AsyncGenerator<ChatMessage[], PromptLog | undefined> { |
| 77 | yield [ |
| 78 | { |
| 79 | role: "assistant", |
| 80 | content: "This is a test", |
| 81 | }, |
| 82 | ]; |
| 83 | |
| 84 | return undefined; |
| 85 | } |
| 86 | |
| 87 | post<T extends keyof FromWebviewProtocol>( |
| 88 | messageType: T, |
| 89 | data: FromWebviewProtocol[T][0], |
| 90 | messageId?: string, |
| 91 | attempt?: number, |
| 92 | ): void {} |
| 93 | |
| 94 | async request<T extends keyof FromWebviewProtocol>( |
| 95 | messageType: T, |
| 96 | data: FromWebviewProtocol[T][0], |
| 97 | ): Promise<WebviewSingleProtocolMessage<T>> { |
| 98 | const content = await defaultMockHandleMessage(messageType, data); |
| 99 | return { |
| 100 | status: "success", |
| 101 | content, |
| 102 | done: true, |
| 103 | }; |
| 104 | } |
| 105 | |
| 106 | respond<T extends keyof ToWebviewProtocol>( |
| 107 | messageType: T, |
| 108 | data: ToWebviewProtocol[T][1], |
| 109 | messageId: string, |
| 110 | ): void {} |
| 111 | |
| 112 | async *streamRequest<T extends keyof FromWebviewProtocol>( |
| 113 | messageType: T, |
| 114 | data: FromWebviewProtocol[T][0], |
| 115 | cancelToken?: AbortSignal, |
| 116 | ): AsyncGenerator< |
| 117 | GeneratorYieldType<FromWebviewProtocol[T][1]>[], |
| 118 | GeneratorReturnType<FromWebviewProtocol[T][1]> | undefined |
| 119 | > { |
| 120 | return undefined; |
| 121 | } |
| 122 | } |