Files
LokiStack/frontend/src/lib/ai-assistant-context.ts
T
opsx c216bdfdd4
CI / Backend (go vet + test) (push) Waiting to run
CI / Frontend (lint + build + test) (push) Waiting to run
feat(ai): 助手浮窗聚焦日志查询页,携带当前查询结果对话
- 浮球只在 /logs 渲染(产品定位:辅助分析正在查看的查询结果,其它页面不打扰)
- /logs 查询成功后写入 AI 上下文:LogQL、时间范围、结果条数、error 行数、
  最新 20 条样本(去 CRI 包装);对话请求自动携带
- 后端 /ai/chat 接收 context:截断 6000 字 + RedactTextOnly 统一脱敏后并入 system
  (ECHO 假模型实测注入完整);空态文案改为查询导向
2026-11-01 00:31:48 +08:00

33 lines
1.2 KiB
TypeScript

// AI 助手页面上下文 — 模块级单例(避免为一个字符串加全局 Provider):
// /logs 页在查询成功后把当前查询与结果摘要写入,AiAssistant 发送时带上,
// 后端注入 system,让"刚才查到的结果有什么问题"有真实数据依据。
export interface LogsAIContext {
query: string
rangeMinutes: number
totalEntries: number
errorLines: number
samples: string[]
}
let logsContext: LogsAIContext | null = null
export function setLogsAIContext(ctx: LogsAIContext | null) {
logsContext = ctx
}
export function getLogsAIContext() {
return logsContext
}
/** 组装给后端的上下文摘要(截断保护:样本行与总量都封顶) */
export function buildLogsContextText(): string {
if (!logsContext) return ''
const { query, rangeMinutes, totalEntries, errorLines, samples } = logsContext
const lines = samples.slice(0, 20).map(s => s.slice(0, 300))
return [
`用户刚执行的 LogQL 查询: ${query}`,
`时间范围: 近 ${rangeMinutes} 分钟;加载 ${totalEntries} 条(首页),其中含 error 关键字约 ${errorLines} 条`,
lines.length ? `样本日志(最新在前,已截断):\n${lines.join('\n')}` : '',
].filter(Boolean).join('\n')
}