Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
| 1 | import { type Cause, Context, Effect, Layer, Ref } from 'effect'; |
| 2 | import type { CliCommandTelemetryContext } from 'src/analytics/types'; |
| 3 | import { BackendMismatchWarning } from 'src/services/backend-mismatch-warning'; |
| 4 | import { |
| 5 | CommandLifecycleTelemetry, |
| 6 | type PluginHintShown, |
| 7 | } from 'src/services/command-lifecycle-telemetry'; |
| 8 | import type { HookInvocation } from 'src/services/invocation-hook'; |
| 9 | import { PluginHint } from 'src/services/plugin-hint'; |
| 10 | import { BackgroundUpdateCheck, UpdateNotice } from 'src/services/update-check'; |
| 11 | |
| 12 | /** One call to an invocation hook or to the lifecycle telemetry sink, in call order. */ |
| 13 | export type RecordedInvocationCall = |
| 14 | | { |
| 15 | readonly service: |
| 16 | 'BackendMismatchWarning' | 'UpdateNotice' | 'PluginHint' | 'BackgroundUpdateCheck'; |
| 17 | readonly invocation: HookInvocation; |
| 18 | } |
| 19 | | { |
| 20 | readonly service: 'CommandLifecycleTelemetry'; |
| 21 | readonly event: 'invoked' | 'succeeded'; |
| 22 | readonly context: CliCommandTelemetryContext; |
| 23 | } |
| 24 | | { |
| 25 | readonly service: 'CommandLifecycleTelemetry'; |
| 26 | readonly event: 'failed'; |
| 27 | readonly context: CliCommandTelemetryContext; |
| 28 | readonly cause: Cause.Cause<unknown>; |
| 29 | } |
| 30 | | { |
| 31 | readonly service: 'CommandLifecycleTelemetry'; |
| 32 | readonly event: 'pluginHintShown'; |
| 33 | readonly shown: PluginHintShown; |
| 34 | }; |
| 35 | |
| 36 | /** Reads what the recording doubles of `InvocationHooksTest` were called with. */ |
| 37 | export class InvocationRecorder extends Context.Service< |
| 38 | InvocationRecorder, |
| 39 | { |
| 40 | readonly calls: Effect.Effect<ReadonlyArray<RecordedInvocationCall>>; |
| 41 | readonly reset: Effect.Effect<void>; |
| 42 | } |
| 43 | >()('test/InvocationRecorder') {} |
| 44 | |
| 45 | /** Every call the recording doubles received so far, oldest first. */ |
| 46 | export const recordedInvocationCalls = Effect.gen(function* () { |
| 47 | const recorder = yield* InvocationRecorder; |
| 48 | return yield* recorder.calls; |
| 49 | }); |
| 50 | |
| 51 | /** |
| 52 | * Forgets every call recorded so far. Tests that share one `TestLive` (`layer(TestLive())`) |
| 53 | * record into one recorder; reset it before the invocation a test asserts on. |
| 54 | */ |
| 55 | export const resetInvocationCalls = Effect.gen(function* () { |
| 56 | const recorder = yield* InvocationRecorder; |
| 57 | yield* recorder.reset; |
| 58 | }); |
| 59 | |
| 60 | /** |
| 61 | * Recording no-op doubles for the invocation hooks and the lifecycle telemetry sink: they print |
| 62 | * nothing and track nothing, so a test's stderr and analytics stay its command's own, and |
| 63 | * `InvocationRecorder` exposes what they were called with. |
| 64 | */ |
| 65 | export const InvocationHooksTest = Layer.unwrap( |
| 66 | Effect.gen(function* () { |
| 67 | const calls = yield* Ref.make<ReadonlyArray<RecordedInvocationCall>>([]); |
| 68 | const record = (call: RecordedInvocationCall) => Ref.update(calls, all => [...all, call]); |
| 69 | return Layer.mergeAll( |
| 70 | Layer.succeed( |
| 71 | InvocationRecorder, |
| 72 | InvocationRecorder.of({ calls: Ref.get(calls), reset: Ref.set(calls, []) }) |
| 73 | ), |
| 74 | Layer.succeed( |
| 75 | BackendMismatchWarning, |
| 76 | BackendMismatchWarning.of({ |
| 77 | run: invocation => record({ service: 'BackendMismatchWarning', invocation }), |
| 78 | }) |
| 79 | ), |
| 80 | Layer.succeed( |
| 81 | UpdateNotice, |
| 82 | UpdateNotice.of({ run: invocation => record({ service: 'UpdateNotice', invocation }) }) |
| 83 | ), |
| 84 | Layer.succeed( |
| 85 | PluginHint, |
| 86 | PluginHint.of({ run: invocation => record({ service: 'PluginHint', invocation }) }) |
| 87 | ), |
| 88 | Layer.succeed( |
| 89 | BackgroundUpdateCheck, |
| 90 | BackgroundUpdateCheck.of({ |
| 91 | run: invocation => record({ service: 'BackgroundUpdateCheck', invocation }), |
| 92 | }) |
| 93 | ), |
| 94 | Layer.succeed( |
| 95 | CommandLifecycleTelemetry, |
| 96 | CommandLifecycleTelemetry.of({ |
| 97 | invoked: context => |
| 98 | record({ service: 'CommandLifecycleTelemetry', event: 'invoked', context }), |
| 99 | succeeded: context => |
| 100 | record({ service: 'CommandLifecycleTelemetry', event: 'succeeded', context }), |
| 101 | failed: (context, cause) => |
| 102 | record({ service: 'CommandLifecycleTelemetry', event: 'failed', context, cause }), |
| 103 | pluginHintShown: shown => |
| 104 | record({ service: 'CommandLifecycleTelemetry', event: 'pluginHintShown', shown }), |
| 105 | }) |
| 106 | ) |
| 107 | ); |
| 108 | }) |
| 109 | ); |