@opentrust/db 7.3.9 → 7.3.11

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.
@@ -8,6 +8,13 @@
8
8
  "when": 1772680703827,
9
9
  "tag": "0000_serious_martin_li",
10
10
  "breakpoints": true
11
+ },
12
+ {
13
+ "idx": 1,
14
+ "version": "6",
15
+ "when": 1772695182037,
16
+ "tag": "0001_ambitious_grandmaster",
17
+ "breakpoints": true
11
18
  }
12
19
  ]
13
20
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@opentrust/db",
3
- "version": "7.3.9",
3
+ "version": "7.3.11",
4
4
  "type": "module",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",
@@ -19,7 +19,7 @@
19
19
  "better-sqlite3": "^11.0.0",
20
20
  "dotenv": "^17.3.1",
21
21
  "drizzle-orm": "^0.36.0",
22
- "@opentrust/shared": "7.3.9"
22
+ "@opentrust/shared": "7.3.11"
23
23
  },
24
24
  "optionalDependencies": {
25
25
  "mysql2": "^3.11.0",
package/src/index.ts CHANGED
@@ -9,4 +9,5 @@ export { usageQueries } from "./queries/usage.js";
9
9
  export { detectionResultQueries } from "./queries/detection-results.js";
10
10
  export { settingsQueries } from "./queries/settings.js";
11
11
  export { observationQueries, inferCategory, inferAccessPattern } from "./queries/observations.js";
12
+ export { commandQueries } from "./queries/commands.js";
12
13
  export { authQueries } from "./queries/auth.js";
@@ -0,0 +1,67 @@
1
+ import { eq, and, desc } from "drizzle-orm";
2
+ import type { Database } from "../client.js";
3
+ import { commands } from "../schema/index.js";
4
+ import { DEFAULT_TENANT_ID } from "@opentrust/shared";
5
+
6
+ export function commandQueries(db: Database) {
7
+ return {
8
+ async create(data: {
9
+ agentId: string;
10
+ type: string;
11
+ payload?: Record<string, unknown>;
12
+ tenantId?: string;
13
+ }) {
14
+ const now = new Date().toISOString();
15
+ const tenantId = data.tenantId ?? DEFAULT_TENANT_ID;
16
+
17
+ await db.insert(commands).values({
18
+ tenantId,
19
+ agentId: data.agentId,
20
+ type: data.type,
21
+ payload: data.payload ?? null,
22
+ status: "pending",
23
+ createdAt: now,
24
+ updatedAt: now,
25
+ });
26
+ },
27
+
28
+ async findPending(agentId: string, tenantId: string = DEFAULT_TENANT_ID) {
29
+ return db
30
+ .select()
31
+ .from(commands)
32
+ .where(
33
+ and(
34
+ eq(commands.tenantId, tenantId),
35
+ eq(commands.agentId, agentId),
36
+ eq(commands.status, "pending"),
37
+ ),
38
+ )
39
+ .orderBy(commands.createdAt);
40
+ },
41
+
42
+ async findByAgent(agentId: string, tenantId: string = DEFAULT_TENANT_ID, limit = 50) {
43
+ return db
44
+ .select()
45
+ .from(commands)
46
+ .where(
47
+ and(
48
+ eq(commands.tenantId, tenantId),
49
+ eq(commands.agentId, agentId),
50
+ ),
51
+ )
52
+ .orderBy(desc(commands.createdAt))
53
+ .limit(limit);
54
+ },
55
+
56
+ async ack(id: string, status: "running" | "completed" | "failed", result?: Record<string, unknown>) {
57
+ await db
58
+ .update(commands)
59
+ .set({
60
+ status,
61
+ result: result ?? null,
62
+ updatedAt: new Date().toISOString(),
63
+ })
64
+ .where(eq(commands.id, id));
65
+ },
66
+ };
67
+ }
@@ -17,6 +17,7 @@ export const policies = mod.policies;
17
17
  export const usageLogs = mod.usageLogs;
18
18
  export const detectionResults = mod.detectionResults;
19
19
  export const toolCallObservations = mod.toolCallObservations;
20
+ export const commands = mod.commands;
20
21
  export const agentPermissions = mod.agentPermissions;
21
22
  export const magicLinks = mod.magicLinks;
22
23
  export const userSessions = mod.userSessions;
@@ -149,6 +149,26 @@ export const toolCallObservations = mysqlTable(
149
149
  })
