@lobehub/chat 1.18.2 → 1.19.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.

Potentially problematic release.


This version of @lobehub/chat might be problematic. Click here for more details.

Files changed (154) hide show
  1. package/CHANGELOG.md +52 -0
  2. package/Dockerfile +2 -0
  3. package/Dockerfile.database +2 -0
  4. package/locales/ar/chat.json +6 -0
  5. package/locales/ar/error.json +1 -0
  6. package/locales/ar/modelProvider.json +7 -0
  7. package/locales/ar/portal.json +16 -0
  8. package/locales/bg-BG/chat.json +6 -0
  9. package/locales/bg-BG/error.json +1 -0
  10. package/locales/bg-BG/modelProvider.json +7 -0
  11. package/locales/bg-BG/portal.json +16 -0
  12. package/locales/de-DE/chat.json +6 -0
  13. package/locales/de-DE/error.json +1 -0
  14. package/locales/de-DE/modelProvider.json +7 -0
  15. package/locales/de-DE/portal.json +16 -0
  16. package/locales/en-US/chat.json +6 -0
  17. package/locales/en-US/error.json +1 -0
  18. package/locales/en-US/modelProvider.json +7 -0
  19. package/locales/en-US/portal.json +16 -0
  20. package/locales/es-ES/chat.json +6 -0
  21. package/locales/es-ES/error.json +1 -0
  22. package/locales/es-ES/modelProvider.json +7 -0
  23. package/locales/es-ES/portal.json +16 -0
  24. package/locales/fr-FR/chat.json +6 -0
  25. package/locales/fr-FR/error.json +1 -0
  26. package/locales/fr-FR/modelProvider.json +7 -0
  27. package/locales/fr-FR/portal.json +16 -0
  28. package/locales/it-IT/chat.json +6 -0
  29. package/locales/it-IT/error.json +1 -0
  30. package/locales/it-IT/modelProvider.json +7 -0
  31. package/locales/it-IT/portal.json +16 -0
  32. package/locales/ja-JP/chat.json +6 -0
  33. package/locales/ja-JP/error.json +1 -0
  34. package/locales/ja-JP/modelProvider.json +7 -0
  35. package/locales/ja-JP/portal.json +16 -0
  36. package/locales/ko-KR/chat.json +6 -0
  37. package/locales/ko-KR/error.json +1 -0
  38. package/locales/ko-KR/modelProvider.json +7 -0
  39. package/locales/ko-KR/portal.json +16 -0
  40. package/locales/nl-NL/chat.json +6 -0
  41. package/locales/nl-NL/error.json +1 -0
  42. package/locales/nl-NL/modelProvider.json +7 -0
  43. package/locales/nl-NL/portal.json +16 -0
  44. package/locales/pl-PL/chat.json +6 -0
  45. package/locales/pl-PL/error.json +1 -0
  46. package/locales/pl-PL/modelProvider.json +7 -0
  47. package/locales/pl-PL/portal.json +16 -0
  48. package/locales/pt-BR/chat.json +6 -0
  49. package/locales/pt-BR/error.json +1 -0
  50. package/locales/pt-BR/modelProvider.json +7 -0
  51. package/locales/pt-BR/portal.json +16 -0
  52. package/locales/ru-RU/chat.json +6 -0
  53. package/locales/ru-RU/error.json +1 -0
  54. package/locales/ru-RU/modelProvider.json +7 -0
  55. package/locales/ru-RU/portal.json +16 -0
  56. package/locales/tr-TR/chat.json +6 -0
  57. package/locales/tr-TR/error.json +1 -0
  58. package/locales/tr-TR/modelProvider.json +7 -0
  59. package/locales/tr-TR/portal.json +16 -0
  60. package/locales/vi-VN/chat.json +6 -0
  61. package/locales/vi-VN/error.json +1 -0
  62. package/locales/vi-VN/modelProvider.json +7 -0
  63. package/locales/vi-VN/portal.json +16 -0
  64. package/locales/zh-CN/chat.json +6 -0
  65. package/locales/zh-CN/error.json +2 -1
  66. package/locales/zh-CN/modelProvider.json +7 -0
  67. package/locales/zh-CN/portal.json +17 -1
  68. package/locales/zh-TW/chat.json +6 -0
  69. package/locales/zh-TW/error.json +1 -0
  70. package/locales/zh-TW/modelProvider.json +7 -0
  71. package/locales/zh-TW/portal.json +16 -0
  72. package/package.json +4 -2
  73. package/src/app/(main)/chat/(workspace)/@portal/Artifacts/Body/Renderer/HTML.tsx +25 -0
  74. package/src/app/(main)/chat/(workspace)/@portal/Artifacts/Body/Renderer/React.tsx +30 -0
  75. package/src/app/(main)/chat/(workspace)/@portal/Artifacts/Body/Renderer/SVG.tsx +114 -0
  76. package/src/app/(main)/chat/(workspace)/@portal/Artifacts/Body/Renderer/index.tsx +25 -0
  77. package/src/app/(main)/chat/(workspace)/@portal/Artifacts/Body/index.tsx +79 -0
  78. package/src/app/(main)/chat/(workspace)/@portal/Artifacts/Header.tsx +69 -0
  79. package/src/app/(main)/chat/(workspace)/@portal/Artifacts/index.ts +10 -0
  80. package/src/app/(main)/chat/(workspace)/@portal/Artifacts/useEnable.ts +4 -0
  81. package/src/app/(main)/chat/(workspace)/@portal/FilePreview/index.ts +2 -1
  82. package/src/app/(main)/chat/(workspace)/@portal/Home/Body/{Artifacts → Plugins}/index.tsx +1 -1
  83. package/src/app/(main)/chat/(workspace)/@portal/Home/Body/index.tsx +2 -2
  84. package/src/app/(main)/chat/(workspace)/@portal/MessageDetail/index.ts +2 -1
  85. package/src/app/(main)/chat/(workspace)/@portal/Plugins/Body/ToolRender.tsx +1 -1
  86. package/src/app/(main)/chat/(workspace)/@portal/Plugins/Body/index.tsx +1 -1
  87. package/src/app/(main)/chat/(workspace)/@portal/Plugins/Footer.tsx +1 -1
  88. package/src/app/(main)/chat/(workspace)/@portal/Plugins/index.ts +2 -1
  89. package/src/app/(main)/chat/(workspace)/@portal/Plugins/useEnable.ts +1 -1
  90. package/src/app/(main)/chat/(workspace)/@portal/_layout/Desktop.tsx +2 -4
  91. package/src/app/(main)/chat/(workspace)/@portal/features/Body.tsx +27 -0
  92. package/src/app/(main)/chat/(workspace)/@portal/router.tsx +3 -1
  93. package/src/app/(main)/chat/(workspace)/@portal/type.ts +7 -0
  94. package/src/app/(main)/chat/(workspace)/_layout/Desktop/Portal.tsx +3 -2
  95. package/src/app/(main)/settings/llm/ProviderList/Github/index.tsx +53 -0
  96. package/src/app/(main)/settings/llm/ProviderList/providers.tsx +6 -1
  97. package/src/app/api/chat/agentRuntime.ts +14 -0
  98. package/src/components/SidebarHeader/index.tsx +1 -1
  99. package/src/config/llm.ts +14 -0
  100. package/src/config/modelProviders/ai21.ts +37 -0
  101. package/src/config/modelProviders/anthropic.ts +4 -0
  102. package/src/config/modelProviders/github.ts +209 -0
  103. package/src/config/modelProviders/index.ts +8 -0
  104. package/src/config/modelProviders/mistral.ts +38 -21
  105. package/src/config/modelProviders/upstage.ts +9 -9
  106. package/src/const/layoutTokens.ts +1 -1
  107. package/src/const/plugin.test.ts +80 -0
  108. package/src/const/plugin.ts +12 -0
  109. package/src/const/settings/llm.ts +10 -0
  110. package/src/features/Conversation/Error/APIKeyForm/index.tsx +4 -0
  111. package/src/features/Conversation/Messages/Tool/Inspector/index.tsx +1 -1
  112. package/src/features/Conversation/Messages/Tool/index.tsx +1 -1
  113. package/src/features/Conversation/components/ChatItem/index.tsx +24 -2
  114. package/src/features/Conversation/components/ChatItem/utils.test.ts +150 -0
  115. package/src/features/Conversation/components/ChatItem/utils.ts +28 -0
  116. package/src/features/Conversation/components/MarkdownElements/LobeArtifact/Render/Icon.tsx +96 -0
  117. package/src/features/Conversation/components/MarkdownElements/LobeArtifact/Render/index.tsx +129 -0
  118. package/src/features/Conversation/components/MarkdownElements/LobeArtifact/index.ts +10 -0
  119. package/src/features/Conversation/components/MarkdownElements/LobeArtifact/rehypePlugin.ts +74 -0
  120. package/src/features/Conversation/components/MarkdownElements/LobeThinking/Render.tsx +86 -0
  121. package/src/features/Conversation/components/MarkdownElements/LobeThinking/index.ts +12 -0
  122. package/src/features/Conversation/components/MarkdownElements/LobeThinking/rehypePlugin.test.ts +124 -0
  123. package/src/features/Conversation/components/MarkdownElements/LobeThinking/rehypePlugin.ts +51 -0
  124. package/src/features/Conversation/components/MarkdownElements/index.ts +4 -0
  125. package/src/features/Conversation/components/MarkdownElements/type.ts +7 -0
  126. package/src/libs/agent-runtime/AgentRuntime.ts +14 -0
  127. package/src/libs/agent-runtime/ai21/index.test.ts +255 -0
  128. package/src/libs/agent-runtime/ai21/index.ts +18 -0
  129. package/src/libs/agent-runtime/error.ts +2 -0
  130. package/src/libs/agent-runtime/github/index.test.ts +246 -0
  131. package/src/libs/agent-runtime/github/index.ts +15 -0
  132. package/src/libs/agent-runtime/openrouter/__snapshots__/index.test.ts.snap +2215 -28
  133. package/src/libs/agent-runtime/openrouter/fixtures/models.json +3345 -37
  134. package/src/libs/agent-runtime/openrouter/index.ts +2 -1
  135. package/src/libs/agent-runtime/types/type.ts +2 -0
  136. package/src/locales/default/chat.ts +7 -0
  137. package/src/locales/default/error.ts +3 -0
  138. package/src/locales/default/modelProvider.ts +7 -0
  139. package/src/locales/default/portal.ts +17 -1
  140. package/src/server/globalConfig/index.ts +14 -0
  141. package/src/store/chat/slices/message/selectors.ts +30 -28
  142. package/src/store/chat/slices/portal/action.ts +15 -2
  143. package/src/store/chat/slices/portal/initialState.ts +11 -0
  144. package/src/store/chat/slices/portal/selectors.test.ts +29 -7
  145. package/src/store/chat/slices/portal/selectors.ts +56 -12
  146. package/src/styles/loading.ts +28 -0
  147. package/src/tools/artifacts/index.ts +13 -0
  148. package/src/tools/artifacts/systemRole.ts +338 -0
  149. package/src/tools/index.ts +6 -0
  150. package/src/types/user/settings/keyVaults.ts +2 -0
  151. package/src/utils/clipboard.ts +53 -0
  152. /package/src/app/(main)/chat/(workspace)/@portal/Home/Body/{Artifacts → Plugins}/ArtifactList/Item/index.tsx +0 -0
  153. /package/src/app/(main)/chat/(workspace)/@portal/Home/Body/{Artifacts → Plugins}/ArtifactList/Item/style.ts +0 -0
  154. /package/src/app/(main)/chat/(workspace)/@portal/Home/Body/{Artifacts → Plugins}/ArtifactList/index.tsx +0 -0
