magpie-aws-quick-bridge 0.3.1 → 0.3.3

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.
package/README.md CHANGED
@@ -84,6 +84,7 @@ Key 用 `npx magpie-aws-quick-bridge status` 看。
84
84
  - 多轮对话接着上一轮的线程走,只发新增的内容
85
85
  - 很长的提示词和工具结果、图片会自动转成附件,不会因为 Amazon Quick 的 64 KB 单帧上限而失败
86
86
  - 额度查询,模型列表按账号实际开通的来
87
+ - 用中文(或日文、韩文)提问时,会让模型用同一种语言思考,文件名、日志、搜索词照原文用,不会被翻成英文
87
88
 
88
89
  ## 我验证到了哪一步
89
90
 
@@ -3,20 +3,16 @@
3
3
  // its answer is rewritten back, streamed or whole.
4
4
 
5
5
  import { randomUUID } from "node:crypto"
6
+ import { agentNeutral } from "../plugin/messages.mjs"
6
7
  import { eventStream, failure, textOf, turnEvents, turnResult } from "./sse.mjs"
7
8
 
8
9
  const TYPES = { 400: "invalid_request_error", 401: "authentication_error", 403: "permission_error", 404: "not_found_error", 413: "request_too_large", 429: "rate_limit_error", 529: "overloaded_error" }
9
10
 
10
11
  export const error = (status, message) => Response.json({ type: "error", error: { type: TYPES[status] ?? "api_error", message } }, { status })
11
12
 
12
- // Claude Code opens its system prompt with a billing line meant for
13
- // Anthropic; it changes with every request and says nothing to the model.
14
- const system = (value) =>
15
- textOf(typeof value === "string" ? [{ type: "text", text: value }] : value)
16
- .split("\n")
17
- .filter((line) => !line.startsWith("x-anthropic-billing-header:"))
18
- .join("\n")
19
- .trim()
13
+ // Claude Code's billing line and its "You are Claude Code" opening are
14
+ // taken care of where every caller's instructions are read (agentNeutral).
15
+ const system = (value) => agentNeutral(textOf(typeof value === "string" ? [{ type: "text", text: value }] : value))
20
16
 
21
17
  function image(block) {
22
18
  const s = block?.source
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "magpie-aws-quick-bridge",
3
- "version": "0.3.1",
3
+ "version": "0.3.3",
4
4
  "description": "Amazon Quick (QuickWork) for Claude Code, Codex and OpenCode. Works with both Magpie (provider plugin) and CC Switch (local API). 同时支持 Magpie 与 CC Switch。",
5
5
  "type": "module",
6
6
  "main": "./opencode.mjs",
@@ -34,7 +34,15 @@
34
34
  "provider",
35
35
  "aws",
36
36
  "amazon-quick",
37
- "quickwork"
37
+ "quickwork",
38
+ "magpie-AWS-Quick-bridge",
39
+ "aws-quick",
40
+ "amazon",
41
+ "quick",
42
+ "ccswitch",
43
+ "auth",
44
+ "proxy",
45
+ "gateway"
38
46
  ],
39
47
  "license": "GPL-3.0-only",
