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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@modular-prompt/utils

v0.2.0

Published

Utility functions for modular-prompt

Readme

@modular-prompt/utils

Moduler Promptフレームワークのユーティリティパッケージ。プロンプトのフォーマット変換とドライバレジストリ機能を提供します。

機能

1. プロンプトフォーマッタ

CompiledPromptをテキストやメッセージ配列に変換:

import { formatPrompt, formatPromptAsMessages } from '@modular-prompt/utils';
import { compile } from '@modular-prompt/core';

const compiled = compile(module, context);

// テキスト形式に変換
const text = formatPrompt(compiled);

// メッセージ配列形式に変換(ChatGPT API用)
const messages = formatPromptAsMessages(compiled);

2. ドライバレジストリ

AIモデルドライバの自動選択と管理:

import { DriverRegistry, registerDriverFactories } from '@modular-prompt/utils';
import * as Drivers from '@modular-prompt/driver';

// レジストリを初期化
const registry = new DriverRegistry();

// ドライバファクトリを登録
registerDriverFactories(registry, Drivers);

// 設定ファイルを読み込み
// (サンプル設定: docs/examples/driver-registry-config.yaml)
await registry.loadConfig('./drivers.yaml');

// 必要な能力に基づいてドライバを自動選択
const driver = await registry.selectAndCreateDriver(
  ['japanese', 'chat', 'local'],
  {
    preferLocal: true,
    preferFast: true
  }
);

// 選択されたドライバで実行
const result = await driver.query(compiledPrompt);

Driver Registry

設定ファイル形式(YAML)

サンプル設定ファイルは docs/examples/driver-registry-config.yaml を参照してください。

version: "1.0"
defaultDriver: mlx-gemma-270m

global:
  temperature: 0.7
  maxTokens: 2048
  timeout: 30000

drivers:
  - id: mlx-gemma-270m
    name: MLX Gemma 270M
    model:
      model: mlx-community/gemma-3-270m-it-qat-4bit
      provider: mlx
      capabilities:
        - local
        - fast
        - streaming
        - japanese
        - chat
      maxInputTokens: 8192
      maxOutputTokens: 2048
      priority: 10
    
  - id: gpt-4o
    name: GPT-4o
    model:
      model: gpt-4o
      provider: openai
      capabilities:
        - streaming
        - tools
        - vision
        - multilingual
        - reasoning
        - large-context
      maxInputTokens: 128000
      maxOutputTokens: 16384
      cost:
        input: 0.0025
        output: 0.01
      priority: 20

ドライバ能力(Capabilities)

  • streaming: ストリーミング応答対応
  • local: ローカル実行可能
  • fast: 高速応答
  • large-context: 大規模コンテキスト対応
  • multilingual: 多言語対応
  • japanese: 日本語特化
  • coding: コーディング特化
  • reasoning: 推論・思考特化
  • chat: チャット特化
  • tools: ツール使用可能
  • vision: 画像認識可能
  • audio: 音声処理可能
  • structured: 構造化出力対応
  • json: JSON出力対応
  • function-calling: 関数呼び出し対応

選択条件の指定

import type { DriverSelectionCriteria } from '@modular-prompt/utils';

const criteria: DriverSelectionCriteria = {
  // 必須の能力(すべて満たす必要がある)
  requiredCapabilities: ['japanese', 'streaming'],
  
  // 望ましい能力(いくつか満たせばよい)
  preferredCapabilities: ['local', 'fast'],
  
  // 除外する能力
  excludeCapabilities: ['tools'],
  
  // 最小トークン数
  minInputTokens: 8000,
  minOutputTokens: 2000,
  
  // 最大コスト(1Kトークンあたり)
  maxCost: {
    input: 0.001,
    output: 0.005
  },
  
  // プロバイダー制限
  providers: ['mlx', 'openai'],
  excludeProviders: ['anthropic'],
  
  // 優先設定
  preferLocal: true,
  preferFast: true
};

const result = registry.selectDriver(criteria);
if (result) {
  console.log(`Selected: ${result.driver.name}`);
  console.log(`Reason: ${result.reason}`);
  console.log(`Score: ${result.score}`);
}

プログラマティックな登録

// カスタムドライバ設定を登録
registry.registerDriver({
  id: 'custom-model',
  name: 'Custom Model',
  model: {
    model: 'my-custom-model',
    provider: 'mlx',
    capabilities: ['local', 'japanese', 'chat'],
    maxInputTokens: 4096,
    maxOutputTokens: 1024,
    priority: 5
  },
  options: {
    temperature: 0.8
  }
});

使用例

import { DriverRegistry, registerDriverFactories, formatPrompt } from '@modular-prompt/utils';
import * as Drivers from '@modular-prompt/driver';
import { compile, createContext } from '@modular-prompt/core';
import { chatPromptModule } from '@modular-prompt/simple-chat';

async function main() {
  // レジストリを設定
  const registry = new DriverRegistry();
  registerDriverFactories(registry, Drivers);
  await registry.loadConfig('./drivers.yaml');
  
  // コンテキストとプロンプトを準備
  const context = createContext(chatPromptModule);
  context.userMessage = 'こんにちは!';
  const compiled = compile(chatPromptModule, context);
  
  // 日本語対応のローカルドライバを自動選択
  const driver = await registry.selectAndCreateDriver(
    ['japanese', 'chat'],
    { preferLocal: true }
  );
  
  if (!driver) {
    console.error('No suitable driver found');
    return;
  }
  
  // AIに問い合わせ
  const response = await driver.query(compiled);
  console.log(response.content);
}

main().catch(console.error);

インストール

npm install @modular-prompt/utils

API リファレンス

DriverRegistry

  • loadConfig(path: string): YAML設定ファイルを読み込み
  • registerDriver(config: DriverConfig): ドライバを登録
  • registerFactory(provider: DriverProvider, factory: DriverFactory): ドライバファクトリを登録
  • selectDriver(criteria: DriverSelectionCriteria): 条件に基づいてドライバを選択
  • getDriver(id: string): IDでドライバ設定を取得
  • getAllDrivers(): すべてのドライバ設定を取得
  • createDriver(config: DriverConfig): ドライバインスタンスを作成
  • selectAndCreateDriver(capabilities, options?): 自動選択してインスタンス作成

Helper Functions

  • registerDriverFactories(registry: DriverRegistry, drivers: DriverClasses): 標準ドライバファクトリを一括登録

Formatter

  • formatPrompt(compiled: CompiledPrompt, options?: FormatterOptions): テキスト形式に変換
  • formatPromptAsMessages(compiled: CompiledPrompt, options?: FormatterOptions): メッセージ配列に変換