import { getActiveContext } from "../context.js";
import { calculateCost, inferProvider } from "../utils/costs.js";
import { randomUUID } from "node:crypto";

//#region src/integrations/openai-agents.ts
var AmplitudeTracingProcessor = class {
	_ai;
	_defaults;
	_turnId = 1;
	constructor(options) {
		const ctx = getActiveContext();
		this._ai = options.amplitudeAI;
		this._defaults = {
			userId: options.userId ?? ctx?.userId ?? "openai-agents-user",
			sessionId: options.sessionId ?? ctx?.sessionId ?? randomUUID(),
			traceId: options.traceId ?? ctx?.traceId ?? randomUUID(),
			agentId: options.agentId ?? ctx?.agentId ?? null,
			env: options.env ?? ctx?.env ?? null
		};
	}
	onSpanStart(_span) {}
	onTraceStart(_trace) {}
	onTraceEnd(_trace) {}
	onSpanEnd(span) {
		const spanData = this._getSpanData(span);
		if (spanData == null) return;
		const traceId = this._getTraceId(span);
		const latencyMs = this._getLatencyMs(span);
		const kind = this._inferKind(spanData);
		if (kind === "generation") {
			this._handleGeneration(spanData, traceId, latencyMs);
			return;
		}
		if (kind === "function") {
			this._handleFunction(spanData, traceId, latencyMs);
			return;
		}
		if (kind === "handoff") {
			const fromAgent = String(spanData.from_agent ?? "unknown");
			const toAgent = String(spanData.to_agent ?? "unknown");
			this._ai.trackSpan({
				userId: this._defaults.userId,
				spanName: `handoff:${fromAgent}->${toAgent}`,
				traceId,
				latencyMs,
				sessionId: this._defaults.sessionId,
				agentId: this._defaults.agentId,
				env: this._defaults.env,
				inputState: { from_agent: fromAgent },
				outputState: { to_agent: toAgent }
			});
			return;
		}
		if (kind === "guardrail") {
			const guardrail = String(spanData.name ?? "guardrail");
			const triggered = Boolean(spanData.triggered);
			this._ai.trackSpan({
				userId: this._defaults.userId,
				spanName: `guardrail:${guardrail}`,
				traceId,
				latencyMs,
				sessionId: this._defaults.sessionId,
				agentId: this._defaults.agentId,
				env: this._defaults.env,
				outputState: { triggered },
				isError: triggered
			});
			return;
		}
		const name = String(spanData.name ?? "agent");
		this._ai.trackSpan({
			userId: this._defaults.userId,
			spanName: `agent:${name}`,
			traceId,
			latencyMs,
			sessionId: this._defaults.sessionId,
			agentId: this._defaults.agentId ?? name,
			env: this._defaults.env,
			outputState: spanData.output != null ? { output: String(spanData.output) } : void 0
		});
	}
	shutdown() {
		this._ai.flush();
	}
	_handleGeneration(data, traceId, latencyMs) {
		const input = this._normalizeMessagesArray(data.input);
		for (const message of input) {
			const role = message?.role;
			const content = message?.content;
			if (role !== "user" || typeof content !== "string" || content.length === 0) continue;
			this._ai.trackUserMessage({
				userId: this._defaults.userId,
				content,
				sessionId: this._defaults.sessionId,
				traceId,
				turnId: this._turnId,
				agentId: this._defaults.agentId,
				env: this._defaults.env
			});
			this._turnId += 1;
		}
		const output = this._normalizeMessagesArray(data.output);
		const responseText = this._extractAssistantText(output);
		const toolCalls = this._extractToolCalls(output);
		const model = String(data.model ?? "unknown");
		const usage = data.usage ?? {};
		const inputTokens = this._safeNumber(usage.input_tokens ?? usage.prompt_tokens ?? null);
		const outputTokens = this._safeNumber(usage.output_tokens ?? usage.completion_tokens ?? null);
		const totalTokens = this._safeNumber(usage.total_tokens ?? null);
		const promptDetails = usage.prompt_tokens_details;
		const cachedTokens = this._safeNumber(promptDetails?.cached_tokens ?? promptDetails?.cachedTokens ?? null) ?? 0;
		let costUsd;
		if (model !== "unknown" && inputTokens != null && outputTokens != null) try {
			const cost = calculateCost({
				modelName: model,
				inputTokens,
				outputTokens,
				cacheReadInputTokens: cachedTokens,
				defaultProvider: inferProvider(model)
			});
			if (cost > 0) costUsd = cost;
		} catch {}
		this._ai.trackAiMessage({
			userId: this._defaults.userId,
			content: responseText,
			sessionId: this._defaults.sessionId,
			traceId,
			turnId: this._turnId,
			model,
			provider: inferProvider(model),
			latencyMs,
			inputTokens,
			outputTokens,
			totalTokens,
			totalCostUsd: costUsd,
			toolCalls: toolCalls.length > 0 ? toolCalls : void 0,
			agentId: this._defaults.agentId,
			env: this._defaults.env
		});
		this._turnId += 1;
	}
	_handleFunction(data, traceId, latencyMs) {
		const toolName = String(data.name ?? "unknown");
		const errorMessage = data.error == null ? void 0 : String(data.error ?? "unknown error");
		const toolInput = data.input != null && typeof data.input === "object" ? data.input : data.input != null ? { raw: String(data.input) } : void 0;
		const toolOutput = data.output == null ? void 0 : String(data.output ?? void 0);
		this._ai.trackToolCall({
			userId: this._defaults.userId,
			toolName,
			success: errorMessage == null,
			latencyMs,
			sessionId: this._defaults.sessionId,
			traceId,
			turnId: this._turnId,
			input: toolInput,
			output: toolOutput,
			errorMessage,
			agentId: this._defaults.agentId,
			env: this._defaults.env
		});
	}
	_inferKind(data) {
		if ("from_agent" in data || "to_agent" in data) return "handoff";
		if ("triggered" in data) return "guardrail";
		if ("model" in data && ("input" in data || "output" in data)) return "generation";
		if ("name" in data && ("input" in data || "output" in data || "error" in data)) return "function";
		return "agent";
	}
	_getSpanData(span) {
		const data = span.span_data ?? span.data ?? span;
		if (data == null || typeof data !== "object") return null;
		return data;
	}
	_getTraceId(span) {
		const traceId = span.trace_id ?? span.traceId ?? this._defaults.traceId;
		return traceId == null ? randomUUID() : String(traceId);
	}
	_getLatencyMs(span) {
		if (typeof span.latency_ms === "number") return span.latency_ms;
		if (typeof span.latencyMs === "number") return span.latencyMs;
		const start = span.start_time_ms ?? span.startTimeMs;
		const end = span.end_time_ms ?? span.endTimeMs;
		if (typeof start === "number" && typeof end === "number" && end >= start) return end - start;
		return 0;
	}
	_safeNumber(value) {
		if (typeof value === "number") return value;
		if (typeof value === "string") {
			const parsed = Number(value);
			return Number.isNaN(parsed) ? void 0 : parsed;
		}
	}
	_normalizeMessagesArray(value) {
		if (Array.isArray(value)) return value;
		if (value == null) return [];
		return [value];
	}
	_extractAssistantText(output) {
		let responseText = "";
		for (const block of output) {
			if (block == null || typeof block !== "object") continue;
			const item = block;
			if (!(item.role === "assistant" || item.type === "message" || item.type === "output_text")) continue;
			if (typeof item.content === "string") {
				responseText += item.content;
				continue;
			}
			if (Array.isArray(item.content)) for (const part of item.content) {
				const p = part;
				if (p.type === "text" && typeof p.text === "string") responseText += p.text;
				if (typeof p.value === "string") responseText += p.value;
			}
			if (item.type === "output_text" && typeof item.text === "string") responseText += item.text;
		}
		return responseText;
	}
	_extractToolCalls(output) {
		const result = [];
		for (const block of output) {
			if (block == null || typeof block !== "object") continue;
			const item = block;
			if (item.type === "function_call") {
				result.push({
					type: "function",
					id: String(item.id ?? ""),
					function: {
						name: String(item.name ?? ""),
						arguments: String(item.arguments ?? "")
					}
				});
				continue;
			}
			if (!Array.isArray(item.tool_calls)) continue;
			for (const rawCall of item.tool_calls) {
				const call = rawCall;
				result.push({
					type: "function",
					id: String(call.id ?? ""),
					function: {
						name: String(call.function?.name ?? ""),
						arguments: String(call.function?.arguments ?? "")
					}
				});
			}
		}
		return result;
	}
};

//#endregion
export { AmplitudeTracingProcessor };
//# sourceMappingURL=openai-agents.js.map