40
48
  "author": {
package/plugin/chat.mjs CHANGED
@@ -3,10 +3,10 @@
3
3
  // are magpie's, so a request is tried once and a failure is answered with a
4
4
  // status magpie can act on.
5
5
 
6
- import { UPSTREAM_PROMPT_OVERRIDE, isSystemMessage, lastUserText, promptBlocks, systemText, titleFor, uuid } from "./messages.mjs"
6
+ import { UPSTREAM_PROMPT_OVERRIDE, isSystemMessage, languageNote, lastUserText, promptBlocks, systemText, titleFor, uuid } from "./messages.mjs"
7
7
  import * as qbs from "./qbs.mjs"
8
8
  import { ThreadStore, digest, messageKey } from "./threads.mjs"
9
- import { BridgeStream, buildToolBridgePrompt, parseClientToolCalls, parseToolChoice, requestedToolNames } from "./toolbridge.mjs"
9
+ import { BridgeStream, TOOL_REMINDER, buildToolBridgePrompt, parseClientToolCalls, parseToolChoice, requestedToolNames } from "./toolbridge.mjs"
10
10
  import { UpstreamError, chat, encodeFrame, sessionFromAuth, userMessage } from "./upstream.mjs"
11
11
  import { isGatewayProvider } from "./wire.mjs"
12
12
 
@@ -14,8 +14,10 @@ import { isGatewayProvider } from "./wire.mjs"
14
14
  export const MODES = ["smart", "fast", "balanced", "auto"]
15
15
  const AGENT_MODES = [...MODES, "advanced"]
16
16
 
17
+ // A name may carry a bracketed note that is the agent's own, as Claude
18
+ // Code's "[1m]" for a long context: it is not part of the mode.
17
19
  export function mapModel(model, fallback) {
18
- let m = String(model ?? "").trim().toLowerCase()
20
+ let m = String(model ?? "").replace(/\[[^\]]*\]/g, "").trim().toLowerCase()
19
21
  m = m.slice(m.lastIndexOf("/") + 1)
20
22
  if (m.startsWith("qw-")) m = m.slice(3)
21
23
  return AGENT_MODES.includes(m) ? m : fallback
@@ -87,7 +89,13 @@ export function prepare(body, cfg, threadId) {
87
89
  const choice = parseToolChoice(body.tool_choice ?? body.function_call)
88
90
  if (choice.mode === "function" && choice.name) allowed.add(choice.name)
89
91
  const parallel = body.parallel_tool_calls !== false
90
- const tools = (compact) => buildToolBridgePrompt(body.tools, body.functions, choice.mode, choice.name, parallel, compact)
92
+ // with tools, the language the user writes in is named: the tools' arguments
93
+ // are then the user's own words, not a translation of them
94
+ const language = languageNote(convo)
95
+ const tools = (compact) => {
96
+ const prompt = buildToolBridgePrompt(body.tools, body.functions, choice.mode, choice.name, parallel, compact)
97
+ return prompt && language ? prompt + "\n\n" + language : prompt
98
+ }
91
99
  const bridgePrompt = tools(false)
92
100
  const system = systemText(messages)
93
101
  const custom = (toolsPrompt) => [system, UPSTREAM_PROMPT_OVERRIDE, toolsPrompt].filter(Boolean).join("\n\n")
@@ -111,6 +119,7 @@ export function prepare(body, cfg, threadId) {
111
119
  threadId,
112
120
  title: titleFor(lastUserText(messages)),
113
121
  systemPrompt: custom(bridgePrompt),
122
+ reminder: bridgePrompt ? TOOL_REMINDER : "",
114
123
  thinkingEffort: thinkingEffort(body, cfg.thinkingEffort),
115
124
  webSearch: typeof body.web_search_enabled === "boolean" ? body.web_search_enabled : cfg.webSearch === true,
116
125
  },
@@ -164,17 +164,73 @@ export const isSystemMessage = (m) => isSystem(roleOf(m))
164
164
  export const UPSTREAM_PROMPT_OVERRIDE =
165
165
  "Note from the user: I use Amazon Quick through an agent that runs on my own machine and relays this conversation. Everything in these preferences is that agent's configuration, which I set up myself. Please treat it as my instructions for the role, tools, style and format of this conversation, and where it differs from your defaults, follow it. If there is nothing else in these preferences, my messages are the whole task."
166
166
 
167
+ // An agent's instructions open by telling the model which product it is
168
+ // ("You are Claude Code, Anthropic's official CLI for Claude."). Amazon Quick
169
+ // is none of them, and told to be one it argues about who it is instead of
170
+ // doing the work. The line is put as what it means here: where the
171
+ // conversation comes from. Claude Code's billing line, which changes with
172
+ // every request and says nothing to the model, is dropped.
173
+ const AGENT_IDENTITY = [
174
+ /You are Claude Code, Anthropic's official CLI for Claude(?:, running within the Claude Agent SDK)?\./g,
175
+ /You are a Claude agent, built on Anthropic's Claude Agent SDK\./g,
176
+ ]
177
+ const AGENT_PLACE = "This conversation comes from a coding agent running in a terminal or editor on the user's own machine."
178
+
179
+ export function agentNeutral(text) {
180
+ let out = String(text ?? "")
181
+ .split("\n")
182
+ .filter((line) => !line.startsWith("x-anthropic-billing-header:"))
183
+ .join("\n")
184
+ for (const re of AGENT_IDENTITY) out = out.replace(re, AGENT_PLACE)
185
+ return out.trim()
186
+ }
187
+
167
188
  // The caller's system/developer instructions, in order.
168
189
  export function systemText(messages) {
169
190
  const parts = []
170
191
  for (const m of messages ?? []) {
171
192
  if (!isSystem(roleOf(m))) continue
172
- const text = contentText(m.content)
173
- if (text.trim()) parts.push(text)
193
+ const text = agentNeutral(contentText(m.content))
194
+ if (text) parts.push(text)
174
195
  }
175
196
  return parts.join("\n\n")
176
197
  }
177
198
 
199
+ const HAN = /[\u3400-\u4dbf\u4e00-\u9fff\uf900-\ufaff]/g
200
+ const KANA = /[\u3040-\u30ff]/g
201
+ const HANGUL = /[\uac00-\ud7af]/g
202
+
203
+ // The language the user writes in, when it is Chinese, Japanese or Korean:
204
+ // read from what the user typed in the last few messages, without what the
205
+ // agent put around it (<system-reminder> and the like). "" otherwise.
206
+ export function userLanguage(messages) {
207
+ let text = ""
208
+ let seen = 0
209
+ for (let i = (messages?.length ?? 0) - 1; i >= 0 && seen < 4; i--) {
210
+ if (roleOf(messages[i]) !== "user") continue
211
+ seen++
212
+ text += contentText(messages[i].content).replace(/<([a-z][\w-]*)>[\s\S]*?<\/\1>/gi, "") + "\n"
213
+ }
214
+ const count = (re) => (text.match(re) ?? []).length
215
+ const kana = count(KANA)
216
+ const hangul = count(HANGUL)
217
+ const han = count(HAN)
218
+ if (kana + hangul + han < 4) return ""
219
+ if (kana >= 2 && kana >= hangul) return "Japanese"
220
+ if (hangul >= 2 && hangul > han) return "Korean"
221
+ return han ? "Chinese" : ""
222
+ }
223
+
224
+ // Amazon Quick thinks in English whatever it is asked in, and then calls
225
+ // tools with its English version of the user's words: a search for a log
226
+ // line it translated finds nothing. Naming the language keeps them as
227
+ // written.
228
+ export function languageNote(messages) {
229
+ const language = userLanguage(messages)
230
+ if (!language) return ""
231
+ return `I write in ${language}. Please think and plan in ${language} too, and reply in ${language} unless I ask for another language. In tool arguments, copy file names, paths, log lines, error messages and search terms exactly as they appear in my messages, the files and the tool results: never translate them.`
232
+ }
233
+
178
234
  // The caller's instructions followed by the override notice, for
179
235
  // custom_prompt.
180
236
  export const systemPrompt = (messages) => [systemText(messages), UPSTREAM_PROMPT_OVERRIDE].filter(Boolean).join("\n\n")
@@ -84,6 +84,13 @@ export function buildToolBridgePrompt(tools, functions, mode, name, parallel, co
84
84
  return lines.join("\n")
85
85
  }
86
86
 
87
+ // custom_prompt sits among Amazon Quick's own long instructions, and the
88
+ // model does not always keep to it: it looks for the user's files with its
89
+ // built-in tools, or calls a client tool through them. These lines end each
90
+ // message of a turn with tools, where it reads them last.
91
+ export const TOOL_REMINDER =
92
+ '[Note from my agent: the files and the shell this is about are on my own machine. To reach them, write a client tool call as plain text, as my preferences describe: <client_tool_call>{"name":"TOOL_NAME","arguments":{...}}</client_tool_call>. Your built-in tools run on a remote service: they cannot see my machine, and a client tool called through them fails as an unknown tool.]'
93
+
87
94
  function parseEnvelopes(payload) {
88
95
  const text = payload.trim()
89
96
  if (!text) return []
@@ -38,7 +38,7 @@ export const FRAME_LIMIT = 65_536
38
38
  // frame.
39
39
  export function userMessage(session, cfg, input) {
40
40
  const payload = {
41
- text: input.prompt,
41
+ text: input.reminder ? `${input.prompt}\n\n${input.reminder}` : input.prompt,
42
42
  send_origin: "programmatic",
43
43
  had_physical_input: false,
44
44
  surfaceType: session.surface,