@chapterdev/mcp-dev-studio 0.0.0-stage → 0.1.1
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/LICENSE +21 -0
- package/README.md +79 -2
- package/bin/mcp-dev-studio.js +2 -0
- package/dist/agent/fill.js +69 -0
- package/dist/agent/judge.js +24 -0
- package/dist/agent/loop-skill.js +103 -0
- package/dist/agent/providers.js +162 -0
- package/dist/agent/runner.js +252 -0
- package/dist/agent/starters.js +47 -0
- package/dist/analysis/estimate.js +7 -0
- package/dist/analysis/lint.js +149 -0
- package/dist/analysis/prefill.js +411 -0
- package/dist/analysis/surface.js +268 -0
- package/dist/analysis/tokens.js +69 -0
- package/dist/analytics/aggregate.js +106 -0
- package/dist/analytics/recorder.js +75 -0
- package/dist/analytics/tracker.js +58 -0
- package/dist/cli.js +264 -0
- package/dist/demo/seed.js +138 -0
- package/dist/demo/server.js +282 -0
- package/dist/demo/stdio.js +7 -0
- package/dist/discover.js +317 -0
- package/dist/eval/attempts.js +54 -0
- package/dist/eval/cli.js +183 -0
- package/dist/eval/journeys.js +161 -0
- package/dist/hosts/claude-code.js +403 -0
- package/dist/hosts/codex.js +328 -0
- package/dist/hosts/index.js +85 -0
- package/dist/hosts/models.js +51 -0
- package/dist/mcp/connection.js +330 -0
- package/dist/server/app.js +602 -0
- package/dist/server/listen.js +12 -0
- package/dist/skills/skills.js +203 -0
- package/dist/store-watch.js +75 -0
- package/dist/store.js +327 -0
- package/dist/types.js +3 -0
- package/dist/ui/assets/index-D9hmZSmq.js +55 -0
- package/dist/ui/assets/index-G_SSBQNV.css +2 -0
- package/dist/ui/index.html +17 -0
- package/dist/workspace.js +195 -0
- package/package.json +79 -4
|
@@ -0,0 +1,411 @@
|
|
|
1
|
+
// Pre-fill a tool's test form with values that will actually work.
|
|
2
|
+
//
|
|
3
|
+
// Every input is one of two kinds:
|
|
4
|
+
// free - something you can make up (a limit, a title, a date, a prompt).
|
|
5
|
+
// ref - something that must already exist (a user id, a world's name, an
|
|
6
|
+
// app path). Inventing these just produces "not found".
|
|
7
|
+
//
|
|
8
|
+
// Free fields get a sensible made-up value. Refs only ever get real values,
|
|
9
|
+
// harvested from responses the server has already given (Try it, Playground,
|
|
10
|
+
// test runs, proxied traffic) and from arguments that worked before. When none
|
|
11
|
+
// are known yet, the plan names the tool that would list them.
|
|
12
|
+
import { randomUUID } from 'node:crypto';
|
|
13
|
+
import { classify, singular, words } from './surface.js';
|
|
14
|
+
// ── field kinds ──
|
|
15
|
+
const ID_NAME = /(^|_)(id|ids|uuid|uuids|slug)$|[a-z0-9](Id|ID|Ids|IDs|Uuid|Slug)$/;
|
|
16
|
+
const REF_DESC = /\b(exact name|by (its )?name|by its (id|directory|url|number|path|slug)|saved \w+|one of the (app|project|user|team|workspace)['’]?s?\b|\b(id|ID|IDs)\b|from (list|get|find|search)_\w+)/i;
|
|
17
|
+
const INVENT_DESC = /idempotenc|new uuid|random uuid|client-generated/i;
|
|
18
|
+
/** Prose you write, whatever the description mentions. */
|
|
19
|
+
const FREE_TEXT = /^(prompt|request|instruction|instructions|message|body|content|text|brief|about|description|context|note|notes|comment|reason|query|q|search|title|summary|caption|subject)$/i;
|
|
20
|
+
/** Things that live outside the server (GitHub, Slack…): real, but not listable here. */
|
|
21
|
+
const EXTERNAL = /^(repo|repository|owner|org|organization|username|login|handle|account|channel)$/i;
|
|
22
|
+
function stripRefSuffix(name) {
|
|
23
|
+
return name.replace(/(_?(id|ids|uuid|uuids|slug|name|path|key)|(Id|ID|Ids|IDs|Uuid|Slug|Name|Path|Key))$/, '');
|
|
24
|
+
}
|
|
25
|
+
/** The entity a field points at: appPath → app, demoId → demo, id → the tool's own. */
|
|
26
|
+
export function fieldEntity(name, toolEntity, known = new Set()) {
|
|
27
|
+
const ws = words(stripRefSuffix(name));
|
|
28
|
+
const last = ws[ws.length - 1];
|
|
29
|
+
if (!last)
|
|
30
|
+
return toolEntity;
|
|
31
|
+
// "pullRequest" → "pr" when that's what the server calls it (find_prs).
|
|
32
|
+
const initials = ws.length > 1 ? ws.map((w) => w[0]).join('') : '';
|
|
33
|
+
return !known.has(singular(last)) && known.has(initials) ? initials : singular(last);
|
|
34
|
+
}
|
|
35
|
+
export const isIdField = (name) => ID_NAME.test(name);
|
|
36
|
+
/** Tools a description names as the place a value comes from: "from list_demo_worlds", "as find_prs suggests". */
|
|
37
|
+
function mentionedTools(desc, tools) {
|
|
38
|
+
if (!desc)
|
|
39
|
+
return [];
|
|
40
|
+
const esc = (n) => n.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
41
|
+
return tools.filter((t) => new RegExp(`\\b(from|as|by|via|returned by|listed by|see|in)\\s+\`?${esc(t.name)}\\b`, 'i').test(desc)).map((t) => t.name);
|
|
42
|
+
}
|
|
43
|
+
/** For a bare `id`, the description often names the entity: "The set to promote." */
|
|
44
|
+
function entityFromDescription(desc, known) {
|
|
45
|
+
return words(desc ?? '').map(singular).find((w) => known.has(w));
|
|
46
|
+
}
|
|
47
|
+
function withClasses(ctx) {
|
|
48
|
+
// Two passes, like the product map: entities are the nouns of list/create tools.
|
|
49
|
+
const known = new Set();
|
|
50
|
+
for (const t of ctx.tools) {
|
|
51
|
+
const c = classify(t);
|
|
52
|
+
if (c.stage === 'list' || c.stage === 'create')
|
|
53
|
+
known.add(c.entity);
|
|
54
|
+
}
|
|
55
|
+
return { ...ctx, known, classes: new Map(ctx.tools.map((t) => [t.name, classify(t, undefined, known)])) };
|
|
56
|
+
}
|
|
57
|
+
function sourceTools(ctx, tool, field, schema, entity) {
|
|
58
|
+
const order = { list: 0, read: 2, create: 3, status: 4 };
|
|
59
|
+
const byEntity = ctx.tools
|
|
60
|
+
.filter((t) => t.name !== tool.name && ctx.classes.get(t.name)?.entity === entity && order[ctx.classes.get(t.name).stage] !== undefined)
|
|
61
|
+
.sort((a, b) => order[ctx.classes.get(a.name).stage] - order[ctx.classes.get(b.name).stage])
|
|
62
|
+
.map((t) => t.name);
|
|
63
|
+
return [...new Set([...mentionedTools(schema.description, ctx.tools).filter((n) => n !== tool.name), ...byEntity])];
|
|
64
|
+
}
|
|
65
|
+
function isRef(ctx, tool, field, schema, entity, sources) {
|
|
66
|
+
if (schema.enum || schema.type === 'boolean' || schema.type === 'number' || schema.type === 'integer' || schema.type === 'object')
|
|
67
|
+
return false;
|
|
68
|
+
if (INVENT_DESC.test(schema.description ?? '') || schema.format === 'date' || schema.format === 'date-time' || FREE_TEXT.test(field))
|
|
69
|
+
return false;
|
|
70
|
+
if (EXTERNAL.test(field))
|
|
71
|
+
return true;
|
|
72
|
+
const cls = ctx.classes.get(tool.name);
|
|
73
|
+
// Naming the thing you're creating is yours to make up.
|
|
74
|
+
if (cls?.stage === 'create' && entity === cls.entity && !isIdField(field))
|
|
75
|
+
return false;
|
|
76
|
+
if (isIdField(field))
|
|
77
|
+
return true;
|
|
78
|
+
if (REF_DESC.test(schema.description ?? ''))
|
|
79
|
+
return true;
|
|
80
|
+
// A field named after an entity the server can list ("world", "appPath").
|
|
81
|
+
return sources.some((s) => ctx.classes.get(s)?.stage === 'list');
|
|
82
|
+
}
|
|
83
|
+
const UUID = /\b[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\b/gi;
|
|
84
|
+
const PREFIXED = /\b[a-z]{2,8}_(?=[A-Za-z0-9]*\d)[A-Za-z0-9]{3,}\b/g;
|
|
85
|
+
const URL_RE = /https?:\/\/[^\s)<>"'`]+/g;
|
|
86
|
+
const STOP_HEADS = /^(note|tip|hint|warning|error|next|total|example|e\.g|i\.e|usage|returns?|result|status|summary|nothing|none|no|only|use|set|try|see)$/i;
|
|
87
|
+
const looksId = (s) => /^[0-9a-f]{8}-[0-9a-f]{4}-/i.test(s) || /^[a-z]{2,8}_[A-Za-z0-9]*\d/.test(s) || /^\d{1,12}$/.test(s) || /^[A-Za-z0-9_-]{16,}$/.test(s);
|
|
88
|
+
function parseJson(text) {
|
|
89
|
+
const t = text.trim();
|
|
90
|
+
const fenced = /```(?:json)?\s*\n([\s\S]*?)```/.exec(t);
|
|
91
|
+
for (const s of [t, fenced?.[1]]) {
|
|
92
|
+
if (!s || !/^\s*[[{]/.test(s))
|
|
93
|
+
continue;
|
|
94
|
+
try {
|
|
95
|
+
return JSON.parse(s);
|
|
96
|
+
}
|
|
97
|
+
catch {
|
|
98
|
+
/* not JSON */
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
return undefined;
|
|
102
|
+
}
|
|
103
|
+
const LABEL_KEYS = ['name', 'title', 'label', 'displayName', 'display_name', 'email', 'login', 'slug', 'description'];
|
|
104
|
+
function walkJson(node, out, depth = 0) {
|
|
105
|
+
if (depth > 6 || out.length > 400)
|
|
106
|
+
return;
|
|
107
|
+
if (Array.isArray(node))
|
|
108
|
+
return node.slice(0, 100).forEach((n) => walkJson(n, out, depth + 1));
|
|
109
|
+
if (!node || typeof node !== 'object')
|
|
110
|
+
return;
|
|
111
|
+
const o = node;
|
|
112
|
+
const labelKey = LABEL_KEYS.find((k) => typeof o[k] === 'string');
|
|
113
|
+
// No name-like key: describe it by its first couple of short plain values ("draft · 1200").
|
|
114
|
+
const label = labelKey
|
|
115
|
+
? String(o[labelKey]).slice(0, 80)
|
|
116
|
+
: Object.entries(o)
|
|
117
|
+
.filter(([k, v]) => (typeof v === 'string' || typeof v === 'number') && !isIdField(k) && k !== 'id' && String(v).length <= 40)
|
|
118
|
+
.slice(0, 2)
|
|
119
|
+
.map(([, v]) => String(v))
|
|
120
|
+
.join(' · ') || undefined;
|
|
121
|
+
for (const [k, v] of Object.entries(o)) {
|
|
122
|
+
if (typeof v === 'string' || typeof v === 'number') {
|
|
123
|
+
const s = String(v);
|
|
124
|
+
if (!s || s.length > 200)
|
|
125
|
+
continue;
|
|
126
|
+
out.push({ value: s, key: k, label: k === labelKey ? undefined : label, idLike: isIdField(k) || looksId(s) });
|
|
127
|
+
}
|
|
128
|
+
else
|
|
129
|
+
walkJson(v, out, depth + 1);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
function walkProse(text, out) {
|
|
133
|
+
for (const raw of text.split('\n').slice(0, 300)) {
|
|
134
|
+
const line = raw.replace(/^\s*(?:[-*•]|\d+[.)])\s+/, '').trim();
|
|
135
|
+
if (!line || line.length > 400)
|
|
136
|
+
continue;
|
|
137
|
+
const quoted = /["“]([^"”]{2,80})["”]/.exec(line)?.[1];
|
|
138
|
+
const ids = [...new Set([...(line.match(UUID) ?? []), ...(line.match(PREFIXED) ?? [])])];
|
|
139
|
+
for (const id of ids)
|
|
140
|
+
out.push({ value: id, idLike: true, label: quoted ?? (line.replace(id, '').replace(/^[\s:—–-]+/, '').slice(0, 80) || undefined) });
|
|
141
|
+
if (quoted && ids.length === 1)
|
|
142
|
+
out.push({ value: quoted, idLike: false, label: ids[0] });
|
|
143
|
+
for (const u of line.match(URL_RE) ?? [])
|
|
144
|
+
out.push({ value: u.replace(/[.,;]+$/, ''), idLike: false });
|
|
145
|
+
for (const m of line.matchAll(/`([^`\n]{1,120})`/g))
|
|
146
|
+
out.push({ value: m[1], idLike: looksId(m[1]) });
|
|
147
|
+
// "website (owner/repo)", "Default (default): Gray backdrop", "apps/web — the dashboard"
|
|
148
|
+
const head = /^\*{0,2}([^:(—–\n]{1,60}?)\*{0,2}\s*(?:\(([^()\s]{1,80})\))?\s*(?::|—|–|\s-\s|\(|$)/.exec(line);
|
|
149
|
+
if (head && line !== head[1] && !ids.includes(head[1].trim())) {
|
|
150
|
+
const h = head[1].trim();
|
|
151
|
+
const nWords = h.split(/\s+/).length;
|
|
152
|
+
const rest = line.slice(head[0].length).trim();
|
|
153
|
+
if (h && nWords <= 6 && !STOP_HEADS.test(h.split(/\s+/)[0]) && !/[.!?]$/.test(h))
|
|
154
|
+
out.push({ value: h, label: rest.slice(0, 80) || undefined, idLike: looksId(h) });
|
|
155
|
+
// "Default (default)": the same name twice is one value.
|
|
156
|
+
if (head[2] && head[2].toLowerCase() !== h.toLowerCase())
|
|
157
|
+
out.push({ value: head[2], label: h, idLike: looksId(head[2]) });
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
export function extractValues(text) {
|
|
162
|
+
const out = [];
|
|
163
|
+
const json = parseJson(text);
|
|
164
|
+
if (json !== undefined)
|
|
165
|
+
walkJson(json, out);
|
|
166
|
+
else
|
|
167
|
+
walkProse(text, out);
|
|
168
|
+
return out;
|
|
169
|
+
}
|
|
170
|
+
/** Does a value found in a response fit this field? */
|
|
171
|
+
function fits(f, field, wantsId, json) {
|
|
172
|
+
if (wantsId ? !f.idLike : f.idLike && !json)
|
|
173
|
+
return false;
|
|
174
|
+
if (!json)
|
|
175
|
+
return true;
|
|
176
|
+
const k = f.key ?? '';
|
|
177
|
+
const norm = (s) => s.toLowerCase().replace(/[^a-z0-9]/g, '');
|
|
178
|
+
if (norm(k) === norm(field))
|
|
179
|
+
return true;
|
|
180
|
+
if (wantsId)
|
|
181
|
+
return k === 'id' || isIdField(k);
|
|
182
|
+
const suffix = /(Path|path|Name|name|Slug|slug|Url|url|Key|key)$/.exec(field)?.[1]?.toLowerCase();
|
|
183
|
+
return suffix ? norm(k).endsWith(suffix) : ['name', 'title', 'slug', 'path', 'key'].includes(k);
|
|
184
|
+
}
|
|
185
|
+
// ── making values up ──
|
|
186
|
+
const today = (offsetDays = 0) => new Date(Date.now() + offsetDays * 86400_000).toISOString().slice(0, 10);
|
|
187
|
+
/** A plausible value you can invent, or undefined when there isn't one. */
|
|
188
|
+
export function invent(name, s, entity, required) {
|
|
189
|
+
if (s.default !== undefined)
|
|
190
|
+
return { value: s.default, why: 'default' };
|
|
191
|
+
if (s.const !== undefined)
|
|
192
|
+
return { value: s.const, why: 'fixed' };
|
|
193
|
+
if (Array.isArray(s.examples) && s.examples.length)
|
|
194
|
+
return { value: s.examples[0], why: 'example' };
|
|
195
|
+
if (s.enum?.length)
|
|
196
|
+
return required ? { value: s.enum[0], why: 'first option' } : undefined;
|
|
197
|
+
const n = name.toLowerCase();
|
|
198
|
+
const desc = s.description ?? '';
|
|
199
|
+
const type = Array.isArray(s.type) ? s.type.find((t) => t !== 'null') : s.type;
|
|
200
|
+
const madeUp = (value) => ({ value, why: 'made up' });
|
|
201
|
+
if (type === 'boolean')
|
|
202
|
+
return required ? madeUp(false) : undefined;
|
|
203
|
+
if (type === 'integer' || type === 'number') {
|
|
204
|
+
const min = typeof s.minimum === 'number' ? s.minimum : undefined;
|
|
205
|
+
const max = typeof s.maximum === 'number' ? s.maximum : undefined;
|
|
206
|
+
let v = /limit|max|count|size|per_?page|top|^n$|turns|results/.test(n) ? 5 : /page/.test(n) ? 1 : /offset|skip|cursor/.test(n) ? 0 : /days|hours/.test(n) ? 7 : /amount|price|cents|total/.test(n) ? 1000 : (min ?? 1);
|
|
207
|
+
if (min !== undefined && v < min)
|
|
208
|
+
v = min;
|
|
209
|
+
if (max !== undefined && v > max)
|
|
210
|
+
v = max;
|
|
211
|
+
return madeUp(v);
|
|
212
|
+
}
|
|
213
|
+
if (type === 'array') {
|
|
214
|
+
const items = s.items;
|
|
215
|
+
if (!required)
|
|
216
|
+
return undefined;
|
|
217
|
+
return madeUp(items?.enum?.length ? [items.enum[0]] : []);
|
|
218
|
+
}
|
|
219
|
+
if (type === 'object') {
|
|
220
|
+
if (!required)
|
|
221
|
+
return undefined;
|
|
222
|
+
const props = s.properties ?? {};
|
|
223
|
+
const req = new Set(s.required ?? []);
|
|
224
|
+
const obj = {};
|
|
225
|
+
for (const [k, ps] of Object.entries(props))
|
|
226
|
+
if (req.has(k))
|
|
227
|
+
obj[k] = invent(k, ps, entity, true)?.value ?? '';
|
|
228
|
+
return madeUp(obj);
|
|
229
|
+
}
|
|
230
|
+
// strings
|
|
231
|
+
if (s.format === 'uuid' || INVENT_DESC.test(desc))
|
|
232
|
+
return required ? madeUp(randomUUID()) : undefined;
|
|
233
|
+
if (s.format === 'date-time')
|
|
234
|
+
return madeUp(new Date().toISOString());
|
|
235
|
+
if (s.format === 'date' || /YYYY-MM-DD/.test(desc) || /^(since|from|start|after)(_?date)?$/.test(n))
|
|
236
|
+
return madeUp(/^(since|from|start|after)/.test(n) ? today(-30) : today());
|
|
237
|
+
if (/^(until|to|end|before)(_?date)?$/.test(n))
|
|
238
|
+
return madeUp(today());
|
|
239
|
+
if (!required)
|
|
240
|
+
return undefined;
|
|
241
|
+
if (s.format === 'email' || /email/.test(n))
|
|
242
|
+
return madeUp('test@example.com');
|
|
243
|
+
if (s.format === 'uri' || /url|uri|link|href/.test(n))
|
|
244
|
+
return madeUp('https://example.com');
|
|
245
|
+
if (/phone/.test(n))
|
|
246
|
+
return madeUp('+15555550100');
|
|
247
|
+
if (/colou?r/.test(n))
|
|
248
|
+
return madeUp('#6b5ad6');
|
|
249
|
+
if (/currency/.test(n))
|
|
250
|
+
return madeUp('USD');
|
|
251
|
+
if (/locale|lang/.test(n))
|
|
252
|
+
return madeUp('en');
|
|
253
|
+
if (/branch/.test(n))
|
|
254
|
+
return madeUp('main');
|
|
255
|
+
if (/^(name|title|label|display_?name|subject)$/.test(n) || /name$|title$/.test(n))
|
|
256
|
+
return madeUp(`Test ${entity === 'general' ? 'item' : entity}`);
|
|
257
|
+
if (/^(q|query|search|keywords?|term)$/.test(n))
|
|
258
|
+
return madeUp(entity === 'general' ? 'test' : entity);
|
|
259
|
+
if (/prompt|request|instruction|message|body|content|text|brief|about|description|note|comment|reason|context/.test(n))
|
|
260
|
+
return madeUp(`A quick test from MCP Dev Studio${entity !== 'general' ? ` for this ${entity}` : ''}.`);
|
|
261
|
+
return madeUp('test');
|
|
262
|
+
}
|
|
263
|
+
const egExample = (desc) => /\be\.g\.?,?\s+([^\s,;)]+)/.exec(desc ?? '')?.[1]?.replace(/[.]$/, '');
|
|
264
|
+
// ── the plan ──
|
|
265
|
+
export function planPrefill(toolName, input, depth = 1) {
|
|
266
|
+
const ctx = withClasses(input);
|
|
267
|
+
const tool = ctx.tools.find((t) => t.name === toolName);
|
|
268
|
+
if (!tool)
|
|
269
|
+
return;
|
|
270
|
+
const props = tool.inputSchema?.properties ?? {};
|
|
271
|
+
const required = new Set(tool.inputSchema?.required ?? []);
|
|
272
|
+
const toolEntity = ctx.classes.get(tool.name)?.entity ?? 'general';
|
|
273
|
+
// The last call that worked, preferring the Try-it server (same process as this form).
|
|
274
|
+
const ok = ctx.traffic.filter((e) => !e.isError && e.args && typeof e.args === 'object').sort((a, b) => b.ts.localeCompare(a.ts));
|
|
275
|
+
const last = ok.find((e) => e.tool === tool.name && e.source === 'sandbox') ?? ok.find((e) => e.tool === tool.name);
|
|
276
|
+
const lastArgs = (last?.args ?? {});
|
|
277
|
+
const fields = {};
|
|
278
|
+
for (const [field, schema] of Object.entries(props)) {
|
|
279
|
+
const entity = /^(id|ids|uuid|slug)$/.test(field) ? (entityFromDescription(schema.description, ctx.known) ?? toolEntity) : fieldEntity(field, toolEntity, ctx.known);
|
|
280
|
+
const sources = sourceTools(ctx, tool, field, schema, entity);
|
|
281
|
+
const ref = isRef(ctx, tool, field, schema, entity, sources);
|
|
282
|
+
const req = required.has(field);
|
|
283
|
+
const plan = { kind: ref ? 'ref' : 'free', candidates: [] };
|
|
284
|
+
if (ref) {
|
|
285
|
+
plan.candidates = candidatesFor(ctx, tool, field, entity, sources);
|
|
286
|
+
const eg = egExample(schema.description);
|
|
287
|
+
plan.placeholder = plan.candidates[0] ? undefined : eg ? `e.g. ${eg}` : undefined;
|
|
288
|
+
// Only a list (or a read that needs nothing) can tell you which values exist.
|
|
289
|
+
const lister = sources.find((s) => {
|
|
290
|
+
const t = ctx.tools.find((x) => x.name === s);
|
|
291
|
+
const st = ctx.classes.get(s)?.stage;
|
|
292
|
+
return t && !t.annotations?.destructiveHint && (st === 'list' || (st === 'read' && !(t.inputSchema?.required ?? []).length));
|
|
293
|
+
});
|
|
294
|
+
if (lister && depth > 0) {
|
|
295
|
+
const sub = planPrefill(lister, input, depth - 1);
|
|
296
|
+
const missing = Object.entries(sub?.fields ?? {})
|
|
297
|
+
.filter(([k, f]) => (ctx.tools.find((t) => t.name === lister)?.inputSchema?.required ?? []).includes(k) && f.value === undefined)
|
|
298
|
+
.map(([k]) => k);
|
|
299
|
+
plan.lookup = { tool: lister, ready: missing.length === 0, missing };
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
else if (/^(q|query|search|keywords?|term)$/i.test(field) && !schema.enum) {
|
|
303
|
+
// Search for something that exists: a name the server has shown us.
|
|
304
|
+
const seen = ctx.samples.filter((x) => ctx.classes.get(x.tool)?.entity === toolEntity);
|
|
305
|
+
const names = (x) => extractValues(x.text).filter((f) => !f.idLike && (f.key === undefined || ['name', 'title', 'label', 'displayName', 'display_name'].includes(f.key)));
|
|
306
|
+
plan.candidates = dedupe(seen.flatMap((x) => names(x).map((f) => ({ value: f.value, from: x.tool, at: x.at, count: 1, proven: false }))))
|
|
307
|
+
.filter((c) => !/[@/]|^https?:/.test(c.value) && c.value.length <= 60)
|
|
308
|
+
.slice(0, 20);
|
|
309
|
+
const made = invent(field, schema, toolEntity, req);
|
|
310
|
+
if (req && plan.candidates[0])
|
|
311
|
+
Object.assign(plan, { value: plan.candidates[0].value, why: `a name from ${plan.candidates[0].from}` });
|
|
312
|
+
else if (made && req)
|
|
313
|
+
Object.assign(plan, made);
|
|
314
|
+
}
|
|
315
|
+
else {
|
|
316
|
+
const made = invent(field, schema, toolEntity, req);
|
|
317
|
+
const eg = egExample(schema.description);
|
|
318
|
+
if (made && req)
|
|
319
|
+
Object.assign(plan, made);
|
|
320
|
+
else if (made)
|
|
321
|
+
plan.placeholder = String(typeof made.value === 'object' ? JSON.stringify(made.value) : made.value);
|
|
322
|
+
else if (eg)
|
|
323
|
+
plan.placeholder = `e.g. ${eg}`;
|
|
324
|
+
}
|
|
325
|
+
if (field in lastArgs && lastArgs[field] !== undefined && lastArgs[field] !== '') {
|
|
326
|
+
plan.value = lastArgs[field];
|
|
327
|
+
plan.why = 'last call';
|
|
328
|
+
}
|
|
329
|
+
else if (ref && req && plan.candidates[0]) {
|
|
330
|
+
plan.value = plan.candidates[0].value;
|
|
331
|
+
plan.why = plan.candidates[0].proven ? `worked in ${plan.candidates[0].from.replace(/^used in /, '')}` : `from ${plan.candidates[0].from}`;
|
|
332
|
+
}
|
|
333
|
+
fields[field] = plan;
|
|
334
|
+
}
|
|
335
|
+
return { tool: tool.name, fields, lastCall: last ? { args: lastArgs, at: last.ts, source: last.source } : undefined };
|
|
336
|
+
}
|
|
337
|
+
function candidatesFor(ctx, tool, field, entity, sources) {
|
|
338
|
+
const wantsId = isIdField(field);
|
|
339
|
+
const generic = /^(id|ids|name|slug|key|path)$/.test(field);
|
|
340
|
+
const sameEntity = (t) => ctx.classes.get(t)?.entity === entity;
|
|
341
|
+
const byValue = new Map();
|
|
342
|
+
const add = (value, from, at, proven, label) => {
|
|
343
|
+
if (!value || value.length > 200)
|
|
344
|
+
return;
|
|
345
|
+
const c = byValue.get(value);
|
|
346
|
+
if (c) {
|
|
347
|
+
c.count++;
|
|
348
|
+
c.proven ||= proven;
|
|
349
|
+
if (proven && !c.from.startsWith('used in'))
|
|
350
|
+
c.from = from;
|
|
351
|
+
if (at > c.at)
|
|
352
|
+
c.at = at;
|
|
353
|
+
c.label ??= label;
|
|
354
|
+
}
|
|
355
|
+
else
|
|
356
|
+
byValue.set(value, { value, label, from, at, count: 1, proven });
|
|
357
|
+
};
|
|
358
|
+
// 1. Arguments that worked: same field name anywhere, or an id field of the same entity.
|
|
359
|
+
for (const e of ctx.traffic) {
|
|
360
|
+
if (e.isError || !e.args || typeof e.args !== 'object')
|
|
361
|
+
continue;
|
|
362
|
+
for (const [k, v] of Object.entries(e.args)) {
|
|
363
|
+
const vals = Array.isArray(v) ? v : [v];
|
|
364
|
+
const match = generic ? k === field && sameEntity(e.tool) : k === field || (wantsId && isIdField(k) && fieldEntity(k, ctx.classes.get(e.tool)?.entity ?? '', ctx.known) === entity);
|
|
365
|
+
if (!match)
|
|
366
|
+
continue;
|
|
367
|
+
for (const x of vals)
|
|
368
|
+
if (typeof x === 'string' || typeof x === 'number')
|
|
369
|
+
add(String(x), `used in ${e.tool}`, e.ts, true);
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
// 2. Values in cached responses from the tools that list or create this entity.
|
|
373
|
+
const fromTools = new Set(sources);
|
|
374
|
+
for (const s of ctx.samples) {
|
|
375
|
+
if (!fromTools.has(s.tool) && !(wantsId && sameEntity(s.tool)))
|
|
376
|
+
continue;
|
|
377
|
+
const json = parseJson(s.text) !== undefined;
|
|
378
|
+
for (const f of extractValues(s.text))
|
|
379
|
+
if (fits(f, field, wantsId, json))
|
|
380
|
+
add(f.value, s.tool, s.at, false, f.label);
|
|
381
|
+
}
|
|
382
|
+
return [...byValue.values()]
|
|
383
|
+
.filter((c) => c.value !== tool.name)
|
|
384
|
+
.sort((a, b) => Number(b.proven) - Number(a.proven) || b.at.localeCompare(a.at) || b.count - a.count)
|
|
385
|
+
.slice(0, 30);
|
|
386
|
+
}
|
|
387
|
+
function dedupe(cs) {
|
|
388
|
+
const m = new Map();
|
|
389
|
+
for (const c of cs)
|
|
390
|
+
if (!m.has(c.value))
|
|
391
|
+
m.set(c.value, c);
|
|
392
|
+
return [...m.values()];
|
|
393
|
+
}
|
|
394
|
+
/** Turn form values (strings from inputs) into call arguments, per the schema. */
|
|
395
|
+
export function coerceArgs(tool, values) {
|
|
396
|
+
const out = {};
|
|
397
|
+
for (const [k, s] of Object.entries(tool.inputSchema?.properties ?? {})) {
|
|
398
|
+
const v = values[k];
|
|
399
|
+
if (v === '' || v === undefined || v === null)
|
|
400
|
+
continue;
|
|
401
|
+
const type = Array.isArray(s.type) ? s.type.find((t) => t !== 'null') : s.type;
|
|
402
|
+
if ((type === 'number' || type === 'integer') && typeof v === 'string')
|
|
403
|
+
out[k] = Number(v);
|
|
404
|
+
else if ((type === 'array' || type === 'object') && typeof v === 'string')
|
|
405
|
+
out[k] = JSON.parse(v);
|
|
406
|
+
else
|
|
407
|
+
out[k] = v;
|
|
408
|
+
}
|
|
409
|
+
return out;
|
|
410
|
+
}
|
|
411
|
+
//# sourceMappingURL=prefill.js.map
|