-
Notifications
You must be signed in to change notification settings - Fork 0
/
schemaUtils.ts
192 lines (175 loc) · 5.44 KB
/
schemaUtils.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
187
188
189
190
191
192
import type { SchemaTraversal } from "@membrane/sdk";
export type Tool = {
id: string;
metadata: {
ref: string;
program: string;
type: string;
action: string;
query?: string;
description: string;
isPage: boolean;
};
};
export function collectTools(
programName: string,
t1: SchemaTraversal,
parentTypeName: string | null,
memberName: string | null,
visited: Set<any>,
result: Tool[],
l?: number
) {
const actions = t1.getContext()?.type?.actions;
const fields = t1.getContext()?.type?.fields;
const typeName = t1.getContext()?.type.name;
const level = l ?? 0;
// const log = (...args) => {
// console.log("----".repeat(level), ...args);
// };
// log(memberName, t1.ref);
const isRoot = typeName === "Root";
for (const action of actions) {
if (shouldSkipMember(programName, typeName, action.name)) {
continue;
}
if (action.params && Array.isArray(action.params)) {
// Add null check and array check
const params = action.params.reduce((obj, item) => {
// const argsRef = { name: item.name, type: item.type, returnType: action.type };
const optional = item.optional ? item.optional : false;
obj[item.name] = `{${item.name}$${item.type}#${optional}}`;
return obj;
}, {});
const dot = isRoot ? "" : ".";
const args = Object.entries(params).map(
([key, value]) => `${key}:"${value}"`
);
const id = `${programName}:${typeName}:${action.name}`;
let ref = programName + t1.ref + dot + action.name;
if (args?.length > 0) {
ref += `(${args.join(", ")})`;
}
result.push({
id,
metadata: {
ref: ref.replace(/\s/g, ""),
program: programName,
type: typeName,
action: action.name,
description:
action.description ||
`Invokes the action ${action.name} on a instance of type ${typeName} in the program ${programName}.`,
isPage: false,
},
});
}
}
const ref = programName + t1.ref;
let queriableFields;
let description;
let isPage = isPageField(memberName, fields);
let id;
if (isPage) {
t1.enterMember("items", {});
const itemFields = t1.getContext()?.type?.fields;
const itemTypeName = t1.getContext()?.type.name;
queriableFields = getQueriableFields(itemFields || []);
t1.pop();
id = `${programName}:${itemTypeName}:list`;
description = `Queries a collection of ${itemTypeName} items from ${parentTypeName} from program ${programName}. Collection is paginated. Can be used to find ${itemTypeName}. Available values: ${queriableFields}.`;
isPage = true;
} else {
queriableFields = getQueriableFields(fields || []);
if (queriableFields.length > 1 || queriableFields[0] !== "gref") {
id = `${programName}:${typeName}:${memberName ?? "root"}`;
if (typeName === "Root") {
description = `Queries values from the root node of program ${programName}. Available values: ${queriableFields}.`;
} else {
description = `Queries the ${typeName} in ${parentTypeName}.${memberName} from program ${programName}. Available values: ${queriableFields}.`;
}
}
}
// log(` description: "${description}"`);
if (description) {
result.push({
id,
metadata: {
ref: ref.replace(/\s/g, ""),
program: programName,
type: typeName,
action: `${typeName}:${memberName}`,
query: queriableFields.join(", "),
description,
isPage,
},
});
}
const filteredFields = (fields ?? []).filter(
(field) =>
!isPrimitiveTypeName(field.type) &&
!shouldSkipMember(programName, typeName, field.name) &&
!isWrapperTypeName(field.type)
);
for (const nestedMember of filteredFields) {
// if (nestedMember.params?.length) {
const args = (nestedMember.params ?? []).reduce((acc, field) => {
const optional = field.optional ?? false;
acc[field.name] = `{${memberName ? memberName : nestedMember.name}$${
field.name
}$${field.type}#${optional}}`;
return acc;
}, {});
if (!visited.has(nestedMember)) {
visited.add(nestedMember);
t1.enterMember(nestedMember.name, args);
collectTools(
programName,
t1,
typeName,
nestedMember.name,
visited,
result,
level + 1
);
t1.pop();
}
}
}
function isPageField(memberName: string | null, fields) {
return (
memberName === "page" &&
fields.find((field) => field.name === "items") &&
fields.find((field) => field.name === "next")
);
}
function isWrapperTypeName(typeName: string) {
return typeName === "Ref" || typeName === "List";
}
function shouldSkipMember(
programName: string,
typeName: string,
memberName: string
) {
if (programName === "me" && /ask|tell/.test(memberName)) {
// These are already provided
return true;
}
return (
typeName === "Root" &&
(memberName === "tests" ||
memberName === "endpoint" ||
memberName === "email" ||
memberName === "configure")
);
}
function getQueriableFields(fields) {
return fields
.filter((field) => isPrimitiveTypeName(field.type))
.map((field) => field.name);
}
function isPrimitiveTypeName(name) {
return /^(Int|Float|String|Boolean|Void|Json|Ref)$/.test(name);
}
// TODO: consider storing names with underscores instead of colons
export const toToolName = (n: string) => n.replace(/:/g, "_");