@copilotkit/runtime
Version:
<img src="https://github.com/user-attachments/assets/0a6b64d9-e193-4940-a3f6-60334ac34084" alt="banner" style="border-radius: 12px; border: 2px solid #d6d4fa;" />
89 lines (87 loc) • 2.97 kB
JavaScript
import "reflect-metadata";
import { InMemoryAgentRunner } from "../../v2/runtime/runner/in-memory.mjs";
import telemetryClient from "../telemetry-client.mjs";
import { catchError, finalize, tap } from "rxjs";
import { createHash } from "node:crypto";
//#region src/lib/runtime/telemetry-agent-runner.ts
/**
* TelemetryAgentRunner - A wrapper around AgentRunner that adds telemetry
* for agent execution streams.
*
* This captures the following telemetry events:
* - oss.runtime.agent_execution_stream_started - when an agent execution starts
* - oss.runtime.agent_execution_stream_ended - when an agent execution completes
* - oss.runtime.agent_execution_stream_errored - when an agent execution fails
*/
/**
* An AgentRunner wrapper that adds telemetry tracking for agent executions.
*
* Usage:
* ```ts
* const runtime = new CopilotRuntime({
* runner: new TelemetryAgentRunner(),
* // or with custom runner:
* runner: new TelemetryAgentRunner({ runner: customRunner }),
* });
* ```
*/
var TelemetryAgentRunner = class {
constructor(config) {
this._runner = config?.runner ?? new InMemoryAgentRunner();
this.hashedLgcKey = config?.langsmithApiKey ? createHash("sha256").update(config.langsmithApiKey).digest("hex") : void 0;
}
/**
* Runs an agent with telemetry tracking.
* Wraps the underlying runner's Observable stream with telemetry events.
*/
run(...args) {
const streamInfo = { hashedLgcKey: this.hashedLgcKey };
let streamErrored = false;
telemetryClient.capture("oss.runtime.agent_execution_stream_started", { hashedLgcKey: this.hashedLgcKey });
return this._runner.run(...args).pipe(tap((event) => {
const rawEvent = event.rawEvent;
if (rawEvent?.data) {
const data = rawEvent.data;
if (data?.output?.model) {
streamInfo.model = data.output.model;
streamInfo.provider = data.output.model;
}
}
if (rawEvent?.metadata) {
const metadata = rawEvent.metadata;
if (metadata?.langgraph_host) streamInfo.langGraphHost = metadata.langgraph_host;
if (metadata?.langgraph_version) streamInfo.langGraphVersion = metadata.langgraph_version;
}
}), catchError((error) => {
streamErrored = true;
telemetryClient.capture("oss.runtime.agent_execution_stream_errored", {
...streamInfo,
error: error instanceof Error ? error.message : String(error)
});
throw error;
}), finalize(() => {
if (!streamErrored) telemetryClient.capture("oss.runtime.agent_execution_stream_ended", streamInfo);
}));
}
/**
* Delegates to the underlying runner's connect method
*/
connect(...args) {
return this._runner.connect(...args);
}
/**
* Delegates to the underlying runner's isRunning method
*/
isRunning(...args) {
return this._runner.isRunning(...args);
}
/**
* Delegates to the underlying runner's stop method
*/
stop(...args) {
return this._runner.stop(...args);
}
};
//#endregion
export { TelemetryAgentRunner };
//# sourceMappingURL=telemetry-agent-runner.mjs.map