Skip to content

Commit

Permalink
🆕 add fetchWeb tool function
Browse files Browse the repository at this point in the history
  • Loading branch information
telesoho committed Sep 15, 2024
1 parent 403bd07 commit fc26013
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 5 deletions.
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -333,11 +333,12 @@
"dependencies": {
"@vscode/vscode-languagedetection": "^1.0.22",
"arch": "^2.2.0",
"axios": "^1.7.7",
"moment": "^2.22.1",
"openai": "^4.61.0",
"shelljs": "^0.8.5",
"turndown": "^7.1.2",
"xclip": "^1.0.5",
"openai": "^4.61.0"
"xclip": "^1.0.5"
},
"devDependencies": {
"@types/glob": "^7.1.3",
Expand Down
12 changes: 10 additions & 2 deletions src/ToolsManager.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ChatCompletionTool } from "openai/resources/chat/completions";
import Logger from "./Logger";
import { fetchWeb } from "./tool_functions";

type ToolFunction = (...args: any[]) => any;

Expand Down Expand Up @@ -35,6 +36,13 @@ export class ToolsManager {
required: ["city"],
}
);
this.registerTool("fetchWeb", fetchWeb, "fetch a web page content", {
type: "object",
properties: {
url: { type: "string", description: "The url of the web page" },
},
required: ["url"],
});
}

public registerTool(
Expand All @@ -46,11 +54,11 @@ export class ToolsManager {
this.tools.set(name, { func, description, parameters });
}

public executeTool(name: string, args: any): string | null {
public async executeTool(name: string, args: any): Promise<string> {
const toolInfo = this.tools.get(name);
if (toolInfo) {
try {
return JSON.stringify(toolInfo.func(args));
return JSON.stringify(await toolInfo.func(args));
} catch (error) {
Logger.log(`Error executing tool ${name}:`, error);
return null;
Expand Down
2 changes: 1 addition & 1 deletion src/ai_paster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export class AIPaster {
if (toolCalls) {
for (const toolCall of toolCalls) {
const functionName = toolCall.function.name;
const functionResponse = this.toolsManager.executeTool(
const functionResponse = await this.toolsManager.executeTool(
functionName,
JSON.parse(toolCall.function.arguments)
);
Expand Down
14 changes: 14 additions & 0 deletions src/tool_functions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import axios from "axios";

export async function fetchWeb({ url }: { url: string }): Promise<any> {
let content = "";
try {
const response = await axios.get(url);
content = response.data;
return { content };
} catch (e) {
return {
error: e,
};
}
}

0 comments on commit fc26013

Please sign in to comment.