Skip to content

Commit fb757c5

Browse files
committed
Merge branch 'acorn/creator-rebase' into feat/creator-mode
2 parents 49fce51 + bfa6666 commit fb757c5

21 files changed

Lines changed: 752 additions & 214 deletions

File tree

src/activate/index.ts

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

src/api/providers/anthropic.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,18 @@ export class AnthropicHandler extends BaseProvider implements SingleCompletionHa
105105
case "claude-3-opus-20240229":
106106
case "claude-3-haiku-20240307":
107107
betas.push("prompt-caching-2024-07-31")
108+
// Include prompt_key if newProjectType is set
108109
return {
109-
headers: { "anthropic-beta": betas.join(",") },
110-
authorization: `Bearer ${this.options.apiKey}`,
110+
headers: {
111+
"anthropic-beta": betas.join(","),
112+
prompt_key: this.options.creatorModeConfig?.newProjectType
113+
? String(this.options.creatorModeConfig.newProjectType)
114+
: undefined,
115+
project_path: this.options.creatorModeConfig?.newProjectPath
116+
? String(this.options.creatorModeConfig.newProjectPath)
117+
: undefined,
118+
authorization: `Bearer ${this.options.apiKey}`,
119+
},
111120
}
112121
default:
113122
return undefined

src/core/Cline.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { serializeError } from "serialize-error"
1111
import * as vscode from "vscode"
1212

1313
// schemas
14-
import { TokenUsage, ToolUsage, ToolName, ModelInfo } from "../schemas"
14+
import { TokenUsage, ToolUsage, ToolName, ModelInfo, CreatorModeConfig } from "../schemas"
1515

1616
// api
1717
import { ApiHandler, buildApiHandler } from "../api"
@@ -127,6 +127,7 @@ export type ClineOptions = {
127127
taskNumber?: number
128128
onCreated?: (cline: Cline) => void
129129
pearaiModels?: Record<string, ModelInfo>
130+
creatorModeConfig?: CreatorModeConfig
130131
}
131132

132133
export class Cline extends EventEmitter<ClineEvents> {
@@ -142,6 +143,7 @@ export class Cline extends EventEmitter<ClineEvents> {
142143
pausedModeSlug: string = defaultModeSlug
143144
private pauseInterval: NodeJS.Timeout | undefined
144145

146+
public creatorModeConfig: CreatorModeConfig
145147
readonly apiConfiguration: ApiConfiguration
146148
api: ApiHandler
147149
private promptCacheKey: string
@@ -220,6 +222,7 @@ export class Cline extends EventEmitter<ClineEvents> {
220222
parentTask,
221223
taskNumber = -1,
222224
onCreated,
225+
creatorModeConfig,
223226
}: ClineOptions) {
224227
super()
225228

@@ -257,6 +260,8 @@ export class Cline extends EventEmitter<ClineEvents> {
257260
this.diffViewProvider = new DiffViewProvider(this.cwd)
258261
this.enableCheckpoints = enableCheckpoints
259262

263+
this.creatorModeConfig = creatorModeConfig ?? historyItem?.creatorModeConfig ?? { creatorMode: false }
264+
260265
this.rootTask = rootTask
261266
this.parentTask = parentTask
262267
this.taskNumber = taskNumber
@@ -371,6 +376,7 @@ export class Cline extends EventEmitter<ClineEvents> {
371376
taskNumber: this.taskNumber,
372377
globalStoragePath: this.globalStoragePath,
373378
workspace: this.cwd,
379+
creatorModeConfig: this.creatorModeConfig,
374380
})
375381

376382
this.emit("taskTokenUsageUpdated", this.taskId, tokenUsage)

src/core/task-persistence/taskMetadata.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ export type TaskMetadataOptions = {
1717
taskNumber: number
1818
globalStoragePath: string
1919
workspace: string
20+
creatorModeConfig?: {
21+
creatorMode?: boolean
22+
newProjectType?: string
23+
newProjectPath?: string
24+
}
2025
}
2126

2227
export async function taskMetadata({
@@ -25,6 +30,7 @@ export async function taskMetadata({
2530
taskNumber,
2631
globalStoragePath,
2732
workspace,
33+
creatorModeConfig,
2834
}: TaskMetadataOptions) {
2935
const taskDir = await getTaskDirectoryPath(globalStoragePath, taskId)
3036
const taskMessage = messages[0] // First message is always the task say.
@@ -57,6 +63,7 @@ export async function taskMetadata({
5763
totalCost: tokenUsage.totalCost,
5864
size: taskDirSize,
5965
workspace,
66+
creatorModeConfig,
6067
}
6168

6269
return { historyItem, tokenUsage }

0 commit comments

Comments
 (0)