@kognitivedev/vercel-ai-provider 0.1.1 → 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.
package/README.md CHANGED
@@ -85,7 +85,8 @@ type CLModelWrapper = (
85
85
  userId?: string;
86
86
  agentId?: string;
87
87
  sessionId?: string;
88
- }
88
+ },
89
+ providerOptions?: Record<string, unknown>
89
90
  ) => LanguageModelV2;
90
91
  ```
91
92
 
@@ -97,6 +98,7 @@ type CLModelWrapper = (
97
98
  | `settings.userId` | `string` | - | User identifier (required for memory features) |
98
99
  | `settings.agentId` | `string` | - | Override default agent ID |
99
100
  | `settings.sessionId` | `string` | - | Session identifier (required for logging) |
101
+ | `providerOptions` | `Record<string, unknown>` | - | Provider-specific options passed directly to the underlying provider |
100
102
 
101
103
  ## Usage Examples
102
104
 
@@ -124,6 +126,41 @@ const { text } = await generateText({
124
126
  });
125
127
  ```
126
128
 
129
+ ### With OpenRouter (Provider Options)
130
+
131
+ Pass provider-specific options as the third parameter:
132
+
133
+ ```typescript
134
+ import { createCognitiveLayer } from "@kognitivedev/vercel-ai-provider";
135
+ import { createOpenRouter } from "@openrouter/ai-sdk-provider";
136
+ import { generateText } from "ai";
137
+
138
+ const openrouter = createOpenRouter({
139
+ apiKey: process.env.OPENROUTER_API_KEY
140
+ });
141
+
142
+ const clModel = createCognitiveLayer({
143
+ provider: openrouter.chat,
144
+ clConfig: {
145
+ appId: "my-app",
146
+ baseUrl: "https://api.kognitive.dev"
147
+ }
148
+ });
149
+
150
+ // Pass provider-specific options as the third parameter
151
+ const { text } = await generateText({
152
+ model: clModel("moonshotai/kimi-k2-0905", {
153
+ userId: "user-123",
154
+ sessionId: "session-abc"
155
+ }, {
156
+ provider: {
157
+ only: ["openai"]
158
+ }
159
+ }),
160
+ prompt: "What's the weather like?"
161
+ });
162
+ ```
163
+
127
164
  ### With Anthropic
128
165
 
129
166
  ```typescript
package/dist/index.d.ts CHANGED
@@ -15,7 +15,7 @@ export type CLModelWrapper = (modelId: string, settings?: {
15
15
  userId?: string;
16
16
  agentId?: string;
17
17
  sessionId?: string;
18
- }) => LanguageModelV2;
18
+ }, providerOptions?: Record<string, unknown>) => LanguageModelV2;
19
19
  export declare function createCognitiveLayer(config: {
20
20
  provider: any;
21
21
  clConfig: CognitiveLayerConfig;
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";
@@ -52,8 +72,11 @@ function createCognitiveLayer(config) {
52
72
  const updated = [{ role: "system", content: memoryPrompt }, ...incomingMessages];
53
73
  return { nextParams, messages: updated, mode: "prepend-system" };
54
74
  };
55
- return (modelId, settings) => {
56
- const model = provider(modelId);
75
+ return (modelId, settings, providerOptions) => {
76
+ // Pass provider options through to the underlying provider
77
+ const model = (providerOptions
78
+ ? provider(modelId, providerOptions)
79
+ : provider(modelId));
57
80
  const userId = settings === null || settings === void 0 ? void 0 : settings.userId;
58
81
  const agentId = (settings === null || settings === void 0 ? void 0 : settings.agentId) || clConfig.defaultAgentId || "default";
59
82
  const sessionId = settings === null || settings === void 0 ? void 0 : settings.sessionId;
@@ -67,52 +90,70 @@ function createCognitiveLayer(config) {
67
90
  async transformParams({ params }) {
68
91
  if (!userId)
69
92
  return params;
70
- let systemPromptToAdd = "";
71
93
  const incomingMessages = Array.isArray(params.prompt)
72
94
  ? params.prompt
73
95
  : [];
74
- try {
75
- const url = `${baseUrl}/api/cognitive/snapshot?userId=${userId}&agentId=${agentId}&appId=${clConfig.appId}`;
76
- const res = await fetch(url);
77
- if (res.ok) {
78
- const data = await res.json();
79
- const systemBlock = data.systemBlock || "";
80
- const userContextBlock = data.userContextBlock || "";
81
- const agentHeuristics = systemBlock || "None";
82
- const userContext = userContextBlock || "None";
83
- 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
+ ? `
84
116
  <MemoryContext>
85
117
  Use the following memory to stay consistent. Prefer UserContext facts for answers; AgentHeuristics guide style, safety, and priorities.
86
- ${agentHeuristics}
87
- ${userContext}
118
+ ${systemBlock || "None"}
119
+ ${userContextBlock || "None"}
88
120
  </MemoryContext>
