Documentation
¶
Overview ¶
Package claudetui runs Claude Code's interactive mode for magpie's Claude subscription bridge, as `magpie claude-tui-helper`: the bridge talks to it as it talks to `claude -p --input-format stream-json --output-format stream-json`, and it runs the real interactive Claude Code in a pseudo-terminal of its own. A user message is pasted into Claude Code's prompt and sent with Enter, as a person does; what Claude Code answers is read from the session's transcript, which Claude Code writes as it goes, and told back as stream-json; a reply as Anthropic streams it, where Claude Code loads the helper's tap (tap.go), and one whole message at a time where it doesn't.
Anthropic counts a request by its entrypoint: the interactive mode's "cli", where -p's is "sdk-cli", and an sdk-cli request costs about twice as much of the plan's allowance (anthropics/claude-code#97074). The run is the real interactive Claude Code: nothing sets its entrypoint.
Index ¶
Constants ¶
const BypassEnv = "MAGPIE_CLAUDE_TUI_BYPASS"
BypassEnv, "1", has the shim tell Claude Code yes when it asks to accept running in Bypass Permissions mode (a user's claude that is a wrapper adding --dangerously-skip-permissions, or an account whose default mode it is; #5042). Claude Code records the acceptance in its config folder's settings.json, for every run after, so the bridge sets it only for an account folder of magpie's, whose Claude Code no one else runs. In the account Claude Code is signed in to the acceptance is its user's to give, and the run ends at once, saying so.
const Command = "claude-tui-helper"
Command is the hidden subcommand that runs it.
const DeadlineEnv = "MAGPIE_CLAUDE_TUI_BY"
DeadlineEnv holds, in Unix milliseconds, when a one-off run's caller (a /usage read, a test's "hi") stops waiting for it. The helper ends then on its own, saying on its stderr what Claude Code hadn't done and what it last drew. Killed at the caller's deadline instead, it said nothing, and the reading's error was its exit code alone, "exit status 1" on Windows (#4830).
const EndedEarly = "Claude Code ended before it showed its prompt"
EndedEarly begins what the helper says as it ends over a Claude Code that ended itself before it showed its prompt, as one that can't load the session it was told to go on with does. Every other way the interactive mode doesn't come up (no prompt in ReadyWait, a sign-in or a question only its user answers) is the same for a new Claude Code told the whole conversation: the bridge tries one again only on this.
const GuardEnv = "MAGPIE_CLAUDE_GUARD"
GuardEnv names, to the helper, a file of the bridge's that holds the extra body (CLAUDE_CODE_EXTRA_BODY) of the client's latest turn: tap.js puts its safeguards in each request Claude Code makes, which reads that variable only from an environment nothing outside it can change, and the effort the run was told since it started, which tap.js asks each main request at. The bridge rewrites the file as a turn brings new ones, and tap.js writes <file>.live once it has seen a request it puts them in.
const ImageNotTaken = "Claude Code did not take an image pasted as its path"
ImageNotTaken begins imageUnpasted's words: what the bridge keeps, for the machine's next turns with images to run as claude -p.
const MCPCommand = "claude-mcp-helper"
MCPCommand is the hidden subcommand of magpie's tool server, the MCP server a run's --mcp-config names "magpie" (claudebridge.RunMCP). Its arguments are the callback's file, the tools' file and, for the interactive mode, a file it writes once Claude Code has listed its tools: the first message waits for it (MinhuWang on X: a first message sent before then had none of the agent's tools).
const NoUsageWindow = "No usage window has started"
NoUsageWindow is what the helper tells for a /usage panel that loaded with none of the account's windows drawn: each is left out until the request that starts it, so none has started.
const ReadyWait = 45 * time.Second
ReadyWait is how long Claude Code may take to show its prompt before the helper ends, saying why. The bridge's wait for the helper's answer to its initialize request is longer (#4504): a wait shorter than this one ended the helper as it waited, and the turn was answered 502 with no reason.
const StartValueMax = 16 << 10
StartValueMax bounds a value of a request's on a Claude Code run's command line or in its environment: Linux refuses to start a program with one over 128KiB (MAX_ARG_STRLEN), Windows a variable over 32K characters, and a long conversation's safeguards grew past that, so every try to resume or replay it failed "argument list too long" (#6848). A larger one goes in a file or on standard input. tap.js holds its CLAUDE_CODE_EXTRA_BODY to it (envValueMax), as what Claude Code runs inherits it.
const TrustEnv = "MAGPIE_CLAUDE_TUI_TRUST"
TrustEnv, "1", has the shim tell Claude Code yes when it asks whether to trust the folder it runs in: the bridge sets it for magpie's own folder, and for a caller's in an account folder of magpie's, whose Claude Code no one else runs. Otherwise the question is the user's to answer in their own Claude Code, and the run ends, saying so.
const Unanswered = ", not answered in "
Unanswered ends, in what the helper says as it ends, a question of Claude Code's it saw and answered without the answer going through (the trust question, the Bypass Permissions disclaimer, the Claude in Chrome offer): Claude Code asks it the same under every account, so the bridge doesn't ask the next account (@ahz2ygnhjmIJ0E0 on X: 502 after every account had waited out 45s on the trust question).
const Unpastable = "this message can't be pasted into Claude Code's interactive mode: "
Unpastable begins the error a turn ends in when its message can't be pasted into the interactive mode: a block it doesn't take, or an image whose path Claude Code didn't take as an image (imageUnpasted). The bridge asks such a turn again as claude -p, which is sent the image itself (ok123now on X: a 64x64 PNG on Windows 10 LTSC 21H2 was answered 502, and the client asked again and again).
const Untrusted = "Claude Code asks whether to trust the folder"
Untrusted begins what the helper says, on its stderr, as it ends over a folder it wasn't told to trust: the bridge then runs the turn in a folder of magpie's.
const WSLEnv = "MAGPIE_CLAUDE_TUI_WSL"
WSLEnv holds, as JSON (WSL), how to run a Claude Code that is in a WSL distro: the helper, on Windows, runs wsl.exe in its pseudo console.
Variables ¶
This section is empty.
Functions ¶
func Run ¶
Run runs Claude Code at path, args being the bridge's `claude -p` ones, with stream-json on standard input and output: interactive, with the same model, tools and settings (interactiveArgs). One that doesn't come up ends, saying why: it never becomes the -p run it stands for, as a -p request counts against the plan as another client's would, and starts without the interactive mode's cache.
Types ¶
type WSL ¶ added in v0.1.2367
type WSL struct {
// Args are wsl.exe's arguments up to Claude Code's own, its path last
Args []string `json:"args"`
// Mount is where the distro mounts Windows' drives ("/mnt/"): an
// image's path is pasted as Claude Code there reads it
Mount string `json:"mount"`
// Projects is Claude Code's projects folder, as Windows reads it; ""
// with Distro set when Windows can't, or its home is unknown: the
// transcript is then read inside the distro (wslread.go)
Projects string `json:"projects,omitempty"`
// Distro is the distro, as wsl.exe -d names it
Distro string `json:"distro,omitempty"`
}
WSL is a Claude Code in a WSL distro, as the helper runs it.