@amaster.ai/pi-task-scheduler

中文文档
0.1.37发布于 2026/11/09Apache-2.0TypeScriptES Module近 30 天下载 5253

Pi extension for cron-based scheduled task management with LLM-callable tools

原始使用文档
文档目录10 节
  1. Storage
  2. Default (file storage)
  3. Custom storage
  4. Integration Modes
  5. Mode 1: Extension auto-discovery (standalone / CLI)
  6. Mode 2: Dependency import (host-controlled)
  7. Choosing a mode
  8. Scheduling
  9. Hooks
  10. Structured results (Pi 0.99+)

@amaster.ai/pi-task-scheduler

pi-task-scheduler preview

Scheduled task domain types, execution scheduler, and pi agent extension.

The package owns schedule parsing, task lifecycle state, process-local timers, runner callbacks, scheduler hooks, and LLM-callable tools for autonomous task management.

The default Pi extension runner delivers scheduled prompts as follow-ups: an idle agent starts immediately, while a busy agent queues the prompt for later processing in the same session.

Storage

The package ships with a built-in file-based storage (default, zero external dependencies):

  • JsonScheduledTaskStore — persists tasks to a JSON file with atomic writes
  • FileSchedulerLock — PID-based file lock for single-owner execution

For database-backed storage (multi-tenant, Prisma), use @amaster.ai/pi-storage/scheduler which implements the same interfaces with DbScheduledTaskStore and RedisSchedulerLock.

Default (file storage)

import {
  PersistentTaskScheduler,
  JsonScheduledTaskStore,
  FileSchedulerLock,
} from "@amaster.ai/pi-task-scheduler";

const scheduler = new PersistentTaskScheduler({
  store: new JsonScheduledTaskStore("/var/lib/pi/tasks.json"),
  lock: new FileSchedulerLock("/var/lib/pi/tasks.lock"),
  runner: async (task, run) => {
    await runPrompt(task.prompt, { sessionId: run.sessionId });
  },
});

await scheduler.start();

Custom storage

Implement ScheduledTaskStore and SchedulerLock interfaces for any backend (database, Redis, etc.):

import { PersistentTaskScheduler } from "@amaster.ai/pi-task-scheduler";

const scheduler = new PersistentTaskScheduler({
  store: myCustomStore,   // implements ScheduledTaskStore
  lock: myCustomLock,     // implements SchedulerLock
  runner: async (task, run) => {
    await runPrompt(task.prompt, { sessionId: run.sessionId });
  },
});

await scheduler.start();

Integration Modes

The package provides two integration paths depending on who controls the scheduler lifecycle.

Mode 1: Extension auto-discovery (standalone / CLI)

When installed as an npm dependency with the pi.extensions field in package.json, the runtime auto-discovers and loads the extension. The extension creates its own PersistentTaskScheduler with file-based storage (JsonScheduledTaskStore + FileSchedulerLock) — fully self-contained, no external infrastructure needed.

pi.extensions → session_start → creates file-based scheduler → registers tools + commands
  • Storage: per-session tasks-<session-hash>.json and scheduler-<session-hash>.lock files under <agentDir>/data (base directory configurable via dataDir)
  • Upgrade: matching tasks from the legacy tasks.json are copied on first use; the legacy file is retained
  • Lifecycle: scheduler starts on session_start, stops on session_shutdown
  • Commands: /pi-scheduler-status, /pi-scheduler-list, /pi-scheduler-run-now
  • LLM Tools: scheduler_create, scheduler_list, scheduler_get, scheduler_update, scheduler_delete, scheduler_run_now

Configuration via settings key pi-scheduler:

Project .pi/settings.json values are loaded only after project trust is accepted and are not environment-interpolated. User and agent settings retain environment interpolation.

{
  "pi-scheduler": {
    "dataDir": "/custom/path"
  }
}

Mode 2: Dependency import (host-controlled)

When the host process owns the scheduler instance (e.g. backed by a database), it bypasses the extension and registers tools directly using createSchedulerTools:

import { createSchedulerTools } from "@amaster.ai/pi-task-scheduler/tools";
import { PersistentTaskScheduler } from "@amaster.ai/pi-task-scheduler";

