@ptkl/toolkit 1.5.0 → 1.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +20 -0
- package/dist/commands/__tests__/forge-bundle-layout.test.js +12 -0
- package/dist/commands/__tests__/forge-tools.test.js +64 -0
- package/dist/commands/__tests__/kortexPlaybook.test.js +328 -0
- package/dist/commands/forge.js +159 -70
- package/dist/commands/kortex.js +8 -6
- package/dist/commands/kortexPlaybook.js +396 -0
- package/dist/lib/__tests__/apiError.test.js +4 -0
- package/dist/lib/apiError.js +12 -1
- package/dist/lib/kortex/__tests__/fixtures/sse-and-persistence.test.js +1 -1
- package/dist/lib/kortex/__tests__/playbookRules.test.js +120 -0
- package/dist/lib/kortex/__tests__/resultSchemaChecker.test.js +37 -0
- package/dist/lib/kortex/playbookValidator.js +297 -0
- package/dist/lib/kortex/providers/protokol.js +1 -1
- package/dist/lib/kortex/resultSchemaChecker.js +188 -0
- package/dist/lib/kortex/session.js +1 -3
- package/package.json +2 -2
|
@@ -0,0 +1,297 @@
|
|
|
1
|
+
// Offline structural checks for a playbook drawing. They mirror the server's graph rules
|
|
2
|
+
// (no_nodes, duplicate_node, no_start, cycle, ...) so a file can be checked before pushing.
|
|
3
|
+
// Whether an agent exists or is enabled is only known to the server and is checked on deploy.
|
|
4
|
+
// The same goes for the syntax of a rule (`when`): only the server can compile it, so use
|
|
5
|
+
// `ptkl kortex playbook rule-check` to try one. Offline, a rule is only checked to be present
|
|
6
|
+
// and not too long. The rule messages are the server's, word for word.
|
|
7
|
+
// Rules on the version (`graph.rules`) are checked here in full, with the server's messages.
|
|
8
|
+
import ResultSchemaChecker, { MAX_RESULT_PROBLEMS, goQuote } from './resultSchemaChecker.js';
|
|
9
|
+
// Limits on rules, the same as the server's.
|
|
10
|
+
export const MAX_RULE_LENGTH = 1000;
|
|
11
|
+
export const MAX_RULE_LINES = 20;
|
|
12
|
+
// Limits on a version's rules, the same as the server's.
|
|
13
|
+
export const MAX_WORK_RULE_TEXT = 1000;
|
|
14
|
+
export const MAX_WORK_RULE_TITLE = 120;
|
|
15
|
+
export const MAX_WORK_RULE_WHY = 1000;
|
|
16
|
+
export const MAX_WORK_RULES = 20;
|
|
17
|
+
// Limits a save enforces (the server's checkSize), so a file that passes here is not refused at push.
|
|
18
|
+
export const MAX_PLAYBOOK_STEPS = 200;
|
|
19
|
+
export const MAX_PLAYBOOK_LINES = 400;
|
|
20
|
+
export const MAX_STEP_TEXT_BYTES = 8000;
|
|
21
|
+
export const MAX_STEP_TITLE_BYTES = 200;
|
|
22
|
+
export const MAX_RULE_ROWS = 100;
|
|
23
|
+
export const MAX_RULE_FIELD_BYTES = 20000;
|
|
24
|
+
export default class PlaybookValidator {
|
|
25
|
+
validate(graph) {
|
|
26
|
+
const problems = [];
|
|
27
|
+
const add = (code, message, node_id) => problems.push({ code, message, node_id });
|
|
28
|
+
const checker = new ResultSchemaChecker();
|
|
29
|
+
if (!graph || typeof graph !== 'object' || !Array.isArray(graph.nodes) || !Array.isArray(graph.edges)) {
|
|
30
|
+
add('bad_graph', 'The graph must have a "nodes" list and an "edges" list.');
|
|
31
|
+
return problems;
|
|
32
|
+
}
|
|
33
|
+
if (graph.nodes.length === 0) {
|
|
34
|
+
add('no_nodes', 'The playbook has no steps.');
|
|
35
|
+
return problems;
|
|
36
|
+
}
|
|
37
|
+
this.sizeProblems(graph, problems);
|
|
38
|
+
// Like the server: an empty id is exact (no trimming), and only the first step with an id is checked.
|
|
39
|
+
const ids = new Set();
|
|
40
|
+
const dupReported = new Set();
|
|
41
|
+
for (const n of graph.nodes) {
|
|
42
|
+
const label = n?.title || n?.id || '(unnamed)';
|
|
43
|
+
if (!n || typeof n.id !== 'string' || n.id === '') {
|
|
44
|
+
add('empty_node_id', 'A step has no id.');
|
|
45
|
+
continue;
|
|
46
|
+
}
|
|
47
|
+
if (ids.has(n.id)) {
|
|
48
|
+
if (!dupReported.has(n.id))
|
|
49
|
+
add('duplicate_node', `Two steps share the id "${n.id}".`, n.id);
|
|
50
|
+
dupReported.add(n.id);
|
|
51
|
+
continue;
|
|
52
|
+
}
|
|
53
|
+
ids.add(n.id);
|
|
54
|
+
if (n.kind !== 'instruction' && n.kind !== 'condition') {
|
|
55
|
+
add('bad_kind', `Step "${label}" must be an "instruction" or a "condition".`, n.id);
|
|
56
|
+
}
|
|
57
|
+
const byRules = n.kind === 'condition' && n.decide_by === 'rules';
|
|
58
|
+
if (!byRules && (typeof n.text !== 'string' || n.text === '')) {
|
|
59
|
+
add('empty_text', `Step "${label}" has no text.`, n.id);
|
|
60
|
+
}
|
|
61
|
+
const l = `Step ${goQuote(label)}`;
|
|
62
|
+
if (n.decide_by !== undefined && n.decide_by !== null && n.decide_by !== '') {
|
|
63
|
+
if (n.kind !== 'condition') {
|
|
64
|
+
add('bad_decide_by', `${l} is not a choice, so it cannot decide by rules.`, n.id);
|
|
65
|
+
}
|
|
66
|
+
else if (n.decide_by !== 'agent' && n.decide_by !== 'rules') {
|
|
67
|
+
add('bad_decide_by', `${l} decides by ${goQuote(String(n.decide_by))}; it must be "agent" or "rules".`, n.id);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
const hasAgent = typeof n.agent_uuid === 'string' && n.agent_uuid !== '';
|
|
71
|
+
if (byRules) {
|
|
72
|
+
if (hasAgent)
|
|
73
|
+
add('rules_no_agent', `${l} decides by rules, so it has no agent.`, n.id);
|
|
74
|
+
}
|
|
75
|
+
else if (!hasAgent) {
|
|
76
|
+
add('no_agent', `Step "${label}" has no agent.`, n.id);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
const outgoingFrom = new Set(graph.edges.map((e) => e?.from));
|
|
80
|
+
for (const n of graph.nodes) {
|
|
81
|
+
if (!n || typeof n.id !== 'string' || n.id === '')
|
|
82
|
+
continue;
|
|
83
|
+
if (n.result_schema === undefined || n.result_schema === null)
|
|
84
|
+
continue;
|
|
85
|
+
const label = n.title || n.id;
|
|
86
|
+
if (n.kind === 'condition') {
|
|
87
|
+
add('result_schema_invalid', `Step ${goQuote(label)} is a choice and cannot return a result.`, n.id);
|
|
88
|
+
}
|
|
89
|
+
else if (outgoingFrom.has(n.id)) {
|
|
90
|
+
add('result_schema_not_at_end', 'Only a last step can return a result.', n.id);
|
|
91
|
+
}
|
|
92
|
+
else {
|
|
93
|
+
// Up to 20 per step, each prefixed with the step, like the server.
|
|
94
|
+
for (const rp of checker.problems(n.result_schema).slice(0, MAX_RESULT_PROBLEMS)) {
|
|
95
|
+
problems.push({ code: 'result_schema_invalid', node_id: n.id, field: rp.field, message: `Step ${goQuote(label)}: ${rp.message}` });
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
const incoming = new Map();
|
|
100
|
+
const outgoing = new Map();
|
|
101
|
+
const seenLines = new Set();
|
|
102
|
+
for (const e of graph.edges) {
|
|
103
|
+
if (!ids.has(e?.from) || !ids.has(e?.to)) {
|
|
104
|
+
add('unknown_node', `A line goes from "${e?.from}" to "${e?.to}", and one of those steps does not exist.`);
|
|
105
|
+
continue;
|
|
106
|
+
}
|
|
107
|
+
const key = `${e.from}->${e.to}`;
|
|
108
|
+
if (seenLines.has(key)) {
|
|
109
|
+
add('duplicate_line', `There are two lines from "${e.from}" to "${e.to}".`, e.from);
|
|
110
|
+
continue;
|
|
111
|
+
}
|
|
112
|
+
seenLines.add(key);
|
|
113
|
+
incoming.set(e.to, (incoming.get(e.to) ?? 0) + 1);
|
|
114
|
+
outgoing.set(e.from, [...(outgoing.get(e.from) ?? []), e]);
|
|
115
|
+
}
|
|
116
|
+
for (const n of graph.nodes) {
|
|
117
|
+
if (!n || !ids.has(n.id) || (n.kind === 'condition' && n.decide_by === 'rules'))
|
|
118
|
+
continue;
|
|
119
|
+
const label = n.title || n.id;
|
|
120
|
+
for (const e of outgoing.get(n.id) ?? []) {
|
|
121
|
+
if ((typeof e.when === 'string' && e.when !== '') || e.otherwise) {
|
|
122
|
+
problems.push({
|
|
123
|
+
code: 'when_on_agent_condition',
|
|
124
|
+
node_id: n.id,
|
|
125
|
+
field: e.name,
|
|
126
|
+
message: `The line ${this.lineLabel(e)} out of Step ${goQuote(label)} has a rule, but only a choice that decides by rules can have one.`,
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
for (const n of graph.nodes) {
|
|
132
|
+
if (n?.kind !== 'condition' || !ids.has(n.id))
|
|
133
|
+
continue;
|
|
134
|
+
const lines = outgoing.get(n.id) ?? [];
|
|
135
|
+
const label = n.title || n.id;
|
|
136
|
+
if (lines.length < 2) {
|
|
137
|
+
add('condition_few_lines', `Condition "${label}" needs at least two lines leaving it.`, n.id);
|
|
138
|
+
}
|
|
139
|
+
const names = new Set();
|
|
140
|
+
for (const l of lines) {
|
|
141
|
+
if (!l.name) {
|
|
142
|
+
add('unnamed_line', `A line leaving condition "${label}" to "${l.to}" has no name.`, n.id);
|
|
143
|
+
}
|
|
144
|
+
else if (names.has(l.name)) {
|
|
145
|
+
add('duplicate_line_name', `Condition "${label}" has two lines named "${l.name}".`, n.id);
|
|
146
|
+
}
|
|
147
|
+
names.add(l.name ?? '');
|
|
148
|
+
}
|
|
149
|
+
if (n.decide_by === 'rules')
|
|
150
|
+
this.ruleProblems(n, lines, problems);
|
|
151
|
+
}
|
|
152
|
+
const starts = [...ids].filter((id) => !incoming.has(id));
|
|
153
|
+
if (starts.length === 0) {
|
|
154
|
+
add('no_start', 'There is no starting step (every step has a line coming in).');
|
|
155
|
+
}
|
|
156
|
+
else if (starts.length > 1) {
|
|
157
|
+
add('several_starts', `There is more than one starting step: ${starts.join(', ')}.`);
|
|
158
|
+
}
|
|
159
|
+
else {
|
|
160
|
+
const reached = new Set([starts[0]]);
|
|
161
|
+
const queue = [starts[0]];
|
|
162
|
+
while (queue.length) {
|
|
163
|
+
for (const e of outgoing.get(queue.shift()) ?? []) {
|
|
164
|
+
if (!reached.has(e.to)) {
|
|
165
|
+
reached.add(e.to);
|
|
166
|
+
queue.push(e.to);
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
for (const id of ids) {
|
|
171
|
+
if (!reached.has(id))
|
|
172
|
+
add('unreachable', `Step "${id}" cannot be reached from the start.`, id);
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
if (this.hasCycle(ids, outgoing))
|
|
176
|
+
add('cycle', 'The lines form a loop; a playbook cannot go back to an earlier step.');
|
|
177
|
+
this.workRuleProblems(graph, ids, problems);
|
|
178
|
+
return problems;
|
|
179
|
+
}
|
|
180
|
+
sizeProblems(graph, problems) {
|
|
181
|
+
const add = (field, message) => problems.push({ code: 'too_large', field, message });
|
|
182
|
+
const bytes = (v) => (typeof v === 'string' ? Buffer.byteLength(v) : 0);
|
|
183
|
+
if (graph.nodes.length > MAX_PLAYBOOK_STEPS)
|
|
184
|
+
add('nodes', `At most ${MAX_PLAYBOOK_STEPS} steps.`);
|
|
185
|
+
if (graph.edges.length > MAX_PLAYBOOK_LINES)
|
|
186
|
+
add('edges', `At most ${MAX_PLAYBOOK_LINES} lines.`);
|
|
187
|
+
if (graph.nodes.some((n) => bytes(n?.text) > MAX_STEP_TEXT_BYTES))
|
|
188
|
+
add('nodes.text', `Step text is at most ${MAX_STEP_TEXT_BYTES} characters.`);
|
|
189
|
+
if (graph.nodes.some((n) => bytes(n?.title) > MAX_STEP_TITLE_BYTES))
|
|
190
|
+
add('nodes.title', `Step title is at most ${MAX_STEP_TITLE_BYTES} characters.`);
|
|
191
|
+
if (Array.isArray(graph.rules)) {
|
|
192
|
+
if (graph.rules.length > MAX_RULE_ROWS)
|
|
193
|
+
add('rules', `At most ${MAX_RULE_ROWS} rules.`);
|
|
194
|
+
if (graph.rules.some((r) => [r?.rule, r?.why, r?.title, r?.step].some((v) => bytes(v) > MAX_RULE_FIELD_BYTES)))
|
|
195
|
+
add('rules', 'A rule is too long.');
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
// Rules on the version: their text, their count and ids, and the step each is for.
|
|
199
|
+
workRuleProblems(graph, ids, problems) {
|
|
200
|
+
if (graph.rules === undefined || graph.rules === null)
|
|
201
|
+
return;
|
|
202
|
+
if (!Array.isArray(graph.rules)) {
|
|
203
|
+
problems.push({ code: 'bad_graph', message: 'The graph\'s "rules" must be a list.' });
|
|
204
|
+
return;
|
|
205
|
+
}
|
|
206
|
+
const rules = graph.rules;
|
|
207
|
+
const add = (code, field, message, node_id) => problems.push({ code, node_id, field, message });
|
|
208
|
+
const runes = (v) => (typeof v === 'string' ? [...v].length : 0);
|
|
209
|
+
const text = (v) => (typeof v === 'string' ? v : '');
|
|
210
|
+
if (rules.length > MAX_WORK_RULES) {
|
|
211
|
+
add('too_many_rules', 'rules', `There are ${rules.length} rules; at most ${MAX_WORK_RULES} are allowed.`);
|
|
212
|
+
}
|
|
213
|
+
const seen = new Set();
|
|
214
|
+
rules.forEach((r, i) => {
|
|
215
|
+
const n = i + 1;
|
|
216
|
+
if (text(r?.rule).trim() === '')
|
|
217
|
+
add('rule_no_text', `rules[${i}].rule`, `Rule ${n} has no text.`);
|
|
218
|
+
else if (runes(r.rule) > MAX_WORK_RULE_TEXT)
|
|
219
|
+
add('rule_too_long', `rules[${i}].rule`, `Rule ${n} is longer than ${MAX_WORK_RULE_TEXT} characters.`);
|
|
220
|
+
if (text(r?.title).trim() === '')
|
|
221
|
+
add('rule_no_title', `rules[${i}].title`, `Rule ${n} has no title.`);
|
|
222
|
+
else if (runes(r.title) > MAX_WORK_RULE_TITLE)
|
|
223
|
+
add('rule_title_too_long', `rules[${i}].title`, `The title of rule ${n} is longer than ${MAX_WORK_RULE_TITLE} characters.`);
|
|
224
|
+
if (runes(r?.why) > MAX_WORK_RULE_WHY)
|
|
225
|
+
add('rule_why_too_long', `rules[${i}].why`, `The reason on rule ${n} is longer than ${MAX_WORK_RULE_WHY} characters.`);
|
|
226
|
+
const id = text(r?.id);
|
|
227
|
+
if (id !== '') {
|
|
228
|
+
if (seen.has(id))
|
|
229
|
+
add('rule_id_twice', `rules[${i}].id`, `Two rules share the id ${goQuote(id)}.`);
|
|
230
|
+
seen.add(id);
|
|
231
|
+
}
|
|
232
|
+
});
|
|
233
|
+
rules.forEach((r, i) => {
|
|
234
|
+
const step = text(r?.step);
|
|
235
|
+
if (step === '')
|
|
236
|
+
return;
|
|
237
|
+
const field = `rules[${i}].step`;
|
|
238
|
+
if (!ids.has(step)) {
|
|
239
|
+
add('rule_unknown_step', field, `Rule ${i + 1} is for the step ${goQuote(step)}, but there is no such step.`);
|
|
240
|
+
return;
|
|
241
|
+
}
|
|
242
|
+
const node = graph.nodes.find((x) => x?.id === step);
|
|
243
|
+
if (node?.kind === 'condition' && node.decide_by === 'rules') {
|
|
244
|
+
add('rule_on_rules_condition', field, `Rule ${i + 1} is for Step ${goQuote(node.title || node.id)}, which decides by rules and never reads rules.`, step);
|
|
245
|
+
}
|
|
246
|
+
});
|
|
247
|
+
}
|
|
248
|
+
// Lines out of a rules condition: each has a rule or is the one "otherwise".
|
|
249
|
+
ruleProblems(n, lines, problems) {
|
|
250
|
+
const l = `Step ${goQuote(n.title || n.id)}`;
|
|
251
|
+
const add = (code, field, message) => problems.push({ code, node_id: n.id, field, message });
|
|
252
|
+
if (lines.length > MAX_RULE_LINES) {
|
|
253
|
+
add('rules_too_many_lines', undefined, `${l} has ${lines.length} lines out; a choice by rules can have at most ${MAX_RULE_LINES}.`);
|
|
254
|
+
}
|
|
255
|
+
let otherwise = 0;
|
|
256
|
+
for (const e of lines) {
|
|
257
|
+
const when = typeof e.when === 'string' ? e.when : '';
|
|
258
|
+
const lbl = this.lineLabel(e);
|
|
259
|
+
if (e.otherwise && when.trim() !== '') {
|
|
260
|
+
add('rules_when_and_otherwise', e.name, `The line ${lbl} out of ${l} has both a rule and "otherwise"; use one of them.`);
|
|
261
|
+
}
|
|
262
|
+
else if (e.otherwise) {
|
|
263
|
+
otherwise++;
|
|
264
|
+
if (otherwise === 2)
|
|
265
|
+
add('rules_otherwise_twice', e.name, `More than one line out of ${l} is "otherwise"; only one can be.`);
|
|
266
|
+
}
|
|
267
|
+
else if (when.trim() === '') {
|
|
268
|
+
add('rules_need_when', e.name, `The line ${lbl} out of ${l} has no rule; give it a rule or mark it "otherwise".`);
|
|
269
|
+
}
|
|
270
|
+
else if (Buffer.byteLength(when) > MAX_RULE_LENGTH) {
|
|
271
|
+
add('rules_when_invalid', e.name, `The rule on line ${lbl} out of ${l} is not valid: the rule is longer than ${MAX_RULE_LENGTH} characters.`);
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
lineLabel(e) {
|
|
276
|
+
return e.name ? goQuote(e.name) : `to ${goQuote(e.to)}`;
|
|
277
|
+
}
|
|
278
|
+
hasCycle(ids, outgoing) {
|
|
279
|
+
const state = new Map();
|
|
280
|
+
const visit = (id) => {
|
|
281
|
+
state.set(id, 1);
|
|
282
|
+
for (const e of outgoing.get(id) ?? []) {
|
|
283
|
+
const s = state.get(e.to);
|
|
284
|
+
if (s === 1)
|
|
285
|
+
return true;
|
|
286
|
+
if (!s && visit(e.to))
|
|
287
|
+
return true;
|
|
288
|
+
}
|
|
289
|
+
state.set(id, 2);
|
|
290
|
+
return false;
|
|
291
|
+
};
|
|
292
|
+
for (const id of ids)
|
|
293
|
+
if (!state.get(id) && visit(id))
|
|
294
|
+
return true;
|
|
295
|
+
return false;
|
|
296
|
+
}
|
|
297
|
+
}
|
|
@@ -43,7 +43,7 @@ export class ProtokolAdapter {
|
|
|
43
43
|
const messages = this.turnsToProtokolMessages(request.turns);
|
|
44
44
|
const agentUUID = request.target.type === 'provider_agent'
|
|
45
45
|
? request.target.id
|
|
46
|
-
: '
|
|
46
|
+
: 'prota';
|
|
47
47
|
try {
|
|
48
48
|
const response = await this.client.post('/luma/kortex/v1/chat', {
|
|
49
49
|
agent_uuid: agentUUID,
|
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
// Mirrors Kortex's result schema checker (commit f35ce00): same rules, same messages,
|
|
2
|
+
// same field paths, same order. Every field is checked, not just the first.
|
|
3
|
+
// Not mirrored: the server's last-resort meta-schema validation (it adds a generic
|
|
4
|
+
// "the schema is not valid" problem only when none of the rules above fired).
|
|
5
|
+
export const MAX_RESULT_SCHEMA_DEPTH = 5;
|
|
6
|
+
export const MAX_RESULT_SCHEMA_BYTES = 8 * 1024;
|
|
7
|
+
export const MAX_RESULT_DESCRIPTION_LENGTH = 500;
|
|
8
|
+
export const MAX_RESULT_PROBLEMS = 20;
|
|
9
|
+
const TYPE_LIST = 'object, array, string, number, integer or boolean';
|
|
10
|
+
const KEYWORD_LIST = 'type, properties, required, items, enum, description and additionalProperties';
|
|
11
|
+
const TYPES = new Set(['object', 'array', 'string', 'number', 'integer', 'boolean']);
|
|
12
|
+
const KEYWORDS = new Set(['type', 'properties', 'required', 'items', 'enum', 'description', 'additionalProperties']);
|
|
13
|
+
const NAME_RE = /^[A-Za-z][A-Za-z0-9_-]{0,63}$/;
|
|
14
|
+
const isObj = (v) => v !== null && typeof v === 'object' && !Array.isArray(v);
|
|
15
|
+
const has = (o, k) => Object.prototype.hasOwnProperty.call(o, k);
|
|
16
|
+
// Like Go's %q for the usual titles: double quotes, backslash escapes.
|
|
17
|
+
export const goQuote = (s) => JSON.stringify(s);
|
|
18
|
+
const deepEqual = (a, b) => JSON.stringify(a) === JSON.stringify(b);
|
|
19
|
+
export default class ResultSchemaChecker {
|
|
20
|
+
found = [];
|
|
21
|
+
// Everything wrong with a schema, in plain words; empty when it is fine.
|
|
22
|
+
problems(schema) {
|
|
23
|
+
this.found = [];
|
|
24
|
+
if (Buffer.byteLength(JSON.stringify(schema ?? null)) > MAX_RESULT_SCHEMA_BYTES) {
|
|
25
|
+
return [{ field: '', message: `the result: the schema is too big (at most ${MAX_RESULT_SCHEMA_BYTES / 1024} KB).` }];
|
|
26
|
+
}
|
|
27
|
+
if (!isObj(schema))
|
|
28
|
+
return [{ field: '', message: 'the result: the schema must be a JSON object.' }];
|
|
29
|
+
this.top(schema);
|
|
30
|
+
return this.found;
|
|
31
|
+
}
|
|
32
|
+
add(field, message) {
|
|
33
|
+
const where = field === '' ? 'the result' : `field '${field}'`;
|
|
34
|
+
this.found.push({ field, message: `${where}: ${message}` });
|
|
35
|
+
}
|
|
36
|
+
top(root) {
|
|
37
|
+
if (root.type !== 'object') {
|
|
38
|
+
this.add('', 'the top level must be an object, with "type": "object".');
|
|
39
|
+
}
|
|
40
|
+
else {
|
|
41
|
+
const p = root.properties;
|
|
42
|
+
if ((p === undefined || p === null || isObj(p)) && Object.keys(p ?? {}).length === 0) {
|
|
43
|
+
this.add('', 'it needs at least one property under "properties".');
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
this.node(root, '', 1, true);
|
|
47
|
+
}
|
|
48
|
+
node(s, field, depth, top) {
|
|
49
|
+
if (depth > MAX_RESULT_SCHEMA_DEPTH) {
|
|
50
|
+
this.add(field, `nested too deeply (at most ${MAX_RESULT_SCHEMA_DEPTH} levels).`);
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
for (const k of Object.keys(s).sort()) {
|
|
54
|
+
if (!KEYWORDS.has(k))
|
|
55
|
+
this.add(field, `'${k}' is not supported. Use only ${KEYWORD_LIST}.`);
|
|
56
|
+
}
|
|
57
|
+
const typ = this.typeOf(s, field, top);
|
|
58
|
+
this.description(s, field);
|
|
59
|
+
if (has(s, 'additionalProperties') && typeof s.additionalProperties !== 'boolean') {
|
|
60
|
+
this.add(field, 'additionalProperties must be true or false.');
|
|
61
|
+
}
|
|
62
|
+
this.enum(s, field, typ);
|
|
63
|
+
if (has(s, 'properties') && typ !== '' && typ !== 'object')
|
|
64
|
+
this.add(field, 'only an object can have properties.');
|
|
65
|
+
if (has(s, 'required') && typ !== '' && typ !== 'object')
|
|
66
|
+
this.add(field, 'only an object can have required.');
|
|
67
|
+
if (has(s, 'items') && typ !== '' && typ !== 'array')
|
|
68
|
+
this.add(field, 'only an array can have items.');
|
|
69
|
+
if (!has(s, 'items') && typ === 'array')
|
|
70
|
+
this.add(field, 'an array needs items, so the agent knows what each element is.');
|
|
71
|
+
this.properties(s, field, depth);
|
|
72
|
+
if (typ === '' || typ === 'object')
|
|
73
|
+
this.required(s, field);
|
|
74
|
+
if (has(s, 'items')) {
|
|
75
|
+
if (!isObj(s.items))
|
|
76
|
+
this.add(field, 'items must be a schema object, like {"type": "string"}.');
|
|
77
|
+
else
|
|
78
|
+
this.node(s.items, `${field}[]`, depth + 1, false);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
typeOf(s, field, top) {
|
|
82
|
+
const raw = s.type;
|
|
83
|
+
if (top && raw !== 'object')
|
|
84
|
+
return '';
|
|
85
|
+
if (raw === undefined || raw === null)
|
|
86
|
+
this.add(field, `it needs a "type": ${TYPE_LIST}.`);
|
|
87
|
+
else if (typeof raw === 'string') {
|
|
88
|
+
if (TYPES.has(raw))
|
|
89
|
+
return raw;
|
|
90
|
+
this.add(field, `unknown type '${raw}'; use ${TYPE_LIST}.`);
|
|
91
|
+
}
|
|
92
|
+
else
|
|
93
|
+
this.add(field, `the type must be one word, not a list or an object: ${TYPE_LIST}.`);
|
|
94
|
+
return '';
|
|
95
|
+
}
|
|
96
|
+
description(s, field) {
|
|
97
|
+
if (!has(s, 'description'))
|
|
98
|
+
return;
|
|
99
|
+
const d = s.description;
|
|
100
|
+
if (typeof d !== 'string')
|
|
101
|
+
this.add(field, 'the description must be text.');
|
|
102
|
+
else if ([...d].length > MAX_RESULT_DESCRIPTION_LENGTH) {
|
|
103
|
+
this.add(field, `the description is too long (at most ${MAX_RESULT_DESCRIPTION_LENGTH} characters).`);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
enum(s, field, typ) {
|
|
107
|
+
if (!has(s, 'enum'))
|
|
108
|
+
return;
|
|
109
|
+
if (typ !== '' && typ !== 'string' && typ !== 'number' && typ !== 'integer') {
|
|
110
|
+
this.add(field, 'enum is only for string, number or integer fields.');
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
const list = s.enum;
|
|
114
|
+
if (!Array.isArray(list) || list.length === 0) {
|
|
115
|
+
this.add(field, 'the allowed values (enum) must be a non-empty list.');
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
for (let i = 0; i < list.length; i++) {
|
|
119
|
+
for (let j = 0; j < i; j++) {
|
|
120
|
+
if (deepEqual(list[i], list[j])) {
|
|
121
|
+
this.add(field, 'the allowed values (enum) must not repeat a value.');
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
let wrong = false;
|
|
127
|
+
let want = '';
|
|
128
|
+
for (const v of list) {
|
|
129
|
+
const isNum = typeof v === 'number';
|
|
130
|
+
if (typ === 'string') {
|
|
131
|
+
wrong = wrong || typeof v !== 'string';
|
|
132
|
+
want = 'text';
|
|
133
|
+
}
|
|
134
|
+
else if (typ === 'number') {
|
|
135
|
+
wrong = wrong || !isNum;
|
|
136
|
+
want = 'numbers';
|
|
137
|
+
}
|
|
138
|
+
else if (typ === 'integer') {
|
|
139
|
+
wrong = wrong || !isNum || v !== Math.trunc(v);
|
|
140
|
+
want = 'whole numbers';
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
if (wrong)
|
|
144
|
+
this.add(field, `the allowed values must be ${want}, like its type.`);
|
|
145
|
+
}
|
|
146
|
+
properties(s, field, depth) {
|
|
147
|
+
if (!has(s, 'properties'))
|
|
148
|
+
return;
|
|
149
|
+
const pm = s.properties;
|
|
150
|
+
if (!isObj(pm)) {
|
|
151
|
+
this.add(field, 'properties must be an object of named fields.');
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
for (const n of Object.keys(pm).sort()) {
|
|
155
|
+
const path = field === '' ? n : `${field}.${n}`;
|
|
156
|
+
if (!NAME_RE.test(n))
|
|
157
|
+
this.add(path, 'the name is not allowed; use 1 to 64 letters, digits, _ or -, starting with a letter.');
|
|
158
|
+
const child = pm[n];
|
|
159
|
+
if (!isObj(child)) {
|
|
160
|
+
this.add(path, 'it must be a schema object, like {"type": "string"}.');
|
|
161
|
+
continue;
|
|
162
|
+
}
|
|
163
|
+
this.node(child, path, depth + 1, false);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
required(s, field) {
|
|
167
|
+
if (!has(s, 'required'))
|
|
168
|
+
return;
|
|
169
|
+
const list = s.required;
|
|
170
|
+
if (!Array.isArray(list)) {
|
|
171
|
+
this.add(field, 'required must be a list of field names.');
|
|
172
|
+
return;
|
|
173
|
+
}
|
|
174
|
+
const pm = isObj(s.properties) ? s.properties : {};
|
|
175
|
+
const seen = new Set();
|
|
176
|
+
for (const v of list) {
|
|
177
|
+
if (typeof v !== 'string') {
|
|
178
|
+
this.add(field, 'required must be a list of field names.');
|
|
179
|
+
return;
|
|
180
|
+
}
|
|
181
|
+
if (seen.has(v))
|
|
182
|
+
this.add(field, `required lists '${v}' more than once.`);
|
|
183
|
+
else if (!has(pm, v) || pm[v] === null)
|
|
184
|
+
this.add(field, `required lists '${v}', which is not among its properties.`);
|
|
185
|
+
seen.add(v);
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
}
|
|
@@ -7,9 +7,7 @@ import { MCPManager } from './mcp.js';
|
|
|
7
7
|
import { saveKortexConfig } from './config.js';
|
|
8
8
|
import { createConversation as createLocalConversation, saveConversation, loadConversation, listConversations, } from './conversations.js';
|
|
9
9
|
const PLATFORM_AGENTS = [
|
|
10
|
-
{ uuid: '
|
|
11
|
-
{ uuid: 'proka', name: 'Proka', description: 'Standard tier platform assistant' },
|
|
12
|
-
{ uuid: 'prota', name: 'Prota', description: 'Pro tier platform assistant' },
|
|
10
|
+
{ uuid: 'prota', name: 'Prota', description: 'Platform assistant' },
|
|
13
11
|
];
|
|
14
12
|
const MAX_TOOL_ROUNDS = 25;
|
|
15
13
|
const CONTEXT_LIMIT = 128_000;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ptkl/toolkit",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.6.0",
|
|
4
4
|
"description": "A command-line toolkit for managing Protokol platform applications, profiles, functions, and components",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"protokol",
|
|
@@ -37,7 +37,7 @@
|
|
|
37
37
|
"@babel/preset-typescript": "^7.28.5",
|
|
38
38
|
"@babel/standalone": "^7.26.10",
|
|
39
39
|
"@inquirer/password": "^4.0.21",
|
|
40
|
-
"@ptkl/sdk": "^1.
|
|
40
|
+
"@ptkl/sdk": "^1.23.0",
|
|
41
41
|
"@rollup/plugin-babel": "^6.1.0",
|
|
42
42
|
"@rollup/plugin-commonjs": "^29.0.0",
|
|
43
43
|
"@rollup/plugin-node-resolve": "^16.0.3",
|