@widgetic/workflows 0.2.15 → 0.2.17

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 (108) hide show
  1. package/README.md +117 -3
  2. package/dist/api/types.d.ts +17 -0
  3. package/dist/api/workflowsApi.d.ts +12 -1
  4. package/dist/api/workflowsApi.js +37 -0
  5. package/dist/assistant/assistantEvents.d.ts +44 -2
  6. package/dist/assistant/assistantEvents.js +58 -2
  7. package/dist/assistant/assistantReferences.d.ts +66 -0
  8. package/dist/assistant/assistantReferences.js +109 -0
  9. package/dist/assistant/assistantSaveMode.d.ts +15 -3
  10. package/dist/assistant/assistantSaveMode.js +16 -2
  11. package/dist/assistant/assistantSeed.d.ts +55 -0
  12. package/dist/assistant/assistantSeed.js +90 -0
  13. package/dist/assistant/assistantStream.js +81 -3
  14. package/dist/assistant/assistantSuggest.d.ts +18 -0
  15. package/dist/assistant/assistantSuggest.js +21 -0
  16. package/dist/assistant/assistantTurns.d.ts +75 -0
  17. package/dist/assistant/assistantTurns.js +56 -0
  18. package/dist/assistant/attachmentUploads.d.ts +42 -0
  19. package/dist/assistant/attachmentUploads.js +69 -0
  20. package/dist/assistant/canvasLink.d.ts +30 -0
  21. package/dist/assistant/canvasLink.js +51 -0
  22. package/dist/assistant/compositionMention.d.ts +18 -0
  23. package/dist/assistant/compositionMention.js +47 -0
  24. package/dist/assistant/liveData.d.ts +150 -0
  25. package/dist/assistant/liveData.js +286 -0
  26. package/dist/assistant/mcpMention.d.ts +20 -0
  27. package/dist/assistant/mcpMention.js +30 -0
  28. package/dist/assistant/mentionEntries.d.ts +39 -0
  29. package/dist/assistant/mentionEntries.js +69 -0
  30. package/dist/assistant/planFlow.d.ts +26 -0
  31. package/dist/assistant/planFlow.js +57 -0
  32. package/dist/assistant/templateDestinations.d.ts +37 -0
  33. package/dist/assistant/templateDestinations.js +64 -0
  34. package/dist/assistant/types.d.ts +99 -2
  35. package/dist/assistant/workflowPlanViewModel.d.ts +13 -2
  36. package/dist/assistant/workflowPlanViewModel.js +9 -1
  37. package/dist/components/Workflows.svelte +7 -1
  38. package/dist/components/WorkflowsEmptyState.svelte +8 -3
  39. package/dist/components/WorkflowsWorkspace.svelte +29 -1
  40. package/dist/components/WorkflowsWorkspace.svelte.d.ts +3 -0
  41. package/dist/components/assistant/AssistantPlanStepRow.svelte +56 -0
  42. package/dist/components/assistant/AssistantPlanStepRow.svelte.d.ts +9 -0
  43. package/dist/components/assistant/CompositionMentionMenu.svelte +135 -0
  44. package/dist/components/assistant/CompositionMentionMenu.svelte.d.ts +9 -0
  45. package/dist/components/assistant/DataPreviewCard.svelte +108 -0
  46. package/dist/components/assistant/DataPreviewCard.svelte.d.ts +7 -0
  47. package/dist/components/assistant/PlanBindingsBlock.svelte +200 -0
  48. package/dist/components/assistant/PlanBindingsBlock.svelte.d.ts +14 -0
  49. package/dist/components/assistant/TemplateProjectCard.svelte +81 -0
  50. package/dist/components/assistant/TemplateProjectCard.svelte.d.ts +14 -0
  51. package/dist/components/assistant/WorkflowAssistantPanel.svelte +1045 -147
  52. package/dist/components/assistant/WorkflowAssistantPanel.svelte.d.ts +5 -0
  53. package/dist/components/canvas/CanvasContextMenu.svelte +199 -0
  54. package/dist/components/canvas/CanvasContextMenu.svelte.d.ts +16 -0
  55. package/dist/components/canvas/CompositionNodeView.svelte +7 -2
  56. package/dist/components/canvas/ConditionNodeView.svelte +5 -1
  57. package/dist/components/canvas/HandlePoint.svelte +25 -3
  58. package/dist/components/canvas/WorkflowCanvas.svelte +351 -12
  59. package/dist/components/canvas/WorkflowCanvas.svelte.d.ts +12 -1
  60. package/dist/components/canvas/altDragDuplicateController.d.ts +29 -0
  61. package/dist/components/canvas/altDragDuplicateController.js +92 -0
  62. package/dist/components/canvas/connectionSnapController.d.ts +29 -0
  63. package/dist/components/canvas/connectionSnapController.js +182 -0
  64. package/dist/components/canvas/spacePanController.d.ts +16 -0
  65. package/dist/components/canvas/spacePanController.js +103 -0
  66. package/dist/components/editor/EditorCanvasArea.svelte +98 -4
  67. package/dist/components/editor/EditorCanvasArea.svelte.d.ts +5 -0
  68. package/dist/components/editor/EditorTopBar.svelte +53 -42
  69. package/dist/components/editor/WorkflowEditor.svelte +74 -41
  70. package/dist/components/editor/WorkflowEditor.svelte.d.ts +5 -0
  71. package/dist/components/editor/WorkflowNameControl.svelte +9 -8
  72. package/dist/components/editor/WorkflowNameControl.svelte.d.ts +3 -1
  73. package/dist/components/list/WorkflowList.svelte +50 -26
  74. package/dist/components/list/WorkflowList.svelte.d.ts +5 -0
  75. package/dist/components/panel/CompositionForm.svelte +3 -2
  76. package/dist/components/panel/CompositionPicker.svelte +15 -20
  77. package/dist/components/panel/CompositionPicker.svelte.d.ts +2 -2
  78. package/dist/components/panel/CompositionSelect.svelte +4 -4
  79. package/dist/components/panel/CompositionSelect.svelte.d.ts +2 -2
  80. package/dist/components/ui/icons.d.ts +8 -0
  81. package/dist/components/ui/icons.js +24 -1
  82. package/dist/editor/autoLayout.d.ts +38 -0
  83. package/dist/editor/autoLayout.js +403 -0
  84. package/dist/editor/blockClipboard.svelte.d.ts +9 -0
  85. package/dist/editor/blockClipboard.svelte.js +24 -0
  86. package/dist/editor/blockCopy.d.ts +39 -0
  87. package/dist/editor/blockCopy.js +137 -0
  88. package/dist/editor/canvasMenu.d.ts +45 -0
  89. package/dist/editor/canvasMenu.js +68 -0
  90. package/dist/editor/connectionSnap.d.ts +90 -0
  91. package/dist/editor/connectionSnap.js +98 -0
  92. package/dist/editor/editorState.svelte.d.ts +122 -13
  93. package/dist/editor/editorState.svelte.js +262 -22
  94. package/dist/editor/graphIssues.d.ts +5 -0
  95. package/dist/editor/graphIssues.js +18 -0
  96. package/dist/editor/graphModel.d.ts +6 -1
  97. package/dist/editor/graphModel.js +8 -1
  98. package/dist/editor/keyHints.d.ts +21 -0
  99. package/dist/editor/keyHints.js +30 -0
  100. package/dist/editor/listState.svelte.d.ts +4 -4
  101. package/dist/editor/staleGuard.d.ts +111 -0
  102. package/dist/editor/staleGuard.js +279 -0
  103. package/dist/i18n/language.d.ts +3 -3
  104. package/dist/i18n/messages.d.ts +84 -1
  105. package/dist/i18n/messages.js +97 -4
  106. package/dist/index.d.ts +1 -0
  107. package/dist/types/index.d.ts +19 -2
  108. package/package.json +10 -10
