@tiny-fish/cli 0.19.1-next.175 → 0.19.1-next.177
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.
|
@@ -1,12 +1,9 @@
|
|
|
1
1
|
import { Command } from "commander";
|
|
2
|
+
import { type AgentClient } from "../lib/connect-runtime.js";
|
|
2
3
|
export declare const DEFAULT_MCP_URL = "https://agent.tinyfish.ai/mcp";
|
|
3
4
|
export declare const UPGRADE_HINT = "Run `tinyfish upgrade` any time to update the CLI and skill.";
|
|
4
5
|
export declare const DEFAULT_ONBOARDING_PROMPT: string;
|
|
5
6
|
export declare const OPENCLAW_ONBOARDING_PROMPT: string;
|
|
6
|
-
export type AgentClient = "claude-code" | "codex" | "cursor" | "hermes" | "openclaw" | "opencode";
|
|
7
|
-
/** Ctrl+C/SIGTERM killed a setup child — abandonment, not error. */
|
|
8
|
-
export declare class ConnectInterruptedError extends Error {
|
|
9
|
-
}
|
|
10
7
|
interface NativeConnectOptions {
|
|
11
8
|
apiKey?: string;
|
|
12
9
|
mcpUrl: string;
|
package/dist/commands/connect.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { randomUUID } from "node:crypto";
|
|
2
1
|
import * as path from "node:path";
|
|
3
2
|
import spawn from "cross-spawn";
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
3
|
+
import { CONNECT_SOURCE, loadConfig, persistApiKeyToEnvironment, saveConnectContext, validateKeyFormat, validatedApiKey, writeConfig, } from "../lib/auth.js";
|
|
4
|
+
import { commandNotFound, ConnectInterruptedError, createConnectTelemetry, requireCommandSupport, runGuarded, settle, throwIfInterrupted, NON_INTERACTIVE_TIMEOUT_MS, } from "../lib/connect-runtime.js";
|
|
5
|
+
import { TINYFISH_CLI_PACKAGE } from "../lib/constants.js";
|
|
6
6
|
import { cursorInstallDeeplink, cursorMcpPath, writeCursorMcpConfig, } from "../lib/cursor-config.js";
|
|
7
7
|
import { runConnectAll } from "../lib/connect-all.js";
|
|
8
8
|
import { detectHumanInitiated } from "../lib/harness.js";
|
|
@@ -10,11 +10,8 @@ import { emitNotice } from "../lib/notice.js";
|
|
|
10
10
|
import { errLine, sanitizeLine } from "../lib/output.js";
|
|
11
11
|
import { codexOauthCompleted } from "../lib/registration-detect.js";
|
|
12
12
|
import { z } from "zod";
|
|
13
|
-
import { installSignalGuard } from "../lib/signals.js";
|
|
14
|
-
import { postConnectEvent, telemetryDisabled } from "../lib/setup-telemetry.js";
|
|
15
13
|
import { verifyMcpAuth } from "../lib/verify.js";
|
|
16
14
|
export const DEFAULT_MCP_URL = "https://agent.tinyfish.ai/mcp";
|
|
17
|
-
const NON_INTERACTIVE_TIMEOUT_MS = 10_000;
|
|
18
15
|
const SKILL_INSTALL_TIMEOUT_MS = 120_000;
|
|
19
16
|
const HERMES_SEED_TIMEOUT_MS = 120_000;
|
|
20
17
|
// An old install is only one reason a flag can go unseen, so no message asserts the cause.
|
|
@@ -74,72 +71,6 @@ export const OPENCLAW_ONBOARDING_PROMPT = "I just installed the TinyFish skill.
|
|
|
74
71
|
"want to search and wait for my reply before using TinyFish Search. Show me the results, ask " +
|
|
75
72
|
"which result I want to read, and wait before using TinyFish Fetch. Then ask what browser task " +
|
|
76
73
|
"I want to complete and wait before using TinyFish Agent. Never skip ahead or choose for me.";
|
|
77
|
-
/** Ctrl+C/SIGTERM killed a setup child — abandonment, not error. */
|
|
78
|
-
export class ConnectInterruptedError extends Error {
|
|
79
|
-
}
|
|
80
|
-
class PrerequisiteError extends Error {
|
|
81
|
-
failureReason;
|
|
82
|
-
harnessVersion;
|
|
83
|
-
constructor(message, failureReason, opts) {
|
|
84
|
-
super(message, { cause: opts?.cause });
|
|
85
|
-
this.failureReason = failureReason;
|
|
86
|
-
this.harnessVersion = opts?.harnessVersion;
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
function throwIfInterrupted(result) {
|
|
90
|
-
// spawn.sync kills a timed-out child with SIGTERM, so signal alone would misread a slow
|
|
91
|
-
// network as the user walking away. A timeout is a failure and must report as one.
|
|
92
|
-
if (result.error?.code === "ETIMEDOUT")
|
|
93
|
-
return;
|
|
94
|
-
if (result.signal === "SIGINT" || result.signal === "SIGTERM") {
|
|
95
|
-
throw new ConnectInterruptedError("Setup interrupted");
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
/** Terminal events exactly once, whichever of flow or signal handler wins. */
|
|
99
|
-
function settle(state, telemetry, stage, detail) {
|
|
100
|
-
if (state.settled)
|
|
101
|
-
return;
|
|
102
|
-
state.settled = true;
|
|
103
|
-
telemetry.track(stage, detail);
|
|
104
|
-
}
|
|
105
|
-
// Raw-mode children still surface via throwIfInterrupted; cooked-mode stages
|
|
106
|
-
// (npm/npx/OAuth waits) land here.
|
|
107
|
-
function installConnectSignalGuard(state, telemetry) {
|
|
108
|
-
return installSignalGuard(async () => {
|
|
109
|
-
if (!state.settled) {
|
|
110
|
-
settle(state, telemetry, "aborted", { failedStage: state.stage });
|
|
111
|
-
errLine("Setup interrupted — run the command again to finish.");
|
|
112
|
-
}
|
|
113
|
-
// Settled spans the whole post-install phase; the `completed` POST may still be in flight.
|
|
114
|
-
await telemetry.flush();
|
|
115
|
-
});
|
|
116
|
-
}
|
|
117
|
-
/** Shared terminal handling: interrupted → aborted, else failed. */
|
|
118
|
-
async function runGuarded(state, telemetry, body) {
|
|
119
|
-
const uninstallSignalGuard = installConnectSignalGuard(state, telemetry);
|
|
120
|
-
try {
|
|
121
|
-
await body();
|
|
122
|
-
}
|
|
123
|
-
catch (error) {
|
|
124
|
-
if (error instanceof ConnectInterruptedError) {
|
|
125
|
-
settle(state, telemetry, "aborted", { failedStage: state.stage });
|
|
126
|
-
errLine("Setup interrupted — run the command again to finish.");
|
|
127
|
-
process.exitCode = 130;
|
|
128
|
-
return;
|
|
129
|
-
}
|
|
130
|
-
settle(state, telemetry, "failed", {
|
|
131
|
-
failedStage: state.stage,
|
|
132
|
-
...(error instanceof PrerequisiteError
|
|
133
|
-
? { failureReason: error.failureReason, harnessVersion: error.harnessVersion }
|
|
134
|
-
: {}),
|
|
135
|
-
});
|
|
136
|
-
throw error;
|
|
137
|
-
}
|
|
138
|
-
finally {
|
|
139
|
-
uninstallSignalGuard();
|
|
140
|
-
await telemetry.flush();
|
|
141
|
-
}
|
|
142
|
-
}
|
|
143
74
|
const CLAUDE_CODE = {
|
|
144
75
|
command: "claude",
|
|
145
76
|
connectClient: "claude-code",
|
|
@@ -291,109 +222,6 @@ const OPENCLAW = {
|
|
|
291
222
|
unavailableMessage: OPENCLAW_SKILL_UNAVAILABLE_MESSAGE,
|
|
292
223
|
},
|
|
293
224
|
};
|
|
294
|
-
/**
|
|
295
|
-
* One page-minted id seeds one install attempt, so `tinyfish onboard` connecting three agents
|
|
296
|
-
* does not report three installs under it. Later connects in the run fall back to a random id.
|
|
297
|
-
*/
|
|
298
|
-
function takeSeedAttemptId() {
|
|
299
|
-
const fromEnv = process.env[CONNECT_ATTEMPT_ENV];
|
|
300
|
-
// Both sources are read-and-clear, and the delete lands before any subprocess inherits it.
|
|
301
|
-
delete process.env[CONNECT_ATTEMPT_ENV];
|
|
302
|
-
const parked = takePendingConnectAttempt();
|
|
303
|
-
return fromEnv && isAttemptId(fromEnv) ? fromEnv : parked;
|
|
304
|
-
}
|
|
305
|
-
function createConnectTelemetry(mcpUrl, client, opts) {
|
|
306
|
-
// Consumed even when --url already supplied an id: leaving it behind would let a stale
|
|
307
|
-
// seed reach the agent this connect launches, or the next connect on this machine.
|
|
308
|
-
const seeded = takeSeedAttemptId();
|
|
309
|
-
// A setup-page id (via --url, the env var, or one parked by login) joins "copied the
|
|
310
|
-
// command" to this install attempt.
|
|
311
|
-
const attemptId = opts?.attemptId ?? seeded ?? randomUUID();
|
|
312
|
-
const endpoint = new URL("/api/cli/connect-event", mcpUrl).toString();
|
|
313
|
-
const pending = [];
|
|
314
|
-
async function deliver(body) {
|
|
315
|
-
// Ahead of the key read; postConnectEvent's own opt-out check is too late to skip it.
|
|
316
|
-
if (telemetryDisabled())
|
|
317
|
-
return;
|
|
318
|
-
// Resolved per event: `tinyfish auth login` writes the config from a subprocess mid-connect.
|
|
319
|
-
await postConnectEvent({
|
|
320
|
-
endpoint,
|
|
321
|
-
body,
|
|
322
|
-
attempts: 2,
|
|
323
|
-
apiKey: resolvedApiKey(opts?.apiKey),
|
|
324
|
-
label: "connect",
|
|
325
|
-
});
|
|
326
|
-
}
|
|
327
|
-
return {
|
|
328
|
-
attemptId,
|
|
329
|
-
// Fire-and-forget; awaiting each event stalls setup when telemetry down.
|
|
330
|
-
track(stage, detail) {
|
|
331
|
-
pending.push(deliver(JSON.stringify({
|
|
332
|
-
attempt_id: attemptId,
|
|
333
|
-
client,
|
|
334
|
-
stage,
|
|
335
|
-
failed_stage: detail?.failedStage,
|
|
336
|
-
phase: detail?.phase,
|
|
337
|
-
failure_reason: detail?.failureReason,
|
|
338
|
-
harness_version: detail?.harnessVersion,
|
|
339
|
-
runtime_platform: process.platform,
|
|
340
|
-
node_version: process.version,
|
|
341
|
-
cli_version: CLI_VERSION,
|
|
342
|
-
// Same signal the usage events carry, so TTY and headless connects can be split.
|
|
343
|
-
is_human_initiated: detectHumanInitiated(),
|
|
344
|
-
})));
|
|
345
|
-
},
|
|
346
|
-
// Awaited in finally so in-flight events land before exit.
|
|
347
|
-
async flush() {
|
|
348
|
-
await Promise.all(pending);
|
|
349
|
-
},
|
|
350
|
-
};
|
|
351
|
-
}
|
|
352
|
-
function requireCommandSupport(client) {
|
|
353
|
-
const result = spawn.sync(client.command, client.supportCheck.args, {
|
|
354
|
-
encoding: "utf8",
|
|
355
|
-
// Agent harnesses export FORCE_COLOR, which makes clients colourise even a piped --help.
|
|
356
|
-
env: { ...process.env, FORCE_COLOR: "0", NO_COLOR: "1" },
|
|
357
|
-
timeout: NON_INTERACTIVE_TIMEOUT_MS,
|
|
358
|
-
});
|
|
359
|
-
if (commandNotFound(result.error)) {
|
|
360
|
-
throw new PrerequisiteError(`${client.displayName} is not installed or not available on PATH.`, "harness_not_installed", { cause: result.error });
|
|
361
|
-
}
|
|
362
|
-
if (result.error || result.status !== 0) {
|
|
363
|
-
throwIfInterrupted(result);
|
|
364
|
-
throw new PrerequisiteError(client.supportCheck.unavailableMessage, "harness_command_unsupported", { cause: result.error });
|
|
365
|
-
}
|
|
366
|
-
// Belt and braces with the colour env: a client that ignores NO_COLOR still has to match.
|
|
367
|
-
const output = sanitizeLine(`${result.stdout ?? ""}\n${result.stderr ?? ""}`);
|
|
368
|
-
const listed = (patterns) => patterns.every((pattern) => pattern.test(output));
|
|
369
|
-
const essential = listed(client.supportCheck.patterns);
|
|
370
|
-
const optionalSupported = listed(client.supportCheck.optionalPatterns ?? []);
|
|
371
|
-
if ((!essential || !optionalSupported) && process.env["TINYFISH_DEBUG"]) {
|
|
372
|
-
errLine(`${client.command} ${client.supportCheck.args.join(" ")} printed:\n${output.trim()}`);
|
|
373
|
-
}
|
|
374
|
-
if (!essential) {
|
|
375
|
-
throw new PrerequisiteError(client.supportCheck.unavailableMessage, "harness_too_old", {
|
|
376
|
-
harnessVersion: probeHarnessVersion(client.command),
|
|
377
|
-
});
|
|
378
|
-
}
|
|
379
|
-
return { optionalSupported };
|
|
380
|
-
}
|
|
381
|
-
/** Best-effort; the server rejects non-printable characters and >32 chars. */
|
|
382
|
-
function probeHarnessVersion(command) {
|
|
383
|
-
const result = spawn.sync(command, ["--version"], {
|
|
384
|
-
encoding: "utf8",
|
|
385
|
-
timeout: NON_INTERACTIVE_TIMEOUT_MS,
|
|
386
|
-
});
|
|
387
|
-
if (result.error || result.status !== 0)
|
|
388
|
-
return undefined;
|
|
389
|
-
const version = sanitizeLine(result.stdout ?? "")
|
|
390
|
-
.replace(/[^\x20-\x7E]/g, "")
|
|
391
|
-
.trim();
|
|
392
|
-
return version ? version.slice(0, 32) : undefined;
|
|
393
|
-
}
|
|
394
|
-
function commandNotFound(error) {
|
|
395
|
-
return error?.code === "ENOENT";
|
|
396
|
-
}
|
|
397
225
|
export function installTinyFishCli() {
|
|
398
226
|
errLine("Installing the TinyFish CLI...");
|
|
399
227
|
const result = spawn.sync("npm", ["install", "--global", TINYFISH_CLI_INSTALL_SPEC], {
|
package/dist/commands/upgrade.js
CHANGED
|
@@ -5,7 +5,8 @@ import { suppressNotice } from "../lib/notice.js";
|
|
|
5
5
|
import { errLine } from "../lib/output.js";
|
|
6
6
|
import { sendUpgradeCompleted, telemetryDisabled, } from "../lib/setup-telemetry.js";
|
|
7
7
|
import { installSignalGuard, SIGNAL_EXIT_CODES } from "../lib/signals.js";
|
|
8
|
-
import { ConnectInterruptedError
|
|
8
|
+
import { ConnectInterruptedError } from "../lib/connect-runtime.js";
|
|
9
|
+
import { installTinyFishCli, updateWebSkill } from "./connect.js";
|
|
9
10
|
const VERSION_READ_TIMEOUT_MS = 2_000;
|
|
10
11
|
const INTERRUPTED_MESSAGE = "Upgrade interrupted — run `tinyfish upgrade` again to finish.";
|
|
11
12
|
function attempt(label, run) {
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
export declare const NON_INTERACTIVE_TIMEOUT_MS = 10000;
|
|
2
|
+
export type AgentClient = "claude-code" | "codex" | "cursor" | "hermes" | "openclaw" | "opencode";
|
|
3
|
+
type ConnectStage = "started" | "checkpoint" | "completed" | "failed" | "aborted" | "post_install_failed";
|
|
4
|
+
type ConnectFailureStage = "prerequisite_check" | "registration_cleanup" | "registration" | "registration_or_authentication" | "client_oauth" | "cli_install" | "skill_install" | "authentication" | "walkthrough_launch";
|
|
5
|
+
type ConnectFailureReason = "harness_not_installed" | "harness_command_unsupported" | "harness_too_old";
|
|
6
|
+
type ConnectCheckpoint = "prerequisite_ok" | "registered" | "oauth_done" | "cli_installed" | "skill_installed" | "authenticated";
|
|
7
|
+
/** Ctrl+C/SIGTERM killed a setup child — abandonment, not error. */
|
|
8
|
+
export declare class ConnectInterruptedError extends Error {
|
|
9
|
+
}
|
|
10
|
+
export declare function throwIfInterrupted(result: {
|
|
11
|
+
signal?: string | null;
|
|
12
|
+
error?: Error;
|
|
13
|
+
}): void;
|
|
14
|
+
export declare function commandNotFound(error: unknown): boolean;
|
|
15
|
+
export interface ConnectRunState {
|
|
16
|
+
stage: ConnectFailureStage;
|
|
17
|
+
settled: boolean;
|
|
18
|
+
}
|
|
19
|
+
interface ConnectStageDetail {
|
|
20
|
+
failedStage?: ConnectFailureStage;
|
|
21
|
+
phase?: ConnectCheckpoint;
|
|
22
|
+
failureReason?: ConnectFailureReason;
|
|
23
|
+
harnessVersion?: string;
|
|
24
|
+
}
|
|
25
|
+
export interface ConnectTelemetry {
|
|
26
|
+
attemptId: string;
|
|
27
|
+
track(stage: ConnectStage, detail?: ConnectStageDetail): void;
|
|
28
|
+
flush(): Promise<void>;
|
|
29
|
+
}
|
|
30
|
+
/** Terminal events exactly once, whichever of flow or signal handler wins. */
|
|
31
|
+
export declare function settle(state: ConnectRunState, telemetry: ConnectTelemetry, stage: ConnectStage, detail?: ConnectStageDetail): void;
|
|
32
|
+
/** Shared terminal handling: interrupted → aborted, else failed. */
|
|
33
|
+
export declare function runGuarded(state: ConnectRunState, telemetry: ConnectTelemetry, body: () => void | Promise<void>): Promise<void>;
|
|
34
|
+
export interface SupportedCommand {
|
|
35
|
+
command: string;
|
|
36
|
+
displayName: string;
|
|
37
|
+
supportCheck: {
|
|
38
|
+
args: string[];
|
|
39
|
+
/** Missing → nothing can work; setup fails. */
|
|
40
|
+
patterns: RegExp[];
|
|
41
|
+
/** Missing → degrade to key or deferred auth, never refuse. */
|
|
42
|
+
optionalPatterns?: RegExp[];
|
|
43
|
+
unavailableMessage: string;
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
export declare function requireCommandSupport(client: SupportedCommand): {
|
|
47
|
+
optionalSupported: boolean;
|
|
48
|
+
};
|
|
49
|
+
export declare function createConnectTelemetry(mcpUrl: string, client: AgentClient, opts?: {
|
|
50
|
+
apiKey?: string;
|
|
51
|
+
attemptId?: string;
|
|
52
|
+
}): ConnectTelemetry;
|
|
53
|
+
export {};
|
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
import spawn from "cross-spawn";
|
|
3
|
+
import { CONNECT_ATTEMPT_ENV, isAttemptId, resolvedApiKey, takePendingConnectAttempt, } from "./auth.js";
|
|
4
|
+
import { CLI_VERSION } from "./constants.js";
|
|
5
|
+
import { detectHumanInitiated } from "./harness.js";
|
|
6
|
+
import { errLine, sanitizeLine } from "./output.js";
|
|
7
|
+
import { postConnectEvent, telemetryDisabled } from "./setup-telemetry.js";
|
|
8
|
+
import { installSignalGuard } from "./signals.js";
|
|
9
|
+
export const NON_INTERACTIVE_TIMEOUT_MS = 10_000;
|
|
10
|
+
/** Ctrl+C/SIGTERM killed a setup child — abandonment, not error. */
|
|
11
|
+
export class ConnectInterruptedError extends Error {
|
|
12
|
+
}
|
|
13
|
+
class PrerequisiteError extends Error {
|
|
14
|
+
failureReason;
|
|
15
|
+
harnessVersion;
|
|
16
|
+
constructor(message, failureReason, opts) {
|
|
17
|
+
super(message, { cause: opts?.cause });
|
|
18
|
+
this.failureReason = failureReason;
|
|
19
|
+
this.harnessVersion = opts?.harnessVersion;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
export function throwIfInterrupted(result) {
|
|
23
|
+
// spawn.sync kills a timed-out child with SIGTERM, so signal alone would misread a slow
|
|
24
|
+
// network as the user walking away. A timeout is a failure and must report as one.
|
|
25
|
+
if (result.error?.code === "ETIMEDOUT")
|
|
26
|
+
return;
|
|
27
|
+
if (result.signal === "SIGINT" || result.signal === "SIGTERM") {
|
|
28
|
+
throw new ConnectInterruptedError("Setup interrupted");
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
export function commandNotFound(error) {
|
|
32
|
+
return error?.code === "ENOENT";
|
|
33
|
+
}
|
|
34
|
+
/** Terminal events exactly once, whichever of flow or signal handler wins. */
|
|
35
|
+
export function settle(state, telemetry, stage, detail) {
|
|
36
|
+
if (state.settled)
|
|
37
|
+
return;
|
|
38
|
+
state.settled = true;
|
|
39
|
+
telemetry.track(stage, detail);
|
|
40
|
+
}
|
|
41
|
+
// Raw-mode children still surface via throwIfInterrupted; cooked-mode stages
|
|
42
|
+
// (npm/npx/OAuth waits) land here.
|
|
43
|
+
function installConnectSignalGuard(state, telemetry) {
|
|
44
|
+
return installSignalGuard(async () => {
|
|
45
|
+
if (!state.settled) {
|
|
46
|
+
settle(state, telemetry, "aborted", { failedStage: state.stage });
|
|
47
|
+
errLine("Setup interrupted — run the command again to finish.");
|
|
48
|
+
}
|
|
49
|
+
// Settled spans the whole post-install phase; the `completed` POST may still be in flight.
|
|
50
|
+
await telemetry.flush();
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
/** Shared terminal handling: interrupted → aborted, else failed. */
|
|
54
|
+
export async function runGuarded(state, telemetry, body) {
|
|
55
|
+
const uninstallSignalGuard = installConnectSignalGuard(state, telemetry);
|
|
56
|
+
try {
|
|
57
|
+
await body();
|
|
58
|
+
}
|
|
59
|
+
catch (error) {
|
|
60
|
+
if (error instanceof ConnectInterruptedError) {
|
|
61
|
+
settle(state, telemetry, "aborted", { failedStage: state.stage });
|
|
62
|
+
errLine("Setup interrupted — run the command again to finish.");
|
|
63
|
+
process.exitCode = 130;
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
settle(state, telemetry, "failed", {
|
|
67
|
+
failedStage: state.stage,
|
|
68
|
+
...(error instanceof PrerequisiteError
|
|
69
|
+
? { failureReason: error.failureReason, harnessVersion: error.harnessVersion }
|
|
70
|
+
: {}),
|
|
71
|
+
});
|
|
72
|
+
throw error;
|
|
73
|
+
}
|
|
74
|
+
finally {
|
|
75
|
+
uninstallSignalGuard();
|
|
76
|
+
await telemetry.flush();
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
export function requireCommandSupport(client) {
|
|
80
|
+
const result = spawn.sync(client.command, client.supportCheck.args, {
|
|
81
|
+
encoding: "utf8",
|
|
82
|
+
// Agent harnesses export FORCE_COLOR, which makes clients colourise even a piped --help.
|
|
83
|
+
env: { ...process.env, FORCE_COLOR: "0", NO_COLOR: "1" },
|
|
84
|
+
timeout: NON_INTERACTIVE_TIMEOUT_MS,
|
|
85
|
+
});
|
|
86
|
+
if (commandNotFound(result.error)) {
|
|
87
|
+
throw new PrerequisiteError(`${client.displayName} is not installed or not available on PATH.`, "harness_not_installed", { cause: result.error });
|
|
88
|
+
}
|
|
89
|
+
if (result.error || result.status !== 0) {
|
|
90
|
+
throwIfInterrupted(result);
|
|
91
|
+
throw new PrerequisiteError(client.supportCheck.unavailableMessage, "harness_command_unsupported", { cause: result.error });
|
|
92
|
+
}
|
|
93
|
+
// Belt and braces with the colour env: a client that ignores NO_COLOR still has to match.
|
|
94
|
+
const output = sanitizeLine(`${result.stdout ?? ""}\n${result.stderr ?? ""}`);
|
|
95
|
+
const listed = (patterns) => patterns.every((pattern) => pattern.test(output));
|
|
96
|
+
const essential = listed(client.supportCheck.patterns);
|
|
97
|
+
const optionalSupported = listed(client.supportCheck.optionalPatterns ?? []);
|
|
98
|
+
if ((!essential || !optionalSupported) && process.env["TINYFISH_DEBUG"]) {
|
|
99
|
+
errLine(`${client.command} ${client.supportCheck.args.join(" ")} printed:\n${output.trim()}`);
|
|
100
|
+
}
|
|
101
|
+
if (!essential) {
|
|
102
|
+
throw new PrerequisiteError(client.supportCheck.unavailableMessage, "harness_too_old", {
|
|
103
|
+
harnessVersion: probeHarnessVersion(client.command),
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
return { optionalSupported };
|
|
107
|
+
}
|
|
108
|
+
/** Best-effort; the server rejects non-printable characters and >32 chars. */
|
|
109
|
+
function probeHarnessVersion(command) {
|
|
110
|
+
const result = spawn.sync(command, ["--version"], {
|
|
111
|
+
encoding: "utf8",
|
|
112
|
+
timeout: NON_INTERACTIVE_TIMEOUT_MS,
|
|
113
|
+
});
|
|
114
|
+
if (result.error || result.status !== 0)
|
|
115
|
+
return undefined;
|
|
116
|
+
const version = sanitizeLine(result.stdout ?? "")
|
|
117
|
+
.replace(/[^\x20-\x7E]/g, "")
|
|
118
|
+
.trim();
|
|
119
|
+
return version ? version.slice(0, 32) : undefined;
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* One page-minted id seeds one install attempt, so `tinyfish onboard` connecting three agents
|
|
123
|
+
* does not report three installs under it. Later connects in the run fall back to a random id.
|
|
124
|
+
*/
|
|
125
|
+
function takeSeedAttemptId() {
|
|
126
|
+
const fromEnv = process.env[CONNECT_ATTEMPT_ENV];
|
|
127
|
+
// Both sources are read-and-clear, and the delete lands before any subprocess inherits it.
|
|
128
|
+
delete process.env[CONNECT_ATTEMPT_ENV];
|
|
129
|
+
const parked = takePendingConnectAttempt();
|
|
130
|
+
return fromEnv && isAttemptId(fromEnv) ? fromEnv : parked;
|
|
131
|
+
}
|
|
132
|
+
export function createConnectTelemetry(mcpUrl, client, opts) {
|
|
133
|
+
// Consumed even when --url already supplied an id: leaving it behind would let a stale
|
|
134
|
+
// seed reach the agent this connect launches, or the next connect on this machine.
|
|
135
|
+
const seeded = takeSeedAttemptId();
|
|
136
|
+
// A setup-page id (via --url, the env var, or one parked by login) joins "copied the
|
|
137
|
+
// command" to this install attempt.
|
|
138
|
+
const attemptId = opts?.attemptId ?? seeded ?? randomUUID();
|
|
139
|
+
const endpoint = new URL("/api/cli/connect-event", mcpUrl).toString();
|
|
140
|
+
const pending = [];
|
|
141
|
+
async function deliver(body) {
|
|
142
|
+
// Ahead of the key read; postConnectEvent's own opt-out check is too late to skip it.
|
|
143
|
+
if (telemetryDisabled())
|
|
144
|
+
return;
|
|
145
|
+
// Resolved per event: `tinyfish auth login` writes the config from a subprocess mid-connect.
|
|
146
|
+
await postConnectEvent({
|
|
147
|
+
endpoint,
|
|
148
|
+
body,
|
|
149
|
+
attempts: 2,
|
|
150
|
+
apiKey: resolvedApiKey(opts?.apiKey),
|
|
151
|
+
label: "connect",
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
return {
|
|
155
|
+
attemptId,
|
|
156
|
+
// Fire-and-forget; awaiting each event stalls setup when telemetry down.
|
|
157
|
+
track(stage, detail) {
|
|
158
|
+
// Absorbed here, not in flush: flush runs in runGuarded's finally, so a rejection would
|
|
159
|
+
// replace the error the flow is already reporting.
|
|
160
|
+
pending.push(deliver(JSON.stringify({
|
|
161
|
+
attempt_id: attemptId,
|
|
162
|
+
client,
|
|
163
|
+
stage,
|
|
164
|
+
failed_stage: detail?.failedStage,
|
|
165
|
+
phase: detail?.phase,
|
|
166
|
+
failure_reason: detail?.failureReason,
|
|
167
|
+
harness_version: detail?.harnessVersion,
|
|
168
|
+
runtime_platform: process.platform,
|
|
169
|
+
node_version: process.version,
|
|
170
|
+
cli_version: CLI_VERSION,
|
|
171
|
+
// Same signal the usage events carry, so TTY and headless connects can be split.
|
|
172
|
+
is_human_initiated: detectHumanInitiated(),
|
|
173
|
+
})).catch((error) => {
|
|
174
|
+
// postConnectEvent is total, so this only fires if something inside deliver starts
|
|
175
|
+
// throwing. Same channel postConnectEvent uses for its own failures.
|
|
176
|
+
if (!process.env["TINYFISH_DEBUG"])
|
|
177
|
+
return;
|
|
178
|
+
errLine(`connect telemetry failed: ${error instanceof Error ? error.message : String(error)}`);
|
|
179
|
+
}));
|
|
180
|
+
},
|
|
181
|
+
// Awaited in finally so in-flight events land before exit.
|
|
182
|
+
async flush() {
|
|
183
|
+
await Promise.all(pending);
|
|
184
|
+
},
|
|
185
|
+
};
|
|
186
|
+
}
|