@igorjpimenta/brayer-opencode 2026.11.7 → 2026.11.22-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.
- package/bin/brayer-wake.js +5 -1
- package/hooks.js +2 -1
- package/identity.js +11 -0
- package/index.js +9 -1
- package/package.json +2 -1
- package/setup.js +36 -0
- package/wake.js +59 -3
package/bin/brayer-wake.js
CHANGED
|
@@ -1,7 +1,11 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
import { startWakeServer } from "../wake.js";
|
|
3
3
|
|
|
4
|
-
|
|
4
|
+
const args = process.argv.slice(2);
|
|
5
|
+
const portIndex = args.indexOf("--port");
|
|
6
|
+
const port = portIndex >= 0 ? Number(args[portIndex + 1]) : undefined;
|
|
7
|
+
|
|
8
|
+
startWakeServer({ port }).catch((err) => {
|
|
5
9
|
console.error(err?.message ?? err);
|
|
6
10
|
process.exit(1);
|
|
7
11
|
});
|
package/hooks.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { stampBrayerIdentity } from "./identity.js";
|
|
1
|
+
import { stampBrayerIdentity, stampStartAttempt } from "./identity.js";
|
|
2
2
|
|
|
3
3
|
export function createBrayerHooks() {
|
|
4
4
|
const agents = new Map();
|
|
@@ -12,6 +12,7 @@ export function createBrayerHooks() {
|
|
|
12
12
|
},
|
|
13
13
|
"tool.execute.before": async (input, output) => {
|
|
14
14
|
stampBrayerIdentity(input, output, { agent: agents.get(input?.sessionID) ?? process.env.BRAYER_AGENT });
|
|
15
|
+
stampStartAttempt(input, output);
|
|
15
16
|
},
|
|
16
17
|
};
|
|
17
18
|
}
|
package/identity.js
CHANGED
|
@@ -15,3 +15,14 @@ export function stampBrayerIdentity(input, output, { agent = null } = {}) {
|
|
|
15
15
|
if (input?.sessionID) output.args.brayer_session = input.sessionID;
|
|
16
16
|
return true;
|
|
17
17
|
}
|
|
18
|
+
|
|
19
|
+
// The claim lease only guards the claimed→attempt window. Opening the attempt
|
|
20
|
+
// in the same call collapses that window, so a working session cannot drift
|
|
21
|
+
// into a 30-minute lease expiry while it is still doing the work.
|
|
22
|
+
export function stampStartAttempt(input, output) {
|
|
23
|
+
if (!isBrayerTool(input?.tool)) return false;
|
|
24
|
+
if (!/order_(claim|next)$/.test(String(input.tool))) return false;
|
|
25
|
+
output.args = output.args ?? {};
|
|
26
|
+
output.args.start_attempt = true;
|
|
27
|
+
return true;
|
|
28
|
+
}
|
package/index.js
CHANGED
|
@@ -1,5 +1,13 @@
|
|
|
1
1
|
import { createBrayerHooks } from "./hooks.js";
|
|
2
|
+
import { setup } from "./setup.js";
|
|
2
3
|
|
|
3
|
-
|
|
4
|
+
// The legacy V1 path object-hook form, kept until the compatibility window
|
|
5
|
+
// closes. V1 hosts call server(); they cannot read id/setup().
|
|
6
|
+
export async function BrayerLegacy() {
|
|
4
7
|
return createBrayerHooks();
|
|
5
8
|
}
|
|
9
|
+
|
|
10
|
+
// setup() first: it is the entry. server() is the legacy V1 path. Each side
|
|
11
|
+
// stays on its own API — sharing the export does not translate one into the
|
|
12
|
+
// other.
|
|
13
|
+
export default { id: "brayer", setup, server: BrayerLegacy };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@igorjpimenta/brayer-opencode",
|
|
3
|
-
"version": "2026.11.
|
|
3
|
+
"version": "2026.11.22-beta.1",
|
|
4
4
|
"description": "OpenCode companion for Brayer: stamps identity and runs the optional claim wake bridge.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -20,6 +20,7 @@
|
|
|
20
20
|
"index.js",
|
|
21
21
|
"hooks.js",
|
|
22
22
|
"identity.js",
|
|
23
|
+
"setup.js",
|
|
23
24
|
"wake.js",
|
|
24
25
|
"bin/"
|
|
25
26
|
],
|
package/setup.js
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
// setup.js — the entry. Hosts read it from the default export; the legacy
|
|
2
|
+
// server() in index.js gives a V1 host the object-hook form of the same
|
|
3
|
+
// behavior. V2 registers each hook on the domain that owns it, and the agent
|
|
4
|
+
// arrives on the session context hook rather than a chat event. The stamp is
|
|
5
|
+
// unchanged from the V1 side: the same overwrite-unconditionally rule applies,
|
|
6
|
+
// because a model-supplied pair must never survive on either host.
|
|
7
|
+
|
|
8
|
+
import { stampBrayerIdentity, stampStartAttempt } from "./identity.js";
|
|
9
|
+
|
|
10
|
+
export async function setup(ctx) {
|
|
11
|
+
// sessionID → agent, learned once per request. The context hook is the only
|
|
12
|
+
// surface that names the agent for a turn, and it runs immediately before
|
|
13
|
+
// the model request that may call a brayer tool.
|
|
14
|
+
const agents = new Map();
|
|
15
|
+
|
|
16
|
+
await ctx.session.hook("context", (event) => {
|
|
17
|
+
if (!event?.sessionID || !event?.agent) return;
|
|
18
|
+
agents.set(String(event.sessionID), String(event.agent));
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
await ctx.tool.hook("execute.before", (event) => {
|
|
22
|
+
if (!event || typeof event !== "object") return;
|
|
23
|
+
const agent = agents.get(String(event.sessionID)) ?? process.env.BRAYER_AGENT ?? null;
|
|
24
|
+
event.input = event.input && typeof event.input === "object" ? event.input : {};
|
|
25
|
+
const stamped = { args: event.input };
|
|
26
|
+
stampBrayerIdentity(
|
|
27
|
+
{ tool: event.tool, sessionID: event.sessionID },
|
|
28
|
+
stamped,
|
|
29
|
+
{ agent },
|
|
30
|
+
);
|
|
31
|
+
stampStartAttempt({ tool: event.tool }, stamped);
|
|
32
|
+
event.input = stamped.args;
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
return () => {};
|
|
36
|
+
}
|
package/wake.js
CHANGED
|
@@ -98,13 +98,52 @@ export function createWakeServer(options = {}) {
|
|
|
98
98
|
return http.createServer(createWakeHandler(options));
|
|
99
99
|
}
|
|
100
100
|
|
|
101
|
+
export async function registerWakeTarget({
|
|
102
|
+
registerUrl,
|
|
103
|
+
harness = "opencode",
|
|
104
|
+
url,
|
|
105
|
+
bearer = null,
|
|
106
|
+
timeoutMs = 3000,
|
|
107
|
+
attempts = 5,
|
|
108
|
+
retryDelayMs = 500,
|
|
109
|
+
logger = null,
|
|
110
|
+
} = {}) {
|
|
111
|
+
if (!registerUrl) return { ok: false, reason: "disabled" };
|
|
112
|
+
const endpoint = `${String(registerUrl).replace(/\/+$/, "")}/api/brayer/webhooks`;
|
|
113
|
+
const headers = { "content-type": "application/json" };
|
|
114
|
+
let last = { ok: false, reason: "error" };
|
|
115
|
+
for (let i = 0; i < attempts; i += 1) {
|
|
116
|
+
try {
|
|
117
|
+
const res = await fetch(endpoint, {
|
|
118
|
+
method: "POST",
|
|
119
|
+
headers,
|
|
120
|
+
body: JSON.stringify({ harness, url, bearer }),
|
|
121
|
+
signal: AbortSignal.timeout(timeoutMs),
|
|
122
|
+
});
|
|
123
|
+
if (res.ok) return { ok: true, status: res.status };
|
|
124
|
+
last = { ok: false, reason: `http_${res.status}` };
|
|
125
|
+
if (res.status < 500) break;
|
|
126
|
+
} catch {
|
|
127
|
+
last = { ok: false, reason: "error" };
|
|
128
|
+
}
|
|
129
|
+
if (i + 1 < attempts) await new Promise((resolve) => setTimeout(resolve, retryDelayMs));
|
|
130
|
+
}
|
|
131
|
+
logger?.warn?.(`brayer wake: register ${endpoint} failed (${last.reason})`);
|
|
132
|
+
return last;
|
|
133
|
+
}
|
|
134
|
+
|
|
101
135
|
export async function startWakeServer({
|
|
102
|
-
port
|
|
136
|
+
port,
|
|
103
137
|
bearer = process.env.BRAYER_WAKE_BEARER,
|
|
104
138
|
baseUrl = process.env.BRAYER_OPENCODE_URL ?? "http://127.0.0.1:4096",
|
|
139
|
+
harness = process.env.BRAYER_WAKE_HARNESS ?? "opencode",
|
|
140
|
+
registerUrl = process.env.BRAYER_REGISTER_URL
|
|
141
|
+
?? `http://127.0.0.1:${process.env.BRAYER_HTTP_PORT ?? 38293}`,
|
|
105
142
|
logger = console,
|
|
106
143
|
} = {}) {
|
|
107
144
|
if (!bearer) throw new Error("brayer wake: BRAYER_WAKE_BEARER is required");
|
|
145
|
+
const resolvedPort = port ?? (process.env.BRAYER_WAKE_PORT ? Number(process.env.BRAYER_WAKE_PORT) : 0);
|
|
146
|
+
if (!Number.isInteger(resolvedPort) || resolvedPort < 0) throw new Error("brayer wake: port must be a non-negative integer");
|
|
108
147
|
const { createOpencodeClient } = await import("@opencode-ai/sdk");
|
|
109
148
|
const client = createOpencodeClient({ baseUrl });
|
|
110
149
|
const server = createWakeServer({
|
|
@@ -118,8 +157,25 @@ export async function startWakeServer({
|
|
|
118
157
|
});
|
|
119
158
|
await new Promise((resolve, reject) => {
|
|
120
159
|
server.once("error", reject);
|
|
121
|
-
server.listen(
|
|
160
|
+
server.listen(resolvedPort, "127.0.0.1", resolve);
|
|
122
161
|
});
|
|
123
|
-
|
|
162
|
+
const actualPort = server.address()?.port ?? resolvedPort;
|
|
163
|
+
const wakeUrl = `http://127.0.0.1:${actualPort}/api/brayer/claimed`;
|
|
164
|
+
logger?.info?.(`brayer wake: listening on ${wakeUrl}`);
|
|
165
|
+
const registered = await registerWakeTarget({
|
|
166
|
+
registerUrl,
|
|
167
|
+
harness,
|
|
168
|
+
url: wakeUrl,
|
|
169
|
+
bearer,
|
|
170
|
+
logger,
|
|
171
|
+
});
|
|
172
|
+
if (registered.ok) {
|
|
173
|
+
logger?.info?.(`brayer wake: registered ${harness} at ${wakeUrl}`);
|
|
174
|
+
} else {
|
|
175
|
+
logger?.warn?.(
|
|
176
|
+
`brayer wake: not registered (${registered.reason}); register manually: ` +
|
|
177
|
+
`brayer webhook set --harness ${harness} --url ${wakeUrl} --bearer <bearer>`
|
|
178
|
+
);
|
|
179
|
+
}
|
|
124
180
|
return server;
|
|
125
181
|
}
|