|
| 1 | +# @objectstack/ai-bridge |
| 2 | + |
| 3 | +**AI Context Bridge** for ObjectStack. |
| 4 | +This library provides a set of tools to bridge the gap between your metadata definitions (`Spec`) and Generative AI agents. |
| 5 | + |
| 6 | +It translates your static schema definitions into formats that LLMs (Large Language Models) can understand, strictly adhere to, and execute against. |
| 7 | + |
| 8 | +## 🌟 Capabilities |
| 9 | + |
| 10 | +1. **Static Type Generator (Code Scaffolding)** |
| 11 | + * Converts Object/Field definitions into strong TypeScript Interfaces (`.d.ts`). |
| 12 | + * Generates ObjectQL Query types (`$select`, `$filter`) with literal types for fields. |
| 13 | + * *Use Case:* Fed into "Coding Agents" to ensure they generate valid, compiling code. |
| 14 | + |
| 15 | +2. **Runtime Introspection Manifest (System Prompts)** |
| 16 | + * Generates a token-optimized, compressed JSON representation of your system. |
| 17 | + * Strip UI/Config noise, keeping only Business Logic (Objects, Fields, Capabilities). |
| 18 | + * Context-aware: Filters capabilities based on the current user's permissions. |
| 19 | + * *Use Case:* Injected into the System Prompt so the AI knows "Permission to Speak". |
| 20 | + |
| 21 | +3. **Tool Definitions (Function Calling)** |
| 22 | + * Outputs standard OpenAI `tools` (JSON Schema) for core ObjectQL operations (`search`, `create`, `update`). |
| 23 | + * *Use Case:* Enables "Agentic" behavior where the AI directly queries your database. |
| 24 | + |
| 25 | +## 📦 Installation |
| 26 | + |
| 27 | +```bash |
| 28 | +pnpm add @objectstack/ai-bridge |
| 29 | +``` |
| 30 | + |
| 31 | +## 🛠️ Usage |
| 32 | + |
| 33 | +### 1. Generating TypeScript Definitions |
| 34 | +Generate `.d.ts` content to paste into an LLM context or save to a file for dynamic execution. |
| 35 | + |
| 36 | +```typescript |
| 37 | +import { generateTypeScriptDefinition } from '@objectstack/ai-bridge'; |
| 38 | + |
| 39 | +const objects = [ |
| 40 | + { |
| 41 | + name: 'project', |
| 42 | + fields: { |
| 43 | + name: { type: 'text', required: true }, |
| 44 | + status: { type: 'select', options: ['planning', 'active'] } |
| 45 | + } |
| 46 | + } |
| 47 | +]; |
| 48 | + |
| 49 | +const tsContent = generateTypeScriptDefinition(objects); |
| 50 | +console.log(tsContent); |
| 51 | +// Output: |
| 52 | +// export interface Project { name: string; status?: 'planning' | 'active'; ... } |
| 53 | +``` |
| 54 | + |
| 55 | +### 2. Runtime Context (Token Efficient) |
| 56 | +Create a lightweight manifest for the AI's "Short-term Memory". |
| 57 | + |
| 58 | +```typescript |
| 59 | +import { generateAIManifest } from '@objectstack/ai-bridge'; |
| 60 | + |
| 61 | +const manifest = generateAIManifest(objects, { |
| 62 | + // Optional: Filter capabilities based on real permissions |
| 63 | + userPermissions: { |
| 64 | + 'project': ['read', 'create'] // User cannot delete |
| 65 | + } |
| 66 | +}); |
| 67 | + |
| 68 | +// Inject this into your System Prompt |
| 69 | +const systemPrompt = ` |
| 70 | +You are an assistant. Here is the database schema you have access to: |
| 71 | +${JSON.stringify(manifest)} |
| 72 | +`; |
| 73 | +``` |
| 74 | + |
| 75 | +### 3. OpenAI Tools |
| 76 | +Get standard Function Calling definitions to pass to the OpenAI API. |
| 77 | + |
| 78 | +```typescript |
| 79 | +import { generateOpenAITools } from '@objectstack/ai-bridge'; |
| 80 | +import OpenAI from 'openai'; |
| 81 | + |
| 82 | +const tools = generateOpenAITools(); |
| 83 | + |
| 84 | +const response = await openai.chat.completions.create({ |
| 85 | + model: 'gpt-4-turbo', |
| 86 | + messages: [...], |
| 87 | + tools: tools, // <-- Direct integration |
| 88 | + tool_choice: 'auto' |
| 89 | +}); |
| 90 | +``` |
| 91 | + |
| 92 | +## 🏗️ Architecture |
| 93 | + |
| 94 | +This package is designed to be **isomorphic** and **dependency-light**. It does not depend on the heavy `runtime` or `core` packages of ObjectStack, meaning it can be run in Edge functions, Lambda, or client-side environments to help construct prompts. |
0 commit comments