forked from RooCodeInc/Roo-Code
-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathpearaiDeployWebappTool.ts
More file actions
151 lines (132 loc) · 5.54 KB
/
pearaiDeployWebappTool.ts
File metadata and controls
151 lines (132 loc) · 5.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import path from "path"
import fs from "fs/promises"
import { Cline } from "../Cline"
import { ToolUse, AskApproval, HandleError, PushToolResult, RemoveClosingTag } from "../../shared/tools"
import { formatResponse } from "../prompts/responses"
import { getReadablePath } from "../../utils/path"
import { isPathOutsideWorkspace } from "../../utils/pathUtils"
import { fileExistsAtPath } from "../../utils/fs"
import FormData from "form-data"
import fetch from "node-fetch"
import * as vscode from "vscode"
const SERVER_URL = "https://server.trypear.ai/pearai-server-api2"
export async function pearaiDeployWebappTool(
cline: Cline,
block: ToolUse,
askApproval: AskApproval,
handleError: HandleError,
pushToolResult: PushToolResult,
removeClosingTag: RemoveClosingTag,
) {
const zip_file_path: string | undefined = block.params.zip_file_path
const env_file_path: string | undefined = block.params.env_file_path
const site_id: string | undefined = block.params.site_id
const isStatic: boolean = block.params.isStatic === "true"
try {
if (block.partial) {
const partialMessage = JSON.stringify({
tool: "pearai_deploy_webapp",
zip_file_path: removeClosingTag("zip_file_path", zip_file_path),
env_file_path: removeClosingTag("env_file_path", env_file_path),
site_id: removeClosingTag("site_id", site_id),
isStatic: removeClosingTag("isStatic", isStatic?.toString()),
})
await cline.ask("tool", partialMessage, block.partial).catch(() => {})
return
}
// Validate required parameters
if (!zip_file_path) {
cline.consecutiveMistakeCount++
cline.recordToolError("pearai_deploy_webapp")
pushToolResult(await cline.sayAndCreateMissingParamError("pearai_deploy_webapp", "zip_file_path"))
return
}
if (!env_file_path) {
cline.consecutiveMistakeCount++
cline.recordToolError("pearai_deploy_webapp")
pushToolResult(await cline.sayAndCreateMissingParamError("pearai_deploy_webapp", "env_file_path"))
return
}
// Check if files exist
if (!fileExistsAtPath(zip_file_path)) {
cline.recordToolError("pearai_deploy_webapp")
pushToolResult(formatResponse.toolError(`Zip file not found at path: ${getReadablePath(zip_file_path)}`))
return
}
if (!fileExistsAtPath(env_file_path)) {
cline.recordToolError("pearai_deploy_webapp")
pushToolResult(formatResponse.toolError(`Environment file not found at path: ${getReadablePath(env_file_path)}`))
return
}
cline.consecutiveMistakeCount = 0
const toolMessage = JSON.stringify({
tool: "pearai_deploy_webapp",
zip_file_path,
env_file_path,
site_id,
isStatic,
})
const didApprove = await askApproval("tool", toolMessage)
if (!didApprove) {
return
}
// Read files
const zipContent = await fs.readFile(zip_file_path)
const envContent = await fs.readFile(env_file_path)
// Prepare form data
const form = new FormData()
form.append("zip_file", zipContent, {
filename: "dist.zip",
contentType: "application/zip",
})
form.append("env_file", envContent, {
filename: ".env",
contentType: "text/plain",
})
if (site_id) {
form.append("site_id", site_id)
}
if (isStatic) {
form.append("static", "true")
}
// Get auth token from extension context
const authToken = await vscode.commands.executeCommand("pearai-roo-cline.getPearAIApiKey")
if (!authToken) {
vscode.commands.executeCommand("pearai-roo-cline.PearAIKeysNotFound", undefined)
vscode.window.showErrorMessage("PearAI API key not found.", "Login to PearAI").then(async (selection) => {
if (selection === "Login to PearAI") {
const extensionUrl = `${vscode.env.uriScheme}://pearai.pearai/auth`
const callbackUri = await vscode.env.asExternalUri(vscode.Uri.parse(extensionUrl))
vscode.env.openExternal(
await vscode.env.asExternalUri(
vscode.Uri.parse(`https://trypear.ai/signin?callback=${callbackUri.toString()}`),
),
)
}
})
throw new Error("PearAI API key not found. Please login to PearAI.")
}
// Make POST request to deployment endpoint
const endpoint = site_id ? `${SERVER_URL}/redeploy-netlify` : `${SERVER_URL}/deploy-netlify`
const response = await fetch(endpoint, {
method: "POST",
headers: {
Authorization: `Bearer ${authToken}`,
},
body: form,
})
if (!response.ok) {
throw new Error(`Deployment failed with status ${response.status}: ${await response.text()}`)
}
const result = await response.text()
pushToolResult(
formatResponse.toolResult(
`Successfully deployed webapp${site_id ? ` to site ${site_id}` : " (new site)"}${isStatic ? " (static deployment)" : ""}\n\n${result}`
)
)
return
} catch (error) {
await handleError("deploying webapp", error)
return
}
}