Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions src/handlers/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -453,3 +453,30 @@ export function startMessageSpan(
)
setBoundedMap(ctx.messageSpans, msgKey, msgSpan)
}

/**
* Initialises or updates the per-session agent metadata when a new user prompt
* arrives. Creates a fresh `SessionTotals` entry if none exists for this session
* (e.g. the session was started before the current plugin process). Updates the
* agent name on subsequent messages within the same session.
*/
export function handleChatMessage(
sessionID: string,
agent: string | undefined,
ctx: Pick<HandlerContext, "sessionTotals">,
) {
const agentName = agent ?? "unknown"
const totals = ctx.sessionTotals.get(sessionID)
if (totals) {
if (agent) totals.agent = agentName
} else {
ctx.sessionTotals.set(sessionID, {
startMs: Date.now(),
tokens: 0,
cost: 0,
messages: 0,
agent: agentName,
agentType: "primary",
})
}
}
5 changes: 2 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { probeEndpoint } from "./probe.ts"
import { setupOtel, createInstruments } from "./otel.ts"
import { remoteParentContext } from "./trace-context.ts"
import { handleSessionCreated, handleSessionIdle, handleSessionError, handleSessionStatus } from "./handlers/session.ts"
import { handleMessageUpdated, handleMessagePartUpdated, startMessageSpan } from "./handlers/message.ts"
import { handleMessageUpdated, handleMessagePartUpdated, startMessageSpan, handleChatMessage } from "./handlers/message.ts"
import { handlePermissionUpdated, handlePermissionReplied } from "./handlers/permission.ts"
import { handleSessionDiff, handleCommandExecuted } from "./handlers/activity.ts"
import { agentAttrs, getSessionAgentMeta } from "./util.ts"
Expand Down Expand Up @@ -183,9 +183,8 @@ export const OtelPlugin: Plugin = async ({ project, client, directory, worktree

"chat.message": safe("chat.message", async (input, output) => {
const agent = input.agent ?? "unknown"
handleChatMessage(input.sessionID, input.agent, ctx)
const { agentType } = getSessionAgentMeta(input.sessionID, ctx)
const totals = sessionTotals.get(input.sessionID)
if (totals) totals.agent = agent
const sessionSpan = sessionSpans.get(input.sessionID)
if (sessionSpan) sessionSpan.setAttributes({ [AGENT_NAME]: agent, "agent.type": agentType })
const promptText = output.parts.map((part) => {
Expand Down
36 changes: 35 additions & 1 deletion tests/handlers/message.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, test, expect } from "bun:test"
import { handleMessageUpdated, handleMessagePartUpdated } from "../../src/handlers/message.ts"
import { handleMessageUpdated, handleMessagePartUpdated, handleChatMessage } from "../../src/handlers/message.ts"
import { makeCtx } from "../helpers.ts"
import type { EventMessageUpdated, EventMessagePartUpdated } from "@opencode-ai/sdk"

Expand Down Expand Up @@ -239,6 +239,40 @@ describe("handleMessageUpdated", () => {
})
})

describe("handleChatMessage", () => {
test("creates sessionTotals entry with agent when none exists", () => {
const { ctx } = makeCtx()
handleChatMessage("ses_no_totals", "build", ctx)
const totals = ctx.sessionTotals.get("ses_no_totals")!
expect(totals.agent).toBe("build")
expect(totals.agentType).toBe("primary")
expect(totals.tokens).toBe(0)
expect(totals.cost).toBe(0)
expect(totals.messages).toBe(0)
expect(totals.startMs).toBeGreaterThan(0)
})

test("defaults agent to 'unknown' when agent is undefined", () => {
const { ctx } = makeCtx()
handleChatMessage("ses_no_agent", undefined, ctx)
expect(ctx.sessionTotals.get("ses_no_agent")!.agent).toBe("unknown")
})

test("updates agent on existing entry", () => {
const { ctx } = makeCtx()
ctx.sessionTotals.set("ses_1", { startMs: 1000, tokens: 100, cost: 0.01, messages: 1, agent: "build", agentType: "primary" })
handleChatMessage("ses_1", "plan", ctx)
expect(ctx.sessionTotals.get("ses_1")!.agent).toBe("plan")
})

test("does not override agent with 'unknown' on existing entry when called without agent", () => {
const { ctx } = makeCtx()
ctx.sessionTotals.set("ses_1", { startMs: 1000, tokens: 100, cost: 0.01, messages: 1, agent: "build", agentType: "primary" })
handleChatMessage("ses_1", undefined, ctx)
expect(ctx.sessionTotals.get("ses_1")!.agent).toBe("build")
})
})

describe("handleMessagePartUpdated", () => {
test("ignores non-tool parts", async () => {
const { ctx, histograms } = makeCtx()
Expand Down
Loading