Skip to content
Open
Show file tree
Hide file tree
Changes from 7 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
13 changes: 10 additions & 3 deletions agents/src/beta/workflows/task_group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export interface TaskGroupOptions {
returnExceptions?: boolean;
chatCtx?: ChatContext;
onTaskCompleted?: (event: TaskCompletedEvent) => Promise<void>;
preserveFunctionCallHistory?: boolean;
}

export class TaskGroup extends AgentTask<TaskGroupResult> {
Expand All @@ -48,9 +49,15 @@ export class TaskGroup extends AgentTask<TaskGroupResult> {
private _currentTask?: AgentTask;

constructor(options: TaskGroupOptions = {}) {
const { summarizeChatCtx = true, returnExceptions = false, chatCtx, onTaskCompleted } = options;

super({ instructions: '*empty*', chatCtx });
const {
summarizeChatCtx = true,
returnExceptions = false,
chatCtx,
onTaskCompleted,
preserveFunctionCallHistory = false,
} = options;

super({ instructions: '*empty*', chatCtx, preserveFunctionCallHistory });

this._summarizeChatCtx = summarizeChatCtx;
this._returnExceptions = returnExceptions;
Expand Down
13 changes: 12 additions & 1 deletion agents/src/voice/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -529,12 +529,23 @@ export class Agent<UserData = any> {
};
}

export interface AgentTaskOptions<UserData = any> extends AgentOptions<UserData> {
preserveFunctionCallHistory?: boolean;
}

export class AgentTask<ResultT = unknown, UserData = any> extends Agent<UserData> {
private started = false;
private future = new Future<ResultT>();
private _preserveFunctionCallHistory: boolean;

#logger = log();

constructor(options: AgentTaskOptions<UserData>) {
const { preserveFunctionCallHistory = false, ...rest } = options;
super(rest);
this._preserveFunctionCallHistory = preserveFunctionCallHistory;
}

get done(): boolean {
return this.future.done;
}
Expand Down Expand Up @@ -648,7 +659,7 @@ export class AgentTask<ResultT = unknown, UserData = any> extends Agent<UserData
}

const mergedChatCtx = oldAgent._chatCtx.merge(this._chatCtx, {
excludeFunctionCall: true,
excludeFunctionCall: !this._preserveFunctionCallHistory,
excludeInstructions: true,
});
oldAgent._chatCtx.items = mergedChatCtx.items;
Expand Down
47 changes: 26 additions & 21 deletions plugins/phonic/src/realtime/realtime_model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const PHONIC_INPUT_FRAME_MS = 20;
const DEFAULT_MODEL = 'merritt';
const WS_CLOSE_NORMAL = 1000;
const TOOL_CALL_OUTPUT_TIMEOUT_MS = 60_000;
const TOOL_CALL_OUTPUT_MAX_CHARS_FOR_HISTORY = 16_000;
const CONVERSATION_HISTORY_PREFIX =
'\n\nThis conversation is being continued from an existing ' +
'conversation. You are the assistant speaking to the user. ' +
Expand Down Expand Up @@ -301,17 +302,8 @@ export class RealtimeSession extends llm.RealtimeSession {
async updateChatCtx(chatCtx: llm.ChatContext): Promise<void> {
if (!this.configSent) {
if (chatCtx.items.length > 0) {
const turnHistory = chatCtx.items
.filter(
(item): item is llm.ChatMessage =>
item.type === 'message' &&
'textContent' in item &&
item.textContent !== undefined &&
item.textContent.trim() !== '',
)
.map((item) => `${item.role}: ${item.textContent}`)
.join('\n');
if (turnHistory.trim() !== '') {
const turnHistory = this.buildTurnHistory(chatCtx);
if (turnHistory) {
this.#logger.debug(
'updateChatCtx called with messages prior to config being sent to Phonic. Including conversation state in system instructions.',
);
Expand Down Expand Up @@ -894,16 +886,13 @@ export class RealtimeSession extends llm.RealtimeSession {
}

private buildTurnHistory(chatCtx: llm.ChatContext): string | undefined {
const messages = chatCtx.items.filter(
(item): item is llm.ChatMessage =>
item.type === 'message' &&
'textContent' in item &&
item.textContent !== undefined &&
item.textContent.trim() !== '',
);
if (messages.length === 0) return undefined;
const history = messages.map((m) => `${m.role}: ${m.textContent}`).join('\n');
return history.trim() || undefined;
const lines: string[] = [];
for (const item of chatCtx.items) {
const text = chatItemToText(item);
if (text) lines.push(text);
}
if (lines.length === 0) return undefined;
return lines.join('\n');
}

private *resampleAudio(frame: AudioFrame): Generator<AudioFrame> {
Expand Down Expand Up @@ -936,3 +925,19 @@ export class RealtimeSession extends llm.RealtimeSession {
}
}
}

function chatItemToText(item: llm.ChatItem): string | undefined {
if (item.type === 'message') {
const text = item.textContent?.trim();
if (!text) return undefined;
return `<${item.role}>${text}</${item.role}>`;
}
if (item.type === 'function_call') {
return `<tool_call name="${item.name}">${item.args}</tool_call>`;
}
if (item.type === 'function_call_output') {
const tag = item.isError ? 'tool_error' : 'tool_output';
return `<${tag} name="${item.name}">${item.output.slice(0, TOOL_CALL_OUTPUT_MAX_CHARS_FOR_HISTORY)}</${tag}>`;
}
return undefined;
}
Loading