0g-memory-sdk

中文文档
0.1.2发布于 2026/05/17MITTypeScriptESM + CJS

Persistent, verifiable memory for AI agents — backed by 0G Storage (KV + Log) and 0G Chain

原始使用文档
文档目录20 节
  1. Why
  2. Quick start
  3. API
  4. new ZeroGMemory(config)
  5. memory.remember(key, value) → Promise<string>
  6. memory.recall(key) → Promise<unknown | null>
  7. memory.recallByHash(rootHash) → Promise<unknown | null>
  8. memory.archive(record) → Promise<{ rootHash, storageScanUrl…
  9. memory.updateClientContext(clientName, context) → Promise<s…
  10. deriveAgentId(name, privateKey) → AgentIdentity
  11. isValidAgentId(agentId) → boolean
  12. Configuration reference
  13. Patterns
  14. Session memory across restarts
  15. Returning-client detection
  16. Verifiable audit trail
  17. LangChain / agent framework integration
  18. Consistency model
  19. Reference application
  20. License

0G-Memory

0G-Memory SDK

npm version license

Persistent, verifiable memory for AI agents — backed by 0G Storage (KV + Log layers) and 0G Chain.

Agents built with this SDK remember clients, context, and history across sessions — from any machine, without a database.

npm install 0g-memory-sdk

Why

AI agents forget everything when a session ends. Context lives in RAM, databases owned by third parties, or not at all. There is no cryptographic proof that stored data was not modified.

0g-memory-sdk gives any agent three primitives that run entirely on the 0G decentralized network:

MethodWhat it does
remember(key, value)Writes permanently to 0G Log (immutable, Merkle-provable) and registers a key → rootHash pointer in 0G KV so it is retrievable from any machine
recall(key)Resolves the key via the KV index, fetches the value from the Log layer
archive(record)Writes typed business records and returns a StorageScan URL as a verifiable on-chain proof

Because the key index lives on 0G — not on disk — any agent instance with the same private key and agentId gets full memory across machines, restarts, and serverless deployments.


Quick start

import { ZeroGMemory, deriveAgentId } from '0g-memory-sdk';

const { agentId } = deriveAgentId('my-agent', process.env.PRIVATE_KEY!);

const memory = new ZeroGMemory({
  agentId,
  privateKey: process.env.PRIVATE_KEY!,
  rpcUrl:     'https://evmrpc-testnet.0g.ai',
  indexerRpc: 'https://indexer-storage-testnet-standard.0g.ai',
});

// Store something
const rootHash = await memory.remember('user:alice', { plan: 'pro', credits: 42 });

// Retrieve later — from any machine
const data = await memory.recall('user:alice');
// → { plan: 'pro', credits: 42 }

// Or retrieve immediately by root hash (before KV index settles on-chain)
const same = await memory.recallByHash(rootHash);

Testnet tokens: Get OG from the 0G faucet to pay for storage transactions.


API

new ZeroGMemory(config)

const memory = new ZeroGMemory({
  agentId:    string,  // unique agent identifier — use deriveAgentId()
  privateKey: string,  // wallet private key, pays for 0G storage txs
  rpcUrl:     string,  // 0G chain RPC endpoint
  indexerRpc: string,  // 0G storage indexer endpoint
});

ChainMemory is exported as an alias of ZeroGMemory for backward compatibility.


memory.remember(key, value) → Promise<string>

Writes value to the 0G Log layer (permanent, content-addressed) and fires a concurrent KV write to register key → rootHash in the distributed index. Returns the root hash.

const rootHash = await memory.remember('session:xyz', {
  messages: [...],
  summary: 'User asked about pricing.',
});

The KV index write settles within a few 0G blocks. Use recallByHash(rootHash) for same-request access before it settles.


memory.recall(key) → Promise<unknown | null>

Looks up key in the 0G KV index, then fetches the value from the Log layer. Returns null if the key was never written or the KV index has not yet settled.

Falls back automatically to the legacy local-file index (.chainmemory/) for data written by earlier SDK versions, and migrates those entries to KV on first access.

const context = await memory.recall('session:xyz');
if (context) {
  // resume where the agent left off
}

memory.recallByHash(rootHash) → Promise<unknown | null>

Fetches a value directly from the 0G Log layer by its root hash, bypassing the KV index entirely. Use this immediately after remember() when you cannot wait for the index to settle.

const rootHash = await memory.remember('key', data);
const same     = await memory.recallByHash(rootHash); // always consistent

memory.archive(record) → Promise<{ rootHash, storageScanUrl }>

Writes a typed, permanent record to the 0G Log layer. Returns the content-address root hash and a StorageScan explorer URL that anyone can use to verify the data was not modified.

const { rootHash, storageScanUrl } = await memory.archive({
  type: 'invoice',   // 'invoice' | 'payment' | 'interaction' | 'custom'
  data: invoice,
});

console.log(storageScanUrl);
// https://storagescan-newton.0g.ai/tx/0xabc...

memory.updateClientContext(clientName, context) → Promise<string>

Convenience wrapper that normalises clientName to a key (client:<name>) and calls remember(). Designed for agents that serve multiple clients and need per-client persistent state. Returns the root hash.

const rootHash = await memory.updateClientContext('Alice Johnson', {
  totalInvoiced: 4200,
  invoiceCount:  3,
  lastJobDescription: 'Frontend redesign — 40 hrs at $120/hr',
});

// Recall in a future session:
const alice = await memory.recall('client:alice_johnson');

deriveAgentId(name, privateKey) → AgentIdentity

Derives a deterministic, collision-resistant agent ID from a human-readable name and a wallet private key. Format: <name>:<first-8-hex-chars-of-address>.

import { deriveAgentId } from '0g-memory-sdk';

const { agentId, walletAddress } = deriveAgentId('billing-agent', process.env.PRIVATE_KEY!);
// agentId:       "billing-agent:a3f8c201"
// walletAddress: "0xA3F8..."

isValidAgentId(agentId) → boolean

Validates that a string matches the <name>:<8-hex-chars> format.


Configuration reference

FieldDescriptionTestnet value
agentIdUnique agent identifierderiveAgentId('my-agent', privateKey).agentId
privateKeyWallet private key — pays gas and storage feesYour Galileo testnet key
rpcUrl0G chain JSON-RPC endpointhttps://evmrpc-testnet.0g.ai
indexerRpc0G storage indexer endpointhttps://indexer-storage-testnet-standard.0g.ai

Patterns

Session memory across restarts

async function chat(sessionId: string, userMessage: string) {
  const history = (await memory.recall(`session:${sessionId}`)) as Message[] ?? [];

  const reply = await llm.complete([
    ...history,
    { role: 'user', content: userMessage },
  ]);

  await memory.remember(`session:${sessionId}`, [
    ...history,
    { role: 'user',      content: userMessage },
    { role: 'assistant', content: reply },
  ]);

  return reply;
}

Returning-client detection

async function greet(clientName: string) {
  const ctx = await memory.recall(`client:${clientName.toLowerCase()}`);
  return ctx
    ? `Welcome back — you've invoiced $${ctx.totalInvoiced} across ${ctx.invoiceCount} jobs.`
    : `Welcome! Let's set up your first invoice.`;
}

Verifiable audit trail

const { rootHash, storageScanUrl } = await memory.archive({
  type: 'invoice',
  data: { id: 'INV-042', client: 'Acme Corp', total: 6000, currency: 'USD' },
});

// Share storageScanUrl — anyone can verify the invoice was not tampered with

LangChain / agent framework integration

import { ZeroGMemory, deriveAgentId } from '0g-memory-sdk';

const { agentId } = deriveAgentId('langchain-agent', process.env.PRIVATE_KEY!);
const memory = new ZeroGMemory({ agentId, privateKey: ..., rpcUrl: ..., indexerRpc: ... });

// Use as a custom memory store
const loadMemory = async (key: string) => memory.recall(key);
const saveMemory = async (key: string, value: unknown) => memory.remember(key, value);

Consistency model

PatternConsistencyWhen to use
remember() → recallByHash()ImmediateSame request, same session
remember() → recall()Eventual (few blocks, ~seconds)Cross-session, cross-machine
archive()Write-once, permanentInvoices, audit trails, proof records

recall() automatically falls back to the local .chainmemory/ index for data written by older SDK versions and migrates those entries to the KV layer transparently.


Reference application

AgentBill is the reference implementation — an autonomous invoicing agent built with this SDK on the full 0G stack:

  • 0G Compute — Qwen-2.5-7B parses natural-language job descriptions into structured invoices
  • 0G Memory — per-client context recalled across sessions via recall() / updateClientContext()
  • 0G Storage — each invoice archived permanently via archive(), root hash returned as proof
  • 0G Chain — invoice registered and paid on-chain via an escrow smart contract

Source: github.com/anbusan19/0g-memory


License

MIT

版本与发布记录

0g-memory-sdk 共发布 3 个版本,首次发布于 2026/05/16;过去 12 个月发布了 3 个版本。

版本发布日期距今
0.1.2latest2026/05/175 个月前
0.1.12026/05/165 个月前
0.1.02026/05/165 个月前

关于 0g-memory-sdk 的常见问题

0g-memory-sdk 是什么?

为人工智能代理提供持久、可验证的内存——由0G Storage (KV + Log)和0G Chain支持

如何安装 0g-memory-sdk?

在项目目录运行 npm install 0g-memory-sdk,或使用 pnpm add / yarn add / bun add。

0g-memory-sdk 支持 TypeScript 吗?

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

0g-memory-sdk 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

0g-memory-sdk 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=18。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

0g-memory-sdk 可以商用吗?

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

0g-memory-sdk 还在维护吗?

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