@kognitivedev/vercel-ai-provider 0.1.3 → 0.1.5
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 +58 -10
- package/package.json +1 -1
- package/src/index.ts +80 -10
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);
|
|
@@ -65,7 +103,10 @@ function createCognitiveLayer(config) {
|
|
|
65
103
|
// 2) If first message is system, replace its content.
|
|
66
104
|
if (incomingMessages.length > 0 && ((_a = incomingMessages[0]) === null || _a === void 0 ? void 0 : _a.role) === "system") {
|
|
67
105
|
const updated = [...incomingMessages];
|
|
68
|
-
|
|
106
|
+
let systemMessage = updated[0];
|
|
107
|
+
if (typeof systemMessage.content === "string")
|
|
108
|
+
systemMessage.content = systemMessage + "\n\n" + memoryPrompt;
|
|
109
|
+
updated[0] = Object.assign(Object.assign({}, updated[0]), systemMessage);
|
|
69
110
|
return { nextParams, messages: updated, mode: "overwrite-first-system" };
|
|
70
111
|
}
|
|
71
112
|
// 3) Otherwise prepend a system message.
|
|
@@ -82,7 +123,7 @@ function createCognitiveLayer(config) {
|
|
|
82
123
|
const sessionId = settings === null || settings === void 0 ? void 0 : settings.sessionId;
|
|
83
124
|
const sessionMissing = !!userId && !sessionId;
|
|
84
125
|
if (sessionMissing) {
|
|
85
|
-
|
|
126
|
+
logger.warn("sessionId is required to log and process memories; skipping logging until provided.");
|
|
86
127
|
}
|
|
87
128
|
return (0, ai_1.wrapLanguageModel)({
|
|
88
129
|
model,
|
|
@@ -95,7 +136,7 @@ function createCognitiveLayer(config) {
|
|
|
95
136
|
: [];
|
|
96
137
|
// 1) Check if memory is already injected in messages
|
|
97
138
|
if (hasExistingMemoryInjection(incomingMessages)) {
|
|
98
|
-
|
|
139
|
+
logger.debug("Memory already injected, skipping");
|
|
99
140
|
return params;
|
|
100
141
|
}
|
|
101
142
|
// 2) Check session cache
|
|
@@ -122,7 +163,7 @@ ${userContextBlock || "None"}
|
|
|
122
163
|
: "";
|
|
123
164
|
// Cache the snapshot for this session
|
|
124
165
|
sessionSnapshots.set(sessionKey, systemPromptToAdd);
|
|
125
|
-
|
|
166
|
+
logger.info("Snapshot fetched and cached", {
|
|
126
167
|
userId,
|
|
127
168
|
agentId,
|
|
128
169
|
sessionId,
|
|
@@ -130,30 +171,37 @@ ${userContextBlock || "None"}
|
|
|
130
171
|
systemLen: systemBlock.length,
|
|
131
172
|
userLen: userContextBlock.length,
|
|
132
173
|
});
|
|
174
|
+
// At debug level, log the full snapshot data
|
|
175
|
+
logger.debug("Full snapshot data", {
|
|
176
|
+
systemBlock,
|
|
177
|
+
userContextBlock,
|
|
178
|
+
rawData: data,
|
|
179
|
+
});
|
|
133
180
|
}
|
|
134
181
|
else {
|
|
135
|
-
|
|
182
|
+
logger.warn("Snapshot fetch failed", { status: res.status });
|
|
136
183
|
systemPromptToAdd = "";
|
|
137
184
|
sessionSnapshots.set(sessionKey, systemPromptToAdd);
|
|
138
185
|
}
|
|
139
186
|
}
|
|
140
187
|
catch (e) {
|
|
141
|
-
|
|
188
|
+
logger.warn("Failed to fetch snapshot", e);
|
|
142
189
|
systemPromptToAdd = "";
|
|
143
190
|
sessionSnapshots.set(sessionKey, systemPromptToAdd);
|
|
144
191
|
}
|
|
145
192
|
}
|
|
146
193
|
else {
|
|
147
|
-
|
|
194
|
+
logger.debug("Using cached snapshot for session", { sessionKey });
|
|
148
195
|
}
|
|
149
196
|
if (!systemPromptToAdd) {
|
|
150
197
|
return Object.assign(Object.assign({}, params), { messages: incomingMessages });
|
|
151
198
|
}
|
|
152
199
|
const { nextParams, messages: messagesWithMemory } = withMemorySystemPrompt(params, incomingMessages, systemPromptToAdd);
|
|
153
|
-
|
|
200
|
+
logger.info("Injecting memory system prompt", {
|
|
154
201
|
sessionKey,
|
|
155
202
|
promptLength: systemPromptToAdd.length,
|
|
156
203
|
});
|
|
204
|
+
logger.debug("Injected prompt content", { systemPromptToAdd });
|
|
157
205
|
return Object.assign(Object.assign({}, nextParams), { prompt: messagesWithMemory });
|
|
158
206
|
},
|
|
159
207
|
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) {
|
|
@@ -108,7 +169,9 @@ export function createCognitiveLayer(config: {
|
|
|
108
169
|
// 2) If first message is system, replace its content.
|
|
109
170
|
if (incomingMessages.length > 0 && incomingMessages[0]?.role === "system") {
|
|
110
171
|
const updated = [...incomingMessages];
|
|
111
|
-
|
|
172
|
+
let systemMessage = updated[0];
|
|
173
|
+
if(typeof systemMessage.content === "string") systemMessage.content = systemMessage + "\n\n" + memoryPrompt;
|
|
174
|
+
updated[0] = { ...updated[0], ...systemMessage };
|
|
112
175
|
return { nextParams, messages: updated, mode: "overwrite-first-system" };
|
|
113
176
|
}
|
|
114
177
|
|
|
@@ -134,7 +197,7 @@ export function createCognitiveLayer(config: {
|
|
|
134
197
|
const sessionMissing = !!userId && !sessionId;
|
|
135
198
|
|
|
136
199
|
if (sessionMissing) {
|
|
137
|
-
|
|
200
|
+
logger.warn("sessionId is required to log and process memories; skipping logging until provided.");
|
|
138
201
|
}
|
|
139
202
|
|
|
140
203
|
return wrapLanguageModel({
|
|
@@ -149,7 +212,7 @@ export function createCognitiveLayer(config: {
|
|
|
149
212
|
|
|
150
213
|
// 1) Check if memory is already injected in messages
|
|
151
214
|
if (hasExistingMemoryInjection(incomingMessages)) {
|
|
152
|
-
|
|
215
|
+
logger.debug("Memory already injected, skipping");
|
|
153
216
|
return params;
|
|
154
217
|
}
|
|
155
218
|
|
|
@@ -180,7 +243,7 @@ ${userContextBlock || "None"}
|
|
|
180
243
|
// Cache the snapshot for this session
|
|
181
244
|
sessionSnapshots.set(sessionKey, systemPromptToAdd);
|
|
182
245
|
|
|
183
|
-
|
|
246
|
+
logger.info("Snapshot fetched and cached", {
|
|
184
247
|
userId,
|
|
185
248
|
agentId,
|
|
186
249
|
sessionId,
|
|
@@ -188,18 +251,24 @@ ${userContextBlock || "None"}
|
|
|
188
251
|
systemLen: systemBlock.length,
|
|
189
252
|
userLen: userContextBlock.length,
|
|
190
253
|
});
|
|
254
|
+
// At debug level, log the full snapshot data
|
|
255
|
+
logger.debug("Full snapshot data", {
|
|
256
|
+
systemBlock,
|
|
257
|
+
userContextBlock,
|
|
258
|
+
rawData: data,
|
|
259
|
+
});
|
|
191
260
|
} else {
|
|
192
|
-
|
|
261
|
+
logger.warn("Snapshot fetch failed", { status: res.status });
|
|
193
262
|
systemPromptToAdd = "";
|
|
194
263
|
sessionSnapshots.set(sessionKey, systemPromptToAdd);
|
|
195
264
|
}
|
|
196
265
|
} catch (e) {
|
|
197
|
-
|
|
266
|
+
logger.warn("Failed to fetch snapshot", e);
|
|
198
267
|
systemPromptToAdd = "";
|
|
199
268
|
sessionSnapshots.set(sessionKey, systemPromptToAdd);
|
|
200
269
|
}
|
|
201
270
|
} else {
|
|
202
|
-
|
|
271
|
+
logger.debug("Using cached snapshot for session", { sessionKey });
|
|
203
272
|
}
|
|
204
273
|
|
|
205
274
|
if (!systemPromptToAdd) {
|
|
@@ -212,10 +281,11 @@ ${userContextBlock || "None"}
|
|
|
212
281
|
systemPromptToAdd
|
|
213
282
|
);
|
|
214
283
|
|
|
215
|
-
|
|
284
|
+
logger.info("Injecting memory system prompt", {
|
|
216
285
|
sessionKey,
|
|
217
286
|
promptLength: systemPromptToAdd.length,
|
|
218
287
|
});
|
|
288
|
+
logger.debug("Injected prompt content", { systemPromptToAdd });
|
|
219
289
|
|
|
220
290
|
return { ...nextParams, prompt: messagesWithMemory };
|
|
221
291
|
},
|