@gooddata/sdk-ui-gen-ai 11.67.0-alpha.2 → 11.67.0-alpha.3

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 (60) hide show
  1. package/NOTICE +943 -2401
  2. package/README.md +44 -1
  3. package/esm/components/GenAIChatDialogConnected.d.ts +5 -8
  4. package/esm/components/GenAIChatDialogConnected.d.ts.map +1 -1
  5. package/esm/components/GenAIChatDialogConnected.js +4 -5
  6. package/esm/components/GenAIChatWrapper.js +1 -1
  7. package/esm/components/hooks/useAppEventSync.d.ts +18 -0
  8. package/esm/components/hooks/useAppEventSync.d.ts.map +1 -0
  9. package/esm/components/hooks/useAppEventSync.js +35 -0
  10. package/esm/components/hooks/useSettingsClick.d.ts +1 -2
  11. package/esm/components/hooks/useSettingsClick.d.ts.map +1 -1
  12. package/esm/components/hooks/useSettingsClick.js +7 -20
  13. package/esm/components/messages/conversationContents/ConversationErrorContent.d.ts.map +1 -1
  14. package/esm/components/messages/conversationContents/ConversationErrorContent.js +1 -4
  15. package/esm/copilot/CopilotComposingOverlay.d.ts +4 -0
  16. package/esm/copilot/CopilotComposingOverlay.d.ts.map +1 -0
  17. package/esm/copilot/CopilotComposingOverlay.js +151 -0
  18. package/esm/copilot/CopilotInteractionComposing.d.ts +29 -0
  19. package/esm/copilot/CopilotInteractionComposing.d.ts.map +1 -0
  20. package/esm/copilot/CopilotInteractionComposing.js +39 -0
  21. package/esm/copilot/CopilotInteractionComposingWithHandler.d.ts +15 -0
  22. package/esm/copilot/CopilotInteractionComposingWithHandler.d.ts.map +1 -0
  23. package/esm/copilot/CopilotInteractionComposingWithHandler.js +11 -0
  24. package/esm/copilot/selectors.d.ts +4 -2
  25. package/esm/copilot/selectors.d.ts.map +1 -1
  26. package/esm/copilot/selectors.js +73 -14
  27. package/esm/copilot/transitions.d.ts.map +1 -1
  28. package/esm/copilot/transitions.js +264 -81
  29. package/esm/copilot/types.d.ts +10 -4
  30. package/esm/copilot/types.d.ts.map +1 -1
  31. package/esm/index.d.ts +2 -0
  32. package/esm/index.d.ts.map +1 -1
  33. package/esm/index.js +2 -0
  34. package/esm/internal.d.ts +1 -2
  35. package/esm/internal.d.ts.map +1 -1
  36. package/esm/localization/bundles/en-US.localization-bundle.d.ts +8 -0
  37. package/esm/localization/bundles/en-US.localization-bundle.d.ts.map +1 -1
  38. package/esm/localization/bundles/en-US.localization-bundle.js +8 -0
  39. package/esm/sdk-ui-gen-ai.d.ts +58 -51
  40. package/esm/store/events.d.ts +6 -45
  41. package/esm/store/events.d.ts.map +1 -1
  42. package/esm/store/sideEffects/onEvent.d.ts +11 -3
  43. package/esm/store/sideEffects/onEvent.d.ts.map +1 -1
  44. package/esm/store/sideEffects/onEvent.js +32 -25
  45. package/esm/utils.d.ts +0 -4
  46. package/esm/utils.d.ts.map +1 -1
  47. package/esm/utils.js +1 -21
  48. package/package.json +25 -26
  49. package/styles/css/copilotInteractionComposing.css +94 -0
  50. package/styles/css/copilotInteractionComposing.css.map +1 -0
  51. package/styles/css/main.css +93 -0
  52. package/styles/css/main.css.map +1 -1
  53. package/styles/scss/copilotInteractionComposing.scss +106 -0
  54. package/styles/scss/main.scss +1 -0
  55. package/esm/components/hooks/useDashboardSavedSync.d.ts +0 -13
  56. package/esm/components/hooks/useDashboardSavedSync.d.ts.map +0 -1
  57. package/esm/components/hooks/useDashboardSavedSync.js +0 -14
  58. package/esm/components/hooks/usePublisherDocumentSavedSync.d.ts +0 -20
  59. package/esm/components/hooks/usePublisherDocumentSavedSync.d.ts.map +0 -1
  60. package/esm/components/hooks/usePublisherDocumentSavedSync.js +0 -18
@@ -1,27 +1,70 @@
1
1
  // (C) 2026 GoodData Corporation
