@hyperwindmill/caretaker-cli 0.33.0 → 0.35.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/dist/cli/index.d.ts.map +1 -1
- package/dist/cli/index.js +25 -1
- package/dist/cli/orchestrator_demo.d.ts +39 -0
- package/dist/cli/orchestrator_demo.d.ts.map +1 -0
- package/dist/cli/orchestrator_demo.js +98 -0
- package/dist/cli/web/api_access.d.ts +38 -0
- package/dist/cli/web/api_access.d.ts.map +1 -0
- package/dist/cli/web/api_access.js +125 -0
- package/dist/cli/web/openapi.d.ts +12 -0
- package/dist/cli/web/openapi.d.ts.map +1 -0
- package/dist/cli/web/openapi.js +526 -0
- package/dist/cli/web/orchestrator.d.ts +51 -0
- package/dist/cli/web/orchestrator.d.ts.map +1 -0
- package/dist/cli/web/orchestrator.js +596 -0
- package/dist/cli/web/orchestrator_core.d.ts +83 -0
- package/dist/cli/web/orchestrator_core.d.ts.map +1 -0
- package/dist/cli/web/orchestrator_core.js +219 -0
- package/dist/cli/web/save_config.d.ts +8 -0
- package/dist/cli/web/save_config.d.ts.map +1 -0
- package/dist/cli/web/save_config.js +9 -0
- package/dist/cli/web/scheduler/task_review.d.ts +8 -0
- package/dist/cli/web/scheduler/task_review.d.ts.map +1 -1
- package/dist/cli/web/scheduler/task_review.js +17 -1
- package/dist/cli/web/scheduler/task_strategy.d.ts.map +1 -1
- package/dist/cli/web/scheduler/task_strategy.js +62 -16
- package/dist/cli/web/server.d.ts +7 -1
- package/dist/cli/web/server.d.ts.map +1 -1
- package/dist/cli/web/server.js +44 -25
- package/dist/harness/tools/builtin/task_tools.d.ts.map +1 -1
- package/dist/harness/tools/builtin/task_tools.js +5 -17
- package/dist/lib/task_git.d.ts +3 -0
- package/dist/lib/task_git.d.ts.map +1 -1
- package/dist/lib/task_git.js +29 -0
- package/dist/store/db.d.ts +12 -0
- package/dist/store/db.d.ts.map +1 -1
- package/dist/store/db.js +37 -0
- package/dist/store/json.d.ts +29 -0
- package/dist/store/json.d.ts.map +1 -1
- package/dist/store/json.js +54 -0
- package/dist/store/orchestrator.d.ts +50 -0
- package/dist/store/orchestrator.d.ts.map +1 -0
- package/dist/store/orchestrator.js +127 -0
- package/dist/webview/standalone.css +159 -1
- package/dist/webview/standalone.css.map +2 -2
- package/dist/webview/standalone.js +933 -495
- package/dist/webview/standalone.js.map +4 -4
- package/package.json +3 -3
package/dist/cli/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/cli/index.ts"],"names":[],"mappings":"AAgBA,wBAAsB,MAAM,CAAC,IAAI,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC,IAAI,CAAC,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/cli/index.ts"],"names":[],"mappings":"AAgBA,wBAAsB,MAAM,CAAC,IAAI,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC,IAAI,CAAC,CA8G1D"}
|
package/dist/cli/index.js
CHANGED
|
@@ -58,9 +58,33 @@ export async function runCli(argv) {
|
|
|
58
58
|
.description('Launch the Caretaker web server local-first GUI.')
|
|
59
59
|
.option('-p, --port <number>', 'Port to listen on', '3000')
|
|
60
60
|
.option('-h, --host <string>', 'Host to bind to', '127.0.0.1')
|
|
61
|
+
.option('--api-port <number>', 'Port of the token-authenticated API listener (overrides the one saved in the settings)')
|
|
62
|
+
.option('--api-host <string>', 'Host the API listener binds to (default: same as --host)')
|
|
61
63
|
.action(async (opts) => {
|
|
62
64
|
const { startServer } = await import('./web/server.js');
|
|
63
|
-
await
|
|
65
|
+
const { parsePort } = await import('./web/api_access.js');
|
|
66
|
+
const apiPort = opts.apiPort === undefined ? undefined : parsePort(opts.apiPort);
|
|
67
|
+
if (apiPort === null) {
|
|
68
|
+
console.error(`Invalid --api-port "${opts.apiPort}".`);
|
|
69
|
+
process.exit(1);
|
|
70
|
+
}
|
|
71
|
+
await startServer(parseInt(opts.port, 10), opts.host, { port: apiPort, host: opts.apiHost });
|
|
72
|
+
});
|
|
73
|
+
program
|
|
74
|
+
.command('orchestrator-demo')
|
|
75
|
+
.description('Launch an in-memory demo orchestrator implementing protocol v1.')
|
|
76
|
+
.requiredOption('--token <token>', 'Bearer token required for all orchestrator routes')
|
|
77
|
+
.option('-p, --port <number>', 'Port to listen on', '4000')
|
|
78
|
+
.option('-h, --host <string>', 'Host to bind to', '127.0.0.1')
|
|
79
|
+
.option('-n, --name <name>', 'Display name returned by /health and /register')
|
|
80
|
+
.action(async (opts) => {
|
|
81
|
+
const { startDemoOrchestrator } = await import('./orchestrator_demo.js');
|
|
82
|
+
startDemoOrchestrator({
|
|
83
|
+
port: parseInt(opts.port, 10),
|
|
84
|
+
host: opts.host,
|
|
85
|
+
token: opts.token,
|
|
86
|
+
name: opts.name,
|
|
87
|
+
});
|
|
64
88
|
});
|
|
65
89
|
program
|
|
66
90
|
.command('mcp')
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { Hono } from 'hono';
|
|
2
|
+
export interface DemoInstance {
|
|
3
|
+
instanceId: string;
|
|
4
|
+
name: string;
|
|
5
|
+
version: string;
|
|
6
|
+
lastSeenAt: string;
|
|
7
|
+
}
|
|
8
|
+
export interface DemoQueuedMessage {
|
|
9
|
+
id: string;
|
|
10
|
+
method: string;
|
|
11
|
+
path: string;
|
|
12
|
+
body?: unknown;
|
|
13
|
+
}
|
|
14
|
+
export interface DemoResult {
|
|
15
|
+
id: string;
|
|
16
|
+
status: number;
|
|
17
|
+
body: unknown;
|
|
18
|
+
}
|
|
19
|
+
export interface DemoOrchestrator {
|
|
20
|
+
app: Hono;
|
|
21
|
+
enqueue(instanceId: string, msg: {
|
|
22
|
+
method: string;
|
|
23
|
+
path: string;
|
|
24
|
+
body?: unknown;
|
|
25
|
+
}): string;
|
|
26
|
+
results(instanceId: string): DemoResult[];
|
|
27
|
+
instances(): DemoInstance[];
|
|
28
|
+
}
|
|
29
|
+
export declare function createDemoOrchestrator(opts: {
|
|
30
|
+
token: string;
|
|
31
|
+
name?: string;
|
|
32
|
+
}): DemoOrchestrator;
|
|
33
|
+
export declare function startDemoOrchestrator(opts: {
|
|
34
|
+
port: number;
|
|
35
|
+
host: string;
|
|
36
|
+
token: string;
|
|
37
|
+
name?: string;
|
|
38
|
+
}): void;
|
|
39
|
+
//# sourceMappingURL=orchestrator_demo.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"orchestrator_demo.d.ts","sourceRoot":"","sources":["../../src/cli/orchestrator_demo.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,IAAI,EAAE,MAAM,MAAM,CAAC;AAI5B,MAAM,WAAW,YAAY;IAC3B,UAAU,EAAE,MAAM,CAAC;IACnB,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,MAAM,CAAC;IAChB,UAAU,EAAE,MAAM,CAAC;CACpB;AAED,MAAM,WAAW,iBAAiB;IAChC,EAAE,EAAE,MAAM,CAAC;IACX,MAAM,EAAE,MAAM,CAAC;IACf,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,CAAC,EAAE,OAAO,CAAC;CAChB;AAED,MAAM,WAAW,UAAU;IACzB,EAAE,EAAE,MAAM,CAAC;IACX,MAAM,EAAE,MAAM,CAAC;IACf,IAAI,EAAE,OAAO,CAAC;CACf;AAED,MAAM,WAAW,gBAAgB;IAC/B,GAAG,EAAE,IAAI,CAAC;IACV,OAAO,CAAC,UAAU,EAAE,MAAM,EAAE,GAAG,EAAE;QAAE,MAAM,EAAE,MAAM,CAAC;QAAC,IAAI,EAAE,MAAM,CAAC;QAAC,IAAI,CAAC,EAAE,OAAO,CAAA;KAAE,GAAG,MAAM,CAAC;IAC3F,OAAO,CAAC,UAAU,EAAE,MAAM,GAAG,UAAU,EAAE,CAAC;IAC1C,SAAS,IAAI,YAAY,EAAE,CAAC;CAC7B;AAED,wBAAgB,sBAAsB,CAAC,IAAI,EAAE;IAAE,KAAK,EAAE,MAAM,CAAC;IAAC,IAAI,CAAC,EAAE,MAAM,CAAA;CAAE,GAAG,gBAAgB,CAyG/F;AAED,wBAAgB,qBAAqB,CAAC,IAAI,EAAE;IAC1C,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;IACb,KAAK,EAAE,MAAM,CAAC;IACd,IAAI,CAAC,EAAE,MAAM,CAAC;CACf,GAAG,IAAI,CAYP"}
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { randomUUID } from 'node:crypto';
|
|
2
|
+
import { serve } from '@hono/node-server';
|
|
3
|
+
import { Hono } from 'hono';
|
|
4
|
+
import { bearerToken } from './web/api_access.js';
|
|
5
|
+
import { PROTOCOL_VERSION } from './web/orchestrator_core.js';
|
|
6
|
+
export function createDemoOrchestrator(opts) {
|
|
7
|
+
const displayName = opts.name || 'Demo Orchestrator';
|
|
8
|
+
const instancesMap = new Map();
|
|
9
|
+
const queues = new Map();
|
|
10
|
+
const resultsMap = new Map();
|
|
11
|
+
function enqueue(instanceId, msg) {
|
|
12
|
+
const id = randomUUID();
|
|
13
|
+
const list = queues.get(instanceId) ?? [];
|
|
14
|
+
list.push({
|
|
15
|
+
id,
|
|
16
|
+
method: msg.method,
|
|
17
|
+
path: msg.path,
|
|
18
|
+
...(msg.body !== undefined ? { body: msg.body } : {}),
|
|
19
|
+
});
|
|
20
|
+
queues.set(instanceId, list);
|
|
21
|
+
return id;
|
|
22
|
+
}
|
|
23
|
+
function results(instanceId) {
|
|
24
|
+
return Array.from(resultsMap.get(instanceId)?.values() ?? []);
|
|
25
|
+
}
|
|
26
|
+
function instances() {
|
|
27
|
+
return Array.from(instancesMap.values());
|
|
28
|
+
}
|
|
29
|
+
const app = new Hono();
|
|
30
|
+
app.use('*', async (c, next) => {
|
|
31
|
+
const tok = bearerToken(c.req.header('authorization'));
|
|
32
|
+
if (!tok || tok !== opts.token) {
|
|
33
|
+
return c.json({ error: 'Unauthorized' }, 401);
|
|
34
|
+
}
|
|
35
|
+
return next();
|
|
36
|
+
});
|
|
37
|
+
app.get('/health', (c) => {
|
|
38
|
+
return c.json({ ok: true, protocol: PROTOCOL_VERSION, name: displayName });
|
|
39
|
+
});
|
|
40
|
+
app.post('/register', async (c) => {
|
|
41
|
+
const body = (await c.req.json());
|
|
42
|
+
if (!body.instanceId || typeof body.instanceId !== 'string') {
|
|
43
|
+
return c.json({ error: 'instanceId is required' }, 400);
|
|
44
|
+
}
|
|
45
|
+
instancesMap.set(body.instanceId, {
|
|
46
|
+
instanceId: body.instanceId,
|
|
47
|
+
name: typeof body.name === 'string' ? body.name : '',
|
|
48
|
+
version: typeof body.version === 'string' ? body.version : '',
|
|
49
|
+
lastSeenAt: new Date().toISOString(),
|
|
50
|
+
});
|
|
51
|
+
return c.json({ ok: true, name: displayName });
|
|
52
|
+
});
|
|
53
|
+
app.post('/poll', async (c) => {
|
|
54
|
+
const body = (await c.req.json());
|
|
55
|
+
const instanceId = body.instanceId;
|
|
56
|
+
if (!instanceId || !instancesMap.has(instanceId)) {
|
|
57
|
+
return c.json({ error: 'Unknown instanceId' }, 409);
|
|
58
|
+
}
|
|
59
|
+
const inst = instancesMap.get(instanceId);
|
|
60
|
+
inst.lastSeenAt = new Date().toISOString();
|
|
61
|
+
let instResults = resultsMap.get(instanceId);
|
|
62
|
+
if (!instResults) {
|
|
63
|
+
instResults = new Map();
|
|
64
|
+
resultsMap.set(instanceId, instResults);
|
|
65
|
+
}
|
|
66
|
+
for (const r of body.results ?? []) {
|
|
67
|
+
if (r && typeof r.id === 'string' && !instResults.has(r.id)) {
|
|
68
|
+
instResults.set(r.id, { id: r.id, status: r.status, body: r.body ?? null });
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
const pending = (queues.get(instanceId) ?? []).filter((m) => !instResults.has(m.id));
|
|
72
|
+
queues.set(instanceId, pending);
|
|
73
|
+
return c.json({ messages: pending });
|
|
74
|
+
});
|
|
75
|
+
app.get('/instances', (c) => {
|
|
76
|
+
return c.json({ instances: instances() });
|
|
77
|
+
});
|
|
78
|
+
app.post('/instances/:id/messages', async (c) => {
|
|
79
|
+
const body = (await c.req.json());
|
|
80
|
+
const id = enqueue(c.req.param('id'), body);
|
|
81
|
+
return c.json({ id });
|
|
82
|
+
});
|
|
83
|
+
app.get('/instances/:id/results', (c) => {
|
|
84
|
+
return c.json({ results: results(c.req.param('id')) });
|
|
85
|
+
});
|
|
86
|
+
return { app, enqueue, results, instances };
|
|
87
|
+
}
|
|
88
|
+
export function startDemoOrchestrator(opts) {
|
|
89
|
+
const demo = createDemoOrchestrator({ token: opts.token, name: opts.name });
|
|
90
|
+
serve({
|
|
91
|
+
fetch: demo.app.fetch,
|
|
92
|
+
port: opts.port,
|
|
93
|
+
hostname: opts.host,
|
|
94
|
+
});
|
|
95
|
+
const baseUrl = `http://${opts.host}:${opts.port}`;
|
|
96
|
+
console.log(`Demo orchestrator listening on ${baseUrl}`);
|
|
97
|
+
console.log(`Enqueue a message: curl -X POST ${baseUrl}/instances/<instanceId>/messages -H "Authorization: Bearer ${opts.token}" -H "Content-Type: application/json" -d '{"method":"GET","path":"/api/projects"}'`);
|
|
98
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import type { Hono } from 'hono';
|
|
2
|
+
/**
|
|
3
|
+
* The authenticated API listener: a second port that serves the web server's
|
|
4
|
+
* REST routes to clients outside the browser, every request carrying
|
|
5
|
+
* `Authorization: Bearer <token>`.
|
|
6
|
+
*
|
|
7
|
+
* Authentication is decided by the port a request arrives on, never by where
|
|
8
|
+
* it comes from: the main port stays as it always was (the GUI, the WebSocket,
|
|
9
|
+
* no auth — loopback or behind an authenticating proxy), and this one always
|
|
10
|
+
* asks for a token, loopback included. That is what keeps it correct behind a
|
|
11
|
+
* reverse proxy or a Docker port mapping, where the peer address says nothing.
|
|
12
|
+
*/
|
|
13
|
+
type Fetch = (req: Request, ...rest: any[]) => Response | Promise<Response>;
|
|
14
|
+
export declare function bearerToken(header: string | null | undefined): string | null;
|
|
15
|
+
/** REST routes only. Left on the main port: the GUI and its WebSocket, the
|
|
16
|
+
* task MCP bridge (its own per-run tokens), and token management itself — a
|
|
17
|
+
* token must not be able to mint or revoke tokens. */
|
|
18
|
+
export declare function isApiPath(pathname: string): boolean;
|
|
19
|
+
/** Wraps the app's fetch for the API port. The token is checked first, so an
|
|
20
|
+
* unauthenticated caller learns nothing about which paths exist — apart from
|
|
21
|
+
* the two that describe the API (OPENAPI_PUBLIC_PATHS). */
|
|
22
|
+
export declare function guardApiFetch(appFetch: Fetch, verify?: (token: string) => Promise<boolean>): Fetch;
|
|
23
|
+
export declare function parsePort(value: unknown): number | null;
|
|
24
|
+
export interface ApiListenerOptions {
|
|
25
|
+
/** The main web port — the API listener refuses to share it. */
|
|
26
|
+
webPort: number;
|
|
27
|
+
host: string;
|
|
28
|
+
/** `--api-port`: wins over the port saved in api.json. */
|
|
29
|
+
portOverride?: number;
|
|
30
|
+
}
|
|
31
|
+
/** (Re)starts the listener on the configured port, or stops it when none is
|
|
32
|
+
* set. Never throws: a port already in use is reported in the status, and the
|
|
33
|
+
* web server keeps running. */
|
|
34
|
+
export declare function applyApiListener(app: Hono, opts: ApiListenerOptions): Promise<void>;
|
|
35
|
+
/** Token management, on the main port only (see isApiPath). */
|
|
36
|
+
export declare function registerApiAccess(app: Hono, opts: ApiListenerOptions): void;
|
|
37
|
+
export {};
|
|
38
|
+
//# sourceMappingURL=api_access.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"api_access.d.ts","sourceRoot":"","sources":["../../../src/cli/web/api_access.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,MAAM,CAAC;AAWjC;;;;;;;;;;GAUG;AAEH,KAAK,KAAK,GAAG,CAAC,GAAG,EAAE,OAAO,EAAE,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,QAAQ,GAAG,OAAO,CAAC,QAAQ,CAAC,CAAC;AAE5E,wBAAgB,WAAW,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI,GAAG,SAAS,GAAG,MAAM,GAAG,IAAI,CAG5E;AAED;;uDAEuD;AACvD,wBAAgB,SAAS,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAKnD;AAED;;4DAE4D;AAC5D,wBAAgB,aAAa,CAAC,QAAQ,EAAE,KAAK,EAAE,MAAM,GAAE,CAAC,KAAK,EAAE,MAAM,KAAK,OAAO,CAAC,OAAO,CAAkB,GAAG,KAAK,CAsBlH;AAED,wBAAgB,SAAS,CAAC,KAAK,EAAE,OAAO,GAAG,MAAM,GAAG,IAAI,CAGvD;AAED,MAAM,WAAW,kBAAkB;IACjC,gEAAgE;IAChE,OAAO,EAAE,MAAM,CAAC;IAChB,IAAI,EAAE,MAAM,CAAC;IACb,0DAA0D;IAC1D,YAAY,CAAC,EAAE,MAAM,CAAC;CACvB;AAqBD;;gCAEgC;AAChC,wBAAsB,gBAAgB,CAAC,GAAG,EAAE,IAAI,EAAE,IAAI,EAAE,kBAAkB,GAAG,OAAO,CAAC,IAAI,CAAC,CAyBzF;AAED,+DAA+D;AAC/D,wBAAgB,iBAAiB,CAAC,GAAG,EAAE,IAAI,EAAE,IAAI,EAAE,kBAAkB,GAAG,IAAI,CAuC3E"}
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import { OPENAPI_PUBLIC_PATHS } from './openapi.js';
|
|
2
|
+
import { serve } from '@hono/node-server';
|
|
3
|
+
import { createApiToken, loadApiAccess, revokeApiToken, setApiPort, verifyApiToken, } from '../../store/json.js';
|
|
4
|
+
export function bearerToken(header) {
|
|
5
|
+
const m = /^Bearer\s+(\S+)\s*$/i.exec(header ?? '');
|
|
6
|
+
return m ? m[1] : null;
|
|
7
|
+
}
|
|
8
|
+
/** REST routes only. Left on the main port: the GUI and its WebSocket, the
|
|
9
|
+
* task MCP bridge (its own per-run tokens), and token management itself — a
|
|
10
|
+
* token must not be able to mint or revoke tokens. */
|
|
11
|
+
export function isApiPath(pathname) {
|
|
12
|
+
if (!pathname.startsWith('/api/'))
|
|
13
|
+
return false;
|
|
14
|
+
return !['/api/ws', '/api/mcp', '/api/access', '/api/orchestrator'].some((p) => pathname === p || pathname.startsWith(`${p}/`));
|
|
15
|
+
}
|
|
16
|
+
/** Wraps the app's fetch for the API port. The token is checked first, so an
|
|
17
|
+
* unauthenticated caller learns nothing about which paths exist — apart from
|
|
18
|
+
* the two that describe the API (OPENAPI_PUBLIC_PATHS). */
|
|
19
|
+
export function guardApiFetch(appFetch, verify = verifyApiToken) {
|
|
20
|
+
return async (req, ...rest) => {
|
|
21
|
+
const pathname = new URL(req.url).pathname;
|
|
22
|
+
// The API description carries no data and Swagger UI must load it before
|
|
23
|
+
// anyone can type a token.
|
|
24
|
+
if (req.method === 'GET' && OPENAPI_PUBLIC_PATHS.includes(pathname))
|
|
25
|
+
return appFetch(req, ...rest);
|
|
26
|
+
const token = bearerToken(req.headers.get('authorization'));
|
|
27
|
+
if (!token || !(await verify(token))) {
|
|
28
|
+
// One answer for a missing and a wrong token.
|
|
29
|
+
return new Response(JSON.stringify({ error: 'Unauthorized' }), {
|
|
30
|
+
status: 401,
|
|
31
|
+
headers: { 'content-type': 'application/json', 'www-authenticate': 'Bearer' },
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
if (!isApiPath(pathname)) {
|
|
35
|
+
return new Response(JSON.stringify({ error: 'Not found' }), {
|
|
36
|
+
status: 404,
|
|
37
|
+
headers: { 'content-type': 'application/json' },
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
return appFetch(req, ...rest);
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
export function parsePort(value) {
|
|
44
|
+
const n = typeof value === 'string' && value.trim() ? Number(value) : value;
|
|
45
|
+
return typeof n === 'number' && Number.isInteger(n) && n >= 1 && n <= 65535 ? n : null;
|
|
46
|
+
}
|
|
47
|
+
let server = null;
|
|
48
|
+
let state = null;
|
|
49
|
+
const closeServer = () => new Promise((resolve) => {
|
|
50
|
+
if (!server)
|
|
51
|
+
return resolve();
|
|
52
|
+
const s = server;
|
|
53
|
+
server = null;
|
|
54
|
+
s.close(() => resolve());
|
|
55
|
+
// Keep-alive connections would otherwise hold close() open.
|
|
56
|
+
s.closeAllConnections?.();
|
|
57
|
+
});
|
|
58
|
+
/** (Re)starts the listener on the configured port, or stops it when none is
|
|
59
|
+
* set. Never throws: a port already in use is reported in the status, and the
|
|
60
|
+
* web server keeps running. */
|
|
61
|
+
export async function applyApiListener(app, opts) {
|
|
62
|
+
await closeServer();
|
|
63
|
+
state = null;
|
|
64
|
+
const port = opts.portOverride ?? (await loadApiAccess()).port;
|
|
65
|
+
if (!port)
|
|
66
|
+
return;
|
|
67
|
+
if (port === opts.webPort) {
|
|
68
|
+
state = { port, host: opts.host, error: 'The API port must differ from the web port.' };
|
|
69
|
+
console.error(`[api] ${state.error}`);
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
const current = { port, host: opts.host };
|
|
73
|
+
state = current;
|
|
74
|
+
await new Promise((resolve) => {
|
|
75
|
+
const s = serve({ fetch: guardApiFetch(app.fetch), port, hostname: opts.host }, () => {
|
|
76
|
+
console.log(`🔑 Caretaker API (token required) at http://${opts.host}:${port}`);
|
|
77
|
+
resolve();
|
|
78
|
+
});
|
|
79
|
+
s.on('error', (err) => {
|
|
80
|
+
current.error = err.message;
|
|
81
|
+
if (server === s)
|
|
82
|
+
server = null;
|
|
83
|
+
console.error(`[api] listener on ${opts.host}:${port} failed: ${err.message}`);
|
|
84
|
+
resolve();
|
|
85
|
+
});
|
|
86
|
+
server = s;
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
/** Token management, on the main port only (see isApiPath). */
|
|
90
|
+
export function registerApiAccess(app, opts) {
|
|
91
|
+
const status = async () => {
|
|
92
|
+
const access = await loadApiAccess();
|
|
93
|
+
return {
|
|
94
|
+
port: access.port ?? null,
|
|
95
|
+
portOverride: opts.portOverride ?? null,
|
|
96
|
+
listener: state,
|
|
97
|
+
tokens: access.tokens.map(({ id, name, createdAt }) => ({ id, name, createdAt })),
|
|
98
|
+
};
|
|
99
|
+
};
|
|
100
|
+
app.get('/api/access', async (c) => c.json(await status()));
|
|
101
|
+
app.put('/api/access/port', async (c) => {
|
|
102
|
+
const body = await c.req.json().catch(() => null);
|
|
103
|
+
const raw = body?.port;
|
|
104
|
+
const port = raw === null || raw === '' ? null : parsePort(raw);
|
|
105
|
+
if (raw !== null && raw !== '' && port === null) {
|
|
106
|
+
return c.json({ error: 'port must be an integer between 1 and 65535, or null to turn the API off.' }, 400);
|
|
107
|
+
}
|
|
108
|
+
if (port !== null && port === opts.webPort) {
|
|
109
|
+
return c.json({ error: 'The API port must differ from the web port.' }, 400);
|
|
110
|
+
}
|
|
111
|
+
await setApiPort(port);
|
|
112
|
+
await applyApiListener(app, opts);
|
|
113
|
+
return c.json(await status());
|
|
114
|
+
});
|
|
115
|
+
app.post('/api/access/tokens', async (c) => {
|
|
116
|
+
const body = await c.req.json().catch(() => null);
|
|
117
|
+
const name = typeof body?.name === 'string' ? body.name.slice(0, 80) : '';
|
|
118
|
+
const { token } = await createApiToken(name);
|
|
119
|
+
return c.json({ token, ...(await status()) });
|
|
120
|
+
});
|
|
121
|
+
app.delete('/api/access/tokens/:id', async (c) => {
|
|
122
|
+
await revokeApiToken(c.req.param('id'));
|
|
123
|
+
return c.json(await status());
|
|
124
|
+
});
|
|
125
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { Hono } from 'hono';
|
|
2
|
+
export declare function openApiDocument(version: string): Record<string, unknown>;
|
|
3
|
+
/** Routes that describe the API rather than expose data: served without a
|
|
4
|
+
* token on the API port too, or Swagger UI's Authorize could never load. */
|
|
5
|
+
export declare const OPENAPI_PUBLIC_PATHS: string[];
|
|
6
|
+
export declare function registerOpenApi(app: Hono): void;
|
|
7
|
+
/**
|
|
8
|
+
* True when method + pathname is an operation of `paths` ({param} = exactly
|
|
9
|
+
* one non-empty segment), or GET /api/openapi.json. Pathname only: no query.
|
|
10
|
+
*/
|
|
11
|
+
export declare function isDocumentedRoute(method: string, pathname: string): boolean;
|
|
12
|
+
//# sourceMappingURL=openapi.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"openapi.d.ts","sourceRoot":"","sources":["../../../src/cli/web/openapi.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,MAAM,CAAC;AA2cjC,wBAAgB,eAAe,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAoBxE;AAED;6EAC6E;AAC7E,eAAO,MAAM,oBAAoB,UAAqC,CAAC;AAmCvE,wBAAgB,eAAe,CAAC,GAAG,EAAE,IAAI,GAAG,IAAI,CAI/C;AAED;;;GAGG;AACH,wBAAgB,iBAAiB,CAAC,MAAM,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,OAAO,CA2B3E"}
|