@elizaos/plugin-sql 2.0.0-alpha.2 → 2.0.0-alpha.20

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.
Files changed (108) hide show
  1. package/dist/base.d.ts +1102 -0
  2. package/dist/browser/index.browser.js +3887 -1999
  3. package/dist/browser/index.browser.js.map +32 -20
  4. package/dist/browser/index.d.ts +2 -2
  5. package/dist/cjs/index.d.ts +2 -0
  6. package/dist/cjs/index.node.cjs +8184 -0
  7. package/dist/cjs/index.node.cjs.map +67 -0
  8. package/dist/index.d.ts +4 -2
  9. package/dist/index.js +2 -0
  10. package/dist/index.node.d.ts +17 -0
  11. package/dist/migration-service.d.ts +17 -0
  12. package/dist/migrations.d.ts +15 -0
  13. package/dist/node/index.d.ts +2 -2
  14. package/dist/node/index.node.js +4411 -8649
  15. package/dist/node/index.node.js.map +34 -117
  16. package/dist/pg/adapter.d.ts +41 -0
  17. package/dist/pg/manager.d.ts +14 -0
  18. package/dist/pglite/adapter.d.ts +23 -0
  19. package/dist/pglite/errors.d.ts +20 -0
  20. package/dist/pglite/manager.d.ts +25 -0
  21. package/dist/rls.d.ts +13 -0
  22. package/dist/runtime-migrator/crypto-utils.d.ts +25 -0
  23. package/dist/runtime-migrator/drizzle-adapters/database-introspector.d.ts +58 -0
  24. package/dist/runtime-migrator/drizzle-adapters/diff-calculator.d.ts +77 -0
  25. package/dist/runtime-migrator/drizzle-adapters/snapshot-generator.d.ts +21 -0
  26. package/dist/runtime-migrator/drizzle-adapters/sql-generator.d.ts +38 -0
  27. package/dist/runtime-migrator/extension-manager.d.ts +6 -0
  28. package/dist/runtime-migrator/index.d.ts +8 -0
  29. package/dist/runtime-migrator/runtime-migrator.d.ts +95 -0
  30. package/dist/runtime-migrator/schema-transformer.d.ts +18 -0
  31. package/dist/runtime-migrator/storage/journal-storage.d.ts +10 -0
  32. package/dist/runtime-migrator/storage/migration-tracker.d.ts +13 -0
  33. package/dist/runtime-migrator/storage/snapshot-storage.d.ts +9 -0
  34. package/dist/runtime-migrator/types.d.ts +157 -0
  35. package/dist/schema/agent.d.ts +344 -0
  36. package/dist/schema/approvalRequests.d.ts +277 -0
  37. package/dist/schema/authAuditEvent.d.ts +153 -0
  38. package/dist/schema/authBootstrapJti.d.ts +49 -0
  39. package/dist/schema/authIdentity.d.ts +121 -0
  40. package/dist/schema/authOwnerBinding.d.ts +168 -0
  41. package/dist/schema/authOwnerLoginToken.d.ts +122 -0
  42. package/dist/schema/authSession.d.ts +225 -0
  43. package/dist/schema/cache.d.ts +97 -0
  44. package/dist/schema/channel.d.ts +177 -0
  45. package/dist/schema/channelParticipant.d.ts +41 -0
  46. package/dist/schema/component.d.ts +163 -0
  47. package/dist/schema/embedding.d.ts +204 -0
  48. package/dist/schema/entity.d.ts +125 -0
  49. package/dist/schema/entityIdentity.d.ts +577 -0
  50. package/dist/schema/index.d.ts +34 -0
  51. package/dist/schema/log.d.ts +114 -0
  52. package/dist/schema/longTermMemories.d.ts +254 -0
  53. package/dist/schema/memory.d.ts +185 -0
  54. package/dist/schema/memoryAccessLogs.d.ts +109 -0
  55. package/dist/schema/message.d.ts +194 -0
  56. package/dist/schema/messageServer.d.ts +126 -0
  57. package/dist/schema/messageServerAgent.d.ts +41 -0
  58. package/dist/schema/pairingAllowlist.d.ts +113 -0
  59. package/dist/schema/pairingRequest.d.ts +147 -0
  60. package/dist/schema/participant.d.ts +114 -0
  61. package/dist/schema/relationship.d.ts +156 -0
  62. package/dist/schema/room.d.ts +195 -0
  63. package/dist/schema/server.d.ts +64 -0
  64. package/dist/schema/sessionSummaries.d.ts +273 -0
  65. package/dist/schema/tasks.d.ts +225 -0
  66. package/dist/schema/types.d.ts +68 -0
  67. package/dist/schema/world.d.ts +114 -0
  68. package/dist/services/advanced-memory-storage.d.ts +36 -0
  69. package/dist/types.d.ts +13 -0
  70. package/dist/utils/string-to-uuid.d.ts +2 -0
  71. package/dist/utils.d.ts +4 -0
  72. package/dist/utils.node.d.ts +4 -0
  73. package/drizzle/index.ts +17 -0
  74. package/package.json +50 -17
  75. package/schema/agent.ts +50 -0
  76. package/schema/approvalRequests.ts +57 -0
  77. package/schema/authAuditEvent.ts +29 -0
  78. package/schema/authBootstrapJti.ts +18 -0
  79. package/schema/authIdentity.ts +35 -0
  80. package/schema/authOwnerBinding.ts +43 -0
  81. package/schema/authOwnerLoginToken.ts +50 -0
  82. package/schema/authSession.ts +44 -0
  83. package/schema/cache.ts +22 -0
  84. package/schema/channel.ts +19 -0
  85. package/schema/channelParticipant.ts +13 -0
  86. package/schema/component.ts +37 -0
  87. package/schema/embedding.ts +73 -0
  88. package/schema/entity.ts +28 -0
  89. package/schema/entityIdentity.ts +142 -0
  90. package/schema/index.ts +41 -0
  91. package/schema/log.ts +38 -0
  92. package/schema/longTermMemories.ts +27 -0
  93. package/schema/memory.ts +103 -0
  94. package/schema/memoryAccessLogs.ts +19 -0
  95. package/schema/message.ts +24 -0
  96. package/schema/messageServer.ts +12 -0
  97. package/schema/messageServerAgent.ts +16 -0
  98. package/schema/pairingAllowlist.ts +36 -0
  99. package/schema/pairingRequest.ts +46 -0
  100. package/schema/participant.ts +43 -0
  101. package/schema/relationship.ts +50 -0
  102. package/schema/room.ts +37 -0
  103. package/schema/server.ts +14 -0
  104. package/schema/sessionSummaries.ts +27 -0
  105. package/schema/tasks.ts +24 -0
  106. package/schema/types.ts +42 -0
  107. package/schema/world.ts +20 -0
  108. package/types.ts +19 -0
