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,14 @@
|
|
|
1
|
+
import type { CommandExecOptions, CommandExecResult } from "./types.js";
|
|
2
|
+
/**
|
|
3
|
+
* Cross-platform quote-aware command tokenizer.
|
|
4
|
+
* Handles "double quotes", 'single quotes', and Windows paths with spaces.
|
|
5
|
+
* Does NOT invoke a shell.
|
|
6
|
+
*/
|
|
7
|
+
export declare function splitCommand(cmdString: unknown): string[];
|
|
8
|
+
/**
|
|
9
|
+
* Safe command execution wrapper.
|
|
10
|
+
* Spawns directly (shell: false) by default to preserve exact arg boundaries
|
|
11
|
+
* and prevent injection; on Windows, falls back to shell: true only for
|
|
12
|
+
* .cmd/.bat shims or explicit shell: true.
|
|
13
|
+
*/
|
|
14
|
+
export declare function runCommand(target: string, args?: readonly (string | number | boolean)[], options?: CommandExecOptions): CommandExecResult;
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import { spawnSync } from "node:child_process";
|
|
2
|
+
/**
|
|
3
|
+
* Cross-platform quote-aware command tokenizer.
|
|
4
|
+
* Handles "double quotes", 'single quotes', and Windows paths with spaces.
|
|
5
|
+
* Does NOT invoke a shell.
|
|
6
|
+
*/
|
|
7
|
+
export function splitCommand(cmdString) {
|
|
8
|
+
const s = String(cmdString ?? "");
|
|
9
|
+
const args = [];
|
|
10
|
+
let cur = "";
|
|
11
|
+
let hasToken = false;
|
|
12
|
+
let inSingle = false;
|
|
13
|
+
let inDouble = false;
|
|
14
|
+
for (let i = 0; i < s.length; i++) {
|
|
15
|
+
const ch = s[i];
|
|
16
|
+
if (inSingle) {
|
|
17
|
+
if (ch === "'")
|
|
18
|
+
inSingle = false;
|
|
19
|
+
else
|
|
20
|
+
cur += ch;
|
|
21
|
+
}
|
|
22
|
+
else if (inDouble) {
|
|
23
|
+
if (ch === '"')
|
|
24
|
+
inDouble = false;
|
|
25
|
+
else if (ch === "\\" && i + 1 < s.length && s[i + 1] === '"') {
|
|
26
|
+
cur += '"';
|
|
27
|
+
i++;
|
|
28
|
+
}
|
|
29
|
+
else
|
|
30
|
+
cur += ch;
|
|
31
|
+
}
|
|
32
|
+
else {
|
|
33
|
+
if (ch === "'") {
|
|
34
|
+
inSingle = true;
|
|
35
|
+
hasToken = true;
|
|
36
|
+
}
|
|
37
|
+
else if (ch === '"') {
|
|
38
|
+
inDouble = true;
|
|
39
|
+
hasToken = true;
|
|
40
|
+
}
|
|
41
|
+
else if (/\s/.test(ch)) {
|
|
42
|
+
if (hasToken) {
|
|
43
|
+
args.push(cur);
|
|
44
|
+
cur = "";
|
|
45
|
+
hasToken = false;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
else {
|
|
49
|
+
cur += ch;
|
|
50
|
+
hasToken = true;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
if (hasToken)
|
|
55
|
+
args.push(cur);
|
|
56
|
+
return args;
|
|
57
|
+
}
|
|
58
|
+
function isCmdShim(target) {
|
|
59
|
+
return /\.(cmd|bat)$/i.test(target);
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Safe command execution wrapper.
|
|
63
|
+
* Spawns directly (shell: false) by default to preserve exact arg boundaries
|
|
64
|
+
* and prevent injection; on Windows, falls back to shell: true only for
|
|
65
|
+
* .cmd/.bat shims or explicit shell: true.
|
|
66
|
+
*/
|
|
67
|
+
export function runCommand(target, args = [], options = {}) {
|
|
68
|
+
const { stdin, env, cwd, timeoutMs = 10000, shell = false } = options;
|
|
69
|
+
const started = Date.now();
|
|
70
|
+
const useShell = shell || (process.platform === "win32" && isCmdShim(target));
|
|
71
|
+
const spawnOpts = {
|
|
72
|
+
cwd,
|
|
73
|
+
encoding: "utf8",
|
|
74
|
+
timeout: timeoutMs,
|
|
75
|
+
shell: useShell,
|
|
76
|
+
env: env ? { ...process.env, ...env } : process.env,
|
|
77
|
+
maxBuffer: 10 * 1024 * 1024
|
|
78
|
+
};
|
|
79
|
+
if (stdin !== undefined && stdin !== null) {
|
|
80
|
+
spawnOpts.input = String(stdin);
|
|
81
|
+
}
|
|
82
|
+
let res;
|
|
83
|
+
try {
|
|
84
|
+
res = spawnSync(target, args.map(String), spawnOpts);
|
|
85
|
+
}
|
|
86
|
+
catch (err) {
|
|
87
|
+
const errObj = err instanceof Error ? err : new Error(String(err));
|
|
88
|
+
return {
|
|
89
|
+
exitCode: null,
|
|
90
|
+
signal: null,
|
|
91
|
+
stdout: "",
|
|
92
|
+
stderr: errObj.message,
|
|
93
|
+
timedOut: false,
|
|
94
|
+
durationMs: Date.now() - started,
|
|
95
|
+
error: errObj
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
const errno = res.error;
|
|
99
|
+
const timedOut = errno?.code === "ETIMEDOUT" || (res.signal === "SIGTERM" && Date.now() - started >= timeoutMs - 10);
|
|
100
|
+
return {
|
|
101
|
+
exitCode: res.status,
|
|
102
|
+
signal: res.signal || null,
|
|
103
|
+
stdout: res.stdout == null ? "" : String(res.stdout),
|
|
104
|
+
stderr: res.stderr == null ? "" : String(res.stderr),
|
|
105
|
+
timedOut: Boolean(timedOut),
|
|
106
|
+
durationMs: Date.now() - started,
|
|
107
|
+
error: res.error || null
|
|
108
|
+
};
|
|
109
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { ContractV02, InvariantResult, ParityReport } from "./types.js";
|
|
2
|
+
/**
|
|
3
|
+
* Score report from oracle results.
|
|
4
|
+
* results: Array<InvariantResult>
|
|
5
|
+
* { id, type, severity: 'hard'|'soft', status: 'pass'|'fail'|'skipped'|'warn', score: number, failures?:[], durationMs? }
|
|
6
|
+
*/
|
|
7
|
+
export declare function scoreReport(contract: Partial<ContractV02> | null | undefined, results: InvariantResult[], options?: {
|
|
8
|
+
strict?: boolean;
|
|
9
|
+
}): ParityReport;
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import { SCHEMA_VERSION } from "./schema.js";
|
|
2
|
+
/**
|
|
3
|
+
* Score report from oracle results.
|
|
4
|
+
* results: Array<InvariantResult>
|
|
5
|
+
* { id, type, severity: 'hard'|'soft', status: 'pass'|'fail'|'skipped'|'warn', score: number, failures?:[], durationMs? }
|
|
6
|
+
*/
|
|
7
|
+
export function scoreReport(contract, results, options = {}) {
|
|
8
|
+
const strict = Boolean(options.strict);
|
|
9
|
+
const hardResults = [];
|
|
10
|
+
const softResults = [];
|
|
11
|
+
const otherResults = [];
|
|
12
|
+
for (const r of results) {
|
|
13
|
+
if (r.severity === "hard")
|
|
14
|
+
hardResults.push(r);
|
|
15
|
+
else if (r.severity === "soft")
|
|
16
|
+
softResults.push(r);
|
|
17
|
+
else
|
|
18
|
+
otherResults.push(r);
|
|
19
|
+
}
|
|
20
|
+
const hardPassed = hardResults.filter((r) => r.status === "pass").length;
|
|
21
|
+
const hardFailed = hardResults.filter((r) => r.status === "fail" || r.status === "error").length;
|
|
22
|
+
const hardSkipped = hardResults.filter((r) => r.status === "skipped").length;
|
|
23
|
+
const hardEvaluated = hardResults.filter((r) => r.status !== "skipped").length;
|
|
24
|
+
const softPassed = softResults.filter((r) => r.status === "pass").length;
|
|
25
|
+
const softFailed = softResults.filter((r) => r.status === "fail" || r.status === "error").length;
|
|
26
|
+
const softWarned = softResults.filter((r) => r.status === "warn").length;
|
|
27
|
+
const softSkipped = softResults.filter((r) => r.status === "skipped").length;
|
|
28
|
+
const softEvaluated = softResults.filter((r) => r.status !== "skipped").length;
|
|
29
|
+
const softScores = softResults
|
|
30
|
+
.filter((r) => r.status !== "skipped" && typeof r.score === "number")
|
|
31
|
+
.map((r) => r.score);
|
|
32
|
+
const hardParity = hardEvaluated > 0 ? hardPassed / hardEvaluated : 1.0;
|
|
33
|
+
const softParity = softEvaluated > 0 ? (softPassed + softWarned) / softEvaluated : 1.0; // warn counts as non-fail in "parity" sense
|
|
34
|
+
const softMeanScore = softScores.length > 0 ? softScores.reduce((a, b) => a + b, 0) / softScores.length : 1.0;
|
|
35
|
+
let compositeScore = 0.0;
|
|
36
|
+
if (hardEvaluated > 0 && softEvaluated > 0) {
|
|
37
|
+
compositeScore = 0.7 * hardParity + 0.3 * softMeanScore;
|
|
38
|
+
}
|
|
39
|
+
else if (hardEvaluated > 0) {
|
|
40
|
+
compositeScore = hardParity;
|
|
41
|
+
}
|
|
42
|
+
else if (softEvaluated > 0) {
|
|
43
|
+
compositeScore = softMeanScore;
|
|
44
|
+
}
|
|
45
|
+
else {
|
|
46
|
+
compositeScore = 0.0;
|
|
47
|
+
}
|
|
48
|
+
const thresholds = contract?.thresholds;
|
|
49
|
+
const hardReq = typeof thresholds?.hardBehavioralParity === "number" ? thresholds.hardBehavioralParity : 1.0;
|
|
50
|
+
const softReq = typeof thresholds?.softPerceptualParity === "number" ? thresholds.softPerceptualParity : 0.85;
|
|
51
|
+
const softGateMode = strict ? "fail" : (thresholds?.softGateMode || "warn");
|
|
52
|
+
const reasons = [];
|
|
53
|
+
let status = "pass";
|
|
54
|
+
if (hardEvaluated === 0 && softEvaluated === 0) {
|
|
55
|
+
status = "incomplete";
|
|
56
|
+
reasons.push("No invariants were evaluated (all skipped or none present).");
|
|
57
|
+
}
|
|
58
|
+
else {
|
|
59
|
+
if (hardEvaluated > 0 && hardParity < hardReq - 1e-12) {
|
|
60
|
+
status = "fail";
|
|
61
|
+
reasons.push(`Hard behavioral parity ${(hardParity * 100).toFixed(1)}% < required ${(hardReq * 100).toFixed(1)}%`);
|
|
62
|
+
}
|
|
63
|
+
const softBelowMean = softEvaluated > 0 && softMeanScore < softReq - 1e-12;
|
|
64
|
+
const softHasFail = softFailed > 0;
|
|
65
|
+
if (softBelowMean || softHasFail) {
|
|
66
|
+
if (softGateMode === "fail") {
|
|
67
|
+
status = "fail";
|
|
68
|
+
if (softBelowMean && !reasons.some((r) => r.includes("Soft perceptual"))) {
|
|
69
|
+
reasons.push(`Soft perceptual score ${softMeanScore.toFixed(3)} < required ${softReq.toFixed(3)} (--strict/fail mode)`);
|
|
70
|
+
}
|
|
71
|
+
else if (softHasFail && !reasons.some((r) => r.includes("soft invariant"))) {
|
|
72
|
+
reasons.push(`${softFailed} soft invariant(s) failed tolerance (--strict/fail mode)`);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
else {
|
|
76
|
+
if (status !== "fail")
|
|
77
|
+
status = "pass_with_warnings";
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
if (status === "pass" && softWarned > 0 && softGateMode !== "fail") {
|
|
81
|
+
status = "pass_with_warnings";
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
let exitCode = 0;
|
|
85
|
+
if (status === "fail")
|
|
86
|
+
exitCode = 1;
|
|
87
|
+
else if (status === "incomplete")
|
|
88
|
+
exitCode = 2;
|
|
89
|
+
else
|
|
90
|
+
exitCode = 0;
|
|
91
|
+
let nextAction = "none";
|
|
92
|
+
if (hardFailed > 0 || (hardEvaluated > 0 && hardParity < hardReq - 1e-12))
|
|
93
|
+
nextAction = "fix_hard_invariants_first";
|
|
94
|
+
else if (status === "incomplete")
|
|
95
|
+
nextAction = "add_or_enable_invariants_or_adapters";
|
|
96
|
+
else if (status === "pass_with_warnings")
|
|
97
|
+
nextAction = "review_soft_warnings";
|
|
98
|
+
else
|
|
99
|
+
nextAction = "none";
|
|
100
|
+
const total = results.length;
|
|
101
|
+
const passedCount = results.filter((r) => r.status === "pass").length;
|
|
102
|
+
return {
|
|
103
|
+
schemaVersion: SCHEMA_VERSION,
|
|
104
|
+
timestamp: new Date().toISOString(),
|
|
105
|
+
// v0.1 compatible fields
|
|
106
|
+
passed: passedCount,
|
|
107
|
+
total,
|
|
108
|
+
parity: total > 0 ? passedCount / total : 0.0,
|
|
109
|
+
// v0.2 fields
|
|
110
|
+
compositeScore,
|
|
111
|
+
hard: {
|
|
112
|
+
passed: hardPassed,
|
|
113
|
+
failed: hardFailed,
|
|
114
|
+
skipped: hardSkipped,
|
|
115
|
+
evaluated: hardEvaluated,
|
|
116
|
+
total: hardResults.length,
|
|
117
|
+
parity: hardEvaluated > 0 ? hardPassed / hardEvaluated : 1.0
|
|
118
|
+
},
|
|
119
|
+
soft: {
|
|
120
|
+
passed: softPassed,
|
|
121
|
+
warned: softWarned,
|
|
122
|
+
failed: softFailed,
|
|
123
|
+
skipped: softSkipped,
|
|
124
|
+
evaluated: softEvaluated,
|
|
125
|
+
total: softResults.length,
|
|
126
|
+
meanScore: softMeanScore,
|
|
127
|
+
parity: softEvaluated > 0 ? (softPassed + softWarned) / softEvaluated : 1.0
|
|
128
|
+
},
|
|
129
|
+
gate: {
|
|
130
|
+
passed: status === "pass" || status === "pass_with_warnings",
|
|
131
|
+
status,
|
|
132
|
+
exitCode,
|
|
133
|
+
reasons
|
|
134
|
+
},
|
|
135
|
+
nextAction,
|
|
136
|
+
results
|
|
137
|
+
};
|
|
138
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { ImageDimensions, RgbaImage } from "./types.js";
|
|
2
|
+
declare const PNG_SIG: Buffer<ArrayBuffer>;
|
|
3
|
+
declare function crc32(buf: Buffer | Uint8Array): number;
|
|
4
|
+
export declare function decodePng(buffer: Buffer | Uint8Array): RgbaImage;
|
|
5
|
+
export declare function encodePng({ width, height, data }: RgbaImage): Buffer;
|
|
6
|
+
export declare function readImageDimensions(buffer: Buffer | Uint8Array): ImageDimensions | null;
|
|
7
|
+
export { PNG_SIG, crc32 };
|
package/dist/core/png.js
ADDED
|
@@ -0,0 +1,303 @@
|
|
|
1
|
+
import zlib from "node:zlib";
|
|
2
|
+
import { VibecodeError, errorMessage } from "./errors.js";
|
|
3
|
+
const PNG_SIG = Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]);
|
|
4
|
+
const CRC_TABLE = (() => {
|
|
5
|
+
const table = new Uint32Array(256);
|
|
6
|
+
for (let n = 0; n < 256; n++) {
|
|
7
|
+
let c = n;
|
|
8
|
+
for (let k = 0; k < 8; k++)
|
|
9
|
+
c = c & 1 ? 0xedb88320 ^ (c >>> 1) : c >>> 1;
|
|
10
|
+
table[n] = c >>> 0;
|
|
11
|
+
}
|
|
12
|
+
return table;
|
|
13
|
+
})();
|
|
14
|
+
function toBuffer(input) {
|
|
15
|
+
return Buffer.isBuffer(input) ? input : Buffer.from(input.buffer, input.byteOffset, input.byteLength);
|
|
16
|
+
}
|
|
17
|
+
function crc32(buf) {
|
|
18
|
+
let c = 0xffffffff;
|
|
19
|
+
for (let i = 0; i < buf.length; i++)
|
|
20
|
+
c = CRC_TABLE[(c ^ buf[i]) & 0xff] ^ (c >>> 8);
|
|
21
|
+
return (c ^ 0xffffffff) >>> 0;
|
|
22
|
+
}
|
|
23
|
+
function readUInt32(buf, off) {
|
|
24
|
+
return ((buf[off] << 24) | (buf[off + 1] << 16) | (buf[off + 2] << 8) | buf[off + 3]) >>> 0;
|
|
25
|
+
}
|
|
26
|
+
function writeUInt32(buf, off, value) {
|
|
27
|
+
buf[off] = (value >>> 24) & 0xff;
|
|
28
|
+
buf[off + 1] = (value >>> 16) & 0xff;
|
|
29
|
+
buf[off + 2] = (value >>> 8) & 0xff;
|
|
30
|
+
buf[off + 3] = value & 0xff;
|
|
31
|
+
}
|
|
32
|
+
function paeth(a, b, c) {
|
|
33
|
+
const p = a + b - c;
|
|
34
|
+
const pa = Math.abs(p - a);
|
|
35
|
+
const pb = Math.abs(p - b);
|
|
36
|
+
const pc = Math.abs(p - c);
|
|
37
|
+
if (pa <= pb && pa <= pc)
|
|
38
|
+
return a;
|
|
39
|
+
if (pb <= pc)
|
|
40
|
+
return b;
|
|
41
|
+
return c;
|
|
42
|
+
}
|
|
43
|
+
function channelsFor(colorType) {
|
|
44
|
+
switch (colorType) {
|
|
45
|
+
case 0: return 1;
|
|
46
|
+
case 2: return 3;
|
|
47
|
+
case 3: return 1;
|
|
48
|
+
case 4: return 2;
|
|
49
|
+
case 6: return 4;
|
|
50
|
+
default: throw new VibecodeError("UNSUPPORTED_FORMAT", `Unsupported PNG color type ${colorType}`);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
export function decodePng(buffer) {
|
|
54
|
+
const buf = toBuffer(buffer);
|
|
55
|
+
if (buf.length < 8 || !buf.subarray(0, 8).equals(PNG_SIG)) {
|
|
56
|
+
throw new VibecodeError("UNSUPPORTED_FORMAT", "Not a PNG file (bad signature)");
|
|
57
|
+
}
|
|
58
|
+
let off = 8;
|
|
59
|
+
let width = 0, height = 0, bitDepth = 0, colorType = 0, interlace = 0;
|
|
60
|
+
let palette = null;
|
|
61
|
+
let trns = null;
|
|
62
|
+
const idat = [];
|
|
63
|
+
while (off + 8 <= buf.length) {
|
|
64
|
+
const len = readUInt32(buf, off);
|
|
65
|
+
const type = buf.toString("ascii", off + 4, off + 8);
|
|
66
|
+
const dataStart = off + 8;
|
|
67
|
+
const dataEnd = dataStart + len;
|
|
68
|
+
if (dataEnd + 4 > buf.length)
|
|
69
|
+
throw new VibecodeError("UNSUPPORTED_FORMAT", "Truncated PNG chunk");
|
|
70
|
+
const data = buf.subarray(dataStart, dataEnd);
|
|
71
|
+
if (type === "IHDR") {
|
|
72
|
+
width = readUInt32(data, 0);
|
|
73
|
+
height = readUInt32(data, 4);
|
|
74
|
+
bitDepth = data[8];
|
|
75
|
+
colorType = data[9];
|
|
76
|
+
interlace = data[12];
|
|
77
|
+
}
|
|
78
|
+
else if (type === "PLTE") {
|
|
79
|
+
palette = Buffer.from(data);
|
|
80
|
+
}
|
|
81
|
+
else if (type === "tRNS") {
|
|
82
|
+
trns = Buffer.from(data);
|
|
83
|
+
}
|
|
84
|
+
else if (type === "IDAT") {
|
|
85
|
+
idat.push(Buffer.from(data));
|
|
86
|
+
}
|
|
87
|
+
else if (type === "IEND") {
|
|
88
|
+
break;
|
|
89
|
+
}
|
|
90
|
+
off = dataEnd + 4; // skip CRC
|
|
91
|
+
}
|
|
92
|
+
if (!width || !height)
|
|
93
|
+
throw new VibecodeError("UNSUPPORTED_FORMAT", "PNG missing IHDR");
|
|
94
|
+
if (bitDepth !== 8)
|
|
95
|
+
throw new VibecodeError("UNSUPPORTED_FORMAT", `Unsupported PNG bit depth ${bitDepth} (only 8-bit supported)`);
|
|
96
|
+
if (interlace !== 0)
|
|
97
|
+
throw new VibecodeError("UNSUPPORTED_FORMAT", "Interlaced PNG is not supported; convert to non-interlaced");
|
|
98
|
+
const channels = channelsFor(colorType);
|
|
99
|
+
if (colorType === 3 && !palette)
|
|
100
|
+
throw new VibecodeError("UNSUPPORTED_FORMAT", "Indexed PNG missing PLTE");
|
|
101
|
+
let raw;
|
|
102
|
+
try {
|
|
103
|
+
raw = zlib.inflateSync(Buffer.concat(idat));
|
|
104
|
+
}
|
|
105
|
+
catch (err) {
|
|
106
|
+
throw new VibecodeError("UNSUPPORTED_FORMAT", `Failed to inflate PNG data: ${errorMessage(err)}`);
|
|
107
|
+
}
|
|
108
|
+
const rowLen = width * channels;
|
|
109
|
+
const expected = (rowLen + 1) * height;
|
|
110
|
+
if (raw.length < expected)
|
|
111
|
+
throw new VibecodeError("UNSUPPORTED_FORMAT", "PNG pixel data is truncated");
|
|
112
|
+
const pixels = new Uint8Array(height * rowLen);
|
|
113
|
+
let srcOff = 0;
|
|
114
|
+
let prev = new Uint8Array(rowLen);
|
|
115
|
+
for (let y = 0; y < height; y++) {
|
|
116
|
+
const filter = raw[srcOff++];
|
|
117
|
+
const rowStart = y * rowLen;
|
|
118
|
+
for (let i = 0; i < rowLen; i++) {
|
|
119
|
+
const rawByte = raw[srcOff + i];
|
|
120
|
+
const left = i >= channels ? pixels[rowStart + i - channels] : 0;
|
|
121
|
+
const up = prev[i];
|
|
122
|
+
const upLeft = i >= channels ? prev[i - channels] : 0;
|
|
123
|
+
let value;
|
|
124
|
+
switch (filter) {
|
|
125
|
+
case 0:
|
|
126
|
+
value = rawByte;
|
|
127
|
+
break;
|
|
128
|
+
case 1:
|
|
129
|
+
value = rawByte + left;
|
|
130
|
+
break;
|
|
131
|
+
case 2:
|
|
132
|
+
value = rawByte + up;
|
|
133
|
+
break;
|
|
134
|
+
case 3:
|
|
135
|
+
value = rawByte + ((left + up) >> 1);
|
|
136
|
+
break;
|
|
137
|
+
case 4:
|
|
138
|
+
value = rawByte + paeth(left, up, upLeft);
|
|
139
|
+
break;
|
|
140
|
+
default: throw new VibecodeError("UNSUPPORTED_FORMAT", `Unsupported PNG filter ${filter}`);
|
|
141
|
+
}
|
|
142
|
+
pixels[rowStart + i] = value & 0xff;
|
|
143
|
+
}
|
|
144
|
+
prev = pixels.subarray(rowStart, rowStart + rowLen);
|
|
145
|
+
srcOff += rowLen;
|
|
146
|
+
}
|
|
147
|
+
// Convert to RGBA
|
|
148
|
+
const data = new Uint8Array(width * height * 4);
|
|
149
|
+
for (let i = 0, p = 0; i < width * height; i++) {
|
|
150
|
+
const s = i * channels;
|
|
151
|
+
if (colorType === 0) {
|
|
152
|
+
const g = pixels[s];
|
|
153
|
+
data[p++] = g;
|
|
154
|
+
data[p++] = g;
|
|
155
|
+
data[p++] = g;
|
|
156
|
+
data[p++] = 255;
|
|
157
|
+
}
|
|
158
|
+
else if (colorType === 2) {
|
|
159
|
+
data[p++] = pixels[s];
|
|
160
|
+
data[p++] = pixels[s + 1];
|
|
161
|
+
data[p++] = pixels[s + 2];
|
|
162
|
+
data[p++] = 255;
|
|
163
|
+
}
|
|
164
|
+
else if (colorType === 3 && palette) {
|
|
165
|
+
const idx = pixels[s];
|
|
166
|
+
const pr = palette[idx * 3] ?? 0;
|
|
167
|
+
const pg = palette[idx * 3 + 1] ?? 0;
|
|
168
|
+
const pb = palette[idx * 3 + 2] ?? 0;
|
|
169
|
+
data[p++] = pr;
|
|
170
|
+
data[p++] = pg;
|
|
171
|
+
data[p++] = pb;
|
|
172
|
+
data[p++] = trns && idx < trns.length ? trns[idx] : 255;
|
|
173
|
+
}
|
|
174
|
+
else if (colorType === 4) {
|
|
175
|
+
const g = pixels[s];
|
|
176
|
+
data[p++] = g;
|
|
177
|
+
data[p++] = g;
|
|
178
|
+
data[p++] = g;
|
|
179
|
+
data[p++] = pixels[s + 1];
|
|
180
|
+
}
|
|
181
|
+
else {
|
|
182
|
+
data[p++] = pixels[s];
|
|
183
|
+
data[p++] = pixels[s + 1];
|
|
184
|
+
data[p++] = pixels[s + 2];
|
|
185
|
+
data[p++] = pixels[s + 3];
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
return { width, height, data };
|
|
189
|
+
}
|
|
190
|
+
function buildChunk(type, data) {
|
|
191
|
+
const typeBuf = Buffer.from(type, "ascii");
|
|
192
|
+
const dataBuf = toBuffer(data);
|
|
193
|
+
const out = Buffer.alloc(12 + dataBuf.length);
|
|
194
|
+
writeUInt32(out, 0, dataBuf.length);
|
|
195
|
+
typeBuf.copy(out, 4);
|
|
196
|
+
dataBuf.copy(out, 8);
|
|
197
|
+
const crc = crc32(Buffer.concat([typeBuf, dataBuf]));
|
|
198
|
+
writeUInt32(out, 8 + dataBuf.length, crc);
|
|
199
|
+
return out;
|
|
200
|
+
}
|
|
201
|
+
export function encodePng({ width, height, data }) {
|
|
202
|
+
if (!Number.isInteger(width) || !Number.isInteger(height) || width <= 0 || height <= 0) {
|
|
203
|
+
throw new VibecodeError("UNSUPPORTED_FORMAT", "encodePng requires positive integer width/height");
|
|
204
|
+
}
|
|
205
|
+
if (!data || data.length < width * height * 4) {
|
|
206
|
+
throw new VibecodeError("UNSUPPORTED_FORMAT", "encodePng requires RGBA data of width*height*4 bytes");
|
|
207
|
+
}
|
|
208
|
+
const rowLen = width * 4;
|
|
209
|
+
const raw = Buffer.alloc((rowLen + 1) * height);
|
|
210
|
+
for (let y = 0; y < height; y++) {
|
|
211
|
+
raw[y * (rowLen + 1)] = 0;
|
|
212
|
+
raw.set(data.subarray(y * rowLen, y * rowLen + rowLen), y * (rowLen + 1) + 1);
|
|
213
|
+
}
|
|
214
|
+
const ihdr = Buffer.alloc(13);
|
|
215
|
+
writeUInt32(ihdr, 0, width);
|
|
216
|
+
writeUInt32(ihdr, 4, height);
|
|
217
|
+
ihdr[8] = 8; // bit depth
|
|
218
|
+
ihdr[9] = 6; // RGBA
|
|
219
|
+
ihdr[10] = 0;
|
|
220
|
+
ihdr[11] = 0;
|
|
221
|
+
ihdr[12] = 0;
|
|
222
|
+
const compressed = zlib.deflateSync(raw, { level: 9 });
|
|
223
|
+
return Buffer.concat([
|
|
224
|
+
PNG_SIG,
|
|
225
|
+
buildChunk("IHDR", ihdr),
|
|
226
|
+
buildChunk("IDAT", compressed),
|
|
227
|
+
buildChunk("IEND", Buffer.alloc(0))
|
|
228
|
+
]);
|
|
229
|
+
}
|
|
230
|
+
export function readImageDimensions(buffer) {
|
|
231
|
+
const buf = toBuffer(buffer);
|
|
232
|
+
if (buf.length >= 8 && buf.subarray(0, 8).equals(PNG_SIG)) {
|
|
233
|
+
if (buf.length < 24)
|
|
234
|
+
return null;
|
|
235
|
+
return { format: "png", width: readUInt32(buf, 16), height: readUInt32(buf, 20) };
|
|
236
|
+
}
|
|
237
|
+
// JPEG
|
|
238
|
+
if (buf.length >= 4 && buf[0] === 0xff && buf[1] === 0xd8) {
|
|
239
|
+
let off = 2;
|
|
240
|
+
while (off + 4 <= buf.length) {
|
|
241
|
+
if (buf[off] !== 0xff) {
|
|
242
|
+
off++;
|
|
243
|
+
continue;
|
|
244
|
+
}
|
|
245
|
+
let marker = buf[off + 1];
|
|
246
|
+
while (marker === 0xff && off + 2 < buf.length) {
|
|
247
|
+
off++;
|
|
248
|
+
marker = buf[off + 1];
|
|
249
|
+
}
|
|
250
|
+
if (marker === 0xff)
|
|
251
|
+
break;
|
|
252
|
+
if (marker === 0xd8 || marker === 0xd9) {
|
|
253
|
+
off += 2;
|
|
254
|
+
continue;
|
|
255
|
+
}
|
|
256
|
+
if (marker >= 0xd0 && marker <= 0xd7) {
|
|
257
|
+
off += 2;
|
|
258
|
+
continue;
|
|
259
|
+
}
|
|
260
|
+
if (off + 4 > buf.length)
|
|
261
|
+
break;
|
|
262
|
+
const len = buf.readUInt16BE(off + 2);
|
|
263
|
+
if (len < 2)
|
|
264
|
+
break;
|
|
265
|
+
if (marker >= 0xc0 && marker <= 0xcf && marker !== 0xc4 && marker !== 0xc8 && marker !== 0xcc) {
|
|
266
|
+
if (off + 9 > buf.length)
|
|
267
|
+
break;
|
|
268
|
+
const height = buf.readUInt16BE(off + 5);
|
|
269
|
+
const width = buf.readUInt16BE(off + 7);
|
|
270
|
+
return { format: "jpeg", width, height };
|
|
271
|
+
}
|
|
272
|
+
off += 2 + len;
|
|
273
|
+
}
|
|
274
|
+
return null;
|
|
275
|
+
}
|
|
276
|
+
// WebP
|
|
277
|
+
if (buf.length >= 30 && buf.toString("ascii", 0, 4) === "RIFF" && buf.toString("ascii", 8, 12) === "WEBP") {
|
|
278
|
+
const kind = buf.toString("ascii", 12, 16);
|
|
279
|
+
if (kind === "VP8X") {
|
|
280
|
+
const width = 1 + (buf[24] | (buf[25] << 8) | (buf[26] << 16));
|
|
281
|
+
const height = 1 + (buf[27] | (buf[28] << 8) | (buf[29] << 16));
|
|
282
|
+
return { format: "webp", width, height };
|
|
283
|
+
}
|
|
284
|
+
if (kind === "VP8 ") {
|
|
285
|
+
if (buf[23] !== 0x9d || buf[24] !== 0x01 || buf[25] !== 0x2a)
|
|
286
|
+
return null;
|
|
287
|
+
const width = buf.readUInt16LE(26) & 0x3fff;
|
|
288
|
+
const height = buf.readUInt16LE(28) & 0x3fff;
|
|
289
|
+
return { format: "webp", width, height };
|
|
290
|
+
}
|
|
291
|
+
if (kind === "VP8L") {
|
|
292
|
+
if (buf[20] !== 0x2f)
|
|
293
|
+
return null;
|
|
294
|
+
const bits = buf.readUInt32LE(21);
|
|
295
|
+
const width = (bits & 0x3fff) + 1;
|
|
296
|
+
const height = ((bits >> 14) & 0x3fff) + 1;
|
|
297
|
+
return { format: "webp", width, height };
|
|
298
|
+
}
|
|
299
|
+
return null;
|
|
300
|
+
}
|
|
301
|
+
return null;
|
|
302
|
+
}
|
|
303
|
+
export { PNG_SIG, crc32 };
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { ContractV02, Domain, JsonPathQueryResult, ValidationError, ValidationResult } from "./types.js";
|
|
2
|
+
export declare const SCHEMA_VERSION = "0.2.0";
|
|
3
|
+
export declare const VALID_SLICE_STATUSES: Set<string>;
|
|
4
|
+
declare const VALID_DOMAINS: Set<string>;
|
|
5
|
+
declare const VALID_SEVERITIES: Set<string>;
|
|
6
|
+
declare const VALID_TYPES: Set<string>;
|
|
7
|
+
export declare function normalizeContract(raw: unknown): ContractV02;
|
|
8
|
+
export declare function validateContract(contract: unknown): ValidationResult;
|
|
9
|
+
export declare function assertValidContract(contract: unknown): ContractV02;
|
|
10
|
+
export declare function validateJsonSchema(value: unknown, schema: unknown, path?: string): ValidationError[];
|
|
11
|
+
export declare function queryJsonPath(obj: unknown, expr: string): JsonPathQueryResult;
|
|
12
|
+
export declare function makeDefaultContract({ name, domain, source }?: {
|
|
13
|
+
name?: string;
|
|
14
|
+
domain?: Domain | string;
|
|
15
|
+
source?: string;
|
|
16
|
+
}): ContractV02;
|
|
17
|
+
export { VALID_DOMAINS, VALID_SEVERITIES, VALID_TYPES };
|