@codetelemetry/mcp 0.3.8 → 0.3.12
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/LICENSE +1 -1
- package/README.md +90 -9
- package/dist/src/client/agent.d.ts +221 -3
- package/dist/src/client/agent.js +174 -0
- package/dist/src/client/assistants.d.ts +4 -0
- package/dist/src/client/assistants.js +5 -0
- package/dist/src/client/bin.d.ts +2 -0
- package/dist/src/client/bin.js +3 -0
- package/dist/src/client/cli.d.ts +1 -0
- package/dist/src/client/cli.js +567 -0
- package/dist/src/client/clients.d.ts +34 -0
- package/dist/src/client/clients.js +74 -0
- package/dist/src/client/codexConfig.d.ts +1 -0
- package/dist/src/client/codexConfig.js +5 -0
- package/dist/src/client/config.d.ts +7 -0
- package/dist/src/client/config.js +7 -0
- package/dist/src/client/graderSeed.d.ts +50 -0
- package/dist/src/client/graderSeed.js +289 -0
- package/dist/src/client/hostMetrics.d.ts +54 -0
- package/dist/src/client/hostMetrics.js +425 -0
- package/dist/src/client/import/browser.d.ts +30 -0
- package/dist/src/client/import/browser.js +157 -0
- package/dist/src/client/import/convert.d.ts +20 -0
- package/dist/src/client/import/convert.js +165 -0
- package/dist/src/client/import/core.d.ts +143 -0
- package/dist/src/client/import/core.js +441 -0
- package/dist/src/client/import/ledger.d.ts +52 -0
- package/dist/src/client/import/ledger.js +161 -0
- package/dist/src/client/import/model.d.ts +70 -0
- package/dist/src/client/import/model.js +42 -0
- package/dist/src/client/import/options.d.ts +24 -0
- package/dist/src/client/import/options.js +71 -0
- package/dist/src/client/import/otlpImport.d.ts +7 -0
- package/dist/src/client/import/otlpImport.js +81 -0
- package/dist/src/client/import/protocol.d.ts +35 -0
- package/dist/src/client/import/protocol.js +51 -0
- package/dist/src/client/import/run.d.ts +30 -0
- package/dist/src/client/import/run.js +127 -0
- package/dist/src/client/import/sources/claudeCodeText.d.ts +19 -0
- package/dist/src/client/import/sources/claudeCodeText.js +214 -0
- package/dist/src/client/import/sources/codexText.d.ts +20 -0
- package/dist/src/client/import/sources/codexText.js +319 -0
- package/dist/src/client/import/sources/files.d.ts +24 -0
- package/dist/src/client/import/sources/files.js +129 -0
- package/dist/src/client/import/transport.d.ts +76 -0
- package/dist/src/client/import/transport.js +141 -0
- package/dist/src/client/index.d.ts +19 -0
- package/dist/src/client/index.js +19 -0
- package/dist/src/client/kiro/collector.d.ts +88 -0
- package/dist/src/client/kiro/collector.js +577 -0
- package/dist/src/client/kiro/command.d.ts +3 -0
- package/dist/src/client/kiro/command.js +109 -0
- package/dist/src/client/kiro/config.d.ts +39 -0
- package/dist/src/client/kiro/config.js +107 -0
- package/dist/src/client/kiro/records.d.ts +52 -0
- package/dist/src/client/kiro/records.js +206 -0
- package/dist/src/client/kiro/service.d.ts +21 -0
- package/dist/src/client/kiro/service.js +183 -0
- package/dist/src/client/kiro/state.d.ts +45 -0
- package/dist/src/client/kiro/state.js +205 -0
- package/dist/src/client/kiro/transport.d.ts +9 -0
- package/dist/src/client/kiro/transport.js +90 -0
- package/dist/src/client/onboard.d.ts +5 -1
- package/dist/src/client/onboard.js +28 -23
- package/dist/src/client/piConfig.d.ts +29 -0
- package/dist/src/client/piConfig.js +112 -0
- package/dist/src/client/plan.d.ts +1 -1
- package/dist/src/client/plan.js +2 -1
- package/dist/src/client/settings.d.ts +4 -0
- package/dist/src/client/settings.js +17 -0
- package/dist/src/client/yaml.d.ts +5 -0
- package/dist/src/client/yaml.js +72 -0
- package/dist/src/mcpServer.js +1 -1
- package/dist/src/tools.d.ts +9 -6
- package/dist/src/tools.js +478 -60
- package/dist/src/version.d.ts +1 -1
- package/dist/src/version.js +1 -1
- package/package.json +178 -9
- package/tool-manifest.json +497 -21
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function run(argv: string[]): Promise<number>;
|
|
@@ -0,0 +1,567 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import { runKiroCommand } from "./kiro/command.js";
|
|
3
|
+
import { spawn } from "node:child_process";
|
|
4
|
+
import { onboard } from "./onboard.js";
|
|
5
|
+
import { resolveEndpoints } from "./config.js";
|
|
6
|
+
import { CLIENTS, PI_EXTENSION_HINT } from "./clients.js";
|
|
7
|
+
import { ONBOARD_CLIENTS, isOnboardClient } from "./assistants.js";
|
|
8
|
+
import { looksLikeKey, mask } from "./keys.js";
|
|
9
|
+
import { runImport } from "./import/run.js";
|
|
10
|
+
import { parseContentFlag, parseWhen, resolveImportContent, resolveImportKey } from "./import/options.js";
|
|
11
|
+
import { requestDeviceCode, pollForKey } from "./deviceClient.js";
|
|
12
|
+
import { sendTestEvent } from "./otlp.js";
|
|
13
|
+
import { confirmLanded, verifyScope } from "./connectivity.js";
|
|
14
|
+
import { DEFAULT_KEY_ENV, defaultHostMetricsPaths, defaultInstanceId, hostMetricsOptionsFromFlags, renderHostMetricsConfig, writeHostMetricsConfig, } from "./hostMetrics.js";
|
|
15
|
+
function parse(argv) {
|
|
16
|
+
const _ = [];
|
|
17
|
+
const flags = {};
|
|
18
|
+
for (let i = 0; i < argv.length; i++) {
|
|
19
|
+
const a = argv[i];
|
|
20
|
+
if (a.startsWith("--")) {
|
|
21
|
+
const body = a.slice(2);
|
|
22
|
+
if (body.startsWith("no-")) {
|
|
23
|
+
flags[body.slice(3)] = false;
|
|
24
|
+
}
|
|
25
|
+
else if (body.includes("=")) {
|
|
26
|
+
const [k, v] = body.split(/=(.*)/s);
|
|
27
|
+
flags[k] = v;
|
|
28
|
+
}
|
|
29
|
+
else if (i + 1 < argv.length && !argv[i + 1].startsWith("--")) {
|
|
30
|
+
flags[body] = argv[++i];
|
|
31
|
+
}
|
|
32
|
+
else {
|
|
33
|
+
flags[body] = true;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
else {
|
|
37
|
+
_.push(a);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
return { _, flags };
|
|
41
|
+
}
|
|
42
|
+
const HELP = `codetelemetry-connect — wire Claude Code, OpenAI Codex or pi into CodeTelemetry
|
|
43
|
+
|
|
44
|
+
Usage:
|
|
45
|
+
codetelemetry-connect <cot_key> [options] Write config, send a test event, confirm it landed
|
|
46
|
+
codetelemetry-connect login [options] Authenticate via device code, then do the above
|
|
47
|
+
codetelemetry-connect doctor [options] Re-test the already-configured key
|
|
48
|
+
codetelemetry-connect print-config Print the config block (no key)
|
|
49
|
+
codetelemetry-connect host-metrics [opts] Write the OpenTelemetry Collector
|
|
50
|
+
config that reports host + agentd
|
|
51
|
+
process health (see below)
|
|
52
|
+
codetelemetry-connect kiro status|collect|enable|disable|uninstall
|
|
53
|
+
Manage the background V3 transcript collector
|
|
54
|
+
codetelemetry-connect import [opts] Backfill past Claude Code / Codex
|
|
55
|
+
sessions from local history files
|
|
56
|
+
(see below)
|
|
57
|
+
|
|
58
|
+
Options:
|
|
59
|
+
--client claude|codex|pi|kiro
|
|
60
|
+
Which coding agent to configure (default claude).
|
|
61
|
+
pi: write the otel block of ~/.pi/agent/settings.json
|
|
62
|
+
(traces only; pi also needs its exporter extension:
|
|
63
|
+
\`pi install npm:pi-otel\`).
|
|
64
|
+
--codex Same as --client codex. Target OpenAI Codex: write the [otel] section of
|
|
65
|
+
~/.codex/config.toml (or \$CODEX_HOME/config.toml when set)
|
|
66
|
+
instead of Claude Code's settings.json. (Codex needs the
|
|
67
|
+
literal key in the file — it does not expand \${VAR}
|
|
68
|
+
placeholders — which is why this flag exists.)
|
|
69
|
+
--scope user|project Which settings.json to write (default: user → ~/.claude/settings.json)
|
|
70
|
+
--settings <path> Explicit config path (overrides --scope; with --codex,
|
|
71
|
+
overrides ~/.codex/config.toml; with --client pi,
|
|
72
|
+
~/.pi/agent/settings.json)
|
|
73
|
+
--api-base <url> Onboarding API base (default: https://codetelemetry.com)
|
|
74
|
+
--endpoint <url> OTLP ingest endpoint (default: https://ingest.codetelemetry.com)
|
|
75
|
+
--enrich Also enable prompt/body/tool-content flags (richer dashboards)
|
|
76
|
+
--no-open On \`login\`, don't auto-open the browser (just print the URL)
|
|
77
|
+
--no-test Skip sending the test event
|
|
78
|
+
--no-verify Skip waiting for the probe to land
|
|
79
|
+
--expect-team <id> Require this organization
|
|
80
|
+
--expect-personal Require a personal key (never accept an org key)
|
|
81
|
+
--expect-tenant <id> Require the key owner tenant
|
|
82
|
+
--expect-billing-account <id>
|
|
83
|
+
Require this billing account attribution
|
|
84
|
+
--json Emit a JSON result instead of prose
|
|
85
|
+
-h, --help Show this help
|
|
86
|
+
|
|
87
|
+
host-metrics options:
|
|
88
|
+
--out <path> Where to write the collector config
|
|
89
|
+
(default: per-platform, printed by --print)
|
|
90
|
+
--print Print the config to stdout instead of writing it
|
|
91
|
+
--instance <id> service.instance.id (default: this machine's hostname)
|
|
92
|
+
--env <name> deployment.environment (default: production)
|
|
93
|
+
--interval <seconds> Scrape interval, 10-3600 (default: 60)
|
|
94
|
+
--gateway <url> Export to a user-managed collector instead of
|
|
95
|
+
CodeTelemetry directly; that collector fans out
|
|
96
|
+
--key-env <VAR> Env var the collector reads the ingest key from
|
|
97
|
+
(default: ${DEFAULT_KEY_ENV}). The key is never
|
|
98
|
+
written into the config file.
|
|
99
|
+
--no-auth Omit the Authorization header (trusted gateway only)
|
|
100
|
+
--allow-insecure Permit a plaintext http:// endpoint
|
|
101
|
+
--queue-dir <path> On-disk sending queue (survives collector restarts)
|
|
102
|
+
--no-queue-dir Use an in-memory queue instead
|
|
103
|
+
--queue-size <n> Max batches queued before backpressure (default: 1000)
|
|
104
|
+
--memory-limit <mib> Collector memory ceiling (default: 128)
|
|
105
|
+
--processes <a,b,c> Executable names counted as agentd process health
|
|
106
|
+
--interfaces Keep per-interface network breakdown (off by default)
|
|
107
|
+
--mounts Keep per-mount filesystem breakdown (off by default)
|
|
108
|
+
|
|
109
|
+
import options:
|
|
110
|
+
Reads ~/.claude/projects/**/*.jsonl (Claude Code) and ~/.codex/sessions/**
|
|
111
|
+
(Codex) locally and sends them as telemetry through the normal ingest, tagged
|
|
112
|
+
ingest.source=history-import. Sessions already captured live, or already
|
|
113
|
+
imported, are skipped; an interrupted import resumes on the next run.
|
|
114
|
+
Imported history has tokens, prompts and tool results, but no per-request
|
|
115
|
+
latency, retries, permission decisions or active time.
|
|
116
|
+
--dry-run Parse, check and summarize; send nothing
|
|
117
|
+
--source claude-code|codex|all Which history to read (default: all; --client
|
|
118
|
+
claude|codex narrows it too). pi is refused: it keeps
|
|
119
|
+
no local transcripts the importer reads
|
|
120
|
+
--since <Nd|date> Window start (default: 30d; at most 364d — raw
|
|
121
|
+
telemetry is kept 365 days by event time)
|
|
122
|
+
--until <Nd|date> Window end (default: now)
|
|
123
|
+
--project <path> Only sessions whose working directory is under <path>
|
|
124
|
+
--email <addr> Stamp user.email on imported rows
|
|
125
|
+
--content none|live|all|prompts,tools
|
|
126
|
+
Text to include (default: live — exactly what your
|
|
127
|
+
live exporters send: Claude OTEL_LOG_USER_PROMPTS /
|
|
128
|
+
OTEL_LOG_TOOL_DETAILS in settings.json, Codex
|
|
129
|
+
log_user_prompt). none = counts, sizes, models and
|
|
130
|
+
tool names only; all / prompts,tools = explicit opt-in
|
|
131
|
+
--max-sessions <n> Send at most n sessions this run
|
|
132
|
+
--rate <n> Ingest requests per minute (default: 120, max: 300;
|
|
133
|
+
the key's 600/min limit is shared with live telemetry)
|
|
134
|
+
--time-limit <min> Stop after this many minutes (default: 60); re-run to continue
|
|
135
|
+
--key-env <VAR> Env var holding the key (default: CODETELEMETRY_INGEST_KEY);
|
|
136
|
+
otherwise the key comes from the configured client
|
|
137
|
+
(--source codex: ~/.codex/config.toml first)
|
|
138
|
+
--codex-config <path> Codex config to read the key from (default: ~/.codex/config.toml)
|
|
139
|
+
--json Emit the full result as JSON`;
|
|
140
|
+
function out(line) {
|
|
141
|
+
process.stdout.write(line + "\n");
|
|
142
|
+
}
|
|
143
|
+
function openBrowser(url) {
|
|
144
|
+
const cmd = process.platform === "darwin" ? "open" : process.platform === "win32" ? "cmd" : "xdg-open";
|
|
145
|
+
const args = process.platform === "win32" ? ["/c", "start", "", url] : [url];
|
|
146
|
+
try {
|
|
147
|
+
const child = spawn(cmd, args, { stdio: "ignore", detached: true });
|
|
148
|
+
child.on("error", () => { });
|
|
149
|
+
child.unref();
|
|
150
|
+
return true;
|
|
151
|
+
}
|
|
152
|
+
catch {
|
|
153
|
+
return false;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
function pickScope(flags) {
|
|
157
|
+
return flags.scope === "project" ? "project" : "user";
|
|
158
|
+
}
|
|
159
|
+
function pickClient(flags) {
|
|
160
|
+
const named = flags.client;
|
|
161
|
+
if (named === undefined)
|
|
162
|
+
return flags.codex ? "codex" : "claude";
|
|
163
|
+
if (typeof named !== "string" || !isOnboardClient(named)) {
|
|
164
|
+
return `--client must be one of ${ONBOARD_CLIENTS.join(", ")}.`;
|
|
165
|
+
}
|
|
166
|
+
if (flags.codex && named !== "codex")
|
|
167
|
+
return `--codex conflicts with --client ${named}.`;
|
|
168
|
+
return named;
|
|
169
|
+
}
|
|
170
|
+
const str = (flags, key) => typeof flags[key] === "string" ? flags[key] : undefined;
|
|
171
|
+
const hasFlag = (flags, key) => Object.prototype.hasOwnProperty.call(flags, key);
|
|
172
|
+
function intFlag(flags, key) {
|
|
173
|
+
if (!hasFlag(flags, key))
|
|
174
|
+
return undefined;
|
|
175
|
+
const raw = str(flags, key);
|
|
176
|
+
if (raw === undefined)
|
|
177
|
+
return null;
|
|
178
|
+
const n = Number(raw);
|
|
179
|
+
return Number.isInteger(n) && n > 0 ? n : null;
|
|
180
|
+
}
|
|
181
|
+
function textFlag(flags, key) {
|
|
182
|
+
if (!hasFlag(flags, key))
|
|
183
|
+
return undefined;
|
|
184
|
+
const raw = flags[key];
|
|
185
|
+
if (typeof raw !== "string")
|
|
186
|
+
return null;
|
|
187
|
+
return raw.trim() || null;
|
|
188
|
+
}
|
|
189
|
+
function boolFlag(flags, key) {
|
|
190
|
+
if (!hasFlag(flags, key))
|
|
191
|
+
return false;
|
|
192
|
+
return flags[key] === true ? true : null;
|
|
193
|
+
}
|
|
194
|
+
function expectedScopeFromFlags(flags) {
|
|
195
|
+
const team = textFlag(flags, "expect-team");
|
|
196
|
+
const tenant = textFlag(flags, "expect-tenant");
|
|
197
|
+
const billing = textFlag(flags, "expect-billing-account");
|
|
198
|
+
const personal = boolFlag(flags, "expect-personal");
|
|
199
|
+
if (team === null)
|
|
200
|
+
return "--expect-team needs an organization id.";
|
|
201
|
+
if (tenant === null)
|
|
202
|
+
return "--expect-tenant needs a tenant id.";
|
|
203
|
+
if (billing === null)
|
|
204
|
+
return "--expect-billing-account needs an account id.";
|
|
205
|
+
if (personal === null)
|
|
206
|
+
return "--expect-personal takes no value.";
|
|
207
|
+
if (personal && team)
|
|
208
|
+
return "--expect-personal and --expect-team are mutually exclusive.";
|
|
209
|
+
return {
|
|
210
|
+
...(tenant ? { tenantId: tenant } : {}),
|
|
211
|
+
...(personal ? { teamId: null } : team ? { teamId: team } : {}),
|
|
212
|
+
...(billing ? { billingAccountId: billing } : tenant && personal ? { billingAccountId: tenant } : {}),
|
|
213
|
+
};
|
|
214
|
+
}
|
|
215
|
+
const COMMON_FLAGS = ["json", "help", "h", "api-base", "endpoint", "codex", "client", "scope", "settings"];
|
|
216
|
+
const ONBOARDING_FLAGS = new Set([
|
|
217
|
+
...COMMON_FLAGS,
|
|
218
|
+
"enrich", "open", "test", "verify",
|
|
219
|
+
"expect-team", "expect-personal", "expect-tenant", "expect-billing-account",
|
|
220
|
+
]);
|
|
221
|
+
const IMPORT_FLAGS = new Set([
|
|
222
|
+
"json", "help", "h", "api-base", "endpoint", "codex", "settings",
|
|
223
|
+
"dry-run", "source", "since", "until", "project", "email", "content", "max-sessions", "rate",
|
|
224
|
+
"time-limit", "key-env", "codex-config", "client",
|
|
225
|
+
]);
|
|
226
|
+
function unknownFlag(flags, allowed) {
|
|
227
|
+
for (const key of Object.keys(flags))
|
|
228
|
+
if (!allowed.has(key))
|
|
229
|
+
return key;
|
|
230
|
+
return null;
|
|
231
|
+
}
|
|
232
|
+
const SKIP_LABELS = {
|
|
233
|
+
older: "older than the window",
|
|
234
|
+
newer: "newer than the window",
|
|
235
|
+
active: "still active (written in the last 15 min)",
|
|
236
|
+
filtered: "outside --project",
|
|
237
|
+
ledger: "already imported (unchanged)",
|
|
238
|
+
empty: "nothing importable",
|
|
239
|
+
live: "already captured live",
|
|
240
|
+
already_imported: "already imported",
|
|
241
|
+
changed_since_import: "changed since an earlier import",
|
|
242
|
+
conflicting_partial: "unfinished import of another version (needs cleanup)",
|
|
243
|
+
settling: "imported moments ago — re-run in a few minutes",
|
|
244
|
+
unverifiable: "partial import could not be verified",
|
|
245
|
+
too_large: "file too large",
|
|
246
|
+
too_many_events: "too many events",
|
|
247
|
+
unreadable: "unreadable",
|
|
248
|
+
changed_during_run: "changed while importing",
|
|
249
|
+
deferred: "deferred (session cap / time limit)",
|
|
250
|
+
};
|
|
251
|
+
async function runImportCommand(positional, flags, endpoints, settingsOpts, client) {
|
|
252
|
+
const rogue = unknownFlag(flags, IMPORT_FLAGS);
|
|
253
|
+
if (rogue) {
|
|
254
|
+
out(`Unknown option --${rogue} for import.`);
|
|
255
|
+
return 1;
|
|
256
|
+
}
|
|
257
|
+
const switchValue = (name) => {
|
|
258
|
+
const v = flags[name];
|
|
259
|
+
if (v === undefined || v === false)
|
|
260
|
+
return { on: false };
|
|
261
|
+
if (typeof v === "string" && looksLikeKey(v))
|
|
262
|
+
return { on: true, key: v };
|
|
263
|
+
return { on: true };
|
|
264
|
+
};
|
|
265
|
+
const dry = switchValue("dry-run");
|
|
266
|
+
const json = switchValue("json");
|
|
267
|
+
const explicit = positional[1] ?? dry.key ?? json.key;
|
|
268
|
+
const now = Date.now();
|
|
269
|
+
const PI_REFUSAL = "import reads Claude Code and Codex history only: pi keeps no local session " +
|
|
270
|
+
"transcripts it can read. Use --source claude-code, codex or all.";
|
|
271
|
+
if (client === "kiro") {
|
|
272
|
+
out("Kiro V3 uses the bounded background collector. Use kiro collect; history import does not support this source.");
|
|
273
|
+
return 1;
|
|
274
|
+
}
|
|
275
|
+
if (client === "pi" || str(flags, "source") === "pi") {
|
|
276
|
+
out(PI_REFUSAL);
|
|
277
|
+
return 1;
|
|
278
|
+
}
|
|
279
|
+
const fromClient = flags.client === undefined && flags.codex !== true ? undefined
|
|
280
|
+
: client === "codex" ? "codex" : "claude-code";
|
|
281
|
+
const sourceRaw = str(flags, "source") ?? fromClient ?? "all";
|
|
282
|
+
if (!["all", "claude-code", "codex"].includes(sourceRaw)) {
|
|
283
|
+
out("--source must be claude-code, codex or all.");
|
|
284
|
+
return 1;
|
|
285
|
+
}
|
|
286
|
+
if (fromClient && str(flags, "source") && str(flags, "source") !== fromClient) {
|
|
287
|
+
out(`--source ${str(flags, "source")} conflicts with --client ${client}.`);
|
|
288
|
+
return 1;
|
|
289
|
+
}
|
|
290
|
+
const source = sourceRaw;
|
|
291
|
+
const paths = {
|
|
292
|
+
claudeSettings: CLIENTS.claude.resolveConfigPath(settingsOpts),
|
|
293
|
+
codexConfig: CLIENTS.codex.resolveConfigPath({ path: str(flags, "codex-config") }),
|
|
294
|
+
};
|
|
295
|
+
const keyEnv = str(flags, "key-env") ?? DEFAULT_KEY_ENV;
|
|
296
|
+
const resolved = resolveImportKey({ explicit, env: process.env[keyEnv], envName: keyEnv, source, paths });
|
|
297
|
+
if ("error" in resolved) {
|
|
298
|
+
out(resolved.error);
|
|
299
|
+
return 1;
|
|
300
|
+
}
|
|
301
|
+
const content = parseContentFlag(str(flags, "content"));
|
|
302
|
+
if (!content) {
|
|
303
|
+
out("--content must be none, live, all, or a comma list of prompts,tools.");
|
|
304
|
+
return 1;
|
|
305
|
+
}
|
|
306
|
+
const when = (name) => {
|
|
307
|
+
if (!hasFlag(flags, name))
|
|
308
|
+
return undefined;
|
|
309
|
+
const raw = str(flags, name);
|
|
310
|
+
return raw === undefined ? null : parseWhen(raw, now);
|
|
311
|
+
};
|
|
312
|
+
const sinceMs = when("since");
|
|
313
|
+
const untilMs = when("until");
|
|
314
|
+
if (sinceMs === null || untilMs === null) {
|
|
315
|
+
out("--since/--until take a number of days or hours (30d, 12h) or an ISO date (2026-09-01).");
|
|
316
|
+
return 1;
|
|
317
|
+
}
|
|
318
|
+
const maxSessions = intFlag(flags, "max-sessions");
|
|
319
|
+
const rate = intFlag(flags, "rate");
|
|
320
|
+
const timeLimit = intFlag(flags, "time-limit");
|
|
321
|
+
if (maxSessions === null || rate === null || timeLimit === null) {
|
|
322
|
+
out("--max-sessions, --rate and --time-limit take a positive whole number.");
|
|
323
|
+
return 1;
|
|
324
|
+
}
|
|
325
|
+
const project = textFlag(flags, "project");
|
|
326
|
+
const email = textFlag(flags, "email");
|
|
327
|
+
if (project === null || email === null) {
|
|
328
|
+
out("--project and --email need a value.");
|
|
329
|
+
return 1;
|
|
330
|
+
}
|
|
331
|
+
const log = json.on ? undefined : out;
|
|
332
|
+
log?.(`${dry.on ? "Dry run: reading" : "Importing"} local history with ${mask(resolved.key)} (from ${resolved.from})…`);
|
|
333
|
+
const result = await runImport({
|
|
334
|
+
key: resolved.key,
|
|
335
|
+
endpoints,
|
|
336
|
+
source,
|
|
337
|
+
sinceMs,
|
|
338
|
+
untilMs,
|
|
339
|
+
project,
|
|
340
|
+
email,
|
|
341
|
+
content: resolveImportContent(content, paths),
|
|
342
|
+
dryRun: dry.on,
|
|
343
|
+
maxSessions,
|
|
344
|
+
ratePerMinute: rate,
|
|
345
|
+
timeLimitMs: timeLimit === undefined ? undefined : timeLimit * 60_000,
|
|
346
|
+
importTool: "connect-cli",
|
|
347
|
+
logger: log,
|
|
348
|
+
});
|
|
349
|
+
if (json.on) {
|
|
350
|
+
out(JSON.stringify(result, null, 2));
|
|
351
|
+
return result.ok ? 0 : 1;
|
|
352
|
+
}
|
|
353
|
+
out("");
|
|
354
|
+
out(`Window ${result.window.sinceIso.slice(0, 10)} → ${result.window.untilIso.slice(0, 16).replace("T", " ")} UTC`);
|
|
355
|
+
const contentLabel = (c) => c.prompts || c.toolDetails ? [c.prompts ? "prompt text" : "", c.toolDetails ? "tool inputs" : ""].filter(Boolean).join(" + ") : "no text";
|
|
356
|
+
for (const s of result.sources) {
|
|
357
|
+
out("");
|
|
358
|
+
out(`${s.source === "claude-code" ? "Claude Code" : "Codex"}: ${s.filesFound} session file(s), ${s.sessionsParsed} parsed, ` +
|
|
359
|
+
`content: ${contentLabel(result.content[s.source])}`);
|
|
360
|
+
out(` ${dry.on ? "would import" : "imported"}: ${s.imported} session(s)` +
|
|
361
|
+
(s.resumed ? ` (${s.resumed} resumed)` : "") +
|
|
362
|
+
`, ${s.events} event(s), ${s.metricPoints} token point(s)`);
|
|
363
|
+
const tok = s.tokens;
|
|
364
|
+
if (tok.input + tok.output + tok.cacheRead + tok.cacheCreation > 0) {
|
|
365
|
+
out(` tokens: ${tok.input} input, ${tok.output} output, ${tok.cacheRead} cache read, ${tok.cacheCreation} cache write`);
|
|
366
|
+
}
|
|
367
|
+
for (const [reason, n] of Object.entries(s.skipped)) {
|
|
368
|
+
if (n)
|
|
369
|
+
out(` skipped ${n}: ${SKIP_LABELS[reason] ?? reason}`);
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
if (result.remaining)
|
|
373
|
+
out(`\n${result.remaining} session(s) remain — re-run the same command to continue.`);
|
|
374
|
+
for (const c of result.cleanup)
|
|
375
|
+
out(` cleanup: ${c.source} ${c.sessionId} → import.plan ${c.plans.join(", ")}`);
|
|
376
|
+
for (const w of result.warnings.slice(0, 20))
|
|
377
|
+
out(`warning: ${w}`);
|
|
378
|
+
if (result.warnings.length > 20)
|
|
379
|
+
out(`…and ${result.warnings.length - 20} more warning(s) (use --json for all)`);
|
|
380
|
+
for (const e of result.errors)
|
|
381
|
+
out(`error: ${e}`);
|
|
382
|
+
if (dry.on && result.ok)
|
|
383
|
+
out("\nNothing was sent. Re-run without --dry-run to import.");
|
|
384
|
+
return result.ok ? 0 : 1;
|
|
385
|
+
}
|
|
386
|
+
export async function run(argv) {
|
|
387
|
+
const { _, flags } = parse(argv);
|
|
388
|
+
const cmd = _[0];
|
|
389
|
+
if (flags.help || flags.h || cmd === "help") {
|
|
390
|
+
out(HELP);
|
|
391
|
+
return 0;
|
|
392
|
+
}
|
|
393
|
+
if (cmd === "kiro")
|
|
394
|
+
return runKiroCommand(_.slice(1), flags, out);
|
|
395
|
+
const picked = pickClient(flags);
|
|
396
|
+
if (!isOnboardClient(picked)) {
|
|
397
|
+
out(picked);
|
|
398
|
+
return 1;
|
|
399
|
+
}
|
|
400
|
+
const client = picked;
|
|
401
|
+
const settingsOpts = { scope: pickScope(flags), path: typeof flags.settings === "string" ? flags.settings : undefined };
|
|
402
|
+
const endpoints = {
|
|
403
|
+
apiBase: typeof flags["api-base"] === "string" ? flags["api-base"] : undefined,
|
|
404
|
+
ingestEndpoint: typeof flags.endpoint === "string" ? flags.endpoint : undefined,
|
|
405
|
+
};
|
|
406
|
+
const common = {
|
|
407
|
+
client,
|
|
408
|
+
endpoints,
|
|
409
|
+
settings: { ...settingsOpts, enrich: !!flags.enrich },
|
|
410
|
+
sendTest: flags.test !== false,
|
|
411
|
+
verify: flags.verify !== false,
|
|
412
|
+
logger: flags.json ? undefined : out,
|
|
413
|
+
};
|
|
414
|
+
if (cmd === "login" || cmd === "doctor" || cmd === "print-config" || (typeof cmd === "string" && looksLikeKey(cmd))) {
|
|
415
|
+
const rogue = unknownFlag(flags, ONBOARDING_FLAGS);
|
|
416
|
+
if (rogue) {
|
|
417
|
+
out(`Unknown option --${rogue}; refusing to continue because a misspelled scope fence could target another organization.`);
|
|
418
|
+
return 1;
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
const expectedScope = expectedScopeFromFlags(flags);
|
|
422
|
+
if (typeof expectedScope === "string") {
|
|
423
|
+
out(expectedScope);
|
|
424
|
+
return 1;
|
|
425
|
+
}
|
|
426
|
+
if (cmd === "print-config") {
|
|
427
|
+
out(CLIENTS[client].printConfig(resolveEndpoints(endpoints).ingestEndpoint, !!flags.enrich));
|
|
428
|
+
if (client === "pi")
|
|
429
|
+
out(`\n${PI_EXTENSION_HINT}`);
|
|
430
|
+
return 0;
|
|
431
|
+
}
|
|
432
|
+
if (cmd === "host-metrics") {
|
|
433
|
+
const ep = resolveEndpoints(endpoints);
|
|
434
|
+
let opts;
|
|
435
|
+
try {
|
|
436
|
+
opts = hostMetricsOptionsFromFlags(flags, {
|
|
437
|
+
ingestEndpoint: ep.ingestEndpoint,
|
|
438
|
+
paths: defaultHostMetricsPaths(),
|
|
439
|
+
instanceId: defaultInstanceId(),
|
|
440
|
+
});
|
|
441
|
+
}
|
|
442
|
+
catch (err) {
|
|
443
|
+
out(err.message);
|
|
444
|
+
return 1;
|
|
445
|
+
}
|
|
446
|
+
try {
|
|
447
|
+
if (flags.print === true) {
|
|
448
|
+
process.stdout.write(renderHostMetricsConfig(opts));
|
|
449
|
+
return 0;
|
|
450
|
+
}
|
|
451
|
+
const result = writeHostMetricsConfig(opts);
|
|
452
|
+
if (flags.json) {
|
|
453
|
+
out(JSON.stringify(result, null, 2));
|
|
454
|
+
return 0;
|
|
455
|
+
}
|
|
456
|
+
out(`${result.created ? "Wrote" : "Updated"} ${result.path}`);
|
|
457
|
+
if (result.backupPath)
|
|
458
|
+
out(`Previous config backed up to ${result.backupPath}`);
|
|
459
|
+
out(`Exporting to ${opts.endpoint} every ${opts.intervalSec ?? 60}s as ${opts.instanceId}`);
|
|
460
|
+
if (result.requiredEnv.length) {
|
|
461
|
+
out("");
|
|
462
|
+
out("Before starting the collector, set the ingest key in its environment:");
|
|
463
|
+
for (const v of result.requiredEnv)
|
|
464
|
+
out(` ${v}=cot_…`);
|
|
465
|
+
}
|
|
466
|
+
out("");
|
|
467
|
+
out("Then run: otelcol-contrib --config " + result.path);
|
|
468
|
+
out("Setup and troubleshooting: docs/otel/agentd-host-metrics.md");
|
|
469
|
+
return 0;
|
|
470
|
+
}
|
|
471
|
+
catch (err) {
|
|
472
|
+
out(`Could not generate the host-metrics config: ${err.message}`);
|
|
473
|
+
return 1;
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
if (cmd === "import") {
|
|
477
|
+
return runImportCommand(_, flags, endpoints, settingsOpts, client);
|
|
478
|
+
}
|
|
479
|
+
if (cmd === "doctor") {
|
|
480
|
+
const path = CLIENTS[client].resolveConfigPath(settingsOpts);
|
|
481
|
+
const key = CLIENTS[client].readKey(path);
|
|
482
|
+
if (!key) {
|
|
483
|
+
const flag = CLIENTS[client].cliFlag;
|
|
484
|
+
out(`No active CodeTelemetry key found in ${path} (a commented-out exporter doesn't count).`);
|
|
485
|
+
out(`Run \`codetelemetry-connect <key>${flag}\` or \`codetelemetry-connect login${flag}\` first.`);
|
|
486
|
+
return 1;
|
|
487
|
+
}
|
|
488
|
+
const ep = resolveEndpoints(endpoints);
|
|
489
|
+
if (Object.keys(expectedScope).length > 0) {
|
|
490
|
+
const preflight = await verifyScope({ key, connectivityUrl: ep.connectivityUrl, expected: expectedScope });
|
|
491
|
+
if (preflight.state !== "scope_verified") {
|
|
492
|
+
out(preflight.state === "wrong_scope"
|
|
493
|
+
? `Wrong organization/tenant/billing attribution (${(preflight.mismatches ?? []).join(", ") || "scope mismatch"}); no probe was sent.`
|
|
494
|
+
: preflight.state === "budget_exhausted"
|
|
495
|
+
? "The key has the expected scope, but its billing pool is exhausted; no probe was sent."
|
|
496
|
+
: preflight.state === "invalid_key"
|
|
497
|
+
? "The key is invalid or revoked; no probe was sent."
|
|
498
|
+
: `Could not verify the expected key scope (${preflight.state}); no probe was sent.`);
|
|
499
|
+
return 1;
|
|
500
|
+
}
|
|
501
|
+
}
|
|
502
|
+
out(`Re-testing ${mask(key)} → ${ep.ingestEndpoint}`);
|
|
503
|
+
const test = await sendTestEvent({ key, ingestEndpoint: ep.ingestEndpoint });
|
|
504
|
+
if (!test.ok) {
|
|
505
|
+
out(test.httpStatus === 401
|
|
506
|
+
? "Invalid or revoked key (ingest returned HTTP 401)."
|
|
507
|
+
: test.httpStatus === 402
|
|
508
|
+
? "The expected billing pool is exhausted (ingest returned HTTP 402)."
|
|
509
|
+
: `Test event rejected (HTTP ${test.httpStatus}).`);
|
|
510
|
+
return 1;
|
|
511
|
+
}
|
|
512
|
+
const verify = await confirmLanded({ key, connectivityUrl: ep.connectivityUrl, probeId: test.probeId, expected: expectedScope });
|
|
513
|
+
const destination = verify.scope?.teamId ? `organization ${verify.scope.teamId}` : "personal scope";
|
|
514
|
+
out(verify.landed
|
|
515
|
+
? `✓ Telemetry landed in ${verify.elapsedMs}ms; destination ${destination}; billing ${verify.scope?.billingAccountId ?? "unreported"}.`
|
|
516
|
+
: verify.state === "wrong_scope"
|
|
517
|
+
? `Wrong organization/tenant/billing attribution (${(verify.mismatches ?? []).join(", ") || "scope mismatch"}).`
|
|
518
|
+
: verify.state === "budget_exhausted"
|
|
519
|
+
? "The key is valid, but its billing pool is exhausted."
|
|
520
|
+
: verify.state === "dashboard_read_failure"
|
|
521
|
+
? "Ingest accepted the probe, but the dashboard read failed."
|
|
522
|
+
: verify.state === "invalid_key"
|
|
523
|
+
? "The key is invalid or revoked."
|
|
524
|
+
: "Probe accepted but not confirmed within the ingestion-delay window.");
|
|
525
|
+
return verify.landed ? 0 : 1;
|
|
526
|
+
}
|
|
527
|
+
if (cmd === "login") {
|
|
528
|
+
const ep = resolveEndpoints(endpoints);
|
|
529
|
+
const code = await requestDeviceCode(ep, fetch, undefined, expectedScope);
|
|
530
|
+
const expectedTeam = expectedScope.teamId || undefined;
|
|
531
|
+
const activateUrl = `${code.verificationUri}?code=${encodeURIComponent(code.userCode)}` +
|
|
532
|
+
`${expectedTeam ? `&teamId=${encodeURIComponent(expectedTeam)}` : ""}` +
|
|
533
|
+
`${code.scopeFenced ? "&scope=v1" : ""}`;
|
|
534
|
+
out("");
|
|
535
|
+
out(" To connect, approve in your browser:");
|
|
536
|
+
out(` ${activateUrl}`);
|
|
537
|
+
out(` code: ${code.userCode}`);
|
|
538
|
+
if (flags.open !== false && !flags.json && openBrowser(activateUrl)) {
|
|
539
|
+
out(" Opening your browser…");
|
|
540
|
+
}
|
|
541
|
+
out("");
|
|
542
|
+
out(" Waiting for approval…");
|
|
543
|
+
const result = await onboard({
|
|
544
|
+
...common,
|
|
545
|
+
expectedScope,
|
|
546
|
+
getKey: async () => {
|
|
547
|
+
const k = await pollForKey(ep, code.deviceCode, {
|
|
548
|
+
intervalMs: code.interval * 1000,
|
|
549
|
+
timeoutMs: code.expiresIn * 1000,
|
|
550
|
+
scopeFenced: code.scopeFenced,
|
|
551
|
+
});
|
|
552
|
+
return { apiKey: k.apiKey, ingestEndpoint: k.ingestEndpoint, scope: k.scope };
|
|
553
|
+
},
|
|
554
|
+
});
|
|
555
|
+
if (flags.json)
|
|
556
|
+
out(JSON.stringify(result, null, 2));
|
|
557
|
+
return result.ok ? 0 : 1;
|
|
558
|
+
}
|
|
559
|
+
if (looksLikeKey(cmd)) {
|
|
560
|
+
const result = await onboard({ ...common, key: cmd, expectedScope });
|
|
561
|
+
if (flags.json)
|
|
562
|
+
out(JSON.stringify(result, null, 2));
|
|
563
|
+
return result.ok ? 0 : 1;
|
|
564
|
+
}
|
|
565
|
+
out(HELP);
|
|
566
|
+
return cmd ? 1 : 0;
|
|
567
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { type SettingsScope } from "./config.ts";
|
|
2
|
+
import { type WriteResult } from "./settings.ts";
|
|
3
|
+
import { type OnboardClient } from "./assistants.ts";
|
|
4
|
+
export type ClientSummary = {
|
|
5
|
+
exists: boolean;
|
|
6
|
+
otelKeys: string[];
|
|
7
|
+
hasKey: boolean;
|
|
8
|
+
extensionInstalled?: boolean;
|
|
9
|
+
};
|
|
10
|
+
export type ClientDescriptor = {
|
|
11
|
+
id: OnboardClient;
|
|
12
|
+
label: string;
|
|
13
|
+
noun: string;
|
|
14
|
+
cliFlag: string;
|
|
15
|
+
resolveConfigPath(opts: {
|
|
16
|
+
scope?: SettingsScope;
|
|
17
|
+
path?: string;
|
|
18
|
+
}): string;
|
|
19
|
+
write(opts: {
|
|
20
|
+
key: string;
|
|
21
|
+
ingestEndpoint: string;
|
|
22
|
+
configPath: string;
|
|
23
|
+
enrich?: boolean;
|
|
24
|
+
repositoryObservation?: boolean;
|
|
25
|
+
backup?: boolean;
|
|
26
|
+
}): WriteResult;
|
|
27
|
+
readKey(configPath: string): string | null;
|
|
28
|
+
readSummary(configPath: string): ClientSummary;
|
|
29
|
+
printConfig(ingestEndpoint: string, enrich: boolean): string;
|
|
30
|
+
};
|
|
31
|
+
export declare function readClaudeKey(settingsPath: string): string | null;
|
|
32
|
+
export declare const CLIENTS: Readonly<Record<OnboardClient, ClientDescriptor>>;
|
|
33
|
+
export declare const PI_EXTENSION_HINT = "pi exports through its pi-otel extension: run `pi install npm:pi-otel` once, then start pi.";
|
|
34
|
+
export declare function resolveClient(name: unknown): ClientDescriptor;
|