Skip to content

Commit 0efeed7

Browse files
fix: preserve OpenCode lifecycle event ordering
Co-authored-by: Shukan Shah <shukanshah14@gmail.com>
1 parent 244de65 commit 0efeed7

3 files changed

Lines changed: 13 additions & 15 deletions

File tree

cli/beacon/internal/endpoint/hooks/assets/opencode/beacon.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ export const BeaconEndpointPlugin = async ({ project, directory, worktree, clien
208208
return {
209209
"chat.message": async (input, output) => {
210210
if (output?.message?.id) messageRoles.set(output.message.id, "user")
211-
await sendToBeacon(client, {
211+
await enqueue({
212212
type: "chat.message",
213213
session_id: sessionID(input),
214214
model: modelName(input),
@@ -218,8 +218,7 @@ export const BeaconEndpointPlugin = async ({ project, directory, worktree, clien
218218
})
219219
},
220220
"command.execute.before": async (input, output) => {
221-
await sendToBeacon(
222-
client,
221+
await enqueue(
223222
payload("command.execute.before", {
224223
session_id: sessionID(input),
225224
command_name: input?.command,
@@ -237,8 +236,7 @@ export const BeaconEndpointPlugin = async ({ project, directory, worktree, clien
237236
started_at: Date.now(),
238237
})
239238
rememberFilePath(input?.tool, output?.args || {}, sid, input?.callID)
240-
await sendToBeacon(
241-
client,
239+
await enqueue(
242240
payload("tool.execute.before", {
243241
session_id: sessionID(input),
244242
tool_name: input?.tool,
@@ -251,8 +249,7 @@ export const BeaconEndpointPlugin = async ({ project, directory, worktree, clien
251249
const active = activeCalls.get(input?.callID)
252250
const args = input?.args || active?.args || {}
253251
rememberFilePath(input?.tool, args, sessionID(input), input?.callID)
254-
await sendToBeacon(
255-
client,
252+
await enqueue(
256253
payload("tool.execute.after", {
257254
session_id: sessionID(input),
258255
tool_name: input?.tool,

plugins/opencode-beacon/src/beacon.test.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ describe("BeaconEndpointPlugin", () => {
2626
test("forwards prompts and correlated tool lifecycle payloads", async () => {
2727
const hooks = await plugin()
2828

29+
await hooks.event!({
30+
event: { type: "session.created", properties: { sessionID: "ses_1", info: { id: "ses_1" } } },
31+
} as any)
2932
await hooks["chat.message"]!(
3033
{ sessionID: "ses_1", model: { providerID: "moonshotai", modelID: "kimi-k3" } } as any,
3134
{ parts: [{ type: "text", text: "summarize" }] } as any,
@@ -40,11 +43,12 @@ describe("BeaconEndpointPlugin", () => {
4043
)
4144

4245
expect(payloads.map((item) => item.type)).toEqual([
46+
"session.created",
4347
"chat.message",
4448
"tool.execute.before",
4549
"tool.execute.after",
4650
])
47-
expect(payloads[2]).toMatchObject({
51+
expect(payloads[3]).toMatchObject({
4852
session_id: "ses_1",
4953
call_id: "call_1",
5054
tool_name: "bash",

plugins/opencode-beacon/src/beacon.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ export const BeaconEndpointPlugin = async ({ project, directory, worktree, clien
208208
return {
209209
"chat.message": async (input, output) => {
210210
if (output?.message?.id) messageRoles.set(output.message.id, "user")
211-
await sendToBeacon(client, {
211+
await enqueue({
212212
type: "chat.message",
213213
session_id: sessionID(input),
214214
model: modelName(input),
@@ -218,8 +218,7 @@ export const BeaconEndpointPlugin = async ({ project, directory, worktree, clien
218218
})
219219
},
220220
"command.execute.before": async (input, output) => {
221-
await sendToBeacon(
222-
client,
221+
await enqueue(
223222
payload("command.execute.before", {
224223
session_id: sessionID(input),
225224
command_name: input?.command,
@@ -237,8 +236,7 @@ export const BeaconEndpointPlugin = async ({ project, directory, worktree, clien
237236
started_at: Date.now(),
238237
})
239238
rememberFilePath(input?.tool, output?.args || {}, sid, input?.callID)
240-
await sendToBeacon(
241-
client,
239+
await enqueue(
242240
payload("tool.execute.before", {
243241
session_id: sessionID(input),
244242
tool_name: input?.tool,
@@ -251,8 +249,7 @@ export const BeaconEndpointPlugin = async ({ project, directory, worktree, clien
251249
const active = activeCalls.get(input?.callID)
252250
const args = input?.args || active?.args || {}
253251
rememberFilePath(input?.tool, args, sessionID(input), input?.callID)
254-
await sendToBeacon(
255-
client,
252+
await enqueue(
256253
payload("tool.execute.after", {
257254
session_id: sessionID(input),
258255
tool_name: input?.tool,

0 commit comments

Comments
 (0)