diff --git a/packages/devin/index.mjs b/packages/devin/index.mjs index 7455547..7dfff7b 100644 --- a/packages/devin/index.mjs +++ b/packages/devin/index.mjs @@ -1179,8 +1179,12 @@ function build(chat, uid, key) { for (const m of msgs) out.bytes(3, encodeMsg(m)) out.varint(7, 5) const max = chat.max_completion_tokens || chat.max_tokens || 128000 // the server holds it to the model's own - const temp = typeof chat.temperature === "number" ? chat.temperature : 1 - const topP = typeof chat.top_p === "number" ? chat.top_p : 0.95 + // Devin answers "an internal error occurred" to a temperature or top_p of + // exactly 0 (on every model: its Claude ones and swe-2), where 1e-6 goes + // through; a 0 is sent as the smallest step above it, as near greedy + const above0 = (v) => (v === 0 ? 1e-6 : v) + const temp = above0(typeof chat.temperature === "number" ? chat.temperature : 1) + const topP = above0(typeof chat.top_p === "number" ? chat.top_p : 0.95) out.bytes(8, new PB().varint(1, 1).varint(2, max).varint(3, 400).double(5, temp).varint(7, 40).double(8, topP)) // the caller's tools, and any the conversation used that it no longer diff --git a/packages/devin/package.json b/packages/devin/package.json index 13dfbf5..eb43659 100644 --- a/packages/devin/package.json +++ b/packages/devin/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-devin-auth", - "version": "0.1.11", + "version": "0.1.12", "description": "Devin subscriptions (the devin CLI's account) in OpenCode and magpie", "type": "module", "main": "./index.mjs", diff --git a/packages/devin/sampling.test.mjs b/packages/devin/sampling.test.mjs new file mode 100644 index 0000000..ea84c0c --- /dev/null +++ b/packages/devin/sampling.test.mjs @@ -0,0 +1,36 @@ +// Devin answers "an internal error occurred" to a temperature or top_p of +// exactly 0 (Hermes's command approvals send temperature 0): a 0 goes to +// Devin as 1e-6, any other value as it came. +import { expect, test } from "bun:test" +import { _internal } from "./index.mjs" + +// sampling is the temperature and top_p in the request's field 8. +const sampling = (chat) => { + const f8 = _internal.fields(_internal.build(chat, "claude-opus-5-5", "k")).find((f) => f.num === 8) + const out = {} + const b = f8.data + const view = new DataView(b.buffer, b.byteOffset, b.byteLength) + for (let i = 0; i < b.length; ) { + const key = b[i++] + if ((key & 7) === 0) while (b[i++] & 0x80); + else if ((key & 7) === 1) { + out[key >> 3] = view.getFloat64(i, true) + i += 8 + } else throw new Error(`unexpected wire type in field 8: ${key & 7}`) + } + return { temperature: out[5], top_p: out[8] } +} + +const say = { messages: [{ role: "user", content: "hi" }] } + +test("a temperature or top_p of 0 goes to Devin as 1e-6", () => { + expect(sampling({ ...say, temperature: 0 })).toEqual({ temperature: 1e-6, top_p: 0.95 }) + expect(sampling({ ...say, top_p: 0 })).toEqual({ temperature: 1, top_p: 1e-6 }) + expect(sampling({ ...say, temperature: -0, top_p: 0 })).toEqual({ temperature: 1e-6, top_p: 1e-6 }) +}) + +test("any other temperature or top_p goes as it came, and none as Devin's defaults", () => { + expect(sampling(say)).toEqual({ temperature: 1, top_p: 0.95 }) + expect(sampling({ ...say, temperature: 0.2, top_p: 1 })).toEqual({ temperature: 0.2, top_p: 1 }) + expect(sampling({ ...say, temperature: 1e-6 })).toEqual({ temperature: 1e-6, top_p: 0.95 }) +})