Skip to content

Commit 2fcbe70

Browse files
committed
Add AgentConfig as a type. accepts array of agent config objects
1 parent 9fe911c commit 2fcbe70

File tree

3 files changed

+37
-9
lines changed

3 files changed

+37
-9
lines changed

sdk/README.md

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,17 @@ const run2 = await client.run({
6060
// event includes streamed updates like assistant messages and tool calls
6161
console.log('event:', event)
6262
},
63+
64+
// Custom agents (optional)
65+
agentConfigs: [
66+
{
67+
id: 'my-awesome-agent',
68+
model: 'openai/gpt-5',
69+
displayName: 'My awesome agent'
70+
instructionsPrompt: 'Do something awesome'
71+
// ... other AgentConfig properties
72+
},
73+
],
6374
})
6475
```
6576

@@ -85,13 +96,13 @@ Runs a Codebuff agent with the specified options.
8596

8697
- **`knowledgeFiles`** (object, optional): Knowledge files to inject into every `run()` call. Uses the same schema as `projectFiles` - keys are file paths and values are file contents. These files are added directly to the agent's context.
8798

88-
- **`agentConfig`** (object, optional): If you defined your own custom agent, pass the agent configuration here. The key should be the agent ID (e.g., 'my-custom-agent'), and the value should be the compiled agent configuration. We should provide a utility function to load and compile agents in the future to make this easier.
89-
99+
- **`agentConfigs`** (array, optional): Array of custom agent configurations. Each object should satisfy the AgentConfig type.
90100
- **`maxAgentSteps`** (number, optional): Maximum number of steps the agent can take before stopping. Use this as a safety measure in case your agent starts going off the rails. A reasonable number is around 20.
91101

92102
#### Returns
93103

94104
Returns a Promise that resolves to a `RunState` object containing:
105+
95106
- `sessionState`: The current session state that can be passed to subsequent runs
96107
- `toolResults`: Results from any tools that were executed during the run
97108

sdk/src/client.ts

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { getInitialSessionState } from '../../common/src/types/session-state'
1414

1515
import type { PrintModeEvent } from '../../common/src/types/print-mode'
1616
import type { SessionState } from '../../common/src/types/session-state'
17+
import type { AgentConfig } from '../../common/src/util/types/agent-config'
1718

1819
type ClientToolName = 'write_file' | 'run_terminal_command'
1920

@@ -116,7 +117,7 @@ export class CodebuffClient {
116117
* @param previousRun - (Optional) JSON state returned from a previous run() call. Use this to continue a conversation or session with the agent, maintaining context from previous interactions.
117118
* @param projectFiles - (Optional) All the files in your project as a plain JavaScript object. Keys should be the full path from your current directory to each file, and values should be the string contents of the file. Example: { "src/index.ts": "console.log('hi')" }. This helps Codebuff pick good source files for context.
118119
* @param knowledgeFiles - (Optional) Knowledge files to inject into every run() call. Uses the same schema as projectFiles - keys are file paths and values are file contents. These files are added directly to the agent's context.
119-
* @param agentConfig - (Optional) If you defined your own custom agent, pass the agent configuration here. The key should be the agent ID (e.g., 'my-custom-agent'), and the value should be the compiled agent configuration. We will provide a utility function to load and compile agents in the future to make this easier.
120+
* @param agentConfigs - (Optional) Array of custom agent configurations. Each object should satisfy the AgentConfig type.
120121
* @param maxAgentSteps - (Optional) Maximum number of steps the agent can take before stopping. Use this as a safety measure in case your agent starts going off the rails. A reasonable number is around 20.
121122
*
122123
* @returns A Promise that resolves to a RunState JSON object which you can pass to a subsequent run() call to continue the run.
@@ -129,7 +130,7 @@ export class CodebuffClient {
129130
previousRun,
130131
projectFiles,
131132
knowledgeFiles,
132-
agentConfig,
133+
agentConfigs,
133134
maxAgentSteps,
134135
}: {
135136
agent: string
@@ -139,7 +140,7 @@ export class CodebuffClient {
139140
previousRun?: RunState
140141
projectFiles?: Record<string, string>
141142
knowledgeFiles?: Record<string, string>
142-
agentConfig?: Record<string, any>
143+
agentConfigs?: AgentConfig[]
143144
maxAgentSteps?: number
144145
}): Promise<RunState> {
145146
await this.websocketHandler.connect()
@@ -149,7 +150,7 @@ export class CodebuffClient {
149150
previousRun?.sessionState ??
150151
initialSessionState(this.cwd, {
151152
knowledgeFiles,
152-
agentConfig,
153+
agentConfigs,
153154
projectFiles,
154155
maxAgentSteps,
155156
})
@@ -271,11 +272,26 @@ function initialSessionState(
271272
// TODO: Parse projectFiles into fileTree, fileTokenScores, tokenCallers
272273
projectFiles?: Record<string, string>
273274
knowledgeFiles?: Record<string, string>
274-
agentConfig?: Record<string, any>
275+
agentConfigs?: AgentConfig[]
275276
maxAgentSteps?: number
276277
},
277278
) {
278-
const { knowledgeFiles = {}, agentConfig = {} } = options
279+
const { knowledgeFiles = {}, agentConfigs = [] } = options
280+
281+
// Process agentConfigs array and convert handleSteps functions to strings
282+
const processedAgentTemplates: Record<string, any> = {}
283+
agentConfigs.forEach((config) => {
284+
const processedConfig = { ...config } as Record<string, any>
285+
if (
286+
processedConfig.handleSteps &&
287+
typeof processedConfig.handleSteps === 'function'
288+
) {
289+
processedConfig.handleSteps = processedConfig.handleSteps.toString()
290+
}
291+
if (processedConfig.id) {
292+
processedAgentTemplates[processedConfig.id] = processedConfig
293+
}
294+
})
279295

280296
const initialState = getInitialSessionState({
281297
projectRoot: cwd,
@@ -285,7 +301,7 @@ function initialSessionState(
285301
tokenCallers: {},
286302
knowledgeFiles,
287303
userKnowledgeFiles: {},
288-
agentTemplates: agentConfig,
304+
agentTemplates: processedAgentTemplates,
289305
gitChanges: {
290306
status: '',
291307
diff: '',

sdk/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export { CodebuffClient } from './client'
22
export { WebSocketHandler } from './websocket-client'
33
export { getInitialSessionState } from '../../common/src/types/session-state'
4+
export { AgentConfig } from '../../common/src/util/types/agent-config'

0 commit comments

Comments
 (0)