retrace-sdk 0.1.4 → 0.1.5
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.
- package/dist/sdk.test.d.ts +1 -0
- package/dist/sdk.test.js +132 -0
- package/package.json +4 -3
- package/src/sdk.test.ts +152 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/sdk.test.js
ADDED
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
import { describe, it, expect, vi, beforeEach } from "vitest";
|
|
2
|
+
import { configure, getConfig } from "./config.js";
|
|
3
|
+
// Reset config between tests
|
|
4
|
+
beforeEach(() => {
|
|
5
|
+
configure({ apiKey: "", baseUrl: "http://localhost:3001", enabled: true, wsUrl: "" });
|
|
6
|
+
});
|
|
7
|
+
describe("Config", () => {
|
|
8
|
+
it("has sensible defaults", () => {
|
|
9
|
+
const c = getConfig();
|
|
10
|
+
expect(c.baseUrl).toBe("http://localhost:3001");
|
|
11
|
+
expect(c.enabled).toBe(true);
|
|
12
|
+
});
|
|
13
|
+
it("configures custom values", () => {
|
|
14
|
+
configure({ apiKey: "rt_live_test", baseUrl: "https://api.example.com" });
|
|
15
|
+
const c = getConfig();
|
|
16
|
+
expect(c.apiKey).toBe("rt_live_test");
|
|
17
|
+
expect(c.baseUrl).toBe("https://api.example.com");
|
|
18
|
+
expect(c.wsUrl).toBe("wss://api.example.com");
|
|
19
|
+
});
|
|
20
|
+
it("auto-derives wsUrl from baseUrl", () => {
|
|
21
|
+
configure({ baseUrl: "https://custom.io" });
|
|
22
|
+
expect(getConfig().wsUrl).toBe("wss://custom.io");
|
|
23
|
+
configure({ baseUrl: "http://localhost:3001" });
|
|
24
|
+
expect(getConfig().wsUrl).toBe("ws://localhost:3001");
|
|
25
|
+
});
|
|
26
|
+
it("warns on invalid API key prefix", () => {
|
|
27
|
+
const spy = vi.spyOn(console, "warn").mockImplementation(() => { });
|
|
28
|
+
configure({ apiKey: "invalid_key" });
|
|
29
|
+
expect(spy).toHaveBeenCalledWith(expect.stringContaining("rt_live_"));
|
|
30
|
+
spy.mockRestore();
|
|
31
|
+
});
|
|
32
|
+
it("does not warn on valid API key", () => {
|
|
33
|
+
const spy = vi.spyOn(console, "warn").mockImplementation(() => { });
|
|
34
|
+
configure({ apiKey: "rt_live_valid123" });
|
|
35
|
+
expect(spy).not.toHaveBeenCalled();
|
|
36
|
+
spy.mockRestore();
|
|
37
|
+
});
|
|
38
|
+
});
|
|
39
|
+
describe("Trace model", () => {
|
|
40
|
+
it("SpanBuilder creates valid span data", async () => {
|
|
41
|
+
const { SpanBuilder, SpanType } = await import("./trace.js");
|
|
42
|
+
const sb = new SpanBuilder("test-span", SpanType.LLM_CALL).start();
|
|
43
|
+
sb.setModel("gpt-4o");
|
|
44
|
+
sb.setInput({ prompt: "hi" });
|
|
45
|
+
const data = sb.end("hello");
|
|
46
|
+
expect(data.name).toBe("test-span");
|
|
47
|
+
expect(data.span_type).toBe("llm_call");
|
|
48
|
+
expect(data.model).toBe("gpt-4o");
|
|
49
|
+
expect(data.output).toBe("hello");
|
|
50
|
+
expect(data.id).toHaveLength(36);
|
|
51
|
+
expect(data.started_at).toBeDefined();
|
|
52
|
+
expect(data.ended_at).toBeDefined();
|
|
53
|
+
});
|
|
54
|
+
it("TraceBuilder creates valid trace data", async () => {
|
|
55
|
+
const { TraceBuilder, TraceStatus } = await import("./trace.js");
|
|
56
|
+
const tb = new TraceBuilder();
|
|
57
|
+
tb.start("my-trace", { prompt: "test" });
|
|
58
|
+
const data = tb.end("result", TraceStatus.COMPLETED);
|
|
59
|
+
expect(data.name).toBe("my-trace");
|
|
60
|
+
expect(data.status).toBe("completed");
|
|
61
|
+
expect(data.output).toBe("result");
|
|
62
|
+
expect(data.id).toHaveLength(36);
|
|
63
|
+
});
|
|
64
|
+
it("SpanType enum values", async () => {
|
|
65
|
+
const { SpanType } = await import("./trace.js");
|
|
66
|
+
expect(SpanType.LLM_CALL).toBe("llm_call");
|
|
67
|
+
expect(SpanType.TOOL_CALL).toBe("tool_call");
|
|
68
|
+
expect(SpanType.ERROR).toBe("error");
|
|
69
|
+
expect(SpanType.FORK_POINT).toBe("fork_point");
|
|
70
|
+
});
|
|
71
|
+
});
|
|
72
|
+
describe("Recorder", () => {
|
|
73
|
+
it("trace() wraps sync function", async () => {
|
|
74
|
+
configure({ apiKey: "rt_live_t", baseUrl: "http://x:1", enabled: true });
|
|
75
|
+
const { trace } = await import("./recorder.js");
|
|
76
|
+
const fn = trace((x) => `result:${x}`, { name: "sync-fn" });
|
|
77
|
+
expect(fn("hi")).toBe("result:hi");
|
|
78
|
+
});
|
|
79
|
+
it("trace() wraps async function", async () => {
|
|
80
|
+
configure({ apiKey: "rt_live_t", baseUrl: "http://x:1", enabled: true });
|
|
81
|
+
const { trace } = await import("./recorder.js");
|
|
82
|
+
const fn = trace(async (x) => `async:${x}`, { name: "async-fn" });
|
|
83
|
+
const result = await fn("hi");
|
|
84
|
+
expect(result).toBe("async:hi");
|
|
85
|
+
});
|
|
86
|
+
it("trace() propagates exceptions", async () => {
|
|
87
|
+
configure({ apiKey: "rt_live_t", baseUrl: "http://x:1", enabled: true });
|
|
88
|
+
const { trace } = await import("./recorder.js");
|
|
89
|
+
const fn = trace(() => { throw new Error("boom"); }, { name: "err-fn" });
|
|
90
|
+
expect(() => fn()).toThrow("boom");
|
|
91
|
+
});
|
|
92
|
+
it("disabled SDK is no-op", async () => {
|
|
93
|
+
configure({ apiKey: "rt_live_t", enabled: false });
|
|
94
|
+
const { trace } = await import("./recorder.js");
|
|
95
|
+
const fn = trace((x) => `r:${x}`, { name: "off" });
|
|
96
|
+
expect(fn("hi")).toBe("r:hi");
|
|
97
|
+
});
|
|
98
|
+
});
|
|
99
|
+
describe("Transport", () => {
|
|
100
|
+
it("HTTPTransport accumulates and flushes", async () => {
|
|
101
|
+
configure({ apiKey: "rt_live_t", baseUrl: "http://x:1" });
|
|
102
|
+
const { HTTPTransport } = await import("./transport.js");
|
|
103
|
+
const http = new HTTPTransport();
|
|
104
|
+
const mockFetch = vi.fn().mockResolvedValue({ ok: true });
|
|
105
|
+
vi.stubGlobal("fetch", mockFetch);
|
|
106
|
+
http.send("trace_started", { id: "abc", name: "t", status: "running", started_at: "2026-01-01T00:00:00Z", total_tokens: 0, total_cost: 0, total_duration_ms: 0 });
|
|
107
|
+
http.send("span_started", { id: "s1", trace_id: "abc", span_type: "llm_call", name: "s", started_at: "2026-01-01T00:00:00Z" });
|
|
108
|
+
http.send("span_ended", { id: "s1", ended_at: "2026-01-01T00:00:01Z", output: "hi" });
|
|
109
|
+
http.send("trace_ended", { id: "abc", ended_at: "2026-01-01T00:00:02Z", status: "completed" });
|
|
110
|
+
expect(mockFetch).toHaveBeenCalled();
|
|
111
|
+
const body = JSON.parse(mockFetch.mock.calls[0][1].body);
|
|
112
|
+
expect(body.id).toBe("abc");
|
|
113
|
+
expect(body.spans).toHaveLength(1);
|
|
114
|
+
expect(body.spans[0].output).toBe("hi");
|
|
115
|
+
vi.unstubAllGlobals();
|
|
116
|
+
});
|
|
117
|
+
it("createTransport returns correct type", async () => {
|
|
118
|
+
const { createTransport, HTTPTransport } = await import("./transport.js");
|
|
119
|
+
expect(createTransport("http")).toBeInstanceOf(HTTPTransport);
|
|
120
|
+
});
|
|
121
|
+
});
|
|
122
|
+
describe("README examples", () => {
|
|
123
|
+
it("TypeScript README example works", async () => {
|
|
124
|
+
configure({ apiKey: "rt_live_test", enabled: false });
|
|
125
|
+
const { trace } = await import("./recorder.js");
|
|
126
|
+
const runAgent = trace(async (prompt) => {
|
|
127
|
+
return `Response to: ${prompt}`;
|
|
128
|
+
}, { name: "my-agent" });
|
|
129
|
+
const result = await runAgent("What is quantum computing?");
|
|
130
|
+
expect(result).toContain("quantum");
|
|
131
|
+
});
|
|
132
|
+
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "retrace-sdk",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.5",
|
|
4
4
|
"description": "The execution replay engine for AI agents. Record, replay, fork, and share agent executions.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -29,8 +29,9 @@
|
|
|
29
29
|
}
|
|
30
30
|
},
|
|
31
31
|
"devDependencies": {
|
|
32
|
-
"
|
|
32
|
+
"@google/genai": "^1.52.0",
|
|
33
33
|
"@types/node": "22.15.3",
|
|
34
|
-
"@types/ws": "8.18.1"
|
|
34
|
+
"@types/ws": "8.18.1",
|
|
35
|
+
"typescript": "6.0.3"
|
|
35
36
|
}
|
|
36
37
|
}
|
package/src/sdk.test.ts
ADDED
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
import { describe, it, expect, vi, beforeEach } from "vitest";
|
|
2
|
+
import { configure, getConfig } from "./config.js";
|
|
3
|
+
|
|
4
|
+
// Reset config between tests
|
|
5
|
+
beforeEach(() => {
|
|
6
|
+
configure({ apiKey: "", baseUrl: "http://localhost:3001", enabled: true, wsUrl: "" });
|
|
7
|
+
});
|
|
8
|
+
|
|
9
|
+
describe("Config", () => {
|
|
10
|
+
it("has sensible defaults", () => {
|
|
11
|
+
const c = getConfig();
|
|
12
|
+
expect(c.baseUrl).toBe("http://localhost:3001");
|
|
13
|
+
expect(c.enabled).toBe(true);
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
it("configures custom values", () => {
|
|
17
|
+
configure({ apiKey: "rt_live_test", baseUrl: "https://api.example.com" });
|
|
18
|
+
const c = getConfig();
|
|
19
|
+
expect(c.apiKey).toBe("rt_live_test");
|
|
20
|
+
expect(c.baseUrl).toBe("https://api.example.com");
|
|
21
|
+
expect(c.wsUrl).toBe("wss://api.example.com");
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
it("auto-derives wsUrl from baseUrl", () => {
|
|
25
|
+
configure({ baseUrl: "https://custom.io" });
|
|
26
|
+
expect(getConfig().wsUrl).toBe("wss://custom.io");
|
|
27
|
+
configure({ baseUrl: "http://localhost:3001" });
|
|
28
|
+
expect(getConfig().wsUrl).toBe("ws://localhost:3001");
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
it("warns on invalid API key prefix", () => {
|
|
32
|
+
const spy = vi.spyOn(console, "warn").mockImplementation(() => {});
|
|
33
|
+
configure({ apiKey: "invalid_key" });
|
|
34
|
+
expect(spy).toHaveBeenCalledWith(expect.stringContaining("rt_live_"));
|
|
35
|
+
spy.mockRestore();
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it("does not warn on valid API key", () => {
|
|
39
|
+
const spy = vi.spyOn(console, "warn").mockImplementation(() => {});
|
|
40
|
+
configure({ apiKey: "rt_live_valid123" });
|
|
41
|
+
expect(spy).not.toHaveBeenCalled();
|
|
42
|
+
spy.mockRestore();
|
|
43
|
+
});
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
describe("Trace model", () => {
|
|
47
|
+
it("SpanBuilder creates valid span data", async () => {
|
|
48
|
+
const { SpanBuilder, SpanType } = await import("./trace.js");
|
|
49
|
+
const sb = new SpanBuilder("test-span", SpanType.LLM_CALL).start();
|
|
50
|
+
sb.setModel("gpt-4o");
|
|
51
|
+
sb.setInput({ prompt: "hi" });
|
|
52
|
+
const data = sb.end("hello");
|
|
53
|
+
expect(data.name).toBe("test-span");
|
|
54
|
+
expect(data.span_type).toBe("llm_call");
|
|
55
|
+
expect(data.model).toBe("gpt-4o");
|
|
56
|
+
expect(data.output).toBe("hello");
|
|
57
|
+
expect(data.id).toHaveLength(36);
|
|
58
|
+
expect(data.started_at).toBeDefined();
|
|
59
|
+
expect(data.ended_at).toBeDefined();
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
it("TraceBuilder creates valid trace data", async () => {
|
|
63
|
+
const { TraceBuilder, TraceStatus } = await import("./trace.js");
|
|
64
|
+
const tb = new TraceBuilder();
|
|
65
|
+
tb.start("my-trace", { prompt: "test" });
|
|
66
|
+
const data = tb.end("result", TraceStatus.COMPLETED);
|
|
67
|
+
expect(data.name).toBe("my-trace");
|
|
68
|
+
expect(data.status).toBe("completed");
|
|
69
|
+
expect(data.output).toBe("result");
|
|
70
|
+
expect(data.id).toHaveLength(36);
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
it("SpanType enum values", async () => {
|
|
74
|
+
const { SpanType } = await import("./trace.js");
|
|
75
|
+
expect(SpanType.LLM_CALL).toBe("llm_call");
|
|
76
|
+
expect(SpanType.TOOL_CALL).toBe("tool_call");
|
|
77
|
+
expect(SpanType.ERROR).toBe("error");
|
|
78
|
+
expect(SpanType.FORK_POINT).toBe("fork_point");
|
|
79
|
+
});
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
describe("Recorder", () => {
|
|
83
|
+
it("trace() wraps sync function", async () => {
|
|
84
|
+
configure({ apiKey: "rt_live_t", baseUrl: "http://x:1", enabled: true });
|
|
85
|
+
const { trace } = await import("./recorder.js");
|
|
86
|
+
const fn = trace((x: unknown) => `result:${x}`, { name: "sync-fn" });
|
|
87
|
+
expect(fn("hi")).toBe("result:hi");
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
it("trace() wraps async function", async () => {
|
|
91
|
+
configure({ apiKey: "rt_live_t", baseUrl: "http://x:1", enabled: true });
|
|
92
|
+
const { trace } = await import("./recorder.js");
|
|
93
|
+
const fn = trace(async (x: unknown) => `async:${x}`, { name: "async-fn" });
|
|
94
|
+
const result = await fn("hi");
|
|
95
|
+
expect(result).toBe("async:hi");
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
it("trace() propagates exceptions", async () => {
|
|
99
|
+
configure({ apiKey: "rt_live_t", baseUrl: "http://x:1", enabled: true });
|
|
100
|
+
const { trace } = await import("./recorder.js");
|
|
101
|
+
const fn = trace(() => { throw new Error("boom"); }, { name: "err-fn" });
|
|
102
|
+
expect(() => fn()).toThrow("boom");
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
it("disabled SDK is no-op", async () => {
|
|
106
|
+
configure({ apiKey: "rt_live_t", enabled: false });
|
|
107
|
+
const { trace } = await import("./recorder.js");
|
|
108
|
+
const fn = trace((x: unknown) => `r:${x}`, { name: "off" });
|
|
109
|
+
expect(fn("hi")).toBe("r:hi");
|
|
110
|
+
});
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
describe("Transport", () => {
|
|
114
|
+
it("HTTPTransport accumulates and flushes", async () => {
|
|
115
|
+
configure({ apiKey: "rt_live_t", baseUrl: "http://x:1" });
|
|
116
|
+
const { HTTPTransport } = await import("./transport.js");
|
|
117
|
+
const http = new HTTPTransport();
|
|
118
|
+
|
|
119
|
+
const mockFetch = vi.fn().mockResolvedValue({ ok: true });
|
|
120
|
+
vi.stubGlobal("fetch", mockFetch);
|
|
121
|
+
|
|
122
|
+
http.send("trace_started", { id: "abc", name: "t", status: "running", started_at: "2026-01-01T00:00:00Z", total_tokens: 0, total_cost: 0, total_duration_ms: 0 });
|
|
123
|
+
http.send("span_started", { id: "s1", trace_id: "abc", span_type: "llm_call", name: "s", started_at: "2026-01-01T00:00:00Z" });
|
|
124
|
+
http.send("span_ended", { id: "s1", ended_at: "2026-01-01T00:00:01Z", output: "hi" });
|
|
125
|
+
http.send("trace_ended", { id: "abc", ended_at: "2026-01-01T00:00:02Z", status: "completed" });
|
|
126
|
+
|
|
127
|
+
expect(mockFetch).toHaveBeenCalled();
|
|
128
|
+
const body = JSON.parse(mockFetch.mock.calls[0][1].body);
|
|
129
|
+
expect(body.id).toBe("abc");
|
|
130
|
+
expect(body.spans).toHaveLength(1);
|
|
131
|
+
expect(body.spans[0].output).toBe("hi");
|
|
132
|
+
|
|
133
|
+
vi.unstubAllGlobals();
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
it("createTransport returns correct type", async () => {
|
|
137
|
+
const { createTransport, HTTPTransport } = await import("./transport.js");
|
|
138
|
+
expect(createTransport("http")).toBeInstanceOf(HTTPTransport);
|
|
139
|
+
});
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
describe("README examples", () => {
|
|
143
|
+
it("TypeScript README example works", async () => {
|
|
144
|
+
configure({ apiKey: "rt_live_test", enabled: false });
|
|
145
|
+
const { trace } = await import("./recorder.js");
|
|
146
|
+
const runAgent = trace(async (prompt: unknown) => {
|
|
147
|
+
return `Response to: ${prompt}`;
|
|
148
|
+
}, { name: "my-agent" });
|
|
149
|
+
const result = await runAgent("What is quantum computing?");
|
|
150
|
+
expect(result).toContain("quantum");
|
|
151
|
+
});
|
|
152
|
+
});
|