From 13d2f3b4e03d7d575a990256958a8469eac5afb7 Mon Sep 17 00:00:00 2001 From: Lenz Weber-Tronic Date: Mon, 8 Dec 2025 10:55:38 +0100 Subject: [PATCH] Also allow tool name/description via Operation Description --- src/vite/application_manifest_plugin.ts | 46 +++++++++++++++++-------- 1 file changed, 31 insertions(+), 15 deletions(-) diff --git a/src/vite/application_manifest_plugin.ts b/src/vite/application_manifest_plugin.ts index e976537..d98fa36 100644 --- a/src/vite/application_manifest_plugin.ts +++ b/src/vite/application_manifest_plugin.ts @@ -57,32 +57,48 @@ export const ApplicationManifestPlugin = () => { link: new ApolloLink((operation) => { const body = print(removeClientDirective(sortTopLevelDefinitions(operation.query))); const name = operation.operationName; - const variables = ( - operation.query.definitions.find((d) => d.kind === "OperationDefinition") as OperationDefinitionNode - ).variableDefinitions?.reduce( + const operationDefinition = operation.query.definitions.find( + (d): d is OperationDefinitionNode => d.kind === "OperationDefinition" + ); + + const variables = operationDefinition?.variableDefinitions?.reduce( (obj, varDef) => ({ ...obj, [varDef.variable.name.value]: getTypeName(varDef.type) }), {} ); - const type = ( - operation.query.definitions.find((d) => d.kind === "OperationDefinition") as OperationDefinitionNode - ).operation; - const prefetch = ( - operation.query.definitions.find((d) => d.kind === "OperationDefinition") as OperationDefinitionNode - ).directives?.some((d) => d.name.value === "prefetch"); + const type = operationDefinition?.operation; + const prefetch = operationDefinition?.directives?.some((d) => d.name.value === "prefetch"); const id = createHash("sha256").update(body).digest("hex"); // TODO: For now, you can only have 1 operation marked as prefetch. In the future, we'll likely support more than 1, and the "prefetchId" will be defined on the `@prefetch` itself as an argument const prefetchID = prefetch ? "__anonymous" : undefined; - const tools = ( - operation.query.definitions.find((d) => d.kind === "OperationDefinition") as OperationDefinitionNode - ).directives + const description = operationDefinition?.description?.value; + let operationToolName: string | undefined = undefined; + let operationToolDescription: string | undefined = undefined; + if (description) { + const idx = description.indexOf("\n"); + if (idx === -1) { + operationToolName = description; + } else { + operationToolName = description.substring(0, idx); + operationToolDescription = description.substring(idx + 1).trim(); + } + } + + const tools = operationDefinition.directives ?.filter((d) => d.name.value === "tool") .map((directive) => { const directiveArguments: Record = - directive.arguments?.reduce((obj, arg) => ({ ...obj, [arg.name.value]: getRawValue(arg.value) }), {}) ?? {}; + directive.arguments?.reduce( + (obj, arg) => ({ + ...obj, + [arg.name.value]: getRawValue(arg.value), + }), + {} + ) ?? {}; + return { - name: directiveArguments["name"], - description: directiveArguments["description"], + name: directiveArguments["name"] || operationToolName, + description: directiveArguments["description"] || operationToolDescription, extraInputs: directiveArguments["extraInputs"], }; });