@percena/weft 0.4.0-next.3 → 0.4.0-next.5

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.
Files changed (81) hide show
  1. package/dist/action-bridge.cjs +172 -6
  2. package/dist/action-bridge.d.cts +112 -13
  3. package/dist/action-bridge.d.ts +112 -13
  4. package/dist/action-bridge.js +166 -4
  5. package/dist/chat.cjs +982 -3
  6. package/dist/chat.d.cts +30 -1
  7. package/dist/chat.d.ts +30 -1
  8. package/dist/chat.js +981 -2
  9. package/dist/index.cjs +341 -1616
  10. package/dist/index.d.cts +4 -274
  11. package/dist/index.d.ts +4 -274
  12. package/dist/index.js +174 -1408
  13. package/dist/styles/fonts/KaTeX_AMS-Regular.ttf +0 -0
  14. package/dist/styles/fonts/KaTeX_AMS-Regular.woff +0 -0
  15. package/dist/styles/fonts/KaTeX_AMS-Regular.woff2 +0 -0
  16. package/dist/styles/fonts/KaTeX_Caligraphic-Bold.ttf +0 -0
  17. package/dist/styles/fonts/KaTeX_Caligraphic-Bold.woff +0 -0
  18. package/dist/styles/fonts/KaTeX_Caligraphic-Bold.woff2 +0 -0
  19. package/dist/styles/fonts/KaTeX_Caligraphic-Regular.ttf +0 -0
  20. package/dist/styles/fonts/KaTeX_Caligraphic-Regular.woff +0 -0
  21. package/dist/styles/fonts/KaTeX_Caligraphic-Regular.woff2 +0 -0
  22. package/dist/styles/fonts/KaTeX_Fraktur-Bold.ttf +0 -0
  23. package/dist/styles/fonts/KaTeX_Fraktur-Bold.woff +0 -0
  24. package/dist/styles/fonts/KaTeX_Fraktur-Bold.woff2 +0 -0
  25. package/dist/styles/fonts/KaTeX_Fraktur-Regular.ttf +0 -0
  26. package/dist/styles/fonts/KaTeX_Fraktur-Regular.woff +0 -0
  27. package/dist/styles/fonts/KaTeX_Fraktur-Regular.woff2 +0 -0
  28. package/dist/styles/fonts/KaTeX_Main-Bold.ttf +0 -0
  29. package/dist/styles/fonts/KaTeX_Main-Bold.woff +0 -0
  30. package/dist/styles/fonts/KaTeX_Main-Bold.woff2 +0 -0
  31. package/dist/styles/fonts/KaTeX_Main-BoldItalic.ttf +0 -0
  32. package/dist/styles/fonts/KaTeX_Main-BoldItalic.woff +0 -0
  33. package/dist/styles/fonts/KaTeX_Main-BoldItalic.woff2 +0 -0
  34. package/dist/styles/fonts/KaTeX_Main-Italic.ttf +0 -0
  35. package/dist/styles/fonts/KaTeX_Main-Italic.woff +0 -0
  36. package/dist/styles/fonts/KaTeX_Main-Italic.woff2 +0 -0
  37. package/dist/styles/fonts/KaTeX_Main-Regular.ttf +0 -0
  38. package/dist/styles/fonts/KaTeX_Main-Regular.woff +0 -0
  39. package/dist/styles/fonts/KaTeX_Main-Regular.woff2 +0 -0
  40. package/dist/styles/fonts/KaTeX_Math-BoldItalic.ttf +0 -0
  41. package/dist/styles/fonts/KaTeX_Math-BoldItalic.woff +0 -0
  42. package/dist/styles/fonts/KaTeX_Math-BoldItalic.woff2 +0 -0
  43. package/dist/styles/fonts/KaTeX_Math-Italic.ttf +0 -0
  44. package/dist/styles/fonts/KaTeX_Math-Italic.woff +0 -0
  45. package/dist/styles/fonts/KaTeX_Math-Italic.woff2 +0 -0
  46. package/dist/styles/fonts/KaTeX_SansSerif-Bold.ttf +0 -0
  47. package/dist/styles/fonts/KaTeX_SansSerif-Bold.woff +0 -0
  48. package/dist/styles/fonts/KaTeX_SansSerif-Bold.woff2 +0 -0
  49. package/dist/styles/fonts/KaTeX_SansSerif-Italic.ttf +0 -0
  50. package/dist/styles/fonts/KaTeX_SansSerif-Italic.woff +0 -0
  51. package/dist/styles/fonts/KaTeX_SansSerif-Italic.woff2 +0 -0
  52. package/dist/styles/fonts/KaTeX_SansSerif-Regular.ttf +0 -0
  53. package/dist/styles/fonts/KaTeX_SansSerif-Regular.woff +0 -0
  54. package/dist/styles/fonts/KaTeX_SansSerif-Regular.woff2 +0 -0
  55. package/dist/styles/fonts/KaTeX_Script-Regular.ttf +0 -0
  56. package/dist/styles/fonts/KaTeX_Script-Regular.woff +0 -0
  57. package/dist/styles/fonts/KaTeX_Script-Regular.woff2 +0 -0
  58. package/dist/styles/fonts/KaTeX_Size1-Regular.ttf +0 -0
  59. package/dist/styles/fonts/KaTeX_Size1-Regular.woff +0 -0
  60. package/dist/styles/fonts/KaTeX_Size1-Regular.woff2 +0 -0
  61. package/dist/styles/fonts/KaTeX_Size2-Regular.ttf +0 -0
  62. package/dist/styles/fonts/KaTeX_Size2-Regular.woff +0 -0
  63. package/dist/styles/fonts/KaTeX_Size2-Regular.woff2 +0 -0
  64. package/dist/styles/fonts/KaTeX_Size3-Regular.ttf +0 -0
  65. package/dist/styles/fonts/KaTeX_Size3-Regular.woff +0 -0
  66. package/dist/styles/fonts/KaTeX_Size3-Regular.woff2 +0 -0
  67. package/dist/styles/fonts/KaTeX_Size4-Regular.ttf +0 -0
  68. package/dist/styles/fonts/KaTeX_Size4-Regular.woff +0 -0
  69. package/dist/styles/fonts/KaTeX_Size4-Regular.woff2 +0 -0
  70. package/dist/styles/fonts/KaTeX_Typewriter-Regular.ttf +0 -0
  71. package/dist/styles/fonts/KaTeX_Typewriter-Regular.woff +0 -0
  72. package/dist/styles/fonts/KaTeX_Typewriter-Regular.woff2 +0 -0
  73. package/package.json +2 -30
  74. package/dist/local-runtime.cjs +0 -1387
  75. package/dist/local-runtime.d.cts +0 -3314
  76. package/dist/local-runtime.d.ts +0 -3314
  77. package/dist/local-runtime.js +0 -1345
  78. package/dist/skills-browser.cjs +0 -118
  79. package/dist/skills-browser.d.cts +0 -105
  80. package/dist/skills-browser.d.ts +0 -105
  81. package/dist/skills-browser.js +0 -88
