MCPcopy Create free account
hub / github.com/Openpanel-dev/openpanel / chatTool

Function chatTool

apps/api/src/agents/tools/helpers.ts:39–82  ·  view source on GitHub ↗
(
  config: {
    name: string;
    description: string;
    schema: z.ZodTypeAny;
  },
  handler: (
    // biome-ignore lint/suspicious/noExplicitAny: deliberate, see comment above
    input: any,
    ctx: ChatAgentContext,
    runCtx: ToolRunContext,
  ) => Promise<unknown>,
)

Source from the content-addressed store, hash-verified

37 * decide whether to retry with narrower params or move on.
38 */
39export function chatTool(
40 config: {
41 name: string;
42 description: string;
43 schema: z.ZodTypeAny;
44 },
45 handler: (
46 // biome-ignore lint/suspicious/noExplicitAny: deliberate, see comment above
47 input: any,
48 ctx: ChatAgentContext,
49 runCtx: ToolRunContext,
50 ) => Promise<unknown>,
51): AgentToolDefinition {
52 // biome-ignore lint/suspicious/noExplicitAny: see block comment above
53 const contract: any = defineTool({
54 name: config.name,
55 description: config.description,
56 schema: config.schema,
57 });
58 return contract.server(
59 async (input: unknown, runCtx: ToolRunContext) => {
60 const work = handler(
61 input,
62 runCtx.context as ChatAgentContext,
63 runCtx,
64 );
65 let timer: NodeJS.Timeout | undefined;
66 const timeout = new Promise<never>((_, reject) => {
67 timer = setTimeout(() => {
68 reject(
69 new Error(
70 `Tool "${config.name}" timed out after ${TOOL_TIMEOUT_MS / 1000}s`,
71 ),
72 );
73 }, TOOL_TIMEOUT_MS);
74 });
75 try {
76 return await Promise.race([work, timeout]);
77 } finally {
78 if (timer) clearTimeout(timer);
79 }
80 },
81 ) as AgentToolDefinition;
82}
83
84/**
85 * Cap an array result to `max` items and append a truncation marker so the

Callers 11

base.tsFile · 0.90
seo.tsFile · 0.90
groups.tsFile · 0.90
report.tsFile · 0.90
events.tsFile · 0.90
dashboard.tsFile · 0.90
pages.tsFile · 0.90
references.tsFile · 0.90
session.tsFile · 0.90
insights.tsFile · 0.90
profile.tsFile · 0.90

Calls 1

handlerFunction · 0.50

Tested by

no test coverage detected