@polyxd/runtime 0.4.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 +202 -0
- package/NOTICE +224 -0
- package/README.md +85 -0
- package/dist/adapters/anthropic.d.ts +22 -0
- package/dist/adapters/anthropic.js +48 -0
- package/dist/adapters/anthropic.js.map +1 -0
- package/dist/adapters/gemini.d.ts +14 -0
- package/dist/adapters/gemini.js +42 -0
- package/dist/adapters/gemini.js.map +1 -0
- package/dist/adapters/http.d.ts +25 -0
- package/dist/adapters/http.js +92 -0
- package/dist/adapters/http.js.map +1 -0
- package/dist/adapters/openai.d.ts +31 -0
- package/dist/adapters/openai.js +89 -0
- package/dist/adapters/openai.js.map +1 -0
- package/dist/audit.d.ts +17 -0
- package/dist/audit.js +48 -0
- package/dist/audit.js.map +1 -0
- package/dist/catalog.generated.d.ts +4 -0
- package/dist/catalog.generated.js +618 -0
- package/dist/catalog.generated.js.map +1 -0
- package/dist/direction.d.ts +22 -0
- package/dist/direction.js +95 -0
- package/dist/direction.js.map +1 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.js +10 -0
- package/dist/index.js.map +1 -0
- package/dist/memory.d.ts +25 -0
- package/dist/memory.js +38 -0
- package/dist/memory.js.map +1 -0
- package/dist/parse.d.ts +15 -0
- package/dist/parse.js +35 -0
- package/dist/parse.js.map +1 -0
- package/dist/prompt.d.ts +47 -0
- package/dist/prompt.js +242 -0
- package/dist/prompt.js.map +1 -0
- package/dist/runtime.d.ts +131 -0
- package/dist/runtime.js +190 -0
- package/dist/runtime.js.map +1 -0
- package/dist/types.d.ts +104 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/package.json +42 -0
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/** A model provider refused or failed. The message never contains the key or the prompt. */
|
|
2
|
+
export declare class GeneratorError extends Error {
|
|
3
|
+
readonly provider: string;
|
|
4
|
+
/** The HTTP status, when the provider answered with one. */
|
|
5
|
+
readonly status?: number;
|
|
6
|
+
constructor(provider: string, message: string, status?: number);
|
|
7
|
+
}
|
|
8
|
+
/** Options every adapter takes. */
|
|
9
|
+
export interface HttpOptions {
|
|
10
|
+
/** Your own `fetch`, for a proxy, retries or tests. Defaults to the global one. */
|
|
11
|
+
fetch?: typeof fetch;
|
|
12
|
+
/** Extra request headers. */
|
|
13
|
+
headers?: Record<string, string>;
|
|
14
|
+
}
|
|
15
|
+
/** POSTs JSON and returns the streaming response, or throws a `GeneratorError` for a non-2xx answer. */
|
|
16
|
+
export declare function post(provider: string, http: HttpOptions, url: string, headers: Record<string, string>, body: unknown, signal?: AbortSignal): Promise<ReadableStream<Uint8Array>>;
|
|
17
|
+
export interface ServerEvent {
|
|
18
|
+
event?: string;
|
|
19
|
+
data: string;
|
|
20
|
+
}
|
|
21
|
+
/** Server-sent events from a response body, whatever the chunk boundaries. */
|
|
22
|
+
export declare function serverEvents(body: ReadableStream<Uint8Array>): AsyncGenerator<ServerEvent>;
|
|
23
|
+
/** Parses one event's data, or throws a `GeneratorError` that says which provider sent something odd. */
|
|
24
|
+
export declare function json(provider: string, data: string): any;
|
|
25
|
+
export declare const trimSlash: (url: string) => string;
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
/** A model provider refused or failed. The message never contains the key or the prompt. */
|
|
2
|
+
export class GeneratorError extends Error {
|
|
3
|
+
provider;
|
|
4
|
+
/** The HTTP status, when the provider answered with one. */
|
|
5
|
+
status;
|
|
6
|
+
constructor(provider, message, status) {
|
|
7
|
+
super(message);
|
|
8
|
+
this.name = "GeneratorError";
|
|
9
|
+
this.provider = provider;
|
|
10
|
+
this.status = status;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
/** POSTs JSON and returns the streaming response, or throws a `GeneratorError` for a non-2xx answer. */
|
|
14
|
+
export async function post(provider, http, url, headers, body, signal) {
|
|
15
|
+
const f = http.fetch ?? globalThis.fetch;
|
|
16
|
+
const res = await f(url, { method: "POST", headers: { "content-type": "application/json", ...headers, ...http.headers }, body: JSON.stringify(body), signal });
|
|
17
|
+
if (!res.ok) {
|
|
18
|
+
let detail = "";
|
|
19
|
+
try {
|
|
20
|
+
detail = (await res.text()).slice(0, 500);
|
|
21
|
+
}
|
|
22
|
+
catch {
|
|
23
|
+
// The status alone will do.
|
|
24
|
+
}
|
|
25
|
+
throw new GeneratorError(provider, `${provider} answered ${res.status}${detail ? `: ${detail}` : ""}`, res.status);
|
|
26
|
+
}
|
|
27
|
+
if (!res.body)
|
|
28
|
+
throw new GeneratorError(provider, `${provider} answered with no body`, res.status);
|
|
29
|
+
return res.body;
|
|
30
|
+
}
|
|
31
|
+
/** Server-sent events from a response body, whatever the chunk boundaries. */
|
|
32
|
+
export async function* serverEvents(body) {
|
|
33
|
+
const reader = body.getReader();
|
|
34
|
+
const decoder = new TextDecoder();
|
|
35
|
+
let buffer = "";
|
|
36
|
+
let event;
|
|
37
|
+
let data = [];
|
|
38
|
+
const ready = [];
|
|
39
|
+
const line = (l) => {
|
|
40
|
+
if (l === "") {
|
|
41
|
+
if (data.length)
|
|
42
|
+
ready.push({ event, data: data.join("\n") });
|
|
43
|
+
event = undefined;
|
|
44
|
+
data = [];
|
|
45
|
+
}
|
|
46
|
+
else if (l.startsWith(":")) {
|
|
47
|
+
// A comment, used as a keep-alive.
|
|
48
|
+
}
|
|
49
|
+
else {
|
|
50
|
+
const colon = l.indexOf(":");
|
|
51
|
+
const field = colon < 0 ? l : l.slice(0, colon);
|
|
52
|
+
const value = colon < 0 ? "" : l.slice(colon + 1).replace(/^ /, "");
|
|
53
|
+
if (field === "event")
|
|
54
|
+
event = value;
|
|
55
|
+
else if (field === "data")
|
|
56
|
+
data.push(value);
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
try {
|
|
60
|
+
for (;;) {
|
|
61
|
+
const { done, value } = await reader.read();
|
|
62
|
+
buffer += done ? decoder.decode() : decoder.decode(value, { stream: true });
|
|
63
|
+
let nl;
|
|
64
|
+
while ((nl = buffer.indexOf("\n")) >= 0) {
|
|
65
|
+
line(buffer.slice(0, nl).replace(/\r$/, ""));
|
|
66
|
+
buffer = buffer.slice(nl + 1);
|
|
67
|
+
}
|
|
68
|
+
if (done) {
|
|
69
|
+
if (buffer)
|
|
70
|
+
line(buffer.replace(/\r$/, ""));
|
|
71
|
+
line("");
|
|
72
|
+
}
|
|
73
|
+
yield* ready.splice(0);
|
|
74
|
+
if (done)
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
finally {
|
|
79
|
+
reader.releaseLock();
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
/** Parses one event's data, or throws a `GeneratorError` that says which provider sent something odd. */
|
|
83
|
+
export function json(provider, data) {
|
|
84
|
+
try {
|
|
85
|
+
return JSON.parse(data);
|
|
86
|
+
}
|
|
87
|
+
catch {
|
|
88
|
+
throw new GeneratorError(provider, `${provider} sent an event that isn't JSON`);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
export const trimSlash = (url) => url.replace(/\/+$/, "");
|
|
92
|
+
//# sourceMappingURL=http.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"http.js","sourceRoot":"","sources":["../../src/adapters/http.ts"],"names":[],"mappings":"AAAA,4FAA4F;AAC5F,MAAM,OAAO,cAAe,SAAQ,KAAK;IAC9B,QAAQ,CAAS;IAC1B,4DAA4D;IACnD,MAAM,CAAU;IACzB,YAAY,QAAgB,EAAE,OAAe,EAAE,MAAe;QAC5D,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,gBAAgB,CAAC;QAC7B,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC;QACzB,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;IACvB,CAAC;CACF;AAUD,wGAAwG;AACxG,MAAM,CAAC,KAAK,UAAU,IAAI,CAAC,QAAgB,EAAE,IAAiB,EAAE,GAAW,EAAE,OAA+B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import type { Generator } from "../types.ts";
|
|
2
|
+
import { type HttpOptions } from "./http.ts";
|
|
3
|
+
export interface OpenAIOptions extends HttpOptions {
|
|
4
|
+
/** Your API key. Leave it out only when a proxy at `baseURL` adds it. */
|
|
5
|
+
apiKey?: string;
|
|
6
|
+
/** The model id. There is no default. */
|
|
7
|
+
model: string;
|
|
8
|
+
/** `chat` for Chat Completions (the default), `responses` for the Responses API. */
|
|
9
|
+
api?: "chat" | "responses";
|
|
10
|
+
/** Default `https://api.openai.com/v1`. */
|
|
11
|
+
baseURL?: string;
|
|
12
|
+
maxTokens?: number;
|
|
13
|
+
temperature?: number;
|
|
14
|
+
}
|
|
15
|
+
export interface LocalOptions extends HttpOptions {
|
|
16
|
+
/** The model name the server knows, e.g. `qwen3:8b` in Ollama. */
|
|
17
|
+
model: string;
|
|
18
|
+
/**
|
|
19
|
+
* The server's OpenAI-compatible base URL. Default `http://localhost:11434/v1` (Ollama).
|
|
20
|
+
* llama.cpp's server is usually `http://localhost:8080/v1`, LM Studio's `http://localhost:1234/v1`.
|
|
21
|
+
*/
|
|
22
|
+
baseURL?: string;
|
|
23
|
+
/** Only if your server wants one. */
|
|
24
|
+
apiKey?: string;
|
|
25
|
+
maxTokens?: number;
|
|
26
|
+
temperature?: number;
|
|
27
|
+
}
|
|
28
|
+
/** OpenAI, through Chat Completions or the Responses API, streaming. */
|
|
29
|
+
export declare function openai(options: OpenAIOptions): Generator;
|
|
30
|
+
/** A model on your own machine or server behind an OpenAI-compatible endpoint: Ollama, llama.cpp, LM Studio. */
|
|
31
|
+
export declare function local(options: LocalOptions): Generator;
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { GeneratorError, json, post, serverEvents, trimSlash } from "./http.js";
|
|
2
|
+
const auth = (apiKey) => (apiKey ? { authorization: `Bearer ${apiKey}` } : {});
|
|
3
|
+
/** Chat Completions, streaming: OpenAI and every server that copies its API. */
|
|
4
|
+
async function chat(provider, http, url, headers, extra, { system, messages, signal, onText }) {
|
|
5
|
+
const body = { ...extra, messages: [{ role: "system", content: system }, ...messages.map((m) => ({ role: m.role, content: m.content }))], stream: true };
|
|
6
|
+
const stream = await post(provider, http, url, headers, body, signal);
|
|
7
|
+
let text = "";
|
|
8
|
+
let usage;
|
|
9
|
+
for await (const e of serverEvents(stream)) {
|
|
10
|
+
if (e.data === "[DONE]")
|
|
11
|
+
break;
|
|
12
|
+
const d = json(provider, e.data);
|
|
13
|
+
if (d.error)
|
|
14
|
+
throw new GeneratorError(provider, `${provider} stopped: ${d.error.message ?? "error"}`);
|
|
15
|
+
const piece = d.choices?.[0]?.delta?.content;
|
|
16
|
+
if (typeof piece === "string" && piece) {
|
|
17
|
+
text += piece;
|
|
18
|
+
onText?.(piece);
|
|
19
|
+
}
|
|
20
|
+
if (d.usage)
|
|
21
|
+
usage = { inputTokens: d.usage.prompt_tokens ?? 0, outputTokens: d.usage.completion_tokens ?? 0 };
|
|
22
|
+
}
|
|
23
|
+
return { text, usage };
|
|
24
|
+
}
|
|
25
|
+
/** The Responses API, streaming. Responses aren't stored on OpenAI's side (`store: false`). */
|
|
26
|
+
async function responses(http, url, headers, extra, { system, messages, signal, onText }) {
|
|
27
|
+
const body = { ...extra, instructions: system, input: messages.map((m) => ({ role: m.role, content: m.content })), stream: true, store: false };
|
|
28
|
+
const stream = await post("openai", http, url, headers, body, signal);
|
|
29
|
+
let text = "";
|
|
30
|
+
let usage;
|
|
31
|
+
for await (const e of serverEvents(stream)) {
|
|
32
|
+
const d = json("openai", e.data);
|
|
33
|
+
const type = d.type ?? e.event;
|
|
34
|
+
if (type === "response.output_text.delta" && typeof d.delta === "string") {
|
|
35
|
+
text += d.delta;
|
|
36
|
+
onText?.(d.delta);
|
|
37
|
+
}
|
|
38
|
+
else if (type === "response.completed" || type === "response.incomplete") {
|
|
39
|
+
const u = d.response?.usage;
|
|
40
|
+
if (u)
|
|
41
|
+
usage = { inputTokens: u.input_tokens ?? 0, outputTokens: u.output_tokens ?? 0 };
|
|
42
|
+
}
|
|
43
|
+
else if (type === "response.failed") {
|
|
44
|
+
throw new GeneratorError("openai", `openai stopped: ${d.response?.error?.message ?? "the response failed"}`);
|
|
45
|
+
}
|
|
46
|
+
else if (type === "error") {
|
|
47
|
+
throw new GeneratorError("openai", `openai stopped: ${d.message ?? d.error?.message ?? "error"}`);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
return { text, usage };
|
|
51
|
+
}
|
|
52
|
+
/** OpenAI, through Chat Completions or the Responses API, streaming. */
|
|
53
|
+
export function openai(options) {
|
|
54
|
+
const base = trimSlash(options.baseURL ?? "https://api.openai.com/v1");
|
|
55
|
+
const api = options.api ?? "chat";
|
|
56
|
+
return {
|
|
57
|
+
name: "openai",
|
|
58
|
+
generate(request) {
|
|
59
|
+
const extra = { model: options.model };
|
|
60
|
+
if (options.temperature !== undefined)
|
|
61
|
+
extra.temperature = options.temperature;
|
|
62
|
+
if (api === "responses") {
|
|
63
|
+
if (options.maxTokens !== undefined)
|
|
64
|
+
extra.max_output_tokens = options.maxTokens;
|
|
65
|
+
return responses(options, `${base}/responses`, auth(options.apiKey), extra, request);
|
|
66
|
+
}
|
|
67
|
+
if (options.maxTokens !== undefined)
|
|
68
|
+
extra.max_completion_tokens = options.maxTokens;
|
|
69
|
+
extra.stream_options = { include_usage: true };
|
|
70
|
+
return chat("openai", options, `${base}/chat/completions`, auth(options.apiKey), extra, request);
|
|
71
|
+
},
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
/** A model on your own machine or server behind an OpenAI-compatible endpoint: Ollama, llama.cpp, LM Studio. */
|
|
75
|
+
export function local(options) {
|
|
76
|
+
const base = trimSlash(options.baseURL ?? "http://localhost:11434/v1");
|
|
77
|
+
return {
|
|
78
|
+
name: "local",
|
|
79
|
+
generate(request) {
|
|
80
|
+
const extra = { model: options.model };
|
|
81
|
+
if (options.temperature !== undefined)
|
|
82
|
+
extra.temperature = options.temperature;
|
|
83
|
+
if (options.maxTokens !== undefined)
|
|
84
|
+
extra.max_tokens = options.maxTokens;
|
|
85
|
+
return chat("local", options, `${base}/chat/completions`, auth(options.apiKey), extra, request);
|
|
86
|
+
},
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
//# sourceMappingURL=openai.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"openai.js","sourceRoot":"","sources":["../../src/adapters/openai.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,cAAc,EAAE,IAAI,EAAE,IAAI,EAAE,YAAY,EAAE,SAAS,EAAoB,MAAM,WAAW,CAAC;AA6BlG,MAAM,IAAI,GAAG,CAAC,MAAe,EAA0B,EAAE,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,aAAa,EAAE,UAAU,MAAM,EAAE,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC;AAEhH,gFAAgF;AAChF,KAAK,UAAU,IAAI,CAAC,QAAgB,EAAE,IAAiB,EAAE,GAAW,EAAE,OAA+B,EAAE,KAA8B,EAAE,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,EAAE,MAAM,EAAmB;IAC1L,MAAM,IAAI,GAAG,EAAE,GAAG,KAAK,EAAE,QAAQ,EAAE,CAAC,EAAE,IAAI,EAAE,QAAQ,EAAE,OAAO,EAAE,MAAM,EAAE,EAAE,GAAG,QAAQ,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,EAAE,IAAI,EAAE,CAAC,CAAC,IAAI,EAAE,OAAO,EAAE,CAAC,CAAC,OAAO,EAAE,CAAC,CAAC,CAAC,EAAE,MAAM,EAAE,IAAI,EAAE,CAAC;IACzJ,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,QAAQ,EAAE,IAAI,EAAE,GAAG,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,CAAC,CAAC;IACtE,IAAI,IAAI,GAAG,EAAE,CAAC;IACd,IAAI,KAAwB,CAAC;IAC7B,IAAI,KAAK,EAAE,MAAM,CAA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,gHAAgH;AAChH,MAAM,UAAU,KAAK,CAAC,OAAqB;IACzC,MAAM,IAAI,GAAG,SAAS,CAAC,OAAO,CAAC,OAAO,IAAI,2BAA2B,CAAC,CAAC;IACvE,OAAO;QACL,IAAI,EAAE,OAAO;QACb,QAAQ,CAAC,OAAO;YACd,MAAM,KAAK,GAA4B,EAAE,KAAK,EAAE,OAAO,CAAC,KAAK,EAAE,CAAC;YAChE,IAAI,OAAO,CAAC,WAAW,KAAK,SAAS;gBAAE,KAAK,CAAC,WAAW,GAAG,OAAO,CAAC,WAAW,CAAC;YAC/E,IAAI,OAAO,CAAC,SAAS,KAAK,SAAS;gBAAE,KAAK,CAAC,UAAU,GAAG,OAAO,CAAC,SAAS,CAAC;YAC1E,OAAO,IAAI,CAAC,OAAO,EAAE,OAAO,EAAE,GAAG,IAAI,mBAAmB,EAAE,IAAI,CAAC,OAAO,CAAC,MAAM,CAAC,EAAE,KAAK,EAAE,OAAO,CAAC,CAAC;QAClG,CAAC;KACF,CAAC;AACJ,CAAC"}
|
package/dist/audit.d.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { AuditOptions, Direction, Finding, Report } from "./types.ts";
|
|
2
|
+
/**
|
|
3
|
+
* The document checks the runtime runs by default: the verifier's `staticAudit`, from its
|
|
4
|
+
* `@polyxd/verifier/static` entry, which needs no file system and no browser. That is the spec
|
|
5
|
+
* validator (with the Direction's `emphasisBudget`, and bindings checked against the data), the
|
|
6
|
+
* pattern the document declares, the capabilities on offer, the Design Direction's rules, and the
|
|
7
|
+
* verifier's agent-readiness checks. A binding that reads nothing is a warning unless `missingData`
|
|
8
|
+
* says otherwise; the runtime makes it an error when the ask carries data.
|
|
9
|
+
*/
|
|
10
|
+
export declare function checkDocument(doc: unknown, options?: AuditOptions): Finding[];
|
|
11
|
+
/**
|
|
12
|
+
* What only a generated document is held to: it is never the product's shell, uses only the
|
|
13
|
+
* components the host allows, and follows none of the patterns the Direction disallows.
|
|
14
|
+
*/
|
|
15
|
+
export declare function generatedChecks(doc: any, direction?: Direction, components?: string[]): Finding[];
|
|
16
|
+
/** Findings, with the same finding reported once. */
|
|
17
|
+
export declare function toReport(findings: Finding[]): Report;
|
package/dist/audit.js
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { staticAudit } from "@polyxd/verifier/static";
|
|
2
|
+
import { COMPONENTS } from "./catalog.generated.js";
|
|
3
|
+
/**
|
|
4
|
+
* The document checks the runtime runs by default: the verifier's `staticAudit`, from its
|
|
5
|
+
* `@polyxd/verifier/static` entry, which needs no file system and no browser. That is the spec
|
|
6
|
+
* validator (with the Direction's `emphasisBudget`, and bindings checked against the data), the
|
|
7
|
+
* pattern the document declares, the capabilities on offer, the Design Direction's rules, and the
|
|
8
|
+
* verifier's agent-readiness checks. A binding that reads nothing is a warning unless `missingData`
|
|
9
|
+
* says otherwise; the runtime makes it an error when the ask carries data.
|
|
10
|
+
*/
|
|
11
|
+
export function checkDocument(doc, options = {}) {
|
|
12
|
+
return staticAudit(doc, { ...options, missingData: options.missingData ?? "warning" });
|
|
13
|
+
}
|
|
14
|
+
const SHELL = new Set(COMPONENTS.filter((c) => c.shell).map((c) => c.name));
|
|
15
|
+
/**
|
|
16
|
+
* What only a generated document is held to: it is never the product's shell, uses only the
|
|
17
|
+
* components the host allows, and follows none of the patterns the Direction disallows.
|
|
18
|
+
*/
|
|
19
|
+
export function generatedChecks(doc, direction, components) {
|
|
20
|
+
const out = [];
|
|
21
|
+
if (doc?.surface?.kind === "shell")
|
|
22
|
+
out.push({ severity: "error", check: "generated:shell", message: "surface.kind is \"shell\": the product's shell is authored, and a generated surface renders inside it" });
|
|
23
|
+
const allowed = components ? new Set(components) : undefined;
|
|
24
|
+
for (const c of Array.isArray(doc?.components) ? doc.components : []) {
|
|
25
|
+
if (SHELL.has(c?.component))
|
|
26
|
+
continue; // the validator and the shell check above already report these
|
|
27
|
+
if (allowed && typeof c?.component === "string" && !allowed.has(c.component))
|
|
28
|
+
out.push({ severity: "error", check: "generated:component", message: `${c.id}: ${c.component} is not one of the components this product allows` });
|
|
29
|
+
}
|
|
30
|
+
const pattern = doc?.surface?.pattern;
|
|
31
|
+
if (pattern && direction?.patterns?.disallow?.includes(pattern))
|
|
32
|
+
out.push({ severity: "error", check: "direction:pattern-disallowed", message: `surface.pattern "${pattern}" is a pattern the design direction disallows` });
|
|
33
|
+
return out;
|
|
34
|
+
}
|
|
35
|
+
/** Findings, with the same finding reported once. */
|
|
36
|
+
export function toReport(findings) {
|
|
37
|
+
const seen = new Set();
|
|
38
|
+
const unique = findings.filter((f) => {
|
|
39
|
+
const k = `${f.severity}\u0000${f.check}\u0000${f.message}`;
|
|
40
|
+
if (seen.has(k))
|
|
41
|
+
return false;
|
|
42
|
+
seen.add(k);
|
|
43
|
+
return true;
|
|
44
|
+
});
|
|
45
|
+
const errors = unique.filter((f) => f.severity === "error").length;
|
|
46
|
+
return { valid: errors === 0, errors, warnings: unique.length - errors, findings: unique };
|
|
47
|
+
}
|
|
48
|
+
//# sourceMappingURL=audit.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"audit.js","sourceRoot":"","sources":["../src/audit.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,yBAAyB,CAAC;AACtD,OAAO,EAAE,UAAU,EAAE,MAAM,wBAAwB,CAAC;AAGpD;;;;;;;GAOG;AACH,MAAM,UAAU,aAAa,CAAC,GAAY,EAAE,OAAO,GAAiB,EAAE;IACpE,OAAO,WAAW,CAAC,GAAG,EAAE,EAAE,GAAG,OAAO,EAAE,WAAW,EAAE,OAAO,CAAC,WAAW,IAAI,SAAS,EAAE,CAAC,CAAC;AACzF,CAAC;AAED,MAAM,KAAK,GAAG,IAAI,GAAG,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CA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