@@ -15,21 +15,187 @@ var __copyProps = (to, from, except, desc) => {
15
15
  }
16
16
  return to;
17
17
  };
18
- var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
19
18
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
20
19
 
21
20
  // src/action-bridge.ts
22
21
  var action_bridge_exports = {};
23
22
  __export(action_bridge_exports, {
24
- ActionReplayLayer: () => import_react.ActionReplayLayer,
25
- useActionBridge: () => import_react.useActionBridge
23
+ ActionReplayLayer: () => ActionReplayLayer,
24
+ DEFAULT_TIMING: () => DEFAULT_TIMING,
25
+ createActionBridge: () => createActionBridge,
26
+ useActionBridge: () => useActionBridge,
27
+ weftAction: () => weftAction,
28
+ weftActionSelector: () => weftActionSelector
26
29
  });
27
30
  module.exports = __toCommonJS(action_bridge_exports);
28
- __reExport(action_bridge_exports, require("@percena/weft-action-bridge"), module.exports);
29
- var import_react = require("@percena/weft-action-bridge/react");
31
+
32
+ // ../packages/action-bridge/src/index.ts
33
+ var DEFAULT_TIMING = {
34
+ cursorMoveMs: 550,
35
+ rippleMs: 450,
36
+ flashMs: 900,
37
+ cursorHideMs: 2600,
38
+ settleMs: 180
39
+ };
40
+ function weftAction(action, id) {
41
+ return { "data-weft-action": id === void 0 ? action : `${action}:${id}` };
42
+ }
43
+ function weftActionSelector(target) {
44
+ return `[data-weft-action=${JSON.stringify(target)}]`;
45
+ }
46
+ var FLASH_CLASS = "weft-action-flash";
47
+ var STYLE_ID = "weft-action-bridge-styles";
48
+ function injectStyles(doc, timing) {
49
+ if (doc.getElementById(STYLE_ID)) return;
50
+ const style = doc.createElement("style");
51
+ style.id = STYLE_ID;
52
+ style.textContent = `
53
+ .weft-ghost-cursor{position:fixed;left:0;top:0;z-index:9999;pointer-events:none;transition:transform ${timing.cursorMoveMs}ms cubic-bezier(.22,1,.36,1),opacity .3s ease}
54
+ .weft-ghost-cursor svg{display:block;filter:drop-shadow(0 1px 2px rgba(0,0,0,.35))}
55
+ .weft-ghost-cursor .weft-cursor-label{position:absolute;left:18px;top:18px;white-space:nowrap;background:rgba(30,30,40,.92);color:#fff;font:12px/1.6 system-ui,sans-serif;padding:2px 8px;border-radius:6px}
56
+ .weft-ghost-cursor .weft-cursor-ripple{position:absolute;left:-14px;top:-14px;width:28px;height:28px;border-radius:50%;border:2px solid rgba(120,90,220,.8);opacity:0;transform:scale(.3)}
57
+ .weft-ghost-cursor.weft-clicking .weft-cursor-ripple{animation:weft-cursor-ripple ${timing.rippleMs}ms ease-out}
58
+ @keyframes weft-cursor-ripple{0%{opacity:.9;transform:scale(.3)}100%{opacity:0;transform:scale(1.6)}}
59
+ .${FLASH_CLASS}{animation:weft-action-flash ${timing.flashMs}ms ease-out}
60
+ @keyframes weft-action-flash{0%{box-shadow:0 0 0 3px rgba(120,90,220,.65)}100%{box-shadow:0 0 0 3px rgba(120,90,220,0)}}
61
+ `;
62
+ doc.head.appendChild(style);
63
+ }
64
+ var sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
65
+ function createActionBridge(options = {}) {
66
+ const doc = options.document ?? (typeof document !== "undefined" ? document : void 0);
67
+ const cursorMode = options.cursor ?? "ghost";
68
+ const timing = { ...DEFAULT_TIMING, ...options.timing };
69
+ const labelPrefix = options.labelPrefix ?? "AI";
70
+ let queue = Promise.resolve();
71
+ let disposed = false;
72
+ let cursorEl = null;
73
+ let hideTimer;
74
+ const disconnects = [];
75
+ function ensureCursor() {
76
+ if (!doc || cursorMode !== "ghost") return null;
77
+ if (cursorEl) return cursorEl;
78
+ injectStyles(doc, timing);
79
+ cursorEl = doc.createElement("div");
80
+ cursorEl.className = "weft-ghost-cursor";
81
+ cursorEl.style.opacity = "0";
82
+ cursorEl.innerHTML = '<div class="weft-cursor-ripple"></div><svg width="20" height="20" viewBox="0 0 24 24"><path d="M4 2l16 8.5-7 1.8L9.4 19z" fill="#fff" stroke="#333" stroke-width="1.4"/></svg><span class="weft-cursor-label"></span>';
83
+ doc.body.appendChild(cursorEl);
84
+ return cursorEl;
85
+ }
86
+ async function replay(event) {
87
+ if (disposed || !doc) return;
88
+ const el = event.target ? doc.querySelector(weftActionSelector(event.target)) : null;
89
+ if (el && cursorMode !== "off") {
90
+ injectStyles(doc, timing);
91
+ const rect = el.getBoundingClientRect();
92
+ const cursor = ensureCursor();
93
+ if (cursor) {
94
+ const label = cursor.querySelector(".weft-cursor-label");
95
+ label.textContent = event.label ? `${labelPrefix} ${event.label}` : labelPrefix;
96
+ cursor.style.transform = `translate(${rect.left + rect.width / 2}px, ${rect.top + rect.height / 2}px)`;
97
+ cursor.style.opacity = "1";
98
+ await sleep(timing.cursorMoveMs + 70);
99
+ if (disposed) return;
100
+ cursor.classList.add("weft-clicking");
101
+ }
102
+ el.classList.add(FLASH_CLASS);
103
+ setTimeout(() => el.classList.remove(FLASH_CLASS), timing.flashMs + 50);
104
+ await sleep(timing.rippleMs);
105
+ cursor?.classList.remove("weft-clicking");
106
+ }
107
+ await options.onReplayed?.(event);
108
+ await sleep(timing.settleMs);
109
+ if (cursorEl) {
110
+ if (hideTimer) clearTimeout(hideTimer);
111
+ hideTimer = setTimeout(() => {
112
+ if (cursorEl) cursorEl.style.opacity = "0";
113
+ }, timing.cursorHideMs);
114
+ }
115
+ }
116
+ function accepts(event) {
117
+ if (event.actor !== "agent") return false;
118
+ if (options.session && event.session && event.session !== options.session) return false;
119
+ return true;
120
+ }
121
+ return {
122
+ dispatch(event) {
123
+ if (disposed || !accepts(event)) return;
124
+ queue = queue.then(() => replay(event)).catch(() => {
125
+ });
126
+ },
127
+ connect(source, connectOptions) {
128
+ const eventName = connectOptions?.eventName ?? "weft-action";
129
+ const map = connectOptions?.map ?? ((raw) => raw);
130
+ const listener = (e) => {
131
+ let raw;
132
+ try {
133
+ raw = JSON.parse(e.data);
134
+ } catch {
135
+ return;
136
+ }
137
+ const event = map(raw);
138
+ if (event) this.dispatch(event);
139
+ };
140
+ source.addEventListener(eventName, listener);
141
+ const disconnect = () => source.removeEventListener?.(eventName, listener);
142
+ disconnects.push(disconnect);
143
+ return disconnect;
144
+ },
145
+ flush() {
146
+ return queue;
147
+ },
148
+ dispose() {
149
+ disposed = true;
150
+ for (const disconnect of disconnects) disconnect();
151
+ if (hideTimer) clearTimeout(hideTimer);
152
+ cursorEl?.remove();
153
+ cursorEl = null;
154
+ doc?.getElementById(STYLE_ID)?.remove();
155
+ }
156
+ };
157
+ }
158
+
159
+ // ../packages/action-bridge/src/react.tsx
160
+ var import_react = require("react");
161
+ function useActionBridge(options = {}) {
162
+ const bridgeRef = (0, import_react.useRef)(null);
163
+ const optionsRef = (0, import_react.useRef)(options);
164
+ optionsRef.current = options;
165
+ if (!bridgeRef.current) {
166
+ bridgeRef.current = createActionBridge({
167
+ ...options,
168
+ onReplayed: (event) => optionsRef.current.onReplayed?.(event)
169
+ });
170
+ }
171
+ (0, import_react.useEffect)(() => {
172
+ return () => {
173
+ bridgeRef.current?.dispose();
174
+ bridgeRef.current = null;
175
+ };
176
+ }, []);
177
+ return bridgeRef.current;
178
+ }
179
+ function ActionReplayLayer(props) {
180
+ const { source, eventName, map, ...bridgeOptions } = props;
181
+ const bridge = useActionBridge(bridgeOptions);
182
+ const mapRef = (0, import_react.useRef)(map);
183
+ mapRef.current = map;
184
+ (0, import_react.useEffect)(() => {
185
+ if (!source) return;
186
+ return bridge.connect(source, {
187
+ eventName,
188
+ map: mapRef.current ? (raw) => mapRef.current?.(raw) ?? null : void 0
189
+ });
190
+ }, [bridge, source, eventName]);
191
+ return null;
192
+ }
30
193
  // Annotate the CommonJS export names for ESM import in node:
