Initial commit: Clean OpenClaw Jarvis Open-Source Template
This commit is contained in:
commit
ac4601b257
28 files changed
+7718
No files matched your search
+333
@@ -0,0 +1,333 @@
|
||||
#!/usr/bin/env node
|
||||
/**
|
||||
* cat — MCP-Server fuer Jarvis: Katzen-Erkennung und Enrollment.
|
||||
*
|
||||
* Der Server ist ein duenner Client gegen den cat-service (YOLO-Detektor +
|
||||
* DINOv2-Embeddings, CPU):
|
||||
* CAT_URL Basis-URL des Dienstes (Standard http://cat-service:8080).
|
||||
*
|
||||
* Zusaetzlich kann er selbst ein Standbild von Home-Assistant-Kameras holen
|
||||
* (HA_URL + HA_TOKEN_FILE) und dann direkt an den cat-service schicken
|
||||
* (cat_recognize_camera / cat_enroll_camera).
|
||||
*
|
||||
* Bilder koennen als Dateipfad (image_path, z.B. aus camera_snapshot) ODER als
|
||||
* Base64 (image_base64) uebergeben werden.
|
||||
*/
|
||||
'use strict';
|
||||
|
||||
const fs = require('fs');
|
||||
|
||||
const CAT_URL = (process.env.CAT_URL || 'http://cat-service:8080').replace(/\/+$/, '');
|
||||
const CAT_TIMEOUT_MS = Number(process.env.CAT_TIMEOUT_MS || 180000);
|
||||
|
||||
const HA_URL = (process.env.HA_URL || 'http://YOUR_SERVER_IP:8123').replace(/\/+$/, '');
|
||||
const HA_TOKEN_FILE = process.env.HA_TOKEN_FILE || '/home/node/.openclaw/mcp/.ha-token';
|
||||
|
||||
function readHaToken() {
|
||||
try {
|
||||
const t = fs.readFileSync(HA_TOKEN_FILE, 'utf8').trim();
|
||||
if (t) return t;
|
||||
} catch (e) {}
|
||||
return (process.env.HA_TOKEN || '').trim();
|
||||
}
|
||||
|
||||
async function catFetch(pathname, opts, timeoutMs) {
|
||||
return await fetch(CAT_URL + pathname, {
|
||||
...(opts || {}),
|
||||
headers: { 'Content-Type': 'application/json', ...((opts && opts.headers) || {}) },
|
||||
signal: AbortSignal.timeout(timeoutMs || CAT_TIMEOUT_MS),
|
||||
});
|
||||
}
|
||||
|
||||
async function catJson(pathname, opts, timeoutMs) {
|
||||
let res;
|
||||
try {
|
||||
res = await catFetch(pathname, opts, timeoutMs);
|
||||
} catch (e) {
|
||||
throw new Error(`cat-service nicht erreichbar (${CAT_URL}${pathname}): ${(e && e.message) || e}`);
|
||||
}
|
||||
const body = await res.text();
|
||||
let data = null;
|
||||
try {
|
||||
data = body ? JSON.parse(body) : {};
|
||||
} catch (e) {}
|
||||
if (!res.ok) {
|
||||
const detail = (data && (data.detail || data.message)) || body || '';
|
||||
throw new Error(`cat-service ${pathname}: HTTP ${res.status}${detail ? ` - ${detail}` : ''}`);
|
||||
}
|
||||
return data || {};
|
||||
}
|
||||
|
||||
async function haSnapshot(entity) {
|
||||
const token = readHaToken();
|
||||
if (!token) throw new Error(`HA-Zugriffstoken fehlt (${HA_TOKEN_FILE}).`);
|
||||
const res = await fetch(HA_URL + `/api/camera_proxy/${encodeURIComponent(entity)}`, {
|
||||
headers: { Authorization: `Bearer ${token}`, Accept: 'application/json' },
|
||||
signal: AbortSignal.timeout(25000),
|
||||
});
|
||||
if (!res.ok) throw new Error(`HA camera_proxy ${entity}: HTTP ${res.status}`);
|
||||
const buf = Buffer.from(await res.arrayBuffer());
|
||||
if (buf.length < 200) throw new Error(`Kamera ${entity} liefert kein brauchbares Bild (${buf.length} B).`);
|
||||
let friendly = entity;
|
||||
try {
|
||||
const st = await fetch(HA_URL + `/api/states/${encodeURIComponent(entity)}`, {
|
||||
headers: { Authorization: `Bearer ${token}`, Accept: 'application/json' },
|
||||
signal: AbortSignal.timeout(10000),
|
||||
});
|
||||
if (st.ok) {
|
||||
const j = await st.json();
|
||||
friendly = (j.attributes && j.attributes.friendly_name) || entity;
|
||||
}
|
||||
} catch (e) {}
|
||||
return { buf, friendly };
|
||||
}
|
||||
|
||||
function imageFromArgs(args) {
|
||||
const p = args && args.image_path ? String(args.image_path).trim() : '';
|
||||
const b64 = args && args.image_base64 ? String(args.image_base64).trim() : '';
|
||||
if (p) {
|
||||
if (!fs.existsSync(p)) throw new Error(`image_path existiert nicht: ${p}`);
|
||||
const buf = fs.readFileSync(p);
|
||||
return { b64: buf.toString('base64'), bytes: buf.length, source: `Datei ${p}` };
|
||||
}
|
||||
if (b64) {
|
||||
const clean = b64.startsWith('data:') && b64.includes(',') ? b64.split(',', 1)[1] : b64;
|
||||
return { b64: clean, bytes: Math.floor((clean.length * 3) / 4), source: 'image_base64' };
|
||||
}
|
||||
throw new Error('Bild fehlt: bitte image_path oder image_base64 angeben.');
|
||||
}
|
||||
|
||||
function fmtRecognize(data, context) {
|
||||
const lines = [`## Katzenerkennung${context ? `: ${context}` : ''}`, ''];
|
||||
if (data.enrolled_cats === 0) {
|
||||
lines.push('Es ist noch **keine Katze eingelernt**. Nutze `cat_enroll`/`cat_enroll_camera`, um eine zu speichern.');
|
||||
return lines.join('\n');
|
||||
}
|
||||
if (!data.count) {
|
||||
lines.push('Keine Katze im Bild erkannt.');
|
||||
return lines.join('\n');
|
||||
}
|
||||
data.cats.forEach((f, i) => {
|
||||
const label = f.matched ? `**${f.name}**` : 'unbekannt';
|
||||
const sim = f.score === null || f.score === undefined ? 'nicht eingelernt' : `Ähnlichkeit ${f.score}`;
|
||||
lines.push(`- Katze ${i + 1}: ${label} (${sim}, Detektor ${f.det_score})`);
|
||||
});
|
||||
const known = data.cats.filter((f) => f.matched).map((f) => f.name);
|
||||
if (known.length) lines.push('', `Erkannt: ${[...new Set(known)].join(', ')}.`);
|
||||
else lines.push('', 'Keine der eingelernten Katzen erkannt.');
|
||||
if (Array.isArray(data.auto_learned) && data.auto_learned.length) {
|
||||
lines.push('');
|
||||
data.auto_learned.forEach((a) => {
|
||||
lines.push(`Dazugelernt: **${a.name}** (Ähnlichkeit ${a.score}) → jetzt ${a.total} Trainingsbild(er).`);
|
||||
});
|
||||
}
|
||||
return lines.join('\n');
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------- tools
|
||||
|
||||
async function catList() {
|
||||
const data = await catJson('/cats');
|
||||
if (!data.count) return { text: '## Eingelernte Katzen\n\nNoch keine Katzen gespeichert. Mit `cat_enroll` (Name + Bild) oder `cat_enroll_camera` (Name + Kamera) anlegen.' };
|
||||
const rows = data.cats.map((f) => `- **${f.name}** — ${f.samples} Bild(er), angelegt ${f.created || '?'}`).join('\n');
|
||||
return { text: `## Eingelernte Katzen (${data.count})\n\n${rows}` };
|
||||
}
|
||||
|
||||
async function catRecognize(args) {
|
||||
const img = imageFromArgs(args);
|
||||
const auto = args && args.auto_learn !== undefined ? Boolean(args.auto_learn) : false;
|
||||
const data = await catJson('/recognize', { method: 'POST', body: JSON.stringify({ image: img.b64, threshold: args && args.threshold, auto_learn: auto }) });
|
||||
const text = fmtRecognize(data, img.source);
|
||||
return { text, content: [{ type: 'text', text }] };
|
||||
}
|
||||
|
||||
async function catRecognizeCamera(args) {
|
||||
const entity = String((args && args.entity_id) || '').trim();
|
||||
if (!/^camera\.[\w-]+$/.test(entity)) return { isError: true, text: `cat_recognize_camera: ungueltige entity_id "${entity}".` };
|
||||
const auto = args && args.auto_learn !== undefined ? Boolean(args.auto_learn) : true;
|
||||
const snap = await haSnapshot(entity);
|
||||
const data = await catJson('/recognize', { method: 'POST', body: JSON.stringify({ image: snap.buf.toString('base64'), threshold: args && args.threshold, auto_learn: auto }) });
|
||||
const text = fmtRecognize(data, `Kamera ${snap.friendly}`);
|
||||
const content = [{ type: 'text', text }];
|
||||
if (args && args.as_image !== false) content.push({ type: 'image', data: snap.buf.toString('base64'), mimeType: 'image/jpeg' });
|
||||
return { text, content };
|
||||
}
|
||||
|
||||
async function catEnroll(args) {
|
||||
const name = String((args && args.name) || '').trim();
|
||||
if (!name) return { isError: true, text: 'cat_enroll: name fehlt.' };
|
||||
const img = imageFromArgs(args);
|
||||
const data = await catJson('/enroll', { method: 'POST', body: JSON.stringify({ name, image: img.b64 }) });
|
||||
return { text: `## Katze gespeichert\n\n- Name: **${data.name}**\n- Gesamtbilder: ${data.total}\n- Detektor-Score: ${data.det_score}\n- Quelle: ${img.source}` };
|
||||
}
|
||||
|
||||
async function catEnrollCamera(args) {
|
||||
const name = String((args && args.name) || '').trim();
|
||||
const entity = String((args && args.entity_id) || '').trim();
|
||||
if (!name) return { isError: true, text: 'cat_enroll_camera: name fehlt.' };
|
||||
if (!/^camera\.[\w-]+$/.test(entity)) return { isError: true, text: `cat_enroll_camera: ungueltige entity_id "${entity}".` };
|
||||
const snap = await haSnapshot(entity);
|
||||
const data = await catJson('/enroll', { method: 'POST', body: JSON.stringify({ name, image: snap.buf.toString('base64') }) });
|
||||
return { text: `## Katze gespeichert\n\n- Name: **${data.name}**\n- Kamera: ${snap.friendly} (\`${entity}\`)\n- Gesamtbilder: ${data.total}\n- Detektor-Score: ${data.det_score}` };
|
||||
}
|
||||
|
||||
async function catDelete(args) {
|
||||
const name = String((args && args.name) || '').trim();
|
||||
if (!name) return { isError: true, text: 'cat_delete: name fehlt.' };
|
||||
await catJson(`/cats/${encodeURIComponent(name)}`, { method: 'DELETE' });
|
||||
return { text: `Katze **${name}** entfernt.` };
|
||||
}
|
||||
|
||||
const TOOLS = [
|
||||
{
|
||||
name: 'cat_list',
|
||||
description: 'Listet alle eingelernten Katzen der Katzenerkennung mit Anzahl der Bilder.',
|
||||
inputSchema: { type: 'object', properties: {}, additionalProperties: false },
|
||||
},
|
||||
{
|
||||
name: 'cat_recognize',
|
||||
description:
|
||||
'Erkennt Katzen in einem Bild und vergleicht sie mit den eingelernten Katzen. Quelle: image_path (Dateipfad, z.B. aus camera_snapshot) ODER image_base64.',
|
||||
inputSchema: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
image_path: { type: 'string', description: 'Pfad zu einer Bilddatei im Container.' },
|
||||
image_base64: { type: 'string', description: 'Bild als Base64 (JPEG/PNG).' },
|
||||
threshold: { type: 'number', description: 'Optionale Kosinus-Schwelle (Standard aus dem Dienst, 0.55).' },
|
||||
auto_learn: { type: 'boolean', description: 'Erkannte Katze automatisch als Trainingsbild speichern (Standard false).' },
|
||||
},
|
||||
additionalProperties: false,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: 'cat_recognize_camera',
|
||||
description:
|
||||
'Holt ein Standbild einer Home-Assistant-Kamera und erkennt darauf die Katzen. Ideal fuer "Welche Katze ist auf der Kamera?".',
|
||||
inputSchema: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
entity_id: { type: 'string', description: 'Kamera-Entity, z.B. "camera.lsc_smart_connect_1080p_ip_indoor_mini".' },
|
||||
threshold: { type: 'number', description: 'Optionale Kosinus-Schwelle.' },
|
||||
auto_learn: { type: 'boolean', description: 'Erkannte Katze automatisch als Trainingsbild speichern, wenn Aehnlichkeit >= 0.70 (Standard true).' },
|
||||
as_image: { type: 'boolean', description: 'Standbild mitliefern (Standard true).' },
|
||||
},
|
||||
required: ['entity_id'],
|
||||
additionalProperties: false,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: 'cat_enroll',
|
||||
description: 'Speichert eine neue Katze unter einem Namen. Quelle: image_path ODER image_base64. Erkennt die groesste Katze im Bild.',
|
||||
inputSchema: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
name: { type: 'string', description: 'Name der Katze, z.B. "Mimi".' },
|
||||
image_path: { type: 'string', description: 'Pfad zu einer Bilddatei im Container.' },
|
||||
image_base64: { type: 'string', description: 'Bild als Base64.' },
|
||||
},
|
||||
required: ['name'],
|
||||
additionalProperties: false,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: 'cat_enroll_camera',
|
||||
description:
|
||||
'Speichert eine Katze ein, indem ein frisches Standbild einer Home-Assistant-Kamera aufgenommen wird. Wenn keine Kamera genannt ist, zuerst camera_list aufrufen.',
|
||||
inputSchema: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
name: { type: 'string', description: 'Name der Katze.' },
|
||||
entity_id: { type: 'string', description: 'Kamera-Entity, z.B. "camera.lsc_smart_connect_1080p_ip_indoor_mini".' },
|
||||
},
|
||||
required: ['name', 'entity_id'],
|
||||
additionalProperties: false,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: 'cat_delete',
|
||||
description: 'Entfernt eine eingelernte Katze samt Bildern.',
|
||||
inputSchema: {
|
||||
type: 'object',
|
||||
properties: { name: { type: 'string', description: 'Name der Katze.' } },
|
||||
required: ['name'],
|
||||
additionalProperties: false,
|
||||
},
|
||||
},
|
||||
];
|
||||
|
||||
function reply(id, result) {
|
||||
process.stdout.write(JSON.stringify({ jsonrpc: '2.0', id, result }) + '\n');
|
||||
}
|
||||
function replyError(id, code, message) {
|
||||
process.stdout.write(JSON.stringify({ jsonrpc: '2.0', id, error: { code, message } }) + '\n');
|
||||
}
|
||||
|
||||
let buf = '';
|
||||
process.stdin.setEncoding('utf8');
|
||||
process.stdin.on('data', (chunk) => {
|
||||
buf += chunk;
|
||||
let idx;
|
||||
while ((idx = buf.indexOf('\n')) >= 0) {
|
||||
const line = buf.slice(0, idx).trim();
|
||||
buf = buf.slice(idx + 1);
|
||||
if (!line) continue;
|
||||
handle(line);
|
||||
}
|
||||
});
|
||||
|
||||
function handle(line) {
|
||||
let msg;
|
||||
try {
|
||||
msg = JSON.parse(line);
|
||||
} catch (e) {
|
||||
process.stdout.write(JSON.stringify({ jsonrpc: '2.0', id: null, error: { code: -32700, message: 'Parse error' } }) + '\n');
|
||||
return;
|
||||
}
|
||||
const { id, method, params } = msg;
|
||||
if (method === 'notifications/initialized' || (id === undefined && method)) return;
|
||||
|
||||
if (method === 'initialize') {
|
||||
reply(id, {
|
||||
protocolVersion: (params && params.protocolVersion) || '2024-11-05',
|
||||
capabilities: { tools: {} },
|
||||
serverInfo: { name: 'cat', version: '1.0.0' },
|
||||
});
|
||||
return;
|
||||
}
|
||||
if (method === 'ping') {
|
||||
reply(id, {});
|
||||
return;
|
||||
}
|
||||
if (method === 'tools/list') {
|
||||
reply(id, {
|
||||
tools: TOOLS.map(({ name, description, inputSchema }) => ({
|
||||
name,
|
||||
description,
|
||||
inputSchema,
|
||||
annotations: { title: name.replace(/_/g, ' '), readOnlyHint: name === 'cat_list' || name === 'cat_recognize' || name === 'cat_recognize_camera', destructiveHint: name === 'cat_delete', idempotentHint: false, openWorldHint: true },
|
||||
})),
|
||||
});
|
||||
return;
|
||||
}
|
||||
if (method === 'tools/call') {
|
||||
const name = params && params.name;
|
||||
const args = (params && params.arguments) || {};
|
||||
const run = async () => {
|
||||
if (name === 'cat_list') return await catList();
|
||||
if (name === 'cat_recognize') return await catRecognize(args);
|
||||
if (name === 'cat_recognize_camera') return await catRecognizeCamera(args);
|
||||
if (name === 'cat_enroll') return await catEnroll(args);
|
||||
if (name === 'cat_enroll_camera') return await catEnrollCamera(args);
|
||||
if (name === 'cat_delete') return await catDelete(args);
|
||||
return { isError: true, text: `Unbekanntes Werkzeug: ${name}` };
|
||||
};
|
||||
run()
|
||||
.then((r) => {
|
||||
const content = r.content || [{ type: 'text', text: r.text }];
|
||||
reply(id, { content, isError: Boolean(r.isError) });
|
||||
})
|
||||
.catch((e) => reply(id, { content: [{ type: 'text', text: String((e && e.message) || e) }], isError: true }));
|
||||
return;
|
||||
}
|
||||
if (id !== undefined) replyError(id, -32601, `Method not found: ${method}`);
|
||||
}
|
||||
Reference in new issue
Block a user