dial now serves OpenAI chat completions, OpenAI Responses and the Anthropic Messages API on 127.0.0.1:3425 and forwards each request to the vendor that serves the model, translating between the APIs when the vendor speaks a different one (streaming, tool calls and reasoning included). Every agent points at that one URL; models are spelled provider/model. Providers are added from presets (vendors, relays, local servers) with just an API key, or as custom OpenAI/Anthropic-compatible endpoints. Keys live in ~/.config/dial/providers.json; nothing is read from the shell environment. Each provider chooses which of its models to expose. Agents get gateway wiring instead of vendor-specific tables: Claude Code via the env block, Codex via a dial model provider plus a model catalog file, OpenCode/Pi/Crush via a dial provider entry; native settings are stashed and restored when a native model is picked again. The Providers page shows the gateway, each provider with the agents on it, an inline editor with model chips, and an add sheet of preset tiles. CLI: dial presets | providers | provider add|key|models|test|rm | models | serve.
105 lines
2.1 KiB
Go
105 lines
2.1 KiB
Go
package gateway
|
|
|
|
import (
|
|
"bufio"
|
|
"bytes"
|
|
"encoding/json"
|
|
"io"
|
|
"net/http"
|
|
"strings"
|
|
)
|
|
|
|
// readSSE walks a server-sent event stream, calling fn with each event's
|
|
// name and data. Data lines of one event are joined with newlines.
|
|
func readSSE(r io.Reader, fn func(event, data string) error) error {
|
|
sc := bufio.NewScanner(r)
|
|
sc.Buffer(make([]byte, 0, 64<<10), 32<<20)
|
|
var event string
|
|
var data []string
|
|
flush := func() error {
|
|
if len(data) == 0 {
|
|
event = ""
|
|
return nil
|
|
}
|
|
err := fn(event, strings.Join(data, "\n"))
|
|
event, data = "", nil
|
|
return err
|
|
}
|
|
for sc.Scan() {
|
|
line := sc.Text()
|
|
switch {
|
|
case line == "":
|
|
if err := flush(); err != nil {
|
|
return err
|
|
}
|
|
case strings.HasPrefix(line, ":"):
|
|
case strings.HasPrefix(line, "event:"):
|
|
event = strings.TrimSpace(line[6:])
|
|
case strings.HasPrefix(line, "data:"):
|
|
data = append(data, strings.TrimPrefix(line[5:], " "))
|
|
}
|
|
}
|
|
if err := flush(); err != nil {
|
|
return err
|
|
}
|
|
return sc.Err()
|
|
}
|
|
|
|
// sseWriter writes events to a client and flushes each one.
|
|
type sseWriter struct {
|
|
w http.ResponseWriter
|
|
f http.Flusher
|
|
begun bool
|
|
}
|
|
|
|
func newSSEWriter(w http.ResponseWriter) *sseWriter {
|
|
f, _ := w.(http.Flusher)
|
|
return &sseWriter{w: w, f: f}
|
|
}
|
|
|
|
func (s *sseWriter) begin() {
|
|
if s.begun {
|
|
return
|
|
}
|
|
s.begun = true
|
|
h := s.w.Header()
|
|
h.Set("Content-Type", "text/event-stream; charset=utf-8")
|
|
h.Set("Cache-Control", "no-cache")
|
|
h.Set("Connection", "keep-alive")
|
|
h.Set("X-Accel-Buffering", "no")
|
|
s.w.WriteHeader(http.StatusOK)
|
|
s.flush()
|
|
}
|
|
|
|
func (s *sseWriter) flush() {
|
|
if s.f != nil {
|
|
s.f.Flush()
|
|
}
|
|
}
|
|
|
|
// event writes one event; a JSON-marshallable value or a raw string.
|
|
func (s *sseWriter) event(name string, v any) {
|
|
s.begin()
|
|
var b bytes.Buffer
|
|
if name != "" {
|
|
b.WriteString("event: ")
|
|
b.WriteString(name)
|
|
b.WriteByte('\n')
|
|
}
|
|
b.WriteString("data: ")
|
|
switch x := v.(type) {
|
|
case string:
|
|
b.WriteString(x)
|
|
case []byte:
|
|
b.Write(x)
|
|
case json.RawMessage:
|
|
b.Write(x)
|
|
default:
|
|
j, _ := json.Marshal(x)
|
|
b.Write(j)
|
|
}
|
|
b.WriteString("\n\n")
|
|
s.w.Write(b.Bytes())
|
|
s.flush()
|
|
}
|