@opengeni/db 4.4.0-canary.0 → 4.4.0-canary.2

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 (105) hide show
  1. package/dist/canonical-human-identities.js +4 -3
  2. package/dist/{chunk-2ZMTQVTR.js → chunk-2KOLL33E.js} +2 -2
  3. package/dist/chunk-45CP2YKY.js +1062 -0
  4. package/dist/chunk-45CP2YKY.js.map +1 -0
  5. package/dist/{chunk-6TKL3GQO.js → chunk-5UUWZDAV.js} +6531 -6480
  6. package/dist/chunk-5UUWZDAV.js.map +1 -0
  7. package/dist/{chunk-QLA3UQNX.js → chunk-6IJD3XXF.js} +17 -3
  8. package/dist/chunk-6IJD3XXF.js.map +1 -0
  9. package/dist/{chunk-232VC2LW.js → chunk-6V3HDEMR.js} +2 -2
  10. package/dist/{chunk-M7PSMVFF.js → chunk-CHBLBEU6.js} +2 -2
  11. package/dist/chunk-M4PKBJRS.js +20318 -0
  12. package/dist/chunk-M4PKBJRS.js.map +1 -0
  13. package/dist/chunk-S2TZ3F4L.js +237 -0
  14. package/dist/chunk-S2TZ3F4L.js.map +1 -0
  15. package/dist/{chunk-UK57QA2H.js → chunk-UOXWVP3T.js} +2 -2
  16. package/dist/{chunk-GVZJ7XSR.js → chunk-WFU6PS54.js} +11 -4
  17. package/dist/chunk-WFU6PS54.js.map +1 -0
  18. package/dist/{chunk-THJGMEK5.js → chunk-XJP7KJ6U.js} +121 -66
  19. package/dist/chunk-XJP7KJ6U.js.map +1 -0
  20. package/dist/connection-token-resolver.d.ts +2 -0
  21. package/dist/database.d.ts +2 -0
  22. package/dist/editable-artifact-durable-export.js +3 -2
  23. package/dist/editable-artifact-durable-export.js.map +1 -1
  24. package/dist/editable-artifacts.js +4 -3
  25. package/dist/external-identities.d.ts +1 -0
  26. package/dist/external-membership-operations.d.ts +44 -0
  27. package/dist/governed-learning-evaluator.d.ts +1 -14
  28. package/dist/host-mcp-bindings.d.ts +4 -0
  29. package/dist/host-mcp-resolvers.d.ts +47 -0
  30. package/dist/host-mcp-task-authority.d.ts +2 -6
  31. package/dist/index.d.ts +29 -31
  32. package/dist/index.js +8616 -8321
  33. package/dist/index.js.map +1 -1
  34. package/dist/knowledge-document-preparation.d.ts +56 -0
  35. package/dist/knowledge-entries-schema.d.ts +2479 -0
  36. package/dist/knowledge-entries.d.ts +421 -0
  37. package/dist/knowledge-indexing.d.ts +99 -0
  38. package/dist/knowledge-migration.d.ts +6 -0
  39. package/dist/managed-auth-session-sets.js +4 -3
  40. package/dist/mcp-operations.js +3 -2
  41. package/dist/mcp-operations.js.map +1 -1
  42. package/dist/migrate.js +2 -1
  43. package/dist/provision-roles.js +1 -1
  44. package/dist/retained-provider-commands.js +3 -2
  45. package/dist/retained-provider-commands.js.map +1 -1
  46. package/dist/runtime-posture.d.ts +7 -7
  47. package/dist/schema.d.ts +115 -12
  48. package/dist/schema.js +26 -1
  49. package/dist/session-background-commands.js +5 -4
  50. package/dist/session-command-output.js +8 -6
  51. package/dist/session-command-output.js.map +1 -1
  52. package/dist/session-event-slices.js +6 -4
  53. package/dist/session-event-slices.js.map +1 -1
  54. package/dist/session-mcp-credential-rotation.js +5 -4
  55. package/dist/session-mcp-credential-rotation.js.map +1 -1
  56. package/dist/session-tenancy.js +4 -3
  57. package/dist/video-generation.js +4 -3
  58. package/dist/workspace-artifacts.d.ts +1 -0
  59. package/dist/workspace-instruction-policies-schema.d.ts +21 -2
  60. package/dist/workspace-instruction-policies.d.ts +1 -1
  61. package/dist/workspace-membership-access.d.ts +11 -0
  62. package/dist/workspace-tool-defaults.js +5 -4
  63. package/dist/workspace-tool-defaults.js.map +1 -1
  64. package/drizzle/0461_unified_knowledge.sql +2859 -0
  65. package/drizzle/0462_agent_instruction_non_destructive_edits.sql +212 -0
  66. package/drizzle/0463_host_mcp_resolver_registration.sql +111 -0
  67. package/drizzle/0464_external_workspace_membership_operations.sql +166 -0
  68. package/package.json +6 -6
  69. package/src/connection-token-resolver.ts +75 -6
  70. package/src/database.ts +12 -1
  71. package/src/external-identities.ts +1 -0
  72. package/src/external-membership-operations.ts +134 -0
  73. package/src/governed-learning-evaluator.ts +0 -69
  74. package/src/host-mcp-binding-match.ts +1 -0
  75. package/src/host-mcp-bindings.ts +24 -1
  76. package/src/host-mcp-resolvers.ts +266 -0
  77. package/src/host-mcp-task-authority.ts +11 -10
  78. package/src/index.ts +307 -128
  79. package/src/knowledge-document-preparation.ts +90 -0
  80. package/src/knowledge-entries-schema.ts +264 -0
  81. package/src/knowledge-entries.ts +860 -0
  82. package/src/knowledge-indexing.ts +126 -0
  83. package/src/knowledge-migration.ts +761 -0
  84. package/src/knowledge-source-sync.ts +46 -7
  85. package/src/migrate.ts +19 -0
  86. package/src/provision-roles.ts +23 -57
  87. package/src/runtime-posture.ts +103 -7
  88. package/src/schema.ts +15 -3
  89. package/src/skills.ts +4 -0
  90. package/src/video-generation.ts +14 -0
  91. package/src/workspace-artifacts.ts +2 -0
  92. package/src/workspace-instruction-policies-schema.ts +1 -0
  93. package/src/workspace-membership-access.ts +61 -0
  94. package/dist/chunk-6TKL3GQO.js.map +0 -1
  95. package/dist/chunk-EYF6MH6C.js +0 -4772
  96. package/dist/chunk-EYF6MH6C.js.map +0 -1
  97. package/dist/chunk-GSBONN55.js +0 -533
  98. package/dist/chunk-GSBONN55.js.map +0 -1
  99. package/dist/chunk-GVZJ7XSR.js.map +0 -1
  100. package/dist/chunk-QLA3UQNX.js.map +0 -1
  101. package/dist/chunk-THJGMEK5.js.map +0 -1
  102. /package/dist/{chunk-2ZMTQVTR.js.map → chunk-2KOLL33E.js.map} +0 -0
  103. /package/dist/{chunk-232VC2LW.js.map → chunk-6V3HDEMR.js.map} +0 -0
  104. /package/dist/{chunk-M7PSMVFF.js.map → chunk-CHBLBEU6.js.map} +0 -0
  105. /package/dist/{chunk-UK57QA2H.js.map → chunk-UOXWVP3T.js.map} +0 -0
