@kognitivedev/vercel-ai-provider 0.1.6 → 0.1.7

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
@@ -55,7 +55,7 @@ const { text } = await generateText({
55
55
  | `apiKey` | `string` | - | - | API key for authentication (if required) |
56
56
  | `processDelayMs` | `number` | - | `500` | Delay before triggering memory processing (set to 0 to disable) |
57
57
  | `logLevel` | `LogLevel` | - | `'info'` | Controls console logging verbosity |
58
- | `providerFactory` | `(baseURL: string) => (modelId: string) => LanguageModel` | - | - | Factory for creating a provider that routes through a gateway URL |
58
+ | `providerFactory` | `(baseURL: string, apiKey: string) => (modelId: string) => LanguageModel` | - | - | Factory for creating a provider that routes through a gateway URL |
59
59
 
60
60
  ### `LogLevel`
61
61
 
@@ -317,8 +317,8 @@ const cl = createCognitiveLayer({
317
317
  clConfig: {
318
318
  appId: "my-app",
319
319
  baseUrl: "https://api.kognitive.dev",
320
- providerFactory: (baseURL) => {
321
- const gatewayProvider = createOpenAI({ baseURL });
320
+ providerFactory: (baseURL, apiKey) => {
321
+ const gatewayProvider = createOpenAI({ baseURL, apiKey });
322
322
  return (modelId) => gatewayProvider(modelId);
323
323
  }
324
324
  }
package/dist/index.d.ts CHANGED
@@ -27,7 +27,7 @@ export interface CognitiveLayerConfig {
27
27
  /**
28
28
  * Factory for creating a provider that routes through a gateway URL.
29
29
  */
30
- providerFactory?: (baseURL: string) => (modelId: string) => LanguageModel;
30
+ providerFactory?: (baseURL: string, apiKey: string) => (modelId: string) => LanguageModel;
31
31
  }
32
32
  export type CLModelWrapper = (modelId: string, settings?: {
33
33
  userId?: string;
package/dist/index.js CHANGED
@@ -13,6 +13,12 @@ var __rest = (this && this.__rest) || function (s, e) {
13
13
  Object.defineProperty(exports, "__esModule", { value: true });
14
14
  exports.createCognitiveLayer = createCognitiveLayer;
15
15
  const ai_1 = require("ai");
16
+ function isValidId(value) {
17
+ if (value == null || typeof value !== "string")
18
+ return false;
19
+ const trimmed = value.trim();
20
+ return trimmed !== "" && trimmed !== "null" && trimmed !== "undefined";
21
+ }
16
22
  const LOG_LEVEL_PRIORITY = {
17
23
  none: 0,
18
24
  error: 1,
@@ -166,7 +172,7 @@ function createCognitiveLayer(config) {
166
172
  const buildMiddleware = (userId, projectId, sessionId, modelId) => ({
167
173
  specificationVersion: 'v3',
168
174
  async transformParams({ params }) {
169
- if (!userId)
175
+ if (!isValidId(userId))
170
176
  return params;
171
177
  const incomingMessages = Array.isArray(params.prompt)
172
178
  ? params.prompt
@@ -254,7 +260,7 @@ ${userContextBlock || "None"}
254
260
  logger.error("doGenerate params.prompt", JSON.stringify((_a = params.prompt) === null || _a === void 0 ? void 0 : _a.map((m) => ({ role: m.role, contentType: typeof m.content, contentLength: Array.isArray(m.content) ? m.content.length : undefined })), null, 2));
255
261
  throw err;
256
262
  }
257
- if (userId && sessionId) {
263
+ if (isValidId(userId) && isValidId(sessionId)) {
258
264
  const sessionKey = `${userId}:${projectId}:${sessionId}`;
259
265
  const promptMeta = sessionPromptMetadata.get(sessionKey);
260
266
  const messagesInput = params.messages || params.prompt || [];
@@ -288,7 +294,7 @@ ${userContextBlock || "None"}
288
294
  logger.error("doStream failed", err);
289
295
  throw err;
290
296
  }
291
- if (userId && sessionId) {
297
+ if (isValidId(userId) && isValidId(sessionId)) {
292
298
  const sessionKey = `${userId}:${projectId}:${sessionId}`;
293
299
  const promptMeta = sessionPromptMetadata.get(sessionKey);
294
300
  const messagesInput = params.messages || params.prompt || [];
@@ -337,7 +343,7 @@ ${userContextBlock || "None"}
337
343
  try {
338
344
  const gatewayURL = `${baseUrl}/api/cognitive/gateway/${gatewaySlug}`;
339
345
  const modelId = originalModel.modelId || 'default';
340
- const rawModel = clConfig.providerFactory(gatewayURL)(modelId);
346
+ const rawModel = clConfig.providerFactory(gatewayURL, clConfig.apiKey)(modelId);
341
347
  const session = originalModel[SESSION_KEY];
342
348
  if (!session)
343
349
  return rawModel;
@@ -361,7 +367,7 @@ ${userContextBlock || "None"}
361
367
  const userId = settings === null || settings === void 0 ? void 0 : settings.userId;
362
368
  const projectId = (settings === null || settings === void 0 ? void 0 : settings.projectId) || clConfig.projectId || "default";
363
369
  const sessionId = settings === null || settings === void 0 ? void 0 : settings.sessionId;
364
- const sessionMissing = !!userId && !sessionId;
370
+ const sessionMissing = isValidId(userId) && !isValidId(sessionId);
365
371
  if (sessionMissing) {
366
372
  logger.warn("sessionId is required to log and process memories; skipping logging until provided.");
367
373
  }
@@ -370,7 +376,7 @@ ${userContextBlock || "None"}
370
376
  middleware: buildMiddleware(userId, projectId, sessionId, modelId),
371
377
  });
372
378
  // Track session settings on the model for use in cl.streamText/cl.generateText
373
- if (userId && sessionId) {
379
+ if (isValidId(userId) && isValidId(sessionId)) {
374
380
  wrappedModel[SESSION_KEY] = { userId, projectId, sessionId };
375
381
  }
376
382
  return wrappedModel;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kognitivedev/vercel-ai-provider",
3
- "version": "0.1.6",
3
+ "version": "0.1.7",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "publishConfig": {
package/src/index.ts CHANGED
@@ -15,6 +15,12 @@ import {
15
15
  */
16
16
  export type LogLevel = 'none' | 'error' | 'warn' | 'info' | 'debug';
17
17
 
18
+ function isValidId(value: string | undefined | null): value is string {
19
+ if (value == null || typeof value !== "string") return false;
20
+ const trimmed = value.trim();
21
+ return trimmed !== "" && trimmed !== "null" && trimmed !== "undefined";
22
+ }
23
+
18
24
  const LOG_LEVEL_PRIORITY: Record<LogLevel, number> = {
19
25
  none: 0,
20
26
  error: 1,
@@ -72,7 +78,7 @@ export interface CognitiveLayerConfig {
72
78
  /**
73
79
  * Factory for creating a provider that routes through a gateway URL.
74
80
  */
75
- providerFactory?: (baseURL: string) => (modelId: string) => LanguageModel;
81
+ providerFactory?: (baseURL: string, apiKey: string) => (modelId: string) => LanguageModel;
76
82
  }
77
83
 
78
84
  export type CLModelWrapper = (
@@ -275,7 +281,7 @@ export function createCognitiveLayer(config: {
275
281
  const buildMiddleware = (userId: string | undefined, projectId: string, sessionId: string | undefined, modelId: string) => ({
276
282
  specificationVersion: 'v3' as const,
277
283
  async transformParams({ params }: { params: any }) {
278
- if (!userId) return params;
284
+ if (!isValidId(userId)) return params;
279
285
 
280
286
  const incomingMessages = Array.isArray((params as any).prompt)
281
287
  ? (params as any).prompt
@@ -373,7 +379,7 @@ ${userContextBlock || "None"}
373
379
  throw err;
374
380
  }
375
381
 
376
- if (userId && sessionId) {
382
+ if (isValidId(userId) && isValidId(sessionId)) {
377
383
  const sessionKey = `${userId}:${projectId}:${sessionId}`;
378
384
  const promptMeta = sessionPromptMetadata.get(sessionKey);
379
385
 
@@ -416,7 +422,7 @@ ${userContextBlock || "None"}
416
422
  throw err;
417
423
  }
418
424
 
419
- if (userId && sessionId) {
425
+ if (isValidId(userId) && isValidId(sessionId)) {
420
426
  const sessionKey = `${userId}:${projectId}:${sessionId}`;
421
427
  const promptMeta = sessionPromptMetadata.get(sessionKey);
422
428
 
@@ -479,7 +485,7 @@ ${userContextBlock || "None"}
479
485
  try {
480
486
  const gatewayURL = `${baseUrl}/api/cognitive/gateway/${gatewaySlug}`;
481
487
  const modelId = (originalModel as any).modelId || 'default';
482
- const rawModel = clConfig.providerFactory(gatewayURL)(modelId);
488
+ const rawModel = clConfig.providerFactory(gatewayURL, clConfig.apiKey)(modelId);
483
489
 
484
490
  const session = (originalModel as any)[SESSION_KEY];
485
491
  if (!session) return rawModel as LanguageModel;
@@ -510,7 +516,7 @@ ${userContextBlock || "None"}
510
516
  const userId = settings?.userId;
511
517
  const projectId = settings?.projectId || clConfig.projectId || "default";
512
518
  const sessionId = settings?.sessionId;
513
- const sessionMissing = !!userId && !sessionId;
519
+ const sessionMissing = isValidId(userId) && !isValidId(sessionId);
514
520
 
515
521
  if (sessionMissing) {
516
522
  logger.warn("sessionId is required to log and process memories; skipping logging until provided.");
@@ -522,7 +528,7 @@ ${userContextBlock || "None"}
522
528
  }) as LanguageModel;
523
529
 
524
530
  // Track session settings on the model for use in cl.streamText/cl.generateText
525
- if (userId && sessionId) {
531
+ if (isValidId(userId) && isValidId(sessionId)) {
526
532
  (wrappedModel as any)[SESSION_KEY] = { userId, projectId, sessionId };
527
533
  }
528
534