JSPM

@dukebot/open-router

0.1.2
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 12
  • Score
    100M100P100Q49515F
  • License MIT

Lightweight local client for interacting with the OpenRouter API (CommonJS format)

Package Exports

  • @dukebot/open-router

Readme

@dukebot/open-router

Lightweight local client for interacting with the OpenRouter API (CommonJS format).

This package provides a modular way to call large language models like GPT-4o, Claude, Mistral or DeepSeek via OpenRouter, using a clean service-based structure and optional agent wrappers.

๐Ÿš€ Features

  • Simple wrapper around OpenRouter's /chat/completions endpoint.
  • Flexible message structure: raw messages or prompt + system.
  • Auto-parsing of JSON responses (with fallback via jsonrepair).
  • Preconfigured agent system for clean multi-role usage.

๐Ÿ“ฆ Installation

npm install @dukebot/open-router

Requires Node.js >=18 (for native fetch support).


โšก Quick Example

const { OpenRouter } = require('@dukebot/open-router')

const openRouter = new OpenRouter({ apiKey: process.env.OPEN_ROUTER_API_KEY })

const response = await openRouter.service.completeChat({
  prompt: 'What is the capital of Japan?',
  system: 'You are a helpful assistant.',
  model: 'openai/gpt-4o',
})

console.log(response.content) // โ†’ "The capital of Japan is Tokyo."

๐Ÿง  Agent Example

const agent = openRouter.createAgent({
  model: 'openai/gpt-4o',
  system: 'You are a professional blog editor.',
  temperature: 0.7,
  responseAsJson: true,
})

const result = await agent.completeChat({ prompt: 'Write a short blog post about AI in 2025' })

console.log(result.json) // Parsed blog post structure (if response is valid JSON)

๐Ÿงฑ Modular Architecture

Component Description
OpenRouterClient Low-level fetcher for /chat/completions
OpenRouterService Adds support for simplified prompts and JSON parsing
OpenRouterAgent Fixed-configuration agent using a predefined system + model
OpenRouterModule Bundles client + service and exposes agent factory

๐Ÿ” Environment

You can provide the API key directly or via environment variable:

OPEN_ROUTER_API_KEY=your-api-key-here

๐Ÿงช Examples

Usage examples are located in the /examples folder.

To run the basic example:

node examples/simple.js

These scripts demonstrate common use cases such as:

  • Basic prompt + system completions
  • Agent-based completions with JSON output
  • Error handling and logging

Feel free to duplicate and adapt them to your own needs.


โœ… Tests

This package includes automated tests written in Jest.

To run the test suite:

npm install
npm test

Tests are located in the /tests folder and cover core functionality such as OpenRouterClient, OpenRouterService, and OpenRouterAgent. Mocking is used to isolate logic and ensure consistent behavior.


๐Ÿ“‹ License

MIT ยฉ Dukebot