@valerypopoff/rivet2-core 2.1.3 → 2.1.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/bundle.cjs +2707 -1012
- package/dist/cjs/bundle.cjs.map +4 -4
- package/dist/cjs/webAppRuntime.cjs +625 -0
- package/dist/cjs/webAppRuntime.cjs.map +7 -0
- package/dist/esm/api/createProcessor.js +3 -0
- package/dist/esm/api/streaming.js +6 -0
- package/dist/esm/exports.js +5 -0
- package/dist/esm/integrations/mcp/MCPBase.js +92 -34
- package/dist/esm/model/GraphProcessor.js +11 -4
- package/dist/esm/model/GraphProgress.js +15 -0
- package/dist/esm/model/Nodes.js +3 -0
- package/dist/esm/model/ProcessContextBuilder.js +2 -1
- package/dist/esm/model/RecordingPlayer.js +10 -0
- package/dist/esm/model/SubprocessorBridge.js +1 -0
- package/dist/esm/model/UiGraph.js +127 -4
- package/dist/esm/model/UiGraphActionProtocol.js +122 -0
- package/dist/esm/model/UiGraphBindings.js +530 -0
- package/dist/esm/model/UiGraphBrowserRuntime.js +40 -0
- package/dist/esm/model/UiGraphNormalization.js +298 -0
- package/dist/esm/model/UiGraphRendererStyles.js +374 -6
- package/dist/esm/model/UiGraphRuntimeModel.js +300 -0
- package/dist/esm/model/UiGraphWebAppStatus.js +19 -0
- package/dist/esm/model/nodes/ArrayNode.js +2 -13
- package/dist/esm/model/nodes/AssembleMessageNode.js +2 -13
- package/dist/esm/model/nodes/AssemblePromptNode.js +2 -13
- package/dist/esm/model/nodes/CoalesceNode.js +2 -15
- package/dist/esm/model/nodes/DelayNode.js +3 -14
- package/dist/esm/model/nodes/DidRunNode.js +2 -13
- package/dist/esm/model/nodes/JoinNode.js +2 -13
- package/dist/esm/model/nodes/LoopControllerNode.js +3 -14
- package/dist/esm/model/nodes/MCPDiscoveryNode.js +19 -90
- package/dist/esm/model/nodes/MCPGetPromptNode.js +11 -97
- package/dist/esm/model/nodes/MCPToolCallNode.js +11 -97
- package/dist/esm/model/nodes/PassthroughNode.js +3 -14
- package/dist/esm/model/nodes/RaceInputsNode.js +2 -13
- package/dist/esm/model/nodes/ReportProgressNode.js +57 -0
- package/dist/esm/model/nodes/variadicPortIndex.js +32 -0
- package/dist/esm/plugins/anthropic/anthropic.js +1 -1
- package/dist/esm/recording/ExecutionRecorder.js +5 -0
- package/dist/esm/utils/fetchEventSource.js +16 -17
- package/dist/esm/utils/serialization/serialization.js +4 -0
- package/dist/esm/utils/serialization/serializationUtils.js +8 -27
- package/dist/esm/utils/serialization/serialization_v4.js +3 -3
- package/dist/esm/webAppRuntime.js +4 -2
- package/dist/types/api/streaming.d.ts +6 -0
- package/dist/types/exports.d.ts +5 -0
- package/dist/types/integrations/mcp/MCPBase.d.ts +26 -3
- package/dist/types/model/ExecutorProtocol.d.ts +7 -0
- package/dist/types/model/GraphProcessor.d.ts +7 -0
- package/dist/types/model/GraphProgress.d.ts +6 -0
- package/dist/types/model/Nodes.d.ts +3 -2
- package/dist/types/model/ProcessContext.d.ts +3 -0
- package/dist/types/model/ProcessContextBuilder.d.ts +2 -1
- package/dist/types/model/UiGraph.d.ts +42 -9
- package/dist/types/model/UiGraphActionProtocol.d.ts +69 -0
- package/dist/types/model/UiGraphBindings.d.ts +45 -0
- package/dist/types/model/UiGraphBrowserRuntime.d.ts +4 -0
- package/dist/types/model/UiGraphNormalization.d.ts +24 -0
- package/dist/types/model/UiGraphRendererStyles.d.ts +1 -1
- package/dist/types/model/UiGraphRuntimeModel.d.ts +77 -5
- package/dist/types/model/UiGraphWebAppStatus.d.ts +5 -0
- package/dist/types/model/nodes/ArrayNode.d.ts +0 -1
- package/dist/types/model/nodes/AssembleMessageNode.d.ts +0 -1
- package/dist/types/model/nodes/AssemblePromptNode.d.ts +0 -1
- package/dist/types/model/nodes/DelayNode.d.ts +0 -1
- package/dist/types/model/nodes/DidRunNode.d.ts +0 -1
- package/dist/types/model/nodes/JoinNode.d.ts +0 -1
- package/dist/types/model/nodes/LoopControllerNode.d.ts +0 -1
- package/dist/types/model/nodes/PassthroughNode.d.ts +0 -1
- package/dist/types/model/nodes/RaceInputsNode.d.ts +0 -1
- package/dist/types/model/nodes/ReportProgressNode.d.ts +23 -0
- package/dist/types/model/nodes/variadicPortIndex.d.ts +4 -0
- package/dist/types/recording/RecordedEvents.d.ts +5 -0
- package/dist/types/webAppRuntime.d.ts +8 -3
- package/package.json +2 -2
- package/dist/esm/plugins/anthropic/fetchEventSource.js +0 -107
- package/dist/types/plugins/anthropic/fetchEventSource.d.ts +0 -11
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import { normalizeGraphProgress } from './GraphProgress.js';
|
|
2
|
+
export const RIVET_WEB_APP_ACTION_PROTOCOL_VERSION = 1;
|
|
3
|
+
export function parseRivetWebAppClientMessage(value) {
|
|
4
|
+
if (!isRecord(value) || typeof value.type !== 'string')
|
|
5
|
+
return undefined;
|
|
6
|
+
switch (value.type) {
|
|
7
|
+
case 'client.hello':
|
|
8
|
+
return value.protocolVersion === RIVET_WEB_APP_ACTION_PROTOCOL_VERSION
|
|
9
|
+
? { type: value.type, protocolVersion: value.protocolVersion }
|
|
10
|
+
: undefined;
|
|
11
|
+
case 'action.start':
|
|
12
|
+
return isNonEmptyString(value.requestId) &&
|
|
13
|
+
isNonEmptyString(value.componentId) &&
|
|
14
|
+
isRecord(value.state) &&
|
|
15
|
+
(value.revisionKey == null || typeof value.revisionKey === 'string')
|
|
16
|
+
? {
|
|
17
|
+
type: value.type,
|
|
18
|
+
requestId: value.requestId,
|
|
19
|
+
componentId: value.componentId,
|
|
20
|
+
state: value.state,
|
|
21
|
+
...(typeof value.revisionKey === 'string' ? { revisionKey: value.revisionKey } : {}),
|
|
22
|
+
}
|
|
23
|
+
: undefined;
|
|
24
|
+
case 'action.cancel':
|
|
25
|
+
return isNonEmptyString(value.runId) ? { type: value.type, runId: value.runId } : undefined;
|
|
26
|
+
case 'run.resume':
|
|
27
|
+
return isNonEmptyString(value.runId) &&
|
|
28
|
+
typeof value.lastSequence === 'number' &&
|
|
29
|
+
Number.isSafeInteger(value.lastSequence) &&
|
|
30
|
+
value.lastSequence >= 0
|
|
31
|
+
? { type: value.type, runId: value.runId, lastSequence: value.lastSequence }
|
|
32
|
+
: undefined;
|
|
33
|
+
default:
|
|
34
|
+
return undefined;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
export function parseRivetWebAppServerMessage(value) {
|
|
38
|
+
if (!isRecord(value) || typeof value.type !== 'string')
|
|
39
|
+
return undefined;
|
|
40
|
+
if (value.type === 'server.ready') {
|
|
41
|
+
return value.protocolVersion === RIVET_WEB_APP_ACTION_PROTOCOL_VERSION
|
|
42
|
+
? { type: value.type, protocolVersion: value.protocolVersion }
|
|
43
|
+
: undefined;
|
|
44
|
+
}
|
|
45
|
+
if (value.type === 'server.draining')
|
|
46
|
+
return { type: value.type };
|
|
47
|
+
if (value.type === 'action.rejected') {
|
|
48
|
+
return isNonEmptyString(value.requestId) && isNonEmptyString(value.error)
|
|
49
|
+
? {
|
|
50
|
+
type: value.type,
|
|
51
|
+
requestId: value.requestId,
|
|
52
|
+
error: value.error,
|
|
53
|
+
...(typeof value.code === 'string' ? { code: value.code } : {}),
|
|
54
|
+
}
|
|
55
|
+
: undefined;
|
|
56
|
+
}
|
|
57
|
+
if (value.type === 'run.rejected') {
|
|
58
|
+
return isNonEmptyString(value.runId) && isNonEmptyString(value.error)
|
|
59
|
+
? {
|
|
60
|
+
type: value.type,
|
|
61
|
+
runId: value.runId,
|
|
62
|
+
error: value.error,
|
|
63
|
+
...(typeof value.code === 'string' ? { code: value.code } : {}),
|
|
64
|
+
}
|
|
65
|
+
: undefined;
|
|
66
|
+
}
|
|
67
|
+
if (!hasRunEventBase(value))
|
|
68
|
+
return undefined;
|
|
69
|
+
const base = {
|
|
70
|
+
requestId: value.requestId,
|
|
71
|
+
runId: value.runId,
|
|
72
|
+
sequence: value.sequence,
|
|
73
|
+
};
|
|
74
|
+
switch (value.type) {
|
|
75
|
+
case 'action.accepted':
|
|
76
|
+
case 'action.cancelled':
|
|
77
|
+
return { type: value.type, ...base };
|
|
78
|
+
case 'action.progress': {
|
|
79
|
+
if (!isRecord(value.progress))
|
|
80
|
+
return undefined;
|
|
81
|
+
const progress = normalizeGraphProgress({
|
|
82
|
+
message: typeof value.progress.message === 'string' ? value.progress.message : undefined,
|
|
83
|
+
percent: typeof value.progress.percent === 'number' ? value.progress.percent : undefined,
|
|
84
|
+
});
|
|
85
|
+
return progress ? { type: value.type, ...base, progress } : undefined;
|
|
86
|
+
}
|
|
87
|
+
case 'action.completed':
|
|
88
|
+
return isRecord(value.statePatch) ? { type: value.type, ...base, statePatch: value.statePatch } : undefined;
|
|
89
|
+
case 'action.failed':
|
|
90
|
+
return isNonEmptyString(value.error)
|
|
91
|
+
? {
|
|
92
|
+
type: value.type,
|
|
93
|
+
...base,
|
|
94
|
+
error: value.error,
|
|
95
|
+
...(typeof value.code === 'string' ? { code: value.code } : {}),
|
|
96
|
+
}
|
|
97
|
+
: undefined;
|
|
98
|
+
case 'action.interrupted':
|
|
99
|
+
return isNonEmptyString(value.error) ? { type: value.type, ...base, error: value.error } : undefined;
|
|
100
|
+
default:
|
|
101
|
+
return undefined;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
export function isRivetWebAppRunTerminalEvent(event) {
|
|
105
|
+
return (event.type === 'action.completed' ||
|
|
106
|
+
event.type === 'action.failed' ||
|
|
107
|
+
event.type === 'action.cancelled' ||
|
|
108
|
+
event.type === 'action.interrupted');
|
|
109
|
+
}
|
|
110
|
+
function hasRunEventBase(value) {
|
|
111
|
+
return (isNonEmptyString(value.requestId) &&
|
|
112
|
+
isNonEmptyString(value.runId) &&
|
|
113
|
+
typeof value.sequence === 'number' &&
|
|
114
|
+
Number.isSafeInteger(value.sequence) &&
|
|
115
|
+
value.sequence > 0);
|
|
116
|
+
}
|
|
117
|
+
function isRecord(value) {
|
|
118
|
+
return typeof value === 'object' && value != null && !Array.isArray(value);
|
|
119
|
+
}
|
|
120
|
+
function isNonEmptyString(value) {
|
|
121
|
+
return typeof value === 'string' && value.length > 0;
|
|
122
|
+
}
|
|
@@ -0,0 +1,530 @@
|
|
|
1
|
+
import { getGraphBoundary } from './GraphBoundaryCache.js';
|
|
2
|
+
import { getUiGraphActionInputBindings, getUiGraphActionOutputBindings, } from './UiGraph.js';
|
|
3
|
+
const CHAT_HISTORY_INPUT_PATTERN = /(?:history|conversation|messages)/i;
|
|
4
|
+
const CHAT_USER_INPUT_PATTERN = /(?:user|prompt|question|message|input)/i;
|
|
5
|
+
/**
|
|
6
|
+
* Aligns a button with a graph boundary without guessing by row position.
|
|
7
|
+
* Exact IDs win; state keys cross an ID change only for a proven node rename.
|
|
8
|
+
*/
|
|
9
|
+
export function reconcileUiGraphRunGraphActionBindings(action, boundary, renames = {}) {
|
|
10
|
+
return reconcileActionBindings(action, boundary, renames, {
|
|
11
|
+
inputs: { addMissing: true },
|
|
12
|
+
outputs: { addMissing: true },
|
|
13
|
+
});
|
|
14
|
+
}
|
|
15
|
+
function reconcileActionBindings(action, boundary, renames, options) {
|
|
16
|
+
let nextAction = action;
|
|
17
|
+
const getEditableAction = () => {
|
|
18
|
+
if (nextAction === action) {
|
|
19
|
+
nextAction = { ...action };
|
|
20
|
+
}
|
|
21
|
+
return nextAction;
|
|
22
|
+
};
|
|
23
|
+
if (options.inputs) {
|
|
24
|
+
if (action.inputMappings) {
|
|
25
|
+
const editableAction = getEditableAction();
|
|
26
|
+
editableAction.inputMappings = alignInputMappings(boundary, action.inputMappings, renames.inputIds, options.inputs.addMissing);
|
|
27
|
+
delete editableAction.inputs;
|
|
28
|
+
}
|
|
29
|
+
else if (action.inputs) {
|
|
30
|
+
const editableAction = getEditableAction();
|
|
31
|
+
if (Object.values(action.inputs).every((binding) => binding.type === 'state')) {
|
|
32
|
+
editableAction.inputMappings = alignInputMappings(boundary, getUiGraphActionInputBindings(action), renames.inputIds, options.inputs.addMissing);
|
|
33
|
+
delete editableAction.inputs;
|
|
34
|
+
}
|
|
35
|
+
else {
|
|
36
|
+
editableAction.inputs = alignLegacyInputs(boundary, action.inputs, renames.inputIds, options.inputs.addMissing);
|
|
37
|
+
delete editableAction.inputMappings;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
else if (options.inputs.addMissing && boundary.inputs.length > 0) {
|
|
41
|
+
getEditableAction().inputMappings = alignInputMappings(boundary, [], renames.inputIds, true);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
if (options.outputs) {
|
|
45
|
+
const currentOutputs = getUiGraphActionOutputBindings(action);
|
|
46
|
+
if (currentOutputs.length > 0 || (options.outputs.addMissing && boundary.outputs.length > 0)) {
|
|
47
|
+
const editableAction = getEditableAction();
|
|
48
|
+
editableAction.outputs = alignOutputMappings(boundary, currentOutputs, renames.outputIds, options.outputs.addMissing);
|
|
49
|
+
delete editableAction.outputKey;
|
|
50
|
+
delete editableAction.outputStateKey;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
return nextAction;
|
|
54
|
+
}
|
|
55
|
+
/** Seeds a new button from ordered factory placeholder data keys. */
|
|
56
|
+
export function initializeUiGraphRunGraphActionBindings(action, boundary) {
|
|
57
|
+
const inputSeeds = getUiGraphActionInputBindings(action);
|
|
58
|
+
const outputSeeds = getUiGraphActionOutputBindings(action);
|
|
59
|
+
const outputStateKeys = new Set();
|
|
60
|
+
const nextAction = {
|
|
61
|
+
...action,
|
|
62
|
+
inputMappings: boundary.inputs.map((input, index) => ({
|
|
63
|
+
inputKey: input.id,
|
|
64
|
+
stateKey: inputSeeds[index]?.stateKey || input.id,
|
|
65
|
+
})),
|
|
66
|
+
outputs: boundary.outputs.map((output, index) => ({
|
|
67
|
+
outputKey: output.id,
|
|
68
|
+
stateKey: createUniqueStateKey(outputSeeds[index]?.stateKey || output.id, outputStateKeys),
|
|
69
|
+
})),
|
|
70
|
+
};
|
|
71
|
+
delete nextAction.inputs;
|
|
72
|
+
delete nextAction.outputKey;
|
|
73
|
+
delete nextAction.outputStateKey;
|
|
74
|
+
return nextAction;
|
|
75
|
+
}
|
|
76
|
+
/** Seeds the three graph-boundary roles required by a new Chat component. */
|
|
77
|
+
export function initializeUiGraphChatActionBindings(action, boundary) {
|
|
78
|
+
const historyInput = boundary?.inputs.find((input) => input.dataType === 'chat-message[]') ??
|
|
79
|
+
boundary?.inputs.find((input) => CHAT_HISTORY_INPUT_PATTERN.test(input.id)) ??
|
|
80
|
+
boundary?.inputs[1];
|
|
81
|
+
const userInputCandidates = boundary?.inputs.filter((input) => input.id !== historyInput?.id) ?? [];
|
|
82
|
+
const userInput = userInputCandidates.find((input) => CHAT_USER_INPUT_PATTERN.test(input.id) && !CHAT_HISTORY_INPUT_PATTERN.test(input.id)) ??
|
|
83
|
+
userInputCandidates.find((input) => !CHAT_HISTORY_INPUT_PATTERN.test(input.id)) ??
|
|
84
|
+
userInputCandidates[0];
|
|
85
|
+
return {
|
|
86
|
+
...action,
|
|
87
|
+
userInputId: userInput?.id,
|
|
88
|
+
historyInputId: historyInput?.id,
|
|
89
|
+
responseOutputId: boundary?.outputs[0]?.id,
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
export function reconcileUiGraphChatActionBindings(action, boundary, renames = {}, changed = { inputs: true, outputs: true }) {
|
|
93
|
+
const userInputId = changed.inputs
|
|
94
|
+
? reconcileBoundaryId(action.userInputId, boundary.inputs, renames.inputIds)
|
|
95
|
+
: action.userInputId;
|
|
96
|
+
const historyInputId = changed.inputs
|
|
97
|
+
? reconcileBoundaryId(action.historyInputId, boundary.inputs, renames.inputIds)
|
|
98
|
+
: action.historyInputId;
|
|
99
|
+
const responseOutputId = changed.outputs
|
|
100
|
+
? reconcileBoundaryId(action.responseOutputId, boundary.outputs, renames.outputIds)
|
|
101
|
+
: action.responseOutputId;
|
|
102
|
+
const inputMappings = changed.inputs && action.inputMappings
|
|
103
|
+
? reconcileChatAdditionalInputMappings(boundary, action.inputMappings, renames.inputIds)
|
|
104
|
+
: action.inputMappings;
|
|
105
|
+
return userInputId === action.userInputId &&
|
|
106
|
+
historyInputId === action.historyInputId &&
|
|
107
|
+
responseOutputId === action.responseOutputId &&
|
|
108
|
+
inputMappings === action.inputMappings
|
|
109
|
+
? action
|
|
110
|
+
: { ...action, userInputId, historyInputId, responseOutputId, inputMappings };
|
|
111
|
+
}
|
|
112
|
+
export function getReconciledUiGraphActionInputBindings(action, boundary) {
|
|
113
|
+
return getUiGraphActionInputBindings(reconcileUiGraphRunGraphActionBindings(action, boundary));
|
|
114
|
+
}
|
|
115
|
+
export function getReconciledUiGraphActionOutputBindings(action, boundary) {
|
|
116
|
+
return getUiGraphActionOutputBindings(reconcileUiGraphRunGraphActionBindings(action, boundary));
|
|
117
|
+
}
|
|
118
|
+
/** Reconciles workflow-bound UI components for changed graph boundaries. */
|
|
119
|
+
export function reconcileProjectUiGraphBindings(previousProject, nextProject) {
|
|
120
|
+
if (!nextProject.uiGraphs) {
|
|
121
|
+
return nextProject;
|
|
122
|
+
}
|
|
123
|
+
const changes = new Map();
|
|
124
|
+
let uiGraphs;
|
|
125
|
+
for (const [uiGraphId, uiGraph] of Object.entries(nextProject.uiGraphs)) {
|
|
126
|
+
let components;
|
|
127
|
+
uiGraph.components.forEach((component, index) => {
|
|
128
|
+
if ((component.type !== 'button' && component.type !== 'chat') ||
|
|
129
|
+
!component.action.graphId ||
|
|
130
|
+
(component.type === 'button' && hasAmbiguousUiGraphButtonBindings(component.action))) {
|
|
131
|
+
return;
|
|
132
|
+
}
|
|
133
|
+
const graphId = component.action.graphId;
|
|
134
|
+
if (!changes.has(graphId)) {
|
|
135
|
+
changes.set(graphId, getGraphBoundaryChange(previousProject, nextProject, graphId));
|
|
136
|
+
}
|
|
137
|
+
const change = changes.get(graphId);
|
|
138
|
+
if (!change) {
|
|
139
|
+
return;
|
|
140
|
+
}
|
|
141
|
+
const action = component.type === 'button'
|
|
142
|
+
? reconcileActionBindings(component.action, change.boundary, change.renames, {
|
|
143
|
+
inputs: change.inputsChanged
|
|
144
|
+
? { addMissing: hasCompleteInputCoverage(component.action, change.previousBoundary) }
|
|
145
|
+
: undefined,
|
|
146
|
+
outputs: change.outputsChanged
|
|
147
|
+
? { addMissing: hasCompleteOutputCoverage(component.action, change.previousBoundary) }
|
|
148
|
+
: undefined,
|
|
149
|
+
})
|
|
150
|
+
: reconcileUiGraphChatActionBindings(component.action, change.boundary, change.renames, {
|
|
151
|
+
inputs: change.inputsChanged,
|
|
152
|
+
outputs: change.outputsChanged,
|
|
153
|
+
});
|
|
154
|
+
if (action === component.action) {
|
|
155
|
+
return;
|
|
156
|
+
}
|
|
157
|
+
components ??= [...uiGraph.components];
|
|
158
|
+
components[index] = {
|
|
159
|
+
...component,
|
|
160
|
+
action,
|
|
161
|
+
};
|
|
162
|
+
});
|
|
163
|
+
if (components) {
|
|
164
|
+
uiGraphs ??= { ...nextProject.uiGraphs };
|
|
165
|
+
uiGraphs[uiGraphId] = { ...uiGraph, components };
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
return uiGraphs ? { ...nextProject, uiGraphs } : nextProject;
|
|
169
|
+
}
|
|
170
|
+
/** @deprecated Use reconcileProjectUiGraphBindings for all workflow-bound components. */
|
|
171
|
+
export const reconcileProjectUiGraphButtonBindings = reconcileProjectUiGraphBindings;
|
|
172
|
+
/** Non-mutating preflight for desktop and hosted web-app runners. */
|
|
173
|
+
export function validateUiGraphActionBindings(project, uiGraph, componentId) {
|
|
174
|
+
return uiGraph.components.flatMap((component) => {
|
|
175
|
+
if (componentId && component.id !== componentId) {
|
|
176
|
+
return [];
|
|
177
|
+
}
|
|
178
|
+
if (component.type === 'button') {
|
|
179
|
+
return validateButtonBindings(project, uiGraph.id, component);
|
|
180
|
+
}
|
|
181
|
+
if (component.type === 'chat') {
|
|
182
|
+
return validateChatBindings(project, uiGraph.id, component);
|
|
183
|
+
}
|
|
184
|
+
return [];
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
export function validateUiGraphButtonBindings(project, uiGraph, componentId) {
|
|
188
|
+
return uiGraph.components.flatMap((component) => {
|
|
189
|
+
if (component.type !== 'button' || (componentId && component.id !== componentId)) {
|
|
190
|
+
return [];
|
|
191
|
+
}
|
|
192
|
+
return validateButtonBindings(project, uiGraph.id, component);
|
|
193
|
+
});
|
|
194
|
+
}
|
|
195
|
+
export function validateProjectUiGraphButtonBindings(project) {
|
|
196
|
+
return Object.values(project.uiGraphs ?? {}).flatMap((uiGraph) => validateUiGraphButtonBindings(project, uiGraph));
|
|
197
|
+
}
|
|
198
|
+
export function validateProjectUiGraphActionBindings(project) {
|
|
199
|
+
return Object.values(project.uiGraphs ?? {}).flatMap((uiGraph) => validateUiGraphActionBindings(project, uiGraph));
|
|
200
|
+
}
|
|
201
|
+
export function formatUiGraphActionBindingIssues(issues) {
|
|
202
|
+
return issues.map((issue) => issue.message).join(' ');
|
|
203
|
+
}
|
|
204
|
+
export function formatUiGraphButtonBindingIssues(issues) {
|
|
205
|
+
return formatUiGraphActionBindingIssues(issues);
|
|
206
|
+
}
|
|
207
|
+
function validateButtonBindings(project, uiGraphId, component) {
|
|
208
|
+
const graphId = component.action.graphId;
|
|
209
|
+
const context = { componentId: component.id, graphId, uiGraphId };
|
|
210
|
+
if (!graphId) {
|
|
211
|
+
return [createIssue(context, 'missing-target-graph', 'The button has no target graph.')];
|
|
212
|
+
}
|
|
213
|
+
const boundary = getGraphBoundary(project, graphId);
|
|
214
|
+
if (!boundary) {
|
|
215
|
+
return [createIssue(context, 'target-graph-not-found', `The button target graph "${graphId}" does not exist.`)];
|
|
216
|
+
}
|
|
217
|
+
return [
|
|
218
|
+
...validateInputBindings(context, component, boundary),
|
|
219
|
+
...validateOutputBindings(context, component, boundary),
|
|
220
|
+
];
|
|
221
|
+
}
|
|
222
|
+
function validateChatBindings(project, uiGraphId, component) {
|
|
223
|
+
const graphId = component.action.graphId;
|
|
224
|
+
const context = { componentId: component.id, graphId, uiGraphId };
|
|
225
|
+
if (!graphId) {
|
|
226
|
+
return [createIssue(context, 'missing-target-graph', 'The Chat component has no target graph.')];
|
|
227
|
+
}
|
|
228
|
+
const boundary = getGraphBoundary(project, graphId);
|
|
229
|
+
if (!boundary) {
|
|
230
|
+
return [
|
|
231
|
+
createIssue(context, 'target-graph-not-found', `The Chat component target graph "${graphId}" does not exist.`),
|
|
232
|
+
];
|
|
233
|
+
}
|
|
234
|
+
const issues = [];
|
|
235
|
+
const inputIds = new Set(boundary.inputs.map((input) => input.id));
|
|
236
|
+
const outputIds = new Set(boundary.outputs.map((output) => output.id));
|
|
237
|
+
validateChatPort(issues, context, component.action.userInputId, inputIds, 'missing-chat-user-input', 'user message input', 'input');
|
|
238
|
+
validateChatPort(issues, context, component.action.historyInputId, inputIds, 'missing-chat-history-input', 'conversation history input', 'input');
|
|
239
|
+
validateChatPort(issues, context, component.action.responseOutputId, outputIds, 'missing-chat-response-output', 'assistant response output', 'output');
|
|
240
|
+
if (component.action.userInputId &&
|
|
241
|
+
component.action.historyInputId &&
|
|
242
|
+
component.action.userInputId === component.action.historyInputId) {
|
|
243
|
+
issues.push(createIssue(context, 'duplicate-chat-input', 'The Chat user message and conversation history must use different graph inputs.', component.action.userInputId));
|
|
244
|
+
}
|
|
245
|
+
validateChatAdditionalInputs(issues, context, component, inputIds);
|
|
246
|
+
return issues;
|
|
247
|
+
}
|
|
248
|
+
function validateChatAdditionalInputs(issues, context, component, availableIds) {
|
|
249
|
+
const seenIds = new Set([component.action.userInputId, component.action.historyInputId].filter(Boolean));
|
|
250
|
+
for (const binding of component.action.inputMappings ?? []) {
|
|
251
|
+
const hasInputKey = binding.inputKey.trim().length > 0;
|
|
252
|
+
if (!hasInputKey) {
|
|
253
|
+
issues.push(createIssue(context, 'empty-input-id', 'A Chat additional input has an empty graph input ID.'));
|
|
254
|
+
}
|
|
255
|
+
else if (seenIds.has(binding.inputKey)) {
|
|
256
|
+
issues.push(createIssue(context, 'duplicate-input-binding', `Graph input "${binding.inputKey}" is used more than once by the Chat component.`, binding.inputKey));
|
|
257
|
+
}
|
|
258
|
+
else if (!availableIds.has(binding.inputKey)) {
|
|
259
|
+
issues.push(createIssue(context, 'stale-input-binding', `The Chat additional input "${binding.inputKey}" no longer exists.`, binding.inputKey));
|
|
260
|
+
}
|
|
261
|
+
if (hasInputKey) {
|
|
262
|
+
seenIds.add(binding.inputKey);
|
|
263
|
+
}
|
|
264
|
+
if (!binding.stateKey.trim()) {
|
|
265
|
+
issues.push(createIssue(context, 'empty-input-state-key', hasInputKey
|
|
266
|
+
? `Chat graph input "${binding.inputKey}" has no data key to send.`
|
|
267
|
+
: 'A Chat additional input has no data key to send.', binding.inputKey));
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
function validateChatPort(issues, context, portId, availableIds, missingCode, label, portKind) {
|
|
272
|
+
if (!portId) {
|
|
273
|
+
issues.push(createIssue(context, missingCode, `The Chat component has no ${label}.`));
|
|
274
|
+
}
|
|
275
|
+
else if (!availableIds.has(portId)) {
|
|
276
|
+
issues.push(createIssue(context, portKind === 'input' ? 'stale-input-binding' : 'stale-output-binding', `The Chat ${label} "${portId}" no longer exists.`, portId));
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
function validateInputBindings(context, component, boundary) {
|
|
280
|
+
const issues = [];
|
|
281
|
+
const boundaryIds = new Set(boundary.inputs.map((input) => input.id));
|
|
282
|
+
const seenIds = new Set();
|
|
283
|
+
for (const binding of getUiGraphActionInputBindings(component.action)) {
|
|
284
|
+
if (!binding.inputKey.trim()) {
|
|
285
|
+
issues.push(createIssue(context, 'empty-input-id', 'A graph input mapping has an empty input ID.'));
|
|
286
|
+
continue;
|
|
287
|
+
}
|
|
288
|
+
if (seenIds.has(binding.inputKey)) {
|
|
289
|
+
issues.push(createIssue(context, 'duplicate-input-binding', `Graph input "${binding.inputKey}" is mapped more than once.`, binding.inputKey));
|
|
290
|
+
continue;
|
|
291
|
+
}
|
|
292
|
+
seenIds.add(binding.inputKey);
|
|
293
|
+
if (!boundaryIds.has(binding.inputKey)) {
|
|
294
|
+
issues.push(createIssue(context, 'stale-input-binding', `Graph input "${binding.inputKey}" no longer exists.`, binding.inputKey));
|
|
295
|
+
}
|
|
296
|
+
const legacyBinding = component.action.inputs?.[binding.inputKey];
|
|
297
|
+
const hasEmptyStateKey = component.action.inputMappings
|
|
298
|
+
? !binding.stateKey.trim()
|
|
299
|
+
: legacyBinding?.type === 'state' && !legacyBinding.key.trim();
|
|
300
|
+
if (hasEmptyStateKey) {
|
|
301
|
+
issues.push(createIssue(context, 'empty-input-state-key', `Graph input "${binding.inputKey}" has no data key to send.`, binding.inputKey));
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
return issues;
|
|
305
|
+
}
|
|
306
|
+
function validateOutputBindings(context, component, boundary) {
|
|
307
|
+
const issues = [];
|
|
308
|
+
const boundaryIds = new Set(boundary.outputs.map((output) => output.id));
|
|
309
|
+
const stateKeys = new Set();
|
|
310
|
+
for (const binding of getUiGraphActionOutputBindings(component.action)) {
|
|
311
|
+
const stateKey = binding.stateKey.trim();
|
|
312
|
+
if (!stateKey) {
|
|
313
|
+
issues.push(createIssue(context, 'empty-output-state-key', `Graph output "${binding.outputKey ?? 'All outputs'}" has no data key to save to.`, binding.outputKey));
|
|
314
|
+
}
|
|
315
|
+
else if (stateKeys.has(stateKey)) {
|
|
316
|
+
issues.push(createIssue(context, 'duplicate-output-state-key', `Data key "${stateKey}" is written by more than one graph output mapping.`, binding.outputKey));
|
|
317
|
+
}
|
|
318
|
+
else {
|
|
319
|
+
stateKeys.add(stateKey);
|
|
320
|
+
}
|
|
321
|
+
if (binding.outputKey && !boundaryIds.has(binding.outputKey)) {
|
|
322
|
+
issues.push(createIssue(context, 'stale-output-binding', `Graph output "${binding.outputKey}" no longer exists.`, binding.outputKey));
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
return issues;
|
|
326
|
+
}
|
|
327
|
+
function createIssue(context, code, message, portId) {
|
|
328
|
+
return { ...context, code, message, portId, severity: 'error' };
|
|
329
|
+
}
|
|
330
|
+
function alignInputMappings(boundary, bindings, renames, addMissing) {
|
|
331
|
+
const findBinding = createBindingLookup(bindings, (binding) => binding.inputKey, renames);
|
|
332
|
+
return boundary.inputs.flatMap((input) => {
|
|
333
|
+
const binding = findBinding(input.id);
|
|
334
|
+
return binding || addMissing ? [{ inputKey: input.id, stateKey: binding?.stateKey || input.id }] : [];
|
|
335
|
+
});
|
|
336
|
+
}
|
|
337
|
+
function reconcileChatAdditionalInputMappings(boundary, bindings, renames) {
|
|
338
|
+
return bindings.map((binding) => {
|
|
339
|
+
if (!binding.inputKey.trim()) {
|
|
340
|
+
return binding;
|
|
341
|
+
}
|
|
342
|
+
const inputKey = reconcileBoundaryId(binding.inputKey, boundary.inputs, renames) ?? '';
|
|
343
|
+
return inputKey === binding.inputKey ? binding : { ...binding, inputKey };
|
|
344
|
+
});
|
|
345
|
+
}
|
|
346
|
+
function alignLegacyInputs(boundary, bindings, renames, addMissing) {
|
|
347
|
+
const findBinding = createBindingLookup(Object.entries(bindings), ([id]) => id, renames);
|
|
348
|
+
return Object.fromEntries(boundary.inputs.flatMap((input) => {
|
|
349
|
+
const binding = findBinding(input.id)?.[1];
|
|
350
|
+
return binding || addMissing ? [[input.id, binding ?? { type: 'state', key: input.id }]] : [];
|
|
351
|
+
}));
|
|
352
|
+
}
|
|
353
|
+
function alignOutputMappings(boundary, bindings, renames, addMissing) {
|
|
354
|
+
const allOutputBindings = bindings
|
|
355
|
+
.filter((binding) => !binding.outputKey)
|
|
356
|
+
.map((binding) => ({ ...binding, outputKey: undefined }));
|
|
357
|
+
const findBindings = createBindingListLookup(bindings, (binding) => binding.outputKey, renames);
|
|
358
|
+
const matchedBindings = boundary.outputs.map((output) => findBindings(output.id));
|
|
359
|
+
const usedStateKeys = new Set([...allOutputBindings, ...matchedBindings.flat()].map((binding) => binding.stateKey).filter(Boolean));
|
|
360
|
+
const specificBindings = boundary.outputs.flatMap((output, index) => {
|
|
361
|
+
const matches = matchedBindings[index];
|
|
362
|
+
if (matches.length > 0) {
|
|
363
|
+
return matches.map((binding) => {
|
|
364
|
+
const stateKey = binding.stateKey || createUniqueStateKey(output.id, usedStateKeys);
|
|
365
|
+
usedStateKeys.add(stateKey);
|
|
366
|
+
return { outputKey: output.id, stateKey };
|
|
367
|
+
});
|
|
368
|
+
}
|
|
369
|
+
return allOutputBindings.length > 0 || !addMissing
|
|
370
|
+
? []
|
|
371
|
+
: [{ outputKey: output.id, stateKey: createUniqueStateKey(output.id, usedStateKeys) }];
|
|
372
|
+
});
|
|
373
|
+
return [...allOutputBindings, ...specificBindings];
|
|
374
|
+
}
|
|
375
|
+
function createUniqueStateKey(preferredKey, usedKeys) {
|
|
376
|
+
let key = preferredKey;
|
|
377
|
+
let suffix = 2;
|
|
378
|
+
while (usedKeys.has(key)) {
|
|
379
|
+
key = `${preferredKey}-${suffix}`;
|
|
380
|
+
suffix += 1;
|
|
381
|
+
}
|
|
382
|
+
usedKeys.add(key);
|
|
383
|
+
return key;
|
|
384
|
+
}
|
|
385
|
+
function createBindingLookup(bindings, getId, renames) {
|
|
386
|
+
const bindingsById = new Map(bindings.map((binding) => [getId(binding), binding]));
|
|
387
|
+
const oldIdsByNewId = new Map(Object.entries(renames ?? {}).map(([oldId, newId]) => [newId, oldId]));
|
|
388
|
+
return (newId) => bindingsById.get(newId) ?? bindingsById.get(oldIdsByNewId.get(newId));
|
|
389
|
+
}
|
|
390
|
+
function reconcileBoundaryId(currentId, ports, renames) {
|
|
391
|
+
if (!currentId) {
|
|
392
|
+
return undefined;
|
|
393
|
+
}
|
|
394
|
+
const availableIds = new Set(ports.map((port) => port.id));
|
|
395
|
+
if (availableIds.has(currentId)) {
|
|
396
|
+
return currentId;
|
|
397
|
+
}
|
|
398
|
+
const renamedId = renames?.[currentId];
|
|
399
|
+
return renamedId && availableIds.has(renamedId) ? renamedId : undefined;
|
|
400
|
+
}
|
|
401
|
+
function createBindingListLookup(bindings, getId, renames) {
|
|
402
|
+
const bindingsById = new Map();
|
|
403
|
+
for (const binding of bindings) {
|
|
404
|
+
const id = getId(binding);
|
|
405
|
+
const existing = bindingsById.get(id);
|
|
406
|
+
if (existing) {
|
|
407
|
+
existing.push(binding);
|
|
408
|
+
}
|
|
409
|
+
else {
|
|
410
|
+
bindingsById.set(id, [binding]);
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
const oldIdsByNewId = new Map(Object.entries(renames ?? {}).map(([oldId, newId]) => [newId, oldId]));
|
|
414
|
+
return (newId) => bindingsById.get(newId) ?? bindingsById.get(oldIdsByNewId.get(newId)) ?? [];
|
|
415
|
+
}
|
|
416
|
+
function hasAmbiguousUiGraphButtonBindings(action) {
|
|
417
|
+
if (action.inputMappings) {
|
|
418
|
+
const inputIds = new Set();
|
|
419
|
+
for (const binding of action.inputMappings) {
|
|
420
|
+
if (!binding.inputKey.trim() || !binding.stateKey.trim() || inputIds.has(binding.inputKey)) {
|
|
421
|
+
return true;
|
|
422
|
+
}
|
|
423
|
+
inputIds.add(binding.inputKey);
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
else {
|
|
427
|
+
for (const [inputId, binding] of Object.entries(action.inputs ?? {})) {
|
|
428
|
+
if (!inputId.trim() || (binding.type === 'state' && !binding.key.trim())) {
|
|
429
|
+
return true;
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
const outputStateKeys = new Set();
|
|
434
|
+
for (const binding of getUiGraphActionOutputBindings(action)) {
|
|
435
|
+
const stateKey = binding.stateKey.trim();
|
|
436
|
+
if (!stateKey || outputStateKeys.has(stateKey)) {
|
|
437
|
+
return true;
|
|
438
|
+
}
|
|
439
|
+
outputStateKeys.add(stateKey);
|
|
440
|
+
}
|
|
441
|
+
return false;
|
|
442
|
+
}
|
|
443
|
+
function hasCompleteInputCoverage(action, boundary) {
|
|
444
|
+
if (!boundary) {
|
|
445
|
+
return false;
|
|
446
|
+
}
|
|
447
|
+
const mappedIds = new Set(getUiGraphActionInputBindings(action).map((binding) => binding.inputKey));
|
|
448
|
+
return boundary.inputs.every((input) => mappedIds.has(input.id));
|
|
449
|
+
}
|
|
450
|
+
function hasCompleteOutputCoverage(action, boundary) {
|
|
451
|
+
if (!boundary) {
|
|
452
|
+
return false;
|
|
453
|
+
}
|
|
454
|
+
const bindings = getUiGraphActionOutputBindings(action);
|
|
455
|
+
if (bindings.some((binding) => !binding.outputKey)) {
|
|
456
|
+
return true;
|
|
457
|
+
}
|
|
458
|
+
const mappedIds = new Set(bindings.map((binding) => binding.outputKey));
|
|
459
|
+
return boundary.outputs.every((output) => mappedIds.has(output.id));
|
|
460
|
+
}
|
|
461
|
+
function getGraphBoundaryChange(previousProject, nextProject, graphId) {
|
|
462
|
+
const boundary = getGraphBoundary(nextProject, graphId);
|
|
463
|
+
if (!boundary) {
|
|
464
|
+
return undefined;
|
|
465
|
+
}
|
|
466
|
+
const previousBoundary = getGraphBoundary(previousProject, graphId);
|
|
467
|
+
const inputsChanged = !previousBoundary ||
|
|
468
|
+
!areStringArraysEqual(previousBoundary.inputs.map((input) => input.id), boundary.inputs.map((input) => input.id));
|
|
469
|
+
const outputsChanged = !previousBoundary ||
|
|
470
|
+
!areStringArraysEqual(previousBoundary.outputs.map((output) => output.id), boundary.outputs.map((output) => output.id));
|
|
471
|
+
if (!inputsChanged && !outputsChanged) {
|
|
472
|
+
return undefined;
|
|
473
|
+
}
|
|
474
|
+
return {
|
|
475
|
+
boundary,
|
|
476
|
+
inputsChanged,
|
|
477
|
+
outputsChanged,
|
|
478
|
+
previousBoundary,
|
|
479
|
+
renames: getSafeGraphBoundaryRenames(previousProject.graphs[graphId], nextProject.graphs[graphId]),
|
|
480
|
+
};
|
|
481
|
+
}
|
|
482
|
+
function getSafeGraphBoundaryRenames(previousGraph, nextGraph) {
|
|
483
|
+
if (!previousGraph || !nextGraph) {
|
|
484
|
+
return {};
|
|
485
|
+
}
|
|
486
|
+
return {
|
|
487
|
+
inputIds: getSafeNodeIdRenames(previousGraph, nextGraph, 'graphInput'),
|
|
488
|
+
outputIds: getSafeNodeIdRenames(previousGraph, nextGraph, 'graphOutput'),
|
|
489
|
+
};
|
|
490
|
+
}
|
|
491
|
+
function getSafeNodeIdRenames(previousGraph, nextGraph, nodeType) {
|
|
492
|
+
const previousIdCounts = getBoundaryNodeDataIdCounts(previousGraph, nodeType);
|
|
493
|
+
const nextIdCounts = getBoundaryNodeDataIdCounts(nextGraph, nodeType);
|
|
494
|
+
const previousNodes = new Map(previousGraph.nodes.filter((node) => node.type === nodeType).map((node) => [node.id, node]));
|
|
495
|
+
const renames = {};
|
|
496
|
+
for (const nextNode of nextGraph.nodes) {
|
|
497
|
+
const oldId = getBoundaryNodeDataId(previousNodes.get(nextNode.id));
|
|
498
|
+
const newId = nextNode.type === nodeType ? getBoundaryNodeDataId(nextNode) : undefined;
|
|
499
|
+
if (!oldId ||
|
|
500
|
+
!newId ||
|
|
501
|
+
oldId === newId ||
|
|
502
|
+
nextIdCounts.has(oldId) ||
|
|
503
|
+
previousIdCounts.has(newId) ||
|
|
504
|
+
previousIdCounts.get(oldId) !== 1 ||
|
|
505
|
+
nextIdCounts.get(newId) !== 1) {
|
|
506
|
+
continue;
|
|
507
|
+
}
|
|
508
|
+
renames[oldId] = newId;
|
|
509
|
+
}
|
|
510
|
+
return renames;
|
|
511
|
+
}
|
|
512
|
+
function getBoundaryNodeDataIdCounts(graph, nodeType) {
|
|
513
|
+
const counts = new Map();
|
|
514
|
+
for (const node of graph.nodes) {
|
|
515
|
+
if (node.type === nodeType) {
|
|
516
|
+
const id = getBoundaryNodeDataId(node);
|
|
517
|
+
if (id) {
|
|
518
|
+
counts.set(id, (counts.get(id) ?? 0) + 1);
|
|
519
|
+
}
|
|
520
|
+
}
|
|
521
|
+
}
|
|
522
|
+
return counts;
|
|
523
|
+
}
|
|
524
|
+
function getBoundaryNodeDataId(node) {
|
|
525
|
+
const id = node?.data?.id;
|
|
526
|
+
return typeof id === 'string' ? id : undefined;
|
|
527
|
+
}
|
|
528
|
+
function areStringArraysEqual(left, right) {
|
|
529
|
+
return left.length === right.length && left.every((value, index) => value === right[index]);
|
|
530
|
+
}
|