@omni-commerce/sdk 0.6.0 → 0.8.0

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/react.cjs ADDED
@@ -0,0 +1,311 @@
1
+ "use strict";
2
+ "use client";
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __export = (target, all) => {
8
+ for (var name in all)
9
+ __defProp(target, name, { get: all[name], enumerable: true });
10
+ };
11
+ var __copyProps = (to, from, except, desc) => {
12
+ if (from && typeof from === "object" || typeof from === "function") {
13
+ for (let key of __getOwnPropNames(from))
14
+ if (!__hasOwnProp.call(to, key) && key !== except)
15
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
16
+ }
17
+ return to;
18
+ };
19
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
20
+
21
+ // src/react.tsx
22
+ var react_exports = {};
23
+ __export(react_exports, {
24
+ OmniChat: () => OmniChat,
25
+ OmniProvider: () => OmniProvider
26
+ });
27
+ module.exports = __toCommonJS(react_exports);
28
+ var import_react = require("react");
29
+
30
+ // src/application.ts
31
+ function boundedJson(value, maximum) {
32
+ const encoded = JSON.stringify(value);
33
+ if (typeof encoded !== "string" || new TextEncoder().encode(encoded).length > maximum || /data:[^"\s]{0,100};base64,/i.test(encoded))
34
+ throw new Error(
35
+ "Application data must be bounded JSON without base64 payloads."
36
+ );
37
+ return JSON.parse(encoded);
38
+ }
39
+ function createApplicationMessageHandler(input) {
40
+ const calls = /* @__PURE__ */ new Map();
41
+ return async (message) => {
42
+ if (typeof message.requestId !== "string" || message.requestId.length > 200)
43
+ return;
44
+ try {
45
+ if (message.type === "omni-chat:application-request") {
46
+ const application = input.options.application;
47
+ input.reply({
48
+ type: "omni-chat:application-response",
49
+ requestId: message.requestId,
50
+ application: application ? boundedJson(
51
+ {
52
+ name: application.name,
53
+ description: application.description,
54
+ context: await application.getContext() ?? null,
55
+ tools: Object.entries(input.options.frontendTools ?? {}).map(
56
+ ([name, definition]) => ({
57
+ name,
58
+ description: definition.description,
59
+ parameters: definition.parameters
60
+ })
61
+ )
62
+ },
63
+ 65536
64
+ ) : void 0
65
+ });
66
+ } else if (message.type === "omni-chat:tool-request") {
67
+ const name = message.toolName;
68
+ const toolCallId = message.toolCallId;
69
+ if (typeof name !== "string" || !/^app_[a-z][a-z0-9_]{0,59}$/.test(name) || typeof toolCallId !== "string" || !toolCallId || toolCallId.length > 200)
70
+ throw new Error("Invalid application tool request.");
71
+ const definition = Object.hasOwn(
72
+ input.options.frontendTools ?? {},
73
+ name
74
+ ) ? input.options.frontendTools?.[name] : void 0;
75
+ if (!input.options.application || !definition)
76
+ throw new Error("This application action is unavailable.");
77
+ const args = message.args;
78
+ if (!args || typeof args !== "object" || Array.isArray(args))
79
+ throw new Error("Invalid application action arguments.");
80
+ const signature = JSON.stringify({ name, args });
81
+ const prior = calls.get(toolCallId);
82
+ if (prior && prior.signature !== signature)
83
+ throw new Error("This tool call ID was already used.");
84
+ const result = prior?.result ?? Promise.resolve().then(
85
+ () => definition.execute(args, {
86
+ toolCallId,
87
+ abortSignal: input.signal
88
+ })
89
+ );
90
+ if (!prior) {
91
+ if (calls.size >= 200)
92
+ throw new Error("Start a new application chat.");
93
+ calls.set(toolCallId, { signature, result });
94
+ }
95
+ input.reply({
96
+ type: "omni-chat:tool-response",
97
+ requestId: message.requestId,
98
+ toolCallId,
99
+ result: boundedJson(await result ?? null, 12e3)
100
+ });
101
+ }
102
+ } catch (error) {
103
+ input.reply({
104
+ type: message.type === "omni-chat:application-request" ? "omni-chat:application-response" : "omni-chat:tool-response",
105
+ requestId: message.requestId,
106
+ toolCallId: message.toolCallId,
107
+ error: error instanceof Error ? error.message : "The application action failed."
108
+ });
109
+ }
110
+ };
111
+ }
112
+
113
+ // src/embed.ts
114
+ function getOmniChatEmbedUrl(options, parentOrigin) {
115
+ if (!/^[\da-f]{8}-[\da-f]{4}-[\da-f]{4}-[\da-f]{4}-[\da-f]{12}$/i.test(
116
+ options.deploymentId
117
+ ))
118
+ throw new Error("A valid chat deployment ID is required.");
119
+ const base = new URL(options.baseUrl ?? "https://omnicommerce.sg");
120
+ if (base.protocol !== "https:" && !(base.protocol === "http:" && ["localhost", "127.0.0.1"].includes(base.hostname)) || base.username || base.password || base.pathname !== "/" || base.search || base.hash)
121
+ throw new Error("Chat baseUrl must be an HTTPS origin, or HTTP localhost.");
122
+ const url = new URL(`/embed/${options.deploymentId}`, base.origin);
123
+ if (parentOrigin)
124
+ url.searchParams.set("parentOrigin", new URL(parentOrigin).origin);
125
+ return url.toString();
126
+ }
127
+ function createOmniChatEmbed(container, options) {
128
+ const document = container.ownerDocument;
129
+ const host = document.defaultView;
130
+ if (!host) throw new Error("A browser document is required to mount chat.");
131
+ const iframe = document.createElement("iframe");
132
+ iframe.src = getOmniChatEmbedUrl(options, host.location.origin);
133
+ iframe.title = options.title ?? "OmniCommerce assistant";
134
+ iframe.referrerPolicy = "strict-origin-when-cross-origin";
135
+ iframe.setAttribute(
136
+ "sandbox",
137
+ "allow-scripts allow-same-origin allow-forms allow-popups allow-popups-to-escape-sandbox"
138
+ );
139
+ Object.assign(iframe.style, {
140
+ width: "100%",
141
+ height: "100%",
142
+ minHeight: "320px",
143
+ border: "0",
144
+ display: "block"
145
+ });
146
+ const chatOrigin = new URL(iframe.src).origin;
147
+ let destroyed = false;
148
+ let pending;
149
+ let previousToken;
150
+ const controller = new AbortController();
151
+ const mint = async () => {
152
+ if (options.getSession) return options.getSession();
153
+ const response = await host.fetch(
154
+ `${chatOrigin}/api/embed/${options.deploymentId}/session`,
155
+ {
156
+ method: "POST",
157
+ credentials: "include",
158
+ signal: controller.signal,
159
+ headers: {
160
+ "Content-Type": "application/json",
161
+ ...previousToken ? { Authorization: `Bearer ${previousToken}` } : {}
162
+ },
163
+ body: "{}"
164
+ }
165
+ );
166
+ const result = await response.json();
167
+ if (!response.ok) {
168
+ if (response.status === 401 || response.status === 403)
169
+ previousToken = void 0;
170
+ throw new Error(result.error ?? "Could not open the chat session.");
171
+ }
172
+ return result;
173
+ };
174
+ const applicationMessage = createApplicationMessageHandler({
175
+ options,
176
+ signal: controller.signal,
177
+ reply: (message) => {
178
+ if (!destroyed)
179
+ iframe.contentWindow?.postMessage(
180
+ { ...message, deploymentId: options.deploymentId },
181
+ chatOrigin
182
+ );
183
+ }
184
+ });
185
+ const receive = (event) => {
186
+ if (destroyed || event.source !== iframe.contentWindow || event.origin !== chatOrigin || event.data?.deploymentId !== options.deploymentId)
187
+ return;
188
+ if (event.data.type === "omni-chat:application-request" || event.data.type === "omni-chat:tool-request") {
189
+ void applicationMessage(event.data);
190
+ return;
191
+ }
192
+ if (event.data.type === "omni-chat:close-request") {
193
+ options.onRequestClose?.();
194
+ return;
195
+ }
196
+ if (event.data.type !== "omni-chat:session-request") return;
197
+ pending ?? (pending = mint());
198
+ void pending.then((session) => {
199
+ if (destroyed) return;
200
+ if (typeof session?.token !== "string" || !session.token || session.token.length > 4096 || typeof session.threadId !== "string" || !session.threadId || !Number.isFinite(session.expiresIn) || session.expiresIn <= 0)
201
+ throw new Error("Invalid chat session response.");
202
+ previousToken = session.token;
203
+ iframe.contentWindow?.postMessage(
204
+ {
205
+ type: "omni-chat:session",
206
+ deploymentId: options.deploymentId,
207
+ session,
208
+ ...options.application ? { applicationEnabled: true } : {}
209
+ },
210
+ chatOrigin
211
+ );
212
+ }).catch((error) => {
213
+ if (destroyed) return;
214
+ const failure = error instanceof Error ? error : new Error("Could not open chat.");
215
+ options.onError?.(failure);
216
+ iframe.contentWindow?.postMessage(
217
+ {
218
+ type: "omni-chat:session-error",
219
+ deploymentId: options.deploymentId,
220
+ error: failure.message,
221
+ ...options.application ? { applicationEnabled: true } : {}
222
+ },
223
+ chatOrigin
224
+ );
225
+ }).finally(() => {
226
+ pending = void 0;
227
+ });
228
+ };
229
+ host.addEventListener("message", receive);
230
+ container.append(iframe);
231
+ return {
232
+ iframe,
233
+ focus() {
234
+ if (destroyed) return;
235
+ iframe.focus();
236
+ iframe.contentWindow?.postMessage(
237
+ {
238
+ type: "omni-chat:focus-composer",
239
+ deploymentId: options.deploymentId
240
+ },
241
+ chatOrigin
242
+ );
243
+ },
244
+ destroy() {
245
+ if (destroyed) return;
246
+ destroyed = true;
247
+ controller.abort();
248
+ previousToken = void 0;
249
+ host.removeEventListener("message", receive);
250
+ iframe.remove();
251
+ }
252
+ };
253
+ }
254
+
255
+ // src/react.tsx
256
+ var import_jsx_runtime = require("react/jsx-runtime");
257
+ var OmniChatContext = (0, import_react.createContext)(null);
258
+ function OmniProvider({ children, ...options }) {
259
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(OmniChatContext.Provider, { value: options, children });
260
+ }
261
+ function OmniChat({ className, style, title }) {
262
+ const options = (0, import_react.useContext)(OmniChatContext);
263
+ if (!options)
264
+ throw new Error("OmniChat must be rendered inside OmniProvider.");
265
+ const container = (0, import_react.useRef)(null);
266
+ const {
267
+ baseUrl,
268
+ deploymentId,
269
+ getSession,
270
+ onError,
271
+ application,
272
+ frontendTools
273
+ } = options;
274
+ const callbacks = (0, import_react.useRef)({ getSession, onError, application, frontendTools });
275
+ (0, import_react.useEffect)(() => {
276
+ callbacks.current = { getSession, onError, application, frontendTools };
277
+ }, [getSession, onError, application, frontendTools]);
278
+ const hasSessionBroker = Boolean(getSession);
279
+ const frameTitle = title ?? options.title;
280
+ (0, import_react.useEffect)(() => {
281
+ if (!container.current) return;
282
+ const embed = createOmniChatEmbed(container.current, {
283
+ baseUrl,
284
+ deploymentId,
285
+ getSession: hasSessionBroker ? () => callbacks.current.getSession() : void 0,
286
+ onError: (error) => callbacks.current.onError?.(error),
287
+ title: frameTitle,
288
+ get application() {
289
+ return callbacks.current.application;
290
+ },
291
+ get frontendTools() {
292
+ return callbacks.current.frontendTools;
293
+ }
294
+ });
295
+ return () => embed.destroy();
296
+ }, [baseUrl, deploymentId, frameTitle, hasSessionBroker]);
297
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
298
+ "div",
299
+ {
300
+ ref: container,
301
+ className,
302
+ style: { height: "100%", minHeight: 320, ...style }
303
+ }
304
+ );
305
+ }
306
+ // Annotate the CommonJS export names for ESM import in node:
307
+ 0 && (module.exports = {
308
+ OmniChat,
309
+ OmniProvider
310
+ });
311
+ //# sourceMappingURL=react.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/react.tsx","../src/application.ts","../src/embed.ts"],"sourcesContent":["\"use client\";\n\nimport {\n createContext,\n useContext,\n useEffect,\n useRef,\n type CSSProperties,\n type ReactNode,\n} from \"react\";\nimport { createOmniChatEmbed, type OmniChatEmbedOptions } from \"./embed\";\n\nconst OmniChatContext = createContext<OmniChatEmbedOptions | null>(null);\nexport type OmniProviderProps = OmniChatEmbedOptions & { children: ReactNode };\n\nexport function OmniProvider({ children, ...options }: OmniProviderProps) {\n return (\n <OmniChatContext.Provider value={options}>\n {children}\n </OmniChatContext.Provider>\n );\n}\nexport interface OmniChatProps {\n className?: string;\n style?: CSSProperties;\n title?: string;\n}\n/** The hosted assistant owns its styles and runtime, so the host needs no Tailwind or assistant-ui dependencies. */\nexport function OmniChat({ className, style, title }: OmniChatProps) {\n const options = useContext(OmniChatContext);\n if (!options)\n throw new Error(\"OmniChat must be rendered inside OmniProvider.\");\n const container = useRef<HTMLDivElement>(null);\n const {\n baseUrl,\n deploymentId,\n getSession,\n onError,\n application,\n frontendTools,\n } = options;\n const callbacks = useRef({ getSession, onError, application, frontendTools });\n useEffect(() => {\n callbacks.current = { getSession, onError, application, frontendTools };\n }, [getSession, onError, application, frontendTools]);\n const hasSessionBroker = Boolean(getSession);\n const frameTitle = title ?? options.title;\n useEffect(() => {\n if (!container.current) return;\n const embed = createOmniChatEmbed(container.current, {\n baseUrl,\n deploymentId,\n getSession: hasSessionBroker\n ? () => callbacks.current.getSession!()\n : undefined,\n onError: (error) => callbacks.current.onError?.(error),\n title: frameTitle,\n get application() {\n return callbacks.current.application;\n },\n get frontendTools() {\n return callbacks.current.frontendTools;\n },\n });\n return () => embed.destroy();\n }, [baseUrl, deploymentId, frameTitle, hasSessionBroker]);\n return (\n <div\n ref={container}\n className={className}\n style={{ height: \"100%\", minHeight: 320, ...style }}\n />\n );\n}\nexport type {\n OmniFrontendTool,\n OmniChatApplication,\n OmniApplicationOptions,\n OmniChatEmbedOptions,\n OmniChatSession,\n} from \"./embed\";\n","/** Host-defined actions execute inside the application, never on the assistant server. */\nexport interface OmniFrontendTool {\n description: string;\n parameters: Record<string, unknown>;\n execute: (\n args: Record<string, unknown>,\n context: { toolCallId: string; abortSignal: AbortSignal },\n ) => unknown | Promise<unknown>;\n}\nexport interface OmniChatApplication {\n name: string;\n description: string;\n /** Return a small, explicitly selected snapshot. Do not include secrets. */\n getContext: () => unknown | Promise<unknown>;\n}\nexport interface OmniApplicationOptions {\n application?: OmniChatApplication;\n /** Names must start with app_; use array arguments for actions with multiple targets. */\n frontendTools?: Record<string, OmniFrontendTool>;\n}\n\nfunction boundedJson(value: unknown, maximum: number): unknown {\n const encoded = JSON.stringify(value);\n if (\n typeof encoded !== \"string\" ||\n new TextEncoder().encode(encoded).length > maximum ||\n /data:[^\"\\s]{0,100};base64,/i.test(encoded)\n )\n throw new Error(\n \"Application data must be bounded JSON without base64 payloads.\",\n );\n return JSON.parse(encoded);\n}\n\nexport function createApplicationMessageHandler(input: {\n options: OmniApplicationOptions;\n signal: AbortSignal;\n reply: (message: Record<string, unknown>) => void;\n}) {\n // A repeated parked call reuses its result instead of applying an action twice.\n const calls = new Map<\n string,\n { signature: string; result: Promise<unknown> }\n >();\n return async (message: Record<string, unknown>) => {\n if (typeof message.requestId !== \"string\" || message.requestId.length > 200)\n return;\n try {\n if (message.type === \"omni-chat:application-request\") {\n const application = input.options.application;\n input.reply({\n type: \"omni-chat:application-response\",\n requestId: message.requestId,\n application: application\n ? boundedJson(\n {\n name: application.name,\n description: application.description,\n context: (await application.getContext()) ?? null,\n tools: Object.entries(input.options.frontendTools ?? {}).map(\n ([name, definition]) => ({\n name,\n description: definition.description,\n parameters: definition.parameters,\n }),\n ),\n },\n 65536,\n )\n : undefined,\n });\n } else if (message.type === \"omni-chat:tool-request\") {\n const name = message.toolName;\n const toolCallId = message.toolCallId;\n if (\n typeof name !== \"string\" ||\n !/^app_[a-z][a-z0-9_]{0,59}$/.test(name) ||\n typeof toolCallId !== \"string\" ||\n !toolCallId ||\n toolCallId.length > 200\n )\n throw new Error(\"Invalid application tool request.\");\n const definition = Object.hasOwn(\n input.options.frontendTools ?? {},\n name,\n )\n ? input.options.frontendTools?.[name]\n : undefined;\n if (!input.options.application || !definition)\n throw new Error(\"This application action is unavailable.\");\n const args = message.args;\n if (!args || typeof args !== \"object\" || Array.isArray(args))\n throw new Error(\"Invalid application action arguments.\");\n const signature = JSON.stringify({ name, args });\n const prior = calls.get(toolCallId);\n if (prior && prior.signature !== signature)\n throw new Error(\"This tool call ID was already used.\");\n const result =\n prior?.result ??\n Promise.resolve().then(() =>\n definition.execute(args as Record<string, unknown>, {\n toolCallId,\n abortSignal: input.signal,\n }),\n );\n if (!prior) {\n if (calls.size >= 200)\n throw new Error(\"Start a new application chat.\");\n calls.set(toolCallId, { signature, result });\n }\n input.reply({\n type: \"omni-chat:tool-response\",\n requestId: message.requestId,\n toolCallId,\n result: boundedJson((await result) ?? null, 12000),\n });\n }\n } catch (error) {\n input.reply({\n type:\n message.type === \"omni-chat:application-request\"\n ? \"omni-chat:application-response\"\n : \"omni-chat:tool-response\",\n requestId: message.requestId,\n toolCallId: message.toolCallId,\n error:\n error instanceof Error\n ? error.message\n : \"The application action failed.\",\n });\n }\n };\n}\n","import {\n createApplicationMessageHandler,\n type OmniApplicationOptions,\n} from \"./application\";\nexport type {\n OmniFrontendTool,\n OmniChatApplication,\n OmniApplicationOptions,\n} from \"./application\";\n/** A hosted chat session. Keep its short-lived token in memory, never in iframe URLs. */\nexport { createOmniChatWidget, type OmniChatWidgetOptions } from \"./widget\";\nexport { getOmniChatEmbedCode, getOmniChatReactCode } from \"./embed-code\";\nexport interface OmniChatSession {\n token: string;\n threadId: string;\n expiresIn: number;\n}\nexport interface OmniChatEmbedOptions extends OmniApplicationOptions {\n deploymentId: string;\n baseUrl?: string;\n /** Optional authenticated session mint on the host application's server. */\n getSession?: () => Promise<OmniChatSession>;\n title?: string;\n onError?: (error: Error) => void;\n /** Floating widgets use this to close on Escape from the hosted chat. */\n onRequestClose?: () => void;\n}\nexport interface OmniChatEmbed {\n iframe: HTMLIFrameElement;\n /** Focus the hosted composer, including when reopening a retained chat. */\n focus(): void;\n destroy(): void;\n}\n\nexport function getOmniChatEmbedUrl(\n options: Pick<OmniChatEmbedOptions, \"baseUrl\" | \"deploymentId\">,\n parentOrigin?: string,\n): string {\n if (\n !/^[\\da-f]{8}-[\\da-f]{4}-[\\da-f]{4}-[\\da-f]{4}-[\\da-f]{12}$/i.test(\n options.deploymentId,\n )\n )\n throw new Error(\"A valid chat deployment ID is required.\");\n const base = new URL(options.baseUrl ?? \"https://omnicommerce.sg\");\n if (\n (base.protocol !== \"https:\" &&\n !(\n base.protocol === \"http:\" &&\n [\"localhost\", \"127.0.0.1\"].includes(base.hostname)\n )) ||\n base.username ||\n base.password ||\n base.pathname !== \"/\" ||\n base.search ||\n base.hash\n )\n throw new Error(\"Chat baseUrl must be an HTTPS origin, or HTTP localhost.\");\n const url = new URL(`/embed/${options.deploymentId}`, base.origin);\n if (parentOrigin)\n url.searchParams.set(\"parentOrigin\", new URL(parentOrigin).origin);\n return url.toString();\n}\n\n/** Browser-only mounting; importing this entry point is safe during SSR. */\nexport function createOmniChatEmbed(\n container: HTMLElement,\n options: OmniChatEmbedOptions,\n): OmniChatEmbed {\n const document = container.ownerDocument;\n const host = document.defaultView;\n if (!host) throw new Error(\"A browser document is required to mount chat.\");\n const iframe = document.createElement(\"iframe\");\n iframe.src = getOmniChatEmbedUrl(options, host.location.origin);\n iframe.title = options.title ?? \"OmniCommerce assistant\";\n iframe.referrerPolicy = \"strict-origin-when-cross-origin\";\n iframe.setAttribute(\n \"sandbox\",\n \"allow-scripts allow-same-origin allow-forms allow-popups allow-popups-to-escape-sandbox\",\n );\n Object.assign(iframe.style, {\n width: \"100%\",\n height: \"100%\",\n minHeight: \"320px\",\n border: \"0\",\n display: \"block\",\n });\n const chatOrigin = new URL(iframe.src).origin;\n let destroyed = false;\n let pending: Promise<OmniChatSession> | undefined;\n let previousToken: string | undefined;\n const controller = new AbortController();\n\n const mint = async (): Promise<OmniChatSession> => {\n if (options.getSession) return options.getSession();\n const response = await host.fetch(\n `${chatOrigin}/api/embed/${options.deploymentId}/session`,\n {\n method: \"POST\",\n credentials: \"include\",\n signal: controller.signal,\n headers: {\n \"Content-Type\": \"application/json\",\n ...(previousToken\n ? { Authorization: `Bearer ${previousToken}` }\n : {}),\n },\n body: \"{}\",\n },\n );\n const result = (await response.json()) as OmniChatSession & {\n error?: string;\n };\n if (!response.ok) {\n // A rejected renewal must not keep Retry bound to an obsolete session.\n // The next mint still performs the deployment's full access checks.\n if (response.status === 401 || response.status === 403)\n previousToken = undefined;\n throw new Error(result.error ?? \"Could not open the chat session.\");\n }\n return result;\n };\n const applicationMessage = createApplicationMessageHandler({\n options,\n signal: controller.signal,\n reply: (message) => {\n if (!destroyed)\n iframe.contentWindow?.postMessage(\n { ...message, deploymentId: options.deploymentId },\n chatOrigin,\n );\n },\n });\n const receive = (event: MessageEvent) => {\n if (\n destroyed ||\n event.source !== iframe.contentWindow ||\n event.origin !== chatOrigin ||\n event.data?.deploymentId !== options.deploymentId\n )\n return;\n if (\n event.data.type === \"omni-chat:application-request\" ||\n event.data.type === \"omni-chat:tool-request\"\n ) {\n void applicationMessage(event.data);\n return;\n }\n if (event.data.type === \"omni-chat:close-request\") {\n options.onRequestClose?.();\n return;\n }\n if (event.data.type !== \"omni-chat:session-request\") return;\n pending ??= mint();\n void pending\n .then((session) => {\n if (destroyed) return;\n if (\n typeof session?.token !== \"string\" ||\n !session.token ||\n session.token.length > 4096 ||\n typeof session.threadId !== \"string\" ||\n !session.threadId ||\n !Number.isFinite(session.expiresIn) ||\n session.expiresIn <= 0\n )\n throw new Error(\"Invalid chat session response.\");\n previousToken = session.token;\n iframe.contentWindow?.postMessage(\n {\n type: \"omni-chat:session\",\n deploymentId: options.deploymentId,\n session,\n ...(options.application ? { applicationEnabled: true } : {}),\n },\n chatOrigin,\n );\n })\n .catch((error: unknown) => {\n if (destroyed) return;\n const failure =\n error instanceof Error ? error : new Error(\"Could not open chat.\");\n options.onError?.(failure);\n iframe.contentWindow?.postMessage(\n {\n type: \"omni-chat:session-error\",\n deploymentId: options.deploymentId,\n error: failure.message,\n ...(options.application ? {applicationEnabled: true} : {}),\n },\n chatOrigin,\n );\n })\n .finally(() => {\n pending = undefined;\n });\n };\n host.addEventListener(\"message\", receive);\n container.append(iframe);\n return {\n iframe,\n focus() {\n if (destroyed) return;\n iframe.focus();\n iframe.contentWindow?.postMessage(\n {\n type: \"omni-chat:focus-composer\",\n deploymentId: options.deploymentId,\n },\n chatOrigin,\n );\n },\n destroy() {\n if (destroyed) return;\n destroyed = true;\n controller.abort();\n previousToken = undefined;\n host.removeEventListener(\"message\", receive);\n iframe.remove();\n },\n };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAEA,mBAOO;;;ACYP,SAAS,YAAY,OAAgB,SAA0B;AAC7D,QAAM,UAAU,KAAK,UAAU,KAAK;AACpC,MACE,OAAO,YAAY,YACnB,IAAI,YAAY,EAAE,OAAO,OAAO,EAAE,SAAS,WAC3C,8BAA8B,KAAK,OAAO;AAE1C,UAAM,IAAI;AAAA,MACR;AAAA,IACF;AACF,SAAO,KAAK,MAAM,OAAO;AAC3B;AAEO,SAAS,gCAAgC,OAI7C;AAED,QAAM,QAAQ,oBAAI,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AU;AAC3C,QAAM,aAAa,SAAS,QAAQ;AACpC,8BAAU,MAAM;AACd,QAAI,CAAC,UAAU,QAAS;AACxB,UAAM,QAAQ,oBAAoB,UAAU,SAAS;AAAA,MACnD;AAAA,MACA;AAAA,MACA,YAAY,mBACR,MAAM,UAAU,QAAQ,WAAY,IACpC;AAAA,MACJ,SAAS,CAAC,UAAU,UAAU,QAAQ,UAAU,KAAK;AAAA,MACrD,OAAO;AAAA,MACP,IAAI,cAAc;AAChB,eAAO,UAAU,QAAQ;AAAA,MAC3B;AAAA,MACA,IAAI,gBAAgB;AAClB,eAAO,UAAU,QAAQ;AAAA,MAC3B;AAAA,IACF,CAAC;AACD,WAAO,MAAM,MAAM,QAAQ;AAAA,EAC7B,GAAG,CAAC,SAAS,cAAc,YAAY,gBAAgB,CAAC;AACxD,SACE;AAAA,IAAC;AAAA;AAAA,MACC,KAAK;AAAA,MACL;AAAA,MACA,OAAO,EAAE,QAAQ,QAAQ,WAAW,KAAK,GAAG,MAAM;AAAA;AAAA,EACpD;AAEJ;","names":[]}
@@ -0,0 +1,18 @@
1
+ import * as react_jsx_runtime from 'react/jsx-runtime';
2
+ import { CSSProperties, ReactNode } from 'react';
3
+ import { OmniChatEmbedOptions } from './embed.cjs';
4
+ export { OmniApplicationOptions, OmniChatApplication, OmniChatSession, OmniFrontendTool } from './embed.cjs';
5
+
6
+ type OmniProviderProps = OmniChatEmbedOptions & {
7
+ children: ReactNode;
8
+ };
9
+ declare function OmniProvider({ children, ...options }: OmniProviderProps): react_jsx_runtime.JSX.Element;
10
+ interface OmniChatProps {
11
+ className?: string;
12
+ style?: CSSProperties;
13
+ title?: string;
14
+ }
15
+ /** The hosted assistant owns its styles and runtime, so the host needs no Tailwind or assistant-ui dependencies. */
16
+ declare function OmniChat({ className, style, title }: OmniChatProps): react_jsx_runtime.JSX.Element;
17
+
18
+ export { OmniChat, OmniChatEmbedOptions, type OmniChatProps, OmniProvider, type OmniProviderProps };
@@ -0,0 +1,18 @@
1
+ import * as react_jsx_runtime from 'react/jsx-runtime';
2
+ import { CSSProperties, ReactNode } from 'react';
3
+ import { OmniChatEmbedOptions } from './embed.js';
4
+ export { OmniApplicationOptions, OmniChatApplication, OmniChatSession, OmniFrontendTool } from './embed.js';
5
+
6
+ type OmniProviderProps = OmniChatEmbedOptions & {
7
+ children: ReactNode;
8
+ };
9
+ declare function OmniProvider({ children, ...options }: OmniProviderProps): react_jsx_runtime.JSX.Element;
10
+ interface OmniChatProps {
11
+ className?: string;
12
+ style?: CSSProperties;
13
+ title?: string;
14
+ }
15
+ /** The hosted assistant owns its styles and runtime, so the host needs no Tailwind or assistant-ui dependencies. */
16
+ declare function OmniChat({ className, style, title }: OmniChatProps): react_jsx_runtime.JSX.Element;
17
+
18
+ export { OmniChat, OmniChatEmbedOptions, type OmniChatProps, OmniProvider, type OmniProviderProps };