a3s-flow 1.1.0

Durable workflow engine and Rust SDK for A3S
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
import {
  createContext,
  useContext,
  useEffect,
  useId,
  useMemo,
  useRef,
  useState,
  type InputHTMLAttributes,
  type KeyboardEvent,
  type ReactNode,
  type RefObject,
  type TextareaHTMLAttributes,
} from 'react';
import { DesignerIcon } from './designer-icons';

export type A3SFlowExpressionVariableGroup = 'input' | 'global' | 'upstream' | 'scope';

export interface A3SFlowExpressionVariable {
  dataType?: string;
  description?: string;
  group: A3SFlowExpressionVariableGroup;
  label: string;
  nodeId?: string;
  path: string;
}

export const A3S_FLOW_DEFAULT_EXPRESSION_VARIABLES: readonly A3SFlowExpressionVariable[] = [
  {
    group: 'input',
    label: 'Workflow input',
    path: 'input',
    dataType: 'object',
  },
  {
    group: 'global',
    label: 'Workflow run ID',
    path: 'global.run_id',
    dataType: 'string',
  },
  {
    group: 'global',
    label: 'Workflow name',
    path: 'global.workflow_name',
    dataType: 'string',
  },
];

const A3SFlowExpressionVariablesContext = createContext<
  readonly A3SFlowExpressionVariable[] | undefined
>(undefined);

export function A3SFlowExpressionVariablesProvider({
  children,
  variables,
}: {
  children?: ReactNode;
  variables?: readonly A3SFlowExpressionVariable[];
}) {
  return (
    <A3SFlowExpressionVariablesContext.Provider value={variables}>
      {children}
    </A3SFlowExpressionVariablesContext.Provider>
  );
}

export function useA3SFlowExpressionVariables(
  variables?: readonly A3SFlowExpressionVariable[],
): readonly A3SFlowExpressionVariable[] {
  const providedVariables = useContext(A3SFlowExpressionVariablesContext);
  return variables ?? providedVariables ?? A3S_FLOW_DEFAULT_EXPRESSION_VARIABLES;
}

function isChinese(locale: string): boolean {
  return locale.toLocaleLowerCase().startsWith('zh');
}

function normalizedSearch(value: string): string {
  return value.trim().replace(/^\$/u, '').toLocaleLowerCase();
}

export function filterA3SFlowExpressionVariables(
  variables: readonly A3SFlowExpressionVariable[],
  query: string,
): A3SFlowExpressionVariable[] {
  const normalized = normalizedSearch(query);
  if (!normalized) return [...variables];
  const terms = normalized.split(/\s+/u).filter(Boolean);
  return variables.filter((variable) => {
    const haystack = [variable.path, variable.label, variable.description, variable.dataType, variable.nodeId]
      .filter(Boolean)
      .join(' ')
      .toLocaleLowerCase();
    return terms.every((term) => haystack.includes(term));
  });
}

function groupLabel(group: A3SFlowExpressionVariableGroup, chinese: boolean): string {
  const labels = chinese
    ? {
        global: '全局变量',
        input: '工作流输入',
        scope: '当前子流程',
        upstream: '上游节点输出',
      }
    : {
        global: 'Global variables',
        input: 'Workflow input',
        scope: 'Current scope',
        upstream: 'Upstream outputs',
      };
  return labels[group];
}

export function VariableSuggestionList({
  activeIndex,
  id,
  locale,
  onActiveIndexChange,
  onSelect,
  query,
  variables,
}: {
  activeIndex: number;
  id: string;
  locale: string;
  onActiveIndexChange: (index: number) => void;
  onSelect: (variable: A3SFlowExpressionVariable) => void;
  query: string;
  variables: readonly A3SFlowExpressionVariable[];
}) {
  const chinese = isChinese(locale);
  const filtered = useMemo(() => filterA3SFlowExpressionVariables(variables, query), [query, variables]);
  const grouped = useMemo(() => {
    const groups = new Map<A3SFlowExpressionVariableGroup, { index: number; variable: A3SFlowExpressionVariable }[]>();
    filtered.forEach((variable, index) => {
      const entries = groups.get(variable.group) ?? [];
      entries.push({ index, variable });
      groups.set(variable.group, entries);
    });
    return groups;
  }, [filtered]);

  return (
    <div
      aria-label={chinese ? '变量智能感知' : 'Variable suggestions'}
      className="a3s-flow-variable-suggestions"
      id={id}
      role="listbox"
    >
      <header>
        <span aria-hidden="true">$</span>
        <strong>{chinese ? '引用变量' : 'Reference a variable'}</strong>
        <kbd>↑↓</kbd>
        <kbd>Enter</kbd>
      </header>
      {filtered.length === 0 ? (
        <p>{chinese ? `没有匹配“${query}”的变量` : `No variables match “${query}”`}</p>
      ) : (
        <div className="a3s-flow-variable-suggestions__groups">
          {[...grouped.entries()].map(([group, entries]) => (
            <section aria-label={groupLabel(group, chinese)} key={group}>
              <h4>{groupLabel(group, chinese)}</h4>
              {entries.map(({ index, variable }) => (
                <button
                  aria-selected={index === activeIndex}
                  className={index === activeIndex ? 'is-active' : undefined}
                  data-variable-option=""
                  key={`${variable.group}-${variable.path}`}
                  onMouseDown={(event) => event.preventDefault()}
                  onMouseEnter={() => onActiveIndexChange(index)}
                  onClick={() => onSelect(variable)}
                  role="option"
                  type="button"
                >
                  <span>
                    <code>${variable.path}</code>
                    <small>{variable.label}</small>
                  </span>
                  {variable.dataType && <em>{variable.dataType}</em>}
                </button>
              ))}
            </section>
          ))}
        </div>
      )}
    </div>
  );
}

