opencode-debug-agent 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/tools.ts ADDED
@@ -0,0 +1,131 @@
1
+ /**
2
+ * Debug Tools - OpenCode tools for runtime debugging workflow
3
+ */
4
+
5
+ import { tool } from '@opencode-ai/plugin';
6
+ import { debugServer } from './server';
7
+
8
+ /**
9
+ * Generate the instrumentation snippet with the given URL
10
+ */
11
+ function generateSnippet(url: string): string {
12
+ return `fetch("${url}/log", {
13
+ method: "POST",
14
+ headers: {"Content-Type": "application/json"},
15
+ body: JSON.stringify({label: "LABEL_HERE", data: {YOUR_DATA}})
16
+ })`;
17
+ }
18
+
19
+ /**
20
+ * Start debug server to capture runtime data
21
+ * Returns port, URL, and ready-to-use code snippet
22
+ */
23
+ export const debugStart = tool({
24
+ description:
25
+ 'Start debug server to capture runtime data. Returns port, URL, and ready-to-use code snippet for instrumentation.',
26
+ args: {
27
+ port: tool.schema
28
+ .number()
29
+ .optional()
30
+ .describe(
31
+ 'Specific port to use (optional). If not provided, reuses previous port or auto-selects.'
32
+ ),
33
+ },
34
+ async execute(args) {
35
+ const result = await debugServer.start(args.port);
36
+ return JSON.stringify({
37
+ port: result.port,
38
+ url: result.url,
39
+ snippet: generateSnippet(result.url),
40
+ message: `Debug server running on port ${result.port}. Use the snippet to instrument code.`,
41
+ });
42
+ },
43
+ });
44
+
45
+ /**
46
+ * Stop the debug server and flush remaining logs
47
+ */
48
+ export const debugStop = tool({
49
+ description: 'Stop the debug server and flush remaining logs to disk.',
50
+ args: {},
51
+ async execute() {
52
+ if (!debugServer.isRunning()) {
53
+ return JSON.stringify({ message: 'Debug server is not running.' });
54
+ }
55
+ await debugServer.stop();
56
+ return JSON.stringify({
57
+ message: 'Debug server stopped. Logs preserved in .opencode/debug.log',
58
+ });
59
+ },
60
+ });
61
+
62
+ /**
63
+ * Read captured debug logs
64
+ */
65
+ export const debugRead = tool({
66
+ description: 'Read captured debug logs. Returns parsed JSON array of log entries.',
67
+ args: {
68
+ tail: tool.schema
69
+ .number()
70
+ .optional()
71
+ .describe('Return only the last N entries. If not provided, returns all entries.'),
72
+ },
73
+ async execute(args) {
74
+ const entries = await debugServer.readLogs(args.tail);
75
+ if (entries.length === 0) {
76
+ return JSON.stringify({
77
+ entries: [],
78
+ message:
79
+ 'No log entries found. Make sure the debug server is running and code is instrumented.',
80
+ });
81
+ }
82
+ return JSON.stringify({
83
+ entries,
84
+ count: entries.length,
85
+ });
86
+ },
87
+ });
88
+
89
+ /**
90
+ * Clear the debug log file
91
+ */
92
+ export const debugClear = tool({
93
+ description: 'Clear the debug log file.',
94
+ args: {},
95
+ async execute() {
96
+ await debugServer.clearLogs();
97
+ return JSON.stringify({ message: 'Debug log cleared.' });
98
+ },
99
+ });
100
+
101
+ /**
102
+ * Check debug server status and get current port/URL
103
+ */
104
+ export const debugStatus = tool({
105
+ description:
106
+ 'Check if debug server is running and get current port/URL. Also shows persisted port from previous sessions.',
107
+ args: {},
108
+ async execute() {
109
+ const info = debugServer.getInfo();
110
+
111
+ if (info.active && info.url) {
112
+ return JSON.stringify({
113
+ active: true,
114
+ port: info.port,
115
+ url: info.url,
116
+ snippet: generateSnippet(info.url),
117
+ });
118
+ }
119
+
120
+ // Server not running - check for persisted port
121
+ const persistedPort = await debugServer.getPersistedPort();
122
+
123
+ return JSON.stringify({
124
+ active: false,
125
+ persistedPort,
126
+ hint: persistedPort
127
+ ? `Previous session used port ${persistedPort}. Call debug_start to reuse it.`
128
+ : 'No debug server configured. Call debug_start to begin.',
129
+ });
130
+ },
131
+ });