@mastra/factory 0.2.1-alpha.2 → 0.2.1-alpha.4
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/CHANGELOG.md +22 -0
- package/dist/factory.d.ts.map +1 -1
- package/dist/factory.js +3462 -3583
- package/dist/factory.js.map +1 -1
- package/dist/index.js +3462 -3583
- package/dist/index.js.map +1 -1
- package/dist/integrations/base.d.ts +13 -25
- package/dist/integrations/base.d.ts.map +1 -1
- package/dist/integrations/github/integration.d.ts.map +1 -1
- package/dist/integrations/github/integration.js +625 -217
- package/dist/integrations/github/integration.js.map +1 -1
- package/dist/integrations/github/routes.d.ts +1 -4
- package/dist/integrations/github/routes.d.ts.map +1 -1
- package/dist/integrations/github/routes.js +113 -203
- package/dist/integrations/github/routes.js.map +1 -1
- package/dist/integrations/github/rules.d.ts +30 -0
- package/dist/integrations/github/rules.d.ts.map +1 -0
- package/dist/{rules/github-service.js → integrations/github/rules.js} +27 -8
- package/dist/integrations/github/rules.js.map +1 -0
- package/dist/integrations/linear/agent-tools.js.map +1 -1
- package/dist/integrations/linear/integration.d.ts.map +1 -1
- package/dist/integrations/linear/integration.js +352 -3
- package/dist/integrations/linear/integration.js.map +1 -1
- package/dist/integrations/linear/routes.d.ts +2 -2
- package/dist/integrations/linear/routes.d.ts.map +1 -1
- package/dist/integrations/linear/routes.js +2 -2
- package/dist/integrations/linear/routes.js.map +1 -1
- package/dist/integrations/linear/rules.d.ts +42 -0
- package/dist/integrations/linear/rules.d.ts.map +1 -0
- package/dist/{rules/linear-service.js → integrations/linear/rules.js} +14 -4
- package/dist/integrations/linear/rules.js.map +1 -0
- package/dist/integrations/platform/github/integration.d.ts.map +1 -1
- package/dist/integrations/platform/github/integration.js +626 -218
- package/dist/integrations/platform/github/integration.js.map +1 -1
- package/dist/integrations/platform/linear/integration.d.ts.map +1 -1
- package/dist/integrations/platform/linear/integration.js +353 -3
- package/dist/integrations/platform/linear/integration.js.map +1 -1
- package/dist/routes/oauth.js +6 -39
- package/dist/routes/oauth.js.map +1 -1
- package/dist/routes/surface.d.ts +3 -1
- package/dist/routes/surface.d.ts.map +1 -1
- package/dist/routes/surface.js +230 -619
- package/dist/routes/surface.js.map +1 -1
- package/dist/routes/work-items.js.map +1 -1
- package/dist/storage/domains/work-items/base.d.ts.map +1 -1
- package/dist/storage/domains/work-items/base.js +74 -75
- package/dist/storage/domains/work-items/base.js.map +1 -1
- package/dist/storage/domains/work-items/metrics.js.map +1 -1
- package/package.json +5 -5
- package/dist/integrations/github/project-lock.d.ts +0 -100
- package/dist/integrations/github/project-lock.d.ts.map +0 -1
- package/dist/integrations/github/project-lock.js +0 -103
- package/dist/integrations/github/project-lock.js.map +0 -1
- package/dist/rules/github-service.d.ts +0 -29
- package/dist/rules/github-service.d.ts.map +0 -1
- package/dist/rules/github-service.js.map +0 -1
- package/dist/rules/linear-service.d.ts +0 -27
- package/dist/rules/linear-service.d.ts.map +0 -1
- package/dist/rules/linear-service.js.map +0 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"integration.d.ts","sourceRoot":"","sources":["../../../../src/integrations/platform/linear/integration.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,8BAA8B,CAAC;AACnE,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,qBAAqB,CAAC;AAKpD,OAAO,KAAK,EAAE,MAAM,EAA0D,MAAM,iCAAiC,CAAC;AACtH,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,0BAA0B,CAAC;AAC1D,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,2CAA2C,CAAC;AACxF,OAAO,KAAK,EAAE,kBAAkB,EAAE,kBAAkB,EAAE,gBAAgB,EAAE,MAAM,eAAe,CAAC;AAE9F,OAAO,KAAK,EAAE,qBAAqB,EAAqB,MAAM,6BAA6B,CAAC;
|
|
1
|
+
{"version":3,"file":"integration.d.ts","sourceRoot":"","sources":["../../../../src/integrations/platform/linear/integration.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,8BAA8B,CAAC;AACnE,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,qBAAqB,CAAC;AAKpD,OAAO,KAAK,EAAE,MAAM,EAA0D,MAAM,iCAAiC,CAAC;AACtH,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,0BAA0B,CAAC;AAC1D,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,2CAA2C,CAAC;AACxF,OAAO,KAAK,EAAE,kBAAkB,EAAE,kBAAkB,EAAE,gBAAgB,EAAE,MAAM,eAAe,CAAC;AAE9F,OAAO,KAAK,EAAE,qBAAqB,EAAqB,MAAM,6BAA6B,CAAC;AAG5F,OAAO,KAAK,EAAwB,mBAAmB,EAAE,mBAAmB,EAAE,MAAM,yBAAyB,CAAC;AAmD9G,KAAK,aAAa,GAAG;IACnB,EAAE,EAAE,MAAM,CAAC;IACX,IAAI,EAAE,MAAM,CAAC;IACb,KAAK,EAAE,MAAM,CAAC;IACd,KAAK,EAAE,KAAK,CAAC;QAAE,EAAE,EAAE,MAAM,CAAC;QAAC,GAAG,EAAE,MAAM,CAAC;QAAC,IAAI,EAAE,MAAM,CAAA;KAAE,CAAC,CAAC;CACzD,CAAC;AAwBF,qBAAa,yBAA0B,YAAW,kBAAkB;;IAClE,QAAQ,CAAC,EAAE,YAAY;IAYvB,QAAQ,CAAC,MAAM,EAAE,MAAM,CAwIrB;;IAQF,IAAI,OAAO,IAAI,mBAAmB,CAuBjC;IAED,IAAI,QAAQ,IAAI,sBAAsB,CAGrC;IAED,UAAU,CAAC,EAAE,QAAQ,EAAE,IAAI,EAAE,EAAE;QAAE,QAAQ,EAAE,sBAAsB,CAAC;QAAC,IAAI,CAAC,EAAE,SAAS,CAAA;KAAE,GAAG,IAAI;IAM5F,IAAI,WAAW,IAAI,OAAO,CAEzB;IAEK,YAAY,CAAC,UAAU,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC;IASxD,cAAc,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,mBAAmB,GAAG,IAAI,CAAC;IAmBlE,mBAAmB,CAAC,WAAW,EAAE,mBAAmB,GAAG,OAAO,CAAC,MAAM,CAAC;IAyB5E,eAAe,CAAC,UAAU,EAAE,mBAAmB,GAAG,OAAO;IAKnD,eAAe,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,qBAAqB,CAAC;IASpE,MAAM,CAAC,GAAG,EAAE,kBAAkB,GAAG,QAAQ,EAAE;IAqCrC,UAAU,CAAC,EAAE,cAAc,EAAE,EAAE;QAAE,cAAc,EAAE,cAAc,CAAA;KAAE,GAAG,OAAO,CAAC,gBAAgB,CAAC;IAInG,WAAW,IAAI,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC;IAIhC,YAAY,IAAI,OAAO,CAAC,KAAK,CAAC,aAAa,GAAG;QAAE,WAAW,EAAE,MAAM,CAAA;KAAE,CAAC,CAAC;CAkK9E"}
|
|
@@ -213,8 +213,8 @@ function buildLinearRoutes(options) {
|
|
|
213
213
|
createdAt: issue.createdAt,
|
|
214
214
|
updatedAt: issue.updatedAt
|
|
215
215
|
}));
|
|
216
|
-
if (factoryProjectId && options.
|
|
217
|
-
await options.
|
|
216
|
+
if (factoryProjectId && options.ingestFactoryIssues) {
|
|
217
|
+
await options.ingestFactoryIssues({
|
|
218
218
|
orgId: resolved.tenant.orgId,
|
|
219
219
|
userId: resolved.tenant.userId,
|
|
220
220
|
factoryProjectId,
|
|
@@ -231,6 +231,355 @@ function buildLinearRoutes(options) {
|
|
|
231
231
|
return routes;
|
|
232
232
|
}
|
|
233
233
|
|
|
234
|
+
// src/rules/resolve.ts
|
|
235
|
+
function resolveFactoryLinearRule(rules, event) {
|
|
236
|
+
return rules.linear[event]?.onEvent;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
// src/rules/types.ts
|
|
240
|
+
var FACTORY_RULE_STAGES = ["intake", "triage", "planning", "execute", "review", "done", "canceled"];
|
|
241
|
+
var FACTORY_RULE_BOARDS = ["work", "review"];
|
|
242
|
+
|
|
243
|
+
// src/rules/validation.ts
|
|
244
|
+
var MAX_FACTORY_RULE_CAUSAL_DEPTH = 8;
|
|
245
|
+
var MAX_IDEMPOTENCY_KEY_LENGTH = 256;
|
|
246
|
+
var MAX_REASON_LENGTH = 512;
|
|
247
|
+
var MAX_TITLE_LENGTH = 512;
|
|
248
|
+
var MAX_MESSAGE_LENGTH = 8192;
|
|
249
|
+
var MAX_ARGUMENTS_LENGTH = 4096;
|
|
250
|
+
var MAX_ROLE_LENGTH = 32;
|
|
251
|
+
var MAX_SKILL_NAME_LENGTH = 128;
|
|
252
|
+
var MAX_SOURCE_KEY_LENGTH = 256;
|
|
253
|
+
var MAX_URL_LENGTH = 2048;
|
|
254
|
+
var MAX_METADATA_JSON_LENGTH = 16384;
|
|
255
|
+
var MAX_JSON_DEPTH = 8;
|
|
256
|
+
var MAX_JSON_COLLECTION_SIZE = 100;
|
|
257
|
+
var IDENTIFIER_RE = /^[a-z0-9][a-z0-9_-]*$/i;
|
|
258
|
+
var SKILL_NAME_RE = /^[a-z0-9]+(?:-[a-z0-9]+)*$/;
|
|
259
|
+
var SENSITIVE_KEY_RE = /(?:authorization|cookie|credential|password|secret|token)/i;
|
|
260
|
+
var WORK_ITEM_SOURCES = ["github-issue", "github-pr", "linear-issue", "manual"];
|
|
261
|
+
var REJECTION_CODES = [
|
|
262
|
+
"forbidden",
|
|
263
|
+
"invalid_transition",
|
|
264
|
+
"missing_binding",
|
|
265
|
+
"stale",
|
|
266
|
+
"timeout",
|
|
267
|
+
"rule_error",
|
|
268
|
+
"causal_depth_exceeded",
|
|
269
|
+
"repeated_transition"
|
|
270
|
+
];
|
|
271
|
+
var FactoryRuleValidationError = class extends Error {
|
|
272
|
+
code = "invalid_factory_rule";
|
|
273
|
+
constructor(message) {
|
|
274
|
+
super(message);
|
|
275
|
+
this.name = "FactoryRuleValidationError";
|
|
276
|
+
}
|
|
277
|
+
};
|
|
278
|
+
function isPlainObject(value) {
|
|
279
|
+
if (typeof value !== "object" || value === null || Array.isArray(value)) return false;
|
|
280
|
+
const prototype = Object.getPrototypeOf(value);
|
|
281
|
+
return prototype === Object.prototype || prototype === null;
|
|
282
|
+
}
|
|
283
|
+
function assertExactKeys(value, keys, label) {
|
|
284
|
+
const allowed = new Set(keys);
|
|
285
|
+
if (Object.keys(value).some((key) => !allowed.has(key))) {
|
|
286
|
+
throw new FactoryRuleValidationError(`${label} contains an unsupported field.`);
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
function boundedString(value, label, max, pattern) {
|
|
290
|
+
if (typeof value !== "string") throw new FactoryRuleValidationError(`${label} must be a string.`);
|
|
291
|
+
const normalized = value.trim();
|
|
292
|
+
if (normalized.length === 0 || normalized.length > max || pattern && !pattern.test(normalized)) {
|
|
293
|
+
throw new FactoryRuleValidationError(`${label} is invalid.`);
|
|
294
|
+
}
|
|
295
|
+
return normalized;
|
|
296
|
+
}
|
|
297
|
+
function optionalBoundedString(value, label, max) {
|
|
298
|
+
if (value === void 0) return void 0;
|
|
299
|
+
return boundedString(value, label, max);
|
|
300
|
+
}
|
|
301
|
+
function enumValue(value, allowed, label) {
|
|
302
|
+
if (typeof value !== "string" || !allowed.includes(value)) {
|
|
303
|
+
throw new FactoryRuleValidationError(`${label} is invalid.`);
|
|
304
|
+
}
|
|
305
|
+
return value;
|
|
306
|
+
}
|
|
307
|
+
function normalizeFactoryRuleJsonValue(value, depth = 0, seen = /* @__PURE__ */ new Set()) {
|
|
308
|
+
if (value === null || typeof value === "boolean" || typeof value === "string") return value;
|
|
309
|
+
if (typeof value === "number") {
|
|
310
|
+
if (!Number.isFinite(value)) throw new FactoryRuleValidationError("Rule metadata must contain finite numbers.");
|
|
311
|
+
return value;
|
|
312
|
+
}
|
|
313
|
+
if (depth >= MAX_JSON_DEPTH || typeof value !== "object" && !Array.isArray(value)) {
|
|
314
|
+
throw new FactoryRuleValidationError("Rule metadata is not bounded JSON.");
|
|
315
|
+
}
|
|
316
|
+
if (seen.has(value)) throw new FactoryRuleValidationError("Rule metadata must not contain cycles.");
|
|
317
|
+
seen.add(value);
|
|
318
|
+
try {
|
|
319
|
+
if (Array.isArray(value)) {
|
|
320
|
+
if (value.length > MAX_JSON_COLLECTION_SIZE) {
|
|
321
|
+
throw new FactoryRuleValidationError("Rule metadata contains too many entries.");
|
|
322
|
+
}
|
|
323
|
+
return value.map((entry) => normalizeFactoryRuleJsonValue(entry, depth + 1, seen));
|
|
324
|
+
}
|
|
325
|
+
if (!isPlainObject(value)) throw new FactoryRuleValidationError("Rule metadata must use plain objects.");
|
|
326
|
+
const entries = Object.entries(value);
|
|
327
|
+
if (entries.length > MAX_JSON_COLLECTION_SIZE) {
|
|
328
|
+
throw new FactoryRuleValidationError("Rule metadata contains too many fields.");
|
|
329
|
+
}
|
|
330
|
+
const sanitized = {};
|
|
331
|
+
for (const [key, entry] of entries) {
|
|
332
|
+
const normalizedKey = boundedString(key, "Rule metadata key", 128, IDENTIFIER_RE);
|
|
333
|
+
sanitized[normalizedKey] = SENSITIVE_KEY_RE.test(normalizedKey) ? "[REDACTED]" : normalizeFactoryRuleJsonValue(entry, depth + 1, seen);
|
|
334
|
+
}
|
|
335
|
+
return sanitized;
|
|
336
|
+
} finally {
|
|
337
|
+
seen.delete(value);
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
function sanitizeMetadata(value) {
|
|
341
|
+
if (value === void 0) return void 0;
|
|
342
|
+
const sanitized = normalizeFactoryRuleJsonValue(value);
|
|
343
|
+
if (!isPlainObject(sanitized)) throw new FactoryRuleValidationError("Rule metadata must be an object.");
|
|
344
|
+
if (JSON.stringify(sanitized).length > MAX_METADATA_JSON_LENGTH) {
|
|
345
|
+
throw new FactoryRuleValidationError("Rule metadata is too large.");
|
|
346
|
+
}
|
|
347
|
+
return sanitized;
|
|
348
|
+
}
|
|
349
|
+
function commonCommitFields(value) {
|
|
350
|
+
return {
|
|
351
|
+
idempotencyKey: boundedString(value.idempotencyKey, "Factory decision idempotencyKey", MAX_IDEMPOTENCY_KEY_LENGTH)
|
|
352
|
+
};
|
|
353
|
+
}
|
|
354
|
+
function validateFactoryRuleDecision(value, causalDepth = 0) {
|
|
355
|
+
if (causalDepth > MAX_FACTORY_RULE_CAUSAL_DEPTH) {
|
|
356
|
+
throw new FactoryRuleValidationError("Factory rule causal depth exceeded.");
|
|
357
|
+
}
|
|
358
|
+
if (!isPlainObject(value)) throw new FactoryRuleValidationError("Factory rule decision must be an object.");
|
|
359
|
+
const type = value.type;
|
|
360
|
+
if (typeof type !== "string") throw new FactoryRuleValidationError("Factory rule decision type is required.");
|
|
361
|
+
switch (type) {
|
|
362
|
+
case "reject": {
|
|
363
|
+
assertExactKeys(value, ["type", "code", "reason"], "Factory reject decision");
|
|
364
|
+
return {
|
|
365
|
+
type,
|
|
366
|
+
code: enumValue(value.code, REJECTION_CODES, "Factory rejection code"),
|
|
367
|
+
reason: boundedString(value.reason, "Factory rejection reason", MAX_REASON_LENGTH)
|
|
368
|
+
};
|
|
369
|
+
}
|
|
370
|
+
case "transition": {
|
|
371
|
+
assertExactKeys(value, ["type", "idempotencyKey", "board", "stage"], "Factory transition decision");
|
|
372
|
+
return {
|
|
373
|
+
type,
|
|
374
|
+
...commonCommitFields(value),
|
|
375
|
+
board: enumValue(value.board, FACTORY_RULE_BOARDS, "Factory transition board"),
|
|
376
|
+
stage: enumValue(value.stage, FACTORY_RULE_STAGES, "Factory transition stage")
|
|
377
|
+
};
|
|
378
|
+
}
|
|
379
|
+
case "upsertLinkedWorkItem": {
|
|
380
|
+
assertExactKeys(
|
|
381
|
+
value,
|
|
382
|
+
["type", "idempotencyKey", "board", "source", "sourceKey", "title", "url", "stage", "metadata"],
|
|
383
|
+
"Factory linked work item decision"
|
|
384
|
+
);
|
|
385
|
+
const url = value.url;
|
|
386
|
+
if (url !== null && (typeof url !== "string" || url.length > MAX_URL_LENGTH || !/^https?:\/\//.test(url))) {
|
|
387
|
+
throw new FactoryRuleValidationError("Factory linked work item URL is invalid.");
|
|
388
|
+
}
|
|
389
|
+
const metadata = sanitizeMetadata(value.metadata);
|
|
390
|
+
return {
|
|
391
|
+
type,
|
|
392
|
+
...commonCommitFields(value),
|
|
393
|
+
board: enumValue(value.board, FACTORY_RULE_BOARDS, "Factory linked work item board"),
|
|
394
|
+
source: enumValue(value.source, WORK_ITEM_SOURCES, "Factory linked work item source"),
|
|
395
|
+
sourceKey: boundedString(value.sourceKey, "Factory linked work item sourceKey", MAX_SOURCE_KEY_LENGTH),
|
|
396
|
+
title: boundedString(value.title, "Factory linked work item title", MAX_TITLE_LENGTH),
|
|
397
|
+
url,
|
|
398
|
+
stage: enumValue(value.stage, FACTORY_RULE_STAGES, "Factory linked work item stage"),
|
|
399
|
+
...metadata ? { metadata } : {}
|
|
400
|
+
};
|
|
401
|
+
}
|
|
402
|
+
case "invokeSkill": {
|
|
403
|
+
assertExactKeys(
|
|
404
|
+
value,
|
|
405
|
+
["type", "idempotencyKey", "role", "skillName", "arguments", "precedingMessage"],
|
|
406
|
+
"Factory invoke skill decision"
|
|
407
|
+
);
|
|
408
|
+
const args = optionalBoundedString(value.arguments, "Factory skill arguments", MAX_ARGUMENTS_LENGTH);
|
|
409
|
+
const precedingMessage = optionalBoundedString(
|
|
410
|
+
value.precedingMessage,
|
|
411
|
+
"Factory skill preceding message",
|
|
412
|
+
MAX_MESSAGE_LENGTH
|
|
413
|
+
);
|
|
414
|
+
return {
|
|
415
|
+
type,
|
|
416
|
+
...commonCommitFields(value),
|
|
417
|
+
role: boundedString(value.role, "Factory skill role", MAX_ROLE_LENGTH, IDENTIFIER_RE),
|
|
418
|
+
skillName: boundedString(value.skillName, "Factory skill name", MAX_SKILL_NAME_LENGTH, SKILL_NAME_RE),
|
|
419
|
+
...args ? { arguments: args } : {},
|
|
420
|
+
...precedingMessage ? { precedingMessage } : {}
|
|
421
|
+
};
|
|
422
|
+
}
|
|
423
|
+
case "sendMessage": {
|
|
424
|
+
assertExactKeys(
|
|
425
|
+
value,
|
|
426
|
+
["type", "idempotencyKey", "role", "message", "priority", "idleBehavior", "prepareBinding"],
|
|
427
|
+
"Factory send message decision"
|
|
428
|
+
);
|
|
429
|
+
const priority = value.priority === void 0 ? void 0 : enumValue(value.priority, ["medium", "high", "urgent"], "Factory message priority");
|
|
430
|
+
const idleBehavior = value.idleBehavior === void 0 ? void 0 : enumValue(value.idleBehavior, ["persist", "wake"], "Factory message idle behavior");
|
|
431
|
+
if (value.prepareBinding !== void 0 && typeof value.prepareBinding !== "boolean") {
|
|
432
|
+
throw new FactoryRuleValidationError("Factory message prepareBinding must be a boolean.");
|
|
433
|
+
}
|
|
434
|
+
return {
|
|
435
|
+
type,
|
|
436
|
+
...commonCommitFields(value),
|
|
437
|
+
role: boundedString(value.role, "Factory message role", MAX_ROLE_LENGTH, IDENTIFIER_RE),
|
|
438
|
+
message: boundedString(value.message, "Factory message", MAX_MESSAGE_LENGTH),
|
|
439
|
+
...priority ? { priority } : {},
|
|
440
|
+
...idleBehavior ? { idleBehavior } : {},
|
|
441
|
+
...value.prepareBinding === true ? { prepareBinding: true } : {}
|
|
442
|
+
};
|
|
443
|
+
}
|
|
444
|
+
case "notify": {
|
|
445
|
+
assertExactKeys(value, ["type", "idempotencyKey", "title", "body", "level"], "Factory notify decision");
|
|
446
|
+
const body = optionalBoundedString(value.body, "Factory notification body", MAX_MESSAGE_LENGTH);
|
|
447
|
+
const level = value.level === void 0 ? void 0 : enumValue(value.level, ["info", "warning", "error"], "Factory notification level");
|
|
448
|
+
return {
|
|
449
|
+
type,
|
|
450
|
+
...commonCommitFields(value),
|
|
451
|
+
title: boundedString(value.title, "Factory notification title", MAX_TITLE_LENGTH),
|
|
452
|
+
...body ? { body } : {},
|
|
453
|
+
...level ? { level } : {}
|
|
454
|
+
};
|
|
455
|
+
}
|
|
456
|
+
default:
|
|
457
|
+
throw new FactoryRuleValidationError("Factory rule decision type is unsupported.");
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
function validateFactoryRuleDecisions(values, causalDepth = 0) {
|
|
461
|
+
if (values.length > MAX_JSON_COLLECTION_SIZE) {
|
|
462
|
+
throw new FactoryRuleValidationError("Factory rule produced too many decisions.");
|
|
463
|
+
}
|
|
464
|
+
const decisions = [];
|
|
465
|
+
for (const value of values) {
|
|
466
|
+
const decision = validateFactoryRuleDecision(value, causalDepth);
|
|
467
|
+
if (decision.type === "reject") {
|
|
468
|
+
throw new FactoryRuleValidationError("A rejection cannot be persisted with commit decisions.");
|
|
469
|
+
}
|
|
470
|
+
decisions.push(decision);
|
|
471
|
+
}
|
|
472
|
+
const keys = decisions.map((decision) => decision.idempotencyKey);
|
|
473
|
+
if (new Set(keys).size !== keys.length) {
|
|
474
|
+
throw new FactoryRuleValidationError("Factory decisions require unique idempotency keys.");
|
|
475
|
+
}
|
|
476
|
+
return decisions;
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
// src/integrations/linear/rules.ts
|
|
480
|
+
var RULE_TIMEOUT_MS = 5e3;
|
|
481
|
+
async function withRuleTimeout(promise) {
|
|
482
|
+
let timeout;
|
|
483
|
+
try {
|
|
484
|
+
return await Promise.race([
|
|
485
|
+
promise,
|
|
486
|
+
new Promise((_, reject) => {
|
|
487
|
+
timeout = setTimeout(() => reject(new Error("FACTORY_RULE_TIMEOUT")), RULE_TIMEOUT_MS);
|
|
488
|
+
})
|
|
489
|
+
]);
|
|
490
|
+
} finally {
|
|
491
|
+
if (timeout) clearTimeout(timeout);
|
|
492
|
+
}
|
|
493
|
+
}
|
|
494
|
+
var LinearRules = class {
|
|
495
|
+
constructor(options) {
|
|
496
|
+
this.options = options;
|
|
497
|
+
}
|
|
498
|
+
options;
|
|
499
|
+
async ingest(input) {
|
|
500
|
+
const project = await this.options.projects.get({ orgId: input.orgId, id: input.factoryProjectId });
|
|
501
|
+
if (!project) return { status: "missing", ingested: 0 };
|
|
502
|
+
const items = await this.options.storage.list({ orgId: input.orgId, factoryProjectId: input.factoryProjectId });
|
|
503
|
+
const itemsBySourceKey = new Map(items.map((item) => [item.externalSource?.externalId, item]));
|
|
504
|
+
const statuses = [];
|
|
505
|
+
for (const issue of input.issues) {
|
|
506
|
+
statuses.push(await this.#ingestIssue(input, issue, itemsBySourceKey.get(`linear:${issue.identifier}`)));
|
|
507
|
+
}
|
|
508
|
+
if (statuses.some((status) => status === "committed")) return { status: "committed", ingested: statuses.length };
|
|
509
|
+
if (statuses.some((status) => status === "replayed")) return { status: "replayed", ingested: statuses.length };
|
|
510
|
+
return { status: "missing", ingested: statuses.length };
|
|
511
|
+
}
|
|
512
|
+
async #ingestIssue(input, issue, relatedItem) {
|
|
513
|
+
const ingressId = `linear:${issue.id}:${issue.updatedAt}`;
|
|
514
|
+
const actor = { type: "human", id: input.userId };
|
|
515
|
+
const context = {
|
|
516
|
+
tenant: { orgId: input.orgId, projectId: input.factoryProjectId },
|
|
517
|
+
actor,
|
|
518
|
+
ingress: { type: "linear", id: ingressId },
|
|
519
|
+
cause: "linear.issueObserved",
|
|
520
|
+
causalChain: [],
|
|
521
|
+
ruleSetVersion: this.options.rules.version,
|
|
522
|
+
...relatedItem ? {
|
|
523
|
+
item: {
|
|
524
|
+
id: relatedItem.id,
|
|
525
|
+
source: "linear-issue",
|
|
526
|
+
sourceKey: relatedItem.externalSource?.externalId ?? null,
|
|
527
|
+
parentWorkItemId: relatedItem.parentWorkItemId,
|
|
528
|
+
title: relatedItem.title,
|
|
529
|
+
url: relatedItem.externalSource?.url ?? null,
|
|
530
|
+
stages: relatedItem.stages
|
|
531
|
+
},
|
|
532
|
+
board: "work",
|
|
533
|
+
itemRevision: relatedItem.revision
|
|
534
|
+
} : {},
|
|
535
|
+
event: "issueObserved",
|
|
536
|
+
issue
|
|
537
|
+
};
|
|
538
|
+
const rule = resolveFactoryLinearRule(this.options.rules, context.event);
|
|
539
|
+
let decision;
|
|
540
|
+
let decisions = [];
|
|
541
|
+
let outcome = { status: "accepted" };
|
|
542
|
+
try {
|
|
543
|
+
decision = rule ? await withRuleTimeout(Promise.resolve(rule(Object.freeze(context)))) : void 0;
|
|
544
|
+
if (decision?.type === "reject") {
|
|
545
|
+
outcome = { status: "rejected", code: decision.code, reason: decision.reason };
|
|
546
|
+
} else if (decision) {
|
|
547
|
+
decisions = validateFactoryRuleDecisions([decision]).map((entry) => ({ ...entry }));
|
|
548
|
+
}
|
|
549
|
+
} catch (error) {
|
|
550
|
+
const timedOut = error instanceof Error && error.message === "FACTORY_RULE_TIMEOUT";
|
|
551
|
+
outcome = {
|
|
552
|
+
status: "rejected",
|
|
553
|
+
code: timedOut ? "timeout" : "rule_error",
|
|
554
|
+
reason: timedOut ? "Factory rule evaluation timed out." : error instanceof Error ? error.message.slice(0, 2e3) : "Factory Linear rule failed."
|
|
555
|
+
};
|
|
556
|
+
}
|
|
557
|
+
const committed = await this.options.storage.commitRuleEvaluation({
|
|
558
|
+
orgId: input.orgId,
|
|
559
|
+
factoryProjectId: input.factoryProjectId,
|
|
560
|
+
workItemId: relatedItem?.id ?? null,
|
|
561
|
+
ingress: { identity: ingressId, triggerType: "linear.issueObserved" },
|
|
562
|
+
ruleSetVersion: this.options.rules.version,
|
|
563
|
+
expectedRevision: relatedItem?.revision ?? null,
|
|
564
|
+
actor,
|
|
565
|
+
outcome,
|
|
566
|
+
decisions,
|
|
567
|
+
causalChain: [],
|
|
568
|
+
now: /* @__PURE__ */ new Date()
|
|
569
|
+
});
|
|
570
|
+
return committed.status;
|
|
571
|
+
}
|
|
572
|
+
};
|
|
573
|
+
function attachLinearRules(context) {
|
|
574
|
+
if (!context.rules) return void 0;
|
|
575
|
+
const rules = new LinearRules({
|
|
576
|
+
projects: context.storage.projects,
|
|
577
|
+
storage: context.rules.workItems,
|
|
578
|
+
rules: context.rules.config
|
|
579
|
+
});
|
|
580
|
+
return (input) => rules.ingest(input);
|
|
581
|
+
}
|
|
582
|
+
|
|
234
583
|
// src/integrations/linear/integration.ts
|
|
235
584
|
var LinearReauthRequiredError = class extends Error {
|
|
236
585
|
constructor() {
|
|
@@ -739,7 +1088,8 @@ var PlatformLinearIntegration = class {
|
|
|
739
1088
|
linear: this,
|
|
740
1089
|
stateSigner: ctx.stateSigner,
|
|
741
1090
|
baseUrl: ctx.baseUrl,
|
|
742
|
-
intake: ctx.storage.intake
|
|
1091
|
+
intake: ctx.storage.intake,
|
|
1092
|
+
ingestFactoryIssues: attachLinearRules(ctx)
|
|
743
1093
|
}).filter((route) => !route.path.startsWith("/auth/linear/"))
|
|
744
1094
|
];
|
|
745
1095
|
}
|