@aidenhq/sdk 0.5.12 → 0.5.14
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.
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/** A state file's name: what the kernel accepts, one path segment. */
|
|
2
|
+
export declare const STATE_NAME: RegExp;
|
|
3
|
+
/** The most one state file holds (the kernel's cap). */
|
|
4
|
+
export declare const STATE_MAX: number;
|
|
5
|
+
export interface StateOptions {
|
|
6
|
+
/** The kernel's backplane (default AIDENHQ_KERNEL_URL, else the host bridge address every module reaches). */
|
|
7
|
+
url?: string;
|
|
8
|
+
/** The module's service key (default: the file named by MCP_SERVICE_TOKEN_FILE, else MCP_TOKEN_FILE). */
|
|
9
|
+
key?: string | (() => string);
|
|
10
|
+
fetchImpl?: typeof fetch;
|
|
11
|
+
}
|
|
12
|
+
/** Whether this process can reach the kernel to save state (a key file is set); a dev or test run without one writes its own folder. */
|
|
13
|
+
export declare const canSaveState: (o?: StateOptions) => boolean;
|
|
14
|
+
/** Saves `text` as the module's state file `name`; resolves with the committed version once the kernel has persisted it, rejects otherwise. */
|
|
15
|
+
export declare function saveState(name: string, text: string, o?: StateOptions): Promise<string>;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
// A connector's runtime state (its sign-in's token pair, a registered client, the accounts it keeps): saved through the kernel,
|
|
2
|
+
// which writes the module's own secret store and answers only once the write is persisted (POST /v1/state/<name>,
|
|
3
|
+
// kernel/src/state-secrets.ts). The caller is known by its connection's service key, so it can write nothing but its own
|
|
4
|
+
// module's state; the target is never part of the request. Node built-ins only.
|
|
5
|
+
import { readFileSync } from "node:fs";
|
|
6
|
+
/** A state file's name: what the kernel accepts, one path segment. */
|
|
7
|
+
export const STATE_NAME = /^[\w.-]{1,100}$/;
|
|
8
|
+
/** The most one state file holds (the kernel's cap). */
|
|
9
|
+
export const STATE_MAX = 64 * 1024;
|
|
10
|
+
const keyFromEnv = () => {
|
|
11
|
+
const file = process.env.MCP_SERVICE_TOKEN_FILE ?? process.env.MCP_TOKEN_FILE;
|
|
12
|
+
return file ? readFileSync(file, "utf8").trim() : "";
|
|
13
|
+
};
|
|
14
|
+
/** Whether this process can reach the kernel to save state (a key file is set); a dev or test run without one writes its own folder. */
|
|
15
|
+
export const canSaveState = (o = {}) => Boolean(o.key ?? process.env.MCP_SERVICE_TOKEN_FILE ?? process.env.MCP_TOKEN_FILE);
|
|
16
|
+
/** Saves `text` as the module's state file `name`; resolves with the committed version once the kernel has persisted it, rejects otherwise. */
|
|
17
|
+
export async function saveState(name, text, o = {}) {
|
|
18
|
+
if (!STATE_NAME.test(name))
|
|
19
|
+
throw new Error(`state file name ${JSON.stringify(name)} is not one path segment (letters, digits, . _ -)`);
|
|
20
|
+
if (Buffer.byteLength(text) > STATE_MAX)
|
|
21
|
+
throw new Error(`state file ${name} is over ${STATE_MAX} bytes`);
|
|
22
|
+
const key = typeof o.key === "function" ? o.key() : (o.key ?? keyFromEnv());
|
|
23
|
+
if (!key)
|
|
24
|
+
throw new Error("no service key to save state with (MCP_SERVICE_TOKEN_FILE)");
|
|
25
|
+
const url = (o.url ?? process.env.AIDENHQ_KERNEL_URL ?? "http://10.231.0.1:3130").replace(/\/$/, "");
|
|
26
|
+
const r = await (o.fetchImpl ?? fetch)(`${url}/v1/state/${name}`, { method: "POST", headers: { authorization: `Bearer ${key}`, "content-type": "text/plain; charset=utf-8" }, body: text, signal: AbortSignal.timeout(30_000) });
|
|
27
|
+
const body = (await r.json().catch(() => ({})));
|
|
28
|
+
if (!r.ok || typeof body.version !== "string")
|
|
29
|
+
throw new Error(`the kernel did not save ${name} (${r.status}${body.error?.message ? `: ${String(body.error.message).slice(0, 200)}` : ""})`);
|
|
30
|
+
return body.version;
|
|
31
|
+
}
|
package/dist/src/types.d.ts
CHANGED
|
@@ -100,12 +100,15 @@ export type EngineEvent = {
|
|
|
100
100
|
t: "final";
|
|
101
101
|
text: string;
|
|
102
102
|
vendor_error?: string;
|
|
103
|
+
start?: number;
|
|
104
|
+
reads?: string[];
|
|
103
105
|
} | {
|
|
104
106
|
t: "reply";
|
|
105
107
|
id: string;
|
|
106
108
|
text: string;
|
|
107
109
|
start?: number;
|
|
108
110
|
end?: number;
|
|
111
|
+
reads?: string[];
|
|
109
112
|
} | {
|
|
110
113
|
t: "turn";
|
|
111
114
|
busy: boolean;
|
|
@@ -113,5 +116,6 @@ export type EngineEvent = {
|
|
|
113
116
|
export interface InboxLine {
|
|
114
117
|
t: "comment";
|
|
115
118
|
from: string;
|
|
116
|
-
text: string;
|
|
119
|
+
text: string; /** The owner's comment this line stands for (the kernel's `saw`): a turn reply names the ones it read. */
|
|
120
|
+
comment?: string;
|
|
117
121
|
}
|
|
@@ -108,7 +108,8 @@ export interface OAuthOptions {
|
|
|
108
108
|
/** Where a sign-in's token is kept. `read` returns the same object until `write` replaces it, so a refresh can tell a new sign-in saved meanwhile; a throwing `write` saves nothing. */
|
|
109
109
|
export interface TokenStore {
|
|
110
110
|
read(): StoredToken | undefined;
|
|
111
|
-
|
|
111
|
+
/** Resolves once the token is persisted: nothing is kept in memory or used before that. */
|
|
112
|
+
write(t: StoredToken): void | Promise<void>;
|
|
112
113
|
}
|
|
113
114
|
/** token.json (0600), written atomically; a file removed from outside reads as signed out. */
|
|
114
115
|
export declare function fileStore(path: string): TokenStore;
|
package/dist/src/vendor/oauth.js
CHANGED
|
@@ -8,7 +8,8 @@ import { lookup } from "node:dns";
|
|
|
8
8
|
import { request } from "node:https";
|
|
9
9
|
import { isIP } from "node:net";
|
|
10
10
|
import { chmodSync, existsSync, readFileSync, renameSync, writeFileSync } from "node:fs";
|
|
11
|
-
import { dirname, join } from "node:path";
|
|
11
|
+
import { basename, dirname, join } from "node:path";
|
|
12
|
+
import { canSaveState, saveState } from "../state-secrets.js";
|
|
12
13
|
export class AuthError extends Error {
|
|
13
14
|
}
|
|
14
15
|
export class SignInError extends Error {
|
|
@@ -17,6 +18,15 @@ export function staticAuth(headers = {}) {
|
|
|
17
18
|
const since = new Date().toISOString();
|
|
18
19
|
return { headers: async () => headers, refresh: async () => { }, status: () => ({ state: "none", since }), mark: () => { }, signedIn: () => true, signInCount: () => 0 };
|
|
19
20
|
}
|
|
21
|
+
/** Writes `text` as the state file at `path`: through the kernel (awaited, into the module's own secret store) when this service can reach it, else beside the reads (a dev or test run). */
|
|
22
|
+
async function persist(path, text) {
|
|
23
|
+
if (canSaveState())
|
|
24
|
+
return void (await saveState(basename(path), text));
|
|
25
|
+
const tmp = join(dirname(path), `.${basename(path)}.${process.pid}.tmp`);
|
|
26
|
+
writeFileSync(tmp, text, { mode: 0o600 });
|
|
27
|
+
chmodSync(tmp, 0o600);
|
|
28
|
+
renameSync(tmp, path);
|
|
29
|
+
}
|
|
20
30
|
/** token.json (0600), written atomically; a file removed from outside reads as signed out. */
|
|
21
31
|
export function fileStore(path) {
|
|
22
32
|
let token;
|
|
@@ -26,11 +36,8 @@ export function fileStore(path) {
|
|
|
26
36
|
return (token = undefined);
|
|
27
37
|
return (token ??= JSON.parse(readFileSync(path, "utf8")));
|
|
28
38
|
},
|
|
29
|
-
write(t) {
|
|
30
|
-
|
|
31
|
-
writeFileSync(tmp, JSON.stringify(t, null, 2), { mode: 0o600 });
|
|
32
|
-
chmodSync(tmp, 0o600);
|
|
33
|
-
renameSync(tmp, path);
|
|
39
|
+
async write(t) {
|
|
40
|
+
await persist(path, JSON.stringify(t, null, 2));
|
|
34
41
|
token = t;
|
|
35
42
|
},
|
|
36
43
|
};
|
|
@@ -185,7 +192,15 @@ export function oauth(o) {
|
|
|
185
192
|
throw new AuthError(unbound ? changed() : `${o.name} isn't connected yet (Connect on the board)`);
|
|
186
193
|
return t;
|
|
187
194
|
}
|
|
188
|
-
|
|
195
|
+
// Commits one at a time: a refresh's save checks, inside its turn, that the token it refreshed is still the current one, so a
|
|
196
|
+
// reconnect that committed while it waited is never overwritten by the older grant.
|
|
197
|
+
let commits = Promise.resolve();
|
|
198
|
+
const save = (t, still) => {
|
|
199
|
+
const p = commits.then(async () => { if (still && !still())
|
|
200
|
+
return false; await store.write(t); return true; });
|
|
201
|
+
commits = p.catch(() => undefined);
|
|
202
|
+
return p;
|
|
203
|
+
};
|
|
189
204
|
const token = () => current();
|
|
190
205
|
async function doRefresh() {
|
|
191
206
|
const t = load();
|
|
@@ -233,7 +248,8 @@ export function oauth(o) {
|
|
|
233
248
|
}
|
|
234
249
|
// The answer updates the token, never what it is bound to or where the next refresh goes.
|
|
235
250
|
const fresh = Object.fromEntries(Object.entries(next).filter(([k]) => !BOUND.includes(k)));
|
|
236
|
-
save({ ...t, ...fresh, refresh_token: next.refresh_token ?? t.refresh_token, scope: next.scope ?? t.scope })
|
|
251
|
+
if (!(await save({ ...t, ...fresh, refresh_token: next.refresh_token ?? t.refresh_token, scope: next.scope ?? t.scope }, () => token() === t)))
|
|
252
|
+
return token();
|
|
237
253
|
return token();
|
|
238
254
|
}
|
|
239
255
|
const refresh = () => (refreshing ??= doRefresh().finally(() => (refreshing = undefined)));
|
|
@@ -316,8 +332,7 @@ export function oauth(o) {
|
|
|
316
332
|
if (!r.ok || typeof r.body.client_id !== "string")
|
|
317
333
|
throw new SignInError(`${o.name} refused to register AidenHQ (${String(r.body.error_description ?? r.body.error ?? r.status).slice(0, 200)})`);
|
|
318
334
|
const secret = typeof r.body.client_secret === "string" ? r.body.client_secret : undefined;
|
|
319
|
-
|
|
320
|
-
chmodSync(clientPath, 0o600);
|
|
335
|
+
await persist(clientPath, JSON.stringify({ client_id: r.body.client_id, ...(secret ? { client_secret: secret } : {}), redirect_uri: redirectUri, issuer: ep.issuer }, null, 2));
|
|
321
336
|
return { id: r.body.client_id, ...(secret ? { secret } : {}) };
|
|
322
337
|
}
|
|
323
338
|
const recommended = (supported) => {
|
|
@@ -382,7 +397,7 @@ export function oauth(o) {
|
|
|
382
397
|
catch (err) {
|
|
383
398
|
throw new SignInError(`${o.name} refused the sign-in: ${String(err.message).slice(0, 200)}`);
|
|
384
399
|
}
|
|
385
|
-
save({ ...strip(parsed), client_id: p.client.id, ...(p.client.secret ? { client_secret: p.client.secret } : {}), token_endpoint: p.ep.token, issuer: p.ep.issuer, ...binding() });
|
|
400
|
+
await save({ ...strip(parsed), client_id: p.client.id, ...(p.client.secret ? { client_secret: p.client.secret } : {}), token_endpoint: p.ep.token, issuer: p.ep.issuer, ...binding() });
|
|
386
401
|
signedInCount += 1;
|
|
387
402
|
mark("ok");
|
|
388
403
|
},
|
|
@@ -441,7 +456,9 @@ export function oauth(o) {
|
|
|
441
456
|
const parsed = parse(p.body);
|
|
442
457
|
if (run !== me || me.state.state !== "pending")
|
|
443
458
|
return; // cancelled while the answer was on its way: nothing saved
|
|
444
|
-
save
|
|
459
|
+
// A cancel that lands while the save waits its turn wins: the token never takes effect.
|
|
460
|
+
if (!(await save({ ...strip(parsed), client_id: d.client_id, token_endpoint: tokenUrl, issuer, ...binding() }, () => run === me && me.state.state === "pending")))
|
|
461
|
+
return;
|
|
445
462
|
signedInCount += 1;
|
|
446
463
|
mark("ok");
|
|
447
464
|
end({ state: "ok" });
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aidenhq/sdk",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.14",
|
|
4
4
|
"description": "The shared code of AidenHQ modules: contract types, engine IO, the caller check and fence, write executors, the reviewer side, the judges' models and call, the source writer and the vendor MCP pass-through.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -62,7 +62,8 @@
|
|
|
62
62
|
"./teammate-name": "./dist/src/teammate-name.js",
|
|
63
63
|
"./face-kit": "./dist/src/face-kit.js",
|
|
64
64
|
"./requested-parent": "./dist/src/requested-parent.js",
|
|
65
|
-
"./vendor/needs": "./dist/src/vendor/needs.js"
|
|
65
|
+
"./vendor/needs": "./dist/src/vendor/needs.js",
|
|
66
|
+
"./state-secrets": "./dist/src/state-secrets.js"
|
|
66
67
|
},
|
|
67
68
|
"files": ["dist", "LICENSE"],
|
|
68
69
|
"peerDependencies": {
|