-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathtools.ts
More file actions
63 lines (58 loc) · 1.8 KB
/
tools.ts
File metadata and controls
63 lines (58 loc) · 1.8 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
import OpenAI from "openai";
import { getGuideParams } from "./params.js";
import { z } from "zod";
export const getGuide = async ({
guideLink,
}: z.infer<typeof getGuideParams>) => {
console.log("Received request for guide:", guideLink);
try {
// Remove the base URL prefix and ensure the path starts correctly
const guidePath = guideLink.replace("https://docs.base.org", "");
const githubRawUrl = `https://raw.githubusercontent.com/base/web/refs/heads/master/apps/base-docs/docs/pages${guidePath}.mdx`;
console.log("Fetching from URL:", githubRawUrl);
const response = await fetch(githubRawUrl);
if (!response.ok) {
throw new Error(`Failed to fetch guide: ${response.statusText}`);
}
const guide = await response.text();
console.log("Successfully fetched guide content");
let finalResult = guide;
if (process.env.OPENAI_API_KEY) {
const client = new OpenAI();
// Process the guide content with GPT-4
console.log("Processing with ChatGPT...");
const result = await client.responses.create({
model: "gpt-4o-mini",
input: [
{
role: "developer",
content:
"convert this guide into a structured JSON of actions, including all steps and gotchas:\n\n" +
guide,
},
],
});
finalResult = result.output_text;
console.log("Successfully processed guide content");
}
return {
content: [
{
type: "text" as const,
text: finalResult,
},
],
};
} catch (err) {
const error = err as Error;
console.error("Error processing guide:", error.message);
return {
content: [
{
type: "text" as const,
text: `Error: ${error.message}`,
},
],
};
}
};