-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Open
Description
Which project does this relate to?
Router
Describe the bug
I'm getting the error in the title of this issue when trying to load a page that references this server function. The defineTool function is just a thin function that helps with types.
"use server" cannot be nested in other blocks or functions.
sample code:
const toolDefinitions = [
defineTool({
id: "recompute-policy",
name: "Recompute Policy",
schema: z.object({
policyId: z.string().min(1, "Policy ID is required"),
}),
handler: async (input, context) => {
const result = await db.$transaction(async (tx) => {
return recomputePolicyService({
tx,
organizationId: context.organizationId,
policyId: input.policyId,
});
});
return {
message: `Recomputed balances for policy ${result.policyNum} (${result.invoiceCount} invoices)`,
};
},
}),
] as const;
type ToolDefinition = (typeof toolDefinitions)[number];
function getToolDefinition(toolId: string): ToolDefinition | undefined {
return toolDefinitions.find((t) => t.id === toolId);
}
export const callToolAction = createServerFn({ method: "POST" })
.middleware([actionTracingMiddleware, actionAuth])
.inputValidator(schema)
.handler(async ({ data, context }) => {
const tool = getToolDefinition(data.toolId);
if (!tool) {
throw new Error(`Unknown tool: ${data.toolId}`);
}
const validatedInput = tool.schema.parse(data.input);
logger.info({
msg: "Calling tool",
toolId: data.toolId,
input: validatedInput,
userId: context.user.id,
organizationId: context.activeOrganizationId,
});
const result = await tool.handler(validatedInput, {
organizationId: context.activeOrganizationId,
userId: context.user.id,
});
return {
success: true as const,
message: result.message,
toolId: data.toolId,
};
});
Your Example Website or App
na
Steps to Reproduce the Bug or Issue
see description
Expected behavior
I expect it to work, or better understand why it doesn't work.
Screenshots or Videos
No response
Platform
- Router / Start Version: [e.g. 1.121.0]
- OS: [e.g. macOS, Windows, Linux]
- Browser: [e.g. Chrome, Safari, Firefox]
- Browser Version: [e.g. 91.1]
- Bundler: [e.g. vite]
- Bundler Version: [e.g. 7.0.0]
Additional context
No response
Metadata
Metadata
Assignees
Labels
No labels