retrace-sdk 0.3.2 → 0.3.4

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.
@@ -1,5 +1,6 @@
1
1
  import { SpanType } from "../trace.js";
2
2
  import { genId, nowIso, truncateJson } from "../utils.js";
3
+ import { isReplaying, consumeCassetteEntry } from "../replay.js";
3
4
  const PRICING = {
4
5
  "claude-opus-4.7": [5.0, 25.0],
5
6
  "claude-opus-4.6": [5.0, 25.0],
@@ -50,6 +51,28 @@ function createPatchedCreate() {
50
51
  const spanId = genId();
51
52
  const startedAt = nowIso();
52
53
  const startMs = Date.now();
54
+ // During replay, return mocked response from cassette
55
+ if (isReplaying()) {
56
+ const entry = consumeCassetteEntry("anthropic.messages.create", "llm_call");
57
+ if (entry) {
58
+ const output = typeof entry.output === "string" ? entry.output : JSON.stringify(entry.output || "");
59
+ const span = {
60
+ id: spanId, trace_id: "", parent_id: null,
61
+ span_type: SpanType.LLM_CALL, name: "anthropic.messages.create", model,
62
+ input: truncateJson({ messages: messages.slice(0, 10) }),
63
+ output: truncateJson(output),
64
+ duration_ms: 0, started_at: startedAt, ended_at: nowIso(),
65
+ metadata: { replayed: true },
66
+ };
67
+ onSpanCallback?.(span);
68
+ return {
69
+ id: `msg-replay-${spanId}`, type: "message", role: "assistant", model,
70
+ content: [{ type: "text", text: output }],
71
+ usage: { input_tokens: 0, output_tokens: 0 },
72
+ stop_reason: "end_turn",
73
+ };
74
+ }
75
+ }
53
76
  try {
54
77
  const result = await originalCreate.apply(this, args);
55
78
  const durationMs = Date.now() - startMs;
@@ -40,6 +40,14 @@ export function installGeminiInterceptor(onSpan) {
40
40
  const spanId = genId();
41
41
  const startedAt = nowIso();
42
42
  const startMs = Date.now();
43
+ // Replay mode — return mocked response from cassette
44
+ const { isReplaying, consumeCassetteEntry } = await import("../replay.js");
45
+ if (isReplaying()) {
46
+ const entry = consumeCassetteEntry("retrace.ai.generate", "llm_call");
47
+ if (entry) {
48
+ return { text: entry.output || "", usageMetadata: { promptTokenCount: 0, candidatesTokenCount: 0 }, candidates: [] };
49
+ }
50
+ }
43
51
  try {
44
52
  const result = await originalGenerateContent.apply(this, args);
45
53
  const durationMs = Date.now() - startMs;
@@ -47,10 +55,11 @@ export function installGeminiInterceptor(onSpan) {
47
55
  const res = result;
48
56
  const inputTokens = res?.usageMetadata?.promptTokenCount || 0;
49
57
  const outputTokens = res?.usageMetadata?.candidatesTokenCount || 0;
58
+ const outputText = res?.text ?? (res?.candidates?.[0]?.content?.parts?.filter((p) => p.functionCall).map((p) => p.functionCall.name).join(", ") ? `[function_call: ${res.candidates[0].content.parts.filter((p) => p.functionCall).map((p) => p.functionCall.name).join(", ")}]` : "");
50
59
  const span = {
51
60
  id: spanId, trace_id: "", parent_id: null,
52
61
  span_type: SpanType.LLM_CALL, name: "retrace.ai.generate", model,
53
- input: truncateJson(contents), output: truncateJson(res?.text || ""),
62
+ input: truncateJson(contents), output: truncateJson(outputText),
54
63
  input_tokens: inputTokens, output_tokens: outputTokens,
55
64
  cost: calcCost(model, inputTokens, outputTokens),
56
65
  duration_ms: durationMs, started_at: startedAt, ended_at: nowIso(),
@@ -1,5 +1,6 @@
1
1
  import { SpanType } from "../trace.js";
2
2
  import { genId, nowIso, truncateJson } from "../utils.js";
3
+ import { isReplaying, consumeCassetteEntry } from "../replay.js";
3
4
  const PRICING = {
4
5
  "gpt-5.5-pro": [30.0, 180.0],
5
6
  "gpt-5.5": [5.0, 30.0],
@@ -66,6 +67,29 @@ function createPatchedCreate() {
66
67
  const spanId = genId();
67
68
  const startedAt = nowIso();
68
69
  const startMs = Date.now();
70
+ // During replay, return mocked response from cassette instead of calling the real API
71
+ if (isReplaying()) {
72
+ const entry = consumeCassetteEntry("openai.chat.completions.create", "llm_call");
73
+ if (entry) {
74
+ const output = typeof entry.output === "string" ? entry.output : JSON.stringify(entry.output || "");
75
+ const span = {
76
+ id: spanId, trace_id: "", parent_id: null,
77
+ span_type: SpanType.LLM_CALL, name: "openai.chat.completions.create", model,
78
+ input: truncateJson({ messages: messages.slice(0, 10) }),
79
+ output: truncateJson(output),
80
+ duration_ms: 0, started_at: startedAt, ended_at: nowIso(),
81
+ metadata: { replayed: true },
82
+ };
83
+ onSpanCallback?.(span);
84
+ return {
85
+ id: `chatcmpl-replay-${spanId}`,
86
+ object: "chat.completion",
87
+ model,
88
+ choices: [{ index: 0, message: { role: "assistant", content: output }, finish_reason: "stop" }],
89
+ usage: { prompt_tokens: 0, completion_tokens: 0, total_tokens: 0 },
90
+ };
91
+ }
92
+ }
69
93
  try {
70
94
  const result = await originalCreate.apply(this, args);
71
95
  const durationMs = Date.now() - startMs;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "retrace-sdk",
3
- "version": "0.3.2",
3
+ "version": "0.3.4",
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",