archik 0.15.1 → 0.16.0

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.
@@ -0,0 +1,18 @@
1
+ export type TraceStepInput = {
2
+ id?: string;
3
+ from: string;
4
+ to: string;
5
+ label: string;
6
+ in?: unknown;
7
+ out?: unknown;
8
+ status?: "ok" | "error";
9
+ };
10
+ export type TraceRecorder = {
11
+ step(s: TraceStepInput): TraceRecorder;
12
+ flush(): void;
13
+ };
14
+ export declare function trace(opts: {
15
+ useCase: string;
16
+ slice: string;
17
+ seqFile?: string;
18
+ }): TraceRecorder;
package/dist/trace.js ADDED
@@ -0,0 +1,58 @@
1
+ // src/trace/recorder.ts
2
+ import { existsSync, mkdirSync, writeFileSync } from "node:fs";
3
+ import path from "node:path";
4
+ function findProjectRoot(start) {
5
+ let dir = start;
6
+ for (let i = 0; i < 30; i++) {
7
+ if (existsSync(path.join(dir, ".archik"))) return dir;
8
+ const parent = path.dirname(dir);
9
+ if (parent === dir) break;
10
+ dir = parent;
11
+ }
12
+ return start;
13
+ }
14
+ function trace(opts) {
15
+ const steps = [];
16
+ let flushed = false;
17
+ const onExit = () => recorder.flush();
18
+ const recorder = {
19
+ step(s) {
20
+ const data = s.in !== void 0 || s.out !== void 0 ? {
21
+ ...s.in !== void 0 ? { in: s.in } : {},
22
+ ...s.out !== void 0 ? { out: s.out } : {}
23
+ } : void 0;
24
+ steps.push({
25
+ ...s.id !== void 0 ? { id: s.id } : {},
26
+ from: s.from,
27
+ to: s.to,
28
+ label: s.label,
29
+ ...data !== void 0 ? { data } : {},
30
+ status: s.status ?? "ok"
31
+ });
32
+ return recorder;
33
+ },
34
+ flush() {
35
+ if (flushed) return;
36
+ flushed = true;
37
+ const root = findProjectRoot(process.cwd());
38
+ const dir = path.join(root, ".archik", "traces");
39
+ mkdirSync(dir, { recursive: true });
40
+ const doc = {
41
+ version: "1.0",
42
+ useCase: opts.useCase,
43
+ slice: opts.slice,
44
+ ...opts.seqFile !== void 0 ? { seqFile: opts.seqFile } : {},
45
+ recordedAt: (/* @__PURE__ */ new Date()).toISOString(),
46
+ steps
47
+ };
48
+ const file = path.join(dir, `${opts.useCase}.${opts.slice}.archik.trace.json`);
49
+ writeFileSync(file, JSON.stringify(doc, null, 2) + "\n", "utf-8");
50
+ process.removeListener("exit", onExit);
51
+ }
52
+ };
53
+ process.on("exit", onExit);
54
+ return recorder;
55
+ }
56
+ export {
57
+ trace
58
+ };