@mastra/loggers 0.1.6-alpha.0 → 0.1.6-alpha.3

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.
@@ -1,19 +1,25 @@
1
1
 
2
- > @mastra/loggers@0.1.6-alpha.0 build /home/runner/work/mastra/mastra/packages/loggers
3
- > tsup src/file/index.ts src/upstash/index.ts --format esm --experimental-dts --clean --treeshake
2
+ > @mastra/loggers@0.1.6-alpha.3 build /home/runner/work/mastra/mastra/packages/loggers
3
+ > tsup src/file/index.ts src/upstash/index.ts --format esm,cjs --experimental-dts --clean --treeshake
4
4
 
5
5
  CLI Building entry: src/file/index.ts, src/upstash/index.ts
6
6
  CLI Using tsconfig: tsconfig.json
7
7
  CLI tsup v8.3.6
8
8
  TSC Build start
9
- TSC ⚡️ Build success in 5437ms
9
+ TSC ⚡️ Build success in 5816ms
10
10
  DTS Build start
11
11
  CLI Target: es2022
12
12
  Analysis will use the bundled TypeScript version 5.7.3
13
13
  Writing package typings: /home/runner/work/mastra/mastra/packages/loggers/dist/_tsup-dts-rollup.d.ts
14
- DTS ⚡️ Build success in 5926ms
14
+ Analysis will use the bundled TypeScript version 5.7.3
15
+ Writing package typings: /home/runner/work/mastra/mastra/packages/loggers/dist/_tsup-dts-rollup.d.cts
16
+ DTS ⚡️ Build success in 8917ms
15
17
  CLI Cleaning output folder
16
18
  ESM Build start
17
- ESM dist/upstash/index.js 3.71 KB
19
+ CJS Build start
18
20
  ESM dist/file/index.js 1.64 KB
19
- ESM ⚡️ Build success in 545ms
21
+ ESM dist/upstash/index.js 3.71 KB
22
+ ESM ⚡️ Build success in 537ms
23
+ CJS dist/file/index.cjs 1.64 KB
24
+ CJS dist/upstash/index.cjs 3.74 KB
25
+ CJS ⚡️ Build success in 536ms
package/CHANGELOG.md CHANGED
@@ -1,5 +1,38 @@
1
1
  # @mastra/loggers
2
2
 
3
+ ## 0.1.6-alpha.3
4
+
5
+ ### Patch Changes
6
+
7
+ - bb4f447: Add support for commonjs
8
+ - Updated dependencies [0fd78ac]
9
+ - Updated dependencies [0d25b75]
10
+ - Updated dependencies [fd14a3f]
11
+ - Updated dependencies [3f369a2]
12
+ - Updated dependencies [4d4e1e1]
13
+ - Updated dependencies [bb4f447]
14
+ - @mastra/core@0.4.3-alpha.3
15
+
16
+ ## 0.1.6-alpha.2
17
+
18
+ ### Patch Changes
19
+
20
+ - Updated dependencies [2512a93]
21
+ - Updated dependencies [e62de74]
22
+ - @mastra/core@0.4.3-alpha.2
23
+
24
+ ## 0.1.6-alpha.1
25
+
26
+ ### Patch Changes
27
+
28
+ - Updated dependencies [0d185b1]
29
+ - Updated dependencies [ed55f1d]
30
+ - Updated dependencies [8d13b14]
31
+ - Updated dependencies [3ee4831]
32
+ - Updated dependencies [108793c]
33
+ - Updated dependencies [5f28f44]
34
+ - @mastra/core@0.4.3-alpha.1
35
+
3
36
  ## 0.1.6-alpha.0
4
37
 
5
38
  ### Patch Changes
