@librechat/agents 2.4.48 → 2.4.50
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/graphs/Graph.cjs.map +1 -1
- package/dist/cjs/llm/google/index.cjs +1 -1
- package/dist/cjs/llm/google/index.cjs.map +1 -1
- package/dist/cjs/llm/google/utils/common.cjs +11 -7
- package/dist/cjs/llm/google/utils/common.cjs.map +1 -1
- package/dist/cjs/run.cjs +4 -3
- package/dist/cjs/run.cjs.map +1 -1
- package/dist/cjs/stream.cjs +24 -5
- package/dist/cjs/stream.cjs.map +1 -1
- package/dist/esm/graphs/Graph.mjs.map +1 -1
- package/dist/esm/llm/google/index.mjs +1 -1
- package/dist/esm/llm/google/index.mjs.map +1 -1
- package/dist/esm/llm/google/utils/common.mjs +11 -7
- package/dist/esm/llm/google/utils/common.mjs.map +1 -1
- package/dist/esm/run.mjs +4 -3
- package/dist/esm/run.mjs.map +1 -1
- package/dist/esm/stream.mjs +25 -6
- package/dist/esm/stream.mjs.map +1 -1
- package/dist/types/graphs/Graph.d.ts +1 -1
- package/dist/types/llm/google/utils/common.d.ts +2 -2
- package/dist/types/stream.d.ts +2 -1
- package/dist/types/types/graph.d.ts +8 -5
- package/dist/types/types/tools.d.ts +1 -1
- package/package.json +2 -2
- package/src/graphs/Graph.ts +2 -2
- package/src/llm/google/index.ts +1 -1
- package/src/llm/google/utils/common.ts +26 -18
- package/src/run.ts +7 -4
- package/src/stream.ts +64 -7
- package/src/types/graph.ts +119 -89
- package/src/types/tools.ts +16 -10
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import type { StateGraphArgs, StateGraph, CompiledStateGraph } from '@langchain/langgraph';
|
|
2
|
+
import type { BindToolsInput } from '@langchain/core/language_models/chat_models';
|
|
2
3
|
import type { BaseMessage, AIMessageChunk } from '@langchain/core/messages';
|
|
3
4
|
import type { ChatGenerationChunk } from '@langchain/core/outputs';
|
|
5
|
+
import type { GoogleAIToolType } from '@langchain/google-common';
|
|
4
6
|
import type { RunnableConfig } from '@langchain/core/runnables';
|
|
5
7
|
import type { ToolMap, GenericTool } from '@/types/tools';
|
|
6
8
|
import type { ClientOptions } from '@/types/llm';
|
|
@@ -17,9 +19,9 @@ export type GraphStateChannels<T extends BaseGraphState> = StateGraphArgs<T>['ch
|
|
|
17
19
|
export type Workflow<T extends BaseGraphState = BaseGraphState, U extends Partial<T> = Partial<T>, N extends string = string> = StateGraph<T, U, N>;
|
|
18
20
|
export type CompiledWorkflow<T extends BaseGraphState = BaseGraphState, U extends Partial<T> = Partial<T>, N extends string = string> = CompiledStateGraph<T, U, N>;
|
|
19
21
|
export type EventStreamCallbackHandlerInput = Parameters<CompiledWorkflow['streamEvents']>[2] extends Omit<infer T, 'autoClose'> ? T : never;
|
|
20
|
-
export type StreamChunk = ChatGenerationChunk & {
|
|
22
|
+
export type StreamChunk = (ChatGenerationChunk & {
|
|
21
23
|
message: AIMessageChunk;
|
|
22
|
-
} | AIMessageChunk;
|
|
24
|
+
}) | AIMessageChunk;
|
|
23
25
|
/**
|
|
24
26
|
* Data associated with a StreamEvent.
|
|
25
27
|
*/
|
|
@@ -117,9 +119,10 @@ export type PartMetadata = {
|
|
|
117
119
|
action?: boolean;
|
|
118
120
|
output?: string;
|
|
119
121
|
};
|
|
120
|
-
export type ModelEndData = StreamEventData & {
|
|
122
|
+
export type ModelEndData = (StreamEventData & {
|
|
121
123
|
output: AIMessageChunk | undefined;
|
|
122
|
-
} | undefined;
|
|
124
|
+
}) | undefined;
|
|
125
|
+
export type GraphTools = GenericTool[] | BindToolsInput[] | GoogleAIToolType[];
|
|
123
126
|
export type StandardGraphInput = {
|
|
124
127
|
runId?: string;
|
|
125
128
|
toolEnd?: boolean;
|
|
@@ -128,8 +131,8 @@ export type StandardGraphInput = {
|
|
|
128
131
|
signal?: AbortSignal;
|
|
129
132
|
instructions?: string;
|
|
130
133
|
streamBuffer?: number;
|
|
131
|
-
tools?: GenericTool[];
|
|
132
134
|
clientOptions: ClientOptions;
|
|
133
135
|
additional_instructions?: string;
|
|
134
136
|
reasoningKey?: 'reasoning_content' | 'reasoning';
|
|
137
|
+
tools?: GraphTools;
|
|
135
138
|
};
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type { RunnableToolLike } from '@langchain/core/runnables';
|
|
2
1
|
import type { StructuredToolInterface } from '@langchain/core/tools';
|
|
2
|
+
import type { RunnableToolLike } from '@langchain/core/runnables';
|
|
3
3
|
import type { ToolCall } from '@langchain/core/messages/tool';
|
|
4
4
|
import type { ToolErrorData } from './stream';
|
|
5
5
|
import { EnvVar } from '@/common';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@librechat/agents",
|
|
3
|
-
"version": "2.4.
|
|
3
|
+
"version": "2.4.50",
|
|
4
4
|
"main": "./dist/cjs/main.cjs",
|
|
5
5
|
"module": "./dist/esm/main.mjs",
|
|
6
6
|
"types": "./dist/types/index.d.ts",
|
|
@@ -47,7 +47,7 @@
|
|
|
47
47
|
"image": "node -r dotenv/config --loader ./tsconfig-paths-bootstrap.mjs --experimental-specifier-resolution=node ./src/scripts/image.ts --provider 'google' --name 'Jo' --location 'New York, NY'",
|
|
48
48
|
"code_exec_files": "node -r dotenv/config --loader ./tsconfig-paths-bootstrap.mjs --experimental-specifier-resolution=node ./src/scripts/code_exec_files.ts --provider 'openAI' --name 'Jo' --location 'New York, NY'",
|
|
49
49
|
"code_exec_simple": "node -r dotenv/config --loader ./tsconfig-paths-bootstrap.mjs --experimental-specifier-resolution=node ./src/scripts/code_exec_simple.ts --provider 'google' --name 'Jo' --location 'New York, NY'",
|
|
50
|
-
"simple": "node -r dotenv/config --loader ./tsconfig-paths-bootstrap.mjs --experimental-specifier-resolution=node ./src/scripts/simple.ts --provider '
|
|
50
|
+
"simple": "node -r dotenv/config --loader ./tsconfig-paths-bootstrap.mjs --experimental-specifier-resolution=node ./src/scripts/simple.ts --provider 'openAI' --name 'Jo' --location 'New York, NY'",
|
|
51
51
|
"caching": "node -r dotenv/config --loader ./tsconfig-paths-bootstrap.mjs --experimental-specifier-resolution=node ./src/scripts/caching.ts --name 'Jo' --location 'New York, NY'",
|
|
52
52
|
"thinking": "node -r dotenv/config --loader ./tsconfig-paths-bootstrap.mjs --experimental-specifier-resolution=node ./src/scripts/thinking.ts --name 'Jo' --location 'New York, NY'",
|
|
53
53
|
"memory": "node -r dotenv/config --loader ./tsconfig-paths-bootstrap.mjs --experimental-specifier-resolution=node ./src/scripts/memory.ts --provider 'openAI' --name 'Jo' --location 'New York, NY'",
|
package/src/graphs/Graph.ts
CHANGED
|
@@ -135,7 +135,7 @@ export class StandardGraph extends Graph<t.BaseGraphState, GraphNode> {
|
|
|
135
135
|
systemMessage: SystemMessage | undefined;
|
|
136
136
|
messages: BaseMessage[] = [];
|
|
137
137
|
runId: string | undefined;
|
|
138
|
-
tools?: t.
|
|
138
|
+
tools?: t.GraphTools;
|
|
139
139
|
toolMap?: t.ToolMap;
|
|
140
140
|
startIndex: number = 0;
|
|
141
141
|
provider: Providers;
|
|
@@ -354,7 +354,7 @@ export class StandardGraph extends Graph<t.BaseGraphState, GraphNode> {
|
|
|
354
354
|
| ToolNode<t.BaseGraphState> {
|
|
355
355
|
// return new ToolNode<t.BaseGraphState>(this.tools);
|
|
356
356
|
return new CustomToolNode<t.BaseGraphState>({
|
|
357
|
-
tools: this.tools || [],
|
|
357
|
+
tools: (this.tools as t.GenericTool[] | undefined) || [],
|
|
358
358
|
toolMap: this.toolMap,
|
|
359
359
|
toolCallStepIds: this.toolCallStepIds,
|
|
360
360
|
errorHandler: (data, metadata) =>
|
package/src/llm/google/index.ts
CHANGED
|
@@ -133,7 +133,7 @@ export class CustomChatGoogleGenerativeAI extends ChatGoogleGenerativeAI {
|
|
|
133
133
|
this.useSystemInstruction
|
|
134
134
|
);
|
|
135
135
|
let actualPrompt = prompt;
|
|
136
|
-
if (prompt[0].role === 'system') {
|
|
136
|
+
if (prompt?.[0].role === 'system') {
|
|
137
137
|
const [systemInstruction] = prompt;
|
|
138
138
|
/** @ts-ignore */
|
|
139
139
|
this.client.systemInstruction = systemInstruction;
|
|
@@ -1,14 +1,15 @@
|
|
|
1
1
|
import {
|
|
2
|
-
EnhancedGenerateContentResponse,
|
|
3
|
-
Content,
|
|
4
|
-
Part,
|
|
5
|
-
type FunctionDeclarationsTool as GoogleGenerativeAIFunctionDeclarationsTool,
|
|
6
|
-
type FunctionDeclaration as GenerativeAIFunctionDeclaration,
|
|
7
2
|
POSSIBLE_ROLES,
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
3
|
+
type Part,
|
|
4
|
+
type Content,
|
|
5
|
+
type TextPart,
|
|
6
|
+
type FileDataPart,
|
|
7
|
+
type InlineDataPart,
|
|
8
|
+
type FunctionCallPart,
|
|
9
|
+
type GenerateContentCandidate,
|
|
10
|
+
type EnhancedGenerateContentResponse,
|
|
11
|
+
type FunctionDeclaration as GenerativeAIFunctionDeclaration,
|
|
12
|
+
type FunctionDeclarationsTool as GoogleGenerativeAIFunctionDeclarationsTool,
|
|
12
13
|
} from '@google/generative-ai';
|
|
13
14
|
import {
|
|
14
15
|
AIMessageChunk,
|
|
@@ -412,9 +413,9 @@ export function convertBaseMessagesToContent(
|
|
|
412
413
|
messages: BaseMessage[],
|
|
413
414
|
isMultimodalModel: boolean,
|
|
414
415
|
convertSystemMessageToHumanContent: boolean = false
|
|
415
|
-
): Content[] {
|
|
416
|
+
): Content[] | undefined {
|
|
416
417
|
return messages.reduce<{
|
|
417
|
-
content: Content[];
|
|
418
|
+
content: Content[] | undefined;
|
|
418
419
|
mergeWithPreviousContent: boolean;
|
|
419
420
|
}>(
|
|
420
421
|
(acc, message, index) => {
|
|
@@ -427,7 +428,7 @@ export function convertBaseMessagesToContent(
|
|
|
427
428
|
}
|
|
428
429
|
const role = convertAuthorToRole(author);
|
|
429
430
|
|
|
430
|
-
const prevContent = acc.content[acc.content.length];
|
|
431
|
+
const prevContent = acc.content?.[acc.content.length];
|
|
431
432
|
if (
|
|
432
433
|
!acc.mergeWithPreviousContent &&
|
|
433
434
|
prevContent &&
|
|
@@ -445,7 +446,7 @@ export function convertBaseMessagesToContent(
|
|
|
445
446
|
);
|
|
446
447
|
|
|
447
448
|
if (acc.mergeWithPreviousContent) {
|
|
448
|
-
const prevContent = acc.content[acc.content.length - 1];
|
|
449
|
+
const prevContent = acc.content?.[acc.content.length - 1];
|
|
449
450
|
if (!prevContent) {
|
|
450
451
|
throw new Error(
|
|
451
452
|
'There was a problem parsing your system message. Please try a prompt without one.'
|
|
@@ -473,7 +474,7 @@ export function convertBaseMessagesToContent(
|
|
|
473
474
|
return {
|
|
474
475
|
mergeWithPreviousContent:
|
|
475
476
|
author === 'system' && !convertSystemMessageToHumanContent,
|
|
476
|
-
content: [...acc.content, content],
|
|
477
|
+
content: [...(acc.content ?? []), content],
|
|
477
478
|
};
|
|
478
479
|
},
|
|
479
480
|
{ content: [], mergeWithPreviousContent: false }
|
|
@@ -491,12 +492,15 @@ export function convertResponseContentToChatGenerationChunk(
|
|
|
491
492
|
return null;
|
|
492
493
|
}
|
|
493
494
|
const functionCalls = response.functionCalls();
|
|
494
|
-
const [candidate] = response.candidates
|
|
495
|
-
|
|
495
|
+
const [candidate] = response.candidates as [
|
|
496
|
+
Partial<GenerateContentCandidate> | undefined,
|
|
497
|
+
];
|
|
498
|
+
const { content: candidateContent, ...generationInfo } = candidate ?? {};
|
|
496
499
|
let content: MessageContent | undefined;
|
|
497
500
|
// Checks if some parts do not have text. If false, it means that the content is a string.
|
|
498
501
|
const reasoningParts: string[] = [];
|
|
499
502
|
if (
|
|
503
|
+
candidateContent != null &&
|
|
500
504
|
Array.isArray(candidateContent.parts) &&
|
|
501
505
|
candidateContent.parts.every((p) => 'text' in p)
|
|
502
506
|
) {
|
|
@@ -510,7 +514,7 @@ export function convertResponseContentToChatGenerationChunk(
|
|
|
510
514
|
textParts.push(part.text ?? '');
|
|
511
515
|
}
|
|
512
516
|
content = textParts.join('');
|
|
513
|
-
} else if (Array.isArray(candidateContent.parts)) {
|
|
517
|
+
} else if (candidateContent && Array.isArray(candidateContent.parts)) {
|
|
514
518
|
content = candidateContent.parts.map((p) => {
|
|
515
519
|
if ('text' in p && 'thought' in p && p.thought === true) {
|
|
516
520
|
reasoningParts.push(p.text ?? '');
|
|
@@ -565,10 +569,14 @@ export function convertResponseContentToChatGenerationChunk(
|
|
|
565
569
|
additional_kwargs.reasoning = reasoningParts.join('');
|
|
566
570
|
}
|
|
567
571
|
|
|
572
|
+
if (candidate?.groundingMetadata) {
|
|
573
|
+
additional_kwargs.groundingMetadata = candidate.groundingMetadata;
|
|
574
|
+
}
|
|
575
|
+
|
|
568
576
|
return new ChatGenerationChunk({
|
|
569
577
|
text,
|
|
570
578
|
message: new AIMessageChunk({
|
|
571
|
-
content: content
|
|
579
|
+
content: content,
|
|
572
580
|
name: !candidateContent ? undefined : candidateContent.role,
|
|
573
581
|
tool_call_chunks: toolCallChunks,
|
|
574
582
|
// Each chunk can have unique "generationInfo", and merging strategy is unclear,
|
package/src/run.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
// src/run.ts
|
|
2
2
|
import { zodToJsonSchema } from 'zod-to-json-schema';
|
|
3
3
|
import { PromptTemplate } from '@langchain/core/prompts';
|
|
4
|
-
import { AzureChatOpenAI, ChatOpenAI } from '@langchain/openai';
|
|
5
4
|
import { SystemMessage } from '@langchain/core/messages';
|
|
5
|
+
import { AzureChatOpenAI, ChatOpenAI } from '@langchain/openai';
|
|
6
6
|
import type {
|
|
7
7
|
BaseMessage,
|
|
8
8
|
MessageContentComplex,
|
|
@@ -147,15 +147,18 @@ export class Run<T extends t.BaseGraphState> {
|
|
|
147
147
|
(streamOptions?.indexTokenCountMap
|
|
148
148
|
? await createTokenCounter()
|
|
149
149
|
: undefined);
|
|
150
|
+
const tools = this.Graph.tools as
|
|
151
|
+
| Array<t.GenericTool | undefined>
|
|
152
|
+
| undefined;
|
|
150
153
|
const toolTokens = tokenCounter
|
|
151
|
-
? (
|
|
154
|
+
? (tools?.reduce((acc, tool) => {
|
|
152
155
|
if (!(tool as Partial<t.GenericTool>).schema) {
|
|
153
156
|
return acc;
|
|
154
157
|
}
|
|
155
158
|
|
|
156
159
|
const jsonSchema = zodToJsonSchema(
|
|
157
|
-
(tool
|
|
158
|
-
tool
|
|
160
|
+
(tool?.schema as t.ZodObjectAny).describe(tool?.description ?? ''),
|
|
161
|
+
tool?.name ?? ''
|
|
159
162
|
);
|
|
160
163
|
return (
|
|
161
164
|
acc + tokenCounter(new SystemMessage(JSON.stringify(jsonSchema)))
|
package/src/stream.ts
CHANGED
|
@@ -2,8 +2,15 @@
|
|
|
2
2
|
import type { AIMessageChunk } from '@langchain/core/messages';
|
|
3
3
|
import type { ToolCall } from '@langchain/core/messages/tool';
|
|
4
4
|
import type { Graph } from '@/graphs';
|
|
5
|
+
import type { ChatOpenAIReasoningSummary } from '@langchain/openai';
|
|
5
6
|
import type * as t from '@/types';
|
|
6
|
-
import {
|
|
7
|
+
import {
|
|
8
|
+
StepTypes,
|
|
9
|
+
ContentTypes,
|
|
10
|
+
GraphEvents,
|
|
11
|
+
ToolCallTypes,
|
|
12
|
+
Providers,
|
|
13
|
+
} from '@/common';
|
|
7
14
|
import { handleToolCalls, handleToolCallChunks } from '@/tools/handlers';
|
|
8
15
|
import { getMessageId } from '@/messages';
|
|
9
16
|
|
|
@@ -66,6 +73,41 @@ function getNonEmptyValue(possibleValues: string[]): string | undefined {
|
|
|
66
73
|
}
|
|
67
74
|
return undefined;
|
|
68
75
|
}
|
|
76
|
+
|
|
77
|
+
function getChunkContent({
|
|
78
|
+
chunk,
|
|
79
|
+
provider,
|
|
80
|
+
reasoningKey,
|
|
81
|
+
}: {
|
|
82
|
+
chunk?: Partial<AIMessageChunk>;
|
|
83
|
+
provider?: Providers;
|
|
84
|
+
reasoningKey: 'reasoning_content' | 'reasoning';
|
|
85
|
+
}): string | t.MessageContentComplex[] | undefined {
|
|
86
|
+
if (
|
|
87
|
+
(provider === Providers.OPENAI || provider === Providers.AZURE) &&
|
|
88
|
+
(
|
|
89
|
+
chunk?.additional_kwargs?.reasoning as
|
|
90
|
+
| Partial<ChatOpenAIReasoningSummary>
|
|
91
|
+
| undefined
|
|
92
|
+
)?.summary?.[0]?.text != null &&
|
|
93
|
+
((
|
|
94
|
+
chunk?.additional_kwargs?.reasoning as
|
|
95
|
+
| Partial<ChatOpenAIReasoningSummary>
|
|
96
|
+
| undefined
|
|
97
|
+
)?.summary?.[0]?.text?.length ?? 0) > 0
|
|
98
|
+
) {
|
|
99
|
+
return (
|
|
100
|
+
chunk?.additional_kwargs?.reasoning as
|
|
101
|
+
| Partial<ChatOpenAIReasoningSummary>
|
|
102
|
+
| undefined
|
|
103
|
+
)?.summary?.[0]?.text;
|
|
104
|
+
}
|
|
105
|
+
return (
|
|
106
|
+
(chunk?.additional_kwargs?.[reasoningKey] as string | undefined) ??
|
|
107
|
+
chunk?.content
|
|
108
|
+
);
|
|
109
|
+
}
|
|
110
|
+
|
|
69
111
|
export class ChatModelStreamHandler implements t.EventHandler {
|
|
70
112
|
handle(
|
|
71
113
|
event: string,
|
|
@@ -85,10 +127,12 @@ export class ChatModelStreamHandler implements t.EventHandler {
|
|
|
85
127
|
}
|
|
86
128
|
|
|
87
129
|
const chunk = data.chunk as Partial<AIMessageChunk>;
|
|
88
|
-
const content =
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
130
|
+
const content = getChunkContent({
|
|
131
|
+
chunk,
|
|
132
|
+
reasoningKey: graph.reasoningKey,
|
|
133
|
+
provider: metadata?.provider as Providers,
|
|
134
|
+
});
|
|
135
|
+
this.handleReasoning(chunk, graph, metadata?.provider as Providers);
|
|
92
136
|
|
|
93
137
|
let hasToolCalls = false;
|
|
94
138
|
if (
|
|
@@ -260,9 +304,14 @@ hasToolCallChunks: ${hasToolCallChunks}
|
|
|
260
304
|
});
|
|
261
305
|
}
|
|
262
306
|
}
|
|
263
|
-
handleReasoning(
|
|
307
|
+
handleReasoning(
|
|
308
|
+
chunk: Partial<AIMessageChunk>,
|
|
309
|
+
graph: Graph,
|
|
310
|
+
provider?: Providers
|
|
311
|
+
): void {
|
|
264
312
|
let reasoning_content = chunk.additional_kwargs?.[graph.reasoningKey] as
|
|
265
313
|
| string
|
|
314
|
+
| Partial<ChatOpenAIReasoningSummary>
|
|
266
315
|
| undefined;
|
|
267
316
|
if (
|
|
268
317
|
Array.isArray(chunk.content) &&
|
|
@@ -271,10 +320,18 @@ hasToolCallChunks: ${hasToolCallChunks}
|
|
|
271
320
|
chunk.content[0]?.type === ContentTypes.REASONING_CONTENT)
|
|
272
321
|
) {
|
|
273
322
|
reasoning_content = 'valid';
|
|
323
|
+
} else if (
|
|
324
|
+
(provider === Providers.OPENAI || provider === Providers.AZURE) &&
|
|
325
|
+
reasoning_content != null &&
|
|
326
|
+
typeof reasoning_content !== 'string' &&
|
|
327
|
+
reasoning_content.summary?.[0]?.text != null &&
|
|
328
|
+
reasoning_content.summary[0].text
|
|
329
|
+
) {
|
|
330
|
+
reasoning_content = 'valid';
|
|
274
331
|
}
|
|
275
332
|
if (
|
|
276
333
|
reasoning_content != null &&
|
|
277
|
-
reasoning_content &&
|
|
334
|
+
reasoning_content !== '' &&
|
|
278
335
|
(chunk.content == null ||
|
|
279
336
|
chunk.content === '' ||
|
|
280
337
|
reasoning_content === 'valid')
|
package/src/types/graph.ts
CHANGED
|
@@ -1,7 +1,13 @@
|
|
|
1
1
|
// src/types/graph.ts
|
|
2
|
-
import type {
|
|
2
|
+
import type {
|
|
3
|
+
StateGraphArgs,
|
|
4
|
+
StateGraph,
|
|
5
|
+
CompiledStateGraph,
|
|
6
|
+
} from '@langchain/langgraph';
|
|
7
|
+
import type { BindToolsInput } from '@langchain/core/language_models/chat_models';
|
|
3
8
|
import type { BaseMessage, AIMessageChunk } from '@langchain/core/messages';
|
|
4
9
|
import type { ChatGenerationChunk } from '@langchain/core/outputs';
|
|
10
|
+
import type { GoogleAIToolType } from '@langchain/google-common';
|
|
5
11
|
import type { RunnableConfig } from '@langchain/core/runnables';
|
|
6
12
|
import type { ToolMap, GenericTool } from '@/types/tools';
|
|
7
13
|
import type { ClientOptions } from '@/types/llm';
|
|
@@ -22,56 +28,78 @@ export type IState = BaseGraphState;
|
|
|
22
28
|
// }
|
|
23
29
|
|
|
24
30
|
export interface EventHandler {
|
|
25
|
-
handle(
|
|
31
|
+
handle(
|
|
32
|
+
event: string,
|
|
33
|
+
data: StreamEventData | ModelEndData,
|
|
34
|
+
metadata?: Record<string, unknown>,
|
|
35
|
+
graph?: Graph
|
|
36
|
+
): void;
|
|
26
37
|
}
|
|
27
38
|
|
|
28
|
-
export type GraphStateChannels<T extends BaseGraphState> =
|
|
39
|
+
export type GraphStateChannels<T extends BaseGraphState> =
|
|
40
|
+
StateGraphArgs<T>['channels'];
|
|
29
41
|
|
|
30
|
-
export type Workflow<
|
|
42
|
+
export type Workflow<
|
|
43
|
+
T extends BaseGraphState = BaseGraphState,
|
|
44
|
+
U extends Partial<T> = Partial<T>,
|
|
45
|
+
N extends string = string,
|
|
46
|
+
> = StateGraph<T, U, N>;
|
|
31
47
|
|
|
32
|
-
export type CompiledWorkflow<
|
|
48
|
+
export type CompiledWorkflow<
|
|
49
|
+
T extends BaseGraphState = BaseGraphState,
|
|
50
|
+
U extends Partial<T> = Partial<T>,
|
|
51
|
+
N extends string = string,
|
|
52
|
+
> = CompiledStateGraph<T, U, N>;
|
|
33
53
|
|
|
34
|
-
export type EventStreamCallbackHandlerInput =
|
|
54
|
+
export type EventStreamCallbackHandlerInput =
|
|
55
|
+
Parameters<CompiledWorkflow['streamEvents']>[2] extends Omit<
|
|
56
|
+
infer T,
|
|
57
|
+
'autoClose'
|
|
58
|
+
>
|
|
59
|
+
? T
|
|
60
|
+
: never;
|
|
35
61
|
|
|
36
|
-
export type StreamChunk =
|
|
37
|
-
|
|
38
|
-
|
|
62
|
+
export type StreamChunk =
|
|
63
|
+
| (ChatGenerationChunk & {
|
|
64
|
+
message: AIMessageChunk;
|
|
65
|
+
})
|
|
66
|
+
| AIMessageChunk;
|
|
39
67
|
|
|
40
68
|
/**
|
|
41
69
|
* Data associated with a StreamEvent.
|
|
42
70
|
*/
|
|
43
71
|
export type StreamEventData = {
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
72
|
+
/**
|
|
73
|
+
* The input passed to the runnable that generated the event.
|
|
74
|
+
* Inputs will sometimes be available at the *START* of the runnable, and
|
|
75
|
+
* sometimes at the *END* of the runnable.
|
|
76
|
+
* If a runnable is able to stream its inputs, then its input by definition
|
|
77
|
+
* won't be known until the *END* of the runnable when it has finished streaming
|
|
78
|
+
* its inputs.
|
|
79
|
+
*/
|
|
80
|
+
input?: unknown;
|
|
81
|
+
/**
|
|
82
|
+
* The output of the runnable that generated the event.
|
|
83
|
+
* Outputs will only be available at the *END* of the runnable.
|
|
84
|
+
* For most runnables, this field can be inferred from the `chunk` field,
|
|
85
|
+
* though there might be some exceptions for special cased runnables (e.g., like
|
|
86
|
+
* chat models), which may return more information.
|
|
87
|
+
*/
|
|
88
|
+
output?: unknown;
|
|
89
|
+
/**
|
|
90
|
+
* A streaming chunk from the output that generated the event.
|
|
91
|
+
* chunks support addition in general, and adding them up should result
|
|
92
|
+
* in the output of the runnable that generated the event.
|
|
93
|
+
*/
|
|
94
|
+
chunk?: StreamChunk;
|
|
95
|
+
/**
|
|
96
|
+
* Runnable config for invoking other runnables within handlers.
|
|
97
|
+
*/
|
|
98
|
+
config?: RunnableConfig;
|
|
99
|
+
/**
|
|
100
|
+
* Custom result from the runnable that generated the event.
|
|
101
|
+
*/
|
|
102
|
+
result?: unknown;
|
|
75
103
|
};
|
|
76
104
|
|
|
77
105
|
/**
|
|
@@ -80,54 +108,54 @@ export type StreamEventData = {
|
|
|
80
108
|
* Schema of a streaming event which is produced from the streamEvents method.
|
|
81
109
|
*/
|
|
82
110
|
export type StreamEvent = {
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
111
|
+
/**
|
|
112
|
+
* Event names are of the format: on_[runnable_type]_(start|stream|end).
|
|
113
|
+
*
|
|
114
|
+
* Runnable types are one of:
|
|
115
|
+
* - llm - used by non chat models
|
|
116
|
+
* - chat_model - used by chat models
|
|
117
|
+
* - prompt -- e.g., ChatPromptTemplate
|
|
118
|
+
* - tool -- LangChain tools
|
|
119
|
+
* - chain - most Runnables are of this type
|
|
120
|
+
*
|
|
121
|
+
* Further, the events are categorized as one of:
|
|
122
|
+
* - start - when the runnable starts
|
|
123
|
+
* - stream - when the runnable is streaming
|
|
124
|
+
* - end - when the runnable ends
|
|
125
|
+
*
|
|
126
|
+
* start, stream and end are associated with slightly different `data` payload.
|
|
127
|
+
*
|
|
128
|
+
* Please see the documentation for `EventData` for more details.
|
|
129
|
+
*/
|
|
130
|
+
event: string;
|
|
131
|
+
/** The name of the runnable that generated the event. */
|
|
132
|
+
name: string;
|
|
133
|
+
/**
|
|
134
|
+
* An randomly generated ID to keep track of the execution of the given runnable.
|
|
135
|
+
*
|
|
136
|
+
* Each child runnable that gets invoked as part of the execution of a parent runnable
|
|
137
|
+
* is assigned its own unique ID.
|
|
138
|
+
*/
|
|
139
|
+
run_id: string;
|
|
140
|
+
/**
|
|
141
|
+
* Tags associated with the runnable that generated this event.
|
|
142
|
+
* Tags are always inherited from parent runnables.
|
|
143
|
+
*/
|
|
144
|
+
tags?: string[];
|
|
145
|
+
/** Metadata associated with the runnable that generated this event. */
|
|
146
|
+
metadata: Record<string, unknown>;
|
|
147
|
+
/**
|
|
148
|
+
* Event data.
|
|
149
|
+
*
|
|
150
|
+
* The contents of the event data depend on the event type.
|
|
151
|
+
*/
|
|
152
|
+
data: StreamEventData;
|
|
125
153
|
};
|
|
126
154
|
|
|
127
155
|
export type GraphConfig = {
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
156
|
+
provider: string;
|
|
157
|
+
thread_id?: string;
|
|
158
|
+
run_id?: string;
|
|
131
159
|
};
|
|
132
160
|
|
|
133
161
|
export type PartMetadata = {
|
|
@@ -138,8 +166,10 @@ export type PartMetadata = {
|
|
|
138
166
|
output?: string;
|
|
139
167
|
};
|
|
140
168
|
|
|
141
|
-
export type ModelEndData =
|
|
142
|
-
|
|
169
|
+
export type ModelEndData =
|
|
170
|
+
| (StreamEventData & { output: AIMessageChunk | undefined })
|
|
171
|
+
| undefined;
|
|
172
|
+
export type GraphTools = GenericTool[] | BindToolsInput[] | GoogleAIToolType[];
|
|
143
173
|
export type StandardGraphInput = {
|
|
144
174
|
runId?: string;
|
|
145
175
|
toolEnd?: boolean;
|
|
@@ -148,8 +178,8 @@ export type StandardGraphInput = {
|
|
|
148
178
|
signal?: AbortSignal;
|
|
149
179
|
instructions?: string;
|
|
150
180
|
streamBuffer?: number;
|
|
151
|
-
tools?: GenericTool[];
|
|
152
181
|
clientOptions: ClientOptions;
|
|
153
182
|
additional_instructions?: string;
|
|
154
183
|
reasoningKey?: 'reasoning_content' | 'reasoning';
|
|
155
|
-
|
|
184
|
+
tools?: GraphTools;
|
|
185
|
+
};
|
package/src/types/tools.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// src/types/tools.ts
|
|
2
|
-
import type { RunnableToolLike } from '@langchain/core/runnables';
|
|
3
2
|
import type { StructuredToolInterface } from '@langchain/core/tools';
|
|
3
|
+
import type { RunnableToolLike } from '@langchain/core/runnables';
|
|
4
4
|
import type { ToolCall } from '@langchain/core/messages/tool';
|
|
5
5
|
import type { ToolErrorData } from './stream';
|
|
6
6
|
import { EnvVar } from '@/common';
|
|
@@ -13,9 +13,10 @@ export type CustomToolCall = {
|
|
|
13
13
|
id?: string;
|
|
14
14
|
type?: 'tool_call';
|
|
15
15
|
output?: string;
|
|
16
|
-
}
|
|
16
|
+
};
|
|
17
17
|
|
|
18
18
|
export type GenericTool = StructuredToolInterface | RunnableToolLike;
|
|
19
|
+
|
|
19
20
|
export type ToolMap = Map<string, GenericTool>;
|
|
20
21
|
export type ToolRefs = {
|
|
21
22
|
tools: GenericTool[];
|
|
@@ -30,7 +31,10 @@ export type ToolNodeOptions = {
|
|
|
30
31
|
handleToolErrors?: boolean;
|
|
31
32
|
loadRuntimeTools?: ToolRefGenerator;
|
|
32
33
|
toolCallStepIds?: Map<string, string>;
|
|
33
|
-
errorHandler?: (
|
|
34
|
+
errorHandler?: (
|
|
35
|
+
data: ToolErrorData,
|
|
36
|
+
metadata?: Record<string, unknown>
|
|
37
|
+
) => void;
|
|
34
38
|
};
|
|
35
39
|
|
|
36
40
|
export type ToolNodeConstructorParams = ToolRefs & ToolNodeOptions;
|
|
@@ -50,13 +54,15 @@ export type CodeEnvFile = {
|
|
|
50
54
|
session_id: string;
|
|
51
55
|
};
|
|
52
56
|
|
|
53
|
-
export type CodeExecutionToolParams =
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
57
|
+
export type CodeExecutionToolParams =
|
|
58
|
+
| undefined
|
|
59
|
+
| {
|
|
60
|
+
session_id?: string;
|
|
61
|
+
user_id?: string;
|
|
62
|
+
apiKey?: string;
|
|
63
|
+
files?: CodeEnvFile[];
|
|
64
|
+
[EnvVar.CODE_API_KEY]?: string;
|
|
65
|
+
};
|
|
60
66
|
|
|
61
67
|
export type FileRef = {
|
|
62
68
|
id: string;
|