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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
import { useEffect, useId, useState } from 'react';
import type { JsonObject, JsonValue } from '@a3s-lab/ui/form/core';
import { DesignerIcon } from './designer-icons';
import type { FormWidgetProps } from '@a3s-lab/ui/form/react';
import { SelectControl } from './select-control';
import { WorkflowCodeEditor } from './workflow-code-editor';

const PROPERTY_TYPES = ['string', 'number', 'integer', 'boolean', 'object', 'array'] as const;
const INVALID_SCHEMA_DRAFT = '__a3s_form_invalid_schema_draft__';

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

function objectValue(value: JsonValue | undefined): JsonObject {
  return value && typeof value === 'object' && !Array.isArray(value) ? value : {};
}

interface InvalidSchemaDraft {
  fieldNames?: string[];
  kind: 'field-name' | 'json';
  schema: JsonObject;
  source?: string;
}

function invalidSchemaDraftValue(
  schema: JsonObject,
  kind: InvalidSchemaDraft['kind'],
  source?: string,
  fieldNames?: readonly string[],
): JsonValue {
  return [
    {
      [INVALID_SCHEMA_DRAFT]: kind,
      schema,
      ...(source === undefined ? {} : { source }),
      ...(fieldNames === undefined ? {} : { fieldNames: [...fieldNames] }),
    },
  ];
}

function invalidSchemaDraftFrom(value: JsonValue | undefined): InvalidSchemaDraft | undefined {
  if (Array.isArray(value) && value.length === 1) {
    const marker = objectValue(value[0]);
    const kind = marker[INVALID_SCHEMA_DRAFT];
    if (kind === 'field-name' || kind === 'json') {
      return {
        kind,
        schema: objectValue(marker.schema),
        source: typeof marker.source === 'string' ? marker.source : undefined,
        fieldNames: Array.isArray(marker.fieldNames)
          ? marker.fieldNames.filter((name): name is string => typeof name === 'string')
          : typeof marker.fieldName === 'string'
            ? [marker.fieldName]
            : undefined,
      };
    }
  }
  return undefined;
}

function schemaProperties(schema: JsonObject): JsonObject {
  return objectValue(schema.properties);
}

function requiredFields(schema: JsonObject): string[] {
  return Array.isArray(schema.required)
    ? schema.required.filter((item): item is string => typeof item === 'string')
    : [];
}

function propertyType(value: JsonValue): string {
  const schema = objectValue(value);
  return typeof schema.type === 'string' && PROPERTY_TYPES.some((type) => type === schema.type)
    ? schema.type
    : 'string';
}

function propertyTypeLabel(type: (typeof PROPERTY_TYPES)[number], chinese: boolean): string {
  if (!chinese) return type;
  return {
    string: '文本',
    number: '数字',
    integer: '整数',
    boolean: '布尔值',
    object: '对象',
    array: '数组',
  }[type];
}

function nextFieldName(properties: JsonObject): string {
  let index = Object.keys(properties).length + 1;
  while (`field_${index}` in properties) index += 1;
  return `field_${index}`;
}

function SchemaFieldRow({
  name,
  schema,
  required,
  disabled,
  locale,
  existingNames,
  onRename,
  onTypeChange,
  onRequiredChange,
  onRemove,
  onInvalidDraft,
  valuePath,
}: {
  name: string;
  schema: JsonValue;
  required: boolean;
  disabled: boolean;
  locale: string;
  existingNames: readonly string[];
  onRename: (name: string) => void;
  onTypeChange: (type: string) => void;
  onRequiredChange: (required: boolean) => void;
  onRemove: () => void;
  onInvalidDraft: () => void;
  valuePath?: string;
}) {
  const chinese = isChinese(locale);
  const nameId = useId();
  const typeId = useId();
  const nameErrorId = `${nameId}-error`;
  const [draft, setDraft] = useState(name);
  const [nameError, setNameError] = useState(false);
  useEffect(() => setDraft(name), [name]);
  const commitName = () => {
    const next = draft.trim();
    const invalid = !next || (next !== name && existingNames.includes(next));
    setNameError(invalid);
    if (invalid) {
      onInvalidDraft();
      return;
    }
    onRename(next);
  };
  return (
    <li className="a3s-form-flow-schema-row" data-invalid={nameError || undefined}>
      <div className="a3s-form-flow-schema-row-fields">
        <label htmlFor={nameId}>
          <span>{chinese ? '字段名' : 'Field name'}</span>
          <input
            id={nameId}
            className="input"
            value={draft}
            disabled={disabled}
            aria-invalid={nameError || undefined}
            aria-describedby={nameError ? nameErrorId : undefined}
            data-a3s-form-path={valuePath}
            required
            onChange={(event) => {
              setDraft(event.target.value);
              setNameError(false);
            }}
            onBlur={commitName}
            onKeyDown={(event) => {
              if (event.key === 'Enter') {
                event.preventDefault();
                commitName();
              }
            }}
          />
        </label>
        <label htmlFor={typeId}>
          <span>{chinese ? '数据类型' : 'Type'}</span>
          <SelectControl
            id={typeId}
            value={propertyType(schema)}
            disabled={disabled}
            onChange={(event) => onTypeChange(event.target.value)}
          >
            {PROPERTY_TYPES.map((type) => (
              <option value={type} key={type}>
                {propertyTypeLabel(type, chinese)}
              </option>
            ))}
          </SelectControl>
        </label>
      </div>
      <label className="a3s-form-flow-schema-required">
        <input
          className="input"
          type="checkbox"
          checked={required}
          disabled={disabled}
          onChange={(event) => onRequiredChange(event.target.checked)}
        />
        <span>{chinese ? '必填' : 'Required'}</span>
      </label>
      <button
        type="button"
        className="btn"
        data-size="icon-sm"
        data-variant="ghost"
        disabled={disabled}
        aria-label={chinese ? `删除字段 ${name}` : `Remove field ${name}`}
        onClick={onRemove}
      >
        <DesignerIcon name="trash" size={14} />
      </button>
      {nameError && (
        <small id={nameErrorId} role="alert">
          {chinese
            ? '字段名不能为空,也不能与其他字段重复。'
            : 'Field names must be non-empty and unique.'}
        </small>
      )}
    </li>
  );
}

