@matatbread/matbot-tool-types 0.3.3 → 0.3.5
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/package.json +5 -5
- package/src/normalize-name.ts +0 -73
package/package.json
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
"matbotRuntime": [
|
|
4
4
|
"node"
|
|
5
5
|
],
|
|
6
|
-
"version": "0.3.
|
|
6
|
+
"version": "0.3.5",
|
|
7
7
|
"engines": {
|
|
8
8
|
"node": ">=24.0.0"
|
|
9
9
|
},
|
|
@@ -36,14 +36,14 @@
|
|
|
36
36
|
"typescript": "^6.0.3"
|
|
37
37
|
},
|
|
38
38
|
"peerDependencies": {
|
|
39
|
-
"@matatbread/matbot-plugin-api": "^0.3.
|
|
40
|
-
"@matatbread/matbot-core": "^0.3.
|
|
39
|
+
"@matatbread/matbot-plugin-api": "^0.3.5",
|
|
40
|
+
"@matatbread/matbot-core": "^0.3.5"
|
|
41
41
|
},
|
|
42
42
|
"devDependencies": {
|
|
43
43
|
"@types/node": "^22.0.0",
|
|
44
44
|
"typescript": "^6.0.3",
|
|
45
|
-
"@matatbread/matbot-plugin-api": "^0.3.
|
|
46
|
-
"@matatbread/matbot-core": "^0.3.
|
|
45
|
+
"@matatbread/matbot-plugin-api": "^0.3.5",
|
|
46
|
+
"@matatbread/matbot-core": "^0.3.5"
|
|
47
47
|
},
|
|
48
48
|
"scripts": {
|
|
49
49
|
"typecheck": "tsc --noEmit"
|
package/src/normalize-name.ts
DELETED
|
@@ -1,73 +0,0 @@
|
|
|
1
|
-
// Force a compiled tool's registered name to the value the compiler dictated.
|
|
2
|
-
//
|
|
3
|
-
// The tool name is compiler-owned metadata — it names the build dir, the package, and the version
|
|
4
|
-
// file — but it also has to appear inside the generated source (the `register({ name })` call, the
|
|
5
|
-
// `ToolContracts` arm key, and the `ToolResultOf<'…'>` literals), so the codegen prompt asks the
|
|
6
|
-
// model to transcribe it. A model that ignores that instruction and names the tool something else
|
|
7
|
-
// (e.g. after the skill) produces source that typechecks and installs but registers under the wrong
|
|
8
|
-
// name — silently, until it clashes with a real tool. The name is not the model's to choose, so this
|
|
9
|
-
// does not detect-and-repair (that would spend repair passes re-rolling a decision that was never
|
|
10
|
-
// the model's); it deterministically rewrites the model's chosen name to the expected one.
|
|
11
|
-
//
|
|
12
|
-
// This is a SYNTACTIC pass (parse + AST walk over one file), not the semantic check — cheap enough
|
|
13
|
-
// for the main thread, unlike the worker-hosted type check whose cost is program-wide analysis.
|
|
14
|
-
|
|
15
|
-
import type TS from 'typescript';
|
|
16
|
-
|
|
17
|
-
export interface NameNormalization { source: string; renamedFrom?: string }
|
|
18
|
-
|
|
19
|
-
/** Rewrite the registered tool name (and its contract-key / ToolResultOf occurrences) to `expected`
|
|
20
|
-
* if the source registered it under a different name. Returns the (possibly unchanged) source and,
|
|
21
|
-
* when a rewrite happened, the name it replaced. */
|
|
22
|
-
export async function normalizeRegisteredName(source: string, expected: string): Promise<NameNormalization> {
|
|
23
|
-
const ts = (await import('typescript')).default as typeof TS;
|
|
24
|
-
const sf = ts.createSourceFile('index.ts', source, ts.ScriptTarget.ES2022, true);
|
|
25
|
-
|
|
26
|
-
// Find the `services.tools.register({ name: <string literal>, … })` call and read its name.
|
|
27
|
-
let actual: string | undefined;
|
|
28
|
-
const findRegister = (node: TS.Node): void => {
|
|
29
|
-
if (actual !== undefined) return;
|
|
30
|
-
if (ts.isCallExpression(node)
|
|
31
|
-
&& ts.isPropertyAccessExpression(node.expression)
|
|
32
|
-
&& node.expression.name.text === 'register'
|
|
33
|
-
&& node.arguments.length > 0
|
|
34
|
-
&& ts.isObjectLiteralExpression(node.arguments[0]!)) {
|
|
35
|
-
for (const p of node.arguments[0].properties) {
|
|
36
|
-
if (ts.isPropertyAssignment(p)
|
|
37
|
-
&& (ts.isIdentifier(p.name) || ts.isStringLiteral(p.name))
|
|
38
|
-
&& p.name.text === 'name'
|
|
39
|
-
&& ts.isStringLiteralLike(p.initializer)) {
|
|
40
|
-
actual = p.initializer.text;
|
|
41
|
-
return;
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
ts.forEachChild(node, findRegister);
|
|
46
|
-
};
|
|
47
|
-
findRegister(sf);
|
|
48
|
-
|
|
49
|
-
if (actual === undefined || actual === expected) return { source };
|
|
50
|
-
|
|
51
|
-
// Collect exact spans to rewrite: every string literal whose *entire* text is the old name (the
|
|
52
|
-
// register name, the ToolResultOf<'…'> / ToolContract literals — never a description sentence,
|
|
53
|
-
// which is never string-equal to an identifier), plus the ToolContracts arm's property-signature
|
|
54
|
-
// key. Rewriting by span (not text substitution) can't touch an unrelated substring.
|
|
55
|
-
const edits: { start: number; end: number }[] = [];
|
|
56
|
-
const collect = (node: TS.Node): void => {
|
|
57
|
-
if (ts.isStringLiteralLike(node) && node.text === actual) {
|
|
58
|
-
edits.push({ start: node.getStart(sf) + 1, end: node.getEnd() - 1 }); // inner text, keep quotes
|
|
59
|
-
} else if (ts.isPropertySignature(node) && node.name && (ts.isIdentifier(node.name) || ts.isStringLiteral(node.name)) && node.name.text === actual) {
|
|
60
|
-
const isQuoted = ts.isStringLiteral(node.name);
|
|
61
|
-
edits.push({ start: node.name.getStart(sf) + (isQuoted ? 1 : 0), end: node.name.getEnd() - (isQuoted ? 1 : 0) });
|
|
62
|
-
}
|
|
63
|
-
ts.forEachChild(node, collect);
|
|
64
|
-
};
|
|
65
|
-
collect(sf);
|
|
66
|
-
|
|
67
|
-
// Only code/type/contract positions are rewritten — a comment (e.g. the template's
|
|
68
|
-
// `// await tool.<name>(params)`) may retain the old name. That is cosmetic: it affects neither
|
|
69
|
-
// runtime, typecheck, nor composition, and the iterate path re-runs this pass, so it self-heals.
|
|
70
|
-
let out = source;
|
|
71
|
-
for (const e of edits.sort((a, b) => b.start - a.start)) out = out.slice(0, e.start) + expected + out.slice(e.end);
|
|
72
|
-
return { source: out, renamedFrom: actual };
|
|
73
|
-
}
|