@igorjpimenta/manifold 0.0.0-bootstrap.1 → 0.0.0-stage
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/README.md +2 -1583
- package/package.json +4 -49
- package/.migrations/ops/0000_futuristic_karma.sql +0 -111
- package/.migrations/ops/0001_low_maximus.sql +0 -7
- package/.migrations/ops/meta/0000_snapshot.json +0 -763
- package/.migrations/ops/meta/0001_snapshot.json +0 -173
- package/.migrations/ops/meta/_journal.json +0 -20
- package/.migrations/runtime/0000_real_boomer.sql +0 -180
- package/.migrations/runtime/0001_hot_dakota_north.sql +0 -2
- package/.migrations/runtime/0002_breezy_vertigo.sql +0 -8
- package/.migrations/runtime/0003_dashing_jack_flag.sql +0 -5
- package/.migrations/runtime/0004_panoramic_ego.sql +0 -1
- package/.migrations/runtime/0005_tidy_ben_grimm.sql +0 -10
- package/.migrations/runtime/0006_wet_nightmare.sql +0 -57
- package/.migrations/runtime/0007_ambiguous_chronomancer.sql +0 -8
- package/.migrations/runtime/0008_wealthy_thundra.sql +0 -301
- package/.migrations/runtime/0009_lying_shocker.sql +0 -96
- package/.migrations/runtime/0010_solid_phantom_reporter.sql +0 -160
- package/.migrations/runtime/0011_windy_demogoblin.sql +0 -175
- package/.migrations/runtime/0012_restore_harness_fks.sql +0 -103
- package/.migrations/runtime/meta/0000_snapshot.json +0 -1181
- package/.migrations/runtime/meta/0001_snapshot.json +0 -1209
- package/.migrations/runtime/meta/0002_snapshot.json +0 -1261
- package/.migrations/runtime/meta/0003_snapshot.json +0 -1292
- package/.migrations/runtime/meta/0004_snapshot.json +0 -1240
- package/.migrations/runtime/meta/0006_snapshot.json +0 -1310
- package/.migrations/runtime/meta/0007_snapshot.json +0 -1362
- package/.migrations/runtime/meta/0008_snapshot.json +0 -1742
- package/.migrations/runtime/meta/0009_snapshot.json +0 -2413
- package/.migrations/runtime/meta/0010_snapshot.json +0 -2562
- package/.migrations/runtime/meta/0011_snapshot.json +0 -2812
- package/.migrations/runtime/meta/0012_snapshot.json +0 -2812
- package/.migrations/runtime/meta/_journal.json +0 -97
- package/RELEASING.md +0 -89
- package/agent/fleet.js +0 -100
- package/bin/cli.js +0 -854
- package/bin/manifold.js +0 -4
- package/boot/flow-revival.js +0 -536
- package/budget/index.js +0 -495
- package/budget/lib.js +0 -59
- package/dashboard/cost.js +0 -528
- package/dashboard/dist/assets/index-CUfp63sj.css +0 -1
- package/dashboard/dist/assets/index-Qy9JMpma.js +0 -371
- package/dashboard/dist/index.html +0 -13
- package/dashboard/event-log.js +0 -267
- package/dashboard/index.html +0 -12
- package/dashboard/package-lock.json +0 -2877
- package/dashboard/package.json +0 -37
- package/dashboard/pipeline-graph.js +0 -131
- package/dashboard/postcss.config.js +0 -6
- package/dashboard/search.js +0 -183
- package/dashboard/server.js +0 -1015
- package/dashboard/src/App.tsx +0 -42
- package/dashboard/src/api/client.ts +0 -118
- package/dashboard/src/api/types.ts +0 -510
- package/dashboard/src/api/ws.tsx +0 -112
- package/dashboard/src/components/CancelButton.tsx +0 -63
- package/dashboard/src/components/ClickToCopy.tsx +0 -68
- package/dashboard/src/components/CopyButton.tsx +0 -61
- package/dashboard/src/components/DailyCostTrend.tsx +0 -85
- package/dashboard/src/components/DailyTimeTrend.tsx +0 -94
- package/dashboard/src/components/Funnel.tsx +0 -149
- package/dashboard/src/components/GateForm.tsx +0 -158
- package/dashboard/src/components/GraphView.tsx +0 -302
- package/dashboard/src/components/GroupPathsSummary.tsx +0 -47
- package/dashboard/src/components/Histogram.tsx +0 -292
- package/dashboard/src/components/Layout.tsx +0 -28
- package/dashboard/src/components/NeedsAttention.tsx +0 -98
- package/dashboard/src/components/NodeContextCard.tsx +0 -132
- package/dashboard/src/components/PathTrail.tsx +0 -35
- package/dashboard/src/components/PipelineCostBreakdown.tsx +0 -277
- package/dashboard/src/components/RerunButton.tsx +0 -46
- package/dashboard/src/components/RunComparison.tsx +0 -53
- package/dashboard/src/components/Sparkline.tsx +0 -94
- package/dashboard/src/components/StatStrip.tsx +0 -147
- package/dashboard/src/components/StateTree.tsx +0 -84
- package/dashboard/src/components/StatusBadge.tsx +0 -18
- package/dashboard/src/components/SubflowPathPills.tsx +0 -38
- package/dashboard/src/components/SubflowSummary.tsx +0 -24
- package/dashboard/src/components/SubflowsCard.tsx +0 -327
- package/dashboard/src/components/TodayStrip.tsx +0 -86
- package/dashboard/src/components/TranscriptPane.tsx +0 -99
- package/dashboard/src/components/TranscriptRow.tsx +0 -69
- package/dashboard/src/components/ui/badge.tsx +0 -38
- package/dashboard/src/components/ui/button.tsx +0 -28
- package/dashboard/src/components/ui/card.tsx +0 -42
- package/dashboard/src/components/ui/skeleton.tsx +0 -9
- package/dashboard/src/components/ui/table.tsx +0 -62
- package/dashboard/src/index.css +0 -26
- package/dashboard/src/lib/anomaly.ts +0 -42
- package/dashboard/src/lib/format.ts +0 -30
- package/dashboard/src/lib/step-history.ts +0 -13
- package/dashboard/src/lib/subflow.ts +0 -154
- package/dashboard/src/lib/use-click-outside.ts +0 -28
- package/dashboard/src/lib/use-ignored-clusters.ts +0 -67
- package/dashboard/src/lib/utils.ts +0 -6
- package/dashboard/src/main.tsx +0 -10
- package/dashboard/src/types/shims.d.ts +0 -4
- package/dashboard/src/views/Home.tsx +0 -478
- package/dashboard/src/views/PipelineDetail.tsx +0 -1185
- package/dashboard/src/views/PipelinesList.tsx +0 -111
- package/dashboard/src/views/RunDetail.tsx +0 -248
- package/dashboard/src/views/Search.tsx +0 -196
- package/dashboard/stats.js +0 -668
- package/dashboard/tailwind.config.ts +0 -43
- package/dashboard/tree-stats.js +0 -12
- package/dashboard/tsconfig.json +0 -25
- package/dashboard/views.js +0 -231
- package/dashboard/vite.config.ts +0 -34
- package/drizzle.config.js +0 -12
- package/drizzle.config.ops.js +0 -12
- package/elevation.js +0 -116
- package/engine/admission-promotion-resume.js +0 -127
- package/engine/agent-dispatch.js +0 -64
- package/engine/child-resume-trigger.js +0 -315
- package/engine/executor-budget.js +0 -87
- package/engine/executor-cancel.js +0 -83
- package/engine/executor-errors.js +0 -122
- package/engine/executor-helpers.js +0 -254
- package/engine/executor-nodes.js +0 -292
- package/engine/executor-persist.js +0 -212
- package/engine/executor-subflow-children.js +0 -275
- package/engine/executor.js +0 -1013
- package/engine/global-concurrency.js +0 -33
- package/engine/loader.js +0 -528
- package/engine/needs-resolver.js +0 -229
- package/engine/pipelines-watcher.js +0 -133
- package/engine/result-replay.js +0 -34
- package/engine/revival-messages.js +0 -11
- package/engine/spawn-child.js +0 -55
- package/engine/wait-for-flow-settlement.js +0 -109
- package/events/catalog.js +0 -26
- package/events/emit.js +0 -44
- package/events/kinds.js +0 -118
- package/events/serve-ws.js +0 -74
- package/events/stream.js +0 -35
- package/events/ws-dialog.js +0 -257
- package/events/ws-frame.js +0 -194
- package/events/ws-run-events.js +0 -70
- package/events.js +0 -32
- package/gates/gate-resolver.js +0 -576
- package/gates/paused-flows.js +0 -55
- package/graphql/columns.js +0 -67
- package/graphql/schema.js +0 -333
- package/index.js +0 -49
- package/mcp/http.js +0 -159
- package/mcp/identity.js +0 -94
- package/mcp/stdio.js +0 -80
- package/mcp/tools.js +0 -245
- package/mcp/wire-ops.js +0 -178
- package/routes/bearer-auth.js +0 -57
- package/routes/discuss-audio.js +0 -87
- package/routes/discuss-component.js +0 -39
- package/routes/discuss-draft.js +0 -159
- package/routes/discuss-helpers.js +0 -20
- package/routes/discuss.js +0 -848
- package/routes/failures.js +0 -104
- package/routes/graphql.js +0 -61
- package/routes/health.js +0 -54
- package/routes/operator-discuss.js +0 -193
- package/routes/operator-resume.js +0 -32
- package/routes/operator-retry.js +0 -301
- package/routes/operator.js +0 -539
- package/routes/pipelines.js +0 -118
- package/routes/runs.js +0 -1129
- package/scripts/ci/import-check.mjs +0 -50
- package/scripts/link-core-self.mjs +0 -31
- package/scripts/with-guard.sh +0 -24
- package/server/app.js +0 -503
- package/server/registry.js +0 -53
- package/server/serve.js +0 -163
- package/state/admission-queue-store.js +0 -325
- package/state/attribution-registries.js +0 -102
- package/state/batch-writer.js +0 -162
- package/state/db-guard.js +0 -63
- package/state/db-health.js +0 -27
- package/state/db.js +0 -122
- package/state/discuss-store.js +0 -299
- package/state/failure-store.js +0 -427
- package/state/migrate.js +0 -104
- package/state/node-cost.js +0 -67
- package/state/node-executions.js +0 -775
- package/state/node-health-store.js +0 -124
- package/state/ops-merge.js +0 -93
- package/state/paths.js +0 -146
- package/state/pipeline-dirs.js +0 -99
- package/state/pipeline-registry.js +0 -449
- package/state/run-store.js +0 -697
- package/state/schema.js +0 -438
- package/state/schema.ops.js +0 -44
- package/state/session-index.js +0 -177
- package/state/session-keys.js +0 -39
- package/state/state-store.js +0 -311
- package/state/subflow-state.js +0 -126
- package/state/tool-handoff.js +0 -169
- package/telemetry/budget-alert.js +0 -353
- package/telemetry/cli.js +0 -331
- package/telemetry/grammar.js +0 -119
- package/telemetry/http-budget.js +0 -132
- package/telemetry/http-cost.js +0 -670
- package/telemetry/http-helpers.js +0 -134
- package/telemetry/http-tree.js +0 -349
- package/telemetry/http.js +0 -44
- package/telemetry/index.js +0 -307
- package/telemetry/ingest.js +0 -134
- package/telemetry/labels.js +0 -115
- package/telemetry/pricer.js +0 -77
- package/telemetry/resolver.js +0 -314
- package/telemetry/store.js +0 -147
- package/utils/run-patch.js +0 -43
package/telemetry/index.js
DELETED
|
@@ -1,307 +0,0 @@
|
|
|
1
|
-
// telemetry/index.js — lineage + cost attribution wired into manifold.
|
|
2
|
-
// Absorbed from extensions/lineage/; exposes registerTelemetry(api) instead of
|
|
3
|
-
// definePluginEntry so manifold's own plugin entry calls it inline.
|
|
4
|
-
//
|
|
5
|
-
// Responsibilities (unchanged from lineage):
|
|
6
|
-
// - session_lineage + cost_by_call writes via before_agent_start /
|
|
7
|
-
// subagent_spawned / session_start / before_message_write hooks
|
|
8
|
-
// - /manifold/telemetry/* HTTP read-only endpoints (cost, tree, budget cap)
|
|
9
|
-
// - api.runtime.lineage.recordTrigger / recordSpawn cross-plugin API
|
|
10
|
-
// - `openclaw lineage` CLI subcommands
|
|
11
|
-
|
|
12
|
-
import { openStore, defaultDbPath } from "./store.js";
|
|
13
|
-
import { makeResolver } from "./resolver.js";
|
|
14
|
-
import { registerCliCommands } from "./cli.js";
|
|
15
|
-
import { resolveCost } from "./pricer.js";
|
|
16
|
-
import { checkBudget, initCapStore } from "./budget-alert.js";
|
|
17
|
-
import { registerHttpRoutes } from "./http.js";
|
|
18
|
-
import { applyTelemetryEvent } from "./ingest.js";
|
|
19
|
-
import { authorizeBearerRequest, sendUnauthorized } from "../routes/bearer-auth.js";
|
|
20
|
-
import { createEmitter } from "../events/emit.js";
|
|
21
|
-
// Hoisted to module-level so every call's hook closures share the same
|
|
22
|
-
// store/resolver. The SDK can call register() repeatedly (config reload,
|
|
23
|
-
// hot-reload); only the first service start actually opens the DB.
|
|
24
|
-
let SHARED_STORE = null;
|
|
25
|
-
let SHARED_RESOLVER = null;
|
|
26
|
-
|
|
27
|
-
// Live accessor for manifold modules that need to resolve a session's
|
|
28
|
-
// flow/item lineage without opening their own resolver instance. Returns
|
|
29
|
-
// null until the lineage-service's start() hook has run.
|
|
30
|
-
export function getSharedResolver() {
|
|
31
|
-
return SHARED_RESOLVER;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
// Calls that arrive before the store is ready (gateway boot revival) are
|
|
35
|
-
// buffered and flushed once the service starts.
|
|
36
|
-
let PENDING_CALLS = [];
|
|
37
|
-
|
|
38
|
-
export function registerTelemetry(api, { getBroadcast, _dbPath } = {}) {
|
|
39
|
-
const dbPath = _dbPath ?? defaultDbPath();
|
|
40
|
-
|
|
41
|
-
const logger = api.logger;
|
|
42
|
-
|
|
43
|
-
// Canonical-kind fan-out for cost.record.added below: canonical wire
|
|
44
|
-
// name, envelope adds kind.
|
|
45
|
-
const { emit } = createEmitter({ getBroadcast, logger });
|
|
46
|
-
|
|
47
|
-
// ── Budget-cap store service ────────────────────────────────────────────────
|
|
48
|
-
// Must load before checkBudget()/the /budget/cap HTTP routes see traffic —
|
|
49
|
-
// otherwise they'd read the DEFAULT_CAP_USD fallback instead of the
|
|
50
|
-
// operator-set value.
|
|
51
|
-
|
|
52
|
-
api.registerService({
|
|
53
|
-
id: "budget-cap-store",
|
|
54
|
-
start: async () => {
|
|
55
|
-
try {
|
|
56
|
-
await initCapStore({ dbPath });
|
|
57
|
-
logger.info(`telemetry: budget-cap store ready`);
|
|
58
|
-
} catch (err) {
|
|
59
|
-
logger.error(`telemetry: budget-cap store init failed: ${err?.message ?? err}`);
|
|
60
|
-
}
|
|
61
|
-
},
|
|
62
|
-
});
|
|
63
|
-
|
|
64
|
-
// ── Store service ──────────────────────────────────────────────────────────
|
|
65
|
-
|
|
66
|
-
api.registerService({
|
|
67
|
-
id: "lineage-service",
|
|
68
|
-
start: async () => {
|
|
69
|
-
if (SHARED_STORE) { logger.info(`telemetry: store already initialized — skipping`); return; }
|
|
70
|
-
try {
|
|
71
|
-
SHARED_STORE = await openStore({ dbPath });
|
|
72
|
-
SHARED_RESOLVER = makeResolver({ store: SHARED_STORE });
|
|
73
|
-
logger.info(`telemetry: store ready at ${dbPath}`);
|
|
74
|
-
if (PENDING_CALLS.length) {
|
|
75
|
-
const pending = PENDING_CALLS.splice(0);
|
|
76
|
-
logger.info(`telemetry: flushing ${pending.length} queued pre-ready call(s)`);
|
|
77
|
-
for (const { type, args } of pending) {
|
|
78
|
-
try {
|
|
79
|
-
if (type === "trigger") await api.runtime.lineage.recordTrigger(args);
|
|
80
|
-
else if (type === "spawn") await api.runtime.lineage.recordSpawn(args);
|
|
81
|
-
} catch (e) {
|
|
82
|
-
logger.warn(`telemetry: pending flush error (${type}): ${e?.message ?? e}`);
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
} catch (err) {
|
|
87
|
-
logger.error(`telemetry: service start failed: ${err?.message ?? err}`);
|
|
88
|
-
SHARED_STORE = null; SHARED_RESOLVER = null;
|
|
89
|
-
}
|
|
90
|
-
},
|
|
91
|
-
});
|
|
92
|
-
|
|
93
|
-
// ── Ingest ─────────────────────────────────────────────────────────────────
|
|
94
|
-
// Harnesses observe their own lifecycle; core decides what those
|
|
95
|
-
// observations mean. Adapters post semantic events (lineage.session,
|
|
96
|
-
// lineage.spawn, cost.usage) and ingest.js derives the rows.
|
|
97
|
-
const ingestToken = process.env.MANIFOLD_INGEST_TOKEN || process.env.MANIFOLD_BEARER || "";
|
|
98
|
-
|
|
99
|
-
api.registerHttpRoute({
|
|
100
|
-
path: "/manifold/telemetry/event",
|
|
101
|
-
auth: "plugin",
|
|
102
|
-
match: "exact",
|
|
103
|
-
handler: async (req, res) => {
|
|
104
|
-
if (req.method !== "POST") {
|
|
105
|
-
res.statusCode = 405;
|
|
106
|
-
res.setHeader("Allow", "POST");
|
|
107
|
-
res.end("method not allowed");
|
|
108
|
-
return true;
|
|
109
|
-
}
|
|
110
|
-
const authz = authorizeBearerRequest(req, { logger, secret: ingestToken });
|
|
111
|
-
if (!authz.ok) { sendUnauthorized(res, authz.reason); return true; }
|
|
112
|
-
let size = 0;
|
|
113
|
-
const chunks = [];
|
|
114
|
-
for await (const chunk of req) {
|
|
115
|
-
size += chunk.length;
|
|
116
|
-
if (size > 256 * 1024) { res.statusCode = 413; res.end("body too large"); return true; }
|
|
117
|
-
chunks.push(chunk);
|
|
118
|
-
}
|
|
119
|
-
let event;
|
|
120
|
-
try { event = JSON.parse(Buffer.concat(chunks).toString("utf8") || "{}"); }
|
|
121
|
-
catch { res.statusCode = 400; res.end(JSON.stringify({ ok: false, error: "invalid json body" })); return true; }
|
|
122
|
-
const result = await applyTelemetryEvent(event, {
|
|
123
|
-
getStore: () => SHARED_STORE,
|
|
124
|
-
getResolver: () => SHARED_RESOLVER,
|
|
125
|
-
getBroadcast, logger, emit,
|
|
126
|
-
});
|
|
127
|
-
res.statusCode = result.ok ? 202 : 400;
|
|
128
|
-
res.setHeader("Content-Type", "application/json");
|
|
129
|
-
res.end(JSON.stringify(result));
|
|
130
|
-
return true;
|
|
131
|
-
},
|
|
132
|
-
});
|
|
133
|
-
|
|
134
|
-
// ── HTTP routes ────────────────────────────────────────────────────────────
|
|
135
|
-
|
|
136
|
-
registerHttpRoutes({ api, getStore: () => SHARED_STORE, logger, getBroadcast });
|
|
137
|
-
|
|
138
|
-
// ── Runtime API ────────────────────────────────────────────────────────────
|
|
139
|
-
|
|
140
|
-
api.runtime = api.runtime ?? {};
|
|
141
|
-
api.runtime.lineage = {
|
|
142
|
-
async recordTrigger({ flowId, runId, pipelineId, parentSessionKey, rootKind: rkHint, rootLabel: rlHint }) {
|
|
143
|
-
try {
|
|
144
|
-
const store = SHARED_STORE, resolver = SHARED_RESOLVER;
|
|
145
|
-
if (!store || !resolver) {
|
|
146
|
-
PENDING_CALLS.push({ type: "trigger", args: { flowId, runId, pipelineId, parentSessionKey, rootKind: rkHint, rootLabel: rlHint } });
|
|
147
|
-
return { ok: false, reason: "not_ready", queued: true };
|
|
148
|
-
}
|
|
149
|
-
if (!flowId || !parentSessionKey) return { ok: false, reason: "missing_args" };
|
|
150
|
-
|
|
151
|
-
const WALK_THROUGH = new Set(["agent_tool", "cli"]);
|
|
152
|
-
let rootKey = parentSessionKey, rootKind = rkHint ?? "unknown", rootLabel = rlHint ?? parentSessionKey;
|
|
153
|
-
if (!rkHint || WALK_THROUGH.has(rkHint)) {
|
|
154
|
-
try {
|
|
155
|
-
const chain = await resolver.walk(parentSessionKey);
|
|
156
|
-
if (chain.length) {
|
|
157
|
-
const r = chain[chain.length - 1];
|
|
158
|
-
rootKey = r.key; rootKind = r.kind; rootLabel = r.rootLabel ?? r.key;
|
|
159
|
-
}
|
|
160
|
-
} catch { /* fall through with self-root defaults */ }
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
// An "explicit" root is a hand-typed/ad hoc session purpose string —
|
|
164
|
-
// it has no durable identity worth grouping by, and labels.js collapses
|
|
165
|
-
// it to "Ad-hoc sessions" once it's UUID-suffixed. The pipeline this
|
|
166
|
-
// trigger is dispatching is the far more useful attribution — same
|
|
167
|
-
// convention routes/operator.js already uses for its parent-less
|
|
168
|
-
// synthetic trigger path (rootKind: "explicit", rootLabel: pipelineId).
|
|
169
|
-
//
|
|
170
|
-
// "operator-trigger" hits the identical problem via a different route:
|
|
171
|
-
// routes/operator.js's synthetic `agent:operator:(cli|operator):
|
|
172
|
-
// <flowId>` key is never itself a stored child_session_key (only
|
|
173
|
-
// `flow:<id>` rows get upserted), so a subflow dispatched from an
|
|
174
|
-
// agent session whose chain reaches that key directly — rather than
|
|
175
|
-
// through the seeded flow:<topFlowId> row — gets classified fresh by
|
|
176
|
-
// grammar.js from the key shape alone, landing on resolver.js's
|
|
177
|
-
// generic "Operator run <shortid>" / "Operator runs" fallback (no
|
|
178
|
-
// pipelineId access there). Same fix applies: prefer the pipeline
|
|
179
|
-
// we're actually dispatching over an uninformative fallback label.
|
|
180
|
-
//
|
|
181
|
-
// pipelineId may be a composite child id (engine/loader.js's
|
|
182
|
-
// `<pipeline>::NODE[::NODE...]` shape for a subflow dispatch, e.g.
|
|
183
|
-
// `freelance_freelancer-proposal::PROPOSE::SUBMIT`) — every subflow of
|
|
184
|
-
// one manually-triggered run independently calls recordTrigger with
|
|
185
|
-
// its own composite id, but they must all collapse to the SAME root
|
|
186
|
-
// label so http-tree.js's GROUP BY root_label merges them into one
|
|
187
|
-
// instance instead of fragmenting one run into N rows, one per node.
|
|
188
|
-
if (pipelineId && (rootKind === "explicit" || rootKind === "operator-trigger")) {
|
|
189
|
-
rootLabel = pipelineId.split("::")[0];
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
await store.upsertLineage({
|
|
193
|
-
child_session_key: `flow:${flowId}`,
|
|
194
|
-
parent_session_key: parentSessionKey,
|
|
195
|
-
root_session_key: rootKey,
|
|
196
|
-
kind: "flow-triggered",
|
|
197
|
-
root_kind: rootKind,
|
|
198
|
-
root_label: rootLabel,
|
|
199
|
-
agent_id: null,
|
|
200
|
-
pipeline_id: pipelineId ?? null,
|
|
201
|
-
flow_id: flowId,
|
|
202
|
-
run_id: runId ?? null,
|
|
203
|
-
node_id: null,
|
|
204
|
-
item_key: null,
|
|
205
|
-
first_seen_at: Date.now(),
|
|
206
|
-
source: "trigger_hook",
|
|
207
|
-
});
|
|
208
|
-
return { ok: true };
|
|
209
|
-
} catch (err) {
|
|
210
|
-
logger.warn(`telemetry.recordTrigger: ${err?.message ?? err}`);
|
|
211
|
-
return { ok: false, reason: err?.message ?? String(err) };
|
|
212
|
-
}
|
|
213
|
-
},
|
|
214
|
-
|
|
215
|
-
async recordSpawn({ childSession, flowId, nodeId, itemKey, pipelineId, agentId, kind, source = "spawn_hook" }) {
|
|
216
|
-
try {
|
|
217
|
-
const store = SHARED_STORE, resolver = SHARED_RESOLVER;
|
|
218
|
-
if (!store) {
|
|
219
|
-
PENDING_CALLS.push({ type: "spawn", args: { childSession, flowId, nodeId, itemKey, pipelineId, agentId, kind, source } });
|
|
220
|
-
return { ok: false, reason: "not_ready", queued: true };
|
|
221
|
-
}
|
|
222
|
-
if (!childSession || !flowId) return { ok: false, reason: "missing_args" };
|
|
223
|
-
|
|
224
|
-
// Resolve the real root by walking the flow's own trigger edge
|
|
225
|
-
// (flow:<flowId>) instead of hardcoding "flow-node" — a flow-node
|
|
226
|
-
// subagent's cost should attribute to whatever triggered the flow
|
|
227
|
-
// (cron, operator, etc.), not to a made-up kind of its own. Falls
|
|
228
|
-
// back to `kind ?? "flow-node"` only if the resolver isn't ready or
|
|
229
|
-
// the trigger edge hasn't landed yet (race between flow start and
|
|
230
|
-
// recordTrigger) — the HTTP-layer ATTR_JOIN re-attribution still
|
|
231
|
-
// recovers those.
|
|
232
|
-
const parentKey = `flow:${flowId}`;
|
|
233
|
-
let rootKind = kind ?? "flow-node", rootLabel = null, rootSessionKey = childSession;
|
|
234
|
-
if (resolver) {
|
|
235
|
-
const chain = await resolver.walk(parentKey);
|
|
236
|
-
const rootFrame = chain[chain.length - 1];
|
|
237
|
-
if (rootFrame) {
|
|
238
|
-
rootKind = rootFrame.rootKind ?? rootFrame.kind ?? rootKind;
|
|
239
|
-
rootLabel = rootFrame.rootLabel ?? null;
|
|
240
|
-
rootSessionKey = rootFrame.rootKey ?? rootFrame.key ?? rootSessionKey;
|
|
241
|
-
}
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
await store.upsertLineage({
|
|
245
|
-
child_session_key: childSession,
|
|
246
|
-
parent_session_key: parentKey,
|
|
247
|
-
root_session_key: rootSessionKey,
|
|
248
|
-
kind: kind ?? "flow-node",
|
|
249
|
-
root_kind: rootKind,
|
|
250
|
-
root_label: rootLabel,
|
|
251
|
-
agent_id: agentId ?? null,
|
|
252
|
-
pipeline_id: pipelineId ?? null,
|
|
253
|
-
flow_id: flowId,
|
|
254
|
-
node_id: nodeId ?? null,
|
|
255
|
-
item_key: itemKey ?? null,
|
|
256
|
-
first_seen_at: Date.now(),
|
|
257
|
-
source,
|
|
258
|
-
});
|
|
259
|
-
return { ok: true };
|
|
260
|
-
} catch (err) {
|
|
261
|
-
logger.warn(`telemetry.recordSpawn: ${err?.message ?? err}`);
|
|
262
|
-
return { ok: false, reason: err?.message ?? String(err) };
|
|
263
|
-
}
|
|
264
|
-
},
|
|
265
|
-
};
|
|
266
|
-
|
|
267
|
-
// ── CLI ────────────────────────────────────────────────────────────────────
|
|
268
|
-
|
|
269
|
-
async function ensureStore() {
|
|
270
|
-
if (SHARED_STORE) return SHARED_STORE;
|
|
271
|
-
try {
|
|
272
|
-
SHARED_STORE = await openStore({ dbPath });
|
|
273
|
-
SHARED_RESOLVER = makeResolver({ store: SHARED_STORE });
|
|
274
|
-
} catch (err) {
|
|
275
|
-
logger.error(`telemetry CLI: lazy store init failed: ${err?.message ?? err}`);
|
|
276
|
-
}
|
|
277
|
-
return SHARED_STORE;
|
|
278
|
-
}
|
|
279
|
-
api.registerCli(
|
|
280
|
-
(ctx) => registerCliCommands({
|
|
281
|
-
program: ctx.program,
|
|
282
|
-
logger,
|
|
283
|
-
getStore: () => ensureStore(),
|
|
284
|
-
getResolver: async () => { await ensureStore(); return SHARED_RESOLVER; },
|
|
285
|
-
dbPath,
|
|
286
|
-
}),
|
|
287
|
-
{
|
|
288
|
-
commands: ["lineage"],
|
|
289
|
-
descriptors: [{
|
|
290
|
-
name: "lineage",
|
|
291
|
-
description: "Session lineage + per-call cost attribution (lookup / stats / cost)",
|
|
292
|
-
hasSubcommands: true,
|
|
293
|
-
}],
|
|
294
|
-
},
|
|
295
|
-
);
|
|
296
|
-
|
|
297
|
-
logger.info("telemetry: registered (subagent_spawned + session_start + before_message_write + CLI)");
|
|
298
|
-
}
|
|
299
|
-
|
|
300
|
-
function parseTs(t) {
|
|
301
|
-
if (typeof t === "number") return t;
|
|
302
|
-
if (typeof t === "string") {
|
|
303
|
-
const ms = Date.parse(t);
|
|
304
|
-
return Number.isFinite(ms) ? ms : null;
|
|
305
|
-
}
|
|
306
|
-
return null;
|
|
307
|
-
}
|
package/telemetry/ingest.js
DELETED
|
@@ -1,134 +0,0 @@
|
|
|
1
|
-
// ingest.js — telemetry event generation and reconciliation.
|
|
2
|
-
//
|
|
3
|
-
// Harnesses observe their own lifecycle; core owns what those observations
|
|
4
|
-
// mean. A harness adapter posts semantic events here (lineage.session,
|
|
5
|
-
// lineage.spawn, cost.usage) and this module derives the lineage/cost rows,
|
|
6
|
-
// resolves ancestry and applies the budget check. No harness field names
|
|
7
|
-
// appear below — the adapter's translation layer carries those.
|
|
8
|
-
//
|
|
9
|
-
// Events are best-effort: a malformed event is logged and dropped, never
|
|
10
|
-
// thrown back at the poster.
|
|
11
|
-
|
|
12
|
-
import { resolveCost } from "./pricer.js";
|
|
13
|
-
import { checkBudget } from "./budget-alert.js";
|
|
14
|
-
|
|
15
|
-
const KINDS = new Map([
|
|
16
|
-
["lineage.session", applyLineageSession],
|
|
17
|
-
["lineage.spawn", applyLineageSpawn],
|
|
18
|
-
["cost.usage", applyCostUsage],
|
|
19
|
-
]);
|
|
20
|
-
|
|
21
|
-
export function ingestKinds() {
|
|
22
|
-
return [...KINDS.keys()];
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
export async function applyTelemetryEvent(event, deps) {
|
|
26
|
-
const apply = KINDS.get(event?.kind);
|
|
27
|
-
if (!apply) return { ok: false, code: "unknown_kind" };
|
|
28
|
-
const { getStore, getResolver, logger = null } = deps;
|
|
29
|
-
try {
|
|
30
|
-
const store = getStore?.();
|
|
31
|
-
const resolver = getResolver?.();
|
|
32
|
-
if (!store || !resolver) return { ok: false, code: "store_not_ready" };
|
|
33
|
-
await apply(event, { ...deps, store, resolver, logger });
|
|
34
|
-
return { ok: true };
|
|
35
|
-
} catch (err) {
|
|
36
|
-
logger?.warn?.(`telemetry[${event?.kind}]: ${err?.message ?? err}`);
|
|
37
|
-
return { ok: false, code: "apply_failed", error: err?.message ?? String(err) };
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
async function applyLineageSession(event, { store, resolver }) {
|
|
42
|
-
const session = event?.session;
|
|
43
|
-
if (!session) return;
|
|
44
|
-
const existing = await store.getLineage(session);
|
|
45
|
-
if (existing && existing.source === "spawn_hook") return;
|
|
46
|
-
const source = event?.source === "session_start" ? "session_start_hook" : "start_hook";
|
|
47
|
-
const row = await resolver.resolve(session, { source });
|
|
48
|
-
if (!row) return;
|
|
49
|
-
row.agent_id = row.agent_id || event?.agentId || null;
|
|
50
|
-
if (event?.parentSession) row.parent_session_key = event.parentSession;
|
|
51
|
-
await store.upsertLineage(row);
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
async function applyLineageSpawn(event, { store, resolver }) {
|
|
55
|
-
const childSession = event?.session;
|
|
56
|
-
if (!childSession) return;
|
|
57
|
-
const parentKey = event?.parentSession || null;
|
|
58
|
-
const row = (await resolver.resolve(childSession, { source: "spawn_hook" })) || {};
|
|
59
|
-
row.child_session_key = childSession;
|
|
60
|
-
row.parent_session_key = parentKey || row.parent_session_key || null;
|
|
61
|
-
row.agent_id = row.agent_id || event?.agentId || null;
|
|
62
|
-
|
|
63
|
-
if (parentKey) {
|
|
64
|
-
const pChain = await resolver.walk(parentKey);
|
|
65
|
-
if (pChain.length) {
|
|
66
|
-
const pRoot = pChain[pChain.length - 1];
|
|
67
|
-
row.root_session_key = pRoot.key;
|
|
68
|
-
row.root_kind = pRoot.kind;
|
|
69
|
-
row.root_label = pRoot.rootLabel ?? pRoot.key;
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
row.kind = row.kind || "unknown";
|
|
73
|
-
row.root_kind = row.root_kind || row.kind;
|
|
74
|
-
row.root_session_key = row.root_session_key || childSession;
|
|
75
|
-
row.source = "spawn_hook";
|
|
76
|
-
await store.upsertLineage(row);
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
async function applyCostUsage(event, { store, resolver, getBroadcast, logger, emit }) {
|
|
80
|
-
const usage = event?.usage;
|
|
81
|
-
if (!usage || typeof usage !== "object") return;
|
|
82
|
-
const provider = event?.provider ?? null;
|
|
83
|
-
const model = event?.model ?? null;
|
|
84
|
-
const cost = resolveCost({ provider, model, usage });
|
|
85
|
-
if (!cost || typeof cost.total !== "number") return;
|
|
86
|
-
|
|
87
|
-
const session = event?.session ?? null;
|
|
88
|
-
const sessionId = event?.sessionId ?? session ?? "unknown";
|
|
89
|
-
const msgId = event?.messageId ?? `${sessionId}-${event?.timestamp ?? Date.now()}`;
|
|
90
|
-
const ts = parseTs(event?.timestamp) ?? Date.now();
|
|
91
|
-
|
|
92
|
-
let lineage = session ? await store.getLineage(session) : null;
|
|
93
|
-
if (!lineage && session) {
|
|
94
|
-
const row = await resolver.resolve(session, { source: "lazy_resolve" });
|
|
95
|
-
if (row) { await store.upsertLineage(row); lineage = row; }
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
await store.insertCost({
|
|
99
|
-
session_id: sessionId,
|
|
100
|
-
msg_id: msgId,
|
|
101
|
-
ts,
|
|
102
|
-
provider, model,
|
|
103
|
-
input_tokens: usage.input ?? null,
|
|
104
|
-
output_tokens: usage.output ?? null,
|
|
105
|
-
cache_read_tokens: usage.cacheRead ?? null,
|
|
106
|
-
cache_write_tokens: usage.cacheWrite ?? null,
|
|
107
|
-
total_tokens: usage.totalTokens ?? usage.total ?? null,
|
|
108
|
-
cost_input: cost.input ?? null,
|
|
109
|
-
cost_output: cost.output ?? null,
|
|
110
|
-
cost_cache_read: cost.cacheRead ?? null,
|
|
111
|
-
cost_cache_write: cost.cacheWrite ?? null,
|
|
112
|
-
cost_total: cost.total,
|
|
113
|
-
session_key: session,
|
|
114
|
-
agent_id: lineage?.agent_id ?? event?.agentId ?? null,
|
|
115
|
-
root_session_key: lineage?.root_session_key ?? session,
|
|
116
|
-
root_kind: lineage?.root_kind ?? lineage?.kind ?? null,
|
|
117
|
-
root_label: lineage?.root_label ?? null,
|
|
118
|
-
pipeline_id: lineage?.pipeline_id ?? null,
|
|
119
|
-
flow_id: lineage?.flow_id ?? null,
|
|
120
|
-
node_id: lineage?.node_id ?? null,
|
|
121
|
-
item_key: lineage?.item_key ?? null,
|
|
122
|
-
});
|
|
123
|
-
try { await emit?.("cost.record.added", { costTotal: cost.total, flowId: lineage?.flow_id ?? null }); } catch {}
|
|
124
|
-
checkBudget({ store, logger, justWroteUsd: cost.total, notify: (payload) => getBroadcast?.()?.("plugin.manifold.budget.alert", payload) });
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
function parseTs(value) {
|
|
128
|
-
if (typeof value === "number" && Number.isFinite(value)) return value;
|
|
129
|
-
if (typeof value === "string") {
|
|
130
|
-
const ms = Date.parse(value);
|
|
131
|
-
return Number.isFinite(ms) ? ms : null;
|
|
132
|
-
}
|
|
133
|
-
return null;
|
|
134
|
-
}
|
package/telemetry/labels.js
DELETED
|
@@ -1,115 +0,0 @@
|
|
|
1
|
-
// labels — translate internal scope names to operator-friendly display
|
|
2
|
-
// strings. Used by HTTP responses so clients don't need their own
|
|
3
|
-
// translation table (single source of truth).
|
|
4
|
-
//
|
|
5
|
-
// Mapping is intentionally lossy in the friendly direction — we hide
|
|
6
|
-
// uuids and internal scope names because the operator doesn't read those.
|
|
7
|
-
|
|
8
|
-
// Matches any label carrying a UUID — the shape of a hand-typed or scripted
|
|
9
|
-
// `--session-id` (test/forge scaffolding, one-off debugging labels), as
|
|
10
|
-
// opposed to a real pipelineId (which never contains one). Detecting by
|
|
11
|
-
// shape instead of enumerating known prefixes ("test-", "verify-",
|
|
12
|
-
// "scorer-creation-", ...) means new ad hoc labels collapse into the same
|
|
13
|
-
// bucket automatically instead of adding fresh noise every time someone
|
|
14
|
-
// types a new one.
|
|
15
|
-
const UUID_RE = /[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/i;
|
|
16
|
-
|
|
17
|
-
// Display label for cost that exists but belongs to no pipeline — the
|
|
18
|
-
// "Where it's going" breakdown's bucket for every cost row whose lineage
|
|
19
|
-
// walk landed on a root that isn't a flow (cron-owned agent turns,
|
|
20
|
-
// operator ad-hoc sessions, spawned subagents, Telegram DMs).
|
|
21
|
-
//
|
|
22
|
-
// Deliberately a real string rather than SQL NULL: CostBreakdown.Row.label
|
|
23
|
-
// is a non-optional String on clients with strict decoding, and
|
|
24
|
-
// stripTeamPrefix passes non-strings through unchanged, so a null label
|
|
25
|
-
// fails the whole decode and silently blanks the Budget card rather than
|
|
26
|
-
// crashing. Kept in step with UNATTRIBUTED_FILTER below so the projection,
|
|
27
|
-
// the transform, and any future client agree on one value.
|
|
28
|
-
export const UNATTRIBUTED_LABEL = "(unattributed)";
|
|
29
|
-
|
|
30
|
-
// The `filter` payload that round-trips UNATTRIBUTED_LABEL back to the NULL
|
|
31
|
-
// rows it was computed from. Mirrors the "__null__" sentinel /cost/top-calls
|
|
32
|
-
// already accepts (see http-cost.js's top-calls filter loop): a literal
|
|
33
|
-
// `pipeline_id = '(unattributed)'` would match zero rows, because no row
|
|
34
|
-
// stores that string — only the projection synthesizes it.
|
|
35
|
-
export const UNATTRIBUTED_FILTER = { pipeline_id: "__null__" };
|
|
36
|
-
|
|
37
|
-
// Pipeline ids are always shaped `<team>_<name>` (workspace/.pipelines/<team>_<name>.js —
|
|
38
|
-
// the same convention every team's pipelines follow). Stripping the leading
|
|
39
|
-
// segment up to the first underscore is therefore a shape-based rule, not a
|
|
40
|
-
// specific-team rule: it applies identically regardless of which team owns
|
|
41
|
-
// the pipeline, so no team name is ever hardcoded here.
|
|
42
|
-
export function stripTeamPrefix(pipelineId) {
|
|
43
|
-
return typeof pipelineId === "string" ? pipelineId.replace(/^[^_]+_/, "") : pipelineId;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
export function displayRootLabel(rootKind, rootLabel) {
|
|
47
|
-
if (!rootLabel || rootKind === "unknown") return null;
|
|
48
|
-
switch (rootKind) {
|
|
49
|
-
case "cron-owner":
|
|
50
|
-
// rootLabel here is the cron job's own free-text name (grammar.js's
|
|
51
|
-
// CRON_OWN capture) — an opaque string we don't generate and have no
|
|
52
|
-
// shape contract for, so it's shown as-is rather than guessing at a
|
|
53
|
-
// team-prefix boundary that may not exist or may differ per job.
|
|
54
|
-
return rootLabel;
|
|
55
|
-
case "telegram-dm":
|
|
56
|
-
// rootLabel is the chat id — opaque, so the label is a constant
|
|
57
|
-
// rather than a pass-through.
|
|
58
|
-
return "Chat (Telegram)";
|
|
59
|
-
case "operator-trigger":
|
|
60
|
-
// rootLabel is the triggering pipelineId (routes/operator.js's
|
|
61
|
-
// recordTrigger call) — strip the team prefix for terseness. Legacy
|
|
62
|
-
// rows still literally "Operator runs" have no underscore, so
|
|
63
|
-
// stripTeamPrefix is a no-op on them.
|
|
64
|
-
return stripTeamPrefix(rootLabel);
|
|
65
|
-
case "explicit":
|
|
66
|
-
// The most common explicit purpose is "graph-flow-owner" — used by
|
|
67
|
-
// any flow launched from the CLI or operator-initiated tool call.
|
|
68
|
-
if (rootLabel === "graph-flow-owner") return "Operator runs";
|
|
69
|
-
// Skill-test / probe / forge sessions
|
|
70
|
-
if (rootLabel.startsWith("skill-")) return `Skill test: ${rootLabel.replace(/^skill-/, "")}`;
|
|
71
|
-
// Hand-typed or scripted --session-id values (test/forge scaffolding,
|
|
72
|
-
// one-off debugging) — collapse by shape (see UUID_RE above) rather
|
|
73
|
-
// than passing through raw, uuid-suffixed strings that clutter the
|
|
74
|
-
// instance list. http-tree.js merges same-display-label instance rows,
|
|
75
|
-
// so every ad hoc label lands in one consolidated bucket.
|
|
76
|
-
if (UUID_RE.test(rootLabel)) return "Ad-hoc sessions";
|
|
77
|
-
// Manually-triggered pipeline runs (index.js's recordTrigger overrides
|
|
78
|
-
// rootLabel to the pipelineId when the resolved root is otherwise a bare
|
|
79
|
-
// ad hoc session) — same shape-based stripping as operator-trigger.
|
|
80
|
-
return stripTeamPrefix(rootLabel);
|
|
81
|
-
case "agent-default":
|
|
82
|
-
return `${rootLabel} (idle agent)`;
|
|
83
|
-
case "bare-subagent":
|
|
84
|
-
return "Spawned subagent";
|
|
85
|
-
case "memory-synth-chunk":
|
|
86
|
-
return "Memory synthesis";
|
|
87
|
-
case "flow-triggered":
|
|
88
|
-
return rootLabel || "Pipeline run";
|
|
89
|
-
case "flow-prime":
|
|
90
|
-
return "Pipeline prime";
|
|
91
|
-
case "flow-node":
|
|
92
|
-
case "flow-gate-discuss":
|
|
93
|
-
// These are mid-chain frames; if they appear as root it's because
|
|
94
|
-
// the parent flow was pruned. Surface gently.
|
|
95
|
-
return `Pipeline (parent expired)`;
|
|
96
|
-
case "cron-tick":
|
|
97
|
-
// Mid-chain frame surfacing as root — rare.
|
|
98
|
-
return `Cron run`;
|
|
99
|
-
default:
|
|
100
|
-
return rootLabel;
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
export function displayRootKind(rootKind) {
|
|
105
|
-
switch (rootKind) {
|
|
106
|
-
case "cron-owner":
|
|
107
|
-
case "cron-tick": return "Cron";
|
|
108
|
-
case "telegram-dm": return "Telegram";
|
|
109
|
-
case "operator-trigger":
|
|
110
|
-
case "explicit":
|
|
111
|
-
case "agent_tool":
|
|
112
|
-
case "cli": return "Operator";
|
|
113
|
-
default: return "unknown";
|
|
114
|
-
}
|
|
115
|
-
}
|
package/telemetry/pricer.js
DELETED
|
@@ -1,77 +0,0 @@
|
|
|
1
|
-
// pricer — when the runner fails to stamp usage.cost in a JSONL message
|
|
2
|
-
// (model not registered in config, fallback chain breakage, etc.), compute
|
|
3
|
-
// dollar cost from token counts × per-million rates. Used by the
|
|
4
|
-
// before_message_write hook so hook-written rows converge on one value.
|
|
5
|
-
//
|
|
6
|
-
// Rates sourced from openclaw.json's per-model `cost` blocks where present.
|
|
7
|
-
// Adding a new (provider, model): just append to RATES.
|
|
8
|
-
|
|
9
|
-
const RATES = {
|
|
10
|
-
// openrouter
|
|
11
|
-
"openrouter|deepseek/deepseek-v4-pro": { in: 0.435, out: 0.870, cR: 0.003625, cW: 0 },
|
|
12
|
-
"openrouter|deepseek/deepseek-v4-flash": { in: 0.0983, out: 0.1966, cR: 0.0197, cW: 0 },
|
|
13
|
-
"openrouter|anthropic/claude-sonnet-4.6":{ in: 3.0, out: 15.0, cR: 0.3, cW: 3.75 },
|
|
14
|
-
|
|
15
|
-
// zai (general endpoint)
|
|
16
|
-
"zai|glm-4.5-flash": { in: 0, out: 0, cR: 0, cW: 0 },
|
|
17
|
-
|
|
18
|
-
// zai-coding (GLM Coding Plan dedicated endpoint — required for glm-5.1
|
|
19
|
-
// on this account; the general endpoint rejects it with "insufficient
|
|
20
|
-
// balance", the plan's resource package only being visible from here)
|
|
21
|
-
"zai-coding|glm-5.1": { in: 1.2, out: 4.0, cR: 0.24, cW: 0 },
|
|
22
|
-
|
|
23
|
-
// evolink-anthropic
|
|
24
|
-
"evolink-anthropic|claude-sonnet-4-6": { in: 2.7, out: 13.5, cR: 0.271, cW: 3.375 },
|
|
25
|
-
"evolink-anthropic|deepseek-v4-pro": { in: 1.74, out: 3.48, cR: 0, cW: 0 },
|
|
26
|
-
|
|
27
|
-
// evolink (Google/DeepSeek aggregator)
|
|
28
|
-
"evolink|gemini-2.5-flash-lite": { in: 0.094, out: 0.374, cR: 0.009, cW: 0 },
|
|
29
|
-
"evolink|gemini-3-pro-preview": { in: 1.865, out: 11.182, cR: 0.187, cW: 0 },
|
|
30
|
-
"evolink|deepseek-reasoner": { in: 0.294, out: 0.441, cR: 0.029, cW: 0 },
|
|
31
|
-
};
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* Returns { input, output, cacheRead, cacheWrite, total } in dollars,
|
|
35
|
-
* or null if (provider, model) isn't in the rate table.
|
|
36
|
-
*/
|
|
37
|
-
export function computeCost(provider, model, usage) {
|
|
38
|
-
if (!provider || !model || !usage) return null;
|
|
39
|
-
const rate = RATES[`${provider}|${model}`];
|
|
40
|
-
if (!rate) return null;
|
|
41
|
-
const ci = ((usage.input ?? 0) / 1_000_000) * rate.in;
|
|
42
|
-
const co = ((usage.output ?? 0) / 1_000_000) * rate.out;
|
|
43
|
-
const cR = ((usage.cacheRead ?? 0) / 1_000_000) * rate.cR;
|
|
44
|
-
const cW = ((usage.cacheWrite ?? 0) / 1_000_000) * rate.cW;
|
|
45
|
-
return { input: ci, output: co, cacheRead: cR, cacheWrite: cW, total: ci + co + cR + cW };
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
/**
|
|
49
|
-
* Take what the source has (usage.cost) and, if it's all zeros despite
|
|
50
|
-
* real tokens, fall back to computed cost. Annotates the returned row
|
|
51
|
-
* with the source so callers can filter.
|
|
52
|
-
*/
|
|
53
|
-
export function resolveCost({ provider, model, usage }) {
|
|
54
|
-
const srcCost = usage?.cost;
|
|
55
|
-
const hasTokens =
|
|
56
|
-
(usage?.input ?? 0) > 0 ||
|
|
57
|
-
(usage?.output ?? 0) > 0 ||
|
|
58
|
-
(usage?.cacheRead ?? 0) > 0 ||
|
|
59
|
-
(usage?.cacheWrite ?? 0) > 0;
|
|
60
|
-
const srcTotal = typeof srcCost?.total === "number" ? srcCost.total : 0;
|
|
61
|
-
|
|
62
|
-
if (srcTotal > 0 || !hasTokens) {
|
|
63
|
-
return { source: "runner", ...srcCost, total: srcTotal };
|
|
64
|
-
}
|
|
65
|
-
// Source has zero cost but real tokens — try to compute
|
|
66
|
-
const computed = computeCost(provider, model, usage);
|
|
67
|
-
if (computed) return { source: "computed", ...computed };
|
|
68
|
-
// Unknown rate — record zero but mark unknown
|
|
69
|
-
return { source: "unknown", input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 };
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
export function knownModels() {
|
|
73
|
-
return Object.keys(RATES).map((k) => {
|
|
74
|
-
const [provider, model] = k.split("|");
|
|
75
|
-
return { provider, model };
|
|
76
|
-
});
|
|
77
|
-
}
|