chopstick
All widgets

Run Copied Command: T3 Code

By Joshua Knauber

When you copy a shell command in T3 Code, offers to run it in that thread's repo (or worktree) and shows its output in the notch.

Chopstick shows you everything in it before installing, and it starts turned off. Or download the .zip to import it yourself.

Type it in the notch's search

  • Run copied command
  • Run command

What it runs

A widget is a folder of plain files. Here is all of its code.

widget.json
{
  "id": "run-copied-command", "icon": "icon-mint-terminal.fill.png",
  "name": "Run Copied Command: T3 Code",
  "description": "When you copy a shell command in T3 Code, offers to run it in that thread's repo (or worktree) and shows its output in the notch.",
  "show_when": {
    "app": "com.t3tools.t3code",
    "copied.is_cmd": true
  },
  "watch": [
    {
      "script": "clip.sh",
      "on": [
        "clipboard"
      ],
      "as": "copied"
    }
  ],
  "initial": "offer",
  "states": {
    "offer": {
      "open": true,
      "task": true,
      "compact": {
        "leading": {
          "symbol": "terminal.fill",
          "tint": "green"
        },
        "trailing": {
          "text": "{{copied.first | No command copied}}"
        }
      },
      "expanded": {
        "size": "regular",
        "header": {
          "symbol": "terminal.fill",
          "tint": "green",
          "title": "Run in {{copied.repo_name | home folder}}?"
        },
        "blocks": [
          {
            "code": "{{copied.cmd | Copy a shell command first}}"
          },
          {
            "row": [
              {
                "note": "{{copied.repo_short | ~}}"
              },
              "spacer",
              {
                "button": {
                  "symbol": "play.fill",
                  "label": "Run",
                  "style": "filled",
                  "tint": "green",
                  "do": [
                    {
                      "set": {
                        "cmd": "{{copied.cmd}}",
                        "repo": "{{copied.repo}}",
                        "repo_name": "{{copied.repo_name}}"
                      }
                    },
                    {
                      "go": "running"
                    }
                  ]
                }
              }
            ],
            "align": "center"
          }
        ]
      },
      "keys": [
        {
          "key": "cmd+return",
          "label": "Run",
          "do": [
            {
              "set": {
                "cmd": "{{copied.cmd}}",
                "repo": "{{copied.repo}}",
                "repo_name": "{{copied.repo_name}}"
              }
            },
            {
              "go": "running"
            }
          ]
        }
      ],
      "dismiss_after": 6
    },
    "running": {
      "task": true,
      "open": true,
      "stays_on_click_outside": true,
      "compact": {
        "leading": {
          "symbol": "terminal.fill",
          "tint": "green"
        },
        "trailing": "spinner"
      },
      "expanded": {
        "size": "wide",
        "header": {
          "symbol": "terminal.fill",
          "tint": "green",
          "title": "{{cmd}}"
        },
        "blocks": [
          {
            "code": "{{out | Starting…}}"
          },
          {
            "row": [
              {
                "chopsticks": {
                  "tint": "green"
                }
              },
              {
                "note": "Running in {{repo_name | the current T3 Code repo}}…"
              }
            ],
            "align": "center",
            "spacing": "tight"
          }
        ]
      },
      "on_enter": [
        {
          "set": {
            "out": ""
          }
        },
        {
          "run": "run.sh",
          "stream_to": "out",
          "then": "done",
          "else": "failed"
        }
      ]
    },
    "done": {
      "task": true,
      "compact": {
        "leading": {
          "symbol": "checkmark.circle.fill",
          "tint": "green"
        },
        "trailing": {
          "text": "Done"
        }
      },
      "expanded": {
        "size": "wide",
        "header": {
          "symbol": "checkmark.circle.fill",
          "tint": "green",
          "title": "{{cmd}}"
        },
        "blocks": [
          {
            "code": "{{out | (no output)}}"
          },
          {
            "row": [
              {
                "note": "{{repo_name}}"
              },
              {
                "button": {
                  "symbol": "doc.on.doc",
                  "label": "Copy output",
                  "style": "plain",
                  "do": [
                    {
                      "copy": "{{out}}"
                    }
                  ]
                }
              },
              {
                "button": {
                  "symbol": "arrow.clockwise",
                  "label": "Run again",
                  "do": [
                    {
                      "go": "running"
                    }
                  ]
                }
              }
            ],
            "align": "center"
          }
        ]
      }
    },
    "failed": {
      "task": true,
      "open": true,
      "compact": {
        "leading": {
          "symbol": "xmark.octagon.fill",
          "tint": "red"
        },
        "trailing": {
          "text": "Failed"
        }
      },
      "expanded": {
        "size": "wide",
        "header": {
          "symbol": "xmark.octagon.fill",
          "tint": "red",
          "title": "{{cmd}}"
        },
        "blocks": [
          {
            "code": "{{out}}{{error}}"
          },
          {
            "row": [
              {
                "note": "{{repo_name}}"
              },
              {
                "button": {
                  "symbol": "doc.on.doc",
                  "label": "Copy output",
                  "style": "plain",
                  "do": [
                    {
                      "copy": "{{out}}{{error}}"
                    }
                  ]
                }
              },
              {
                "button": {
                  "symbol": "arrow.clockwise",
                  "label": "Run again",
                  "do": [
                    {
                      "go": "running"
                    }
                  ]
                }
              }
            ],
            "align": "center"
          }
        ]
      }
    }
  },
  "commands": {
    "run_copied": {
      "description": "Run the shell command on the clipboard in the repo of the T3 Code thread it came from, and show its output.",
      "phrase": "Run copied command",
      "do": [
        {
          "set": {
            "cmd": "{{copied.cmd}}",
            "repo": "{{copied.repo}}",
            "repo_name": "{{copied.repo_name}}"
          }
        },
        {
          "go": "running"
        }
      ]
    },
    "run": {
      "description": "Run a shell command in a folder (defaults to the repo of the most recent T3 Code thread) and show its output.",
      "phrase": "Run {command}",
      "inputs": {
        "command": {
          "type": "string",
          "description": "The shell command to run"
        },
        "folder": {
          "type": "string",
          "description": "Absolute path of the folder to run it in",
          "optional": true
        }
      },
      "do": [
        {
          "set": {
            "cmd": "{{input.command}}",
            "repo": "{{input.folder}}",
            "repo_name": ""
          }
        },
        {
          "go": "running"
        }
      ]
    }
  },
  "preview": {
    "states": [
      "offer",
      "running",
      "done",
      "failed"
    ],
    "data": {
      "copied": {
        "is_cmd": true,
        "cmd": "npx wrangler deploy",
        "first": "npx wrangler deploy",
        "repo": "/Users/you/Projects/my-app",
        "repo_name": "my-app",
        "repo_short": "~/Projects/my-app"
      },
      "cmd": "npx wrangler deploy",
      "out": " ⛅️ wrangler 4.42.0\n───────────────────\nTotal Upload: 412.18 KiB / gzip: 88.03 KiB\nUploaded my-app (3.21 sec)\nDeployed my-app triggers (0.84 sec)\n  https://my-app.example.workers.dev\nCurrent Version ID: 7c1e4b2a-9f3d-4e61-b0a8-2d5f6c9e1a73",
      "error": "",
      "repo": "/Users/you/Projects/my-app",
      "repo_name": "my-app"
    }
  }
}
clip.sh
# Reports whether the clipboard holds a shell command, and which repo
# (the T3 Code thread it came from) to run it in.
text=$(pbpaste 2>/dev/null | head -c 2000)
first=$(printf '%s' "$text" | sed -n '/[^[:space:]]/{s/^[[:space:]]*//;s/^\$ //;p;q;}')
if printf '%s' "$first" | grep -Eq '^(npx|npm|pnpm|yarn|bun|bunx|node|cd|git|gh|wrangler|curl|ls|make|docker|brew|python3?|pip3?|cargo|swift|xcodebuild|rm|mv|cp|mkdir|cat|echo|export|sh|bash|zsh)( |$)'; then
  /usr/bin/python3 -I -c '
