-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Expand file tree
/
Copy pathmemory.ts
More file actions
130 lines (119 loc) · 4.02 KB
/
memory.ts
File metadata and controls
130 lines (119 loc) · 4.02 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
/**
* @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 takeMemorySnapshot = definePageTool({
name: 'take_memory_snapshot',
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,
handler: async (request, response, context) => {
const page = request.page;
context.validatePath(request.params.filePath);
await page.pptrPage.captureHeapSnapshot({
path: ensureExtension(request.params.filePath, '.heapsnapshot'),
});
response.appendResponseLine(
`Heap snapshot saved to ${request.params.filePath}`,
);
},
});
export const exploreMemorySnapshot = defineTool({
name: 'load_memory_snapshot',
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,
handler: async (request, response, context) => {
context.validatePath(request.params.filePath);
const stats = await context.getHeapSnapshotStats(request.params.filePath);
const staticData = await context.getHeapSnapshotStaticData(
request.params.filePath,
);
response.setHeapSnapshotStats(stats, staticData);
},
});
export const getMemorySnapshotDetails = defineTool({
name: 'get_memory_snapshot_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,
handler: async (request, response, context) => {
context.validatePath(request.params.filePath);
const aggregates = await context.getHeapSnapshotAggregates(
request.params.filePath,
);
response.setHeapSnapshotAggregates(aggregates, {
pageIdx: request.params.pageIdx,
pageSize: request.params.pageSize,
});
},
});
export const getNodesByClass = defineTool({
name: 'get_nodes_by_class',
description:
'Loads a memory heapsnapshot and returns instances of a specific class with their stable IDs.',
annotations: {
category: ToolCategory.MEMORY,
readOnlyHint: true,
conditions: ['experimentalMemory'],
},
schema: {
filePath: zod.string().describe('A path to a .heapsnapshot file to read.'),
uid: zod
.number()
.describe(
'The unique UID for the class, obtained from aggregates listing.',
),
pageIdx: zod.number().optional().describe('The page index for pagination.'),
pageSize: zod.number().optional().describe('The page size for pagination.'),
},
blockedByDialog: false,
handler: async (request, response, context) => {
context.validatePath(request.params.filePath);
const nodes = await context.getHeapSnapshotNodesByUid(
request.params.filePath,
request.params.uid,
);
response.setHeapSnapshotNodes(nodes, {
pageIdx: request.params.pageIdx,
pageSize: request.params.pageSize,
});
},
});