@@ -1,8 +1,22 @@
1
+ /**
2
+ * Reads the `mode` of a `workflow_saved` event. A mode this version does not know is read as the open workflow having been
3
+ * filled: that is what an older screen did with every mode but `created_new`, and it shows the person the workflow they have
4
+ * open, which is never wrong.
5
+ */
6
+ export function saveModeFromWire(mode) {
7
+ if (mode === 'created_new' || mode === 'updated_open')
8
+ return mode;
9
+ return 'filled_empty';
10
+ }
1
11
  /** Catalog message key for the completion line after approve. */
2
12
  export function completionMessageKeyForSaveMode(mode) {
3
- return mode === 'created_new' ? 'workflows.assistant.createdNew' : 'workflows.assistant.ready';
13
+ if (mode === 'created_new')
14
+ return 'workflows.assistant.createdNew';
15
+ if (mode === 'updated_open')
16
+ return 'workflows.assistant.updatedOpen';
17
+ return 'workflows.assistant.ready';
4
18
  }
5
- /** Whether approving should reload the open workflow or navigate to a new one. */
19
+ /** Whether approving should show the open workflow's new draft or navigate to a new one. */
6
20
  export function editorActionForSaveMode(mode) {
7
21
  return mode === 'created_new' ? 'open_new' : 'reload_open';
8
22
  }
