@predicatelabs/sdk 0.99.9
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/LICENSE +24 -0
- package/README.md +252 -0
- package/dist/actions.d.ts +185 -0
- package/dist/actions.d.ts.map +1 -0
- package/dist/actions.js +1120 -0
- package/dist/actions.js.map +1 -0
- package/dist/agent-runtime.d.ts +352 -0
- package/dist/agent-runtime.d.ts.map +1 -0
- package/dist/agent-runtime.js +1170 -0
- package/dist/agent-runtime.js.map +1 -0
- package/dist/agent.d.ts +164 -0
- package/dist/agent.d.ts.map +1 -0
- package/dist/agent.js +408 -0
- package/dist/agent.js.map +1 -0
- package/dist/asserts/expect.d.ts +159 -0
- package/dist/asserts/expect.d.ts.map +1 -0
- package/dist/asserts/expect.js +547 -0
- package/dist/asserts/expect.js.map +1 -0
- package/dist/asserts/index.d.ts +58 -0
- package/dist/asserts/index.d.ts.map +1 -0
- package/dist/asserts/index.js +70 -0
- package/dist/asserts/index.js.map +1 -0
- package/dist/asserts/query.d.ts +199 -0
- package/dist/asserts/query.d.ts.map +1 -0
- package/dist/asserts/query.js +288 -0
- package/dist/asserts/query.js.map +1 -0
- package/dist/backends/actions.d.ts +119 -0
- package/dist/backends/actions.d.ts.map +1 -0
- package/dist/backends/actions.js +291 -0
- package/dist/backends/actions.js.map +1 -0
- package/dist/backends/browser-use-adapter.d.ts +131 -0
- package/dist/backends/browser-use-adapter.d.ts.map +1 -0
- package/dist/backends/browser-use-adapter.js +219 -0
- package/dist/backends/browser-use-adapter.js.map +1 -0
- package/dist/backends/cdp-backend.d.ts +66 -0
- package/dist/backends/cdp-backend.d.ts.map +1 -0
- package/dist/backends/cdp-backend.js +273 -0
- package/dist/backends/cdp-backend.js.map +1 -0
- package/dist/backends/index.d.ts +80 -0
- package/dist/backends/index.d.ts.map +1 -0
- package/dist/backends/index.js +101 -0
- package/dist/backends/index.js.map +1 -0
- package/dist/backends/protocol.d.ts +156 -0
- package/dist/backends/protocol.d.ts.map +1 -0
- package/dist/backends/protocol.js +16 -0
- package/dist/backends/protocol.js.map +1 -0
- package/dist/backends/sentience-context.d.ts +143 -0
- package/dist/backends/sentience-context.d.ts.map +1 -0
- package/dist/backends/sentience-context.js +359 -0
- package/dist/backends/sentience-context.js.map +1 -0
- package/dist/backends/snapshot.d.ts +188 -0
- package/dist/backends/snapshot.d.ts.map +1 -0
- package/dist/backends/snapshot.js +360 -0
- package/dist/backends/snapshot.js.map +1 -0
- package/dist/browser.d.ts +154 -0
- package/dist/browser.d.ts.map +1 -0
- package/dist/browser.js +920 -0
- package/dist/browser.js.map +1 -0
- package/dist/canonicalization.d.ts +126 -0
- package/dist/canonicalization.d.ts.map +1 -0
- package/dist/canonicalization.js +161 -0
- package/dist/canonicalization.js.map +1 -0
- package/dist/captcha/strategies.d.ts +12 -0
- package/dist/captcha/strategies.d.ts.map +1 -0
- package/dist/captcha/strategies.js +43 -0
- package/dist/captcha/strategies.js.map +1 -0
- package/dist/captcha/types.d.ts +45 -0
- package/dist/captcha/types.d.ts.map +1 -0
- package/dist/captcha/types.js +12 -0
- package/dist/captcha/types.js.map +1 -0
- package/dist/cli.d.ts +5 -0
- package/dist/cli.d.ts.map +1 -0
- package/dist/cli.js +422 -0
- package/dist/cli.js.map +1 -0
- package/dist/conversational-agent.d.ts +123 -0
- package/dist/conversational-agent.d.ts.map +1 -0
- package/dist/conversational-agent.js +341 -0
- package/dist/conversational-agent.js.map +1 -0
- package/dist/cursor-policy.d.ts +41 -0
- package/dist/cursor-policy.d.ts.map +1 -0
- package/dist/cursor-policy.js +81 -0
- package/dist/cursor-policy.js.map +1 -0
- package/dist/debugger.d.ts +28 -0
- package/dist/debugger.d.ts.map +1 -0
- package/dist/debugger.js +107 -0
- package/dist/debugger.js.map +1 -0
- package/dist/expect.d.ts +16 -0
- package/dist/expect.d.ts.map +1 -0
- package/dist/expect.js +67 -0
- package/dist/expect.js.map +1 -0
- package/dist/failure-artifacts.d.ts +95 -0
- package/dist/failure-artifacts.d.ts.map +1 -0
- package/dist/failure-artifacts.js +805 -0
- package/dist/failure-artifacts.js.map +1 -0
- package/dist/generator.d.ts +16 -0
- package/dist/generator.d.ts.map +1 -0
- package/dist/generator.js +205 -0
- package/dist/generator.js.map +1 -0
- package/dist/index.d.ts +37 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +160 -0
- package/dist/index.js.map +1 -0
- package/dist/inspector.d.ts +13 -0
- package/dist/inspector.d.ts.map +1 -0
- package/dist/inspector.js +153 -0
- package/dist/inspector.js.map +1 -0
- package/dist/llm-provider.d.ts +144 -0
- package/dist/llm-provider.d.ts.map +1 -0
- package/dist/llm-provider.js +460 -0
- package/dist/llm-provider.js.map +1 -0
- package/dist/ordinal.d.ts +90 -0
- package/dist/ordinal.d.ts.map +1 -0
- package/dist/ordinal.js +249 -0
- package/dist/ordinal.js.map +1 -0
- package/dist/overlay.d.ts +63 -0
- package/dist/overlay.d.ts.map +1 -0
- package/dist/overlay.js +102 -0
- package/dist/overlay.js.map +1 -0
- package/dist/protocols/browser-protocol.d.ts +79 -0
- package/dist/protocols/browser-protocol.d.ts.map +1 -0
- package/dist/protocols/browser-protocol.js +9 -0
- package/dist/protocols/browser-protocol.js.map +1 -0
- package/dist/query.d.ts +66 -0
- package/dist/query.d.ts.map +1 -0
- package/dist/query.js +482 -0
- package/dist/query.js.map +1 -0
- package/dist/read.d.ts +47 -0
- package/dist/read.d.ts.map +1 -0
- package/dist/read.js +128 -0
- package/dist/read.js.map +1 -0
- package/dist/recorder.d.ts +44 -0
- package/dist/recorder.d.ts.map +1 -0
- package/dist/recorder.js +262 -0
- package/dist/recorder.js.map +1 -0
- package/dist/runtime-agent.d.ts +72 -0
- package/dist/runtime-agent.d.ts.map +1 -0
- package/dist/runtime-agent.js +357 -0
- package/dist/runtime-agent.js.map +1 -0
- package/dist/screenshot.d.ts +17 -0
- package/dist/screenshot.d.ts.map +1 -0
- package/dist/screenshot.js +40 -0
- package/dist/screenshot.js.map +1 -0
- package/dist/snapshot-diff.d.ts +23 -0
- package/dist/snapshot-diff.d.ts.map +1 -0
- package/dist/snapshot-diff.js +119 -0
- package/dist/snapshot-diff.js.map +1 -0
- package/dist/snapshot.d.ts +47 -0
- package/dist/snapshot.d.ts.map +1 -0
- package/dist/snapshot.js +358 -0
- package/dist/snapshot.js.map +1 -0
- package/dist/textSearch.d.ts +64 -0
- package/dist/textSearch.d.ts.map +1 -0
- package/dist/textSearch.js +113 -0
- package/dist/textSearch.js.map +1 -0
- package/dist/tools/context.d.ts +18 -0
- package/dist/tools/context.d.ts.map +1 -0
- package/dist/tools/context.js +40 -0
- package/dist/tools/context.js.map +1 -0
- package/dist/tools/defaults.d.ts +5 -0
- package/dist/tools/defaults.d.ts.map +1 -0
- package/dist/tools/defaults.js +368 -0
- package/dist/tools/defaults.js.map +1 -0
- package/dist/tools/filesystem.d.ts +12 -0
- package/dist/tools/filesystem.d.ts.map +1 -0
- package/dist/tools/filesystem.js +137 -0
- package/dist/tools/filesystem.js.map +1 -0
- package/dist/tools/index.d.ts +5 -0
- package/dist/tools/index.d.ts.map +1 -0
- package/dist/tools/index.js +15 -0
- package/dist/tools/index.js.map +1 -0
- package/dist/tools/registry.d.ts +38 -0
- package/dist/tools/registry.d.ts.map +1 -0
- package/dist/tools/registry.js +100 -0
- package/dist/tools/registry.js.map +1 -0
- package/dist/tracing/cloud-sink.d.ts +189 -0
- package/dist/tracing/cloud-sink.d.ts.map +1 -0
- package/dist/tracing/cloud-sink.js +1067 -0
- package/dist/tracing/cloud-sink.js.map +1 -0
- package/dist/tracing/index-schema.d.ts +231 -0
- package/dist/tracing/index-schema.d.ts.map +1 -0
- package/dist/tracing/index-schema.js +235 -0
- package/dist/tracing/index-schema.js.map +1 -0
- package/dist/tracing/index.d.ts +12 -0
- package/dist/tracing/index.d.ts.map +1 -0
- package/dist/tracing/index.js +28 -0
- package/dist/tracing/index.js.map +1 -0
- package/dist/tracing/indexer.d.ts +20 -0
- package/dist/tracing/indexer.d.ts.map +1 -0
- package/dist/tracing/indexer.js +347 -0
- package/dist/tracing/indexer.js.map +1 -0
- package/dist/tracing/jsonl-sink.d.ts +51 -0
- package/dist/tracing/jsonl-sink.d.ts.map +1 -0
- package/dist/tracing/jsonl-sink.js +329 -0
- package/dist/tracing/jsonl-sink.js.map +1 -0
- package/dist/tracing/sink.d.ts +25 -0
- package/dist/tracing/sink.d.ts.map +1 -0
- package/dist/tracing/sink.js +15 -0
- package/dist/tracing/sink.js.map +1 -0
- package/dist/tracing/tracer-factory.d.ts +102 -0
- package/dist/tracing/tracer-factory.d.ts.map +1 -0
- package/dist/tracing/tracer-factory.js +375 -0
- package/dist/tracing/tracer-factory.js.map +1 -0
- package/dist/tracing/tracer.d.ts +140 -0
- package/dist/tracing/tracer.d.ts.map +1 -0
- package/dist/tracing/tracer.js +336 -0
- package/dist/tracing/tracer.js.map +1 -0
- package/dist/tracing/types.d.ts +203 -0
- package/dist/tracing/types.d.ts.map +1 -0
- package/dist/tracing/types.js +8 -0
- package/dist/tracing/types.js.map +1 -0
- package/dist/types.d.ts +422 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +6 -0
- package/dist/types.js.map +1 -0
- package/dist/utils/action-executor.d.ts +25 -0
- package/dist/utils/action-executor.d.ts.map +1 -0
- package/dist/utils/action-executor.js +121 -0
- package/dist/utils/action-executor.js.map +1 -0
- package/dist/utils/browser-evaluator.d.ts +76 -0
- package/dist/utils/browser-evaluator.d.ts.map +1 -0
- package/dist/utils/browser-evaluator.js +130 -0
- package/dist/utils/browser-evaluator.js.map +1 -0
- package/dist/utils/browser.d.ts +30 -0
- package/dist/utils/browser.d.ts.map +1 -0
- package/dist/utils/browser.js +75 -0
- package/dist/utils/browser.js.map +1 -0
- package/dist/utils/element-filter.d.ts +76 -0
- package/dist/utils/element-filter.d.ts.map +1 -0
- package/dist/utils/element-filter.js +195 -0
- package/dist/utils/element-filter.js.map +1 -0
- package/dist/utils/grid-utils.d.ts +37 -0
- package/dist/utils/grid-utils.d.ts.map +1 -0
- package/dist/utils/grid-utils.js +283 -0
- package/dist/utils/grid-utils.js.map +1 -0
- package/dist/utils/llm-interaction-handler.d.ts +41 -0
- package/dist/utils/llm-interaction-handler.d.ts.map +1 -0
- package/dist/utils/llm-interaction-handler.js +171 -0
- package/dist/utils/llm-interaction-handler.js.map +1 -0
- package/dist/utils/llm-response-builder.d.ts +56 -0
- package/dist/utils/llm-response-builder.d.ts.map +1 -0
- package/dist/utils/llm-response-builder.js +130 -0
- package/dist/utils/llm-response-builder.js.map +1 -0
- package/dist/utils/selector-utils.d.ts +12 -0
- package/dist/utils/selector-utils.d.ts.map +1 -0
- package/dist/utils/selector-utils.js +32 -0
- package/dist/utils/selector-utils.js.map +1 -0
- package/dist/utils/snapshot-event-builder.d.ts +28 -0
- package/dist/utils/snapshot-event-builder.d.ts.map +1 -0
- package/dist/utils/snapshot-event-builder.js +88 -0
- package/dist/utils/snapshot-event-builder.js.map +1 -0
- package/dist/utils/snapshot-processor.d.ts +27 -0
- package/dist/utils/snapshot-processor.d.ts.map +1 -0
- package/dist/utils/snapshot-processor.js +47 -0
- package/dist/utils/snapshot-processor.js.map +1 -0
- package/dist/utils/trace-event-builder.d.ts +122 -0
- package/dist/utils/trace-event-builder.d.ts.map +1 -0
- package/dist/utils/trace-event-builder.js +365 -0
- package/dist/utils/trace-event-builder.js.map +1 -0
- package/dist/utils/trace-file-manager.d.ts +70 -0
- package/dist/utils/trace-file-manager.d.ts.map +1 -0
- package/dist/utils/trace-file-manager.js +194 -0
- package/dist/utils/trace-file-manager.js.map +1 -0
- package/dist/utils/zod.d.ts +5 -0
- package/dist/utils/zod.d.ts.map +1 -0
- package/dist/utils/zod.js +80 -0
- package/dist/utils/zod.js.map +1 -0
- package/dist/utils.d.ts +8 -0
- package/dist/utils.d.ts.map +1 -0
- package/dist/utils.js +13 -0
- package/dist/utils.js.map +1 -0
- package/dist/verification.d.ts +194 -0
- package/dist/verification.d.ts.map +1 -0
- package/dist/verification.js +530 -0
- package/dist/verification.js.map +1 -0
- package/dist/vision-executor.d.ts +18 -0
- package/dist/vision-executor.d.ts.map +1 -0
- package/dist/vision-executor.js +60 -0
- package/dist/vision-executor.js.map +1 -0
- package/dist/visual-agent.d.ts +120 -0
- package/dist/visual-agent.d.ts.map +1 -0
- package/dist/visual-agent.js +796 -0
- package/dist/visual-agent.js.map +1 -0
- package/dist/wait.d.ts +35 -0
- package/dist/wait.d.ts.map +1 -0
- package/dist/wait.js +76 -0
- package/dist/wait.js.map +1 -0
- package/package.json +94 -0
- package/spec/README.md +72 -0
- package/spec/SNAPSHOT_V1.md +208 -0
- package/spec/sdk-types.md +259 -0
- package/spec/snapshot.schema.json +148 -0
- package/src/extension/background.js +104 -0
- package/src/extension/content.js +162 -0
- package/src/extension/injected_api.js +1399 -0
- package/src/extension/manifest.json +36 -0
- package/src/extension/pkg/README.md +1340 -0
- package/src/extension/pkg/package.json +15 -0
- package/src/extension/pkg/sentience_core.d.ts +51 -0
- package/src/extension/pkg/sentience_core.js +371 -0
- package/src/extension/pkg/sentience_core_bg.wasm +0 -0
- package/src/extension/pkg/sentience_core_bg.wasm.d.ts +10 -0
- package/src/extension/release.json +116 -0
|
@@ -0,0 +1,375 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Tracer Factory with Automatic Tier Detection
|
|
4
|
+
*
|
|
5
|
+
* Provides convenient factory function for creating tracers with cloud upload support
|
|
6
|
+
*
|
|
7
|
+
* PRODUCTION HARDENING:
|
|
8
|
+
* - Recovers orphaned traces from previous crashes on SDK init (Risk #3)
|
|
9
|
+
* - Passes runId to CloudTraceSink for persistent cache naming (Risk #1)
|
|
10
|
+
*/
|
|
11
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
12
|
+
if (k2 === undefined) k2 = k;
|
|
13
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
14
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
15
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
16
|
+
}
|
|
17
|
+
Object.defineProperty(o, k2, desc);
|
|
18
|
+
}) : (function(o, m, k, k2) {
|
|
19
|
+
if (k2 === undefined) k2 = k;
|
|
20
|
+
o[k2] = m[k];
|
|
21
|
+
}));
|
|
22
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
23
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
24
|
+
}) : function(o, v) {
|
|
25
|
+
o["default"] = v;
|
|
26
|
+
});
|
|
27
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
28
|
+
var ownKeys = function(o) {
|
|
29
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
30
|
+
var ar = [];
|
|
31
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
32
|
+
return ar;
|
|
33
|
+
};
|
|
34
|
+
return ownKeys(o);
|
|
35
|
+
};
|
|
36
|
+
return function (mod) {
|
|
37
|
+
if (mod && mod.__esModule) return mod;
|
|
38
|
+
var result = {};
|
|
39
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
40
|
+
__setModuleDefault(result, mod);
|
|
41
|
+
return result;
|
|
42
|
+
};
|
|
43
|
+
})();
|
|
44
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
45
|
+
exports.SENTIENCE_API_URL = void 0;
|
|
46
|
+
exports.createTracer = createTracer;
|
|
47
|
+
exports.createLocalTracer = createLocalTracer;
|
|
48
|
+
const path = __importStar(require("path"));
|
|
49
|
+
const fs = __importStar(require("fs"));
|
|
50
|
+
const os = __importStar(require("os"));
|
|
51
|
+
const https = __importStar(require("https"));
|
|
52
|
+
const http = __importStar(require("http"));
|
|
53
|
+
const url_1 = require("url");
|
|
54
|
+
const crypto_1 = require("crypto");
|
|
55
|
+
const tracer_1 = require("./tracer");
|
|
56
|
+
const cloud_sink_1 = require("./cloud-sink");
|
|
57
|
+
const jsonl_sink_1 = require("./jsonl-sink");
|
|
58
|
+
/**
|
|
59
|
+
* Helper to emit run_start event with available metadata
|
|
60
|
+
*/
|
|
61
|
+
function emitRunStart(tracer, agentType, llmModel, goal, startUrl) {
|
|
62
|
+
try {
|
|
63
|
+
const config = {};
|
|
64
|
+
if (goal) {
|
|
65
|
+
config.goal = goal;
|
|
66
|
+
}
|
|
67
|
+
if (startUrl) {
|
|
68
|
+
config.start_url = startUrl;
|
|
69
|
+
}
|
|
70
|
+
tracer.emitRunStart(agentType || 'SentienceAgent', llmModel, Object.keys(config).length > 0 ? config : undefined);
|
|
71
|
+
}
|
|
72
|
+
catch {
|
|
73
|
+
// Tracing must be non-fatal
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Sentience API base URL (constant)
|
|
78
|
+
*/
|
|
79
|
+
exports.SENTIENCE_API_URL = 'https://api.sentienceapi.com';
|
|
80
|
+
/**
|
|
81
|
+
* Get persistent cache directory for traces
|
|
82
|
+
*/
|
|
83
|
+
function getPersistentCacheDir() {
|
|
84
|
+
const homeDir = os.homedir();
|
|
85
|
+
return path.join(homeDir, '.sentience', 'traces', 'pending');
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Recover orphaned traces from previous crashes
|
|
89
|
+
* PRODUCTION FIX: Risk #3 - Upload traces from crashed sessions
|
|
90
|
+
*
|
|
91
|
+
* Note: Silently skips in test environments to avoid test noise
|
|
92
|
+
*/
|
|
93
|
+
async function recoverOrphanedTraces(apiKey, apiUrl = exports.SENTIENCE_API_URL) {
|
|
94
|
+
// Skip orphan recovery in test environments (CI, Jest, etc.)
|
|
95
|
+
// This prevents test failures from orphan recovery attempts
|
|
96
|
+
const isTestEnv = process.env.CI === 'true' ||
|
|
97
|
+
process.env.NODE_ENV === 'test' ||
|
|
98
|
+
process.env.JEST_WORKER_ID !== undefined ||
|
|
99
|
+
(typeof global !== 'undefined' && global.__JEST__);
|
|
100
|
+
if (isTestEnv) {
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
const cacheDir = getPersistentCacheDir();
|
|
104
|
+
if (!fs.existsSync(cacheDir)) {
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
let orphanedFiles;
|
|
108
|
+
try {
|
|
109
|
+
orphanedFiles = fs.readdirSync(cacheDir).filter(f => f.endsWith('.jsonl'));
|
|
110
|
+
}
|
|
111
|
+
catch (error) {
|
|
112
|
+
// Silently fail if directory read fails (permissions, etc.)
|
|
113
|
+
return;
|
|
114
|
+
}
|
|
115
|
+
if (orphanedFiles.length === 0) {
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
console.log(`⚠️ [Sentience] Found ${orphanedFiles.length} un-uploaded trace(s) from previous run(s)`);
|
|
119
|
+
console.log(' Attempting to upload now...');
|
|
120
|
+
for (const file of orphanedFiles) {
|
|
121
|
+
const filePath = path.join(cacheDir, file);
|
|
122
|
+
const runId = path.basename(file, '.jsonl');
|
|
123
|
+
try {
|
|
124
|
+
// Request upload URL for this orphaned trace
|
|
125
|
+
// Use a shorter timeout for orphan recovery to avoid blocking
|
|
126
|
+
const response = await Promise.race([
|
|
127
|
+
httpPost(`${apiUrl}/v1/traces/init`, { run_id: runId }, { Authorization: `Bearer ${apiKey}` }),
|
|
128
|
+
new Promise(resolve => setTimeout(() => resolve({ status: 500, data: {} }), 5000)),
|
|
129
|
+
]);
|
|
130
|
+
if (response.status === 200 && response.data.upload_url) {
|
|
131
|
+
// Create a temporary CloudTraceSink to upload this orphaned trace
|
|
132
|
+
const sink = new cloud_sink_1.CloudTraceSink(response.data.upload_url, runId);
|
|
133
|
+
await sink.close(); // This will upload the existing file
|
|
134
|
+
console.log(`✅ [Sentience] Uploaded orphaned trace: ${runId}`);
|
|
135
|
+
}
|
|
136
|
+
else if (response.status === 409) {
|
|
137
|
+
// HTTP 409 means trace already exists (already uploaded)
|
|
138
|
+
// Treat as success and delete local file
|
|
139
|
+
console.log(`✅ [Sentience] Trace ${runId} already exists in cloud (skipping re-upload)`);
|
|
140
|
+
try {
|
|
141
|
+
fs.unlinkSync(filePath);
|
|
142
|
+
}
|
|
143
|
+
catch {
|
|
144
|
+
// Ignore cleanup errors
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
// Silently skip other failures - don't log errors for orphan recovery
|
|
148
|
+
// These are expected in many scenarios (network issues, invalid API keys, etc.)
|
|
149
|
+
}
|
|
150
|
+
catch (error) {
|
|
151
|
+
// Silently skip failures - don't log errors for orphan recovery
|
|
152
|
+
// These are expected in many scenarios (network issues, invalid API keys, etc.)
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* Make HTTP/HTTPS POST request using built-in Node modules
|
|
158
|
+
*/
|
|
159
|
+
function httpPost(url, data, headers) {
|
|
160
|
+
return new Promise((resolve, reject) => {
|
|
161
|
+
const urlObj = new url_1.URL(url);
|
|
162
|
+
const protocol = urlObj.protocol === 'https:' ? https : http;
|
|
163
|
+
const body = JSON.stringify(data);
|
|
164
|
+
const options = {
|
|
165
|
+
hostname: urlObj.hostname,
|
|
166
|
+
port: urlObj.port || (urlObj.protocol === 'https:' ? 443 : 80),
|
|
167
|
+
path: urlObj.pathname + urlObj.search,
|
|
168
|
+
method: 'POST',
|
|
169
|
+
headers: {
|
|
170
|
+
'Content-Type': 'application/json',
|
|
171
|
+
'Content-Length': Buffer.byteLength(body),
|
|
172
|
+
...headers,
|
|
173
|
+
},
|
|
174
|
+
timeout: 10000, // 10 second timeout
|
|
175
|
+
};
|
|
176
|
+
const req = protocol.request(options, res => {
|
|
177
|
+
let responseBody = '';
|
|
178
|
+
res.on('data', chunk => {
|
|
179
|
+
responseBody += chunk;
|
|
180
|
+
});
|
|
181
|
+
res.on('end', () => {
|
|
182
|
+
try {
|
|
183
|
+
const parsed = responseBody ? JSON.parse(responseBody) : {};
|
|
184
|
+
resolve({ status: res.statusCode || 500, data: parsed });
|
|
185
|
+
}
|
|
186
|
+
catch (error) {
|
|
187
|
+
resolve({ status: res.statusCode || 500, data: {} });
|
|
188
|
+
}
|
|
189
|
+
});
|
|
190
|
+
});
|
|
191
|
+
req.on('error', error => {
|
|
192
|
+
reject(error);
|
|
193
|
+
});
|
|
194
|
+
req.on('timeout', () => {
|
|
195
|
+
req.destroy();
|
|
196
|
+
reject(new Error('Request timeout'));
|
|
197
|
+
});
|
|
198
|
+
req.write(body);
|
|
199
|
+
req.end();
|
|
200
|
+
});
|
|
201
|
+
}
|
|
202
|
+
/**
|
|
203
|
+
* Create tracer with automatic tier detection
|
|
204
|
+
*
|
|
205
|
+
* Tier Detection:
|
|
206
|
+
* - If apiKey is provided AND uploadTrace is true: Try to initialize CloudTraceSink (Pro/Enterprise)
|
|
207
|
+
* - If cloud init fails, no apiKey, or uploadTrace is false: Fall back to JsonlTraceSink (Free tier)
|
|
208
|
+
*
|
|
209
|
+
* @param options - Configuration options
|
|
210
|
+
* @param options.apiKey - Sentience API key (e.g., "sk_pro_xxxxx")
|
|
211
|
+
* @param options.runId - Unique identifier for this agent run (generates UUID if not provided)
|
|
212
|
+
* @param options.apiUrl - Sentience API base URL (default: https://api.sentienceapi.com)
|
|
213
|
+
* @param options.logger - Optional logger instance for logging file sizes and errors
|
|
214
|
+
* @param options.uploadTrace - Enable cloud trace upload (default: true for backward compatibility)
|
|
215
|
+
* @param options.goal - User's goal/objective for this trace run. This will be displayed as the trace name in the frontend. Should be descriptive and action-oriented. Example: "Add wireless headphones to cart on Amazon"
|
|
216
|
+
* @param options.agentType - Type of agent running (e.g., "SentienceAgent", "CustomAgent")
|
|
217
|
+
* @param options.llmModel - LLM model used (e.g., "gpt-4-turbo", "claude-3-5-sonnet")
|
|
218
|
+
* @param options.startUrl - Starting URL of the agent run (e.g., "https://amazon.com")
|
|
219
|
+
* @param options.screenshotProcessor - Optional function to process screenshots before upload. Takes base64 string, returns processed base64 string. Useful for PII redaction or custom image processing.
|
|
220
|
+
* @param options.autoEmitRunStart - If true (default), automatically emit run_start event with provided metadata. This ensures traces have complete structure for Studio visualization.
|
|
221
|
+
* @returns Tracer configured with appropriate sink
|
|
222
|
+
*
|
|
223
|
+
* @example
|
|
224
|
+
* ```typescript
|
|
225
|
+
* // Pro tier user with goal and metadata
|
|
226
|
+
* const tracer = await createTracer({
|
|
227
|
+
* apiKey: "sk_pro_xyz",
|
|
228
|
+
* runId: "demo",
|
|
229
|
+
* goal: "Add headphones to cart",
|
|
230
|
+
* agentType: "SentienceAgent",
|
|
231
|
+
* llmModel: "gpt-4-turbo",
|
|
232
|
+
* startUrl: "https://amazon.com",
|
|
233
|
+
* uploadTrace: true
|
|
234
|
+
* });
|
|
235
|
+
* // Returns: Tracer with CloudTraceSink
|
|
236
|
+
* // run_start event is automatically emitted
|
|
237
|
+
*
|
|
238
|
+
* // With screenshot processor for PII redaction
|
|
239
|
+
* const redactPII = (screenshot: string): string => {
|
|
240
|
+
* // Your custom redaction logic
|
|
241
|
+
* return redactedScreenshot;
|
|
242
|
+
* };
|
|
243
|
+
* const tracer = await createTracer({
|
|
244
|
+
* apiKey: "sk_pro_xyz",
|
|
245
|
+
* screenshotProcessor: redactPII
|
|
246
|
+
* });
|
|
247
|
+
* // Screenshots will be processed before upload
|
|
248
|
+
*
|
|
249
|
+
* // Pro tier user with local-only tracing
|
|
250
|
+
* const tracer = await createTracer({ apiKey: "sk_pro_xyz", runId: "demo", uploadTrace: false });
|
|
251
|
+
* // Returns: Tracer with JsonlTraceSink (local-only)
|
|
252
|
+
*
|
|
253
|
+
* // Disable auto-emit for manual control
|
|
254
|
+
* const tracer = await createTracer({ runId: "demo", autoEmitRunStart: false });
|
|
255
|
+
* tracer.emitRunStart("MyAgent", "gpt-4o"); // Manual emit
|
|
256
|
+
*
|
|
257
|
+
* // Free tier user
|
|
258
|
+
* const tracer = await createTracer({ runId: "demo" });
|
|
259
|
+
* // Returns: Tracer with JsonlTraceSink (local-only)
|
|
260
|
+
*
|
|
261
|
+
* // Use with agent
|
|
262
|
+
* const agent = new SentienceAgent(browser, llm, 50, true, tracer);
|
|
263
|
+
* await agent.act("Click search");
|
|
264
|
+
* await tracer.close(); // Uploads to cloud if uploadTrace: true and Pro tier
|
|
265
|
+
* ```
|
|
266
|
+
*/
|
|
267
|
+
async function createTracer(options) {
|
|
268
|
+
const runId = options.runId || (0, crypto_1.randomUUID)();
|
|
269
|
+
const apiUrl = options.apiUrl || exports.SENTIENCE_API_URL;
|
|
270
|
+
// Default uploadTrace to true for backward compatibility
|
|
271
|
+
const uploadTrace = options.uploadTrace !== false;
|
|
272
|
+
// PRODUCTION FIX: Recover orphaned traces from previous crashes
|
|
273
|
+
// Note: This is skipped in test environments (see recoverOrphanedTraces function)
|
|
274
|
+
// Run in background to avoid blocking tracer creation
|
|
275
|
+
// Only recover if uploadTrace is enabled
|
|
276
|
+
if (options.apiKey && uploadTrace) {
|
|
277
|
+
// Don't await - run in background to avoid blocking
|
|
278
|
+
recoverOrphanedTraces(options.apiKey, apiUrl).catch(() => {
|
|
279
|
+
// Silently fail - orphan recovery should not block tracer creation
|
|
280
|
+
});
|
|
281
|
+
}
|
|
282
|
+
// 1. Try to initialize Cloud Sink (Pro/Enterprise tier)
|
|
283
|
+
// Only attempt cloud init if uploadTrace is enabled
|
|
284
|
+
if (options.apiKey && uploadTrace) {
|
|
285
|
+
try {
|
|
286
|
+
// Build metadata object for trace initialization
|
|
287
|
+
// Only include non-empty fields to avoid sending empty strings
|
|
288
|
+
const metadata = {};
|
|
289
|
+
if (options.goal && options.goal.trim()) {
|
|
290
|
+
metadata.goal = options.goal.trim();
|
|
291
|
+
}
|
|
292
|
+
if (options.agentType && options.agentType.trim()) {
|
|
293
|
+
metadata.agent_type = options.agentType.trim();
|
|
294
|
+
}
|
|
295
|
+
if (options.llmModel && options.llmModel.trim()) {
|
|
296
|
+
metadata.llm_model = options.llmModel.trim();
|
|
297
|
+
}
|
|
298
|
+
if (options.startUrl && options.startUrl.trim()) {
|
|
299
|
+
metadata.start_url = options.startUrl.trim();
|
|
300
|
+
}
|
|
301
|
+
// Build request payload
|
|
302
|
+
const payload = { run_id: runId };
|
|
303
|
+
if (Object.keys(metadata).length > 0) {
|
|
304
|
+
payload.metadata = metadata;
|
|
305
|
+
}
|
|
306
|
+
// Request pre-signed upload URL from backend
|
|
307
|
+
const response = await httpPost(`${apiUrl}/v1/traces/init`, payload, {
|
|
308
|
+
Authorization: `Bearer ${options.apiKey}`,
|
|
309
|
+
});
|
|
310
|
+
if (response.status === 200 && response.data.upload_url) {
|
|
311
|
+
const uploadUrl = response.data.upload_url;
|
|
312
|
+
console.log('☁️ [Sentience] Cloud tracing enabled (Pro tier)');
|
|
313
|
+
// PRODUCTION FIX: Pass runId for persistent cache naming
|
|
314
|
+
const tracer = new tracer_1.Tracer(runId, new cloud_sink_1.CloudTraceSink(uploadUrl, runId, options.apiKey, apiUrl, options.logger), options.screenshotProcessor);
|
|
315
|
+
// Auto-emit run_start for complete trace structure
|
|
316
|
+
if (options.autoEmitRunStart !== false) {
|
|
317
|
+
emitRunStart(tracer, options.agentType, options.llmModel, options.goal, options.startUrl);
|
|
318
|
+
}
|
|
319
|
+
return tracer;
|
|
320
|
+
}
|
|
321
|
+
else if (response.status === 403) {
|
|
322
|
+
console.log('⚠️ [Sentience] Cloud tracing requires Pro tier');
|
|
323
|
+
console.log(' Falling back to local-only tracing');
|
|
324
|
+
}
|
|
325
|
+
else {
|
|
326
|
+
console.log(`⚠️ [Sentience] Cloud init failed: HTTP ${response.status}`);
|
|
327
|
+
console.log(' Falling back to local-only tracing');
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
catch (error) {
|
|
331
|
+
if (error.message?.includes('timeout')) {
|
|
332
|
+
console.log('⚠️ [Sentience] Cloud init timeout');
|
|
333
|
+
}
|
|
334
|
+
else if (error.code === 'ECONNREFUSED' || error.message?.includes('connect')) {
|
|
335
|
+
console.log('⚠️ [Sentience] Cloud init connection error');
|
|
336
|
+
}
|
|
337
|
+
else {
|
|
338
|
+
console.log(`⚠️ [Sentience] Cloud init error: ${error.message}`);
|
|
339
|
+
}
|
|
340
|
+
console.log(' Falling back to local-only tracing');
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
// 2. Fallback to Local Sink (Free tier / Offline mode)
|
|
344
|
+
const tracesDir = path.join(process.cwd(), 'traces');
|
|
345
|
+
// Create traces directory if it doesn't exist
|
|
346
|
+
if (!fs.existsSync(tracesDir)) {
|
|
347
|
+
fs.mkdirSync(tracesDir, { recursive: true });
|
|
348
|
+
}
|
|
349
|
+
const localPath = path.join(tracesDir, `${runId}.jsonl`);
|
|
350
|
+
console.log(`💾 [Sentience] Local tracing: ${localPath}`);
|
|
351
|
+
const tracer = new tracer_1.Tracer(runId, new jsonl_sink_1.JsonlTraceSink(localPath), options.screenshotProcessor);
|
|
352
|
+
// Auto-emit run_start for complete trace structure
|
|
353
|
+
if (options.autoEmitRunStart !== false) {
|
|
354
|
+
emitRunStart(tracer, options.agentType, options.llmModel, options.goal, options.startUrl);
|
|
355
|
+
}
|
|
356
|
+
return tracer;
|
|
357
|
+
}
|
|
358
|
+
/**
|
|
359
|
+
* Synchronous version of createTracer for non-async contexts
|
|
360
|
+
* Always returns local JsonlTraceSink (no cloud upload)
|
|
361
|
+
*
|
|
362
|
+
* @param runId - Unique identifier for this agent run (generates UUID if not provided)
|
|
363
|
+
* @returns Tracer with JsonlTraceSink
|
|
364
|
+
*/
|
|
365
|
+
function createLocalTracer(runId) {
|
|
366
|
+
const traceRunId = runId || (0, crypto_1.randomUUID)();
|
|
367
|
+
const tracesDir = path.join(process.cwd(), 'traces');
|
|
368
|
+
if (!fs.existsSync(tracesDir)) {
|
|
369
|
+
fs.mkdirSync(tracesDir, { recursive: true });
|
|
370
|
+
}
|
|
371
|
+
const localPath = path.join(tracesDir, `${traceRunId}.jsonl`);
|
|
372
|
+
console.log(`💾 [Sentience] Local tracing: ${localPath}`);
|
|
373
|
+
return new tracer_1.Tracer(traceRunId, new jsonl_sink_1.JsonlTraceSink(localPath));
|
|
374
|
+
}
|
|
375
|
+
//# sourceMappingURL=tracer-factory.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tracer-factory.js","sourceRoot":"","sources":["../../src/tracing/tracer-factory.ts"],"names":[],"mappings":";AAAA;;;;;;;;GAQG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA4QH,oCAmHC;AASD,8CAYC;AAlZD,2CAA6B;AAC7B,uCAAyB;AACzB,uCAAyB;AACzB,6CAA+B;AAC/B,2CAA6B;AAC7B,6BAA0B;AAC1B,mCAAoC;AACpC,qCAAkC;AAClC,6CAA+D;AAC/D,6CAA8C;AAE9C;;GAEG;AACH,SAAS,YAAY,CACnB,MAAc,EACd,SAAkB,EAClB,QAAiB,EACjB,IAAa,EACb,QAAiB;IAEjB,IAAI,CAAC;QACH,MAAM,MAAM,GAA2B,EAAE,CAAC;QAC1C,IAAI,IAAI,EAAE,CAAC;YACT,MAAM,CAAC,IAAI,GAAG,IAAI,CAAC;QACrB,CAAC;QACD,IAAI,QAAQ,EAAE,CAAC;YACb,MAAM,CAAC,SAAS,GAAG,QAAQ,CAAC;QAC9B,CAAC;QAED,MAAM,CAAC,YAAY,CACjB,SAAS,IAAI,gBAAgB,EAC7B,QAAQ,EACR,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,SAAS,CACpD,CAAC;IACJ,CAAC;IAAC,MAAM,CAAC;QACP,4BAA4B;IAC9B,CAAC;AACH,CAAC;AAED;;GAEG;AACU,QAAA,iBAAiB,GAAG,8BAA8B,CAAC;AAEhE;;GAEG;AACH,SAAS,qBAAqB;IAC5B,MAAM,OAAO,GAAG,EAAE,CAAC,OAAO,EAAE,CAAC;IAC7B,OAAO,IAAI,C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AAQ,CAAC,CAAC;IAC5F,CAAC;IAED,OAAO,MAAM,CAAC;AAChB,CAAC;AAED;;;;;;GAMG;AACH,SAAgB,iBAAiB,CAAC,KAAc;IAC9C,MAAM,UAAU,GAAG,KAAK,IAAI,IAAA,mBAAU,GAAE,CAAC;IACzC,MAAM,SAAS,GAAG,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,GAAG,EAAE,EAAE,QAAQ,CAAC,CAAC;IAErD,IAAI,CAAC,EAAE,CAAC,UAAU,CAAC,SAAS,CAAC,EAAE,CAAC;QAC9B,EAAE,CAAC,SAAS,CAAC,SAAS,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC,CAAC;IAC/C,CAAC;IAED,MAAM,SAAS,GAAG,IAAI,CAAC,IAAI,CAAC,SAAS,EAAE,GAAG,UAAU,QAAQ,CAAC,CAAC;IAC9D,OAAO,CAAC,GAAG,CAAC,iCAAiC,SAAS,EAAE,CAAC,CAAC;IAE1D,OAAO,IAAI,eAAM,CAAC,UAAU,EAAE,IAAI,2BAAc,CAAC,SAAS,CAAC,CAAC,CAAC;AAC/D,CAAC"}
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tracer Class
|
|
3
|
+
*
|
|
4
|
+
* High-level API for emitting trace events with automatic sequencing and timestamps
|
|
5
|
+
*/
|
|
6
|
+
import { TraceSink } from './sink';
|
|
7
|
+
import { TraceEventData } from './types';
|
|
8
|
+
/**
|
|
9
|
+
* Tracer provides a high-level API for recording agent execution traces
|
|
10
|
+
*/
|
|
11
|
+
export declare class Tracer {
|
|
12
|
+
private runId;
|
|
13
|
+
private sink;
|
|
14
|
+
private seq;
|
|
15
|
+
private screenshotProcessor?;
|
|
16
|
+
private totalSteps;
|
|
17
|
+
private totalEvents;
|
|
18
|
+
private startedAt;
|
|
19
|
+
private endedAt;
|
|
20
|
+
private finalStatus;
|
|
21
|
+
private stepSuccesses;
|
|
22
|
+
private stepFailures;
|
|
23
|
+
private hasErrors;
|
|
24
|
+
/**
|
|
25
|
+
* Create a new Tracer
|
|
26
|
+
* @param runId - Unique run identifier (UUID)
|
|
27
|
+
* @param sink - TraceSink implementation (e.g., JsonlTraceSink)
|
|
28
|
+
* @param screenshotProcessor - Optional function to process screenshots before emission.
|
|
29
|
+
* Takes base64 string, returns processed base64 string.
|
|
30
|
+
* Useful for PII redaction or custom image processing.
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* // Basic usage
|
|
34
|
+
* const sink = new JsonlTraceSink('trace.jsonl');
|
|
35
|
+
* const tracer = new Tracer('run-123', sink);
|
|
36
|
+
*
|
|
37
|
+
* @example
|
|
38
|
+
* // With screenshot processor for PII redaction
|
|
39
|
+
* const redactPII = (screenshot: string): string => {
|
|
40
|
+
* // Your custom redaction logic
|
|
41
|
+
* return redactedScreenshot;
|
|
42
|
+
* };
|
|
43
|
+
* const tracer = new Tracer('run-123', sink, redactPII);
|
|
44
|
+
*/
|
|
45
|
+
constructor(runId: string, sink: TraceSink, screenshotProcessor?: (screenshot: string) => string);
|
|
46
|
+
/**
|
|
47
|
+
* Emit a trace event
|
|
48
|
+
* @param eventType - Type of event (e.g., 'run_start', 'snapshot')
|
|
49
|
+
* @param data - Event-specific payload
|
|
50
|
+
* @param stepId - Optional step UUID
|
|
51
|
+
*/
|
|
52
|
+
emit(eventType: string, data: TraceEventData, stepId?: string): void;
|
|
53
|
+
/**
|
|
54
|
+
* Emit run_start event
|
|
55
|
+
* @param agent - Agent type (e.g., 'SentienceAgent')
|
|
56
|
+
* @param llmModel - Optional LLM model name
|
|
57
|
+
* @param config - Optional configuration
|
|
58
|
+
*/
|
|
59
|
+
emitRunStart(agent: string, llmModel?: string, config?: Record<string, any>): void;
|
|
60
|
+
/**
|
|
61
|
+
* Emit step_start event
|
|
62
|
+
* @param stepId - Step UUID
|
|
63
|
+
* @param stepIndex - Step number (1-indexed)
|
|
64
|
+
* @param goal - Goal description
|
|
65
|
+
* @param attempt - Retry attempt number (0 = first try)
|
|
66
|
+
* @param preUrl - Optional URL before step execution
|
|
67
|
+
*/
|
|
68
|
+
emitStepStart(stepId: string, stepIndex: number, goal: string, attempt?: number, preUrl?: string): void;
|
|
69
|
+
/**
|
|
70
|
+
* Emit run_end event
|
|
71
|
+
* @param steps - Total number of steps executed
|
|
72
|
+
* @param status - Optional final status ("success", "failure", "partial", "unknown")
|
|
73
|
+
* If not provided, infers from tracked outcomes or uses this.finalStatus
|
|
74
|
+
*/
|
|
75
|
+
emitRunEnd(steps: number, status?: string): void;
|
|
76
|
+
/**
|
|
77
|
+
* Emit error event
|
|
78
|
+
* @param stepId - Step UUID where error occurred
|
|
79
|
+
* @param error - Error message
|
|
80
|
+
* @param attempt - Retry attempt number
|
|
81
|
+
*/
|
|
82
|
+
emitError(stepId: string, error: string, attempt?: number): void;
|
|
83
|
+
/**
|
|
84
|
+
* Emit snapshot event with screenshot for Studio visualization.
|
|
85
|
+
*
|
|
86
|
+
* This method builds and emits a 'snapshot' trace event that includes:
|
|
87
|
+
* - Page URL and element data
|
|
88
|
+
* - Screenshot (if present in snapshot)
|
|
89
|
+
* - Step correlation info
|
|
90
|
+
*
|
|
91
|
+
* Use this when you want screenshots to appear in the Sentience Studio timeline.
|
|
92
|
+
*
|
|
93
|
+
* @param snapshot - Snapshot object (must have 'screenshot' property for images)
|
|
94
|
+
* @param stepId - Step UUID (for correlating snapshot with a step)
|
|
95
|
+
* @param stepIndex - Step index (0-based) for Studio timeline ordering
|
|
96
|
+
* @param screenshotFormat - Format of screenshot ("jpeg" or "png", default: "jpeg")
|
|
97
|
+
*
|
|
98
|
+
* @example
|
|
99
|
+
* // After taking a snapshot with AgentRuntime
|
|
100
|
+
* const snapshot = await runtime.snapshot();
|
|
101
|
+
* tracer.emitSnapshot(snapshot, runtime.getStepId(), runtime.getStepIndex());
|
|
102
|
+
*
|
|
103
|
+
* // Or use auto-emit (default in AgentRuntime.snapshot())
|
|
104
|
+
* const snapshot = await runtime.snapshot(); // Auto-emits snapshot event
|
|
105
|
+
*/
|
|
106
|
+
emitSnapshot(snapshot: any, stepId?: string, stepIndex?: number, screenshotFormat?: string): void;
|
|
107
|
+
/**
|
|
108
|
+
* Automatically infer finalStatus from tracked step outcomes if not explicitly set.
|
|
109
|
+
* This is called automatically in close() if finalStatus is still "unknown".
|
|
110
|
+
*/
|
|
111
|
+
private _inferFinalStatus;
|
|
112
|
+
/**
|
|
113
|
+
* Close the underlying sink (flush buffered data)
|
|
114
|
+
* @param blocking - If false, upload happens in background (default: true). Only applies to CloudTraceSink.
|
|
115
|
+
*/
|
|
116
|
+
close(blocking?: boolean): Promise<void>;
|
|
117
|
+
/**
|
|
118
|
+
* Get run ID
|
|
119
|
+
*/
|
|
120
|
+
getRunId(): string;
|
|
121
|
+
/**
|
|
122
|
+
* Get current sequence number
|
|
123
|
+
*/
|
|
124
|
+
getSeq(): number;
|
|
125
|
+
/**
|
|
126
|
+
* Get sink type (for debugging)
|
|
127
|
+
*/
|
|
128
|
+
getSinkType(): string;
|
|
129
|
+
/**
|
|
130
|
+
* Set the final status of the trace run
|
|
131
|
+
* @param status - Final status ("success", "failure", "partial", "unknown")
|
|
132
|
+
*/
|
|
133
|
+
setFinalStatus(status: string): void;
|
|
134
|
+
/**
|
|
135
|
+
* Get execution statistics for trace completion
|
|
136
|
+
* @returns Dictionary with stats fields for /v1/traces/complete
|
|
137
|
+
*/
|
|
138
|
+
getStats(): Record<string, any>;
|
|
139
|
+
}
|
|
140
|
+
//# sourceMappingURL=tracer.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tracer.d.ts","sourceRoot":"","sources":["../../src/tracing/tracer.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,SAAS,EAAE,MAAM,QAAQ,CAAC;AACnC,OAAO,EAAc,cAAc,EAAE,MAAM,SAAS,CAAC;AAErD;;GAEG;AACH,qBAAa,MAAM;IACjB,OAAO,CAAC,KAAK,CAAS;IACtB,OAA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