Skip to content
Open
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
12 changes: 6 additions & 6 deletions apps/memos-local-openclaw/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,7 @@ const memosLocalPlugin = {
// ─── Tool: memory_search ───

api.registerTool(
(context) => ({
(context: any) => ({
name: "memory_search",
label: "Memory Search",
description:
Expand Down Expand Up @@ -758,7 +758,7 @@ const memosLocalPlugin = {
// ─── Tool: memory_timeline ───

api.registerTool(
(context) => ({
(context: any) => ({
name: "memory_timeline",
label: "Memory Timeline",
description:
Expand Down Expand Up @@ -819,7 +819,7 @@ const memosLocalPlugin = {
// ─── Tool: memory_get ───

api.registerTool(
(context) => ({
(context: any) => ({
name: "memory_get",
label: "Memory Get",
description:
Expand Down Expand Up @@ -1124,7 +1124,7 @@ ${detail.content}`,
};
}

const groupNames = status.user.groups.map((group) => group.name);
const groupNames = (status.user.groups ?? []).map((group: { name: string }) => group.name);
return {
content: [{
type: "text",
Expand Down Expand Up @@ -1382,7 +1382,7 @@ Groups: ${groupNames.length > 0 ? groupNames.join(", ") : "(none)"}`,
const viewerPort = (pluginCfg as any).viewerPort ?? (gatewayPort + 10);

api.registerTool(
(context) => ({
(context: any) => ({
name: "memory_viewer",
label: "Open Memory Viewer",
description:
Expand Down Expand Up @@ -1646,7 +1646,7 @@ Groups: ${groupNames.length > 0 ? groupNames.join(", ") : "(none)"}`,
// ─── Tool: skill_search ───

api.registerTool(
(context) => ({
(context: any) => ({
name: "skill_search",
label: "Skill Search",
description:
Expand Down
2 changes: 1 addition & 1 deletion apps/memos-local-openclaw/openclaw.plugin.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,5 @@
"If better-sqlite3 fails to build, ensure you have C++ build tools: xcode-select --install (macOS) or build-essential (Linux)"
]
},
"extensions": ["./index.ts"]
"extensions": ["./dist/index.js"]
}
12 changes: 6 additions & 6 deletions apps/memos-local-openclaw/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
"version": "1.0.9-beta.1",
"description": "MemOS Local memory plugin for OpenClaw — full-write, hybrid-recall, progressive retrieval",
"type": "module",
"main": "index.ts",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"files": [
"index.ts",
"src",
"dist",
"skill",
"prebuilds",
"scripts/native-binding.cjs",
Expand All @@ -19,22 +19,22 @@
"openclaw": {
"id": "memos-local-openclaw-plugin",
"extensions": [
"./index.ts"
"./dist/index.js"
],
"skills": [
"skill/memos-memory-guide"
],
"installDependencies": true
},
"scripts": {
"build": "tsc",
"build": "tsc && node scripts/fix-esm-imports.cjs",
"dev": "tsc --watch",
"lint": "eslint src --ext .ts",
"test": "vitest run",
"test:watch": "vitest",
"test:accuracy": "tsx scripts/run-accuracy-test.ts",
"postinstall": "node scripts/postinstall.cjs",
"prepublishOnly": "echo 'Source-only publish — no build needed.'"
"prepublishOnly": "rm -rf dist && tsc && node scripts/fix-esm-imports.cjs"
},
"keywords": [
"openclaw",
Expand Down
58 changes: 58 additions & 0 deletions apps/memos-local-openclaw/scripts/fix-esm-imports.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#!/usr/bin/env node
"use strict";

/**
* Post-build: append `.js` (or `/index.js`) to every relative ESM import in dist/.
*
* Why: tsconfig uses `moduleResolution: "bundler"` so source files don't need
* `.js` suffixes — but Node's native ESM loader requires explicit suffixes.
* OpenClaw's plugin loader runs the emitted JS through the standard loader,
* so we rewrite the imports here instead of polluting source with `.js`.
*/

const fs = require("fs");
const path = require("path");

const DIST = path.resolve(__dirname, "..", "dist");

// Matches relative module specifiers in:
// import ... from "X" / export ... from "X" (static import / re-export)
// import("X") (dynamic import)
// import "X" (side-effect import)
//
// `\bimport\s+['"]` requires whitespace between the keyword and the quote so
// it cannot collide with identifiers like `important`. Specifiers must start
// with `.` so absolute and bare-package imports are left untouched.
const IMPORT_RE = /(\bfrom\s*['"]|\bimport\s*\(\s*['"]|\bimport\s+['"])(\.[^'"]+)(['"])/g;

function rewriteSpec(spec, fileDir) {
if (/\.(m?js|cjs|json)$/.test(spec)) return spec;
const abs = path.resolve(fileDir, spec);
if (fs.existsSync(abs + ".js")) return spec + ".js";
if (fs.existsSync(path.join(abs, "index.js"))) return spec.replace(/\/?$/, "/index.js");
return spec;
}

let touched = 0;
function walk(dir) {
for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
const full = path.join(dir, entry.name);
if (entry.isDirectory()) { walk(full); continue; }
if (!entry.name.endsWith(".js")) continue;
const src = fs.readFileSync(full, "utf8");
let changed = false;
const out = src.replace(IMPORT_RE, (m, head, spec, tail) => {
const next = rewriteSpec(spec, path.dirname(full));
if (next !== spec) { changed = true; return head + next + tail; }
return m;
});
if (changed) { fs.writeFileSync(full, out); touched++; }
}
}

if (!fs.existsSync(DIST)) {
console.error("[fix-esm-imports] dist/ not found — run tsc first");
process.exit(1);
}
walk(DIST);
console.log(`[fix-esm-imports] rewrote imports in ${touched} file(s)`);
13 changes: 13 additions & 0 deletions apps/memos-local-openclaw/src/openclaw-sdk.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
declare module "openclaw/plugin-sdk" {
export interface OpenClawPluginApi {
registerTool(...args: any[]): void;
registerHook(...args: any[]): void;
registerMemoryCapability(...args: any[]): void;
registerService(...args: any[]): void;
getConfig(): any;
getLogger(): any;
config: any;
logger: any;
[key: string]: any;
}
}
44 changes: 44 additions & 0 deletions apps/memos-local-openclaw/src/shared/plugin-root.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import * as fs from "node:fs";
import * as path from "node:path";
import { fileURLToPath } from "node:url";

/**
* Exact package names recognized as this plugin's installation root.
*
* Kept narrow (not a substring match) so monorepo layouts — where sibling
* packages such as `memos-local-plugin` live next to `memos-local-openclaw` —
* can never accidentally lock onto the wrong root.
*
* The unscoped form covers historical / local checkouts; the scoped form is
* the published name on npm.
*/
const PLUGIN_PACKAGE_NAMES = new Set([
"memos-local-openclaw-plugin",
"@memtensor/memos-local-openclaw-plugin",
]);

/**
* Resolve the plugin's installation root by walking up from the caller's file
* until a `package.json` whose `name` matches one of {@link PLUGIN_PACKAGE_NAMES}
* is found.
*
* Necessary because the build emits to `dist/` with `rootDir: "."`, so
* compiled files live one extra level deep than their sources. Hard-coded
* `../../` paths break across dev (src/) vs published (dist/src/) layouts.
Comment thread
fancyboi999 marked this conversation as resolved.
*/
export function findPluginRoot(importMetaUrl: string): string {
let dir = path.dirname(fileURLToPath(importMetaUrl));
for (let i = 0; i < 8; i++) {
const pkgPath = path.join(dir, "package.json");
if (fs.existsSync(pkgPath)) {
try {
const pkg = JSON.parse(fs.readFileSync(pkgPath, "utf-8"));
if (typeof pkg.name === "string" && PLUGIN_PACKAGE_NAMES.has(pkg.name)) return dir;
} catch { /* keep walking */ }
}
const parent = path.dirname(dir);
if (parent === dir) break;
dir = parent;
}
throw new Error(`findPluginRoot: could not locate plugin root from ${importMetaUrl}`);
}
3 changes: 2 additions & 1 deletion apps/memos-local-openclaw/src/skill/bundled-memory-guide.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*/
import * as fs from "fs";
import * as path from "path";
import { findPluginRoot } from "../shared/plugin-root";

const skillPath = path.join(__dirname, "..", "..", "skill", "memos-memory-guide", "SKILL.md");
const skillPath = path.join(findPluginRoot(import.meta.url), "skill", "memos-memory-guide", "SKILL.md");
export const MEMORY_GUIDE_SKILL_MD: string = fs.readFileSync(skillPath, "utf-8");
6 changes: 5 additions & 1 deletion apps/memos-local-openclaw/src/storage/ensure-binding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ import { existsSync, mkdirSync, copyFileSync } from "fs";
import { execSync } from "child_process";
import path from "path";
import { createRequire } from "module";
import { fileURLToPath } from "node:url";
import { findPluginRoot } from "../shared/plugin-root";

const __filename = fileURLToPath(import.meta.url);

/**
* Ensure the better-sqlite3 native binary is available.
Expand All @@ -19,7 +23,7 @@ export function ensureSqliteBinding(log?: { info: (msg: string) => void; warn: (
if (existsSync(bindingPath)) return;

const platform = `${process.platform}-${process.arch}`;
const pluginRoot = path.resolve(__dirname, "..", "..");
const pluginRoot = findPluginRoot(import.meta.url);
const prebuildSrc = path.join(pluginRoot, "prebuilds", platform, "better_sqlite3.node");

if (existsSync(prebuildSrc)) {
Expand Down
3 changes: 2 additions & 1 deletion apps/memos-local-openclaw/src/telemetry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import * as path from "path";
import * as os from "os";
import { v4 as uuidv4 } from "uuid";
import type { Logger } from "./types";
import { findPluginRoot } from "./shared/plugin-root";

export interface TelemetryConfig {
enabled?: boolean;
Expand All @@ -27,7 +28,7 @@ function loadTelemetryCredentials(pluginDir?: string): { endpoint: string; pid:
};
}
const bases = pluginDir ? [pluginDir, path.join(pluginDir, "src")] : [];
if (typeof __dirname === "string") bases.push(path.resolve(__dirname, ".."), __dirname);
try { bases.push(findPluginRoot(import.meta.url)); } catch { /* not in a memos-local install */ }
const candidates = bases.map(b => path.join(b, "telemetry.credentials.json"));
for (const credPath of candidates) {
try {
Expand Down
18 changes: 6 additions & 12 deletions apps/memos-local-openclaw/src/viewer/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { execSync, exec, execFile } from "node:child_process";
import fs from "node:fs";
import path from "node:path";
import readline from "node:readline";
import { findPluginRoot } from "../shared/plugin-root";
import type { SqliteStore } from "../storage/sqlite";
import type { Embedder } from "../embedding";
import { Summarizer, modelHealth } from "../ingest/providers";
Expand Down Expand Up @@ -121,7 +122,7 @@ export class ViewerServer {
private static readonly SESSION_TTL = 24 * 60 * 60 * 1000;
private static readonly PLUGIN_VERSION: string = (() => {
try {
const pkgPath = path.resolve(__dirname, "../../package.json");
const pkgPath = path.join(findPluginRoot(import.meta.url), "package.json");
return JSON.parse(fs.readFileSync(pkgPath, "utf-8")).version ?? "unknown";
} catch {
return "unknown";
Expand Down Expand Up @@ -3554,18 +3555,11 @@ export class ViewerServer {
}

private findPluginPackageJson(): string | null {
let dir = __dirname;
for (let i = 0; i < 6; i++) {
const candidate = path.join(dir, "package.json");
if (fs.existsSync(candidate)) {
try {
const pkg = JSON.parse(fs.readFileSync(candidate, "utf-8"));
if (pkg.name && pkg.name.includes("memos-local")) return candidate;
} catch { /* skip */ }
}
dir = path.dirname(dir);
try {
return path.join(findPluginRoot(import.meta.url), "package.json");
} catch {
return null;
}
return null;
}

private async handleUpdateCheck(res: http.ServerResponse): Promise<void> {
Expand Down
10 changes: 5 additions & 5 deletions apps/memos-local-openclaw/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"compilerOptions": {
"target": "ES2022",
"module": "CommonJS",
"module": "ESNext",
"lib": ["ES2022"],
"outDir": "dist",
"rootDir": "src",
"rootDir": ".",
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
Expand All @@ -13,8 +13,8 @@
"declaration": true,
"declarationMap": true,
"sourceMap": true,
"moduleResolution": "node"
"moduleResolution": "bundler"
},
"include": ["src"],
"exclude": ["node_modules", "dist", "**/*.test.ts"]
"include": ["index.ts", "src/**/*.ts"],
"exclude": ["node_modules", "dist", "tests", "scripts", "**/*.test.ts"]
}
Loading