@librechat/agents 2.4.75 → 2.4.76

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.
@@ -0,0 +1,89 @@
1
+ import { AIMessageChunk } from '@langchain/core/messages';
2
+ import { ChatGenerationChunk } from '@langchain/core/outputs';
3
+ import { ChatOllama as BaseChatOllama } from '@langchain/ollama';
4
+ import { CallbackManagerForLLMRun } from '@langchain/core/callbacks/manager';
5
+ import type {
6
+ ChatResponse as OllamaChatResponse,
7
+ Message as OllamaMessage,
8
+ } from 'ollama';
9
+ import type { UsageMetadata, BaseMessage } from '@langchain/core/messages';
10
+ import {
11
+ convertOllamaMessagesToLangChain,
12
+ convertToOllamaMessages,
13
+ } from './utils';
14
+
15
+ export class ChatOllama extends BaseChatOllama {
16
+ async *_streamResponseChunks(
17
+ messages: BaseMessage[],
18
+ options: this['ParsedCallOptions'],
19
+ runManager?: CallbackManagerForLLMRun
20
+ ): AsyncGenerator<ChatGenerationChunk> {
21
+ if (this.checkOrPullModel) {
22
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
23
+ // @ts-ignore
24
+ if (!((await this.checkModelExistsOnMachine(this.model)) as boolean)) {
25
+ await this.pull(this.model, {
26
+ logProgress: true,
27
+ });
28
+ }
29
+ }
30
+
31
+ const params = this.invocationParams(options);
32
+ // TODO: remove cast after SDK adds support for tool calls
33
+ const ollamaMessages = convertToOllamaMessages(messages) as OllamaMessage[];
34
+
35
+ const usageMetadata: UsageMetadata = {
36
+ input_tokens: 0,
37
+ output_tokens: 0,
38
+ total_tokens: 0,
39
+ };
40
+
41
+ const stream = await this.client.chat({
42
+ ...params,
43
+ messages: ollamaMessages,
44
+ stream: true,
45
+ });
46
+
47
+ let lastMetadata: Omit<OllamaChatResponse, 'message'> | undefined;
48
+
49
+ for await (const chunk of stream) {
50
+ if (options.signal?.aborted === true) {
51
+ this.client.abort();
52
+ }
53
+ const { message: responseMessage, ...rest } =
54
+ chunk as Partial<OllamaChatResponse>;
55
+ usageMetadata.input_tokens += rest.prompt_eval_count ?? 0;
56
+ usageMetadata.output_tokens += rest.eval_count ?? 0;
57
+ usageMetadata.total_tokens =
58
+ usageMetadata.input_tokens + usageMetadata.output_tokens;
59
+ lastMetadata = rest as Omit<OllamaChatResponse, 'message'>;
60
+ if (!responseMessage) {
61
+ continue;
62
+ }
63
+ const message = convertOllamaMessagesToLangChain(responseMessage);
64
+ const generationChunk = new ChatGenerationChunk({
65
+ text: responseMessage.content || '',
66
+ message,
67
+ });
68
+ yield generationChunk;
69
+ await runManager?.handleLLMNewToken(
70
+ responseMessage.content || '',
71
+ undefined,
72
+ undefined,
73
+ undefined,
74
+ undefined,
75
+ { chunk: generationChunk }
76
+ );
77
+ }
78
+
79
+ // Yield the `response_metadata` as the final chunk.
80
+ yield new ChatGenerationChunk({
81
+ text: '',
82
+ message: new AIMessageChunk({
83
+ content: '',
84
+ response_metadata: lastMetadata,
85
+ usage_metadata: usageMetadata,
86
+ }),
87
+ });
88
+ }
89
+ }
@@ -0,0 +1,193 @@
1
+ import {
2
+ AIMessage,
3
+ AIMessageChunk,
4
+ BaseMessage,
5
+ HumanMessage,
6
+ MessageContentText,
7
+ SystemMessage,
8
+ ToolMessage,
9
+ UsageMetadata,
10
+ } from '@langchain/core/messages';
11
+ import type {
12
+ Message as OllamaMessage,
13
+ ToolCall as OllamaToolCall,
14
+ } from 'ollama';
15
+ import { v4 as uuidv4 } from 'uuid';
16
+
17
+ export function convertOllamaMessagesToLangChain(
18
+ messages: OllamaMessage,
19
+ extra?: {
20
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
21
+ responseMetadata?: Record<string, any>;
22
+ usageMetadata?: UsageMetadata;
23
+ }
24
+ ): AIMessageChunk {
25
+ const additional_kwargs: BaseMessage['additional_kwargs'] = {};
26
+ if ('thinking' in messages) {
27
+ additional_kwargs.reasoning_content = messages.thinking as string;
28
+ }
29
+ return new AIMessageChunk({
30
+ content: messages.content || '',
31
+ tool_call_chunks: messages.tool_calls?.map((tc) => ({
32
+ name: tc.function.name,
33
+ args: JSON.stringify(tc.function.arguments),
34
+ type: 'tool_call_chunk',
35
+ index: 0,
36
+ id: uuidv4(),
37
+ })),
38
+ response_metadata: extra?.responseMetadata,
39
+ usage_metadata: extra?.usageMetadata,
40
+ additional_kwargs,
41
+ });
42
+ }
43
+
44
+ function extractBase64FromDataUrl(dataUrl: string): string {
45
+ const match = dataUrl.match(/^data:.*?;base64,(.*)$/);
46
+ return match ? match[1] : '';
47
+ }
48
+
49
+ function convertAMessagesToOllama(messages: AIMessage): OllamaMessage[] {
50
+ if (typeof messages.content === 'string') {
51
+ return [
52
+ {
53
+ role: 'assistant',
54
+ content: messages.content,
55
+ },
56
+ ];
57
+ }
58
+
59
+ const textFields = messages.content.filter(
60
+ (c) => c.type === 'text' && typeof c.text === 'string'
61
+ );
62
+ const textMessages = (textFields as MessageContentText[]).map((c) => ({
63
+ role: 'assistant',
64
+ content: c.text,
65
+ }));
66
+ let toolCallMsgs: OllamaMessage | undefined;
67
+
68
+ if (
69
+ messages.content.find((c) => c.type === 'tool_use') &&
70
+ messages.tool_calls?.length
71
+ ) {
72
+ // `tool_use` content types are accepted if the message has tool calls
73
+ const toolCalls: OllamaToolCall[] | undefined = messages.tool_calls.map(
74
+ (tc) => ({
75
+ id: tc.id,
76
+ type: 'function',
77
+ function: {
78
+ name: tc.name,
79
+ arguments: tc.args,
80
+ },
81
+ })
82
+ );
83
+
84
+ if (toolCalls) {
85
+ toolCallMsgs = {
86
+ role: 'assistant',
87
+ tool_calls: toolCalls,
88
+ content: '',
89
+ };
90
+ }
91
+ } else if (
92
+ messages.content.find((c) => c.type === 'tool_use') &&
93
+ !messages.tool_calls?.length
94
+ ) {
95
+ throw new Error(
96
+ '\'tool_use\' content type is not supported without tool calls.'
97
+ );
98
+ }
99
+
100
+ return [...textMessages, ...(toolCallMsgs ? [toolCallMsgs] : [])];
101
+ }
102
+
103
+ function convertHumanGenericMessagesToOllama(
104
+ message: HumanMessage
105
+ ): OllamaMessage[] {
106
+ if (typeof message.content === 'string') {
107
+ return [
108
+ {
109
+ role: 'user',
110
+ content: message.content,
111
+ },
112
+ ];
113
+ }
114
+ return message.content.map((c) => {
115
+ if (c.type === 'text') {
116
+ return {
117
+ role: 'user',
118
+ content: c.text,
119
+ };
120
+ } else if (c.type === 'image_url') {
121
+ if (typeof c.image_url === 'string') {
122
+ return {
123
+ role: 'user',
124
+ content: '',
125
+ images: [extractBase64FromDataUrl(c.image_url)],
126
+ };
127
+ } else if (c.image_url.url && typeof c.image_url.url === 'string') {
128
+ return {
129
+ role: 'user',
130
+ content: '',
131
+ images: [extractBase64FromDataUrl(c.image_url.url)],
132
+ };
133
+ }
134
+ }
135
+ throw new Error(`Unsupported content type: ${c.type}`);
136
+ });
137
+ }
138
+
139
+ function convertSystemMessageToOllama(message: SystemMessage): OllamaMessage[] {
140
+ if (typeof message.content === 'string') {
141
+ return [
142
+ {
143
+ role: 'system',
144
+ content: message.content,
145
+ },
146
+ ];
147
+ } else if (
148
+ message.content.every(
149
+ (c) => c.type === 'text' && typeof c.text === 'string'
150
+ )
151
+ ) {
152
+ return (message.content as MessageContentText[]).map((c) => ({
153
+ role: 'system',
154
+ content: c.text,
155
+ }));
156
+ } else {
157
+ throw new Error(
158
+ `Unsupported content type(s): ${message.content
159
+ .map((c) => c.type)
160
+ .join(', ')}`
161
+ );
162
+ }
163
+ }
164
+
165
+ function convertToolMessageToOllama(message: ToolMessage): OllamaMessage[] {
166
+ if (typeof message.content !== 'string') {
167
+ throw new Error('Non string tool message content is not supported');
168
+ }
169
+ return [
170
+ {
171
+ role: 'tool',
172
+ content: message.content,
173
+ },
174
+ ];
175
+ }
176
+
177
+ export function convertToOllamaMessages(
178
+ messages: BaseMessage[]
179
+ ): OllamaMessage[] {
180
+ return messages.flatMap((msg) => {
181
+ if (['human', 'generic'].includes(msg._getType())) {
182
+ return convertHumanGenericMessagesToOllama(msg);
183
+ } else if (msg._getType() === 'ai') {
184
+ return convertAMessagesToOllama(msg);
185
+ } else if (msg._getType() === 'system') {
186
+ return convertSystemMessageToOllama(msg);
187
+ } else if (msg._getType() === 'tool') {
188
+ return convertToolMessageToOllama(msg as ToolMessage);
189
+ } else {
190
+ throw new Error(`Unsupported message type: ${msg._getType()}`);
191
+ }
192
+ });
193
+ }
@@ -342,6 +342,8 @@ export class ChatOpenAI extends OriginalChatOpenAI<t.ChatOpenAICallOptions> {
342
342
  );
