refactor(hooks): remove unused openclaw hook
Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent) Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
This commit is contained in:
@@ -1,86 +0,0 @@
|
|||||||
import { afterAll, beforeEach, describe, expect, mock, test } from "bun:test"
|
|
||||||
|
|
||||||
const wakeOpenClawMock = mock(async () => null)
|
|
||||||
|
|
||||||
mock.module("../openclaw", () => ({
|
|
||||||
wakeOpenClaw: wakeOpenClawMock,
|
|
||||||
}))
|
|
||||||
|
|
||||||
afterAll(() => {
|
|
||||||
mock.restore()
|
|
||||||
})
|
|
||||||
|
|
||||||
describe("createOpenClawHook", () => {
|
|
||||||
beforeEach(() => {
|
|
||||||
wakeOpenClawMock.mockClear()
|
|
||||||
})
|
|
||||||
|
|
||||||
test("maps session.created to session-start", async () => {
|
|
||||||
const { createOpenClawHook } = await import("./openclaw")
|
|
||||||
const hook = createOpenClawHook(
|
|
||||||
{ directory: "/tmp/project" } as any,
|
|
||||||
{ openclaw: { enabled: true } } as any,
|
|
||||||
)
|
|
||||||
|
|
||||||
await hook?.event?.({
|
|
||||||
event: {
|
|
||||||
type: "session.created",
|
|
||||||
properties: { sessionID: "session-1" },
|
|
||||||
},
|
|
||||||
})
|
|
||||||
|
|
||||||
expect(wakeOpenClawMock).toHaveBeenCalledWith(
|
|
||||||
expect.anything(),
|
|
||||||
"session-start",
|
|
||||||
expect.objectContaining({
|
|
||||||
projectPath: "/tmp/project",
|
|
||||||
sessionId: "session-1",
|
|
||||||
}),
|
|
||||||
)
|
|
||||||
})
|
|
||||||
|
|
||||||
test("uses tool.execute.before for question tools", async () => {
|
|
||||||
const { createOpenClawHook } = await import("./openclaw")
|
|
||||||
const hook = createOpenClawHook(
|
|
||||||
{ directory: "/tmp/project" } as any,
|
|
||||||
{ openclaw: { enabled: true } } as any,
|
|
||||||
)
|
|
||||||
|
|
||||||
await hook?.["tool.execute.before"]?.(
|
|
||||||
{ tool: "ask_user_question", sessionID: "session-2" },
|
|
||||||
{ args: { questions: [{ question: "Need approval?", options: [{ label: "Yes" }] }] } },
|
|
||||||
)
|
|
||||||
|
|
||||||
expect(wakeOpenClawMock).toHaveBeenCalledWith(
|
|
||||||
expect.anything(),
|
|
||||||
"ask-user-question",
|
|
||||||
expect.objectContaining({
|
|
||||||
projectPath: "/tmp/project",
|
|
||||||
question: "Need approval?",
|
|
||||||
sessionId: "session-2",
|
|
||||||
}),
|
|
||||||
)
|
|
||||||
})
|
|
||||||
|
|
||||||
test("falls back to args.question string when questions array absent", async () => {
|
|
||||||
const { createOpenClawHook } = await import("./openclaw")
|
|
||||||
const hook = createOpenClawHook(
|
|
||||||
{ directory: "/tmp/project" } as any,
|
|
||||||
{ openclaw: { enabled: true } } as any,
|
|
||||||
)
|
|
||||||
|
|
||||||
await hook?.["tool.execute.before"]?.(
|
|
||||||
{ tool: "question", sessionID: "session-3" },
|
|
||||||
{ args: { question: "Fallback?" } },
|
|
||||||
)
|
|
||||||
|
|
||||||
expect(wakeOpenClawMock).toHaveBeenCalledWith(
|
|
||||||
expect.anything(),
|
|
||||||
"ask-user-question",
|
|
||||||
expect.objectContaining({
|
|
||||||
question: "Fallback?",
|
|
||||||
sessionId: "session-3",
|
|
||||||
}),
|
|
||||||
)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
@@ -1,66 +0,0 @@
|
|||||||
import type { PluginContext } from "../plugin/types"
|
|
||||||
import type { OhMyOpenCodeConfig } from "../config"
|
|
||||||
import { wakeOpenClaw } from "../openclaw"
|
|
||||||
import type { OpenClawContext } from "../openclaw/types"
|
|
||||||
|
|
||||||
export function createOpenClawHook(
|
|
||||||
ctx: PluginContext,
|
|
||||||
pluginConfig: OhMyOpenCodeConfig,
|
|
||||||
) {
|
|
||||||
const config = pluginConfig.openclaw
|
|
||||||
if (!config?.enabled) return null
|
|
||||||
|
|
||||||
const handleWake = async (event: string, context: OpenClawContext) => {
|
|
||||||
await wakeOpenClaw(config, event, context)
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
|
||||||
event: async (input: any) => {
|
|
||||||
const { event } = input
|
|
||||||
const props = event.properties || {}
|
|
||||||
const sessionID = props.sessionID || props.info?.id
|
|
||||||
|
|
||||||
const context: OpenClawContext = {
|
|
||||||
sessionId: sessionID,
|
|
||||||
projectPath: ctx.directory,
|
|
||||||
}
|
|
||||||
|
|
||||||
if (event.type === "session.created") {
|
|
||||||
await handleWake("session-start", context)
|
|
||||||
} else if (event.type === "session.deleted") {
|
|
||||||
await handleWake("session-end", context)
|
|
||||||
} else if (event.type === "session.idle") {
|
|
||||||
// Check if we are waiting for user input (ask-user-question)
|
|
||||||
// This is heuristic. If the last message was from assistant and ended with a question?
|
|
||||||
// Or if the system is idle.
|
|
||||||
await handleWake("session-idle", context)
|
|
||||||
}
|
|
||||||
},
|
|
||||||
|
|
||||||
"tool.execute.before": async (
|
|
||||||
input: { tool: string; sessionID: string },
|
|
||||||
output: { args: Record<string, unknown> },
|
|
||||||
) => {
|
|
||||||
const normalizedToolName = input.tool.toLowerCase()
|
|
||||||
if (
|
|
||||||
normalizedToolName !== "question"
|
|
||||||
&& normalizedToolName !== "ask_user_question"
|
|
||||||
&& normalizedToolName !== "askuserquestion"
|
|
||||||
) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// question tool uses args.questions array, not args.question
|
|
||||||
const questions = Array.isArray(output.args.questions) ? output.args.questions : []
|
|
||||||
const question = questions.length > 0 && typeof questions[0]?.question === "string"
|
|
||||||
? questions[0].question
|
|
||||||
: typeof output.args.question === "string" ? output.args.question : undefined
|
|
||||||
const context: OpenClawContext = {
|
|
||||||
sessionId: input.sessionID,
|
|
||||||
projectPath: ctx.directory,
|
|
||||||
question,
|
|
||||||
}
|
|
||||||
await handleWake("ask-user-question", context)
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user