@librechat/agents-types 1.4.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/graph.ts +126 -0
- package/index.d.ts +1 -0
- package/index.ts +6 -0
- package/llm.ts +38 -0
- package/package.json +12 -0
- package/run.ts +56 -0
- package/stream.ts +174 -0
- package/tools.ts +31 -0
- package/tsconfig.json +14 -0
package/graph.ts
ADDED
@@ -0,0 +1,126 @@
|
|
1
|
+
// src/types/graph.ts
|
2
|
+
import type { StateGraphArgs, StateGraph, CompiledStateGraph } from '@langchain/langgraph';
|
3
|
+
import type { BaseMessage, AIMessageChunk } from '@langchain/core/messages';
|
4
|
+
import type { ChatGenerationChunk } from '@langchain/core/outputs';
|
5
|
+
import type { Graph } from '@/graphs';
|
6
|
+
import { RunnableConfig } from '@langchain/core/runnables';
|
7
|
+
// import type { RunnableConfig } from '@langchain/core/runnables';
|
8
|
+
|
9
|
+
export type BaseGraphState = {
|
10
|
+
messages: BaseMessage[];
|
11
|
+
// [key: string]: unknown;
|
12
|
+
};
|
13
|
+
|
14
|
+
export type IState = BaseGraphState;
|
15
|
+
|
16
|
+
// export interface IState extends BaseGraphState {
|
17
|
+
// instructions?: string;
|
18
|
+
// additional_instructions?: string;
|
19
|
+
// }
|
20
|
+
|
21
|
+
export interface EventHandler {
|
22
|
+
handle(event: string, data: StreamEventData, metadata?: Record<string, unknown>, graph?: Graph): void;
|
23
|
+
}
|
24
|
+
|
25
|
+
export type GraphStateChannels<T extends BaseGraphState> = StateGraphArgs<T>['channels'];
|
26
|
+
|
27
|
+
export type Workflow<T extends BaseGraphState = BaseGraphState, U extends Partial<T> = Partial<T>, N extends string = string> = StateGraph<T, U, N>;
|
28
|
+
|
29
|
+
export type CompiledWorkflow<T extends BaseGraphState = BaseGraphState, U extends Partial<T> = Partial<T>, N extends string = string> = CompiledStateGraph<T, U, N>;
|
30
|
+
|
31
|
+
export type EventStreamCallbackHandlerInput = Parameters<CompiledWorkflow['streamEvents']>[2] extends Omit<infer T, 'autoClose'> ? T : never;
|
32
|
+
|
33
|
+
export type StreamChunk = ChatGenerationChunk & {
|
34
|
+
message: AIMessageChunk;
|
35
|
+
} | AIMessageChunk;
|
36
|
+
|
37
|
+
/**
|
38
|
+
* Data associated with a StreamEvent.
|
39
|
+
*/
|
40
|
+
export type StreamEventData = {
|
41
|
+
/**
|
42
|
+
* The input passed to the runnable that generated the event.
|
43
|
+
* Inputs will sometimes be available at the *START* of the runnable, and
|
44
|
+
* sometimes at the *END* of the runnable.
|
45
|
+
* If a runnable is able to stream its inputs, then its input by definition
|
46
|
+
* won't be known until the *END* of the runnable when it has finished streaming
|
47
|
+
* its inputs.
|
48
|
+
*/
|
49
|
+
input?: unknown;
|
50
|
+
/**
|
51
|
+
* The output of the runnable that generated the event.
|
52
|
+
* Outputs will only be available at the *END* of the runnable.
|
53
|
+
* For most runnables, this field can be inferred from the `chunk` field,
|
54
|
+
* though there might be some exceptions for special cased runnables (e.g., like
|
55
|
+
* chat models), which may return more information.
|
56
|
+
*/
|
57
|
+
output?: unknown;
|
58
|
+
/**
|
59
|
+
* A streaming chunk from the output that generated the event.
|
60
|
+
* chunks support addition in general, and adding them up should result
|
61
|
+
* in the output of the runnable that generated the event.
|
62
|
+
*/
|
63
|
+
chunk?: StreamChunk;
|
64
|
+
/**
|
65
|
+
* Runnable config for invoking other runnables within handlers.
|
66
|
+
*/
|
67
|
+
config?: RunnableConfig;
|
68
|
+
/**
|
69
|
+
* Custom result from the runnable that generated the event.
|
70
|
+
*/
|
71
|
+
result?: unknown;
|
72
|
+
};
|
73
|
+
/**
|
74
|
+
* A streaming event.
|
75
|
+
*
|
76
|
+
* Schema of a streaming event which is produced from the streamEvents method.
|
77
|
+
*/
|
78
|
+
export type StreamEvent = {
|
79
|
+
/**
|
80
|
+
* Event names are of the format: on_[runnable_type]_(start|stream|end).
|
81
|
+
*
|
82
|
+
* Runnable types are one of:
|
83
|
+
* - llm - used by non chat models
|
84
|
+
* - chat_model - used by chat models
|
85
|
+
* - prompt -- e.g., ChatPromptTemplate
|
86
|
+
* - tool -- LangChain tools
|
87
|
+
* - chain - most Runnables are of this type
|
88
|
+
*
|
89
|
+
* Further, the events are categorized as one of:
|
90
|
+
* - start - when the runnable starts
|
91
|
+
* - stream - when the runnable is streaming
|
92
|
+
* - end - when the runnable ends
|
93
|
+
*
|
94
|
+
* start, stream and end are associated with slightly different `data` payload.
|
95
|
+
*
|
96
|
+
* Please see the documentation for `EventData` for more details.
|
97
|
+
*/
|
98
|
+
event: string;
|
99
|
+
/** The name of the runnable that generated the event. */
|
100
|
+
name: string;
|
101
|
+
/**
|
102
|
+
* An randomly generated ID to keep track of the execution of the given runnable.
|
103
|
+
*
|
104
|
+
* Each child runnable that gets invoked as part of the execution of a parent runnable
|
105
|
+
* is assigned its own unique ID.
|
106
|
+
*/
|
107
|
+
run_id: string;
|
108
|
+
/**
|
109
|
+
* Tags associated with the runnable that generated this event.
|
110
|
+
* Tags are always inherited from parent runnables.
|
111
|
+
*/
|
112
|
+
tags?: string[];
|
113
|
+
/** Metadata associated with the runnable that generated this event. */
|
114
|
+
metadata: Record<string, unknown>;
|
115
|
+
/**
|
116
|
+
* Event data.
|
117
|
+
*
|
118
|
+
* The contents of the event data depend on the event type.
|
119
|
+
*/
|
120
|
+
data: StreamEventData;
|
121
|
+
};
|
122
|
+
|
123
|
+
export type GraphConfig = {
|
124
|
+
provider: string;
|
125
|
+
thread_id?: string;
|
126
|
+
};
|
package/index.d.ts
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
export * from '../src/types';
|
package/index.ts
ADDED
package/llm.ts
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
// src/types/llm.ts
|
2
|
+
import { ChatOpenAI } from '@langchain/openai';
|
3
|
+
import { ChatAnthropic } from '@langchain/anthropic';
|
4
|
+
import { ChatMistralAI } from '@langchain/mistralai';
|
5
|
+
import { ChatBedrockConverse } from '@langchain/aws';
|
6
|
+
import { ChatVertexAI } from '@langchain/google-vertexai';
|
7
|
+
import { BedrockChat, BedrockChatFields } from '@langchain/community/chat_models/bedrock/web';
|
8
|
+
import type { ChatVertexAIInput } from '@langchain/google-vertexai';
|
9
|
+
import type { ChatBedrockConverseInput } from '@langchain/aws';
|
10
|
+
import type { ChatMistralAIInput } from '@langchain/mistralai';
|
11
|
+
import type { AnthropicInput } from '@langchain/anthropic';
|
12
|
+
import type { OpenAIInput } from '@langchain/openai';
|
13
|
+
import { Providers } from '@/common';
|
14
|
+
|
15
|
+
export type OpenAIClientOptions = Partial<OpenAIInput>;
|
16
|
+
export type AnthropicClientOptions = Partial<AnthropicInput>;
|
17
|
+
export type MistralAIClientOptions = Partial<ChatMistralAIInput>;
|
18
|
+
export type VertexAIClientOptions = Partial<ChatVertexAIInput>;
|
19
|
+
export type BedrockClientOptions = Partial<BedrockChatFields>;
|
20
|
+
export type BedrockConverseClientOptions = Partial<ChatBedrockConverseInput>;
|
21
|
+
|
22
|
+
export type ClientOptions =
|
23
|
+
| OpenAIClientOptions
|
24
|
+
| AnthropicClientOptions
|
25
|
+
| MistralAIClientOptions
|
26
|
+
| VertexAIClientOptions
|
27
|
+
| BedrockClientOptions
|
28
|
+
| BedrockConverseClientOptions;
|
29
|
+
|
30
|
+
export type ChatModel = typeof ChatAnthropic | typeof ChatOpenAI | typeof ChatMistralAI | typeof ChatVertexAI | typeof BedrockChat | typeof ChatBedrockConverse;
|
31
|
+
|
32
|
+
export type LLMConfig = {
|
33
|
+
provider: Providers;
|
34
|
+
} & ClientOptions;
|
35
|
+
|
36
|
+
export type ChatModelInstance = ChatOpenAI | ChatAnthropic | ChatMistralAI | ChatVertexAI | BedrockChat | ChatBedrockConverse;
|
37
|
+
|
38
|
+
export type ChatModelConstructor = new (config: ClientOptions) => ChatModelInstance;
|
package/package.json
ADDED
package/run.ts
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
// src/types/run.ts
|
2
|
+
import type * as z from 'zod';
|
3
|
+
import type { BaseMessage } from '@langchain/core/messages';
|
4
|
+
import type { StructuredTool } from '@langchain/core/tools';
|
5
|
+
import type * as e from '@/common/enum';
|
6
|
+
import type * as g from '@/types/graph';
|
7
|
+
import type * as t from '@/types/tools';
|
8
|
+
import type * as l from '@/types/llm';
|
9
|
+
|
10
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
11
|
+
export type ZodObjectAny = z.ZodObject<any, any, any, any>;
|
12
|
+
|
13
|
+
export type StandardGraphConfig = {
|
14
|
+
runId?: string;
|
15
|
+
type?: 'standard';
|
16
|
+
provider?: e.Providers;
|
17
|
+
instructions?: string;
|
18
|
+
llmConfig: l.LLMConfig;
|
19
|
+
tools?: t.GenericTool[];
|
20
|
+
toolMap?: t.ToolMap;
|
21
|
+
additional_instructions?: string;
|
22
|
+
clientOptions?: Record<string, unknown>;
|
23
|
+
};
|
24
|
+
|
25
|
+
export interface AgentStateChannels {
|
26
|
+
messages: BaseMessage[];
|
27
|
+
next: string;
|
28
|
+
[key: string]: unknown;
|
29
|
+
instructions?: string;
|
30
|
+
additional_instructions?: string;
|
31
|
+
}
|
32
|
+
|
33
|
+
export interface Member {
|
34
|
+
name: string;
|
35
|
+
systemPrompt: string;
|
36
|
+
tools: StructuredTool[];
|
37
|
+
llmConfig: l.LLMConfig;
|
38
|
+
}
|
39
|
+
|
40
|
+
export type CollaborativeGraphConfig = {
|
41
|
+
type: 'collaborative';
|
42
|
+
members: Member[];
|
43
|
+
supervisorConfig: { systemPrompt?: string; llmConfig: l.LLMConfig };
|
44
|
+
};
|
45
|
+
|
46
|
+
export type TaskManagerGraphConfig = {
|
47
|
+
type: 'taskmanager';
|
48
|
+
members: Member[];
|
49
|
+
supervisorConfig: { systemPrompt?: string; llmConfig: l.LLMConfig };
|
50
|
+
};
|
51
|
+
|
52
|
+
export type RunConfig = {
|
53
|
+
graphConfig: StandardGraphConfig | CollaborativeGraphConfig | TaskManagerGraphConfig;
|
54
|
+
customHandlers?: Record<string, g.EventHandler>;
|
55
|
+
runId?: string;
|
56
|
+
};
|
package/stream.ts
ADDED
@@ -0,0 +1,174 @@
|
|
1
|
+
// src/types/stream.ts
|
2
|
+
import type { MessageContentImageUrl, MessageContentText, ToolMessage } from '@langchain/core/messages';
|
3
|
+
import type { ToolCall, ToolCallChunk } from '@langchain/core/messages/tool';
|
4
|
+
import { StepTypes } from '@/common/enum';
|
5
|
+
|
6
|
+
/** Event names are of the format: on_[runnable_type]_(start|stream|end).
|
7
|
+
|
8
|
+
Runnable types are one of:
|
9
|
+
|
10
|
+
llm - used by non chat models
|
11
|
+
chat_model - used by chat models
|
12
|
+
prompt -- e.g., ChatPromptTemplate
|
13
|
+
tool -- LangChain tools
|
14
|
+
chain - most Runnables are of this type
|
15
|
+
Further, the events are categorized as one of:
|
16
|
+
|
17
|
+
start - when the runnable starts
|
18
|
+
stream - when the runnable is streaming
|
19
|
+
end - when the runnable ends
|
20
|
+
start, stream and end are associated with slightly different data payload.
|
21
|
+
|
22
|
+
Please see the documentation for EventData for more details. */
|
23
|
+
export type EventName = string;
|
24
|
+
|
25
|
+
export type RunStep = {
|
26
|
+
// id: string;
|
27
|
+
// object: 'thread.run.step'; // Updated from 'run.step' # missing
|
28
|
+
// created_at: number;
|
29
|
+
// run_id: string;
|
30
|
+
// assistant_id: string;
|
31
|
+
// thread_id: string;
|
32
|
+
type: StepTypes;
|
33
|
+
// status: 'in_progress' | 'completed' | 'failed' | 'cancelled'; // Add other possible status values if needed
|
34
|
+
// cancelled_at: number | null;
|
35
|
+
// completed_at: number | null;
|
36
|
+
// expires_at: number;
|
37
|
+
// failed_at: number | null;
|
38
|
+
// last_error: string | null;
|
39
|
+
id: string; // #new
|
40
|
+
runId?: string; // #new
|
41
|
+
index: number; // #new
|
42
|
+
stepIndex?: number; // #new
|
43
|
+
stepDetails: StepDetails;
|
44
|
+
usage: null | {
|
45
|
+
// Define usage structure if it's ever non-null
|
46
|
+
// prompt_tokens: number; // #new
|
47
|
+
// completion_tokens: number; // #new
|
48
|
+
// total_tokens: number; // #new
|
49
|
+
};
|
50
|
+
};
|
51
|
+
|
52
|
+
/**
|
53
|
+
* Represents a run step delta i.e. any changed fields on a run step during
|
54
|
+
* streaming.
|
55
|
+
*/
|
56
|
+
export interface RunStepDeltaEvent {
|
57
|
+
/**
|
58
|
+
* The identifier of the run step, which can be referenced in API endpoints.
|
59
|
+
*/
|
60
|
+
id: string;
|
61
|
+
/**
|
62
|
+
* The delta containing the fields that have changed on the run step.
|
63
|
+
*/
|
64
|
+
delta: ToolCallDelta;
|
65
|
+
}
|
66
|
+
|
67
|
+
export type StepDetails =
|
68
|
+
| MessageCreationDetails
|
69
|
+
| ToolCallsDetails;
|
70
|
+
|
71
|
+
export type StepCompleted = ToolCallCompleted;
|
72
|
+
|
73
|
+
export type MessageCreationDetails = {
|
74
|
+
type: StepTypes.MESSAGE_CREATION;
|
75
|
+
message_creation: {
|
76
|
+
message_id: string;
|
77
|
+
};
|
78
|
+
};
|
79
|
+
|
80
|
+
export type ToolEndData = { input: string | Record<string, unknown>, output: ToolMessage };
|
81
|
+
|
82
|
+
export type ProcessedToolCall = {
|
83
|
+
name: string;
|
84
|
+
args: string | Record<string, unknown>;
|
85
|
+
id: string;
|
86
|
+
output: string;
|
87
|
+
progress: number;
|
88
|
+
};
|
89
|
+
|
90
|
+
export type ProcessedContent = {
|
91
|
+
type: ContentType;
|
92
|
+
text?: string;
|
93
|
+
tool_call?: ProcessedToolCall;
|
94
|
+
};
|
95
|
+
|
96
|
+
export type ToolCallCompleted = {
|
97
|
+
type: 'tool_call';
|
98
|
+
tool_call: ProcessedToolCall;
|
99
|
+
};
|
100
|
+
|
101
|
+
export type ToolCompleteEvent = ToolCallCompleted & {
|
102
|
+
/** The Step Id of the Tool Call */
|
103
|
+
id: string;
|
104
|
+
/** The content index of the tool call */
|
105
|
+
index: number;
|
106
|
+
type: 'tool_call';
|
107
|
+
};
|
108
|
+
|
109
|
+
export type ToolCallsDetails = {
|
110
|
+
type: StepTypes.TOOL_CALLS;
|
111
|
+
tool_calls: AgentToolCall[]; // #new
|
112
|
+
};
|
113
|
+
|
114
|
+
export type ToolCallDelta = {
|
115
|
+
type: StepTypes.TOOL_CALLS;
|
116
|
+
tool_calls: ToolCallChunk[]; // #new
|
117
|
+
};
|
118
|
+
|
119
|
+
export type AgentToolCall = {
|
120
|
+
id: string; // #new
|
121
|
+
type: 'function'; // #new
|
122
|
+
function: {
|
123
|
+
name: string; // #new
|
124
|
+
arguments: string | object; // JSON string // #new
|
125
|
+
};
|
126
|
+
} | ToolCall;
|
127
|
+
|
128
|
+
export interface ExtendedMessageContent {
|
129
|
+
type?: string;
|
130
|
+
text?: string;
|
131
|
+
input?: string;
|
132
|
+
index?: number;
|
133
|
+
id?: string;
|
134
|
+
name?: string;
|
135
|
+
}
|
136
|
+
|
137
|
+
/**
|
138
|
+
* Represents a message delta i.e. any changed fields on a message during
|
139
|
+
* streaming.
|
140
|
+
*/
|
141
|
+
export interface MessageDeltaEvent {
|
142
|
+
/**
|
143
|
+
* The identifier of the message, which can be referenced in API endpoints.
|
144
|
+
*/
|
145
|
+
id: string;
|
146
|
+
|
147
|
+
/**
|
148
|
+
* The delta containing the fields that have changed on the Message.
|
149
|
+
*/
|
150
|
+
delta: MessageDelta;
|
151
|
+
}
|
152
|
+
|
153
|
+
/**
|
154
|
+
* The delta containing the fields that have changed on the Message.
|
155
|
+
*/
|
156
|
+
export interface MessageDelta {
|
157
|
+
/**
|
158
|
+
* The content of the message in array of text and/or images.
|
159
|
+
*/
|
160
|
+
content?: MessageContentComplex[];
|
161
|
+
}
|
162
|
+
|
163
|
+
export type ContentType = 'text' | 'image_url' | 'tool_call' | string;
|
164
|
+
|
165
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
166
|
+
export type MessageContentComplex = (MessageContentText | MessageContentImageUrl | (Record<string, any> & {
|
167
|
+
type?: 'text' | 'image_url' | string;
|
168
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
169
|
+
}) | (Record<string, any> & {
|
170
|
+
type?: never;
|
171
|
+
})) & {
|
172
|
+
tool_call_ids?: string[];
|
173
|
+
};
|
174
|
+
// #new
|
package/tools.ts
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
// src/types/tools.ts
|
2
|
+
import type { RunnableToolLike } from '@langchain/core/runnables';
|
3
|
+
import type { StructuredToolInterface } from '@langchain/core/tools';
|
4
|
+
import type { ToolCall } from '@langchain/core/messages/tool';
|
5
|
+
|
6
|
+
/** Replacement type for `import type { ToolCall } from '@langchain/core/messages/tool'` in order to have stringified args typed */
|
7
|
+
export type CustomToolCall = {
|
8
|
+
name: string;
|
9
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
10
|
+
args: string | Record<string, any>;
|
11
|
+
id?: string;
|
12
|
+
type?: 'tool_call';
|
13
|
+
}
|
14
|
+
|
15
|
+
export type GenericTool = StructuredToolInterface | RunnableToolLike;
|
16
|
+
export type ToolMap = Map<string, GenericTool>;
|
17
|
+
export type ToolRefs = {
|
18
|
+
tools: GenericTool[];
|
19
|
+
toolMap?: ToolMap;
|
20
|
+
};
|
21
|
+
|
22
|
+
export type ToolRefGenerator = (tool_calls: ToolCall[]) => ToolRefs;
|
23
|
+
|
24
|
+
export type ToolNodeOptions = {
|
25
|
+
name?: string;
|
26
|
+
tags?: string[];
|
27
|
+
handleToolErrors?: boolean;
|
28
|
+
loadRuntimeTools?: ToolRefGenerator;
|
29
|
+
};
|
30
|
+
|
31
|
+
export type ToolNodeConstructorParams = ToolRefs & ToolNodeOptions;
|
package/tsconfig.json
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
{
|
2
|
+
"compilerOptions": {
|
3
|
+
"module": "commonjs",
|
4
|
+
"lib": ["es6"],
|
5
|
+
"noImplicitAny": true,
|
6
|
+
"noImplicitThis": true,
|
7
|
+
"strictNullChecks": true,
|
8
|
+
"strictFunctionTypes": true,
|
9
|
+
"types": [],
|
10
|
+
"noEmit": true,
|
11
|
+
"forceConsistentCasingInFileNames": true
|
12
|
+
},
|
13
|
+
"include": ["index.d.ts"]
|
14
|
+
}
|