@apeira/storage

中文文档
0.0.8发布于 2026/08/09MITTypeScriptES Module零依赖构建来源已验证近 30 天下载 9.3万

Persistent storage adapters for Apeira agents.

原始使用文档
文档目录6 节
  1. Install
  2. JSON
  3. JSONL
  4. Key-value storage
  5. Storage lifecycle
  6. Generic storage

@apeira/storage

Persistent AgentStorage implementations for Apeira.

Install

pnpm add @apeira/storage

JSON

Stores the complete entry array in a JSON file. Each append rewrites the file.

import { createAgent } from '@apeira/core'
import { json } from '@apeira/storage/json'

const agent = createAgent({
  instructions: 'You are a helpful assistant.',
  runner,
  storage: json({ path: './data/agent.json' }),
})

Use JSON storage when human-readable files are useful and the history is relatively small.

JSONL

Stores one entry per line and appends new entries without rewriting existing content.

import { createAgent } from '@apeira/core'
import { jsonl } from '@apeira/storage/jsonl'

const agent = createAgent({
  instructions: 'You are a helpful assistant.',
  runner,
  storage: jsonl({ path: './data/agent.jsonl' }),
})

JSONL is the better file format for long-running, append-heavy agents.

Key-value storage

Adapts a string key-value backend such as Web Storage or an asynchronous storage API. Entries are split into segments to avoid storing the complete log under one key.

import type { StorageLike } from '@apeira/storage/kv'

import { createAgent } from '@apeira/core'
import { kv } from '@apeira/storage/kv'

declare const backend: StorageLike

const agent = createAgent({
  instructions: 'You are a helpful assistant.',
  runner,
  storage: kv({
    backend,
    prefix: 'assistant',
    segmentSize: 100,
  }),
})

The backend must implement:

import type { MaybePromise } from '@apeira/core'

interface StorageLike {
  getItem: (key: string) => MaybePromise<null | string | undefined>
  removeItem: (key: string) => MaybePromise<void>
  setItem: (key: string, value: string) => MaybePromise<void>
}

prefix defaults to apeira and segmentSize defaults to 100.

Storage lifecycle

All implementations provide the standard AgentStorage operations:

import type { MaybePromise } from '@apeira/core'

interface AgentStorage<T> {
  append: (...items: T[]) => MaybePromise<void>
  clear: () => MaybePromise<void>
  read: () => MaybePromise<Readonly<T[]>>
}

Storage only manages persisted entries. Agent initialization and reset baselines belong to core:

const agent = createAgent({
  initialInput: [user('Existing context')],
  initialState: { userId: 'user-123' },
  instructions: 'You are a helpful assistant.',
  runner,
  storage: jsonl({ path: './data/agent.jsonl' }),
})

During initialization, core writes initialInput only when storage contains no input entries. agent.reset() clears storage and restores initialInput and initialState.

Generic storage

The storage functions default to AgentEntry, but can store another JSON-serializable type:

const store = jsonl<string>({ path: './data/items.jsonl' })

await store.append('first', 'second')
const items = await store.read()

JSON and JSONL storage require Node.js. Key-value storage works in any runtime that provides a compatible backend.

版本与发布记录

@apeira/storage 共发布 3 个版本,首次发布于 2026/06/19;过去 12 个月发布了 3 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.0.8latest2026/08/092 个月前
0.0.72026/07/143 个月前
0.0.62026/06/194 个月前

关于 @apeira/storage 的常见问题

@apeira/storage 是什么?

Persistent storage adapters for Apeira agents.

如何安装 @apeira/storage?

在项目目录运行 npm install @apeira/storage,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:@apeira/core(0.0.8)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@apeira/storage 支持 TypeScript 吗?

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

@apeira/storage 支持 ESM 还是 CommonJS?

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

@apeira/storage 可以商用吗?

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

@apeira/storage 的下载量有多少?

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

@apeira/storage 还在维护吗?

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