@devclocked/tracker-core
v2.1.2
Published
Shared SDK for DevClocked tracker applications (VS Code, Chrome, Desktop)
Maintainers
Readme
@devclocked/tracker-core
Shared SDK for DevClocked tracker applications (VS Code, Chrome, Desktop).
Features
- Unified API: Same interface across all tracker applications
- Offline Support: Local queue with exponential retry
- Auto Authentication: JWT token management with refresh
- Cross-Platform: Works in browser, Node.js, and VS Code
- Privacy First: User-controlled exclusion patterns
- Real-time Sync: Automatic session and tick synchronization
Installation
npm install @devclocked/tracker-coreQuick Start
import { TrackerClient, StorageAdapter } from '@devclocked/tracker-core';
// Create a storage adapter for your environment
const storage = new YourStorageAdapter();
// Initialize the tracker client
const tracker = new TrackerClient({
supabaseUrl: 'https://your-project.supabase.co',
supabaseAnonKey: 'your-anon-key',
source: 'vscode', // or 'chrome', 'desktop'
clientId: 'unique-client-id',
deviceName: 'My Development Machine',
debug: true
}, storage);
// Authenticate with Supabase access token
await tracker.authenticate('your-access-token');
// Start tracking a session
const session = await tracker.startSession('https://github.com/user/repo', 'main');
// Send activity ticks
await tracker.sendTick('/src/components/App.tsx', 'file', {
repo_url: 'https://github.com/user/repo',
branch: 'main',
language: 'typescript',
is_write: true
});
// End the session
await tracker.endSession(session.id);API Reference
TrackerClient
Main class for all tracker operations.
Methods
authenticate(token: string)- Authenticate with SupabasestartSession(repo?, branch?, projectName?)- Start a new sessionendSession(sessionId?)- End current or specified sessiongetActiveSession()- Get current active sessionsendTick(entity, type, metadata?)- Send activity tickflushQueue()- Manually flush the tick queuelinkCommit(sessionId, sha, repo, committedAt)- Link commit to sessionsendTelemetry(events)- Send telemetry eventsgetTrackerState()- Get current tracker stategetQueueStats()- Get queue statisticsclearQueue()- Clear the tick queuedestroy()- Cleanup resources
StorageAdapter
Abstract class for cross-platform storage. Implement this for your environment.
Methods
get(key: string)- Get value by keyset(key: string, value: string)- Set value by keyremove(key: string)- Remove value by keyclear()- Clear all valueskeys()- Get all keysgetObject<T>(key: string)- Get JSON objectsetObject<T>(key: string, value: T)- Set JSON object
AuthManager
Handles authentication and token management.
Methods
authenticate(token: string)- Authenticate with tokenisAuthenticated()- Check authentication statusgetAccessToken()- Get current access tokenrefreshToken()- Refresh access tokenlogout()- Logout and clear state
TickQueue
Manages local queue with retry logic.
Methods
enqueueTicks(ticks: TickData[])- Add ticks to queueenqueueTelemetry(events: TelemetryEvent[])- Add telemetry to queueprocessQueue(processor)- Process queue itemsgetQueueStats()- Get queue statisticsclearQueue()- Clear all items
Storage Adapter Implementations
Browser (IndexedDB)
import { StorageAdapter } from '@devclocked/tracker-core';
class BrowserStorageAdapter extends StorageAdapter {
private db: IDBDatabase;
async get(key: string): Promise<string | null> {
// Implement IndexedDB get
}
async set(key: string, value: string): Promise<void> {
// Implement IndexedDB set
}
// ... implement other methods
}Node.js (File System)
import { StorageAdapter } from '@devclocked/tracker-core';
import fs from 'fs/promises';
import path from 'path';
class NodeStorageAdapter extends StorageAdapter {
private storageDir: string;
async get(key: string): Promise<string | null> {
try {
const filePath = path.join(this.storageDir, key);
return await fs.readFile(filePath, 'utf-8');
} catch {
return null;
}
}
async set(key: string, value: string): Promise<void> {
const filePath = path.join(this.storageDir, key);
await fs.writeFile(filePath, value, 'utf-8');
}
// ... implement other methods
}VS Code (GlobalState)
import { StorageAdapter } from '@devclocked/tracker-core';
import * as vscode from 'vscode';
class VSCodeStorageAdapter extends StorageAdapter {
constructor(private context: vscode.ExtensionContext) {
super();
}
async get(key: string): Promise<string | null> {
return this.context.globalState.get(key) || null;
}
async set(key: string, value: string): Promise<void> {
await this.context.globalState.update(key, value);
}
// ... implement other methods
}Configuration
TrackerConfig
interface TrackerConfig {
supabaseUrl: string; // Your Supabase project URL
supabaseAnonKey: string; // Your Supabase anon key
source: TrackerSource; // 'vscode' | 'chrome' | 'desktop'
clientId: string; // Unique client identifier
deviceName?: string; // Optional device name
debug?: boolean; // Enable debug logging
}Error Handling
The SDK includes comprehensive error handling:
- Authentication errors: Invalid tokens, expired sessions
- Network errors: Connection failures, timeouts
- Rate limiting: Automatic retry with exponential backoff
- Queue management: Persistent storage of failed requests
Privacy & Security
- Local storage: All data stored locally until successfully synced
- Encryption: Sensitive data encrypted at rest
- User control: Exclusion patterns for private work
- No content logging: Only metadata (paths, URLs, timestamps)
License
MIT
