Skip to content
This repository was archived by the owner on Sep 14, 2026. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 28 additions & 22 deletions bun.lock

Large diffs are not rendered by default.

25 changes: 12 additions & 13 deletions examples/agent-research/tools/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ import { homedir } from "node:os";
import { join } from "node:path";

import { createOrbitMem, getNetwork, type NetworkId } from "@orbitmem/sdk";
import { createOwsAdapter } from "@orbitmem/sdk/identity";
import { createPublicClient, createWalletClient, http } from "viem";
import { privateKeyToAccount } from "viem/accounts";
import { baseSepolia } from "viem/chains";

interface Config {
walletName: string;
network: NetworkId;
relay: string;
}
Expand All @@ -23,36 +24,34 @@ function getConfigDir(): string {
function loadConfig(): Config {
const configPath = join(getConfigDir(), "config.json");
const network = getNetwork();
const defaults: Config = { network: "base-sepolia", relay: network.relayUrl };
const defaults: Config = { walletName: "", network: "base-sepolia", relay: network.relayUrl };
if (!existsSync(configPath)) return defaults;
const raw = JSON.parse(readFileSync(configPath, "utf-8"));
return { ...defaults, ...raw };
}

function loadKey(): string {
const keyPath = join(getConfigDir(), "key.json");
if (!existsSync(keyPath)) {
throw new Error("No key found. Run `bun run cli init` first.");
}
return JSON.parse(readFileSync(keyPath, "utf-8")).privateKey;
}

export async function createAgentClient() {
const config = loadConfig();
if (!config.walletName) {
throw new Error("No wallet configured. Run `bun run cli init` first.");
}

const network = getNetwork(config.network);
const transport = http(network.rpcUrl);
const account = privateKeyToAccount(loadKey() as `0x${string}`);

const adapter = createOwsAdapter(config.walletName, `eip155:${baseSepolia.id}`);
const viemAccount = await adapter.toViemAccount();

const publicClient = createPublicClient({ chain: baseSepolia, transport });
const walletClient = createWalletClient({
chain: baseSepolia,
transport,
account,
account: viemAccount,
});

const client = await createOrbitMem({
network: config.network,
identity: { privateKey: loadKey() },
identity: { owsWallet: config.walletName },
vault: { dbName: "orbitmem-agent-research" },
encryption: { defaultEngine: "aes", aes: { kdf: "hkdf-sha256" } },
persistence: { relayUrl: config.relay },
Expand Down
3 changes: 2 additions & 1 deletion packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@
},
"dependencies": {
"@hono/node-server": "^1.13.0",
"@orbitmem/sdk": "workspace:*",
"@open-wallet-standard/core": "^1.1.0",
"@orbitmem/relay": "workspace:*",
"@orbitmem/sdk": "workspace:*",
"viem": "^2.0.0"
},
"devDependencies": {
Expand Down
74 changes: 48 additions & 26 deletions packages/cli/src/__tests__/cli.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,19 @@ import { join } from "node:path";
const TEST_DIR = join(import.meta.dir, ".test-orbitmem-cli");
const CLI = join(import.meta.dir, "..", "index.ts");

// Unique wallet name per test run to avoid OWS keystore conflicts
const RUN_ID = Date.now().toString(36);
const WALLET_NAME = `orbitmem-cli-test-${RUN_ID}`;

async function deleteTestWallet(name: string): Promise<void> {
try {
const { deleteWallet } = await import("@open-wallet-standard/core");
deleteWallet(name);
} catch {
// ignore — wallet may not exist
}
}

describe("CLI integration", () => {
beforeEach(() => {
mkdirSync(TEST_DIR, { recursive: true });
Expand All @@ -27,36 +40,45 @@ describe("CLI integration", () => {
});

test("init --json creates config and returns address", async () => {
const proc = Bun.spawn(["bun", CLI, "init", "--json"], {
env: { ...process.env, ORBITMEM_HOME: TEST_DIR },
stdout: "pipe",
});
const text = await new Response(proc.stdout).text();
await proc.exited;
const result = JSON.parse(text);
expect(result.address).toMatch(/^0x[0-9a-fA-F]{40}$/);
expect(result.network).toBe("base-sepolia");
expect(result.relay).toBe("https://orbitmem-relay.fly.dev");
try {
const proc = Bun.spawn(["bun", CLI, "init", "--json", "--name", WALLET_NAME], {
env: { ...process.env, ORBITMEM_HOME: TEST_DIR },
stdout: "pipe",
});
const text = await new Response(proc.stdout).text();
await proc.exited;
const result = JSON.parse(text);
expect(result.address).toMatch(/^0x[0-9a-fA-F]{40}$/);
expect(result.network).toBe("base-sepolia");
expect(result.relay).toBe("https://orbitmem-relay.fly.dev");
} finally {
await deleteTestWallet(WALLET_NAME);
}
});

test("status --json after init shows config", async () => {
// First init
const initProc = Bun.spawn(["bun", CLI, "init"], {
env: { ...process.env, ORBITMEM_HOME: TEST_DIR },
stdout: "pipe",
});
await initProc.exited;
const walletName = `${WALLET_NAME}-status`;
try {
// First init
const initProc = Bun.spawn(["bun", CLI, "init", "--name", walletName], {
env: { ...process.env, ORBITMEM_HOME: TEST_DIR },
stdout: "pipe",
});
await initProc.exited;

// Then status
const proc = Bun.spawn(["bun", CLI, "status", "--json"], {
env: { ...process.env, ORBITMEM_HOME: TEST_DIR },
stdout: "pipe",
});
const text = await new Response(proc.stdout).text();
await proc.exited;
const result = JSON.parse(text);
expect(result.address).toMatch(/^0x/);
expect(result.chain).toBe("base-sepolia");
// Then status
const proc = Bun.spawn(["bun", CLI, "status", "--json"], {
env: { ...process.env, ORBITMEM_HOME: TEST_DIR },
stdout: "pipe",
});
const text = await new Response(proc.stdout).text();
await proc.exited;
const result = JSON.parse(text);
expect(result.address).toMatch(/^0x/);
expect(result.chain).toBe("base-sepolia");
} finally {
await deleteTestWallet(walletName);
}
});

test("unknown command exits with error", async () => {
Expand Down
14 changes: 5 additions & 9 deletions packages/cli/src/__tests__/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { afterEach, beforeEach, describe, expect, test } from "bun:test";
import { mkdirSync, rmSync } from "node:fs";
import { join } from "node:path";

import { loadConfig, loadKey, saveConfig, saveKey } from "../config.js";
import { loadConfig, saveConfig } from "../config.js";

const TEST_DIR = join(import.meta.dir, ".test-orbitmem");

Expand Down Expand Up @@ -31,13 +31,9 @@ describe("config", () => {
expect(config.chain).toBe("base-sepolia");
});

test("saveKey and loadKey roundtrip", () => {
saveKey("0xabc123");
const key = loadKey();
expect(key).toBe("0xabc123");
});

test("loadKey throws when no key exists", () => {
expect(() => loadKey()).toThrow("No key found");
test("saveConfig persists walletName", () => {
saveConfig({ walletName: "my-wallet" });
const loaded = loadConfig();
expect(loaded.walletName).toBe("my-wallet");
});
});
55 changes: 36 additions & 19 deletions packages/cli/src/__tests__/init.test.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,58 @@
import { afterEach, beforeEach, describe, expect, test } from "bun:test";
import { existsSync, mkdirSync, rmSync } from "node:fs";
import { mkdirSync, rmSync } from "node:fs";
import { join } from "node:path";

import { init } from "../commands/init.js";
import { loadConfig, loadKey } from "../config.js";
import { loadConfig } from "../config.js";

const TEST_DIR = join(import.meta.dir, ".test-orbitmem-init");

// Use a unique wallet name per test run to avoid OWS keystore conflicts
const RUN_ID = Date.now().toString(36);

async function deleteTestWallet(name: string): Promise<void> {
try {
const { deleteWallet } = await import("@open-wallet-standard/core");
deleteWallet(name);
} catch {
// ignore — wallet may not exist
}
}

describe("init", () => {
beforeEach(() => {
process.env.ORBITMEM_HOME = TEST_DIR;
mkdirSync(TEST_DIR, { recursive: true });
});

afterEach(() => {
afterEach(async () => {
// Clean up config dir
rmSync(TEST_DIR, { recursive: true, force: true });
delete process.env.ORBITMEM_HOME;
});

test("creates config and key files", async () => {
await init([], {});
expect(existsSync(join(TEST_DIR, "config.json"))).toBe(true);
expect(existsSync(join(TEST_DIR, "key.json"))).toBe(true);
});

test("generated key is a valid hex private key", async () => {
await init([], {});
const key = loadKey();
expect(key).toMatch(/^0x[0-9a-f]{64}$/);
test("creates config file with walletName", async () => {
const walletName = `test-init-a-${RUN_ID}`;
try {
await init([], { name: walletName });
const config = loadConfig();
expect(config.walletName).toBe(walletName);
} finally {
await deleteTestWallet(walletName);
}
});

test("config has default network and relay", async () => {
await init([], {});
const config = loadConfig();
expect(config.network).toBe("base-sepolia");
expect(config.relay).toBe("https://orbitmem-relay.fly.dev");
expect(config.registryAddress).toBe("0x9eE44938ED77227470CaA2DbCC0459F49d249B7A");
expect(config.reputationAddress).toBe("0x1Bce77f90C33A5f8faCa54782Ce3a17d1AD7109a");
const walletName = `test-init-b-${RUN_ID}`;
try {
await init([], { name: walletName });
const config = loadConfig();
expect(config.network).toBe("base-sepolia");
expect(config.relay).toBe("https://orbitmem-relay.fly.dev");
expect(config.registryAddress).toBe("0x9eE44938ED77227470CaA2DbCC0459F49d249B7A");
expect(config.reputationAddress).toBe("0x1Bce77f90C33A5f8faCa54782Ce3a17d1AD7109a");
} finally {
await deleteTestWallet(walletName);
}
});
});
4 changes: 2 additions & 2 deletions packages/cli/src/commands/discover.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { loadConfig, loadKey } from "../config.js";
import { loadConfig } from "../config.js";
import { createClient } from "../utils/client.js";
import { error, output } from "../utils/output.js";

Expand All @@ -8,7 +8,7 @@ export async function discover(args: string[], flags: Record<string, string>): P
if (!config.registryAddress)
error("No registry address configured. Set registryAddress in ~/.orbitmem/config.json");

const client = await createClient(config, loadKey());
const client = await createClient(config);

try {
const query: Record<string, unknown> = {};
Expand Down
35 changes: 21 additions & 14 deletions packages/cli/src/commands/init.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,39 @@
import { existsSync } from "node:fs";
import { join } from "node:path";

import { createWallet } from "@open-wallet-standard/core";
import type { NetworkId } from "@orbitmem/sdk/contracts";
import { generatePrivateKey, privateKeyToAccount } from "viem/accounts";
import { createOwsAdapter } from "@orbitmem/sdk/identity";

import { getConfigDir, loadConfig, saveConfig, saveKey } from "../config.js";
import { getConfigDir, loadConfig, saveConfig, toCaip2 } from "../config.js";
import { output } from "../utils/output.js";

export async function init(_args: string[], flags: Record<string, string>): Promise<void> {
const configDir = getConfigDir();
const keyPath = join(configDir, "key.json");

if (existsSync(keyPath) && flags.force === undefined) {
process.stderr.write(`Already initialized at ${configDir}. Use --force to reinitialize.\n`);
process.exit(1);
const configPath = join(configDir, "config.json");

if (existsSync(configPath) && flags.force === undefined) {
const existing = loadConfig();
if (existing.walletName) {
process.stderr.write(`Already initialized at ${configDir}. Use --force to reinitialize.\n`);
process.exit(1);
}
}

const privateKey = generatePrivateKey();
const account = privateKeyToAccount(privateKey);
const walletName = flags.name ?? "orbitmem";
createWallet(walletName);

const network = (flags.network ?? "base-sepolia") as NetworkId;

saveKey(privateKey);
saveConfig({ network });
saveConfig({ walletName, network });

const config = loadConfig();
const caip2 = toCaip2(network);
const adapter = createOwsAdapter(walletName, caip2);
const address = await adapter.getAddress();

const info = {
address: account.address,
wallet: walletName,
address,
configDir,
network: config.network,
relay: config.relay,
Expand All @@ -39,7 +46,7 @@ export async function init(_args: string[], flags: Record<string, string>): Prom
output(info, true);
} else {
process.stdout.write(`\nOrbitMem initialized!\n\n`);
process.stdout.write(` Address: ${info.address}\n`);
process.stdout.write(` Wallet: ${info.wallet}\n`);
process.stdout.write(` Config: ${info.configDir}\n`);
process.stdout.write(` Network: ${info.network}\n`);
process.stdout.write(` Relay: ${info.relay}\n`);
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/src/commands/register.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { loadConfig, loadKey } from "../config.js";
import { loadConfig } from "../config.js";
import { createClient } from "../utils/client.js";
import { error, output } from "../utils/output.js";

Expand All @@ -11,7 +11,7 @@ export async function register(args: string[], flags: Record<string, string>): P
if (!config.registryAddress)
error("No registry address configured. Set registryAddress in ~/.orbitmem/config.json");

const client = await createClient(config, loadKey());
const client = await createClient(config);

try {
const entry = await client.vault.get(path);
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/src/commands/snapshot.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { loadConfig, loadKey } from "../config.js";
import { loadConfig } from "../config.js";
import { createClient } from "../utils/client.js";
import { output } from "../utils/output.js";

export async function snapshot(_args: string[], flags: Record<string, string>): Promise<void> {
const config = loadConfig();
if (flags.relay) config.relay = flags.relay;
const client = await createClient(config, loadKey());
const client = await createClient(config);

try {
const result = await client.persistence.archive({
Expand Down
Loading