@base44-preview/sdk 0.8.35-pr.212.b15255e → 0.8.35-pr.212.e4f0f07
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/dist/index.d.ts +2 -1
- package/dist/index.js +1 -0
- package/dist/modules/realtime.d.ts +1 -4
- package/dist/modules/realtime.js +16 -18
- package/dist/modules/realtime.types.d.ts +42 -25
- package/dist/realtime-handler.d.ts +27 -0
- package/dist/realtime-handler.js +24 -0
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -10,8 +10,9 @@ export type { IntegrationsModule, IntegrationEndpointFunction, CoreIntegrations,
|
|
|
10
10
|
export type { FunctionsModule, FunctionName, FunctionNameRegistry, } from "./modules/functions.types.js";
|
|
11
11
|
export type { AgentsModule, AgentName, AgentNameRegistry, AgentConversation, AgentMessage, AgentMessageReasoning, AgentMessageToolCall, AgentMessageUsage, AgentMessageCustomContext, AgentMessageMetadata, CreateConversationParams, } from "./modules/agents.types.js";
|
|
12
12
|
export type { AppLogsModule } from "./modules/app-logs.types.js";
|
|
13
|
-
export type { RealtimeModule, RealtimeHandlerClient,
|
|
13
|
+
export type { RealtimeModule, RealtimeHandlerClient, RealtimeHandlerNameRegistry, RealtimeHandlerRegistry, } from "./modules/realtime.types.js";
|
|
14
14
|
export type { SsoModule, SsoAccessTokenResponse } from "./modules/sso.types.js";
|
|
15
|
+
export { RealtimeHandler, type Conn } from "./realtime-handler.js";
|
|
15
16
|
export type { ConnectorsModule, UserConnectorsModule, } from "./modules/connectors.types.js";
|
|
16
17
|
export type { CustomIntegrationsModule, CustomIntegrationCallParams, CustomIntegrationCallResponse, } from "./modules/custom-integrations.types.js";
|
|
17
18
|
export type { GetAccessTokenOptions, SaveAccessTokenOptions, RemoveAccessTokenOptions, GetLoginUrlOptions, } from "./utils/auth-utils.types.js";
|
package/dist/index.js
CHANGED
|
@@ -3,3 +3,4 @@ import { Base44Error } from "./utils/axios-client.js";
|
|
|
3
3
|
import { getAccessToken, saveAccessToken, removeAccessToken, getLoginUrl, } from "./utils/auth-utils.js";
|
|
4
4
|
export { createClient, createClientFromRequest, Base44Error, getAccessToken, saveAccessToken, removeAccessToken, getLoginUrl, };
|
|
5
5
|
export * from "./types.js";
|
|
6
|
+
export { RealtimeHandler } from "./realtime-handler.js";
|
|
@@ -4,10 +4,7 @@ export declare function createRealtimeModule(config: {
|
|
|
4
4
|
dispatcherWsUrl: string;
|
|
5
5
|
}): Record<string, RealtimeHandler>;
|
|
6
6
|
interface RealtimeHandler {
|
|
7
|
-
subscribe(instanceId: string, callback: (data: unknown) => void):
|
|
8
|
-
send(data: unknown): void;
|
|
9
|
-
close(): void;
|
|
10
|
-
}>;
|
|
7
|
+
subscribe(instanceId: string, callback: (data: unknown) => void): () => void;
|
|
11
8
|
send(instanceId: string, data: unknown): void;
|
|
12
9
|
}
|
|
13
10
|
export {};
|
package/dist/modules/realtime.js
CHANGED
|
@@ -8,17 +8,17 @@ export function createRealtimeModule(config) {
|
|
|
8
8
|
return new Proxy({}, {
|
|
9
9
|
get(_, handlerName) {
|
|
10
10
|
return {
|
|
11
|
-
|
|
11
|
+
subscribe(instanceId, callback) {
|
|
12
12
|
var _a;
|
|
13
13
|
const key = socketKey(handlerName, instanceId);
|
|
14
14
|
// close existing if any
|
|
15
15
|
(_a = activeSockets.get(key)) === null || _a === void 0 ? void 0 : _a.close();
|
|
16
|
-
|
|
16
|
+
// startClosed: don't connect until we have a token
|
|
17
17
|
const ws = new PartySocket({
|
|
18
18
|
host: config.dispatcherWsUrl,
|
|
19
19
|
party: handlerName,
|
|
20
20
|
room: instanceId,
|
|
21
|
-
|
|
21
|
+
startClosed: true,
|
|
22
22
|
});
|
|
23
23
|
activeSockets.set(key, ws);
|
|
24
24
|
ws.addEventListener("message", (ev) => {
|
|
@@ -29,26 +29,24 @@ export function createRealtimeModule(config) {
|
|
|
29
29
|
// ignore malformed
|
|
30
30
|
}
|
|
31
31
|
});
|
|
32
|
-
//
|
|
33
|
-
|
|
32
|
+
// Fetch token then open; re-fetch on every close (token expires in 30s)
|
|
33
|
+
const connect = async () => {
|
|
34
34
|
if (activeSockets.get(key) !== ws)
|
|
35
|
-
return;
|
|
35
|
+
return;
|
|
36
36
|
try {
|
|
37
|
-
const
|
|
38
|
-
ws.updateProperties({ query: { token
|
|
37
|
+
const token = await config.getToken(handlerName, instanceId);
|
|
38
|
+
ws.updateProperties({ party: handlerName, room: instanceId, query: { token } });
|
|
39
|
+
ws.reconnect();
|
|
39
40
|
}
|
|
40
41
|
catch (_a) {
|
|
41
|
-
//
|
|
42
|
+
// retry on next close
|
|
42
43
|
}
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
close()
|
|
49
|
-
activeSockets.delete(key);
|
|
50
|
-
ws.close();
|
|
51
|
-
},
|
|
44
|
+
};
|
|
45
|
+
ws.addEventListener("close", () => connect());
|
|
46
|
+
connect();
|
|
47
|
+
return () => {
|
|
48
|
+
activeSockets.delete(key);
|
|
49
|
+
ws.close();
|
|
52
50
|
};
|
|
53
51
|
},
|
|
54
52
|
send(instanceId, data) {
|
|
@@ -1,32 +1,46 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* Extend this interface to add typed `subscribe` callbacks and `send` payloads
|
|
3
|
+
* for your deployed RealtimeHandlers.
|
|
4
|
+
*
|
|
5
|
+
* This is separate from {@link RealtimeHandlerNameRegistry} (which is auto-generated
|
|
6
|
+
* by `base44 types generate`), so there are no conflicts.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```typescript
|
|
10
|
+
* declare module "@base44/sdk" {
|
|
11
|
+
* interface RealtimeHandlerRegistry {
|
|
12
|
+
* ChatRoom: {
|
|
13
|
+
* inbound: { type: "joined" | "left" | "message"; userId?: string; from?: string; text?: string };
|
|
14
|
+
* outbound: { text: string };
|
|
15
|
+
* };
|
|
16
|
+
* }
|
|
17
|
+
* }
|
|
18
|
+
* ```
|
|
19
|
+
*/
|
|
20
|
+
export interface RealtimeHandlerRegistry {
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Auto-populated by `base44 types generate` with the names of your deployed handlers.
|
|
24
|
+
* Do not edit this interface manually — use {@link RealtimeHandlerRegistry} for message types.
|
|
3
25
|
*/
|
|
4
|
-
export interface
|
|
5
|
-
/** Send a message to all subscribers of this instance. */
|
|
6
|
-
send(data: unknown): void;
|
|
7
|
-
/** Close the WebSocket connection and remove the subscription. */
|
|
8
|
-
close(): void;
|
|
26
|
+
export interface RealtimeHandlerNameRegistry {
|
|
9
27
|
}
|
|
28
|
+
type AllHandlerNames = keyof RealtimeHandlerRegistry | keyof RealtimeHandlerNameRegistry;
|
|
29
|
+
type InboundFor<N extends string> = N extends keyof RealtimeHandlerRegistry ? RealtimeHandlerRegistry[N] extends {
|
|
30
|
+
inbound: infer I;
|
|
31
|
+
} ? I : unknown : unknown;
|
|
32
|
+
type OutboundFor<N extends string> = N extends keyof RealtimeHandlerRegistry ? RealtimeHandlerRegistry[N] extends {
|
|
33
|
+
outbound: infer O;
|
|
34
|
+
} ? O : unknown : unknown;
|
|
10
35
|
/**
|
|
11
36
|
* Client for a single named RealtimeHandler.
|
|
37
|
+
* Typed automatically when the handler is registered in {@link RealtimeHandlerRegistry}.
|
|
12
38
|
*/
|
|
13
|
-
export interface RealtimeHandlerClient {
|
|
14
|
-
/**
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
* @param callback - Called with each parsed message payload.
|
|
19
|
-
* @returns A subscription handle with `send` and `close` methods.
|
|
20
|
-
*/
|
|
21
|
-
subscribe(instanceId: string, callback: (data: unknown) => void): Promise<RealtimeSubscription>;
|
|
22
|
-
/**
|
|
23
|
-
* Send a message to an existing active subscription.
|
|
24
|
-
*
|
|
25
|
-
* @param instanceId - The instance ID of the Durable Object.
|
|
26
|
-
* @param data - The data to send (will be JSON-serialized).
|
|
27
|
-
* @throws {Error} When no active subscription exists for this handler/instance pair.
|
|
28
|
-
*/
|
|
29
|
-
send(instanceId: string, data: unknown): void;
|
|
39
|
+
export interface RealtimeHandlerClient<N extends string = string> {
|
|
40
|
+
/** Open a WebSocket subscription. Returns a synchronous unsubscribe function. */
|
|
41
|
+
subscribe(instanceId: string, callback: (data: InboundFor<N>) => void): () => void;
|
|
42
|
+
/** Send a message over the open socket. Throws if not subscribed. */
|
|
43
|
+
send(instanceId: string, data: OutboundFor<N>): void;
|
|
30
44
|
}
|
|
31
45
|
/**
|
|
32
46
|
* The realtime module provides access to Cloudflare Durable Object-backed
|
|
@@ -35,10 +49,13 @@ export interface RealtimeHandlerClient {
|
|
|
35
49
|
* Handler names are accessed as dynamic properties on this module:
|
|
36
50
|
* ```typescript
|
|
37
51
|
* const sub = await base44.realtime.MyHandler.subscribe("room-1", (msg) => {
|
|
38
|
-
* console.log(msg);
|
|
52
|
+
* console.log(msg); // typed if MyHandler is in RealtimeHandlerRegistry
|
|
39
53
|
* });
|
|
40
54
|
* sub.send({ text: "hello" });
|
|
41
55
|
* sub.close();
|
|
42
56
|
* ```
|
|
43
57
|
*/
|
|
44
|
-
export type RealtimeModule =
|
|
58
|
+
export type RealtimeModule = {
|
|
59
|
+
[K in AllHandlerNames]: K extends keyof RealtimeHandlerRegistry ? RealtimeHandlerClient<string & K> : RealtimeHandlerClient;
|
|
60
|
+
} & Record<string, RealtimeHandlerClient>;
|
|
61
|
+
export {};
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Type-only base class for Realtime Handlers.
|
|
3
|
+
*
|
|
4
|
+
* Import and extend this in your handler files:
|
|
5
|
+
* import { RealtimeHandler } from "@base44/sdk";
|
|
6
|
+
* export class MyHandler extends RealtimeHandler { ... }
|
|
7
|
+
*
|
|
8
|
+
* At deploy time the bundler replaces this import with the compiled
|
|
9
|
+
* Cloudflare Durable Object implementation — this file provides types only.
|
|
10
|
+
*/
|
|
11
|
+
export interface Conn {
|
|
12
|
+
userId: string;
|
|
13
|
+
appId: string;
|
|
14
|
+
instanceId: string;
|
|
15
|
+
send(data: unknown): void;
|
|
16
|
+
reject(code: number, reason: string): void;
|
|
17
|
+
}
|
|
18
|
+
export declare abstract class RealtimeHandler<_State = unknown, Message = unknown> {
|
|
19
|
+
abstract handleConnect(conn: Conn): void | Promise<void>;
|
|
20
|
+
abstract handleMessage(conn: Conn, msg: Message): void | Promise<void>;
|
|
21
|
+
abstract handleClose(conn: Conn): void | Promise<void>;
|
|
22
|
+
abstract handleTick(): void | Promise<void>;
|
|
23
|
+
protected broadcast(_data: unknown): void;
|
|
24
|
+
protected getConnections(): Conn[];
|
|
25
|
+
protected startLoop(_ms: number): Promise<void>;
|
|
26
|
+
protected stopLoop(): Promise<void>;
|
|
27
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Type-only base class for Realtime Handlers.
|
|
3
|
+
*
|
|
4
|
+
* Import and extend this in your handler files:
|
|
5
|
+
* import { RealtimeHandler } from "@base44/sdk";
|
|
6
|
+
* export class MyHandler extends RealtimeHandler { ... }
|
|
7
|
+
*
|
|
8
|
+
* At deploy time the bundler replaces this import with the compiled
|
|
9
|
+
* Cloudflare Durable Object implementation — this file provides types only.
|
|
10
|
+
*/
|
|
11
|
+
export class RealtimeHandler {
|
|
12
|
+
broadcast(_data) {
|
|
13
|
+
throw new Error("RealtimeHandler.broadcast() is only available inside a deployed handler");
|
|
14
|
+
}
|
|
15
|
+
getConnections() {
|
|
16
|
+
throw new Error("RealtimeHandler.getConnections() is only available inside a deployed handler");
|
|
17
|
+
}
|
|
18
|
+
startLoop(_ms) {
|
|
19
|
+
throw new Error("RealtimeHandler.startLoop() is only available inside a deployed handler");
|
|
20
|
+
}
|
|
21
|
+
stopLoop() {
|
|
22
|
+
throw new Error("RealtimeHandler.stopLoop() is only available inside a deployed handler");
|
|
23
|
+
}
|
|
24
|
+
}
|