function AdvancedSchemaEditor({
  id,
  schema,
  onChange,
  onInvalidDraft,
  disabled,
  locale,
  describedBy,
  draftSource,
  draftInvalid,
}: {
  id: string;
  schema: JsonObject;
  onChange: (schema: JsonObject) => void;
  onInvalidDraft: (source: string) => void;
  disabled: boolean;
  locale: string;
  describedBy?: string;
  draftSource?: string;
  draftInvalid?: boolean;
}) {
  const chinese = isChinese(locale);
  const source = draftSource ?? JSON.stringify(schema, null, 2);
  const [draft, setDraft] = useState(source);
  const [invalid, setInvalid] = useState(Boolean(draftInvalid));
  const errorId = `${id}-draft-error`;
  useEffect(() => {
    setDraft(source);
    setInvalid(Boolean(draftInvalid));
  }, [draftInvalid, source]);
  return (
    <div className="a3s-form-flow-schema-json" data-invalid={invalid || undefined}>
      <WorkflowCodeEditor
        ariaLabel={chinese ? '输入规则 JSON' : 'Input schema JSON'}
        describedBy={
          [describedBy, invalid ? errorId : undefined]
            .filter(Boolean)
            .join(' ') || undefined
        }
        disabled={disabled}
        fileName="input.schema.json"
        id={id}
        invalid={invalid}
        language="json"
        locale={locale}
        onChange={(next) => {
          setDraft(next);
          try {
            const parsed = JSON.parse(next) as JsonValue;
            if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) {
              setInvalid(true);
              onInvalidDraft(next);
              return;
            }
            setInvalid(false);
            onChange(parsed);
          } catch {
            setInvalid(true);
            onInvalidDraft(next);
          }
        }}
        status={
          invalid
            ? chinese
              ? '输入规则无效'
              : 'Invalid input schema'
            : chinese
              ? '输入规则有效'
              : 'Valid input schema'
        }
        value={draft}
      />
      <small id={errorId} role={invalid ? 'alert' : undefined}>
        {invalid
          ? chinese
            ? 'JSON 格式有误,请修正后再保存配置。'
            : 'The JSON is invalid. Fix it before saving changes.'
          : chinese
            ? '用于嵌套对象、数组约束等高级 JSON Schema 设置。'
            : 'Use for nested objects, array constraints, and other advanced JSON Schema settings.'}
      </small>
    </div>
  );
}

