Skip to content

はじめる

Atom Memory は Node.js 22.13 以降の ESM ライブラリです。ランタイム依存パッケージはありません。

sh
npm install atom-memory

最初の read / write

この例はリポジトリの npm run example で実行できます。

mjs
import {
  AtomKernel,
  LocalAuthority,
  content,
  membership,
  logical,
  defaultBudget,
} from 'atom-memory';

const authority = new LocalAuthority();
const auth = authority.issue({
  subject: 'example-host',
  readPolicies: ['notes'],
  writePolicies: ['notes'],
  canIngestSource: true,
});
const memory = new AtomKernel({ authority });

await memory.write(
  {
    idempotencyKey: 'example:seed',
    guards: [],
    revisions: [
      {
        atomId: 'note',
        revisionId: 'note:1',
        expectedHead: null,
        content: content('source', 'Atom の所属は独立した Atom で表す。', 'notes'),
      },
      {
        atomId: 'topic',
        revisionId: 'topic:1',
        expectedHead: null,
        content: content('collection', { form: 'set', title: '設計ノート' }, 'notes'),
      },
      {
        atomId: 'link',
        revisionId: 'link:1',
        expectedHead: null,
        content: membership('topic', 'note', 'notes'),
      },
    ],
  },
  auth,
);

const result = await memory.read(
  {
    selector: { kind: 'relations', target: logical('topic'), role: 'group', schema: 'membership' },
    context: { requestedPolicyIds: ['notes'], consistency: { mode: 'snapshot' } },
    budget: defaultBudget,
    render: 'evidence',
  },
  auth,
);
console.log(result.atoms.map((atom) => `${atom.atomId}@${atom.revisionId}`).join('\n'));
console.log(result.diagnostics);

LocalAuthority はホストが持つ権限管理です。認証済みの利用者を確認した後にハンドルを発行してください。モデルや HTTP リクエストから issue() を自由に呼べるようにしないでください。

canIngestSource は原資料として記録する権限です。Writer が生成した内容には organizationextractionderived などの区分を使います。ハーネスは入力を読んだ receipt を差分へ付け、生成物を source と申告する操作を拒否します。

SQLite に保存する

ts
import { AtomKernel, LocalAuthority } from 'atom-memory';
import { SqliteStorage } from 'atom-memory/sqlite';

const storage = new SqliteStorage('./memory.sqlite');
const authority = new LocalAuthority();
const memory = new AtomKernel({ storage, authority });

// read / write は同じ API
// 終了時:
storage.close();

SQLite は版、読取 receipt、cursor、冪等キー、blob、埋め込みを永続化します。LocalAuthority のハンドルはプロセス内の権限です。再起動をまたぐ認証は、アプリ側の Authorizer を実装してください。永続化した DB に、権限自体が自動的に付与されることはありません。

次に読む

MIT License · TypeScript / Node.js