@fungi.computer/mule

中文文档
0.2.3发布于 2026/11/08MITTypeScriptES Module近 30 天下载 1.3万

Effect-native Pi model and tool loop

原始使用文档
文档目录6 节
  1. Set up
  2. Quick start without a provider account
  3. Tools and control
  4. Cancellation, retries and errors
  5. API and examples
  6. Contribute and license

Mule (@fungi.computer/mule)

Mule runs Pi's model and tool loop inside Effect. Use it when your host already owns the model, messages and tools and needs an awaited, cancellable run. Pi's models, messages, provider replay fields and stream events keep their original types.

Set up

npm install @fungi.computer/mule

The package ships ESM only and depends on effect and Pi (@earendil-works/pi-ai) at runtime, plus TypeBox and @fungi.computer/cairn for IDs. Mule does not require or install the MCP SDK. Consumers that type-check dependency declarations (skipLibCheck: false) must install Pi’s Google GenAI optional declaration dependency themselves:

npm install --save-dev @modelcontextprotocol/sdk

The root entrypoint exports the Promise facade, mule.run. @fungi.computer/mule/effect exports the canonical muleEffect.run. The Promise facade adapts tools, hooks and listeners into that same Effect program, so public Promise declarations do not require Effect types.

Quick start without a provider account

This deterministic example uses the public testing helper. It makes no network request and needs no credential.

import { fauxAssistantMessage } from "@earendil-works/pi-ai/providers/faux";
import { mule } from "@fungi.computer/mule";
import { fauxModel } from "@fungi.computer/mule/testing";

const faux = fauxModel([fauxAssistantMessage("pong")]);
const outcome = await mule.run(
  {
    model: faux.model,
    retryable: faux.retryable,
    stream: faux.stream,
    systemPrompt: "Reply once.",
    messages: [{ role: "user", content: "ping", timestamp: 1 }],
  },
  () => {},
);
console.log(outcome.status);

The result is completed. No active stream remains when the awaited run settles. The test helper uses this provider and input shape, and facade tests compare Promise and Effect lifecycles. This fake provider checks orchestration rather than a real supplier.

For a real model, supply Pi's Model<Api> and either its normal streamSimple or an explicit provider stream. The host supplies credentials and billing. Await the listener: its completion is part of the run's lifecycle.

Tools and control

A tool pairs Pi's descriptor with prepare(call, signal). Preparation decodes and authorizes the exact call. It returns either a typed rejection or a prepared executor. The Promise executor yields parts through an AsyncIterable. The Effect executor uses a Stream. Every executor must observe cancellation.

Run, turn, message and tool events carry causal identities. They let the host record which execution produced an observation. They do not create durable Session storage. Hosts may select parallel or sequential tool execution, steer active work and queue follow-up input through the existing control interfaces.

A tool opts into safe crash replay only when it is read-only or explicitly idempotent. The host persists the checkpoints needed to use resume. Declaring replay: "safe" does not make an external write idempotent. Pi's provider messages and replay fields pass through unchanged.

Cancellation, retries and errors

Abort the Promise facade's signal or interrupt the Effect fiber. Mule aborts the Pi stream and running tools and records one cancelled run_end. The Promise facade preserves an Error abort reason and rejects with it. A non-Error reason becomes an AbortError. An operation that ignores abort still needs its host to account for the backing work.

Tool preparation rejections and execution failures become model-visible error results, so the run can continue. Fatal model or control failures produce a failed run_end. A failed listener cannot be trusted to record that terminal event. Preserve the rejection and its cause when reporting it.

retry selects Pi's bounded retry policy for each assistant call. Omitting it means no retry. Provider Retry-After evidence and retry observations use the existing run interfaces. Mule does not choose a provider policy for your host. modelStreamIdleTimeoutMs bounds silence between provider events.

Mule owns no model catalog, credential store, durable Session database, compaction service or process-restart scheduler. Compose those in the host.

API and examples

Use the generated API reference and the exports in package.json. Cancellation parity, retry policy and tool batch tests exercise the shared loop.

The prompt example is an optional live-provider caller. It requires CAMEL_API_KEY supplied through your local environment. Its endpoint and model overrides belong to that example, not to Mule's API. It is not needed for the deterministic quick start. Do not commit credentials.

Contribute and license

Add an orchestration regression using the existing faux provider. From the root:

pnpm --dir packages/mule run test
pnpm --dir packages/mule run docs

MIT. See LICENSE and THIRD_PARTY_NOTICES.

版本与发布记录

@fungi.computer/mule 共发布 9 个版本,首次发布于 2026/10/15;过去 12 个月发布了 9 个版本。

版本发布日期距今
0.2.3latest2026/11/088 天前
0.2.22026/11/0213 天前
0.2.12026/10/2818 天前
0.2.02026/10/2620 天前
0.1.12026/10/2521 天前
0.1.02026/10/2323 天前

关于 @fungi.computer/mule 的常见问题

@fungi.computer/mule 是什么?

Effect-native Pi model and tool loop

如何安装 @fungi.computer/mule?

在项目目录运行 npm install @fungi.computer/mule,或使用 pnpm add / yarn add / bun add。

@fungi.computer/mule 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@fungi.computer/mule 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@fungi.computer/mule 可以商用吗?

@fungi.computer/mule 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@fungi.computer/mule 的下载量有多少?

根据 npm 官方下载统计,@fungi.computer/mule 在 2026/10/16 – 2026/11/14 这 30 天内被下载 12,786 次,日均约 426 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@fungi.computer/mule 还在维护吗?

最新版本 0.2.3 发布于 2026/11/08(8 天前);至今共发布 9 个版本,其中过去 12 个月发布了 9 个;首次发布于 2026/10/15。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。