connectedApps.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. import { randomUUID, createHash } from 'node:crypto'
  2. import { z } from 'zod'
  3. import type { WorkspaceStore } from '../workspaceStore.js'
  4. import type { WorkspaceNodes } from '../workspaceGateway.js'
  5. import { HttpError } from '../errors.js'
  6. import type { BotAppCard, BotAppConnection, BotPluginSettings } from '../../shared/workspacePlugins.js'
  7. import { bundledProviderLogo, safeProviderLogo } from '../../shared/botProviderLogos.js'
  8. const API = 'https://backend.composio.dev/api/v3.1'
  9. const CATALOG_API = 'https://backend.composio.dev/api/v3'
  10. export const appSlug = z.string().regex(/^[a-z][a-z0-9_-]{0,80}$/)
  11. const sessionSchema = z.object({ session_id: z.string().min(1), mcp: z.object({ url: z.string().url(), headers: z.record(z.string(), z.string()).optional() }), config: z.object({ user_id: z.string().optional() }).passthrough().optional() }).passthrough()
  12. type Session = z.infer<typeof sessionSchema>
  13. interface Configuration { owner: string; key: string; userId: string; revision: number; session?: Session; authConfigs: Record<string, string> }
  14. export const CURATED_APPS: BotAppCard[] = [
  15. ['gmail', 'Gmail', '邮件与草稿'], ['googledrive', 'Google Drive', '云端文件'], ['googlecalendar', 'Google Calendar', '日历与日程'],
  16. ['github', 'GitHub', '代码仓库与议题'], ['notion', 'Notion', '文档与知识库'], ['slack', 'Slack', '团队沟通'],
  17. ['linear', 'Linear', '项目与任务'], ['discord', 'Discord', '社区消息'], ['outlook', 'Outlook', '邮件与日历'],
  18. ['trello', 'Trello', '看板与任务'], ['airtable', 'Airtable', '表格与业务数据'], ['twitter', 'X', '社交内容'],
  19. ].map(([slug, name, description]) => ({ slug: slug!, name: name!, description: description!, logo: bundledProviderLogo(slug!) }))
  20. function trustedUrl(value: string): string {
  21. let url: URL
  22. try { url = new URL(value) } catch { throw new HttpError(502, '应用连接服务返回的地址无效', 'plugin_untrusted_url') }
  23. if (url.protocol !== 'https:' || url.username || url.password || !(url.hostname === 'composio.dev' || url.hostname.endsWith('.composio.dev'))) throw new HttpError(502, '应用连接服务返回的地址无效', 'plugin_untrusted_url')
  24. return url.href
  25. }
  26. /** Account-owned Composio sessions. Keys, session URLs and remote credentials
  27. * remain on the Web server, separate from ordinary Hermes configuration. */
  28. export class ConnectedApps {
  29. private catalogCache = new Map<string, { at: number; cards: BotAppCard[] }>()
  30. private sessions = new Map<string, Promise<Session>>()
  31. constructor(readonly store: WorkspaceStore, readonly nodes: WorkspaceNodes, readonly fetchImpl: typeof fetch, readonly assertOwner: (owner: string) => void, readonly invalidate: (owner: string) => void) {}
  32. read(owner: string): Configuration | undefined {
  33. const record = this.store.get<{ sealed: string }>(owner, 'bot-plugin-settings', 'apps')
  34. if (!record) return undefined
  35. const value = this.nodes.open<Configuration>(record.sealed)
  36. if (value.owner !== owner) throw new HttpError(403, '应用配置不属于当前账号', 'plugin_forbidden')
  37. return value
  38. }
  39. status(owner: string): BotPluginSettings { const value = this.read(owner); return { configured: !!value?.key, revision: value?.revision ?? 0 } }
  40. private current(owner: string, revision: number) {
  41. this.assertOwner(owner)
  42. if (this.status(owner).revision !== revision) throw new HttpError(409, '应用配置已变化,请刷新后重试', 'plugin_configuration_changed')
  43. }
  44. private async request(key: string, path: string, body?: unknown, method = body === undefined ? 'GET' : 'POST', signal?: AbortSignal, base = API): Promise<any> {
  45. const response = await this.fetchImpl(base + path, { method, redirect: 'error', headers: { 'x-api-key': key, Accept: 'application/json', ...(body === undefined ? {} : { 'Content-Type': 'application/json' }) }, ...(body === undefined ? {} : { body: JSON.stringify(body) }), signal: AbortSignal.any([AbortSignal.timeout(20_000), ...(signal ? [signal] : [])]) })
  46. if (!response.ok) { await response.body?.cancel(); throw new HttpError(502, `应用连接服务请求失败(HTTP ${response.status}),请检查 API Key 和项目权限`, 'connected_apps_unavailable') }
  47. if (response.status === 204) return {}
  48. const reader = response.body?.getReader(); if (!reader) return {}
  49. const chunks: Uint8Array[] = []; let size = 0
  50. try { for (;;) { const next = await reader.read(); if (next.done) break; size += next.value.byteLength; if (size > 8 * 1024 * 1024) throw new HttpError(502, '应用连接服务响应过大', 'plugin_response_limit'); chunks.push(next.value) } }
  51. finally { await reader.cancel().catch(() => {}) }
  52. try { return JSON.parse(Buffer.concat(chunks).toString() || '{}') } catch { throw new HttpError(502, '应用连接服务响应格式无效', 'plugin_invalid_response') }
  53. }
  54. private async createSession(config: Configuration, slugs?: string[], signal?: AbortSignal): Promise<Session> {
  55. const raw = await this.request(config.key, '/tool_router/session', {
  56. user_id: config.userId, ...(slugs ? { toolkits: { enable: slugs } } : {}), auth_configs: config.authConfigs,
  57. manage_connections: { enable: !slugs, enable_wait_for_connections: false, enable_connection_removal: false },
  58. multi_account: { enable: true, max_accounts_per_toolkit: 5, require_explicit_selection: true },
  59. }, 'POST', signal)
  60. const session = sessionSchema.safeParse(raw)
  61. if (!session.success || (session.data.config?.user_id && session.data.config.user_id !== config.userId)) throw new HttpError(502, '应用连接会话身份无效', 'plugin_invalid_response')
  62. session.data.mcp.url = trustedUrl(session.data.mcp.url)
  63. return session.data
  64. }
  65. async configure(owner: string, key: string, revision: number, authConfigs: Record<string, string>) {
  66. this.current(owner, revision)
  67. const old = this.read(owner)
  68. const value: Configuration = { owner, key, revision: revision + 1, userId: old?.userId ?? `yaoyao_${randomUUID()}`, authConfigs }
  69. if (key) value.session = await this.createSession(value)
  70. this.current(owner, revision)
  71. this.store.put(owner, 'bot-plugin-settings', 'apps', { sealed: this.nodes.seal(value) })
  72. this.catalogCache.delete(owner); this.invalidate(owner)
  73. return this.status(owner)
  74. }
  75. private require(owner: string) { const value = this.read(owner); if (!value?.key) throw new HttpError(409, '请先在已连接应用中配置 Composio API Key', 'connected_apps_unconfigured'); return value }
  76. async catalog(owner: string): Promise<{ cards: BotAppCard[]; source: 'api' | 'curated' }> {
  77. const value = this.read(owner), cache = this.catalogCache.get(owner)
  78. if (cache && Date.now() - cache.at < 600_000) return { cards: cache.cards, source: 'api' }
  79. if (value?.key) try {
  80. const items = await this.pages(value.key, '/toolkits', { limit: '500', sort_by: 'usage' }, undefined, CATALOG_API)
  81. this.current(owner, value.revision)
  82. const cards = items.filter(item => appSlug.safeParse(item.slug).success).map(item => ({ slug: item.slug, name: String(item.name ?? item.slug).slice(0, 100), description: String(item.meta?.description ?? item.description ?? '').slice(0, 180), logo: safeProviderLogo(item.meta?.logo ?? item.logo) ?? bundledProviderLogo(item.slug) }))
  83. if (cards.length) { this.catalogCache.set(owner, { at: Date.now(), cards }); return { cards, source: 'api' } }
  84. } catch { /* Catalog fallback never claims that account inventory is empty. */ }
  85. return { cards: CURATED_APPS, source: 'curated' }
  86. }
  87. private async pages(key: string, path: string, params: Record<string, string>, signal?: AbortSignal, base = API): Promise<any[]> {
  88. const items: any[] = [], cursors = new Set<string>(); let cursor = ''
  89. for (let page = 0; page < 20; page++) {
  90. const query = new URLSearchParams({ ...params, ...(cursor ? { cursor } : {}) })
  91. const result = await this.request(key, path + '?' + query, undefined, 'GET', signal, base)
  92. if (!Array.isArray(result.items)) throw new HttpError(502, '应用列表响应格式无效', 'plugin_invalid_response')
  93. items.push(...result.items)
  94. if (!result.next_cursor) return items
  95. if (typeof result.next_cursor !== 'string' || cursors.has(result.next_cursor)) break
  96. cursor = result.next_cursor; cursors.add(cursor)
  97. }
  98. throw new HttpError(502, '应用列表尚未完整读取,请重试', 'plugin_inventory_incomplete')
  99. }
  100. async inventory(owner: string): Promise<BotAppConnection[]> {
  101. const config = this.require(owner)
  102. const accounts = await this.pages(config.key, '/connected_accounts', { user_ids: config.userId, limit: '100' })
  103. this.current(owner, config.revision)
  104. const groups = new Map<string, BotAppConnection>()
  105. for (const account of accounts) {
  106. const slug = String(account.toolkit?.slug ?? '').toLowerCase()
  107. if (!appSlug.safeParse(slug).success || !/^[A-Za-z0-9_-]{1,128}$/.test(account.id) || (account.user_id && account.user_id !== config.userId)) continue
  108. const grant = this.grant(owner, slug)
  109. const group = groups.get(slug) ?? { slug, accounts: [], ...grant }
  110. group.accounts.push({ id: account.id, alias: typeof account.alias === 'string' ? account.alias.slice(0, 64) : undefined, status: String(account.status ?? 'UNKNOWN').slice(0, 40) })
  111. groups.set(slug, group)
  112. }
  113. return [...groups.values()]
  114. }
  115. grant(owner: string, slug: string): { agentIds: string[]; revision: number } { return this.store.get(owner, 'bot-app-grant', slug) ?? { agentIds: [], revision: 0 } }
  116. async authorize(owner: string, slug: string, alias?: string) {
  117. const config = this.require(owner), inventory = await this.inventory(owner)
  118. const existing = inventory.find(item => item.slug === slug)?.accounts ?? []
  119. if (existing.some(a => /^(ACTIVE|INITIATED|PENDING|INITIALIZING)$/i.test(a.status)) && !alias) throw new HttpError(409, '添加另一个账号时请填写账号别名', 'plugin_alias_required')
  120. if (alias && existing.some(a => a.alias?.toLowerCase() === alias.toLowerCase())) throw new HttpError(409, '该账号别名已经存在', 'plugin_alias_conflict')
  121. if (!config.session) throw new HttpError(409, '请重新保存应用连接服务配置', 'plugin_session_missing')
  122. const result = await this.request(config.key, `/tool_router/session/${encodeURIComponent(config.session.session_id)}/link`, { toolkit: slug, ...(alias ? { alias } : {}) })
  123. this.current(owner, config.revision)
  124. return { url: trustedUrl(String(result.redirect_url ?? '')) }
  125. }
  126. async disconnect(owner: string, slug: string, id: string) {
  127. const config = this.require(owner)
  128. if (!(await this.inventory(owner)).some(item => item.slug === slug && item.accounts.some(a => a.id === id))) throw new HttpError(404, '应用账号不存在', 'not_found')
  129. this.current(owner, config.revision)
  130. await this.request(config.key, `/connected_accounts/${encodeURIComponent(id)}?revoke_on_delete=true`, undefined, 'DELETE')
  131. this.invalidate(owner)
  132. }
  133. async sessionFor(owner: string, agentId: string, slugs: string[], signal: AbortSignal): Promise<{ url: string; headers: Record<string, string>; revision: number }> {
  134. const config = this.require(owner)
  135. const id = createHash('sha256').update(JSON.stringify([agentId, config.revision, [...slugs].sort()])).digest('hex')
  136. const saved = this.store.get<{ sealed: string }>(owner, 'bot-app-session', id)
  137. const cacheKey = owner + ':' + id
  138. let session = saved ? this.nodes.open<{ owner: string; session: Session }>(saved.sealed) : undefined
  139. if (session && session.owner !== owner) throw new HttpError(403, '应用会话身份无效', 'plugin_forbidden')
  140. if (!session) {
  141. let pending = this.sessions.get(cacheKey)
  142. if (!pending) { pending = this.createSession(config, slugs, signal); this.sessions.set(cacheKey, pending); void pending.finally(() => this.sessions.delete(cacheKey)).catch(() => {}) }
  143. session = { owner, session: await pending }; this.current(owner, config.revision)
  144. this.store.put(owner, 'bot-app-session', id, { sealed: this.nodes.seal(session) })
  145. }
  146. this.current(owner, config.revision)
  147. return { url: trustedUrl(session.session.mcp.url), headers: { ...session.session.mcp.headers, 'x-api-key': config.key }, revision: config.revision }
  148. }
  149. }