export function A3SFlowSchemaWidget(props: FormWidgetProps) {
  const chinese = isChinese(props.locale);
  const invalidDraft = invalidSchemaDraftFrom(props.value);
  const schema = invalidDraft?.schema ?? objectValue(props.value);
  const properties = schemaProperties(schema);
  const required = new Set(requiredFields(schema));
  const names = Object.keys(properties);
  const invalidFieldNames =
    invalidDraft?.kind === 'field-name' ? (invalidDraft.fieldNames ?? []) : [];
  const writeWithInvalidFields = (next: JsonObject, candidates: readonly string[]) => {
    const nextProperties = schemaProperties(next);
    const remaining = candidates.filter((name) => Object.hasOwn(nextProperties, name));
    if (remaining.length === 0) {
      props.onChange(next);
      return;
    }
    props.onChange(invalidSchemaDraftValue(next, 'field-name', undefined, remaining));
  };
  const update = (next: JsonObject) => writeWithInvalidFields(next, invalidFieldNames);
  const resolveInvalidField = (next: JsonObject, name: string) =>
    writeWithInvalidFields(
      next,
      invalidFieldNames.filter((candidate) => candidate !== name),
    );
  const updateProperties = (nextProperties: JsonObject, nextRequired = [...required]) =>
    update({
      ...schema,
      type: 'object',
      properties: nextProperties,
      required: nextRequired,
    });

  return (
    <fieldset
      id={props.id}
      className="a3s-form-flow-schema"
      tabIndex={-1}
      aria-labelledby={props.labelledBy}
      aria-describedby={props.describedBy}
      aria-invalid={props.invalid || undefined}
      onBlur={(event) => {
        if (!event.currentTarget.contains(event.relatedTarget as Node | null)) props.onBlur?.();
      }}
      onFocus={props.onFocus}
    >
      {names.length > 0 ? (
        <ol aria-label={chinese ? '输入字段' : 'Input fields'}>
          {names.map((name) => (
            <SchemaFieldRow
              key={name}
              name={name}
              schema={properties[name] ?? { type: 'string' }}
              required={required.has(name)}
              disabled={props.disabled}
              locale={props.locale}
              existingNames={names}
              valuePath={props.valuePath ? `${props.valuePath}.properties.${name}` : undefined}
              onInvalidDraft={() =>
                props.onChange(
                  invalidSchemaDraftValue(schema, 'field-name', undefined, [
                    ...new Set([...invalidFieldNames, name]),
                  ]),
                )
              }
              onRename={(nextName) => {
                if (nextName === name) {
                  resolveInvalidField(schema, name);
                  return;
                }
                const nextProperties = Object.fromEntries(
                  Object.entries(properties).map(([candidate, value]) => [
                    candidate === name ? nextName : candidate,
                    value,
                  ]),
                );
                const nextRequired = [...required].map((candidate) =>
                  candidate === name ? nextName : candidate,
                );
                resolveInvalidField(
                  {
                    ...schema,
                    type: 'object',
                    properties: nextProperties,
                    required: nextRequired,
                  },
                  name,
                );
              }}
              onTypeChange={(type) =>
                updateProperties({
                  ...properties,
                  [name]: { ...objectValue(properties[name]), type },
                })
              }
              onRequiredChange={(isRequired) => {
                const nextRequired = new Set(required);
                if (isRequired) nextRequired.add(name);
                else nextRequired.delete(name);
                updateProperties(properties, [...nextRequired]);
              }}
              onRemove={() => {
                const nextProperties = Object.fromEntries(
                  Object.entries(properties).filter(([candidate]) => candidate !== name),
                );
                const nextSchema = {
                  ...schema,
                  type: 'object',
                  properties: nextProperties,
                  required: [...required].filter((candidate) => candidate !== name),
                };
                resolveInvalidField(nextSchema, name);
              }}
            />
          ))}
        </ol>
      ) : (
        <div className="a3s-form-flow-schema-empty">
          <DesignerIcon name="field" size={18} />
          <span>
            <strong>{chinese ? '暂未限定输入字段' : 'No input fields defined'}</strong>
            <small>
              {chinese ? '当前允许传入任意 JSON 字段。' : 'Any JSON field is currently accepted.'}
            </small>
          </span>
        </div>
      )}

      <div className="a3s-form-flow-schema-actions">
        <button
          type="button"
          className="btn"
          data-size="sm"
          data-variant="secondary"
          disabled={props.disabled}
          onClick={() => {
            const name = nextFieldName(properties);
            updateProperties({ ...properties, [name]: { type: 'string' } });
          }}
        >
          <DesignerIcon name="field" size={14} />
          {chinese ? '添加输入字段' : 'Add input field'}
        </button>
        <label>
          <input
            className="input"
            type="checkbox"
            checked={schema.additionalProperties !== false}
            disabled={props.disabled}
            onChange={(event) => update({ ...schema, additionalProperties: event.target.checked })}
          />
          <span>{chinese ? '允许其他字段' : 'Allow additional fields'}</span>
        </label>
      </div>

      <details className="a3s-form-flow-schema-advanced">
        <summary>{chinese ? '高级 JSON Schema' : 'Advanced JSON Schema'}</summary>
        <AdvancedSchemaEditor
          id={`${props.id}-advanced`}
          schema={schema}
          onChange={update}
          onInvalidDraft={(source) => {
            if (invalidFieldNames.length > 0) {
              props.onChange(
                invalidSchemaDraftValue(schema, 'field-name', undefined, invalidFieldNames),
              );
              return;
            }
            props.onChange(invalidSchemaDraftValue(schema, 'json', source));
          }}
          disabled={props.disabled}
          locale={props.locale}
          describedBy={props.describedBy}
          draftSource={invalidDraft?.kind === 'json' ? invalidDraft.source : undefined}
          draftInvalid={invalidDraft?.kind === 'json'}
        />
      </details>
      {(props.errors?.length ?? 0) > 0 && (
        <div className="a3s-form-flow-widget-errors">
          {props.errors?.map((error, index) => (
            <small
              id={`${props.id}-error-${index + 1}`}
              role="alert"
              key={`${error.path}-${error.code}`}
            >
              {error.message}
            </small>
          ))}
        </div>
      )}
    </fieldset>
  );
}