|
| 1 | +import Hanzo from 'hanzoai'; |
| 2 | +import { Endpoint } from './tools'; |
| 3 | +import { zodToJsonSchema } from 'zod-to-json-schema'; |
| 4 | +import { z } from 'zod'; |
| 5 | +import { Cabidela } from '@cloudflare/cabidela'; |
| 6 | + |
| 7 | +function zodToInputSchema(schema: z.ZodSchema) { |
| 8 | + return { |
| 9 | + type: 'object' as const, |
| 10 | + ...(zodToJsonSchema(schema) as any), |
| 11 | + }; |
| 12 | +} |
| 13 | + |
| 14 | +/** |
| 15 | + * A list of tools that expose all the endpoints in the API dynamically. |
| 16 | + * |
| 17 | + * Instead of exposing every endpoint as it's own tool, which uses up too many tokens for LLMs to use at once, |
| 18 | + * we expose a single tool that can be used to search for endpoints by name, resource, operation, or tag, and then |
| 19 | + * a generic endpoint that can be used to invoke any endpoint with the provided arguments. |
| 20 | + * |
| 21 | + * @param endpoints - The endpoints to include in the list. |
| 22 | + */ |
| 23 | +export function dynamicTools(endpoints: Endpoint[]): Endpoint[] { |
| 24 | + const listEndpointsSchema = z.object({ |
| 25 | + search_query: z |
| 26 | + .string() |
| 27 | + .optional() |
| 28 | + .describe( |
| 29 | + 'An optional search query to filter the endpoints by. Provide a partial name, resource, operation, or tag to filter the endpoints returned.', |
| 30 | + ), |
| 31 | + }); |
| 32 | + |
| 33 | + const listEndpointsTool = { |
| 34 | + metadata: { |
| 35 | + resource: 'dynamic_tools', |
| 36 | + operation: 'read' as const, |
| 37 | + tags: [], |
| 38 | + }, |
| 39 | + tool: { |
| 40 | + name: 'list_api_endpoints', |
| 41 | + description: 'List or search for all endpoints in the Hanzo TypeScript API', |
| 42 | + inputSchema: zodToInputSchema(listEndpointsSchema), |
| 43 | + }, |
| 44 | + handler: async (client: Hanzo, args: Record<string, unknown> | undefined) => { |
| 45 | + const query = args && listEndpointsSchema.parse(args).search_query?.trim(); |
| 46 | + |
| 47 | + const filteredEndpoints = |
| 48 | + query && query.length > 0 ? |
| 49 | + endpoints.filter((endpoint) => { |
| 50 | + const fieldsToMatch = [ |
| 51 | + endpoint.tool.name, |
| 52 | + endpoint.metadata.resource, |
| 53 | + endpoint.metadata.operation, |
| 54 | + ...endpoint.metadata.tags, |
| 55 | + ]; |
| 56 | + return fieldsToMatch.some((field) => field.toLowerCase().includes(query.toLowerCase())); |
| 57 | + }) |
| 58 | + : endpoints; |
| 59 | + |
| 60 | + return { |
| 61 | + tools: filteredEndpoints.map(({ tool, metadata }) => ({ |
| 62 | + name: tool.name, |
| 63 | + description: tool.description, |
| 64 | + resource: metadata.resource, |
| 65 | + operation: metadata.operation, |
| 66 | + tags: metadata.tags, |
| 67 | + })), |
| 68 | + }; |
| 69 | + }, |
| 70 | + }; |
| 71 | + |
| 72 | + const getEndpointSchema = z.object({ |
| 73 | + endpoint: z.string().describe('The name of the endpoint to get the schema for.'), |
| 74 | + }); |
| 75 | + const getEndpointTool = { |
| 76 | + metadata: { |
| 77 | + resource: 'dynamic_tools', |
| 78 | + operation: 'read' as const, |
| 79 | + tags: [], |
| 80 | + }, |
| 81 | + tool: { |
| 82 | + name: 'get_api_endpoint_schema', |
| 83 | + description: |
| 84 | + 'Get the schema for an endpoint in the Hanzo TypeScript API. You can use the schema returned by this tool to invoke an endpoint with the `invoke_api_endpoint` tool.', |
| 85 | + inputSchema: zodToInputSchema(getEndpointSchema), |
| 86 | + }, |
| 87 | + handler: async (client: Hanzo, args: Record<string, unknown> | undefined) => { |
| 88 | + if (!args) { |
| 89 | + throw new Error('No endpoint provided'); |
| 90 | + } |
| 91 | + const endpointName = getEndpointSchema.parse(args).endpoint; |
| 92 | + |
| 93 | + const endpoint = endpoints.find((e) => e.tool.name === endpointName); |
| 94 | + if (!endpoint) { |
| 95 | + throw new Error(`Endpoint ${endpointName} not found`); |
| 96 | + } |
| 97 | + return endpoint.tool; |
| 98 | + }, |
| 99 | + }; |
| 100 | + |
| 101 | + const invokeEndpointSchema = z.object({ |
| 102 | + endpoint_name: z.string().describe('The name of the endpoint to invoke.'), |
| 103 | + args: z |
| 104 | + .record(z.string(), z.any()) |
| 105 | + .describe( |
| 106 | + 'The arguments to pass to the endpoint. This must match the schema returned by the `get_api_endpoint_schema` tool.', |
| 107 | + ), |
| 108 | + }); |
| 109 | + |
| 110 | + const invokeEndpointTool = { |
| 111 | + metadata: { |
| 112 | + resource: 'dynamic_tools', |
| 113 | + operation: 'write' as const, |
| 114 | + tags: [], |
| 115 | + }, |
| 116 | + tool: { |
| 117 | + name: 'invoke_api_endpoint', |
| 118 | + description: |
| 119 | + 'Invoke an endpoint in the Hanzo TypeScript API. Note: use the `list_api_endpoints` tool to get the list of endpoints and `get_api_endpoint_schema` tool to get the schema for an endpoint.', |
| 120 | + inputSchema: zodToInputSchema(invokeEndpointSchema), |
| 121 | + }, |
| 122 | + handler: async (client: Hanzo, args: Record<string, unknown> | undefined) => { |
| 123 | + if (!args) { |
| 124 | + throw new Error('No endpoint provided'); |
| 125 | + } |
| 126 | + const { success, data, error } = invokeEndpointSchema.safeParse(args); |
| 127 | + if (!success) { |
| 128 | + throw new Error(`Invalid arguments for endpoint. ${error?.format()}`); |
| 129 | + } |
| 130 | + const { endpoint_name, args: endpointArgs } = data; |
| 131 | + |
| 132 | + const endpoint = endpoints.find((e) => e.tool.name === endpoint_name); |
| 133 | + if (!endpoint) { |
| 134 | + throw new Error( |
| 135 | + `Endpoint ${endpoint_name} not found. Use the \`list_api_endpoints\` tool to get the list of available endpoints.`, |
| 136 | + ); |
| 137 | + } |
| 138 | + |
| 139 | + try { |
| 140 | + // Try to validate the arguments for a better error message |
| 141 | + const cabidela = new Cabidela(endpoint.tool.inputSchema, { fullErrors: true }); |
| 142 | + cabidela.validate(endpointArgs); |
| 143 | + } catch (error) { |
| 144 | + throw new Error(`Invalid arguments for endpoint ${endpoint_name}:\n${error}`); |
| 145 | + } |
| 146 | + |
| 147 | + return endpoint.handler(client, endpointArgs); |
| 148 | + }, |
| 149 | + }; |
| 150 | + |
| 151 | + return [getEndpointTool, listEndpointsTool, invokeEndpointTool]; |
| 152 | +} |
0 commit comments