@@ -0,0 +1,50 @@
1
+ import type { BaseLogMessage } from '@mastra/core/logger';
2
+ import { LoggerTransport } from '@mastra/core/logger';
3
+ import type { WriteStream } from 'fs';
4
+
5
+ export declare class FileTransport extends LoggerTransport {
6
+ path: string;
7
+ fileStream: WriteStream;
8
+ constructor({ path }: {
9
+ path: string;
10
+ });
11
+ _transform(chunk: any, _encoding: string, callback: (error: Error | null, chunk: any) => void): void;
12
+ _flush(callback: Function): void;
13
+ _write(chunk: any, encoding?: string, callback?: (error?: Error | null) => void): boolean;
14
+ _destroy(error: Error, callback: Function): void;
15
+ getLogs(): Promise<BaseLogMessage[]>;
16
+ getLogsByRunId({ runId }: {
17
+ runId: string;
18
+ }): Promise<BaseLogMessage[]>;
19
+ }
20
+
21
+ export declare class UpstashTransport extends LoggerTransport {
22
+ upstashUrl: string;
23
+ upstashToken: string;
24
+ listName: string;
25
+ maxListLength: number;
26
+ batchSize: number;
27
+ flushInterval: number;
28
+ logBuffer: any[];
29
+ lastFlush: number;
30
+ flushIntervalId: NodeJS.Timeout;
31
+ constructor(opts: {
32
+ listName?: string;
33
+ maxListLength?: number;
34
+ batchSize?: number;
35
+ upstashUrl: string;
36
+ flushInterval?: number;
37
+ upstashToken: string;
38
+ });
39
+ private executeUpstashCommand;
40
+ _flush(): Promise<void>;
41
+ _write(chunk: any, encoding?: string, callback?: (error?: Error | null) => void): boolean;
42
+ _transform(chunk: string, _enc: string, cb: Function): void;
43
+ _destroy(err: Error, cb: Function): void;
44
+ getLogs(): Promise<BaseLogMessage[]>;
45
+ getLogsByRunId({ runId }: {
46
+ runId: string;
47
+ }): Promise<BaseLogMessage[]>;
48
+ }
49
+
50
+ export { }
@@ -0,0 +1,63 @@
1
+ 'use strict';
2
+
3
+ var fs = require('fs');
4
+ var logger = require('@mastra/core/logger');
5
+
6
+ // src/file/index.ts
7
+ var FileTransport = class extends logger.LoggerTransport {
8
+ path;
9
+ fileStream;
10
+ constructor({ path }) {
11
+ super({ objectMode: true });
12
+ this.path = path;
13
+ if (!fs.existsSync(this.path)) {
14
+ console.log(this.path);
15
+ throw new Error("File path does not exist");
16
+ }
17
+ this.fileStream = fs.createWriteStream(this.path, { flags: "a" });
18
+ }
19
+ _transform(chunk, _encoding, callback) {
20
+ try {
21
+ this.fileStream.write(chunk);
22
+ } catch (error) {
23
+ console.error("Error parsing log entry:", error);
24
+ }
25
+ callback(null, chunk);
26
+ }
27
+ _flush(callback) {
28
+ this.fileStream.end(() => {
29
+ callback();
30
+ });
31
+ }
32
+ _write(chunk, encoding, callback) {
33
+ if (typeof callback === "function") {
34
+ this._transform(chunk, encoding || "utf8", callback);
35
+ return true;
36
+ }
37
+ this._transform(chunk, encoding || "utf8", (error) => {
38
+ if (error) console.error("Transform error in write:", error);
39
+ });
40
+ return true;
41
+ }
42
+ // Clean up resources
43
+ _destroy(error, callback) {
44
+ if (this.fileStream) {
45
+ this.fileStream.destroy(error);
46
+ }
47
+ callback(error);
48
+ }
49
+ async getLogs() {
50
+ return fs.readFileSync(this.path, "utf8").split("\n").filter(Boolean).map((log) => JSON.parse(log));
51
+ }
52
+ async getLogsByRunId({ runId }) {
53
+ try {
54
+ const allLogs = await this.getLogs();
55
+ return allLogs.filter((log) => log?.runId === runId) || [];
56
+ } catch (error) {
57
+ console.error("Error getting logs by runId from Upstash:", error);
58
+ return [];
59
+ }
60
+ }
61
+ };
62
+
63
+ exports.FileTransport = FileTransport;
@@ -0,0 +1 @@
1
+ export { FileTransport } from '../_tsup-dts-rollup.cjs';
@@ -0,0 +1,133 @@
1
+ 'use strict';
2
+
3
+ var logger = require('@mastra/core/logger');
4
+
5
+ // src/upstash/index.ts
6
+ var UpstashTransport = class extends logger.LoggerTransport {
7
+ upstashUrl;
8
+ upstashToken;
9
+ listName;
10
+ maxListLength;
11
+ batchSize;
12
+ flushInterval;
13
+ logBuffer;
14
+ lastFlush;
15
+ flushIntervalId;
16
+ constructor(opts) {
17
+ super({ objectMode: true });
18
+ if (!opts.upstashUrl || !opts.upstashToken) {
19
+ throw new Error("Upstash URL and token are required");
20
+ }
21
+ this.upstashUrl = opts.upstashUrl;
22
+ this.upstashToken = opts.upstashToken;
23
+ this.listName = opts.listName || "application-logs";
24
+ this.maxListLength = opts.maxListLength || 1e4;
25
+ this.batchSize = opts.batchSize || 100;
26
+ this.flushInterval = opts.flushInterval || 1e4;
27
+ this.logBuffer = [];
28
+ this.lastFlush = Date.now();
29
+ this.flushIntervalId = setInterval(() => {
30
+ this._flush().catch((err) => {
31
+ console.error("Error flushing logs to Upstash:", err);
32
+ });
33
+ }, this.flushInterval);
34
+ }
35
+ async executeUpstashCommand(command) {
36
+ const response = await fetch(`${this.upstashUrl}/pipeline`, {
37
+ method: "POST",
38
+ headers: {
39
+ Authorization: `Bearer ${this.upstashToken}`,
40
+ "Content-Type": "application/json"
41
+ },
42
+ body: JSON.stringify([command])
43
+ });
44
+ if (!response.ok) {
45
+ throw new Error(`Failed to execute Upstash command: ${response.statusText}`);
46
+ }
47
+ return response.json();
48
+ }
49
+ async _flush() {
50
+ if (this.logBuffer.length === 0) {
51
+ return;
52
+ }
53
+ const now = Date.now();
54
+ const logs = this.logBuffer.splice(0, this.batchSize);
55
+ try {
56
+ const command = ["LPUSH", this.listName, ...logs.map((log) => JSON.stringify(log))];
57
+ if (this.maxListLength > 0) {
58
+ command.push("LTRIM", this.listName, 0, this.maxListLength - 1);
59
+ }
60
+ await this.executeUpstashCommand(command);
61
+ this.lastFlush = now;
62
+ } catch (error) {
63
+ this.logBuffer.unshift(...logs);
64
+ throw error;
65
+ }
66
+ }
67
+ _write(chunk, encoding, callback) {
68
+ if (typeof callback === "function") {
69
+ this._transform(chunk, encoding || "utf8", callback);
70
+ return true;
71
+ }
72
+ this._transform(chunk, encoding || "utf8", (error) => {
73
+ if (error) console.error("Transform error in write:", error);
74
+ });
75
+ return true;
76
+ }
77
+ _transform(chunk, _enc, cb) {
78
+ try {
79
+ const log = typeof chunk === "string" ? JSON.parse(chunk) : chunk;
80
+ if (!log.time) {
81
+ log.time = Date.now();
82
+ }
83
+ this.logBuffer.push(log);
84
+ if (this.logBuffer.length >= this.batchSize) {
85
+ this._flush().catch((err) => {
86
+ console.error("Error flushing logs to Upstash:", err);
87
+ });
88
+ }
89
+ cb(null, chunk);
90
+ } catch (error) {
91
+ cb(error);
92
+ }
93
+ }
94
+ _destroy(err, cb) {
95
+ clearInterval(this.flushIntervalId);
96
+ if (this.logBuffer.length > 0) {
97
+ this._flush().then(() => cb(err)).catch((flushErr) => {
98
+ console.error("Error in final flush:", flushErr);
99
+ cb(err || flushErr);
100
+ });
101
+ } else {
102
+ cb(err);
103
+ }
104
+ }
105
+ async getLogs() {
106
+ try {
107
+ const command = ["LRANGE", this.listName, 0, -1];
108
+ const response = await this.executeUpstashCommand(command);
109
+ return response?.[0]?.result.map((log) => {
110
+ try {
111
+ return JSON.parse(log);
112
+ } catch {
113
+ return "";
114
+ }
115
+ });
116
+ } catch (error) {
117
+ console.error("Error getting logs from Upstash:", error);
118
+ return [];
119
+ }
120
+ }
121
+ async getLogsByRunId({ runId }) {
122
+ try {
123
+ const allLogs = await this.getLogs();
124
+ const logs = allLogs.filter((log) => log.runId === runId) || [];
125
+ return logs;
126
+ } catch (error) {
127
+ console.error("Error getting logs by runId from Upstash:", error);
128
+ return [];
129
+ }
130
+ }
131
+ };
132
+
133
+ exports.UpstashTransport = UpstashTransport;
@@ -0,0 +1 @@
1
+ export { UpstashTransport } from '../_tsup-dts-rollup.cjs';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mastra/loggers",
3
- "version": "0.1.6-alpha.0",
3
+ "version": "0.1.6-alpha.3",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "types": "./dist/index.d.ts",
@@ -10,18 +10,30 @@
10
10
  "import": {
11
11
  "types": "./dist/index.d.ts",
12
12
  "default": "./dist/index.js"
13
+ },
14
+ "require": {
15
+ "types": "./dist/index.d.cts",
16
+ "default": "./dist/index.cjs"
13
17
  }