@@ -0,0 +1,73 @@
1
+ import { relations, sql } from "drizzle-orm";
2
+ import { check, foreignKey, index, pgTable, timestamp, uuid, vector } from "drizzle-orm/pg-core";
3
+ import { memoryTable } from "./memory";
4
+
5
+ export const VECTOR_DIMS = {
6
+ SMALL: 384,
7
+ MEDIUM: 512,
8
+ LARGE: 768,
9
+ XL: 1024,
10
+ XXL: 1536,
11
+ XXXL: 3072,
12
+ } as const;
13
+
14
+ export const DIMENSION_MAP = {
15
+ [VECTOR_DIMS.SMALL]: "dim384",
16
+ [VECTOR_DIMS.MEDIUM]: "dim512",
17
+ [VECTOR_DIMS.LARGE]: "dim768",
18
+ [VECTOR_DIMS.XL]: "dim1024",
19
+ [VECTOR_DIMS.XXL]: "dim1536",
20
+ [VECTOR_DIMS.XXXL]: "dim3072",
21
+ } as const;
22
+
23
+ /**
24
+ * Definition of the embeddings table in the database.
25
+ * Contains columns for ID, Memory ID, Creation Timestamp, and multiple vector dimensions.
26
+ */
27
+ export const embeddingTable = pgTable(
28
+ "embeddings",
29
+ {
30
+ id: uuid("id").primaryKey().defaultRandom().notNull(),
31
+ memoryId: uuid("memory_id").references(() => memoryTable.id, {
32
+ onDelete: "cascade",
33
+ }),
34
+ createdAt: timestamp("created_at").default(sql`now()`).notNull(),
35
+ dim384: vector("dim_384", { dimensions: VECTOR_DIMS.SMALL }),
36
+ dim512: vector("dim_512", { dimensions: VECTOR_DIMS.MEDIUM }),
37
+ dim768: vector("dim_768", { dimensions: VECTOR_DIMS.LARGE }),
38
+ dim1024: vector("dim_1024", { dimensions: VECTOR_DIMS.XL }),
39
+ dim1536: vector("dim_1536", { dimensions: VECTOR_DIMS.XXL }),
40
+ dim3072: vector("dim_3072", { dimensions: VECTOR_DIMS.XXXL }),
41
+ },
42
+ (table) => [
43
+ check("embedding_source_check", sql`"memory_id" IS NOT NULL`),
44
+ index("idx_embedding_memory").on(table.memoryId),
45
+ foreignKey({
46
+ name: "fk_embedding_memory",
47
+ columns: [table.memoryId],
48
+ foreignColumns: [memoryTable.id],
49
+ }).onDelete("cascade"),
50
+ ]
51
+ );
52
+
53
+ /**
54
+ * Defines the possible values for the Embedding Dimension Column.
55
+ * It can be "dim384", "dim512", "dim768", "dim1024", "dim1536", or "dim3072".
56
+ */
57
+ export type EmbeddingDimensionColumn =
58
+ | "dim384"
59
+ | "dim512"
60
+ | "dim768"
61
+ | "dim1024"
62
+ | "dim1536"
63
+ | "dim3072";
64
+
65
+ /**
66
+ * Retrieve the type of a specific column in the EmbeddingTable based on the EmbeddingDimensionColumn key.
67
+ */
68
+ export type EmbeddingTableColumn = (typeof embeddingTable._.columns)[EmbeddingDimensionColumn];
69
+
70
+ // Relations - defined here to avoid circular dependency with memory.ts
71
+ export const memoryRelations = relations(memoryTable, ({ one }) => ({
72
+ embedding: one(embeddingTable),
73
+ }));
@@ -0,0 +1,28 @@
1
+ import type { Metadata } from "@elizaos/core";
2
+ import { sql } from "drizzle-orm";
3
+ import { jsonb, pgTable, text, timestamp, unique, uuid } from "drizzle-orm/pg-core";
4
+ import { agentTable } from "./agent";
5
+
6
+ /**
7
+ * Represents an entity table in the database.
8
+ * Includes columns for id, agentId, createdAt, names, and metadata.
9
+ */
10
+ export const entityTable = pgTable(
11
+ "entities",
12
+ {
13
+ id: uuid("id").notNull().primaryKey(),
14
+ agentId: uuid("agent_id")
15
+ .notNull()
16
+ .references(() => agentTable.id, {
17
+ onDelete: "cascade",
18
+ }),
19
+ createdAt: timestamp("created_at").default(sql`now()`).notNull(),
20
+ names: text("names").array().default(sql`'{}'::text[]`).notNull(),
21
+ metadata: jsonb("metadata").$type<Metadata>().default(sql`'{}'::jsonb`).notNull(),
22
+ },
23
+ (table) => {
24
+ return {
25
+ idAgentIdUnique: unique("id_agent_id_unique").on(table.id, table.agentId),
26
+ };
27
+ }
28
+ );
@@ -0,0 +1,142 @@
1
+ import { sql } from "drizzle-orm";
2
+ import {
3
+ boolean,
4
+ foreignKey,
5
+ index,
6
+ jsonb,
7
+ pgTable,
8
+ real,
9
+ text,
10
+ timestamp,
11
+ unique,
12
+ uuid,
13
+ } from "drizzle-orm/pg-core";
14
+ import { agentTable } from "./agent";
15
+ import { entityTable } from "./entity";
16
+
17
+ /**
18
+ * Strengthened (platform, handle) claim attached to an entity. Re-observations
19
+ * of the same pair bump confidence and append to evidence_message_ids rather
20
+ * than producing duplicate rows.
21
+ */
22
+ export const entityIdentityTable = pgTable(
23
+ "entity_identities",
24
+ {
25
+ id: uuid("id").notNull().primaryKey().default(sql`gen_random_uuid()`),
26
+ entityId: uuid("entity_id")
27
+ .notNull()
28
+ .references(() => entityTable.id, { onDelete: "cascade" }),
29
+ agentId: uuid("agent_id")
30
+ .notNull()
31
+ .references(() => agentTable.id, { onDelete: "cascade" }),
32
+ platform: text("platform").notNull(),
33
+ handle: text("handle").notNull(),
34
+ verified: boolean("verified").notNull().default(false),
35
+ confidence: real("confidence").notNull().default(0),
36
+ source: text("source"),
37
+ firstSeen: timestamp("first_seen", { withTimezone: true }).notNull().default(sql`now()`),
38
+ lastSeen: timestamp("last_seen", { withTimezone: true }).notNull().default(sql`now()`),
39
+ evidenceMessageIds: jsonb("evidence_message_ids").$type<string[]>(),
40
+ createdAt: timestamp("created_at", { withTimezone: true }).notNull().default(sql`now()`),
41
+ },
42
+ (table) => [
43
+ index("idx_entity_identities_entity").on(table.entityId),
44
+ index("idx_entity_identities_platform_handle").on(table.platform, table.handle),
45
+ unique("unique_entity_identity").on(
46
+ table.entityId,
47
+ table.platform,
48
+ table.handle,
49
+ table.agentId
50
+ ),
51
+ foreignKey({
52
+ name: "fk_entity_identities_entity",
53
+ columns: [table.entityId],
54
+ foreignColumns: [entityTable.id],
55
+ }).onDelete("cascade"),
56
+ foreignKey({
57
+ name: "fk_entity_identities_agent",
58
+ columns: [table.agentId],
59
+ foreignColumns: [agentTable.id],
60
+ }).onDelete("cascade"),
61
+ ]
62
+ );
63
+
64
+ export const entityMergeCandidateTable = pgTable(
65
+ "entity_merge_candidates",
66
+ {
67
+ id: uuid("id").notNull().primaryKey().default(sql`gen_random_uuid()`),
68
+ agentId: uuid("agent_id")
69
+ .notNull()
70
+ .references(() => agentTable.id, { onDelete: "cascade" }),
71
+ entityA: uuid("entity_a")
72
+ .notNull()
73
+ .references(() => entityTable.id, { onDelete: "cascade" }),
74
+ entityB: uuid("entity_b")
75
+ .notNull()
76
+ .references(() => entityTable.id, { onDelete: "cascade" }),
77
+ confidence: real("confidence").notNull().default(0),
78
+ evidence: jsonb("evidence"),
79
+ status: text("status").notNull().default("pending"),
80
+ proposedAt: timestamp("proposed_at", { withTimezone: true }).notNull().default(sql`now()`),
81
+ resolvedAt: timestamp("resolved_at", { withTimezone: true }),
82
+ },
83
+ (table) => [
84
+ index("idx_entity_merge_candidates_status").on(table.status),
85
+ index("idx_entity_merge_candidates_pair").on(table.entityA, table.entityB),
86
+ foreignKey({
87
+ name: "fk_entity_merge_candidates_a",
88
+ columns: [table.entityA],
89
+ foreignColumns: [entityTable.id],
90
+ }).onDelete("cascade"),
91
+ foreignKey({
92
+ name: "fk_entity_merge_candidates_b",
93
+ columns: [table.entityB],
94
+ foreignColumns: [entityTable.id],
95
+ }).onDelete("cascade"),
96
+ foreignKey({
97
+ name: "fk_entity_merge_candidates_agent",
98
+ columns: [table.agentId],
99
+ foreignColumns: [agentTable.id],
100
+ }).onDelete("cascade"),
101
+ ]
102
+ );
103
+
104
+ /**
105
+ * Pending fact refinement proposals (contradictions / merges) surfaced from
106
+ * the FactRefinementEvaluator. The Facts tab in the UI lets users accept
107
+ * or reject these.
108
+ */
109
+ export const factCandidateTable = pgTable(
110
+ "fact_candidates",
111
+ {
112
+ id: uuid("id").notNull().primaryKey().default(sql`gen_random_uuid()`),
113
+ agentId: uuid("agent_id")
114
+ .notNull()
115
+ .references(() => agentTable.id, { onDelete: "cascade" }),
116
+ entityId: uuid("entity_id")
117
+ .notNull()
118
+ .references(() => entityTable.id, { onDelete: "cascade" }),
119
+ kind: text("kind").notNull(),
120
+ existingFactId: uuid("existing_fact_id"),
121
+ proposedText: text("proposed_text").notNull(),
122
+ confidence: real("confidence").notNull().default(0),
123
+ evidence: jsonb("evidence"),
124
+ status: text("status").notNull().default("pending"),
125
+ proposedAt: timestamp("proposed_at", { withTimezone: true }).notNull().default(sql`now()`),
126
+ resolvedAt: timestamp("resolved_at", { withTimezone: true }),
127
+ },
128
+ (table) => [
129
+ index("idx_fact_candidates_status").on(table.status),
130
+ index("idx_fact_candidates_entity").on(table.entityId),
131
+ foreignKey({
132
+ name: "fk_fact_candidates_entity",
133
+ columns: [table.entityId],
134
+ foreignColumns: [entityTable.id],
135
+ }).onDelete("cascade"),
136
+ foreignKey({
137
+ name: "fk_fact_candidates_agent",
138
+ columns: [table.agentId],
139
+ foreignColumns: [agentTable.id],
140
+ }).onDelete("cascade"),
141
+ ]
142
+ );
@@ -0,0 +1,41 @@
1
+ export { agentTable } from "./agent";
2
+ export { approvalRequestTable } from "./approvalRequests";
3
+ export type { AuthAuditOutcome } from "./authAuditEvent";
4
+ export { authAuditEventTable } from "./authAuditEvent";
5
+ export { authBootstrapJtiSeenTable } from "./authBootstrapJti";
6
+ export type { AuthIdentityKind } from "./authIdentity";
7
+ export {
8
+ authIdentityCreatedAtDefault,
9
+ authIdentityTable,
10
+ } from "./authIdentity";
11
+ export { authOwnerBindingTable } from "./authOwnerBinding";
12
+ export { authOwnerLoginTokenTable } from "./authOwnerLoginToken";
13
+ export type { AuthSessionKind } from "./authSession";
14
+ export { authSessionTable } from "./authSession";
15
+ export { cacheTable } from "./cache";
16
+ export { channelTable } from "./channel";
17
+ export { channelParticipantsTable } from "./channelParticipant";
18
+ export { componentTable } from "./component";
19
+ export { embeddingTable } from "./embedding";
20
+ export { entityTable } from "./entity";
21
+ export {
22
+ entityIdentityTable,
23
+ entityMergeCandidateTable,
24
+ factCandidateTable,
25
+ } from "./entityIdentity";
26
+ export { logTable } from "./log";
27
+ export { longTermMemories } from "./longTermMemories";
28
+ export { memoryTable } from "./memory";
29
+ export { memoryAccessLogs } from "./memoryAccessLogs";
30
+ export { messageTable } from "./message";
31
+ export { messageServerTable } from "./messageServer";
32
+ export { messageServerAgentsTable } from "./messageServerAgent";
33
+ export { pairingAllowlistTable } from "./pairingAllowlist";
34
+ export { pairingRequestTable } from "./pairingRequest";
35
+ export { participantTable } from "./participant";
36
+ export { relationshipTable } from "./relationship";
37
+ export { roomTable } from "./room";
38
+ export { serverTable } from "./server";
39
+ export { sessionSummaries } from "./sessionSummaries";
40
+ export { taskTable } from "./tasks";
41
+ export { worldTable } from "./world";
package/schema/log.ts ADDED
@@ -0,0 +1,38 @@
1
+ import { sql } from "drizzle-orm";
2
+ import { foreignKey, jsonb, pgTable, text, timestamp, uuid } from "drizzle-orm/pg-core";
3
+ import { entityTable } from "./entity";
4
+ import { roomTable } from "./room";
5
+
6
+ /**
7
+ * Represents a PostgreSQL table for storing logs.
8
+ *
9
+ * @type {Table}
10
+ */
11
+
12
+ export const logTable = pgTable(
13
+ "logs",
14
+ {
15
+ id: uuid("id").defaultRandom().notNull(),
16
+ createdAt: timestamp("created_at", { withTimezone: true }).default(sql`now()`).notNull(),
17
+ entityId: uuid("entity_id")
18
+ .notNull()
19
+ .references(() => entityTable.id, { onDelete: "cascade" }),
20
+ body: jsonb("body").notNull(),
21
+ type: text("type").notNull(),
22
+ roomId: uuid("room_id")
23
+ .notNull()
24
+ .references(() => roomTable.id, { onDelete: "cascade" }),
25
+ },
26
+ (table) => [
27
+ foreignKey({
28
+ name: "fk_room",
29
+ columns: [table.roomId],
30
+ foreignColumns: [roomTable.id],
31
+ }).onDelete("cascade"),
32
+ foreignKey({
33
+ name: "fk_user",
34
+ columns: [table.entityId],
35
+ foreignColumns: [entityTable.id],
36
+ }).onDelete("cascade"),
37
+ ]
38
+ );
@@ -0,0 +1,27 @@
1
+ import { sql } from "drizzle-orm";
2
+ import { index, integer, jsonb, pgTable, real, text, timestamp, uuid } from "drizzle-orm/pg-core";
3
+
4
+ export const longTermMemories = pgTable(
5
+ "long_term_memories",
6
+ {
7
+ id: uuid("id").primaryKey().notNull(),
8
+ agentId: uuid("agent_id").notNull(),
9
+ entityId: uuid("entity_id").notNull(),
10
+ category: text("category").notNull(),
11
+ content: text("content").notNull(),
12
+ metadata: jsonb("metadata"),
13
+ embedding: real("embedding").array(),
14
+ confidence: real("confidence").default(1),
15
+ source: text("source"),
16
+ createdAt: timestamp("created_at").default(sql`now()`).notNull(),
17
+ updatedAt: timestamp("updated_at").default(sql`now()`).notNull(),
18
+ lastAccessedAt: timestamp("last_accessed_at"),
19
+ accessCount: integer("access_count").default(0),
20
+ },
21
+ (table) => [
22
+ index("long_term_memories_agent_entity_idx").on(table.agentId, table.entityId),
23
+ index("long_term_memories_category_idx").on(table.category),
24
+ index("long_term_memories_confidence_idx").on(table.confidence),
25
+ index("long_term_memories_created_at_idx").on(table.createdAt),
26
+ ]
27
+ );
@@ -0,0 +1,103 @@
1
+ import { sql } from "drizzle-orm";
2
+ import {
3
+ boolean,
4
+ check,
5
+ foreignKey,
6
+ index,
7
+ jsonb,
8
+ pgTable,
9
+ text,
10
+ timestamp,
11
+ uuid,
12
+ } from "drizzle-orm/pg-core";
13
+ import { agentTable } from "./agent";
14
+ import { entityTable } from "./entity";
15
+ import { roomTable } from "./room";
16
+
17
+ /**
18
+ * Definition of the memory table in the database.
19
+ *
20
+ * @param {string} tableName - The name of the table.
21
+ * @param {object} columns - An object containing the column definitions.
22
+ * @param {function} indexes - A function that defines the indexes for the table.
23
+ * @returns {object} - The memory table object.
24
+ */
25
+ export const memoryTable = pgTable(
26
+ "memories",
27
+ {
28
+ id: uuid("id").primaryKey().notNull(),
29
+ type: text("type").notNull(),
30
+ createdAt: timestamp("created_at").default(sql`now()`).notNull(),
31
+ content: jsonb("content").notNull(),
32
+ entityId: uuid("entity_id").references(() => entityTable.id, {
33
+ onDelete: "cascade",
34
+ }),
35
+ agentId: uuid("agent_id")
36
+ .references(() => agentTable.id, {
37
+ onDelete: "cascade",
38
+ })
39
+ .notNull(),
40
+ roomId: uuid("room_id").references(() => roomTable.id, {
41
+ onDelete: "cascade",
42
+ }),
43
+ worldId: uuid("world_id"),
44
+ // .references(() => worldTable.id, {
45
+ // onDelete: 'set null',
46
+ // }),
47
+ unique: boolean("unique").default(true).notNull(),
48
+ metadata: jsonb("metadata").default({}).notNull(),
49
+ },
50
+ (table) => [
51
+ index("idx_memories_type_room").on(table.type, table.roomId),
52
+ index("idx_memories_world_id").on(table.worldId),
53
+ foreignKey({
54
+ name: "fk_room",
55
+ columns: [table.roomId],
56
+ foreignColumns: [roomTable.id],
57
+ }).onDelete("cascade"),
58
+ foreignKey({
59
+ name: "fk_user",
60
+ columns: [table.entityId],
61
+ foreignColumns: [entityTable.id],
62
+ }).onDelete("cascade"),
63
+ foreignKey({
64
+ name: "fk_agent",
65
+ columns: [table.agentId],
66
+ foreignColumns: [agentTable.id],
67
+ }).onDelete("cascade"),
68
+ // foreignKey({
69
+ // name: 'fk_world',
70
+ // columns: [table.worldId],
71
+ // foreignColumns: [worldTable.id],
72
+ // }).onDelete('set null'),
73
+ index("idx_memories_metadata_type").on(sql`((metadata->>'type'))`),
74
+ index("idx_memories_document_id").on(sql`((metadata->>'documentId'))`),
75
+ index("idx_fragments_order").on(
76
+ sql`((metadata->>'documentId'))`,
77
+ sql`((metadata->>'position'))`
78
+ ),
79
+ check(
80
+ "fragment_metadata_check",
81
+ sql`
82
+ CASE
83
+ WHEN metadata->>'type' = 'fragment' THEN
84
+ metadata ? 'documentId' AND
85
+ metadata ? 'position'
86
+ ELSE true
87
+ END
88
+ `
89
+ ),
90
+ check(
91
+ "document_metadata_check",
92
+ sql`
93
+ CASE
94
+ WHEN metadata->>'type' = 'document' THEN
95
+ metadata ? 'timestamp'
96
+ ELSE true
97
+ END
98
+ `
99
+ ),
100
+ ]
101
+ );
102
+
103
+ // Relations are defined in embedding.ts to avoid circular dependency
@@ -0,0 +1,19 @@
1
+ import { sql } from "drizzle-orm";
2
+ import { index, pgTable, text, timestamp, uuid } from "drizzle-orm/pg-core";
3
+
4
+ export const memoryAccessLogs = pgTable(
5
+ "memory_access_logs",
6
+ {
7
+ id: uuid("id").primaryKey().notNull(),
8
+ memoryId: uuid("memory_id").notNull(),
9
+ memoryType: text("memory_type").notNull(),
10
+ agentId: uuid("agent_id").notNull(),
11
+ accessType: text("access_type").notNull(),
12
+ accessedAt: timestamp("accessed_at").default(sql`now()`).notNull(),
13
+ },
14
+ (table) => [
15
+ index("memory_access_logs_memory_id_idx").on(table.memoryId),
16
+ index("memory_access_logs_agent_id_idx").on(table.agentId),
17
+ index("memory_access_logs_accessed_at_idx").on(table.accessedAt),
18
+ ]
19
+ );
@@ -0,0 +1,24 @@
1
+ import { sql } from "drizzle-orm";
2
+ import { type AnyPgColumn, jsonb, pgTable, text, timestamp } from "drizzle-orm/pg-core";
3
+ import { channelTable } from "./channel";
4
+
5
+ export const messageTable = pgTable("central_messages", {
6
+ id: text("id").primaryKey(), // UUID stored as text
7
+ channelId: text("channel_id")
8
+ .notNull()
9
+ .references(() => channelTable.id, { onDelete: "cascade" }),
10
+ authorId: text("author_id").notNull(),
11
+ content: text("content").notNull(),
12
+ rawMessage: jsonb("raw_message"),
13
+ inReplyToRootMessageId: text("in_reply_to_root_message_id").references(
14
+ (): AnyPgColumn => messageTable.id,
15
+ {
16
+ onDelete: "set null",
17
+ }
18
+ ),
19
+ sourceType: text("source_type"),
20
+ sourceId: text("source_id"),
21
+ metadata: jsonb("metadata"),
22
+ createdAt: timestamp("created_at", { mode: "date" }).default(sql`CURRENT_TIMESTAMP`).notNull(),
23
+ updatedAt: timestamp("updated_at", { mode: "date" }).default(sql`CURRENT_TIMESTAMP`).notNull(),
24
+ });
@@ -0,0 +1,12 @@
1
+ import { sql } from "drizzle-orm";
2
+ import { jsonb, pgTable, text, timestamp, uuid } from "drizzle-orm/pg-core";
3
+
4
+ export const messageServerTable = pgTable("message_servers", {
5
+ id: uuid("id").primaryKey(),
6
+ name: text("name").notNull(),
7
+ sourceType: text("source_type").notNull(),
8
+ sourceId: text("source_id"),
9
+ metadata: jsonb("metadata"),
10
+ createdAt: timestamp("created_at", { mode: "date" }).default(sql`CURRENT_TIMESTAMP`).notNull(),
11
+ updatedAt: timestamp("updated_at", { mode: "date" }).default(sql`CURRENT_TIMESTAMP`).notNull(),
12
+ });
@@ -0,0 +1,16 @@
1
+ import { pgTable, primaryKey, uuid } from "drizzle-orm/pg-core";
2
+ import { agentTable } from "./agent";
3
+ import { messageServerTable } from "./messageServer";
4
+
5
+ export const messageServerAgentsTable = pgTable(
6
+ "message_server_agents",
7
+ {
8
+ messageServerId: uuid("message_server_id")
9
+ .notNull()
10
+ .references(() => messageServerTable.id, { onDelete: "cascade" }),
11
+ agentId: uuid("agent_id")
12
+ .notNull()
13
+ .references(() => agentTable.id, { onDelete: "cascade" }),
14
+ },
15
+ (table) => [primaryKey({ columns: [table.messageServerId, table.agentId] })]
16
+ );
@@ -0,0 +1,36 @@
1
+ import { sql } from "drizzle-orm";
2
+ import { index, jsonb, pgTable, text, timestamp, uniqueIndex, uuid } from "drizzle-orm/pg-core";
3
+ import { agentTable } from "./agent";
4
+
5
+ /**
6
+ * Represents the allowlist of approved senders for each channel.
7
+ * Senders in this list are permitted to send DMs to the bot.
8
+ */
9
+ export const pairingAllowlistTable = pgTable(
10
+ "pairing_allowlist",
11
+ {
12
+ id: uuid("id").primaryKey().defaultRandom(),
13
+ /** The messaging channel (telegram, discord, whatsapp, etc.) */
14
+ channel: text("channel").notNull(),
15
+ /** Approved sender identifier */
16
+ senderId: text("sender_id").notNull(),
17
+ /** When the entry was added */
18
+ createdAt: timestamp("created_at", { withTimezone: true }).default(sql`now()`).notNull(),
19
+ /** Optional metadata about the approved sender */
20
+ metadata: jsonb("metadata").default(sql`'{}'::jsonb`),
21
+ /** Agent ID this allowlist belongs to */
22
+ agentId: uuid("agent_id")
23
+ .notNull()
24
+ .references(() => agentTable.id, { onDelete: "cascade" }),
25
+ },
26
+ (table) => [
27
+ // Index for looking up allowlist by channel and agent
28
+ index("pairing_allowlist_channel_agent_idx").on(table.channel, table.agentId),
29
+ // Unique constraint to prevent duplicate entries
30
+ uniqueIndex("pairing_allowlist_sender_channel_agent_idx").on(
31
+ table.senderId,
32
+ table.channel,
33
+ table.agentId
34
+ ),
35
+ ]
36
+ );
@@ -0,0 +1,46 @@
1
+ import { sql } from "drizzle-orm";
2
+ import { index, jsonb, pgTable, text, timestamp, uniqueIndex, uuid } from "drizzle-orm/pg-core";
3
+ import { agentTable } from "./agent";
4
+
5
+ /**
6
+ * Represents pending pairing requests from users trying to access the bot via DM.
7
+ * Requests are stored temporarily until approved or expired.
8
+ */
9
+ export const pairingRequestTable = pgTable(
10
+ "pairing_requests",
11
+ {
12
+ id: uuid("id").primaryKey().defaultRandom(),
13
+ /** The messaging channel (telegram, discord, whatsapp, etc.) */
14
+ channel: text("channel").notNull(),
15
+ /** User identifier on the channel (userId, phoneNumber, etc.) */
16
+ senderId: text("sender_id").notNull(),
17
+ /** Human-friendly pairing code */
18
+ code: text("code").notNull(),
19
+ /** When the request was created */
20
+ createdAt: timestamp("created_at", { withTimezone: true }).default(sql`now()`).notNull(),
21
+ /** When the request was last seen/updated */
22
+ lastSeenAt: timestamp("last_seen_at", { withTimezone: true }).default(sql`now()`).notNull(),
23
+ /** Optional metadata about the requester */
24
+ metadata: jsonb("metadata").default(sql`'{}'::jsonb`),
25
+ /** Agent ID that received this request */
26
+ agentId: uuid("agent_id")
27
+ .notNull()
28
+ .references(() => agentTable.id, { onDelete: "cascade" }),
29
+ },
30
+ (table) => [
31
+ // Index for looking up requests by channel and agent
32
+ index("pairing_requests_channel_agent_idx").on(table.channel, table.agentId),
33
+ // Unique constraint on code per channel/agent to prevent duplicates
34
+ uniqueIndex("pairing_requests_code_channel_agent_idx").on(
35
+ table.code,
36
+ table.channel,
37
+ table.agentId
38
+ ),
39
+ // Unique constraint on sender per channel/agent (one request per user)
40
+ uniqueIndex("pairing_requests_sender_channel_agent_idx").on(
41
+ table.senderId,
42
+ table.channel,
43
+ table.agentId
44
+ ),
45
+ ]
46
+ );
@@ -0,0 +1,43 @@
1
+ import { sql } from "drizzle-orm";
2
+ import { foreignKey, index, pgTable, text, timestamp, uuid } from "drizzle-orm/pg-core";
3
+ import { agentTable } from "./agent";
4
+ import { entityTable } from "./entity";
5
+ import { roomTable } from "./room";
6
+
7
+ /**
8
+ * Defines the schema for the "participants" table in the database.
9
+ *
10
+ * @type {import('knex').TableBuilder}
11
+ */
12
+ export const participantTable = pgTable(
13
+ "participants",
14
+ {
15
+ id: uuid("id").notNull().primaryKey().default(sql`gen_random_uuid()`),
16
+ createdAt: timestamp("created_at", { withTimezone: true }).default(sql`now()`).notNull(),
17
+ entityId: uuid("entity_id").references(() => entityTable.id, {
18
+ onDelete: "cascade",
19
+ }),
20
+ roomId: uuid("room_id").references(() => roomTable.id, {
21
+ onDelete: "cascade",
22
+ }),
23
+ agentId: uuid("agent_id").references(() => agentTable.id, {
24
+ onDelete: "cascade",
25
+ }),
26
+ roomState: text("room_state"),
27
+ },
28
+ (table) => [
29
+ // unique("participants_user_room_agent_unique").on(table.entityId, table.roomId, table.agentId),
30
+ index("idx_participants_user").on(table.entityId),
31
+ index("idx_participants_room").on(table.roomId),
32
+ foreignKey({
33
+ name: "fk_room",
34
+ columns: [table.roomId],
35
+ foreignColumns: [roomTable.id],
36
+ }).onDelete("cascade"),
37
+ foreignKey({
38
+ name: "fk_user",
39
+ columns: [table.entityId],
40
+ foreignColumns: [entityTable.id],
41
+ }).onDelete("cascade"),
42
+ ]
43
+ );