vibecode-anything 0.0.0-stage → 0.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/LICENSE +21 -0
- package/README.md +235 -2
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +452 -0
- package/dist/core/adapters.d.ts +8 -0
- package/dist/core/adapters.js +92 -0
- package/dist/core/diff.d.ts +10 -0
- package/dist/core/diff.js +207 -0
- package/dist/core/doctor.d.ts +50 -0
- package/dist/core/doctor.js +312 -0
- package/dist/core/errors.d.ts +32 -0
- package/dist/core/errors.js +81 -0
- package/dist/core/exec.d.ts +14 -0
- package/dist/core/exec.js +109 -0
- package/dist/core/parity.d.ts +9 -0
- package/dist/core/parity.js +138 -0
- package/dist/core/png.d.ts +7 -0
- package/dist/core/png.js +303 -0
- package/dist/core/schema.d.ts +17 -0
- package/dist/core/schema.js +614 -0
- package/dist/core/slices.d.ts +24 -0
- package/dist/core/slices.js +294 -0
- package/dist/core/store.d.ts +17 -0
- package/dist/core/store.js +97 -0
- package/dist/core/types.d.ts +751 -0
- package/dist/core/types.js +4 -0
- package/dist/core/visual.d.ts +34 -0
- package/dist/core/visual.js +459 -0
- package/dist/core/watch.d.ts +53 -0
- package/dist/core/watch.js +285 -0
- package/dist/ingestors/api-repo.d.ts +5 -0
- package/dist/ingestors/api-repo.js +722 -0
- package/dist/ingestors/bridges.d.ts +3 -0
- package/dist/ingestors/bridges.js +562 -0
- package/dist/ingestors/cli.d.ts +9 -0
- package/dist/ingestors/cli.js +266 -0
- package/dist/ingestors/image.d.ts +2 -0
- package/dist/ingestors/image.js +147 -0
- package/dist/ingestors/index.d.ts +5 -0
- package/dist/ingestors/index.js +97 -0
- package/dist/ingestors/video.d.ts +47 -0
- package/dist/ingestors/video.js +605 -0
- package/dist/ingestors/web.d.ts +25 -0
- package/dist/ingestors/web.js +397 -0
- package/dist/mcp/server.d.ts +2 -0
- package/dist/mcp/server.js +504 -0
- package/dist/oracles/api-repo.d.ts +2 -0
- package/dist/oracles/api-repo.js +214 -0
- package/dist/oracles/cli.d.ts +2 -0
- package/dist/oracles/cli.js +157 -0
- package/dist/oracles/image.d.ts +2 -0
- package/dist/oracles/image.js +279 -0
- package/dist/oracles/index.d.ts +4 -0
- package/dist/oracles/index.js +91 -0
- package/dist/oracles/video.d.ts +21 -0
- package/dist/oracles/video.js +411 -0
- package/dist/oracles/web.d.ts +2 -0
- package/dist/oracles/web.js +480 -0
- package/dist/setup/index.d.ts +7 -0
- package/dist/setup/index.js +186 -0
- package/package.json +47 -6
- package/skills/vibecode-anything/SKILL.md +52 -0
|
@@ -0,0 +1,722 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { VibecodeError, errorMessage } from "../core/errors.js";
|
|
4
|
+
function isObject(v) {
|
|
5
|
+
return v !== null && typeof v === "object" && !Array.isArray(v);
|
|
6
|
+
}
|
|
7
|
+
function asArray(v) {
|
|
8
|
+
return Array.isArray(v) ? v : [];
|
|
9
|
+
}
|
|
10
|
+
function synthesizeSchema(value) {
|
|
11
|
+
if (value === null)
|
|
12
|
+
return { type: "null" };
|
|
13
|
+
if (Array.isArray(value))
|
|
14
|
+
return { type: "array", items: value.length ? synthesizeSchema(value[0]) : {} };
|
|
15
|
+
if (isObject(value)) {
|
|
16
|
+
const properties = {};
|
|
17
|
+
for (const [k, v] of Object.entries(value))
|
|
18
|
+
properties[k] = synthesizeSchema(v);
|
|
19
|
+
return { type: "object", required: Object.keys(value), properties };
|
|
20
|
+
}
|
|
21
|
+
if (Number.isInteger(value))
|
|
22
|
+
return { type: "integer" };
|
|
23
|
+
return { type: typeof value };
|
|
24
|
+
}
|
|
25
|
+
async function fetchJson(url, { method = "GET", headers = {}, body, timeoutMs = 10000 } = {}) {
|
|
26
|
+
const ctrl = new AbortController();
|
|
27
|
+
const t = setTimeout(() => ctrl.abort(), timeoutMs);
|
|
28
|
+
try {
|
|
29
|
+
const res = await fetch(url, {
|
|
30
|
+
method,
|
|
31
|
+
headers,
|
|
32
|
+
body: body === undefined || body === null ? undefined : (typeof body === "string" ? body : JSON.stringify(body)),
|
|
33
|
+
signal: ctrl.signal
|
|
34
|
+
});
|
|
35
|
+
const text = await res.text();
|
|
36
|
+
let json = null;
|
|
37
|
+
try {
|
|
38
|
+
json = text ? JSON.parse(text) : null;
|
|
39
|
+
}
|
|
40
|
+
catch { /* ignore */ }
|
|
41
|
+
const headersObj = {};
|
|
42
|
+
for (const [k, v] of res.headers.entries())
|
|
43
|
+
headersObj[k.toLowerCase()] = v;
|
|
44
|
+
return { status: res.status, headers: headersObj, text, json };
|
|
45
|
+
}
|
|
46
|
+
finally {
|
|
47
|
+
clearTimeout(t);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
function normalizeEndpoint(ep) {
|
|
51
|
+
if (typeof ep === "string")
|
|
52
|
+
return { method: "GET", path: ep, headers: {}, body: null, explicit: true };
|
|
53
|
+
if (!ep || typeof ep !== "object" || Array.isArray(ep))
|
|
54
|
+
return null;
|
|
55
|
+
const method = String(ep.method || "GET").toUpperCase();
|
|
56
|
+
const p = String(ep.path || ep.url || "/");
|
|
57
|
+
const pathOnly = /^https?:\/\//i.test(p) ? new URL(p).pathname + new URL(p).search : p;
|
|
58
|
+
const headers = {};
|
|
59
|
+
if (isObject(ep.headers)) {
|
|
60
|
+
for (const [k, v] of Object.entries(ep.headers))
|
|
61
|
+
headers[k] = String(v);
|
|
62
|
+
}
|
|
63
|
+
const query = isObject(ep.query) ? ep.query : undefined;
|
|
64
|
+
return {
|
|
65
|
+
id: typeof ep.id === "string" ? ep.id : undefined,
|
|
66
|
+
method,
|
|
67
|
+
path: pathOnly.startsWith("/") ? pathOnly : `/${pathOnly}`,
|
|
68
|
+
headers,
|
|
69
|
+
query,
|
|
70
|
+
body: ep.body ?? null,
|
|
71
|
+
expectedStatus: ep.expectedStatus,
|
|
72
|
+
jsonSchema: isObject(ep.jsonSchema) ? ep.jsonSchema : undefined,
|
|
73
|
+
jsonPaths: Array.isArray(ep.jsonPaths) ? ep.jsonPaths : undefined,
|
|
74
|
+
explicit: true
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
function buildApiUrl(base, p, query) {
|
|
78
|
+
const baseUrl = /^https?:\/\//i.test(base) ? base : `http://${base}`;
|
|
79
|
+
const baseObj = new URL(baseUrl);
|
|
80
|
+
let url;
|
|
81
|
+
if (/^https?:\/\//i.test(p)) {
|
|
82
|
+
url = new URL(p);
|
|
83
|
+
}
|
|
84
|
+
else {
|
|
85
|
+
const pathPart = String(p || "/");
|
|
86
|
+
if (pathPart.startsWith("/")) {
|
|
87
|
+
const basePath = baseObj.pathname.replace(/\/$/, "");
|
|
88
|
+
const merged = `${basePath}${pathPart}` || "/";
|
|
89
|
+
url = new URL(baseObj.origin);
|
|
90
|
+
url.pathname = merged.startsWith("/") ? merged : `/${merged}`;
|
|
91
|
+
}
|
|
92
|
+
else {
|
|
93
|
+
url = new URL(pathPart, baseObj);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
if (isObject(query)) {
|
|
97
|
+
for (const [k, v] of Object.entries(query)) {
|
|
98
|
+
if (v !== undefined && v !== null)
|
|
99
|
+
url.searchParams.set(k, String(v));
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
return url.toString();
|
|
103
|
+
}
|
|
104
|
+
function slugify(text, fallback = "root") {
|
|
105
|
+
const s = String(text || "").toLowerCase().replace(/[^a-z0-9]+/g, "-").replace(/^-+|-+$/g, "").slice(0, 64);
|
|
106
|
+
return s || fallback;
|
|
107
|
+
}
|
|
108
|
+
function uniqueId(base, used) {
|
|
109
|
+
let id = base.startsWith("inv-") ? base : `inv-${base}`;
|
|
110
|
+
id = slugify(id.replace(/^inv-/, ""), "api");
|
|
111
|
+
id = `inv-${id}`;
|
|
112
|
+
if (!used.has(id)) {
|
|
113
|
+
used.add(id);
|
|
114
|
+
return id;
|
|
115
|
+
}
|
|
116
|
+
let n = 2;
|
|
117
|
+
while (used.has(`${id}-${n}`))
|
|
118
|
+
n++;
|
|
119
|
+
const out = `${id}-${n}`;
|
|
120
|
+
used.add(out);
|
|
121
|
+
return out;
|
|
122
|
+
}
|
|
123
|
+
function decodeJsonPointerSegment(seg) {
|
|
124
|
+
return seg.replace(/~1/g, "/").replace(/~0/g, "~");
|
|
125
|
+
}
|
|
126
|
+
function resolveJsonPointer(doc, ref) {
|
|
127
|
+
if (typeof ref !== "string" || !ref.startsWith("#/"))
|
|
128
|
+
return undefined;
|
|
129
|
+
const parts = ref.slice(2).split("/").map(decodeJsonPointerSegment);
|
|
130
|
+
let cur = doc;
|
|
131
|
+
for (const p of parts) {
|
|
132
|
+
if (cur === null || cur === undefined)
|
|
133
|
+
return undefined;
|
|
134
|
+
if (Array.isArray(cur)) {
|
|
135
|
+
const idx = Number(p);
|
|
136
|
+
if (!Number.isInteger(idx) || idx < 0 || idx >= cur.length)
|
|
137
|
+
return undefined;
|
|
138
|
+
cur = cur[idx];
|
|
139
|
+
}
|
|
140
|
+
else if (isObject(cur)) {
|
|
141
|
+
if (!(p in cur))
|
|
142
|
+
return undefined;
|
|
143
|
+
cur = cur[p];
|
|
144
|
+
}
|
|
145
|
+
else {
|
|
146
|
+
return undefined;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
return cur;
|
|
150
|
+
}
|
|
151
|
+
function mergeSchemas(base, extra) {
|
|
152
|
+
if (!isObject(base))
|
|
153
|
+
return isObject(extra) ? extra : {};
|
|
154
|
+
if (!isObject(extra))
|
|
155
|
+
return base;
|
|
156
|
+
const out = { ...base, ...extra };
|
|
157
|
+
// merge required
|
|
158
|
+
const req = [...new Set([...(Array.isArray(base.required) ? base.required.map(String) : []), ...(Array.isArray(extra.required) ? extra.required.map(String) : [])])];
|
|
159
|
+
if (req.length)
|
|
160
|
+
out.required = req;
|
|
161
|
+
// merge properties
|
|
162
|
+
if (isObject(base.properties) || isObject(extra.properties)) {
|
|
163
|
+
out.properties = { ...(isObject(base.properties) ? base.properties : {}), ...(isObject(extra.properties) ? extra.properties : {}) };
|
|
164
|
+
}
|
|
165
|
+
// merge items shallowly if both objects
|
|
166
|
+
if (isObject(base.items) && isObject(extra.items))
|
|
167
|
+
out.items = mergeSchemas(base.items, extra.items);
|
|
168
|
+
return out;
|
|
169
|
+
}
|
|
170
|
+
function resolveSchema(schema, doc, stack = [], memo = new Map()) {
|
|
171
|
+
if (!isObject(schema) && !Array.isArray(schema))
|
|
172
|
+
return schema;
|
|
173
|
+
if (Array.isArray(schema))
|
|
174
|
+
return schema.map((v) => resolveSchema(v, doc, stack, memo));
|
|
175
|
+
if (typeof schema.$ref === "string") {
|
|
176
|
+
const ref = schema.$ref;
|
|
177
|
+
if (stack.includes(ref))
|
|
178
|
+
return { $ref: ref };
|
|
179
|
+
if (memo.has(ref)) {
|
|
180
|
+
// cycle-safe: return memoized shallow result; if not ready, return ref marker
|
|
181
|
+
return memo.get(ref) || { $ref: ref };
|
|
182
|
+
}
|
|
183
|
+
const target = resolveJsonPointer(doc, ref);
|
|
184
|
+
if (target === undefined)
|
|
185
|
+
return { $ref: ref };
|
|
186
|
+
memo.set(ref, null);
|
|
187
|
+
const resolvedTarget = resolveSchema(target, doc, [...stack, ref], memo);
|
|
188
|
+
const sibling = { ...schema };
|
|
189
|
+
delete sibling.$ref;
|
|
190
|
+
const merged = Object.keys(sibling).length
|
|
191
|
+
? mergeSchemas(resolvedTarget, resolveSchema(sibling, doc, stack, memo))
|
|
192
|
+
: (isObject(resolvedTarget) ? resolvedTarget : { value: resolvedTarget });
|
|
193
|
+
memo.set(ref, merged);
|
|
194
|
+
return merged;
|
|
195
|
+
}
|
|
196
|
+
if (Array.isArray(schema.allOf)) {
|
|
197
|
+
let merged = {};
|
|
198
|
+
for (const part of schema.allOf) {
|
|
199
|
+
merged = mergeSchemas(merged, resolveSchema(part, doc, stack, memo));
|
|
200
|
+
}
|
|
201
|
+
const rest = { ...schema };
|
|
202
|
+
delete rest.allOf;
|
|
203
|
+
const resolvedRest = resolveSchema(rest, doc, stack, memo);
|
|
204
|
+
return mergeSchemas(merged, isObject(resolvedRest) ? resolvedRest : {});
|
|
205
|
+
}
|
|
206
|
+
const out = {};
|
|
207
|
+
for (const [k, v] of Object.entries(schema)) {
|
|
208
|
+
if (k === "properties" && isObject(v)) {
|
|
209
|
+
const props = {};
|
|
210
|
+
for (const [pk, pv] of Object.entries(v))
|
|
211
|
+
props[pk] = resolveSchema(pv, doc, stack, memo);
|
|
212
|
+
out[k] = props;
|
|
213
|
+
}
|
|
214
|
+
else if (k === "items") {
|
|
215
|
+
out[k] = resolveSchema(v, doc, stack, memo);
|
|
216
|
+
}
|
|
217
|
+
else if (k === "additionalProperties" && isObject(v)) {
|
|
218
|
+
out[k] = resolveSchema(v, doc, stack, memo);
|
|
219
|
+
}
|
|
220
|
+
else if ((k === "anyOf" || k === "oneOf") && Array.isArray(v)) {
|
|
221
|
+
out[k] = v.map((x) => resolveSchema(x, doc, stack, memo));
|
|
222
|
+
}
|
|
223
|
+
else {
|
|
224
|
+
out[k] = v;
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
return out;
|
|
228
|
+
}
|
|
229
|
+
function resolveSchemaObject(schema, doc) {
|
|
230
|
+
const resolved = resolveSchema(schema, doc);
|
|
231
|
+
return isObject(resolved) ? resolved : undefined;
|
|
232
|
+
}
|
|
233
|
+
function pickParamValue(param) {
|
|
234
|
+
if (!isObject(param))
|
|
235
|
+
return undefined;
|
|
236
|
+
if (param.example !== undefined)
|
|
237
|
+
return param.example;
|
|
238
|
+
if (isObject(param.examples)) {
|
|
239
|
+
for (const ex of Object.values(param.examples)) {
|
|
240
|
+
if (isObject(ex) && ex.value !== undefined)
|
|
241
|
+
return ex.value;
|
|
242
|
+
if (ex !== undefined)
|
|
243
|
+
return ex;
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
const schema = isObject(param.schema) ? param.schema : param;
|
|
247
|
+
if (schema.example !== undefined)
|
|
248
|
+
return schema.example;
|
|
249
|
+
if (schema.default !== undefined)
|
|
250
|
+
return schema.default;
|
|
251
|
+
if (Array.isArray(schema.enum) && schema.enum.length)
|
|
252
|
+
return schema.enum[0];
|
|
253
|
+
const t = Array.isArray(schema.type) ? schema.type[0] : schema.type;
|
|
254
|
+
if (t === "integer" || t === "number")
|
|
255
|
+
return 1;
|
|
256
|
+
if (t === "boolean")
|
|
257
|
+
return true;
|
|
258
|
+
return "sample";
|
|
259
|
+
}
|
|
260
|
+
function substitutePathParams(p, params) {
|
|
261
|
+
return String(p).replace(/\{([^}]+)\}/g, (_, name) => {
|
|
262
|
+
const param = params.find((pr) => pr.in === "path" && pr.name === name);
|
|
263
|
+
const val = pickParamValue(param);
|
|
264
|
+
return encodeURIComponent(String(val ?? "sample"));
|
|
265
|
+
});
|
|
266
|
+
}
|
|
267
|
+
function collectParams(pathItem, op) {
|
|
268
|
+
const all = [...asArray(pathItem?.parameters), ...asArray(op?.parameters)].filter(isObject);
|
|
269
|
+
// op overrides path-level by name+in
|
|
270
|
+
const map = new Map();
|
|
271
|
+
for (const p of all) {
|
|
272
|
+
const key = `${String(p.in)}:${String(p.name)}`;
|
|
273
|
+
map.set(key, p);
|
|
274
|
+
}
|
|
275
|
+
return [...map.values()];
|
|
276
|
+
}
|
|
277
|
+
function getResponseSpec(op, doc) {
|
|
278
|
+
const responses = isObject(op?.responses) ? op.responses : {};
|
|
279
|
+
const codes = Object.keys(responses);
|
|
280
|
+
const preferred = ["200", "201", "204", "default", ...codes].find((c) => c in responses);
|
|
281
|
+
if (!preferred)
|
|
282
|
+
return { status: 200 };
|
|
283
|
+
const rawResp = responses[preferred];
|
|
284
|
+
const resp = isObject(rawResp) ? rawResp : {};
|
|
285
|
+
const status = preferred === "default" ? 200 : Number(preferred);
|
|
286
|
+
let schema;
|
|
287
|
+
let contentType;
|
|
288
|
+
if (isObject(resp.content)) {
|
|
289
|
+
const jsonKey = Object.keys(resp.content).find((k) => k.toLowerCase().includes("json")) || Object.keys(resp.content)[0];
|
|
290
|
+
if (jsonKey) {
|
|
291
|
+
contentType = jsonKey;
|
|
292
|
+
const media = resp.content[jsonKey];
|
|
293
|
+
if (isObject(media) && isObject(media.schema))
|
|
294
|
+
schema = resolveSchemaObject(media.schema, doc);
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
else if (isObject(resp.schema)) { // swagger 2.0
|
|
298
|
+
schema = resolveSchemaObject(resp.schema, doc);
|
|
299
|
+
contentType = "application/json";
|
|
300
|
+
}
|
|
301
|
+
return { status: Number.isFinite(status) ? status : 200, schema, contentType };
|
|
302
|
+
}
|
|
303
|
+
function endpointsFromOpenApiDoc(doc, options = {}) {
|
|
304
|
+
const eps = [];
|
|
305
|
+
const paths = isObject(doc?.paths) ? doc.paths : {};
|
|
306
|
+
const includeMutating = options.includeMutating === true;
|
|
307
|
+
const globalConsumes = asArray(doc.consumes);
|
|
308
|
+
const basePath = typeof doc?.basePath === "string" ? doc.basePath.replace(/\/$/, "") : "";
|
|
309
|
+
for (const [rawPath, pathItem] of Object.entries(paths)) {
|
|
310
|
+
if (!isObject(pathItem))
|
|
311
|
+
continue;
|
|
312
|
+
for (const [m, op] of Object.entries(pathItem)) {
|
|
313
|
+
const method = m.toUpperCase();
|
|
314
|
+
const allowed = ["GET", "HEAD", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"];
|
|
315
|
+
if (!allowed.includes(method))
|
|
316
|
+
continue;
|
|
317
|
+
const isMutating = ["POST", "PUT", "PATCH", "DELETE"].includes(method);
|
|
318
|
+
if (isMutating && !includeMutating)
|
|
319
|
+
continue;
|
|
320
|
+
if (!isObject(op))
|
|
321
|
+
continue;
|
|
322
|
+
const params = collectParams(pathItem, op).map((p) => {
|
|
323
|
+
if (p.schema)
|
|
324
|
+
return { ...p, schema: resolveSchema(p.schema, doc) };
|
|
325
|
+
return p;
|
|
326
|
+
});
|
|
327
|
+
const substituted = substitutePathParams(rawPath, params);
|
|
328
|
+
const resolvedPath = `${basePath}${substituted.startsWith("/") ? substituted : `/${substituted}`}` || substituted;
|
|
329
|
+
const query = {};
|
|
330
|
+
const headers = {};
|
|
331
|
+
for (const p of params) {
|
|
332
|
+
const paramName = typeof p.name === "string" ? p.name : "";
|
|
333
|
+
if (!paramName)
|
|
334
|
+
continue;
|
|
335
|
+
if (p.in === "query") {
|
|
336
|
+
const val = pickParamValue(p);
|
|
337
|
+
if (val !== undefined)
|
|
338
|
+
query[paramName] = val;
|
|
339
|
+
}
|
|
340
|
+
else if (p.in === "header") {
|
|
341
|
+
const val = pickParamValue(p);
|
|
342
|
+
if (val !== undefined)
|
|
343
|
+
headers[paramName] = String(val);
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
let body = null;
|
|
347
|
+
if (isObject(op.requestBody)) {
|
|
348
|
+
const content = isObject(op.requestBody.content) ? op.requestBody.content : {};
|
|
349
|
+
const jsonKey = Object.keys(content).find((k) => k.toLowerCase().includes("json")) || Object.keys(content)[0];
|
|
350
|
+
if (jsonKey && isObject(content[jsonKey])) {
|
|
351
|
+
const media = content[jsonKey];
|
|
352
|
+
if (media.example !== undefined)
|
|
353
|
+
body = media.example;
|
|
354
|
+
else if (isObject(media.schema))
|
|
355
|
+
body = exampleFromSchema(resolveSchema(media.schema, doc));
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
else {
|
|
359
|
+
const bodyParam = params.find((p) => p.in === "body");
|
|
360
|
+
if (bodyParam) {
|
|
361
|
+
if (bodyParam.example !== undefined)
|
|
362
|
+
body = bodyParam.example;
|
|
363
|
+
else if (isObject(bodyParam.schema))
|
|
364
|
+
body = exampleFromSchema(resolveSchema(bodyParam.schema, doc));
|
|
365
|
+
else
|
|
366
|
+
body = exampleFromSchema(bodyParam);
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
const resp = getResponseSpec(op, doc);
|
|
370
|
+
if (resp.contentType && !headers.accept)
|
|
371
|
+
headers.accept = resp.contentType;
|
|
372
|
+
if (body !== null && !headers["content-type"]) {
|
|
373
|
+
const opConsumes = asArray(op.consumes);
|
|
374
|
+
const ct = opConsumes[0] || globalConsumes[0] || "application/json";
|
|
375
|
+
headers["content-type"] = String(ct);
|
|
376
|
+
}
|
|
377
|
+
eps.push({
|
|
378
|
+
id: `openapi-${method.toLowerCase()}-${slugify(rawPath)}`,
|
|
379
|
+
method,
|
|
380
|
+
path: resolvedPath.startsWith("/") ? resolvedPath : `/${resolvedPath}`,
|
|
381
|
+
query: Object.keys(query).length ? query : undefined,
|
|
382
|
+
headers,
|
|
383
|
+
body,
|
|
384
|
+
expectedStatus: resp.status,
|
|
385
|
+
jsonSchema: resp.schema,
|
|
386
|
+
expectedContentType: resp.contentType,
|
|
387
|
+
fromSpec: true
|
|
388
|
+
});
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
return eps;
|
|
392
|
+
}
|
|
393
|
+
function exampleFromSchema(schema, depth = 0) {
|
|
394
|
+
if (!isObject(schema) || depth > 8)
|
|
395
|
+
return undefined;
|
|
396
|
+
if (schema.example !== undefined)
|
|
397
|
+
return schema.example;
|
|
398
|
+
if (schema.default !== undefined)
|
|
399
|
+
return schema.default;
|
|
400
|
+
if (Array.isArray(schema.enum) && schema.enum.length)
|
|
401
|
+
return schema.enum[0];
|
|
402
|
+
if (Array.isArray(schema.allOf)) {
|
|
403
|
+
let merged = {};
|
|
404
|
+
for (const p of schema.allOf)
|
|
405
|
+
merged = mergeSchemas(merged, p);
|
|
406
|
+
return exampleFromSchema(merged, depth + 1);
|
|
407
|
+
}
|
|
408
|
+
const t = Array.isArray(schema.type) ? schema.type[0] : schema.type;
|
|
409
|
+
if (t === "object" || isObject(schema.properties)) {
|
|
410
|
+
const out = {};
|
|
411
|
+
for (const [k, v] of Object.entries(isObject(schema.properties) ? schema.properties : {})) {
|
|
412
|
+
const ex = exampleFromSchema(v, depth + 1);
|
|
413
|
+
if (ex !== undefined)
|
|
414
|
+
out[k] = ex;
|
|
415
|
+
else if (Array.isArray(schema.required) && schema.required.includes(k))
|
|
416
|
+
out[k] = "sample";
|
|
417
|
+
}
|
|
418
|
+
return out;
|
|
419
|
+
}
|
|
420
|
+
if (t === "array") {
|
|
421
|
+
const itemEx = exampleFromSchema(schema.items || {}, depth + 1);
|
|
422
|
+
return itemEx === undefined ? [] : [itemEx];
|
|
423
|
+
}
|
|
424
|
+
if (t === "integer" || t === "number")
|
|
425
|
+
return 1;
|
|
426
|
+
if (t === "boolean")
|
|
427
|
+
return true;
|
|
428
|
+
if (t === "null")
|
|
429
|
+
return null;
|
|
430
|
+
return "sample";
|
|
431
|
+
}
|
|
432
|
+
function pickBaseUrlFromSpec(doc, options) {
|
|
433
|
+
if (typeof options.baseUrl === "string" && options.baseUrl)
|
|
434
|
+
return options.baseUrl;
|
|
435
|
+
if (Array.isArray(doc?.servers) && doc.servers.length && isObject(doc.servers[0]) && typeof doc.servers[0].url === "string") {
|
|
436
|
+
return doc.servers[0].url;
|
|
437
|
+
}
|
|
438
|
+
if (typeof doc?.host === "string") {
|
|
439
|
+
const scheme = asArray(doc.schemes)[0] || "http";
|
|
440
|
+
const basePath = typeof doc.basePath === "string" ? doc.basePath : "";
|
|
441
|
+
return `${String(scheme)}://${doc.host}${basePath}`;
|
|
442
|
+
}
|
|
443
|
+
return "http://localhost";
|
|
444
|
+
}
|
|
445
|
+
function isOpenApiDoc(doc) {
|
|
446
|
+
return isObject(doc) && Boolean(doc.openapi || doc.swagger) && isObject(doc.paths);
|
|
447
|
+
}
|
|
448
|
+
function scanExportsFromText(text) {
|
|
449
|
+
const exports = [];
|
|
450
|
+
const push = (name, kind) => {
|
|
451
|
+
if (name && !exports.some((e) => e.name === name))
|
|
452
|
+
exports.push({ name, kind });
|
|
453
|
+
};
|
|
454
|
+
// export function foo / export async function foo
|
|
455
|
+
for (const m of text.matchAll(/\bexport\s+(?:async\s+)?function\s+([A-Za-z_$][A-Za-z0-9_$]*)/g))
|
|
456
|
+
push(m[1], "function");
|
|
457
|
+
// export class Foo
|
|
458
|
+
for (const m of text.matchAll(/\bexport\s+class\s+([A-Za-z_$][A-Za-z0-9_$]*)/g))
|
|
459
|
+
push(m[1], "class");
|
|
460
|
+
// export const/let/var foo
|
|
461
|
+
for (const m of text.matchAll(/\bexport\s+(?:const|let|var)\s+([A-Za-z_$][A-Za-z0-9_$]*)/g))
|
|
462
|
+
push(m[1], "const");
|
|
463
|
+
// export { a, b as c }
|
|
464
|
+
for (const m of text.matchAll(/\bexport\s*\{([^}]+)\}\s*(?:from\s*['"][^'"]+['"])?/g)) {
|
|
465
|
+
for (const part of m[1].split(",")) {
|
|
466
|
+
const name = part.trim().split(/\s+as\s+/i).pop()?.trim();
|
|
467
|
+
if (name && /^[A-Za-z_$][A-Za-z0-9_$]*$/.test(name))
|
|
468
|
+
push(name, "unknown");
|
|
469
|
+
}
|
|
470
|
+
}
|
|
471
|
+
// export default
|
|
472
|
+
if (/\bexport\s+default\b/.test(text))
|
|
473
|
+
push("default", "default");
|
|
474
|
+
// CJS module.exports = { a, b }
|
|
475
|
+
const modObj = text.match(/\bmodule\.exports\s*=\s*\{([\s\S]*?)\}/m);
|
|
476
|
+
if (modObj) {
|
|
477
|
+
for (const part of modObj[1].split(",")) {
|
|
478
|
+
const kv = part.trim().split(":")[0].trim();
|
|
479
|
+
const name = kv.split(/\s+/)[0];
|
|
480
|
+
if (name && /^[A-Za-z_$][A-Za-z0-9_$]*$/.test(name))
|
|
481
|
+
push(name, "unknown");
|
|
482
|
+
}
|
|
483
|
+
}
|
|
484
|
+
for (const m of text.matchAll(/\bexports\.([A-Za-z_$][A-Za-z0-9_$]*)\s*=/g))
|
|
485
|
+
push(m[1], "unknown");
|
|
486
|
+
return exports;
|
|
487
|
+
}
|
|
488
|
+
export async function ingestRepo(options = {}, _deps = {}) {
|
|
489
|
+
const source = options.source;
|
|
490
|
+
if (!source || typeof source !== "string") {
|
|
491
|
+
throw new VibecodeError("TARGET_MISSING", "Repo ingestor requires --source <repo-dir>");
|
|
492
|
+
}
|
|
493
|
+
const repoDir = path.resolve(options.cwd || process.cwd(), source);
|
|
494
|
+
if (!fs.existsSync(repoDir) || !fs.statSync(repoDir).isDirectory()) {
|
|
495
|
+
throw new VibecodeError("TARGET_MISSING", `Repo source not found: ${source}`);
|
|
496
|
+
}
|
|
497
|
+
const warnings = [];
|
|
498
|
+
const pkgPath = path.join(repoDir, "package.json");
|
|
499
|
+
let pkg = null;
|
|
500
|
+
if (fs.existsSync(pkgPath)) {
|
|
501
|
+
try {
|
|
502
|
+
const parsed = JSON.parse(fs.readFileSync(pkgPath, "utf8"));
|
|
503
|
+
if (isObject(parsed))
|
|
504
|
+
pkg = parsed;
|
|
505
|
+
}
|
|
506
|
+
catch {
|
|
507
|
+
warnings.push({ code: "PARSE_WARNING", message: "Could not parse package.json" });
|
|
508
|
+
}
|
|
509
|
+
}
|
|
510
|
+
const entryCandidates = [];
|
|
511
|
+
if (pkg?.exports) {
|
|
512
|
+
if (typeof pkg.exports === "string")
|
|
513
|
+
entryCandidates.push(pkg.exports);
|
|
514
|
+
else if (isObject(pkg.exports)) {
|
|
515
|
+
for (const v of Object.values(pkg.exports)) {
|
|
516
|
+
if (typeof v === "string")
|
|
517
|
+
entryCandidates.push(v);
|
|
518
|
+
else if (isObject(v))
|
|
519
|
+
for (const vv of Object.values(v))
|
|
520
|
+
if (typeof vv === "string")
|
|
521
|
+
entryCandidates.push(vv);
|
|
522
|
+
}
|
|
523
|
+
}
|
|
524
|
+
}
|
|
525
|
+
if (typeof pkg?.module === "string")
|
|
526
|
+
entryCandidates.push(pkg.module);
|
|
527
|
+
if (typeof pkg?.main === "string")
|
|
528
|
+
entryCandidates.push(pkg.main);
|
|
529
|
+
for (const c of ["src/index.mjs", "src/index.js", "index.mjs", "index.js"])
|
|
530
|
+
entryCandidates.push(c);
|
|
531
|
+
const seen = new Set();
|
|
532
|
+
const filesPresent = [];
|
|
533
|
+
const exports = [];
|
|
534
|
+
for (const rel of entryCandidates) {
|
|
535
|
+
const norm = rel.replace(/^\.\//, "");
|
|
536
|
+
if (seen.has(norm))
|
|
537
|
+
continue;
|
|
538
|
+
seen.add(norm);
|
|
539
|
+
const abs = path.join(repoDir, norm);
|
|
540
|
+
if (!fs.existsSync(abs) || !fs.statSync(abs).isFile())
|
|
541
|
+
continue;
|
|
542
|
+
filesPresent.push(norm.replace(/\\/g, "/"));
|
|
543
|
+
try {
|
|
544
|
+
const text = fs.readFileSync(abs, "utf8");
|
|
545
|
+
for (const e of scanExportsFromText(text)) {
|
|
546
|
+
if (!exports.some((x) => x.name === e.name))
|
|
547
|
+
exports.push(e);
|
|
548
|
+
}
|
|
549
|
+
}
|
|
550
|
+
catch (err) {
|
|
551
|
+
warnings.push({ code: "READ_WARNING", message: `Failed to scan ${norm}: ${errorMessage(err)}` });
|
|
552
|
+
}
|
|
553
|
+
if (exports.length >= 50)
|
|
554
|
+
break;
|
|
555
|
+
}
|
|
556
|
+
if (exports.length === 0) {
|
|
557
|
+
warnings.push({ code: "ADAPTER_DEGRADED", message: "No exports detected; emitting filesPresent-only invariant." });
|
|
558
|
+
}
|
|
559
|
+
const invariant = {
|
|
560
|
+
id: "inv-repo-exports",
|
|
561
|
+
domain: "repo",
|
|
562
|
+
type: "repo_exports",
|
|
563
|
+
severity: "hard",
|
|
564
|
+
description: "Public entrypoint preserves exported symbol contract",
|
|
565
|
+
tags: ["repo", "exports"],
|
|
566
|
+
sourceId: "src-1",
|
|
567
|
+
input: { modulePath: filesPresent[0] || "src/index.mjs", language: "js" },
|
|
568
|
+
expected: {
|
|
569
|
+
filesPresent: filesPresent.length ? filesPresent : ["package.json"].filter((f) => fs.existsSync(path.join(repoDir, f))),
|
|
570
|
+
exports
|
|
571
|
+
},
|
|
572
|
+
tolerance: {}
|
|
573
|
+
};
|
|
574
|
+
return {
|
|
575
|
+
domain: "repo",
|
|
576
|
+
sources: [{ id: "src-1", kind: "repo", ref: source, meta: { repoDir } }],
|
|
577
|
+
artifacts: { root: ".vibecode/artifacts", files: {} },
|
|
578
|
+
invariants: [invariant],
|
|
579
|
+
warnings
|
|
580
|
+
};
|
|
581
|
+
}
|
|
582
|
+
export { scanExportsFromText };
|
|
583
|
+
export async function ingestApi(options = {}, deps = {}) {
|
|
584
|
+
const source = options.source;
|
|
585
|
+
if (!source || typeof source !== "string") {
|
|
586
|
+
throw new VibecodeError("TARGET_MISSING", "API ingestor requires --source <base-url-or-openapi.json>");
|
|
587
|
+
}
|
|
588
|
+
const warnings = [];
|
|
589
|
+
let baseUrl = source;
|
|
590
|
+
let endpoints = [];
|
|
591
|
+
let specDoc = null;
|
|
592
|
+
let specMode = false;
|
|
593
|
+
if (Array.isArray(options.endpoints) && options.endpoints.length) {
|
|
594
|
+
endpoints = options.endpoints.map((e) => normalizeEndpoint(e)).filter((e) => e !== null);
|
|
595
|
+
}
|
|
596
|
+
else if (typeof options.endpointsFile === "string") {
|
|
597
|
+
const raw = JSON.parse(fs.readFileSync(path.resolve(options.cwd || process.cwd(), options.endpointsFile), "utf8"));
|
|
598
|
+
const arr = Array.isArray(raw) ? raw : (isObject(raw) && Array.isArray(raw.endpoints) ? raw.endpoints : []);
|
|
599
|
+
endpoints = arr
|
|
600
|
+
.map((e) => normalizeEndpoint(e))
|
|
601
|
+
.filter((e) => e !== null);
|
|
602
|
+
}
|
|
603
|
+
const isUrl = /^https?:\/\//i.test(source);
|
|
604
|
+
if (!isUrl) {
|
|
605
|
+
const abs = path.resolve(options.cwd || process.cwd(), source);
|
|
606
|
+
if (fs.existsSync(abs) && abs.toLowerCase().endsWith(".json")) {
|
|
607
|
+
try {
|
|
608
|
+
const doc = JSON.parse(fs.readFileSync(abs, "utf8"));
|
|
609
|
+
if (isOpenApiDoc(doc)) {
|
|
610
|
+
specDoc = doc;
|
|
611
|
+
baseUrl = pickBaseUrlFromSpec(doc, options);
|
|
612
|
+
if (endpoints.length === 0) {
|
|
613
|
+
endpoints = endpointsFromOpenApiDoc(doc, { includeMutating: options.includeMutating === true });
|
|
614
|
+
specMode = true;
|
|
615
|
+
}
|
|
616
|
+
}
|
|
617
|
+
}
|
|
618
|
+
catch (err) {
|
|
619
|
+
warnings.push({ code: "BRIDGE_FORMAT_WARNING", message: `Failed to parse OpenAPI source: ${errorMessage(err)}` });
|
|
620
|
+
}
|
|
621
|
+
}
|
|
622
|
+
}
|
|
623
|
+
if (!specDoc && typeof options.baseUrl === "string" && options.baseUrl)
|
|
624
|
+
baseUrl = options.baseUrl;
|
|
625
|
+
if (endpoints.length === 0) {
|
|
626
|
+
endpoints = [
|
|
627
|
+
{ method: "GET", path: "/health", headers: {}, body: null },
|
|
628
|
+
{ method: "GET", path: "/", headers: {}, body: null }
|
|
629
|
+
];
|
|
630
|
+
}
|
|
631
|
+
const usedIds = new Set();
|
|
632
|
+
const invariants = [];
|
|
633
|
+
for (const ep of endpoints.slice(0, 25)) {
|
|
634
|
+
const method = String(ep.method || "GET").toUpperCase();
|
|
635
|
+
const url = buildApiUrl(baseUrl, ep.path, ep.query);
|
|
636
|
+
let observed = null;
|
|
637
|
+
let probeFailed = null;
|
|
638
|
+
const shouldProbe = !(options.offline === true && specMode);
|
|
639
|
+
if (shouldProbe) {
|
|
640
|
+
try {
|
|
641
|
+
observed = await (deps.fetchJson || fetchJson)(url, {
|
|
642
|
+
method,
|
|
643
|
+
headers: ep.headers || {},
|
|
644
|
+
body: ep.body ?? null,
|
|
645
|
+
timeoutMs: ep.timeoutMs || 10000
|
|
646
|
+
});
|
|
647
|
+
}
|
|
648
|
+
catch (err) {
|
|
649
|
+
probeFailed = err;
|
|
650
|
+
}
|
|
651
|
+
}
|
|
652
|
+
const canUseSpecFallback = specMode && (options.offline === true || Boolean(probeFailed));
|
|
653
|
+
if (!observed && !canUseSpecFallback) {
|
|
654
|
+
if (probeFailed)
|
|
655
|
+
warnings.push({ code: "EXEC_ERROR", message: `Failed to probe ${method} ${ep.path}: ${errorMessage(probeFailed)}` });
|
|
656
|
+
continue;
|
|
657
|
+
}
|
|
658
|
+
if (!observed && canUseSpecFallback) {
|
|
659
|
+
const msg = options.offline === true
|
|
660
|
+
? `Offline spec-only mode: emitted ${method} ${ep.path} from OpenAPI spec`
|
|
661
|
+
: `Live probe failed for ${method} ${ep.path}; fell back to OpenAPI spec: ${probeFailed ? errorMessage(probeFailed) : "unknown error"}`;
|
|
662
|
+
warnings.push({ code: options.offline === true ? "ADAPTER_DEGRADED" : "EXEC_ERROR", message: msg });
|
|
663
|
+
}
|
|
664
|
+
const expected = {};
|
|
665
|
+
if (observed) {
|
|
666
|
+
expected.status = ep.expectedStatus ?? observed.status;
|
|
667
|
+
if (ep.jsonSchema)
|
|
668
|
+
expected.jsonSchema = ep.jsonSchema;
|
|
669
|
+
else if (observed.json !== null && isObject(observed.json))
|
|
670
|
+
expected.jsonSchema = synthesizeSchema(observed.json);
|
|
671
|
+
else if (Array.isArray(observed.json))
|
|
672
|
+
expected.jsonSchema = synthesizeSchema(observed.json);
|
|
673
|
+
if (observed.json !== null && isObject(observed.json))
|
|
674
|
+
expected.jsonSubset = observed.json;
|
|
675
|
+
if (ep.jsonPaths)
|
|
676
|
+
expected.jsonPaths = ep.jsonPaths;
|
|
677
|
+
if (specMode && ep.fromSpec && ep.jsonSchema)
|
|
678
|
+
expected.jsonSchema = ep.jsonSchema;
|
|
679
|
+
if (specMode && ep.fromSpec && ep.expectedContentType) {
|
|
680
|
+
expected.headers = { ...(isObject(expected.headers) ? expected.headers : {}), "content-type": ep.expectedContentType };
|
|
681
|
+
}
|
|
682
|
+
}
|
|
683
|
+
else {
|
|
684
|
+
expected.status = ep.expectedStatus ?? 200;
|
|
685
|
+
if (ep.jsonSchema)
|
|
686
|
+
expected.jsonSchema = ep.jsonSchema;
|
|
687
|
+
if (ep.jsonPaths)
|
|
688
|
+
expected.jsonPaths = ep.jsonPaths;
|
|
689
|
+
if (ep.expectedContentType)
|
|
690
|
+
expected.headers = { "content-type": ep.expectedContentType };
|
|
691
|
+
}
|
|
692
|
+
const baseId = ep.id || `inv-api-${method.toLowerCase()}-${slugify(ep.path)}`;
|
|
693
|
+
const id = uniqueId(baseId, usedIds);
|
|
694
|
+
const tags = ["api", "http", ep.fromSpec ? "openapi" : "auto-probe", observed ? "observed" : "spec-only"];
|
|
695
|
+
invariants.push({
|
|
696
|
+
id,
|
|
697
|
+
domain: "api",
|
|
698
|
+
type: "http",
|
|
699
|
+
severity: "hard",
|
|
700
|
+
description: `${method} ${ep.path} matches ${observed ? "observed" : "OpenAPI"} contract`,
|
|
701
|
+
tags,
|
|
702
|
+
sourceId: "src-1",
|
|
703
|
+
input: {
|
|
704
|
+
method,
|
|
705
|
+
path: ep.path,
|
|
706
|
+
headers: isObject(ep.headers) ? ep.headers : {},
|
|
707
|
+
...(ep.query ? { query: ep.query } : {}),
|
|
708
|
+
body: ep.body ?? null,
|
|
709
|
+
timeoutMs: ep.timeoutMs || 10000
|
|
710
|
+
},
|
|
711
|
+
expected,
|
|
712
|
+
tolerance: {}
|
|
713
|
+
});
|
|
714
|
+
}
|
|
715
|
+
return {
|
|
716
|
+
domain: "api",
|
|
717
|
+
sources: [{ id: "src-1", kind: specDoc ? "openapi" : "api", ref: source, meta: { baseUrl, spec: Boolean(specDoc) } }],
|
|
718
|
+
artifacts: { root: ".vibecode/artifacts", files: {} },
|
|
719
|
+
invariants,
|
|
720
|
+
warnings
|
|
721
|
+
};
|
|
722
|
+
}
|