@mydatavalue/polter 0.1.2 → 0.1.4

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 CHANGED
@@ -79,6 +79,28 @@ import { z } from 'zod';
79
79
  </AgentAction>
80
80
  ```
81
81
 
82
+ **Modal interactions** — click a button to open a modal, type a value, then confirm:
83
+
84
+ ```tsx
85
+ // Parent — 3-step flow: open → type → confirm
86
+ <AgentAction name="apply_discount" parameters={z.object({ pct: z.number() })}
87
+ onExecute={async () => { /* await async work started by the confirm click */ }}>
88
+ <AgentStep label="Open settings">
89
+ <SettingsButton />
90
+ </AgentStep>
91
+ <AgentStep label="Set value" fromTarget="discount-input" setParam="pct" />
92
+ <AgentStep label="Confirm" fromTarget="confirm-btn" />
93
+ </AgentAction>
94
+
95
+ // Child (modal) — targets with prepareView to set up internal state
96
+ <AgentTarget name="discount-input" prepareView={() => setMode("custom")}>
97
+ <Input value={value} onChange={...} />
98
+ </AgentTarget>
99
+ <AgentTarget name="confirm-btn">
100
+ <ConfirmButton />
101
+ </AgentTarget>
102
+ ```
103
+
82
104
  ### 3. Connect to your agent
83
105
 
84
106
  ```tsx
package/dist/index.d.mts CHANGED
@@ -73,6 +73,8 @@ interface AgentTargetEntry {
73
73
  value?: string;
74
74
  /** Named target key — used for static lazy resolution via `fromTarget`. */
75
75
  name?: string;
76
+ /** Run a callback to prepare component state before the agent interacts with this target. */
77
+ prepareView?: (params: Record<string, unknown>) => void | Promise<void>;
76
78
  }
