magpie-aws-quick-bridge 0.1.2 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,195 @@
1
+ // The standalone gateway: Amazon Quick behind a local HTTP address, for
2
+ // CC Switch and any agent that takes an API address and a key. It answers
3
+ // the three APIs agents speak, each on top of the plugin's chat completions:
4
+ // POST /v1/chat/completions OpenAI Chat Completions (OpenCode, …)
5
+ // POST /v1/messages Anthropic Messages (Claude Code)
6
+ // POST /v1/responses OpenAI Responses (Codex)
7
+ // GET /v1/models
8
+ // magpie does not use this: it loads the plugin (index.mjs) itself.
9
+
10
+ import { timingSafeEqual } from "node:crypto"
11
+ import { createServer } from "node:http"
12
+ import { MODES, chatCompletions, mapModel } from "../plugin/chat.mjs"
13
+ import * as oauth from "../plugin/oauth.mjs"
14
+ import * as qbs from "../plugin/qbs.mjs"
15
+ import { MODEL_NAMES, accountModels, models, settings } from "../plugin/settings.mjs"
16
+ import * as anthropic from "./anthropic.mjs"
17
+ import * as responses from "./responses.mjs"
18
+
19
+ const MAX_BODY = 64 * 1024 * 1024
20
+ const SIGN_IN = "Amazon Quick is not signed in; run: npx magpie-aws-quick-bridge login"
21
+
22
+ const openai = (status, message, code = "invalid_request_error") => Response.json({ error: { message, type: code, code } }, { status })
23
+
24
+ // The Quick mode a client's model name stands for. Agents ask for their own
25
+ // models when nobody tells them otherwise: a small one (Claude Code's
26
+ // background model) is answered by "fast", anything else by the default.
27
+ export function quickModel(model, fallback) {
28
+ const mode = mapModel(model, "")
29
+ if (mode) return mode
30
+ return /haiku|mini|nano|small|flash/i.test(String(model ?? "")) ? "fast" : fallback
31
+ }
32
+
33
+ function sameKey(given, key) {
34
+ const a = Buffer.from(String(given ?? ""))
35
+ const b = Buffer.from(key)
36
+ return a.length === b.length && timingSafeEqual(a, b)
37
+ }
38
+
39
+ const givenKey = (headers) => headers.get("x-api-key") || (headers.get("authorization") ?? "").replace(/^Bearer\s+/i, "")
40
+
41
+ // /v1/messages, /messages and /v1/v1/messages are one route: clients differ
42
+ // on whether the address they are given ends in /v1.
43
+ const route = (pathname) => pathname.replace(/\/+$/, "").replace(/^(\/v1)+(?=\/|$)/, "") || "/"
44
+
45
+ // handle(Request) -> Response. `account` is a store.Account (or anything
46
+ // with get()); `options` are the plugin's options.
47
+ export function createGateway({ apiKey, account, options = {}, log = () => {} }) {
48
+ const cfg = settings(options)
49
+ oauth.setEndpoints({ oidcBase: options.oidc_base, discoveryUrl: options.discovery_url, dataPlaneUrl: options.data_plane_url })
50
+
51
+ // The account for a request, or the answer that says why there is none.
52
+ async function signedIn(fail) {
53
+ try {
54
+ const auth = await account.get()
55
+ return auth ? { auth } : { response: fail(401, SIGN_IN) }
56
+ } catch (e) {
57
+ return { response: fail(e?.signIn === "expired" ? 401 : 502, `${e?.message ?? e}${e?.signIn === "expired" ? "; run: npx magpie-aws-quick-bridge login" : ""}`) }
58
+ }
59
+ }
60
+
61
+ async function modelList() {
62
+ let listed = models()
63
+ try {
64
+ const auth = await account.get()
65
+ if (auth) listed = accountModels(listed, await qbs.settings(auth))
66
+ } catch {}
67
+ const created = Math.floor(Date.now() / 1000)
68
+ const data = Object.keys(listed).map((id) => ({
69
+ id,
70
+ object: "model",
71
+ created,
72
+ owned_by: "amazon-quick",
73
+ // what Anthropic's list carries
74
+ type: "model",
75
+ display_name: MODEL_NAMES[id] ?? id,
76
+ created_at: new Date(created * 1000).toISOString(),
77
+ }))
78
+ return Response.json({ object: "list", data, has_more: false, first_id: data[0]?.id ?? null, last_id: data.at(-1)?.id ?? null })
79
+ }
80
+
81
+ async function turn(request, api, fail) {
82
+ let body
83
+ try {
84
+ body = await request.json()
85
+ if (!body || typeof body !== "object" || Array.isArray(body)) throw new Error()
86
+ } catch {
87
+ return fail(400, "the request body is not a JSON object")
88
+ }
89
+ if (api === "tokens") return anthropic.countTokens(body)
90
+ if (api === "responses" && body.previous_response_id) return fail(400, "previous_response_id is not supported: responses are not stored; send the conversation in input")
91
+
92
+ const { auth, response } = await signedIn(fail)
93
+ if (response) return response
94
+ const req =
95
+ api === "messages"
96
+ ? anthropic.toChat(body)
97
+ : api === "responses"
98
+ ? responses.toChat(body, request.headers)
99
+ : { chat: body, conversation: request.headers.get("x-aws-quick-conversation") ?? request.headers.get("x-thread-id") ?? request.headers.get("session_id") ?? "" }
100
+ req.chat.model = quickModel(req.chat.model, cfg.defaultModel)
101
+ log(`${api} ${req.chat.model}${req.chat.stream ? " stream" : ""}`)
102
+ const ask = (auth) => chatCompletions({ body: req.chat, auth, cfg, signal: request.signal, conversation: String(req.conversation).slice(0, 256) })
103
+ let answer = await ask(auth)
104
+ // Amazon Quick refused the token before its time: renew it and ask once more
105
+ if (answer.status === 401 && answer.headers.get("x-magpie-sign-in") === "expired" && account.renew) {
106
+ try {
107
+ const renewed = await account.renew()
108
+ if (renewed) answer = await ask(renewed)
109
+ } catch (e) {
110
+ if (e?.signIn !== "expired") throw e
111
+ return fail(401, `${e.message}; run: npx magpie-aws-quick-bridge login`)
112
+ }
113
+ }
114
+ if (api === "messages") return anthropic.fromChat(answer, req)
115
+ if (api === "responses") return responses.fromChat(answer, req)
116
+ return answer
117
+ }
118
+
119
+ return async function handle(request) {
120
+ const url = new URL(request.url)
121
+ const path = route(url.pathname)
122
+ const fail = path.startsWith("/messages") ? anthropic.error : path === "/responses" ? responses.error : openai
123
+ if (request.method === "GET" && (path === "/" || path === "/health")) return Response.json({ ok: true, name: "magpie-aws-quick-bridge" })
124
+ if (!sameKey(givenKey(request.headers), apiKey)) return fail(401, "the API key is wrong; `npx magpie-aws-quick-bridge status` prints the right one", "invalid_api_key")
125
+ try {
126
+ if (request.method === "GET" && path === "/models") return await modelList()
127
+ if (request.method === "POST" && path === "/chat/completions") return await turn(request, "chat", fail)
128
+ if (request.method === "POST" && path === "/messages") return await turn(request, "messages", fail)
129
+ if (request.method === "POST" && path === "/messages/count_tokens") return await turn(request, "tokens", fail)
130
+ if (request.method === "POST" && path === "/responses") return await turn(request, "responses", fail)
131
+ } catch (e) {
132
+ if (request.signal.aborted) return new Response(null, { status: 499 })
133
+ log(`error: ${e?.message ?? e}`)
134
+ return fail(502, String(e?.message ?? e), "upstream_error")
135
+ }
136
+ return fail(404, `no such route: ${request.method} ${url.pathname}`, "not_found")
137
+ }
138
+ }
139
+
140
+ // Serves `handle` on node:http (Node and Bun both have it). Resolves once it
141
+ // listens, with { port, close }.
142
+ export function listen(handle, { port, host }) {
143
+ const server = createServer(async (req, res) => {
144
+ const abort = new AbortController()
145
+ res.on("close", () => {
146
+ if (!res.writableFinished) abort.abort()
147
+ })
148
+ try {
149
+ const chunks = []
150
+ let size = 0
151
+ for await (const chunk of req) {
152
+ size += chunk.length
153
+ if (size > MAX_BODY) {
154
+ res.writeHead(413, { "content-type": "application/json" })
155
+ return res.end(JSON.stringify({ error: { message: "the request is too large", type: "invalid_request_error" } }))
156
+ }
157
+ chunks.push(chunk)
158
+ }
159
+ const headers = new Headers()
160
+ for (const [k, v] of Object.entries(req.headers)) if (v !== undefined) headers.set(k, Array.isArray(v) ? v.join(", ") : v)
161
+ const body = req.method === "GET" || req.method === "HEAD" ? undefined : Buffer.concat(chunks)
162
+ const answer = await handle(new Request(`http://${req.headers.host || "localhost"}${req.url}`, { method: req.method, headers, body, signal: abort.signal }))
163
+ res.writeHead(answer.status, Object.fromEntries(answer.headers))
164
+ if (!answer.body) return res.end()
165
+ // Read and written one chunk at a time: Readable.fromWeb(...).pipe(res)
166
+ // delivers chunks out of order on Bun 1.3.11.
167
+ const reader = answer.body.getReader()
168
+ res.on("close", () => reader.cancel().catch(() => {}))
169
+ for (;;) {
170
+ const { done, value } = await reader.read()
171
+ if (done) return res.end()
172
+ if (!res.write(value)) await new Promise((next) => res.once("drain", next).once("close", next))
173
+ if (res.destroyed) return
174
+ }
175
+ } catch (e) {
176
+ if (res.headersSent) return res.destroy()
177
+ res.writeHead(500, { "content-type": "application/json" })
178
+ res.end(JSON.stringify({ error: { message: String(e?.message ?? e), type: "server_error" } }))
179
+ }
180
+ })
181
+ return new Promise((resolve, reject) => {
182
+ server.once("error", reject)
183
+ server.listen(port, host, () =>
184
+ resolve({
185
+ port: server.address().port,
186
+ close: () => {
187
+ server.close()
188
+ server.closeAllConnections?.()
189
+ },
190
+ }),
191
+ )
192
+ })
193
+ }
194
+
195
+ export { MODES }
@@ -0,0 +1,68 @@
1
+ // Keeps the gateway running in the background on macOS (a launchd agent):
2
+ // started at login, restarted if it stops. Other systems run `serve`
3
+ // themselves.
4
+
5
+ import { spawnSync } from "node:child_process"
6
+ import { existsSync, mkdirSync, rmSync, writeFileSync } from "node:fs"
7
+ import { homedir, userInfo } from "node:os"
8
+ import { dirname, join } from "node:path"
9
+ import { home } from "./store.mjs"
10
+
11
+ export const LABEL = "com.dabinbinah.aws-quick-bridge"
12
+ export const plistPath = () => join(homedir(), "Library", "LaunchAgents", `${LABEL}.plist`)
13
+ export const logPath = () => join(home(), "serve.log")
14
+
15
+ const xml = (s) => String(s).replace(/[&<>]/g, (c) => ({ "&": "&amp;", "<": "&lt;", ">": "&gt;" })[c])
16
+
17
+ export function plist({ runtime, script, env = {} }) {
18
+ const vars = Object.entries(env)
19
+ .map(([k, v]) => ` <key>${xml(k)}</key><string>${xml(v)}</string>\n`)
20
+ .join("")
21
+ return `<?xml version="1.0" encoding="UTF-8"?>
22
+ <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
23
+ <plist version="1.0">
24
+ <dict>
25
+ <key>Label</key><string>${LABEL}</string>
26
+ <key>ProgramArguments</key>
27
+ <array>
28
+ <string>${xml(runtime)}</string>
29
+ <string>${xml(script)}</string>
30
+ <string>serve</string>
31
+ </array>
32
+ <key>EnvironmentVariables</key>
33
+ <dict>
34
+ ${vars} </dict>
35
+ <key>RunAtLoad</key><true/>
36
+ <key>KeepAlive</key><true/>
37
+ <key>ThrottleInterval</key><integer>10</integer>
38
+ <key>StandardOutPath</key><string>${xml(logPath())}</string>
39
+ <key>StandardErrorPath</key><string>${xml(logPath())}</string>
40
+ </dict>
41
+ </plist>
42
+ `
43
+ }
44
+
45
+ const launchctl = (...args) => spawnSync("launchctl", args, { encoding: "utf8" })
46
+ const domain = () => `gui/${userInfo().uid}`
47
+
48
+ export function install({ runtime, script }) {
49
+ if (process.platform !== "darwin") throw new Error("the background service is for macOS only; on this system keep `magpie-aws-quick-bridge serve` running yourself")
50
+ // npx keeps a package only for a while: a service started from there stops working
51
+ if (/[\\/]_npx[\\/]/.test(script)) throw new Error("install the package first, so the service has a file that stays: npm install -g magpie-aws-quick-bridge, then aws-quick-bridge service install")
52
+ mkdirSync(dirname(plistPath()), { recursive: true })
53
+ mkdirSync(home(), { recursive: true, mode: 0o700 })
54
+ const env = { PATH: process.env.PATH ?? "/usr/bin:/bin" }
55
+ if (process.env.AWS_QUICK_BRIDGE_HOME) env.AWS_QUICK_BRIDGE_HOME = process.env.AWS_QUICK_BRIDGE_HOME
56
+ launchctl("bootout", domain(), plistPath())
57
+ writeFileSync(plistPath(), plist({ runtime, script, env }))
58
+ const res = launchctl("bootstrap", domain(), plistPath())
59
+ if (res.status !== 0) throw new Error(`launchctl could not start the service: ${(res.stderr || res.stdout).trim()}`)
60
+ }
61
+
62
+ export function uninstall() {
63
+ if (process.platform !== "darwin") throw new Error("the background service is for macOS only")
64
+ launchctl("bootout", domain(), plistPath())
65
+ rmSync(plistPath(), { force: true })
66
+ }
67
+
68
+ export const installed = () => process.platform === "darwin" && existsSync(plistPath())
@@ -0,0 +1,113 @@
1
+ // The chat completions answer (plugin/chat.mjs) read back as a few plain
2
+ // events, and a streamed Response to write another API's events into. The
3
+ // Anthropic and Responses routes are both made of these two.
4
+
5
+ const SSE = { "content-type": "text/event-stream; charset=utf-8", "cache-control": "no-cache", "x-accel-buffering": "no" }
6
+
7
+ async function* dataLines(response) {
8
+ const decoder = new TextDecoder()
9
+ let buffer = ""
10
+ for await (const part of response.body) {
11
+ buffer += decoder.decode(part, { stream: true })
12
+ let end
13
+ while ((end = buffer.indexOf("\n\n")) >= 0) {
14
+ const block = buffer.slice(0, end)
15
+ buffer = buffer.slice(end + 2)
16
+ for (const line of block.split("\n")) if (line.startsWith("data: ")) yield line.slice(6)
17
+ }
18
+ }
19
+ }
20
+
21
+ const usageOf = (u) => ({
22
+ input: Math.max(0, (u?.prompt_tokens ?? 0) - (u?.prompt_tokens_details?.cached_tokens ?? 0)),
23
+ cached: u?.prompt_tokens_details?.cached_tokens ?? 0,
24
+ output: u?.completion_tokens ?? 0,
25
+ total: u?.total_tokens ?? 0,
26
+ })
27
+
28
+ // A streamed chat completion as events:
29
+ // { type: "reasoning" | "text", text }
30
+ // { type: "tool", call: { id, name, arguments } }
31
+ // { type: "done", usage: { input, cached, output, total } }
32
+ // { type: "error", message }
33
+ // "done" or "error" is always the last one.
34
+ export async function* turnEvents(response) {
35
+ let usage = usageOf()
36
+ try {
37
+ for await (const data of dataLines(response)) {
38
+ if (data === "[DONE]") break
39
+ let chunk
40
+ try {
41
+ chunk = JSON.parse(data)
42
+ } catch {
43
+ continue
44
+ }
45
+ if (chunk.error) return void (yield { type: "error", message: String(chunk.error.message ?? "upstream error") })
46
+ if (chunk.usage) usage = usageOf(chunk.usage)
47
+ const delta = chunk.choices?.[0]?.delta
48
+ if (!delta) continue
49
+ if (delta.reasoning_content) yield { type: "reasoning", text: delta.reasoning_content }
50
+ if (delta.content) yield { type: "text", text: delta.content }
51
+ for (const call of delta.tool_calls ?? [])
52
+ yield { type: "tool", call: { id: call.id, name: call.function?.name ?? "", arguments: call.function?.arguments || "{}" } }
53
+ }
54
+ } catch (e) {
55
+ return void (yield { type: "error", message: String(e?.message ?? e) })
56
+ }
57
+ yield { type: "done", usage }
58
+ }
59
+
60
+ // A whole (not streamed) chat completion as the same things.
61
+ export function turnResult(completion) {
62
+ const message = completion?.choices?.[0]?.message ?? {}
63
+ return {
64
+ reasoning: message.reasoning_content ?? "",
65
+ text: message.content ?? "",
66
+ calls: (message.tool_calls ?? []).map((c) => ({ id: c.id, name: c.function?.name ?? "", arguments: c.function?.arguments || "{}" })),
67
+ usage: usageOf(completion?.usage),
68
+ }
69
+ }
70
+
71
+ // A streamed Response; `write(send)` sends events with send(name, data) and
72
+ // the stream closes when it returns.
73
+ export function eventStream(write) {
74
+ const encoder = new TextEncoder()
75
+ let closed = false
76
+ const body = new ReadableStream({
77
+ start(controller) {
78
+ const send = (event, data) => {
79
+ if (!closed) controller.enqueue(encoder.encode(`event: ${event}\ndata: ${JSON.stringify(data)}\n\n`))
80
+ }
81
+ Promise.resolve()
82
+ .then(() => write(send))
83
+ .catch(() => {})
84
+ .finally(() => {
85
+ if (!closed) controller.close()
86
+ closed = true
87
+ })
88
+ },
89
+ cancel() {
90
+ closed = true
91
+ },
92
+ })
93
+ return new Response(body, { status: 200, headers: SSE })
94
+ }
95
+
96
+ // What a failed chat completion said: { status, message, code }.
97
+ export async function failure(response) {
98
+ let error = {}
99
+ try {
100
+ error = (await response.json())?.error ?? {}
101
+ } catch {}
102
+ return { status: response.status, message: String(error.message ?? `HTTP ${response.status}`), code: String(error.code ?? error.type ?? "") }
103
+ }
104
+
105
+ export const textOf = (content, types = ["text"]) =>
106
+ typeof content === "string"
107
+ ? content
108
+ : Array.isArray(content)
109
+ ? content
110
+ .filter((p) => p && types.includes(p.type) && typeof p.text === "string")
111
+ .map((p) => p.text)
112
+ .join("\n")
113
+ : ""
@@ -0,0 +1,118 @@
1
+ // Where the standalone gateway keeps its settings and the signed-in account:
2
+ // ~/.aws-quick-bridge (AWS_QUICK_BRIDGE_HOME names another folder). magpie
3
+ // keeps accounts itself and never reads this.
4
+
5
+ import { randomBytes } from "node:crypto"
6
+ import { chmodSync, existsSync, mkdirSync, readFileSync, renameSync, rmSync, statSync, writeFileSync } from "node:fs"
7
+ import { homedir } from "node:os"
8
+ import { join } from "node:path"
9
+ import * as oauth from "../plugin/oauth.mjs"
10
+
11
+ export const DEFAULT_PORT = 18765
12
+ export const DEFAULT_HOST = "127.0.0.1"
13
+ const REFRESH_LEAD_MS = 10 * 60_000
14
+
15
+ export const home = () => process.env.AWS_QUICK_BRIDGE_HOME || join(homedir(), ".aws-quick-bridge")
16
+ const file = (name) => join(home(), name)
17
+
18
+ function read(name) {
19
+ try {
20
+ const value = JSON.parse(readFileSync(file(name), "utf8"))
21
+ return value && typeof value === "object" ? value : null
22
+ } catch {
23
+ return null
24
+ }
25
+ }
26
+
27
+ // Written beside the file and moved over it, readable by the owner only:
28
+ // the account file holds tokens.
29
+ function write(name, value) {
30
+ mkdirSync(home(), { recursive: true, mode: 0o700 })
31
+ const tmp = file(`${name}.${process.pid}.tmp`)
32
+ writeFileSync(tmp, JSON.stringify(value, null, 2) + "\n", { mode: 0o600 })
33
+ try {
34
+ chmodSync(tmp, 0o600)
35
+ } catch {}
36
+ renameSync(tmp, file(name))
37
+ }
38
+
39
+ // config.json: { apiKey, port, host, options }. `options` are the plugin's
40
+ // options (README), the same names magpie's plugins.json takes. The key is
41
+ // made on first use.
42
+ export function config() {
43
+ const saved = read("config.json") ?? {}
44
+ const out = {
45
+ apiKey: typeof saved.apiKey === "string" && saved.apiKey.trim() ? saved.apiKey.trim() : "",
46
+ port: Number.isInteger(saved.port) && saved.port > 0 && saved.port < 65536 ? saved.port : DEFAULT_PORT,
47
+ host: typeof saved.host === "string" && saved.host.trim() ? saved.host.trim() : DEFAULT_HOST,
48
+ options: saved.options && typeof saved.options === "object" ? saved.options : {},
49
+ }
50
+ if (!out.apiKey) {
51
+ out.apiKey = "sk-quick-" + randomBytes(24).toString("hex")
52
+ write("config.json", { ...saved, ...out })
53
+ }
54
+ return out
55
+ }
56
+
57
+ export function saveConfig(patch) {
58
+ const next = { ...(read("config.json") ?? {}), ...patch }
59
+ write("config.json", next)
60
+ return next
61
+ }
62
+
63
+ export const newKey = () => saveConfig({ apiKey: "sk-quick-" + randomBytes(24).toString("hex") }).apiKey
64
+
65
+ export const saveAccount = (auth) => write("account.json", auth)
66
+ export const removeAccount = () => rmSync(file("account.json"), { force: true })
67
+ export const accountFile = () => file("account.json")
68
+
69
+ // The signed-in account, read again when the file changes (a sign-in while
70
+ // the gateway runs is picked up) and renewed shortly before it expires.
71
+ export class Account {
72
+ #auth = null
73
+ #stamp = ""
74
+ #renewing = null
75
+
76
+ peek() {
77
+ let stamp = ""
78
+ try {
79
+ const s = statSync(file("account.json"))
80
+ stamp = `${s.mtimeMs}:${s.size}`
81
+ } catch {}
82
+ if (stamp !== this.#stamp) {
83
+ this.#stamp = stamp
84
+ const value = stamp ? read("account.json") : null
85
+ this.#auth = value?.type === "oauth" && value.access ? value : null
86
+ }
87
+ return this.#auth
88
+ }
89
+
90
+ // The account with a token that is good for a while yet; null when nobody
91
+ // is signed in. A refused renewal is thrown with signIn: "expired".
92
+ async get() {
93
+ const auth = this.peek()
94
+ if (!auth || !auth.expires || auth.expires - Date.now() > REFRESH_LEAD_MS) return auth
95
+ try {
96
+ return await this.renew()
97
+ } catch (e) {
98
+ if (e?.signIn === "expired" || auth.expires <= Date.now()) throw e
99
+ // a renewal that failed for another reason: the token still has time
100
+ return auth
101
+ }
102
+ }
103
+
104
+ // Renews the token now, whatever time it has left: Amazon Quick can refuse
105
+ // a token before it expires. Requests that arrive together share one renewal.
106
+ renew() {
107
+ const auth = this.peek()
108
+ if (!auth) return Promise.resolve(null)
109
+ this.#renewing ??= (async () => {
110
+ const next = { ...auth, ...(await oauth.refresh(auth)) }
111
+ saveAccount(next)
112
+ return next
113
+ })().finally(() => (this.#renewing = null))
114
+ return this.#renewing
115
+ }
116
+ }
117
+
118
+ export const exists = (name) => existsSync(file(name))
package/index.mjs CHANGED
@@ -2,10 +2,13 @@
2
2
  // and OpenCode. It signs in with Amazon Quick's own desktop OAuth flow and
