@@ -12,11 +12,11 @@ import { PROVIDER_DEFINITIONS } from '@/providers/models'
1212const logger = createLogger ( 'Memory' )
1313
1414export class Memory {
15- async hasMemory ( workflowId : string , conversationId : string ) : Promise < boolean > {
15+ async hasMemory ( workspaceId : string , conversationId : string ) : Promise < boolean > {
1616 const result = await db
1717 . select ( { id : memory . id } )
1818 . from ( memory )
19- . where ( and ( eq ( memory . workflowId , workflowId ) , eq ( memory . key , conversationId ) ) )
19+ . where ( and ( eq ( memory . workspaceId , workspaceId ) , eq ( memory . key , conversationId ) ) )
2020 . limit ( 1 )
2121
2222 return result . length > 0
@@ -27,13 +27,10 @@ export class Memory {
2727 return [ ]
2828 }
2929
30- if ( ! ctx . workflowId ) {
31- throw new Error ( 'workflowId is required to fetch memory' )
32- }
33-
30+ const workspaceId = this . requireWorkspaceId ( ctx )
3431 this . validateConversationId ( inputs . conversationId )
3532
36- const messages = await this . fetchMemory ( ctx . workflowId , inputs . conversationId ! )
33+ const messages = await this . fetchMemory ( workspaceId , inputs . conversationId ! )
3734
3835 switch ( inputs . memoryType ) {
3936 case 'conversation' :
@@ -69,10 +66,7 @@ export class Memory {
6966 return
7067 }
7168
72- if ( ! ctx . workflowId ) {
73- throw new Error ( 'workflowId is required to append to memory' )
74- }
75-
69+ const workspaceId = this . requireWorkspaceId ( ctx )
7670 this . validateConversationId ( inputs . conversationId )
7771 this . validateContent ( message . content )
7872
@@ -83,23 +77,23 @@ export class Memory {
8377 inputs . slidingWindowSize ,
8478 MEMORY . DEFAULT_SLIDING_WINDOW_SIZE
8579 )
86- const existing = await this . fetchMemory ( ctx . workflowId , key )
80+ const existing = await this . fetchMemory ( workspaceId , key )
8781 const updated = this . applyWindow ( [ ...existing , message ] , limit )
88- await this . persistMemory ( ctx . workflowId , key , updated )
82+ await this . persistMemory ( workspaceId , key , updated )
8983 } else if ( inputs . memoryType === 'sliding_window_tokens' ) {
9084 const maxTokens = this . parsePositiveInt (
9185 inputs . slidingWindowTokens ,
9286 MEMORY . DEFAULT_SLIDING_WINDOW_TOKENS
9387 )
94- const existing = await this . fetchMemory ( ctx . workflowId , key )
88+ const existing = await this . fetchMemory ( workspaceId , key )
9589 const updated = this . applyTokenWindow ( [ ...existing , message ] , maxTokens , inputs . model )
96- await this . persistMemory ( ctx . workflowId , key , updated )
90+ await this . persistMemory ( workspaceId , key , updated )
9791 } else {
98- await this . appendMessage ( ctx . workflowId , key , message )
92+ await this . appendMessage ( workspaceId , key , message )
9993 }
10094
10195 logger . debug ( 'Appended message to memory' , {
102- workflowId : ctx . workflowId ,
96+ workspaceId ,
10397 key,
10498 role : message . role ,
10599 } )
@@ -110,9 +104,7 @@ export class Memory {
110104 return
111105 }
112106
113- if ( ! ctx . workflowId ) {
114- throw new Error ( 'workflowId is required to seed memory' )
115- }
107+ const workspaceId = this . requireWorkspaceId ( ctx )
116108
117109 const conversationMessages = messages . filter ( ( m ) => m . role !== 'system' )
118110 if ( conversationMessages . length === 0 ) {
@@ -138,10 +130,10 @@ export class Memory {
138130 messagesToStore = this . applyTokenWindow ( conversationMessages , maxTokens , inputs . model )
139131 }
140132
141- await this . persistMemory ( ctx . workflowId , key , messagesToStore )
133+ await this . persistMemory ( workspaceId , key , messagesToStore )
142134
143135 logger . debug ( 'Seeded memory' , {
144- workflowId : ctx . workflowId ,
136+ workspaceId ,
145137 key,
146138 count : messagesToStore . length ,
147139 } )
@@ -175,6 +167,13 @@ export class Memory {
175167 return stream . pipeThrough ( transformStream )
176168 }
177169
170+ private requireWorkspaceId ( ctx : ExecutionContext ) : string {
171+ if ( ! ctx . workspaceId ) {
172+ throw new Error ( 'workspaceId is required for memory operations' )
173+ }
174+ return ctx . workspaceId
175+ }
176+
178177 private applyWindow ( messages : Message [ ] , limit : number ) : Message [ ] {
179178 return messages . slice ( - limit )
180179 }
@@ -222,11 +221,11 @@ export class Memory {
222221 return messages
223222 }
224223
225- private async fetchMemory ( workflowId : string , key : string ) : Promise < Message [ ] > {
224+ private async fetchMemory ( workspaceId : string , key : string ) : Promise < Message [ ] > {
226225 const result = await db
227226 . select ( { data : memory . data } )
228227 . from ( memory )
229- . where ( and ( eq ( memory . workflowId , workflowId ) , eq ( memory . key , key ) ) )
228+ . where ( and ( eq ( memory . workspaceId , workspaceId ) , eq ( memory . key , key ) ) )
230229 . limit ( 1 )
231230
232231 if ( result . length === 0 ) return [ ]
@@ -239,43 +238,47 @@ export class Memory {
239238 )
240239 }
241240
242- private async persistMemory ( workflowId : string , key : string , messages : Message [ ] ) : Promise < void > {
241+ private async persistMemory (
242+ workspaceId : string ,
243+ key : string ,
244+ messages : Message [ ]
245+ ) : Promise < void > {
243246 const now = new Date ( )
244247
245248 await db
246249 . insert ( memory )
247250 . values ( {
248251 id : randomUUID ( ) ,
249- workflowId ,
252+ workspaceId ,
250253 key,
251254 data : messages ,
252255 createdAt : now ,
253256 updatedAt : now ,
254257 } )
255258 . onConflictDoUpdate ( {
256- target : [ memory . workflowId , memory . key ] ,
259+ target : [ memory . workspaceId , memory . key ] ,
257260 set : {
258261 data : messages ,
259262 updatedAt : now ,
260263 } ,
261264 } )
262265 }
263266
264- private async appendMessage ( workflowId : string , key : string , message : Message ) : Promise < void > {
267+ private async appendMessage ( workspaceId : string , key : string , message : Message ) : Promise < void > {
265268 const now = new Date ( )
266269
267270 await db
268271 . insert ( memory )
269272 . values ( {
270273 id : randomUUID ( ) ,
271- workflowId ,
274+ workspaceId ,
272275 key,
273276 data : [ message ] ,
274277 createdAt : now ,
275278 updatedAt : now ,
276279 } )
277280 . onConflictDoUpdate ( {
278- target : [ memory . workflowId , memory . key ] ,
281+ target : [ memory . workspaceId , memory . key ] ,
279282 set : {
280283 data : sql `${ memory . data } || ${ JSON . stringify ( [ message ] ) } ::jsonb` ,
281284 updatedAt : now ,
0 commit comments