@librechat/agents 2.4.74 → 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.
Files changed (42) hide show
  1. package/dist/cjs/llm/anthropic/index.cjs +4 -1
  2. package/dist/cjs/llm/anthropic/index.cjs.map +1 -1
  3. package/dist/cjs/llm/ollama/index.cjs +67 -0
  4. package/dist/cjs/llm/ollama/index.cjs.map +1 -0
  5. package/dist/cjs/llm/ollama/utils.cjs +158 -0
  6. package/dist/cjs/llm/ollama/utils.cjs.map +1 -0
  7. package/dist/cjs/llm/openai/index.cjs +3 -0
  8. package/dist/cjs/llm/openai/index.cjs.map +1 -1
  9. package/dist/cjs/llm/openai/utils/index.cjs +1 -3
  10. package/dist/cjs/llm/openai/utils/index.cjs.map +1 -1
  11. package/dist/cjs/llm/providers.cjs +2 -2
  12. package/dist/cjs/llm/providers.cjs.map +1 -1
  13. package/dist/cjs/llm/text.cjs +14 -3
  14. package/dist/cjs/llm/text.cjs.map +1 -1
  15. package/dist/esm/llm/anthropic/index.mjs +4 -1
  16. package/dist/esm/llm/anthropic/index.mjs.map +1 -1
  17. package/dist/esm/llm/ollama/index.mjs +65 -0
  18. package/dist/esm/llm/ollama/index.mjs.map +1 -0
  19. package/dist/esm/llm/ollama/utils.mjs +155 -0
  20. package/dist/esm/llm/ollama/utils.mjs.map +1 -0
  21. package/dist/esm/llm/openai/index.mjs +3 -0
  22. package/dist/esm/llm/openai/index.mjs.map +1 -1
  23. package/dist/esm/llm/openai/utils/index.mjs +1 -3
  24. package/dist/esm/llm/openai/utils/index.mjs.map +1 -1
  25. package/dist/esm/llm/providers.mjs +1 -1
  26. package/dist/esm/llm/providers.mjs.map +1 -1
  27. package/dist/esm/llm/text.mjs +14 -3
  28. package/dist/esm/llm/text.mjs.map +1 -1
  29. package/dist/types/llm/ollama/index.d.ts +7 -0
  30. package/dist/types/llm/ollama/utils.d.ts +7 -0
  31. package/dist/types/llm/text.d.ts +1 -1
  32. package/package.json +2 -2
  33. package/src/llm/anthropic/index.ts +4 -1
  34. package/src/llm/ollama/index.ts +89 -0
  35. package/src/llm/ollama/utils.ts +193 -0
  36. package/src/llm/openai/index.ts +2 -0
  37. package/src/llm/openai/utils/index.ts +1 -5
  38. package/src/llm/openai/utils/isReasoningModel.test.ts +90 -0
  39. package/src/llm/providers.ts +1 -1
  40. package/src/llm/text.ts +26 -7
  41. package/src/scripts/simple.ts +1 -1
  42. package/src/utils/llmConfig.ts +11 -2
@@ -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> = {
package/src/llm/text.ts CHANGED
@@ -1,4 +1,3 @@
1
-
2
1
  export interface TextStreamOptions {
3
2
  minChunkSize?: number;
4
3
  maxChunkSize?: number;
@@ -30,7 +29,15 @@ export class TextStream {
30
29
  return Math.floor(Math.random() * (max - min)) + min;
31
30
  }
32
31
 
33
- private static readonly BOUNDARIES = new Set([' ', '.', ',', '!', '?', ';', ':']);
32
+ private static readonly BOUNDARIES = new Set([
33
+ ' ',
34
+ '.',
35
+ ',',
36
+ '!',
37
+ '?',
38
+ ';',
39
+ ':',
40
+ ]);
34
41
 
35
42
  private findFirstWordBoundary(text: string, minSize: number): number {
36
43
  if (minSize >= text.length) return text.length;
@@ -49,11 +56,17 @@ export class TextStream {
49
56
  return text.length; // If no boundary found, return entire remaining text
50
57
  }
51
58
 
52
- async *generateText(progressCallback?: ProgressCallback): AsyncGenerator<string, void, unknown> {
59
+ async *generateText(
60
+ signal?: AbortSignal,
61
+ progressCallback?: ProgressCallback
62
+ ): AsyncGenerator<string, void, unknown> {
53
63
  const { delay, minChunkSize, maxChunkSize } = this;
54
64
 
55
65
  while (this.currentIndex < this.text.length) {
56
- await new Promise(resolve => setTimeout(resolve, delay));
66
+ if (signal?.aborted === true) {
67
+ break;
68
+ }
69
+ await new Promise((resolve) => setTimeout(resolve, delay));
57
70
 
58
71
  const remainingText = this.text.slice(this.currentIndex);
59
72
  let chunkSize: number;
@@ -62,14 +75,20 @@ export class TextStream {
62
75
  chunkSize = this.findFirstWordBoundary(remainingText, minChunkSize);
63
76
  } else {
64
77
  const remainingChars = remainingText.length;
65
- chunkSize = Math.min(this.randomInt(minChunkSize, maxChunkSize + 1), remainingChars);
78
+ chunkSize = Math.min(
79
+ this.randomInt(minChunkSize, maxChunkSize + 1),
80
+ remainingChars
81
+ );
66
82
  }
67
83
 
68
- const chunk = this.text.slice(this.currentIndex, this.currentIndex + chunkSize);
84
+ const chunk = this.text.slice(
85
+ this.currentIndex,
86
+ this.currentIndex + chunkSize
87
+ );
69
88
  progressCallback?.(chunk);
70
89
 
71
90
  yield chunk;
72
91
  this.currentIndex += chunkSize;
73
92
  }
74
93
  }
75
- }
94
+ }
@@ -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,