hopwire 0.1.9 → 0.2.1
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/account.js +180 -35
- package/dist/agent/redact.js +55 -8
- package/dist/cli.js +172 -58
- package/dist/commands/doctor.js +53 -12
- package/dist/commands/http.js +19 -4
- package/dist/commands/init.js +38 -9
- package/dist/commands/login.js +47 -8
- package/dist/commands/up.js +16 -3
- package/dist/commands/watch.js +33 -4
- package/dist/config-template.js +11 -1
- package/dist/config.js +65 -6
- package/dist/control-plane-client.js +42 -0
- package/dist/inspector/assets/index-CIOfyIbK.js +5 -0
- package/dist/inspector/assets/index-Du4-DCNN.css +2 -0
- package/dist/inspector/index.html +2 -2
- package/dist/inspector-api.js +145 -0
- package/dist/interceptor.js +19 -4
- package/dist/log-follow.js +473 -0
- package/dist/log.js +178 -11
- package/dist/mcp/logs.js +46 -0
- package/dist/mcp/tools.js +136 -3
- package/dist/output.js +38 -0
- package/dist/relay.js +1 -1
- package/dist/serve.js +49 -2
- package/node_modules/@hopwire/protocol/dist/constants.d.ts +10 -0
- package/node_modules/@hopwire/protocol/dist/constants.js +10 -0
- package/node_modules/@hopwire/protocol/dist/index.d.ts +1 -0
- package/node_modules/@hopwire/protocol/dist/index.js +1 -0
- package/node_modules/@hopwire/protocol/dist/inspector.d.ts +18 -0
- package/node_modules/@hopwire/protocol/dist/log-entries.d.ts +72 -0
- package/node_modules/@hopwire/protocol/dist/log-entries.js +147 -0
- package/node_modules/@hopwire/protocol/dist/ws-messages.d.ts +2 -1
- package/package.json +1 -1
- package/dist/inspector/assets/index-D3yNjmSA.js +0 -2
- package/dist/inspector/assets/index-XotF7D1X.css +0 -2
package/dist/inspector-api.js
CHANGED
|
@@ -4,6 +4,68 @@ import { isRedactedHeader } from './agent/redact.js';
|
|
|
4
4
|
import { summarize } from './agent/summary.js';
|
|
5
5
|
import { InvalidBlockError } from './log.js';
|
|
6
6
|
import { WsReplays, recordedInMessages } from './ws-replay.js';
|
|
7
|
+
/**
|
|
8
|
+
* This machine's own log together with its core of log entries that are not shared (phase 15): everything is read
|
|
9
|
+
* from the log, and log entries from both, merged by time.
|
|
10
|
+
*/
|
|
11
|
+
export function withLocalLogs(log, local) {
|
|
12
|
+
if (local === null) {
|
|
13
|
+
return log;
|
|
14
|
+
}
|
|
15
|
+
return {
|
|
16
|
+
get length() {
|
|
17
|
+
return log.length;
|
|
18
|
+
},
|
|
19
|
+
get: (index, options) => log.get(index, options),
|
|
20
|
+
find: (id) => log.find(id),
|
|
21
|
+
subscribe: (listener) => log.subscribe(listener),
|
|
22
|
+
wsMessages: (exchangeId, handshakeIndex, afterSeq) => log.wsMessages(exchangeId, handshakeIndex, afterSeq),
|
|
23
|
+
subscribeWsMessages: (listener) => log.subscribeWsMessages(listener),
|
|
24
|
+
async logEntries(query) {
|
|
25
|
+
const [shared, unshared] = await Promise.all([log.logEntries(query), local.logEntries(query)]);
|
|
26
|
+
return mergeLogSlices([shared, unshared], query.limit);
|
|
27
|
+
},
|
|
28
|
+
subscribeLogEntries(listener) {
|
|
29
|
+
const endShared = log.subscribeLogEntries(listener);
|
|
30
|
+
const endLocal = local.subscribeLogEntries(listener);
|
|
31
|
+
return () => {
|
|
32
|
+
endShared();
|
|
33
|
+
endLocal();
|
|
34
|
+
};
|
|
35
|
+
},
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
/** Several answers of `logEntries` as one: blocks by the time of their first entry, the newest `limit` entries kept. */
|
|
39
|
+
export function mergeLogSlices(slices, limit) {
|
|
40
|
+
const merged = newestRecords(slices.flatMap((slice) => slice.blocks.map((block) => ({ origin: 'own', block }))), limit);
|
|
41
|
+
return {
|
|
42
|
+
blocks: merged.records.map((record) => record.block),
|
|
43
|
+
more: merged.more || slices.some((slice) => slice.more),
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
/** Records of log entries sorted by the time of their first entry, cut to the newest `limit` entries. */
|
|
47
|
+
function newestRecords(records, limit) {
|
|
48
|
+
const sorted = [...records].sort((a, b) => (a.block.entries[0]?.readAt ?? 0) - (b.block.entries[0]?.readAt ?? 0));
|
|
49
|
+
let more = false;
|
|
50
|
+
let count = sorted.reduce((sum, record) => sum + record.block.entries.length, 0);
|
|
51
|
+
while (count > limit && sorted.length > 0) {
|
|
52
|
+
const oldest = sorted[0];
|
|
53
|
+
if (oldest === undefined) {
|
|
54
|
+
break;
|
|
55
|
+
}
|
|
56
|
+
const over = count - limit;
|
|
57
|
+
if (oldest.block.entries.length <= over) {
|
|
58
|
+
sorted.shift();
|
|
59
|
+
count -= oldest.block.entries.length;
|
|
60
|
+
}
|
|
61
|
+
else {
|
|
62
|
+
sorted[0] = { ...oldest, block: { ...oldest.block, entries: oldest.block.entries.slice(over) } };
|
|
63
|
+
count = limit;
|
|
64
|
+
}
|
|
65
|
+
more = true;
|
|
66
|
+
}
|
|
67
|
+
return { records: sorted, more };
|
|
68
|
+
}
|
|
7
69
|
/**
|
|
8
70
|
* A teammate's log as their coding agents may see it (spec decision 140): only the exchanges the sharer marked with
|
|
9
71
|
* agents: true, and the recorded messages of a WebSocket whose handshake is marked. Any other block is left out as a
|
|
@@ -50,6 +112,17 @@ export function agentsOnly(log) {
|
|
|
50
112
|
}
|
|
51
113
|
return log.wsMessages(exchangeId, handshakeIndex, afterSeq);
|
|
52
114
|
},
|
|
115
|
+
async logEntries(query) {
|
|
116
|
+
const slice = await log.logEntries(query);
|
|
117
|
+
return { blocks: slice.blocks.filter((block) => block.agents === true), more: slice.more };
|
|
118
|
+
},
|
|
119
|
+
subscribeLogEntries(listener) {
|
|
120
|
+
return log.subscribeLogEntries((block) => {
|
|
121
|
+
if (block.agents === true) {
|
|
122
|
+
listener(block);
|
|
123
|
+
}
|
|
124
|
+
});
|
|
125
|
+
},
|
|
53
126
|
subscribeWsMessages(listener) {
|
|
54
127
|
const endExchanges = log.subscribe((_index, exchange) => {
|
|
55
128
|
if (isWebSocketHandshake(exchange)) {
|
|
@@ -153,6 +226,10 @@ export function createInspectorApi(options) {
|
|
|
153
226
|
const record = { index, origin, block };
|
|
154
227
|
broadcast(`event: ws_messages\ndata: ${JSON.stringify(record)}\n\n`);
|
|
155
228
|
}),
|
|
229
|
+
log.subscribeLogEntries((block) => {
|
|
230
|
+
const record = { origin, block };
|
|
231
|
+
broadcast(`event: log_entries\ndata: ${JSON.stringify(record)}\n\n`);
|
|
232
|
+
}),
|
|
156
233
|
]);
|
|
157
234
|
const heartbeat = setInterval(() => broadcast(': heartbeat\n\n'), options.heartbeatIntervalMs ?? INSPECTOR_HEARTBEAT_INTERVAL_MS);
|
|
158
235
|
heartbeat.unref();
|
|
@@ -189,6 +266,10 @@ export function createInspectorApi(options) {
|
|
|
189
266
|
only(method, 'GET');
|
|
190
267
|
sendJson(res, 200, await wsMessagesOf(options.sources, second, params));
|
|
191
268
|
}
|
|
269
|
+
else if (route.length === 1 && first === 'log-entries') {
|
|
270
|
+
only(method, 'GET');
|
|
271
|
+
sendJson(res, 200, await logEntriesOf(options.sources, params));
|
|
272
|
+
}
|
|
192
273
|
else if (first === 'replay' && second !== undefined && second !== '' && more.length === 0) {
|
|
193
274
|
only(method, 'POST');
|
|
194
275
|
sendJson(res, 200, await replay(options, replays, second, req, params));
|
|
@@ -636,6 +717,70 @@ async function wsMessagesOf(sources, id, query) {
|
|
|
636
717
|
}
|
|
637
718
|
throw new ApiError(404, 'not_found', 'No WebSocket handshake with this id is in the log');
|
|
638
719
|
}
|
|
720
|
+
const LOG_ENTRIES_DEFAULT_LIMIT = 200;
|
|
721
|
+
const LOG_ENTRIES_MAX_LIMIT = 1000;
|
|
722
|
+
/** Entries linked to one exchange one answer holds at most */
|
|
723
|
+
const LOG_ENTRIES_PER_EXCHANGE_LIMIT = 2000;
|
|
724
|
+
const LOG_LEVELS = ['debug', 'info', 'warning', 'error'];
|
|
725
|
+
/**
|
|
726
|
+
* `GET /api/log-entries` (phase 15): with `exchangeId` the entries linked to that exchange, from the log that holds
|
|
727
|
+
* it; otherwise the newest entries of every log (or the one `origin` names), filtered by `since`, `source`, `level`
|
|
728
|
+
* and `contains`, up to `limit`. Oldest first.
|
|
729
|
+
*/
|
|
730
|
+
async function logEntriesOf(sources, query) {
|
|
731
|
+
const origin = originParameter(query);
|
|
732
|
+
const text = (name, max) => {
|
|
733
|
+
const value = query.get(name);
|
|
734
|
+
if (value === null) {
|
|
735
|
+
return undefined;
|
|
736
|
+
}
|
|
737
|
+
if (value === '' || value.length > max) {
|
|
738
|
+
throw new ApiError(400, 'invalid_request', `${name} must be between 1 and ${max} characters`);
|
|
739
|
+
}
|
|
740
|
+
return value;
|
|
741
|
+
};
|
|
742
|
+
const exchangeId = text('exchangeId', 128);
|
|
743
|
+
const level = text('level', 16);
|
|
744
|
+
if (level !== undefined && !LOG_LEVELS.some((known) => known === level)) {
|
|
745
|
+
throw new ApiError(400, 'invalid_request', 'level must be debug, info, warning or error');
|
|
746
|
+
}
|
|
747
|
+
if (exchangeId !== undefined) {
|
|
748
|
+
for (const source of sources) {
|
|
749
|
+
if (origin !== undefined && source.origin !== origin) {
|
|
750
|
+
continue;
|
|
751
|
+
}
|
|
752
|
+
const found = await source.log.find(exchangeId);
|
|
753
|
+
if (found !== null) {
|
|
754
|
+
const slice = await source.log.logEntries({
|
|
755
|
+
exchange: found.exchange,
|
|
756
|
+
limit: LOG_ENTRIES_PER_EXCHANGE_LIMIT,
|
|
757
|
+
});
|
|
758
|
+
return { blocks: slice.blocks.map((block) => ({ origin: source.origin, block })), more: slice.more };
|
|
759
|
+
}
|
|
760
|
+
}
|
|
761
|
+
throw new ApiError(404, 'unknown_exchange', 'No exchange with this id is in the log');
|
|
762
|
+
}
|
|
763
|
+
const filter = {
|
|
764
|
+
limit: integerParameter(query, 'limit', 1, LOG_ENTRIES_MAX_LIMIT) ?? LOG_ENTRIES_DEFAULT_LIMIT,
|
|
765
|
+
since: integerParameter(query, 'since', 0, Number.MAX_SAFE_INTEGER),
|
|
766
|
+
source: text('source', 255),
|
|
767
|
+
level: level,
|
|
768
|
+
contains: text('contains', 1024),
|
|
769
|
+
};
|
|
770
|
+
const records = [];
|
|
771
|
+
let more = false;
|
|
772
|
+
for (const source of sources) {
|
|
773
|
+
if (origin !== undefined && source.origin !== origin) {
|
|
774
|
+
continue;
|
|
775
|
+
}
|
|
776
|
+
const slice = await source.log.logEntries(filter);
|
|
777
|
+
more ||= slice.more;
|
|
778
|
+
records.push(...slice.blocks.map((block) => ({ origin: source.origin, block })));
|
|
779
|
+
}
|
|
780
|
+
// The newest `limit` entries of all logs together
|
|
781
|
+
const merged = newestRecords(records, filter.limit);
|
|
782
|
+
return { blocks: merged.records, more: more || merged.more };
|
|
783
|
+
}
|
|
639
784
|
/** The body of a replay is known in full, so Content-Length is set to what is really sent. */
|
|
640
785
|
function replayHeaders(headers, body) {
|
|
641
786
|
const hadLength = headers.some(([name]) => name.toLowerCase() === 'content-length');
|
package/dist/interceptor.js
CHANGED
|
@@ -2,7 +2,7 @@ import { randomUUID } from 'node:crypto';
|
|
|
2
2
|
import http from 'node:http';
|
|
3
3
|
import https from 'node:https';
|
|
4
4
|
import { WebSocket } from 'ws';
|
|
5
|
-
import { BODY_CAPTURE_LIMIT_BYTES, WS_MESSAGE_MAX_BYTES, getHeader, headerListFromRaw, headerListToRaw, offeredSubprotocols, withoutHandshakeHeaders, withoutHopByHopHeaders, } from '@hopwire/protocol';
|
|
5
|
+
import { BODY_CAPTURE_LIMIT_BYTES, EXCHANGE_ID_HEADER, WS_MESSAGE_MAX_BYTES, getHeader, headerListFromRaw, headerListToRaw, offeredSubprotocols, withoutHandshakeHeaders, withoutHopByHopHeaders, } from '@hopwire/protocol';
|
|
6
6
|
import { BodyCapture } from './body-capture.js';
|
|
7
7
|
import { WS_CLOSE_TIMEOUT_MS } from './websocket.js';
|
|
8
8
|
import { NO_WS_CAPTURE, captureWebSocket } from './ws-capture.js';
|
|
@@ -38,6 +38,10 @@ export function createInterceptor(options) {
|
|
|
38
38
|
const completed = new Promise((resolve) => {
|
|
39
39
|
settle = resolve;
|
|
40
40
|
});
|
|
41
|
+
const ended = options.inFlight?.begin(id);
|
|
42
|
+
if (ended !== undefined) {
|
|
43
|
+
void completed.then(ended);
|
|
44
|
+
}
|
|
41
45
|
/** Logs the exchange once the request body was read; the duration ends with the response. */
|
|
42
46
|
const record = async (status, headers, requestBodyRead) => {
|
|
43
47
|
const durationMs = Math.round(performance.now() - startedAt);
|
|
@@ -78,10 +82,21 @@ export function createInterceptor(options) {
|
|
|
78
82
|
};
|
|
79
83
|
return { id, requestCapture, responseCapture, completed, settle, record };
|
|
80
84
|
};
|
|
85
|
+
/** The request as it goes to the local service: with capture.logRequestId it names its exchange. */
|
|
86
|
+
function withExchangeId(request, id) {
|
|
87
|
+
if (options.logRequestId !== true) {
|
|
88
|
+
return request;
|
|
89
|
+
}
|
|
90
|
+
const name = EXCHANGE_ID_HEADER.toLowerCase();
|
|
91
|
+
// A client cannot choose the id the service logs
|
|
92
|
+
const headers = request.headers.filter(([header]) => header.toLowerCase() !== name);
|
|
93
|
+
headers.push([EXCHANGE_ID_HEADER, id]);
|
|
94
|
+
return { ...request, headers };
|
|
95
|
+
}
|
|
81
96
|
return {
|
|
82
97
|
async forward(request) {
|
|
83
|
-
const { requestCapture, responseCapture, completed, settle, record: recordAfter } = begin(request);
|
|
84
|
-
const sent = sendUpstream(target, request, requestCapture);
|
|
98
|
+
const { id, requestCapture, responseCapture, completed, settle, record: recordAfter } = begin(request);
|
|
99
|
+
const sent = sendUpstream(target, withExchangeId(request, id), requestCapture);
|
|
85
100
|
const record = (status, headers) => recordAfter(status, headers, sent.requestBody.read);
|
|
86
101
|
let upstream;
|
|
87
102
|
try {
|
|
@@ -146,7 +161,7 @@ export function createInterceptor(options) {
|
|
|
146
161
|
const record = (status, headers) => recordAfter(status, headers, Promise.resolve(true));
|
|
147
162
|
let answer;
|
|
148
163
|
try {
|
|
149
|
-
answer = await openUpstreamSocket(target, request);
|
|
164
|
+
answer = await openUpstreamSocket(target, withExchangeId(request, id));
|
|
150
165
|
}
|
|
151
166
|
catch (error) {
|
|
152
167
|
if (request.signal?.aborted === true) {
|