Skip to content

OpenSIN SDK

The @opensin/sdk package provides the core building blocks for creating AI agents, tools, and workflows.

Installation

bash
npm install @opensin/sdk

Module Overview

ModuleDescription
Agent LoopReAct loop with tool calling, streaming, and turn management
Tool SystemDefine, validate, and execute tools with JSON Schema parameters
Model RoutingSmart model selection based on task complexity
Memory ManagerPersistent memory with search, tags, and access tracking
Context ManagementToken-aware context compression and sliding windows
Safety & PermissionsDestructive command detection and permission gates
Session PersistenceSave and restore agent sessions across restarts
Hooks & LifecycleEvent-driven lifecycle hooks for agent customization
Parallel ExecutionConcurrent tool execution with path-scoped locking
Usage & PricingToken counting and cost estimation per model

Quick Start

typescript
import {
  AgentLoop,
  ToolRegistry,
  SmartModelRouter,
  PermissionManager,
  SessionManager,
  ContextCompressor,
  SafetyDetector,
  UsagePricing,
} from '@opensin/sdk'

// 1. Set up model routing
const router = new SmartModelRouter({
  models: {
    trivial: 'gpt-4o-mini',
    simple: 'gpt-4o-mini',
    moderate: 'gpt-4o',
    complex: 'claude-sonnet-4-6',
    expert: 'claude-opus-4-6',
  },
})

// 2. Register tools
const tools = new ToolRegistry()
tools.register({
  name: 'read_file',
  description: 'Read a file from the workspace',
  parameters: {
    type: 'object',
    properties: {
      path: { type: 'string' },
    },
    required: ['path'],
  },
  execute: async ({ path }) => {
    const content = await fs.readFile(path, 'utf-8')
    return { content }
  },
})

// 3. Create the agent loop
const agent = new AgentLoop({
  model: router,
  tools,
  permissions: new PermissionManager({ mode: 'interactive' }),
  session: new SessionManager({ persistence: 'file' }),
  context: new ContextCompressor({ maxTokens: 8000 }),
  safety: new SafetyDetector(),
  pricing: new UsagePricing(),
  maxTurns: 25,
})

// 4. Run
const result = await agent.run('Read the README and summarize it')

Architecture

┌─────────────────────────────────────────────────┐
│                   Agent Loop                     │
│                                                  │
│  ┌──────────┐  ┌──────────┐  ┌───────────────┐  │
│  │  Model   │  │  Tool    │  │   Context     │  │
│  │  Router  │  │  System  │  │   Compressor  │  │
│  └──────────┘  └──────────┘  └───────────────┘  │
│                                                  │
│  ┌──────────┐  ┌──────────┐  ┌───────────────┐  │
│  │ Session  │  │ Safety   │  │   Permission  │  │
│  │ Manager  │  │ Detector │  │   Manager     │  │
│  └──────────┘  └──────────┘  └───────────────┘  │
│                                                  │
│  ┌──────────┐  ┌──────────┐  ┌───────────────┐  │
│  │  Memory  │  │  Hooks   │  │   Usage       │  │
│  │  Manager │  │  System  │  │   Pricing     │  │
│  └──────────┘  └──────────┘  └───────────────┘  │
└─────────────────────────────────────────────────┘

Package Exports

All modules are exported from the main package entry:

typescript
import {
  // Agent Loop
  AgentLoop,
  
  // Tool System
  ToolRegistry,
  MCPStdioClient,
  
  // Intelligence
  SmartModelRouter,
  ContextCompressor,
  MemoryManager,
  FileMemoryProvider,
  
  // Safety
  SafetyDetector,
  PermissionManager,
  
  // Infrastructure
  SessionManager,
  HookSystem,
  ParallelToolExecutor,
  UsagePricing,
  
  // Prompt Building
  PromptBuilder,
  
  // Skills
  SkillSystem,
} from '@opensin/sdk'

Test Coverage

The SDK has 573+ passing tests across all modules, verified with Vitest:

bash
cd packages/opensin-sdk
npm test

# Output:
# Test Files  15 passed
# Tests       573 passed
# Duration    4.2s

Released under the Apache 2.0 License.