aboutsummaryrefslogtreecommitdiff
path: root/src/client/views/nodes/chatbot/agentsystem/Agent.ts
diff options
context:
space:
mode:
authoralyssaf16 <alyssa_feinberg@brown.edu>2024-11-04 21:56:25 -0500
committeralyssaf16 <alyssa_feinberg@brown.edu>2024-11-04 21:56:25 -0500
commit1e4909f04fdcc4c0b3a60b8c75e8b687e2b63b8e (patch)
tree16fe239082c37cd4f7e10bbfa6964d13a458046a /src/client/views/nodes/chatbot/agentsystem/Agent.ts
parent95afe2c1093dc3229375c08e6684b3d9866ef7a2 (diff)
parent09d7d63d1f248a0bf1d36e4da804cbde5e12e209 (diff)
Merge branch 'ajs-finalagent' into alyssa-agent
Diffstat (limited to 'src/client/views/nodes/chatbot/agentsystem/Agent.ts')
-rw-r--r--src/client/views/nodes/chatbot/agentsystem/Agent.ts87
1 files changed, 45 insertions, 42 deletions
diff --git a/src/client/views/nodes/chatbot/agentsystem/Agent.ts b/src/client/views/nodes/chatbot/agentsystem/Agent.ts
index 23e7d4a9d..05d13d1db 100644
--- a/src/client/views/nodes/chatbot/agentsystem/Agent.ts
+++ b/src/client/views/nodes/chatbot/agentsystem/Agent.ts
@@ -15,7 +15,7 @@ import { AgentMessage, AssistantMessage, Observation, PROCESSING_TYPE, Processin
import { Vectorstore } from '../vectorstore/Vectorstore';
import { getReactPrompt } from './prompts';
import { BaseTool } from '../tools/BaseTool';
-import { Parameter, ParametersType } from '../types/tool_types';
+import { Parameter, ParametersType, TypeMap } from '../types/tool_types';
import { CreateDocTool } from '../tools/CreateDocumentTool';
import { DocumentOptions } from '../../../../documents/Documents';
@@ -269,11 +269,35 @@ export class Agent {
}
/**
+ * Helper function to check if a string can be parsed as an array of the expected type.
+ * @param input The input string to check.
+ * @param expectedType The expected type of the array elements ('string', 'number', or 'boolean').
+ * @returns The parsed array if valid, otherwise throws an error.
+ */
+ private parseArray<T>(input: string, expectedType: 'string' | 'number' | 'boolean'): T[] {
+ try {
+ // Parse the input string into a JSON object
+ const parsed = JSON.parse(input);
+
+ // Check if the parsed object is an array and if all elements are of the expected type
+ if (Array.isArray(parsed) && parsed.every(item => typeof item === expectedType)) {
+ return parsed;
+ } else {
+ throw new Error(`Invalid ${expectedType} array format.`);
+ }
+ } catch (error) {
+ throw new Error(`Failed to parse ${expectedType} array: ` + error);
+ }
+ }
+
+ /**
* Processes a specific action by invoking the appropriate tool with the provided inputs.
* This method ensures that the action exists and validates the types of `actionInput`
* based on the tool's parameter rules. It throws errors for missing required parameters
* or mismatched types before safely executing the tool with the validated input.
*
+ * NOTE: In the future, it should typecheck for specific tool parameter types using the `TypeMap` or otherwise.
+ *
* Type validation includes checks for:
* - `string`, `number`, `boolean`
* - `string[]`, `number[]` (arrays of strings or numbers)
@@ -283,56 +307,35 @@ export class Agent {
* @returns A promise that resolves to an array of `Observation` objects representing the result of the action.
* @throws An error if the action is unknown, if required parameters are missing, or if input types don't match the expected parameter types.
*/
- private async processAction(action: string, actionInput: Record<string, unknown>): Promise<Observation[]> {
+ private async processAction(action: string, actionInput: ParametersType<ReadonlyArray<Parameter>>): Promise<Observation[]> {
// Check if the action exists in the tools list
if (!(action in this.tools)) {
throw new Error(`Unknown action: ${action}`);
}
+ console.log(actionInput);
- const tool = this.tools[action];
-
- // Validate actionInput based on tool's parameter rules
- for (const paramRule of tool.parameterRules) {
- const inputValue = actionInput[paramRule.name];
-
- if (paramRule.required && inputValue === undefined) {
- throw new Error(`Missing required parameter: ${paramRule.name}`);
+ for (const param of this.tools[action].parameterRules) {
+ // Check if the parameter is required and missing in the input
+ if (param.required && !(param.name in actionInput)) {
+ throw new Error(`Missing required parameter: ${param.name}`);
}
- // If the parameter is defined, check its type
- if (inputValue !== undefined) {
- switch (paramRule.type) {
- case 'string':
- if (typeof inputValue !== 'string') {
- throw new Error(`Expected parameter '${paramRule.name}' to be a string.`);
- }
- break;
- case 'number':
- if (typeof inputValue !== 'number') {
- throw new Error(`Expected parameter '${paramRule.name}' to be a number.`);
- }
- break;
- case 'boolean':
- if (typeof inputValue !== 'boolean') {
- throw new Error(`Expected parameter '${paramRule.name}' to be a boolean.`);
- }
- break;
- case 'string[]':
- if (!Array.isArray(inputValue) || !inputValue.every(item => typeof item === 'string')) {
- throw new Error(`Expected parameter '${paramRule.name}' to be an array of strings.`);
- }
- break;
- case 'number[]':
- if (!Array.isArray(inputValue) || !inputValue.every(item => typeof item === 'number')) {
- throw new Error(`Expected parameter '${paramRule.name}' to be an array of numbers.`);
- }
- break;
- default:
- throw new Error(`Unsupported parameter type: ${paramRule.type}`);
- }
+ // Check if the parameter type matches the expected type
+ const expectedType = param.type.replace('[]', '') as 'string' | 'number' | 'boolean';
+ const isArray = param.type.endsWith('[]');
+ const input = actionInput[param.name];
+
+ if (isArray) {
+ // Check if the input is a valid array of the expected type
+ const parsedArray = this.parseArray(input as string, expectedType);
+ actionInput[param.name] = parsedArray as TypeMap[typeof param.type];
+ } else if (typeof input !== expectedType) {
+ throw new Error(`Invalid type for parameter ${param.name}: expected ${expectedType}`);
}
}
- return await tool.execute(actionInput as ParametersType<typeof tool.parameterRules>);
+ const tool = this.tools[action];
+
+ return await tool.execute(actionInput);
}
}