Skip to content
Open
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
12 changes: 7 additions & 5 deletions packages/agents-runtime/src/pi-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,13 @@ export function toAgentHistory(

case `assistant`: {
const prev = lastAssistant()
if (prev) {
;(prev.content as Array<unknown>).push({
type: `text`,
text: message.content,
})
const prevContent = prev?.content as
| Array<{ type: string; text?: string }>
| undefined
const lastBlock = prevContent?.[prevContent.length - 1]

if (lastBlock?.type === `text`) {
lastBlock.text = `${lastBlock.text ?? ``}${message.content}`
} else {
history.push({
role: `assistant`,
Expand Down
17 changes: 17 additions & 0 deletions packages/agents-runtime/test/pi-adapter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,23 @@ describe(`toAgentHistory`, () => {
expect(second?.role).toBe(`assistant`)
})

it(`merges adjacent assistant text messages into one text block`, () => {
const messages: Array<LLMMessage> = [
{ role: `user`, content: `Question` },
{ role: `assistant`, content: `First chunk.` },
{ role: `assistant`, content: ` Second chunk.` },
]

const history = toAgentHistory(messages)

expect(history).toHaveLength(2)
const assistant = history[1] as AssistantMessage
expect(assistant.role).toBe(`assistant`)
expect(assistant.content).toEqual([
{ type: `text`, text: `First chunk. Second chunk.` },
])
})

it(`merges assistant text and tool_call into a single assistant message`, () => {
const messages: Array<LLMMessage> = [
{ role: `user`, content: `Help me` },
Expand Down
Loading