-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_opencode
More file actions
609 lines (542 loc) · 17.2 KB
/
_opencode
File metadata and controls
609 lines (542 loc) · 17.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
#compdef opencode
# opencode zsh completion
# Provides completions for opencode CLI including sessions, models, subcommands
# Uses zsh official cache API: _cache_invalid, _retrieve_cache, _store_cache
# Common options shared across most subcommands
_opencode_common_opts=(
{-h,--help}'[Show help]'
'--print-logs[Print logs to stderr]'
'--log-level=[Log level]:log level:(DEBUG INFO WARN ERROR)'
'--pure[Run without external plugins]'
)
# Server-related options shared by serve/web and fallback
_opencode_server_opts=(
'--port=[Port to listen on]:port number:'
'--hostname=[Hostname to listen on]:hostname:(127.0.0.1 0.0.0.0 localhost)'
'--mdns[Enable mDNS service discovery]'
'--mdns-domain=[Custom domain name for mDNS]:domain:'
'--cors=[Additional domains for CORS]:domain:'
)
# Ensure cache is enabled and set cache path
zstyle ':completion:*' use-cache on
zstyle ':completion:*:opencode:*' cache-path "${ZSH_CACHE_DIR:-${XDG_CACHE_HOME:-$HOME/.cache}/zsh}/opencode"
# Caching policy: invalidate cache when opencode database file changes
# This function is called by _cache_invalid to determine if cache should be regenerated
_opencode_sessions_caching_policy() {
local db_path
# Use cached db path if available
if [[ -z "${_opencode_db_path}" ]]; then
_opencode_db_path=$(opencode db path 2>/dev/null)
fi
db_path="${_opencode_db_path}"
# Invalidate if no database path
[[ -z "$db_path" ]] && return 0
# Invalidate if database file doesn't exist
[[ ! -f "$db_path" ]] && return 0
# Invalidate if database is newer than cache file
# -nt operator: true if first file is newer than second file
[[ "$db_path" -nt "$1" ]] && return 0
# Cache is still valid
return 1
}
# Caching policy for models - refresh weekly
_opencode_models_caching_policy() {
local -a oldp
# Invalidate if cache is older than 1 week
oldp=( "$1"(Nmw+1) )
(( $#oldp )) && return 0
return 1
}
# Main completion function
_opencode() {
local curcontext="$curcontext" state line subcmd
local -A opt_args
# Parse global options and the first positional argument (subcommand).
# When CURRENT==2 we land in state "cmds" and complete subcommands.
# When CURRENT>2 we land in state "args"; line[1] is the subcommand
# and line[2..] are the words that follow it.
_arguments -C \
'(-v --version)'{-v,--version}'[Show version number]' \
"$_opencode_common_opts[@]" \
"$_opencode_server_opts[@]" \
'(-m --model)'{-m,--model}'[Model to use]:model:_opencode_models' \
'(-c --continue)'{-c,--continue}'[Continue the last session]' \
'(-s --session)'{-s,--session}'[Session id to continue]:session:_opencode_sessions' \
'--fork[Fork the session when continuing]' \
'--prompt=[Prompt to use]:prompt:' \
'--agent=[Agent to use]:agent:_opencode_agents' \
'1: :->cmds' \
'*:: :->args'
case "$state" in
cmds)
_opencode_commands
;;
args)
subcmd="${line[1]}"
case "$subcmd" in
completion)
_arguments -C \
"$_opencode_common_opts[@]" \
'1:shell:(bash zsh fish powershell)'
;;
mcp)
_opencode_mcp
;;
debug)
_opencode_debug
;;
providers|auth)
_opencode_providers
;;
agent)
_opencode_agent
;;
models)
_arguments -C \
"$_opencode_common_opts[@]" \
'--verbose[Use verbose model output]' \
'--refresh[Refresh models cache]' \
'1:provider:_opencode_providers_list'
;;
export)
_arguments -C \
"$_opencode_common_opts[@]" \
'1:session:_opencode_sessions'
;;
import)
_arguments -C \
"$_opencode_common_opts[@]" \
'1:file:_files -g "*.json"'
;;
pr)
_arguments -C \
"$_opencode_common_opts[@]" \
'1:PR number:'
;;
plugin|plug)
_arguments -C \
"$_opencode_common_opts[@]" \
'1:npm module:'
;;
db)
_opencode_db
;;
session)
_opencode_session
;;
github)
_opencode_github
;;
run)
_arguments -C \
"$_opencode_common_opts[@]" \
'(-m --model)'{-m,--model}'[Model to use]:model:_opencode_models' \
'(-s --session)'{-s,--session}'[Session id to continue]:session:_opencode_sessions' \
'--fork[Fork the session when continuing]' \
'*:message:'
;;
serve|web)
_arguments -C \
"$_opencode_common_opts[@]" \
"$_opencode_server_opts[@]"
;;
attach)
_arguments -C \
"$_opencode_common_opts[@]" \
'1:url:_urls'
;;
upgrade)
_arguments -C \
"$_opencode_common_opts[@]" \
'1:target:(latest stable canary)'
;;
*)
# Unknown subcommand – treat as project path / file
_arguments -C \
"$_opencode_common_opts[@]" \
"$_opencode_server_opts[@]" \
'(-m --model)'{-m,--model}'[Model to use]:model:_opencode_models' \
'(-c --continue)'{-c,--continue}'[Continue the last session]' \
'(-s --session)'{-s,--session}'[Session id to continue]:session:_opencode_sessions' \
'--fork[Fork the session when continuing]' \
'--prompt=[Prompt to use]:prompt:' \
'--agent=[Agent to use]:agent:_opencode_agents' \
'*:file:_files'
;;
esac
;;
esac
}
# Subcommand list
_opencode_commands() {
local -a commands
commands=(
'completion:generate shell completion script'
'acp:start ACP (Agent Client Protocol) server'
'mcp:manage MCP (Model Context Protocol) servers'
'attach:attach to a running opencode server'
'run:run opencode with a message'
'debug:debugging and troubleshooting tools'
'providers:manage AI providers and credentials'
'auth:manage AI providers and credentials (alias)'
'agent:manage agents'
'upgrade:upgrade opencode to latest or specific version'
'uninstall:uninstall opencode and remove all related files'
'serve:start a headless opencode server'
'web:start opencode server and open web interface'
'models:list all available models'
'stats:show token usage and cost statistics'
'export:export session data as JSON'
'import:import session data from JSON file or URL'
'github:manage GitHub agent'
'pr:fetch and checkout a GitHub PR branch'
'session:manage sessions'
'plugin:install plugin and update config'
'plug:install plugin and update config (alias)'
'db:database tools'
)
_describe -t commands 'opencode command' commands
}
# MCP subcommand
_opencode_mcp() {
local curcontext="$curcontext" state line
local -A opt_args
_arguments -C \
"$_opencode_common_opts[@]" \
'1: :_opencode_mcp_commands' \
'*:: :->args'
case "$state" in
args)
case "$line[1]" in
auth|logout|debug)
_arguments -C \
"$_opencode_common_opts[@]" \
'1:server:_opencode_mcp_servers'
;;
*)
_arguments -C \
"$_opencode_common_opts[@]" \
'1: :_opencode_mcp_commands'
;;
esac
;;
esac
}
_opencode_mcp_commands() {
local -a commands
commands=(
'add:add an MCP server'
'list:list MCP servers and their status'
'ls:list MCP servers and their status (alias)'
'auth:authenticate with an OAuth-enabled MCP server'
'logout:remove OAuth credentials for an MCP server'
'debug:debug OAuth connection for an MCP server'
)
_describe -t commands 'mcp command' commands
}
_opencode_mcp_servers() {
# Could fetch from config, but for now just provide generic completion
_message "MCP server name"
}
# Debug subcommand
_opencode_debug() {
local curcontext="$curcontext" state line
local -A opt_args
_arguments -C \
"$_opencode_common_opts[@]" \
'1: :_opencode_debug_commands' \
'*:: :->args'
case "$state" in
args)
case "$line[1]" in
agent)
_arguments -C \
"$_opencode_common_opts[@]" \
'1:agent:_opencode_agents'
;;
*)
_arguments -C \
"$_opencode_common_opts[@]" \
'1: :_opencode_debug_commands'
;;
esac
;;
esac
}
_opencode_debug_commands() {
local -a commands
commands=(
'config:show resolved configuration'
'lsp:LSP debugging utilities'
'rg:ripgrep debugging utilities'
'file:file system debugging utilities'
'scrap:list all known projects'
'skill:list all available skills'
'snapshot:snapshot debugging utilities'
'agent:show agent configuration details'
'paths:show global paths (data, config, cache, state)'
'wait:wait indefinitely (for debugging)'
)
_describe -t commands 'debug command' commands
}
# Providers subcommand
_opencode_providers() {
local curcontext="$curcontext" state line
local -A opt_args
_arguments -C \
"$_opencode_common_opts[@]" \
'1: :_opencode_providers_commands' \
'*:: :->args'
case "$state" in
args)
case "$line[1]" in
login)
_arguments -C \
"$_opencode_common_opts[@]" \
'1:url:_urls'
;;
*)
_arguments -C \
"$_opencode_common_opts[@]" \
'1: :_opencode_providers_commands'
;;
esac
;;
esac
}
_opencode_providers_commands() {
local -a commands
commands=(
'list:list providers and credentials'
'ls:list providers and credentials (alias)'
'login:log in to a provider'
'logout:log out from a configured provider'
)
_describe -t commands 'providers command' commands
}
_opencode_providers_list() {
# No prefill - use fallback (files)
_files
}
# Agent subcommand
_opencode_agent() {
_arguments \
"$_opencode_common_opts[@]" \
'1: :_opencode_agent_commands' \
&& return 0
}
_opencode_agent_commands() {
local -a commands
commands=(
'create:create a new agent'
'list:list all available agents'
)
_describe -t commands 'agent command' commands
}
# Complete agent names
_opencode_agents() {
local -a agents
local update_policy
# Set up caching policy
zstyle -s ":completion:${curcontext}:" cache-policy update_policy
[[ -z "$update_policy" ]] && zstyle ":completion:${curcontext}:" cache-policy _opencode_models_caching_policy
if _cache_invalid opencode-agents || ! _retrieve_cache opencode-agents; then
local -a _opencode_agent_list
_opencode_agent_list=()
# Try to get agents from opencode
local agent_data
agent_data=$(opencode agent list 2>/dev/null | grep -E '^[[:space:]]*[^[:space:]]' | head -20)
if [[ -n "$agent_data" ]]; then
while IFS= read -r line; do
[[ -n "$line" ]] && _opencode_agent_list+=("${line%%[[:space:]]*}")
done <<< "$agent_data"
fi
# Fallback: provide common agent names
if [[ ${#_opencode_agent_list} -eq 0 ]]; then
_opencode_agent_list=(
'default:Default agent'
'github:GitHub agent'
'code-review:Code review agent'
)
fi
[[ ${#_opencode_agent_list} -gt 0 ]] && _store_cache opencode-agents _opencode_agent_list
fi
_describe -t agents 'agent' _opencode_agent_list
}
# DB subcommand
_opencode_db() {
local curcontext="$curcontext" state line
local -A opt_args
_arguments -C \
"$_opencode_common_opts[@]" \
'--format=[Output format]:format:(json tsv)' \
'1: :_opencode_db_commands' \
'*:: :->args'
case "$state" in
args)
if [[ "$line[1]" == "migrate" ]]; then
_arguments -C \
"$_opencode_common_opts[@]" \
'--format=[Output format]:format:(json tsv)'
else
# For query argument
_arguments -C \
"$_opencode_common_opts[@]" \
'--format=[Output format]:format:(json tsv)' \
'1:SQL query:'
fi
;;
esac
}
_opencode_db_commands() {
local -a commands
commands=(
':open interactive sqlite3 shell or run query'
'path:print the database path'
'migrate:migrate JSON data to SQLite'
)
_describe -t commands 'db command' commands
}
# Session subcommand
_opencode_session() {
local curcontext="$curcontext" state line
local -A opt_args
_arguments -C \
"$_opencode_common_opts[@]" \
'1: :_opencode_session_commands' \
'*:: :->args'
case "$state" in
args)
case "${line[1]}" in
list)
_arguments -C \
"$_opencode_common_opts[@]" \
'(-n --max-count)'{-n,--max-count}'[Limit to N most recent sessions]:count:' \
'--format=[Output format]:format:(table json)'
;;
delete)
_arguments -C \
"$_opencode_common_opts[@]" \
'1:session:_opencode_sessions'
;;
*)
_arguments -C \
"$_opencode_common_opts[@]"
;;
esac
;;
esac
}
_opencode_session_commands() {
local -a commands
commands=(
'list:list sessions'
'delete:delete a session'
)
_describe -t commands 'session command' commands
}
# GitHub subcommand
_opencode_github() {
_arguments \
"$_opencode_common_opts[@]" \
'1: :_opencode_github_commands' \
&& return 0
}
_opencode_github_commands() {
local -a commands
commands=(
'install:install the GitHub agent'
'run:run the GitHub agent'
)
_describe -t commands 'github command' commands
}
# Complete session IDs and titles using zsh cache API
# The cache persists across multiple Tab presses in the same shell
(( $+functions[_opencode_sessions] )) ||
_opencode_sessions() {
local -a comp_list
local update_policy session_limit
# Get session limit from zstyle (default: 30)
zstyle -s ":completion:${curcontext}:" session-limit session_limit || session_limit=30
# Set up caching policy for this completion context
zstyle -s ":completion:${curcontext}:" cache-policy update_policy
[[ -z "$update_policy" ]] && zstyle ":completion:${curcontext}:" cache-policy _opencode_sessions_caching_policy
# Use zsh cache API: check if invalid, try to retrieve, or rebuild
# _cache_invalid returns 0 (true) if cache needs to be regenerated
# _retrieve_cache returns 0 (true) if successfully retrieved
if _cache_invalid opencode-sessions || ! _retrieve_cache opencode-sessions; then
# Cache miss or invalid - fetch fresh data from database
local db_path
# Get database path (may be cached in _opencode_db_path)
if [[ -z "${_opencode_db_path}" ]]; then
_opencode_db_path=$(opencode db path 2>/dev/null)
fi
db_path="${_opencode_db_path}"
if [[ -n "$db_path" && -f "$db_path" ]]; then
# Fetch session data from database
# Filter out subagent sessions, order by most recently updated
local fresh_data
fresh_data=$(opencode db "
SELECT id, directory, title, datetime(time_updated / 1000, 'unixepoch') FROM session
WHERE parent_id IS NULL
ORDER BY time_updated DESC
LIMIT ${session_limit}
" 2>/dev/null | tail -n +2)
# Build completion list array
local -a _opencode_session_list
_opencode_session_list=()
if [[ -n "$fresh_data" ]]; then
while IFS=$'\t' read -r id dir_path title time_updated; do
if [[ -n "$id" && -n "$title" ]]; then
_opencode_session_list+=("${id}:${title} (${dir_path} | ${time_updated})")
fi
done <<< "$fresh_data"
fi
# Store in zsh cache (persists across completion calls)
# This creates a cache file that survives multiple Tab presses
if [[ ${#_opencode_session_list} -gt 0 ]]; then
_store_cache opencode-sessions _opencode_session_list
fi
fi
fi
# Use cached or retrieved data
if [[ ${#_opencode_session_list} -gt 0 ]]; then
# Use -V to preserve the time_updated DESC order from SQL query
# Without -V, _describe sorts alphabetically by the completion string (ID)
_describe -t sessions -V 'opencode session' _opencode_session_list
else
_message "no sessions found"
return 1
fi
}
# Complete models using cache
(( $+functions[_opencode_models] )) ||
_opencode_models() {
local update_policy
# Set up caching policy
zstyle -s ":completion:${curcontext}:" cache-policy update_policy
[[ -z "$update_policy" ]] && zstyle ":completion:${curcontext}:" cache-policy _opencode_models_caching_policy
if _cache_invalid opencode-models || ! _retrieve_cache opencode-models; then
local -a _opencode_model_list
_opencode_model_list=()
# Fetch models from opencode
local model_data
model_data=$(opencode models 2>/dev/null)
if [[ -n "$model_data" ]]; then
while IFS= read -r line; do
[[ -n "$line" && ! "$line" =~ ^[[:space:]]*[━┏┗┓┛┣┫┳┻┼├┤─│].*$ ]] && _opencode_model_list+=("$line")
done <<< "$model_data"
fi
# Store in cache
if [[ ${#_opencode_model_list} -gt 0 ]]; then
_store_cache opencode-models _opencode_model_list
fi
fi
if [[ ${#_opencode_model_list} -gt 0 ]]; then
_describe -t models 'model' _opencode_model_list
else
_message "no models found"
return 1
fi
}
_opencode "$@"