Refactor exports

This commit is contained in:
Saoud Rizwan
2024-09-24 10:51:31 -04:00
parent a009c84597
commit 3466859536
4 changed files with 1 additions and 1 deletions

55
src/exports/README.md Normal file
View File

@@ -0,0 +1,55 @@
# Claude Dev API
The Claude Dev extension exposes an API that can be used by other extensions. To use this API in your extension:
1. Copy `src/extension-api/claude-dev.d.ts` to your extension's source directory.
2. Include `claude-dev.d.ts` in your extension's compilation.
3. Get access to the API with the following code:
```ts
const claudeDevExtension = vscode.extensions.getExtension<ClaudeDevAPI>("saoudrizwan.claude-dev")
if (!claudeDevExtension?.isActive) {
throw new Error("Claude Dev extension is not activated")
}
const claudeDev = claudeDevExtension.exports
if (claudeDev) {
// Now you can use the API
// Set custom instructions
await claudeDev.setCustomInstructions("Talk like a pirate")
// Get custom instructions
const instructions = await claudeDev.getCustomInstructions()
console.log("Current custom instructions:", instructions)
// Start a new task with an initial message
await claudeDev.startNewTask("Hello, Claude! Let's make a new project...")
// Start a new task with an initial message and images
await claudeDev.startNewTask("Use this design language", ["data:image/webp;base64,..."])
// Send a message to the current task
await claudeDev.sendMessage("Can you fix the @problems?")
// Simulate pressing the primary button in the chat interface (e.g. 'Save' or 'Proceed While Running')
await claudeDev.pressPrimaryButton()
// Simulate pressing the secondary button in the chat interface (e.g. 'Reject')
await claudeDev.pressSecondaryButton()
} else {
console.error("Claude Dev API is not available")
}
```
**Note:** To ensure that the `saoudrizwan.claude-dev` extension is activated before your extension, add it to the `extensionDependencies` in your `package.json`:
```json
"extensionDependencies": [
"saoudrizwan.claude-dev"
]
```
For detailed information on the available methods and their usage, refer to the `claude-dev.d.ts` file.

37
src/exports/claude-dev.d.ts vendored Normal file
View File

@@ -0,0 +1,37 @@
export interface ClaudeDevAPI {
/**
* Sets the custom instructions in the global storage.
* @param value The custom instructions to be saved.
*/
setCustomInstructions(value: string): Promise<void>
/**
* Retrieves the custom instructions from the global storage.
* @returns The saved custom instructions, or undefined if not set.
*/
getCustomInstructions(): Promise<string | undefined>
/**
* Starts a new task with an optional initial message and images.
* @param task Optional initial task message.
* @param images Optional array of image data URIs (e.g., "data:image/webp;base64,...").
*/
startNewTask(task?: string, images?: string[]): Promise<void>
/**
* Sends a message to the current task.
* @param message Optional message to send.
* @param images Optional array of image data URIs (e.g., "data:image/webp;base64,...").
*/
sendMessage(message?: string, images?: string[]): Promise<void>
/**
* Simulates pressing the primary button in the chat interface.
*/
pressPrimaryButton(): Promise<void>
/**
* Simulates pressing the secondary button in the chat interface.
*/
pressSecondaryButton(): Promise<void>
}

65
src/exports/index.ts Normal file
View File

@@ -0,0 +1,65 @@
import * as vscode from "vscode"
import { ClaudeDevProvider } from "../providers/ClaudeDevProvider"
import { ClaudeDevAPI } from "./claude-dev"
export function createClaudeDevAPI(
outputChannel: vscode.OutputChannel,
sidebarProvider: ClaudeDevProvider
): ClaudeDevAPI {
const api: ClaudeDevAPI = {
setCustomInstructions: async (value: string) => {
await sidebarProvider.updateCustomInstructions(value)
outputChannel.appendLine("Custom instructions set")
},
getCustomInstructions: async () => {
return (await sidebarProvider.getGlobalState("customInstructions")) as string | undefined
},
startNewTask: async (task?: string, images?: string[]) => {
outputChannel.appendLine("Starting new task")
await sidebarProvider.clearTask()
await sidebarProvider.postStateToWebview()
await sidebarProvider.postMessageToWebview({ type: "action", action: "chatButtonTapped" })
await sidebarProvider.postMessageToWebview({
type: "invoke",
invoke: "sendMessage",
text: task,
images: images,
})
outputChannel.appendLine(
`Task started with message: ${task ? `"${task}"` : "undefined"} and ${images?.length || 0} image(s)`
)
},
sendMessage: async (message?: string, images?: string[]) => {
outputChannel.appendLine(
`Sending message: ${message ? `"${message}"` : "undefined"} with ${images?.length || 0} image(s)`
)
await sidebarProvider.postMessageToWebview({
type: "invoke",
invoke: "sendMessage",
text: message,
images: images,
})
},
pressPrimaryButton: async () => {
outputChannel.appendLine("Pressing primary button")
await sidebarProvider.postMessageToWebview({
type: "invoke",
invoke: "primaryButtonClick",
})
},
pressSecondaryButton: async () => {
outputChannel.appendLine("Pressing secondary button")
await sidebarProvider.postMessageToWebview({
type: "invoke",
invoke: "secondaryButtonClick",
})
},
}
return api
}