|
| 1 | +/** |
| 2 | + * AI 记忆锚点生成功能 Handler |
| 3 | + * |
| 4 | + * 处理 ai_anchor_point IPC 请求,调用 AI 网关从笔记内容生成记忆锚点。 |
| 5 | + */ |
| 6 | + |
| 7 | +import { safeHandle } from '../../ipcUtils.js'; |
| 8 | +import { logger } from '../../logger.js'; |
| 9 | +import { postJson, gatewayUrl, type AIFeatureDef } from '../utils.js'; |
| 10 | + |
| 11 | +// ================================================================ |
| 12 | +// IPC Handler |
| 13 | +// ================================================================ |
| 14 | + |
| 15 | +/** |
| 16 | + * ai_anchor_point — POST /api/v1/ai/anchor-point |
| 17 | + */ |
| 18 | +function register(): void { |
| 19 | + safeHandle( |
| 20 | + 'ai_anchor_point', |
| 21 | + async ( |
| 22 | + _event, |
| 23 | + args: { |
| 24 | + content: string; |
| 25 | + title?: string; |
| 26 | + authToken?: string; |
| 27 | + userApiKey?: string; |
| 28 | + }, |
| 29 | + ) => { |
| 30 | + const startMs = Date.now(); |
| 31 | + logger.info(`[AI] [anchor-point] IPC received: content_length=${args.content.length}, title=${args.title ?? '(empty)'}, hasAuth=${!!args.authToken}`); |
| 32 | + logger.debug(`[AI] [anchor-point] Content preview: ${args.content.slice(0, 80)}...`); |
| 33 | + |
| 34 | + const reqBody = { |
| 35 | + content: args.content, |
| 36 | + title: args.title ?? '', |
| 37 | + }; |
| 38 | + |
| 39 | + logger.info(`[AI] [anchor-point] Target: ${gatewayUrl()}/api/v1/ai/anchor-point`); |
| 40 | + |
| 41 | + interface AnchorPointResp { |
| 42 | + concept: string; |
| 43 | + association: string; |
| 44 | + memory_technique: string; |
| 45 | + importance: number; |
| 46 | + } |
| 47 | + interface AnchorPointGenResp { |
| 48 | + anchor_points: AnchorPointResp[]; |
| 49 | + status: string; |
| 50 | + model: string; |
| 51 | + tokens_used: number; |
| 52 | + latency_ms: number; |
| 53 | + } |
| 54 | + |
| 55 | + try { |
| 56 | + const { data: resp, requestId } = await postJson<typeof reqBody, AnchorPointGenResp>( |
| 57 | + '/api/v1/ai/anchor-point', |
| 58 | + reqBody, |
| 59 | + args.authToken, |
| 60 | + args.userApiKey, |
| 61 | + 60000, |
| 62 | + ); |
| 63 | + |
| 64 | + const elapsed = Date.now() - startMs; |
| 65 | + logger.info(`[AI] [anchor-point] ✔ Success: anchor_points=${resp.anchor_points.length}, status=${resp.status}, model=${resp.model}, tokens=${resp.tokens_used}, total=${elapsed}ms, reqId=${requestId ?? 'N/A'}`); |
| 66 | + return { |
| 67 | + anchorPoints: resp.anchor_points.map((ap) => ({ |
| 68 | + concept: ap.concept, |
| 69 | + association: ap.association, |
| 70 | + memoryTechnique: ap.memory_technique, |
| 71 | + importance: ap.importance, |
| 72 | + })), |
| 73 | + status: resp.status, |
| 74 | + model: resp.model, |
| 75 | + tokensUsed: resp.tokens_used, |
| 76 | + latencyMs: resp.latency_ms, |
| 77 | + requestId, |
| 78 | + }; |
| 79 | + } catch (err) { |
| 80 | + const elapsed = Date.now() - startMs; |
| 81 | + const error = err instanceof Error ? err : new Error(String(err)); |
| 82 | + logger.error(`[AI] [anchor-point] ✖ Failed after ${elapsed}ms: ${error.message}`); |
| 83 | + if (error.cause) logger.error(`[AI] [anchor-point] Error cause: ${error.cause}`); |
| 84 | + throw error; |
| 85 | + } |
| 86 | + }, |
| 87 | + ); |
| 88 | +} |
| 89 | + |
| 90 | +// ================================================================ |
| 91 | +// 功能定义导出 |
| 92 | +// ================================================================ |
| 93 | + |
| 94 | +export const feature: AIFeatureDef = { |
| 95 | + id: 'ai_anchor_point', |
| 96 | + name: 'AI 记忆锚点', |
| 97 | + version: '1.0.0', |
| 98 | + register, |
| 99 | +}; |
0 commit comments