11import type { Hooks } from "@opencode-ai/plugin" ;
2- import type { Part } from "@opencode-ai/sdk" ;
32import type { GraphitiClient } from "../services/client.ts" ;
43import { calculateInjectionBudget } from "../services/context-limit.ts" ;
54import {
@@ -26,28 +25,17 @@ export interface ChatHandlerDeps {
2625export function createChatHandler ( deps : ChatHandlerDeps ) {
2726 const { sessionManager, driftThreshold, factStaleDays, client } = deps ;
2827
29- const removeSyntheticMemoryParts = ( parts : Part [ ] ) : Part [ ] =>
30- parts . filter ( ( part ) => {
31- if ( part . type !== "text" ) return true ;
32- if ( part . id ?. startsWith ( "graphiti-memory-" ) ) return false ;
33- if ( part . id ?. startsWith ( "graphiti-refresh-" ) ) return false ;
34-
35- return true ;
36- } ) ;
37-
38- const injectMemoryContext = async (
28+ const searchAndCacheMemoryContext = async (
3929 state : {
4030 groupId : string ;
4131 userGroupId : string ;
4232 contextLimit : number ;
4333 lastInjectionFactUuids : Set < string > ;
34+ cachedMemoryContext ?: string ;
4435 } ,
4536 messageText : string ,
46- output : ChatMessageOutput ,
47- prefix : string ,
4837 useUserScope : boolean ,
4938 characterBudget : number ,
50- shouldReinject : boolean ,
5139 seedFactUuids ?: Set < string > | null ,
5240 ) => {
5341 const userGroupId = state . userGroupId ;
@@ -125,10 +113,10 @@ export function createChatHandler(deps: ChatHandlerDeps) {
125113 if ( snapshot ?. content ) {
126114 const snapshotBudget = Math . min ( characterBudget , 1200 ) ;
127115 snapshotPrimer = [
128- '<memory source="snapshot">' ,
129- "<instruction> Most recent session snapshot; use to restore active strategy and open questions.</instruction> " ,
130- `<snapshot> ${ snapshot . content . slice ( 0 , snapshotBudget ) } </snapshot>` ,
131- "</memory>" ,
116+ "## Session Snapshot" ,
117+ "> Most recent session snapshot; use to restore active strategy and open questions." ,
118+ "" ,
119+ snapshot . content . slice ( 0 , snapshotBudget ) ,
132120 ] . join ( "\n" ) ;
133121 }
134122 } catch ( err ) {
@@ -150,41 +138,18 @@ export function createChatHandler(deps: ChatHandlerDeps) {
150138 . slice ( 0 , characterBudget ) ;
151139 if ( ! memoryContext ) return ;
152140
153- if ( shouldReinject ) {
154- output . parts = removeSyntheticMemoryParts ( output . parts ) ;
155- }
156-
157- const allFactUuids = seedFactUuids ??
141+ const factUuids = seedFactUuids ??
158142 new Set < string > ( [
159143 ...projectContext . facts . map ( ( fact ) => fact . uuid ) ,
160144 ...userContext . facts . map ( ( fact ) => fact . uuid ) ,
161145 ] ) ;
162-
163- if ( "system" in output . message ) {
164- try {
165- output . message . system = memoryContext ;
166- return ;
167- } catch ( _err ) {
168- // Fall through to synthetic injection.
169- }
170- }
171-
172- {
173- output . parts . unshift ( {
174- type : "text" ,
175- text : memoryContext ,
176- id : `${ prefix } ${ Date . now ( ) } ` ,
177- sessionID : output . message . sessionID ,
178- messageID : output . message . id ,
179- synthetic : true ,
180- } as Part ) ;
181- }
146+ state . cachedMemoryContext = memoryContext ;
182147 logger . info (
183- `Injected ${ projectFacts . length + userFacts . length } facts and ${
148+ `Cached ${ projectFacts . length + userFacts . length } facts and ${
184149 projectNodes . length + userNodes . length
185- } nodes`,
150+ } nodes for system prompt injection `,
186151 ) ;
187- state . lastInjectionFactUuids = allFactUuids ;
152+ state . lastInjectionFactUuids = factUuids ;
188153 } ;
189154
190155 const computeJaccardSimilarity = (
@@ -254,20 +219,14 @@ export function createChatHandler(deps: ChatHandlerDeps) {
254219 }
255220 }
256221
257- if ( ! shouldInjectOnFirst && ! shouldReinject ) return ;
258-
259222 try {
260- const prefix = shouldReinject ? "graphiti-refresh-" : "graphiti-memory-" ;
261223 const useUserScope = shouldInjectOnFirst ;
262224 const characterBudget = calculateInjectionBudget ( state . contextLimit ) ;
263- await injectMemoryContext (
225+ await searchAndCacheMemoryContext (
264226 state ,
265227 messageText ,
266- output ,
267- prefix ,
268228 useUserScope ,
269229 characterBudget ,
270- shouldReinject ,
271230 currentFactUuids ,
272231 ) ;
273232 state . injectedMemories = true ;
0 commit comments