@valerypopoff/rivet2-core 2.0.1 → 2.0.2
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 +153 -39
- package/dist/cjs/bundle.cjs.map +2 -2
- package/dist/esm/model/SubprocessorBridge.js +29 -19
- package/dist/esm/model/chat-v2/chatV2Pipeline.js +25 -9
- package/dist/esm/model/chat-v2/chatV2ResponseFormat.js +71 -1
- package/dist/esm/model/chat-v2/llmChatV2NodeRuntime.js +2 -2
- package/dist/esm/model/chat-v2/toolContinuation.js +4 -1
- package/dist/esm/model/nodes/ContextNode.js +19 -8
- package/dist/esm/plugins/gentrace/plugin.js +4 -3
- package/dist/types/model/chat-v2/chatV2ResponseFormat.d.ts +2 -1
- package/dist/types/plugins/gentrace/plugin.d.ts +2 -1
- package/package.json +1 -1
|
@@ -1,3 +1,23 @@
|
|
|
1
|
+
function subscribeOwnGraphRunLifecycle(processor, onLifecycleEvent) {
|
|
2
|
+
let ownGraphRunId;
|
|
3
|
+
const unsubscribeGraphStart = processor.on('graphStart', (event) => {
|
|
4
|
+
ownGraphRunId ??= event.execution.graphRunId;
|
|
5
|
+
});
|
|
6
|
+
const onPossibleLifecycleEvent = (event) => {
|
|
7
|
+
if (ownGraphRunId != null && event.execution.graphRunId === ownGraphRunId) {
|
|
8
|
+
onLifecycleEvent();
|
|
9
|
+
}
|
|
10
|
+
};
|
|
11
|
+
const unsubscribeGraphFinish = processor.on('graphFinish', onPossibleLifecycleEvent);
|
|
12
|
+
const unsubscribeGraphAbort = processor.on('graphAbort', onPossibleLifecycleEvent);
|
|
13
|
+
const unsubscribeGraphError = processor.on('graphError', onPossibleLifecycleEvent);
|
|
14
|
+
return () => {
|
|
15
|
+
unsubscribeGraphStart();
|
|
16
|
+
unsubscribeGraphFinish();
|
|
17
|
+
unsubscribeGraphAbort();
|
|
18
|
+
unsubscribeGraphError();
|
|
19
|
+
};
|
|
20
|
+
}
|
|
1
21
|
export function wireSubprocessorEvents(processor, parentEmitter, parentState) {
|
|
2
22
|
const unsubscribers = [
|
|
3
23
|
processor.on('nodeError', (event) => parentEmitter.emit('nodeError', event)),
|
|
@@ -36,14 +56,8 @@ export function wireSubprocessorEvents(processor, parentEmitter, parentState) {
|
|
|
36
56
|
}
|
|
37
57
|
cleanedUp = true;
|
|
38
58
|
unsubscribers.forEach((unsubscribe) => unsubscribe());
|
|
39
|
-
unsubscribeAny();
|
|
40
|
-
unsubscribeGraphFinish();
|
|
41
|
-
unsubscribeGraphAbort();
|
|
42
|
-
unsubscribeGraphError();
|
|
43
59
|
};
|
|
44
|
-
|
|
45
|
-
const unsubscribeGraphAbort = processor.on('graphAbort', cleanup);
|
|
46
|
-
const unsubscribeGraphError = processor.on('graphError', cleanup);
|
|
60
|
+
unsubscribers.push(unsubscribeAny, subscribeOwnGraphRunLifecycle(processor, cleanup));
|
|
47
61
|
}
|
|
48
62
|
export function wireSubprocessorLifecycle(processor, options) {
|
|
49
63
|
const abortFromSignal = () => {
|
|
@@ -60,23 +74,19 @@ export function wireSubprocessorLifecycle(processor, options) {
|
|
|
60
74
|
};
|
|
61
75
|
options.signal?.addEventListener('abort', abortFromSignal, { once: true });
|
|
62
76
|
options.parentAbortSignal.addEventListener('abort', abortFromParent, { once: true });
|
|
63
|
-
const
|
|
64
|
-
|
|
77
|
+
const unsubscribers = [
|
|
78
|
+
() => options.signal?.removeEventListener('abort', abortFromSignal),
|
|
79
|
+
() => options.parentAbortSignal.removeEventListener('abort', abortFromParent),
|
|
80
|
+
options.onParentPause(pauseProcessor),
|
|
81
|
+
options.onParentResume(resumeProcessor),
|
|
82
|
+
];
|
|
65
83
|
let cleanedUp = false;
|
|
66
84
|
const cleanup = () => {
|
|
67
85
|
if (cleanedUp) {
|
|
68
86
|
return;
|
|
69
87
|
}
|
|
70
88
|
cleanedUp = true;
|
|
71
|
-
|
|
72
|
-
options.parentAbortSignal.removeEventListener('abort', abortFromParent);
|
|
73
|
-
unsubscribeParentPause();
|
|
74
|
-
unsubscribeParentResume();
|
|
75
|
-
unsubscribeGraphFinish();
|
|
76
|
-
unsubscribeGraphAbort();
|
|
77
|
-
unsubscribeGraphError();
|
|
89
|
+
unsubscribers.forEach((unsubscribe) => unsubscribe());
|
|
78
90
|
};
|
|
79
|
-
|
|
80
|
-
const unsubscribeGraphAbort = processor.on('graphAbort', cleanup);
|
|
81
|
-
const unsubscribeGraphError = processor.on('graphError', cleanup);
|
|
91
|
+
unsubscribers.push(subscribeOwnGraphRunLifecycle(processor, cleanup));
|
|
82
92
|
}
|
|
@@ -92,15 +92,7 @@ function buildCommonOutputs(requestMessages, response, functionCalls, usage, rea
|
|
|
92
92
|
};
|
|
93
93
|
}
|
|
94
94
|
if (options.outputReasoning) {
|
|
95
|
-
outputs['reasoning'] =
|
|
96
|
-
? {
|
|
97
|
-
type: 'string[]',
|
|
98
|
-
value: reasoning,
|
|
99
|
-
}
|
|
100
|
-
: {
|
|
101
|
-
type: 'string',
|
|
102
|
-
value: reasoning,
|
|
103
|
-
};
|
|
95
|
+
outputs['reasoning'] = buildReasoningOutput(reasoning);
|
|
104
96
|
}
|
|
105
97
|
if (options.outputRequestStatus) {
|
|
106
98
|
if (options.retryOnNon200) {
|
|
@@ -126,6 +118,30 @@ function buildCommonOutputs(requestMessages, response, functionCalls, usage, rea
|
|
|
126
118
|
}
|
|
127
119
|
return outputs;
|
|
128
120
|
}
|
|
121
|
+
function buildReasoningOutput(reasoning) {
|
|
122
|
+
if (Array.isArray(reasoning)) {
|
|
123
|
+
const nonEmptyReasoning = reasoning.filter((part) => typeof part === 'string' && part.trim().length > 0);
|
|
124
|
+
return nonEmptyReasoning.length > 0
|
|
125
|
+
? {
|
|
126
|
+
type: 'string[]',
|
|
127
|
+
value: nonEmptyReasoning,
|
|
128
|
+
}
|
|
129
|
+
: {
|
|
130
|
+
type: 'control-flow-excluded',
|
|
131
|
+
value: undefined,
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
const reasoningText = typeof reasoning === 'string' ? reasoning : '';
|
|
135
|
+
return reasoningText.trim().length > 0
|
|
136
|
+
? {
|
|
137
|
+
type: 'string',
|
|
138
|
+
value: reasoningText,
|
|
139
|
+
}
|
|
140
|
+
: {
|
|
141
|
+
type: 'control-flow-excluded',
|
|
142
|
+
value: undefined,
|
|
143
|
+
};
|
|
144
|
+
}
|
|
129
145
|
function getProviderFailureMessage(error) {
|
|
130
146
|
if (error instanceof Error) {
|
|
131
147
|
return error.message;
|
|
@@ -12,6 +12,40 @@ function normalizeOptionalString(value) {
|
|
|
12
12
|
const trimmed = value?.trim();
|
|
13
13
|
return trimmed ? trimmed : undefined;
|
|
14
14
|
}
|
|
15
|
+
function toJsonValue(value, path = 'Response Schema', seen = new WeakSet()) {
|
|
16
|
+
if (value === undefined) {
|
|
17
|
+
throw new Error(`${path} must be JSON-compatible; undefined values are not supported.`);
|
|
18
|
+
}
|
|
19
|
+
if (value === null || typeof value === 'string' || typeof value === 'boolean') {
|
|
20
|
+
return value;
|
|
21
|
+
}
|
|
22
|
+
if (typeof value === 'number') {
|
|
23
|
+
if (!Number.isFinite(value)) {
|
|
24
|
+
throw new Error(`${path} must be JSON-compatible; non-finite numbers are not supported.`);
|
|
25
|
+
}
|
|
26
|
+
return value;
|
|
27
|
+
}
|
|
28
|
+
if (Array.isArray(value)) {
|
|
29
|
+
return value.map((item, index) => toJsonValue(item, `${path}[${index}]`, seen));
|
|
30
|
+
}
|
|
31
|
+
if (typeof value === 'object') {
|
|
32
|
+
const prototype = Object.getPrototypeOf(value);
|
|
33
|
+
if (prototype !== Object.prototype && prototype !== null) {
|
|
34
|
+
throw new Error(`${path} must be JSON-compatible; only plain objects are supported.`);
|
|
35
|
+
}
|
|
36
|
+
if (seen.has(value)) {
|
|
37
|
+
throw new Error(`${path} must be JSON-compatible; circular references are not supported.`);
|
|
38
|
+
}
|
|
39
|
+
seen.add(value);
|
|
40
|
+
const jsonObject = {};
|
|
41
|
+
for (const [key, item] of Object.entries(value)) {
|
|
42
|
+
jsonObject[key] = toJsonValue(item, `${path}.${key}`, seen);
|
|
43
|
+
}
|
|
44
|
+
seen.delete(value);
|
|
45
|
+
return jsonObject;
|
|
46
|
+
}
|
|
47
|
+
throw new Error(`${path} must be JSON-compatible; ${typeof value} values are not supported.`);
|
|
48
|
+
}
|
|
15
49
|
export function resolveChatV2ResponseFormatParameters(data, inputs) {
|
|
16
50
|
const responseFormat = data.responseFormat ?? '';
|
|
17
51
|
if (!responseFormat) {
|
|
@@ -50,8 +84,44 @@ export function createChatV2ResponseOutput(parameters) {
|
|
|
50
84
|
});
|
|
51
85
|
}
|
|
52
86
|
return Output.object({
|
|
53
|
-
schema: jsonSchema(parameters.schema),
|
|
87
|
+
schema: jsonSchema(toJsonValue(parameters.schema)),
|
|
54
88
|
name: parameters.schemaName,
|
|
55
89
|
description: parameters.schemaDescription,
|
|
56
90
|
});
|
|
57
91
|
}
|
|
92
|
+
export function mergeCustomProviderResponseFormatOptions(provider, providerOptions, parameters) {
|
|
93
|
+
if (provider !== 'custom' || parameters == null) {
|
|
94
|
+
return providerOptions;
|
|
95
|
+
}
|
|
96
|
+
const customOptions = providerOptions?.custom ?? {};
|
|
97
|
+
if (parameters.responseFormat === 'json') {
|
|
98
|
+
return {
|
|
99
|
+
...providerOptions,
|
|
100
|
+
custom: {
|
|
101
|
+
...customOptions,
|
|
102
|
+
response_format: { type: 'json_object' },
|
|
103
|
+
},
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
if (parameters.responseFormat !== 'json_schema') {
|
|
107
|
+
return providerOptions;
|
|
108
|
+
}
|
|
109
|
+
const jsonSchemaOptions = {
|
|
110
|
+
name: parameters.schemaName,
|
|
111
|
+
strict: true,
|
|
112
|
+
schema: toJsonValue(parameters.schema),
|
|
113
|
+
};
|
|
114
|
+
if (parameters.schemaDescription != null) {
|
|
115
|
+
jsonSchemaOptions.description = parameters.schemaDescription;
|
|
116
|
+
}
|
|
117
|
+
return {
|
|
118
|
+
...providerOptions,
|
|
119
|
+
custom: {
|
|
120
|
+
...customOptions,
|
|
121
|
+
response_format: {
|
|
122
|
+
type: 'json_schema',
|
|
123
|
+
json_schema: jsonSchemaOptions,
|
|
124
|
+
},
|
|
125
|
+
},
|
|
126
|
+
};
|
|
127
|
+
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { coerceTypeOptional } from '../../utils/coerceType.js';
|
|
2
2
|
import { getInputOrData } from '../../utils/inputs.js';
|
|
3
|
-
import { createChatV2ResponseOutput, resolveChatV2ResponseFormatParameters } from './chatV2ResponseFormat.js';
|
|
3
|
+
import { createChatV2ResponseOutput, mergeCustomProviderResponseFormatOptions, resolveChatV2ResponseFormatParameters, } from './chatV2ResponseFormat.js';
|
|
4
4
|
import { hasLLMChatV2ToolResponseFormatConflict, LLM_CHAT_V2_TOOL_RESPONSE_FORMAT_CONFLICT_COPY, } from './chatV2FeatureCompatibility.js';
|
|
5
5
|
import { createChatV2Model, parseChatV2Provider, resolveChatV2ProviderConfig } from './providerOptions.js';
|
|
6
6
|
import { buildLLMChatV2EditorCacheKey, cloneLLMChatV2EditorCacheOutputs, resolveLLMChatV2EditorCache, } from './chatV2EditorCache.js';
|
|
@@ -34,9 +34,9 @@ export async function resolveLLMChatV2RuntimeConfig(params) {
|
|
|
34
34
|
const functions = data.useToolCalling && inputs['functions'] != null
|
|
35
35
|
? coerceTypeOptional(inputs['functions'], 'gpt-function[]')
|
|
36
36
|
: undefined;
|
|
37
|
-
const providerOptions = resolveLLMChatV2RuntimeProviderOptions(data, inputs);
|
|
38
37
|
const toolChoice = resolveLLMChatV2ToolChoice(data);
|
|
39
38
|
const responseFormatParameters = resolveChatV2ResponseFormatParameters(data, inputs);
|
|
39
|
+
const providerOptions = mergeCustomProviderResponseFormatOptions(provider, resolveLLMChatV2RuntimeProviderOptions(data, inputs), responseFormatParameters);
|
|
40
40
|
const generationParameters = resolveLLMChatV2GenerationParameters(data, inputs);
|
|
41
41
|
const runOptions = {
|
|
42
42
|
provider,
|
|
@@ -41,7 +41,10 @@ function applyAccumulatedUsage(result, usage, outputUsage) {
|
|
|
41
41
|
}
|
|
42
42
|
function appendReasoningRound(accumulated, reasoning) {
|
|
43
43
|
const roundReasoning = Array.isArray(reasoning) ? reasoning : [reasoning];
|
|
44
|
-
const nonEmptyReasoning = roundReasoning
|
|
44
|
+
const nonEmptyReasoning = roundReasoning
|
|
45
|
+
.filter((part) => typeof part === 'string')
|
|
46
|
+
.map((part) => part.trim())
|
|
47
|
+
.filter((part) => part.length > 0);
|
|
45
48
|
if (nonEmptyReasoning.length === 0) {
|
|
46
49
|
return;
|
|
47
50
|
}
|
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
import {} from '../NodeBase.js';
|
|
2
2
|
import { nanoid } from 'nanoid/non-secure';
|
|
3
3
|
import { NodeImpl } from '../NodeImpl.js';
|
|
4
|
-
import {} from '../DataValue.js';
|
|
4
|
+
import { getDefaultValue } from '../DataValue.js';
|
|
5
5
|
import {} from '../GraphProcessor.js';
|
|
6
6
|
import {} from '../ProcessContext.js';
|
|
7
7
|
import { dedent } from 'ts-dedent';
|
|
8
8
|
import {} from '../EditorDefinition.js';
|
|
9
9
|
import {} from '../NodeBodySpec.js';
|
|
10
10
|
import { nodeDefinition } from '../NodeDefinition.js';
|
|
11
|
+
import { coerceTypeOptional, inferType } from '../../utils/coerceType.js';
|
|
11
12
|
export class ContextNodeImpl extends NodeImpl {
|
|
12
13
|
static create() {
|
|
13
14
|
const chartNode = {
|
|
@@ -78,21 +79,31 @@ export class ContextNodeImpl extends NodeImpl {
|
|
|
78
79
|
};
|
|
79
80
|
}
|
|
80
81
|
async process(inputs, context) {
|
|
81
|
-
const contextValue = context.contextValues[this.data.id]
|
|
82
|
-
|
|
82
|
+
const contextValue = context.contextValues?.[this.data.id] == null
|
|
83
|
+
? undefined
|
|
84
|
+
: coerceTypeOptional(context.contextValues[this.data.id], this.data.dataType);
|
|
85
|
+
if (contextValue != null) {
|
|
83
86
|
return {
|
|
84
|
-
['data']:
|
|
87
|
+
['data']: {
|
|
88
|
+
type: this.data.dataType,
|
|
89
|
+
value: contextValue,
|
|
90
|
+
},
|
|
85
91
|
};
|
|
86
92
|
}
|
|
87
93
|
let defaultValue;
|
|
88
94
|
if (this.data.useDefaultValueInput) {
|
|
89
|
-
|
|
95
|
+
const defaultInputValue = inputs['default'];
|
|
96
|
+
defaultValue = defaultInputValue == null ? undefined : coerceTypeOptional(defaultInputValue, this.data.dataType);
|
|
90
97
|
}
|
|
91
|
-
|
|
92
|
-
defaultValue =
|
|
98
|
+
if (defaultValue == null) {
|
|
99
|
+
defaultValue =
|
|
100
|
+
coerceTypeOptional(inferType(this.data.defaultValue), this.data.dataType) ?? getDefaultValue(this.data.dataType);
|
|
93
101
|
}
|
|
94
102
|
return {
|
|
95
|
-
['data']:
|
|
103
|
+
['data']: {
|
|
104
|
+
type: this.data.dataType,
|
|
105
|
+
value: defaultValue,
|
|
106
|
+
},
|
|
96
107
|
};
|
|
97
108
|
}
|
|
98
109
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Pipeline, StepRun, init, runTest, getPipelines } from '@gentrace/core';
|
|
2
2
|
import { globalRivetNodeRegistry } from '../../model/Nodes.js';
|
|
3
|
-
import { mapValues } from 'lodash-es';
|
|
3
|
+
import { cloneDeep, mapValues } from 'lodash-es';
|
|
4
4
|
import { GptTokenizerTokenizer } from '../../integrations/GptTokenizerTokenizer.js';
|
|
5
5
|
import { ExecutionRecorder } from '../../recording/ExecutionRecorder.js';
|
|
6
6
|
import { inferType } from '../../utils/coerceType.js';
|
|
@@ -18,7 +18,7 @@ function initializeGentrace(gentraceApiKey) {
|
|
|
18
18
|
apiKey: gentraceApiKey,
|
|
19
19
|
});
|
|
20
20
|
}
|
|
21
|
-
export const runGentraceTests = async (gentracePipelineSlug, settings, project, graph, nativeApi) => {
|
|
21
|
+
export const runGentraceTests = async (gentracePipelineSlug, settings, project, graph, nativeApi, contextValues = {}) => {
|
|
22
22
|
const gentraceApiKey = settings.pluginSettings?.gentrace?.gentraceApiKey;
|
|
23
23
|
if (!gentraceApiKey) {
|
|
24
24
|
throw new Error('Gentrace API key not set.');
|
|
@@ -45,11 +45,12 @@ export const runGentraceTests = async (gentracePipelineSlug, settings, project,
|
|
|
45
45
|
const processor = new GraphProcessor(tempProject, graphId, globalRivetNodeRegistry);
|
|
46
46
|
processor.executor = 'browser';
|
|
47
47
|
recorder.record(processor);
|
|
48
|
+
const runContextValues = cloneDeep(contextValues);
|
|
48
49
|
await processor.processGraph({
|
|
49
50
|
settings,
|
|
50
51
|
nativeApi,
|
|
51
52
|
tokenizer: new GptTokenizerTokenizer(),
|
|
52
|
-
}, rivetFormattedInputs);
|
|
53
|
+
}, rivetFormattedInputs, runContextValues);
|
|
53
54
|
const fullRecording = recorder.getRecording();
|
|
54
55
|
const stepRuns = convertRecordingToStepRuns(fullRecording, project, graphId);
|
|
55
56
|
for (const stepRun of stepRuns) {
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { Inputs } from '../GraphProcessor.js';
|
|
2
|
-
import type { ChatV2ResponseOutput } from './chatV2Types.js';
|
|
2
|
+
import type { ChatV2Provider, ChatV2ProviderOptions, ChatV2ResponseOutput } from './chatV2Types.js';
|
|
3
3
|
export type ChatV2ResponseFormat = '' | 'text' | 'json' | 'json_schema';
|
|
4
4
|
export type ChatV2ResponseFormatData = {
|
|
5
5
|
responseFormat?: ChatV2ResponseFormat;
|
|
@@ -22,3 +22,4 @@ export type ChatV2ResponseFormatParameters = undefined | {
|
|
|
22
22
|
};
|
|
23
23
|
export declare function resolveChatV2ResponseFormatParameters(data: ChatV2ResponseFormatData, inputs: Inputs): ChatV2ResponseFormatParameters;
|
|
24
24
|
export declare function createChatV2ResponseOutput(parameters: ChatV2ResponseFormatParameters): ChatV2ResponseOutput | undefined;
|
|
25
|
+
export declare function mergeCustomProviderResponseFormatOptions(provider: ChatV2Provider, providerOptions: ChatV2ProviderOptions | undefined, parameters: ChatV2ResponseFormatParameters): ChatV2ProviderOptions | undefined;
|
|
@@ -4,7 +4,8 @@ import type { NativeApi } from '../../native/NativeApi.js';
|
|
|
4
4
|
import type { Recording } from '../../recording/RecordedEvents.js';
|
|
5
5
|
import type { RivetPlugin } from '../../model/RivetPlugin.js';
|
|
6
6
|
import type { Settings } from '../../model/Settings.js';
|
|
7
|
-
|
|
7
|
+
import type { DataValue } from '../../model/DataValue.js';
|
|
8
|
+
export declare const runGentraceTests: (gentracePipelineSlug: string, settings: Settings, project: Omit<Project, "data">, graph: NodeGraph, nativeApi: NativeApi, contextValues?: Record<string, DataValue>) => Promise<import("@gentrace/core").TestResultPost200Response>;
|
|
8
9
|
export declare const runRemoteGentraceTests: (gentracePipelineSlug: string, settings: Settings, project: Omit<Project, "data">, graph: NodeGraph, runAndRecord: (testCase: Record<string, any>) => Promise<Recording>) => Promise<import("@gentrace/core").TestResultPost200Response>;
|
|
9
10
|
export declare const getGentracePipelines: (gentraceApiKey: string) => Promise<import("@gentrace/core/dist/models/pipelines-get200-response-pipelines-inner.js").PipelinesGet200ResponsePipelinesInner[]>;
|
|
10
11
|
export declare const gentracePlugin: RivetPlugin;
|