-
Notifications
You must be signed in to change notification settings - Fork 0
/
ToolsDispatcher.ts
187 lines (170 loc) · 6.01 KB
/
ToolsDispatcher.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import {ChatData} from "./data/ChatState";
import {Continuation} from "./data/Continuation";
import {ContinuationCommand} from "./data/ContinuationCommand";
import {ChatMeta} from "./data/Meta";
export interface ChatDispatchData<CM extends ChatMeta = ChatMeta> {
readonly ownerId: string
readonly chatDocumentPath: string
readonly meta: CM | null
}
/**
* Function Dispatch was successful. Contains function result
*/
export interface FunctionSuccess {
readonly result: Record<string, unknown>;
readonly comment?: string;
}
/**
* Reducer Dispatch was successful. Contains changed data state
*/
export interface ReducerSuccess<out DATA extends ChatData> {
readonly data: DATA;
readonly comment?: string;
}
/**
* Dispatch error. AI may interpret it
*/
export interface DispatchError {
readonly error: string
}
/**
* Function dispatch result
*/
export type DispatchResult<DATA extends ChatData> = FunctionSuccess | ReducerSuccess<DATA> | DispatchError;
/**
* Checks if `data` is `DispatchResult`
* @param data Data to check
* @return True if `data' is `DispatchResult`
*/
export function isDispatchResult<DATA extends ChatData>(data: unknown): data is DispatchResult<DATA> {
return isReducerSuccess(data) || isFunctionSuccess(data) || isDispatchError(data);
}
/**
* Tool dispatcher return value. May be:
* - Some value. Wrapped to `FunctionSuccess` and returned to AI
* - `DispatchResult` (`FunctionSuccess`, `ReducerSuccess`, `DispatchError' - this will be returned to AI tool
* - Continuation of above
*/
export type ToolDispatcherReturnValue<DATA extends ChatData> = DATA | DispatchResult<DATA> | Continuation<DATA | DispatchResult<DATA>>;
/**
* Represents a functions tools dispatcher that can execute different tools based on their names
* @typedef {function} ToolsDispatcher
* @param {string} name - The name of the tool to be executed
* @param {Record<string, unknown>} args - The arguments to be passed to the tool
* @param {ChatCommand} continuation - Command to dispatch when result is ready in case you want to suspend
* @param {ChatCommandData} chatData - Chat data
* @returns {PromiseLike<DispatchResult>} A promise that resolves with the result of the tool execution or suspension
* @see ToolsContinuation
*/
export interface ToolsDispatcher<DATA extends ChatData, CM extends ChatMeta = ChatMeta> {
(
data: DATA,
name: string,
args: Record<string, unknown>,
continuation: ContinuationCommand<unknown>,
chatData: ChatDispatchData<CM>
): ToolDispatcherReturnValue<DATA> | PromiseLike<ToolDispatcherReturnValue<DATA>>
}
/**
* Creates a function success result
* @param result Function return value
* @param comment Comment to supplement evaluated data
* @returns Reducer success result
*/
export function getFunctionSuccess(result: Record<string, unknown>, comment?:string): FunctionSuccess {
return {
result: result,
...(undefined !== comment ? {comment: comment} : {})
};
}
/**
* Creates a reducer success result
* @param data Result data
* @param comment Comment to supplement evaluated data
* @returns Reducer success result
*/
export function getReducerSuccess<DATA extends ChatData>(data: DATA, comment?:string): ReducerSuccess<DATA> {
return {
data: data,
...(undefined !== comment ? {comment: comment} : {})
};
}
/**
* Checks if `data` is `ReducerSuccess`
* @param data Data to check
* @return True if `data' is `ReducerSuccess`
*/
export function isReducerSuccess<DATA extends ChatData>(data: unknown): data is ReducerSuccess<DATA> {
return "object" === typeof data && null !== data && "data" in data && "object" === typeof data.data && null !== data.data;
}
/**
* Checks if `data` is `FunctionSuccess`
* @param data Data to check
* @return True if `data' is `FunctionSuccess`
*/
export function isFunctionSuccess(data: unknown): data is FunctionSuccess {
return "object" === typeof data && null !== data && "result" in data && "object" === typeof data.result && null !== data.result;
}
/**
* Evaluates the error message from tools dispatcher
* @param e {unknown} Error to analyze
* @returns {DispatchError} Normalized dispatch error
*/
export function getDispatchError(e: unknown): DispatchError {
if (isDispatchError(e)) {
return e;
}
if ("string" === typeof e) {
return {
error: e
};
}
if ("object" === typeof e && null !== e) {
if ("error" in e && "string" === typeof e.error) {
return {
error: e.error
};
}
if ("message" in e && "string" === typeof e.message) {
return {
error: e.message
};
}
return {
error: e.toString()
};
}
return {
error: "Unknown error"
};
}
/**
* Checks if `data` is `DispatchError`
* @param data Data to check
* @return True if `data' is `DispatchError`
*/
export function isDispatchError(data: unknown): data is DispatchError {
return "object" === typeof data && null !== data && "error" in data && "string" === typeof data.error;
}
/**
* Wraps dispatch to continuation
* @param block Dispatching code
*/
export async function dispatchToContinuation<DATA extends ChatData>(block: () => ToolDispatcherReturnValue<DATA> | PromiseLike<ToolDispatcherReturnValue<DATA>>): Promise<Continuation<DispatchResult<DATA>>> {
try {
const result = await block();
if (Continuation.isContinuation(result)) {
return result.isResolved() ? wrapToDispatchResult(result.value) : <Continuation<DispatchResult<DATA>>>result;
}
return wrapToDispatchResult(result);
} catch (e) {
return wrapToDispatchResult(getDispatchError(e));
}
function wrapToDispatchResult(value: DATA | DispatchResult<DATA>): Continuation<DispatchResult<DATA>> {
if (isDispatchResult(value)) {
return Continuation.resolve(value);
} else {
return Continuation.resolve(getFunctionSuccess(<DATA>value));
}
}
}