Branchwell

OpenTalon / app · opentalon.ai / native / runtime / agent-readiness.cjs

1const fs = require('node:fs')
2const path = require('node:path')
3const { AppError } = require('./contracts.cjs')
4
5function agentWorkingDirectory(folder) {
6 try {
7 if (typeof folder !== 'string' || !path.isAbsolute(folder) || !fs.statSync(folder).isDirectory())
8 throw new Error('Unavailable working folder')
9 return fs.realpathSync(folder)
10 } catch {
11 throw new AppError('AGENT_SETUP_REQUIRED', 'The agent folder is unavailable. Choose an existing folder in the project’s Agent folder settings, or use the global folder in Settings.')
12 }
13}
14
15function assertAgentReady(settings, runtimes, connection) {
16 if (!settings.workingFolder)
17 throw new AppError(
18 'AGENT_SETUP_REQUIRED',
19 'Choose where your agent should work before sending this task.'
20 )
21 try {
22 if (!fs.statSync(settings.workingFolder).isDirectory()) throw new Error('Not a folder')
23 } catch {
24 throw new AppError(
25 'AGENT_SETUP_REQUIRED',
26 'The working folder is unavailable. Choose another folder to send this task.'
27 )
28 }
29 if (!runtimes[settings.defaultAgent]?.installed)
30 throw new AppError(
31 'AGENT_NOT_INSTALLED',
32 'Install your selected agent’s launcher and sign in, or choose another agent.'
33 )
34 if (!connection.running)
35 throw new AppError('MCP_DISABLED', 'Enable the local agent connection to send this task.')
36}
37module.exports = { assertAgentReady, agentWorkingDirectory }