api.ts 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import { EventEmitter } from "events"
  2. import * as vscode from "vscode"
  3. import { ClineProvider } from "../core/webview/ClineProvider"
  4. import { RooCodeAPI, RooCodeEvents, ConfigurationValues } from "./roo-code"
  5. import { MessageHistory } from "./message-history"
  6. export class API extends EventEmitter<RooCodeEvents> implements RooCodeAPI {
  7. private readonly outputChannel: vscode.OutputChannel
  8. private readonly provider: ClineProvider
  9. private readonly history: MessageHistory
  10. constructor(outputChannel: vscode.OutputChannel, provider: ClineProvider) {
  11. super()
  12. this.outputChannel = outputChannel
  13. this.provider = provider
  14. this.history = new MessageHistory()
  15. this.on("message", ({ taskId, action, message }) => {
  16. // if (message.type === "say") {
  17. // console.log("message", { taskId, action, message })
  18. // }
  19. if (action === "created") {
  20. this.history.add(taskId, message)
  21. } else if (action === "updated") {
  22. this.history.update(taskId, message)
  23. }
  24. })
  25. }
  26. public async startNewTask(text?: string, images?: string[]) {
  27. await this.provider.removeClineFromStack()
  28. await this.provider.postStateToWebview()
  29. await this.provider.postMessageToWebview({ type: "action", action: "chatButtonClicked" })
  30. await this.provider.postMessageToWebview({ type: "invoke", invoke: "newChat", text, images })
  31. const cline = await this.provider.initClineWithTask(text, images)
  32. cline.on("message", (message) => this.emit("message", { taskId: cline.taskId, ...message }))
  33. cline.on("taskStarted", () => this.emit("taskStarted", cline.taskId))
  34. cline.on("taskPaused", () => this.emit("taskPaused", cline.taskId))
  35. cline.on("taskUnpaused", () => this.emit("taskUnpaused", cline.taskId))
  36. cline.on("taskAborted", () => this.emit("taskAborted", cline.taskId))
  37. cline.on("taskSpawned", (taskId) => this.emit("taskSpawned", cline.taskId, taskId))
  38. return cline.taskId
  39. }
  40. public async clearCurrentTask(lastMessage?: string) {
  41. await this.provider.finishSubTask(lastMessage)
  42. }
  43. public async cancelCurrentTask() {
  44. await this.provider.cancelTask()
  45. }
  46. public async sendMessage(text?: string, images?: string[]) {
  47. await this.provider.postMessageToWebview({ type: "invoke", invoke: "sendMessage", text, images })
  48. }
  49. public async pressPrimaryButton() {
  50. await this.provider.postMessageToWebview({ type: "invoke", invoke: "primaryButtonClick" })
  51. }
  52. public async pressSecondaryButton() {
  53. await this.provider.postMessageToWebview({ type: "invoke", invoke: "secondaryButtonClick" })
  54. }
  55. // TODO: Change this to `setApiConfiguration`.
  56. public async setConfiguration(values: Partial<ConfigurationValues>) {
  57. await this.provider.setValues(values)
  58. }
  59. public isReady() {
  60. return this.provider.viewLaunched
  61. }
  62. public getMessages(taskId: string) {
  63. return this.history.getMessages(taskId)
  64. }
  65. }