@ghuri/ai-widget 0.0.0-stage → 0.1.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/README.md +111 -2
- package/dist/core/client.d.ts +76 -0
- package/dist/core/history.d.ts +17 -0
- package/dist/core/index.d.ts +4 -0
- package/dist/core/sse.d.ts +22 -0
- package/dist/core/types.d.ts +134 -0
- package/dist/core-BqhB9RhK.js +523 -0
- package/dist/core-BqhB9RhK.js.map +1 -0
- package/dist/core.js +1 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.js +3 -0
- package/dist/react.d.ts +22 -0
- package/dist/react.js +88 -0
- package/dist/react.js.map +1 -0
- package/dist/src-DcFnX61O.js +1532 -0
- package/dist/src-DcFnX61O.js.map +1 -0
- package/dist/ui/element.d.ts +150 -0
- package/dist/ui/format.d.ts +13 -0
- package/dist/ui/icons.d.ts +5 -0
- package/dist/ui/labels.d.ts +45 -0
- package/dist/ui/markdown.d.ts +3 -0
- package/dist/ui/styles.d.ts +1 -0
- package/package.json +83 -4
package/dist/react.js
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import "./src-DcFnX61O.js";
|
|
2
|
+
import { createElement as e, forwardRef as t, useEffect as n, useImperativeHandle as r, useRef as i, useState as a } from "react";
|
|
3
|
+
import { createPortal as o } from "react-dom";
|
|
4
|
+
// src/react.tsx
|
|
5
|
+
function s(e, t) {
|
|
6
|
+
let n = {};
|
|
7
|
+
for (let [r, i] of Object.entries(e ?? {})) {
|
|
8
|
+
let e = (e) => e ? (n, r) => (t((t) => new Map(t).set(n, e(r))), () => t((e) => {
|
|
9
|
+
let t = new Map(e);
|
|
10
|
+
return t.delete(n), t;
|
|
11
|
+
})) : void 0;
|
|
12
|
+
n[r] = {
|
|
13
|
+
result: e(i.result),
|
|
14
|
+
confirm: e(i.confirm)
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
return n;
|
|
18
|
+
}
|
|
19
|
+
var c = t(function(t, c) {
|
|
20
|
+
let l = i(null), u = i(t);
|
|
21
|
+
u.current = t;
|
|
22
|
+
let [d, f] = a(() => /* @__PURE__ */ new Map());
|
|
23
|
+
return r(c, () => l.current, []), n(() => {
|
|
24
|
+
let e = l.current;
|
|
25
|
+
if (!e) return;
|
|
26
|
+
let t = u.current;
|
|
27
|
+
e.configure({
|
|
28
|
+
apiUrl: t.apiUrl,
|
|
29
|
+
startSession: () => u.current.startSession(),
|
|
30
|
+
refreshUserToken: (e) => u.current.refreshUserToken(e),
|
|
31
|
+
endSession: (e) => u.current.endSession?.(e) ?? Promise.resolve(),
|
|
32
|
+
...t.fetch ? { fetch: t.fetch } : {},
|
|
33
|
+
layout: t.layout,
|
|
34
|
+
title: t.title,
|
|
35
|
+
subtitle: t.subtitle,
|
|
36
|
+
locale: t.locale,
|
|
37
|
+
labels: t.labels,
|
|
38
|
+
starters: t.starters,
|
|
39
|
+
toolSteps: t.toolSteps,
|
|
40
|
+
cards: s(t.cards, f)
|
|
41
|
+
});
|
|
42
|
+
}, [t.apiUrl]), n(() => {
|
|
43
|
+
l.current?.setOptions({
|
|
44
|
+
layout: t.layout,
|
|
45
|
+
title: t.title,
|
|
46
|
+
subtitle: t.subtitle,
|
|
47
|
+
locale: t.locale,
|
|
48
|
+
labels: t.labels,
|
|
49
|
+
starters: t.starters,
|
|
50
|
+
toolSteps: t.toolSteps,
|
|
51
|
+
cards: s(t.cards, f)
|
|
52
|
+
});
|
|
53
|
+
}, [
|
|
54
|
+
t.layout,
|
|
55
|
+
t.title,
|
|
56
|
+
t.subtitle,
|
|
57
|
+
t.locale,
|
|
58
|
+
t.labels,
|
|
59
|
+
t.starters,
|
|
60
|
+
t.toolSteps,
|
|
61
|
+
t.cards
|
|
62
|
+
]), n(() => {
|
|
63
|
+
let e = l.current;
|
|
64
|
+
e && t.open !== void 0 && (t.open ? e.show() : e.hide());
|
|
65
|
+
}, [t.open]), n(() => {
|
|
66
|
+
let e = l.current;
|
|
67
|
+
if (!e) return;
|
|
68
|
+
let t = [
|
|
69
|
+
["ghuri:tool-result", (e) => u.current.onToolResult?.(e.detail)],
|
|
70
|
+
["ghuri:approval-pending", (e) => u.current.onApprovalPending?.(e.detail)],
|
|
71
|
+
["ghuri:turn-end", (e) => u.current.onTurnEnd?.(e.detail)],
|
|
72
|
+
["ghuri:open", () => u.current.onOpen?.()],
|
|
73
|
+
["ghuri:close", () => u.current.onClose?.()]
|
|
74
|
+
];
|
|
75
|
+
for (let [n, r] of t) e.addEventListener(n, r);
|
|
76
|
+
return () => {
|
|
77
|
+
for (let [n, r] of t) e.removeEventListener(n, r);
|
|
78
|
+
};
|
|
79
|
+
}, []), e("ghuri-chat", {
|
|
80
|
+
ref: l,
|
|
81
|
+
className: t.className,
|
|
82
|
+
style: t.style,
|
|
83
|
+
layout: t.layout ?? "panel"
|
|
84
|
+
}, ...[...d].map(([e, t]) => o(t, e, e.slot)));
|
|
85
|
+
});
|
|
86
|
+
export { c as GhuriChat };
|
|
87
|
+
|
|
88
|
+
//# sourceMappingURL=react.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"react.js","names":["toCards","out","tool","spec","cards","wrap","fn","setPortals","m","container","ctx","next","GhuriChat","forwardRef","element","useRef","latest","props","portals","useState","useImperativeHandle","ref","useEffect","el","p","id","handlers","name","createElement","node","createPortal"],"sources":["../src/react.tsx"],"sourcesContent":["// @ghuri/ai-widget/react: <GhuriChat> for React apps. A thin wrapper around <ghuri-chat>: props configure it, `on*`\n// props receive its events, and `cards` take React components (rendered with portals into the element's card slots,\n// so they live in the app's React tree and its CSS applies).\nimport {\n type CSSProperties,\n createElement,\n forwardRef,\n type ReactNode,\n useEffect,\n useImperativeHandle,\n useRef,\n useState,\n} from 'react'\nimport { createPortal } from 'react-dom'\nimport './index'\nimport type { ChatEvents } from './core/types'\nimport type { CardContext, CardSpec, GhuriChatElement, GhuriChatOptions } from './ui/element'\n\nexport interface ReactCardSpec {\n result?: (ctx: CardContext) => ReactNode\n confirm?: (ctx: CardContext) => ReactNode\n}\n\nexport interface GhuriChatProps extends Omit<GhuriChatOptions, 'cards'> {\n /** Panel layout: whether it is shown. */\n open?: boolean\n cards?: Record<string, ReactCardSpec>\n onToolResult?: (e: ChatEvents['tool-result']) => void\n onApprovalPending?: (e: ChatEvents['approval-pending']) => void\n onTurnEnd?: (e: ChatEvents['turn-end']) => void\n onOpen?: () => void\n onClose?: () => void\n className?: string\n style?: CSSProperties\n}\n\nexport type { CardContext, GhuriChatElement, GhuriChatOptions } from './ui/element'\n\ntype Portals = Map<HTMLElement, ReactNode>\ntype SetPortals = (update: (m: Portals) => Portals) => void\n\n/** Each React card becomes a DOM renderer that puts its node in a portal (removed when the card goes away). */\nfunction toCards(\n cards: Record<string, ReactCardSpec> | undefined,\n setPortals: SetPortals,\n): Record<string, CardSpec> {\n const out: Record<string, CardSpec> = {}\n for (const [tool, spec] of Object.entries(cards ?? {})) {\n const wrap = (fn?: (ctx: CardContext) => ReactNode) =>\n fn\n ? (container: HTMLElement, ctx: CardContext) => {\n setPortals((m) => new Map(m).set(container, fn(ctx)))\n return () =>\n setPortals((m) => {\n const next = new Map(m)\n next.delete(container)\n return next\n })\n }\n : undefined\n out[tool] = { result: wrap(spec.result), confirm: wrap(spec.confirm) }\n }\n return out\n}\n\nexport const GhuriChat = forwardRef<GhuriChatElement | null, GhuriChatProps>(function GhuriChat(props, ref) {\n const element = useRef<GhuriChatElement | null>(null)\n const latest = useRef(props)\n latest.current = props\n const [portals, setPortals] = useState<Portals>(() => new Map())\n useImperativeHandle(ref, () => element.current as GhuriChatElement, [])\n\n // Connect once per Ghuri AI URL; the product's callbacks are read from the latest props on every call.\n // biome-ignore lint/correctness/useExhaustiveDependencies: reconnect only when the API URL changes\n useEffect(() => {\n const el = element.current\n if (!el) return\n const p = latest.current\n el.configure({\n apiUrl: p.apiUrl,\n startSession: () => latest.current.startSession(),\n refreshUserToken: (id) => latest.current.refreshUserToken(id),\n endSession: (id) => latest.current.endSession?.(id) ?? Promise.resolve(),\n ...(p.fetch ? { fetch: p.fetch } : {}),\n layout: p.layout,\n title: p.title,\n subtitle: p.subtitle,\n locale: p.locale,\n labels: p.labels,\n starters: p.starters,\n toolSteps: p.toolSteps,\n cards: toCards(p.cards, setPortals),\n })\n }, [props.apiUrl])\n\n // How it looks: applied without reconnecting.\n useEffect(() => {\n element.current?.setOptions({\n layout: props.layout,\n title: props.title,\n subtitle: props.subtitle,\n locale: props.locale,\n labels: props.labels,\n starters: props.starters,\n toolSteps: props.toolSteps,\n cards: toCards(props.cards, setPortals),\n })\n }, [\n props.layout,\n props.title,\n props.subtitle,\n props.locale,\n props.labels,\n props.starters,\n props.toolSteps,\n props.cards,\n ])\n\n useEffect(() => {\n const el = element.current\n if (!el || props.open === undefined) return\n if (props.open) el.show()\n else el.hide()\n }, [props.open])\n\n useEffect(() => {\n const el = element.current\n if (!el) return\n const handlers: Array<[string, (e: Event) => void]> = [\n ['ghuri:tool-result', (e) => latest.current.onToolResult?.((e as CustomEvent).detail)],\n ['ghuri:approval-pending', (e) => latest.current.onApprovalPending?.((e as CustomEvent).detail)],\n ['ghuri:turn-end', (e) => latest.current.onTurnEnd?.((e as CustomEvent).detail)],\n ['ghuri:open', () => latest.current.onOpen?.()],\n ['ghuri:close', () => latest.current.onClose?.()],\n ]\n for (const [name, fn] of handlers) el.addEventListener(name, fn)\n return () => {\n for (const [name, fn] of handlers) el.removeEventListener(name, fn)\n }\n }, [])\n\n return createElement(\n 'ghuri-chat',\n { ref: element, className: props.className, style: props.style, layout: props.layout ?? 'panel' },\n ...[...portals].map(([container, node]) => createPortal(node, container, container.slot)),\n )\n})\n"],"mappings":";;;;AA0CA,SAASA,EACP,GACA,GAC0B;CAC1B,IAAMC,IAAgC,CAAC;CACvC,KAAK,IAAM,CAACC,GAAMC,MAAS,OAAO,QAAQC,KAAS,CAAC,CAAC,GAAG;EACtD,IAAMC,KAAQ,MACZC,KACK,GAAwB,OACvBC,GAAY,MAAM,IA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