3
3
  // answers chat completions over Amazon Quick's chat WebSocket.
4
4
 
5
- import { MODES, chatCompletions, errorResponse, failureResponse, mapModel, prepare, thinkingEffort, usageBody } from "./plugin/chat.mjs"
5
+ import { MODES, chatCompletions, compose, errorResponse, failureResponse, mapModel, prepare, thinkingEffort, usageBody } from "./plugin/chat.mjs"
6
6
  import { browserSignIn } from "./plugin/login.mjs"
7
7
  import * as messages from "./plugin/messages.mjs"
8
8
  import * as oauth from "./plugin/oauth.mjs"
9
+ import * as qbs from "./plugin/qbs.mjs"
10
+ import { accountModels, durationMs, models, settings } from "./plugin/settings.mjs"
11
+ import * as threads from "./plugin/threads.mjs"
9
12
  import * as toolbridge from "./plugin/toolbridge.mjs"
10
13
  import * as upstream from "./plugin/upstream.mjs"
11
14
  import * as wire from "./plugin/wire.mjs"
@@ -15,58 +18,8 @@ const ID = "aws-quick"
15
18
  const BASE = "https://aws-quick.invalid/v1"
16
19
  const KEEP_SESSIONS = "keep_sessions"
17
20
  const REFRESH_LEAD_MS = 10 * 60_000
