@n-dream/builders-sdk 0.3.1 → 1.0.0-next.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/README.md CHANGED
@@ -1,51 +1,18 @@
1
1
  # @n-dream/builders-sdk
2
2
 
3
- Platform helpers for apps on the Builders platform (builders.n-dream.com):
4
- the signed-in user, a database, AI, file storage and notifications.
5
-
6
- This package is for apps that the platform made. It does not work on other
7
- hosting. The platform sets the environment variables that it needs.
8
-
9
- ## Install
10
-
11
- Apps import the package as `@builders/sdk`. Use an npm alias in
12
- `package.json`:
13
-
14
- ```json
15
- "dependencies": {
16
- "@builders/sdk": "npm:@n-dream/builders-sdk@^0.3.0"
17
- }
18
- ```
19
-
20
- Node.js 22 or later is necessary. `@builders/sdk/next` needs Next.js 15 or
21
- later.
22
-
23
- ## Use
3
+ The SDK for apps on Builders (design 4.5). Apps import it from npm.
24
4
 
25
5
  ```ts
26
- import { currentUser } from "@builders/sdk/next"; // server code only
27
- import { db, ai, storage, notify, secrets } from "@builders/sdk";
28
- import { z } from "zod";
6
+ import { appInfo } from "@n-dream/builders-sdk";
29
7
 
30
- const user = await currentUser(); // { email, name }
31
- const notes = db.collection<{ text: string; author: string }>("notes");
32
- await notes.add({ text: "Hello", author: user.email });
33
- await notes.where("author", "==", user.email).limit(20).list();
34
-
35
- await ai.ask("Summarise: ...", { model: "fast" }); // AI add-on
36
- await ai.json("Sort: ...", { schema: z.object({ tag: z.string() }) }); // a checked object
37
- await ai.image("A robot mascot"); // also ai.chat, ai.stream, ai.speak, ai.transcribe
38
- await storage.upload("files/a.pdf", bytes); // storage add-on
39
- await notify.slack("Done"); // notify add-on
40
- await secrets.get("HUBSPOT_API_KEY"); // set in the portal
8
+ const info = await appInfo(); // { appId, slug, environment, version }
41
9
  ```
42
10
 
43
- - `db` uses Firestore on the platform. In `next dev` and in tests, it uses
44
- a temporary database in memory. In the Builders dev preview, `ai` and
45
- `notify` work through the platform (notifications go only to you).
46
- - `ai`, `storage` and `notify` are add-ons. If an add-on is off, the call
47
- throws `AddOnNotEnabledError`.
48
-
49
- ## License
11
+ - `appInfo()` reads `/_api/app.json` on the app's own host. `environment`
12
+ is `live`, `preview` or `version`. `version` is null on the preview.
13
+ - There is no local mode (D37). Without the platform, `appInfo()` throws a
14
+ `BuildersError`. Use it only for things that the page can do without.
15
+ - Later platform services add one named export each (design section 12).
50
16
 
