The best way to control Claude Code, Codex and more from any computer or phone

main
ts 333 lines 12.9 KB
Raw
1 import * as Axiom from "@distilled.cloud/axiom";
2 import { Credentials } from "@distilled.cloud/axiom/Credentials";
3 import { Unowned } from "alchemy/AdoptPolicy";
4 import { isResolved } from "alchemy/Diff";
5 import * as Provider from "alchemy/Provider";
6 import { Resource } from "alchemy/Resource";
7 import { Stack } from "alchemy/Stack";
8 import { Stage } from "alchemy/Stage";
9 import * as Data from "effect/Data";
10 import * as Effect from "effect/Effect";
11 import { otelDatasetHeader } from "./OtelHeaders.ts";
12 import type { Providers } from "./Providers.ts";
13
14 export class DatasetUnexpectedResponse extends Data.TaggedError("DatasetUnexpectedResponse")<{
15 readonly message: string;
16 readonly cause?: unknown;
17 }> {}
18
19 export type DatasetKind =
20 | "otel:metrics:v1"
21 | "otel:traces:v1"
22 | "otel:logs:v1"
23 | "axiom:events:v1"
24 | (string & {});
25
26 export type DatasetProps = {
27 /**
28 * Dataset name. Used as the dataset's stable identifier in Axiom — changing
29 * this triggers a replacement.
30 */
31 name: string;
32 /** Free-form description shown in the Axiom UI. */
33 description?: string;
34 /**
35 * Edge deployment where Axiom stores and processes the dataset's data.
36 * Defaults to the organization's default edge deployment.
37 *
38 * Cannot be changed after creation — triggers a replacement.
39 */
40 edgeDeployment?: string;
41 /**
42 * Dataset kind. Defaults to `axiom:events:v1`.
43 *
44 * For OTEL pipelines, choose:
45 * - `otel:traces:v1` for traces
46 * - `otel:logs:v1` for logs
47 * - `otel:metrics:v1` for metrics
48 *
49 * Cannot be changed after creation — triggers a replacement.
50 */
51 kind?: DatasetKind;
52 /** Retention in days. Plan-dependent — see Axiom docs. */
53 retentionDays?: number;
54 /** Whether to enforce the configured retention period. */
55 useRetentionPeriod?: boolean;
56 };
57
58 export type Dataset = Resource<
59 "Axiom.Dataset",
60 DatasetProps,
61 {
62 id: string;
63 name: string;
64 kind: DatasetKind;
65 description: string;
66 created: string;
67 /** Edge deployment where the dataset's data is stored and processed. */
68 edgeDeployment: string;
69 /** Base URL for ingesting and querying data in the dataset's edge deployment. */
70 edgeDeploymentUrl: string;
71 /** Base URL for Axiom's centralized management API. */
72 apiBaseUrl: string;
73 /** Root OTLP endpoint. Most exporters auto-append the signal path. */
74 otelEndpoint: string;
75 /** OTLP/HTTP traces endpoint. */
76 otelTracesEndpoint: string;
77 /** OTLP/HTTP logs endpoint. */
78 otelLogsEndpoint: string;
79 /** OTLP/HTTP metrics endpoint. */
80 otelMetricsEndpoint: string;
81 /**
82 * Headers required for OTLP shipping aside from the bearer token:
83 * `X-Axiom-Dataset`, or `X-Axiom-Metrics-Dataset` for an
84 * `otel:metrics:v1` dataset.
85 * Add `Authorization: Bearer <AXIOM_TOKEN>` separately at runtime so the
86 * secret is never persisted in resource state.
87 */
88 otelHeaders: Record<string, string>;
89 },
90 never,
91 Providers
92 >;
93
94 /**
95 * An Axiom dataset — the top-level container that stores events, logs,
96 * traces, or metrics. Pick a `kind` up-front: it determines schema and how
97 * the data is shown in the UI, and **cannot be changed** after creation
98 * (changing it triggers a replacement, which deletes the data).
99 *
100 * Datasets expose their edge deployment and Axiom's OTLP/HTTP endpoints
101 * (`otelTracesEndpoint`, `otelLogsEndpoint`, `otelMetricsEndpoint`) as output
102 * attributes so you can inject them into a Worker / Lambda's env vars for
103 * OpenTelemetry shipping. The bearer token is **not** stored in resource state
104 * — supply `Authorization: Bearer <AXIOM_TOKEN>` separately at runtime.
105 * @see https://axiom.co/docs/reference/datasets
106 * @see https://axiom.co/docs/send-data/opentelemetry — OTLP endpoint reference
107 *
108 * ### Creating a Dataset
109 * **Example:** Logs dataset with 30-day retention
110 * ```typescript
111 * const logs = yield* Axiom.Dataset("app-logs", {
112 * name: "my-app-logs",
113 * kind: "otel:logs:v1",
114 * description: "Application logs from prod workers",
115 * retentionDays: 30,
116 * useRetentionPeriod: true,
117 * });
118 * ```
119 *
120 * **Example:** Separate datasets per OTEL signal
121 * ```typescript
122 * const traces = yield* Axiom.Dataset("traces", { name: "app-traces", kind: "otel:traces:v1" });
123 * const logs = yield* Axiom.Dataset("logs", { name: "app-logs", kind: "otel:logs:v1" });
124 * const metrics = yield* Axiom.Dataset("metrics", { name: "app-metrics", kind: "otel:metrics:v1" });
125 * ```
126 *
127 * ### Shipping OTEL data
128 * **Example:** Wire OTEL env vars into a Cloudflare Worker
129 * ```typescript
130 * yield* Cloudflare.Worker("api", {
131 * vars: {
132 * OTEL_EXPORTER_OTLP_TRACES_ENDPOINT: traces.otelTracesEndpoint,
133 * OTEL_EXPORTER_OTLP_LOGS_ENDPOINT: logs.otelLogsEndpoint,
134 * // Bearer token must come from a secret store, not the dataset state.
135 * OTEL_EXPORTER_OTLP_HEADERS:
136 * `Authorization=Bearer ${env.AXIOM_TOKEN},X-Axiom-Dataset=${traces.name}`,
137 * },
138 * });
139 * ```
140 *
141 * @resource
142 * @product Dataset
143 */
144 export const Dataset = Resource<Dataset>("Axiom.Dataset");
145
146 /**
147 * Axiom has no tags/labels API. The only writable field we can use to mark
148 * ownership is `description`. We append a deterministic marker on create so
149 * that on a re-apply (e.g. state was wiped) we can safely identify a dataset
150 * that **we** previously created and adopt it idempotently — without
151 * accidentally hijacking a dataset created by someone else with the same name.
152 */
153 const MARKER_RE = /\s*\[alchemy:stack=([^;]+);stage=([^;]+);id=([^\]]+)\]\s*$/;
154
155 const buildMarker = (stack: string, stage: string, id: string) =>
156 `[alchemy:stack=${stack};stage=${stage};id=${id}]`;
157
158 const augmentDescription = (description: string | undefined, marker: string) => {
159 const base = stripMarker(description ?? "");
160 return base.length > 0 ? `${base}\n${marker}` : marker;
161 };
162
163 const stripMarker = (description: string): string => description.replace(MARKER_RE, "").trimEnd();
164
165 const parseMarker = (
166 description: string | undefined,
167 ): { stack: string; stage: string; id: string } | undefined => {
168 if (!description) return undefined;
169 const m = description.match(MARKER_RE);
170 if (!m) return undefined;
171 return { stack: m[1], stage: m[2], id: m[3] };
172 };
173
174 export const DatasetProvider = () =>
175 Provider.effect(
176 Dataset,
177 Effect.gen(function* () {
178 const credentials = yield* Credentials;
179 const create = yield* Axiom.createDataset;
180 const update = yield* Axiom.updateDataset;
181 const get = yield* Axiom.getDataset;
182 const listDatasets = yield* Axiom.getDatasets;
183 const del = yield* Axiom.deleteDataset;
184
185 const toAttrs = Effect.fn(function* (dataset: Axiom.Dataset) {
186 if (!dataset.edgeDeployment || !dataset.edgeDeploymentUrl) {
187 return yield* new DatasetUnexpectedResponse({
188 message: `Axiom dataset "${dataset.name}" is missing its edge deployment metadata`,
189 });
190 }
191 const { apiBaseUrl } = yield* credentials;
192 const apiRoot = apiBaseUrl.replace(/\/$/, "");
193 const otelRoot = dataset.edgeDeploymentUrl.replace(/\/$/, "");
194 return {
195 id: dataset.id,
196 name: dataset.name,
197 kind: dataset.kind,
198 description: stripMarker(dataset.description),
199 created: dataset.created,
200 edgeDeployment: dataset.edgeDeployment,
201 edgeDeploymentUrl: dataset.edgeDeploymentUrl,
202 apiBaseUrl: apiRoot,
203 otelEndpoint: otelRoot,
204 otelTracesEndpoint: `${otelRoot}/v1/traces`,
205 otelLogsEndpoint: `${otelRoot}/v1/logs`,
206 otelMetricsEndpoint: `${otelRoot}/v1/metrics`,
207 otelHeaders: {
208 [otelDatasetHeader(dataset.kind === "otel:metrics:v1")]: dataset.name,
209 } as Record<string, string>,
210 };
211 });
212
213 return {
214 stables: ["id", "name", "kind", "edgeDeployment", "edgeDeploymentUrl"],
215 // Enumerate every dataset in the org. Axiom exposes a single
216 // account-wide `GET /v2/datasets` collection op (no pagination), so we
217 // fetch it once and hydrate each row into the exact `read`/`toAttrs`
218 // Attributes shape — directly usable by `delete` with no follow-up get.
219 list: () =>
220 Effect.gen(function* () {
221 const datasets = yield* listDatasets({});
222 return yield* Effect.forEach(datasets, toAttrs);
223 }),
224 diff: Effect.fn(function* ({ olds, news, output }) {
225 if (!isResolved(news)) return undefined;
226 if (output && news.name !== output.name) {
227 return { action: "replace" } as const;
228 }
229 if (news.kind && output && news.kind !== output.kind) {
230 return { action: "replace" } as const;
231 }
232 if (news.edgeDeployment && output && news.edgeDeployment !== output.edgeDeployment) {
233 return { action: "replace" } as const;
234 }
235 if (
236 news.description !== olds?.description ||
237 news.retentionDays !== olds?.retentionDays ||
238 news.useRetentionPeriod !== olds?.useRetentionPeriod
239 ) {
240 return { action: "update" } as const;
241 }
242 return undefined;
243 }),
244 reconcile: Effect.fn(function* ({ id, news, output }) {
245 const stack = yield* Stack;
246 const stage = yield* Stage;
247 const marker = buildMarker(stack.name, stage, id);
248
249 // Observe — Axiom's dataset path identifier is `name`, which is
250 // also stable input. Probe for live state by name (preferring the
251 // cached `output.id` when present). `read` upstream has already
252 // surfaced foreign datasets as `Unowned`, so by the time we land
253 // here mutation is safe.
254 const datasetId = output?.id ?? news.name;
255 const observed = yield* get({ dataset_id: datasetId }).pipe(
256 Effect.catchTag("NotFound", () => Effect.undefined),
257 );
258
259 // Ensure — POST creates the dataset. Tolerate Conflict/
260 // UnprocessableEntity as a race with a peer reconciler (or with
261 // upstream read↔create), falling through to the sync path.
262 let current = observed;
263 if (current === undefined) {
264 current = yield* (
265 create({
266 name: news.name,
267 description: augmentDescription(news.description, marker),
268 edgeDeployment: news.edgeDeployment,
269 kind: news.kind,
270 retentionDays: news.retentionDays,
271 useRetentionPeriod: news.useRetentionPeriod,
272 }) as Effect.Effect<Axiom.Dataset, { readonly _tag: string }, never>
273 ).pipe(
274 Effect.catchIf(
275 (e): e is { readonly _tag: "Conflict" | "UnprocessableEntity" } =>
276 e._tag === "Conflict" || e._tag === "UnprocessableEntity",
277 () =>
278 update({
279 dataset_id: news.name,
280 description: augmentDescription(news.description, marker),
281 retentionDays: news.retentionDays,
282 useRetentionPeriod: news.useRetentionPeriod,
283 }),
284 ),
285 );
286 return yield* toAttrs(current);
287 }
288
289 // Sync — the dataset exists. Apply mutable aspects (description,
290 // retentionDays, useRetentionPeriod) via PATCH. `kind` and `name`
291 // are stable and replacement-only via diff above.
292 const desiredDescription = augmentDescription(news.description, marker);
293 const needsSync =
294 current.description !== desiredDescription ||
295 current.retentionDays !== news.retentionDays ||
296 current.useRetentionPeriod !== news.useRetentionPeriod;
297 if (!needsSync) {
298 return yield* toAttrs(current);
299 }
300 const updated = yield* update({
301 dataset_id: current.id,
302 description: desiredDescription,
303 retentionDays: news.retentionDays,
304 useRetentionPeriod: news.useRetentionPeriod,
305 });
306 return yield* toAttrs(updated);
307 }),
308 delete: Effect.fn(function* ({ output }) {
309 yield* del({ dataset_id: output.id }).pipe(
310 Effect.catchTag("NotFound", () => Effect.void),
311 );
312 }),
313 read: Effect.fn(function* ({ id, olds, output }) {
314 const stack = yield* Stack;
315 const stage = yield* Stage;
316 const datasetId = output?.id ?? olds?.name;
317 if (!datasetId) return undefined;
318 const existing = yield* get({ dataset_id: datasetId }).pipe(
319 Effect.catchTag("NotFound", () => Effect.undefined),
320 );
321 if (!existing) return undefined;
322 const ownership = parseMarker(existing.description);
323 const isOurs =
324 ownership !== undefined &&
325 ownership.stack === stack.name &&
326 ownership.stage === stage &&
327 ownership.id === id;
328 const attrs = yield* toAttrs(existing);
329 return isOurs ? attrs : Unowned(attrs);
330 }),
331 };
332 }),
333 );