fix(opencode-plugin): fix tsconfig, add publishConfig and bun lockfile (#5273)
This commit is contained in:
@@ -3,7 +3,7 @@ import { MemoryClient } from "mem0ai";
|
||||
import { userInfo } from "os";
|
||||
import { basename, resolve, dirname } from "path";
|
||||
import { randomBytes } from "crypto";
|
||||
import { existsSync, readdirSync, cpSync } from "fs";
|
||||
import { existsSync, readdirSync, cpSync, mkdirSync, readFileSync, writeFileSync } from "fs";
|
||||
|
||||
async function getUserId(): Promise<string> {
|
||||
if (process.env.MEM0_USER_ID) return process.env.MEM0_USER_ID;
|
||||
@@ -115,7 +115,7 @@ function installSkills(projectDir: string): void {
|
||||
const skills = readdirSync(srcSkills, { withFileTypes: true });
|
||||
for (const entry of skills) {
|
||||
if (!entry.isDirectory()) continue;
|
||||
const dest = resolve(destSkills, entry.name);
|
||||
const dest = resolve(destSkills, `mem0-${entry.name}`);
|
||||
if (existsSync(dest)) continue;
|
||||
cpSync(resolve(srcSkills, entry.name), dest, { recursive: true });
|
||||
}
|
||||
@@ -128,24 +128,24 @@ function installSkills(projectDir: string): void {
|
||||
if (!existsSync(skillMd)) continue;
|
||||
let desc = "Mem0 " + entry.name + " skill";
|
||||
try {
|
||||
const content = require("fs").readFileSync(skillMd, "utf8");
|
||||
const content = readFileSync(skillMd, "utf8");
|
||||
const m = content.match(/^description:\s*(.+)$/m);
|
||||
if (m) desc = m[1].trim();
|
||||
} catch {}
|
||||
const cmdContent = `---\ndescription: ${desc}\n---\nLoad and follow the skill at .opencode/skills/${entry.name}/SKILL.md\n\nUse the mem0 MCP tools (search_memories, get_memories, add_memory, delete_memory, update_memory, list_entities, delete_entities, get_event_status) to execute the skill instructions.\n\nIdentity context (from environment):\n- user_id: Use MEM0_USER_ID env var, or fall back to $USER\n- app_id: Use MEM0_APP_ID env var\n- session_id: Use MEM0_SESSION_ID env var\n- branch: Use MEM0_BRANCH env var\n`;
|
||||
const cmdContent = `---\ndescription: ${desc}\n---\nLoad and follow the skill at .opencode/skills/mem0-${entry.name}/SKILL.md\n\nUse the mem0 MCP tools (search_memories, get_memories, add_memory, delete_memory, update_memory, list_entities, delete_entities, get_event_status) to execute the skill instructions.\n\nIdentity context (from environment):\n- user_id: Use MEM0_USER_ID env var, or fall back to $USER\n- app_id: Use MEM0_APP_ID env var\n- session_id: Use MEM0_SESSION_ID env var\n- branch: Use MEM0_BRANCH env var\n`;
|
||||
try {
|
||||
require("fs").mkdirSync(destCommands, { recursive: true });
|
||||
require("fs").writeFileSync(cmdFile, cmdContent, "utf8");
|
||||
mkdirSync(destCommands, { recursive: true });
|
||||
writeFileSync(cmdFile, cmdContent, "utf8");
|
||||
} catch {}
|
||||
}
|
||||
} catch {}
|
||||
}
|
||||
|
||||
export const Mem0Plugin: Plugin = async (ctx) => {
|
||||
const Mem0Plugin: Plugin = async (ctx) => {
|
||||
const { $, client } = ctx;
|
||||
|
||||
try {
|
||||
const projectDir = (ctx as any).directory ?? (ctx as any).worktree ?? process.cwd();
|
||||
const projectDir = (ctx as any).directory ?? process.cwd();
|
||||
installSkills(projectDir);
|
||||
} catch {}
|
||||
|
||||
@@ -203,9 +203,6 @@ export const Mem0Plugin: Plugin = async (ctx) => {
|
||||
(all as any)?.results?.length ??
|
||||
0;
|
||||
|
||||
systemContext.push(
|
||||
`Mem0 Active | user=${userId} | project=${appId} | branch=${branch} | memories=${memoryCount}`,
|
||||
);
|
||||
systemContext.push(
|
||||
`Always include user_id="${userId}" and app_id="${appId}" in every search_memories filter and add_memory call.`,
|
||||
);
|
||||
@@ -350,14 +347,23 @@ export const Mem0Plugin: Plugin = async (ctx) => {
|
||||
}
|
||||
},
|
||||
|
||||
"experimental.chat.system.transform": async (
|
||||
"experimental.chat.messages.transform": async (
|
||||
_input: any,
|
||||
output: { system: string[] },
|
||||
output: { messages: { info: any; parts: any[] }[] },
|
||||
) => {
|
||||
if (systemContext.length > 0 && output?.system) {
|
||||
const block = `## Mem0 Memory Context\n\n${systemContext.join("\n\n")}`;
|
||||
output.system.push(block);
|
||||
}
|
||||
if (systemContext.length === 0 || !output?.messages?.length) return;
|
||||
|
||||
const firstUser = output.messages.find(
|
||||
(m) => m.info.role === "user",
|
||||
);
|
||||
if (!firstUser || !firstUser.parts.length) return;
|
||||
|
||||
const marker = "## Mem0 Memory Context";
|
||||
if (firstUser.parts.some((p: any) => p.type === "text" && p.text?.includes(marker))) return;
|
||||
|
||||
const block = `${marker}\n\n${systemContext.join("\n\n")}`;
|
||||
const ref = firstUser.parts[0];
|
||||
firstUser.parts.unshift({ ...ref, type: "text", text: block });
|
||||
},
|
||||
|
||||
"tool.execute.before": async (input: any, output: any) => {
|
||||
|
||||
Reference in New Issue
Block a user