31
194
  0 && (module.exports = {
32
195
  ActionReplayLayer,
196
+ DEFAULT_TIMING,
197
+ createActionBridge,
33
198
  useActionBridge,
34
- ...require("@percena/weft-action-bridge")
199
+ weftAction,
200
+ weftActionSelector
35
201
  });
@@ -1,13 +1,112 @@
1
- export * from '@percena/weft-action-bridge';
2
- export { ActionReplayLayer, ActionReplayLayerProps, useActionBridge } from '@percena/weft-action-bridge/react';
3
- import '@weft/ui';
4
- import '@weft/local-chat';
5
- import '@weft/ui/lib/en-fallback';
6
- import '@percena/weft-react';
7
- import '@weft/core';
8
- import '@weft/timeline';
9
- import '@weft/runtime-core';
10
- import '@weft/policy';
11
- import '@weft/provider-flitro';
12
- import '@weft/local-runtime/browser';
13
- import '@weft/skills/browser';
1
+ /**
2
+ * @percena/weft-action-bridge — productized action replay.
3
+ *
4
+ * Hosts annotate clickable elements with `data-weft-action` (via
5
+ * `weftAction(...)`), push standard action descriptors (usually derived from
6
+ * their own business event stream, with `actor` taken from the
7
+ * `X-Weft-Actor` header Flitro stamps on agent tool calls), and the bridge
8
+ * replays each agent action on the live page: a ghost cursor moves to the
9
+ * element, clicks (ripple), the element flashes, then `onReplayed` fires so
10
+ * the host can refresh its data. Elements that are not on screen degrade
11
+ * gracefully: the animation is skipped but `onReplayed` still fires.
12
+ *
13
+ * Framework-agnostic and dependency-free; React glue lives in
14
+ * `@percena/weft-action-bridge/react`.
15
+ */
16
+ /** Standard action descriptor (optimization plan §6.1). */
17
+ interface WeftActionEvent {
18
+ /** `agent` events are replayed; anything else is passed through. */
19
+ actor: string;
20
+ /** Business semantic, host-defined (e.g. `cart.item_added`). */
21
+ action?: string;
22
+ /** Matches `data-weft-action` on the page (e.g. `add-to-cart:p1`). */
23
+ target?: string | null;
24
+ /** Cursor bubble copy (e.g. `加入购物车:机械键盘`). */
25
+ label?: string;
26
+ /** Scopes replay to one panel/session when several are mounted. */
27
+ session?: string;
28
+ }
29
+ type CursorMode = 'ghost' | 'highlight' | 'off';
30
+ /** Replay choreography (defaults match the reference shop demo). */
31
+ interface ActionBridgeTiming {
32
+ cursorMoveMs: number;
33
+ rippleMs: number;
34
+ flashMs: number;
35
+ cursorHideMs: number;
36
+ /** Pause after `onReplayed` before the next queued event. */
37
+ settleMs: number;
38
+ }
39
+ declare const DEFAULT_TIMING: ActionBridgeTiming;
40
+ interface EventSourceLike {
41
+ addEventListener(type: string, listener: (event: {
42
+ data: string;
43
+ }) => void): void;
44
+ removeEventListener?(type: string, listener: (event: {
45
+ data: string;
46
+ }) => void): void;
47
+ }
48
+ interface ActionBridgeOptions {
49
+ /** `ghost` (cursor + ripple + flash), `highlight` (flash only), `off`. */
50
+ cursor?: CursorMode;
51
+ /** Only replay events whose `session` matches (when both are set). */
52
+ session?: string;
53
+ /** Called after each agent event (replayed or degraded) settles. */
54
+ onReplayed?: (event: WeftActionEvent) => void | Promise<void>;
55
+ timing?: Partial<ActionBridgeTiming>;
56
+ /** Cursor bubble prefix (default `AI`). */
57
+ labelPrefix?: string;
58
+ document?: Document;
59
+ }
60
+ interface ConnectOptions<TRaw = unknown> {
61
+ /** SSE event name to listen for (default `weft-action`). */
62
+ eventName?: string;
63
+ /**
64
+ * Translate the host's raw event payload into a descriptor. Return null to
65
+ * ignore the event. Defaults to treating the payload as a descriptor.
66
+ */
67
+ map?: (raw: TRaw) => WeftActionEvent | null;
68
+ }
69
+ interface ActionBridge {
70
+ /** Queue one descriptor for replay (agent events only). */
71
+ dispatch(event: WeftActionEvent): void;
72
+ /** Subscribe to an EventSource/WebSocket-like emitter of descriptors. */
73
+ connect<TRaw = unknown>(source: EventSourceLike, options?: ConnectOptions<TRaw>): () => void;
74
+ /** Wait until everything queued so far has been replayed. */
75
+ flush(): Promise<void>;
76
+ dispose(): void;
77
+ }
78
+ /** Build the `data-weft-action` attribute: `weftAction('add-to-cart', 'p1')`. */
79
+ declare function weftAction(action: string, id?: string | number): {
80
+ 'data-weft-action': string;
81
+ };
82
+ /** Selector for a descriptor target. */
83
+ declare function weftActionSelector(target: string): string;
84
+ declare function createActionBridge(options?: ActionBridgeOptions): ActionBridge;
85
+
86
+ /**
87
+ * @percena/weft-action-bridge/react — React glue for the action bridge.
88
+ *
89
+ * ```tsx
90
+ * <button {...weftAction('add-to-cart', product.id)}>加入购物车</button>
91
+ * <ActionReplayLayer source={eventSource} eventName="shop.event"
92
+ * map={toWeftActionEvent} onReplayed={refresh} />
93
+ * ```
94
+ */
95
+
96
+ interface ActionReplayLayerProps<TRaw = unknown> extends Omit<ActionBridgeOptions, 'document'> {
97
+ /** Event emitter carrying action descriptors (EventSource, WebSocket, …). */
98
+ source?: EventSourceLike;
99
+ /** SSE event name on `source` (default `weft-action`). */
100
+ eventName?: string;
101
+ /** Translate raw payloads into descriptors; return null to ignore. */
102
+ map?: ConnectOptions<TRaw>['map'];
103
+ }
104
+ /** Hook variant: returns the bridge so the host can `dispatch` directly. */
105
+ declare function useActionBridge(options?: Omit<ActionBridgeOptions, 'document'>): ActionBridge;
106
+ /**
107
+ * Mounts an action bridge for the lifetime of the component and subscribes it
108
+ * to `source`. Renders nothing — the ghost cursor manages its own DOM.
109
+ */
110
+ declare function ActionReplayLayer<TRaw = unknown>(props: ActionReplayLayerProps<TRaw>): null;
111
+
112
+ export { type ActionBridge, type ActionBridgeOptions, type ActionBridgeTiming, ActionReplayLayer, type ActionReplayLayerProps, type ConnectOptions, type CursorMode, DEFAULT_TIMING, type EventSourceLike, type WeftActionEvent, createActionBridge, useActionBridge, weftAction, weftActionSelector };
@@ -1,13 +1,112 @@
1
- export * from '@percena/weft-action-bridge';
2
- export { ActionReplayLayer, ActionReplayLayerProps, useActionBridge } from '@percena/weft-action-bridge/react';
3
- import '@weft/ui';
4
- import '@weft/local-chat';
5
- import '@weft/ui/lib/en-fallback';
6
- import '@percena/weft-react';
7
- import '@weft/core';
8
- import '@weft/timeline';
9
- import '@weft/runtime-core';
10
- import '@weft/policy';
11
- import '@weft/provider-flitro';
12
- import '@weft/local-runtime/browser';
13
- import '@weft/skills/browser';
1
+ /**
2
+ * @percena/weft-action-bridge — productized action replay.
3
+ *
4
+ * Hosts annotate clickable elements with `data-weft-action` (via
5
+ * `weftAction(...)`), push standard action descriptors (usually derived from
6
+ * their own business event stream, with `actor` taken from the
7
+ * `X-Weft-Actor` header Flitro stamps on agent tool calls), and the bridge
8
+ * replays each agent action on the live page: a ghost cursor moves to the
9
+ * element, clicks (ripple), the element flashes, then `onReplayed` fires so
10
+ * the host can refresh its data. Elements that are not on screen degrade
11
+ * gracefully: the animation is skipped but `onReplayed` still fires.
12
+ *
13
+ * Framework-agnostic and dependency-free; React glue lives in
14
+ * `@percena/weft-action-bridge/react`.
15
+ */
16
+ /** Standard action descriptor (optimization plan §6.1). */
17
+ interface WeftActionEvent {
18
+ /** `agent` events are replayed; anything else is passed through. */
19
+ actor: string;
20
+ /** Business semantic, host-defined (e.g. `cart.item_added`). */
21
+ action?: string;
22
+ /** Matches `data-weft-action` on the page (e.g. `add-to-cart:p1`). */
23
+ target?: string | null;
24
+ /** Cursor bubble copy (e.g. `加入购物车:机械键盘`). */
25
+ label?: string;
26
+ /** Scopes replay to one panel/session when several are mounted. */
27
+ session?: string;
28
+ }
29
+ type CursorMode = 'ghost' | 'highlight' | 'off';
30
+ /** Replay choreography (defaults match the reference shop demo). */
31
+ interface ActionBridgeTiming {
32
+ cursorMoveMs: number;
33
+ rippleMs: number;
34
+ flashMs: number;
35
+ cursorHideMs: number;
36
+ /** Pause after `onReplayed` before the next queued event. */
37
+ settleMs: number;
38
+ }
39
+ declare const DEFAULT_TIMING: ActionBridgeTiming;
40
+ interface EventSourceLike {
41
+ addEventListener(type: string, listener: (event: {
42
+ data: string;
43
+ }) => void): void;
44
+ removeEventListener?(type: string, listener: (event: {
45
+ data: string;
46
+ }) => void): void;
47
+ }
48
+ interface ActionBridgeOptions {
49
+ /** `ghost` (cursor + ripple + flash), `highlight` (flash only), `off`. */
50
+ cursor?: CursorMode;
51
+ /** Only replay events whose `session` matches (when both are set). */
52
+ session?: string;
53
+ /** Called after each agent event (replayed or degraded) settles. */
54
+ onReplayed?: (event: WeftActionEvent) => void | Promise<void>;
55
+ timing?: Partial<ActionBridgeTiming>;
56
+ /** Cursor bubble prefix (default `AI`). */
57
+ labelPrefix?: string;
58
+ document?: Document;
59
+ }
60
+ interface ConnectOptions<TRaw = unknown> {
61
+ /** SSE event name to listen for (default `weft-action`). */
62
+ eventName?: string;
63
+ /**
64
+ * Translate the host's raw event payload into a descriptor. Return null to
65
+ * ignore the event. Defaults to treating the payload as a descriptor.
66
+ */
67
+ map?: (raw: TRaw) => WeftActionEvent | null;
68
+ }
69
+ interface ActionBridge {
70
+ /** Queue one descriptor for replay (agent events only). */
71
+ dispatch(event: WeftActionEvent): void;
72
+ /** Subscribe to an EventSource/WebSocket-like emitter of descriptors. */
73
+ connect<TRaw = unknown>(source: EventSourceLike, options?: ConnectOptions<TRaw>): () => void;
74
+ /** Wait until everything queued so far has been replayed. */
75
+ flush(): Promise<void>;
76
+ dispose(): void;
77
+ }
78
+ /** Build the `data-weft-action` attribute: `weftAction('add-to-cart', 'p1')`. */
79
+ declare function weftAction(action: string, id?: string | number): {
80
+ 'data-weft-action': string;
81
+ };
82
+ /** Selector for a descriptor target. */
83
+ declare function weftActionSelector(target: string): string;
84
+ declare function createActionBridge(options?: ActionBridgeOptions): ActionBridge;
85
+
86
+ /**
87
+ * @percena/weft-action-bridge/react — React glue for the action bridge.
88
+ *
89
+ * ```tsx
90
+ * <button {...weftAction('add-to-cart', product.id)}>加入购物车</button>
91
+ * <ActionReplayLayer source={eventSource} eventName="shop.event"
92
+ * map={toWeftActionEvent} onReplayed={refresh} />
93
+ * ```
94
+ */
95
+
96
+ interface ActionReplayLayerProps<TRaw = unknown> extends Omit<ActionBridgeOptions, 'document'> {
97
+ /** Event emitter carrying action descriptors (EventSource, WebSocket, …). */
98
+ source?: EventSourceLike;
99
+ /** SSE event name on `source` (default `weft-action`). */
100
+ eventName?: string;
101
+ /** Translate raw payloads into descriptors; return null to ignore. */
102
+ map?: ConnectOptions<TRaw>['map'];
103
+ }
104
+ /** Hook variant: returns the bridge so the host can `dispatch` directly. */
105
+ declare function useActionBridge(options?: Omit<ActionBridgeOptions, 'document'>): ActionBridge;
106
+ /**
107
+ * Mounts an action bridge for the lifetime of the component and subscribes it
108
+ * to `source`. Renders nothing — the ghost cursor manages its own DOM.
109
+ */
110
+ declare function ActionReplayLayer<TRaw = unknown>(props: ActionReplayLayerProps<TRaw>): null;
111
+
112
+ export { type ActionBridge, type ActionBridgeOptions, type ActionBridgeTiming, ActionReplayLayer, type ActionReplayLayerProps, type ConnectOptions, type CursorMode, DEFAULT_TIMING, type EventSourceLike, type WeftActionEvent, createActionBridge, useActionBridge, weftAction, weftActionSelector };
@@ -1,7 +1,169 @@
1
- // src/action-bridge.ts
2
- export * from "@percena/weft-action-bridge";
3
- import { ActionReplayLayer, useActionBridge } from "@percena/weft-action-bridge/react";
1
+ // ../packages/action-bridge/src/index.ts
2
+ var DEFAULT_TIMING = {
3
+ cursorMoveMs: 550,
4
+ rippleMs: 450,
5
+ flashMs: 900,
6
+ cursorHideMs: 2600,
7
+ settleMs: 180
8
+ };
9
+ function weftAction(action, id) {
10
+ return { "data-weft-action": id === void 0 ? action : `${action}:${id}` };
11
+ }
12
+ function weftActionSelector(target) {
13
+ return `[data-weft-action=${JSON.stringify(target)}]`;
14
+ }
15
+ var FLASH_CLASS = "weft-action-flash";
16
+ var STYLE_ID = "weft-action-bridge-styles";
17
+ function injectStyles(doc, timing) {
18
+ if (doc.getElementById(STYLE_ID)) return;
19
+ const style = doc.createElement("style");
20
+ style.id = STYLE_ID;
21
+ style.textContent = `
22
+ .weft-ghost-cursor{position:fixed;left:0;top:0;z-index:9999;pointer-events:none;transition:transform ${timing.cursorMoveMs}ms cubic-bezier(.22,1,.36,1),opacity .3s ease}
23
+ .weft-ghost-cursor svg{display:block;filter:drop-shadow(0 1px 2px rgba(0,0,0,.35))}
24
+ .weft-ghost-cursor .weft-cursor-label{position:absolute;left:18px;top:18px;white-space:nowrap;background:rgba(30,30,40,.92);color:#fff;font:12px/1.6 system-ui,sans-serif;padding:2px 8px;border-radius:6px}
25
+ .weft-ghost-cursor .weft-cursor-ripple{position:absolute;left:-14px;top:-14px;width:28px;height:28px;border-radius:50%;border:2px solid rgba(120,90,220,.8);opacity:0;transform:scale(.3)}
26
+ .weft-ghost-cursor.weft-clicking .weft-cursor-ripple{animation:weft-cursor-ripple ${timing.rippleMs}ms ease-out}
27
+ @keyframes weft-cursor-ripple{0%{opacity:.9;transform:scale(.3)}100%{opacity:0;transform:scale(1.6)}}
28
+ .${FLASH_CLASS}{animation:weft-action-flash ${timing.flashMs}ms ease-out}
29
+ @keyframes weft-action-flash{0%{box-shadow:0 0 0 3px rgba(120,90,220,.65)}100%{box-shadow:0 0 0 3px rgba(120,90,220,0)}}
30
+ `;
31
+ doc.head.appendChild(style);
32
+ }
33
+ var sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
34
+ function createActionBridge(options = {}) {
35
+ const doc = options.document ?? (typeof document !== "undefined" ? document : void 0);
36
+ const cursorMode = options.cursor ?? "ghost";
37
+ const timing = { ...DEFAULT_TIMING, ...options.timing };
38
+ const labelPrefix = options.labelPrefix ?? "AI";
39
+ let queue = Promise.resolve();
40
+ let disposed = false;
41
+ let cursorEl = null;
42
+ let hideTimer;
43
+ const disconnects = [];
44
+ function ensureCursor() {
45
+ if (!doc || cursorMode !== "ghost") return null;
46
+ if (cursorEl) return cursorEl;
47
+ injectStyles(doc, timing);
48
+ cursorEl = doc.createElement("div");
49
+ cursorEl.className = "weft-ghost-cursor";
50
+ cursorEl.style.opacity = "0";
51
+ cursorEl.innerHTML = '<div class="weft-cursor-ripple"></div><svg width="20" height="20" viewBox="0 0 24 24"><path d="M4 2l16 8.5-7 1.8L9.4 19z" fill="#fff" stroke="#333" stroke-width="1.4"/></svg><span class="weft-cursor-label"></span>';
52
+ doc.body.appendChild(cursorEl);
53
+ return cursorEl;
54
+ }
55
+ async function replay(event) {
56
+ if (disposed || !doc) return;
57
+ const el = event.target ? doc.querySelector(weftActionSelector(event.target)) : null;
58
+ if (el && cursorMode !== "off") {
59
+ injectStyles(doc, timing);
60
+ const rect = el.getBoundingClientRect();
61
+ const cursor = ensureCursor();
62
+ if (cursor) {
63
+ const label = cursor.querySelector(".weft-cursor-label");
64
+ label.textContent = event.label ? `${labelPrefix} ${event.label}` : labelPrefix;
65
+ cursor.style.transform = `translate(${rect.left + rect.width / 2}px, ${rect.top + rect.height / 2}px)`;
66
+ cursor.style.opacity = "1";
67
+ await sleep(timing.cursorMoveMs + 70);
68
+ if (disposed) return;
69
+ cursor.classList.add("weft-clicking");
70
+ }
71
+ el.classList.add(FLASH_CLASS);
72
+ setTimeout(() => el.classList.remove(FLASH_CLASS), timing.flashMs + 50);
73
+ await sleep(timing.rippleMs);
74
+ cursor?.classList.remove("weft-clicking");
75
+ }
76
+ await options.onReplayed?.(event);
77
+ await sleep(timing.settleMs);
78
+ if (cursorEl) {
79
+ if (hideTimer) clearTimeout(hideTimer);
80
+ hideTimer = setTimeout(() => {
81
+ if (cursorEl) cursorEl.style.opacity = "0";
82
+ }, timing.cursorHideMs);
83
+ }
84
+ }
85
+ function accepts(event) {
86
+ if (event.actor !== "agent") return false;
87
+ if (options.session && event.session && event.session !== options.session) return false;
88
+ return true;
89
+ }
90
+ return {
91
+ dispatch(event) {
92
+ if (disposed || !accepts(event)) return;
93
+ queue = queue.then(() => replay(event)).catch(() => {
94
+ });
95
+ },
96
+ connect(source, connectOptions) {
97
+ const eventName = connectOptions?.eventName ?? "weft-action";
98
+ const map = connectOptions?.map ?? ((raw) => raw);
99
+ const listener = (e) => {
100
+ let raw;
101
+ try {
102
+ raw = JSON.parse(e.data);
103
+ } catch {
104
+ return;
105
+ }
106
+ const event = map(raw);
107
+ if (event) this.dispatch(event);
108
+ };
109
+ source.addEventListener(eventName, listener);
110
+ const disconnect = () => source.removeEventListener?.(eventName, listener);
111
+ disconnects.push(disconnect);
112
+ return disconnect;
113
+ },
114
+ flush() {
115
+ return queue;
116
+ },
117
+ dispose() {
118
+ disposed = true;
119
+ for (const disconnect of disconnects) disconnect();
120
+ if (hideTimer) clearTimeout(hideTimer);
121
+ cursorEl?.remove();
122
+ cursorEl = null;
123
+ doc?.getElementById(STYLE_ID)?.remove();
124
+ }
125
+ };
126
+ }
127
+
128
+ // ../packages/action-bridge/src/react.tsx
129
+ import { useEffect, useRef } from "react";
130
+ function useActionBridge(options = {}) {
131
+ const bridgeRef = useRef(null);
132
+ const optionsRef = useRef(options);
133
+ optionsRef.current = options;
134
+ if (!bridgeRef.current) {
135
+ bridgeRef.current = createActionBridge({
136
+ ...options,
137
+ onReplayed: (event) => optionsRef.current.onReplayed?.(event)
138
+ });
139
+ }
140
+ useEffect(() => {
141
+ return () => {
142
+ bridgeRef.current?.dispose();
143
+ bridgeRef.current = null;
144
+ };
145
+ }, []);
146
+ return bridgeRef.current;
147
+ }
148
+ function ActionReplayLayer(props) {
149
+ const { source, eventName, map, ...bridgeOptions } = props;
150
+ const bridge = useActionBridge(bridgeOptions);
151
+ const mapRef = useRef(map);
152
+ mapRef.current = map;
153
+ useEffect(() => {
154
+ if (!source) return;
155
+ return bridge.connect(source, {
156
+ eventName,
157
+ map: mapRef.current ? (raw) => mapRef.current?.(raw) ?? null : void 0
158
+ });
159
+ }, [bridge, source, eventName]);
160
+ return null;
161
+ }
4
162
  export {
5
163
  ActionReplayLayer,
6
- useActionBridge
164
+ DEFAULT_TIMING,
165
+ createActionBridge,
166
+ useActionBridge,
167
+ weftAction,
168
+ weftActionSelector
7
169
  };