@valerypopoff/rivet2-core 2.0.10 → 2.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/bundle.cjs +1762 -274
- package/dist/cjs/bundle.cjs.map +4 -4
- package/dist/esm/api/createProcessor.js +1 -0
- package/dist/esm/exports.js +4 -0
- package/dist/esm/model/GraphBoundaryCache.js +36 -4
- package/dist/esm/model/GraphBoundaryEffects.js +32 -0
- package/dist/esm/model/GraphPreprocessor.js +6 -4
- package/dist/esm/model/GraphProcessor.js +344 -118
- package/dist/esm/model/NodeGraph.js +1 -1
- package/dist/esm/model/NodePrefabResolver.js +57 -0
- package/dist/esm/model/Nodes.js +4 -1
- package/dist/esm/model/ProcessContextBuilder.js +3 -1
- package/dist/esm/model/UiGraph.js +113 -0
- package/dist/esm/model/UiGraphRendererStyles.js +208 -0
- package/dist/esm/model/chat/openAIChatRuntime.js +0 -1
- package/dist/esm/model/chat-v2/aiSdkBridge.js +100 -2
- package/dist/esm/model/chat-v2/chatV2Pipeline.js +35 -31
- package/dist/esm/model/chat-v2/chatV2Shared.js +1 -1
- package/dist/esm/model/chat-v2/providerOptions.js +5 -1
- package/dist/esm/model/nodes/CoalesceNode.js +1 -1
- package/dist/esm/model/nodes/CompareNode.js +15 -14
- package/dist/esm/model/nodes/GraphOutputNode.js +1 -0
- package/dist/esm/model/nodes/LLMChatV2Node.js +62 -17
- package/dist/esm/model/nodes/NodePrefabInstanceNode.js +39 -0
- package/dist/esm/model/nodes/PromptNode.js +2 -1
- package/dist/esm/model/nodes/ReferencedGraphAliasNode.js +49 -7
- package/dist/esm/model/nodes/SubGraphNode.js +42 -5
- package/dist/esm/utils/index.js +2 -0
- package/dist/esm/utils/jsonValueToDataValue.js +48 -0
- package/dist/esm/utils/projectComparison/connections.js +57 -0
- package/dist/esm/utils/projectComparison/graphs.js +60 -0
- package/dist/esm/utils/projectComparison/nodes.js +53 -0
- package/dist/esm/utils/projectComparison/summaries.js +60 -0
- package/dist/esm/utils/projectComparison/values.js +40 -0
- package/dist/esm/utils/projectComparison.js +73 -0
- package/dist/esm/utils/serialization/serializationHelpers.js +24 -3
- package/dist/esm/utils/serialization/serialization_v4.js +30 -0
- package/dist/types/api/createProcessor.d.ts +2 -1
- package/dist/types/exports.d.ts +5 -0
- package/dist/types/model/EditorDefinition.d.ts +1 -0
- package/dist/types/model/ExecutorProtocol.d.ts +13 -3
- package/dist/types/model/GraphBoundaryCache.d.ts +9 -3
- package/dist/types/model/GraphBoundaryEffects.d.ts +9 -0
- package/dist/types/model/GraphProcessor.d.ts +20 -0
- package/dist/types/model/NodeBase.d.ts +5 -0
- package/dist/types/model/NodeBodySpec.d.ts +1 -0
- package/dist/types/model/NodePrefabResolver.d.ts +13 -0
- package/dist/types/model/Nodes.d.ts +5 -4
- package/dist/types/model/ProcessContext.d.ts +5 -1
- package/dist/types/model/ProcessContextBuilder.d.ts +4 -2
- package/dist/types/model/Project.d.ts +9 -0
- package/dist/types/model/UiGraph.d.ts +84 -0
- package/dist/types/model/UiGraphRendererStyles.d.ts +2 -0
- package/dist/types/model/chat-v2/aiSdkBridge.d.ts +1 -0
- package/dist/types/model/chat-v2/chatV2Types.d.ts +23 -1
- package/dist/types/model/nodes/CompareNode.d.ts +4 -0
- package/dist/types/model/nodes/LLMChatV2Node.d.ts +2 -1
- package/dist/types/model/nodes/NodePrefabInstanceNode.d.ts +13 -0
- package/dist/types/model/nodes/SubGraphNode.d.ts +2 -0
- package/dist/types/utils/index.d.ts +2 -0
- package/dist/types/utils/jsonValueToDataValue.d.ts +2 -0
- package/dist/types/utils/projectComparison/connections.d.ts +5 -0
- package/dist/types/utils/projectComparison/graphs.d.ts +3 -0
- package/dist/types/utils/projectComparison/nodes.d.ts +5 -0
- package/dist/types/utils/projectComparison/summaries.d.ts +4 -0
- package/dist/types/utils/projectComparison/values.d.ts +5 -0
- package/dist/types/utils/projectComparison.d.ts +77 -0
- package/package.json +1 -1
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
export const NODE_PREFAB_INSTANCE_TYPE = 'nodePrefabInstance';
|
|
2
|
+
const BLOCKED_NODE_PREFAB_SOURCE_TYPES = new Set([
|
|
3
|
+
'comment',
|
|
4
|
+
'graphInput',
|
|
5
|
+
'graphOutput',
|
|
6
|
+
'referencedGraphAlias',
|
|
7
|
+
NODE_PREFAB_INSTANCE_TYPE,
|
|
8
|
+
]);
|
|
9
|
+
export function isNodePrefabInstanceNode(node) {
|
|
10
|
+
return node?.type === NODE_PREFAB_INSTANCE_TYPE;
|
|
11
|
+
}
|
|
12
|
+
export function canUseNodeAsPrefabSource(node) {
|
|
13
|
+
return !BLOCKED_NODE_PREFAB_SOURCE_TYPES.has(node.type);
|
|
14
|
+
}
|
|
15
|
+
export function getNodePrefabInstancePrefabId(node) {
|
|
16
|
+
if (!isNodePrefabInstanceNode(node)) {
|
|
17
|
+
return undefined;
|
|
18
|
+
}
|
|
19
|
+
const prefabId = node.data?.prefabId;
|
|
20
|
+
return typeof prefabId === 'string' && prefabId.length > 0 ? prefabId : undefined;
|
|
21
|
+
}
|
|
22
|
+
export function resolveNodePrefabInstance(project, node) {
|
|
23
|
+
const prefabId = getNodePrefabInstancePrefabId(node);
|
|
24
|
+
if (!prefabId) {
|
|
25
|
+
return node;
|
|
26
|
+
}
|
|
27
|
+
const sourceNode = project.nodePrefabs?.[prefabId]?.sourceNode;
|
|
28
|
+
if (!sourceNode || !canUseNodeAsPrefabSource(sourceNode)) {
|
|
29
|
+
return {
|
|
30
|
+
...node,
|
|
31
|
+
title: `Missing library node (${prefabId})`,
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
return {
|
|
35
|
+
...structuredClone(sourceNode),
|
|
36
|
+
id: node.id,
|
|
37
|
+
visualData: {
|
|
38
|
+
...structuredClone(sourceNode.visualData),
|
|
39
|
+
x: node.visualData.x,
|
|
40
|
+
y: node.visualData.y,
|
|
41
|
+
width: node.visualData.width ?? sourceNode.visualData.width,
|
|
42
|
+
zIndex: node.visualData.zIndex,
|
|
43
|
+
},
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
export function resolveNodePrefabInstances(project, nodes) {
|
|
47
|
+
return nodes.map((node) => resolveNodePrefabInstance(project, node));
|
|
48
|
+
}
|
|
49
|
+
export function getNodePrefabDisplayName(project, prefabId) {
|
|
50
|
+
if (!prefabId) {
|
|
51
|
+
return 'Missing library node';
|
|
52
|
+
}
|
|
53
|
+
const sourceNode = project.nodePrefabs?.[prefabId]?.sourceNode;
|
|
54
|
+
return sourceNode && canUseNodeAsPrefabSource(sourceNode)
|
|
55
|
+
? sourceNode.title || 'Untitled library node'
|
|
56
|
+
: 'Missing library node';
|
|
57
|
+
}
|
package/dist/esm/model/Nodes.js
CHANGED
|
@@ -178,6 +178,8 @@ import { mcpGetPromptNode } from './nodes/MCPGetPromptNode.js';
|
|
|
178
178
|
export * from './nodes/MCPGetPromptNode.js';
|
|
179
179
|
import { referencedGraphAliasNode } from './nodes/ReferencedGraphAliasNode.js';
|
|
180
180
|
export * from './nodes/ReferencedGraphAliasNode.js';
|
|
181
|
+
import { nodePrefabInstanceNode } from './nodes/NodePrefabInstanceNode.js';
|
|
182
|
+
export * from './nodes/NodePrefabInstanceNode.js';
|
|
181
183
|
export const registerBuiltInNodes = (registry) => {
|
|
182
184
|
return registry
|
|
183
185
|
.register(toYamlNode)
|
|
@@ -268,7 +270,8 @@ export const registerBuiltInNodes = (registry) => {
|
|
|
268
270
|
.register(mcpDiscoveryNode)
|
|
269
271
|
.register(mcpToolCallNode)
|
|
270
272
|
.register(mcpGetPromptNode)
|
|
271
|
-
.register(referencedGraphAliasNode)
|
|
273
|
+
.register(referencedGraphAliasNode)
|
|
274
|
+
.register(nodePrefabInstanceNode);
|
|
272
275
|
};
|
|
273
276
|
let globalRivetNodeRegistry = registerBuiltInNodes(new NodeRegistration());
|
|
274
277
|
export { globalRivetNodeRegistry };
|
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
export function buildNodeProcessContext(options) {
|
|
2
|
-
const { attachedData, base, createSubProcessor, execution, externalFunctions, getPluginConfig, node, nodeAbortController, onPartialOutputs, processId, requestUserInput, setGlobal, waitEvent, } = options;
|
|
2
|
+
const { attachedData, activeOutputPortIds, base, createSubProcessor, execution, externalFunctions, getPluginConfig, isDirectRunTarget, node, nodeAbortController, onPartialOutputs, processId, requestUserInput, setGlobal, waitEvent, } = options;
|
|
3
3
|
return {
|
|
4
4
|
...base,
|
|
5
5
|
node,
|
|
6
6
|
attachedData,
|
|
7
|
+
activeOutputPortIds,
|
|
8
|
+
isDirectRunTarget,
|
|
7
9
|
waitEvent,
|
|
8
10
|
externalFunctions: { ...externalFunctions },
|
|
9
11
|
onPartialOutputs,
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import { newId } from '../utils/newId.js';
|
|
2
|
+
export function createDefaultUiGraph(options = {}) {
|
|
3
|
+
const promptStateKey = 'input';
|
|
4
|
+
const resultStateKey = 'result';
|
|
5
|
+
return {
|
|
6
|
+
id: newId(),
|
|
7
|
+
name: options.name ?? 'Untitled web app',
|
|
8
|
+
components: [
|
|
9
|
+
{
|
|
10
|
+
id: newId(),
|
|
11
|
+
type: 'markdown',
|
|
12
|
+
markdown: '## New Rivet web app\n\nEnter a value and run a graph.',
|
|
13
|
+
},
|
|
14
|
+
{
|
|
15
|
+
id: newId(),
|
|
16
|
+
type: 'textarea',
|
|
17
|
+
label: 'Input',
|
|
18
|
+
stateKey: promptStateKey,
|
|
19
|
+
placeholder: 'Type something...',
|
|
20
|
+
},
|
|
21
|
+
{
|
|
22
|
+
id: newId(),
|
|
23
|
+
type: 'button',
|
|
24
|
+
label: 'Run graph',
|
|
25
|
+
action: {
|
|
26
|
+
type: 'runGraph',
|
|
27
|
+
graphId: options.graphId,
|
|
28
|
+
inputMappings: [{ inputKey: 'input', stateKey: promptStateKey }],
|
|
29
|
+
outputs: [{ stateKey: resultStateKey }],
|
|
30
|
+
},
|
|
31
|
+
},
|
|
32
|
+
{
|
|
33
|
+
id: newId(),
|
|
34
|
+
type: 'output',
|
|
35
|
+
label: 'Result',
|
|
36
|
+
stateKey: resultStateKey,
|
|
37
|
+
renderAs: 'json',
|
|
38
|
+
},
|
|
39
|
+
],
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
export function getUiGraphInitialState(uiGraph) {
|
|
43
|
+
const state = {};
|
|
44
|
+
for (const component of uiGraph.components) {
|
|
45
|
+
if (component.type === 'input' || component.type === 'textarea') {
|
|
46
|
+
state[component.stateKey] = component.defaultValue ?? '';
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
return state;
|
|
50
|
+
}
|
|
51
|
+
export function getUiGraphActionComponent(uiGraph, componentId) {
|
|
52
|
+
return uiGraph.components.find((component) => component.id === componentId && component.type === 'button');
|
|
53
|
+
}
|
|
54
|
+
export function resolveUiGraphActionInputs(action, state) {
|
|
55
|
+
const inputs = {};
|
|
56
|
+
if (action.inputMappings) {
|
|
57
|
+
for (const binding of action.inputMappings) {
|
|
58
|
+
if (binding.inputKey) {
|
|
59
|
+
inputs[binding.inputKey] = state[binding.stateKey];
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
return inputs;
|
|
63
|
+
}
|
|
64
|
+
for (const [inputId, binding] of Object.entries(action.inputs ?? {})) {
|
|
65
|
+
inputs[inputId] = binding.type === 'state' ? state[binding.key] : binding.value;
|
|
66
|
+
}
|
|
67
|
+
return inputs;
|
|
68
|
+
}
|
|
69
|
+
export function resolveUiGraphActionOutputStateValue(action, outputs) {
|
|
70
|
+
if (!action.outputKey) {
|
|
71
|
+
return outputs;
|
|
72
|
+
}
|
|
73
|
+
if (!Object.prototype.hasOwnProperty.call(outputs, action.outputKey)) {
|
|
74
|
+
throw new Error(`Graph output "${action.outputKey}" was not returned by the target graph.`);
|
|
75
|
+
}
|
|
76
|
+
return outputs[action.outputKey]?.value;
|
|
77
|
+
}
|
|
78
|
+
export function resolveUiGraphActionOutputStatePatch(action, outputs) {
|
|
79
|
+
const bindings = getUiGraphActionOutputBindings(action);
|
|
80
|
+
const statePatch = {};
|
|
81
|
+
for (const binding of bindings) {
|
|
82
|
+
const stateKey = binding.stateKey.trim();
|
|
83
|
+
if (!stateKey) {
|
|
84
|
+
continue;
|
|
85
|
+
}
|
|
86
|
+
statePatch[stateKey] = resolveUiGraphOutputBindingValue(binding, outputs);
|
|
87
|
+
}
|
|
88
|
+
return statePatch;
|
|
89
|
+
}
|
|
90
|
+
export function getUiGraphActionOutputBindings(action) {
|
|
91
|
+
if (action.outputs) {
|
|
92
|
+
return action.outputs;
|
|
93
|
+
}
|
|
94
|
+
return action.outputStateKey ? [{ outputKey: action.outputKey, stateKey: action.outputStateKey }] : [];
|
|
95
|
+
}
|
|
96
|
+
export function getUiGraphActionInputBindings(action) {
|
|
97
|
+
if (action.inputMappings) {
|
|
98
|
+
return action.inputMappings;
|
|
99
|
+
}
|
|
100
|
+
return Object.entries(action.inputs ?? {}).map(([inputKey, binding]) => ({
|
|
101
|
+
inputKey,
|
|
102
|
+
stateKey: binding.type === 'state' ? binding.key : '',
|
|
103
|
+
}));
|
|
104
|
+
}
|
|
105
|
+
function resolveUiGraphOutputBindingValue(binding, outputs) {
|
|
106
|
+
if (!binding.outputKey) {
|
|
107
|
+
return outputs;
|
|
108
|
+
}
|
|
109
|
+
if (!Object.prototype.hasOwnProperty.call(outputs, binding.outputKey)) {
|
|
110
|
+
throw new Error(`Graph output "${binding.outputKey}" was not returned by the target graph.`);
|
|
111
|
+
}
|
|
112
|
+
return outputs[binding.outputKey]?.value;
|
|
113
|
+
}
|
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
export const RIVET_WEB_APP_RENDERER_CSS = `
|
|
2
|
+
.rivet-web-app-root {
|
|
3
|
+
--rivet-web-app-background: var(--grey-dark-colorish, #1c222b);
|
|
4
|
+
--rivet-web-app-foreground: var(--foreground, #ffffff);
|
|
5
|
+
--rivet-web-app-card-background: color-mix(in srgb, var(--modal-surface-bg, #252b34) 88%, var(--foreground, #ffffff) 4%);
|
|
6
|
+
--rivet-web-app-card-border: var(--foldable-section-border, rgba(255, 255, 255, 0.04));
|
|
7
|
+
--rivet-web-app-control-background: var(--form-control-bg, #20252d);
|
|
8
|
+
--rivet-web-app-control-border: var(--form-control-border, rgba(255, 255, 255, 0.14));
|
|
9
|
+
--rivet-web-app-button-radius: var(--rivet-web-app-host-button-radius, 6px);
|
|
10
|
+
--rivet-web-app-button-background: var(--success, #3ba85b);
|
|
11
|
+
--rivet-web-app-button-foreground: var(--grey-lightest, #ffffff);
|
|
12
|
+
--rivet-web-app-output-title: var(--rivet-web-app-foreground, #ffffff);
|
|
13
|
+
--rivet-web-app-error-color: var(--error, #ff6b5f);
|
|
14
|
+
--rivet-web-app-font-size: var(--rivet-web-app-host-font-size, 15px);
|
|
15
|
+
box-sizing: border-box;
|
|
16
|
+
height: 100%;
|
|
17
|
+
background: var(--rivet-web-app-background);
|
|
18
|
+
color: var(--rivet-web-app-foreground);
|
|
19
|
+
overflow: auto;
|
|
20
|
+
font-family: Inter, system-ui, sans-serif;
|
|
21
|
+
font-size: var(--rivet-web-app-font-size);
|
|
22
|
+
line-height: 1.4;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
.rivet-web-app-root *,
|
|
26
|
+
.rivet-web-app-root *::before,
|
|
27
|
+
.rivet-web-app-root *::after {
|
|
28
|
+
box-sizing: border-box;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
.rivet-web-app-surface {
|
|
32
|
+
display: grid;
|
|
33
|
+
gap: 16px;
|
|
34
|
+
margin: 0 auto;
|
|
35
|
+
max-width: 760px;
|
|
36
|
+
padding: 48px 20px;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
.rivet-web-app-card,
|
|
40
|
+
.rivet-web-app-field {
|
|
41
|
+
border: 1px solid var(--rivet-web-app-card-border);
|
|
42
|
+
border-radius: 10px;
|
|
43
|
+
background: var(--rivet-web-app-card-background);
|
|
44
|
+
padding: 16px;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
.rivet-web-app-component-frame {
|
|
48
|
+
border-radius: 12px;
|
|
49
|
+
margin: -5px;
|
|
50
|
+
padding: 4px;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
.rivet-web-app-component-frame.active {
|
|
54
|
+
background: color-mix(in srgb, var(--modal-surface-bg, #252b34) 75%, var(--primary, #ff9e2c) 16%);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
.rivet-web-app-field {
|
|
58
|
+
display: grid;
|
|
59
|
+
gap: 8px;
|
|
60
|
+
color: var(--rivet-web-app-foreground);
|
|
61
|
+
font-size: inherit;
|
|
62
|
+
font-weight: 600;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
.rivet-web-app-field input,
|
|
66
|
+
.rivet-web-app-field textarea {
|
|
67
|
+
appearance: none;
|
|
68
|
+
width: 100%;
|
|
69
|
+
box-sizing: border-box;
|
|
70
|
+
border: 1px solid var(--rivet-web-app-control-border);
|
|
71
|
+
border-radius: 8px;
|
|
72
|
+
background: var(--rivet-web-app-control-background);
|
|
73
|
+
color: var(--rivet-web-app-foreground);
|
|
74
|
+
font: inherit;
|
|
75
|
+
font-weight: 400;
|
|
76
|
+
padding: 10px 12px;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
.rivet-web-app-field textarea {
|
|
80
|
+
min-height: 110px;
|
|
81
|
+
resize: vertical;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
.rivet-web-app-button {
|
|
85
|
+
width: fit-content;
|
|
86
|
+
border: 0;
|
|
87
|
+
border-radius: var(--rivet-web-app-button-radius);
|
|
88
|
+
background: var(--rivet-web-app-button-background);
|
|
89
|
+
color: var(--rivet-web-app-button-foreground);
|
|
90
|
+
cursor: pointer;
|
|
91
|
+
font: inherit;
|
|
92
|
+
font-weight: 700;
|
|
93
|
+
padding: 10px 16px;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
.rivet-web-app-button:disabled {
|
|
97
|
+
cursor: wait;
|
|
98
|
+
opacity: 0.72;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
.rivet-web-app-output {
|
|
102
|
+
display: grid;
|
|
103
|
+
gap: 8px;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
.rivet-web-app-output-title {
|
|
107
|
+
color: var(--rivet-web-app-output-title);
|
|
108
|
+
font-weight: 700;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
.rivet-web-app-output pre {
|
|
112
|
+
margin: 0;
|
|
113
|
+
background: transparent;
|
|
114
|
+
border-radius: 0;
|
|
115
|
+
font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace;
|
|
116
|
+
font-size: inherit;
|
|
117
|
+
padding: 0;
|
|
118
|
+
white-space: pre-wrap;
|
|
119
|
+
word-break: break-word;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
.rivet-web-app-markdown,
|
|
123
|
+
.rivet-web-app-output-markdown {
|
|
124
|
+
word-break: break-word;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
.rivet-web-app-markdown.markdown-body {
|
|
128
|
+
background: var(--rivet-web-app-card-background);
|
|
129
|
+
font-family: inherit;
|
|
130
|
+
min-width: 0;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
.rivet-web-app-output-markdown.markdown-body {
|
|
134
|
+
background: transparent;
|
|
135
|
+
font-family: inherit;
|
|
136
|
+
min-width: 0;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
.rivet-web-app-markdown h1,
|
|
140
|
+
.rivet-web-app-output-markdown h1 {
|
|
141
|
+
margin: 0 0 12px;
|
|
142
|
+
padding-bottom: 8px;
|
|
143
|
+
border-bottom: 1px solid var(--rivet-web-app-card-border);
|
|
144
|
+
font-size: 24px;
|
|
145
|
+
line-height: 1.22;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
.rivet-web-app-markdown h2,
|
|
149
|
+
.rivet-web-app-output-markdown h2 {
|
|
150
|
+
margin: 0 0 10px;
|
|
151
|
+
font-size: 20px;
|
|
152
|
+
line-height: 1.25;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
.rivet-web-app-markdown p,
|
|
156
|
+
.rivet-web-app-output-markdown p,
|
|
157
|
+
.rivet-web-app-markdown ul,
|
|
158
|
+
.rivet-web-app-output-markdown ul {
|
|
159
|
+
margin: 0 0 12px;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
.rivet-web-app-markdown.markdown-body code,
|
|
163
|
+
.rivet-web-app-output-markdown.markdown-body code {
|
|
164
|
+
border-radius: 4px;
|
|
165
|
+
background: color-mix(in srgb, var(--rivet-web-app-control-background) 70%, var(--rivet-web-app-foreground) 6%);
|
|
166
|
+
font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace;
|
|
167
|
+
padding: 1px 4px;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
.rivet-web-app-markdown.markdown-body pre,
|
|
171
|
+
.rivet-web-app-output-markdown.markdown-body pre {
|
|
172
|
+
background: transparent;
|
|
173
|
+
font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace;
|
|
174
|
+
padding: 0;
|
|
175
|
+
white-space: pre-wrap;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
.rivet-web-app-markdown > :first-child,
|
|
179
|
+
.rivet-web-app-output-markdown > :first-child {
|
|
180
|
+
margin-top: 0;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
.rivet-web-app-markdown > :last-child,
|
|
184
|
+
.rivet-web-app-output-markdown > :last-child {
|
|
185
|
+
margin-bottom: 0;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
.rivet-web-app-error {
|
|
189
|
+
color: var(--rivet-web-app-error-color);
|
|
190
|
+
font-weight: 700;
|
|
191
|
+
}
|
|
192
|
+
`;
|
|
193
|
+
export const RIVET_WEB_APP_DOCUMENT_CSS = `
|
|
194
|
+
html,
|
|
195
|
+
body {
|
|
196
|
+
height: 100%;
|
|
197
|
+
margin: 0;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
body {
|
|
201
|
+
min-height: 100vh;
|
|
202
|
+
background: #1c222b;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
#app {
|
|
206
|
+
min-height: 100vh;
|
|
207
|
+
}
|
|
208
|
+
`;
|
|
@@ -153,7 +153,6 @@ export async function applyOpenAIStreamingResponse(params) {
|
|
|
153
153
|
};
|
|
154
154
|
}
|
|
155
155
|
output['cost'] = { type: 'number', value: totalCost };
|
|
156
|
-
output['__hidden_token_count'] = { type: 'number', value: inputTokenCount + outputTokenCount };
|
|
157
156
|
}
|
|
158
157
|
export function handleOpenAIRetryableFailure(params) {
|
|
159
158
|
const { originalError, context } = params;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { streamText } from 'ai';
|
|
1
|
+
import { generateText, NoObjectGeneratedError, streamText } from 'ai';
|
|
2
2
|
import { consumeAiSdkStream } from '../chat/aiSdkStreaming.js';
|
|
3
3
|
import { isChatV2StructuredResponseFormat } from './chatV2ResponseFormat.js';
|
|
4
4
|
function keepPromiseHandled(value) {
|
|
@@ -35,6 +35,9 @@ function defaultStreamExecutor(args) {
|
|
|
35
35
|
: undefined,
|
|
36
36
|
};
|
|
37
37
|
}
|
|
38
|
+
async function defaultGenerateExecutor(args) {
|
|
39
|
+
return (await generateText(args));
|
|
40
|
+
}
|
|
38
41
|
async function resolveOptionalValue(value) {
|
|
39
42
|
return value == null ? undefined : await value;
|
|
40
43
|
}
|
|
@@ -49,6 +52,52 @@ async function resolveOptionalStructuredOutput(value) {
|
|
|
49
52
|
return undefined;
|
|
50
53
|
}
|
|
51
54
|
}
|
|
55
|
+
async function resolveGenerateStructuredOutput(result, responseOutput, finishReason) {
|
|
56
|
+
if (responseOutput == null || (finishReason != null && finishReason !== 'stop')) {
|
|
57
|
+
return undefined;
|
|
58
|
+
}
|
|
59
|
+
try {
|
|
60
|
+
return await resolveOptionalStructuredOutput(result.output);
|
|
61
|
+
}
|
|
62
|
+
catch {
|
|
63
|
+
return undefined;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
function recoverGenerateStructuredOutputError(error, options, toolCalls) {
|
|
67
|
+
if (options.responseOutput == null || !isChatV2StructuredResponseFormat(options.responseFormat)) {
|
|
68
|
+
return undefined;
|
|
69
|
+
}
|
|
70
|
+
if (!NoObjectGeneratedError.isInstance(error)) {
|
|
71
|
+
return undefined;
|
|
72
|
+
}
|
|
73
|
+
if (typeof error.text !== 'string') {
|
|
74
|
+
return undefined;
|
|
75
|
+
}
|
|
76
|
+
const rawText = error.text;
|
|
77
|
+
const responseText = collapseRepeatedStructuredJsonText(rawText);
|
|
78
|
+
return {
|
|
79
|
+
responseText,
|
|
80
|
+
structuredOutput: undefined,
|
|
81
|
+
functionCalls: toStreamedFunctionCalls(toolCalls),
|
|
82
|
+
usage: error.usage,
|
|
83
|
+
reasoning: '',
|
|
84
|
+
finishReason: error.finishReason == null ? undefined : String(error.finishReason),
|
|
85
|
+
providerMetadata: undefined,
|
|
86
|
+
requestStatus: undefined,
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
function attachGenerateStepToolCallCollector(args) {
|
|
90
|
+
const toolCalls = [];
|
|
91
|
+
const previousOnStepFinish = args.onStepFinish;
|
|
92
|
+
args.onStepFinish = async (event) => {
|
|
93
|
+
const stepToolCalls = event.toolCalls;
|
|
94
|
+
if (stepToolCalls != null) {
|
|
95
|
+
toolCalls.push(...stepToolCalls);
|
|
96
|
+
}
|
|
97
|
+
await previousOnStepFinish?.(event);
|
|
98
|
+
};
|
|
99
|
+
return toolCalls;
|
|
100
|
+
}
|
|
52
101
|
function collapseRepeatedStructuredJsonText(responseText) {
|
|
53
102
|
if (responseText.length === 0 || responseText.length % 2 !== 0) {
|
|
54
103
|
return responseText;
|
|
@@ -70,10 +119,11 @@ function collapseRepeatedStructuredJsonText(responseText) {
|
|
|
70
119
|
return responseText;
|
|
71
120
|
}
|
|
72
121
|
}
|
|
73
|
-
|
|
122
|
+
function buildTextArgs(options) {
|
|
74
123
|
const args = {
|
|
75
124
|
model: options.model,
|
|
76
125
|
messages: options.messages,
|
|
126
|
+
maxRetries: 0,
|
|
77
127
|
};
|
|
78
128
|
if (options.tools !== undefined)
|
|
79
129
|
args.tools = options.tools;
|
|
@@ -101,6 +151,26 @@ async function executeStream(options, executor) {
|
|
|
101
151
|
args.toolChoice = options.toolChoice;
|
|
102
152
|
if (options.abortSignal !== undefined)
|
|
103
153
|
args.abortSignal = options.abortSignal;
|
|
154
|
+
return args;
|
|
155
|
+
}
|
|
156
|
+
function toStreamedFunctionCalls(toolCalls) {
|
|
157
|
+
return (toolCalls ?? []).map((toolCall) => ({
|
|
158
|
+
type: 'function',
|
|
159
|
+
id: toolCall.toolCallId,
|
|
160
|
+
name: toolCall.toolName,
|
|
161
|
+
arguments: JSON.stringify(toolCall.input),
|
|
162
|
+
lastParsedArguments: toolCall.input,
|
|
163
|
+
}));
|
|
164
|
+
}
|
|
165
|
+
async function resolveGenerateReasoning(result) {
|
|
166
|
+
const reasoningText = await resolveOptionalValue(result.reasoningText);
|
|
167
|
+
if (reasoningText != null && reasoningText.trim().length > 0) {
|
|
168
|
+
return reasoningText;
|
|
169
|
+
}
|
|
170
|
+
return (await resolveOptionalValue(result.reasoning))?.map((part) => part.text).join('') ?? '';
|
|
171
|
+
}
|
|
172
|
+
async function executeStream(options, executor) {
|
|
173
|
+
const args = buildTextArgs(options);
|
|
104
174
|
const handle = await executor(args);
|
|
105
175
|
markOptionalPromiseHandled(handle.finishReason);
|
|
106
176
|
markOptionalPromiseHandled(handle.output);
|
|
@@ -133,3 +203,31 @@ async function executeStream(options, executor) {
|
|
|
133
203
|
export async function streamChatV2(options) {
|
|
134
204
|
return executeStream(options, options.executeStream ?? defaultStreamExecutor);
|
|
135
205
|
}
|
|
206
|
+
export async function generateChatV2(options) {
|
|
207
|
+
const args = buildTextArgs(options);
|
|
208
|
+
const stepToolCalls = attachGenerateStepToolCallCollector(args);
|
|
209
|
+
let result;
|
|
210
|
+
try {
|
|
211
|
+
result = await (options.executeGenerate ?? defaultGenerateExecutor)(args);
|
|
212
|
+
}
|
|
213
|
+
catch (error) {
|
|
214
|
+
const recovered = recoverGenerateStructuredOutputError(error, options, stepToolCalls);
|
|
215
|
+
if (recovered != null) {
|
|
216
|
+
return recovered;
|
|
217
|
+
}
|
|
218
|
+
throw error;
|
|
219
|
+
}
|
|
220
|
+
const isStructuredOutput = isChatV2StructuredResponseFormat(options.responseFormat);
|
|
221
|
+
const responseText = isStructuredOutput ? collapseRepeatedStructuredJsonText(result.text) : result.text;
|
|
222
|
+
const finishReason = await resolveOptionalValue(result.finishReason);
|
|
223
|
+
return {
|
|
224
|
+
responseText,
|
|
225
|
+
structuredOutput: await resolveGenerateStructuredOutput(result, options.responseOutput, finishReason),
|
|
226
|
+
functionCalls: toStreamedFunctionCalls(result.toolCalls != null && result.toolCalls.length > 0 ? result.toolCalls : stepToolCalls),
|
|
227
|
+
usage: (await resolveOptionalValue(result.totalUsage)) ?? (await resolveOptionalValue(result.usage)),
|
|
228
|
+
reasoning: await resolveGenerateReasoning(result),
|
|
229
|
+
finishReason,
|
|
230
|
+
providerMetadata: await resolveOptionalValue(result.providerMetadata),
|
|
231
|
+
requestStatus: await resolveOptionalValue(result.requestStatus),
|
|
232
|
+
};
|
|
233
|
+
}
|