150
150
  );
151
151
 
152
+ // ─── Commands ─────────────────────────────────────────────────
153
+ export const commands = mysqlTable(
154
+ "commands",
155
+ {
156
+ id: varchar("id", { length: 36 }).primaryKey().$defaultFn(() => crypto.randomUUID()),
157
+ tenantId: varchar("tenant_id", { length: 64 }).notNull().default("default"),
158
+ agentId: varchar("agent_id", { length: 36 }).notNull(),
159
+ type: varchar("type", { length: 64 }).notNull(),
160
+ payload: json("payload"),
161
+ status: varchar("status", { length: 32 }).notNull().default("pending"),
162
+ result: json("result"),
163
+ createdAt: datetime("created_at").notNull().$defaultFn(() => new Date()),
164
+ updatedAt: datetime("updated_at").notNull().$defaultFn(() => new Date()),
165
+ },
166
+ (table) => ({
167
+ agentStatusIdx: index("idx_commands_agent_status").on(table.tenantId, table.agentId, table.status),
168
+ createdAtIdx: index("idx_commands_created_at").on(table.createdAt),
169
+ })
170
+ );
171
+
152
172
  // ─── Agent Permissions ────────────────────────────────────────
153
173
  export const agentPermissions = mysqlTable(
154
174
  "agent_permissions",
package/src/schema/pg.ts CHANGED
@@ -150,6 +150,26 @@ export const toolCallObservations = pgTable(
150
150
  })
151
151
  );
152
152
 
153
+ // ─── Commands ─────────────────────────────────────────────────
154
+ export const commands = pgTable(
155
+ "commands",
156
+ {
157
+ id: uuid("id").primaryKey().defaultRandom(),
158
+ tenantId: varchar("tenant_id", { length: 64 }).notNull().default("default"),
159
+ agentId: uuid("agent_id").notNull(),
160
+ type: varchar("type", { length: 64 }).notNull(),
161
+ payload: jsonb("payload"),
162
+ status: varchar("status", { length: 32 }).notNull().default("pending"),
163
+ result: jsonb("result"),
164
+ createdAt: timestamp("created_at", { withTimezone: true }).notNull().defaultNow(),
165
+ updatedAt: timestamp("updated_at", { withTimezone: true }).notNull().defaultNow(),
166
+ },
167
+ (table) => ({
168
+ agentStatusIdx: index("idx_commands_agent_status").on(table.tenantId, table.agentId, table.status),
169
+ createdAtIdx: index("idx_commands_created_at").on(table.createdAt),
170
+ })
171
+ );
172
+
153
173
  // ─── Agent Permissions ────────────────────────────────────────
154
174
  export const agentPermissions = pgTable(
155
175
  "agent_permissions",
@@ -174,6 +174,26 @@ export const userSessions = sqliteTable(
174
174
  })
175
175
  );
176
176
 
177
+ // ─── Commands ─────────────────────────────────────────────────
178
+ export const commands = sqliteTable(
179
+ "commands",
180
+ {
181
+ id: text("id").primaryKey().$defaultFn(() => crypto.randomUUID()),
182
+ tenantId: text("tenant_id").notNull().default("default"),
183
+ agentId: text("agent_id").notNull(),
184
+ type: text("type").notNull(),
185
+ payload: text("payload", { mode: "json" }),
186
+ status: text("status").notNull().default("pending"),
187
+ result: text("result", { mode: "json" }),
188
+ createdAt: text("created_at").notNull().$defaultFn(() => new Date().toISOString()),
189
+ updatedAt: text("updated_at").notNull().$defaultFn(() => new Date().toISOString()),
190
+ },
191
+ (table) => ({
192
+ agentStatusIdx: index("idx_commands_agent_status").on(table.tenantId, table.agentId, table.status),
193
+ createdAtIdx: index("idx_commands_created_at").on(table.createdAt),
194
+ })
195
+ );
196
+
177
197
  // ─── Agent Permissions ────────────────────────────────────────
178
198
  export const agentPermissions = sqliteTable(
179
199
  "agent_permissions",