Skip to content

Commit b1420f3

Browse files
committed
Support legacy initialSessionState signature
1 parent f3c287a commit b1420f3

File tree

1 file changed

+39
-13
lines changed

1 file changed

+39
-13
lines changed

sdk/src/run-state.ts

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,16 @@ export type RunState = {
2828
output: AgentOutput
2929
}
3030

31+
export type InitialSessionStateOptions = {
32+
cwd?: string
33+
projectFiles?: Record<string, string>
34+
knowledgeFiles?: Record<string, string>
35+
agentDefinitions?: AgentDefinition[]
36+
customToolDefinitions?: CustomToolDefinition[]
37+
maxAgentSteps?: number
38+
fs?: CodebuffFileSystem
39+
};
40+
3141
/**
3242
* Processes agent definitions array and converts handleSteps functions to strings
3343
*/
@@ -155,16 +165,32 @@ function deriveKnowledgeFiles(
155165
return knowledgeFiles
156166
}
157167

158-
export async function initialSessionState(options: {
159-
cwd: string | undefined
160-
projectFiles?: Record<string, string>
161-
knowledgeFiles?: Record<string, string>
162-
agentDefinitions?: AgentDefinition[]
163-
customToolDefinitions?: CustomToolDefinition[]
164-
maxAgentSteps?: number
165-
fs: CodebuffFileSystem
166-
}) {
167-
let { projectFiles, agentDefinitions = [], cwd, knowledgeFiles, fs } = options
168+
export function initialSessionState(
169+
options: InitialSessionStateOptions,
170+
): Promise<SessionState>;
171+
export function initialSessionState(
172+
cwd: string,
173+
options?: Omit<InitialSessionStateOptions, 'cwd'>,
174+
): Promise<SessionState>;
175+
export async function initialSessionState(
176+
arg1: string | InitialSessionStateOptions,
177+
arg2?: Omit<InitialSessionStateOptions, 'cwd'>,
178+
): Promise<SessionState> {
179+
const options: InitialSessionStateOptions =
180+
typeof arg1 === 'string' ? { ...(arg2 ?? {}), cwd: arg1 } : arg1 ?? {}
181+
182+
const cwd = options.cwd
183+
const agentDefinitions = options.agentDefinitions ?? []
184+
const customToolDefinitions = options.customToolDefinitions ?? []
185+
const maxAgentSteps = options.maxAgentSteps
186+
187+
let projectFiles = options.projectFiles
188+
let knowledgeFiles = options.knowledgeFiles
189+
let fs: CodebuffFileSystem | undefined = options.fs
190+
191+
if (!fs) {
192+
fs = (await import('fs')) as unknown as CodebuffFileSystem
193+
}
168194

169195
// Auto-discover project files if not provided and cwd is available
170196
if (projectFiles === undefined && cwd) {
@@ -176,7 +202,7 @@ export async function initialSessionState(options: {
176202

177203
const processedAgentTemplates = processAgentDefinitions(agentDefinitions)
178204
const processedCustomToolDefinitions = processCustomToolDefinitions(
179-
options.customToolDefinitions ?? [],
205+
customToolDefinitions,
180206
)
181207

182208
// Generate file tree and token scores from projectFiles if available
@@ -219,8 +245,8 @@ export async function initialSessionState(options: {
219245
},
220246
})
221247

222-
if (options.maxAgentSteps) {
223-
initialState.mainAgentState.stepsRemaining = options.maxAgentSteps
248+
if (maxAgentSteps) {
249+
initialState.mainAgentState.stepsRemaining = maxAgentSteps
224250
}
225251

226252
return initialState

0 commit comments

Comments
 (0)