@agentbadge/database 0.6.21 → 0.6.22

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,3 +1,4 @@
1
+ import { param } from "@prisma/orm-postgres/relational-core/expression";
1
2
  const TERMINAL = new Set(["completed", "failed"]);
2
3
  const toBig = (n) => BigInt(n);
3
4
  const toBigOrNull = (n) => n == null ? null : BigInt(n);
@@ -101,11 +102,16 @@ export class InteractionRepository {
101
102
  if (patch.classifiedBy !== undefined)
102
103
  update.classifiedBy = patch.classifiedBy;
103
104
  const transitioned = cur.state === "submitted";
104
- if (transitioned)
105
- update.state = "working";
105
+ if (transitioned) {
106
+ const stats = await tx.execute(this.db.raw
107
+ .sql `UPDATE "agentRequest" SET "state" = 'working' WHERE "id" = ${param(id, { codecId: "pg/text@1" })} AND "state" = 'submitted'`
108
+ .affectedCount()
109
+ .build());
110
+ if (stats.affectedRows === 0)
111
+ return { raced: true };
112
+ }
106
113
  const row = await tx.orm.public.AgentRequest.where({
107
114
  id,
108
- state: cur.state,
109
115
  }).update(update);
110
116
  if (!row)
111
117
  return { raced: true };
@@ -129,16 +135,13 @@ export class InteractionRepository {
129
135
  if (TERMINAL.has(cur.state)) {
130
136
  return { row: cur, transitioned: false, raced: false };
131
137
  }
132
- const row = await tx.orm.public.AgentRequest.where({
133
- id,
134
- state: cur.state,
135
- }).update({
136
- state: "completed",
137
- response: { answer: response.body },
138
- respondedAtMs: toBigOrNull(nowMs),
139
- });
140
- if (!row)
138
+ const stats = await tx.execute(this.db.raw
139
+ .sql `UPDATE "agentRequest" SET "state" = 'completed', "response" = ${param({ answer: response.body }, { codecId: "pg/json@1" })}, "respondedAtMs" = ${param(BigInt(nowMs), { codecId: "pg/int8@1" })} WHERE "id" = ${param(id, { codecId: "pg/text@1" })} AND "state" = ${param(cur.state, { codecId: "pg/text@1" })}`
140
+ .affectedCount()
141
+ .build());
142
+ if (stats.affectedRows === 0)
141
143
  return { raced: true };
144
+ const row = await tx.orm.public.AgentRequest.where({ id }).first();
142
145
  await tx.orm.public.InteractionResponse.create({
143
146
  id: response.id,
144
147
  requestId: response.requestId,
@@ -165,12 +168,13 @@ export class InteractionRepository {
165
168
  if (TERMINAL.has(cur.state)) {
166
169
  return { row: cur, transitioned: false, raced: false };
167
170
  }
168
- const row = await tx.orm.public.AgentRequest.where({
169
- id,
170
- state: cur.state,
171
- }).update({ state: "failed" });
172
- if (!row)
171
+ const stats = await tx.execute(this.db.raw
172
+ .sql `UPDATE "agentRequest" SET "state" = 'failed' WHERE "id" = ${param(id, { codecId: "pg/text@1" })} AND "state" = ${param(cur.state, { codecId: "pg/text@1" })}`
173
+ .affectedCount()
174
+ .build());
175
+ if (stats.affectedRows === 0)
173
176
  return { raced: true };
177
+ const row = await tx.orm.public.AgentRequest.where({ id }).first();
174
178
  await this.appendEventsTx(tx.orm, id, events, nowMs);
175
179
  return { row, transitioned: true, raced: false };
176
180
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@agentbadge/database",
3
- "version": "0.6.21",
3
+ "version": "0.6.22",
4
4
  "description": "Shared database layer for AgentBadge — Prisma ORM 8 client, repositories, store abstraction",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",