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

cersei_rs

v1.0.28

Published

Cersei N-API 包装器,支持流式智能体对话 / Cersei N-API wrapper supporting streaming agent chat

Downloads

1,098

Readme

English | 中文


cersei_rs

N-API wrapper for Cersei, providing agent creation, streaming chat session, and logging functionality in Node.js.

Installation

npm install cersei_rs

API Usage

Constants

Import constants from "cersei_rs/MSG" to identify event types in the stream:

  • MSG_ERR (1): Error occurred.
  • MSG_TOOL (2): Tool execution started.
  • MSG_TXT (3): Streaming text content.
  • MSG_THINK (4): Streaming thinking process.

logChat (Default Export)

Wrapper function that automatically logs agent execution (thinking process, tool calls, and output text) to stdout and returns a Promise resolving to the final accumulated text response.

import logChat from "cersei_rs";

const agent = logChat(baseUrl, apiKey, model);
const response = await agent("Write a programmer joke in tmp.md", "./gen");
console.log("\nResponse:", response);

logSession

Wrapper function that automatically logs agent execution for a session maintaining chat history, returning a function that yields a Promise resolving to the final accumulated text response.

import logSession from "cersei_rs/logSession";

const history = [
  { role: "user", content: "Remember my name is Cersei" },
  { role: "assistant", content: "Got it, I'll remember that your name is Cersei." },
];

const chatSession = logSession(baseUrl, apiKey, model, "./gen", history);
const response = await chatSession("What is my name?");
console.log("\nResponse:", response);

chat

Creates a stateless agent function that returns an async generator streaming events.

import chat from "cersei_rs/chat";
import { MSG_TXT, MSG_TOOL } from "cersei_rs/MSG";

const agent = chat(baseUrl, apiKey, model);

// Run task and stream events
const prompt = "Write a programmer joke in tmp.md";
const workingDir = "./gen";

for await (const [type, content, args] of agent(prompt, workingDir)) {
  switch (type) {
    case MSG_TXT:
      process.stdout.write(content);
      break;
    case MSG_TOOL:
      console.log(`\n[Tool]: ${content} ${args || ""}`);
      break;
  }
}

session

Creates an agent session that maintains chat history, returning a function that yields streaming events.

import session from "cersei_rs/session";
import { MSG_TXT } from "cersei_rs/MSG";

const history = [
  { role: "user", content: "Remember my name is Cersei" },
  { role: "assistant", content: "Got it, I'll remember that your name is Cersei." },
];

const chatSession = session(baseUrl, apiKey, model, "./gen", history);

for await (const [type, content] of chatSession("What is my name?")) {
  if (type === MSG_TXT) {
    process.stdout.write(content);
  }
}

License

MulanPSL-2.0

About

This library is developed by WebC.site.

WebC.site: A new paradigm of web development for AI


cersei_rs

Cersei 的 N-API 包装器,为 Node.js 环境提供智能体创建、流式对话会话,以及自动日志记录功能。

安装

npm install cersei_rs

API 使用说明

常量定义

"cersei_rs/MSG" 导入常量以识别流式输出中的事件类型:

  • MSG_ERR (1): 异常错误事件。
  • MSG_TOOL (2): 工具执行启动事件。
  • MSG_TXT (3): 文本内容流式输出事件。
  • MSG_THINK (4): 思考过程流式输出事件。

logChat (默认导出)

包装函数,能自动向 stdout 打印智能体执行日志(包含思考过程、工具调用和文本输出),并返回 Promise 解析为最终累积的文本响应。

import logChat from "cersei_rs";

const agent = logChat(baseUrl, apiKey, model);
const response = await agent("在 tmp.md 中写一个程序员的笑话", "./gen");
console.log("\n响应:", response);

logSession

包装函数,能自动向 stdout 打印维护对话历史的智能体执行日志,并返回 Promise 解析为最终累积的文本响应。

import logSession from "cersei_rs/logSession";

const history = [
  { role: "user", content: "记住我的名字叫 Cersei" },
  { role: "assistant", content: "好的,我已经记住了,您的名字是 Cersei。" },
];

const chatSession = logSession(baseUrl, apiKey, model, "./gen", history);
const response = await chatSession("我的名字叫什么?");
console.log("\n响应:", response);

chat

创建一个无状态的智能体执行函数,返回一个流式事件的异步生成器。

import chat from "cersei_rs/chat";
import { MSG_TXT, MSG_TOOL } from "cersei_rs/MSG";

const agent = chat(baseUrl, apiKey, model);

// 运行任务并以流式获取事件
const prompt = "在 tmp.md 中写一个程序员的笑话";
const workingDir = "./gen";

for await (const [type, content, args] of agent(prompt, workingDir)) {
  switch (type) {
    case MSG_TXT:
      process.stdout.write(content);
      break;
    case MSG_TOOL:
      console.log(`\n[工具]: ${content} ${args || ""}`);
      break;
  }
}

session

创建一个在会话上下文中维护对话历史的智能体函数,返回一个流式事件的异步生成器。

import session from "cersei_rs/session";
import { MSG_TXT } from "cersei_rs/MSG";

const history = [
  { role: "user", content: "记住我的名字叫 Cersei" },
  { role: "assistant", content: "好的,我已经记住了,您的名字是 Cersei。" },
];

const chatSession = session(baseUrl, apiKey, model, "./gen", history);

for await (const [type, content] of chatSession("我的名字叫什么?")) {
  if (type === MSG_TXT) {
    process.stdout.write(content);
  }
}

开源协议

MulanPSL-2.0

关于

本库由 WebC.site 开发。

WebC.site : 面向人工智能的网站开发新范式