@livekit/agents 1.0.36 → 1.0.37

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -21,6 +21,7 @@ __export(testing_exports, {
21
21
  AgentHandoffAssert: () => import_run_result.AgentHandoffAssert,
22
22
  AssertionError: () => import_run_result.AssertionError,
23
23
  EventAssert: () => import_run_result.EventAssert,
24
+ EventRangeAssert: () => import_run_result.EventRangeAssert,
24
25
  FunctionCallAssert: () => import_run_result.FunctionCallAssert,
25
26
  FunctionCallOutputAssert: () => import_run_result.FunctionCallOutputAssert,
26
27
  MessageAssert: () => import_run_result.MessageAssert,
@@ -39,6 +40,7 @@ var import_types = require("./types.cjs");
39
40
  AgentHandoffAssert,
40
41
  AssertionError,
41
42
  EventAssert,
43
+ EventRangeAssert,
42
44
  FunctionCallAssert,
43
45
  FunctionCallOutputAssert,
44
46
  MessageAssert,
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../src/voice/testing/index.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2025 LiveKit, Inc.\n//\n// SPDX-License-Identifier: Apache-2.0\n\n/**\n * Testing utilities for agent evaluation.\n *\n * @example\n * ```typescript\n * import { AgentSession, Agent, voice } from '@livekit/agents';\n *\n * const session = new AgentSession({ llm });\n * await session.start(agent);\n *\n * const result = await session.run({ userInput: 'Hello' });\n * result.expect.nextEvent().isMessage({ role: 'assistant' });\n * result.expect.noMoreEvents();\n * ```\n *\n * @packageDocumentation\n */\n\nexport {\n AgentHandoffAssert,\n AssertionError,\n EventAssert,\n FunctionCallAssert,\n FunctionCallOutputAssert,\n MessageAssert,\n RunAssert,\n RunResult,\n} from './run_result.js';\n\nexport {\n isAgentHandoffEvent,\n isChatMessageEvent,\n isFunctionCallEvent,\n isFunctionCallOutputEvent,\n type AgentHandoffAssertOptions,\n type AgentHandoffEvent,\n type ChatMessageEvent,\n type EventType,\n type FunctionCallAssertOptions,\n type FunctionCallEvent,\n type FunctionCallOutputAssertOptions,\n type FunctionCallOutputEvent,\n type MessageAssertOptions,\n type RunEvent,\n} from './types.js';\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAsBA,wBASO;AAEP,mBAeO;","names":[]}
1
+ {"version":3,"sources":["../../../src/voice/testing/index.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2025 LiveKit, Inc.\n//\n// SPDX-License-Identifier: Apache-2.0\n\n/**\n * Testing utilities for agent evaluation.\n *\n * @example\n * ```typescript\n * import { AgentSession, Agent, voice } from '@livekit/agents';\n *\n * const session = new AgentSession({ llm });\n * await session.start(agent);\n *\n * const result = await session.run({ userInput: 'Hello' });\n * result.expect.nextEvent().isMessage({ role: 'assistant' });\n * result.expect.noMoreEvents();\n * ```\n *\n * @packageDocumentation\n */\n\nexport {\n AgentHandoffAssert,\n AssertionError,\n EventAssert,\n EventRangeAssert,\n FunctionCallAssert,\n FunctionCallOutputAssert,\n MessageAssert,\n RunAssert,\n RunResult,\n} from './run_result.js';\n\nexport {\n isAgentHandoffEvent,\n isChatMessageEvent,\n isFunctionCallEvent,\n isFunctionCallOutputEvent,\n type AgentHandoffAssertOptions,\n type AgentHandoffEvent,\n type ChatMessageEvent,\n type EventType,\n type FunctionCallAssertOptions,\n type FunctionCallEvent,\n type FunctionCallOutputAssertOptions,\n type FunctionCallOutputEvent,\n type MessageAssertOptions,\n type RunEvent,\n} from './types.js';\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAsBA,wBAUO;AAEP,mBAeO;","names":[]}
@@ -15,6 +15,6 @@
15
15
  *
16
16
  * @packageDocumentation
17
17
  */
18
- export { AgentHandoffAssert, AssertionError, EventAssert, FunctionCallAssert, FunctionCallOutputAssert, MessageAssert, RunAssert, RunResult, } from './run_result.js';
18
+ export { AgentHandoffAssert, AssertionError, EventAssert, EventRangeAssert, FunctionCallAssert, FunctionCallOutputAssert, MessageAssert, RunAssert, RunResult, } from './run_result.js';
19
19
  export { isAgentHandoffEvent, isChatMessageEvent, isFunctionCallEvent, isFunctionCallOutputEvent, type AgentHandoffAssertOptions, type AgentHandoffEvent, type ChatMessageEvent, type EventType, type FunctionCallAssertOptions, type FunctionCallEvent, type FunctionCallOutputAssertOptions, type FunctionCallOutputEvent, type MessageAssertOptions, type RunEvent, } from './types.js';
20
20
  //# sourceMappingURL=index.d.ts.map
@@ -15,6 +15,6 @@
15
15
  *
16
16
  * @packageDocumentation
17
17
  */
18
- export { AgentHandoffAssert, AssertionError, EventAssert, FunctionCallAssert, FunctionCallOutputAssert, MessageAssert, RunAssert, RunResult, } from './run_result.js';
18
+ export { AgentHandoffAssert, AssertionError, EventAssert, EventRangeAssert, FunctionCallAssert, FunctionCallOutputAssert, MessageAssert, RunAssert, RunResult, } from './run_result.js';
19
19
  export { isAgentHandoffEvent, isChatMessageEvent, isFunctionCallEvent, isFunctionCallOutputEvent, type AgentHandoffAssertOptions, type AgentHandoffEvent, type ChatMessageEvent, type EventType, type FunctionCallAssertOptions, type FunctionCallEvent, type FunctionCallOutputAssertOptions, type FunctionCallOutputEvent, type MessageAssertOptions, type RunEvent, } from './types.js';
20
20
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/voice/testing/index.ts"],"names":[],"mappings":"AAIA;;;;;;;;;;;;;;;;GAgBG;AAEH,OAAO,EACL,kBAAkB,EAClB,cAAc,EACd,WAAW,EACX,kBAAkB,EAClB,wBAAwB,EACxB,aAAa,EACb,SAAS,EACT,SAAS,GACV,MAAM,iBAAiB,CAAC;AAEzB,OAAO,EACL,mBAAmB,EACnB,kBAAkB,EAClB,mBAAmB,EACnB,yBAAyB,EACzB,KAAK,yBAAyB,EAC9B,KAAK,iBAAiB,EACtB,KAAK,gBAAgB,EACrB,KAAK,SAAS,EACd,KAAK,yBAAyB,EAC9B,KAAK,iBAAiB,EACtB,KAAK,+BAA+B,EACpC,KAAK,uBAAuB,EAC5B,KAAK,oBAAoB,EACzB,KAAK,QAAQ,GACd,MAAM,YAAY,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/voice/testing/index.ts"],"names":[],"mappings":"AAIA;;;;;;;;;;;;;;;;GAgBG;AAEH,OAAO,EACL,kBAAkB,EAClB,cAAc,EACd,WAAW,EACX,gBAAgB,EAChB,kBAAkB,EAClB,wBAAwB,EACxB,aAAa,EACb,SAAS,EACT,SAAS,GACV,MAAM,iBAAiB,CAAC;AAEzB,OAAO,EACL,mBAAmB,EACnB,kBAAkB,EAClB,mBAAmB,EACnB,yBAAyB,EACzB,KAAK,yBAAyB,EAC9B,KAAK,iBAAiB,EACtB,KAAK,gBAAgB,EACrB,KAAK,SAAS,EACd,KAAK,yBAAyB,EAC9B,KAAK,iBAAiB,EACtB,KAAK,+BAA+B,EACpC,KAAK,uBAAuB,EAC5B,KAAK,oBAAoB,EACzB,KAAK,QAAQ,GACd,MAAM,YAAY,CAAC"}
@@ -2,6 +2,7 @@ import {
2
2
  AgentHandoffAssert,
3
3
  AssertionError,
4
4
  EventAssert,
5
+ EventRangeAssert,
5
6
  FunctionCallAssert,
6
7
  FunctionCallOutputAssert,
7
8
  MessageAssert,
@@ -18,6 +19,7 @@ export {
18
19
  AgentHandoffAssert,
19
20
  AssertionError,
20
21
  EventAssert,
22
+ EventRangeAssert,
21
23
  FunctionCallAssert,
22
24
  FunctionCallOutputAssert,
23
25
  MessageAssert,
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../src/voice/testing/index.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2025 LiveKit, Inc.\n//\n// SPDX-License-Identifier: Apache-2.0\n\n/**\n * Testing utilities for agent evaluation.\n *\n * @example\n * ```typescript\n * import { AgentSession, Agent, voice } from '@livekit/agents';\n *\n * const session = new AgentSession({ llm });\n * await session.start(agent);\n *\n * const result = await session.run({ userInput: 'Hello' });\n * result.expect.nextEvent().isMessage({ role: 'assistant' });\n * result.expect.noMoreEvents();\n * ```\n *\n * @packageDocumentation\n */\n\nexport {\n AgentHandoffAssert,\n AssertionError,\n EventAssert,\n FunctionCallAssert,\n FunctionCallOutputAssert,\n MessageAssert,\n RunAssert,\n RunResult,\n} from './run_result.js';\n\nexport {\n isAgentHandoffEvent,\n isChatMessageEvent,\n isFunctionCallEvent,\n isFunctionCallOutputEvent,\n type AgentHandoffAssertOptions,\n type AgentHandoffEvent,\n type ChatMessageEvent,\n type EventType,\n type FunctionCallAssertOptions,\n type FunctionCallEvent,\n type FunctionCallOutputAssertOptions,\n type FunctionCallOutputEvent,\n type MessageAssertOptions,\n type RunEvent,\n} from './types.js';\n"],"mappings":"AAsBA;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AAEP;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OAWK;","names":[]}
1
+ {"version":3,"sources":["../../../src/voice/testing/index.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2025 LiveKit, Inc.\n//\n// SPDX-License-Identifier: Apache-2.0\n\n/**\n * Testing utilities for agent evaluation.\n *\n * @example\n * ```typescript\n * import { AgentSession, Agent, voice } from '@livekit/agents';\n *\n * const session = new AgentSession({ llm });\n * await session.start(agent);\n *\n * const result = await session.run({ userInput: 'Hello' });\n * result.expect.nextEvent().isMessage({ role: 'assistant' });\n * result.expect.noMoreEvents();\n * ```\n *\n * @packageDocumentation\n */\n\nexport {\n AgentHandoffAssert,\n AssertionError,\n EventAssert,\n EventRangeAssert,\n FunctionCallAssert,\n FunctionCallOutputAssert,\n MessageAssert,\n RunAssert,\n RunResult,\n} from './run_result.js';\n\nexport {\n isAgentHandoffEvent,\n isChatMessageEvent,\n isFunctionCallEvent,\n isFunctionCallOutputEvent,\n type AgentHandoffAssertOptions,\n type AgentHandoffEvent,\n type ChatMessageEvent,\n type EventType,\n type FunctionCallAssertOptions,\n type FunctionCallEvent,\n type FunctionCallOutputAssertOptions,\n type FunctionCallOutputEvent,\n type MessageAssertOptions,\n type RunEvent,\n} from './types.js';\n"],"mappings":"AAsBA;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AAEP;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OAWK;","names":[]}
@@ -21,6 +21,7 @@ __export(run_result_exports, {
21
21
  AgentHandoffAssert: () => AgentHandoffAssert,
22
22
  AssertionError: () => AssertionError,
23
23
  EventAssert: () => EventAssert,
24
+ EventRangeAssert: () => EventRangeAssert,
24
25
  FunctionCallAssert: () => FunctionCallAssert,
25
26
  FunctionCallOutputAssert: () => FunctionCallOutputAssert,
26
27
  MessageAssert: () => MessageAssert,
@@ -28,6 +29,9 @@ __export(run_result_exports, {
28
29
  RunResult: () => RunResult
29
30
  });
30
31
  module.exports = __toCommonJS(run_result_exports);
32
+ var import_zod = require("zod");
33
+ var import_chat_context = require("../../llm/chat_context.cjs");
34
+ var import_tool_context = require("../../llm/tool_context.cjs");
31
35
  var import_utils = require("../../utils.cjs");
32
36
  var import_speech_handle = require("../speech_handle.cjs");
33
37
  var import_types = require("./types.cjs");
@@ -188,10 +192,6 @@ ${eventsStr}
188
192
  class RunAssert {
189
193
  _events;
190
194
  _currentIndex = 0;
191
- // TODO(brian): Add range access for parity with Python __getitem__ slice support.
192
- // - Add range(start?, end?) method returning EventRangeAssert
193
- // - EventRangeAssert should have containsFunctionCall(), containsMessage() methods
194
- // See Python run_result.py lines 247-251 for reference.
195
195
  constructor(runResult) {
196
196
  this._events = runResult.events;
197
197
  }
@@ -253,6 +253,120 @@ class RunAssert {
253
253
  }
254
254
  return this;
255
255
  }
256
+ /**
257
+ * Conditionally skip the next event if it matches the specified criteria.
258
+ * Returns the event assertion if matched and skipped, or undefined if not matched.
259
+ *
260
+ * @example
261
+ * ```typescript
262
+ * // Skip optional assistant message before function call
263
+ * result.expect.skipNextEventIf({ type: 'message', role: 'assistant' });
264
+ * result.expect.nextEvent().isFunctionCall({ name: 'foo' });
265
+ * ```
266
+ */
267
+ skipNextEventIf(options) {
268
+ if (this._currentIndex >= this._events.length) {
269
+ return void 0;
270
+ }
271
+ try {
272
+ const evAssert = this._currentEvent();
273
+ if (options.type === "message") {
274
+ const { role } = options;
275
+ const result = evAssert.isMessage({ role });
276
+ this._currentIndex++;
277
+ return result;
278
+ } else if (options.type === "function_call") {
279
+ const { name, args } = options;
280
+ const result = evAssert.isFunctionCall({
281
+ name,
282
+ args
283
+ });
284
+ this._currentIndex++;
285
+ return result;
286
+ } else if (options.type === "function_call_output") {
287
+ const { output, isError } = options;
288
+ const result = evAssert.isFunctionCallOutput({
289
+ output,
290
+ isError
291
+ });
292
+ this._currentIndex++;
293
+ return result;
294
+ } else if (options.type === "agent_handoff") {
295
+ const { newAgentType } = options;
296
+ const result = evAssert.isAgentHandoff({ newAgentType });
297
+ this._currentIndex++;
298
+ return result;
299
+ }
300
+ } catch {
301
+ return void 0;
302
+ }
303
+ return void 0;
304
+ }
305
+ /**
306
+ * Get an EventRangeAssert for a range of events.
307
+ * Similar to Python's slice access: expect[0:3] or expect[:]
308
+ *
309
+ * @param start - Start index (inclusive), defaults to 0
310
+ * @param end - End index (exclusive), defaults to events.length
311
+ *
312
+ * @example
313
+ * ```typescript
314
+ * // Search all events
315
+ * result.expect.range().containsFunctionCall({ name: 'foo' });
316
+ * // Search first 3 events
317
+ * result.expect.range(0, 3).containsMessage({ role: 'assistant' });
318
+ * ```
319
+ */
320
+ range(start, end) {
321
+ const startIdx = start ?? 0;
322
+ const endIdx = end ?? this._events.length;
323
+ const events = this._events.slice(startIdx, endIdx);
324
+ return new EventRangeAssert(events, this, { start: startIdx, end: endIdx });
325
+ }
326
+ /**
327
+ * Assert that a function call matching criteria exists anywhere in the events.
328
+ *
329
+ * @example
330
+ * ```typescript
331
+ * result.expect.containsFunctionCall({ name: 'order_item' });
332
+ * ```
333
+ */
334
+ containsFunctionCall(options) {
335
+ return this.range().containsFunctionCall(options);
336
+ }
337
+ /**
338
+ * Assert that a message matching criteria exists anywhere in the events.
339
+ *
340
+ * @example
341
+ * ```typescript
342
+ * result.expect.containsMessage({ role: 'assistant' });
343
+ * ```
344
+ */
345
+ containsMessage(options) {
346
+ return this.range().containsMessage(options);
347
+ }
348
+ /**
349
+ * Assert that a function call output matching criteria exists anywhere in the events.
350
+ *
351
+ * @example
352
+ * ```typescript
353
+ * result.expect.containsFunctionCallOutput({ isError: false });
354
+ * ```
355
+ */
356
+ containsFunctionCallOutput(options) {
357
+ return this.range().containsFunctionCallOutput(options);
358
+ }
359
+ /**
360
+ * Assert that an agent handoff matching criteria exists anywhere in the events.
361
+ *
362
+ * @example
363
+ * ```typescript
364
+ * result.expect.containsAgentHandoff({ newAgentType: MyAgent });
365
+ * ```
366
+ */
367
+ containsAgentHandoff(options) {
368
+ return this.range().containsAgentHandoff(options);
369
+ }
256
370
  /**
257
371
  * Assert that there are no further events.
258
372
  *
@@ -391,6 +505,100 @@ class EventAssert {
391
505
  return new AgentHandoffAssert(event, this._parent, this._index);
392
506
  }
393
507
  }
508
+ class EventRangeAssert {
509
+ _events;
510
+ _parent;
511
+ _range;
512
+ constructor(events, parent, range) {
513
+ this._events = events;
514
+ this._parent = parent;
515
+ this._range = range;
516
+ }
517
+ /**
518
+ * Assert that a function call matching criteria exists in this event range.
519
+ *
520
+ * @example
521
+ * ```typescript
522
+ * result.expect.range(0, 3).containsFunctionCall({ name: 'foo' });
523
+ * ```
524
+ */
525
+ containsFunctionCall(options) {
526
+ for (let idx = 0; idx < this._events.length; idx++) {
527
+ const ev = this._events[idx];
528
+ const candidate = new EventAssert(ev, this._parent, this._range.start + idx);
529
+ try {
530
+ return candidate.isFunctionCall(options);
531
+ } catch {
532
+ }
533
+ }
534
+ this._parent._raiseWithDebugInfo(
535
+ `No FunctionCallEvent satisfying criteria found in range [${this._range.start}:${this._range.end}]`
536
+ );
537
+ }
538
+ /**
539
+ * Assert that a message matching criteria exists in this event range.
540
+ *
541
+ * @example
542
+ * ```typescript
543
+ * result.expect.range(0, 2).containsMessage({ role: 'assistant' });
544
+ * ```
545
+ */
546
+ containsMessage(options) {
547
+ for (let idx = 0; idx < this._events.length; idx++) {
548
+ const ev = this._events[idx];
549
+ const candidate = new EventAssert(ev, this._parent, this._range.start + idx);
550
+ try {
551
+ return candidate.isMessage(options);
552
+ } catch {
553
+ }
554
+ }
555
+ this._parent._raiseWithDebugInfo(
556
+ `No ChatMessageEvent matching criteria found in range [${this._range.start}:${this._range.end}]`
557
+ );
558
+ }
559
+ /**
560
+ * Assert that a function call output matching criteria exists in this event range.
561
+ *
562
+ * @example
563
+ * ```typescript
564
+ * result.expect.range(1, 4).containsFunctionCallOutput({ isError: true });
565
+ * ```
566
+ */
567
+ containsFunctionCallOutput(options) {
568
+ for (let idx = 0; idx < this._events.length; idx++) {
569
+ const ev = this._events[idx];
570
+ const candidate = new EventAssert(ev, this._parent, this._range.start + idx);
571
+ try {
572
+ return candidate.isFunctionCallOutput(options);
573
+ } catch {
574
+ }
575
+ }
576
+ this._parent._raiseWithDebugInfo(
577
+ `No FunctionCallOutputEvent matching criteria found in range [${this._range.start}:${this._range.end}]`
578
+ );
579
+ }
580
+ /**
581
+ * Assert that an agent handoff matching criteria exists in this event range.
582
+ *
583
+ * @example
584
+ * ```typescript
585
+ * result.expect.range(0, 3).containsAgentHandoff({ newAgentType: MyAgent });
586
+ * ```
587
+ */
588
+ containsAgentHandoff(options) {
589
+ for (let idx = 0; idx < this._events.length; idx++) {
590
+ const ev = this._events[idx];
591
+ const candidate = new EventAssert(ev, this._parent, this._range.start + idx);
592
+ try {
593
+ return candidate.isAgentHandoff(options);
594
+ } catch {
595
+ }
596
+ }
597
+ this._parent._raiseWithDebugInfo(
598
+ `No AgentHandoffEvent matching criteria found in range [${this._range.start}:${this._range.end}]`
599
+ );
600
+ }
601
+ }
394
602
  class MessageAssert extends EventAssert {
395
603
  constructor(event, parent, index) {
396
604
  super(event, parent, index);
@@ -398,7 +606,87 @@ class MessageAssert extends EventAssert {
398
606
  event() {
399
607
  return this._event;
400
608
  }
401
- // Phase 3: judge() method will be added here
609
+ /**
610
+ * Evaluate whether the message fulfills the given intent using an LLM.
611
+ *
612
+ * @param llm - LLM instance for judgment
613
+ * @param options - Options containing the intent description
614
+ * @returns Self for chaining further assertions
615
+ *
616
+ * @example
617
+ * ```typescript
618
+ * await result.expect
619
+ * .nextEvent()
620
+ * .isMessage({ role: 'assistant' })
621
+ * .judge(llm, { intent: 'should ask for the drink size' });
622
+ * ```
623
+ */
624
+ async judge(llm, options) {
625
+ const { intent } = options;
626
+ const content = this._event.item.content;
627
+ const msgContent = typeof content === "string" ? content : Array.isArray(content) ? content.filter((c) => typeof c === "string").join(" ") : "";
628
+ if (!msgContent) {
629
+ this._raise("The chat message is empty.");
630
+ }
631
+ if (!intent) {
632
+ this._raise("Intent is required to judge the message.");
633
+ }
634
+ const checkIntentTool = (0, import_tool_context.tool)({
635
+ description: "Determines whether the message correctly fulfills the given intent. Returns success=true if the message satisfies the intent, false otherwise. Provide a concise reason justifying the result.",
636
+ parameters: import_zod.z.object({
637
+ success: import_zod.z.boolean().describe("Whether the message satisfies the intent"),
638
+ reason: import_zod.z.string().describe("A concise explanation justifying the result")
639
+ }),
640
+ execute: async ({ success: success2, reason: reason2 }) => {
641
+ return { success: success2, reason: reason2 };
642
+ }
643
+ });
644
+ const chatCtx = import_chat_context.ChatContext.empty();
645
+ chatCtx.addMessage({
646
+ role: "system",
647
+ content: "You are a test evaluator for conversational agents.\nYou will be shown a message and a target intent. Determine whether the message accomplishes the intent.\nOnly respond by calling the `check_intent(success: bool, reason: str)` function with your final judgment.\nBe strict: if the message does not clearly fulfill the intent, return `success = false` and explain why."
648
+ });
649
+ chatCtx.addMessage({
650
+ role: "user",
651
+ content: `Check if the following message fulfills the given intent.
652
+
653
+ Intent:
654
+ ${intent}
655
+
656
+ Message:
657
+ ${msgContent}`
658
+ });
659
+ let toolArgs;
660
+ const stream = llm.chat({
661
+ chatCtx,
662
+ toolCtx: { check_intent: checkIntentTool },
663
+ toolChoice: { type: "function", function: { name: "check_intent" } },
664
+ extraKwargs: { temperature: 0 }
665
+ });
666
+ for await (const chunk of stream) {
667
+ if (!chunk.delta) continue;
668
+ if (chunk.delta.toolCalls && chunk.delta.toolCalls.length > 0) {
669
+ const toolCall = chunk.delta.toolCalls[0];
670
+ if (toolCall.args) {
671
+ try {
672
+ toolArgs = JSON.parse(toolCall.args);
673
+ } catch {
674
+ }
675
+ }
676
+ }
677
+ }
678
+ if (!toolArgs) {
679
+ this._raise("LLM did not return any arguments for evaluation.");
680
+ }
681
+ const { success, reason } = toolArgs;
682
+ if (!success) {
683
+ this._raise(`Judgment failed: ${reason}`);
684
+ } else if (evalsVerbose) {
685
+ const printMsg = msgContent.length > 30 ? msgContent.slice(0, 30).replace(/\n/g, "\\n") + "..." : msgContent;
686
+ console.log(`- Judgment succeeded for \`${printMsg}\`: \`${reason}\``);
687
+ }
688
+ return this;
689
+ }
402
690
  }
403
691
  class FunctionCallAssert extends EventAssert {
404
692
  constructor(event, parent, index) {
@@ -468,6 +756,7 @@ function formatEvents(events, selectedIndex) {
468
756
  AgentHandoffAssert,
469
757
  AssertionError,
470
758
  EventAssert,
759
+ EventRangeAssert,
471
760
  FunctionCallAssert,
472
761
  FunctionCallOutputAssert,
473
762
  MessageAssert,
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../src/voice/testing/run_result.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2025 LiveKit, Inc.\n//\n// SPDX-License-Identifier: Apache-2.0\nimport type { AgentHandoffItem, ChatItem } from '../../llm/chat_context.js';\nimport type { Task } from '../../utils.js';\nimport { Future } from '../../utils.js';\nimport type { Agent } from '../agent.js';\nimport { type SpeechHandle, isSpeechHandle } from '../speech_handle.js';\nimport {\n type AgentHandoffAssertOptions,\n type AgentHandoffEvent,\n type ChatMessageEvent,\n type EventType,\n type FunctionCallAssertOptions,\n type FunctionCallEvent,\n type FunctionCallOutputAssertOptions,\n type FunctionCallOutputEvent,\n type MessageAssertOptions,\n type RunEvent,\n isAgentHandoffEvent,\n isChatMessageEvent,\n isFunctionCallEvent,\n isFunctionCallOutputEvent,\n} from './types.js';\n\n// Environment variable for verbose output\nconst evalsVerbose = parseInt(process.env.LIVEKIT_EVALS_VERBOSE || '0', 10);\n\n/**\n * Result of a test run containing recorded events and assertion utilities.\n *\n * @example\n * ```typescript\n * const result = await session.run({ userInput: 'Hello' });\n * result.expect.nextEvent().isMessage({ role: 'assistant' });\n * result.expect.noMoreEvents();\n * ```\n */\nexport class RunResult<T = unknown> {\n private _events: RunEvent[] = [];\n private doneFut = new Future<void>();\n private userInput?: string;\n\n private handles: Set<SpeechHandle | Task<void>> = new Set();\n private lastSpeechHandle?: SpeechHandle;\n private runAssert?: RunAssert;\n\n // TODO(brian): Add typed output support for parity with Python\n // - Add outputType?: new (...args: unknown[]) => T\n // - Add finalOutput?: T\n // - Implement markDone() to extract final_output from SpeechHandle.maybeRunFinalOutput\n // - See Python: run_result.py lines 182-201\n\n constructor(options?: { userInput?: string }) {\n this.userInput = options?.userInput;\n }\n\n /**\n * List of all recorded events generated during the run.\n */\n get events(): RunEvent[] {\n return this._events;\n }\n\n /**\n * Provides an assertion helper for verifying the run events.\n */\n get expect(): RunAssert {\n if (evalsVerbose) {\n const eventsStr = formatEvents(this._events)\n .map((line) => ` ${line}`)\n .join('\\n');\n console.log(\n `\\n+ RunResult {\\n userInput: \"${this.userInput}\"\\n events: [\\n${eventsStr}\\n ]\\n }`,\n );\n }\n\n // Cache the RunAssert so cursor position persists across multiple .expect accesses\n if (!this.runAssert) {\n this.runAssert = new RunAssert(this);\n }\n return this.runAssert;\n }\n\n /**\n * Returns the final output of the run after completion.\n *\n * @throws Error - Not implemented yet.\n */\n get finalOutput(): T {\n // TODO(brian): Implement typed output support after AgentTask is implemented.\n throw new Error('finalOutput is not yet implemented in JS.');\n }\n\n /**\n * Indicates whether the run has finished processing all events.\n */\n done(): boolean {\n return this.doneFut.done;\n }\n\n /**\n * Wait for the RunResult to complete. Returns `this` for method chaining.\n *\n * @example\n * ```ts\n * const result = session.run({ userInput: 'Hi!' });\n * await result.wait(); // waits for completion\n * result.expect.nextEvent().isMessage({ role: 'assistant' });\n * ```\n */\n async wait(): Promise<this> {\n await this.doneFut.await;\n return this;\n }\n\n /**\n * @internal\n * Records an agent handoff event.\n */\n _agentHandoff(params: { item: AgentHandoffItem; oldAgent?: Agent; newAgent: Agent }): void {\n const event: AgentHandoffEvent = {\n type: 'agent_handoff',\n item: params.item,\n oldAgent: params.oldAgent,\n newAgent: params.newAgent,\n };\n const index = this._findInsertionIndex(event.item.createdAt);\n this._events.splice(index, 0, event);\n }\n\n /**\n * @internal\n * Called when a chat item is added during the run.\n */\n _itemAdded(item: ChatItem): void {\n if (this.doneFut.done) {\n return;\n }\n\n let event: RunEvent | undefined;\n\n if (item.type === 'message') {\n event = { type: 'message', item } as ChatMessageEvent;\n } else if (item.type === 'function_call') {\n event = { type: 'function_call', item } as FunctionCallEvent;\n } else if (item.type === 'function_call_output') {\n event = { type: 'function_call_output', item } as FunctionCallOutputEvent;\n }\n\n if (event) {\n const index = this._findInsertionIndex(item.createdAt);\n this._events.splice(index, 0, event);\n }\n }\n\n /**\n * @internal\n * Watch a speech handle or task for completion.\n */\n _watchHandle(handle: SpeechHandle | Task<void>): void {\n this.handles.add(handle);\n\n if (isSpeechHandle(handle)) {\n handle._addItemAddedCallback(this._itemAdded.bind(this));\n }\n\n handle.addDoneCallback(() => {\n this._markDoneIfNeeded(handle);\n });\n }\n\n /**\n * @internal\n * Unwatch a handle.\n */\n _unwatchHandle(handle: SpeechHandle | Task<void>): void {\n this.handles.delete(handle);\n\n if (isSpeechHandle(handle)) {\n handle._removeItemAddedCallback(this._itemAdded.bind(this));\n }\n }\n\n private _markDoneIfNeeded(handle: SpeechHandle | Task<void>): void {\n if (isSpeechHandle(handle)) {\n this.lastSpeechHandle = handle;\n }\n\n if ([...this.handles].every((h) => (isSpeechHandle(h) ? h.done() : h.done))) {\n this._markDone();\n }\n }\n\n private _markDone(): void {\n // TODO(brian): Implement final output support after AgentTask is implemented.\n // See Python run_result.py _mark_done() for reference:\n // - Check lastSpeechHandle._maybeRunFinalOutput\n // - Validate output type matches expected type\n // - Set exception or resolve based on output\n if (!this.doneFut.done) {\n this.doneFut.resolve();\n }\n }\n\n /**\n * Find the correct insertion index to maintain chronological order.\n */\n private _findInsertionIndex(createdAt: number): number {\n for (let i = this._events.length - 1; i >= 0; i--) {\n if (this._events[i]!.item.createdAt <= createdAt) {\n return i + 1;\n }\n }\n return 0;\n }\n}\n\n/**\n * Assertion helper for verifying run events in sequence.\n */\nexport class RunAssert {\n private _events: RunEvent[];\n private _currentIndex = 0;\n\n // TODO(brian): Add range access for parity with Python __getitem__ slice support.\n // - Add range(start?, end?) method returning EventRangeAssert\n // - EventRangeAssert should have containsFunctionCall(), containsMessage() methods\n // See Python run_result.py lines 247-251 for reference.\n\n constructor(runResult: RunResult) {\n this._events = runResult.events;\n }\n\n /**\n * Access a specific event by index for assertions.\n * Supports negative indices (e.g., -1 for last event).\n *\n * @example\n * ```typescript\n * result.expect.at(0).isMessage({ role: 'user' });\n * result.expect.at(-1).isMessage({ role: 'assistant' });\n * ```\n */\n at(index: number): EventAssert {\n let normalizedIndex = index;\n if (index < 0) {\n normalizedIndex = this._events.length + index;\n }\n\n if (normalizedIndex < 0 || normalizedIndex >= this._events.length) {\n this._raiseWithDebugInfo(\n `at(${index}) out of range (total events: ${this._events.length})`,\n normalizedIndex,\n );\n }\n\n return new EventAssert(this._events[normalizedIndex]!, this, normalizedIndex);\n }\n\n /**\n * Advance to the next event, optionally filtering by type.\n *\n * @example\n * ```typescript\n * result.expect.nextEvent().isMessage({ role: 'assistant' });\n * result.expect.nextEvent({ type: 'function_call' }).isFunctionCall({ name: 'foo' });\n * ```\n */\n nextEvent(options?: { type?: EventType }): EventAssert {\n while (true) {\n const evAssert = this._currentEvent();\n this._currentIndex++;\n\n if (!options?.type || evAssert.event().type === options.type) {\n return evAssert;\n }\n }\n }\n\n /**\n * Skip a specified number of upcoming events without assertions.\n *\n * @example\n * ```typescript\n * result.expect.skipNext(2);\n * ```\n */\n skipNext(count: number = 1): this {\n for (let i = 0; i < count; i++) {\n if (this._currentIndex >= this._events.length) {\n this._raiseWithDebugInfo(`Tried to skip ${count} event(s), but only ${i} were available.`);\n }\n this._currentIndex++;\n }\n return this;\n }\n\n /**\n * Assert that there are no further events.\n *\n * @example\n * ```typescript\n * result.expect.noMoreEvents();\n * ```\n */\n noMoreEvents(): void {\n if (this._currentIndex < this._events.length) {\n const event = this._events[this._currentIndex]!;\n this._raiseWithDebugInfo(`Expected no more events, but found: ${event.type}`);\n }\n }\n\n private _currentEvent(): EventAssert {\n if (this._currentIndex >= this._events.length) {\n this._raiseWithDebugInfo('Expected another event, but none left.');\n }\n return this.at(this._currentIndex);\n }\n\n /** @internal */\n _raiseWithDebugInfo(message: string, index?: number): never {\n const markerIndex = index ?? this._currentIndex;\n const eventsStr = formatEvents(this._events, markerIndex).join('\\n');\n throw new AssertionError(`${message}\\nContext around failure:\\n${eventsStr}`);\n }\n}\n\n/**\n * Assertion wrapper for a single event.\n */\nexport class EventAssert {\n protected _event: RunEvent;\n protected _parent: RunAssert;\n protected _index: number;\n\n constructor(event: RunEvent, parent: RunAssert, index: number) {\n this._event = event;\n this._parent = parent;\n this._index = index;\n }\n\n /**\n * Get the underlying event.\n */\n event(): RunEvent {\n return this._event;\n }\n\n protected _raise(message: string): never {\n this._parent._raiseWithDebugInfo(message, this._index);\n }\n\n /**\n * Verify this event is a message with optional role matching.\n *\n * @example\n * ```typescript\n * result.expect.nextEvent().isMessage({ role: 'assistant' });\n * ```\n */\n isMessage(options?: MessageAssertOptions): MessageAssert {\n if (!isChatMessageEvent(this._event)) {\n this._raise(`Expected ChatMessageEvent, got ${this._event.type}`);\n }\n\n if (options?.role && this._event.item.role !== options.role) {\n this._raise(`Expected role '${options.role}', got '${this._event.item.role}'`);\n }\n\n return new MessageAssert(this._event, this._parent, this._index);\n }\n\n /**\n * Verify this event is a function call with optional name/args matching.\n *\n * @example\n * ```typescript\n * result.expect.nextEvent().isFunctionCall({ name: 'order_item', args: { id: 'big_mac' } });\n * ```\n */\n isFunctionCall(options?: FunctionCallAssertOptions): FunctionCallAssert {\n if (!isFunctionCallEvent(this._event)) {\n this._raise(`Expected FunctionCallEvent, got ${this._event.type}`);\n }\n\n if (options?.name && this._event.item.name !== options.name) {\n this._raise(`Expected call name '${options.name}', got '${this._event.item.name}'`);\n }\n\n if (options?.args) {\n let actual: Record<string, unknown>;\n try {\n actual = JSON.parse(this._event.item.args);\n } catch {\n this._raise(`Failed to parse function call arguments: ${this._event.item.args}`);\n }\n\n for (const [key, value] of Object.entries(options.args)) {\n if (!(key in actual) || actual[key] !== value) {\n this._raise(\n `For key '${key}', expected ${JSON.stringify(value)}, got ${JSON.stringify(actual[key])}`,\n );\n }\n }\n }\n\n return new FunctionCallAssert(this._event, this._parent, this._index);\n }\n\n /**\n * Verify this event is a function call output with optional matching.\n *\n * @example\n * ```typescript\n * result.expect.nextEvent().isFunctionCallOutput({ isError: false });\n * ```\n */\n isFunctionCallOutput(options?: FunctionCallOutputAssertOptions): FunctionCallOutputAssert {\n if (!isFunctionCallOutputEvent(this._event)) {\n this._raise(`Expected FunctionCallOutputEvent, got ${this._event.type}`);\n }\n\n if (options?.output !== undefined && this._event.item.output !== options.output) {\n this._raise(`Expected output '${options.output}', got '${this._event.item.output}'`);\n }\n\n if (options?.isError !== undefined && this._event.item.isError !== options.isError) {\n this._raise(`Expected isError=${options.isError}, got ${this._event.item.isError}`);\n }\n\n return new FunctionCallOutputAssert(this._event, this._parent, this._index);\n }\n\n /**\n * Verify this event is an agent handoff with optional type matching.\n *\n * @example\n * ```typescript\n * result.expect.nextEvent().isAgentHandoff({ newAgentType: MyAgent });\n * ```\n */\n isAgentHandoff(options?: AgentHandoffAssertOptions): AgentHandoffAssert {\n if (!isAgentHandoffEvent(this._event)) {\n this._raise(`Expected AgentHandoffEvent, got ${this._event.type}`);\n }\n\n // Cast to the correct type after validation\n const event = this._event as AgentHandoffEvent;\n\n if (options?.newAgentType) {\n const actualType = event.newAgent.constructor.name;\n if (!(event.newAgent instanceof options.newAgentType)) {\n this._raise(`Expected new_agent '${options.newAgentType.name}', got '${actualType}'`);\n }\n }\n\n return new AgentHandoffAssert(event, this._parent, this._index);\n }\n}\n\n/**\n * Assertion wrapper for message events.\n */\nexport class MessageAssert extends EventAssert {\n protected declare _event: ChatMessageEvent;\n\n constructor(event: ChatMessageEvent, parent: RunAssert, index: number) {\n super(event, parent, index);\n }\n\n override event(): ChatMessageEvent {\n return this._event;\n }\n\n // Phase 3: judge() method will be added here\n}\n\n/**\n * Assertion wrapper for function call events.\n */\nexport class FunctionCallAssert extends EventAssert {\n protected declare _event: FunctionCallEvent;\n\n constructor(event: FunctionCallEvent, parent: RunAssert, index: number) {\n super(event, parent, index);\n }\n\n override event(): FunctionCallEvent {\n return this._event;\n }\n}\n\n/**\n * Assertion wrapper for function call output events.\n */\nexport class FunctionCallOutputAssert extends EventAssert {\n protected declare _event: FunctionCallOutputEvent;\n\n constructor(event: FunctionCallOutputEvent, parent: RunAssert, index: number) {\n super(event, parent, index);\n }\n\n override event(): FunctionCallOutputEvent {\n return this._event;\n }\n}\n\n/**\n * Assertion wrapper for agent handoff events.\n */\nexport class AgentHandoffAssert extends EventAssert {\n protected declare _event: AgentHandoffEvent;\n\n constructor(event: AgentHandoffEvent, parent: RunAssert, index: number) {\n super(event, parent, index);\n }\n\n override event(): AgentHandoffEvent {\n return this._event;\n }\n}\n\n/**\n * Custom assertion error for test failures.\n */\nexport class AssertionError extends Error {\n constructor(message: string) {\n super(message);\n this.name = 'AssertionError';\n Error.captureStackTrace?.(this, AssertionError);\n }\n}\n\n/**\n * Format events for debug output, optionally marking a selected index.\n */\nfunction formatEvents(events: RunEvent[], selectedIndex?: number): string[] {\n const lines: string[] = [];\n\n for (let i = 0; i < events.length; i++) {\n const event = events[i]!;\n let prefix = '';\n if (selectedIndex !== undefined) {\n prefix = i === selectedIndex ? '>>>' : ' ';\n }\n\n let line: string;\n if (isChatMessageEvent(event)) {\n const { role, content, interrupted } = event.item;\n const textContent =\n typeof content === 'string'\n ? content\n : Array.isArray(content)\n ? content.filter((c): c is string => typeof c === 'string').join(' ')\n : '';\n const truncated = textContent.length > 50 ? textContent.slice(0, 50) + '...' : textContent;\n line = `${prefix}[${i}] { type: \"message\", role: \"${role}\", content: \"${truncated}\", interrupted: ${interrupted} }`;\n } else if (isFunctionCallEvent(event)) {\n const { name, args } = event.item;\n line = `${prefix}[${i}] { type: \"function_call\", name: \"${name}\", args: ${args} }`;\n } else if (isFunctionCallOutputEvent(event)) {\n const { output, isError } = event.item;\n const truncated = output.length > 50 ? output.slice(0, 50) + '...' : output;\n line = `${prefix}[${i}] { type: \"function_call_output\", output: \"${truncated}\", isError: ${isError} }`;\n } else if (isAgentHandoffEvent(event)) {\n line = `${prefix}[${i}] { type: \"agent_handoff\", oldAgent: \"${event.oldAgent?.constructor.name}\", newAgent: \"${event.newAgent.constructor.name}\" }`;\n } else {\n line = `${prefix}[${i}] ${event}`;\n }\n\n lines.push(line);\n }\n\n return lines;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAKA,mBAAuB;AAEvB,2BAAkD;AAClD,mBAeO;AAGP,MAAM,eAAe,SAAS,QAAQ,IAAI,yBAAyB,KAAK,EAAE;AAYnE,MAAM,UAAuB;AAAA,EAC1B,UAAsB,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AI,CAAC,qCAAqC,IAAI,YAAY,IAAI;AAAA,IAChF,eAAW,wCAA0B,KAAK,GAAG;AAC3C,YAAM,EAAE,QAAQ,QAAQ,IAAI,MAAM;AAClC,YAAM,YAAY,OAAO,SAAS,KAAK,OAAO,MAAM,GAAG,EAAE,IAAI,QAAQ;AACrE,aAAO,GAAG,MAAM,IAAI,CAAC,8CAA8C,SAAS,eAAe,OAAO;AAAA,IACpG,eAAW,kCAAoB,KAAK,GAAG;AACrC,aAAO,GAAG,MAAM,IAAI,CAAC,0CAAyC,WAAM,aAAN,mBAAgB,YAAY,IAAI,iBAAiB,MAAM,SAAS,YAAY,IAAI;AAAA,IAChJ,OAAO;AACL,aAAO,GAAG,MAAM,IAAI,CAAC,KAAK,KAAK;AAAA,IACjC;AAEA,UAAM,KAAK,IAAI;AAAA,EACjB;AAEA,SAAO;AACT;","names":[]}
1
+ {"version":3,"sources":["../../../src/voice/testing/run_result.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2025 LiveKit, Inc.\n//\n// SPDX-License-Identifier: Apache-2.0\nimport { z } from 'zod';\nimport type { AgentHandoffItem, ChatItem, ChatRole } from '../../llm/chat_context.js';\nimport { ChatContext } from '../../llm/chat_context.js';\nimport type { LLM } from '../../llm/llm.js';\nimport { tool } from '../../llm/tool_context.js';\nimport type { Task } from '../../utils.js';\nimport { Future } from '../../utils.js';\nimport type { Agent } from '../agent.js';\nimport { type SpeechHandle, isSpeechHandle } from '../speech_handle.js';\nimport {\n type AgentHandoffAssertOptions,\n type AgentHandoffEvent,\n type ChatMessageEvent,\n type EventType,\n type FunctionCallAssertOptions,\n type FunctionCallEvent,\n type FunctionCallOutputAssertOptions,\n type FunctionCallOutputEvent,\n type MessageAssertOptions,\n type RunEvent,\n isAgentHandoffEvent,\n isChatMessageEvent,\n isFunctionCallEvent,\n isFunctionCallOutputEvent,\n} from './types.js';\n\n// Type for agent constructor (used in assertions)\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\ntype AgentConstructor = new (...args: any[]) => Agent;\n\n// Environment variable for verbose output\nconst evalsVerbose = parseInt(process.env.LIVEKIT_EVALS_VERBOSE || '0', 10);\n\n/**\n * Result of a test run containing recorded events and assertion utilities.\n *\n * @example\n * ```typescript\n * const result = await session.run({ userInput: 'Hello' });\n * result.expect.nextEvent().isMessage({ role: 'assistant' });\n * result.expect.noMoreEvents();\n * ```\n */\nexport class RunResult<T = unknown> {\n private _events: RunEvent[] = [];\n private doneFut = new Future<void>();\n private userInput?: string;\n\n private handles: Set<SpeechHandle | Task<void>> = new Set();\n private lastSpeechHandle?: SpeechHandle;\n private runAssert?: RunAssert;\n\n // TODO(brian): Add typed output support for parity with Python\n // - Add outputType?: new (...args: unknown[]) => T\n // - Add finalOutput?: T\n // - Implement markDone() to extract final_output from SpeechHandle.maybeRunFinalOutput\n // - See Python: run_result.py lines 182-201\n\n constructor(options?: { userInput?: string }) {\n this.userInput = options?.userInput;\n }\n\n /**\n * List of all recorded events generated during the run.\n */\n get events(): RunEvent[] {\n return this._events;\n }\n\n /**\n * Provides an assertion helper for verifying the run events.\n */\n get expect(): RunAssert {\n if (evalsVerbose) {\n const eventsStr = formatEvents(this._events)\n .map((line) => ` ${line}`)\n .join('\\n');\n console.log(\n `\\n+ RunResult {\\n userInput: \"${this.userInput}\"\\n events: [\\n${eventsStr}\\n ]\\n }`,\n );\n }\n\n // Cache the RunAssert so cursor position persists across multiple .expect accesses\n if (!this.runAssert) {\n this.runAssert = new RunAssert(this);\n }\n return this.runAssert;\n }\n\n /**\n * Returns the final output of the run after completion.\n *\n * @throws Error - Not implemented yet.\n */\n get finalOutput(): T {\n // TODO(brian): Implement typed output support after AgentTask is implemented.\n throw new Error('finalOutput is not yet implemented in JS.');\n }\n\n /**\n * Indicates whether the run has finished processing all events.\n */\n done(): boolean {\n return this.doneFut.done;\n }\n\n /**\n * Wait for the RunResult to complete. Returns `this` for method chaining.\n *\n * @example\n * ```ts\n * const result = session.run({ userInput: 'Hi!' });\n * await result.wait(); // waits for completion\n * result.expect.nextEvent().isMessage({ role: 'assistant' });\n * ```\n */\n async wait(): Promise<this> {\n await this.doneFut.await;\n return this;\n }\n\n /**\n * @internal\n * Records an agent handoff event.\n */\n _agentHandoff(params: { item: AgentHandoffItem; oldAgent?: Agent; newAgent: Agent }): void {\n const event: AgentHandoffEvent = {\n type: 'agent_handoff',\n item: params.item,\n oldAgent: params.oldAgent,\n newAgent: params.newAgent,\n };\n const index = this._findInsertionIndex(event.item.createdAt);\n this._events.splice(index, 0, event);\n }\n\n /**\n * @internal\n * Called when a chat item is added during the run.\n */\n _itemAdded(item: ChatItem): void {\n if (this.doneFut.done) {\n return;\n }\n\n let event: RunEvent | undefined;\n\n if (item.type === 'message') {\n event = { type: 'message', item };\n } else if (item.type === 'function_call') {\n event = { type: 'function_call', item };\n } else if (item.type === 'function_call_output') {\n event = { type: 'function_call_output', item };\n }\n\n if (event) {\n const index = this._findInsertionIndex(item.createdAt);\n this._events.splice(index, 0, event);\n }\n }\n\n /**\n * @internal\n * Watch a speech handle or task for completion.\n */\n _watchHandle(handle: SpeechHandle | Task<void>): void {\n this.handles.add(handle);\n\n if (isSpeechHandle(handle)) {\n handle._addItemAddedCallback(this._itemAdded.bind(this));\n }\n\n handle.addDoneCallback(() => {\n this._markDoneIfNeeded(handle);\n });\n }\n\n /**\n * @internal\n * Unwatch a handle.\n */\n _unwatchHandle(handle: SpeechHandle | Task<void>): void {\n this.handles.delete(handle);\n\n if (isSpeechHandle(handle)) {\n handle._removeItemAddedCallback(this._itemAdded.bind(this));\n }\n }\n\n private _markDoneIfNeeded(handle: SpeechHandle | Task<void>): void {\n if (isSpeechHandle(handle)) {\n this.lastSpeechHandle = handle;\n }\n\n if ([...this.handles].every((h) => (isSpeechHandle(h) ? h.done() : h.done))) {\n this._markDone();\n }\n }\n\n private _markDone(): void {\n // TODO(brian): Implement final output support after AgentTask is implemented.\n // See Python run_result.py _mark_done() for reference:\n // - Check lastSpeechHandle._maybeRunFinalOutput\n // - Validate output type matches expected type\n // - Set exception or resolve based on output\n if (!this.doneFut.done) {\n this.doneFut.resolve();\n }\n }\n\n /**\n * Find the correct insertion index to maintain chronological order.\n */\n private _findInsertionIndex(createdAt: number): number {\n for (let i = this._events.length - 1; i >= 0; i--) {\n if (this._events[i]!.item.createdAt <= createdAt) {\n return i + 1;\n }\n }\n return 0;\n }\n}\n\n/**\n * Assertion helper for verifying run events in sequence.\n */\nexport class RunAssert {\n private _events: RunEvent[];\n private _currentIndex = 0;\n\n constructor(runResult: RunResult) {\n this._events = runResult.events;\n }\n\n /**\n * Access a specific event by index for assertions.\n * Supports negative indices (e.g., -1 for last event).\n *\n * @example\n * ```typescript\n * result.expect.at(0).isMessage({ role: 'user' });\n * result.expect.at(-1).isMessage({ role: 'assistant' });\n * ```\n */\n at(index: number): EventAssert {\n let normalizedIndex = index;\n if (index < 0) {\n normalizedIndex = this._events.length + index;\n }\n\n if (normalizedIndex < 0 || normalizedIndex >= this._events.length) {\n this._raiseWithDebugInfo(\n `at(${index}) out of range (total events: ${this._events.length})`,\n normalizedIndex,\n );\n }\n\n return new EventAssert(this._events[normalizedIndex]!, this, normalizedIndex);\n }\n\n /**\n * Advance to the next event, optionally filtering by type.\n *\n * @example\n * ```typescript\n * result.expect.nextEvent().isMessage({ role: 'assistant' });\n * result.expect.nextEvent({ type: 'function_call' }).isFunctionCall({ name: 'foo' });\n * ```\n */\n nextEvent(options?: { type?: EventType }): EventAssert {\n while (true) {\n const evAssert = this._currentEvent();\n this._currentIndex++;\n\n if (!options?.type || evAssert.event().type === options.type) {\n return evAssert;\n }\n }\n }\n\n /**\n * Skip a specified number of upcoming events without assertions.\n *\n * @example\n * ```typescript\n * result.expect.skipNext(2);\n * ```\n */\n skipNext(count: number = 1): this {\n for (let i = 0; i < count; i++) {\n if (this._currentIndex >= this._events.length) {\n this._raiseWithDebugInfo(`Tried to skip ${count} event(s), but only ${i} were available.`);\n }\n this._currentIndex++;\n }\n return this;\n }\n\n /**\n * Conditionally skip the next event if it matches the specified criteria.\n * Returns the event assertion if matched and skipped, or undefined if not matched.\n *\n * @example\n * ```typescript\n * // Skip optional assistant message before function call\n * result.expect.skipNextEventIf({ type: 'message', role: 'assistant' });\n * result.expect.nextEvent().isFunctionCall({ name: 'foo' });\n * ```\n */\n skipNextEventIf(\n options:\n | { type: 'message'; role?: ChatRole }\n | { type: 'function_call'; name?: string; args?: Record<string, unknown> }\n | { type: 'function_call_output'; output?: string; isError?: boolean }\n | { type: 'agent_handoff'; newAgentType?: AgentConstructor },\n ):\n | MessageAssert\n | FunctionCallAssert\n | FunctionCallOutputAssert\n | AgentHandoffAssert\n | undefined {\n if (this._currentIndex >= this._events.length) {\n return undefined;\n }\n\n try {\n const evAssert = this._currentEvent();\n\n if (options.type === 'message') {\n const { role } = options;\n const result = evAssert.isMessage({ role });\n this._currentIndex++;\n return result;\n } else if (options.type === 'function_call') {\n const { name, args } = options;\n const result = evAssert.isFunctionCall({\n name,\n args,\n });\n this._currentIndex++;\n return result;\n } else if (options.type === 'function_call_output') {\n const { output, isError } = options;\n const result = evAssert.isFunctionCallOutput({\n output,\n isError,\n });\n this._currentIndex++;\n return result;\n } else if (options.type === 'agent_handoff') {\n const { newAgentType } = options;\n const result = evAssert.isAgentHandoff({ newAgentType });\n this._currentIndex++;\n return result;\n }\n } catch {\n // Assertion failed, event doesn't match criteria\n return undefined;\n }\n\n return undefined;\n }\n\n /**\n * Get an EventRangeAssert for a range of events.\n * Similar to Python's slice access: expect[0:3] or expect[:]\n *\n * @param start - Start index (inclusive), defaults to 0\n * @param end - End index (exclusive), defaults to events.length\n *\n * @example\n * ```typescript\n * // Search all events\n * result.expect.range().containsFunctionCall({ name: 'foo' });\n * // Search first 3 events\n * result.expect.range(0, 3).containsMessage({ role: 'assistant' });\n * ```\n */\n range(start?: number, end?: number): EventRangeAssert {\n const startIdx = start ?? 0;\n const endIdx = end ?? this._events.length;\n const events = this._events.slice(startIdx, endIdx);\n return new EventRangeAssert(events, this, { start: startIdx, end: endIdx });\n }\n\n /**\n * Assert that a function call matching criteria exists anywhere in the events.\n *\n * @example\n * ```typescript\n * result.expect.containsFunctionCall({ name: 'order_item' });\n * ```\n */\n containsFunctionCall(options?: FunctionCallAssertOptions): FunctionCallAssert {\n return this.range().containsFunctionCall(options);\n }\n\n /**\n * Assert that a message matching criteria exists anywhere in the events.\n *\n * @example\n * ```typescript\n * result.expect.containsMessage({ role: 'assistant' });\n * ```\n */\n containsMessage(options?: MessageAssertOptions): MessageAssert {\n return this.range().containsMessage(options);\n }\n\n /**\n * Assert that a function call output matching criteria exists anywhere in the events.\n *\n * @example\n * ```typescript\n * result.expect.containsFunctionCallOutput({ isError: false });\n * ```\n */\n containsFunctionCallOutput(options?: FunctionCallOutputAssertOptions): FunctionCallOutputAssert {\n return this.range().containsFunctionCallOutput(options);\n }\n\n /**\n * Assert that an agent handoff matching criteria exists anywhere in the events.\n *\n * @example\n * ```typescript\n * result.expect.containsAgentHandoff({ newAgentType: MyAgent });\n * ```\n */\n containsAgentHandoff(options?: AgentHandoffAssertOptions): AgentHandoffAssert {\n return this.range().containsAgentHandoff(options);\n }\n\n /**\n * Assert that there are no further events.\n *\n * @example\n * ```typescript\n * result.expect.noMoreEvents();\n * ```\n */\n noMoreEvents(): void {\n if (this._currentIndex < this._events.length) {\n const event = this._events[this._currentIndex]!;\n this._raiseWithDebugInfo(`Expected no more events, but found: ${event.type}`);\n }\n }\n\n private _currentEvent(): EventAssert {\n if (this._currentIndex >= this._events.length) {\n this._raiseWithDebugInfo('Expected another event, but none left.');\n }\n return this.at(this._currentIndex);\n }\n\n /** @internal */\n _raiseWithDebugInfo(message: string, index?: number): never {\n const markerIndex = index ?? this._currentIndex;\n const eventsStr = formatEvents(this._events, markerIndex).join('\\n');\n throw new AssertionError(`${message}\\nContext around failure:\\n${eventsStr}`);\n }\n}\n\n/**\n * Assertion wrapper for a single event.\n */\nexport class EventAssert {\n protected _event: RunEvent;\n protected _parent: RunAssert;\n protected _index: number;\n\n constructor(event: RunEvent, parent: RunAssert, index: number) {\n this._event = event;\n this._parent = parent;\n this._index = index;\n }\n\n /**\n * Get the underlying event.\n */\n event(): RunEvent {\n return this._event;\n }\n\n protected _raise(message: string): never {\n this._parent._raiseWithDebugInfo(message, this._index);\n }\n\n /**\n * Verify this event is a message with optional role matching.\n *\n * @example\n * ```typescript\n * result.expect.nextEvent().isMessage({ role: 'assistant' });\n * ```\n */\n isMessage(options?: MessageAssertOptions): MessageAssert {\n if (!isChatMessageEvent(this._event)) {\n this._raise(`Expected ChatMessageEvent, got ${this._event.type}`);\n }\n\n if (options?.role && this._event.item.role !== options.role) {\n this._raise(`Expected role '${options.role}', got '${this._event.item.role}'`);\n }\n\n return new MessageAssert(this._event, this._parent, this._index);\n }\n\n /**\n * Verify this event is a function call with optional name/args matching.\n *\n * @example\n * ```typescript\n * result.expect.nextEvent().isFunctionCall({ name: 'order_item', args: { id: 'big_mac' } });\n * ```\n */\n isFunctionCall(options?: FunctionCallAssertOptions): FunctionCallAssert {\n if (!isFunctionCallEvent(this._event)) {\n this._raise(`Expected FunctionCallEvent, got ${this._event.type}`);\n }\n\n if (options?.name && this._event.item.name !== options.name) {\n this._raise(`Expected call name '${options.name}', got '${this._event.item.name}'`);\n }\n\n if (options?.args) {\n let actual: Record<string, unknown>;\n try {\n actual = JSON.parse(this._event.item.args);\n } catch {\n this._raise(`Failed to parse function call arguments: ${this._event.item.args}`);\n }\n\n for (const [key, value] of Object.entries(options.args)) {\n if (!(key in actual) || actual[key] !== value) {\n this._raise(\n `For key '${key}', expected ${JSON.stringify(value)}, got ${JSON.stringify(actual[key])}`,\n );\n }\n }\n }\n\n return new FunctionCallAssert(this._event, this._parent, this._index);\n }\n\n /**\n * Verify this event is a function call output with optional matching.\n *\n * @example\n * ```typescript\n * result.expect.nextEvent().isFunctionCallOutput({ isError: false });\n * ```\n */\n isFunctionCallOutput(options?: FunctionCallOutputAssertOptions): FunctionCallOutputAssert {\n if (!isFunctionCallOutputEvent(this._event)) {\n this._raise(`Expected FunctionCallOutputEvent, got ${this._event.type}`);\n }\n\n if (options?.output !== undefined && this._event.item.output !== options.output) {\n this._raise(`Expected output '${options.output}', got '${this._event.item.output}'`);\n }\n\n if (options?.isError !== undefined && this._event.item.isError !== options.isError) {\n this._raise(`Expected isError=${options.isError}, got ${this._event.item.isError}`);\n }\n\n return new FunctionCallOutputAssert(this._event, this._parent, this._index);\n }\n\n /**\n * Verify this event is an agent handoff with optional type matching.\n *\n * @example\n * ```typescript\n * result.expect.nextEvent().isAgentHandoff({ newAgentType: MyAgent });\n * ```\n */\n isAgentHandoff(options?: AgentHandoffAssertOptions): AgentHandoffAssert {\n if (!isAgentHandoffEvent(this._event)) {\n this._raise(`Expected AgentHandoffEvent, got ${this._event.type}`);\n }\n\n const event = this._event;\n\n if (options?.newAgentType) {\n const actualType = event.newAgent.constructor.name;\n if (!(event.newAgent instanceof options.newAgentType)) {\n this._raise(`Expected new_agent '${options.newAgentType.name}', got '${actualType}'`);\n }\n }\n\n return new AgentHandoffAssert(event, this._parent, this._index);\n }\n}\n\n/**\n * Assertion wrapper for a range of events.\n * Provides contains*() methods to search within the range.\n */\nexport class EventRangeAssert {\n private _events: RunEvent[];\n private _parent: RunAssert;\n private _range: { start: number; end: number };\n\n constructor(events: RunEvent[], parent: RunAssert, range: { start: number; end: number }) {\n this._events = events;\n this._parent = parent;\n this._range = range;\n }\n\n /**\n * Assert that a function call matching criteria exists in this event range.\n *\n * @example\n * ```typescript\n * result.expect.range(0, 3).containsFunctionCall({ name: 'foo' });\n * ```\n */\n containsFunctionCall(options?: FunctionCallAssertOptions): FunctionCallAssert {\n for (let idx = 0; idx < this._events.length; idx++) {\n const ev = this._events[idx]!;\n const candidate = new EventAssert(ev, this._parent, this._range.start + idx);\n try {\n return candidate.isFunctionCall(options);\n } catch {\n // Continue searching\n }\n }\n\n this._parent._raiseWithDebugInfo(\n `No FunctionCallEvent satisfying criteria found in range [${this._range.start}:${this._range.end}]`,\n );\n }\n\n /**\n * Assert that a message matching criteria exists in this event range.\n *\n * @example\n * ```typescript\n * result.expect.range(0, 2).containsMessage({ role: 'assistant' });\n * ```\n */\n containsMessage(options?: MessageAssertOptions): MessageAssert {\n for (let idx = 0; idx < this._events.length; idx++) {\n const ev = this._events[idx]!;\n const candidate = new EventAssert(ev, this._parent, this._range.start + idx);\n try {\n return candidate.isMessage(options);\n } catch {\n // Continue searching\n }\n }\n\n this._parent._raiseWithDebugInfo(\n `No ChatMessageEvent matching criteria found in range [${this._range.start}:${this._range.end}]`,\n );\n }\n\n /**\n * Assert that a function call output matching criteria exists in this event range.\n *\n * @example\n * ```typescript\n * result.expect.range(1, 4).containsFunctionCallOutput({ isError: true });\n * ```\n */\n containsFunctionCallOutput(options?: FunctionCallOutputAssertOptions): FunctionCallOutputAssert {\n for (let idx = 0; idx < this._events.length; idx++) {\n const ev = this._events[idx]!;\n const candidate = new EventAssert(ev, this._parent, this._range.start + idx);\n try {\n return candidate.isFunctionCallOutput(options);\n } catch {\n // Continue searching\n }\n }\n\n this._parent._raiseWithDebugInfo(\n `No FunctionCallOutputEvent matching criteria found in range [${this._range.start}:${this._range.end}]`,\n );\n }\n\n /**\n * Assert that an agent handoff matching criteria exists in this event range.\n *\n * @example\n * ```typescript\n * result.expect.range(0, 3).containsAgentHandoff({ newAgentType: MyAgent });\n * ```\n */\n containsAgentHandoff(options?: AgentHandoffAssertOptions): AgentHandoffAssert {\n for (let idx = 0; idx < this._events.length; idx++) {\n const ev = this._events[idx]!;\n const candidate = new EventAssert(ev, this._parent, this._range.start + idx);\n try {\n return candidate.isAgentHandoff(options);\n } catch {\n // Continue searching\n }\n }\n\n this._parent._raiseWithDebugInfo(\n `No AgentHandoffEvent matching criteria found in range [${this._range.start}:${this._range.end}]`,\n );\n }\n}\n\n/**\n * Assertion wrapper for message events.\n */\nexport class MessageAssert extends EventAssert {\n protected declare _event: ChatMessageEvent;\n\n constructor(event: ChatMessageEvent, parent: RunAssert, index: number) {\n super(event, parent, index);\n }\n\n override event(): ChatMessageEvent {\n return this._event;\n }\n\n /**\n * Evaluate whether the message fulfills the given intent using an LLM.\n *\n * @param llm - LLM instance for judgment\n * @param options - Options containing the intent description\n * @returns Self for chaining further assertions\n *\n * @example\n * ```typescript\n * await result.expect\n * .nextEvent()\n * .isMessage({ role: 'assistant' })\n * .judge(llm, { intent: 'should ask for the drink size' });\n * ```\n */\n async judge(llm: LLM, options: { intent: string }): Promise<MessageAssert> {\n const { intent } = options;\n\n // Extract text content from message\n const content = this._event.item.content;\n const msgContent =\n typeof content === 'string'\n ? content\n : Array.isArray(content)\n ? content.filter((c): c is string => typeof c === 'string').join(' ')\n : '';\n\n if (!msgContent) {\n this._raise('The chat message is empty.');\n }\n\n if (!intent) {\n this._raise('Intent is required to judge the message.');\n }\n\n // Create the check_intent tool\n const checkIntentTool = tool({\n description:\n 'Determines whether the message correctly fulfills the given intent. ' +\n 'Returns success=true if the message satisfies the intent, false otherwise. ' +\n 'Provide a concise reason justifying the result.',\n parameters: z.object({\n success: z.boolean().describe('Whether the message satisfies the intent'),\n reason: z.string().describe('A concise explanation justifying the result'),\n }),\n execute: async ({ success, reason }: { success: boolean; reason: string }) => {\n return { success, reason };\n },\n });\n\n // Create chat context for the judge\n const chatCtx = ChatContext.empty();\n chatCtx.addMessage({\n role: 'system',\n content:\n 'You are a test evaluator for conversational agents.\\n' +\n 'You will be shown a message and a target intent. Determine whether the message accomplishes the intent.\\n' +\n 'Only respond by calling the `check_intent(success: bool, reason: str)` function with your final judgment.\\n' +\n 'Be strict: if the message does not clearly fulfill the intent, return `success = false` and explain why.',\n });\n chatCtx.addMessage({\n role: 'user',\n content:\n 'Check if the following message fulfills the given intent.\\n\\n' +\n `Intent:\\n${intent}\\n\\n` +\n `Message:\\n${msgContent}`,\n });\n\n // Call the LLM with the check_intent tool\n let toolArgs: { success: boolean; reason: string } | undefined;\n\n const stream = llm.chat({\n chatCtx,\n toolCtx: { check_intent: checkIntentTool },\n toolChoice: { type: 'function', function: { name: 'check_intent' } },\n extraKwargs: { temperature: 0 },\n });\n\n for await (const chunk of stream) {\n if (!chunk.delta) continue;\n\n if (chunk.delta.toolCalls && chunk.delta.toolCalls.length > 0) {\n const toolCall = chunk.delta.toolCalls[0]!;\n if (toolCall.args) {\n try {\n toolArgs = JSON.parse(toolCall.args);\n } catch {\n // Args might be streamed incrementally, keep the last valid parse\n }\n }\n }\n }\n\n if (!toolArgs) {\n this._raise('LLM did not return any arguments for evaluation.');\n }\n\n const { success, reason } = toolArgs;\n\n if (!success) {\n this._raise(`Judgment failed: ${reason}`);\n } else if (evalsVerbose) {\n const printMsg =\n msgContent.length > 30 ? msgContent.slice(0, 30).replace(/\\n/g, '\\\\n') + '...' : msgContent;\n console.log(`- Judgment succeeded for \\`${printMsg}\\`: \\`${reason}\\``);\n }\n\n return this;\n }\n}\n\n/**\n * Assertion wrapper for function call events.\n */\nexport class FunctionCallAssert extends EventAssert {\n protected declare _event: FunctionCallEvent;\n\n constructor(event: FunctionCallEvent, parent: RunAssert, index: number) {\n super(event, parent, index);\n }\n\n override event(): FunctionCallEvent {\n return this._event;\n }\n}\n\n/**\n * Assertion wrapper for function call output events.\n */\nexport class FunctionCallOutputAssert extends EventAssert {\n protected declare _event: FunctionCallOutputEvent;\n\n constructor(event: FunctionCallOutputEvent, parent: RunAssert, index: number) {\n super(event, parent, index);\n }\n\n override event(): FunctionCallOutputEvent {\n return this._event;\n }\n}\n\n/**\n * Assertion wrapper for agent handoff events.\n */\nexport class AgentHandoffAssert extends EventAssert {\n protected declare _event: AgentHandoffEvent;\n\n constructor(event: AgentHandoffEvent, parent: RunAssert, index: number) {\n super(event, parent, index);\n }\n\n override event(): AgentHandoffEvent {\n return this._event;\n }\n}\n\n/**\n * Custom assertion error for test failures.\n */\nexport class AssertionError extends Error {\n constructor(message: string) {\n super(message);\n this.name = 'AssertionError';\n Error.captureStackTrace?.(this, AssertionError);\n }\n}\n\n// TODO: mockTools() utility for mocking tool implementations in tests\n// Will be implemented for test suites.\n// See Python run_result.py lines 1010-1031 for reference.\n\n/**\n * Format events for debug output, optionally marking a selected index.\n */\nfunction formatEvents(events: RunEvent[], selectedIndex?: number): string[] {\n const lines: string[] = [];\n\n for (let i = 0; i < events.length; i++) {\n const event = events[i]!;\n let prefix = '';\n if (selectedIndex !== undefined) {\n prefix = i === selectedIndex ? '>>>' : ' ';\n }\n\n let line: string;\n if (isChatMessageEvent(event)) {\n const { role, content, interrupted } = event.item;\n const textContent =\n typeof content === 'string'\n ? content\n : Array.isArray(content)\n ? content.filter((c): c is string => typeof c === 'string').join(' ')\n : '';\n const truncated = textContent.length > 50 ? textContent.slice(0, 50) + '...' : textContent;\n line = `${prefix}[${i}] { type: \"message\", role: \"${role}\", content: \"${truncated}\", interrupted: ${interrupted} }`;\n } else if (isFunctionCallEvent(event)) {\n const { name, args } = event.item;\n line = `${prefix}[${i}] { type: \"function_call\", name: \"${name}\", args: ${args} }`;\n } else if (isFunctionCallOutputEvent(event)) {\n const { output, isError } = event.item;\n const truncated = output.length > 50 ? output.slice(0, 50) + '...' : output;\n line = `${prefix}[${i}] { type: \"function_call_output\", output: \"${truncated}\", isError: ${isError} }`;\n } else if (isAgentHandoffEvent(event)) {\n line = `${prefix}[${i}] { type: \"agent_handoff\", oldAgent: \"${event.oldAgent?.constructor.name}\", newAgent: \"${event.newAgent.constructor.name}\" }`;\n } else {\n line = `${prefix}[${i}] ${event}`;\n }\n\n lines.push(line);\n }\n\n return lines;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,iBAAkB;AAElB,0BAA4B;AAE5B,0BAAqB;AAErB,mBAAuB;AAEvB,2BAAkD;AAClD,mBAeO;AAOP,MAAM,eAAe,SAAS,QAAQ,IAAI,yBAAyB,KAAK,EAAE;AAYnE,MAAM,UAAuB;AAAA,EAC1B,UAAsB,CAAC;AAAA,EACvB,UAAU,IAAI,o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