File size: 14,544 Bytes
90d354b
 
 
 
 
 
 
133b124
 
 
90d354b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
133b124
 
 
 
90d354b
 
133b124
90d354b
 
133b124
 
90d354b
 
 
 
 
 
 
133b124
90d354b
 
 
 
 
133b124
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
90d354b
 
 
 
 
133b124
90d354b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import fs from 'node:fs'
import path from 'node:path'
import { randomUUID } from 'node:crypto'
import { performance } from 'node:perf_hooks'
import { compilePlanReliable, readPlannerKey, PLANNER_MODEL } from './planner.mjs'
import {createGroundedService} from './grounded-service.mjs'
import {createMediaLibrary} from './media-library.mjs'
import {modelUrl,modelHeaders,cloudMode,runtimeContext} from './runtime-context.mjs'
import {createAppApi,appPublic,appToken,appError,AppError,readAppBody} from './app-api.mjs'
import {writeAtomic} from './atomic-file.mjs'

const TZ = 'Asia/Shanghai'
export function dayKey(value) {
  return new Intl.DateTimeFormat('en-CA', { timeZone: TZ, year: 'numeric', month: '2-digit', day: '2-digit' }).format(new Date(value))
}
export function percentile(values, p) {
  if (!values.length) return null
  const sorted = [...values].sort((a, b) => a - b)
  return Math.round(sorted[Math.max(0, Math.ceil(sorted.length * p) - 1)])
}
export function summarize(records, range = 'today', now = new Date()) {
  const today = dayKey(now), weekStart = dayKey(new Date(now.getTime() - 6 * 86400000))
  const selected = records.filter(r => range === 'all' || (range === 'week' ? dayKey(r.startedAt) >= weekStart && dayKey(r.startedAt) <= today : dayKey(r.startedAt) === today))
  const complete = selected.filter(r => r.status !== 'running' && r.modelAttempt)
  const valid = complete.filter(r => r.status === 'success' || r.status === 'unknown')
  const errors = complete.filter(r => r.status === 'error'), unknown = complete.filter(r => r.status === 'unknown')
  const latencies = valid.map(r => r.latencyMs).filter(Number.isFinite)
  const ratio = n => complete.length ? +(n / complete.length * 100).toFixed(1) : null
  const hourly = Array.from({ length: 24 }, (_, hour) => ({ hour, captures: 0, requests: 0, errors: 0, latencySum: 0, latencyCount: 0 }))
  for (const r of selected) {
    const h = Number(new Intl.DateTimeFormat('en-GB', { timeZone: TZ, hour: '2-digit', hourCycle: 'h23' }).format(new Date(r.startedAt)))
    hourly[h].captures += r.captureCount || 0
    hourly[h].requests += r.modelAttempt ? 1 : 0
    hourly[h].errors += r.status === 'error' ? 1 : 0
    if (Number.isFinite(r.latencyMs) && r.status !== 'error') { hourly[h].latencySum += r.latencyMs; hourly[h].latencyCount++ }
  }
  const failures = Object.entries(selected.filter(r => r.status === 'error').reduce((acc, r) => { acc[r.error || '请求失败'] = (acc[r.error || '请求失败'] || 0) + 1; return acc }, {})).map(([reason, count]) => ({ reason, count }))
  return {
    range, timezone: TZ, date: today, captures: selected.reduce((s, r) => s + (r.captureCount || 0), 0),
    requests: selected.filter(r => r.modelAttempt).length, completed: complete.length,
    succeeded: valid.length, failed: errors.length, unknown: unknown.length,
    captureFailures: selected.filter(r => !r.modelAttempt && r.status === 'error').length,
    successRate: ratio(valid.length), failureRate: ratio(errors.length), unknownRate: ratio(unknown.length),
    decisions: complete.filter(r => r.kind === 'observation').length,
    p50: percentile(latencies, .5), p95: percentile(latencies, .95),
    average: latencies.length ? Math.round(latencies.reduce((a, b) => a + b, 0) / latencies.length) : null,
    stateChanges: selected.filter(r => r.stateChanged).length,
    notifications: selected.filter(r => r.notified).length,
    inFlight: selected.filter(r => r.status === 'running').length,
    hourly: hourly.map(h => ({ ...h, latency: h.latencyCount ? Math.round(h.latencySum / h.latencyCount) : null })),
    failures, records: [...selected].reverse().slice(0, 300), totalRecords: selected.length,
  }
}

