@kognitivedev/vercel-ai-provider 0.1.3 → 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 +19 -0
- package/dist/index.js +54 -9
- package/package.json +1 -1
- package/src/index.ts +77 -9
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,6 +2,41 @@
|
|
|
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
|
+
}
|
|
5
40
|
// Session-scoped snapshot cache: sessionKey → formatted memory block
|
|
6
41
|
const sessionSnapshots = new Map();
|
|
7
42
|
// Regex to detect if memory has already been injected
|
|
@@ -27,6 +62,9 @@ function createCognitiveLayer(config) {
|
|
|
27
62
|
const baseUrl = clConfig.baseUrl || "http://localhost:3001";
|
|
28
63
|
// Default to 500ms delay to allow DB writes to settle
|
|
29
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);
|
|
30
68
|
const logConversation = async (payload) => {
|
|
31
69
|
try {
|
|
32
70
|
await fetch(`${baseUrl}/api/cognitive/log`, {
|
|
@@ -36,7 +74,7 @@ function createCognitiveLayer(config) {
|
|
|
36
74
|
});
|
|
37
75
|
}
|
|
38
76
|
catch (e) {
|
|
39
|
-
|
|
77
|
+
logger.error("Log failed", e);
|
|
40
78
|
}
|
|
41
79
|
};
|
|
42
80
|
const triggerProcessing = (userId, agentId, sessionId) => {
|
|
@@ -45,7 +83,7 @@ function createCognitiveLayer(config) {
|
|
|
45
83
|
method: "POST",
|
|
46
84
|
headers: { "Content-Type": "application/json" },
|
|
47
85
|
body: JSON.stringify({ userId, agentId, sessionId }),
|
|
48
|
-
}).catch(e =>
|
|
86
|
+
}).catch(e => logger.error("Process trigger failed", e));
|
|
49
87
|
};
|
|
50
88
|
if (processDelay > 0) {
|
|
51
89
|
setTimeout(run, processDelay);
|
|
@@ -82,7 +120,7 @@ function createCognitiveLayer(config) {
|
|
|
82
120
|
const sessionId = settings === null || settings === void 0 ? void 0 : settings.sessionId;
|
|
83
121
|
const sessionMissing = !!userId && !sessionId;
|
|
84
122
|
if (sessionMissing) {
|
|
85
|
-
|
|
123
|
+
logger.warn("sessionId is required to log and process memories; skipping logging until provided.");
|
|
86
124
|
}
|
|
87
125
|
return (0, ai_1.wrapLanguageModel)({
|
|
88
126
|
model,
|
|
@@ -95,7 +133,7 @@ function createCognitiveLayer(config) {
|
|
|
95
133
|
: [];
|
|
96
134
|
// 1) Check if memory is already injected in messages
|
|
97
135
|
if (hasExistingMemoryInjection(incomingMessages)) {
|
|
98
|
-
|
|
136
|
+
logger.debug("Memory already injected, skipping");
|
|
99
137
|
return params;
|
|
100
138
|
}
|
|
101
139
|
// 2) Check session cache
|
|
@@ -122,7 +160,7 @@ ${userContextBlock || "None"}
|
|
|
122
160
|
: "";
|
|
123
161
|
// Cache the snapshot for this session
|
|
124
162
|
sessionSnapshots.set(sessionKey, systemPromptToAdd);
|
|
125
|
-
|
|
163
|
+
logger.info("Snapshot fetched and cached", {
|
|
126
164
|
userId,
|
|
127
165
|
agentId,
|
|
128
166
|
sessionId,
|
|
@@ -130,30 +168,37 @@ ${userContextBlock || "None"}
|
|
|
130
168
|
systemLen: systemBlock.length,
|
|
131
169
|
userLen: userContextBlock.length,
|
|
132
170
|
});
|
|
171
|
+
// At debug level, log the full snapshot data
|
|
172
|
+
logger.debug("Full snapshot data", {
|
|
173
|
+
systemBlock,
|
|
174
|
+
userContextBlock,
|
|
175
|
+
rawData: data,
|
|
176
|
+
});
|
|
133
177
|
}
|
|
134
178
|
else {
|
|
135
|
-
|
|
179
|
+
logger.warn("Snapshot fetch failed", { status: res.status });
|
|
136
180
|
systemPromptToAdd = "";
|
|
137
181
|
sessionSnapshots.set(sessionKey, systemPromptToAdd);
|
|
138
182
|
}
|
|
139
183
|
}
|
|
140
184
|
catch (e) {
|
|
141
|
-
|
|
185
|
+
logger.warn("Failed to fetch snapshot", e);
|
|
142
186
|
systemPromptToAdd = "";
|
|
143
187
|
sessionSnapshots.set(sessionKey, systemPromptToAdd);
|
|
144
188
|
}
|
|
145
189
|
}
|
|
146
190
|
else {
|
|
147
|
-
|
|
191
|
+
logger.debug("Using cached snapshot for session", { sessionKey });
|
|
148
192
|
}
|
|
149
193
|
if (!systemPromptToAdd) {
|
|
150
194
|
return Object.assign(Object.assign({}, params), { messages: incomingMessages });
|
|
151
195
|
}
|
|
152
196
|
const { nextParams, messages: messagesWithMemory } = withMemorySystemPrompt(params, incomingMessages, systemPromptToAdd);
|
|
153
|
-
|
|
197
|
+
logger.info("Injecting memory system prompt", {
|
|
154
198
|
sessionKey,
|
|
155
199
|
promptLength: systemPromptToAdd.length,
|
|
156
200
|
});
|
|
201
|
+
logger.debug("Injected prompt content", { systemPromptToAdd });
|
|
157
202
|
return Object.assign(Object.assign({}, nextParams), { prompt: messagesWithMemory });
|
|
158
203
|
},
|
|
159
204
|
async wrapGenerate({ doGenerate, params }) {
|
package/package.json
CHANGED
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 = (
|
|
@@ -52,6 +110,9 @@ export function createCognitiveLayer(config: {
|
|
|
52
110
|
const baseUrl = clConfig.baseUrl || "http://localhost:3001";
|
|
53
111
|
// Default to 500ms delay to allow DB writes to settle
|
|
54
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);
|
|
55
116
|
|
|
56
117
|
const logConversation = async (payload: {
|
|
57
118
|
userId: string;
|
|
@@ -72,7 +133,7 @@ export function createCognitiveLayer(config: {
|
|
|
72
133
|
}),
|
|
73
134
|
});
|
|
74
135
|
} catch (e) {
|
|
75
|
-
|
|
136
|
+
logger.error("Log failed", e);
|
|
76
137
|
}
|
|
77
138
|
};
|
|
78
139
|
|
|
@@ -82,7 +143,7 @@ export function createCognitiveLayer(config: {
|
|
|
82
143
|
method: "POST",
|
|
83
144
|
headers: { "Content-Type": "application/json" },
|
|
84
145
|
body: JSON.stringify({ userId, agentId, sessionId }),
|
|
85
|
-
}).catch(e =>
|
|
146
|
+
}).catch(e => logger.error("Process trigger failed", e));
|
|
86
147
|
};
|
|
87
148
|
|
|
88
149
|
if (processDelay > 0) {
|
|
@@ -134,7 +195,7 @@ export function createCognitiveLayer(config: {
|
|
|
134
195
|
const sessionMissing = !!userId && !sessionId;
|
|
135
196
|
|
|
136
197
|
if (sessionMissing) {
|
|
137
|
-
|
|
198
|
+
logger.warn("sessionId is required to log and process memories; skipping logging until provided.");
|
|
138
199
|
}
|
|
139
200
|
|
|
140
201
|
return wrapLanguageModel({
|
|
@@ -149,7 +210,7 @@ export function createCognitiveLayer(config: {
|
|
|
149
210
|
|
|
150
211
|
// 1) Check if memory is already injected in messages
|
|
151
212
|
if (hasExistingMemoryInjection(incomingMessages)) {
|
|
152
|
-
|
|
213
|
+
logger.debug("Memory already injected, skipping");
|
|
153
214
|
return params;
|
|
154
215
|
}
|
|
155
216
|
|
|
@@ -180,7 +241,7 @@ ${userContextBlock || "None"}
|
|
|
180
241
|
// Cache the snapshot for this session
|
|
181
242
|
sessionSnapshots.set(sessionKey, systemPromptToAdd);
|
|
182
243
|
|
|
183
|
-
|
|
244
|
+
logger.info("Snapshot fetched and cached", {
|
|
184
245
|
userId,
|
|
185
246
|
agentId,
|
|
186
247
|
sessionId,
|
|
@@ -188,18 +249,24 @@ ${userContextBlock || "None"}
|
|
|
188
249
|
systemLen: systemBlock.length,
|
|
189
250
|
userLen: userContextBlock.length,
|
|
190
251
|
});
|
|
252
|
+
// At debug level, log the full snapshot data
|
|
253
|
+
logger.debug("Full snapshot data", {
|
|
254
|
+
systemBlock,
|
|
255
|
+
userContextBlock,
|
|
256
|
+
rawData: data,
|
|
257
|
+
});
|
|
191
258
|
} else {
|
|
192
|
-
|
|
259
|
+
logger.warn("Snapshot fetch failed", { status: res.status });
|
|
193
260
|
systemPromptToAdd = "";
|
|
194
261
|
sessionSnapshots.set(sessionKey, systemPromptToAdd);
|
|
195
262
|
}
|
|
196
263
|
} catch (e) {
|
|
197
|
-
|
|
264
|
+
logger.warn("Failed to fetch snapshot", e);
|
|
198
265
|
systemPromptToAdd = "";
|
|
199
266
|
sessionSnapshots.set(sessionKey, systemPromptToAdd);
|
|
200
267
|
}
|
|
201
268
|
} else {
|
|
202
|
-
|
|
269
|
+
logger.debug("Using cached snapshot for session", { sessionKey });
|
|
203
270
|
}
|
|
204
271
|
|
|
205
272
|
if (!systemPromptToAdd) {
|
|
@@ -212,10 +279,11 @@ ${userContextBlock || "None"}
|
|
|
212
279
|
systemPromptToAdd
|
|
213
280
|
);
|
|
214
281
|
|
|
215
|
-
|
|
282
|
+
logger.info("Injecting memory system prompt", {
|
|
216
283
|
sessionKey,
|
|
217
284
|
promptLength: systemPromptToAdd.length,
|
|
218
285
|
});
|
|
286
|
+
logger.debug("Injected prompt content", { systemPromptToAdd });
|
|
219
287
|
|
|
220
288
|
return { ...nextParams, prompt: messagesWithMemory };
|
|
221
289
|
},
|