npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

@spencerkinneydev/aicontext

v0.0.2

Published

Simple context management for AI conversations

Downloads

25

Readme

AIContext

Simple context management for AI chat applications. Handles message history and formatting across different LLM providers.

Installation

npm install @spencerkinney/aicontext

Example

import { AIChatContext, aiChatContext } from '@spencerkinneydev/aicontext';
import OpenAI from 'openai';
import Anthropic from '@anthropic-ai/sdk';

// Initialize clients and context
const context = new AIChatContext({ systemPrompt: "You are a helpful assistant." });
const openai = new OpenAI();
const anthropic = new Anthropic();

class AIClient {
    @aiChatContext(context)
    async askGPT(prompt: string) {
        return openai.chat.completions.create({
            model: "gpt-4",
            messages: [
                { role: "system", content: context.getSystemPrompt() },
                ...context.getMessages()
            ]
        });
    }

    @aiChatContext(context)
    async askClaude(prompt: string) {
        return anthropic.messages.create({
            model: "claude-3-5-sonnet-20241022",
            max_tokens: 1024,
            messages: context.getMessages()
        });
    }
}

// Use in async context
const ai = new AIClient();
await ai.askGPT("What's the capital of France?");
await ai.askClaude("What's interesting about it?");
console.log(context.format());  // Print conversation

More Examples

Named Assistants

class AISpecialists {
    @aiChatContext(context, "Researcher")
    async researcher(prompt: string) {
        return anthropic.messages.create({
            model: "claude-3-5-sonnet-20241022",
            max_tokens: 1024,
            messages: context.getMessages()
        });
    }

    @aiChatContext(context, "Coder")
    async coder(prompt: string) {
        return openai.chat.completions.create({
            model: "gpt-4",
            messages: context.getMessages()
        });
    }
}

const specialists = new AISpecialists();

// Use different assistants
await specialists.researcher("Explain quantum computing");
await specialists.coder("Show me a quantum random number generator");

// Get messages by assistant
console.log(context.format({ assistant_name: "Researcher" }));
console.log(context.latest("Coder"));

Message Filtering

// Last 5 messages
console.log(context.format({ limit: 5 }));

// Filter by assistant and role
console.log(context.format({
    assistant_name: "Researcher",
    role: "assistant",
    limit: 2
}));

// Get raw messages for API
const messages = context.getMessages();

Save & Load

// Save context
const saved = context.toJSON();

// Load in new context
const newContext = new AIChatContext({ messages: saved });

API Reference

AIChatContext

new AIChatContext({
    systemPrompt?: string;    // System instructions
    maxMessages?: number;     // Message limit
    messages?: AIChatMessageData[] | string;  // Load existing
})

Methods

| Method | Description | |--------|-------------| | getMessages() | Get messages for API calls | | filter(options) | Filter messages | | format(options?) | Get formatted history | | latest(assistant_name?) | Get latest message | | toJSON() | Export to JSON | | loadMessages(messages) | Load messages | | clear(assistant_name?) | Clear history | | getSystemPrompt() | Get system prompt | | getMaxMessages() | Get message limit |

Decorator

@aiChatContext(
    context: AIChatContext,         // Context instance
    assistant_name?: string = "default" // Optional name
)

Filtering Options

interface FilterOptions {
    assistant_name?: string;  // Filter by assistant
    role?: string;           // Filter by role
    limit?: number;          // Limit results
    offset?: number;         // Skip messages
}

Using with JavaScript

const { AIChatContext, aiChatContext } = require('@spencerkinneydev/aicontext');

class AIClient {
    constructor() {
        this.context = new AIChatContext({ systemPrompt: "You are a helpful assistant." });
    }

    askGPT = aiChatContext(this.context)(async function(prompt) {
        // OpenAI implementation
    });
}

License

MIT