@senpi/runtime 1.0.95-dev.runtime-phase-2.20260506193916 → 1.0.95-dev.runtime-phase-2-api.20260507133904
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/README.md +13 -0
- package/dist/index.d.mts +38 -0
- package/dist/index.d.mts.map +1 -0
- package/dist/index.mjs +38 -0
- package/dist/index.mjs.map +1 -0
- package/dist/{index.d.ts → plugin.d.ts} +8 -1
- package/dist/plugin.d.ts.map +1 -0
- package/dist/{index.js → plugin.js} +152 -32
- package/dist/plugin.js.map +1 -0
- package/dist/runtime/__tests__/helpers/runtime-gateway-harness.d.ts +3 -3
- package/dist/runtime/audit-query.types.d.ts +113 -0
- package/dist/runtime/audit-query.types.d.ts.map +1 -0
- package/dist/runtime/audit-query.types.js +35 -0
- package/dist/runtime/audit-query.types.js.map +1 -0
- package/dist/runtime/auto-update/coordinator.d.ts +12 -8
- package/dist/runtime/auto-update/coordinator.d.ts.map +1 -1
- package/dist/runtime/auto-update/coordinator.js +18 -17
- package/dist/runtime/auto-update/coordinator.js.map +1 -1
- package/dist/runtime/auto-update/gateway-restart.d.ts +9 -2
- package/dist/runtime/auto-update/gateway-restart.d.ts.map +1 -1
- package/dist/runtime/auto-update/gateway-restart.js +23 -13
- package/dist/runtime/auto-update/gateway-restart.js.map +1 -1
- package/dist/runtime/create-actions.d.ts.map +1 -1
- package/dist/runtime/create-actions.js +13 -2
- package/dist/runtime/create-actions.js.map +1 -1
- package/dist/runtime/run.d.ts +52 -1
- package/dist/runtime/run.d.ts.map +1 -1
- package/dist/runtime/run.js +9 -0
- package/dist/runtime/run.js.map +1 -1
- package/dist/runtime/runtime.d.ts +56 -0
- package/dist/runtime/runtime.d.ts.map +1 -1
- package/dist/runtime/runtime.js +169 -1
- package/dist/runtime/runtime.js.map +1 -1
- package/dist/runtime-api/config.d.ts +47 -0
- package/dist/runtime-api/config.d.ts.map +1 -0
- package/dist/runtime-api/config.js +74 -0
- package/dist/runtime-api/config.js.map +1 -0
- package/dist/runtime-api/handlers/audit.d.ts +78 -0
- package/dist/runtime-api/handlers/audit.d.ts.map +1 -0
- package/dist/runtime-api/handlers/audit.js +65 -0
- package/dist/runtime-api/handlers/audit.js.map +1 -0
- package/dist/runtime-api/handlers/group-by-scanner.d.ts +43 -0
- package/dist/runtime-api/handlers/group-by-scanner.d.ts.map +1 -0
- package/dist/runtime-api/handlers/group-by-scanner.js +43 -0
- package/dist/runtime-api/handlers/group-by-scanner.js.map +1 -0
- package/dist/runtime-api/handlers/ingest-signals.d.ts +54 -0
- package/dist/runtime-api/handlers/ingest-signals.d.ts.map +1 -0
- package/dist/runtime-api/handlers/ingest-signals.js +141 -0
- package/dist/runtime-api/handlers/ingest-signals.js.map +1 -0
- package/dist/runtime-api/handlers/lookup.d.ts +36 -0
- package/dist/runtime-api/handlers/lookup.d.ts.map +1 -0
- package/dist/runtime-api/handlers/lookup.js +31 -0
- package/dist/runtime-api/handlers/lookup.js.map +1 -0
- package/dist/runtime-api/handlers/types.d.ts +85 -0
- package/dist/runtime-api/handlers/types.d.ts.map +1 -0
- package/dist/runtime-api/handlers/types.js +8 -0
- package/dist/runtime-api/handlers/types.js.map +1 -0
- package/dist/runtime-api/lifecycle.d.ts +98 -0
- package/dist/runtime-api/lifecycle.d.ts.map +1 -0
- package/dist/runtime-api/lifecycle.js +137 -0
- package/dist/runtime-api/lifecycle.js.map +1 -0
- package/dist/runtime-api/readiness.d.ts +37 -0
- package/dist/runtime-api/readiness.d.ts.map +1 -0
- package/dist/runtime-api/readiness.js +35 -0
- package/dist/runtime-api/readiness.js.map +1 -0
- package/dist/runtime-api/routes/audit.d.ts +40 -0
- package/dist/runtime-api/routes/audit.d.ts.map +1 -0
- package/dist/runtime-api/routes/audit.js +90 -0
- package/dist/runtime-api/routes/audit.js.map +1 -0
- package/dist/runtime-api/routes/audit.schema.d.ts +75 -0
- package/dist/runtime-api/routes/audit.schema.d.ts.map +1 -0
- package/dist/runtime-api/routes/audit.schema.js +78 -0
- package/dist/runtime-api/routes/audit.schema.js.map +1 -0
- package/dist/runtime-api/routes/health.d.ts +53 -0
- package/dist/runtime-api/routes/health.d.ts.map +1 -0
- package/dist/runtime-api/routes/health.js +48 -0
- package/dist/runtime-api/routes/health.js.map +1 -0
- package/dist/runtime-api/routes/signals.d.ts +50 -0
- package/dist/runtime-api/routes/signals.d.ts.map +1 -0
- package/dist/runtime-api/routes/signals.js +90 -0
- package/dist/runtime-api/routes/signals.js.map +1 -0
- package/dist/runtime-api/routes/signals.schema.d.ts +94 -0
- package/dist/runtime-api/routes/signals.schema.d.ts.map +1 -0
- package/dist/runtime-api/routes/signals.schema.js +58 -0
- package/dist/runtime-api/routes/signals.schema.js.map +1 -0
- package/dist/runtime-api/server.d.ts +79 -0
- package/dist/runtime-api/server.d.ts.map +1 -0
- package/dist/runtime-api/server.js +109 -0
- package/dist/runtime-api/server.js.map +1 -0
- package/dist/senpi/client.d.ts +92 -2
- package/dist/senpi/client.d.ts.map +1 -1
- package/dist/senpi/client.js +181 -8
- package/dist/senpi/client.js.map +1 -1
- package/openclaw.plugin.json +24 -0
- package/package.json +8 -4
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js.map +0 -1
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Start/stop helpers for the runtime HTTP API.
|
|
3
|
+
*
|
|
4
|
+
* This module owns the Fastify server lifecycle: binding the port,
|
|
5
|
+
* flipping readiness states, and draining in-flight requests on shutdown
|
|
6
|
+
* with a hard 15s timeout so teardown always proceeds.
|
|
7
|
+
*
|
|
8
|
+
* The plugin glue layer (S6 in `src/index.ts`) calls {@link startApi} during
|
|
9
|
+
* `api.lifecycle.start` and {@link ApiHandle.close} during `api.lifecycle.stop`.
|
|
10
|
+
* This module does NOT modify `src/index.ts`.
|
|
11
|
+
*/
|
|
12
|
+
import { createApi } from "./server.js";
|
|
13
|
+
/**
|
|
14
|
+
* Hard timeout for Fastify drain.
|
|
15
|
+
*
|
|
16
|
+
* After this many milliseconds, {@link stopApi} logs a warning and proceeds
|
|
17
|
+
* to runtime teardown regardless of in-flight requests. 15s is enough
|
|
18
|
+
* headroom for any Telegram notification or close-resolver tick that may
|
|
19
|
+
* be mid-flight on the underlying runtime.
|
|
20
|
+
*/
|
|
21
|
+
export const DRAIN_HARD_TIMEOUT_MS = 15_000;
|
|
22
|
+
/**
|
|
23
|
+
* Start the HTTP API server.
|
|
24
|
+
*
|
|
25
|
+
* Calls {@link createApi} to build the Fastify instance, binds it to
|
|
26
|
+
* `deps.apiConfig.host:deps.apiConfig.port`, and returns an {@link ApiHandle}.
|
|
27
|
+
*
|
|
28
|
+
* The server is NOT yet marked ready after this call — `/health` will return
|
|
29
|
+
* 503 until the caller invokes `handle.ready()`. Call `ready()` immediately
|
|
30
|
+
* after the runtimes are confirmed up, which in the plugin lifecycle is right
|
|
31
|
+
* after `startApi` returns.
|
|
32
|
+
*
|
|
33
|
+
* A boot-time warning is logged when `deps.apiConfig.host !== "127.0.0.1"` so
|
|
34
|
+
* any operator who opts into Docker port mapping sees it in the logs.
|
|
35
|
+
*
|
|
36
|
+
* @param deps Same dependencies passed to {@link createApi}: runtime handles,
|
|
37
|
+
* validated API config, logger, and plugin version string.
|
|
38
|
+
* @returns An {@link ApiHandle} whose `ready()` and `close()` methods drive
|
|
39
|
+
* the lifecycle.
|
|
40
|
+
* @throws Re-throws any `fastify.listen()` error (e.g. `EADDRINUSE`) so
|
|
41
|
+
* the plugin lifecycle layer can perform its rollback and fail the
|
|
42
|
+
* openclaw start cleanly.
|
|
43
|
+
*/
|
|
44
|
+
export async function startApi(deps) {
|
|
45
|
+
const artifacts = createApi(deps);
|
|
46
|
+
await artifacts.fastify.listen({
|
|
47
|
+
host: deps.apiConfig.host,
|
|
48
|
+
port: deps.apiConfig.port,
|
|
49
|
+
});
|
|
50
|
+
deps.logger.info("[runtime-api] api server started", {
|
|
51
|
+
host: deps.apiConfig.host,
|
|
52
|
+
port: deps.apiConfig.port,
|
|
53
|
+
});
|
|
54
|
+
// Friction is a feature: anyone who flips host to 0.0.0.0 sees this in logs.
|
|
55
|
+
if (deps.apiConfig.host !== "127.0.0.1") {
|
|
56
|
+
deps.logger.warn("[runtime-api] api.host is bound to \"" +
|
|
57
|
+
deps.apiConfig.host +
|
|
58
|
+
"\". " +
|
|
59
|
+
"The API is reachable from anything on this container's Docker network. " +
|
|
60
|
+
"If you set this for Postman testing, make sure your docker-compose port mapping " +
|
|
61
|
+
"uses the \"127.0.0.1:\" prefix so the API is not exposed beyond your laptop's loopback. " +
|
|
62
|
+
"See docs/runtime-docs/runtime-api.md \"Postman testing recipe\".");
|
|
63
|
+
}
|
|
64
|
+
return {
|
|
65
|
+
fastify: artifacts.fastify,
|
|
66
|
+
ready: () => artifacts.readiness.setOk(),
|
|
67
|
+
close: () => stopApi(artifacts, deps.logger),
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Drain in-flight requests and close the HTTP server.
|
|
72
|
+
*
|
|
73
|
+
* Sets readiness to `"draining"` immediately so `/health` stops returning 200,
|
|
74
|
+
* then races `fastify.close()` against {@link DRAIN_HARD_TIMEOUT_MS}. If the
|
|
75
|
+
* hard timeout fires first, a warning is logged and a best-effort force-close
|
|
76
|
+
* is issued (fire-and-forget). The function always resolves — it never rejects.
|
|
77
|
+
*
|
|
78
|
+
* @param artifacts The {@link ApiArtifacts} returned by `createApi`.
|
|
79
|
+
* @param logger Logger for the startup/drain messages.
|
|
80
|
+
*/
|
|
81
|
+
async function stopApi(artifacts, logger) {
|
|
82
|
+
artifacts.readiness.setDraining();
|
|
83
|
+
// Capture the close promise once. Two reasons:
|
|
84
|
+
// 1. We need to mute its rejection unconditionally — when the drain
|
|
85
|
+
// timeout wins the race, the close promise is still in flight and
|
|
86
|
+
// may reject later, producing an unhandled rejection (the second
|
|
87
|
+
// .close() call in the timeout branch was a *new* promise, not a
|
|
88
|
+
// handler for the original).
|
|
89
|
+
// 2. We need a stable reference to race against the timeout below.
|
|
90
|
+
const closePromise = artifacts.fastify.close();
|
|
91
|
+
// Pre-attach a no-op catch so a late rejection on the original promise
|
|
92
|
+
// is always considered handled. The catch returns a new derived promise
|
|
93
|
+
// that we ignore — this does NOT prevent Promise.race below from seeing
|
|
94
|
+
// the rejection, since handlers on the same promise are independent.
|
|
95
|
+
closePromise.catch(() => { });
|
|
96
|
+
// Capture the timeout handle so we can clear it on the happy path.
|
|
97
|
+
// Otherwise, when fastify.close() wins the race, the still-pending
|
|
98
|
+
// setTimeout fires DRAIN_HARD_TIMEOUT_MS later and rejects an unwatched
|
|
99
|
+
// promise — an unhandled rejection on every clean shutdown.
|
|
100
|
+
let timeoutHandle;
|
|
101
|
+
try {
|
|
102
|
+
await Promise.race([
|
|
103
|
+
closePromise,
|
|
104
|
+
new Promise((_resolve, reject) => {
|
|
105
|
+
timeoutHandle = setTimeout(() => reject(new Error("DRAIN_TIMEOUT")), DRAIN_HARD_TIMEOUT_MS);
|
|
106
|
+
}),
|
|
107
|
+
]);
|
|
108
|
+
logger.info("[runtime-api] api server stopped");
|
|
109
|
+
}
|
|
110
|
+
catch (err) {
|
|
111
|
+
if (err.message === "DRAIN_TIMEOUT") {
|
|
112
|
+
logger.warn("[runtime-api] api server drain exceeded hard timeout; proceeding to teardown", { timeoutMs: DRAIN_HARD_TIMEOUT_MS });
|
|
113
|
+
// The original closePromise is still in flight; any future rejection
|
|
114
|
+
// is already muted by the .catch() above. Caller proceeds to
|
|
115
|
+
// runtime teardown.
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
// Unexpected close error — log and swallow so teardown sequence continues.
|
|
119
|
+
logger.error("[runtime-api] unexpected error during api server close", {
|
|
120
|
+
err,
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
finally {
|
|
124
|
+
// Always clear so the timeout cannot fire after stopApi returns.
|
|
125
|
+
if (timeoutHandle !== undefined) {
|
|
126
|
+
clearTimeout(timeoutHandle);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Exported for tests (S8) so they can assert drain timeout behavior without
|
|
132
|
+
* going through the full `startApi` path.
|
|
133
|
+
*
|
|
134
|
+
* @internal Not part of the public API surface — import only in `__tests__/`.
|
|
135
|
+
*/
|
|
136
|
+
export const __test_only = { stopApi };
|
|
137
|
+
//# sourceMappingURL=lifecycle.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"lifecycle.js","sourceRoot":"","sources":["../../src/runtime-api/lifecycle.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAGH,OAAO,EAAE,SAAS,EAAyC,MAAM,aAAa,CAAC;AAG/E;;;;;;;GAOG;AACH,MAAM,CAAC,MAAM,qBAAqB,GAAG,MAAM,CAAC;AAgC5C;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,MAAM,CAAC,KAAK,UAAU,QAAQ,CAAC,IAAmB;IAChD,MAAM,SAAS,GAAiB,SAAS,CAAC,IAAI,CAAC,CAAC;IAEhD,MAAM,SAAS,CAAC,OAAO,CAAC,MAAM,CAAC;QAC7B,IAAI,EAAE,IAAI,CAAC,SAAS,CAAC,IAAI;QACzB,IAAI,EAAE,IAAI,CAAC,SAAS,CAAC,IAAI;KAC1B,CAAC,CAAC;IAEH,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,kCAAkC,EAAE;QACnD,IAAI,EAAE,IAAI,CAAC,SAAS,CAAC,IAAI;QACzB,IAAI,EAAE,IAAI,CAAC,SAAS,CAAC,IAAI;KAC1B,CAAC,CAAC;IAEH,6EAA6E;IAC7E,IAAI,IAAI,CAAC,SAAS,CAAC,IAAI,KAAK,WAAW,EAAE,CAAC;QACxC,IAAI,CAAC,MAAM,CAAC,IAAI,CACd,uCAAuC;YACrC,IAAI,CAAC,SAAS,CAAC,IAAI;YACnB,MAAM;YACN,yEAAyE;YACzE,kFAAkF;YAClF,0FAA0F;YAC1F,kEAAkE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E,SAAS,EAAE,qBAAqB,EAAE,CACrC,CAAC;YACF,qEAAqE;YACrE,6DAA6D;YAC7D,oBAAoB;YACpB,OAAO;QACT,CAAC;QACD,2EAA2E;QAC3E,MAAM,CAAC,KAAK,CAAC,wDAAwD,EAAE;YACrE,GAAG;SACJ,CAAC,CAAC;IACL,CAAC;YAAS,CAAC;QACT,iEAAiE;QACjE,IAAI,aAAa,KAAK,SAAS,EAAE,CAAC;YAChC,YAAY,CAAC,aAAa,CAAC,CAAC;QAC9B,CAAC;IACH,CAAC;AACH,CAAC;AAED;;;;;GAKG;AACH,MAAM,CAAC,MAAM,WAAW,GAAG,EAAE,OAAO,EAAE,CAAC"}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Three-state readiness contract for `GET /health`.
|
|
3
|
+
*
|
|
4
|
+
* S5 (lifecycle layer) wires the lifecycle to flip these states:
|
|
5
|
+
* - `"starting"` — initial state; server is binding or warming up.
|
|
6
|
+
* - `"ok"` — server is bound and ready to handle traffic.
|
|
7
|
+
* - `"draining"` — graceful-shutdown in progress; stop routing new requests.
|
|
8
|
+
*
|
|
9
|
+
* `GET /health` returns `200` for `"ok"` and `503` for the other two states
|
|
10
|
+
* so load balancers stop routing before the process exits.
|
|
11
|
+
*/
|
|
12
|
+
/** Three valid readiness states. */
|
|
13
|
+
export type ReadinessState = "starting" | "ok" | "draining";
|
|
14
|
+
/**
|
|
15
|
+
* Read-only interface consumed by `GET /health` and any other component that
|
|
16
|
+
* needs to observe readiness without being able to mutate it.
|
|
17
|
+
*/
|
|
18
|
+
export interface Readiness {
|
|
19
|
+
/** Return the current readiness state synchronously. */
|
|
20
|
+
state(): ReadinessState;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Create a mutable readiness object.
|
|
24
|
+
*
|
|
25
|
+
* The returned object satisfies both the read-only {@link Readiness} interface
|
|
26
|
+
* and exposes `setOk()` / `setDraining()` mutators for the lifecycle layer
|
|
27
|
+
* (S5) to call at the appropriate moments.
|
|
28
|
+
*
|
|
29
|
+
* Initial state is `"starting"`.
|
|
30
|
+
*
|
|
31
|
+
* @returns A combined readiness + mutator object.
|
|
32
|
+
*/
|
|
33
|
+
export declare function createReadiness(): Readiness & {
|
|
34
|
+
setOk(): void;
|
|
35
|
+
setDraining(): void;
|
|
36
|
+
};
|
|
37
|
+
//# sourceMappingURL=readiness.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"readiness.d.ts","sourceRoot":"","sources":["../../src/runtime-api/readiness.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAEH,oCAAoC;AACpC,MAAM,MAAM,cAAc,GAAG,UAAU,GAAG,IAAI,GAAG,UAAU,CAAC;AAE5D;;;GAGG;AACH,MAAM,WAAW,SAAS;IACxB,wDAAwD;IACxD,KAAK,IAAI,cAAc,CAAC;CACzB;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,eAAe,IAAI,SAAS,GAAG;IAC7C,KAAK,IAAI,IAAI,CAAC;IACd,WAAW,IAAI,IAAI,CAAC;CACrB,CAWA"}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Three-state readiness contract for `GET /health`.
|
|
3
|
+
*
|
|
4
|
+
* S5 (lifecycle layer) wires the lifecycle to flip these states:
|
|
5
|
+
* - `"starting"` — initial state; server is binding or warming up.
|
|
6
|
+
* - `"ok"` — server is bound and ready to handle traffic.
|
|
7
|
+
* - `"draining"` — graceful-shutdown in progress; stop routing new requests.
|
|
8
|
+
*
|
|
9
|
+
* `GET /health` returns `200` for `"ok"` and `503` for the other two states
|
|
10
|
+
* so load balancers stop routing before the process exits.
|
|
11
|
+
*/
|
|
12
|
+
/**
|
|
13
|
+
* Create a mutable readiness object.
|
|
14
|
+
*
|
|
15
|
+
* The returned object satisfies both the read-only {@link Readiness} interface
|
|
16
|
+
* and exposes `setOk()` / `setDraining()` mutators for the lifecycle layer
|
|
17
|
+
* (S5) to call at the appropriate moments.
|
|
18
|
+
*
|
|
19
|
+
* Initial state is `"starting"`.
|
|
20
|
+
*
|
|
21
|
+
* @returns A combined readiness + mutator object.
|
|
22
|
+
*/
|
|
23
|
+
export function createReadiness() {
|
|
24
|
+
let s = "starting";
|
|
25
|
+
return {
|
|
26
|
+
state: () => s,
|
|
27
|
+
setOk: () => {
|
|
28
|
+
s = "ok";
|
|
29
|
+
},
|
|
30
|
+
setDraining: () => {
|
|
31
|
+
s = "draining";
|
|
32
|
+
},
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
//# sourceMappingURL=readiness.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"readiness.js","sourceRoot":"","sources":["../../src/runtime-api/readiness.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAcH;;;;;;;;;;GAUG;AACH,MAAM,UAAU,eAAe;IAI7B,IAAI,CAAC,GAAmB,UAAU,CAAC;IACnC,OAAO;QACL,KAAK,EAAE,GAAG,EAAE,CAAC,CAAC;QACd,KAAK,EAAE,GAAG,EAAE;YACV,CAAC,GAAG,IAAI,CAAC;QACX,CAAC;QACD,WAAW,EAAE,GAAG,EAAE;YAChB,CAAC,GAAG,UAAU,CAAC;QACjB,CAAC;KACF,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Fastify route: `GET /audit`.
|
|
3
|
+
*
|
|
4
|
+
* Proxies a `audit_query` MCP call through the runtime handle matching the
|
|
5
|
+
* requested wallet address. AbortSignal is wired to the HTTP request lifetime
|
|
6
|
+
* so MCP cancels in-flight work when the client disconnects.
|
|
7
|
+
*
|
|
8
|
+
* Business logic lives entirely in `handlers/audit.ts`. This file contains
|
|
9
|
+
* only Fastify wiring and query-string coercion.
|
|
10
|
+
*/
|
|
11
|
+
import type { FastifyInstance } from "fastify";
|
|
12
|
+
import type { RuntimeHandle } from "../../runtime/run.js";
|
|
13
|
+
import type { Logger } from "../../utils/logger.js";
|
|
14
|
+
/**
|
|
15
|
+
* Dependencies injected into the `/audit` route at registration time.
|
|
16
|
+
*/
|
|
17
|
+
export interface AuditRouteDeps {
|
|
18
|
+
/** Live runtime handles maintained by the plugin, keyed by lowercased address. */
|
|
19
|
+
runtimeHandles: Map<string, RuntimeHandle>;
|
|
20
|
+
/** Logger instance for unexpected errors. */
|
|
21
|
+
logger: Logger;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Register `GET /audit` on the given Fastify instance.
|
|
25
|
+
*
|
|
26
|
+
* Coerces string query-string values to their typed counterparts before
|
|
27
|
+
* forwarding to `auditHandler`. The `success` field is `"true" | "false"` from
|
|
28
|
+
* the schema; converted to `boolean`. Integer fields are parsed with
|
|
29
|
+
* `parseInt`. Missing optional fields are left as `undefined`.
|
|
30
|
+
*
|
|
31
|
+
* HTTP status mapping from `AuditQueryError.code`:
|
|
32
|
+
* - `NOT_FOUND` → 404
|
|
33
|
+
* - `ABORTED` → 504 (gateway timeout — client disconnected)
|
|
34
|
+
* - `UPSTREAM` → 502 (bad gateway — MCP error)
|
|
35
|
+
*
|
|
36
|
+
* @param fastify Fastify instance to register the route on.
|
|
37
|
+
* @param deps Route dependencies injected at plugin start.
|
|
38
|
+
*/
|
|
39
|
+
export declare function registerAuditRoute(fastify: FastifyInstance, deps: AuditRouteDeps): void;
|
|
40
|
+
//# sourceMappingURL=audit.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"audit.d.ts","sourceRoot":"","sources":["../../../src/runtime-api/routes/audit.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAEH,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,SAAS,CAAC;AAK/C,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,sBAAsB,CAAC;AAC1D,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,uBAAuB,CAAC;AAEpD;;GAEG;AACH,MAAM,WAAW,cAAc;IAC7B,kFAAkF;IAClF,cAAc,EAAE,GAAG,CAAC,MAAM,EAAE,aAAa,CAAC,CAAC;IAC3C,6CAA6C;IAC7C,MAAM,EAAE,MAAM,CAAC;CAChB;AAED;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,kBAAkB,CAChC,OAAO,EAAE,eAAe,EACxB,IAAI,EAAE,cAAc,GACnB,IAAI,CAoEN"}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Fastify route: `GET /audit`.
|
|
3
|
+
*
|
|
4
|
+
* Proxies a `audit_query` MCP call through the runtime handle matching the
|
|
5
|
+
* requested wallet address. AbortSignal is wired to the HTTP request lifetime
|
|
6
|
+
* so MCP cancels in-flight work when the client disconnects.
|
|
7
|
+
*
|
|
8
|
+
* Business logic lives entirely in `handlers/audit.ts`. This file contains
|
|
9
|
+
* only Fastify wiring and query-string coercion.
|
|
10
|
+
*/
|
|
11
|
+
import { AuditQuery } from "./audit.schema.js";
|
|
12
|
+
import { auditHandler } from "../handlers/audit.js";
|
|
13
|
+
/**
|
|
14
|
+
* Register `GET /audit` on the given Fastify instance.
|
|
15
|
+
*
|
|
16
|
+
* Coerces string query-string values to their typed counterparts before
|
|
17
|
+
* forwarding to `auditHandler`. The `success` field is `"true" | "false"` from
|
|
18
|
+
* the schema; converted to `boolean`. Integer fields are parsed with
|
|
19
|
+
* `parseInt`. Missing optional fields are left as `undefined`.
|
|
20
|
+
*
|
|
21
|
+
* HTTP status mapping from `AuditQueryError.code`:
|
|
22
|
+
* - `NOT_FOUND` → 404
|
|
23
|
+
* - `ABORTED` → 504 (gateway timeout — client disconnected)
|
|
24
|
+
* - `UPSTREAM` → 502 (bad gateway — MCP error)
|
|
25
|
+
*
|
|
26
|
+
* @param fastify Fastify instance to register the route on.
|
|
27
|
+
* @param deps Route dependencies injected at plugin start.
|
|
28
|
+
*/
|
|
29
|
+
export function registerAuditRoute(fastify, deps) {
|
|
30
|
+
fastify.get("/audit", { schema: { querystring: AuditQuery } }, async (req, reply) => {
|
|
31
|
+
const q = req.query;
|
|
32
|
+
// Coerce string query-string values to typed handler args.
|
|
33
|
+
const args = {
|
|
34
|
+
...(q.tool_name !== undefined && { tool_name: q.tool_name }),
|
|
35
|
+
...(q.action_type !== undefined && { action_type: q.action_type }),
|
|
36
|
+
...(q.success !== undefined && { success: q.success === "true" }),
|
|
37
|
+
...(q.start_time !== undefined && { start_time: q.start_time }),
|
|
38
|
+
...(q.end_time !== undefined && { end_time: q.end_time }),
|
|
39
|
+
...(q.min_duration_ms !== undefined && {
|
|
40
|
+
min_duration_ms: parseInt(q.min_duration_ms, 10),
|
|
41
|
+
}),
|
|
42
|
+
...(q.max_duration_ms !== undefined && {
|
|
43
|
+
max_duration_ms: parseInt(q.max_duration_ms, 10),
|
|
44
|
+
}),
|
|
45
|
+
...(q.limit !== undefined
|
|
46
|
+
? { limit: parseInt(q.limit, 10) }
|
|
47
|
+
: { limit: 100 }),
|
|
48
|
+
...(q.offset !== undefined
|
|
49
|
+
? { offset: parseInt(q.offset, 10) }
|
|
50
|
+
: { offset: 0 }),
|
|
51
|
+
...(q.cursor_requested_at !== undefined && {
|
|
52
|
+
cursor_requested_at: q.cursor_requested_at,
|
|
53
|
+
}),
|
|
54
|
+
...(q.cursor_id !== undefined && { cursor_id: q.cursor_id }),
|
|
55
|
+
};
|
|
56
|
+
// Wire AbortSignal to the request close event so MCP cancellation works
|
|
57
|
+
// when the HTTP client disconnects before the MCP response arrives.
|
|
58
|
+
const ac = new AbortController();
|
|
59
|
+
req.raw.on("close", () => {
|
|
60
|
+
if (req.raw.destroyed)
|
|
61
|
+
ac.abort();
|
|
62
|
+
});
|
|
63
|
+
const result = await auditHandler(q.address, args, deps, ac.signal);
|
|
64
|
+
if (result.success) {
|
|
65
|
+
// Spread MCP fields first, then explicit `address` / `strategyId` —
|
|
66
|
+
// ours take precedence if MCP ever introduces a colliding field name.
|
|
67
|
+
// Without this order, a future MCP schema bump could silently shadow
|
|
68
|
+
// our routing identifiers.
|
|
69
|
+
return reply.send({
|
|
70
|
+
success: true,
|
|
71
|
+
data: {
|
|
72
|
+
...result.result,
|
|
73
|
+
address: result.address,
|
|
74
|
+
strategyId: result.strategyId,
|
|
75
|
+
},
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
const statusMap = {
|
|
79
|
+
NOT_FOUND: 404,
|
|
80
|
+
ABORTED: 504,
|
|
81
|
+
UPSTREAM: 502,
|
|
82
|
+
};
|
|
83
|
+
const status = statusMap[result.code] ?? 502;
|
|
84
|
+
return reply.status(status).send({
|
|
85
|
+
success: false,
|
|
86
|
+
error: { code: result.code, message: result.message },
|
|
87
|
+
});
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
//# sourceMappingURL=audit.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"audit.js","sourceRoot":"","sources":["../../../src/runtime-api/routes/audit.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAIH,OAAO,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAC;AAC/C,OAAO,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAC;AAepD;;;;;;;;;;;;;;;GAeG;AACH,MAAM,UAAU,kBAAkB,CAChC,OAAwB,EACxB,IAAoB;IAEpB,OAAO,CAAC,GAAG,CACT,QAAQ,EACR,EAAE,MAAM,EAAE,EAAE,WAAW,EAAE,UAAU,EAAE,EAAE,EACvC,KAAK,EAAE,GAAG,EAAE,KAAK,EAAE,EAAE;QACnB,MAAM,CAAC,GAAG,GAAG,CAAC,KAAkC,CAAC;QAEjD,2DAA2D;QAC3D,MAAM,IAAI,GAAmB;YAC3B,GAAG,CAAC,CAAC,CAAC,SAAS,KAAK,SAAS,IAAI,EAAE,SAAS,EAAE,CAAC,CAAC,SAAS,EAAE,CAAC;YAC5D,GAAG,CAAC,CAAC,CAAC,WAAW,KAAK,SAAS,IAAI,EAAE,WAAW,EAAE,CAAC,CAAC,WAAW,EAAE,CAA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|
5
|
+
* (`min_duration_ms`, `max_duration_ms`, `limit`, `offset`) and the `success`
|
|
6
|
+
* boolean are declared as strings here so TypeBox validation passes on the raw
|
|
7
|
+
* HTTP input. The route handler coerces them to the appropriate types before
|
|
8
|
+
* forwarding to `auditHandler`.
|
|
9
|
+
*
|
|
10
|
+
* Fields excluded from the caller-visible surface (per plan Section 6):
|
|
11
|
+
* - `resource_type` — always `"strategy"`, set by `RuntimeHandle.auditQuery`.
|
|
12
|
+
* - `resource_id` — resolved wallet → strategyId, set by the handle.
|
|
13
|
+
* - `user_ids` — server-controlled (the MCP API-key user).
|
|
14
|
+
* - `usernames` — server-controlled (the MCP API-key user).
|
|
15
|
+
*
|
|
16
|
+
* @see senpi-hyperliquid-mcp/src/tools/audit.tools.ts `auditQueryInputSchema`
|
|
17
|
+
* for the upstream MCP Zod schema this mirrors.
|
|
18
|
+
*/
|
|
19
|
+
import { type Static } from "@sinclair/typebox";
|
|
20
|
+
/** TypeBox schema for `GET /audit` query-string parameters. */
|
|
21
|
+
export declare const AuditQuery: import("@sinclair/typebox").TObject<{
|
|
22
|
+
/**
|
|
23
|
+
* Wallet address to scope the audit query.
|
|
24
|
+
* Required — used to look up the matching runtime handle.
|
|
25
|
+
*/
|
|
26
|
+
address: import("@sinclair/typebox").TString;
|
|
27
|
+
/** Filter by exact tool name (e.g. `create_position`). */
|
|
28
|
+
tool_name: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TString>;
|
|
29
|
+
/**
|
|
30
|
+
* Filter by action classification.
|
|
31
|
+
* One of: `read`, `create`, `update`, `delete`.
|
|
32
|
+
*/
|
|
33
|
+
action_type: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TUnion<[import("@sinclair/typebox").TLiteral<"read">, import("@sinclair/typebox").TLiteral<"create">, import("@sinclair/typebox").TLiteral<"update">, import("@sinclair/typebox").TLiteral<"delete">]>>;
|
|
34
|
+
/**
|
|
35
|
+
* Filter by success/failure.
|
|
36
|
+
* Query strings arrive as text — coerce `"true"` / `"false"` in the route.
|
|
37
|
+
*/
|
|
38
|
+
success: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TUnion<[import("@sinclair/typebox").TLiteral<"true">, import("@sinclair/typebox").TLiteral<"false">]>>;
|
|
39
|
+
/** ISO 8601 inclusive lower bound on `requested_at`. */
|
|
40
|
+
start_time: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TString>;
|
|
41
|
+
/** ISO 8601 inclusive upper bound on `requested_at`. */
|
|
42
|
+
end_time: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TString>;
|
|
43
|
+
/**
|
|
44
|
+
* Minimum end-to-end duration in ms.
|
|
45
|
+
* Arrives as a string; pattern restricts to non-negative integers so
|
|
46
|
+
* `parseInt` never produces NaN. Coerced to integer in the route handler.
|
|
47
|
+
*/
|
|
48
|
+
min_duration_ms: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TString>;
|
|
49
|
+
/**
|
|
50
|
+
* Maximum end-to-end duration in ms.
|
|
51
|
+
* Arrives as a string; pattern restricts to non-negative integers so
|
|
52
|
+
* `parseInt` never produces NaN. Coerced to integer in the route handler.
|
|
53
|
+
*/
|
|
54
|
+
max_duration_ms: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TString>;
|
|
55
|
+
/**
|
|
56
|
+
* Page size (1–1000, MCP default 100).
|
|
57
|
+
* Arrives as a string; pattern restricts to non-negative integers.
|
|
58
|
+
* Coerced to integer in the route handler. Default `100` applied on
|
|
59
|
+
* missing value. MCP enforces the 1–1000 range.
|
|
60
|
+
*/
|
|
61
|
+
limit: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TString>;
|
|
62
|
+
/**
|
|
63
|
+
* Offset-based pagination skip count (≥0, MCP default 0).
|
|
64
|
+
* Arrives as a string; pattern restricts to non-negative integers.
|
|
65
|
+
* Coerced to integer in the route handler.
|
|
66
|
+
*/
|
|
67
|
+
offset: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TString>;
|
|
68
|
+
/** Cursor timestamp for keyset pagination (ISO 8601). Must pair with `cursor_id`. */
|
|
69
|
+
cursor_requested_at: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TString>;
|
|
70
|
+
/** Cursor UUID for keyset pagination. Must pair with `cursor_requested_at`. */
|
|
71
|
+
cursor_id: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TString>;
|
|
72
|
+
}>;
|
|
73
|
+
/** TypeScript type for the `GET /audit` query string. */
|
|
74
|
+
export type AuditQueryT = Static<typeof AuditQuery>;
|
|
75
|
+
//# sourceMappingURL=audit.schema.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"audit.schema.d.ts","sourceRoot":"","sources":["../../../src/runtime-api/routes/audit.schema.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AAEH,OAAO,EAAQ,KAAK,MAAM,EAAE,MAAM,mBAAmB,CAAC;AAEtD,+DAA+D;AAC/D,eAAO,MAAM,UAAU;IAEnB;;;OAGG;;IAGH,0DAA0D;;IAG1D;;;OAGG;;IAUH;;;OAGG;;IAKH,wDAAwD;;IAGxD,wDAAwD;;IAGxD;;;;OAIG;;IAGH;;;;OAIG;;IAGH;;;;;OAKG;;IAGH;;;;OAIG;;IAGH,qFAAqF;;IAGrF,+EAA+E;;EAIlF,CAAC;AAEF,yDAAyD;AACzD,MAAM,MAAM,WAAW,GAAG,MAAM,CAAC,OAAO,UAAU,CAAC,CAAC"}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* TypeBox schema for the `GET /audit` query string.
|
|
3
|
+
*
|
|
4
|
+
* All Fastify query-string values arrive as strings. Integer fields
|
|
5
|
+
* (`min_duration_ms`, `max_duration_ms`, `limit`, `offset`) and the `success`
|
|
6
|
+
* boolean are declared as strings here so TypeBox validation passes on the raw
|
|
7
|
+
* HTTP input. The route handler coerces them to the appropriate types before
|
|
8
|
+
* forwarding to `auditHandler`.
|
|
9
|
+
*
|
|
10
|
+
* Fields excluded from the caller-visible surface (per plan Section 6):
|
|
11
|
+
* - `resource_type` — always `"strategy"`, set by `RuntimeHandle.auditQuery`.
|
|
12
|
+
* - `resource_id` — resolved wallet → strategyId, set by the handle.
|
|
13
|
+
* - `user_ids` — server-controlled (the MCP API-key user).
|
|
14
|
+
* - `usernames` — server-controlled (the MCP API-key user).
|
|
15
|
+
*
|
|
16
|
+
* @see senpi-hyperliquid-mcp/src/tools/audit.tools.ts `auditQueryInputSchema`
|
|
17
|
+
* for the upstream MCP Zod schema this mirrors.
|
|
18
|
+
*/
|
|
19
|
+
import { Type } from "@sinclair/typebox";
|
|
20
|
+
/** TypeBox schema for `GET /audit` query-string parameters. */
|
|
21
|
+
export const AuditQuery = Type.Object({
|
|
22
|
+
/**
|
|
23
|
+
* Wallet address to scope the audit query.
|
|
24
|
+
* Required — used to look up the matching runtime handle.
|
|
25
|
+
*/
|
|
26
|
+
address: Type.String({ minLength: 1 }),
|
|
27
|
+
/** Filter by exact tool name (e.g. `create_position`). */
|
|
28
|
+
tool_name: Type.Optional(Type.String()),
|
|
29
|
+
/**
|
|
30
|
+
* Filter by action classification.
|
|
31
|
+
* One of: `read`, `create`, `update`, `delete`.
|
|
32
|
+
*/
|
|
33
|
+
action_type: Type.Optional(Type.Union([
|
|
34
|
+
Type.Literal("read"),
|
|
35
|
+
Type.Literal("create"),
|
|
36
|
+
Type.Literal("update"),
|
|
37
|
+
Type.Literal("delete"),
|
|
38
|
+
])),
|
|
39
|
+
/**
|
|
40
|
+
* Filter by success/failure.
|
|
41
|
+
* Query strings arrive as text — coerce `"true"` / `"false"` in the route.
|
|
42
|
+
*/
|
|
43
|
+
success: Type.Optional(Type.Union([Type.Literal("true"), Type.Literal("false")])),
|
|
44
|
+
/** ISO 8601 inclusive lower bound on `requested_at`. */
|
|
45
|
+
start_time: Type.Optional(Type.String()),
|
|
46
|
+
/** ISO 8601 inclusive upper bound on `requested_at`. */
|
|
47
|
+
end_time: Type.Optional(Type.String()),
|
|
48
|
+
/**
|
|
49
|
+
* Minimum end-to-end duration in ms.
|
|
50
|
+
* Arrives as a string; pattern restricts to non-negative integers so
|
|
51
|
+
* `parseInt` never produces NaN. Coerced to integer in the route handler.
|
|
52
|
+
*/
|
|
53
|
+
min_duration_ms: Type.Optional(Type.String({ pattern: "^\\d+$" })),
|
|
54
|
+
/**
|
|
55
|
+
* Maximum end-to-end duration in ms.
|
|
56
|
+
* Arrives as a string; pattern restricts to non-negative integers so
|
|
57
|
+
* `parseInt` never produces NaN. Coerced to integer in the route handler.
|
|
58
|
+
*/
|
|
59
|
+
max_duration_ms: Type.Optional(Type.String({ pattern: "^\\d+$" })),
|
|
60
|
+
/**
|
|
61
|
+
* Page size (1–1000, MCP default 100).
|
|
62
|
+
* Arrives as a string; pattern restricts to non-negative integers.
|
|
63
|
+
* Coerced to integer in the route handler. Default `100` applied on
|
|
64
|
+
* missing value. MCP enforces the 1–1000 range.
|
|
65
|
+
*/
|
|
66
|
+
limit: Type.Optional(Type.String({ pattern: "^\\d+$" })),
|
|
67
|
+
/**
|
|
68
|
+
* Offset-based pagination skip count (≥0, MCP default 0).
|
|
69
|
+
* Arrives as a string; pattern restricts to non-negative integers.
|
|
70
|
+
* Coerced to integer in the route handler.
|
|
71
|
+
*/
|
|
72
|
+
offset: Type.Optional(Type.String({ pattern: "^\\d+$" })),
|
|
73
|
+
/** Cursor timestamp for keyset pagination (ISO 8601). Must pair with `cursor_id`. */
|
|
74
|
+
cursor_requested_at: Type.Optional(Type.String()),
|
|
75
|
+
/** Cursor UUID for keyset pagination. Must pair with `cursor_requested_at`. */
|
|
76
|
+
cursor_id: Type.Optional(Type.String()),
|
|
77
|
+
}, { additionalProperties: false });
|
|
78
|
+
//# sourceMappingURL=audit.schema.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"audit.schema.js","sourceRoot":"","sources":["../../../src/runtime-api/routes/audit.schema.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;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AAE,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,MAAM,CAAC,EAAE,OAAO,EAAE,QAAQ,EAAE,CAAC,CAAC;IAExD;;;;OAIG;IACH,MAAM,EAAE,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,MAAM,CAAC,EAAE,OAAO,EAAE,QAAQ,EAAE,CAAC,CAAC;IAEzD,qFAAqF;IACrF,mBAAmB,EAAE,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,MAAM,EAAE,CAAC;IAEjD,+EAA+E;IAC/E,SAAS,EAAE,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,MAAM,EAAE,CAAC;CACxC,EACD,EAAE,oBAAoB,EAAE,KAAK,EAAE,CAChC,CAAC"}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Fastify route: `GET /health`.
|
|
3
|
+
*
|
|
4
|
+
* Returns a three-state readiness signal (`starting` | `ok` | `draining`)
|
|
5
|
+
* suitable for load-balancer / Kubernetes liveness and readiness probes.
|
|
6
|
+
* Also exposes per-runtime signal-queue depth for capacity planning.
|
|
7
|
+
*
|
|
8
|
+
* HTTP status:
|
|
9
|
+
* - `200` when `status === "ok"`.
|
|
10
|
+
* - `503` when `status === "starting"` or `"draining"` — load balancer
|
|
11
|
+
* should stop routing new requests but the process is still alive.
|
|
12
|
+
*/
|
|
13
|
+
import type { FastifyInstance } from "fastify";
|
|
14
|
+
import type { Readiness } from "../readiness.js";
|
|
15
|
+
import type { RuntimeHandle } from "../../runtime/run.js";
|
|
16
|
+
/**
|
|
17
|
+
* Dependencies injected into the `/health` route at registration time.
|
|
18
|
+
*/
|
|
19
|
+
export interface HealthRouteDeps {
|
|
20
|
+
/** Live runtime handles maintained by the plugin, keyed by lowercased address. */
|
|
21
|
+
runtimeHandles: Map<string, RuntimeHandle>;
|
|
22
|
+
/** Subset of `ApiConfig` needed for the response body. */
|
|
23
|
+
apiConfig: {
|
|
24
|
+
host: string;
|
|
25
|
+
port: number;
|
|
26
|
+
};
|
|
27
|
+
/**
|
|
28
|
+
* Three-state readiness flag.
|
|
29
|
+
* S5 (lifecycle) flips it to `"ok"` after the server binds and to
|
|
30
|
+
* `"draining"` during graceful shutdown.
|
|
31
|
+
*/
|
|
32
|
+
readiness: Readiness;
|
|
33
|
+
/**
|
|
34
|
+
* Plugin version string (read from `openclaw.plugin.json` at boot).
|
|
35
|
+
* Included in the response so callers can correlate logs with deployments.
|
|
36
|
+
*/
|
|
37
|
+
version: string;
|
|
38
|
+
/** Unix epoch ms timestamp recorded when `createApi()` was called. */
|
|
39
|
+
bootedAt: number;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Register `GET /health` on the given Fastify instance.
|
|
43
|
+
*
|
|
44
|
+
* Response is never cached (`Cache-Control: no-store`). The shape is stable
|
|
45
|
+
* across minor versions — callers may rely on `status`, `runtimes.count`, and
|
|
46
|
+
* `runtimes.items[].signalQueueDepth` being present whenever the server is
|
|
47
|
+
* reachable.
|
|
48
|
+
*
|
|
49
|
+
* @param fastify Fastify instance to register the route on.
|
|
50
|
+
* @param deps Route dependencies injected at plugin start.
|
|
51
|
+
*/
|
|
52
|
+
export declare function registerHealthRoute(fastify: FastifyInstance, deps: HealthRouteDeps): void;
|
|
53
|
+
//# sourceMappingURL=health.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"health.d.ts","sourceRoot":"","sources":["../../../src/runtime-api/routes/health.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAEH,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,SAAS,CAAC;AAC/C,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AACjD,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,sBAAsB,CAAC;AAE1D;;GAEG;AACH,MAAM,WAAW,eAAe;IAC9B,kFAAkF;IAClF,cAAc,EAAE,GAAG,CAAC,MAAM,EAAE,aAAa,CAAC,CAAC;IAC3C,0DAA0D;IAC1D,SAAS,EAAE;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,IAAI,EAAE,MAAM,CAAA;KAAE,CAAC;IAC1C;;;;OAIG;IACH,SAAS,EAAE,SAAS,CAAC;IACrB;;;OAGG;IACH,OAAO,EAAE,MAAM,CAAC;IAChB,sEAAsE;IACtE,QAAQ,EAAE,MAAM,CAAC;CAClB;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,mBAAmB,CACjC,OAAO,EAAE,eAAe,EACxB,IAAI,EAAE,eAAe,GACpB,IAAI,CAyBN"}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Fastify route: `GET /health`.
|
|
3
|
+
*
|
|
4
|
+
* Returns a three-state readiness signal (`starting` | `ok` | `draining`)
|
|
5
|
+
* suitable for load-balancer / Kubernetes liveness and readiness probes.
|
|
6
|
+
* Also exposes per-runtime signal-queue depth for capacity planning.
|
|
7
|
+
*
|
|
8
|
+
* HTTP status:
|
|
9
|
+
* - `200` when `status === "ok"`.
|
|
10
|
+
* - `503` when `status === "starting"` or `"draining"` — load balancer
|
|
11
|
+
* should stop routing new requests but the process is still alive.
|
|
12
|
+
*/
|
|
13
|
+
/**
|
|
14
|
+
* Register `GET /health` on the given Fastify instance.
|
|
15
|
+
*
|
|
16
|
+
* Response is never cached (`Cache-Control: no-store`). The shape is stable
|
|
17
|
+
* across minor versions — callers may rely on `status`, `runtimes.count`, and
|
|
18
|
+
* `runtimes.items[].signalQueueDepth` being present whenever the server is
|
|
19
|
+
* reachable.
|
|
20
|
+
*
|
|
21
|
+
* @param fastify Fastify instance to register the route on.
|
|
22
|
+
* @param deps Route dependencies injected at plugin start.
|
|
23
|
+
*/
|
|
24
|
+
export function registerHealthRoute(fastify, deps) {
|
|
25
|
+
fastify.get("/health", async (_req, reply) => {
|
|
26
|
+
const status = deps.readiness.state();
|
|
27
|
+
const httpStatus = status === "ok" ? 200 : 503;
|
|
28
|
+
const items = Array.from(deps.runtimeHandles, ([_id, handle]) => ({
|
|
29
|
+
address: handle.wallet.toLowerCase(),
|
|
30
|
+
signalQueueDepth: handle.getSignalQueueDepth(),
|
|
31
|
+
}));
|
|
32
|
+
return reply
|
|
33
|
+
.status(httpStatus)
|
|
34
|
+
.header("Cache-Control", "no-store")
|
|
35
|
+
.send({
|
|
36
|
+
status,
|
|
37
|
+
version: deps.version,
|
|
38
|
+
uptime_ms: Date.now() - deps.bootedAt,
|
|
39
|
+
runtimes: { count: items.length, items },
|
|
40
|
+
api: { host: deps.apiConfig.host, port: deps.apiConfig.port },
|
|
41
|
+
build: {
|
|
42
|
+
commit: process.env.SENPI_GIT_COMMIT ?? null,
|
|
43
|
+
node: process.version,
|
|
44
|
+
},
|
|
45
|
+
});
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
//# sourceMappingURL=health.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"health.js","sourceRoot":"","sources":["../../../src/runtime-api/routes/health.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AA6BH;;;;;;;;;;GAUG;AACH,MAAM,UAAU,mBAAmB,CACjC,OAAwB,EACxB,IAAqB;IAErB,OAAO,CAAC,GAAG,CAAC,SAAS,EAAE,KAAK,EAAE,IAAI,EAAE,KAAK,EAAE,EAAE;QAC3C,MAAM,MAAM,GAAG,IAAI,CAAC,SAAS,CAAC,KAAK,EAAE,CAAC;QACtC,MAAM,UAAU,GAAG,MAAM,KAAK,IAAI,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,GAAG,CAAC;QAE/C,MAAM,KAAK,GAAG,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,cAAc,EAAE,CAAC,CAAC,GAAG,EAAE,MAAM,CAAC,EAAE,EAAE,CAAC,CAAC;YAChE,OAAO,EAAE,MAAM,CAAC,MAAM,CAAC,WAAW,EAAE;YACpC,gBAAgB,EAAE,MAAM,CAAC,mBAAmB,EAAE;SAC/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