@base44-preview/platform 0.0.0 → 0.1.0-pr.312.2ef55e3

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,3 @@
1
+ // Generated from asyncapi.json by scripts/gen-events.mjs. Do not edit; run `npm run gen:events`.
2
+ /** @internal */
3
+ export const serverEventNames = ["message.updated", "message.removed", "app.status_changed", "preview.reload_requested", "preview.navigation_requested", "queue.updated", "task.progressed", "image.resolved", "conversation.changed", "files.changed", "branch.deleted", "repository.changed", "pull_request.changed", "session.ended", "room.access_denied", "room.access_revoked", "room.snapshot_unavailable", "app.snapshot"];
@@ -0,0 +1,58 @@
1
+ import type { Message, QueueState, ServerEventMap, Snapshot } from "./builder.events.generated.js";
2
+ /**
3
+ * Maps each live event name to the type of its `data` payload.
4
+ *
5
+ * Generated from the service's AsyncAPI document, so it covers every app event the service sends.
6
+ * The client handles the session events `app.snapshot`, `session.ended` and `room.*` itself, and
7
+ * reports them through `onSnapshot` and `onError` instead.
8
+ */
9
+ export type PlatformEventMap = {
10
+ [K in keyof ServerEventMap as K extends "app.snapshot" | "session.ended" | `room.${string}` ? never : K]: ServerEventMap[K];
11
+ };
12
+ /**
13
+ * One live event for a subscribed app.
14
+ *
15
+ * Check `type` to narrow `data` to that event's payload. A key missing from `data` means the value
16
+ * is unchanged, and an explicit `null` clears it. For every event and its payload, see
17
+ * [Socket messages](/developers/references/platform-sdk/socket-messages).
18
+ *
19
+ * @example
20
+ * ```typescript
21
+ * // Narrow an event by type
22
+ * function onEvent(event: PlatformEvent) {
23
+ * if (event.type === "message.updated") {
24
+ * const { message } = event.data;
25
+ * console.log(`Message ${message.id} changed`);
26
+ * }
27
+ * }
28
+ * ```
29
+ */
30
+ export type PlatformEvent = {
31
+ [K in keyof PlatformEventMap]: {
32
+ /** Event name. Narrows the type of `data`. */
33
+ type: K;
34
+ /** ID of the app the event belongs to. */
35
+ appId: string;
36
+ /** Event payload. */
37
+ data: PlatformEventMap[K];
38
+ };
39
+ }[keyof PlatformEventMap];
40
+ /**
41
+ * An app's current state.
42
+ *
43
+ * Arrives after every join and rejoin, and after a rewrite of the main conversation. Replace
44
+ * everything you hold for the app with it. It holds only the last 50 messages.
45
+ */
46
+ export interface PlatformSnapshot extends Omit<Snapshot, "queue"> {
47
+ /** The app's room, `/apps/{app_id}`. */
48
+ room: string;
49
+ /** The main branch's prompt queue. Missing when the server sends none. */
50
+ queue?: QueueState;
51
+ }
52
+ /**
53
+ * One tool call in a message's `tool_calls`. Checking `name` narrows it to that tool's call and
54
+ * `OtherToolCall`, which covers tools the SDK doesn't know, so check a field with `in` before using it.
55
+ */
56
+ export type ToolCall = NonNullable<Message["tool_calls"]>[number];
57
+ /** Why a safety guard parked a tool call for approval. Check `guard` to narrow it. */
58
+ export type GuardApproval = NonNullable<ToolCall["approval"]>;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,7 @@
1
+ import { BuilderSocket } from "./builder-socket.js";
2
+ /** @internal */
3
+ export function createBuilder(config) {
4
+ return {
5
+ init(options) { return new BuilderSocket(config, options); },
6
+ };
7
+ }
@@ -0,0 +1,162 @@
1
+ import type { PlatformEvent, PlatformSnapshot } from "./builder.events.types.js";
2
+ import type { PlatformSocketError } from "../errors.js";
3
+ /** Options for {@linkcode BuilderModule.init | init()}. */
4
+ export interface BuilderInitOptions {
5
+ /**
6
+ * Called on errors that affect the whole session, such as a failed connection or an ended session.
7
+ * Errors for one app go to that subscription's `onError` instead. See the
8
+ * [error codes](/developers/references/platform-sdk/docs/classes/PlatformSocketError#platformsocketerrorcode).
9
+ */
10
+ onError: (error: PlatformSocketError) => void;
11
+ }
12
+ /**
13
+ * Builder module for following the AI chat in your users' apps live.
14
+ *
15
+ * Each session holds one socket and up to eight app subscriptions. For each app you get a
16
+ * snapshot of its chat, then every change as it happens:
17
+ * - New and updated chat messages, with the AI's tool calls.
18
+ * - The AI's status, from processing to ready.
19
+ * - Preview reloads and navigation, the prompt queue, and generated images.
20
+ *
21
+ * The module does nothing until you call {@linkcode init | init()}.
22
+ */
23
+ export interface BuilderModule {
24
+ /**
25
+ * Creates a builder session without connecting it.
26
+ *
27
+ * Call {@linkcode BuilderSession.connect | connect()} on the returned session to open the socket.
28
+ * Each session is independent, with its own socket, token and subscriptions.
29
+ *
30
+ * @param options - Session options.
31
+ * @returns The new session.
32
+ *
33
+ * @example
34
+ * ```typescript
35
+ * // Create and connect a session
36
+ * const builder = client.builder.init({
37
+ * onError(error) {
38
+ * console.error('Builder session error', error.code);
39
+ * },
40
+ * });
41
+ * await builder.connect();
42
+ * ```
43
+ */
44
+ init(options: BuilderInitOptions): BuilderSession;
45
+ }
46
+ /**
47
+ * A `BuilderSession`: one socket with its app subscriptions and its reconnection. Use its methods
48
+ * to connect, follow apps and close.
49
+ */
50
+ export interface BuilderSession {
51
+ /**
52
+ * Connects the session's socket.
53
+ *
54
+ * Resolves once the socket connects, before any snapshot arrives. If the connection drops, the
55
+ * session retries five times and rejoins every active subscription, each with a fresh snapshot.
56
+ * An expired or rejected token is replaced once through `getSessionToken`. Concurrent calls share
57
+ * one attempt. Call it again after a failure or a `session_replaced` error.
58
+ *
59
+ * @returns Promise that resolves when the socket connects.
60
+ * @throws {PlatformSocketError} With `connection_denied`, `connection_failed` or `session_unavailable`
61
+ * when the session can't connect, or `client_closed` when the session is closed first.
62
+ *
63
+ * @example
64
+ * ```typescript
65
+ * // Connect and handle failure
66
+ * try {
67
+ * await builder.connect();
68
+ * } catch (error) {
69
+ * console.error('Could not connect', error.code);
70
+ * }
71
+ * ```
72
+ */
73
+ connect(): Promise<void>;
74
+ /**
75
+ * Subscribes to one app's chat.
76
+ *
77
+ * You can subscribe before or after connecting. Each app can have one subscription per session,
78
+ * and a session can have up to eight. The app must be on the session's allowlist. The
79
+ * subscription first receives a snapshot of the app, then each live event.
80
+ *
81
+ * Callbacks for an app run one at a time, in order, and the session waits for a returned promise
82
+ * before the next one. A slow app doesn't hold up the others. If more than 1,000 deliveries wait
83
+ * for one app, its subscription ends with `delivery_overflow`.
84
+ *
85
+ * @param appId - ID of the app to follow, 24 lowercase hexadecimal characters.
86
+ * @param options - Callbacks for the app's snapshots, events and errors.
87
+ * @returns The subscription.
88
+ * @throws {TypeError} When `appId` isn't a valid app ID or the app already has a subscription.
89
+ * @throws {PlatformSocketError} With `subscription_limit` when the session has eight subscriptions.
90
+ *
91
+ * @example
92
+ * ```typescript
93
+ * // Follow an app
94
+ * const subscription = builder.subscribe(appId, {
95
+ * onSnapshot(snapshot) {
96
+ * renderChat(snapshot.messages);
97
+ * },
98
+ * onEvent(event) {
99
+ * if (event.type === 'message.updated') upsertMessage(event.data.message);
100
+ * },
101
+ * onError(error) {
102
+ * console.error(`App ${error.appId}`, error.code);
103
+ * },
104
+ * });
105
+ * ```
106
+ */
107
+ subscribe(appId: string, options: SubscriptionOptions): PlatformSubscription;
108
+ /**
109
+ * Closes the session.
110
+ *
111
+ * Ends every subscription, stops reconnecting and disconnects the socket. Calling it again does
112
+ * nothing. A closed session can't be reused, so call [`init()`](/developers/references/platform-sdk/docs/interfaces/builder#init)
113
+ * for a new one.
114
+ *
115
+ * @example
116
+ * ```typescript
117
+ * // Close a session
118
+ * builder.close();
119
+ * ```
120
+ */
121
+ close(): void;
122
+ }
123
+ /** Callbacks for one app subscription. */
124
+ export interface SubscriptionOptions {
125
+ /**
126
+ * Called with the app's current state, a [`PlatformSnapshot`](#platformsnapshot). Replace everything you hold for the app with it.
127
+ *
128
+ * Arrives after every join and rejoin, and after the main conversation is rewritten by an undo,
129
+ * restore or sync. Live events follow it. It holds only the last 50 messages, so merge by message
130
+ * ID to keep older history.
131
+ */
132
+ onSnapshot: (snapshot: PlatformSnapshot) => void | Promise<void>;
133
+ /**
134
+ * Called with each live event, a [`PlatformEvent`](#platformevent). Events for an app arrive one at a time, in order. If the callback
135
+ * throws or rejects, the subscription ends with `handler_failed`. New event types can be added at
136
+ * any time, so ignore types you don't handle. For every event and its payload,
137
+ * see [Socket messages](/developers/references/platform-sdk/socket-messages).
138
+ */
139
+ onEvent: (event: PlatformEvent) => void | Promise<void>;
140
+ /**
141
+ * Called on errors for this app. Every error ends the subscription except `snapshot_unavailable`.
142
+ * See the [error codes](/developers/references/platform-sdk/docs/classes/PlatformSocketError#platformsocketerrorcode).
143
+ */
144
+ onError: (error: PlatformSocketError) => void;
145
+ }
146
+ /** One app subscription. */
147
+ export interface PlatformSubscription {
148
+ /** ID of the app. */
149
+ readonly appId: string;
150
+ /** Whether the subscription still receives updates. It turns false after an error or {@linkcode unsubscribe | unsubscribe()}. */
151
+ readonly active: boolean;
152
+ /**
153
+ * Stops updates for the app and frees its subscription slot. Calling it again does nothing.
154
+ *
155
+ * @example
156
+ * ```typescript
157
+ * // Stop following an app
158
+ * subscription.unsubscribe();
159
+ * ```
160
+ */
161
+ unsubscribe(): void;
162
+ }
@@ -0,0 +1 @@
1
+ export {};
package/package.json CHANGED
@@ -1,7 +1,60 @@
1
1
  {
2
2
  "name": "@base44-preview/platform",
3
- "version": "0.0.0",
4
- "description": "Preview builds of @base44/platform, published from PRs. Not for production use.",
3
+ "version": "0.1.0-pr.312.2ef55e3",
4
+ "description": "Browser client for building on the Base44 platform",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "type": "module",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./dist/index.d.ts",
11
+ "default": "./dist/index.js"
12
+ },
13
+ "./package.json": "./package.json"
14
+ },
15
+ "files": [
16
+ "dist"
17
+ ],
18
+ "publishConfig": {
19
+ "access": "public"
20
+ },
21
+ "scripts": {
22
+ "build": "tsc",
23
+ "gen:events": "node scripts/gen-events.mjs",
24
+ "check:events": "node scripts/gen-events.mjs --check",
25
+ "sync:asyncapi": "node scripts/sync-asyncapi.mjs",
26
+ "docs": "typedoc",
27
+ "test": "npm run test:types && npm run test:unit && npm run docs && npm run test:package",
28
+ "test:types": "tsc --noEmit -p tsconfig.type-tests.json",
29
+ "test:unit": "vitest run",
30
+ "test:package": "npm run build && node --test tests/package.test.mjs",
31
+ "prepublishOnly": "npm run build",
32
+ "create-docs": "npm run create-docs:generate && npm run create-docs:process",
33
+ "create-docs-local": "npm run create-docs && npm run copy-docs-local",
34
+ "copy-docs-local": "node ../sdk/scripts/mintlify-post-processing/copy-to-local-docs.js --package-dir .",
35
+ "create-docs:generate": "node -e \"fs.rmSync('docs',{recursive:true,force:true})\" && typedoc --options typedoc.mintlify.json",
36
+ "create-docs:process": "node ../sdk/scripts/mintlify-post-processing/file-processing/file-processing.js --package-dir ."
37
+ },
38
+ "dependencies": {
39
+ "socket.io-client": "^4.8.3"
40
+ },
41
+ "keywords": [
42
+ "base44",
43
+ "platform",
44
+ "white-label"
45
+ ],
46
+ "author": "Base44",
5
47
  "license": "MIT",
6
- "repository": { "type": "git", "url": "git+https://github.com/base44/javascript-sdk.git", "directory": "packages/platform" }
48
+ "repository": {
49
+ "type": "git",
50
+ "url": "git+https://github.com/base44/javascript-sdk.git",
51
+ "directory": "packages/platform"
52
+ },
53
+ "bugs": {
54
+ "url": "https://github.com/base44/javascript-sdk/issues"
55
+ },
56
+ "homepage": "https://github.com/base44/javascript-sdk/tree/main/packages/platform#readme",
57
+ "devDependencies": {
58
+ "json-schema-to-typescript": "16.0.0"
59
+ }
7
60
  }