@lobehub/lobehub 2.0.0-next.194 → 2.0.0-next.196
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/CHANGELOG.md +50 -0
- package/changelog/v1.json +18 -0
- package/locales/ar/setting.json +0 -3
- package/locales/bg-BG/setting.json +0 -3
- package/locales/de-DE/setting.json +0 -3
- package/locales/en-US/setting.json +0 -3
- package/locales/es-ES/setting.json +0 -3
- package/locales/fa-IR/setting.json +0 -3
- package/locales/fr-FR/setting.json +0 -3
- package/locales/it-IT/setting.json +0 -3
- package/locales/ja-JP/setting.json +0 -3
- package/locales/ko-KR/setting.json +0 -3
- package/locales/nl-NL/setting.json +0 -3
- package/locales/pl-PL/setting.json +0 -3
- package/locales/pt-BR/setting.json +0 -3
- package/locales/ru-RU/setting.json +0 -3
- package/locales/tr-TR/setting.json +0 -3
- package/locales/vi-VN/setting.json +0 -3
- package/locales/zh-CN/setting.json +0 -3
- package/locales/zh-TW/setting.json +0 -3
- package/package.json +1 -1
- package/packages/const/src/fetch.ts +1 -4
- package/packages/database/src/models/user.ts +8 -0
- package/packages/database/src/repositories/aiInfra/index.test.ts +11 -8
- package/packages/database/src/repositories/dataExporter/index.test.ts +11 -9
- package/packages/database/src/repositories/tableViewer/index.test.ts +13 -14
- package/packages/model-runtime/src/providers/zhipu/index.ts +6 -6
- package/packages/types/src/auth.ts +0 -4
- package/packages/utils/src/server/xor.test.ts +1 -2
- package/src/app/(backend)/_deprecated/createBizOpenAI/auth.test.ts +7 -41
- package/src/app/(backend)/_deprecated/createBizOpenAI/auth.ts +1 -15
- package/src/app/(backend)/_deprecated/createBizOpenAI/index.ts +2 -9
- package/src/app/(backend)/middleware/auth/index.ts +0 -1
- package/src/app/(backend)/middleware/auth/utils.test.ts +2 -42
- package/src/app/(backend)/middleware/auth/utils.ts +3 -17
- package/src/app/(backend)/webapi/chat/[provider]/route.test.ts +0 -5
- package/src/app/(backend)/webapi/models/[provider]/route.test.ts +0 -6
- package/src/app/(backend)/webapi/plugin/gateway/route.ts +2 -32
- package/src/app/[variants]/(main)/settings/common/features/Common/Common.tsx +1 -16
- package/src/envs/app.ts +2 -0
- package/src/libs/trpc/lambda/middleware/index.ts +1 -0
- package/src/libs/trpc/lambda/middleware/telemetry.test.ts +237 -0
- package/src/libs/trpc/lambda/middleware/telemetry.ts +74 -0
- package/src/locales/default/setting.ts +0 -3
- package/src/server/routers/lambda/market/index.ts +1 -93
- package/src/server/routers/tools/_helpers/index.ts +1 -0
- package/src/server/routers/tools/_helpers/scheduleToolCallReport.ts +113 -0
- package/src/server/routers/tools/index.ts +2 -2
- package/src/server/routers/tools/market.ts +375 -0
- package/src/server/routers/tools/mcp.ts +77 -20
- package/src/services/chat/index.ts +0 -2
- package/src/services/codeInterpreter.ts +6 -6
- package/src/services/mcp.test.ts +60 -46
- package/src/services/mcp.ts +67 -48
- package/src/store/chat/slices/plugin/action.test.ts +191 -0
- package/src/store/chat/slices/plugin/actions/internals.ts +2 -18
- package/src/store/chat/slices/plugin/actions/pluginTypes.ts +31 -44
- package/packages/database/src/client/db.test.ts +0 -52
- package/packages/database/src/client/db.ts +0 -195
- package/packages/database/src/client/type.ts +0 -6
- package/src/server/routers/tools/codeInterpreter.ts +0 -255
|
@@ -1,255 +0,0 @@
|
|
|
1
|
-
import { type CodeInterpreterToolName, MarketSDK } from '@lobehub/market-sdk';
|
|
2
|
-
import debug from 'debug';
|
|
3
|
-
import { z } from 'zod';
|
|
4
|
-
|
|
5
|
-
import { DocumentModel } from '@/database/models/document';
|
|
6
|
-
import { FileModel } from '@/database/models/file';
|
|
7
|
-
import { authedProcedure, router } from '@/libs/trpc/lambda';
|
|
8
|
-
import { marketUserInfo, serverDatabase } from '@/libs/trpc/lambda/middleware';
|
|
9
|
-
import { generateTrustedClientToken } from '@/libs/trusted-client';
|
|
10
|
-
import { FileS3 } from '@/server/modules/S3';
|
|
11
|
-
|
|
12
|
-
const log = debug('lobe-server:tools:code-interpreter');
|
|
13
|
-
|
|
14
|
-
const codeInterpreterProcedure = authedProcedure.use(serverDatabase).use(marketUserInfo);
|
|
15
|
-
|
|
16
|
-
// Schema for tool call request
|
|
17
|
-
const callToolSchema = z.object({
|
|
18
|
-
/** Market access token from OIDC (stored in user settings) */
|
|
19
|
-
marketAccessToken: z.string().optional(),
|
|
20
|
-
params: z.record(z.any()),
|
|
21
|
-
toolName: z.string(),
|
|
22
|
-
// Session context for isolation
|
|
23
|
-
topicId: z.string(),
|
|
24
|
-
userId: z.string(),
|
|
25
|
-
});
|
|
26
|
-
|
|
27
|
-
// Schema for getting export file upload URL
|
|
28
|
-
const getExportFileUploadUrlSchema = z.object({
|
|
29
|
-
/** Original filename from sandbox */
|
|
30
|
-
filename: z.string(),
|
|
31
|
-
/** Topic ID for organizing files */
|
|
32
|
-
topicId: z.string(),
|
|
33
|
-
});
|
|
34
|
-
|
|
35
|
-
// Schema for saving exported file content to document
|
|
36
|
-
const saveExportedFileContentSchema = z.object({
|
|
37
|
-
/** File content (text content from code-interpreter export) */
|
|
38
|
-
content: z.string(),
|
|
39
|
-
/** File ID to associate with the document */
|
|
40
|
-
fileId: z.string(),
|
|
41
|
-
/** File MIME type */
|
|
42
|
-
fileType: z.string(),
|
|
43
|
-
/** Filename */
|
|
44
|
-
filename: z.string(),
|
|
45
|
-
/** File URL */
|
|
46
|
-
url: z.string(),
|
|
47
|
-
});
|
|
48
|
-
|
|
49
|
-
export type CallToolInput = z.infer<typeof callToolSchema>;
|
|
50
|
-
export type GetExportFileUploadUrlInput = z.infer<typeof getExportFileUploadUrlSchema>;
|
|
51
|
-
export type SaveExportedFileContentInput = z.infer<typeof saveExportedFileContentSchema>;
|
|
52
|
-
|
|
53
|
-
export interface CallToolResult {
|
|
54
|
-
error?: {
|
|
55
|
-
message: string;
|
|
56
|
-
name?: string;
|
|
57
|
-
};
|
|
58
|
-
result: any;
|
|
59
|
-
sessionExpiredAndRecreated?: boolean;
|
|
60
|
-
success: boolean;
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
export interface GetExportFileUploadUrlResult {
|
|
64
|
-
/** The download URL after file is uploaded */
|
|
65
|
-
downloadUrl: string;
|
|
66
|
-
error?: {
|
|
67
|
-
message: string;
|
|
68
|
-
};
|
|
69
|
-
/** The S3 key where file will be stored */
|
|
70
|
-
key: string;
|
|
71
|
-
success: boolean;
|
|
72
|
-
/** Pre-signed upload URL */
|
|
73
|
-
uploadUrl: string;
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
export interface SaveExportedFileContentResult {
|
|
77
|
-
/** Created document ID */
|
|
78
|
-
documentId?: string;
|
|
79
|
-
error?: {
|
|
80
|
-
message: string;
|
|
81
|
-
};
|
|
82
|
-
success: boolean;
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
export const codeInterpreterRouter = router({
|
|
86
|
-
callTool: codeInterpreterProcedure.input(callToolSchema).mutation(async ({ input, ctx }) => {
|
|
87
|
-
const { toolName, params, userId, topicId, marketAccessToken } = input;
|
|
88
|
-
|
|
89
|
-
log('Calling cloud code interpreter tool: %s with params: %O', toolName, {
|
|
90
|
-
params,
|
|
91
|
-
topicId,
|
|
92
|
-
userId,
|
|
93
|
-
});
|
|
94
|
-
log('Market access token available: %s', marketAccessToken ? 'yes' : 'no');
|
|
95
|
-
|
|
96
|
-
// Generate trusted client token if user info is available
|
|
97
|
-
const trustedClientToken = ctx.marketUserInfo
|
|
98
|
-
? generateTrustedClientToken(ctx.marketUserInfo)
|
|
99
|
-
: undefined;
|
|
100
|
-
|
|
101
|
-
try {
|
|
102
|
-
// Initialize MarketSDK with market access token and trusted client token
|
|
103
|
-
const market = new MarketSDK({
|
|
104
|
-
accessToken: marketAccessToken,
|
|
105
|
-
baseURL: process.env.NEXT_PUBLIC_MARKET_BASE_URL,
|
|
106
|
-
trustedClientToken,
|
|
107
|
-
});
|
|
108
|
-
|
|
109
|
-
// Call market-sdk's runBuildInTool
|
|
110
|
-
// API signature: runBuildInTool(toolName, params, context, options?)
|
|
111
|
-
const response = await market.plugins.runBuildInTool(
|
|
112
|
-
toolName as CodeInterpreterToolName,
|
|
113
|
-
params as any,
|
|
114
|
-
{ topicId, userId },
|
|
115
|
-
);
|
|
116
|
-
|
|
117
|
-
log('Cloud code interpreter tool %s response: %O', toolName, response);
|
|
118
|
-
|
|
119
|
-
if (!response.success) {
|
|
120
|
-
return {
|
|
121
|
-
error: {
|
|
122
|
-
message: response.error?.message || 'Unknown error',
|
|
123
|
-
name: response.error?.code,
|
|
124
|
-
},
|
|
125
|
-
result: null,
|
|
126
|
-
sessionExpiredAndRecreated: false,
|
|
127
|
-
success: false,
|
|
128
|
-
} as CallToolResult;
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
return {
|
|
132
|
-
result: response.data?.result,
|
|
133
|
-
sessionExpiredAndRecreated: response.data?.sessionExpiredAndRecreated || false,
|
|
134
|
-
success: true,
|
|
135
|
-
} as CallToolResult;
|
|
136
|
-
} catch (error) {
|
|
137
|
-
log('Error calling cloud code interpreter tool %s: %O', toolName, error);
|
|
138
|
-
|
|
139
|
-
return {
|
|
140
|
-
error: {
|
|
141
|
-
message: (error as Error).message,
|
|
142
|
-
name: (error as Error).name,
|
|
143
|
-
},
|
|
144
|
-
result: null,
|
|
145
|
-
sessionExpiredAndRecreated: false,
|
|
146
|
-
success: false,
|
|
147
|
-
} as CallToolResult;
|
|
148
|
-
}
|
|
149
|
-
}),
|
|
150
|
-
|
|
151
|
-
/**
|
|
152
|
-
* Generate a pre-signed upload URL for exporting files from sandbox
|
|
153
|
-
* The URL can be used by the sandbox to upload the file directly to S3
|
|
154
|
-
*/
|
|
155
|
-
getExportFileUploadUrl: codeInterpreterProcedure
|
|
156
|
-
.input(getExportFileUploadUrlSchema)
|
|
157
|
-
|
|
158
|
-
// TODO if upload success, should add it path to files db
|
|
159
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
160
|
-
.mutation(async ({ ctx, input }) => {
|
|
161
|
-
const { filename, topicId } = input;
|
|
162
|
-
|
|
163
|
-
log('Generating export file upload URL for: %s in topic: %s', filename, topicId);
|
|
164
|
-
|
|
165
|
-
try {
|
|
166
|
-
const s3 = new FileS3();
|
|
167
|
-
|
|
168
|
-
// Generate a unique key for the exported file
|
|
169
|
-
// Format: code-interpreter-exports/{topicId}/{filename}
|
|
170
|
-
const key = `code-interpreter-exports/${topicId}/${filename}`;
|
|
171
|
-
|
|
172
|
-
// Generate pre-signed upload URL
|
|
173
|
-
const uploadUrl = await s3.createPreSignedUrl(key);
|
|
174
|
-
|
|
175
|
-
// Generate download URL (pre-signed for preview)
|
|
176
|
-
const downloadUrl = await s3.createPreSignedUrlForPreview(key);
|
|
177
|
-
|
|
178
|
-
log('Generated upload URL for key: %s', key);
|
|
179
|
-
|
|
180
|
-
return {
|
|
181
|
-
downloadUrl,
|
|
182
|
-
key,
|
|
183
|
-
success: true,
|
|
184
|
-
uploadUrl,
|
|
185
|
-
} as GetExportFileUploadUrlResult;
|
|
186
|
-
} catch (error) {
|
|
187
|
-
log('Error generating export file upload URL: %O', error);
|
|
188
|
-
|
|
189
|
-
return {
|
|
190
|
-
downloadUrl: '',
|
|
191
|
-
error: {
|
|
192
|
-
message: (error as Error).message,
|
|
193
|
-
},
|
|
194
|
-
key: '',
|
|
195
|
-
success: false,
|
|
196
|
-
uploadUrl: '',
|
|
197
|
-
} as GetExportFileUploadUrlResult;
|
|
198
|
-
}
|
|
199
|
-
}),
|
|
200
|
-
|
|
201
|
-
/**
|
|
202
|
-
* Save exported file content to documents table
|
|
203
|
-
* This creates a document record linked to the file, allowing content to be retrieved
|
|
204
|
-
* when querying messages with file attachments
|
|
205
|
-
*/
|
|
206
|
-
saveExportedFileContent: codeInterpreterProcedure
|
|
207
|
-
.input(saveExportedFileContentSchema)
|
|
208
|
-
.use(serverDatabase)
|
|
209
|
-
.mutation(async ({ ctx, input }) => {
|
|
210
|
-
const { content, fileId, fileType, filename, url } = input;
|
|
211
|
-
|
|
212
|
-
log('Saving exported file content: fileId=%s, filename=%s', fileId, filename);
|
|
213
|
-
|
|
214
|
-
try {
|
|
215
|
-
const documentModel = new DocumentModel(ctx.serverDB, ctx.userId);
|
|
216
|
-
const fileModel = new FileModel(ctx.serverDB, ctx.userId);
|
|
217
|
-
|
|
218
|
-
// Verify the file exists
|
|
219
|
-
const file = await fileModel.findById(fileId);
|
|
220
|
-
if (!file) {
|
|
221
|
-
return {
|
|
222
|
-
error: { message: 'File not found' },
|
|
223
|
-
success: false,
|
|
224
|
-
} as SaveExportedFileContentResult;
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
// Create document record with the file content
|
|
228
|
-
const document = await documentModel.create({
|
|
229
|
-
content,
|
|
230
|
-
fileId,
|
|
231
|
-
fileType,
|
|
232
|
-
filename,
|
|
233
|
-
source: url,
|
|
234
|
-
sourceType: 'file',
|
|
235
|
-
title: filename,
|
|
236
|
-
totalCharCount: content.length,
|
|
237
|
-
totalLineCount: content.split('\n').length,
|
|
238
|
-
});
|
|
239
|
-
|
|
240
|
-
log('Created document for exported file: documentId=%s, fileId=%s', document.id, fileId);
|
|
241
|
-
|
|
242
|
-
return {
|
|
243
|
-
documentId: document.id,
|
|
244
|
-
success: true,
|
|
245
|
-
} as SaveExportedFileContentResult;
|
|
246
|
-
} catch (error) {
|
|
247
|
-
log('Error saving exported file content: %O', error);
|
|
248
|
-
|
|
249
|
-
return {
|
|
250
|
-
error: { message: (error as Error).message },
|
|
251
|
-
success: false,
|
|
252
|
-
} as SaveExportedFileContentResult;
|
|
253
|
-
}
|
|
254
|
-
}),
|
|
255
|
-
});
|