startRun notes the entry on the request's context (noteEntry); serve copies it to Try.Entry and Try.EntryWhy (the claudePrint code). The Routing row shows a TUI or -p chip, with the reason in its title, in en/zh/zh-TW/ja/de. The bridge page also records the answer to Tossipe on X: a local Claude Code on a remote magpie sends its history each turn, and the remote resumes its session and types only the new turn, so the history isn't sent to Anthropic twice. Run on mote: TestEntryNoted; routing-entry.test.cjs in both engines, five languages, 1000 and 480 wide; i18n-keys (a key repeating a Go string is reworded). Not tried: a real Claude Code run on each path.
47 lines
1.2 KiB
Go
47 lines
1.2 KiB
Go
package gateway
|
|
|
|
import (
|
|
"context"
|
|
"sync"
|
|
)
|
|
|
|
// entryKey carries a request's try the entry point Claude Code ran it by.
|
|
type entryKey struct{}
|
|
|
|
// entryNote is how a Claude Code run was made, noted by the subscription
|
|
// bridge as the run starts and read for the try's row on the Routing page.
|
|
type entryNote struct {
|
|
mu sync.Mutex
|
|
set, tui bool
|
|
why string
|
|
}
|
|
|
|
// withEntry has the try's context carry a note of the entry point its run
|
|
// takes; the func returns it: "tui" or "print" (and claudePrint's code for
|
|
// why it is -p), "" for a try that ran no Claude Code.
|
|
func withEntry(ctx context.Context) (context.Context, func() (entry, why string)) {
|
|
n := &entryNote{}
|
|
return context.WithValue(ctx, entryKey{}, n), func() (string, string) {
|
|
n.mu.Lock()
|
|
defer n.mu.Unlock()
|
|
switch {
|
|
case !n.set:
|
|
return "", ""
|
|
case n.tui:
|
|
return "tui", ""
|
|
}
|
|
return "print", n.why
|
|
}
|
|
}
|
|
|
|
// noteEntry notes the run ctx belongs to is the interactive mode (tui) or
|
|
// -p, and for -p claudePrint's code. The last run of a try is the one told,
|
|
// as a run started again is.
|
|
func noteEntry(ctx context.Context, tui bool, why string) {
|
|
if n, ok := ctx.Value(entryKey{}).(*entryNote); ok {
|
|
n.mu.Lock()
|
|
n.set, n.tui, n.why = true, tui, why
|
|
n.mu.Unlock()
|
|
}
|
|
}
|