Connect an editor from the account page, and serve the Responses API

The account page makes a key for Continue, Codex or Cline and shows
what the tool needs with the key, this gateway's address and a served
model in it: Continue's assistant file, written to its own place under
~/.continue/assistants so the person's config.yaml is not touched;
Codex's provider and profile, added to ~/.codex/config.toml without
changing its default model; and for Cline, whose settings live in VS
Code's secret storage, the four fields to paste. Continue and Codex get
a one-line command whose script, served without a session, holds no
secret - the key comes from the environment, never a URL - keeps a copy
of whatever it replaces, and leaves the rest of the file as it was.

POST /v1/responses translates the Responses API, which Codex speaks, to
chat completions and back: instructions, messages, function tools and
Codex's free-form custom tools, their calls and outputs, and usage, as a
response object or as its stream of typed events. It goes down the same
path as any request - quotas, admission, routing, usage. Server-side
tools and stored responses have no equivalent and are ignored.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Ilker ManapandClaude Opus 5.5 committed 2026-09-29 10:04:17 +02:00
1 parent b7e8e1a3a1
commit f989e67e1a
9 files changed
+1229 -1

No files matched your search

+12
View File
@@ -127,6 +127,8 @@ func (p *AccountPages) Register(mux *http.ServeMux) {
mux.HandleFunc("POST /profile/keys", p.createKey)
mux.HandleFunc("POST /profile/keys/{id}/revoke", p.revokeKey)
mux.HandleFunc("POST /profile/password", p.changePassword)
mux.HandleFunc("POST /profile/connect", p.connect)
mux.HandleFunc("GET /setup/{tool}", p.setupScript)
}
// --- signup ---
@@ -258,6 +260,12 @@ type profileData struct {
Keys []store.Key
Limits []limitRow
NewKey string
// Connect is an editor just connected: its key and configuration, shown
// once, like any new key.
Connect *connectView
// Tools and Models are what the connect form offers.
Tools []struct{ Key, Name string }
Models []string
Error string
PasswordChanged bool
@@ -366,6 +374,10 @@ func (p *AccountPages) render(w http.ResponseWriter, r *http.Request,
d.MinPassword = account.MinPasswordLen
d.BaseURL = baseURL(r)
d.ExampleModel = p.exampleModel()
d.Tools = editorTools
if p.served != nil {
d.Models = p.served()
}
d.Version = p.version
d.Now = time.Now().UTC().Format(time.RFC3339)
// The form token comes from the cookie, never from whatever the request
+235
View File
@@ -0,0 +1,235 @@
package web
import (
"fmt"
"net/http"
"regexp"
"strings"
"time"
"gitea.mynodes.xyz/ilker/llmgw/internal/store"
)
// Connecting an editor: a key made for it, and the configuration it needs
// with that key, this gateway's address and a served model already in it.
//
// A page cannot write files on somebody's machine, so what it can do is hand
// over the file, ready — to copy, to download, or to have a one-line command
// write. The command's script is served without authentication and holds no
// secret: the key travels in an environment variable, never in a URL, where
// it would land in shell history and proxy logs.
// editorTools are the tools the page knows how to configure.
var editorTools = []struct{ Key, Name string }{
{"continue", "Continue"}, {"codex", "Codex CLI"}, {"cline", "Cline"},
}
// connectView is what the page shows after a key was made for a tool.
type connectView struct {
Tool, ToolName, Model string
Key string
// Files are what the tool reads, with the key in them.
Files []connectFile
// Fields are for a tool that keeps its settings where no file reaches:
// Cline's live in VS Code's secret storage, set through its own screen.
Fields []connectField
// Command writes the files; Run is how to start the tool on the gateway.
Command, Run string
Notes []string
}
type connectFile struct{ Path, Name, Body string }
type connectField struct{ Label, Value string }
// safeModel is what a model name may be to go into a script and a file
// unquoted: the names this gateway serves, and nothing a shell reads twice.
var safeModel = regexp.MustCompile(`^[A-Za-z0-9._:/@+-]{1,200}$`)
// continueYAML is a Continue assistant pointing at the gateway: a file of its
// own under ~/.continue/assistants, so the person's config.yaml is not touched.
func continueYAML(base, model, key string) string {
return fmt.Sprintf(`# Written by llmgw for Continue: the gateway at %[1]s.
name: llmgw
version: 1.0.0
schema: v1
models:
- name: %[2]s (llmgw)
provider: openai
model: %[2]s
apiBase: %[1]s/v1
apiKey: %[3]s
roles:
- chat
- edit
- apply
capabilities:
- tool_use
# For completion as you type, add a small, fast model here with the role
# autocomplete; a large one makes every keystroke wait for it.
`, base, model, key)
}
// codexTOML is the provider and profile Codex needs, and nothing else of its
// configuration: codex --profile llmgw uses the gateway, plain codex what it
// used before. The key is read from LLMGW_API_KEY rather than written down.
func codexTOML(base, model string) string {
return fmt.Sprintf(`# Written by llmgw for Codex: the gateway at %[1]s.
[model_providers.llmgw]
name = "llmgw"
base_url = "%[1]s/v1"
env_key = "LLMGW_API_KEY"
wire_api = "responses"
[profiles.llmgw]
model_provider = "llmgw"
model = "%[2]s"
`, base, model)
}
// connectFor builds what the page shows for a tool.
func connectFor(tool, base, model, key string) connectView {
v := connectView{Tool: tool, Model: model, Key: key}
for _, t := range editorTools {
if t.Key == tool {
v.ToolName = t.Name
}
}
script := base + "/setup/" + tool + "?model=" + model
switch tool {
case "continue":
v.Files = []connectFile{{Path: "~/.continue/assistants/llmgw.yaml", Name: "llmgw.yaml",
Body: continueYAML(base, model, key)}}
v.Command = fmt.Sprintf("curl -fsSL '%s' | LLMGW_KEY='%s' sh", script, key)
v.Run = `In Continue, choose the "llmgw" assistant at the top of its panel.`
case "codex":
v.Files = []connectFile{{Path: "~/.codex/config.toml", Name: "config.toml", Body: codexTOML(base, model)}}
v.Command = fmt.Sprintf("curl -fsSL '%s' | sh", script)
v.Run = fmt.Sprintf("LLMGW_API_KEY='%s' codex --profile llmgw", key)
v.Notes = []string{
"The command adds the two blocks above to ~/.codex/config.toml, replacing any it added before, and keeps a copy of the file as it was.",
"Put export LLMGW_API_KEY=… in your shell's profile to leave it out of the command line.",
}
case "cline":
v.Fields = []connectField{
{"API Provider", "OpenAI Compatible"},
{"Base URL", base + "/v1"},
{"API Key", key},
{"Model ID", model},
}
v.Run = "Cline keeps these in VS Code's own secret storage, so they are set in its settings: open Cline, the gear, and fill them in."
}
return v
}
// connect makes a key for an editor and shows its configuration.
func (p *AccountPages) connect(w http.ResponseWriter, r *http.Request) {
noStore(w)
sess, user, ok := p.session(r)
if !ok {
http.Redirect(w, r, "/login", http.StatusSeeOther)
return
}
if !p.accounts.CheckCSRF(sess, r.FormValue("csrf")) {
http.Error(w, "bad request", http.StatusBadRequest)
return
}
tool := r.FormValue("tool")
known := false
for _, t := range editorTools {
known = known || t.Key == tool
}
model := strings.TrimSpace(r.FormValue("model"))
if !known || !safeModel.MatchString(model) {
p.renderProfile(w, r, sess, user, "", "choose a tool and a model")
return
}
project := r.FormValue("context")
if project == personalValue {
if p.personalOff {
p.renderProfile(w, r, sess, user, "", "personal keys are off on this gateway; choose a project")
return
}
project = ""
}
name := tool + " " + time.Now().Format("2006-01-02")
secret, k, err := store.NewKey(randomKeyID(), name, project)
if err != nil {
p.renderProfile(w, r, sess, user, "", "something went wrong; try again")
return
}
k.CreatedAt = time.Now()
if err := p.keys.CreateUserKey(r.Context(), user.ID, k); err != nil {
msg := "something went wrong; try again"
if err == store.ErrNotMember {
msg = "you are not a member of that project"
}
p.renderProfile(w, r, sess, user, "", msg)
return
}
p.log.Info("a user connected an editor", "user", user.ID, "tool", tool, "key", k.ID)
c := connectFor(tool, baseURL(r), model, secret)
p.render(w, r, sess, user, profileData{Connect: &c})
}
// setupScript is the script the one-line command runs: it writes the tool's
// configuration with the key from the environment, keeping a copy of what
// was there. Served without a session — it holds nothing a stranger could
// use: this gateway's address, which they already typed, and a model name.
func (p *AccountPages) setupScript(w http.ResponseWriter, r *http.Request) {
tool := r.PathValue("tool")
model := r.URL.Query().Get("model")
if !safeModel.MatchString(model) {
http.Error(w, "which model? add ?model=<name>", http.StatusBadRequest)
return
}
base := baseURL(r)
var body string
switch tool {
case "continue":
body = fmt.Sprintf(`#!/bin/sh
# Connects Continue to the llmgw gateway at %[1]s.
set -eu
: "${LLMGW_KEY:?set LLMGW_KEY to the key the gateway gave you}"
dir="$HOME/.continue/assistants"
file="$dir/llmgw.yaml"
mkdir -p "$dir"
[ -f "$file" ] && cp "$file" "$file.bak.$(date +%%Y%%m%%d%%H%%M%%S)"
umask 077
cat > "$file" <<'LLMGW'
%[2]sLLMGW
# The key goes in last, from the environment: never through a URL.
tmp="$file.tmp"
sed "s|__LLMGW_KEY__|$LLMGW_KEY|" "$file" > "$tmp" && mv "$tmp" "$file"
echo "Wrote $file. In Continue, choose the llmgw assistant."
`, base, continueYAML(base, model, "__LLMGW_KEY__"))
case "codex":
body = fmt.Sprintf(`#!/bin/sh
# Connects Codex to the llmgw gateway at %[1]s.
set -eu
dir="$HOME/.codex"
file="$dir/config.toml"
mkdir -p "$dir"
touch "$file"
cp "$file" "$file.bak.$(date +%%Y%%m%%d%%H%%M%%S)"
# Out with the blocks this wrote before, in with the new: every other line
# of the file stays as it was.
awk '
/^\[/ { skip = ($0 == "[model_providers.llmgw]" || $0 == "[profiles.llmgw]" || $0 ~ /^# Written by llmgw/) }
/^# Written by llmgw/ { skip = 1; next }
!skip { print }
' "$file" > "$file.tmp"
cat >> "$file.tmp" <<'LLMGW'
%[2]sLLMGW
mv "$file.tmp" "$file"
echo "Added the llmgw provider to $file."
echo "Run: LLMGW_API_KEY=<your key> codex --profile llmgw"
`, base, codexTOML(base, model))
default:
http.NotFound(w, r)
return
}
w.Header().Set("Content-Type", "text/x-shellscript; charset=utf-8")
w.Header().Set("Cache-Control", "no-store")
_, _ = w.Write([]byte(body))
}
+117
View File
@@ -0,0 +1,117 @@
package web
import (
"net/http"
"net/http/httptest"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
)
func TestEachToolGetsItsConfigurationWithTheKey(t *testing.T) {
c := connectFor("continue", "https://gw.example", "qwen2.5-coder-7b", "sk-llmgw-abc")
if !strings.Contains(c.Files[0].Body, "apiBase: https://gw.example/v1") || !strings.Contains(c.Files[0].Body, "apiKey: sk-llmgw-abc") ||
c.Files[0].Path != "~/.continue/assistants/llmgw.yaml" {
t.Errorf("continue = %+v", c.Files)
}
// The key goes in the environment, not the URL, where it would be kept in
// shell history and proxy logs.
if strings.Contains(strings.Split(c.Command, "|")[0], "sk-llmgw-abc") {
t.Errorf("the key is in the URL: %s", c.Command)
}
x := connectFor("codex", "https://gw.example", "qwen2.5-coder-7b", "sk-llmgw-abc")
if strings.Contains(x.Files[0].Body, "sk-llmgw-abc") || !strings.Contains(x.Files[0].Body, `env_key = "LLMGW_API_KEY"`) {
t.Errorf("codex writes the key into its file: %s", x.Files[0].Body)
}
cl := connectFor("cline", "https://gw.example", "m", "sk-llmgw-abc")
if len(cl.Fields) != 4 || cl.Fields[1].Value != "https://gw.example/v1" || cl.Command != "" {
t.Errorf("cline = %+v", cl)
}
}
// runScript fetches a setup script and runs it with HOME in a temporary
// directory, as a person's shell would.
func runScript(t *testing.T, tool string, env ...string) string {
t.Helper()
p := &AccountPages{}
w := httptest.NewRecorder()
r := httptest.NewRequest("GET", "http://gw.example/setup/"+tool+"?model=qwen2.5-coder-7b", nil)
r.SetPathValue("tool", tool)
p.setupScript(w, r)
if w.Code != http.StatusOK {
t.Fatalf("%d %s", w.Code, w.Body.String())
}
home := t.TempDir()
cmd := exec.Command("sh")
cmd.Stdin = strings.NewReader(w.Body.String())
cmd.Env = append([]string{"HOME=" + home, "PATH=" + os.Getenv("PATH")}, env...)
if out, err := cmd.CombinedOutput(); err != nil {
t.Fatalf("%v: %s", err, out)
}
return home
}
func TestTheContinueScriptWritesItsOwnFileWithTheKey(t *testing.T) {
home := runScript(t, "continue", "LLMGW_KEY=sk-llmgw-xyz")
b, err := os.ReadFile(filepath.Join(home, ".continue", "assistants", "llmgw.yaml"))
if err != nil {
t.Fatal(err)
}
if !strings.Contains(string(b), "apiKey: sk-llmgw-xyz") || strings.Contains(string(b), "__LLMGW_KEY__") {
t.Errorf("wrote %s", b)
}
if fi, _ := os.Stat(filepath.Join(home, ".continue", "assistants", "llmgw.yaml")); fi.Mode().Perm()&0o077 != 0 {
t.Errorf("a file holding a key is readable by others: %v", fi.Mode())
}
}
func TestTheCodexScriptKeepsTheRestOfTheConfig(t *testing.T) {
// Run twice over a config with the person's own settings: theirs stay,
// and ours appear once.
p := &AccountPages{}
w := httptest.NewRecorder()
r := httptest.NewRequest("GET", "http://gw.example/setup/codex?model=qwen2.5-coder-7b", nil)
r.SetPathValue("tool", "codex")
p.setupScript(w, r)
home := t.TempDir()
if err := os.MkdirAll(filepath.Join(home, ".codex"), 0o755); err != nil {
t.Fatal(err)
}
mine := "model = \"o3\"\n\n[mcp_servers.docs]\ncommand = \"docs-mcp\"\n"
cfg := filepath.Join(home, ".codex", "config.toml")
if err := os.WriteFile(cfg, []byte(mine), 0o644); err != nil {
t.Fatal(err)
}
for range 2 {
cmd := exec.Command("sh")
cmd.Stdin = strings.NewReader(w.Body.String())
cmd.Env = []string{"HOME=" + home, "PATH=" + os.Getenv("PATH")}
if out, err := cmd.CombinedOutput(); err != nil {
t.Fatalf("%v: %s", err, out)
}
}
b, _ := os.ReadFile(cfg)
got := string(b)
if !strings.Contains(got, `model = "o3"`) || !strings.Contains(got, "[mcp_servers.docs]") {
t.Errorf("the person's settings were lost:\n%s", got)
}
if strings.Count(got, "[model_providers.llmgw]") != 1 || strings.Count(got, "[profiles.llmgw]") != 1 {
t.Errorf("ours should be there once:\n%s", got)
}
if backups, _ := filepath.Glob(cfg + ".bak.*"); len(backups) == 0 {
t.Error("no copy was kept of the file as it was")
}
}
func TestASetupScriptRefusesANameAShellWouldRead(t *testing.T) {
p := &AccountPages{}
w := httptest.NewRecorder()
r := httptest.NewRequest("GET", "http://gw.example/setup/codex?model=$(rm%20-rf%20~)", nil)
r.SetPathValue("tool", "codex")
p.setupScript(w, r)
if w.Code != http.StatusBadRequest {
t.Errorf("%d", w.Code)
}
}
+9
View File
@@ -795,3 +795,12 @@ details.chat-settings .temp input[type=range] { width: 14rem; vertical-align: mi
details.chat-settings output { font-variant-numeric: tabular-nums; font-weight: 700; margin-left: .3rem; }
#send.stop { background: var(--bad); border-color: var(--bad); }
.chat-stats { margin: -.2rem 0 .6rem; font-size: .72rem; font-variant-numeric: tabular-nums; }
/* Connecting an editor. */
.connect-result { border: 1px solid var(--accent); border-radius: 12px; padding: .25rem 1.1rem 1rem; margin: 1rem 0 1.5rem;
background: var(--card); box-shadow: 0 10px 30px -18px var(--accent); }
.copyable { position: relative; }
.copyable pre { margin-bottom: .25rem; overflow-x: auto; }
.copyable button { margin-right: .75rem; font-size: .8rem; }
.connect-form .field-row label { flex: 1 1 12rem; }
table.connect-fields th { white-space: nowrap; }
+63
View File
@@ -68,6 +68,69 @@ Tell whoever you asked for access that you have signed up.</p>
-d '{"model": "{{.ExampleModel}}", "messages": [{"role":"user","content":"hello"}]}'</pre>
{{end}}
{{with .Connect}}
<section class="connect-result" id="connected">
<h2>{{.ToolName}} is ready to connect</h2>
<p>A key was made for it, <strong>shown this once</strong> — it is in everything below. Nothing can produce it again.</p>
{{if .Command}}
<h3>One command</h3>
<p class="note">Writes the file below{{if eq .Tool "codex"}} into your Codex configuration{{end}}, keeping a copy of anything it replaces.</p>
<div class="copyable"><pre>{{.Command}}</pre><button type="button" class="link copy">copy</button></div>
{{end}}
{{range .Files}}
<h3>Or by hand: <code>{{.Path}}</code></h3>
<div class="copyable"><pre>{{.Body}}</pre><button type="button" class="link copy">copy</button>
<button type="button" class="link download-file" data-name="{{.Name}}">download</button></div>
{{end}}
{{if .Fields}}
<h3>Settings to fill in</h3>
<table class="compact narrow-table connect-fields">{{range .Fields}}
<tr><th>{{.Label}}</th><td><code>{{.Value}}</code></td><td><button type="button" class="link copy" data-value="{{.Value}}">copy</button></td></tr>{{end}}
</table>
{{end}}
<h3>Then</h3>
<p>{{if eq .Tool "codex"}}<code>{{.Run}}</code>{{else}}{{.Run}}{{end}}</p>
{{range .Notes}}<p class="note">{{.}}</p>{{end}}
</section>
{{end}}
<h2>Connect your editor</h2>
<p class="note">A key of its own for the tool, and its configuration with the key, this gateway and a model
already in it. Revoke the key below to disconnect it.</p>
<form method="post" action="/profile/connect#connected" class="connect-form">
<input type="hidden" name="csrf" value="{{.CSRF}}">
<div class="field-row">
<label>Tool
<select name="tool">{{range .Tools}}<option value="{{.Key}}">{{.Name}}</option>{{end}}</select></label>
<label>Model
{{if .Models}}<select name="model">{{range .Models}}<option value="{{.}}">{{.}}</option>{{end}}</select>
{{else}}<input type="text" name="model" placeholder="a model the gateway serves" required>{{end}}</label>
{{if .Contexts}}<label>Spends against
<select name="context">{{range .Contexts}}<option value="{{.Value}}">{{.Label}}</option>{{end}}</select></label>{{end}}
</div>
<button type="submit">Make a key and the configuration</button>
</form>
<script>
// Copy and download for what the connect panel shows, done in the page: the
// key in it must not go back to the server to become a file.
document.addEventListener("click", (e) => {
const b = e.target.closest("button.copy, button.download-file");
if (!b) return;
const text = b.dataset.value || b.parentElement.querySelector("pre").textContent;
if (b.classList.contains("copy")) {
const done = () => { const was = b.textContent; b.textContent = "copied"; setTimeout(() => (b.textContent = was), 1500); };
if (navigator.clipboard) navigator.clipboard.writeText(text).then(done, () => window.prompt("Copy", text));
else window.prompt("Copy", text);
return;
}
const url = URL.createObjectURL(new Blob([text], { type: "text/plain" }));
const a = document.createElement("a");
a.href = url; a.download = b.dataset.name || "config";
a.click();
URL.revokeObjectURL(url);
});
</script>
<h2>Create a key</h2>
<form method="post" action="/profile/keys" class="narrow">
<input type="hidden" name="csrf" value="{{.CSRF}}">