@mastra/memory 0.2.11-alpha.1 → 0.2.11-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/.turbo/turbo-build.log +8 -8
- package/CHANGELOG.md +6 -0
- package/dist/index.cjs +14 -14
- package/dist/index.js +14 -13
- package/package.json +1 -1
- package/src/index.ts +22 -15
package/.turbo/turbo-build.log
CHANGED
|
@@ -1,29 +1,29 @@
|
|
|
1
1
|
|
|
2
|
-
> @mastra/memory@0.2.11-alpha.
|
|
2
|
+
> @mastra/memory@0.2.11-alpha.2 build /home/runner/work/mastra/mastra/packages/memory
|
|
3
3
|
> pnpm run check && tsup src/index.ts src/processors/index.ts --format esm,cjs --experimental-dts --clean --treeshake=smallest --splitting
|
|
4
4
|
|
|
5
5
|
|
|
6
|
-
> @mastra/memory@0.2.11-alpha.
|
|
6
|
+
> @mastra/memory@0.2.11-alpha.2 check /home/runner/work/mastra/mastra/packages/memory
|
|
7
7
|
> tsc --noEmit
|
|
8
8
|
|
|
9
9
|
[34mCLI[39m Building entry: src/index.ts, src/processors/index.ts
|
|
10
10
|
[34mCLI[39m Using tsconfig: tsconfig.json
|
|
11
11
|
[34mCLI[39m tsup v8.4.0
|
|
12
12
|
[34mTSC[39m Build start
|
|
13
|
-
[32mTSC[39m ⚡️ Build success in
|
|
13
|
+
[32mTSC[39m ⚡️ Build success in 10363ms
|
|
14
14
|
[34mDTS[39m Build start
|
|
15
15
|
[34mCLI[39m Target: es2022
|
|
16
16
|
Analysis will use the bundled TypeScript version 5.8.2
|
|
17
17
|
[36mWriting package typings: /home/runner/work/mastra/mastra/packages/memory/dist/_tsup-dts-rollup.d.ts[39m
|
|
18
18
|
Analysis will use the bundled TypeScript version 5.8.2
|
|
19
19
|
[36mWriting package typings: /home/runner/work/mastra/mastra/packages/memory/dist/_tsup-dts-rollup.d.cts[39m
|
|
20
|
-
[32mDTS[39m ⚡️ Build success in
|
|
20
|
+
[32mDTS[39m ⚡️ Build success in 12416ms
|
|
21
21
|
[34mCLI[39m Cleaning output folder
|
|
22
22
|
[34mESM[39m Build start
|
|
23
23
|
[34mCJS[39m Build start
|
|
24
|
+
[32mCJS[39m [1mdist/index.cjs [22m[32m17.58 KB[39m
|
|
25
|
+
[32mCJS[39m [1mdist/processors/index.cjs [22m[32m5.54 KB[39m
|
|
26
|
+
[32mCJS[39m ⚡️ Build success in 946ms
|
|
24
27
|
[32mESM[39m [1mdist/index.js [22m[32m17.39 KB[39m
|
|
25
28
|
[32mESM[39m [1mdist/processors/index.js [22m[32m5.33 KB[39m
|
|
26
|
-
[32mESM[39m ⚡️ Build success in
|
|
27
|
-
[32mCJS[39m [1mdist/index.cjs [22m[32m17.66 KB[39m
|
|
28
|
-
[32mCJS[39m [1mdist/processors/index.cjs [22m[32m5.54 KB[39m
|
|
29
|
-
[32mCJS[39m ⚡️ Build success in 1103ms
|
|
29
|
+
[32mESM[39m ⚡️ Build success in 950ms
|
package/CHANGELOG.md
CHANGED
package/dist/index.cjs
CHANGED
|
@@ -3,14 +3,11 @@
|
|
|
3
3
|
var core = require('@mastra/core');
|
|
4
4
|
var memory = require('@mastra/core/memory');
|
|
5
5
|
var ai = require('ai');
|
|
6
|
-
var lite = require('js-tiktoken/lite');
|
|
7
|
-
var o200k_base = require('js-tiktoken/ranks/o200k_base');
|
|
8
6
|
var xxhash = require('xxhash-wasm');
|
|
9
7
|
var zod = require('zod');
|
|
10
8
|
|
|
11
9
|
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
12
10
|
|
|
13
|
-
var o200k_base__default = /*#__PURE__*/_interopDefault(o200k_base);
|
|
14
11
|
var xxhash__default = /*#__PURE__*/_interopDefault(xxhash);
|
|
15
12
|
|
|
16
13
|
// src/index.ts
|
|
@@ -84,7 +81,7 @@ function reorderToolCallsAndResults(messages) {
|
|
|
84
81
|
}
|
|
85
82
|
|
|
86
83
|
// src/index.ts
|
|
87
|
-
var
|
|
84
|
+
var CHARS_PER_TOKEN = 4;
|
|
88
85
|
var Memory = class extends memory.MastraMemory {
|
|
89
86
|
constructor(config = {}) {
|
|
90
87
|
super({ name: "Memory", ...config });
|
|
@@ -237,19 +234,22 @@ var Memory = class extends memory.MastraMemory {
|
|
|
237
234
|
async deleteThread(threadId) {
|
|
238
235
|
await this.storage.__deleteThread({ threadId });
|
|
239
236
|
}
|
|
240
|
-
chunkText(text,
|
|
241
|
-
const
|
|
237
|
+
chunkText(text, tokenSize = 4096) {
|
|
238
|
+
const charSize = tokenSize * CHARS_PER_TOKEN;
|
|
242
239
|
const chunks = [];
|
|
243
|
-
let currentChunk =
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
currentChunk
|
|
240
|
+
let currentChunk = "";
|
|
241
|
+
const words = text.split(/\s+/);
|
|
242
|
+
for (const word of words) {
|
|
243
|
+
const wordWithSpace = currentChunk ? " " + word : word;
|
|
244
|
+
if (currentChunk.length + wordWithSpace.length > charSize) {
|
|
245
|
+
chunks.push(currentChunk);
|
|
246
|
+
currentChunk = word;
|
|
247
|
+
} else {
|
|
248
|
+
currentChunk += wordWithSpace;
|
|
249
249
|
}
|
|
250
250
|
}
|
|
251
|
-
if (currentChunk
|
|
252
|
-
chunks.push(
|
|
251
|
+
if (currentChunk) {
|
|
252
|
+
chunks.push(currentChunk);
|
|
253
253
|
}
|
|
254
254
|
return chunks;
|
|
255
255
|
}
|
package/dist/index.js
CHANGED
|
@@ -1,8 +1,6 @@
|
|
|
1
1
|
import { deepMerge } from '@mastra/core';
|
|
2
2
|
import { MastraMemory } from '@mastra/core/memory';
|
|
3
3
|
import { embedMany } from 'ai';
|
|
4
|
-
import { Tiktoken } from 'js-tiktoken/lite';
|
|
5
|
-
import o200k_base from 'js-tiktoken/ranks/o200k_base';
|
|
6
4
|
import xxhash from 'xxhash-wasm';
|
|
7
5
|
import { z } from 'zod';
|
|
8
6
|
|
|
@@ -77,7 +75,7 @@ function reorderToolCallsAndResults(messages) {
|
|
|
77
75
|
}
|
|
78
76
|
|
|
79
77
|
// src/index.ts
|
|
80
|
-
var
|
|
78
|
+
var CHARS_PER_TOKEN = 4;
|
|
81
79
|
var Memory = class extends MastraMemory {
|
|
82
80
|
constructor(config = {}) {
|
|
83
81
|
super({ name: "Memory", ...config });
|
|
@@ -230,19 +228,22 @@ var Memory = class extends MastraMemory {
|
|
|
230
228
|
async deleteThread(threadId) {
|
|
231
229
|
await this.storage.__deleteThread({ threadId });
|
|
232
230
|
}
|
|
233
|
-
chunkText(text,
|
|
234
|
-
const
|
|
231
|
+
chunkText(text, tokenSize = 4096) {
|
|
232
|
+
const charSize = tokenSize * CHARS_PER_TOKEN;
|
|
235
233
|
const chunks = [];
|
|
236
|
-
let currentChunk =
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
currentChunk
|
|
234
|
+
let currentChunk = "";
|
|
235
|
+
const words = text.split(/\s+/);
|
|
236
|
+
for (const word of words) {
|
|
237
|
+
const wordWithSpace = currentChunk ? " " + word : word;
|
|
238
|
+
if (currentChunk.length + wordWithSpace.length > charSize) {
|
|
239
|
+
chunks.push(currentChunk);
|
|
240
|
+
currentChunk = word;
|
|
241
|
+
} else {
|
|
242
|
+
currentChunk += wordWithSpace;
|
|
242
243
|
}
|
|
243
244
|
}
|
|
244
|
-
if (currentChunk
|
|
245
|
-
chunks.push(
|
|
245
|
+
if (currentChunk) {
|
|
246
|
+
chunks.push(currentChunk);
|
|
246
247
|
}
|
|
247
248
|
return chunks;
|
|
248
249
|
}
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -4,13 +4,13 @@ import { MastraMemory } from '@mastra/core/memory';
|
|
|
4
4
|
import type { MessageType, MemoryConfig, SharedMemoryConfig, StorageThreadType } from '@mastra/core/memory';
|
|
5
5
|
import type { StorageGetMessagesArg } from '@mastra/core/storage';
|
|
6
6
|
import { embedMany } from 'ai';
|
|
7
|
-
|
|
8
|
-
import o200k_base from 'js-tiktoken/ranks/o200k_base';
|
|
7
|
+
|
|
9
8
|
import xxhash from 'xxhash-wasm';
|
|
10
9
|
import { updateWorkingMemoryTool } from './tools/working-memory';
|
|
11
10
|
import { reorderToolCallsAndResults } from './utils';
|
|
12
11
|
|
|
13
|
-
|
|
12
|
+
// Average characters per token based on OpenAI's tokenization
|
|
13
|
+
const CHARS_PER_TOKEN = 4;
|
|
14
14
|
|
|
15
15
|
/**
|
|
16
16
|
* Concrete implementation of MastraMemory that adds support for thread configuration
|
|
@@ -233,24 +233,31 @@ export class Memory extends MastraMemory {
|
|
|
233
233
|
await this.storage.__deleteThread({ threadId });
|
|
234
234
|
}
|
|
235
235
|
|
|
236
|
-
private chunkText(text: string,
|
|
237
|
-
|
|
236
|
+
private chunkText(text: string, tokenSize = 4096) {
|
|
237
|
+
// Convert token size to character size with some buffer
|
|
238
|
+
const charSize = tokenSize * CHARS_PER_TOKEN;
|
|
238
239
|
const chunks: string[] = [];
|
|
239
|
-
let currentChunk
|
|
240
|
+
let currentChunk = '';
|
|
241
|
+
|
|
242
|
+
// Split text into words to avoid breaking words
|
|
243
|
+
const words = text.split(/\s+/);
|
|
240
244
|
|
|
241
|
-
for (const
|
|
242
|
-
|
|
245
|
+
for (const word of words) {
|
|
246
|
+
// Add space before word unless it's the first word in the chunk
|
|
247
|
+
const wordWithSpace = currentChunk ? ' ' + word : word;
|
|
243
248
|
|
|
244
|
-
// If
|
|
245
|
-
if (currentChunk.length
|
|
246
|
-
chunks.push(
|
|
247
|
-
currentChunk =
|
|
249
|
+
// If adding this word would exceed the chunk size, start a new chunk
|
|
250
|
+
if (currentChunk.length + wordWithSpace.length > charSize) {
|
|
251
|
+
chunks.push(currentChunk);
|
|
252
|
+
currentChunk = word;
|
|
253
|
+
} else {
|
|
254
|
+
currentChunk += wordWithSpace;
|
|
248
255
|
}
|
|
249
256
|
}
|
|
250
257
|
|
|
251
|
-
// Add
|
|
252
|
-
if (currentChunk
|
|
253
|
-
chunks.push(
|
|
258
|
+
// Add the final chunk if not empty
|
|
259
|
+
if (currentChunk) {
|
|
260
|
+
chunks.push(currentChunk);
|
|
254
261
|
}
|
|
255
262
|
|
|
256
263
|
return chunks;
|