- packages/sdk-ts: BatchTransport, TraceBuilder, models, decision helpers Zero external deps, native fetch, ESM+CJS output - packages/opencode-plugin: OpenCode plugin with hooks for: - Session lifecycle (create/idle/error/delete/diff) - Tool execution capture (before/after -> TOOL_CALL spans + TOOL_SELECTION decisions) - LLM call tracking (chat.message -> LLM_CALL spans with model/provider) - Permission flow (permission.ask -> ESCALATION decisions) - File edit events - Model cost estimation (Claude, GPT-4o, o3-mini pricing)
78 lines
2.0 KiB
TypeScript
78 lines
2.0 KiB
TypeScript
import type { TracePayload } from "./models.js";
|
|
|
|
export interface BatchTransportOptions {
|
|
apiKey: string;
|
|
endpoint: string;
|
|
maxBatchSize?: number;
|
|
flushInterval?: number;
|
|
}
|
|
|
|
export class BatchTransport {
|
|
private readonly apiKey: string;
|
|
private readonly endpoint: string;
|
|
private readonly maxBatchSize: number;
|
|
private readonly flushInterval: number;
|
|
private buffer: TracePayload[] = [];
|
|
private timer: ReturnType<typeof setInterval> | null = null;
|
|
|
|
constructor(options: BatchTransportOptions) {
|
|
this.apiKey = options.apiKey;
|
|
this.endpoint = options.endpoint.replace(/\/+$/, "");
|
|
this.maxBatchSize = options.maxBatchSize ?? 10;
|
|
this.flushInterval = options.flushInterval ?? 5_000;
|
|
|
|
this.timer = setInterval(() => {
|
|
void this._doFlush();
|
|
}, this.flushInterval);
|
|
}
|
|
|
|
add(trace: TracePayload): void {
|
|
this.buffer.push(trace);
|
|
if (this.buffer.length >= this.maxBatchSize) {
|
|
void this._doFlush();
|
|
}
|
|
}
|
|
|
|
async flush(): Promise<void> {
|
|
await this._doFlush();
|
|
}
|
|
|
|
async shutdown(): Promise<void> {
|
|
if (this.timer !== null) {
|
|
clearInterval(this.timer);
|
|
this.timer = null;
|
|
}
|
|
await this._doFlush();
|
|
}
|
|
|
|
private async _doFlush(): Promise<void> {
|
|
if (this.buffer.length === 0) {
|
|
return;
|
|
}
|
|
|
|
const batch = this.buffer.splice(0, this.buffer.length);
|
|
|
|
try {
|
|
const response = await fetch(`${this.endpoint}/api/traces`, {
|
|
method: "POST",
|
|
headers: {
|
|
Authorization: `Bearer ${this.apiKey}`,
|
|
"Content-Type": "application/json",
|
|
},
|
|
body: JSON.stringify({ traces: batch }),
|
|
});
|
|
|
|
if (!response.ok) {
|
|
const text = await response.text().catch(() => "");
|
|
console.warn(
|
|
`AgentLens: Failed to send traces (HTTP ${response.status}): ${text.slice(0, 200)}`,
|
|
);
|
|
}
|
|
} catch (error: unknown) {
|
|
const message =
|
|
error instanceof Error ? error.message : String(error);
|
|
console.warn(`AgentLens: Failed to send traces: ${message}`);
|
|
}
|
|
}
|
|
}
|