const scheduler = new PersistentTaskScheduler({
  store: myStore,
  lock: myLock,
  runner,
});
await scheduler.start();

// Returns ToolDefinition[] — register via your tool injection mechanism
const tools = createSchedulerTools(scheduler);

In this mode the auto-discovered extension should be filtered out to prevent double tool registration. The host manages scheduler start/stop independently.

host creates scheduler → createSchedulerTools(scheduler) → tools injected into runtime
                       → extension filtered from auto-discovery

Choosing a mode

Extension (Mode 1)Dependency (Mode 2)
StorageFile-based (JSON)Host-controlled
Scheduler lifecycleExtension managesHost manages
Tool registrationExtension registersHost registers
Multi-tenantNoYes
Use caseCLI / standalone agentServer / custom host

Scheduling

Tasks support three schedule types:

  • interval: duration strings such as 30s, 10m, 1h, or 1d
  • once: ISO timestamps or relative values such as +5m
  • cron: 5/6-field cron expressions and a small RRULE subset

The scheduler does not catch up missed runs after process downtime. When it starts, it registers future timers for enabled tasks.

Hooks

Hooks are best-effort observability callbacks. If a hook throws, the scheduler swallows the error and keeps task state authoritative.

Structured results (Pi 0.99+)

Scheduler tools declare outputSchema. List returns { tasks, total, truncated }; create/get/update/run_now return { task, truncated }; delete returns { taskId, deleted, truncated }. Structured task prompts are complete when the task record is not marked truncated, even when the readable summary shows only the first 100 characters. Oversized task records omit the prompt and preserve the task ID for updates/deletion. Get includes stored task metadata and run-history identifiers/status/timestamps, excluding raw execution error messages. Missing tasks, invalid schedules, and backend failures return { error, truncated } with isError: true. The existing session scope applies equally to scripts.

const result = await tools.scheduler_list({});
if (result.error || result.truncated) throw new Error("Inspect the complete task list first");
for (const task of result.tasks.filter(t => t.enabled && t.name === "Daily check")) {
  text(await tools.scheduler_update({ taskId: task.id, enabled: false }));
}

Structured output is limited to 12 KiB and top-level lists to 100 complete records. truncated: true indicates omitted records; total retains the original count where available. Oversized records with an id retain that complete ID and small scalar metadata, omit large fields, and carry truncated: true both on the record and envelope; callers must check that flag before reusing content for an update. Oversized results without a record ID return a typed output-limit error instead of clipped paths or content. A write may already have completed when that happens: inspect saved state before retrying. Readable text and renderer details are independently bounded.

版本与发布记录

@amaster.ai/pi-task-scheduler 共发布 132 个版本,首次发布于 2026/05/16;过去 12 个月发布了 132 个版本。

版本发布日期距今
0.1.37latest2026/11/09今天
0.1.2-beta.91beta2026/11/072 天前
0.1.2-beta.902026/11/062 天前
0.1.2-beta.892026/11/053 天前
0.1.2-beta.882026/11/054 天前
0.1.362026/11/054 天前

关于 @amaster.ai/pi-task-scheduler 的常见问题

@amaster.ai/pi-task-scheduler 是什么?

Pi extension for cron-based scheduled task management with LLM-callable tools

如何安装 @amaster.ai/pi-task-scheduler?

在项目目录运行 npm install @amaster.ai/pi-task-scheduler,或使用 pnpm add / yarn add / bun add。

@amaster.ai/pi-task-scheduler 支持 TypeScript 吗?

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

@amaster.ai/pi-task-scheduler 支持 ESM 还是 CommonJS?

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

@amaster.ai/pi-task-scheduler 可以商用吗?

@amaster.ai/pi-task-scheduler 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@amaster.ai/pi-task-scheduler 的下载量有多少?

根据 npm 官方下载统计,@amaster.ai/pi-task-scheduler 在 2026/10/06 – 2026/11/04 这 30 天内被下载 5,253 次,日均约 175 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@amaster.ai/pi-task-scheduler 还在维护吗?

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