@mastra/deployer-cloud 0.14.2-alpha.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.md +15 -0
- package/README.md +176 -0
- package/dist/index.cjs +488 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +462 -0
- package/dist/index.js.map +1 -0
- package/dist/utils/auth.d.ts +2 -0
- package/dist/utils/auth.d.ts.map +1 -0
- package/dist/utils/constants.d.ts +13 -0
- package/dist/utils/constants.d.ts.map +1 -0
- package/dist/utils/deps.d.ts +24 -0
- package/dist/utils/deps.d.ts.map +1 -0
- package/dist/utils/execa.d.ts +18 -0
- package/dist/utils/execa.d.ts.map +1 -0
- package/dist/utils/file.d.ts +2 -0
- package/dist/utils/file.d.ts.map +1 -0
- package/dist/utils/logger.d.ts +45 -0
- package/dist/utils/logger.d.ts.map +1 -0
- package/dist/utils/report.d.ts +2 -0
- package/dist/utils/report.d.ts.map +1 -0
- package/package.json +55 -0
- package/templates/instrumentation-template.js +69 -0
package/LICENSE.md
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# Apache License 2.0
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Kepler Software, Inc.
|
|
4
|
+
|
|
5
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
you may not use this file except in compliance with the License.
|
|
7
|
+
You may obtain a copy of the License at
|
|
8
|
+
|
|
9
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
|
|
11
|
+
Unless required by applicable law or agreed to in writing, software
|
|
12
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
See the License for the specific language governing permissions and
|
|
15
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
# @mastra/deployer-cloud
|
|
2
|
+
|
|
3
|
+
A cloud-optimized deployer for Mastra applications with built-in telemetry, logging, and storage integration.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- **Cloud-Native Integration**: Automatic setup for LibSQL storage and vector databases
|
|
8
|
+
- **Advanced Logging**: Built-in PinoLogger with HTTP transport for cloud logging endpoints
|
|
9
|
+
- **Telemetry & Monitoring**: OpenTelemetry instrumentation with readiness logging
|
|
10
|
+
- **Evaluation Hooks**: Automatic agent evaluation tracking and storage
|
|
11
|
+
- **Multi-Logger Support**: Combines cloud logging with existing application loggers
|
|
12
|
+
- **Environment-Based Configuration**: Smart configuration based on deployment environment
|
|
13
|
+
|
|
14
|
+
## Installation
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
pnpm add @mastra/deployer-cloud
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## Usage
|
|
21
|
+
|
|
22
|
+
The cloud deployer is used as part of the Mastra build process:
|
|
23
|
+
|
|
24
|
+
```typescript
|
|
25
|
+
import { CloudDeployer } from '@mastra/deployer-cloud';
|
|
26
|
+
|
|
27
|
+
const deployer = new CloudDeployer();
|
|
28
|
+
|
|
29
|
+
// Bundle your Mastra application
|
|
30
|
+
await deployer.bundle(mastraDir, outputDirectory);
|
|
31
|
+
|
|
32
|
+
// The deployer automatically:
|
|
33
|
+
// - Adds cloud dependencies
|
|
34
|
+
// - Sets up instrumentation
|
|
35
|
+
// - Configures logging and storage
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
## What It Does
|
|
39
|
+
|
|
40
|
+
### 1. Dependency Management
|
|
41
|
+
|
|
42
|
+
Automatically adds cloud-specific dependencies to your package.json:
|
|
43
|
+
|
|
44
|
+
- `@mastra/loggers` - Cloud-optimized logging
|
|
45
|
+
- `@mastra/libsql` - Serverless SQL storage
|
|
46
|
+
- `@mastra/cloud` - Cloud platform utilities
|
|
47
|
+
|
|
48
|
+
### 2. Server Entry Generation
|
|
49
|
+
|
|
50
|
+
Creates a production-ready server entry point with:
|
|
51
|
+
|
|
52
|
+
- Cloud storage initialization (LibSQL)
|
|
53
|
+
- Vector database setup
|
|
54
|
+
- Multi-transport logging
|
|
55
|
+
- Telemetry and monitoring
|
|
56
|
+
- Evaluation hooks for agent metrics
|
|
57
|
+
|
|
58
|
+
### 3. Instrumentation
|
|
59
|
+
|
|
60
|
+
Provides OpenTelemetry instrumentation for:
|
|
61
|
+
|
|
62
|
+
- Distributed tracing
|
|
63
|
+
- Performance monitoring
|
|
64
|
+
- Custom telemetry configuration
|
|
65
|
+
|
|
66
|
+
## Environment Variables
|
|
67
|
+
|
|
68
|
+
The deployer configures your application to use these environment variables:
|
|
69
|
+
|
|
70
|
+
```bash
|
|
71
|
+
# Storage Configuration
|
|
72
|
+
MASTRA_STORAGE_URL=your-libsql-url
|
|
73
|
+
MASTRA_STORAGE_AUTH_TOKEN=your-auth-token
|
|
74
|
+
|
|
75
|
+
# Logging Configuration
|
|
76
|
+
BUSINESS_API_RUNNER_LOGS_ENDPOINT=your-logs-endpoint
|
|
77
|
+
BUSINESS_JWT_TOKEN=your-jwt-token
|
|
78
|
+
|
|
79
|
+
# Runtime Configuration
|
|
80
|
+
RUNNER_START_TIME=deployment-start-time
|
|
81
|
+
CI=true|false
|
|
82
|
+
|
|
83
|
+
# Deployment Metadata
|
|
84
|
+
TEAM_ID=your-team-id
|
|
85
|
+
PROJECT_ID=your-project-id
|
|
86
|
+
BUILD_ID=your-build-id
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
## Generated Server Code
|
|
90
|
+
|
|
91
|
+
The deployer generates a server entry that:
|
|
92
|
+
|
|
93
|
+
1. **Initializes Logging**:
|
|
94
|
+
- Sets up PinoLogger with cloud transports
|
|
95
|
+
- Combines with existing application loggers
|
|
96
|
+
- Provides structured JSON logging
|
|
97
|
+
|
|
98
|
+
2. **Configures Storage**:
|
|
99
|
+
- Initializes LibSQL store when credentials are provided
|
|
100
|
+
- Sets up vector database for semantic search
|
|
101
|
+
- Integrates with Mastra's memory system
|
|
102
|
+
|
|
103
|
+
3. **Registers Hooks**:
|
|
104
|
+
- `ON_GENERATION` - Tracks agent generation metrics
|
|
105
|
+
- `ON_EVALUATION` - Stores evaluation results
|
|
106
|
+
|
|
107
|
+
4. **Starts Server**:
|
|
108
|
+
- Creates Node.js server with Mastra configuration
|
|
109
|
+
- Disables playground and Swagger UI for production
|
|
110
|
+
- Exports tools for API access
|
|
111
|
+
|
|
112
|
+
## Project Structure
|
|
113
|
+
|
|
114
|
+
After deployment, your project will have:
|
|
115
|
+
|
|
116
|
+
```
|
|
117
|
+
output/
|
|
118
|
+
├── instrumentation.mjs # OpenTelemetry setup
|
|
119
|
+
├── package.json # With cloud dependencies
|
|
120
|
+
├── index.mjs # Main server entry
|
|
121
|
+
├── mastra.mjs # Your Mastra configuration
|
|
122
|
+
└── tools/ # Exported tools
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
## Readiness Logging
|
|
126
|
+
|
|
127
|
+
The deployer includes structured readiness logs for monitoring:
|
|
128
|
+
|
|
129
|
+
```json
|
|
130
|
+
{
|
|
131
|
+
"message": "Server starting|Server started|Runner Initialized",
|
|
132
|
+
"type": "READINESS",
|
|
133
|
+
"startTime": 1234567890,
|
|
134
|
+
"durationMs": 123,
|
|
135
|
+
"metadata": {
|
|
136
|
+
"teamId": "your-team-id",
|
|
137
|
+
"projectId": "your-project-id",
|
|
138
|
+
"buildId": "your-build-id"
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
## Testing
|
|
144
|
+
|
|
145
|
+
The cloud deployer includes comprehensive tests covering:
|
|
146
|
+
|
|
147
|
+
- Build pipeline functionality
|
|
148
|
+
- Server runtime generation
|
|
149
|
+
- Dependency management
|
|
150
|
+
- Error handling
|
|
151
|
+
- Integration scenarios
|
|
152
|
+
|
|
153
|
+
Run tests with:
|
|
154
|
+
|
|
155
|
+
```bash
|
|
156
|
+
pnpm test
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
## Development
|
|
160
|
+
|
|
161
|
+
For local development:
|
|
162
|
+
|
|
163
|
+
```bash
|
|
164
|
+
# Build the deployer
|
|
165
|
+
pnpm build
|
|
166
|
+
|
|
167
|
+
# Run tests
|
|
168
|
+
pnpm test
|
|
169
|
+
|
|
170
|
+
# Lint code
|
|
171
|
+
pnpm lint
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
## License
|
|
175
|
+
|
|
176
|
+
Apache-2.0
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,488 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var url = require('url');
|
|
4
|
+
var path = require('path');
|
|
5
|
+
var deployer = require('@mastra/deployer');
|
|
6
|
+
var fsExtra = require('fs-extra');
|
|
7
|
+
var os = require('os');
|
|
8
|
+
var fs = require('fs');
|
|
9
|
+
var error = require('@mastra/core/error');
|
|
10
|
+
var stream = require('stream');
|
|
11
|
+
var execa = require('execa');
|
|
12
|
+
var logger$1 = require('@mastra/core/logger');
|
|
13
|
+
var loggers = require('@mastra/loggers');
|
|
14
|
+
var redis = require('redis');
|
|
15
|
+
|
|
16
|
+
var _documentCurrentScript = typeof document !== 'undefined' ? document.currentScript : null;
|
|
17
|
+
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
18
|
+
|
|
19
|
+
function _interopNamespace(e) {
|
|
20
|
+
if (e && e.__esModule) return e;
|
|
21
|
+
var n = Object.create(null);
|
|
22
|
+
if (e) {
|
|
23
|
+
Object.keys(e).forEach(function (k) {
|
|
24
|
+
if (k !== 'default') {
|
|
25
|
+
var d = Object.getOwnPropertyDescriptor(e, k);
|
|
26
|
+
Object.defineProperty(n, k, d.get ? d : {
|
|
27
|
+
enumerable: true,
|
|
28
|
+
get: function () { return e[k]; }
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
n.default = e;
|
|
34
|
+
return Object.freeze(n);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
var os__default = /*#__PURE__*/_interopDefault(os);
|
|
38
|
+
var fs__namespace = /*#__PURE__*/_interopNamespace(fs);
|
|
39
|
+
|
|
40
|
+
// src/index.ts
|
|
41
|
+
|
|
42
|
+
// src/utils/auth.ts
|
|
43
|
+
function getAuthEntrypoint() {
|
|
44
|
+
return `
|
|
45
|
+
import { MastraAuthProvider } from '@mastra/core/server';
|
|
46
|
+
|
|
47
|
+
class MastraCloudAuth extends MastraAuthProvider {
|
|
48
|
+
constructor (auth) {
|
|
49
|
+
super()
|
|
50
|
+
this.auth = auth
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
async authenticateToken (...args) {
|
|
54
|
+
if (typeof args[0] === 'string' && args[0].replace('Bearer ', '') === '${process.env.BUSINESS_JWT_TOKEN}') {
|
|
55
|
+
return { id: 'business-api' }
|
|
56
|
+
}
|
|
57
|
+
return this.auth.authenticateToken(...args)
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
async authorizeUser (...args) {
|
|
61
|
+
if (args[1] && args[1].path === '/api') {
|
|
62
|
+
return true
|
|
63
|
+
}
|
|
64
|
+
if (args[0] && args[0].id === 'business-api') {
|
|
65
|
+
return true
|
|
66
|
+
}
|
|
67
|
+
return this.auth.authorizeUser(...args)
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const serverConfig = mastra.getServer()
|
|
72
|
+
if (serverConfig && serverConfig.experimental_auth) {
|
|
73
|
+
const auth = serverConfig.experimental_auth
|
|
74
|
+
serverConfig.experimental_auth = new MastraCloudAuth(auth)
|
|
75
|
+
}
|
|
76
|
+
`;
|
|
77
|
+
}
|
|
78
|
+
var LOCAL = process.env.LOCAL === "true";
|
|
79
|
+
var TEAM_ID = process.env.TEAM_ID ?? "";
|
|
80
|
+
var PROJECT_ID = process.env.PROJECT_ID ?? "";
|
|
81
|
+
var BUILD_ID = process.env.BUILD_ID ?? "";
|
|
82
|
+
var BUILD_URL = process.env.BUILD_URL ?? "";
|
|
83
|
+
var LOG_REDIS_URL = process.env.LOG_REDIS_URL ?? "redis://localhost:6379";
|
|
84
|
+
var USER_IP_ADDRESS = process.env.USER_IP_ADDRESS ?? "";
|
|
85
|
+
var MASTRA_DIRECTORY = process.env.MASTRA_DIRECTORY ?? "src/mastra";
|
|
86
|
+
var PROJECT_ENV_VARS = safelyParseJson(process.env.PROJECT_ENV_VARS ?? "{}");
|
|
87
|
+
LOCAL ? os__default.default.tmpdir() + "/project" : process.env.PROJECT_ROOT ?? "/project";
|
|
88
|
+
function safelyParseJson(json) {
|
|
89
|
+
try {
|
|
90
|
+
return JSON.parse(json);
|
|
91
|
+
} catch {
|
|
92
|
+
return {};
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
var redisClient = redis.createClient({
|
|
96
|
+
url: LOG_REDIS_URL
|
|
97
|
+
});
|
|
98
|
+
var RedisTransport = class extends logger$1.LoggerTransport {
|
|
99
|
+
_transform(chunk, _encoding, callback) {
|
|
100
|
+
chunk = chunk.toString();
|
|
101
|
+
const logKey = `builder:logs:${TEAM_ID}:${PROJECT_ID}:${BUILD_ID}`;
|
|
102
|
+
const ttl = 2 * 24 * 60 * 60;
|
|
103
|
+
const logData = typeof chunk === "string" ? chunk : JSON.stringify(chunk);
|
|
104
|
+
process.nextTick(async () => {
|
|
105
|
+
try {
|
|
106
|
+
if (!redisClient.isOpen) {
|
|
107
|
+
await redisClient.connect().catch((err) => {
|
|
108
|
+
console.error("Redis connection error:", err);
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
const pipeline = redisClient.multi();
|
|
112
|
+
pipeline.rPush(logKey, logData);
|
|
113
|
+
pipeline.expire(logKey, ttl);
|
|
114
|
+
await pipeline.exec();
|
|
115
|
+
} catch (err) {
|
|
116
|
+
console.error("Redis logging error:", err);
|
|
117
|
+
}
|
|
118
|
+
});
|
|
119
|
+
callback(null, chunk);
|
|
120
|
+
}
|
|
121
|
+
_write(chunk, encoding, callback) {
|
|
122
|
+
if (typeof callback === "function") {
|
|
123
|
+
this._transform(chunk, encoding || "utf8", callback);
|
|
124
|
+
return true;
|
|
125
|
+
}
|
|
126
|
+
this._transform(chunk, encoding || "utf8", (error) => {
|
|
127
|
+
if (error) console.error("Transform error in write:", error);
|
|
128
|
+
});
|
|
129
|
+
return true;
|
|
130
|
+
}
|
|
131
|
+
async _flush() {
|
|
132
|
+
await new Promise((resolve2) => setTimeout(resolve2, 10));
|
|
133
|
+
}
|
|
134
|
+
async _destroy(err, cb) {
|
|
135
|
+
await closeLogger();
|
|
136
|
+
cb(err);
|
|
137
|
+
}
|
|
138
|
+
getLogs(_args) {
|
|
139
|
+
return Promise.resolve({
|
|
140
|
+
logs: [],
|
|
141
|
+
total: 0,
|
|
142
|
+
page: _args?.page ?? 1,
|
|
143
|
+
perPage: _args?.perPage ?? 100,
|
|
144
|
+
hasMore: false
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
getLogsByRunId(_args) {
|
|
148
|
+
return Promise.resolve({
|
|
149
|
+
logs: [],
|
|
150
|
+
total: 0,
|
|
151
|
+
page: _args?.page ?? 1,
|
|
152
|
+
perPage: _args?.perPage ?? 100,
|
|
153
|
+
hasMore: false
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
};
|
|
157
|
+
var transport = new RedisTransport();
|
|
158
|
+
var closeLogger = async () => {
|
|
159
|
+
if (redisClient.isOpen) {
|
|
160
|
+
setTimeout(async () => {
|
|
161
|
+
await redisClient.quit();
|
|
162
|
+
}, 10);
|
|
163
|
+
}
|
|
164
|
+
};
|
|
165
|
+
var logger = new loggers.PinoLogger({
|
|
166
|
+
level: "info",
|
|
167
|
+
transports: {
|
|
168
|
+
redis: transport
|
|
169
|
+
}
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
// src/utils/execa.ts
|
|
173
|
+
var createPinoStream = () => {
|
|
174
|
+
return new stream.Transform({
|
|
175
|
+
transform(chunk, encoding, callback) {
|
|
176
|
+
const line = chunk.toString().trim();
|
|
177
|
+
if (line) {
|
|
178
|
+
logger.info(line);
|
|
179
|
+
}
|
|
180
|
+
callback(null, chunk);
|
|
181
|
+
}
|
|
182
|
+
});
|
|
183
|
+
};
|
|
184
|
+
async function runWithExeca({
|
|
185
|
+
cmd,
|
|
186
|
+
args,
|
|
187
|
+
cwd = process.cwd(),
|
|
188
|
+
env = PROJECT_ENV_VARS
|
|
189
|
+
}) {
|
|
190
|
+
const pinoStream = createPinoStream();
|
|
191
|
+
try {
|
|
192
|
+
const subprocess = execa.execa(cmd, args, {
|
|
193
|
+
cwd,
|
|
194
|
+
env: {
|
|
195
|
+
...process.env,
|
|
196
|
+
...env
|
|
197
|
+
}
|
|
198
|
+
});
|
|
199
|
+
subprocess.stdout?.pipe(pinoStream);
|
|
200
|
+
subprocess.stderr?.pipe(pinoStream);
|
|
201
|
+
const { stdout, stderr, exitCode } = await subprocess;
|
|
202
|
+
return { stdout, stderr, success: exitCode === 0 };
|
|
203
|
+
} catch (error) {
|
|
204
|
+
logger.error(`Process failed: ${error}`);
|
|
205
|
+
return { success: false, error: error instanceof Error ? error : new Error(String(error)) };
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
// src/utils/deps.ts
|
|
210
|
+
var MEMOIZED = /* @__PURE__ */ new Map();
|
|
211
|
+
function findLockFile(dir) {
|
|
212
|
+
const lockFiles = ["pnpm-lock.yaml", "package-lock.json", "yarn.lock", "bun.lock"];
|
|
213
|
+
for (const file of lockFiles) {
|
|
214
|
+
if (fs__namespace.existsSync(path.join(dir, file))) {
|
|
215
|
+
return file;
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
const parentDir = path.resolve(dir, "..");
|
|
219
|
+
if (parentDir !== dir) {
|
|
220
|
+
return findLockFile(parentDir);
|
|
221
|
+
}
|
|
222
|
+
return null;
|
|
223
|
+
}
|
|
224
|
+
function detectPm({ path }) {
|
|
225
|
+
const cached = MEMOIZED.get(path);
|
|
226
|
+
if (cached) {
|
|
227
|
+
return cached;
|
|
228
|
+
}
|
|
229
|
+
const lockFile = findLockFile(path);
|
|
230
|
+
let pm = "npm";
|
|
231
|
+
switch (lockFile) {
|
|
232
|
+
case "pnpm-lock.yaml":
|
|
233
|
+
pm = "pnpm";
|
|
234
|
+
break;
|
|
235
|
+
case "package-lock.json":
|
|
236
|
+
pm = "npm";
|
|
237
|
+
break;
|
|
238
|
+
case "yarn.lock":
|
|
239
|
+
pm = "yarn";
|
|
240
|
+
break;
|
|
241
|
+
case "bun.lock":
|
|
242
|
+
pm = "bun";
|
|
243
|
+
break;
|
|
244
|
+
default:
|
|
245
|
+
pm = "npm";
|
|
246
|
+
}
|
|
247
|
+
MEMOIZED.set(path, pm);
|
|
248
|
+
return pm;
|
|
249
|
+
}
|
|
250
|
+
async function installDeps({ path, pm }) {
|
|
251
|
+
pm = pm ?? detectPm({ path });
|
|
252
|
+
logger.info(`Installing dependencies with ${pm} in ${path}`);
|
|
253
|
+
const args = ["install"];
|
|
254
|
+
const { success, error: error$1 } = await runWithExeca({ cmd: pm, args, cwd: path });
|
|
255
|
+
if (!success) {
|
|
256
|
+
throw new error.MastraError(
|
|
257
|
+
{
|
|
258
|
+
id: "FAIL_INSTALL_DEPS",
|
|
259
|
+
category: "USER",
|
|
260
|
+
domain: "DEPLOYER"
|
|
261
|
+
},
|
|
262
|
+
error$1
|
|
263
|
+
);
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
function getMastraEntryFile(mastraDir) {
|
|
267
|
+
try {
|
|
268
|
+
const fileService = new deployer.FileService();
|
|
269
|
+
return fileService.getFirstExistingFile([
|
|
270
|
+
path.join(mastraDir, MASTRA_DIRECTORY, "index.ts"),
|
|
271
|
+
path.join(mastraDir, MASTRA_DIRECTORY, "index.js")
|
|
272
|
+
]);
|
|
273
|
+
} catch (error$1) {
|
|
274
|
+
throw new error.MastraError(
|
|
275
|
+
{
|
|
276
|
+
id: "MASTRA_ENTRY_FILE_NOT_FOUND",
|
|
277
|
+
category: "USER",
|
|
278
|
+
domain: "DEPLOYER"
|
|
279
|
+
},
|
|
280
|
+
error$1
|
|
281
|
+
);
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
// src/utils/report.ts
|
|
286
|
+
function successEntrypoint() {
|
|
287
|
+
return `
|
|
288
|
+
if (process.env.CI !== 'true' && process.env.REPORTER_API_URL && process.env.REPORTER_API_URL_AUTH_TOKEN) {
|
|
289
|
+
await fetch(process.env.REPORTER_API_URL, {
|
|
290
|
+
method: 'POST',
|
|
291
|
+
headers: {
|
|
292
|
+
'Content-Type': 'application/json',
|
|
293
|
+
Authorization: \`Bearer \${process.env.REPORTER_API_URL_AUTH_TOKEN}\`,
|
|
294
|
+
},
|
|
295
|
+
body: JSON.stringify(${JSON.stringify({
|
|
296
|
+
projectId: PROJECT_ID,
|
|
297
|
+
buildId: BUILD_ID,
|
|
298
|
+
status: "success",
|
|
299
|
+
url: BUILD_URL,
|
|
300
|
+
userIpAddress: USER_IP_ADDRESS
|
|
301
|
+
})}),
|
|
302
|
+
}).catch(err => {
|
|
303
|
+
console.error('Failed to report build status to monitoring service', {
|
|
304
|
+
error: err,
|
|
305
|
+
});
|
|
306
|
+
})
|
|
307
|
+
}
|
|
308
|
+
`;
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
// src/index.ts
|
|
312
|
+
var CloudDeployer = class extends deployer.Deployer {
|
|
313
|
+
constructor() {
|
|
314
|
+
super({ name: "cloud" });
|
|
315
|
+
}
|
|
316
|
+
async deploy(_outputDirectory) {
|
|
317
|
+
}
|
|
318
|
+
async writeInstrumentationFile(outputDirectory) {
|
|
319
|
+
const instrumentationFile = path.join(outputDirectory, "instrumentation.mjs");
|
|
320
|
+
const __dirname = path.dirname(url.fileURLToPath((typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.tagName.toUpperCase() === 'SCRIPT' && _documentCurrentScript.src || new URL('index.cjs', document.baseURI).href))));
|
|
321
|
+
await fsExtra.copy(path.join(__dirname, "../templates", "instrumentation-template.js"), instrumentationFile);
|
|
322
|
+
}
|
|
323
|
+
writePackageJson(outputDirectory, dependencies) {
|
|
324
|
+
dependencies.set("@mastra/loggers", "0.10.6");
|
|
325
|
+
dependencies.set("@mastra/libsql", "0.13.1");
|
|
326
|
+
dependencies.set("@mastra/cloud", "0.1.7");
|
|
327
|
+
return super.writePackageJson(outputDirectory, dependencies);
|
|
328
|
+
}
|
|
329
|
+
async lint() {
|
|
330
|
+
}
|
|
331
|
+
async installDependencies(outputDirectory, _rootDir = process.cwd()) {
|
|
332
|
+
await installDeps({ path: path.join(outputDirectory, "output"), pm: "npm" });
|
|
333
|
+
}
|
|
334
|
+
async bundle(mastraDir, outputDirectory) {
|
|
335
|
+
const currentCwd = process.cwd();
|
|
336
|
+
process.chdir(mastraDir);
|
|
337
|
+
const mastraEntryFile = getMastraEntryFile(mastraDir);
|
|
338
|
+
const defaultToolsPath = path.join(mastraDir, MASTRA_DIRECTORY, "tools");
|
|
339
|
+
await this._bundle(this.getEntry(), mastraEntryFile, outputDirectory, [defaultToolsPath]);
|
|
340
|
+
process.chdir(currentCwd);
|
|
341
|
+
}
|
|
342
|
+
getAuthEntrypoint() {
|
|
343
|
+
return getAuthEntrypoint();
|
|
344
|
+
}
|
|
345
|
+
getEntry() {
|
|
346
|
+
return `
|
|
347
|
+
import { createNodeServer, getToolExports } from '#server';
|
|
348
|
+
import { tools } from '#tools';
|
|
349
|
+
import { mastra } from '#mastra';
|
|
350
|
+
import { MultiLogger } from '@mastra/core/logger';
|
|
351
|
+
import { PinoLogger } from '@mastra/loggers';
|
|
352
|
+
import { UpstashTransport } from '@mastra/loggers/upstash';
|
|
353
|
+
import { HttpTransport } from '@mastra/loggers/http';
|
|
354
|
+
import { evaluate } from '@mastra/core/eval';
|
|
355
|
+
import { AvailableHooks, registerHook } from '@mastra/core/hooks';
|
|
356
|
+
import { LibSQLStore, LibSQLVector } from '@mastra/libsql';
|
|
357
|
+
|
|
358
|
+
const startTime = process.env.RUNNER_START_TIME ? new Date(process.env.RUNNER_START_TIME).getTime() : Date.now();
|
|
359
|
+
const createNodeServerStartTime = Date.now();
|
|
360
|
+
|
|
361
|
+
console.log(JSON.stringify({
|
|
362
|
+
message: "Server starting",
|
|
363
|
+
operation: 'builder.createNodeServer',
|
|
364
|
+
operation_startTime: createNodeServerStartTime,
|
|
365
|
+
type: "READINESS",
|
|
366
|
+
startTime,
|
|
367
|
+
metadata: {
|
|
368
|
+
teamId: "${TEAM_ID}",
|
|
369
|
+
projectId: "${PROJECT_ID}",
|
|
370
|
+
buildId: "${BUILD_ID}",
|
|
371
|
+
},
|
|
372
|
+
}));
|
|
373
|
+
|
|
374
|
+
const transports = {}
|
|
375
|
+
if (process.env.CI !== 'true') {
|
|
376
|
+
if (process.env.BUSINESS_API_RUNNER_LOGS_ENDPOINT) {
|
|
377
|
+
transports.default = new HttpTransport({
|
|
378
|
+
url: process.env.BUSINESS_API_RUNNER_LOGS_ENDPOINT,
|
|
379
|
+
headers: {
|
|
380
|
+
Authorization: 'Bearer ' + process.env.BUSINESS_JWT_TOKEN,
|
|
381
|
+
},
|
|
382
|
+
});
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
const logger = new PinoLogger({
|
|
387
|
+
name: 'MastraCloud',
|
|
388
|
+
transports,
|
|
389
|
+
level: 'debug',
|
|
390
|
+
});
|
|
391
|
+
const existingLogger = mastra?.getLogger();
|
|
392
|
+
const combinedLogger = existingLogger ? new MultiLogger([logger, existingLogger]) : logger;
|
|
393
|
+
|
|
394
|
+
mastra.setLogger({ logger: combinedLogger });
|
|
395
|
+
|
|
396
|
+
if (mastra?.storage) {
|
|
397
|
+
mastra.storage.init()
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
if (process.env.MASTRA_STORAGE_URL && process.env.MASTRA_STORAGE_AUTH_TOKEN) {
|
|
401
|
+
const { MastraStorage } = await import('@mastra/core/storage');
|
|
402
|
+
logger.info('Using Mastra Cloud Storage: ' + process.env.MASTRA_STORAGE_URL)
|
|
403
|
+
const storage = new LibSQLStore({
|
|
404
|
+
url: process.env.MASTRA_STORAGE_URL,
|
|
405
|
+
authToken: process.env.MASTRA_STORAGE_AUTH_TOKEN,
|
|
406
|
+
})
|
|
407
|
+
const vector = new LibSQLVector({
|
|
408
|
+
connectionUrl: process.env.MASTRA_STORAGE_URL,
|
|
409
|
+
authToken: process.env.MASTRA_STORAGE_AUTH_TOKEN,
|
|
410
|
+
})
|
|
411
|
+
|
|
412
|
+
await storage.init()
|
|
413
|
+
mastra?.setStorage(storage)
|
|
414
|
+
|
|
415
|
+
mastra?.memory?.setStorage(storage)
|
|
416
|
+
mastra?.memory?.setVector(vector)
|
|
417
|
+
|
|
418
|
+
registerHook(AvailableHooks.ON_GENERATION, ({ input, output, metric, runId, agentName, instructions }) => {
|
|
419
|
+
evaluate({
|
|
420
|
+
agentName,
|
|
421
|
+
input,
|
|
422
|
+
metric,
|
|
423
|
+
output,
|
|
424
|
+
runId,
|
|
425
|
+
globalRunId: runId,
|
|
426
|
+
instructions,
|
|
427
|
+
});
|
|
428
|
+
});
|
|
429
|
+
registerHook(AvailableHooks.ON_EVALUATION, async traceObject => {
|
|
430
|
+
if (mastra?.storage) {
|
|
431
|
+
await mastra.storage.insert({
|
|
432
|
+
tableName: MastraStorage.TABLE_EVALS,
|
|
433
|
+
record: {
|
|
434
|
+
input: traceObject.input,
|
|
435
|
+
output: traceObject.output,
|
|
436
|
+
result: JSON.stringify(traceObject.result),
|
|
437
|
+
agent_name: traceObject.agentName,
|
|
438
|
+
metric_name: traceObject.metricName,
|
|
439
|
+
instructions: traceObject.instructions,
|
|
440
|
+
test_info: null,
|
|
441
|
+
global_run_id: traceObject.globalRunId,
|
|
442
|
+
run_id: traceObject.runId,
|
|
443
|
+
created_at: new Date().toISOString(),
|
|
444
|
+
},
|
|
445
|
+
});
|
|
446
|
+
}
|
|
447
|
+
});
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
${getAuthEntrypoint()}
|
|
451
|
+
|
|
452
|
+
await createNodeServer(mastra, { playground: false, swaggerUI: false, tools: getToolExports(tools) });
|
|
453
|
+
|
|
454
|
+
${successEntrypoint()}
|
|
455
|
+
|
|
456
|
+
console.log(JSON.stringify({
|
|
457
|
+
message: "Server started",
|
|
458
|
+
operation: 'builder.createNodeServer',
|
|
459
|
+
operation_startTime: createNodeServerStartTime,
|
|
460
|
+
operation_durationMs: Date.now() - createNodeServerStartTime,
|
|
461
|
+
type: "READINESS",
|
|
462
|
+
startTime,
|
|
463
|
+
metadata: {
|
|
464
|
+
teamId: "${TEAM_ID}",
|
|
465
|
+
projectId: "${PROJECT_ID}",
|
|
466
|
+
buildId: "${BUILD_ID}",
|
|
467
|
+
},
|
|
468
|
+
}));
|
|
469
|
+
|
|
470
|
+
|
|
471
|
+
console.log(JSON.stringify({
|
|
472
|
+
message: "Runner Initialized",
|
|
473
|
+
type: "READINESS",
|
|
474
|
+
startTime,
|
|
475
|
+
durationMs: Date.now() - startTime,
|
|
476
|
+
metadata: {
|
|
477
|
+
teamId: "${TEAM_ID}",
|
|
478
|
+
projectId: "${PROJECT_ID}",
|
|
479
|
+
buildId: "${BUILD_ID}",
|
|
480
|
+
},
|
|
481
|
+
}));
|
|
482
|
+
`;
|
|
483
|
+
}
|
|
484
|
+
};
|
|
485
|
+
|
|
486
|
+
exports.CloudDeployer = CloudDeployer;
|
|
487
|
+
//# sourceMappingURL=index.cjs.map
|
|
488
|
+
//# sourceMappingURL=index.cjs.map
|