Branchwell

OpenTalon / app / native / runtime / agent-setup.cjs

1const fs = require('node:fs/promises')
2const fsSync = require('node:fs')
3const { randomUUID } = require('node:crypto')
4const path = require('node:path')
5const { execFile } = require('node:child_process')
6const { promisify } = require('node:util')
7const { AppError } = require('./contracts.cjs')
8const run = promisify(execFile)
9
10function setupFields(config) {
11 const server = config.mcpServers.opentalon
12 return [
13 { id: 'name', label: 'Name', value: 'opentalon' },
14 { id: 'command', label: 'Command', value: server.command },
15 ...server.args.map((value, index) => ({
16 id: `argument-${index}`,
17 label: `Argument ${index + 1}`,
18 value,
19 })),
20 ...Object.entries(server.env).map(([name, value]) => ({ id: name, label: name, value })),
21 ]
22}
23const quote = (value) => `'${value.replaceAll("'", "'\\''")}'`
24function terminalCommand(config, client) {
25 const server = config.mcpServers.opentalon
26 const environment = Object.entries(server.env).flatMap(([name, value]) => ['--env', `${name}=${value}`])
27 if (client === 'gemini') {
28 return ['gemini', 'mcp', 'add', '--scope', 'project', '--transport', 'stdio', ...environment,
29 'opentalon', server.command, ...server.args].map(quote).join(' ')
30 }
31 const prefix =
32 client === 'codex'
33 ? ['codex', 'mcp', 'add', 'opentalon']
34 : client === 'claude-code'
35 ? ['claude', 'mcp', 'add', '--scope', 'user', '--transport', 'stdio', 'opentalon']
36 : null
37 if (!prefix) throw new AppError('INVALID_ARGUMENT', 'Choose Claude, Codex, or Gemini CLI.')
38 return [
39 ...prefix,
40 ...environment,
41 '--',
42 server.command,
43 ...server.args,
44 ]
45 .map(quote)
46 .join(' ')
47}
48async function saveClaudeExtension(destination, configFile, version) {
49 const staging = await fs.mkdtemp(path.join(path.dirname(destination), '.opentalon-extension-'))
50 try {
51 const manifest = {
52 manifest_version: '0.3',
53 name: 'opentalon',
54 display_name: 'OpenTalon',
55 icon: 'icon.png',
56 version,
57 description: 'Manage your local OpenTalon boards and tasks from Claude.',
58 author: { name: 'Protomota', url: 'https://protomota.com' },
59 server: {
60 type: 'node',
61 entry_point: 'mcp-extension.cjs',
62 mcp_config: {
63 command: 'node',
64 args: ['${__dirname}/mcp-extension.cjs'],
65 env: { OPENTALON_MCP_CONFIG: configFile },
66 },
67 },
68 compatibility: { platforms: ['darwin'] },
69 }
70 await fs.writeFile(path.join(staging, 'manifest.json'), JSON.stringify(manifest, null, 2), {
71 mode: 0o600,
72 })
73 await fs.copyFile(path.join(__dirname, 'mcp-bridge.cjs'), path.join(staging, 'mcp-bridge.cjs'))
74 await fs.copyFile(
75 path.join(__dirname, 'mcp-extension.cjs'),
76 path.join(staging, 'mcp-extension.cjs')
77 )
78 await fs.copyFile(
79 path.join(__dirname, '..', 'assets', 'opentalon-logo-1024.png'),
80 path.join(staging, 'icon.png')
81 )
82 const archive = path.join(staging, 'OpenTalon.mcpb')
83 await run(
84 '/usr/bin/zip',
85 ['-q', archive, 'manifest.json', 'mcp-extension.cjs', 'mcp-bridge.cjs', 'icon.png'],
86 {
87 cwd: staging,
88 }
89 )
90 await fs.copyFile(archive, destination)
91 return { path: destination }
92 } finally {
93 await fs.rm(staging, { recursive: true, force: true })
94 }
95}
96function readProjectConfig(file) {
97 try {
98 const stat = fsSync.lstatSync(file)
99 if (!stat.isFile() || stat.isSymbolicLink())
100 throw new AppError(
101 'INVALID_CONFIG',
102 'Choose a folder with a regular .mcp.json file, not a link.'
103 )
104 return fsSync.readFileSync(file, 'utf8')
105 } catch (error) {
106 if (error.code === 'ENOENT') return null
107 throw error
108 }
109}
110function prepareProjectSetup(folder, config) {
111 const file = path.join(folder, '.mcp.json')
112 const previous = readProjectConfig(file)
113 let existing
114 try {
115 existing = previous === null ? {} : JSON.parse(previous)
116 } catch {
117 throw new AppError(
118 'INVALID_CONFIG',
119 'The existing .mcp.json is not valid JSON. OpenTalon has not changed it.'
120 )
121 }
122 const object = (value) => value && typeof value === 'object' && !Array.isArray(value)
123 if (!object(existing) || (existing.mcpServers !== undefined && !object(existing.mcpServers)))
124 throw new AppError(
125 'INVALID_CONFIG',
126 'The existing .mcp.json needs an object containing mcpServers. OpenTalon has not changed it.'
127 )
128 const server = { type: 'stdio', ...config.mcpServers.opentalon }
129 return {
130 file,
131 previous,
132 replacing:
133 existing.mcpServers?.opentalon !== undefined &&
134 JSON.stringify(existing.mcpServers.opentalon) !== JSON.stringify(server),
135 next:
136 JSON.stringify(
137 { ...existing, mcpServers: { ...existing.mcpServers, opentalon: server } },
138 null,
139 2
140 ) + '\n',
141 }
142}
143function writeProjectSetup(setup) {
144 if (readProjectConfig(setup.file) !== setup.previous)
145 throw new AppError(
146 'CONFIG_CHANGED',
147 'The project connection file changed. Try again; your changes were preserved.'
148 )
149 const temporary = path.join(path.dirname(setup.file), `.opentalon-mcp-${randomUUID()}.tmp`)
150 try {
151 fsSync.writeFileSync(temporary, setup.next, { flag: 'wx', mode: 0o600 })
152 fsSync.renameSync(temporary, setup.file)
153 } finally {
154 if (fsSync.existsSync(temporary)) fsSync.unlinkSync(temporary)
155 }
156 return { file: setup.file }
157}
158module.exports = {
159 setupFields,
160 terminalCommand,
161 saveClaudeExtension,
162 prepareProjectSetup,
163 writeProjectSetup,
164}