2
+ /** An omitted object ID includes every target of the workflow; a supplied ID must match exactly. */
3
+ function matchesTargetScope(target, scope) {
4
+ return target.type === scope.type && (scope.objectId === undefined || target.objectId === scope.objectId);
5
+ }
6
+ /** Edit requests require an editing or builder scope unless they originated from scoped generation. */
7
+ function matchesScope(request, scope) {
8
+ const isProgress = scope.isEditing ||
9
+ scope.isBuilder ||
10
+ request.intent === "create" ||
11
+ request.generation === scope.type;
12
+ const isSameType = (request.type ?? request.generation) === scope.type;
13
+ const isSameObject = scope.objectId === undefined || request.objectId === scope.objectId;
14
+ return !request.superseded && isSameType && isSameObject && Boolean(isProgress);
15
+ }
16
+ /** Before activation, generation identifies creation; after activation, the resolved type and intent do. */
17
+ function isActiveCreationRequest(request, type) {
18
+ return (!request.superseded &&
19
+ (request.type === undefined
20
+ ? request.generation === type
21
+ : request.type === type && request.intent === "create"));
22
+ }
23
+ /** Clarification or a finished response waits for a reply only after all host applications complete. */
24
+ function isWaitingForReply(request) {
25
+ return (request.awaitingClarification || request.responseFinished) && !request.applicationIds.length;
26
+ }
27
+ /** Foreground creation counts before activation too; foreground edits count only in builder scopes. */
28
+ function hasForegroundProgress(state, scope) {
29
+ return (state.generation === scope.type ||
30
+ (state.interaction.build?.type === scope.type &&
31
+ (state.interaction.build.intent === "create" || scope.isBuilder)));
32
+ }
33
+ /** Any non-superseded request of the workflow type claims foreground startup reasoning, regardless of object scope. */
34
+ function isForegroundWorkflowUnclaimed(state, scope) {
35
+ return ((state.generation === scope.type || state.interaction.build?.type === scope.type) &&
36
+ !state.requests.some((request) => !request.superseded && (request.type ?? request.generation) === scope.type));
37
+ }
38
+ /** A scoped request owns reasoning by its truthy thread ID, otherwise by direct conversation identity. */
39
+ function ownsForegroundReasoning(request, state, scope) {
40
+ return (matchesScope(request, scope) &&
41
+ (request.threadId
42
+ ? request.threadId === state.interaction.threadId
43
+ : request.conversationId === state.interaction.conversationId));
44
+ }
2
45
  /** Whether creation is pending, including an activated creation build. @alpha */
3
46
  export function selectCopilotGenerationPending(state, type) {
4
47
  return (state.generation === type ||
5
- (state.interaction.build?.type === type && state.interaction.build.intent === "create"));
48
+ (state.interaction.build?.type === type && state.interaction.build.intent === "create") ||
49
+ state.requests.some((request) => isActiveCreationRequest(request, type)));
6
50
  }
7
51
  /** Calculate progress without host presentation rules or side effects. @alpha */