18
-
19
- const MODEL_NAMES = { smart: "Quick Smart", fast: "Quick Fast", balanced: "Quick Balanced", auto: "Quick Auto" }
20
-
21
- // "30s", "5m", "1h" or a number of seconds.
22
- function durationMs(value, fallback) {
23
- if (typeof value === "number" && value > 0) return value * 1000
24
- const m = /^\s*(\d+(?:\.\d+)?)\s*(ms|s|m|h)?\s*$/.exec(String(value ?? ""))
25
- if (!m || !(Number(m[1]) > 0)) return fallback
26
- return Number(m[1]) * { ms: 1, s: 1000, m: 60_000, h: 3_600_000 }[m[2] ?? "s"]
27
- }
28
-
29
- const text = (value, fallback = "") => (typeof value === "string" && value.trim() ? value.trim() : fallback)
30
-
31
- // The plugin's options (plugins.json).
32
- function settings(options = {}) {
33
- // 0 takes any free port
34
- const port = typeof options.callback_port === "number" ? options.callback_port : Number(options.callback_port || NaN)
35
- const effort = text(options.thinking_effort).toLowerCase()
36
- return {
37
- appVersion: text(options.app_version, "1.2.4660"),
38
- surface: text(options.surface, "QUICK_WORK_DESKTOP_WINDOWS"),
39
- outpostId: text(options.outpost_id),
40
- workingSessionId: text(options.working_session_id),
41
- timezone: text(options.timezone, "UTC"),
42
- locale: text(options.locale, "en-US"),
43
- thinkingEffort: ["off", "low", "medium", "high"].includes(effort) ? effort : "",
44
- keepSessions: options.keep_sessions === true,
45
- defaultModel: mapModel(options.default_model, "smart"),
46
- requestTimeoutMs: durationMs(options.request_timeout, 5 * 60_000),
47
- proxy: text(options.proxy),
48
- provider: oauth.normalizedProvider(options.provider),
49
- stage: oauth.normalizedStage(options.build_stage),
50
- region: text(options.region),
51
- callbackPort: Number.isInteger(port) && port >= 0 && port < 65536 ? port : 18080,
52
- loginTimeoutMs: durationMs(options.login_timeout, 30 * 60_000),
53
- }
54
- }
55
-
56
- const models = () =>
57
- Object.fromEntries(
58
- MODES.map((id) => [
59
- id,
60
- {
61
- name: MODEL_NAMES[id],
62
- limit: { context: 200000, output: 32000 },
63
- tool_call: true,
64
- reasoning: true,
65
- variants: { low: {}, medium: {}, high: {} },
66
- modalities: { input: ["text"], output: ["text"] },
67
- },
68
- ]),
69
- )
21
+ // magpie's id for the conversation a request belongs to (chat.headers)
22
+ const CONVERSATION = "x-aws-quick-conversation"
70
23
 
