| 479 | } |
| 480 | |
| 481 | export async function initialSessionState( |
| 482 | params: InitialSessionStateOptions, |
| 483 | ): Promise<SessionState> { |
| 484 | const { cwd, maxAgentSteps, skillsDir } = params |
| 485 | let { |
| 486 | agentDefinitions, |
| 487 | customToolDefinitions, |
| 488 | projectFiles, |
| 489 | knowledgeFiles, |
| 490 | userKnowledgeFiles: providedUserKnowledgeFiles, |
| 491 | fs, |
| 492 | spawn, |
| 493 | logger, |
| 494 | } = params |
| 495 | if (!agentDefinitions) { |
| 496 | agentDefinitions = [] |
| 497 | } |
| 498 | if (!customToolDefinitions) { |
| 499 | customToolDefinitions = [] |
| 500 | } |
| 501 | if (!fs) { |
| 502 | fs = (require('fs') as typeof fsType).promises |
| 503 | } |
| 504 | if (!spawn) { |
| 505 | const { spawn: nodeSpawn } = require('child_process') |
| 506 | spawn = nodeSpawn as CodebuffSpawn |
| 507 | } |
| 508 | if (!logger) { |
| 509 | logger = { |
| 510 | debug: () => {}, |
| 511 | info: () => {}, |
| 512 | warn: () => {}, |
| 513 | error: () => {}, |
| 514 | } |
| 515 | } |
| 516 | |
| 517 | let discoveredProject: |
| 518 | | { fileTree: FileTreeNode[]; filePaths: string[] } |
| 519 | | undefined |
| 520 | |
| 521 | // Auto-discover project files if not provided and cwd is available |
| 522 | if (projectFiles === undefined && cwd) { |
| 523 | discoveredProject = await discoverProjectPaths({ cwd, fs }) |
| 524 | } |
| 525 | if (knowledgeFiles === undefined) { |
| 526 | if (projectFiles) { |
| 527 | knowledgeFiles = deriveKnowledgeFiles(projectFiles) |
| 528 | } else if (cwd && discoveredProject) { |
| 529 | knowledgeFiles = await loadKnowledgeFilesFromPaths({ |
| 530 | cwd, |
| 531 | filePaths: discoveredProject.filePaths, |
| 532 | fs, |
| 533 | logger, |
| 534 | }) |
| 535 | } else { |
| 536 | knowledgeFiles = {} |
| 537 | } |
| 538 | } |