@bitbybit-dev/mcp 1.4.1 → 1.5.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 +3 -3
- package/dist/descriptions.d.ts.map +1 -1
- package/dist/descriptions.js +10 -5
- package/dist/descriptions.js.map +1 -1
- package/dist/guides-split.d.ts +6 -0
- package/dist/guides-split.d.ts.map +1 -1
- package/dist/guides-split.js +17 -3
- package/dist/guides-split.js.map +1 -1
- package/dist/guides.generated.d.ts +4 -3
- package/dist/guides.generated.d.ts.map +1 -1
- package/dist/guides.generated.js +249 -17
- package/dist/guides.generated.js.map +1 -1
- package/dist/index-loader.d.ts.map +1 -1
- package/dist/index-loader.js +10 -5
- package/dist/index-loader.js.map +1 -1
- package/dist/index-reader.d.ts +11 -1
- package/dist/index-reader.d.ts.map +1 -1
- package/dist/index-reader.js +87 -22
- package/dist/index-reader.js.map +1 -1
- package/dist/index-shape.d.ts.map +1 -1
- package/dist/index-shape.js +4 -2
- package/dist/index-shape.js.map +1 -1
- package/dist/index-types.d.ts +7 -0
- package/dist/index-types.d.ts.map +1 -1
- package/dist/index-url.d.ts.map +1 -1
- package/dist/index-url.js +2 -1
- package/dist/index-url.js.map +1 -1
- package/dist/installed-version.d.ts.map +1 -1
- package/dist/installed-version.js +14 -7
- package/dist/installed-version.js.map +1 -1
- package/dist/json-schema.d.ts.map +1 -1
- package/dist/json-schema.js +16 -8
- package/dist/json-schema.js.map +1 -1
- package/dist/registry.d.ts.map +1 -1
- package/dist/registry.js +6 -3
- package/dist/registry.js.map +1 -1
- package/dist/render.d.ts +3 -1
- package/dist/render.d.ts.map +1 -1
- package/dist/render.js +55 -21
- package/dist/render.js.map +1 -1
- package/dist/server.d.ts.map +1 -1
- package/dist/server.js +4 -2
- package/dist/server.js.map +1 -1
- package/dist/stdio.js +4 -2
- package/dist/stdio.js.map +1 -1
- package/dist/tools/describe.d.ts.map +1 -1
- package/dist/tools/describe.js +13 -3
- package/dist/tools/describe.js.map +1 -1
- package/dist/tools/fetch.d.ts.map +1 -1
- package/dist/tools/fetch.js +3 -2
- package/dist/tools/fetch.js.map +1 -1
- package/dist/tools/get-examples.d.ts.map +1 -1
- package/dist/tools/get-examples.js +8 -4
- package/dist/tools/get-examples.js.map +1 -1
- package/dist/tools/get-guide.d.ts.map +1 -1
- package/dist/tools/get-guide.js +52 -7
- package/dist/tools/get-guide.js.map +1 -1
- package/package.json +1 -1
- package/server.json +2 -2
- package/src/__fixtures__/index.sample.json +261 -0
- package/src/__fixtures__/load.ts +1 -0
- package/src/descriptions.ts +10 -5
- package/src/guides-split.ts +27 -3
- package/src/guides.generated.ts +249 -17
- package/src/guides.test.ts +41 -7
- package/src/index-loader.test.ts +3 -1
- package/src/index-loader.ts +15 -5
- package/src/index-reader.ts +116 -21
- package/src/index-shape.ts +6 -2
- package/src/index-types.ts +8 -0
- package/src/index-url.ts +3 -1
- package/src/installed-version.test.ts +3 -1
- package/src/installed-version.ts +21 -7
- package/src/json-schema.ts +21 -7
- package/src/registry.ts +9 -3
- package/src/render.test.ts +153 -1
- package/src/render.ts +78 -20
- package/src/server.ts +6 -2
- package/src/stdio.test.ts +15 -5
- package/src/stdio.ts +6 -2
- package/src/tools/describe.ts +13 -3
- package/src/tools/docs-tools.test.ts +57 -1
- package/src/tools/fetch.ts +4 -2
- package/src/tools/get-examples.ts +12 -4
- package/src/tools/get-guide.ts +54 -7
package/src/index-loader.ts
CHANGED
|
@@ -28,16 +28,26 @@ export async function loadIndex(options: LoadIndexOptions): Promise<ApiIndex> {
|
|
|
28
28
|
const cacheFile = cacheDir === null ? null : join(cacheDir, `index-v${options.version}.json`);
|
|
29
29
|
if (cacheFile !== null) {
|
|
30
30
|
const cached = await readCached(cacheFile, options.version);
|
|
31
|
-
if (cached)
|
|
31
|
+
if (cached) {
|
|
32
|
+
return cached;
|
|
33
|
+
}
|
|
32
34
|
}
|
|
33
35
|
const doFetch = options.fetch ?? fetch;
|
|
34
36
|
const response = await doFetch(url);
|
|
35
|
-
if (response.status === NOT_FOUND)
|
|
36
|
-
|
|
37
|
+
if (response.status === NOT_FOUND) {
|
|
38
|
+
throw new IndexNotPublishedError(options.version, url);
|
|
39
|
+
}
|
|
40
|
+
if (!response.ok) {
|
|
41
|
+
throw new Error(`Fetching ${url} failed with HTTP ${response.status}`);
|
|
42
|
+
}
|
|
37
43
|
const text = await response.text();
|
|
38
44
|
const parsed: unknown = JSON.parse(text);
|
|
39
|
-
if (!isApiIndex(parsed, options.version))
|
|
40
|
-
|
|
45
|
+
if (!isApiIndex(parsed, options.version)) {
|
|
46
|
+
throw new Error(`${url} is not an API index for version ${options.version}`);
|
|
47
|
+
}
|
|
48
|
+
if (cacheFile !== null && cacheDir !== null) {
|
|
49
|
+
await writeCached(cacheDir, cacheFile, text);
|
|
50
|
+
}
|
|
41
51
|
return parsed;
|
|
42
52
|
}
|
|
43
53
|
|
package/src/index-reader.ts
CHANGED
|
@@ -1,10 +1,15 @@
|
|
|
1
|
-
import type { ApiIndex, Engine, IndexExample, IndexMember } from "./index-types.js";
|
|
1
|
+
import type { ApiIndex, Engine, IndexExample, IndexMember, IndexType } from "./index-types.js";
|
|
2
2
|
|
|
3
3
|
export interface SearchHit {
|
|
4
4
|
member: IndexMember;
|
|
5
5
|
score: number;
|
|
6
6
|
}
|
|
7
7
|
|
|
8
|
+
export interface NamedType {
|
|
9
|
+
key: string;
|
|
10
|
+
type: IndexType;
|
|
11
|
+
}
|
|
12
|
+
|
|
8
13
|
export interface SearchOptions {
|
|
9
14
|
engine?: Engine | undefined;
|
|
10
15
|
limit?: number | undefined;
|
|
@@ -24,6 +29,8 @@ const MAX_LIMIT = 50;
|
|
|
24
29
|
const NEAREST_LIMIT = 5;
|
|
25
30
|
const MIN_EDIT_TOLERANCE = 2;
|
|
26
31
|
const EDIT_TOLERANCE_DIVISOR = 3;
|
|
32
|
+
const TYPE_NAME = /[A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*)*/g;
|
|
33
|
+
const INPUTS = "Inputs.";
|
|
27
34
|
|
|
28
35
|
export function tokenize(text: string): string[] {
|
|
29
36
|
return text
|
|
@@ -33,6 +40,16 @@ export function tokenize(text: string): string[] {
|
|
|
33
40
|
.filter((token) => token.length > 0);
|
|
34
41
|
}
|
|
35
42
|
|
|
43
|
+
function namespaceOf(typeText: string): string | undefined {
|
|
44
|
+
const name = typeText.match(TYPE_NAME)?.[0] ?? "";
|
|
45
|
+
const dot = name.lastIndexOf(".");
|
|
46
|
+
return dot > 0 ? name.slice(0, dot) : undefined;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function uniqueTypes(found: readonly NamedType[]): NamedType[] {
|
|
50
|
+
return [...new Map(found.map((named) => [named.key, named])).values()];
|
|
51
|
+
}
|
|
52
|
+
|
|
36
53
|
interface MemberTokens {
|
|
37
54
|
path: Set<string>;
|
|
38
55
|
name: Set<string>;
|
|
@@ -46,9 +63,13 @@ export class IndexReader {
|
|
|
46
63
|
readonly version: string;
|
|
47
64
|
private readonly byPath = new Map<string, IndexMember>();
|
|
48
65
|
private readonly tokens = new Map<string, MemberTokens>();
|
|
66
|
+
private readonly typeKeysByName = new Map<string, string[]>();
|
|
49
67
|
|
|
50
68
|
constructor(readonly index: ApiIndex) {
|
|
51
69
|
this.version = index.version;
|
|
70
|
+
for (const [key, type] of Object.entries(index.types ?? {})) {
|
|
71
|
+
this.typeKeysByName.set(type.name, [...(this.typeKeysByName.get(type.name) ?? []), key]);
|
|
72
|
+
}
|
|
52
73
|
for (const member of index.members) {
|
|
53
74
|
this.byPath.set(member.path, member);
|
|
54
75
|
this.tokens.set(member.path, {
|
|
@@ -63,6 +84,40 @@ export class IndexReader {
|
|
|
63
84
|
return this.byPath.get(path);
|
|
64
85
|
}
|
|
65
86
|
|
|
87
|
+
type(name: string, near?: string): NamedType | undefined {
|
|
88
|
+
const types = this.index.types ?? {};
|
|
89
|
+
const bare = name.replace(/^Bit\./, "").replace(/\[\]$/, "");
|
|
90
|
+
for (const key of [bare, ...(near === undefined ? [] : [`${near}.${bare}`]), `${INPUTS}${bare}`]) {
|
|
91
|
+
const type = types[key];
|
|
92
|
+
if (type) {
|
|
93
|
+
return { key, type };
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
const keys = this.typeKeysByName.get(bare) ?? [];
|
|
97
|
+
const only = keys.length === 1 ? keys[0] : undefined;
|
|
98
|
+
const type = only === undefined ? undefined : types[only];
|
|
99
|
+
return only === undefined || !type ? undefined : { key: only, type };
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
typeKeysNamed(name: string): string[] {
|
|
103
|
+
return this.typeKeysByName.get(name.replace(/^Bit\./, "").replace(/\[\]$/, "")) ?? [];
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
typesIn(texts: readonly (string | null | undefined)[], near?: string): NamedType[] {
|
|
107
|
+
return uniqueTypes(texts.flatMap((text) => (text?.match(TYPE_NAME) ?? []).flatMap((name) => this.type(name, near) ?? [])));
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
typesUsedBy(member: IndexMember): NamedType[] {
|
|
111
|
+
return uniqueTypes([
|
|
112
|
+
...this.typesIn([member.returns]),
|
|
113
|
+
...member.params.flatMap((param) => this.typesIn((param.fields ?? []).map((field) => field.type), namespaceOf(param.type))),
|
|
114
|
+
]);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
typesUsedByType(named: NamedType): NamedType[] {
|
|
118
|
+
return this.typesIn(named.type.fields.map((field) => field.type), namespaceOf(named.key)).filter((used) => used.key !== named.key);
|
|
119
|
+
}
|
|
120
|
+
|
|
66
121
|
all(): IndexMember[] {
|
|
67
122
|
return this.index.members;
|
|
68
123
|
}
|
|
@@ -79,9 +134,13 @@ export class IndexReader {
|
|
|
79
134
|
const limit = Math.min(Math.max(options.limit ?? DEFAULT_LIMIT, 1), MAX_LIMIT);
|
|
80
135
|
const hits: SearchHit[] = [];
|
|
81
136
|
for (const member of this.index.members) {
|
|
82
|
-
if (options.engine && member.engines.length > 0 && !member.engines.includes(options.engine))
|
|
137
|
+
if (options.engine && member.engines.length > 0 && !member.engines.includes(options.engine)) {
|
|
138
|
+
continue;
|
|
139
|
+
}
|
|
83
140
|
const score = this.score(member, queryTokens, phrase);
|
|
84
|
-
if (score > 0)
|
|
141
|
+
if (score > 0) {
|
|
142
|
+
hits.push({ member, score });
|
|
143
|
+
}
|
|
85
144
|
}
|
|
86
145
|
return hits.sort(byPathThenLength).slice(0, limit);
|
|
87
146
|
}
|
|
@@ -94,11 +153,17 @@ export class IndexReader {
|
|
|
94
153
|
const found: IndexMember[] = [];
|
|
95
154
|
const seen = new Set<string>();
|
|
96
155
|
const add = (member: IndexMember): void => {
|
|
97
|
-
if (seen.has(member.path))
|
|
156
|
+
if (seen.has(member.path)) {
|
|
157
|
+
return;
|
|
158
|
+
}
|
|
98
159
|
seen.add(member.path);
|
|
99
160
|
found.push(member);
|
|
100
161
|
};
|
|
101
|
-
for (const member of this.index.members)
|
|
162
|
+
for (const member of this.index.members) {
|
|
163
|
+
if (member.path.toLowerCase() === lower) {
|
|
164
|
+
add(member);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
102
167
|
if (parent) {
|
|
103
168
|
const tolerance = Math.max(MIN_EDIT_TOLERANCE, Math.floor(lastSegment.length / EDIT_TOLERANCE_DIVISOR));
|
|
104
169
|
const siblings = this.index.members
|
|
@@ -106,20 +171,28 @@ export class IndexReader {
|
|
|
106
171
|
.map((member) => ({ member, distance: editDistance(member.name.toLowerCase(), lastSegment, tolerance) }))
|
|
107
172
|
.filter(({ distance }) => distance <= tolerance)
|
|
108
173
|
.sort((a, b) => a.distance - b.distance || (a.member.path < b.member.path ? -1 : 1));
|
|
109
|
-
for (const { member } of siblings)
|
|
174
|
+
for (const { member } of siblings) {
|
|
175
|
+
add(member);
|
|
176
|
+
}
|
|
110
177
|
}
|
|
111
178
|
const sameName = this.index.members
|
|
112
179
|
.filter((member) => member.name.toLowerCase() === lastSegment)
|
|
113
180
|
.map((member) => ({ member, distance: editDistance(member.path.toLowerCase(), lower, lower.length) }))
|
|
114
181
|
.sort((a, b) => a.distance - b.distance || (a.member.path < b.member.path ? -1 : 1));
|
|
115
|
-
for (const { member } of sameName)
|
|
116
|
-
|
|
182
|
+
for (const { member } of sameName) {
|
|
183
|
+
add(member);
|
|
184
|
+
}
|
|
185
|
+
for (const hit of this.search(path.replace(/\./g, " "), { limit })) {
|
|
186
|
+
add(hit.member);
|
|
187
|
+
}
|
|
117
188
|
return found.slice(0, limit);
|
|
118
189
|
}
|
|
119
190
|
|
|
120
191
|
examplesOf(path: string): IndexExample[] {
|
|
121
192
|
const member = this.byPath.get(path);
|
|
122
|
-
if (!member)
|
|
193
|
+
if (!member) {
|
|
194
|
+
return [];
|
|
195
|
+
}
|
|
123
196
|
return member.examples.flatMap((id) => {
|
|
124
197
|
const example = Object.hasOwn(this.index.examples, id) ? this.index.examples[id] : undefined;
|
|
125
198
|
return example ? [example] : [];
|
|
@@ -130,10 +203,14 @@ export class IndexReader {
|
|
|
130
203
|
const prefix = `${path}.`;
|
|
131
204
|
const examples: IndexExample[] = [];
|
|
132
205
|
for (const member of this.index.members) {
|
|
133
|
-
if (!member.path.startsWith(prefix))
|
|
206
|
+
if (!member.path.startsWith(prefix)) {
|
|
207
|
+
continue;
|
|
208
|
+
}
|
|
134
209
|
for (const example of this.examplesOf(member.path)) {
|
|
135
210
|
examples.push(example);
|
|
136
|
-
if (examples.length >= limit)
|
|
211
|
+
if (examples.length >= limit) {
|
|
212
|
+
return examples;
|
|
213
|
+
}
|
|
137
214
|
}
|
|
138
215
|
}
|
|
139
216
|
return examples;
|
|
@@ -141,22 +218,34 @@ export class IndexReader {
|
|
|
141
218
|
|
|
142
219
|
private score(member: IndexMember, queryTokens: string[], phrase: string): number {
|
|
143
220
|
const tokens = this.tokens.get(member.path);
|
|
144
|
-
if (!tokens || queryTokens.length === 0)
|
|
221
|
+
if (!tokens || queryTokens.length === 0) {
|
|
222
|
+
return 0;
|
|
223
|
+
}
|
|
145
224
|
let score = member.path.toLowerCase() === phrase ? EXACT_PATH : 0;
|
|
146
225
|
for (const token of queryTokens) {
|
|
147
|
-
if (tokens.name.has(token))
|
|
148
|
-
|
|
149
|
-
else if (
|
|
150
|
-
|
|
151
|
-
else if (token.length >= PREFIX_MIN_LENGTH && startsWithAny(tokens.
|
|
226
|
+
if (tokens.name.has(token)) {
|
|
227
|
+
score += NAME_MATCH;
|
|
228
|
+
} else if (tokens.path.has(token)) {
|
|
229
|
+
score += PATH_MATCH;
|
|
230
|
+
} else if (token.length >= PREFIX_MIN_LENGTH && startsWithAny(tokens.path, token)) {
|
|
231
|
+
score += PATH_PREFIX;
|
|
232
|
+
} else if (tokens.text.has(token)) {
|
|
233
|
+
score += TEXT_MATCH;
|
|
234
|
+
} else if (token.length >= PREFIX_MIN_LENGTH && startsWithAny(tokens.text, token)) {
|
|
235
|
+
score += TEXT_PREFIX;
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
if (phrase.length > 0 && member.summary.toLowerCase().includes(phrase)) {
|
|
239
|
+
score += PHRASE_IN_SUMMARY;
|
|
152
240
|
}
|
|
153
|
-
if (phrase.length > 0 && member.summary.toLowerCase().includes(phrase)) score += PHRASE_IN_SUMMARY;
|
|
154
241
|
return score;
|
|
155
242
|
}
|
|
156
243
|
}
|
|
157
244
|
|
|
158
245
|
export function editDistance(a: string, b: string, cap: number): number {
|
|
159
|
-
if (Math.abs(a.length - b.length) > cap)
|
|
246
|
+
if (Math.abs(a.length - b.length) > cap) {
|
|
247
|
+
return cap + 1;
|
|
248
|
+
}
|
|
160
249
|
let previous = Array.from({ length: b.length + 1 }, (_, index) => index);
|
|
161
250
|
for (let i = 1; i <= a.length; i++) {
|
|
162
251
|
const current = [i];
|
|
@@ -167,13 +256,19 @@ export function editDistance(a: string, b: string, cap: number): number {
|
|
|
167
256
|
current.push(value);
|
|
168
257
|
rowMinimum = Math.min(rowMinimum, value);
|
|
169
258
|
}
|
|
170
|
-
if (rowMinimum > cap)
|
|
259
|
+
if (rowMinimum > cap) {
|
|
260
|
+
return cap + 1;
|
|
261
|
+
}
|
|
171
262
|
previous = current;
|
|
172
263
|
}
|
|
173
264
|
return previous[b.length] ?? cap + 1;
|
|
174
265
|
}
|
|
175
266
|
|
|
176
267
|
function startsWithAny(tokens: Set<string>, prefix: string): boolean {
|
|
177
|
-
for (const token of tokens)
|
|
268
|
+
for (const token of tokens) {
|
|
269
|
+
if (token.startsWith(prefix)) {
|
|
270
|
+
return true;
|
|
271
|
+
}
|
|
272
|
+
}
|
|
178
273
|
return false;
|
|
179
274
|
}
|
package/src/index-shape.ts
CHANGED
|
@@ -1,13 +1,17 @@
|
|
|
1
1
|
import type { ApiIndex } from "./index-types.js";
|
|
2
2
|
|
|
3
3
|
function isMemberLike(value: unknown): boolean {
|
|
4
|
-
if (typeof value !== "object" || value === null)
|
|
4
|
+
if (typeof value !== "object" || value === null) {
|
|
5
|
+
return false;
|
|
6
|
+
}
|
|
5
7
|
const candidate = value as { path?: unknown; name?: unknown; engines?: unknown; examples?: unknown };
|
|
6
8
|
return typeof candidate.path === "string" && typeof candidate.name === "string" && Array.isArray(candidate.engines) && Array.isArray(candidate.examples);
|
|
7
9
|
}
|
|
8
10
|
|
|
9
11
|
export function isApiIndex(value: unknown, version: string): value is ApiIndex {
|
|
10
|
-
if (typeof value !== "object" || value === null)
|
|
12
|
+
if (typeof value !== "object" || value === null) {
|
|
13
|
+
return false;
|
|
14
|
+
}
|
|
11
15
|
const candidate = value as { version?: unknown; members?: unknown; examples?: unknown };
|
|
12
16
|
return candidate.version === version
|
|
13
17
|
&& Array.isArray(candidate.members)
|
package/src/index-types.ts
CHANGED
|
@@ -58,6 +58,13 @@ export interface IndexEnum {
|
|
|
58
58
|
values: { key: string; value: string }[];
|
|
59
59
|
}
|
|
60
60
|
|
|
61
|
+
export interface IndexType {
|
|
62
|
+
name: string;
|
|
63
|
+
summary: string;
|
|
64
|
+
doc: string;
|
|
65
|
+
fields: IndexField[];
|
|
66
|
+
}
|
|
67
|
+
|
|
61
68
|
export interface IndexExample {
|
|
62
69
|
path: string;
|
|
63
70
|
code: string;
|
|
@@ -70,5 +77,6 @@ export interface ApiIndex {
|
|
|
70
77
|
namespaces: string[];
|
|
71
78
|
members: IndexMember[];
|
|
72
79
|
enums: Record<string, IndexEnum>;
|
|
80
|
+
types?: Record<string, IndexType>;
|
|
73
81
|
examples: Record<string, IndexExample>;
|
|
74
82
|
}
|
package/src/index-url.ts
CHANGED
|
@@ -11,7 +11,9 @@ export function indexPath(version: string): string {
|
|
|
11
11
|
}
|
|
12
12
|
|
|
13
13
|
export function indexUrl(version: string, host: string = INDEX_HOST): string {
|
|
14
|
-
if (!isExactVersion(version))
|
|
14
|
+
if (!isExactVersion(version)) {
|
|
15
|
+
throw new Error(`"${version}" is not an exact version; the index is published per release and never under a moving name`);
|
|
16
|
+
}
|
|
15
17
|
return `${host}${indexPath(version)}`;
|
|
16
18
|
}
|
|
17
19
|
|
|
@@ -14,7 +14,9 @@ describe("installedVersions", () => {
|
|
|
14
14
|
const temporaryDirectories: string[] = [];
|
|
15
15
|
|
|
16
16
|
afterEach(() => {
|
|
17
|
-
for (const directory of temporaryDirectories.splice(0))
|
|
17
|
+
for (const directory of temporaryDirectories.splice(0)) {
|
|
18
|
+
rmSync(directory, { recursive: true, force: true });
|
|
19
|
+
}
|
|
18
20
|
});
|
|
19
21
|
|
|
20
22
|
it("reads the manifests from disk when no reader is given, and skips a package that is not installed", () => {
|
package/src/installed-version.ts
CHANGED
|
@@ -24,10 +24,14 @@ function readTextIfPresent(path: string): string | undefined {
|
|
|
24
24
|
}
|
|
25
25
|
|
|
26
26
|
function versionOf(text: string | undefined): string | undefined {
|
|
27
|
-
if (text === undefined)
|
|
27
|
+
if (text === undefined) {
|
|
28
|
+
return undefined;
|
|
29
|
+
}
|
|
28
30
|
try {
|
|
29
31
|
const manifest: unknown = JSON.parse(text);
|
|
30
|
-
if (typeof manifest === "object" && manifest !== null && "version" in manifest && typeof manifest.version === "string")
|
|
32
|
+
if (typeof manifest === "object" && manifest !== null && "version" in manifest && typeof manifest.version === "string") {
|
|
33
|
+
return manifest.version;
|
|
34
|
+
}
|
|
31
35
|
} catch {
|
|
32
36
|
return undefined;
|
|
33
37
|
}
|
|
@@ -49,12 +53,18 @@ export function installedVersions(cwd: string, readFile: (path: string) => strin
|
|
|
49
53
|
let directory = cwd;
|
|
50
54
|
for (;;) {
|
|
51
55
|
for (const name of VERSION_PACKAGES) {
|
|
52
|
-
if (found.has(name))
|
|
56
|
+
if (found.has(name)) {
|
|
57
|
+
continue;
|
|
58
|
+
}
|
|
53
59
|
const version = versionOf(readFile(join(directory, "node_modules", "@bitbybit-dev", name, "package.json")));
|
|
54
|
-
if (version !== undefined)
|
|
60
|
+
if (version !== undefined) {
|
|
61
|
+
found.set(name, version);
|
|
62
|
+
}
|
|
55
63
|
}
|
|
56
64
|
const parent = dirname(directory);
|
|
57
|
-
if (parent === directory)
|
|
65
|
+
if (parent === directory) {
|
|
66
|
+
break;
|
|
67
|
+
}
|
|
58
68
|
directory = parent;
|
|
59
69
|
}
|
|
60
70
|
return found;
|
|
@@ -62,9 +72,13 @@ export function installedVersions(cwd: string, readFile: (path: string) => strin
|
|
|
62
72
|
|
|
63
73
|
export function detectVersion(options: DetectVersionOptions): DetectedVersion {
|
|
64
74
|
const flag = flagValue(options.argv ?? [], "--version");
|
|
65
|
-
if (flag !== undefined && flag !== "")
|
|
75
|
+
if (flag !== undefined && flag !== "") {
|
|
76
|
+
return { version: flag, source: "flag" };
|
|
77
|
+
}
|
|
66
78
|
const fromEnv = options.env?.["BITBYBIT_VERSION"];
|
|
67
|
-
if (fromEnv !== undefined && fromEnv !== "")
|
|
79
|
+
if (fromEnv !== undefined && fromEnv !== "") {
|
|
80
|
+
return { version: fromEnv, source: "env" };
|
|
81
|
+
}
|
|
68
82
|
if (options.cwd !== undefined) {
|
|
69
83
|
const installed = installedVersions(options.cwd, options.readFile);
|
|
70
84
|
const first = VERSION_PACKAGES.map((name) => installed.get(name)).find((version) => version !== undefined);
|
package/src/json-schema.ts
CHANGED
|
@@ -8,17 +8,25 @@ function resolvePointer(root: unknown, pointer: string): unknown {
|
|
|
8
8
|
let node: unknown = root;
|
|
9
9
|
for (const part of pointer.slice(LOCAL_POINTER.length).split("/")) {
|
|
10
10
|
const key = part.replace(/~1/g, "/").replace(/~0/g, "~");
|
|
11
|
-
if (Array.isArray(node))
|
|
12
|
-
|
|
13
|
-
else
|
|
11
|
+
if (Array.isArray(node)) {
|
|
12
|
+
node = node[Number(key)];
|
|
13
|
+
} else if (isRecord(node)) {
|
|
14
|
+
node = node[key];
|
|
15
|
+
} else {
|
|
16
|
+
return undefined;
|
|
17
|
+
}
|
|
14
18
|
}
|
|
15
19
|
return node;
|
|
16
20
|
}
|
|
17
21
|
|
|
18
22
|
export function inlineJsonSchemaReferences(schema: Record<string, unknown>): Record<string, unknown> {
|
|
19
23
|
const inline = (node: unknown, seen: readonly string[]): unknown => {
|
|
20
|
-
if (Array.isArray(node))
|
|
21
|
-
|
|
24
|
+
if (Array.isArray(node)) {
|
|
25
|
+
return node.map((item) => inline(item, seen));
|
|
26
|
+
}
|
|
27
|
+
if (!isRecord(node)) {
|
|
28
|
+
return node;
|
|
29
|
+
}
|
|
22
30
|
const reference = node["$ref"];
|
|
23
31
|
if (typeof reference === "string" && reference.startsWith(LOCAL_POINTER) && !seen.includes(reference)) {
|
|
24
32
|
const target = resolvePointer(schema, reference);
|
|
@@ -30,11 +38,17 @@ export function inlineJsonSchemaReferences(schema: Record<string, unknown>): Rec
|
|
|
30
38
|
}
|
|
31
39
|
}
|
|
32
40
|
const out: Record<string, unknown> = {};
|
|
33
|
-
for (const [key, value] of Object.entries(node))
|
|
41
|
+
for (const [key, value] of Object.entries(node)) {
|
|
42
|
+
if (key !== "$defs") {
|
|
43
|
+
out[key] = inline(value, seen);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
34
46
|
return out;
|
|
35
47
|
};
|
|
36
48
|
const result = inline(schema, []);
|
|
37
49
|
const inlined = isRecord(result) ? result : {};
|
|
38
|
-
if (JSON.stringify(inlined).includes("\"$ref\"") && isRecord(schema["$defs"]))
|
|
50
|
+
if (JSON.stringify(inlined).includes("\"$ref\"") && isRecord(schema["$defs"])) {
|
|
51
|
+
inlined["$defs"] = schema["$defs"];
|
|
52
|
+
}
|
|
39
53
|
return inlined;
|
|
40
54
|
}
|
package/src/registry.ts
CHANGED
|
@@ -47,7 +47,9 @@ export class Registry<TContext> {
|
|
|
47
47
|
private readonly tools = new Map<string, AnyToolDefinition<TContext>>();
|
|
48
48
|
|
|
49
49
|
register<TInput extends ToolInput>(definition: ToolDefinition<TInput, TContext>): this {
|
|
50
|
-
if (this.tools.has(definition.name))
|
|
50
|
+
if (this.tools.has(definition.name)) {
|
|
51
|
+
throw new Error(`Tool ${definition.name} is already registered`);
|
|
52
|
+
}
|
|
51
53
|
const { handler } = definition;
|
|
52
54
|
const erased: AnyToolDefinition<TContext> = handler
|
|
53
55
|
? { ...definition, handler: (args, context) => handler(args as z.output<TInput>, context) }
|
|
@@ -82,7 +84,9 @@ const JSON_SCHEMAS = new WeakMap<ToolInput, Record<string, unknown>>();
|
|
|
82
84
|
|
|
83
85
|
export function inputJsonSchema(input: ToolInput): Record<string, unknown> {
|
|
84
86
|
const known = JSON_SCHEMAS.get(input);
|
|
85
|
-
if (known)
|
|
87
|
+
if (known) {
|
|
88
|
+
return known;
|
|
89
|
+
}
|
|
86
90
|
const rendered = inlineJsonSchemaReferences(z.toJSONSchema(input, { target: "draft-2020-12", io: "input" }));
|
|
87
91
|
JSON_SCHEMAS.set(input, rendered);
|
|
88
92
|
return rendered;
|
|
@@ -120,7 +124,9 @@ export function toHttp<TContext>(
|
|
|
120
124
|
const tools: HttpTool[] = [];
|
|
121
125
|
for (const definition of registry.list()) {
|
|
122
126
|
const { handler } = definition;
|
|
123
|
-
if (!handler || (filter && !filter(definition)))
|
|
127
|
+
if (!handler || (filter && !filter(definition))) {
|
|
128
|
+
continue;
|
|
129
|
+
}
|
|
124
130
|
tools.push({
|
|
125
131
|
name: definition.name,
|
|
126
132
|
description: definition.description,
|
package/src/render.test.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { describe, expect, it } from "vitest";
|
|
2
2
|
import { IndexReader } from "./index-reader.js";
|
|
3
|
-
import { renderMember } from "./render.js";
|
|
3
|
+
import { renderMember, renderType } from "./render.js";
|
|
4
4
|
import type { IndexMember } from "./index-types.js";
|
|
5
5
|
import { fixtureIndex } from "./__fixtures__/load.js";
|
|
6
6
|
|
|
@@ -62,6 +62,26 @@ describe("renderMember", () => {
|
|
|
62
62
|
expect(text).toContain("- axis: ordinateAxisEnum; default \"x\"; one of x, y - Which axis");
|
|
63
63
|
expect(text).toContain("- width?: number; default 1; range 0 to 10; step 0.5");
|
|
64
64
|
});
|
|
65
|
+
|
|
66
|
+
it("names an option by the key a caller writes and the value it stands for, when the two differ", () => {
|
|
67
|
+
// Arrange
|
|
68
|
+
const withEnum = member({
|
|
69
|
+
params: [{
|
|
70
|
+
name: "inputs",
|
|
71
|
+
type: "AddWallDto",
|
|
72
|
+
optional: false,
|
|
73
|
+
fields: [
|
|
74
|
+
{ name: "predefinedType", type: "wallPredefinedTypeEnum", optional: true, doc: "", default: "standard", options: [{ value: "STANDARD", label: "standard" }, { value: "SOLIDWALL", label: "solidWall" }] },
|
|
75
|
+
],
|
|
76
|
+
}],
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
// Act
|
|
80
|
+
const text = renderMember(withEnum, "9.9.9", []);
|
|
81
|
+
|
|
82
|
+
// Assert
|
|
83
|
+
expect(text).toContain("- predefinedType?: wallPredefinedTypeEnum; default \"standard\"; one of standard = \"STANDARD\", solidWall = \"SOLIDWALL\"");
|
|
84
|
+
});
|
|
65
85
|
});
|
|
66
86
|
|
|
67
87
|
describe("IndexReader", () => {
|
|
@@ -78,4 +98,136 @@ describe("IndexReader", () => {
|
|
|
78
98
|
expect(all.map((entry) => entry.path)).toEqual(index.members.map((entry) => entry.path));
|
|
79
99
|
expect(reader.get(index.members[0]?.path ?? "")).toBe(index.members[0]);
|
|
80
100
|
});
|
|
101
|
+
|
|
102
|
+
it("finds a type by its full name, by its name under Inputs, or by a name only one type has, array or not", () => {
|
|
103
|
+
// Arrange
|
|
104
|
+
const reader = new IndexReader(fixtureIndex());
|
|
105
|
+
|
|
106
|
+
// Act
|
|
107
|
+
const names = ["Inputs.IFC.ModelSummaryDto", "IFC.ModelSummaryDto", "ModelSummaryDto", "Bit.Inputs.IFC.ModelSummaryDto[]"].map((name) => reader.type(name)?.key);
|
|
108
|
+
|
|
109
|
+
// Assert
|
|
110
|
+
expect(names).toEqual(["Inputs.IFC.ModelSummaryDto", "Inputs.IFC.ModelSummaryDto", "Inputs.IFC.ModelSummaryDto", "Inputs.IFC.ModelSummaryDto"]);
|
|
111
|
+
expect(reader.type("Inputs.IFC.IfcModelPointer")).toBeUndefined();
|
|
112
|
+
expect(reader.type("string")).toBeUndefined();
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
it("resolves a name two types share only within the namespace that refers to it, and names both", () => {
|
|
116
|
+
// Arrange
|
|
117
|
+
const reader = new IndexReader(fixtureIndex());
|
|
118
|
+
|
|
119
|
+
// Act
|
|
120
|
+
const bare = reader.type("DecomposedManifoldMeshDto");
|
|
121
|
+
const near = reader.type("DecomposedManifoldMeshDto", "Inputs.Manifold");
|
|
122
|
+
|
|
123
|
+
// Assert
|
|
124
|
+
expect(bare).toBeUndefined();
|
|
125
|
+
expect(near?.key).toBe("Inputs.Manifold.DecomposedManifoldMeshDto");
|
|
126
|
+
expect(reader.typeKeysNamed("DecomposedManifoldMeshDto[]")).toEqual(["Inputs.OCCT.DecomposedManifoldMeshDto", "Inputs.Manifold.DecomposedManifoldMeshDto"]);
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
it("lists the types a member returns and the types its inputs' fields take, read in the inputs' namespace, each once", () => {
|
|
130
|
+
// Arrange
|
|
131
|
+
const reader = new IndexReader(fixtureIndex());
|
|
132
|
+
const summary = reader.get("ifc.model.summary");
|
|
133
|
+
const taking = member({
|
|
134
|
+
returns: "Inputs.IFC.ModelSummaryDto",
|
|
135
|
+
params: [{
|
|
136
|
+
name: "inputs",
|
|
137
|
+
type: "Inputs.Manifold.MeshDto<Inputs.Manifold.ManifoldPointer>",
|
|
138
|
+
optional: false,
|
|
139
|
+
fields: [
|
|
140
|
+
{ name: "mesh", type: "DecomposedManifoldMeshDto", optional: false, doc: "" },
|
|
141
|
+
{ name: "summaries", type: "Inputs.IFC.ModelSummaryDto[]", optional: false, doc: "" },
|
|
142
|
+
],
|
|
143
|
+
}],
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
// Act
|
|
147
|
+
const returned = summary ? reader.typesUsedBy(summary) : [];
|
|
148
|
+
const taken = reader.typesUsedBy(taking);
|
|
149
|
+
|
|
150
|
+
// Assert
|
|
151
|
+
expect(returned.map((used) => used.key)).toEqual(["Inputs.IFC.ModelSummaryDto"]);
|
|
152
|
+
expect(taken.map((used) => used.key)).toEqual(["Inputs.IFC.ModelSummaryDto", "Inputs.Manifold.DecomposedManifoldMeshDto"]);
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
it("lists the types a type's fields name, in the type's own namespace, leaving the type itself out", () => {
|
|
156
|
+
// Arrange
|
|
157
|
+
const reader = new IndexReader(fixtureIndex());
|
|
158
|
+
const named = reader.type("Inputs.IFC.ModelSummaryDto");
|
|
159
|
+
|
|
160
|
+
// Act
|
|
161
|
+
const used = named ? reader.typesUsedByType(named) : [];
|
|
162
|
+
|
|
163
|
+
// Assert
|
|
164
|
+
expect(used.map((entry) => entry.key)).toEqual(["Inputs.IFC.StoreyInfoDto"]);
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
it("reads a type's bare field types in its own namespace and leaves out a field naming the type itself", () => {
|
|
168
|
+
// Arrange
|
|
169
|
+
const index = fixtureIndex();
|
|
170
|
+
const node = { name: "MeshTreeDto", summary: "", doc: "", fields: [
|
|
171
|
+
{ name: "mesh", type: "DecomposedManifoldMeshDto", optional: false, doc: "" },
|
|
172
|
+
{ name: "children", type: "MeshTreeDto[]", optional: false, doc: "" },
|
|
173
|
+
] };
|
|
174
|
+
const reader = new IndexReader({ ...index, types: { ...index.types, "Inputs.Manifold.MeshTreeDto": node } });
|
|
175
|
+
|
|
176
|
+
// Act
|
|
177
|
+
const used = reader.typesUsedByType({ key: "Inputs.Manifold.MeshTreeDto", type: node });
|
|
178
|
+
|
|
179
|
+
// Assert
|
|
180
|
+
expect(used.map((entry) => entry.key)).toEqual(["Inputs.Manifold.DecomposedManifoldMeshDto"]);
|
|
181
|
+
});
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
describe("renderType", () => {
|
|
185
|
+
it("renders a type's name, version, doc and every field, then the types its fields name", () => {
|
|
186
|
+
// Arrange
|
|
187
|
+
const reader = new IndexReader(fixtureIndex());
|
|
188
|
+
const named = reader.type("Inputs.IFC.ModelSummaryDto");
|
|
189
|
+
if (!named) {
|
|
190
|
+
throw new Error("the fixture lacks Inputs.IFC.ModelSummaryDto");
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
// Act
|
|
194
|
+
const text = renderType(named, "9.9.9", reader.typesUsedByType(named));
|
|
195
|
+
|
|
196
|
+
// Assert
|
|
197
|
+
expect(text.startsWith("# Inputs.IFC.ModelSummaryDto\n\n- kind: type\n- version: 9.9.9\n\nWhat a model holds, as `model.summary` describes it.\n\n## Fields\n")).toBe(true);
|
|
198
|
+
expect(text).toContain("- storeys: StoreyInfoDto[]; default [] - The storeys, lowest first.");
|
|
199
|
+
expect(text).toContain("- editable: boolean; default true - Whether the model can be changed: false for an `IFC2X3` file, which is read through `IFC4` to be shown and queried.");
|
|
200
|
+
expect(text).toContain("## Types\n\nDescribe one by its name for its fields:\n- `Inputs.IFC.StoreyInfoDto`: A storey as `spatial.storeys` and `model.summary` list it: its GlobalId, name and elevation.\n");
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
it("renders a type no field of which names another without a Types section", () => {
|
|
204
|
+
// Arrange
|
|
205
|
+
const reader = new IndexReader(fixtureIndex());
|
|
206
|
+
const named = reader.type("StoreyInfoDto");
|
|
207
|
+
if (!named) {
|
|
208
|
+
throw new Error("the fixture lacks Inputs.IFC.StoreyInfoDto");
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
// Act
|
|
212
|
+
const text = renderType(named, "9.9.9", reader.typesUsedByType(named));
|
|
213
|
+
|
|
214
|
+
// Assert
|
|
215
|
+
expect(text).not.toContain("## Types");
|
|
216
|
+
expect(text.endsWith("- elevation: number; default 0 - How high its floor is above the building's origin, in the model's length unit.\n")).toBe(true);
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
it("puts a member's Types section after its parameters and before its examples", () => {
|
|
220
|
+
// Arrange
|
|
221
|
+
const reader = new IndexReader(fixtureIndex());
|
|
222
|
+
const named = reader.type("StoreyInfoDto");
|
|
223
|
+
const example = { path: "occt.shapes.solid.createBox", code: "const box = 1;" };
|
|
224
|
+
|
|
225
|
+
// Act
|
|
226
|
+
const text = renderMember(member({}), "9.9.9", [example], named ? [named] : []);
|
|
227
|
+
|
|
228
|
+
// Assert
|
|
229
|
+
expect(text.indexOf("## Types")).toBeGreaterThan(0);
|
|
230
|
+
expect(text.indexOf("## Types")).toBeLessThan(text.indexOf("## Examples"));
|
|
231
|
+
expect(text).toContain("- `Inputs.IFC.StoreyInfoDto`: A storey as");
|
|
232
|
+
});
|
|
81
233
|
});
|