diff --git a/.changeset/integration-tool-context.md b/.changeset/integration-tool-context.md new file mode 100644 index 000000000..988365bcf --- /dev/null +++ b/.changeset/integration-tool-context.md @@ -0,0 +1,7 @@ +--- +"@gitbook/browser-types": patch +"@gitbook/embed": patch +"gitbook": patch +--- + +Add an optional `context` property (string, up to 512 characters) to the `confirmation` of custom AI tools, shown above the confirmation dialog to help the user understand what they are approving or rejecting. The `confirmation` can now also be a function that receives the AI-provided input and returns the confirmation, so the context can be derived dynamically from the arguments the tool is about to run with. Available both to integrations (`GitBookIntegrationTool`) and to embed consumers (`GitBookToolDefinition`). diff --git a/packages/browser-types/src/index.ts b/packages/browser-types/src/index.ts index 93a213a25..35e0c8b80 100644 --- a/packages/browser-types/src/index.ts +++ b/packages/browser-types/src/index.ts @@ -5,14 +5,28 @@ export type GitBookIntegrationEvent = 'load' | 'unload'; export type GitBookIntegrationEventCallback = (...args: any[]) => void; +export type GitBookIntegrationToolConfirmation = { + icon?: IconName; + label: string; + + /** + * Supporting context displayed to the user above the confirmation dialog, + * to help them understand what they are approving or rejecting. + * Limited to 512 characters. + */ + context?: string; +}; + export type GitBookIntegrationTool = AIToolDefinition & { /** * Confirmation action to be displayed to the user before executing the tool. + * Provide a static object, or a function that receives the input provided by + * the AI assistant and returns the confirmation — useful to display dynamic + * context based on the arguments the tool is about to be executed with. */ - confirmation?: { - icon?: IconName; - label: string; - }; + confirmation?: + | GitBookIntegrationToolConfirmation + | ((input: object) => GitBookIntegrationToolConfirmation); /** * Callback when the tool is executed. diff --git a/packages/embed/src/client/protocol.ts b/packages/embed/src/client/protocol.ts index 647ad4bb0..5deb02524 100644 --- a/packages/embed/src/client/protocol.ts +++ b/packages/embed/src/client/protocol.ts @@ -1,17 +1,32 @@ import type { AIToolCallResult, AIToolDefinition } from '@gitbook/api'; import type { IconName } from '@gitbook/icons'; +/** + * Confirmation action to be displayed to the user before executing a tool. + */ +export type GitBookToolConfirmation = { + icon?: IconName; + label: string; + + /** + * Supporting context displayed to the user above the confirmation dialog, + * to help them understand what they are approving or rejecting. + * Limited to 512 characters. + */ + context?: string; +}; + /** * Custom tool definition to be passed to the AI assistant. */ export type GitBookToolDefinition = AIToolDefinition & { /** * Confirmation action to be displayed to the user before executing the tool. + * Provide a static object, or a function that receives the input provided by + * the AI assistant and returns the confirmation — useful to display dynamic + * context based on the arguments the tool is about to be executed with. */ - confirmation?: { - icon?: IconName; - label: string; - }; + confirmation?: GitBookToolConfirmation | ((input: object) => GitBookToolConfirmation); /** * Callback when the tool is executed. diff --git a/packages/gitbook/src/components/AI/controls/ConfirmControl.tsx b/packages/gitbook/src/components/AI/controls/ConfirmControl.tsx index ff8a0afa2..ce7fc66e7 100644 --- a/packages/gitbook/src/components/AI/controls/ConfirmControl.tsx +++ b/packages/gitbook/src/components/AI/controls/ConfirmControl.tsx @@ -16,6 +16,13 @@ export const ConfirmControlDef = createAIControl({ description: 'Display a confirmation prompt to the user (Confirm / Cancel) to approve or abort a pending action. Use this when an operation is irreversible, sensitive, or should only proceed with explicit user consent. Returns either a `confirmed` or `cancelled` result based on the user’s click.', inputSchema: z.object({ + context: z + .string() + .max(512) + .optional() + .describe( + 'Supporting context shown above the prompt to help the user understand what they are approving or rejecting.' + ), icon: z .string() .optional() @@ -29,10 +36,13 @@ export const ConfirmControlDef = createAIControl({ }); function ConfirmControl(props: GetAIControlProps) { - const { label, icon, onSubmit } = props; + const { label, icon, context, onSubmit } = props; const language = useLanguage(); return ( + {context ? ( +

{context}

+ ) : null}