trae: a claim's extra_credits alone, the reasoning an empty field hid, a name that grows, the realm in an error (#260, moxuanchen)
A check-in claim answering extra_credits alone is credited (NaN before); an
empty reasoning_content no longer hides the event's reasoning; a native call's
name snapshot that grows takes the place of its first piece; a Global reply's
error names Trae Global.
Run at the reviewed head 2a0a835, merged with #257 and #273 on main 68170b3:
bun test in packages/trae 173 pass 0 fail; check.mjs trae ok. The new tests
fail on main's index.mjs. Not tried against a live Trae account.
This commit is contained in:
@@ -353,7 +353,9 @@ answered it 9074 even with the client's headers.
|
||||
in today, and that holds for the day. No device id is ever changed or
|
||||
rotated to get round a 9095 or a 9074, and no account is given another
|
||||
account's device or its own client's device unless the client is signed
|
||||
in as that account.
|
||||
in as that account. The credit a claim granted is its `credits` plus
|
||||
its `extra_credits`, either alone; the status answer's is the fallback
|
||||
when the claim says none.
|
||||
|
||||
Only `api.trae.cn`'s `/trae/api/` pages are sent this way; any other URL
|
||||
that isn't a chat request is still refused (400). Needs 0.1.4 or later.
|
||||
|
||||
+24
-8
@@ -2060,7 +2060,13 @@ class NativeCalls {
|
||||
this.calls.push({ ...c })
|
||||
return
|
||||
}
|
||||
// a name comes as the arguments do: a snapshot that grows. A name that
|
||||
// starts with what was read is that snapshot further along, and takes
|
||||
// its place — a wrapper cut short at a tag, then the whole call again.
|
||||
// A name that doesn't start with it is a piece of the name's own, or
|
||||
// one no snapshot explains, and the first is kept as it was
|
||||
if (!o.name) o.name = c.name
|
||||
else if (c.name && c.name.startsWith(o.name) && c.name !== o.name) o.name = c.name
|
||||
if (c.index !== null && o.index === null) o.index = c.index
|
||||
const prev = o.arguments
|
||||
const next = c.arguments
|
||||
@@ -2187,7 +2193,9 @@ async function* parts(events, tools = []) {
|
||||
if (data === "[DONE]") break
|
||||
if (name === "error" || (name !== "output" && d.code && d.message && !d.response && !d.content)) {
|
||||
const e = errorOf(d)
|
||||
yield { error: e.message || "Trae CN returned an error", code: e.code }
|
||||
// the realm answering is named by the caller, which knows it; a bare
|
||||
// word here reads as neither realm's
|
||||
yield { error: e.message || "Trae returned an error", code: e.code }
|
||||
return
|
||||
}
|
||||
if (name === "token_usage") {
|
||||
@@ -2203,9 +2211,12 @@ async function* parts(events, tools = []) {
|
||||
}
|
||||
if (!ANSWER.has(name)) continue
|
||||
const delta = d.delta && typeof d.delta === "object" ? d.delta : {}
|
||||
const reasoning = d.reasoning_content ?? d.reasoning ?? delta.reasoning_content ?? ""
|
||||
if (typeof reasoning === "string" && reasoning) {
|
||||
const r = th.add(reasoning)
|
||||
// the reasoning's own fields, in the order Trae has put it under: an
|
||||
// empty one is no answer, so the next is asked (an event carrying both
|
||||
// reasoning_content: "" and reasoning: "…" says the thought in the second)
|
||||
const said = [d.reasoning_content, d.reasoning, delta.reasoning_content, delta.reasoning].find((v) => typeof v === "string" && v) ?? ""
|
||||
if (said) {
|
||||
const r = th.add(said)
|
||||
if (r) yield { reasoning: r }
|
||||
}
|
||||
let text = typeof d.response === "string" ? d.response : typeof d.content === "string" ? d.content : typeof delta.content === "string" ? delta.content : typeof d.delta === "string" ? d.delta : ""
|
||||
@@ -2307,8 +2318,9 @@ async function openai(req, it, abort, site) {
|
||||
// a reply that failed (an error event, or Trae's stream breaking off)
|
||||
// ends with the error and [DONE], not a finish that says it stopped; the
|
||||
// stream is closed whatever happens, and an agent that goes away stops
|
||||
// the reading of Trae's
|
||||
const error = (message, code = null) => enc.encode(`data: ${JSON.stringify({ error: { message: `Trae CN: ${message}`, type: "api_error", code } })}\n\n`)
|
||||
// the reading of Trae's. The realm answering names itself, so a Global
|
||||
// account's failure reads as Trae Global's
|
||||
const error = (message, code = null) => enc.encode(`data: ${JSON.stringify({ error: { message: `${site.realm}: ${message}`, type: "api_error", code } })}\n\n`)
|
||||
let gone = false
|
||||
const stream = new ReadableStream({
|
||||
async start(ctl) {
|
||||
@@ -2614,8 +2626,12 @@ const makePlugin = (site) => async ({ client }) => {
|
||||
if (String(ce.code) === "9074") return { outcome: "ineligible", message: `${ce.message} — check in in the Trae CN app` }
|
||||
if (claim.status !== 200 || (ce.code && String(ce.code) !== "0")) return { outcome: "failed", message: ce.message || statusLine(claim.status) }
|
||||
if (!answer || typeof answer !== "object" || Array.isArray(answer)) return { outcome: "failed", message: `check-in claim: unreadable reply ${claimText.trim().slice(0, 120) || "(empty)"}` }
|
||||
const credit = Number(answer.credits) + Number(answer.extra_credits || 0)
|
||||
const fallback = Number(body.credits) + Number(body.extra_credits || 0)
|
||||
// a claim may answer extra_credits with no credits beside it, and either
|
||||
// may be absent: Number(undefined) is NaN, and NaN + n is NaN, which
|
||||
// reads as no credit at all
|
||||
const n = (v) => (Number.isFinite(Number(v)) ? Number(v) : 0)
|
||||
const credit = n(answer.credits) + n(answer.extra_credits)
|
||||
const fallback = n(body.credits) + n(body.extra_credits)
|
||||
return { outcome: "claimed", ...(credit > 0 ? { credit } : fallback > 0 ? { credit: fallback } : {}) }
|
||||
}
|
||||
|
||||
|
||||
@@ -155,3 +155,45 @@ test("a call's own arguments that aren't a set of fields are passed on, not merg
|
||||
expect(read('{"name":"bash","arguments":{"command":"ls"}}', "[1,2]").arguments).toBe("[1,2]")
|
||||
expect(read('{"name":"bash","arguments":{"command":"ls"}}', '"ls -la"').arguments).toBe('"ls -la"')
|
||||
})
|
||||
|
||||
// a name comes as the arguments do: a snapshot that grows. A name that
|
||||
// starts with what was read is that snapshot further along and takes its
|
||||
// place — a wrapper cut short at a tag, then the whole call again, which
|
||||
// was dropped with the wrapper before
|
||||
const stream = (...events) => {
|
||||
const nc = new _internal.NativeCalls()
|
||||
for (const e of events) nc.push(e)
|
||||
return nc.take(TOOLS)
|
||||
}
|
||||
const ev = (id, name, args = "") => ({ id, function: { name, arguments: args } })
|
||||
|
||||
test("a name that grows is the snapshot, not its first piece", () => {
|
||||
expect(stream(ev("n1", '{"name":"bash","arguments":{"command":"ec'), ev("n1", '{"name":"bash","arguments":{"command":"echo hi"}}'))).toEqual([
|
||||
{ id: "n1", name: "bash", arguments: '{"command":"echo hi"}' },
|
||||
])
|
||||
})
|
||||
|
||||
test("the whole name arriving after a part of it was read is the whole one", () => {
|
||||
expect(stream(ev("n1", '{"name":"bash","arguments":{"command'), ev("n1", '{"name":"bash","arguments":{"command":"ls"}}'))).toEqual([
|
||||
{ id: "n1", name: "bash", arguments: '{"command":"ls"}' },
|
||||
])
|
||||
})
|
||||
|
||||
test("a name that is no snapshot's next step keeps the first, as any other name does", () => {
|
||||
// a piece of the name's own, or one nothing explains: the first is kept
|
||||
const cut = '{"name":"bash","arguments":{"command'
|
||||
expect(stream(ev("n1", cut), ev("n1", 'sh","arguments":{"command":"ls"}}'))).toEqual([{ id: "n1", name: cut, arguments: "{}" }])
|
||||
})
|
||||
|
||||
test("a plain name with the arguments streamed in pieces is unchanged", () => {
|
||||
expect(stream(ev("n1", "bash"), ev("n1", "", '{"command":"ls'), ev("n1", "", '"}'))).toEqual([
|
||||
{ id: "n1", name: "bash", arguments: '{"command":"ls"}' },
|
||||
])
|
||||
})
|
||||
|
||||
test("two calls under one stream keep their own names", () => {
|
||||
expect(stream(ev("n1", '{"name":"bash","arguments":{"command":"ls"}}'), ev("n2", '{"name":"read","arguments":{"file_path":"a.go"}}'))).toEqual([
|
||||
{ id: "n1", name: "bash", arguments: '{"command":"ls"}' },
|
||||
{ id: "n2", name: "read", arguments: '{"file_path":"a.go"}' },
|
||||
])
|
||||
})
|
||||
|
||||
@@ -13,6 +13,26 @@ async function checkin(plugin = TraeCNAuthPlugin, auth = signedIn(), client = {}
|
||||
return hooks.auth.checkin(async () => auth)
|
||||
}
|
||||
|
||||
test("a claim answering extra_credits alone is claimed with that credit", async () => {
|
||||
f = fakeTrae()
|
||||
f.route("POST " + STATUS, () => json({ code: 0, enable: true, checked_in: false, credits: 0 }))
|
||||
f.route("POST " + CLAIM, () => json({ code: 0, extra_credits: 20 }))
|
||||
// credits absent: Number(undefined) is NaN, and NaN + 20 read as no credit
|
||||
expect(await checkin()).toEqual({ outcome: "claimed", credit: 20 })
|
||||
})
|
||||
|
||||
test("a claim answering neither credit says so, and the status's is the fallback", async () => {
|
||||
f = fakeTrae()
|
||||
f.route("POST " + STATUS, () => json({ code: 0, enable: true, checked_in: false, credits: 30 }))
|
||||
f.route("POST " + CLAIM, () => json({ code: 0 }))
|
||||
expect(await checkin()).toEqual({ outcome: "claimed", credit: 30 })
|
||||
f.close()
|
||||
f = fakeTrae()
|
||||
f.route("POST " + STATUS, () => json({ code: 0, enable: true, checked_in: false }))
|
||||
f.route("POST " + CLAIM, () => json({ code: 0 }))
|
||||
expect(await checkin()).toEqual({ outcome: "claimed" })
|
||||
})
|
||||
|
||||
test("a claimable day is claimed through Trae's own pages", async () => {
|
||||
f = fakeTrae()
|
||||
f.route("POST " + STATUS, () => json({ code: 0, enable: true, checked_in: false }))
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
// limit, and Trae's Max context.
|
||||
import "./nonet.mjs"
|
||||
import { afterEach, expect, test } from "bun:test"
|
||||
import { TraeCNAuthPlugin, _internal } from "./index.mjs"
|
||||
import { TraeCNAuthPlugin, TraeGlobalAuthPlugin, _internal } from "./index.mjs"
|
||||
import { fakeTrae, json, signedIn, sse } from "./fake.mjs"
|
||||
|
||||
let f
|
||||
@@ -113,6 +113,29 @@ test("a reply that fails mid-way ends with the error and [DONE], not a stop", as
|
||||
expect(c.at(-1)).toBe("[DONE]")
|
||||
})
|
||||
|
||||
// the realm answering names itself in its errors, so a Global account's
|
||||
// failure doesn't read as Trae CN's
|
||||
test("a Global reply that fails names Trae Global, not Trae CN", async () => {
|
||||
f = fakeTrae("trae-global")
|
||||
f.route("POST /api/agent/v3/llm_utils_chat", () => sse([["output", { response: "partial" }], ["error", { code: 500, message: "server broke" }]]))
|
||||
const hooks = await TraeGlobalAuthPlugin({ client: {} })
|
||||
const opts = await hooks.auth.loader(async () => signedIn())
|
||||
const c = await chunks(await opts.fetch(opts.baseURL + "/chat/completions", { method: "POST", body: JSON.stringify({ model: "gpt-5.4", stream: true, messages: [{ role: "user", content: "hi" }] }) }))
|
||||
expect(c.at(-2).error.message).toBe("Trae Global: server broke")
|
||||
expect(c.at(-1)).toBe("[DONE]")
|
||||
})
|
||||
|
||||
test("an empty reasoning_content leaves the reasoning the event carries", async () => {
|
||||
stream([
|
||||
["output", { reasoning_content: "", reasoning: "the real thought" }],
|
||||
["output", { response: "hi" }],
|
||||
["done", {}],
|
||||
])
|
||||
const c = await chunks(await ask({ model: "glm-5", stream: true, messages: [{ role: "user", content: "hi" }] }))
|
||||
expect(textOf(c)).toBe("hi")
|
||||
expect(reasoningOf(c)).toBe("the real thought")
|
||||
})
|
||||
|
||||
test("a reply cut at its limit says length", async () => {
|
||||
stream([["output", { response: "a lot" }], ["done", { finish_reason: "length" }]])
|
||||
const c = await chunks(await ask({ model: "glm-5", stream: true, messages: [{ role: "user", content: "hi" }] }))
|
||||
|
||||
Reference in New Issue
Block a user