|
| 1 | +import type { Hooks } from "@opencode-ai/plugin"; |
| 2 | +import { extractVisibleUuids } from "../services/context.ts"; |
| 3 | +import { logger } from "../services/logger.ts"; |
| 4 | +import type { SessionManager } from "../session.ts"; |
| 5 | + |
| 6 | +type MessagesTransformHook = NonNullable< |
| 7 | + Hooks["experimental.chat.messages.transform"] |
| 8 | +>; |
| 9 | +type MessagesTransformInput = Parameters<MessagesTransformHook>[0]; |
| 10 | +type MessagesTransformOutput = Parameters<MessagesTransformHook>[1]; |
| 11 | + |
| 12 | +export interface MessagesHandlerDeps { |
| 13 | + sessionManager: SessionManager; |
| 14 | +} |
| 15 | + |
| 16 | +export function createMessagesHandler(deps: MessagesHandlerDeps) { |
| 17 | + const { sessionManager } = deps; |
| 18 | + |
| 19 | + // deno-lint-ignore require-await |
| 20 | + return async ( |
| 21 | + _input: MessagesTransformInput, |
| 22 | + output: MessagesTransformOutput, |
| 23 | + ) => { |
| 24 | + const lastUserEntry = [...output.messages] |
| 25 | + .reverse() |
| 26 | + .find((message) => message.info.role === "user"); |
| 27 | + if (!lastUserEntry) return; |
| 28 | + |
| 29 | + const sessionID = lastUserEntry.info.sessionID; |
| 30 | + const state = sessionManager.getState(sessionID); |
| 31 | + if (!state?.isMain) { |
| 32 | + logger.debug("Skipping memory injection; not main session", { |
| 33 | + sessionID, |
| 34 | + }); |
| 35 | + return; |
| 36 | + } |
| 37 | + |
| 38 | + const allVisibleUuids: string[] = []; |
| 39 | + for (const entry of output.messages) { |
| 40 | + for (const part of entry.parts) { |
| 41 | + if (part.type === "text" && "text" in part) { |
| 42 | + const uuids = extractVisibleUuids((part as { text: string }).text); |
| 43 | + if (uuids.length > 0) { |
| 44 | + logger.debug("Found <memory> block UUIDs", { |
| 45 | + sessionID, |
| 46 | + uuids, |
| 47 | + messageID: entry.info.id, |
| 48 | + }); |
| 49 | + } |
| 50 | + allVisibleUuids.push(...uuids); |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + state.visibleFactUuids = [...new Set(allVisibleUuids)]; |
| 55 | + logger.debug("Updated visibleFactUuids from message scan", { |
| 56 | + sessionID, |
| 57 | + visibleCount: state.visibleFactUuids.length, |
| 58 | + }); |
| 59 | + |
| 60 | + if (!state.cachedMemoryContext) { |
| 61 | + logger.debug("Skipping memory injection; no cached context", { |
| 62 | + sessionID, |
| 63 | + }); |
| 64 | + return; |
| 65 | + } |
| 66 | + |
| 67 | + const textPart = lastUserEntry.parts.find( |
| 68 | + (part): part is typeof part & { type: "text"; text: string } => |
| 69 | + part.type === "text" && "text" in part, |
| 70 | + ); |
| 71 | + if (!textPart) { |
| 72 | + logger.debug("Skipping memory injection; no text part", { |
| 73 | + sessionID, |
| 74 | + }); |
| 75 | + return; |
| 76 | + } |
| 77 | + |
| 78 | + if (textPart.text.includes("<memory")) { |
| 79 | + logger.debug("Skipping memory injection; already injected", { |
| 80 | + sessionID, |
| 81 | + }); |
| 82 | + state.cachedMemoryContext = undefined; |
| 83 | + state.cachedFactUuids = undefined; |
| 84 | + return; |
| 85 | + } |
| 86 | + |
| 87 | + const uuids = state.cachedFactUuids ?? []; |
| 88 | + const uuidAttr = uuids.length > 0 ? ` data-uuids="${uuids.join(",")}"` : ""; |
| 89 | + const memoryBlock = |
| 90 | + `<memory${uuidAttr}>\n${state.cachedMemoryContext}\n</memory>`; |
| 91 | + |
| 92 | + textPart.text = `${memoryBlock}\n\n${textPart.text}`; |
| 93 | + |
| 94 | + logger.info("Injected memory context into last user message", { |
| 95 | + sessionID, |
| 96 | + factCount: uuids.length, |
| 97 | + blockLength: memoryBlock.length, |
| 98 | + preview: state.cachedMemoryContext.slice(0, 100), |
| 99 | + }); |
| 100 | + |
| 101 | + state.cachedMemoryContext = undefined; |
| 102 | + state.cachedFactUuids = undefined; |
| 103 | + }; |
| 104 | +} |
0 commit comments