@link-assistant/agent 0.18.3 → 0.19.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,1146 @@
1
+ import { Log } from '../util/log';
2
+ import path from 'path';
3
+ import os from 'os';
4
+ import z from 'zod';
5
+ import { Filesystem } from '../util/filesystem';
6
+ import { ModelsDev } from '../provider/models';
7
+ import { mergeDeep, pipe } from 'remeda';
8
+ import { Global } from '../global';
9
+ import fs from 'fs/promises';
10
+ import { lazy } from '../util/lazy';
11
+ import { NamedError } from '../util/error';
12
+ import { config } from './config';
13
+ import { Auth } from '../auth';
14
+ import { createVerboseFetch } from '../util/verbose-fetch';
15
+ import {
16
+ type ParseError as JsoncParseError,
17
+ parse as parseJsonc,
18
+ printParseErrorCode,
19
+ } from 'jsonc-parser';
20
+ import { Instance } from '../project/instance';
21
+ import { ConfigMarkdown } from './markdown';
22
+
23
+ export namespace Config {
24
+ const log = Log.create({ service: 'config' });
25
+ const verboseFetch = createVerboseFetch(fetch, { caller: 'config' });
26
+
27
+ /**
28
+ * Automatically migrate .opencode directories to .link-assistant-agent
29
+ * This ensures a smooth transition for both file system configs and environment variables.
30
+ * Once .link-assistant-agent exists, we stop reading from .opencode.
31
+ */
32
+ async function migrateConfigDirectories() {
33
+ // Find all .opencode and .link-assistant-agent directories in the project hierarchy
34
+ const allDirs = await Array.fromAsync(
35
+ Filesystem.up({
36
+ targets: ['.link-assistant-agent', '.opencode'],
37
+ start: Instance.directory,
38
+ stop: Instance.worktree,
39
+ })
40
+ );
41
+
42
+ const newConfigDirs = allDirs.filter((d) =>
43
+ d.endsWith('.link-assistant-agent')
44
+ );
45
+ const oldConfigDirs = allDirs.filter((d) => d.endsWith('.opencode'));
46
+
47
+ // For each old config directory, check if there's a corresponding new one
48
+ for (const oldDir of oldConfigDirs) {
49
+ const parentDir = path.dirname(oldDir);
50
+ const newDir = path.join(parentDir, '.link-assistant-agent');
51
+
52
+ // Check if the new directory already exists at the same level
53
+ const newDirExists = newConfigDirs.includes(newDir);
54
+
55
+ if (!newDirExists) {
56
+ try {
57
+ // Perform migration by copying the entire directory
58
+ log.info(() => ({
59
+ message: `Migrating config from ${oldDir} to ${newDir} for smooth transition`,
60
+ }));
61
+
62
+ // Use fs-extra style recursive copy
63
+ await copyDirectory(oldDir, newDir);
64
+
65
+ log.info(() => ({
66
+ message: `Successfully migrated config to ${newDir}`,
67
+ }));
68
+ } catch (error) {
69
+ log.error(() => ({
70
+ message: `Failed to migrate config from ${oldDir}:`,
71
+ error,
72
+ }));
73
+ // Don't throw - allow the app to continue with the old config
74
+ }
75
+ }
76
+ }
77
+
78
+ // Also migrate global config if needed
79
+ const oldGlobalPath = path.join(os.homedir(), '.config', 'opencode');
80
+ const newGlobalPath = Global.Path.config;
81
+
82
+ try {
83
+ const oldGlobalExists = await fs
84
+ .stat(oldGlobalPath)
85
+ .then(() => true)
86
+ .catch(() => false);
87
+ const newGlobalExists = await fs
88
+ .stat(newGlobalPath)
89
+ .then(() => true)
90
+ .catch(() => false);
91
+
92
+ if (oldGlobalExists && !newGlobalExists) {
93
+ log.info(() => ({
94
+ message: `Migrating global config from ${oldGlobalPath} to ${newGlobalPath}`,
95
+ }));
96
+ await copyDirectory(oldGlobalPath, newGlobalPath);
97
+ log.info(() => ({
98
+ message: `Successfully migrated global config to ${newGlobalPath}`,
99
+ }));
100
+ }
101
+ } catch (error) {
102
+ log.error(() => ({
103
+ message: 'Failed to migrate global config:',
104
+ error,
105
+ }));
106
+ // Don't throw - allow the app to continue
107
+ }
108
+ }
109
+
110
+ /**
111
+ * Recursively copy a directory and all its contents
112
+ */
113
+ async function copyDirectory(src: string, dest: string) {
114
+ // Create destination directory
115
+ await fs.mkdir(dest, { recursive: true });
116
+
117
+ // Read all entries in source directory
118
+ const entries = await fs.readdir(src, { withFileTypes: true });
119
+
120
+ for (const entry of entries) {
121
+ const srcPath = path.join(src, entry.name);
122
+ const destPath = path.join(dest, entry.name);
123
+
124
+ if (entry.isDirectory()) {
125
+ // Recursively copy subdirectories
126
+ await copyDirectory(srcPath, destPath);
127
+ } else if (entry.isFile() || entry.isSymbolicLink()) {
128
+ // Copy files
129
+ await fs.copyFile(srcPath, destPath);
130
+ }
131
+ }
132
+ }
133
+
134
+ export const state = Instance.state(async () => {
135
+ const auth = await Auth.all();
136
+ let result = await global();
137
+
138
+ // Override with custom config if provided
139
+ if (config.config) {
140
+ result = mergeDeep(result, await loadFile(config.config));
141
+ log.debug(() => ({
142
+ message: 'loaded custom config',
143
+ path: config.config,
144
+ }));
145
+ }
146
+
147
+ for (const file of ['opencode.jsonc', 'opencode.json']) {
148
+ const found = await Filesystem.findUp(
149
+ file,
150
+ Instance.directory,
151
+ Instance.worktree
152
+ );
153
+ for (const resolved of found.toReversed()) {
154
+ result = mergeDeep(result, await loadFile(resolved));
155
+ }
156
+ }
157
+
158
+ if (config.configContent) {
159
+ result = mergeDeep(result, JSON.parse(config.configContent));
160
+ log.debug(() => ({
161
+ message:
162
+ 'loaded custom config from LINK_ASSISTANT_AGENT_CONFIG_CONTENT',
163
+ }));
164
+ }
165
+
166
+ for (const [key, value] of Object.entries(auth)) {
167
+ if (value.type === 'wellknown') {
168
+ process.env[value.key] = value.token;
169
+ const wellknown = (await verboseFetch(
170
+ `${key}/.well-known/opencode`
171
+ ).then((x) => x.json())) as any;
172
+ result = mergeDeep(
173
+ result,
174
+ await load(JSON.stringify(wellknown.config ?? {}), process.cwd())
175
+ );
176
+ }
177
+ }
178
+
179
+ result.agent = result.agent || {};
180
+ result.mode = result.mode || {};
181
+
182
+ // Perform automatic migration from .opencode to .link-assistant-agent if needed
183
+ await migrateConfigDirectories();
184
+
185
+ // Find all config directories
186
+ const foundDirs = await Array.fromAsync(
187
+ Filesystem.up({
188
+ targets: ['.link-assistant-agent', '.opencode'],
189
+ start: Instance.directory,
190
+ stop: Instance.worktree,
191
+ })
192
+ );
193
+
194
+ // Check if any .link-assistant-agent directory exists
195
+ const hasNewConfig = foundDirs.some((d) =>
196
+ d.endsWith('.link-assistant-agent')
197
+ );
198
+
199
+ // Filter out .opencode directories if .link-assistant-agent exists
200
+ const filteredDirs = foundDirs.filter((dir) => {
201
+ // If .link-assistant-agent exists, exclude .opencode directories
202
+ if (hasNewConfig && dir.endsWith('.opencode')) {
203
+ log.debug(() => ({
204
+ message:
205
+ 'Skipping .opencode directory (using .link-assistant-agent):',
206
+ path: dir,
207
+ }));
208
+ return false;
209
+ }
210
+ return true;
211
+ });
212
+
213
+ const directories = [Global.Path.config, ...filteredDirs];
214
+
215
+ if (config.configDir) {
216
+ directories.push(config.configDir);
217
+ log.debug(() => ({
218
+ message: 'loading config from LINK_ASSISTANT_AGENT_CONFIG_DIR',
219
+ path: config.configDir,
220
+ }));
221
+ }
222
+
223
+ const promises: Promise<void>[] = [];
224
+ for (const dir of directories) {
225
+ await assertValid(dir);
226
+
227
+ if (
228
+ dir.endsWith('.link-assistant-agent') ||
229
+ dir.endsWith('.opencode') ||
230
+ dir === config.configDir
231
+ ) {
232
+ for (const file of ['opencode.jsonc', 'opencode.json']) {
233
+ log.debug(() => ({
234
+ message: `loading config from ${path.join(dir, file)}`,
235
+ }));
236
+ result = mergeDeep(result, await loadFile(path.join(dir, file)));
237
+ // to satisy the type checker
238
+ result.agent ??= {};
239
+ result.mode ??= {};
240
+ }
241
+ }
242
+
243
+ promises.push(installDependencies(dir));
244
+ result.command = mergeDeep(result.command ?? {}, await loadCommand(dir));
245
+ result.agent = mergeDeep(result.agent, await loadAgent(dir));
246
+ result.agent = mergeDeep(result.agent, await loadMode(dir));
247
+ }
248
+ await Promise.allSettled(promises);
249
+
250
+ // Migrate deprecated mode field to agent field
251
+ for (const [name, mode] of Object.entries(result.mode)) {
252
+ result.agent = mergeDeep(result.agent ?? {}, {
253
+ [name]: {
254
+ ...mode,
255
+ mode: 'primary' as const,
256
+ },
257
+ });
258
+ }
259
+
260
+ // Permission system removed
261
+ // Share/autoshare removed - no sharing support
262
+
263
+ if (!result.username) result.username = os.userInfo().username;
264
+
265
+ if (!result.keybinds) result.keybinds = Info.shape.keybinds.parse({});
266
+
267
+ return {
268
+ config: result,
269
+ directories,
270
+ };
271
+ });
272
+
273
+ const INVALID_DIRS = new Bun.Glob(
274
+ `{${['agents', 'commands', 'tools'].join(',')}}/`
275
+ );
276
+ async function assertValid(dir: string) {
277
+ const invalid = await Array.fromAsync(
278
+ INVALID_DIRS.scan({
279
+ onlyFiles: false,
280
+ cwd: dir,
281
+ })
282
+ );
283
+ for (const item of invalid) {
284
+ throw new ConfigDirectoryTypoError({
285
+ path: dir,
286
+ dir: item,
287
+ suggestion: item.substring(0, item.length - 1),
288
+ });
289
+ }
290
+ }
291
+
292
+ async function installDependencies(dir: string) {
293
+ // Dependency installation removed - no plugin support
294
+ }
295
+
296
+ const COMMAND_GLOB = new Bun.Glob('command/**/*.md');
297
+ async function loadCommand(dir: string) {
298
+ const result: Record<string, Command> = {};
299
+ for await (const item of COMMAND_GLOB.scan({
300
+ absolute: true,
301
+ followSymlinks: true,
302
+ dot: true,
303
+ cwd: dir,
304
+ })) {
305
+ const md = await ConfigMarkdown.parse(item);
306
+ if (!md.data) continue;
307
+
308
+ const name = (() => {
309
+ const patterns = [
310
+ '/.link-assistant-agent/command/',
311
+ '/.opencode/command/',
312
+ '/command/',
313
+ ];
314
+ const pattern = patterns.find((p) => item.includes(p));
315
+
316
+ if (pattern) {
317
+ const index = item.indexOf(pattern);
318
+ return item.slice(index + pattern.length, -3);
319
+ }
320
+ return path.basename(item, '.md');
321
+ })();
322
+
323
+ const config = {
324
+ name,
325
+ ...md.data,
326
+ template: md.content.trim(),
327
+ };
328
+ const parsed = Command.safeParse(config);
329
+ if (parsed.success) {
330
+ result[config.name] = parsed.data;
331
+ continue;
332
+ }
333
+ throw new InvalidError({ path: item }, { cause: parsed.error });
334
+ }
335
+ return result;
336
+ }
337
+
338
+ const AGENT_GLOB = new Bun.Glob('agent/**/*.md');
339
+ async function loadAgent(dir: string) {
340
+ const result: Record<string, Agent> = {};
341
+
342
+ for await (const item of AGENT_GLOB.scan({
343
+ absolute: true,
344
+ followSymlinks: true,
345
+ dot: true,
346
+ cwd: dir,
347
+ })) {
348
+ const md = await ConfigMarkdown.parse(item);
349
+ if (!md.data) continue;
350
+
351
+ // Extract relative path from agent folder for nested agents
352
+ let agentName = path.basename(item, '.md');
353
+ const agentFolderPath = item.includes('/.link-assistant-agent/agent/')
354
+ ? item.split('/.link-assistant-agent/agent/')[1]
355
+ : item.includes('/.opencode/agent/')
356
+ ? item.split('/.opencode/agent/')[1]
357
+ : item.includes('/agent/')
358
+ ? item.split('/agent/')[1]
359
+ : agentName + '.md';
360
+
361
+ // If agent is in a subfolder, include folder path in name
362
+ if (agentFolderPath.includes('/')) {
363
+ const relativePath = agentFolderPath.replace('.md', '');
364
+ const pathParts = relativePath.split('/');
365
+ agentName =
366
+ pathParts.slice(0, -1).join('/') +
367
+ '/' +
368
+ pathParts[pathParts.length - 1];
369
+ }
370
+
371
+ const config = {
372
+ name: agentName,
373
+ ...md.data,
374
+ prompt: md.content.trim(),
375
+ };
376
+ const parsed = Agent.safeParse(config);
377
+ if (parsed.success) {
378
+ result[config.name] = parsed.data;
379
+ continue;
380
+ }
381
+ throw new InvalidError({ path: item }, { cause: parsed.error });
382
+ }
383
+ return result;
384
+ }
385
+
386
+ const MODE_GLOB = new Bun.Glob('mode/*.md');
387
+ async function loadMode(dir: string) {
388
+ const result: Record<string, Agent> = {};
389
+ for await (const item of MODE_GLOB.scan({
390
+ absolute: true,
391
+ followSymlinks: true,
392
+ dot: true,
393
+ cwd: dir,
394
+ })) {
395
+ const md = await ConfigMarkdown.parse(item);
396
+ if (!md.data) continue;
397
+
398
+ const config = {
399
+ name: path.basename(item, '.md'),
400
+ ...md.data,
401
+ prompt: md.content.trim(),
402
+ };
403
+ const parsed = Agent.safeParse(config);
404
+ if (parsed.success) {
405
+ result[config.name] = {
406
+ ...parsed.data,
407
+ mode: 'primary' as const,
408
+ };
409
+ continue;
410
+ }
411
+ }
412
+ return result;
413
+ }
414
+
415
+ export const McpLocal = z
416
+ .object({
417
+ type: z.literal('local').describe('Type of MCP server connection'),
418
+ command: z
419
+ .string()
420
+ .array()
421
+ .describe('Command and arguments to run the MCP server'),
422
+ environment: z
423
+ .record(z.string(), z.string())
424
+ .optional()
425
+ .describe('Environment variables to set when running the MCP server'),
426
+ enabled: z
427
+ .boolean()
428
+ .optional()
429
+ .describe('Enable or disable the MCP server on startup'),
430
+ timeout: z
431
+ .number()
432
+ .int()
433
+ .positive()
434
+ .optional()
435
+ .describe(
436
+ 'Timeout in ms for fetching tools from the MCP server. Defaults to 5000 (5 seconds) if not specified.'
437
+ ),
438
+ tool_call_timeout: z
439
+ .number()
440
+ .int()
441
+ .positive()
442
+ .optional()
443
+ .describe(
444
+ 'Default timeout in ms for MCP tool execution. Defaults to 120000 (2 minutes) if not specified. Set per-tool overrides in tool_timeouts.'
445
+ ),
446
+ tool_timeouts: z
447
+ .record(z.string(), z.number().int().positive())
448
+ .optional()
449
+ .describe(
450
+ 'Per-tool timeout overrides in ms. Keys are tool names (e.g., "browser_run_code": 300000 for 5 minutes).'
451
+ ),
452
+ })
453
+ .strict()
454
+ .meta({
455
+ ref: 'McpLocalConfig',
456
+ });
457
+
458
+ export const McpRemote = z
459
+ .object({
460
+ type: z.literal('remote').describe('Type of MCP server connection'),
461
+ url: z.string().describe('URL of the remote MCP server'),
462
+ enabled: z
463
+ .boolean()
464
+ .optional()
465
+ .describe('Enable or disable the MCP server on startup'),
466
+ headers: z
467
+ .record(z.string(), z.string())
468
+ .optional()
469
+ .describe('Headers to send with the request'),
470
+ timeout: z
471
+ .number()
472
+ .int()
473
+ .positive()
474
+ .optional()
475
+ .describe(
476
+ 'Timeout in ms for fetching tools from the MCP server. Defaults to 5000 (5 seconds) if not specified.'
477
+ ),
478
+ tool_call_timeout: z
479
+ .number()
480
+ .int()
481
+ .positive()
482
+ .optional()
483
+ .describe(
484
+ 'Default timeout in ms for MCP tool execution. Defaults to 120000 (2 minutes) if not specified. Set per-tool overrides in tool_timeouts.'
485
+ ),
486
+ tool_timeouts: z
487
+ .record(z.string(), z.number().int().positive())
488
+ .optional()
489
+ .describe(
490
+ 'Per-tool timeout overrides in ms. Keys are tool names (e.g., "browser_run_code": 300000 for 5 minutes).'
491
+ ),
492
+ })
493
+ .strict()
494
+ .meta({
495
+ ref: 'McpRemoteConfig',
496
+ });
497
+
498
+ export const Mcp = z.discriminatedUnion('type', [McpLocal, McpRemote]);
499
+ export type Mcp = z.infer<typeof Mcp>;
500
+
501
+ export const Permission = z.enum(['ask', 'allow', 'deny']);
502
+ export type Permission = z.infer<typeof Permission>;
503
+
504
+ export const Command = z.object({
505
+ template: z.string(),
506
+ description: z.string().optional(),
507
+ agent: z.string().optional(),
508
+ model: z.string().optional(),
509
+ subtask: z.boolean().optional(),
510
+ });
511
+ export type Command = z.infer<typeof Command>;
512
+
513
+ export const Agent = z
514
+ .object({
515
+ model: z.string().optional(),
516
+ temperature: z.number().optional(),
517
+ top_p: z.number().optional(),
518
+ prompt: z.string().optional(),
519
+ tools: z.record(z.string(), z.boolean()).optional(),
520
+ disable: z.boolean().optional(),
521
+ description: z
522
+ .string()
523
+ .optional()
524
+ .describe('Description of when to use the agent'),
525
+ mode: z.enum(['subagent', 'primary', 'all']).optional(),
526
+ color: z
527
+ .string()
528
+ .regex(/^#[0-9a-fA-F]{6}$/, 'Invalid hex color format')
529
+ .optional()
530
+ .describe('Hex color code for the agent (e.g., #FF5733)'),
531
+ permission: z
532
+ .object({
533
+ edit: Permission.optional(),
534
+ bash: z
535
+ .union([Permission, z.record(z.string(), Permission)])
536
+ .optional(),
537
+ webfetch: Permission.optional(),
538
+ doom_loop: Permission.optional(),
539
+ external_directory: Permission.optional(),
540
+ })
541
+ .optional(),
542
+ })
543
+ .catchall(z.any())
544
+ .meta({
545
+ ref: 'AgentConfig',
546
+ });
547
+ export type Agent = z.infer<typeof Agent>;
548
+
549
+ export const Keybinds = z
550
+ .object({
551
+ leader: z
552
+ .string()
553
+ .optional()
554
+ .default('ctrl+x')
555
+ .describe('Leader key for keybind combinations'),
556
+ app_exit: z
557
+ .string()
558
+ .optional()
559
+ .default('ctrl+c,ctrl+d,<leader>q')
560
+ .describe('Exit the application'),
561
+ editor_open: z
562
+ .string()
563
+ .optional()
564
+ .default('<leader>e')
565
+ .describe('Open external editor'),
566
+ theme_list: z
567
+ .string()
568
+ .optional()
569
+ .default('<leader>t')
570
+ .describe('List available themes'),
571
+ sidebar_toggle: z
572
+ .string()
573
+ .optional()
574
+ .default('<leader>b')
575
+ .describe('Toggle sidebar'),
576
+ status_view: z
577
+ .string()
578
+ .optional()
579
+ .default('<leader>s')
580
+ .describe('View status'),
581
+ session_export: z
582
+ .string()
583
+ .optional()
584
+ .default('<leader>x')
585
+ .describe('Export session to editor'),
586
+ session_new: z
587
+ .string()
588
+ .optional()
589
+ .default('<leader>n')
590
+ .describe('Create a new session'),
591
+ session_list: z
592
+ .string()
593
+ .optional()
594
+ .default('<leader>l')
595
+ .describe('List all sessions'),
596
+ session_timeline: z
597
+ .string()
598
+ .optional()
599
+ .default('<leader>g')
600
+ .describe('Show session timeline'),
601
+ session_interrupt: z
602
+ .string()
603
+ .optional()
604
+ .default('escape')
605
+ .describe('Interrupt current session'),
606
+ session_compact: z
607
+ .string()
608
+ .optional()
609
+ .default('<leader>c')
610
+ .describe('Compact the session'),
611
+ messages_page_up: z
612
+ .string()
613
+ .optional()
614
+ .default('pageup')
615
+ .describe('Scroll messages up by one page'),
616
+ messages_page_down: z
617
+ .string()
618
+ .optional()
619
+ .default('pagedown')
620
+ .describe('Scroll messages down by one page'),
621
+ messages_half_page_up: z
622
+ .string()
623
+ .optional()
624
+ .default('ctrl+alt+u')
625
+ .describe('Scroll messages up by half page'),
626
+ messages_half_page_down: z
627
+ .string()
628
+ .optional()
629
+ .default('ctrl+alt+d')
630
+ .describe('Scroll messages down by half page'),
631
+ messages_first: z
632
+ .string()
633
+ .optional()
634
+ .default('ctrl+g,home')
635
+ .describe('Navigate to first message'),
636
+ messages_last: z
637
+ .string()
638
+ .optional()
639
+ .default('ctrl+alt+g,end')
640
+ .describe('Navigate to last message'),
641
+ messages_copy: z
642
+ .string()
643
+ .optional()
644
+ .default('<leader>y')
645
+ .describe('Copy message'),
646
+ messages_undo: z
647
+ .string()
648
+ .optional()
649
+ .default('<leader>u')
650
+ .describe('Undo message'),
651
+ messages_redo: z
652
+ .string()
653
+ .optional()
654
+ .default('<leader>r')
655
+ .describe('Redo message'),
656
+ messages_toggle_conceal: z
657
+ .string()
658
+ .optional()
659
+ .default('<leader>h')
660
+ .describe('Toggle code block concealment in messages'),
661
+ model_list: z
662
+ .string()
663
+ .optional()
664
+ .default('<leader>m')
665
+ .describe('List available models'),
666
+ model_cycle_recent: z
667
+ .string()
668
+ .optional()
669
+ .default('f2')
670
+ .describe('Next recently used model'),
671
+ model_cycle_recent_reverse: z
672
+ .string()
673
+ .optional()
674
+ .default('shift+f2')
675
+ .describe('Previous recently used model'),
676
+ command_list: z
677
+ .string()
678
+ .optional()
679
+ .default('ctrl+p')
680
+ .describe('List available commands'),
681
+ agent_list: z
682
+ .string()
683
+ .optional()
684
+ .default('<leader>a')
685
+ .describe('List agents'),
686
+ agent_cycle: z.string().optional().default('tab').describe('Next agent'),
687
+ agent_cycle_reverse: z
688
+ .string()
689
+ .optional()
690
+ .default('shift+tab')
691
+ .describe('Previous agent'),
692
+ input_clear: z
693
+ .string()
694
+ .optional()
695
+ .default('ctrl+c')
696
+ .describe('Clear input field'),
697
+ input_forward_delete: z
698
+ .string()
699
+ .optional()
700
+ .default('ctrl+d')
701
+ .describe('Forward delete'),
702
+ input_paste: z
703
+ .string()
704
+ .optional()
705
+ .default('ctrl+v')
706
+ .describe('Paste from clipboard'),
707
+ input_submit: z
708
+ .string()
709
+ .optional()
710
+ .default('return')
711
+ .describe('Submit input'),
712
+ input_newline: z
713
+ .string()
714
+ .optional()
715
+ .default('shift+return,ctrl+j')
716
+ .describe('Insert newline in input'),
717
+ history_previous: z
718
+ .string()
719
+ .optional()
720
+ .default('up')
721
+ .describe('Previous history item'),
722
+ history_next: z
723
+ .string()
724
+ .optional()
725
+ .default('down')
726
+ .describe('Next history item'),
727
+ session_child_cycle: z
728
+ .string()
729
+ .optional()
730
+ .default('ctrl+right')
731
+ .describe('Next child session'),
732
+ session_child_cycle_reverse: z
733
+ .string()
734
+ .optional()
735
+ .default('ctrl+left')
736
+ .describe('Previous child session'),
737
+ })
738
+ .strict()
739
+ .meta({
740
+ ref: 'KeybindsConfig',
741
+ });
742
+
743
+ export const TUI = z.object({
744
+ scroll_speed: z
745
+ .number()
746
+ .min(0.001)
747
+ .optional()
748
+ .default(1)
749
+ .describe('TUI scroll speed'),
750
+ scroll_acceleration: z
751
+ .object({
752
+ enabled: z.boolean().describe('Enable scroll acceleration'),
753
+ })
754
+ .optional()
755
+ .describe('Scroll acceleration settings'),
756
+ });
757
+
758
+ export const Layout = z.enum(['auto', 'stretch']).meta({
759
+ ref: 'LayoutConfig',
760
+ });
761
+ export type Layout = z.infer<typeof Layout>;
762
+
763
+ export const Info = z
764
+ .object({
765
+ $schema: z
766
+ .string()
767
+ .optional()
768
+ .describe('JSON schema reference for configuration validation'),
769
+ theme: z
770
+ .string()
771
+ .optional()
772
+ .describe('Theme name to use for the interface'),
773
+ keybinds: Keybinds.optional().describe('Custom keybind configurations'),
774
+ tui: TUI.optional().describe('TUI specific settings'),
775
+ command: z
776
+ .record(z.string(), Command)
777
+ .optional()
778
+ .describe(
779
+ 'Command configuration, see https://opencode.ai/docs/commands'
780
+ ),
781
+ watcher: z
782
+ .object({
783
+ ignore: z.array(z.string()).optional(),
784
+ })
785
+ .optional(),
786
+ snapshot: z.boolean().optional(),
787
+ // share and autoshare fields removed - no sharing support
788
+ autoupdate: z
789
+ .boolean()
790
+ .optional()
791
+ .describe('Automatically update to the latest version'),
792
+ disabled_providers: z
793
+ .array(z.string())
794
+ .optional()
795
+ .describe('Disable providers that are loaded automatically'),
796
+ model: z
797
+ .string()
798
+ .describe(
799
+ 'Model to use in the format of provider/model, eg anthropic/claude-2'
800
+ )
801
+ .optional(),
802
+ small_model: z
803
+ .string()
804
+ .describe(
805
+ 'Small model to use for tasks like title generation in the format of provider/model'
806
+ )
807
+ .optional(),
808
+ username: z
809
+ .string()
810
+ .optional()
811
+ .describe(
812
+ 'Custom username to display in conversations instead of system username'
813
+ ),
814
+ mode: z
815
+ .object({
816
+ build: Agent.optional(),
817
+ plan: Agent.optional(),
818
+ })
819
+ .catchall(Agent)
820
+ .optional()
821
+ .describe('@deprecated Use `agent` field instead.'),
822
+ agent: z
823
+ .object({
824
+ plan: Agent.optional(),
825
+ build: Agent.optional(),
826
+ general: Agent.optional(),
827
+ })
828
+ .catchall(Agent)
829
+ .optional()
830
+ .describe('Agent configuration, see https://opencode.ai/docs/agent'),
831
+ provider: z
832
+ .record(
833
+ z.string(),
834
+ ModelsDev.Provider.partial()
835
+ .extend({
836
+ models: z
837
+ .record(z.string(), ModelsDev.Model.partial())
838
+ .optional(),
839
+ options: z
840
+ .object({
841
+ apiKey: z.string().optional(),
842
+ baseURL: z.string().optional(),
843
+ enterpriseUrl: z
844
+ .string()
845
+ .optional()
846
+ .describe(
847
+ 'GitHub Enterprise URL for copilot authentication'
848
+ ),
849
+ timeout: z
850
+ .union([
851
+ z
852
+ .number()
853
+ .int()
854
+ .positive()
855
+ .describe(
856
+ 'Timeout in milliseconds for requests to this provider. Default is 300000 (5 minutes). Set to false to disable timeout.'
857
+ ),
858
+ z
859
+ .literal(false)
860
+ .describe(
861
+ 'Disable timeout for this provider entirely.'
862
+ ),
863
+ ])
864
+ .optional()
865
+ .describe(
866
+ 'Timeout in milliseconds for requests to this provider. Default is 300000 (5 minutes). Set to false to disable timeout.'
867
+ ),
868
+ })
869
+ .catchall(z.any())
870
+ .optional(),
871
+ })
872
+ .strict()
873
+ )
874
+ .optional()
875
+ .describe('Custom provider configurations and model overrides'),
876
+ mcp: z
877
+ .record(z.string(), Mcp)
878
+ .optional()
879
+ .describe('MCP (Model Context Protocol) server configurations'),
880
+ mcp_defaults: z
881
+ .object({
882
+ tool_call_timeout: z
883
+ .number()
884
+ .int()
885
+ .positive()
886
+ .optional()
887
+ .describe(
888
+ 'Global default timeout in ms for MCP tool execution. Defaults to 120000 (2 minutes). Can be overridden per-server or per-tool.'
889
+ ),
890
+ max_tool_call_timeout: z
891
+ .number()
892
+ .int()
893
+ .positive()
894
+ .optional()
895
+ .describe(
896
+ 'Maximum allowed timeout in ms for MCP tool execution. Defaults to 600000 (10 minutes). Tool timeouts will be capped at this value.'
897
+ ),
898
+ })
899
+ .optional()
900
+ .describe(
901
+ 'Global default settings for MCP tool call timeouts. Can be overridden at the server level.'
902
+ ),
903
+ formatter: z
904
+ .union([
905
+ z.literal(false),
906
+ z.record(
907
+ z.string(),
908
+ z.object({
909
+ disabled: z.boolean().optional(),
910
+ command: z.array(z.string()).optional(),
911
+ environment: z.record(z.string(), z.string()).optional(),
912
+ extensions: z.array(z.string()).optional(),
913
+ })
914
+ ),
915
+ ])
916
+ .optional(),
917
+ instructions: z
918
+ .array(z.string())
919
+ .optional()
920
+ .describe('Additional instruction files or patterns to include'),
921
+ layout: Layout.optional().describe(
922
+ '@deprecated Always uses stretch layout.'
923
+ ),
924
+ permission: z
925
+ .object({
926
+ edit: Permission.optional(),
927
+ bash: z
928
+ .union([Permission, z.record(z.string(), Permission)])
929
+ .optional(),
930
+ webfetch: Permission.optional(),
931
+ doom_loop: Permission.optional(),
932
+ external_directory: Permission.optional(),
933
+ })
934
+ .optional(),
935
+ tools: z.record(z.string(), z.boolean()).optional(),
936
+ experimental: z
937
+ .object({
938
+ hook: z
939
+ .object({
940
+ file_edited: z
941
+ .record(
942
+ z.string(),
943
+ z
944
+ .object({
945
+ command: z.string().array(),
946
+ environment: z.record(z.string(), z.string()).optional(),
947
+ })
948
+ .array()
949
+ )
950
+ .optional(),
951
+ session_completed: z
952
+ .object({
953
+ command: z.string().array(),
954
+ environment: z.record(z.string(), z.string()).optional(),
955
+ })
956
+ .array()
957
+ .optional(),
958
+ })
959
+ .optional(),
960
+ chatMaxRetries: z
961
+ .number()
962
+ .optional()
963
+ .describe('Number of retries for chat completions on failure'),
964
+ disable_paste_summary: z.boolean().optional(),
965
+ batch_tool: z.boolean().optional().describe('Enable the batch tool'),
966
+ })
967
+ .optional(),
968
+ })
969
+ .strict()
970
+ .meta({
971
+ ref: 'Config',
972
+ });
973
+
974
+ export type Info = z.output<typeof Info>;
975
+
976
+ export const global = lazy(async () => {
977
+ let result: Info = pipe(
978
+ {},
979
+ mergeDeep(await loadFile(path.join(Global.Path.config, 'config.json'))),
980
+ mergeDeep(await loadFile(path.join(Global.Path.config, 'opencode.json'))),
981
+ mergeDeep(await loadFile(path.join(Global.Path.config, 'opencode.jsonc')))
982
+ );
983
+
984
+ await import(path.join(Global.Path.config, 'config'), {
985
+ with: {
986
+ type: 'toml',
987
+ },
988
+ })
989
+ .then(async (mod) => {
990
+ const { provider, model, ...rest } = mod.default;
991
+ if (provider && model) result.model = `${provider}/${model}`;
992
+ result['$schema'] = 'https://opencode.ai/config.json';
993
+ result = mergeDeep(result, rest);
994
+ await Bun.write(
995
+ path.join(Global.Path.config, 'config.json'),
996
+ JSON.stringify(result, null, 2)
997
+ );
998
+ await fs.unlink(path.join(Global.Path.config, 'config'));
999
+ })
1000
+ .catch(() => {});
1001
+
1002
+ return result;
1003
+ });
1004
+
1005
+ async function loadFile(filepath: string): Promise<Info> {
1006
+ log.info(() => ({ message: 'loading', path: filepath }));
1007
+ let text = await Bun.file(filepath)
1008
+ .text()
1009
+ .catch((err) => {
1010
+ if (err.code === 'ENOENT') return;
1011
+ throw new JsonError({ path: filepath }, { cause: err });
1012
+ });
1013
+ if (!text) return {};
1014
+ return load(text, filepath);
1015
+ }
1016
+
1017
+ async function load(text: string, configFilepath: string) {
1018
+ text = text.replace(/\{env:([^}]+)\}/g, (_, varName) => {
1019
+ return process.env[varName] || '';
1020
+ });
1021
+
1022
+ const fileMatches = text.match(/\{file:[^}]+\}/g);
1023
+ if (fileMatches) {
1024
+ const configDir = path.dirname(configFilepath);
1025
+ const lines = text.split('\n');
1026
+
1027
+ for (const match of fileMatches) {
1028
+ const lineIndex = lines.findIndex((line) => line.includes(match));
1029
+ if (lineIndex !== -1 && lines[lineIndex].trim().startsWith('//')) {
1030
+ continue; // Skip if line is commented
1031
+ }
1032
+ let filePath = match.replace(/^\{file:/, '').replace(/\}$/, '');
1033
+ if (filePath.startsWith('~/')) {
1034
+ filePath = path.join(os.homedir(), filePath.slice(2));
1035
+ }
1036
+ const resolvedPath = path.isAbsolute(filePath)
1037
+ ? filePath
1038
+ : path.resolve(configDir, filePath);
1039
+ const fileContent = (
1040
+ await Bun.file(resolvedPath)
1041
+ .text()
1042
+ .catch((error) => {
1043
+ const errMsg = `bad file reference: "${match}"`;
1044
+ if (error.code === 'ENOENT') {
1045
+ throw new InvalidError(
1046
+ {
1047
+ path: configFilepath,
1048
+ message: errMsg + ` ${resolvedPath} does not exist`,
1049
+ },
1050
+ { cause: error }
1051
+ );
1052
+ }
1053
+ throw new InvalidError(
1054
+ { path: configFilepath, message: errMsg },
1055
+ { cause: error }
1056
+ );
1057
+ })
1058
+ ).trim();
1059
+ // escape newlines/quotes, strip outer quotes
1060
+ text = text.replace(match, JSON.stringify(fileContent).slice(1, -1));
1061
+ }
1062
+ }
1063
+
1064
+ const errors: JsoncParseError[] = [];
1065
+ const data = parseJsonc(text, errors, { allowTrailingComma: true });
1066
+ if (errors.length) {
1067
+ const lines = text.split('\n');
1068
+ const errorDetails = errors
1069
+ .map((e) => {
1070
+ const beforeOffset = text.substring(0, e.offset).split('\n');
1071
+ const line = beforeOffset.length;
1072
+ const column = beforeOffset[beforeOffset.length - 1].length + 1;
1073
+ const problemLine = lines[line - 1];
1074
+
1075
+ const error = `${printParseErrorCode(e.error)} at line ${line}, column ${column}`;
1076
+ if (!problemLine) return error;
1077
+
1078
+ return `${error}\n Line ${line}: ${problemLine}\n${''.padStart(column + 9)}^`;
1079
+ })
1080
+ .join('\n');
1081
+
1082
+ throw new JsonError({
1083
+ path: configFilepath,
1084
+ message: `\n--- JSONC Input ---\n${text}\n--- Errors ---\n${errorDetails}\n--- End ---`,
1085
+ });
1086
+ }
1087
+
1088
+ const parsed = Info.safeParse(data);
1089
+ if (parsed.success) {
1090
+ if (!parsed.data.$schema) {
1091
+ parsed.data.$schema = 'https://opencode.ai/config.json';
1092
+ await Bun.write(configFilepath, JSON.stringify(parsed.data, null, 2));
1093
+ }
1094
+ const data = parsed.data;
1095
+ return data;
1096
+ }
1097
+
1098
+ throw new InvalidError({
1099
+ path: configFilepath,
1100
+ issues: parsed.error.issues,
1101
+ });
1102
+ }
1103
+ export const JsonError = NamedError.create(
1104
+ 'ConfigJsonError',
1105
+ z.object({
1106
+ path: z.string(),
1107
+ message: z.string().optional(),
1108
+ })
1109
+ );
1110
+
1111
+ export const ConfigDirectoryTypoError = NamedError.create(
1112
+ 'ConfigDirectoryTypoError',
1113
+ z.object({
1114
+ path: z.string(),
1115
+ dir: z.string(),
1116
+ suggestion: z.string(),
1117
+ })
1118
+ );
1119
+
1120
+ export const InvalidError = NamedError.create(
1121
+ 'ConfigInvalidError',
1122
+ z.object({
1123
+ path: z.string(),
1124
+ issues: z.custom<z.core.$ZodIssue[]>().optional(),
1125
+ message: z.string().optional(),
1126
+ })
1127
+ );
1128
+
1129
+ export async function get() {
1130
+ return state().then((x) => x.config);
1131
+ }
1132
+
1133
+ export async function update(config: Info) {
1134
+ const filepath = path.join(Instance.directory, 'config.json');
1135
+ const existing = await loadFile(filepath);
1136
+ await Bun.write(
1137
+ filepath,
1138
+ JSON.stringify(mergeDeep(existing, config), null, 2)
1139
+ );
1140
+ await Instance.dispose();
1141
+ }
1142
+
1143
+ export async function directories() {
1144
+ return state().then((x) => x.directories);
1145
+ }
1146
+ }