89
- `.trim() : "";
90
- console.log("CL: snapshot fetched", {
91
- userId,
92
- agentId,
93
- sessionId,
94
- systemLen: systemBlock.length,
95
- userLen: userContextBlock.length,
96
- hasSystem: !!systemBlock,
97
- hasUserContext: !!userContextBlock,
98
- });
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
+ }
99
139
  }
100
- else {
101
- 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);
102
144
  }
103
145
  }
104
- catch (e) {
105
- console.warn("CognitiveLayer: Failed to fetch snapshot", e);
146
+ else {
147
+ console.log("CL: using cached snapshot for session", { sessionKey });
106
148
  }
107
149
  if (!systemPromptToAdd) {
108
150
  return Object.assign(Object.assign({}, params), { messages: incomingMessages });
109
151
  }
110
152
  const { nextParams, messages: messagesWithMemory } = withMemorySystemPrompt(params, incomingMessages, systemPromptToAdd);
111
153
  console.log("CL: injecting memory system prompt", {
112
- systemPromptToAdd
154
+ sessionKey,
155
+ promptLength: systemPromptToAdd.length,
113
156
  });
114
- console.log("CL: messagesWithMemory", messagesWithMemory);
115
- console.log("CL: nextParams", nextParams);
116
157
  return Object.assign(Object.assign({}, nextParams), { prompt: messagesWithMemory });
117
158
  },
118
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.1",
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
@@ -16,9 +16,34 @@ export interface CognitiveLayerConfig {
16
16
 
17
17
  export type CLModelWrapper = (
18
18
  modelId: string,
19
- settings?: { userId?: string; agentId?: string; sessionId?: string }
19
+ settings?: { userId?: string; agentId?: string; sessionId?: string },
20
+ providerOptions?: Record<string, unknown>
20
21
  ) => LanguageModelV2;
21
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
+
22
47
  export function createCognitiveLayer(config: {
23
48
  provider: any;
24
49
  clConfig: CognitiveLayerConfig;
@@ -92,8 +117,17 @@ export function createCognitiveLayer(config: {
92
117
  return { nextParams, messages: updated, mode: "prepend-system" };
93
118
  };
94
119
 
95
- return (modelId: string, settings?: { userId?: string; agentId?: string; sessionId?: string }) => {
96
- const model = provider(modelId) as LanguageModelV2;
120
+ return (
121
+ modelId: string,
122
+ settings?: { userId?: string; agentId?: string; sessionId?: string },
123
+ providerOptions?: Record<string, unknown>
124
+ ) => {
125
+ // Pass provider options through to the underlying provider
126
+ const model = (
127
+ providerOptions
128
+ ? provider(modelId, providerOptions)
129
+ : provider(modelId)
130
+ ) as LanguageModelV2;
97
131
  const userId = settings?.userId;
98
132
  const agentId = settings?.agentId || clConfig.defaultAgentId || "default";
99
133
  const sessionId = settings?.sessionId;
@@ -109,43 +143,63 @@ export function createCognitiveLayer(config: {
109
143
  async transformParams({ params }) {
110
144
  if (!userId) return params;
111
145
 
112
- let systemPromptToAdd = "";
113
146
  const incomingMessages = Array.isArray((params as any).prompt)
114
147
  ? (params as any).prompt
115
148
  : [];
116
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);
117
159
 
118
- try {
119
- const url = `${baseUrl}/api/cognitive/snapshot?userId=${userId}&agentId=${agentId}&appId=${clConfig.appId}`;
120
- const res = await fetch(url);
121
- if (res.ok) {
122
- const data = await res.json();
123
- const systemBlock = data.systemBlock || "";
124
- const userContextBlock = data.userContextBlock || "";
125
- const agentHeuristics = systemBlock || "None";
126
- const userContext = userContextBlock || "None";
127
- 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
+ ? `
128
172
  <MemoryContext>
129
173
  Use the following memory to stay consistent. Prefer UserContext facts for answers; AgentHeuristics guide style, safety, and priorities.
130
- ${agentHeuristics}
131
- ${userContext}
174
+ ${systemBlock || "None"}
175
+ ${userContextBlock || "None"}
132
176
  </MemoryContext>
133
- `.trim() : "";
134
-
135
- console.log("CL: snapshot fetched", {
136
- userId,
137
- agentId,
138
- sessionId,
139
- systemLen: systemBlock.length,
140
- userLen: userContextBlock.length,
141
- hasSystem: !!systemBlock,
142
- hasUserContext: !!userContextBlock,
143
- });
144
- } else {
145
- 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);
146
200
  }
147
- } catch (e) {
148
- console.warn("CognitiveLayer: Failed to fetch snapshot", e);
201
+ } else {
202
+ console.log("CL: using cached snapshot for session", { sessionKey });
149
203
  }
150
204
 
151
205
  if (!systemPromptToAdd) {
@@ -158,18 +212,14 @@ ${userContext}
158
212
  systemPromptToAdd
159
213
  );
160
214
 
161
-
162
-
163
215
  console.log("CL: injecting memory system prompt", {
164
- systemPromptToAdd
216
+ sessionKey,
217
+ promptLength: systemPromptToAdd.length,
165
218
  });
166
219
 
167
- console.log("CL: messagesWithMemory", messagesWithMemory);
168
- console.log("CL: nextParams", nextParams);
169
-
170
220
  return { ...nextParams, prompt: messagesWithMemory };
171
221
  },
172
-
222
+
173
223
  async wrapGenerate({ doGenerate, params }) {
174
224
  const result = await doGenerate();
175
225