import json, os, subprocess, sys
t = sys.argv[1].strip()
t = "\n".join(l[2:] if l.startswith("$ ") else l for l in t.splitlines())
repo = subprocess.run(["/usr/bin/python3", "-I", "repo.py", t], capture_output=True, text=True).stdout.strip()
home = os.path.expanduser("~")
print(json.dumps({"is_cmd": True, "cmd": t, "first": sys.argv[2], "repo": repo,
                  "repo_name": os.path.basename(repo) if repo else "",
                  "repo_short": repo.replace(home, "~", 1) if repo else ""}))' "$text" "$first"
else
  echo '{"is_cmd": false}'
fi
repo.py
"""Prints the folder of the T3 Code thread the copied text came from.

Looks through the most recently active threads for an assistant message that
contains the text; falls back to the most recently active thread.
Usage: python3 -I repo.py "<copied text>"
"""
import json, os, sqlite3, sys

text = sys.argv[1].strip() if len(sys.argv) > 1 else ""
db = os.path.expanduser("~/.t3/userdata/statev2.sqlite")
try:
    con = sqlite3.connect(f"file:{db}?mode=ro", uri=True, timeout=2)
    threads = con.execute(
        "select thread_id, project_id, json_extract(payload_json,'$.worktreePath') "
        "from orchestration_v2_projection_threads "
        "where deleted_at is null and archived_at is null "
        "order by updated_at desc limit 15").fetchall()
    pick = threads[0] if threads else None
    if text:
        for t in threads:
            hit = con.execute(
                "select 1 from orchestration_v2_projection_messages "
                "where thread_id = ? and role = 'assistant' "
                "and instr(json_extract(payload_json,'$.text'), ?) > 0 "
                "order by created_at desc limit 1", (t[0], text)).fetchone()
            if hit:
                pick = t
                break
    path = None
    if pick:
        path = pick[2]
        if not path or not os.path.isdir(path):
            row = con.execute("select workspace_root from projection_projects where project_id = ?",
                              (pick[1],)).fetchone()
            path = row[0] if row else None
    if path and os.path.isdir(path):
        print(path)
except Exception:
    pass
run.sh
# Runs the command in the repo it was copied for, output (stdout + stderr) streamed.
dir="$CHOPSTICK_REPO"
[ -n "$dir" ] || dir=$(/usr/bin/python3 -I repo.py "")
dir="${dir:-$HOME}"
cd "$dir" || { echo "Folder not found: $dir" >&2; exit 1; }
exec /bin/zsh -lc "$CHOPSTICK_CMD" </dev/null 2>&1