8
* exits with its `[Runtime.errorExitCode]` (1 when unset), or 0 on success. Signals are the
9
* runner's (`src/run-main.ts`): a command a signal cancelled exits 128 + its number.
10
*/
11
-import { Cause, ConfigProvider, Effect, Layer, Logger, Option, Runtime } from 'effect';
11
+import { Cause, ConfigProvider, Effect, Layer, Logger, Runtime } from 'effect';
12
+import * as Path from 'effect/Path';
13
import { captureErrors, prettyPrintFromCapturedErrors } from 'effect-errors/index';
14
import { CliConfig, CliError } from 'effect/cli';
15
import { FetchHttpClient } from 'effect/http';
30
import { NodeProcess } from 'src/services/node-process';
31
import { JsPackageManagerDetector } from 'src/services/js-package-manager-detector';
32
import { ComposioCliUserConfigLive } from 'src/services/cli-user-config';
32
-import {
33
- ComposioUserContext,
34
- ComposioUserContextLive as _ComposioUserContextLive,
35
-} from 'src/services/user-context';
36
-import { formatBackendMismatch } from 'src/config/backend';
33
+import { ComposioUserContextLive as _ComposioUserContextLive } from 'src/services/user-context';
34
import { isRejectedUserApiKey, rejectedUserApiKeyHint } from 'src/effects/rejected-user-api-key';
35
import { extractMessage } from 'src/utils/api-error-extraction';
36
import { UpgradeBinary } from 'src/services/upgrade-binary';
43
import { ProjectEnvironmentDetector } from 'src/services/project-environment-detector';
44
import { CommandRunner } from 'src/services/command-runner';
45
import { StdinLive } from 'src/services/stdin';
49
-import { showPluginAcquisitionHint } from 'src/services/plugin-hint';
50
-import { showUpdateNotice } from 'src/services/update-check';
46
+import { PluginHint } from 'src/services/plugin-hint';
47
+import { UpdateNotice } from 'src/services/update-check';
48
+import { BackendMismatchWarning } from 'src/services/backend-mismatch-warning';
49
+import type { HookInvocation } from 'src/services/invocation-hook';
50
+import { CommandLifecycleTelemetry } from 'src/services/command-lifecycle-telemetry';
51
import {
52
configureCliAnalyticsReleaseVersion,
53
createCliCommandTelemetryContext,
54
getExecuteCommandToolSlug,
55
- getPrimaryLifecycleFailedEvent,
56
- getPrimaryLifecycleInvokedEvent,
57
- getPrimaryLifecycleSucceededEvent,
55
} from 'src/analytics/events';
59
-import { trackCliEventEffect } from 'src/analytics/dispatch';
56
import { getVersion } from 'src/effects/version';
57
import { LogLevelFromConfigLive } from 'src/effects/with-log-level';
58
import { toolkitFromToolSlug } from 'src/effects/toolkit-from-tool-slug';
145
Layer.mergeAll(BunFileSystem.layer, BunPath.layer, NodeOs.Default)
146
) satisfies RequiredLayer;
147
148
+export const BackendMismatchWarningLive = Layer.provide(
149
+ BackendMismatchWarning.Default,
150
+ Layer.mergeAll(ComposioUserContextLive, TerminalUILive)
151
+) satisfies RequiredLayer;
152
+
153
+// `Path.layer`, not `BunPath.layer`: the update notice and the plugin hint have always resolved
154
+// their state files with Effect's own path implementation.
155
+export const UpdateNoticeLive = Layer.provide(
156
+ UpdateNotice.Default,
157
+ Layer.mergeAll(TerminalUILive, BunFileSystem.layer, Path.layer, NodeOs.Default)
158
+) satisfies RequiredLayer;
159
+
160
+export const PluginHintLive = Layer.provide(
161
+ PluginHint.Default,
162
+ Layer.mergeAll(TerminalUILive, BunFileSystem.layer, Path.layer, NodeOs.Default)
163
+) satisfies RequiredLayer;
164
+
165
+export const CommandLifecycleTelemetryLive = Layer.provide(
166
+ CommandLifecycleTelemetry.Default,
167
+ Layer.mergeAll(BunFileSystem.layer, BunPath.layer, NodeOs.Default, TerminalUILive)
168
+) satisfies RequiredLayer;
169
+
170
const layers = Layer.mergeAll(
171
CliConfigLive.pipe(Layer.provide(LogLevelFromConfigLive)),
172
NodeOs.Default,
192
FetchHttpClient.layer,
193
StdinLive,
194
TerminalUILive,
195
+ BackendMismatchWarningLive,
196
+ UpdateNoticeLive,
197
+ PluginHintLive,
198
+ CommandLifecycleTelemetryLive,
199
Layer.merge(
200
Logger.layer([withoutClientNotices(Logger.consolePretty())]),
201
Layer.succeed(Logger.LogToStderr, true)
281
commandTelemetryContext.commandPath === 'run' && commandTelemetryContext.runId
282
? { runId: commandTelemetryContext.runId }
283
: {};
262
- return yield* trackCliEventEffect(
263
- getPrimaryLifecycleInvokedEvent(commandTelemetryContext)
264
- ).pipe(
284
+ const telemetry = yield* CommandLifecycleTelemetry;
285
+ return yield* telemetry.invoked(commandTelemetryContext).pipe(
286
Effect.andThen(runWithArgs(argv, bootstrap)),
287
Effect.scoped,
288
Effect.mapError(error =>
289
CliError.isCliError(error) ? error : mapOnlyComposioOverrideError({ error })
290
),
270
- Effect.tap(() =>
271
- trackCliEventEffect(getPrimaryLifecycleSucceededEvent(commandTelemetryContext))
272
- ),
273
- Effect.tapCause(cause =>
274
- trackCliEventEffect(
275
- getPrimaryLifecycleFailedEvent(commandTelemetryContext, Cause.squash(cause))
276
- )
277
- )
291
+ Effect.tap(() => telemetry.succeeded(commandTelemetryContext)),
292
+ Effect.tapCause(cause => telemetry.failed(commandTelemetryContext, cause))
293
);
294
});
295
301
readonly argv: ReadonlyArray<string>;
302
};
303
289
-/** Writes `message` to stderr: as a decorated warning on a terminal, as plain text otherwise. */
290
-const warn = (message: string) =>
304
+/** Runs the hooks every invocation gets before its command, in order, on stderr. */
305
+const runInvocationHooks = (invocation: HookInvocation) =>
306
Effect.gen(function* () {
292
- const ui = yield* TerminalUI;
293
- if ((yield* ui.capabilities).canDecorate) {
294
- yield* ui.log.warn(message);
295
- } else {
296
- yield* ui.error(`Warning: ${message}`);
297
- }
307
+ const backendMismatchWarning = yield* BackendMismatchWarning;
308
+ const updateNotice = yield* UpdateNotice;
309
+ const pluginHint = yield* PluginHint;
310
+ yield* backendMismatchWarning.run(invocation);
311
+ yield* updateNotice.run(invocation);
312
+ yield* pluginHint.run(invocation);
313
});
314
300
-/** Warns, before any request, when an env var sends the stored key to another backend. */
301
-const warnOnBackendMismatch = Effect.gen(function* () {
302
- const ctx = yield* ComposioUserContext;
303
- yield* Option.match(ctx.backendMismatch, {
304
- onNone: () => Effect.void,
305
- onSome: mismatch => warn(formatBackendMismatch(mismatch)),
306
- });
307
-});
308
-
315
const cliProgram = (argv: ReadonlyArray<string>) =>
310
- warnOnBackendMismatch.pipe(
311
- Effect.andThen(showUpdateNotice(argv)),
312
- Effect.andThen(showPluginAcquisitionHint(argv)),
316
+ runInvocationHooks({ argv }).pipe(
317
Effect.andThen(runWithTelemetry(argv)),
318
// A usage mistake, a missing setup or login, or a failure a remote service reported clearly
319
// (see `ExpectedFailure`) is not a broken invariant: print the message and exit 1 instead of