Browse Source

Forward events from all running Cline instances to the exposed API

Wojciech Kordalski 10 months ago
parent
commit
67f6dab782
2 changed files with 19 additions and 8 deletions
  1. 10 1
      src/core/webview/ClineProvider.ts
  2. 9 7
      src/exports/api.ts

+ 10 - 1
src/core/webview/ClineProvider.ts

@@ -1,6 +1,7 @@
 import { Anthropic } from "@anthropic-ai/sdk"
 import { Anthropic } from "@anthropic-ai/sdk"
 import delay from "delay"
 import delay from "delay"
 import axios from "axios"
 import axios from "axios"
+import EventEmitter from "events"
 import fs from "fs/promises"
 import fs from "fs/promises"
 import os from "os"
 import os from "os"
 import pWaitFor from "p-wait-for"
 import pWaitFor from "p-wait-for"
@@ -67,7 +68,11 @@ import { TelemetrySetting } from "../../shared/TelemetrySetting"
  * https://github.com/KumarVariable/vscode-extension-sidebar-html/blob/master/src/customSidebarViewProvider.ts
  * https://github.com/KumarVariable/vscode-extension-sidebar-html/blob/master/src/customSidebarViewProvider.ts
  */
  */
 
 
-export class ClineProvider implements vscode.WebviewViewProvider {
+export type ClineProviderEvents = {
+	clineAdded: [cline: Cline]
+}
+
+export class ClineProvider extends EventEmitter<ClineProviderEvents> implements vscode.WebviewViewProvider {
 	public static readonly sideBarId = "roo-cline.SidebarProvider" // used in package.json as the view's id. This value cannot be changed due to how vscode caches views based on their id, and updating the id would break existing instances of the extension.
 	public static readonly sideBarId = "roo-cline.SidebarProvider" // used in package.json as the view's id. This value cannot be changed due to how vscode caches views based on their id, and updating the id would break existing instances of the extension.
 	public static readonly tabPanelId = "roo-cline.TabPanelProvider"
 	public static readonly tabPanelId = "roo-cline.TabPanelProvider"
 	private static activeInstances: Set<ClineProvider> = new Set()
 	private static activeInstances: Set<ClineProvider> = new Set()
@@ -86,6 +91,8 @@ export class ClineProvider implements vscode.WebviewViewProvider {
 		readonly context: vscode.ExtensionContext,
 		readonly context: vscode.ExtensionContext,
 		private readonly outputChannel: vscode.OutputChannel,
 		private readonly outputChannel: vscode.OutputChannel,
 	) {
 	) {
+		super()
+
 		this.outputChannel.appendLine("ClineProvider instantiated")
 		this.outputChannel.appendLine("ClineProvider instantiated")
 		this.contextProxy = new ContextProxy(context)
 		this.contextProxy = new ContextProxy(context)
 		ClineProvider.activeInstances.add(this)
 		ClineProvider.activeInstances.add(this)
@@ -118,6 +125,8 @@ export class ClineProvider implements vscode.WebviewViewProvider {
 		// Add this cline instance into the stack that represents the order of all the called tasks.
 		// Add this cline instance into the stack that represents the order of all the called tasks.
 		this.clineStack.push(cline)
 		this.clineStack.push(cline)
 
 
+		this.emit("clineAdded", cline)
+
 		// Ensure getState() resolves correctly.
 		// Ensure getState() resolves correctly.
 		const state = await this.getState()
 		const state = await this.getState()
 
 

+ 9 - 7
src/exports/api.ts

@@ -18,6 +18,15 @@ export class API extends EventEmitter<RooCodeEvents> implements RooCodeAPI {
 		this.provider = provider
 		this.provider = provider
 		this.history = new MessageHistory()
 		this.history = new MessageHistory()
 
 
+		this.provider.on("clineAdded", (cline) => {
+			cline.on("message", (message) => this.emit("message", { taskId: cline.taskId, ...message }))
+			cline.on("taskStarted", () => this.emit("taskStarted", cline.taskId))
+			cline.on("taskPaused", () => this.emit("taskPaused", cline.taskId))
+			cline.on("taskUnpaused", () => this.emit("taskUnpaused", cline.taskId))
+			cline.on("taskAborted", () => this.emit("taskAborted", cline.taskId))
+			cline.on("taskSpawned", (taskId) => this.emit("taskSpawned", cline.taskId, taskId))
+		})
+
 		this.on("message", ({ taskId, action, message }) => {
 		this.on("message", ({ taskId, action, message }) => {
 			// if (message.type === "say") {
 			// if (message.type === "say") {
 			// 	console.log("message", { taskId, action, message })
 			// 	console.log("message", { taskId, action, message })
@@ -38,13 +47,6 @@ export class API extends EventEmitter<RooCodeEvents> implements RooCodeAPI {
 		await this.provider.postMessageToWebview({ type: "invoke", invoke: "newChat", text, images })
 		await this.provider.postMessageToWebview({ type: "invoke", invoke: "newChat", text, images })
 
 
 		const cline = await this.provider.initClineWithTask(text, images)
 		const cline = await this.provider.initClineWithTask(text, images)
-		cline.on("message", (message) => this.emit("message", { taskId: cline.taskId, ...message }))
-		cline.on("taskStarted", () => this.emit("taskStarted", cline.taskId))
-		cline.on("taskPaused", () => this.emit("taskPaused", cline.taskId))
-		cline.on("taskUnpaused", () => this.emit("taskUnpaused", cline.taskId))
-		cline.on("taskAborted", () => this.emit("taskAborted", cline.taskId))
-		cline.on("taskSpawned", (taskId) => this.emit("taskSpawned", cline.taskId, taskId))
-
 		return cline.taskId
 		return cline.taskId
 	}
 	}