Skip to content

Claude Code is an agentic coding tool that lives in your terminal, understands your codebase, and helps you code faster by executing routine tasks, explaining complex code, and handling git workflows - all through natural language commands.

TypeScript

mods: the telemetry contract exports its types at top level, its hooks and tests importing them from the folder, so the engine's repository reads the same file by path

poteat485170e06b3e
diff --git a/mods/README.md b/mods/README.md
index a5a7c553..ceb3f659 100644
--- a/mods/README.md
+++ b/mods/README.md
@@ -81,20 +81,21 @@ rendered, as a click in the terminal does.
## Composing mods: noun contracts
A mod that adds a noun to `$` in the `engine.create` fold owns that noun's
-types, and keeps them in one place: its `types/index.d.ts`, an ambient file
-with no imports that merges into `claude-code`, declaring the noun on
-`EngineInterface` and exporting the types it is made of, each named for the
-noun (`telemetry/types/index.d.ts` declares `$.telemetry` and exports
-`Telemetry`, `TelemetryLogEntry`, `TelemetryMarkEntry` and the rest).
+types, and keeps them in one place: its `types/index.d.ts`, a declaration
+file with no imports that exports the types the noun is made of, each named
+for the noun, and declares the noun on `EngineInterface` in `claude-code`
+(`telemetry/types/index.d.ts` exports `Telemetry`, `TelemetryLogEntry`,
+`TelemetryMarkEntry` and the rest, and declares `$.telemetry`).
- The contract is the only declaration of the noun. The mod's own hooks
- import its types from `claude-code` (`import type { Telemetry } from
- 'claude-code'`), and the value its `engine.create` hook returns is checked
+ import its types from the folder (`import type { Telemetry } from
+ '../types'`), and the value its `engine.create` hook returns is checked
against `EngineInterface['telemetry']`, so the implementation cannot drift
from what callers read.
- A mod that calls another's noun reads the same file and never copies it:
`mods/tsconfig.json` includes `*/types/**/*.d.ts`, so `$.telemetry.log(…)`
- in `diff` types against `telemetry`'s contract as it stands.
+ in `diff` types against `telemetry`'s contract as it stands, and a helper
+ that must name one of its types imports it from that folder by path.
- A test of a mod that calls another's noun seats a provider for it, an inline
plugin whose `engine.create` hook adds the noun, and answers the calls the
way it answers the engine's: `on('telemetry.log', ($, e) => ({ value:
diff --git a/mods/telemetry/hooks/entries/is-mark-kind/is-mark-kind.ts b/mods/telemetry/hooks/entries/is-mark-kind/is-mark-kind.ts
index 7b799b64..a5de8e2b 100644
--- a/mods/telemetry/hooks/entries/is-mark-kind/is-mark-kind.ts
+++ b/mods/telemetry/hooks/entries/is-mark-kind/is-mark-kind.ts
@@ -1,5 +1,4 @@
-import type { TelemetryMarkKind } from 'claude-code'
-
+import type { TelemetryMarkKind } from '../../../types'
import { MARK_KINDS } from '../mark-kinds'
/**
diff --git a/mods/telemetry/hooks/entries/mark-kinds/mark-kinds.ts b/mods/telemetry/hooks/entries/mark-kinds/mark-kinds.ts
index 59a1959a..ede36ad8 100644
--- a/mods/telemetry/hooks/entries/mark-kinds/mark-kinds.ts
+++ b/mods/telemetry/hooks/entries/mark-kinds/mark-kinds.ts
@@ -1,4 +1,4 @@
-import type { TelemetryMarkKind } from 'claude-code'
+import type { TelemetryMarkKind } from '../../../types'
/**
* The three kinds a mark may be, in the order the feature events name them.
diff --git a/mods/telemetry/hooks/entries/mark/mark.ts b/mods/telemetry/hooks/entries/mark/mark.ts
index ae93b3f2..0f2a1d64 100644
--- a/mods/telemetry/hooks/entries/mark/mark.ts
+++ b/mods/telemetry/hooks/entries/mark/mark.ts
@@ -1,5 +1,4 @@
-import type { TelemetryMarkKind } from 'claude-code'
-
+import type { TelemetryMarkKind } from '../../../types'
import type { Fields } from '../fields'
/**
diff --git a/mods/telemetry/hooks/telemetry-of/telemetry-of.ts b/mods/telemetry/hooks/telemetry-of/telemetry-of.ts
index 08146d9c..70395f1b 100644
--- a/mods/telemetry/hooks/telemetry-of/telemetry-of.ts
+++ b/mods/telemetry/hooks/telemetry-of/telemetry-of.ts
@@ -1,5 +1,4 @@
-import type { Telemetry } from 'claude-code'
-
+import type { Telemetry } from '../../types'
import Entries from '../entries'
import { isAnalyticsOff } from '../is-analytics-off'
import type { TelemetryDeps } from '../telemetry-deps'
diff --git a/mods/telemetry/tests/fixtures/record.ts b/mods/telemetry/tests/fixtures/record.ts
index 116fabb8..2d48156b 100644
--- a/mods/telemetry/tests/fixtures/record.ts
+++ b/mods/telemetry/tests/fixtures/record.ts
@@ -1,4 +1,6 @@
-import type { CommandRunInput, TelemetryLogEntry } from 'claude-code'
+import type { CommandRunInput } from 'claude-code'
+
+import type { TelemetryLogEntry } from '../../types'
/**
* The command that has the recording plugin log an entry, typed as the
diff --git a/mods/telemetry/tests/fixtures/survey-answer.ts b/mods/telemetry/tests/fixtures/survey-answer.ts
index 0c8c43ac..46553e7e 100644
--- a/mods/telemetry/tests/fixtures/survey-answer.ts
+++ b/mods/telemetry/tests/fixtures/survey-answer.ts
@@ -1,4 +1,4 @@
-import type { TelemetryLogEntry } from 'claude-code'
+import type { TelemetryLogEntry } from '../../types'
/**
* A survey answered, as a plugin logs it.
diff --git a/mods/telemetry/types/index.d.ts b/mods/telemetry/types/index.d.ts
index dc12fa12..6527f970 100644
--- a/mods/telemetry/types/index.d.ts
+++ b/mods/telemetry/types/index.d.ts
@@ -1,115 +1,116 @@
/**
* The `$.telemetry` noun as every caller sees it: the one contract for the
- * noun, merged into `claude-code` beside the engine's own declarations.
+ * noun, its types exported here and the noun declared on `EngineInterface`.
*
* The telemetry mod adds the noun in the `engine.create` fold and checks its
- * return against `EngineInterface['telemetry']`; a mod that calls it, and a
- * test that answers it with `on('telemetry.log', …)`, read the same types by
- * including this folder in their tsconfig. Nothing here is imported: the file
- * is ambient, so it merges wherever it is included.
+ * return against `EngineInterface['telemetry']`; its hooks import these types
+ * from this folder, a mod that calls the noun and a test that answers it read
+ * them by including it in their tsconfig, and the engine's repository imports
+ * the folder by path. Nothing here is imported, so it stands on its own.
*/
-declare module 'claude-code' {
- interface EngineInterface {
- /**
- * A plugin's analytics, one first-party row per call; present only where
- * the telemetry mod is seated (internal builds), absent everywhere else.
- */
- telemetry: Telemetry
- }
+/**
+ * A plugin's analytics, sent one event at a time through `$.telemetry`.
+ *
+ * Internal builds alone: the telemetry mod adds the noun in the
+ * `engine.create` fold, so a plugin on an external build, or one where the
+ * mod is off, finds no `$.telemetry` and its call throws.
+ */
+export type Telemetry = {
/**
- * A plugin's analytics, sent one event at a time through `$.telemetry`.
+ * Sends one event, `tengu_plugin_<event>`, as one first-party row;
+ * resolves once the ingest accepted it.
+ *
+ * The calling mod names itself in `event`; one already named `tengu_…` is
+ * sent as named. A value is a finite number, a boolean or a
+ * TelemetryChoice; free text is refused. One input, as every op on `$`
+ * takes.
*
- * Internal builds alone: the telemetry mod adds the noun in the
- * `engine.create` fold, so a plugin on an external build, or one where the
- * mod is off, finds no `$.telemetry` and its call throws.
+ * @param entry the event's name, a snake_case token, and its properties by
+ * snake_case key
+ * @example
+ * await $.telemetry.log({
+ * event: "suggest_learning_survey_answered",
+ * props: {
+ * answer: 2,
+ * page: { value: "ready", of: ["ready", "later"] },
+ * },
+ * })
*/
- export type Telemetry = {
- /**
- * Sends one event, `tengu_plugin_<event>`, as one first-party row;
- * resolves once the ingest accepted it.
- *
- * The calling mod names itself in `event`; one already named `tengu_…` is
- * sent as named. A value is a finite number, a boolean or a
- * TelemetryChoice; free text is refused. One input, as every op on `$`
- * takes.
- *
- * @param entry the event's name, a snake_case token, and its properties by
- * snake_case key
- * @example
- * await $.telemetry.log({
- * event: "suggest_learning_survey_answered",
- * props: {
- * answer: 2,
- * page: { value: "ready", of: ["ready", "later"] },
- * },
- * })
- */
- log: (entry: TelemetryLogEntry) => Promise<void>
-
- /**
- * Marks one use of a feature as the CLI's own feature events do, one
- * `tengu_feature_<kind>` row; resolves once the ingest accepted it.
- *
- * The row carries `feature_name`, `error_code` on sad or bad (`reason`,
- * required there and refused on ok) and the entry's `props`, checked as
- * `log`'s are; it joins the product-wide feature surface, so no prefix.
- *
- * @param entry the feature, how it went, why when not ok, and the row's
- * properties by snake_case key
- * @example
- * await $.telemetry.mark({ feature: "learn_page", kind: "ok" })
- * await $.telemetry.mark({
- * feature: "learn_page",
- * kind: "sad",
- * reason: "blocked",
- * })
- */
- mark: (entry: TelemetryMarkEntry) => Promise<void>
- }
+ log: (entry: TelemetryLogEntry) => Promise<void>
/**
- * What `$.telemetry.log` takes: the event's name after the prefix, and its
- * properties by snake_case key.
+ * Marks one use of a feature as the CLI's own feature events do, one
+ * `tengu_feature_<kind>` row; resolves once the ingest accepted it.
+ *
+ * The row carries `feature_name`, `error_code` on sad or bad (`reason`,
+ * required there and refused on ok) and the entry's `props`, checked as
+ * `log`'s are; it joins the product-wide feature surface, so no prefix.
+ *
+ * @param entry the feature, how it went, why when not ok, and the row's
+ * properties by snake_case key
+ * @example
+ * await $.telemetry.mark({ feature: "learn_page", kind: "ok" })
+ * await $.telemetry.mark({
+ * feature: "learn_page",
+ * kind: "sad",
+ * reason: "blocked",
+ * })
*/
- export type TelemetryLogEntry = {
- event: string
- props?: Readonly<Record<string, TelemetryProp>>
- }
+ mark: (entry: TelemetryMarkEntry) => Promise<void>
+}
- /**
- * What `$.telemetry.mark` takes: the feature, how it went, why when not
- * ok, and the properties the row carries beside them by snake_case key.
- */
- export type TelemetryMarkEntry = {
- feature: string
- kind: TelemetryMarkKind
- reason?: string
- props?: Readonly<Record<string, TelemetryProp>>
- }
+/**
+ * What `$.telemetry.log` takes: the event's name after the prefix, and its
+ * properties by snake_case key.
+ */
+export type TelemetryLogEntry = {
+ event: string
+ props?: Readonly<Record<string, TelemetryProp>>
+}
- /**
- * How a feature went, as the CLI's own feature events count it.
- *
- * `ok`: used, the person got what they asked. `sad`: degraded, a fallback
- * or a partial, the person still got something. `bad`: failed, the person
- * got nothing.
- */
- export type TelemetryMarkKind = 'ok' | 'sad' | 'bad'
+/**
+ * What `$.telemetry.mark` takes: the feature, how it went, why when not
+ * ok, and the properties the row carries beside them by snake_case key.
+ */
+export type TelemetryMarkEntry = {
+ feature: string
+ kind: TelemetryMarkKind
+ reason?: string
+ props?: Readonly<Record<string, TelemetryProp>>
+}
- /**
- * A property's value: a finite number, a boolean, or a TelemetryChoice;
- * never free text.
- */
- export type TelemetryProp = number | boolean | TelemetryChoice
+/**
+ * How a feature went, as the CLI's own feature events count it.
+ *
+ * `ok`: used, the person got what they asked. `sad`: degraded, a fallback
+ * or a partial, the person still got something. `bad`: failed, the person
+ * got nothing.
+ */
+export type TelemetryMarkKind = 'ok' | 'sad' | 'bad'
- /**
- * A string property: the value and the list it is chosen from, declared
- * beside it, so no free text reaches the row.
- *
- * Every member of `of` is a lowercase token of letters, digits, `_` and
- * `-`, which may start with a digit, at most 32 of them; `value` is one of
- * them.
- */
- export type TelemetryChoice = { value: string; of: readonly string[] }
+/**
+ * A property's value: a finite number, a boolean, or a TelemetryChoice;
+ * never free text.
+ */
+export type TelemetryProp = number | boolean | TelemetryChoice
+
+/**
+ * A string property: the value and the list it is chosen from, declared
+ * beside it, so no free text reaches the row.
+ *
+ * Every member of `of` is a lowercase token of letters, digits, `_` and
+ * `-`, which may start with a digit, at most 32 of them; `value` is one of
+ * them.
+ */
+export type TelemetryChoice = { value: string; of: readonly string[] }
+
+declare module 'claude-code' {
+ interface EngineInterface {
+ /**
+ * A plugin's analytics, one first-party row per call; present only where
+ * the telemetry mod is seated (internal builds), absent everywhere else.
+ */
+ telemetry: Telemetry
+ }
}