14
18
  },
15
19
  "./file": {
16
20
  "import": {
17
21
  "types": "./dist/file/index.d.ts",
18
22
  "default": "./dist/file/index.js"
23
+ },
24
+ "require": {
25
+ "types": "./dist/file/index.d.cts",
26
+ "default": "./dist/file/index.cjs"
19
27
  }
20
28
  },
21
29
  "./upstash": {
22
30
  "import": {
23
31
  "types": "./dist/upstash/index.d.ts",
24
32
  "default": "./dist/upstash/index.js"
33
+ },
34
+ "require": {
35
+ "types": "./dist/upstash/index.d.cts",
36
+ "default": "./dist/upstash/index.cjs"
25
37
  }
26
38
  },
27
39
  "./package.json": "./package.json"
@@ -30,7 +42,7 @@
30
42
  "author": "",
31
43
  "license": "ISC",
32
44
  "dependencies": {
33
- "@mastra/core": "^0.4.3-alpha.0"
45
+ "@mastra/core": "^0.4.3-alpha.3"
34
46
  },
35
47
  "devDependencies": {
36
48
  "@microsoft/api-extractor": "^7.49.2",
@@ -42,7 +54,7 @@
42
54
  "@internal/lint": "0.0.0"
43
55
  },
44
56
  "scripts": {
45
- "build": "tsup src/file/index.ts src/upstash/index.ts --format esm --experimental-dts --clean --treeshake",
57
+ "build": "tsup src/file/index.ts src/upstash/index.ts --format esm,cjs --experimental-dts --clean --treeshake",
46
58
  "build:watch": "pnpm build --watch",
47
59
  "test": "vitest run",
48
60
  "lint": "eslint ."