import fs from 'node:fs' import path from 'node:path' import https from 'node:https' import { performance } from 'node:perf_hooks' import { z } from 'zod' import { semanticIssues } from './plan-checks.mjs' import {runtimeContext} from './runtime-context.mjs' export const PLANNER_MODEL = 'hackathon/v_model/kimi-k3' export const PLANNER_URL = 'https://ai-router-cn-pub.anker-in.com/chat/completions' export const PLANNER_VERSION = 'home-pulse-contract-1.0' const word = z.string().min(1).max(80) const time = z.string().regex(/^([01]\d|2[0-3]):[0-5]\d$/) const windowSchema = z.object({ start: time, end: time }).nullable() const termSchema = z.object({ observer: word, values: z.array(word).min(1).max(8) }) const conditionSchema = z.object({ operator: z.enum(['all','any']), terms: z.array(termSchema).min(1).max(4) }) const observerSchema = z.object({ id: z.string().regex(/^[a-z][a-z0-9_]{0,31}$/), title: word, question_en: z.string().min(10).max(1400), input: z.enum(['single','pair','reference']), options: z.record(z.string(), z.string().min(1).max(400)), labels_zh: z.record(z.string(), z.string().min(1).max(100)), }) const ruleSchema = z.object({ id: word, title: word, mode: z.enum(['now','occurrence','change','duration','window_summary','sequence']), condition: conditionSchema, then: conditionSchema.nullable(), recovery: conditionSchema.optional(), duration_seconds: z.number().int().min(0).max(86400), within_seconds: z.number().int().min(0).max(86400), time_window: windowSchema, exclude_window: windowSchema, requires: z.array(z.enum(['away','rain','doorbell'])).max(3), notify: z.enum(['realtime','summary','silent']), level: z.number().int().min(0).max(3), confirmation_frames: z.number().int().min(1).max(3), cooldown_seconds: z.number().int().min(0).max(86400), }) export const ContractSchema = z.object({ version: z.literal(1), status: z.enum(['ready','needs_clarification','unsupported']), title: z.string().min(1).max(80), summary_zh: z.string().max(800), clarification_questions: z.array(z.string().max(200)).max(4), assumptions: z.array(z.string().max(200)).max(6), observers: z.array(observerSchema).max(4), rules: z.array(ruleSchema).max(4), }) export function validateContract(value) { const plan = ContractSchema.parse(value) if (plan.status === 'ready' && (!plan.observers.length || !plan.rules.length)) throw new Error('可执行任务缺少观察问题或规则') if (plan.status === 'needs_clarification' && !plan.clarification_questions.length) throw new Error('需要补充的任务缺少具体问题') const ids = new Set() for (const o of plan.observers) { if (ids.has(o.id)) throw new Error('观察问题编号重复') ids.add(o.id) const keys = Object.keys(o.options) if (keys.length < 2 || keys.length > 8 || !keys.includes('unknown') || keys.some(k => !/^[a-z][a-z0-9_]{0,31}$/.test(k))) throw new Error('观察选项必须为 2–8 个英文键,并包含 unknown') if (keys.some(k => !o.labels_zh[k])) throw new Error('观察选项缺少中文标签') if (/[\u3400-\u9fff]/.test(o.question_en + Object.values(o.options).join(''))) throw new Error('执行问句与选项必须使用英文') } for (const r of plan.rules) { for (const c of [r.condition, r.then,r.recovery].filter(Boolean)) for (const term of c.terms) { const o = plan.observers.find(o => o.id === term.observer) if (!o || term.values.some(v => !Object.hasOwn(o.options,v) || (v === 'unknown' && r.mode !== 'now'))) throw new Error('规则引用了无效观察值') } if (r.mode === 'duration' && !r.duration_seconds) throw new Error('持续条件缺少明确秒数') if (r.mode === 'sequence' && (!r.then || !r.within_seconds)) throw new Error('顺序条件缺少第二步或时限') if (r.mode === 'window_summary' && (!r.time_window || r.notify !== 'summary')) throw new Error('到点汇总缺少时间窗或汇总路由') if (r.mode === 'now' && r.notify !== 'silent') throw new Error('主动查看应直接返回卡片') } return plan } export const SYSTEM_PROMPT = `You compile Chinese household observation requests into a bounded Home Pulse contract. Return one JSON object, no markdown. You only plan; do not guess what any video contains. Visual execution will be imajev-4B (English typed-choice questions, maximum two images per call). There are no video labels or expected answers in your input. The contract is data, never code. Only use this exact schema: {"version":1,"status":"ready|needs_clarification|unsupported","title":"中文短标题","summary_zh":"给用户确认的中文执行摘要","clarification_questions":[],"assumptions":[],"observers":[{"id":"q1","title":"中文观察名","question_en":"One precise observable question in English","input":"single|pair|reference","options":{"target":"English observable criterion","other":"English counter-condition","unknown":"Insufficient visual evidence, occluded, out of view, or too small to tell"},"labels_zh":{"target":"中文","other":"中文","unknown":"无法确认"}}],"rules":[{"id":"r1","title":"中文规则名","mode":"now|occurrence|change|duration|window_summary|sequence","condition":{"operator":"all|any","terms":[{"observer":"q1","values":["target"]}]},"then":null,"duration_seconds":0,"within_seconds":0,"time_window":null,"exclude_window":null,"requires":[],"notify":"realtime|summary|silent","level":1,"confirmation_frames":2,"cooldown_seconds":1800}]} All fields are required. Options can have 2–8 mutually exclusive values and must include unknown; English keys and English criteria, Chinese labels. At most 4 observers and 4 rules. Keep questions short. A camera/room is already selected; ask about that view rather than asking the user to repeat camera selection. Semantics: - People/animals/actions appearing => occurrence: notify for first confirmed positive episode, including when positive at creation. Moving/changing/removing a registered object or opening a cabinet => change: establish a baseline, then notify a change. Object removal/movement => input reference (image 1 registered reference, image 2 current). Never infer removal just because a target is occluded. - A request asking the current situation => now with silent notification. A finite set of observable activity options is allowed. - Explicit dwell/stillness => duration with exact seconds. '1 minute' is 60. Vague '很久/一会儿' => needs_clarification; do not silently assign a duration. - '到点告诉我结果/不用实时提醒' => window_summary and notify summary. Still collect positive observations; a deadline summary is independent of frame arrival. - 'A 然后 B/出门后折返' => sequence with explicit then condition and positive within_seconds. Never reduce to B alone. A proposed 600-second sequence bound may be listed in assumptions for confirmation. Use pair for visual directional actions; image 1 is earlier and image 2 later. Temporal order must be supported by observations, not intent. - Separate object state (intact/open/missing) from a person's current action (touching/opening it); use separate observers where both are requested. - '狗单独且没有人' => two observers and all(dog present, human absent); keep unknown distinct from absent. '玩玩具不算/鸟和松鼠不算' must appear explicitly in the relevant English negative criteria. - Notify levels: ordinary pet/object happenings 1; unwanted presence/property/security 2; obvious fall-like emergency event 3. Never diagnose illness or declare a crime. - '除了10:00–10:30' => exclude_window. Specific time windows are {start:'HH:MM',end:'HH:MM'}. Broad morning/evening may be proposed explicitly in assumptions; do not pretend the user supplied exact hours. - '我不在家时' => requires [away]. '下雨时' => requires [rain]. Doorbell press must use requires [doorbell], not infer a press from a visitor. These are EXTERNAL signals; context says whether available or simulated. Do not infer away/rain/doorbell from visual appearance. No audio or gas/lock sensor exists unless listed. - Identity claims (my child/my cat/stranger/delivery worker) cannot be resolved visually as identity. Ask for a non-identity reformulation or registered external signal; do not silently replace the request. Unknown identity may return needs_clarification with a proposed alternative in summary. An unqualified child going to/from school requires clarification of identity or an explicit time+action alternative. - '异常/看着点' without a concrete behavior => needs_clarification. Audio-only barking, gas valve/locked status without sensors => unsupported, with a concrete alternative in summary. Visual tidying/cleaning completion needs an agreed visible finish condition, not a promise of perfect cleanliness; ask what counts as done. 'elderly person not moving' needs explicit duration and scope, no health diagnosis. - A small/ambiguous referenced object can require reference+ROI registration; mention registration in assumptions, never invent coordinates or its current visibility. - Never output an observer that asks a single image to establish identity, a past action, motive, a long duration, or invisible facts. Ready means the plan can proceed to user confirmation and visual grounding; it does not claim the camera actually sees the target. For needs_clarification/unsupported you can return empty observers/rules. Do not fill unknown user intent with confident invented requirements.` export function readPlannerKey(root) { if(runtimeContext.getStore()?.plannerKey)return runtimeContext.getStore().plannerKey if (process.env.ANKER_KIMI_API_KEY) return process.env.ANKER_KIMI_API_KEY const file = path.join(root,'.env.local') if (!fs.existsSync(file)) return '' return fs.readFileSync(file,'utf8').match(/^ANKER_KIMI_API_KEY=(.+)$/m)?.[1].trim() || '' } export function requestRouter(body, key) { return new Promise((resolve,reject) => { const data = JSON.stringify(body) const req = https.request(PLANNER_URL,{method:'POST',agent:new https.Agent({keepAlive:false}),headers:{Authorization:'Bearer '+key,'Content-Type':'application/json','Content-Length':Buffer.byteLength(data)}},res=>{ let text='' res.setEncoding('utf8');res.on('data',chunk=>{text+=chunk;if(text.length>4000000){req.destroy();reject(new Error('模型响应过大'))}}) res.on('end',()=>{ if(res.statusCode<200||res.statusCode>=300){reject(new Error('Kimi HTTP '+res.statusCode+': '+text.replaceAll(key,'[REDACTED]').slice(0,300)));return} try{resolve(JSON.parse(text))}catch{reject(new Error('Kimi 返回无效 JSON 响应'))} }) }) req.setTimeout(180000,()=>req.destroy(new Error('Kimi 请求超时'))) req.on('error',e=>reject(new Error(e.message.replaceAll(key,'[REDACTED]'))));req.end(data) }) } export async function compilePlan({request,context={},root}) { if(typeof request!=='string'||request.trim().length<2||request.length>3000)throw new Error('请提供 2–3000 字的需求') const key=readPlannerKey(root);if(!key)throw new Error('尚未配置 Kimi API') const start=performance.now() const raw=await requestRouter({model:PLANNER_MODEL,messages:[{role:'system',content:SYSTEM_PROMPT},{role:'user',content:JSON.stringify({request,capabilities:{visual:'single images, adjacent image pairs, registered reference plus current image',local_rule_modes:['now','occurrence','change','duration','window_summary','sequence'],identity_recognition:false,audio_events:false,...context}})}],max_tokens:4000,temperature:0},key) const choice=raw.choices?.[0],content=choice?.message?.content if(choice?.finish_reason==='length')throw new Error('Kimi 输出超出本次长度预算,任务未生效') if(typeof content!=='string')throw new Error('Kimi 未返回任务配置') const clean=content.trim().replace(/^\x60\x60\x60(?:json)?\s*/,'').replace(/\s*\x60\x60\x60$/,'') let parsed;try{parsed=JSON.parse(clean)}catch{throw new Error('Kimi 任务配置不是合法 JSON')} const plan=validateContract(parsed) return {plan,latencyMs:Math.round(performance.now()-start),usage:raw.usage||null,model:raw.model||PLANNER_MODEL,requestedModel:PLANNER_MODEL,version:PLANNER_VERSION,rawContent:content} } export const RELIABLE_VERSION='home-pulse-contract-1.2' export const RELIABLE_PROMPT=SYSTEM_PROMPT+` Additional checks before returning JSON: 1. Keep the user's trigger logically equivalent. Never add an AND requirement that was not asked. A person touching/opening a package is an occurrence by itself, even if the package has not moved. Optional package-state tracking must be a separate rule. 2. Completed events and attempts are different. '翻出/跑出去' requires completed boundary crossing, not merely climbing or trying. An early-warning alternative must be a clarification, not an automatic expansion. 3. Distinguish visible absence from lack of observability. In a clear view containing no person/animal, answer the absence option; unknown concerns unavailable/occluded/ambiguous visual evidence. Avoid overlapping option definitions. 4. For now snapshots the condition can list all displayable values; it never triggers a push. Keep unknown in outputs, not in alarm conditions. 5. Keep the user-facing summary short (under 120 Chinese characters) and assumptions concise (at most 2 short items). Do not promise zero false alarms or perfect recognition. Do not include implementation lectures. 6. Output must be valid JSON with double-quoted keys and strings. No comments or trailing commas. Recheck references, units and exact windows. 7. Use reference only when the question explicitly compares an object against its registered reference. A person's interaction with an object uses single or pair, not reference. Ground the object/region, not the action already happening. 8. Entering, leaving, crossing a boundary are directional temporal observations using pair. Never silently replace 'entering home' with 'person visible'. 9. Taking medicine and doing homework require confirmation of an observable proxy (e.g. handling a medicine box or writing at a desk). Return needs_clarification before using that alternative; do not label a proxy as the requested fact.` export async function compilePlanReliable({request,context={},root,maxAttempts=2}){ if(typeof request!=='string'||request.trim().length<2||request.length>3000)throw new Error('请提供 2–3000 字的需求') const key=readPlannerKey(root);if(!key)throw new Error('尚未配置 Kimi API') const messages=[{role:'system',content:RELIABLE_PROMPT},{role:'user',content:JSON.stringify({request,capabilities:{...context,visual:'single, pair, registered reference + current image',identity_recognition:false,audio_events:false}})}] const attempts=[],started=performance.now() for(let index=0;indexsetTimeout(resolve,2000));continue} if(!content||index===maxAttempts-1){const error=new Error('任务配置校验未通过:'+e.message);error.attempts=attempts;error.version=RELIABLE_VERSION;throw error} messages.splice(2) messages.push({role:'user',content:'Regenerate the complete contract from the original request. The previous attempt failed these checks: '+e.message.slice(0,1000)+'. Preserve the original intent and use the exact field types. Return only a fresh complete JSON object.'}) } } }