Skip to content

Commit fa2b76e

Browse files
author
SentienceDEV
committed
Merge pull request #72 from SentienceAPI/local_trace
local storage first
2 parents f4be24c + a50666a commit fa2b76e

File tree

3 files changed

+30
-17
lines changed

3 files changed

+30
-17
lines changed
Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
/**
2-
* Example: Agent with Cloud Tracing
2+
* Example: Agent with Local Tracing
33
*
4-
* Demonstrates how to use cloud tracing with SentienceAgent to upload traces
5-
* and screenshots to cloud storage for remote viewing and analysis.
4+
* Demonstrates how to use local tracing with SentienceAgent to save traces
5+
* and screenshots to local storage for offline viewing and analysis.
66
*
77
* Requirements:
88
* - Pro or Enterprise tier API key (SENTIENCE_API_KEY)
99
* - OpenAI API key (OPENAI_API_KEY) for LLM
1010
*
1111
* Usage:
12-
* ts-node examples/cloud-tracing-agent.ts
12+
* ts-node examples/local-tracing-agent.ts
1313
* or
14-
* npm run example:cloud-tracing
14+
* npm run example:local-tracing
1515
*/
1616

1717
import { SentienceBrowser } from '../src/browser';
@@ -39,12 +39,15 @@ async function main() {
3939
console.log('🚀 Starting Agent with Cloud Tracing Demo\n');
4040

4141
// 1. Create tracer with automatic tier detection
42-
// If apiKey is Pro/Enterprise, uses CloudTraceSink
43-
// If apiKey is missing/invalid, falls back to local JsonlTraceSink
44-
const runId = 'cloud-tracing-demo';
42+
// If apiKey is Pro/Enterprise AND uploadTrace is true, uses CloudTraceSink
43+
// If apiKey is missing/invalid or uploadTrace is false, falls back to local JsonlTraceSink
44+
// Note: uploadTrace defaults to true for backward compatibility
45+
const runId = 'local-tracing-demo';
46+
// if apiKey is provided and uploadTrace is true, will use cloud storage tracer
4547
const tracer = await createTracer({
4648
apiKey: sentienceKey,
47-
runId: runId
49+
runId: runId,
50+
uploadTrace: false // local storage tracer
4851
});
4952

5053
console.log(`🆔 Run ID: ${runId}\n`);

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "sentienceapi",
3-
"version": "0.90.5",
3+
"version": "0.90.7",
44
"description": "TypeScript SDK for Sentience AI Agent Browser Automation",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",

src/tracing/tracer-factory.ts

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -165,53 +165,63 @@ function httpPost(url: string, data: any, headers: Record<string, string>): Prom
165165
* Create tracer with automatic tier detection
166166
*
167167
* Tier Detection:
168-
* - If apiKey is provided: Try to initialize CloudTraceSink (Pro/Enterprise)
169-
* - If cloud init fails or no apiKey: Fall back to JsonlTraceSink (Free tier)
168+
* - If apiKey is provided AND uploadTrace is true: Try to initialize CloudTraceSink (Pro/Enterprise)
169+
* - If cloud init fails, no apiKey, or uploadTrace is false: Fall back to JsonlTraceSink (Free tier)
170170
*
171171
* @param options - Configuration options
172172
* @param options.apiKey - Sentience API key (e.g., "sk_pro_xxxxx")
173173
* @param options.runId - Unique identifier for this agent run (generates UUID if not provided)
174174
* @param options.apiUrl - Sentience API base URL (default: https://api.sentienceapi.com)
175175
* @param options.logger - Optional logger instance for logging file sizes and errors
176+
* @param options.uploadTrace - Enable cloud trace upload (default: true for backward compatibility)
176177
* @returns Tracer configured with appropriate sink
177178
*
178179
* @example
179180
* ```typescript
180-
* // Pro tier user
181-
* const tracer = await createTracer({ apiKey: "sk_pro_xyz", runId: "demo" });
181+
* // Pro tier user with cloud upload
182+
* const tracer = await createTracer({ apiKey: "sk_pro_xyz", runId: "demo", uploadTrace: true });
182183
* // Returns: Tracer with CloudTraceSink
183184
*
185+
* // Pro tier user with local-only tracing
186+
* const tracer = await createTracer({ apiKey: "sk_pro_xyz", runId: "demo", uploadTrace: false });
187+
* // Returns: Tracer with JsonlTraceSink (local-only)
188+
*
184189
* // Free tier user
185190
* const tracer = await createTracer({ runId: "demo" });
186191
* // Returns: Tracer with JsonlTraceSink (local-only)
187192
*
188193
* // Use with agent
189194
* const agent = new SentienceAgent(browser, llm, 50, true, tracer);
190195
* await agent.act("Click search");
191-
* await tracer.close(); // Uploads to cloud if Pro tier
196+
* await tracer.close(); // Uploads to cloud if uploadTrace: true and Pro tier
192197
* ```
193198
*/
194199
export async function createTracer(options: {
195200
apiKey?: string;
196201
runId?: string;
197202
apiUrl?: string;
198203
logger?: SentienceLogger;
204+
uploadTrace?: boolean;
199205
}): Promise<Tracer> {
200206
const runId = options.runId || randomUUID();
201207
const apiUrl = options.apiUrl || SENTIENCE_API_URL;
208+
// Default uploadTrace to true for backward compatibility
209+
const uploadTrace = options.uploadTrace !== false;
202210

203211
// PRODUCTION FIX: Recover orphaned traces from previous crashes
204212
// Note: This is skipped in test environments (see recoverOrphanedTraces function)
205213
// Run in background to avoid blocking tracer creation
206-
if (options.apiKey) {
214+
// Only recover if uploadTrace is enabled
215+
if (options.apiKey && uploadTrace) {
207216
// Don't await - run in background to avoid blocking
208217
recoverOrphanedTraces(options.apiKey, apiUrl).catch(() => {
209218
// Silently fail - orphan recovery should not block tracer creation
210219
});
211220
}
212221

213222
// 1. Try to initialize Cloud Sink (Pro/Enterprise tier)
214-
if (options.apiKey) {
223+
// Only attempt cloud init if uploadTrace is enabled
224+
if (options.apiKey && uploadTrace) {
215225
try {
216226
// Request pre-signed upload URL from backend
217227
const response = await httpPost(

0 commit comments

Comments
 (0)