Skip to content

Commit cbc71a5

Browse files
authored
API to let integrations define custom tools for Docs Assistant (#3568)
1 parent 1420180 commit cbc71a5

File tree

18 files changed

+468
-119
lines changed

18 files changed

+468
-119
lines changed

.changeset/eleven-melons-smash.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@gitbook/browser-types": minor
3+
---
4+
5+
First version of the public package for typing script integrations.

.changeset/modern-laws-kick.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"gitbook": minor
3+
---
4+
5+
Allow integrations to provide tools to the Docs Assistant

bun.lock

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,17 @@
1010
"vercel": "^39.3.0",
1111
},
1212
},
13+
"packages/browser-types": {
14+
"name": "@gitbook/browser-types",
15+
"version": "0.3.1",
16+
"dependencies": {
17+
"@gitbook/api": "catalog:",
18+
"@gitbook/icons": "workspace:",
19+
},
20+
"devDependencies": {
21+
"typescript": "^5.5.3",
22+
},
23+
},
1324
"packages/cache-tags": {
1425
"name": "@gitbook/cache-tags",
1526
"version": "0.3.1",
@@ -51,6 +62,7 @@
5162
"version": "0.15.0",
5263
"dependencies": {
5364
"@gitbook/api": "catalog:",
65+
"@gitbook/browser-types": "workspace:*",
5466
"@gitbook/cache-tags": "workspace:*",
5567
"@gitbook/colors": "workspace:*",
5668
"@gitbook/emoji-codepoints": "workspace:*",
@@ -246,7 +258,7 @@
246258
"react-dom": "^19.0.0",
247259
},
248260
"catalog": {
249-
"@gitbook/api": "^0.134.0",
261+
"@gitbook/api": "^0.136.0",
250262
},
251263
"packages": {
252264
"@ai-sdk/provider": ["@ai-sdk/[email protected]", "", { "dependencies": { "json-schema": "^0.4.0" } }, "sha512-0M+qjp+clUD0R1E5eWQFhxEvWLNaOtGQRUaBn8CUABnSKredagq92hUS9VjOzGsTm37xLfpaxl97AVtbeOsHew=="],
@@ -611,7 +623,9 @@
611623

612624
"@fortawesome/fontawesome-svg-core": ["@fortawesome/[email protected]", "", { "dependencies": { "@fortawesome/fontawesome-common-types": "6.6.0" } }, "sha512-KHwPkCk6oRT4HADE7smhfsKudt9N/9lm6EJ5BVg0tD1yPA5hht837fB87F8pn15D8JfTqQOjhKTktwmLMiD7Kg=="],
613625

614-
"@gitbook/api": ["@gitbook/[email protected]", "", { "dependencies": { "event-iterator": "^2.0.0", "eventsource-parser": "^3.0.0" } }, "sha512-WMdLsA0ZOTbOyoloevPs0qa/VR2xmfp+YB6T/a2o8fkFUv5fMXxDVfCAcIxB2q9NCmkriMSCohWKmxLfz44s6w=="],
626+
"@gitbook/api": ["@gitbook/[email protected]", "", { "dependencies": { "event-iterator": "^2.0.0", "eventsource-parser": "^3.0.0" } }, "sha512-IxNqmXE6yUEUq0IzbenN8S/PcMfgxr4+akY8xh6V5ShI/+U37ixujJHZp2zJq6NdZOdBQoR3UQmhPnvtWrkF7g=="],
627+
628+
"@gitbook/browser-types": ["@gitbook/browser-types@workspace:packages/browser-types"],
615629

616630
"@gitbook/cache-tags": ["@gitbook/cache-tags@workspace:packages/cache-tags"],
617631

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
"workspaces": {
3535
"packages": ["packages/*"],
3636
"catalog": {
37-
"@gitbook/api": "^0.134.0"
37+
"@gitbook/api": "^0.136.0"
3838
}
3939
},
4040
"patchedDependencies": {

packages/browser-types/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
dist/

packages/browser-types/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# `@gitbook/browser-types`
2+
3+
Typescript types for the global variables available in a GitBook website. These types can be used by integrations embedding scripts.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"name": "@gitbook/browser-types",
3+
"description": "Typescript types for the global variables available in a GitBook website. These types can be used by integrations embedding scripts.",
4+
"type": "module",
5+
"exports": {
6+
".": {
7+
"types": "./dist/index.d.ts",
8+
"default": "./dist/index.js"
9+
}
10+
},
11+
"version": "0.0.0",
12+
"dependencies": {
13+
"@gitbook/api": "catalog:",
14+
"@gitbook/icons": "workspace:"
15+
},
16+
"devDependencies": {
17+
"typescript": "^5.5.3"
18+
},
19+
"scripts": {
20+
"build": "tsc",
21+
"typecheck": "tsc --noEmit"
22+
},
23+
"files": ["dist", "README.md", "CHANGELOG.md"]
24+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import type { AIToolCallResult, AIToolDefinition } from '@gitbook/api';
2+
import type { IconName } from '@gitbook/icons';
3+
4+
export type GitBookIntegrationEvent = 'load' | 'unload';
5+
6+
export type GitBookIntegrationEventCallback = (...args: any[]) => void;
7+
8+
export type GitBookIntegrationTool = AIToolDefinition & {
9+
/**
10+
* Confirmation action to be displayed to the user before executing the tool.
11+
*/
12+
confirmation?: {
13+
icon?: IconName;
14+
label: string;
15+
};
16+
17+
/**
18+
* Callback when the tool is executed.
19+
* The input is provided by the AI assistant following the input schema of the tool.
20+
*/
21+
execute: (input: object) => Promise<Pick<AIToolCallResult, 'output' | 'summary'>>;
22+
};
23+
24+
export type GitBookGlobal = {
25+
/**
26+
* Register an event listener.
27+
*/
28+
addEventListener: (
29+
type: GitBookIntegrationEvent,
30+
func: GitBookIntegrationEventCallback
31+
) => void;
32+
33+
/**
34+
* Remove an event listener.
35+
*/
36+
removeEventListener: (
37+
type: GitBookIntegrationEvent,
38+
func: GitBookIntegrationEventCallback
39+
) => void;
40+
41+
/**
42+
* Register a custom tool to be exposed to the AI assistant.
43+
*/
44+
registerTool: (tool: GitBookIntegrationTool) => void;
45+
};
46+
47+
declare global {
48+
interface Window {
49+
/**
50+
* Global `window.GitBook` object accessible by integrations.
51+
*/
52+
GitBook?: GitBookGlobal;
53+
}
54+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"compilerOptions": {
3+
"target": "esnext",
4+
"lib": ["dom", "dom.iterable", "esnext"],
5+
"allowJs": true,
6+
"skipLibCheck": true,
7+
"strict": true,
8+
"noEmit": false,
9+
"declaration": true,
10+
"outDir": "dist",
11+
"esModuleInterop": true,
12+
"module": "esnext",
13+
"moduleResolution": "bundler",
14+
"resolveJsonModule": true,
15+
"isolatedModules": true,
16+
"jsx": "react-jsx",
17+
"incremental": true,
18+
"types": [
19+
"bun-types" // add Bun global
20+
]
21+
},
22+
"include": ["src/**/*.ts", "src/**/*.tsx"],
23+
"exclude": ["node_modules"]
24+
}

packages/gitbook/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"@gitbook/api": "catalog:",
77
"@gitbook/cache-tags": "workspace:*",
88
"@gitbook/colors": "workspace:*",
9+
"@gitbook/browser-types": "workspace:*",
910
"@gitbook/emoji-codepoints": "workspace:*",
1011
"@gitbook/fonts": "workspace:*",
1112
"@gitbook/icons": "workspace:*",

0 commit comments

Comments
 (0)