diff --git a/opencode/opencode.json b/opencode/opencode.json index 781b512..721256f 100644 --- a/opencode/opencode.json +++ b/opencode/opencode.json @@ -61,5 +61,22 @@ } } } + }, + "agent": { + "Search": { + "model": "zai-coding-plan/glm-4.6", + "description": "Read-only agent for web searches", + "prompt": "You are Jeff, a research specialist. Use only read/online tools to gather information, summarize briefly with sources, and avoid edits or speculation. When researching information on the internet, use the google_search tool to conduct searches, and then follow up with website_fetch tool calls to gather detailed content from relevant pages.", + "permission": { + "edit": "deny", + "bash": "deny", + "webfetch": "deny" + }, + "tools": { + "edit": false, + "bash": false, + "webfetch": false + } + } } } diff --git a/zsh/functions/_op_common.zsh b/zsh/functions/_op_common.zsh new file mode 100644 index 0000000..b3de0a3 --- /dev/null +++ b/zsh/functions/_op_common.zsh @@ -0,0 +1,113 @@ +_op_run() { + local tag="$1" + shift + + local -a opencode_cmd + opencode_cmd=(opencode "$@") + + local base_home="${XDG_CONFIG_HOME:-$HOME/.config}/opencode" + local base_config="$base_home/opencode.json" + + if [ ! -f "$base_config" ]; then + print -u2 "$tag: missing base config at $base_config" + return 1 + fi + + setopt local_options null_glob + + local tmp_home + tmp_home=$(mktemp -d "${TMPDIR:-/tmp}/opencode-home.XXXXXX") || return 1 + print -u2 "$tag: using temporary OPENCODE_CONFIG_DIR at $tmp_home" + + local cleanup_cmd="print -u2 \"$tag: removing temporary OPENCODE_CONFIG_DIR $tmp_home\"; rm -rf '$tmp_home'" + trap "$cleanup_cmd" EXIT INT TERM + + if ! cp "$base_config" "$tmp_home/opencode.json" >/dev/null 2>&1; then + trap - EXIT INT TERM + eval "$cleanup_cmd" + print -u2 "$tag: failed to copy $base_config" + return 1 + fi + + local base_agents="$base_home/AGENTS.md" + if [ "$tag" = "se" ]; then + local search_agents="$base_home/agent/search/AGENTS.md" + if [ -f "$search_agents" ]; then + base_agents="$search_agents" + fi + fi + + if [ -f "$base_agents" ]; then + if ! cp "$base_agents" "$tmp_home/AGENTS.md" >/dev/null 2>&1; then + trap - EXIT INT TERM + eval "$cleanup_cmd" + print -u2 "$tag: failed to copy $base_agents" + return 1 + fi + fi + + local -a to_link=( + plugin + history + sessions + logs + ) + + local name + for name in "${to_link[@]}"; do + if [ -e "$base_home/$name" ]; then + if ! ln -s "$base_home/$name" "$tmp_home/$name" 2>/dev/null; then + trap - EXIT INT TERM + eval "$cleanup_cmd" + print -u2 "$tag: failed to symlink $base_home/$name" + return 1 + fi + fi + done + + if ! mkdir -p "$tmp_home/command"; then + trap - EXIT INT TERM + eval "$cleanup_cmd" + print -u2 "$tag: failed to create $tmp_home/command" + return 1 + fi + + if [ -d "$base_home/command" ]; then + local f + for f in "$base_home/command"/*.md; do + [ -f "$f" ] || continue + cp "$f" "$tmp_home/command/" + done + fi + + local project_prompts_dir="" + if [ -d "$PWD/.agent-prompts" ]; then + project_prompts_dir="$PWD/.agent-prompts" + fi + + if [ -n "$project_prompts_dir" ]; then + local copied_any=0 + local f + for f in "$project_prompts_dir"/*.md; do + [ -f "$f" ] || continue + copied_any=1 + local filename="${f:t}" + if ! cp -f "$f" "$tmp_home/command/prompt_$filename" >/dev/null 2>&1; then + trap - EXIT INT TERM + eval "$cleanup_cmd" + print -u2 "$tag: failed to copy project prompt $f" + return 1 + fi + done + if (( copied_any )); then + print -u2 "$tag: added project prompts from $project_prompts_dir to commands" + fi + fi + + OPENCODE_CONFIG_DIR="$tmp_home" "${opencode_cmd[@]}" + local exit_code=$? + + trap - EXIT INT TERM + eval "$cleanup_cmd" + return $exit_code +} diff --git a/zsh/functions/op.zsh b/zsh/functions/op.zsh index 94dc7cb..7fa9619 100644 --- a/zsh/functions/op.zsh +++ b/zsh/functions/op.zsh @@ -1,109 +1,17 @@ op() { - local -a opencode_cmd - opencode_cmd=(opencode) - - local base_home="${XDG_CONFIG_HOME:-$HOME/.config}/opencode" - local base_config="$base_home/opencode.json" - - if [ ! -f "$base_config" ]; then - print -u2 "op: missing base config at $base_config" - return 1 - fi - - setopt local_options null_glob - - local tmp_home - tmp_home=$(mktemp -d "${TMPDIR:-/tmp}/opencode-home.XXXXXX") || return 1 - print -u2 "op: using temporary OPENCODE_CONFIG_DIR at $tmp_home" - - local cleanup_cmd="print -u2 \"op: removing temporary OPENCODE_CONFIG_DIR $tmp_home\"; rm -rf '$tmp_home'" - trap "$cleanup_cmd" EXIT INT TERM - - if ! cp "$base_config" "$tmp_home/opencode.json" >/dev/null 2>&1; then - trap - EXIT INT TERM - eval "$cleanup_cmd" - print -u2 "op: failed to copy $base_config" - return 1 - fi - - local base_agents="$base_home/AGENTS.md" - if [ -f "$base_agents" ]; then - if ! cp "$base_agents" "$tmp_home/AGENTS.md" >/dev/null 2>&1; then - trap - EXIT INT TERM - eval "$cleanup_cmd" - print -u2 "op: failed to copy $base_agents" + if ! typeset -f _op_run >/dev/null; then + local src=${funcfiletrace[1]%:*} + local dir=${src:h} + local common="$dir/_op_common.zsh" + if [ ! -f "$common" ]; then + common="${XDG_CONFIG_HOME:-$HOME/.config}/zsh/functions/_op_common.zsh" + fi + if [ ! -f "$common" ]; then + print -u2 "op: missing _op_common.zsh at $common" return 1 fi + source "$common" || return 1 fi - # Symlink specific persistent items - local -a to_link - to_link=( - plugin - history - sessions - logs - ) - - local name - for name in "${to_link[@]}"; do - if [ -e "$base_home/$name" ]; then - if ! ln -s "$base_home/$name" "$tmp_home/$name" 2>/dev/null; then - trap - EXIT INT TERM - eval "$cleanup_cmd" - print -u2 "op: failed to symlink $base_home/$name" - return 1 - fi - fi - done - - # Prepare command directory for prompts (mapping .agent-prompts to command/) - if ! mkdir -p "$tmp_home/command"; then - trap - EXIT INT TERM - eval "$cleanup_cmd" - print -u2 "op: failed to create $tmp_home/command" - return 1 - fi - - # Copy base commands if they exist - if [ -d "$base_home/command" ]; then - local f - for f in "$base_home/command"/*.md; do - [ -f "$f" ] || continue - cp "$f" "$tmp_home/command/" - done - fi - - # Copy project prompts - local project_prompts_dir="" - if [ -d "$PWD/.agent-prompts" ]; then - project_prompts_dir="$PWD/.agent-prompts" - fi - - if [ -n "$project_prompts_dir" ]; then - local copied_any=0 - local f - for f in "$project_prompts_dir"/*.md; do - [ -f "$f" ] || continue - copied_any=1 - local filename="${f:t}" - if ! cp -f "$f" "$tmp_home/command/prompt_$filename" >/dev/null 2>&1; then - trap - EXIT INT TERM - eval "$cleanup_cmd" - print -u2 "op: failed to copy project prompt $f" - return 1 - fi - done - if (( copied_any )); then - print -u2 "op: added project prompts from $project_prompts_dir to commands" - fi - fi - - opencode_cmd+=("$@") - OPENCODE_CONFIG_DIR="$tmp_home" "${opencode_cmd[@]}" - local exit_code=$? - - trap - EXIT INT TERM - eval "$cleanup_cmd" - return $exit_code + _op_run op "$@" } diff --git a/zsh/functions/se.zsh b/zsh/functions/se.zsh new file mode 100644 index 0000000..89861c4 --- /dev/null +++ b/zsh/functions/se.zsh @@ -0,0 +1,17 @@ +se() { + if ! typeset -f _op_run >/dev/null; then + local src=${funcfiletrace[1]%:*} + local dir=${src:h} + local common="$dir/_op_common.zsh" + if [ ! -f "$common" ]; then + common="${XDG_CONFIG_HOME:-$HOME/.config}/zsh/functions/_op_common.zsh" + fi + if [ ! -f "$common" ]; then + print -u2 "se: missing _op_common.zsh at $common" + return 1 + fi + source "$common" || return 1 + fi + + _op_run se --agent Search "$@" +} diff --git a/zsh/zshrc b/zsh/zshrc index 48212ae..93156b9 100644 --- a/zsh/zshrc +++ b/zsh/zshrc @@ -18,4 +18,5 @@ source ~/.config/zsh/functions/cd_git_root.zsh source ~/.config/zsh/functions/co.zsh source ~/.config/zsh/functions/op.zsh +source ~/.config/zsh/functions/se.zsh