-
Notifications
You must be signed in to change notification settings - Fork 1
/
reducer.ts
72 lines (62 loc) · 2.21 KB
/
reducer.ts
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
64
65
66
67
68
69
70
71
72
import { ChatCompletionMessageToolCall } from "openai/resources";
import { ZodFunctionDef } from "./function";
import { HttpError } from "./utils/errors";
import { logger } from "./utils/logging";
import { findFunction, parseArguments } from "./utils/parse";
type FunctionReducer<State, Parameters = any> = (
prevState: State,
functionName: string,
args: Parameters,
) => State;
/**
* A reducer function threw an error.
*/
export class FunctionReducerError extends HttpError {
constructor(name: string, err: unknown) {
const message = err instanceof Error ? err.message : "Unknown error";
super(`Error calling reducer ${name}: ${message}`, 500);
}
}
/**
* A ZodFunctionReducer is a more advanced alternative to ZodFunctionHandlers.
*
* It can be useful when:
* - You are using function calls to modify an object's state (such as the state of a chart), not in a chat messages context
* - You have many functions and want to organize state updates in one place
*
* This pattern is inspired by "reducers" in Elm/Redux/React, replacing "actions" with LLM function calls.
* For example, see: https://react.dev/reference/react/useReducer
*/
export type ZodFunctionReducer<State> = {
functions: ZodFunctionDef<any>[];
reducer: FunctionReducer<State>;
};
/**
* Handle tool calls by updating state using a reducer function.
*
* Returns the new state if successful.
* Throws an error if a tool name isn't found, arguments are invalid, or a reducer throws.
*/
export async function reduceToolCalls<State>(
prevState: State,
functionReducer: ZodFunctionReducer<State>,
toolCalls: ChatCompletionMessageToolCall[] | undefined,
): Promise<State> {
if (toolCalls === undefined) {
return prevState;
}
let state = prevState;
toolCalls.forEach((toolCall) => {
const { name } = toolCall.function;
const func = findFunction(name, functionReducer.functions);
const parameters = parseArguments(name, toolCall.function.arguments, func.schema);
try {
logger.info("Calling reducer: ", name);
state = functionReducer.reducer(state, name, parameters);
logger.info("New state: ", state);
} catch (err) {
throw new FunctionReducerError(name, err);
}
});
return state;
}