@@ -0,0 +1,55 @@
1
+ import type { WorkflowAssistantCompositionReference } from './types.js';
2
+ /**
3
+ * The rules of the assistant's seed: the first message a host (the page that sends people here from an idea, say) leaves waiting in
4
+ * the composer, and the compositions that message names with `@`.
5
+ *
6
+ * Plain TypeScript with no Svelte in it, so the rules can be tested in Node as they are. The panel does what these functions say:
7
+ * what to do with a seed that was handed over (`readAssistantSeed`, `seedForWorkflow`), what the composer holds once the seed has had
8
+ * its say (`composerAfterSeed`), and which compositions go along with a message when it is sent (`referencesNamedIn`).
9
+ *
10
+ * What the assistant accepts is held here, so a seed the assistant would refuse is dropped before a person presses Send and gets
11
+ * an error they cannot do anything about: the references are at most 10, each with a UUID and a name of at most 200 characters.
12
+ */
13
+ /** The most compositions one message may name: what the assistant accepts for a message. */
14
+ export declare const MAX_COMPOSITION_REFERENCES = 10;
15
+ /** The longest name a reference may carry: what the assistant accepts. */
16
+ export declare const MAX_REFERENCE_NAME_LENGTH = 200;
17
+ /** A seed that is fit to use: it has a workflow, a message, and references the assistant will take. */
18
+ export interface ReadyAssistantSeed {
19
+ readonly workflowId: string;
20
+ readonly message: string;
21
+ readonly compositionReferences: readonly WorkflowAssistantCompositionReference[];
22
+ }
23
+ /**
24
+ * The seed as the panel uses it, or `null` when what the host handed over is not one: not an object, no workflow, or no message to
25
+ * put in the composer. The message is kept as it is (the host wrote it), and only the references that the assistant would refuse
26
+ * are left out of it.
27
+ */
28
+ export declare function readAssistantSeed(candidate: unknown): ReadyAssistantSeed | null;
29
+ /**
30
+ * The seed for the workflow that is open: the host's seed when it is for that workflow, and nothing otherwise. A seed always names
31
+ * a workflow (a blank one is not a seed), so when no workflow is open (`null`) nothing matches.
32
+ */
33
+ export declare function seedForWorkflow(candidate: unknown, openWorkflowId: string | null): ReadyAssistantSeed | null;
34
+ /** What the panel has in front of the person when the seed arrives. */
35
+ export interface ComposerFacts {
36
+ /** What is in the composer. */
37
+ readonly draft: string;
38
+ /** How many messages the workflow's conversation with the assistant already has. */
39
+ readonly messageCount: number;
40
+ }
41
+ /**
42
+ * What the composer holds once the seed has had its say, and the compositions that wait to go along with it. The seed is written only
43
+ * to an empty composer of a workflow that has no conversation yet: a person who is already typing, or already talking to the
44
+ * assistant, keeps what they have, and the seed is then used up without being written (`applied: false`).
45
+ */
46
+ export declare function composerAfterSeed(facts: ComposerFacts, seed: ReadyAssistantSeed): {
47
+ readonly applied: boolean;
48
+ readonly draft: string;
49
+ readonly compositionReferences: readonly WorkflowAssistantCompositionReference[];
50
+ };
51
+ /**
52
+ * The compositions that go along with `text` when it is sent: those whose `@Name` is still in it, in the order they were given. A
53
+ * person who takes a mention out of the message stops pointing at that composition; one who changes the rest of the sentence does not.
54
+ */
55
+ export declare function referencesNamedIn(text: string, references: readonly WorkflowAssistantCompositionReference[]): WorkflowAssistantCompositionReference[];
@@ -0,0 +1,90 @@
1
+ /**
2
+ * The rules of the assistant's seed: the first message a host (the page that sends people here from an idea, say) leaves waiting in
3
+ * the composer, and the compositions that message names with `@`.
4
+ *
5
+ * Plain TypeScript with no Svelte in it, so the rules can be tested in Node as they are. The panel does what these functions say:
6
+ * what to do with a seed that was handed over (`readAssistantSeed`, `seedForWorkflow`), what the composer holds once the seed has had
7
+ * its say (`composerAfterSeed`), and which compositions go along with a message when it is sent (`referencesNamedIn`).
8
+ *
9
+ * What the assistant accepts is held here, so a seed the assistant would refuse is dropped before a person presses Send and gets
10
+ * an error they cannot do anything about: the references are at most 10, each with a UUID and a name of at most 200 characters.
11
+ */
12
+ /** The most compositions one message may name: what the assistant accepts for a message. */
13
+ export const MAX_COMPOSITION_REFERENCES = 10;
14
+ /** The longest name a reference may carry: what the assistant accepts. */
15
+ export const MAX_REFERENCE_NAME_LENGTH = 200;
16
+ /** What a name ends with when the host had to cut it short to fit `MAX_REFERENCE_NAME_LENGTH`: only its beginning is in the text. */
17
+ const CLIPPED_NAME_MARK = '…';
18
+ const UUID_PATTERN = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
19
+ /** What a mention of a composition looks like in the text: `@` and the name. A name that was cut short is held to its beginning. */
20
+ function mentionOf(name) {
21
+ const shownName = name.endsWith(CLIPPED_NAME_MARK)
22
+ ? name.slice(0, -CLIPPED_NAME_MARK.length)
23
+ : name;
24
+ return `@${shownName}`;
25
+ }
26
+ /** The references of a seed that the assistant will take, in the order they came: the others are left out, and so is any beyond the tenth. */
27
+ function readReferences(candidate) {
28
+ if (!Array.isArray(candidate))
29
+ return [];
30
+ const seenIds = new Set();
31
+ const references = [];
32
+ for (const entry of candidate) {
33
+ if (typeof entry !== 'object' || entry === null)
34
+ continue;
35
+ const { id, name } = entry;
36
+ if (typeof id !== 'string' || !UUID_PATTERN.test(id) || seenIds.has(id))
37
+ continue;
38
+ if (typeof name !== 'string' || name.length > MAX_REFERENCE_NAME_LENGTH)
39
+ continue;
40
+ // A name with nothing in it (or only the mark of a cut) would be found in every text that has an `@` at all.
41
+ if (mentionOf(name).trim() === '@')
42
+ continue;
43
+ seenIds.add(id);
44
+ references.push({ id, name });
45
+ if (references.length === MAX_COMPOSITION_REFERENCES)
46
+ break;
47
+ }
48
+ return references;
49
+ }
50
+ /**
51
+ * The seed as the panel uses it, or `null` when what the host handed over is not one: not an object, no workflow, or no message to
52
+ * put in the composer. The message is kept as it is (the host wrote it), and only the references that the assistant would refuse
53
+ * are left out of it.
54
+ */
55
+ export function readAssistantSeed(candidate) {
56
+ if (typeof candidate !== 'object' || candidate === null)
57
+ return null;
58
+ const { workflowId, message, compositionReferences } = candidate;
59
+ if (typeof workflowId !== 'string' || workflowId.trim().length === 0)
60
+ return null;
61
+ if (typeof message !== 'string' || message.trim().length === 0)
62
+ return null;
63
+ return { workflowId, message, compositionReferences: readReferences(compositionReferences) };
64
+ }
65
+ /**
66
+ * The seed for the workflow that is open: the host's seed when it is for that workflow, and nothing otherwise. A seed always names
67
+ * a workflow (a blank one is not a seed), so when no workflow is open (`null`) nothing matches.
68
+ */
69
+ export function seedForWorkflow(candidate, openWorkflowId) {
70
+ const seed = readAssistantSeed(candidate);
71
+ return seed && seed.workflowId === openWorkflowId ? seed : null;
72
+ }
73
+ /**
74
+ * What the composer holds once the seed has had its say, and the compositions that wait to go along with it. The seed is written only
75
+ * to an empty composer of a workflow that has no conversation yet: a person who is already typing, or already talking to the
76
+ * assistant, keeps what they have, and the seed is then used up without being written (`applied: false`).
77
+ */
78
+ export function composerAfterSeed(facts, seed) {
79
+ if (facts.messageCount > 0 || facts.draft.trim().length > 0) {
80
+ return { applied: false, draft: facts.draft, compositionReferences: [] };
81
+ }
82
+ return { applied: true, draft: seed.message, compositionReferences: seed.compositionReferences };
83
+ }
84
+ /**
85
+ * The compositions that go along with `text` when it is sent: those whose `@Name` is still in it, in the order they were given. A
86
+ * person who takes a mention out of the message stops pointing at that composition; one who changes the rest of the sentence does not.
87
+ */
88
+ export function referencesNamedIn(text, references) {
89
+ return references.filter((reference) => text.includes(mentionOf(reference.name)));
90
+ }
@@ -1,5 +1,23 @@
1
1
  import { WORKFLOWS_MESSAGES } from '../i18n/messages.js';
