Skip to content

Commit 3dd0914

Browse files
authored
refactor(tool): Tool.Context.metadata returns Effect (#21972)
1 parent 9581bf0 commit 3dd0914

17 files changed

Lines changed: 63 additions & 67 deletions

packages/opencode/src/cli/cmd/debug/agent.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ async function createToolContext(agent: Agent.Info) {
158158
agent: agent.name,
159159
abort: new AbortController().signal,
160160
messages: [],
161-
metadata: () => {},
161+
metadata: () => Effect.void,
162162
ask(req: Omit<Permission.Request, "id" | "sessionID" | "tool">) {
163163
return Effect.sync(() => {
164164
for (const pattern of req.patterns) {

packages/opencode/src/session/prompt.ts

Lines changed: 22 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -364,21 +364,19 @@ NOTE: At any point in time through this workflow you should feel free to ask the
364364
agent: input.agent.name,
365365
messages: input.messages,
366366
metadata: (val) =>
367-
run.promise(
368-
input.processor.updateToolCall(options.toolCallId, (match) => {
369-
if (!["running", "pending"].includes(match.state.status)) return match
370-
return {
371-
...match,
372-
state: {
373-
title: val.title,
374-
metadata: val.metadata,
375-
status: "running",
376-
input: args,
377-
time: { start: Date.now() },
378-
},
379-
}
380-
}),
381-
),
367+
input.processor.updateToolCall(options.toolCallId, (match) => {
368+
if (!["running", "pending"].includes(match.state.status)) return match
369+
return {
370+
...match,
371+
state: {
372+
title: val.title,
373+
metadata: val.metadata,
374+
status: "running",
375+
input: args,
376+
time: { start: Date.now() },
377+
},
378+
}
379+
}),
382380
ask: (req) =>
383381
permission
384382
.ask({
@@ -592,17 +590,14 @@ NOTE: At any point in time through this workflow you should feel free to ask the
592590
callID: part.callID,
593591
extra: { bypassAgentCheck: true, promptOps },
594592
messages: msgs,
595-
metadata(val: { title?: string; metadata?: Record<string, any> }) {
596-
return run.promise(
597-
Effect.gen(function* () {
598-
part = yield* sessions.updatePart({
599-
...part,
600-
type: "tool",
601-
state: { ...part.state, ...val },
602-
} satisfies MessageV2.ToolPart)
603-
}),
604-
)
605-
},
593+
metadata: (val: { title?: string; metadata?: Record<string, any> }) =>
594+
Effect.gen(function* () {
595+
part = yield* sessions.updatePart({
596+
...part,
597+
type: "tool",
598+
state: { ...part.state, ...val },
599+
} satisfies MessageV2.ToolPart)
600+
}),
606601
ask: (req: any) =>
607602
permission
608603
.ask({
@@ -1054,7 +1049,7 @@ NOTE: At any point in time through this workflow you should feel free to ask the
10541049
messageID: info.id,
10551050
extra: { bypassCwdCheck: true, ...extra },
10561051
messages: [],
1057-
metadata: () => {},
1052+
metadata: () => Effect.void,
10581053
ask: () => Effect.void,
10591054
})
10601055
.pipe(Effect.onInterrupt(() => Effect.sync(() => controller.abort())))

packages/opencode/src/tool/bash.ts

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,7 @@ export const BashTool = Tool.define(
385385
let expired = false
386386
let aborted = false
387387

388-
ctx.metadata({
388+
yield* ctx.metadata({
389389
metadata: {
390390
output: "",
391391
description: input.description,
@@ -397,16 +397,15 @@ export const BashTool = Tool.define(
397397
const handle = yield* spawner.spawn(cmd(input.shell, input.name, input.command, input.cwd, input.env))
398398

399399
yield* Effect.forkScoped(
400-
Stream.runForEach(Stream.decodeText(handle.all), (chunk) =>
401-
Effect.sync(() => {
402-
output += chunk
403-
ctx.metadata({
404-
metadata: {
405-
output: preview(output),
406-
description: input.description,
407-
},
408-
})
409-
}),
400+
Stream.runForEach(Stream.decodeText(handle.all), (chunk) => {
401+
output += chunk
402+
return ctx.metadata({
403+
metadata: {
404+
output: preview(output),
405+
description: input.description,
406+
},
407+
})
408+
},
410409
),
411410
)
412411

packages/opencode/src/tool/edit.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ export const EditTool = Tool.define(
158158
if (change.removed) filediff.deletions += change.count || 0
159159
}
160160

161-
ctx.metadata({
161+
yield* ctx.metadata({
162162
metadata: {
163163
diff,
164164
filediff,

packages/opencode/src/tool/task.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ export const TaskTool = Tool.define(
109109
providerID: msg.info.providerID,
110110
}
111111

112-
ctx.metadata({
112+
yield* ctx.metadata({
113113
title: params.description,
114114
metadata: {
115115
sessionId: nextSession.id,

packages/opencode/src/tool/tool.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export namespace Tool {
2222
callID?: string
2323
extra?: { [key: string]: any }
2424
messages: MessageV2.WithParts[]
25-
metadata(input: { title?: string; metadata?: M }): void
25+
metadata(input: { title?: string; metadata?: M }): Effect.Effect<void>
2626
ask(input: Omit<Permission.Request, "id" | "sessionID" | "tool">): Effect.Effect<void>
2727
}
2828

packages/opencode/test/tool/apply_patch.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ const baseCtx = {
2222
agent: "build",
2323
abort: AbortSignal.any([]),
2424
messages: [],
25-
metadata: () => {},
25+
metadata: () => Effect.void,
2626
}
2727

2828
type AskInput = {

packages/opencode/test/tool/bash.test.ts

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ const ctx = {
2929
agent: "build",
3030
abort: AbortSignal.any([]),
3131
messages: [],
32-
metadata: () => {},
32+
metadata: () => Effect.void,
3333
ask: () => Effect.void,
3434
}
3535

@@ -982,13 +982,14 @@ describe("tool.bash abort", () => {
982982
{
983983
...ctx,
984984
abort: controller.signal,
985-
metadata: (input) => {
986-
const output = (input.metadata as { output?: string })?.output
987-
if (output && output.includes("before") && !controller.signal.aborted) {
988-
collected.push(output)
989-
controller.abort()
990-
}
991-
},
985+
metadata: (input) =>
986+
Effect.sync(() => {
987+
const output = (input.metadata as { output?: string })?.output
988+
if (output && output.includes("before") && !controller.signal.aborted) {
989+
collected.push(output)
990+
controller.abort()
991+
}
992+
}),
992993
},
993994
),
994995
)
@@ -1074,10 +1075,11 @@ describe("tool.bash abort", () => {
10741075
},
10751076
{
10761077
...ctx,
1077-
metadata: (input) => {
1078-
const output = (input.metadata as { output?: string })?.output
1079-
if (output) updates.push(output)
1080-
},
1078+
metadata: (input) =>
1079+
Effect.sync(() => {
1080+
const output = (input.metadata as { output?: string })?.output
1081+
if (output) updates.push(output)
1082+
}),
10811083
},
10821084
),
10831085
)

packages/opencode/test/tool/edit.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const ctx = {
2020
agent: "build",
2121
abort: AbortSignal.any([]),
2222
messages: [],
23-
metadata: () => {},
23+
metadata: () => Effect.void,
2424
ask: () => Effect.void,
2525
}
2626

packages/opencode/test/tool/external-directory.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const baseCtx: Omit<Tool.Context, "ask"> = {
1616
agent: "build",
1717
abort: AbortSignal.any([]),
1818
messages: [],
19-
metadata: () => {},
19+
metadata: () => Effect.void,
2020
}
2121

2222
const glob = (p: string) =>

0 commit comments

Comments
 (0)