Skip to content

Commit 8a4087b

Browse files
committed
feat: add ctx.log() for custom tools and plugins
- adds log() method to ToolContext in both plugin and internal Tool.Context - implements log() using Log.create() in tool execution contexts - updates plugin docs to show ctx.log() for custom tools - removes console.log example that leaks to stdout - fixes anomalyco#6830 anomalyco#6833
1 parent 5f10243 commit 8a4087b

8 files changed

Lines changed: 56 additions & 2 deletions

File tree

‎packages/opencode/src/session/prompt.ts‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,10 @@ export namespace SessionPrompt {
400400
ruleset: PermissionNext.merge(taskAgent.permission, session.permission ?? []),
401401
})
402402
},
403+
log(level, message, extra) {
404+
const logger = Log.create({ service: "tool" })
405+
logger[level](message, extra)
406+
},
403407
}
404408
const result = await taskTool.execute(taskArgs, taskCtx).catch((error) => {
405409
executionError = error
@@ -673,6 +677,10 @@ export namespace SessionPrompt {
673677
ruleset: PermissionNext.merge(input.agent.permission, input.session.permission ?? []),
674678
})
675679
},
680+
log(level, message, extra) {
681+
const logger = Log.create({ service: "tool" })
682+
logger[level](message, extra)
683+
},
676684
})
677685

678686
for (const item of await ToolRegistry.tools(input.model.providerID)) {
@@ -983,6 +991,10 @@ export namespace SessionPrompt {
983991
extra: { bypassCwdCheck: true, model },
984992
metadata: async () => {},
985993
ask: async () => {},
994+
log(level, message, extra) {
995+
const logger = Log.create({ service: "tool" })
996+
logger[level](message, extra)
997+
},
986998
}
987999
const result = await t.execute(args, readCtx)
9881000
pieces.push({
@@ -1044,6 +1056,10 @@ export namespace SessionPrompt {
10441056
extra: { bypassCwdCheck: true },
10451057
metadata: async () => {},
10461058
ask: async () => {},
1059+
log(level, message, extra) {
1060+
const logger = Log.create({ service: "tool" })
1061+
logger[level](message, extra)
1062+
},
10471063
}
10481064
const result = await ListTool.init().then((t) => t.execute(args, listCtx))
10491065
return [

‎packages/opencode/src/tool/tool.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export namespace Tool {
2121
extra?: { [key: string]: any }
2222
metadata(input: { title?: string; metadata?: M }): void
2323
ask(input: Omit<PermissionNext.Request, "id" | "sessionID" | "tool">): Promise<void>
24+
log(level: "debug" | "info" | "warn" | "error", message: string, extra?: Record<string, unknown>): void
2425
}
2526
export interface Info<Parameters extends z.ZodType = z.ZodType, M extends Metadata = Metadata> {
2627
id: string

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const ctx = {
1313
abort: AbortSignal.any([]),
1414
metadata: () => {},
1515
ask: async () => {},
16+
log: () => {},
1617
}
1718

1819
const projectRoot = path.join(__dirname, "../..")

‎packages/opencode/test/tool/grep.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const ctx = {
1212
abort: AbortSignal.any([]),
1313
metadata: () => {},
1414
ask: async () => {},
15+
log: () => {},
1516
}
1617

1718
const projectRoot = path.join(__dirname, "../..")

‎packages/opencode/test/tool/patch.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const ctx = {
1414
abort: AbortSignal.any([]),
1515
metadata: () => {},
1616
ask: async () => {},
17+
log: () => {},
1718
}
1819

1920
const patchTool = await PatchTool.init()

‎packages/opencode/test/tool/read.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const ctx = {
1414
abort: AbortSignal.any([]),
1515
metadata: () => {},
1616
ask: async () => {},
17+
log: () => {},
1718
}
1819

1920
describe("tool.read external_directory permission", () => {

‎packages/plugin/src/tool.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export type ToolContext = {
55
messageID: string
66
agent: string
77
abort: AbortSignal
8+
log(level: "debug" | "info" | "warn" | "error", message: string, extra?: Record<string, unknown>): void
89
}
910

1011
export function tool<Args extends z.ZodRawShape>(input: {

‎packages/web/src/content/docs/plugins.mdx‎

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,12 @@ export const MyPlugin = async (ctx) => {
105105

106106
```js title=".opencode/plugin/example.js"
107107
export const MyPlugin = async ({ project, client, $, directory, worktree }) => {
108-
console.log("Plugin initialized!")
108+
// Use client.app.log() instead of console.log to avoid leaking output
109+
await client.app.log({
110+
service: "my-plugin",
111+
level: "info",
112+
message: "Plugin initialized",
113+
})
109114

110115
return {
111116
// Hook implementations go here
@@ -290,7 +295,11 @@ Your custom tools will be available to opencode alongside built-in tools.
290295

291296
### Logging
292297

293-
Use `client.app.log()` instead of `console.log` for structured logging:
298+
Use structured logging instead of `console.log` to avoid output leaking to stdout.
299+
300+
#### In plugins
301+
302+
Use `client.app.log()` for logging in plugin initialization and hooks:
294303

295304
```ts title=".opencode/plugin/my-plugin.ts"
296305
export const MyPlugin = async ({ client }) => {
@@ -303,6 +312,29 @@ export const MyPlugin = async ({ client }) => {
303312
}
304313
```
305314

315+
#### In custom tools
316+
317+
Use `ctx.log()` for logging in custom tool implementations:
318+
319+
```ts title=".opencode/plugin/custom-tool.ts"
320+
import { type Plugin, tool } from "@opencode-ai/plugin"
321+
322+
export const CustomToolPlugin: Plugin = async (ctx) => {
323+
return {
324+
tool: {
325+
mytool: tool({
326+
description: "A tool that logs",
327+
args: { name: tool.schema.string() },
328+
async execute(args, ctx) {
329+
ctx.log("info", "Tool executed", { name: args.name })
330+
return `Hello ${args.name}!`
331+
},
332+
}),
333+
},
334+
}
335+
}
336+
```
337+
306338
Levels: `debug`, `info`, `warn`, `error`. See [SDK documentation](https://opencode.ai/docs/sdk) for details.
307339

308340
---

0 commit comments

Comments
 (0)