From 1e97953d678754aafea68aee6ebf08bbf397e683 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=94=B5=E6=91=87=E5=B0=8F=E5=AD=90?= Date: Wed, 15 Jul 2026 15:23:16 +0800 Subject: [PATCH 1/3] =?UTF-8?q?feat:=20=E9=9B=86=E6=88=90=20Agent=20Hub=20?= =?UTF-8?q?=E5=BE=AE=E4=BF=A1=E6=9C=BA=E5=99=A8=E4=BA=BA=E8=83=BD=E5=8A=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + electron-builder.yml | 1 + package.json | 16 +- scripts/build-wechat-connector.cjs | 67 + src/main/http-server.ts | 68 +- src/main/index.ts | 23 + .../services/agent-group-report-service.ts | 89 + src/main/services/agent-hub-service.ts | 672 ++++ src/main/services/local-api-test-service.ts | 7 +- src/main/services/settings-store.ts | 16 +- src/preload/index.d.ts | 19 + src/preload/index.ts | 23 +- src/renderer/src/App.tsx | 36 +- src/renderer/src/assets/main.css | 2942 ++++++++++++++++- .../components/layout/PrimaryNavigation.tsx | 10 + .../src/components/layout/navigation.ts | 3 +- .../features/agent-hub/AgentHubWorkspace.tsx | 269 ++ .../components/ApiRequestTester.tsx | 19 + .../hooks/useApiCenterController.ts | 19 +- .../features/api-center/model/apiEndpoints.ts | 14 + .../api-center/model/requestPresets.ts | 12 + .../settings/pages/AccountDatabasePage.tsx | 35 + src/renderer/src/utils/group-report-facts.ts | 32 +- src/shared/agent-hub.ts | 38 + src/shared/local-api-test.ts | 5 +- 25 files changed, 4249 insertions(+), 187 deletions(-) create mode 100644 scripts/build-wechat-connector.cjs create mode 100644 src/main/services/agent-group-report-service.ts create mode 100644 src/main/services/agent-hub-service.ts create mode 100644 src/renderer/src/features/agent-hub/AgentHubWorkspace.tsx create mode 100644 src/shared/agent-hub.ts diff --git a/.gitignore b/.gitignore index d196c22..9527636 100644 --- a/.gitignore +++ b/.gitignore @@ -6,6 +6,7 @@ out .DS_Store .eslintcache *.log* +resources/connectors/wechat/ .omc .codex/ docs/design/ diff --git a/electron-builder.yml b/electron-builder.yml index 04b427c..094c3fb 100644 --- a/electron-builder.yml +++ b/electron-builder.yml @@ -16,6 +16,7 @@ extraMetadata: asarUnpack: - resources/** extraResources: + # Includes the optional WeChat connector binary for the target platform. - from: resources to: resources filter: diff --git a/package.json b/package.json index 59e6ff5..dd0002c 100644 --- a/package.json +++ b/package.json @@ -25,16 +25,20 @@ "test:skill-install": "node scripts/test-skill-install-instruction.cjs", "cp:env": "node scripts/ensure-env.cjs", "prepare:env": "node scripts/ensure-env.cjs", - "predev": "node scripts/ensure-env.cjs", + "predev": "node scripts/ensure-env.cjs && node scripts/build-wechat-connector.cjs", "start": "electron-vite preview", "dev": "electron-vite dev", - "build": "npm run typecheck && electron-vite build", + "build:wechat-connector": "node scripts/build-wechat-connector.cjs", + "build:wechat-connector:win": "node scripts/build-wechat-connector.cjs --platform win32 --arch x64,arm64", + "build:wechat-connector:mac": "node scripts/build-wechat-connector.cjs --platform darwin --arch x64,arm64", + "build:native-services": "npm run build:wechat-connector", + "build": "npm run typecheck && npm run build:native-services && electron-vite build", "postinstall": "electron-builder install-app-deps && node scripts/prepare-electron-runtime.cjs", "build:unpack": "npm run build && electron-builder --config electron-builder.yml --dir", - "build:win": "npm run build && electron-builder --config electron-builder.yml --win", - "build:mac:x64": "electron-vite build && electron-builder --config electron-builder.yml --mac --x64", - "build:mac:arm64": "electron-vite build && electron-builder --config electron-builder.yml --mac --arm64", - "release:mac": "electron-vite build && electron-builder --config electron-builder.yml --mac --x64 --arm64 --publish always", + "build:win": "npm run typecheck && npm run build:wechat-connector:win && electron-vite build && electron-builder --config electron-builder.yml --win", + "build:mac:x64": "npm run typecheck && node scripts/build-wechat-connector.cjs --platform darwin --arch x64 && electron-vite build && electron-builder --config electron-builder.yml --mac --x64", + "build:mac:arm64": "npm run typecheck && node scripts/build-wechat-connector.cjs --platform darwin --arch arm64 && electron-vite build && electron-builder --config electron-builder.yml --mac --arm64", + "release:mac": "npm run typecheck && npm run build:wechat-connector:mac && electron-vite build && electron-builder --config electron-builder.yml --mac --x64 --arm64 --publish always", "build:linux": "electron-vite build && electron-builder --config electron-builder.yml --linux" }, "dependencies": { diff --git a/scripts/build-wechat-connector.cjs b/scripts/build-wechat-connector.cjs new file mode 100644 index 0000000..41f0f3a --- /dev/null +++ b/scripts/build-wechat-connector.cjs @@ -0,0 +1,67 @@ +/* eslint-disable @typescript-eslint/no-require-imports, @typescript-eslint/explicit-function-return-type */ +const { execFileSync } = require('node:child_process') +const fs = require('node:fs') +const path = require('node:path') + +const projectRoot = path.resolve(__dirname, '..') +const sourceDir = process.env.WECHAT_CONNECTOR_SOURCE + ? path.resolve(process.env.WECHAT_CONNECTOR_SOURCE) + : path.join(projectRoot, 'services', 'wechat-connector') +const outputRoot = path.join(projectRoot, 'resources', 'connectors', 'wechat') + +function normalizePlatform(value) { + if (value === 'win32' || value === 'windows') return 'windows' + if (value === 'darwin' || value === 'macos') return 'darwin' + if (value === 'linux') return 'linux' + throw new Error(`Unsupported connector platform: ${value}`) +} + +function normalizeArch(value) { + if (value === 'x64' || value === 'amd64') return 'amd64' + if (value === 'arm64') return 'arm64' + throw new Error(`Unsupported connector architecture: ${value}`) +} + +function parseTargets() { + const platformArg = process.argv.indexOf('--platform') + const archArg = process.argv.indexOf('--arch') + const platforms = platformArg >= 0 ? process.argv[platformArg + 1].split(',') : [process.platform] + const arches = archArg >= 0 ? process.argv[archArg + 1].split(',') : [process.arch] + return platforms.flatMap((platform) => + arches.map((arch) => ({ goos: normalizePlatform(platform), goarch: normalizeArch(arch) })) + ) +} + +if (!fs.existsSync(path.join(sourceDir, 'go.mod'))) { + const existingTargets = parseTargets().every((target) => { + const directoryName = `${target.goos === 'windows' ? 'win32' : target.goos}-${target.goarch === 'amd64' ? 'x64' : target.goarch}` + const executable = target.goos === 'windows' ? 'wechat-connector.exe' : 'wechat-connector' + return fs.existsSync(path.join(outputRoot, directoryName, executable)) + }) + if (existingTargets) { + console.log('[build-wechat-connector] source not configured; reusing existing binary') + process.exit(0) + } + throw new Error( + 'Wechat connector source is not included in this repository. Set WECHAT_CONNECTOR_SOURCE to a compatible connector checkout.' + ) +} + +fs.rmSync(outputRoot, { recursive: true, force: true }) + +for (const target of parseTargets()) { + const directoryName = `${target.goos === 'windows' ? 'win32' : target.goos}-${target.goarch === 'amd64' ? 'x64' : target.goarch}` + const outputDir = path.join(outputRoot, directoryName) + const outputPath = path.join( + outputDir, + target.goos === 'windows' ? 'wechat-connector.exe' : 'wechat-connector' + ) + fs.mkdirSync(outputDir, { recursive: true }) + execFileSync('go', ['build', '-trimpath', '-o', outputPath, '.'], { + cwd: sourceDir, + env: { ...process.env, GOOS: target.goos, GOARCH: target.goarch, CGO_ENABLED: '0' }, + stdio: 'inherit' + }) + if (target.goos !== 'windows') fs.chmodSync(outputPath, 0o755) + console.log(`[build-wechat-connector] built ${directoryName}: ${outputPath}`) +} diff --git a/src/main/http-server.ts b/src/main/http-server.ts index 4d10c62..f7ea559 100644 --- a/src/main/http-server.ts +++ b/src/main/http-server.ts @@ -9,6 +9,8 @@ import { } from './services/chat-service' import { exportGroupReport } from './group-report-service' import { GroupReportExportRequest } from '../shared/group-report' +import { generateAgentGroupReport } from './services/agent-group-report-service' +import { agentHubService } from './services/agent-hub-service' import { safeError, safeLog, safeWarn } from './safe-log' export const DEFAULT_HTTP_HOST = '127.0.0.1' @@ -157,7 +159,9 @@ const routes: Record = { if (keyword) { const lower = keyword.toLowerCase() groups = groups.filter( - (c) => c.m_nsNickName.toLowerCase().includes(lower) || c.m_nsUsrName.toLowerCase().includes(lower) + (c) => + c.m_nsNickName.toLowerCase().includes(lower) || + c.m_nsUsrName.toLowerCase().includes(lower) ) } sendJson(res, 200, { count: groups.length, chatrooms: groups }) @@ -236,13 +240,62 @@ const routes: Record = { try { request = JSON.parse(body) as GroupReportExportRequest } catch (error) { - return sendError(res, 400, '请求体 JSON 解析失败', error instanceof Error ? error.message : String(error)) + return sendError( + res, + 400, + '请求体 JSON 解析失败', + error instanceof Error ? error.message : String(error) + ) } if (!request?.report || !request?.metadata) { return sendError(res, 400, '请求体需包含 report 和 metadata 字段') } const result = await exportGroupReport(request) sendJson(res, result.success ? 200 : 500, result) + }, + + '/api/v1/agent/group-report': async ({ req, res, body }) => { + if (req.method !== 'POST') return sendError(res, 405, '需要 POST 请求') + if (!isReady()) return sendError(res, 503, 'WechatExplorer 数据库未初始化') + let request: { group?: string; range?: 'today' | 'yesterday' | '7days' } + try { + request = JSON.parse(typeof body === 'string' ? body : '{}') + } catch { + return sendError(res, 400, '请求体 JSON 解析失败') + } + const result = await generateAgentGroupReport({ + group: request.group || '', + range: request.range + }) + sendJson(res, result.success ? 200 : 400, result) + }, + + '/api/v1/agent/status': ({ res }) => { + const status = agentHubService.getStatus() + sendJson(res, 200, { + ok: status.hub === 'online' && status.connector === 'online', + hub: status.hub, + connector: status.connector, + dataApi: status.dataApi, + databaseReady: status.databaseReady, + accountId: status.accountId + }) + }, + + '/api/v1/agent/send': async ({ req, res, body }) => { + if (req.method !== 'POST') return sendError(res, 405, '需要 POST 请求') + let request: { to?: string; text?: string; media_url?: string } + try { + request = JSON.parse(typeof body === 'string' ? body : '{}') + } catch { + return sendError(res, 400, '请求体 JSON 解析失败') + } + const result = await agentHubService.testSend({ + to: request.to, + text: request.text, + mediaUrl: request.media_url + }) + sendJson(res, result.success ? 200 : result.status === 'token_expired' ? 401 : 503, result) } } @@ -311,7 +364,11 @@ export interface ApiServerState { } let singleton: HttpServerHandle | null = null -let singletonState: ApiServerState = { running: false, host: DEFAULT_HTTP_HOST, port: DEFAULT_HTTP_PORT } +let singletonState: ApiServerState = { + running: false, + host: DEFAULT_HTTP_HOST, + port: DEFAULT_HTTP_PORT +} function sleep(ms: number): Promise { return new Promise((resolve) => setTimeout(resolve, ms)) @@ -326,7 +383,10 @@ export const apiServer = { return { ...singletonState } }, - async start(host: string = DEFAULT_HTTP_HOST, port: number = DEFAULT_HTTP_PORT): Promise { + async start( + host: string = DEFAULT_HTTP_HOST, + port: number = DEFAULT_HTTP_PORT + ): Promise { if (singleton) { return this.getState() } diff --git a/src/main/index.ts b/src/main/index.ts index a39a1b9..c6f65e8 100644 --- a/src/main/index.ts +++ b/src/main/index.ts @@ -68,6 +68,7 @@ import { saveCachedMessages } from './services/bootstrap-cache' import { installSafeConsole } from './safe-log' +import { agentHubService } from './services/agent-hub-service' // electron-vite can close the child's stdout/stderr after spawning Electron. // Plain console.error then throws EPIPE on a closed pipe and crashes the IPC @@ -698,6 +699,25 @@ app.whenReady().then(async () => { return { success: false, error: error instanceof Error ? error.message : String(error) } } }) + ipcMain.handle('agent-hub:getStatus', () => agentHubService.getStatus()) + ipcMain.handle('agent-hub:getLogs', () => agentHubService.getLogs()) + ipcMain.handle('agent-hub:clearLogs', () => agentHubService.clearLogs()) + ipcMain.handle('agent-hub:startLogin', () => agentHubService.startLogin()) + ipcMain.handle('agent-hub:cancelLogin', () => agentHubService.cancelLogin()) + ipcMain.handle('agent-hub:reconnect', () => agentHubService.reconnect()) + ipcMain.handle('agent-hub:disconnect', () => agentHubService.disconnect()) + ipcMain.handle('agent-hub:selectTestImage', async (event) => { + const window = BrowserWindow.fromWebContents(event.sender) + const result = await dialog.showOpenDialog(window!, { + title: '选择要测试发送的图片', + properties: ['openFile'], + filters: [ + { name: '图片', extensions: ['png', 'jpg', 'jpeg', 'gif', 'webp'] }, + { name: '所有文件', extensions: ['*'] } + ] + }) + return result.canceled ? { canceled: true } : { canceled: false, path: result.filePaths[0] } + }) createWindow() @@ -707,6 +727,8 @@ app.whenReady().then(async () => { await apiServer.start(settings.apiHost, settings.apiPort) } + await agentHubService.start(settings) + if (TRAY_MODE) { app.dock?.hide() setupTray() @@ -730,6 +752,7 @@ app.on('window-all-closed', () => { }) app.on('before-quit', async () => { + agentHubService.stop() chat.setChatDb(null) await apiServer.stop().catch(() => undefined) if (tray) { diff --git a/src/main/services/agent-group-report-service.ts b/src/main/services/agent-group-report-service.ts new file mode 100644 index 0000000..2b42852 --- /dev/null +++ b/src/main/services/agent-group-report-service.ts @@ -0,0 +1,89 @@ +import type { Contact, Message } from '../../shared/types' +import { exportGroupReport } from '../group-report-service' +import { getGroupSnapshot, listMessages, resolveMd5 } from './chat-service' +import { AIProviderService } from './ai-provider-service' +import { + buildGroupReportInput, + getSummaryDateRange, + GROUP_REPORT_SYSTEM_PROMPT, + isInternalName, + parseGroupDailyReport, + type SummaryDateRange +} from '../../renderer/src/utils/group-report' + +const aiProvider = new AIProviderService() + +export interface AgentGroupReportRequest { + group: string + range?: SummaryDateRange +} + +export interface AgentGroupReportResult { + success: boolean + groupName?: string + pngPath?: string + messageCount?: number + error?: string +} + +export async function generateAgentGroupReport( + request: AgentGroupReportRequest +): Promise { + const query = String(request.group || '') + .trim() + .replace(/群聊?$/, '') + .trim() + if (!query) return { success: false, error: '缺少群聊名称' } + const contact = resolveMd5(query) + if (!contact) return { success: false, error: `没有找到群聊“${query}”` } + if (contact.type !== 'group' && !contact.m_nsUsrName.endsWith('@chatroom')) { + return { success: false, error: `“${query}”不是群聊` } + } + + const range = request.range === 'yesterday' || request.range === '7days' ? request.range : 'today' + const { startTime, endTime } = getSummaryDateRange(range) + let messages = listMessages(contact.md5, startTime, endTime) as Message[] + if (!messages.length) return { success: false, error: '所选时间范围没有可总结的消息' } + + const snapshot = getGroupSnapshot(contact.md5) + if (snapshot) { + const members = new Map( + snapshot.members.map((member) => [ + member.wxid, + { name: member.nickname, avatar: member.avatar } + ]) + ) + messages = messages.map((message) => { + if (!isInternalName(message.name)) return message + const member = members.get(String(message.senderId || message.name || '')) + return member?.name + ? { ...message, name: member.name, img: message.img || member.avatar } + : message + }) + } + + const input = await buildGroupReportInput(messages, contact as Contact, true, 'full') + const ai = await aiProvider.chat([ + { role: 'system', content: GROUP_REPORT_SYSTEM_PROMPT }, + { role: 'user', content: input.prompt } + ]) + if (!ai.success || !ai.data) return { success: false, error: ai.error || 'AI 总结失败' } + const report = parseGroupDailyReport( + ai.data, + input.topSpeakers, + input.activeTimeline, + input.voiceLeaderboard, + input.metadata, + input.media + ) + const exported = await exportGroupReport({ report, metadata: input.metadata }) + if (!exported.success || !exported.pngPath) { + return { success: false, error: exported.error || '总结图片生成失败' } + } + return { + success: true, + groupName: input.metadata.groupName, + pngPath: exported.pngPath, + messageCount: messages.length + } +} diff --git a/src/main/services/agent-hub-service.ts b/src/main/services/agent-hub-service.ts new file mode 100644 index 0000000..fcce51d --- /dev/null +++ b/src/main/services/agent-hub-service.ts @@ -0,0 +1,672 @@ +import { app, BrowserWindow } from 'electron' +import { ChildProcess, execFile, spawn } from 'child_process' +import { randomBytes, timingSafeEqual } from 'crypto' +import { appendFileSync, existsSync, mkdirSync, writeFileSync } from 'fs' +import { createServer, type IncomingMessage, type Server, type ServerResponse } from 'http' +import { dirname, join } from 'path' +import { promisify } from 'util' +import type { + AgentHubActionResult, + AgentHubLogEntry, + AgentHubLogLevel, + AgentHubLogSource, + AgentHubStatus +} from '../../shared/agent-hub' +import type { AppSettings } from './settings-store' +import { generateAgentGroupReport } from './agent-group-report-service' +import { isReady, listRecentChat } from './chat-service' + +const execFileAsync = promisify(execFile) +const HEALTH_INTERVAL_MS = 5_000 +const HUB_ADDR = '127.0.0.1:5300' +const HUB_HOST = '127.0.0.1' +const HUB_PORT = 5300 +const CONNECTOR_ADDR = '127.0.0.1:18011' +const MAX_LOG_ENTRIES = 800 + +interface InboundMessage { + account_id?: string + from_user_id?: string + message_id?: string | number + items?: Array<{ type?: number; text?: string }> +} + +interface GroupReportIntent { + group: string + range: 'today' | 'yesterday' | '7days' +} + +function resolveBundledBinary( + resourceSegments: string[], + executable: string, + packaged = app.isPackaged, + platform = process.platform, + arch = process.arch +): string { + const relativeSegments = [...resourceSegments, `${platform}-${arch}`, executable] + const packagedPath = join(process.resourcesPath, 'resources', ...relativeSegments) + const developmentPath = join(app.getAppPath(), 'resources', ...relativeSegments) + const candidates = packaged ? [packagedPath, developmentPath] : [developmentPath, packagedPath] + return candidates.find((candidate) => existsSync(candidate)) || candidates[0] +} + +export function resolveWechatConnectorBinaryPath( + packaged = app.isPackaged, + platform = process.platform, + arch = process.arch +): string { + return resolveBundledBinary( + ['connectors', 'wechat'], + platform === 'win32' ? 'wechat-connector.exe' : 'wechat-connector', + packaged, + platform, + arch + ) +} + +class AgentHubService { + private hubServer: Server | null = null + private connectorChild: ChildProcess | null = null + private loginChild: ChildProcess | null = null + private stopping = false + private healthTimer: NodeJS.Timeout | null = null + private logs: AgentHubLogEntry[] = [] + private nextLogId = 1 + private readonly processedMessages = new Map() + private readonly inboundToken = + process.env['AGENT_HUB_INBOUND_TOKEN'] || randomBytes(32).toString('hex') + private status: AgentHubStatus = { + hub: 'offline', + connector: 'checking', + dataApi: 'checking', + updatedAt: Date.now() + } + + async start(settings: AppSettings): Promise { + void settings + this.stopping = false + const hubStarted = await this.startHub() + await this.initializeConnector() + return hubStarted + } + + getStatus(): AgentHubStatus { + return { ...this.status } + } + + getLogs(): AgentHubLogEntry[] { + return [...this.logs] + } + + clearLogs(): void { + this.logs = [] + try { + writeFileSync(this.logFilePath(), '', 'utf8') + } catch { + // The live log remains usable when the persistent file cannot be cleared. + } + this.addLog('system', 'info', '运行日志已清空') + } + + async testSend(input: { to?: string; text?: string; mediaUrl?: string }): Promise<{ + success: boolean + status: 'sent' | 'token_expired' | 'connector_offline' | 'invalid_request' | 'send_failed' + message: string + }> { + const to = String(input.to || this.status.wechatUserId || '').trim() + const text = String(input.text || '').trim() + const mediaUrl = String(input.mediaUrl || '').trim() + if (!to || (!text && !mediaUrl)) { + return { + success: false, + status: 'invalid_request', + message: '请填写接收者以及文字或图片路径' + } + } + try { + const response = await fetch(`http://${CONNECTOR_ADDR}/api/send`, { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ + account_id: this.status.accountId, + to, + text: text || undefined, + media_url: mediaUrl || undefined + }), + signal: AbortSignal.timeout(30_000) + }) + const body = await response.text() + if (response.ok) { + this.addLog('system', 'info', 'API 页面发送测试成功') + return { success: true, status: 'sent', message: '发送成功' } + } + const expired = /token|session|expired|unauthorized/i.test(body) + return { + success: false, + status: expired ? 'token_expired' : 'send_failed', + message: expired + ? '微信登录凭证已失效,请重新扫码登录' + : `发送失败:${body || response.status}` + } + } catch (error) { + return { + success: false, + status: 'connector_offline', + message: `微信连接器不可用:${error instanceof Error ? error.message : String(error)}` + } + } + } + + async startLogin(): Promise { + if (this.loginChild && this.loginChild.exitCode === null) { + return { success: true, status: this.getStatus() } + } + const executable = resolveWechatConnectorBinaryPath() + if (!existsSync(executable)) { + return this.fail(`微信连接器不存在:${executable}`) + } + + this.stopConnector() + this.patchStatus({ connector: 'starting', qrCodeDataUrl: undefined, error: undefined }) + const child = spawn(executable, ['login', '--json'], { + stdio: ['ignore', 'pipe', 'pipe'], + windowsHide: true + }) + this.loginChild = child + this.addLog('wechat-connector', 'info', '已启动扫码登录流程') + let stdoutBuffer = '' + let stderr = '' + + child.stdout?.on('data', (data: Buffer) => { + stdoutBuffer += data.toString() + const lines = stdoutBuffer.split(/\r?\n/) + stdoutBuffer = lines.pop() || '' + for (const line of lines) this.handleLoginEvent(line) + }) + child.stderr?.on('data', (data: Buffer) => { + stderr += data.toString() + this.addProcessOutput('wechat-connector', 'warn', data.toString()) + }) + child.once('error', (error) => { + this.addLog('wechat-connector', 'error', `登录进程错误:${error.message}`) + this.patchStatus({ connector: 'error', error: error.message }) + }) + child.once('exit', (code) => { + if (this.loginChild === child) this.loginChild = null + if ( + code !== 0 && + this.status.connector !== 'online' && + this.status.connector !== 'disconnected' && + !this.stopping + ) { + this.patchStatus({ connector: 'error', error: stderr.trim() || `登录进程退出:${code}` }) + } + }) + return { success: true, status: this.getStatus() } + } + + cancelLogin(): AgentHubActionResult { + if (this.loginChild && this.loginChild.exitCode === null) this.loginChild.kill() + this.loginChild = null + this.patchStatus({ connector: 'disconnected', qrCodeDataUrl: undefined, error: undefined }) + return { success: true, status: this.getStatus() } + } + + async reconnect(): Promise { + const accounts = await this.loadAccounts() + if (accounts.length === 0) return this.startLogin() + this.startConnector(accounts.at(-1)!) + return { success: true, status: this.getStatus() } + } + + disconnect(): AgentHubActionResult { + this.stopConnector() + this.patchStatus({ connector: 'disconnected', error: undefined }) + return { success: true, status: this.getStatus() } + } + + stop(): void { + this.stopping = true + this.clearHealthCheck() + if (this.loginChild && this.loginChild.exitCode === null) this.loginChild.kill() + this.loginChild = null + this.stopConnector() + const hubServer = this.hubServer + this.hubServer = null + hubServer?.close() + this.patchStatus({ hub: 'offline' }) + } + + private async startHub(): Promise { + if (this.hubServer) return true + this.patchStatus({ hub: 'starting' }) + const server = createServer((request, response) => { + void this.handleHubRequest(request, response).catch((error) => { + this.addLog('agent-hub', 'error', `请求处理失败:${this.errorMessage(error)}`) + this.sendHubJson(response, 500, { error: 'internal error' }) + }) + }) + this.hubServer = server + return new Promise((resolve) => { + const fail = (error: Error): void => { + if (this.hubServer === server) this.hubServer = null + this.patchStatus({ hub: 'error', error: error.message }) + this.addLog('agent-hub', 'error', `TypeScript 服务启动失败:${error.message}`) + resolve(false) + } + server.once('error', fail) + server.listen(HUB_PORT, HUB_HOST, () => { + server.off('error', fail) + server.on('error', (error) => { + this.patchStatus({ hub: 'error', error: error.message }) + this.addLog('agent-hub', 'error', error.message) + }) + this.patchStatus({ hub: 'online', error: undefined }) + this.addLog('system', 'info', `Agent Hub TypeScript 服务已启动(${HUB_ADDR})`) + this.scheduleHealthCheck() + resolve(true) + }) + }) + } + + private async handleHubRequest( + request: IncomingMessage, + response: ServerResponse + ): Promise { + const url = new URL(request.url || '/', `http://${HUB_ADDR}`) + if (request.method === 'GET' && url.pathname === '/health') { + return this.sendHubJson(response, 200, { + status: 'ok', + service: 'agent-hub', + runtime: 'typescript' + }) + } + if (request.method !== 'POST' || url.pathname !== '/v1/connectors/wechat/inbound') { + return this.sendHubJson(response, 404, { error: 'not found' }) + } + if (!this.authorized(request.headers.authorization)) { + return this.sendHubJson(response, 401, { error: 'unauthorized' }) + } + + let inbound: InboundMessage + try { + inbound = JSON.parse(await this.readHubBody(request)) as InboundMessage + } catch { + return this.sendHubJson(response, 400, { error: 'invalid request' }) + } + const from = String(inbound.from_user_id || '').trim() + if (!from) return this.sendHubJson(response, 400, { error: 'from_user_id is required' }) + + const messageId = String(inbound.message_id || '') + this.cleanProcessedMessages() + if (messageId && this.processedMessages.has(messageId)) { + return this.sendHubJson(response, 200, { status: 'duplicate' }) + } + const text = (inbound.items || []) + .filter((item) => item.type === 1 && item.text?.trim()) + .map((item) => item.text!.trim()) + .join(' ') + this.addLog('agent-hub', 'info', `收到微信消息 message_id=${messageId || 'unknown'}`) + + const reportIntent = this.matchGroupReportIntent(text) + if (reportIntent) { + if (messageId) this.processedMessages.set(messageId, Date.now()) + this.addLog( + 'agent-hub', + 'info', + `匹配群聊总结:${reportIntent.group}(${reportIntent.range})` + ) + await this.sendConnector(inbound, '收到!正在生成群聊总结,请等待…').catch((error) => { + this.addLog('agent-hub', 'warn', `等待提示发送失败:${this.errorMessage(error)}`) + }) + void this.generateAndSendReport(inbound, reportIntent) + return this.sendHubJson(response, 202, { status: 'generating' }) + } + + const limit = this.matchRecentChatIntent(text) + if (limit === null) { + this.addLog('agent-hub', 'info', '消息已忽略:没有匹配到支持的意图') + return this.sendHubJson(response, 202, { status: 'ignored', reason: 'no matching intent' }) + } + if (!isReady()) return this.sendHubJson(response, 502, { error: 'upstream query failed' }) + const items = listRecentChat(limit) + const lines = items.map((item, index) => { + const name = item.m_nsNickName.trim() || item.m_nsUsrName.trim() + return `${index + 1}. ${name}(${item.type === 'group' ? '群聊' : '联系人'})` + }) + const reply = lines.length + ? `最近 ${items.length} 个会话:\n${lines.join('\n')}` + : '暂时没有找到最近会话。' + try { + await this.sendConnector(inbound, reply) + } catch (error) { + this.addLog('agent-hub', 'error', `回复发送失败:${this.errorMessage(error)}`) + return this.sendHubJson(response, 502, { error: 'reply delivery failed' }) + } + if (messageId) this.processedMessages.set(messageId, Date.now()) + this.addLog('agent-hub', 'info', `最近会话回复已发送(${items.length} 条)`) + this.sendHubJson(response, 200, { status: 'ok' }) + } + + private async generateAndSendReport( + inbound: InboundMessage, + intent: GroupReportIntent + ): Promise { + try { + const result = await generateAgentGroupReport({ group: intent.group, range: intent.range }) + if (!result.success || !result.pngPath) throw new Error(result.error || '群聊总结生成失败') + await this.sendConnector( + inbound, + `已生成${result.groupName || intent.group}的群聊总结(${result.messageCount || 0} 条消息),正在发送图片。` + ) + await this.sendConnector(inbound, undefined, result.pngPath) + this.addLog('agent-hub', 'info', `群聊总结图片已发送:${result.groupName || intent.group}`) + } catch (error) { + const message = this.errorMessage(error) + this.addLog('agent-hub', 'error', `群聊总结生成失败:${message}`) + await this.sendConnector(inbound, `群聊总结生成失败:${message}`).catch(() => undefined) + } + } + + private async sendConnector( + inbound: InboundMessage, + text?: string, + mediaUrl?: string + ): Promise { + const response = await fetch(`http://${CONNECTOR_ADDR}/api/send`, { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ + account_id: inbound.account_id, + to: inbound.from_user_id, + text, + media_url: mediaUrl + }), + signal: AbortSignal.timeout(mediaUrl ? 60_000 : 30_000) + }) + if (!response.ok) throw new Error((await response.text()) || `HTTP ${response.status}`) + } + + private matchRecentChatIntent(text: string): number | null { + const normalized = text.replace(/\s+/g, '') + if (!normalized.includes('最近') || !/(消息|会话|聊天)/.test(normalized)) return null + const limit = Number(normalized.match(/\d{1,2}/)?.[0] || 5) + return Math.max(1, Math.min(20, limit)) + } + + private matchGroupReportIntent(text: string): GroupReportIntent | null { + const normalized = text.trim() + if (!normalized.includes('群') || !/(总结|日报|报告)/.test(normalized)) return null + const range = /(7天|七天|一周)/.test(normalized) + ? '7days' + : /(昨天|昨日)/.test(normalized) + ? 'yesterday' + : 'today' + const group = normalized + .replace( + /请|帮我|生成|做一份|做个|今天的|今日的|今天|今日|昨天的|昨日的|昨天|昨日|最近7天的|最近七天的|最近7天|最近七天|近7天的|近七天的|近7天|近七天|消息|聊天记录|聊天|群聊总结|群总结|群日报|群报告|总结|日报|报告|图片|长图/g, + '' + ) + .replace(/[,。!??::]/g, '') + .trim() + .replace(/成$/, '') + .replace(/群$/, '') + .trim() + return group ? { group, range } : null + } + + private authorized(header: string | undefined): boolean { + if (!header?.startsWith('Bearer ')) return false + const expected = Buffer.from(this.inboundToken) + const provided = Buffer.from(header.slice(7)) + return expected.length === provided.length && timingSafeEqual(expected, provided) + } + + private readHubBody(request: IncomingMessage): Promise { + return new Promise((resolve, reject) => { + const chunks: Buffer[] = [] + let size = 0 + request.on('data', (chunk: Buffer) => { + size += chunk.length + if (size > 1024 * 1024) { + reject(new Error('request too large')) + request.destroy() + return + } + chunks.push(chunk) + }) + request.on('end', () => resolve(Buffer.concat(chunks).toString('utf8'))) + request.on('error', reject) + }) + } + + private sendHubJson(response: ServerResponse, status: number, payload: unknown): void { + if (response.writableEnded) return + response.writeHead(status, { 'Content-Type': 'application/json; charset=utf-8' }) + response.end(JSON.stringify(payload)) + } + + private cleanProcessedMessages(): void { + const cutoff = Date.now() - 10 * 60_000 + for (const [id, timestamp] of this.processedMessages) { + if (timestamp < cutoff) this.processedMessages.delete(id) + } + } + + private errorMessage(error: unknown): string { + return error instanceof Error ? error.message : String(error) + } + + private async initializeConnector(): Promise { + this.patchStatus({ connector: 'checking' }) + try { + const accounts = await this.loadAccounts() + if (accounts.length === 0) { + this.patchStatus({ connector: 'disconnected' }) + return + } + this.startConnector(accounts.at(-1)!) + } catch (error) { + this.patchStatus({ + connector: 'error', + error: error instanceof Error ? error.message : String(error) + }) + } + } + + private async loadAccounts(): Promise<{ accountId: string; wechatUserId: string }[]> { + const executable = resolveWechatConnectorBinaryPath() + if (!existsSync(executable)) throw new Error(`微信连接器不存在:${executable}`) + const { stdout } = await execFileAsync(executable, ['accounts', '--json'], { + windowsHide: true, + timeout: 10_000 + }) + const parsed = JSON.parse(stdout) as { + accounts?: { account_id: string; wechat_user_id: string }[] + } + return (parsed.accounts || []).map((account) => ({ + accountId: account.account_id, + wechatUserId: account.wechat_user_id + })) + } + + private startConnector(account: { accountId: string; wechatUserId: string }): void { + if (this.connectorChild && this.connectorChild.exitCode === null) return + const executable = resolveWechatConnectorBinaryPath() + this.patchStatus({ + connector: 'starting', + accountId: account.accountId, + wechatUserId: account.wechatUserId, + qrCodeDataUrl: undefined, + error: undefined + }) + const child = spawn( + executable, + ['start', '--foreground', '--api-addr', CONNECTOR_ADDR, '--account-id', account.accountId], + { + env: { + ...process.env, + WECHAT_CONNECTOR_INBOUND_WEBHOOK_URL: `http://${HUB_ADDR}/v1/connectors/wechat/inbound`, + WECHAT_CONNECTOR_INBOUND_WEBHOOK_TOKEN: this.inboundToken, + WECHAT_CONNECTOR_INBOUND_WEBHOOK_ONLY: 'true' + }, + stdio: ['ignore', 'pipe', 'pipe'], + windowsHide: true + } + ) + this.connectorChild = child + this.addLog('system', 'info', `正在启动微信连接器(账号 ${account.accountId})`) + child.stdout?.on('data', (data: Buffer) => this.handleConnectorOutput('info', data.toString())) + child.stderr?.on('data', (data: Buffer) => this.handleConnectorOutput('warn', data.toString())) + child.once('spawn', () => { + this.addLog('system', 'info', `微信连接器已启动(PID ${child.pid})`) + this.patchStatus({ connector: 'online' }) + }) + child.once('error', (error) => { + this.addLog('wechat-connector', 'error', error.message) + this.patchStatus({ connector: 'error', error: error.message }) + }) + child.once('exit', (code) => { + if (this.connectorChild === child) this.connectorChild = null + this.addLog('system', code === 0 ? 'info' : 'error', `微信连接器已退出(code=${code})`) + if (!this.stopping && this.status.connector !== 'disconnected') { + this.patchStatus({ connector: 'error', error: `微信连接器退出:${code}` }) + } + }) + } + + private stopConnector(): void { + const child = this.connectorChild + this.connectorChild = null + if (child && child.exitCode === null) child.kill() + } + + private handleLoginEvent(line: string): void { + if (!line.trim()) return + try { + const event = JSON.parse(line) as { + status: string + qr_code_data_url?: string + account_id?: string + wechat_user_id?: string + } + switch (event.status) { + case 'qrcode': + case 'wait': + this.patchStatus({ + connector: 'waiting_scan', + qrCodeDataUrl: event.qr_code_data_url || this.status.qrCodeDataUrl + }) + break + case 'scaned': + this.patchStatus({ connector: 'scanned' }) + break + case 'confirmed': + this.patchStatus({ connector: 'starting' }) + break + case 'expired': + this.patchStatus({ connector: 'error', error: '二维码已过期,请重新获取' }) + break + case 'active': { + const account = { + accountId: event.account_id || '', + wechatUserId: event.wechat_user_id || '' + } + this.patchStatus({ ...account, connector: 'starting', qrCodeDataUrl: undefined }) + this.startConnector(account) + break + } + } + } catch (error) { + console.warn('[AgentHub] invalid login event:', line, error) + } + } + + private patchStatus(patch: Partial): void { + this.status = { ...this.status, ...patch, updatedAt: Date.now() } + for (const window of BrowserWindow.getAllWindows()) { + window.webContents.send('agent-hub:status', this.getStatus()) + } + } + + private addProcessOutput( + source: AgentHubLogSource, + level: AgentHubLogLevel, + output: string + ): void { + for (const line of output.split(/\r?\n/)) { + if (line.trim()) this.addLog(source, level, line) + } + } + + private handleConnectorOutput(level: AgentHubLogLevel, output: string): void { + this.addProcessOutput('wechat-connector', level, output) + if (/session expired/i.test(output)) { + this.addLog('system', 'error', '当前微信机器人登录已失效,需要重新扫码登录') + this.patchStatus({ connector: 'error', error: '当前登录已失效,请重新扫码登录' }) + this.stopConnector() + } + } + + private addLog(source: AgentHubLogSource, level: AgentHubLogLevel, rawMessage: string): void { + const message = this.redactLog(rawMessage).trim() + if (!message) return + const entry: AgentHubLogEntry = { + id: this.nextLogId++, + timestamp: Date.now(), + source, + level, + message + } + this.logs.push(entry) + if (this.logs.length > MAX_LOG_ENTRIES) this.logs.splice(0, this.logs.length - MAX_LOG_ENTRIES) + try { + const path = this.logFilePath() + mkdirSync(dirname(path), { recursive: true }) + appendFileSync( + path, + `${new Date(entry.timestamp).toISOString()} [${source}] [${level}] ${message}\n`, + 'utf8' + ) + } catch { + // Do not interrupt message handling because log persistence failed. + } + for (const window of BrowserWindow.getAllWindows()) { + window.webContents.send('agent-hub:log', entry) + } + } + + private redactLog(message: string): string { + return message + .replace(/Bearer\s+[A-Za-z0-9._~-]+/gi, 'Bearer [已隐藏]') + .replace(/data:image\/[^;]+;base64,[A-Za-z0-9+/=]+/gi, 'data:image/[二维码已隐藏]') + .replace(/(token[=:\s]+)[^\s,}]+/gi, '$1[已隐藏]') + } + + private logFilePath(): string { + return join(app.getPath('logs'), 'agent-hub.log') + } + + private fail(error: string): AgentHubActionResult { + this.patchStatus({ connector: 'error', error }) + return { success: false, status: this.getStatus(), error } + } + + private scheduleHealthCheck(): void { + this.clearHealthCheck() + this.healthTimer = setInterval(() => this.checkDataApi(), HEALTH_INTERVAL_MS) + this.checkDataApi() + } + + private checkDataApi(): void { + const ready = isReady() + this.patchStatus({ dataApi: 'online', databaseReady: ready }) + } + + private clearHealthCheck(): void { + if (this.healthTimer) clearInterval(this.healthTimer) + this.healthTimer = null + } +} + +export const agentHubService = new AgentHubService() diff --git a/src/main/services/local-api-test-service.ts b/src/main/services/local-api-test-service.ts index b4dad71..11f4c8f 100644 --- a/src/main/services/local-api-test-service.ts +++ b/src/main/services/local-api-test-service.ts @@ -8,6 +8,7 @@ import { } from '../../shared/local-api-test' const REQUEST_TIMEOUT_MS = 10_000 +const GROUP_REPORT_TIMEOUT_MS = 180_000 const MAX_BODY_SIZE = 512 * 1024 function isEndpointId(value: unknown): value is LocalApiEndpointId { @@ -122,7 +123,9 @@ export async function testLocalApiRequest(payload: unknown): Promise { + const timeoutMs = + endpointId === 'agent-group-report' ? GROUP_REPORT_TIMEOUT_MS : REQUEST_TIMEOUT_MS + request.setTimeout(timeoutMs, () => { request.destroy(new Error('请求超时')) finish({ ok: false, @@ -132,7 +135,7 @@ export async function testLocalApiRequest(payload: unknown): Promise { diff --git a/src/main/services/settings-store.ts b/src/main/services/settings-store.ts index b6a14d5..5029d20 100644 --- a/src/main/services/settings-store.ts +++ b/src/main/services/settings-store.ts @@ -12,6 +12,8 @@ export interface AppSettings { imageXorKey: string imageAesKey: string imageKeyFallbackDisabled: boolean + autoLogin: boolean + autoLoginPreferenceSet: boolean } function getDefaultDbRoot(): string { @@ -118,7 +120,13 @@ const DEFAULT_SETTINGS: AppSettings = { imageKeyRoot: defaultDbRoot, imageXorKey: '', imageAesKey: '', - imageKeyFallbackDisabled: false + imageKeyFallbackDisabled: false, + autoLogin: ['1', 'true', 'yes', 'on'].includes( + String(import.meta.env.VITE_AUTO_LOGIN || '') + .trim() + .toLowerCase() + ), + autoLoginPreferenceSet: false } const SETTINGS_FILE = path.join( @@ -138,6 +146,12 @@ export function loadSettings(): AppSettings { if (fs.existsSync(SETTINGS_FILE)) { const raw = fs.readJsonSync(SETTINGS_FILE) as Partial cache = { ...DEFAULT_SETTINGS, ...raw } + if (raw.autoLogin === undefined) { + const hasSavedDatabaseKey = fs.existsSync( + path.join(app.getPath('userData'), 'wechat-db-key.bin') + ) + if (hasSavedDatabaseKey) cache.autoLogin = true + } if (process.platform === 'win32' && !isUsableDbRoot(cache.dbRoot)) { cache.dbRoot = getDefaultDbRoot() } diff --git a/src/preload/index.d.ts b/src/preload/index.d.ts index a914428..3519c27 100644 --- a/src/preload/index.d.ts +++ b/src/preload/index.d.ts @@ -37,6 +37,7 @@ import type { ImageCandidateQuery, ImageInsight } from '../shared/image-insight' +import type { AgentHubActionResult, AgentHubLogEntry, AgentHubStatus } from '../shared/agent-hub' export type ParsedContent = | { type: 'text'; content: string } @@ -186,6 +187,8 @@ declare global { apiHost: string apiPort: number imageKeyRoot: string + autoLogin: boolean + autoLoginPreferenceSet: boolean imageXorKey: string imageAesKey: string } @@ -210,6 +213,8 @@ declare global { apiHost: string apiPort: number imageKeyRoot: string + autoLogin: boolean + autoLoginPreferenceSet: boolean imageXorKey: string imageAesKey: string } @@ -222,6 +227,8 @@ declare global { apiHost: string apiPort: number imageKeyRoot: string + autoLogin: boolean + autoLoginPreferenceSet: boolean imageXorKey: string imageAesKey: string }> @@ -232,6 +239,8 @@ declare global { apiHost: string apiPort: number imageKeyRoot: string + autoLogin: boolean + autoLoginPreferenceSet: boolean imageXorKey: string imageAesKey: string } @@ -298,6 +307,16 @@ declare global { sessionId: string, limit?: number ) => Promise<{ success: boolean; insights: ImageInsight[] }> + getAgentHubStatus: () => Promise + getAgentHubLogs: () => Promise + clearAgentHubLogs: () => Promise + startAgentHubLogin: () => Promise + cancelAgentHubLogin: () => Promise + reconnectAgentHub: () => Promise + disconnectAgentHub: () => Promise + selectAgentHubTestImage: () => Promise<{ canceled: boolean; path?: string }> + onAgentHubStatus: (callback: (status: AgentHubStatus) => void) => () => void + onAgentHubLog: (callback: (entry: AgentHubLogEntry) => void) => () => void } } } diff --git a/src/preload/index.ts b/src/preload/index.ts index b7e9f96..9a7a823 100644 --- a/src/preload/index.ts +++ b/src/preload/index.ts @@ -15,6 +15,7 @@ import type { ImageCandidateQuery, ImageInsight } from '../shared/image-insight' +import type { AgentHubLogEntry, AgentHubStatus } from '../shared/agent-hub' // 渲染器的自定义 API const api = { @@ -132,7 +133,27 @@ const api = { sessionId: string, limit?: number ): Promise<{ success: boolean; insights: ImageInsight[] }> => - ipcRenderer.invoke('image:listInsights', sessionId, limit) + ipcRenderer.invoke('image:listInsights', sessionId, limit), + getAgentHubStatus: () => ipcRenderer.invoke('agent-hub:getStatus'), + getAgentHubLogs: () => ipcRenderer.invoke('agent-hub:getLogs'), + clearAgentHubLogs: () => ipcRenderer.invoke('agent-hub:clearLogs'), + startAgentHubLogin: () => ipcRenderer.invoke('agent-hub:startLogin'), + cancelAgentHubLogin: () => ipcRenderer.invoke('agent-hub:cancelLogin'), + reconnectAgentHub: () => ipcRenderer.invoke('agent-hub:reconnect'), + disconnectAgentHub: () => ipcRenderer.invoke('agent-hub:disconnect'), + selectAgentHubTestImage: () => ipcRenderer.invoke('agent-hub:selectTestImage'), + onAgentHubStatus: (callback: (status: AgentHubStatus) => void) => { + const listener = (_event: Electron.IpcRendererEvent, status: AgentHubStatus): void => + callback(status) + ipcRenderer.on('agent-hub:status', listener) + return () => ipcRenderer.removeListener('agent-hub:status', listener) + }, + onAgentHubLog: (callback: (entry: AgentHubLogEntry) => void) => { + const listener = (_event: Electron.IpcRendererEvent, entry: AgentHubLogEntry): void => + callback(entry) + ipcRenderer.on('agent-hub:log', listener) + return () => ipcRenderer.removeListener('agent-hub:log', listener) + } } if (process.contextIsolated) { diff --git a/src/renderer/src/App.tsx b/src/renderer/src/App.tsx index 77a5640..2b099af 100644 --- a/src/renderer/src/App.tsx +++ b/src/renderer/src/App.tsx @@ -4,6 +4,7 @@ import ChatWindow from './components/ChatWindow' import { AppShell } from './components/layout/AppShell' import { ApiWorkspace } from './features/api-center/ApiWorkspace' import { SettingsWorkspace } from './features/settings/SettingsWorkspace' +import { AgentHubWorkspace } from './features/agent-hub/AgentHubWorkspace' import type { SettingsCategoryId } from './features/settings/model/types' import type { AIRuntimeModelConfig } from '../../shared/ai-provider' import { AppPage } from './components/layout/navigation' @@ -57,12 +58,6 @@ interface SelfInfo { const MAC_KEY_FAQ_URL = 'https://github.com/hicccc77/WeFlow/blob/main/docs/MAC-KEY-FAQ.md' const MESSAGE_MONITOR_DEBOUNCE_MS = 8000 const VIEW_MESSAGE_LIMIT = 600 -const AUTO_LOGIN_ENABLED = ['1', 'true', 'yes', 'on'].includes( - String(import.meta.env.VITE_AUTO_LOGIN || '') - .trim() - .toLowerCase() -) - const getMessageIdentity = (message: Message): string => { if (message.localId) return `local:${message.localId}` if (message.id) return `id:${message.id}` @@ -369,13 +364,12 @@ function App(): React.ReactElement { React.useEffect(() => { let active = true const attemptAutoConnect = async (): Promise => { + const settingsResult = await window.api.getSettings() // 预填已保存的微信聊天文件路径 - try { - const settings = await window.api.getSettings() - if (active && settings?.settings?.dbRoot) setDbRootInput(settings.settings.dbRoot) - } catch { - // 忽略读取设置失败,继续走密钥流程 + if (active && settingsResult.settings.dbRoot) { + setDbRootInput(settingsResult.settings.dbRoot) } + const autoLoginEnabled = settingsResult.settings.autoLogin // 优先级 1: 构建期环境变量 VITE_DB_KEY(本地开发用) const envKey = String(import.meta.env.VITE_DB_KEY || '').trim() // 优先级 2: 上一次保存到 safeStorage 的密钥 @@ -393,7 +387,7 @@ function App(): React.ReactElement { setDbKey(key) setAutoConnectSource(envKey ? 'env' : 'saved') setDbKeyStatus( - AUTO_LOGIN_ENABLED + autoLoginEnabled ? envKey ? '检测到环境变量中的密钥,正在自动连接...' : '已加载安全保存的密钥,正在自动连接...' @@ -402,14 +396,17 @@ function App(): React.ReactElement { : '已加载安全保存的密钥,请手动点击 Connect' ) setDbKeyStatusKind('normal') - setBootState(AUTO_LOGIN_ENABLED ? 'connecting' : 'login') + setBootState(autoLoginEnabled ? 'connecting' : 'login') } - if (!AUTO_LOGIN_ENABLED) return + if (!autoLoginEnabled) return try { const result = await window.api.initDb(key) if (!active) return const success = typeof result === 'boolean' ? result : result.success if (success) { + if (!settingsResult.settings.autoLoginPreferenceSet) { + void window.api.setSettings({ autoLogin: true }) + } setIsNativeMonitorActive(typeof result !== 'boolean' && result.monitoring === true) setIsAuthenticated(true) setIsDatabaseConnected(true) @@ -489,6 +486,11 @@ function App(): React.ReactElement { const hasBootstrapCache = await loadBootstrapCache() // 持久化手动输入的密钥,供下次启动继续使用 void window.api.saveDbKey(keyToUse).catch(() => undefined) + void window.api.getSettings().then((current) => { + if (!current.settings.autoLoginPreferenceSet) { + void window.api.setSettings({ autoLogin: true }) + } + }) setStartupProgress({ title: '正在加载账号信息...', subtitle: '即将进入 WechatExplorer', @@ -1036,9 +1038,9 @@ function App(): React.ReactElement { } const renderPlaceholderPage = ( - page: Exclude + page: Exclude ): React.ReactElement => { - const labels: Record, string> = { + const labels: Record, string> = { search: '检索', export: '导出', api: 'API', @@ -1168,6 +1170,8 @@ function App(): React.ReactElement { return renderArchiveWorkspace() case 'report': return renderReportWorkspace() + case 'agent-hub': + return case 'api': return ( p { + margin-top: 8px; +} +.agent-hub-example { + margin: 24px 0 20px; + padding: 16px; + border-radius: 12px; + background: var(--wxex-brand-soft); +} +.agent-hub-example span { + display: block; + color: var(--wxex-text-muted); + font: 600 11px/16px var(--wxex-font); +} +.agent-hub-example strong { + display: block; + margin-top: 4px; + color: var(--wxex-brand); + font: 700 14px/21px var(--wxex-font); +} +.agent-hub-capability-card ul { + margin: 0; + padding: 0; + list-style: none; +} +.agent-hub-capability-card li { + display: flex; + align-items: center; + gap: 9px; + padding: 9px 0; + border-bottom: 1px solid var(--wxex-border); + color: var(--wxex-text-secondary); + font: 13px/19px var(--wxex-font); +} +.agent-hub-capability-card li:last-child { + border-bottom: 0; +} +.agent-hub-capability-card li i { + width: 7px; + height: 7px; + border-radius: 50%; + background: var(--wxex-brand); +} +.agent-hub-example strong + strong { + margin-top: 9px; + padding-top: 9px; + border-top: 1px solid rgba(36, 122, 99, 0.15); +} +.agent-hub-capability-card li i.offline { + background: var(--wxex-danger); +} +.agent-hub-log-card { + max-width: 1080px; + margin: 20px auto 0; + padding: 22px; +} +.agent-hub-log-heading { + display: flex; + align-items: center; + justify-content: space-between; + gap: 16px; + margin-bottom: 14px; +} +.agent-hub-log-heading h2 { + margin-top: 3px; +} +.agent-hub-log-actions { + display: flex; + align-items: center; + gap: 8px; +} +.agent-hub-log-actions select, +.agent-hub-log-actions button { + height: 32px; + border: 1px solid var(--wxex-border-strong); + border-radius: 8px; + background: #fff; + color: var(--wxex-text-secondary); + font: 600 12px/18px var(--wxex-font); +} +.agent-hub-log-actions select { + padding: 0 28px 0 10px; +} +.agent-hub-log-actions button { + padding: 0 11px; + cursor: pointer; +} +.agent-hub-log-actions button:disabled { + opacity: 0.45; + cursor: default; +} +.agent-hub-log-body { + height: 250px; + overflow: auto; + padding: 12px 14px; + border: 1px solid #26312a; + border-radius: 11px; + background: #18201b; + color: #dfe9e2; +} +.agent-hub-log-line { + display: grid; + grid-template-columns: 78px 94px minmax(0, 1fr); + gap: 9px; + align-items: baseline; + min-height: 24px; + font: + 12px/19px ui-monospace, + SFMono-Regular, + Menlo, + monospace; +} +.agent-hub-log-line time { + color: #84938a; +} +.agent-hub-log-line .source { + color: #76d49a; +} +.agent-hub-log-line .source.wechat-connector { + color: #7dbcf1; +} +.agent-hub-log-line .source.system { + color: #d8bd6b; +} +.agent-hub-log-line code { + min-width: 0; + color: inherit; + white-space: pre-wrap; + overflow-wrap: anywhere; +} +.agent-hub-log-line.warn code { + color: #f1cf77; +} +.agent-hub-log-line.error code { + color: #ff9690; +} +.agent-hub-log-empty { + height: 100%; + display: grid; + place-items: center; + color: #84938a; + font: 12px/20px var(--wxex-font); +} +.agent-hub-log-note { + margin-top: 9px !important; + color: var(--wxex-text-muted) !important; + font-size: 11px !important; +} +@media (max-width: 900px) { + .agent-hub-workspace { + padding: 24px; + } + .agent-hub-grid { + grid-template-columns: 1fr; + } + .agent-hub-qr-panel { + flex-direction: column; + text-align: center; + } + .agent-hub-capability-card { + width: 100%; + } +} + .ai-vision-test { display: grid; gap: 16px; @@ -3766,153 +4204,2325 @@ body { } /* API-01 本地 API 中心 */ -.api-center-layout { display: grid; grid-template-columns: minmax(0, 1fr) 320px; width: 100%; height: 100%; min-width: 0; min-height: 0; overflow: hidden; background: var(--wxex-bg-main); } +.api-center-layout { + display: grid; + grid-template-columns: minmax(0, 1fr) 320px; + width: 100%; + height: 100%; + min-width: 0; + min-height: 0; + overflow: hidden; + background: var(--wxex-bg-main); +} /* SETTINGS-01: formal settings workspace. The legacy SettingsPanel remains only as a fallback source. */ -.settings-workspace { display:flex; width:100%; height:100%; min-width:0; min-height:0; overflow:hidden; background:#fafbfa; } -.settings-sidebar { width:294px; flex:0 0 294px; min-width:0; min-height:0; display:flex; flex-direction:column; background:#eef2f0; border-right:1px solid #dde3e0; } -.settings-sidebar header { padding:22px 20px 16px; border-bottom:1px solid #dde3e0; }.settings-sidebar h1 { margin:0; color:#202724; font-size:20px; }.settings-sidebar header p { margin:5px 0 16px; color:#66706b; font-size:12px; }.settings-search { height:34px; display:flex; align-items:center; gap:8px; padding:0 10px; border:1px solid #dde3e0; border-radius:8px; background:#fff; color:#66706b; }.settings-search input { min-width:0; width:100%; border:0; outline:0; color:#202724; background:transparent; font:inherit; font-size:12px; } -.settings-sidebar-list { flex:1; min-height:0; overflow:auto; padding:12px 8px; }.settings-sidebar-list section { margin:0 0 18px; }.settings-sidebar-list h2 { margin:0 12px 6px; color:#929a96; font-size:12px; font-weight:500; }.settings-sidebar-list button { position:relative; display:block; width:100%; border:0; padding:9px 12px; background:transparent; color:#39433e; text-align:left; font:600 13px inherit; cursor:pointer; border-radius:8px; }.settings-sidebar-list button.active { background:#fff; color:#202724; }.settings-sidebar-list button.active::before { content:''; position:absolute; left:0; top:7px; bottom:7px; width:3px; border-radius:0 3px 3px 0; background:#247a63; }.settings-sidebar-account { flex:0 0 auto; padding:10px; border-top:1px solid #dde3e0; } -.settings-page { position:relative; display:flex; flex:1; min-width:0; min-height:0; flex-direction:column; } -.settings-page-header { flex:0 0 auto; display:flex; justify-content:space-between; align-items:flex-start; gap:18px; padding:24px 34px; border-bottom:1px solid #eef1ef; background:#fafbfa; } -.settings-page-header h1 { margin:0; color:#202724; font-size:21px; } -.settings-page-header p { margin:6px 0 0; color:#66706b; font-size:13px; } -.settings-status-badge { margin-top:5px; flex:0 0 auto; border-radius:16px; padding:7px 12px; background:#edf5f1; color:#2e8b68; font-size:12px; } -.settings-status-badge::before { content:'●'; margin-right:6px; font-size:9px; } -.settings-status-badge.checking { background:#eef4f8; color:#4f7188; } -.settings-status-badge.warning { background:#fff6e8; color:#a56a24; } -.settings-status-badge.error { background:#f9eeee; color:#c85a5a; } -.settings-status-badge.unavailable { background:#f1f3f2; color:#66706b; } -.settings-page-scroll { flex:1; min-height:0; overflow-y:auto; overflow-x:hidden; } -.settings-page-content { box-sizing:border-box; width:min(100%, 860px); padding:34px 34px 64px; } -.settings-privacy-notice { display:flex; gap:14px; align-items:flex-start; padding:17px; border:1px solid #bfded3; border-radius:10px; background:#f0f7f4; color:#36564d; } -.settings-privacy-notice svg { flex:0 0 24px; width:24px; stroke:#247a63; fill:none; stroke-width:1.8; } -.settings-privacy-notice strong { color:#294b41; font-size:14px; } -.settings-privacy-notice p { margin:5px 0 0; color:#66706b; font-size:13px; line-height:1.6; } -.settings-section-heading { margin:30px 0 14px; color:#3d4742; font-size:16px; } -.settings-card { border:1px solid #dde3e0; border-radius:10px; background:#fff; padding:24px 26px; } -.settings-account-overview { display:grid; grid-template-columns:minmax(190px,1fr) minmax(220px,1fr) auto; gap:24px; align-items:center; } -.settings-account-primary { display:flex; min-width:0; align-items:center; gap:15px; } -.settings-avatar { width:66px; height:66px; flex:0 0 auto; overflow:hidden; display:grid; place-items:center; border-radius:9px; background:#dcede6; color:#247a63; font-size:23px; } -.settings-avatar img { width:100%; height:100%; object-fit:cover; } -.settings-account-identity { display:grid; min-width:0; gap:3px; } -.settings-account-identity span,.settings-account-root>span,.settings-account-facts dt { color:#929a96; font-size:12px; } -.settings-account-identity strong,.settings-account-identity small { overflow:hidden; text-overflow:ellipsis; white-space:nowrap; } -.settings-account-identity strong { color:#35403b; font-size:14px; } -.settings-account-identity small { color:#66706b; font:12px/18px ui-monospace,SFMono-Regular,Consolas,monospace; } -.settings-account-facts { display:grid; min-width:0; gap:12px; margin:0; } -.settings-account-facts div { min-width:0; } -.settings-account-facts dd { overflow:hidden; margin:3px 0 0; color:#35403b; font-size:13px; text-overflow:ellipsis; white-space:nowrap; } -.settings-connection-text.success { color:#2e8b68; } -.settings-connection-text.warning { color:#a56a24; } -.settings-connection-text.error { color:#c85a5a; } -.settings-account-actions { display:grid; gap:8px; } -.settings-account-actions button { min-height:34px; white-space:nowrap; } -.settings-account-root { grid-column:1/-1; display:grid; min-width:0; gap:7px; padding-top:19px; border-top:1px solid #edf0ee; } -.settings-account-root>div { display:flex; min-width:0; align-items:center; gap:8px; } -.settings-account-root code { min-width:0; flex:1; overflow:hidden; padding:10px 12px; border:1px solid #dde3e0; border-radius:8px; background:#f2f5f3; color:#46514c; text-overflow:ellipsis; white-space:nowrap; font:12px/18px ui-monospace,SFMono-Regular,Consolas,monospace; } -.settings-icon-button { display:grid; width:36px; height:36px; flex:0 0 auto; place-items:center; border:1px solid #dde3e0; border-radius:8px; background:#fff; color:#66706b; cursor:pointer; } -.settings-icon-button:hover:not(:disabled) { border-color:#247a63; color:#247a63; } -.settings-icon-button:disabled { cursor:not-allowed; opacity:.45; } -.settings-icon-button svg { width:17px; fill:none; stroke:currentColor; stroke-linecap:round; stroke-linejoin:round; stroke-width:1.8; } -.settings-health-card { padding:12px; } -.settings-diagnostic-summary { margin:0 0 10px; padding:9px 11px; border-left:3px solid #c68635; border-radius:6px; background:#fff8ec; color:#79501f; font-size:12px; line-height:1.55; } -.settings-diagnostics { display:grid; gap:8px; } -.settings-diagnostic { display:flex; align-items:center; gap:12px; min-width:0; padding:13px; border:1px solid #edf0ee; border-radius:8px; } -.settings-diagnostic-icon { display:grid; width:18px; height:18px; flex:0 0 auto; place-items:center; border:1px solid #929a96; border-radius:50%; color:#929a96; font-size:11px; font-weight:700; } -.settings-diagnostic-icon.success { border-color:#2e8b68; color:#2e8b68; } -.settings-diagnostic-icon.warning { border-color:#c68635; color:#c68635; } -.settings-diagnostic-icon.error { border-color:#c85a5a; color:#c85a5a; } -.settings-diagnostic-icon.checking { border-color:#cbd7d2; border-top-color:#247a63; animation:settings-spin .8s linear infinite; } -.settings-diagnostic span { color:#3d4742; font-size:13px; } -.settings-diagnostic small { margin-left:auto; overflow:hidden; color:#66706b; text-overflow:ellipsis; white-space:nowrap; } -.settings-empty-state { display:grid; flex:1; place-content:center; color:#66706b; text-align:center; } -.settings-empty-state h2 { color:#202724; } +.settings-workspace { + display: flex; + width: 100%; + height: 100%; + min-width: 0; + min-height: 0; + overflow: hidden; + background: #fafbfa; +} +.settings-sidebar { + width: 294px; + flex: 0 0 294px; + min-width: 0; + min-height: 0; + display: flex; + flex-direction: column; + background: #eef2f0; + border-right: 1px solid #dde3e0; +} +.settings-sidebar header { + padding: 22px 20px 16px; + border-bottom: 1px solid #dde3e0; +} +.settings-sidebar h1 { + margin: 0; + color: #202724; + font-size: 20px; +} +.settings-sidebar header p { + margin: 5px 0 16px; + color: #66706b; + font-size: 12px; +} +.settings-search { + height: 34px; + display: flex; + align-items: center; + gap: 8px; + padding: 0 10px; + border: 1px solid #dde3e0; + border-radius: 8px; + background: #fff; + color: #66706b; +} +.settings-search input { + min-width: 0; + width: 100%; + border: 0; + outline: 0; + color: #202724; + background: transparent; + font: inherit; + font-size: 12px; +} +.settings-sidebar-list { + flex: 1; + min-height: 0; + overflow: auto; + padding: 12px 8px; +} +.settings-sidebar-list section { + margin: 0 0 18px; +} +.settings-sidebar-list h2 { + margin: 0 12px 6px; + color: #929a96; + font-size: 12px; + font-weight: 500; +} +.settings-sidebar-list button { + position: relative; + display: block; + width: 100%; + border: 0; + padding: 9px 12px; + background: transparent; + color: #39433e; + text-align: left; + font: 600 13px inherit; + cursor: pointer; + border-radius: 8px; +} +.settings-sidebar-list button.active { + background: #fff; + color: #202724; +} +.settings-sidebar-list button.active::before { + content: ''; + position: absolute; + left: 0; + top: 7px; + bottom: 7px; + width: 3px; + border-radius: 0 3px 3px 0; + background: #247a63; +} +.settings-sidebar-account { + flex: 0 0 auto; + padding: 10px; + border-top: 1px solid #dde3e0; +} +.settings-page { + position: relative; + display: flex; + flex: 1; + min-width: 0; + min-height: 0; + flex-direction: column; +} +.settings-page-header { + flex: 0 0 auto; + display: flex; + justify-content: space-between; + align-items: flex-start; + gap: 18px; + padding: 24px 34px; + border-bottom: 1px solid #eef1ef; + background: #fafbfa; +} +.settings-page-header h1 { + margin: 0; + color: #202724; + font-size: 21px; +} +.settings-page-header p { + margin: 6px 0 0; + color: #66706b; + font-size: 13px; +} +.settings-status-badge { + margin-top: 5px; + flex: 0 0 auto; + border-radius: 16px; + padding: 7px 12px; + background: #edf5f1; + color: #2e8b68; + font-size: 12px; +} +.settings-status-badge::before { + content: '●'; + margin-right: 6px; + font-size: 9px; +} +.settings-status-badge.checking { + background: #eef4f8; + color: #4f7188; +} +.settings-status-badge.warning { + background: #fff6e8; + color: #a56a24; +} +.settings-status-badge.error { + background: #f9eeee; + color: #c85a5a; +} +.settings-status-badge.unavailable { + background: #f1f3f2; + color: #66706b; +} +.settings-page-scroll { + flex: 1; + min-height: 0; + overflow-y: auto; + overflow-x: hidden; +} +.settings-page-content { + box-sizing: border-box; + width: min(100%, 860px); + padding: 34px 34px 64px; +} +.settings-privacy-notice { + display: flex; + gap: 14px; + align-items: flex-start; + padding: 17px; + border: 1px solid #bfded3; + border-radius: 10px; + background: #f0f7f4; + color: #36564d; +} +.settings-privacy-notice svg { + flex: 0 0 24px; + width: 24px; + stroke: #247a63; + fill: none; + stroke-width: 1.8; +} +.settings-privacy-notice strong { + color: #294b41; + font-size: 14px; +} +.settings-privacy-notice p { + margin: 5px 0 0; + color: #66706b; + font-size: 13px; + line-height: 1.6; +} +.settings-section-heading { + margin: 30px 0 14px; + color: #3d4742; + font-size: 16px; +} +.settings-card { + border: 1px solid #dde3e0; + border-radius: 10px; + background: #fff; + padding: 24px 26px; +} +.settings-account-overview { + display: grid; + grid-template-columns: minmax(190px, 1fr) minmax(220px, 1fr) auto; + gap: 24px; + align-items: center; +} +.settings-account-primary { + display: flex; + min-width: 0; + align-items: center; + gap: 15px; +} +.settings-avatar { + width: 66px; + height: 66px; + flex: 0 0 auto; + overflow: hidden; + display: grid; + place-items: center; + border-radius: 9px; + background: #dcede6; + color: #247a63; + font-size: 23px; +} +.settings-avatar img { + width: 100%; + height: 100%; + object-fit: cover; +} +.settings-account-identity { + display: grid; + min-width: 0; + gap: 3px; +} +.settings-account-identity span, +.settings-account-root > span, +.settings-account-facts dt { + color: #929a96; + font-size: 12px; +} +.settings-account-identity strong, +.settings-account-identity small { + overflow: hidden; + text-overflow: ellipsis; + white-space: nowrap; +} +.settings-account-identity strong { + color: #35403b; + font-size: 14px; +} +.settings-account-identity small { + color: #66706b; + font: + 12px/18px ui-monospace, + SFMono-Regular, + Consolas, + monospace; +} +.settings-account-facts { + display: grid; + min-width: 0; + gap: 12px; + margin: 0; +} +.settings-account-facts div { + min-width: 0; +} +.settings-account-facts dd { + overflow: hidden; + margin: 3px 0 0; + color: #35403b; + font-size: 13px; + text-overflow: ellipsis; + white-space: nowrap; +} +.settings-connection-text.success { + color: #2e8b68; +} +.settings-connection-text.warning { + color: #a56a24; +} +.settings-connection-text.error { + color: #c85a5a; +} +.settings-account-actions { + display: grid; + gap: 8px; +} +.settings-account-actions button { + min-height: 34px; + white-space: nowrap; +} +.settings-account-root { + grid-column: 1/-1; + display: grid; + min-width: 0; + gap: 7px; + padding-top: 19px; + border-top: 1px solid #edf0ee; +} +.settings-account-root > div { + display: flex; + min-width: 0; + align-items: center; + gap: 8px; +} +.settings-account-root code { + min-width: 0; + flex: 1; + overflow: hidden; + padding: 10px 12px; + border: 1px solid #dde3e0; + border-radius: 8px; + background: #f2f5f3; + color: #46514c; + text-overflow: ellipsis; + white-space: nowrap; + font: + 12px/18px ui-monospace, + SFMono-Regular, + Consolas, + monospace; +} +.settings-icon-button { + display: grid; + width: 36px; + height: 36px; + flex: 0 0 auto; + place-items: center; + border: 1px solid #dde3e0; + border-radius: 8px; + background: #fff; + color: #66706b; + cursor: pointer; +} +.settings-icon-button:hover:not(:disabled) { + border-color: #247a63; + color: #247a63; +} +.settings-icon-button:disabled { + cursor: not-allowed; + opacity: 0.45; +} +.settings-icon-button svg { + width: 17px; + fill: none; + stroke: currentColor; + stroke-linecap: round; + stroke-linejoin: round; + stroke-width: 1.8; +} +.settings-health-card { + padding: 12px; +} +.settings-diagnostic-summary { + margin: 0 0 10px; + padding: 9px 11px; + border-left: 3px solid #c68635; + border-radius: 6px; + background: #fff8ec; + color: #79501f; + font-size: 12px; + line-height: 1.55; +} +.settings-diagnostics { + display: grid; + gap: 8px; +} +.settings-diagnostic { + display: flex; + align-items: center; + gap: 12px; + min-width: 0; + padding: 13px; + border: 1px solid #edf0ee; + border-radius: 8px; +} +.settings-diagnostic-icon { + display: grid; + width: 18px; + height: 18px; + flex: 0 0 auto; + place-items: center; + border: 1px solid #929a96; + border-radius: 50%; + color: #929a96; + font-size: 11px; + font-weight: 700; +} +.settings-diagnostic-icon.success { + border-color: #2e8b68; + color: #2e8b68; +} +.settings-diagnostic-icon.warning { + border-color: #c68635; + color: #c68635; +} +.settings-diagnostic-icon.error { + border-color: #c85a5a; + color: #c85a5a; +} +.settings-diagnostic-icon.checking { + border-color: #cbd7d2; + border-top-color: #247a63; + animation: settings-spin 0.8s linear infinite; +} +.settings-diagnostic span { + color: #3d4742; + font-size: 13px; +} +.settings-diagnostic small { + margin-left: auto; + overflow: hidden; + color: #66706b; + text-overflow: ellipsis; + white-space: nowrap; +} +.settings-empty-state { + display: grid; + flex: 1; + place-content: center; + color: #66706b; + text-align: center; +} +.settings-empty-state h2 { + color: #202724; +} /* SETTINGS-02: database key */ -.database-key-content { padding-bottom:48px; } -.database-key-badge.unconfigured { background:#f1f3f2; color:#66706b; } -.database-key-badge.validating { background:#eef4f8; color:#4f7188; } -.database-key-badge.invalid { background:#f9eeee; color:#c85a5a; } -.database-key-status-card dl { display:grid; grid-template-columns:repeat(2,minmax(0,1fr)); gap:18px 36px; margin:0 0 20px; } -.database-key-status-card dl div { min-width:0; } -.database-key-status-card dt,.database-key-diagnostics dt { color:#929a96; font-size:12px; } -.database-key-status-card dd,.database-key-diagnostics dd { overflow:hidden; margin:5px 0 0; color:#35403b; font-size:13px; text-overflow:ellipsis; white-space:nowrap; } -.database-key-mono { font-family:ui-monospace,SFMono-Regular,Consolas,monospace; } -.database-key-success { color:#2e8b68!important; }.database-key-muted { color:#66706b!important; } -.database-key-primary,.database-key-secondary { min-height:36px; padding:0 15px; border:1px solid #247a63; border-radius:8px; cursor:pointer; font:600 13px/1 inherit; } -.database-key-primary { background:#247a63; color:#fff; }.database-key-secondary { background:#fff; color:#247a63; } -.database-key-primary:hover:not(:disabled) { background:#1d6754; }.database-key-secondary:hover:not(:disabled) { background:#f0f7f4; } -.database-key-primary:disabled,.database-key-secondary:disabled { cursor:not-allowed; opacity:.45; } -.database-key-editor label { display:block; margin-bottom:9px; color:#46514c; font-size:12px; font-weight:700; letter-spacing:.04em; text-transform:uppercase; } -.database-key-input-row { display:flex; min-width:0; height:42px; border:1px solid #d5ddda; border-radius:8px; background:#f4f7f5; } -.database-key-input-row:focus-within { border-color:#247a63; box-shadow:0 0 0 2px rgba(36,122,99,.09); } -.database-key-input-row input { min-width:0; flex:1; border:0; outline:0; padding:0 14px; background:transparent; color:#202724; font:13px/1.4 ui-monospace,SFMono-Regular,Consolas,monospace; } -.database-key-input-row button { display:grid; width:38px; flex:0 0 38px; place-items:center; border:0; background:transparent; color:#66706b; cursor:pointer; } -.database-key-input-row button:hover:not(:disabled) { color:#247a63; }.database-key-input-row button:disabled { cursor:not-allowed; opacity:.4; } -.database-key-input-row svg { width:17px; fill:none; stroke:currentColor; stroke-linecap:round; stroke-linejoin:round; stroke-width:1.8; } -.database-key-editor>p { margin:9px 0 0; color:#66706b; font-size:12px; line-height:1.6; } -.database-key-actions { display:flex; gap:9px; margin-top:18px; } -.database-key-feedback { display:flex; flex-wrap:wrap; align-items:center; gap:8px 18px; margin-top:12px; padding:12px 15px; border-radius:8px; font-size:12px; line-height:1.5; } -.database-key-feedback.checking { background:#f1f4f3; color:#66706b; }.database-key-feedback.checking i { width:14px; height:14px; border:2px solid #c8d5d0; border-top-color:#247a63; border-radius:50%; animation:settings-spin .8s linear infinite; } -.database-key-feedback.success { border:1px solid #bfded3; background:#eaf5f1; color:#2e765d; }.database-key-feedback.success strong { width:100%; } -.database-key-feedback.error { border:1px solid #efcaca; background:#fff2f2; color:#a84444; } -.database-key-auto-heading { display:flex; align-items:flex-start; justify-content:space-between; gap:24px; }.database-key-auto strong { color:#35403b; font-size:14px; }.database-key-auto p { margin:6px 0 0; color:#66706b; font-size:12px; line-height:1.6; } -.database-key-prerequisites { display:flex; flex-wrap:wrap; gap:8px 20px; margin:18px 0 0; padding:14px 0 0; border-top:1px solid #edf0ee; list-style:none; color:#8b5c25; font-size:12px; }.database-key-prerequisites li::before { content:'○'; margin-right:6px; }.database-key-prerequisites li.ok { color:#2e8b68; }.database-key-prerequisites li.ok::before { content:'✓'; } -.database-key-phases { display:grid; grid-template-columns:repeat(5,minmax(0,1fr)); gap:8px; margin:18px 0 0; padding:0; counter-reset:key-phase; list-style:none; }.database-key-phases li { position:relative; padding-top:24px; color:#929a96; text-align:center; font-size:11px; }.database-key-phases li::before { position:absolute; top:0; left:50%; display:grid; width:18px; height:18px; place-items:center; border:1px solid #d5ddda; border-radius:50%; background:#fff; content:counter(key-phase); counter-increment:key-phase; transform:translateX(-50%); }.database-key-phases li.active { color:#247a63; }.database-key-phases li.active::before { border-color:#247a63; background:#247a63; color:#fff; } -.database-key-auto-error { display:grid; gap:4px; margin-top:16px; padding:12px 14px; border-left:3px solid #c68635; background:#fff8ec; color:#79501f; font-size:12px; }.database-key-auto-error span { overflow-wrap:anywhere; }.database-key-auto-error p { color:#79501f; }.database-key-auto-error button { justify-self:start; border:0; padding:4px 0; background:transparent; color:#247a63; cursor:pointer; font:600 12px inherit; } -.database-key-security-info { display:grid; grid-template-columns:repeat(3,minmax(0,1fr)); gap:1px; overflow:hidden; border:1px solid #e3e8e5; border-radius:9px; background:#e3e8e5; }.database-key-security-info span { display:grid; gap:6px; padding:15px; background:#f5f7f6; color:#66706b; font-size:12px; line-height:1.5; }.database-key-security-info strong { color:#35403b; font-size:13px; } -.database-key-diagnostics { margin-top:22px; border:1px solid #dde3e0; border-radius:9px; background:#fff; }.database-key-diagnostics summary { padding:14px 16px; color:#46514c; cursor:pointer; font-size:13px; font-weight:600; }.database-key-diagnostics dl { display:grid; grid-template-columns:repeat(2,minmax(0,1fr)); gap:14px 28px; margin:0; padding:2px 16px 16px; }.database-key-diagnostics button { margin:0 16px 16px; border:0; padding:0; background:transparent; color:#247a63; cursor:pointer; font:600 12px inherit; } -.database-key-connection-actions { margin-top:30px; }.database-key-connection-actions>h2 { margin:0 0 14px; color:#3d4742; font-size:15px; }.database-key-connection-actions>div { display:flex; align-items:center; justify-content:space-between; gap:20px; padding:16px 20px; border:1px solid #dde3e0; border-radius:9px; background:#fff; }.database-key-connection-actions span { display:grid; gap:4px; }.database-key-connection-actions strong { color:#35403b; font-size:13px; }.database-key-connection-actions small { color:#66706b; font-size:12px; }.database-key-connection-actions button { min-height:34px; flex:0 0 auto; border:1px solid #247a63; border-radius:8px; padding:0 13px; background:#fff; color:#247a63; cursor:pointer; }.database-key-connection-actions button:hover:not(:disabled) { background:#f0f7f4; }.database-key-danger { margin-top:30px; padding-bottom:1px; }.database-key-danger>h2 { margin:0 0 14px; color:#c85a5a; font-size:15px; }.database-key-danger>div { display:flex; align-items:center; justify-content:space-between; gap:20px; padding:16px 20px; border:1px solid #efcdcd; background:#fff7f7; }.database-key-danger>div:first-of-type { border-radius:9px 9px 0 0; }.database-key-danger>div:last-of-type { border-top:0; border-radius:0 0 9px 9px; }.database-key-danger span { display:grid; gap:4px; }.database-key-danger strong { color:#4a3c3c; font-size:13px; }.database-key-danger small { color:#786868; font-size:12px; }.database-key-danger button { min-height:34px; flex:0 0 auto; border:1px solid #e8baba; border-radius:8px; padding:0 13px; background:#fff; color:#c85a5a; cursor:pointer; }.database-key-danger button:disabled { cursor:not-allowed; opacity:.45; } -.database-key-confirm-backdrop { position:fixed; z-index:30; inset:0; display:grid; place-items:center; padding:24px; background:rgba(25,32,29,.34); }.database-key-confirm { width:min(100%,430px); padding:22px; border-radius:12px; background:#fff; box-shadow:0 18px 50px rgba(24,35,30,.2); }.database-key-confirm h2 { margin:0; color:#202724; font-size:17px; }.database-key-confirm p { margin:12px 0 22px; color:#66706b; font-size:13px; line-height:1.7; }.database-key-confirm>div { display:flex; justify-content:flex-end; gap:8px; }.database-key-confirm button { min-height:34px; border:1px solid #dde3e0; border-radius:8px; padding:0 14px; background:#fff; color:#46514c; cursor:pointer; }.database-key-confirm button.danger { border-color:#c85a5a; background:#c85a5a; color:#fff; } +.database-key-content { + padding-bottom: 48px; +} +.database-key-badge.unconfigured { + background: #f1f3f2; + color: #66706b; +} +.database-key-badge.validating { + background: #eef4f8; + color: #4f7188; +} +.database-key-badge.invalid { + background: #f9eeee; + color: #c85a5a; +} +.database-key-status-card dl { + display: grid; + grid-template-columns: repeat(2, minmax(0, 1fr)); + gap: 18px 36px; + margin: 0 0 20px; +} +.database-key-status-card dl div { + min-width: 0; +} +.database-key-status-card dt, +.database-key-diagnostics dt { + color: #929a96; + font-size: 12px; +} +.database-key-status-card dd, +.database-key-diagnostics dd { + overflow: hidden; + margin: 5px 0 0; + color: #35403b; + font-size: 13px; + text-overflow: ellipsis; + white-space: nowrap; +} +.database-key-mono { + font-family: ui-monospace, SFMono-Regular, Consolas, monospace; +} +.database-key-success { + color: #2e8b68 !important; +} +.database-key-muted { + color: #66706b !important; +} +.database-key-primary, +.database-key-secondary { + min-height: 36px; + padding: 0 15px; + border: 1px solid #247a63; + border-radius: 8px; + cursor: pointer; + font: 600 13px/1 inherit; +} +.database-key-primary { + background: #247a63; + color: #fff; +} +.database-key-secondary { + background: #fff; + color: #247a63; +} +.database-key-primary:hover:not(:disabled) { + background: #1d6754; +} +.database-key-secondary:hover:not(:disabled) { + background: #f0f7f4; +} +.database-key-primary:disabled, +.database-key-secondary:disabled { + cursor: not-allowed; + opacity: 0.45; +} +.database-key-editor label { + display: block; + margin-bottom: 9px; + color: #46514c; + font-size: 12px; + font-weight: 700; + letter-spacing: 0.04em; + text-transform: uppercase; +} +.database-key-input-row { + display: flex; + min-width: 0; + height: 42px; + border: 1px solid #d5ddda; + border-radius: 8px; + background: #f4f7f5; +} +.database-key-input-row:focus-within { + border-color: #247a63; + box-shadow: 0 0 0 2px rgba(36, 122, 99, 0.09); +} +.database-key-input-row input { + min-width: 0; + flex: 1; + border: 0; + outline: 0; + padding: 0 14px; + background: transparent; + color: #202724; + font: + 13px/1.4 ui-monospace, + SFMono-Regular, + Consolas, + monospace; +} +.database-key-input-row button { + display: grid; + width: 38px; + flex: 0 0 38px; + place-items: center; + border: 0; + background: transparent; + color: #66706b; + cursor: pointer; +} +.database-key-input-row button:hover:not(:disabled) { + color: #247a63; +} +.database-key-input-row button:disabled { + cursor: not-allowed; + opacity: 0.4; +} +.database-key-input-row svg { + width: 17px; + fill: none; + stroke: currentColor; + stroke-linecap: round; + stroke-linejoin: round; + stroke-width: 1.8; +} +.database-key-editor > p { + margin: 9px 0 0; + color: #66706b; + font-size: 12px; + line-height: 1.6; +} +.database-key-actions { + display: flex; + gap: 9px; + margin-top: 18px; +} +.database-key-feedback { + display: flex; + flex-wrap: wrap; + align-items: center; + gap: 8px 18px; + margin-top: 12px; + padding: 12px 15px; + border-radius: 8px; + font-size: 12px; + line-height: 1.5; +} +.database-key-feedback.checking { + background: #f1f4f3; + color: #66706b; +} +.database-key-feedback.checking i { + width: 14px; + height: 14px; + border: 2px solid #c8d5d0; + border-top-color: #247a63; + border-radius: 50%; + animation: settings-spin 0.8s linear infinite; +} +.database-key-feedback.success { + border: 1px solid #bfded3; + background: #eaf5f1; + color: #2e765d; +} +.database-key-feedback.success strong { + width: 100%; +} +.database-key-feedback.error { + border: 1px solid #efcaca; + background: #fff2f2; + color: #a84444; +} +.database-key-auto-heading { + display: flex; + align-items: flex-start; + justify-content: space-between; + gap: 24px; +} +.database-key-auto strong { + color: #35403b; + font-size: 14px; +} +.database-key-auto p { + margin: 6px 0 0; + color: #66706b; + font-size: 12px; + line-height: 1.6; +} +.database-key-prerequisites { + display: flex; + flex-wrap: wrap; + gap: 8px 20px; + margin: 18px 0 0; + padding: 14px 0 0; + border-top: 1px solid #edf0ee; + list-style: none; + color: #8b5c25; + font-size: 12px; +} +.database-key-prerequisites li::before { + content: '○'; + margin-right: 6px; +} +.database-key-prerequisites li.ok { + color: #2e8b68; +} +.database-key-prerequisites li.ok::before { + content: '✓'; +} +.database-key-phases { + display: grid; + grid-template-columns: repeat(5, minmax(0, 1fr)); + gap: 8px; + margin: 18px 0 0; + padding: 0; + counter-reset: key-phase; + list-style: none; +} +.database-key-phases li { + position: relative; + padding-top: 24px; + color: #929a96; + text-align: center; + font-size: 11px; +} +.database-key-phases li::before { + position: absolute; + top: 0; + left: 50%; + display: grid; + width: 18px; + height: 18px; + place-items: center; + border: 1px solid #d5ddda; + border-radius: 50%; + background: #fff; + content: counter(key-phase); + counter-increment: key-phase; + transform: translateX(-50%); +} +.database-key-phases li.active { + color: #247a63; +} +.database-key-phases li.active::before { + border-color: #247a63; + background: #247a63; + color: #fff; +} +.database-key-auto-error { + display: grid; + gap: 4px; + margin-top: 16px; + padding: 12px 14px; + border-left: 3px solid #c68635; + background: #fff8ec; + color: #79501f; + font-size: 12px; +} +.database-key-auto-error span { + overflow-wrap: anywhere; +} +.database-key-auto-error p { + color: #79501f; +} +.database-key-auto-error button { + justify-self: start; + border: 0; + padding: 4px 0; + background: transparent; + color: #247a63; + cursor: pointer; + font: 600 12px inherit; +} +.database-key-security-info { + display: grid; + grid-template-columns: repeat(3, minmax(0, 1fr)); + gap: 1px; + overflow: hidden; + border: 1px solid #e3e8e5; + border-radius: 9px; + background: #e3e8e5; +} +.database-key-security-info span { + display: grid; + gap: 6px; + padding: 15px; + background: #f5f7f6; + color: #66706b; + font-size: 12px; + line-height: 1.5; +} +.database-key-security-info strong { + color: #35403b; + font-size: 13px; +} +.database-key-diagnostics { + margin-top: 22px; + border: 1px solid #dde3e0; + border-radius: 9px; + background: #fff; +} +.database-key-diagnostics summary { + padding: 14px 16px; + color: #46514c; + cursor: pointer; + font-size: 13px; + font-weight: 600; +} +.database-key-diagnostics dl { + display: grid; + grid-template-columns: repeat(2, minmax(0, 1fr)); + gap: 14px 28px; + margin: 0; + padding: 2px 16px 16px; +} +.database-key-diagnostics button { + margin: 0 16px 16px; + border: 0; + padding: 0; + background: transparent; + color: #247a63; + cursor: pointer; + font: 600 12px inherit; +} +.database-key-connection-actions { + margin-top: 30px; +} +.database-key-connection-actions > h2 { + margin: 0 0 14px; + color: #3d4742; + font-size: 15px; +} +.database-key-connection-actions > div { + display: flex; + align-items: center; + justify-content: space-between; + gap: 20px; + padding: 16px 20px; + border: 1px solid #dde3e0; + border-radius: 9px; + background: #fff; +} +.database-key-connection-actions span { + display: grid; + gap: 4px; +} +.database-key-connection-actions strong { + color: #35403b; + font-size: 13px; +} +.database-key-connection-actions small { + color: #66706b; + font-size: 12px; +} +.database-key-connection-actions button { + min-height: 34px; + flex: 0 0 auto; + border: 1px solid #247a63; + border-radius: 8px; + padding: 0 13px; + background: #fff; + color: #247a63; + cursor: pointer; +} +.database-key-connection-actions button:hover:not(:disabled) { + background: #f0f7f4; +} +.database-key-danger { + margin-top: 30px; + padding-bottom: 1px; +} +.database-key-danger > h2 { + margin: 0 0 14px; + color: #c85a5a; + font-size: 15px; +} +.database-key-danger > div { + display: flex; + align-items: center; + justify-content: space-between; + gap: 20px; + padding: 16px 20px; + border: 1px solid #efcdcd; + background: #fff7f7; +} +.database-key-danger > div:first-of-type { + border-radius: 9px 9px 0 0; +} +.database-key-danger > div:last-of-type { + border-top: 0; + border-radius: 0 0 9px 9px; +} +.database-key-danger span { + display: grid; + gap: 4px; +} +.database-key-danger strong { + color: #4a3c3c; + font-size: 13px; +} +.database-key-danger small { + color: #786868; + font-size: 12px; +} +.database-key-danger button { + min-height: 34px; + flex: 0 0 auto; + border: 1px solid #e8baba; + border-radius: 8px; + padding: 0 13px; + background: #fff; + color: #c85a5a; + cursor: pointer; +} +.database-key-danger button:disabled { + cursor: not-allowed; + opacity: 0.45; +} +.database-key-confirm-backdrop { + position: fixed; + z-index: 30; + inset: 0; + display: grid; + place-items: center; + padding: 24px; + background: rgba(25, 32, 29, 0.34); +} +.database-key-confirm { + width: min(100%, 430px); + padding: 22px; + border-radius: 12px; + background: #fff; + box-shadow: 0 18px 50px rgba(24, 35, 30, 0.2); +} +.database-key-confirm h2 { + margin: 0; + color: #202724; + font-size: 17px; +} +.database-key-confirm p { + margin: 12px 0 22px; + color: #66706b; + font-size: 13px; + line-height: 1.7; +} +.database-key-confirm > div { + display: flex; + justify-content: flex-end; + gap: 8px; +} +.database-key-confirm button { + min-height: 34px; + border: 1px solid #dde3e0; + border-radius: 8px; + padding: 0 14px; + background: #fff; + color: #46514c; + cursor: pointer; +} +.database-key-confirm button.danger { + border-color: #c85a5a; + background: #c85a5a; + color: #fff; +} /* SETTINGS-03: image decryption */ -.image-decryption-content { padding-bottom:48px; } -.image-decrypt-badge.unconfigured { background:#f1f3f2; color:#66706b; }.image-decrypt-badge.partial { background:#fff6e8; color:#a56a24; } -.image-decrypt-status dl { display:grid; grid-template-columns:repeat(3,minmax(0,1fr)); gap:18px 30px; margin:0 0 20px; }.image-decrypt-status dl div { min-width:0; }.image-decrypt-status dt { color:#929a96; font-size:12px; }.image-decrypt-status dd { overflow:hidden; margin:5px 0 0; color:#35403b; font-size:13px; text-overflow:ellipsis; white-space:nowrap; }.image-decrypt-status .image-decrypt-wide { grid-column:span 2; }.image-decrypt-mono { font-family:ui-monospace,SFMono-Regular,Consolas,monospace; }.image-state-success { color:#2e8b68!important; }.image-state-muted { color:#66706b!important; }.image-state-error { color:#c85a5a!important; } -.image-resource-checks { padding:10px 18px; }.image-resource-checks>div { display:flex; min-width:0; align-items:center; gap:11px; padding:13px 2px; border-bottom:1px solid #edf0ee; }.image-resource-checks>div:last-child { border-bottom:0; }.image-resource-checks strong { color:#3d4742; font-size:13px; }.image-resource-checks small { margin-left:auto; overflow:hidden; color:#66706b; text-overflow:ellipsis; white-space:nowrap; font-size:12px; }.image-resource-icon { display:grid; width:18px; height:18px; flex:0 0 auto; place-items:center; border-radius:50%; background:#f1f3f2; color:#66706b; font-size:11px; font-weight:700; }.image-resource-icon.available { background:#e6f2ed; color:#2e8b68; }.image-resource-icon.unavailable { background:#f9eaea; color:#c85a5a; } -.image-key-editor { display:grid; gap:17px; }.image-key-editor label { display:grid; min-width:0; gap:8px; color:#46514c; font-size:12px; font-weight:600; }.image-key-editor input,.image-test-section select { box-sizing:border-box; width:100%; height:40px; border:1px solid #d5ddda; border-radius:8px; outline:0; padding:0 12px; background:#f4f7f5; color:#202724; font:12px/1.4 ui-monospace,SFMono-Regular,Consolas,monospace; }.image-key-editor input:focus,.image-test-section select:focus { border-color:#247a63; box-shadow:0 0 0 2px rgba(36,122,99,.09); }.image-key-grid { display:grid; grid-template-columns:180px minmax(0,1fr); gap:14px; }.image-key-editor>p { margin:0; color:#66706b; font-size:12px; line-height:1.6; } -.image-test-section>div:first-child strong,.image-auto-detect strong,.image-auto-unavailable strong { color:#35403b; font-size:14px; }.image-test-section>div:first-child p,.image-auto-detect p,.image-auto-unavailable p { margin:6px 0 0; color:#66706b; font-size:12px; line-height:1.6; }.image-test-section>label { display:block; margin:18px 0 8px; color:#46514c; font-size:12px; font-weight:600; }.image-test-actions { display:flex; gap:9px; margin-top:14px; }.image-test-result { display:flex; flex-wrap:wrap; gap:8px 18px; margin-top:14px; padding:12px 14px; border-radius:8px; color:#2e765d; font-size:12px; }.image-test-result.success { border:1px solid #bfded3; background:#eaf5f1; }.image-test-result.error { border:1px solid #efcaca; background:#fff2f2; color:#a84444; }.image-test-result p { width:100%; margin:2px 0 0; }.image-inline-error { margin:14px 0 0; color:#a84444; font-size:12px; } -.image-auto-heading { display:flex; align-items:flex-start; justify-content:space-between; gap:24px; }.image-auto-detect ul { display:flex; flex-wrap:wrap; gap:10px 22px; margin:17px 0 0; padding:14px 0 0; border-top:1px solid #edf0ee; list-style:none; color:#8b5c25; font-size:12px; }.image-auto-detect li::before { content:'○'; margin-right:6px; }.image-auto-detect li.ok { color:#2e8b68; }.image-auto-detect li.ok::before { content:'✓'; }.image-auto-progress { margin-top:12px!important; padding:9px 12px; border-radius:7px; background:#f1f4f3; }.image-auto-unavailable { padding-top:20px; padding-bottom:20px; }.image-key-danger>div { border-radius:9px!important; }.image-key-danger { margin-top:30px; } -.image-auto-phases { display:grid; grid-template-columns:repeat(4,minmax(0,1fr)); gap:8px; margin:18px 0 0; padding:0; counter-reset:image-auto-phase; list-style:none; }.image-auto-phases li { position:relative; padding-top:24px; color:#929a96; text-align:center; font-size:11px; }.image-auto-phases li::before { position:absolute; top:0; left:50%; display:grid; width:18px; height:18px; place-items:center; border:1px solid #d5ddda; border-radius:50%; background:#fff; content:counter(image-auto-phase); counter-increment:image-auto-phase; transform:translateX(-50%); }.image-auto-phases li.active { color:#247a63; }.image-auto-phases li.active::before { border-color:#247a63; background:#247a63; color:#fff; }.image-auto-error { margin:12px 0 0!important; padding:10px 12px; border-left:3px solid #c85a5a; background:#fff2f2; color:#a84444!important; }.image-auto-success { display:grid; grid-template-columns:minmax(0,1fr) auto; gap:7px 20px; align-items:center; margin-top:14px; padding:14px 16px; border:1px solid #bfded3; border-radius:8px; background:#eaf5f1; color:#2e765d; font-size:12px; }.image-auto-success strong { grid-column:1; color:#2e765d; }.image-auto-success span { grid-column:1; }.image-auto-success button { grid-column:2; grid-row:1/4; } +.image-decryption-content { + padding-bottom: 48px; +} +.image-decrypt-badge.unconfigured { + background: #f1f3f2; + color: #66706b; +} +.image-decrypt-badge.partial { + background: #fff6e8; + color: #a56a24; +} +.image-decrypt-status dl { + display: grid; + grid-template-columns: repeat(3, minmax(0, 1fr)); + gap: 18px 30px; + margin: 0 0 20px; +} +.image-decrypt-status dl div { + min-width: 0; +} +.image-decrypt-status dt { + color: #929a96; + font-size: 12px; +} +.image-decrypt-status dd { + overflow: hidden; + margin: 5px 0 0; + color: #35403b; + font-size: 13px; + text-overflow: ellipsis; + white-space: nowrap; +} +.image-decrypt-status .image-decrypt-wide { + grid-column: span 2; +} +.image-decrypt-mono { + font-family: ui-monospace, SFMono-Regular, Consolas, monospace; +} +.image-state-success { + color: #2e8b68 !important; +} +.image-state-muted { + color: #66706b !important; +} +.image-state-error { + color: #c85a5a !important; +} +.image-resource-checks { + padding: 10px 18px; +} +.image-resource-checks > div { + display: flex; + min-width: 0; + align-items: center; + gap: 11px; + padding: 13px 2px; + border-bottom: 1px solid #edf0ee; +} +.image-resource-checks > div:last-child { + border-bottom: 0; +} +.image-resource-checks strong { + color: #3d4742; + font-size: 13px; +} +.image-resource-checks small { + margin-left: auto; + overflow: hidden; + color: #66706b; + text-overflow: ellipsis; + white-space: nowrap; + font-size: 12px; +} +.image-resource-icon { + display: grid; + width: 18px; + height: 18px; + flex: 0 0 auto; + place-items: center; + border-radius: 50%; + background: #f1f3f2; + color: #66706b; + font-size: 11px; + font-weight: 700; +} +.image-resource-icon.available { + background: #e6f2ed; + color: #2e8b68; +} +.image-resource-icon.unavailable { + background: #f9eaea; + color: #c85a5a; +} +.image-key-editor { + display: grid; + gap: 17px; +} +.image-key-editor label { + display: grid; + min-width: 0; + gap: 8px; + color: #46514c; + font-size: 12px; + font-weight: 600; +} +.image-key-editor input, +.image-test-section select { + box-sizing: border-box; + width: 100%; + height: 40px; + border: 1px solid #d5ddda; + border-radius: 8px; + outline: 0; + padding: 0 12px; + background: #f4f7f5; + color: #202724; + font: + 12px/1.4 ui-monospace, + SFMono-Regular, + Consolas, + monospace; +} +.image-key-editor input:focus, +.image-test-section select:focus { + border-color: #247a63; + box-shadow: 0 0 0 2px rgba(36, 122, 99, 0.09); +} +.image-key-grid { + display: grid; + grid-template-columns: 180px minmax(0, 1fr); + gap: 14px; +} +.image-key-editor > p { + margin: 0; + color: #66706b; + font-size: 12px; + line-height: 1.6; +} +.image-test-section > div:first-child strong, +.image-auto-detect strong, +.image-auto-unavailable strong { + color: #35403b; + font-size: 14px; +} +.image-test-section > div:first-child p, +.image-auto-detect p, +.image-auto-unavailable p { + margin: 6px 0 0; + color: #66706b; + font-size: 12px; + line-height: 1.6; +} +.image-test-section > label { + display: block; + margin: 18px 0 8px; + color: #46514c; + font-size: 12px; + font-weight: 600; +} +.image-test-actions { + display: flex; + gap: 9px; + margin-top: 14px; +} +.image-test-result { + display: flex; + flex-wrap: wrap; + gap: 8px 18px; + margin-top: 14px; + padding: 12px 14px; + border-radius: 8px; + color: #2e765d; + font-size: 12px; +} +.image-test-result.success { + border: 1px solid #bfded3; + background: #eaf5f1; +} +.image-test-result.error { + border: 1px solid #efcaca; + background: #fff2f2; + color: #a84444; +} +.image-test-result p { + width: 100%; + margin: 2px 0 0; +} +.image-inline-error { + margin: 14px 0 0; + color: #a84444; + font-size: 12px; +} +.image-auto-heading { + display: flex; + align-items: flex-start; + justify-content: space-between; + gap: 24px; +} +.image-auto-detect ul { + display: flex; + flex-wrap: wrap; + gap: 10px 22px; + margin: 17px 0 0; + padding: 14px 0 0; + border-top: 1px solid #edf0ee; + list-style: none; + color: #8b5c25; + font-size: 12px; +} +.image-auto-detect li::before { + content: '○'; + margin-right: 6px; +} +.image-auto-detect li.ok { + color: #2e8b68; +} +.image-auto-detect li.ok::before { + content: '✓'; +} +.image-auto-progress { + margin-top: 12px !important; + padding: 9px 12px; + border-radius: 7px; + background: #f1f4f3; +} +.image-auto-unavailable { + padding-top: 20px; + padding-bottom: 20px; +} +.image-key-danger > div { + border-radius: 9px !important; +} +.image-key-danger { + margin-top: 30px; +} +.image-auto-phases { + display: grid; + grid-template-columns: repeat(4, minmax(0, 1fr)); + gap: 8px; + margin: 18px 0 0; + padding: 0; + counter-reset: image-auto-phase; + list-style: none; +} +.image-auto-phases li { + position: relative; + padding-top: 24px; + color: #929a96; + text-align: center; + font-size: 11px; +} +.image-auto-phases li::before { + position: absolute; + top: 0; + left: 50%; + display: grid; + width: 18px; + height: 18px; + place-items: center; + border: 1px solid #d5ddda; + border-radius: 50%; + background: #fff; + content: counter(image-auto-phase); + counter-increment: image-auto-phase; + transform: translateX(-50%); +} +.image-auto-phases li.active { + color: #247a63; +} +.image-auto-phases li.active::before { + border-color: #247a63; + background: #247a63; + color: #fff; +} +.image-auto-error { + margin: 12px 0 0 !important; + padding: 10px 12px; + border-left: 3px solid #c85a5a; + background: #fff2f2; + color: #a84444 !important; +} +.image-auto-success { + display: grid; + grid-template-columns: minmax(0, 1fr) auto; + gap: 7px 20px; + align-items: center; + margin-top: 14px; + padding: 14px 16px; + border: 1px solid #bfded3; + border-radius: 8px; + background: #eaf5f1; + color: #2e765d; + font-size: 12px; +} +.image-auto-success strong { + grid-column: 1; + color: #2e765d; +} +.image-auto-success span { + grid-column: 1; +} +.image-auto-success button { + grid-column: 2; + grid-row: 1/4; +} /* SETTINGS-04: AI provider center */ -.ai-model-content { padding-bottom:48px; }.ai-model-default { display:flex; align-items:center; justify-content:space-between; gap:20px; }.ai-model-default>div { display:grid; gap:5px; }.ai-model-default>div>span,.ai-model-default small { color:#66706b; font-size:12px; }.ai-model-default strong { color:#202724; font-size:18px; }.ai-model-page-error { padding:11px 14px; border-left:3px solid #c85a5a; background:#fff2f2; color:#a84444; font-size:12px; }.ai-provider-list { display:grid; gap:12px; }.ai-provider-card { border:1px solid #dde3e0; border-radius:10px; background:#fff; padding:19px 20px; }.ai-provider-card header { display:flex; align-items:flex-start; justify-content:space-between; gap:18px; }.ai-provider-card h3 { margin:0 0 5px; color:#202724; font-size:15px; }.ai-provider-card header span { color:#66706b; font-size:12px; }.ai-provider-status { border-radius:12px; padding:4px 8px; background:#f1f3f2; }.ai-provider-status.connected { background:#eaf5f1; color:#2e8b68!important; }.ai-provider-status.error { background:#fff2f2; color:#c85a5a!important; }.ai-provider-card dl { display:grid; grid-template-columns:2fr 1fr 1fr; gap:18px; margin:18px 0; }.ai-provider-card dt { color:#929a96; font-size:11px; }.ai-provider-card dd { overflow:hidden; margin:5px 0 0; color:#3d4742; text-overflow:ellipsis; white-space:nowrap; font-size:12px; }.ai-provider-card footer { display:flex; flex-wrap:wrap; gap:8px; padding-top:14px; border-top:1px solid #edf0ee; }.ai-provider-card button,.ai-provider-editor button { min-height:32px; border:1px solid #d5ddda; border-radius:7px; padding:0 11px; background:#fff; color:#46514c; cursor:pointer; font:600 12px inherit; }.ai-provider-card button:hover,.ai-provider-editor button:hover { border-color:#247a63; color:#247a63; }.ai-provider-card button:disabled,.ai-provider-editor button:disabled { cursor:not-allowed; opacity:.45; }.ai-provider-card button.danger,.ai-provider-editor button.danger { color:#c85a5a; }.ai-provider-error { margin:0 0 12px; color:#a84444; font-size:12px; }.ai-provider-empty { color:#66706b; text-align:center; }.ai-provider-empty p { margin:6px 0 0; font-size:12px; } -.ai-provider-editor { display:grid; gap:18px; margin-top:22px; }.ai-provider-editor>header { display:flex; align-items:flex-start; justify-content:space-between; gap:18px; }.ai-provider-editor h2,.ai-provider-editor h3 { margin:0; color:#35403b; font-size:15px; }.ai-provider-editor header p { margin:5px 0 0; color:#66706b; font-size:12px; }.ai-provider-editor label { display:grid; gap:7px; color:#46514c; font-size:12px; font-weight:600; }.ai-provider-editor input:not([type=checkbox]):not([type=radio]),.ai-provider-editor select,.ai-provider-editor textarea { box-sizing:border-box; width:100%; border:1px solid #d5ddda; border-radius:7px; outline:0; padding:9px 10px; background:#f4f7f5; color:#202724; font:12px/1.4 ui-monospace,SFMono-Regular,Consolas,monospace; }.ai-provider-editor input:focus,.ai-provider-editor select:focus,.ai-provider-editor textarea:focus { border-color:#247a63; box-shadow:0 0 0 2px rgba(36,122,99,.08); }.ai-provider-form-grid,.ai-provider-advanced>div { display:grid; grid-template-columns:repeat(2,minmax(0,1fr)); gap:14px; }.ai-provider-form-grid .wide,.ai-provider-advanced .wide { grid-column:1/-1; }.ai-model-table-heading { display:flex; align-items:center; justify-content:space-between; }.ai-model-table { display:grid; gap:9px; }.ai-model-row { display:grid; grid-template-columns:1fr 1fr repeat(3,auto) 110px auto auto; gap:8px; align-items:center; padding:11px; border:1px solid #e3e8e5; border-radius:8px; }.ai-model-row label { display:flex; align-items:center; gap:4px; white-space:nowrap; font-weight:400; }.ai-provider-advanced,.ai-provider-preview { border:1px solid #e3e8e5; border-radius:8px; }.ai-provider-advanced summary,.ai-provider-preview summary { padding:12px 14px; cursor:pointer; color:#46514c; font-size:13px; font-weight:600; }.ai-provider-advanced>div { padding:2px 14px 14px; }.ai-provider-advanced textarea { min-height:90px; resize:vertical; }.ai-provider-preview pre { overflow:auto; max-height:280px; margin:0; padding:0 14px 14px; color:#46514c; font:11px/1.6 ui-monospace,SFMono-Regular,Consolas,monospace; }.ai-provider-editor>footer { display:flex; justify-content:flex-end; gap:8px; } -@keyframes settings-spin { to { transform:rotate(360deg); } } -@media (max-width:900px) { - .settings-sidebar { width:248px; flex-basis:248px; } - .settings-page-content { padding:24px; } - .settings-page-header { padding:20px 24px; } - .settings-account-overview { grid-template-columns:minmax(0,1fr) auto; gap:18px; } - .settings-account-facts { grid-column:1/-1; grid-template-columns:repeat(2,minmax(0,1fr)); } - .settings-account-actions { grid-row:1; grid-column:2; } - .database-key-security-info { grid-template-columns:1fr; } - .image-decrypt-status dl { grid-template-columns:repeat(2,minmax(0,1fr)); }.image-decrypt-status .image-decrypt-wide { grid-column:span 2; } - .ai-model-row { grid-template-columns:1fr 1fr; }.ai-model-row label,.ai-model-row button { justify-self:start; } +.ai-model-content { + padding-bottom: 48px; } -@media (max-width:700px) { - .settings-account-overview { grid-template-columns:minmax(0,1fr); } - .settings-account-actions { grid-row:auto; grid-column:1; grid-template-columns:repeat(2,minmax(0,1fr)); } - .settings-account-root { grid-column:1; } - .database-key-status-card dl,.database-key-diagnostics dl { grid-template-columns:1fr; } - .database-key-auto-heading { flex-direction:column; }.database-key-phases { grid-template-columns:1fr; }.database-key-phases li { padding:0 0 0 28px; text-align:left; }.database-key-phases li::before { top:-2px; left:0; transform:none; } - .image-decrypt-status dl,.image-key-grid { grid-template-columns:1fr; }.image-decrypt-status .image-decrypt-wide { grid-column:1; }.image-auto-heading { flex-direction:column; }.image-auto-phases { grid-template-columns:1fr; }.image-auto-phases li { padding:0 0 0 28px; text-align:left; }.image-auto-phases li::before { top:-2px; left:0; transform:none; }.image-auto-success { grid-template-columns:1fr; }.image-auto-success button { grid-column:1; grid-row:auto; justify-self:start; }.image-resource-checks>div { align-items:flex-start; }.image-resource-checks small { white-space:normal; text-align:right; } - .ai-provider-card dl,.ai-provider-form-grid,.ai-provider-advanced>div { grid-template-columns:1fr; }.ai-provider-form-grid .wide,.ai-provider-advanced .wide { grid-column:1; }.ai-model-row { grid-template-columns:1fr; } +.ai-model-default { + display: flex; + align-items: center; + justify-content: space-between; + gap: 20px; +} +.ai-model-default > div { + display: grid; + gap: 5px; +} +.ai-model-default > div > span, +.ai-model-default small { + color: #66706b; + font-size: 12px; +} +.ai-model-default strong { + color: #202724; + font-size: 18px; +} +.ai-model-page-error { + padding: 11px 14px; + border-left: 3px solid #c85a5a; + background: #fff2f2; + color: #a84444; + font-size: 12px; +} +.ai-provider-list { + display: grid; + gap: 12px; +} +.ai-provider-card { + border: 1px solid #dde3e0; + border-radius: 10px; + background: #fff; + padding: 19px 20px; +} +.ai-provider-card header { + display: flex; + align-items: flex-start; + justify-content: space-between; + gap: 18px; +} +.ai-provider-card h3 { + margin: 0 0 5px; + color: #202724; + font-size: 15px; +} +.ai-provider-card header span { + color: #66706b; + font-size: 12px; +} +.ai-provider-status { + border-radius: 12px; + padding: 4px 8px; + background: #f1f3f2; +} +.ai-provider-status.connected { + background: #eaf5f1; + color: #2e8b68 !important; +} +.ai-provider-status.error { + background: #fff2f2; + color: #c85a5a !important; +} +.ai-provider-card dl { + display: grid; + grid-template-columns: 2fr 1fr 1fr; + gap: 18px; + margin: 18px 0; +} +.ai-provider-card dt { + color: #929a96; + font-size: 11px; +} +.ai-provider-card dd { + overflow: hidden; + margin: 5px 0 0; + color: #3d4742; + text-overflow: ellipsis; + white-space: nowrap; + font-size: 12px; +} +.ai-provider-card footer { + display: flex; + flex-wrap: wrap; + gap: 8px; + padding-top: 14px; + border-top: 1px solid #edf0ee; +} +.ai-provider-card button, +.ai-provider-editor button { + min-height: 32px; + border: 1px solid #d5ddda; + border-radius: 7px; + padding: 0 11px; + background: #fff; + color: #46514c; + cursor: pointer; + font: 600 12px inherit; +} +.ai-provider-card button:hover, +.ai-provider-editor button:hover { + border-color: #247a63; + color: #247a63; +} +.ai-provider-card button:disabled, +.ai-provider-editor button:disabled { + cursor: not-allowed; + opacity: 0.45; +} +.ai-provider-card button.danger, +.ai-provider-editor button.danger { + color: #c85a5a; +} +.ai-provider-error { + margin: 0 0 12px; + color: #a84444; + font-size: 12px; +} +.ai-provider-empty { + color: #66706b; + text-align: center; +} +.ai-provider-empty p { + margin: 6px 0 0; + font-size: 12px; +} +.ai-provider-editor { + display: grid; + gap: 18px; + margin-top: 22px; +} +.ai-provider-editor > header { + display: flex; + align-items: flex-start; + justify-content: space-between; + gap: 18px; +} +.ai-provider-editor h2, +.ai-provider-editor h3 { + margin: 0; + color: #35403b; + font-size: 15px; +} +.ai-provider-editor header p { + margin: 5px 0 0; + color: #66706b; + font-size: 12px; +} +.ai-provider-editor label { + display: grid; + gap: 7px; + color: #46514c; + font-size: 12px; + font-weight: 600; +} +.ai-provider-editor input:not([type='checkbox']):not([type='radio']), +.ai-provider-editor select, +.ai-provider-editor textarea { + box-sizing: border-box; + width: 100%; + border: 1px solid #d5ddda; + border-radius: 7px; + outline: 0; + padding: 9px 10px; + background: #f4f7f5; + color: #202724; + font: + 12px/1.4 ui-monospace, + SFMono-Regular, + Consolas, + monospace; +} +.ai-provider-editor input:focus, +.ai-provider-editor select:focus, +.ai-provider-editor textarea:focus { + border-color: #247a63; + box-shadow: 0 0 0 2px rgba(36, 122, 99, 0.08); +} +.ai-provider-form-grid, +.ai-provider-advanced > div { + display: grid; + grid-template-columns: repeat(2, minmax(0, 1fr)); + gap: 14px; +} +.ai-provider-form-grid .wide, +.ai-provider-advanced .wide { + grid-column: 1/-1; +} +.ai-model-table-heading { + display: flex; + align-items: center; + justify-content: space-between; +} +.ai-model-table { + display: grid; + gap: 9px; +} +.ai-model-row { + display: grid; + grid-template-columns: 1fr 1fr repeat(3, auto) 110px auto auto; + gap: 8px; + align-items: center; + padding: 11px; + border: 1px solid #e3e8e5; + border-radius: 8px; +} +.ai-model-row label { + display: flex; + align-items: center; + gap: 4px; + white-space: nowrap; + font-weight: 400; +} +.ai-provider-advanced, +.ai-provider-preview { + border: 1px solid #e3e8e5; + border-radius: 8px; +} +.ai-provider-advanced summary, +.ai-provider-preview summary { + padding: 12px 14px; + cursor: pointer; + color: #46514c; + font-size: 13px; + font-weight: 600; +} +.ai-provider-advanced > div { + padding: 2px 14px 14px; +} +.ai-provider-advanced textarea { + min-height: 90px; + resize: vertical; +} +.ai-provider-preview pre { + overflow: auto; + max-height: 280px; + margin: 0; + padding: 0 14px 14px; + color: #46514c; + font: + 11px/1.6 ui-monospace, + SFMono-Regular, + Consolas, + monospace; +} +.ai-provider-editor > footer { + display: flex; + justify-content: flex-end; + gap: 8px; +} +@keyframes settings-spin { + to { + transform: rotate(360deg); + } +} +@media (max-width: 900px) { + .settings-sidebar { + width: 248px; + flex-basis: 248px; + } + .settings-page-content { + padding: 24px; + } + .settings-page-header { + padding: 20px 24px; + } + .settings-account-overview { + grid-template-columns: minmax(0, 1fr) auto; + gap: 18px; + } + .settings-account-facts { + grid-column: 1/-1; + grid-template-columns: repeat(2, minmax(0, 1fr)); + } + .settings-account-actions { + grid-row: 1; + grid-column: 2; + } + .database-key-security-info { + grid-template-columns: 1fr; + } + .image-decrypt-status dl { + grid-template-columns: repeat(2, minmax(0, 1fr)); + } + .image-decrypt-status .image-decrypt-wide { + grid-column: span 2; + } + .ai-model-row { + grid-template-columns: 1fr 1fr; + } + .ai-model-row label, + .ai-model-row button { + justify-self: start; + } +} +@media (max-width: 700px) { + .settings-account-overview { + grid-template-columns: minmax(0, 1fr); + } + .settings-account-actions { + grid-row: auto; + grid-column: 1; + grid-template-columns: repeat(2, minmax(0, 1fr)); + } + .settings-account-root { + grid-column: 1; + } + .database-key-status-card dl, + .database-key-diagnostics dl { + grid-template-columns: 1fr; + } + .database-key-auto-heading { + flex-direction: column; + } + .database-key-phases { + grid-template-columns: 1fr; + } + .database-key-phases li { + padding: 0 0 0 28px; + text-align: left; + } + .database-key-phases li::before { + top: -2px; + left: 0; + transform: none; + } + .image-decrypt-status dl, + .image-key-grid { + grid-template-columns: 1fr; + } + .image-decrypt-status .image-decrypt-wide { + grid-column: 1; + } + .image-auto-heading { + flex-direction: column; + } + .image-auto-phases { + grid-template-columns: 1fr; + } + .image-auto-phases li { + padding: 0 0 0 28px; + text-align: left; + } + .image-auto-phases li::before { + top: -2px; + left: 0; + transform: none; + } + .image-auto-success { + grid-template-columns: 1fr; + } + .image-auto-success button { + grid-column: 1; + grid-row: auto; + justify-self: start; + } + .image-resource-checks > div { + align-items: flex-start; + } + .image-resource-checks small { + white-space: normal; + text-align: right; + } + .ai-provider-card dl, + .ai-provider-form-grid, + .ai-provider-advanced > div { + grid-template-columns: 1fr; + } + .ai-provider-form-grid .wide, + .ai-provider-advanced .wide { + grid-column: 1; + } + .ai-model-row { + grid-template-columns: 1fr; + } +} +.api-center-layout > * { + min-width: 0; + min-height: 0; + box-sizing: border-box; +} +.api-section-heading h2, +.api-introduction h2, +.api-integrations h2, +.api-runtime-title h2 { + margin: 0; + color: var(--wxex-text-primary); + font: 700 17px/24px var(--wxex-font); +} +.ready-text { + color: var(--wxex-success); +} +.api-main { + position: relative; + display: flex; + min-width: 0; + min-height: 0; + overflow: hidden; + background: var(--wxex-bg-main); +} +.api-main-scroll { + flex: 1; + min-width: 0; + min-height: 0; + overflow-y: auto; + padding: 22px 28px 36px; +} +.api-workspace-heading { + display: flex; + align-items: flex-start; + justify-content: space-between; + gap: 16px; + padding-bottom: 16px; + border-bottom: 1px solid var(--wxex-border); +} +.api-title-line { + display: flex; + flex-wrap: wrap; + align-items: center; + gap: 8px; +} +.api-title-line h1 { + margin: 0; + color: var(--wxex-text-primary); + font: 700 22px/30px var(--wxex-font); +} +.api-workspace-heading p { + margin: 4px 0 0; + color: var(--wxex-text-secondary); + font: 13px/19px var(--wxex-font); +} +.api-skill-status, +.api-version { + padding: 2px 7px; + border-radius: 5px; + font: 700 11px/17px var(--wxex-font); +} +.api-skill-status.ready { + color: var(--wxex-brand); + background: var(--wxex-brand-soft); +} +.api-skill-status.error { + color: var(--wxex-danger); + background: #fff1f1; +} +.api-version { + color: var(--wxex-text-muted); + background: var(--wxex-bg-app); +} +.api-header-actions, +.api-tester-actions, +.api-runtime-actions, +.api-skill-file-actions { + display: flex; + flex-wrap: wrap; + gap: 8px; +} +.api-header-actions button, +.api-tester-actions button, +.api-runtime-actions button, +.api-endpoint-row button, +.api-response-summary + pre + p + button, +.api-skill-file-actions button { + min-height: 32px; + padding: 0 11px; + border: 1px solid var(--wxex-border); + border-radius: var(--wxex-radius-sm); + background: var(--wxex-bg-elevated); + color: var(--wxex-text-primary); + cursor: pointer; + font: 600 12px/18px var(--wxex-font); +} +.api-header-actions button:hover, +.api-tester-actions button:hover, +.api-runtime-actions button:hover, +.api-endpoint-row button:hover, +.api-skill-file-actions button:hover { + border-color: var(--wxex-brand); + color: var(--wxex-brand); +} +.api-primary-button { + border-color: var(--wxex-brand) !important; + background: var(--wxex-brand) !important; + color: #fff !important; +} +.api-primary-button:hover:not(:disabled), +.api-primary-button:focus-visible, +.api-primary-button:active { + background: var(--wxex-brand-hover) !important; + color: #fff !important; +} +.api-primary-button:disabled { + cursor: not-allowed; + opacity: 0.55; +} +.api-trust-bar { + display: flex; + flex-wrap: wrap; + align-items: center; + gap: 10px; + margin-top: 16px; + padding: 10px 12px; + border: 1px solid var(--wxex-border); + border-radius: var(--wxex-radius-md); + background: #f5f8f6; + color: var(--wxex-text-secondary); + font: 12px/18px var(--wxex-font); +} +.api-trust-bar i { + width: 3px; + height: 3px; + border-radius: 50%; + background: var(--wxex-text-muted); +} +.api-introduction { + margin-top: 24px; + padding: 18px 20px; + border: 1px solid var(--wxex-border); + border-radius: var(--wxex-radius-lg); + background: var(--wxex-bg-elevated); +} +.api-introduction p { + margin: 10px 0 16px; + color: var(--wxex-text-secondary); + font: 13px/20px var(--wxex-font); +} +.api-flow { + display: flex; + align-items: center; + justify-content: space-between; + gap: 10px; + padding: 12px; + border: 1px dashed var(--wxex-border); + border-radius: var(--wxex-radius-md); + color: var(--wxex-text-secondary); + font: 12px/18px var(--wxex-font); +} +.api-flow span, +.api-flow strong { + padding: 7px 10px; + border: 1px solid var(--wxex-border); + border-radius: 5px; + background: #fff; + white-space: nowrap; +} +.api-flow strong { + color: var(--wxex-brand); + background: var(--wxex-brand-soft); +} +.api-flow b { + color: var(--wxex-text-muted); + font-size: 17px; +} +.api-endpoint-catalog, +.api-request-tester { + margin-top: 26px; +} +.skill-install-flow { + margin-top: 26px; + padding: 18px 20px; + border: 1px solid var(--wxex-border); + border-radius: var(--wxex-radius-lg); + background: var(--wxex-bg-elevated); +} +.skill-flow-steps { + display: grid; + gap: 16px; + margin-top: 16px; +} +.skill-flow-steps > section { + position: relative; + display: grid; + grid-template-columns: 26px minmax(0, 1fr); + gap: 10px; + color: var(--wxex-text-muted); +} +.skill-flow-steps > section:not(:last-child)::after { + position: absolute; + top: 28px; + left: 12px; + bottom: -16px; + width: 1px; + background: var(--wxex-border); + content: ''; +} +.skill-flow-steps > section > b { + position: relative; + z-index: 1; + display: grid; + width: 24px; + height: 24px; + place-items: center; + border: 1px solid var(--wxex-border); + border-radius: 50%; + background: var(--wxex-bg-elevated); + font: 700 12px/1 var(--wxex-font); +} +.skill-flow-steps > section.active > b, +.skill-flow-steps > section.done > b { + border-color: var(--wxex-brand); + background: var(--wxex-brand); + color: #fff; +} +.skill-flow-steps h3 { + margin: 1px 0 5px; + color: var(--wxex-text-primary); + font: 700 14px/20px var(--wxex-font); +} +.skill-flow-steps p { + margin: 3px 0; + color: var(--wxex-text-secondary); + font: 12px/18px var(--wxex-font); +} +.skill-flow-actions { + display: flex; + flex-wrap: wrap; + gap: 8px; + margin-top: 10px; +} +.skill-flow-actions button, +.api-skill-details button { + min-height: 30px; + padding: 0 10px; + border: 1px solid var(--wxex-border); + border-radius: var(--wxex-radius-sm); + background: #fff; + color: var(--wxex-text-primary); + cursor: pointer; + font: 600 12px/18px var(--wxex-font); +} +.skill-flow-actions button:disabled, +.api-skill-details button:disabled { + cursor: not-allowed; + opacity: 0.55; +} +.skill-target-selector { + display: flex; + flex-wrap: wrap; + gap: 6px; + margin-top: 8px; +} +.skill-target-selector button { + min-height: 30px; + padding: 0 10px; + border: 1px solid var(--wxex-border); + border-radius: var(--wxex-radius-sm); + background: #fff; + color: var(--wxex-text-secondary); + cursor: pointer; + font: 600 12px/18px var(--wxex-font); +} +.skill-target-selector button.active { + border-color: var(--wxex-brand); + background: var(--wxex-brand-soft); + color: var(--wxex-brand); +} +.api-skill-details { + margin-top: 16px; + border: 1px solid var(--wxex-border); + border-radius: var(--wxex-radius-md); + background: var(--wxex-bg-elevated); +} +.api-skill-details summary { + padding: 12px 14px; + color: var(--wxex-text-primary); + cursor: pointer; + font: 700 13px/18px var(--wxex-font); +} +.api-skill-details > dl, +.api-skill-details > div { + margin: 0; + padding: 0 14px 14px; +} +.api-skill-details dl div { + display: grid; + grid-template-columns: 72px minmax(0, 1fr); + gap: 8px; + padding: 4px 0; + color: var(--wxex-text-secondary); + font: 12px/18px var(--wxex-font); +} +.api-skill-details dd { + margin: 0; + color: var(--wxex-text-primary); +} +.api-skill-more { + position: relative; +} +.api-skill-more summary { + display: grid; + min-height: 32px; + place-items: center; + padding: 0 11px; + border: 1px solid var(--wxex-border); + border-radius: var(--wxex-radius-sm); + background: var(--wxex-bg-elevated); + color: var(--wxex-text-primary); + cursor: pointer; + font: 600 12px/18px var(--wxex-font); + list-style: none; +} +.api-skill-more[open] > button { + position: absolute; + z-index: 2; + top: 36px; + right: 0; + width: 170px; + padding: 9px 10px; + border: 1px solid var(--wxex-border); + border-radius: var(--wxex-radius-sm); + background: #fff; + color: var(--wxex-text-primary); + box-shadow: var(--wxex-shadow-popover); + font: 12px/18px var(--wxex-font); + text-align: left; +} +.api-section-heading { + display: flex; + align-items: baseline; + justify-content: space-between; + gap: 12px; +} +.api-section-heading > span { + color: var(--wxex-text-muted); + font: 12px/18px var(--wxex-font); +} +.api-endpoint-table { + margin-top: 12px; + overflow: hidden; + border: 1px solid var(--wxex-border); + border-radius: var(--wxex-radius-md); + background: var(--wxex-bg-elevated); +} +.api-endpoint-head, +.api-endpoint-row { + display: grid; + grid-template-columns: 72px minmax(128px, 1.1fr) minmax(150px, 1.2fr) 90px; + align-items: center; + gap: 12px; + padding: 11px 14px; +} +.api-endpoint-head { + background: #f1f4f2; + color: var(--wxex-text-secondary); + font: 700 12px/18px var(--wxex-font); +} +.api-endpoint-row { + min-height: 54px; + border-top: 1px solid var(--wxex-border); + color: var(--wxex-text-secondary); + font: 12px/18px var(--wxex-font); +} +.api-endpoint-row.active { + background: #f7faf8; +} +.api-endpoint-row code { + color: var(--wxex-text-primary); + font: + 12px/18px ui-monospace, + monospace; +} +.api-endpoint-row small { + display: block; + color: var(--wxex-text-muted); +} +.api-endpoint-row > span:last-child { + display: flex; + gap: 4px; +} +.api-endpoint-row button { + min-height: 26px; + padding: 0 7px; + font-size: 11px; +} +.api-method { + display: inline-block; + color: var(--wxex-brand); + font: + 700 11px/18px ui-monospace, + monospace; +} +.api-method.post { + color: var(--wxex-ai); +} +.api-request-meta { + display: flex; + align-items: center; + gap: 10px; + margin: 12px 0; + padding: 10px 12px; + border: 1px solid var(--wxex-border); + border-radius: var(--wxex-radius-md); + background: #f5f8f6; + overflow: hidden; +} +.api-request-meta code { + overflow: hidden; + color: var(--wxex-text-primary); + font: + 12px/18px ui-monospace, + monospace; + text-overflow: ellipsis; + white-space: nowrap; +} +.api-param-grid { + display: grid; + grid-template-columns: repeat(2, minmax(0, 1fr)); + gap: 10px; +} +.api-param-grid label, +.api-json-input { + display: grid; + gap: 5px; + color: var(--wxex-text-secondary); + font: 12px/18px var(--wxex-font); +} +.api-param-grid label span b { + margin-left: 5px; + color: var(--wxex-danger); + font-size: 11px; +} +.api-param-grid input, +.api-json-input textarea { + box-sizing: border-box; + width: 100%; + border: 1px solid var(--wxex-border); + border-radius: var(--wxex-radius-sm); + outline: none; + background: #fff; + color: var(--wxex-text-primary); + font: 12px/18px var(--wxex-font); +} +.api-param-grid input { + height: 34px; + padding: 0 9px; +} +.api-json-input { + margin-top: 10px; +} +.api-json-input textarea { + min-height: 180px; + padding: 10px; + resize: vertical; + font-family: ui-monospace, monospace; +} +.api-tester-actions { + justify-content: flex-end; + margin-top: 12px; +} +.api-inline-error { + margin: 10px 0 0; + padding: 8px 10px; + border: 1px solid rgba(200, 90, 90, 0.35); + border-radius: var(--wxex-radius-sm); + background: #fff4f4; + color: var(--wxex-danger); + font: 12px/18px var(--wxex-font); +} +.api-runtime-panel { + border-left: 1px solid var(--wxex-border); + background: var(--wxex-bg-main); + overflow: hidden; +} +.api-runtime-scroll { + height: 100%; + overflow-y: auto; +} +.api-runtime-scroll section { + padding: 18px 16px; + border-bottom: 1px solid var(--wxex-border); +} +.api-runtime-title { + display: flex; + align-items: center; + justify-content: space-between; + gap: 8px; +} +.api-runtime-title > span { + font: 700 12px/18px var(--wxex-font); +} +.api-runtime-title .ready { + color: var(--wxex-success); +} +.api-runtime-title .stopped { + color: var(--wxex-text-muted); +} +.api-runtime-scroll h3 { + margin: 0 0 12px; + color: var(--wxex-text-primary); + font: 700 14px/20px var(--wxex-font); +} +.api-runtime-scroll dl { + margin: 14px 0; +} +.api-runtime-scroll dl div { + display: flex; + justify-content: space-between; + gap: 12px; + padding: 5px 0; + font: 12px/18px var(--wxex-font); +} +.api-runtime-scroll dt { + color: var(--wxex-text-secondary); +} +.api-runtime-scroll dd { + margin: 0; + color: var(--wxex-text-primary); + text-align: right; +} +.warning-text { + color: var(--wxex-warning); +} +.api-runtime-actions { + display: grid; + grid-template-columns: 1fr 1fr; +} +.api-runtime-actions button { + min-height: 30px; + padding: 0 6px; +} +.api-security-warning { + margin: 12px 16px; + padding: 9px 10px; + border-left: 3px solid var(--wxex-warning); + background: #fff8ec; + color: #915d1e; + font: 12px/18px var(--wxex-font); +} +.api-response-summary { + display: flex; + align-items: center; + justify-content: space-between; + gap: 8px; + color: var(--wxex-text-primary); + font: + 12px/18px ui-monospace, + monospace; +} +.api-runtime-scroll pre { + max-height: 180px; + overflow: auto; + margin: 10px 0 8px; + padding: 10px; + border-radius: var(--wxex-radius-sm); + background: #1f2824; + color: #cde7dc; + font: + 11px/16px ui-monospace, + monospace; + white-space: pre-wrap; + word-break: break-word; +} +.api-response-meta, +.api-empty-text { + margin: 0 0 9px; + color: var(--wxex-text-muted); + font: 11px/17px var(--wxex-font); +} +.api-history { + display: grid; + gap: 8px; + margin: 0; + padding: 0; + list-style: none; +} +.api-history li { + display: grid; + grid-template-columns: minmax(0, 1fr) auto; + gap: 2px 8px; + padding-bottom: 8px; + border-bottom: 1px solid var(--wxex-border); +} +.api-history span { + overflow: hidden; + color: var(--wxex-text-primary); + font: + 11px/17px ui-monospace, + monospace; + text-overflow: ellipsis; + white-space: nowrap; +} +.api-history b { + font: 700 11px/17px var(--wxex-font); +} +.api-history small { + grid-column: 1 / -1; + color: var(--wxex-text-muted); + font: 11px/16px var(--wxex-font); +} +.api-privacy { + background: #f5f8f6; +} +.api-privacy p { + margin: 0; + color: var(--wxex-text-secondary); + font: 12px/18px var(--wxex-font); +} +.api-markdown-overlay { + position: absolute; + inset: 0; + z-index: 4; + display: flex; + align-items: stretch; + justify-content: center; + padding: 20px; + background: rgba(32, 39, 36, 0.24); +} +.api-markdown-overlay > div { + display: flex; + width: min(820px, 100%); + flex-direction: column; + min-height: 0; + padding: 14px; + border: 1px solid var(--wxex-border); + border-radius: var(--wxex-radius-lg); + background: #fff; + box-shadow: 0 12px 40px rgba(0, 0, 0, 0.16); +} +.api-markdown-overlay header { + display: flex; + align-items: center; + justify-content: space-between; + gap: 12px; +} +.api-markdown-overlay header > div { + display: flex; + align-items: center; + gap: 8px; +} +.api-markdown-overlay header span { + color: var(--wxex-text-muted); + font: 12px/18px var(--wxex-font); +} +.api-markdown-overlay button { + min-height: 30px; + padding: 0 10px; + border: 1px solid var(--wxex-border); + border-radius: var(--wxex-radius-sm); + background: #fff; + color: var(--wxex-text-primary); + cursor: pointer; +} +.api-markdown-overlay pre, +.skill-markdown-preview { + min-height: 0; + overflow: auto; + margin: 14px 0 0; + white-space: pre-wrap; + color: var(--wxex-text-primary); + font: 12px/19px var(--wxex-font); +} +.skill-markdown-preview h1 { + font-size: 20px; +} +.skill-markdown-preview h2 { + margin-top: 18px; + font-size: 16px; +} +.skill-markdown-preview p { + margin: 6px 0; +} +.skill-markdown-preview li { + margin-left: 18px; +} +@media (max-width: 1100px) { + .api-center-layout { + grid-template-columns: minmax(0, 1fr) 280px; + } + .api-main-scroll { + padding: 18px; + } + .api-header-actions { + max-width: 260px; + justify-content: flex-end; + } + .api-endpoint-head, + .api-endpoint-row { + grid-template-columns: 56px minmax(110px, 1fr) minmax(100px, 1fr) 78px; + gap: 7px; + padding: 10px; + } + .api-flow { + overflow-x: auto; + justify-content: flex-start; + } } -.api-center-layout > * { min-width: 0; min-height: 0; box-sizing: border-box; } -.api-section-heading h2, .api-introduction h2, .api-integrations h2, .api-runtime-title h2 { margin: 0; color: var(--wxex-text-primary); font: 700 17px/24px var(--wxex-font); } -.ready-text { color: var(--wxex-success); } -.api-main { position: relative; display: flex; min-width: 0; min-height: 0; overflow: hidden; background: var(--wxex-bg-main); }.api-main-scroll { flex: 1; min-width: 0; min-height: 0; overflow-y: auto; padding: 22px 28px 36px; } -.api-workspace-heading { display: flex; align-items: flex-start; justify-content: space-between; gap: 16px; padding-bottom: 16px; border-bottom: 1px solid var(--wxex-border); }.api-title-line { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; }.api-title-line h1 { margin: 0; color: var(--wxex-text-primary); font: 700 22px/30px var(--wxex-font); }.api-workspace-heading p { margin: 4px 0 0; color: var(--wxex-text-secondary); font: 13px/19px var(--wxex-font); }.api-skill-status, .api-version { padding: 2px 7px; border-radius: 5px; font: 700 11px/17px var(--wxex-font); }.api-skill-status.ready { color: var(--wxex-brand); background: var(--wxex-brand-soft); }.api-skill-status.error { color: var(--wxex-danger); background: #fff1f1; }.api-version { color: var(--wxex-text-muted); background: var(--wxex-bg-app); } -.api-header-actions, .api-tester-actions, .api-runtime-actions, .api-skill-file-actions { display: flex; flex-wrap: wrap; gap: 8px; }.api-header-actions button, .api-tester-actions button, .api-runtime-actions button, .api-endpoint-row button, .api-response-summary + pre + p + button, .api-skill-file-actions button { min-height: 32px; padding: 0 11px; border: 1px solid var(--wxex-border); border-radius: var(--wxex-radius-sm); background: var(--wxex-bg-elevated); color: var(--wxex-text-primary); cursor: pointer; font: 600 12px/18px var(--wxex-font); }.api-header-actions button:hover, .api-tester-actions button:hover, .api-runtime-actions button:hover, .api-endpoint-row button:hover, .api-skill-file-actions button:hover { border-color: var(--wxex-brand); color: var(--wxex-brand); }.api-primary-button { border-color: var(--wxex-brand) !important; background: var(--wxex-brand) !important; color: #fff !important; }.api-primary-button:hover:not(:disabled), .api-primary-button:focus-visible, .api-primary-button:active { background: var(--wxex-brand-hover) !important; color: #fff !important; }.api-primary-button:disabled { cursor: not-allowed; opacity: .55; } -.api-trust-bar { display: flex; flex-wrap: wrap; align-items: center; gap: 10px; margin-top: 16px; padding: 10px 12px; border: 1px solid var(--wxex-border); border-radius: var(--wxex-radius-md); background: #f5f8f6; color: var(--wxex-text-secondary); font: 12px/18px var(--wxex-font); }.api-trust-bar i { width: 3px; height: 3px; border-radius: 50%; background: var(--wxex-text-muted); }.api-introduction { margin-top: 24px; padding: 18px 20px; border: 1px solid var(--wxex-border); border-radius: var(--wxex-radius-lg); background: var(--wxex-bg-elevated); }.api-introduction p { margin: 10px 0 16px; color: var(--wxex-text-secondary); font: 13px/20px var(--wxex-font); }.api-flow { display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 12px; border: 1px dashed var(--wxex-border); border-radius: var(--wxex-radius-md); color: var(--wxex-text-secondary); font: 12px/18px var(--wxex-font); }.api-flow span, .api-flow strong { padding: 7px 10px; border: 1px solid var(--wxex-border); border-radius: 5px; background: #fff; white-space: nowrap; }.api-flow strong { color: var(--wxex-brand); background: var(--wxex-brand-soft); }.api-flow b { color: var(--wxex-text-muted); font-size: 17px; } -.api-endpoint-catalog, .api-request-tester { margin-top: 26px; } -.skill-install-flow { margin-top: 26px; padding: 18px 20px; border: 1px solid var(--wxex-border); border-radius: var(--wxex-radius-lg); background: var(--wxex-bg-elevated); }.skill-flow-steps { display: grid; gap: 16px; margin-top: 16px; }.skill-flow-steps > section { position: relative; display: grid; grid-template-columns: 26px minmax(0,1fr); gap: 10px; color: var(--wxex-text-muted); }.skill-flow-steps > section:not(:last-child)::after { position: absolute; top: 28px; left: 12px; bottom: -16px; width: 1px; background: var(--wxex-border); content: ''; }.skill-flow-steps > section > b { position: relative; z-index: 1; display: grid; width: 24px; height: 24px; place-items: center; border: 1px solid var(--wxex-border); border-radius: 50%; background: var(--wxex-bg-elevated); font: 700 12px/1 var(--wxex-font); }.skill-flow-steps > section.active > b, .skill-flow-steps > section.done > b { border-color: var(--wxex-brand); background: var(--wxex-brand); color: #fff; }.skill-flow-steps h3 { margin: 1px 0 5px; color: var(--wxex-text-primary); font: 700 14px/20px var(--wxex-font); }.skill-flow-steps p { margin: 3px 0; color: var(--wxex-text-secondary); font: 12px/18px var(--wxex-font); }.skill-flow-actions { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 10px; }.skill-flow-actions button, .api-skill-details button { min-height: 30px; padding: 0 10px; border: 1px solid var(--wxex-border); border-radius: var(--wxex-radius-sm); background: #fff; color: var(--wxex-text-primary); cursor: pointer; font: 600 12px/18px var(--wxex-font); }.skill-flow-actions button:disabled, .api-skill-details button:disabled { cursor: not-allowed; opacity: .55; }.skill-target-selector { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; }.skill-target-selector button { min-height: 30px; padding: 0 10px; border: 1px solid var(--wxex-border); border-radius: var(--wxex-radius-sm); background: #fff; color: var(--wxex-text-secondary); cursor: pointer; font: 600 12px/18px var(--wxex-font); }.skill-target-selector button.active { border-color: var(--wxex-brand); background: var(--wxex-brand-soft); color: var(--wxex-brand); } -.api-skill-details { margin-top: 16px; border: 1px solid var(--wxex-border); border-radius: var(--wxex-radius-md); background: var(--wxex-bg-elevated); }.api-skill-details summary { padding: 12px 14px; color: var(--wxex-text-primary); cursor: pointer; font: 700 13px/18px var(--wxex-font); }.api-skill-details > dl, .api-skill-details > div { margin: 0; padding: 0 14px 14px; }.api-skill-details dl div { display: grid; grid-template-columns: 72px minmax(0,1fr); gap: 8px; padding: 4px 0; color: var(--wxex-text-secondary); font: 12px/18px var(--wxex-font); }.api-skill-details dd { margin: 0; color: var(--wxex-text-primary); } -.api-skill-more { position: relative; }.api-skill-more summary { display: grid; min-height: 32px; place-items: center; padding: 0 11px; border: 1px solid var(--wxex-border); border-radius: var(--wxex-radius-sm); background: var(--wxex-bg-elevated); color: var(--wxex-text-primary); cursor: pointer; font: 600 12px/18px var(--wxex-font); list-style: none; }.api-skill-more[open] > button { position: absolute; z-index: 2; top: 36px; right: 0; width: 170px; padding: 9px 10px; border: 1px solid var(--wxex-border); border-radius: var(--wxex-radius-sm); background: #fff; color: var(--wxex-text-primary); box-shadow: var(--wxex-shadow-popover); font: 12px/18px var(--wxex-font); text-align: left; } -.api-section-heading { display: flex; align-items: baseline; justify-content: space-between; gap: 12px; }.api-section-heading > span { color: var(--wxex-text-muted); font: 12px/18px var(--wxex-font); }.api-endpoint-table { margin-top: 12px; overflow: hidden; border: 1px solid var(--wxex-border); border-radius: var(--wxex-radius-md); background: var(--wxex-bg-elevated); }.api-endpoint-head, .api-endpoint-row { display: grid; grid-template-columns: 72px minmax(128px, 1.1fr) minmax(150px, 1.2fr) 90px; align-items: center; gap: 12px; padding: 11px 14px; }.api-endpoint-head { background: #f1f4f2; color: var(--wxex-text-secondary); font: 700 12px/18px var(--wxex-font); }.api-endpoint-row { min-height: 54px; border-top: 1px solid var(--wxex-border); color: var(--wxex-text-secondary); font: 12px/18px var(--wxex-font); }.api-endpoint-row.active { background: #f7faf8; }.api-endpoint-row code { color: var(--wxex-text-primary); font: 12px/18px ui-monospace, monospace; }.api-endpoint-row small { display: block; color: var(--wxex-text-muted); }.api-endpoint-row > span:last-child { display: flex; gap: 4px; }.api-endpoint-row button { min-height: 26px; padding: 0 7px; font-size: 11px; }.api-method { display: inline-block; color: var(--wxex-brand); font: 700 11px/18px ui-monospace, monospace; }.api-method.post { color: var(--wxex-ai); } -.api-request-meta { display: flex; align-items: center; gap: 10px; margin: 12px 0; padding: 10px 12px; border: 1px solid var(--wxex-border); border-radius: var(--wxex-radius-md); background: #f5f8f6; overflow: hidden; }.api-request-meta code { overflow: hidden; color: var(--wxex-text-primary); font: 12px/18px ui-monospace, monospace; text-overflow: ellipsis; white-space: nowrap; }.api-param-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; }.api-param-grid label, .api-json-input { display: grid; gap: 5px; color: var(--wxex-text-secondary); font: 12px/18px var(--wxex-font); }.api-param-grid label span b { margin-left: 5px; color: var(--wxex-danger); font-size: 11px; }.api-param-grid input, .api-json-input textarea { box-sizing: border-box; width: 100%; border: 1px solid var(--wxex-border); border-radius: var(--wxex-radius-sm); outline: none; background: #fff; color: var(--wxex-text-primary); font: 12px/18px var(--wxex-font); }.api-param-grid input { height: 34px; padding: 0 9px; }.api-json-input { margin-top: 10px; }.api-json-input textarea { min-height: 180px; padding: 10px; resize: vertical; font-family: ui-monospace, monospace; }.api-tester-actions { justify-content: flex-end; margin-top: 12px; }.api-inline-error { margin: 10px 0 0; padding: 8px 10px; border: 1px solid rgba(200,90,90,.35); border-radius: var(--wxex-radius-sm); background: #fff4f4; color: var(--wxex-danger); font: 12px/18px var(--wxex-font); } -.api-runtime-panel { border-left: 1px solid var(--wxex-border); background: var(--wxex-bg-main); overflow: hidden; }.api-runtime-scroll { height: 100%; overflow-y: auto; }.api-runtime-scroll section { padding: 18px 16px; border-bottom: 1px solid var(--wxex-border); }.api-runtime-title { display: flex; align-items: center; justify-content: space-between; gap: 8px; }.api-runtime-title > span { font: 700 12px/18px var(--wxex-font); }.api-runtime-title .ready { color: var(--wxex-success); }.api-runtime-title .stopped { color: var(--wxex-text-muted); }.api-runtime-scroll h3 { margin: 0 0 12px; color: var(--wxex-text-primary); font: 700 14px/20px var(--wxex-font); }.api-runtime-scroll dl { margin: 14px 0; }.api-runtime-scroll dl div { display: flex; justify-content: space-between; gap: 12px; padding: 5px 0; font: 12px/18px var(--wxex-font); }.api-runtime-scroll dt { color: var(--wxex-text-secondary); }.api-runtime-scroll dd { margin: 0; color: var(--wxex-text-primary); text-align: right; }.warning-text { color: var(--wxex-warning); }.api-runtime-actions { display: grid; grid-template-columns: 1fr 1fr; }.api-runtime-actions button { min-height: 30px; padding: 0 6px; }.api-security-warning { margin: 12px 16px; padding: 9px 10px; border-left: 3px solid var(--wxex-warning); background: #fff8ec; color: #915d1e; font: 12px/18px var(--wxex-font); }.api-response-summary { display: flex; align-items: center; justify-content: space-between; gap: 8px; color: var(--wxex-text-primary); font: 12px/18px ui-monospace, monospace; }.api-runtime-scroll pre { max-height: 180px; overflow: auto; margin: 10px 0 8px; padding: 10px; border-radius: var(--wxex-radius-sm); background: #1f2824; color: #cde7dc; font: 11px/16px ui-monospace, monospace; white-space: pre-wrap; word-break: break-word; }.api-response-meta, .api-empty-text { margin: 0 0 9px; color: var(--wxex-text-muted); font: 11px/17px var(--wxex-font); }.api-history { display: grid; gap: 8px; margin: 0; padding: 0; list-style: none; }.api-history li { display: grid; grid-template-columns: minmax(0, 1fr) auto; gap: 2px 8px; padding-bottom: 8px; border-bottom: 1px solid var(--wxex-border); }.api-history span { overflow: hidden; color: var(--wxex-text-primary); font: 11px/17px ui-monospace, monospace; text-overflow: ellipsis; white-space: nowrap; }.api-history b { font: 700 11px/17px var(--wxex-font); }.api-history small { grid-column: 1 / -1; color: var(--wxex-text-muted); font: 11px/16px var(--wxex-font); }.api-privacy { background: #f5f8f6; }.api-privacy p { margin: 0; color: var(--wxex-text-secondary); font: 12px/18px var(--wxex-font); } -.api-markdown-overlay { position: absolute; inset: 0; z-index: 4; display: flex; align-items: stretch; justify-content: center; padding: 20px; background: rgba(32,39,36,.24); }.api-markdown-overlay > div { display: flex; width: min(820px, 100%); flex-direction: column; min-height: 0; padding: 14px; border: 1px solid var(--wxex-border); border-radius: var(--wxex-radius-lg); background: #fff; box-shadow: 0 12px 40px rgba(0,0,0,.16); }.api-markdown-overlay header { display: flex; align-items: center; justify-content: space-between; gap: 12px; }.api-markdown-overlay header > div { display: flex; align-items: center; gap: 8px; }.api-markdown-overlay header span { color: var(--wxex-text-muted); font: 12px/18px var(--wxex-font); }.api-markdown-overlay button { min-height: 30px; padding: 0 10px; border: 1px solid var(--wxex-border); border-radius: var(--wxex-radius-sm); background: #fff; color: var(--wxex-text-primary); cursor: pointer; }.api-markdown-overlay pre, .skill-markdown-preview { min-height: 0; overflow: auto; margin: 14px 0 0; white-space: pre-wrap; color: var(--wxex-text-primary); font: 12px/19px var(--wxex-font); }.skill-markdown-preview h1 { font-size: 20px; }.skill-markdown-preview h2 { margin-top: 18px; font-size: 16px; }.skill-markdown-preview p { margin: 6px 0; }.skill-markdown-preview li { margin-left: 18px; } -@media (max-width: 1100px) { .api-center-layout { grid-template-columns: minmax(0,1fr) 280px; }.api-main-scroll { padding: 18px; }.api-header-actions { max-width: 260px; justify-content: flex-end; }.api-endpoint-head, .api-endpoint-row { grid-template-columns: 56px minmax(110px,1fr) minmax(100px,1fr) 78px; gap: 7px; padding: 10px; }.api-flow { overflow-x: auto; justify-content: flex-start; } } .report-history-list-title { padding: 4px 4px 8px; @@ -4609,3 +7219,51 @@ body { font-weight: 700; cursor: pointer; } + +.settings-auto-login-card { + padding: 18px 20px; +} +.settings-auto-login-card label { + display: flex; + align-items: center; + justify-content: space-between; + gap: 20px; + cursor: pointer; +} +.settings-auto-login-card label > span { + display: grid; + gap: 4px; +} +.settings-auto-login-card b { + color: var(--wxex-text-primary); + font: 700 13px/19px var(--wxex-font); +} +.settings-auto-login-card small { + color: var(--wxex-text-secondary); + font: 12px/18px var(--wxex-font); +} +.settings-auto-login-card input { + width: 18px; + height: 18px; + accent-color: var(--wxex-brand); +} +.api-upload-test-row { + display: flex; + align-items: center; + gap: 10px; + margin: 10px 0; +} +.api-upload-test-row button { + min-height: 32px; + padding: 0 12px; + border: 1px solid var(--wxex-border); + border-radius: var(--wxex-radius-sm); + background: var(--wxex-bg-elevated); + color: var(--wxex-text-primary); + cursor: pointer; + font: 600 12px/18px var(--wxex-font); +} +.api-upload-test-row span { + color: var(--wxex-text-muted); + font: 11px/17px var(--wxex-font); +} diff --git a/src/renderer/src/components/layout/PrimaryNavigation.tsx b/src/renderer/src/components/layout/PrimaryNavigation.tsx index 665868d..5a78200 100644 --- a/src/renderer/src/components/layout/PrimaryNavigation.tsx +++ b/src/renderer/src/components/layout/PrimaryNavigation.tsx @@ -45,6 +45,16 @@ function NavIcon({ page }: NavIconProps): React.ReactElement { ) + case 'agent-hub': + return ( + + ) case 'api': return (
+
+
+
WechatExplorer
+

Agent Hub

+

让微信机器人安全调用聊天数据与 AI 能力。

+
+ + Agent Hub {status.hub === 'online' ? '运行中' : '未运行'} + +
+ +
+
+
+
+ 微信机器人 +

连接微信

+
+ + + {STATUS_LABELS[status.connector]} + +
+ + {showQRCode ? ( +
+
+ 微信机器人登录二维码 +
+
+

+ {status.connector === 'scanned' ? '请在手机上确认登录' : '使用微信扫描二维码'} +

+

二维码仅用于机器人账号登录,不会读取你的微信密码。

+ +
+
+ ) : status.connector === 'online' ? ( +
+
+ ✓ +
+
+

微信机器人已连接

+

{status.accountId || status.wechatUserId || '登录凭据已就绪'}

+
+
+ ) : ( +
+
+ +
+

{status.connector === 'error' ? '连接遇到问题' : '尚未连接微信机器人'}

+

{status.error || '扫码登录后,即可从微信向 Agent Hub 提问。'}

+
+ )} + +
+ {status.connector === 'online' ? ( + <> + + + + ) : !isLoginFlow ? ( + + ) : null} +
+
+ + +
+ +
+
+
+ 故障诊断 +

运行日志

+
+
+ + + +
+
+
+ {visibleLogs.length === 0 ? ( +
暂无运行日志。收到消息后,这里会显示处理到哪一步。
+ ) : ( + visibleLogs.map((entry) => ( +
+ + {LOG_SOURCE_LABELS[entry.source]} + {entry.message} +
+ )) + )} +
+

日志会隐藏 Token 和二维码数据,不记录你的微信密码。

+
+
+ ) +} diff --git a/src/renderer/src/features/api-center/components/ApiRequestTester.tsx b/src/renderer/src/features/api-center/components/ApiRequestTester.tsx index e13657e..f39bf14 100644 --- a/src/renderer/src/features/api-center/components/ApiRequestTester.tsx +++ b/src/renderer/src/features/api-center/components/ApiRequestTester.tsx @@ -40,6 +40,17 @@ export function ApiRequestTester({ void onCopyCurl(command) } const update = (key: string, value: string): void => onParams({ ...params, [key]: value }) + const selectTestImage = async (): Promise => { + const result = await window.api.selectAgentHubTestImage() + if (result.canceled || !result.path) return + let payload: Record = {} + try { + payload = JSON.parse(body) as Record + } catch { + // Replace an invalid draft with a valid send-test request. + } + onBody(JSON.stringify({ ...payload, media_url: result.path }, null, 2)) + } return (
@@ -77,6 +88,14 @@ export function ApiRequestTester({ /> )} + {endpoint.id === 'agent-send' && ( +
+ + 选择后只会填入本地路径;点击“发送请求”才会真正发送。 +
+ )}
diff --git a/src/renderer/src/utils/group-report-facts.ts b/src/renderer/src/utils/group-report-facts.ts index ec4d12f..027e481 100644 --- a/src/renderer/src/utils/group-report-facts.ts +++ b/src/renderer/src/utils/group-report-facts.ts @@ -11,6 +11,14 @@ import { ReportVoiceLeaderboardItem } from '../../../shared/group-report' +declare const window: { + api: { + imageListCandidates: (...args: unknown[]) => Promise + imageAnalyze: (...args: unknown[]) => Promise + getImage: (...args: unknown[]) => Promise + } +} + export interface GroupReportTranscriptRow { id: string datetime: string @@ -184,6 +192,7 @@ const buildMediaSection = async ( warnings: string[] }> => { const warnings: string[] = [] + const rendererApi = typeof window === 'undefined' ? null : window.api const rawImageCandidates = messages .map((message, index) => { if (message.contentData?.type !== 'image') return null @@ -214,6 +223,7 @@ const buildMediaSection = async ( // ============================================================ let visionGallery: ReportVisionGalleryItem[] = [] try { + if (!rendererApi) throw new Error('后台模式不读取 Renderer 图片') const sessionId = messages.find((m) => m.sessionId)?.sessionId || (contact?.md5 ?? '') const startTime = messages.length ? parseTimestamp(messages[0]) : 0 const endTime = messages.length ? parseTimestamp(messages[messages.length - 1]) : 0 @@ -233,7 +243,7 @@ const buildMediaSection = async ( } }) - const candidatesResp = await window.api.imageListCandidates({ + const candidatesResp = await rendererApi.imageListCandidates({ sessionId, startTime, endTime, @@ -249,7 +259,7 @@ const buildMediaSection = async ( if (candidate.insight) return candidate.insight // 未命中:解密图片拿 base64 → 调 AI try { - const img = await window.api.getImage( + const img = await rendererApi.getImage( candidate.md5, candidate.datName, candidate.sessionId @@ -260,7 +270,7 @@ const buildMediaSection = async ( ) return null } - const analyzeResp = await window.api.imageAnalyze({ + const analyzeResp = await rendererApi.imageAnalyze({ imageHash: candidate.imageHash, imageDataUrl: img.data, messageId: candidate.messageId, @@ -306,7 +316,7 @@ const buildMediaSection = async ( const orig = rawImageCandidates.find((c) => c.sourceMessageIds[0] === item.messageId) if (!orig) return item try { - const img = await window.api.getImage(orig.md5, orig.datName, orig.sessionId) + const img = await rendererApi.getImage(orig.md5, orig.datName, orig.sessionId) if (img.success && img.data?.startsWith('data:image/')) { return { ...item, imageUrl: img.data } } @@ -323,9 +333,10 @@ const buildMediaSection = async ( visionGallery = [] } - const imageCandidates = await Promise.all( - rawImageCandidates.map(async (item) => { - const result = await window.api.getImage(item.md5, item.datName, item.sessionId) + const imageCandidates = rendererApi + ? await Promise.all( + rawImageCandidates.map(async (item) => { + const result = await rendererApi.getImage(item.md5, item.datName, item.sessionId) if (!result.success || !result.data?.startsWith('data:image/')) return null return { sender: item.sender, @@ -337,9 +348,10 @@ const buildMediaSection = async ( sourceMessageIds: item.sourceMessageIds, replyCount: item.replyCount, score: item.score - } - }) - ) + } + }) + ) + : [] const gallery: ReportMediaGalleryItem[] = imageCandidates .filter((item): item is NonNullable => Boolean(item)) diff --git a/src/shared/agent-hub.ts b/src/shared/agent-hub.ts new file mode 100644 index 0000000..6b8c826 --- /dev/null +++ b/src/shared/agent-hub.ts @@ -0,0 +1,38 @@ +export type AgentHubRuntimeStatus = 'starting' | 'online' | 'offline' | 'error' +export type WechatConnectorStatus = + | 'checking' + | 'disconnected' + | 'starting' + | 'waiting_scan' + | 'scanned' + | 'online' + | 'error' + +export interface AgentHubStatus { + hub: AgentHubRuntimeStatus + connector: WechatConnectorStatus + qrCodeDataUrl?: string + accountId?: string + wechatUserId?: string + error?: string + updatedAt: number + dataApi?: 'checking' | 'online' | 'offline' + databaseReady?: boolean +} + +export interface AgentHubActionResult { + success: boolean + status: AgentHubStatus + error?: string +} + +export type AgentHubLogSource = 'agent-hub' | 'wechat-connector' | 'system' +export type AgentHubLogLevel = 'info' | 'warn' | 'error' + +export interface AgentHubLogEntry { + id: number + timestamp: number + source: AgentHubLogSource + level: AgentHubLogLevel + message: string +} diff --git a/src/shared/local-api-test.ts b/src/shared/local-api-test.ts index 0f2ffe3..b8a9852 100644 --- a/src/shared/local-api-test.ts +++ b/src/shared/local-api-test.ts @@ -11,7 +11,10 @@ export const LOCAL_API_ENDPOINTS = { }, 'group-snapshot': { method: 'GET', path: '/api/v1/group_snapshot', queryKeys: ['md5'] }, resolve: { method: 'GET', path: '/api/v1/resolve', queryKeys: ['q'] }, - report: { method: 'POST', path: '/api/v1/report', queryKeys: [] } + report: { method: 'POST', path: '/api/v1/report', queryKeys: [] }, + 'agent-status': { method: 'GET', path: '/api/v1/agent/status', queryKeys: [] }, + 'agent-group-report': { method: 'POST', path: '/api/v1/agent/group-report', queryKeys: [] }, + 'agent-send': { method: 'POST', path: '/api/v1/agent/send', queryKeys: [] } } as const export type LocalApiEndpointId = keyof typeof LOCAL_API_ENDPOINTS From eaea8d8435aba4a98a7a32cd4a217a8de4a73314 Mon Sep 17 00:00:00 2001 From: Wxw-Gu Date: Wed, 15 Jul 2026 20:27:05 +0800 Subject: [PATCH 2/3] =?UTF-8?q?feat:=20=E5=86=85=E7=BD=AE=E5=BE=AE?= =?UTF-8?q?=E4=BF=A1=E8=BF=9E=E6=8E=A5=E5=99=A8=E5=B9=B6=E5=AE=8C=E5=96=84?= =?UTF-8?q?=20Agent=20=E6=9F=A5=E8=AF=A2=E8=83=BD=E5=8A=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 15 +- pnpm-lock.yaml | 3 +- scripts/after-pack.cjs | 17 ++ scripts/build-wechat-connector.cjs | 34 ++- services/wechat-connector/LICENSE | 21 ++ services/wechat-connector/README.md | 25 ++ services/wechat-connector/api/server.go | 135 ++++++++++ services/wechat-connector/api/server_test.go | 20 ++ services/wechat-connector/go.mod | 8 + services/wechat-connector/go.sum | 4 + services/wechat-connector/ilink/auth.go | 197 +++++++++++++++ services/wechat-connector/ilink/auth_test.go | 36 +++ services/wechat-connector/ilink/client.go | 218 ++++++++++++++++ services/wechat-connector/ilink/monitor.go | 181 ++++++++++++++ services/wechat-connector/ilink/types.go | 219 +++++++++++++++++ services/wechat-connector/main.go | 200 +++++++++++++++ services/wechat-connector/messaging/cdn.go | 232 ++++++++++++++++++ .../messaging/inbound_webhook.go | 121 +++++++++ .../messaging/inbound_webhook_test.go | 75 ++++++ .../wechat-connector/messaging/markdown.go | 103 ++++++++ services/wechat-connector/messaging/media.go | 221 +++++++++++++++++ .../wechat-connector/messaging/media_test.go | 73 ++++++ services/wechat-connector/messaging/sender.go | 86 +++++++ src/main/services/agent-hub-service.ts | 76 +++++- .../features/agent-hub/AgentHubWorkspace.tsx | 10 +- src/renderer/src/utils/group-report-facts.ts | 48 ++-- 26 files changed, 2340 insertions(+), 38 deletions(-) create mode 100644 services/wechat-connector/LICENSE create mode 100644 services/wechat-connector/README.md create mode 100644 services/wechat-connector/api/server.go create mode 100644 services/wechat-connector/api/server_test.go create mode 100644 services/wechat-connector/go.mod create mode 100644 services/wechat-connector/go.sum create mode 100644 services/wechat-connector/ilink/auth.go create mode 100644 services/wechat-connector/ilink/auth_test.go create mode 100644 services/wechat-connector/ilink/client.go create mode 100644 services/wechat-connector/ilink/monitor.go create mode 100644 services/wechat-connector/ilink/types.go create mode 100644 services/wechat-connector/main.go create mode 100644 services/wechat-connector/messaging/cdn.go create mode 100644 services/wechat-connector/messaging/inbound_webhook.go create mode 100644 services/wechat-connector/messaging/inbound_webhook_test.go create mode 100644 services/wechat-connector/messaging/markdown.go create mode 100644 services/wechat-connector/messaging/media.go create mode 100644 services/wechat-connector/messaging/media_test.go create mode 100644 services/wechat-connector/messaging/sender.go diff --git a/package.json b/package.json index dd0002c..3c1e3bb 100644 --- a/package.json +++ b/package.json @@ -25,9 +25,9 @@ "test:skill-install": "node scripts/test-skill-install-instruction.cjs", "cp:env": "node scripts/ensure-env.cjs", "prepare:env": "node scripts/ensure-env.cjs", - "predev": "node scripts/ensure-env.cjs && node scripts/build-wechat-connector.cjs", "start": "electron-vite preview", - "dev": "electron-vite dev", + "dev": "node scripts/ensure-env.cjs && node scripts/build-wechat-connector.cjs && electron-vite dev", + "test:wechat-connector": "go -C services/wechat-connector test ./... && go -C services/wechat-connector vet ./...", "build:wechat-connector": "node scripts/build-wechat-connector.cjs", "build:wechat-connector:win": "node scripts/build-wechat-connector.cjs --platform win32 --arch x64,arm64", "build:wechat-connector:mac": "node scripts/build-wechat-connector.cjs --platform darwin --arch x64,arm64", @@ -42,6 +42,7 @@ "build:linux": "electron-vite build && electron-builder --config electron-builder.yml --linux" }, "dependencies": { + "@koromix/koffi-win32-x64": "3.1.0", "@electron-toolkit/preload": "^3.0.2", "@electron-toolkit/utils": "^4.0.0", "fs-extra": "^11.3.2", @@ -75,6 +76,16 @@ "vite": "^7.2.6" }, "pnpm": { + "supportedArchitectures": { + "os": [ + "current", + "win32" + ], + "cpu": [ + "current", + "x64" + ] + }, "onlyBuiltDependencies": [ "electron", "esbuild" diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 040b4e5..595d1a3 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -10,6 +10,7 @@ specifiers: '@electron-toolkit/preload': ^3.0.2 '@electron-toolkit/tsconfig': ^2.0.0 '@electron-toolkit/utils': ^4.0.0 + '@koromix/koffi-win32-x64': 3.1.0 '@rollup/rollup-darwin-arm64': ^4.62.2 '@types/fs-extra': ^11.0.4 '@types/node': ^22.19.1 @@ -38,6 +39,7 @@ specifiers: dependencies: '@electron-toolkit/preload': 3.0.2_electron@43.1.0 '@electron-toolkit/utils': 4.0.0_electron@43.1.0 + '@koromix/koffi-win32-x64': 3.1.0 fs-extra: 11.3.2 fzstd: 0.1.1 koffi: 3.1.0 @@ -887,7 +889,6 @@ packages: cpu: [x64] os: [win32] dev: false - optional: true /@malept/cross-spawn-promise/2.0.0: resolution: {integrity: sha512-1DpKU0Z5ThltBwjNySMC14g0CkbyhCaz9FkhxqNsZI6uAPJXFS8cMXlBKo26FJ8ZuW6S9GCMcR9IO5k2X5/9Fg==} diff --git a/scripts/after-pack.cjs b/scripts/after-pack.cjs index 13679d4..83ae541 100644 --- a/scripts/after-pack.cjs +++ b/scripts/after-pack.cjs @@ -10,6 +10,23 @@ function setPlistValue(plistPath, key, value) { } exports.default = async function afterPack(context) { + if (context.electronPlatformName === 'win32') { + const koffiNative = path.join( + context.appOutDir, + 'resources', + 'app.asar.unpacked', + 'node_modules', + '@koromix', + 'koffi-win32-x64', + 'win32_x64', + 'koffi.node' + ) + if (!existsSync(koffiNative)) { + throw new Error(`Missing Windows Koffi native module: ${koffiNative}`) + } + return + } + if (context.electronPlatformName !== 'darwin') return const productName = context.packager.appInfo.productFilename diff --git a/scripts/build-wechat-connector.cjs b/scripts/build-wechat-connector.cjs index 41f0f3a..7dce35b 100644 --- a/scripts/build-wechat-connector.cjs +++ b/scripts/build-wechat-connector.cjs @@ -4,9 +4,7 @@ const fs = require('node:fs') const path = require('node:path') const projectRoot = path.resolve(__dirname, '..') -const sourceDir = process.env.WECHAT_CONNECTOR_SOURCE - ? path.resolve(process.env.WECHAT_CONNECTOR_SOURCE) - : path.join(projectRoot, 'services', 'wechat-connector') +const sourceDir = path.join(projectRoot, 'services', 'wechat-connector') const outputRoot = path.join(projectRoot, 'resources', 'connectors', 'wechat') function normalizePlatform(value) { @@ -22,33 +20,32 @@ function normalizeArch(value) { throw new Error(`Unsupported connector architecture: ${value}`) } +function detectHostArch() { + if (process.platform !== 'darwin') return process.arch + try { + const arm64Supported = execFileSync('sysctl', ['-n', 'hw.optional.arm64'], { + encoding: 'utf8' + }).trim() + return arm64Supported === '1' ? 'arm64' : process.arch + } catch { + return process.arch + } +} + function parseTargets() { const platformArg = process.argv.indexOf('--platform') const archArg = process.argv.indexOf('--arch') const platforms = platformArg >= 0 ? process.argv[platformArg + 1].split(',') : [process.platform] - const arches = archArg >= 0 ? process.argv[archArg + 1].split(',') : [process.arch] + const arches = archArg >= 0 ? process.argv[archArg + 1].split(',') : [detectHostArch()] return platforms.flatMap((platform) => arches.map((arch) => ({ goos: normalizePlatform(platform), goarch: normalizeArch(arch) })) ) } if (!fs.existsSync(path.join(sourceDir, 'go.mod'))) { - const existingTargets = parseTargets().every((target) => { - const directoryName = `${target.goos === 'windows' ? 'win32' : target.goos}-${target.goarch === 'amd64' ? 'x64' : target.goarch}` - const executable = target.goos === 'windows' ? 'wechat-connector.exe' : 'wechat-connector' - return fs.existsSync(path.join(outputRoot, directoryName, executable)) - }) - if (existingTargets) { - console.log('[build-wechat-connector] source not configured; reusing existing binary') - process.exit(0) - } - throw new Error( - 'Wechat connector source is not included in this repository. Set WECHAT_CONNECTOR_SOURCE to a compatible connector checkout.' - ) + throw new Error(`Repository-local WeChat connector source is missing: ${sourceDir}`) } -fs.rmSync(outputRoot, { recursive: true, force: true }) - for (const target of parseTargets()) { const directoryName = `${target.goos === 'windows' ? 'win32' : target.goos}-${target.goarch === 'amd64' ? 'x64' : target.goarch}` const outputDir = path.join(outputRoot, directoryName) @@ -56,6 +53,7 @@ for (const target of parseTargets()) { outputDir, target.goos === 'windows' ? 'wechat-connector.exe' : 'wechat-connector' ) + fs.rmSync(outputDir, { recursive: true, force: true }) fs.mkdirSync(outputDir, { recursive: true }) execFileSync('go', ['build', '-trimpath', '-o', outputPath, '.'], { cwd: sourceDir, diff --git a/services/wechat-connector/LICENSE b/services/wechat-connector/LICENSE new file mode 100644 index 0000000..94250e8 --- /dev/null +++ b/services/wechat-connector/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2026 fastclaw-ai + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/services/wechat-connector/README.md b/services/wechat-connector/README.md new file mode 100644 index 0000000..8329350 --- /dev/null +++ b/services/wechat-connector/README.md @@ -0,0 +1,25 @@ +# WechatExplorer WeChat Connector + +This repository-local service provides the minimal WeChat bridge required by WechatExplorer: + +- QR-code login with a single persisted credential +- account discovery +- inbound long polling and authenticated webhook delivery +- local HTTP health and send endpoints +- text and local/remote media sending + +The executable is managed by the Electron main process. It is not a general-purpose agent runtime and does not load external AI command-line tools. + +## Commands + +```bash +go run . login --json +go run . accounts --json +go run . start --foreground --api-addr 127.0.0.1:18011 --account-id +``` + +Credential and synchronization state is stored under `~/.wechatexplorer/wechat-connector/accounts`. A successful login is written before the older credential and synchronization state are removed, so an incomplete login cannot destroy the last working credential. + +## Attribution + +Low-level protocol and media transport portions are distributed under the MIT license in [LICENSE](LICENSE). WechatExplorer-specific process management, webhook contract, product UI, and Agent Hub behavior live in the surrounding WechatExplorer project. diff --git a/services/wechat-connector/api/server.go b/services/wechat-connector/api/server.go new file mode 100644 index 0000000..5ee86ae --- /dev/null +++ b/services/wechat-connector/api/server.go @@ -0,0 +1,135 @@ +package api + +import ( + "context" + "encoding/json" + "fmt" + "log" + "net/http" + + "github.com/Wxw-Gu/WechatExplorer/services/wechat-connector/ilink" + "github.com/Wxw-Gu/WechatExplorer/services/wechat-connector/messaging" +) + +// Server provides an HTTP API for sending messages. +type Server struct { + clients []*ilink.Client + addr string +} + +// NewServer creates an API server. +func NewServer(clients []*ilink.Client, addr string) *Server { + if addr == "" { + addr = "127.0.0.1:18011" + } + return &Server{clients: clients, addr: addr} +} + +// SendRequest is the JSON body for POST /api/send. +type SendRequest struct { + AccountID string `json:"account_id,omitempty"` + To string `json:"to"` + Text string `json:"text,omitempty"` + MediaURL string `json:"media_url,omitempty"` // image/video/file URL +} + +// Run starts the HTTP server. Blocks until ctx is cancelled. +func (s *Server) Run(ctx context.Context) error { + mux := http.NewServeMux() + mux.HandleFunc("/api/send", s.handleSend) + mux.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusOK) + fmt.Fprintln(w, "ok") + }) + + srv := &http.Server{Addr: s.addr, Handler: mux} + + go func() { + <-ctx.Done() + srv.Shutdown(context.Background()) + }() + + log.Printf("[api] listening on %s", s.addr) + if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed { + return err + } + return nil +} + +func (s *Server) handleSend(w http.ResponseWriter, r *http.Request) { + if r.Method != http.MethodPost { + http.Error(w, "POST only", http.StatusMethodNotAllowed) + return + } + + var req SendRequest + if err := json.NewDecoder(r.Body).Decode(&req); err != nil { + http.Error(w, "invalid JSON: "+err.Error(), http.StatusBadRequest) + return + } + + if req.To == "" { + http.Error(w, `"to" is required`, http.StatusBadRequest) + return + } + if req.Text == "" && req.MediaURL == "" { + http.Error(w, `"text" or "media_url" is required`, http.StatusBadRequest) + return + } + + if len(s.clients) == 0 { + http.Error(w, "no accounts configured", http.StatusServiceUnavailable) + return + } + + client := s.clientForAccount(req.AccountID) + if client == nil { + http.Error(w, "requested account is not available", http.StatusNotFound) + return + } + ctx := r.Context() + + // Send text if provided + if req.Text != "" { + if err := messaging.SendTextReply(ctx, client, req.To, req.Text, "", ""); err != nil { + log.Printf("[api] send text failed: %v", err) + http.Error(w, "send text failed: "+err.Error(), http.StatusInternalServerError) + return + } + log.Printf("[api] sent text to %s: %q", req.To, req.Text) + + // Extract and send any markdown images embedded in text + for _, imgURL := range messaging.ExtractImageURLs(req.Text) { + if err := messaging.SendMediaFromURL(ctx, client, req.To, imgURL, ""); err != nil { + log.Printf("[api] send extracted image failed: %v", err) + } else { + log.Printf("[api] sent extracted image to %s: %s", req.To, imgURL) + } + } + } + + // Send media if provided + if req.MediaURL != "" { + if err := messaging.SendMediaFromURL(ctx, client, req.To, req.MediaURL, ""); err != nil { + log.Printf("[api] send media failed: %v", err) + http.Error(w, "send media failed: "+err.Error(), http.StatusInternalServerError) + return + } + log.Printf("[api] sent media to %s: %s", req.To, req.MediaURL) + } + + w.Header().Set("Content-Type", "application/json") + json.NewEncoder(w).Encode(map[string]string{"status": "ok"}) +} + +func (s *Server) clientForAccount(accountID string) *ilink.Client { + if accountID == "" { + return s.clients[0] + } + for _, client := range s.clients { + if client.BotID() == accountID { + return client + } + } + return nil +} diff --git a/services/wechat-connector/api/server_test.go b/services/wechat-connector/api/server_test.go new file mode 100644 index 0000000..0c66680 --- /dev/null +++ b/services/wechat-connector/api/server_test.go @@ -0,0 +1,20 @@ +package api + +import ( + "testing" + + "github.com/Wxw-Gu/WechatExplorer/services/wechat-connector/ilink" +) + +func TestClientForAccountSelectsMatchingBot(t *testing.T) { + oldClient := ilink.NewClient(&ilink.Credentials{ILinkBotID: "bot-old"}) + newClient := ilink.NewClient(&ilink.Credentials{ILinkBotID: "bot-new"}) + server := NewServer([]*ilink.Client{oldClient, newClient}, "") + + if got := server.clientForAccount("bot-new"); got != newClient { + t.Fatal("clientForAccount did not select the requested account") + } + if got := server.clientForAccount("missing"); got != nil { + t.Fatal("clientForAccount should reject an unknown account") + } +} diff --git a/services/wechat-connector/go.mod b/services/wechat-connector/go.mod new file mode 100644 index 0000000..c59810d --- /dev/null +++ b/services/wechat-connector/go.mod @@ -0,0 +1,8 @@ +module github.com/Wxw-Gu/WechatExplorer/services/wechat-connector + +go 1.23.0 + +require ( + github.com/google/uuid v1.6.0 + rsc.io/qr v0.2.0 +) diff --git a/services/wechat-connector/go.sum b/services/wechat-connector/go.sum new file mode 100644 index 0000000..23dfdfa --- /dev/null +++ b/services/wechat-connector/go.sum @@ -0,0 +1,4 @@ +github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= +github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +rsc.io/qr v0.2.0 h1:6vBLea5/NRMVTz8V66gipeLycZMl/+UlFmk8DvqQ6WY= +rsc.io/qr v0.2.0/go.mod h1:IF+uZjkb9fqyeF/4tlBoynqmQxUoPfWEKh921coOuXs= diff --git a/services/wechat-connector/ilink/auth.go b/services/wechat-connector/ilink/auth.go new file mode 100644 index 0000000..b8cad41 --- /dev/null +++ b/services/wechat-connector/ilink/auth.go @@ -0,0 +1,197 @@ +package ilink + +import ( + "context" + "encoding/json" + "fmt" + "os" + "path/filepath" + "strings" + "time" +) + +const ( + qrCodeURL = "https://ilinkai.weixin.qq.com/ilink/bot/get_bot_qrcode?bot_type=3" + qrStatusURL = "https://ilinkai.weixin.qq.com/ilink/bot/get_qrcode_status?qrcode=" + statusWait = "wait" + statusScanned = "scaned" + statusConfirmed = "confirmed" + statusExpired = "expired" +) + +// FetchQRCode retrieves a new QR code for login. +func FetchQRCode(ctx context.Context) (*QRCodeResponse, error) { + c := NewUnauthenticatedClient() + var resp QRCodeResponse + if err := c.doGet(ctx, qrCodeURL, &resp); err != nil { + return nil, fmt.Errorf("fetch QR code: %w", err) + } + return &resp, nil +} + +// PollQRStatus polls for QR code scan status until confirmed or expired. +// It calls onStatus for each status change so the caller can display progress. +func PollQRStatus(ctx context.Context, qrcode string, onStatus func(status string)) (*Credentials, error) { + c := NewUnauthenticatedClient() + url := qrStatusURL + qrcode + + for { + select { + case <-ctx.Done(): + return nil, ctx.Err() + default: + } + + pollCtx, cancel := context.WithTimeout(ctx, 40*time.Second) + var resp QRStatusResponse + err := c.doGet(pollCtx, url, &resp) + cancel() + + if err != nil { + // Timeout is normal for long-poll, retry + if ctx.Err() != nil { + return nil, ctx.Err() + } + continue + } + + if onStatus != nil { + onStatus(resp.Status) + } + + switch resp.Status { + case statusConfirmed: + creds := &Credentials{ + BotToken: resp.BotToken, + ILinkBotID: resp.ILinkBotID, + BaseURL: resp.BaseURL, + ILinkUserID: resp.ILinkUserID, + } + return creds, nil + case statusExpired: + return nil, fmt.Errorf("QR code expired") + case statusWait, statusScanned: + // Continue polling + default: + // Unknown status, continue + } + } +} + +// AccountsDir returns the directory where account credentials are stored. +func AccountsDir() (string, error) { + home, err := os.UserHomeDir() + if err != nil { + return "", err + } + return filepath.Join(home, ".wechatexplorer", "wechat-connector", "accounts"), nil +} + +// NormalizeAccountID converts raw bot ID to filesystem-safe format. +func NormalizeAccountID(raw string) string { + s := raw + for _, ch := range []string{"@", ".", ":"} { + s = filepath.Clean(s) + s = replaceAll(s, ch, "-") + } + return s +} + +func replaceAll(s, old, new string) string { + for { + i := indexOf(s, old) + if i < 0 { + return s + } + s = s[:i] + new + s[i+len(old):] + } +} + +func indexOf(s, sub string) int { + for i := range s { + if i+len(sub) <= len(s) && s[i:i+len(sub)] == sub { + return i + } + } + return -1 +} + +// SaveCredentials saves the latest credentials and removes older accounts. +// The new credential is written first so a failed login never destroys the +// previously working credential. +func SaveCredentials(creds *Credentials) error { + dir, err := AccountsDir() + if err != nil { + return err + } + if err := os.MkdirAll(dir, 0o700); err != nil { + return fmt.Errorf("create accounts dir: %w", err) + } + + id := NormalizeAccountID(creds.ILinkBotID) + path := filepath.Join(dir, id+".json") + + data, err := json.MarshalIndent(creds, "", " ") + if err != nil { + return fmt.Errorf("marshal credentials: %w", err) + } + + if err := os.WriteFile(path, data, 0o600); err != nil { + return fmt.Errorf("write credentials: %w", err) + } + + entries, err := os.ReadDir(dir) + if err != nil { + return fmt.Errorf("prune old credentials: %w", err) + } + keepPrefix := id + "." + for _, entry := range entries { + if entry.IsDir() || strings.HasPrefix(entry.Name(), keepPrefix) { + continue + } + if filepath.Ext(entry.Name()) != ".json" { + continue + } + if err := os.Remove(filepath.Join(dir, entry.Name())); err != nil && !os.IsNotExist(err) { + return fmt.Errorf("remove old credential %s: %w", entry.Name(), err) + } + } + return nil +} + +// LoadAllCredentials loads all saved account credentials. +func LoadAllCredentials() ([]*Credentials, error) { + dir, err := AccountsDir() + if err != nil { + return nil, err + } + + entries, err := os.ReadDir(dir) + if err != nil { + if os.IsNotExist(err) { + return nil, nil + } + return nil, fmt.Errorf("read accounts dir: %w", err) + } + + var result []*Credentials + for _, e := range entries { + if e.IsDir() || filepath.Ext(e.Name()) != ".json" { + continue + } + data, err := os.ReadFile(filepath.Join(dir, e.Name())) + if err != nil { + continue + } + var creds Credentials + if json.Unmarshal(data, &creds) == nil && creds.BotToken != "" { + result = append(result, &creds) + } + } + return result, nil +} + +// CredentialsPath returns the path for display purposes. +func CredentialsPath() (string, error) { + return AccountsDir() +} diff --git a/services/wechat-connector/ilink/auth_test.go b/services/wechat-connector/ilink/auth_test.go new file mode 100644 index 0000000..fdfa6fa --- /dev/null +++ b/services/wechat-connector/ilink/auth_test.go @@ -0,0 +1,36 @@ +package ilink + +import ( + "os" + "path/filepath" + "testing" +) + +func TestSaveCredentialsKeepsOnlyLatestAccount(t *testing.T) { + t.Setenv("HOME", t.TempDir()) + old := &Credentials{ILinkBotID: "bot-old@im.bot", BotToken: "old-token"} + latest := &Credentials{ILinkBotID: "bot-new@im.bot", BotToken: "new-token"} + if err := SaveCredentials(old); err != nil { + t.Fatal(err) + } + dir, err := AccountsDir() + if err != nil { + t.Fatal(err) + } + if err := os.WriteFile(filepath.Join(dir, NormalizeAccountID(old.ILinkBotID)+".sync.json"), []byte(`{}`), 0o600); err != nil { + t.Fatal(err) + } + if err := SaveCredentials(latest); err != nil { + t.Fatal(err) + } + accounts, err := LoadAllCredentials() + if err != nil { + t.Fatal(err) + } + if len(accounts) != 1 || accounts[0].ILinkBotID != latest.ILinkBotID { + t.Fatalf("accounts = %#v", accounts) + } + if _, err := os.Stat(filepath.Join(dir, NormalizeAccountID(old.ILinkBotID)+".sync.json")); !os.IsNotExist(err) { + t.Fatalf("old sync state still exists: %v", err) + } +} diff --git a/services/wechat-connector/ilink/client.go b/services/wechat-connector/ilink/client.go new file mode 100644 index 0000000..3697d1b --- /dev/null +++ b/services/wechat-connector/ilink/client.go @@ -0,0 +1,218 @@ +package ilink + +import ( + "bytes" + "context" + "crypto/rand" + "encoding/base64" + "encoding/binary" + "encoding/json" + "fmt" + "io" + "net/http" + "time" +) + +const ( + defaultBaseURL = "https://ilinkai.weixin.qq.com" + longPollTimeout = 35 * time.Second + sendTimeout = 15 * time.Second +) + +// Client is an iLink HTTP API client. +type Client struct { + baseURL string + botToken string + botID string + httpClient *http.Client + wechatUIN string +} + +// NewClient creates a new iLink API client. +func NewClient(creds *Credentials) *Client { + baseURL := creds.BaseURL + if baseURL == "" { + baseURL = defaultBaseURL + } + return &Client{ + baseURL: baseURL, + botToken: creds.BotToken, + botID: creds.ILinkBotID, + httpClient: &http.Client{}, + wechatUIN: generateWechatUIN(), + } +} + +// NewUnauthenticatedClient creates a client without credentials for login flow. +func NewUnauthenticatedClient() *Client { + return &Client{ + baseURL: defaultBaseURL, + httpClient: &http.Client{Timeout: 40 * time.Second}, + wechatUIN: generateWechatUIN(), + } +} + +// BotID returns the bot's user ID. +func (c *Client) BotID() string { + return c.botID +} + +// GetUpdates performs a long-poll for new messages. +func (c *Client) GetUpdates(ctx context.Context, buf string) (*GetUpdatesResponse, error) { + reqBody := GetUpdatesRequest{ + GetUpdatesBuf: buf, + BaseInfo: BaseInfo{ChannelVersion: "1.0.0"}, + } + + ctx, cancel := context.WithTimeout(ctx, longPollTimeout+5*time.Second) + defer cancel() + + var resp GetUpdatesResponse + if err := c.doPost(ctx, "/ilink/bot/getupdates", reqBody, &resp); err != nil { + return nil, err + } + return &resp, nil +} + +// SendMessage sends a message through iLink. +func (c *Client) SendMessage(ctx context.Context, msg *SendMessageRequest) (*SendMessageResponse, error) { + ctx, cancel := context.WithTimeout(ctx, sendTimeout) + defer cancel() + + var resp SendMessageResponse + if err := c.doPost(ctx, "/ilink/bot/sendmessage", msg, &resp); err != nil { + return nil, err + } + return &resp, nil +} + +// GetConfig fetches bot config for a user (includes typing_ticket). +func (c *Client) GetConfig(ctx context.Context, userID, contextToken string) (*GetConfigResponse, error) { + ctx, cancel := context.WithTimeout(ctx, 10*time.Second) + defer cancel() + + req := GetConfigRequest{ + ILinkUserID: userID, + ContextToken: contextToken, + BaseInfo: BaseInfo{}, + } + + var resp GetConfigResponse + if err := c.doPost(ctx, "/ilink/bot/getconfig", req, &resp); err != nil { + return nil, err + } + return &resp, nil +} + +// SendTyping sends a typing indicator to a user. +func (c *Client) SendTyping(ctx context.Context, userID, typingTicket string, status int) error { + ctx, cancel := context.WithTimeout(ctx, 10*time.Second) + defer cancel() + + req := SendTypingRequest{ + ILinkUserID: userID, + TypingTicket: typingTicket, + Status: status, + BaseInfo: BaseInfo{}, + } + + var resp SendTypingResponse + if err := c.doPost(ctx, "/ilink/bot/sendtyping", req, &resp); err != nil { + return err + } + if resp.Ret != 0 { + return fmt.Errorf("sendtyping failed: ret=%d errmsg=%s", resp.Ret, resp.ErrMsg) + } + return nil +} + +// GetUploadURL gets a pre-signed CDN upload URL for media files. +func (c *Client) GetUploadURL(ctx context.Context, req *GetUploadURLRequest) (*GetUploadURLResponse, error) { + ctx, cancel := context.WithTimeout(ctx, sendTimeout) + defer cancel() + + var resp GetUploadURLResponse + if err := c.doPost(ctx, "/ilink/bot/getuploadurl", req, &resp); err != nil { + return nil, err + } + return &resp, nil +} + +// BaseURL returns the base URL for CDN operations. +func (c *Client) BaseURL() string { + return c.baseURL +} + +func (c *Client) doPost(ctx context.Context, path string, body interface{}, result interface{}) error { + data, err := json.Marshal(body) + if err != nil { + return fmt.Errorf("marshal request: %w", err) + } + + req, err := http.NewRequestWithContext(ctx, http.MethodPost, c.baseURL+path, bytes.NewReader(data)) + if err != nil { + return fmt.Errorf("create request: %w", err) + } + c.setHeaders(req) + + resp, err := c.httpClient.Do(req) + if err != nil { + return err + } + defer resp.Body.Close() + + respBody, err := io.ReadAll(resp.Body) + if err != nil { + return fmt.Errorf("read response: %w", err) + } + + if resp.StatusCode != http.StatusOK { + return fmt.Errorf("HTTP %d: %s", resp.StatusCode, string(respBody)) + } + + if err := json.Unmarshal(respBody, result); err != nil { + return fmt.Errorf("unmarshal response: %w", err) + } + return nil +} + +func (c *Client) doGet(ctx context.Context, url string, result interface{}) error { + req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil) + if err != nil { + return fmt.Errorf("create request: %w", err) + } + + resp, err := c.httpClient.Do(req) + if err != nil { + return err + } + defer resp.Body.Close() + + respBody, err := io.ReadAll(resp.Body) + if err != nil { + return fmt.Errorf("read response: %w", err) + } + + if resp.StatusCode != http.StatusOK { + return fmt.Errorf("HTTP %d: %s", resp.StatusCode, string(respBody)) + } + + if err := json.Unmarshal(respBody, result); err != nil { + return fmt.Errorf("unmarshal response: %w", err) + } + return nil +} + +func (c *Client) setHeaders(req *http.Request) { + req.Header.Set("Content-Type", "application/json") + req.Header.Set("AuthorizationType", "ilink_bot_token") + req.Header.Set("Authorization", "Bearer "+c.botToken) + req.Header.Set("X-WECHAT-UIN", c.wechatUIN) +} + +func generateWechatUIN() string { + var n uint32 + _ = binary.Read(rand.Reader, binary.LittleEndian, &n) + s := fmt.Sprintf("%d", n) + return base64.StdEncoding.EncodeToString([]byte(s)) +} diff --git a/services/wechat-connector/ilink/monitor.go b/services/wechat-connector/ilink/monitor.go new file mode 100644 index 0000000..0a775cd --- /dev/null +++ b/services/wechat-connector/ilink/monitor.go @@ -0,0 +1,181 @@ +package ilink + +import ( + "context" + "encoding/json" + "fmt" + "log" + "os" + "path/filepath" + "time" +) + +const ( + maxConsecutiveFailures = 5 + initialBackoff = 3 * time.Second + maxBackoff = 60 * time.Second + sessionExpiredBackoff = 5 * time.Second + errCodeSessionExpired = -14 +) + +// MessageHandler is called for each received message. +type MessageHandler func(ctx context.Context, client *Client, msg WeixinMessage) + +// Monitor manages the long-poll loop for receiving messages. +type Monitor struct { + client *Client + handler MessageHandler + getUpdatesBuf string + bufPath string + failures int + lastActivity time.Time +} + +// NewMonitor creates a new long-poll monitor. +func NewMonitor(client *Client, handler MessageHandler) (*Monitor, error) { + home, err := os.UserHomeDir() + if err != nil { + return nil, err + } + accountID := NormalizeAccountID(client.BotID()) + bufPath := filepath.Join(home, ".wechatexplorer", "wechat-connector", "accounts", accountID+".sync.json") + + m := &Monitor{ + client: client, + handler: handler, + bufPath: bufPath, + lastActivity: time.Now(), + } + m.loadBuf() + return m, nil +} + +// Run starts the long-poll loop. It blocks until ctx is cancelled. +// Automatically recovers from errors with exponential backoff. +func (m *Monitor) Run(ctx context.Context) error { + log.Println("[monitor] starting long-poll loop") + + for { + select { + case <-ctx.Done(): + log.Println("[monitor] shutting down") + return ctx.Err() + default: + } + + resp, err := m.client.GetUpdates(ctx, m.getUpdatesBuf) + if err != nil { + if ctx.Err() != nil { + return ctx.Err() + } + m.failures++ + backoff := m.calcBackoff() + log.Printf("[monitor] GetUpdates error (%d/%d, backoff=%s): %v", + m.failures, maxConsecutiveFailures, backoff, err) + if m.failures == maxConsecutiveFailures { + log.Printf("[monitor] WARNING: %d consecutive failures; reconnect from WechatExplorer if this persists.", maxConsecutiveFailures) + } + select { + case <-time.After(backoff): + case <-ctx.Done(): + return ctx.Err() + } + continue + } + + // Reset failure counter on any successful response + m.failures = 0 + m.lastActivity = time.Now() + + // Session expired — reset sync buf and reconnect silently + if resp.ErrCode == errCodeSessionExpired { + if m.getUpdatesBuf != "" { + log.Printf("[monitor] session expired, resetting sync buf") + m.getUpdatesBuf = "" + m.saveBuf() + } else { + // Sync buf already empty but still getting session expired: + // the bot token itself has expired. The user needs to re-login. + log.Printf("[monitor] WARNING: WeChat session expired and cannot be auto-recovered; reconnect from WechatExplorer.") + } + select { + case <-time.After(sessionExpiredBackoff): + case <-ctx.Done(): + return ctx.Err() + } + continue + } + + // Other server errors + if resp.Ret != 0 && resp.ErrCode != 0 { + log.Printf("[monitor] server error: ret=%d errcode=%d errmsg=%s", resp.Ret, resp.ErrCode, resp.ErrMsg) + continue + } + + // Update buf for next poll + if resp.GetUpdatesBuf != "" { + m.getUpdatesBuf = resp.GetUpdatesBuf + m.saveBuf() + } + + // Process messages concurrently — don't block the poll loop + for _, msg := range resp.Msgs { + go m.handler(ctx, m.client, msg) + } + } +} + +// calcBackoff returns an exponential backoff duration capped at maxBackoff. +func (m *Monitor) calcBackoff() time.Duration { + d := initialBackoff + for i := 1; i < m.failures; i++ { + d *= 2 + if d > maxBackoff { + return maxBackoff + } + } + return d +} + +type syncData struct { + GetUpdatesBuf string `json:"get_updates_buf"` +} + +func (m *Monitor) loadBuf() { + data, err := os.ReadFile(m.bufPath) + if err != nil { + return + } + var s syncData + if json.Unmarshal(data, &s) == nil && s.GetUpdatesBuf != "" { + m.getUpdatesBuf = s.GetUpdatesBuf + log.Printf("[monitor] loaded sync buf from %s", m.bufPath) + } +} + +func (m *Monitor) saveBuf() { + dir := filepath.Dir(m.bufPath) + if err := os.MkdirAll(dir, 0o700); err != nil { + log.Printf("[monitor] failed to create buf dir: %v", err) + return + } + data, _ := json.Marshal(syncData{GetUpdatesBuf: m.getUpdatesBuf}) + if err := os.WriteFile(m.bufPath, data, 0o600); err != nil { + log.Printf("[monitor] failed to save buf: %v", err) + } +} + +// FormatMessageSummary returns a short description of a message for logging. +func FormatMessageSummary(msg WeixinMessage) string { + text := "" + for _, item := range msg.ItemList { + if item.Type == ItemTypeText && item.TextItem != nil { + text = item.TextItem.Text + break + } + } + if len(text) > 50 { + text = text[:50] + "..." + } + return fmt.Sprintf("from=%s type=%d state=%d text=%q", msg.FromUserID, msg.MessageType, msg.MessageState, text) +} diff --git a/services/wechat-connector/ilink/types.go b/services/wechat-connector/ilink/types.go new file mode 100644 index 0000000..b388ca8 --- /dev/null +++ b/services/wechat-connector/ilink/types.go @@ -0,0 +1,219 @@ +package ilink + +// Message types +const ( + MessageTypeNone = 0 + MessageTypeUser = 1 + MessageTypeBot = 2 +) + +// Message states +const ( + MessageStateNew = 0 + MessageStateGenerating = 1 + MessageStateFinish = 2 +) + +// Item types +const ( + ItemTypeNone = 0 + ItemTypeText = 1 + ItemTypeImage = 2 + ItemTypeVoice = 3 + ItemTypeFile = 4 + ItemTypeVideo = 5 +) + +// QRCodeResponse is the response from get_bot_qrcode. +type QRCodeResponse struct { + QRCode string `json:"qrcode"` + QRCodeImgContent string `json:"qrcode_img_content"` +} + +// QRStatusResponse is the response from get_qrcode_status. +type QRStatusResponse struct { + Status string `json:"status"` + BotToken string `json:"bot_token"` + ILinkBotID string `json:"ilink_bot_id"` + BaseURL string `json:"baseurl"` + ILinkUserID string `json:"ilink_user_id"` +} + +// Credentials stores login session data. +type Credentials struct { + BotToken string `json:"bot_token"` + ILinkBotID string `json:"ilink_bot_id"` + BaseURL string `json:"baseurl"` + ILinkUserID string `json:"ilink_user_id"` +} + +// BaseInfo is included in request bodies. +type BaseInfo struct { + ChannelVersion string `json:"channel_version,omitempty"` +} + +// GetUpdatesRequest is the body for getupdates. +type GetUpdatesRequest struct { + GetUpdatesBuf string `json:"get_updates_buf"` + BaseInfo BaseInfo `json:"base_info"` +} + +// GetUpdatesResponse is the response from getupdates. +type GetUpdatesResponse struct { + Ret int `json:"ret"` + ErrCode int `json:"errcode,omitempty"` + ErrMsg string `json:"errmsg,omitempty"` + Msgs []WeixinMessage `json:"msgs"` + GetUpdatesBuf string `json:"get_updates_buf"` + LongPollingTimeoutMs int `json:"longpolling_timeout_ms,omitempty"` +} + +// WeixinMessage represents a message from WeChat. +type WeixinMessage struct { + Seq int `json:"seq,omitempty"` + MessageID int64 `json:"message_id,omitempty"` + FromUserID string `json:"from_user_id"` + ToUserID string `json:"to_user_id"` + MessageType int `json:"message_type"` + MessageState int `json:"message_state"` + ItemList []MessageItem `json:"item_list"` + ContextToken string `json:"context_token"` +} + +// MessageItem is a single item in a message. +type MessageItem struct { + Type int `json:"type"` + TextItem *TextItem `json:"text_item,omitempty"` + ImageItem *ImageItem `json:"image_item,omitempty"` + VoiceItem *VoiceItem `json:"voice_item,omitempty"` + VideoItem *VideoItem `json:"video_item,omitempty"` + FileItem *FileItem `json:"file_item,omitempty"` +} + +// CDN media type constants. +const ( + CDNMediaTypeImage = 1 + CDNMediaTypeVideo = 2 + CDNMediaTypeFile = 3 +) + +// GetUploadURLRequest is the body for getuploadurl. +type GetUploadURLRequest struct { + FileKey string `json:"filekey"` + MediaType int `json:"media_type"` + ToUserID string `json:"to_user_id"` + RawSize int `json:"rawsize"` + RawFileMD5 string `json:"rawfilemd5"` + FileSize int `json:"filesize"` + NoNeedThumb bool `json:"no_need_thumb"` + AESKey string `json:"aeskey"` + BaseInfo BaseInfo `json:"base_info"` +} + +// GetUploadURLResponse is the response from getuploadurl. +type GetUploadURLResponse struct { + Ret int `json:"ret"` + ErrMsg string `json:"errmsg,omitempty"` + UploadParam string `json:"upload_param"` + UploadFullURL string `json:"upload_full_url,omitempty"` +} + +// TextItem holds text content. +type TextItem struct { + Text string `json:"text"` +} + +// MediaInfo holds CDN media reference for uploaded files. +type MediaInfo struct { + EncryptQueryParam string `json:"encrypt_query_param"` + AESKey string `json:"aes_key"` // base64-encoded + EncryptType int `json:"encrypt_type"` // 1 = AES-128-ECB +} + +// VoiceItem holds voice content. +type VoiceItem struct { + Media *MediaInfo `json:"media,omitempty"` + VoiceSize int `json:"voice_size,omitempty"` + EncodeType int `json:"encode_type,omitempty"` // 1=pcm 2=adpcm 3=feature 4=speex 5=amr 6=silk 7=mp3 + BitsPerSample int `json:"bits_per_sample,omitempty"` + SampleRate int `json:"sample_rate,omitempty"` // Hz + Playtime int `json:"playtime,omitempty"` // duration in milliseconds + Text string `json:"text,omitempty"` // speech-to-text transcription from WeChat +} + +// ImageItem holds image content. +type ImageItem struct { + URL string `json:"url,omitempty"` + Media *MediaInfo `json:"media,omitempty"` + MidSize int `json:"mid_size,omitempty"` // ciphertext size +} + +// VideoItem holds video content. +type VideoItem struct { + Media *MediaInfo `json:"media,omitempty"` + VideoSize int `json:"video_size,omitempty"` +} + +// FileItem holds file content. +type FileItem struct { + Media *MediaInfo `json:"media,omitempty"` + FileName string `json:"file_name,omitempty"` + Len string `json:"len,omitempty"` // plaintext size as string +} + +// SendMessageRequest is the body for sendmessage. +type SendMessageRequest struct { + Msg SendMsg `json:"msg"` + BaseInfo BaseInfo `json:"base_info"` +} + +// SendMsg is the message payload for sending. +type SendMsg struct { + FromUserID string `json:"from_user_id"` + ToUserID string `json:"to_user_id"` + ClientID string `json:"client_id"` + MessageType int `json:"message_type"` + MessageState int `json:"message_state"` + ItemList []MessageItem `json:"item_list"` + ContextToken string `json:"context_token"` +} + +// SendMessageResponse is the response from sendmessage. +type SendMessageResponse struct { + Ret int `json:"ret"` + ErrMsg string `json:"errmsg,omitempty"` +} + +// Typing status constants. +const ( + TypingStatusTyping = 1 + TypingStatusCancel = 2 +) + +// GetConfigRequest is the body for getconfig. +type GetConfigRequest struct { + ILinkUserID string `json:"ilink_user_id"` + ContextToken string `json:"context_token,omitempty"` + BaseInfo BaseInfo `json:"base_info"` +} + +// GetConfigResponse is the response from getconfig. +type GetConfigResponse struct { + Ret int `json:"ret"` + ErrMsg string `json:"errmsg,omitempty"` + TypingTicket string `json:"typing_ticket,omitempty"` +} + +// SendTypingRequest is the body for sendtyping. +type SendTypingRequest struct { + ILinkUserID string `json:"ilink_user_id"` + TypingTicket string `json:"typing_ticket"` + Status int `json:"status"` + BaseInfo BaseInfo `json:"base_info"` +} + +// SendTypingResponse is the response from sendtyping. +type SendTypingResponse struct { + Ret int `json:"ret"` + ErrMsg string `json:"errmsg,omitempty"` +} diff --git a/services/wechat-connector/main.go b/services/wechat-connector/main.go new file mode 100644 index 0000000..09a0111 --- /dev/null +++ b/services/wechat-connector/main.go @@ -0,0 +1,200 @@ +package main + +import ( + "context" + "encoding/base64" + "encoding/json" + "errors" + "flag" + "fmt" + "log" + "os" + "os/signal" + "strings" + "sync" + "syscall" + + "github.com/Wxw-Gu/WechatExplorer/services/wechat-connector/api" + "github.com/Wxw-Gu/WechatExplorer/services/wechat-connector/ilink" + "github.com/Wxw-Gu/WechatExplorer/services/wechat-connector/messaging" + "rsc.io/qr" +) + +type loginEvent struct { + Status string `json:"status"` + QRCodeDataURL string `json:"qr_code_data_url,omitempty"` + AccountID string `json:"account_id,omitempty"` + WeChatUserID string `json:"wechat_user_id,omitempty"` +} + +type accountSummary struct { + AccountID string `json:"account_id"` + WeChatUserID string `json:"wechat_user_id"` +} + +func main() { + if len(os.Args) < 2 { + fatal(errors.New("expected one of: login, accounts, start")) + } + var err error + switch os.Args[1] { + case "login": + err = runLogin(os.Args[2:]) + case "accounts": + err = runAccounts(os.Args[2:]) + case "start": + err = runStart(os.Args[2:]) + default: + err = fmt.Errorf("unknown command %q", os.Args[1]) + } + if err != nil { + fatal(err) + } +} + +func fatal(err error) { + fmt.Fprintln(os.Stderr, err) + os.Exit(1) +} + +func signalContext() (context.Context, context.CancelFunc) { + return signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM) +} + +func runLogin(args []string) error { + flags := flag.NewFlagSet("login", flag.ContinueOnError) + jsonOutput := flags.Bool("json", false, "emit JSON Lines events") + if err := flags.Parse(args); err != nil { + return err + } + ctx, cancel := signalContext() + defer cancel() + creds, err := login(ctx, *jsonOutput) + if err != nil { + return err + } + if !*jsonOutput { + fmt.Printf("WeChat account %s connected.\n", creds.ILinkBotID) + } + return nil +} + +func login(ctx context.Context, jsonOutput bool) (*ilink.Credentials, error) { + qrResponse, err := ilink.FetchQRCode(ctx) + if err != nil { + return nil, err + } + code, err := qr.Encode(qrResponse.QRCodeImgContent, qr.L) + if err != nil { + return nil, fmt.Errorf("encode QR image: %w", err) + } + emit := func(event loginEvent) { + if jsonOutput { + _ = json.NewEncoder(os.Stdout).Encode(event) + } + } + emit(loginEvent{Status: "qrcode", QRCodeDataURL: "data:image/png;base64," + base64.StdEncoding.EncodeToString(code.PNG())}) + lastStatus := "" + creds, err := ilink.PollQRStatus(ctx, qrResponse.QRCode, func(status string) { + if status != lastStatus { + lastStatus = status + emit(loginEvent{Status: status}) + } + }) + if err != nil { + return nil, err + } + if err := ilink.SaveCredentials(creds); err != nil { + return nil, fmt.Errorf("save credentials: %w", err) + } + emit(loginEvent{Status: "active", AccountID: creds.ILinkBotID, WeChatUserID: creds.ILinkUserID}) + return creds, nil +} + +func runAccounts(args []string) error { + flags := flag.NewFlagSet("accounts", flag.ContinueOnError) + jsonOutput := flags.Bool("json", false, "print JSON") + if err := flags.Parse(args); err != nil { + return err + } + accounts, err := ilink.LoadAllCredentials() + if err != nil { + return err + } + items := make([]accountSummary, 0, len(accounts)) + for _, account := range accounts { + items = append(items, accountSummary{AccountID: account.ILinkBotID, WeChatUserID: account.ILinkUserID}) + } + if *jsonOutput { + return json.NewEncoder(os.Stdout).Encode(map[string]any{"accounts": items}) + } + for _, item := range items { + fmt.Printf("%s\t%s\n", item.AccountID, item.WeChatUserID) + } + return nil +} + +func runStart(args []string) error { + flags := flag.NewFlagSet("start", flag.ContinueOnError) + _ = flags.Bool("foreground", false, "kept for host compatibility") + apiAddr := flags.String("api-addr", "127.0.0.1:18011", "local send API address") + accountID := flags.String("account-id", "", "account to start") + if err := flags.Parse(args); err != nil { + return err + } + accounts, err := ilink.LoadAllCredentials() + if err != nil { + return err + } + if len(accounts) == 0 { + return errors.New("no connected WeChat account; scan a QR code first") + } + selected := accounts[len(accounts)-1] + if *accountID != "" { + selected = nil + for _, account := range accounts { + if account.ILinkBotID == *accountID { + selected = account + break + } + } + if selected == nil { + return fmt.Errorf("account %q not found", *accountID) + } + } + + ctx, cancel := signalContext() + defer cancel() + client := ilink.NewClient(selected) + server := api.NewServer([]*ilink.Client{client}, *apiAddr) + webhookURL := strings.TrimSpace(os.Getenv("WECHAT_CONNECTOR_INBOUND_WEBHOOK_URL")) + webhook := messaging.NewInboundWebhook(webhookURL, os.Getenv("WECHAT_CONNECTOR_INBOUND_WEBHOOK_TOKEN")) + + monitor, err := ilink.NewMonitor(client, func(messageContext context.Context, source *ilink.Client, message ilink.WeixinMessage) { + if webhookURL != "" { + webhook.Dispatch(messageContext, source, message) + } + }) + if err != nil { + return err + } + + var wait sync.WaitGroup + wait.Add(2) + go func() { + defer wait.Done() + if err := server.Run(ctx); err != nil && ctx.Err() == nil { + log.Printf("[api] stopped: %v", err) + cancel() + } + }() + go func() { + defer wait.Done() + if err := monitor.Run(ctx); err != nil && ctx.Err() == nil { + log.Printf("[monitor] stopped: %v", err) + cancel() + } + }() + wait.Wait() + return nil +} diff --git a/services/wechat-connector/messaging/cdn.go b/services/wechat-connector/messaging/cdn.go new file mode 100644 index 0000000..e0a07ea --- /dev/null +++ b/services/wechat-connector/messaging/cdn.go @@ -0,0 +1,232 @@ +package messaging + +import ( + "bytes" + "context" + "crypto/aes" + "crypto/md5" + "crypto/rand" + "encoding/base64" + "encoding/hex" + "fmt" + "io" + "net/http" + "net/url" + "strings" + "time" + + "github.com/Wxw-Gu/WechatExplorer/services/wechat-connector/ilink" +) + +const cdnBaseURL = "https://novac2c.cdn.weixin.qq.com/c2c" + +// UploadedFile holds the result of a CDN upload. +type UploadedFile struct { + DownloadParam string // encrypted query param for download + AESKeyHex string // hex-encoded AES key + FileSize int // plaintext size + CipherSize int // ciphertext size +} + +// UploadFileToCDN encrypts and uploads a file to the WeChat CDN. +func UploadFileToCDN(ctx context.Context, client *ilink.Client, data []byte, toUserID string, mediaType int) (*UploadedFile, error) { + // Generate random filekey and AES key + filekey := make([]byte, 16) + aeskey := make([]byte, 16) + if _, err := rand.Read(filekey); err != nil { + return nil, fmt.Errorf("generate filekey: %w", err) + } + if _, err := rand.Read(aeskey); err != nil { + return nil, fmt.Errorf("generate aeskey: %w", err) + } + + filekeyHex := hex.EncodeToString(filekey) + aeskeyHex := hex.EncodeToString(aeskey) + + // Calculate MD5 of plaintext + hash := md5.Sum(data) + rawMD5 := hex.EncodeToString(hash[:]) + + // Calculate ciphertext size (PKCS7 padding) + cipherSize := aesECBPaddedSize(len(data)) + + // Get upload URL from iLink API + uploadReq := &ilink.GetUploadURLRequest{ + FileKey: filekeyHex, + MediaType: mediaType, + ToUserID: toUserID, + RawSize: len(data), + RawFileMD5: rawMD5, + FileSize: cipherSize, + NoNeedThumb: true, + AESKey: aeskeyHex, + BaseInfo: ilink.BaseInfo{}, + } + + uploadResp, err := client.GetUploadURL(ctx, uploadReq) + if err != nil { + return nil, fmt.Errorf("get upload URL: %w", err) + } + if uploadResp.Ret != 0 { + return nil, fmt.Errorf("get upload URL failed: ret=%d errmsg=%s", uploadResp.Ret, uploadResp.ErrMsg) + } + + // Encrypt data with AES-128-ECB + encrypted, err := encryptAESECB(data, aeskey) + if err != nil { + return nil, fmt.Errorf("encrypt: %w", err) + } + + // Upload to CDN: prefer server-provided full URL, fall back to param-based construction + cdnURL := strings.TrimSpace(uploadResp.UploadFullURL) + if cdnURL == "" { + if uploadResp.UploadParam == "" { + return nil, fmt.Errorf("getuploadurl returned no upload URL (need upload_full_url or upload_param)") + } + cdnURL = fmt.Sprintf("%s/upload?encrypted_query_param=%s&filekey=%s", + cdnBaseURL, url.QueryEscape(uploadResp.UploadParam), url.QueryEscape(filekeyHex)) + } + + downloadParam, err := uploadToCDN(ctx, encrypted, cdnURL) + if err != nil { + return nil, fmt.Errorf("CDN upload: %w", err) + } + + return &UploadedFile{ + DownloadParam: downloadParam, + AESKeyHex: aeskeyHex, + FileSize: len(data), + CipherSize: cipherSize, + }, nil +} + +// AESKeyToBase64 converts a hex AES key to base64 format for message items. +func AESKeyToBase64(hexKey string) string { + return base64.StdEncoding.EncodeToString([]byte(hexKey)) +} + +// DownloadFileFromCDN downloads and decrypts a file from the WeChat CDN. +func DownloadFileFromCDN(ctx context.Context, encryptQueryParam, aesKeyBase64 string) ([]byte, error) { + // Decode AES key: base64 -> hex string -> raw bytes + aesKeyHexBytes, err := base64.StdEncoding.DecodeString(aesKeyBase64) + if err != nil { + return nil, fmt.Errorf("decode AES key base64: %w", err) + } + aesKey, err := hex.DecodeString(string(aesKeyHexBytes)) + if err != nil { + return nil, fmt.Errorf("decode AES key hex: %w", err) + } + + // Download encrypted data from CDN + downloadURL := fmt.Sprintf("%s/download?encrypted_query_param=%s", + cdnBaseURL, url.QueryEscape(encryptQueryParam)) + + reqCtx, cancel := context.WithTimeout(ctx, 60*time.Second) + defer cancel() + + req, err := http.NewRequestWithContext(reqCtx, http.MethodGet, downloadURL, nil) + if err != nil { + return nil, fmt.Errorf("create download request: %w", err) + } + + resp, err := http.DefaultClient.Do(req) + if err != nil { + return nil, fmt.Errorf("download from CDN: %w", err) + } + defer resp.Body.Close() + + if resp.StatusCode != http.StatusOK { + body, _ := io.ReadAll(resp.Body) + return nil, fmt.Errorf("CDN download HTTP %d: %s", resp.StatusCode, string(body)) + } + + encrypted, err := io.ReadAll(resp.Body) + if err != nil { + return nil, fmt.Errorf("read CDN response: %w", err) + } + + // Decrypt AES-128-ECB + return decryptAESECB(encrypted, aesKey) +} + +// decryptAESECB decrypts data encrypted with AES-128-ECB and removes PKCS7 padding. +func decryptAESECB(ciphertext, key []byte) ([]byte, error) { + block, err := aes.NewCipher(key) + if err != nil { + return nil, err + } + + if len(ciphertext)%aes.BlockSize != 0 { + return nil, fmt.Errorf("ciphertext is not a multiple of block size") + } + + plaintext := make([]byte, len(ciphertext)) + for i := 0; i < len(ciphertext); i += aes.BlockSize { + block.Decrypt(plaintext[i:i+aes.BlockSize], ciphertext[i:i+aes.BlockSize]) + } + + // Remove PKCS7 padding + if len(plaintext) == 0 { + return plaintext, nil + } + padLen := int(plaintext[len(plaintext)-1]) + if padLen > aes.BlockSize || padLen == 0 { + return nil, fmt.Errorf("invalid PKCS7 padding") + } + return plaintext[:len(plaintext)-padLen], nil +} + +func uploadToCDN(ctx context.Context, encrypted []byte, cdnURL string) (string, error) { + req, err := http.NewRequestWithContext(ctx, http.MethodPost, cdnURL, bytes.NewReader(encrypted)) + if err != nil { + return "", err + } + req.Header.Set("Content-Type", "application/octet-stream") + + client := &http.Client{Timeout: 60 * time.Second} + resp, err := client.Do(req) + if err != nil { + return "", err + } + defer resp.Body.Close() + + if resp.StatusCode != http.StatusOK { + body, _ := io.ReadAll(resp.Body) + return "", fmt.Errorf("CDN upload HTTP %d: %s", resp.StatusCode, string(body)) + } + + downloadParam := resp.Header.Get("X-Encrypted-Param") + if downloadParam == "" { + return "", fmt.Errorf("CDN upload: missing X-Encrypted-Param header") + } + + return downloadParam, nil +} + +// encryptAESECB encrypts data using AES-128-ECB with PKCS7 padding. +func encryptAESECB(plaintext, key []byte) ([]byte, error) { + block, err := aes.NewCipher(key) + if err != nil { + return nil, err + } + + // PKCS7 padding + padLen := aes.BlockSize - (len(plaintext) % aes.BlockSize) + padded := make([]byte, len(plaintext)+padLen) + copy(padded, plaintext) + for i := len(plaintext); i < len(padded); i++ { + padded[i] = byte(padLen) + } + + // ECB mode: encrypt each block independently + encrypted := make([]byte, len(padded)) + for i := 0; i < len(padded); i += aes.BlockSize { + block.Encrypt(encrypted[i:i+aes.BlockSize], padded[i:i+aes.BlockSize]) + } + + return encrypted, nil +} + +func aesECBPaddedSize(plaintextSize int) int { + return (plaintextSize/aes.BlockSize + 1) * aes.BlockSize +} diff --git a/services/wechat-connector/messaging/inbound_webhook.go b/services/wechat-connector/messaging/inbound_webhook.go new file mode 100644 index 0000000..6fc4090 --- /dev/null +++ b/services/wechat-connector/messaging/inbound_webhook.go @@ -0,0 +1,121 @@ +package messaging + +import ( + "bytes" + "context" + "encoding/json" + "fmt" + "io" + "log" + "net/http" + "strings" + "time" + + "github.com/Wxw-Gu/WechatExplorer/services/wechat-connector/ilink" +) + +const ( + webhookAttempts = 3 + webhookTimeout = 5 * time.Second +) + +type InboundWebhook struct { + url string + token string + client *http.Client +} + +type inboundWebhookPayload struct { + AccountID string `json:"account_id"` + FromUserID string `json:"from_user_id"` + MessageID int64 `json:"message_id"` + MessageType int `json:"message_type"` + Items []inboundWebhookItem `json:"items"` + ReceivedAt time.Time `json:"received_at"` +} + +type inboundWebhookItem struct { + Type int `json:"type"` + Text string `json:"text,omitempty"` +} + +func NewInboundWebhook(url, token string) *InboundWebhook { + return &InboundWebhook{ + url: strings.TrimSpace(url), + token: token, + client: &http.Client{Timeout: webhookTimeout}, + } +} + +// Dispatch is intentionally non-blocking so webhook failures never stall iLink polling. +func (w *InboundWebhook) Dispatch(ctx context.Context, client *ilink.Client, msg ilink.WeixinMessage) { + payload := normalizeInboundMessage(client.BotID(), msg) + go func() { + if err := w.deliver(ctx, payload); err != nil { + log.Printf("[webhook] inbound delivery failed for message %d: %v", msg.MessageID, err) + } + }() +} + +func (w *InboundWebhook) deliver(ctx context.Context, payload inboundWebhookPayload) error { + body, err := json.Marshal(payload) + if err != nil { + return fmt.Errorf("encode payload: %w", err) + } + var lastErr error + for attempt := 1; attempt <= webhookAttempts; attempt++ { + if attempt > 1 { + timer := time.NewTimer(time.Duration(attempt-1) * time.Second) + select { + case <-ctx.Done(): + timer.Stop() + return ctx.Err() + case <-timer.C: + } + } + req, reqErr := http.NewRequestWithContext(ctx, http.MethodPost, w.url, bytes.NewReader(body)) + if reqErr != nil { + return fmt.Errorf("create request: %w", reqErr) + } + req.Header.Set("Content-Type", "application/json") + if w.token != "" { + req.Header.Set("Authorization", "Bearer "+w.token) + } + resp, doErr := w.client.Do(req) + if doErr != nil { + lastErr = doErr + continue + } + responseBody, _ := io.ReadAll(io.LimitReader(resp.Body, 4096)) + resp.Body.Close() + if resp.StatusCode >= 200 && resp.StatusCode < 300 { + return nil + } + lastErr = fmt.Errorf("status %s: %s", resp.Status, strings.TrimSpace(string(responseBody))) + if resp.StatusCode >= 400 && resp.StatusCode < 500 { + break + } + } + return lastErr +} + +func normalizeInboundMessage(accountID string, msg ilink.WeixinMessage) inboundWebhookPayload { + items := make([]inboundWebhookItem, 0, len(msg.ItemList)) + for _, item := range msg.ItemList { + normalized := inboundWebhookItem{Type: item.Type} + if item.TextItem != nil { + normalized.Text = item.TextItem.Text + } else if item.VoiceItem != nil { + normalized.Text = item.VoiceItem.Text + } + items = append(items, normalized) + } + return inboundWebhookPayload{ + AccountID: accountID, + FromUserID: msg.FromUserID, + MessageID: msg.MessageID, + MessageType: msg.MessageType, + Items: items, + ReceivedAt: time.Now().UTC(), + } +} diff --git a/services/wechat-connector/messaging/inbound_webhook_test.go b/services/wechat-connector/messaging/inbound_webhook_test.go new file mode 100644 index 0000000..c40a4f3 --- /dev/null +++ b/services/wechat-connector/messaging/inbound_webhook_test.go @@ -0,0 +1,75 @@ +package messaging + +import ( + "context" + "encoding/json" + "net/http" + "net/http/httptest" + "sync/atomic" + "testing" + + "github.com/Wxw-Gu/WechatExplorer/services/wechat-connector/ilink" +) + +func TestInboundWebhookDeliversNormalizedPayload(t *testing.T) { + var got inboundWebhookPayload + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.Header.Get("Authorization") != "Bearer secret" { + t.Errorf("authorization = %q", r.Header.Get("Authorization")) + } + if err := json.NewDecoder(r.Body).Decode(&got); err != nil { + t.Errorf("decode: %v", err) + } + w.WriteHeader(http.StatusOK) + })) + defer server.Close() + + webhook := NewInboundWebhook(server.URL, "secret") + err := webhook.deliver(context.Background(), normalizeInboundMessage("bot-new", ilink.WeixinMessage{ + MessageID: 7, FromUserID: "user-1", MessageType: ilink.MessageTypeUser, + ItemList: []ilink.MessageItem{{Type: ilink.ItemTypeText, TextItem: &ilink.TextItem{Text: "最近5条消息"}}}, + })) + if err != nil { + t.Fatalf("deliver: %v", err) + } + if got.AccountID != "bot-new" || got.MessageID != 7 || len(got.Items) != 1 || got.Items[0].Text != "最近5条消息" { + t.Fatalf("payload = %#v", got) + } +} + +func TestInboundWebhookRetriesServerErrors(t *testing.T) { + var calls atomic.Int32 + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { + if calls.Add(1) < 3 { + http.Error(w, "temporary", http.StatusServiceUnavailable) + return + } + w.WriteHeader(http.StatusOK) + })) + defer server.Close() + + webhook := NewInboundWebhook(server.URL, "") + if err := webhook.deliver(context.Background(), inboundWebhookPayload{}); err != nil { + t.Fatalf("deliver: %v", err) + } + if calls.Load() != 3 { + t.Fatalf("calls = %d, want 3", calls.Load()) + } +} + +func TestInboundWebhookDoesNotRetryClientErrors(t *testing.T) { + var calls atomic.Int32 + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { + calls.Add(1) + http.Error(w, "unauthorized", http.StatusUnauthorized) + })) + defer server.Close() + + webhook := NewInboundWebhook(server.URL, "") + if err := webhook.deliver(context.Background(), inboundWebhookPayload{}); err == nil { + t.Fatal("deliver error = nil") + } + if calls.Load() != 1 { + t.Fatalf("calls = %d, want 1", calls.Load()) + } +} diff --git a/services/wechat-connector/messaging/markdown.go b/services/wechat-connector/messaging/markdown.go new file mode 100644 index 0000000..012b47b --- /dev/null +++ b/services/wechat-connector/messaging/markdown.go @@ -0,0 +1,103 @@ +package messaging + +import ( + "regexp" + "strings" +) + +var ( + // Code blocks: strip fences, keep code content + reCodeBlock = regexp.MustCompile("(?s)```[^\n]*\n?(.*?)```") + // Inline code: strip backticks, keep content + reInlineCode = regexp.MustCompile("`([^`]+)`") + // Images: remove entirely + reImage = regexp.MustCompile(`!\[[^\]]*\]\([^)]*\)`) + // Links: keep display text only + reLink = regexp.MustCompile(`\[([^\]]+)\]\([^)]*\)`) + // Table separator rows: remove + reTableSep = regexp.MustCompile(`(?m)^\|[\s:|\-]+\|$`) + // Table rows: convert pipe-delimited to space-delimited + reTableRow = regexp.MustCompile(`(?m)^\|(.+)\|$`) + // Headers: remove # prefix + reHeader = regexp.MustCompile(`(?m)^#{1,6}\s+`) + // Bold: **text** or __text__ + reBold = regexp.MustCompile(`\*\*(.+?)\*\*|__(.+?)__`) + // Italic: *text* or _text_ + reItalic = regexp.MustCompile(`(?:^|[^*])\*([^*]+)\*(?:[^*]|$)|(?:^|[^_])_([^_]+)_(?:[^_]|$)`) + // Strikethrough: ~~text~~ + reStrike = regexp.MustCompile(`~~(.+?)~~`) + // Blockquote: > prefix + reBlockquote = regexp.MustCompile(`(?m)^>\s?`) + // Horizontal rule + reHR = regexp.MustCompile(`(?m)^[-*_]{3,}\s*$`) + // Unordered list markers: -, *, + + reUL = regexp.MustCompile(`(?m)^(\s*)[-*+]\s+`) +) + +// MarkdownToPlainText converts markdown to readable plain text for WeChat. +func MarkdownToPlainText(text string) string { + result := text + + // Code blocks: strip fences, keep code content + result = reCodeBlock.ReplaceAllStringFunc(result, func(match string) string { + parts := reCodeBlock.FindStringSubmatch(match) + if len(parts) > 1 { + return strings.TrimSpace(parts[1]) + } + return match + }) + + // Images: remove entirely + result = reImage.ReplaceAllString(result, "") + + // Links: keep display text only + result = reLink.ReplaceAllString(result, "$1") + + // Table separator rows: remove + result = reTableSep.ReplaceAllString(result, "") + + // Table rows: pipe-delimited to space-delimited + result = reTableRow.ReplaceAllStringFunc(result, func(match string) string { + parts := reTableRow.FindStringSubmatch(match) + if len(parts) > 1 { + cells := strings.Split(parts[1], "|") + for i := range cells { + cells[i] = strings.TrimSpace(cells[i]) + } + return strings.Join(cells, " ") + } + return match + }) + + // Headers: remove # prefix + result = reHeader.ReplaceAllString(result, "") + + // Bold + result = reBold.ReplaceAllStringFunc(result, func(match string) string { + parts := reBold.FindStringSubmatch(match) + if parts[1] != "" { + return parts[1] + } + return parts[2] + }) + + // Strikethrough + result = reStrike.ReplaceAllString(result, "$1") + + // Blockquote + result = reBlockquote.ReplaceAllString(result, "") + + // Horizontal rule -> empty line + result = reHR.ReplaceAllString(result, "") + + // Unordered list: replace markers with "• " + result = reUL.ReplaceAllString(result, "${1}• ") + + // Inline code: strip backticks (do after code blocks) + result = reInlineCode.ReplaceAllString(result, "$1") + + // Clean up excessive blank lines + result = regexp.MustCompile(`\n{3,}`).ReplaceAllString(result, "\n\n") + + return strings.TrimSpace(result) +} diff --git a/services/wechat-connector/messaging/media.go b/services/wechat-connector/messaging/media.go new file mode 100644 index 0000000..043eeb3 --- /dev/null +++ b/services/wechat-connector/messaging/media.go @@ -0,0 +1,221 @@ +package messaging + +import ( + "context" + "fmt" + "io" + "log" + "mime" + "net/http" + "os" + "path/filepath" + "regexp" + "strings" + "time" + + "github.com/Wxw-Gu/WechatExplorer/services/wechat-connector/ilink" +) + +// reMarkdownImage matches markdown image syntax: ![alt](url) +var reMarkdownImage = regexp.MustCompile(`!\[[^\]]*\]\(([^)]+)\)`) + +// ExtractImageURLs extracts image URLs from markdown text. +func ExtractImageURLs(text string) []string { + matches := reMarkdownImage.FindAllStringSubmatch(text, -1) + var urls []string + for _, m := range matches { + url := strings.TrimSpace(m[1]) + if strings.HasPrefix(url, "http://") || strings.HasPrefix(url, "https://") { + urls = append(urls, url) + } + } + return urls +} + +// SendMediaFromURL sends a local file or downloads from a URL and sends it as a media message. +func SendMediaFromURL(ctx context.Context, client *ilink.Client, toUserID, mediaURL, contextToken string) error { + // Check if it's a local file + if _, err := os.Stat(mediaURL); err == nil { + return SendMediaFromPath(ctx, client, toUserID, mediaURL, contextToken) + } + // Must be a valid HTTP URL to download + if !strings.HasPrefix(mediaURL, "http://") && !strings.HasPrefix(mediaURL, "https://") { + return fmt.Errorf("unsupported media path (not a local file and not an HTTP URL): %s", mediaURL) + } + data, contentType, err := downloadFile(ctx, mediaURL) + if err != nil { + return fmt.Errorf("download %s: %w", mediaURL, err) + } + + return sendMediaData(ctx, client, toUserID, filenameFromURL(mediaURL), mediaURL, data, contentType, contextToken) +} + +// SendMediaFromPath reads a local file and sends it as a media message. +func SendMediaFromPath(ctx context.Context, client *ilink.Client, toUserID, path, contextToken string) error { + data, err := os.ReadFile(path) + if err != nil { + return fmt.Errorf("read %s: %w", path, err) + } + + return sendMediaData(ctx, client, toUserID, filepath.Base(path), path, data, inferContentType(path), contextToken) +} + +func sendMediaData(ctx context.Context, client *ilink.Client, toUserID, fileName, source string, data []byte, contentType, contextToken string) error { + if fileName == "" { + fileName = "file" + } + + cdnMediaType, itemType := classifyMedia(contentType, source) + + log.Printf("[media] uploading %s (%s, %d bytes) for %s", source, contentType, len(data), toUserID) + + uploaded, err := UploadFileToCDN(ctx, client, data, toUserID, cdnMediaType) + if err != nil { + return fmt.Errorf("upload to CDN: %w", err) + } + + media := &ilink.MediaInfo{ + EncryptQueryParam: uploaded.DownloadParam, + AESKey: AESKeyToBase64(uploaded.AESKeyHex), + EncryptType: 1, + } + + var item ilink.MessageItem + switch itemType { + case ilink.ItemTypeImage: + item = ilink.MessageItem{ + Type: ilink.ItemTypeImage, + ImageItem: &ilink.ImageItem{ + Media: media, + MidSize: uploaded.CipherSize, + }, + } + case ilink.ItemTypeVideo: + item = ilink.MessageItem{ + Type: ilink.ItemTypeVideo, + VideoItem: &ilink.VideoItem{ + Media: media, + VideoSize: uploaded.CipherSize, + }, + } + default: + item = ilink.MessageItem{ + Type: ilink.ItemTypeFile, + FileItem: &ilink.FileItem{ + Media: media, + FileName: fileName, + Len: fmt.Sprintf("%d", uploaded.FileSize), + }, + } + } + + req := &ilink.SendMessageRequest{ + Msg: ilink.SendMsg{ + FromUserID: client.BotID(), + ToUserID: toUserID, + ClientID: NewClientID(), + MessageType: ilink.MessageTypeBot, + MessageState: ilink.MessageStateFinish, + ItemList: []ilink.MessageItem{item}, + ContextToken: contextToken, + }, + BaseInfo: ilink.BaseInfo{}, + } + + resp, err := client.SendMessage(ctx, req) + if err != nil { + return fmt.Errorf("send media message: %w", err) + } + if resp.Ret != 0 { + return fmt.Errorf("send media failed: ret=%d errmsg=%s", resp.Ret, resp.ErrMsg) + } + + log.Printf("[media] sent %s to %s from %s", contentType, toUserID, source) + return nil +} + +func downloadFile(ctx context.Context, url string) ([]byte, string, error) { + ctx, cancel := context.WithTimeout(ctx, 60*time.Second) + defer cancel() + + req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil) + if err != nil { + return nil, "", err + } + + resp, err := http.DefaultClient.Do(req) + if err != nil { + return nil, "", err + } + defer resp.Body.Close() + + if resp.StatusCode != http.StatusOK { + return nil, "", fmt.Errorf("HTTP %d", resp.StatusCode) + } + + data, err := io.ReadAll(resp.Body) + if err != nil { + return nil, "", err + } + + contentType := resp.Header.Get("Content-Type") + if contentType == "" { + contentType = inferContentType(url) + } + + return data, contentType, nil +} + +func classifyMedia(contentType, url string) (cdnMediaType int, itemType int) { + ct := strings.ToLower(contentType) + + if strings.HasPrefix(ct, "image/") || isImageExt(url) { + return ilink.CDNMediaTypeImage, ilink.ItemTypeImage + } + if strings.HasPrefix(ct, "video/") || isVideoExt(url) { + return ilink.CDNMediaTypeVideo, ilink.ItemTypeVideo + } + return ilink.CDNMediaTypeFile, ilink.ItemTypeFile +} + +func isImageExt(url string) bool { + ext := strings.ToLower(filepath.Ext(stripQuery(url))) + switch ext { + case ".png", ".jpg", ".jpeg", ".gif", ".webp", ".bmp": + return true + } + return false +} + +func isVideoExt(url string) bool { + ext := strings.ToLower(filepath.Ext(stripQuery(url))) + switch ext { + case ".mp4", ".mov", ".webm", ".mkv", ".avi": + return true + } + return false +} + +func inferContentType(url string) string { + ext := filepath.Ext(stripQuery(url)) + if ct := mime.TypeByExtension(ext); ct != "" { + return ct + } + return "application/octet-stream" +} + +func filenameFromURL(rawURL string) string { + u := stripQuery(rawURL) + name := filepath.Base(u) + if name == "" || name == "." || name == "/" { + return "file" + } + return name +} + +func stripQuery(rawURL string) string { + if i := strings.IndexByte(rawURL, '?'); i >= 0 { + return rawURL[:i] + } + return rawURL +} diff --git a/services/wechat-connector/messaging/media_test.go b/services/wechat-connector/messaging/media_test.go new file mode 100644 index 0000000..38029b6 --- /dev/null +++ b/services/wechat-connector/messaging/media_test.go @@ -0,0 +1,73 @@ +package messaging + +import "testing" + +func TestExtractImageURLs(t *testing.T) { + text := "check ![img](https://example.com/a.png) and ![](https://example.com/b.jpg)" + urls := ExtractImageURLs(text) + if len(urls) != 2 { + t.Fatalf("expected 2 urls, got %d", len(urls)) + } + if urls[0] != "https://example.com/a.png" { + t.Errorf("urls[0] = %q", urls[0]) + } + if urls[1] != "https://example.com/b.jpg" { + t.Errorf("urls[1] = %q", urls[1]) + } +} + +func TestExtractImageURLs_NoImages(t *testing.T) { + urls := ExtractImageURLs("just plain text") + if len(urls) != 0 { + t.Errorf("expected 0 urls, got %d", len(urls)) + } +} + +func TestExtractImageURLs_RelativeURL(t *testing.T) { + text := "![img](./local.png)" + urls := ExtractImageURLs(text) + if len(urls) != 0 { + t.Errorf("expected 0 urls for relative path, got %d", len(urls)) + } +} + +func TestFilenameFromURL(t *testing.T) { + tests := []struct { + url string + want string + }{ + {"https://example.com/photo.png", "photo.png"}, + {"https://example.com/path/to/report.pdf", "report.pdf"}, + {"https://example.com/file", "file"}, + } + for _, tt := range tests { + got := filenameFromURL(tt.url) + if got != tt.want { + t.Errorf("filenameFromURL(%q) = %q, want %q", tt.url, got, tt.want) + } + } +} + +func TestFilenameFromURL_WithQuery(t *testing.T) { + got := filenameFromURL("https://example.com/photo.png?token=abc") + if got != "photo.png" { + t.Errorf("got %q, want %q", got, "photo.png") + } +} + +func TestStripQuery(t *testing.T) { + tests := []struct { + input string + want string + }{ + {"https://example.com/a?b=c", "https://example.com/a"}, + {"https://example.com/a", "https://example.com/a"}, + {"https://example.com/?x=1&y=2", "https://example.com/"}, + } + for _, tt := range tests { + got := stripQuery(tt.input) + if got != tt.want { + t.Errorf("stripQuery(%q) = %q, want %q", tt.input, got, tt.want) + } + } +} diff --git a/services/wechat-connector/messaging/sender.go b/services/wechat-connector/messaging/sender.go new file mode 100644 index 0000000..1bed0c8 --- /dev/null +++ b/services/wechat-connector/messaging/sender.go @@ -0,0 +1,86 @@ +package messaging + +import ( + "context" + "fmt" + "log" + + "github.com/Wxw-Gu/WechatExplorer/services/wechat-connector/ilink" + "github.com/google/uuid" +) + +// NewClientID generates a new unique client ID for message correlation. +func NewClientID() string { + return uuid.New().String() +} + +// SendTypingState sends a typing indicator to a user via the iLink sendtyping API. +// It first fetches a typing_ticket via getconfig, then sends the typing status. +func SendTypingState(ctx context.Context, client *ilink.Client, userID, contextToken string) error { + // Get typing ticket + configResp, err := client.GetConfig(ctx, userID, contextToken) + if err != nil { + return fmt.Errorf("get config for typing: %w", err) + } + if configResp.TypingTicket == "" { + return fmt.Errorf("no typing_ticket returned from getconfig") + } + + // Send typing + if err := client.SendTyping(ctx, userID, configResp.TypingTicket, ilink.TypingStatusTyping); err != nil { + return fmt.Errorf("send typing: %w", err) + } + + log.Printf("[sender] sent typing indicator to %s", userID) + return nil +} + +// SendTextReply sends a text reply to a user through the iLink API. +// If clientID is empty, a new one is generated. +func SendTextReply(ctx context.Context, client *ilink.Client, toUserID, text, contextToken, clientID string) error { + if clientID == "" { + clientID = NewClientID() + } + + // Convert markdown to plain text for WeChat display + plainText := MarkdownToPlainText(text) + + req := &ilink.SendMessageRequest{ + Msg: ilink.SendMsg{ + FromUserID: client.BotID(), + ToUserID: toUserID, + ClientID: clientID, + MessageType: ilink.MessageTypeBot, + MessageState: ilink.MessageStateFinish, + ItemList: []ilink.MessageItem{ + { + Type: ilink.ItemTypeText, + TextItem: &ilink.TextItem{ + Text: plainText, + }, + }, + }, + ContextToken: contextToken, + }, + BaseInfo: ilink.BaseInfo{}, + } + + resp, err := client.SendMessage(ctx, req) + if err != nil { + return fmt.Errorf("send message: %w", err) + } + + if resp.Ret != 0 { + return fmt.Errorf("send message failed: ret=%d errmsg=%s", resp.Ret, resp.ErrMsg) + } + + log.Printf("[sender] sent reply to %s: %q", toUserID, truncate(text, 50)) + return nil +} + +func truncate(s string, n int) string { + if len(s) <= n { + return s + } + return s[:n] + "..." +} diff --git a/src/main/services/agent-hub-service.ts b/src/main/services/agent-hub-service.ts index fcce51d..b044365 100644 --- a/src/main/services/agent-hub-service.ts +++ b/src/main/services/agent-hub-service.ts @@ -14,7 +14,7 @@ import type { } from '../../shared/agent-hub' import type { AppSettings } from './settings-store' import { generateAgentGroupReport } from './agent-group-report-service' -import { isReady, listRecentChat } from './chat-service' +import { isReady, listMessages, listRecentChat, resolveMd5 } from './chat-service' const execFileAsync = promisify(execFile) const HEALTH_INTERVAL_MS = 5_000 @@ -36,6 +36,11 @@ interface GroupReportIntent { range: 'today' | 'yesterday' | '7days' } +interface ContactChatIntent { + contact: string + limit: number +} + function resolveBundledBinary( resourceSegments: string[], executable: string, @@ -323,6 +328,13 @@ class AgentHubService { return this.sendHubJson(response, 202, { status: 'generating' }) } + const contactChatIntent = this.matchContactChatIntent(text) + if (contactChatIntent) { + if (messageId) this.processedMessages.set(messageId, Date.now()) + await this.replyContactChat(inbound, contactChatIntent) + return this.sendHubJson(response, 200, { status: 'ok' }) + } + const limit = this.matchRecentChatIntent(text) if (limit === null) { this.addLog('agent-hub', 'info', '消息已忽略:没有匹配到支持的意图') @@ -348,6 +360,50 @@ class AgentHubService { this.sendHubJson(response, 200, { status: 'ok' }) } + private async replyContactChat( + inbound: InboundMessage, + intent: ContactChatIntent + ): Promise { + if (!isReady()) { + await this.sendConnector(inbound, 'WechatExplorer 本地数据库尚未连接,请连接后再试。') + return + } + + const contact = resolveMd5(intent.contact) + if (!contact || contact.type !== 'user') { + this.addLog('agent-hub', 'info', `没有匹配到联系人:${intent.contact}`) + await this.sendConnector(inbound, `没有找到联系人“${intent.contact}”。`) + return + } + + this.addLog( + 'agent-hub', + 'info', + `匹配联系人聊天查询:${contact.m_nsNickName}(最近 ${intent.limit} 条)` + ) + const messages = listMessages(contact.md5, undefined, undefined, { limit: intent.limit }) + const recent = messages.slice(-intent.limit) + const lines = recent.map((message) => { + const speaker = message.isSender ? '我' : contact.m_nsNickName + const content = this.describeChatMessage(message.content, message.type) + return `${speaker}:${content}` + }) + const reply = lines.length + ? `我和${contact.m_nsNickName}最近聊了这些:\n${lines.join('\n')}` + : `暂时没有找到和${contact.m_nsNickName}的聊天记录。` + await this.sendConnector(inbound, reply) + this.addLog('agent-hub', 'info', `联系人聊天回复已发送(${recent.length} 条)`) + } + + private describeChatMessage(content: string, type: string): string { + const normalized = String(content || '') + .replace(/\s+/g, ' ') + .trim() + if (normalized) return normalized.length > 100 ? `${normalized.slice(0, 100)}…` : normalized + const label = String(type || '消息').replace(/^普通文本$/, '消息') + return `[${label}]` + } + private async generateAndSendReport( inbound: InboundMessage, intent: GroupReportIntent @@ -394,6 +450,24 @@ class AgentHubService { return Math.max(1, Math.min(20, limit)) } + private matchContactChatIntent(text: string): ContactChatIntent | null { + const normalized = text.replace(/\s+/g, '').replace(/[,。!??::]/g, '') + if (!normalized.includes('最近') || !/(聊|消息|会话)/.test(normalized)) return null + + const patterns = [ + /(?:看一下|看看|查一下|查询)?我和(.+?)最近(?:\d{1,2}条)?(?:聊了什么|聊什么|的聊天|的消息|聊天|消息)/, + /(?:看一下|看看|查一下|查询)?(?:我)?最近(?:\d{1,2}条)?和(.+?)(?:聊了什么|聊什么|的聊天|的消息|聊天|消息)/, + /(?:看一下|看看|查一下|查询)?和(.+?)最近(?:\d{1,2}条)?(?:聊了什么|聊什么|的聊天|的消息|聊天|消息)/ + ] + const contact = patterns + .map((pattern) => normalized.match(pattern)?.[1]?.trim()) + .find((value): value is string => Boolean(value)) + if (!contact) return null + + const limit = Number(normalized.match(/最近(\d{1,2})条/)?.[1] || 10) + return { contact, limit: Math.max(1, Math.min(20, limit)) } + } + private matchGroupReportIntent(text: string): GroupReportIntent | null { const normalized = text.trim() if (!normalized.includes('群') || !/(总结|日报|报告)/.test(normalized)) return null diff --git a/src/renderer/src/features/agent-hub/AgentHubWorkspace.tsx b/src/renderer/src/features/agent-hub/AgentHubWorkspace.tsx index 7dae7e4..0e44312 100644 --- a/src/renderer/src/features/agent-hub/AgentHubWorkspace.tsx +++ b/src/renderer/src/features/agent-hub/AgentHubWorkspace.tsx @@ -241,7 +241,11 @@ export function AgentHubWorkspace(): React.ReactElement { -