@astralbeam/sdk 0.0.1 → 0.0.2
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 +152 -4
- package/dist/chat-DdU0uj9s.js +65 -0
- package/dist/client-utils-DyEcBInP.js +1 -0
- package/dist/client.d.ts +127 -2
- package/dist/client.js +1 -4
- package/dist/react.d.ts +45 -2
- package/dist/react.js +91 -3
- package/dist/server.d.ts +31 -2
- package/dist/server.js +692 -2
- package/package.json +29 -5
|
@@ -0,0 +1 @@
|
|
|
1
|
+
const e=`AstralBeam`,t=`/api/chat`,n=`system`,r=`astralbeam-root`,i=e=>`background:${e};color:#fff;border-radius:3px;padding:1px 5px`,a=`${i(`#7c3aed`)};font-weight:600`,o={mount:`#7c3aed`,auth:`#be185d`,theme:`#8b5cf6`,send:`#2563eb`,run:`#0891b2`,stream:`#0e7490`,text:`#16a34a`,reasoning:`#64748b`,tool:`#d97706`,widget:`#db2777`,attachment:`#0d9488`,questionnaire:`#9333ea`,status:`#475569`,error:`#dc2626`};function s(e){if(e)return(e,t,n)=>{console.log(`%cAstralBeam%c ${new Date().toISOString().slice(11,19)} %c${e}%c ${t}`,a,`color:#94a3b8;font-weight:400`,i(o[e]),``,...n===void 0?[]:[n])}}export{r as a,e as i,n,t as r,s as t};
|
package/dist/client.d.ts
CHANGED
|
@@ -1,4 +1,129 @@
|
|
|
1
|
+
//#region src/lib/client-types.d.ts
|
|
2
|
+
interface StandardSchemaV1 {
|
|
3
|
+
readonly "~standard": {
|
|
4
|
+
readonly version: 1;
|
|
5
|
+
readonly vendor: string;
|
|
6
|
+
readonly validate: (value: unknown) => unknown;
|
|
7
|
+
};
|
|
8
|
+
}
|
|
9
|
+
/** A plain JSON Schema object, the same shape tool definitions use for their parameters. */
|
|
10
|
+
interface JsonSchemaObject {
|
|
11
|
+
type: "object";
|
|
12
|
+
properties?: Record<string, unknown>;
|
|
13
|
+
required?: string[];
|
|
14
|
+
[keyword: string]: unknown;
|
|
15
|
+
}
|
|
16
|
+
/** Schema of the input the agent supplies to a widget or tool, like a tool definition's parameters. */
|
|
17
|
+
type ParametersSchema = StandardSchemaV1 | JsonSchemaObject;
|
|
18
|
+
interface WidgetDefinition {
|
|
19
|
+
/** Tells the agent what the widget shows so it can decide when to render it. */
|
|
20
|
+
description: string;
|
|
21
|
+
/**
|
|
22
|
+
* Forwarded to the agent as JSON Schema. Only a Standard Schema also validates the
|
|
23
|
+
* props before `render` runs; with a plain JSON Schema, treat the props as untrusted.
|
|
24
|
+
*/
|
|
25
|
+
parameters?: ParametersSchema;
|
|
26
|
+
/**
|
|
27
|
+
* Draws the widget with the agent-chosen props into `container`, a light-DOM child of
|
|
28
|
+
* the mount target. May return a cleanup, called before a re-render and on unmount.
|
|
29
|
+
*/
|
|
30
|
+
render: (props: Record<string, unknown>, container: HTMLElement) => (() => void) | void;
|
|
31
|
+
}
|
|
32
|
+
interface ToolDefinition {
|
|
33
|
+
/** Tells the agent what the tool does so it can decide when to call it. */
|
|
34
|
+
description: string;
|
|
35
|
+
/**
|
|
36
|
+
* Forwarded to the agent as JSON Schema. Only a Standard Schema also validates the
|
|
37
|
+
* input before `execute` runs; with a plain JSON Schema, treat the input as untrusted.
|
|
38
|
+
*/
|
|
39
|
+
parameters?: ParametersSchema;
|
|
40
|
+
/**
|
|
41
|
+
* Runs in the host page with the agent-chosen input. The resolved value is returned
|
|
42
|
+
* to the agent as the tool result; a thrown error is returned as a tool error.
|
|
43
|
+
*/
|
|
44
|
+
execute: (input: Record<string, unknown>) => unknown | Promise<unknown>;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Limits and accepted types for the composer's file attachments. Every field is optional;
|
|
48
|
+
* omitting the whole option leaves attachments enabled with the defaults below.
|
|
49
|
+
*/
|
|
50
|
+
interface AstralBeamChatAttachmentOptions {
|
|
51
|
+
/** Hides the attach button (and ignores drops and pastes) when `false`. Default `true`. */
|
|
52
|
+
enabled?: boolean | undefined;
|
|
53
|
+
/** How many files one message may carry. Default `5`. */
|
|
54
|
+
maxFiles?: number | undefined;
|
|
55
|
+
/**
|
|
56
|
+
* Ceiling for a single file, in bytes. The widget also applies its own per-kind caps
|
|
57
|
+
* (5 MB image, 10 MB PDF, 1 MB text file), so the smaller of the two wins.
|
|
58
|
+
*/
|
|
59
|
+
maxFileBytes?: number | undefined;
|
|
60
|
+
/** Ceiling for all files on one message, in bytes. Default 20 MB. */
|
|
61
|
+
maxTotalBytes?: number | undefined;
|
|
62
|
+
/**
|
|
63
|
+
* Narrows what the composer takes, as MIME types or `type/*` patterns (`["image/*"]` for
|
|
64
|
+
* images only). Omit to accept everything the chat endpoint supports: PNG, JPEG, WebP and
|
|
65
|
+
* GIF images, PDFs, and text files (which the endpoint reads as text for the agent).
|
|
66
|
+
*/
|
|
67
|
+
accept?: readonly string[] | undefined;
|
|
68
|
+
}
|
|
69
|
+
/** Color scheme of the chat widget; `"system"` follows the OS `prefers-color-scheme` setting. */
|
|
70
|
+
type AstralBeamChatColorScheme = "light" | "dark" | "system";
|
|
71
|
+
/** Overrides for the widget's theming CSS variables, keyed by custom-property name (`"--primary"`). */
|
|
72
|
+
type AstralBeamChatThemeVariables = Record<`--${string}`, string>;
|
|
73
|
+
/**
|
|
74
|
+
* Custom values for the CSS variables the widget's shadcn theme exposes (`--background`,
|
|
75
|
+
* `--primary`, `--radius`, and the `--font-sans`/`--font-heading`/`--font-mono` stacks, ...),
|
|
76
|
+
* mirroring shadcn's `:root`/`.dark` split: `light` is the base applied in both color schemes,
|
|
77
|
+
* and `dark` overrides it when the resolved scheme is dark.
|
|
78
|
+
*/
|
|
79
|
+
interface AstralBeamChatTheme {
|
|
80
|
+
light?: AstralBeamChatThemeVariables | undefined;
|
|
81
|
+
dark?: AstralBeamChatThemeVariables | undefined;
|
|
82
|
+
}
|
|
83
|
+
interface MountAstralBeamChatOptions {
|
|
84
|
+
/** Name shown in the widget's header. Default `"AstralBeam"`. */
|
|
85
|
+
title?: string | undefined;
|
|
86
|
+
/** URL of the AstralBeam chat endpoint the widget streams from. Fixed at mount. Default `"/api/chat"`. */
|
|
87
|
+
chatEndpoint?: string | undefined;
|
|
88
|
+
/** Application endpoint that mints a short-lived chat JWT. Fixed at mount; omit for guest chat. */
|
|
89
|
+
authEndpoint?: string | undefined;
|
|
90
|
+
/** Host-specific instructions the endpoint appends to the agent's system prompt. */
|
|
91
|
+
systemPrompt?: string | undefined;
|
|
92
|
+
/** Host-defined tools the agent can call, executed in the host page, keyed by tool name. */
|
|
93
|
+
tools?: Record<string, ToolDefinition> | undefined;
|
|
94
|
+
/** Host-defined widgets the agent can render inline in the conversation, keyed by identifier. */
|
|
95
|
+
widgets?: Record<string, WidgetDefinition>;
|
|
96
|
+
/**
|
|
97
|
+
* File attachments in the composer, on by default. `false` turns them off; an options object
|
|
98
|
+
* narrows the limits and accepted types.
|
|
99
|
+
*/
|
|
100
|
+
attachments?: boolean | AstralBeamChatAttachmentOptions | undefined;
|
|
101
|
+
/** Color scheme of the widget. Default `"system"`. */
|
|
102
|
+
colorScheme?: AstralBeamChatColorScheme;
|
|
103
|
+
/** Custom values for the widget's theming CSS variables, per color scheme. */
|
|
104
|
+
theme?: AstralBeamChatTheme | undefined;
|
|
105
|
+
/**
|
|
106
|
+
* Logs every SDK action to the browser console with UTC timestamps and full payloads,
|
|
107
|
+
* and asks the endpoint (via the forwarded props) to log its side of the run too.
|
|
108
|
+
*/
|
|
109
|
+
debug?: boolean | undefined;
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Mount options the handle can change afterwards. `chatEndpoint` is excluded on purpose: the
|
|
113
|
+
* streaming connection is constructed once, so a new endpoint would mean a new client and a
|
|
114
|
+
* discarded transcript.
|
|
115
|
+
*/
|
|
116
|
+
type AstralBeamChatUpdate = Partial<Omit<MountAstralBeamChatOptions, "chatEndpoint" | "authEndpoint">>;
|
|
117
|
+
interface AstralBeamChatHandle {
|
|
118
|
+
unmount: () => void;
|
|
119
|
+
/**
|
|
120
|
+
* Merges option changes into the live mount options and applies them in place, keeping the
|
|
121
|
+
* transcript, the chat session, and live widget renders. Only the keys given are replaced.
|
|
122
|
+
*/
|
|
123
|
+
update: (options: AstralBeamChatUpdate) => void;
|
|
124
|
+
}
|
|
125
|
+
//#endregion
|
|
1
126
|
//#region src/client.d.ts
|
|
2
|
-
declare
|
|
127
|
+
declare function mountAstralBeamChat(target: HTMLElement, options?: MountAstralBeamChatOptions): AstralBeamChatHandle;
|
|
3
128
|
//#endregion
|
|
4
|
-
export {
|
|
129
|
+
export { type AstralBeamChatAttachmentOptions, type AstralBeamChatColorScheme, type AstralBeamChatHandle, type AstralBeamChatTheme, type AstralBeamChatThemeVariables, type AstralBeamChatUpdate, type JsonSchemaObject, type MountAstralBeamChatOptions, type ParametersSchema, type StandardSchemaV1, type ToolDefinition, type WidgetDefinition, mountAstralBeamChat };
|
package/dist/client.js
CHANGED
|
@@ -1,4 +1 @@
|
|
|
1
|
-
|
|
2
|
-
const entrypoint = "client";
|
|
3
|
-
//#endregion
|
|
4
|
-
export { entrypoint };
|
|
1
|
+
import{a as e,t}from"./client-utils-DyEcBInP.js";function n(n,r={}){let i={...r},a=t(i.debug);a?.(`mount`,`mounting chat widget`,{title:i.title??`AstralBeam`,chatEndpoint:i.chatEndpoint??`/api/chat`,authentication:i.authEndpoint?`configured`:`guest`,colorScheme:i.colorScheme??`system`,theme:i.theme,systemPrompt:i.systemPrompt,tools:Object.keys(i.tools??{}),widgets:Object.keys(i.widgets??{}),attachments:i.attachments??!0});let o=n.shadowRoot??n.attachShadow({mode:`open`}),s=document.createElement(`div`);s.className=e,s.style.height=`100%`,o.append(s);let c=matchMedia(`(prefers-color-scheme: dark)`),l=new Set,u=e=>{for(let e of l)s.style.removeProperty(e);l.clear();let t={...i.theme?.light,...e?i.theme?.dark:void 0};for(let[e,n]of Object.entries(t))e.startsWith(`--`)&&(s.style.setProperty(e,n),l.add(e))},d=()=>{let e=i.colorScheme??`system`,t=e===`dark`||e===`system`&&c.matches;s.classList.toggle(`dark`,t),u(t),a?.(`theme`,`color scheme "${e}" resolved to ${t?`dark`:`light`}`,{themeVariables:[...l]})};d(),c.addEventListener(`change`,d);let f=!1,p;return import(`./chat-DdU0uj9s.js`).then(({renderChat:e})=>{a?.(`mount`,`chat chunk loaded`),f||(p=e(o,s,i))}),{update:e=>{i={...i,...e},a=t(i.debug),a?.(`mount`,`options updated`,{changed:Object.keys(e)}),d(),p?.update(i)},unmount:()=>{a?.(`mount`,`unmounting chat widget`),f=!0,c.removeEventListener(`change`,d),p?.dispose(),p=void 0,s.remove()}}}export{n as mountAstralBeamChat};
|
package/dist/react.d.ts
CHANGED
|
@@ -1,4 +1,47 @@
|
|
|
1
|
+
import { ReactNode } from "react";
|
|
2
|
+
import { AstralBeamChatAttachmentOptions, AstralBeamChatColorScheme, AstralBeamChatTheme, ToolDefinition, WidgetDefinition as WidgetDefinition$1 } from "@astralbeam/sdk/client";
|
|
3
|
+
|
|
1
4
|
//#region src/react.d.ts
|
|
2
|
-
|
|
5
|
+
interface WidgetDefinition extends Omit<WidgetDefinition$1, "render"> {
|
|
6
|
+
/** Draws the widget with the agent-chosen props, in the host's own React tree. */
|
|
7
|
+
render: (props: Record<string, unknown>) => ReactNode;
|
|
8
|
+
}
|
|
9
|
+
interface AstralBeamChatProps {
|
|
10
|
+
/** Name shown in the widget's header; prop changes apply immediately. Default `"AstralBeam"`. */
|
|
11
|
+
title?: string;
|
|
12
|
+
/** URL of the AstralBeam chat endpoint the widget streams from. Default `"/api/chat"`. */
|
|
13
|
+
chatEndpoint?: string;
|
|
14
|
+
/** Application endpoint that mints a short-lived chat JWT; omit for guest chat. */
|
|
15
|
+
authEndpoint?: string;
|
|
16
|
+
/** Host-specific instructions the endpoint appends to the agent's system prompt. */
|
|
17
|
+
systemPrompt?: string;
|
|
18
|
+
/** Host-defined tools the agent can call, executed in the host's React app, keyed by name. */
|
|
19
|
+
tools?: Record<string, ToolDefinition>;
|
|
20
|
+
/** Host-defined widgets the agent can render inline in the conversation, keyed by identifier. */
|
|
21
|
+
widgets?: Record<string, WidgetDefinition>;
|
|
22
|
+
/** Color scheme of the chat widget; prop changes apply immediately. Default `"system"`. */
|
|
23
|
+
colorScheme?: AstralBeamChatColorScheme;
|
|
24
|
+
/** Custom values for the widget's theming CSS variables, per color scheme; changes apply immediately. */
|
|
25
|
+
theme?: AstralBeamChatTheme | undefined;
|
|
26
|
+
/** File attachments in the composer, on by default; `false` turns them off. */
|
|
27
|
+
attachments?: boolean | AstralBeamChatAttachmentOptions;
|
|
28
|
+
/**
|
|
29
|
+
* Logs every SDK action to the browser console with UTC timestamps and full payloads,
|
|
30
|
+
* and asks the endpoint to log its side of the run too; prop changes apply immediately.
|
|
31
|
+
*/
|
|
32
|
+
debug?: boolean;
|
|
33
|
+
}
|
|
34
|
+
declare function AstralBeamChat({
|
|
35
|
+
title,
|
|
36
|
+
chatEndpoint,
|
|
37
|
+
authEndpoint,
|
|
38
|
+
systemPrompt,
|
|
39
|
+
tools,
|
|
40
|
+
widgets,
|
|
41
|
+
colorScheme,
|
|
42
|
+
theme,
|
|
43
|
+
attachments,
|
|
44
|
+
debug
|
|
45
|
+
}: AstralBeamChatProps): import("react").JSX.Element;
|
|
3
46
|
//#endregion
|
|
4
|
-
export {
|
|
47
|
+
export { AstralBeamChat, type AstralBeamChatAttachmentOptions, type AstralBeamChatColorScheme, AstralBeamChatProps, type AstralBeamChatTheme, type ToolDefinition, WidgetDefinition };
|
package/dist/react.js
CHANGED
|
@@ -1,4 +1,92 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
import { useEffect, useMemo, useRef, useState } from "react";
|
|
2
|
+
import { createPortal } from "react-dom";
|
|
3
|
+
import { mountAstralBeamChat } from "@astralbeam/sdk/client";
|
|
4
|
+
import { jsx } from "react/jsx-runtime";
|
|
5
|
+
//#region src/lib/client-constants.ts
|
|
6
|
+
/** Color scheme used when the mount options and the React prop give none. */
|
|
7
|
+
const DEFAULT_COLOR_SCHEME = "system";
|
|
3
8
|
//#endregion
|
|
4
|
-
|
|
9
|
+
//#region src/react.tsx
|
|
10
|
+
function AstralBeamChat({ title, chatEndpoint, authEndpoint, systemPrompt, tools, widgets = {}, colorScheme = DEFAULT_COLOR_SCHEME, theme, attachments, debug }) {
|
|
11
|
+
const targetRef = useRef(null);
|
|
12
|
+
const handleRef = useRef(null);
|
|
13
|
+
const [activeRenders, setActiveRenders] = useState(/* @__PURE__ */ new Map());
|
|
14
|
+
const toolsRef = useRef(tools);
|
|
15
|
+
useEffect(() => {
|
|
16
|
+
toolsRef.current = tools;
|
|
17
|
+
});
|
|
18
|
+
const nextRenderKey = useRef(0);
|
|
19
|
+
const hostTools = useMemo(() => Object.fromEntries(Object.entries(tools ?? {}).map(([name, definition]) => [name, {
|
|
20
|
+
...definition,
|
|
21
|
+
execute: (input) => {
|
|
22
|
+
const current = toolsRef.current?.[name];
|
|
23
|
+
if (!current) throw new Error(`Tool "${name}" is no longer registered`);
|
|
24
|
+
return current.execute(input);
|
|
25
|
+
}
|
|
26
|
+
}])), [tools]);
|
|
27
|
+
const hostWidgets = useMemo(() => Object.fromEntries(Object.entries(widgets).map(([name, definition]) => [name, {
|
|
28
|
+
...definition,
|
|
29
|
+
render: (props, container) => {
|
|
30
|
+
const key = `astralbeam-render-${nextRenderKey.current++}`;
|
|
31
|
+
setActiveRenders((previous) => new Map(previous).set(key, {
|
|
32
|
+
widget: name,
|
|
33
|
+
container,
|
|
34
|
+
props
|
|
35
|
+
}));
|
|
36
|
+
return () => {
|
|
37
|
+
setActiveRenders((previous) => {
|
|
38
|
+
const next = new Map(previous);
|
|
39
|
+
next.delete(key);
|
|
40
|
+
return next;
|
|
41
|
+
});
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
}])), [widgets]);
|
|
45
|
+
const live = useMemo(() => ({
|
|
46
|
+
title,
|
|
47
|
+
systemPrompt,
|
|
48
|
+
colorScheme,
|
|
49
|
+
theme,
|
|
50
|
+
attachments,
|
|
51
|
+
debug,
|
|
52
|
+
tools: hostTools,
|
|
53
|
+
widgets: hostWidgets
|
|
54
|
+
}), [
|
|
55
|
+
title,
|
|
56
|
+
systemPrompt,
|
|
57
|
+
colorScheme,
|
|
58
|
+
theme,
|
|
59
|
+
attachments,
|
|
60
|
+
debug,
|
|
61
|
+
hostTools,
|
|
62
|
+
hostWidgets
|
|
63
|
+
]);
|
|
64
|
+
const liveRef = useRef(live);
|
|
65
|
+
liveRef.current = live;
|
|
66
|
+
useEffect(() => {
|
|
67
|
+
if (!targetRef.current) return;
|
|
68
|
+
const handle = mountAstralBeamChat(targetRef.current, {
|
|
69
|
+
...liveRef.current,
|
|
70
|
+
chatEndpoint,
|
|
71
|
+
authEndpoint
|
|
72
|
+
});
|
|
73
|
+
handleRef.current = handle;
|
|
74
|
+
return () => {
|
|
75
|
+
handleRef.current = null;
|
|
76
|
+
handle.unmount();
|
|
77
|
+
};
|
|
78
|
+
}, []);
|
|
79
|
+
useEffect(() => {
|
|
80
|
+
handleRef.current?.update(live);
|
|
81
|
+
}, [live]);
|
|
82
|
+
return /* @__PURE__ */ jsx("div", {
|
|
83
|
+
style: { height: "100%" },
|
|
84
|
+
ref: targetRef,
|
|
85
|
+
children: [...activeRenders].map(([key, { widget, container, props }]) => {
|
|
86
|
+
const definition = widgets[widget];
|
|
87
|
+
return definition ? createPortal(definition.render(props), container, key) : null;
|
|
88
|
+
})
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
//#endregion
|
|
92
|
+
export { AstralBeamChat };
|
package/dist/server.d.ts
CHANGED
|
@@ -1,4 +1,33 @@
|
|
|
1
1
|
//#region src/server.d.ts
|
|
2
|
-
declare const
|
|
2
|
+
declare const ASTRALBEAM_CHAT_TOKEN_AUDIENCE = "astralbeam-chat";
|
|
3
|
+
declare const ASTRALBEAM_CHAT_TOKEN_ISSUER = "astralbeam-global";
|
|
4
|
+
declare const ASTRALBEAM_CHAT_TOKEN_TYPE = "astralbeam-chat+jwt";
|
|
5
|
+
declare const ASTRALBEAM_CHAT_TOKEN_KEY_ID = "global-v1";
|
|
6
|
+
declare const ASTRALBEAM_CHAT_TOKEN_LIFETIME_SECONDS = 300;
|
|
7
|
+
declare const ASTRALBEAM_CHAT_TOKEN_MAX_LIFETIME_SECONDS = 600;
|
|
8
|
+
interface AstralBeamChatUser {
|
|
9
|
+
id: string;
|
|
10
|
+
name?: string | undefined;
|
|
11
|
+
email?: string | undefined;
|
|
12
|
+
avatarUrl?: string | undefined;
|
|
13
|
+
}
|
|
14
|
+
interface AstralBeamChatTenant {
|
|
15
|
+
id: string;
|
|
16
|
+
name?: string | undefined;
|
|
17
|
+
logoUrl?: string | undefined;
|
|
18
|
+
}
|
|
19
|
+
interface CreateAstralBeamChatTokenOptions {
|
|
20
|
+
secret: string | Uint8Array;
|
|
21
|
+
user: AstralBeamChatUser;
|
|
22
|
+
tenant: AstralBeamChatTenant;
|
|
23
|
+
expiresInSeconds?: number | undefined;
|
|
24
|
+
}
|
|
25
|
+
/** Creates the short-lived bearer token returned by an application's auth endpoint. */
|
|
26
|
+
declare function createAstralBeamChatToken({
|
|
27
|
+
secret,
|
|
28
|
+
user,
|
|
29
|
+
tenant,
|
|
30
|
+
expiresInSeconds
|
|
31
|
+
}: CreateAstralBeamChatTokenOptions): Promise<string>;
|
|
3
32
|
//#endregion
|
|
4
|
-
export {
|
|
33
|
+
export { ASTRALBEAM_CHAT_TOKEN_AUDIENCE, ASTRALBEAM_CHAT_TOKEN_ISSUER, ASTRALBEAM_CHAT_TOKEN_KEY_ID, ASTRALBEAM_CHAT_TOKEN_LIFETIME_SECONDS, ASTRALBEAM_CHAT_TOKEN_MAX_LIFETIME_SECONDS, ASTRALBEAM_CHAT_TOKEN_TYPE, AstralBeamChatTenant, AstralBeamChatUser, CreateAstralBeamChatTokenOptions, createAstralBeamChatToken };
|