|
| 1 | +import * as os from 'os' |
| 2 | + |
| 3 | +import { getInitialSessionState } from '../../common/src/types/session-state' |
| 4 | + |
| 5 | +import type { ServerAction } from '../../common/src/actions' |
| 6 | +import type { AgentDefinition } from '../../common/src/templates/initial-agents-dir/types/agent-definition' |
| 7 | +import type { CodebuffMessage } from '../../common/src/types/message' |
| 8 | +import type { SessionState } from '../../common/src/types/session-state' |
| 9 | + |
| 10 | +export type RunState = { |
| 11 | + sessionState: SessionState |
| 12 | + toolResults: ServerAction<'prompt-response'>['toolResults'] |
| 13 | +} |
| 14 | + |
| 15 | +export function initialSessionState( |
| 16 | + cwd: string, |
| 17 | + options: { |
| 18 | + // TODO: Parse projectFiles into fileTree, fileTokenScores, tokenCallers |
| 19 | + projectFiles?: Record<string, string> |
| 20 | + knowledgeFiles?: Record<string, string> |
| 21 | + agentDefinitions?: AgentDefinition[] |
| 22 | + maxAgentSteps?: number |
| 23 | + }, |
| 24 | +) { |
| 25 | + const { knowledgeFiles = {}, agentDefinitions = [] } = options |
| 26 | + |
| 27 | + // Process agentDefinitions array and convert handleSteps functions to strings |
| 28 | + const processedAgentTemplates: Record<string, any> = {} |
| 29 | + agentDefinitions.forEach((definition) => { |
| 30 | + const processedConfig = { ...definition } as Record<string, any> |
| 31 | + if ( |
| 32 | + processedConfig.handleSteps && |
| 33 | + typeof processedConfig.handleSteps === 'function' |
| 34 | + ) { |
| 35 | + processedConfig.handleSteps = processedConfig.handleSteps.toString() |
| 36 | + } |
| 37 | + if (processedConfig.id) { |
| 38 | + processedAgentTemplates[processedConfig.id] = processedConfig |
| 39 | + } |
| 40 | + }) |
| 41 | + |
| 42 | + const initialState = getInitialSessionState({ |
| 43 | + projectRoot: cwd, |
| 44 | + cwd, |
| 45 | + fileTree: [], |
| 46 | + fileTokenScores: {}, |
| 47 | + tokenCallers: {}, |
| 48 | + knowledgeFiles, |
| 49 | + userKnowledgeFiles: {}, |
| 50 | + agentTemplates: processedAgentTemplates, |
| 51 | + gitChanges: { |
| 52 | + status: '', |
| 53 | + diff: '', |
| 54 | + diffCached: '', |
| 55 | + lastCommitMessages: '', |
| 56 | + }, |
| 57 | + changesSinceLastChat: {}, |
| 58 | + shellConfigFiles: {}, |
| 59 | + systemInfo: { |
| 60 | + platform: process.platform, |
| 61 | + shell: process.platform === 'win32' ? 'cmd.exe' : 'bash', |
| 62 | + nodeVersion: process.version, |
| 63 | + arch: process.arch, |
| 64 | + homedir: os.homedir(), |
| 65 | + cpus: os.cpus().length ?? 1, |
| 66 | + }, |
| 67 | + }) |
| 68 | + |
| 69 | + if (options.maxAgentSteps) { |
| 70 | + initialState.mainAgentState.stepsRemaining = options.maxAgentSteps |
| 71 | + } |
| 72 | + |
| 73 | + return initialState |
| 74 | +} |
| 75 | + |
| 76 | +export function generateInitialRunState({ |
| 77 | + cwd, |
| 78 | + projectFiles, |
| 79 | + knowledgeFiles, |
| 80 | + agentDefinitions, |
| 81 | + maxAgentSteps, |
| 82 | +}: { |
| 83 | + cwd: string |
| 84 | + projectFiles?: Record<string, string> |
| 85 | + knowledgeFiles?: Record<string, string> |
| 86 | + agentDefinitions?: AgentDefinition[] |
| 87 | + maxAgentSteps?: number |
| 88 | +}): RunState { |
| 89 | + return { |
| 90 | + sessionState: initialSessionState(cwd, { |
| 91 | + projectFiles, |
| 92 | + knowledgeFiles, |
| 93 | + agentDefinitions, |
| 94 | + maxAgentSteps, |
| 95 | + }), |
| 96 | + toolResults: [], |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +export function withAdditionalMessage({ |
| 101 | + runState, |
| 102 | + message, |
| 103 | +}: { |
| 104 | + runState: RunState |
| 105 | + message: CodebuffMessage |
| 106 | +}): RunState { |
| 107 | + // Deep copy |
| 108 | + const newRunState = JSON.parse(JSON.stringify(runState)) as typeof runState |
| 109 | + |
| 110 | + newRunState.sessionState.mainAgentState.messageHistory.push(message) |
| 111 | + |
| 112 | + return newRunState |
| 113 | +} |
| 114 | + |
| 115 | +export function withMessageHistory({ |
| 116 | + runState, |
| 117 | + messages, |
| 118 | +}: { |
| 119 | + runState: RunState |
| 120 | + messages: CodebuffMessage[] |
| 121 | +}): RunState { |
| 122 | + // Deep copy |
| 123 | + const newRunState = JSON.parse(JSON.stringify(runState)) as typeof runState |
| 124 | + |
| 125 | + newRunState.sessionState.mainAgentState.messageHistory = messages |
| 126 | + |
| 127 | + return newRunState |
| 128 | +} |
0 commit comments