{"_id":"@cubicler/cubickit-openai","name":"@cubicler/cubickit-openai","dist-tags":{"latest":"0.0.1"},"versions":{"0.0.1":{"name":"@cubicler/cubickit-openai","version":"0.0.1","description":"Adapter for integrating OpenAI's Chat API with the Cubicler orchestration system.","main":"dist/index.js","types":"dist/index.d.ts","scripts":{"build":"tsc","prepublishOnly":"npm run build && npm test","test":"jest","test:unit":"jest --testPathIgnorePatterns=integration","test:integration":"jest --testPathPattern=integration","lint":"eslint .","start":"node dist/index.js"},"dependencies":{"@cubicler/cubickit":"^0.0.1","openai":"^4.0.0"},"devDependencies":{"@types/express":"^5.0.3","@types/jest":"^30.0.0","@types/js-yaml":"^4.0.9","dotenv":"^17.2.0","eslint":"^8.0.0","express":"^5.1.0","jest":"^29.0.0","js-yaml":"^4.1.0","ts-jest":"^29.4.0","typescript":"^5.0.0"},"keywords":["openai","cubickit","cubicler","ai","agent","chat","api","integration","typescript"],"author":{"name":"hainayanda"},"license":"MIT","repository":{"type":"git","url":"git+https://github.com/hainayanda/CubicKit-OpenAI.git"},"bugs":{"url":"https://github.com/hainayanda/CubicKit-OpenAI/issues"},"homepage":"https://github.com/hainayanda/CubicKit-OpenAI#readme","_id":"@cubicler/cubickit-openai@0.0.1","gitHead":"d50c179fe8215bd7eb15b8bc80fd8c0f4d4a9754","_nodeVersion":"24.4.0","_npmVersion":"11.4.2","dist":{"integrity":"sha512-nZUUYbI1IjnpOs5TBFJhyuBrIS7KHi30HtPAIzyfGZCULZBMsDtZXgXA6aHOWCKPYd8Yv7zL9giopifCVmfZVQ==","shasum":"d965d0f0b5246bc8f3965c61335f2d846049a6d4","tarball":"https://registry.npmjs.org/@cubicler/cubickit-openai/-/cubickit-openai-0.0.1.tgz","fileCount":11,"unpackedSize":32532,"signatures":[{"keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U","sig":"MEUCIQCOIAItpf6bGiCiB62xHnjcfaNZIpPdEN3mRt1PedjxCwIgXp8mqz/8LNJKscyKISbRfZyMQmUeRfkv0NTHBRrR2nM="}]},"_npmUser":{"name":"hainayanda","email":"hainayanda@gmail.com"},"directories":{},"maintainers":[{"name":"hainayanda","email":"hainayanda@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/cubickit-openai_0.0.1_1753010920605_0.9599184165451566"},"_hasShrinkwrap":false}},"time":{"created":"2025-07-20T11:28:40.530Z","0.0.1":"2025-07-20T11:28:40.802Z","modified":"2025-07-20T11:28:41.069Z"},"maintainers":[{"name":"hainayanda","email":"hainayanda@gmail.com"}],"description":"Adapter for integrating OpenAI's Chat API with the Cubicler orchestration system.","homepage":"https://github.com/hainayanda/CubicKit-OpenAI#readme","keywords":["openai","cubickit","cubicler","ai","agent","chat","api","integration","typescript"],"repository":{"type":"git","url":"git+https://github.com/hainayanda/CubicKit-OpenAI.git"},"author":{"name":"hainayanda"},"bugs":{"url":"https://github.com/hainayanda/CubicKit-OpenAI/issues"},"license":"MIT","readme":"# CubicKit-OpenAI\n\nAn adapter for integrating OpenAI's Chat API with the Cubicler orchestration system.\n\n## Features\n\n- 🤖 **Seamless OpenAI Integration**: Uses OpenAI's chat completions API with automatic tool/function calling\n- 🔧 **Automatic Tool Injection**: Fetches function specifications from Cubicler and automatically converts them to OpenAI tools format\n- 🔄 **Smart Tool Call Routing**: Automatically routes OpenAI tool calls to Cubicler's function execution endpoint\n- 📦 **Built on CubicKit**: Leverages CubicKit's caching and fallback mechanisms\n- 🎯 **Type-Safe**: Full TypeScript support with proper type definitions\n- ⚡ **Easy to Use**: Simple API that handles all the complexity behind the scenes\n\n## Installation\n\n```bash\nnpm install @cubicler/cubickit-openai\n```\n\n## Quick Start\n\n```typescript\nimport { OpenAICubicClient } from '@cubicler/cubickit-openai';\n\n// Initialize the kit\nconst openaiClient = new OpenAICubicClient({\n  apiKey: 'your-openai-api-key',\n  cubiclerBaseUrl: 'https://your-cubicler-instance.com',\n  model: 'gpt-4o', // Optional, defaults to 'gpt-4o'\n});\n\n// Start chatting - tools are automatically injected and handled\nconst response = await openaiClient.chat([\n  { role: 'user', content: 'Hello! Can you help me with something?' }\n]);\n\nconsole.log(response.choices[0].message.content);\n```\n\n## How It Works\n\n1. **Fetches System Prompt**: Automatically retrieves the system prompt from `GET /prompt` via CubicKit\n2. **Loads Function Specs**: Fetches available functions from `GET /spec` via CubicKit  \n3. **Converts to OpenAI Tools**: Transforms Cubicler function specs into OpenAI tools format\n4. **Handles Chat**: Sends chat requests to OpenAI with injected tools\n5. **Routes Tool Calls**: When OpenAI calls a tool, automatically routes it to Cubicler via `POST /call`\n6. **Continues Conversation**: Feeds tool results back to OpenAI to complete the conversation\n\n## Configuration\n\n```typescript\nimport { OpenAICubicClient, OpenAICubicClientConfigurations } from '@cubicler/cubickit-openai';\n\nconst config: OpenAICubicClientConfigurations = {\n  apiKey: 'your-openai-api-key',              // Required: OpenAI API key\n  cubiclerBaseUrl: 'https://your.cubicler.com', // Required: Cubicler base URL\n  model: 'gpt-4o',                            // Optional: OpenAI model (default: 'gpt-4o')\n  promptCacheTimeout: 600,                    // Optional: Prompt cache timeout in seconds (default: 600)\n  specCacheTimeout: 600,                      // Optional: Spec cache timeout in seconds (default: 600)\n};\n\nconst openaiClient = new OpenAICubicClient(config);\n```\n\n## Advanced Usage\n\n### Adding Internal Functions\n\nYou can add internal functions that will be injected alongside (or override) Cubicler functions:\n\n```typescript\n// Add a single function\nopenaiClient.addFunction(\n  {\n    type: 'function',\n    function: {\n      name: 'getCurrentTime',\n      description: 'Get the current date and time',\n      parameters: {\n        type: 'object',\n        properties: {\n          timezone: { type: 'string', description: 'Timezone (optional)' },\n        },\n      },\n    },\n  },\n  async (parameters) => {\n    const timezone = parameters.timezone || 'UTC';\n    return {\n      currentTime: new Date().toLocaleString('en-US', { timeZone: timezone }),\n      timezone: timezone,\n    };\n  }\n);\n\n// Chain multiple functions\nopenaiClient\n  .addFunction(spec1, callback1)\n  .addFunction(spec2, callback2)\n  .addFunction(spec3, callback3);\n\n// Remove a function\nopenaiClient.removeFunction('functionName');\n\n// Clear all internal functions\nopenaiClient.clearFunctions();\n\n// Get all available function names\nconst functionNames = await openaiClient.getFunctionNames();\n```\n\n**Priority Rules:**\n\n- Internal functions take priority over Cubicler functions with the same name\n- This allows you to override or extend Cubicler's functionality\n- Internal functions are called directly without going through Cubicler\n\n### Custom Chat Options\n\nYou can pass any OpenAI chat completion parameters:\n\n```typescript\nconst response = await openaiClient.chat(\n  [{ role: 'user', content: 'Hello!' }],\n  {\n    temperature: 0.7,\n    max_tokens: 1000,\n    top_p: 0.9,\n  }\n);\n```\n\n### Access Underlying Clients\n\n```typescript\n// Get the OpenAI client for advanced usage\nconst openaiClient = openaiClient.getOpenAIClient();\n\n// Get the CubicKit client for direct Cubicler interaction\nconst cubicKitClient = openaiClient.getCubicKitClient();\n```\n\n### Force Cache Refresh\n\n```typescript\n// Force refresh both prompt and spec caches\nawait openaiClient.refreshCache();\n```\n\n## Error Handling\n\nThe kit automatically handles:\n\n- Network failures (falls back to cached data via CubicKit)\n- Tool call errors (returns error messages to continue conversation)\n- Invalid function parameters (graceful error handling)\n\n```typescript\ntry {\n  const response = await openaiClient.chat([\n    { role: 'user', content: 'Execute a complex task' }\n  ]);\n} catch (error) {\n  console.error('Chat failed:', error);\n}\n```\n\n## Examples\n\n### Simple Question Answering\n\n```typescript\nconst response = await openaiClient.chat([\n  { role: 'user', content: 'What is the weather like today?' }\n]);\n```\n\n### Multi-turn Conversation\n\n```typescript\nconst messages = [\n  { role: 'user', content: 'I need to analyze some data' },\n  { role: 'assistant', content: 'I can help with that. What kind of data analysis do you need?' },\n  { role: 'user', content: 'Customer sales data for the last quarter' },\n];\n\nconst response = await openaiClient.chat(messages);\n```\n\n### Function Calling\n\nThe kit automatically handles function calls. When OpenAI decides to call a function:\n\n1. OpenAI returns a tool call\n2. The kit routes it to Cubicler's `/call` endpoint\n3. The result is fed back to OpenAI\n4. OpenAI continues the conversation with the result\n\n```typescript\n// This will automatically trigger function calls if the AI decides they're needed\nconst response = await openaiClient.chat([\n  { role: 'user', content: 'Get me the user details for user ID 12345' }\n]);\n```\n\n## Architecture\n\n```\n┌─────────────────┐    ┌─────────────────┐    ┌─────────────────┐\n│   Your App      │───▶│   OpenAI Kit    │───▶│     OpenAI      │\n└─────────────────┘    └─────────────────┘    └─────────────────┘\n                                │\n                                ▼\n                       ┌─────────────────┐    ┌─────────────────┐\n                       │    CubicKit     │───▶│    Cubicler     │\n                       └─────────────────┘    └─────────────────┘\n```\n\nThe OpenAI Kit acts as a bridge between OpenAI's chat API and Cubicler's function execution system, providing a seamless experience for building AI agents that can execute real-world tasks.\n\n## License\n\nMIT\n","readmeFilename":"README.md","_rev":"1-2e9a608f6e16f3da301abef9bef7fb06"}