2
+ import { isPlanBindingStatus, parseDataPreview, parsePlanBindings } from './liveData.js';
2
3
  import { mapWorkflowPlanWireToCard } from './workflowPlanViewModel.js';
4
+ import { saveModeFromWire } from './assistantSaveMode.js';
5
+ import { isCanvasId } from './canvasLink.js';
6
+ import { parseTemplateDestinations } from './templateDestinations.js';
7
+ /**
8
+ * The assistant saved a change to the draft a moment ago (on the chat stream while it edits, on the approve stream while the
9
+ * widgets of a plan go into the open workflow as they are built): the editor is told so it can show it on the spot. An event that
10
+ * does not name a workflow is of no use to it, so there is no action for it.
11
+ */
12
+ function workflowUpdatedAction(payload) {
13
+ if (typeof payload.workflowId !== 'string' || payload.workflowId === '')
14
+ return null;
15
+ return {
16
+ type: 'workflow_updated',
17
+ workflowId: payload.workflowId,
18
+ revision: typeof payload.revision === 'number' && Number.isFinite(payload.revision) ? payload.revision : null,
19
+ };
20
+ }
3
21
  export async function* parseAssistantSseStream(response) {
4
22
  const reader = response.body?.getReader();
5
23
  if (!reader)
@@ -50,6 +68,17 @@ function* mapStreamPayloadToAction(payload) {
50
68
  }
51
69
  case 'plan_card': {
52
70
  const raw = payload.plan;
71
+ // Live data in the plan is read strictly, only to be sure the card can say all of it: the plan that goes back on approval is
72
+ // the one that came, untouched. A plan the person could not read in full is not offered for approval.
73
+ if (raw.dataBindings !== undefined && parsePlanBindings(raw.dataBindings) === null) {
74
+ console.debug('[workflows-assistant] a plan with live data that could not be read was left out');
75
+ yield {
76
+ type: 'assistant_text',
77
+ id: crypto.randomUUID(),
78
+ text: WORKFLOWS_MESSAGES['workflows.error.generic'],
79
+ };
80
+ break;
81
+ }
53
82
  const plan = {
54
83
  ...raw,
55
84
  planToken: raw.planToken ?? '',
@@ -57,7 +86,25 @@ function* mapStreamPayloadToAction(payload) {
57
86
  attachments: raw.attachments ?? [],
58
87
  estimate: raw.estimate ?? { credits: 0, widgetsToCreate: 0 },
59
88
  };
60
- yield { type: 'plan_card', card: mapWorkflowPlanWireToCard(plan) };
89
+ // Where the copies of the plan's template widgets can go: sent beside the plan, and only when there is a choice to make.
90
+ yield {
91
+ type: 'plan_card',
92
+ card: mapWorkflowPlanWireToCard(plan, parseTemplateDestinations(payload.templateDestinations)),
93
+ };
94
+ break;
95
+ }
96
+ case 'data_preview': {
97
+ // What a call of one of the person's Open MCPs would show, for their eyes only. One that cannot be read is left out: it is a
98
+ // view for checking, and the plan card carries the call itself.
99
+ const preview = parseDataPreview(payload.preview);
100
+ if (preview)
101
+ yield { type: 'data_preview', preview };
102
+ break;
103
+ }
104
+ case 'workflow_updated': {
105
+ const updated = workflowUpdatedAction(payload);
106
+ if (updated)
107
+ yield updated;
61
108
  break;
62
109
  }
63
110
  case 'refusal':
@@ -72,6 +119,7 @@ function* mapStreamPayloadToAction(payload) {
72
119
  type: 'assistant_text',
73
120
  id: crypto.randomUUID(),
74
121
  text: String(payload.message ?? WORKFLOWS_MESSAGES['workflows.error.generic']),
122
+ failed: true,
75
123
  };
76
124
  break;
77
125
  case 'done':
@@ -108,6 +156,26 @@ export async function* parseAssistantApproveStream(response) {
108
156
  detail: typeof payload.detail === 'string' && payload.detail.length > 0 ? payload.detail : undefined,
109
157
  };
110
158
  }
159
+ else if (payload.type === 'widget_started') {
160
+ // A widget of the plan began to build, and this is the canvas it is built on: the card links to it, so the person can
161
+ // open it and watch. One that names no step or no usable canvas has nothing to link to, and is skipped.
162
+ if (typeof payload.stepId === 'string' && payload.stepId !== '' && isCanvasId(payload.canvasId)) {
163
+ yield { type: 'plan_step_canvas', stepId: payload.stepId, canvasId: payload.canvasId };
164
+ }
165
+ }
166
+ else if (payload.type === 'binding_status') {
167
+ // Live data, one event per binding as it moves. A status this screen does not know is not shown as progress.
168
+ if (typeof payload.bindingId === 'string' && isPlanBindingStatus(payload.status)) {
169
+ yield {
170
+ type: 'binding_status',
171
+ bindingId: payload.bindingId,
172
+ status: payload.status,
173
+ // The gateway's sentence for a binding that failed, and how many rows the call gave: never a value of the answer.
174
+ detail: typeof payload.detail === 'string' && payload.detail.length > 0 ? payload.detail : undefined,
175
+ rows: typeof payload.rows === 'number' ? payload.rows : undefined,
176
+ };
177
+ }
178
+ }
111
179
  else if (payload.type === 'error') {
112
180
  // Credits short, a plan used twice, a build that failed: the person is told in the chat, not left looking at a card
113
181
  // that does nothing.
@@ -115,11 +183,21 @@ export async function* parseAssistantApproveStream(response) {
115
183
  type: 'assistant_text',
116
184
  id: crypto.randomUUID(),
117
185
  text: String(payload.message ?? WORKFLOWS_MESSAGES['workflows.error.generic']),
186
+ failed: true,
118
187
  };
119
188
  }
189
+ else if (payload.type === 'workflow_updated') {
190
+ // A widget of the plan went into the open workflow while the others are still being built: the editor shows it now.
191
+ const updated = workflowUpdatedAction(payload);
192
+ if (updated)
193
+ yield updated;
194
+ }
120
195
  else if (payload.type === 'workflow_saved') {
121
- const mode = payload.mode === 'created_new' ? 'created_new' : 'filled_empty';
122
- yield { type: 'workflow_ready', workflowId: String(payload.workflowId), mode };
196
+ yield {
197
+ type: 'workflow_ready',
198
+ workflowId: String(payload.workflowId),
199
+ mode: saveModeFromWire(payload.mode),
200
+ };
123
201
  }
124
202
  else if (payload.type === 'done') {
125
203
  yield { type: 'done' };
@@ -0,0 +1,18 @@
1
+ /**
2
+ * What the workflows assistant's box tells the gateway about itself when it asks how to go on: how many steps the open workflow holds,
3
+ * so an empty canvas is offered ways to begin and a full one ways to go on. A number, and nothing else of the workflow: no name, no
4
+ * widget, no text of it ever leaves the screen with a suggestion request.
5
+ *
6
+ * Plain TypeScript with no Svelte, so a test in Node can run it.
7
+ */
8
+ /** The surface of the box, as the gateway names it in the counts of what happened to its suggestions. */
9
+ export declare const WORKFLOW_ASSISTANT_SUGGEST_SURFACE = "workflow_assistant";
10
+ /** The most steps the gateway reads (a bigger number would make it refuse the whole request). */
11
+ export declare const WORKFLOW_SUGGEST_MAX_STEP_COUNT = 200;
12
+ /**
13
+ * The context of a request: the steps the workflow holds, a whole number from 0 (an empty canvas is something to say) up to what the
14
+ * gateway reads. `undefined` when the screen has no number to give: a box with nothing to say about itself sends no context.
15
+ */
16
+ export declare function workflowSuggestContext(stepCount: number | null | undefined): {
17
+ stepCount: number;
18
+ } | undefined;
@@ -0,0 +1,21 @@
1
+ /**
2
+ * What the workflows assistant's box tells the gateway about itself when it asks how to go on: how many steps the open workflow holds,
3
+ * so an empty canvas is offered ways to begin and a full one ways to go on. A number, and nothing else of the workflow: no name, no
4
+ * widget, no text of it ever leaves the screen with a suggestion request.
5
+ *
6
+ * Plain TypeScript with no Svelte, so a test in Node can run it.
7
+ */
8
+ /** The surface of the box, as the gateway names it in the counts of what happened to its suggestions. */
9
+ export const WORKFLOW_ASSISTANT_SUGGEST_SURFACE = 'workflow_assistant';
10
+ /** The most steps the gateway reads (a bigger number would make it refuse the whole request). */
11
+ export const WORKFLOW_SUGGEST_MAX_STEP_COUNT = 200;
12
+ /**
13
+ * The context of a request: the steps the workflow holds, a whole number from 0 (an empty canvas is something to say) up to what the
14
+ * gateway reads. `undefined` when the screen has no number to give: a box with nothing to say about itself sends no context.
15
+ */
16
+ export function workflowSuggestContext(stepCount) {
17
+ if (typeof stepCount !== 'number' || !Number.isFinite(stepCount))
18
+ return undefined;
19
+ const wholeSteps = Math.floor(stepCount);
20
+ return { stepCount: Math.min(WORKFLOW_SUGGEST_MAX_STEP_COUNT, Math.max(0, wholeSteps)) };
21
+ }
@@ -0,0 +1,75 @@
1
+ import type { AssistantTranscriptMessage } from './types.js';
2
+ /**
3
+ * The rules of the assistant's face in the panel: how the transcript is cut into turns, and what the assistant is doing at
4
+ * this moment, as far as its avatar cares.
5
+ *
6
+ * Plain TypeScript with no Svelte in it, so the rules can be tested in Node as they are. The panel turns a moment into the
7
+ * look of the avatar with the chat's own `liveAgentLook` and `settledAgentLook` (`@widgetic/chat`), so the panel follows the
8
+ * same choreography as the chat of the creator instead of keeping a copy of it.
9
+ */
10
+ /** One row of the transcript as the panel draws it. */
11
+ export type AssistantTurn = {
12
+ readonly kind: 'user';
13
+ /** Stable while messages are added (the position in the transcript), so a row is never drawn again for nothing. */
14
+ readonly key: string;
15
+ readonly message: AssistantTranscriptMessage;
16
+ } | {
17
+ readonly kind: 'assistant';
18
+ readonly key: string;
19
+ /** What the assistant said in a row, one after the other. Empty while it has only its live content to show. */
20
+ readonly messages: readonly AssistantTranscriptMessage[];
21
+ /**
22
+ * True for the last turn when it is the assistant's: the only one whose avatar moves, and the one that holds what is
23
+ * still live (the working line, the cards, the conclusion). Older turns are still pictures.
24
+ */
25
+ readonly isNewest: boolean;
26
+ };
27
+ /**
28
+ * Cuts the transcript into turns: a message of the person is a turn of its own, and the messages the assistant said in a row
29
+ * are one turn (one face for what it said, not one for each sentence).
30
+ *
31
+ * `hasLiveContent` says that something belonging to the assistant is on screen after the transcript (it is working, a
32
+ * question or a plan is waiting for the person, there is a conclusion or a failure). Then the last turn is the assistant's
33
+ * even when the person spoke last: it starts with that content, so the face sits beside it.
34
+ */
35
+ export declare function groupAssistantTurns(messages: readonly AssistantTranscriptMessage[], hasLiveContent: boolean): AssistantTurn[];
36
+ /** What the assistant is doing, as far as its avatar cares. */
37
+ export type AssistantMoment = {
38
+ readonly kind: 'live';
39
+ /**
40
+ * `shimmer-only`: waiting for the first word (the avatar is large and thinks). `code-only`: it is answering or building
41
+ * (the avatar is small and writes). The names are the chat's `GenerationActivityPhase`.
42
+ */
43
+ readonly phase: 'shimmer-only' | 'code-only';
44
+ } | {
45
+ readonly kind: 'settled';
46
+ /** The names are the chat's `TurnOutcome`. */
47
+ readonly outcome: 'success' | 'error' | 'question' | 'answer';
48
+ };
49
+ /**
50
+ * Whether the avatar of a moment stays small even where the chat would make it large. The panel is narrow (a third of a
51
+ * screen at most), and the chat's large avatar beside a question or a plan waiting for an answer would leave the card two
52
+ * thirds of the panel, then widen it again the moment the person answers. So a card waiting keeps the small avatar; the
53
+ * other large moments (waiting for the first word, the conclusion) have only words beside them and keep the chat's size.
54
+ */
55
+ export declare function momentKeepsAvatarSmall(moment: AssistantMoment): boolean;
56
+ /** What the panel knows about the run, as plain facts. */
57
+ export interface AssistantRunFacts {
58
+ /** A stream is open (the assistant is answering, or the approved plan is being built). */
59
+ readonly isStreaming: boolean;
60
+ /** The assistant has already said something since the person last spoke. */
61
+ readonly hasReplyStarted: boolean;
62
+ /** Work is under way that shows no words of its own: the widgets of an approved plan are being built. */
63
+ readonly isBuilding: boolean;
64
+ /** A question or a plan is on screen, waiting for the person. */
65
+ readonly hasCardToAnswer: boolean;
66
+ /** The last run failed (the stream, the approval or a step of the plan). */
67
+ readonly hasFailed: boolean;
68
+ /** The last run ended with what the person asked for (the workflow is ready). */
69
+ readonly hasSucceeded: boolean;
70
+ }
71
+ /**
72
+ * The moment of the assistant. A failure is looked at first (a plan with a failed step still shows its card, and the card is
73
+ * not what the person needs to see first), then the conclusion, then a card waiting for the person, then a plain answer.
74
+ */
75
+ export declare function assistantMoment(facts: AssistantRunFacts): AssistantMoment;
@@ -0,0 +1,56 @@
1
+ /**
2
+ * Cuts the transcript into turns: a message of the person is a turn of its own, and the messages the assistant said in a row
3
+ * are one turn (one face for what it said, not one for each sentence).
4
+ *
5
+ * `hasLiveContent` says that something belonging to the assistant is on screen after the transcript (it is working, a
6
+ * question or a plan is waiting for the person, there is a conclusion or a failure). Then the last turn is the assistant's
7
+ * even when the person spoke last: it starts with that content, so the face sits beside it.
8
+ */
9
+ export function groupAssistantTurns(messages, hasLiveContent) {
10
+ const turns = [];
11
+ for (const message of messages) {
12
+ const previousTurn = turns[turns.length - 1];
13
+ if (message.role === 'assistant' && previousTurn?.kind === 'assistant') {
14
+ turns[turns.length - 1] = { ...previousTurn, messages: [...previousTurn.messages, message] };
15
+ continue;
16
+ }
17
+ const key = `turn-${turns.length}`;
18
+ turns.push(message.role === 'user'
19
+ ? { kind: 'user', key, message }
20
+ : { kind: 'assistant', key, messages: [message], isNewest: false });
21
+ }
22
+ const lastTurn = turns[turns.length - 1];
23
+ if (hasLiveContent && (!lastTurn || lastTurn.kind === 'user')) {
24
+ turns.push({ kind: 'assistant', key: `turn-${turns.length}`, messages: [], isNewest: false });
25
+ }
26
+ const newestTurn = turns[turns.length - 1];
27
+ if (newestTurn?.kind === 'assistant') {
28
+ turns[turns.length - 1] = { ...newestTurn, isNewest: true };
29
+ }
30
+ return turns;
31
+ }
32
+ /**
33
+ * Whether the avatar of a moment stays small even where the chat would make it large. The panel is narrow (a third of a
34
+ * screen at most), and the chat's large avatar beside a question or a plan waiting for an answer would leave the card two
35
+ * thirds of the panel, then widen it again the moment the person answers. So a card waiting keeps the small avatar; the
36
+ * other large moments (waiting for the first word, the conclusion) have only words beside them and keep the chat's size.
37
+ */
38
+ export function momentKeepsAvatarSmall(moment) {
39
+ return moment.kind === 'settled' && moment.outcome === 'question';
40
+ }
41
+ /**
42
+ * The moment of the assistant. A failure is looked at first (a plan with a failed step still shows its card, and the card is
43
+ * not what the person needs to see first), then the conclusion, then a card waiting for the person, then a plain answer.
44
+ */
45
+ export function assistantMoment(facts) {
46
+ if (facts.isStreaming || facts.isBuilding) {
47
+ return { kind: 'live', phase: facts.isBuilding || facts.hasReplyStarted ? 'code-only' : 'shimmer-only' };
48
+ }
49
+ if (facts.hasFailed)
50
+ return { kind: 'settled', outcome: 'error' };
51
+ if (facts.hasSucceeded)
52
+ return { kind: 'settled', outcome: 'success' };
53
+ if (facts.hasCardToAnswer)
54
+ return { kind: 'settled', outcome: 'question' };
55
+ return { kind: 'settled', outcome: 'answer' };
56
+ }
@@ -0,0 +1,42 @@
1
+ /**
2
+ * The files the person attaches to a message to the assistant, from the moment they pick them to the moment the library
3
+ * has them. Every file is a chip above the composer; while it goes up, the chip's own background shows how far it got
4
+ * (the design's Chip with a Progress value), and there is no separate bar.
5
+ *
6
+ * Each function takes the list and returns a new one, so the panel keeps it in one piece of state and the rules can be
7
+ * tested without a screen.
8
+ */
9
+ import type { AssistantAttachmentChip } from './types.js';
10
+ /** A share uploaded as the chip can draw it: 0 to 100, and 0 for anything that is not a number. */
11
+ export declare function clampUploadPercentage(percentage: number): number;
12
+ /** A file just picked: it starts at 0%, and its media id is not known until the library has the file. */
13
+ export declare function startAttachment(attachments: readonly AssistantAttachmentChip[], id: string, fileName: string): AssistantAttachmentChip[];
14
+ /** The host reported how far a file got. Only a file that is still going up takes it (a late report after the end is ignored). */
15
+ export declare function setAttachmentProgress(attachments: readonly AssistantAttachmentChip[], id: string, percentage: number): AssistantAttachmentChip[];
16
+ /** The library has the file: the chip is ready to be sent with the message and has nothing left to show. */
17
+ export declare function finishAttachment(attachments: readonly AssistantAttachmentChip[], id: string, mediaId: string): AssistantAttachmentChip[];
18
+ /** The upload failed: the chip stays, marked, so the person sees which file did not go up. */
19
+ export declare function failAttachment(attachments: readonly AssistantAttachmentChip[], id: string): AssistantAttachmentChip[];
20
+ /** The person removed the chip (of a file that is still going up, that also stops the upload: the panel aborts it). */
21
+ export declare function removeAttachment(attachments: readonly AssistantAttachmentChip[], id: string): AssistantAttachmentChip[];
22
+ /**
23
+ * The uploads that are still going up, by attachment id, so that removing a chip stops its own upload and no other.
24
+ * (Kept here and not in the panel: it is a plain bookkeeping object, not state the screen draws.)
25
+ */
26
+ export declare function createUploadRegistry(): {
27
+ /** A file starts going up: the controller whose signal goes to the host. */
28
+ start(id: string): AbortController;
29
+ /** The person removed the chip: its upload (if it is still going) is aborted. */
30
+ abort(id: string): void;
31
+ /** The upload ended, one way or the other. */
32
+ finish(id: string): void;
33
+ /** Whether the file is still going up. */
34
+ isUploading(id: string): boolean;
35
+ };
36
+ /** The files the message may refer to: only the ones the library has. */
37
+ export declare function readyAttachmentMediaIds(attachments: readonly AssistantAttachmentChip[]): string[];
38
+ /**
39
+ * The share the chip draws as its background: only while the file goes up. A chip that is ready or has failed has no
40
+ * progress layer at all, so it looks like every other chip.
41
+ */
42
+ export declare function attachmentChipProgress(attachment: AssistantAttachmentChip): number | undefined;
@@ -0,0 +1,69 @@
1
+ /** A share uploaded as the chip can draw it: 0 to 100, and 0 for anything that is not a number. */
2
+ export function clampUploadPercentage(percentage) {
3
+ if (Number.isNaN(percentage))
4
+ return 0;
5
+ return Math.min(100, Math.max(0, percentage));
6
+ }
7
+ /** A file just picked: it starts at 0%, and its media id is not known until the library has the file. */
8
+ export function startAttachment(attachments, id, fileName) {
9
+ return [...attachments, { id, mediaId: '', fileName, state: 'uploading', progress: 0 }];
10
+ }
11
+ /** The host reported how far a file got. Only a file that is still going up takes it (a late report after the end is ignored). */
12
+ export function setAttachmentProgress(attachments, id, percentage) {
13
+ return attachments.map((entry) => entry.id === id && entry.state === 'uploading'
14
+ ? { ...entry, progress: clampUploadPercentage(percentage) }
15
+ : entry);
16
+ }
17
+ /** The library has the file: the chip is ready to be sent with the message and has nothing left to show. */
18
+ export function finishAttachment(attachments, id, mediaId) {
19
+ return attachments.map((entry) => entry.id === id && entry.state === 'uploading'
20
+ ? { ...entry, mediaId, state: 'ready', progress: 100 }
21
+ : entry);
22
+ }
23
+ /** The upload failed: the chip stays, marked, so the person sees which file did not go up. */
24
+ export function failAttachment(attachments, id) {
25
+ return attachments.map((entry) => entry.id === id && entry.state === 'uploading' ? { ...entry, state: 'error' } : entry);
26
+ }
27
+ /** The person removed the chip (of a file that is still going up, that also stops the upload: the panel aborts it). */
28
+ export function removeAttachment(attachments, id) {
29
+ return attachments.filter((entry) => entry.id !== id);
30
+ }
31
+ /**
32
+ * The uploads that are still going up, by attachment id, so that removing a chip stops its own upload and no other.
33
+ * (Kept here and not in the panel: it is a plain bookkeeping object, not state the screen draws.)
34
+ */
35
+ export function createUploadRegistry() {
36
+ const controllers = new Map();
37
+ return {
38
+ /** A file starts going up: the controller whose signal goes to the host. */
39
+ start(id) {
40
+ const controller = new AbortController();
41
+ controllers.set(id, controller);
42
+ return controller;
43
+ },
44
+ /** The person removed the chip: its upload (if it is still going) is aborted. */
45
+ abort(id) {
46
+ controllers.get(id)?.abort();
47
+ controllers.delete(id);
48
+ },
49
+ /** The upload ended, one way or the other. */
50
+ finish(id) {
51
+ controllers.delete(id);
52
+ },
53
+ /** Whether the file is still going up. */
54
+ isUploading(id) {
55
+ return controllers.has(id);
56
+ },
57
+ };
58
+ }
59
+ /** The files the message may refer to: only the ones the library has. */
60
+ export function readyAttachmentMediaIds(attachments) {
61
+ return attachments.filter((entry) => entry.state === 'ready').map((entry) => entry.mediaId);
62
+ }
63
+ /**
64
+ * The share the chip draws as its background: only while the file goes up. A chip that is ready or has failed has no
65
+ * progress layer at all, so it looks like every other chip.
66
+ */
67
+ export function attachmentChipProgress(attachment) {
68
+ return attachment.state === 'uploading' ? attachment.progress : undefined;
69
+ }
@@ -0,0 +1,30 @@
1
+ import type { WorkflowsMessageKey } from '../i18n/messages.js';
2
+ export declare function isCanvasId(value: unknown): value is string;
3
+ /** Where the Widgetic site opens a canvas. A page that is not the site gives its own address (`WorkflowAssistantConfig.canvasHref`). */
4
+ export declare function defaultCanvasHref(canvasId: string): string;
5
+ /** What the link needs of a step of the card: where its build is, and the canvas it is built on once it has started. */
6
+ interface StepWithCanvas {
7
+ readonly canvasId?: string;
8
+ readonly status: 'waiting' | 'creating' | 'done' | 'failed';
9
+ }
10
+ export interface StepCanvasLink {
11
+ readonly href: string;
12
+ /** The words of the link: it says "watch" while the build runs and "open" once it is done. */
13
+ readonly labelKey: Extract<WorkflowsMessageKey, 'workflows.assistant.watchBuild' | 'workflows.assistant.openBuilt'>;
14
+ }
15
+ /**
16
+ * The link of a step, or null when it has none: no canvas yet (the build has not started, or the widget is one the person already
17
+ * had), a build that failed (its canvas has nothing to watch), or an address that cannot be used. Never throws: a page's address builder
18
+ * that does is a missing link, not a broken card.
19
+ *
20
+ * @param step the step as the card holds it
21
+ * @param buildHref the page's own address builder, when it gave one
22
+ */
23
+ export declare function canvasLinkOfStep(step: StepWithCanvas, buildHref?: ((canvasId: string) => string) | undefined): StepCanvasLink | null;
24
+ /**
25
+ * True while a step of the card is being built and has a canvas to open: the card then says what its links are for (the build can
26
+ * be watched, and taken over, in the tab they open). Once every build is done the links only open the finished widgets, and the
27
+ * sentence goes.
28
+ */
29
+ export declare function hasBuildToWatch(steps: ReadonlyArray<StepWithCanvas>, buildHref?: ((canvasId: string) => string) | undefined): boolean;
30
+ export {};