export function useVariableSuggestionState(variables: readonly A3SFlowExpressionVariable[]) {
  const [open, setOpen] = useState(false);
  const [query, setQuery] = useState('');
  const [activeIndex, setActiveIndex] = useState(0);
  const filtered = useMemo(() => filterA3SFlowExpressionVariables(variables, query), [query, variables]);

  useEffect(() => setActiveIndex(0), [query]);

  const close = () => {
    setOpen(false);
    setQuery('');
    setActiveIndex(0);
  };

  const handleKeyDown = (
    event: KeyboardEvent<HTMLElement>,
    onSelect: (variable: A3SFlowExpressionVariable) => void,
  ): boolean => {
    if (!open) return false;
    if (event.key === 'Escape') {
      event.preventDefault();
      close();
      return true;
    }
    if (filtered.length === 0) return false;
    if (event.key === 'ArrowDown' || event.key === 'ArrowUp') {
      event.preventDefault();
      const direction = event.key === 'ArrowDown' ? 1 : -1;
      setActiveIndex((current) => (current + direction + filtered.length) % filtered.length);
      return true;
    }
    if (event.key === 'Enter') {
      event.preventDefault();
      onSelect(filtered[Math.min(activeIndex, filtered.length - 1)]);
      close();
      return true;
    }
    return false;
  };

  return {
    activeIndex,
    close,
    filtered,
    handleKeyDown,
    open,
    query,
    setActiveIndex,
    setOpen,
    setQuery,
  };
}

function useOutsideDismiss(ref: RefObject<HTMLElement | null>, open: boolean, onClose: () => void) {
  useEffect(() => {
    if (!open) return;
    const handlePointerDown = (event: PointerEvent) => {
      if (event.target instanceof Node && !ref.current?.contains(event.target)) {
        onClose();
      }
    };
    document.addEventListener('pointerdown', handlePointerDown, true);
    return () => document.removeEventListener('pointerdown', handlePointerDown, true);
  }, [onClose, open, ref]);
}

export interface VariableReferenceInputProps extends Omit<InputHTMLAttributes<HTMLInputElement>, 'onChange' | 'value'> {
  locale: string;
  onPathChange: (path: string) => void;
  path: string;
  variables?: readonly A3SFlowExpressionVariable[];
}

export function VariableReferenceInput({
  locale,
  onPathChange,
  path,
  variables = A3S_FLOW_DEFAULT_EXPRESSION_VARIABLES,
  ...props
}: VariableReferenceInputProps) {
  const generatedId = useId();
  const suggestionsId = `${props.id ?? generatedId}-variables`;
  const rootRef = useRef<HTMLDivElement>(null);
  const state = useVariableSuggestionState(variables);
  useOutsideDismiss(rootRef, state.open, state.close);

  const choose = (variable: A3SFlowExpressionVariable) => {
    onPathChange(variable.path);
    state.close();
  };

  return (
    <div className="a3s-flow-variable-input" data-open={state.open || undefined} ref={rootRef}>
      <span aria-hidden="true" className="a3s-flow-variable-input__sigil">
        $
      </span>
      <input
        {...props}
        aria-controls={state.open ? suggestionsId : undefined}
        aria-expanded={state.open}
        aria-haspopup="listbox"
        autoComplete="off"
        className={['input', props.className].filter(Boolean).join(' ')}
        value={path}
        onChange={(event) => {
          const raw = event.target.value;
          const marker = raw.lastIndexOf('$');
          const next = marker < 0 ? raw : `${raw.slice(0, marker)}${raw.slice(marker + 1)}`;
          onPathChange(next);
          if (marker >= 0) {
            state.setQuery(raw.slice(marker + 1));
            state.setOpen(true);
          } else if (state.open) {
            state.setQuery(next);
          }
        }}
        onKeyDown={(event) => {
          if (state.handleKeyDown(event, choose)) return;
          props.onKeyDown?.(event);
        }}
      />
      <button
        aria-label={isChinese(locale) ? '选择变量' : 'Choose variable'}
        className="a3s-flow-variable-input__trigger"
        disabled={props.disabled}
        onClick={() => {
          state.setQuery(path);
          state.setOpen((current) => !current);
        }}
        title={isChinese(locale) ? '输入 $ 或点击选择变量' : 'Type $ or choose a variable'}
        type="button"
      >
        <DesignerIcon name="sparkles" size={13} />
      </button>
      {state.open && (
        <VariableSuggestionList
          activeIndex={state.activeIndex}
          id={suggestionsId}
          locale={locale}
          onActiveIndexChange={state.setActiveIndex}
          onSelect={choose}
          query={state.query}
          variables={variables}
        />
      )}
    </div>
  );
}

