feat: allow hook model overrides
This commit is contained in:
@@ -38,6 +38,30 @@ describe("hooks mapping", () => {
|
||||
}
|
||||
});
|
||||
|
||||
it("passes model override from mapping", async () => {
|
||||
const mappings = resolveHookMappings({
|
||||
mappings: [
|
||||
{
|
||||
id: "demo",
|
||||
match: { path: "gmail" },
|
||||
action: "agent",
|
||||
messageTemplate: "Subject: {{messages[0].subject}}",
|
||||
model: "openai/gpt-4.1-mini",
|
||||
},
|
||||
],
|
||||
});
|
||||
const result = await applyHookMappings(mappings, {
|
||||
payload: { messages: [{ subject: "Hello" }] },
|
||||
headers: {},
|
||||
url: baseUrl,
|
||||
path: "gmail",
|
||||
});
|
||||
expect(result?.ok).toBe(true);
|
||||
if (result?.ok && result.action.kind === "agent") {
|
||||
expect(result.action.model).toBe("openai/gpt-4.1-mini");
|
||||
}
|
||||
});
|
||||
|
||||
it("runs transform module", async () => {
|
||||
const dir = fs.mkdtempSync(path.join(os.tmpdir(), "clawdbot-hooks-"));
|
||||
const modPath = path.join(dir, "transform.mjs");
|
||||
|
||||
@@ -27,6 +27,7 @@ export type HookMappingResolved = {
|
||||
| "signal"
|
||||
| "imessage";
|
||||
to?: string;
|
||||
model?: string;
|
||||
thinking?: string;
|
||||
timeoutSeconds?: number;
|
||||
transform?: HookMappingTransformResolved;
|
||||
@@ -66,6 +67,7 @@ export type HookAction =
|
||||
| "signal"
|
||||
| "imessage";
|
||||
to?: string;
|
||||
model?: string;
|
||||
thinking?: string;
|
||||
timeoutSeconds?: number;
|
||||
};
|
||||
@@ -110,6 +112,7 @@ type HookTransformResult = Partial<{
|
||||
| "signal"
|
||||
| "imessage";
|
||||
to: string;
|
||||
model: string;
|
||||
thinking: string;
|
||||
timeoutSeconds: number;
|
||||
}> | null;
|
||||
@@ -198,6 +201,7 @@ function normalizeHookMapping(
|
||||
deliver: mapping.deliver,
|
||||
provider: mapping.provider,
|
||||
to: mapping.to,
|
||||
model: mapping.model,
|
||||
thinking: mapping.thinking,
|
||||
timeoutSeconds: mapping.timeoutSeconds,
|
||||
transform,
|
||||
@@ -243,6 +247,7 @@ function buildActionFromMapping(
|
||||
deliver: mapping.deliver,
|
||||
provider: mapping.provider,
|
||||
to: renderOptional(mapping.to, ctx),
|
||||
model: renderOptional(mapping.model, ctx),
|
||||
thinking: renderOptional(mapping.thinking, ctx),
|
||||
timeoutSeconds: mapping.timeoutSeconds,
|
||||
},
|
||||
@@ -293,6 +298,7 @@ function mergeAction(
|
||||
: baseAgent?.deliver,
|
||||
provider: override.provider ?? baseAgent?.provider,
|
||||
to: override.to ?? baseAgent?.to,
|
||||
model: override.model ?? baseAgent?.model,
|
||||
thinking: override.thinking ?? baseAgent?.thinking,
|
||||
timeoutSeconds: override.timeoutSeconds ?? baseAgent?.timeoutSeconds,
|
||||
});
|
||||
|
||||
@@ -147,6 +147,7 @@ export type HookAgentPayload = {
|
||||
| "signal"
|
||||
| "imessage";
|
||||
to?: string;
|
||||
model?: string;
|
||||
thinking?: string;
|
||||
timeoutSeconds?: number;
|
||||
};
|
||||
@@ -201,6 +202,14 @@ export function normalizeAgentPayload(
|
||||
const toRaw = payload.to;
|
||||
const to =
|
||||
typeof toRaw === "string" && toRaw.trim() ? toRaw.trim() : undefined;
|
||||
const modelRaw = payload.model;
|
||||
const model =
|
||||
typeof modelRaw === "string" && modelRaw.trim()
|
||||
? modelRaw.trim()
|
||||
: undefined;
|
||||
if (modelRaw !== undefined && !model) {
|
||||
return { ok: false, error: "model required" };
|
||||
}
|
||||
const deliver = payload.deliver === true;
|
||||
const thinkingRaw = payload.thinking;
|
||||
const thinking =
|
||||
@@ -224,6 +233,7 @@ export function normalizeAgentPayload(
|
||||
deliver,
|
||||
provider,
|
||||
to,
|
||||
model,
|
||||
thinking,
|
||||
timeoutSeconds,
|
||||
},
|
||||
|
||||
@@ -664,6 +664,7 @@ export const CronPayloadSchema = Type.Union([
|
||||
{
|
||||
kind: Type.Literal("agentTurn"),
|
||||
message: NonEmptyString,
|
||||
model: Type.Optional(Type.String()),
|
||||
thinking: Type.Optional(Type.String()),
|
||||
timeoutSeconds: Type.Optional(Type.Integer({ minimum: 1 })),
|
||||
deliver: Type.Optional(Type.Boolean()),
|
||||
|
||||
@@ -41,6 +41,7 @@ type HookDispatchers = {
|
||||
| "signal"
|
||||
| "imessage";
|
||||
to?: string;
|
||||
model?: string;
|
||||
thinking?: string;
|
||||
timeoutSeconds?: number;
|
||||
}) => string;
|
||||
@@ -177,6 +178,7 @@ export function createHooksRequestHandler(
|
||||
deliver: mapped.action.deliver === true,
|
||||
provider: mapped.action.provider ?? "last",
|
||||
to: mapped.action.to,
|
||||
model: mapped.action.model,
|
||||
thinking: mapped.action.thinking,
|
||||
timeoutSeconds: mapped.action.timeoutSeconds,
|
||||
});
|
||||
|
||||
@@ -67,6 +67,37 @@ describe("gateway server hooks", () => {
|
||||
await server.close();
|
||||
});
|
||||
|
||||
test("hooks agent forwards model override", async () => {
|
||||
testState.hooksConfig = { enabled: true, token: "hook-secret" };
|
||||
cronIsolatedRun.mockClear();
|
||||
cronIsolatedRun.mockResolvedValueOnce({
|
||||
status: "ok",
|
||||
summary: "done",
|
||||
});
|
||||
const port = await getFreePort();
|
||||
const server = await startGatewayServer(port);
|
||||
const res = await fetch(`http://127.0.0.1:${port}/hooks/agent`, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
Authorization: "Bearer hook-secret",
|
||||
},
|
||||
body: JSON.stringify({
|
||||
message: "Do it",
|
||||
name: "Email",
|
||||
model: "openai/gpt-4.1-mini",
|
||||
}),
|
||||
});
|
||||
expect(res.status).toBe(202);
|
||||
await waitForSystemEvent();
|
||||
const call = cronIsolatedRun.mock.calls[0]?.[0] as {
|
||||
job?: { payload?: { model?: string } };
|
||||
};
|
||||
expect(call?.job?.payload?.model).toBe("openai/gpt-4.1-mini");
|
||||
drainSystemEvents();
|
||||
await server.close();
|
||||
});
|
||||
|
||||
test("hooks wake accepts query token", async () => {
|
||||
testState.hooksConfig = { enabled: true, token: "hook-secret" };
|
||||
const port = await getFreePort();
|
||||
|
||||
@@ -502,6 +502,7 @@ export async function startGatewayServer(
|
||||
| "signal"
|
||||
| "imessage";
|
||||
to?: string;
|
||||
model?: string;
|
||||
thinking?: string;
|
||||
timeoutSeconds?: number;
|
||||
}) => {
|
||||
@@ -522,6 +523,7 @@ export async function startGatewayServer(
|
||||
payload: {
|
||||
kind: "agentTurn",
|
||||
message: value.message,
|
||||
model: value.model,
|
||||
thinking: value.thinking,
|
||||
timeoutSeconds: value.timeoutSeconds,
|
||||
deliver: value.deliver,
|
||||
|
||||
Reference in New Issue
Block a user