@mastra/factory 0.30.0 → 0.31.0-alpha.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +32 -10
- package/dist/factory.d.ts +12 -1
- package/dist/factory.d.ts.map +1 -1
- package/dist/factory.js +72 -31
- package/dist/factory.js.map +1 -1
- package/dist/integrations/gitlab/api.d.ts +3 -2
- package/dist/integrations/gitlab/api.d.ts.map +1 -1
- package/dist/integrations/gitlab/api.js +12 -9
- package/dist/integrations/gitlab/api.js.map +1 -1
- package/dist/integrations/issue-reconcile-worker.d.ts +5 -0
- package/dist/integrations/issue-reconcile-worker.d.ts.map +1 -1
- package/dist/integrations/issue-reconcile-worker.js +17 -1
- package/dist/integrations/issue-reconcile-worker.js.map +1 -1
- package/dist/integrations/platform/connect/routes.d.ts +22 -13
- package/dist/integrations/platform/connect/routes.d.ts.map +1 -1
- package/dist/integrations/platform/connect/routes.js +63 -64
- package/dist/integrations/platform/connect/routes.js.map +1 -1
- package/dist/integrations/platform/foundation/connect-binding.d.ts +125 -0
- package/dist/integrations/platform/foundation/connect-binding.d.ts.map +1 -0
- package/dist/integrations/platform/foundation/connect-binding.js +252 -0
- package/dist/integrations/platform/foundation/connect-binding.js.map +1 -0
- package/dist/integrations/platform/foundation/connect-event-poller.d.ts +49 -0
- package/dist/integrations/platform/foundation/connect-event-poller.d.ts.map +1 -0
- package/dist/integrations/platform/foundation/connect-event-poller.js +304 -0
- package/dist/integrations/platform/foundation/connect-event-poller.js.map +1 -0
- package/dist/integrations/platform/foundation/connect-events.d.ts +31 -0
- package/dist/integrations/platform/foundation/connect-events.d.ts.map +1 -0
- package/dist/integrations/platform/foundation/connect-events.js +7 -0
- package/dist/integrations/platform/foundation/connect-events.js.map +1 -0
- package/dist/integrations/platform/foundation/index.d.ts +10 -0
- package/dist/integrations/platform/foundation/index.d.ts.map +1 -0
- package/dist/integrations/platform/foundation/index.js +6 -0
- package/dist/integrations/platform/foundation/intake.d.ts +39 -0
- package/dist/integrations/platform/foundation/intake.d.ts.map +1 -0
- package/dist/integrations/platform/foundation/intake.js +45 -0
- package/dist/integrations/platform/foundation/intake.js.map +1 -0
- package/dist/integrations/platform/foundation/integration.d.ts +84 -0
- package/dist/integrations/platform/foundation/integration.d.ts.map +1 -0
- package/dist/integrations/platform/foundation/integration.js +107 -0
- package/dist/integrations/platform/foundation/integration.js.map +1 -0
- package/dist/integrations/platform/github/event-worker.d.ts +2 -2
- package/dist/integrations/platform/github/event-worker.d.ts.map +1 -1
- package/dist/integrations/platform/github/event-worker.js +4 -8
- package/dist/integrations/platform/github/event-worker.js.map +1 -1
- package/dist/integrations/platform/github/integration.d.ts +3 -0
- package/dist/integrations/platform/github/integration.d.ts.map +1 -1
- package/dist/integrations/platform/github/integration.js +131 -70
- package/dist/integrations/platform/github/integration.js.map +1 -1
- package/dist/integrations/platform/gitlab/connect-events.d.ts +22 -0
- package/dist/integrations/platform/gitlab/connect-events.d.ts.map +1 -0
- package/dist/integrations/platform/gitlab/connect-events.js +49 -0
- package/dist/integrations/platform/gitlab/connect-events.js.map +1 -0
- package/dist/integrations/platform/gitlab/integration.d.ts +24 -15
- package/dist/integrations/platform/gitlab/integration.d.ts.map +1 -1
- package/dist/integrations/platform/gitlab/integration.js +61 -26
- package/dist/integrations/platform/gitlab/integration.js.map +1 -1
- package/dist/integrations/platform/incidentio/intake.d.ts +33 -0
- package/dist/integrations/platform/incidentio/intake.d.ts.map +1 -0
- package/dist/integrations/platform/incidentio/intake.js +271 -0
- package/dist/integrations/platform/incidentio/intake.js.map +1 -0
- package/dist/integrations/platform/incidentio/integration.d.ts +11 -23
- package/dist/integrations/platform/incidentio/integration.d.ts.map +1 -1
- package/dist/integrations/platform/incidentio/integration.js +27 -297
- package/dist/integrations/platform/incidentio/integration.js.map +1 -1
- package/dist/integrations/platform/jira/intake.d.ts +60 -0
- package/dist/integrations/platform/jira/intake.d.ts.map +1 -0
- package/dist/integrations/platform/jira/intake.js +445 -0
- package/dist/integrations/platform/jira/intake.js.map +1 -0
- package/dist/integrations/platform/jira/integration.d.ts +13 -37
- package/dist/integrations/platform/jira/integration.d.ts.map +1 -1
- package/dist/integrations/platform/jira/integration.js +29 -468
- package/dist/integrations/platform/jira/integration.js.map +1 -1
- package/dist/integrations/platform/linear/event-worker.d.ts +2 -2
- package/dist/integrations/platform/linear/event-worker.d.ts.map +1 -1
- package/dist/integrations/platform/linear/event-worker.js +5 -9
- package/dist/integrations/platform/linear/event-worker.js.map +1 -1
- package/dist/integrations/platform/linear/integration.d.ts +3 -1
- package/dist/integrations/platform/linear/integration.d.ts.map +1 -1
- package/dist/integrations/platform/linear/integration.js +9 -9
- package/dist/integrations/platform/linear/integration.js.map +1 -1
- package/dist/integrations/platform/logging.d.ts +6 -0
- package/dist/integrations/platform/logging.d.ts.map +1 -0
- package/dist/integrations/platform/logging.js +25 -0
- package/dist/integrations/platform/logging.js.map +1 -0
- package/dist/integrations/platform/platform-connect.d.ts +38 -0
- package/dist/integrations/platform/platform-connect.d.ts.map +1 -0
- package/dist/integrations/platform/platform-connect.js +109 -0
- package/dist/integrations/platform/platform-connect.js.map +1 -0
- package/dist/integrations/platform/vanta/integration.d.ts +12 -18
- package/dist/integrations/platform/vanta/integration.d.ts.map +1 -1
- package/dist/integrations/platform/vanta/integration.js +38 -38
- package/dist/integrations/platform/vanta/integration.js.map +1 -1
- package/dist/memory-embedder.d.ts +11 -0
- package/dist/memory-embedder.d.ts.map +1 -0
- package/dist/memory-embedder.js +19 -0
- package/dist/memory-embedder.js.map +1 -0
- package/dist/metrics/config.d.ts +1 -1
- package/dist/metrics/config.d.ts.map +1 -1
- package/dist/metrics/config.js +1 -0
- package/dist/metrics/config.js.map +1 -1
- package/dist/metrics/index.js +2 -2
- package/dist/metrics/index.js.map +1 -1
- package/dist/metrics/sandbox.d.ts +3 -3
- package/dist/metrics/sandbox.js +13 -13
- package/dist/metrics/sandbox.js.map +1 -1
- package/dist/metrics/session.d.ts +28 -0
- package/dist/metrics/session.d.ts.map +1 -0
- package/dist/metrics/session.js +76 -0
- package/dist/metrics/session.js.map +1 -0
- package/dist/routes/source-control-sessions.d.ts +5 -0
- package/dist/routes/source-control-sessions.d.ts.map +1 -1
- package/dist/routes/source-control-sessions.js +65 -7
- package/dist/routes/source-control-sessions.js.map +1 -1
- package/dist/routes/surface.d.ts +5 -0
- package/dist/routes/surface.d.ts.map +1 -1
- package/dist/routes/surface.js +1 -0
- package/dist/routes/surface.js.map +1 -1
- package/dist/rules/dispatcher.d.ts.map +1 -1
- package/dist/rules/dispatcher.js.map +1 -1
- package/dist/rules/processor.d.ts +29 -1
- package/dist/rules/processor.d.ts.map +1 -1
- package/dist/rules/processor.js +156 -23
- package/dist/rules/processor.js.map +1 -1
- package/dist/rules/public-failure-reason.js +1 -1
- package/dist/rules/public-failure-reason.js.map +1 -1
- package/dist/rules/transition-service.d.ts.map +1 -1
- package/dist/rules/transition-service.js +33 -9
- package/dist/rules/transition-service.js.map +1 -1
- package/dist/storage/domains/work-items/base.d.ts +2 -0
- package/dist/storage/domains/work-items/base.d.ts.map +1 -1
- package/dist/storage/domains/work-items/base.js +4 -2
- package/dist/storage/domains/work-items/base.js.map +1 -1
- package/dist/work-item-branch.d.ts +7 -1
- package/dist/work-item-branch.d.ts.map +1 -1
- package/dist/work-item-branch.js +9 -1
- package/dist/work-item-branch.js.map +1 -1
- package/dist/work-items/card-facts.d.ts.map +1 -1
- package/dist/work-items/card-facts.js +2 -2
- package/dist/work-items/card-facts.js.map +1 -1
- package/package.json +9 -9
- package/dist/integrations/platform/api-client.d.ts +0 -34
- package/dist/integrations/platform/api-client.d.ts.map +0 -1
- package/dist/integrations/platform/api-client.js +0 -186
- package/dist/integrations/platform/api-client.js.map +0 -1
- package/dist/integrations/platform/gitlab/event-worker.d.ts +0 -64
- package/dist/integrations/platform/gitlab/event-worker.d.ts.map +0 -1
- package/dist/integrations/platform/gitlab/event-worker.js +0 -304
- package/dist/integrations/platform/gitlab/event-worker.js.map +0 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"integration.d.ts","sourceRoot":"","sources":["../../../../src/integrations/platform/vanta/integration.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,qBAAqB,CAAC;
|
|
1
|
+
{"version":3,"file":"integration.d.ts","sourceRoot":"","sources":["../../../../src/integrations/platform/vanta/integration.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,qBAAqB,CAAC;AAGpD,OAAO,KAAK,EAAE,MAAM,EAAgB,MAAM,iCAAiC,CAAC;AAE5E,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,eAAe,CAAC;AACxD,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,2BAA2B,CAAC;AAQjE,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,qBAAqB,CAAC;AAC9D,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,kCAAkC,CAAC;AAC3E,OAAO,EAAE,0BAA0B,EAAE,MAAM,8BAA8B,CAAC;AAE1E,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,wBAAwB,CAAC;AAEpE,MAAM,WAAW,8BAA8B;IAC7C,yFAAyF;IACzF,OAAO,CAAC,EAAE,qBAAqB,CAAC;IAChC,cAAc,CAAC,EAAE,MAAM,CAAC;CACzB;AAcD;;;;GAIG;AACH,qBAAa,wBAAyB,SAAQ,0BAA0B;;IACtE,QAAQ,CAAC,MAAM,EAAE,MAAM,CAOrB;IAEF,YAAY,MAAM,GAAE,8BAAmC,EAUtD;IAOD,qFAAqF;IAC/E,mBAAmB,CAAC,WAAW,CAAC,EAAE,kBAAkB,EAAE,GAAG,OAAO,CAAC,kBAAkB,EAAE,CAAC,CA2B3F;IAEK,yBAAyB,CAC7B,KAAK,EAAE,MAAM,EACb,eAAe,EAAE,aAAa,CAAC;QAAE,EAAE,EAAE,MAAM,CAAC;QAAC,KAAK,EAAE,MAAM,CAAA;KAAE,CAAC,GAC5D,OAAO,CAAC,kBAAkB,EAAE,GAAG,IAAI,CAAC,CA+BtC;IAED,UAAmB,SAAS,CAAC,GAAG,EAAE,kBAAkB,GAAG,eAAe,GAAG,IAAI,CAe5E;IAED,MAAM,CAAC,GAAG,EAAE,kBAAkB,GAAG,QAAQ,EAAE,CAe1C;CACF"}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { loose } from "../../../routes/route.js";
|
|
2
|
-
import { IssueReconcileWorker } from "../../issue-reconcile-worker.js";
|
|
3
2
|
import { attachVantaRules } from "../../vanta/rules.js";
|
|
4
|
-
import { VANTA_VULNERABILITIES_SOURCE_ID, createVantaSync, vantaVulnerabilitiesSelected } from "../../vanta/sync.js";
|
|
5
|
-
import {
|
|
3
|
+
import { DEFAULT_VANTA_SYNC_INTERVAL_MS, VANTA_VULNERABILITIES_SOURCE_ID, createVantaSync, vantaVulnerabilitiesSelected } from "../../vanta/sync.js";
|
|
4
|
+
import { PlatformConnectIntegration } from "../foundation/integration.js";
|
|
5
|
+
import { logPlatformWarn } from "../logging.js";
|
|
6
6
|
import { registerApiRoute } from "@mastra/core/server";
|
|
7
7
|
// src/integrations/platform/vanta/integration.ts
|
|
8
8
|
/** Provider key registered for Vanta in Mastra Platform's integrations catalog. */
|
|
@@ -11,12 +11,10 @@ const PAGE_SIZE = 100;
|
|
|
11
11
|
const MAX_PAGES = 50;
|
|
12
12
|
/**
|
|
13
13
|
* Files Vanta vulnerabilities as `security`-labeled cards through Platform-managed (MastraConnect)
|
|
14
|
-
* Vanta connections. Vanta has no webhooks Factory can consume, so
|
|
14
|
+
* Vanta connections. Vanta has no webhooks Factory can consume, so its reconcile syncs the active
|
|
15
|
+
* list, every 15 minutes by default.
|
|
15
16
|
*/
|
|
16
|
-
var PlatformVantaIntegration = class {
|
|
17
|
-
id = "vanta";
|
|
18
|
-
#client;
|
|
19
|
-
#syncIntervalMs;
|
|
17
|
+
var PlatformVantaIntegration = class extends PlatformConnectIntegration {
|
|
20
18
|
intake = {
|
|
21
19
|
listSources: () => this.#listSources(),
|
|
22
20
|
listItems: async () => ({
|
|
@@ -32,17 +30,21 @@ var PlatformVantaIntegration = class {
|
|
|
32
30
|
updateIssue: async () => null
|
|
33
31
|
};
|
|
34
32
|
constructor(config = {}) {
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
33
|
+
super({
|
|
34
|
+
id: "vanta",
|
|
35
|
+
providerKeys: [PLATFORM_VANTA_PROVIDER_KEY],
|
|
36
|
+
...config.connect ? { connect: config.connect } : {},
|
|
37
|
+
linkOrgConnectionsAtBoot: true,
|
|
38
|
+
connectProviders: [{
|
|
39
|
+
provider: "vanta",
|
|
40
|
+
integrationId: PLATFORM_VANTA_PROVIDER_KEY
|
|
41
|
+
}],
|
|
42
|
+
...config.syncIntervalMs === void 0 ? {} : { reconcileIntervalMs: config.syncIntervalMs },
|
|
43
|
+
reconcileDefaultIntervalMs: DEFAULT_VANTA_SYNC_INTERVAL_MS
|
|
44
|
+
});
|
|
43
45
|
}
|
|
44
46
|
async #listSources() {
|
|
45
|
-
if ((await this
|
|
47
|
+
if ((await this.activeConnections()).length === 0) return [];
|
|
46
48
|
return [{
|
|
47
49
|
id: VANTA_VULNERABILITIES_SOURCE_ID,
|
|
48
50
|
name: "Vulnerabilities",
|
|
@@ -60,12 +62,15 @@ var PlatformVantaIntegration = class {
|
|
|
60
62
|
let cursor = null;
|
|
61
63
|
for (let page = 0;; page++) {
|
|
62
64
|
if (page === MAX_PAGES) throw new Error(`Vanta connection ${connection.id} has more than ${MAX_PAGES} pages of vulnerabilities.`);
|
|
63
|
-
const
|
|
64
|
-
|
|
65
|
-
|
|
65
|
+
const result = await this.proxy(connection.id, {
|
|
66
|
+
method: "GET",
|
|
67
|
+
path: "v1/vulnerabilities",
|
|
68
|
+
query: {
|
|
69
|
+
isDeactivated: "false",
|
|
70
|
+
pageSize: PAGE_SIZE,
|
|
71
|
+
...cursor ? { pageCursor: cursor } : {}
|
|
72
|
+
}
|
|
66
73
|
});
|
|
67
|
-
if (cursor) query.set("pageCursor", cursor);
|
|
68
|
-
const result = await this.#client.request("GET", `/v2/connections/${encodeURIComponent(connection.id)}/proxy/v1/vulnerabilities?${query}`);
|
|
69
74
|
for (const vulnerability of result.results.data) byId.set(vulnerability.id, vulnerability);
|
|
70
75
|
if (!result.results.pageInfo.hasNextPage) break;
|
|
71
76
|
if (!result.results.pageInfo.endCursor || page === 49) throw new Error(`Incomplete Vanta vulnerability snapshot for connection ${connection.id}`);
|
|
@@ -75,20 +80,26 @@ var PlatformVantaIntegration = class {
|
|
|
75
80
|
return [...byId.values()];
|
|
76
81
|
}
|
|
77
82
|
async listVulnerabilitiesForOrg(orgId, factoryProjects) {
|
|
78
|
-
const connections = await this.listConnections();
|
|
83
|
+
const connections = await this.listConnections({ fresh: true });
|
|
79
84
|
const ownerByProject = new Map(factoryProjects.map((project) => [project.id, project.orgId]));
|
|
80
85
|
if (connections.some((connection) => connection.resourceId && !ownerByProject.has(connection.resourceId))) return null;
|
|
81
86
|
if (new Set(factoryProjects.map((project) => project.orgId)).size > 1 && connections.some((connection) => !connection.resourceId)) return null;
|
|
82
87
|
const owned = connections.filter((connection) => connection.resourceId ? ownerByProject.get(connection.resourceId) === orgId : true);
|
|
83
88
|
if (owned.length === 0 || owned.some((connection) => connection.status === "needs_reauth")) return null;
|
|
89
|
+
const unlinked = await this.binding.unlinkedOrgConnections(connections);
|
|
90
|
+
if (unlinked.length > 0) {
|
|
91
|
+
logPlatformWarn("Skipping the Vanta sync until every Vanta connection is linked to the Factory project", { connectionIds: unlinked });
|
|
92
|
+
await this.binding.linkOrgConnections().catch((error) => logPlatformWarn("Could not link Vanta connections to the Factory project", { error: error instanceof Error ? error.message : String(error) }));
|
|
93
|
+
return null;
|
|
94
|
+
}
|
|
84
95
|
return this.listVulnerabilities(owned.filter((connection) => connection.status === "active"));
|
|
85
96
|
}
|
|
86
|
-
|
|
97
|
+
reconcile(ctx) {
|
|
87
98
|
const ingest = attachVantaRules(ctx);
|
|
88
|
-
if (!ingest) return
|
|
99
|
+
if (!ingest) return null;
|
|
89
100
|
const { projects, intake } = ctx.storage;
|
|
90
|
-
|
|
91
|
-
connected: async () => (await this
|
|
101
|
+
return createVantaSync({
|
|
102
|
+
connected: async () => (await this.activeConnections()).length > 0,
|
|
92
103
|
listVulnerabilities: async (orgId) => this.listVulnerabilitiesForOrg(orgId, await projects.listAll()),
|
|
93
104
|
listProjects: () => projects.listAll(),
|
|
94
105
|
intakeEnabled: async (orgId) => vantaVulnerabilitiesSelected((await intake.getConfig({
|
|
@@ -102,11 +113,6 @@ var PlatformVantaIntegration = class {
|
|
|
102
113
|
ingest,
|
|
103
114
|
userId: "factory-rule-dispatcher"
|
|
104
115
|
});
|
|
105
|
-
return [new IssueReconcileWorker({
|
|
106
|
-
integrationId: this.id,
|
|
107
|
-
reconcile: sync,
|
|
108
|
-
intervalMs: this.#syncIntervalMs
|
|
109
|
-
})];
|
|
110
116
|
}
|
|
111
117
|
routes(ctx) {
|
|
112
118
|
return [registerApiRoute("/web/vanta/status", {
|
|
@@ -137,12 +143,6 @@ var PlatformVantaIntegration = class {
|
|
|
137
143
|
}
|
|
138
144
|
})];
|
|
139
145
|
}
|
|
140
|
-
diagnostics() {
|
|
141
|
-
return {
|
|
142
|
-
configured: true,
|
|
143
|
-
mode: "platform"
|
|
144
|
-
};
|
|
145
|
-
}
|
|
146
146
|
};
|
|
147
147
|
export { PlatformVantaIntegration };
|
|
148
148
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"integration.js","names":[],"sources":["../../../../src/integrations/platform/vanta/integration.ts"],"sourcesContent":["import type { ApiRoute } from '@mastra/core/server';\nimport { registerApiRoute } from '@mastra/core/server';\nimport type { MastraWorker } from '@mastra/core/worker';\n\nimport type { Intake, IntakeSource } from '../../../capabilities/intake.js';\nimport { loose } from '../../../routes/route.js';\nimport type { FactoryIntegration, IntegrationContext } from '../../base.js';\nimport { IssueReconcileWorker } from '../../issue-reconcile-worker.js';\nimport { attachVantaRules } from '../../vanta/rules.js';\nimport {\n createVantaSync,\n DEFAULT_VANTA_SYNC_INTERVAL_MS,\n VANTA_VULNERABILITIES_SOURCE_ID,\n vantaVulnerabilitiesSelected,\n} from '../../vanta/sync.js';\nimport type { VantaVulnerability } from '../../vanta/sync.js';\nimport { PlatformApiClient, platformApiClientConfigFromEnv } from '../api-client.js';\nimport type { PlatformApiClientConfig } from '../api-client.js';\n\nexport interface PlatformVantaIntegrationConfig {\n clientConfig?: PlatformApiClientConfig;\n syncIntervalMs?: number;\n}\n\n/** Provider key registered for Vanta in Mastra Platform's integrations catalog. */\nconst PLATFORM_VANTA_PROVIDER_KEY = 'vanta';\nconst PAGE_SIZE = 100;\nconst MAX_PAGES = 50;\n\ninterface PlatformIntegrationConnection {\n id: string;\n status: 'active' | 'needs_reauth';\n accountLabel: string | null;\n resourceId: string | null;\n}\n\ninterface VantaVulnerabilityPage {\n results: {\n data: VantaVulnerability[];\n pageInfo: { endCursor: string | null; hasNextPage: boolean };\n };\n}\n\n/**\n * Files Vanta vulnerabilities as `security`-labeled cards through Platform-managed (MastraConnect)\n * Vanta connections. Vanta has no webhooks Factory can consume, so a worker polls the active list.\n */\nexport class PlatformVantaIntegration implements FactoryIntegration {\n readonly id = 'vanta';\n readonly #client: PlatformApiClient;\n readonly #syncIntervalMs: number;\n\n readonly intake: Intake = {\n listSources: () => this.#listSources(),\n listItems: async () => ({ items: [], nextCursor: null }),\n listIssues: async () => ({ issues: [], nextCursor: null }),\n getIssue: async () => null,\n createComment: async () => null,\n updateIssue: async () => null,\n };\n\n constructor(config: PlatformVantaIntegrationConfig = {}) {\n this.#client = new PlatformApiClient(config.clientConfig ?? platformApiClientConfigFromEnv());\n this.#syncIntervalMs = config.syncIntervalMs ?? DEFAULT_VANTA_SYNC_INTERVAL_MS;\n }\n\n async listConnections(): Promise<PlatformIntegrationConnection[]> {\n const result = await this.#client.request<{ connections: PlatformIntegrationConnection[] }>(\n 'GET',\n `/v2/connections?providerKey=${PLATFORM_VANTA_PROVIDER_KEY}`,\n );\n return result.connections;\n }\n\n async #activeConnections(): Promise<PlatformIntegrationConnection[]> {\n const connections = await this.listConnections();\n return connections.filter(connection => connection.status === 'active').sort((a, b) => a.id.localeCompare(b.id));\n }\n\n async #listSources(): Promise<IntakeSource[]> {\n if ((await this.#activeConnections()).length === 0) return [];\n return [{ id: VANTA_VULNERABILITIES_SOURCE_ID, name: 'Vulnerabilities', type: 'vulnerabilities' }];\n }\n\n /** Every active vulnerability from the supplied connections, de-duplicated by id. */\n async listVulnerabilities(connections?: PlatformIntegrationConnection[]): Promise<VantaVulnerability[]> {\n const selected = connections ?? (await this.listConnections()).sort((a, b) => a.id.localeCompare(b.id));\n if (selected.length === 0) throw new Error('There is no active Vanta connection.');\n const unreadable = selected.find(connection => connection.status !== 'active');\n if (unreadable) throw new Error(`Vanta connection ${unreadable.id} is ${unreadable.status}.`);\n const byId = new Map<string, VantaVulnerability>();\n for (const connection of selected) {\n let cursor: string | null = null;\n for (let page = 0; ; page++) {\n if (page === MAX_PAGES) {\n // A cut-off list would read as the rest being resolved.\n throw new Error(`Vanta connection ${connection.id} has more than ${MAX_PAGES} pages of vulnerabilities.`);\n }\n const query = new URLSearchParams({ isDeactivated: 'false', pageSize: String(PAGE_SIZE) });\n if (cursor) query.set('pageCursor', cursor);\n const result: VantaVulnerabilityPage = await this.#client.request<VantaVulnerabilityPage>(\n 'GET',\n `/v2/connections/${encodeURIComponent(connection.id)}/proxy/v1/vulnerabilities?${query}`,\n );\n for (const vulnerability of result.results.data) byId.set(vulnerability.id, vulnerability);\n if (!result.results.pageInfo.hasNextPage) break;\n if (!result.results.pageInfo.endCursor || page === MAX_PAGES - 1) {\n throw new Error(`Incomplete Vanta vulnerability snapshot for connection ${connection.id}`);\n }\n cursor = result.results.pageInfo.endCursor;\n }\n }\n return [...byId.values()];\n }\n\n async listVulnerabilitiesForOrg(orgId: string, factoryProjects: ReadonlyArray<{ id: string; orgId: string }>): Promise<VantaVulnerability[] | null> {\n const connections = await this.listConnections();\n const ownerByProject = new Map(factoryProjects.map(project => [project.id, project.orgId]));\n // A global or unrecognized connection cannot be attributed safely across organizations.\n if (connections.some(connection => connection.resourceId && !ownerByProject.has(connection.resourceId))) return null;\n if (new Set(factoryProjects.map(project => project.orgId)).size > 1 &&\n connections.some(connection => !connection.resourceId)) return null;\n const owned = connections.filter(connection =>\n connection.resourceId ? ownerByProject.get(connection.resourceId) === orgId : true,\n );\n if (owned.length === 0 || owned.some(connection => connection.status === 'needs_reauth')) return null;\n return this.listVulnerabilities(owned.filter(connection => connection.status === 'active'));\n }\n\n workers(ctx: IntegrationContext): MastraWorker[] {\n const ingest = attachVantaRules(ctx);\n if (!ingest) return [];\n const { projects, intake } = ctx.storage;\n const sync = createVantaSync({\n connected: async () => (await this.#activeConnections()).length > 0,\n listVulnerabilities: async orgId => this.listVulnerabilitiesForOrg(orgId, await projects.listAll()),\n listProjects: () => projects.listAll(),\n intakeEnabled: async orgId =>\n vantaVulnerabilitiesSelected((await intake.getConfig({ orgId, integrationIds: [this.id] }))[this.id]),\n boundProjectIds: async orgId =>\n (await intake.listBindings({ orgId, integrationId: this.id })).map(binding => binding.factoryProjectId),\n ingest,\n userId: 'factory-rule-dispatcher',\n });\n return [new IssueReconcileWorker({ integrationId: this.id, reconcile: sync, intervalMs: this.#syncIntervalMs })];\n }\n\n routes(ctx: IntegrationContext): ApiRoute[] {\n return [\n registerApiRoute('/web/vanta/status', {\n method: 'GET',\n requiresAuth: false,\n handler: async c => {\n if (!ctx.auth.enabled()) return c.json({ enabled: false, configured: true, reason: 'missing_config' });\n await ctx.auth.ensureUser(loose(c));\n const tenant = ctx.auth.tenant(loose(c));\n if (!tenant) return c.json({ error: 'unauthorized', reason: 'auth_required' }, 401);\n if (!tenant.orgId) return c.json({ enabled: true, configured: true, reason: 'organization_required' });\n return c.json({ enabled: true, configured: true, reason: 'ready' });\n },\n }),\n ];\n }\n\n diagnostics(): Record<string, unknown> {\n return { configured: true, mode: 'platform' };\n }\n}\n"],"mappings":";;;;;;;;AAyBA,MAAM,8BAA8B;AACpC,MAAM,YAAY;AAClB,MAAM,YAAY;;;;;AAoBlB,IAAa,2BAAb,MAAoE;CAClE,KAAc;CACd;CACA;CAEA,SAA0B;EACxB,mBAAmB,KAAK,aAAa;EACrC,WAAW,aAAa;GAAE,OAAO,CAAC;GAAG,YAAY;EAAK;EACtD,YAAY,aAAa;GAAE,QAAQ,CAAC;GAAG,YAAY;EAAK;EACxD,UAAU,YAAY;EACtB,eAAe,YAAY;EAC3B,aAAa,YAAY;CAC3B;CAEA,YAAY,SAAyC,CAAC,GAAG;EACvD,KAAK,UAAU,IAAI,kBAAkB,OAAO,gBAAgB,+BAA+B,CAAC;EAC5F,KAAK,kBAAkB,OAAO,kBAAA;CAChC;CAEA,MAAM,kBAA4D;EAKhE,QAAO,MAJc,KAAK,QAAQ,QAChC,OACA,+BAA+B,6BACjC,EAAA,CACc;CAChB;CAEA,MAAM,qBAA+D;EAEnE,QAAO,MADmB,KAAK,gBAAgB,EAAA,CAC5B,QAAO,eAAc,WAAW,WAAW,QAAQ,CAAC,CAAC,MAAM,GAAG,MAAM,EAAE,GAAG,cAAc,EAAE,EAAE,CAAC;CACjH;CAEA,MAAM,eAAwC;EAC5C,KAAK,MAAM,KAAK,mBAAmB,EAAA,CAAG,WAAW,GAAG,OAAO,CAAC;EAC5D,OAAO,CAAC;GAAE,IAAI;GAAiC,MAAM;GAAmB,MAAM;EAAkB,CAAC;CACnG;;CAGA,MAAM,oBAAoB,aAA8E;EACtG,MAAM,WAAW,gBAAgB,MAAM,KAAK,gBAAgB,EAAA,CAAG,MAAM,GAAG,MAAM,EAAE,GAAG,cAAc,EAAE,EAAE,CAAC;EACtG,IAAI,SAAS,WAAW,GAAG,MAAM,IAAI,MAAM,sCAAsC;EAC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AAC,CAAC;IAClC,MAAM,SAAS,IAAI,KAAK,OAAO,MAAM,CAAC,CAAC;IACvC,IAAI,CAAC,QAAQ,OAAO,EAAE,KAAK;KAAE,OAAO;KAAgB,QAAQ;IAAgB,GAAG,GAAG;IAClF,IAAI,CAAC,OAAO,OAAO,OAAO,EAAE,KAAK;KAAE,SAAS;KAAM,YAAY;KAAM,QAAQ;IAAwB,CAAC;IACrG,OAAO,EAAE,KAAK;KAAE,SAAS;KAAM,YAAY;KAAM,QAAQ;IAAQ,CAAC;GACpE;EACF,CAAC,CACH;CACF;CAEA,cAAuC;EACrC,OAAO;GAAE,YAAY;GAAM,MAAM;EAAW;CAC9C;AACF"}
|
|
1
|
+
{"version":3,"file":"integration.js","names":[],"sources":["../../../../src/integrations/platform/vanta/integration.ts"],"sourcesContent":["import type { ApiRoute } from '@mastra/core/server';\nimport { registerApiRoute } from '@mastra/core/server';\n\nimport type { Intake, IntakeSource } from '../../../capabilities/intake.js';\nimport { loose } from '../../../routes/route.js';\nimport type { IntegrationContext } from '../../base.js';\nimport type { IssueReconciler } from '../../issue-reconciler.js';\nimport { attachVantaRules } from '../../vanta/rules.js';\nimport {\n createVantaSync,\n DEFAULT_VANTA_SYNC_INTERVAL_MS,\n VANTA_VULNERABILITIES_SOURCE_ID,\n vantaVulnerabilitiesSelected,\n} from '../../vanta/sync.js';\nimport type { VantaVulnerability } from '../../vanta/sync.js';\nimport type { PlatformConnection } from '../foundation/connect-binding.js';\nimport { PlatformConnectIntegration } from '../foundation/integration.js';\nimport { logPlatformWarn } from '../logging.js';\nimport type { PlatformConnectOption } from '../platform-connect.js';\n\nexport interface PlatformVantaIntegrationConfig {\n /** The Connect client, or options to build one; defaults to the platform environment. */\n connect?: PlatformConnectOption;\n syncIntervalMs?: number;\n}\n\n/** Provider key registered for Vanta in Mastra Platform's integrations catalog. */\nconst PLATFORM_VANTA_PROVIDER_KEY = 'vanta';\nconst PAGE_SIZE = 100;\nconst MAX_PAGES = 50;\n\ninterface VantaVulnerabilityPage {\n results: {\n data: VantaVulnerability[];\n pageInfo: { endCursor: string | null; hasNextPage: boolean };\n };\n}\n\n/**\n * Files Vanta vulnerabilities as `security`-labeled cards through Platform-managed (MastraConnect)\n * Vanta connections. Vanta has no webhooks Factory can consume, so its reconcile syncs the active\n * list, every 15 minutes by default.\n */\nexport class PlatformVantaIntegration extends PlatformConnectIntegration {\n readonly intake: Intake = {\n listSources: () => this.#listSources(),\n listItems: async () => ({ items: [], nextCursor: null }),\n listIssues: async () => ({ issues: [], nextCursor: null }),\n getIssue: async () => null,\n createComment: async () => null,\n updateIssue: async () => null,\n };\n\n constructor(config: PlatformVantaIntegrationConfig = {}) {\n super({\n id: 'vanta',\n providerKeys: [PLATFORM_VANTA_PROVIDER_KEY],\n ...(config.connect ? { connect: config.connect } : {}),\n linkOrgConnectionsAtBoot: true,\n connectProviders: [{ provider: 'vanta', integrationId: PLATFORM_VANTA_PROVIDER_KEY }],\n ...(config.syncIntervalMs === undefined ? {} : { reconcileIntervalMs: config.syncIntervalMs }),\n reconcileDefaultIntervalMs: DEFAULT_VANTA_SYNC_INTERVAL_MS,\n });\n }\n\n async #listSources(): Promise<IntakeSource[]> {\n if ((await this.activeConnections()).length === 0) return [];\n return [{ id: VANTA_VULNERABILITIES_SOURCE_ID, name: 'Vulnerabilities', type: 'vulnerabilities' }];\n }\n\n /** Every active vulnerability from the supplied connections, de-duplicated by id. */\n async listVulnerabilities(connections?: PlatformConnection[]): Promise<VantaVulnerability[]> {\n const selected = connections ?? (await this.listConnections()).sort((a, b) => a.id.localeCompare(b.id));\n if (selected.length === 0) throw new Error('There is no active Vanta connection.');\n const unreadable = selected.find(connection => connection.status !== 'active');\n if (unreadable) throw new Error(`Vanta connection ${unreadable.id} is ${unreadable.status}.`);\n const byId = new Map<string, VantaVulnerability>();\n for (const connection of selected) {\n let cursor: string | null = null;\n for (let page = 0; ; page++) {\n if (page === MAX_PAGES) {\n // A cut-off list would read as the rest being resolved.\n throw new Error(`Vanta connection ${connection.id} has more than ${MAX_PAGES} pages of vulnerabilities.`);\n }\n const result = (await this.proxy(connection.id, {\n method: 'GET',\n path: 'v1/vulnerabilities',\n query: { isDeactivated: 'false', pageSize: PAGE_SIZE, ...(cursor ? { pageCursor: cursor } : {}) },\n })) as VantaVulnerabilityPage;\n for (const vulnerability of result.results.data) byId.set(vulnerability.id, vulnerability);\n if (!result.results.pageInfo.hasNextPage) break;\n if (!result.results.pageInfo.endCursor || page === MAX_PAGES - 1) {\n throw new Error(`Incomplete Vanta vulnerability snapshot for connection ${connection.id}`);\n }\n cursor = result.results.pageInfo.endCursor;\n }\n }\n return [...byId.values()];\n }\n\n async listVulnerabilitiesForOrg(\n orgId: string,\n factoryProjects: ReadonlyArray<{ id: string; orgId: string }>,\n ): Promise<VantaVulnerability[] | null> {\n // Fresh, so the unlinked check below compares against exactly what the sync reads.\n const connections = await this.listConnections({ fresh: true });\n const ownerByProject = new Map(factoryProjects.map(project => [project.id, project.orgId]));\n // A global or unrecognized connection cannot be attributed safely across organizations.\n if (connections.some(connection => connection.resourceId && !ownerByProject.has(connection.resourceId)))\n return null;\n if (\n new Set(factoryProjects.map(project => project.orgId)).size > 1 &&\n connections.some(connection => !connection.resourceId)\n )\n return null;\n const owned = connections.filter(connection =>\n connection.resourceId ? ownerByProject.get(connection.resourceId) === orgId : true,\n );\n if (owned.length === 0 || owned.some(connection => connection.status === 'needs_reauth')) return null;\n // A Vanta connection the project can't see would read as all its vulnerabilities resolved.\n const unlinked = await this.binding.unlinkedOrgConnections(connections);\n if (unlinked.length > 0) {\n logPlatformWarn('Skipping the Vanta sync until every Vanta connection is linked to the Factory project', {\n connectionIds: unlinked,\n });\n // A refusal lands in needsLinking for the UI; a successful link re-runs the sync.\n await this.binding.linkOrgConnections().catch(error =>\n logPlatformWarn('Could not link Vanta connections to the Factory project', {\n error: error instanceof Error ? error.message : String(error),\n }),\n );\n return null;\n }\n return this.listVulnerabilities(owned.filter(connection => connection.status === 'active'));\n }\n\n protected override reconcile(ctx: IntegrationContext): IssueReconciler | null {\n const ingest = attachVantaRules(ctx);\n if (!ingest) return null;\n const { projects, intake } = ctx.storage;\n return createVantaSync({\n connected: async () => (await this.activeConnections()).length > 0,\n listVulnerabilities: async orgId => this.listVulnerabilitiesForOrg(orgId, await projects.listAll()),\n listProjects: () => projects.listAll(),\n intakeEnabled: async orgId =>\n vantaVulnerabilitiesSelected((await intake.getConfig({ orgId, integrationIds: [this.id] }))[this.id]),\n boundProjectIds: async orgId =>\n (await intake.listBindings({ orgId, integrationId: this.id })).map(binding => binding.factoryProjectId),\n ingest,\n userId: 'factory-rule-dispatcher',\n });\n }\n\n routes(ctx: IntegrationContext): ApiRoute[] {\n return [\n registerApiRoute('/web/vanta/status', {\n method: 'GET',\n requiresAuth: false,\n handler: async c => {\n if (!ctx.auth.enabled()) return c.json({ enabled: false, configured: true, reason: 'missing_config' });\n await ctx.auth.ensureUser(loose(c));\n const tenant = ctx.auth.tenant(loose(c));\n if (!tenant) return c.json({ error: 'unauthorized', reason: 'auth_required' }, 401);\n if (!tenant.orgId) return c.json({ enabled: true, configured: true, reason: 'organization_required' });\n return c.json({ enabled: true, configured: true, reason: 'ready' });\n },\n }),\n ];\n }\n}\n"],"mappings":";;;;;;;;AA2BA,MAAM,8BAA8B;AACpC,MAAM,YAAY;AAClB,MAAM,YAAY;;;;;;AAclB,IAAa,2BAAb,cAA8C,2BAA2B;CACvE,SAA0B;EACxB,mBAAmB,KAAK,aAAa;EACrC,WAAW,aAAa;GAAE,OAAO,CAAC;GAAG,YAAY;EAAK;EACtD,YAAY,aAAa;GAAE,QAAQ,CAAC;GAAG,YAAY;EAAK;EACxD,UAAU,YAAY;EACtB,eAAe,YAAY;EAC3B,aAAa,YAAY;CAC3B;CAEA,YAAY,SAAyC,CAAC,GAAG;EACvD,MAAM;GACJ,IAAI;GACJ,cAAc,CAAC,2BAA2B;GAC1C,GAAI,OAAO,UAAU,EAAE,SAAS,OAAO,QAAQ,IAAI,CAAC;G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BAAgB;GACrB,WAAW,aAAa,MAAM,KAAK,kBAAkB,EAAA,CAAG,SAAS;GACjE,qBAAqB,OAAM,UAAS,KAAK,0BAA0B,OAAO,MAAM,SAAS,QAAQ,CAAC;GAClG,oBAAoB,SAAS,QAAQ;GACrC,eAAe,OAAM,UACnB,8BAA8B,MAAM,OAAO,UAAU;IAAE;IAAO,gBAAgB,CAAC,KAAK,EAAE;GAAE,CAAC,EAAA,CAAG,KAAK,GAAG;GACtG,iBAAiB,OAAM,WACpB,MAAM,OAAO,aAAa;IAAE;IAAO,eAAe,KAAK;GAAG,CAAC,EAAA,CAAG,KAAI,YAAW,QAAQ,gBAAgB;GACxG;GACA,QAAQ;EACV,CAAC;CACH;CAEA,OAAO,KAAqC;EAC1C,OAAO,CACL,iBAAiB,qBAAqB;GACpC,QAAQ;GACR,cAAc;GACd,SAAS,OAAM,MAAK;IAClB,IAAI,CAAC,IAAI,KAAK,QAAQ,GAAG,OAAO,EAAE,KAAK;KAAE,SAAS;KAAO,YAAY;KAAM,QAAQ;IAAiB,CAAC;IACrG,MAAM,IAAI,KAAK,WAAW,MAAM,CAAC,CAAC;IAClC,MAAM,SAAS,IAAI,KAAK,OAAO,MAAM,CAAC,CAAC;IACvC,IAAI,CAAC,QAAQ,OAAO,EAAE,KAAK;KAAE,OAAO;KAAgB,QAAQ;IAAgB,GAAG,GAAG;IAClF,IAAI,CAAC,OAAO,OAAO,OAAO,EAAE,KAAK;KAAE,SAAS;KAAM,YAAY;KAAM,QAAQ;IAAwB,CAAC;IACrG,OAAO,EAAE,KAAK;KAAE,SAAS;KAAM,YAAY;KAAM,QAAQ;IAAQ,CAAC;GACpE;EACF,CAAC,CACH;CACF;AACF"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { MemoryEmbedder } from '@mastra/code-sdk/agents/memory';
|
|
2
|
+
import type { MastraEmbeddingModel, MastraVector } from '@mastra/core/vector';
|
|
3
|
+
/**
|
|
4
|
+
* Builds the deployment's recall embedder once, at construction. A router string is turned into a
|
|
5
|
+
* model here so a missing provider key fails the boot instead of the first recall.
|
|
6
|
+
*/
|
|
7
|
+
export declare function resolveFactoryEmbedder(config: {
|
|
8
|
+
vector?: MastraVector;
|
|
9
|
+
embedder?: MemoryEmbedder;
|
|
10
|
+
}): MastraEmbeddingModel<string> | undefined;
|
|
11
|
+
//# sourceMappingURL=memory-embedder.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"memory-embedder.d.ts","sourceRoot":"","sources":["../src/memory-embedder.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,gCAAgC,CAAC;AAErE,OAAO,KAAK,EAAE,oBAAoB,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AAE9E;;;GAGG;AACH,wBAAgB,sBAAsB,CAAC,MAAM,EAAE;IAC7C,MAAM,CAAC,EAAE,YAAY,CAAC;IACtB,QAAQ,CAAC,EAAE,cAAc,CAAC;CAC3B,GAAG,oBAAoB,CAAC,MAAM,CAAC,GAAG,SAAS,CAiB3C"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { ModelRouterEmbeddingModel } from "@mastra/core/llm";
|
|
2
|
+
// src/memory-embedder.ts
|
|
3
|
+
/**
|
|
4
|
+
* Builds the deployment's recall embedder once, at construction. A router string is turned into a
|
|
5
|
+
* model here so a missing provider key fails the boot instead of the first recall.
|
|
6
|
+
*/
|
|
7
|
+
function resolveFactoryEmbedder(config) {
|
|
8
|
+
if (config.embedder === void 0) return void 0;
|
|
9
|
+
if (!config.vector) throw new Error("MastraFactory: 'embedder' needs 'vector'. Recall vectors are stored in the vector store, so pass one (e.g. new PgVector({ connectionString }) from '@mastra/pg').");
|
|
10
|
+
if (typeof config.embedder !== "string") return config.embedder;
|
|
11
|
+
try {
|
|
12
|
+
return new ModelRouterEmbeddingModel(config.embedder);
|
|
13
|
+
} catch (error) {
|
|
14
|
+
throw new Error(`MastraFactory: could not build the embedder '${config.embedder}': ${error instanceof Error ? error.message : String(error)}`, { cause: error });
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
export { resolveFactoryEmbedder };
|
|
18
|
+
|
|
19
|
+
//# sourceMappingURL=memory-embedder.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"memory-embedder.js","names":[],"sources":["../src/memory-embedder.ts"],"sourcesContent":["import type { MemoryEmbedder } from '@mastra/code-sdk/agents/memory';\nimport { ModelRouterEmbeddingModel } from '@mastra/core/llm';\nimport type { MastraEmbeddingModel, MastraVector } from '@mastra/core/vector';\n\n/**\n * Builds the deployment's recall embedder once, at construction. A router string is turned into a\n * model here so a missing provider key fails the boot instead of the first recall.\n */\nexport function resolveFactoryEmbedder(config: {\n vector?: MastraVector;\n embedder?: MemoryEmbedder;\n}): MastraEmbeddingModel<string> | undefined {\n if (config.embedder === undefined) return undefined;\n if (!config.vector) {\n throw new Error(\n \"MastraFactory: 'embedder' needs 'vector'. Recall vectors are stored in the vector store, so pass one \" +\n \"(e.g. new PgVector({ connectionString }) from '@mastra/pg').\",\n );\n }\n if (typeof config.embedder !== 'string') return config.embedder;\n try {\n return new ModelRouterEmbeddingModel(config.embedder);\n } catch (error) {\n throw new Error(\n `MastraFactory: could not build the embedder '${config.embedder}': ${error instanceof Error ? error.message : String(error)}`,\n { cause: error },\n );\n }\n}\n"],"mappings":";;;;;;AAQA,SAAgB,uBAAuB,QAGM;CAC3C,IAAI,OAAO,aAAa,KAAA,GAAW,OAAO,KAAA;CAC1C,IAAI,CAAC,OAAO,QACV,MAAM,IAAI,MACR,mKAEF;CAEF,IAAI,OAAO,OAAO,aAAa,UAAU,OAAO,OAAO;CACvD,IAAI;EACF,OAAO,IAAI,0BAA0B,OAAO,QAAQ;CACtD,SAAS,OAAO;EACd,MAAM,IAAI,MACR,gDAAgD,OAAO,SAAS,KAAK,iBAAiB,QAAQ,MAAM,UAAU,OAAO,KAAK,KAC1H,EAAE,OAAO,MAAM,CACjB;CACF;AACF"}
|
package/dist/metrics/config.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/** Metric groups a deployment can turn on, one per component to investigate. */
|
|
2
|
-
export declare const METRIC_GROUPS: readonly ['process', 'postgres', 'redis', 'sandbox', 'intake'];
|
|
2
|
+
export declare const METRIC_GROUPS: readonly ['process', 'postgres', 'redis', 'sandbox', 'session', 'intake'];
|
|
3
3
|
export type MetricGroup = (typeof METRIC_GROUPS)[number];
|
|
4
4
|
export interface FactoryMetricsOptions {
|
|
5
5
|
/** Groups to collect, or `'all'`. An empty list turns metrics off. */
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../../src/metrics/config.ts"],"names":[],"mappings":"AAAA,gFAAgF;AAChF,eAAO,MAAM,aAAa,YAAI,SAAS,EAAE,UAAU,EAAE,OAAO,EAAE,SAAS,EAAE,QAAQ,CAAU,CAAC;
|
|
1
|
+
{"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../../src/metrics/config.ts"],"names":[],"mappings":"AAAA,gFAAgF;AAChF,eAAO,MAAM,aAAa,YAAI,SAAS,EAAE,UAAU,EAAE,OAAO,EAAE,SAAS,EAAE,SAAS,EAAE,QAAQ,CAAU,CAAC;AACvG,MAAM,MAAM,WAAW,GAAG,CAAC,OAAO,aAAa,CAAC,CAAC,MAAM,CAAC,CAAC;AAEzD,MAAM,WAAW,qBAAqB;IACpC,sEAAsE;IACtE,MAAM,EAAE,SAAS,WAAW,EAAE,GAAG,KAAK,CAAC;IACvC,8FAA8F;IAC9F,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,+FAA+F;IAC/F,mBAAmB,CAAC,EAAE,MAAM,CAAC;CAC9B;AAED,MAAM,WAAW,qBAAqB;IACpC,MAAM,EAAE,WAAW,CAAC,WAAW,CAAC,CAAC;IACjC,UAAU,EAAE,MAAM,CAAC;IACnB,YAAY,EAAE,MAAM,CAAC;IACrB,uDAAuD;IACvD,aAAa,EAAE,MAAM,EAAE,CAAC;CACzB;AAmBD;;;;;GAKG;AACH,wBAAgB,oBAAoB,CAClC,MAAM,EAAE,qBAAqB,GAAG,SAAS,EACzC,GAAG,GAAE,MAAM,CAAC,UAAwB,GACnC,qBAAqB,GAAG,SAAS,CA2BnC;AAED,MAAM,MAAM,cAAc,GAAG,KAAK,GAAG,QAAQ,GAAG,KAAK,CAAC;AAEtD;;;;GAIG;AACH,wBAAgB,cAAc,CAAC,GAAG,GAAE,MAAM,CAAC,UAAwB,GAAG,cAAc,CAInF"}
|
package/dist/metrics/config.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"config.js","names":[],"sources":["../../src/metrics/config.ts"],"sourcesContent":["/** Metric groups a deployment can turn on, one per component to investigate. */\nexport const METRIC_GROUPS = ['process', 'postgres', 'redis', 'sandbox', 'intake'] as const;\nexport type MetricGroup = (typeof METRIC_GROUPS)[number];\n\nexport interface FactoryMetricsOptions {\n /** Groups to collect, or `'all'`. An empty list turns metrics off. */\n groups: readonly MetricGroup[] | 'all';\n /** How often gauges are sampled and aggregated timings are sent. Default 60s, minimum 15s. */\n intervalMs?: number;\n /** Most records sent per minute per process; the rest are dropped and counted. Default 500. */\n maxRecordsPerMinute?: number;\n}\n\nexport interface ResolvedMetricsConfig {\n groups: ReadonlySet<MetricGroup>;\n intervalMs: number;\n maxPerMinute: number;\n /** Group names that were asked for but don't exist. */\n unknownGroups: string[];\n}\n\nconst DEFAULT_INTERVAL_MS = 60_000;\nconst MIN_INTERVAL_MS = 15_000;\n// Longest delay setInterval accepts; Node turns anything above it into 1 ms.\nconst MAX_INTERVAL_MS = 2_147_483_647;\nconst DEFAULT_MAX_PER_MINUTE = 500;\n\nfunction parseGroups(value: readonly string[] | 'all'): { groups: Set<MetricGroup>; unknownGroups: string[] } {\n if (value === 'all') return { groups: new Set(METRIC_GROUPS), unknownGroups: [] };\n const groups = new Set<MetricGroup>();\n const unknownGroups: string[] = [];\n for (const name of value) {\n if ((METRIC_GROUPS as readonly string[]).includes(name)) groups.add(name as MetricGroup);\n else unknownGroups.push(name);\n }\n return { groups, unknownGroups };\n}\n\n/**\n * Resolves which metric groups are on. Every record counts towards the deployment's observability\n * usage, so nothing is collected unless a group is asked for. Env vars override the code option:\n * `FACTORY_METRICS` (`all`, `0`, or a list like `sandbox,intake`), `FACTORY_METRICS_INTERVAL_MS`\n * and `FACTORY_METRICS_MAX_PER_MIN`. Returns `undefined` when no group is on.\n */\nexport function resolveMetricsConfig(\n option: FactoryMetricsOptions | undefined,\n env: NodeJS.ProcessEnv = process.env,\n): ResolvedMetricsConfig | undefined {\n const envGroups = env.FACTORY_METRICS?.trim();\n let requested: readonly string[] | 'all' | undefined = option?.groups;\n if (envGroups) {\n if (/^(0|false|no|off)$/i.test(envGroups)) return undefined;\n requested = /^(all|1|true|yes)$/i.test(envGroups)\n ? 'all'\n : envGroups\n .split(',')\n .map(name => name.trim())\n .filter(Boolean);\n }\n if (!requested) return undefined;\n const { groups, unknownGroups } = parseGroups(requested);\n if (groups.size === 0 && unknownGroups.length === 0) return undefined;\n\n const interval = Number(env.FACTORY_METRICS_INTERVAL_MS || option?.intervalMs);\n const maxPerMinute = Number(env.FACTORY_METRICS_MAX_PER_MIN || option?.maxRecordsPerMinute);\n return {\n groups,\n intervalMs:\n Number.isFinite(interval) && interval > 0\n ? Math.min(Math.max(interval, MIN_INTERVAL_MS), MAX_INTERVAL_MS)\n : DEFAULT_INTERVAL_MS,\n maxPerMinute: Number.isInteger(maxPerMinute) && maxPerMinute > 0 ? maxPerMinute : DEFAULT_MAX_PER_MINUTE,\n unknownGroups,\n };\n}\n\nexport type MetricsService = 'api' | 'worker' | 'all';\n\n/**\n * Which part of a split deployment this process is. Platform deploys a separate worker service\n * when the app has storage and pubsub: the API container runs with `MASTRA_WORKERS=false`, the\n * worker container with it set (empty, or a list of worker names). Unset means one process does both.\n */\nexport function metricsService(env: NodeJS.ProcessEnv = process.env): MetricsService {\n const workers = env.MASTRA_WORKERS;\n if (workers === undefined) return 'all';\n return workers.trim() === 'false' ? 'api' : 'worker';\n}\n"],"mappings":";;AACA,MAAa,gBAAgB;CAAC;CAAW;CAAY;CAAS;CAAW;AAAQ;
|
|
1
|
+
{"version":3,"file":"config.js","names":[],"sources":["../../src/metrics/config.ts"],"sourcesContent":["/** Metric groups a deployment can turn on, one per component to investigate. */\nexport const METRIC_GROUPS = ['process', 'postgres', 'redis', 'sandbox', 'session', 'intake'] as const;\nexport type MetricGroup = (typeof METRIC_GROUPS)[number];\n\nexport interface FactoryMetricsOptions {\n /** Groups to collect, or `'all'`. An empty list turns metrics off. */\n groups: readonly MetricGroup[] | 'all';\n /** How often gauges are sampled and aggregated timings are sent. Default 60s, minimum 15s. */\n intervalMs?: number;\n /** Most records sent per minute per process; the rest are dropped and counted. Default 500. */\n maxRecordsPerMinute?: number;\n}\n\nexport interface ResolvedMetricsConfig {\n groups: ReadonlySet<MetricGroup>;\n intervalMs: number;\n maxPerMinute: number;\n /** Group names that were asked for but don't exist. */\n unknownGroups: string[];\n}\n\nconst DEFAULT_INTERVAL_MS = 60_000;\nconst MIN_INTERVAL_MS = 15_000;\n// Longest delay setInterval accepts; Node turns anything above it into 1 ms.\nconst MAX_INTERVAL_MS = 2_147_483_647;\nconst DEFAULT_MAX_PER_MINUTE = 500;\n\nfunction parseGroups(value: readonly string[] | 'all'): { groups: Set<MetricGroup>; unknownGroups: string[] } {\n if (value === 'all') return { groups: new Set(METRIC_GROUPS), unknownGroups: [] };\n const groups = new Set<MetricGroup>();\n const unknownGroups: string[] = [];\n for (const name of value) {\n if ((METRIC_GROUPS as readonly string[]).includes(name)) groups.add(name as MetricGroup);\n else unknownGroups.push(name);\n }\n return { groups, unknownGroups };\n}\n\n/**\n * Resolves which metric groups are on. Every record counts towards the deployment's observability\n * usage, so nothing is collected unless a group is asked for. Env vars override the code option:\n * `FACTORY_METRICS` (`all`, `0`, or a list like `sandbox,intake`), `FACTORY_METRICS_INTERVAL_MS`\n * and `FACTORY_METRICS_MAX_PER_MIN`. Returns `undefined` when no group is on.\n */\nexport function resolveMetricsConfig(\n option: FactoryMetricsOptions | undefined,\n env: NodeJS.ProcessEnv = process.env,\n): ResolvedMetricsConfig | undefined {\n const envGroups = env.FACTORY_METRICS?.trim();\n let requested: readonly string[] | 'all' | undefined = option?.groups;\n if (envGroups) {\n if (/^(0|false|no|off)$/i.test(envGroups)) return undefined;\n requested = /^(all|1|true|yes)$/i.test(envGroups)\n ? 'all'\n : envGroups\n .split(',')\n .map(name => name.trim())\n .filter(Boolean);\n }\n if (!requested) return undefined;\n const { groups, unknownGroups } = parseGroups(requested);\n if (groups.size === 0 && unknownGroups.length === 0) return undefined;\n\n const interval = Number(env.FACTORY_METRICS_INTERVAL_MS || option?.intervalMs);\n const maxPerMinute = Number(env.FACTORY_METRICS_MAX_PER_MIN || option?.maxRecordsPerMinute);\n return {\n groups,\n intervalMs:\n Number.isFinite(interval) && interval > 0\n ? Math.min(Math.max(interval, MIN_INTERVAL_MS), MAX_INTERVAL_MS)\n : DEFAULT_INTERVAL_MS,\n maxPerMinute: Number.isInteger(maxPerMinute) && maxPerMinute > 0 ? maxPerMinute : DEFAULT_MAX_PER_MINUTE,\n unknownGroups,\n };\n}\n\nexport type MetricsService = 'api' | 'worker' | 'all';\n\n/**\n * Which part of a split deployment this process is. Platform deploys a separate worker service\n * when the app has storage and pubsub: the API container runs with `MASTRA_WORKERS=false`, the\n * worker container with it set (empty, or a list of worker names). Unset means one process does both.\n */\nexport function metricsService(env: NodeJS.ProcessEnv = process.env): MetricsService {\n const workers = env.MASTRA_WORKERS;\n if (workers === undefined) return 'all';\n return workers.trim() === 'false' ? 'api' : 'worker';\n}\n"],"mappings":";;AACA,MAAa,gBAAgB;CAAC;CAAW;CAAY;CAAS;CAAW;CAAW;AAAQ;AAoB5F,MAAM,sBAAsB;AAC5B,MAAM,kBAAkB;AAExB,MAAM,kBAAkB;AACxB,MAAM,yBAAyB;AAE/B,SAAS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ics/index.js
CHANGED
|
@@ -3,7 +3,7 @@ import { asPool, postgresPoolCollector, postgresStatsCollector } from "./collect
|
|
|
3
3
|
import { processCollector } from "./collectors/process.js";
|
|
4
4
|
import { asMonitorablePubSub, connectMonitor, redisProbeCollector, redisStatsCollector } from "./collectors/redis.js";
|
|
5
5
|
import { intakeCollector, keepIntakeSource } from "./intake.js";
|
|
6
|
-
import {
|
|
6
|
+
import { sandboxStartsCollector } from "./sandbox.js";
|
|
7
7
|
import { METRIC_GROUPS, metricsService, resolveMetricsConfig } from "./config.js";
|
|
8
8
|
import { FactoryMetrics, factoryMetrics, installFactoryMetrics } from "./metrics.js";
|
|
9
9
|
// src/metrics/index.ts
|
|
@@ -48,7 +48,7 @@ function startFactoryMetrics(metrics, targets, sources) {
|
|
|
48
48
|
collectors.push({ collect(sink) {
|
|
49
49
|
sink.emit("mastra_factory_sandboxes_active", countRunningSessionSandboxes());
|
|
50
50
|
} });
|
|
51
|
-
collectors.push(
|
|
51
|
+
collectors.push(sandboxStartsCollector());
|
|
52
52
|
}
|
|
53
53
|
if (metrics.enabled("intake")) {
|
|
54
54
|
for (const id of sources.integrationIds ?? []) if (id === "github" || id === "linear") keepIntakeSource(id);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":[],"sources":["../../src/metrics/index.ts"],"sourcesContent":["import { countRunningSessionSandboxes } from '../sandbox/session-sandbox.js';\nimport { postgresPoolCollector, postgresStatsCollector, asPool } from './collectors/postgres.js';\nimport type { PoolLike } from './collectors/postgres.js';\nimport { processCollector } from './collectors/process.js';\nimport { asMonitorablePubSub, connectMonitor, redisProbeCollector, redisStatsCollector } from './collectors/redis.js';\nimport type { RedisMonitorClient } from './collectors/redis.js';\nimport { intakeCollector, keepIntakeSource } from './intake.js';\nimport type { FactoryMetrics, MetricsCollector, MetricsTargets } from './metrics.js';\nimport {
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"sources":["../../src/metrics/index.ts"],"sourcesContent":["import { countRunningSessionSandboxes } from '../sandbox/session-sandbox.js';\nimport { postgresPoolCollector, postgresStatsCollector, asPool } from './collectors/postgres.js';\nimport type { PoolLike } from './collectors/postgres.js';\nimport { processCollector } from './collectors/process.js';\nimport { asMonitorablePubSub, connectMonitor, redisProbeCollector, redisStatsCollector } from './collectors/redis.js';\nimport type { RedisMonitorClient } from './collectors/redis.js';\nimport { intakeCollector, keepIntakeSource } from './intake.js';\nimport type { FactoryMetrics, MetricsCollector, MetricsTargets } from './metrics.js';\nimport { sandboxStartsCollector } from './sandbox.js';\n\nexport { METRIC_GROUPS, metricsService, resolveMetricsConfig } from './config.js';\nexport type { FactoryMetricsOptions, MetricGroup } from './config.js';\nexport { FactoryMetrics, factoryMetrics, installFactoryMetrics } from './metrics.js';\n\nconst SLOW_STATEMENTS_EVERY_MS = 10 * 60_000;\n\nexport interface MetricsSources {\n /** The Mastra storage, read for its `pg` pool when it's Postgres. */\n storage: unknown;\n vector: unknown;\n pubsub: unknown;\n /** Ids of the configured integrations; GitHub and Linear intake counts are sent as 0 when quiet. */\n integrationIds?: readonly string[];\n}\n\n/**\n * Builds the collectors for the enabled groups and starts the interval timer. Server-wide stats\n * (Postgres activity, Redis memory and stream backlog) only run on the worker service (or a\n * single-process deployment), so a split deployment doesn't report them twice. Returns a stop\n * function that also closes the Redis monitor connection.\n */\nexport function startFactoryMetrics(\n metrics: FactoryMetrics,\n targets: MetricsTargets,\n sources: MetricsSources,\n): () => Promise<void> {\n const intervalMs = metrics.intervalMs;\n if (intervalMs === 0) return async () => {};\n const timeoutMs = Math.min(5_000, intervalMs / 2);\n const serverWide = metrics.service !== 'api';\n const collectors: MetricsCollector[] = [];\n let redisClient: RedisMonitorClient | undefined;\n\n if (metrics.enabled('process'))\n collectors.push(processCollector((kind, ms) => metrics.observe('process', 'mastra_factory_gc_ms', ms, { kind })));\n\n if (metrics.enabled('postgres')) {\n const pools: Record<string, PoolLike> = {};\n const store = asPool((sources.storage as { pool?: unknown } | undefined)?.pool);\n const vector = asPool((sources.vector as { pool?: unknown } | undefined)?.pool);\n if (store) pools.store = store;\n if (vector) pools.vector = vector;\n if (Object.keys(pools).length > 0) collectors.push(postgresPoolCollector(pools, timeoutMs));\n const statsPool = store ?? vector;\n if (statsPool && serverWide) {\n const statementsEveryTicks = Math.max(1, Math.round(SLOW_STATEMENTS_EVERY_MS / intervalMs));\n collectors.push(postgresStatsCollector(statsPool, timeoutMs, statementsEveryTicks, targets.getLogger));\n }\n }\n\n if (metrics.enabled('redis')) {\n const pubsub = asMonitorablePubSub(sources.pubsub);\n if (pubsub) {\n redisClient = connectMonitor(pubsub, targets.getLogger);\n collectors.push(redisProbeCollector(redisClient, timeoutMs));\n // On a cluster, INFO reads one node, so server-wide stats would mix nodes. Only the probe runs.\n const cluster = 'masters' in (redisClient as object);\n if (serverWide && !cluster) collectors.push(redisStatsCollector(pubsub, redisClient, timeoutMs));\n }\n }\n\n if (metrics.enabled('sandbox')) {\n collectors.push({\n collect(sink) {\n sink.emit('mastra_factory_sandboxes_active', countRunningSessionSandboxes());\n },\n });\n collectors.push(sandboxStartsCollector());\n }\n\n if (metrics.enabled('intake')) {\n for (const id of sources.integrationIds ?? []) {\n if (id === 'github' || id === 'linear') keepIntakeSource(id);\n }\n collectors.push(intakeCollector());\n }\n\n metrics.start(targets, collectors);\n return async () => {\n metrics.stop();\n await redisClient?.destroy?.();\n };\n}\n"],"mappings":";;;;;;;;;AAcA,MAAM,2BAA2B;;;;;;;AAiBjC,SAAgB,oBACd,SACA,SACA,SACqB;CACrB,MAAM,aAAa,QAAQ;CAC3B,IAAI,eAAe,GAAG,OAAO,YAAY,CAAC;CAC1C,MAAM,YAAY,KAAK,IAAI,KAAO,aAAa,CAAC;CAChD,MAAM,aAAa,QAAQ,YAAY;CACvC,MAAM,aAAiC,CAAC;CACxC,IAAI;CAEJ,IAAI,QAAQ,QAAQ,SAAS,GAC3B,WAAW,KAAK,kBAAkB,MAAM,OAAO,QAAQ,QAAQ,WAAW,wBAAwB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ACjC,OAAO,YAAY;EACjB,QAAQ,KAAK;EACb,MAAM,aAAa,UAAU;CAC/B;AACF"}
|
|
@@ -17,10 +17,10 @@ export declare function markEagerSandboxStart(sandbox: object): void;
|
|
|
17
17
|
* Times every real start of `sandbox`:
|
|
18
18
|
* - the provider's part (lease and boot, up to the onStart hook), so a slow or failing provider is
|
|
19
19
|
* told apart from slow or failing repository setup;
|
|
20
|
-
* - the whole start, up to the end of the onStart hook (`
|
|
20
|
+
* - the whole start, up to the end of the onStart hook (`mastra_factory_sandbox_ready_ms`), with
|
|
21
21
|
* failures included. `kind` is `first` for a session's first sandbox and `resume` for later starts;
|
|
22
22
|
* - how long the first caller other than the eager start waited for it
|
|
23
|
-
* (`
|
|
23
|
+
* (`mastra_factory_sandbox_wait_ms`): the start-up time someone actually waits through.
|
|
24
24
|
* Patches `_start` in place: `start()`, `ensureRunning()` and the lazy start before the first command
|
|
25
25
|
* all go through it. `hadSandbox` says whether the session already had a sandbox before this instance.
|
|
26
26
|
*/
|
|
@@ -31,5 +31,5 @@ export declare function recordSandboxAcquired(sandbox: object, outcome: string |
|
|
|
31
31
|
* Starts in flight and the age of the oldest, sent every interval (0 when none), so a start that hangs
|
|
32
32
|
* shows while it's happening instead of only when it ends. Also shows the group is on while idle.
|
|
33
33
|
*/
|
|
34
|
-
export declare function
|
|
34
|
+
export declare function sandboxStartsCollector(): MetricsCollector;
|
|
35
35
|
//# sourceMappingURL=sandbox.d.ts.map
|
package/dist/metrics/sandbox.js
CHANGED
|
@@ -45,7 +45,7 @@ function recordSandboxCommand(phase, ms, outcome) {
|
|
|
45
45
|
});
|
|
46
46
|
}
|
|
47
47
|
const pendingStarts = /* @__PURE__ */ new WeakMap();
|
|
48
|
-
const
|
|
48
|
+
const sandboxStarts = /* @__PURE__ */ new Map();
|
|
49
49
|
const eagerStarts = /* @__PURE__ */ new WeakSet();
|
|
50
50
|
function isTimeout(error) {
|
|
51
51
|
const text = error instanceof Error ? `${error.name} ${error.message}` : String(error);
|
|
@@ -59,10 +59,10 @@ function markEagerSandboxStart(sandbox) {
|
|
|
59
59
|
* Times every real start of `sandbox`:
|
|
60
60
|
* - the provider's part (lease and boot, up to the onStart hook), so a slow or failing provider is
|
|
61
61
|
* told apart from slow or failing repository setup;
|
|
62
|
-
* - the whole start, up to the end of the onStart hook (`
|
|
62
|
+
* - the whole start, up to the end of the onStart hook (`mastra_factory_sandbox_ready_ms`), with
|
|
63
63
|
* failures included. `kind` is `first` for a session's first sandbox and `resume` for later starts;
|
|
64
64
|
* - how long the first caller other than the eager start waited for it
|
|
65
|
-
* (`
|
|
65
|
+
* (`mastra_factory_sandbox_wait_ms`): the start-up time someone actually waits through.
|
|
66
66
|
* Patches `_start` in place: `start()`, `ensureRunning()` and the lazy start before the first command
|
|
67
67
|
* all go through it. `hadSandbox` says whether the session already had a sandbox before this instance.
|
|
68
68
|
*/
|
|
@@ -81,7 +81,7 @@ function instrumentSandboxStart(sandbox, provider, hadSandbox) {
|
|
|
81
81
|
const eager = eagerStarts.delete(sandbox);
|
|
82
82
|
if (target.status === "running") return original.apply(sandbox, args);
|
|
83
83
|
const callStartedAt = performance.now();
|
|
84
|
-
let start =
|
|
84
|
+
let start = sandboxStarts.get(sandbox);
|
|
85
85
|
const real = !start;
|
|
86
86
|
if (!start) {
|
|
87
87
|
start = {
|
|
@@ -90,7 +90,7 @@ function instrumentSandboxStart(sandbox, provider, hadSandbox) {
|
|
|
90
90
|
waitRecorded: false
|
|
91
91
|
};
|
|
92
92
|
started = true;
|
|
93
|
-
|
|
93
|
+
sandboxStarts.set(sandbox, start);
|
|
94
94
|
pendingStarts.set(sandbox, {
|
|
95
95
|
startedAt: callStartedAt,
|
|
96
96
|
provider
|
|
@@ -100,11 +100,11 @@ function instrumentSandboxStart(sandbox, provider, hadSandbox) {
|
|
|
100
100
|
if (waits) start.waitRecorded = true;
|
|
101
101
|
const done = (outcome) => {
|
|
102
102
|
const now = performance.now();
|
|
103
|
-
if (waits) factoryMetrics().observe("sandbox", "
|
|
103
|
+
if (waits) factoryMetrics().observe("sandbox", "mastra_factory_sandbox_wait_ms", now - callStartedAt, {
|
|
104
104
|
kind: start.kind,
|
|
105
105
|
outcome
|
|
106
106
|
});
|
|
107
|
-
if (real) factoryMetrics().observe("sandbox", "
|
|
107
|
+
if (real) factoryMetrics().observe("sandbox", "mastra_factory_sandbox_ready_ms", now - start.startedAt, {
|
|
108
108
|
kind: start.kind,
|
|
109
109
|
outcome
|
|
110
110
|
});
|
|
@@ -123,7 +123,7 @@ function instrumentSandboxStart(sandbox, provider, hadSandbox) {
|
|
|
123
123
|
} finally {
|
|
124
124
|
if (real) {
|
|
125
125
|
pendingStarts.delete(sandbox);
|
|
126
|
-
|
|
126
|
+
sandboxStarts.delete(sandbox);
|
|
127
127
|
}
|
|
128
128
|
}
|
|
129
129
|
};
|
|
@@ -142,15 +142,15 @@ function recordSandboxAcquired(sandbox, outcome) {
|
|
|
142
142
|
* Starts in flight and the age of the oldest, sent every interval (0 when none), so a start that hangs
|
|
143
143
|
* shows while it's happening instead of only when it ends. Also shows the group is on while idle.
|
|
144
144
|
*/
|
|
145
|
-
function
|
|
145
|
+
function sandboxStartsCollector() {
|
|
146
146
|
return { collect(sink) {
|
|
147
147
|
const now = performance.now();
|
|
148
148
|
let oldest = 0;
|
|
149
|
-
for (const start of
|
|
150
|
-
sink.emit("
|
|
151
|
-
sink.emit("
|
|
149
|
+
for (const start of sandboxStarts.values()) oldest = Math.max(oldest, now - start.startedAt);
|
|
150
|
+
sink.emit("mastra_factory_sandboxes_starting", sandboxStarts.size);
|
|
151
|
+
sink.emit("mastra_factory_sandbox_start_oldest_age_ms", oldest);
|
|
152
152
|
} };
|
|
153
153
|
}
|
|
154
|
-
export { commandStepLabel, instrumentSandboxStart, markEagerSandboxStart, recordSandboxAcquired, recordSandboxCommand, recordWorkspacePhase,
|
|
154
|
+
export { commandStepLabel, instrumentSandboxStart, markEagerSandboxStart, recordSandboxAcquired, recordSandboxCommand, recordWorkspacePhase, sandboxStartsCollector, workspacePhaseLabel };
|
|
155
155
|
|
|
156
156
|
//# sourceMappingURL=sandbox.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"sandbox.js","names":[],"sources":["../../src/metrics/sandbox.ts"],"sourcesContent":["import type { MetricsCollector } from './metrics.js';\nimport { factoryMetrics } from './metrics.js';\n\nconst PHASES: Record<string, string> = {\n materialize: 'materialize',\n checkout: 'checkout',\n setup: 'setup',\n 'setup-marker': 'setup_marker',\n sync: 'sync',\n onStart: 'on_start',\n};\n\n/**\n * Maps a `timedPhase` name to the `phase` label. Only session workspace phases count; boot\n * phases (`prepare.*`, `background.*`) don't. The `(…)` argument names a repository or an outcome\n * and is never sent: repository names are unbounded.\n */\nexport function workspacePhaseLabel(phase: string): string | undefined {\n const match = /^workspace\\.([A-Za-z-]+)(?:\\(.*\\))?$/.exec(phase);\n return match ? PHASES[match[1]!] : undefined;\n}\n\n/** Maps a sandbox command `phase` (e.g. `repository clone`, `environment branch fetch`) to the `step` label. */\nexport function commandStepLabel(phase: string): string {\n if (phase === 'repository clone') return 'clone';\n if (phase === 'setup' || phase === 'teardown') return phase;\n if (phase.endsWith(' probe')) return 'probe';\n if (phase.endsWith(' fetch') || phase.endsWith(' refresh')) return 'fetch';\n if (/(checkout|create|retry)$/.test(phase)) return 'checkout';\n return 'other';\n}\n\n/** `timedPhase` hook: records a workspace phase duration. */\nexport function recordWorkspacePhase(phase: string, ms: number, ok: boolean): void {\n const metrics = factoryMetrics();\n if (!metrics.enabled('sandbox')) return;\n const label = workspacePhaseLabel(phase);\n if (!label) return;\n metrics.observe('sandbox', 'mastra_factory_sandbox_phase_ms', ms, { phase: label, outcome: ok ? 'ok' : 'error' });\n}\n\n/** Records one attempt of a sandbox command run during workspace setup. */\nexport function recordSandboxCommand(phase: string, ms: number, outcome: 'ok' | 'exit' | 'threw'): void {\n factoryMetrics().observe('sandbox', 'mastra_factory_sandbox_command_ms', ms, {\n step: commandStepLabel(phase),\n outcome,\n });\n}\n\ninterface StartableSandbox {\n status?: string;\n _start?: (...args: unknown[]) => Promise<unknown>;\n}\n\n// Sandboxes with a start in flight that hasn't reached their onStart hook yet, with its start time\n// and provider. The hook firing marks the end of the provider's part of the start.\nconst pendingStarts = new WeakMap<object, { startedAt: number; provider: string }>();\n\n// Every start in flight, from the first `_start` call until the onStart hook finishes or the start\n// fails. A `Map` (entries always removed when the start settles) so a collector can list them.\ninterface SessionStart {\n startedAt: number;\n kind: 'first' | 'resume';\n // Whether a caller other than the eager start has had its wait recorded.\n waitRecorded: boolean;\n}\nconst sessionStarts = new Map<object, SessionStart>();\n\n// Sandboxes whose next `_start` call is the eager start, which nobody waits on.\nconst eagerStarts = new WeakSet<object>();\n\nfunction isTimeout(error: unknown): boolean {\n const text = error instanceof Error ? `${error.name} ${error.message}` : String(error);\n return /time(d)?\\s?out/i.test(text);\n}\n\n/** Marks the next start of `sandbox` as the eager start fired when the workspace resolves. */\nexport function markEagerSandboxStart(sandbox: object): void {\n if (factoryMetrics().enabled('sandbox')) eagerStarts.add(sandbox);\n}\n\n/**\n * Times every real start of `sandbox`:\n * - the provider's part (lease and boot, up to the onStart hook), so a slow or failing provider is\n * told apart from slow or failing repository setup;\n * - the whole start, up to the end of the onStart hook (`mastra_factory_session_ready_ms`), with\n * failures included. `kind` is `first` for a session's first sandbox and `resume` for later starts;\n * - how long the first caller other than the eager start waited for it\n * (`mastra_factory_session_sandbox_wait_ms`): the start-up time someone actually waits through.\n * Patches `_start` in place: `start()`, `ensureRunning()` and the lazy start before the first command\n * all go through it. `hadSandbox` says whether the session already had a sandbox before this instance.\n */\nexport function instrumentSandboxStart(sandbox: object, provider: string, hadSandbox: boolean): void {\n const metrics = factoryMetrics();\n if (!metrics.enabled('sandbox')) return;\n // From the first sandbox on, start failures are sent every interval (0 when none).\n for (const cause of ['timeout', 'error']) {\n metrics.keepCounter('sandbox', 'mastra_factory_sandbox_start_failures_total', {\n sandbox_provider: provider,\n cause,\n });\n }\n const target = sandbox as StartableSandbox;\n const original = target._start;\n if (typeof original !== 'function') return;\n let started = hadSandbox;\n target._start = async (...args: unknown[]) => {\n const eager = eagerStarts.delete(sandbox);\n if (target.status === 'running') return original.apply(sandbox, args);\n const callStartedAt = performance.now();\n let start = sessionStarts.get(sandbox);\n const real = !start;\n if (!start) {\n start = { startedAt: callStartedAt, kind: started ? 'resume' : 'first', waitRecorded: false };\n started = true;\n sessionStarts.set(sandbox, start);\n pendingStarts.set(sandbox, { startedAt: callStartedAt, provider });\n }\n const waits = !eager && !start.waitRecorded;\n if (waits) start.waitRecorded = true;\n const done = (outcome: 'ok' | 'error') => {\n const now = performance.now();\n if (waits) {\n factoryMetrics().observe('sandbox', 'mastra_factory_session_sandbox_wait_ms', now - callStartedAt, {\n kind: start.kind,\n outcome,\n });\n }\n if (real) {\n factoryMetrics().observe('sandbox', 'mastra_factory_session_ready_ms', now - start.startedAt, {\n kind: start.kind,\n outcome,\n });\n }\n };\n try {\n const result = await original.apply(sandbox, args);\n done('ok');\n return result;\n } catch (error) {\n // Still pending: the provider failed before the onStart hook ran. Hook failures are setup\n // failures and are counted on the phase instead.\n if (real && pendingStarts.has(sandbox)) {\n factoryMetrics().count('sandbox', 'mastra_factory_sandbox_start_failures_total', {\n sandbox_provider: provider,\n cause: isTimeout(error) ? 'timeout' : 'error',\n });\n }\n done('error');\n throw error;\n } finally {\n if (real) {\n pendingStarts.delete(sandbox);\n sessionStarts.delete(sandbox);\n }\n }\n };\n}\n\n/** Called as the sandbox's onStart hook begins: the provider has handed over a running VM. */\nexport function recordSandboxAcquired(sandbox: object, outcome: string | undefined): void {\n const pending = pendingStarts.get(sandbox);\n if (!pending) return;\n pendingStarts.delete(sandbox);\n factoryMetrics().observe('sandbox', 'mastra_factory_sandbox_start_ms', performance.now() - pending.startedAt, {\n sandbox_provider: pending.provider,\n outcome: outcome === 'created' || outcome === 'connected' ? outcome : 'unknown',\n });\n}\n\n/**\n * Starts in flight and the age of the oldest, sent every interval (0 when none), so a start that hangs\n * shows while it's happening instead of only when it ends. Also shows the group is on while idle.\n */\nexport function sessionStartsCollector(): MetricsCollector {\n return {\n collect(sink) {\n const now = performance.now();\n let oldest = 0;\n for (const start of sessionStarts.values()) oldest = Math.max(oldest, now - start.startedAt);\n sink.emit('mastra_factory_sessions_starting', sessionStarts.size);\n sink.emit('mastra_factory_session_start_oldest_age_ms', oldest);\n },\n };\n}\n"],"mappings":";;AAGA,MAAM,SAAiC;CACrC,aAAa;CACb,UAAU;CACV,OAAO;CACP,gBAAgB;CAChB,MAAM;CACN,SAAS;AACX;;;;;;AAOA,SAAgB,oBAAoB,OAAmC;CACrE,MAAM,QAAQ,uCAAuC,KAAK,KAAK;CAC/D,OAAO,QAAQ,OAAO,MAAM,MAAO,KAAA;AACrC;;AAGA,SAAgB,iBAAiB,OAAuB;CACtD,IAAI,UAAU,oBAAoB,OAAO;CACzC,IAAI,UAAU,WAAW,UAAU,YAAY,OAAO;CACtD,IAAI,MAAM,SAAS,QAAQ,GAAG,OAAO;CACrC,IAAI,MAAM,SAAS,QAAQ,KAAK,MAAM,SAAS,UAAU,GAAG,OAAO;CACnE,IAAI,2BAA2B,KAAK,KAAK,GAAG,OAAO;CACnD,OAAO;AACT;;AAGA,SAAgB,qBAAqB,OAAe,IAAY,IAAmB;CACjF,MAAM,UAAU,eAAe;CAC/B,IAAI,CAAC,QAAQ,QAAQ,SAAS,GAAG;CACjC,MAAM,QAAQ,oBAAoB,KAAK;CACvC,IAAI,CAAC,OAAO;CACZ,QAAQ,QAAQ,WAAW,mCAAmC,IAAI;EAAE,OAAO;EAAO,SAAS,KAAK,OAAO;CAAQ,CAAC;AAClH;;AAGA,SAAgB,qBAAqB,OAAe,IAAY,SAAwC;CACtG,eAAe,CAAC,CAAC,QAAQ,WAAW,qCAAqC,IAAI;EAC3E,MAAM,iBAAiB,KAAK;EAC5B;CACF,CAAC;AACH;AASA,MAAM,gCAAgB,IAAI,QAAyD;AAUnF,MAAM,gCAAgB,IAAI,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AAe;IACjG,MAAM,MAAM;IACZ;GACF,CAAC;GAEH,IAAI,MACF,eAAe,CAAC,CAAC,QAAQ,WAAW,mCAAmC,MAAM,MAAM,WAAW;IAC5F,MAAM,MAAM;IACZ;GACF,CAAC;EAEL;EACA,IAAI;GACF,MAAM,SAAS,MAAM,SAAS,MAAM,SAAS,IAAI;GACjD,KAAK,IAAI;GACT,OAAO;EACT,SAAS,OAAO;GAGd,IAAI,QAAQ,cAAc,IAAI,OAAO,GACnC,eAAe,CAAC,CAAC,MAAM,WAAW,+CAA+C;IAC/E,kBAAkB;IAClB,OAAO,UAAU,KAAK,IAAI,YAAY;GACxC,CAAC;GAEH,KAAK,OAAO;GACZ,MAAM;EACR,UAAU;GACR,IAAI,MAAM;IACR,cAAc,OAAO,OAAO;IAC5B,cAAc,OAAO,OAAO;GAC9B;EACF;CACF;AACF;;AAGA,SAAgB,sBAAsB,SAAiB,SAAmC;CACxF,MAAM,UAAU,cAAc,IAAI,OAAO;CACzC,IAAI,CAAC,SAAS;CACd,cAAc,OAAO,OAAO;CAC5B,eAAe,CAAC,CAAC,QAAQ,WAAW,mCAAmC,YAAY,IAAI,IAAI,QAAQ,WAAW;EAC5G,kBAAkB,QAAQ;EAC1B,SAAS,YAAY,aAAa,YAAY,cAAc,UAAU;CACxE,CAAC;AACH;;;;;AAMA,SAAgB,yBAA2C;CACzD,OAAO,EACL,QAAQ,MAAM;EACZ,MAAM,MAAM,YAAY,IAAI;EAC5B,IAAI,SAAS;EACb,KAAK,MAAM,SAAS,cAAc,OAAO,GAAG,SAAS,KAAK,IAAI,QAAQ,MAAM,MAAM,SAAS;EAC3F,KAAK,KAAK,oCAAoC,cAAc,IAAI;EAChE,KAAK,KAAK,8CAA8C,MAAM;CAChE,EACF;AACF"}
|
|
1
|
+
{"version":3,"file":"sandbox.js","names":[],"sources":["../../src/metrics/sandbox.ts"],"sourcesContent":["import type { MetricsCollector } from './metrics.js';\nimport { factoryMetrics } from './metrics.js';\n\nconst PHASES: Record<string, string> = {\n materialize: 'materialize',\n checkout: 'checkout',\n setup: 'setup',\n 'setup-marker': 'setup_marker',\n sync: 'sync',\n onStart: 'on_start',\n};\n\n/**\n * Maps a `timedPhase` name to the `phase` label. Only session workspace phases count; boot\n * phases (`prepare.*`, `background.*`) don't. The `(…)` argument names a repository or an outcome\n * and is never sent: repository names are unbounded.\n */\nexport function workspacePhaseLabel(phase: string): string | undefined {\n const match = /^workspace\\.([A-Za-z-]+)(?:\\(.*\\))?$/.exec(phase);\n return match ? PHASES[match[1]!] : undefined;\n}\n\n/** Maps a sandbox command `phase` (e.g. `repository clone`, `environment branch fetch`) to the `step` label. */\nexport function commandStepLabel(phase: string): string {\n if (phase === 'repository clone') return 'clone';\n if (phase === 'setup' || phase === 'teardown') return phase;\n if (phase.endsWith(' probe')) return 'probe';\n if (phase.endsWith(' fetch') || phase.endsWith(' refresh')) return 'fetch';\n if (/(checkout|create|retry)$/.test(phase)) return 'checkout';\n return 'other';\n}\n\n/** `timedPhase` hook: records a workspace phase duration. */\nexport function recordWorkspacePhase(phase: string, ms: number, ok: boolean): void {\n const metrics = factoryMetrics();\n if (!metrics.enabled('sandbox')) return;\n const label = workspacePhaseLabel(phase);\n if (!label) return;\n metrics.observe('sandbox', 'mastra_factory_sandbox_phase_ms', ms, { phase: label, outcome: ok ? 'ok' : 'error' });\n}\n\n/** Records one attempt of a sandbox command run during workspace setup. */\nexport function recordSandboxCommand(phase: string, ms: number, outcome: 'ok' | 'exit' | 'threw'): void {\n factoryMetrics().observe('sandbox', 'mastra_factory_sandbox_command_ms', ms, {\n step: commandStepLabel(phase),\n outcome,\n });\n}\n\ninterface StartableSandbox {\n status?: string;\n _start?: (...args: unknown[]) => Promise<unknown>;\n}\n\n// Sandboxes with a start in flight that hasn't reached their onStart hook yet, with its start time\n// and provider. The hook firing marks the end of the provider's part of the start.\nconst pendingStarts = new WeakMap<object, { startedAt: number; provider: string }>();\n\n// Every start in flight, from the first `_start` call until the onStart hook finishes or the start\n// fails. A `Map` (entries always removed when the start settles) so a collector can list them.\ninterface SandboxStart {\n startedAt: number;\n kind: 'first' | 'resume';\n // Whether a caller other than the eager start has had its wait recorded.\n waitRecorded: boolean;\n}\nconst sandboxStarts = new Map<object, SandboxStart>();\n\n// Sandboxes whose next `_start` call is the eager start, which nobody waits on.\nconst eagerStarts = new WeakSet<object>();\n\nfunction isTimeout(error: unknown): boolean {\n const text = error instanceof Error ? `${error.name} ${error.message}` : String(error);\n return /time(d)?\\s?out/i.test(text);\n}\n\n/** Marks the next start of `sandbox` as the eager start fired when the workspace resolves. */\nexport function markEagerSandboxStart(sandbox: object): void {\n if (factoryMetrics().enabled('sandbox')) eagerStarts.add(sandbox);\n}\n\n/**\n * Times every real start of `sandbox`:\n * - the provider's part (lease and boot, up to the onStart hook), so a slow or failing provider is\n * told apart from slow or failing repository setup;\n * - the whole start, up to the end of the onStart hook (`mastra_factory_sandbox_ready_ms`), with\n * failures included. `kind` is `first` for a session's first sandbox and `resume` for later starts;\n * - how long the first caller other than the eager start waited for it\n * (`mastra_factory_sandbox_wait_ms`): the start-up time someone actually waits through.\n * Patches `_start` in place: `start()`, `ensureRunning()` and the lazy start before the first command\n * all go through it. `hadSandbox` says whether the session already had a sandbox before this instance.\n */\nexport function instrumentSandboxStart(sandbox: object, provider: string, hadSandbox: boolean): void {\n const metrics = factoryMetrics();\n if (!metrics.enabled('sandbox')) return;\n // From the first sandbox on, start failures are sent every interval (0 when none).\n for (const cause of ['timeout', 'error']) {\n metrics.keepCounter('sandbox', 'mastra_factory_sandbox_start_failures_total', {\n sandbox_provider: provider,\n cause,\n });\n }\n const target = sandbox as StartableSandbox;\n const original = target._start;\n if (typeof original !== 'function') return;\n let started = hadSandbox;\n target._start = async (...args: unknown[]) => {\n const eager = eagerStarts.delete(sandbox);\n if (target.status === 'running') return original.apply(sandbox, args);\n const callStartedAt = performance.now();\n let start = sandboxStarts.get(sandbox);\n const real = !start;\n if (!start) {\n start = { startedAt: callStartedAt, kind: started ? 'resume' : 'first', waitRecorded: false };\n started = true;\n sandboxStarts.set(sandbox, start);\n pendingStarts.set(sandbox, { startedAt: callStartedAt, provider });\n }\n const waits = !eager && !start.waitRecorded;\n if (waits) start.waitRecorded = true;\n const done = (outcome: 'ok' | 'error') => {\n const now = performance.now();\n if (waits) {\n factoryMetrics().observe('sandbox', 'mastra_factory_sandbox_wait_ms', now - callStartedAt, {\n kind: start.kind,\n outcome,\n });\n }\n if (real) {\n factoryMetrics().observe('sandbox', 'mastra_factory_sandbox_ready_ms', now - start.startedAt, {\n kind: start.kind,\n outcome,\n });\n }\n };\n try {\n const result = await original.apply(sandbox, args);\n done('ok');\n return result;\n } catch (error) {\n // Still pending: the provider failed before the onStart hook ran. Hook failures are setup\n // failures and are counted on the phase instead.\n if (real && pendingStarts.has(sandbox)) {\n factoryMetrics().count('sandbox', 'mastra_factory_sandbox_start_failures_total', {\n sandbox_provider: provider,\n cause: isTimeout(error) ? 'timeout' : 'error',\n });\n }\n done('error');\n throw error;\n } finally {\n if (real) {\n pendingStarts.delete(sandbox);\n sandboxStarts.delete(sandbox);\n }\n }\n };\n}\n\n/** Called as the sandbox's onStart hook begins: the provider has handed over a running VM. */\nexport function recordSandboxAcquired(sandbox: object, outcome: string | undefined): void {\n const pending = pendingStarts.get(sandbox);\n if (!pending) return;\n pendingStarts.delete(sandbox);\n factoryMetrics().observe('sandbox', 'mastra_factory_sandbox_start_ms', performance.now() - pending.startedAt, {\n sandbox_provider: pending.provider,\n outcome: outcome === 'created' || outcome === 'connected' ? outcome : 'unknown',\n });\n}\n\n/**\n * Starts in flight and the age of the oldest, sent every interval (0 when none), so a start that hangs\n * shows while it's happening instead of only when it ends. Also shows the group is on while idle.\n */\nexport function sandboxStartsCollector(): MetricsCollector {\n return {\n collect(sink) {\n const now = performance.now();\n let oldest = 0;\n for (const start of sandboxStarts.values()) oldest = Math.max(oldest, now - start.startedAt);\n sink.emit('mastra_factory_sandboxes_starting', sandboxStarts.size);\n sink.emit('mastra_factory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jD,KAAK,IAAI;GACT,OAAO;EACT,SAAS,OAAO;GAGd,IAAI,QAAQ,cAAc,IAAI,OAAO,GACnC,eAAe,CAAC,CAAC,MAAM,WAAW,+CAA+C;IAC/E,kBAAkB;IAClB,OAAO,UAAU,KAAK,IAAI,YAAY;GACxC,CAAC;GAEH,KAAK,OAAO;GACZ,MAAM;EACR,UAAU;GACR,IAAI,MAAM;IACR,cAAc,OAAO,OAAO;IAC5B,cAAc,OAAO,OAAO;GAC9B;EACF;CACF;AACF;;AAGA,SAAgB,sBAAsB,SAAiB,SAAmC;CACxF,MAAM,UAAU,cAAc,IAAI,OAAO;CACzC,IAAI,CAAC,SAAS;CACd,cAAc,OAAO,OAAO;CAC5B,eAAe,CAAC,CAAC,QAAQ,WAAW,mCAAmC,YAAY,IAAI,IAAI,QAAQ,WAAW;EAC5G,kBAAkB,QAAQ;EAC1B,SAAS,YAAY,aAAa,YAAY,cAAc,UAAU;CACxE,CAAC;AACH;;;;;AAMA,SAAgB,yBAA2C;CACzD,OAAO,EACL,QAAQ,MAAM;EACZ,MAAM,MAAM,YAAY,IAAI;EAC5B,IAAI,SAAS;EACb,KAAK,MAAM,SAAS,cAAc,OAAO,GAAG,SAAS,KAAK,IAAI,QAAQ,MAAM,MAAM,SAAS;EAC3F,KAAK,KAAK,qCAAqC,cAAc,IAAI;EACjE,KAAK,KAAK,8CAA8C,MAAM;CAChE,EACF;AACF"}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { AgentControllerEvent } from '@mastra/core/agent-controller';
|
|
2
|
+
/** The slice of a session the first-output timer needs. */
|
|
3
|
+
export interface FirstOutputSession {
|
|
4
|
+
readonly run: {
|
|
5
|
+
isRunning(): boolean;
|
|
6
|
+
};
|
|
7
|
+
subscribe(listener: (event: AgentControllerEvent) => void): () => void;
|
|
8
|
+
sendSignal(...args: never[]): {
|
|
9
|
+
accepted: Promise<{
|
|
10
|
+
action?: string;
|
|
11
|
+
}>;
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Times how long a message waits for the model's first output
|
|
16
|
+
* (`mastra_factory_session_first_output_ms`): from the message reaching an idle session to the
|
|
17
|
+
* first text, reasoning or tool call of the run it starts. Everything in between counts: waiting
|
|
18
|
+
* for the thread, building tools, memory, and the model's own time to first token.
|
|
19
|
+
*
|
|
20
|
+
* `turn` is `first` for the first run since this process loaded the session and `later` after
|
|
21
|
+
* that; `first` says what the output was. A run that ends without any output is recorded with
|
|
22
|
+
* `outcome=no_output`, so a start that fails still counts. A message sent while a run is in
|
|
23
|
+
* flight joins that run and is not timed.
|
|
24
|
+
*
|
|
25
|
+
* Patches `sendSignal` in place: every message, notice and board-started run goes through it.
|
|
26
|
+
*/
|
|
27
|
+
export declare function instrumentSessionFirstOutput(session: FirstOutputSession): void;
|
|
28
|
+
//# sourceMappingURL=session.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"session.d.ts","sourceRoot":"","sources":["../../src/metrics/session.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,+BAA+B,CAAC;AAI1E,2DAA2D;AAC3D,MAAM,WAAW,kBAAkB;IACjC,QAAQ,CAAC,GAAG,EAAE;QAAE,SAAS,IAAI,OAAO,CAAA;KAAE,CAAC;IACvC,SAAS,CAAC,QAAQ,EAAE,CAAC,KAAK,EAAE,oBAAoB,KAAK,IAAI,GAAG,MAAM,IAAI,CAAC;IACvE,UAAU,CAAC,GAAG,IAAI,EAAE,KAAK,EAAE,GAAG;QAAE,QAAQ,EAAE,OAAO,CAAC;YAAE,MAAM,CAAC,EAAE,MAAM,CAAA;SAAE,CAAC,CAAA;KAAE,CAAC;CAC1E;AA4BD;;;;;;;;;;;;GAYG;AACH,wBAAgB,4BAA4B,CAAC,OAAO,EAAE,kBAAkB,GAAG,IAAI,CAsC9E"}
|