🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.
TypeScript agentagent-collaborationagent-harnessloop-engineeringmcppersonal-assistantproactive-agentskillssuper-agenttask-management
🐛 fix: add the creds tools into execAgentRuntime (#13399)
LiJian9c08fa5cdf21
diff --git a/packages/builtin-tool-creds/src/ExecutionRuntime/index.ts b/packages/builtin-tool-creds/src/ExecutionRuntime/index.tsnew file mode 100644index 00000000000..03f73b32dba--- /dev/null+++ b/packages/builtin-tool-creds/src/ExecutionRuntime/index.ts@@ -0,0 +1,372 @@+import { getLobehubSkillProviderById } from '@lobechat/const';+import type { BuiltinServerRuntimeOutput } from '@lobechat/types';++import type {+ GetPlaintextCredParams,+ InitiateOAuthConnectParams,+ InjectCredsToSandboxParams,+ SaveCredsParams,+} from '../types';++/**+ * Service interface for Credentials operations+ * Abstracted to allow different implementations (e.g., MarketService-based)+ */+export interface ICredsService {+ /**+ * Get plaintext credential by key+ */+ getByKey: (+ key: string,+ options?: { decrypt?: boolean },+ ) => Promise<{+ fileName?: string;+ fileUrl?: string;+ name?: string;+ plaintext?: Record<string, string>;+ type: string;+ values?: Record<string, string>;+ }>;++ /**+ * Get OAuth authorization URL+ */+ getOAuthAuthorizeUrl: (+ provider: string,+ redirectUri: string,+ ) => Promise<{+ authorizeUrl: string;+ }>;++ /**+ * Check if OAuth connection exists+ */+ getOAuthConnectionStatus: (provider: string) => Promise<{+ connected: boolean;+ }>;++ /**+ * Inject credentials into sandbox+ */+ injectCreds: (params: {+ keys: string[];+ sandbox?: boolean;+ topicId: string;+ userId: string;+ }) => Promise<{+ credentials?: {+ env?: Record<string, string>;+ files?: Array<{ filename: string; key: string; path: string }>;+ };+ notFound?: string[];+ success: boolean;+ unsupportedInSandbox?: string[];+ }>;++ /**+ * List all user credentials+ */+ listCreds: () => Promise<{+ data?: Array<{ id: number; key: string }>;+ }>;++ /**+ * Save KV credential+ */+ saveKVCred: (params: {+ description?: string;+ key: string;+ name: string;+ type: 'kv-env' | 'kv-header';+ values: Record<string, string>;+ }) => Promise<{ id: number }>;+}++/**+ * Creds Execution Runtime (Server-side)+ *+ * This runtime executes creds tools via the injected ICredsService.+ * The service handles context (userId, topicId) internally.+ *+ * Key differences from frontend executor:+ * - No browser APIs (window.open for OAuth)+ * - Direct service calls instead of tRPC client+ * - For OAuth: returns authorization URL instead of opening popup+ */+export class CredsExecutionRuntime {+ private credsService: ICredsService;+ private context: { topicId?: string; userId?: string };++ constructor(credsService: ICredsService, context: { topicId?: string; userId?: string } = {}) {+ this.credsService = credsService;+ this.context = context;+ }++ /**+ * Initiate OAuth connection flow+ * In server-side context, returns authorization URL for the user to click+ * (cannot open popup like frontend executor)+ */+ async initiateOAuthConnect(+ args: InitiateOAuthConnectParams,+ ): Promise<BuiltinServerRuntimeOutput> {+ try {+ const { provider } = args;++ // Get provider config for display name+ const providerConfig = getLobehubSkillProviderById(provider);+ if (!providerConfig) {+ return {+ content: `Unknown OAuth provider: ${provider}. Available providers: github, linear, microsoft, twitter`,+ error: {+ message: `Unknown OAuth provider: ${provider}`,+ type: 'UnknownProvider',+ },+ success: false,+ };+ }++ // Check if already connected+ const statusResponse = await this.credsService.getOAuthConnectionStatus(provider);+ if (statusResponse.connected) {+ return {+ content: `You are already connected to ${providerConfig.label}. The credential is available for use.`,+ state: {+ alreadyConnected: true,+ providerName: providerConfig.label,+ },+ success: true,+ };+ }++ // Get the authorization URL+ // Note: In background execution, we cannot use window.location.origin+ // The redirect URI should be configured for server-side OAuth flow+ const redirectUri = `https://app.lobehub.com/oauth/callback/success?provider=${provider}`;+ const response = await this.credsService.getOAuthAuthorizeUrl(provider, redirectUri);++ // In server-side context, return the URL for user to click+ // This is different from frontend which opens a popup+ return {+ content: `To connect to ${providerConfig.label}, please click the following authorization link:\n\n${response.authorizeUrl}\n\nAfter authorization, the credential will be automatically saved and available for use.`,+ state: {+ authorizeUrl: response.authorizeUrl,+ connected: false,+ providerName: providerConfig.label,+ requiresUserAction: true,+ },+ success: true,+ };+ } catch (error) {+ return {+ content: `Failed to initiate OAuth connection: ${error instanceof Error ? error.message : 'Unknown error'}`,+ error: {+ message: error instanceof Error ? error.message : 'Failed to initiate OAuth connection',+ type: 'InitiateOAuthFailed',+ },+ success: false,+ };+ }+ }++ /**+ * Get plaintext credential value by key+ */+ async getPlaintextCred(args: GetPlaintextCredParams): Promise<BuiltinServerRuntimeOutput> {+ try {+ const result = await this.credsService.getByKey(args.key, { decrypt: true });++ const credType = result.type;+ const credName = result.name || args.key;++ // Handle file type credentials+ if (credType === 'file') {+ const fileUrl = result.fileUrl;+ const fileName = result.fileName;++ if (!fileUrl) {+ return {+ content: `File credential "${credName}" (key: ${args.key}) found but file URL is not available.`,+ error: {+ message: 'File URL not available',+ type: 'FileUrlNotAvailable',+ },+ success: false,+ };+ }++ return {+ content: `Successfully retrieved file credential "${credName}" (key: ${args.key}). File: ${fileName || 'unknown'}. The file download URL is available in the state.`,+ state: {+ fileName,+ fileUrl,+ key: args.key,+ name: credName,+ type: 'file',+ },+ success: true,+ };+ }++ // Handle KV types (kv-env, kv-header, oauth)+ const values = result.values || result.plaintext || {};+ const valueKeys = Object.keys(values);++ // Return content with masked values for security, but include actual values in state+ const maskedValues = valueKeys.map((k) => `${k}: ****`).join(', ');++ return {+ content: `Successfully retrieved credential "${credName}" (key: ${args.key}). Contains ${valueKeys.length} value(s): ${maskedValues}. The actual values are available in the state for use.`,+ state: {+ key: args.key,+ name: credName,+ type: credType,+ values,+ },+ success: true,+ };+ } catch (error) {+ const errorMessage = error instanceof Error ? error.message : 'Unknown error';+ const isNotFound = errorMessage.includes('not found') || errorMessage.includes('NOT_FOUND');++ return {+ content: isNotFound+ ? `Credential not found: ${args.key}. Please check if the credential exists in Settings > Credentials.`+ : `Failed to get credential: ${errorMessage}`,+ error: {+ message: errorMessage,+ type: isNotFound ? 'CredentialNotFound' : 'GetCredentialFailed',+ },+ success: false,+ };+ }+ }++ /**+ * Inject credentials to sandbox environment+ */+ async injectCredsToSandbox(+ args: InjectCredsToSandboxParams,+ ): Promise<BuiltinServerRuntimeOutput> {+ try {+ const topicId = this.context.topicId;+ if (!topicId) {+ return {+ content: 'Cannot inject credentials: topicId is not available in the current context.',+ error: {+ message: 'topicId is required but not available',+ type: 'MissingTopicId',+ },+ success: false,+ };+ }++ const userId = this.context.userId;+ if (!userId) {+ return {+ content: 'Cannot inject credentials: user is not authenticated.',+ error: {+ message: 'userId is required but not available',+ type: 'MissingUserId',+ },+ success: false,+ };+ }++ const result = await this.credsService.injectCreds({+ keys: args.keys,+ sandbox: true,+ topicId,+ userId,+ });++ const credentials = result.credentials || {};+ const notFound = result.notFound || [];+ const unsupportedInSandbox = result.unsupportedInSandbox || [];++ // Build response content+ const injectedKeys = args.keys.filter((k) => !notFound.includes(k));+ let content = '';++ if (injectedKeys.length > 0) {+ content = `Credentials injected successfully: ${injectedKeys.join(', ')}.`;+ }++ if (notFound.length > 0) {+ content += ` Not found: ${notFound.join(', ')}. Please configure them in Settings > Credentials.`;+ }++ if (unsupportedInSandbox.length > 0) {+ content += ` Not supported in sandbox: ${unsupportedInSandbox.join(', ')}.`;+ }++ return {+ content: content.trim(),+ state: {+ credentials,+ injected: injectedKeys,+ notFound,+ success: notFound.length === 0,+ unsupportedInSandbox,+ },+ success: true,+ };+ } catch (error) {+ return {+ content: `Failed to inject credentials: ${error instanceof Error ? error.message : 'Unknown error'}`,+ error: {+ message: error instanceof Error ? error.message : 'Failed to inject credentials',+ type: 'InjectCredentialsFailed',+ },+ success: false,+ };+ }+ }++ /**+ * Save new credentials+ */+ async saveCreds(args: SaveCredsParams): Promise<BuiltinServerRuntimeOutput> {+ try {+ // Only support kv-env and kv-header types in server runtime+ // File upload requires frontend interaction+ if (args.type !== 'kv-env' && args.type !== 'kv-header') {+ return {+ content: `Credential type "${args.type}" is not supported in background execution. Only kv-env and kv-header types are supported.`,+ error: {+ message: `Unsupported credential type: ${args.type}`,+ type: 'UnsupportedCredentialType',+ },+ success: false,+ };+ }++ await this.credsService.saveKVCred({+ description: args.description,+ key: args.key,+ name: args.name,+ type: args.type,+ values: args.values,+ });++ return {+ content: `Credential "${args.name}" saved successfully with key "${args.key}"`,+ state: {+ key: args.key,+ message: `Credential "${args.name}" saved successfully`,+ success: true,+ },+ success: true,+ };+ } catch (error) {+ return {+ content: `Failed to save credential: ${error instanceof Error ? error.message : 'Unknown error'}`,+ error: {+ message: error instanceof Error ? error.message : 'Failed to save credential',+ type: 'SaveCredentialFailed',+ },+ success: false,+ };+ }+ }+}diff --git a/packages/builtin-tool-creds/src/index.ts b/packages/builtin-tool-creds/src/index.tsindex a8ebf585f48..6bd006d0867 100644--- a/packages/builtin-tool-creds/src/index.ts+++ b/packages/builtin-tool-creds/src/index.ts@@ -1,3 +1,4 @@+export { CredsExecutionRuntime, type ICredsService } from './ExecutionRuntime';export {checkCredsSatisfied,type CredRequirement,@@ -15,6 +16,7 @@ export {type CredSummaryForContext,type GetPlaintextCredParams,type GetPlaintextCredState,+ type InitiateOAuthConnectParams,type InjectCredsToSandboxParams,type InjectCredsToSandboxState,type SaveCredsParams,diff --git a/src/server/services/toolExecution/serverRuntimes/creds.ts b/src/server/services/toolExecution/serverRuntimes/creds.tsnew file mode 100644index 00000000000..086eb73c295--- /dev/null+++ b/src/server/services/toolExecution/serverRuntimes/creds.ts@@ -0,0 +1,167 @@+import {+ CredsExecutionRuntime,+ CredsIdentifier,+ type ICredsService,+} from '@lobechat/builtin-tool-creds';+import debug from 'debug';++import { MarketService } from '@/server/services/market';++import { type ServerRuntimeRegistration } from './types';++const log = debug('lobe-server:creds-runtime');++/**+ * Server-side Creds Service implementation+ * Wraps MarketService.market.creds to provide ICredsService interface+ */+class ServerCredsService implements ICredsService {+ private marketService: MarketService;++ constructor(marketService: MarketService) {+ this.marketService = marketService;+ }++ async getByKey(+ key: string,+ options?: { decrypt?: boolean },+ ): Promise<{+ fileName?: string;+ fileUrl?: string;+ name?: string;+ plaintext?: Record<string, string>;+ type: string;+ values?: Record<string, string>;+ }> {+ log('getByKey: key=%s, decrypt=%s', key, options?.decrypt);++ // First find the credential by key from the list+ const listResult = await this.marketService.market.creds.list();+ const cred = listResult.data?.find((c) => c.key === key);++ if (!cred) {+ throw new Error(`Credential not found: ${key}`);+ }++ // Then get the full credential with optional decryption+ const result = await this.marketService.market.creds.get(cred.id, {+ decrypt: options?.decrypt,+ });++ log('getByKey success: key=%s, id=%d', key, cred.id);++ return result as any;+ }++ async getOAuthAuthorizeUrl(+ provider: string,+ redirectUri: string,+ ): Promise<{+ authorizeUrl: string;+ }> {+ log('getOAuthAuthorizeUrl: provider=%s', provider);++ const response = await this.marketService.market.connect.authorize(provider, {+ redirect_uri: redirectUri,+ });++ return {+ authorizeUrl: response.authorize_url,+ };+ }++ async getOAuthConnectionStatus(provider: string): Promise<{+ connected: boolean;+ }> {+ log('getOAuthConnectionStatus: provider=%s', provider);++ const response = await this.marketService.market.connect.getStatus(provider);++ return {+ connected: response.connected,+ };+ }++ async injectCreds(params: {+ keys: string[];+ sandbox?: boolean;+ topicId: string;+ userId: string;+ }): Promise<{+ credentials?: {+ env?: Record<string, string>;+ files?: Array<{ filename: string; key: string; path: string }>;+ };+ notFound?: string[];+ success: boolean;+ unsupportedInSandbox?: string[];+ }> {+ log('injectCreds: keys=%O, topicId=%s', params.keys, params.topicId);++ const result = await this.marketService.market.creds.inject({+ keys: params.keys,+ sandbox: params.sandbox,+ topicId: params.topicId,+ userId: params.userId,+ });++ log('injectCreds success: notFound=%d', result.notFound?.length || 0);++ return result as any;+ }++ async listCreds(): Promise<{+ data?: Array<{ id: number; key: string }>;+ }> {+ log('listCreds');++ const result = await this.marketService.market.creds.list();++ log('listCreds success: %d credentials', result.data?.length || 0);++ return result as any;+ }++ async saveKVCred(params: {+ description?: string;+ key: string;+ name: string;+ type: 'kv-env' | 'kv-header';+ values: Record<string, string>;+ }): Promise<{ id: number }> {+ log('saveKVCred: key=%s, name=%s, type=%s', params.key, params.name, params.type);++ const result = await this.marketService.market.creds.createKV(params);++ log('saveKVCred success: id=%d', result.id);++ return result;+ }+}++/**+ * Creds Server Runtime+ * Per-request runtime (needs userId, topicId)+ */+export const credsRuntime: ServerRuntimeRegistration = {+ factory: (context) => {+ if (!context.userId) {+ throw new Error('userId is required for Creds execution');+ }++ log(+ 'Creating CredsExecutionRuntime for userId=%s, topicId=%s',+ context.userId,+ context.topicId,+ );++ const marketService = new MarketService({ userInfo: { userId: context.userId } });+ const credsService = new ServerCredsService(marketService);++ return new CredsExecutionRuntime(credsService, {+ topicId: context.topicId,+ userId: context.userId,+ });+ },+ identifier: CredsIdentifier,+};diff --git a/src/server/services/toolExecution/serverRuntimes/index.ts b/src/server/services/toolExecution/serverRuntimes/index.tsindex 4595212a0a7..dc1e75365f1 100644--- a/src/server/services/toolExecution/serverRuntimes/index.ts+++ b/src/server/services/toolExecution/serverRuntimes/index.ts@@ -12,6 +12,7 @@ import { agentDocumentsRuntime } from './agentDocuments';import { briefRuntime } from './brief';import { calculatorRuntime } from './calculator';import { cloudSandboxRuntime } from './cloudSandbox';+import { credsRuntime } from './creds';import { localSystemRuntime } from './localSystem';import { memoryRuntime } from './memory';import { notebookRuntime } from './notebook';@@ -53,6 +54,7 @@ registerRuntimes([briefRuntime,taskRuntime,topicReferenceRuntime,+ credsRuntime,]);// ==================== Registry API ====================