8
52
  export function selectCopilotStatus(state, scope) {
9
- if (state.applications.some(({ target }) => target.type === scope.type &&
10
- (scope.objectId === undefined || target.objectId === scope.objectId))) {
53
+ if (state.applications.some(({ target }) => matchesTargetScope(target, scope))) {
11
54
  return "building";
12
55
  }
13
- if (scope.isEditing) {
14
- const requests = state.requests.filter((request) => request.type === scope.type &&
15
- (scope.objectId === undefined || request.objectId === scope.objectId));
16
- if (requests.length) {
17
- return requests.every((request) => request.awaitingClarification && !request.applicationIds.length)
18
- ? "waiting"
19
- : "building";
56
+ const requests = state.requests.filter((request) => matchesScope(request, scope));
57
+ if (requests.length) {
58
+ if (requests.every(isWaitingForReply)) {
59
+ return "waiting";
60
+ }
61
+ if (requests.some((request) => request.type !== undefined)) {
62
+ return "building";
20
63
  }
64
+ return "generating";
21
65
  }
22
66
  const interaction = state.interaction;
23
- if (selectCopilotGenerationPending(state, scope.type) ||
24
- (scope.isBuilder && interaction.build?.type === scope.type)) {
67
+ if (hasForegroundProgress(state, scope)) {
25
68
  if (interaction.waitingForUserInput || interaction.pendingClarificationIds.length) {
26
69
  return "waiting";
27
70
  }
@@ -29,11 +72,27 @@ export function selectCopilotStatus(state, scope) {
29
72
  }
30
73
  return "idle";
31
74
  }
75
+ /** Resolve the newest waiting workflow's thread rather than the selected chat. @internal */
76
+ export function selectCopilotReplyThreadId(state, scope) {
77
+ const waiting = state.requests.reduce((selected, request) => matchesScope(request, scope) &&
78
+ isWaitingForReply(request) &&
79
+ (!selected || request.revision > selected.revision)
80
+ ? request
81
+ : selected, undefined);
82
+ return waiting ? (waiting.threadId ?? waiting.conversationId) : state.interaction.threadId;
83
+ }
32
84
  /** Query the originating conversation of an applied object. @alpha */
33
85
  export function selectCopilotSource(state, target) {
34
86
  return state.sources.find((entry) => entry.target.type === target.type && entry.target.objectId === target.objectId)?.source;
35
87
  }
36
- /** Query the current foreground reasoning heading. @alpha */
37
- export function selectCopilotReasoningTitle(state) {
88
+ /** Query foreground reasoning, optionally restricted to the owning workflow. @alpha */
89
+ export function selectCopilotReasoningTitle(state, scope) {
90
+ if (!scope) {
91
+ return state.interaction.reasoningTitle;
92
+ }
93
+ const unclaimed = isForegroundWorkflowUnclaimed(state, scope);
94
+ if (!unclaimed && !state.requests.some((request) => ownsForegroundReasoning(request, state, scope))) {
95
+ return undefined;
96
+ }
38
97
  return state.interaction.reasoningTitle;
39
98
  }
@@ -1 +1 @@
1
- {"version":3,"file":"transitions.d.ts","sourceRoot":"","sources":["../../src/copilot/transitions.ts"],"names":[],"mappings":"AASA,OAAO,EACH,KAAK,gBAAgB,EACrB,KAAK,YAAY,EAEjB,KAAK,qBAAqB,EAK1B,KAAK,gBAAgB,EAExB,MAAM,YAAY,CAAC;AAEpB,0DAA0D;AAC1D,wBAAgB,kBAAkB,CAAC,MAAM,SAAS,MAAM,GAAG,gBAAgB,KAAK,gBAAgB,CAAC,MAAM,CAAC,CASvG;AAshBD,wGAAwG;AACxG,wBAAgB,iBAAiB,CAAC,MAAM,SAAS,MAAM,GAAG,gBAAgB,EACtE,KAAK,EAAE,gBAAgB,CAAC,MAAM,CAAC,EAC/B,KAAK,EAAE,YAAY,CAAC,MAAM,CAAC,EAC3B,aAAa,EAAE,qBAAqB,CAAC,MAAM,CAAC,GAC7C,gBAAgB,CAAC,MAAM,CAAC,CA8B1B"}
1
+ {"version":3,"file":"transitions.d.ts","sourceRoot":"","sources":["../../src/copilot/transitions.ts"],"names":[],"mappings":"AASA,OAAO,EACH,KAAK,gBAAgB,EACrB,KAAK,YAAY,EAEjB,KAAK,qBAAqB,EAK1B,KAAK,gBAAgB,EAExB,MAAM,YAAY,CAAC;AAEpB,0DAA0D;AAC1D,wBAAgB,kBAAkB,CAAC,MAAM,SAAS,MAAM,GAAG,gBAAgB,KAAK,gBAAgB,CAAC,MAAM,CAAC,CASvG;AAg0BD,wGAAwG;AACxG,wBAAgB,iBAAiB,CAAC,MAAM,SAAS,MAAM,GAAG,gBAAgB,EACtE,KAAK,EAAE,gBAAgB,CAAC,MAAM,CAAC,EAC/B,KAAK,EAAE,YAAY,CAAC,MAAM,CAAC,EAC3B,aAAa,EAAE,qBAAqB,CAAC,MAAM,CAAC,GAC7C,gBAAgB,CAAC,MAAM,CAAC,CA0C1B"}
@@ -30,50 +30,77 @@ function sameTarget(a, b) {
30
30
  return a.type === b.type && a.objectId === b.objectId;
31
31
  }
32
32
  function matches(request, threadId, conversationId) {
33
- if (conversationId && request.conversationId === conversationId) {
34
- return true;
33
+ // Local IDs take precedence: a new draft can still report the previous backend thread.
34
+ if (conversationId && request.conversationId) {
35
+ return request.conversationId === conversationId;
35
36
  }
36
37
  return threadId
37
38
  ? request.threadId === threadId || request.conversationId === threadId
38
39
  : !conversationId && !request.threadId && !request.conversationId;
39
40
  }
41
+ /** Both text and clarification answers start a new response revision. */
42
+ function isUserInputEvent(event) {
43
+ return event.type === "chatUserMessage" || event.type === "chatUserAnswers";
44
+ }
40
45
  function conversationId(event) {
41
46
  if (event.type === "chatConversationChanged") {
42
47
  return event.conversation.localId;
43
48
  }
44
- return event.type === "chatUserMessage" || event.type === "chatUserAnswers"
45
- ? event.conversationId
46
- : undefined;
49
+ return isUserInputEvent(event) ? event.conversationId : undefined;
47
50
  }
48
51
  function failedResponse(event) {
49
- return !!(event.cancelled || event.useCase === "INVALID");
52
+ return Boolean(event.cancelled || event.useCase === "INVALID");
53
+ }
54
+ /** A background creation can supply intent when another conversation activates the same target. */
55
+ function hasOngoingCreation(state, type) {
56
+ return (!state.context.isEditing &&
57
+ state.requests.some((request) => !request.superseded &&
58
+ (request.type ?? request.generation) === type &&
59
+ (request.intent === "create" || request.generation === type) &&
60
+ (request.objectId === undefined ||
61
+ state.context.objectId === undefined ||
62
+ request.objectId === state.context.objectId)));
63
+ }
64
+ /** Foreground intent is reusable only for the same workflow and originating conversation. */
65
+ function foregroundBuildIntent(state, type, pending) {
66
+ return state.interaction.build?.type === type &&
67
+ (!pending || matches(state.interaction, pending.threadId, pending.conversationId))
68
+ ? state.interaction.build.intent
69
+ : undefined;
70
+ }
71
+ /** Prefer request intent, then the owning foreground build, before inferring creation from other work. */
72
+ function buildIntent(state, type, pending) {
73
+ return ((pending?.type === type ? pending.intent : undefined) ??
74
+ (pending?.generation === type ? "create" : undefined) ??
75
+ foregroundBuildIntent(state, type, pending) ??
76
+ (requestedGeneration(state) === type || hasOngoingCreation(state, type) ? "create" : "edit"));
50
77
  }
51
78
  function finishedResponse(event) {
52
- return ((event.type === "chatAssistantMessage" && (event.responseCompleted || failedResponse(event))) ||
53
- (event.type === "chatClarificationQuestions" && event.responses === null));
79
+ return event.type === "chatAssistantMessage" && (event.responseCompleted || failedResponse(event));
54
80
  }
55
81
  function clarificationIds(ids, event) {
56
- return event.responses === undefined
57
- ? ids.includes(event.interactionId)
58
- ? ids
59
- : [...ids, event.interactionId]
60
- : ids.filter((id) => id !== event.interactionId);
82
+ if (event.responses !== undefined) {
83
+ return ids.filter((id) => id !== event.interactionId);
84
+ }
85
+ return ids.includes(event.interactionId) ? ids : [...ids, event.interactionId];
61
86
  }
62
87
  function transitionRequestClarification(pending, event) {
63
88
  const ids = pending.pendingClarificationIds ?? [];
64
89
  const pendingClarificationIds = clarificationIds(ids, event);
65
- return pending.awaitingClarification !== pendingClarificationIds.length > 0 ||
66
- pendingClarificationIds !== ids
67
- ? {
68
- ...pending,
69
- awaitingClarification: pendingClarificationIds.length > 0,
70
- pendingClarificationIds,
71
- }
72
- : pending;
90
+ const awaitingClarification = pendingClarificationIds.length > 0;
91
+ if (pending.awaitingClarification === awaitingClarification && pendingClarificationIds === ids) {
92
+ return pending;
93
+ }
94
+ return { ...pending, awaitingClarification, pendingClarificationIds };
73
95
  }
74
96
  function transitionPendingRequest(state, pending, event, registration) {
75
- if (event.type === "chatConversationChanged" && event.threadId && event.threadId !== pending.threadId) {
76
- return { ...pending, threadId: event.threadId };
97
+ if (event.type === "chatConversationChanged" &&
98
+ event.threadId &&
99
+ (event.threadId !== pending.threadId || event.conversation.localId !== pending.conversationId)) {
100
+ return { ...pending, threadId: event.threadId, conversationId: event.conversation.localId };
101
+ }
102
+ if (pending.superseded) {
103
+ return pending;
77
104
  }
78
105
  if (event.type === "chatClarificationQuestions") {
79
106
  return transitionRequestClarification(pending, event);
@@ -83,17 +110,59 @@ function transitionPendingRequest(state, pending, event, registration) {
83
110
  ...pending,
84
111
  threadId: event.threadId,
85
112
  type: registration.type,
113
+ intent: buildIntent(state, registration.type, pending),
86
114
  objectId: state.context.type === registration.type ? state.context.objectId : undefined,
87
115
  };
88
116
  }
89
117
  return pending;
90
118
  }
119
+ function requestedGeneration(state) {
120
+ return state.requests.some((request) => request.generation === state.generation && request.revision > (state.generationRevision ?? 0))
121
+ ? undefined
122
+ : state.generation;
123
+ }
124
+ function generationAfterRequests(state, requests) {
125
+ const ownsGeneration = (request) => !request.superseded &&
126
+ request.generation === state.generation &&
127
+ request.revision > (state.generationRevision ?? 0);
128
+ return state.requests.some(ownsGeneration) && !requests.some(ownsGeneration)
129
+ ? undefined
130
+ : state.generation;
131
+ }
132
+ /** A reply resumes known work even after its clarification form was dismissed, unless superseded. */
133
+ function canContinueRequest(pending, event) {
134
+ return !!(pending &&
135
+ !pending.superseded &&
136
+ (pending.type !== undefined ||
137
+ pending.generation !== undefined ||
138
+ pending.responseFinished ||
139
+ pending.awaitingClarification ||
140
+ event.type === "chatUserAnswers"));
141
+ }
142
+ /** Successful creation, clarification, and builder responses retain ownership while awaiting a reply. */
143
+ function waitsForReplyAfterResponse(state, request, event) {
144
+ if (request.superseded || event.type !== "chatAssistantMessage" || failedResponse(event)) {
145
+ return false;
146
+ }
147
+ if (request.type === undefined) {
148
+ return request.generation !== undefined;
149
+ }
150
+ return Boolean(request.awaitingClarification ||
151
+ request.intent === "create" ||
152
+ (state.context.builderTarget?.type === request.type &&
153
+ (request.objectId === undefined || state.context.builderTarget.objectId === request.objectId)));
154
+ }
91
155
  function transitionRequests(state, event, index, registration) {
92
156
  const requests = state.requests;
93
157
  const pending = requests[index];
94
- if (event.type === "chatUserMessage" || event.type === "chatUserAnswers") {
158
+ if (isUserInputEvent(event)) {
95
159
  // An old acknowledgement must not replace a newer request still applying a definition.
96
160
  const retained = pending?.applicationIds.length ? [pending] : [];
161
+ const continuation = canContinueRequest(pending, event) ? pending : undefined;
162
+ const generation = continuation?.generation ?? requestedGeneration(state);
163
+ const pendingClarificationIds = event.type === "chatUserAnswers"
164
+ ? continuation?.pendingClarificationIds?.filter((id) => id !== event.interactionId)
165
+ : undefined;
97
166
  return [
98
167
  ...requests.filter((_, i) => i !== index),
99
168
  ...retained,
@@ -101,6 +170,15 @@ function transitionRequests(state, event, index, registration) {
101
170
  revision: state.revision + 1,
102
171
  conversationId: conversationId(event),
103
172
  threadId: event.threadId,
173
+ type: continuation?.type,
174
+ generation,
175
+ intent: continuation?.intent,
176
+ objectId: continuation?.objectId ??
177
+ (generation !== undefined && state.context.type === generation
178
+ ? state.context.objectId
179
+ : undefined),
180
+ pendingClarificationIds,
181
+ awaitingClarification: !!pendingClarificationIds?.length,
104
182
  applicationIds: [],
105
183
  },
106
184
  ];
@@ -108,21 +186,27 @@ function transitionRequests(state, event, index, registration) {
108
186
  if (!pending || event.type === "chatReset") {
109
187
  return requests;
110
188
  }
189
+ const updated = transitionPendingRequest(state, pending, event, registration);
111
190
  if (finishedResponse(event)) {
112
- return pending.applicationIds.length
113
- ? requests.map((request, i) => (i === index ? { ...pending, responseFinished: true } : request))
191
+ const waitingForReply = waitsForReplyAfterResponse(state, updated, event);
192
+ return updated.applicationIds.length || waitingForReply
193
+ ? requests.map((request, i) => (i === index ? { ...updated, responseFinished: true } : request))
114
194
  : requests.filter((_, i) => i !== index);
115
195
  }
116
- const updated = transitionPendingRequest(state, pending, event, registration);
117
196
  return updated === pending ? requests : requests.map((request, i) => (i === index ? updated : request));
118
197
  }
198
+ /** Assigning a draft's first backend thread must preserve its foreground progress. */
199
+ function isInitialThreadAssignment(state, event, foreground) {
200
+ const origin = conversationId(event);
201
+ return (!!origin &&
202
+ state.requests.some((request) => request.conversationId === origin) &&
203
+ (!state.interaction.threadId || foreground) &&
204
+ (!state.interaction.conversationId || state.interaction.conversationId === origin));
205
+ }
119
206
  function transitionConversation(state, event, foreground) {
120
207
  const interaction = state.interaction;
121
208
  const origin = conversationId(event);
122
- const initialAssignment = !!origin &&
123
- state.requests.some((request) => request.conversationId === origin) &&
124
- (!interaction.threadId || foreground) &&
125
- (!interaction.conversationId || interaction.conversationId === origin);
209
+ const initialAssignment = isInitialThreadAssignment(state, event, foreground);
126
210
  return {
127
211
  ...(initialAssignment ? interaction : emptyInteraction()),
128
212
  conversationId: origin,
@@ -137,21 +221,23 @@ function transitionInteraction(state, event, foreground) {
137
221
  ? { ...interaction, reasoningTitle: event.title }
138
222
  : interaction;
139
223
  case "chatUserMessage":
224
+ case "chatUserAnswers": {
225
+ const ongoing = matches(interaction, event.threadId, conversationId(event))
226
+ ? interaction
227
+ : emptyInteraction();
140
228
  return {
141
- ...interaction,
229
+ ...ongoing,
142
230
  conversationId: conversationId(event),
143
231
  threadId: event.threadId,
144
232
  reasoningTitle: undefined,
145
233
  waitingForUserInput: false,
234
+ ...(event.type === "chatUserAnswers"
235
+ ? {
236
+ pendingClarificationIds: ongoing.pendingClarificationIds.filter((id) => id !== event.interactionId),
237
+ }
238
+ : {}),
146
239
  };
147
- case "chatUserAnswers":
148
- return foreground
149
- ? {
150
- ...interaction,
151
- waitingForUserInput: false,
152
- pendingClarificationIds: interaction.pendingClarificationIds.filter((id) => id !== event.interactionId),
153
- }
154
- : interaction;
240
+ }
155
241
  case "chatReset":
156
242
  return emptyInteraction();
157
243
  case "chatConversationChanged":
@@ -160,7 +246,7 @@ function transitionInteraction(state, event, foreground) {
160
246
  return interaction;
161
247
  }
162
248
  }
163
- function transitionAssistant(state, event, registration) {
249
+ function transitionAssistant(state, event, registration, pending) {
164
250
  let interaction = state.interaction;
165
251
  let generation = state.generation;
166
252
  if (failedResponse(event)) {
@@ -172,8 +258,7 @@ function transitionAssistant(state, event, registration) {
172
258
  ...interaction,
173
259
  build: {
174
260
  type: registration.type,
175
- intent: (interaction.build?.type === registration.type ? interaction.build.intent : undefined) ??
176
- (generation === registration.type ? "create" : "edit"),
261
+ intent: buildIntent(state, registration.type, pending),
177
262
  },
178
263
  waitingForUserInput: interaction.waitingForUserInput ||
179
264
  !!(event.responseCompleted && !interaction.pendingClarificationIds.length),
@@ -188,9 +273,7 @@ function transitionAssistant(state, event, registration) {
188
273
  return { interaction, generation };
189
274
  }
190
275
  function transitionClarification(state, event) {
191
- if (event.responses === null) {
192
- return { generation: undefined, interaction: emptyInteraction() };
193
- }
276
+ // Null dismisses the form before a typed reply; it does not cancel the owning workflow.
194
277
  const interaction = state.interaction;
195
278
  return {
196
279
  generation: state.generation,
@@ -201,26 +284,45 @@ function transitionClarification(state, event) {
201
284
  },
202
285
  };
203
286
  }
287
+ /** Before backend assignment, failures may identify the draft using its local conversation ID. */
288
+ function isDraftFailure(state, event, pending) {
289
+ return (event.type === "chatAssistantMessage" &&
290
+ failedResponse(event) &&
291
+ !state.interaction.threadId &&
292
+ !!pending &&
293
+ !pending.threadId &&
294
+ !pending.responseFinished &&
295
+ !!event.threadId &&
296
+ event.threadId === state.interaction.conversationId &&
297
+ pending.conversationId === state.interaction.conversationId);
298
+ }
299
+ /** Threadless events can update foreground work only while the foreground is also unassigned. */
300
+ function isUnassignedForegroundResponse(state, event, pending) {
301
+ return (!event.threadId &&
302
+ !state.interaction.threadId &&
303
+ (!!pending || state.generation !== undefined || !!state.interaction.build));
304
+ }
204
305
  function isForegroundResponse(state, event, pending, foreground) {
306
+ // Target navigation clears the thread but retains the local owner while an application is pending.
307
+ const retainedForeground = !state.interaction.threadId &&
308
+ !!pending?.threadId &&
309
+ matches(state.interaction, pending.threadId, pending.conversationId);
205
310
  return (foreground ||
206
- (event.type === "chatAssistantMessage" &&
207
- failedResponse(event) &&
208
- !state.interaction.threadId &&
209
- !!pending &&
210
- !pending.threadId &&
211
- !pending.responseFinished &&
212
- !!event.threadId &&
213
- event.threadId === state.interaction.conversationId &&
214
- pending.conversationId === state.interaction.conversationId) ||
215
- (!event.threadId &&
216
- !state.interaction.threadId &&
217
- (!!pending || state.generation !== undefined || !!state.interaction.build)));
311
+ retainedForeground ||
312
+ isDraftFailure(state, event, pending) ||
313
+ isUnassignedForegroundResponse(state, event, pending));
218
314
  }
219
315
  function transitionForegroundEvent(state, event, pending, registration) {
316
+ if (pending?.superseded &&
317
+ !isUserInputEvent(event) &&
318
+ event.type !== "chatConversationChanged" &&
319
+ event.type !== "chatReset") {
320
+ return { interaction: state.interaction, generation: state.generation };
321
+ }
220
322
  const foreground = !!event.threadId && event.threadId === state.interaction.threadId;
221
323
  const foregroundResponse = isForegroundResponse(state, event, pending, foreground);
222
324
  if (event.type === "chatAssistantMessage" && foregroundResponse) {
223
- return transitionAssistant(state, event, registration);
325
+ return transitionAssistant(state, event, registration, pending);
224
326
  }
225
327
  if (event.type === "chatClarificationQuestions" && foregroundResponse) {
226
328
  return transitionClarification(state, event);
@@ -230,19 +332,78 @@ function transitionForegroundEvent(state, event, pending, registration) {
230
332
  generation: state.generation,
231
333
  };
232
334
  }
233
- function transitionEvent(state, event, configuration) {
234
- if (event.type === "chatUserAnswers" && event.threadId !== state.interaction.threadId) {
235
- return state;
335
+ /** A new owner displaces only other conversations managing the same workflow and compatible target. */
336
+ function shouldSupersedeRequest(request, owner, type) {
337
+ return (request !== owner &&
338
+ !request.superseded &&
339
+ (request.type ?? request.generation) === type &&
340
+ (request.objectId === owner.objectId ||
341
+ request.objectId === undefined ||
342
+ owner.objectId === undefined) &&
343
+ !matches(request, owner.threadId, owner.conversationId));
344
+ }
345
+ function transferWorkflow(state, event, requests, pending, registration) {
346
+ if (!pending || pending.type || !registration) {
347
+ return requests;
236
348
  }
237
- const index = matchingRequestIndex(state.requests, event.threadId, conversationId(event));
238
- const pending = state.requests[index];
239
- const registration = event.type === "chatAssistantMessage"
349
+ const owner = requests.find((request) => request.revision === pending.revision) ??
350
+ transitionPendingRequest(state, pending, event, registration);
351
+ return requests.map((request) => shouldSupersedeRequest(request, owner, registration.type)
352
+ ? { ...request, superseded: true }
353
+ : request);
354
+ }
355
+ /** Reloads match the assigned backend thread first because a reload may replace its local ID. */
356
+ function eventRequestIndex(state, event) {
357
+ const assignedThreadIndex = event.type === "chatConversationChanged" && event.threadId
358
+ ? matchingRequestIndex(state.requests, event.threadId)
359
+ : -1;
360
+ return assignedThreadIndex >= 0
361
+ ? assignedThreadIndex
362
+ : matchingRequestIndex(state.requests, event.threadId, conversationId(event));
363
+ }
364
+ /** Only a live request or an unclaimed foreground generation can activate a configured workflow. */
365
+ function eventRegistration(state, event, pending, configuration) {
366
+ if (event.type !== "chatAssistantMessage" || failedResponse(event)) {
367
+ return undefined;
368
+ }
369
+ const canActivate = pending
370
+ ? !pending.superseded
371
+ : event.threadId === state.interaction.threadId && requestedGeneration(state) !== undefined;
372
+ return canActivate
240
373
  ? configuration.registrations.find((entry) => event.skills?.some((skill) => entry.skills.includes(skill.name)))
241
374
  : undefined;
242
- const requests = transitionRequests(state, event, index, registration);
243
- const foregroundState = transitionForegroundEvent(state, event, pending, registration);
375
+ }
376
+ /** Clear foreground progress only when this event newly supersedes its owning request. */
377
+ function hasNewlySupersededForeground(state, requests) {
378
+ return requests.some((request) => request.superseded &&
379
+ !state.requests.find((previous) => previous.revision === request.revision)?.superseded &&
380
+ matches(state.interaction, request.threadId, request.conversationId));
381
+ }
382
+ function transitionEvent(state, event, configuration) {
383
+ const index = eventRequestIndex(state, event);
384
+ const pending = state.requests[index];
385
+ if (event.type === "chatUserAnswers" &&
386
+ !pending &&
387
+ !matches(state.interaction, event.threadId, conversationId(event))) {
388
+ return state;
389
+ }
390
+ const registration = eventRegistration(state, event, pending, configuration);
391
+ const requests = transferWorkflow(state, event, transitionRequests(state, event, index, registration), pending, registration);
392
+ const supersededForeground = hasNewlySupersededForeground(state, requests);
393
+ const foregroundState = transitionForegroundEvent(supersededForeground
394
+ ? {
395
+ ...state,
396
+ interaction: {
397
+ ...emptyInteraction(),
398
+ conversationId: state.interaction.conversationId,
399
+ threadId: state.interaction.threadId,
400
+ },
401
+ }
402
+ : state, event, pending, registration);
244
403
  let interaction = foregroundState.interaction;
245
- const generation = foregroundState.generation;
404
+ const generation = foregroundState.generation === state.generation
405
+ ? generationAfterRequests(state, requests)
406
+ : foregroundState.generation;
246
407
  if (sameInteraction(interaction, state.interaction)) {
247
408
  interaction = state.interaction;
248
409
  }
@@ -253,9 +414,7 @@ function transitionEvent(state, event, configuration) {
253
414
  requests,
254
415
  interaction,
255
416
  generation,
256
- revision: event.type === "chatUserMessage" || event.type === "chatUserAnswers"
257
- ? state.revision + 1
258
- : state.revision,
417
+ revision: isUserInputEvent(event) ? state.revision + 1 : state.revision,
259
418
  };
260
419
  }
261
420
  function transitionContext(state, context) {
@@ -272,14 +431,22 @@ function transitionContext(state, context) {
272
431
  context: { ...b, builderTarget: b.builderTarget ? { ...b.builderTarget } : undefined },
273
432
  };
274
433
  }
275
- function requestsAfterTargetReset(requests, target) {
276
- return requests.some((request) => request.applicationIds.length &&
434
+ /** Definition installation outlives navigation and must retain its request until acknowledgement. */
435
+ function isApplyingToTarget(request, target) {
436
+ return (request.applicationIds.length > 0 &&
277
437
  request.type === target.type &&
278
- (request.objectId === undefined || request.objectId === target.objectId))
438
+ (request.objectId === undefined || request.objectId === target.objectId));
439
+ }
440
+ /** Only work explicitly bound to another workflow or object survives a target reset. */
441
+ function belongsToOtherTarget(request, target) {
442
+ const type = request.type ?? request.generation;
443
+ return (type !== undefined &&
444
+ (type !== target.type || (request.objectId !== undefined && request.objectId !== target.objectId)));
445
+ }
446
+ function requestsAfterTargetReset(requests, target) {
447
+ return requests.some((request) => isApplyingToTarget(request, target))
279
448
  ? requests
280
- : requests.filter((request) => request.type !== undefined &&
281
- (request.type !== target.type ||
282
- (request.objectId !== undefined && request.objectId !== target.objectId)));
449
+ : requests.filter((request) => belongsToOtherTarget(request, target));
283
450
  }
284
451
  function transitionTargetReset(state, target) {
285
452
  const next = {
@@ -385,7 +552,7 @@ function transitionApplicationFinished(state, input) {
385
552
  const applications = state.applications.filter((entry) => entry !== application);
386
553
  const requests = requestsAfterApplicationFinished(state.requests, input);
387
554
  if (input.kind === "applicationInterrupted") {
388
- return { ...state, applications, requests };
555
+ return { ...state, applications, requests, generation: generationAfterRequests(state, requests) };
389
556
  }
390
557
  const sources = sourcesAfterApplicationApplied(state.sources, application, input.target);
391
558
  const isForeground = isForegroundApplication(state, application);
@@ -395,11 +562,18 @@ function transitionApplicationFinished(state, input) {
395
562
  applications,
396
563
  requests,
397
564
  sources,
565
+ generation: generationAfterRequests(state, requests),
398
566
  ...(isForeground && !hasNewer
399
567
  ? { generation: undefined, interaction: emptyInteraction() }
400
568
  : {}),
401
569
  };
402
570
  }
571
+ /** Cancelling generation drops completed waits, but running responses and host installations still need cleanup. */
572
+ function isCompletedGenerationRequest(request, type) {
573
+ return !!((request.type ?? request.generation) === type &&
574
+ request.responseFinished &&
575
+ !request.applicationIds.length);
576
+ }
403
577
  /** Deterministic immutable transition; never allocates IDs, invokes effects, or performs I/O. @alpha */
404
578
  export function transitionCopilot(state, input, configuration) {
405
579
  switch (input.kind) {
@@ -415,10 +589,19 @@ export function transitionCopilot(state, input, configuration) {
415
589
  revision: state.revision + 1,
416
590
  interaction: emptyInteraction(),
417
591
  };
418
- case "cancelGeneration":
419
- return state.generation === input.type || state.interaction.build?.type === input.type
420
- ? { ...state, generation: undefined, interaction: emptyInteraction() }
592
+ case "cancelGeneration": {
593
+ const requests = state.requests.filter((request) => !isCompletedGenerationRequest(request, input.type));
594
+ const clearForeground = state.generation === input.type || state.interaction.build?.type === input.type;
595
+ return clearForeground || requests.length !== state.requests.length
596
+ ? {
597
+ ...state,
598
+ ...(clearForeground
599
+ ? { generation: undefined, interaction: emptyInteraction() }
600
+ : {}),
601
+ requests,
602
+ }
421
603
  : state;
604
+ }
422
605
  case "resetInteraction":
423
606
  return sameInteraction(state.interaction, emptyInteraction())
424
607
  ? state