@mastra/factory 0.0.0-fix-thread-subscription-multi-instance-redis-streams-20260730204150
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 +695 -0
- package/LICENSE.md +30 -0
- package/README.md +27 -0
- package/dist/auth.d.ts +181 -0
- package/dist/auth.d.ts.map +1 -0
- package/dist/auth.js +505 -0
- package/dist/auth.js.map +1 -0
- package/dist/capabilities/connection.d.ts +9 -0
- package/dist/capabilities/connection.d.ts.map +1 -0
- package/dist/capabilities/connection.js +1 -0
- package/dist/capabilities/intake.d.ts +140 -0
- package/dist/capabilities/intake.d.ts.map +1 -0
- package/dist/capabilities/intake.js +1 -0
- package/dist/capabilities/version-control.d.ts +245 -0
- package/dist/capabilities/version-control.d.ts.map +1 -0
- package/dist/capabilities/version-control.js +1 -0
- package/dist/factory.d.ts +156 -0
- package/dist/factory.d.ts.map +1 -0
- package/dist/factory.js +483 -0
- package/dist/factory.js.map +1 -0
- package/dist/index.d.ts +14 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +7 -0
- package/dist/integrations/base.d.ts +210 -0
- package/dist/integrations/base.d.ts.map +1 -0
- package/dist/integrations/base.js +1 -0
- package/dist/integrations/github/config.d.ts +60 -0
- package/dist/integrations/github/config.d.ts.map +1 -0
- package/dist/integrations/github/config.js +42 -0
- package/dist/integrations/github/config.js.map +1 -0
- package/dist/integrations/github/factory-session.d.ts +12 -0
- package/dist/integrations/github/factory-session.d.ts.map +1 -0
- package/dist/integrations/github/factory-session.js +37 -0
- package/dist/integrations/github/factory-session.js.map +1 -0
- package/dist/integrations/github/integration.d.ts +191 -0
- package/dist/integrations/github/integration.d.ts.map +1 -0
- package/dist/integrations/github/integration.js +1077 -0
- package/dist/integrations/github/integration.js.map +1 -0
- package/dist/integrations/github/issue-triage.d.ts +27 -0
- package/dist/integrations/github/issue-triage.d.ts.map +1 -0
- package/dist/integrations/github/issue-triage.js +82 -0
- package/dist/integrations/github/issue-triage.js.map +1 -0
- package/dist/integrations/github/pat.d.ts +38 -0
- package/dist/integrations/github/pat.d.ts.map +1 -0
- package/dist/integrations/github/pat.js +59 -0
- package/dist/integrations/github/pat.js.map +1 -0
- package/dist/integrations/github/provenance.d.ts +27 -0
- package/dist/integrations/github/provenance.d.ts.map +1 -0
- package/dist/integrations/github/provenance.js +75 -0
- package/dist/integrations/github/provenance.js.map +1 -0
- package/dist/integrations/github/routes.d.ts +69 -0
- package/dist/integrations/github/routes.d.ts.map +1 -0
- package/dist/integrations/github/routes.js +1324 -0
- package/dist/integrations/github/routes.js.map +1 -0
- package/dist/integrations/github/rules.d.ts +78 -0
- package/dist/integrations/github/rules.d.ts.map +1 -0
- package/dist/integrations/github/rules.js +362 -0
- package/dist/integrations/github/rules.js.map +1 -0
- package/dist/integrations/github/sandbox-release.d.ts +38 -0
- package/dist/integrations/github/sandbox-release.d.ts.map +1 -0
- package/dist/integrations/github/sandbox-release.js +74 -0
- package/dist/integrations/github/sandbox-release.js.map +1 -0
- package/dist/integrations/github/sandbox.d.ts +281 -0
- package/dist/integrations/github/sandbox.d.ts.map +1 -0
- package/dist/integrations/github/sandbox.js +547 -0
- package/dist/integrations/github/sandbox.js.map +1 -0
- package/dist/integrations/github/session-subscriptions.d.ts +26 -0
- package/dist/integrations/github/session-subscriptions.d.ts.map +1 -0
- package/dist/integrations/github/session-subscriptions.js +192 -0
- package/dist/integrations/github/session-subscriptions.js.map +1 -0
- package/dist/integrations/github/subscriptions.d.ts +54 -0
- package/dist/integrations/github/subscriptions.d.ts.map +1 -0
- package/dist/integrations/github/subscriptions.js +62 -0
- package/dist/integrations/github/subscriptions.js.map +1 -0
- package/dist/integrations/github/token-refresh.d.ts +12 -0
- package/dist/integrations/github/token-refresh.d.ts.map +1 -0
- package/dist/integrations/github/token-refresh.js +24 -0
- package/dist/integrations/github/token-refresh.js.map +1 -0
- package/dist/integrations/github/webhook.d.ts +87 -0
- package/dist/integrations/github/webhook.d.ts.map +1 -0
- package/dist/integrations/github/webhook.js +397 -0
- package/dist/integrations/github/webhook.js.map +1 -0
- package/dist/integrations/linear/agent-tools.d.ts +34 -0
- package/dist/integrations/linear/agent-tools.d.ts.map +1 -0
- package/dist/integrations/linear/agent-tools.js +85 -0
- package/dist/integrations/linear/agent-tools.js.map +1 -0
- package/dist/integrations/linear/integration.d.ts +233 -0
- package/dist/integrations/linear/integration.d.ts.map +1 -0
- package/dist/integrations/linear/integration.js +672 -0
- package/dist/integrations/linear/integration.js.map +1 -0
- package/dist/integrations/linear/routes.d.ts +61 -0
- package/dist/integrations/linear/routes.d.ts.map +1 -0
- package/dist/integrations/linear/routes.js +239 -0
- package/dist/integrations/linear/routes.js.map +1 -0
- package/dist/integrations/linear/rules.d.ts +42 -0
- package/dist/integrations/linear/rules.d.ts.map +1 -0
- package/dist/integrations/linear/rules.js +133 -0
- package/dist/integrations/linear/rules.js.map +1 -0
- package/dist/integrations/linear/storage.d.ts +65 -0
- package/dist/integrations/linear/storage.d.ts.map +1 -0
- package/dist/integrations/linear/storage.js +1 -0
- package/dist/integrations/platform/api-client.d.ts +26 -0
- package/dist/integrations/platform/api-client.d.ts.map +1 -0
- package/dist/integrations/platform/api-client.js +156 -0
- package/dist/integrations/platform/api-client.js.map +1 -0
- package/dist/integrations/platform/github/event-worker.d.ts +49 -0
- package/dist/integrations/platform/github/event-worker.d.ts.map +1 -0
- package/dist/integrations/platform/github/event-worker.js +348 -0
- package/dist/integrations/platform/github/event-worker.js.map +1 -0
- package/dist/integrations/platform/github/integration.d.ts +50 -0
- package/dist/integrations/platform/github/integration.d.ts.map +1 -0
- package/dist/integrations/platform/github/integration.js +961 -0
- package/dist/integrations/platform/github/integration.js.map +1 -0
- package/dist/integrations/platform/linear/integration.d.ts +46 -0
- package/dist/integrations/platform/linear/integration.d.ts.map +1 -0
- package/dist/integrations/platform/linear/integration.js +483 -0
- package/dist/integrations/platform/linear/integration.js.map +1 -0
- package/dist/integrations/workos/integration.d.ts +39 -0
- package/dist/integrations/workos/integration.d.ts.map +1 -0
- package/dist/integrations/workos/integration.js +91 -0
- package/dist/integrations/workos/integration.js.map +1 -0
- package/dist/routes/config.d.ts +235 -0
- package/dist/routes/config.d.ts.map +1 -0
- package/dist/routes/config.js +972 -0
- package/dist/routes/config.js.map +1 -0
- package/dist/routes/custom-provider-source.d.ts +52 -0
- package/dist/routes/custom-provider-source.d.ts.map +1 -0
- package/dist/routes/custom-provider-source.js +124 -0
- package/dist/routes/custom-provider-source.js.map +1 -0
- package/dist/routes/fs.d.ts +151 -0
- package/dist/routes/fs.d.ts.map +1 -0
- package/dist/routes/fs.js +708 -0
- package/dist/routes/fs.js.map +1 -0
- package/dist/routes/intake.d.ts +23 -0
- package/dist/routes/intake.d.ts.map +1 -0
- package/dist/routes/intake.js +179 -0
- package/dist/routes/intake.js.map +1 -0
- package/dist/routes/oauth.d.ts +43 -0
- package/dist/routes/oauth.d.ts.map +1 -0
- package/dist/routes/oauth.js +463 -0
- package/dist/routes/oauth.js.map +1 -0
- package/dist/routes/projects.d.ts +19 -0
- package/dist/routes/projects.d.ts.map +1 -0
- package/dist/routes/projects.js +461 -0
- package/dist/routes/projects.js.map +1 -0
- package/dist/routes/provider-credentials.d.ts +81 -0
- package/dist/routes/provider-credentials.d.ts.map +1 -0
- package/dist/routes/provider-credentials.js +86 -0
- package/dist/routes/provider-credentials.js.map +1 -0
- package/dist/routes/route.d.ts +48 -0
- package/dist/routes/route.d.ts.map +1 -0
- package/dist/routes/route.js +15 -0
- package/dist/routes/route.js.map +1 -0
- package/dist/routes/skills.d.ts +30 -0
- package/dist/routes/skills.d.ts.map +1 -0
- package/dist/routes/skills.js +168 -0
- package/dist/routes/skills.js.map +1 -0
- package/dist/routes/surface.d.ts +91 -0
- package/dist/routes/surface.d.ts.map +1 -0
- package/dist/routes/surface.js +260 -0
- package/dist/routes/surface.js.map +1 -0
- package/dist/routes/tenant-credentials.d.ts +74 -0
- package/dist/routes/tenant-credentials.d.ts.map +1 -0
- package/dist/routes/tenant-credentials.js +156 -0
- package/dist/routes/tenant-credentials.js.map +1 -0
- package/dist/routes/work-items.d.ts +40 -0
- package/dist/routes/work-items.d.ts.map +1 -0
- package/dist/routes/work-items.js +695 -0
- package/dist/routes/work-items.js.map +1 -0
- package/dist/rules/binding-context.d.ts +5 -0
- package/dist/rules/binding-context.d.ts.map +1 -0
- package/dist/rules/binding-context.js +28 -0
- package/dist/rules/binding-context.js.map +1 -0
- package/dist/rules/defaults.d.ts +9 -0
- package/dist/rules/defaults.d.ts.map +1 -0
- package/dist/rules/defaults.js +260 -0
- package/dist/rules/defaults.js.map +1 -0
- package/dist/rules/dispatcher.d.ts +42 -0
- package/dist/rules/dispatcher.d.ts.map +1 -0
- package/dist/rules/dispatcher.js +518 -0
- package/dist/rules/dispatcher.js.map +1 -0
- package/dist/rules/index.d.ts +7 -0
- package/dist/rules/index.d.ts.map +1 -0
- package/dist/rules/index.js +5 -0
- package/dist/rules/processor.d.ts +54 -0
- package/dist/rules/processor.d.ts.map +1 -0
- package/dist/rules/processor.js +378 -0
- package/dist/rules/processor.js.map +1 -0
- package/dist/rules/resolve.d.ts +17 -0
- package/dist/rules/resolve.d.ts.map +1 -0
- package/dist/rules/resolve.js +30 -0
- package/dist/rules/resolve.js.map +1 -0
- package/dist/rules/start-coordinator.d.ts +60 -0
- package/dist/rules/start-coordinator.d.ts.map +1 -0
- package/dist/rules/start-coordinator.js +194 -0
- package/dist/rules/start-coordinator.js.map +1 -0
- package/dist/rules/tools.d.ts +10 -0
- package/dist/rules/tools.d.ts.map +1 -0
- package/dist/rules/tools.js +58 -0
- package/dist/rules/tools.js.map +1 -0
- package/dist/rules/transition-service.d.ts +49 -0
- package/dist/rules/transition-service.d.ts.map +1 -0
- package/dist/rules/transition-service.js +232 -0
- package/dist/rules/transition-service.js.map +1 -0
- package/dist/rules/types.d.ts +234 -0
- package/dist/rules/types.d.ts.map +1 -0
- package/dist/rules/types.js +38 -0
- package/dist/rules/types.js.map +1 -0
- package/dist/rules/validation.d.ts +11 -0
- package/dist/rules/validation.d.ts.map +1 -0
- package/dist/rules/validation.js +308 -0
- package/dist/rules/validation.js.map +1 -0
- package/dist/sandbox/fleet.d.ts +222 -0
- package/dist/sandbox/fleet.d.ts.map +1 -0
- package/dist/sandbox/fleet.js +322 -0
- package/dist/sandbox/fleet.js.map +1 -0
- package/dist/sandbox/reattach.d.ts +3 -0
- package/dist/sandbox/reattach.d.ts.map +1 -0
- package/dist/sandbox/reattach.js +16 -0
- package/dist/sandbox/reattach.js.map +1 -0
- package/dist/server-error.d.ts +12 -0
- package/dist/server-error.d.ts.map +1 -0
- package/dist/server-error.js +16 -0
- package/dist/server-error.js.map +1 -0
- package/dist/skills/service.d.ts +48 -0
- package/dist/skills/service.d.ts.map +1 -0
- package/dist/skills/service.js +40 -0
- package/dist/skills/service.js.map +1 -0
- package/dist/spa-static.d.ts +33 -0
- package/dist/spa-static.d.ts.map +1 -0
- package/dist/spa-static.js +104 -0
- package/dist/spa-static.js.map +1 -0
- package/dist/state-signing.d.ts +59 -0
- package/dist/state-signing.d.ts.map +1 -0
- package/dist/state-signing.js +83 -0
- package/dist/state-signing.js.map +1 -0
- package/dist/storage/domains/audit/agent-audit.d.ts +27 -0
- package/dist/storage/domains/audit/agent-audit.d.ts.map +1 -0
- package/dist/storage/domains/audit/agent-audit.js +66 -0
- package/dist/storage/domains/audit/agent-audit.js.map +1 -0
- package/dist/storage/domains/audit/base.d.ts +140 -0
- package/dist/storage/domains/audit/base.d.ts.map +1 -0
- package/dist/storage/domains/audit/base.js +184 -0
- package/dist/storage/domains/audit/base.js.map +1 -0
- package/dist/storage/domains/audit/domain.d.ts +69 -0
- package/dist/storage/domains/audit/domain.d.ts.map +1 -0
- package/dist/storage/domains/audit/domain.js +167 -0
- package/dist/storage/domains/audit/domain.js.map +1 -0
- package/dist/storage/domains/channel-identity/base.d.ts +96 -0
- package/dist/storage/domains/channel-identity/base.d.ts.map +1 -0
- package/dist/storage/domains/channel-identity/base.js +149 -0
- package/dist/storage/domains/channel-identity/base.js.map +1 -0
- package/dist/storage/domains/credentials/base.d.ts +134 -0
- package/dist/storage/domains/credentials/base.d.ts.map +1 -0
- package/dist/storage/domains/credentials/base.js +273 -0
- package/dist/storage/domains/credentials/base.js.map +1 -0
- package/dist/storage/domains/custom-providers/base.d.ts +57 -0
- package/dist/storage/domains/custom-providers/base.d.ts.map +1 -0
- package/dist/storage/domains/custom-providers/base.js +145 -0
- package/dist/storage/domains/custom-providers/base.js.map +1 -0
- package/dist/storage/domains/intake/base.d.ts +38 -0
- package/dist/storage/domains/intake/base.d.ts.map +1 -0
- package/dist/storage/domains/intake/base.js +80 -0
- package/dist/storage/domains/intake/base.js.map +1 -0
- package/dist/storage/domains/integrations/base.d.ts +126 -0
- package/dist/storage/domains/integrations/base.d.ts.map +1 -0
- package/dist/storage/domains/integrations/base.js +317 -0
- package/dist/storage/domains/integrations/base.js.map +1 -0
- package/dist/storage/domains/memory-settings/base.d.ts +62 -0
- package/dist/storage/domains/memory-settings/base.d.ts.map +1 -0
- package/dist/storage/domains/memory-settings/base.js +126 -0
- package/dist/storage/domains/memory-settings/base.js.map +1 -0
- package/dist/storage/domains/model-packs/base.d.ts +49 -0
- package/dist/storage/domains/model-packs/base.d.ts.map +1 -0
- package/dist/storage/domains/model-packs/base.js +96 -0
- package/dist/storage/domains/model-packs/base.js.map +1 -0
- package/dist/storage/domains/projects/base.d.ts +58 -0
- package/dist/storage/domains/projects/base.d.ts.map +1 -0
- package/dist/storage/domains/projects/base.js +111 -0
- package/dist/storage/domains/projects/base.js.map +1 -0
- package/dist/storage/domains/queue-health/base.d.ts +52 -0
- package/dist/storage/domains/queue-health/base.d.ts.map +1 -0
- package/dist/storage/domains/queue-health/base.js +122 -0
- package/dist/storage/domains/queue-health/base.js.map +1 -0
- package/dist/storage/domains/source-control/base.d.ts +358 -0
- package/dist/storage/domains/source-control/base.d.ts.map +1 -0
- package/dist/storage/domains/source-control/base.js +908 -0
- package/dist/storage/domains/source-control/base.js.map +1 -0
- package/dist/storage/domains/source-control/inmemory.d.ts +175 -0
- package/dist/storage/domains/source-control/inmemory.d.ts.map +1 -0
- package/dist/storage/domains/source-control/inmemory.js +377 -0
- package/dist/storage/domains/source-control/inmemory.js.map +1 -0
- package/dist/storage/domains/work-items/base.d.ts +412 -0
- package/dist/storage/domains/work-items/base.d.ts.map +1 -0
- package/dist/storage/domains/work-items/base.js +1309 -0
- package/dist/storage/domains/work-items/base.js.map +1 -0
- package/dist/storage/domains/work-items/metrics.d.ts +101 -0
- package/dist/storage/domains/work-items/metrics.d.ts.map +1 -0
- package/dist/storage/domains/work-items/metrics.js +221 -0
- package/dist/storage/domains/work-items/metrics.js.map +1 -0
- package/dist/timing.d.ts +15 -0
- package/dist/timing.d.ts.map +1 -0
- package/dist/timing.js +34 -0
- package/dist/timing.js.map +1 -0
- package/dist/workspace.d.ts +24 -0
- package/dist/workspace.d.ts.map +1 -0
- package/dist/workspace.js +286 -0
- package/dist/workspace.js.map +1 -0
- package/factory-skills/configure-factory-rules/SKILL.md +67 -0
- package/factory-skills/factory-plan/SKILL.md +61 -0
- package/factory-skills/factory-review/SKILL.md +155 -0
- package/factory-skills/factory-triage/SKILL.md +77 -0
- package/package.json +81 -0
|
@@ -0,0 +1,672 @@
|
|
|
1
|
+
import { buildLinearRoutes } from "./routes.js";
|
|
2
|
+
import { attachLinearRules } from "./rules.js";
|
|
3
|
+
import { buildLinearAgentTools } from "./agent-tools.js";
|
|
4
|
+
//#region src/integrations/linear/integration.ts
|
|
5
|
+
const LINEAR_GRAPHQL_URL = "https://api.linear.app/graphql";
|
|
6
|
+
const LINEAR_TOKEN_URL = "https://api.linear.app/oauth/token";
|
|
7
|
+
const LINEAR_AUTHORIZE_URL = "https://linear.app/oauth/authorize";
|
|
8
|
+
const LINEAR_ISSUES_PAGE_SIZE = 30;
|
|
9
|
+
const ISSUE_COMMENTS_PAGE_SIZE = 50;
|
|
10
|
+
/** Hard stop for comment pagination so a misbehaving cursor can't loop forever. */
|
|
11
|
+
const ISSUE_COMMENTS_MAX_PAGES = 20;
|
|
12
|
+
/** Refresh this many ms before the recorded expiry to absorb clock skew. */
|
|
13
|
+
const TOKEN_REFRESH_SKEW_MS = 6e4;
|
|
14
|
+
/** Re-check an org's Linear connection (and its scopes) at most this often. */
|
|
15
|
+
const CONNECTION_TTL_MS = 6e4;
|
|
16
|
+
const UUID_PATTERN = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
|
|
17
|
+
/** Thrown when the org's Linear authorization can no longer be renewed. */
|
|
18
|
+
var LinearReauthRequiredError = class extends Error {
|
|
19
|
+
constructor() {
|
|
20
|
+
super("Linear authorization expired. Reconnect Linear to keep syncing intake issues.");
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
/** POST a GraphQL query to Linear with the given OAuth access token. */
|
|
24
|
+
async function linearGraphql(accessToken, query, variables) {
|
|
25
|
+
const res = await fetch(LINEAR_GRAPHQL_URL, {
|
|
26
|
+
method: "POST",
|
|
27
|
+
signal: AbortSignal.timeout(15e3),
|
|
28
|
+
headers: {
|
|
29
|
+
"content-type": "application/json",
|
|
30
|
+
authorization: `Bearer ${accessToken}`
|
|
31
|
+
},
|
|
32
|
+
body: JSON.stringify({
|
|
33
|
+
query,
|
|
34
|
+
variables
|
|
35
|
+
})
|
|
36
|
+
});
|
|
37
|
+
if (!res.ok) {
|
|
38
|
+
let detail = null;
|
|
39
|
+
try {
|
|
40
|
+
detail = (await res.json()).errors?.[0]?.message ?? null;
|
|
41
|
+
} catch {}
|
|
42
|
+
const err = /* @__PURE__ */ new Error(`Linear API request failed (${res.status})${detail ? `: ${detail}` : ""}`);
|
|
43
|
+
err.status = res.status;
|
|
44
|
+
throw err;
|
|
45
|
+
}
|
|
46
|
+
const body = await res.json();
|
|
47
|
+
if (body.errors?.length) throw new Error(`Linear API error: ${body.errors[0]?.message ?? "unknown error"}`);
|
|
48
|
+
if (!body.data) throw new Error("Linear API returned no data.");
|
|
49
|
+
return body.data;
|
|
50
|
+
}
|
|
51
|
+
var LinearIntegration = class {
|
|
52
|
+
/** Stable integration identifier (see `../base.ts`). */
|
|
53
|
+
id = "linear";
|
|
54
|
+
/** Bound once by the factory via `initialize()` before any surface is used. */
|
|
55
|
+
#storage;
|
|
56
|
+
#projects;
|
|
57
|
+
#auth;
|
|
58
|
+
/** Bind Linear's slice of the generic integration storage, the projects domain, and the host auth seam. */
|
|
59
|
+
initialize({ storage, projects, auth }) {
|
|
60
|
+
this.#storage = storage;
|
|
61
|
+
this.#projects = projects;
|
|
62
|
+
this.#auth = auth;
|
|
63
|
+
}
|
|
64
|
+
get storage() {
|
|
65
|
+
if (!this.#storage) throw new Error("LinearIntegration is not initialized — the factory binds storage during prepare().");
|
|
66
|
+
return this.#storage;
|
|
67
|
+
}
|
|
68
|
+
/** Factory projects domain — maps a session's resourceId to its owning org. */
|
|
69
|
+
get projects() {
|
|
70
|
+
if (!this.#projects) throw new Error("LinearIntegration is not initialized — the factory binds storage during prepare().");
|
|
71
|
+
return this.#projects;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Whether the host runs with web auth enabled. Linear connections are
|
|
75
|
+
* org-owned, so every Linear surface is inert without a tenant auth seam.
|
|
76
|
+
*/
|
|
77
|
+
get authEnabled() {
|
|
78
|
+
return this.#auth?.enabled() ?? false;
|
|
79
|
+
}
|
|
80
|
+
/** Load the org's Linear connection, or `null` when not connected. */
|
|
81
|
+
async loadConnection(orgId) {
|
|
82
|
+
const connection = await this.storage.connections.get(orgId);
|
|
83
|
+
if (!connection) return null;
|
|
84
|
+
const { data } = connection;
|
|
85
|
+
return {
|
|
86
|
+
id: connection.id,
|
|
87
|
+
orgId: connection.orgId,
|
|
88
|
+
userId: connection.userId,
|
|
89
|
+
accessToken: data.accessToken,
|
|
90
|
+
scope: data.scope ?? null,
|
|
91
|
+
refreshToken: data.refreshToken ?? null,
|
|
92
|
+
expiresAt: data.expiresAtMs === null || data.expiresAtMs === void 0 ? null : new Date(data.expiresAtMs),
|
|
93
|
+
workspaceName: data.workspaceName ?? null,
|
|
94
|
+
workspaceUrlKey: data.workspaceUrlKey ?? null,
|
|
95
|
+
createdAt: connection.createdAt,
|
|
96
|
+
updatedAt: connection.updatedAt
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
/** Insert or replace the org's connection (one per org). */
|
|
100
|
+
async upsertConnection(input) {
|
|
101
|
+
await this.storage.connections.upsert(input.orgId, {
|
|
102
|
+
userId: input.userId,
|
|
103
|
+
data: {
|
|
104
|
+
accessToken: input.accessToken,
|
|
105
|
+
refreshToken: input.refreshToken,
|
|
106
|
+
expiresAtMs: input.expiresAt?.getTime() ?? null,
|
|
107
|
+
scope: input.scope,
|
|
108
|
+
workspaceName: input.workspaceName,
|
|
109
|
+
workspaceUrlKey: input.workspaceUrlKey
|
|
110
|
+
}
|
|
111
|
+
});
|
|
112
|
+
this.invalidateConnectionCache(input.orgId);
|
|
113
|
+
}
|
|
114
|
+
/** Persist a rotated token set on the org's existing connection row. */
|
|
115
|
+
async #updateTokens(orgId, tokens) {
|
|
116
|
+
await this.storage.connections.update(orgId, (data) => ({
|
|
117
|
+
...data,
|
|
118
|
+
accessToken: tokens.accessToken,
|
|
119
|
+
refreshToken: tokens.refreshToken,
|
|
120
|
+
expiresAtMs: tokens.expiresAt?.getTime() ?? null,
|
|
121
|
+
scope: tokens.scope ?? data.scope
|
|
122
|
+
}));
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Whether the connection's token can post issue comments. Legacy rows
|
|
126
|
+
* without a recorded scope were minted with `read` only, so they count as
|
|
127
|
+
* read-only until the org reconnects Linear.
|
|
128
|
+
*/
|
|
129
|
+
canPostComments(connection) {
|
|
130
|
+
return (connection.scope ?? "").split(/[\s,]+/).filter(Boolean).some((scope) => scope === "comments:create" || scope === "write" || scope === "admin");
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* In-flight refreshes keyed by org. Linear rotates refresh tokens, so two
|
|
134
|
+
* concurrent refreshes with the same token would invalidate each other —
|
|
135
|
+
* single-flight ensures one exchange per org and shares the result.
|
|
136
|
+
*/
|
|
137
|
+
#inflightRefreshes = /* @__PURE__ */ new Map();
|
|
138
|
+
/**
|
|
139
|
+
* Return a usable access token for the connection, proactively refreshing
|
|
140
|
+
* it when the recorded expiry is past (or imminent). Throws
|
|
141
|
+
* `LinearReauthRequiredError` when the token is expired and cannot be
|
|
142
|
+
* refreshed — the org has to go through the OAuth flow again.
|
|
143
|
+
*/
|
|
144
|
+
async getFreshAccessToken(connection) {
|
|
145
|
+
if (!(connection.expiresAt !== null && connection.expiresAt.getTime() - TOKEN_REFRESH_SKEW_MS <= Date.now())) return connection.accessToken;
|
|
146
|
+
if (!connection.refreshToken) throw new LinearReauthRequiredError();
|
|
147
|
+
const existing = this.#inflightRefreshes.get(connection.orgId);
|
|
148
|
+
if (existing) return existing;
|
|
149
|
+
const latest = await this.loadConnection(connection.orgId);
|
|
150
|
+
if (!latest) throw new LinearReauthRequiredError();
|
|
151
|
+
const concurrent = this.#inflightRefreshes.get(connection.orgId);
|
|
152
|
+
if (concurrent) return concurrent;
|
|
153
|
+
if (!(latest.expiresAt !== null && latest.expiresAt.getTime() - TOKEN_REFRESH_SKEW_MS <= Date.now())) return latest.accessToken;
|
|
154
|
+
if (!latest.refreshToken) throw new LinearReauthRequiredError();
|
|
155
|
+
const refreshToken = latest.refreshToken;
|
|
156
|
+
const refresh = (async () => {
|
|
157
|
+
try {
|
|
158
|
+
const tokens = await this.refreshAccessToken(refreshToken);
|
|
159
|
+
await this.#updateTokens(connection.orgId, tokens);
|
|
160
|
+
return tokens.accessToken;
|
|
161
|
+
} catch (err) {
|
|
162
|
+
const status = err.status;
|
|
163
|
+
if (status === 400 || status === 401) throw new LinearReauthRequiredError();
|
|
164
|
+
throw err;
|
|
165
|
+
} finally {
|
|
166
|
+
this.#inflightRefreshes.delete(connection.orgId);
|
|
167
|
+
}
|
|
168
|
+
})();
|
|
169
|
+
this.#inflightRefreshes.set(connection.orgId, refresh);
|
|
170
|
+
return refresh;
|
|
171
|
+
}
|
|
172
|
+
/** Re-check an org's Linear connection (and its scopes) at most this often. */
|
|
173
|
+
#connectionChecks = /* @__PURE__ */ new Map();
|
|
174
|
+
/**
|
|
175
|
+
* Whether the org has an active connection and whether its granted scope
|
|
176
|
+
* allows posting comments. Cached per org with a short TTL — tool-set
|
|
177
|
+
* resolution runs on every request.
|
|
178
|
+
*/
|
|
179
|
+
async checkConnection(orgId) {
|
|
180
|
+
const cached = this.#connectionChecks.get(orgId);
|
|
181
|
+
if (cached && Date.now() - cached.checkedAt < CONNECTION_TTL_MS) return cached;
|
|
182
|
+
const connection = await this.loadConnection(orgId);
|
|
183
|
+
const check = {
|
|
184
|
+
connected: connection !== null,
|
|
185
|
+
canComment: connection !== null && this.canPostComments(connection),
|
|
186
|
+
checkedAt: Date.now()
|
|
187
|
+
};
|
|
188
|
+
this.#connectionChecks.set(orgId, check);
|
|
189
|
+
return check;
|
|
190
|
+
}
|
|
191
|
+
/**
|
|
192
|
+
* Drop the cached connection check for an org. Called after a connection is
|
|
193
|
+
* persisted so the tools show up on the very next run instead of after the
|
|
194
|
+
* TTL lapses.
|
|
195
|
+
*/
|
|
196
|
+
invalidateConnectionCache(orgId) {
|
|
197
|
+
this.#connectionChecks.delete(orgId);
|
|
198
|
+
}
|
|
199
|
+
/**
|
|
200
|
+
* A project's org never changes, so the resourceId → orgId mapping is
|
|
201
|
+
* cached forever. `null` marks resource ids that aren't factory projects
|
|
202
|
+
* (e.g. local default resources) so we don't re-query them on every
|
|
203
|
+
* tool-set resolution.
|
|
204
|
+
*/
|
|
205
|
+
#orgIdByResourceId = /* @__PURE__ */ new Map();
|
|
206
|
+
/** Map a session's resourceId to its owning org, or `null` when it isn't a project. */
|
|
207
|
+
async resolveOrgId(resourceId) {
|
|
208
|
+
const cached = this.#orgIdByResourceId.get(resourceId);
|
|
209
|
+
if (cached !== void 0) return cached;
|
|
210
|
+
if (!UUID_PATTERN.test(resourceId)) {
|
|
211
|
+
this.#orgIdByResourceId.set(resourceId, null);
|
|
212
|
+
return null;
|
|
213
|
+
}
|
|
214
|
+
let orgId;
|
|
215
|
+
try {
|
|
216
|
+
await this.projects.ensureReady();
|
|
217
|
+
orgId = (await this.projects.getById({ id: resourceId }))?.orgId ?? null;
|
|
218
|
+
} catch {
|
|
219
|
+
return null;
|
|
220
|
+
}
|
|
221
|
+
this.#orgIdByResourceId.set(resourceId, orgId);
|
|
222
|
+
return orgId;
|
|
223
|
+
}
|
|
224
|
+
/** Test hook: clear the org/connection caches between specs. */
|
|
225
|
+
clearCaches() {
|
|
226
|
+
this.#orgIdByResourceId.clear();
|
|
227
|
+
this.#connectionChecks.clear();
|
|
228
|
+
}
|
|
229
|
+
intake = {
|
|
230
|
+
resolveIntakeDispatch: (input) => this.#resolveIntakeDispatch(input),
|
|
231
|
+
listSources: async ({ orgId }) => {
|
|
232
|
+
const connection = await this.loadConnection(orgId);
|
|
233
|
+
if (!connection) return [];
|
|
234
|
+
const accessToken = await this.getFreshAccessToken(connection);
|
|
235
|
+
return (await this.listProjects(accessToken)).map((project) => ({
|
|
236
|
+
id: project.id,
|
|
237
|
+
name: project.name,
|
|
238
|
+
type: "project"
|
|
239
|
+
}));
|
|
240
|
+
},
|
|
241
|
+
listItems: async ({ orgId, sourceIds, cursor }) => {
|
|
242
|
+
if (sourceIds.length === 0) return {
|
|
243
|
+
items: [],
|
|
244
|
+
nextCursor: null
|
|
245
|
+
};
|
|
246
|
+
const connection = await this.loadConnection(orgId);
|
|
247
|
+
if (!connection) return {
|
|
248
|
+
items: [],
|
|
249
|
+
nextCursor: null
|
|
250
|
+
};
|
|
251
|
+
const accessToken = await this.getFreshAccessToken(connection);
|
|
252
|
+
const page = await this.listActiveIssues(accessToken, cursor, sourceIds);
|
|
253
|
+
return {
|
|
254
|
+
items: page.issues.map((issue) => ({
|
|
255
|
+
source: {
|
|
256
|
+
type: "issue",
|
|
257
|
+
externalId: issue.id,
|
|
258
|
+
url: issue.url
|
|
259
|
+
},
|
|
260
|
+
sourceId: issue.projectId,
|
|
261
|
+
title: `${issue.identifier}: ${issue.title}`,
|
|
262
|
+
status: issue.state,
|
|
263
|
+
labels: issue.labels,
|
|
264
|
+
assignee: issue.assignee,
|
|
265
|
+
createdAt: issue.createdAt,
|
|
266
|
+
updatedAt: issue.updatedAt,
|
|
267
|
+
metadata: {
|
|
268
|
+
identifier: issue.identifier,
|
|
269
|
+
stateType: issue.stateType,
|
|
270
|
+
priority: issue.priorityLabel,
|
|
271
|
+
team: issue.team
|
|
272
|
+
}
|
|
273
|
+
})),
|
|
274
|
+
nextCursor: page.nextCursor
|
|
275
|
+
};
|
|
276
|
+
},
|
|
277
|
+
listIssues: (input) => this.#listIntakeIssues(input),
|
|
278
|
+
getIssue: (input) => this.#getIntakeIssue(input),
|
|
279
|
+
createComment: (input) => this.#createIntakeComment(input),
|
|
280
|
+
updateIssue: (input) => this.#updateIntakeIssue(input)
|
|
281
|
+
};
|
|
282
|
+
/**
|
|
283
|
+
* Background-dispatch context: the org's OAuth connection with a fresh
|
|
284
|
+
* token. Linear work items store the issue UUID directly as `externalId`.
|
|
285
|
+
*/
|
|
286
|
+
async #resolveIntakeDispatch({ orgId, externalSource }) {
|
|
287
|
+
if (externalSource.type !== "issue") return null;
|
|
288
|
+
const connection = await this.loadConnection(orgId);
|
|
289
|
+
if (!connection) return null;
|
|
290
|
+
return {
|
|
291
|
+
connection: {
|
|
292
|
+
type: "oauth",
|
|
293
|
+
accessToken: await this.getFreshAccessToken(connection)
|
|
294
|
+
},
|
|
295
|
+
issueId: externalSource.externalId
|
|
296
|
+
};
|
|
297
|
+
}
|
|
298
|
+
/**
|
|
299
|
+
* The OAuth connect/callback flow round-trips a signed `state` through
|
|
300
|
+
* Linear, so a multi-replica deploy needs a deployment-stable state secret.
|
|
301
|
+
*/
|
|
302
|
+
requiresStableStateSigner = true;
|
|
303
|
+
#clientId;
|
|
304
|
+
#clientSecret;
|
|
305
|
+
constructor(config) {
|
|
306
|
+
const missing = ["clientId", "clientSecret"].filter((key) => !config[key]);
|
|
307
|
+
if (missing.length > 0) throw new Error(`LinearIntegration is missing required config: ${missing.join(", ")}.`);
|
|
308
|
+
this.#clientId = config.clientId;
|
|
309
|
+
this.#clientSecret = config.clientSecret;
|
|
310
|
+
}
|
|
311
|
+
/**
|
|
312
|
+
* Build the OAuth authorize URL. `prompt=consent` forces the workspace
|
|
313
|
+
* picker even for an already-authorized user, so "reconnect" can switch
|
|
314
|
+
* workspaces.
|
|
315
|
+
*/
|
|
316
|
+
buildAuthorizeUrl(state, redirectUri) {
|
|
317
|
+
const url = new URL(LINEAR_AUTHORIZE_URL);
|
|
318
|
+
url.searchParams.set("client_id", this.#clientId);
|
|
319
|
+
url.searchParams.set("redirect_uri", redirectUri);
|
|
320
|
+
url.searchParams.set("response_type", "code");
|
|
321
|
+
url.searchParams.set("scope", "read,comments:create");
|
|
322
|
+
url.searchParams.set("state", state);
|
|
323
|
+
url.searchParams.set("prompt", "consent");
|
|
324
|
+
return url.toString();
|
|
325
|
+
}
|
|
326
|
+
/** Exchange an OAuth `code` for a workspace-scoped token set. */
|
|
327
|
+
async exchangeOAuthCode(code, redirectUri) {
|
|
328
|
+
return this.#requestTokens({
|
|
329
|
+
grant_type: "authorization_code",
|
|
330
|
+
code,
|
|
331
|
+
redirect_uri: redirectUri
|
|
332
|
+
}, "token exchange");
|
|
333
|
+
}
|
|
334
|
+
/**
|
|
335
|
+
* Exchange a refresh token for a new token set. Linear rotates refresh
|
|
336
|
+
* tokens, so the returned set replaces the stored one entirely. A 400/401
|
|
337
|
+
* here means the refresh token is invalid/revoked and the org must
|
|
338
|
+
* re-authorize.
|
|
339
|
+
*/
|
|
340
|
+
async refreshAccessToken(refreshToken) {
|
|
341
|
+
return this.#requestTokens({
|
|
342
|
+
grant_type: "refresh_token",
|
|
343
|
+
refresh_token: refreshToken
|
|
344
|
+
}, "token refresh");
|
|
345
|
+
}
|
|
346
|
+
/** POST to Linear's token endpoint and normalize the response. */
|
|
347
|
+
async #requestTokens(params, label) {
|
|
348
|
+
const res = await fetch(LINEAR_TOKEN_URL, {
|
|
349
|
+
method: "POST",
|
|
350
|
+
signal: AbortSignal.timeout(1e4),
|
|
351
|
+
headers: { "content-type": "application/x-www-form-urlencoded" },
|
|
352
|
+
body: new URLSearchParams({
|
|
353
|
+
...params,
|
|
354
|
+
client_id: this.#clientId,
|
|
355
|
+
client_secret: this.#clientSecret
|
|
356
|
+
})
|
|
357
|
+
});
|
|
358
|
+
if (!res.ok) {
|
|
359
|
+
const err = /* @__PURE__ */ new Error(`Linear ${label} failed (${res.status})`);
|
|
360
|
+
err.status = res.status;
|
|
361
|
+
throw err;
|
|
362
|
+
}
|
|
363
|
+
const body = await res.json();
|
|
364
|
+
if (!body.access_token) throw new Error(`Linear ${label} returned no access token.`);
|
|
365
|
+
return {
|
|
366
|
+
accessToken: body.access_token,
|
|
367
|
+
refreshToken: body.refresh_token ?? null,
|
|
368
|
+
expiresAt: typeof body.expires_in === "number" ? new Date(Date.now() + body.expires_in * 1e3) : null,
|
|
369
|
+
scope: body.scope ?? null
|
|
370
|
+
};
|
|
371
|
+
}
|
|
372
|
+
/** Fetch the workspace (organization) the access token is scoped to. */
|
|
373
|
+
async fetchWorkspace(accessToken) {
|
|
374
|
+
const data = await linearGraphql(accessToken, `query { organization { name urlKey } }`);
|
|
375
|
+
return {
|
|
376
|
+
name: data.organization.name,
|
|
377
|
+
urlKey: data.organization.urlKey
|
|
378
|
+
};
|
|
379
|
+
}
|
|
380
|
+
async #listIntakeIssues(input) {
|
|
381
|
+
const accessToken = getLinearAccessToken(input.connection);
|
|
382
|
+
const result = await this.listActiveIssues(accessToken, input.cursor, input.sourceIds, input.labels);
|
|
383
|
+
return {
|
|
384
|
+
issues: result.issues.map((issue) => linearIssueToIntakeIssue(issue)),
|
|
385
|
+
nextCursor: result.nextCursor
|
|
386
|
+
};
|
|
387
|
+
}
|
|
388
|
+
async #getIntakeIssue(input) {
|
|
389
|
+
const accessToken = getLinearAccessToken(input.connection);
|
|
390
|
+
const issue = await this.fetchIssueDetail(accessToken, input.issueId);
|
|
391
|
+
if (!issue) return null;
|
|
392
|
+
return {
|
|
393
|
+
...linearIssueToIntakeIssue(issue),
|
|
394
|
+
description: issue.description,
|
|
395
|
+
commentCount: issue.comments.length,
|
|
396
|
+
comments: issue.comments
|
|
397
|
+
};
|
|
398
|
+
}
|
|
399
|
+
async #createIntakeComment(input) {
|
|
400
|
+
const accessToken = getLinearAccessToken(input.connection);
|
|
401
|
+
return this.createIssueComment(accessToken, input.issueId, input.body);
|
|
402
|
+
}
|
|
403
|
+
async #updateIntakeIssue(input) {
|
|
404
|
+
const accessToken = getLinearAccessToken(input.connection);
|
|
405
|
+
const issue = await this.fetchIssueDetail(accessToken, input.issueId);
|
|
406
|
+
if (!issue) return null;
|
|
407
|
+
const teamKey = issue.team;
|
|
408
|
+
if (!teamKey) return null;
|
|
409
|
+
const states = await this.#listTeamWorkflowStates(accessToken, teamKey);
|
|
410
|
+
let targetState = null;
|
|
411
|
+
if (input.state.kind === "byType") {
|
|
412
|
+
const wantedType = input.state.stateType;
|
|
413
|
+
targetState = states.find((state) => state.type === wantedType) ?? null;
|
|
414
|
+
} else {
|
|
415
|
+
const wanted = input.state.name.toLowerCase();
|
|
416
|
+
targetState = states.find((state) => state.name.toLowerCase() === wanted) ?? null;
|
|
417
|
+
}
|
|
418
|
+
if (!targetState) return null;
|
|
419
|
+
if (targetState.name === issue.state) return linearIssueToIntakeIssue(issue);
|
|
420
|
+
if (!(await linearGraphql(accessToken, `mutation UpdateIssueState($id: String!, $stateId: String!) {
|
|
421
|
+
issueUpdate(id: $id, input: { stateId: $stateId }) { success }
|
|
422
|
+
}`, {
|
|
423
|
+
id: issue.id,
|
|
424
|
+
stateId: targetState.id
|
|
425
|
+
})).issueUpdate.success) throw new Error("Linear did not accept the issue update.");
|
|
426
|
+
const fresh = await this.fetchIssueDetail(accessToken, issue.id);
|
|
427
|
+
if (!fresh) return null;
|
|
428
|
+
return linearIssueToIntakeIssue(fresh);
|
|
429
|
+
}
|
|
430
|
+
async #listTeamWorkflowStates(accessToken, teamKey) {
|
|
431
|
+
return (await linearGraphql(accessToken, `query TeamStates($key: String!) {
|
|
432
|
+
team(id: $key) { states(first: 100) { nodes { id name type } } }
|
|
433
|
+
}`, { key: teamKey })).team?.states.nodes ?? [];
|
|
434
|
+
}
|
|
435
|
+
/** List the workspace's projects (for the Settings intake-source picker). */
|
|
436
|
+
async listProjects(accessToken) {
|
|
437
|
+
return (await linearGraphql(accessToken, `query { projects(first: 100) { nodes { id name state teams(first: 10) { nodes { id key name } } } } }`)).projects.nodes.map((node) => ({
|
|
438
|
+
id: node.id,
|
|
439
|
+
name: node.name,
|
|
440
|
+
state: node.state,
|
|
441
|
+
teams: node.teams.nodes.map((team) => ({
|
|
442
|
+
id: team.id,
|
|
443
|
+
key: team.key,
|
|
444
|
+
name: team.name
|
|
445
|
+
}))
|
|
446
|
+
}));
|
|
447
|
+
}
|
|
448
|
+
/**
|
|
449
|
+
* List one page of the workspace's active issues (triage/backlog/unstarted/
|
|
450
|
+
* started — completed and canceled are excluded), most recently updated
|
|
451
|
+
* first. When `projectIds` is provided, only issues from those projects are
|
|
452
|
+
* returned.
|
|
453
|
+
*/
|
|
454
|
+
async listActiveIssues(accessToken, after, projectIds, labels) {
|
|
455
|
+
const normalizedLabels = [...new Set((labels ?? []).map((label) => label.trim()).filter(Boolean))];
|
|
456
|
+
const projectFilter = projectIds?.length ? ", project: { id: { in: $projectIds } }" : "";
|
|
457
|
+
const projectVar = projectIds?.length ? ", $projectIds: [ID!]" : "";
|
|
458
|
+
const labelFilter = normalizedLabels.length > 0 ? ", labels: { name: { in: $labels } }" : "";
|
|
459
|
+
const { nodes, pageInfo } = (await linearGraphql(accessToken, `query Intake($first: Int!, $after: String${projectVar}${normalizedLabels.length > 0 ? ", $labels: [String!]" : ""}) {
|
|
460
|
+
issues(
|
|
461
|
+
first: $first
|
|
462
|
+
after: $after
|
|
463
|
+
orderBy: updatedAt
|
|
464
|
+
filter: { state: { type: { in: ["triage", "backlog", "unstarted", "started"] } }${projectFilter}${labelFilter} }
|
|
465
|
+
) {
|
|
466
|
+
nodes {
|
|
467
|
+
id
|
|
468
|
+
identifier
|
|
469
|
+
title
|
|
470
|
+
url
|
|
471
|
+
priorityLabel
|
|
472
|
+
createdAt
|
|
473
|
+
updatedAt
|
|
474
|
+
state { name type }
|
|
475
|
+
project { id }
|
|
476
|
+
assignee { name }
|
|
477
|
+
team { key }
|
|
478
|
+
labels { nodes { name } }
|
|
479
|
+
}
|
|
480
|
+
pageInfo { hasNextPage endCursor }
|
|
481
|
+
}
|
|
482
|
+
}`, {
|
|
483
|
+
first: LINEAR_ISSUES_PAGE_SIZE,
|
|
484
|
+
after: after ?? null,
|
|
485
|
+
...projectIds?.length ? { projectIds } : {},
|
|
486
|
+
...normalizedLabels.length > 0 ? { labels: normalizedLabels } : {}
|
|
487
|
+
})).issues;
|
|
488
|
+
return {
|
|
489
|
+
issues: nodes.map((node) => ({
|
|
490
|
+
id: node.id,
|
|
491
|
+
projectId: node.project.id,
|
|
492
|
+
identifier: node.identifier,
|
|
493
|
+
title: node.title,
|
|
494
|
+
url: node.url,
|
|
495
|
+
state: node.state.name,
|
|
496
|
+
stateType: node.state.type,
|
|
497
|
+
priorityLabel: node.priorityLabel,
|
|
498
|
+
assignee: node.assignee?.name ?? null,
|
|
499
|
+
team: node.team?.key ?? null,
|
|
500
|
+
labels: node.labels.nodes.map((label) => label.name),
|
|
501
|
+
createdAt: node.createdAt,
|
|
502
|
+
updatedAt: node.updatedAt
|
|
503
|
+
})),
|
|
504
|
+
nextCursor: pageInfo.hasNextPage ? pageInfo.endCursor : null
|
|
505
|
+
};
|
|
506
|
+
}
|
|
507
|
+
/** Follow `comments.pageInfo` until exhausted so long discussions aren't truncated. */
|
|
508
|
+
async #fetchRemainingIssueComments(accessToken, issueId, firstPage) {
|
|
509
|
+
const nodes = [...firstPage.nodes];
|
|
510
|
+
let { hasNextPage, endCursor } = firstPage.pageInfo;
|
|
511
|
+
for (let page = 1; hasNextPage && endCursor && page < ISSUE_COMMENTS_MAX_PAGES; page++) {
|
|
512
|
+
const comments = (await linearGraphql(accessToken, `query IssueComments($id: String!, $first: Int!, $after: String!) {
|
|
513
|
+
issue(id: $id) {
|
|
514
|
+
comments(first: $first, after: $after) {
|
|
515
|
+
nodes { body createdAt user { name } }
|
|
516
|
+
pageInfo { hasNextPage endCursor }
|
|
517
|
+
}
|
|
518
|
+
}
|
|
519
|
+
}`, {
|
|
520
|
+
id: issueId,
|
|
521
|
+
first: ISSUE_COMMENTS_PAGE_SIZE,
|
|
522
|
+
after: endCursor
|
|
523
|
+
})).issue?.comments;
|
|
524
|
+
if (!comments) break;
|
|
525
|
+
nodes.push(...comments.nodes);
|
|
526
|
+
({hasNextPage, endCursor} = comments.pageInfo);
|
|
527
|
+
}
|
|
528
|
+
return nodes;
|
|
529
|
+
}
|
|
530
|
+
/**
|
|
531
|
+
* Fetch one issue with its description and comments. `idOrIdentifier`
|
|
532
|
+
* accepts both the Linear UUID and the human key (`ENG-123`). Returns
|
|
533
|
+
* `null` when the issue doesn't exist (Linear reports it as an "Entity not
|
|
534
|
+
* found" error).
|
|
535
|
+
*/
|
|
536
|
+
async fetchIssueDetail(accessToken, idOrIdentifier) {
|
|
537
|
+
let data;
|
|
538
|
+
try {
|
|
539
|
+
data = await linearGraphql(accessToken, `query IssueDetail($id: String!, $commentsFirst: Int!) {
|
|
540
|
+
issue(id: $id) {
|
|
541
|
+
id
|
|
542
|
+
identifier
|
|
543
|
+
title
|
|
544
|
+
description
|
|
545
|
+
url
|
|
546
|
+
priorityLabel
|
|
547
|
+
createdAt
|
|
548
|
+
updatedAt
|
|
549
|
+
state { name type }
|
|
550
|
+
project { id }
|
|
551
|
+
assignee { name }
|
|
552
|
+
team { key }
|
|
553
|
+
labels { nodes { name } }
|
|
554
|
+
comments(first: $commentsFirst) {
|
|
555
|
+
nodes { body createdAt user { name } }
|
|
556
|
+
pageInfo { hasNextPage endCursor }
|
|
557
|
+
}
|
|
558
|
+
}
|
|
559
|
+
}`, {
|
|
560
|
+
id: idOrIdentifier,
|
|
561
|
+
commentsFirst: ISSUE_COMMENTS_PAGE_SIZE
|
|
562
|
+
});
|
|
563
|
+
} catch (err) {
|
|
564
|
+
if (err instanceof Error && /entity not found/i.test(err.message)) return null;
|
|
565
|
+
throw err;
|
|
566
|
+
}
|
|
567
|
+
const issue = data.issue;
|
|
568
|
+
if (!issue) return null;
|
|
569
|
+
const comments = (await this.#fetchRemainingIssueComments(accessToken, issue.id, issue.comments)).sort((a, b) => new Date(a.createdAt).getTime() - new Date(b.createdAt).getTime());
|
|
570
|
+
return {
|
|
571
|
+
id: issue.id,
|
|
572
|
+
projectId: issue.project.id,
|
|
573
|
+
identifier: issue.identifier,
|
|
574
|
+
title: issue.title,
|
|
575
|
+
description: issue.description?.trim() ? issue.description : null,
|
|
576
|
+
url: issue.url,
|
|
577
|
+
state: issue.state.name,
|
|
578
|
+
stateType: issue.state.type,
|
|
579
|
+
priorityLabel: issue.priorityLabel,
|
|
580
|
+
assignee: issue.assignee?.name ?? null,
|
|
581
|
+
team: issue.team?.key ?? null,
|
|
582
|
+
labels: issue.labels.nodes.map((label) => label.name),
|
|
583
|
+
createdAt: issue.createdAt,
|
|
584
|
+
updatedAt: issue.updatedAt,
|
|
585
|
+
comments: comments.map((comment) => ({
|
|
586
|
+
author: comment.user?.name ?? null,
|
|
587
|
+
body: comment.body,
|
|
588
|
+
createdAt: comment.createdAt
|
|
589
|
+
}))
|
|
590
|
+
};
|
|
591
|
+
}
|
|
592
|
+
/**
|
|
593
|
+
* Post a comment on an issue. `idOrIdentifier` accepts both the Linear UUID
|
|
594
|
+
* and the human key (`ENG-123`) — the identifier is resolved to a UUID
|
|
595
|
+
* first because `commentCreate` only accepts UUIDs. Returns `null` when the
|
|
596
|
+
* issue doesn't exist.
|
|
597
|
+
*/
|
|
598
|
+
async createIssueComment(accessToken, idOrIdentifier, body) {
|
|
599
|
+
let issueId;
|
|
600
|
+
try {
|
|
601
|
+
const data = await linearGraphql(accessToken, `query IssueId($id: String!) { issue(id: $id) { id } }`, { id: idOrIdentifier });
|
|
602
|
+
if (!data.issue) return null;
|
|
603
|
+
issueId = data.issue.id;
|
|
604
|
+
} catch (err) {
|
|
605
|
+
if (err instanceof Error && /entity not found/i.test(err.message)) return null;
|
|
606
|
+
throw err;
|
|
607
|
+
}
|
|
608
|
+
const data = await linearGraphql(accessToken, `mutation CommentCreate($input: CommentCreateInput!) {
|
|
609
|
+
commentCreate(input: $input) { success comment { id url } }
|
|
610
|
+
}`, { input: {
|
|
611
|
+
issueId,
|
|
612
|
+
body
|
|
613
|
+
} });
|
|
614
|
+
if (!data.commentCreate.success || !data.commentCreate.comment) throw new Error("Linear did not accept the comment.");
|
|
615
|
+
return data.commentCreate.comment;
|
|
616
|
+
}
|
|
617
|
+
/**
|
|
618
|
+
* The integration's HTTP surface: `/web/linear/*` + `/auth/linear/*` Mastra
|
|
619
|
+
* `apiRoutes` (status, OAuth connect/callback, projects + issues for
|
|
620
|
+
* Intake). Handlers operate on this instance.
|
|
621
|
+
*/
|
|
622
|
+
routes(ctx) {
|
|
623
|
+
return buildLinearRoutes({
|
|
624
|
+
linear: this,
|
|
625
|
+
auth: ctx.auth,
|
|
626
|
+
stateSigner: ctx.stateSigner,
|
|
627
|
+
baseUrl: ctx.baseUrl,
|
|
628
|
+
intake: ctx.storage.intake,
|
|
629
|
+
ingestFactoryIssues: attachLinearRules(ctx)
|
|
630
|
+
});
|
|
631
|
+
}
|
|
632
|
+
/**
|
|
633
|
+
* Org-scoped agent tools: issue detail + comment tools for sessions whose
|
|
634
|
+
* project belongs to an org with an active Linear connection.
|
|
635
|
+
*/
|
|
636
|
+
async agentTools(args) {
|
|
637
|
+
return buildLinearAgentTools({
|
|
638
|
+
requestContext: args.requestContext,
|
|
639
|
+
linear: this
|
|
640
|
+
});
|
|
641
|
+
}
|
|
642
|
+
/** Non-secret config snapshot for system diagnostics/startup logs. */
|
|
643
|
+
diagnostics() {
|
|
644
|
+
return { oauthAppConfigured: true };
|
|
645
|
+
}
|
|
646
|
+
};
|
|
647
|
+
function getLinearAccessToken(connection) {
|
|
648
|
+
if (connection.type !== "oauth") throw new Error("Linear capabilities require an OAuth connection.");
|
|
649
|
+
return connection.accessToken;
|
|
650
|
+
}
|
|
651
|
+
function linearIssueToIntakeIssue(issue) {
|
|
652
|
+
return {
|
|
653
|
+
id: issue.id,
|
|
654
|
+
identifier: issue.identifier,
|
|
655
|
+
title: issue.title,
|
|
656
|
+
url: issue.url,
|
|
657
|
+
author: null,
|
|
658
|
+
state: issue.state,
|
|
659
|
+
stateType: issue.stateType,
|
|
660
|
+
priority: issue.priorityLabel,
|
|
661
|
+
assignee: issue.assignee,
|
|
662
|
+
source: issue.team,
|
|
663
|
+
labels: issue.labels,
|
|
664
|
+
commentCount: null,
|
|
665
|
+
createdAt: issue.createdAt,
|
|
666
|
+
updatedAt: issue.updatedAt
|
|
667
|
+
};
|
|
668
|
+
}
|
|
669
|
+
//#endregion
|
|
670
|
+
export { LinearIntegration, LinearReauthRequiredError };
|
|
671
|
+
|
|
672
|
+
//# sourceMappingURL=integration.js.map
|