| ... | @@ -14,28 +14,39 @@ import { relayAgentActivityRows, relayEnvironmentLinks } from "../persistence/sc | ... | @@ -14,28 +14,39 @@ import { relayAgentActivityRows, relayEnvironmentLinks } from "../persistence/sc |
| 14 | | 14 | |
| 15 | export class AgentActivityRowUpsertPersistenceError extends Schema.TaggedErrorClass<AgentActivityRowUpsertPersistenceError>()( | 15 | export class AgentActivityRowUpsertPersistenceError extends Schema.TaggedErrorClass<AgentActivityRowUpsertPersistenceError>()( |
| 16 | "AgentActivityRowUpsertPersistenceError", | 16 | "AgentActivityRowUpsertPersistenceError", |
| 17 | { cause: Schema.Defect() }, | 17 | { |
| | 18 | environmentId: Schema.String, |
| | 19 | threadId: Schema.String, |
| | 20 | cause: Schema.Defect(), |
| | 21 | }, |
| 18 | ) { | 22 | ) { |
| 19 | override get message(): string { | 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 | export class AgentActivityRowDeletePersistenceError extends Schema.TaggedErrorClass<AgentActivityRowDeletePersistenceError>()( | 28 | export class AgentActivityRowDeletePersistenceError extends Schema.TaggedErrorClass<AgentActivityRowDeletePersistenceError>()( |
| 25 | "AgentActivityRowDeletePersistenceError", | 29 | "AgentActivityRowDeletePersistenceError", |
| 26 | { cause: Schema.Defect() }, | 30 | { |
| | 31 | environmentId: Schema.String, |
| | 32 | threadId: Schema.String, |
| | 33 | cause: Schema.Defect(), |
| | 34 | }, |
| 27 | ) { | 35 | ) { |
| 28 | override get message(): string { | 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 | export class AgentActivityRowListPersistenceError extends Schema.TaggedErrorClass<AgentActivityRowListPersistenceError>()( | 41 | export class AgentActivityRowListPersistenceError extends Schema.TaggedErrorClass<AgentActivityRowListPersistenceError>()( |
| 34 | "AgentActivityRowListPersistenceError", | 42 | "AgentActivityRowListPersistenceError", |
| 35 | { cause: Schema.Defect() }, | 43 | { |
| | 44 | userId: Schema.String, |
| | 45 | cause: Schema.Defect(), |
| | 46 | }, |
| 36 | ) { | 47 | ) { |
| 37 | override get message(): string { | 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,41 +86,56 @@ export const make = Effect.gen(function* () { |
| 75 | const db = yield* RelayDb.RelayDb; | 86 | const db = yield* RelayDb.RelayDb; |
| 76 | | 87 | |
| 77 | return AgentActivityRows.of({ | 88 | return AgentActivityRows.of({ |
| 78 | upsert: Effect.fn("relay.agent_activity_rows.upsert")( | 89 | upsert: Effect.fn("relay.agent_activity_rows.upsert")(function* (input) { |
| 79 | function* (input) { | 90 | yield* Effect.annotateCurrentSpan({ |
| 80 | yield* Effect.annotateCurrentSpan({ | 91 | "relay.environment_id": input.state.environmentId, |
| 81 | "relay.environment_id": input.state.environmentId, | 92 | "relay.thread_id": input.state.threadId, |
| 82 | "relay.thread_id": input.state.threadId, | 93 | }); |
| 83 | }); | 94 | const now = yield* DateTime.now; |
| 84 | const now = yield* DateTime.now; | 95 | const stateJson = yield* encodeRelayAgentActivityStateJson(input.state).pipe( |
| 85 | const stateJson = yield* encodeRelayAgentActivityStateJson(input.state).pipe( | 96 | Effect.flatMap(decodeJsonString), |
| 86 | Effect.flatMap(decodeJsonString), | 97 | Effect.map(Function.cast<unknown, RelayAgentActivityState>), |
| 87 | Effect.map(Function.cast<unknown, RelayAgentActivityState>), | 98 | Effect.mapError( |
| 88 | ); | 99 | (cause) => |
| 89 | yield* db | 100 | new AgentActivityRowUpsertPersistenceError({ |
| 90 | .insert(relayAgentActivityRows) | 101 | environmentId: input.state.environmentId, |
| 91 | .values({ | 102 | threadId: input.state.threadId, |
| 92 | environmentId: input.state.environmentId, | 103 | cause, |
| 93 | environmentPublicKey: input.environmentPublicKey, | 104 | }), |
| 94 | threadId: input.state.threadId, | 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 | stateJson, | 124 | stateJson, |
| 96 | updatedAt: input.state.updatedAt, | 125 | updatedAt: input.state.updatedAt, |
| 97 | createdAt: DateTime.formatIso(now), | 126 | }, |
| 98 | }) | 127 | }) |
| 99 | .onConflictDoUpdate({ | 128 | .pipe( |
| 100 | target: [ | 129 | Effect.mapError( |
| 101 | relayAgentActivityRows.environmentId, | 130 | (cause) => |
| 102 | relayAgentActivityRows.environmentPublicKey, | 131 | new AgentActivityRowUpsertPersistenceError({ |
| 103 | relayAgentActivityRows.threadId, | 132 | environmentId: input.state.environmentId, |
| 104 | ], | 133 | threadId: input.state.threadId, |
| 105 | set: { | 134 | cause, |
| 106 | stateJson, | 135 | }), |
| 107 | updatedAt: input.state.updatedAt, | 136 | ), |
| 108 | }, | 137 | ); |
| 109 | }); | 138 | }), |
| 110 | }, | | |
| 111 | Effect.mapError((cause) => new AgentActivityRowUpsertPersistenceError({ cause })), | | |
| 112 | ), | | |
| 113 | | 139 | |
| 114 | remove: Effect.fn("relay.agent_activity_rows.remove")(function* (input) { | 140 | remove: Effect.fn("relay.agent_activity_rows.remove")(function* (input) { |
| 115 | yield* Effect.annotateCurrentSpan({ | 141 | yield* Effect.annotateCurrentSpan({ |
| ... | @@ -125,7 +151,16 @@ export const make = Effect.gen(function* () { | ... | @@ -125,7 +151,16 @@ export const make = Effect.gen(function* () { |
| 125 | eq(relayAgentActivityRows.threadId, input.threadId), | 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 | listForUser: Effect.fn("relay.agent_activity_rows.list_for_user")(function* (input) { | 166 | listForUser: Effect.fn("relay.agent_activity_rows.list_for_user")(function* (input) { |
| ... | @@ -159,7 +194,13 @@ export const make = Effect.gen(function* () { | ... | @@ -159,7 +194,13 @@ export const make = Effect.gen(function* () { |
| 159 | Effect.map((rows) => | 194 | Effect.map((rows) => |
| 160 | rows.flatMap((row) => Option.toArray(decodeRelayAgentActivityStateJson(row))), | 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 | }); |