@mastra/factory 0.1.0-alpha.6 → 0.1.0-alpha.7

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.
Files changed (37) hide show
  1. package/CHANGELOG.md +7 -0
  2. package/dist/factory.d.ts.map +1 -1
  3. package/dist/factory.js +140 -8
  4. package/dist/factory.js.map +1 -1
  5. package/dist/index.js +140 -8
  6. package/dist/index.js.map +1 -1
  7. package/dist/integrations/github/integration.js +108 -1
  8. package/dist/integrations/github/integration.js.map +1 -1
  9. package/dist/integrations/github/pat.d.ts +38 -0
  10. package/dist/integrations/github/pat.d.ts.map +1 -0
  11. package/dist/integrations/github/pat.js +48 -0
  12. package/dist/integrations/github/pat.js.map +1 -0
  13. package/dist/integrations/github/provenance.js.map +1 -1
  14. package/dist/integrations/github/routes.d.ts.map +1 -1
  15. package/dist/integrations/github/routes.js +94 -1
  16. package/dist/integrations/github/routes.js.map +1 -1
  17. package/dist/integrations/github/session-subscriptions.d.ts.map +1 -1
  18. package/dist/integrations/github/session-subscriptions.js +30 -0
  19. package/dist/integrations/github/session-subscriptions.js.map +1 -1
  20. package/dist/integrations/github/token-refresh.d.ts +6 -0
  21. package/dist/integrations/github/token-refresh.d.ts.map +1 -1
  22. package/dist/integrations/github/token-refresh.js +10 -0
  23. package/dist/integrations/github/token-refresh.js.map +1 -1
  24. package/dist/integrations/platform/github/integration.d.ts.map +1 -1
  25. package/dist/integrations/platform/github/integration.js +110 -2
  26. package/dist/integrations/platform/github/integration.js.map +1 -1
  27. package/dist/routes/surface.js +7 -1
  28. package/dist/routes/surface.js.map +1 -1
  29. package/dist/routes/work-items.js.map +1 -1
  30. package/dist/rules/start-coordinator.d.ts.map +1 -1
  31. package/dist/rules/start-coordinator.js +7 -1
  32. package/dist/rules/start-coordinator.js.map +1 -1
  33. package/dist/workspace.d.ts +5 -0
  34. package/dist/workspace.d.ts.map +1 -1
  35. package/dist/workspace.js +64 -5
  36. package/dist/workspace.js.map +1 -1
  37. package/package.json +6 -6
@@ -130,6 +130,48 @@ function getGithubFeatureDiagnostics(options) {
130
130
  };
131
131
  }
132
132
 
133
+ // src/integrations/github/pat.ts
134
+ var PAT_SETTINGS_USER_ID = "__github_org_settings__";
135
+ var FIELD_FOR_KIND = {
136
+ default: "pat",
137
+ reviewer: "reviewerPat"
138
+ };
139
+ function asToken(value) {
140
+ return typeof value === "string" && value.length > 0 ? value : null;
141
+ }
142
+ async function getGithubPat(getStorage, orgId, kind = "default") {
143
+ try {
144
+ const settings = await getStorage().settings.get(orgId, PAT_SETTINGS_USER_ID);
145
+ if (!settings) return null;
146
+ if (kind === "reviewer") return asToken(settings.reviewerPat) ?? asToken(settings.pat);
147
+ return asToken(settings.pat);
148
+ } catch {
149
+ return null;
150
+ }
151
+ }
152
+ async function getGithubPatStatus(getStorage, orgId) {
153
+ try {
154
+ const settings = await getStorage().settings.get(orgId, PAT_SETTINGS_USER_ID);
155
+ return {
156
+ configured: asToken(settings?.pat) !== null,
157
+ reviewerConfigured: asToken(settings?.reviewerPat) !== null
158
+ };
159
+ } catch {
160
+ return { configured: false, reviewerConfigured: false };
161
+ }
162
+ }
163
+ async function setGithubPat(storage, orgId, pat, kind = "default") {
164
+ const existing = await storage.settings.get(orgId, PAT_SETTINGS_USER_ID) ?? {};
165
+ await storage.settings.save(orgId, PAT_SETTINGS_USER_ID, { ...existing, [FIELD_FOR_KIND[kind]]: pat });
166
+ }
167
+ async function clearGithubPat(storage, orgId, kind = "default") {
168
+ const existing = await storage.settings.get(orgId, PAT_SETTINGS_USER_ID);
169
+ const field = FIELD_FOR_KIND[kind];
170
+ if (!existing?.[field]) return;
171
+ const { [field]: _removed, ...rest } = existing;
172
+ await storage.settings.save(orgId, PAT_SETTINGS_USER_ID, rest);
173
+ }
174
+
133
175
  // src/integrations/github/project-lock.ts
134
176
  import { createHash } from "crypto";
135
177
  var inProcessLocks = /* @__PURE__ */ new Map();
@@ -1439,6 +1481,56 @@ function buildGithubRoutes(options) {
1439
1481
  }
1440
1482
  })
1441
1483
  );
