fix: gate run event traces behind --verbose
This commit is contained in:
parent
991dcdb6c1
commit
5f5b476f12
@ -75,6 +75,7 @@ program
|
|||||||
.option("--attach <url>", "Attach to existing opencode server URL")
|
.option("--attach <url>", "Attach to existing opencode server URL")
|
||||||
.option("--on-complete <command>", "Shell command to run after completion")
|
.option("--on-complete <command>", "Shell command to run after completion")
|
||||||
.option("--json", "Output structured JSON result to stdout")
|
.option("--json", "Output structured JSON result to stdout")
|
||||||
|
.option("--verbose", "Show full event stream (default: messages/tools only)")
|
||||||
.option("--session-id <id>", "Resume existing session instead of creating new one")
|
.option("--session-id <id>", "Resume existing session instead of creating new one")
|
||||||
.addHelpText("after", `
|
.addHelpText("after", `
|
||||||
Examples:
|
Examples:
|
||||||
@ -114,6 +115,7 @@ Unlike 'opencode run', this command waits until:
|
|||||||
attach: options.attach,
|
attach: options.attach,
|
||||||
onComplete: options.onComplete,
|
onComplete: options.onComplete,
|
||||||
json: options.json ?? false,
|
json: options.json ?? false,
|
||||||
|
verbose: options.verbose ?? false,
|
||||||
sessionId: options.sessionId,
|
sessionId: options.sessionId,
|
||||||
}
|
}
|
||||||
const exitCode = await run(runOptions)
|
const exitCode = await run(runOptions)
|
||||||
|
|||||||
@ -24,11 +24,15 @@ export async function processEvents(
|
|||||||
try {
|
try {
|
||||||
const payload = event as EventPayload
|
const payload = event as EventPayload
|
||||||
if (!payload?.type) {
|
if (!payload?.type) {
|
||||||
|
if (ctx.verbose) {
|
||||||
console.error(pc.dim(`[event] no type: ${JSON.stringify(event)}`))
|
console.error(pc.dim(`[event] no type: ${JSON.stringify(event)}`))
|
||||||
|
}
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (ctx.verbose) {
|
||||||
logEventVerbose(ctx, payload)
|
logEventVerbose(ctx, payload)
|
||||||
|
}
|
||||||
|
|
||||||
handleSessionError(ctx, payload, state)
|
handleSessionError(ctx, payload, state)
|
||||||
handleSessionIdle(ctx, payload, state)
|
handleSessionIdle(ctx, payload, state)
|
||||||
|
|||||||
@ -1,4 +1,4 @@
|
|||||||
import { describe, it, expect } from "bun:test"
|
import { describe, it, expect, spyOn } from "bun:test"
|
||||||
import { createEventState, serializeError, type EventState } from "./events"
|
import { createEventState, serializeError, type EventState } from "./events"
|
||||||
import type { RunContext, EventPayload } from "./types"
|
import type { RunContext, EventPayload } from "./types"
|
||||||
|
|
||||||
@ -87,6 +87,52 @@ describe("createEventState", () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
describe("event handling", () => {
|
describe("event handling", () => {
|
||||||
|
it("does not log verbose event traces by default", async () => {
|
||||||
|
// given
|
||||||
|
const ctx = createMockContext("my-session")
|
||||||
|
const state = createEventState()
|
||||||
|
const errorSpy = spyOn(console, "error").mockImplementation(() => {})
|
||||||
|
|
||||||
|
const payload: EventPayload = {
|
||||||
|
type: "custom.event",
|
||||||
|
properties: { sessionID: "my-session" },
|
||||||
|
}
|
||||||
|
|
||||||
|
const events = toAsyncIterable([payload])
|
||||||
|
const { processEvents } = await import("./events")
|
||||||
|
|
||||||
|
// when
|
||||||
|
await processEvents(ctx, events, state)
|
||||||
|
|
||||||
|
// then
|
||||||
|
expect(errorSpy).not.toHaveBeenCalled()
|
||||||
|
errorSpy.mockRestore()
|
||||||
|
})
|
||||||
|
|
||||||
|
it("logs full event traces when verbose is enabled", async () => {
|
||||||
|
// given
|
||||||
|
const ctx = { ...createMockContext("my-session"), verbose: true }
|
||||||
|
const state = createEventState()
|
||||||
|
const errorSpy = spyOn(console, "error").mockImplementation(() => {})
|
||||||
|
|
||||||
|
const payload: EventPayload = {
|
||||||
|
type: "custom.event",
|
||||||
|
properties: { sessionID: "my-session" },
|
||||||
|
}
|
||||||
|
|
||||||
|
const events = toAsyncIterable([payload])
|
||||||
|
const { processEvents } = await import("./events")
|
||||||
|
|
||||||
|
// when
|
||||||
|
await processEvents(ctx, events, state)
|
||||||
|
|
||||||
|
// then
|
||||||
|
expect(errorSpy).toHaveBeenCalledTimes(1)
|
||||||
|
const firstCall = errorSpy.mock.calls[0]
|
||||||
|
expect(String(firstCall?.[0] ?? "")).toContain("custom.event")
|
||||||
|
errorSpy.mockRestore()
|
||||||
|
})
|
||||||
|
|
||||||
it("session.idle sets mainSessionIdle to true for matching session", async () => {
|
it("session.idle sets mainSessionIdle to true for matching session", async () => {
|
||||||
// given
|
// given
|
||||||
const ctx = createMockContext("my-session")
|
const ctx = createMockContext("my-session")
|
||||||
|
|||||||
@ -84,7 +84,13 @@ export async function run(options: RunOptions): Promise<number> {
|
|||||||
|
|
||||||
console.log(pc.dim(`Session: ${sessionID}`))
|
console.log(pc.dim(`Session: ${sessionID}`))
|
||||||
|
|
||||||
const ctx: RunContext = { client, sessionID, directory, abortController }
|
const ctx: RunContext = {
|
||||||
|
client,
|
||||||
|
sessionID,
|
||||||
|
directory,
|
||||||
|
abortController,
|
||||||
|
verbose: options.verbose ?? false,
|
||||||
|
}
|
||||||
const events = await client.event.subscribe({ query: { directory } })
|
const events = await client.event.subscribe({ query: { directory } })
|
||||||
const eventState = createEventState()
|
const eventState = createEventState()
|
||||||
const eventProcessor = processEvents(ctx, events.stream, eventState).catch(
|
const eventProcessor = processEvents(ctx, events.stream, eventState).catch(
|
||||||
|
|||||||
@ -4,6 +4,7 @@ export type { OpencodeClient }
|
|||||||
export interface RunOptions {
|
export interface RunOptions {
|
||||||
message: string
|
message: string
|
||||||
agent?: string
|
agent?: string
|
||||||
|
verbose?: boolean
|
||||||
directory?: string
|
directory?: string
|
||||||
timeout?: number
|
timeout?: number
|
||||||
port?: number
|
port?: number
|
||||||
@ -31,6 +32,7 @@ export interface RunContext {
|
|||||||
sessionID: string
|
sessionID: string
|
||||||
directory: string
|
directory: string
|
||||||
abortController: AbortController
|
abortController: AbortController
|
||||||
|
verbose?: boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface Todo {
|
export interface Todo {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user