@base44-preview/platform 0.2.0-pr.335.2bd5f4f → 0.2.0-pr.337.2f0349e
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 +68 -35
- package/dist/react/chat-items.d.ts +6 -6
- package/dist/react/chat-items.js +79 -19
- package/dist/react/chat.types.d.ts +39 -4
- package/dist/react/index.d.ts +2 -6
- package/dist/react/index.js +1 -4
- package/dist/react/useBase44Chat.js +55 -15
- package/dist/server/errors.d.ts +53 -0
- package/dist/server/errors.js +56 -0
- package/dist/server/errors.types.d.ts +60 -0
- package/dist/server/index.d.ts +6 -2
- package/dist/server/index.js +2 -1
- package/dist/server/modules/webhooks-signature.d.ts +11 -0
- package/dist/server/modules/webhooks-signature.js +65 -0
- package/dist/server/modules/webhooks.d.ts +3 -0
- package/dist/server/modules/webhooks.events.generated.d.ts +235 -0
- package/dist/server/modules/webhooks.events.generated.js +4 -0
- package/dist/server/modules/webhooks.js +91 -0
- package/dist/server/modules/webhooks.types.d.ts +218 -0
- package/dist/server/modules/webhooks.types.js +1 -0
- package/dist/server/server.d.ts +28 -3
- package/dist/server/server.js +31 -7
- package/dist/server/server.types.d.ts +8 -8
- package/package.json +11 -5
- package/dist/react/Base44ChatProvider.d.ts +0 -43
- package/dist/react/Base44ChatProvider.js +0 -68
- package/dist/react/Message.d.ts +0 -39
- package/dist/react/Message.js +0 -55
- package/dist/react/components.types.d.ts +0 -63
- package/dist/react/context.d.ts +0 -50
- package/dist/react/context.js +0 -57
- package/dist/react/fallbacks.d.ts +0 -3
- package/dist/react/fallbacks.js +0 -13
- package/dist/server/server-url.d.ts +0 -2
- package/dist/server/server-url.js +0 -8
- /package/dist/{react/components.types.js → server/errors.types.js} +0 -0
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
import type { WebhookEventMap, WebhookTestV1Data } from "./webhooks.events.generated.js";
|
|
2
|
+
export type * from "./webhooks.events.generated.js";
|
|
3
|
+
/** Name of a webhook event, such as `app.deleted.v1`. The version is part of the name. */
|
|
4
|
+
export type WebhookEventType = keyof WebhookEventMap;
|
|
5
|
+
/** The CloudEvents 1.0 envelope every webhook arrives in. Its `data` depends on `type`. */
|
|
6
|
+
export interface CloudEvent<TType extends string, TData> {
|
|
7
|
+
/** CloudEvents version. Always `1.0`. */
|
|
8
|
+
specversion: "1.0";
|
|
9
|
+
/** Unique event ID, starting with `evt_`. A redelivery keeps the same ID, so use it to skip events you already handled. */
|
|
10
|
+
id: string;
|
|
11
|
+
/** URL of the workspace the event happened in. */
|
|
12
|
+
source: string;
|
|
13
|
+
/** Event type, including its version. */
|
|
14
|
+
type: TType;
|
|
15
|
+
/** The resource the event is about, such as `apps/<app ID>`. */
|
|
16
|
+
subject: string;
|
|
17
|
+
/** When the event happened, as an RFC 3339 timestamp in UTC. */
|
|
18
|
+
time: string;
|
|
19
|
+
/** Content type of `data`. Always `application/json`. */
|
|
20
|
+
datacontenttype: string;
|
|
21
|
+
/** URL of the JSON Schema that describes `data`. */
|
|
22
|
+
dataschema: string;
|
|
23
|
+
/** W3C trace context, when the event was produced inside a trace. */
|
|
24
|
+
traceparent?: string;
|
|
25
|
+
/** W3C trace state, when the event was produced inside a trace. */
|
|
26
|
+
tracestate?: string;
|
|
27
|
+
/** Kind of actor that caused the event. */
|
|
28
|
+
authtype: string;
|
|
29
|
+
/** ID of the actor that caused the event. */
|
|
30
|
+
authid: string;
|
|
31
|
+
/** The event's payload. Its shape depends on `type`. */
|
|
32
|
+
data: TData;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* A webhook event whose type this version of the SDK doesn't know, such as one Base44 added later.
|
|
36
|
+
*
|
|
37
|
+
* The envelope is the same as for every event, but `type` is `"unknown"`, the event's real type is
|
|
38
|
+
* in `name`, and `data` isn't checked, so check a field before using it.
|
|
39
|
+
*/
|
|
40
|
+
export interface UnknownWebhookEvent extends Omit<CloudEvent<string, Record<string, unknown>>, "type"> {
|
|
41
|
+
/** Always `"unknown"`. Narrows the event to this shape. */
|
|
42
|
+
type: "unknown";
|
|
43
|
+
/** The event's type as Base44 sent it, such as `app.renamed.v1`. */
|
|
44
|
+
name: string;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* A verified webhook event.
|
|
48
|
+
*
|
|
49
|
+
* Check `type` to narrow `data` to that event's payload. An event added to Base44 after this version
|
|
50
|
+
* of the SDK arrives with `type: "unknown"`, its type in `name` and an unchecked `data`, so check a
|
|
51
|
+
* field before using it. After you upgrade, it arrives under its own `type`. Answer it with a 2xx
|
|
52
|
+
* too: Base44 retries any answer that isn't 2xx.
|
|
53
|
+
*
|
|
54
|
+
* @example
|
|
55
|
+
* ```typescript
|
|
56
|
+
* // Narrow an event by type
|
|
57
|
+
* switch (event.type) {
|
|
58
|
+
* case 'app.deleted.v1':
|
|
59
|
+
* await removeApp(event.data.app_id);
|
|
60
|
+
* break;
|
|
61
|
+
* case 'unknown':
|
|
62
|
+
* console.log(`Skipping ${event.name}, which this SDK version doesn't know`);
|
|
63
|
+
* break;
|
|
64
|
+
* }
|
|
65
|
+
* ```
|
|
66
|
+
*/
|
|
67
|
+
export type WebhookEvent = {
|
|
68
|
+
[K in WebhookEventType]: CloudEvent<K, WebhookEventMap[K]>;
|
|
69
|
+
}[WebhookEventType] | UnknownWebhookEvent;
|
|
70
|
+
/** Request headers, as a fetch `Headers` object or a Node.js-style object. Header names are case-insensitive. */
|
|
71
|
+
export type WebhookHeaders = Headers | Record<string, string | string[] | undefined>;
|
|
72
|
+
/** One Ed25519 public key, starting with `whpk_`, or several in an array or separated by commas or whitespace. */
|
|
73
|
+
export type WebhookPublicKeys = string | string[];
|
|
74
|
+
/** Options for {@linkcode WebhooksModule.verifyEvent | verifyEvent()}. */
|
|
75
|
+
export interface VerifyEventOptions {
|
|
76
|
+
/**
|
|
77
|
+
* Your workspace's public keys.
|
|
78
|
+
*
|
|
79
|
+
* Pass both the old and the new key while a key rotation is in progress. Missing or malformed keys
|
|
80
|
+
* reject with `WebhookConfigurationError`.
|
|
81
|
+
*/
|
|
82
|
+
publicKeys: WebhookPublicKeys;
|
|
83
|
+
/** How far, in seconds, a delivery's timestamp can be from your server's clock. Defaults to `300`. */
|
|
84
|
+
toleranceSeconds?: number;
|
|
85
|
+
}
|
|
86
|
+
/** An event your callbacks receive. Every event except the `webhook.test.v1` activation probe, which the handler answers itself. */
|
|
87
|
+
export type DeliveredWebhookEvent = Exclude<WebhookEvent, CloudEvent<"webhook.test.v1", WebhookTestV1Data>>;
|
|
88
|
+
/**
|
|
89
|
+
* Handlers keyed by event type. Each handler receives the event narrowed to its type. The `unknown`
|
|
90
|
+
* key handles every event type this version of the SDK doesn't know.
|
|
91
|
+
*/
|
|
92
|
+
export type WebhookEventHandlers = {
|
|
93
|
+
[K in DeliveredWebhookEvent["type"]]?: (event: Extract<DeliveredWebhookEvent, {
|
|
94
|
+
type: K;
|
|
95
|
+
}>) => unknown;
|
|
96
|
+
};
|
|
97
|
+
/** Options for {@linkcode WebhooksModule.createHandler | createHandler()}. Every callback can be async. */
|
|
98
|
+
export interface CreateHandlerOptions extends VerifyEventOptions {
|
|
99
|
+
/** Handlers keyed by event type. Events without a handler, including unknown ones, are still answered 2xx, so Base44 doesn't retry them. */
|
|
100
|
+
on?: WebhookEventHandlers;
|
|
101
|
+
/** Called for every verified event, before its handler in `on`. */
|
|
102
|
+
onEvent?: (event: DeliveredWebhookEvent) => unknown;
|
|
103
|
+
/**
|
|
104
|
+
* Called after `onEvent` and the event's handler finish without throwing.
|
|
105
|
+
*
|
|
106
|
+
* Return a `Response` to replace the default empty 204.
|
|
107
|
+
*/
|
|
108
|
+
onSuccess?: (event: DeliveredWebhookEvent) => Response | void | Promise<Response | void>;
|
|
109
|
+
/**
|
|
110
|
+
* Called when a delivery fails.
|
|
111
|
+
*
|
|
112
|
+
* The handler answers a `WebhookVerificationError` with a 400, and a `WebhookConfigurationError`, a
|
|
113
|
+
* failed body read or anything a callback throws with a 500. Base44 retries either way, because it
|
|
114
|
+
* retries any answer that isn't 2xx. The status only shows in its delivery log whether the delivery
|
|
115
|
+
* or your side failed. The `event` argument is set when the delivery verified and a callback failed.
|
|
116
|
+
* Return a `Response` to replace the default answer.
|
|
117
|
+
*/
|
|
118
|
+
onError?: (error: unknown, event?: DeliveredWebhookEvent) => Response | void | Promise<Response | void>;
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Webhooks module for receiving the events Base44 sends to your server.
|
|
122
|
+
*
|
|
123
|
+
* Base44 sends a signed event to your endpoint when something happens in your workspace:
|
|
124
|
+
* - An app is created, published, unpublished, moved to trash or restored.
|
|
125
|
+
* - Base44 tests a new endpoint before activating it.
|
|
126
|
+
*
|
|
127
|
+
* The module verifies each delivery's signature with your workspace's public keys, and gives you
|
|
128
|
+
* the event with its payload typed. It runs on Node.js 20 and later and on edge runtimes, and makes
|
|
129
|
+
* no network requests.
|
|
130
|
+
*
|
|
131
|
+
* Use {@linkcode createHandler | createHandler()} for a ready-made route handler, or
|
|
132
|
+
* {@linkcode verifyEvent | verifyEvent()} to handle the request yourself. Base44 delivers each event at
|
|
133
|
+
* least once and in no guaranteed order, so skip events whose `id` you already handled.
|
|
134
|
+
*/
|
|
135
|
+
export interface WebhooksModule {
|
|
136
|
+
/**
|
|
137
|
+
* Verifies a delivery and returns its event.
|
|
138
|
+
*
|
|
139
|
+
* Pass the request body exactly as received, before any JSON parsing, because the signature covers
|
|
140
|
+
* its exact bytes. Nothing is parsed until the signature checks out.
|
|
141
|
+
*
|
|
142
|
+
* To activate a new endpoint, answer the `webhook.test.v1` event with
|
|
143
|
+
* `{ "challenge": event.data.challenge }` within 5 seconds. Answer every other event with a 2xx.
|
|
144
|
+
*
|
|
145
|
+
* @param body - The raw request body, as text or bytes.
|
|
146
|
+
* @param headers - The request headers.
|
|
147
|
+
* @param options - Verification options, including your public keys.
|
|
148
|
+
* @returns Promise resolving to the verified event.
|
|
149
|
+
* @throws {WebhookVerificationError} When the delivery can't be trusted. Answer with a 400.
|
|
150
|
+
* @throws {WebhookConfigurationError} When the public keys are missing or malformed, or the runtime can't use them. Answer with a 500.
|
|
151
|
+
*
|
|
152
|
+
* @example
|
|
153
|
+
* ```typescript
|
|
154
|
+
* // Verify the request and handle each event type
|
|
155
|
+
* export async function POST(request) {
|
|
156
|
+
* const rawBody = await request.text();
|
|
157
|
+
* let event;
|
|
158
|
+
* try {
|
|
159
|
+
* event = await platform.webhooks.verifyEvent(rawBody, request.headers, {
|
|
160
|
+
* publicKeys: process.env.BASE44_WEBHOOK_PUBLIC_KEYS,
|
|
161
|
+
* });
|
|
162
|
+
* } catch (error) {
|
|
163
|
+
* if (error instanceof WebhookVerificationError) return new Response(null, { status: 400 });
|
|
164
|
+
* throw error;
|
|
165
|
+
* }
|
|
166
|
+
*
|
|
167
|
+
* switch (event.type) {
|
|
168
|
+
* case 'webhook.test.v1':
|
|
169
|
+
* return Response.json({ challenge: event.data.challenge });
|
|
170
|
+
* case 'app.deleted.v1':
|
|
171
|
+
* await removeApp(event.data.app_id);
|
|
172
|
+
* break;
|
|
173
|
+
* }
|
|
174
|
+
* return new Response(null, { status: 204 });
|
|
175
|
+
* }
|
|
176
|
+
* ```
|
|
177
|
+
*/
|
|
178
|
+
verifyEvent(body: string | Uint8Array, headers: WebhookHeaders, options: VerifyEventOptions): Promise<WebhookEvent>;
|
|
179
|
+
/**
|
|
180
|
+
* Creates a route handler that verifies deliveries and calls your handlers.
|
|
181
|
+
*
|
|
182
|
+
* The handler takes a fetch `Request` and returns a `Response`, so it works as a Next.js route
|
|
183
|
+
* handler and in Hono, Cloudflare Workers, Deno and Bun. For each request it reads the raw body,
|
|
184
|
+
* verifies it, answers the activation probe itself, and then calls `onEvent`, the event's handler in
|
|
185
|
+
* `on`, and `onSuccess`, in that order.
|
|
186
|
+
*
|
|
187
|
+
* It answers with:
|
|
188
|
+
* - 204 when the event was handled, including events with no handler and unknown events.
|
|
189
|
+
* - 400 when the delivery doesn't verify, with the error code in the body.
|
|
190
|
+
* - 500 when the public keys are misconfigured (`receiver_misconfigured`), the body can't be read
|
|
191
|
+
* (`request_failed`) or a callback throws (`handler_failed`).
|
|
192
|
+
*
|
|
193
|
+
* Base44 retries any answer that isn't 2xx, 400 and 500 alike. The status only shows in its delivery
|
|
194
|
+
* log whether the delivery or your side failed.
|
|
195
|
+
*
|
|
196
|
+
* Base44 can deliver the same event more than once, so make your handlers safe to run twice for the
|
|
197
|
+
* same `event.id`.
|
|
198
|
+
*
|
|
199
|
+
* @param options - Your public keys, handlers and callbacks.
|
|
200
|
+
* @returns A function that takes a `Request` and resolves to the `Response` to send.
|
|
201
|
+
*
|
|
202
|
+
* @example
|
|
203
|
+
* ```typescript
|
|
204
|
+
* // Handle app events in a Next.js route
|
|
205
|
+
* export const POST = platform.webhooks.createHandler({
|
|
206
|
+
* publicKeys: process.env.BASE44_WEBHOOK_PUBLIC_KEYS,
|
|
207
|
+
* on: {
|
|
208
|
+
* 'app.deleted.v1': async (event) => removeApp(event.data.app_id),
|
|
209
|
+
* 'app.published.v1': async (event) => refreshApp(event.data.app_id),
|
|
210
|
+
* },
|
|
211
|
+
* onError(error, event) {
|
|
212
|
+
* console.error('Webhook failed', event?.id, error);
|
|
213
|
+
* },
|
|
214
|
+
* });
|
|
215
|
+
* ```
|
|
216
|
+
*/
|
|
217
|
+
createHandler(options: CreateHandlerOptions): (request: Request) => Promise<Response>;
|
|
218
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/server/server.d.ts
CHANGED
|
@@ -1,3 +1,28 @@
|
|
|
1
|
-
import type { PlatformServer
|
|
2
|
-
/**
|
|
3
|
-
|
|
1
|
+
import type { PlatformServer } from "./server.types.js";
|
|
2
|
+
/**
|
|
3
|
+
* Creates a platform server client.
|
|
4
|
+
*
|
|
5
|
+
* This is the entry point of the Platform SDK on your backend. Import it from
|
|
6
|
+
* `@base44/platform/server`. The client gives your server access to the platform's server modules,
|
|
7
|
+
* such as [`webhooks`](/developers/references/platform-sdk/docs/interfaces/webhooks). It takes no
|
|
8
|
+
* options and makes no network requests. Each module takes what it needs when you call it.
|
|
9
|
+
*
|
|
10
|
+
* @returns A server client with access to the platform's server modules.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```typescript
|
|
14
|
+
* // Create a server client on your backend
|
|
15
|
+
* import { createPlatformServer } from '@base44/platform/server';
|
|
16
|
+
*
|
|
17
|
+
* const platform = createPlatformServer();
|
|
18
|
+
*
|
|
19
|
+
* // Use the client to receive webhooks
|
|
20
|
+
* export const POST = platform.webhooks.createHandler({
|
|
21
|
+
* publicKeys: process.env.BASE44_WEBHOOK_PUBLIC_KEYS,
|
|
22
|
+
* on: {
|
|
23
|
+
* 'app.deleted.v1': async (event) => removeApp(event.data.app_id),
|
|
24
|
+
* },
|
|
25
|
+
* });
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
28
|
+
export declare function createPlatformServer(): PlatformServer;
|
package/dist/server/server.js
CHANGED
|
@@ -1,8 +1,32 @@
|
|
|
1
|
-
import {
|
|
2
|
-
/**
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
1
|
+
import { createWebhooks } from "./modules/webhooks.js";
|
|
2
|
+
/**
|
|
3
|
+
* Creates a platform server client.
|
|
4
|
+
*
|
|
5
|
+
* This is the entry point of the Platform SDK on your backend. Import it from
|
|
6
|
+
* `@base44/platform/server`. The client gives your server access to the platform's server modules,
|
|
7
|
+
* such as [`webhooks`](/developers/references/platform-sdk/docs/interfaces/webhooks). It takes no
|
|
8
|
+
* options and makes no network requests. Each module takes what it needs when you call it.
|
|
9
|
+
*
|
|
10
|
+
* @returns A server client with access to the platform's server modules.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```typescript
|
|
14
|
+
* // Create a server client on your backend
|
|
15
|
+
* import { createPlatformServer } from '@base44/platform/server';
|
|
16
|
+
*
|
|
17
|
+
* const platform = createPlatformServer();
|
|
18
|
+
*
|
|
19
|
+
* // Use the client to receive webhooks
|
|
20
|
+
* export const POST = platform.webhooks.createHandler({
|
|
21
|
+
* publicKeys: process.env.BASE44_WEBHOOK_PUBLIC_KEYS,
|
|
22
|
+
* on: {
|
|
23
|
+
* 'app.deleted.v1': async (event) => removeApp(event.data.app_id),
|
|
24
|
+
* },
|
|
25
|
+
* });
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
28
|
+
export function createPlatformServer() {
|
|
29
|
+
return Object.freeze({
|
|
30
|
+
webhooks: Object.freeze(createWebhooks()),
|
|
31
|
+
});
|
|
8
32
|
}
|
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
}
|
|
8
|
-
/** Server platform client, returned by `createPlatformServer`. Modules are added as properties. */
|
|
1
|
+
import type { WebhooksModule } from "./modules/webhooks.types.js";
|
|
2
|
+
/**
|
|
3
|
+
* The platform server client.
|
|
4
|
+
*
|
|
5
|
+
* Provides access to the platform's server modules.
|
|
6
|
+
*/
|
|
9
7
|
export interface PlatformServer {
|
|
8
|
+
/** [Webhooks module](/developers/references/platform-sdk/docs/interfaces/webhooks) for receiving the events Base44 sends to your server. */
|
|
9
|
+
readonly webhooks: WebhooksModule;
|
|
10
10
|
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@base44-preview/platform",
|
|
3
|
-
"version": "0.2.0-pr.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "0.2.0-pr.337.2f0349e",
|
|
4
|
+
"description": "Base44 Platform SDK for white-label platforms: a browser client for watching the builder and a server entrypoint for webhooks",
|
|
5
5
|
"main": "dist/client/index.js",
|
|
6
6
|
"types": "dist/client/index.d.ts",
|
|
7
7
|
"type": "module",
|
|
@@ -51,7 +51,9 @@
|
|
|
51
51
|
"create-docs-local": "npm run create-docs && npm run copy-docs-local",
|
|
52
52
|
"copy-docs-local": "node ../sdk/scripts/mintlify-post-processing/copy-to-local-docs.js --package-dir .",
|
|
53
53
|
"create-docs:generate": "node -e \"fs.rmSync('docs',{recursive:true,force:true})\" && typedoc --options typedoc.mintlify.json",
|
|
54
|
-
"create-docs:process": "node ../sdk/scripts/mintlify-post-processing/file-processing/file-processing.js --package-dir ."
|
|
54
|
+
"create-docs:process": "node ../sdk/scripts/mintlify-post-processing/file-processing/file-processing.js --package-dir .",
|
|
55
|
+
"generate:webhooks": "node scripts/generate-webhook-types.mjs",
|
|
56
|
+
"sync:webhooks": "node scripts/sync-webhook-event-types.mjs"
|
|
55
57
|
},
|
|
56
58
|
"dependencies": {
|
|
57
59
|
"socket.io-client": "^4.8.3"
|
|
@@ -75,7 +77,8 @@
|
|
|
75
77
|
"keywords": [
|
|
76
78
|
"base44",
|
|
77
79
|
"platform",
|
|
78
|
-
"white-label"
|
|
80
|
+
"white-label",
|
|
81
|
+
"webhooks"
|
|
79
82
|
],
|
|
80
83
|
"author": "Base44",
|
|
81
84
|
"license": "MIT",
|
|
@@ -87,5 +90,8 @@
|
|
|
87
90
|
"bugs": {
|
|
88
91
|
"url": "https://github.com/base44/javascript-sdk/issues"
|
|
89
92
|
},
|
|
90
|
-
"homepage": "https://github.com/base44/javascript-sdk/tree/main/packages/platform#readme"
|
|
93
|
+
"homepage": "https://github.com/base44/javascript-sdk/tree/main/packages/platform#readme",
|
|
94
|
+
"engines": {
|
|
95
|
+
"node": ">=20"
|
|
96
|
+
}
|
|
91
97
|
}
|
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
import type { Base44ChatProviderProps } from "./components.types.js";
|
|
2
|
-
/**
|
|
3
|
-
* Runs the AI chat for one app for the tree below it. It calls {@link useBase44Chat} once and
|
|
4
|
-
* shares the result through context, so any component below reads it with {@link useChatState}
|
|
5
|
-
* and {@link useChatActions}, and {@link Message} draws an item with your parts.
|
|
6
|
-
*
|
|
7
|
-
* Put it as high as the components that need the chat: a header that shows the status, a preview
|
|
8
|
-
* that waits for the build, and the message list all read the same chat. It renders nothing of its
|
|
9
|
-
* own.
|
|
10
|
-
*
|
|
11
|
-
* You pass a part for every question kind. The message parts, `Text` and `Step`, are optional:
|
|
12
|
-
* one left out is drawn as plain HTML with no classes and a `data-base44` attribute for CSS.
|
|
13
|
-
*
|
|
14
|
-
* @param props - The {@link useBase44Chat} options, the parts, and the children.
|
|
15
|
-
* @returns The children, inside the chat's contexts.
|
|
16
|
-
*
|
|
17
|
-
* @example
|
|
18
|
-
* ```tsx
|
|
19
|
-
* // The provider, a message list, and a composer of your own
|
|
20
|
-
* <Base44ChatProvider appId={appId} backend={backend} onAppCreated={selectApp} components={{ question: { Choice, Secrets, Approval } }}>
|
|
21
|
-
* <Messages />
|
|
22
|
-
* <Composer />
|
|
23
|
-
* </Base44ChatProvider>
|
|
24
|
-
*
|
|
25
|
-
* function Messages() {
|
|
26
|
-
* const { items } = useChatState();
|
|
27
|
-
* return items.map((item) => <div key={item.id} className={item.role}><Message item={item} /></div>);
|
|
28
|
-
* }
|
|
29
|
-
*
|
|
30
|
-
* function Composer() {
|
|
31
|
-
* const { status } = useChatState();
|
|
32
|
-
* const { send } = useChatActions();
|
|
33
|
-
* const [prompt, setPrompt] = useState("");
|
|
34
|
-
* return (
|
|
35
|
-
* <form onSubmit={(e) => { e.preventDefault(); send(prompt); setPrompt(""); }}>
|
|
36
|
-
* <input value={prompt} onChange={(e) => setPrompt(e.target.value)} />
|
|
37
|
-
* <button disabled={status === "submitted" || status === "waiting"}>Send</button>
|
|
38
|
-
* </form>
|
|
39
|
-
* );
|
|
40
|
-
* }
|
|
41
|
-
* ```
|
|
42
|
-
*/
|
|
43
|
-
export declare function Base44ChatProvider({ components, children, appId, backend, onAppCreated }: Base44ChatProviderProps): import("react").JSX.Element;
|
|
@@ -1,68 +0,0 @@
|
|
|
1
|
-
"use client";
|
|
2
|
-
import { jsx as _jsx } from "react/jsx-runtime";
|
|
3
|
-
import { useCallback, useEffect, useMemo, useRef } from "react";
|
|
4
|
-
import { ActionsContext, ComponentsContext, StateContext } from "./context.js";
|
|
5
|
-
import { fallbackMessageComponents } from "./fallbacks.js";
|
|
6
|
-
import { useBase44Chat } from "./useBase44Chat.js";
|
|
7
|
-
/**
|
|
8
|
-
* Runs the AI chat for one app for the tree below it. It calls {@link useBase44Chat} once and
|
|
9
|
-
* shares the result through context, so any component below reads it with {@link useChatState}
|
|
10
|
-
* and {@link useChatActions}, and {@link Message} draws an item with your parts.
|
|
11
|
-
*
|
|
12
|
-
* Put it as high as the components that need the chat: a header that shows the status, a preview
|
|
13
|
-
* that waits for the build, and the message list all read the same chat. It renders nothing of its
|
|
14
|
-
* own.
|
|
15
|
-
*
|
|
16
|
-
* You pass a part for every question kind. The message parts, `Text` and `Step`, are optional:
|
|
17
|
-
* one left out is drawn as plain HTML with no classes and a `data-base44` attribute for CSS.
|
|
18
|
-
*
|
|
19
|
-
* @param props - The {@link useBase44Chat} options, the parts, and the children.
|
|
20
|
-
* @returns The children, inside the chat's contexts.
|
|
21
|
-
*
|
|
22
|
-
* @example
|
|
23
|
-
* ```tsx
|
|
24
|
-
* // The provider, a message list, and a composer of your own
|
|
25
|
-
* <Base44ChatProvider appId={appId} backend={backend} onAppCreated={selectApp} components={{ question: { Choice, Secrets, Approval } }}>
|
|
26
|
-
* <Messages />
|
|
27
|
-
* <Composer />
|
|
28
|
-
* </Base44ChatProvider>
|
|
29
|
-
*
|
|
30
|
-
* function Messages() {
|
|
31
|
-
* const { items } = useChatState();
|
|
32
|
-
* return items.map((item) => <div key={item.id} className={item.role}><Message item={item} /></div>);
|
|
33
|
-
* }
|
|
34
|
-
*
|
|
35
|
-
* function Composer() {
|
|
36
|
-
* const { status } = useChatState();
|
|
37
|
-
* const { send } = useChatActions();
|
|
38
|
-
* const [prompt, setPrompt] = useState("");
|
|
39
|
-
* return (
|
|
40
|
-
* <form onSubmit={(e) => { e.preventDefault(); send(prompt); setPrompt(""); }}>
|
|
41
|
-
* <input value={prompt} onChange={(e) => setPrompt(e.target.value)} />
|
|
42
|
-
* <button disabled={status === "submitted" || status === "waiting"}>Send</button>
|
|
43
|
-
* </form>
|
|
44
|
-
* );
|
|
45
|
-
* }
|
|
46
|
-
* ```
|
|
47
|
-
*/
|
|
48
|
-
export function Base44ChatProvider({ components, children, appId, backend, onAppCreated }) {
|
|
49
|
-
var _a;
|
|
50
|
-
// The hook reads the backend through a ref already. The callback goes through one here, so an
|
|
51
|
-
// inline `onAppCreated` keeps `create`, and every reader of the actions, stable.
|
|
52
|
-
const onAppCreatedRef = useRef(onAppCreated);
|
|
53
|
-
useEffect(() => {
|
|
54
|
-
onAppCreatedRef.current = onAppCreated;
|
|
55
|
-
});
|
|
56
|
-
const reportAppCreated = useCallback((app) => { var _a; return (_a = onAppCreatedRef.current) === null || _a === void 0 ? void 0 : _a.call(onAppCreatedRef, app); }, []);
|
|
57
|
-
const { items, status, connection, error, send, create, clearError } = useBase44Chat({ appId, backend, onAppCreated: reportAppCreated });
|
|
58
|
-
const state = useMemo(() => ({ items, status, connection, error }), [items, status, connection, error]);
|
|
59
|
-
const actions = useMemo(() => ({ send, create, clearError }), [send, create, clearError]);
|
|
60
|
-
// Merged part by part, so an inline `components={{…}}` with the same parts keeps the same identity.
|
|
61
|
-
const { Text, Step } = (_a = components.message) !== null && _a !== void 0 ? _a : {};
|
|
62
|
-
const { Choice, Secrets, Approval } = components.question;
|
|
63
|
-
const parts = useMemo(() => ({
|
|
64
|
-
message: { Text: Text !== null && Text !== void 0 ? Text : fallbackMessageComponents.Text, Step: Step !== null && Step !== void 0 ? Step : fallbackMessageComponents.Step },
|
|
65
|
-
question: { Choice, Secrets, Approval },
|
|
66
|
-
}), [Text, Step, Choice, Secrets, Approval]);
|
|
67
|
-
return (_jsx(ComponentsContext.Provider, { value: parts, children: _jsx(ActionsContext.Provider, { value: actions, children: _jsx(StateContext.Provider, { value: state, children: children }) }) }));
|
|
68
|
-
}
|
package/dist/react/Message.d.ts
DELETED
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
import type { MessageProps, QuestionProps } from "./components.types.js";
|
|
2
|
-
/**
|
|
3
|
-
* Draws one chat item with the parts from the nearest {@link Base44ChatProvider}, always in the
|
|
4
|
-
* same order: its text, then one `Step` per tool call, each followed by its open question, if it has
|
|
5
|
-
* one. Only the last assistant message carries questions.
|
|
6
|
-
*
|
|
7
|
-
* It adds no element of its own, so wrap it to lay messages out.
|
|
8
|
-
*
|
|
9
|
-
* @param props - The item to draw.
|
|
10
|
-
* @returns The item's parts.
|
|
11
|
-
* @throws {Error} When there is no provider above the component.
|
|
12
|
-
*
|
|
13
|
-
* @example
|
|
14
|
-
* ```tsx
|
|
15
|
-
* // Align by role in your own wrapper
|
|
16
|
-
* <div className={item.role === "user" ? "ml-auto" : ""}>
|
|
17
|
-
* <Message item={item} />
|
|
18
|
-
* </div>
|
|
19
|
-
* ```
|
|
20
|
-
*/
|
|
21
|
-
export declare function Message({ item }: MessageProps): import("react").JSX.Element;
|
|
22
|
-
/**
|
|
23
|
-
* Draws an open question with the part for its kind, from the nearest {@link Base44ChatProvider}.
|
|
24
|
-
* The part receives the question with its bound actions. {@link Message} already draws a message's
|
|
25
|
-
* questions; use this to show one somewhere else.
|
|
26
|
-
*
|
|
27
|
-
* @param props - The question to draw.
|
|
28
|
-
* @returns The part for the question's kind.
|
|
29
|
-
* @throws {Error} When there is no provider above the component.
|
|
30
|
-
*
|
|
31
|
-
* @example
|
|
32
|
-
* ```tsx
|
|
33
|
-
* // The open questions, pinned above the composer
|
|
34
|
-
* const { items } = useChatState();
|
|
35
|
-
* const open = items.flatMap((item) => item.steps.flatMap((step) => (step.question ? [step.question] : [])));
|
|
36
|
-
* return open.map((question, i) => <Question key={i} question={question} />);
|
|
37
|
-
* ```
|
|
38
|
-
*/
|
|
39
|
-
export declare function Question({ question }: QuestionProps): import("react").JSX.Element;
|
package/dist/react/Message.js
DELETED
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
"use client";
|
|
2
|
-
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
|
|
3
|
-
import { Fragment } from "react";
|
|
4
|
-
import { useResolvedComponents } from "./context.js";
|
|
5
|
-
/**
|
|
6
|
-
* Draws one chat item with the parts from the nearest {@link Base44ChatProvider}, always in the
|
|
7
|
-
* same order: its text, then one `Step` per tool call, each followed by its open question, if it has
|
|
8
|
-
* one. Only the last assistant message carries questions.
|
|
9
|
-
*
|
|
10
|
-
* It adds no element of its own, so wrap it to lay messages out.
|
|
11
|
-
*
|
|
12
|
-
* @param props - The item to draw.
|
|
13
|
-
* @returns The item's parts.
|
|
14
|
-
* @throws {Error} When there is no provider above the component.
|
|
15
|
-
*
|
|
16
|
-
* @example
|
|
17
|
-
* ```tsx
|
|
18
|
-
* // Align by role in your own wrapper
|
|
19
|
-
* <div className={item.role === "user" ? "ml-auto" : ""}>
|
|
20
|
-
* <Message item={item} />
|
|
21
|
-
* </div>
|
|
22
|
-
* ```
|
|
23
|
-
*/
|
|
24
|
-
export function Message({ item }) {
|
|
25
|
-
const { Text, Step } = useResolvedComponents("Message").message;
|
|
26
|
-
return (_jsxs(_Fragment, { children: [item.text && _jsx(Text, { text: item.text, role: item.role, pending: item.pending }), item.steps.map(step => (_jsxs(Fragment, { children: [_jsx(Step, { ...step }), step.question && _jsx(Question, { question: step.question })] }, step.id)))] }));
|
|
27
|
-
}
|
|
28
|
-
/**
|
|
29
|
-
* Draws an open question with the part for its kind, from the nearest {@link Base44ChatProvider}.
|
|
30
|
-
* The part receives the question with its bound actions. {@link Message} already draws a message's
|
|
31
|
-
* questions; use this to show one somewhere else.
|
|
32
|
-
*
|
|
33
|
-
* @param props - The question to draw.
|
|
34
|
-
* @returns The part for the question's kind.
|
|
35
|
-
* @throws {Error} When there is no provider above the component.
|
|
36
|
-
*
|
|
37
|
-
* @example
|
|
38
|
-
* ```tsx
|
|
39
|
-
* // The open questions, pinned above the composer
|
|
40
|
-
* const { items } = useChatState();
|
|
41
|
-
* const open = items.flatMap((item) => item.steps.flatMap((step) => (step.question ? [step.question] : [])));
|
|
42
|
-
* return open.map((question, i) => <Question key={i} question={question} />);
|
|
43
|
-
* ```
|
|
44
|
-
*/
|
|
45
|
-
export function Question({ question }) {
|
|
46
|
-
const { Choice, Secrets, Approval } = useResolvedComponents("Question").question;
|
|
47
|
-
switch (question.kind) {
|
|
48
|
-
case "choice":
|
|
49
|
-
return _jsx(Choice, { ...question });
|
|
50
|
-
case "secrets":
|
|
51
|
-
return _jsx(Secrets, { ...question });
|
|
52
|
-
case "approval":
|
|
53
|
-
return _jsx(Approval, { ...question });
|
|
54
|
-
}
|
|
55
|
-
}
|
|
@@ -1,63 +0,0 @@
|
|
|
1
|
-
import type { ComponentType, ReactNode } from "react";
|
|
2
|
-
import type { ApprovalQuestion, Base44Chat, Base44ChatOptions, ChatItem, ChatQuestion, ChatStep, ChoiceQuestion, SecretsQuestion } from "./chat.types.js";
|
|
3
|
-
/** Props of the `Text` part: one message's text. */
|
|
4
|
-
export interface TextProps {
|
|
5
|
-
/** The message text, as raw markdown. Never empty: an item without text draws no `Text`. */
|
|
6
|
-
text: string;
|
|
7
|
-
/** Who wrote the message, to align or color it. */
|
|
8
|
-
role: ChatItem["role"];
|
|
9
|
-
/** Whether this is a prompt Base44 hasn't confirmed yet. */
|
|
10
|
-
pending: boolean;
|
|
11
|
-
}
|
|
12
|
-
/** Props of the `Step` part: one tool call of a message. */
|
|
13
|
-
export type StepProps = ChatStep;
|
|
14
|
-
/** The parts of a message's own content. Each one left out is drawn as plain, unstyled HTML. */
|
|
15
|
-
export interface MessageComponents {
|
|
16
|
-
/** Draws a message's text. */
|
|
17
|
-
Text: ComponentType<TextProps>;
|
|
18
|
-
/** Draws one tool call as a line of activity. */
|
|
19
|
-
Step: ComponentType<StepProps>;
|
|
20
|
-
}
|
|
21
|
-
/**
|
|
22
|
-
* One part per question kind, all required. Each receives the question itself: its content and the
|
|
23
|
-
* actions bound to it, so a part can only answer the question it draws.
|
|
24
|
-
*
|
|
25
|
-
* A release that adds a kind adds a part here, so upgrading tells you at compile time which part to
|
|
26
|
-
* write. Until you upgrade, the chat declines the new kind for you.
|
|
27
|
-
*/
|
|
28
|
-
export interface QuestionComponents {
|
|
29
|
-
/** Draws a pick between options. Call `answer` with one list of labels per question. */
|
|
30
|
-
Choice: ComponentType<ChoiceQuestion>;
|
|
31
|
-
/** Draws a form for values such as API keys. Call `submit` with the values keyed by field name. */
|
|
32
|
-
Secrets: ComponentType<SecretsQuestion>;
|
|
33
|
-
/** Draws a request to run an action. Call `approve` or `decline`. */
|
|
34
|
-
Approval: ComponentType<ApprovalQuestion>;
|
|
35
|
-
}
|
|
36
|
-
/** The parts you pass to {@link Base44ChatProvider}: every question part, and the message parts you replace. */
|
|
37
|
-
export interface ChatComponents {
|
|
38
|
-
/** Replacements for the message parts. */
|
|
39
|
-
message?: Partial<MessageComponents>;
|
|
40
|
-
/** A part for every question kind. */
|
|
41
|
-
question: QuestionComponents;
|
|
42
|
-
}
|
|
43
|
-
/** What {@link useChatState} returns: everything that changes as the chat runs. */
|
|
44
|
-
export type ChatState = Pick<Base44Chat, "items" | "status" | "connection" | "error">;
|
|
45
|
-
/** What {@link useChatActions} returns. The actions keep their identity while the app stays the same. */
|
|
46
|
-
export type ChatActions = Pick<Base44Chat, "send" | "create" | "clearError">;
|
|
47
|
-
/** Props of {@link Base44ChatProvider}: the {@link useBase44Chat} options, the parts, and the tree that reads them. */
|
|
48
|
-
export interface Base44ChatProviderProps extends Base44ChatOptions {
|
|
49
|
-
/** The parts that draw the chat. Read part by part, so an inline object works. */
|
|
50
|
-
components: ChatComponents;
|
|
51
|
-
/** The components that read the chat. */
|
|
52
|
-
children?: ReactNode;
|
|
53
|
-
}
|
|
54
|
-
/** Props of {@link Message}. */
|
|
55
|
-
export interface MessageProps {
|
|
56
|
-
/** The item to draw, one of the `items` from {@link useChatState}. */
|
|
57
|
-
item: ChatItem;
|
|
58
|
-
}
|
|
59
|
-
/** Props of {@link Question}. */
|
|
60
|
-
export interface QuestionProps {
|
|
61
|
-
/** The open question to draw, from {@link ChatStep.question}. */
|
|
62
|
-
question: ChatQuestion;
|
|
63
|
-
}
|