@alpic80/rivet-core 1.19.1-aidon.2 → 1.24.0-aidon.1
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/README.md +4 -0
- package/dist/cjs/bundle.cjs +4187 -1020
- package/dist/cjs/bundle.cjs.map +4 -4
- package/dist/esm/api/createProcessor.js +8 -17
- package/dist/esm/api/looseDataValue.js +16 -0
- package/dist/esm/exports.js +2 -0
- package/dist/esm/integrations/CodeRunner.js +36 -0
- package/dist/esm/integrations/GptTokenizerTokenizer.js +7 -4
- package/dist/esm/integrations/openai/OpenAIEmbeddingGenerator.js +1 -1
- package/dist/esm/model/DataValue.js +14 -2
- package/dist/esm/model/GraphProcessor.js +275 -104
- package/dist/esm/model/NodeBase.js +11 -1
- package/dist/esm/model/NodeImpl.js +8 -0
- package/dist/esm/model/Nodes.js +31 -4
- package/dist/esm/model/ProjectReferenceLoader.js +1 -0
- package/dist/esm/model/nodes/AssembleMessageNode.js +12 -2
- package/dist/esm/model/nodes/AssemblePromptNode.js +22 -0
- package/dist/esm/model/nodes/CallGraphNode.js +3 -4
- package/dist/esm/model/nodes/ChatLoopNode.js +150 -0
- package/dist/esm/model/nodes/ChatNode.js +7 -934
- package/dist/esm/model/nodes/ChatNodeBase.js +1275 -0
- package/dist/esm/model/nodes/ChunkNode.js +2 -2
- package/dist/esm/model/nodes/CodeNode.js +40 -4
- package/dist/esm/model/nodes/CronNode.js +248 -0
- package/dist/esm/model/nodes/DelegateFunctionCallNode.js +37 -12
- package/dist/esm/model/nodes/DestructureNode.js +1 -1
- package/dist/esm/model/nodes/DocumentNode.js +183 -0
- package/dist/esm/model/nodes/ExtractJsonNode.js +4 -4
- package/dist/esm/model/nodes/ExtractRegexNode.js +10 -11
- package/dist/esm/model/nodes/GetEmbeddingNode.js +1 -1
- package/dist/esm/model/nodes/HttpCallNode.js +3 -1
- package/dist/esm/model/nodes/IfNode.js +5 -0
- package/dist/esm/model/nodes/ImageToMDNode.js +116 -0
- package/dist/esm/model/nodes/LoopUntilNode.js +214 -0
- package/dist/esm/model/nodes/PromptNode.js +29 -6
- package/dist/esm/model/nodes/ReadAllFilesNode.js +210 -0
- package/dist/esm/model/nodes/ReadDirectoryNode.js +31 -25
- package/dist/esm/model/nodes/ReferencedGraphAliasNode.js +199 -0
- package/dist/esm/model/nodes/TextNode.js +9 -4
- package/dist/esm/model/nodes/ToMarkdownTableNode.js +119 -0
- package/dist/esm/model/nodes/ToTreeNode.js +133 -0
- package/dist/esm/model/nodes/{GptFunctionNode.js → ToolNode.js} +10 -10
- package/dist/esm/model/nodes/UserInputNode.js +10 -12
- package/dist/esm/plugins/aidon/nodes/ChatAidonNode.js +3 -3
- package/dist/esm/plugins/anthropic/anthropic.js +29 -10
- package/dist/esm/plugins/anthropic/fetchEventSource.js +3 -2
- package/dist/esm/plugins/anthropic/nodes/ChatAnthropicNode.js +267 -147
- package/dist/esm/plugins/anthropic/plugin.js +9 -1
- package/dist/esm/plugins/gentrace/plugin.js +6 -6
- package/dist/esm/plugins/google/google.js +113 -5
- package/dist/esm/plugins/google/nodes/ChatGoogleNode.js +211 -54
- package/dist/esm/plugins/google/plugin.js +13 -6
- package/dist/esm/plugins/openai/nodes/RunThreadNode.js +2 -2
- package/dist/esm/recording/ExecutionRecorder.js +5 -1
- package/dist/esm/utils/chatMessageToOpenAIChatCompletionMessage.js +15 -2
- package/dist/esm/utils/coerceType.js +1 -1
- package/dist/esm/utils/fetchEventSource.js +1 -1
- package/dist/esm/utils/interpolation.js +108 -3
- package/dist/esm/utils/openai.js +106 -50
- package/dist/esm/utils/paths.js +80 -0
- package/dist/esm/utils/serialization/serialization_v4.js +5 -0
- package/dist/types/api/createProcessor.d.ts +11 -5
- package/dist/types/api/looseDataValue.d.ts +4 -0
- package/dist/types/api/streaming.d.ts +1 -1
- package/dist/types/exports.d.ts +2 -0
- package/dist/types/integrations/CodeRunner.d.ts +18 -0
- package/dist/types/model/DataValue.d.ts +29 -6
- package/dist/types/model/EditorDefinition.d.ts +6 -1
- package/dist/types/model/GraphProcessor.d.ts +14 -7
- package/dist/types/model/NodeBase.d.ts +4 -0
- package/dist/types/model/NodeImpl.d.ts +5 -4
- package/dist/types/model/Nodes.d.ts +13 -4
- package/dist/types/model/ProcessContext.d.ts +16 -1
- package/dist/types/model/Project.d.ts +19 -7
- package/dist/types/model/ProjectReferenceLoader.d.ts +5 -0
- package/dist/types/model/RivetPlugin.d.ts +6 -0
- package/dist/types/model/RivetUIContext.d.ts +5 -1
- package/dist/types/model/Settings.d.ts +1 -0
- package/dist/types/model/nodes/AssemblePromptNode.d.ts +4 -1
- package/dist/types/model/nodes/ChatLoopNode.d.ts +21 -0
- package/dist/types/model/nodes/ChatNode.d.ts +2 -62
- package/dist/types/model/nodes/ChatNodeBase.d.ts +85 -0
- package/dist/types/model/nodes/CodeNode.d.ts +8 -2
- package/dist/types/model/nodes/CronNode.d.ts +34 -0
- package/dist/types/model/nodes/DelegateFunctionCallNode.d.ts +1 -0
- package/dist/types/model/nodes/DocumentNode.d.ts +28 -0
- package/dist/types/model/nodes/ImageToMDNode.d.ts +20 -0
- package/dist/types/model/nodes/LoopUntilNode.d.ts +32 -0
- package/dist/types/model/nodes/PromptNode.d.ts +2 -0
- package/dist/types/model/nodes/ReadAllFilesNode.d.ts +30 -0
- package/dist/types/model/nodes/ReadDirectoryNode.d.ts +1 -1
- package/dist/types/model/nodes/ReferencedGraphAliasNode.d.ts +31 -0
- package/dist/types/model/nodes/ToMarkdownTableNode.d.ts +19 -0
- package/dist/types/model/nodes/ToTreeNode.d.ts +21 -0
- package/dist/types/model/nodes/UserInputNode.d.ts +2 -3
- package/dist/types/plugins/anthropic/anthropic.d.ts +94 -13
- package/dist/types/plugins/anthropic/nodes/ChatAnthropicNode.d.ts +7 -2
- package/dist/types/plugins/google/google.d.ts +93 -18
- package/dist/types/plugins/google/nodes/ChatGoogleNode.d.ts +3 -2
- package/dist/types/recording/RecordedEvents.d.ts +2 -0
- package/dist/types/utils/base64.d.ts +1 -1
- package/dist/types/utils/chatMessageToOpenAIChatCompletionMessage.d.ts +3 -1
- package/dist/types/utils/interpolation.d.ts +3 -0
- package/dist/types/utils/openai.d.ts +127 -21
- package/dist/types/utils/paths.d.ts +8 -0
- package/package.json +15 -11
- /package/dist/types/model/nodes/{GptFunctionNode.d.ts → ToolNode.d.ts} +0 -0
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import {} from '../NodeBase.js';
|
|
2
|
+
import { nanoid } from 'nanoid/non-secure';
|
|
3
|
+
import { NodeImpl } from '../NodeImpl.js';
|
|
4
|
+
import { nodeDefinition } from '../NodeDefinition.js';
|
|
5
|
+
import {} from '../DataValue.js';
|
|
6
|
+
import {} from '../EditorDefinition.js';
|
|
7
|
+
import { dedent } from 'ts-dedent';
|
|
8
|
+
import { coerceTypeOptional } from '../../utils/coerceType.js';
|
|
9
|
+
import { extractInterpolationVariables, interpolate } from '../../utils/interpolation.js';
|
|
10
|
+
import { get, sortBy } from 'lodash-es';
|
|
11
|
+
export class ToTreeNodeImpl extends NodeImpl {
|
|
12
|
+
static create() {
|
|
13
|
+
const chartNode = {
|
|
14
|
+
type: 'toTree',
|
|
15
|
+
title: 'To Tree',
|
|
16
|
+
id: nanoid(),
|
|
17
|
+
visualData: {
|
|
18
|
+
x: 0,
|
|
19
|
+
y: 0,
|
|
20
|
+
width: 300,
|
|
21
|
+
},
|
|
22
|
+
data: {
|
|
23
|
+
format: '{{path}}',
|
|
24
|
+
childrenProperty: 'children',
|
|
25
|
+
useSortAlphabetically: true,
|
|
26
|
+
},
|
|
27
|
+
};
|
|
28
|
+
return chartNode;
|
|
29
|
+
}
|
|
30
|
+
getInputDefinitions() {
|
|
31
|
+
return [
|
|
32
|
+
{
|
|
33
|
+
id: 'objects',
|
|
34
|
+
title: 'Objects',
|
|
35
|
+
dataType: ['object[]', 'object'],
|
|
36
|
+
required: true,
|
|
37
|
+
},
|
|
38
|
+
];
|
|
39
|
+
}
|
|
40
|
+
getOutputDefinitions() {
|
|
41
|
+
return [
|
|
42
|
+
{
|
|
43
|
+
id: 'tree',
|
|
44
|
+
title: 'Tree',
|
|
45
|
+
dataType: 'string',
|
|
46
|
+
},
|
|
47
|
+
];
|
|
48
|
+
}
|
|
49
|
+
getEditors() {
|
|
50
|
+
return [
|
|
51
|
+
{
|
|
52
|
+
type: 'string',
|
|
53
|
+
label: 'Children Property',
|
|
54
|
+
dataKey: 'childrenProperty',
|
|
55
|
+
},
|
|
56
|
+
{
|
|
57
|
+
type: 'code',
|
|
58
|
+
label: 'Format',
|
|
59
|
+
dataKey: 'format',
|
|
60
|
+
language: 'prompt-interpolation-markdown',
|
|
61
|
+
theme: 'prompt-interpolation',
|
|
62
|
+
},
|
|
63
|
+
{
|
|
64
|
+
type: 'toggle',
|
|
65
|
+
label: 'Sort Alphabetically',
|
|
66
|
+
dataKey: 'useSortAlphabetically',
|
|
67
|
+
},
|
|
68
|
+
];
|
|
69
|
+
}
|
|
70
|
+
getBody() {
|
|
71
|
+
return dedent `
|
|
72
|
+
Format: ${this.data.format}
|
|
73
|
+
Children: ${this.data.childrenProperty}
|
|
74
|
+
Sort: ${this.data.useSortAlphabetically ? 'Yes' : 'No'}
|
|
75
|
+
`;
|
|
76
|
+
}
|
|
77
|
+
static getUIData() {
|
|
78
|
+
return {
|
|
79
|
+
infoBoxBody: dedent `
|
|
80
|
+
Converts an array of objects into a tree structure and renders it as text.
|
|
81
|
+
|
|
82
|
+
The format field supports interpolation using {{property}} syntax to determine
|
|
83
|
+
how each node is displayed.
|
|
84
|
+
|
|
85
|
+
Use the children property to specify which field contains child nodes.
|
|
86
|
+
`,
|
|
87
|
+
infoBoxTitle: 'To Tree Node',
|
|
88
|
+
contextMenuTitle: 'To Tree',
|
|
89
|
+
group: ['Text'],
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
buildTree(objects, parentPath = '', level = 0, isLast = true) {
|
|
93
|
+
if (!Array.isArray(objects) || objects.length === 0)
|
|
94
|
+
return '';
|
|
95
|
+
let result = '';
|
|
96
|
+
const sortedObjects = this.data.useSortAlphabetically
|
|
97
|
+
? sortBy(objects, (obj) => String(get(obj, 'path', '')))
|
|
98
|
+
: objects;
|
|
99
|
+
sortedObjects.forEach((obj, index) => {
|
|
100
|
+
const isLastItem = index === sortedObjects.length - 1;
|
|
101
|
+
const prefix = level === 0 ? '' : isLast ? '└── ' : '├── ';
|
|
102
|
+
const indent = level === 0 ? '' : ' '.repeat(level - 1) + (isLast ? ' ' : '│ ');
|
|
103
|
+
// Get all potential interpolation variables from the format string
|
|
104
|
+
const matches = extractInterpolationVariables(this.data.format);
|
|
105
|
+
const interpolationVars = matches.reduce((acc, match) => {
|
|
106
|
+
const key = match;
|
|
107
|
+
acc[key] = String(get(obj, key, ''));
|
|
108
|
+
return acc;
|
|
109
|
+
}, {});
|
|
110
|
+
const formattedNode = interpolate(this.data.format, interpolationVars);
|
|
111
|
+
// Add this node to the result
|
|
112
|
+
result += indent + prefix + formattedNode + '\n';
|
|
113
|
+
// Process children if they exist
|
|
114
|
+
const children = get(obj, this.data.childrenProperty);
|
|
115
|
+
if (Array.isArray(children) && children.length > 0) {
|
|
116
|
+
const newPath = parentPath ? `${parentPath}/${formattedNode}` : formattedNode;
|
|
117
|
+
result += this.buildTree(children, newPath, level + 1, isLastItem);
|
|
118
|
+
}
|
|
119
|
+
});
|
|
120
|
+
return result;
|
|
121
|
+
}
|
|
122
|
+
async process(inputs) {
|
|
123
|
+
const objects = coerceTypeOptional(inputs['objects'], 'object[]') ?? [];
|
|
124
|
+
const treeOutput = this.buildTree(objects);
|
|
125
|
+
return {
|
|
126
|
+
['tree']: {
|
|
127
|
+
type: 'string',
|
|
128
|
+
value: treeOutput,
|
|
129
|
+
},
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
export const toTreeNode = nodeDefinition(ToTreeNodeImpl, 'To Tree');
|
|
@@ -13,7 +13,7 @@ export class GptFunctionNodeImpl extends NodeImpl {
|
|
|
13
13
|
static create() {
|
|
14
14
|
const chartNode = {
|
|
15
15
|
type: 'gptFunction',
|
|
16
|
-
title: '
|
|
16
|
+
title: 'Tool',
|
|
17
17
|
id: nanoid(),
|
|
18
18
|
visualData: {
|
|
19
19
|
x: 0,
|
|
@@ -21,7 +21,7 @@ export class GptFunctionNodeImpl extends NodeImpl {
|
|
|
21
21
|
width: 250,
|
|
22
22
|
},
|
|
23
23
|
data: {
|
|
24
|
-
name: '
|
|
24
|
+
name: 'newTool',
|
|
25
25
|
description: 'No description provided',
|
|
26
26
|
schema: dedent `
|
|
27
27
|
{
|
|
@@ -39,7 +39,7 @@ export class GptFunctionNodeImpl extends NodeImpl {
|
|
|
39
39
|
id: 'name',
|
|
40
40
|
title: 'Name',
|
|
41
41
|
dataType: 'string',
|
|
42
|
-
description: 'The name of the
|
|
42
|
+
description: 'The name of the tool that the LLM will see as available to call',
|
|
43
43
|
});
|
|
44
44
|
}
|
|
45
45
|
if (this.data.useDescriptionInput) {
|
|
@@ -47,7 +47,7 @@ export class GptFunctionNodeImpl extends NodeImpl {
|
|
|
47
47
|
id: 'description',
|
|
48
48
|
title: 'Description',
|
|
49
49
|
dataType: 'string',
|
|
50
|
-
description: 'The description of the
|
|
50
|
+
description: 'The description of the tool that the LLM will see as available to call',
|
|
51
51
|
});
|
|
52
52
|
}
|
|
53
53
|
if (this.data.useSchemaInput) {
|
|
@@ -55,7 +55,7 @@ export class GptFunctionNodeImpl extends NodeImpl {
|
|
|
55
55
|
id: 'schema',
|
|
56
56
|
title: 'Schema',
|
|
57
57
|
dataType: 'object',
|
|
58
|
-
description: 'The schema of the
|
|
58
|
+
description: 'The schema of the tool that the LLM will see as available to call',
|
|
59
59
|
});
|
|
60
60
|
}
|
|
61
61
|
// Extract inputs from promptText, everything like {{input}}
|
|
@@ -81,7 +81,7 @@ export class GptFunctionNodeImpl extends NodeImpl {
|
|
|
81
81
|
id: 'function',
|
|
82
82
|
title: 'Function',
|
|
83
83
|
dataType: 'gpt-function',
|
|
84
|
-
description: 'The
|
|
84
|
+
description: 'The tool that can be called by the LLM.',
|
|
85
85
|
},
|
|
86
86
|
];
|
|
87
87
|
}
|
|
@@ -127,10 +127,10 @@ export class GptFunctionNodeImpl extends NodeImpl {
|
|
|
127
127
|
static getUIData() {
|
|
128
128
|
return {
|
|
129
129
|
infoBoxBody: dedent `
|
|
130
|
-
Defines a
|
|
130
|
+
Defines a tool, which is a method that the LLM can call in its responses.
|
|
131
131
|
`,
|
|
132
|
-
infoBoxTitle: '
|
|
133
|
-
contextMenuTitle: '
|
|
132
|
+
infoBoxTitle: 'Tool Node',
|
|
133
|
+
contextMenuTitle: 'Tool',
|
|
134
134
|
group: ['AI'],
|
|
135
135
|
};
|
|
136
136
|
}
|
|
@@ -166,4 +166,4 @@ export class GptFunctionNodeImpl extends NodeImpl {
|
|
|
166
166
|
};
|
|
167
167
|
}
|
|
168
168
|
}
|
|
169
|
-
export const gptFunctionNode = nodeDefinition(GptFunctionNodeImpl, '
|
|
169
|
+
export const gptFunctionNode = nodeDefinition(GptFunctionNodeImpl, 'Tool');
|
|
@@ -5,6 +5,7 @@ import {} from '../../index.js';
|
|
|
5
5
|
import { dedent } from 'ts-dedent';
|
|
6
6
|
import { nodeDefinition } from '../NodeDefinition.js';
|
|
7
7
|
import { coerceType } from '../../utils/coerceType.js';
|
|
8
|
+
import {} from '../ProcessContext.js';
|
|
8
9
|
export class UserInputNodeImpl extends NodeImpl {
|
|
9
10
|
static create() {
|
|
10
11
|
const chartNode = {
|
|
@@ -89,21 +90,18 @@ export class UserInputNodeImpl extends NodeImpl {
|
|
|
89
90
|
group: ['Input/Output'],
|
|
90
91
|
};
|
|
91
92
|
}
|
|
92
|
-
async process() {
|
|
93
|
+
async process(inputs, context) {
|
|
94
|
+
const questions = this.data.useInput ? coerceType(inputs['questions'], 'string[]') : [this.data.prompt];
|
|
95
|
+
const renderingFormat = this.data.renderingFormat === 'preformatted' ? 'text' : 'markdown';
|
|
96
|
+
const response = await context.requestUserInput(questions, renderingFormat);
|
|
93
97
|
return {
|
|
94
|
-
['output']:
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
getOutputValuesFromUserInput(questions, answers) {
|
|
99
|
-
const questionsList = this.data.useInput
|
|
100
|
-
? coerceType(questions['questions'], 'string[]')
|
|
101
|
-
: [this.data.prompt];
|
|
102
|
-
return {
|
|
103
|
-
['output']: answers,
|
|
98
|
+
['output']: {
|
|
99
|
+
type: 'string[]',
|
|
100
|
+
value: response.value,
|
|
101
|
+
},
|
|
104
102
|
['questionsAndAnswers']: {
|
|
105
103
|
type: 'string[]',
|
|
106
|
-
value: zip(
|
|
104
|
+
value: zip(questions, response.value).map(([q, a]) => `${q}\n${a}`),
|
|
107
105
|
},
|
|
108
106
|
};
|
|
109
107
|
}
|
|
@@ -84,7 +84,7 @@ class ChatAidonNodeImpl extends ChatNodeImpl {
|
|
|
84
84
|
const customHeaders = schemaDetail.headers; // Moved this line up to the loop
|
|
85
85
|
// Check if custom headers are set and are of type string
|
|
86
86
|
if (customHeaders && typeof customHeaders === "string") {
|
|
87
|
-
|
|
87
|
+
const parsedCustomHeaders = JSON.parse(customHeaders);
|
|
88
88
|
headers = {
|
|
89
89
|
...headers,
|
|
90
90
|
...parsedCustomHeaders
|
|
@@ -129,7 +129,7 @@ class ChatAidonNodeImpl extends ChatNodeImpl {
|
|
|
129
129
|
//call the tool(s) to get the results to add to the message
|
|
130
130
|
for (const functionCall of functionCalls) {
|
|
131
131
|
// Find the schema detail that contains the function name
|
|
132
|
-
const toolSchema = toolSchemas.find(detail => detail.name
|
|
132
|
+
const toolSchema = toolSchemas.find(detail => detail.name === functionCall.name);
|
|
133
133
|
if (!toolSchema) {
|
|
134
134
|
throw new Error(`Function ${functionCall.name} not found in any schema`);
|
|
135
135
|
}
|
|
@@ -181,7 +181,7 @@ const createPluginNodeImpl = (chatNode) => {
|
|
|
181
181
|
impl.chartNode.data = data; // Ensure data is set correctly in the base class
|
|
182
182
|
const outputs = impl.getOutputDefinitions();
|
|
183
183
|
return outputs.filter(output => {
|
|
184
|
-
return
|
|
184
|
+
return output.id !== 'function-calls';
|
|
185
185
|
});
|
|
186
186
|
},
|
|
187
187
|
getEditors(data) {
|
|
@@ -56,7 +56,7 @@ export const anthropicModels = {
|
|
|
56
56
|
},
|
|
57
57
|
displayName: 'Claude 3 Opus',
|
|
58
58
|
},
|
|
59
|
-
'claude-3-5-sonnet-
|
|
59
|
+
'claude-3-5-sonnet-latest': {
|
|
60
60
|
maxTokens: 200_000,
|
|
61
61
|
cost: {
|
|
62
62
|
prompt: 3e-6,
|
|
@@ -64,19 +64,36 @@ export const anthropicModels = {
|
|
|
64
64
|
},
|
|
65
65
|
displayName: 'Claude 3.5 Sonnet',
|
|
66
66
|
},
|
|
67
|
+
'claude-3-5-haiku-latest': {
|
|
68
|
+
maxTokens: 200_000,
|
|
69
|
+
cost: {
|
|
70
|
+
prompt: 0.8e-6,
|
|
71
|
+
completion: 4e-6,
|
|
72
|
+
},
|
|
73
|
+
displayName: 'Claude 3.5 Haiku',
|
|
74
|
+
},
|
|
75
|
+
'claude-3-7-sonnet-latest': {
|
|
76
|
+
maxTokens: 200_000,
|
|
77
|
+
cost: {
|
|
78
|
+
prompt: 3e-6,
|
|
79
|
+
completion: 15e-6,
|
|
80
|
+
},
|
|
81
|
+
displayName: 'Claude 3.7 Sonnet',
|
|
82
|
+
},
|
|
67
83
|
};
|
|
68
84
|
export const anthropicModelOptions = Object.entries(anthropicModels).map(([id, { displayName }]) => ({
|
|
69
85
|
value: id,
|
|
70
86
|
label: displayName,
|
|
71
87
|
}));
|
|
72
|
-
export async function* streamChatCompletions({ apiKey, signal, ...rest }) {
|
|
88
|
+
export async function* streamChatCompletions({ apiEndpoint, apiKey, signal, ...rest }) {
|
|
73
89
|
const defaultSignal = new AbortController().signal;
|
|
74
|
-
const response = await fetchEventSource(
|
|
90
|
+
const response = await fetchEventSource(`${apiEndpoint}/completions`, {
|
|
75
91
|
method: 'POST',
|
|
76
92
|
headers: {
|
|
77
93
|
'Content-Type': 'application/json',
|
|
78
94
|
'x-api-key': apiKey,
|
|
79
95
|
'anthropic-version': '2023-06-01',
|
|
96
|
+
'anthropic-dangerous-direct-browser-access': 'true',
|
|
80
97
|
},
|
|
81
98
|
body: JSON.stringify({
|
|
82
99
|
...rest,
|
|
@@ -110,15 +127,16 @@ export async function* streamChatCompletions({ apiKey, signal, ...rest }) {
|
|
|
110
127
|
throw new AnthropicError(`No chunks received. Response: ${JSON.stringify(responseJson)}`, response, responseJson);
|
|
111
128
|
}
|
|
112
129
|
}
|
|
113
|
-
export async function callMessageApi({ apiKey, signal, tools, ...rest }) {
|
|
130
|
+
export async function callMessageApi({ apiEndpoint, apiKey, signal, tools, beta, ...rest }) {
|
|
114
131
|
const defaultSignal = new AbortController().signal;
|
|
115
|
-
const response = await fetch(
|
|
132
|
+
const response = await fetch(`${apiEndpoint}/messages`, {
|
|
116
133
|
method: 'POST',
|
|
117
134
|
headers: {
|
|
118
135
|
'Content-Type': 'application/json',
|
|
119
136
|
'x-api-key': apiKey,
|
|
120
137
|
'anthropic-version': '2023-06-01',
|
|
121
|
-
'anthropic-
|
|
138
|
+
'anthropic-dangerous-direct-browser-access': 'true',
|
|
139
|
+
...(beta ? { 'anthropic-beta': beta } : {}),
|
|
122
140
|
},
|
|
123
141
|
body: JSON.stringify({
|
|
124
142
|
...rest,
|
|
@@ -133,16 +151,17 @@ export async function callMessageApi({ apiKey, signal, tools, ...rest }) {
|
|
|
133
151
|
}
|
|
134
152
|
return responseJson;
|
|
135
153
|
}
|
|
136
|
-
export async function* streamMessageApi({ apiKey, signal, ...rest }) {
|
|
154
|
+
export async function* streamMessageApi({ apiEndpoint, apiKey, signal, beta, ...rest }) {
|
|
137
155
|
// Use the Messages API for Claude 3 models
|
|
138
156
|
const defaultSignal = new AbortController().signal;
|
|
139
|
-
const response = await fetchEventSource(
|
|
157
|
+
const response = await fetchEventSource(`${apiEndpoint}/messages`, {
|
|
140
158
|
method: 'POST',
|
|
141
159
|
headers: {
|
|
142
160
|
'Content-Type': 'application/json',
|
|
143
161
|
'x-api-key': apiKey,
|
|
144
162
|
'anthropic-version': '2023-06-01',
|
|
145
|
-
'anthropic-
|
|
163
|
+
'anthropic-dangerous-direct-browser-access': 'true',
|
|
164
|
+
...(beta ? { 'anthropic-beta': beta } : {}),
|
|
146
165
|
},
|
|
147
166
|
body: JSON.stringify({
|
|
148
167
|
...rest,
|
|
@@ -157,7 +176,7 @@ export async function* streamMessageApi({ apiKey, signal, ...rest }) {
|
|
|
157
176
|
if (chunk === '[message_stop]') {
|
|
158
177
|
return;
|
|
159
178
|
}
|
|
160
|
-
else if (
|
|
179
|
+
else if (/^\[\w+\]$/.test(chunk)) {
|
|
161
180
|
nextDataType = chunk.slice(1, -1);
|
|
162
181
|
continue;
|
|
163
182
|
}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { DEFAULT_CHAT_NODE_TIMEOUT } from '../../utils/defaults.js';
|
|
1
2
|
// https://github.com/openai/openai-node/issues/18#issuecomment-1518715285
|
|
2
3
|
export class EventSourceResponse extends Response {
|
|
3
4
|
name;
|
|
@@ -24,7 +25,7 @@ export class EventSourceResponse extends Response {
|
|
|
24
25
|
const reader = this.streams.eventStream.getReader();
|
|
25
26
|
try {
|
|
26
27
|
while (true) {
|
|
27
|
-
const { done, value } = await this.raceWithTimeout(reader.read());
|
|
28
|
+
const { done, value } = await this.raceWithTimeout(reader.read(), DEFAULT_CHAT_NODE_TIMEOUT);
|
|
28
29
|
if (done) {
|
|
29
30
|
break;
|
|
30
31
|
}
|
|
@@ -36,7 +37,7 @@ export class EventSourceResponse extends Response {
|
|
|
36
37
|
}
|
|
37
38
|
}
|
|
38
39
|
async raceWithTimeout(promise, timeout = 5000) {
|
|
39
|
-
// eslint-disable-next-line no-async-promise-executor -- Error handled correctly
|
|
40
|
+
// eslint-disable-next-line no-async-promise-executor,@typescript-eslint/no-misused-promises -- Error handled correctly
|
|
40
41
|
return new Promise(async (resolve, reject) => {
|
|
41
42
|
const timer = setTimeout(() => {
|
|
42
43
|
reject(new Error('Timeout: API response took too long.'));
|