toolmenu 0.8.0 → 0.10.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 +64 -8
- package/dist/args.d.ts +22 -0
- package/dist/args.js +81 -0
- package/dist/auth.d.ts +77 -0
- package/dist/auth.js +304 -0
- package/dist/auto.d.ts +28 -0
- package/dist/auto.js +81 -0
- package/dist/breakdown.d.ts +30 -0
- package/dist/breakdown.js +63 -0
- package/dist/catalog.d.ts +50 -0
- package/dist/catalog.js +168 -0
- package/dist/cli.js +139 -11
- package/dist/config.d.ts +6 -0
- package/dist/connect.d.ts +1 -0
- package/dist/connect.js +11 -1
- package/dist/diff.js +43 -0
- package/dist/init.d.ts +22 -0
- package/dist/init.js +105 -0
- package/dist/menu.js +2 -0
- package/dist/report.js +34 -2
- package/dist/session.d.ts +10 -0
- package/dist/session.js +7 -1
- package/dist/snapshot.d.ts +3 -0
- package/dist/snapshot.js +4 -0
- package/dist/types.d.ts +2 -0
- package/package.json +1 -1
package/dist/auto.js
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { synthesizeArgs } from './args.js';
|
|
2
|
+
import { COLLECTION_VERBS, LOOKUP_VERBS, verbOf } from './words.js';
|
|
3
|
+
/**
|
|
4
|
+
* A scenario built from the menu, no hand-written steps: every read-only tool
|
|
5
|
+
* whose required arguments the schema can fill, cheapest first (no arguments,
|
|
6
|
+
* then lists and searches, then single lookups, then the rest), then the first
|
|
7
|
+
* call again, which should change nothing. Never a tool that isn't marked
|
|
8
|
+
* readOnlyHint, whatever allow_writes says.
|
|
9
|
+
*/
|
|
10
|
+
export function autoScenario(menu, options = {}) {
|
|
11
|
+
const maxCalls = options.maxCalls ?? 20;
|
|
12
|
+
const skipped = [];
|
|
13
|
+
const candidates = [];
|
|
14
|
+
for (const tool of menu.tools) {
|
|
15
|
+
const a = tool.annotations ?? {};
|
|
16
|
+
if (a.readOnlyHint !== true) {
|
|
17
|
+
skipped.push({ tool: tool.name, reason: 'not read-only' });
|
|
18
|
+
continue;
|
|
19
|
+
}
|
|
20
|
+
// readOnlyHint is the server's own claim, and a read-only search still spends
|
|
21
|
+
// credits on a paid API. Servers that mark a tool openWorldHint: true are the
|
|
22
|
+
// web search, fetch and scraping ones (Exa, Firecrawl, Context7, Playwright):
|
|
23
|
+
// opt-in. Unmarked tools are called: on GitHub, Atlassian or Notion that's
|
|
24
|
+
// every read, of the account the credentials belong to (FINDINGS F13).
|
|
25
|
+
if (a.openWorldHint === true && !options.openWorld) {
|
|
26
|
+
skipped.push({ tool: tool.name, reason: 'open world' });
|
|
27
|
+
continue;
|
|
28
|
+
}
|
|
29
|
+
const filled = synthesizeArgs(tool.inputSchema);
|
|
30
|
+
if (!filled.ok) {
|
|
31
|
+
skipped.push({ tool: tool.name, reason: 'needs values', missing: filled.missing });
|
|
32
|
+
continue;
|
|
33
|
+
}
|
|
34
|
+
candidates.push({ tool, args: filled.args, rank: rank(tool, filled.args) });
|
|
35
|
+
}
|
|
36
|
+
candidates.sort((x, y) => x.rank - y.rank);
|
|
37
|
+
const chosen = candidates.slice(0, maxCalls);
|
|
38
|
+
for (const c of candidates.slice(maxCalls))
|
|
39
|
+
skipped.push({ tool: c.tool.name, reason: 'over the call budget' });
|
|
40
|
+
const steps = [{ kind: 'list' }];
|
|
41
|
+
for (const c of chosen)
|
|
42
|
+
steps.push({ kind: 'call', tool: c.tool.name, args: c.args });
|
|
43
|
+
if (chosen.length)
|
|
44
|
+
steps.push({ kind: 'call', tool: chosen[0].tool.name, args: chosen[0].args });
|
|
45
|
+
return { scenario: { allowWrites: false, steps }, called: chosen.map((c) => c.tool.name), skipped };
|
|
46
|
+
}
|
|
47
|
+
function rank(tool, args) {
|
|
48
|
+
if (Object.keys(args).length === 0)
|
|
49
|
+
return 0;
|
|
50
|
+
const verb = verbOf(tool.name);
|
|
51
|
+
if (verb && COLLECTION_VERBS.has(verb))
|
|
52
|
+
return 1;
|
|
53
|
+
if (verb && LOOKUP_VERBS.has(verb))
|
|
54
|
+
return 2;
|
|
55
|
+
return 3;
|
|
56
|
+
}
|
|
57
|
+
/** The plan as a scenario file, to commit and run again with --scenario. */
|
|
58
|
+
export function scenarioYaml(plan, serverName) {
|
|
59
|
+
const lines = [
|
|
60
|
+
`# Written by \`toolmenu session --auto\`${serverName ? ` for ${serverName}` : ''}: every read-only tool whose`,
|
|
61
|
+
'# required arguments the schema could fill. Edit freely.',
|
|
62
|
+
'allow_writes: false',
|
|
63
|
+
'steps:',
|
|
64
|
+
];
|
|
65
|
+
for (const step of plan.scenario.steps) {
|
|
66
|
+
if (step.kind === 'list')
|
|
67
|
+
lines.push(' - list');
|
|
68
|
+
else if (step.kind === 'call') {
|
|
69
|
+
lines.push(` - call: ${step.tool}`);
|
|
70
|
+
if (Object.keys(step.args).length)
|
|
71
|
+
lines.push(` args: ${JSON.stringify(step.args)}`);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
const needs = plan.skipped.filter((s) => s.reason === 'needs values');
|
|
75
|
+
if (needs.length) {
|
|
76
|
+
lines.push('', ' # Read-only tools that need values the schema doesn\'t give:');
|
|
77
|
+
for (const s of needs.slice(0, 15))
|
|
78
|
+
lines.push(` # - call: ${s.tool}`, ` # args: { ${s.missing.map((m) => `${m}: TODO`).join(', ')} }`);
|
|
79
|
+
}
|
|
80
|
+
return lines.join('\n') + '\n';
|
|
81
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { Menu } from './types.js';
|
|
2
|
+
export interface Breakdown {
|
|
3
|
+
total: number;
|
|
4
|
+
/** The biggest tools, with their share of the menu and what their tokens are spent on. */
|
|
5
|
+
top: {
|
|
6
|
+
name: string;
|
|
7
|
+
tokens: number;
|
|
8
|
+
share: number;
|
|
9
|
+
description: number;
|
|
10
|
+
schema: number;
|
|
11
|
+
}[];
|
|
12
|
+
/** Enums with many values. */
|
|
13
|
+
enums: {
|
|
14
|
+
tool: string;
|
|
15
|
+
param: string;
|
|
16
|
+
values: number;
|
|
17
|
+
tokens: number;
|
|
18
|
+
}[];
|
|
19
|
+
/** The same parameter, byte for byte, in several tools: paid once per tool. */
|
|
20
|
+
repeated: {
|
|
21
|
+
param: string;
|
|
22
|
+
tokens: number;
|
|
23
|
+
tools: number;
|
|
24
|
+
total: number;
|
|
25
|
+
}[];
|
|
26
|
+
}
|
|
27
|
+
/** Where a menu's tokens go. Estimates, like every count in toolmenu. */
|
|
28
|
+
export declare function breakdown(menu: Menu): Breakdown;
|
|
29
|
+
/** The breakdown as report lines, or none for a menu too small to need one. */
|
|
30
|
+
export declare function breakdownLines(b: Breakdown, tools: number): string[];
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { canonical } from './compare.js';
|
|
2
|
+
import { countTokens } from './menu.js';
|
|
3
|
+
const TOP = 5;
|
|
4
|
+
const BIG_ENUM = 20;
|
|
5
|
+
const REPEATED_MIN_TOKENS = 30;
|
|
6
|
+
const REPEATED_MIN_TOOLS = 3;
|
|
7
|
+
/** Where a menu's tokens go. Estimates, like every count in toolmenu. */
|
|
8
|
+
export function breakdown(menu) {
|
|
9
|
+
const total = menu.totalTokens || 1;
|
|
10
|
+
const top = [...menu.tools]
|
|
11
|
+
.sort((a, b) => b.tokens - a.tokens)
|
|
12
|
+
.slice(0, TOP)
|
|
13
|
+
.map((t) => ({
|
|
14
|
+
name: t.name,
|
|
15
|
+
tokens: t.tokens,
|
|
16
|
+
share: t.tokens / total,
|
|
17
|
+
description: t.description ? countTokens(JSON.stringify(t.description)) : 0,
|
|
18
|
+
schema: t.inputSchema ? countTokens(JSON.stringify(t.inputSchema)) : 0,
|
|
19
|
+
}));
|
|
20
|
+
const enums = [];
|
|
21
|
+
const seen = new Map();
|
|
22
|
+
for (const tool of menu.tools) {
|
|
23
|
+
for (const [param, schema] of Object.entries(tool.inputSchema?.properties ?? {})) {
|
|
24
|
+
const values = enumValues(schema);
|
|
25
|
+
if (values.length >= BIG_ENUM)
|
|
26
|
+
enums.push({ tool: tool.name, param, values: values.length, tokens: countTokens(JSON.stringify(values)) });
|
|
27
|
+
const key = `${param}\u0000${canonical(schema)}`;
|
|
28
|
+
const entry = seen.get(key) ?? { param, tokens: countTokens(JSON.stringify({ [param]: schema })), tools: new Set() };
|
|
29
|
+
entry.tools.add(tool.name);
|
|
30
|
+
seen.set(key, entry);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
const repeated = [...seen.values()]
|
|
34
|
+
.filter((e) => e.tokens >= REPEATED_MIN_TOKENS && e.tools.size >= REPEATED_MIN_TOOLS)
|
|
35
|
+
.map((e) => ({ param: e.param, tokens: e.tokens, tools: e.tools.size, total: e.tokens * e.tools.size }))
|
|
36
|
+
.sort((a, b) => b.total - a.total)
|
|
37
|
+
.slice(0, 5);
|
|
38
|
+
enums.sort((a, b) => b.tokens - a.tokens);
|
|
39
|
+
return { total: menu.totalTokens, top, enums: enums.slice(0, 5), repeated };
|
|
40
|
+
}
|
|
41
|
+
function enumValues(schema) {
|
|
42
|
+
if (Array.isArray(schema.enum))
|
|
43
|
+
return schema.enum;
|
|
44
|
+
if (Array.isArray(schema.items?.enum))
|
|
45
|
+
return schema.items.enum;
|
|
46
|
+
return [];
|
|
47
|
+
}
|
|
48
|
+
/** The breakdown as report lines, or none for a menu too small to need one. */
|
|
49
|
+
export function breakdownLines(b, tools) {
|
|
50
|
+
if (tools < 5)
|
|
51
|
+
return [];
|
|
52
|
+
const n = (x) => `~${x.toLocaleString('en-US')}`;
|
|
53
|
+
const pct = (x) => `${Math.round(x * 100)}%`;
|
|
54
|
+
const topShare = b.top.reduce((s, t) => s + t.share, 0);
|
|
55
|
+
const lines = [`Where the tokens go (estimate): the top ${b.top.length} tools are ${pct(topShare)} of the menu`];
|
|
56
|
+
for (const t of b.top)
|
|
57
|
+
lines.push(` ${pct(t.share).padStart(4)} ${t.name} ${n(t.tokens)} (description ${n(t.description)}, schema ${n(t.schema)})`);
|
|
58
|
+
for (const e of b.enums)
|
|
59
|
+
lines.push(` enum: ${e.tool}.${e.param} has ${e.values} values, ${n(e.tokens)} tokens`);
|
|
60
|
+
for (const r of b.repeated)
|
|
61
|
+
lines.push(` repeated: ${r.param} (${n(r.tokens)}) in ${r.tools} tools, ${n(r.total)} in all`);
|
|
62
|
+
return lines;
|
|
63
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import type { Connection } from './connect.js';
|
|
2
|
+
import type { MenuTool } from './types.js';
|
|
3
|
+
/**
|
|
4
|
+
* Operations a server keeps behind a search tool instead of in its menu (search
|
|
5
|
+
* and execute: Sentry's search_sentry_tools, Atlassian's discover). `diff` can't
|
|
6
|
+
* see them in the menu, so snapshot --catalog asks the search tool a fixed set of
|
|
7
|
+
* queries and keeps what comes back.
|
|
8
|
+
*/
|
|
9
|
+
export interface Catalog {
|
|
10
|
+
/** The search tool asked. */
|
|
11
|
+
tool: string;
|
|
12
|
+
/** The queries asked, in order: the same ones every run, so runs compare. */
|
|
13
|
+
queries: string[];
|
|
14
|
+
/** Every operation found, by name. Not complete: a search returns its top matches. */
|
|
15
|
+
operations: MenuTool[];
|
|
16
|
+
/** Queries that failed (rate limits, errors): the catalog is partial where they would have looked. */
|
|
17
|
+
failed?: {
|
|
18
|
+
query: string;
|
|
19
|
+
error: string;
|
|
20
|
+
}[];
|
|
21
|
+
}
|
|
22
|
+
export interface CatalogOptions {
|
|
23
|
+
/** The search tool, when detection picks the wrong one. */
|
|
24
|
+
tool?: string;
|
|
25
|
+
/** The queries to ask. Default: derived from the menu. */
|
|
26
|
+
queries?: string[];
|
|
27
|
+
timeoutMs?: number;
|
|
28
|
+
/** Pause between queries, ms (default 300): hosted servers rate-limit searches (Sentry did). */
|
|
29
|
+
pauseMs?: number;
|
|
30
|
+
}
|
|
31
|
+
/** The menu's catalog search tool: read-only, one required query, named like one. */
|
|
32
|
+
export declare function findCatalogTool(tools: MenuTool[]): MenuTool | undefined;
|
|
33
|
+
/**
|
|
34
|
+
* Queries derived from the menu, the same for the same menu: the nouns in its tool
|
|
35
|
+
* names ("issue", "release", "page"), each asked as "list", "get", "create",
|
|
36
|
+
* "update" and "delete".
|
|
37
|
+
*/
|
|
38
|
+
export declare function defaultQueries(tools: MenuTool[], max?: number): string[];
|
|
39
|
+
/** Ask the search tool every query and collect the operations it returns. */
|
|
40
|
+
export declare function readCatalog(conn: Connection, tools: MenuTool[], options?: CatalogOptions): Promise<Catalog>;
|
|
41
|
+
/**
|
|
42
|
+
* Operation definitions in a search result: objects with a `name` and either a
|
|
43
|
+
* JSON Schema (`inputSchema`, `input_schema`, `parameters`) or a list of
|
|
44
|
+
* `inputs` (Atlassian), found anywhere in structuredContent or in text that
|
|
45
|
+
* parses as JSON.
|
|
46
|
+
*/
|
|
47
|
+
export declare function operationsIn(result: {
|
|
48
|
+
structuredContent?: unknown;
|
|
49
|
+
content?: unknown;
|
|
50
|
+
}): MenuTool[];
|
package/dist/catalog.js
ADDED
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
import { toolTokens } from './menu.js';
|
|
2
|
+
import { nouns, singular } from './words.js';
|
|
3
|
+
const QUERY_PARAM = /^(q|query|search|keywords?|text)$/i;
|
|
4
|
+
const SEARCH_NAME = /(search|find|list|discover)[\w-]*(tools?|operations?|actions?|capabilit)|^discover$|(tools?|operations?)[\w-]*(search|discover)/i;
|
|
5
|
+
/** The menu's catalog search tool: read-only, one required query, named like one. */
|
|
6
|
+
export function findCatalogTool(tools) {
|
|
7
|
+
return tools.find((t) => {
|
|
8
|
+
if (t.annotations?.readOnlyHint !== true)
|
|
9
|
+
return false;
|
|
10
|
+
const required = t.inputSchema?.required ?? [];
|
|
11
|
+
return SEARCH_NAME.test(t.name) && required.length === 1 && QUERY_PARAM.test(required[0]);
|
|
12
|
+
});
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Queries derived from the menu, the same for the same menu: the nouns in its tool
|
|
16
|
+
* names ("issue", "release", "page"), each asked as "list", "get", "create",
|
|
17
|
+
* "update" and "delete".
|
|
18
|
+
*/
|
|
19
|
+
export function defaultQueries(tools, max = 25) {
|
|
20
|
+
const counts = new Map();
|
|
21
|
+
for (const t of tools)
|
|
22
|
+
for (const n of new Set(nouns(t.name).map(singular)))
|
|
23
|
+
counts.set(n, (counts.get(n) ?? 0) + 1);
|
|
24
|
+
const subjects = [...counts].filter(([n]) => n.length > 2).sort((a, b) => b[1] - a[1] || a[0].localeCompare(b[0])).map(([n]) => n);
|
|
25
|
+
const queries = [];
|
|
26
|
+
for (const noun of subjects)
|
|
27
|
+
for (const verb of ['list', 'get', 'create', 'update', 'delete'])
|
|
28
|
+
queries.push(`${verb} ${noun}`);
|
|
29
|
+
return queries.slice(0, max);
|
|
30
|
+
}
|
|
31
|
+
/** Ask the search tool every query and collect the operations it returns. */
|
|
32
|
+
export async function readCatalog(conn, tools, options = {}) {
|
|
33
|
+
const tool = options.tool ? tools.find((t) => t.name === options.tool) : findCatalogTool(tools);
|
|
34
|
+
if (!tool)
|
|
35
|
+
throw new Error(options.tool ? `--catalog: no tool named ${options.tool} in the menu.` : '--catalog: no catalog search tool in the menu (read-only, one required query, named like search_*_tools or discover). Name it with catalog.tool in the config.');
|
|
36
|
+
const queryParam = tool.inputSchema.required[0];
|
|
37
|
+
const limit = limitArg(tool.inputSchema);
|
|
38
|
+
const queries = options.queries ?? defaultQueries(tools);
|
|
39
|
+
const found = new Map();
|
|
40
|
+
const failed = [];
|
|
41
|
+
const pause = options.pauseMs ?? 300;
|
|
42
|
+
for (const [i, query] of queries.entries()) {
|
|
43
|
+
if (i > 0)
|
|
44
|
+
await sleep(pause);
|
|
45
|
+
// A rate limit is waited out (2 s, 4 s, 8 s); anything else, or a limit that
|
|
46
|
+
// persists, leaves this query out and the catalog partial, never the snapshot
|
|
47
|
+
// failed: diff treats operations not found as notices.
|
|
48
|
+
for (let attempt = 0;; attempt++) {
|
|
49
|
+
try {
|
|
50
|
+
const result = await conn.client.callTool({ name: tool.name, arguments: { [queryParam]: query, ...limit } }, { timeout: options.timeoutMs ?? 30_000 });
|
|
51
|
+
const text = result.isError ? resultText(result) : '';
|
|
52
|
+
if (result.isError && RATE_LIMIT.test(text) && attempt < 3) {
|
|
53
|
+
await sleep(2000 * 2 ** attempt);
|
|
54
|
+
continue;
|
|
55
|
+
}
|
|
56
|
+
if (result.isError)
|
|
57
|
+
failed.push({ query, error: text.slice(0, 200) || 'the tool returned an error' });
|
|
58
|
+
else
|
|
59
|
+
for (const op of operationsIn(result))
|
|
60
|
+
if (!found.has(op.name))
|
|
61
|
+
found.set(op.name, op);
|
|
62
|
+
break;
|
|
63
|
+
}
|
|
64
|
+
catch (error) {
|
|
65
|
+
const message = error instanceof Error ? error.message.split('\n')[0] : String(error);
|
|
66
|
+
if (RATE_LIMIT.test(message) && attempt < 3) {
|
|
67
|
+
await sleep(2000 * 2 ** attempt);
|
|
68
|
+
continue;
|
|
69
|
+
}
|
|
70
|
+
failed.push({ query, error: message.slice(0, 200) });
|
|
71
|
+
break;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
const operations = [...found.values()].sort((a, b) => a.name.localeCompare(b.name));
|
|
76
|
+
return { tool: tool.name, queries, operations, ...(failed.length ? { failed } : {}) };
|
|
77
|
+
}
|
|
78
|
+
const RATE_LIMIT = /rate.?limit|too many requests|\b429\b/i;
|
|
79
|
+
function sleep(ms) {
|
|
80
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
81
|
+
}
|
|
82
|
+
function resultText(result) {
|
|
83
|
+
return (Array.isArray(result.content) ? result.content : []).map((c) => c.text ?? '').join(' ').trim();
|
|
84
|
+
}
|
|
85
|
+
/** Ask for as many results as the search allows (Sentry: limit up to 20). */
|
|
86
|
+
function limitArg(schema) {
|
|
87
|
+
for (const [name, s] of Object.entries(schema?.properties ?? {})) {
|
|
88
|
+
if (!/^(limit|max_?results|top_?k|count)$/i.test(name))
|
|
89
|
+
continue;
|
|
90
|
+
const branches = [s, ...(s.anyOf ?? s.oneOf ?? [])];
|
|
91
|
+
const max = branches.map((b) => b.maximum).find((m) => typeof m === 'number');
|
|
92
|
+
if (max)
|
|
93
|
+
return { [name]: max };
|
|
94
|
+
}
|
|
95
|
+
return {};
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Operation definitions in a search result: objects with a `name` and either a
|
|
99
|
+
* JSON Schema (`inputSchema`, `input_schema`, `parameters`) or a list of
|
|
100
|
+
* `inputs` (Atlassian), found anywhere in structuredContent or in text that
|
|
101
|
+
* parses as JSON.
|
|
102
|
+
*/
|
|
103
|
+
export function operationsIn(result) {
|
|
104
|
+
const roots = [];
|
|
105
|
+
if (result.structuredContent)
|
|
106
|
+
roots.push(result.structuredContent);
|
|
107
|
+
for (const part of Array.isArray(result.content) ? result.content : []) {
|
|
108
|
+
const text = part.text;
|
|
109
|
+
if (typeof text !== 'string')
|
|
110
|
+
continue;
|
|
111
|
+
try {
|
|
112
|
+
roots.push(JSON.parse(text));
|
|
113
|
+
}
|
|
114
|
+
catch {
|
|
115
|
+
// prose, not a catalog
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
const out = new Map();
|
|
119
|
+
const walk = (v, depth) => {
|
|
120
|
+
if (depth > 6 || !v || typeof v !== 'object')
|
|
121
|
+
return;
|
|
122
|
+
if (Array.isArray(v))
|
|
123
|
+
return v.forEach((x) => walk(x, depth + 1));
|
|
124
|
+
const op = asOperation(v);
|
|
125
|
+
if (op) {
|
|
126
|
+
out.set(op.name, op);
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
for (const x of Object.values(v))
|
|
130
|
+
walk(x, depth + 1);
|
|
131
|
+
};
|
|
132
|
+
roots.forEach((r) => walk(r, 0));
|
|
133
|
+
return [...out.values()];
|
|
134
|
+
}
|
|
135
|
+
function asOperation(o) {
|
|
136
|
+
if (typeof o.name !== 'string' || !o.name)
|
|
137
|
+
return undefined;
|
|
138
|
+
const schema = (o.inputSchema ?? o.input_schema ?? o.parameters);
|
|
139
|
+
let inputSchema;
|
|
140
|
+
if (schema && typeof schema === 'object' && !Array.isArray(schema))
|
|
141
|
+
inputSchema = schema;
|
|
142
|
+
else if (Array.isArray(o.inputs))
|
|
143
|
+
inputSchema = fromInputs(o.inputs);
|
|
144
|
+
else
|
|
145
|
+
return undefined;
|
|
146
|
+
const def = { name: o.name, ...(typeof o.description === 'string' ? { description: o.description } : {}), inputSchema };
|
|
147
|
+
if (o.annotations && typeof o.annotations === 'object')
|
|
148
|
+
def.annotations = o.annotations;
|
|
149
|
+
return { ...def, tokens: toolTokens(def) };
|
|
150
|
+
}
|
|
151
|
+
/** Atlassian's `inputs: [{ name, type, required, description, minimum, … }]` as a JSON Schema. */
|
|
152
|
+
function fromInputs(inputs) {
|
|
153
|
+
const properties = {};
|
|
154
|
+
const required = [];
|
|
155
|
+
for (const i of inputs) {
|
|
156
|
+
if (typeof i.name !== 'string')
|
|
157
|
+
continue;
|
|
158
|
+
// repairHint stays: it's text the agent reads, so a change to it is a change.
|
|
159
|
+
const { name, required: req, integer, ...rest } = i;
|
|
160
|
+
const p = { ...rest };
|
|
161
|
+
if (integer === true && p.type === 'number')
|
|
162
|
+
p.type = 'integer';
|
|
163
|
+
properties[name] = p;
|
|
164
|
+
if (req === true)
|
|
165
|
+
required.push(name);
|
|
166
|
+
}
|
|
167
|
+
return { type: 'object', properties, ...(required.length ? { required } : {}) };
|
|
168
|
+
}
|