Skip to content

Commit

Permalink
Feat: Allow sys tools to be added by URL (#483)
Browse files Browse the repository at this point in the history
* chore: clearn up unnecessary try/catch wrapper

* chore: update eslint to allow ignored caught errors

* feat: enable `sys.` tools to be added via url
  • Loading branch information
ryanhopperlowe authored Sep 10, 2024
1 parent e6f2a4a commit 18eaca7
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 13 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@
"react/react-in-jsx-scope": "off",
"react/prop-types": "off",
"@typescript-eslint/no-explicit-any": "off",
"@typescript-eslint/no-unused-vars": ["error", { "argsIgnorePattern": "^_", "varsIgnorePattern": "^_"}]
"@typescript-eslint/no-unused-vars": ["error", { "argsIgnorePattern": "^_", "varsIgnorePattern": "^_", "caughtErrorsIgnorePattern": "^_" }]
}
}
17 changes: 15 additions & 2 deletions actions/gptscript.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,21 @@ export const parseContent = async (toolContent: string): Promise<Tool[]> => {
) as Tool[];
};

export const parseBlock = async (ref: string) => {
return await gpt().parse(ref);
/**
* Verifies that a tool exists by parsing it.
* @param toolRef The tool reference to verify.
* @returns A boolean indicating whether the tool exists.
*/
export const verifyToolExists = async (toolRef: string) => {
// skip verification if the tool is a system tool
if (toolRef.startsWith('sys.')) return true;

try {
await gpt().parse(toolRef);
return true;
} catch (_) {
return false;
}
};

export const parse = async (file: string): Promise<Tool[]> => {
Expand Down
6 changes: 1 addition & 5 deletions actions/scripts/fetch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,7 @@ export const path = async (file: string): Promise<string> => {
export const fetchFullScript = async (file: string): Promise<Block[]> => {
if (!external(file)) file = `${SCRIPTS_PATH()}/${file}.gpt`;

try {
return await gpt().parse(file);
} catch (e) {
throw e;
}
return await gpt().parse(file);
};

export const fetchScript = async (file: string): Promise<Tool> => {
Expand Down
6 changes: 3 additions & 3 deletions components/chat/chatBar/commands.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getToolDisplayName, parseBlock } from '@/actions/gptscript';
import { getToolDisplayName, verifyToolExists } from '@/actions/gptscript';
import { ingest } from '@/actions/knowledge/knowledge';
import { gatewayTool, getCookie } from '@/actions/knowledge/util';
import { uploadFile } from '@/actions/upload';
Expand Down Expand Up @@ -235,9 +235,9 @@ export default forwardRef<ChatCommandsRef, CommandsProps>(
if (tools.includes(tool)) throw new Error('Tool already added');

setLoadingTool(tool);
const [foundTool] = await parseBlock(tool);

if (!foundTool) {
const toolExists = await verifyToolExists(tool);
if (!toolExists) {
throw new Error(`Tool ${tool} does not exist`);
}

Expand Down
7 changes: 5 additions & 2 deletions components/edit/configure/imports.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import PropTypes from 'prop-types';
import { useContext, useEffect, useRef, useState } from 'react';
import { GoGlobe, GoPencil, GoTools, GoTrash } from 'react-icons/go';

import { getToolDisplayName, parse } from '@/actions/gptscript';
import { getToolDisplayName, verifyToolExists } from '@/actions/gptscript';
import {
CatalogListBox,
ToolCatalogRef,
Expand Down Expand Up @@ -81,7 +81,10 @@ const Imports: React.FC<ImportsProps> = ({

const verifyAndAddToolUrl = useAsync(async (url: string) => {
if (!url) throw new Error('Tool URL cannot be empty');
await parse(url); // throws if the url is invalid

const toolExists = await verifyToolExists(url);
if (!toolExists) throw new Error(`Tool ${url} does not exist`);

setTools([...(tools || []), url]);
});

Expand Down

0 comments on commit 18eaca7

Please sign in to comment.