@crasi/bisque-core 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 +23 -2
- package/dist/index.js +163 -0
- package/dist/index.js.map +7 -0
- package/dist/types/buckets.d.ts +10 -0
- package/dist/types/constants.d.ts +41 -0
- package/dist/types/errors.d.ts +21 -0
- package/dist/types/hash.d.ts +8 -0
- package/dist/types/index.d.ts +6 -0
- package/dist/types/token.d.ts +28 -0
- package/dist/types/types.d.ts +32 -0
- package/package.json +40 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Crasi
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,24 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @crasi/bisque-core
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Constants, shared types, errors and session tokens.
|
|
4
|
+
|
|
5
|
+
Part of [Bisque](https://github.com/gogeorge/bisque): contract-bounded, end-user generative customization for embedded web apps. A host declares a contract; its users ask for views and tools in plain language; a propose-only conductor returns a declarative spec that runs on the user's data in the browser. The model never sees a record.
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm i @crasi/bisque-core
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
```ts
|
|
12
|
+
import { signToken, verifyToken, PRODUCT, pkg } from "@crasi/bisque-core";
|
|
13
|
+
|
|
14
|
+
const token = await signToken({ app: "acme", tenant: "t1", sub: "u1", role: "member" }, process.env.BISQUE_SECRET_KEY!);
|
|
15
|
+
const claims = await verifyToken(token, process.env.BISQUE_SECRET_KEY!);
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
HS256 session tokens, the error classes every package throws, count buckets, hashing, and the one place the product's
|
|
19
|
+
name, package scope, CLI binary, env prefix and domains are spelled (`PRODUCT`, `ENV`, `pkg`).
|
|
20
|
+
|
|
21
|
+
Every Bisque package shares one version; see the [changelog](https://github.com/gogeorge/bisque/blob/main/CHANGELOG.md). Documentation lives in the
|
|
22
|
+
[repository](https://github.com/gogeorge/bisque#readme).
|
|
23
|
+
|
|
24
|
+
MIT © Crasi
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
// src/constants.ts
|
|
2
|
+
var PRODUCT = {
|
|
3
|
+
name: "Bisque",
|
|
4
|
+
slug: "bisque",
|
|
5
|
+
scope: "@crasi",
|
|
6
|
+
/** Published names are the scope, this prefix and the short name: `@crasi/bisque-react`. */
|
|
7
|
+
pkgPrefix: "bisque-",
|
|
8
|
+
cli: "bisque",
|
|
9
|
+
envPrefix: "BISQUE_",
|
|
10
|
+
cloudDomain: "bisque.example",
|
|
11
|
+
/** Each host gets `<host>.run.bisque.example` as its sandbox runtime origin (L3, after v1). */
|
|
12
|
+
runtimeDomainSuffix: "run.bisque.example",
|
|
13
|
+
conductorUrl: "https://api.bisque.example",
|
|
14
|
+
configFile: "bisque.config.ts",
|
|
15
|
+
reviewFile: "bisque.review.json",
|
|
16
|
+
lockFile: ".bisque/contract.lock.json",
|
|
17
|
+
artifactsTable: "bisque_artifacts",
|
|
18
|
+
usageTable: "bisque_usage"
|
|
19
|
+
};
|
|
20
|
+
function pkg(path) {
|
|
21
|
+
return `${PRODUCT.scope}/${PRODUCT.pkgPrefix}${path}`;
|
|
22
|
+
}
|
|
23
|
+
var ENV = {
|
|
24
|
+
publishableKey: `${PRODUCT.envPrefix}PUBLISHABLE_KEY`,
|
|
25
|
+
secretKey: `${PRODUCT.envPrefix}SECRET_KEY`,
|
|
26
|
+
conductorUrl: `${PRODUCT.envPrefix}CONDUCTOR_URL`,
|
|
27
|
+
model: `${PRODUCT.envPrefix}MODEL`
|
|
28
|
+
};
|
|
29
|
+
var ARTIFACT_FORMAT_VERSION = 1;
|
|
30
|
+
var MAX_ROWS = 5e3;
|
|
31
|
+
function runtimeOrigin(hostSlug) {
|
|
32
|
+
return `https://${hostSlug}.${PRODUCT.runtimeDomainSuffix}`;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// src/errors.ts
|
|
36
|
+
var BisqueError = class extends Error {
|
|
37
|
+
problems;
|
|
38
|
+
constructor(message, problems = []) {
|
|
39
|
+
super(problems.length ? `${message}: ${problems.map((p) => `${p.path || "(root)"}: ${p.message}`).join("; ")}` : message);
|
|
40
|
+
this.name = "BisqueError";
|
|
41
|
+
this.problems = problems;
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
function problem(path, code, message) {
|
|
45
|
+
return { path, code, message };
|
|
46
|
+
}
|
|
47
|
+
function nest(prefix, problems) {
|
|
48
|
+
return problems.map((p) => ({ ...p, path: p.path ? `${prefix}.${p.path}` : prefix }));
|
|
49
|
+
}
|
|
50
|
+
function formatProblems(problems) {
|
|
51
|
+
return problems.map((p) => `- ${p.path || "(root)"}: ${p.message}`).join("\n");
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// src/token.ts
|
|
55
|
+
var enc = new TextEncoder();
|
|
56
|
+
function b64url(bytes) {
|
|
57
|
+
let s = "";
|
|
58
|
+
for (const b of bytes) s += String.fromCharCode(b);
|
|
59
|
+
return btoa(s).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
|
|
60
|
+
}
|
|
61
|
+
function fromB64url(s) {
|
|
62
|
+
const pad = s.length % 4 ? "=".repeat(4 - s.length % 4) : "";
|
|
63
|
+
const bin = atob(s.replace(/-/g, "+").replace(/_/g, "/") + pad);
|
|
64
|
+
return Uint8Array.from(bin, (c) => c.charCodeAt(0));
|
|
65
|
+
}
|
|
66
|
+
async function hmac(secret, data) {
|
|
67
|
+
const key = await crypto.subtle.importKey("raw", enc.encode(secret), { name: "HMAC", hash: "SHA-256" }, false, ["sign"]);
|
|
68
|
+
return new Uint8Array(await crypto.subtle.sign("HMAC", key, enc.encode(data)));
|
|
69
|
+
}
|
|
70
|
+
async function signToken(claims, secret, ttlSeconds = 900, now = Date.now()) {
|
|
71
|
+
const iat = Math.floor(now / 1e3);
|
|
72
|
+
const body = { ...claims, iat, exp: iat + ttlSeconds };
|
|
73
|
+
const head = b64url(enc.encode(JSON.stringify({ alg: "HS256", typ: "JWT" })));
|
|
74
|
+
const payload = b64url(enc.encode(JSON.stringify(body)));
|
|
75
|
+
const sig = b64url(await hmac(secret, `${head}.${payload}`));
|
|
76
|
+
return `${head}.${payload}.${sig}`;
|
|
77
|
+
}
|
|
78
|
+
async function verifyToken(token, secret, now = Date.now()) {
|
|
79
|
+
const parts = token.split(".");
|
|
80
|
+
if (parts.length !== 3) return { ok: false, error: "malformed" };
|
|
81
|
+
const [head, payload, sig] = parts;
|
|
82
|
+
const expected = b64url(await hmac(secret, `${head}.${payload}`));
|
|
83
|
+
if (!constantTimeEqual(expected, sig)) return { ok: false, error: "signature" };
|
|
84
|
+
let claims;
|
|
85
|
+
try {
|
|
86
|
+
claims = JSON.parse(new TextDecoder().decode(fromB64url(payload)));
|
|
87
|
+
} catch {
|
|
88
|
+
return { ok: false, error: "malformed" };
|
|
89
|
+
}
|
|
90
|
+
if (typeof claims.exp !== "number" || claims.exp * 1e3 < now) return { ok: false, error: "expired" };
|
|
91
|
+
return { ok: true, claims };
|
|
92
|
+
}
|
|
93
|
+
function constantTimeEqual(a, b) {
|
|
94
|
+
if (a.length !== b.length) return false;
|
|
95
|
+
let diff = 0;
|
|
96
|
+
for (let i = 0; i < a.length; i++) diff |= a.charCodeAt(i) ^ b.charCodeAt(i);
|
|
97
|
+
return diff === 0;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
// src/hash.ts
|
|
101
|
+
function canonicalJson(value) {
|
|
102
|
+
return JSON.stringify(sortKeys(value));
|
|
103
|
+
}
|
|
104
|
+
function sortKeys(v) {
|
|
105
|
+
if (Array.isArray(v)) return v.map(sortKeys);
|
|
106
|
+
if (v && typeof v === "object") {
|
|
107
|
+
const out = {};
|
|
108
|
+
for (const k of Object.keys(v).sort()) {
|
|
109
|
+
const x = v[k];
|
|
110
|
+
if (x !== void 0) out[k] = sortKeys(x);
|
|
111
|
+
}
|
|
112
|
+
return out;
|
|
113
|
+
}
|
|
114
|
+
return v;
|
|
115
|
+
}
|
|
116
|
+
async function sha256Hex(text) {
|
|
117
|
+
const buf = await crypto.subtle.digest("SHA-256", new TextEncoder().encode(text));
|
|
118
|
+
return [...new Uint8Array(buf)].map((b) => b.toString(16).padStart(2, "0")).join("");
|
|
119
|
+
}
|
|
120
|
+
function fnv1a(text) {
|
|
121
|
+
let h = 2166136261;
|
|
122
|
+
for (let i = 0; i < text.length; i++) {
|
|
123
|
+
h ^= text.charCodeAt(i);
|
|
124
|
+
h = Math.imul(h, 16777619);
|
|
125
|
+
}
|
|
126
|
+
return (h >>> 0).toString(16).padStart(8, "0");
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
// src/buckets.ts
|
|
130
|
+
var COUNT_BUCKETS = ["0", "1", "2-12", "13-50", ">50"];
|
|
131
|
+
function bucketCount(n) {
|
|
132
|
+
if (n <= 0) return "0";
|
|
133
|
+
if (n === 1) return "1";
|
|
134
|
+
if (n <= 12) return "2-12";
|
|
135
|
+
if (n <= 50) return "13-50";
|
|
136
|
+
return ">50";
|
|
137
|
+
}
|
|
138
|
+
function bucketShare(share) {
|
|
139
|
+
if (!Number.isFinite(share)) return 0;
|
|
140
|
+
return Math.round(Math.min(1, Math.max(0, share)) * 10) / 10;
|
|
141
|
+
}
|
|
142
|
+
export {
|
|
143
|
+
ARTIFACT_FORMAT_VERSION,
|
|
144
|
+
BisqueError,
|
|
145
|
+
COUNT_BUCKETS,
|
|
146
|
+
ENV,
|
|
147
|
+
MAX_ROWS,
|
|
148
|
+
PRODUCT,
|
|
149
|
+
bucketCount,
|
|
150
|
+
bucketShare,
|
|
151
|
+
canonicalJson,
|
|
152
|
+
constantTimeEqual,
|
|
153
|
+
fnv1a,
|
|
154
|
+
formatProblems,
|
|
155
|
+
nest,
|
|
156
|
+
pkg,
|
|
157
|
+
problem,
|
|
158
|
+
runtimeOrigin,
|
|
159
|
+
sha256Hex,
|
|
160
|
+
signToken,
|
|
161
|
+
verifyToken
|
|
162
|
+
};
|
|
163
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../src/constants.ts", "../src/errors.ts", "../src/token.ts", "../src/hash.ts", "../src/buckets.ts"],
|
|
4
|
+
"sourcesContent": ["/**\n * Every place the product name, package scope, CLI binary or a domain appears\n * reads it from here (decision D5: \"Bisque\" is a working name pending\n * trademark clearance, so a rename must stay mechanical). Domains use the\n * reserved `.example` TLD until real ones are registered.\n */\n// Note for the rename: the unscoped npm name \"bisque\" belongs to an unrelated\n// package, so `npx bisque` without a local install runs someone else's code.\n// Docs and printed hints use `npx --no bisque`; a final CLI name should be\n// one nobody else holds on the registry.\nexport const PRODUCT = {\n name: \"Bisque\",\n slug: \"bisque\",\n scope: \"@crasi\",\n /** Published names are the scope, this prefix and the short name: `@crasi/bisque-react`. */\n pkgPrefix: \"bisque-\",\n cli: \"bisque\",\n envPrefix: \"BISQUE_\",\n cloudDomain: \"bisque.example\",\n /** Each host gets `<host>.run.bisque.example` as its sandbox runtime origin (L3, after v1). */\n runtimeDomainSuffix: \"run.bisque.example\",\n conductorUrl: \"https://api.bisque.example\",\n configFile: \"bisque.config.ts\",\n reviewFile: \"bisque.review.json\",\n lockFile: \".bisque/contract.lock.json\",\n artifactsTable: \"bisque_artifacts\",\n usageTable: \"bisque_usage\",\n} as const;\n\n/**\n * The published name of a package, or one of its subpaths:\n * `pkg(\"react\")` is `@crasi/bisque-react`, `pkg(\"next/server\")` is\n * `@crasi/bisque-next/server`. Nothing spells a package name any other way.\n */\nexport function pkg(path: string): string {\n return `${PRODUCT.scope}/${PRODUCT.pkgPrefix}${path}`;\n}\n\nexport const ENV = {\n publishableKey: `${PRODUCT.envPrefix}PUBLISHABLE_KEY`,\n secretKey: `${PRODUCT.envPrefix}SECRET_KEY`,\n conductorUrl: `${PRODUCT.envPrefix}CONDUCTOR_URL`,\n model: `${PRODUCT.envPrefix}MODEL`,\n} as const;\n\n/** Version of the artifact body format. Independent of SDK semver (\u00A77.6). */\nexport const ARTIFACT_FORMAT_VERSION = 1;\n\n/** Hard ceiling on rows any query may return, whatever a resource's cap says. */\nexport const MAX_ROWS = 5_000;\n\nexport function runtimeOrigin(hostSlug: string): string {\n return `https://${hostSlug}.${PRODUCT.runtimeDomainSuffix}`;\n}\n", "/**\n * A structured problem. Validators return lists of these rather than throwing,\n * because the conductor feeds them straight back to the model for its one\n * repair turn, and a model recovers from \"limit must be at most 400\" but not\n * from a stack trace.\n */\nexport type Problem = {\n /** Dotted path into the checked object, e.g. `body.data.join.0.to`. Empty for the whole object. */\n path: string;\n code: ProblemCode;\n message: string;\n};\n\nexport type ProblemCode =\n | \"schema\"\n | \"unknown_resource\"\n | \"unknown_field\"\n | \"undeclared_join\"\n | \"undeclared_read\"\n | \"unused_read\"\n | \"role_denied\"\n | \"limit\"\n | \"type\"\n | \"unknown_intent\"\n | \"undeclared_act\"\n | \"unknown_param\"\n | \"unknown_slot\"\n | \"slot_kind\"\n | \"context\"\n | \"encode\"\n | \"unsafe_text\"\n | \"deferred\"\n | \"scope\"\n | \"locale\";\n\nexport class BisqueError extends Error {\n readonly problems: Problem[];\n constructor(message: string, problems: Problem[] = []) {\n super(problems.length ? `${message}: ${problems.map((p) => `${p.path || \"(root)\"}: ${p.message}`).join(\"; \")}` : message);\n this.name = \"BisqueError\";\n this.problems = problems;\n }\n}\n\nexport function problem(path: string, code: ProblemCode, message: string): Problem {\n return { path, code, message };\n}\n\n/** Prefix every problem's path, for validators that delegate to a nested validator. */\nexport function nest(prefix: string, problems: Problem[]): Problem[] {\n return problems.map((p) => ({ ...p, path: p.path ? `${prefix}.${p.path}` : prefix }));\n}\n\nexport function formatProblems(problems: Problem[]): string {\n return problems.map((p) => `- ${p.path || \"(root)\"}: ${p.message}`).join(\"\\n\");\n}\n", "import type { Role } from \"./types\";\n\n/**\n * Session tokens are HS256 JWTs minted by the host's token route with\n * BISQUE_SECRET_KEY and verified by the conductor. They carry who is asking\n * and in which role; the conductor uses the role to validate proposals the\n * same way the host will. WebCrypto only, so this runs in Node, edge\n * runtimes and browsers alike.\n */\nexport type TokenClaims = {\n app: string;\n tenant: string;\n sub: string;\n role: Role;\n locale?: string;\n iat: number;\n exp: number;\n};\n\nconst enc = new TextEncoder();\n\nfunction b64url(bytes: Uint8Array): string {\n let s = \"\";\n for (const b of bytes) s += String.fromCharCode(b);\n return btoa(s).replace(/\\+/g, \"-\").replace(/\\//g, \"_\").replace(/=+$/, \"\");\n}\n\nfunction fromB64url(s: string): Uint8Array {\n const pad = s.length % 4 ? \"=\".repeat(4 - (s.length % 4)) : \"\";\n const bin = atob(s.replace(/-/g, \"+\").replace(/_/g, \"/\") + pad);\n return Uint8Array.from(bin, (c) => c.charCodeAt(0));\n}\n\nasync function hmac(secret: string, data: string): Promise<Uint8Array> {\n const key = await crypto.subtle.importKey(\"raw\", enc.encode(secret), { name: \"HMAC\", hash: \"SHA-256\" }, false, [\"sign\"]);\n return new Uint8Array(await crypto.subtle.sign(\"HMAC\", key, enc.encode(data)));\n}\n\nexport async function signToken(claims: Omit<TokenClaims, \"iat\" | \"exp\">, secret: string, ttlSeconds = 900, now = Date.now()): Promise<string> {\n const iat = Math.floor(now / 1000);\n const body: TokenClaims = { ...claims, iat, exp: iat + ttlSeconds };\n const head = b64url(enc.encode(JSON.stringify({ alg: \"HS256\", typ: \"JWT\" })));\n const payload = b64url(enc.encode(JSON.stringify(body)));\n const sig = b64url(await hmac(secret, `${head}.${payload}`));\n return `${head}.${payload}.${sig}`;\n}\n\nexport type VerifyResult = { ok: true; claims: TokenClaims } | { ok: false; error: \"malformed\" | \"signature\" | \"expired\" };\n\nexport async function verifyToken(token: string, secret: string, now = Date.now()): Promise<VerifyResult> {\n const parts = token.split(\".\");\n if (parts.length !== 3) return { ok: false, error: \"malformed\" };\n const [head, payload, sig] = parts;\n const expected = b64url(await hmac(secret, `${head}.${payload}`));\n if (!constantTimeEqual(expected, sig)) return { ok: false, error: \"signature\" };\n let claims: TokenClaims;\n try {\n claims = JSON.parse(new TextDecoder().decode(fromB64url(payload)));\n } catch {\n return { ok: false, error: \"malformed\" };\n }\n if (typeof claims.exp !== \"number\" || claims.exp * 1000 < now) return { ok: false, error: \"expired\" };\n return { ok: true, claims };\n}\n\n/** String comparison whose time does not depend on where the inputs differ. */\nexport function constantTimeEqual(a: string, b: string): boolean {\n if (a.length !== b.length) return false;\n let diff = 0;\n for (let i = 0; i < a.length; i++) diff |= a.charCodeAt(i) ^ b.charCodeAt(i);\n return diff === 0;\n}\n", "/** JSON with object keys sorted at every level, so equal values hash equally. */\nexport function canonicalJson(value: unknown): string {\n return JSON.stringify(sortKeys(value));\n}\n\nfunction sortKeys(v: unknown): unknown {\n if (Array.isArray(v)) return v.map(sortKeys);\n if (v && typeof v === \"object\") {\n const out: Record<string, unknown> = {};\n for (const k of Object.keys(v as object).sort()) {\n const x = (v as Record<string, unknown>)[k];\n if (x !== undefined) out[k] = sortKeys(x);\n }\n return out;\n }\n return v;\n}\n\nexport async function sha256Hex(text: string): Promise<string> {\n const buf = await crypto.subtle.digest(\"SHA-256\", new TextEncoder().encode(text));\n return [...new Uint8Array(buf)].map((b) => b.toString(16).padStart(2, \"0\")).join(\"\");\n}\n\n/**\n * Synchronous FNV-1a, used where an async digest is awkward (memo keys,\n * React render paths). Not for anything security-relevant.\n */\nexport function fnv1a(text: string): string {\n let h = 0x811c9dc5;\n for (let i = 0; i < text.length; i++) {\n h ^= text.charCodeAt(i);\n h = Math.imul(h, 0x01000193);\n }\n return (h >>> 0).toString(16).padStart(8, \"0\");\n}\n", "/**\n * Render diagnostics cross the custody boundary (\u00A73.4, P1 exception b), so\n * counts are coarsened before they leave the host: row and distinct counts\n * become one of five buckets, and null shares are rounded to 10%.\n */\nexport const COUNT_BUCKETS = [\"0\", \"1\", \"2-12\", \"13-50\", \">50\"] as const;\nexport type CountBucket = (typeof COUNT_BUCKETS)[number];\n\nexport function bucketCount(n: number): CountBucket {\n if (n <= 0) return \"0\";\n if (n === 1) return \"1\";\n if (n <= 12) return \"2-12\";\n if (n <= 50) return \"13-50\";\n return \">50\";\n}\n\n/** Share in [0, 1] rounded to the nearest 0.1. */\nexport function bucketShare(share: number): number {\n if (!Number.isFinite(share)) return 0;\n return Math.round(Math.min(1, Math.max(0, share)) * 10) / 10;\n}\n"],
|
|
5
|
+
"mappings": ";AAUO,IAAM,UAAU;AAAA,EACrB,MAAM;AAAA,EACN,MAAM;AAAA,EACN,OAAO;AAAA;AAAA,EA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IAAK,EAA8B,CAAC;AAC1C,UAAI,MAAM,OAAW,KAAI,CAAC,IAAI,SAAS,CAAC;AAAA,IAC1C;AACA,WAAO;AAAA,EACT;AACA,SAAO;AACT;AAEA,eAAsB,UAAU,MAA+B;AAC7D,QAAM,MAAM,MAAM,OAAO,OAAO,OAAO,WAAW,IAAI,YAAY,EAAE,OAAO,IAAI,CAAC;AAChF,SAAO,CAAC,GAAG,IAAI,WAAW,GAAG,CAAC,EAAE,IAAI,CAAC,MAAM,EAAE,SAAS,EAAE,EAAE,SAAS,GAAG,GAAG,CAAC,EAAE,KAAK,EAAE;AACrF;AAMO,SAAS,MAAM,MAAsB;AAC1C,MAAI,IAAI;AACR,WAAS,IAAI,GAAG,IAAI,KAAK,QAAQ,KAAK;AACpC,SAAK,KAAK,WAAW,CAAC;AACtB,QAAI,KAAK,KAAK,GAAG,QAAU;AAAA,EAC7B;AACA,UAAQ,MAAM,GAAG,SAAS,EAAE,EAAE,SAAS,GAAG,GAAG;AAC/C;;;AC7BO,IAAM,gBAAgB,CAAC,KAAK,KAAK,QAAQ,SAAS,KAAK;AAGvD,SAAS,YAAY,GAAwB;AAClD,MAAI,KAAK,EAAG,QAAO;AACnB,MAAI,MAAM,EAAG,QAAO;AACpB,MAAI,KAAK,GAAI,QAAO;AACpB,MAAI,KAAK,GAAI,QAAO;AACpB,SAAO;AACT;AAGO,SAAS,YAAY,OAAuB;AACjD,MAAI,CAAC,OAAO,SAAS,KAAK,EAAG,QAAO;AACpC,SAAO,KAAK,MAAM,KAAK,IAAI,GAAG,KAAK,IAAI,GAAG,KAAK,CAAC,IAAI,EAAE,IAAI;AAC5D;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Render diagnostics cross the custody boundary (§3.4, P1 exception b), so
|
|
3
|
+
* counts are coarsened before they leave the host: row and distinct counts
|
|
4
|
+
* become one of five buckets, and null shares are rounded to 10%.
|
|
5
|
+
*/
|
|
6
|
+
export declare const COUNT_BUCKETS: readonly ["0", "1", "2-12", "13-50", ">50"];
|
|
7
|
+
export type CountBucket = (typeof COUNT_BUCKETS)[number];
|
|
8
|
+
export declare function bucketCount(n: number): CountBucket;
|
|
9
|
+
/** Share in [0, 1] rounded to the nearest 0.1. */
|
|
10
|
+
export declare function bucketShare(share: number): number;
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Every place the product name, package scope, CLI binary or a domain appears
|
|
3
|
+
* reads it from here (decision D5: "Bisque" is a working name pending
|
|
4
|
+
* trademark clearance, so a rename must stay mechanical). Domains use the
|
|
5
|
+
* reserved `.example` TLD until real ones are registered.
|
|
6
|
+
*/
|
|
7
|
+
export declare const PRODUCT: {
|
|
8
|
+
readonly name: "Bisque";
|
|
9
|
+
readonly slug: "bisque";
|
|
10
|
+
readonly scope: "@crasi";
|
|
11
|
+
/** Published names are the scope, this prefix and the short name: `@crasi/bisque-react`. */
|
|
12
|
+
readonly pkgPrefix: "bisque-";
|
|
13
|
+
readonly cli: "bisque";
|
|
14
|
+
readonly envPrefix: "BISQUE_";
|
|
15
|
+
readonly cloudDomain: "bisque.example";
|
|
16
|
+
/** Each host gets `<host>.run.bisque.example` as its sandbox runtime origin (L3, after v1). */
|
|
17
|
+
readonly runtimeDomainSuffix: "run.bisque.example";
|
|
18
|
+
readonly conductorUrl: "https://api.bisque.example";
|
|
19
|
+
readonly configFile: "bisque.config.ts";
|
|
20
|
+
readonly reviewFile: "bisque.review.json";
|
|
21
|
+
readonly lockFile: ".bisque/contract.lock.json";
|
|
22
|
+
readonly artifactsTable: "bisque_artifacts";
|
|
23
|
+
readonly usageTable: "bisque_usage";
|
|
24
|
+
};
|
|
25
|
+
/**
|
|
26
|
+
* The published name of a package, or one of its subpaths:
|
|
27
|
+
* `pkg("react")` is `@crasi/bisque-react`, `pkg("next/server")` is
|
|
28
|
+
* `@crasi/bisque-next/server`. Nothing spells a package name any other way.
|
|
29
|
+
*/
|
|
30
|
+
export declare function pkg(path: string): string;
|
|
31
|
+
export declare const ENV: {
|
|
32
|
+
readonly publishableKey: "BISQUE_PUBLISHABLE_KEY";
|
|
33
|
+
readonly secretKey: "BISQUE_SECRET_KEY";
|
|
34
|
+
readonly conductorUrl: "BISQUE_CONDUCTOR_URL";
|
|
35
|
+
readonly model: "BISQUE_MODEL";
|
|
36
|
+
};
|
|
37
|
+
/** Version of the artifact body format. Independent of SDK semver (§7.6). */
|
|
38
|
+
export declare const ARTIFACT_FORMAT_VERSION = 1;
|
|
39
|
+
/** Hard ceiling on rows any query may return, whatever a resource's cap says. */
|
|
40
|
+
export declare const MAX_ROWS = 5000;
|
|
41
|
+
export declare function runtimeOrigin(hostSlug: string): string;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A structured problem. Validators return lists of these rather than throwing,
|
|
3
|
+
* because the conductor feeds them straight back to the model for its one
|
|
4
|
+
* repair turn, and a model recovers from "limit must be at most 400" but not
|
|
5
|
+
* from a stack trace.
|
|
6
|
+
*/
|
|
7
|
+
export type Problem = {
|
|
8
|
+
/** Dotted path into the checked object, e.g. `body.data.join.0.to`. Empty for the whole object. */
|
|
9
|
+
path: string;
|
|
10
|
+
code: ProblemCode;
|
|
11
|
+
message: string;
|
|
12
|
+
};
|
|
13
|
+
export type ProblemCode = "schema" | "unknown_resource" | "unknown_field" | "undeclared_join" | "undeclared_read" | "unused_read" | "role_denied" | "limit" | "type" | "unknown_intent" | "undeclared_act" | "unknown_param" | "unknown_slot" | "slot_kind" | "context" | "encode" | "unsafe_text" | "deferred" | "scope" | "locale";
|
|
14
|
+
export declare class BisqueError extends Error {
|
|
15
|
+
readonly problems: Problem[];
|
|
16
|
+
constructor(message: string, problems?: Problem[]);
|
|
17
|
+
}
|
|
18
|
+
export declare function problem(path: string, code: ProblemCode, message: string): Problem;
|
|
19
|
+
/** Prefix every problem's path, for validators that delegate to a nested validator. */
|
|
20
|
+
export declare function nest(prefix: string, problems: Problem[]): Problem[];
|
|
21
|
+
export declare function formatProblems(problems: Problem[]): string;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/** JSON with object keys sorted at every level, so equal values hash equally. */
|
|
2
|
+
export declare function canonicalJson(value: unknown): string;
|
|
3
|
+
export declare function sha256Hex(text: string): Promise<string>;
|
|
4
|
+
/**
|
|
5
|
+
* Synchronous FNV-1a, used where an async digest is awkward (memo keys,
|
|
6
|
+
* React render paths). Not for anything security-relevant.
|
|
7
|
+
*/
|
|
8
|
+
export declare function fnv1a(text: string): string;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { Role } from "./types.js";
|
|
2
|
+
/**
|
|
3
|
+
* Session tokens are HS256 JWTs minted by the host's token route with
|
|
4
|
+
* BISQUE_SECRET_KEY and verified by the conductor. They carry who is asking
|
|
5
|
+
* and in which role; the conductor uses the role to validate proposals the
|
|
6
|
+
* same way the host will. WebCrypto only, so this runs in Node, edge
|
|
7
|
+
* runtimes and browsers alike.
|
|
8
|
+
*/
|
|
9
|
+
export type TokenClaims = {
|
|
10
|
+
app: string;
|
|
11
|
+
tenant: string;
|
|
12
|
+
sub: string;
|
|
13
|
+
role: Role;
|
|
14
|
+
locale?: string;
|
|
15
|
+
iat: number;
|
|
16
|
+
exp: number;
|
|
17
|
+
};
|
|
18
|
+
export declare function signToken(claims: Omit<TokenClaims, "iat" | "exp">, secret: string, ttlSeconds?: number, now?: number): Promise<string>;
|
|
19
|
+
export type VerifyResult = {
|
|
20
|
+
ok: true;
|
|
21
|
+
claims: TokenClaims;
|
|
22
|
+
} | {
|
|
23
|
+
ok: false;
|
|
24
|
+
error: "malformed" | "signature" | "expired";
|
|
25
|
+
};
|
|
26
|
+
export declare function verifyToken(token: string, secret: string, now?: number): Promise<VerifyResult>;
|
|
27
|
+
/** String comparison whose time does not depend on where the inputs differ. */
|
|
28
|
+
export declare function constantTimeEqual(a: string, b: string): boolean;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
export type Role = string;
|
|
2
|
+
export type Locale = string;
|
|
3
|
+
/** What the identity adapter (ι) reports for the current session. */
|
|
4
|
+
export type Identity = {
|
|
5
|
+
tenant: string;
|
|
6
|
+
user: string;
|
|
7
|
+
role: Role;
|
|
8
|
+
locale: Locale;
|
|
9
|
+
/** Display name for attribution on shared artifacts. Never sent to the conductor. */
|
|
10
|
+
name?: string;
|
|
11
|
+
};
|
|
12
|
+
/** Localized text: one entry per host locale. The default locale is required. */
|
|
13
|
+
export type LocalizedText = Record<Locale, string>;
|
|
14
|
+
export type ArtifactLayer = "spec" | "patch" | "code";
|
|
15
|
+
export type Visibility = "private" | "workspace";
|
|
16
|
+
export type ArtifactStatus = "live" | "broken" | "disabled" | "archived";
|
|
17
|
+
/** Passed to every intent body: who acts, and which artifact asked (for the host's audit tag). */
|
|
18
|
+
export type IntentContext = {
|
|
19
|
+
identity: Identity;
|
|
20
|
+
artifactId: string | null;
|
|
21
|
+
};
|
|
22
|
+
export type IntentResult = {
|
|
23
|
+
id?: string;
|
|
24
|
+
undo?: () => Promise<void> | void;
|
|
25
|
+
[k: string]: unknown;
|
|
26
|
+
} | void;
|
|
27
|
+
/** A host write path, shared by the host's own UI and Bisque (Listing 7). */
|
|
28
|
+
export type IntentHandler<A = any> = (args: A, ctx: IntentContext) => Promise<IntentResult> | IntentResult;
|
|
29
|
+
export type NavigationHandler<A = any> = (args: A) => void;
|
|
30
|
+
export type Json = string | number | boolean | null | Json[] | {
|
|
31
|
+
[k: string]: Json;
|
|
32
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,43 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@crasi/bisque-core",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.3.0",
|
|
4
|
+
"description": "Constants, shared types, errors and session tokens",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"author": "Crasi",
|
|
7
|
+
"homepage": "https://github.com/gogeorge/bisque#readme",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "git+https://github.com/gogeorge/bisque.git",
|
|
11
|
+
"directory": "packages/core"
|
|
12
|
+
},
|
|
13
|
+
"bugs": "https://github.com/gogeorge/bisque/issues",
|
|
14
|
+
"keywords": [
|
|
15
|
+
"bisque",
|
|
16
|
+
"generative-ui",
|
|
17
|
+
"ai",
|
|
18
|
+
"llm",
|
|
19
|
+
"tokens",
|
|
20
|
+
"jwt"
|
|
21
|
+
],
|
|
22
|
+
"type": "module",
|
|
23
|
+
"engines": {
|
|
24
|
+
"node": ">=20"
|
|
25
|
+
},
|
|
26
|
+
"exports": {
|
|
27
|
+
".": {
|
|
28
|
+
"types": "./dist/types/index.d.ts",
|
|
29
|
+
"default": "./dist/index.js"
|
|
30
|
+
}
|
|
31
|
+
},
|
|
32
|
+
"files": [
|
|
33
|
+
"dist",
|
|
34
|
+
"README.md",
|
|
35
|
+
"LICENSE"
|
|
36
|
+
],
|
|
37
|
+
"publishConfig": {
|
|
38
|
+
"access": "public"
|
|
39
|
+
},
|
|
40
|
+
"scripts": {
|
|
41
|
+
"typecheck": "tsc -p ."
|
|
42
|
+
}
|
|
6
43
|
}
|