@rudderjs/ai 1.18.1 → 1.18.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,5 +1,187 @@
1
- // @rudderjs/ai is deprecated. The AI engine now lives in @gemstack/ai-sdk.
2
- // This module re-exports it for backwards compatibility; import from
3
- // '@gemstack/ai-sdk/memory-orm' directly in new code.
4
- export * from '@gemstack/ai-sdk/memory-orm';
1
+ /**
2
+ * `@rudderjs/ai/memory-orm` — ORM-backed {@link UserMemory} for #A4 Phase 4.
3
+ *
4
+ * Stores per-user facts in a `UserMemory` table via the registered
5
+ * `@rudderjs/orm` adapter (Prisma today; Drizzle as well once the user's
6
+ * tables are wired). Drop-in alongside Phase 1's in-process
7
+ * `MemoryUserMemory`.
8
+ *
9
+ * Wire it from your AI config:
10
+ *
11
+ * ```ts
12
+ * // config/ai.ts
13
+ * import type { AiConfig } from '@gemstack/ai-sdk'
14
+ * import { OrmUserMemory } from '@rudderjs/ai/memory-orm'
15
+ *
16
+ * export default {
17
+ * default: 'anthropic/claude-sonnet-4-5',
18
+ * providers: { ... },
19
+ * memory: new OrmUserMemory(),
20
+ * } satisfies AiConfig
21
+ * ```
22
+ *
23
+ * The schema lives at `@rudderjs/ai/memory-orm`'s {@link userMemoryPrismaSchema}
24
+ * — copy it into your Prisma schema. The optional `embedding Bytes?`
25
+ * column is shipped here in Phase 4 (intentionally nullable) so Phase 5's
26
+ * `EmbeddingUserMemory` can populate it without forcing an additive
27
+ * migration.
28
+ */
29
+ import { Model } from '@rudderjs/orm';
30
+ // ─── ORM Model ────────────────────────────────────────────
31
+ /**
32
+ * The Model row backing {@link OrmUserMemory}. Exposed so apps that
33
+ * want their own queries (admin views, audit dumps) can use the
34
+ * familiar `UserMemoryRecord.where(...).get()` instead of routing
35
+ * everything through the {@link UserMemory} interface.
36
+ *
37
+ * Tags persist as a JSON-encoded string in the `tags` column — both
38
+ * Prisma's portable `String?` and Drizzle's `text` work without
39
+ * needing native array columns. The {@link UserMemory.recall} path
40
+ * filters tags in JavaScript for the same reason.
41
+ *
42
+ * The `embedding Bytes?` column is in the schema as of Phase 4
43
+ * (nullable) so `@rudderjs/ai/memory-embedding`'s `EmbeddingUserMemory`
44
+ * (Phase 5) writes the Float32-packed vector here on `remember()` and
45
+ * reads it for cosine recall. `OrmUserMemory` ignores it — the
46
+ * column stays `null` for any row stored without the embedding
47
+ * composer.
48
+ */
49
+ export class UserMemoryRecord extends Model {
50
+ static table = 'userMemory';
51
+ static fillable = ['userId', 'fact', 'tags', 'score', 'embedding'];
52
+ /** Parsed tags array; empty when nothing was stored. */
53
+ getTags() {
54
+ if (this.tags == null || this.tags === '')
55
+ return [];
56
+ try {
57
+ const parsed = JSON.parse(this.tags);
58
+ return Array.isArray(parsed) ? parsed.filter(t => typeof t === 'string') : [];
59
+ }
60
+ catch {
61
+ return [];
62
+ }
63
+ }
64
+ }
65
+ // ─── UserMemory adapter ───────────────────────────────────
66
+ /**
67
+ * `UserMemory` implementation that persists rows to the registered
68
+ * ORM adapter. Designed for production use — the in-process
69
+ * `MemoryUserMemory` is for tests and dev.
70
+ *
71
+ * Adapter coverage:
72
+ * - Prisma — works out of the box; copy {@link userMemoryPrismaSchema}
73
+ * into your schema.
74
+ * - Drizzle — works once you define a table matching the schema's
75
+ * columns and register it via `tables: { userMemory: <table> }` on
76
+ * the `drizzle()` config.
77
+ *
78
+ * Recall semantics: case-insensitive **token-OR-LIKE** matching against
79
+ * the `fact` column. The query is tokenized on non-alphanumeric
80
+ * boundaries (≥3-char tokens) and any row whose `fact` matches at
81
+ * least one token via `LIKE %tok%` is returned. Mirrors Phase 1's
82
+ * `MemoryUserMemory.recall()` behavior so the two backends are
83
+ * swap-compatible. Tag scope is applied JS-side after fetch — pushing
84
+ * tag-array filtering into the WHERE is adapter-specific and lands in a
85
+ * follow-up.
86
+ */
87
+ export class OrmUserMemory {
88
+ async remember(userId, fact, opts) {
89
+ const data = { userId, fact };
90
+ if (opts?.tags !== undefined)
91
+ data['tags'] = JSON.stringify(opts.tags);
92
+ if (opts?.score !== undefined)
93
+ data['score'] = opts.score;
94
+ const created = await UserMemoryRecord.create(data);
95
+ return rowToEntry(created);
96
+ }
97
+ async recall(userId, query, opts) {
98
+ const tokens = tokenize(query);
99
+ let q = UserMemoryRecord.where('userId', userId);
100
+ if (tokens.size > 0) {
101
+ const tokenList = [...tokens];
102
+ q = q.whereGroup(g => {
103
+ for (const tok of tokenList)
104
+ g.orWhere('fact', 'LIKE', `%${tok}%`);
105
+ });
106
+ }
107
+ const rows = await q.orderBy('createdAt', 'ASC').get();
108
+ const entries = rows.map(rowToEntry).filter(e => matchesTags(e, opts?.tags));
109
+ return capLimit(entries, opts?.limit);
110
+ }
111
+ async forget(userId, factId) {
112
+ const row = await UserMemoryRecord.where('id', factId).where('userId', userId).first();
113
+ if (row)
114
+ await row.delete();
115
+ }
116
+ async list(userId, opts) {
117
+ const rows = await UserMemoryRecord.where('userId', userId).orderBy('createdAt', 'ASC').get();
118
+ const entries = rows.map(rowToEntry).filter(e => matchesTags(e, opts?.tags));
119
+ return capLimit(entries, opts?.limit);
120
+ }
121
+ async forgetAll(userId) {
122
+ await UserMemoryRecord.where('userId', userId).deleteAll();
123
+ }
124
+ }
125
+ // ─── Schema reference ─────────────────────────────────────
126
+ /**
127
+ * Reference Prisma schema for `OrmUserMemory`. Copy into your
128
+ * `prisma/schema/<file>.prisma` (or paste alongside an existing
129
+ * model). The `embedding Bytes?` column is intentionally nullable so
130
+ * Phase 5's `EmbeddingUserMemory` becomes additive — no schema
131
+ * migration when you upgrade.
132
+ *
133
+ * SQLite stores `Bytes` as `BLOB`; Postgres stores it as `bytea`.
134
+ * Both work for the dot-product implementation Phase 5 will use.
135
+ */
136
+ export const userMemoryPrismaSchema = `model UserMemory {
137
+ id String @id @default(cuid())
138
+ userId String
139
+ fact String
140
+ /// JSON-encoded \`string[]\` of tags, or null
141
+ tags String?
142
+ /// Confidence score in [0, 1] — extract sets this from the model's self-rating
143
+ score Float?
144
+ /// Phase 5 — vector embedding for cosine recall (nullable so Phase 4 ignores it)
145
+ embedding Bytes?
146
+ createdAt DateTime @default(now())
147
+ updatedAt DateTime @updatedAt
148
+
149
+ @@index([userId])
150
+ }
151
+ `;
152
+ // ─── Helpers ──────────────────────────────────────────────
153
+ function rowToEntry(row) {
154
+ const tags = row.getTags();
155
+ const out = {
156
+ id: row.id,
157
+ userId: row.userId,
158
+ fact: row.fact,
159
+ createdAt: row.createdAt,
160
+ };
161
+ if (tags.length > 0)
162
+ out.tags = tags;
163
+ if (row.score != null)
164
+ out.score = row.score;
165
+ if (row.updatedAt != null)
166
+ out.updatedAt = row.updatedAt;
167
+ return out;
168
+ }
169
+ function tokenize(s) {
170
+ const out = new Set();
171
+ for (const tok of s.toLowerCase().split(/[^a-z0-9]+/)) {
172
+ if (tok.length >= 3)
173
+ out.add(tok);
174
+ }
175
+ return out;
176
+ }
177
+ function matchesTags(entry, wanted) {
178
+ if (!wanted || wanted.length === 0)
179
+ return true;
180
+ if (!entry.tags || entry.tags.length === 0)
181
+ return false;
182
+ return wanted.every(t => entry.tags.includes(t));
183
+ }
184
+ function capLimit(items, limit) {
185
+ return limit !== undefined && limit > 0 ? items.slice(0, limit) : items;
186
+ }
5
187
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/memory-orm/index.ts"],"names":[],"mappings":"AAAA,2EAA2E;AAC3E,qEAAqE;AACrE,sDAAsD;AACtD,cAAc,6BAA6B,CAAA"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/memory-orm/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AAEH,OAAO,EAAE,KAAK,EAAE,MAAM,eAAe,CAAA;AAMrC,6DAA6D;AAE7D;;;;;;;;;;;;;;;;;GAiBG;AACH,MAAM,OAAO,gBAAiB,SAAQ,KAAK;IACzC,MAAM,CAAU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,IAAI,CAAC,CAAC,WAAW,EAAE,CAAC,KAAK,CAAC,YAAY,CAAC,EAAE,CAAC;QACtD,IAAI,GAAG,CAAC,MAAM,IAAI,CAAC;YAAE,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,CAAA;IACnC,CAAC;IACD,OAAO,GAAG,CAAA;AACZ,CAAC;AAED,SAAS,WAAW,CAAC,KAAkB,EAAE,MAA4B;IACnE,IAAI,CAAC,MAAM,IAAI,MAAM,CAAC,MAAM,KAAK,CAAC;QAAE,OAAO,IAAI,CAAA;IAC/C,IAAI,CAAC,KAAK,CAAC,IAAI,IAAI,KAAK,CAAC,IAAI,CAAC,MAAM,KAAK,CAAC;QAAE,OAAO,KAAK,CAAA;IACxD,OAAO,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,CAAC,KAAK,CAAC,IAAK,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,CAAA;AACnD,CAAC;AAED,SAAS,QAAQ,CAAI,KAAU,EAAE,KAAyB;IACxD,OAAO,KAAK,KAAK,SAAS,IAAI,KAAK,GAAG,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,EAAE,KAAK,CAAC,CAAC,CAAC,CAAC,KAAK,CAAA;AACzE,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rudderjs/ai",
3
- "version": "1.18.1",
3
+ "version": "1.18.3",
4
4
  "description": "[Deprecated] Moved to @gemstack/ai-sdk. This package now re-exports it for backwards compatibility.",
