{"_id":"@astralibx/chat-ai","_rev":"5-57825adceac09ddbf76c25409d9b3c55","name":"@astralibx/chat-ai","dist-tags":{"latest":"0.2.1"},"versions":{"0.1.1":{"name":"@astralibx/chat-ai","version":"0.1.1","keywords":["chat","ai","memory","prompt","knowledge","orchestration","handlebars","template"],"license":"MIT","_id":"@astralibx/chat-ai@0.1.1","maintainers":[{"name":"astralib","email":"1997hariprakash@gmail.com"}],"homepage":"https://github.com/Hariprakash1997/astralib#readme","bugs":{"url":"https://github.com/Hariprakash1997/astralib/issues"},"dist":{"shasum":"f6b443be680344958c07c015b9fd0fa6a829cb5f","tarball":"https://registry.npmjs.org/@astralibx/chat-ai/-/chat-ai-0.1.1.tgz","fileCount":8,"integrity":"sha512-dicycM7kI5zgcBTCnRxIhjEJcRLFdF3dIXg/24RWJf/p1sQDzVMc4SeuvLpZqCLzUASTIk03qxBg4+LgAYQ31A==","signatures":[{"sig":"MEUCIHOha830FimGcz+IdRULvO4k1hf6YUrjhfz+ENGbxXbEAiEA6QE0XwMCyUDbdrP4glDPmDjf0XmgKCj2yCBwqWpIK2M=","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"unpackedSize":484422},"main":"dist/index.cjs","types":"dist/index.d.ts","module":"dist/index.mjs","exports":{".":{"import":{"types":"./dist/index.d.mts","default":"./dist/index.mjs"},"require":{"types":"./dist/index.d.ts","default":"./dist/index.cjs"}}},"gitHead":"df956e4b9f30b71fc0ee0eba6b811eee2092bf52","scripts":{"dev":"tsup --watch","lint":"eslint src/","test":"vitest run","build":"tsup","clean":"rm -rf dist","test:watch":"vitest","test:coverage":"vitest run --coverage"},"_npmUser":{"name":"astralib","email":"1997hariprakash@gmail.com"},"repository":{"url":"git+https://github.com/Hariprakash1997/astralib.git","type":"git","directory":"packages/chat/chat-ai"},"_npmVersion":"10.9.4","description":"Optional AI layer for chat-engine: memory management, prompt templates, knowledge base, and AI call orchestration","directories":{},"_nodeVersion":"22.22.1","dependencies":{"zod":"^3.23.0","handlebars":"^4.7.0","@astralibx/core":"^1.2.0","@astralibx/chat-types":"^0.1.1"},"_hasShrinkwrap":false,"devDependencies":{"express":"^5.0.0","mongoose":"^8.12.1","typescript":"^5.8.2","@types/node":"^22.0.0","@types/express":"^5.0.0","@vitest/coverage-v8":"^3.0.0"},"peerDependencies":{"express":"^4.18.0 || ^5.0.0","mongoose":"^7.0.0 || ^8.0.0"},"_npmOperationalInternal":{"tmp":"tmp/chat-ai_0.1.1_1773730922820_0.14579583507650629","host":"s3://npm-registry-packages-npm-production"}},"0.1.2":{"name":"@astralibx/chat-ai","version":"0.1.2","keywords":["chat","ai","memory","prompt","knowledge","orchestration","handlebars","template"],"license":"MIT","_id":"@astralibx/chat-ai@0.1.2","maintainers":[{"name":"astralib","email":"1997hariprakash@gmail.com"}],"homepage":"https://github.com/Hariprakash1997/astralib#readme","bugs":{"url":"https://github.com/Hariprakash1997/astralib/issues"},"dist":{"shasum":"3d4cea1d19b176486d502482ef42150a628abf08","tarball":"https://registry.npmjs.org/@astralibx/chat-ai/-/chat-ai-0.1.2.tgz","fileCount":8,"integrity":"sha512-B1WJh8THJ1nu0bLMkFfo+hZPJmj9HKgC/Ul3i0nfTvPrZG3FEcR+rkMnwtZxNtq8/mHdrbMrSWyU8hGpn/+6Yw==","signatures":[{"sig":"MEUCIB708kD0SyHtqdF5EIZ7thyRp6ubxeM+fWN9OskEehJ2AiEAuG71hKPSNZQ3gC6/DsXxOfDgqNxlcmCivmn5ElEJ14w=","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"unpackedSize":485604},"main":"dist/index.cjs","types":"dist/index.d.ts","module":"dist/index.mjs","exports":{".":{"import":{"types":"./dist/index.d.mts","default":"./dist/index.mjs"},"require":{"types":"./dist/index.d.ts","default":"./dist/index.cjs"}}},"gitHead":"fd42f0db8c9fd08982aee61f94304819cd425d54","scripts":{"dev":"tsup --watch","lint":"eslint src/","test":"vitest run","build":"tsup","clean":"rm -rf dist","test:watch":"vitest","test:coverage":"vitest run --coverage"},"_npmUser":{"name":"astralib","email":"1997hariprakash@gmail.com"},"repository":{"url":"git+https://github.com/Hariprakash1997/astralib.git","type":"git","directory":"packages/chat/chat-ai"},"_npmVersion":"10.9.4","description":"Optional AI layer for chat-engine: memory management, prompt templates, knowledge base, and AI call orchestration","directories":{},"_nodeVersion":"22.22.1","dependencies":{"zod":"^3.23.0","handlebars":"^4.7.0","@astralibx/core":"^1.2.1","@astralibx/chat-types":"^0.1.2"},"_hasShrinkwrap":false,"devDependencies":{"express":"^5.0.0","mongoose":"^8.12.1","typescript":"^5.8.2","@types/node":"^22.0.0","@types/express":"^5.0.0","@vitest/coverage-v8":"^3.0.0"},"peerDependencies":{"express":"^4.18.0 || ^5.0.0","mongoose":"^7.0.0 || ^8.0.0"},"_npmOperationalInternal":{"tmp":"tmp/chat-ai_0.1.2_1773733706961_0.656116848948449","host":"s3://npm-registry-packages-npm-production"}},"0.1.3":{"name":"@astralibx/chat-ai","version":"0.1.3","keywords":["chat","ai","memory","prompt","knowledge","orchestration","handlebars","template"],"license":"MIT","_id":"@astralibx/chat-ai@0.1.3","maintainers":[{"name":"astralib","email":"1997hariprakash@gmail.com"}],"homepage":"https://github.com/Hariprakash1997/astralib#readme","bugs":{"url":"https://github.com/Hariprakash1997/astralib/issues"},"dist":{"shasum":"0feefc4baea44abfe0cf7ace0da054cc644f9df1","tarball":"https://registry.npmjs.org/@astralibx/chat-ai/-/chat-ai-0.1.3.tgz","fileCount":8,"integrity":"sha512-TirdV7kiB8xKOmOFhkAyIxYgeUS8BiD7BWosany1fVOXIIKYu0jL0YVCyF0gn+8VwhgJqNkNs5G+Udo4gMpw3Q==","signatures":[{"sig":"MEYCIQD3EyMcls0sO6dTufFn/q0p9Fk6M33Ey8Dn418k8Z67IgIhANftbbbiSU4qKvUdMoClsCxKCEif+jYc9XzfNwUim22Q","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"unpackedSize":485604},"main":"dist/index.cjs","types":"dist/index.d.ts","module":"dist/index.mjs","exports":{".":{"import":{"types":"./dist/index.d.mts","default":"./dist/index.mjs"},"require":{"types":"./dist/index.d.ts","default":"./dist/index.cjs"}}},"gitHead":"b96fb6e63feccf19591d627ddda0c3d04f165ad2","scripts":{"dev":"tsup --watch","lint":"eslint src/","test":"vitest run","build":"tsup","clean":"rm -rf dist","test:watch":"vitest","test:coverage":"vitest run --coverage"},"_npmUser":{"name":"astralib","email":"1997hariprakash@gmail.com"},"repository":{"url":"git+https://github.com/Hariprakash1997/astralib.git","type":"git","directory":"packages/chat/chat-ai"},"_npmVersion":"10.9.4","description":"Optional AI layer for chat-engine: memory management, prompt templates, knowledge base, and AI call orchestration","directories":{},"_nodeVersion":"22.22.1","dependencies":{"zod":"^3.23.0","handlebars":"^4.7.0","@astralibx/core":"^1.2.1","@astralibx/chat-types":"^0.2.0"},"_hasShrinkwrap":false,"devDependencies":{"express":"^5.0.0","mongoose":"^8.12.1","typescript":"^5.8.2","@types/node":"^22.0.0","@types/express":"^5.0.0","@vitest/coverage-v8":"^3.0.0"},"peerDependencies":{"express":"^4.18.0 || ^5.0.0","mongoose":"^7.0.0 || ^8.0.0"},"_npmOperationalInternal":{"tmp":"tmp/chat-ai_0.1.3_1773814060048_0.18142133905568691","host":"s3://npm-registry-packages-npm-production"}},"0.2.0":{"name":"@astralibx/chat-ai","version":"0.2.0","keywords":["chat","ai","memory","prompt","knowledge","orchestration","handlebars","template"],"license":"MIT","_id":"@astralibx/chat-ai@0.2.0","maintainers":[{"name":"astralib","email":"1997hariprakash@gmail.com"}],"homepage":"https://github.com/Hariprakash1997/astralib#readme","bugs":{"url":"https://github.com/Hariprakash1997/astralib/issues"},"dist":{"shasum":"0f91f75453b5b916e95e324403332f90dd10d656","tarball":"https://registry.npmjs.org/@astralibx/chat-ai/-/chat-ai-0.2.0.tgz","fileCount":8,"integrity":"sha512-ixOvjtvOrX5gq+NY8sV5h/v1Oug8UwaMcVAKtZbXuHYnjbew64K8A/Klw0Y4AueVLiz5ivySs7QvyEZe8coByA==","signatures":[{"sig":"MEQCICr8f9y299R/yzuSLCeEvJ23bqmcgZ/ePuOY/yncSPU4AiBMyb6dXulzstTznsVlB3FrcaIyX1iANpwJbPRBpyStwg==","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"unpackedSize":628158},"main":"dist/index.cjs","types":"dist/index.d.ts","module":"dist/index.mjs","exports":{".":{"import":{"types":"./dist/index.d.mts","default":"./dist/index.mjs"},"require":{"types":"./dist/index.d.ts","default":"./dist/index.cjs"}}},"gitHead":"ae1365ff1004be6b7ef07ac5ca5a5d6d8d9a6fa6","scripts":{"dev":"tsup --watch","lint":"eslint src/","test":"vitest run","build":"tsup","clean":"rm -rf dist","test:watch":"vitest","test:coverage":"vitest run --coverage"},"_npmUser":{"name":"astralib","email":"1997hariprakash@gmail.com"},"repository":{"url":"git+https://github.com/Hariprakash1997/astralib.git","type":"git","directory":"packages/chat/chat-ai"},"_npmVersion":"10.9.4","description":"Optional AI layer for chat-engine: memory management, prompt templates, knowledge base, and AI call orchestration","directories":{},"_nodeVersion":"22.22.1","dependencies":{"zod":"^3.23.0","handlebars":"^4.7.0","@astralibx/core":"^1.2.1","@astralibx/chat-types":"^0.3.0"},"_hasShrinkwrap":false,"devDependencies":{"express":"^5.0.0","mongoose":"^8.12.1","typescript":"^5.8.2","@types/node":"^22.0.0","@types/express":"^5.0.0","@vitest/coverage-v8":"^3.0.0"},"peerDependencies":{"express":"^4.18.0 || ^5.0.0","mongoose":"^7.0.0 || ^8.0.0"},"_npmOperationalInternal":{"tmp":"tmp/chat-ai_0.2.0_1773917001738_0.31944797578157713","host":"s3://npm-registry-packages-npm-production"}},"0.2.1":{"name":"@astralibx/chat-ai","version":"0.2.1","description":"Optional AI layer for chat-engine: memory management, prompt templates, knowledge base, and AI call orchestration","repository":{"type":"git","url":"git+https://github.com/Hariprakash1997/astralib.git","directory":"packages/chat/chat-ai"},"main":"dist/index.cjs","module":"dist/index.mjs","types":"dist/index.d.ts","exports":{".":{"import":{"types":"./dist/index.d.mts","default":"./dist/index.mjs"},"require":{"types":"./dist/index.d.ts","default":"./dist/index.cjs"}}},"scripts":{"build":"tsup","dev":"tsup --watch","test":"vitest run","test:watch":"vitest","test:coverage":"vitest run --coverage","lint":"eslint src/","clean":"rm -rf dist"},"keywords":["chat","ai","memory","prompt","knowledge","orchestration","handlebars","template"],"license":"MIT","dependencies":{"@astralibx/chat-types":"^0.4.0","@astralibx/core":"^1.2.1","handlebars":"^4.7.0","zod":"^3.23.0"},"peerDependencies":{"express":"^4.18.0 || ^5.0.0","mongoose":"^7.0.0 || ^8.0.0"},"devDependencies":{"@types/express":"^5.0.0","@types/node":"^22.0.0","@vitest/coverage-v8":"^3.0.0","express":"^5.0.0","mongoose":"^8.12.1","typescript":"^5.8.2"},"_id":"@astralibx/chat-ai@0.2.1","gitHead":"4d2a55b3968e55f083d4a957eb2319acb1fa5ba3","bugs":{"url":"https://github.com/Hariprakash1997/astralib/issues"},"homepage":"https://github.com/Hariprakash1997/astralib#readme","_nodeVersion":"22.22.1","_npmVersion":"10.9.4","dist":{"integrity":"sha512-yoNj6XI27j3pJUyzyiCIaUjkQx1VW5iPiXDg8u1SsqkCK8glfs8itk5dKVy6hYisGZw8yOqKjujaHF+jg+LBoQ==","shasum":"bfd2d8282f454222c785db3772bc1644c72b0089","tarball":"https://registry.npmjs.org/@astralibx/chat-ai/-/chat-ai-0.2.1.tgz","fileCount":8,"unpackedSize":628158,"signatures":[{"keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U","sig":"MEUCIQCf0pXs8NtOR5Mf1G+niKFXGcV9oVdBAvW8fUnO1mDBGwIgZ3riw/h+8+gQtG0v429UIfMUnhmfbjKRZVmbfnN+2ag="}]},"_npmUser":{"name":"astralib","email":"1997hariprakash@gmail.com"},"directories":{},"maintainers":[{"name":"astralib","email":"1997hariprakash@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/chat-ai_0.2.1_1773998151726_0.14298054299609508"},"_hasShrinkwrap":false}},"time":{"created":"2026-03-17T07:02:02.717Z","modified":"2026-03-20T09:15:52.070Z","0.1.1":"2026-03-17T07:02:02.980Z","0.1.2":"2026-03-17T07:48:27.109Z","0.1.3":"2026-03-18T06:07:40.192Z","0.2.0":"2026-03-19T10:43:21.869Z","0.2.1":"2026-03-20T09:15:51.910Z"},"bugs":{"url":"https://github.com/Hariprakash1997/astralib/issues"},"license":"MIT","homepage":"https://github.com/Hariprakash1997/astralib#readme","keywords":["chat","ai","memory","prompt","knowledge","orchestration","handlebars","template"],"repository":{"type":"git","url":"git+https://github.com/Hariprakash1997/astralib.git","directory":"packages/chat/chat-ai"},"description":"Optional AI layer for chat-engine: memory management, prompt templates, knowledge base, and AI call orchestration","maintainers":[{"name":"astralib","email":"1997hariprakash@gmail.com"}],"readme":"# @astralibx/chat-ai\n\n[![npm version](https://img.shields.io/npm/v/@astralibx/chat-ai.svg)](https://www.npmjs.com/package/@astralibx/chat-ai)\n[![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](https://opensource.org/licenses/MIT)\n\nOptional AI layer for `@astralibx/chat-engine` -- memory management, prompt templates, knowledge base, and AI call orchestration. Consumers own everything: which AI to call, what prompts to write, what to remember. This package provides the infrastructure.\n\n## Install\n\n```bash\nnpm install @astralibx/chat-ai\n```\n\n### Peer Dependencies\n\n| Package | Required |\n|---------|----------|\n| `express` | Yes |\n| `mongoose` | Yes |\n\n## Quick Start\n\n```ts\nimport { createChatAI } from '@astralibx/chat-ai';\nimport { createChatEngine } from '@astralibx/chat-engine';\n\nconst ai = createChatAI({\n  db: { connection: mongooseConnection },\n\n  // Consumer provides AI call function -- manages own keys/rotation/fallback\n  chat: {\n    generate: async (systemPrompt, userMessage, history, options) => {\n      const res = await groq.chat.completions.create({ /* ... */ });\n      return { content: res.choices[0].message.content, model: 'llama-3.3-70b' };\n    },\n  },\n});\n\n// Use as chat-engine adapter\nconst engine = createChatEngine({\n  adapters: {\n    generateAiResponse: ai.generateResponse,\n  },\n});\n\n// Mount REST routes\napp.use('/api/chat-ai', ai.routes);\n```\n\n## Memory Examples\n\n```ts\n// Admin creates global business knowledge\nawait ai.memories.create({\n  scope: 'global',\n  key: 'return_policy',\n  content: 'We offer 30-day returns for unused items with receipt.',\n  category: 'policies',\n  priority: 80,\n  source: 'admin',\n});\n\n// Agent saves note about visitor during chat\nawait ai.memories.create({\n  scope: 'visitor',\n  scopeId: 'visitor_abc123',\n  key: 'preference',\n  content: 'Prefers email follow-up over phone calls.',\n  category: 'agent_notes',\n  source: 'agent',\n});\n\n// AI context building fetches relevant memories automatically\nconst response = await ai.generateResponse({\n  sessionId: 'sess_1',\n  visitorId: 'visitor_abc123',\n  messages: [...recentMessages],\n  agent: currentAgent,\n  visitorContext: { visitorId: 'visitor_abc123', channel: 'website' },\n});\n```\n\n## Mem0 Backend\n\n```ts\nconst ai = createChatAI({\n  db: { connection },\n  memoryBackend: {\n    type: 'mem0',\n    client: new MemClient({ apiKey: process.env.MEM0_KEY }),\n    scopeMapping: {\n      visitor: (id) => ({ user_id: id }),\n      agent: (id) => ({ agent_id: id }),\n      global: () => ({ user_id: 'global' }),\n      channel: (id) => ({ metadata: { channel: id } }),\n    },\n  },\n  chat: {\n    generate: async (systemPrompt, userMessage, history) => {\n      const key = groqKeys[callCount++ % groqKeys.length]; // key rotation\n      const client = new Groq({ apiKey: key });\n      const res = await client.chat.completions.create({\n        model: 'llama-3.3-70b-versatile',\n        messages: [\n          { role: 'system', content: systemPrompt },\n          ...history,\n          { role: 'user', content: userMessage },\n        ],\n      });\n      return { content: res.choices[0].message.content, model: 'llama-3.3-70b', tokensUsed: 0 };\n    },\n  },\n});\n```\n\n## Prompt Template\n\n```ts\nawait ai.prompts.create({\n  name: 'Support Agent',\n  isDefault: true,\n  sections: [\n    { key: 'identity', label: 'Identity', content: 'You are {{agentName}}, a support agent at {{companyName}}.', position: 1, isEnabled: true, isSystem: false },\n    { key: 'rules', label: 'Rules', content: 'Be friendly. Never share internal pricing. Ask for email before ending chat.', position: 2, isEnabled: true, isSystem: false },\n    { key: 'memory_injection', label: 'Memories', content: '', position: 3, isEnabled: true, isSystem: true },\n    { key: 'knowledge_injection', label: 'Knowledge', content: '', position: 4, isEnabled: true, isSystem: true },\n    { key: 'conversation_history', label: 'History', content: '', position: 5, isEnabled: true, isSystem: true },\n  ],\n});\n```\n\n## Features\n\n- **Memory management** -- Three backends (builtin, Mem0, custom), four scopes, three search strategies. [Details](https://github.com/Hariprakash1997/astralib/blob/main/packages/chat/chat-ai/docs/memory-management.md)\n- **Prompt templates** -- Ordered sections with Handlebars variables, system-injected memory/knowledge/history. [Details](https://github.com/Hariprakash1997/astralib/blob/main/packages/chat/chat-ai/docs/prompt-templates.md)\n- **Knowledge base** -- Store documents with optional embeddings for vector search injection. [Details](https://github.com/Hariprakash1997/astralib/blob/main/packages/chat/chat-ai/docs/knowledge-base.md)\n- **No provider mode** -- Use memory, prompts, and knowledge without wiring up AI generation. [Details](https://github.com/Hariprakash1997/astralib/blob/main/packages/chat/chat-ai/docs/configuration.md)\n- **Error classes** -- Typed errors with codes for every failure scenario. [Details](https://github.com/Hariprakash1997/astralib/blob/main/packages/chat/chat-ai/docs/error-handling.md)\n\n## Architecture\n\nThe library exposes a single Express router and programmatic services from a single factory call:\n\n| Access | Description |\n|--------|-------------|\n| `ai.routes` | REST API -- memories, prompts, knowledge (protect with your own auth middleware) |\n| `ai.memories` | Memory CRUD and search |\n| `ai.prompts` | Prompt template CRUD and preview |\n| `ai.knowledge` | Knowledge base CRUD and search |\n| `ai.generateResponse` | AI response generation (requires `chat.generate` config) |\n\n## Getting Started Guide\n\n1. [Configuration](https://github.com/Hariprakash1997/astralib/blob/main/packages/chat/chat-ai/docs/configuration.md) -- Set up database, AI provider, memory backend, and embeddings\n2. [Memory Management](https://github.com/Hariprakash1997/astralib/blob/main/packages/chat/chat-ai/docs/memory-management.md) -- Backends, scopes, and search strategies\n3. [Prompt Templates](https://github.com/Hariprakash1997/astralib/blob/main/packages/chat/chat-ai/docs/prompt-templates.md) -- System and user sections, variables, preview\n4. [Knowledge Base](https://github.com/Hariprakash1997/astralib/blob/main/packages/chat/chat-ai/docs/knowledge-base.md) -- Documents, embeddings, and search\n\nReference: [API Routes](https://github.com/Hariprakash1997/astralib/blob/main/packages/chat/chat-ai/docs/api-routes.md) | [Error Handling](https://github.com/Hariprakash1997/astralib/blob/main/packages/chat/chat-ai/docs/error-handling.md)\n\n## License\n\nMIT\n","readmeFilename":"README.md"}