@ptkl/toolkit 1.2.0 → 1.3.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/dist/commands/forge.js +5 -1
- package/dist/lib/kortexDevPlugin.js +392 -21
- package/package.json +1 -1
package/dist/commands/forge.js
CHANGED
|
@@ -775,7 +775,11 @@ class ForgeCommand {
|
|
|
775
775
|
},
|
|
776
776
|
plugins: [
|
|
777
777
|
forgeDevKernelPlugin(appName, platformUrl, DEV_HUB_PORT),
|
|
778
|
-
kortexDevPlugin(
|
|
778
|
+
kortexDevPlugin({
|
|
779
|
+
host,
|
|
780
|
+
token: Util.getCurrentProfile().token,
|
|
781
|
+
projectEnv: env ?? 'dev',
|
|
782
|
+
}),
|
|
779
783
|
{
|
|
780
784
|
// Console bridge (dev only): when the app runs EMBEDDED (e.g. the
|
|
781
785
|
// Studio's Preview iframe), forward console output + uncaught errors
|
|
@@ -1,21 +1,12 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
* `useKortexTool()` calls silently no-op. This plugin closes that gap:
|
|
8
|
-
*
|
|
9
|
-
* 1. Polyfills `window.kortex` with a fully functional KortexAPI that stores
|
|
10
|
-
* every registered tool, just like production.
|
|
11
|
-
* 2. Injects a floating DevTools overlay so developers can see every
|
|
12
|
-
* registered tool, its schema, policy, and page context in real time.
|
|
13
|
-
* 3. Includes a tool tester — invoke any tool with custom JSON arguments
|
|
14
|
-
* and inspect the return value or error.
|
|
15
|
-
*/
|
|
16
|
-
export function kortexDevPlugin() {
|
|
1
|
+
export function kortexDevPlugin(options = {}) {
|
|
2
|
+
const cfg = JSON.stringify({
|
|
3
|
+
host: options.host || '',
|
|
4
|
+
token: options.token || '',
|
|
5
|
+
projectEnv: options.projectEnv || 'dev',
|
|
6
|
+
});
|
|
17
7
|
// ── Polyfill script (runs BEFORE the app) ──────────────────────────
|
|
18
8
|
const polyfillScript = `(function(){
|
|
9
|
+
var __KTX_CFG = ${cfg};
|
|
19
10
|
if (window.kortex) return; // already provided (e.g. by the real platform runtime)
|
|
20
11
|
|
|
21
12
|
var _tools = [];
|
|
@@ -23,16 +14,317 @@ export function kortexDevPlugin() {
|
|
|
23
14
|
var _helperActive = false;
|
|
24
15
|
var _listeners = [];
|
|
25
16
|
|
|
26
|
-
function notify() {
|
|
17
|
+
function notify(eventName) {
|
|
27
18
|
for (var i = 0; i < _listeners.length; i++) _listeners[i]();
|
|
19
|
+
// Production dispatches these; apps listening for them must work in dev too.
|
|
20
|
+
try {
|
|
21
|
+
window.dispatchEvent(new CustomEvent(eventName || 'kortex:capabilities-changed'));
|
|
22
|
+
} catch (e) { /* older browsers */ }
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
var _trace = [];
|
|
26
|
+
|
|
27
|
+
// ── Parity helpers ────────────────────────────────────────────────
|
|
28
|
+
//
|
|
29
|
+
// These mirror platform-forge/src/kortex/{toolPolicy,kortexTools,kortexRun}.ts.
|
|
30
|
+
// They are duplicated, not shared, because platform-forge is not a published
|
|
31
|
+
// package — so they MUST be updated together. A dev tool that reports
|
|
32
|
+
// different approval behaviour than production is worse than no dev tool.
|
|
33
|
+
|
|
34
|
+
var __KTX_POLICY_TABLE = {
|
|
35
|
+
get_current_page: { category: 'read', risk: 'low', requiresConfirmation: false, fullAccessAuto: true },
|
|
36
|
+
get_project_context: { category: 'read', risk: 'low', requiresConfirmation: false, fullAccessAuto: true }
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
function __ktxInferPolicy(name) {
|
|
40
|
+
if (/^(get_|list_|find_|search_|format_)/.test(name) || /(_info|_status)$/.test(name)) {
|
|
41
|
+
return { category: 'read', risk: 'low', requiresConfirmation: false, fullAccessAuto: true };
|
|
42
|
+
}
|
|
43
|
+
if (/^(delete_|remove_|clear_|archive_)/.test(name)) {
|
|
44
|
+
return { category: 'destructive', risk: 'high', requiresConfirmation: true, fullAccessAuto: false };
|
|
45
|
+
}
|
|
46
|
+
if (/^(run_|test_|execute_)/.test(name)) {
|
|
47
|
+
return { category: 'execute', risk: 'high', requiresConfirmation: true, fullAccessAuto: false };
|
|
48
|
+
}
|
|
49
|
+
if (/^(create_|publish_|top_up_)/.test(name) || /payment/.test(name)) {
|
|
50
|
+
return { category: 'high_risk', risk: 'high', requiresConfirmation: true, fullAccessAuto: false };
|
|
51
|
+
}
|
|
52
|
+
if (/^(save_|refresh_|add_|connect_)/.test(name)) {
|
|
53
|
+
return { category: 'persist', risk: 'medium', requiresConfirmation: true, fullAccessAuto: false };
|
|
54
|
+
}
|
|
55
|
+
return { category: 'write', risk: 'medium', requiresConfirmation: true, fullAccessAuto: false };
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function __ktxPolicyFromSchema(schema) {
|
|
59
|
+
if (!schema) return null;
|
|
60
|
+
var fn = schema.function || {};
|
|
61
|
+
return schema.kortex || schema.x_kortex || schema['x-kortex'] ||
|
|
62
|
+
fn.kortex || fn.x_kortex || fn['x-kortex'] || null;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function __ktxResolvePolicy(name, schema, explicit) {
|
|
66
|
+
var base = __KTX_POLICY_TABLE[name] || __ktxInferPolicy(name || '');
|
|
67
|
+
var fromSchema = __ktxPolicyFromSchema(schema) || {};
|
|
68
|
+
var merged = { name: name, label: name };
|
|
69
|
+
for (var k in base) merged[k] = base[k];
|
|
70
|
+
for (var k2 in fromSchema) merged[k2] = fromSchema[k2];
|
|
71
|
+
if (explicit) for (var k3 in explicit) merged[k3] = explicit[k3];
|
|
72
|
+
return merged;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// Production strips policy hints before the schema reaches the model.
|
|
76
|
+
function __ktxCleanSchema(schema) {
|
|
77
|
+
if (!schema) return schema;
|
|
78
|
+
var clean = {};
|
|
79
|
+
for (var k in schema) if (k !== 'kortex' && k !== 'x_kortex' && k !== 'x-kortex') clean[k] = schema[k];
|
|
80
|
+
if (clean.function && typeof clean.function === 'object') {
|
|
81
|
+
var fn = {};
|
|
82
|
+
for (var f in clean.function) if (f !== 'kortex' && f !== 'x_kortex' && f !== 'x-kortex') fn[f] = clean.function[f];
|
|
83
|
+
clean.function = fn;
|
|
84
|
+
}
|
|
85
|
+
return clean;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function __ktxShouldAutoExecute(policy) {
|
|
89
|
+
if (!policy) return false;
|
|
90
|
+
return policy.requiresConfirmation === false;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// ── Minimal schema validation (mirrors schemaValidate.ts) ─────────
|
|
94
|
+
function __ktxValidate(value, schema, path, errors) {
|
|
95
|
+
if (!schema || typeof schema !== 'object') return;
|
|
96
|
+
var t = schema.type;
|
|
97
|
+
var actual = value === null ? 'null' : (Array.isArray(value) ? 'array' : typeof value);
|
|
98
|
+
if (typeof t === 'string') {
|
|
99
|
+
var ok = t === 'integer'
|
|
100
|
+
? (typeof value === 'number' && value % 1 === 0)
|
|
101
|
+
: (t === 'number' ? typeof value === 'number' : actual === t);
|
|
102
|
+
if (!ok) { errors.push((path || '(root)') + ': expected ' + t + ', got ' + actual); return; }
|
|
103
|
+
}
|
|
104
|
+
if (Array.isArray(schema.enum) && schema.enum.indexOf(value) === -1) {
|
|
105
|
+
errors.push((path || '(root)') + ': must be one of ' + JSON.stringify(schema.enum));
|
|
106
|
+
}
|
|
107
|
+
if (typeof value === 'number') {
|
|
108
|
+
if (typeof schema.minimum === 'number' && value < schema.minimum) errors.push(path + ': must be >= ' + schema.minimum);
|
|
109
|
+
if (typeof schema.maximum === 'number' && value > schema.maximum) errors.push(path + ': must be <= ' + schema.maximum);
|
|
110
|
+
}
|
|
111
|
+
if (Array.isArray(value) && schema.items) {
|
|
112
|
+
for (var i = 0; i < value.length; i++) __ktxValidate(value[i], schema.items, path + '[' + i + ']', errors);
|
|
113
|
+
}
|
|
114
|
+
if (value && typeof value === 'object' && !Array.isArray(value)) {
|
|
115
|
+
if (Array.isArray(schema.required)) {
|
|
116
|
+
for (var r = 0; r < schema.required.length; r++) {
|
|
117
|
+
if (!(schema.required[r] in value)) errors.push((path ? path + '.' : '') + schema.required[r] + ': is required but missing');
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
if (schema.properties) {
|
|
121
|
+
for (var key in schema.properties) {
|
|
122
|
+
if (key in value) __ktxValidate(value[key], schema.properties[key], (path ? path + '.' : '') + key, errors);
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
function __ktxExtractJSON(text) {
|
|
129
|
+
var trimmed = String(text || '').trim();
|
|
130
|
+
var fence = trimmed.match(/\`\`\`(?:json)?\\s*([\\s\\S]*?)\`\`\`/);
|
|
131
|
+
var candidate = fence ? fence[1].trim() : trimmed;
|
|
132
|
+
try { return JSON.parse(candidate); } catch (e) {}
|
|
133
|
+
var start = candidate.search(/[\\[{]/);
|
|
134
|
+
var end = Math.max(candidate.lastIndexOf('}'), candidate.lastIndexOf(']'));
|
|
135
|
+
if (start >= 0 && end > start) {
|
|
136
|
+
try { return JSON.parse(candidate.slice(start, end + 1)); } catch (e2) {}
|
|
137
|
+
}
|
|
138
|
+
return undefined;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
function __ktxErr(code, message) {
|
|
142
|
+
var e = new Error(message);
|
|
143
|
+
e.name = 'KortexRunError';
|
|
144
|
+
e.code = code;
|
|
145
|
+
return e;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
function __ktxTrace(entry) {
|
|
149
|
+
entry.at = new Date().toISOString();
|
|
150
|
+
_trace.unshift(entry);
|
|
151
|
+
if (_trace.length > 50) _trace.pop();
|
|
152
|
+
notify('kortex:dev-trace');
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
var __KTX_RESERVED = ['bash', 'read_file'];
|
|
156
|
+
var __KTX_MAX_ROUNDS = 5;
|
|
157
|
+
|
|
158
|
+
function __ktxRun(request) {
|
|
159
|
+
if (!request || !request.agent) return Promise.reject(__ktxErr('agent_not_found', 'run() requires an agent uuid'));
|
|
160
|
+
if (!request.instructions) return Promise.reject(__ktxErr('upstream', 'run() requires instructions'));
|
|
161
|
+
if (!__KTX_CFG.token || !__KTX_CFG.host) {
|
|
162
|
+
return Promise.reject(__ktxErr('no_access', 'ptkl forge dev has no Kortex session. Run \`ptkl login\` and restart the dev server.'));
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
var selected = [];
|
|
166
|
+
if (request.tools) {
|
|
167
|
+
var all = _tools.filter(function (t) {
|
|
168
|
+
var n = t.schema && t.schema.function && t.schema.function.name;
|
|
169
|
+
return n !== 'get_current_page' && n !== 'get_project_context';
|
|
170
|
+
});
|
|
171
|
+
selected = request.tools === true ? all : all.filter(function (t) {
|
|
172
|
+
return request.tools.indexOf(t.schema.function.name) !== -1;
|
|
173
|
+
});
|
|
174
|
+
for (var i = 0; i < selected.length; i++) {
|
|
175
|
+
if (__KTX_RESERVED.indexOf(selected[i].schema.function.name) !== -1) {
|
|
176
|
+
return Promise.reject(__ktxErr('upstream',
|
|
177
|
+
'Tool "' + selected[i].schema.function.name + '" is reserved — it switches Kortex into CLI mode. Rename it.'));
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
var instructions = request.instructions;
|
|
183
|
+
if (request.schema) {
|
|
184
|
+
instructions += '\\n\\nRespond with a single JSON object matching this JSON Schema exactly. ' +
|
|
185
|
+
'Output only the JSON — no prose, no markdown, no code fences.\\n\\n' +
|
|
186
|
+
JSON.stringify(request.schema, null, 2);
|
|
187
|
+
}
|
|
188
|
+
var inputText = request.input === undefined ? '' : JSON.stringify(request.input, null, 2);
|
|
189
|
+
var messages = inputText
|
|
190
|
+
? [{ role: 'context', content: instructions }, { role: 'user', content: inputText }]
|
|
191
|
+
: [{ role: 'user', content: instructions }];
|
|
192
|
+
|
|
193
|
+
var toolCalls = [];
|
|
194
|
+
var usage = { prompt_tokens: 0, completion_tokens: 0, total_tokens: 0 };
|
|
195
|
+
var rounds = 0;
|
|
196
|
+
var repaired = false;
|
|
197
|
+
var traceSteps = [];
|
|
198
|
+
|
|
199
|
+
function post() {
|
|
200
|
+
var body = { agent_uuid: request.agent, messages: messages };
|
|
201
|
+
if (selected.length) body.tools = selected.map(function (t) { return t.schema; });
|
|
202
|
+
|
|
203
|
+
return fetch(__KTX_CFG.host + '/luma/kortex/v1/chat', {
|
|
204
|
+
method: 'POST',
|
|
205
|
+
headers: {
|
|
206
|
+
'Content-Type': 'application/json',
|
|
207
|
+
'Accept': 'text/event-stream',
|
|
208
|
+
'Authorization': 'Bearer ' + __KTX_CFG.token,
|
|
209
|
+
'X-Project-Env': __KTX_CFG.projectEnv
|
|
210
|
+
},
|
|
211
|
+
body: JSON.stringify(body),
|
|
212
|
+
signal: request.signal
|
|
213
|
+
}).then(function (res) {
|
|
214
|
+
if (!res.ok) {
|
|
215
|
+
return res.text().then(function (t) {
|
|
216
|
+
var msg = t;
|
|
217
|
+
try { var j = JSON.parse(t); msg = j.error || j.message || t; } catch (e) {}
|
|
218
|
+
var code = res.status === 403 ? 'no_access'
|
|
219
|
+
: res.status === 404 ? 'agent_not_found'
|
|
220
|
+
: (res.status === 429 || res.status === 402) ? 'quota_exceeded' : 'upstream';
|
|
221
|
+
throw __ktxErr(code, msg || ('Request failed with status ' + res.status));
|
|
222
|
+
});
|
|
223
|
+
}
|
|
224
|
+
return res.text();
|
|
225
|
+
}).then(function (raw) {
|
|
226
|
+
var text = '', calls = [];
|
|
227
|
+
raw.split('\\n').forEach(function (line) {
|
|
228
|
+
if (line.indexOf('data: ') !== 0) return;
|
|
229
|
+
var payload = line.slice(6);
|
|
230
|
+
if (payload === '[DONE]') return;
|
|
231
|
+
try {
|
|
232
|
+
var d = JSON.parse(payload);
|
|
233
|
+
if (typeof d.delta === 'string') text += d.delta;
|
|
234
|
+
if (typeof d.content === 'string' && d.content) text = d.content;
|
|
235
|
+
if (Array.isArray(d.tool_calls)) calls = d.tool_calls;
|
|
236
|
+
if (d.token_usage) {
|
|
237
|
+
usage.prompt_tokens = d.token_usage.prompt_tokens || usage.prompt_tokens;
|
|
238
|
+
usage.completion_tokens = d.token_usage.completion_tokens || usage.completion_tokens;
|
|
239
|
+
usage.total_tokens = d.token_usage.total_tokens || usage.total_tokens;
|
|
240
|
+
}
|
|
241
|
+
} catch (e) {}
|
|
242
|
+
});
|
|
243
|
+
return { text: text, calls: calls };
|
|
244
|
+
});
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
function loop() {
|
|
248
|
+
return post().then(function (turn) {
|
|
249
|
+
if (turn.calls.length) {
|
|
250
|
+
rounds++;
|
|
251
|
+
if (rounds > __KTX_MAX_ROUNDS) throw __ktxErr('upstream', 'Exceeded ' + __KTX_MAX_ROUNDS + ' tool rounds.');
|
|
252
|
+
messages.push({ role: 'assistant', content: turn.text, tool_calls: turn.calls });
|
|
253
|
+
|
|
254
|
+
return turn.calls.reduce(function (chain, tc) {
|
|
255
|
+
return chain.then(function () {
|
|
256
|
+
var name = tc.function && tc.function.name;
|
|
257
|
+
var args = {};
|
|
258
|
+
try { args = JSON.parse((tc.function && tc.function.arguments) || '{}'); } catch (e) {}
|
|
259
|
+
var entry = _tools.filter(function (t) { return t.schema.function.name === name; })[0];
|
|
260
|
+
if (!entry) {
|
|
261
|
+
messages.push({ role: 'tool', content: JSON.stringify({ error: 'Unknown tool: ' + name }), tool_call_id: tc.id });
|
|
262
|
+
return;
|
|
263
|
+
}
|
|
264
|
+
if (!__ktxShouldAutoExecute(entry.policy)) {
|
|
265
|
+
console.warn('[kortex dev] "' + name + '" requires confirmation in production (' +
|
|
266
|
+
entry.policy.category + '/' + entry.policy.risk + '). Auto-running in dev — verify approval behaviour in platform-forge.');
|
|
267
|
+
}
|
|
268
|
+
return Promise.resolve(entry.execute(args)).then(function (result) {
|
|
269
|
+
toolCalls.push({ name: name, arguments: args, result: result });
|
|
270
|
+
traceSteps.push({ tool: name, arguments: args, result: result, policy: entry.policy });
|
|
271
|
+
messages.push({ role: 'tool', content: JSON.stringify(result === undefined ? null : result), tool_call_id: tc.id });
|
|
272
|
+
}, function (err) {
|
|
273
|
+
var e = { error: String((err && err.message) || err) };
|
|
274
|
+
toolCalls.push({ name: name, arguments: args, result: e });
|
|
275
|
+
traceSteps.push({ tool: name, arguments: args, result: e, policy: entry.policy });
|
|
276
|
+
messages.push({ role: 'tool', content: JSON.stringify(e), tool_call_id: tc.id });
|
|
277
|
+
});
|
|
278
|
+
});
|
|
279
|
+
}, Promise.resolve()).then(loop);
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
if (!request.schema) {
|
|
283
|
+
return { data: null, text: turn.text, usage: usage, toolCalls: toolCalls };
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
var parsed = __ktxExtractJSON(turn.text);
|
|
287
|
+
var errors = [];
|
|
288
|
+
if (parsed === undefined) errors.push('response was not valid JSON');
|
|
289
|
+
else __ktxValidate(parsed, request.schema, '', errors);
|
|
290
|
+
|
|
291
|
+
if (!errors.length) return { data: parsed, text: turn.text, usage: usage, toolCalls: toolCalls };
|
|
292
|
+
if (repaired) {
|
|
293
|
+
var e = __ktxErr('schema_invalid', 'Model output did not match the schema after one repair attempt.');
|
|
294
|
+
e.details = errors.join('\\n');
|
|
295
|
+
throw e;
|
|
296
|
+
}
|
|
297
|
+
repaired = true;
|
|
298
|
+
messages.push({ role: 'assistant', content: turn.text });
|
|
299
|
+
messages.push({ role: 'user', content: 'That response did not satisfy the schema:\\n' + errors.join('\\n') +
|
|
300
|
+
'\\n\\nReturn corrected JSON only — no prose, no code fences.' });
|
|
301
|
+
return loop();
|
|
302
|
+
});
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
return loop().then(function (result) {
|
|
306
|
+
__ktxTrace({ name: request.name, request: request, steps: traceSteps, result: result });
|
|
307
|
+
return result;
|
|
308
|
+
}, function (err) {
|
|
309
|
+
__ktxTrace({ name: request.name, request: request, steps: traceSteps, error: { code: err.code, message: err.message, details: err.details } });
|
|
310
|
+
throw err;
|
|
311
|
+
});
|
|
28
312
|
}
|
|
29
313
|
|
|
30
314
|
window.kortex = {
|
|
315
|
+
// Must track platform-forge's KORTEX_BRIDGE_VERSION. v2 adds run().
|
|
316
|
+
version: 2,
|
|
317
|
+
capabilities: ['tools', 'context', 'helper', 'run'],
|
|
318
|
+
|
|
31
319
|
get tools() { return _tools; },
|
|
32
320
|
|
|
33
321
|
_context: null,
|
|
34
322
|
_helperActive: false,
|
|
35
323
|
|
|
324
|
+
_trace: _trace,
|
|
325
|
+
|
|
326
|
+
run: function (request) { return __ktxRun(request); },
|
|
327
|
+
|
|
36
328
|
registerTool: function (schema, executeFn, policy) {
|
|
37
329
|
// Remove any existing tool with the same name
|
|
38
330
|
var name = schema && schema.function && schema.function.name;
|
|
@@ -42,9 +334,9 @@ export function kortexDevPlugin() {
|
|
|
42
334
|
}
|
|
43
335
|
}
|
|
44
336
|
_tools.push({
|
|
45
|
-
schema: schema,
|
|
337
|
+
schema: __ktxCleanSchema(schema),
|
|
46
338
|
execute: executeFn,
|
|
47
|
-
policy:
|
|
339
|
+
policy: __ktxResolvePolicy(name, schema, policy)
|
|
48
340
|
});
|
|
49
341
|
_context = window.kortex._context;
|
|
50
342
|
_helperActive = window.kortex._helperActive;
|
|
@@ -80,13 +372,13 @@ export function kortexDevPlugin() {
|
|
|
80
372
|
showHelper: function () {
|
|
81
373
|
window.kortex._helperActive = true;
|
|
82
374
|
_helperActive = true;
|
|
83
|
-
notify();
|
|
375
|
+
notify('kortex:helper-changed');
|
|
84
376
|
},
|
|
85
377
|
|
|
86
378
|
hideHelper: function () {
|
|
87
379
|
window.kortex._helperActive = false;
|
|
88
380
|
_helperActive = false;
|
|
89
|
-
notify();
|
|
381
|
+
notify('kortex:helper-changed');
|
|
90
382
|
},
|
|
91
383
|
|
|
92
384
|
// Internal: subscribe to changes (used by the devtools panel)
|
|
@@ -183,6 +475,14 @@ export function kortexDevPlugin() {
|
|
|
183
475
|
'}',
|
|
184
476
|
'.ktx-panel-body::-webkit-scrollbar { width: 6px; }',
|
|
185
477
|
'.ktx-panel-body::-webkit-scrollbar-thumb { background: #45475a; border-radius: 3px; }',
|
|
478
|
+
'.ktx-run-box { padding: 10px; border-bottom: 1px solid rgba(255,255,255,0.08); display: flex; flex-direction: column; gap: 6px; }',
|
|
479
|
+
'.ktx-run-input { width: 100%; box-sizing: border-box; background: rgba(255,255,255,0.06); color: inherit; border: 1px solid rgba(255,255,255,0.12); border-radius: 6px; padding: 6px 8px; font: inherit; font-size: 12px; }',
|
|
480
|
+
'.ktx-run-check { font-size: 11px; opacity: 0.75; display: flex; align-items: center; gap: 6px; }',
|
|
481
|
+
'.ktx-run-go { align-self: flex-start; cursor: pointer; border: 0; border-radius: 6px; padding: 5px 12px; font-size: 12px; background: #4f7cff; color: #fff; }',
|
|
482
|
+
'.ktx-run-status { font-size: 11px; color: #ff8888; min-height: 14px; }',
|
|
483
|
+
'.ktx-step { font-size: 11px; margin-top: 6px; padding-left: 8px; border-left: 2px solid rgba(255,255,255,0.12); }',
|
|
484
|
+
'.ktx-step pre { margin: 4px 0 0; white-space: pre-wrap; word-break: break-word; font-size: 10px; opacity: 0.85; }',
|
|
485
|
+
'.ktx-err { border-left-color: #ff5c5c; color: #ff9c9c; }',
|
|
186
486
|
'.ktx-empty {',
|
|
187
487
|
' text-align: center; padding: 32px 16px; color: #585b70;',
|
|
188
488
|
'}',
|
|
@@ -286,6 +586,7 @@ export function kortexDevPlugin() {
|
|
|
286
586
|
' <div class="ktx-tabs">',
|
|
287
587
|
' <button class="ktx-tab active" data-tab="tools">Tools (<span id="ktx-tool-count">0</span>)</button>',
|
|
288
588
|
' <button class="ktx-tab" data-tab="context">Context</button>',
|
|
589
|
+
' <button class="ktx-tab" data-tab="runs">Runs (<span id="ktx-run-count">0</span>)</button>',
|
|
289
590
|
' </div>',
|
|
290
591
|
' <button class="ktx-close-btn" id="ktx-close">×</button>',
|
|
291
592
|
'</div>',
|
|
@@ -341,13 +642,83 @@ export function kortexDevPlugin() {
|
|
|
341
642
|
var body = document.getElementById('ktx-body');
|
|
342
643
|
if (!body) return;
|
|
343
644
|
|
|
645
|
+
var runCountEl = document.getElementById('ktx-run-count');
|
|
646
|
+
var trace = (window.kortex && window.kortex._trace) ? window.kortex._trace : [];
|
|
647
|
+
if (runCountEl) runCountEl.textContent = trace.length;
|
|
648
|
+
|
|
344
649
|
if (activeTab === 'tools') {
|
|
345
650
|
renderTools(body, tools);
|
|
651
|
+
} else if (activeTab === 'runs') {
|
|
652
|
+
renderRuns(body, trace);
|
|
346
653
|
} else {
|
|
347
654
|
renderContext(body, ctx);
|
|
348
655
|
}
|
|
349
656
|
}
|
|
350
657
|
|
|
658
|
+
function esc(v) {
|
|
659
|
+
return String(v).replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');
|
|
660
|
+
}
|
|
661
|
+
|
|
662
|
+
// The point of this panel: a developer needs to see whether the model chose
|
|
663
|
+
// their tool and what arguments it invented — a chat transcript does not
|
|
664
|
+
// show that. Prompt box drives a real /chat round-trip through the same
|
|
665
|
+
// run() the app uses.
|
|
666
|
+
function renderRuns(body, trace) {
|
|
667
|
+
var html = '<div class="ktx-run-box">' +
|
|
668
|
+
'<input id="ktx-run-agent" class="ktx-run-input" placeholder="agent uuid (required)" />' +
|
|
669
|
+
'<textarea id="ktx-run-prompt" class="ktx-run-input" rows="2" placeholder="Ask something — does the model call your tool?"></textarea>' +
|
|
670
|
+
'<label class="ktx-run-check"><input type="checkbox" id="ktx-run-tools" checked /> expose registered tools</label>' +
|
|
671
|
+
'<button id="ktx-run-go" class="ktx-run-go">Run</button>' +
|
|
672
|
+
'<div id="ktx-run-status" class="ktx-run-status"></div>' +
|
|
673
|
+
'</div>';
|
|
674
|
+
|
|
675
|
+
if (!trace.length) {
|
|
676
|
+
html += '<div class="ktx-empty">No runs yet.<br><span style="font-size:11px">Trigger an action from your app, or use the box above.</span></div>';
|
|
677
|
+
}
|
|
678
|
+
|
|
679
|
+
for (var i = 0; i < trace.length; i++) {
|
|
680
|
+
var t = trace[i];
|
|
681
|
+
html += '<div class="ktx-tool">';
|
|
682
|
+
html += '<div class="ktx-tool-name">' + esc(t.name || '(unnamed)') + '</div>';
|
|
683
|
+
html += '<div class="ktx-tool-desc">' + esc(t.at) + '</div>';
|
|
684
|
+
if (t.steps && t.steps.length) {
|
|
685
|
+
for (var j = 0; j < t.steps.length; j++) {
|
|
686
|
+
var st = t.steps[j];
|
|
687
|
+
html += '<div class="ktx-step"><b>tool</b> ' + esc(st.tool) +
|
|
688
|
+
' <span class="ktx-badge">' + esc(st.policy && st.policy.category) + '/' + esc(st.policy && st.policy.risk) + '</span>' +
|
|
689
|
+
'<pre>args: ' + esc(JSON.stringify(st.arguments)) + '\\nresult: ' + esc(JSON.stringify(st.result)) + '</pre></div>';
|
|
690
|
+
}
|
|
691
|
+
} else {
|
|
692
|
+
html += '<div class="ktx-step"><i>no tool calls</i></div>';
|
|
693
|
+
}
|
|
694
|
+
if (t.error) {
|
|
695
|
+
html += '<div class="ktx-step ktx-err"><b>' + esc(t.error.code) + '</b> ' + esc(t.error.message) +
|
|
696
|
+
(t.error.details ? '<pre>' + esc(t.error.details) + '</pre>' : '') + '</div>';
|
|
697
|
+
} else if (t.result) {
|
|
698
|
+
html += '<div class="ktx-step"><b>result</b><pre>' +
|
|
699
|
+
esc(t.result.data !== null && t.result.data !== undefined ? JSON.stringify(t.result.data, null, 2) : t.result.text) +
|
|
700
|
+
'</pre></div>';
|
|
701
|
+
}
|
|
702
|
+
html += '</div>';
|
|
703
|
+
}
|
|
704
|
+
|
|
705
|
+
body.innerHTML = html;
|
|
706
|
+
|
|
707
|
+
var go = document.getElementById('ktx-run-go');
|
|
708
|
+
if (go) go.onclick = function (e) {
|
|
709
|
+
e.stopPropagation();
|
|
710
|
+
var agent = (document.getElementById('ktx-run-agent') || {}).value || '';
|
|
711
|
+
var prompt = (document.getElementById('ktx-run-prompt') || {}).value || '';
|
|
712
|
+
var useTools = (document.getElementById('ktx-run-tools') || {}).checked;
|
|
713
|
+
var status = document.getElementById('ktx-run-status');
|
|
714
|
+
if (!agent || !prompt) { if (status) status.textContent = 'agent uuid and prompt are both required'; return; }
|
|
715
|
+
if (status) status.textContent = 'running…';
|
|
716
|
+
window.kortex.run({ name: 'devtools-prompt', agent: agent, instructions: prompt, tools: !!useTools })
|
|
717
|
+
.then(function () { if (status) status.textContent = ''; })
|
|
718
|
+
.catch(function (err) { if (status) status.textContent = (err.code || 'error') + ': ' + err.message; });
|
|
719
|
+
};
|
|
720
|
+
}
|
|
721
|
+
|
|
351
722
|
function renderTools(body, tools) {
|
|
352
723
|
if (tools.length === 0) {
|
|
353
724
|
body.innerHTML = '<div class="ktx-empty">No tools registered yet.<br><span style="font-size:11px">Call <code>registerTool()</code> or <code>useKortexTool()</code> from your app.</span></div>';
|