From c3523d002e728ce8c89efc27b3c823e532094b1c Mon Sep 17 00:00:00 2001 From: ywkj Date: Mon, 20 Apr 2026 07:46:45 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E9=81=A5=E6=B5=8B=E6=94=B9=E7=94=A8=20T?= =?UTF-8?q?ELEMETRY=5FENDPOINT=EF=BC=8C=E4=B8=8D=E5=A4=8D=E7=94=A8=20hookU?= =?UTF-8?q?rl?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Sonnet 4.6 --- .env.example | 5 +++++ scripts/run.ts | 53 +++++++++++--------------------------------------- 2 files changed, 16 insertions(+), 42 deletions(-) diff --git a/.env.example b/.env.example index e4eb4ee..cff5c49 100644 --- a/.env.example +++ b/.env.example @@ -38,3 +38,8 @@ ONEBOUND_KEYWORD_SEARCH_ENDPOINT=http://localhost:3202/api/v1/tasks/keyword-sear # Auth(由 auth-rt 自动处理,配置见 ~/.openclaw/.env) # 只需在 ~/.openclaw/.env 中设置 CLIENT_KEY=sk_xxx # ----------------------------------------------------------------------------- + +# ----------------------------------------------------------------------------- +# 遥测(可选)— 上报 skill 执行结果到服务端 Loki,用于调试 +# ----------------------------------------------------------------------------- +# TELEMETRY_ENDPOINT=http://localhost:3202/api/v1/tasks/telemetry diff --git a/scripts/run.ts b/scripts/run.ts index 56d99ad..5ea9acd 100644 --- a/scripts/run.ts +++ b/scripts/run.ts @@ -2,8 +2,6 @@ import { resolve } from 'path'; import type { Command } from '../src/types.ts'; import { run } from '../src/index.ts'; -import { createSkillClient } from '../src/auth-cli.ts'; - const SKILL_NAME = 'video-product-snapshot'; // Load .env from skill root (does not override existing env vars) @@ -52,14 +50,14 @@ Config: ~/.openclaw/.env (CLIENT_KEY), skill .env (VISION_API_KEY) `); } -async function reportHook( - hookUrl: string, - hookToken: string | undefined, - payload: object, -): Promise { - const headers: Record = { 'Content-Type': 'application/json' }; - if (hookToken) headers['Authorization'] = `Bearer ${hookToken}`; - await fetch(hookUrl, { method: 'POST', headers, body: JSON.stringify(payload) }); +function reportTelemetry(payload: object): void { + const endpoint = process.env.TELEMETRY_ENDPOINT; + if (!endpoint) return; + fetch(endpoint, { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify(payload), + }).catch(() => {}); } async function main(): Promise { @@ -81,19 +79,6 @@ async function main(): Promise { if (positionals.length < 1) { printUsage(); process.exit(1); } const command = positionals[0] as Command; - - // Exchange CLIENT_KEY for session — gives us hookUrl for telemetry - let hookUrl: string | undefined; - let hookToken: string | undefined; - try { - const client = createSkillClient({ dryRun }); - const session = await client.session(); - hookUrl = session.hookUrl; - hookToken = session.hookToken; - } catch { - // Auth failure is non-fatal for telemetry; skill still runs - } - const startMs = Date.now(); let result: Awaited>; @@ -101,29 +86,13 @@ async function main(): Promise { result = await run(command, positionals.slice(1), dryRun); } catch (err) { const error = err instanceof Error ? err.message : String(err); - const failed = { status: 'failed' as const, command, dryRun, error }; - console.log(JSON.stringify(failed, null, 2)); - - if (hookUrl && !dryRun) { - reportHook(hookUrl, hookToken, { - skill: SKILL_NAME, command, status: 'failed', - durationMs: Date.now() - startMs, error, - }).catch(() => {}); - } + console.log(JSON.stringify({ status: 'failed', command, dryRun, error }, null, 2)); + if (!dryRun) reportTelemetry({ skill: SKILL_NAME, command, status: 'failed', durationMs: Date.now() - startMs, error }); process.exit(1); } console.log(JSON.stringify(result, null, 2)); - - if (hookUrl && !dryRun) { - reportHook(hookUrl, hookToken, { - skill: SKILL_NAME, - command, - status: result.status, - durationMs: Date.now() - startMs, - error: (result as any).error, - }).catch(() => {}); - } + if (!dryRun) reportTelemetry({ skill: SKILL_NAME, command, status: result.status, durationMs: Date.now() - startMs, error: (result as any).error }); } main().catch((err) => {