Skip to content

Commit ff00bb5

Browse files
committed
remove easp and old parsing
1 parent f995615 commit ff00bb5

File tree

13 files changed

+50
-618
lines changed

13 files changed

+50
-618
lines changed

common/src/tools/constants.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import type { ToolResultOutput } from '../types/messages/content-part'
22
import type z from 'zod/v4'
33

4-
export const toolNameParam = 'cb_tool_name'
5-
export const endsAgentStepParam = 'cb_easp'
6-
export const toolXmlName = 'codebuff_tool_call'
7-
export const startToolTag = `<${toolXmlName}>\n`
8-
export const endToolTag = `\n</${toolXmlName}>`
4+
// export const toolNameParam = 'cb_tool_name'
5+
// export const endsAgentStepParam = 'cb_easp'
6+
// export const toolXmlName = 'codebuff_tool_call'
7+
// export const startToolTag = `<${toolXmlName}>\n`
8+
// export const endToolTag = `\n</${toolXmlName}>`
99

1010
export const TOOLS_WHICH_WONT_FORCE_NEXT_STEP = [
1111
'think_deeply',

common/src/tools/utils.ts

Lines changed: 2 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,3 @@
1-
import {
2-
endsAgentStepParam,
3-
endToolTag,
4-
startToolTag,
5-
toolNameParam,
6-
} from './constants'
7-
import { $toolParams } from './list'
8-
9-
import type { ToolName } from './constants'
10-
import type z from 'zod/v4'
11-
12-
export function getToolCallString<T extends ToolName | (string & {})>(
13-
toolName: T,
14-
params: T extends ToolName
15-
? z.input<(typeof $toolParams)[T]['parameters']>
16-
: Record<string, any>,
17-
...endsAgentStep: T extends ToolName ? [] : [boolean]
18-
): string {
19-
const endsAgentStepValue =
20-
toolName in $toolParams
21-
? $toolParams[toolName as keyof typeof $toolParams].endsAgentStep
22-
: endsAgentStep[0] ?? false
23-
const obj: Record<string, any> = {
24-
[toolNameParam]: toolName,
25-
...params,
26-
}
27-
if (endsAgentStepValue) {
28-
obj[endsAgentStepParam] = endsAgentStepValue satisfies true
29-
}
30-
return [startToolTag, JSON.stringify(obj, null, 2), endToolTag].join('')
1+
export function getToolCallString(toolName: string, input: any) {
2+
return `<${toolName}>\n${(JSON.stringify(input), null, 2)}\n<\n${toolName}>`
313
}

common/src/util/file.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ export const customToolDefinitionsSchema = z
4444
z.string(),
4545
z.object({
4646
inputJsonSchema: z.any(),
47-
endsAgentStep: z.boolean().optional().default(false),
4847
description: z.string().optional(),
4948
exampleInputs: z.record(z.string(), z.any()).array().optional(),
5049
}),
Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +0,0 @@
1-
import { endsAgentStepParam } from '@codebuff/common/tools/constants'
2-
3-
export const globalStopSequence = `${JSON.stringify(endsAgentStepParam)}`

packages/agent-runtime/src/prompt-agent-stream.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import { globalStopSequence } from './constants'
2-
31
import type { AgentTemplate } from './templates/types'
42
import type { TrackEventFn } from '@codebuff/common/types/contracts/analytics'
53
import type { SendActionFn } from '@codebuff/common/types/contracts/client'
@@ -77,7 +75,6 @@ export const getAgentStreamFromTemplate = (params: {
7775
runId,
7876
messages,
7977
model,
80-
stopSequences: [globalStopSequence],
8178
clientSessionId,
8279
fingerprintId,
8380
userInputId,

packages/agent-runtime/src/run-programmatic-step.ts

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { getToolCallString } from '@codebuff/common/tools/utils'
21
import { getErrorObject } from '@codebuff/common/util/error'
32
import { assistantMessage } from '@codebuff/common/util/messages'
43
import { generateCompactId } from '@codebuff/common/util/string'
@@ -271,25 +270,18 @@ export async function runProgrammaticStep(
271270
const excludeToolFromMessageHistory = toolCall?.includeToolCall === false
272271
// Add assistant message with the tool call before executing it
273272
if (!excludeToolFromMessageHistory) {
274-
const toolCallString = getToolCallString(
275-
toolCall.toolName,
276-
toolCall.input,
277-
)
278-
onResponseChunk(toolCallString)
273+
onResponseChunk({
274+
type: 'tool_call',
275+
toolCallId: toolCall.toolCallId,
276+
toolName: toolCall.toolName,
277+
input: toolCall.input,
278+
})
279279
state.messages.push(
280280
assistantMessage({
281281
...toolCall,
282282
type: 'tool-call',
283283
}),
284284
)
285-
// Optional call handles both top-level and nested agents
286-
state.sendSubagentChunk?.({
287-
userInputId,
288-
agentId: state.agentState.agentId,
289-
agentType: state.agentState.agentType!,
290-
chunk: toolCallString,
291-
forwardToPrompt: !state.agentState.parentId,
292-
})
293285
}
294286

295287
// Execute the tool synchronously and get the result immediately

packages/agent-runtime/src/templates/prompts.ts

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import { getAgentTemplate } from './agent-registry'
21
import { buildArray } from '@codebuff/common/util/array'
32
import { schemaToJsonStr } from '@codebuff/common/util/zod-schema'
43

4+
import { getAgentTemplate } from './agent-registry'
5+
56
import type { AgentTemplate } from '@codebuff/common/types/agent-template'
67
import type { Logger } from '@codebuff/common/types/contracts/logger'
78
import type { ParamsExcluding } from '@codebuff/common/types/function-params'
89
import type { AgentTemplateType } from '@codebuff/common/types/session-state'
9-
import { getToolCallString } from '@codebuff/common/tools/utils'
1010

1111
export async function buildSpawnableAgentsDescription(
1212
params: {
@@ -75,14 +75,19 @@ Notes:
7575
7676
Example:
7777
78-
${getToolCallString('spawn_agents', {
79-
agents: [
80-
{
81-
agent_type: 'example-agent',
82-
prompt: 'Do an example task for me',
83-
},
84-
],
85-
})}
78+
${JSON.stringify(
79+
{
80+
agents: [
81+
{
82+
agent_type: 'example-agent',
83+
prompt: 'Do an example task for me',
84+
},
85+
],
86+
},
87+
null,
88+
2,
89+
)}
90+
8691
8792
Spawn only the below agents:
8893

packages/agent-runtime/src/templates/strings.ts

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,6 @@ import {
1111
getProjectFileTreePrompt,
1212
getSystemInfoPrompt,
1313
} from '../system-prompt/prompts'
14-
import {
15-
fullToolList,
16-
getShortToolInstructions,
17-
getToolsInstructions,
18-
} from '../tools/prompts'
1914
import { parseUserMessage } from '../util/messages'
2015

2116
import type { AgentTemplate, PlaceholderValue } from './types'
@@ -110,8 +105,7 @@ export async function formatPrompt(
110105
[PLACEHOLDER.REMAINING_STEPS]: () => `${agentState.stepsRemaining!}`,
111106
[PLACEHOLDER.PROJECT_ROOT]: () => fileContext.projectRoot,
112107
[PLACEHOLDER.SYSTEM_INFO_PROMPT]: () => getSystemInfoPrompt(fileContext),
113-
[PLACEHOLDER.TOOLS_PROMPT]: async () =>
114-
getToolsInstructions(tools, await additionalToolDefinitions()),
108+
[PLACEHOLDER.TOOLS_PROMPT]: async () => '',
115109
[PLACEHOLDER.AGENTS_PROMPT]: () => buildSpawnableAgentsDescription(params),
116110
[PLACEHOLDER.USER_CWD]: () => fileContext.cwd,
117111
[PLACEHOLDER.USER_INPUT_PROMPT]: () => escapeString(lastUserInput ?? ''),
@@ -203,15 +197,7 @@ export async function getAgentPrompt<T extends StringField>(
203197

204198
// Add tool instructions, spawnable agents, and output schema prompts to instructionsPrompt
205199
if (promptType.type === 'instructionsPrompt' && agentState.agentType) {
206-
const toolsInstructions = agentTemplate.inheritParentSystemPrompt
207-
? fullToolList(agentTemplate.toolNames, await additionalToolDefinitions())
208-
: getShortToolInstructions(
209-
agentTemplate.toolNames,
210-
await additionalToolDefinitions(),
211-
)
212200
addendum +=
213-
'\n\n' +
214-
toolsInstructions +
215201
'\n\n' +
216202
(await buildSpawnableAgentsDescription({
217203
...params,

packages/agent-runtime/src/tool-stream-parser.ts

Lines changed: 9 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
11
import { AnalyticsEvent } from '@codebuff/common/constants/analytics-events'
2-
import {
3-
endsAgentStepParam,
4-
endToolTag,
5-
startToolTag,
6-
toolNameParam,
7-
} from '@codebuff/common/tools/constants'
82

93
import type { Model } from '@codebuff/common/old-constants'
104
import type { TrackEventFn } from '@codebuff/common/types/contracts/analytics'
@@ -15,13 +9,6 @@ import type {
159
PrintModeText,
1610
} from '@codebuff/common/types/print-mode'
1711

18-
const toolExtractionPattern = new RegExp(
19-
`${startToolTag}(.*?)${endToolTag}`,
20-
'gs',
21-
)
22-
23-
const completionSuffix = `${JSON.stringify(endsAgentStepParam)}: true\n}${endToolTag}`
24-
2512
export async function* processStreamWithTags(params: {
2613
stream: AsyncGenerator<StreamChunk, string | null>
2714
processors: Record<
@@ -60,83 +47,6 @@ export async function* processStreamWithTags(params: {
6047
let buffer = ''
6148
let autocompleted = false
6249

63-
function extractToolCalls(): string[] {
64-
const matches: string[] = []
65-
let lastIndex = 0
66-
for (const match of buffer.matchAll(toolExtractionPattern)) {
67-
if (match.index > lastIndex) {
68-
onResponseChunk({
69-
type: 'text',
70-
text: buffer.slice(lastIndex, match.index),
71-
})
72-
}
73-
lastIndex = match.index + match[0].length
74-
matches.push(match[1])
75-
}
76-
77-
buffer = buffer.slice(lastIndex)
78-
return matches
79-
}
80-
81-
function processToolCallContents(contents: string): void {
82-
let input: any
83-
try {
84-
input = JSON.parse(contents)
85-
} catch (error: any) {
86-
trackEvent({
87-
event: AnalyticsEvent.MALFORMED_TOOL_CALL_JSON,
88-
userId: loggerOptions?.userId ?? '',
89-
properties: {
90-
contents: JSON.stringify(contents),
91-
model: loggerOptions?.model,
92-
agent: loggerOptions?.agentName,
93-
error: {
94-
name: error.name,
95-
message: error.message,
96-
stack: error.stack,
97-
},
98-
autocompleted,
99-
},
100-
logger,
101-
})
102-
const shortenedContents =
103-
contents.length < 200
104-
? contents
105-
: contents.slice(0, 100) + '...' + contents.slice(-100)
106-
const errorMessage = `Invalid JSON: ${JSON.stringify(shortenedContents)}\nError: ${error.message}`
107-
onResponseChunk({
108-
type: 'error',
109-
message: errorMessage,
110-
})
111-
onError('parse_error', errorMessage)
112-
return
113-
}
114-
115-
const toolName = input[toolNameParam] as keyof typeof processors
116-
if (typeof toolName !== 'string') {
117-
trackEvent({
118-
event: AnalyticsEvent.UNKNOWN_TOOL_CALL,
119-
userId: loggerOptions?.userId ?? '',
120-
properties: {
121-
contents,
122-
toolName,
123-
model: loggerOptions?.model,
124-
agent: loggerOptions?.agentName,
125-
autocompleted,
126-
},
127-
logger,
128-
})
129-
onError(
130-
'parse_error',
131-
`Unknown tool ${JSON.stringify(toolName)} for tool call: ${contents}`,
132-
)
133-
return
134-
}
135-
136-
delete input[toolNameParam]
137-
processToolCallObject({ toolName, input, contents })
138-
}
139-
14050
function processToolCallObject(params: {
14151
toolName: string
14252
input: any
@@ -164,18 +74,14 @@ export async function* processStreamWithTags(params: {
16474
processor.onTagEnd(toolName, input)
16575
}
16676

167-
function extractToolsFromBufferAndProcess(forceFlush = false) {
168-
const matches = extractToolCalls()
169-
matches.forEach(processToolCallContents)
170-
if (forceFlush) {
171-
if (buffer) {
172-
onResponseChunk({
173-
type: 'text',
174-
text: buffer,
175-
})
176-
}
177-
buffer = ''
77+
function flush() {
78+
if (buffer) {
79+
onResponseChunk({
80+
type: 'text',
81+
text: buffer,
82+
})
17883
}
84+
buffer = ''
17985
}
18086

18187
function* processChunk(
@@ -185,23 +91,13 @@ export async function* processStreamWithTags(params: {
18591
buffer += chunk.text
18692
}
18793
if (chunk && chunk.type === 'tool-call') {
188-
extractToolsFromBufferAndProcess(true)
94+
flush()
18995
processToolCallObject(chunk)
190-
} else {
191-
extractToolsFromBufferAndProcess()
19296
}
19397

19498
if (chunk === undefined) {
19599
streamCompleted = true
196-
if (buffer.includes(startToolTag)) {
197-
buffer += completionSuffix
198-
chunk = {
199-
type: 'text',
200-
text: completionSuffix,
201-
}
202-
autocompleted = true
203-
}
204-
extractToolsFromBufferAndProcess(true)
100+
flush()
205101
}
206102

207103
if (chunk) {

0 commit comments

Comments
 (0)