51
- UNLICENSED. Only for apps on the Builders platform.
17
+ Release: the owner publishes with the `next` dist-tag until the
18
+ switch-over (Q29). `npm pack` builds `dist/` first.
@@ -0,0 +1,11 @@
1
+ export declare const APP_INFO_PATH = "/_api/app.json";
2
+ export type Environment = "live" | "preview" | "version";
3
+ export interface AppInfo {
4
+ appId: string;
5
+ slug: string;
6
+ environment: Environment;
7
+ version: number | null;
8
+ }
9
+ export declare function appInfo(opts?: {
10
+ fetch?: typeof fetch;
11
+ }): Promise<AppInfo>;
@@ -0,0 +1,44 @@
1
+ import { BuildersError } from "./errors.js";
2
+ // The path of the app information on every app host (design 3.2, D42).
3
+ export const APP_INFO_PATH = "/_api/app.json";
4
+ const ENVIRONMENTS = ["live", "preview", "version"];
5
+ function parse(v) {
6
+ if (typeof v !== "object" || v === null)
7
+ return null;
8
+ const o = v;
9
+ if (typeof o.appId !== "string" || !o.appId)
10
+ return null;
11
+ if (typeof o.slug !== "string" || !o.slug)
12
+ return null;
13
+ if (typeof o.environment !== "string" || !ENVIRONMENTS.includes(o.environment))
14
+ return null;
15
+ const version = o.version ?? null;
16
+ if (version !== null && (typeof version !== "number" || !Number.isInteger(version) || version < 1))
17
+ return null;
18
+ return { appId: o.appId, slug: o.slug, environment: o.environment, version };
19
+ }
20
+ // Reads the app information from the platform. There is no local mode
21
+ // (D37): on a local server without the platform, this throws.
22
+ export async function appInfo(opts = {}) {
23
+ const f = opts.fetch ?? globalThis.fetch;
24
+ let res;
25
+ try {
26
+ res = await f(APP_INFO_PATH, { credentials: "same-origin", cache: "no-store", headers: { accept: "application/json" } });
27
+ }
28
+ catch {
29
+ throw new BuildersError("Could not reach Builders to read the app information.", 0);
30
+ }
31
+ if (!res.ok)
32
+ throw new BuildersError(`Builders did not give the app information (status ${res.status}).`, res.status);
33
+ let body;
34
+ try {
35
+ body = await res.json();
36
+ }
37
+ catch {
38
+ throw new BuildersError("The app information from Builders is not valid.", res.status);
39
+ }
40
+ const info = parse(body);
41
+ if (!info)
42
+ throw new BuildersError("The app information from Builders is not valid.", res.status);
43
+ return info;
44
+ }
@@ -0,0 +1,4 @@
1
+ export declare class BuildersError extends Error {
2
+ readonly status: number;
3
+ constructor(message: string, status?: number);
4
+ }
package/dist/errors.js ADDED
@@ -0,0 +1,12 @@
1
+ // The SDK error pattern (from Builders v1 packages/sdk/src/errors.ts).
2
+ // Each message is plain, so that Claude can give it to the person as it is.
3
+ export class BuildersError extends Error {
4
+ status;
5
+ constructor(message,
6
+ // The HTTP status of the platform's answer, or 0 when no answer came.
7
+ status = 500) {
8
+ super(message);
9
+ this.status = status;
10
+ this.name = "BuildersError";
11
+ }
12
+ }
package/dist/index.d.ts CHANGED
@@ -1,167 +1,2 @@
1
- export { G as GetUserOptions, H as HeaderSource, U as User, g as getUser, n as nameFromEmail } from './chunks/user-Dr8gM34m.js';
2
- import { z } from 'zod';
3
- import 'jose';
4
-
5
- declare const ADDONS: readonly ["ai", "storage", "cron", "notify", "internet"];
6
- type AddOn = (typeof ADDONS)[number];
7
-
8
- declare const IDENTITY_HEADER = "x-builders-identity";
9
- declare const SIGN_OUT_PATH = "/_builders/sign-out";
10
-
11
- declare const CLAUDE_MODELS: {
12
- readonly fast: "claude-haiku-5-5";
13
- readonly balanced: "claude-sonnet-5-5";
14
- readonly best: "claude-opus-5-5";
15
- };
16
- type AiModelChoice = keyof typeof CLAUDE_MODELS;
17
-
18
- declare const ATTACHMENT_TYPES: readonly ["image/png", "image/jpeg", "image/gif", "image/webp", "application/pdf"];
19
- type AttachmentType = (typeof ATTACHMENT_TYPES)[number];
20
-
21
- declare function resetMemoryDb(): void;
22
-
23
- declare const MAX_QUERY_LIMIT = 500;
24
-
25
- type WhereOp = "==" | "!=" | "<" | "<=" | ">" | ">=" | "in" | "not-in" | "array-contains" | "array-contains-any";
26
- type Doc<T> = T & {
27
- id: string;
28
- };
29
- interface Query<T extends object> {
30
- where(field: keyof T & string, op: WhereOp, value: unknown): Query<T>;
31
- orderBy(field: keyof T & string, direction?: "asc" | "desc"): Query<T>;
32
- limit(n: number): Query<T>;
33
- list(): Promise<Doc<T>[]>;
34
- }
35
- interface Collection<T extends object> extends Query<T> {
36
- get(id: string): Promise<Doc<T> | null>;
37
- add(data: T): Promise<Doc<T>>;
38
- set(id: string, data: T): Promise<void>;
39
- update(id: string, patch: Partial<T>): Promise<void>;
40
- delete(id: string): Promise<void>;
41
- }
42
- type DbMode = "firestore" | "memory";
43
- declare function dbMode(env?: Record<string, string | undefined>): DbMode;
44
- declare const db: {
45
- collection<T extends object>(name: string): Collection<T>;
46
- reset: typeof resetMemoryDb;
47
- };
48
-
49
- type AiModel = AiModelChoice;
50
- type Attachment = {
51
- data: Uint8Array | string;
52
- mediaType: AttachmentType;
53
- } | {
54
- storagePath: string;
55
- mediaType: AttachmentType;
56
- };
57
- interface AskOptions {
58
- system?: string;
59
- model?: AiModel;
60
- maxTokens?: number;
61
- temperature?: number;
62
- attachments?: Attachment[];
63
- signal?: AbortSignal;
64
- }
65
- interface JsonOptions<T> extends AskOptions {
66
- schema: z.ZodType<T>;
67
- description?: string;
68
- }
69
- type Message = {
70
- role: "user" | "assistant";
71
- content: string;
72
- attachments?: Attachment[];
73
- };
74
- interface ChatResult {
75
- text: string;
76
- stopReason: string | null;
77
- usage: {
78
- inputTokens: number;
79
- outputTokens: number;
80
- };
81
- }
82
- interface ImageOptions {
83
- aspectRatio?: "1:1" | "3:2" | "2:3" | "3:4" | "4:3" | "4:5" | "5:4" | "9:16" | "16:9" | "21:9";
84
- size?: "512" | "1K" | "2K";
85
- images?: Attachment[];
86
- signal?: AbortSignal;
87
- }
88
- interface GeneratedImage {
89
- mediaType: string;
90
- data: Buffer;
91
- text: string;
92
- }
93
- interface SpeakOptions {
94
- voice?: "Kore" | "Puck" | "Charon" | "Fenrir" | "Aoede" | "Leda" | "Orus" | "Zephyr";
95
- style?: string;
96
- signal?: AbortSignal;
97
- }
98
- interface TranscribeOptions {
99
- mediaType: "audio/wav" | "audio/mpeg" | "audio/mp3" | "audio/flac" | "audio/ogg" | "audio/webm" | "audio/mp4" | "audio/m4a";
100
- languages?: string[];
101
- signal?: AbortSignal;
102
- }
103
- declare const ai: {
104
- ask(prompt: string, opts?: AskOptions): Promise<string>;
105
- chat(messages: Message[], opts?: AskOptions): Promise<ChatResult>;
106
- json<T>(prompt: string, opts: JsonOptions<T>): Promise<T>;
107
- stream(prompt: string | Message[], opts?: AskOptions): AsyncGenerator<string, ChatResult>;
108
- image(prompt: string, opts?: ImageOptions): Promise<GeneratedImage>;
109
- speak(text: string, opts?: SpeakOptions): Promise<{
110
- mediaType: "audio/wav";
111
- data: Buffer;
112
- }>;
113
- transcribe(audio: Uint8Array | {
114
- storagePath: string;
115
- }, opts: TranscribeOptions): Promise<{
116
- text: string;
117
- seconds: number;
118
- }>;
119
- };
120
-
121
- declare const storage: {
122
- upload(path: string, data: string | Uint8Array, opts?: {
123
- contentType?: string;
124
- }): Promise<{
125
- path: string;
126
- }>;
127
- download(path: string): Promise<Buffer>;
128
- signedUrl(path: string, opts?: {
129
- action?: "read" | "write";
130
- expiresInMinutes?: number;
131
- contentType?: string;
132
- }): Promise<string>;
133
- list(prefix?: string): Promise<string[]>;
134
- delete(path: string): Promise<void>;
135
- };
136
-
137
- declare const notify: {
138
- email(to: string | string[], subject: string, text: string): Promise<void>;
139
- slack(text: string): Promise<void>;
140
- };
141
-
142
- declare const secrets: {
143
- get(name: string): Promise<string>;
144
- clearCache(): void;
145
- };
146
-
147
- declare class BuildersError extends Error {
148
- readonly status: number;
149
- constructor(message: string, status?: number);
150
- }
151
- declare class NotSignedInError extends BuildersError {
152
- constructor(message: string);
153
- }
154
- declare class NotFoundError extends BuildersError {
155
- constructor(message: string);
156
- }
157
- declare class AddOnNotEnabledError extends BuildersError {
158
- readonly addOn: AddOn;
159
- constructor(addOn: AddOn);
160
- }
161
-
162
- declare class NotOnBuildersError extends BuildersError {
163
- constructor(what: string);
164
- }
165
-
166
- export { AddOnNotEnabledError, BuildersError, IDENTITY_HEADER, MAX_QUERY_LIMIT, NotFoundError, NotOnBuildersError, NotSignedInError, SIGN_OUT_PATH, ai, db, dbMode, notify, secrets, storage };
167
- export type { AiModel, AskOptions, Attachment, ChatResult, Collection, DbMode, Doc, GeneratedImage, ImageOptions, JsonOptions, Message, Query, SpeakOptions, TranscribeOptions, WhereOp };
1
+ export { appInfo, APP_INFO_PATH, type AppInfo, type Environment } from "./app-info.js";
2
+ export { BuildersError } from "./errors.js";