| 1 | import { IDE, PromptLog } from "core"; |
| 2 | import { |
| 3 | FromIdeProtocol, |
| 4 | FromWebviewProtocol, |
| 5 | ToCoreProtocol, |
| 6 | ToWebviewProtocol, |
| 7 | } from "core/protocol"; |
| 8 | import { Message } from "core/protocol/messenger"; |
| 9 | import { MessageIde } from "core/protocol/messenger/messageIde"; |
| 10 | import { |
| 11 | GeneratorReturnType, |
| 12 | GeneratorYieldType, |
| 13 | WebviewSingleProtocolMessage, |
| 14 | } from "core/protocol/util"; |
| 15 | import { ChatMessage } from "../redux/store"; |
| 16 | import { IIdeMessenger } from "./IdeMessenger"; |
| 17 | |
| 18 | async function defaultMockHandleMessage<T extends keyof FromWebviewProtocol>( |
| 19 | messageType: T, |
| 20 | data: FromWebviewProtocol[T][0], |
| 21 | ): Promise<FromWebviewProtocol[T][1]> { |
| 22 | function returnFor<K extends keyof FromWebviewProtocol>( |
| 23 | _: K, |
| 24 | value: FromWebviewProtocol[K][1], |
| 25 | ): FromWebviewProtocol[T][1] { |
| 26 | return value as unknown as FromWebviewProtocol[T][1]; |
| 27 | } |
| 28 | |
| 29 | switch (messageType) { |
| 30 | case "history/list": |
| 31 | return returnFor("history/list", [ |
| 32 | { |
| 33 | title: "Session 1", |
| 34 | sessionId: "session-1", |
| 35 | dateCreated: new Date().toString(), |
| 36 | workspaceDirectory: "/tmp", |
| 37 | }, |
| 38 | ]); |
| 39 | case "getControlPlaneSessionInfo": |
| 40 | return returnFor("getControlPlaneSessionInfo", { |
| 41 | accessToken: "", |
| 42 | account: { |
| 43 | label: "", |
| 44 | id: "", |
| 45 | }, |
| 46 | }); |
| 47 | case "config/getSerializedProfileInfo": |
| 48 | return returnFor("config/getSerializedProfileInfo", { |
| 49 | organizations: [], |
| 50 | profileId: "test-profile", |
| 51 | result: { |
| 52 | config: undefined, |
| 53 | errors: [], |
| 54 | configLoadInterrupted: false, |
| 55 | }, |
| 56 | selectedOrgId: "local", |
| 57 | }); |
| 58 | default: |
| 59 | throw new Error(`Unknown message type ${messageType}`); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | export class MockIdeMessenger implements IIdeMessenger { |
| 64 | ide: IDE; |
| 65 | private messageHandlers: Map< |
| 66 | keyof FromIdeProtocol, |
| 67 | Array<(data: any) => void> |
| 68 | > = new Map(); |
| 69 | |
| 70 | constructor() { |
| 71 | this.ide = new MessageIde( |
| 72 | (messageType, data) => { |
| 73 | throw new Error("Not implemented"); |
| 74 | }, |
| 75 | (messageType, callback) => { |
| 76 | // Store the callback in our handlers map |
| 77 | if (!this.messageHandlers.has(messageType)) { |
| 78 | this.messageHandlers.set(messageType, []); |
| 79 | } |
| 80 | this.messageHandlers.get(messageType)?.push(callback); |
| 81 | }, |
| 82 | ); |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Simulates a message being sent from the IDE to the webview |
| 87 | * @param messageType The type of message to send |
| 88 | * @param data The data to send with the message |
| 89 | */ |
| 90 | mockMessageToWebview<T extends keyof ToWebviewProtocol>( |
| 91 | messageType: T, |
| 92 | data: ToWebviewProtocol[T][0], |
| 93 | ): void { |
| 94 | // Create a message object that matches what the useWebviewListener hook expects |
| 95 | const messageData: Message<ToWebviewProtocol[T][0]> = { |
| 96 | messageType, |
| 97 | data, |
| 98 | messageId: `mock-${Date.now()}-${Math.random().toString(36).substring(2)}`, |
| 99 | }; |
| 100 | |
| 101 | // Dispatch a custom message event that the window event listener will pick up |
| 102 | window.dispatchEvent( |
| 103 | new MessageEvent("message", { |
| 104 | data: messageData, |
| 105 | origin: window.location.origin, |
| 106 | }), |
| 107 | ); |
| 108 | } |
| 109 | |
| 110 | async *llmStreamChat( |
| 111 | msg: ToCoreProtocol["llm/streamChat"][0], |
| 112 | cancelToken: AbortSignal, |
| 113 | ): AsyncGenerator<ChatMessage[], PromptLog | undefined> { |
| 114 | yield [ |
| 115 | { |
| 116 | role: "assistant", |
| 117 | content: "This is a test", |
| 118 | }, |
| 119 | ]; |
| 120 | return undefined; |
| 121 | } |
| 122 | |
| 123 | post<T extends keyof FromWebviewProtocol>( |
| 124 | messageType: T, |
| 125 | data: FromWebviewProtocol[T][0], |
| 126 | messageId?: string, |
| 127 | attempt?: number, |
| 128 | ): void {} |
| 129 | |
| 130 | async request<T extends keyof FromWebviewProtocol>( |
| 131 | messageType: T, |
| 132 | data: FromWebviewProtocol[T][0], |
| 133 | ): Promise<WebviewSingleProtocolMessage<T>> { |
| 134 | const content = await defaultMockHandleMessage(messageType, data); |
| 135 | return { |
| 136 | status: "success", |
| 137 | content, |
| 138 | done: true, |
| 139 | }; |
| 140 | } |
| 141 | |
| 142 | respond<T extends keyof ToWebviewProtocol>( |
| 143 | messageType: T, |
| 144 | data: ToWebviewProtocol[T][1], |
| 145 | messageId: string, |
| 146 | ): void {} |
| 147 | |
| 148 | async *streamRequest<T extends keyof FromWebviewProtocol>( |
| 149 | messageType: T, |
| 150 | data: FromWebviewProtocol[T][0], |
| 151 | cancelToken?: AbortSignal, |
| 152 | ): AsyncGenerator< |
| 153 | GeneratorYieldType<FromWebviewProtocol[T][1]>[], |
| 154 | GeneratorReturnType<FromWebviewProtocol[T][1]> | undefined |
| 155 | > { |
| 156 | return undefined; |
| 157 | } |
| 158 | } |