@hallohuman/hightea 0.1.0 → 0.1.2
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/bin.js +30 -4
- package/dist/chunk-9wxqnv9x.js +149 -0
- package/dist/{chunk-e1ze9fw5.js → chunk-mcmsnaer.js} +84 -4
- package/dist/plugin/plugin.json +1 -1
- package/dist/plugin/plugin.mjs +1309 -29
- package/package.json +15 -6
package/dist/plugin/plugin.mjs
CHANGED
|
@@ -2971,6 +2971,14 @@ function cleanRegex(source) {
|
|
|
2971
2971
|
const end = source.endsWith("$") ? source.length - 1 : source.length;
|
|
2972
2972
|
return source.slice(start, end);
|
|
2973
2973
|
}
|
|
2974
|
+
function floatSafeRemainder(val, step) {
|
|
2975
|
+
const ratio = val / step;
|
|
2976
|
+
const roundedRatio = Math.round(ratio);
|
|
2977
|
+
const tolerance = 4 * Number.EPSILON * Math.max(Math.abs(ratio), 1);
|
|
2978
|
+
if (Math.abs(ratio - roundedRatio) < tolerance)
|
|
2979
|
+
return 0;
|
|
2980
|
+
return ratio - roundedRatio;
|
|
2981
|
+
}
|
|
2974
2982
|
function assignProp(target, prop, value) {
|
|
2975
2983
|
Object.defineProperty(target, prop, {
|
|
2976
2984
|
value,
|
|
@@ -3910,6 +3918,8 @@ function datetime(args) {
|
|
|
3910
3918
|
return new RegExp(`^${dateSource}T(?:${timeRegex})$`);
|
|
3911
3919
|
}
|
|
3912
3920
|
var anyString = /^[\s\S]{0,}$/;
|
|
3921
|
+
var number = /^-?\d+(?:\.\d+)?$/;
|
|
3922
|
+
var boolean = /^(?:true|false)$/i;
|
|
3913
3923
|
var lowercase = /^[^A-Z]*$/;
|
|
3914
3924
|
var uppercase = /^[^a-z]*$/;
|
|
3915
3925
|
|
|
@@ -3924,6 +3934,136 @@ var _whenHasLength = (payload) => {
|
|
|
3924
3934
|
const val = payload.value;
|
|
3925
3935
|
return !nullish(val) && val.length !== undefined;
|
|
3926
3936
|
};
|
|
3937
|
+
var numericOriginMap = {
|
|
3938
|
+
number: "number",
|
|
3939
|
+
bigint: "bigint",
|
|
3940
|
+
object: "date"
|
|
3941
|
+
};
|
|
3942
|
+
var $ZodCheckLessThan = /* @__PURE__ */ $constructor("$ZodCheckLessThan", (inst, def) => {
|
|
3943
|
+
$ZodCheck.init(inst, def);
|
|
3944
|
+
const origin = numericOriginMap[typeof def.value];
|
|
3945
|
+
inst._zod.check = (payload) => {
|
|
3946
|
+
if (def.inclusive ? payload.value <= def.value : payload.value < def.value) {
|
|
3947
|
+
return;
|
|
3948
|
+
}
|
|
3949
|
+
payload.issues.push({
|
|
3950
|
+
origin: numericOriginMap[typeof payload.value] ?? origin,
|
|
3951
|
+
code: "too_big",
|
|
3952
|
+
maximum: typeof def.value === "object" ? def.value.getTime() : def.value,
|
|
3953
|
+
input: payload.value,
|
|
3954
|
+
inclusive: def.inclusive,
|
|
3955
|
+
inst,
|
|
3956
|
+
continue: !def.abort
|
|
3957
|
+
});
|
|
3958
|
+
};
|
|
3959
|
+
});
|
|
3960
|
+
var $ZodCheckGreaterThan = /* @__PURE__ */ $constructor("$ZodCheckGreaterThan", (inst, def) => {
|
|
3961
|
+
$ZodCheck.init(inst, def);
|
|
3962
|
+
const origin = numericOriginMap[typeof def.value];
|
|
3963
|
+
inst._zod.check = (payload) => {
|
|
3964
|
+
if (def.inclusive ? payload.value >= def.value : payload.value > def.value) {
|
|
3965
|
+
return;
|
|
3966
|
+
}
|
|
3967
|
+
payload.issues.push({
|
|
3968
|
+
origin: numericOriginMap[typeof payload.value] ?? origin,
|
|
3969
|
+
code: "too_small",
|
|
3970
|
+
minimum: typeof def.value === "object" ? def.value.getTime() : def.value,
|
|
3971
|
+
input: payload.value,
|
|
3972
|
+
inclusive: def.inclusive,
|
|
3973
|
+
inst,
|
|
3974
|
+
continue: !def.abort
|
|
3975
|
+
});
|
|
3976
|
+
};
|
|
3977
|
+
});
|
|
3978
|
+
var $ZodCheckMultipleOf = /* @__PURE__ */ $constructor("$ZodCheckMultipleOf", (inst, def) => {
|
|
3979
|
+
$ZodCheck.init(inst, def);
|
|
3980
|
+
inst._zod.check = (payload) => {
|
|
3981
|
+
if (typeof payload.value !== typeof def.value)
|
|
3982
|
+
throw new Error("Cannot mix number and bigint in multiple_of check.");
|
|
3983
|
+
const isMultiple = typeof payload.value === "bigint" ? def.value !== BigInt(0) && payload.value % def.value === BigInt(0) : floatSafeRemainder(payload.value, def.value) === 0;
|
|
3984
|
+
if (isMultiple)
|
|
3985
|
+
return;
|
|
3986
|
+
payload.issues.push({
|
|
3987
|
+
origin: typeof payload.value,
|
|
3988
|
+
code: "not_multiple_of",
|
|
3989
|
+
divisor: def.value,
|
|
3990
|
+
input: payload.value,
|
|
3991
|
+
inst,
|
|
3992
|
+
continue: !def.abort
|
|
3993
|
+
});
|
|
3994
|
+
};
|
|
3995
|
+
});
|
|
3996
|
+
var $ZodCheckNumberFormat = /* @__PURE__ */ $constructor("$ZodCheckNumberFormat", (inst, def) => {
|
|
3997
|
+
$ZodCheck.init(inst, def);
|
|
3998
|
+
def.format = def.format || "float64";
|
|
3999
|
+
const isInt = def.format?.includes("int");
|
|
4000
|
+
const origin = isInt ? "int" : "number";
|
|
4001
|
+
const [minimum, maximum] = NUMBER_FORMAT_RANGES[def.format];
|
|
4002
|
+
inst._zod.check = (payload) => {
|
|
4003
|
+
const input = payload.value;
|
|
4004
|
+
if (isInt) {
|
|
4005
|
+
if (!Number.isInteger(input)) {
|
|
4006
|
+
payload.issues.push({
|
|
4007
|
+
expected: origin,
|
|
4008
|
+
format: def.format,
|
|
4009
|
+
code: "invalid_type",
|
|
4010
|
+
continue: false,
|
|
4011
|
+
input,
|
|
4012
|
+
inst
|
|
4013
|
+
});
|
|
4014
|
+
return;
|
|
4015
|
+
}
|
|
4016
|
+
if (!Number.isSafeInteger(input)) {
|
|
4017
|
+
if (input > 0) {
|
|
4018
|
+
payload.issues.push({
|
|
4019
|
+
input,
|
|
4020
|
+
code: "too_big",
|
|
4021
|
+
maximum: Number.MAX_SAFE_INTEGER,
|
|
4022
|
+
note: "Integers must be within the safe integer range.",
|
|
4023
|
+
inst,
|
|
4024
|
+
origin,
|
|
4025
|
+
inclusive: true,
|
|
4026
|
+
continue: !def.abort
|
|
4027
|
+
});
|
|
4028
|
+
} else {
|
|
4029
|
+
payload.issues.push({
|
|
4030
|
+
input,
|
|
4031
|
+
code: "too_small",
|
|
4032
|
+
minimum: Number.MIN_SAFE_INTEGER,
|
|
4033
|
+
note: "Integers must be within the safe integer range.",
|
|
4034
|
+
inst,
|
|
4035
|
+
origin,
|
|
4036
|
+
inclusive: true,
|
|
4037
|
+
continue: !def.abort
|
|
4038
|
+
});
|
|
4039
|
+
}
|
|
4040
|
+
return;
|
|
4041
|
+
}
|
|
4042
|
+
}
|
|
4043
|
+
if (input < minimum) {
|
|
4044
|
+
payload.issues.push({
|
|
4045
|
+
origin: "number",
|
|
4046
|
+
input,
|
|
4047
|
+
code: "too_small",
|
|
4048
|
+
minimum,
|
|
4049
|
+
inclusive: true,
|
|
4050
|
+
inst,
|
|
4051
|
+
continue: !def.abort
|
|
4052
|
+
});
|
|
4053
|
+
}
|
|
4054
|
+
if (input > maximum) {
|
|
4055
|
+
payload.issues.push({
|
|
4056
|
+
origin: "number",
|
|
4057
|
+
input,
|
|
4058
|
+
code: "too_big",
|
|
4059
|
+
maximum,
|
|
4060
|
+
inclusive: true,
|
|
4061
|
+
inst,
|
|
4062
|
+
continue: !def.abort
|
|
4063
|
+
});
|
|
4064
|
+
}
|
|
4065
|
+
};
|
|
4066
|
+
});
|
|
3927
4067
|
var $ZodCheckMaxLength = /* @__PURE__ */ $constructor("$ZodCheckMaxLength", (inst, def) => {
|
|
3928
4068
|
var _a;
|
|
3929
4069
|
$ZodCheck.init(inst, def);
|
|
@@ -4634,6 +4774,53 @@ var $ZodJWT = /* @__PURE__ */ $constructor("$ZodJWT", (inst, def) => {
|
|
|
4634
4774
|
});
|
|
4635
4775
|
};
|
|
4636
4776
|
});
|
|
4777
|
+
var $ZodNumber = /* @__PURE__ */ $constructor("$ZodNumber", (inst, def) => {
|
|
4778
|
+
$ZodType.init(inst, def);
|
|
4779
|
+
inst._zod.pattern = number;
|
|
4780
|
+
inst._zod.parse = (payload, _ctx) => {
|
|
4781
|
+
if (def.coerce)
|
|
4782
|
+
try {
|
|
4783
|
+
payload.value = Number(payload.value);
|
|
4784
|
+
} catch (_) {}
|
|
4785
|
+
const input = payload.value;
|
|
4786
|
+
if (typeof input === "number" && !Number.isNaN(input) && Number.isFinite(input)) {
|
|
4787
|
+
return payload;
|
|
4788
|
+
}
|
|
4789
|
+
const received = typeof input === "number" ? Number.isNaN(input) ? "NaN" : !Number.isFinite(input) ? String(input) : undefined : undefined;
|
|
4790
|
+
payload.issues.push({
|
|
4791
|
+
expected: "number",
|
|
4792
|
+
code: "invalid_type",
|
|
4793
|
+
input,
|
|
4794
|
+
inst,
|
|
4795
|
+
...received ? { received } : {}
|
|
4796
|
+
});
|
|
4797
|
+
return payload;
|
|
4798
|
+
};
|
|
4799
|
+
});
|
|
4800
|
+
var $ZodNumberFormat = /* @__PURE__ */ $constructor("$ZodNumberFormat", (inst, def) => {
|
|
4801
|
+
$ZodCheckNumberFormat.init(inst, def);
|
|
4802
|
+
$ZodNumber.init(inst, def);
|
|
4803
|
+
});
|
|
4804
|
+
var $ZodBoolean = /* @__PURE__ */ $constructor("$ZodBoolean", (inst, def) => {
|
|
4805
|
+
$ZodType.init(inst, def);
|
|
4806
|
+
inst._zod.pattern = boolean;
|
|
4807
|
+
inst._zod.parse = (payload, _ctx) => {
|
|
4808
|
+
if (def.coerce)
|
|
4809
|
+
try {
|
|
4810
|
+
payload.value = Boolean(payload.value);
|
|
4811
|
+
} catch (_) {}
|
|
4812
|
+
const input = payload.value;
|
|
4813
|
+
if (typeof input === "boolean")
|
|
4814
|
+
return payload;
|
|
4815
|
+
payload.issues.push({
|
|
4816
|
+
expected: "boolean",
|
|
4817
|
+
code: "invalid_type",
|
|
4818
|
+
input,
|
|
4819
|
+
inst
|
|
4820
|
+
});
|
|
4821
|
+
return payload;
|
|
4822
|
+
};
|
|
4823
|
+
});
|
|
4637
4824
|
var $ZodUnknown = /* @__PURE__ */ $constructor("$ZodUnknown", (inst, def) => {
|
|
4638
4825
|
$ZodType.init(inst, def);
|
|
4639
4826
|
inst._zod.parse = (payload) => payload;
|
|
@@ -5087,6 +5274,89 @@ var $ZodUnion = /* @__PURE__ */ $constructor("$ZodUnion", (inst, def) => {
|
|
|
5087
5274
|
});
|
|
5088
5275
|
};
|
|
5089
5276
|
});
|
|
5277
|
+
function discriminatorMap(def) {
|
|
5278
|
+
const map = new Map;
|
|
5279
|
+
for (const option of def.options) {
|
|
5280
|
+
const values = option._zod.propValues?.[def.discriminator];
|
|
5281
|
+
if (!values || values.size === 0)
|
|
5282
|
+
throw new Error(`Invalid discriminated union option at index "${def.options.indexOf(option)}"`);
|
|
5283
|
+
for (const value of values) {
|
|
5284
|
+
if (map.has(value)) {
|
|
5285
|
+
if (value !== undefined)
|
|
5286
|
+
throw new Error(`Duplicate discriminator value "${String(value)}"`);
|
|
5287
|
+
map.set(value, null);
|
|
5288
|
+
} else {
|
|
5289
|
+
map.set(value, option);
|
|
5290
|
+
}
|
|
5291
|
+
}
|
|
5292
|
+
}
|
|
5293
|
+
return map;
|
|
5294
|
+
}
|
|
5295
|
+
var $ZodDiscriminatedUnion = /* @__PURE__ */ $constructor("$ZodDiscriminatedUnion", (inst, def) => {
|
|
5296
|
+
def.inclusive = false;
|
|
5297
|
+
$ZodUnion.init(inst, def);
|
|
5298
|
+
const _super = inst._zod.parse;
|
|
5299
|
+
defineLazyInternal(inst, "propValues", (zod) => {
|
|
5300
|
+
const propValues = {};
|
|
5301
|
+
let undefinedCount = 0;
|
|
5302
|
+
for (const option of zod.def.options) {
|
|
5303
|
+
const pv = option._zod.propValues;
|
|
5304
|
+
if (!pv || Object.keys(pv).length === 0)
|
|
5305
|
+
throw new Error(`Invalid discriminated union option at index "${zod.def.options.indexOf(option)}"`);
|
|
5306
|
+
if (pv[zod.def.discriminator]?.has(undefined))
|
|
5307
|
+
undefinedCount++;
|
|
5308
|
+
for (const [k, v] of Object.entries(pv)) {
|
|
5309
|
+
if (!Object.prototype.hasOwnProperty.call(propValues, k)) {
|
|
5310
|
+
assignProp(propValues, k, new Set);
|
|
5311
|
+
}
|
|
5312
|
+
for (const val of v) {
|
|
5313
|
+
propValues[k].add(val);
|
|
5314
|
+
}
|
|
5315
|
+
}
|
|
5316
|
+
}
|
|
5317
|
+
if (!zod.def.unionFallback && undefinedCount > 1)
|
|
5318
|
+
propValues[zod.def.discriminator]?.delete(undefined);
|
|
5319
|
+
return propValues;
|
|
5320
|
+
});
|
|
5321
|
+
def.options.forEach((option, i) => {
|
|
5322
|
+
const propShape = rawShape(option._zod.def);
|
|
5323
|
+
if (propShape && !Object.prototype.hasOwnProperty.call(propShape, def.discriminator)) {
|
|
5324
|
+
throw new Error(`Invalid discriminated union option at index "${i}"`);
|
|
5325
|
+
}
|
|
5326
|
+
});
|
|
5327
|
+
const disc = cached(() => discriminatorMap(def));
|
|
5328
|
+
inst._zod.parse = (payload, ctx) => {
|
|
5329
|
+
const input = payload.value;
|
|
5330
|
+
if (!isObject(input)) {
|
|
5331
|
+
payload.issues.push({
|
|
5332
|
+
code: "invalid_type",
|
|
5333
|
+
expected: "object",
|
|
5334
|
+
input,
|
|
5335
|
+
inst
|
|
5336
|
+
});
|
|
5337
|
+
return payload;
|
|
5338
|
+
}
|
|
5339
|
+
const value = input?.[def.discriminator];
|
|
5340
|
+
const opt = disc.value.get(value);
|
|
5341
|
+
if (opt && (value !== undefined || ctx.direction !== "backward")) {
|
|
5342
|
+
return opt._zod.run(payload, ctx);
|
|
5343
|
+
}
|
|
5344
|
+
if (def.unionFallback || ctx.direction === "backward") {
|
|
5345
|
+
return _super(payload, ctx);
|
|
5346
|
+
}
|
|
5347
|
+
payload.issues.push({
|
|
5348
|
+
code: "invalid_union",
|
|
5349
|
+
errors: [],
|
|
5350
|
+
note: "No matching discriminator",
|
|
5351
|
+
discriminator: def.discriminator,
|
|
5352
|
+
options: Array.from(disc.value.keys()).filter((value) => disc.value.get(value) !== null),
|
|
5353
|
+
input,
|
|
5354
|
+
path: [def.discriminator],
|
|
5355
|
+
inst
|
|
5356
|
+
});
|
|
5357
|
+
return payload;
|
|
5358
|
+
};
|
|
5359
|
+
});
|
|
5090
5360
|
var $ZodIntersection = /* @__PURE__ */ $constructor("$ZodIntersection", (inst, def) => {
|
|
5091
5361
|
$ZodType.init(inst, def);
|
|
5092
5362
|
inst._zod.parse = (payload, ctx) => {
|
|
@@ -6306,6 +6576,24 @@ function _isoDuration(Class, params) {
|
|
|
6306
6576
|
...normalizeParams(params)
|
|
6307
6577
|
});
|
|
6308
6578
|
}
|
|
6579
|
+
function _number(Class, params) {
|
|
6580
|
+
return new Class(snapshotChecks({ type: "number", checks: [], ...normalizeParams(params) }));
|
|
6581
|
+
}
|
|
6582
|
+
function _int(Class, params) {
|
|
6583
|
+
return new Class({
|
|
6584
|
+
type: "number",
|
|
6585
|
+
check: "number_format",
|
|
6586
|
+
abort: false,
|
|
6587
|
+
format: "safeint",
|
|
6588
|
+
...normalizeParams(params)
|
|
6589
|
+
});
|
|
6590
|
+
}
|
|
6591
|
+
function _boolean(Class, params) {
|
|
6592
|
+
return new Class({
|
|
6593
|
+
type: "boolean",
|
|
6594
|
+
...normalizeParams(params)
|
|
6595
|
+
});
|
|
6596
|
+
}
|
|
6309
6597
|
function _unknown(Class) {
|
|
6310
6598
|
return new Class({
|
|
6311
6599
|
type: "unknown"
|
|
@@ -6317,6 +6605,45 @@ function _never(Class, params) {
|
|
|
6317
6605
|
...normalizeParams(params)
|
|
6318
6606
|
});
|
|
6319
6607
|
}
|
|
6608
|
+
function _lt(value, params) {
|
|
6609
|
+
return new $ZodCheckLessThan({
|
|
6610
|
+
check: "less_than",
|
|
6611
|
+
...normalizeParams(params),
|
|
6612
|
+
value,
|
|
6613
|
+
inclusive: false
|
|
6614
|
+
});
|
|
6615
|
+
}
|
|
6616
|
+
function _lte(value, params) {
|
|
6617
|
+
return new $ZodCheckLessThan({
|
|
6618
|
+
check: "less_than",
|
|
6619
|
+
...normalizeParams(params),
|
|
6620
|
+
value,
|
|
6621
|
+
inclusive: true
|
|
6622
|
+
});
|
|
6623
|
+
}
|
|
6624
|
+
function _gt(value, params) {
|
|
6625
|
+
return new $ZodCheckGreaterThan({
|
|
6626
|
+
check: "greater_than",
|
|
6627
|
+
...normalizeParams(params),
|
|
6628
|
+
value,
|
|
6629
|
+
inclusive: false
|
|
6630
|
+
});
|
|
6631
|
+
}
|
|
6632
|
+
function _gte(value, params) {
|
|
6633
|
+
return new $ZodCheckGreaterThan({
|
|
6634
|
+
check: "greater_than",
|
|
6635
|
+
...normalizeParams(params),
|
|
6636
|
+
value,
|
|
6637
|
+
inclusive: true
|
|
6638
|
+
});
|
|
6639
|
+
}
|
|
6640
|
+
function _multipleOf(value, params) {
|
|
6641
|
+
return new $ZodCheckMultipleOf({
|
|
6642
|
+
check: "multiple_of",
|
|
6643
|
+
...normalizeParams(params),
|
|
6644
|
+
value
|
|
6645
|
+
});
|
|
6646
|
+
}
|
|
6320
6647
|
function _maxLength(maximum, params) {
|
|
6321
6648
|
const ch = new $ZodCheckMaxLength({
|
|
6322
6649
|
check: "max_length",
|
|
@@ -7107,6 +7434,51 @@ var stringProcessor = (schema, ctx, _json, _params) => {
|
|
|
7107
7434
|
}
|
|
7108
7435
|
}
|
|
7109
7436
|
};
|
|
7437
|
+
var numberProcessor = (schema, ctx, _json, params) => {
|
|
7438
|
+
const json = _json;
|
|
7439
|
+
const { minimum, maximum, multipleOf, exclusiveMaximum, exclusiveMinimum, isInt } = aggregateChecks(schema);
|
|
7440
|
+
json.type = isInt ? "integer" : "number";
|
|
7441
|
+
const exMin = typeof exclusiveMinimum === "number" && exclusiveMinimum >= (minimum ?? Number.NEGATIVE_INFINITY);
|
|
7442
|
+
const exMax = typeof exclusiveMaximum === "number" && exclusiveMaximum <= (maximum ?? Number.POSITIVE_INFINITY);
|
|
7443
|
+
const legacy = ctx.target === "draft-04" || ctx.target === "openapi-3.0";
|
|
7444
|
+
if (exMin) {
|
|
7445
|
+
if (legacy) {
|
|
7446
|
+
json.minimum = exclusiveMinimum;
|
|
7447
|
+
json.exclusiveMinimum = true;
|
|
7448
|
+
} else {
|
|
7449
|
+
json.exclusiveMinimum = exclusiveMinimum;
|
|
7450
|
+
}
|
|
7451
|
+
} else if (typeof minimum === "number") {
|
|
7452
|
+
json.minimum = minimum;
|
|
7453
|
+
}
|
|
7454
|
+
if (exMax) {
|
|
7455
|
+
if (legacy) {
|
|
7456
|
+
json.maximum = exclusiveMaximum;
|
|
7457
|
+
json.exclusiveMaximum = true;
|
|
7458
|
+
} else {
|
|
7459
|
+
json.exclusiveMaximum = exclusiveMaximum;
|
|
7460
|
+
}
|
|
7461
|
+
} else if (typeof maximum === "number") {
|
|
7462
|
+
json.maximum = maximum;
|
|
7463
|
+
}
|
|
7464
|
+
if (multipleOf) {
|
|
7465
|
+
const divisors = new Set;
|
|
7466
|
+
for (const divisor of multipleOf) {
|
|
7467
|
+
if (Number.isFinite(divisor) && divisor !== 0)
|
|
7468
|
+
divisors.add(Math.abs(divisor));
|
|
7469
|
+
else
|
|
7470
|
+
handleUnrepresentable(schema, ctx, json, params, `A multipleOf divisor of ${divisor} cannot be represented in JSON Schema`);
|
|
7471
|
+
}
|
|
7472
|
+
const [first, ...rest] = divisors;
|
|
7473
|
+
if (first !== undefined)
|
|
7474
|
+
json.multipleOf = first;
|
|
7475
|
+
if (rest.length)
|
|
7476
|
+
json.allOf = [...json.allOf ?? [], ...rest.map((m) => ({ multipleOf: m }))];
|
|
7477
|
+
}
|
|
7478
|
+
};
|
|
7479
|
+
var booleanProcessor = (_schema, _ctx, json, _params) => {
|
|
7480
|
+
json.type = "boolean";
|
|
7481
|
+
};
|
|
7110
7482
|
var neverProcessor = (_schema, _ctx, json, _params) => {
|
|
7111
7483
|
json.not = {};
|
|
7112
7484
|
};
|
|
@@ -7889,6 +8261,90 @@ var ZodJWT = /* @__PURE__ */ $constructor("ZodJWT", (inst, def) => {
|
|
|
7889
8261
|
$ZodJWT.init(inst, def);
|
|
7890
8262
|
ZodStringFormat.init(inst, def);
|
|
7891
8263
|
});
|
|
8264
|
+
var ZodNumber = /* @__PURE__ */ $constructor("ZodNumber", (inst, def) => {
|
|
8265
|
+
$ZodNumber.init(inst, def);
|
|
8266
|
+
ZodType.init(inst, def);
|
|
8267
|
+
inst._zod.processJSONSchema = (ctx, json, params) => numberProcessor(inst, ctx, json, params);
|
|
8268
|
+
inst.isFinite = true;
|
|
8269
|
+
}, /* @__PURE__ */ derived({
|
|
8270
|
+
minValue: (inst) => {
|
|
8271
|
+
const { minimum, exclusiveMinimum } = aggregateChecks(inst);
|
|
8272
|
+
return Math.max(minimum ?? Number.NEGATIVE_INFINITY, exclusiveMinimum ?? Number.NEGATIVE_INFINITY);
|
|
8273
|
+
},
|
|
8274
|
+
maxValue: (inst) => {
|
|
8275
|
+
const { maximum, exclusiveMaximum } = aggregateChecks(inst);
|
|
8276
|
+
return Math.min(maximum ?? Number.POSITIVE_INFINITY, exclusiveMaximum ?? Number.POSITIVE_INFINITY);
|
|
8277
|
+
},
|
|
8278
|
+
isInt: (inst) => {
|
|
8279
|
+
const { isInt, multipleOf } = aggregateChecks(inst);
|
|
8280
|
+
return !!isInt || !!multipleOf?.some(Number.isSafeInteger);
|
|
8281
|
+
},
|
|
8282
|
+
format: (inst) => aggregateChecks(inst).format ?? null
|
|
8283
|
+
}, {
|
|
8284
|
+
gt(value, params) {
|
|
8285
|
+
return this.check(_gt(value, params));
|
|
8286
|
+
},
|
|
8287
|
+
gte(value, params) {
|
|
8288
|
+
return this.check(_gte(value, params));
|
|
8289
|
+
},
|
|
8290
|
+
min(value, params) {
|
|
8291
|
+
return this.check(_gte(value, params));
|
|
8292
|
+
},
|
|
8293
|
+
lt(value, params) {
|
|
8294
|
+
return this.check(_lt(value, params));
|
|
8295
|
+
},
|
|
8296
|
+
lte(value, params) {
|
|
8297
|
+
return this.check(_lte(value, params));
|
|
8298
|
+
},
|
|
8299
|
+
max(value, params) {
|
|
8300
|
+
return this.check(_lte(value, params));
|
|
8301
|
+
},
|
|
8302
|
+
int(params) {
|
|
8303
|
+
return this.check(int(params));
|
|
8304
|
+
},
|
|
8305
|
+
safe(params) {
|
|
8306
|
+
return this.check(int(params));
|
|
8307
|
+
},
|
|
8308
|
+
positive(params) {
|
|
8309
|
+
return this.check(_gt(0, params));
|
|
8310
|
+
},
|
|
8311
|
+
nonnegative(params) {
|
|
8312
|
+
return this.check(_gte(0, params));
|
|
8313
|
+
},
|
|
8314
|
+
negative(params) {
|
|
8315
|
+
return this.check(_lt(0, params));
|
|
8316
|
+
},
|
|
8317
|
+
nonpositive(params) {
|
|
8318
|
+
return this.check(_lte(0, params));
|
|
8319
|
+
},
|
|
8320
|
+
multipleOf(value, params) {
|
|
8321
|
+
return this.check(_multipleOf(value, params));
|
|
8322
|
+
},
|
|
8323
|
+
step(value, params) {
|
|
8324
|
+
return this.check(_multipleOf(value, params));
|
|
8325
|
+
},
|
|
8326
|
+
finite() {
|
|
8327
|
+
return this;
|
|
8328
|
+
}
|
|
8329
|
+
}));
|
|
8330
|
+
function number2(params) {
|
|
8331
|
+
return _number(ZodNumber, params);
|
|
8332
|
+
}
|
|
8333
|
+
var ZodNumberFormat = /* @__PURE__ */ $constructor("ZodNumberFormat", (inst, def) => {
|
|
8334
|
+
$ZodNumberFormat.init(inst, def);
|
|
8335
|
+
ZodNumber.init(inst, def);
|
|
8336
|
+
});
|
|
8337
|
+
function int(params) {
|
|
8338
|
+
return _int(ZodNumberFormat, params);
|
|
8339
|
+
}
|
|
8340
|
+
var ZodBoolean = /* @__PURE__ */ $constructor("ZodBoolean", (inst, def) => {
|
|
8341
|
+
$ZodBoolean.init(inst, def);
|
|
8342
|
+
ZodType.init(inst, def);
|
|
8343
|
+
inst._zod.processJSONSchema = (ctx, json, params) => booleanProcessor(inst, ctx, json, params);
|
|
8344
|
+
});
|
|
8345
|
+
function boolean2(params) {
|
|
8346
|
+
return _boolean(ZodBoolean, params);
|
|
8347
|
+
}
|
|
7892
8348
|
var ZodUnknown = /* @__PURE__ */ $constructor("ZodUnknown", (inst, def) => {
|
|
7893
8349
|
$ZodUnknown.init(inst, def);
|
|
7894
8350
|
ZodType.init(inst, def);
|
|
@@ -8002,6 +8458,18 @@ function union(options, params) {
|
|
|
8002
8458
|
...normalizeParams(params)
|
|
8003
8459
|
});
|
|
8004
8460
|
}
|
|
8461
|
+
var ZodDiscriminatedUnion = /* @__PURE__ */ $constructor("ZodDiscriminatedUnion", (inst, def) => {
|
|
8462
|
+
ZodUnion.init(inst, def);
|
|
8463
|
+
$ZodDiscriminatedUnion.init(inst, def);
|
|
8464
|
+
});
|
|
8465
|
+
function discriminatedUnion(discriminator, options, params) {
|
|
8466
|
+
return new ZodDiscriminatedUnion({
|
|
8467
|
+
type: "union",
|
|
8468
|
+
options,
|
|
8469
|
+
discriminator,
|
|
8470
|
+
...normalizeParams(params)
|
|
8471
|
+
});
|
|
8472
|
+
}
|
|
8005
8473
|
var ZodIntersection = /* @__PURE__ */ $constructor("ZodIntersection", (inst, def) => {
|
|
8006
8474
|
$ZodIntersection.init(inst, def);
|
|
8007
8475
|
ZodType.init(inst, def);
|
|
@@ -8987,6 +9455,95 @@ var ACTION_INTRODUCED_IN = {
|
|
|
8987
9455
|
var STATUS_ACTIVITY_MASK = (1 << 5) - 1;
|
|
8988
9456
|
// ../../node_modules/.bun/@microsoft+agent-host-protocol@1.1.0/node_modules/@microsoft/agent-host-protocol/dist/types/channels-chat/reducer.js
|
|
8989
9457
|
var STATUS_ACTIVITY_MASK2 = (1 << 5) - 1;
|
|
9458
|
+
// ../ahp/src/source.ts
|
|
9459
|
+
var SOURCE_ERROR_CODES = [
|
|
9460
|
+
"SOURCE_UNBOUND",
|
|
9461
|
+
"SOURCE_UNAVAILABLE",
|
|
9462
|
+
"NOT_MAPPED",
|
|
9463
|
+
"INVALID_ADDRESS",
|
|
9464
|
+
"HOST_OFFLINE",
|
|
9465
|
+
"SOURCE_GAP",
|
|
9466
|
+
"FRAME_TOO_LARGE",
|
|
9467
|
+
"ITEM_TOO_LARGE",
|
|
9468
|
+
"STALE_EXPECTATION"
|
|
9469
|
+
];
|
|
9470
|
+
var SOURCE_FRAME_MAX_BYTES = 512 * 1024;
|
|
9471
|
+
var SOURCE_PAGE_BUDGET_BYTES = 256 * 1024;
|
|
9472
|
+
var SOURCE_LIST_LIMIT_MAX = 1000;
|
|
9473
|
+
function sourceFrameBytes(frame) {
|
|
9474
|
+
return new TextEncoder().encode(JSON.stringify(frame)).length;
|
|
9475
|
+
}
|
|
9476
|
+
var Id = string2().min(1).max(256);
|
|
9477
|
+
var Address = string2().min(1).max(1024);
|
|
9478
|
+
var Ordinal = string2().regex(/^(0|[1-9][0-9]{0,17})$/u);
|
|
9479
|
+
var SourcePositionSchema = object({ epoch: Id, ordinal: Ordinal }).strict();
|
|
9480
|
+
var Binding = { workspaceId: Id, sourceId: Id, hostId: Id };
|
|
9481
|
+
var SourceRowSchema = object({
|
|
9482
|
+
address: Address,
|
|
9483
|
+
version: Id,
|
|
9484
|
+
scope: Id,
|
|
9485
|
+
columns: object({
|
|
9486
|
+
title: string2().max(4096),
|
|
9487
|
+
state: Id,
|
|
9488
|
+
owner: Id.nullable(),
|
|
9489
|
+
parent: Address.nullable(),
|
|
9490
|
+
labels: array(Id).max(256),
|
|
9491
|
+
url: string2().max(2048).nullable(),
|
|
9492
|
+
updatedAt: string2().max(64).nullable()
|
|
9493
|
+
}).strict(),
|
|
9494
|
+
extra: unknown()
|
|
9495
|
+
}).strict();
|
|
9496
|
+
var SourceObservationSchema = object({
|
|
9497
|
+
position: SourcePositionSchema,
|
|
9498
|
+
change: discriminatedUnion("op", [
|
|
9499
|
+
object({ op: literal("upsert"), row: SourceRowSchema }).strict(),
|
|
9500
|
+
object({ op: literal("delete"), address: Address }).strict()
|
|
9501
|
+
])
|
|
9502
|
+
}).strict();
|
|
9503
|
+
var ReadArgs = object({ address: Address }).strict();
|
|
9504
|
+
var ListArgs = object({ page: string2().min(1).max(4096).optional(), limit: number2().int().min(1).max(SOURCE_LIST_LIMIT_MAX) }).strict();
|
|
9505
|
+
var AssignArgs = object({ address: Address, ownerMemberId: Id, expectedOwner: Id.nullable(), intentId: Id }).strict();
|
|
9506
|
+
var WatchArgs = object({ on: boolean2() }).strict();
|
|
9507
|
+
var Caller = object({ memberId: Id }).strict();
|
|
9508
|
+
var request = (op, args) => object({ ...Binding, caller: Caller, op: literal(op), args }).strict();
|
|
9509
|
+
var SourceRequestSchema = discriminatedUnion("op", [
|
|
9510
|
+
request("read", ReadArgs),
|
|
9511
|
+
request("list", ListArgs),
|
|
9512
|
+
request("assign", AssignArgs),
|
|
9513
|
+
request("watch", WatchArgs)
|
|
9514
|
+
]);
|
|
9515
|
+
var SourceListPageSchema = object({
|
|
9516
|
+
generation: Id,
|
|
9517
|
+
snapshot: Id,
|
|
9518
|
+
base: SourcePositionSchema,
|
|
9519
|
+
rows: array(SourceRowSchema),
|
|
9520
|
+
next: string2().min(1).max(4096).nullable(),
|
|
9521
|
+
complete: boolean2()
|
|
9522
|
+
}).strict().refine((page) => page.rows.length > 0 || page.complete && page.next === null, { message: "a page carries at least one item" }).refine((page) => page.complete === (page.next === null), { message: "a page is complete exactly when it has no next token" });
|
|
9523
|
+
var AssignReply = object({ intentId: Id, result: unknown(), observation: SourceObservationSchema }).strict();
|
|
9524
|
+
var BODIES = {
|
|
9525
|
+
read: object({ observation: SourceObservationSchema }).strict(),
|
|
9526
|
+
list: SourceListPageSchema,
|
|
9527
|
+
assign: AssignReply,
|
|
9528
|
+
watch: object({ on: boolean2() }).strict()
|
|
9529
|
+
};
|
|
9530
|
+
var ErrorCode = _enum(SOURCE_ERROR_CODES);
|
|
9531
|
+
var NOT_SOURCE_CODES = ["OPERATION_NOT_ALLOWLISTED", "INVALID_OPERATION_SHAPE"];
|
|
9532
|
+
var response = (op) => union([
|
|
9533
|
+
object({ ...Binding, op: literal(op), outcome: _enum(["applied", "noop"]), body: BODIES[op] }).strict(),
|
|
9534
|
+
object({ ...Binding, op: literal(op), outcome: literal("rejected"), code: ErrorCode, body: BODIES[op] }).strict(),
|
|
9535
|
+
object({ ...Binding, op: literal(op), outcome: _enum(["unknown", "refused"]), code: union([ErrorCode.exclude(["ITEM_TOO_LARGE"]), _enum(NOT_SOURCE_CODES)]) }).strict(),
|
|
9536
|
+
object({ ...Binding, op: literal(op), outcome: literal("refused"), code: literal("ITEM_TOO_LARGE"), item: Address }).strict()
|
|
9537
|
+
]);
|
|
9538
|
+
var SourceResponseSchema = union([response("read"), response("list"), response("assign"), response("watch")]);
|
|
9539
|
+
var SourcePushSchema = object({ ...Binding, position: SourcePositionSchema, observations: array(SourceObservationSchema).min(1) }).strict().refine((push) => {
|
|
9540
|
+
const { observations, position } = push;
|
|
9541
|
+
const last = observations[observations.length - 1];
|
|
9542
|
+
if (last === undefined || last.position.epoch !== position.epoch || last.position.ordinal !== position.ordinal)
|
|
9543
|
+
return false;
|
|
9544
|
+
return observations.every((one, index) => one.position.epoch === position.epoch && (index === 0 || BigInt(one.position.ordinal) === BigInt(observations[index - 1].position.ordinal) + 1n));
|
|
9545
|
+
}, { message: "a push is one epoch of consecutive ordinals, numbered by its last" });
|
|
9546
|
+
var SourceAckSchema = object({ applied: SourcePositionSchema.nullable(), seq: number2().int().min(0), relist: boolean2() }).strict();
|
|
8990
9547
|
// ../ahp/src/index.ts
|
|
8991
9548
|
var HOST_TO_CLOUD_EVENTS = [
|
|
8992
9549
|
"session.created",
|
|
@@ -9006,7 +9563,9 @@ var BOUNDED_ERROR_CODES = [
|
|
|
9006
9563
|
"INPUT_REQUIRED",
|
|
9007
9564
|
"APPROVAL_REQUIRED",
|
|
9008
9565
|
"VERSION_UNSUPPORTED",
|
|
9009
|
-
"WAKE_NOT_SENT"
|
|
9566
|
+
"WAKE_NOT_SENT",
|
|
9567
|
+
"TURN_TIMED_OUT",
|
|
9568
|
+
...SOURCE_ERROR_CODES
|
|
9010
9569
|
];
|
|
9011
9570
|
var WakeMetaSchema = object({
|
|
9012
9571
|
houseId: string2().min(1),
|
|
@@ -9241,6 +9800,25 @@ class ActiveTurnTokenVault {
|
|
|
9241
9800
|
}
|
|
9242
9801
|
|
|
9243
9802
|
// ../ahp-host/src/runtime.ts
|
|
9803
|
+
class UnmappedAgent extends Error {
|
|
9804
|
+
}
|
|
9805
|
+
|
|
9806
|
+
class FrameRefusal extends Error {
|
|
9807
|
+
}
|
|
9808
|
+
var UNSTARTABLE_AGENT_TEXT = [
|
|
9809
|
+
/No agent provider registered for: /u,
|
|
9810
|
+
/: this conversation has no matching configured ACP process authority/u
|
|
9811
|
+
];
|
|
9812
|
+
function cloudOperationFailureCode(error) {
|
|
9813
|
+
if (error instanceof FrameRefusal)
|
|
9814
|
+
return "INVALID_OPERATION_SHAPE";
|
|
9815
|
+
if (error instanceof UnmappedAgent)
|
|
9816
|
+
return "UNMAPPED_LOCAL_AGENT";
|
|
9817
|
+
const text = error instanceof Error ? error.message : String(error);
|
|
9818
|
+
if (UNSTARTABLE_AGENT_TEXT.some((pattern) => pattern.test(text)))
|
|
9819
|
+
return "UNMAPPED_LOCAL_AGENT";
|
|
9820
|
+
return "WAKE_NOT_SENT";
|
|
9821
|
+
}
|
|
9244
9822
|
var SUPPORTED_PROTOCOL = "1.1.0";
|
|
9245
9823
|
var REQUEST_TIMEOUT_MS = 15000;
|
|
9246
9824
|
var attemptKey = (deliveryId, turnId) => JSON.stringify([deliveryId, turnId]);
|
|
@@ -9414,7 +9992,7 @@ class WakePipe {
|
|
|
9414
9992
|
const id = frame?.id;
|
|
9415
9993
|
if (!this.#started || this.#storeError !== undefined) {
|
|
9416
9994
|
if (id !== undefined)
|
|
9417
|
-
this.cloud.answer(id, { ok: false, code: "
|
|
9995
|
+
this.cloud.answer(id, { ok: false, code: "WAKE_NOT_SENT" });
|
|
9418
9996
|
return;
|
|
9419
9997
|
}
|
|
9420
9998
|
if (frame?.method === "x-hightea/revoke") {
|
|
@@ -9433,7 +10011,7 @@ class WakePipe {
|
|
|
9433
10011
|
if (decision.operation === "session.create") {
|
|
9434
10012
|
const agent = this.options.agents.get(decision.meta.agentMemberId);
|
|
9435
10013
|
if (agent === undefined)
|
|
9436
|
-
throw new
|
|
10014
|
+
throw new UnmappedAgent("ahp-host: local agent mapping disappeared");
|
|
9437
10015
|
let winnerUri;
|
|
9438
10016
|
let sessionUri;
|
|
9439
10017
|
try {
|
|
@@ -9481,9 +10059,10 @@ class WakePipe {
|
|
|
9481
10059
|
if (id !== undefined)
|
|
9482
10060
|
this.cloud.answer(id, { ok: true });
|
|
9483
10061
|
} catch (error) {
|
|
9484
|
-
|
|
10062
|
+
const code = cloudOperationFailureCode(error);
|
|
10063
|
+
this.options.log.error(`ahp-host: cloud operation ${decision.operation} failed (${code}): ${String(error)}`);
|
|
9485
10064
|
if (id !== undefined)
|
|
9486
|
-
this.cloud.answer(id, { ok: false, code
|
|
10065
|
+
this.cloud.answer(id, { ok: false, code });
|
|
9487
10066
|
}
|
|
9488
10067
|
}
|
|
9489
10068
|
async#resumeSession(sessionUri) {
|
|
@@ -9516,7 +10095,7 @@ class WakePipe {
|
|
|
9516
10095
|
return;
|
|
9517
10096
|
}
|
|
9518
10097
|
if (this.options.tokenExpiresAt(token) === undefined)
|
|
9519
|
-
throw new
|
|
10098
|
+
throw new FrameRefusal("ahp-host: an agent token whose expiry cannot be read");
|
|
9520
10099
|
const running = this.#active.get(key);
|
|
9521
10100
|
if (running !== undefined) {
|
|
9522
10101
|
if (running.deliveryId === meta.deliveryId) {
|
|
@@ -9549,7 +10128,7 @@ class WakePipe {
|
|
|
9549
10128
|
}
|
|
9550
10129
|
const expiresAtMs = this.options.tokenExpiresAt(token);
|
|
9551
10130
|
if (expiresAtMs === undefined)
|
|
9552
|
-
throw new
|
|
10131
|
+
throw new FrameRefusal("ahp-host: an agent token whose expiry cannot be read");
|
|
9553
10132
|
this.vault.bindActiveTurn({
|
|
9554
10133
|
sessionId: mapped.sessionUri,
|
|
9555
10134
|
chatId: mapped.chatUri,
|
|
@@ -9935,7 +10514,7 @@ function createCloudLink(options) {
|
|
|
9935
10514
|
fail("CONTROL_SOCKET_CLOSED", false);
|
|
9936
10515
|
closeSocket(current?.socket);
|
|
9937
10516
|
};
|
|
9938
|
-
const message = (event) => {
|
|
10517
|
+
const message = (event, connection) => {
|
|
9939
10518
|
let frame;
|
|
9940
10519
|
try {
|
|
9941
10520
|
frame = JSON.parse(String(event.data));
|
|
@@ -9962,7 +10541,7 @@ function createCloudLink(options) {
|
|
|
9962
10541
|
return;
|
|
9963
10542
|
}
|
|
9964
10543
|
}
|
|
9965
|
-
receive(frame);
|
|
10544
|
+
receive(frame, connection);
|
|
9966
10545
|
};
|
|
9967
10546
|
const connect = () => {
|
|
9968
10547
|
if (stopped)
|
|
@@ -10001,9 +10580,10 @@ function createCloudLink(options) {
|
|
|
10001
10580
|
lost("CONTROL_SOCKET_ERROR");
|
|
10002
10581
|
});
|
|
10003
10582
|
connection.socket = socket;
|
|
10583
|
+
const own = { send: (value) => sendThrough(connection, value) };
|
|
10004
10584
|
socket.addEventListener("message", (event) => {
|
|
10005
10585
|
if (!stopped && current === connection && connection.live)
|
|
10006
|
-
message(event);
|
|
10586
|
+
message(event, own);
|
|
10007
10587
|
});
|
|
10008
10588
|
socket.addEventListener("error", () => lost("CONTROL_SOCKET_ERROR"));
|
|
10009
10589
|
socket.addEventListener("close", (event) => {
|
|
@@ -10017,7 +10597,7 @@ function createCloudLink(options) {
|
|
|
10017
10597
|
return;
|
|
10018
10598
|
connection.opened = true;
|
|
10019
10599
|
wait = 1000;
|
|
10020
|
-
options.opened?.(
|
|
10600
|
+
options.opened?.(own);
|
|
10021
10601
|
};
|
|
10022
10602
|
socket.addEventListener("open", opened);
|
|
10023
10603
|
if (stopped || !connection.live)
|
|
@@ -10028,26 +10608,698 @@ function createCloudLink(options) {
|
|
|
10028
10608
|
lost("CONTROL_SOCKET_ERROR");
|
|
10029
10609
|
}
|
|
10030
10610
|
};
|
|
10611
|
+
const request = (id, method, params) => new Promise((resolve, reject) => {
|
|
10612
|
+
const timer = setTimeout(() => {
|
|
10613
|
+
pending.delete(id);
|
|
10614
|
+
log.error("hightea-pipe: CONTROL_REQUEST_TIMEOUT");
|
|
10615
|
+
reject(new Error("CONTROL_REQUEST_TIMEOUT"));
|
|
10616
|
+
}, REQUEST_TIMEOUT_MS2);
|
|
10617
|
+
pending.set(id, { resolve, reject, timer });
|
|
10618
|
+
if (!send({ jsonrpc: "2.0", id, method, params })) {
|
|
10619
|
+
pending.delete(id);
|
|
10620
|
+
clearTimeout(timer);
|
|
10621
|
+
reject(new Error("CONTROL_SOCKET_UNAVAILABLE"));
|
|
10622
|
+
}
|
|
10623
|
+
});
|
|
10031
10624
|
queueMicrotask(connect);
|
|
10032
10625
|
return {
|
|
10033
10626
|
send,
|
|
10034
10627
|
close,
|
|
10035
10628
|
answer: (id, outcome) => send(outcome.ok ? { jsonrpc: "2.0", id, result: null } : { jsonrpc: "2.0", id, error: { code: -32000, message: outcome.code } }),
|
|
10036
10629
|
status: (frame) => send({ jsonrpc: "2.0", method: "x-hightea/status", params: frame }),
|
|
10037
|
-
renewToken: (params) =>
|
|
10038
|
-
|
|
10039
|
-
|
|
10040
|
-
|
|
10041
|
-
|
|
10042
|
-
|
|
10043
|
-
|
|
10044
|
-
|
|
10045
|
-
|
|
10046
|
-
|
|
10047
|
-
|
|
10048
|
-
|
|
10049
|
-
|
|
10050
|
-
|
|
10630
|
+
renewToken: (params) => request(`renew:${randomUUID()}`, "x-hightea/renewToken", params),
|
|
10631
|
+
sourcePush: (params) => request(`push:${randomUUID()}`, "x-hightea/source/push", params)
|
|
10632
|
+
};
|
|
10633
|
+
}
|
|
10634
|
+
|
|
10635
|
+
// ../ahp-host/src/km-source.ts
|
|
10636
|
+
import { spawn } from "node:child_process";
|
|
10637
|
+
import { createHash as createHash2, randomUUID as randomUUID2 } from "node:crypto";
|
|
10638
|
+
import { createInterface } from "node:readline";
|
|
10639
|
+
var OUTPUT_MAX_BYTES = 16 * 1024 * 1024;
|
|
10640
|
+
var GENERATIONS_KEPT = 8;
|
|
10641
|
+
var GENERATION_MAX_MS = 15 * 60000;
|
|
10642
|
+
var UNACKED_MAX = 1e4;
|
|
10643
|
+
var FEED_RESTART_MS = 5000;
|
|
10644
|
+
|
|
10645
|
+
class ItemTooLarge extends Error {
|
|
10646
|
+
item;
|
|
10647
|
+
constructor(item) {
|
|
10648
|
+
super(`ITEM_TOO_LARGE: ${item}`);
|
|
10649
|
+
this.item = item;
|
|
10650
|
+
}
|
|
10651
|
+
}
|
|
10652
|
+
|
|
10653
|
+
class KmSource {
|
|
10654
|
+
sourceId;
|
|
10655
|
+
#epoch = "e_" + randomUUID2();
|
|
10656
|
+
#ordinal = 0n;
|
|
10657
|
+
#reads = 0;
|
|
10658
|
+
#current;
|
|
10659
|
+
#kept = new Map;
|
|
10660
|
+
#reading;
|
|
10661
|
+
#readingNo = 0;
|
|
10662
|
+
#dirty = false;
|
|
10663
|
+
#hintTimer;
|
|
10664
|
+
#queue = [];
|
|
10665
|
+
#inFlight = false;
|
|
10666
|
+
#pushTimer;
|
|
10667
|
+
#lastPushAt = -Infinity;
|
|
10668
|
+
#pushFailures = 0;
|
|
10669
|
+
#retryAt = -Infinity;
|
|
10670
|
+
#watching = true;
|
|
10671
|
+
#stop = new AbortController;
|
|
10672
|
+
#feed;
|
|
10673
|
+
#o;
|
|
10674
|
+
#run;
|
|
10675
|
+
#coalesce;
|
|
10676
|
+
#now;
|
|
10677
|
+
#native;
|
|
10678
|
+
#duty;
|
|
10679
|
+
#minGap;
|
|
10680
|
+
#readEndedAt = -Infinity;
|
|
10681
|
+
#readTookMs = 0;
|
|
10682
|
+
#assigning = new Map;
|
|
10683
|
+
constructor(options) {
|
|
10684
|
+
this.#o = options;
|
|
10685
|
+
this.sourceId = options.config.sourceId;
|
|
10686
|
+
this.#run = options.run ?? runChild;
|
|
10687
|
+
this.#coalesce = options.coalesceMs ?? 2000;
|
|
10688
|
+
this.#duty = options.readDuty ?? 0.1;
|
|
10689
|
+
if (!(this.#duty > 0 && this.#duty <= 1))
|
|
10690
|
+
throw new Error(`readDuty must be in (0, 1], not ${this.#duty}`);
|
|
10691
|
+
this.#minGap = options.config.minReadGapMs ?? 0;
|
|
10692
|
+
if (!(Number.isFinite(this.#minGap) && this.#minGap >= 0)) {
|
|
10693
|
+
throw new Error(`minReadGapMs must be a finite number of milliseconds, 0 or more, not ${this.#minGap}`);
|
|
10694
|
+
}
|
|
10695
|
+
this.#now = options.now ?? Date.now;
|
|
10696
|
+
this.#native = new Map(Object.entries(options.config.members).map(([member, owner]) => [owner, member]));
|
|
10697
|
+
}
|
|
10698
|
+
get epoch() {
|
|
10699
|
+
return this.#epoch;
|
|
10700
|
+
}
|
|
10701
|
+
start() {
|
|
10702
|
+
this.#read().catch((error) => this.#o.log.error(`hightea-source: the first read of ${this.sourceId} failed: ${describe2(error)}`));
|
|
10703
|
+
this.#feed = this.#follow();
|
|
10704
|
+
}
|
|
10705
|
+
async stop() {
|
|
10706
|
+
this.#stop.abort();
|
|
10707
|
+
if (this.#hintTimer !== undefined)
|
|
10708
|
+
clearTimeout(this.#hintTimer);
|
|
10709
|
+
if (this.#pushTimer !== undefined)
|
|
10710
|
+
clearTimeout(this.#pushTimer);
|
|
10711
|
+
await this.#feed;
|
|
10712
|
+
await this.#reading?.catch(() => {
|
|
10713
|
+
return;
|
|
10714
|
+
});
|
|
10715
|
+
}
|
|
10716
|
+
hint() {
|
|
10717
|
+
if (this.#stop.signal.aborted)
|
|
10718
|
+
return;
|
|
10719
|
+
if (this.#reading !== undefined || this.#hintTimer !== undefined) {
|
|
10720
|
+
this.#dirty = true;
|
|
10721
|
+
return;
|
|
10722
|
+
}
|
|
10723
|
+
const rested = this.#readEndedAt + Math.max(this.#readTookMs * (1 / this.#duty - 1), this.#minGap) - this.#now();
|
|
10724
|
+
this.#hintTimer = setTimeout(() => {
|
|
10725
|
+
this.#hintTimer = undefined;
|
|
10726
|
+
this.#read().catch((error) => this.#o.log.error(`hightea-source: reading ${this.sourceId} after a change failed: ${describe2(error)}`));
|
|
10727
|
+
}, Math.max(this.#coalesce, rested));
|
|
10728
|
+
}
|
|
10729
|
+
async handle(request) {
|
|
10730
|
+
const binding = { workspaceId: request.workspaceId, sourceId: request.sourceId, hostId: request.hostId };
|
|
10731
|
+
const refuse = (code, outcome = "refused") => ({ ...binding, op: request.op, outcome, code });
|
|
10732
|
+
if (request.sourceId !== this.sourceId)
|
|
10733
|
+
return refuse("SOURCE_UNBOUND");
|
|
10734
|
+
if (!this.#o.config.members[request.caller.memberId])
|
|
10735
|
+
return refuse("NOT_MAPPED");
|
|
10736
|
+
try {
|
|
10737
|
+
switch (request.op) {
|
|
10738
|
+
case "watch":
|
|
10739
|
+
this.#watching = request.args.on;
|
|
10740
|
+
if (this.#watching)
|
|
10741
|
+
this.#pump();
|
|
10742
|
+
return { ...binding, op: "watch", outcome: "applied", body: { on: this.#watching } };
|
|
10743
|
+
case "list":
|
|
10744
|
+
return { ...binding, op: "list", outcome: "applied", body: await this.#page(request.args) };
|
|
10745
|
+
case "read": {
|
|
10746
|
+
const read = await this.#latest();
|
|
10747
|
+
return { ...binding, op: "read", outcome: "applied", body: { observation: this.#observed(read, request.args.address) } };
|
|
10748
|
+
}
|
|
10749
|
+
case "assign": {
|
|
10750
|
+
const running = this.#assigning.get(request.args.intentId);
|
|
10751
|
+
if (running !== undefined)
|
|
10752
|
+
return await running;
|
|
10753
|
+
const assigning = this.#assign(request, binding).finally(() => this.#assigning.delete(request.args.intentId));
|
|
10754
|
+
this.#assigning.set(request.args.intentId, assigning);
|
|
10755
|
+
return await assigning;
|
|
10756
|
+
}
|
|
10757
|
+
}
|
|
10758
|
+
} catch (error) {
|
|
10759
|
+
if (error instanceof ItemTooLarge)
|
|
10760
|
+
return { ...binding, op: request.op, outcome: "refused", code: "ITEM_TOO_LARGE", item: error.item };
|
|
10761
|
+
this.#o.log.error(`hightea-source: ${request.op} on ${this.sourceId} failed: ${describe2(error)}`);
|
|
10762
|
+
return refuse("SOURCE_UNAVAILABLE", "unknown");
|
|
10763
|
+
}
|
|
10764
|
+
}
|
|
10765
|
+
async#page(args) {
|
|
10766
|
+
const token = args.page === undefined ? undefined : decodeToken(args.page);
|
|
10767
|
+
const kept = token === undefined ? undefined : this.#kept.get(token.generation);
|
|
10768
|
+
const read = kept ?? await this.#read();
|
|
10769
|
+
const offset = kept === undefined ? 0 : token.offset;
|
|
10770
|
+
const rows = [];
|
|
10771
|
+
let bytes = 0;
|
|
10772
|
+
let next = offset;
|
|
10773
|
+
while (next < read.rows.length && rows.length < args.limit) {
|
|
10774
|
+
const row = read.rows[next].row;
|
|
10775
|
+
const size = sourceFrameBytes(row);
|
|
10776
|
+
if (size > SOURCE_PAGE_BUDGET_BYTES) {
|
|
10777
|
+
if (rows.length === 0)
|
|
10778
|
+
throw new ItemTooLarge(row.address);
|
|
10779
|
+
break;
|
|
10780
|
+
}
|
|
10781
|
+
if (rows.length > 0 && bytes + size > SOURCE_PAGE_BUDGET_BYTES)
|
|
10782
|
+
break;
|
|
10783
|
+
rows.push(row);
|
|
10784
|
+
bytes += size;
|
|
10785
|
+
next++;
|
|
10786
|
+
}
|
|
10787
|
+
const complete = next >= read.rows.length;
|
|
10788
|
+
return {
|
|
10789
|
+
generation: read.generation,
|
|
10790
|
+
snapshot: read.snapshot,
|
|
10791
|
+
base: { epoch: read.epoch, ordinal: String(read.base) },
|
|
10792
|
+
rows,
|
|
10793
|
+
next: complete ? null : encodeToken(read.generation, next),
|
|
10794
|
+
complete
|
|
10795
|
+
};
|
|
10796
|
+
}
|
|
10797
|
+
async#assign(request, binding) {
|
|
10798
|
+
const { address, ownerMemberId, expectedOwner, intentId } = request.args;
|
|
10799
|
+
const seat = this.#o.config.members[ownerMemberId];
|
|
10800
|
+
const expected = expectedOwner === null ? null : this.#nativeOf(expectedOwner);
|
|
10801
|
+
if (seat === undefined || expected === undefined)
|
|
10802
|
+
return { ...binding, op: "assign", outcome: "refused", code: "NOT_MAPPED" };
|
|
10803
|
+
const key = this.#keyOf(address);
|
|
10804
|
+
if (key === undefined)
|
|
10805
|
+
return { ...binding, op: "assign", outcome: "refused", code: "INVALID_ADDRESS" };
|
|
10806
|
+
const requester = { authority: `hightea:${request.workspaceId}`, subject: request.caller.memberId };
|
|
10807
|
+
const ran = await this.#yrd([
|
|
10808
|
+
"issues",
|
|
10809
|
+
"assign",
|
|
10810
|
+
key,
|
|
10811
|
+
seat,
|
|
10812
|
+
"--id",
|
|
10813
|
+
intentId,
|
|
10814
|
+
"--expected-owner",
|
|
10815
|
+
JSON.stringify(expected),
|
|
10816
|
+
"--requested-by",
|
|
10817
|
+
JSON.stringify(requester)
|
|
10818
|
+
]);
|
|
10819
|
+
const read = await this.#readAfter();
|
|
10820
|
+
const observation = this.#observed(read, address);
|
|
10821
|
+
const owner = observation.change.op === "upsert" ? observation.change.row.columns.owner : null;
|
|
10822
|
+
if (ran.code === 0)
|
|
10823
|
+
return { ...binding, op: "assign", outcome: "applied", body: { intentId, result: { owner }, observation } };
|
|
10824
|
+
const current = observation.change.op === "upsert" ? this.#nativeOf(observation.change.row.columns.owner ?? "") : null;
|
|
10825
|
+
if (current === seat)
|
|
10826
|
+
return { ...binding, op: "assign", outcome: "applied", body: { intentId, result: { owner }, observation } };
|
|
10827
|
+
if ((current ?? null) !== expected) {
|
|
10828
|
+
return { ...binding, op: "assign", outcome: "rejected", code: "STALE_EXPECTATION", body: { intentId, result: { owner }, observation } };
|
|
10829
|
+
}
|
|
10830
|
+
this.#o.log.error(`hightea-source: assign ${intentId} on ${this.sourceId} exited ${ran.code ?? "on a signal"}: ${firstCode(ran.stderr)}`);
|
|
10831
|
+
return { ...binding, op: "assign", outcome: "unknown", code: "SOURCE_UNAVAILABLE" };
|
|
10832
|
+
}
|
|
10833
|
+
#observed(read, address) {
|
|
10834
|
+
const held = read.byAddress.get(address);
|
|
10835
|
+
if (held === undefined)
|
|
10836
|
+
return { position: { epoch: read.epoch, ordinal: String(read.base) }, change: { op: "delete", address } };
|
|
10837
|
+
return { position: { epoch: read.epoch, ordinal: String(held.at) }, change: { op: "upsert", row: held.row } };
|
|
10838
|
+
}
|
|
10839
|
+
async#readAfter() {
|
|
10840
|
+
const mark = this.#reads;
|
|
10841
|
+
for (;; ) {
|
|
10842
|
+
const reading = this.#reading;
|
|
10843
|
+
if (reading === undefined)
|
|
10844
|
+
return this.#read();
|
|
10845
|
+
if (this.#readingNo > mark)
|
|
10846
|
+
return reading;
|
|
10847
|
+
await reading.catch(() => {
|
|
10848
|
+
return;
|
|
10849
|
+
});
|
|
10850
|
+
}
|
|
10851
|
+
}
|
|
10852
|
+
async#latest() {
|
|
10853
|
+
return this.#current ?? await this.#read();
|
|
10854
|
+
}
|
|
10855
|
+
#read() {
|
|
10856
|
+
if (this.#reading !== undefined) {
|
|
10857
|
+
this.#dirty = true;
|
|
10858
|
+
return this.#reading;
|
|
10859
|
+
}
|
|
10860
|
+
if (this.#hintTimer !== undefined) {
|
|
10861
|
+
clearTimeout(this.#hintTimer);
|
|
10862
|
+
this.#hintTimer = undefined;
|
|
10863
|
+
}
|
|
10864
|
+
this.#dirty = false;
|
|
10865
|
+
const began = this.#now();
|
|
10866
|
+
const reading = this.#readOnce().finally(() => {
|
|
10867
|
+
this.#reading = undefined;
|
|
10868
|
+
this.#readEndedAt = this.#now();
|
|
10869
|
+
this.#readTookMs = this.#readEndedAt - began;
|
|
10870
|
+
if (this.#dirty && !this.#stop.signal.aborted) {
|
|
10871
|
+
this.#dirty = false;
|
|
10872
|
+
this.hint();
|
|
10873
|
+
}
|
|
10874
|
+
});
|
|
10875
|
+
this.#reading = reading;
|
|
10876
|
+
this.#readingNo = this.#reads;
|
|
10877
|
+
return reading;
|
|
10878
|
+
}
|
|
10879
|
+
async#readOnce() {
|
|
10880
|
+
const generation = `${this.#epoch}.${++this.#reads}`;
|
|
10881
|
+
const ran = await this.#yrd(["issues", "list"]);
|
|
10882
|
+
if (ran.code !== 0)
|
|
10883
|
+
throw new Error(`list exited ${ran.code ?? "on a signal"}: ${firstCode(ran.stderr)}`);
|
|
10884
|
+
const fresh = new Map;
|
|
10885
|
+
for (const one of kmList(ran.stdout)) {
|
|
10886
|
+
const row = this.#row(one);
|
|
10887
|
+
fresh.set(row.address, { key: one.id, row });
|
|
10888
|
+
}
|
|
10889
|
+
return this.#diff(generation, contentRevision(fresh), fresh);
|
|
10890
|
+
}
|
|
10891
|
+
#diff(generation, snapshot, fresh) {
|
|
10892
|
+
const last = this.#current;
|
|
10893
|
+
const byAddress = new Map;
|
|
10894
|
+
const addresses = [...new Set([...fresh.keys(), ...last?.byAddress.keys() ?? []])].sort();
|
|
10895
|
+
const observations = [];
|
|
10896
|
+
for (const address of addresses) {
|
|
10897
|
+
const now = fresh.get(address);
|
|
10898
|
+
const was = last?.byAddress.get(address);
|
|
10899
|
+
if (now === undefined) {
|
|
10900
|
+
observations.push({ position: this.#next(), change: { op: "delete", address } });
|
|
10901
|
+
continue;
|
|
10902
|
+
}
|
|
10903
|
+
if (last !== undefined && (was === undefined || was.row.version !== now.row.version || !sameColumns(was.row, now.row))) {
|
|
10904
|
+
const position = this.#next();
|
|
10905
|
+
observations.push({ position, change: { op: "upsert", row: now.row } });
|
|
10906
|
+
byAddress.set(address, { key: now.key, row: now.row, at: BigInt(position.ordinal) });
|
|
10907
|
+
} else {
|
|
10908
|
+
byAddress.set(address, { key: now.key, row: now.row, at: was?.at ?? 0n });
|
|
10909
|
+
}
|
|
10910
|
+
}
|
|
10911
|
+
if (last === undefined && byAddress.size > 0) {
|
|
10912
|
+
const first = byAddress.get(addresses[0]);
|
|
10913
|
+
const position = this.#next();
|
|
10914
|
+
observations.push({ position, change: { op: "upsert", row: first.row } });
|
|
10915
|
+
byAddress.set(first.row.address, { ...first, at: BigInt(position.ordinal) });
|
|
10916
|
+
}
|
|
10917
|
+
const rows = [...byAddress.values()];
|
|
10918
|
+
const read = { generation, snapshot, base: this.#ordinal, epoch: this.#epoch, rows, byAddress, at: this.#now() };
|
|
10919
|
+
this.#current = read;
|
|
10920
|
+
this.#kept.set(generation, read);
|
|
10921
|
+
for (const [name, kept] of this.#kept) {
|
|
10922
|
+
if (this.#kept.size > GENERATIONS_KEPT || this.#now() - kept.at > GENERATION_MAX_MS)
|
|
10923
|
+
this.#kept.delete(name);
|
|
10924
|
+
}
|
|
10925
|
+
this.#enqueue(observations);
|
|
10926
|
+
return read;
|
|
10927
|
+
}
|
|
10928
|
+
#next() {
|
|
10929
|
+
this.#ordinal++;
|
|
10930
|
+
return { epoch: this.#epoch, ordinal: String(this.#ordinal) };
|
|
10931
|
+
}
|
|
10932
|
+
#row(one) {
|
|
10933
|
+
const address = `issue:${this.sourceId}/${one.id}`;
|
|
10934
|
+
const owner = one.owner.trim();
|
|
10935
|
+
const row = {
|
|
10936
|
+
address,
|
|
10937
|
+
version: one.version,
|
|
10938
|
+
scope: "all",
|
|
10939
|
+
columns: {
|
|
10940
|
+
title: one.title,
|
|
10941
|
+
state: one.status.trim() === "" ? "none" : one.status.trim(),
|
|
10942
|
+
owner: owner === "" ? null : this.#native.get(owner) ?? `km:${owner}`,
|
|
10943
|
+
parent: null,
|
|
10944
|
+
labels: [],
|
|
10945
|
+
url: null,
|
|
10946
|
+
updatedAt: null
|
|
10947
|
+
},
|
|
10948
|
+
extra: { path: one.id, id: one.id }
|
|
10949
|
+
};
|
|
10950
|
+
if (one.title.length > 4096 || address.length > 1024 || sourceFrameBytes(row) > SOURCE_PAGE_BUDGET_BYTES)
|
|
10951
|
+
throw new ItemTooLarge(address);
|
|
10952
|
+
return row;
|
|
10953
|
+
}
|
|
10954
|
+
#nativeOf(owner) {
|
|
10955
|
+
if (owner.startsWith("km:"))
|
|
10956
|
+
return owner.slice(3);
|
|
10957
|
+
return this.#o.config.members[owner];
|
|
10958
|
+
}
|
|
10959
|
+
#keyOf(address) {
|
|
10960
|
+
const prefix = `issue:${this.sourceId}/`;
|
|
10961
|
+
return address.startsWith(prefix) && address.length > prefix.length ? address.slice(prefix.length) : undefined;
|
|
10962
|
+
}
|
|
10963
|
+
#enqueue(observations) {
|
|
10964
|
+
this.#queue.push(...observations);
|
|
10965
|
+
if (this.#queue.length > UNACKED_MAX) {
|
|
10966
|
+
this.#o.log.error(`hightea-source: ${this.sourceId} has more than ${UNACKED_MAX} unacknowledged observations; starting a new epoch`);
|
|
10967
|
+
this.#queue.length = 0;
|
|
10968
|
+
this.#epoch = "e_" + randomUUID2();
|
|
10969
|
+
this.#ordinal = 0n;
|
|
10970
|
+
this.#kept.clear();
|
|
10971
|
+
const last = this.#current;
|
|
10972
|
+
this.#current = undefined;
|
|
10973
|
+
const fresh = new Map(last.rows.map((held) => [held.row.address, { key: held.key, row: held.row }]));
|
|
10974
|
+
this.#diff(`${this.#epoch}.${++this.#reads}`, last.snapshot, fresh);
|
|
10975
|
+
return;
|
|
10976
|
+
}
|
|
10977
|
+
this.#pump();
|
|
10978
|
+
}
|
|
10979
|
+
#pump() {
|
|
10980
|
+
if (!this.#watching || this.#inFlight || this.#queue.length === 0 || this.#pushTimer !== undefined || this.#stop.signal.aborted)
|
|
10981
|
+
return;
|
|
10982
|
+
const retryIn = this.#retryAt - this.#now();
|
|
10983
|
+
if (retryIn > 0) {
|
|
10984
|
+
this.#pushTimer = setTimeout(() => {
|
|
10985
|
+
this.#pushTimer = undefined;
|
|
10986
|
+
this.#pump();
|
|
10987
|
+
}, retryIn);
|
|
10988
|
+
return;
|
|
10989
|
+
}
|
|
10990
|
+
const wait = this.#lastPushAt + this.#coalesce - this.#now();
|
|
10991
|
+
if (wait > 0) {
|
|
10992
|
+
this.#pushTimer = setTimeout(() => {
|
|
10993
|
+
this.#pushTimer = undefined;
|
|
10994
|
+
this.#pump();
|
|
10995
|
+
}, wait);
|
|
10996
|
+
return;
|
|
10997
|
+
}
|
|
10998
|
+
const batch = [];
|
|
10999
|
+
let bytes = 1024;
|
|
11000
|
+
for (const one of this.#queue) {
|
|
11001
|
+
const size = sourceFrameBytes(one);
|
|
11002
|
+
if (batch.length > 0 && bytes + size > SOURCE_FRAME_MAX_BYTES)
|
|
11003
|
+
break;
|
|
11004
|
+
batch.push(one);
|
|
11005
|
+
bytes += size;
|
|
11006
|
+
}
|
|
11007
|
+
const last = batch[batch.length - 1];
|
|
11008
|
+
const epoch = this.#epoch;
|
|
11009
|
+
this.#inFlight = true;
|
|
11010
|
+
this.#lastPushAt = this.#now();
|
|
11011
|
+
this.#o.push({ workspaceId: this.#o.houseId, sourceId: this.sourceId, hostId: this.#o.hostId, position: last.position, observations: batch }).then((answer) => {
|
|
11012
|
+
if (this.#pushFailures > 0) {
|
|
11013
|
+
this.#o.log.info?.(`hightea-source: a push of ${this.sourceId} is answered again after ${this.#pushFailures} refusal(s)`);
|
|
11014
|
+
this.#pushFailures = 0;
|
|
11015
|
+
this.#retryAt = -Infinity;
|
|
11016
|
+
}
|
|
11017
|
+
const ack = SourceAckSchema.safeParse(answer);
|
|
11018
|
+
if (!ack.success) {
|
|
11019
|
+
this.#o.log.error(`hightea-source: the workspace answered a push of ${this.sourceId} outside its schema`);
|
|
11020
|
+
return;
|
|
11021
|
+
}
|
|
11022
|
+
const applied = ack.data.applied;
|
|
11023
|
+
if (applied === null || applied.epoch !== epoch || applied.epoch !== this.#epoch)
|
|
11024
|
+
return;
|
|
11025
|
+
const through = BigInt(applied.ordinal);
|
|
11026
|
+
let retire = 0;
|
|
11027
|
+
while (retire < this.#queue.length && BigInt(this.#queue[retire].position.ordinal) <= through)
|
|
11028
|
+
retire++;
|
|
11029
|
+
this.#queue.splice(0, retire);
|
|
11030
|
+
}, (error) => {
|
|
11031
|
+
this.#pushFailures++;
|
|
11032
|
+
const delay = Math.min(this.#coalesce * 2 ** Math.min(this.#pushFailures - 1, 5), 60000);
|
|
11033
|
+
this.#retryAt = this.#now() + delay;
|
|
11034
|
+
if (this.#pushFailures === 1)
|
|
11035
|
+
this.#o.log.error(`hightea-source: a push of ${this.sourceId} was not answered: ${describe2(error)}; retrying with backoff, next in ${Math.round(delay / 1000)} s`);
|
|
11036
|
+
}).finally(() => {
|
|
11037
|
+
this.#inFlight = false;
|
|
11038
|
+
this.#pump();
|
|
11039
|
+
});
|
|
11040
|
+
}
|
|
11041
|
+
async#yrd(args) {
|
|
11042
|
+
const { config } = this.#o;
|
|
11043
|
+
try {
|
|
11044
|
+
return await this.#run([...config.yrd, ...args, "--project-root", config.projectRoot], { timeoutMs: this.#o.childTimeoutMs ?? 120000, signal: this.#stop.signal });
|
|
11045
|
+
} catch (error) {
|
|
11046
|
+
throw new Error(`the yrd command ${config.yrd[0] ?? "(none)"} could not run: ${describe2(error)}`);
|
|
11047
|
+
}
|
|
11048
|
+
}
|
|
11049
|
+
async#follow() {
|
|
11050
|
+
const follow = this.#o.follow ?? followChild;
|
|
11051
|
+
const signal = this.#stop.signal;
|
|
11052
|
+
while (!signal.aborted) {
|
|
11053
|
+
const since = String(this.#now());
|
|
11054
|
+
try {
|
|
11055
|
+
const ended = await follow([...this.#o.config.feed, "--since", since], { signal, onLine: () => this.hint() });
|
|
11056
|
+
if (signal.aborted)
|
|
11057
|
+
return;
|
|
11058
|
+
this.#o.log.error(`hightea-source: km's change feed for ${this.sourceId} ended (${ended.code ?? "signal"}): ${firstCode(ended.stderr)}`);
|
|
11059
|
+
} catch (error) {
|
|
11060
|
+
if (signal.aborted)
|
|
11061
|
+
return;
|
|
11062
|
+
this.#o.log.error(`hightea-source: km's change feed for ${this.sourceId} could not run: ${describe2(error)}`);
|
|
11063
|
+
}
|
|
11064
|
+
this.hint();
|
|
11065
|
+
await new Promise((resolve) => {
|
|
11066
|
+
const timer = setTimeout(resolve, FEED_RESTART_MS);
|
|
11067
|
+
signal.addEventListener("abort", () => {
|
|
11068
|
+
clearTimeout(timer);
|
|
11069
|
+
resolve();
|
|
11070
|
+
}, { once: true });
|
|
11071
|
+
});
|
|
11072
|
+
}
|
|
11073
|
+
}
|
|
11074
|
+
}
|
|
11075
|
+
function kmList(stdout) {
|
|
11076
|
+
const envelope = JSON.parse(stdout);
|
|
11077
|
+
if (envelope.verb !== "list" || !Array.isArray(envelope.result))
|
|
11078
|
+
throw new Error("list answered outside km's list shape");
|
|
11079
|
+
return envelope.result.map((one) => {
|
|
11080
|
+
const row = one;
|
|
11081
|
+
const text = (value) => typeof value === "string";
|
|
11082
|
+
if (!text(row.id) || !text(row.title) || !text(row.status) || !text(row.owner)) {
|
|
11083
|
+
throw new Error("list answered a row outside the declared columns (id, title, status, owner)");
|
|
11084
|
+
}
|
|
11085
|
+
if ("content" in row || "description" in row)
|
|
11086
|
+
throw new Error("list answered a row carrying the issue body");
|
|
11087
|
+
const columns = { id: row.id, title: row.title, status: row.status, owner: row.owner };
|
|
11088
|
+
return { ...columns, version: contentVersion(columns) };
|
|
11089
|
+
});
|
|
11090
|
+
}
|
|
11091
|
+
function contentVersion(row) {
|
|
11092
|
+
return createHash2("sha256").update(`${row.id}\x00${row.title}\x00${row.status}\x00${row.owner}`).digest("hex").slice(0, 16);
|
|
11093
|
+
}
|
|
11094
|
+
function contentRevision(rows) {
|
|
11095
|
+
const hash = createHash2("sha256");
|
|
11096
|
+
for (const address of [...rows.keys()].sort())
|
|
11097
|
+
hash.update(`${address}\x00${rows.get(address).row.version}
|
|
11098
|
+
`);
|
|
11099
|
+
return hash.digest("hex");
|
|
11100
|
+
}
|
|
11101
|
+
function sameColumns(a, b) {
|
|
11102
|
+
return JSON.stringify(a.columns) === JSON.stringify(b.columns);
|
|
11103
|
+
}
|
|
11104
|
+
function encodeToken(generation, offset) {
|
|
11105
|
+
return Buffer.from(JSON.stringify([generation, offset])).toString("base64url");
|
|
11106
|
+
}
|
|
11107
|
+
function decodeToken(token) {
|
|
11108
|
+
try {
|
|
11109
|
+
const value = JSON.parse(Buffer.from(token, "base64url").toString("utf8"));
|
|
11110
|
+
if (Array.isArray(value) && typeof value[0] === "string" && Number.isSafeInteger(value[1]) && value[1] >= 0) {
|
|
11111
|
+
return { generation: value[0], offset: value[1] };
|
|
11112
|
+
}
|
|
11113
|
+
} catch {}
|
|
11114
|
+
return;
|
|
11115
|
+
}
|
|
11116
|
+
function firstCode(stderr) {
|
|
11117
|
+
return /\b(yrd-[a-z-]+|[A-Z][A-Z_]{3,})\b/u.exec(stderr)?.[1] ?? "no code";
|
|
11118
|
+
}
|
|
11119
|
+
function describe2(error) {
|
|
11120
|
+
if (error instanceof Error && "code" in error && typeof error.code === "string")
|
|
11121
|
+
return error.code;
|
|
11122
|
+
return error instanceof Error ? error.message : String(error);
|
|
11123
|
+
}
|
|
11124
|
+
var GROUP_WATCHDOG = [
|
|
11125
|
+
'command -v "$1" >/dev/null 2>&1 || { echo "hightea-spawn: ENOENT" >&2; exit 127; }',
|
|
11126
|
+
"exec 3<&0",
|
|
11127
|
+
'"$@" </dev/null 3<&- &',
|
|
11128
|
+
"c=$!",
|
|
11129
|
+
"{ cat <&3 >/dev/null 2>&1; kill -KILL 0; } >/dev/null 2>&1 &",
|
|
11130
|
+
"w=$!",
|
|
11131
|
+
"exec 3<&-",
|
|
11132
|
+
'wait "$c"',
|
|
11133
|
+
"s=$?",
|
|
11134
|
+
'kill -KILL "$w" 2>/dev/null',
|
|
11135
|
+
'exit "$s"'
|
|
11136
|
+
].join(`
|
|
11137
|
+
`);
|
|
11138
|
+
function spawnGroup(argv) {
|
|
11139
|
+
return spawn("/bin/sh", ["-c", GROUP_WATCHDOG, "hightea-spawn", ...argv], { stdio: ["pipe", "pipe", "pipe"], detached: true });
|
|
11140
|
+
}
|
|
11141
|
+
function notFound(command, code, stderr) {
|
|
11142
|
+
if (code !== 127 || !stderr.includes("hightea-spawn: ENOENT"))
|
|
11143
|
+
return;
|
|
11144
|
+
return Object.assign(new Error(`spawn ${command} ENOENT`), { code: "ENOENT" });
|
|
11145
|
+
}
|
|
11146
|
+
function signalGroup(child, signal) {
|
|
11147
|
+
if (child.pid === undefined)
|
|
11148
|
+
return;
|
|
11149
|
+
try {
|
|
11150
|
+
process.kill(-child.pid, signal);
|
|
11151
|
+
} catch {}
|
|
11152
|
+
}
|
|
11153
|
+
var runChild = (argv, { timeoutMs, signal }) => new Promise((resolve, reject) => {
|
|
11154
|
+
const [command, ...args] = argv;
|
|
11155
|
+
if (command === undefined) {
|
|
11156
|
+
reject(new Error("no command"));
|
|
11157
|
+
return;
|
|
11158
|
+
}
|
|
11159
|
+
const child = spawnGroup([command, ...args]);
|
|
11160
|
+
let stdout = "";
|
|
11161
|
+
let stderr = "";
|
|
11162
|
+
let bytes = 0;
|
|
11163
|
+
let hard;
|
|
11164
|
+
const kill = () => {
|
|
11165
|
+
if (child.exitCode !== null || child.signalCode !== null)
|
|
11166
|
+
return;
|
|
11167
|
+
signalGroup(child, "SIGTERM");
|
|
11168
|
+
hard ??= setTimeout(() => signalGroup(child, "SIGKILL"), 2000);
|
|
11169
|
+
};
|
|
11170
|
+
const deadline = setTimeout(kill, timeoutMs);
|
|
11171
|
+
signal.addEventListener("abort", kill, { once: true });
|
|
11172
|
+
const collect = (into) => (chunk) => {
|
|
11173
|
+
bytes += chunk.length;
|
|
11174
|
+
if (bytes > OUTPUT_MAX_BYTES)
|
|
11175
|
+
return kill();
|
|
11176
|
+
if (into === "out")
|
|
11177
|
+
stdout += chunk;
|
|
11178
|
+
else
|
|
11179
|
+
stderr += chunk;
|
|
11180
|
+
};
|
|
11181
|
+
child.stdout.setEncoding("utf8").on("data", collect("out"));
|
|
11182
|
+
child.stderr.setEncoding("utf8").on("data", collect("err"));
|
|
11183
|
+
let started = true;
|
|
11184
|
+
child.on("error", (error) => {
|
|
11185
|
+
started = false;
|
|
11186
|
+
clearTimeout(deadline);
|
|
11187
|
+
reject(error);
|
|
11188
|
+
});
|
|
11189
|
+
child.on("close", (code, killed) => {
|
|
11190
|
+
clearTimeout(deadline);
|
|
11191
|
+
if (hard !== undefined)
|
|
11192
|
+
clearTimeout(hard);
|
|
11193
|
+
signalGroup(child, "SIGKILL");
|
|
11194
|
+
signal.removeEventListener("abort", kill);
|
|
11195
|
+
if (!started)
|
|
11196
|
+
return;
|
|
11197
|
+
const missing = notFound(command, code, stderr);
|
|
11198
|
+
if (missing !== undefined)
|
|
11199
|
+
reject(missing);
|
|
11200
|
+
else
|
|
11201
|
+
resolve({ pid: child.pid, code: killed === null ? code : null, stdout, stderr });
|
|
11202
|
+
});
|
|
11203
|
+
});
|
|
11204
|
+
var followChild = (argv, { signal, onLine }) => new Promise((resolve, reject) => {
|
|
11205
|
+
const [command, ...args] = argv;
|
|
11206
|
+
if (command === undefined) {
|
|
11207
|
+
reject(new Error("no command"));
|
|
11208
|
+
return;
|
|
11209
|
+
}
|
|
11210
|
+
const child = spawnGroup([command, ...args]);
|
|
11211
|
+
let stderr = "";
|
|
11212
|
+
let hard;
|
|
11213
|
+
const kill = () => {
|
|
11214
|
+
if (child.exitCode !== null || child.signalCode !== null)
|
|
11215
|
+
return;
|
|
11216
|
+
signalGroup(child, "SIGTERM");
|
|
11217
|
+
hard ??= setTimeout(() => signalGroup(child, "SIGKILL"), 2000);
|
|
11218
|
+
};
|
|
11219
|
+
signal.addEventListener("abort", kill, { once: true });
|
|
11220
|
+
createInterface({ input: child.stdout }).on("line", (line) => {
|
|
11221
|
+
if (line.trim() !== "")
|
|
11222
|
+
onLine();
|
|
11223
|
+
});
|
|
11224
|
+
child.stderr.setEncoding("utf8").on("data", (chunk) => {
|
|
11225
|
+
if (stderr.length < 64 * 1024)
|
|
11226
|
+
stderr += chunk;
|
|
11227
|
+
});
|
|
11228
|
+
let started = true;
|
|
11229
|
+
child.on("error", (error) => {
|
|
11230
|
+
started = false;
|
|
11231
|
+
reject(error);
|
|
11232
|
+
});
|
|
11233
|
+
child.on("close", (code, killed) => {
|
|
11234
|
+
if (hard !== undefined)
|
|
11235
|
+
clearTimeout(hard);
|
|
11236
|
+
signalGroup(child, "SIGKILL");
|
|
11237
|
+
signal.removeEventListener("abort", kill);
|
|
11238
|
+
if (!started)
|
|
11239
|
+
return;
|
|
11240
|
+
const missing = notFound(command, code, stderr);
|
|
11241
|
+
if (missing !== undefined)
|
|
11242
|
+
reject(missing);
|
|
11243
|
+
else
|
|
11244
|
+
resolve({ pid: child.pid, code: killed === null ? code : null, stdout: "", stderr });
|
|
11245
|
+
});
|
|
11246
|
+
});
|
|
11247
|
+
|
|
11248
|
+
// ../ahp-host/src/source-channel.ts
|
|
11249
|
+
var REQUEST = "x-hightea/source/request";
|
|
11250
|
+
function createSourceChannel(options) {
|
|
11251
|
+
const { log } = options;
|
|
11252
|
+
const refuse = (connection, id, code) => connection.send({ jsonrpc: "2.0", id, error: { code: -32000, message: code } });
|
|
11253
|
+
const answer = (connection, id, request, outcome, code) => send(connection, id, request, { workspaceId: request.workspaceId, sourceId: request.sourceId, hostId: request.hostId, op: request.op, outcome, code });
|
|
11254
|
+
const send = (connection, id, request, result) => {
|
|
11255
|
+
const frame = { jsonrpc: "2.0", id, result };
|
|
11256
|
+
if (sourceFrameBytes(frame) > SOURCE_FRAME_MAX_BYTES) {
|
|
11257
|
+
log.error(`hightea-source: FRAME_TOO_LARGE for ${request.op} on ${request.sourceId}`);
|
|
11258
|
+
return answer(connection, id, request, "unknown", "FRAME_TOO_LARGE");
|
|
11259
|
+
}
|
|
11260
|
+
return connection.send(frame);
|
|
11261
|
+
};
|
|
11262
|
+
const run = async (connection, id, request) => {
|
|
11263
|
+
const { handler } = options;
|
|
11264
|
+
if (handler === undefined)
|
|
11265
|
+
return answer(connection, id, request, "refused", "SOURCE_UNAVAILABLE");
|
|
11266
|
+
let result;
|
|
11267
|
+
try {
|
|
11268
|
+
result = await handler.handle(request);
|
|
11269
|
+
} catch {
|
|
11270
|
+
log.error(`hightea-source: ${request.op} on ${request.sourceId} failed in its handler`);
|
|
11271
|
+
return answer(connection, id, request, "unknown", "SOURCE_UNAVAILABLE");
|
|
11272
|
+
}
|
|
11273
|
+
const parsed = SourceResponseSchema.safeParse(result);
|
|
11274
|
+
const same = parsed.success && parsed.data.workspaceId === request.workspaceId && parsed.data.sourceId === request.sourceId && parsed.data.hostId === request.hostId && parsed.data.op === request.op;
|
|
11275
|
+
if (!parsed.success || !same) {
|
|
11276
|
+
log.error(`hightea-source: ${request.op} on ${request.sourceId} returned an answer outside its schema`);
|
|
11277
|
+
return answer(connection, id, request, "unknown", "SOURCE_UNAVAILABLE");
|
|
11278
|
+
}
|
|
11279
|
+
return send(connection, id, request, parsed.data);
|
|
11280
|
+
};
|
|
11281
|
+
return {
|
|
11282
|
+
receive(frame, connection) {
|
|
11283
|
+
if (frame === null || typeof frame !== "object" || frame.method !== REQUEST)
|
|
11284
|
+
return false;
|
|
11285
|
+
const { id, params } = frame;
|
|
11286
|
+
if (typeof id !== "string" || !id.startsWith("source:") || id.length > 300) {
|
|
11287
|
+
refuse(connection, id ?? null, "INVALID_OPERATION_SHAPE");
|
|
11288
|
+
return true;
|
|
11289
|
+
}
|
|
11290
|
+
const parsed = SourceRequestSchema.safeParse(params);
|
|
11291
|
+
if (!parsed.success) {
|
|
11292
|
+
refuse(connection, id, "OPERATION_NOT_ALLOWLISTED");
|
|
11293
|
+
return true;
|
|
11294
|
+
}
|
|
11295
|
+
const request = parsed.data;
|
|
11296
|
+
if (request.workspaceId !== options.houseId || request.hostId !== options.hostId || !options.bindings().has(request.sourceId)) {
|
|
11297
|
+
answer(connection, id, request, "refused", "SOURCE_UNBOUND");
|
|
11298
|
+
return true;
|
|
11299
|
+
}
|
|
11300
|
+
run(connection, id, request);
|
|
11301
|
+
return true;
|
|
11302
|
+
}
|
|
10051
11303
|
};
|
|
10052
11304
|
}
|
|
10053
11305
|
|
|
@@ -10087,7 +11339,11 @@ function registerTools(host, mcpUrl, pipe) {
|
|
|
10087
11339
|
const resolved = activePipe?.resolveToolToken(context);
|
|
10088
11340
|
if (resolved === undefined || !resolved.ok)
|
|
10089
11341
|
throw new Error("TOKEN_EXPIRED_OR_REVOKED");
|
|
10090
|
-
const headers = {
|
|
11342
|
+
const headers = {
|
|
11343
|
+
authorization: `Bearer ${resolved.token}`,
|
|
11344
|
+
"content-type": "application/json",
|
|
11345
|
+
accept: "application/json, text/event-stream"
|
|
11346
|
+
};
|
|
10091
11347
|
if (tool === "post_message")
|
|
10092
11348
|
headers["idempotency-key"] = resolved.deliveryId;
|
|
10093
11349
|
if (tool === "read_thread" || tool === "post_message") {
|
|
@@ -10178,12 +11434,14 @@ function applyWith(host, options, seams) {
|
|
|
10178
11434
|
let stopped = false;
|
|
10179
11435
|
let startup;
|
|
10180
11436
|
let cloud;
|
|
10181
|
-
|
|
11437
|
+
let kmSources = new Map;
|
|
11438
|
+
const stop = async () => {
|
|
10182
11439
|
stopped = true;
|
|
10183
11440
|
if (startup !== undefined)
|
|
10184
11441
|
clearTimeout(startup);
|
|
10185
11442
|
startup = undefined;
|
|
10186
11443
|
cloud?.close();
|
|
11444
|
+
await Promise.all([...kmSources.values()].map((source) => source.stop()));
|
|
10187
11445
|
};
|
|
10188
11446
|
host.registerClose(stop);
|
|
10189
11447
|
if (options.mcpUrl !== undefined)
|
|
@@ -10196,6 +11454,23 @@ function applyWith(host, options, seams) {
|
|
|
10196
11454
|
if (stopped)
|
|
10197
11455
|
return;
|
|
10198
11456
|
let initialized;
|
|
11457
|
+
const hostId = options.headers["x-hightea-host"] ?? "";
|
|
11458
|
+
kmSources = new Map((options.sources ?? []).map((config) => [config.sourceId, new KmSource({
|
|
11459
|
+
config,
|
|
11460
|
+
houseId: options.houseId,
|
|
11461
|
+
hostId,
|
|
11462
|
+
log: host.log,
|
|
11463
|
+
...seams.sourceChildren,
|
|
11464
|
+
push: (params) => link.sourcePush(params)
|
|
11465
|
+
})]));
|
|
11466
|
+
const offered = new Set(kmSources.keys());
|
|
11467
|
+
const sources = createSourceChannel({
|
|
11468
|
+
houseId: options.houseId,
|
|
11469
|
+
hostId,
|
|
11470
|
+
bindings: () => offered,
|
|
11471
|
+
log: host.log,
|
|
11472
|
+
handler: { handle: (request) => kmSources.get(request.sourceId).handle(request) }
|
|
11473
|
+
});
|
|
10199
11474
|
const link = createCloudLink({
|
|
10200
11475
|
dial: (status) => {
|
|
10201
11476
|
const socket = new wrapper_default(options.cloudUrl, { headers: options.headers, handshakeTimeout: 15000, followRedirects: false });
|
|
@@ -10206,8 +11481,11 @@ function applyWith(host, options, seams) {
|
|
|
10206
11481
|
});
|
|
10207
11482
|
return socket;
|
|
10208
11483
|
},
|
|
10209
|
-
receive: (frame) => {
|
|
10210
|
-
|
|
11484
|
+
receive: (frame, connection) => {
|
|
11485
|
+
const seen = seams.cloudFrame === undefined ? frame : seams.cloudFrame(frame);
|
|
11486
|
+
if (sources.receive(seen, connection))
|
|
11487
|
+
return;
|
|
11488
|
+
pipe.fromCloud(seen);
|
|
10211
11489
|
},
|
|
10212
11490
|
log: host.log,
|
|
10213
11491
|
opened: (connection) => {
|
|
@@ -10225,6 +11503,8 @@ function applyWith(host, options, seams) {
|
|
|
10225
11503
|
}
|
|
10226
11504
|
});
|
|
10227
11505
|
cloud = link;
|
|
11506
|
+
for (const source of kmSources.values())
|
|
11507
|
+
source.start();
|
|
10228
11508
|
const shared = shareListener(host.connect());
|
|
10229
11509
|
const pipe = new WakePipe(seams.peer === undefined ? shared : seams.peer(shared), link, {
|
|
10230
11510
|
houseId: options.houseId,
|