| ... | ... | @@ -14,28 +14,39 @@ import { relayAgentActivityRows, relayEnvironmentLinks } from "../persistence/sc |
| 14 | 14 | |
| 15 | 15 | export class AgentActivityRowUpsertPersistenceError extends Schema.TaggedErrorClass<AgentActivityRowUpsertPersistenceError>()( |
| 16 | 16 | "AgentActivityRowUpsertPersistenceError", |
| 17 | | { cause: Schema.Defect() }, |
| 17 | { |
| 18 | environmentId: Schema.String, |
| 19 | threadId: Schema.String, |
| 20 | cause: Schema.Defect(), |
| 21 | }, |
| 18 | 22 | ) { |
| 19 | 23 | override get message(): string { |
| 20 | | return "Failed to persist agent activity state"; |
| 24 | return `Failed to persist agent activity state for environment ${this.environmentId}, thread ${this.threadId}.`; |
| 21 | 25 | } |
| 22 | 26 | } |
| 23 | 27 | |
| 24 | 28 | export class AgentActivityRowDeletePersistenceError extends Schema.TaggedErrorClass<AgentActivityRowDeletePersistenceError>()( |
| 25 | 29 | "AgentActivityRowDeletePersistenceError", |
| 26 | | { cause: Schema.Defect() }, |
| 30 | { |
| 31 | environmentId: Schema.String, |
| 32 | threadId: Schema.String, |
| 33 | cause: Schema.Defect(), |
| 34 | }, |
| 27 | 35 | ) { |
| 28 | 36 | override get message(): string { |
| 29 | | return "Failed to delete agent activity state"; |
| 37 | return `Failed to delete agent activity state for environment ${this.environmentId}, thread ${this.threadId}.`; |
| 30 | 38 | } |
| 31 | 39 | } |
| 32 | 40 | |
| 33 | 41 | export class AgentActivityRowListPersistenceError extends Schema.TaggedErrorClass<AgentActivityRowListPersistenceError>()( |
| 34 | 42 | "AgentActivityRowListPersistenceError", |
| 35 | | { cause: Schema.Defect() }, |
| 43 | { |
| 44 | userId: Schema.String, |
| 45 | cause: Schema.Defect(), |
| 46 | }, |
| 36 | 47 | ) { |
| 37 | 48 | override get message(): string { |
| 38 | | return "Failed to list agent activity state"; |
| 49 | return `Failed to list agent activity state for user ${this.userId}.`; |
| 39 | 50 | } |
| 40 | 51 | } |
| 41 | 52 | |
| ... | ... | @@ -75,41 +86,56 @@ export const make = Effect.gen(function* () { |
| 75 | 86 | const db = yield* RelayDb.RelayDb; |
| 76 | 87 | |
| 77 | 88 | return AgentActivityRows.of({ |
| 78 | | upsert: Effect.fn("relay.agent_activity_rows.upsert")( |
| 79 | | function* (input) { |
| 80 | | yield* Effect.annotateCurrentSpan({ |
| 81 | | "relay.environment_id": input.state.environmentId, |
| 82 | | "relay.thread_id": input.state.threadId, |
| 83 | | }); |
| 84 | | const now = yield* DateTime.now; |
| 85 | | const stateJson = yield* encodeRelayAgentActivityStateJson(input.state).pipe( |
| 86 | | Effect.flatMap(decodeJsonString), |
| 87 | | Effect.map(Function.cast<unknown, RelayAgentActivityState>), |
| 88 | | ); |
| 89 | | yield* db |
| 90 | | .insert(relayAgentActivityRows) |
| 91 | | .values({ |
| 92 | | environmentId: input.state.environmentId, |
| 93 | | environmentPublicKey: input.environmentPublicKey, |
| 94 | | threadId: input.state.threadId, |
| 89 | upsert: Effect.fn("relay.agent_activity_rows.upsert")(function* (input) { |
| 90 | yield* Effect.annotateCurrentSpan({ |
| 91 | "relay.environment_id": input.state.environmentId, |
| 92 | "relay.thread_id": input.state.threadId, |
| 93 | }); |
| 94 | const now = yield* DateTime.now; |
| 95 | const stateJson = yield* encodeRelayAgentActivityStateJson(input.state).pipe( |
| 96 | Effect.flatMap(decodeJsonString), |
| 97 | Effect.map(Function.cast<unknown, RelayAgentActivityState>), |
| 98 | Effect.mapError( |
| 99 | (cause) => |
| 100 | new AgentActivityRowUpsertPersistenceError({ |
| 101 | environmentId: input.state.environmentId, |
| 102 | threadId: input.state.threadId, |
| 103 | cause, |
| 104 | }), |
| 105 | ), |
| 106 | ); |
| 107 | yield* db |
| 108 | .insert(relayAgentActivityRows) |
| 109 | .values({ |
| 110 | environmentId: input.state.environmentId, |
| 111 | environmentPublicKey: input.environmentPublicKey, |
| 112 | threadId: input.state.threadId, |
| 113 | stateJson, |
| 114 | updatedAt: input.state.updatedAt, |
| 115 | createdAt: DateTime.formatIso(now), |
| 116 | }) |
| 117 | .onConflictDoUpdate({ |
| 118 | target: [ |
| 119 | relayAgentActivityRows.environmentId, |
| 120 | relayAgentActivityRows.environmentPublicKey, |
| 121 | relayAgentActivityRows.threadId, |
| 122 | ], |
| 123 | set: { |
| 95 | 124 | stateJson, |
| 96 | 125 | updatedAt: input.state.updatedAt, |
| 97 | | createdAt: DateTime.formatIso(now), |
| 98 | | }) |
| 99 | | .onConflictDoUpdate({ |
| 100 | | target: [ |
| 101 | | relayAgentActivityRows.environmentId, |
| 102 | | relayAgentActivityRows.environmentPublicKey, |
| 103 | | relayAgentActivityRows.threadId, |
| 104 | | ], |
| 105 | | set: { |
| 106 | | stateJson, |
| 107 | | updatedAt: input.state.updatedAt, |
| 108 | | }, |
| 109 | | }); |
| 110 | | }, |
| 111 | | Effect.mapError((cause) => new AgentActivityRowUpsertPersistenceError({ cause })), |
| 112 | | ), |
| 126 | }, |
| 127 | }) |
| 128 | .pipe( |
| 129 | Effect.mapError( |
| 130 | (cause) => |
| 131 | new AgentActivityRowUpsertPersistenceError({ |
| 132 | environmentId: input.state.environmentId, |
| 133 | threadId: input.state.threadId, |
| 134 | cause, |
| 135 | }), |
| 136 | ), |
| 137 | ); |
| 138 | }), |
| 113 | 139 | |
| 114 | 140 | remove: Effect.fn("relay.agent_activity_rows.remove")(function* (input) { |
| 115 | 141 | yield* Effect.annotateCurrentSpan({ |
| ... | ... | @@ -125,7 +151,16 @@ export const make = Effect.gen(function* () { |
| 125 | 151 | eq(relayAgentActivityRows.threadId, input.threadId), |
| 126 | 152 | ), |
| 127 | 153 | ) |
| 128 | | .pipe(Effect.mapError((cause) => new AgentActivityRowDeletePersistenceError({ cause }))); |
| 154 | .pipe( |
| 155 | Effect.mapError( |
| 156 | (cause) => |
| 157 | new AgentActivityRowDeletePersistenceError({ |
| 158 | environmentId: input.environmentId, |
| 159 | threadId: input.threadId, |
| 160 | cause, |
| 161 | }), |
| 162 | ), |
| 163 | ); |
| 129 | 164 | }), |
| 130 | 165 | |
| 131 | 166 | listForUser: Effect.fn("relay.agent_activity_rows.list_for_user")(function* (input) { |
| ... | ... | @@ -159,7 +194,13 @@ export const make = Effect.gen(function* () { |
| 159 | 194 | Effect.map((rows) => |
| 160 | 195 | rows.flatMap((row) => Option.toArray(decodeRelayAgentActivityStateJson(row))), |
| 161 | 196 | ), |
| 162 | | Effect.mapError((cause) => new AgentActivityRowListPersistenceError({ cause })), |
| 197 | Effect.mapError( |
| 198 | (cause) => |
| 199 | new AgentActivityRowListPersistenceError({ |
| 200 | userId: input.userId, |
| 201 | cause, |
| 202 | }), |
| 203 | ), |
| 163 | 204 | ); |
| 164 | 205 | }), |
| 165 | 206 | }); |