- Go gateway (stdlib only): /v1/messages non-stream + SSE, /v1/messages/count_tokens, management API /api/* (providers CRUD, apply/restore, settings, backups, logs) - Protocol conversion: Anthropic Messages <-> Chat Completions with a streaming state machine keyed by content block index (text, tool_use, parallel calls) - ~/.claude/settings.json guarded read/write: timestamped backups, managed env keys only, one-click restore - Tauri 2 shell: spawns the gateway sidecar, parses the random port from stdout, stdin pipe watchdog prevents orphans, kills the child on exit - React frontend: Dashboard/Providers/Settings, zh-CN + en-US, TanStack Query - Tests: Go table-driven + httptest integration (all green), vitest + RTL
33 lines
652 B
Go
33 lines
652 B
Go
package anthropic
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
)
|
|
|
|
// flusherOf extracts the http.Flusher when the writer supports it.
|
|
func flusherOf(w http.ResponseWriter) http.Flusher {
|
|
if f, ok := w.(http.Flusher); ok {
|
|
return f
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// writeEvent emits one Server-Sent Event in Anthropic's format:
|
|
// "event: <name>\ndata: <json>\n\n".
|
|
func writeEvent(w io.Writer, f http.Flusher, name string, data any) error {
|
|
payload, err := json.Marshal(data)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if _, err := fmt.Fprintf(w, "event: %s\ndata: %s\n\n", name, payload); err != nil {
|
|
return err
|
|
}
|
|
if f != nil {
|
|
f.Flush()
|
|
}
|
|
return nil
|
|
}
|