|
| 1 | +import * as vscode from "vscode" |
| 2 | +import { ClineProvider } from "../core/webview/ClineProvider" |
| 3 | +import { assert } from "../utils/util" |
| 4 | +import { PEARAI_CREATOR_MODE_WEBAPP_MANAGER_SLUG } from "../shared/modes" |
| 5 | + |
| 6 | +export const getPearaiExtension = async () => { |
| 7 | + const pearAiExtension = vscode.extensions.getExtension("pearai.pearai") |
| 8 | + |
| 9 | + assert(!!pearAiExtension, "PearAI extension not found") |
| 10 | + |
| 11 | + if (!pearAiExtension.isActive) { |
| 12 | + await pearAiExtension.activate() |
| 13 | + } |
| 14 | + |
| 15 | + return pearAiExtension |
| 16 | +} |
| 17 | + |
| 18 | +// TODO: TYPES |
| 19 | +export const getpearAIExports = async () => { |
| 20 | + const pearAiExtension = await getPearaiExtension() |
| 21 | + |
| 22 | + assert(!!pearAiExtension.exports, "⚠️⚠️ Error, no PearAI Exports could be found :( ⚠️⚠️"); |
| 23 | + |
| 24 | + return pearAiExtension.exports; |
| 25 | +} |
| 26 | + |
| 27 | +// TODO: SHOULD HAVE TYPE SYNCED WITH THE PEARAI SUBMODULE! |
| 28 | +type CreatorModeState = "OVERLAY_CLOSED" | "OVERLAY_OPEN" | "OVERLAY_CLOSED_CREATOR_ACTIVE" |
| 29 | + |
| 30 | +export const registerPearListener = async (provider: ClineProvider) => { |
| 31 | + // Getting the pear ai extension instance |
| 32 | + const exports = await getpearAIExports() |
| 33 | + |
| 34 | + exports.pearAPI.creatorMode.onDidRequestExecutePlan(async (msg: any) => { |
| 35 | + console.dir(`onDidRequestNewTask triggered with: ${JSON.stringify(msg)}`) |
| 36 | + |
| 37 | + let canContinue = false; |
| 38 | + |
| 39 | + while(!canContinue) { |
| 40 | + await new Promise((resolve) => setTimeout(resolve, 10)); |
| 41 | + canContinue = provider.viewLaunched && provider.isViewLaunched; |
| 42 | + } |
| 43 | + |
| 44 | + |
| 45 | + // Get the sidebar provider |
| 46 | + // Focus the sidebar first |
| 47 | + await vscode.commands.executeCommand("pearai-roo-cline.SidebarProvider.focus") |
| 48 | + |
| 49 | + // Wait for the view to be ready using a helper function |
| 50 | + await ensureViewIsReady(provider) |
| 51 | + // Wait a brief moment for UI to update |
| 52 | + await new Promise((resolve) => setTimeout(resolve, 3000)) |
| 53 | + |
| 54 | + // * This does actually work but the UI update does not happen. This method calls this.postStateToWebview() so not sure what is going on - James |
| 55 | + if(msg.newProjectType === "WEBAPP") { |
| 56 | + // Only switch to the creator manager if we're creating a new project |
| 57 | + // TODO: later when we need to make a different type of project, we need to change this |
| 58 | + await provider.handleModeSwitch(PEARAI_CREATOR_MODE_WEBAPP_MANAGER_SLUG); |
| 59 | + } |
| 60 | + |
| 61 | + // Clicl the chat btn |
| 62 | + await provider.postMessageToWebview({ type: "action", action: "chatButtonClicked" }) |
| 63 | + |
| 64 | + const creatorModeConfig = { |
| 65 | + creatorMode: true, |
| 66 | + newProjectType: msg.newProjectType, |
| 67 | + newProjectPath: msg.newProjectPath, |
| 68 | + } |
| 69 | + |
| 70 | + |
| 71 | + // Initialize with task |
| 72 | + await provider.initClineWithTask(msg.plan, undefined, undefined, undefined, creatorModeConfig); |
| 73 | + }); |
| 74 | + // If there's a creator event in the cache after the extensions were refreshed, we need to get it! |
| 75 | + exports.pearAPI.creatorMode.triggerCachedCreatorEvent(true); |
| 76 | + |
| 77 | + exports.pearAPI.creatorMode.onDidChangeCreatorModeState(async (state: CreatorModeState) => { |
| 78 | + // Get the sidebar provider |
| 79 | + const sidebarProvider = ClineProvider.getVisibleInstance(); |
| 80 | + |
| 81 | + if (sidebarProvider) { |
| 82 | + // Send a message to the webview that will trigger a window event |
| 83 | + sidebarProvider.postMessageToWebview({ |
| 84 | + type: "creatorModeUpdate", |
| 85 | + text: state, |
| 86 | + }); |
| 87 | + } |
| 88 | + }); |
| 89 | + |
| 90 | +} |
| 91 | + |
| 92 | +// TODO: decide if this is needed |
| 93 | +// Helper function to ensure the webview is ready |
| 94 | +async function ensureViewIsReady(provider: ClineProvider): Promise<void> { |
| 95 | + // If the view is already launched, we're good to go |
| 96 | + if (provider.viewLaunched) { |
| 97 | + return |
| 98 | + } |
| 99 | + |
| 100 | + // Otherwise, we need to wait for it to initialize |
| 101 | + return new Promise<void>((resolve) => { |
| 102 | + // Set up a one-time listener for when the view is ready |
| 103 | + const disposable = provider.on("clineCreated", () => { |
| 104 | + // Clean up the listener |
| 105 | + disposable.dispose() |
| 106 | + resolve() |
| 107 | + }) |
| 108 | + |
| 109 | + // Set a timeout just in case |
| 110 | + setTimeout(() => { |
| 111 | + disposable.dispose() |
| 112 | + resolve() |
| 113 | + }, 5000) |
| 114 | + }) |
| 115 | +} |
0 commit comments