343
343
  if ('reasoning_content' in delta) {
344
344
  chunk.additional_kwargs.reasoning_content = delta.reasoning_content;
345
+ } else if ('reasoning' in delta) {
346
+ chunk.additional_kwargs.reasoning_content = delta.reasoning;
345
347
  }
346
348
  defaultRole = delta.role ?? defaultRole;
347
349
  const newTokenIndices = {
@@ -648,11 +648,7 @@ export function _convertMessagesToOpenAIResponsesParams(
648
648
  }
649
649
 
650
650
  export function isReasoningModel(model?: string) {
651
- return (
652
- model != null &&
653
- model !== '' &&
654
- (/^o\d/.test(model) || /^gpt-[5-9]/.test(model))
655
- );
651
+ return model != null && model !== '' && /\b(o\d|gpt-[5-9])\b/i.test(model);
656
652
  }
657
653
 
658
654
  function _convertOpenAIResponsesMessageToBaseMessage(
@@ -0,0 +1,90 @@
1
+ import { isReasoningModel } from './index';
2
+
3
+ describe('isReasoningModel', () => {
4
+ describe('should return true for reasoning models', () => {
5
+ test('basic o-series models', () => {
6
+ expect(isReasoningModel('o1')).toBe(true);
7
+ expect(isReasoningModel('o2')).toBe(true);
8
+ expect(isReasoningModel('o9')).toBe(true);
9
+ expect(isReasoningModel('o1-preview')).toBe(true);
10
+ expect(isReasoningModel('o1-mini')).toBe(true);
11
+ });
12
+
13
+ test('gpt-5+ models', () => {
14
+ expect(isReasoningModel('gpt-5')).toBe(true);
15
+ expect(isReasoningModel('gpt-6')).toBe(true);
16
+ expect(isReasoningModel('gpt-7')).toBe(true);
17
+ expect(isReasoningModel('gpt-8')).toBe(true);
18
+ expect(isReasoningModel('gpt-9')).toBe(true);
19
+ });
20
+
21
+ test('with provider prefixes', () => {
22
+ expect(isReasoningModel('azure/o1')).toBe(true);
23
+ expect(isReasoningModel('azure/gpt-5')).toBe(true);
24
+ expect(isReasoningModel('openai/o1')).toBe(true);
25
+ expect(isReasoningModel('openai/gpt-5')).toBe(true);
26
+ });
27
+
28
+ test('with custom prefixes', () => {
29
+ expect(isReasoningModel('custom-provider/o1')).toBe(true);
30
+ expect(isReasoningModel('my-deployment/gpt-5')).toBe(true);
31
+ expect(isReasoningModel('company/azure/gpt-5')).toBe(true);
32
+ });
33
+
34
+ test('case insensitive', () => {
35
+ expect(isReasoningModel('O1')).toBe(true);
36
+ expect(isReasoningModel('GPT-5')).toBe(true);
37
+ expect(isReasoningModel('gPt-6')).toBe(true);
38
+ expect(isReasoningModel('Azure/O1')).toBe(true);
39
+ });
40
+ });
41
+
42
+ describe('should return false for non-reasoning models', () => {
43
+ test('older GPT models', () => {
44
+ expect(isReasoningModel('gpt-3.5-turbo')).toBe(false);
45
+ expect(isReasoningModel('gpt-4')).toBe(false);
46
+ expect(isReasoningModel('gpt-4-turbo')).toBe(false);
47
+ expect(isReasoningModel('gpt-4o')).toBe(false);
48
+ expect(isReasoningModel('gpt-4o-mini')).toBe(false);
49
+ });
50
+
51
+ test('other model families', () => {
52
+ expect(isReasoningModel('claude-3')).toBe(false);
53
+ expect(isReasoningModel('claude-3-opus')).toBe(false);
54
+ expect(isReasoningModel('llama-2')).toBe(false);
55
+ expect(isReasoningModel('gemini-pro')).toBe(false);
56
+ });
57
+
58
+ test('partial matches that should not match', () => {
59
+ expect(isReasoningModel('proto1')).toBe(false);
60
+ expect(isReasoningModel('version-o1')).toBe(true);
61
+ expect(isReasoningModel('gpt-40')).toBe(false);
62
+ expect(isReasoningModel('gpt-3.5')).toBe(false);
63
+ });
64
+
65
+ test('empty, null, and undefined', () => {
66
+ expect(isReasoningModel('')).toBe(false);
67
+ expect(isReasoningModel()).toBe(false);
68
+ expect(isReasoningModel(undefined)).toBe(false);
69
+ });
70
+ });
71
+
72
+ describe('edge cases', () => {
73
+ test('with special characters', () => {
74
+ expect(isReasoningModel('deployment_o1_model')).toBe(false);
75
+ expect(isReasoningModel('gpt-5-deployment')).toBe(true);
76
+ expect(isReasoningModel('o1@latest')).toBe(true);
77
+ expect(isReasoningModel('gpt-5.0')).toBe(true);
78
+ });
79
+
80
+ test('word boundary behavior', () => {
81
+ // These should match because o1 and gpt-5 are whole words
82
+ expect(isReasoningModel('use-o1-model')).toBe(true);
83
+ expect(isReasoningModel('model-gpt-5-latest')).toBe(true);
84
+
85
+ // These should not match because o1/gpt-5 are not whole words
86
+ expect(isReasoningModel('proto1model')).toBe(false);
87
+ expect(isReasoningModel('supergpt-50')).toBe(false);
88
+ });
89
+ });
90
+ });
@@ -1,5 +1,4 @@
1
1
  // src/llm/providers.ts
2
- import { ChatOllama } from '@langchain/ollama';
3
2
  import { ChatMistralAI } from '@langchain/mistralai';
4
3
  import { ChatBedrockConverse } from '@langchain/aws';
5
4
  // import { ChatAnthropic } from '@langchain/anthropic';
@@ -20,6 +19,7 @@ import { CustomChatGoogleGenerativeAI } from '@/llm/google';
20
19
  import { CustomAnthropic } from '@/llm/anthropic';
21
20
  import { ChatOpenRouter } from '@/llm/openrouter';
22
21
  import { ChatVertexAI } from '@/llm/vertexai';
22
+ import { ChatOllama } from '@/llm/ollama';
23
23
  import { Providers } from '@/common';
24
24
 
25
25
  export const llmProviders: Partial<ChatModelConstructorMap> = {
@@ -128,7 +128,7 @@ async function testStandardStreaming(): Promise<void> {
128
128
  type: 'standard',
129
129
  llmConfig,
130
130
  // tools: [new TavilySearchResults()],
131
- reasoningKey: 'reasoning',
131
+ // reasoningKey: 'reasoning',
132
132
  instructions:
133
133
  'You are a friendly AI assistant. Always address the user by their name.',
134
134
  additional_instructions: `The user's name is ${userName} and they are located in ${location}.`,
@@ -56,10 +56,19 @@ export const llmConfigs: Record<string, t.LLMConfig | undefined> = {
56
56
  },
57
57
  [Providers.OLLAMA]: {
58
58
  provider: Providers.OLLAMA,
59
- model: 'llama3.2',
59
+ model: 'gpt-oss:20b',
60
60
  streaming: true,
61
61
  streamUsage: true,
62
- baseUrl: 'http://host.docker.internal:11434',
62
+ baseUrl: 'http://localhost:11434',
63
+ },
64
+ lmstudio: {
65
+ provider: Providers.OPENAI,
66
+ model: 'gpt-oss-120b',
67
+ streaming: true,
68
+ streamUsage: true,
69
+ configuration: {
70
+ baseURL: 'http://192.168.254.183:1233/v1',
71
+ },
63
72
  },
64
73
  [Providers.DEEPSEEK]: {
65
74
  provider: Providers.DEEPSEEK,