@@ -83,6 +83,11 @@ export class AgentActivityRows extends Context.Service<
8383 ReadonlyArray < RelayAgentActivityState > ,
8484 AgentActivityRowListPersistenceError
8585 > ;
86+ readonly getForUserThread : ( input : {
87+ readonly userId : string ;
88+ readonly environmentId : string ;
89+ readonly threadId : string ;
90+ } ) => Effect . Effect < RelayAgentActivityState | null , AgentActivityRowListPersistenceError > ;
8691 }
8792> ( ) ( "t3code-relay/agentActivity/AgentActivityRows" ) { }
8893
@@ -241,6 +246,54 @@ export const make = Effect.gen(function* () {
241246 ) ,
242247 ) ;
243248 } ) ,
249+
250+ getForUserThread : Effect . fn ( "relay.agent_activity_rows.get_for_user_thread" ) ( function * ( input ) {
251+ return yield * db
252+ . select ( { stateJson : relayAgentActivityRows . stateJson } )
253+ . from ( relayAgentActivityRows )
254+ . innerJoin (
255+ relayEnvironmentLinks ,
256+ and (
257+ eq ( relayEnvironmentLinks . environmentId , relayAgentActivityRows . environmentId ) ,
258+ eq (
259+ relayEnvironmentLinks . environmentPublicKey ,
260+ relayAgentActivityRows . environmentPublicKey ,
261+ ) ,
262+ ) ,
263+ )
264+ . where (
265+ and (
266+ eq ( relayEnvironmentLinks . userId , input . userId ) ,
267+ isNull ( relayEnvironmentLinks . revokedAt ) ,
268+ eq ( relayAgentActivityRows . environmentId , input . environmentId ) ,
269+ eq ( relayAgentActivityRows . threadId , input . threadId ) ,
270+ ) ,
271+ )
272+ . orderBy ( desc ( relayAgentActivityRows . updatedAt ) )
273+ . pipe (
274+ Effect . flatMap ( ( rows ) =>
275+ Effect . forEach ( rows , ( row ) => encodeJsonValue ( row . stateJson ) , {
276+ concurrency : "unbounded" ,
277+ } ) ,
278+ ) ,
279+ Effect . map ( ( rows ) => {
280+ for ( const row of rows ) {
281+ const decoded = decodeRelayAgentActivityStateJson ( row ) ;
282+ if ( Option . isSome ( decoded ) ) {
283+ return decoded . value ;
284+ }
285+ }
286+ return null ;
287+ } ) ,
288+ Effect . mapError (
289+ ( cause ) =>
290+ new AgentActivityRowListPersistenceError ( {
291+ userId : input . userId ,
292+ cause,
293+ } ) ,
294+ ) ,
295+ ) ;
296+ } ) ,
244297 } ) ;
245298} ) ;
246299
0 commit comments