@@ -0,0 +1,90 @@
1
+ import { FileAsset, KnowledgeEntryWriteReceipt } from "@opengeni/contracts";
2
+ import { sql } from "drizzle-orm";
3
+ import { z } from "zod";
4
+ import { knowledgeOriginalFileAsset } from "./knowledge-entries";
5
+ import { rawRows, withWorkspaceRls, type Database } from "./database";
6
+ import { toPostgresLosslessJson, toPostgresLosslessText } from "./lossless-json";
7
+
8
+ const Identity = z.object({
9
+ accountId: z.uuid(),
10
+ workspaceId: z.uuid(),
11
+ documentId: z.uuid(),
12
+ fileId: z.uuid(),
13
+ });
14
+ const Claim = z.object({
15
+ status: z.literal("prepare"),
16
+ entryId: z.uuid(),
17
+ leaseId: z.uuid(),
18
+ fileId: z.uuid(),
19
+ file: FileAsset,
20
+ });
21
+ const Completed = z.discriminatedUnion("status", [
22
+ z.object({ status: z.literal("retained"), receipt: KnowledgeEntryWriteReceipt }),
23
+ z.object({ status: z.literal("unchanged"), entryId: z.uuid(), revisionId: z.uuid() }),
24
+ ]);
25
+
26
+ async function prepare(
27
+ db: Database,
28
+ input: z.infer<typeof Identity>,
29
+ request: Record<string, unknown>,
30
+ ) {
31
+ const identity = Identity.parse(input);
32
+ return withWorkspaceRls(db, identity.workspaceId, async (tx) => {
33
+ const [row] = await rawRows<{ result: unknown }>(
34
+ tx,
35
+ sql`SELECT knowledge_document_prepare(
36
+ ${identity.accountId}::uuid,${identity.workspaceId}::uuid,${identity.documentId}::uuid,
37
+ ${JSON.stringify({ ...request, fileId: identity.fileId })}::jsonb) AS result`,
38
+ );
39
+ return row?.result;
40
+ });
41
+ }
42
+
43
+ /** Internal adapter for an existing, authorized Document indexing obligation.
44
+ * No public tool receives the lease or can construct this processing actor. */
45
+ export async function claimKnowledgeDocumentPreparation(
46
+ db: Database,
47
+ input: z.infer<typeof Identity>,
48
+ ) {
49
+ const raw = z
50
+ .record(z.string(), z.unknown())
51
+ .parse(await prepare(db, input, { operation: "claim" }));
52
+ return z.union([Claim, z.object({ status: z.literal("disabled"), fileId: z.uuid() })]).parse(
53
+ raw.status === "prepare"
54
+ ? {
55
+ ...raw,
56
+ file: knowledgeOriginalFileAsset(z.record(z.string(), z.unknown()).parse(raw.file)),
57
+ }
58
+ : raw,
59
+ );
60
+ }
61
+
62
+ export async function completeKnowledgeDocumentPreparation(
63
+ db: Database,
64
+ input: z.infer<typeof Identity> & {
65
+ leaseId: string;
66
+ title: string;
67
+ content: string;
68
+ sourceVersion: string;
69
+ },
70
+ ) {
71
+ const text = z
72
+ .string()
73
+ .refine((value) => Boolean(value.trim()), "No source text was extracted")
74
+ .parse(input.content);
75
+ return Completed.parse(
76
+ await prepare(db, input, {
77
+ operation: "complete",
78
+ leaseId: z.uuid().parse(input.leaseId),
79
+ sourceVersion: z
80
+ .string()
81
+ .regex(/^[a-f0-9]{64}$/)
82
+ .parse(input.sourceVersion),
83
+ title: toPostgresLosslessJson(z.string().min(1).parse(input.title)),
84
+ content: toPostgresLosslessJson(text),
85
+ preview: toPostgresLosslessText(text.slice(0, 512)),
86
+ searchText: toPostgresLosslessText(`${input.title}\n${text}`),
87
+ codecVersion: 1,
88
+ }),
89
+ );
90
+ }
@@ -0,0 +1,264 @@
1
+ import type {
2
+ KnowledgeEntryContent,
3
+ KnowledgeEntryWriteReceipt,
4
+ AgentLearningOverrides,
5
+ } from "@opengeni/contracts";
6
+ import {
7
+ boolean,
8
+ customType,
9
+ index,
10
+ integer,
11
+ jsonb,
12
+ pgTable,
13
+ primaryKey,
14
+ text,
15
+ timestamp,
16
+ uniqueIndex,
17
+ uuid,
18
+ } from "drizzle-orm/pg-core";
19
+ import { losslessCodecVersion, losslessJsonb, losslessText } from "./lossless-columns";
20
+
21
+ // SQL owns composite foreign keys, immutable history guards, tenant policies
22
+ // and lifecycle-only grants. These mappings are not an alternate write API.
23
+ export const agentLearningRevisions = pgTable(
24
+ "agent_learning_revisions",
25
+ {
26
+ id: uuid("id").primaryKey().defaultRandom(),
27
+ accountId: uuid("account_id").notNull(),
28
+ originWorkspaceId: uuid("origin_workspace_id").notNull(),
29
+ ownerKey: text("owner_key").notNull(),
30
+ subjectId: text("subject_id"),
31
+ contextKey: text("context_key").notNull().default("defaults"),
32
+ version: integer("version").notNull(),
33
+ settings: jsonb("settings").$type<AgentLearningOverrides>().notNull(),
34
+ operationId: uuid("operation_id").notNull(),
35
+ requestHash: text("request_hash").notNull(),
36
+ actorSubjectId: text("actor_subject_id").notNull(),
37
+ createdAt: timestamp("created_at", { withTimezone: true }).notNull().defaultNow(),
38
+ },
39
+ (t) => ({
40
+ current: index("agent_learning_revisions_current_idx").on(
41
+ t.accountId,
42
+ t.ownerKey,
43
+ t.contextKey,
44
+ t.version,
45
+ ),
46
+ operation: uniqueIndex("agent_learning_revisions_operation_idx").on(t.accountId, t.operationId),
47
+ }),
48
+ );
49
+
50
+ export const agentLearningSnapshots = pgTable(
51
+ "agent_learning_snapshots",
52
+ {
53
+ accountId: uuid("account_id").notNull(),
54
+ workspaceId: uuid("workspace_id").notNull(),
55
+ sessionId: uuid("session_id").notNull(),
56
+ turnId: uuid("turn_id").notNull(),
57
+ snapshot: jsonb("snapshot").notNull(),
58
+ createdAt: timestamp("created_at", { withTimezone: true }).notNull().defaultNow(),
59
+ },
60
+ (t) => ({ identity: primaryKey({ columns: [t.accountId, t.turnId] }) }),
61
+ );
62
+
63
+ export const knowledgeEntries = pgTable(
64
+ "knowledge_entries",
65
+ {
66
+ id: uuid("id").primaryKey().defaultRandom(),
67
+ accountId: uuid("account_id").notNull(),
68
+ originWorkspaceId: uuid("origin_workspace_id").notNull(),
69
+ scope: text("scope").$type<"organization" | "workspace" | "personal">().notNull(),
70
+ scopeWorkspaceId: uuid("scope_workspace_id"),
71
+ scopeSubjectId: text("scope_subject_id"),
72
+ version: integer("version").notNull().default(0),
73
+ publishedRevisionId: uuid("published_revision_id"),
74
+ latestRevisionId: uuid("latest_revision_id"),
75
+ archived: boolean("archived").notNull().default(false),
76
+ legacyMemoryId: uuid("legacy_memory_id"),
77
+ legacyDocumentId: uuid("legacy_document_id"),
78
+ legacyClaimId: uuid("legacy_claim_id"),
79
+ preparedFileId: uuid("prepared_file_id"),
80
+ documentPreparation: jsonb("document_preparation"),
81
+ legacyDocumentVersionId: uuid("legacy_document_version_id"),
82
+ legacyScopeWorkspaceId: uuid("legacy_scope_workspace_id"),
83
+ accessDocumentId: uuid("access_document_id"),
84
+ accessFileId: uuid("access_file_id"),
85
+ legacyScopeType: text("legacy_scope_type"),
86
+ legacyScopeRoleKey: text("legacy_scope_role_key"),
87
+ legacyScopeSessionId: uuid("legacy_scope_session_id"),
88
+ validFrom: timestamp("valid_from", { withTimezone: true }).notNull().defaultNow(),
89
+ validUntil: timestamp("valid_until", { withTimezone: true }),
90
+ createdAt: timestamp("created_at", { withTimezone: true }).notNull().defaultNow(),
91
+ updatedAt: timestamp("updated_at", { withTimezone: true }).notNull().defaultNow(),
92
+ },
93
+ (t) => ({
94
+ scope: index("knowledge_entries_scope_idx").on(
95
+ t.accountId,
96
+ t.scope,
97
+ t.scopeWorkspaceId,
98
+ t.scopeSubjectId,
99
+ t.id,
100
+ ),
101
+ }),
102
+ );
103
+
104
+ export const knowledgeReviewBatches = pgTable("knowledge_review_batches", {
105
+ id: uuid("id").primaryKey().defaultRandom(),
106
+ accountId: uuid("account_id").notNull(),
107
+ originWorkspaceId: uuid("origin_workspace_id").notNull(),
108
+ ownerKey: text("owner_key").notNull(),
109
+ sessionId: uuid("session_id"),
110
+ turnId: uuid("turn_id"),
111
+ scheduledTaskRunId: uuid("scheduled_task_run_id"),
112
+ createdAt: timestamp("created_at", { withTimezone: true }).notNull().defaultNow(),
113
+ });
114
+
115
+ export const knowledgeEntryRevisions = pgTable("knowledge_entry_revisions", {
116
+ id: uuid("id").primaryKey().defaultRandom(),
117
+ accountId: uuid("account_id").notNull(),
118
+ entryId: uuid("entry_id").notNull(),
119
+ number: integer("number").notNull(),
120
+ changeKind: text("change_kind").notNull().default("upsert"),
121
+ body: losslessJsonb("body").$type<KnowledgeEntryContent>().notNull(),
122
+ bodyCodecVersion: losslessCodecVersion("body_codec_version"),
123
+ legacySnapshot: jsonb("legacy_snapshot").$type<Record<string, unknown>>(),
124
+ preview: losslessText("preview").notNull().default(""),
125
+ previewCodecVersion: losslessCodecVersion("preview_codec_version"),
126
+ previousRevisionId: uuid("previous_revision_id"),
127
+ restoredFromRevisionId: uuid("restored_from_revision_id"),
128
+ actor: jsonb("actor").notNull(),
129
+ createdBySessionId: uuid("created_by_session_id"),
130
+ createdByTurnId: uuid("created_by_turn_id"),
131
+ reviewBatchId: uuid("review_batch_id"),
132
+ createdAt: timestamp("created_at", { withTimezone: true }).notNull().defaultNow(),
133
+ });
134
+
135
+ export const knowledgeEntryLinks = pgTable(
136
+ "knowledge_entry_links",
137
+ {
138
+ accountId: uuid("account_id").notNull(),
139
+ entryId: uuid("entry_id").notNull(),
140
+ revisionId: uuid("revision_id").notNull(),
141
+ ordinal: integer("ordinal").notNull(),
142
+ targetEntryId: uuid("target_entry_id").notNull(),
143
+ targetRevisionId: uuid("target_revision_id"),
144
+ relation: text("relation").notNull(),
145
+ },
146
+ (t) => ({
147
+ identity: primaryKey({ columns: [t.revisionId, t.ordinal] }),
148
+ target: index("knowledge_entry_links_target_idx").on(
149
+ t.accountId,
150
+ t.targetEntryId,
151
+ t.relation,
152
+ t.revisionId,
153
+ ),
154
+ }),
155
+ );
156
+
157
+ export const knowledgeEntryDecisions = pgTable("knowledge_entry_decisions", {
158
+ id: uuid("id").primaryKey().defaultRandom(),
159
+ accountId: uuid("account_id").notNull(),
160
+ entryId: uuid("entry_id").notNull(),
161
+ revisionId: uuid("revision_id").notNull(),
162
+ version: integer("version").notNull(),
163
+ outcome: text("outcome").notNull(),
164
+ actor: jsonb("actor").notNull(),
165
+ policySnapshot: jsonb("policy_snapshot"),
166
+ createdAt: timestamp("created_at", { withTimezone: true }).notNull().defaultNow(),
167
+ });
168
+
169
+ export const knowledgeEntryOperations = pgTable(
170
+ "knowledge_entry_operations",
171
+ {
172
+ accountId: uuid("account_id").notNull(),
173
+ originWorkspaceId: uuid("origin_workspace_id").notNull(),
174
+ operationId: uuid("operation_id").notNull(),
175
+ entryId: uuid("entry_id").notNull(),
176
+ actor: jsonb("actor").notNull(),
177
+ requestHash: text("request_hash").notNull(),
178
+ receipt: jsonb("receipt").$type<KnowledgeEntryWriteReceipt>().notNull(),
179
+ createdAt: timestamp("created_at", { withTimezone: true }).notNull().defaultNow(),
180
+ },
181
+ (t) => ({ identity: primaryKey({ columns: [t.accountId, t.operationId] }) }),
182
+ );
183
+
184
+ const tsvector = customType<{ data: string }>({ dataType: () => "tsvector" });
185
+ export const knowledgeEntrySearch = pgTable(
186
+ "knowledge_entry_search",
187
+ {
188
+ accountId: uuid("account_id").notNull(),
189
+ entryId: uuid("entry_id").notNull(),
190
+ revisionId: uuid("revision_id").notNull(),
191
+ chunkIndex: integer("chunk_index").notNull(),
192
+ searchVector: tsvector("search_vector").notNull(),
193
+ },
194
+ (t) => ({
195
+ identity: primaryKey({ columns: [t.accountId, t.entryId, t.revisionId, t.chunkIndex] }),
196
+ }),
197
+ );
198
+
199
+ export const knowledgeIndexJobs = pgTable("knowledge_index_jobs", {
200
+ accountId: uuid("account_id").notNull(),
201
+ entryId: uuid("entry_id").notNull(),
202
+ revisionId: uuid("revision_id").primaryKey(),
203
+ state: text("state")
204
+ .$type<"pending" | "running" | "ready" | "obsolete">()
205
+ .notNull()
206
+ .default("pending"),
207
+ model: text("model"),
208
+ dimensions: integer("dimensions"),
209
+ generation: integer("generation").notNull().default(0),
210
+ completedGeneration: integer("completed_generation"),
211
+ nextIndex: integer("next_index").notNull().default(0),
212
+ leaseId: uuid("lease_id"),
213
+ leaseUntil: timestamp("lease_until", { withTimezone: true }),
214
+ attempts: integer("attempts").notNull().default(0),
215
+ nextAttemptAt: timestamp("next_attempt_at", { withTimezone: true }).notNull().defaultNow(),
216
+ lastFailure: text("last_failure"),
217
+ createdAt: timestamp("created_at", { withTimezone: true }).notNull().defaultNow(),
218
+ });
219
+
220
+ // The provider is configurable. SQL validates each row's dimensions; a model
221
+ // change builds a new generation before replacing the completed projection.
222
+ const knowledgeVector = customType<{ data: number[]; driverData: string }>({
223
+ dataType: () => "vector",
224
+ toDriver: (value) => JSON.stringify(value),
225
+ fromDriver: (value) => JSON.parse(value) as number[],
226
+ });
227
+ export const knowledgeEntryVectors = pgTable(
228
+ "knowledge_entry_vectors",
229
+ {
230
+ accountId: uuid("account_id").notNull(),
231
+ entryId: uuid("entry_id").notNull(),
232
+ revisionId: uuid("revision_id").notNull(),
233
+ generation: integer("generation").notNull(),
234
+ chunkIndex: integer("chunk_index").notNull(),
235
+ model: text("model").notNull(),
236
+ dimensions: integer("dimensions").notNull(),
237
+ field: text("field").$type<"title" | "content">().notNull(),
238
+ startOffset: integer("start_offset").notNull(),
239
+ endOffset: integer("end_offset").notNull(),
240
+ text: losslessText("text").notNull(),
241
+ textCodecVersion: losslessCodecVersion("text_codec_version").notNull(),
242
+ embedding: knowledgeVector("embedding").notNull(),
243
+ },
244
+ (t) => ({
245
+ identity: primaryKey({
246
+ columns: [t.accountId, t.entryId, t.revisionId, t.generation, t.chunkIndex],
247
+ }),
248
+ }),
249
+ );
250
+
251
+ export const agentInstructionOperations = pgTable(
252
+ "agent_instruction_operations",
253
+ {
254
+ accountId: uuid("account_id").notNull(),
255
+ originWorkspaceId: uuid("origin_workspace_id").notNull(),
256
+ operationId: uuid("operation_id").notNull(),
257
+ revisionId: uuid("revision_id").notNull(),
258
+ requestHash: text("request_hash").notNull(),
259
+ actor: jsonb("actor").notNull(),
260
+ receipt: jsonb("receipt").notNull(),
261
+ createdAt: timestamp("created_at", { withTimezone: true }).notNull().defaultNow(),
262
+ },
263
+ (t) => ({ identity: primaryKey({ columns: [t.accountId, t.operationId] }) }),
264
+ );