71
24
  const ACCOUNT_TYPE = {
72
25
  type: "select",
@@ -127,6 +80,22 @@ const server = async (input, options) => {
127
80
  }
128
81
 
129
82
  return {
83
+ "chat.headers": async (hook, output) => {
84
+ if ((hook?.model?.providerID ?? hook?.provider?.info?.id) === ID && hook?.sessionID) output.headers[CONVERSATION] = String(hook.sessionID).slice(0, 128)
85
+ },
86
+ provider: {
87
+ id: ID,
88
+ // magpie asks for each account; a failure leaves the list as it was
89
+ async models(provider, { auth } = {}) {
90
+ const listed = provider?.models ?? {}
91
+ if (auth?.type !== "oauth" || !auth.access) return listed
92
+ try {
93
+ return accountModels(listed, await qbs.settings(auth))
94
+ } catch {
95
+ return listed
96
+ }
97
+ },
98
+ },
130
99
  config: async (config) => {
131
100
  config.provider ??= {}
132
101
  const was = config.provider[ID] ?? {}
@@ -144,12 +113,12 @@ const server = async (input, options) => {
144
113
  options: [
145
114
  {
146
115
  key: KEEP_SESSIONS,
147
- label: "Keep Amazon Quick sessions",
148
- description: "Leaves the conversation each request creates in Amazon Quick's own chat list instead of deleting it when the reply ends.",
149
- labels: { zh: "保留 Amazon Quick 会话", "zh-TW": "保留 Amazon Quick 對話" },
116
+ label: "Show conversations in Amazon Quick",
117
+ description: "Conversations are incognito by default: they stay out of Amazon Quick's chat list and its memory. Turn this on to have them listed there, where Amazon Quick may also save things from them to its memory.",
118
+ labels: { zh: "在 Amazon Quick 里显示对话", "zh-TW": "在 Amazon Quick 裡顯示對話" },
150
119
  descriptions: {
151
- zh: "每个请求在 Amazon Quick 里创建的会话,回复结束后不再删除,会留在 Amazon Quick 自己的聊天列表里。",
152
- "zh-TW": "每個請求在 Amazon Quick 裡建立的對話,回覆結束後不再刪除,會留在 Amazon Quick 自己的聊天列表裡。",
120
+ zh: "对话默认是隐身的:不出现在 Amazon Quick 的聊天列表里,也不写入它的记忆。打开后对话会列在那里,Amazon Quick 也可能把其中的内容存进它的记忆。",
121
+ "zh-TW": "對話預設是隱身的:不出現在 Amazon Quick 的聊天列表裡,也不寫入它的記憶。開啟後對話會列在那裡,Amazon Quick 也可能把其中的內容存進它的記憶。",
153
122
  },
154
123
  },
155
124
  ],
@@ -162,6 +131,17 @@ const server = async (input, options) => {
162
131
  authorize: (inputs, ctx) => browserSignIn(inputs, cfg(), ctx),
163
132
  },
164
133
  ],
134
+ // magpie's hook: how much of the plan is used. A refusal here never
135
+ // marks the account: only a chat request does.
136
+ async usage(getAuth) {
137
+ const auth = await getAuth()
138
+ if (auth?.type !== "oauth" || !auth.access) return { error: "not signed in", windows: [], signIn: "kept" }
139
+ try {
140
+ return { ...(await qbs.usage(auth)), signIn: "kept" }
141
+ } catch (e) {
142
+ return { error: String(e?.message ?? e), windows: [], signIn: "kept" }
143
+ }
144
+ },
165
145
  // magpie's hook: renews the account's tokens before they expire
166
146
  async refresh(auth) {
167
147
  if (auth?.type !== "oauth") return
@@ -204,7 +184,7 @@ const server = async (input, options) => {
204
184
  auth,
205
185
  cfg: cfg(),
206
186
  signal: init?.signal ?? (request instanceof Request ? request.signal : undefined),
207
- threadId: headers.get("x-thread-id") ?? "",
187
+ conversation: headers.get(CONVERSATION) ?? headers.get("x-thread-id") ?? "",
208
188
  })
209
189
  },
210
190
  }
@@ -216,4 +196,4 @@ const server = async (input, options) => {
216
196
  export default { id: "magpie-aws-quick-bridge", server }
217
197
 
218
198
  // for tests
219
- export const _internal = { settings, durationMs, models, prepare, mapModel, thinkingEffort, usageBody, failureResponse, chatCompletions, messages, oauth, toolbridge, upstream, wire }
199
+ export const _internal = { settings, durationMs, models, prepare, compose, mapModel, thinkingEffort, usageBody, failureResponse, chatCompletions, accountModels, threads, messages, oauth, qbs, toolbridge, upstream, wire }
package/opencode.mjs ADDED
@@ -0,0 +1,2 @@
1
+ // The entry magpie and OpenCode 1.x load: the plugin, and nothing else.
2
+ export { default } from "./index.mjs"