@base44-preview/sdk 0.8.48-pr.284.e0a8d2f → 0.8.48-pr.286.f67ed26
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 +9 -0
- package/dist/platform/client/client.d.ts +35 -0
- package/dist/platform/client/client.js +218 -0
- package/dist/platform/client/errors.d.ts +11 -0
- package/dist/platform/client/errors.js +10 -0
- package/dist/platform/client/events.d.ts +159 -0
- package/dist/platform/client/events.js +1 -0
- package/dist/platform/client/index.d.ts +6 -0
- package/dist/platform/client/index.js +3 -0
- package/dist/platform/client/protocol.d.ts +11 -0
- package/dist/platform/client/protocol.js +39 -0
- package/dist/platform/client/subscription.d.ts +23 -0
- package/dist/platform/client/subscription.js +69 -0
- package/dist/platform/client/types.d.ts +33 -0
- package/dist/platform/client/types.js +1 -0
- package/package.json +40 -5
package/README.md
CHANGED
|
@@ -151,3 +151,12 @@ npm run create-docs
|
|
|
151
151
|
cd docs
|
|
152
152
|
mintlify dev
|
|
153
153
|
```
|
|
154
|
+
|
|
155
|
+
### Platform browser subscriptions
|
|
156
|
+
|
|
157
|
+
The separate `@base44/sdk/platform/client` entry point subscribes to public builder
|
|
158
|
+
updates through the white-label socket. It supports typed events, bounded delivery,
|
|
159
|
+
and reconnect replay using browser credentials supplied by your backend.
|
|
160
|
+
See [setup, public contract and recovery](platform-docs/client.md) and the
|
|
161
|
+
[TypeScript example](examples/platform-client.ts). Backend token integration and
|
|
162
|
+
workspace rollout are prerequisites; never use an API key in the browser.
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import type { PlatformClientOptions, PlatformSubscription, SubscriptionOptions } from "./types.js";
|
|
2
|
+
/** Browser client for read-only platform app events. Constructing it opens no connection. */
|
|
3
|
+
export declare class Base44PlatformClient {
|
|
4
|
+
private readonly socket;
|
|
5
|
+
private readonly subscriptions;
|
|
6
|
+
private readonly options;
|
|
7
|
+
private closed;
|
|
8
|
+
private needsFreshConnection;
|
|
9
|
+
private generation;
|
|
10
|
+
private authAttempt;
|
|
11
|
+
private cancelAuth?;
|
|
12
|
+
private connecting?;
|
|
13
|
+
private resolveConnect?;
|
|
14
|
+
private rejectConnect?;
|
|
15
|
+
/** Configure a dedicated connection. API keys belong exclusively on your backend. */
|
|
16
|
+
constructor(options: PlatformClientOptions);
|
|
17
|
+
/** Connect using a freshly obtained token. Resolves on CONNECT, not on app replay completion.
|
|
18
|
+
* Unexpected transport loss retries up to five times and rejoins active subscriptions.
|
|
19
|
+
* Call again after addressing a connection/auth failure; concurrent calls share one attempt.
|
|
20
|
+
*/
|
|
21
|
+
connect(): Promise<void>;
|
|
22
|
+
/** Subscribe before or after connecting. One subscription per app, maximum eight.
|
|
23
|
+
* Events and boundary callbacks are awaited in order per app. Failed application,
|
|
24
|
+
* invalid frames or server errors end the subscription without advancing its cursor.
|
|
25
|
+
*/
|
|
26
|
+
subscribe(appId: string, options: SubscriptionOptions): PlatformSubscription;
|
|
27
|
+
/** Stop delivery, cancel reconnection and release all listeners. Idempotent and terminal. */
|
|
28
|
+
close(): void;
|
|
29
|
+
private join;
|
|
30
|
+
private authenticate;
|
|
31
|
+
private clearConnecting;
|
|
32
|
+
private connectionError;
|
|
33
|
+
private serverError;
|
|
34
|
+
private protocolError;
|
|
35
|
+
}
|
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
import { io } from "socket.io-client";
|
|
2
|
+
import { PlatformSocketError } from "./errors.js";
|
|
3
|
+
import { appFromRoom, appPattern, decode, decodeJoined, errorCodes, eventApp, eventNames, object, roomFor } from "./protocol.js";
|
|
4
|
+
import { notify, Subscription } from "./subscription.js";
|
|
5
|
+
/** Browser client for read-only platform app events. Constructing it opens no connection. */
|
|
6
|
+
export class Base44PlatformClient {
|
|
7
|
+
/** Configure a dedicated connection. API keys belong exclusively on your backend. */
|
|
8
|
+
constructor(options) {
|
|
9
|
+
this.subscriptions = new Map();
|
|
10
|
+
this.closed = false;
|
|
11
|
+
this.needsFreshConnection = false;
|
|
12
|
+
this.generation = 0;
|
|
13
|
+
this.authAttempt = 0;
|
|
14
|
+
const url = new URL(options.serverUrl);
|
|
15
|
+
if (!["https:", "http:"].includes(url.protocol) || url.username || url.password || url.search || url.hash || url.pathname !== "/") {
|
|
16
|
+
throw new TypeError("serverUrl must be an HTTP(S) origin without credentials, path, query or fragment");
|
|
17
|
+
}
|
|
18
|
+
this.options = { ...options };
|
|
19
|
+
this.socket = io(`${url.origin}/partner`, {
|
|
20
|
+
path: "/ws-whitelabel/socket.io/", transports: ["websocket"], autoConnect: false,
|
|
21
|
+
forceNew: true, reconnectionAttempts: 5, reconnectionDelay: 1000, reconnectionDelayMax: 10000,
|
|
22
|
+
timeout: 20000,
|
|
23
|
+
auth: (callback) => { void this.authenticate(callback); },
|
|
24
|
+
});
|
|
25
|
+
this.socket.on("connect", () => {
|
|
26
|
+
var _a;
|
|
27
|
+
const generation = ++this.generation;
|
|
28
|
+
this.needsFreshConnection = false;
|
|
29
|
+
for (const subscription of this.subscriptions.values())
|
|
30
|
+
this.join(subscription, generation);
|
|
31
|
+
(_a = this.resolveConnect) === null || _a === void 0 ? void 0 : _a.call(this);
|
|
32
|
+
this.clearConnecting();
|
|
33
|
+
});
|
|
34
|
+
this.socket.on("disconnect", (reason) => {
|
|
35
|
+
++this.generation;
|
|
36
|
+
++this.authAttempt;
|
|
37
|
+
if (reason === "io server disconnect")
|
|
38
|
+
this.connectionError(new PlatformSocketError("connection_failed"));
|
|
39
|
+
});
|
|
40
|
+
this.socket.on("connect_error", (error) => {
|
|
41
|
+
var _a;
|
|
42
|
+
this.connectionError(new PlatformSocketError(((_a = error.data) === null || _a === void 0 ? void 0 : _a.code) === "connection_denied" ? "connection_denied" : "connection_failed"));
|
|
43
|
+
});
|
|
44
|
+
this.socket.io.on("reconnect_failed", () => this.connectionError(new PlatformSocketError("connection_failed")));
|
|
45
|
+
this.socket.on("joined", (raw) => {
|
|
46
|
+
var _a;
|
|
47
|
+
try {
|
|
48
|
+
const joined = decodeJoined(raw);
|
|
49
|
+
(_a = this.subscriptions.get(appFromRoom(joined.room))) === null || _a === void 0 ? void 0 : _a.joined(joined);
|
|
50
|
+
}
|
|
51
|
+
catch (_b) {
|
|
52
|
+
this.protocolError(raw);
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
this.socket.on("error", (raw) => this.serverError(raw));
|
|
56
|
+
for (const type of eventNames)
|
|
57
|
+
this.socket.on(type, (raw) => {
|
|
58
|
+
var _a;
|
|
59
|
+
try {
|
|
60
|
+
const appId = eventApp(type, raw);
|
|
61
|
+
if (!appId)
|
|
62
|
+
throw new Error("Invalid app");
|
|
63
|
+
(_a = this.subscriptions.get(appId)) === null || _a === void 0 ? void 0 : _a.event(decode(type, appId, raw));
|
|
64
|
+
}
|
|
65
|
+
catch (_b) {
|
|
66
|
+
this.protocolError(raw);
|
|
67
|
+
}
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
/** Connect using a freshly obtained token. Resolves on CONNECT, not on app replay completion.
|
|
71
|
+
* Unexpected transport loss retries up to five times and rejoins active subscriptions.
|
|
72
|
+
* Call again after addressing a connection/auth failure; concurrent calls share one attempt.
|
|
73
|
+
*/
|
|
74
|
+
connect() {
|
|
75
|
+
if (this.closed)
|
|
76
|
+
return Promise.reject(new PlatformSocketError("client_closed"));
|
|
77
|
+
if (this.socket.connected)
|
|
78
|
+
return Promise.resolve();
|
|
79
|
+
if (this.connecting)
|
|
80
|
+
return this.connecting;
|
|
81
|
+
const promise = new Promise((resolve, reject) => {
|
|
82
|
+
this.resolveConnect = resolve;
|
|
83
|
+
this.rejectConnect = reject;
|
|
84
|
+
});
|
|
85
|
+
this.connecting = promise;
|
|
86
|
+
this.socket.connect();
|
|
87
|
+
return promise;
|
|
88
|
+
}
|
|
89
|
+
/** Subscribe before or after connecting. One subscription per app, maximum eight.
|
|
90
|
+
* Events and boundary callbacks are awaited in order per app. Failed application,
|
|
91
|
+
* invalid frames or server errors end the subscription without advancing its cursor.
|
|
92
|
+
*/
|
|
93
|
+
subscribe(appId, options) {
|
|
94
|
+
if (this.closed)
|
|
95
|
+
throw new PlatformSocketError("client_closed");
|
|
96
|
+
if (!appPattern.test(appId))
|
|
97
|
+
throw new TypeError("appId must be 24 lowercase hexadecimal characters");
|
|
98
|
+
if (options.afterSeq !== undefined && (typeof options.afterSeq !== "string" || !options.afterSeq))
|
|
99
|
+
throw new TypeError("afterSeq must be a nonempty opaque cursor");
|
|
100
|
+
if (this.subscriptions.has(appId))
|
|
101
|
+
throw new TypeError("An app may only have one subscription per client");
|
|
102
|
+
if (this.subscriptions.size >= 8)
|
|
103
|
+
throw new PlatformSocketError("subscription_limit", appId);
|
|
104
|
+
const subscription = new Subscription(appId, { ...options }, () => {
|
|
105
|
+
this.subscriptions.delete(appId);
|
|
106
|
+
this.needsFreshConnection = true;
|
|
107
|
+
if (this.socket.connected)
|
|
108
|
+
this.socket.emit("leave", roomFor(appId));
|
|
109
|
+
});
|
|
110
|
+
this.subscriptions.set(appId, subscription);
|
|
111
|
+
if (this.socket.connected && this.needsFreshConnection) {
|
|
112
|
+
// Leave has no acknowledgement; a new transport fences late events from retired streams.
|
|
113
|
+
this.socket.disconnect();
|
|
114
|
+
void this.connect().catch(() => { }); // Connection errors are delivered through onError.
|
|
115
|
+
}
|
|
116
|
+
else if (this.socket.connected) {
|
|
117
|
+
this.join(subscription, this.generation);
|
|
118
|
+
}
|
|
119
|
+
return subscription;
|
|
120
|
+
}
|
|
121
|
+
/** Stop delivery, cancel reconnection and release all listeners. Idempotent and terminal. */
|
|
122
|
+
close() {
|
|
123
|
+
var _a, _b;
|
|
124
|
+
if (this.closed)
|
|
125
|
+
return;
|
|
126
|
+
this.closed = true;
|
|
127
|
+
++this.authAttempt;
|
|
128
|
+
(_a = this.cancelAuth) === null || _a === void 0 ? void 0 : _a.call(this);
|
|
129
|
+
++this.generation;
|
|
130
|
+
for (const subscription of this.subscriptions.values())
|
|
131
|
+
subscription.unsubscribe();
|
|
132
|
+
(_b = this.rejectConnect) === null || _b === void 0 ? void 0 : _b.call(this, new PlatformSocketError("client_closed"));
|
|
133
|
+
this.clearConnecting();
|
|
134
|
+
this.socket.removeAllListeners();
|
|
135
|
+
this.socket.io.removeAllListeners();
|
|
136
|
+
this.socket.disconnect();
|
|
137
|
+
}
|
|
138
|
+
join(subscription, generation) {
|
|
139
|
+
subscription.join((cursor) => {
|
|
140
|
+
if (this.socket.connected && generation === this.generation) {
|
|
141
|
+
this.socket.emit("join", roomFor(subscription.appId), cursor === undefined ? {} : { after_seq: cursor });
|
|
142
|
+
}
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
async authenticate(callback) {
|
|
146
|
+
var _a;
|
|
147
|
+
const attempt = ++this.authAttempt;
|
|
148
|
+
(_a = this.cancelAuth) === null || _a === void 0 ? void 0 : _a.call(this);
|
|
149
|
+
let timer;
|
|
150
|
+
const timeout = new Promise((_, reject) => {
|
|
151
|
+
timer = setTimeout(() => reject(new Error("Token timeout")), 20000);
|
|
152
|
+
this.cancelAuth = () => { clearTimeout(timer); reject(new Error("Cancelled")); };
|
|
153
|
+
});
|
|
154
|
+
try {
|
|
155
|
+
const token = await Promise.race([Promise.resolve().then(() => this.options.getToken()), timeout]);
|
|
156
|
+
if (this.closed || attempt !== this.authAttempt)
|
|
157
|
+
return;
|
|
158
|
+
if (typeof token !== "string" || !token.trim())
|
|
159
|
+
throw new Error("Missing token");
|
|
160
|
+
callback({ token });
|
|
161
|
+
}
|
|
162
|
+
catch (_b) {
|
|
163
|
+
if (this.closed || attempt !== this.authAttempt)
|
|
164
|
+
return;
|
|
165
|
+
this.socket.disconnect();
|
|
166
|
+
this.connectionError(new PlatformSocketError("token_unavailable"));
|
|
167
|
+
}
|
|
168
|
+
finally {
|
|
169
|
+
clearTimeout(timer);
|
|
170
|
+
if (attempt === this.authAttempt)
|
|
171
|
+
this.cancelAuth = undefined;
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
clearConnecting() {
|
|
175
|
+
this.connecting = undefined;
|
|
176
|
+
this.resolveConnect = undefined;
|
|
177
|
+
this.rejectConnect = undefined;
|
|
178
|
+
}
|
|
179
|
+
connectionError(error) {
|
|
180
|
+
var _a;
|
|
181
|
+
(_a = this.rejectConnect) === null || _a === void 0 ? void 0 : _a.call(this, error);
|
|
182
|
+
this.clearConnecting();
|
|
183
|
+
if (!this.closed)
|
|
184
|
+
notify(this.options.onError, error);
|
|
185
|
+
}
|
|
186
|
+
serverError(raw) {
|
|
187
|
+
var _a;
|
|
188
|
+
try {
|
|
189
|
+
const frame = object(raw);
|
|
190
|
+
const code = errorCodes.find((code) => code === frame.code);
|
|
191
|
+
if (!code)
|
|
192
|
+
throw new Error("Unknown error");
|
|
193
|
+
const appId = appFromRoom(frame.room);
|
|
194
|
+
if (appId)
|
|
195
|
+
(_a = this.subscriptions.get(appId)) === null || _a === void 0 ? void 0 : _a.fail(code);
|
|
196
|
+
else if (frame.room === null)
|
|
197
|
+
this.connectionError(new PlatformSocketError(code));
|
|
198
|
+
else
|
|
199
|
+
throw new Error("Invalid room");
|
|
200
|
+
}
|
|
201
|
+
catch (_b) {
|
|
202
|
+
this.protocolError(raw);
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
protocolError(raw) {
|
|
206
|
+
var _a, _b;
|
|
207
|
+
const frame = raw && typeof raw === "object" ? raw : {};
|
|
208
|
+
const appId = (_a = appFromRoom(frame.room)) !== null && _a !== void 0 ? _a : (typeof frame.app_id === "string" && appPattern.test(frame.app_id) ? frame.app_id : undefined);
|
|
209
|
+
if (appId)
|
|
210
|
+
(_b = this.subscriptions.get(appId)) === null || _b === void 0 ? void 0 : _b.fail("protocol_error");
|
|
211
|
+
else {
|
|
212
|
+
// Unknown routing means no app cursor can safely advance past this frame.
|
|
213
|
+
for (const subscription of [...this.subscriptions.values()])
|
|
214
|
+
subscription.fail("protocol_error");
|
|
215
|
+
this.connectionError(new PlatformSocketError("protocol_error"));
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/** Server subscription failures and client transport/processing failures. */
|
|
2
|
+
export type PlatformSocketErrorCode = "invalid_room" | "invalid_cursor" | "access_denied" | "subscription_limit" | "resync_required" | "stream_unavailable" | "connection_denied" | "connection_failed" | "token_unavailable" | "protocol_error" | "handler_failed" | "client_closed";
|
|
3
|
+
/** Sanitized failure. Original token-provider, handler and server exceptions are not retained. */
|
|
4
|
+
export declare class PlatformSocketError extends Error {
|
|
5
|
+
/** Stable machine-readable category. */
|
|
6
|
+
readonly code: PlatformSocketErrorCode;
|
|
7
|
+
/** Associated app, when the server identifies a valid room. */
|
|
8
|
+
readonly appId?: string;
|
|
9
|
+
/** Create a sanitized error with no credential-bearing cause or payload. */
|
|
10
|
+
constructor(code: PlatformSocketErrorCode, appId?: string);
|
|
11
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/** Sanitized failure. Original token-provider, handler and server exceptions are not retained. */
|
|
2
|
+
export class PlatformSocketError extends Error {
|
|
3
|
+
/** Create a sanitized error with no credential-bearing cause or payload. */
|
|
4
|
+
constructor(code, appId) {
|
|
5
|
+
super(`Platform socket: ${code}`);
|
|
6
|
+
this.name = "PlatformSocketError";
|
|
7
|
+
this.code = code;
|
|
8
|
+
this.appId = appId;
|
|
9
|
+
}
|
|
10
|
+
}
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
/** Public progress of an existing builder tool; arguments and results are withheld. */
|
|
2
|
+
export interface ToolCall {
|
|
3
|
+
/** Stable tool call identifier, when included in the update. */
|
|
4
|
+
id?: string;
|
|
5
|
+
/** Tool name displayed by the builder. */
|
|
6
|
+
name?: string;
|
|
7
|
+
/** Current execution state. */
|
|
8
|
+
status?: "running" | "success" | "error" | "stopped" | "waiting_for_user_input";
|
|
9
|
+
/** Whether the tool needs a user response through the partner backend. */
|
|
10
|
+
requires_user_input?: boolean;
|
|
11
|
+
/** Existing serialized interaction category; no raw interaction payload. */
|
|
12
|
+
waiting_on?: {
|
|
13
|
+
/** Kind of response expected. */
|
|
14
|
+
kind?: "approval" | "choice" | "input" | null;
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
/** Public message replacement. Omitted properties are not synthesized by the SDK. */
|
|
18
|
+
export interface ChatMessage {
|
|
19
|
+
/** Existing message identifier; replace a message with the same identifier. */
|
|
20
|
+
id?: string;
|
|
21
|
+
/** Public message author category. System messages are never delivered. */
|
|
22
|
+
role?: "user" | "assistant";
|
|
23
|
+
/** Generated or user-authored text. Structural filtering is not prose redaction. */
|
|
24
|
+
content?: string | null;
|
|
25
|
+
/** Attached file URLs. */
|
|
26
|
+
file_urls?: string[] | null;
|
|
27
|
+
/** Public tool progress, without arguments or results. */
|
|
28
|
+
tool_calls?: ToolCall[] | null;
|
|
29
|
+
/** Message timestamp, without author identity. */
|
|
30
|
+
metadata?: {
|
|
31
|
+
/** Existing timestamp string. */
|
|
32
|
+
created_date?: string | null;
|
|
33
|
+
} | null;
|
|
34
|
+
/** Existing checkpoint reference; mutations remain on the partner backend. */
|
|
35
|
+
checkpoint_id?: string | null;
|
|
36
|
+
}
|
|
37
|
+
/** Partial app update. Omitted keys mean unchanged; explicit null means clear. */
|
|
38
|
+
export interface AppUpdate {
|
|
39
|
+
/** Public builder state, without error diagnostics or billing context. */
|
|
40
|
+
status?: {
|
|
41
|
+
/** Current builder state. */
|
|
42
|
+
state?: "ready" | "processing" | "error";
|
|
43
|
+
/** Existing state timestamp. */
|
|
44
|
+
last_updated_date?: string | null;
|
|
45
|
+
} | null;
|
|
46
|
+
/** Whole-message replacement by identifier, not a recursive message patch. */
|
|
47
|
+
_last_msg?: ChatMessage | null;
|
|
48
|
+
/** Conversation containing the replacement message. */
|
|
49
|
+
_last_msg_conversation_id?: string | null;
|
|
50
|
+
/** Existing branch scope, if supplied by the producer. */
|
|
51
|
+
_scope_branch_id?: string | null;
|
|
52
|
+
/** Whether to reload the app preview. */
|
|
53
|
+
sandbox_should_reload?: boolean | null;
|
|
54
|
+
/** Existing preview navigation target. */
|
|
55
|
+
navigate_preview_to?: string | null;
|
|
56
|
+
/** Existing forced preview navigation target. */
|
|
57
|
+
navigate_preview_force_to?: string | null;
|
|
58
|
+
}
|
|
59
|
+
/** Public queued builder request. */
|
|
60
|
+
export interface QueueItem {
|
|
61
|
+
/** Stable queue item identifier. */
|
|
62
|
+
id: string;
|
|
63
|
+
/** User-authored request text. */
|
|
64
|
+
content: string;
|
|
65
|
+
/** Attached file URLs. */
|
|
66
|
+
file_urls?: string[] | null;
|
|
67
|
+
/** Existing creation timestamp. */
|
|
68
|
+
created_at: string;
|
|
69
|
+
/** Existing branch scope. */
|
|
70
|
+
branch_id?: string | null;
|
|
71
|
+
}
|
|
72
|
+
/** Full public queue snapshot, replacing the previous queue. */
|
|
73
|
+
export interface QueueUpdate {
|
|
74
|
+
/** App owning this queue. */
|
|
75
|
+
app_id: string;
|
|
76
|
+
/** Existing branch scope. */
|
|
77
|
+
branch_id?: string | null;
|
|
78
|
+
/** Current pending items. */
|
|
79
|
+
items: QueueItem[];
|
|
80
|
+
/** Whether queue processing is paused. */
|
|
81
|
+
is_paused: boolean;
|
|
82
|
+
/** Identifier of the item just processed, when supplied. */
|
|
83
|
+
processed_item_id?: string | null;
|
|
84
|
+
}
|
|
85
|
+
/** Public tool task progress. */
|
|
86
|
+
export interface TaskUpdate {
|
|
87
|
+
/** Existing task lifecycle event. */
|
|
88
|
+
event_type: "task_started" | "task_progress" | "task_completed" | "task_failed" | "task_cancelled";
|
|
89
|
+
/** Associated tool call. */
|
|
90
|
+
tool_call_id?: string | null;
|
|
91
|
+
/** Associated chat message. */
|
|
92
|
+
message_id?: string | null;
|
|
93
|
+
/** Existing branch scope. */
|
|
94
|
+
branch_id?: string | null;
|
|
95
|
+
/** Numeric progress only; diagnostic/free-text messages are withheld. */
|
|
96
|
+
progress?: {
|
|
97
|
+
/** Completed work units. */
|
|
98
|
+
current?: number | null;
|
|
99
|
+
/** Total work units, when known. */
|
|
100
|
+
total?: number | null;
|
|
101
|
+
/** Producer-supplied percentage. */
|
|
102
|
+
percentage?: number | null;
|
|
103
|
+
} | null;
|
|
104
|
+
}
|
|
105
|
+
/** Placeholder resolution or image-generation completion. */
|
|
106
|
+
export interface ImageReady {
|
|
107
|
+
/** Placeholder being resolved. */
|
|
108
|
+
placeholder_url: string;
|
|
109
|
+
/** Existing generation state. */
|
|
110
|
+
status: "pending" | "completed" | "failed";
|
|
111
|
+
/** Resolved image URL, or null when unavailable. */
|
|
112
|
+
image_url?: string | null;
|
|
113
|
+
}
|
|
114
|
+
/** Invalidation notice; fetch current state through the partner backend. */
|
|
115
|
+
export interface Directive {
|
|
116
|
+
/** Canonical app room. */
|
|
117
|
+
room: string;
|
|
118
|
+
/** Public invalidation category. */
|
|
119
|
+
type: "conversation_changed" | "app_files_changed";
|
|
120
|
+
/** Existing branch scope. */
|
|
121
|
+
branch_id?: string | null;
|
|
122
|
+
}
|
|
123
|
+
/** Mapping of wire event names to decoded public payloads. */
|
|
124
|
+
export interface PlatformEventMap {
|
|
125
|
+
/** Partial builder/app update. */
|
|
126
|
+
update_model: AppUpdate;
|
|
127
|
+
/** Conversation or file invalidation. */
|
|
128
|
+
directive: Directive;
|
|
129
|
+
/** Full queue snapshot. */
|
|
130
|
+
queue_update: QueueUpdate;
|
|
131
|
+
/** Numeric tool progress. */
|
|
132
|
+
task_update: TaskUpdate;
|
|
133
|
+
/** Image placeholder resolution. */
|
|
134
|
+
image_ready: ImageReady;
|
|
135
|
+
}
|
|
136
|
+
/** Ordered delivery with decoded data and the original event name and cursor. */
|
|
137
|
+
export type PlatformEvent = {
|
|
138
|
+
[K in keyof PlatformEventMap]: {
|
|
139
|
+
/** Original socket event name; narrows the payload type. */
|
|
140
|
+
type: K;
|
|
141
|
+
/** Authorized app receiving this event. */
|
|
142
|
+
appId: string;
|
|
143
|
+
/** Opaque replay cursor. Never parse, compare or increment it. */
|
|
144
|
+
seq: string;
|
|
145
|
+
/** Decoded payload; existing field names and omission/null semantics are retained. */
|
|
146
|
+
data: PlatformEventMap[K];
|
|
147
|
+
};
|
|
148
|
+
}[keyof PlatformEventMap];
|
|
149
|
+
/** Server replay boundary, delivered after all retained events through that boundary. */
|
|
150
|
+
export interface Joined {
|
|
151
|
+
/** Canonical app room. */
|
|
152
|
+
room: string;
|
|
153
|
+
/** Opaque boundary cursor; not an initial app snapshot. */
|
|
154
|
+
seq: string;
|
|
155
|
+
/** Server retention limit (currently 2,000 events per app). */
|
|
156
|
+
max_entries: number;
|
|
157
|
+
/** Server inactivity expiry (currently 3,600 seconds). */
|
|
158
|
+
inactivity_expiry_seconds: number;
|
|
159
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/** Read-only browser platform subscriptions, separate from the runtime and server SDKs. */
|
|
2
|
+
export { Base44PlatformClient } from "./client.js";
|
|
3
|
+
export { PlatformSocketError } from "./errors.js";
|
|
4
|
+
export type { PlatformSocketErrorCode } from "./errors.js";
|
|
5
|
+
export type { PlatformClientOptions, PlatformSubscription, SubscriptionOptions } from "./types.js";
|
|
6
|
+
export type { AppUpdate, ChatMessage, ToolCall, QueueItem, QueueUpdate, TaskUpdate, ImageReady, Directive, PlatformEventMap, PlatformEvent, Joined } from "./events.js";
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { Joined, PlatformEvent, PlatformEventMap } from "./events.js";
|
|
2
|
+
export declare const eventNames: readonly ["update_model", "directive", "queue_update", "task_update", "image_ready"];
|
|
3
|
+
export declare const errorCodes: readonly ["invalid_room", "invalid_cursor", "access_denied", "subscription_limit", "resync_required", "stream_unavailable"];
|
|
4
|
+
export declare const appPattern: RegExp;
|
|
5
|
+
export declare const roomFor: (appId: string) => string;
|
|
6
|
+
export declare function object(value: unknown): Record<string, unknown>;
|
|
7
|
+
export declare function string(value: unknown): string;
|
|
8
|
+
export declare function appFromRoom(value: unknown): string | undefined;
|
|
9
|
+
export declare function eventApp(type: keyof PlatformEventMap, raw: unknown): string | undefined;
|
|
10
|
+
export declare function decode(type: keyof PlatformEventMap, appId: string, raw: unknown): PlatformEvent;
|
|
11
|
+
export declare function decodeJoined(raw: unknown): Joined;
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
export const eventNames = ["update_model", "directive", "queue_update", "task_update", "image_ready"];
|
|
2
|
+
export const errorCodes = ["invalid_room", "invalid_cursor", "access_denied", "subscription_limit", "resync_required", "stream_unavailable"];
|
|
3
|
+
export const appPattern = /^[a-f0-9]{24}$/;
|
|
4
|
+
export const roomFor = (appId) => `/apps/${appId}`;
|
|
5
|
+
export function object(value) {
|
|
6
|
+
if (!value || typeof value !== "object" || Array.isArray(value))
|
|
7
|
+
throw new Error("Invalid frame");
|
|
8
|
+
return value;
|
|
9
|
+
}
|
|
10
|
+
export function string(value) {
|
|
11
|
+
if (typeof value !== "string" || !value)
|
|
12
|
+
throw new Error("Invalid string");
|
|
13
|
+
return value;
|
|
14
|
+
}
|
|
15
|
+
export function appFromRoom(value) {
|
|
16
|
+
return typeof value === "string" && /^\/apps\/[a-f0-9]{24}$/.test(value) ? value.slice(6) : undefined;
|
|
17
|
+
}
|
|
18
|
+
export function eventApp(type, raw) {
|
|
19
|
+
const frame = object(raw);
|
|
20
|
+
return type === "queue_update"
|
|
21
|
+
? typeof frame.app_id === "string" && appPattern.test(frame.app_id) ? frame.app_id : undefined
|
|
22
|
+
: appFromRoom(frame.room);
|
|
23
|
+
}
|
|
24
|
+
export function decode(type, appId, raw) {
|
|
25
|
+
const frame = object(raw);
|
|
26
|
+
const seq = string(frame.seq);
|
|
27
|
+
const wrapped = type === "update_model" || type === "task_update" || type === "image_ready";
|
|
28
|
+
const { seq: _, ...flat } = frame;
|
|
29
|
+
const data = wrapped ? object(JSON.parse(string(frame.data))) : flat;
|
|
30
|
+
// Payload schemas are owned by the service; only decode/validate the transport envelope here.
|
|
31
|
+
return { type, appId, seq, data };
|
|
32
|
+
}
|
|
33
|
+
export function decodeJoined(raw) {
|
|
34
|
+
const frame = object(raw);
|
|
35
|
+
if (!appFromRoom(frame.room) || !Number.isInteger(frame.max_entries) || !Number.isInteger(frame.inactivity_expiry_seconds))
|
|
36
|
+
throw new Error("Invalid boundary");
|
|
37
|
+
string(frame.seq);
|
|
38
|
+
return frame;
|
|
39
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { Joined, PlatformEvent } from "./events.js";
|
|
2
|
+
import { PlatformSocketError, type PlatformSocketErrorCode } from "./errors.js";
|
|
3
|
+
import type { PlatformSubscription, SubscriptionOptions } from "./types.js";
|
|
4
|
+
/** @internal */
|
|
5
|
+
export declare function notify(callback: (error: PlatformSocketError) => void, error: PlatformSocketError): void;
|
|
6
|
+
/** @internal */
|
|
7
|
+
export declare class Subscription implements PlatformSubscription {
|
|
8
|
+
readonly appId: string;
|
|
9
|
+
private options;
|
|
10
|
+
private remove;
|
|
11
|
+
cursor: string | undefined;
|
|
12
|
+
active: boolean;
|
|
13
|
+
private ready;
|
|
14
|
+
private pending;
|
|
15
|
+
private tail;
|
|
16
|
+
constructor(appId: string, options: SubscriptionOptions, remove: () => void);
|
|
17
|
+
enqueue(work: () => void | Promise<void>): void;
|
|
18
|
+
join(send: (cursor?: string) => void): void;
|
|
19
|
+
event(event: PlatformEvent): void;
|
|
20
|
+
joined(joined: Joined): void;
|
|
21
|
+
fail(code: PlatformSocketErrorCode): void;
|
|
22
|
+
unsubscribe(): void;
|
|
23
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { PlatformSocketError } from "./errors.js";
|
|
2
|
+
/** @internal */
|
|
3
|
+
export function notify(callback, error) {
|
|
4
|
+
try {
|
|
5
|
+
callback(error);
|
|
6
|
+
}
|
|
7
|
+
catch ( /* An error observer cannot interrupt other app subscriptions. */_a) { /* An error observer cannot interrupt other app subscriptions. */ }
|
|
8
|
+
}
|
|
9
|
+
/** @internal */
|
|
10
|
+
export class Subscription {
|
|
11
|
+
constructor(appId, options, remove) {
|
|
12
|
+
this.appId = appId;
|
|
13
|
+
this.options = options;
|
|
14
|
+
this.remove = remove;
|
|
15
|
+
this.active = true;
|
|
16
|
+
this.ready = false;
|
|
17
|
+
this.pending = 0;
|
|
18
|
+
this.tail = Promise.resolve();
|
|
19
|
+
this.cursor = options.afterSeq;
|
|
20
|
+
}
|
|
21
|
+
enqueue(work) {
|
|
22
|
+
if (!this.active)
|
|
23
|
+
return;
|
|
24
|
+
if (this.pending >= 1000) {
|
|
25
|
+
this.fail("resync_required");
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
this.pending++;
|
|
29
|
+
this.tail = this.tail.then(async () => {
|
|
30
|
+
if (this.active)
|
|
31
|
+
await work();
|
|
32
|
+
}).catch(() => this.fail("handler_failed")).finally(() => { this.pending--; });
|
|
33
|
+
}
|
|
34
|
+
join(send) {
|
|
35
|
+
this.enqueue(() => { this.ready = false; send(this.cursor); });
|
|
36
|
+
}
|
|
37
|
+
event(event) {
|
|
38
|
+
this.enqueue(async () => {
|
|
39
|
+
// A fresh subscription starts at joined, not at any old in-flight room events.
|
|
40
|
+
if ((!this.ready && this.cursor === undefined) || event.seq === this.cursor)
|
|
41
|
+
return;
|
|
42
|
+
await this.options.onEvent(event);
|
|
43
|
+
if (this.active)
|
|
44
|
+
this.cursor = event.seq;
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
joined(joined) {
|
|
48
|
+
this.enqueue(async () => {
|
|
49
|
+
var _a, _b;
|
|
50
|
+
await ((_b = (_a = this.options).onJoined) === null || _b === void 0 ? void 0 : _b.call(_a, joined));
|
|
51
|
+
if (this.active) {
|
|
52
|
+
this.cursor = joined.seq;
|
|
53
|
+
this.ready = true;
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
fail(code) {
|
|
58
|
+
if (!this.active)
|
|
59
|
+
return;
|
|
60
|
+
this.unsubscribe();
|
|
61
|
+
notify(this.options.onError, new PlatformSocketError(code, this.appId));
|
|
62
|
+
}
|
|
63
|
+
unsubscribe() {
|
|
64
|
+
if (!this.active)
|
|
65
|
+
return;
|
|
66
|
+
this.active = false;
|
|
67
|
+
this.remove();
|
|
68
|
+
}
|
|
69
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import type { Joined, PlatformEvent } from "./events.js";
|
|
2
|
+
import type { PlatformSocketError } from "./errors.js";
|
|
3
|
+
/** Configuration for the browser platform connection. Never supply an API key. */
|
|
4
|
+
export interface PlatformClientOptions {
|
|
5
|
+
/** Origin of the platform service, e.g. https://base44.app. No path/query/credentials. */
|
|
6
|
+
serverUrl: string;
|
|
7
|
+
/** Fetch a browser credential from your backend. Called for every connection attempt. */
|
|
8
|
+
getToken: () => string | Promise<string>;
|
|
9
|
+
/** Connection-level error notification. Errors never include tokens or server exception text. */
|
|
10
|
+
onError: (error: PlatformSocketError) => void;
|
|
11
|
+
}
|
|
12
|
+
/** One app subscription; at most eight may be active per client. */
|
|
13
|
+
export interface SubscriptionOptions {
|
|
14
|
+
/** Last successfully applied cursor for this app; omit for a fresh live boundary. */
|
|
15
|
+
afterSeq?: string;
|
|
16
|
+
/** Apply each event. Delivery is serial per app; rejection pauses this subscription. */
|
|
17
|
+
onEvent: (event: PlatformEvent) => void | Promise<void>;
|
|
18
|
+
/** Handle subscription errors. Reconcile on resync_required; never silently reset a cursor. */
|
|
19
|
+
onError: (error: PlatformSocketError) => void;
|
|
20
|
+
/** Optional replay-complete notification, awaited before advancing to the boundary cursor. */
|
|
21
|
+
onJoined?: (joined: Joined) => void | Promise<void>;
|
|
22
|
+
}
|
|
23
|
+
/** Subscription lifetime and last successfully applied cursor. */
|
|
24
|
+
export interface PlatformSubscription {
|
|
25
|
+
/** App identifier. */
|
|
26
|
+
readonly appId: string;
|
|
27
|
+
/** Last applied event/boundary cursor; persist alongside the state it describes. */
|
|
28
|
+
readonly cursor: string | undefined;
|
|
29
|
+
/** True while subscribed; false after an error or explicit unsubscribe. */
|
|
30
|
+
readonly active: boolean;
|
|
31
|
+
/** Stop this app's delivery and release its subscription slot. Idempotent. */
|
|
32
|
+
unsubscribe(): void;
|
|
33
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@base44-preview/sdk",
|
|
3
|
-
"version": "0.8.48-pr.
|
|
3
|
+
"version": "0.8.48-pr.286.f67ed26",
|
|
4
4
|
"description": "JavaScript SDK for Base44 API",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -9,8 +9,8 @@
|
|
|
9
9
|
"dist"
|
|
10
10
|
],
|
|
11
11
|
"scripts": {
|
|
12
|
-
"build": "
|
|
13
|
-
"lint": "eslint src",
|
|
12
|
+
"build": "npm run build:runtime && npm run build:platform",
|
|
13
|
+
"lint": "eslint src platform-src examples/platform-client.ts",
|
|
14
14
|
"test": "npm run test:types && vitest run",
|
|
15
15
|
"test:types": "tsc --noEmit -p tsconfig.type-tests.json",
|
|
16
16
|
"test:unit": "vitest run tests/unit",
|
|
@@ -23,7 +23,11 @@
|
|
|
23
23
|
"create-docs-local": "npm run create-docs && npm run copy-docs-local",
|
|
24
24
|
"copy-docs-local": "node scripts/mintlify-post-processing/copy-to-local-docs.js",
|
|
25
25
|
"create-docs:generate": "typedoc",
|
|
26
|
-
"create-docs:process": "node scripts/mintlify-post-processing/file-processing/file-processing.js"
|
|
26
|
+
"create-docs:process": "node scripts/mintlify-post-processing/file-processing/file-processing.js",
|
|
27
|
+
"build:runtime": "tsc",
|
|
28
|
+
"build:platform": "tsc -p tsconfig.platform.json",
|
|
29
|
+
"docs:platform-client": "typedoc --options typedoc.platform-client.json",
|
|
30
|
+
"test:package": "npm run build && node --test tests/package/platform-client.test.mjs"
|
|
27
31
|
},
|
|
28
32
|
"dependencies": {
|
|
29
33
|
"axios": "^1.18.1",
|
|
@@ -63,5 +67,36 @@
|
|
|
63
67
|
"bugs": {
|
|
64
68
|
"url": "https://github.com/base44/javascript-sdk/issues"
|
|
65
69
|
},
|
|
66
|
-
"homepage": "https://github.com/base44/javascript-sdk#readme"
|
|
70
|
+
"homepage": "https://github.com/base44/javascript-sdk#readme",
|
|
71
|
+
"exports": {
|
|
72
|
+
".": {
|
|
73
|
+
"types": "./dist/index.d.ts",
|
|
74
|
+
"default": "./dist/index.js"
|
|
75
|
+
},
|
|
76
|
+
"./dist/*.d.ts": "./dist/*.d.ts",
|
|
77
|
+
"./dist/*.js": {
|
|
78
|
+
"types": "./dist/*.d.ts",
|
|
79
|
+
"default": "./dist/*.js"
|
|
80
|
+
},
|
|
81
|
+
"./dist/*": {
|
|
82
|
+
"types": "./dist/*.d.ts",
|
|
83
|
+
"default": "./dist/*.js"
|
|
84
|
+
},
|
|
85
|
+
"./package.json": "./package.json",
|
|
86
|
+
"./*": "./*",
|
|
87
|
+
"./platform/client": {
|
|
88
|
+
"types": "./dist/platform/client/index.d.ts",
|
|
89
|
+
"default": "./dist/platform/client/index.js"
|
|
90
|
+
}
|
|
91
|
+
},
|
|
92
|
+
"typesVersions": {
|
|
93
|
+
"*": {
|
|
94
|
+
"platform/client": [
|
|
95
|
+
"dist/platform/client/index.d.ts"
|
|
96
|
+
],
|
|
97
|
+
"*": [
|
|
98
|
+
"*"
|
|
99
|
+
]
|
|
100
|
+
}
|
|
101
|
+
}
|
|
67
102
|
}
|