Skip to content

Commit 3f7c217

Browse files
committed
tweak message format
1 parent fb760a0 commit 3f7c217

File tree

69 files changed

+1101
-2024
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

69 files changed

+1101
-2024
lines changed

.agents/__tests__/context-pruner.test.ts

Lines changed: 23 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { describe, test, expect, beforeEach } from 'bun:test'
22

33
import contextPruner from '../context-pruner'
44

5-
import type { Message } from '../types/util-types'
5+
import type { Message, ToolMessage } from '../types/util-types'
66

77
describe('context-pruner handleSteps', () => {
88
let mockAgentState: any
@@ -25,43 +25,37 @@ describe('context-pruner handleSteps', () => {
2525
command: string,
2626
output: string,
2727
exitCode?: number,
28-
): any => ({
28+
): ToolMessage => ({
2929
role: 'tool',
30-
content: {
31-
type: 'tool-result',
32-
toolCallId: 'test-id',
33-
toolName: 'run_terminal_command',
34-
output: [
35-
{
36-
type: 'json',
37-
value: {
38-
command,
39-
stdout: output,
40-
...(exitCode !== undefined && { exitCode }),
41-
},
30+
toolCallId: 'test-id',
31+
toolName: 'run_terminal_command',
32+
content: [
33+
{
34+
type: 'json',
35+
value: {
36+
command,
37+
stdout: output,
38+
...(exitCode !== undefined && { exitCode }),
4239
},
43-
],
44-
},
40+
},
41+
],
4542
})
4643

4744
const createLargeToolMessage = (
4845
toolName: string,
4946
largeData: string,
50-
): any => ({
47+
): ToolMessage => ({
5148
role: 'tool',
52-
content: {
53-
type: 'tool-result',
54-
toolCallId: 'test-id',
55-
toolName,
56-
output: [
57-
{
58-
type: 'json',
59-
value: {
60-
data: largeData,
61-
},
49+
toolCallId: 'test-id',
50+
toolName,
51+
content: [
52+
{
53+
type: 'json',
54+
value: {
55+
data: largeData,
6256
},
63-
],
64-
},
57+
},
58+
],
6559
})
6660

6761
const runHandleSteps = (messages: Message[]) => {

.agents/base2/alloy2/base2-alloy2.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { createBase2 } from '../base2'
2+
3+
import type { ToolCall } from '../../types/agent-definition'
24
import type { SecretAgentDefinition } from '../../types/secret-agent-definition'
3-
import { ToolCall } from '../../types/agent-definition'
45

56
const base2 = createBase2('default')
67
const definition: SecretAgentDefinition = {
@@ -49,10 +50,9 @@ Two hypothetical next steps have been generated from planner agents (base2-plan-
4950
const spawnResultIndex = agentState.messageHistory.findLastIndex(
5051
(m) =>
5152
m.role === 'tool' &&
52-
m.content.toolName === 'spawn_agents' &&
53-
m.content.output[0].type === 'json' &&
54-
(m.content.output[0].value as any[])[0]?.agentType ===
55-
'base2-plan-step',
53+
m.toolName === 'spawn_agents' &&
54+
m.content[0].type === 'json' &&
55+
(m.content[0].value as any[])[0]?.agentType === 'base2-plan-step',
5656
)
5757

5858
const updatedMessageHistory = agentState.messageHistory.concat()

.agents/base2/base2-read-moar.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { type ToolCall } from '../types/agent-definition'
21
import { createBase2 } from './base2'
2+
import { type ToolCall } from '../types/agent-definition'
33
import { type SecretAgentDefinition } from '../types/secret-agent-definition'
44

55
const definition: SecretAgentDefinition = {
@@ -27,8 +27,8 @@ const definition: SecretAgentDefinition = {
2727
const readFilesToolResults = agentState.messageHistory
2828
.filter((message) => message.role === 'tool')
2929
.slice(-1)
30-
.filter((message) => message.content.toolName === 'read_files')
31-
.map((message) => message.content.output)
30+
.filter((message) => message.toolName === 'read_files')
31+
.map((message) => message.content)
3232
.flat()
3333
.filter((result) => result.type === 'json')
3434
.map((result) => result.value)[0] as {
@@ -40,8 +40,8 @@ const definition: SecretAgentDefinition = {
4040
const spawnAgentsToolResults = agentState.messageHistory
4141
.filter((message) => message.role === 'tool')
4242
.slice(-2)
43-
.filter((message) => message.content.toolName === 'spawn_agents')
44-
.map((message) => message.content.output)
43+
.filter((message) => message.toolName === 'spawn_agents')
44+
.map((message) => message.content)
4545
.flat()
4646
.filter((result) => result.type === 'json')
4747
.map((result) => result.value)[0] as {

.agents/base2/task-researcher/base2-gpt-5-with-task-researcher.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,8 @@ Important: you *must* make at least one tool call in every response message unle
152152
const spawnAgentsToolResults = agentState.messageHistory
153153
.slice(-3)
154154
.filter((message) => message.role === 'tool')
155-
.filter((message) => message.content.toolName === 'spawn_agents')
156-
.map((message) => message.content.output)
155+
.filter((message) => message.toolName === 'spawn_agents')
156+
.map((message) => message.content)
157157
.flat()
158158
.filter((result) => result.type === 'json')
159159
.map((result) => result.value)[0] as {

.agents/base2/task-researcher/base2-with-task-researcher-planner-pro.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,8 @@ You may not need to spawn the task-researcher/planner-pro if the user's request
154154
// If found, reset messages to only include the task researcher's result and read the relevant files!
155155
const spawnAgentsToolResults = agentState.messageHistory
156156
.filter((message) => message.role === 'tool')
157-
.filter((message) => message.content.toolName === 'spawn_agents')
158-
.map((message) => message.content.output)
157+
.filter((message) => message.toolName === 'spawn_agents')
158+
.map((message) => message.content)
159159
.flat()
160160
.filter((result) => result.type === 'json')
161161
.map((result) => result.value)[0] as {

.agents/base2/task-researcher/base2-with-task-researcher.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,8 @@ You may not need to spawn the task-researcher2 if the user's request is trivial
9292
const spawnAgentsToolResults = agentState.messageHistory
9393
.filter((message) => message.role === 'tool')
9494
.slice(-1)
95-
.filter((message) => message.content.toolName === 'spawn_agents')
96-
.map((message) => message.content.output)
95+
.filter((message) => message.toolName === 'spawn_agents')
96+
.map((message) => message.content)
9797
.flat()
9898
.filter((result) => result.type === 'json')
9999
.map((result) => result.value)[0] as {

.agents/context-pruner.ts

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ const definition: AgentDefinition = {
6060
// Handle tool messages with new object format
6161
if (
6262
message.role === 'tool' &&
63-
message.content.toolName === 'run_terminal_command'
63+
message.toolName === 'run_terminal_command'
6464
) {
6565
const toolMessage =
6666
message as CodebuffToolMessage<'run_terminal_command'>
@@ -73,19 +73,15 @@ const definition: AgentDefinition = {
7373
const simplifiedMessage: CodebuffToolMessage<'run_terminal_command'> =
7474
{
7575
...toolMessage,
76-
content: {
77-
...toolMessage.content,
78-
output: [
79-
{
80-
type: 'json',
81-
value: {
82-
command:
83-
toolMessage.content.output[0]?.value?.command || '',
84-
stdoutOmittedForLength: true,
85-
},
76+
content: [
77+
{
78+
type: 'json',
79+
value: {
80+
command: toolMessage.content[0]?.value?.command || '',
81+
stdoutOmittedForLength: true,
8682
},
87-
],
88-
},
83+
},
84+
],
8985
}
9086
afterTerminalPass.unshift(simplifiedMessage)
9187
}
@@ -110,24 +106,21 @@ const definition: AgentDefinition = {
110106
// PASS 2: Remove large tool results (any tool result output > 1000 chars when stringified)
111107
const afterToolResultsPass = afterTerminalPass.map((message) => {
112108
if (message.role === 'tool') {
113-
const outputSize = JSON.stringify(message.content.output).length
109+
const outputSize = JSON.stringify(message.content).length
114110

115111
if (outputSize > 1000) {
116112
// Replace with simplified output
117113
const simplifiedMessage: ToolMessage = {
118114
...message,
119-
content: {
120-
...message.content,
121-
output: [
122-
{
123-
type: 'json',
124-
value: {
125-
message: '[LARGE_TOOL_RESULT_OMITTED]',
126-
originalSize: outputSize,
127-
},
115+
content: [
116+
{
117+
type: 'json',
118+
value: {
119+
message: '[LARGE_TOOL_RESULT_OMITTED]',
120+
originalSize: outputSize,
128121
},
129-
],
130-
},
122+
},
123+
],
131124
}
132125
return simplifiedMessage
133126
}

.agents/editor/best-of-n/editor-best-of-n.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1+
import { buildArray } from '@codebuff/common/util/array'
2+
13
import { publisher } from '../../constants'
24

3-
import type { SecretAgentDefinition } from '../../types/secret-agent-definition'
45
import type {
56
AgentStepContext,
67
StepText,
78
ToolCall,
89
} from '../../types/agent-definition'
9-
import { buildArray } from '@codebuff/common/util/array'
10+
import type { SecretAgentDefinition } from '../../types/secret-agent-definition'
1011

1112
export function createBestOfNEditor(
1213
model: 'sonnet' | 'gpt-5' | 'gemini',
@@ -217,7 +218,7 @@ function* handleStepsSonnet({
217218
const editToolResults = messageHistory
218219
.slice(lastAssistantMessageIndex)
219220
.filter((message) => message.role === 'tool')
220-
.flatMap((message) => message.content.output)
221+
.flatMap((message) => message.content)
221222
.filter((output) => output.type === 'json')
222223
.map((output) => output.value)
223224

@@ -341,7 +342,7 @@ function* handleStepsGemini({
341342
const editToolResults = messageHistory
342343
.slice(lastAssistantMessageIndex)
343344
.filter((message) => message.role === 'tool')
344-
.flatMap((message) => message.content.output)
345+
.flatMap((message) => message.content)
345346
.filter((output) => output.type === 'json')
346347
.map((output) => output.value)
347348

.agents/editor/best-of-n/editor-implementor-gpt-5.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ Write out your complete implementation now, formatting all changes as tool calls
123123
const editToolResults = messageHistory
124124
.slice(lastAssistantMessageIndex)
125125
.filter((message) => message.role === 'tool')
126-
.flatMap((message) => message.content.output)
126+
.flatMap((message) => message.content)
127127
.filter((output) => output.type === 'json')
128128
.map((output) => output.value)
129129

.agents/editor/editor-lite.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
import { Message } from 'types/util-types'
21
import { publisher } from '../constants'
32
import {
43
PLACEHOLDER,
54
type SecretAgentDefinition,
65
} from '../types/secret-agent-definition'
76

7+
import type { Message } from 'types/util-types'
8+
89
const editor: SecretAgentDefinition = {
910
id: 'editor-lite',
1011
publisher,
@@ -151,14 +152,14 @@ ${PLACEHOLDER.KNOWLEDGE_FILES_CONTENTS}`,
151152
.filter((message) => message.role === 'tool')
152153
.filter(
153154
(message) =>
154-
message.content.toolName === 'write_file' ||
155-
message.content.toolName === 'str_replace',
155+
message.toolName === 'write_file' ||
156+
message.toolName === 'str_replace',
156157
)
157158

158159
// Extract and return new edit tool results
159160
return (
160161
newToolMessages
161-
.flatMap((message) => message.content.output)
162+
.flatMap((message) => message.content)
162163
.filter((output) => output.type === 'json')
163164
.map((output) => output.value)
164165
// Only successful edits!

0 commit comments

Comments
 (0)