-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Expand file tree
/
Copy pathmemory.ts
More file actions
156 lines (143 loc) · 4.9 KB
/
memory.ts
File metadata and controls
156 lines (143 loc) · 4.9 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/**
* @license
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import {zod} from '../third_party/index.js';
import {ensureExtension} from '../utils/files.js';
import {ToolCategory} from './categories.js';
import {definePageTool, defineTool} from './ToolDefinition.js';
export const takeHeapSnapshot = definePageTool({
name: 'take_heapsnapshot',
description: `Capture a heap snapshot of the currently selected page. Use to analyze the memory distribution of JavaScript objects and debug memory leaks.`,
annotations: {
category: ToolCategory.MEMORY,
readOnlyHint: false,
},
schema: {
filePath: zod
.string()
.describe('A path to a .heapsnapshot file to save the heapsnapshot to.'),
},
blockedByDialog: true,
verifyFilesSchema: ['filePath'],
handler: async (request, response) => {
const page = request.page;
await page.pptrPage.captureHeapSnapshot({
path: ensureExtension(request.params.filePath, '.heapsnapshot'),
});
response.appendResponseLine(
`Heap snapshot saved to ${request.params.filePath}`,
);
},
});
export const getHeapSnapshotSummary = defineTool({
name: 'get_heapsnapshot_summary',
description:
'Loads a memory heapsnapshot and returns snapshot summary stats.',
annotations: {
category: ToolCategory.MEMORY,
readOnlyHint: true,
conditions: ['experimentalMemory'],
},
schema: {
filePath: zod.string().describe('A path to a .heapsnapshot file to read.'),
},
blockedByDialog: false,
verifyFilesSchema: ['filePath'],
handler: async (request, response, context) => {
const stats = await context.getHeapSnapshotStats(request.params.filePath);
const staticData = await context.getHeapSnapshotStaticData(
request.params.filePath,
);
response.setHeapSnapshotStats(stats, staticData);
},
});
export const getHeapSnapshotDetails = defineTool({
name: 'get_heapsnapshot_details',
description:
'Loads a memory heapsnapshot and returns all available information including statistics, static data, and aggregated node information. Supports pagination for aggregates.',
annotations: {
category: ToolCategory.MEMORY,
readOnlyHint: true,
conditions: ['experimentalMemory'],
},
schema: {
filePath: zod.string().describe('A path to a .heapsnapshot file to read.'),
pageIdx: zod
.number()
.optional()
.describe('The page index for pagination of aggregates.'),
pageSize: zod
.number()
.optional()
.describe('The page size for pagination of aggregates.'),
},
blockedByDialog: false,
verifyFilesSchema: ['filePath'],
handler: async (request, response, context) => {
const aggregates = await context.getHeapSnapshotAggregates(
request.params.filePath,
);
response.setHeapSnapshotAggregates(aggregates, {
pageIdx: request.params.pageIdx,
pageSize: request.params.pageSize,
});
},
});
export const getHeapSnapshotClassNodes = defineTool({
name: 'get_heapsnapshot_class_nodes',
description:
'Loads a memory heapsnapshot and returns instances of a specific class with their IDs.',
annotations: {
category: ToolCategory.MEMORY,
readOnlyHint: true,
conditions: ['experimentalMemory'],
},
schema: {
filePath: zod.string().describe('A path to a .heapsnapshot file to read.'),
id: zod.number().describe('The ID for the class, obtained from details.'),
pageIdx: zod.number().optional().describe('The page index for pagination.'),
pageSize: zod.number().optional().describe('The page size for pagination.'),
},
blockedByDialog: false,
verifyFilesSchema: ['filePath'],
handler: async (request, response, context) => {
const nodes = await context.getHeapSnapshotNodesById(
request.params.filePath,
request.params.id,
);
response.setHeapSnapshotNodes(nodes, {
pageIdx: request.params.pageIdx,
pageSize: request.params.pageSize,
});
},
});
export const getHeapSnapshotRetainers = defineTool({
name: 'get_heapsnapshot_retainers',
description:
'Loads a memory heapsnapshot and returns retainers for a specific node ID.',
annotations: {
category: ToolCategory.MEMORY,
readOnlyHint: true,
conditions: ['experimentalMemory'],
},
blockedByDialog: false,
verifyFilesSchema: ['filePath'],
schema: {
filePath: zod.string().describe('A path to a .heapsnapshot file to read.'),
nodeId: zod.number().describe('The node ID to get retainers for.'),
pageIdx: zod.number().optional().describe('The page index for pagination.'),
pageSize: zod.number().optional().describe('The page size for pagination.'),
},
handler: async (request, response, context) => {
const retainers = await context.getHeapSnapshotRetainers(
request.params.filePath,
request.params.nodeId,
);
response.setHeapSnapshotNodes(retainers, {
pageIdx: request.params.pageIdx,
pageSize: request.params.pageSize,
});
},
});