@gradio/core 1.0.0-dev.0 → 1.0.0

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 (109) hide show
  1. package/CHANGELOG.md +110 -0
  2. package/dist/index.d.ts +1 -1
  3. package/dist/index.js +1 -1
  4. package/dist/src/Blocks.svelte +534 -1001
  5. package/dist/src/Blocks.svelte.d.ts +32 -45
  6. package/dist/src/Embed.svelte +82 -55
  7. package/dist/src/Embed.svelte.d.ts +39 -30
  8. package/dist/src/Login.svelte +33 -29
  9. package/dist/src/Login.svelte.d.ts +21 -19
  10. package/dist/src/MountComponents.svelte +19 -25
  11. package/dist/src/MountComponents.svelte.d.ts +5 -28
  12. package/dist/src/{init.d.ts → _init.d.ts} +5 -4
  13. package/dist/src/{init.js → _init.js} +31 -108
  14. package/dist/src/api_docs/ApiBanner.svelte +12 -8
  15. package/dist/src/api_docs/ApiBanner.svelte.d.ts +22 -20
  16. package/dist/src/api_docs/ApiDocs.svelte +356 -247
  17. package/dist/src/api_docs/ApiDocs.svelte.d.ts +27 -24
  18. package/dist/src/api_docs/ApiRecorder.svelte +6 -3
  19. package/dist/src/api_docs/ApiRecorder.svelte.d.ts +19 -17
  20. package/dist/src/api_docs/CodeSnippet.svelte +122 -48
  21. package/dist/src/api_docs/CodeSnippet.svelte.d.ts +29 -25
  22. package/dist/src/api_docs/CopyButton.svelte +69 -13
  23. package/dist/src/api_docs/CopyButton.svelte.d.ts +18 -16
  24. package/dist/src/api_docs/CopyMarkdown.svelte +734 -0
  25. package/dist/src/api_docs/CopyMarkdown.svelte.d.ts +37 -0
  26. package/dist/src/api_docs/EndpointDetail.svelte +81 -23
  27. package/dist/src/api_docs/EndpointDetail.svelte.d.ts +23 -18
  28. package/dist/src/api_docs/IconArrowUpRight.svelte +34 -0
  29. package/dist/src/api_docs/IconArrowUpRight.svelte.d.ts +20 -0
  30. package/dist/src/api_docs/IconCaret.svelte +39 -0
  31. package/dist/src/api_docs/IconCaret.svelte.d.ts +20 -0
  32. package/dist/src/api_docs/IconHuggingChat.svelte +62 -0
  33. package/dist/src/api_docs/IconHuggingChat.svelte.d.ts +20 -0
  34. package/dist/src/api_docs/InputPayload.svelte +17 -11
  35. package/dist/src/api_docs/InputPayload.svelte.d.ts +25 -23
  36. package/dist/src/api_docs/InstallSnippet.svelte +9 -6
  37. package/dist/src/api_docs/InstallSnippet.svelte.d.ts +18 -16
  38. package/dist/src/api_docs/MCPSnippet.svelte +139 -126
  39. package/dist/src/api_docs/MCPSnippet.svelte.d.ts +60 -58
  40. package/dist/src/api_docs/NoApi.svelte +7 -4
  41. package/dist/src/api_docs/NoApi.svelte.d.ts +20 -18
  42. package/dist/src/api_docs/ParametersSnippet.svelte +8 -6
  43. package/dist/src/api_docs/ParametersSnippet.svelte.d.ts +21 -19
  44. package/dist/src/api_docs/PercentileChart.svelte +125 -0
  45. package/dist/src/api_docs/PercentileChart.svelte.d.ts +22 -0
  46. package/dist/src/api_docs/RecordingSnippet.svelte +124 -110
  47. package/dist/src/api_docs/RecordingSnippet.svelte.d.ts +24 -22
  48. package/dist/src/api_docs/ResponseSnippet.svelte +7 -5
  49. package/dist/src/api_docs/ResponseSnippet.svelte.d.ts +21 -19
  50. package/dist/src/api_docs/Settings.svelte +73 -62
  51. package/dist/src/api_docs/Settings.svelte.d.ts +25 -23
  52. package/dist/src/api_docs/SettingsBanner.svelte +11 -8
  53. package/dist/src/api_docs/SettingsBanner.svelte.d.ts +20 -18
  54. package/dist/src/api_docs/TryButton.svelte +5 -3
  55. package/dist/src/api_docs/TryButton.svelte.d.ts +19 -17
  56. package/dist/src/api_docs/img/IconCheck.svelte +33 -0
  57. package/dist/src/api_docs/img/IconCheck.svelte.d.ts +26 -0
  58. package/dist/src/api_docs/img/IconCopy.svelte +40 -0
  59. package/dist/src/api_docs/img/IconCopy.svelte.d.ts +26 -0
  60. package/dist/src/api_docs/img/clear.svelte.d.ts +22 -21
  61. package/dist/src/dependency.d.ts +145 -0
  62. package/dist/src/dependency.js +668 -0
  63. package/dist/src/init.svelte.d.ts +78 -0
  64. package/dist/src/init.svelte.js +469 -0
  65. package/dist/src/init_utils.d.ts +32 -0
  66. package/dist/src/init_utils.js +73 -0
  67. package/dist/src/lang/en.json +10 -1
  68. package/dist/src/lang/get_lang_names.js +0 -3
  69. package/dist/src/lang/ru.json +10 -1
  70. package/dist/src/stores.d.ts +0 -21
  71. package/dist/src/stories/I18nMultiLanguageTestComponent.svelte +5 -3
  72. package/dist/src/stories/I18nMultiLanguageTestComponent.svelte.d.ts +16 -14
  73. package/dist/src/stories/I18nTestSetup.svelte +14 -10
  74. package/dist/src/stories/I18nTestSetup.svelte.d.ts +18 -16
  75. package/dist/src/types.d.ts +31 -26
  76. package/index.ts +1 -1
  77. package/package.json +62 -63
  78. package/src/Blocks.svelte +360 -1063
  79. package/src/MountComponents.svelte +17 -27
  80. package/src/{init.ts → _init.ts} +49 -126
  81. package/src/api_docs/ApiDocs.svelte +84 -62
  82. package/src/api_docs/CodeSnippet.svelte +83 -24
  83. package/src/api_docs/CopyButton.svelte +61 -7
  84. package/src/api_docs/CopyMarkdown.svelte +734 -0
  85. package/src/api_docs/EndpointDetail.svelte +73 -17
  86. package/src/api_docs/IconArrowUpRight.svelte +34 -0
  87. package/src/api_docs/IconCaret.svelte +39 -0
  88. package/src/api_docs/IconHuggingChat.svelte +62 -0
  89. package/src/api_docs/MCPSnippet.svelte +44 -73
  90. package/src/api_docs/ParametersSnippet.svelte +1 -1
  91. package/src/api_docs/PercentileChart.svelte +125 -0
  92. package/src/api_docs/ResponseSnippet.svelte +1 -1
  93. package/src/api_docs/Settings.svelte +11 -11
  94. package/src/api_docs/img/IconCheck.svelte +33 -0
  95. package/src/api_docs/img/IconCopy.svelte +40 -0
  96. package/src/dependency.ts +909 -0
  97. package/src/init.svelte.ts +717 -0
  98. package/src/init_utils.ts +99 -0
  99. package/src/lang/en.json +10 -1
  100. package/src/lang/get_lang_names.js +0 -3
  101. package/src/lang/ru.json +10 -1
  102. package/src/stores.ts +22 -22
  103. package/src/types.ts +55 -43
  104. package/dist/src/Render.svelte +0 -105
  105. package/dist/src/Render.svelte.d.ts +0 -31
  106. package/dist/src/RenderComponent.svelte +0 -72
  107. package/dist/src/RenderComponent.svelte.d.ts +0 -33
  108. package/src/Render.svelte +0 -126
  109. package/src/RenderComponent.svelte +0 -91
