Skip to content

Commit dc94a9f

Browse files
committed
User input placeholder string
1 parent fd7e679 commit dc94a9f

File tree

3 files changed

+18
-20
lines changed

3 files changed

+18
-20
lines changed

backend/src/templates/strings.ts

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import {
2323
AgentTemplate,
2424
} from './types'
2525
import type { AgentRegistry } from './agent-registry'
26+
import { parseUserMessage } from '../util/messages'
2627

2728
export async function formatPrompt(
2829
prompt: string,
@@ -33,25 +34,16 @@ export async function formatPrompt(
3334
agentRegistry: AgentRegistry,
3435
intitialAgentPrompt?: string
3536
): Promise<string> {
36-
// Handle structured prompt data
37-
let processedPrompt = intitialAgentPrompt ?? ''
38-
39-
try {
40-
// Try to parse as JSON to extract structured data
41-
const promptData = JSON.parse(intitialAgentPrompt ?? '{}')
42-
if (typeof promptData === 'object' && promptData !== null) {
43-
// If it's structured data, extract the main prompt
44-
processedPrompt = promptData.prompt || intitialAgentPrompt || ''
45-
46-
// Handle file paths for planner agent
47-
if (promptData.filePaths && Array.isArray(promptData.filePaths)) {
48-
processedPrompt += `\n\nRelevant files to consider:\n${promptData.filePaths.map((path: string) => `- ${path}`).join('\n')}`
49-
}
50-
}
51-
} catch {
52-
// If not JSON, use as-is
53-
processedPrompt = intitialAgentPrompt ?? ''
54-
}
37+
const { messageHistory } = agentState
38+
const lastUserMessage = messageHistory.findLast(
39+
({ role, content }) =>
40+
role === 'user' &&
41+
typeof content === 'string' &&
42+
parseUserMessage(content)
43+
)
44+
const lastUserInput = lastUserMessage
45+
? parseUserMessage(lastUserMessage.content as string)
46+
: undefined
5547

5648
// Initialize agent registry to ensure dynamic agents are available
5749
await agentRegistry.initialize(fileContext)
@@ -69,12 +61,13 @@ export async function formatPrompt(
6961
'agent'
7062
),
7163
[PLACEHOLDER.GIT_CHANGES_PROMPT]: getGitChangesPrompt(fileContext),
64+
[PLACEHOLDER.USER_INPUT_PROMPT]: lastUserInput ?? '',
7265
[PLACEHOLDER.REMAINING_STEPS]: `${agentState.stepsRemaining!}`,
7366
[PLACEHOLDER.PROJECT_ROOT]: fileContext.projectRoot,
7467
[PLACEHOLDER.SYSTEM_INFO_PROMPT]: getSystemInfoPrompt(fileContext),
7568
[PLACEHOLDER.TOOLS_PROMPT]: getToolsInstructions(tools, spawnableAgents),
7669
[PLACEHOLDER.USER_CWD]: fileContext.cwd,
77-
[PLACEHOLDER.INITIAL_AGENT_PROMPT]: processedPrompt,
70+
[PLACEHOLDER.INITIAL_AGENT_PROMPT]: intitialAgentPrompt ?? '',
7871
[PLACEHOLDER.KNOWLEDGE_FILES_CONTENTS]: renderToolResults(
7972
Object.entries({
8073
...Object.fromEntries(

backend/src/templates/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ const placeholderNames = [
7777
'SYSTEM_INFO_PROMPT',
7878
'TOOLS_PROMPT',
7979
'USER_CWD',
80+
'USER_INPUT_PROMPT',
8081
] as const
8182

8283
type PlaceholderType<T extends typeof placeholderNames> = {

backend/src/util/messages.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ export function coreMessagesWithSystem(
4242
export function asUserMessage(str: string): string {
4343
return `<user_message>${str}${closeXml('user_message')}`
4444
}
45+
export function parseUserMessage(str: string): string | undefined {
46+
const match = str.match(/<user_message>(.*?)<\/user_message>/s)
47+
return match ? match[1] : undefined
48+
}
4549

4650
export function asSystemInstruction(str: string): string {
4751
return `<system_instructions>${str}${closeXml('system_instructions')}`

0 commit comments

Comments
 (0)