1484
+ const parsePatKind = (value) => {
1485
+ if (value === void 0 || value === null || value === "default") return "default";
1486
+ if (value === "reviewer") return "reviewer";
1487
+ return null;
1488
+ };
1489
+ routes.push(
1490
+ registerApiRoute("/web/github/pat", {
1491
+ method: "GET",
1492
+ requiresAuth: false,
1493
+ handler: async (c) => {
1494
+ const resolved = await resolveOrgTenant(loose(c), auth);
1495
+ if ("response" in resolved) return resolved.response;
1496
+ return c.json(await getGithubPatStatus(() => github.integrationStorage, resolved.tenant.orgId));
1497
+ }
1498
+ }),
1499
+ registerApiRoute("/web/github/pat", {
1500
+ method: "POST",
1501
+ requiresAuth: false,
1502
+ handler: async (c) => {
1503
+ const resolved = await resolveOrgTenant(loose(c), auth);
1504
+ if ("response" in resolved) return resolved.response;
1505
+ let body;
1506
+ try {
1507
+ body = await c.req.json();
1508
+ } catch {
1509
+ return c.json({ error: "Invalid JSON body" }, 400);
1510
+ }
1511
+ const kind = parsePatKind(body.kind);
1512
+ if (!kind) return c.json({ error: "kind must be 'default' or 'reviewer'" }, 400);
1513
+ const token = typeof body.token === "string" ? body.token.trim() : "";
1514
+ if (!token) return c.json({ error: "A token is required" }, 400);
1515
+ if (token.length > 500) return c.json({ error: "Token too long (max 500 characters)" }, 400);
1516
+ if (/\s/.test(token)) return c.json({ error: "Token must not contain whitespace" }, 400);
1517
+ await setGithubPat(github.integrationStorage, resolved.tenant.orgId, token, kind);
1518
+ return c.json(await getGithubPatStatus(() => github.integrationStorage, resolved.tenant.orgId));
1519
+ }
1520
+ }),
1521
+ registerApiRoute("/web/github/pat", {
1522
+ method: "DELETE",
1523
+ requiresAuth: false,
1524
+ handler: async (c) => {
1525
+ const resolved = await resolveOrgTenant(loose(c), auth);
1526
+ if ("response" in resolved) return resolved.response;
1527
+ const kind = parsePatKind(c.req.query("kind"));
1528
+ if (!kind) return c.json({ error: "kind must be 'default' or 'reviewer'" }, 400);
1529
+ await clearGithubPat(github.integrationStorage, resolved.tenant.orgId, kind);
1530
+ return c.json(await getGithubPatStatus(() => github.integrationStorage, resolved.tenant.orgId));
1531
+ }
1532
+ })
1533
+ );
1442
1534
  routes.push(...buildProjectGitRoutes({ github, auth, fleet, storage, emitAudit }));
1443
1535
  return routes;
1444
1536
  }
@@ -1481,11 +1573,12 @@ async function prepareProject(options) {
1481
1573
  if (!access.authorization) {
1482
1574
  throw new MaterializeError("Repository access did not include a bearer token.", "clone-failed");
1483
1575
  }
1576
+ const ghCliToken = await getGithubPat(() => github.integrationStorage, project.installation.orgId) ?? access.authorization.token;
1484
1577
  const sandbox = await ensureProjectSandbox({
1485
1578
  fleet,
1486
1579
  row: sandboxRow,
1487
1580
  storage: github.sourceControlStorage.sandboxes,
1488
- token: access.authorization.token,
1581
+ token: ghCliToken,
1489
1582
  onProgress
1490
1583
  });
1491
1584
  const fresh = await github.sourceControlStorage.sandboxes.getById({ id: sandboxRow.id });
@@ -1923,6 +2016,11 @@ import { z } from "zod";
1923
2016
 
1924
2017
  // src/integrations/github/token-refresh.ts
1925
2018
  var GITHUB_TOKEN_INJECTOR_CONTEXT_KEY = "factoryGithubTokenInjector";
2019
+ var GITHUB_PAT_KIND_CONTEXT_KEY = "factoryGithubPatKind";
2020
+ function getRegisteredGithubPatKind(requestContext) {
2021
+ const kind = requestContext.get(GITHUB_PAT_KIND_CONTEXT_KEY);
2022
+ return kind === "reviewer" ? "reviewer" : "default";
2023
+ }
1926
2024
  function injectGithubToken(requestContext, token) {
1927
2025
  const injector = requestContext.get(GITHUB_TOKEN_INJECTOR_CONTEXT_KEY);
1928
2026
  if (!injector) {
@@ -2020,6 +2118,15 @@ async function unsubscribeCurrentSessionFromPullRequest(requestContext, pullRequ
2020
2118
  }
2021
2119
  async function refreshGithubToken(requestContext, github) {
2022
2120
  const target = await resolveSessionTarget(requestContext, github);
2121
+ const pat = await getGithubPat(
2122
+ () => github.integrationStorage,
2123
+ target.orgId,
2124
+ getRegisteredGithubPatKind(requestContext)
2125
+ );
2126
+ if (pat) {
2127
+ injectGithubToken(requestContext, pat);
2128
+ return;
2129
+ }
2023
2130
  const access = await github.versionControl.getRepositoryAccess({
2024
2131
  orgId: target.orgId,
2025
2132
  repositoryId: target.repository.id