@widgetic/workflows 0.2.10 → 0.2.12
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 +11 -13
- package/dist/api/workflowsApi.d.ts +9 -1
- package/dist/api/workflowsApi.js +2 -1
- package/dist/assistant/assistantEvents.d.ts +42 -0
- package/dist/assistant/assistantEvents.js +61 -0
- package/dist/assistant/assistantRemoteChange.d.ts +4 -0
- package/dist/assistant/assistantRemoteChange.js +8 -0
- package/dist/assistant/assistantSaveMode.d.ts +5 -0
- package/dist/assistant/assistantSaveMode.js +8 -0
- package/dist/assistant/assistantStream.d.ts +8 -0
- package/dist/assistant/assistantStream.js +140 -0
- package/dist/assistant/assistantTranscript.d.ts +11 -0
- package/dist/assistant/assistantTranscript.js +18 -0
- package/dist/assistant/types.d.ts +81 -0
- package/dist/assistant/types.js +1 -0
- package/dist/assistant/workflowPlanViewModel.d.ts +48 -0
- package/dist/assistant/workflowPlanViewModel.js +17 -0
- package/dist/components/Workflows.svelte +3 -1
- package/dist/components/WorkflowsEmptyState.svelte +15 -3
- package/dist/components/WorkflowsEmptyState.svelte.d.ts +2 -0
- package/dist/components/WorkflowsWorkspace.svelte +4 -0
- package/dist/components/WorkflowsWorkspace.svelte.d.ts +1 -0
- package/dist/components/assistant/WorkflowAssistantPanel.svelte +384 -0
- package/dist/components/assistant/WorkflowAssistantPanel.svelte.d.ts +8 -0
- package/dist/components/canvas/CompositionNodeView.svelte +5 -5
- package/dist/components/canvas/ConditionNodeView.svelte +4 -4
- package/dist/components/canvas/HandlePoint.svelte +1 -1
- package/dist/components/canvas/TriggerNodeView.svelte +5 -4
- package/dist/components/canvas/WorkflowCanvas.svelte +16 -6
- package/dist/components/canvas/WorkflowCanvas.svelte.d.ts +2 -2
- package/dist/components/editor/EditorCanvasArea.svelte +57 -2
- package/dist/components/editor/EditorCanvasArea.svelte.d.ts +8 -1
- package/dist/components/editor/EditorTabs.svelte +1 -2
- package/dist/components/editor/EditorTopBar.svelte +29 -21
- package/dist/components/editor/EditorTopBar.svelte.d.ts +3 -0
- package/dist/components/editor/RunsFlow.svelte +73 -23
- package/dist/components/editor/WorkflowEditor.svelte +31 -3
- package/dist/components/editor/WorkflowEditor.svelte.d.ts +5 -0
- package/dist/components/editor/WorkflowNameControl.svelte +136 -0
- package/dist/components/editor/WorkflowNameControl.svelte.d.ts +13 -0
- package/dist/components/list/WorkflowList.svelte +112 -60
- package/dist/components/list/WorkflowList.svelte.d.ts +2 -0
- package/dist/components/list/WorkflowRow.svelte +2 -0
- package/dist/components/panel/ConditionForm.svelte +7 -7
- package/dist/components/ui/Menu.svelte +31 -7
- package/dist/components/ui/Menu.svelte.d.ts +4 -0
- package/dist/components/ui/icons.d.ts +2 -0
- package/dist/components/ui/icons.js +3 -1
- package/dist/drafts/draftJournal.d.ts +59 -0
- package/dist/drafts/draftJournal.js +261 -0
- package/dist/drafts/workflowDraft.d.ts +72 -0
- package/dist/drafts/workflowDraft.js +151 -0
- package/dist/drafts/workflowListViewState.d.ts +30 -0
- package/dist/drafts/workflowListViewState.js +92 -0
- package/dist/editor/createBlankWorkflow.d.ts +30 -0
- package/dist/editor/createBlankWorkflow.js +37 -0
- package/dist/editor/editorState.svelte.d.ts +68 -2
- package/dist/editor/editorState.svelte.js +273 -0
- package/dist/editor/listState.svelte.d.ts +73 -36
- package/dist/editor/listState.svelte.js +260 -46
- package/dist/editor/presetChoice.d.ts +4 -4
- package/dist/editor/presetChoice.js +4 -4
- package/dist/editor/runsFlowBandLabel.d.ts +16 -0
- package/dist/editor/runsFlowBandLabel.js +34 -0
- package/dist/editor/runsFlowLayout.d.ts +24 -9
- package/dist/editor/runsFlowLayout.js +73 -28
- package/dist/editor/workflowNameCommit.d.ts +12 -0
- package/dist/editor/workflowNameCommit.js +11 -0
- package/dist/i18n/language.d.ts +3 -3
- package/dist/i18n/messages.d.ts +40 -26
- package/dist/i18n/messages.js +43 -30
- package/dist/types/index.d.ts +4 -0
- package/package.json +8 -1
- package/dist/components/list/CreateWorkflowDialog.svelte +0 -262
- package/dist/components/list/CreateWorkflowDialog.svelte.d.ts +0 -9
|
@@ -0,0 +1,261 @@
|
|
|
1
|
+
/** Refresh-safe draft journal (localStorage). Copied per package; see QoL persistence spec. */
|
|
2
|
+
export const DRAFT_STORAGE_PREFIX = 'widgetic:draft:';
|
|
3
|
+
export const DRAFT_FORMAT_VERSION = 1;
|
|
4
|
+
export const DEFAULT_DRAFT_MAX_BYTES = Math.floor(1.5 * 1024 * 1024);
|
|
5
|
+
export const DEFAULT_DRAFT_MAX_AGE_MS = 7 * 24 * 60 * 60 * 1000;
|
|
6
|
+
export const DRAFT_WRITE_THROTTLE_MS = 250;
|
|
7
|
+
const quietLogger = {
|
|
8
|
+
debug(message, detail) {
|
|
9
|
+
if (typeof console !== 'undefined' && console.debug) {
|
|
10
|
+
console.debug(message, detail ?? '');
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
};
|
|
14
|
+
const pageTimers = {
|
|
15
|
+
setTimeout: (callback, milliseconds) => setTimeout(callback, milliseconds),
|
|
16
|
+
clearTimeout: (handle) => clearTimeout(handle)
|
|
17
|
+
};
|
|
18
|
+
function storageKey(namespace, key) {
|
|
19
|
+
return `${DRAFT_STORAGE_PREFIX}${namespace}:${key}`;
|
|
20
|
+
}
|
|
21
|
+
function isDraftStorageKey(fullKey) {
|
|
22
|
+
return fullKey.startsWith(DRAFT_STORAGE_PREFIX);
|
|
23
|
+
}
|
|
24
|
+
function parseEntry(raw) {
|
|
25
|
+
try {
|
|
26
|
+
const parsed = JSON.parse(raw);
|
|
27
|
+
if (parsed?.v !== DRAFT_FORMAT_VERSION)
|
|
28
|
+
return null;
|
|
29
|
+
if (typeof parsed.savedAtMs !== 'number')
|
|
30
|
+
return null;
|
|
31
|
+
if (!('payload' in parsed))
|
|
32
|
+
return null;
|
|
33
|
+
return parsed;
|
|
34
|
+
}
|
|
35
|
+
catch {
|
|
36
|
+
return null;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
function evictOldestDraftEntries(storage, logger) {
|
|
40
|
+
const draftKeys = [];
|
|
41
|
+
for (let index = 0; index < storage.length; index += 1) {
|
|
42
|
+
const fullKey = storage.key(index);
|
|
43
|
+
if (!fullKey || !isDraftStorageKey(fullKey))
|
|
44
|
+
continue;
|
|
45
|
+
const raw = storage.getItem(fullKey);
|
|
46
|
+
if (!raw)
|
|
47
|
+
continue;
|
|
48
|
+
const entry = parseEntry(raw);
|
|
49
|
+
draftKeys.push({ key: fullKey, savedAtMs: entry?.savedAtMs ?? 0 });
|
|
50
|
+
}
|
|
51
|
+
draftKeys.sort((left, right) => left.savedAtMs - right.savedAtMs);
|
|
52
|
+
const evictCount = Math.max(1, Math.ceil(draftKeys.length / 4));
|
|
53
|
+
for (let index = 0; index < evictCount && index < draftKeys.length; index += 1) {
|
|
54
|
+
storage.removeItem(draftKeys[index].key);
|
|
55
|
+
}
|
|
56
|
+
logger.debug('[draftJournal] Evicted oldest draft entries after quota error', { evictCount });
|
|
57
|
+
}
|
|
58
|
+
function getDefaultStorage() {
|
|
59
|
+
if (typeof localStorage === 'undefined')
|
|
60
|
+
return undefined;
|
|
61
|
+
return localStorage;
|
|
62
|
+
}
|
|
63
|
+
function resolveWindow(options) {
|
|
64
|
+
if (options.window !== undefined)
|
|
65
|
+
return options.window;
|
|
66
|
+
if (typeof globalThis.window === 'undefined')
|
|
67
|
+
return null;
|
|
68
|
+
return globalThis.window;
|
|
69
|
+
}
|
|
70
|
+
function resolveDocument(options, win) {
|
|
71
|
+
if (options.document !== undefined)
|
|
72
|
+
return options.document;
|
|
73
|
+
const candidate = win;
|
|
74
|
+
return candidate?.document ?? null;
|
|
75
|
+
}
|
|
76
|
+
export function createDraftJournal(namespace, options = {}) {
|
|
77
|
+
const storage = options.storage ?? getDefaultStorage();
|
|
78
|
+
const clock = options.clock ?? { now: () => Date.now() };
|
|
79
|
+
const logger = options.logger ?? quietLogger;
|
|
80
|
+
const timers = options.timers ?? pageTimers;
|
|
81
|
+
const maxBytes = options.maxBytes ?? DEFAULT_DRAFT_MAX_BYTES;
|
|
82
|
+
const maxAgeMs = options.maxAgeMs ?? DEFAULT_DRAFT_MAX_AGE_MS;
|
|
83
|
+
const throttleMs = options.throttleMs ?? DRAFT_WRITE_THROTTLE_MS;
|
|
84
|
+
const win = resolveWindow(options);
|
|
85
|
+
const doc = resolveDocument(options, win);
|
|
86
|
+
const pendingByKey = new Map();
|
|
87
|
+
const throttleTimers = new Map();
|
|
88
|
+
let loggedOversize = false;
|
|
89
|
+
function removeCorrupt(fullKey, reason) {
|
|
90
|
+
try {
|
|
91
|
+
storage?.removeItem(fullKey);
|
|
92
|
+
}
|
|
93
|
+
catch {
|
|
94
|
+
// ignore
|
|
95
|
+
}
|
|
96
|
+
logger.debug('[draftJournal] Discarded corrupt draft', { fullKey, reason });
|
|
97
|
+
}
|
|
98
|
+
function read(key) {
|
|
99
|
+
if (!storage)
|
|
100
|
+
return null;
|
|
101
|
+
const fullKey = storageKey(namespace, key);
|
|
102
|
+
let raw;
|
|
103
|
+
try {
|
|
104
|
+
raw = storage.getItem(fullKey);
|
|
105
|
+
}
|
|
106
|
+
catch {
|
|
107
|
+
return null;
|
|
108
|
+
}
|
|
109
|
+
if (!raw)
|
|
110
|
+
return null;
|
|
111
|
+
const entry = parseEntry(raw);
|
|
112
|
+
if (!entry) {
|
|
113
|
+
removeCorrupt(fullKey, 'invalid-json-or-version');
|
|
114
|
+
return null;
|
|
115
|
+
}
|
|
116
|
+
if (clock.now() - entry.savedAtMs > maxAgeMs) {
|
|
117
|
+
removeCorrupt(fullKey, 'expired');
|
|
118
|
+
return null;
|
|
119
|
+
}
|
|
120
|
+
return entry;
|
|
121
|
+
}
|
|
122
|
+
function persist(key, payload, baseRevision, skipThrottle) {
|
|
123
|
+
if (!storage)
|
|
124
|
+
return false;
|
|
125
|
+
const fullKey = storageKey(namespace, key);
|
|
126
|
+
const entry = {
|
|
127
|
+
v: DRAFT_FORMAT_VERSION,
|
|
128
|
+
savedAtMs: clock.now(),
|
|
129
|
+
baseRevision,
|
|
130
|
+
payload
|
|
131
|
+
};
|
|
132
|
+
let serialized;
|
|
133
|
+
try {
|
|
134
|
+
serialized = JSON.stringify(entry);
|
|
135
|
+
}
|
|
136
|
+
catch {
|
|
137
|
+
return false;
|
|
138
|
+
}
|
|
139
|
+
if (serialized.length > maxBytes) {
|
|
140
|
+
if (!loggedOversize) {
|
|
141
|
+
loggedOversize = true;
|
|
142
|
+
logger.debug('[draftJournal] Draft too large; refused write', {
|
|
143
|
+
namespace,
|
|
144
|
+
key,
|
|
145
|
+
bytes: serialized.length
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
return false;
|
|
149
|
+
}
|
|
150
|
+
if (!skipThrottle) {
|
|
151
|
+
pendingByKey.set(key, { payload, baseRevision });
|
|
152
|
+
if (!throttleTimers.has(key)) {
|
|
153
|
+
const timer = timers.setTimeout(() => {
|
|
154
|
+
throttleTimers.delete(key);
|
|
155
|
+
const pending = pendingByKey.get(key);
|
|
156
|
+
if (pending) {
|
|
157
|
+
pendingByKey.delete(key);
|
|
158
|
+
persist(key, pending.payload, pending.baseRevision, true);
|
|
159
|
+
}
|
|
160
|
+
}, throttleMs);
|
|
161
|
+
throttleTimers.set(key, timer);
|
|
162
|
+
}
|
|
163
|
+
return true;
|
|
164
|
+
}
|
|
165
|
+
try {
|
|
166
|
+
storage.setItem(fullKey, serialized);
|
|
167
|
+
return true;
|
|
168
|
+
}
|
|
169
|
+
catch {
|
|
170
|
+
try {
|
|
171
|
+
evictOldestDraftEntries(storage, logger);
|
|
172
|
+
storage.setItem(fullKey, serialized);
|
|
173
|
+
return true;
|
|
174
|
+
}
|
|
175
|
+
catch {
|
|
176
|
+
return false;
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
function flushNow(key, payload, baseRevision = null) {
|
|
181
|
+
const timer = throttleTimers.get(key);
|
|
182
|
+
if (timer !== undefined) {
|
|
183
|
+
timers.clearTimeout(timer);
|
|
184
|
+
throttleTimers.delete(key);
|
|
185
|
+
}
|
|
186
|
+
pendingByKey.delete(key);
|
|
187
|
+
return persist(key, payload, baseRevision, true);
|
|
188
|
+
}
|
|
189
|
+
function write(key, payload, baseRevision = null) {
|
|
190
|
+
return persist(key, payload, baseRevision, false);
|
|
191
|
+
}
|
|
192
|
+
function clear(key) {
|
|
193
|
+
const timer = throttleTimers.get(key);
|
|
194
|
+
if (timer !== undefined)
|
|
195
|
+
timers.clearTimeout(timer);
|
|
196
|
+
throttleTimers.delete(key);
|
|
197
|
+
pendingByKey.delete(key);
|
|
198
|
+
if (!storage)
|
|
199
|
+
return;
|
|
200
|
+
try {
|
|
201
|
+
storage.removeItem(storageKey(namespace, key));
|
|
202
|
+
}
|
|
203
|
+
catch {
|
|
204
|
+
// ignore
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
function purgeExpired() {
|
|
208
|
+
if (!storage)
|
|
209
|
+
return 0;
|
|
210
|
+
let removed = 0;
|
|
211
|
+
const now = clock.now();
|
|
212
|
+
for (let index = 0; index < storage.length; index += 1) {
|
|
213
|
+
const fullKey = storage.key(index);
|
|
214
|
+
if (!fullKey || !isDraftStorageKey(fullKey))
|
|
215
|
+
continue;
|
|
216
|
+
const raw = storage.getItem(fullKey);
|
|
217
|
+
if (!raw)
|
|
218
|
+
continue;
|
|
219
|
+
const entry = parseEntry(raw);
|
|
220
|
+
if (!entry || now - entry.savedAtMs > maxAgeMs) {
|
|
221
|
+
storage.removeItem(fullKey);
|
|
222
|
+
removed += 1;
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
if (removed > 0) {
|
|
226
|
+
logger.debug('[draftJournal] Purged expired drafts', { removed });
|
|
227
|
+
}
|
|
228
|
+
return removed;
|
|
229
|
+
}
|
|
230
|
+
function installFlushOnLeave(flushCallback) {
|
|
231
|
+
if (!win)
|
|
232
|
+
return () => { };
|
|
233
|
+
let flushedThisNavigation = false;
|
|
234
|
+
const runFlush = () => {
|
|
235
|
+
if (flushedThisNavigation)
|
|
236
|
+
return;
|
|
237
|
+
flushedThisNavigation = true;
|
|
238
|
+
try {
|
|
239
|
+
flushCallback();
|
|
240
|
+
}
|
|
241
|
+
catch {
|
|
242
|
+
// ignore
|
|
243
|
+
}
|
|
244
|
+
};
|
|
245
|
+
const onPageHide = () => runFlush();
|
|
246
|
+
const onVisibilityChange = () => {
|
|
247
|
+
if (doc?.visibilityState === 'hidden')
|
|
248
|
+
runFlush();
|
|
249
|
+
};
|
|
250
|
+
const onBeforeUnload = () => runFlush();
|
|
251
|
+
win.addEventListener('pagehide', onPageHide);
|
|
252
|
+
doc?.addEventListener('visibilitychange', onVisibilityChange);
|
|
253
|
+
win.addEventListener('beforeunload', onBeforeUnload);
|
|
254
|
+
return () => {
|
|
255
|
+
win.removeEventListener('pagehide', onPageHide);
|
|
256
|
+
doc?.removeEventListener('visibilitychange', onVisibilityChange);
|
|
257
|
+
win.removeEventListener('beforeunload', onBeforeUnload);
|
|
258
|
+
};
|
|
259
|
+
}
|
|
260
|
+
return { read, write, flushNow, clear, purgeExpired, installFlushOnLeave };
|
|
261
|
+
}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Workflow editor draft payload, restore rule, and session view state for refresh persistence.
|
|
3
|
+
*/
|
|
4
|
+
import type { WorkflowGraph } from '@widgetic/workflows-engine';
|
|
5
|
+
import type { DraftJournal, DraftJournalEntry } from './draftJournal.js';
|
|
6
|
+
type EditorTab = 'editor' | 'runs' | 'settings';
|
|
7
|
+
type PanelMode = 'selection' | 'history' | 'test';
|
|
8
|
+
export declare const WORKFLOW_DRAFT_NAMESPACE = "workflow";
|
|
9
|
+
export declare const STALE_DRAFT_KEY_SUFFIX = ":stale";
|
|
10
|
+
export declare const STALE_DRAFT_MAX_AGE_MS: number;
|
|
11
|
+
export declare const KEEPALIVE_SAVE_MAX_BYTES: number;
|
|
12
|
+
/** What the workflow autosave would send, plus inputs not yet committed to the graph. */
|
|
13
|
+
export interface WorkflowDraftPayload {
|
|
14
|
+
graph: WorkflowGraph;
|
|
15
|
+
name: string;
|
|
16
|
+
timezone: string;
|
|
17
|
+
/** The workflow name being typed in the header before commit. */
|
|
18
|
+
pendingName?: string;
|
|
19
|
+
}
|
|
20
|
+
export interface WorkflowEditorViewStateV1 {
|
|
21
|
+
v: 1;
|
|
22
|
+
tab: EditorTab;
|
|
23
|
+
panel: PanelMode;
|
|
24
|
+
selectedNodeIds: string[];
|
|
25
|
+
selectedEdgeIds: string[];
|
|
26
|
+
viewport: {
|
|
27
|
+
x: number;
|
|
28
|
+
y: number;
|
|
29
|
+
zoom: number;
|
|
30
|
+
} | null;
|
|
31
|
+
expandedRuleId?: string | null;
|
|
32
|
+
}
|
|
33
|
+
export declare const WORKFLOW_VIEW_STATE_PREFIX = "widgetic:editor-view:workflow:";
|
|
34
|
+
export interface DraftRestoreResult {
|
|
35
|
+
applied: boolean;
|
|
36
|
+
discardedAsStale: boolean;
|
|
37
|
+
clearedAsNoOp: boolean;
|
|
38
|
+
savedAtMs?: number;
|
|
39
|
+
}
|
|
40
|
+
export interface DraftRestoredInfo {
|
|
41
|
+
key: string;
|
|
42
|
+
savedAtMs: number;
|
|
43
|
+
}
|
|
44
|
+
export declare function payloadsEqual(left: WorkflowDraftPayload, right: WorkflowDraftPayload): boolean;
|
|
45
|
+
export declare function draftPayloadFromServerCopy(copy: {
|
|
46
|
+
graph: WorkflowGraph;
|
|
47
|
+
name: string;
|
|
48
|
+
timezone: string;
|
|
49
|
+
}): WorkflowDraftPayload;
|
|
50
|
+
export declare function serverBaseRevision(revision: number): string;
|
|
51
|
+
export declare function preserveStaleDraft<T>(journal: DraftJournal<T>, workflowId: string, entry: DraftJournalEntry<T>, logger?: {
|
|
52
|
+
debug(message: string, detail?: unknown): void;
|
|
53
|
+
}): void;
|
|
54
|
+
export declare function readStaleDraft<T>(journal: DraftJournal<T>, workflowId: string, nowMs: number): DraftJournalEntry<T> | null;
|
|
55
|
+
export declare function applyDraftRestoreRule(options: {
|
|
56
|
+
entry: DraftJournalEntry<WorkflowDraftPayload> | null;
|
|
57
|
+
serverRevision: string;
|
|
58
|
+
serverPayload: WorkflowDraftPayload;
|
|
59
|
+
journal: DraftJournal<WorkflowDraftPayload>;
|
|
60
|
+
workflowId: string;
|
|
61
|
+
nowMs: number;
|
|
62
|
+
logger?: {
|
|
63
|
+
debug(message: string, detail?: unknown): void;
|
|
64
|
+
};
|
|
65
|
+
}): DraftRestoreResult;
|
|
66
|
+
export declare function validateWorkflowEditorViewState(raw: unknown, validNodeIds: ReadonlySet<string>, validEdgeIds: ReadonlySet<string>): WorkflowEditorViewStateV1 | null;
|
|
67
|
+
export declare function viewStateStorageKey(workflowId: string): string;
|
|
68
|
+
export declare function serializeViewState(state: WorkflowEditorViewStateV1): string;
|
|
69
|
+
export declare function parseViewStateEntry(raw: string | null): WorkflowEditorViewStateV1 | null;
|
|
70
|
+
/** Whether a journalled workflow graph differs from what the server copy holds. */
|
|
71
|
+
export declare function draftDiffersFromServer(payload: WorkflowDraftPayload, serverPayload: WorkflowDraftPayload): boolean;
|
|
72
|
+
export {};
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
import { isSameGraph } from '../editor/graphModel.js';
|
|
2
|
+
export const WORKFLOW_DRAFT_NAMESPACE = 'workflow';
|
|
3
|
+
export const STALE_DRAFT_KEY_SUFFIX = ':stale';
|
|
4
|
+
export const STALE_DRAFT_MAX_AGE_MS = 24 * 60 * 60 * 1000;
|
|
5
|
+
export const KEEPALIVE_SAVE_MAX_BYTES = 60 * 1024;
|
|
6
|
+
export const WORKFLOW_VIEW_STATE_PREFIX = 'widgetic:editor-view:workflow:';
|
|
7
|
+
function stableSerialize(value) {
|
|
8
|
+
return JSON.stringify(value, (_key, inner) => {
|
|
9
|
+
if (inner && typeof inner === 'object' && !Array.isArray(inner)) {
|
|
10
|
+
const record = inner;
|
|
11
|
+
const sorted = {};
|
|
12
|
+
for (const key of Object.keys(record).sort()) {
|
|
13
|
+
sorted[key] = record[key];
|
|
14
|
+
}
|
|
15
|
+
return sorted;
|
|
16
|
+
}
|
|
17
|
+
return inner;
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
export function payloadsEqual(left, right) {
|
|
21
|
+
return stableSerialize(left) === stableSerialize(right);
|
|
22
|
+
}
|
|
23
|
+
export function draftPayloadFromServerCopy(copy) {
|
|
24
|
+
return { graph: copy.graph, name: copy.name, timezone: copy.timezone };
|
|
25
|
+
}
|
|
26
|
+
export function serverBaseRevision(revision) {
|
|
27
|
+
return String(revision);
|
|
28
|
+
}
|
|
29
|
+
function staleStorageKey(workflowId) {
|
|
30
|
+
return `${workflowId}${STALE_DRAFT_KEY_SUFFIX}`;
|
|
31
|
+
}
|
|
32
|
+
export function preserveStaleDraft(journal, workflowId, entry, logger) {
|
|
33
|
+
journal.flushNow(staleStorageKey(workflowId), entry.payload, entry.baseRevision);
|
|
34
|
+
journal.clear(workflowId);
|
|
35
|
+
logger?.debug('[workflowDraft] Draft kept as stale; server revision changed', {
|
|
36
|
+
workflowId,
|
|
37
|
+
baseRevision: entry.baseRevision
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
export function readStaleDraft(journal, workflowId, nowMs) {
|
|
41
|
+
const staleKey = staleStorageKey(workflowId);
|
|
42
|
+
const entry = journal.read(staleKey);
|
|
43
|
+
if (!entry)
|
|
44
|
+
return null;
|
|
45
|
+
if (nowMs - entry.savedAtMs > STALE_DRAFT_MAX_AGE_MS) {
|
|
46
|
+
journal.clear(staleKey);
|
|
47
|
+
return null;
|
|
48
|
+
}
|
|
49
|
+
return entry;
|
|
50
|
+
}
|
|
51
|
+
export function applyDraftRestoreRule(options) {
|
|
52
|
+
const { entry, serverRevision, serverPayload, journal, workflowId, nowMs, logger } = options;
|
|
53
|
+
if (!entry)
|
|
54
|
+
return { applied: false, discardedAsStale: false, clearedAsNoOp: false };
|
|
55
|
+
if (entry.baseRevision !== serverRevision) {
|
|
56
|
+
preserveStaleDraft(journal, workflowId, entry, logger);
|
|
57
|
+
return { applied: false, discardedAsStale: true, clearedAsNoOp: false };
|
|
58
|
+
}
|
|
59
|
+
if (payloadsEqual(entry.payload, serverPayload)) {
|
|
60
|
+
journal.clear(workflowId);
|
|
61
|
+
logger?.debug('[workflowDraft] Draft matched server; cleared without applying', { workflowId });
|
|
62
|
+
return { applied: false, discardedAsStale: false, clearedAsNoOp: true };
|
|
63
|
+
}
|
|
64
|
+
logger?.debug('[workflowDraft] Restored unsaved draft', {
|
|
65
|
+
workflowId,
|
|
66
|
+
savedAtMs: entry.savedAtMs
|
|
67
|
+
});
|
|
68
|
+
return {
|
|
69
|
+
applied: true,
|
|
70
|
+
discardedAsStale: false,
|
|
71
|
+
clearedAsNoOp: false,
|
|
72
|
+
savedAtMs: entry.savedAtMs
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
export function validateWorkflowEditorViewState(raw, validNodeIds, validEdgeIds) {
|
|
76
|
+
if (!raw || typeof raw !== 'object')
|
|
77
|
+
return null;
|
|
78
|
+
const candidate = raw;
|
|
79
|
+
if (candidate.v !== 1)
|
|
80
|
+
return null;
|
|
81
|
+
const tab = candidate.tab === 'runs' || candidate.tab === 'settings' ? candidate.tab : 'editor';
|
|
82
|
+
const panel = candidate.panel === 'history' || candidate.panel === 'test' ? candidate.panel : 'selection';
|
|
83
|
+
const filterIds = (value, valid) => Array.isArray(value)
|
|
84
|
+
? value.filter((item) => typeof item === 'string' && valid.has(item))
|
|
85
|
+
: [];
|
|
86
|
+
const selectedNodeIds = filterIds(candidate.selectedNodeIds, validNodeIds);
|
|
87
|
+
const selectedEdgeIds = filterIds(candidate.selectedEdgeIds, validEdgeIds);
|
|
88
|
+
let viewport = null;
|
|
89
|
+
if (candidate.viewport && typeof candidate.viewport === 'object') {
|
|
90
|
+
const view = candidate.viewport;
|
|
91
|
+
if (typeof view.x === 'number' &&
|
|
92
|
+
Number.isFinite(view.x) &&
|
|
93
|
+
typeof view.y === 'number' &&
|
|
94
|
+
Number.isFinite(view.y) &&
|
|
95
|
+
typeof view.zoom === 'number' &&
|
|
96
|
+
Number.isFinite(view.zoom) &&
|
|
97
|
+
view.zoom > 0) {
|
|
98
|
+
viewport = { x: view.x, y: view.y, zoom: view.zoom };
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
let expandedRuleId = null;
|
|
102
|
+
if (typeof candidate.expandedRuleId === 'string' && validNodeIds.has(candidate.expandedRuleId)) {
|
|
103
|
+
expandedRuleId = candidate.expandedRuleId;
|
|
104
|
+
}
|
|
105
|
+
else if (candidate.expandedRuleId === null) {
|
|
106
|
+
expandedRuleId = null;
|
|
107
|
+
}
|
|
108
|
+
return {
|
|
109
|
+
v: 1,
|
|
110
|
+
tab,
|
|
111
|
+
panel,
|
|
112
|
+
selectedNodeIds,
|
|
113
|
+
selectedEdgeIds,
|
|
114
|
+
viewport,
|
|
115
|
+
expandedRuleId
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
export function viewStateStorageKey(workflowId) {
|
|
119
|
+
return `${WORKFLOW_VIEW_STATE_PREFIX}${workflowId}`;
|
|
120
|
+
}
|
|
121
|
+
export function serializeViewState(state) {
|
|
122
|
+
return JSON.stringify(state);
|
|
123
|
+
}
|
|
124
|
+
export function parseViewStateEntry(raw) {
|
|
125
|
+
if (!raw)
|
|
126
|
+
return null;
|
|
127
|
+
try {
|
|
128
|
+
const parsed = JSON.parse(raw);
|
|
129
|
+
if (parsed?.v !== 1)
|
|
130
|
+
return null;
|
|
131
|
+
return parsed;
|
|
132
|
+
}
|
|
133
|
+
catch {
|
|
134
|
+
return null;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
/** Whether a journalled workflow graph differs from what the server copy holds. */
|
|
138
|
+
export function draftDiffersFromServer(payload, serverPayload) {
|
|
139
|
+
if (!isSameGraph(payload.graph, serverPayload.graph))
|
|
140
|
+
return true;
|
|
141
|
+
if (payload.name !== serverPayload.name)
|
|
142
|
+
return true;
|
|
143
|
+
if (payload.timezone !== serverPayload.timezone)
|
|
144
|
+
return true;
|
|
145
|
+
if (typeof payload.pendingName === 'string' &&
|
|
146
|
+
payload.pendingName.trim() !== '' &&
|
|
147
|
+
payload.pendingName.trim() !== payload.name) {
|
|
148
|
+
return true;
|
|
149
|
+
}
|
|
150
|
+
return false;
|
|
151
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Session view state and dialog draft payload for the workflows list screen.
|
|
3
|
+
*/
|
|
4
|
+
import { type SortDirection, type WorkflowSortField, type WorkflowStatus } from '../api/types.js';
|
|
5
|
+
type ListTab = 'all' | WorkflowStatus;
|
|
6
|
+
export declare const WORKFLOW_LIST_VIEW_STATE_PREFIX = "widgetic:editor-view:workflow-list:";
|
|
7
|
+
export declare const WORKFLOW_NEW_DIALOG_DRAFT_NAMESPACE = "workflow-new-dialog";
|
|
8
|
+
export interface WorkflowListViewStateV1 {
|
|
9
|
+
v: 1;
|
|
10
|
+
search: string;
|
|
11
|
+
tab: ListTab;
|
|
12
|
+
sortField: WorkflowSortField;
|
|
13
|
+
sortDirection: SortDirection;
|
|
14
|
+
scrollTop: number;
|
|
15
|
+
/** Row whose actions menu was open when the person left. */
|
|
16
|
+
openMenuWorkflowId: string | null;
|
|
17
|
+
}
|
|
18
|
+
export interface WorkflowNewDialogDraftPayload {
|
|
19
|
+
isOpen: boolean;
|
|
20
|
+
chosenPresetKey: string | null;
|
|
21
|
+
name: string;
|
|
22
|
+
}
|
|
23
|
+
export declare function listViewStateStorageKey(workspaceId: string): string;
|
|
24
|
+
export declare function serializeListViewState(state: WorkflowListViewStateV1): string;
|
|
25
|
+
export declare function parseListViewStateEntry(raw: string | null): WorkflowListViewStateV1 | null;
|
|
26
|
+
export declare function validateWorkflowListViewState(raw: unknown, validWorkflowIds: ReadonlySet<string>): WorkflowListViewStateV1 | null;
|
|
27
|
+
export declare function validateWorkflowNewDialogDraft(raw: unknown, validPresetKeys: ReadonlySet<string>): WorkflowNewDialogDraftPayload | null;
|
|
28
|
+
/** Defaults when nothing valid is stored (matches a freshly opened list). */
|
|
29
|
+
export declare function defaultWorkflowListViewState(): WorkflowListViewStateV1;
|
|
30
|
+
export {};
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Session view state and dialog draft payload for the workflows list screen.
|
|
3
|
+
*/
|
|
4
|
+
import { WORKFLOW_STATUSES } from '../api/types.js';
|
|
5
|
+
const LIST_TABS = ['all', ...WORKFLOW_STATUSES];
|
|
6
|
+
function isListTab(value) {
|
|
7
|
+
return LIST_TABS.includes(value);
|
|
8
|
+
}
|
|
9
|
+
export const WORKFLOW_LIST_VIEW_STATE_PREFIX = 'widgetic:editor-view:workflow-list:';
|
|
10
|
+
export const WORKFLOW_NEW_DIALOG_DRAFT_NAMESPACE = 'workflow-new-dialog';
|
|
11
|
+
const SORT_FIELDS = [
|
|
12
|
+
'createdAt',
|
|
13
|
+
'lastPublishedAt',
|
|
14
|
+
'runsCount',
|
|
15
|
+
'name'
|
|
16
|
+
];
|
|
17
|
+
export function listViewStateStorageKey(workspaceId) {
|
|
18
|
+
return `${WORKFLOW_LIST_VIEW_STATE_PREFIX}${workspaceId}`;
|
|
19
|
+
}
|
|
20
|
+
export function serializeListViewState(state) {
|
|
21
|
+
return JSON.stringify(state);
|
|
22
|
+
}
|
|
23
|
+
export function parseListViewStateEntry(raw) {
|
|
24
|
+
if (!raw)
|
|
25
|
+
return null;
|
|
26
|
+
try {
|
|
27
|
+
const parsed = JSON.parse(raw);
|
|
28
|
+
if (parsed?.v !== 1)
|
|
29
|
+
return null;
|
|
30
|
+
return parsed;
|
|
31
|
+
}
|
|
32
|
+
catch {
|
|
33
|
+
return null;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
export function validateWorkflowListViewState(raw, validWorkflowIds) {
|
|
37
|
+
if (!raw || typeof raw !== 'object')
|
|
38
|
+
return null;
|
|
39
|
+
const candidate = raw;
|
|
40
|
+
if (candidate.v !== 1)
|
|
41
|
+
return null;
|
|
42
|
+
const search = typeof candidate.search === 'string' ? candidate.search : '';
|
|
43
|
+
const tab = typeof candidate.tab === 'string' && isListTab(candidate.tab) ? candidate.tab : 'all';
|
|
44
|
+
const sortField = SORT_FIELDS.includes(candidate.sortField)
|
|
45
|
+
? candidate.sortField
|
|
46
|
+
: 'createdAt';
|
|
47
|
+
const sortDirection = candidate.sortDirection === 'asc' || candidate.sortDirection === 'desc'
|
|
48
|
+
? candidate.sortDirection
|
|
49
|
+
: 'desc';
|
|
50
|
+
const scrollTop = typeof candidate.scrollTop === 'number' && Number.isFinite(candidate.scrollTop)
|
|
51
|
+
? Math.max(0, candidate.scrollTop)
|
|
52
|
+
: 0;
|
|
53
|
+
let openMenuWorkflowId = null;
|
|
54
|
+
if (typeof candidate.openMenuWorkflowId === 'string' &&
|
|
55
|
+
validWorkflowIds.has(candidate.openMenuWorkflowId)) {
|
|
56
|
+
openMenuWorkflowId = candidate.openMenuWorkflowId;
|
|
57
|
+
}
|
|
58
|
+
else if (candidate.openMenuWorkflowId === null) {
|
|
59
|
+
openMenuWorkflowId = null;
|
|
60
|
+
}
|
|
61
|
+
return { v: 1, search, tab, sortField, sortDirection, scrollTop, openMenuWorkflowId };
|
|
62
|
+
}
|
|
63
|
+
export function validateWorkflowNewDialogDraft(raw, validPresetKeys) {
|
|
64
|
+
if (!raw || typeof raw !== 'object')
|
|
65
|
+
return null;
|
|
66
|
+
const candidate = raw;
|
|
67
|
+
if (typeof candidate.isOpen !== 'boolean')
|
|
68
|
+
return null;
|
|
69
|
+
const name = typeof candidate.name === 'string' ? candidate.name : '';
|
|
70
|
+
let chosenPresetKey = null;
|
|
71
|
+
if (typeof candidate.chosenPresetKey === 'string') {
|
|
72
|
+
chosenPresetKey = validPresetKeys.has(candidate.chosenPresetKey)
|
|
73
|
+
? candidate.chosenPresetKey
|
|
74
|
+
: null;
|
|
75
|
+
}
|
|
76
|
+
else if (candidate.chosenPresetKey === null) {
|
|
77
|
+
chosenPresetKey = null;
|
|
78
|
+
}
|
|
79
|
+
return { isOpen: candidate.isOpen, chosenPresetKey, name };
|
|
80
|
+
}
|
|
81
|
+
/** Defaults when nothing valid is stored (matches a freshly opened list). */
|
|
82
|
+
export function defaultWorkflowListViewState() {
|
|
83
|
+
return {
|
|
84
|
+
v: 1,
|
|
85
|
+
search: '',
|
|
86
|
+
tab: 'all',
|
|
87
|
+
sortField: 'createdAt',
|
|
88
|
+
sortDirection: 'desc',
|
|
89
|
+
scrollTop: 0,
|
|
90
|
+
openMenuWorkflowId: null
|
|
91
|
+
};
|
|
92
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { WorkflowDetail } from '../api/types.js';
|
|
2
|
+
/** The name every immediate blank start uses (the workspace allows duplicate names). */
|
|
3
|
+
export declare const BLANK_WORKFLOW_NAME = "Untitled";
|
|
4
|
+
/**
|
|
5
|
+
* The name for a new blank workflow. The database has no unique constraint on `workflows.name` per workspace, so every
|
|
6
|
+
* start uses {@link BLANK_WORKFLOW_NAME}.
|
|
7
|
+
*/
|
|
8
|
+
export declare function blankWorkflowName(_existingNames?: readonly string[]): string;
|
|
9
|
+
export type CreateBlankWorkflowResult = {
|
|
10
|
+
outcome: 'opened';
|
|
11
|
+
workflow: WorkflowDetail;
|
|
12
|
+
} | {
|
|
13
|
+
outcome: 'busy';
|
|
14
|
+
} | {
|
|
15
|
+
outcome: 'failed';
|
|
16
|
+
};
|
|
17
|
+
/**
|
|
18
|
+
* Makes a blank workflow and opens it. Double-clicks while a create is in flight are ignored; the caller shows
|
|
19
|
+
* {@link WorkflowListState.createFailure} when the API refused.
|
|
20
|
+
*/
|
|
21
|
+
export declare function createBlankWorkflowAndOpen(input: {
|
|
22
|
+
isCreating: () => boolean;
|
|
23
|
+
setCreating: (busy: boolean) => void;
|
|
24
|
+
create: (input: {
|
|
25
|
+
name: string;
|
|
26
|
+
composition: null;
|
|
27
|
+
}) => Promise<WorkflowDetail | undefined>;
|
|
28
|
+
open: (workflowId: string) => void;
|
|
29
|
+
existingNames?: readonly string[];
|
|
30
|
+
}): Promise<CreateBlankWorkflowResult>;
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/** The name every immediate blank start uses (the workspace allows duplicate names). */
|
|
2
|
+
export const BLANK_WORKFLOW_NAME = 'Untitled';
|
|
3
|
+
/**
|
|
4
|
+
* The name for a new blank workflow. The database has no unique constraint on `workflows.name` per workspace, so every
|
|
5
|
+
* start uses {@link BLANK_WORKFLOW_NAME}.
|
|
6
|
+
*/
|
|
7
|
+
export function blankWorkflowName(_existingNames) {
|
|
8
|
+
return BLANK_WORKFLOW_NAME;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Makes a blank workflow and opens it. Double-clicks while a create is in flight are ignored; the caller shows
|
|
12
|
+
* {@link WorkflowListState.createFailure} when the API refused.
|
|
13
|
+
*/
|
|
14
|
+
export async function createBlankWorkflowAndOpen(input) {
|
|
15
|
+
if (input.isCreating()) {
|
|
16
|
+
console.debug('[Workflows] createBlankWorkflowAndOpen: already creating, ignored');
|
|
17
|
+
return { outcome: 'busy' };
|
|
18
|
+
}
|
|
19
|
+
const name = blankWorkflowName(input.existingNames);
|
|
20
|
+
input.setCreating(true);
|
|
21
|
+
try {
|
|
22
|
+
console.debug('[Workflows] createBlankWorkflowAndOpen: creating', { name });
|
|
23
|
+
const created = await input.create({ name, composition: null });
|
|
24
|
+
if (!created) {
|
|
25
|
+
return { outcome: 'failed' };
|
|
26
|
+
}
|
|
27
|
+
input.open(created.id);
|
|
28
|
+
return { outcome: 'opened', workflow: created };
|
|
29
|
+
}
|
|
30
|
+
catch (error) {
|
|
31
|
+
console.debug('[Workflows] createBlankWorkflowAndOpen: unexpected failure', error);
|
|
32
|
+
return { outcome: 'failed' };
|
|
33
|
+
}
|
|
34
|
+
finally {
|
|
35
|
+
input.setCreating(false);
|
|
36
|
+
}
|
|
37
|
+
}
|