diff --git a/src/index.ts b/src/index.ts index 7a5a441..def3cf0 100644 --- a/src/index.ts +++ b/src/index.ts @@ -7,6 +7,7 @@ import { formatContextForPrompt } from "./services/context.js"; import { getTags } from "./services/tags.js"; import { stripPrivateContent, isFullyPrivate } from "./services/privacy.js"; import { createCompactionHook, type CompactionContext } from "./services/compaction.js"; +import { generatePartId } from "./services/ids.js"; import { isConfigured, CONFIG } from "./config.js"; import { log } from "./services/logger.js"; @@ -112,7 +113,7 @@ export const SupermemoryPlugin: Plugin = async (ctx: PluginInput) => { if (detectMemoryKeyword(userMessage)) { log("chat.message: memory keyword detected"); const nudgePart: Part = { - id: `supermemory-nudge-${Date.now()}`, + id: generatePartId(), sessionID: input.sessionID, messageID: output.message.id, type: "text", @@ -157,7 +158,7 @@ export const SupermemoryPlugin: Plugin = async (ctx: PluginInput) => { if (memoryContext) { const contextPart: Part = { - id: `supermemory-context-${Date.now()}`, + id: generatePartId(), sessionID: input.sessionID, messageID: output.message.id, type: "text", diff --git a/src/services/compaction.ts b/src/services/compaction.ts index 4701beb..7bc2f09 100644 --- a/src/services/compaction.ts +++ b/src/services/compaction.ts @@ -4,6 +4,7 @@ import { homedir } from "node:os"; import { supermemoryClient } from "./client.js"; import { log } from "./logger.js"; import { CONFIG } from "../config.js"; +import { generatePartId, generateMessageId } from "./ids.js"; const MESSAGE_STORAGE = join(homedir(), ".opencode", "messages"); const PART_STORAGE = join(homedir(), ".opencode", "parts"); @@ -152,18 +153,6 @@ function findNearestMessageWithFields(messageDir: string): StoredMessage | null return null; } -function generateMessageId(): string { - const timestamp = Date.now().toString(16); - const random = Math.random().toString(36).substring(2, 14); - return `msg_${timestamp}${random}`; -} - -function generatePartId(): string { - const timestamp = Date.now().toString(16); - const random = Math.random().toString(36).substring(2, 10); - return `prt_${timestamp}${random}`; -} - function injectHookMessage( sessionID: string, hookContent: string, diff --git a/src/services/ids.ts b/src/services/ids.ts new file mode 100644 index 0000000..7e7786e --- /dev/null +++ b/src/services/ids.ts @@ -0,0 +1,16 @@ +/** + * Shared ID generation utilities. + * OpenCode v1.2.25+ requires all part IDs to start with "prt". + */ + +export function generatePartId(): string { + const timestamp = Date.now().toString(16); + const random = Math.random().toString(36).substring(2, 10); + return `prt_${timestamp}${random}`; +} + +export function generateMessageId(): string { + const timestamp = Date.now().toString(16); + const random = Math.random().toString(36).substring(2, 14); + return `msg_${timestamp}${random}`; +}