@igorjpimenta/brayer-opencode 2026.10.28-beta.3 → 2026.11.3-beta.1

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,7 @@
1
+ #!/usr/bin/env node
2
+ import { startWakeServer } from "../wake.js";
3
+
4
+ startWakeServer().catch((err) => {
5
+ console.error(err?.message ?? err);
6
+ process.exit(1);
7
+ });
package/identity.js CHANGED
@@ -1,3 +1,9 @@
1
+ // identity.js — opencode can mutate tool args but not request _meta, so the
2
+ // brayer server accepts a whole agent+session pair as brayer_agent/
3
+ // brayer_session args (src/identity.js, args channel). The hook overwrites
4
+ // both unconditionally: a model-supplied pair must never survive. The harness
5
+ // is not an argument — the MCP connection declares it via X-Brayer-Harness.
6
+
1
7
  export function isBrayerTool(name) {
2
8
  return typeof name === "string" && name.includes("brayer");
3
9
  }
@@ -5,7 +11,7 @@ export function isBrayerTool(name) {
5
11
  export function stampBrayerIdentity(input, output, { agent = null } = {}) {
6
12
  if (!isBrayerTool(input?.tool)) return false;
7
13
  output.args = output.args ?? {};
8
- if (!output.args.brayer_agent && agent) output.args.brayer_agent = agent;
9
- if (!output.args.brayer_session) output.args.brayer_session = input.sessionID;
14
+ if (agent) output.args.brayer_agent = agent;
15
+ if (input?.sessionID) output.args.brayer_session = input.sessionID;
10
16
  return true;
11
17
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@igorjpimenta/brayer-opencode",
3
- "version": "2026.10.28-beta.3",
4
- "description": "OpenCode companion for Brayer: stamps agent and session identity on Brayer tool calls.",
3
+ "version": "2026.11.3-beta.1",
4
+ "description": "OpenCode companion for Brayer: stamps identity and runs the optional claim wake bridge.",
5
5
  "license": "MIT",
6
6
  "type": "module",
7
7
  "repository": {
@@ -19,7 +19,15 @@
19
19
  "files": [
20
20
  "index.js",
21
21
  "hooks.js",
22
- "identity.js"
22
+ "identity.js",
23
+ "wake.js",
24
+ "bin/"
23
25
  ],
24
- "main": "index.js"
26
+ "main": "index.js",
27
+ "bin": {
28
+ "brayer-wake": "bin/brayer-wake.js"
29
+ },
30
+ "dependencies": {
31
+ "@opencode-ai/sdk": "1.19.3"
32
+ }
25
33
  }
package/wake.js ADDED
@@ -0,0 +1,125 @@
1
+ import http from "node:http";
2
+ import { timingSafeEqual } from "node:crypto";
3
+
4
+ const MAX_BODY_BYTES = 1024 * 1024;
5
+
6
+ function safeEqual(a, b) {
7
+ const ab = Buffer.from(String(a ?? ""));
8
+ const bb = Buffer.from(String(b ?? ""));
9
+ return ab.length === bb.length && timingSafeEqual(ab, bb);
10
+ }
11
+
12
+ async function readJson(req, res) {
13
+ let size = 0;
14
+ let raw = "";
15
+ for await (const chunk of req) {
16
+ size += chunk.length;
17
+ if (size > MAX_BODY_BYTES) {
18
+ res.statusCode = 413;
19
+ res.end(JSON.stringify({ error: "body too large" }));
20
+ return undefined;
21
+ }
22
+ raw += chunk;
23
+ }
24
+ if (!raw) return {};
25
+ try {
26
+ return JSON.parse(raw);
27
+ } catch {
28
+ res.statusCode = 400;
29
+ res.end(JSON.stringify({ error: "invalid json body" }));
30
+ return undefined;
31
+ }
32
+ }
33
+
34
+ function json(res, status, body) {
35
+ res.statusCode = status;
36
+ res.setHeader("content-type", "application/json");
37
+ res.end(JSON.stringify(body));
38
+ }
39
+
40
+ export function claimError(body) {
41
+ if (!body || typeof body !== "object") return "body must be an object";
42
+ if (body.event === "webhook.test") return null;
43
+ if (body.event !== "order.claimed") return `unsupported event ${JSON.stringify(body.event)}`;
44
+ if (!body.order || typeof body.order.id !== "string" || !body.order.id) return "order.id required";
45
+ if (!body.session || typeof body.session.sessionKey !== "string" || !body.session.sessionKey) {
46
+ return "session.sessionKey required";
47
+ }
48
+ return null;
49
+ }
50
+
51
+ export function claimMessage(body) {
52
+ const order = body.order;
53
+ const lines = [`Claimed Brayer order ${order.id}: ${order.title ?? "(untitled)"}`];
54
+ if (order.acceptance) lines.push(`Acceptance: ${order.acceptance}`);
55
+ lines.push(`Attach with BRAYER_ORDER_ID=${order.id} and report through the Brayer tools.`);
56
+ return lines.join("\n");
57
+ }
58
+
59
+ export function createWakeHandler({ prompt, bearer, logger } = {}) {
60
+ return async (req, res) => {
61
+ if (req.method !== "POST") {
62
+ res.statusCode = 405;
63
+ res.setHeader("allow", "POST");
64
+ res.end(JSON.stringify({ error: "method not allowed" }));
65
+ return;
66
+ }
67
+ if (!bearer) {
68
+ json(res, 503, { error: "brayer wake not configured (BRAYER_WAKE_BEARER)" });
69
+ return;
70
+ }
71
+ const auth = req.headers?.authorization ?? "";
72
+ if (!auth.startsWith("Bearer ") || !safeEqual(auth.slice(7), bearer)) {
73
+ json(res, 401, { error: "unauthorized" });
74
+ return;
75
+ }
76
+ const body = await readJson(req, res);
77
+ if (body === undefined) return;
78
+ const shapeErr = claimError(body);
79
+ if (shapeErr) {
80
+ json(res, 400, { error: shapeErr });
81
+ return;
82
+ }
83
+ if (body.event === "webhook.test") {
84
+ json(res, 200, { ok: true, test: true });
85
+ return;
86
+ }
87
+ try {
88
+ await prompt(body.session.sessionKey, claimMessage(body));
89
+ json(res, 200, { ok: true, sessionKey: body.session.sessionKey });
90
+ } catch (err) {
91
+ logger?.warn?.(`brayer wake: ${err?.message ?? err}`);
92
+ json(res, 502, { error: "wake failed" });
93
+ }
94
+ };
95
+ }
96
+
97
+ export function createWakeServer(options = {}) {
98
+ return http.createServer(createWakeHandler(options));
99
+ }
100
+
101
+ export async function startWakeServer({
102
+ port = 38295,
103
+ bearer = process.env.BRAYER_WAKE_BEARER,
104
+ baseUrl = process.env.BRAYER_OPENCODE_URL ?? "http://127.0.0.1:4096",
105
+ logger = console,
106
+ } = {}) {
107
+ if (!bearer) throw new Error("brayer wake: BRAYER_WAKE_BEARER is required");
108
+ const { createOpencodeClient } = await import("@opencode-ai/sdk");
109
+ const client = createOpencodeClient({ baseUrl });
110
+ const server = createWakeServer({
111
+ bearer,
112
+ logger,
113
+ prompt: (sessionID, text) =>
114
+ client.session.prompt({
115
+ path: { id: sessionID },
116
+ body: { parts: [{ type: "text", text }] },
117
+ }),
118
+ });
119
+ await new Promise((resolve, reject) => {
120
+ server.once("error", reject);
121
+ server.listen(port, "127.0.0.1", resolve);
122
+ });
123
+ logger?.info?.(`brayer wake: listening on http://127.0.0.1:${port}/api/brayer/claimed`);
124
+ return server;
125
+ }