Skip to content

Commit 25b7eca

Browse files
Add API Gateway schema file handling and improve logging in main process
1 parent 3a8a0ab commit 25b7eca

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

main.ts

+9-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import {parseArgs} from "jsr:@std/cli/parse-args"
33
import {runHelp} from "./help.ts"
44
import {handleConfig} from "./config.ts"
5-
import {getExamplesFile, getFiles, getTaskFile} from "./utils.ts"
5+
import {getApiGatewaySchemaFile, getExamplesFile, getFiles, getTaskFile} from "./utils.ts"
66
import {determineClient, listModels} from "./aiClients.ts"
77
import OpenAI from "openai/mod.ts"
88
import {generateCurl, generatePrompt} from "./prompt.ts"
@@ -111,16 +111,17 @@ if (import.meta.main) {
111111

112112
const taskContent = getTaskFile(task)
113113
const filesContent = getFiles(files)
114+
const apiGatewaySchemaContent = getApiGatewaySchemaFile(apiGatewaySchema)
114115
const examplesContent = getExamplesFile(examples)
115116
if (globalThis.isVerbose) {
116-
console.log({taskContent, files, filesContent, examplesContent, executeCommands})
117+
console.log({taskContent, files, filesContent, examplesContent, apiGatewaySchemaContent, executeCommands})
117118
}
118119
if (!taskContent || !filesContent) {
119120
console.error("Task and files are required")
120121
Deno.exit(1)
121122
}
122123
await generateCurls(model, taskContent, filesContent,
123-
apiGatewaySchema, apiKey, endpoint, examplesContent, requiresLogin, executeCommands)
124+
apiGatewaySchemaContent, apiKey, endpoint, examplesContent, requiresLogin, executeCommands)
124125

125126
// Add this line to print the results
126127
printCurlResults()
@@ -243,6 +244,11 @@ async function generateCurlsWithAnthropic(client: Anthropic, model: string, prom
243244
} else if (message.type === "tool_use") {
244245
const commands = message.input && typeof message.input === 'object' ? (message.input as {commands?: Array<{command: string, explanation: string, expected_success: boolean}>}).commands || [] : []
245246
toolUseId = message.id
247+
if (!Array.isArray(commands)) {
248+
console.error("Commands should be an array")
249+
console.log({commands, input: message.input, type: typeof message.input})
250+
Deno.exit(1)
251+
}
246252
const transformedCommands = commands.map(cmd => ({command: cmd.command, expected_success: cmd.expected_success}))
247253
const response = await runCurlsAndReturnResult(transformedCommands, endpoint, apiKey, executeCommands)
248254
if (globalThis.isVerbose) {

utils.ts

+22
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,25 @@ export function getFiles(files: string[]) {
8585
}
8686
return filesContent.join("\n")
8787
}
88+
89+
export function getApiGatewaySchemaFile(schema: string) {
90+
91+
const readPermission = Deno.permissions.querySync({name: "read", path: schema})
92+
if (readPermission.state === 'prompt') {
93+
const readRequest = Deno.permissions.requestSync({
94+
name: "read",
95+
path: schema,
96+
})
97+
if (readRequest.state === 'granted') {
98+
return Deno.readTextFileSync(schema)
99+
} else {
100+
console.error("Permission denied")
101+
Deno.exit(1)
102+
}
103+
} else if (readPermission.state === "granted") {
104+
return Deno.readTextFileSync(schema)
105+
} else {
106+
return schema
107+
}
108+
109+
}

0 commit comments

Comments
 (0)