@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.
@@ -0,0 +1,24 @@
1
+ # NSCA 风格技术检查点(依据 nsca-technique-4e / nsca-essentials-4e)
2
+
3
+ ## 卧推
4
+ - 五点接触:头、上背/肩、臀贴凳,双脚平放地面
5
+ - 握距:略宽于肩,前臂在到底时接近垂直(正侧面看);握距 > 1.5 倍肩宽时肩部风险上升(green-comfort-2007)
6
+ - 下放:受控地下放到乳头线附近的胸部(bench.touch_point 0.2–0.5)
7
+ - 推起:杠铃从胸口推回肩关节上方,肘伸直
8
+ - 呼吸:下放吸气、推过最难点后呼气;大重量时保持腹内压
9
+
10
+ ## 深蹲
11
+ - 站距约与肩同宽或略宽,脚尖略外展;膝与脚尖方向一致(squat.valgus)
12
+ - 下蹲时髋和膝同时屈曲,杠铃保持在脚中线上方(squat.bar_path)
13
+ - 深度:至少大腿与地面平行,能力允许时可更低(schoenfeld-2010)
14
+ - 脚跟全程着地(squat.heel_lift);躯干角度与胫骨角度大致平行
15
+
16
+ ## 硬拉(传统)
17
+ - 起始:杠铃在脚中线上方,肩在杠铃略前方,背部挺直、胸抬起
18
+ - 第一拉:髋和肩同速上升(deadlift.hips_first),杠铃贴着小腿、大腿上行(deadlift.bar_drift)
19
+ - 锁定:髋膝完全伸直、肩后收,不过度后仰(deadlift.hyperextension)
20
+ - 相扑和传统的选择看个人比例与目标(escamilla-2000)
21
+
22
+ ## 渐进(acsm-2009)
23
+ - 能比目标次数多做 1–2 次 → 加重 2–10%
24
+ - 新手每周 2–3 次,中级 3–4 次,高级 4–5 次
@@ -0,0 +1,25 @@
1
+ ---
2
+ name: coach-rehab-pt
3
+ description: 康复取向物理治疗师视角的动作评估,优先识别受伤风险(肩、膝、下背),并给出更保守的动作调整与替代方案。当用户有伤病史、动作中感到疼痛,或选择"康复理疗师"教练时使用。
4
+ metadata:
5
+ displayName: 康复理疗师
6
+ region: 运动医学 · 循证
7
+ flag: "🩺"
8
+ school: 伤病预防 · 动作调整
9
+ tone: 谨慎、温和、解释机制
10
+ accent: "#ff9d5c"
11
+ sourceIds: [fees-1998, green-comfort-2007, noteboom-2024, escamilla-2001, fry-2003, bengtsson-2022]
12
+ ---
13
+
14
+ # 康复理疗师
15
+
16
+ 你以运动康复物理治疗师的思路评估动作(不是任何具体的真实人物,也**不做医学诊断**)。先读 `../_shared/protocol.md`、`../_shared/metrics.md`,按需读 `references/risk-map.md`。
17
+
18
+ ## 工作重点
19
+ 1. 先找**负荷集中在哪个关节**:卧推看肩(触胸点过高、握距过宽、肘完全外展),深蹲看膝与下背,硬拉看下背。
20
+ 2. 每条风险给出:机制(为什么会痛)→ 调整(握距、触胸点、深度、器械)→ 替代动作(例如地板卧推、箱式深蹲、六角杠硬拉)。
21
+ 3. 对"看起来不标准但不一定有害"的现象要纠偏:膝过脚尖(fry-2003)、硬拉起始腰椎前凸减少(bengtsson-2022)不应被当作错误。
22
+ 4. 如果用户 notes 里提到疼痛:summary 第一句建议先找线下医生/治疗师评估,并降低负荷。
23
+
24
+ ## 禁止
25
+ - 不下诊断、不推荐药物;不说"一定会受伤",而是"会增加 X 部位负荷"。
@@ -0,0 +1,11 @@
1
+ # 风险地图
2
+
3
+ | 现象 | 负荷部位 | 机制 | 调整 | 依据 |
4
+ |---|---|---|---|---|
5
+ | 卧推触胸点靠近锁骨 / 颈部 | 肩前侧、肩锁关节 | 肩关节在大幅外展 + 水平外展下承重 | 凳子往头方向挪,触胸点下移到乳头线;肘内收到 45–70° | fees-1998、noteboom-2024 |
6
+ | 卧推握距 > 1.5 倍肩宽 | 肩锁关节、盂肱关节 | 水平肩力矩增大 | 收窄握距到 ≤1.5 倍;配合肩胛后收 | green-comfort-2007、noteboom-2024、larsen-2021 |
7
+ | 卧推下放过深 + 肩痛史 | 肩前关节囊 | 末端伸展 | 临时限制深度(地板卧推 / 板卧推) | fees-1998 |
8
+ | 深蹲膝内扣 | 膝内侧 | 膝外翻力矩 | 降重、口令"膝盖对准脚尖"、弹力带辅助 | escamilla-2001 |
9
+ | 深蹲膝前移 | 膝前侧(髌股) | 正常现象;限制它会把负荷转移到髋与下背 | 不需要强行限制,疼痛时可用箱式深蹲 | fry-2003 |
10
+ | 硬拉臀先起 | 下背 | 变成直腿拉,力臂加长 | 降重、起杠前"把胸挺起来、腿推地" | escamilla-2000 |
11
+ | 硬拉起始腰椎前凸减少 | — | 正常范围内(约 20°) | 关注的是拉起过程中继续弯曲,而非起始的轻微变化 | bengtsson-2022 |
package/package.json CHANGED
@@ -1,6 +1,50 @@
1
1
  {
2
2
  "name": "@dobbylab/liftlens-agent",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.0",
4
+ "description": "LiftLens local coach bridge: lets the LiftLens web app use your own Claude Code / Codex / local models to review training videos (videos and logs never leave your devices)",
5
+ "main": "src/server.mjs",
6
+ "scripts": {
7
+ "start": "node src/cli.mjs",
8
+ "serve": "node src/cli.mjs serve"
9
+ },
10
+ "keywords": [
11
+ "liftlens",
12
+ "claude-code",
13
+ "fitness",
14
+ "pose",
15
+ "agent"
16
+ ],
17
+ "license": "MIT",
18
+ "dependencies": {
19
+ "@hono/node-server": "^2.1.6",
20
+ "ai": "^7.0.166",
21
+ "gray-matter": "^4.0.3",
22
+ "hono": "^4.13.17",
23
+ "undici": "^7.30.2",
24
+ "zod": "^4.6.7"
25
+ },
26
+ "type": "module",
27
+ "bin": {
28
+ "liftlens-agent": "src/cli.mjs"
29
+ },
30
+ "files": [
31
+ "src",
32
+ "coaches"
33
+ ],
34
+ "engines": {
35
+ "node": ">=20"
36
+ },
37
+ "repository": {
38
+ "type": "git",
39
+ "url": "git+https://github.com/dobbylabhq/liftlens.git",
40
+ "directory": "agent"
41
+ },
42
+ "homepage": "https://github.com/dobbylabhq/liftlens#readme",
43
+ "publishConfig": {
44
+ "access": "public"
45
+ },
46
+ "liftlens": {
47
+ "webUrl": "https://liftlens.dobbylab.com",
48
+ "build": "7d58c7c072"
49
+ }
50
+ }
package/src/ai-run.mjs ADDED
@@ -0,0 +1,122 @@
1
+ // Coach review through the Vercel AI SDK (any runtime except the native Claude Code path).
2
+ // Same inputs as the Claude Code path, but everything is inlined into one request:
3
+ // system = coach SKILL.md + shared protocol + metric glossary + the coach's reference docs
4
+ // user = instructions + payload JSON + annotated key frames (images)
5
+ // output = JSON Schema (structured output); providers that can't enforce it get a
6
+ // prompt-level schema + local parse/validate + one repair attempt.
7
+
8
+ import fs from 'node:fs'
9
+ import path from 'node:path'
10
+ import { APICallError, LoadAPIKeyError, generateText, jsonSchema, Output } from 'ai'
11
+ import { createModel } from './runtimes.mjs'
12
+
13
+ function readIf(p) {
14
+ try {
15
+ return fs.readFileSync(p, 'utf8')
16
+ } catch {
17
+ return ''
18
+ }
19
+ }
20
+
21
+ export function buildSystem(coachesDir, coachId) {
22
+ const skillDir = path.join(coachesDir, coachId)
23
+ const refs = fs.existsSync(path.join(skillDir, 'references'))
24
+ ? fs
25
+ .readdirSync(path.join(skillDir, 'references'))
26
+ .filter((f) => f.endsWith('.md'))
27
+ .map((f) => `\n\n<reference file="${f}">\n${readIf(path.join(skillDir, 'references', f))}\n</reference>`)
28
+ .join('')
29
+ : ''
30
+ return [
31
+ readIf(path.join(skillDir, 'SKILL.md')),
32
+ `\n\n<shared file="protocol.md">\n${readIf(path.join(coachesDir, '_shared', 'protocol.md'))}\n</shared>`,
33
+ `\n\n<shared file="metrics.md">\n${readIf(path.join(coachesDir, '_shared', 'metrics.md'))}\n</shared>`,
34
+ refs,
35
+ ].join('')
36
+ }
37
+
38
+ /** minimal JSON-Schema check for the fallback path: required keys + enum values (top level and cues) */
39
+ export function looksValid(obj, schema) {
40
+ if (!obj || typeof obj !== 'object') return false
41
+ for (const k of schema.required ?? []) if (!(k in obj)) return false
42
+ for (const [k, s] of Object.entries(schema.properties ?? {})) if (s.enum && k in obj && !s.enum.includes(obj[k])) return false
43
+ return true
44
+ }
45
+ export function extractJson(text) {
46
+ const t = text.trim().replace(/^```(?:json)?\s*/i, '').replace(/```\s*$/, '')
47
+ const s = t.indexOf('{')
48
+ const e = t.lastIndexOf('}')
49
+ return JSON.parse(s >= 0 && e > s ? t.slice(s, e + 1) : t)
50
+ }
51
+
52
+ /**
53
+ * @param {{ runtimeId:string, coachesDir:string, coachId:string, kind:'session'|'progress', payload:object,
54
+ * images:{name:string, data:Buffer, mediaType:string}[], schema:object, sourceIds:string[],
55
+ * signal?:AbortSignal, onStatus:(text:string)=>void }} o
56
+ */
57
+ export async function runWithAiSdk(o) {
58
+ const { model, modelId, label, noImages } = await createModel(o.runtimeId, o.onStatus)
59
+ o.onStatus(`${label} · ${modelId} 已就绪`)
60
+ // text-only models (e.g. DeepSeek) reject image parts: review from the measurements alone
61
+ if (noImages) o = { ...o, images: [] }
62
+ const system = buildSystem(o.coachesDir, o.coachId)
63
+ const instructions = [
64
+ o.kind === 'session' ? '请以你的教练身份分析这次训练视频的动作质量。' : '请以你的教练身份分析这位用户一段时间的训练记录,评估能力变化并给出计划。',
65
+ noImages
66
+ ? '下面的 payload 是程序测量的指标、规则判定结果和用户信息。这个模型看不了图,本次没有关键帧,只基于数据分析,并在结论里说明没有看画面。'
67
+ : '下面的 payload 是程序测量的指标、规则判定结果和用户信息;之后的图片是关键帧(已叠加骨架和杠铃轨迹),文件名依次为:' +
68
+ (o.images.map((i) => i.name).join('、') || '(无)') + '。',
69
+ '程序指标来自单目 2D 姿态估计,可能因遮挡失真:请用关键帧交叉核对,不一致的写进 metricDisagreements。',
70
+ `引用依据时 sourceIds 只能使用以下 id:${o.sourceIds.join(', ')}。`,
71
+ '全部用简体中文输出。',
72
+ `\n<payload>\n${JSON.stringify(o.payload, null, 2)}\n</payload>`,
73
+ ].join('\n')
74
+ const content = [
75
+ { type: 'text', text: instructions },
76
+ ...o.images.map((img) => ({ type: 'file', mediaType: img.mediaType, data: img.data })),
77
+ ]
78
+ const started = Date.now()
79
+ o.onStatus(`读取 ${o.images.length} 张关键帧与测量数据,教练分析中…`)
80
+
81
+ try {
82
+ const res = await generateText({
83
+ model,
84
+ system,
85
+ messages: [{ role: 'user', content }],
86
+ output: Output.object({ schema: jsonSchema(o.schema) }),
87
+ abortSignal: o.signal,
88
+ })
89
+ return { output: res.output, durationMs: Date.now() - started, usage: res.totalUsage ?? res.usage ?? null, modelId, label }
90
+ } catch (err) {
91
+ if (o.signal?.aborted) throw err
92
+ // auth / quota / server / network problems: report as-is, a schema-less retry won't help
93
+ if (LoadAPIKeyError.isInstance(err)) throw new Error(`${label}:没有找到 API key`)
94
+ if (APICallError.isInstance(err) && err.statusCode !== 400) {
95
+ throw new Error(`${label} 调用失败(HTTP ${err.statusCode ?? '网络错误'}):${err.message}`)
96
+ }
97
+ // Provider can't do native structured output (or returned something off-schema):
98
+ // ask for raw JSON with the schema in the prompt, validate locally, repair once.
99
+ o.onStatus('该模型不支持强制结构化输出,改用提示词约束 + 本地校验…')
100
+ const schemaText = JSON.stringify(o.schema)
101
+ let lastText = ''
102
+ for (let attempt = 0; attempt < 2; attempt++) {
103
+ const res = await generateText({
104
+ model,
105
+ system: `${system}\n\n只输出一个 JSON 对象,不要任何解释或代码块标记。它必须符合这个 JSON Schema:\n${schemaText}`,
106
+ messages: [
107
+ { role: 'user', content },
108
+ ...(attempt ? [{ role: 'assistant', content: lastText }, { role: 'user', content: '上面的输出不符合 JSON Schema(缺字段或枚举值不对)。请只输出修正后的完整 JSON。' }] : []),
109
+ ],
110
+ abortSignal: o.signal,
111
+ })
112
+ lastText = res.text
113
+ try {
114
+ const obj = extractJson(res.text)
115
+ if (looksValid(obj, o.schema)) return { output: obj, durationMs: Date.now() - started, usage: res.totalUsage ?? res.usage ?? null, modelId, label }
116
+ } catch {
117
+ /* retry */
118
+ }
119
+ }
120
+ throw new Error(`${label} 没有返回有效的结构化结果(${err.message})`)
121
+ }
122
+ }
@@ -0,0 +1,14 @@
1
+ // Content fingerprint of the agent source (src/*.mjs). The web app is built with the same
2
+ // fingerprint, so it can tell when a user's local agent is out of date — no manual version bumps.
3
+ import crypto from 'node:crypto'
4
+ import fs from 'node:fs'
5
+ import path from 'node:path'
6
+
7
+ export function agentBuildId(srcDir) {
8
+ const h = crypto.createHash('sha256')
9
+ for (const f of fs.readdirSync(srcDir).filter((n) => n.endsWith('.mjs')).sort()) {
10
+ h.update(f + '\0')
11
+ h.update(fs.readFileSync(path.join(srcDir, f)))
12
+ }
13
+ return h.digest('hex').slice(0, 10)
14
+ }
@@ -0,0 +1,107 @@
1
+ // Coach review through a coding-agent CLI the user is already logged into (Gemini CLI, Qwen Code,
2
+ // OpenCode…), run headless. These CLIs have no structured-output flag, so the coach skill, the
3
+ // payload and the JSON Schema all go into the prompt; key frames are attached as files; the JSON
4
+ // is extracted from whatever text comes back and validated locally (one repair round).
5
+
6
+ import { spawn } from 'node:child_process'
7
+ import fs from 'node:fs'
8
+ import os from 'node:os'
9
+ import path from 'node:path'
10
+ import { buildSystem, extractJson, looksValid } from './ai-run.mjs'
11
+ import { runtimeById } from './runtimes.mjs'
12
+
13
+ const TIMEOUT_MS = 6 * 60 * 1000
14
+
15
+ function run(bin, args, cwd, signal) {
16
+ return new Promise((resolve, reject) => {
17
+ const child = spawn(bin, args, { cwd, env: { ...process.env, NO_COLOR: '1', CI: '1' }, stdio: ['ignore', 'pipe', 'pipe'] })
18
+ let out = ''
19
+ let err = ''
20
+ const timer = setTimeout(() => child.kill('SIGTERM'), TIMEOUT_MS)
21
+ signal?.addEventListener('abort', () => child.kill('SIGTERM'), { once: true })
22
+ child.stdout.on('data', (d) => (out += d))
23
+ child.stderr.on('data', (d) => (err += d))
24
+ child.on('error', (e) => {
25
+ clearTimeout(timer)
26
+ reject(new Error(`无法启动 ${bin}:${e.message}`))
27
+ })
28
+ child.on('close', (code) => {
29
+ clearTimeout(timer)
30
+ resolve({ code, out, err })
31
+ })
32
+ })
33
+ }
34
+
35
+ /** the last balanced {...} block in the text that parses — CLIs often print chatter around the answer */
36
+ function lastJson(text) {
37
+ try {
38
+ return extractJson(text)
39
+ } catch {
40
+ /* fall through */
41
+ }
42
+ for (let e = text.lastIndexOf('}'); e > 0; e = text.lastIndexOf('}', e - 1)) {
43
+ let depth = 0
44
+ for (let s = e; s >= 0; s--) {
45
+ if (text[s] === '}') depth++
46
+ else if (text[s] === '{' && --depth === 0) {
47
+ try {
48
+ return JSON.parse(text.slice(s, e + 1))
49
+ } catch {
50
+ break
51
+ }
52
+ }
53
+ }
54
+ }
55
+ return null
56
+ }
57
+
58
+ /**
59
+ * @param {{ runtimeId:string, coachesDir:string, coachId:string, kind:'session'|'progress', payload:object,
60
+ * images:{name:string, data:Buffer, mediaType:string}[], schema:object, sourceIds:string[],
61
+ * signal?:AbortSignal, onStatus:(text:string)=>void }} o
62
+ */
63
+ export async function runWithCli(o) {
64
+ const r = runtimeById(o.runtimeId)
65
+ const d = await r.detect()
66
+ if (!d.ok) throw new Error(`${r.label} 不可用:${d.reason}`)
67
+ const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'liftlens-'))
68
+ try {
69
+ const files = o.images.map((img, i) => {
70
+ const f = path.join(dir, `${String(i + 1).padStart(2, '0')}-${img.name.replace(/[^\w.-]/g, '_')}.${img.mediaType.split('/')[1].replace('jpeg', 'jpg')}`)
71
+ fs.writeFileSync(f, img.data)
72
+ return f
73
+ })
74
+ const prompt = [
75
+ buildSystem(o.coachesDir, o.coachId),
76
+ '\n\n---\n',
77
+ o.kind === 'session' ? '请以你的教练身份分析这次训练视频的动作质量。' : '请以你的教练身份分析这位用户一段时间的训练记录,评估能力变化并给出计划。',
78
+ files.length ? `附带的图片是关键帧(已叠加骨架和杠铃轨迹),依次为:${o.images.map((i) => i.name).join('、')}。` : '本次没有关键帧,只基于数据分析。',
79
+ '程序指标来自单目 2D 姿态估计,可能因遮挡失真:请用关键帧交叉核对,不一致的写进 metricDisagreements。',
80
+ `引用依据时 sourceIds 只能使用以下 id:${o.sourceIds.join(', ')}。`,
81
+ '全部用简体中文。不要修改或创建任何文件,不要运行命令。',
82
+ `最后只输出一个 JSON 对象(不要代码块标记、不要解释),必须符合这个 JSON Schema:\n${JSON.stringify(o.schema)}`,
83
+ `\n<payload>\n${JSON.stringify(o.payload, null, 2)}\n</payload>`,
84
+ ].join('\n')
85
+ const started = Date.now()
86
+ let ask = prompt
87
+ let last = ''
88
+ for (let attempt = 0; attempt < 2; attempt++) {
89
+ o.onStatus(attempt ? '结果不符合格式,让它修正一次…' : `调用 ${r.label}(${files.length} 张关键帧),教练分析中…`)
90
+ const res = await run(d.path, r.args({ prompt: ask, files, model: readModel(o.runtimeId, d) }), dir, o.signal)
91
+ if (o.signal?.aborted) throw new Error('已取消')
92
+ last = res.out
93
+ const obj = lastJson(res.out)
94
+ if (looksValid(obj, o.schema)) return { output: obj, durationMs: Date.now() - started, modelId: d.model, label: r.label }
95
+ if (res.code !== 0 && !res.out.trim()) throw new Error(`${r.label} 退出码 ${res.code}:${res.err.slice(-500) || '没有输出'}`)
96
+ ask = `${prompt}\n\n你上一次的输出不是符合 Schema 的 JSON(缺字段或枚举值不对):\n${res.out.slice(-4000)}\n\n请只输出修正后的完整 JSON 对象。`
97
+ }
98
+ throw new Error(`${r.label} 没有返回有效的结构化结果:${last.slice(-300)}`)
99
+ } finally {
100
+ fs.rmSync(dir, { recursive: true, force: true })
101
+ }
102
+ }
103
+
104
+ /** only pass --model when the user chose one (the detect() placeholder is just a label) */
105
+ function readModel(id, d) {
106
+ return d.model && !d.model.endsWith('默认模型') ? d.model : null
107
+ }