@@ -0,0 +1,668 @@
1
+ import { AsyncFunction } from "./init_utils";
2
+ import { Client } from "@gradio/client";
3
+ import { LoadingStatus } from "@gradio/statustracker";
4
+ const MESSAGE_QUOTE_RE = /^'([^]+)'$/;
5
+ const NOVALUE = Symbol("NOVALUE");
6
+ /**
7
+ * A dependency as used by the frontend
8
+ * This class represents a discrete dependency that can be triggered by an event
9
+ * It is responsible calling the appropriate functions and reporting back results
10
+ */
11
+ export class Dependency {
12
+ id;
13
+ inputs;
14
+ outputs;
15
+ cancels;
16
+ pending = false;
17
+ trigger_modes;
18
+ event_args = {};
19
+ targets = [];
20
+ connection_type;
21
+ // if this dependency has any then, success or failure triggers
22
+ triggers = [];
23
+ // the id of the original event_id that caused this dependency to run
24
+ // in the case of chained events, it would be the id of the initial trigger
25
+ original_trigger_id = null;
26
+ show_progress_on = null;
27
+ component_prop_inputs = [];
28
+ show_progress;
29
+ functions;
30
+ constructor(dep_config) {
31
+ this.id = dep_config.id;
32
+ this.original_trigger_id = dep_config.id;
33
+ this.inputs = dep_config.inputs;
34
+ this.outputs = dep_config.outputs;
35
+ this.connection_type = dep_config.connection;
36
+ this.show_progress = dep_config.show_progress;
37
+ this.functions = {
38
+ frontend: dep_config.js
39
+ ? process_frontend_fn(dep_config.js, dep_config.backend_fn, dep_config.inputs.length, dep_config.outputs.length)
40
+ : undefined,
41
+ backend: dep_config.backend_fn,
42
+ backend_js: dep_config.js_implementation
43
+ ? new AsyncFunction(`let result = await (${dep_config.js_implementation})(...arguments);
44
+ return (!Array.isArray(result)) ? [result] : result;`)
45
+ : undefined
46
+ };
47
+ this.targets = dep_config.targets;
48
+ this.cancels = dep_config.cancels;
49
+ this.trigger_modes = dep_config.trigger_mode;
50
+ this.show_progress_on = dep_config.show_progress_on || null;
51
+ this.component_prop_inputs = dep_config.component_prop_inputs || [];
52
+ for (let i = 0; i < dep_config.event_specific_args?.length || 0; i++) {
53
+ const key = dep_config.event_specific_args[i];
54
+ this.event_args[key] = dep_config[key] ?? null;
55
+ }
56
+ }
57
+ async run(client, data_payload, event_data, target_id) {
58
+ let _data_payload = data_payload;
59
+ // if the function is backend_js, then it's the entire event
60
+ // no need to chain frontend and backend
61
+ if (this.functions.backend_js) {
62
+ const data = await this.functions.backend_js(..._data_payload);
63
+ return { type: "data", data };
64
+ }
65
+ // If it has a js implementation, the correct behavior
66
+ // is to run that and pass the output to the backend
67
+ if (this.functions.frontend) {
68
+ _data_payload = await this.functions.frontend(data_payload);
69
+ }
70
+ if (this.functions.backend) {
71
+ return {
72
+ type: "submit",
73
+ data: client.submit(this.id, _data_payload, event_data, target_id)
74
+ };
75
+ }
76
+ else if (this.functions.frontend) {
77
+ return { type: "data", data: _data_payload };
78
+ }
79
+ return { type: "void", data: null };
80
+ }
81
+ add_trigger(dep_id, condition) {
82
+ this.triggers.push([dep_id, condition]);
83
+ }
84
+ get_triggers() {
85
+ return {
86
+ success: this.triggers
87
+ .filter(([, condition]) => condition === "success")
88
+ .map(([id]) => id),
89
+ failure: this.triggers
90
+ .filter(([, condition]) => condition === "failure")
91
+ .map(([id]) => id),
92
+ all: this.triggers
93
+ .filter(([, condition]) => condition === "all")
94
+ .map(([id]) => id)
95
+ };
96
+ }
97
+ }
98
+ /**
99
+ * Manages all dependencies for an app acting as a bridge between app state and Dependencies
100
+ * Responsible for registering dependencies and dispatching events to them
101
+ * It is also responsible for orchestrating dependencies based on the follwing:
102
+ * - Cancelling dependencies
103
+ * - Ensuring individual dependencies respect `trigger_mode`
104
+ * - Managing then, success and failure events
105
+ * - Ensuring that dependencies bound to the same id are treated a single unit
106
+ * - updating loading states
107
+ * - updating component states
108
+ */
109
+ export class DependencyManager {
110
+ dependencies_by_fn = new Map();
111
+ dependencies_by_event = new Map();
112
+ render_id_deps = new Map();
113
+ submissions = new Map();
114
+ client;
115
+ queue = new Set();
116
+ add_to_api_calls;
117
+ update_state_cb;
118
+ get_state_cb;
119
+ rerender_cb;
120
+ log_cb;
121
+ loading_stati = new LoadingStatus();
122
+ constructor(dependencies, client, update_state_cb, get_state_cb, rerender_cb, log_cb, add_to_api_calls) {
123
+ this.add_to_api_calls = add_to_api_calls;
124
+ this.log_cb = log_cb;
125
+ this.update_state_cb = update_state_cb;
126
+ this.get_state_cb = get_state_cb;
127
+ this.rerender_cb = rerender_cb;
128
+ this.reload(dependencies, update_state_cb, get_state_cb, rerender_cb, client);
129
+ }
130
+ reload(dependencies, update_state, get_state, rerender, client) {
131
+ const { by_id, by_event } = this.create(dependencies);
132
+ this.dependencies_by_event = by_event;
133
+ this.dependencies_by_fn = by_id;
134
+ this.client = client;
135
+ this.update_state_cb = update_state;
136
+ this.get_state_cb = get_state;
137
+ this.rerender_cb = rerender;
138
+ for (const [dep_id, dep] of this.dependencies_by_fn) {
139
+ for (const [output_id] of dep.targets) {
140
+ this.set_event_args(output_id, dep.event_args);
141
+ }
142
+ }
143
+ this.register_loading_stati(by_id);
144
+ }
145
+ register_loading_stati(deps) {
146
+ for (const [_, dep] of deps) {
147
+ this.loading_stati.register(dep.id, dep.show_progress_on || dep.outputs, dep.inputs, dep.show_progress);
148
+ }
149
+ }
150
+ clear_loading_status(component_id) {
151
+ this.loading_stati.clear(component_id);
152
+ }
153
+ async update_loading_stati_state() {
154
+ for (const [component_id, loading_status] of Object.entries(this.loading_stati.current)) {
155
+ this.update_state_cb(Number(component_id), {
156
+ loading_status: loading_status
157
+ }, false);
158
+ }
159
+ }
160
+ dispatch_state_change_events(result) {
161
+ if (result.changed_state_ids) {
162
+ for (const changed_id of result.changed_state_ids) {
163
+ const change_dep = this.dependencies_by_event.get("change-" + changed_id);
164
+ change_dep?.forEach((dep) => {
165
+ this.dispatch({
166
+ type: "fn",
167
+ fn_index: dep.id,
168
+ target_id: changed_id,
169
+ event_data: null
170
+ });
171
+ });
172
+ }
173
+ }
174
+ }
175
+ /** Dispatches an event to the appropriate dependency
176
+ * @param event_name the name of the event
177
+ * @param target_id the id of the component that triggered the event
178
+ * @param event_data any additional data to pass to the dependency
179
+ * @returns a value if there is no backend fn, a 'submission' if there is a backend fn, or null if there is no dependency
180
+ */
181
+ async dispatch(event_meta) {
182
+ let deps;
183
+ if (event_meta.type === "fn") {
184
+ const dep = this.dependencies_by_fn.get(event_meta.fn_index);
185
+ if (dep)
186
+ deps = [dep];
187
+ }
188
+ else {
189
+ deps = this.dependencies_by_event.get(`${event_meta.event_name}-${event_meta.target_id}`);
190
+ }
191
+ for (let i = 0; i < (deps?.length || 0); i++) {
192
+ const dep = deps ? deps[i] : undefined;
193
+ if (dep) {
194
+ this.cancel(dep.cancels);
195
+ const dispatch_status = should_dispatch(dep.trigger_modes, this.submissions.has(dep.id));
196
+ if (dispatch_status === "skip") {
197
+ continue;
198
+ }
199
+ else if (dispatch_status === "defer") {
200
+ this.queue.add(dep.id);
201
+ continue;
202
+ }
203
+ // No loading status for js-only deps
204
+ if (dep.functions.backend) {
205
+ this.loading_stati.update({
206
+ status: "pending",
207
+ fn_index: dep.id,
208
+ stream_state: null
209
+ });
210
+ this.update_loading_stati_state();
211
+ }
212
+ const data_payload = await this.gather_state(dep.inputs, dep.component_prop_inputs);
213
+ const unset_args = await Promise.all(dep.targets.map(([output_id]) => this.set_event_args(output_id, dep.event_args)));
214
+ const { success, failure, all } = dep.get_triggers();
215
+ try {
216
+ let target_id = null;
217
+ if (event_meta.target_id !== undefined ||
218
+ event_meta.type === "event") {
219
+ target_id = event_meta.target_id || null;
220
+ }
221
+ else {
222
+ target_id = dep.original_trigger_id;
223
+ }
224
+ if (dep.connection_type === "stream" &&
225
+ this.submissions.has(dep.id)) {
226
+ const submission = this.submissions.get(dep.id);
227
+ let payload = {
228
+ fn_index: dep.id,
229
+ data: data_payload,
230
+ event_data: event_meta.event_data
231
+ };
232
+ submission.send_chunk(payload);
233
+ unset_args.forEach((fn) => fn());
234
+ continue;
235
+ }
236
+ this.add_to_api_calls({
237
+ fn_index: dep.id,
238
+ data: data_payload,
239
+ event_data: event_meta.event_data,
240
+ trigger_id: target_id
241
+ });
242
+ const dep_submission = await dep.run(this.client, data_payload, event_meta.event_data, target_id);
243
+ if (dep_submission.type === "void") {
244
+ unset_args.forEach((fn) => fn());
245
+ }
246
+ else if (dep_submission.type === "data") {
247
+ await this.handle_data(dep.outputs, dep_submission.data);
248
+ unset_args.forEach((fn) => fn());
249
+ }
250
+ else {
251
+ let stream_state = null;
252
+ if (dep.connection_type === "stream" &&
253
+ !this.submissions.has(dep.id)) {
254
+ stream_state = "waiting";
255
+ }
256
+ this.submissions.set(dep.id, dep_submission.data);
257
+ let index = 0;
258
+ // fn for this?
259
+ submit_loop: for await (const result of dep_submission.data) {
260
+ if (index === 0) {
261
+ // Clear out previously set validation errors
262
+ dep.inputs.forEach((input_id) => {
263
+ this.update_state_cb(input_id, {
264
+ loading_status: {
265
+ validation_error: null
266
+ }
267
+ }, false);
268
+ });
269
+ }
270
+ index += 1;
271
+ if (result === null)
272
+ continue;
273
+ if (result.type === "data") {
274
+ await this.handle_data(dep.outputs, result.data);
275
+ }
276
+ if (result.type === "status") {
277
+ if (result.original_msg === "process_starts" &&
278
+ dep.connection_type === "stream") {
279
+ stream_state = "open";
280
+ }
281
+ const { fn_index, ...status } = result;
282
+ // handle status updates here
283
+ if (result.stage === "complete") {
284
+ stream_state = "closed";
285
+ success.forEach((dep_id) => {
286
+ this.dispatch({
287
+ type: "fn",
288
+ fn_index: dep_id,
289
+ event_data: null,
290
+ target_id: target_id
291
+ });
292
+ });
293
+ this.dispatch_state_change_events(result);
294
+ // @ts-ignore
295
+ this.loading_stati.update({
296
+ ...status,
297
+ status: status.stage,
298
+ fn_index: dep.id,
299
+ stream_state
300
+ });
301
+ this.update_loading_stati_state();
302
+ break submit_loop;
303
+ }
304
+ else if (result.stage === "generating") {
305
+ this.dispatch_state_change_events(result);
306
+ // @ts-ignore
307
+ this.loading_stati.update({
308
+ ...status,
309
+ status: status.stage,
310
+ fn_index: dep.id,
311
+ stream_state
312
+ });
313
+ this.update_loading_stati_state();
314
+ }
315
+ else if (result.stage === "error") {
316
+ if (Array.isArray(result?.message)) {
317
+ result.message.forEach((m, i) => {
318
+ this.update_state_cb(dep.inputs[i], {
319
+ loading_status: {
320
+ validation_error: !m.is_valid ? m.message : null,
321
+ show_validation_error: true
322
+ }
323
+ }, false);
324
+ });
325
+ // Manually set the output statuses to null
326
+ // Doing this in update_loading_stati_state would
327
+ // validation errors set above
328
+ // For example, if the input component is an output component (chatinterface)
329
+ dep.outputs.forEach((output_id) => {
330
+ if (dep.inputs.includes(output_id))
331
+ return;
332
+ this.update_state_cb(output_id, {
333
+ loading_status: {
334
+ status: null
335
+ }
336
+ }, false);
337
+ });
338
+ unset_args.forEach((fn) => fn());
339
+ this.submissions.delete(dep.id);
340
+ if (this.queue.has(dep.id)) {
341
+ this.queue.delete(dep.id);
342
+ this.dispatch(event_meta);
343
+ }
344
+ return;
345
+ }
346
+ const _message = result?.message?.replace(MESSAGE_QUOTE_RE, (_, b) => b);
347
+ this.log_cb(result._title ?? "Error", _message, fn_index, "error", status.duration, status.visible);
348
+ throw new Error("Dependency function failed");
349
+ }
350
+ else {
351
+ // @ts-ignore
352
+ this.loading_stati.update({
353
+ ...status,
354
+ status: status.stage,
355
+ fn_index: dep.id,
356
+ stream_state
357
+ });
358
+ this.update_loading_stati_state();
359
+ }
360
+ }
361
+ if (result.type === "render") {
362
+ this.loading_stati.update({
363
+ status: "complete",
364
+ fn_index: dep.id,
365
+ stream_state: null
366
+ });
367
+ this.update_loading_stati_state();
368
+ const { layout, components, render_id, dependencies } = result.data;
369
+ this.rerender_cb(components, layout);
370
+ // update dependencies
371
+ const { by_id, by_event } = this.create(dependencies);
372
+ this.register_loading_stati(by_id);
373
+ by_id.forEach((dep) => this.dependencies_by_fn.set(dep.id, dep));
374
+ by_event.forEach((dep, key) => this.dependencies_by_event.set(key, dep));
375
+ const current_deps = this.render_id_deps.get(render_id);
376
+ if (current_deps) {
377
+ current_deps.forEach((old_dep_id) => {
378
+ if (!by_id.has(old_dep_id)) {
379
+ this.dependencies_by_fn.delete(old_dep_id);
380
+ }
381
+ });
382
+ }
383
+ this.render_id_deps.set(render_id, new Set(Array.from(by_id.keys())));
384
+ this.register_loading_stati(by_id);
385
+ break submit_loop;
386
+ }
387
+ if (result.type === "log") {
388
+ this.handle_log(result);
389
+ }
390
+ }
391
+ all.forEach((dep_id) => {
392
+ this.dispatch({
393
+ type: "fn",
394
+ fn_index: dep_id,
395
+ event_data: null,
396
+ target_id: target_id
397
+ });
398
+ });
399
+ unset_args.forEach((fn) => fn());
400
+ this.submissions.delete(dep.id);
401
+ if (this.queue.has(dep.id)) {
402
+ this.queue.delete(dep.id);
403
+ this.dispatch(event_meta);
404
+ }
405
+ }
406
+ }
407
+ catch (error) {
408
+ this.loading_stati.update({
409
+ status: "error",
410
+ fn_index: dep.id,
411
+ eta: 0,
412
+ queue: false,
413
+ stream_state: null
414
+ });
415
+ this.update_loading_stati_state();
416
+ this.submissions.delete(dep.id);
417
+ failure.forEach((dep_id) => {
418
+ this.dispatch({
419
+ type: "fn",
420
+ fn_index: dep_id,
421
+ event_data: null
422
+ });
423
+ });
424
+ }
425
+ }
426
+ }
427
+ return;
428
+ }
429
+ /**
430
+ * Creates a map of dependencies for easy lookup
431
+ *
432
+ * @param dependencies the list of dependencies from the backend
433
+ * @returns a map of dependencies keyed by `${event_name}-${target_id}`
434
+ * */
435
+ create(dependencies) {
436
+ const _deps_by_id = new Map();
437
+ const _deps_by_event = new Map();
438
+ const then_triggers = [];
439
+ for (const dep_config of dependencies) {
440
+ const dependency = new Dependency(dep_config);
441
+ for (const [target_id, event_name] of dep_config.targets) {
442
+ // if the key is already present, add it to the list. Otherwise, create a new element with the list
443
+ if (!_deps_by_event.has(`${event_name}-${target_id}`)) {
444
+ _deps_by_event.set(`${event_name}-${target_id}`, []);
445
+ }
446
+ _deps_by_event.get(`${event_name}-${target_id}`)?.push(dependency);
447
+ }
448
+ _deps_by_id.set(dep_config.id, dependency);
449
+ if (dep_config.trigger_after !== undefined) {
450
+ const then_mode = dep_config.trigger_only_on_failure
451
+ ? "failure"
452
+ : dep_config.trigger_only_on_success
453
+ ? "success"
454
+ : "all";
455
+ then_triggers.push([
456
+ dep_config.id,
457
+ dep_config.trigger_after,
458
+ then_mode
459
+ ]);
460
+ }
461
+ }
462
+ for (const [dep_id, trigger_after, condition] of then_triggers) {
463
+ const dependency = _deps_by_id.get(trigger_after);
464
+ if (dependency) {
465
+ dependency.add_trigger(dep_id, condition);
466
+ dependency.original_trigger_id = walk_after_to_original(dependencies, trigger_after);
467
+ }
468
+ }
469
+ return { by_id: _deps_by_id, by_event: _deps_by_event };
470
+ }
471
+ handle_log(msg) {
472
+ const { title, log, fn_index, level, duration, visible } = msg;
473
+ this.log_cb(title, log, fn_index, level, duration, visible);
474
+ }
475
+ /**
476
+ * Updates the state of the outputs based on the data received from the dependency
477
+ *
478
+ * @param outputs the ids of the output components
479
+ * @param data the data to update the components with
480
+ * */
481
+ async handle_data(outputs, data) {
482
+ await Promise.all(outputs.map(async (output_id, i) => {
483
+ const _data = data[i] === undefined ? NOVALUE : data[i];
484
+ if (_data === NOVALUE)
485
+ return;
486
+ if (is_prop_update(_data)) {
487
+ let pending_visibility_update = false;
488
+ let pending_visibility_value = null;
489
+ for (const [update_key, update_value] of Object.entries(_data)) {
490
+ if (update_key === "__type__")
491
+ continue;
492
+ if (update_key === "visible") {
493
+ pending_visibility_update = true;
494
+ pending_visibility_value = update_value;
495
+ continue;
496
+ }
497
+ await this.update_state_cb(outputs[i], {
498
+ [update_key]: update_value
499
+ }, false);
500
+ }
501
+ if (pending_visibility_update) {
502
+ await this.update_state_cb(outputs[i], {
503
+ visible: pending_visibility_value
504
+ }, true);
505
+ }
506
+ }
507
+ else {
508
+ await this.update_state_cb(output_id, { value: _data }, false);
509
+ }
510
+ }));
511
+ }
512
+ /**
513
+ * Gathers the current state of the inputs
514
+ *
515
+ * @param ids the ids of the components to gather state from
516
+ * @param prop_indices the indices (relative to ids array) that should return all component props instead of just the value
517
+ * @returns an array of the current state of the components, in the same order as the ids
518
+ */
519
+ async gather_state(ids, prop_indices = []) {
520
+ return (await Promise.all(ids.map((id) => this.get_state_cb(id)))).map((state, index) => {
521
+ if (prop_indices.includes(index)) {
522
+ return state ?? null;
523
+ }
524
+ return state?.value ?? null;
525
+ });
526
+ }
527
+ /** Sets the event arguments for a specific component
528
+ *
529
+ * @param id the id of the component to set the event arguments for
530
+ * @param args the event arguments to set
531
+ * @returns a function that can be called to reset the event arguments to their previous values
532
+ */
533
+ async set_event_args(id, args) {
534
+ let current_args = {};
535
+ const current_state = await this.get_state_cb?.(id);
536
+ if (!current_state)
537
+ return () => { };
538
+ for (const [key] of Object.entries(args)) {
539
+ current_args[key] = current_state?.[key] ?? null;
540
+ }
541
+ if (Object.keys(args).length === 0) {
542
+ return () => {
543
+ // do nothing
544
+ };
545
+ }
546
+ await this.update_state_cb(id, args, false);
547
+ return () => {
548
+ this.update_state_cb(id, current_args, false);
549
+ };
550
+ }
551
+ async cancel(ids) {
552
+ if (!ids)
553
+ return;
554
+ for (const id of ids) {
555
+ const submission = this.submissions.get(id);
556
+ if (submission) {
557
+ await submission.cancel();
558
+ this.submissions.delete(id);
559
+ }
560
+ }
561
+ }
562
+ dispatch_load_events() {
563
+ this.dependencies_by_fn.forEach((dep) => {
564
+ dep.targets.forEach(([target_id, event_name]) => {
565
+ if (event_name === "load") {
566
+ this.dispatch({
567
+ type: "fn",
568
+ fn_index: dep.id,
569
+ event_data: null,
570
+ target_id: target_id
571
+ });
572
+ }
573
+ });
574
+ });
575
+ }
576
+ get_fns_from_targets(target_id) {
577
+ const fn_indices = [];
578
+ this.dependencies_by_event.forEach((deps, key) => {
579
+ const [, dep_target_id] = key.split("-");
580
+ if (Number(dep_target_id) === target_id) {
581
+ deps.forEach((dep) => {
582
+ fn_indices.push(dep.id);
583
+ });
584
+ }
585
+ });
586
+ return fn_indices;
587
+ }
588
+ close_stream(id) {
589
+ const fn_ids = this.get_fns_from_targets(id);
590
+ for (const fn_id of fn_ids) {
591
+ const submission = this.submissions.get(fn_id);
592
+ if (submission) {
593
+ submission.close_stream();
594
+ this.submissions.delete(fn_id);
595
+ }
596
+ this.loading_stati.update({
597
+ status: "complete",
598
+ fn_index: fn_id,
599
+ eta: 0,
600
+ queue: false,
601
+ stream_state: "closed"
602
+ });
603
+ }
604
+ this.update_loading_stati_state();
605
+ }
606
+ }
607
+ function is_prop_update(payload) {
608
+ return (typeof payload === "object" &&
609
+ payload !== null &&
610
+ "__type__" in payload &&
611
+ payload?.__type__ === "update");
612
+ }
613
+ function should_dispatch(mode, is_running) {
614
+ if (!is_running)
615
+ return "run";
616
+ if (mode === "always_last") {
617
+ return "defer";
618
+ }
619
+ else if (mode === "multiple") {
620
+ return "run";
621
+ }
622
+ else if (mode === "once") {
623
+ return "skip";
624
+ }
625
+ return "run";
626
+ }
627
+ /**
628
+ * Takes a string of source code and returns a function that can be called with arguments
629
+ * @param source the source code
630
+ * @param backend_fn if there is also a backend function
631
+ * @param input_length the number of inputs
632
+ * @param output_length the number of outputs
633
+ * @returns The function, or null if the source code is invalid or missing
634
+ */
635
+ export function process_frontend_fn(source, backend_fn, input_length, output_length) {
636
+ const wrap = backend_fn ? input_length === 1 : output_length === 1;
637
+ try {
638
+ return new AsyncFunction("__fn_args", ` let result = await (${source})(...__fn_args);
639
+ if (typeof result === "undefined") return [];
640
+ return (${wrap} && !Array.isArray(result)) ? [result] : result;`);
641
+ }
642
+ catch (e) {
643
+ throw e;
644
+ }
645
+ }
646
+ /**
647
+ * Walks the dependency graph to find the original trigger ID for a given dependency.
648
+ * @param dependency_map The map of all dependencies.
649
+ * @param dep_id The ID of the dependency to start from.
650
+ * @returns The ID of the original trigger dependency, or the input ID if not found.
651
+ */
652
+ function walk_after_to_original(dependency_map, dep_id) {
653
+ // TODO: hoist this cache later so it is useful across multiple calls
654
+ let cache = new Map();
655
+ let current_id = dep_id;
656
+ let safety_counter = 0;
657
+ while (safety_counter < 100) {
658
+ const dep = cache.get(current_id) || dependency_map.find((d) => d.id === current_id);
659
+ if (!dep)
660
+ break;
661
+ cache.set(dep.id, dep);
662
+ if (dep.trigger_after === null || dep.trigger_after === undefined)
663
+ break;
664
+ current_id = dep.trigger_after;
665
+ safety_counter += 1;
666
+ }
667
+ return current_id;
668
+ }