@themoltnet/agent-daemon 0.38.1 → 0.39.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/cli.js +1088 -9
- package/package.json +8 -7
package/dist/cli.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
import { defaultPiDaemonAdapter } from "./pi.js";
|
|
3
3
|
import { assertRuntimeAdapterSupportsProfile } from "./runtime.js";
|
|
4
4
|
import { Type } from "typebox";
|
|
5
|
-
import crypto from "crypto";
|
|
5
|
+
import crypto, { createHash } from "crypto";
|
|
6
6
|
import { Value } from "typebox/value";
|
|
7
7
|
import { dirname, join, resolve } from "node:path";
|
|
8
8
|
import { parseArgs, promisify } from "node:util";
|
|
@@ -11,7 +11,9 @@ import { createPiRetryTriage, findMainWorktree, isResolvedPathInsideRoot, normal
|
|
|
11
11
|
import { execFile, execFileSync } from "node:child_process";
|
|
12
12
|
import { createReadStream, createWriteStream, existsSync, mkdirSync, readdirSync, realpathSync, rmSync } from "node:fs";
|
|
13
13
|
import { AuthenticationError, MoltNetError, connect } from "@themoltnet/sdk";
|
|
14
|
-
import { createHash, randomUUID } from "node:crypto";
|
|
14
|
+
import { createHash as createHash$1, randomUUID } from "node:crypto";
|
|
15
|
+
import { Writable } from "node:stream";
|
|
16
|
+
import { mkdir, realpath, stat } from "node:fs/promises";
|
|
15
17
|
import { once } from "node:events";
|
|
16
18
|
import { pino, transport } from "pino";
|
|
17
19
|
import { metrics } from "@opentelemetry/api";
|
|
@@ -24,9 +26,7 @@ import { NodeTracerProvider } from "@opentelemetry/sdk-trace-node";
|
|
|
24
26
|
import { ATTR_SERVICE_NAME, ATTR_SERVICE_VERSION } from "@opentelemetry/semantic-conventions";
|
|
25
27
|
import { AsyncLocalStorage } from "node:async_hooks";
|
|
26
28
|
import { getModel } from "@earendil-works/pi-ai";
|
|
27
|
-
import { mkdir, realpath, stat } from "node:fs/promises";
|
|
28
29
|
import { pipeline } from "node:stream/promises";
|
|
29
|
-
import { Writable } from "node:stream";
|
|
30
30
|
import { createGzip } from "node:zlib";
|
|
31
31
|
//#region ../../libs/tasks/src/context.ts
|
|
32
32
|
/**
|
|
@@ -4004,6 +4004,1076 @@ function isHelpFlag(args) {
|
|
|
4004
4004
|
return args.includes("--help") || args.includes("-h");
|
|
4005
4005
|
}
|
|
4006
4006
|
//#endregion
|
|
4007
|
+
//#region ../../libs/api-client/src/generated/core/bodySerializer.gen.ts
|
|
4008
|
+
var jsonBodySerializer = { bodySerializer: (body) => JSON.stringify(body, (_key, value) => typeof value === "bigint" ? value.toString() : value) };
|
|
4009
|
+
Object.entries({
|
|
4010
|
+
$body_: "body",
|
|
4011
|
+
$headers_: "headers",
|
|
4012
|
+
$path_: "path",
|
|
4013
|
+
$query_: "query"
|
|
4014
|
+
});
|
|
4015
|
+
//#endregion
|
|
4016
|
+
//#region ../../libs/api-client/src/generated/core/serverSentEvents.gen.ts
|
|
4017
|
+
var createSseClient = ({ onRequest, onSseError, onSseEvent, responseTransformer, responseValidator, sseDefaultRetryDelay, sseMaxRetryAttempts, sseMaxRetryDelay, sseSleepFn, url, ...options }) => {
|
|
4018
|
+
let lastEventId;
|
|
4019
|
+
const sleep = sseSleepFn ?? ((ms) => new Promise((resolve) => setTimeout(resolve, ms)));
|
|
4020
|
+
const createStream = async function* () {
|
|
4021
|
+
let retryDelay = sseDefaultRetryDelay ?? 3e3;
|
|
4022
|
+
let attempt = 0;
|
|
4023
|
+
const signal = options.signal ?? new AbortController().signal;
|
|
4024
|
+
while (true) {
|
|
4025
|
+
if (signal.aborted) break;
|
|
4026
|
+
attempt++;
|
|
4027
|
+
const headers = options.headers instanceof Headers ? options.headers : new Headers(options.headers);
|
|
4028
|
+
if (lastEventId !== void 0) headers.set("Last-Event-ID", lastEventId);
|
|
4029
|
+
try {
|
|
4030
|
+
const requestInit = {
|
|
4031
|
+
redirect: "follow",
|
|
4032
|
+
...options,
|
|
4033
|
+
body: options.serializedBody,
|
|
4034
|
+
headers,
|
|
4035
|
+
signal
|
|
4036
|
+
};
|
|
4037
|
+
let request = new Request(url, requestInit);
|
|
4038
|
+
if (onRequest) request = await onRequest(url, requestInit);
|
|
4039
|
+
const response = await (options.fetch ?? globalThis.fetch)(request);
|
|
4040
|
+
if (!response.ok) throw new Error(`SSE failed: ${response.status} ${response.statusText}`);
|
|
4041
|
+
if (!response.body) throw new Error("No body in SSE response");
|
|
4042
|
+
const reader = response.body.pipeThrough(new TextDecoderStream()).getReader();
|
|
4043
|
+
let buffer = "";
|
|
4044
|
+
const abortHandler = () => {
|
|
4045
|
+
try {
|
|
4046
|
+
reader.cancel();
|
|
4047
|
+
} catch {}
|
|
4048
|
+
};
|
|
4049
|
+
signal.addEventListener("abort", abortHandler);
|
|
4050
|
+
try {
|
|
4051
|
+
while (true) {
|
|
4052
|
+
const { done, value } = await reader.read();
|
|
4053
|
+
if (done) break;
|
|
4054
|
+
buffer += value;
|
|
4055
|
+
buffer = buffer.replace(/\r\n/g, "\n").replace(/\r/g, "\n");
|
|
4056
|
+
const chunks = buffer.split("\n\n");
|
|
4057
|
+
buffer = chunks.pop() ?? "";
|
|
4058
|
+
for (const chunk of chunks) {
|
|
4059
|
+
const lines = chunk.split("\n");
|
|
4060
|
+
const dataLines = [];
|
|
4061
|
+
let eventName;
|
|
4062
|
+
for (const line of lines) if (line.startsWith("data:")) dataLines.push(line.replace(/^data:\s*/, ""));
|
|
4063
|
+
else if (line.startsWith("event:")) eventName = line.replace(/^event:\s*/, "");
|
|
4064
|
+
else if (line.startsWith("id:")) lastEventId = line.replace(/^id:\s*/, "");
|
|
4065
|
+
else if (line.startsWith("retry:")) {
|
|
4066
|
+
const parsed = Number.parseInt(line.replace(/^retry:\s*/, ""), 10);
|
|
4067
|
+
if (!Number.isNaN(parsed)) retryDelay = parsed;
|
|
4068
|
+
}
|
|
4069
|
+
let data;
|
|
4070
|
+
let parsedJson = false;
|
|
4071
|
+
if (dataLines.length) {
|
|
4072
|
+
const rawData = dataLines.join("\n");
|
|
4073
|
+
try {
|
|
4074
|
+
data = JSON.parse(rawData);
|
|
4075
|
+
parsedJson = true;
|
|
4076
|
+
} catch {
|
|
4077
|
+
data = rawData;
|
|
4078
|
+
}
|
|
4079
|
+
}
|
|
4080
|
+
if (parsedJson) {
|
|
4081
|
+
if (responseValidator) await responseValidator(data);
|
|
4082
|
+
if (responseTransformer) data = await responseTransformer(data);
|
|
4083
|
+
}
|
|
4084
|
+
onSseEvent?.({
|
|
4085
|
+
data,
|
|
4086
|
+
event: eventName,
|
|
4087
|
+
id: lastEventId,
|
|
4088
|
+
retry: retryDelay
|
|
4089
|
+
});
|
|
4090
|
+
if (dataLines.length) yield data;
|
|
4091
|
+
}
|
|
4092
|
+
}
|
|
4093
|
+
} finally {
|
|
4094
|
+
signal.removeEventListener("abort", abortHandler);
|
|
4095
|
+
reader.releaseLock();
|
|
4096
|
+
}
|
|
4097
|
+
break;
|
|
4098
|
+
} catch (error) {
|
|
4099
|
+
onSseError?.(error);
|
|
4100
|
+
if (sseMaxRetryAttempts !== void 0 && attempt >= sseMaxRetryAttempts) break;
|
|
4101
|
+
await sleep(Math.min(retryDelay * 2 ** (attempt - 1), sseMaxRetryDelay ?? 3e4));
|
|
4102
|
+
}
|
|
4103
|
+
}
|
|
4104
|
+
};
|
|
4105
|
+
return { stream: createStream() };
|
|
4106
|
+
};
|
|
4107
|
+
//#endregion
|
|
4108
|
+
//#region ../../libs/api-client/src/generated/core/pathSerializer.gen.ts
|
|
4109
|
+
var separatorArrayExplode = (style) => {
|
|
4110
|
+
switch (style) {
|
|
4111
|
+
case "label": return ".";
|
|
4112
|
+
case "matrix": return ";";
|
|
4113
|
+
case "simple": return ",";
|
|
4114
|
+
default: return "&";
|
|
4115
|
+
}
|
|
4116
|
+
};
|
|
4117
|
+
var separatorArrayNoExplode = (style) => {
|
|
4118
|
+
switch (style) {
|
|
4119
|
+
case "form": return ",";
|
|
4120
|
+
case "pipeDelimited": return "|";
|
|
4121
|
+
case "spaceDelimited": return "%20";
|
|
4122
|
+
default: return ",";
|
|
4123
|
+
}
|
|
4124
|
+
};
|
|
4125
|
+
var separatorObjectExplode = (style) => {
|
|
4126
|
+
switch (style) {
|
|
4127
|
+
case "label": return ".";
|
|
4128
|
+
case "matrix": return ";";
|
|
4129
|
+
case "simple": return ",";
|
|
4130
|
+
default: return "&";
|
|
4131
|
+
}
|
|
4132
|
+
};
|
|
4133
|
+
var serializeArrayParam = ({ allowReserved, explode, name, style, value }) => {
|
|
4134
|
+
if (!explode) {
|
|
4135
|
+
const joinedValues = (allowReserved ? value : value.map((v) => encodeURIComponent(v))).join(separatorArrayNoExplode(style));
|
|
4136
|
+
switch (style) {
|
|
4137
|
+
case "label": return `.${joinedValues}`;
|
|
4138
|
+
case "matrix": return `;${name}=${joinedValues}`;
|
|
4139
|
+
case "simple": return joinedValues;
|
|
4140
|
+
default: return `${name}=${joinedValues}`;
|
|
4141
|
+
}
|
|
4142
|
+
}
|
|
4143
|
+
const separator = separatorArrayExplode(style);
|
|
4144
|
+
const joinedValues = value.map((v) => {
|
|
4145
|
+
if (style === "label" || style === "simple") return allowReserved ? v : encodeURIComponent(v);
|
|
4146
|
+
return serializePrimitiveParam({
|
|
4147
|
+
allowReserved,
|
|
4148
|
+
name,
|
|
4149
|
+
value: v
|
|
4150
|
+
});
|
|
4151
|
+
}).join(separator);
|
|
4152
|
+
return style === "label" || style === "matrix" ? separator + joinedValues : joinedValues;
|
|
4153
|
+
};
|
|
4154
|
+
var serializePrimitiveParam = ({ allowReserved, name, value }) => {
|
|
4155
|
+
if (value === void 0 || value === null) return "";
|
|
4156
|
+
if (typeof value === "object") throw new Error("Deeply-nested arrays/objects aren’t supported. Provide your own `querySerializer()` to handle these.");
|
|
4157
|
+
return `${name}=${allowReserved ? value : encodeURIComponent(value)}`;
|
|
4158
|
+
};
|
|
4159
|
+
var serializeObjectParam = ({ allowReserved, explode, name, style, value, valueOnly }) => {
|
|
4160
|
+
if (value instanceof Date) return valueOnly ? value.toISOString() : `${name}=${value.toISOString()}`;
|
|
4161
|
+
if (style !== "deepObject" && !explode) {
|
|
4162
|
+
let values = [];
|
|
4163
|
+
Object.entries(value).forEach(([key, v]) => {
|
|
4164
|
+
values = [
|
|
4165
|
+
...values,
|
|
4166
|
+
key,
|
|
4167
|
+
allowReserved ? v : encodeURIComponent(v)
|
|
4168
|
+
];
|
|
4169
|
+
});
|
|
4170
|
+
const joinedValues = values.join(",");
|
|
4171
|
+
switch (style) {
|
|
4172
|
+
case "form": return `${name}=${joinedValues}`;
|
|
4173
|
+
case "label": return `.${joinedValues}`;
|
|
4174
|
+
case "matrix": return `;${name}=${joinedValues}`;
|
|
4175
|
+
default: return joinedValues;
|
|
4176
|
+
}
|
|
4177
|
+
}
|
|
4178
|
+
const separator = separatorObjectExplode(style);
|
|
4179
|
+
const joinedValues = Object.entries(value).map(([key, v]) => serializePrimitiveParam({
|
|
4180
|
+
allowReserved,
|
|
4181
|
+
name: style === "deepObject" ? `${name}[${key}]` : key,
|
|
4182
|
+
value: v
|
|
4183
|
+
})).join(separator);
|
|
4184
|
+
return style === "label" || style === "matrix" ? separator + joinedValues : joinedValues;
|
|
4185
|
+
};
|
|
4186
|
+
//#endregion
|
|
4187
|
+
//#region ../../libs/api-client/src/generated/core/utils.gen.ts
|
|
4188
|
+
var PATH_PARAM_RE = /\{[^{}]+\}/g;
|
|
4189
|
+
var defaultPathSerializer = ({ path, url: _url }) => {
|
|
4190
|
+
let url = _url;
|
|
4191
|
+
const matches = _url.match(PATH_PARAM_RE);
|
|
4192
|
+
if (matches) for (const match of matches) {
|
|
4193
|
+
let explode = false;
|
|
4194
|
+
let name = match.substring(1, match.length - 1);
|
|
4195
|
+
let style = "simple";
|
|
4196
|
+
if (name.endsWith("*")) {
|
|
4197
|
+
explode = true;
|
|
4198
|
+
name = name.substring(0, name.length - 1);
|
|
4199
|
+
}
|
|
4200
|
+
if (name.startsWith(".")) {
|
|
4201
|
+
name = name.substring(1);
|
|
4202
|
+
style = "label";
|
|
4203
|
+
} else if (name.startsWith(";")) {
|
|
4204
|
+
name = name.substring(1);
|
|
4205
|
+
style = "matrix";
|
|
4206
|
+
}
|
|
4207
|
+
const value = path[name];
|
|
4208
|
+
if (value === void 0 || value === null) continue;
|
|
4209
|
+
if (Array.isArray(value)) {
|
|
4210
|
+
url = url.replace(match, serializeArrayParam({
|
|
4211
|
+
explode,
|
|
4212
|
+
name,
|
|
4213
|
+
style,
|
|
4214
|
+
value
|
|
4215
|
+
}));
|
|
4216
|
+
continue;
|
|
4217
|
+
}
|
|
4218
|
+
if (typeof value === "object") {
|
|
4219
|
+
url = url.replace(match, serializeObjectParam({
|
|
4220
|
+
explode,
|
|
4221
|
+
name,
|
|
4222
|
+
style,
|
|
4223
|
+
value,
|
|
4224
|
+
valueOnly: true
|
|
4225
|
+
}));
|
|
4226
|
+
continue;
|
|
4227
|
+
}
|
|
4228
|
+
if (style === "matrix") {
|
|
4229
|
+
url = url.replace(match, `;${serializePrimitiveParam({
|
|
4230
|
+
name,
|
|
4231
|
+
value
|
|
4232
|
+
})}`);
|
|
4233
|
+
continue;
|
|
4234
|
+
}
|
|
4235
|
+
const replaceValue = encodeURIComponent(style === "label" ? `.${value}` : value);
|
|
4236
|
+
url = url.replace(match, replaceValue);
|
|
4237
|
+
}
|
|
4238
|
+
return url;
|
|
4239
|
+
};
|
|
4240
|
+
var getUrl = ({ baseUrl, path, query, querySerializer, url: _url }) => {
|
|
4241
|
+
const pathUrl = _url.startsWith("/") ? _url : `/${_url}`;
|
|
4242
|
+
let url = (baseUrl ?? "") + pathUrl;
|
|
4243
|
+
if (path) url = defaultPathSerializer({
|
|
4244
|
+
path,
|
|
4245
|
+
url
|
|
4246
|
+
});
|
|
4247
|
+
let search = query ? querySerializer(query) : "";
|
|
4248
|
+
if (search.startsWith("?")) search = search.substring(1);
|
|
4249
|
+
if (search) url += `?${search}`;
|
|
4250
|
+
return url;
|
|
4251
|
+
};
|
|
4252
|
+
function getValidRequestBody(options) {
|
|
4253
|
+
const hasBody = options.body !== void 0;
|
|
4254
|
+
if (hasBody && options.bodySerializer) {
|
|
4255
|
+
if ("serializedBody" in options) return options.serializedBody !== void 0 && options.serializedBody !== "" ? options.serializedBody : null;
|
|
4256
|
+
return options.body !== "" ? options.body : null;
|
|
4257
|
+
}
|
|
4258
|
+
if (hasBody) return options.body;
|
|
4259
|
+
}
|
|
4260
|
+
//#endregion
|
|
4261
|
+
//#region ../../libs/api-client/src/generated/core/auth.gen.ts
|
|
4262
|
+
var getAuthToken = async (auth, callback) => {
|
|
4263
|
+
const token = typeof callback === "function" ? await callback(auth) : callback;
|
|
4264
|
+
if (!token) return;
|
|
4265
|
+
if (auth.scheme === "bearer") return `Bearer ${token}`;
|
|
4266
|
+
if (auth.scheme === "basic") return `Basic ${btoa(token)}`;
|
|
4267
|
+
return token;
|
|
4268
|
+
};
|
|
4269
|
+
//#endregion
|
|
4270
|
+
//#region ../../libs/api-client/src/generated/client/utils.gen.ts
|
|
4271
|
+
var createQuerySerializer = ({ parameters = {}, ...args } = {}) => {
|
|
4272
|
+
const querySerializer = (queryParams) => {
|
|
4273
|
+
const search = [];
|
|
4274
|
+
if (queryParams && typeof queryParams === "object") for (const name in queryParams) {
|
|
4275
|
+
const value = queryParams[name];
|
|
4276
|
+
if (value === void 0 || value === null) continue;
|
|
4277
|
+
const options = parameters[name] || args;
|
|
4278
|
+
if (Array.isArray(value)) {
|
|
4279
|
+
const serializedArray = serializeArrayParam({
|
|
4280
|
+
allowReserved: options.allowReserved,
|
|
4281
|
+
explode: true,
|
|
4282
|
+
name,
|
|
4283
|
+
style: "form",
|
|
4284
|
+
value,
|
|
4285
|
+
...options.array
|
|
4286
|
+
});
|
|
4287
|
+
if (serializedArray) search.push(serializedArray);
|
|
4288
|
+
} else if (typeof value === "object") {
|
|
4289
|
+
const serializedObject = serializeObjectParam({
|
|
4290
|
+
allowReserved: options.allowReserved,
|
|
4291
|
+
explode: true,
|
|
4292
|
+
name,
|
|
4293
|
+
style: "deepObject",
|
|
4294
|
+
value,
|
|
4295
|
+
...options.object
|
|
4296
|
+
});
|
|
4297
|
+
if (serializedObject) search.push(serializedObject);
|
|
4298
|
+
} else {
|
|
4299
|
+
const serializedPrimitive = serializePrimitiveParam({
|
|
4300
|
+
allowReserved: options.allowReserved,
|
|
4301
|
+
name,
|
|
4302
|
+
value
|
|
4303
|
+
});
|
|
4304
|
+
if (serializedPrimitive) search.push(serializedPrimitive);
|
|
4305
|
+
}
|
|
4306
|
+
}
|
|
4307
|
+
return search.join("&");
|
|
4308
|
+
};
|
|
4309
|
+
return querySerializer;
|
|
4310
|
+
};
|
|
4311
|
+
/**
|
|
4312
|
+
* Infers parseAs value from provided Content-Type header.
|
|
4313
|
+
*/
|
|
4314
|
+
var getParseAs = (contentType) => {
|
|
4315
|
+
if (!contentType) return "stream";
|
|
4316
|
+
const cleanContent = contentType.split(";")[0]?.trim();
|
|
4317
|
+
if (!cleanContent) return;
|
|
4318
|
+
if (cleanContent.startsWith("application/json") || cleanContent.endsWith("+json")) return "json";
|
|
4319
|
+
if (cleanContent === "multipart/form-data") return "formData";
|
|
4320
|
+
if ([
|
|
4321
|
+
"application/",
|
|
4322
|
+
"audio/",
|
|
4323
|
+
"image/",
|
|
4324
|
+
"video/"
|
|
4325
|
+
].some((type) => cleanContent.startsWith(type))) return "blob";
|
|
4326
|
+
if (cleanContent.startsWith("text/")) return "text";
|
|
4327
|
+
};
|
|
4328
|
+
var checkForExistence = (options, name) => {
|
|
4329
|
+
if (!name) return false;
|
|
4330
|
+
if (options.headers.has(name) || options.query?.[name] || options.headers.get("Cookie")?.includes(`${name}=`)) return true;
|
|
4331
|
+
return false;
|
|
4332
|
+
};
|
|
4333
|
+
var setAuthParams = async ({ security, ...options }) => {
|
|
4334
|
+
for (const auth of security) {
|
|
4335
|
+
if (checkForExistence(options, auth.name)) continue;
|
|
4336
|
+
const token = await getAuthToken(auth, options.auth);
|
|
4337
|
+
if (!token) continue;
|
|
4338
|
+
const name = auth.name ?? "Authorization";
|
|
4339
|
+
switch (auth.in) {
|
|
4340
|
+
case "query":
|
|
4341
|
+
if (!options.query) options.query = {};
|
|
4342
|
+
options.query[name] = token;
|
|
4343
|
+
break;
|
|
4344
|
+
case "cookie":
|
|
4345
|
+
options.headers.append("Cookie", `${name}=${token}`);
|
|
4346
|
+
break;
|
|
4347
|
+
default:
|
|
4348
|
+
options.headers.set(name, token);
|
|
4349
|
+
break;
|
|
4350
|
+
}
|
|
4351
|
+
}
|
|
4352
|
+
};
|
|
4353
|
+
var buildUrl = (options) => getUrl({
|
|
4354
|
+
baseUrl: options.baseUrl,
|
|
4355
|
+
path: options.path,
|
|
4356
|
+
query: options.query,
|
|
4357
|
+
querySerializer: typeof options.querySerializer === "function" ? options.querySerializer : createQuerySerializer(options.querySerializer),
|
|
4358
|
+
url: options.url
|
|
4359
|
+
});
|
|
4360
|
+
var mergeConfigs = (a, b) => {
|
|
4361
|
+
const config = {
|
|
4362
|
+
...a,
|
|
4363
|
+
...b
|
|
4364
|
+
};
|
|
4365
|
+
if (config.baseUrl?.endsWith("/")) config.baseUrl = config.baseUrl.substring(0, config.baseUrl.length - 1);
|
|
4366
|
+
config.headers = mergeHeaders(a.headers, b.headers);
|
|
4367
|
+
return config;
|
|
4368
|
+
};
|
|
4369
|
+
var headersEntries = (headers) => {
|
|
4370
|
+
const entries = [];
|
|
4371
|
+
headers.forEach((value, key) => {
|
|
4372
|
+
entries.push([key, value]);
|
|
4373
|
+
});
|
|
4374
|
+
return entries;
|
|
4375
|
+
};
|
|
4376
|
+
var mergeHeaders = (...headers) => {
|
|
4377
|
+
const mergedHeaders = new Headers();
|
|
4378
|
+
for (const header of headers) {
|
|
4379
|
+
if (!header) continue;
|
|
4380
|
+
const iterator = header instanceof Headers ? headersEntries(header) : Object.entries(header);
|
|
4381
|
+
for (const [key, value] of iterator) if (value === null) mergedHeaders.delete(key);
|
|
4382
|
+
else if (Array.isArray(value)) for (const v of value) mergedHeaders.append(key, v);
|
|
4383
|
+
else if (value !== void 0) mergedHeaders.set(key, typeof value === "object" ? JSON.stringify(value) : value);
|
|
4384
|
+
}
|
|
4385
|
+
return mergedHeaders;
|
|
4386
|
+
};
|
|
4387
|
+
var Interceptors = class {
|
|
4388
|
+
fns = [];
|
|
4389
|
+
clear() {
|
|
4390
|
+
this.fns = [];
|
|
4391
|
+
}
|
|
4392
|
+
eject(id) {
|
|
4393
|
+
const index = this.getInterceptorIndex(id);
|
|
4394
|
+
if (this.fns[index]) this.fns[index] = null;
|
|
4395
|
+
}
|
|
4396
|
+
exists(id) {
|
|
4397
|
+
const index = this.getInterceptorIndex(id);
|
|
4398
|
+
return Boolean(this.fns[index]);
|
|
4399
|
+
}
|
|
4400
|
+
getInterceptorIndex(id) {
|
|
4401
|
+
if (typeof id === "number") return this.fns[id] ? id : -1;
|
|
4402
|
+
return this.fns.indexOf(id);
|
|
4403
|
+
}
|
|
4404
|
+
update(id, fn) {
|
|
4405
|
+
const index = this.getInterceptorIndex(id);
|
|
4406
|
+
if (this.fns[index]) {
|
|
4407
|
+
this.fns[index] = fn;
|
|
4408
|
+
return id;
|
|
4409
|
+
}
|
|
4410
|
+
return false;
|
|
4411
|
+
}
|
|
4412
|
+
use(fn) {
|
|
4413
|
+
this.fns.push(fn);
|
|
4414
|
+
return this.fns.length - 1;
|
|
4415
|
+
}
|
|
4416
|
+
};
|
|
4417
|
+
var createInterceptors = () => ({
|
|
4418
|
+
error: new Interceptors(),
|
|
4419
|
+
request: new Interceptors(),
|
|
4420
|
+
response: new Interceptors()
|
|
4421
|
+
});
|
|
4422
|
+
var defaultQuerySerializer = createQuerySerializer({
|
|
4423
|
+
allowReserved: false,
|
|
4424
|
+
array: {
|
|
4425
|
+
explode: true,
|
|
4426
|
+
style: "form"
|
|
4427
|
+
},
|
|
4428
|
+
object: {
|
|
4429
|
+
explode: true,
|
|
4430
|
+
style: "deepObject"
|
|
4431
|
+
}
|
|
4432
|
+
});
|
|
4433
|
+
var defaultHeaders = { "Content-Type": "application/json" };
|
|
4434
|
+
var createConfig = (override = {}) => ({
|
|
4435
|
+
...jsonBodySerializer,
|
|
4436
|
+
headers: defaultHeaders,
|
|
4437
|
+
parseAs: "auto",
|
|
4438
|
+
querySerializer: defaultQuerySerializer,
|
|
4439
|
+
...override
|
|
4440
|
+
});
|
|
4441
|
+
//#endregion
|
|
4442
|
+
//#region ../../libs/api-client/src/generated/client/client.gen.ts
|
|
4443
|
+
var createClient = (config = {}) => {
|
|
4444
|
+
let _config = mergeConfigs(createConfig(), config);
|
|
4445
|
+
const getConfig = () => ({ ..._config });
|
|
4446
|
+
const setConfig = (config) => {
|
|
4447
|
+
_config = mergeConfigs(_config, config);
|
|
4448
|
+
return getConfig();
|
|
4449
|
+
};
|
|
4450
|
+
const interceptors = createInterceptors();
|
|
4451
|
+
const beforeRequest = async (options) => {
|
|
4452
|
+
const opts = {
|
|
4453
|
+
..._config,
|
|
4454
|
+
...options,
|
|
4455
|
+
fetch: options.fetch ?? _config.fetch ?? globalThis.fetch,
|
|
4456
|
+
headers: mergeHeaders(_config.headers, options.headers),
|
|
4457
|
+
serializedBody: void 0
|
|
4458
|
+
};
|
|
4459
|
+
if (opts.security) await setAuthParams({
|
|
4460
|
+
...opts,
|
|
4461
|
+
security: opts.security
|
|
4462
|
+
});
|
|
4463
|
+
if (opts.requestValidator) await opts.requestValidator(opts);
|
|
4464
|
+
if (opts.body !== void 0 && opts.bodySerializer) opts.serializedBody = opts.bodySerializer(opts.body);
|
|
4465
|
+
if (opts.body === void 0 || opts.serializedBody === "") opts.headers.delete("Content-Type");
|
|
4466
|
+
return {
|
|
4467
|
+
opts,
|
|
4468
|
+
url: buildUrl(opts)
|
|
4469
|
+
};
|
|
4470
|
+
};
|
|
4471
|
+
const request = async (options) => {
|
|
4472
|
+
const { opts, url } = await beforeRequest(options);
|
|
4473
|
+
const requestInit = {
|
|
4474
|
+
redirect: "follow",
|
|
4475
|
+
...opts,
|
|
4476
|
+
body: getValidRequestBody(opts)
|
|
4477
|
+
};
|
|
4478
|
+
let request = new Request(url, requestInit);
|
|
4479
|
+
for (const fn of interceptors.request.fns) if (fn) request = await fn(request, opts);
|
|
4480
|
+
const _fetch = opts.fetch;
|
|
4481
|
+
let response;
|
|
4482
|
+
try {
|
|
4483
|
+
response = await _fetch(request);
|
|
4484
|
+
} catch (error) {
|
|
4485
|
+
let finalError = error;
|
|
4486
|
+
for (const fn of interceptors.error.fns) if (fn) finalError = await fn(error, void 0, request, opts);
|
|
4487
|
+
finalError = finalError || {};
|
|
4488
|
+
if (opts.throwOnError) throw finalError;
|
|
4489
|
+
return opts.responseStyle === "data" ? void 0 : {
|
|
4490
|
+
error: finalError,
|
|
4491
|
+
request,
|
|
4492
|
+
response: void 0
|
|
4493
|
+
};
|
|
4494
|
+
}
|
|
4495
|
+
for (const fn of interceptors.response.fns) if (fn) response = await fn(response, request, opts);
|
|
4496
|
+
const result = {
|
|
4497
|
+
request,
|
|
4498
|
+
response
|
|
4499
|
+
};
|
|
4500
|
+
if (response.ok) {
|
|
4501
|
+
const parseAs = (opts.parseAs === "auto" ? getParseAs(response.headers.get("Content-Type")) : opts.parseAs) ?? "json";
|
|
4502
|
+
if (response.status === 204 || response.headers.get("Content-Length") === "0") {
|
|
4503
|
+
let emptyData;
|
|
4504
|
+
switch (parseAs) {
|
|
4505
|
+
case "arrayBuffer":
|
|
4506
|
+
case "blob":
|
|
4507
|
+
case "text":
|
|
4508
|
+
emptyData = await response[parseAs]();
|
|
4509
|
+
break;
|
|
4510
|
+
case "formData":
|
|
4511
|
+
emptyData = new FormData();
|
|
4512
|
+
break;
|
|
4513
|
+
case "stream":
|
|
4514
|
+
emptyData = response.body;
|
|
4515
|
+
break;
|
|
4516
|
+
default:
|
|
4517
|
+
emptyData = {};
|
|
4518
|
+
break;
|
|
4519
|
+
}
|
|
4520
|
+
return opts.responseStyle === "data" ? emptyData : {
|
|
4521
|
+
data: emptyData,
|
|
4522
|
+
...result
|
|
4523
|
+
};
|
|
4524
|
+
}
|
|
4525
|
+
let data;
|
|
4526
|
+
switch (parseAs) {
|
|
4527
|
+
case "arrayBuffer":
|
|
4528
|
+
case "blob":
|
|
4529
|
+
case "formData":
|
|
4530
|
+
case "json":
|
|
4531
|
+
case "text":
|
|
4532
|
+
data = await response[parseAs]();
|
|
4533
|
+
break;
|
|
4534
|
+
case "stream": return opts.responseStyle === "data" ? response.body : {
|
|
4535
|
+
data: response.body,
|
|
4536
|
+
...result
|
|
4537
|
+
};
|
|
4538
|
+
}
|
|
4539
|
+
if (parseAs === "json") {
|
|
4540
|
+
if (opts.responseValidator) await opts.responseValidator(data);
|
|
4541
|
+
if (opts.responseTransformer) data = await opts.responseTransformer(data);
|
|
4542
|
+
}
|
|
4543
|
+
return opts.responseStyle === "data" ? data : {
|
|
4544
|
+
data,
|
|
4545
|
+
...result
|
|
4546
|
+
};
|
|
4547
|
+
}
|
|
4548
|
+
const textError = await response.text();
|
|
4549
|
+
let jsonError;
|
|
4550
|
+
try {
|
|
4551
|
+
jsonError = JSON.parse(textError);
|
|
4552
|
+
} catch {}
|
|
4553
|
+
const error = jsonError ?? textError;
|
|
4554
|
+
let finalError = error;
|
|
4555
|
+
for (const fn of interceptors.error.fns) if (fn) finalError = await fn(error, response, request, opts);
|
|
4556
|
+
finalError = finalError || {};
|
|
4557
|
+
if (opts.throwOnError) throw finalError;
|
|
4558
|
+
return opts.responseStyle === "data" ? void 0 : {
|
|
4559
|
+
error: finalError,
|
|
4560
|
+
...result
|
|
4561
|
+
};
|
|
4562
|
+
};
|
|
4563
|
+
const makeMethodFn = (method) => (options) => request({
|
|
4564
|
+
...options,
|
|
4565
|
+
method
|
|
4566
|
+
});
|
|
4567
|
+
const makeSseFn = (method) => async (options) => {
|
|
4568
|
+
const { opts, url } = await beforeRequest(options);
|
|
4569
|
+
return createSseClient({
|
|
4570
|
+
...opts,
|
|
4571
|
+
body: opts.body,
|
|
4572
|
+
headers: opts.headers,
|
|
4573
|
+
method,
|
|
4574
|
+
onRequest: async (url, init) => {
|
|
4575
|
+
let request = new Request(url, init);
|
|
4576
|
+
for (const fn of interceptors.request.fns) if (fn) request = await fn(request, opts);
|
|
4577
|
+
return request;
|
|
4578
|
+
},
|
|
4579
|
+
url
|
|
4580
|
+
});
|
|
4581
|
+
};
|
|
4582
|
+
return {
|
|
4583
|
+
buildUrl,
|
|
4584
|
+
connect: makeMethodFn("CONNECT"),
|
|
4585
|
+
delete: makeMethodFn("DELETE"),
|
|
4586
|
+
get: makeMethodFn("GET"),
|
|
4587
|
+
getConfig,
|
|
4588
|
+
head: makeMethodFn("HEAD"),
|
|
4589
|
+
interceptors,
|
|
4590
|
+
options: makeMethodFn("OPTIONS"),
|
|
4591
|
+
patch: makeMethodFn("PATCH"),
|
|
4592
|
+
post: makeMethodFn("POST"),
|
|
4593
|
+
put: makeMethodFn("PUT"),
|
|
4594
|
+
request,
|
|
4595
|
+
setConfig,
|
|
4596
|
+
sse: {
|
|
4597
|
+
connect: makeSseFn("CONNECT"),
|
|
4598
|
+
delete: makeSseFn("DELETE"),
|
|
4599
|
+
get: makeSseFn("GET"),
|
|
4600
|
+
head: makeSseFn("HEAD"),
|
|
4601
|
+
options: makeSseFn("OPTIONS"),
|
|
4602
|
+
patch: makeSseFn("PATCH"),
|
|
4603
|
+
post: makeSseFn("POST"),
|
|
4604
|
+
put: makeSseFn("PUT"),
|
|
4605
|
+
trace: makeSseFn("TRACE")
|
|
4606
|
+
},
|
|
4607
|
+
trace: makeMethodFn("TRACE")
|
|
4608
|
+
};
|
|
4609
|
+
};
|
|
4610
|
+
createClient(createConfig({ baseUrl: "https://api.themolt.net" }));
|
|
4611
|
+
var { p: P, n: N, Gx, Gy, a: _a, d: _d } = {
|
|
4612
|
+
p: 57896044618658097711785492504343953926634992332820282019728792003956564819949n,
|
|
4613
|
+
n: 7237005577332262213973186563042994240857116359379907606001950938285454250989n,
|
|
4614
|
+
h: 8n,
|
|
4615
|
+
a: 57896044618658097711785492504343953926634992332820282019728792003956564819948n,
|
|
4616
|
+
d: 37095705934669439343138083508754565189542113879843219016388785533085940283555n,
|
|
4617
|
+
Gx: 15112221349535400772501151409588531511454012693041857206046113283949847762202n,
|
|
4618
|
+
Gy: 46316835694926478169428394003475163141307993866256225615783033603165251855960n
|
|
4619
|
+
};
|
|
4620
|
+
var h = 8n;
|
|
4621
|
+
var L = 32;
|
|
4622
|
+
var L2 = 64;
|
|
4623
|
+
var err = (m = "") => {
|
|
4624
|
+
throw new Error(m);
|
|
4625
|
+
};
|
|
4626
|
+
var isBig = (n) => typeof n === "bigint";
|
|
4627
|
+
var isStr = (s) => typeof s === "string";
|
|
4628
|
+
var isBytes = (a) => a instanceof Uint8Array || ArrayBuffer.isView(a) && a.constructor.name === "Uint8Array";
|
|
4629
|
+
/** assert is Uint8Array (of specific length) */
|
|
4630
|
+
var abytes = (a, l) => !isBytes(a) || typeof l === "number" && l > 0 && a.length !== l ? err("Uint8Array expected") : a;
|
|
4631
|
+
/** create Uint8Array */
|
|
4632
|
+
var u8n = (len) => new Uint8Array(len);
|
|
4633
|
+
var u8fr = (buf) => Uint8Array.from(buf);
|
|
4634
|
+
var padh = (n, pad) => n.toString(16).padStart(pad, "0");
|
|
4635
|
+
var bytesToHex = (b) => Array.from(abytes(b)).map((e) => padh(e, 2)).join("");
|
|
4636
|
+
var C = {
|
|
4637
|
+
_0: 48,
|
|
4638
|
+
_9: 57,
|
|
4639
|
+
A: 65,
|
|
4640
|
+
F: 70,
|
|
4641
|
+
a: 97,
|
|
4642
|
+
f: 102
|
|
4643
|
+
};
|
|
4644
|
+
var _ch = (ch) => {
|
|
4645
|
+
if (ch >= C._0 && ch <= C._9) return ch - C._0;
|
|
4646
|
+
if (ch >= C.A && ch <= C.F) return ch - (C.A - 10);
|
|
4647
|
+
if (ch >= C.a && ch <= C.f) return ch - (C.a - 10);
|
|
4648
|
+
};
|
|
4649
|
+
var hexToBytes = (hex) => {
|
|
4650
|
+
const e = "hex invalid";
|
|
4651
|
+
if (!isStr(hex)) return err(e);
|
|
4652
|
+
const hl = hex.length;
|
|
4653
|
+
const al = hl / 2;
|
|
4654
|
+
if (hl % 2) return err(e);
|
|
4655
|
+
const array = u8n(al);
|
|
4656
|
+
for (let ai = 0, hi = 0; ai < al; ai++, hi += 2) {
|
|
4657
|
+
const n1 = _ch(hex.charCodeAt(hi));
|
|
4658
|
+
const n2 = _ch(hex.charCodeAt(hi + 1));
|
|
4659
|
+
if (n1 === void 0 || n2 === void 0) return err(e);
|
|
4660
|
+
array[ai] = n1 * 16 + n2;
|
|
4661
|
+
}
|
|
4662
|
+
return array;
|
|
4663
|
+
};
|
|
4664
|
+
/** normalize hex or ui8a to ui8a */
|
|
4665
|
+
var toU8 = (a, len) => abytes(isStr(a) ? hexToBytes(a) : u8fr(abytes(a)), len);
|
|
4666
|
+
var cr = () => globalThis?.crypto;
|
|
4667
|
+
var subtle = () => cr()?.subtle ?? err("crypto.subtle must be defined");
|
|
4668
|
+
var concatBytes = (...arrs) => {
|
|
4669
|
+
const r = u8n(arrs.reduce((sum, a) => sum + abytes(a).length, 0));
|
|
4670
|
+
let pad = 0;
|
|
4671
|
+
arrs.forEach((a) => {
|
|
4672
|
+
r.set(a, pad);
|
|
4673
|
+
pad += a.length;
|
|
4674
|
+
});
|
|
4675
|
+
return r;
|
|
4676
|
+
};
|
|
4677
|
+
/** WebCrypto OS-level CSPRNG (random number generator). Will throw when not available. */
|
|
4678
|
+
var randomBytes = (len = L) => {
|
|
4679
|
+
return cr().getRandomValues(u8n(len));
|
|
4680
|
+
};
|
|
4681
|
+
var big = BigInt;
|
|
4682
|
+
var arange = (n, min, max, msg = "bad number: out of range") => isBig(n) && min <= n && n < max ? n : err(msg);
|
|
4683
|
+
/** modular division */
|
|
4684
|
+
var M = (a, b = P) => {
|
|
4685
|
+
const r = a % b;
|
|
4686
|
+
return r >= 0n ? r : b + r;
|
|
4687
|
+
};
|
|
4688
|
+
/** Modular inversion using eucledian GCD (non-CT). No negative exponent for now. */
|
|
4689
|
+
var invert = (num, md) => {
|
|
4690
|
+
if (num === 0n || md <= 0n) err("no inverse n=" + num + " mod=" + md);
|
|
4691
|
+
let a = M(num, md), b = md, x = 0n, y = 1n, u = 1n, v = 0n;
|
|
4692
|
+
while (a !== 0n) {
|
|
4693
|
+
const q = b / a, r = b % a;
|
|
4694
|
+
const m = x - u * q, n = y - v * q;
|
|
4695
|
+
b = a, a = r, x = u, y = v, u = m, v = n;
|
|
4696
|
+
}
|
|
4697
|
+
return b === 1n ? M(x, md) : err("no inverse");
|
|
4698
|
+
};
|
|
4699
|
+
var apoint = (p) => p instanceof Point ? p : err("Point expected");
|
|
4700
|
+
var B256 = 2n ** 256n;
|
|
4701
|
+
/** Point in XYZT extended coordinates. */
|
|
4702
|
+
var Point = class Point {
|
|
4703
|
+
static BASE;
|
|
4704
|
+
static ZERO;
|
|
4705
|
+
ex;
|
|
4706
|
+
ey;
|
|
4707
|
+
ez;
|
|
4708
|
+
et;
|
|
4709
|
+
constructor(ex, ey, ez, et) {
|
|
4710
|
+
const max = B256;
|
|
4711
|
+
this.ex = arange(ex, 0n, max);
|
|
4712
|
+
this.ey = arange(ey, 0n, max);
|
|
4713
|
+
this.ez = arange(ez, 1n, max);
|
|
4714
|
+
this.et = arange(et, 0n, max);
|
|
4715
|
+
Object.freeze(this);
|
|
4716
|
+
}
|
|
4717
|
+
static fromAffine(p) {
|
|
4718
|
+
return new Point(p.x, p.y, 1n, M(p.x * p.y));
|
|
4719
|
+
}
|
|
4720
|
+
/** RFC8032 5.1.3: Uint8Array to Point. */
|
|
4721
|
+
static fromBytes(hex, zip215 = false) {
|
|
4722
|
+
const d = _d;
|
|
4723
|
+
const normed = u8fr(abytes(hex, L));
|
|
4724
|
+
const lastByte = hex[31];
|
|
4725
|
+
normed[31] = lastByte & -129;
|
|
4726
|
+
const y = bytesToNumLE(normed);
|
|
4727
|
+
arange(y, 0n, zip215 ? B256 : P);
|
|
4728
|
+
const y2 = M(y * y);
|
|
4729
|
+
let { isValid, value: x } = uvRatio(M(y2 - 1n), M(d * y2 + 1n));
|
|
4730
|
+
if (!isValid) err("bad point: y not sqrt");
|
|
4731
|
+
const isXOdd = (x & 1n) === 1n;
|
|
4732
|
+
const isLastByteOdd = (lastByte & 128) !== 0;
|
|
4733
|
+
if (!zip215 && x === 0n && isLastByteOdd) err("bad point: x==0, isLastByteOdd");
|
|
4734
|
+
if (isLastByteOdd !== isXOdd) x = M(-x);
|
|
4735
|
+
return new Point(x, y, 1n, M(x * y));
|
|
4736
|
+
}
|
|
4737
|
+
/** Checks if the point is valid and on-curve. */
|
|
4738
|
+
assertValidity() {
|
|
4739
|
+
const a = _a;
|
|
4740
|
+
const d = _d;
|
|
4741
|
+
const p = this;
|
|
4742
|
+
if (p.is0()) throw new Error("bad point: ZERO");
|
|
4743
|
+
const { ex: X, ey: Y, ez: Z, et: T } = p;
|
|
4744
|
+
const X2 = M(X * X);
|
|
4745
|
+
const Y2 = M(Y * Y);
|
|
4746
|
+
const Z2 = M(Z * Z);
|
|
4747
|
+
const Z4 = M(Z2 * Z2);
|
|
4748
|
+
if (M(Z2 * M(M(X2 * a) + Y2)) !== M(Z4 + M(d * M(X2 * Y2)))) throw new Error("bad point: equation left != right (1)");
|
|
4749
|
+
if (M(X * Y) !== M(Z * T)) throw new Error("bad point: equation left != right (2)");
|
|
4750
|
+
return this;
|
|
4751
|
+
}
|
|
4752
|
+
/** Equality check: compare points P&Q. */
|
|
4753
|
+
equals(other) {
|
|
4754
|
+
const { ex: X1, ey: Y1, ez: Z1 } = this;
|
|
4755
|
+
const { ex: X2, ey: Y2, ez: Z2 } = apoint(other);
|
|
4756
|
+
const X1Z2 = M(X1 * Z2);
|
|
4757
|
+
const X2Z1 = M(X2 * Z1);
|
|
4758
|
+
const Y1Z2 = M(Y1 * Z2);
|
|
4759
|
+
const Y2Z1 = M(Y2 * Z1);
|
|
4760
|
+
return X1Z2 === X2Z1 && Y1Z2 === Y2Z1;
|
|
4761
|
+
}
|
|
4762
|
+
is0() {
|
|
4763
|
+
return this.equals(I);
|
|
4764
|
+
}
|
|
4765
|
+
/** Flip point over y coordinate. */
|
|
4766
|
+
negate() {
|
|
4767
|
+
return new Point(M(-this.ex), this.ey, this.ez, M(-this.et));
|
|
4768
|
+
}
|
|
4769
|
+
/** Point doubling. Complete formula. Cost: `4M + 4S + 1*a + 6add + 1*2`. */
|
|
4770
|
+
double() {
|
|
4771
|
+
const { ex: X1, ey: Y1, ez: Z1 } = this;
|
|
4772
|
+
const a = _a;
|
|
4773
|
+
const A = M(X1 * X1);
|
|
4774
|
+
const B = M(Y1 * Y1);
|
|
4775
|
+
const C = M(2n * M(Z1 * Z1));
|
|
4776
|
+
const D = M(a * A);
|
|
4777
|
+
const x1y1 = X1 + Y1;
|
|
4778
|
+
const E = M(M(x1y1 * x1y1) - A - B);
|
|
4779
|
+
const G = D + B;
|
|
4780
|
+
const F = G - C;
|
|
4781
|
+
const H = D - B;
|
|
4782
|
+
const X3 = M(E * F);
|
|
4783
|
+
const Y3 = M(G * H);
|
|
4784
|
+
const T3 = M(E * H);
|
|
4785
|
+
return new Point(X3, Y3, M(F * G), T3);
|
|
4786
|
+
}
|
|
4787
|
+
/** Point addition. Complete formula. Cost: `8M + 1*k + 8add + 1*2`. */
|
|
4788
|
+
add(other) {
|
|
4789
|
+
const { ex: X1, ey: Y1, ez: Z1, et: T1 } = this;
|
|
4790
|
+
const { ex: X2, ey: Y2, ez: Z2, et: T2 } = apoint(other);
|
|
4791
|
+
const a = _a;
|
|
4792
|
+
const d = _d;
|
|
4793
|
+
const A = M(X1 * X2);
|
|
4794
|
+
const B = M(Y1 * Y2);
|
|
4795
|
+
const C = M(T1 * d * T2);
|
|
4796
|
+
const D = M(Z1 * Z2);
|
|
4797
|
+
const E = M((X1 + Y1) * (X2 + Y2) - A - B);
|
|
4798
|
+
const F = M(D - C);
|
|
4799
|
+
const G = M(D + C);
|
|
4800
|
+
const H = M(B - a * A);
|
|
4801
|
+
const X3 = M(E * F);
|
|
4802
|
+
const Y3 = M(G * H);
|
|
4803
|
+
const T3 = M(E * H);
|
|
4804
|
+
return new Point(X3, Y3, M(F * G), T3);
|
|
4805
|
+
}
|
|
4806
|
+
/**
|
|
4807
|
+
* Point-by-scalar multiplication. Scalar must be in range 1 <= n < CURVE.n.
|
|
4808
|
+
* Uses {@link wNAF} for base point.
|
|
4809
|
+
* Uses fake point to mitigate side-channel leakage.
|
|
4810
|
+
* @param n scalar by which point is multiplied
|
|
4811
|
+
* @param safe safe mode guards against timing attacks; unsafe mode is faster
|
|
4812
|
+
*/
|
|
4813
|
+
multiply(n, safe = true) {
|
|
4814
|
+
if (!safe && (n === 0n || this.is0())) return I;
|
|
4815
|
+
arange(n, 1n, N);
|
|
4816
|
+
if (n === 1n) return this;
|
|
4817
|
+
if (this.equals(G)) return wNAF(n).p;
|
|
4818
|
+
let p = I;
|
|
4819
|
+
let f = G;
|
|
4820
|
+
for (let d = this; n > 0n; d = d.double(), n >>= 1n) if (n & 1n) p = p.add(d);
|
|
4821
|
+
else if (safe) f = f.add(d);
|
|
4822
|
+
return p;
|
|
4823
|
+
}
|
|
4824
|
+
/** Convert point to 2d xy affine point. (X, Y, Z) ∋ (x=X/Z, y=Y/Z) */
|
|
4825
|
+
toAffine() {
|
|
4826
|
+
const { ex: x, ey: y, ez: z } = this;
|
|
4827
|
+
if (this.equals(I)) return {
|
|
4828
|
+
x: 0n,
|
|
4829
|
+
y: 1n
|
|
4830
|
+
};
|
|
4831
|
+
const iz = invert(z, P);
|
|
4832
|
+
if (M(z * iz) !== 1n) err("invalid inverse");
|
|
4833
|
+
return {
|
|
4834
|
+
x: M(x * iz),
|
|
4835
|
+
y: M(y * iz)
|
|
4836
|
+
};
|
|
4837
|
+
}
|
|
4838
|
+
toBytes() {
|
|
4839
|
+
const { x, y } = this.assertValidity().toAffine();
|
|
4840
|
+
const b = numTo32bLE(y);
|
|
4841
|
+
b[31] |= x & 1n ? 128 : 0;
|
|
4842
|
+
return b;
|
|
4843
|
+
}
|
|
4844
|
+
toHex() {
|
|
4845
|
+
return bytesToHex(this.toBytes());
|
|
4846
|
+
}
|
|
4847
|
+
clearCofactor() {
|
|
4848
|
+
return this.multiply(big(h), false);
|
|
4849
|
+
}
|
|
4850
|
+
isSmallOrder() {
|
|
4851
|
+
return this.clearCofactor().is0();
|
|
4852
|
+
}
|
|
4853
|
+
isTorsionFree() {
|
|
4854
|
+
let p = this.multiply(N / 2n, false).double();
|
|
4855
|
+
if (N % 2n) p = p.add(this);
|
|
4856
|
+
return p.is0();
|
|
4857
|
+
}
|
|
4858
|
+
static fromHex(hex, zip215) {
|
|
4859
|
+
return Point.fromBytes(toU8(hex), zip215);
|
|
4860
|
+
}
|
|
4861
|
+
get x() {
|
|
4862
|
+
return this.toAffine().x;
|
|
4863
|
+
}
|
|
4864
|
+
get y() {
|
|
4865
|
+
return this.toAffine().y;
|
|
4866
|
+
}
|
|
4867
|
+
toRawBytes() {
|
|
4868
|
+
return this.toBytes();
|
|
4869
|
+
}
|
|
4870
|
+
};
|
|
4871
|
+
/** Generator / base point */
|
|
4872
|
+
var G = new Point(Gx, Gy, 1n, M(Gx * Gy));
|
|
4873
|
+
/** Identity / zero point */
|
|
4874
|
+
var I = new Point(0n, 1n, 1n, 0n);
|
|
4875
|
+
Point.BASE = G;
|
|
4876
|
+
Point.ZERO = I;
|
|
4877
|
+
var numTo32bLE = (num) => hexToBytes(padh(arange(num, 0n, B256), L2)).reverse();
|
|
4878
|
+
var bytesToNumLE = (b) => big("0x" + bytesToHex(u8fr(abytes(b)).reverse()));
|
|
4879
|
+
var pow2 = (x, power) => {
|
|
4880
|
+
let r = x;
|
|
4881
|
+
while (power-- > 0n) {
|
|
4882
|
+
r *= r;
|
|
4883
|
+
r %= P;
|
|
4884
|
+
}
|
|
4885
|
+
return r;
|
|
4886
|
+
};
|
|
4887
|
+
var pow_2_252_3 = (x) => {
|
|
4888
|
+
const b2 = x * x % P * x % P;
|
|
4889
|
+
const b5 = pow2(pow2(b2, 2n) * b2 % P, 1n) * x % P;
|
|
4890
|
+
const b10 = pow2(b5, 5n) * b5 % P;
|
|
4891
|
+
const b20 = pow2(b10, 10n) * b10 % P;
|
|
4892
|
+
const b40 = pow2(b20, 20n) * b20 % P;
|
|
4893
|
+
const b80 = pow2(b40, 40n) * b40 % P;
|
|
4894
|
+
return {
|
|
4895
|
+
pow_p_5_8: pow2(pow2(pow2(pow2(b80, 80n) * b80 % P, 80n) * b80 % P, 10n) * b10 % P, 2n) * x % P,
|
|
4896
|
+
b2
|
|
4897
|
+
};
|
|
4898
|
+
};
|
|
4899
|
+
var RM1 = 19681161376707505956807079304988542015446066515923890162744021073123829784752n;
|
|
4900
|
+
var uvRatio = (u, v) => {
|
|
4901
|
+
const v3 = M(v * v * v);
|
|
4902
|
+
const pow = pow_2_252_3(u * M(v3 * v3 * v)).pow_p_5_8;
|
|
4903
|
+
let x = M(u * v3 * pow);
|
|
4904
|
+
const vx2 = M(v * x * x);
|
|
4905
|
+
const root1 = x;
|
|
4906
|
+
const root2 = M(x * RM1);
|
|
4907
|
+
const useRoot1 = vx2 === u;
|
|
4908
|
+
const useRoot2 = vx2 === M(-u);
|
|
4909
|
+
const noRoot = vx2 === M(-u * RM1);
|
|
4910
|
+
if (useRoot1) x = root1;
|
|
4911
|
+
if (useRoot2 || noRoot) x = root2;
|
|
4912
|
+
if ((M(x) & 1n) === 1n) x = M(-x);
|
|
4913
|
+
return {
|
|
4914
|
+
isValid: useRoot1 || useRoot2,
|
|
4915
|
+
value: x
|
|
4916
|
+
};
|
|
4917
|
+
};
|
|
4918
|
+
/** Math, hex, byte helpers. Not in `utils` because utils share API with noble-curves. */
|
|
4919
|
+
var etc = {
|
|
4920
|
+
sha512Async: async (...messages) => {
|
|
4921
|
+
const s = subtle();
|
|
4922
|
+
const m = concatBytes(...messages);
|
|
4923
|
+
return u8n(await s.digest("SHA-512", m.buffer));
|
|
4924
|
+
},
|
|
4925
|
+
sha512Sync: void 0,
|
|
4926
|
+
bytesToHex,
|
|
4927
|
+
hexToBytes,
|
|
4928
|
+
concatBytes,
|
|
4929
|
+
mod: M,
|
|
4930
|
+
invert,
|
|
4931
|
+
randomBytes
|
|
4932
|
+
};
|
|
4933
|
+
var W = 8;
|
|
4934
|
+
var pwindows = Math.ceil(256 / W) + 1;
|
|
4935
|
+
var pwindowSize = 2 ** (W - 1);
|
|
4936
|
+
var precompute = () => {
|
|
4937
|
+
const points = [];
|
|
4938
|
+
let p = G;
|
|
4939
|
+
let b = p;
|
|
4940
|
+
for (let w = 0; w < pwindows; w++) {
|
|
4941
|
+
b = p;
|
|
4942
|
+
points.push(b);
|
|
4943
|
+
for (let i = 1; i < pwindowSize; i++) {
|
|
4944
|
+
b = b.add(p);
|
|
4945
|
+
points.push(b);
|
|
4946
|
+
}
|
|
4947
|
+
p = b.double();
|
|
4948
|
+
}
|
|
4949
|
+
return points;
|
|
4950
|
+
};
|
|
4951
|
+
var Gpows = void 0;
|
|
4952
|
+
var ctneg = (cnd, p) => {
|
|
4953
|
+
const n = p.negate();
|
|
4954
|
+
return cnd ? n : p;
|
|
4955
|
+
};
|
|
4956
|
+
/**
|
|
4957
|
+
* Precomputes give 12x faster getPublicKey(), 10x sign(), 2x verify() by
|
|
4958
|
+
* caching multiples of G (base point). Cache is stored in 32MB of RAM.
|
|
4959
|
+
* Any time `G.multiply` is done, precomputes are used.
|
|
4960
|
+
* Not used for getSharedSecret, which instead multiplies random pubkey `P.multiply`.
|
|
4961
|
+
*
|
|
4962
|
+
* w-ary non-adjacent form (wNAF) precomputation method is 10% slower than windowed method,
|
|
4963
|
+
* but takes 2x less RAM. RAM reduction is possible by utilizing `.subtract`.
|
|
4964
|
+
*
|
|
4965
|
+
* !! Precomputes can be disabled by commenting-out call of the wNAF() inside Point#multiply().
|
|
4966
|
+
*/
|
|
4967
|
+
var wNAF = (n) => {
|
|
4968
|
+
const comp = Gpows || (Gpows = precompute());
|
|
4969
|
+
let p = I;
|
|
4970
|
+
let f = G;
|
|
4971
|
+
const pow_2_w = 2 ** W;
|
|
4972
|
+
const maxNum = pow_2_w;
|
|
4973
|
+
const mask = big(pow_2_w - 1);
|
|
4974
|
+
const shiftBy = big(W);
|
|
4975
|
+
for (let w = 0; w < pwindows; w++) {
|
|
4976
|
+
let wbits = Number(n & mask);
|
|
4977
|
+
n >>= shiftBy;
|
|
4978
|
+
if (wbits > pwindowSize) {
|
|
4979
|
+
wbits -= maxNum;
|
|
4980
|
+
n += 1n;
|
|
4981
|
+
}
|
|
4982
|
+
const off = w * pwindowSize;
|
|
4983
|
+
const offF = off;
|
|
4984
|
+
const offP = off + Math.abs(wbits) - 1;
|
|
4985
|
+
const isEven = w % 2 !== 0;
|
|
4986
|
+
const isNeg = wbits < 0;
|
|
4987
|
+
if (wbits === 0) f = f.add(ctneg(isEven, comp[offF]));
|
|
4988
|
+
else p = p.add(ctneg(isNeg, comp[offP]));
|
|
4989
|
+
}
|
|
4990
|
+
return {
|
|
4991
|
+
p,
|
|
4992
|
+
f
|
|
4993
|
+
};
|
|
4994
|
+
};
|
|
4995
|
+
//#endregion
|
|
4996
|
+
//#region ../../libs/sdk/src/config.ts
|
|
4997
|
+
/** Read one environment value behind the SDK's config boundary. */
|
|
4998
|
+
function readEnvironmentVariable(name) {
|
|
4999
|
+
return globalThis.process?.env?.[name];
|
|
5000
|
+
}
|
|
5001
|
+
//#endregion
|
|
5002
|
+
//#region ../../libs/crypto-service/src/ssh.ts
|
|
5003
|
+
/**
|
|
5004
|
+
* SSH key format conversion for MoltNet Ed25519 keys
|
|
5005
|
+
*
|
|
5006
|
+
* Converts MoltNet agent keys (ed25519:<base64>) to OpenSSH format
|
|
5007
|
+
* for use with git commit signing and SSH authentication.
|
|
5008
|
+
*/
|
|
5009
|
+
if (!etc.sha512Sync) etc.sha512Sync = (...m) => {
|
|
5010
|
+
const hash = createHash("sha512");
|
|
5011
|
+
m.forEach((msg) => hash.update(msg));
|
|
5012
|
+
return hash.digest();
|
|
5013
|
+
};
|
|
5014
|
+
var OS_KEYRING_SECRET_PROVIDER = "os-keyring";
|
|
5015
|
+
var SecretProviderRegistry = class {
|
|
5016
|
+
#providers = /* @__PURE__ */ new Map();
|
|
5017
|
+
register(provider) {
|
|
5018
|
+
const name = provider.name.trim();
|
|
5019
|
+
if (!name) throw new Error("Secret provider name must not be empty");
|
|
5020
|
+
this.#providers.set(name, provider);
|
|
5021
|
+
return this;
|
|
5022
|
+
}
|
|
5023
|
+
get(name) {
|
|
5024
|
+
return this.#providers.get(name);
|
|
5025
|
+
}
|
|
5026
|
+
async resolve(reference) {
|
|
5027
|
+
const providerName = reference.provider.trim();
|
|
5028
|
+
const key = reference.key.trim();
|
|
5029
|
+
if (!providerName || !key) throw new Error("Secret reference requires provider and key");
|
|
5030
|
+
const provider = this.get(providerName);
|
|
5031
|
+
if (!provider) throw new Error(`Secret provider ${JSON.stringify(providerName)} is not registered`);
|
|
5032
|
+
const value = await provider.read(key);
|
|
5033
|
+
if (!value) throw new Error(`Secret provider ${JSON.stringify(providerName)} has no value for the requested key`);
|
|
5034
|
+
return value;
|
|
5035
|
+
}
|
|
5036
|
+
};
|
|
5037
|
+
var EnvironmentSecretProvider = class {
|
|
5038
|
+
name = "env";
|
|
5039
|
+
constructor(readValue = readEnvironmentVariable) {
|
|
5040
|
+
this.readValue = readValue;
|
|
5041
|
+
}
|
|
5042
|
+
read(key) {
|
|
5043
|
+
if (!/^[A-Za-z_][A-Za-z0-9_]*$/.test(key)) return Promise.reject(/* @__PURE__ */ new Error("Environment secret key must be a variable name"));
|
|
5044
|
+
return Promise.resolve(this.readValue(key) || null);
|
|
5045
|
+
}
|
|
5046
|
+
};
|
|
5047
|
+
function createDefaultSecretProviderRegistry() {
|
|
5048
|
+
return new SecretProviderRegistry().register(new EnvironmentSecretProvider());
|
|
5049
|
+
}
|
|
5050
|
+
//#endregion
|
|
5051
|
+
//#region ../../libs/sdk/src/node.ts
|
|
5052
|
+
/**
|
|
5053
|
+
* Lazy Node adapter. Browser SDK installs never pull in native keyring
|
|
5054
|
+
* packages, and Node consumers only load the adapter when an os-keyring
|
|
5055
|
+
* reference is actually resolved.
|
|
5056
|
+
*/
|
|
5057
|
+
var OSKeyringSecretProvider = class {
|
|
5058
|
+
name = OS_KEYRING_SECRET_PROVIDER;
|
|
5059
|
+
providerPromise;
|
|
5060
|
+
constructor(platform = process.platform) {
|
|
5061
|
+
this.platform = platform;
|
|
5062
|
+
}
|
|
5063
|
+
async read(key) {
|
|
5064
|
+
return (await this.provider()).read(key);
|
|
5065
|
+
}
|
|
5066
|
+
provider() {
|
|
5067
|
+
this.providerPromise ??= import("./assets/src-BRybnxdG.js").then(({ OSKeyringSecretProvider: Provider }) => new Provider(this.platform)).catch((error) => {
|
|
5068
|
+
throw new Error("OS keyring support requires @themoltnet/os-keyring; install it in this Node application", { cause: error });
|
|
5069
|
+
});
|
|
5070
|
+
return this.providerPromise;
|
|
5071
|
+
}
|
|
5072
|
+
};
|
|
5073
|
+
function createNodeSecretProviderRegistry(platform = process.platform) {
|
|
5074
|
+
return createDefaultSecretProviderRegistry().register(new OSKeyringSecretProvider(platform));
|
|
5075
|
+
}
|
|
5076
|
+
//#endregion
|
|
4007
5077
|
//#region src/lib/agent-context.ts
|
|
4008
5078
|
/**
|
|
4009
5079
|
* Report which auth mode `connect()` will use, without ever reading the secret
|
|
@@ -4077,7 +5147,10 @@ async function resolveAgentContext(agentName, options = {}) {
|
|
|
4077
5147
|
if (existsSync(join(agentDir, "moltnet.json"))) return {
|
|
4078
5148
|
agentDir,
|
|
4079
5149
|
agentRootDir: rootDir,
|
|
4080
|
-
agent: await connect({
|
|
5150
|
+
agent: await connect({
|
|
5151
|
+
configDir: agentDir,
|
|
5152
|
+
secretProviders: createNodeSecretProviderRegistry()
|
|
5153
|
+
})
|
|
4081
5154
|
};
|
|
4082
5155
|
}
|
|
4083
5156
|
if (options.allowMissingConfig) {
|
|
@@ -4086,7 +5159,10 @@ async function resolveAgentContext(agentName, options = {}) {
|
|
|
4086
5159
|
return {
|
|
4087
5160
|
agentDir,
|
|
4088
5161
|
agentRootDir: rootDir,
|
|
4089
|
-
agent: await connect({
|
|
5162
|
+
agent: await connect({
|
|
5163
|
+
configDir: agentDir,
|
|
5164
|
+
secretProviders: createNodeSecretProviderRegistry()
|
|
5165
|
+
})
|
|
4090
5166
|
};
|
|
4091
5167
|
}
|
|
4092
5168
|
const tried = roots.map((root) => join(root, ".moltnet", agentName));
|
|
@@ -4405,7 +5481,7 @@ function toDaemonWorkspaceMode(mode) {
|
|
|
4405
5481
|
function boundedKeyDirComponent(key) {
|
|
4406
5482
|
const encoded = encodeURIComponent(key);
|
|
4407
5483
|
if (encoded.length <= 200) return encoded;
|
|
4408
|
-
const hash = createHash("sha256").update(key).digest("hex").slice(0, 16);
|
|
5484
|
+
const hash = createHash$1("sha256").update(key).digest("hex").slice(0, 16);
|
|
4409
5485
|
return `${encoded.slice(0, 180)}-${hash}`;
|
|
4410
5486
|
}
|
|
4411
5487
|
function resolveTaskWorkspaceId(task, executionPlan) {
|
|
@@ -5152,7 +6228,10 @@ async function initWorkerOtel(options) {
|
|
|
5152
6228
|
});
|
|
5153
6229
|
let headersFactory;
|
|
5154
6230
|
if (options.agentDir) {
|
|
5155
|
-
const agent = await connect({
|
|
6231
|
+
const agent = await connect({
|
|
6232
|
+
configDir: options.agentDir,
|
|
6233
|
+
secretProviders: createNodeSecretProviderRegistry()
|
|
6234
|
+
});
|
|
5156
6235
|
headersFactory = async () => {
|
|
5157
6236
|
return { Authorization: `Bearer ${await agent.getToken()}` };
|
|
5158
6237
|
};
|
|
@@ -6908,7 +7987,7 @@ async function isPathInsideRoot(path, root) {
|
|
|
6908
7987
|
return isResolvedPathInsideRoot(realResolvedPath, realResolvedRoot);
|
|
6909
7988
|
}
|
|
6910
7989
|
async function computeCompressedSessionFingerprint(path) {
|
|
6911
|
-
const hash = createHash("sha256");
|
|
7990
|
+
const hash = createHash$1("sha256");
|
|
6912
7991
|
let bytes = 0;
|
|
6913
7992
|
const sink = new Writable({ write(chunk, _encoding, callback) {
|
|
6914
7993
|
bytes += chunk.byteLength;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@themoltnet/agent-daemon",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.39.1",
|
|
4
4
|
"license": "AGPL-3.0-only",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"description": "Universal MoltNet agent daemon host with a built-in Pi/Gondolin runtime and support for trusted operator-owned runtime modules. CLI: moltnet-agent.",
|
|
@@ -44,7 +44,7 @@
|
|
|
44
44
|
"dist/runtime.d.ts.map"
|
|
45
45
|
],
|
|
46
46
|
"engines": {
|
|
47
|
-
"node": ">=
|
|
47
|
+
"node": ">=24"
|
|
48
48
|
},
|
|
49
49
|
"dependencies": {
|
|
50
50
|
"@earendil-works/gondolin": "^0.9.1",
|
|
@@ -68,9 +68,10 @@
|
|
|
68
68
|
"pino": "^10.3.1",
|
|
69
69
|
"pino-pretty": "^13.1.3",
|
|
70
70
|
"typebox": "^1.2.8",
|
|
71
|
-
"@themoltnet/
|
|
72
|
-
"@themoltnet/
|
|
73
|
-
"@themoltnet/sdk": "0.
|
|
71
|
+
"@themoltnet/os-keyring": "0.2.0",
|
|
72
|
+
"@themoltnet/pi-runtime": "0.7.3",
|
|
73
|
+
"@themoltnet/sdk": "0.131.0",
|
|
74
|
+
"@themoltnet/agent-runtime": "0.41.2"
|
|
74
75
|
},
|
|
75
76
|
"devDependencies": {
|
|
76
77
|
"tsx": "^4.7.0",
|
|
@@ -79,8 +80,8 @@
|
|
|
79
80
|
"vite-plugin-dts": "^4.5.4",
|
|
80
81
|
"vitest": "^3.0.0",
|
|
81
82
|
"@moltnet/bootstrap": "0.1.0",
|
|
82
|
-
"@moltnet/observability": "0.1.0",
|
|
83
83
|
"@moltnet/crypto-service": "0.1.0",
|
|
84
|
+
"@moltnet/observability": "0.1.0",
|
|
84
85
|
"@moltnet/tasks": "0.1.0"
|
|
85
86
|
},
|
|
86
87
|
"nx": {
|
|
@@ -98,6 +99,6 @@
|
|
|
98
99
|
"start": "node dist/main.js",
|
|
99
100
|
"build": "vite build",
|
|
100
101
|
"check:pack": "tsx ../../tools/src/check-pack.ts --package . && pnpm run smoke:pack",
|
|
101
|
-
"smoke:pack": "tsx ../../tools/src/smoke-pack.ts --package . --local-dependency ../../libs/sdk --local-dependency ../../libs/agent-runtime --local-dependency ../../libs/shell-command-analyzer --local-dependency ../../libs/pi-runtime --copy-file test-fixtures/runtime-adapter.mjs runtime-adapter.mjs --bin moltnet-agent --args --runtime ./runtime-adapter.mjs --help --expect \"long-running task worker for MoltNet\""
|
|
102
|
+
"smoke:pack": "tsx ../../tools/src/smoke-pack.ts --package . --local-dependency ../../libs/sdk --local-dependency ../../libs/os-keyring --local-dependency ../../libs/agent-runtime --local-dependency ../../libs/shell-command-analyzer --local-dependency ../../libs/pi-runtime --copy-file test-fixtures/runtime-adapter.mjs runtime-adapter.mjs --bin moltnet-agent --args --runtime ./runtime-adapter.mjs --help --expect \"long-running task worker for MoltNet\""
|
|
102
103
|
}
|
|
103
104
|
}
|