@dobbylab/liftlens-agent 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/server.mjs ADDED
@@ -0,0 +1,283 @@
1
+ // LiftLens local agent bridge.
2
+ //
3
+ // The web app never talks to a model provider directly. Instead this tiny
4
+ // daemon runs on the user's machine, and drives the
5
+ // coding agent the user already has — Claude Code (`claude -p`) — on the
6
+ // user's own subscription. Coach personas are Agent Skills (coaches/<id>/SKILL.md).
7
+ //
8
+ // Security: binds to 127.0.0.1 only, restricts CORS to known web origins and
9
+ // requires a pairing token (printed on start, stored in ~/.liftlens/token).
10
+
11
+ import { serve } from '@hono/node-server'
12
+ import { Hono } from 'hono'
13
+ import { cors } from 'hono/cors'
14
+ import { streamSSE } from 'hono/streaming'
15
+ import matter from 'gray-matter'
16
+ import { spawn } from 'node:child_process'
17
+ import fs from 'node:fs'
18
+ import os from 'node:os'
19
+ import path from 'node:path'
20
+ import { sessionSchema, progressSchema } from './schemas.mjs'
21
+ import { BUILD, MODEL, ORIGINS, PORT, VERSION, WEB_URL, detectRuntimes, readToken } from './config.mjs'
22
+ import { coachesDir, syncCoaches, syncState } from './sync.mjs'
23
+ import { currentRuntimeId, listRuntimes, readConfig, runtimeById, writeConfig } from './runtimes.mjs'
24
+ import { runWithAiSdk } from './ai-run.mjs'
25
+ import { runWithCli } from './cli-run.mjs'
26
+
27
+ const TOKEN = readToken()
28
+ const runtimes = detectRuntimes()
29
+
30
+ // ---------- coaches (Agent Skills) ----------
31
+ function loadCoaches() {
32
+ const COACHES_DIR = coachesDir()
33
+ if (!fs.existsSync(COACHES_DIR)) return []
34
+ return fs
35
+ .readdirSync(COACHES_DIR, { withFileTypes: true })
36
+ .filter((d) => d.isDirectory() && !d.name.startsWith('_'))
37
+ .map((d) => {
38
+ const file = path.join(COACHES_DIR, d.name, 'SKILL.md')
39
+ if (!fs.existsSync(file)) return null
40
+ const { data } = matter(fs.readFileSync(file, 'utf8'))
41
+ return {
42
+ id: d.name,
43
+ name: data.name ?? d.name,
44
+ description: data.description ?? '',
45
+ ...(data.metadata ?? {}),
46
+ }
47
+ })
48
+ .filter(Boolean)
49
+ }
50
+ function loadSources() {
51
+ const f = path.join(coachesDir(), '_shared', 'sources.json')
52
+ return fs.existsSync(f) ? JSON.parse(fs.readFileSync(f, 'utf8')) : []
53
+ }
54
+
55
+ // ---------- app ----------
56
+ const app = new Hono()
57
+ app.use(
58
+ '*',
59
+ cors({
60
+ origin: (origin) => (ORIGINS.includes(origin) ? origin : null),
61
+ allowHeaders: ['content-type', 'x-liftlens-token'],
62
+ allowMethods: ['GET', 'POST', 'OPTIONS'],
63
+ }),
64
+ )
65
+ app.use('*', async (c, next) => {
66
+ if (c.req.method === 'OPTIONS' || c.req.path === '/health') return next()
67
+ if (c.req.header('x-liftlens-token') !== TOKEN) return c.json({ error: 'bad token' }, 401)
68
+ return next()
69
+ })
70
+
71
+ app.get('/health', (c) =>
72
+ c.json({ ok: true, name: 'liftlens-agent', version: VERSION, build: BUILD, web: WEB_URL, runtimes, runtime: currentRuntimeId(), coachesDir: coachesDir(), skills: { ...syncState } }),
73
+ )
74
+ app.get('/coaches', (c) => c.json(loadCoaches()))
75
+ /** POST /shutdown — a newer version asks the old process to free the port on startup (requires the pairing token, only available locally) */
76
+ app.post('/shutdown', (c) => {
77
+ setTimeout(() => process.exit(0), 150)
78
+ return c.json({ ok: true })
79
+ })
80
+
81
+ /** GET /runtimes — every supported agent/model runtime, availability, current default (no secrets) */
82
+ app.get('/runtimes', async (c) => c.json(await listRuntimes()))
83
+ /** POST /runtime {id} — choose the default runtime. Keys/models are configured on this machine only. */
84
+ app.post('/runtime', async (c) => {
85
+ const { id } = await c.req.json()
86
+ if (!runtimeById(id)) return c.json({ error: '未知的运行时' }, 400)
87
+ writeConfig({ ...readConfig(), runtime: id })
88
+ return c.json({ ok: true, runtime: id })
89
+ })
90
+
91
+ /**
92
+ * POST /analyze (SSE)
93
+ * body: { kind: 'session'|'progress', coachId, payload: object, images: [{name, dataUrl}] }
94
+ * events: status {stage, text} · tool {name, input} · text {text} · result {...} · error {message}
95
+ */
96
+ app.post('/analyze', async (c) => {
97
+ const body = await c.req.json()
98
+ const runtimeId = body.runtime ?? currentRuntimeId()
99
+ if (runtimeId !== 'claude-code') return analyzeWithAiSdk(c, body, runtimeId)
100
+ if (!runtimes.claude) return c.json({ error: '未找到 claude CLI,请先安装 Claude Code' }, 500)
101
+ await syncCoaches()
102
+ const COACHES_DIR = coachesDir()
103
+ const coaches = loadCoaches()
104
+ const coach = coaches.find((x) => x.id === body.coachId) ?? coaches[0]
105
+ if (!coach) return c.json({ error: '没有可用的教练 skill' }, 500)
106
+
107
+ // Workspace for this run: metrics + keyframes the agent can Read.
108
+ const runDir = fs.mkdtempSync(path.join(os.tmpdir(), 'liftlens-'))
109
+ const imageNames = []
110
+ for (const img of (body.images ?? []).slice(0, 8)) {
111
+ const m = /^data:image\/(png|jpe?g|webp);base64,(.+)$/.exec(img.dataUrl ?? '')
112
+ if (!m) continue
113
+ const name = (img.name ?? `frame-${imageNames.length}`).replace(/[^\w.-]/g, '_') + '.' + m[1].replace('jpeg', 'jpg')
114
+ fs.writeFileSync(path.join(runDir, name), Buffer.from(m[2], 'base64'))
115
+ imageNames.push(name)
116
+ }
117
+ fs.writeFileSync(path.join(runDir, 'payload.json'), JSON.stringify(body.payload ?? {}, null, 2))
118
+
119
+ const sources = loadSources()
120
+ const kind = body.kind === 'progress' ? 'progress' : 'session'
121
+ const schema = kind === 'progress' ? progressSchema : sessionSchema
122
+ const skillDir = path.join(COACHES_DIR, coach.id)
123
+ const sharedDir = path.join(COACHES_DIR, '_shared')
124
+
125
+ const prompt = [
126
+ kind === 'session'
127
+ ? `请以你的教练身份分析这次训练视频的动作质量。`
128
+ : `请以你的教练身份分析这位用户一段时间的训练记录,评估能力变化并给出计划。`,
129
+ `工作目录:${runDir}`,
130
+ `1. 先 Read ${path.join(runDir, 'payload.json')}(程序测量的指标、规则判定结果、用户信息)。`,
131
+ imageNames.length
132
+ ? `2. 再逐张 Read 关键帧(已叠加骨架和杠铃轨迹):${imageNames.map((n) => path.join(runDir, n)).join('、')}。`
133
+ : `2. 本次没有关键帧,只基于数据分析。`,
134
+ `0. 先 Read ${path.join(sharedDir, 'protocol.md')}(通用协议)与 ${path.join(sharedDir, 'metrics.md')}(指标含义与依据)。`,
135
+ `3. 需要时可 Read ${skillDir}/references/ 下你的流派标准文档。`,
136
+ `程序指标来自单目 2D 姿态估计,可能因遮挡(杠铃片挡住手臂)而失真:请用关键帧交叉核对,不一致的写进 metricDisagreements。`,
137
+ `引用依据时 sourceIds 只能使用以下 id:${sources.map((s) => s.id).join(', ')}。`,
138
+ `全部用简体中文输出,按 JSON schema 返回。`,
139
+ ].join('\n')
140
+
141
+ const args = [
142
+ '-p',
143
+ prompt,
144
+ '--output-format',
145
+ 'stream-json',
146
+ '--verbose',
147
+ '--no-session-persistence',
148
+ '--tools',
149
+ 'Read',
150
+ '--allowedTools',
151
+ 'Read',
152
+ '--add-dir',
153
+ runDir,
154
+ '--add-dir',
155
+ COACHES_DIR,
156
+ '--append-system-prompt-file',
157
+ path.join(skillDir, 'SKILL.md'),
158
+ '--json-schema',
159
+ JSON.stringify(schema),
160
+ ]
161
+ if (MODEL) args.push('--model', MODEL)
162
+
163
+ return streamSSE(c, async (stream) => {
164
+ const send = (event, data) => stream.writeSSE({ event, data: JSON.stringify(data) })
165
+ await send('status', { stage: 'spawn', text: `唤起本地 Claude Code · 教练:${coach.name}` })
166
+
167
+ let child
168
+ try {
169
+ child = spawn(runtimes.claude.path, args, { cwd: runDir, env: process.env })
170
+ } catch (err) {
171
+ fs.rmSync(runDir, { recursive: true, force: true })
172
+ await send('error', { message: `无法启动 claude:${err.message}` })
173
+ return
174
+ }
175
+ child.on('error', (err) => send('error', { message: `claude 进程错误:${err.message}` }))
176
+ stream.onAbort(() => child.kill('SIGTERM'))
177
+
178
+ let buf = ''
179
+ let final = null
180
+ let stderr = ''
181
+ child.stderr.on('data', (d) => (stderr += d))
182
+
183
+ const done = new Promise((resolve) => child.on('close', resolve))
184
+ child.stdout.on('data', async (chunk) => {
185
+ buf += chunk
186
+ let i
187
+ while ((i = buf.indexOf('\n')) >= 0) {
188
+ const line = buf.slice(0, i).trim()
189
+ buf = buf.slice(i + 1)
190
+ if (!line) continue
191
+ let ev
192
+ try {
193
+ ev = JSON.parse(line)
194
+ } catch {
195
+ continue
196
+ }
197
+ if (ev.type === 'system' && ev.subtype === 'init') {
198
+ send('status', { stage: 'init', text: `模型 ${ev.model ?? ''} 已就绪` })
199
+ } else if (ev.type === 'assistant') {
200
+ for (const part of ev.message?.content ?? []) {
201
+ if (part.type === 'tool_use' && part.name === 'Read') {
202
+ send('tool', { name: 'Read', file: path.basename(part.input?.file_path ?? '') })
203
+ } else if (part.type === 'text' && part.text?.trim()) {
204
+ send('text', { text: part.text.trim().slice(0, 400) })
205
+ } else if (part.type === 'thinking') {
206
+ send('status', { stage: 'thinking', text: '教练正在思考…' })
207
+ }
208
+ }
209
+ } else if (ev.type === 'result') {
210
+ final = ev
211
+ }
212
+ }
213
+ })
214
+
215
+ const code = await done
216
+ fs.rmSync(runDir, { recursive: true, force: true })
217
+ if (final && !final.is_error && final.structured_output) {
218
+ await send('result', {
219
+ kind,
220
+ coachId: coach.id,
221
+ output: final.structured_output,
222
+ costUsd: final.total_cost_usd ?? null,
223
+ durationMs: final.duration_ms ?? null,
224
+ })
225
+ } else {
226
+ await send('error', {
227
+ message: final?.result || stderr.slice(-600) || `claude 退出码 ${code}`,
228
+ })
229
+ }
230
+ })
231
+ })
232
+
233
+ /** Any non-Claude-Code runtime: Vercel AI SDK + provider (installed on first use). */
234
+ async function analyzeWithAiSdk(c, body, runtimeId) {
235
+ await syncCoaches()
236
+ const COACHES_DIR = coachesDir()
237
+ const coaches = loadCoaches()
238
+ const coach = coaches.find((x) => x.id === body.coachId) ?? coaches[0]
239
+ if (!coach) return c.json({ error: '没有可用的教练 skill' }, 500)
240
+ const kind = body.kind === 'progress' ? 'progress' : 'session'
241
+ const images = []
242
+ for (const img of (body.images ?? []).slice(0, 8)) {
243
+ const m = /^data:(image\/(?:png|jpe?g|webp));base64,(.+)$/.exec(img.dataUrl ?? '')
244
+ if (m) images.push({ name: img.name ?? `frame-${images.length}`, mediaType: m[1], data: Buffer.from(m[2], 'base64') })
245
+ }
246
+ return streamSSE(c, async (stream) => {
247
+ const send = (event, data) => stream.writeSSE({ event, data: JSON.stringify(data) })
248
+ const ac = new AbortController()
249
+ stream.onAbort(() => ac.abort())
250
+ await send('status', { stage: 'spawn', text: `运行时:${runtimeById(runtimeId)?.label ?? runtimeId} · 教练:${coach.displayName ?? coach.name}` })
251
+ try {
252
+ const run = runtimeById(runtimeId)?.engine === 'cli' ? runWithCli : runWithAiSdk
253
+ const r = await run({
254
+ runtimeId,
255
+ coachesDir: COACHES_DIR,
256
+ coachId: coach.id,
257
+ kind,
258
+ payload: body.payload ?? {},
259
+ images,
260
+ schema: kind === 'progress' ? progressSchema : sessionSchema,
261
+ sourceIds: loadSources().map((s) => s.id),
262
+ signal: ac.signal,
263
+ onStatus: (text) => send('status', { stage: 'run', text }),
264
+ })
265
+ await send('result', { kind, coachId: coach.id, output: r.output, costUsd: null, durationMs: r.durationMs, runtime: runtimeId, model: r.modelId })
266
+ } catch (e) {
267
+ await send('error', { message: e.message })
268
+ }
269
+ })
270
+ }
271
+
272
+ export { loadCoaches, runtimes }
273
+
274
+ /** Start the HTTP bridge on 127.0.0.1 only. Resolves once listening. */
275
+ export function startServer() {
276
+ return new Promise((resolve, reject) => {
277
+ const server = serve({ fetch: app.fetch, port: PORT, hostname: '127.0.0.1' }, () => {
278
+ syncCoaches({ force: true }).then((r) => r.updated && console.log(`[skills] 已同步到 ${r.version}(之前 ${r.from})`))
279
+ resolve(server)
280
+ })
281
+ server.on('error', reject)
282
+ })
283
+ }
package/src/sync.mjs ADDED
@@ -0,0 +1,110 @@
1
+ // Coach-skill sync: the site publishes coaches/ + manifest.json on every deploy;
2
+ // the local agent pulls changes so skill updates reach users without reinstalling.
3
+ //
4
+ // Layout: ~/.liftlens/coaches/<version>/... (one dir per manifest version)
5
+ // ~/.liftlens/coaches/current (text file with the active version)
6
+ // A version only becomes "current" after every file downloaded and matched its sha256.
7
+
8
+ import crypto from 'node:crypto'
9
+ import fs from 'node:fs'
10
+ import path from 'node:path'
11
+ import { BUNDLED_COACHES_DIR, HOME, IS_DEV_WEB, WEB_URL } from './config.mjs'
12
+
13
+ const ROOT = path.join(HOME, 'coaches')
14
+ const CURRENT = path.join(ROOT, 'current')
15
+ const MIN_INTERVAL_MS = 10 * 60 * 1000
16
+
17
+ let lastCheck = 0
18
+ let inflight = null
19
+ export const syncState = { version: null, source: 'bundled', lastError: null, checkedAt: null }
20
+
21
+ function currentDir() {
22
+ try {
23
+ const v = fs.readFileSync(CURRENT, 'utf8').trim()
24
+ const dir = path.join(ROOT, v)
25
+ if (v && fs.existsSync(path.join(dir, 'manifest.json'))) return { v, dir }
26
+ } catch {
27
+ /* none yet */
28
+ }
29
+ return null
30
+ }
31
+
32
+ /** Directory the agent should load skills from right now. */
33
+ export function coachesDir() {
34
+ const cur = IS_DEV_WEB ? null : currentDir()
35
+ if (cur) {
36
+ syncState.version = cur.v
37
+ syncState.source = 'synced'
38
+ return cur.dir
39
+ }
40
+ syncState.source = 'bundled'
41
+ return BUNDLED_COACHES_DIR
42
+ }
43
+
44
+ async function doSync() {
45
+ const base = `${WEB_URL}/coaches`
46
+ const res = await fetch(`${base}/manifest.json`, { signal: AbortSignal.timeout(5000), cache: 'no-store' })
47
+ if (!res.ok) throw new Error(`manifest HTTP ${res.status}`)
48
+ const manifest = await res.json()
49
+ if (!manifest?.version || !Array.isArray(manifest.files)) throw new Error('manifest 格式不对')
50
+ const cur = currentDir()
51
+ if (cur?.v === manifest.version) return { updated: false, version: manifest.version }
52
+
53
+ const target = path.join(ROOT, manifest.version)
54
+ const tmp = `${target}.partial-${process.pid}`
55
+ fs.rmSync(tmp, { recursive: true, force: true })
56
+ for (const f of manifest.files) {
57
+ if (f.path.includes('..') || path.isAbsolute(f.path)) throw new Error(`非法路径 ${f.path}`)
58
+ // reuse unchanged files from the current version instead of downloading again
59
+ let buf = null
60
+ if (cur) {
61
+ const old = path.join(cur.dir, f.path)
62
+ if (fs.existsSync(old)) {
63
+ const b = fs.readFileSync(old)
64
+ if (crypto.createHash('sha256').update(b).digest('hex') === f.sha256) buf = b
65
+ }
66
+ }
67
+ if (!buf) {
68
+ const r = await fetch(`${base}/${f.path.split('/').map(encodeURIComponent).join('/')}`, { signal: AbortSignal.timeout(10000) })
69
+ if (!r.ok) throw new Error(`${f.path} HTTP ${r.status}`)
70
+ buf = Buffer.from(await r.arrayBuffer())
71
+ if (crypto.createHash('sha256').update(buf).digest('hex') !== f.sha256) throw new Error(`${f.path} 校验失败`)
72
+ }
73
+ fs.mkdirSync(path.dirname(path.join(tmp, f.path)), { recursive: true })
74
+ fs.writeFileSync(path.join(tmp, f.path), buf)
75
+ }
76
+ fs.writeFileSync(path.join(tmp, 'manifest.json'), JSON.stringify(manifest, null, 2))
77
+ fs.rmSync(target, { recursive: true, force: true })
78
+ fs.renameSync(tmp, target)
79
+ fs.writeFileSync(CURRENT, manifest.version)
80
+ // keep only the two newest versions
81
+ const keep = new Set([manifest.version, cur?.v])
82
+ for (const d of fs.readdirSync(ROOT)) {
83
+ const p = path.join(ROOT, d)
84
+ if (d !== 'current' && !keep.has(d) && fs.statSync(p).isDirectory()) fs.rmSync(p, { recursive: true, force: true })
85
+ }
86
+ return { updated: true, version: manifest.version, from: cur?.v ?? 'bundled' }
87
+ }
88
+
89
+ /** Check the site for newer skills (throttled). Never throws — falls back to what we have. */
90
+ export function syncCoaches({ force = false } = {}) {
91
+ if (IS_DEV_WEB) return Promise.resolve({ updated: false, skipped: true, reason: 'dev' })
92
+ if (inflight) return inflight
93
+ if (!force && Date.now() - lastCheck < MIN_INTERVAL_MS) return Promise.resolve({ updated: false, skipped: true })
94
+ lastCheck = Date.now()
95
+ fs.mkdirSync(ROOT, { recursive: true })
96
+ inflight = doSync()
97
+ .then((r) => {
98
+ syncState.lastError = null
99
+ syncState.checkedAt = new Date().toISOString()
100
+ syncState.version = r.version
101
+ return r
102
+ })
103
+ .catch((e) => {
104
+ syncState.lastError = e.message
105
+ syncState.checkedAt = new Date().toISOString()
106
+ return { updated: false, error: e.message }
107
+ })
108
+ .finally(() => (inflight = null))
109
+ return inflight
110
+ }