export class Journal {
  constructor(file) {
    this.file = file
    this.records = new Map()
    fs.mkdirSync(path.dirname(file), { recursive: true })
    if (fs.existsSync(file)) for (const line of fs.readFileSync(file, 'utf8').split('\n')) {
      try { const { id, ...patch } = JSON.parse(line); this.records.set(id, { ...this.records.get(id), ...patch, id }) } catch { /* Ignore incomplete trailing writes. */ }
    }
    for (const r of this.records.values()) if (r.status === 'running') this.update(r.id, { status: 'error', error: '服务重启,请重新取证', finishedAt: new Date().toISOString() })
  }
  update(id, patch) {
    const record = { ...this.records.get(id), ...patch, id }
    fs.appendFileSync(this.file, JSON.stringify({ id, ...patch }) + '\n', 'utf8')
    this.records.set(id, record)
    return record
  }
  list() { return [...this.records.values()] }
  removeWhere(predicate) {
    for(const [id,row] of this.records)if(predicate(row))this.records.delete(id)
    writeAtomic(this.file,this.list().map(r=>JSON.stringify(r)).join('\n')+'\n')
  }
}

export function localApiPlugin(root,{appScoped=false}={}) {
  let journal
  let queue = Promise.resolve()
  let grounded,media,app
  const nativeWorkspaces=new Map()
  const getJournal = () => journal ||= new Journal(path.join(root, '.local', 'telemetry.jsonl'))
  const send = (res, code, data) => { res.statusCode = code; res.setHeader('Content-Type', 'application/json; charset=utf-8'); res.setHeader('Cache-Control', 'no-store'); res.end(JSON.stringify(data)) }
  const read = async req => {
    let size = 0; const chunks = []
    for await (const chunk of req) { size += chunk.length; if (size > 6 * 1024 * 1024) throw new Error('图片过大,请使用 6 MB 以内的画面'); chunks.push(chunk) }
    return JSON.parse(Buffer.concat(chunks).toString('utf8') || '{}')
  }
  const getGrounded=()=>grounded ||= createGroundedService(root,{journal:getJournal,enqueue:task=>{const pending=queue.then(task,task);queue=pending.catch(()=>{});return pending}})
  const middleware = async (req, res, next) => {
    if (!req.url?.startsWith('/api/')) return next()
    const url = new URL(req.url, 'http://localhost')
    if (req.headers.origin && req.headers.origin !== `http://${req.headers.host}` && req.headers.origin !== `https://${req.headers.host}`) return send(res, 403, { error: '来源不匹配' })
    try {
      if(url.pathname.startsWith('/api/v1/')){
        const publicResult=appPublic(req,url)
        if(publicResult)return send(res,publicResult.status,publicResult.body)
        const token=appToken(req)
        if(!token){const error=appError(new AppError(401,'UNAUTHORIZED','请提供 Home Pulse 空间令牌'));return send(res,error.status,error.body)}
        if(!appScoped){
          let middleware=nativeWorkspaces.get(token)
          if(!middleware){
            if(nativeWorkspaces.size>=2000){const error=appError(new AppError(503,'WORKSPACE_LIMIT','当前连接数已满'));return send(res,error.status,error.body)}
            localApiPlugin(path.join(root,'.local','app-workspaces',token),{appScoped:true}).configureServer({middlewares:{use:m=>middleware=m}})
            nativeWorkspaces.set(token,middleware)
          }
          return runtimeContext.run({...runtimeContext.getStore(),plannerKey:readPlannerKey(root)},()=>middleware(req,res,next))
        }
        app ||= createAppApi(root,{grounded:getGrounded,journal:getJournal,metrics:()=>summarize(getJournal().list(),'today')})
        try{
          const result=await app.handle(req,url,req.method==='GET'?{}:await readAppBody(req),token)
          return send(res,result.status,result.body)
        }catch(e){const error=appError(e);return send(res,error.status,error.body)}
      }
      if(url.pathname.startsWith('/api/media/')){
        media ||= createMediaLibrary(root)
        if(await media.handle(req,res,url))return
      }
      if(/^\/api\/(setup\/|contracts\/|memories(?:\/|$)|grounded\/)/.test(url.pathname)){
        grounded=getGrounded()
        if(req.method==='GET'&&url.pathname.startsWith('/api/grounded/image/')){
          const [, , , ,kind,id,field]=url.pathname.split('/')
          const value=grounded.getImage(kind,id,field),match=value.match(/^data:(image\/[a-z]+);base64,(.+)$/)
          if(!match)throw new Error('图片不存在')
          res.setHeader('Content-Type',match[1]);res.setHeader('Cache-Control','no-store');return res.end(Buffer.from(match[2],'base64'))
        }
        if(grounded.handles(url))return send(res,200,await grounded.handle(req.method,url,req.method==='GET'?{}:await read(req)))
      }
      if (req.method === 'GET' && url.pathname === '/api/health') {
        let runtime = null
        try { const r = JSON.parse(fs.readFileSync(path.join(root, '..', 'imajev-lab', 'results', 'runtime_info.json'), 'utf8')); runtime = { model: r.model, gpu: r.gpu, memoryGiB: r.allocated_gib_after_load, quantization: 'NF4 · 4-bit', maxImageSide: r.max_image_side } } catch { /* Optional model metadata. */ }
        let ready = false
        try { const r = await fetch(modelUrl('/v1/models'), { signal: AbortSignal.timeout(3000) }); const data=await r.json(); ready=r.ok&&JSON.stringify(data).toLowerCase().includes('imajev'); if(data.runtime)runtime=data.runtime } catch { /* Offline is a normal state. */ }
        return send(res, 200, { ready, name: 'imajev-4B', deployment:cloudMode()?'hf':'local', runtime, planner: { configured: !!readPlannerKey(root), model: PLANNER_MODEL }, inFlight: getJournal().list().filter(r => r.status === 'running').length, serverTime: new Date().toISOString() })
      }
      if (req.method === 'GET' && url.pathname === '/api/metrics') return send(res, 200, summarize(getJournal().list(), url.searchParams.get('range') || 'today'))
      if (req.method === 'POST' && url.pathname === '/api/plan') {
        const body = await read(req)
        const result = await compilePlanReliable({request:body.request,context:body.context||{},root})
        const {rawContent,attempts,...publicResult}=result
        return send(res,200,publicResult)
      }
      if (req.method === 'GET' && url.pathname === '/api/export') {
        res.setHeader('Content-Disposition', 'attachment; filename="home-pulse-telemetry.json"')
        return send(res, 200, { exportedAt: new Date().toISOString(), timezone: TZ, records: getJournal().list() })
      }
      if (req.method === 'POST' && url.pathname === '/api/capture-failure') {
        const b = await read(req)
        return send(res, 200, getJournal().update(randomUUID(), { startedAt: new Date().toISOString(), kind: 'observation', status: 'error', modelAttempt: false, captureCount: 0, error: String(b.error || '画面获取失败').slice(0, 200), itemName: String(b.itemName || ''), camera: String(b.camera || ''), source: String(b.source || '手动取证') }))
      }
      if (req.method === 'POST' && url.pathname === '/api/decision') {
        const b = await read(req), old = getJournal().records.get(b.id)
        if (!old) return send(res, 404, { error: '记录不存在' })
        if (old.committed) return send(res, 200, old)
        return send(res, 200, getJournal().update(b.id, { committed: true, stateChanged: b.stateChanged === true, notified: b.notified === true, verdict: String(b.verdict || '').slice(0, 240), engineMs: Math.max(0, Math.min(120000, Number(b.engineMs) || 0)) }))
      }
      if (req.method === 'POST' && url.pathname === '/api/analyze') {
        const body = await read(req)
        if (!body.instructions || !body.criteria || Object.keys(body.criteria).length < 2 || Object.keys(body.criteria).length > 12 || typeof body.image !== 'string' || !/^data:image\/(jpeg|png|webp);base64,/.test(body.image)) return send(res, 422, { error: '请提供有效画面与判断选项' })
        const id = randomUUID(), begin = performance.now(), meta = body.meta || {}
        getJournal().update(id, { startedAt: new Date().toISOString(), status: 'running', captureId: meta.captureId || id, captureCount: meta.captureId && getJournal().list().some(r => r.captureId === meta.captureId) ? 0 : 1, modelAttempt: true, kind: meta.kind === 'grounding' ? 'grounding' : 'observation', itemName: String(meta.itemName || '视觉观察').slice(0, 100), itemId: String(meta.itemId || '').slice(0, 100), camera: String(meta.camera || '').slice(0, 80), source: String(meta.source || '手动取证').slice(0, 80), sourceName: String(meta.sourceName || '').slice(0, 100), mediaTime: Number.isFinite(meta.mediaTime) ? meta.mediaTime : null })
        const task = async () => {
          const queueMs = Math.round(performance.now() - begin)
          try {
            const upstream = await fetch(modelUrl('/v1/systemone'), { method: 'POST', headers: modelHeaders(), body: JSON.stringify({ state: {}, questions: { q1: { type: 'choice', instructions: body.instructions, criteria: body.criteria } }, images: [body.image] }), signal: AbortSignal.timeout(120000) })
            if (!upstream.ok) { const detail=await upstream.json().catch(()=>({}));throw new Error(typeof detail.error==='string'?detail.error.slice(0,300):`模型服务返回 ${upstream.status}`) }
            const data = await upstream.json(), a = data?.answers?.q1
            if (!a || (a.abstained !== true && !(a.choice in body.criteria))) throw new Error('模型响应格式异常')
            const unknown = a.abstained || a.choice === 'unclear'
            const record = getJournal().update(id, { status: unknown ? 'unknown' : 'success', choice: a.choice ?? null, abstained: !!unknown, latencyMs: Math.round(performance.now() - begin), inferenceMs: Number.isFinite(data?.usage?.total_ms) ? Math.round(data.usage.total_ms) : null, queueMs, finishedAt: new Date().toISOString() })
            return send(res, 200, { answer: { choice: a.choice ?? null, abstained: !!unknown }, latencyMs: record.latencyMs, inferenceMs: record.inferenceMs, recordId: id })
          } catch (e) {
            const error = e.name === 'TimeoutError' ? '模型响应超时' : e.message === 'fetch failed' ? '模型服务未连接' : e.message
            const record = getJournal().update(id, { status: 'error', error, latencyMs: Math.round(performance.now() - begin), queueMs, finishedAt: new Date().toISOString() })
            return send(res, 502, { error, recordId: id, latencyMs: record.latencyMs })
          }
        }
        const pending = queue.then(task, task); queue = pending.catch(() => {}); return await pending
      }
      return send(res, 404, { error: '接口不存在' })
    } catch (e) { return send(res, 400, { error: e.message || '请求无效' }) }
  }
  return { name: 'home-pulse-local-api', configureServer(server) { server.middlewares.use(middleware) }, configurePreviewServer(server) { server.middlewares.use(middleware) } }
}