-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.ts
More file actions
173 lines (154 loc) · 5.14 KB
/
utils.ts
File metadata and controls
173 lines (154 loc) · 5.14 KB
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
import { toolPermissions, tools } from "../lib/tools/index.js";
import { ToolkitConfig, ToolCategory } from "../types.js";
import { KnockClient } from "./knock-client.js";
import { KnockTool } from "./knock-tool.js";
import { createWorkflowTools } from "./tools/workflows-as-tools.js";
/**
* Given a list of tools, and some config may describe the tools that should be provided to the LLM,
* returns a filtered list of tools that match the config.
*
* Options:
* `*` - All tools
* `users.*` - All tools that start with `users.`
* `users.getUser` - A specific tool
*
*/
export function filterTools(
tools: Record<string, Record<string, KnockTool>>,
pattern: string | undefined
): KnockTool[] {
if (!pattern) {
throw new Error("No pattern provided");
}
// If the pattern is `*`, return all tools
if (pattern === "*") {
return Object.values(tools).flatMap((category) => Object.values(category));
}
const [category, tool] = pattern.split(".");
// If the pattern is `*.*`, return all tools
if (category === "*" && tool === "*") {
return Object.values(tools).flatMap((category) => Object.values(category));
}
if (category && !tools[category]) {
throw new Error(`Tool category ${category} not found`);
}
// If the pattern is `users.*`, return all tools that start with `users.`
if (category && tool === "*") {
return Object.values(tools[category]);
}
// If the pattern is `users.getUser`, return the `getUser` tool
if (category && tool && !tools[category][tool]) {
throw new Error(`Tool ${pattern} not found`);
}
return [tools[category][tool]];
}
/**
* Given a category and a list of permissions, return a list of tools that the user has permission to use.
*
* @param category - The category to get tools for
* @param categoryPermissions - The permissions to use
* @returns A list of tools that the user has permission to use
*/
export function getToolsWithPermissions(
category: keyof typeof toolPermissions,
categoryPermissions: Record<string, boolean | string[] | undefined>
) {
// Return all of the tools for the category that have permission
const toolsInCategory = tools[category] as Record<string, KnockTool>;
const toolPermissionsInCategory = toolPermissions[category] as Record<
string,
string[]
>;
// Look over each permission type, like `read: true`
// If it's `true`, then find all of the tools that have that permission
return Object.entries(categoryPermissions).reduce(
(acc: KnockTool[], [permissionType, hasPermission]) => {
if (
(Array.isArray(hasPermission) && hasPermission.length > 0) ||
hasPermission === true
) {
return acc.concat(
toolPermissionsInCategory[permissionType].map(
(toolName) => toolsInCategory[toolName]
)
);
}
return acc;
},
[]
);
}
/**
* Given a config, return a list of tools for each category that the user has permission to use.
*
* If the user has run permissions for workflows, then we need to get the workflow triggers tools,
* and add them to the list of tools for the workflows category.
*
* @param config - The config to use
* @returns A list of tools for each category that the user has permission to use
*/
export async function getToolsByPermissionsInCategories(
knockClient: KnockClient,
config: ToolkitConfig
): Promise<Record<ToolCategory, KnockTool[]>> {
const toolsByCategory = Object.keys(config.permissions).reduce(
(acc, category) => {
const categoryKey = category as ToolCategory;
const categoryPermissions = config.permissions[categoryKey];
if (tools[categoryKey] && categoryPermissions) {
const tools = getToolsWithPermissions(categoryKey, categoryPermissions);
return { ...acc, [categoryKey]: tools };
}
return acc;
},
{} as Record<ToolCategory, KnockTool[]>
);
// If the user has run permissions for workflows, then we need to get the workflow triggers tools,
// and add them to the list of tools for the workflows category.
if (
config.permissions.workflows &&
config.permissions.workflows.trigger &&
Array.isArray(config.permissions.workflows.trigger)
) {
const workflowTools = await createWorkflowTools(
knockClient,
config,
config.permissions.workflows.trigger
);
toolsByCategory.workflows = [
...toolsByCategory.workflows,
...workflowTools,
];
}
return toolsByCategory;
}
/**
* Given a list of tools, return a map of tools by method name.
*
* @param tools - The tools to serialize
* @returns A map of tools by method name
*/
export function getToolMap(tools: KnockTool[]) {
return tools.reduce(
(acc, tool) => {
acc[tool.method] = tool;
return acc;
},
{} as Record<string, KnockTool>
);
}
/**
* Serialize a message response from the API to a more LLM-friendly format.
*
* @param message - The message to serialize
* @returns A serialized message
*/
export function serializeMessageResponse(message: Record<string, any>) {
return {
id: message.id,
status: message.status,
engagement_statuses: message.engagement_statuses,
data: message.data,
metadata: message.metadata,
};
}