Skip to content
Merged
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
5 changes: 5 additions & 0 deletions .changeset/brave-aliens-study.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@openai/agents-realtime': patch
---

fix: #639 Type issue with realtime agent handoffs
7 changes: 5 additions & 2 deletions packages/agents-realtime/src/realtimeAgent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { RealtimeContextData } from './realtimeSession';

export type RealtimeAgentConfiguration<TContext = UnknownContext> = Partial<
Omit<
AgentConfiguration<TContext, TextOutput>,
AgentConfiguration<RealtimeContextData<TContext>, TextOutput>,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wrapping with RealtimeContextData just appends history property as optional, so this is safe for both tsc and runtime.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was great, Thank you for resolving so fast.

| 'model'
| 'handoffs'
| 'modelSettings'
Expand All @@ -29,7 +29,10 @@ export type RealtimeAgentConfiguration<TContext = UnknownContext> = Partial<
/**
* Any other `RealtimeAgent` instances the agent is able to hand off to.
*/
handoffs?: (RealtimeAgent | Handoff)[];
handoffs?: (
| RealtimeAgent<TContext>
| Handoff<RealtimeContextData<TContext>, TextOutput>
)[];
/**
* The voice intended to be used by the agent. If another agent already spoke during the
* RealtimeSession, changing the voice during a handoff will fail.
Expand Down
107 changes: 107 additions & 0 deletions packages/agents-realtime/test/realtimeAgentHandoffs.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
import { describe, expect, it } from 'vitest';
import { z } from 'zod';
import {
RealtimeAgent,
tool,
RealtimeContextData,
type RealtimeAgentConfiguration,
} from '../src';

describe('RealtimeAgent handoffs', () => {
it('accepts handoffs sharing the session context', () => {
type SessionContext = { userId: string };

const specialist = new RealtimeAgent<SessionContext>({
name: 'specialist',
});

const mainAgent = new RealtimeAgent<SessionContext>({
name: 'main',
handoffs: [specialist],
});

expect(mainAgent.handoffs).toEqual([specialist]);
});

it('accepts handoffs with default context parameters', () => {
const specialist = new RealtimeAgent({
name: 'specialist',
});

const mainAgent = new RealtimeAgent({
name: 'main',
handoffs: [specialist],
});

expect(mainAgent.handoffs).toEqual([specialist]);
});

it('supports tool definitions without RealtimeContextData', () => {
type SessionContext = { userId: string };
const parameters = z.object({ message: z.string() });
const echoTool = tool<typeof parameters, SessionContext>({
name: 'echo',
description: 'Echo the user id with the provided message.',
parameters,
execute: async ({ message }, runContext) => {
// if you want to access history data, the type parameter must be RealtimeContextData<SessionContext>
// console.log(runContext?.context?.history);
return `${runContext?.context?.userId}: ${message}`;
},
});

const agent = new RealtimeAgent<SessionContext>({
name: 'Tool Agent',
tools: [echoTool],
});
expect(agent.tools).toContain(echoTool);
});

it('supports tool definitions that rely on RealtimeContextData', () => {
type SessionContext = { userId: string };
const parameters = z.object({ message: z.string() });
const echoTool = tool<
typeof parameters,
RealtimeContextData<SessionContext>
>({
name: 'echo',
description: 'Echo the user id with the provided message.',
parameters,
execute: async ({ message }, runContext) => {
// if you want to access history data, the type parameter must be RealtimeContextData<SessionContext>
console.log(runContext?.context?.history);
return `${runContext?.context?.userId}: ${message}`;
},
});

const agent = new RealtimeAgent<SessionContext>({
name: 'Tool Agent',
tools: [echoTool],
});
expect(agent.tools).toContain(echoTool);
});

it('rejects handoffs with incompatible session contexts', () => {
type SessionContext = { userId: string };
type OtherContext = { language: string };

const specialist = new RealtimeAgent<SessionContext>({
name: 'specialist',
});

const validConfig: RealtimeAgentConfiguration<SessionContext> = {
name: 'main',
handoffs: [specialist],
};

expect(validConfig.handoffs).toEqual([specialist]);

const invalidConfig: RealtimeAgentConfiguration<OtherContext> = {
name: 'incompatible',
// @ts-expect-error - mismatched handoff context should not be allowed
handoffs: [specialist],
};

expect(invalidConfig).toBeDefined();
});
});