Skip to content

Commit 689dcc0

Browse files
committed
Changed: Publish a LiveKit model response's links once
1 parent dc85f6f commit 689dcc0

2 files changed

Lines changed: 55 additions & 2 deletions

File tree

‎js/src/instrumentation/plugins/livekit-plugin.test.ts‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { z } from "zod/v3";
1313
import { configureNode } from "../../node/config";
1414
import {
1515
Attachment,
16+
SpanImpl,
1617
_exportsForTestingOnly as logging,
1718
initLogger,
1819
} from "../../logger";
@@ -413,6 +414,36 @@ describe("tracing", () => {
413414
});
414415
});
415416

417+
test("a model response publishes the links from its context once, and again for a late target", async () => {
418+
const root = start("agent_session");
419+
const turn = start("agent_turn", root);
420+
const tool = (callId: string) =>
421+
start("function_tool", turn, {
422+
"lk.function_tool.name": "lookup_order",
423+
"gen_ai.tool.call.id": callId,
424+
}).end();
425+
for (const callId of ["call-1", "call-2", "call-3"]) tool(callId);
426+
const log = vi.spyOn(SpanImpl.prototype, "log");
427+
const publications = () =>
428+
log.mock.calls.filter(([event]) => event.metadata?.links).length;
429+
start("llm_node", turn, {
430+
"lk.pii.chat_ctx": JSON.stringify({
431+
items: ["call-1", "call-2", "call-3", "call-4"].map((call_id) => ({
432+
type: "function_call_output",
433+
call_id,
434+
})),
435+
}),
436+
}).end();
437+
expect(publications()).toBe(1);
438+
tool("call-4");
439+
expect(publications()).toBe(2);
440+
turn.end();
441+
const { one, named } = await rows();
442+
expect(
443+
one("llm_response").metadata.links.map((l: any) => l.context.spanId),
444+
).toEqual(named("lookup_order").map((r) => r.span_id));
445+
});
446+
416447
test("speech is generated by every synthesis in its reply, and only its reply", async () => {
417448
const root = start("agent_session");
418449
const turn = start("agent_turn", root);

‎js/src/instrumentation/plugins/livekit-plugin.ts‎

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -629,7 +629,10 @@ class LiveKitTracing {
629629
const state = this.sessions.get(row.session);
630630
if (!row.owner) {
631631
row.span.log(endEvent(row, attrs, events));
632-
if (state) correlate(row, identities, state.relationships);
632+
if (state)
633+
state.relationships.batch(row.span, () =>
634+
correlate(row, identities, state.relationships),
635+
);
633636
if (state && row.name === "user_speaking")
634637
row.span.log({
635638
metadata: {
@@ -1287,6 +1290,19 @@ class Relationships {
12871290
private targets = new Map<string, Span[]>();
12881291
private waiting = new Map<string, Map<Span, Relationship>>();
12891292
private links = new Map<Span, Map<string, Link>>();
1293+
/** A source whose new links publish together, and whether it has any. */
1294+
private batched?: { source: Span; changed: boolean };
1295+
1296+
/** Publishes the links `fn` adds from `source` once, as one complete list. */
1297+
batch(source: Span, fn: () => void): void {
1298+
const batched = (this.batched = { source, changed: false });
1299+
try {
1300+
fn();
1301+
} finally {
1302+
this.batched = undefined;
1303+
if (batched.changed) this.publish(source);
1304+
}
1305+
}
12901306

12911307
register(id: string, target: Span): void {
12921308
const targets = this.targets.get(id) ?? [];
@@ -1337,9 +1353,15 @@ class Relationships {
13371353
attributes: { "braintrust.relationship.type": relationship },
13381354
});
13391355
this.links.set(source, links);
1356+
if (this.batched?.source === source) this.batched.changed = true;
1357+
else this.publish(source);
1358+
}
1359+
1360+
private publish(source: Span): void {
13401361
// Native rows have no top-level links field, and OTel ingestion does not keep
13411362
// links yet. Metadata carries them until links are first-class.
1342-
source.log({ metadata: { links: [...links.values()] } });
1363+
const links = [...(this.links.get(source)?.values() ?? [])];
1364+
source.log({ metadata: { links } });
13431365
}
13441366
}
13451367

0 commit comments

Comments
 (0)