@kognitivedev/vercel-ai-provider 0.1.2 → 0.1.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.
Files changed (3) hide show
  1. package/dist/index.js +68 -30
  2. package/package.json +1 -1
  3. package/src/index.ts +74 -34
package/dist/index.js CHANGED
@@ -2,6 +2,26 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.createCognitiveLayer = createCognitiveLayer;
4
4
  const ai_1 = require("ai");
5
+ // Session-scoped snapshot cache: sessionKey → formatted memory block
6
+ const sessionSnapshots = new Map();
7
+ // Regex to detect if memory has already been injected
8
+ const MEMORY_TAG_REGEX = /<MemoryContext>/i;
9
+ /**
10
+ * Check if any system message already contains a <MemoryContext> block.
11
+ */
12
+ function hasExistingMemoryInjection(messages) {
13
+ return messages.some((msg) => {
14
+ if (msg.role === "system") {
15
+ const content = typeof msg.content === "string"
16
+ ? msg.content
17
+ : Array.isArray(msg.content)
18
+ ? msg.content.map((c) => c.text || "").join("")
19
+ : "";
20
+ return MEMORY_TAG_REGEX.test(content);
21
+ }
22
+ return false;
23
+ });
24
+ }
5
25
  function createCognitiveLayer(config) {
6
26
  const { provider, clConfig } = config;
7
27
  const baseUrl = clConfig.baseUrl || "http://localhost:3001";
@@ -70,52 +90,70 @@ function createCognitiveLayer(config) {
70
90
  async transformParams({ params }) {
71
91
  if (!userId)
72
92
  return params;
73
- let systemPromptToAdd = "";
74
93
  const incomingMessages = Array.isArray(params.prompt)
75
94
  ? params.prompt
76
95
  : [];
77
- try {
78
- const url = `${baseUrl}/api/cognitive/snapshot?userId=${userId}&agentId=${agentId}&appId=${clConfig.appId}`;
79
- const res = await fetch(url);
80
- if (res.ok) {
81
- const data = await res.json();
82
- const systemBlock = data.systemBlock || "";
83
- const userContextBlock = data.userContextBlock || "";
84
- const agentHeuristics = systemBlock || "None";
85
- const userContext = userContextBlock || "None";
86
- systemPromptToAdd = systemBlock !== "" || userContextBlock !== "" ? `
96
+ // 1) Check if memory is already injected in messages
97
+ if (hasExistingMemoryInjection(incomingMessages)) {
98
+ console.log("CL: memory already injected, skipping");
99
+ return params;
100
+ }
101
+ // 2) Check session cache
102
+ const sessionKey = `${userId}:${agentId}:${sessionId || "default"}`;
103
+ let systemPromptToAdd = sessionSnapshots.get(sessionKey);
104
+ // 3) Fetch snapshot only if not cached
105
+ if (systemPromptToAdd === undefined) {
106
+ try {
107
+ const url = `${baseUrl}/api/cognitive/snapshot?userId=${userId}&agentId=${agentId}&appId=${clConfig.appId}`;
108
+ const res = await fetch(url);
109
+ if (res.ok) {
110
+ const data = await res.json();
111
+ const systemBlock = data.systemBlock || "";
112
+ const userContextBlock = data.userContextBlock || "";
113
+ systemPromptToAdd =
114
+ systemBlock !== "" || userContextBlock !== ""
115
+ ? `
87
116
  <MemoryContext>
88
117
  Use the following memory to stay consistent. Prefer UserContext facts for answers; AgentHeuristics guide style, safety, and priorities.
89
- ${agentHeuristics}
90
- ${userContext}
118
+ ${systemBlock || "None"}
119
+ ${userContextBlock || "None"}
91
120
  </MemoryContext>
92
- `.trim() : "";
93
- console.log("CL: snapshot fetched", {
94
- userId,
95
- agentId,
96
- sessionId,
97
- systemLen: systemBlock.length,
98
- userLen: userContextBlock.length,
99
- hasSystem: !!systemBlock,
100
- hasUserContext: !!userContextBlock,
101
- });
121
+ `.trim()
122
+ : "";
123
+ // Cache the snapshot for this session
124
+ sessionSnapshots.set(sessionKey, systemPromptToAdd);
125
+ console.log("CL: snapshot fetched and cached", {
126
+ userId,
127
+ agentId,
128
+ sessionId,
129
+ sessionKey,
130
+ systemLen: systemBlock.length,
131
+ userLen: userContextBlock.length,
132
+ });
133
+ }
134
+ else {
135
+ console.warn("CognitiveLayer: snapshot fetch failed status", res.status);
136
+ systemPromptToAdd = "";
137
+ sessionSnapshots.set(sessionKey, systemPromptToAdd);
138
+ }
102
139
  }
103
- else {
104
- console.warn("CognitiveLayer: snapshot fetch failed status", res.status);
140
+ catch (e) {
141
+ console.warn("CognitiveLayer: Failed to fetch snapshot", e);
142
+ systemPromptToAdd = "";
143
+ sessionSnapshots.set(sessionKey, systemPromptToAdd);
105
144
  }
106
145
  }
107
- catch (e) {
108
- console.warn("CognitiveLayer: Failed to fetch snapshot", e);
146
+ else {
147
+ console.log("CL: using cached snapshot for session", { sessionKey });
109
148
  }
110
149
  if (!systemPromptToAdd) {
111
150
  return Object.assign(Object.assign({}, params), { messages: incomingMessages });
112
151
  }
113
152
  const { nextParams, messages: messagesWithMemory } = withMemorySystemPrompt(params, incomingMessages, systemPromptToAdd);
114
153
  console.log("CL: injecting memory system prompt", {
115
- systemPromptToAdd
154
+ sessionKey,
155
+ promptLength: systemPromptToAdd.length,
116
156
  });
117
- console.log("CL: messagesWithMemory", messagesWithMemory);
118
- console.log("CL: nextParams", nextParams);
119
157
  return Object.assign(Object.assign({}, nextParams), { prompt: messagesWithMemory });
120
158
  },
121
159
  async wrapGenerate({ doGenerate, params }) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kognitivedev/vercel-ai-provider",
3
- "version": "0.1.2",
3
+ "version": "0.1.3",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "publishConfig": {
package/src/index.ts CHANGED
@@ -20,6 +20,30 @@ export type CLModelWrapper = (
20
20
  providerOptions?: Record<string, unknown>
21
21
  ) => LanguageModelV2;
22
22
 
23
+ // Session-scoped snapshot cache: sessionKey → formatted memory block
24
+ const sessionSnapshots = new Map<string, string>();
25
+
26
+ // Regex to detect if memory has already been injected
27
+ const MEMORY_TAG_REGEX = /<MemoryContext>/i;
28
+
29
+ /**
30
+ * Check if any system message already contains a <MemoryContext> block.
31
+ */
32
+ function hasExistingMemoryInjection(messages: any[]): boolean {
33
+ return messages.some((msg) => {
34
+ if (msg.role === "system") {
35
+ const content =
36
+ typeof msg.content === "string"
37
+ ? msg.content
38
+ : Array.isArray(msg.content)
39
+ ? msg.content.map((c: any) => c.text || "").join("")
40
+ : "";
41
+ return MEMORY_TAG_REGEX.test(content);
42
+ }
43
+ return false;
44
+ });
45
+ }
46
+
23
47
  export function createCognitiveLayer(config: {
24
48
  provider: any;
25
49
  clConfig: CognitiveLayerConfig;
@@ -119,43 +143,63 @@ export function createCognitiveLayer(config: {
119
143
  async transformParams({ params }) {
120
144
  if (!userId) return params;
121
145
 
122
- let systemPromptToAdd = "";
123
146
  const incomingMessages = Array.isArray((params as any).prompt)
124
147
  ? (params as any).prompt
125
148
  : [];
126
149
 
150
+ // 1) Check if memory is already injected in messages
151
+ if (hasExistingMemoryInjection(incomingMessages)) {
152
+ console.log("CL: memory already injected, skipping");
153
+ return params;
154
+ }
155
+
156
+ // 2) Check session cache
157
+ const sessionKey = `${userId}:${agentId}:${sessionId || "default"}`;
158
+ let systemPromptToAdd = sessionSnapshots.get(sessionKey);
127
159
 
128
- try {
129
- const url = `${baseUrl}/api/cognitive/snapshot?userId=${userId}&agentId=${agentId}&appId=${clConfig.appId}`;
130
- const res = await fetch(url);
131
- if (res.ok) {
132
- const data = await res.json();
133
- const systemBlock = data.systemBlock || "";
134
- const userContextBlock = data.userContextBlock || "";
135
- const agentHeuristics = systemBlock || "None";
136
- const userContext = userContextBlock || "None";
137
- systemPromptToAdd = systemBlock !== "" || userContextBlock !== "" ? `
160
+ // 3) Fetch snapshot only if not cached
161
+ if (systemPromptToAdd === undefined) {
162
+ try {
163
+ const url = `${baseUrl}/api/cognitive/snapshot?userId=${userId}&agentId=${agentId}&appId=${clConfig.appId}`;
164
+ const res = await fetch(url);
165
+ if (res.ok) {
166
+ const data = await res.json();
167
+ const systemBlock = data.systemBlock || "";
168
+ const userContextBlock = data.userContextBlock || "";
169
+ systemPromptToAdd =
170
+ systemBlock !== "" || userContextBlock !== ""
171
+ ? `
138
172
  <MemoryContext>
139
173
  Use the following memory to stay consistent. Prefer UserContext facts for answers; AgentHeuristics guide style, safety, and priorities.
140
- ${agentHeuristics}
141
- ${userContext}
174
+ ${systemBlock || "None"}
175
+ ${userContextBlock || "None"}
142
176
  </MemoryContext>
143
- `.trim() : "";
144
-
145
- console.log("CL: snapshot fetched", {
146
- userId,
147
- agentId,
148
- sessionId,
149
- systemLen: systemBlock.length,
150
- userLen: userContextBlock.length,
151
- hasSystem: !!systemBlock,
152
- hasUserContext: !!userContextBlock,
153
- });
154
- } else {
155
- console.warn("CognitiveLayer: snapshot fetch failed status", res.status);
177
+ `.trim()
178
+ : "";
179
+
180
+ // Cache the snapshot for this session
181
+ sessionSnapshots.set(sessionKey, systemPromptToAdd);
182
+
183
+ console.log("CL: snapshot fetched and cached", {
184
+ userId,
185
+ agentId,
186
+ sessionId,
187
+ sessionKey,
188
+ systemLen: systemBlock.length,
189
+ userLen: userContextBlock.length,
190
+ });
191
+ } else {
192
+ console.warn("CognitiveLayer: snapshot fetch failed status", res.status);
193
+ systemPromptToAdd = "";
194
+ sessionSnapshots.set(sessionKey, systemPromptToAdd);
195
+ }
196
+ } catch (e) {
197
+ console.warn("CognitiveLayer: Failed to fetch snapshot", e);
198
+ systemPromptToAdd = "";
199
+ sessionSnapshots.set(sessionKey, systemPromptToAdd);
156
200
  }
157
- } catch (e) {
158
- console.warn("CognitiveLayer: Failed to fetch snapshot", e);
201
+ } else {
202
+ console.log("CL: using cached snapshot for session", { sessionKey });
159
203
  }
160
204
 
161
205
  if (!systemPromptToAdd) {
@@ -168,15 +212,11 @@ ${userContext}
168
212
  systemPromptToAdd
169
213
  );
170
214
 
171
-
172
-
173
215
  console.log("CL: injecting memory system prompt", {
174
- systemPromptToAdd
216
+ sessionKey,
217
+ promptLength: systemPromptToAdd.length,
175
218
  });
176
219
 
177
- console.log("CL: messagesWithMemory", messagesWithMemory);
178
- console.log("CL: nextParams", nextParams);
179
-
180
220
  return { ...nextParams, prompt: messagesWithMemory };
181
221
  },
182
222