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IACH,OAAO,CAAC,gBAAgB;IAwBxB;;OAEG;IACH,OAAO,CAAC,qBAAqB;IAI7B;;OAEG;IACH,OAAO,CAAC,iBAAiB;CAS5B","file":"copilot-extension-service.d.ts","sourcesContent":["import { Injectable } from '@angular/core';\r\nimport { catchError, map, Observable } from 'rxjs';\r\nimport { of } from 'rxjs/internal/observable/of';\r\nimport { AppContext } from '../data/app-context';\r\nimport { Logging } from '../diagnostics/logging';\r\nimport { RpcObservableCopilotClient } from '../rpc/copilot/rpc-observable-copilot-client';\r\nimport { RpcObservableCopilotConfigServer } from '../rpc/copilot/rpc-observable-copilot-config-server';\r\nimport { RpcObservableCopilotError, RpcObservableCopilotRequest, RpcObservableCopilotRequestType, RpcObservableCopilotResult } from '../rpc/copilot/rpc-observable-copilot-model';\r\n\r\n@Injectable({\r\n    providedIn: 'root'\r\n})\r\nexport class CopilotExtensionService {\r\n    /**\r\n     * The RpcObservableCopilotConfigServer class for sending.\r\n     */\r\n    public rpcObservableCopilotClient: RpcObservableCopilotClient;\r\n\r\n    /**\r\n     * The RpcObservableCopilotConfigServer classf for receiving.\r\n     */\r\n    public rpcObservableCopilotConfigServer: RpcObservableCopilotConfigServer;\r\n\r\n    /**\r\n     * Request handlers for different types of requests.\r\n     */\r\n    private requestHandlers = {\r\n        [RpcObservableCopilotRequestType.RouteNavigation]: () => this.handleRouteNavigation(),\r\n        [RpcObservableCopilotRequestType.ThemeChange]: (request?: RpcObservableCopilotRequest) => this.handleThemeChange(request)\r\n    };\r\n\r\n    private readonly serviceName = 'CopilotExtensionService';\r\n\r\n    private copilotResultMessage = {\r\n        success: 'Data sent to shell successfully.',\r\n        error: 'Error occurred while sending data to shell.',\r\n        notInitialized: 'The rpcObservableCopilotClient is not initialized.',\r\n        rpcNotProvided: 'The rpc object is not provided.',\r\n        handlerNotFound: 'No handler found for request type: '\r\n    };\r\n\r\n    constructor(protected appContext: AppContext) {\r\n        if (!appContext?.rpc) {\r\n            Logging.logError(this.serviceName, this.copilotResultMessage.rpcNotProvided);\r\n            return;\r\n        }\r\n\r\n        this.rpcObservableCopilotClient = new RpcObservableCopilotClient(appContext.rpc);\r\n        this.rpcObservableCopilotConfigServer = new RpcObservableCopilotConfigServer(appContext.rpc);\r\n    }\r\n\r\n    /**\r\n     * Register custom handlers for different types of requests.\r\n     * @param customHandlers Custom handlers for different types of requests.\r\n     */\r\n    public registerRpcHandler(customHandlers = {}) {\r\n        const handlers = { ...this.requestHandlers, ...customHandlers };\r\n\r\n        this.rpcObservableCopilotConfigServer.register(request => {\r\n            const handler = handlers[request.type];\r\n\r\n            if (!handler) {\r\n                Logging.logError(this.serviceName, `${this.copilotResultMessage.handlerNotFound}${request.type}`);\r\n                return of(null);\r\n            }\r\n\r\n            return handler(request);\r\n        });\r\n    }\r\n\r\n    /**\r\n     * Send to shell by rpc and then request shell to send prompt from extension to copilot.\r\n     * @param prompt the prompt to send to copilot\r\n     *\r\n     */\r\n    public sendToShellSendingPromptToCopilot(prompt: string): Observable<RpcObservableCopilotResult | RpcObservableCopilotError> {\r\n        const config = {\r\n            type: RpcObservableCopilotRequestType.RequestToSendToShellSendingPromptToCopilot,\r\n            copilotChatRequest: {\r\n                prompt\r\n            }\r\n        };\r\n        return this.sendToShellByRpc(config);\r\n    }\r\n\r\n    /**\r\n     * Send to shell by rpc to request sending copilot response to extension.\r\n     * @param response the response to send to extension.\r\n     * @param targetExtension the target extension to send the response.\r\n     */\r\n    public sendToShellSendingCopilotResponseToExtension(response: string, targetExtension: string): Observable<RpcObservableCopilotResult | RpcObservableCopilotError> {\r\n        const config = {\r\n            type: RpcObservableCopilotRequestType.RequestToSendToExtensionSendingCopilotResponse,\r\n            copilotChatResponse: {\r\n                response,\r\n                targetExtension\r\n            }\r\n        };\r\n\r\n        return this.sendToShellByRpc(config);\r\n    }\r\n\r\n    /**\r\n     * Send to shell by rpc to request for theme update.\r\n     */\r\n     public sendToShellUpdateTheme(): Observable<RpcObservableCopilotResult | RpcObservableCopilotError> {\r\n        const config = {\r\n            type: RpcObservableCopilotRequestType.RequestThemeChange,\r\n            themeChange: {\r\n                current: MsftSme.self().Resources.theme === 'dark' ? 'light' : 'dark',\r\n                previous: MsftSme.self().Resources.theme\r\n            }\r\n        };\r\n\r\n        return this.sendToShellByRpc(config);\r\n    }\r\n\r\n    /**\r\n     * Send to shell by rpc to request url\r\n     */\r\n    public sendToShellRequestUrl(): Observable<RpcObservableCopilotResult | RpcObservableCopilotError> {\r\n        const config = {\r\n            type: RpcObservableCopilotRequestType.RequestRouteNavigation\r\n        };\r\n\r\n        return this.sendToShellByRpc(config);\r\n    }\r\n\r\n    /**\r\n     * Send to shell by rpc to request tool\r\n     */\r\n    public sendToShellRequestTool(): Observable<RpcObservableCopilotResult | RpcObservableCopilotError> {\r\n        const config = {\r\n            type: RpcObservableCopilotRequestType.RequestToolChange\r\n        };\r\n\r\n        return this.sendToShellByRpc(config);\r\n    }\r\n\r\n    /**\r\n     * Send to shell by rpc to request active node.\r\n     */\r\n    public sendToShellRequestActiveNodeName(): Observable<RpcObservableCopilotResult | RpcObservableCopilotError> {\r\n        const config = {\r\n            type: RpcObservableCopilotRequestType.RequestActiveNodeName\r\n        };\r\n\r\n        return this.sendToShellByRpc(config);\r\n    }\r\n\r\n    /**\r\n     * Send an RPC request to the specified target with the provided configuration.\r\n     *\r\n     * @param config The configuration object to send with the RPC request.\r\n     */\r\n    private sendToShellByRpc(config: any): Observable<RpcObservableCopilotResult | RpcObservableCopilotError> {\r\n        if (!this.rpcObservableCopilotClient) {\r\n            return of(<RpcObservableCopilotError>{\r\n                error: new Error(this.copilotResultMessage.notInitialized),\r\n                message: this.copilotResultMessage.notInitialized\r\n            });\r\n        }\r\n\r\n        return this.rpcObservableCopilotClient.log(config).pipe(\r\n            map(result => {\r\n                return <RpcObservableCopilotResult>{\r\n                    ...result,\r\n                    message: this.copilotResultMessage.success\r\n                };\r\n            }),\r\n            catchError((error) => {\r\n                return of(<RpcObservableCopilotError>{\r\n                    error: error,\r\n                    message: this.copilotResultMessage.error\r\n                });\r\n            })\r\n        );\r\n    }\r\n\r\n    /**\r\n     * Default handler for navigation request.\r\n     */\r\n    private handleRouteNavigation(): Observable<void> {\r\n        return of(null);\r\n    }\r\n\r\n    /**\r\n     * Default handler for theme update request.\r\n     */\r\n    private handleThemeChange(request?: RpcObservableCopilotRequest): Observable<void> {\r\n        const theme = request?.themeChange?.current;\r\n        if (theme && MsftSme.self().Resources.theme !== theme) {\r\n            MsftSme.self().Resources.theme = theme;\r\n            document.body.classList.remove(`sme-theme-${theme === 'dark' ? 'light' : 'dark'}`);\r\n            document.body.classList.add(`sme-theme-${theme}`);\r\n        }\r\n        return of(null);\r\n    }\r\n}\r\n"]}