5
5
  "rudderjs": {
6
6
  "provider": "AiProvider",
@@ -93,15 +93,20 @@
93
93
  }
94
94
  },
95
95
  "dependencies": {
96
- "@gemstack/ai-sdk": "^0.2.0"
96
+ "@gemstack/ai-mcp": "^0.1.0",
97
+ "@gemstack/ai-sdk": "^0.3.0"
97
98
  },
98
99
  "peerDependencies": {
99
100
  "@modelcontextprotocol/sdk": "^1.29.0",
100
101
  "react": ">=19.2.0",
101
- "@rudderjs/core": "^1.13.3",
102
- "@rudderjs/orm": "^1.22.0"
102
+ "@rudderjs/console": "^1.4.3",
103
+ "@rudderjs/orm": "^1.22.0",
104
+ "@rudderjs/core": "^1.13.3"
103
105
  },
104
106
  "peerDependenciesMeta": {
107
+ "@rudderjs/console": {
108
+ "optional": true
109
+ },
105
110
  "@rudderjs/core": {
106
111
  "optional": true
107
112
  },
@@ -123,12 +128,14 @@
123
128
  "openai": ">=4.70.0"
124
129
  },
125
130
  "devDependencies": {
126
- "@gemstack/ai-sdk": "^0.2.0",
131
+ "@gemstack/ai-mcp": "^0.1.0",
132
+ "@gemstack/ai-sdk": "^0.3.0",
127
133
  "@modelcontextprotocol/sdk": "^1.29.0",
128
134
  "@types/node": "^20.0.0",
129
135
  "@types/react": "^19.2.0",
130
136
  "react": "^19.2.0",
131
137
  "typescript": "^5.4.0",
138
+ "@rudderjs/console": "^1.4.3",
132
139
  "@rudderjs/core": "^1.13.3",
133
140
  "@rudderjs/orm": "^1.22.0"
134
141
  },
@@ -137,6 +144,7 @@
137
144
  "build": "tsc -p tsconfig.build.json",
138
145
  "dev": "tsc -p tsconfig.build.json --watch",
139
146
  "typecheck": "tsc --noEmit",
140
- "clean": "rm -rf dist"
147
+ "test": "tsc -p tsconfig.test.json && node --test \"dist-test/**/*.test.js\"",
148
+ "clean": "rm -rf dist dist-test"
141
149
  }
142
150
  }