@lilbox/contracts 0.0.0-stage → 0.0.2

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.
@@ -0,0 +1,60 @@
1
+ type JsonSchema = {
2
+ properties?: Record<string, unknown>;
3
+ required?: string[];
4
+ [k: string]: unknown;
5
+ };
6
+ export declare function buildOpenApi(version: string): {
7
+ openapi: string;
8
+ info: {
9
+ title: string;
10
+ version: string;
11
+ description: string;
12
+ };
13
+ servers: {
14
+ url: string;
15
+ }[];
16
+ paths: Record<string, Record<string, unknown>>;
17
+ components: {
18
+ securitySchemes: {
19
+ bearer: {
20
+ type: string;
21
+ scheme: string;
22
+ description: string;
23
+ };
24
+ session: {
25
+ type: string;
26
+ in: string;
27
+ name: string;
28
+ description: string;
29
+ };
30
+ };
31
+ schemas: Record<string, unknown>;
32
+ responses: {
33
+ Error: {
34
+ description: string;
35
+ content: {
36
+ "application/json": {
37
+ schema: JsonSchema;
38
+ };
39
+ };
40
+ };
41
+ };
42
+ };
43
+ };
44
+ /** The MCP tool list: name, description, JSON Schemas and hints, as the MCP server registers them. */
45
+ /** Only audience "agent": dashboard-only operations are never MCP tools. */
46
+ export declare function mcpTools(): {
47
+ name: string;
48
+ title: string;
49
+ description: string;
50
+ inputSchema: JsonSchema;
51
+ outputSchema: JsonSchema;
52
+ annotations: {
53
+ title: string;
54
+ readOnlyHint: boolean;
55
+ destructiveHint: boolean;
56
+ idempotentHint: boolean;
57
+ openWorldHint: boolean;
58
+ };
59
+ }[];
60
+ export {};
@@ -0,0 +1,156 @@
1
+ // Builds the OpenAPI 3.1 document from the registry. OpenAPI 3.1 uses JSON Schema 2020-12, which is what
2
+ // zod's toJSONSchema emits, so schemas go in unchanged.
3
+ //
4
+ // Mapping (see operation.ts): {param} path segments come from input fields of the same name; for GET and
5
+ // DELETE the other fields become query parameters, otherwise the JSON request body.
6
+ import { z } from "zod";
7
+ import { ErrorResponse } from "./errors.js";
8
+ import { pathParams } from "./define.js";
9
+ import { operationList } from "./registry.js";
10
+ const toSchema = (s, io) => z.toJSONSchema(s, { io, target: "draft-2020-12", unrepresentable: "any" });
11
+ /** Strips $schema so the fragment can be embedded. */
12
+ const embed = ({ $schema: _drop, ...rest }) => rest;
13
+ /**
14
+ * Schemas with a meta id (App, Operation…) come out as "$ref": "#/$defs/App" plus a local $defs. Inside
15
+ * OpenAPI, "#/" means the document root, so the defs move to components/schemas and the refs are rewritten.
16
+ */
17
+ function hoist(schema, components) {
18
+ const { $defs, ...rest } = embed(schema);
19
+ const rewrite = (v) => {
20
+ if (Array.isArray(v))
21
+ return v.map(rewrite);
22
+ if (v && typeof v === "object") {
23
+ return Object.fromEntries(Object.entries(v).map(([k, x]) => k === "$ref" && typeof x === "string" ? [k, x.replace("#/$defs/", "#/components/schemas/")] : [k, rewrite(x)]));
24
+ }
25
+ return v;
26
+ };
27
+ for (const [name, def] of Object.entries($defs ?? {}))
28
+ components[name] ??= rewrite(def);
29
+ return rewrite(rest);
30
+ }
31
+ /** Replaces every local "#/$defs/X" ref with the definition itself, for consumers that don't follow $ref. */
32
+ function inline(schema) {
33
+ const { $defs = {}, ...rest } = embed(schema);
34
+ const walk = (v, seen) => {
35
+ if (Array.isArray(v))
36
+ return v.map((x) => walk(x, seen));
37
+ if (v && typeof v === "object") {
38
+ const ref = v.$ref;
39
+ if (typeof ref === "string" && ref.startsWith("#/$defs/")) {
40
+ const name = ref.slice("#/$defs/".length);
41
+ if (seen.includes(name))
42
+ throw new Error(`recursive schema ${name} can't be inlined`);
43
+ const { $ref: _r, ...siblings } = v;
44
+ return { ...walk($defs[name], [...seen, name]), ...siblings };
45
+ }
46
+ return Object.fromEntries(Object.entries(v).map(([k, x]) => [k, walk(x, seen)]));
47
+ }
48
+ return v;
49
+ };
50
+ return walk(rest, []);
51
+ }
52
+ const ERROR_STATUS = ["400", "401", "403", "404", "409", "422", "429", "500"];
53
+ function operationObject(op, components) {
54
+ const input = hoist(toSchema(op.input, "input"), components);
55
+ const props = input.properties ?? {};
56
+ const required = new Set(input.required ?? []);
57
+ const inPath = pathParams(op);
58
+ const bodyless = op.http.method === "GET" || op.http.method === "DELETE";
59
+ const parameters = [];
60
+ const bodyProps = {};
61
+ for (const [name, schema] of Object.entries(props)) {
62
+ if (inPath.includes(name))
63
+ parameters.push({ name, in: "path", required: true, schema });
64
+ else if (bodyless)
65
+ parameters.push({ name, in: "query", required: required.has(name), schema });
66
+ else
67
+ bodyProps[name] = schema;
68
+ }
69
+ if (op.mutating) {
70
+ parameters.push({
71
+ name: "Idempotency-Key",
72
+ in: "header",
73
+ required: false,
74
+ schema: { type: "string", maxLength: 255 },
75
+ description: "Retries with the same key return the first result instead of repeating the change.",
76
+ });
77
+ }
78
+ const bodyRequired = [...required].filter((n) => n in bodyProps);
79
+ const requestBody = !bodyless && Object.keys(bodyProps).length > 0
80
+ ? {
81
+ required: bodyRequired.length > 0,
82
+ content: {
83
+ "application/json": {
84
+ schema: { type: "object", properties: bodyProps, ...(bodyRequired.length ? { required: bodyRequired } : {}), additionalProperties: false },
85
+ },
86
+ },
87
+ }
88
+ : undefined;
89
+ const errors = Object.fromEntries(ERROR_STATUS.map((s) => [s, { $ref: "#/components/responses/Error" }]));
90
+ return {
91
+ operationId: op.name,
92
+ summary: op.title,
93
+ description: op.description,
94
+ tags: [op.http.path.split("/")[2] ?? "misc"],
95
+ "x-lilbox-scopes": op.scopes,
96
+ "x-lilbox-phase": op.phase,
97
+ "x-lilbox-audience": op.audience,
98
+ security: op.audience === "human" ? [{ session: [] }] : [{ bearer: op.scopes }, { session: [] }],
99
+ ...(parameters.length ? { parameters } : {}),
100
+ ...(requestBody ? { requestBody } : {}),
101
+ responses: {
102
+ "200": { description: "OK", content: { "application/json": { schema: hoist(toSchema(op.output, "output"), components) } } },
103
+ ...errors,
104
+ },
105
+ };
106
+ }
107
+ export function buildOpenApi(version) {
108
+ const paths = {};
109
+ const schemas = {};
110
+ for (const op of operationList) {
111
+ const item = (paths[op.http.path] ??= {});
112
+ item[op.http.method.toLowerCase()] = operationObject(op, schemas);
113
+ }
114
+ const errorSchema = hoist(toSchema(ErrorResponse, "output"), schemas);
115
+ return {
116
+ openapi: "3.1.0",
117
+ info: {
118
+ title: "lilbox API",
119
+ version,
120
+ description: "The lilbox REST API. Every operation is also an MCP tool with the same name at https://mcp.lilbox.ai.",
121
+ },
122
+ servers: [{ url: "https://api.lilbox.ai" }],
123
+ paths,
124
+ components: {
125
+ securitySchemes: {
126
+ bearer: { type: "http", scheme: "bearer", description: "OAuth access token or an lbx_ API key." },
127
+ session: { type: "apiKey", in: "cookie", name: "lilbox_session", description: "The dashboard's session. Accepted everywhere; dashboard-only operations accept nothing else." },
128
+ },
129
+ schemas,
130
+ responses: {
131
+ Error: {
132
+ description: "Error. `error.code` says what happened; see docs_url.",
133
+ content: { "application/json": { schema: errorSchema } },
134
+ },
135
+ },
136
+ },
137
+ };
138
+ }
139
+ /** The MCP tool list: name, description, JSON Schemas and hints, as the MCP server registers them. */
140
+ /** Only audience "agent": dashboard-only operations are never MCP tools. */
141
+ export function mcpTools() {
142
+ return operationList.filter((op) => op.audience === "agent").map((op) => ({
143
+ name: op.name,
144
+ title: op.title,
145
+ description: op.description,
146
+ inputSchema: inline(toSchema(op.input, "input")),
147
+ outputSchema: inline(toSchema(op.output, "output")),
148
+ annotations: {
149
+ title: op.title,
150
+ readOnlyHint: op.hints?.readOnly ?? false,
151
+ destructiveHint: op.hints?.destructive ?? false,
152
+ idempotentHint: op.hints?.idempotent ?? op.hints?.readOnly ?? false,
153
+ openWorldHint: false,
154
+ },
155
+ }));
156
+ }