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