export interface VariableTemplateTextareaProps extends Omit<
  TextareaHTMLAttributes<HTMLTextAreaElement>,
  'onChange' | 'value'
> {
  locale: string;
  onValueChange: (value: string) => void;
  value: string;
  variables?: readonly A3SFlowExpressionVariable[];
}

export function VariableTemplateTextarea({
  locale,
  onValueChange,
  value,
  variables = A3S_FLOW_DEFAULT_EXPRESSION_VARIABLES,
  ...props
}: VariableTemplateTextareaProps) {
  const generatedId = useId();
  const suggestionsId = `${props.id ?? generatedId}-variables`;
  const rootRef = useRef<HTMLDivElement>(null);
  const textareaRef = useRef<HTMLTextAreaElement>(null);
  const markerRef = useRef<number | undefined>(undefined);
  const state = useVariableSuggestionState(variables);
  useOutsideDismiss(rootRef, state.open, state.close);

  const choose = (variable: A3SFlowExpressionVariable) => {
    const textarea = textareaRef.current;
    const caret = textarea?.selectionStart ?? value.length;
    const end = textarea?.selectionEnd ?? caret;
    const start = markerRef.current ?? caret;
    const replacement = `{{${variable.path}}}`;
    onValueChange(`${value.slice(0, start)}${replacement}${value.slice(end)}`);
    markerRef.current = undefined;
    state.close();
    requestAnimationFrame(() => {
      const nextCaret = start + replacement.length;
      textareaRef.current?.focus();
      textareaRef.current?.setSelectionRange(nextCaret, nextCaret);
    });
  };

  return (
    <div className="a3s-flow-variable-textarea" data-open={state.open || undefined} ref={rootRef}>
      <textarea
        {...props}
        aria-controls={state.open ? suggestionsId : undefined}
        aria-expanded={state.open}
        aria-haspopup="listbox"
        className={['textarea', props.className].filter(Boolean).join(' ')}
        onChange={(event) => {
          const next = event.target.value;
          onValueChange(next);
          const caret = event.target.selectionStart ?? next.length;
          const marker = next.lastIndexOf('$', Math.max(0, caret - 1));
          const query = marker >= 0 ? next.slice(marker + 1, caret) : '';
          if (marker >= 0 && /^[\w.-]*$/u.test(query)) {
            markerRef.current = marker;
            state.setQuery(query);
            state.setOpen(true);
          } else {
            markerRef.current = undefined;
            state.close();
          }
        }}
        onKeyDown={(event) => {
          if (state.handleKeyDown(event, choose)) return;
          props.onKeyDown?.(event);
        }}
        ref={textareaRef}
        value={value}
      />
      <button
        aria-label={isChinese(locale) ? '插入变量' : 'Insert variable'}
        className="a3s-flow-variable-textarea__trigger"
        disabled={props.disabled}
        onClick={() => {
          markerRef.current = undefined;
          state.setQuery('');
          state.setOpen((current) => !current);
        }}
        title={isChinese(locale) ? '输入 $ 或点击插入变量' : 'Type $ or insert a variable'}
        type="button"
      >
        <span aria-hidden="true">$</span>
        {isChinese(locale) ? '变量' : 'Variable'}
      </button>
      {state.open && (
        <VariableSuggestionList
          activeIndex={state.activeIndex}
          id={suggestionsId}
          locale={locale}
          onActiveIndexChange={state.setActiveIndex}
          onSelect={choose}
          query={state.query}
          variables={variables}
        />
      )}
    </div>
  );
}