hopwire 0.1.3 → 0.1.5
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/dist/agent/redact.js +28 -1
- package/dist/agent-socket.js +3 -3
- package/dist/cli.js +37 -11
- package/dist/commands/doctor.js +20 -0
- package/dist/commands/http.js +10 -3
- package/dist/commands/mcp.js +1 -1
- package/dist/commands/up.js +10 -3
- package/dist/commands/watch.js +24 -2
- package/dist/config.js +9 -3
- package/dist/control-plane-client.js +65 -7
- package/dist/edge-bridge.js +27 -1
- package/dist/inspector-api.js +30 -0
- package/dist/interceptor.js +1 -0
- package/dist/mcp/body.js +15 -5
- package/dist/mcp/client.js +2 -2
- package/dist/mcp/error-summary.js +278 -0
- package/dist/mcp/tools.js +28 -6
- package/dist/outbound-proxy.js +206 -0
- package/dist/output.js +14 -3
- package/dist/serve.js +20 -6
- package/node_modules/@hopwire/protocol/dist/exchange.d.ts +5 -0
- package/node_modules/@hopwire/protocol/dist/exchange.js +2 -1
- package/package.json +1 -1
package/dist/inspector-api.js
CHANGED
|
@@ -3,6 +3,36 @@ import { INSPECTOR_HEARTBEAT_INTERVAL_MS, ProtocolError, decodeReplayOverrides,
|
|
|
3
3
|
import { isRedactedHeader } from './agent/redact.js';
|
|
4
4
|
import { summarize } from './agent/summary.js';
|
|
5
5
|
import { InvalidBlockError } from './log.js';
|
|
6
|
+
/**
|
|
7
|
+
* A teammate's log as their coding agents may see it (spec decision 140): only the exchanges the sharer marked with
|
|
8
|
+
* agents: true. Any other block is left out as a block that is not an exchange is, so no route can list, find or
|
|
9
|
+
* replay it; the browser inspector reads the log itself.
|
|
10
|
+
*/
|
|
11
|
+
export function agentsOnly(log) {
|
|
12
|
+
return {
|
|
13
|
+
get length() {
|
|
14
|
+
return log.length;
|
|
15
|
+
},
|
|
16
|
+
async get(index, options) {
|
|
17
|
+
const exchange = await log.get(index, options);
|
|
18
|
+
if (exchange.agents !== true) {
|
|
19
|
+
throw new InvalidBlockError(index);
|
|
20
|
+
}
|
|
21
|
+
return exchange;
|
|
22
|
+
},
|
|
23
|
+
async find(id) {
|
|
24
|
+
const found = await log.find(id);
|
|
25
|
+
return found?.exchange.agents === true ? found : null;
|
|
26
|
+
},
|
|
27
|
+
subscribe(listener) {
|
|
28
|
+
return log.subscribe((index, exchange) => {
|
|
29
|
+
if (exchange.agents === true) {
|
|
30
|
+
listener(index, exchange);
|
|
31
|
+
}
|
|
32
|
+
});
|
|
33
|
+
},
|
|
34
|
+
};
|
|
35
|
+
}
|
|
6
36
|
/** An answer of the API that is not a success: HTTP status, a stable code and one sentence. */
|
|
7
37
|
export class ApiError extends Error {
|
|
8
38
|
status;
|
package/dist/interceptor.js
CHANGED
|
@@ -45,6 +45,7 @@ export function createInterceptor(options) {
|
|
|
45
45
|
serviceName,
|
|
46
46
|
source: request.source,
|
|
47
47
|
...(request.replayOf === undefined ? {} : { replayOf: request.replayOf }),
|
|
48
|
+
...(options.allowAgents === true ? { agents: true } : {}),
|
|
48
49
|
request: {
|
|
49
50
|
method: request.method,
|
|
50
51
|
path: request.path,
|
package/dist/mcp/body.js
CHANGED
|
@@ -99,11 +99,14 @@ export function showBody(part, offset, maxChars) {
|
|
|
99
99
|
truncatedAtCapture: part.bodyTruncated,
|
|
100
100
|
};
|
|
101
101
|
}
|
|
102
|
-
const
|
|
102
|
+
const display = redactText(text, contentType);
|
|
103
|
+
const shown = safeText(display.text);
|
|
103
104
|
const page = shown.slice(offset, offset + maxChars);
|
|
104
105
|
return {
|
|
105
106
|
kind: 'text',
|
|
106
107
|
decodedFrom: decoded.decodedFrom,
|
|
108
|
+
capturedBytes: raw.byteLength,
|
|
109
|
+
display: display.display,
|
|
107
110
|
text: page,
|
|
108
111
|
offset,
|
|
109
112
|
totalChars: shown.length,
|
|
@@ -111,22 +114,29 @@ export function showBody(part, offset, maxChars) {
|
|
|
111
114
|
truncatedAtCapture: part.bodyTruncated,
|
|
112
115
|
};
|
|
113
116
|
}
|
|
117
|
+
/** The whole text of a body as the model is shown it (decoded and redacted, not paged), or null for a binary body. */
|
|
118
|
+
export function shownText(part) {
|
|
119
|
+
const contentType = getHeader(part.headers, 'content-type') ?? null;
|
|
120
|
+
const decoded = decodedBytes(part);
|
|
121
|
+
const text = decoded === null ? null : bodyText(decoded.bytes, contentType, part.bodyTruncated);
|
|
122
|
+
return text === null || text === '' ? null : { text: redactText(text, contentType).text, contentType };
|
|
123
|
+
}
|
|
114
124
|
/** JSON is shown pretty-printed with sensitive keys redacted, a form with sensitive fields redacted. */
|
|
115
125
|
function redactText(text, contentType) {
|
|
116
126
|
const type = (contentType ?? '').toLowerCase();
|
|
117
127
|
if (type.includes('x-www-form-urlencoded')) {
|
|
118
|
-
return redactUrlEncoded(text);
|
|
128
|
+
return { text: redactUrlEncoded(text), display: 'as-captured' };
|
|
119
129
|
}
|
|
120
130
|
if (type === '' || type.includes('json')) {
|
|
121
131
|
try {
|
|
122
|
-
return JSON.stringify(redactJson(JSON.parse(text)), null, 2);
|
|
132
|
+
return { text: JSON.stringify(redactJson(JSON.parse(text)), null, 2), display: 'json-pretty-printed' };
|
|
123
133
|
}
|
|
124
134
|
catch {
|
|
125
135
|
// Not JSON after all, or cut at the capture limit: what looks like a sensitive field is still redacted
|
|
126
|
-
return redactJsonText(text);
|
|
136
|
+
return { text: redactJsonText(text), display: 'as-captured' };
|
|
127
137
|
}
|
|
128
138
|
}
|
|
129
|
-
return text;
|
|
139
|
+
return { text, display: 'as-captured' };
|
|
130
140
|
}
|
|
131
141
|
/** Text with control and invisible format characters escaped; tabs and line breaks stay. */
|
|
132
142
|
export function safeText(text) {
|
package/dist/mcp/client.js
CHANGED
|
@@ -19,7 +19,7 @@ export class ToolError extends Error {
|
|
|
19
19
|
this.hint = hint;
|
|
20
20
|
}
|
|
21
21
|
}
|
|
22
|
-
export const START_HINT = 'Start
|
|
22
|
+
export const START_HINT = 'Start a tunnel with hopwire http <port> (or hopwire up) on the same data directory, without --no-agent, then try again.';
|
|
23
23
|
/** Largest answer read from the tunnel: a record holds up to about 6 MB of Base64 bodies */
|
|
24
24
|
const ANSWER_LIMIT_BYTES = 32 * 1024 * 1024;
|
|
25
25
|
const REQUEST_TIMEOUT_MS = 30_000;
|
|
@@ -165,5 +165,5 @@ export function createAgentClient(dataDir) {
|
|
|
165
165
|
};
|
|
166
166
|
}
|
|
167
167
|
export function noInstance() {
|
|
168
|
-
return new ToolError('no_instance', 'No tunnel with
|
|
168
|
+
return new ToolError('no_instance', 'No tunnel with the agent socket is running on this data directory.', START_HINT);
|
|
169
169
|
}
|
|
@@ -0,0 +1,278 @@
|
|
|
1
|
+
import { safeText } from './body.js';
|
|
2
|
+
const MAX_MESSAGE_CHARS = 600;
|
|
3
|
+
const MAX_FRAMES = 5;
|
|
4
|
+
/** How far into a page the summary looks for the error */
|
|
5
|
+
const SCAN_CHARS = 64 * 1024;
|
|
6
|
+
/** How far into a page the text fallback reads: error pages carry kilobytes of inline CSS before the first word */
|
|
7
|
+
const TEXT_SCAN_CHARS = 512 * 1024;
|
|
8
|
+
/** The summary of an error response, or null when the text holds nothing recognisable. */
|
|
9
|
+
export function summarizeError(text, contentType) {
|
|
10
|
+
const head = text.slice(0, SCAN_CHARS);
|
|
11
|
+
const type = (contentType ?? '').toLowerCase();
|
|
12
|
+
const summary = type.includes('json') ? fromJson(text) : (fromJson(text) ?? fromHtml(head, text));
|
|
13
|
+
if (summary === null) {
|
|
14
|
+
return null;
|
|
15
|
+
}
|
|
16
|
+
return {
|
|
17
|
+
...summary,
|
|
18
|
+
exception: clean(summary.exception, 200),
|
|
19
|
+
message: clean(summary.message, MAX_MESSAGE_CHARS),
|
|
20
|
+
location: clean(summary.location, 300),
|
|
21
|
+
appFrames: summary.appFrames.map((frame) => clean(frame, 300) ?? ''),
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
function clean(value, max) {
|
|
25
|
+
if (value === null) {
|
|
26
|
+
return null;
|
|
27
|
+
}
|
|
28
|
+
const text = safeText(value.trim());
|
|
29
|
+
return text.length > max ? `${text.slice(0, max)}…` : text;
|
|
30
|
+
}
|
|
31
|
+
// ---- JSON: Laravel and Symfony debug output, RFC 9457 problem details, { error } and { message } ----
|
|
32
|
+
function fromJson(text) {
|
|
33
|
+
let body;
|
|
34
|
+
try {
|
|
35
|
+
body = JSON.parse(text);
|
|
36
|
+
}
|
|
37
|
+
catch {
|
|
38
|
+
return null;
|
|
39
|
+
}
|
|
40
|
+
if (!isObject(body)) {
|
|
41
|
+
return null;
|
|
42
|
+
}
|
|
43
|
+
const nested = isObject(body.error) ? body.error : null;
|
|
44
|
+
const exception = string(body.exception) ?? string(body.class) ?? string(nested?.type) ?? string(body.title);
|
|
45
|
+
const message = string(body.message) ??
|
|
46
|
+
string(body.detail) ??
|
|
47
|
+
string(nested?.message) ??
|
|
48
|
+
string(body.error) ??
|
|
49
|
+
string(body.error_description);
|
|
50
|
+
if (exception === null && message === null) {
|
|
51
|
+
return null;
|
|
52
|
+
}
|
|
53
|
+
const file = string(body.file);
|
|
54
|
+
const line = typeof body.line === 'number' || typeof body.line === 'string' ? String(body.line) : null;
|
|
55
|
+
const frames = jsonFrames(body.trace ?? nested?.trace ?? body.stack ?? nested?.stack);
|
|
56
|
+
return {
|
|
57
|
+
format: 'json',
|
|
58
|
+
exception,
|
|
59
|
+
message,
|
|
60
|
+
location: file === null ? null : line === null ? file : `${file}:${line}`,
|
|
61
|
+
appFrames: pickFrames(frames),
|
|
62
|
+
totalFrames: frames.length,
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
function jsonFrames(trace) {
|
|
66
|
+
if (typeof trace === 'string') {
|
|
67
|
+
return stackLines(trace);
|
|
68
|
+
}
|
|
69
|
+
if (!Array.isArray(trace)) {
|
|
70
|
+
return [];
|
|
71
|
+
}
|
|
72
|
+
return trace.flatMap((frame) => {
|
|
73
|
+
if (typeof frame === 'string') {
|
|
74
|
+
return [frame];
|
|
75
|
+
}
|
|
76
|
+
if (!isObject(frame)) {
|
|
77
|
+
return [];
|
|
78
|
+
}
|
|
79
|
+
const where = string(frame.file) === null ? '[internal]' : `${string(frame.file)}:${String(frame.line ?? '?')}`;
|
|
80
|
+
const call = `${string(frame.class) ?? ''}${string(frame.type) ?? ''}${string(frame.function) ?? ''}`;
|
|
81
|
+
return [call === '' ? where : `${where} ${call}()`];
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
// ---- HTML ----
|
|
85
|
+
/**
|
|
86
|
+
* The known page formats first, then what any framework's page shows a reader (the exception name and the
|
|
87
|
+
* file:line locations in its visible text), then the title.
|
|
88
|
+
*/
|
|
89
|
+
function fromHtml(head, text) {
|
|
90
|
+
return (fromPhpComment(head) ??
|
|
91
|
+
fromExpress(head) ??
|
|
92
|
+
fromDjango(head) ??
|
|
93
|
+
fromVisibleText(text.slice(0, TEXT_SCAN_CHARS)) ??
|
|
94
|
+
fromTitle(head));
|
|
95
|
+
}
|
|
96
|
+
/** Laravel's and Symfony's HTML error pages begin with a comment that holds the exception and its trace. */
|
|
97
|
+
function fromPhpComment(html) {
|
|
98
|
+
const comment = /<!--\s*([\s\S]*?)-->/.exec(html)?.[1];
|
|
99
|
+
if (comment === undefined) {
|
|
100
|
+
return null;
|
|
101
|
+
}
|
|
102
|
+
const text = decodeEntities(comment);
|
|
103
|
+
const head = /^([A-Za-z_\\][\w\\]*): ([\s\S]*?) in file (\S+) on line (\d+)/.exec(text);
|
|
104
|
+
if (head === null) {
|
|
105
|
+
return null;
|
|
106
|
+
}
|
|
107
|
+
const frames = text
|
|
108
|
+
.split('\n')
|
|
109
|
+
.map((line) => /^#\d+ (.+)$/.exec(line.trim())?.[1])
|
|
110
|
+
.filter((line) => line !== undefined);
|
|
111
|
+
return {
|
|
112
|
+
format: 'php-comment',
|
|
113
|
+
exception: head[1] ?? null,
|
|
114
|
+
message: head[2] ?? null,
|
|
115
|
+
location: `${head[3]}:${head[4]}`,
|
|
116
|
+
appFrames: pickFrames(frames),
|
|
117
|
+
totalFrames: frames.length,
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
/** Express's default error handler: the stack in a <pre>, lines joined with <br>. */
|
|
121
|
+
function fromExpress(html) {
|
|
122
|
+
const pre = /<pre>([\s\S]*?)<\/pre>/i.exec(html)?.[1];
|
|
123
|
+
if (pre === undefined) {
|
|
124
|
+
return null;
|
|
125
|
+
}
|
|
126
|
+
const text = decodeEntities(pre.replace(/<br\s*\/?>/gi, '\n'));
|
|
127
|
+
const first = /^([A-Za-z_$][\w$.]*(?:Error|Exception)): ?(.*)$/m.exec(text.trim());
|
|
128
|
+
if (first === null) {
|
|
129
|
+
return null;
|
|
130
|
+
}
|
|
131
|
+
const frames = stackLines(text);
|
|
132
|
+
return {
|
|
133
|
+
format: 'express',
|
|
134
|
+
exception: first[1] ?? null,
|
|
135
|
+
message: first[2] ?? null,
|
|
136
|
+
location: frames[0] ?? null,
|
|
137
|
+
appFrames: pickFrames(frames),
|
|
138
|
+
totalFrames: frames.length,
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
/** Django's debug page. */
|
|
142
|
+
function fromDjango(html) {
|
|
143
|
+
const value = /<pre class="exception_value">([\s\S]*?)<\/pre>/i.exec(html)?.[1];
|
|
144
|
+
if (value === undefined) {
|
|
145
|
+
return null;
|
|
146
|
+
}
|
|
147
|
+
const title = titleOf(html);
|
|
148
|
+
const where = /<th[^>]*>Exception Location:<\/th>\s*<td>([\s\S]*?)<\/td>/i.exec(html)?.[1];
|
|
149
|
+
return {
|
|
150
|
+
format: 'django',
|
|
151
|
+
exception: title === null ? null : (/^(\S+) at /.exec(title)?.[1] ?? null),
|
|
152
|
+
message: decodeEntities(value),
|
|
153
|
+
location: where === undefined ? null : stripTags(where),
|
|
154
|
+
appFrames: [],
|
|
155
|
+
totalFrames: 0,
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* Any page or plain text, whatever the framework: the visible text (scripts, styles and tags removed) read for the
|
|
160
|
+
* first exception or error name (TypeError, ErrorException, java.lang.NullPointerException, KeyError) and for source
|
|
161
|
+
* locations in the forms stack traces print them (file.php:25, file.js:26:28, File "app.py", line 12, Foo.cs:line 42,
|
|
162
|
+
* Foo.java:42). Null when the text names neither, so a page without an error falls through to its title.
|
|
163
|
+
*/
|
|
164
|
+
function fromVisibleText(page) {
|
|
165
|
+
const text = visibleText(page);
|
|
166
|
+
const exception = EXCEPTION_NAME.exec(text);
|
|
167
|
+
const frames = locations(text);
|
|
168
|
+
if (exception === null && frames.length === 0) {
|
|
169
|
+
return null;
|
|
170
|
+
}
|
|
171
|
+
const start = exception?.index ?? 0;
|
|
172
|
+
return {
|
|
173
|
+
format: 'text',
|
|
174
|
+
exception: exception?.[1] ?? null,
|
|
175
|
+
message: text.slice(start, start + MAX_MESSAGE_CHARS * 2),
|
|
176
|
+
location: frames[0] ?? null,
|
|
177
|
+
appFrames: pickFrames(frames),
|
|
178
|
+
totalFrames: frames.length,
|
|
179
|
+
};
|
|
180
|
+
}
|
|
181
|
+
/** A class name ending in Exception or Error, optionally qualified (App\\Foo, java.lang.Foo, Foo::Bar) */
|
|
182
|
+
const EXCEPTION_NAME = /(?<![\w$\\.:])((?:[A-Za-z_][\w$]*(?:\\|\.|::))*[A-Z][\w$]*(?:Exception|Error))\b/;
|
|
183
|
+
const SOURCE_EXTENSION = 'php|phtml|m?js|cjs|tsx?|jsx|mts|cts|py|rb|erb|go|java|kt|scala|groovy|cs|cshtml|razor|fs|vb|ex|exs|erl|rs|swift|dart|c|cc|cpp|h|hpp|lua|pl|pm|twig|vue|svelte|astro';
|
|
184
|
+
/** file.ext followed by a line number: ":25" (also " :25", as separate HTML elements leave it), "(25)", '", line 25', ", line 25", ":line 25" */
|
|
185
|
+
const LOCATION = new RegExp(`((?:[A-Za-z]:)?[\\w@~.\\-/\\\\]*[\\w-]\\.(?:${SOURCE_EXTENSION}))(?:\\s?:(\\d+)|\\((\\d+)\\)|"?, line (\\d+)|:line (\\d+))`, 'g');
|
|
186
|
+
/** The source locations of a text in order of appearance, each once, as "file:line". */
|
|
187
|
+
function locations(text) {
|
|
188
|
+
const seen = new Set();
|
|
189
|
+
for (const match of text.matchAll(LOCATION)) {
|
|
190
|
+
const line = match[2] ?? match[3] ?? match[4] ?? match[5];
|
|
191
|
+
if (match[1] !== undefined && line !== undefined) {
|
|
192
|
+
seen.add(`${match[1]}:${line}`);
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
return [...seen];
|
|
196
|
+
}
|
|
197
|
+
/**
|
|
198
|
+
* What a reader of the page sees: no scripts, styles, comments or head, tags removed with their quoted attributes.
|
|
199
|
+
* Text that is not HTML (a plain stack trace, where "<anonymous>" is not a tag) is only folded onto one line.
|
|
200
|
+
*/
|
|
201
|
+
function visibleText(html) {
|
|
202
|
+
if (!/<(!doctype|html|body|div|p|pre|h1)\b/i.test(html.slice(0, SCAN_CHARS))) {
|
|
203
|
+
return html.replace(/\s+/g, ' ').trim();
|
|
204
|
+
}
|
|
205
|
+
return decodeEntities(html
|
|
206
|
+
.replace(/<!--[\s\S]*?-->/g, ' ')
|
|
207
|
+
.replace(/<(script|style|svg|template|noscript|head)\b[^>]*>[\s\S]*?<\/\1\s*>/gi, ' ')
|
|
208
|
+
// a quoted attribute may hold ">" (Alpine's x-data, inline handlers)
|
|
209
|
+
.replace(/<\/?[A-Za-z!][^>"']*(?:(?:"[^"]*"|'[^']*')[^>"']*)*>/g, ' '))
|
|
210
|
+
.replace(/\s+/g, ' ')
|
|
211
|
+
.trim();
|
|
212
|
+
}
|
|
213
|
+
/** Any other page: its title and first heading. */
|
|
214
|
+
function fromTitle(html) {
|
|
215
|
+
const title = titleOf(html);
|
|
216
|
+
const h1 = /<h1[^>]*>([\s\S]*?)<\/h1>/i.exec(html)?.[1];
|
|
217
|
+
const heading = h1 === undefined ? null : stripTags(h1);
|
|
218
|
+
if (title === null && (heading === null || heading === '')) {
|
|
219
|
+
return null;
|
|
220
|
+
}
|
|
221
|
+
return {
|
|
222
|
+
format: 'html',
|
|
223
|
+
exception: null,
|
|
224
|
+
message: heading !== null && heading !== '' && heading !== title
|
|
225
|
+
? `${title ?? ''}${title ? ': ' : ''}${heading}`
|
|
226
|
+
: title,
|
|
227
|
+
location: null,
|
|
228
|
+
appFrames: [],
|
|
229
|
+
totalFrames: 0,
|
|
230
|
+
};
|
|
231
|
+
}
|
|
232
|
+
function titleOf(html) {
|
|
233
|
+
const title = /<title[^>]*>([\s\S]*?)<\/title>/i.exec(html)?.[1];
|
|
234
|
+
if (title === undefined) {
|
|
235
|
+
return null;
|
|
236
|
+
}
|
|
237
|
+
const text = stripTags(title);
|
|
238
|
+
return text === '' ? null : text;
|
|
239
|
+
}
|
|
240
|
+
// ---- helpers ----
|
|
241
|
+
/** The "at ..." lines of a JavaScript stack. */
|
|
242
|
+
function stackLines(stack) {
|
|
243
|
+
return stack
|
|
244
|
+
.split('\n')
|
|
245
|
+
.map((line) => /^\s*at (.+)$/.exec(line)?.[1]?.trim())
|
|
246
|
+
.filter((line) => line !== undefined);
|
|
247
|
+
}
|
|
248
|
+
const LIBRARY_FRAME = /(^|[\s(\\/])(vendor|node_modules|site-packages|dist-packages|gems)[\\/]|[\\/]go[\\/]pkg[\\/]mod[\\/]|^node:|\(node:|\[internal\]/;
|
|
249
|
+
function pickFrames(frames) {
|
|
250
|
+
const own = frames.filter((frame) => !LIBRARY_FRAME.test(frame));
|
|
251
|
+
return (own.length > 0 ? own : frames).slice(0, MAX_FRAMES);
|
|
252
|
+
}
|
|
253
|
+
function stripTags(html) {
|
|
254
|
+
return decodeEntities(html.replace(/<[^>]*>/g, ' '))
|
|
255
|
+
.replace(/\s+/g, ' ')
|
|
256
|
+
.trim();
|
|
257
|
+
}
|
|
258
|
+
const ENTITIES = { amp: '&', lt: '<', gt: '>', quot: '"', apos: "'", nbsp: ' ' };
|
|
259
|
+
function decodeEntities(text) {
|
|
260
|
+
return text.replace(/&(#x[0-9a-f]+|#\d+|[a-z]+);/gi, (entity, name) => {
|
|
261
|
+
if (name.startsWith('#x') || name.startsWith('#X')) {
|
|
262
|
+
return codePoint(Number.parseInt(name.slice(2), 16)) ?? entity;
|
|
263
|
+
}
|
|
264
|
+
if (name.startsWith('#')) {
|
|
265
|
+
return codePoint(Number.parseInt(name.slice(1), 10)) ?? entity;
|
|
266
|
+
}
|
|
267
|
+
return ENTITIES[name.toLowerCase()] ?? entity;
|
|
268
|
+
});
|
|
269
|
+
}
|
|
270
|
+
function codePoint(value) {
|
|
271
|
+
return Number.isInteger(value) && value >= 0 && value <= 0x10ffff ? String.fromCodePoint(value) : null;
|
|
272
|
+
}
|
|
273
|
+
function isObject(value) {
|
|
274
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
275
|
+
}
|
|
276
|
+
function string(value) {
|
|
277
|
+
return typeof value === 'string' && value.trim() !== '' ? value : null;
|
|
278
|
+
}
|
package/dist/mcp/tools.js
CHANGED
|
@@ -2,8 +2,9 @@ import { getHeader, isBase64, isWebSocketHandshake, } from '@hopwire/protocol';
|
|
|
2
2
|
import { REDACTION_MARKER, isRedactedHeader, redactHeaders } from '../agent/redact.js';
|
|
3
3
|
import { summarize } from '../agent/summary.js';
|
|
4
4
|
import { printable } from '../output.js';
|
|
5
|
-
import { contentEncoding, decodedBytes, encode, showBody } from './body.js';
|
|
5
|
+
import { contentEncoding, decodedBytes, encode, showBody, shownText } from './body.js';
|
|
6
6
|
import { START_HINT, ToolError } from './client.js';
|
|
7
|
+
import { summarizeError } from './error-summary.js';
|
|
7
8
|
/**
|
|
8
9
|
* The tools of `hopwire mcp` (spec phase 10). Everything they return is captured traffic of third parties, so every
|
|
9
10
|
* result carries NOTE and every description repeats it; values a model reads are escaped and redacted, and a replay
|
|
@@ -231,6 +232,13 @@ function show(summary) {
|
|
|
231
232
|
response: printableContentType(summary.response),
|
|
232
233
|
};
|
|
233
234
|
}
|
|
235
|
+
/** The summary without its part summaries, for a result that shows the parts themselves. */
|
|
236
|
+
function showHead(summary) {
|
|
237
|
+
const head = show(summary);
|
|
238
|
+
delete head.request;
|
|
239
|
+
delete head.response;
|
|
240
|
+
return head;
|
|
241
|
+
}
|
|
234
242
|
function printableContentType(part) {
|
|
235
243
|
return { ...part, contentType: part.contentType === null ? null : printable(part.contentType) };
|
|
236
244
|
}
|
|
@@ -240,6 +248,18 @@ function shownHeaders(headers) {
|
|
|
240
248
|
function shownPart(part, offset, max) {
|
|
241
249
|
return { headers: shownHeaders(part.headers), body: showBody(part, offset, max) };
|
|
242
250
|
}
|
|
251
|
+
/** The response of a server error with a summary of its error page first, so the exception is read before the page. */
|
|
252
|
+
function shownResponse(exchange, offset, max) {
|
|
253
|
+
const { headers, body } = shownPart(exchange.response, offset, max);
|
|
254
|
+
if (exchange.response.status < 500) {
|
|
255
|
+
return { headers, body };
|
|
256
|
+
}
|
|
257
|
+
return { headers, errorSummary: errorSummaryOf(exchange.response), body };
|
|
258
|
+
}
|
|
259
|
+
function errorSummaryOf(part) {
|
|
260
|
+
const shown = shownText(part);
|
|
261
|
+
return shown === null ? null : summarizeError(shown.text, shown.contentType);
|
|
262
|
+
}
|
|
243
263
|
/** Why an exchange cannot be replayed as it is, or null. */
|
|
244
264
|
function notReplayable(exchange) {
|
|
245
265
|
if (isWebSocketHandshake(exchange)) {
|
|
@@ -277,7 +297,7 @@ async function record(context, id) {
|
|
|
277
297
|
const statusTool = {
|
|
278
298
|
name: 'hopwire_status',
|
|
279
299
|
title: 'Hopwire tunnel status',
|
|
280
|
-
description: `Whether a Hopwire tunnel started with --agent is running on this machine's data directory, its mode and its services (local target, loopback entry point, public URL). Call it first. ${NOTE}`,
|
|
300
|
+
description: `Whether a Hopwire tunnel with the agent socket (on unless started with --no-agent) is running on this machine's data directory, its mode and its services (local target, loopback entry point, public URL). Call it first. ${NOTE}`,
|
|
281
301
|
inputSchema: schema({}),
|
|
282
302
|
annotations: { readOnlyHint: true, openWorldHint: false },
|
|
283
303
|
async call(args, context) {
|
|
@@ -340,7 +360,7 @@ const listTool = {
|
|
|
340
360
|
const getTool = {
|
|
341
361
|
name: 'hopwire_get_exchange',
|
|
342
362
|
title: 'Show a captured request',
|
|
343
|
-
description: `One captured exchange by id: request and response headers (credentials and signatures redacted) and bodies, decompressed and decoded as text in pages of maxChars (JSON pretty-printed, fields such as password, token or card redacted; continue with bodyOffset); a binary body is given as size and sha256 only. ${NOTE}`,
|
|
363
|
+
description: `One captured exchange by id: request and response headers (credentials and signatures redacted) and bodies, decompressed and decoded as text in pages of maxChars (JSON pretty-printed, fields such as password, token or card redacted; continue with bodyOffset); a binary body is given as size and sha256 only. A text body names capturedBytes and display: json-pretty-printed means the JSON was printed again for reading, so the text is not the bytes that were sent (a signature over the body covers those bytes, and bodyOffset, maxBodyChars and totalChars count characters of the text shown); as-captured is the text as sent, decompressed when decodedFrom is set. Signature headers keep their timestamp (Stripe's t=) and show the rest redacted. A response with status 500 or above also has errorSummary: exception, message, location and the first stack frames outside library folders (vendor, node_modules, site-packages), read from the error page: the known formats of Laravel and Symfony, Express, Django and JSON error bodies, otherwise from any framework's page or plain-text stack trace the first exception name and the file:line locations it shows, otherwise the page title, or null when the page names no error. Read it before paging through the body. ${NOTE}`,
|
|
344
364
|
inputSchema: schema({
|
|
345
365
|
id: { type: 'string' },
|
|
346
366
|
part: { type: 'string', enum: ['request', 'response', 'both'], default: 'both' },
|
|
@@ -360,9 +380,9 @@ const getTool = {
|
|
|
360
380
|
const found = await record(context, id);
|
|
361
381
|
const reason = notReplayable(found.exchange);
|
|
362
382
|
return {
|
|
363
|
-
exchange:
|
|
383
|
+
exchange: showHead(summarize(found)),
|
|
364
384
|
...(part === 'response' ? {} : { request: shownPart(found.exchange.request, offset, max) }),
|
|
365
|
-
...(part === 'request' ? {} : { response:
|
|
385
|
+
...(part === 'request' ? {} : { response: shownResponse(found.exchange, offset, max) }),
|
|
366
386
|
replayable: reason === null,
|
|
367
387
|
notReplayableReason: reason,
|
|
368
388
|
};
|
|
@@ -630,7 +650,7 @@ export function applyEdit(stored, edit) {
|
|
|
630
650
|
const replayTool = {
|
|
631
651
|
name: 'hopwire_replay',
|
|
632
652
|
title: 'Replay a captured request',
|
|
633
|
-
description: `Sends a captured request again to the tunnel's local service (and nowhere else) and returns the new response; the replay is logged with source replay. Without edit the stored request is sent byte for byte, credentials and signatures included. edit changes it: method, path, setHeaders, removeHeaders, and one of body (text), bodyBase64 or jsonSet (RFC 6901 pointers patched into the stored JSON body). Redacted placeholders are refused: leave a field out to keep its stored value. Side effects on the local service are real (database writes, mails). Signed webhooks (Stripe's timestamp tolerance, GitHub's HMAC) may reject an old or edited replay; use the handler's test signing mode. One replay at a time, at most ${REPLAYS_PER_MINUTE} a minute. ${NOTE}`,
|
|
653
|
+
description: `Sends a captured request again to the tunnel's local service (and nowhere else) and returns the new response; the replay is logged with source replay. Without edit the stored request is sent byte for byte, credentials and signatures included, and the result says sentAsCaptured: true; with edit it is false. edit changes it: method, path, setHeaders, removeHeaders, and one of body (text), bodyBase64 or jsonSet (RFC 6901 pointers patched into the stored JSON body). Redacted placeholders are refused: leave a field out to keep its stored value. Side effects on the local service are real (database writes, mails). Signed webhooks (Stripe's timestamp tolerance, GitHub's HMAC) may reject an old or edited replay; use the handler's test signing mode. One replay at a time, at most ${REPLAYS_PER_MINUTE} a minute. ${NOTE}`,
|
|
634
654
|
inputSchema: schema({
|
|
635
655
|
id: { type: 'string' },
|
|
636
656
|
responseBodyChars: {
|
|
@@ -695,6 +715,8 @@ const replayTool = {
|
|
|
695
715
|
const body = showBody(exchange.response, 0, chars);
|
|
696
716
|
return {
|
|
697
717
|
original: { id: original.exchange.id, status: original.exchange.response.status },
|
|
718
|
+
// Without an edit the tunnel sends the stored method, path, headers and body bytes of that block
|
|
719
|
+
sentAsCaptured: edit === null,
|
|
698
720
|
replay: show(summarize(stored)),
|
|
699
721
|
response: {
|
|
700
722
|
status: exchange.response.status,
|