@kognitivedev/vercel-ai-provider 0.1.2 → 0.1.4

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/dist/index.d.ts CHANGED
@@ -1,4 +1,13 @@
1
1
  import { LanguageModelV2 } from "@ai-sdk/provider";
2
+ /**
3
+ * Log levels for controlling verbosity of CognitiveLayer logging.
4
+ * - 'none': No logging
5
+ * - 'error': Only errors
6
+ * - 'warn': Errors and warnings
7
+ * - 'info': Errors, warnings, and info messages
8
+ * - 'debug': All messages including detailed snapshot data
9
+ */
10
+ export type LogLevel = 'none' | 'error' | 'warn' | 'info' | 'debug';
2
11
  export interface CognitiveLayerConfig {
3
12
  appId: string;
4
13
  defaultAgentId?: string;
@@ -10,6 +19,16 @@ export interface CognitiveLayerConfig {
10
19
  * Default: 500ms
11
20
  */
12
21
  processDelayMs?: number;
22
+ /**
23
+ * Log level for controlling verbosity of CognitiveLayer logging.
24
+ * - 'none': No logging
25
+ * - 'error': Only errors
26
+ * - 'warn': Errors and warnings
27
+ * - 'info': Errors, warnings, and info messages
28
+ * - 'debug': All messages including detailed snapshot data
29
+ * Default: 'info'
30
+ */
31
+ logLevel?: LogLevel;
13
32
  }
14
33
  export type CLModelWrapper = (modelId: string, settings?: {
15
34
  userId?: string;
package/dist/index.js CHANGED
@@ -2,11 +2,69 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.createCognitiveLayer = createCognitiveLayer;
4
4
  const ai_1 = require("ai");
5
+ const LOG_LEVEL_PRIORITY = {
6
+ none: 0,
7
+ error: 1,
8
+ warn: 2,
9
+ info: 3,
10
+ debug: 4,
11
+ };
12
+ /**
13
+ * Creates a logger that respects the configured log level.
14
+ */
15
+ function createLogger(logLevel) {
16
+ const currentPriority = LOG_LEVEL_PRIORITY[logLevel];
17
+ return {
18
+ error: (message, ...args) => {
19
+ if (currentPriority >= LOG_LEVEL_PRIORITY.error) {
20
+ console.error(`[CognitiveLayer:ERROR] ${message}`, ...args);
21
+ }
22
+ },
23
+ warn: (message, ...args) => {
24
+ if (currentPriority >= LOG_LEVEL_PRIORITY.warn) {
25
+ console.warn(`[CognitiveLayer:WARN] ${message}`, ...args);
26
+ }
27
+ },
28
+ info: (message, ...args) => {
29
+ if (currentPriority >= LOG_LEVEL_PRIORITY.info) {
30
+ console.log(`[CognitiveLayer:INFO] ${message}`, ...args);
31
+ }
32
+ },
33
+ debug: (message, ...args) => {
34
+ if (currentPriority >= LOG_LEVEL_PRIORITY.debug) {
35
+ console.log(`[CognitiveLayer:DEBUG] ${message}`, ...args);
36
+ }
37
+ },
38
+ };
39
+ }
40
+ // Session-scoped snapshot cache: sessionKey → formatted memory block
41
+ const sessionSnapshots = new Map();
42
+ // Regex to detect if memory has already been injected
43
+ const MEMORY_TAG_REGEX = /<MemoryContext>/i;
44
+ /**
45
+ * Check if any system message already contains a <MemoryContext> block.
46
+ */
47
+ function hasExistingMemoryInjection(messages) {
48
+ return messages.some((msg) => {
49
+ if (msg.role === "system") {
50
+ const content = typeof msg.content === "string"
51
+ ? msg.content
52
+ : Array.isArray(msg.content)
53
+ ? msg.content.map((c) => c.text || "").join("")
54
+ : "";
55
+ return MEMORY_TAG_REGEX.test(content);
56
+ }
57
+ return false;
58
+ });
59
+ }
5
60
  function createCognitiveLayer(config) {
6
61
  const { provider, clConfig } = config;
7
62
  const baseUrl = clConfig.baseUrl || "http://localhost:3001";
8
63
  // Default to 500ms delay to allow DB writes to settle
9
64
  const processDelay = clConfig.processDelayMs !== undefined ? clConfig.processDelayMs : 500;
65
+ // Default to 'info' log level
66
+ const logLevel = clConfig.logLevel || 'info';
67
+ const logger = createLogger(logLevel);
10
68
  const logConversation = async (payload) => {
11
69
  try {
12
70
  await fetch(`${baseUrl}/api/cognitive/log`, {
@@ -16,7 +74,7 @@ function createCognitiveLayer(config) {
16
74
  });
17
75
  }
18
76
  catch (e) {
19
- console.error("CognitiveLayer: Log failed", e);
77
+ logger.error("Log failed", e);
20
78
  }
21
79
  };
22
80
  const triggerProcessing = (userId, agentId, sessionId) => {
@@ -25,7 +83,7 @@ function createCognitiveLayer(config) {
25
83
  method: "POST",
26
84
  headers: { "Content-Type": "application/json" },
27
85
  body: JSON.stringify({ userId, agentId, sessionId }),
28
- }).catch(e => console.error("CognitiveLayer: Process trigger failed", e));
86
+ }).catch(e => logger.error("Process trigger failed", e));
29
87
  };
30
88
  if (processDelay > 0) {
31
89
  setTimeout(run, processDelay);
@@ -62,7 +120,7 @@ function createCognitiveLayer(config) {
62
120
  const sessionId = settings === null || settings === void 0 ? void 0 : settings.sessionId;
63
121
  const sessionMissing = !!userId && !sessionId;
64
122
  if (sessionMissing) {
65
- console.warn("CognitiveLayer: sessionId is required to log and process memories; skipping logging until provided.");
123
+ logger.warn("sessionId is required to log and process memories; skipping logging until provided.");
66
124
  }
67
125
  return (0, ai_1.wrapLanguageModel)({
68
126
  model,
@@ -70,52 +128,77 @@ function createCognitiveLayer(config) {
70
128
  async transformParams({ params }) {
71
129
  if (!userId)
72
130
  return params;
73
- let systemPromptToAdd = "";
74
131
  const incomingMessages = Array.isArray(params.prompt)
75
132
  ? params.prompt
76
133
  : [];
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 !== "" ? `
134
+ // 1) Check if memory is already injected in messages
135
+ if (hasExistingMemoryInjection(incomingMessages)) {
136
+ logger.debug("Memory already injected, skipping");
137
+ return params;
138
+ }
139
+ // 2) Check session cache
140
+ const sessionKey = `${userId}:${agentId}:${sessionId || "default"}`;
141
+ let systemPromptToAdd = sessionSnapshots.get(sessionKey);
142
+ // 3) Fetch snapshot only if not cached
143
+ if (systemPromptToAdd === undefined) {
144
+ try {
145
+ const url = `${baseUrl}/api/cognitive/snapshot?userId=${userId}&agentId=${agentId}&appId=${clConfig.appId}`;
146
+ const res = await fetch(url);
147
+ if (res.ok) {
148
+ const data = await res.json();
149
+ const systemBlock = data.systemBlock || "";
150
+ const userContextBlock = data.userContextBlock || "";
151
+ systemPromptToAdd =
152
+ systemBlock !== "" || userContextBlock !== ""
153
+ ? `
87
154
  <MemoryContext>
88
155
  Use the following memory to stay consistent. Prefer UserContext facts for answers; AgentHeuristics guide style, safety, and priorities.
89
- ${agentHeuristics}
90
- ${userContext}
156
+ ${systemBlock || "None"}
157
+ ${userContextBlock || "None"}
91
158
  </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
- });
159
+ `.trim()
160
+ : "";
161
+ // Cache the snapshot for this session
162
+ sessionSnapshots.set(sessionKey, systemPromptToAdd);
163
+ logger.info("Snapshot fetched and cached", {
164
+ userId,
165
+ agentId,
166
+ sessionId,
167
+ sessionKey,
168
+ systemLen: systemBlock.length,
169
+ userLen: userContextBlock.length,
170
+ });
171
+ // At debug level, log the full snapshot data
172
+ logger.debug("Full snapshot data", {
173
+ systemBlock,
174
+ userContextBlock,
175
+ rawData: data,
176
+ });
177
+ }
178
+ else {
179
+ logger.warn("Snapshot fetch failed", { status: res.status });
180
+ systemPromptToAdd = "";
181
+ sessionSnapshots.set(sessionKey, systemPromptToAdd);
182
+ }
102
183
  }
103
- else {
104
- console.warn("CognitiveLayer: snapshot fetch failed status", res.status);
184
+ catch (e) {
185
+ logger.warn("Failed to fetch snapshot", e);
186
+ systemPromptToAdd = "";
187
+ sessionSnapshots.set(sessionKey, systemPromptToAdd);
105
188
  }
106
189
  }
107
- catch (e) {
108
- console.warn("CognitiveLayer: Failed to fetch snapshot", e);
190
+ else {
191
+ logger.debug("Using cached snapshot for session", { sessionKey });
109
192
  }
110
193
  if (!systemPromptToAdd) {
111
194
  return Object.assign(Object.assign({}, params), { messages: incomingMessages });
112
195
  }
113
196
  const { nextParams, messages: messagesWithMemory } = withMemorySystemPrompt(params, incomingMessages, systemPromptToAdd);
114
- console.log("CL: injecting memory system prompt", {
115
- systemPromptToAdd
197
+ logger.info("Injecting memory system prompt", {
198
+ sessionKey,
199
+ promptLength: systemPromptToAdd.length,
116
200
  });
117
- console.log("CL: messagesWithMemory", messagesWithMemory);
118
- console.log("CL: nextParams", nextParams);
201
+ logger.debug("Injected prompt content", { systemPromptToAdd });
119
202
  return Object.assign(Object.assign({}, nextParams), { prompt: messagesWithMemory });
120
203
  },
121
204
  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.4",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "publishConfig": {
package/src/index.ts CHANGED
@@ -1,6 +1,54 @@
1
1
  import { wrapLanguageModel } from "ai";
2
2
  import { LanguageModelV2 } from "@ai-sdk/provider";
3
3
 
4
+ /**
5
+ * Log levels for controlling verbosity of CognitiveLayer logging.
6
+ * - 'none': No logging
7
+ * - 'error': Only errors
8
+ * - 'warn': Errors and warnings
9
+ * - 'info': Errors, warnings, and info messages
10
+ * - 'debug': All messages including detailed snapshot data
11
+ */
12
+ export type LogLevel = 'none' | 'error' | 'warn' | 'info' | 'debug';
13
+
14
+ const LOG_LEVEL_PRIORITY: Record<LogLevel, number> = {
15
+ none: 0,
16
+ error: 1,
17
+ warn: 2,
18
+ info: 3,
19
+ debug: 4,
20
+ };
21
+
22
+ /**
23
+ * Creates a logger that respects the configured log level.
24
+ */
25
+ function createLogger(logLevel: LogLevel) {
26
+ const currentPriority = LOG_LEVEL_PRIORITY[logLevel];
27
+
28
+ return {
29
+ error: (message: string, ...args: any[]) => {
30
+ if (currentPriority >= LOG_LEVEL_PRIORITY.error) {
31
+ console.error(`[CognitiveLayer:ERROR] ${message}`, ...args);
32
+ }
33
+ },
34
+ warn: (message: string, ...args: any[]) => {
35
+ if (currentPriority >= LOG_LEVEL_PRIORITY.warn) {
36
+ console.warn(`[CognitiveLayer:WARN] ${message}`, ...args);
37
+ }
38
+ },
39
+ info: (message: string, ...args: any[]) => {
40
+ if (currentPriority >= LOG_LEVEL_PRIORITY.info) {
41
+ console.log(`[CognitiveLayer:INFO] ${message}`, ...args);
42
+ }
43
+ },
44
+ debug: (message: string, ...args: any[]) => {
45
+ if (currentPriority >= LOG_LEVEL_PRIORITY.debug) {
46
+ console.log(`[CognitiveLayer:DEBUG] ${message}`, ...args);
47
+ }
48
+ },
49
+ };
50
+ }
51
+
4
52
  export interface CognitiveLayerConfig {
5
53
  appId: string;
6
54
  defaultAgentId?: string;
@@ -12,6 +60,16 @@ export interface CognitiveLayerConfig {
12
60
  * Default: 500ms
13
61
  */
14
62
  processDelayMs?: number;
63
+ /**
64
+ * Log level for controlling verbosity of CognitiveLayer logging.
65
+ * - 'none': No logging
66
+ * - 'error': Only errors
67
+ * - 'warn': Errors and warnings
68
+ * - 'info': Errors, warnings, and info messages
69
+ * - 'debug': All messages including detailed snapshot data
70
+ * Default: 'info'
71
+ */
72
+ logLevel?: LogLevel;
15
73
  }
16
74
 
17
75
  export type CLModelWrapper = (
@@ -20,6 +78,30 @@ export type CLModelWrapper = (
20
78
  providerOptions?: Record<string, unknown>
21
79
  ) => LanguageModelV2;
22
80
 
81
+ // Session-scoped snapshot cache: sessionKey → formatted memory block
82
+ const sessionSnapshots = new Map<string, string>();
83
+
84
+ // Regex to detect if memory has already been injected
85
+ const MEMORY_TAG_REGEX = /<MemoryContext>/i;
86
+
87
+ /**
88
+ * Check if any system message already contains a <MemoryContext> block.
89
+ */
90
+ function hasExistingMemoryInjection(messages: any[]): boolean {
91
+ return messages.some((msg) => {
92
+ if (msg.role === "system") {
93
+ const content =
94
+ typeof msg.content === "string"
95
+ ? msg.content
96
+ : Array.isArray(msg.content)
97
+ ? msg.content.map((c: any) => c.text || "").join("")
98
+ : "";
99
+ return MEMORY_TAG_REGEX.test(content);
100
+ }
101
+ return false;
102
+ });
103
+ }
104
+
23
105
  export function createCognitiveLayer(config: {
24
106
  provider: any;
25
107
  clConfig: CognitiveLayerConfig;
@@ -28,6 +110,9 @@ export function createCognitiveLayer(config: {
28
110
  const baseUrl = clConfig.baseUrl || "http://localhost:3001";
29
111
  // Default to 500ms delay to allow DB writes to settle
30
112
  const processDelay = clConfig.processDelayMs !== undefined ? clConfig.processDelayMs : 500;
113
+ // Default to 'info' log level
114
+ const logLevel = clConfig.logLevel || 'info';
115
+ const logger = createLogger(logLevel);
31
116
 
32
117
  const logConversation = async (payload: {
33
118
  userId: string;
@@ -48,7 +133,7 @@ export function createCognitiveLayer(config: {
48
133
  }),
49
134
  });
50
135
  } catch (e) {
51
- console.error("CognitiveLayer: Log failed", e);
136
+ logger.error("Log failed", e);
52
137
  }
53
138
  };
54
139
 
@@ -58,7 +143,7 @@ export function createCognitiveLayer(config: {
58
143
  method: "POST",
59
144
  headers: { "Content-Type": "application/json" },
60
145
  body: JSON.stringify({ userId, agentId, sessionId }),
61
- }).catch(e => console.error("CognitiveLayer: Process trigger failed", e));
146
+ }).catch(e => logger.error("Process trigger failed", e));
62
147
  };
63
148
 
64
149
  if (processDelay > 0) {
@@ -110,7 +195,7 @@ export function createCognitiveLayer(config: {
110
195
  const sessionMissing = !!userId && !sessionId;
111
196
 
112
197
  if (sessionMissing) {
113
- console.warn("CognitiveLayer: sessionId is required to log and process memories; skipping logging until provided.");
198
+ logger.warn("sessionId is required to log and process memories; skipping logging until provided.");
114
199
  }
115
200
 
116
201
  return wrapLanguageModel({
@@ -119,43 +204,69 @@ export function createCognitiveLayer(config: {
119
204
  async transformParams({ params }) {
120
205
  if (!userId) return params;
121
206
 
122
- let systemPromptToAdd = "";
123
207
  const incomingMessages = Array.isArray((params as any).prompt)
124
208
  ? (params as any).prompt
125
209
  : [];
126
210
 
211
+ // 1) Check if memory is already injected in messages
212
+ if (hasExistingMemoryInjection(incomingMessages)) {
213
+ logger.debug("Memory already injected, skipping");
214
+ return params;
215
+ }
216
+
217
+ // 2) Check session cache
218
+ const sessionKey = `${userId}:${agentId}:${sessionId || "default"}`;
219
+ let systemPromptToAdd = sessionSnapshots.get(sessionKey);
127
220
 
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 !== "" ? `
221
+ // 3) Fetch snapshot only if not cached
222
+ if (systemPromptToAdd === undefined) {
223
+ try {
224
+ const url = `${baseUrl}/api/cognitive/snapshot?userId=${userId}&agentId=${agentId}&appId=${clConfig.appId}`;
225
+ const res = await fetch(url);
226
+ if (res.ok) {
227
+ const data = await res.json();
228
+ const systemBlock = data.systemBlock || "";
229
+ const userContextBlock = data.userContextBlock || "";
230
+ systemPromptToAdd =
231
+ systemBlock !== "" || userContextBlock !== ""
232
+ ? `
138
233
  <MemoryContext>
139
234
  Use the following memory to stay consistent. Prefer UserContext facts for answers; AgentHeuristics guide style, safety, and priorities.
140
- ${agentHeuristics}
141
- ${userContext}
235
+ ${systemBlock || "None"}
236
+ ${userContextBlock || "None"}
142
237
  </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);
238
+ `.trim()
239
+ : "";
240
+
241
+ // Cache the snapshot for this session
242
+ sessionSnapshots.set(sessionKey, systemPromptToAdd);
243
+
244
+ logger.info("Snapshot fetched and cached", {
245
+ userId,
246
+ agentId,
247
+ sessionId,
248
+ sessionKey,
249
+ systemLen: systemBlock.length,
250
+ userLen: userContextBlock.length,
251
+ });
252
+ // At debug level, log the full snapshot data
253
+ logger.debug("Full snapshot data", {
254
+ systemBlock,
255
+ userContextBlock,
256
+ rawData: data,
257
+ });
258
+ } else {
259
+ logger.warn("Snapshot fetch failed", { status: res.status });
260
+ systemPromptToAdd = "";
261
+ sessionSnapshots.set(sessionKey, systemPromptToAdd);
262
+ }
263
+ } catch (e) {
264
+ logger.warn("Failed to fetch snapshot", e);
265
+ systemPromptToAdd = "";
266
+ sessionSnapshots.set(sessionKey, systemPromptToAdd);
156
267
  }
157
- } catch (e) {
158
- console.warn("CognitiveLayer: Failed to fetch snapshot", e);
268
+ } else {
269
+ logger.debug("Using cached snapshot for session", { sessionKey });
159
270
  }
160
271
 
161
272
  if (!systemPromptToAdd) {
@@ -168,14 +279,11 @@ ${userContext}
168
279
  systemPromptToAdd
169
280
  );
170
281
 
171
-
172
-
173
- console.log("CL: injecting memory system prompt", {
174
- systemPromptToAdd
282
+ logger.info("Injecting memory system prompt", {
283
+ sessionKey,
284
+ promptLength: systemPromptToAdd.length,
175
285
  });
176
-
177
- console.log("CL: messagesWithMemory", messagesWithMemory);
178
- console.log("CL: nextParams", nextParams);
286
+ logger.debug("Injected prompt content", { systemPromptToAdd });
179
287
 
180
288
  return { ...nextParams, prompt: messagesWithMemory };
181
289
  },