77
79
  interface RegisteredAction {
78
80
  name: string;
@@ -119,7 +121,7 @@ interface ExecutorConfig {
119
121
  /** Resolve an element from the AgentTarget registry. Used by fromParam steps. */
120
122
  resolveTarget?: (actionName: string, param: string, value: string, signal?: AbortSignal) => Promise<HTMLElement | null>;
121
123
  /** Resolve a named target from the AgentTarget registry. Used by fromTarget steps. */
122
- resolveNamedTarget?: (actionName: string, name: string, signal?: AbortSignal) => Promise<HTMLElement | null>;
124
+ resolveNamedTarget?: (actionName: string, name: string, signal?: AbortSignal, params?: Record<string, unknown>) => Promise<HTMLElement | null>;
123
125
  }
124
126
  interface AgentActionProviderProps {
125
127
  mode?: ExecutionMode;
@@ -196,6 +198,8 @@ interface AgentTargetProps {
196
198
  value?: string;
197
199
  /** Named target key (for fromTarget resolution — static elements inside popovers/dropdowns). */
198
200
  name?: string;
201
+ /** Run a callback to prepare component state before the agent interacts with this target. Runs in the child's scope so it can access internal state. */
202
+ prepareView?: (params: Record<string, unknown>) => void | Promise<void>;
199
203
  }
200
204
  /**
201
205
  * Register a DOM element as a selectable target for an agent action step.
@@ -219,7 +223,7 @@ interface AgentTargetProps {
219
223
  * </AgentTarget>
220
224
  * ```
221
225
  */
222
- declare function AgentTarget({ action, param, value, name, children }: AgentTargetProps): react_jsx_runtime.JSX.Element;
226
+ declare function AgentTarget({ action, param, value, name, prepareView, children }: AgentTargetProps): react_jsx_runtime.JSX.Element;
223
227
 
224
228
  interface AgentDevToolsProps {
225
229
  /** Default open state. */
package/dist/index.d.ts CHANGED
@@ -73,6 +73,8 @@ interface AgentTargetEntry {
73
73
  value?: string;
74
74
  /** Named target key — used for static lazy resolution via `fromTarget`. */
75
75
  name?: string;
76
+ /** Run a callback to prepare component state before the agent interacts with this target. */
77
+ prepareView?: (params: Record<string, unknown>) => void | Promise<void>;
76
78
  }
77
79
  interface RegisteredAction {
78
80
  name: string;
@@ -119,7 +121,7 @@ interface ExecutorConfig {
119
121
  /** Resolve an element from the AgentTarget registry. Used by fromParam steps. */
120
122
  resolveTarget?: (actionName: string, param: string, value: string, signal?: AbortSignal) => Promise<HTMLElement | null>;
121
123
  /** Resolve a named target from the AgentTarget registry. Used by fromTarget steps. */
122
- resolveNamedTarget?: (actionName: string, name: string, signal?: AbortSignal) => Promise<HTMLElement | null>;
124
+ resolveNamedTarget?: (actionName: string, name: string, signal?: AbortSignal, params?: Record<string, unknown>) => Promise<HTMLElement | null>;
123
125
  }
124
126
  interface AgentActionProviderProps {
125
127
  mode?: ExecutionMode;
@@ -196,6 +198,8 @@ interface AgentTargetProps {
196
198
  value?: string;
197
199
  /** Named target key (for fromTarget resolution — static elements inside popovers/dropdowns). */
198
200
  name?: string;
201
+ /** Run a callback to prepare component state before the agent interacts with this target. Runs in the child's scope so it can access internal state. */
202
+ prepareView?: (params: Record<string, unknown>) => void | Promise<void>;
199
203
  }
200
204
  /**
201
205
  * Register a DOM element as a selectable target for an agent action step.
@@ -219,7 +223,7 @@ interface AgentTargetProps {
219
223
  * </AgentTarget>
220
224
  * ```
221
225
  */
222
- declare function AgentTarget({ action, param, value, name, children }: AgentTargetProps): react_jsx_runtime.JSX.Element;
226
+ declare function AgentTarget({ action, param, value, name, prepareView, children }: AgentTargetProps): react_jsx_runtime.JSX.Element;
223
227
 
224
228
  interface AgentDevToolsProps {
225
229
  /** Default open state. */
package/dist/index.js CHANGED
@@ -330,7 +330,7 @@ async function resolveStepElement(target, actionName, params, config) {
330
330
  return config.resolveTarget(actionName, target.fromParam, paramValue, config.signal);
331
331
  }
332
332
  if (target.fromTarget && config.resolveNamedTarget) {
333
- return config.resolveNamedTarget(actionName, target.fromTarget, config.signal);
333
+ return config.resolveNamedTarget(actionName, target.fromTarget, config.signal, params);
334
334
  }
335
335
  return target.element;
336
336
  }
@@ -542,7 +542,7 @@ function AgentActionProvider({
542
542
  []
543
543
  );
544
544
  const resolveNamedTarget = react.useCallback(
545
- async (actionName, name, signal) => {
545
+ async (actionName, name, signal, params) => {
546
546
  const maxWait = 3e3;
547
547
  const pollInterval = 50;
548
548
  const start = Date.now();
@@ -550,6 +550,9 @@ function AgentActionProvider({
550
550
  if (signal?.aborted) return null;
551
551
  for (const entry of targetsRef.current.values()) {
552
552
  if ((!entry.action || entry.action === actionName) && entry.name === name && entry.element.isConnected) {
553
+ if (entry.prepareView && params) {
554
+ await entry.prepareView(params);
555
+ }
553
556
  return entry.element;
554
557
  }
555
558
  }
@@ -848,10 +851,12 @@ function AgentStep({
848
851
  if (!children) return null;
849
852
  return /* @__PURE__ */ jsxRuntime.jsx("div", { ref: wrapperRef, style: { display: "contents" }, children });
850
853
  }
851
- function AgentTarget({ action, param, value, name, children }) {
854
+ function AgentTarget({ action, param, value, name, prepareView, children }) {
852
855
  const id = react.useId();
853
856
  const wrapperRef = react.useRef(null);
854
857
  const context = react.useContext(AgentActionContext);
858
+ const prepareViewRef = react.useRef(prepareView);
859
+ prepareViewRef.current = prepareView;
855
860
  if (!context) {
856
861
  throw new Error("AgentTarget must be used within an AgentActionProvider");
857
862
  }
@@ -859,7 +864,7 @@ function AgentTarget({ action, param, value, name, children }) {
859
864
  react.useEffect(() => {
860
865
  const element = wrapperRef.current?.firstElementChild;
861
866
  if (element) {
862
- registerTarget(id, { action, param, value, name, element });
867
+ registerTarget(id, { action, param, value, name, element, prepareView: prepareViewRef.current });
863
868
  }
864
869
  return () => unregisterTarget(id);
865
870
  }, [id, action, param, value, name, registerTarget, unregisterTarget]);