@@ -0,0 +1,246 @@
1
+ // @vitest-environment node
2
+ import OpenAI from 'openai';
3
+ import { Mock, afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
4
+
5
+ import { LobeOpenAICompatibleRuntime } from '@/libs/agent-runtime';
6
+ import { ModelProvider } from '@/libs/agent-runtime';
7
+ import { AgentRuntimeErrorType } from '@/libs/agent-runtime';
8
+
9
+ import * as debugStreamModule from '../utils/debugStream';
10
+ import { LobeGithubAI } from './index';
11
+
12
+ const provider = ModelProvider.Github;
13
+ const defaultBaseURL = 'https://models.inference.ai.azure.com';
14
+ const bizErrorType = AgentRuntimeErrorType.ProviderBizError;
15
+ const invalidErrorType = AgentRuntimeErrorType.InvalidGithubToken;
16
+
17
+ // Mock the console.error to avoid polluting test output
18
+ vi.spyOn(console, 'error').mockImplementation(() => {});
19
+
20
+ let instance: LobeOpenAICompatibleRuntime;
21
+
22
+ beforeEach(() => {
23
+ instance = new LobeGithubAI({ apiKey: 'test' });
24
+
25
+ // Use vi.spyOn to mock the chat.completions.create method
26
+ vi.spyOn(instance['client'].chat.completions, 'create').mockResolvedValue(
27
+ new ReadableStream() as any,
28
+ );
29
+ });
30
+
31
+ afterEach(() => {
32
+ vi.clearAllMocks();
33
+ });
34
+
35
+ describe('LobeGithubAI', () => {
36
+ describe('init', () => {
37
+ it('should correctly initialize with an API key', async () => {
38
+ const instance = new LobeGithubAI({ apiKey: 'test_api_key' });
39
+ expect(instance).toBeInstanceOf(LobeGithubAI);
40
+ expect(instance.baseURL).toEqual(defaultBaseURL);
41
+ });
42
+ });
43
+
44
+ describe('chat', () => {
45
+ describe('Error', () => {
46
+ it('should return GithubBizError with an openai error response when OpenAI.APIError is thrown', async () => {
47
+ // Arrange
48
+ const apiError = new OpenAI.APIError(
49
+ 400,
50
+ {
51
+ status: 400,
52
+ error: {
53
+ message: 'Bad Request',
54
+ },
55
+ },
56
+ 'Error message',
57
+ {},
58
+ );
59
+
60
+ vi.spyOn(instance['client'].chat.completions, 'create').mockRejectedValue(apiError);
61
+
62
+ // Act
63
+ try {
64
+ await instance.chat({
65
+ messages: [{ content: 'Hello', role: 'user' }],
66
+ model: 'meta-llama-3-70b-instruct',
67
+ temperature: 0.7,
68
+ });
69
+ } catch (e) {
70
+ expect(e).toEqual({
71
+ endpoint: defaultBaseURL,
72
+ error: {
73
+ error: { message: 'Bad Request' },
74
+ status: 400,
75
+ },
76
+ errorType: bizErrorType,
77
+ provider,
78
+ });
79
+ }
80
+ });
81
+
82
+ it('should throw AgentRuntimeError with InvalidGithubToken if no apiKey is provided', async () => {
83
+ try {
84
+ new LobeGithubAI({});
85
+ } catch (e) {
86
+ expect(e).toEqual({ errorType: invalidErrorType });
87
+ }
88
+ });
89
+
90
+ it('should return GithubBizError with the cause when OpenAI.APIError is thrown with cause', async () => {
91
+ // Arrange
92
+ const errorInfo = {
93
+ stack: 'abc',
94
+ cause: {
95
+ message: 'api is undefined',
96
+ },
97
+ };
98
+ const apiError = new OpenAI.APIError(400, errorInfo, 'module error', {});
99
+
100
+ vi.spyOn(instance['client'].chat.completions, 'create').mockRejectedValue(apiError);
101
+
102
+ // Act
103
+ try {
104
+ await instance.chat({
105
+ messages: [{ content: 'Hello', role: 'user' }],
106
+ model: 'meta-llama-3-70b-instruct',
107
+ temperature: 0.7,
108
+ });
109
+ } catch (e) {
110
+ expect(e).toEqual({
111
+ endpoint: defaultBaseURL,
112
+ error: {
113
+ cause: { message: 'api is undefined' },
114
+ stack: 'abc',
115
+ },
116
+ errorType: bizErrorType,
117
+ provider,
118
+ });
119
+ }
120
+ });
121
+
122
+ it('should return GithubBizError with an cause response with desensitize Url', async () => {
123
+ // Arrange
124
+ const errorInfo = {
125
+ stack: 'abc',
126
+ cause: { message: 'api is undefined' },
127
+ };
128
+ const apiError = new OpenAI.APIError(400, errorInfo, 'module error', {});
129
+
130
+ instance = new LobeGithubAI({
131
+ apiKey: 'test',
132
+ baseURL: 'https://api.abc.com/v1',
133
+ });
134
+
135
+ vi.spyOn(instance['client'].chat.completions, 'create').mockRejectedValue(apiError);
136
+
137
+ // Act
138
+ try {
139
+ await instance.chat({
140
+ messages: [{ content: 'Hello', role: 'user' }],
141
+ model: 'meta-llama-3-70b-instruct',
142
+ temperature: 0.7,
143
+ });
144
+ } catch (e) {
145
+ expect(e).toEqual({
146
+ endpoint: 'https://api.***.com/v1',
147
+ error: {
148
+ cause: { message: 'api is undefined' },
149
+ stack: 'abc',
150
+ },
151
+ errorType: bizErrorType,
152
+ provider,
153
+ });
154
+ }
155
+ });
156
+
157
+ it('should throw an InvalidGithubToken error type on 401 status code', async () => {
158
+ // Mock the API call to simulate a 401 error
159
+ const error = new Error('InvalidApiKey') as any;
160
+ error.status = 401;
161
+ vi.mocked(instance['client'].chat.completions.create).mockRejectedValue(error);
162
+
163
+ try {
164
+ await instance.chat({
165
+ messages: [{ content: 'Hello', role: 'user' }],
166
+ model: 'meta-llama-3-70b-instruct',
167
+ temperature: 0.7,
168
+ });
169
+ } catch (e) {
170
+ expect(e).toEqual({
171
+ endpoint: defaultBaseURL,
172
+ error: new Error('InvalidApiKey'),
173
+ errorType: invalidErrorType,
174
+ provider,
175
+ });
176
+ }
177
+ });
178
+
179
+ it('should return AgentRuntimeError for non-OpenAI errors', async () => {
180
+ // Arrange
181
+ const genericError = new Error('Generic Error');
182
+
183
+ vi.spyOn(instance['client'].chat.completions, 'create').mockRejectedValue(genericError);
184
+
185
+ // Act
186
+ try {
187
+ await instance.chat({
188
+ messages: [{ content: 'Hello', role: 'user' }],
189
+ model: 'meta-llama-3-70b-instruct',
190
+ temperature: 0.7,
191
+ });
192
+ } catch (e) {
193
+ expect(e).toEqual({
194
+ endpoint: defaultBaseURL,
195
+ errorType: 'AgentRuntimeError',
196
+ provider,
197
+ error: {
198
+ name: genericError.name,
199
+ cause: genericError.cause,
200
+ message: genericError.message,
201
+ stack: genericError.stack,
202
+ },
203
+ });
204
+ }
205
+ });
206
+ });
207
+
208
+ describe('DEBUG', () => {
209
+ it('should call debugStream and return StreamingTextResponse when DEBUG_GITHUB_CHAT_COMPLETION is 1', async () => {
210
+ // Arrange
211
+ const mockProdStream = new ReadableStream() as any;
212
+ const mockDebugStream = new ReadableStream({
213
+ start(controller) {
214
+ controller.enqueue('Debug stream content');
215
+ controller.close();
216
+ },
217
+ }) as any;
218
+ mockDebugStream.toReadableStream = () => mockDebugStream;
219
+
220
+ // mock
221
+ (instance['client'].chat.completions.create as Mock).mockResolvedValue({
222
+ tee: () => [mockProdStream, { toReadableStream: () => mockDebugStream }],
223
+ });
224
+
225
+ const originalDebugValue = process.env.DEBUG_GITHUB_CHAT_COMPLETION;
226
+
227
+ process.env.DEBUG_GITHUB_CHAT_COMPLETION = '1';
228
+ vi.spyOn(debugStreamModule, 'debugStream').mockImplementation(() => Promise.resolve());
229
+
230
+ // 执行测试
231
+ await instance.chat({
232
+ messages: [{ content: 'Hello', role: 'user' }],
233
+ model: 'meta-llama-3-70b-instruct',
234
+ stream: true,
235
+ temperature: 0.7,
236
+ });
237
+
238
+ // 验证 debugStream 被调用
239
+ expect(debugStreamModule.debugStream).toHaveBeenCalled();
240
+
241
+ // 恢复原始环境变量值
242
+ process.env.DEBUG_GITHUB_CHAT_COMPLETION = originalDebugValue;
243
+ });
244
+ });
245
+ });
246
+ });
@@ -0,0 +1,15 @@
1
+ import { AgentRuntimeErrorType } from '../error';
2
+ import { ModelProvider } from '../types';
3
+ import { LobeOpenAICompatibleFactory } from '../utils/openaiCompatibleFactory';
4
+
5
+ export const LobeGithubAI = LobeOpenAICompatibleFactory({
6
+ baseURL: 'https://models.inference.ai.azure.com',
7
+ debug: {
8
+ chatCompletion: () => process.env.DEBUG_GITHUB_CHAT_COMPLETION === '1',
9
+ },
10
+ errorType: {
11
+ bizError: AgentRuntimeErrorType.ProviderBizError,
12
+ invalidAPIKey: AgentRuntimeErrorType.InvalidGithubToken,
13
+ },
14
+ provider: ModelProvider.Github,
15
+ });