-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Expand file tree
/
Copy pathToolDefinition.ts
More file actions
84 lines (76 loc) · 2.29 KB
/
ToolDefinition.ts
File metadata and controls
84 lines (76 loc) · 2.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/**
* @license
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import z from 'zod';
import {Dialog, ElementHandle, Page} from 'puppeteer-core';
import {ToolCategories} from './categories.js';
import {TraceResult} from '../trace-processing/parse.js';
export interface ToolDefinition<
Schema extends Zod.ZodRawShape = Zod.ZodRawShape,
> {
name: string;
description: string;
annotations: {
title?: string;
category: ToolCategories;
/**
* If true, the tool does not modify its environment.
*/
readOnlyHint: boolean;
};
schema: Schema;
handler: (
request: Request<Schema>,
response: Response,
context: Context,
) => Promise<void>;
}
export interface Request<Schema extends Zod.ZodRawShape> {
params: z.objectOutputType<Schema, z.ZodTypeAny>;
}
export type ImageContentData = {
data: string;
mimeType: string;
};
export interface Response {
appendResponseLine(value: string): void;
setIncludePages(value: boolean): void;
setIncludeNetworkRequests(value: boolean): void;
setIncludeConsoleData(value: boolean): void;
setIncludeSnapshot(value: boolean): void;
attachImage(value: ImageContentData): void;
attachNetworkRequest(url: string): void;
}
/**
* Only add methods required by tools/*.
*/
export type Context = Readonly<{
isRunningPerformanceTrace(): boolean;
setIsRunningPerformanceTrace(x: boolean): void;
recordedTraces(): TraceResult[];
storeTraceRecording(result: TraceResult): void;
getSelectedPage(): Page;
getDialog(): Dialog | undefined;
clearDialog(): void;
getPageByIdx(idx: number): Page;
newPage(): Promise<Page>;
closePage(pageIdx: number): Promise<void>;
setSelectedPageIdx(idx: number): void;
getElementByUid(uid: string): Promise<ElementHandle<Element>>;
setNetworkConditions(conditions: string | null): void;
setCpuThrottlingRate(rate: number): void;
saveTemporaryFile(
data: Uint8Array<ArrayBufferLike>,
mimeType: 'image/png' | 'image/jpeg',
): Promise<{filename: string}>;
waitForEventsAfterAction(action: () => Promise<unknown>): Promise<void>;
}>;
export function defineTool<Schema extends Zod.ZodRawShape>(
definition: ToolDefinition<Schema>,
) {
return definition;
}
export const CLOSE_PAGE_ERROR =
'The last open page cannot be closed. It is fine to keep it open.';