@igorjpimenta/winch 2026.11.23-beta.1 → 2026.11.23-beta.2
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/.migrations/0014_usage_events.sql +30 -0
- package/.migrations/meta/0012_snapshot.json +2 -2
- package/.migrations/meta/0013_snapshot.json +9 -9
- package/.migrations/meta/0014_snapshot.json +2121 -0
- package/.migrations/meta/_journal.json +7 -0
- package/package.json +1 -1
- package/plugins/README.md +11 -0
- package/plugins/openclaw/winch/README.md +7 -1
- package/plugins/openclaw/winch/index.js +24 -3
- package/plugins/openclaw/winch/package.json +2 -1
- package/plugins/openclaw/winch/usage.js +111 -0
- package/plugins/opencode/winch/hooks.js +16 -2
- package/plugins/opencode/winch/index.js +20 -2
- package/plugins/opencode/winch/package.json +4 -3
- package/plugins/opencode/winch/usage.js +120 -0
- package/src/schema.js +30 -0
- package/src/store/index.js +3 -0
- package/src/store/usage.js +85 -0
- package/src/tools/index.js +16 -0
- package/src/webhook/server.js +57 -0
package/src/webhook/server.js
CHANGED
|
@@ -10,6 +10,29 @@ import { eventsSince, lastEventId, toEventMessage, HISTORY_LIMIT } from "./envel
|
|
|
10
10
|
|
|
11
11
|
export const DEFAULT_DISPATCH_PORT = 38293;
|
|
12
12
|
|
|
13
|
+
const USAGE_NUM = (v) => (typeof v === "number" && Number.isFinite(v) ? v : null);
|
|
14
|
+
|
|
15
|
+
// Canonical usage shape from the harness. Accepts snake_case aliases so a
|
|
16
|
+
// plugin need not match our field names exactly. Returns null when nothing
|
|
17
|
+
// numeric/identifiable is present, so the endpoint rejects empty payloads.
|
|
18
|
+
function normalizeUsage(raw) {
|
|
19
|
+
if (raw == null || typeof raw !== "object" || Array.isArray(raw)) return null;
|
|
20
|
+
const model = typeof raw.model === "string" && raw.model.trim() !== "" ? raw.model.trim() : null;
|
|
21
|
+
const out = {
|
|
22
|
+
model,
|
|
23
|
+
tokensIn: USAGE_NUM(raw.tokensIn ?? raw.tokens_in ?? raw.inputTokens ?? raw.input_tokens),
|
|
24
|
+
tokensOut: USAGE_NUM(raw.tokensOut ?? raw.tokens_out ?? raw.outputTokens ?? raw.output_tokens),
|
|
25
|
+
cacheRead: USAGE_NUM(raw.cacheRead ?? raw.cache_read ?? raw.cachedTokens ?? raw.cached_tokens),
|
|
26
|
+
costUsd: USAGE_NUM(raw.costUsd ?? raw.cost_usd ?? raw.cost),
|
|
27
|
+
toolCalls: USAGE_NUM(raw.toolCalls ?? raw.tool_calls),
|
|
28
|
+
};
|
|
29
|
+
if (out.model === null && out.tokensIn === null && out.tokensOut === null &&
|
|
30
|
+
out.cacheRead === null && out.costUsd === null && out.toolCalls === null) {
|
|
31
|
+
return null;
|
|
32
|
+
}
|
|
33
|
+
return out;
|
|
34
|
+
}
|
|
35
|
+
|
|
13
36
|
export function startDispatchServer({ store, port = null, host = "127.0.0.1", intervalMs = null, logger } = {}) {
|
|
14
37
|
const cfg = store.config ?? {};
|
|
15
38
|
const listenPort = port ?? cfg.dispatchPort ?? DEFAULT_DISPATCH_PORT;
|
|
@@ -172,6 +195,40 @@ export function startDispatchServer({ store, port = null, host = "127.0.0.1", in
|
|
|
172
195
|
}
|
|
173
196
|
return;
|
|
174
197
|
}
|
|
198
|
+
if (req.method === "POST" && url.pathname === "/usage") {
|
|
199
|
+
if (unauthorized(req)) { json(res, 401, { error: "unauthorized" }); return; }
|
|
200
|
+
const body = await readJson(req, res);
|
|
201
|
+
if (body === undefined) return;
|
|
202
|
+
try {
|
|
203
|
+
const sessionKey = typeof body.sessionKey === "string" ? body.sessionKey.trim() : "";
|
|
204
|
+
if (!sessionKey) throw new Error("usage requires sessionKey");
|
|
205
|
+
const usage = normalizeUsage(body.usage);
|
|
206
|
+
if (!usage) throw new Error("usage requires a non-empty usage object");
|
|
207
|
+
const sess = store.db.prepare(
|
|
208
|
+
"SELECT id, item_id, harness, agent_id, external_session_id FROM work_sessions WHERE session_key=?"
|
|
209
|
+
).get(sessionKey);
|
|
210
|
+
if (!sess) { json(res, 404, { error: `no session: ${sessionKey}` }); return; }
|
|
211
|
+
const live = store.db.prepare(
|
|
212
|
+
"SELECT id FROM attempts WHERE item_id=? AND ended_at IS NULL ORDER BY attempt_no DESC LIMIT 1"
|
|
213
|
+
).get(sess.item_id);
|
|
214
|
+
const turnRef = typeof body.turnRef === "string" && body.turnRef.trim() !== "" ? body.turnRef.trim() : null;
|
|
215
|
+
const id = store.usage.record({
|
|
216
|
+
harness: sess.harness,
|
|
217
|
+
agent: sess.agent_id,
|
|
218
|
+
session: sess.external_session_id,
|
|
219
|
+
itemId: sess.item_id,
|
|
220
|
+
sessionId: sess.id,
|
|
221
|
+
attemptId: live?.id ?? null,
|
|
222
|
+
usage,
|
|
223
|
+
source: "api",
|
|
224
|
+
turnRef,
|
|
225
|
+
});
|
|
226
|
+
json(res, 201, { ok: true, id, itemId: sess.item_id, duplicate: id === null });
|
|
227
|
+
} catch (err) {
|
|
228
|
+
json(res, 400, { error: String(err?.message ?? err) });
|
|
229
|
+
}
|
|
230
|
+
return;
|
|
231
|
+
}
|
|
175
232
|
json(res, 404, { error: "not found" });
|
|
176
233
|
});
|
|
177
234
|
|