Repository navigation
Expand file tree
/
Copy pathcontent-subscriptions.ts
More file actions
184 lines (183 loc) · 5.75 KB
/
Copy pathcontent-subscriptions.ts
File metadata and controls
184 lines (183 loc) · 5.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import { randomUUID } from "node:crypto";
import { z } from "zod";
import type { DB, Schema } from "@db/index.js";
import { transact } from "@db/transactions.js";
import type { Actor } from "../identity/passwords.js";
import type { JsonObject } from "@smartdoca/plugin-sdk";
import type { PluginRequestContext } from "@smartdoca/plugin-sdk/platform";
import { createContentService } from "../content/service.js";
import {
readContentInventory,
} from "../content/snapshot.js";
import { authorize } from "../access/queries.js";
import { fail } from "../../shared/errors.js";
const configSchema = z
.object({
protocol: z.literal("content.v1"),
sourceId: z.string().min(1).max(200),
config: z.record(z.string(), z.json()),
principalId: z.string().uuid(),
})
.strict();
export function contentRequest(actor: Actor): PluginRequestContext {
return {
requestId: randomUUID(),
signal: AbortSignal.timeout(120_000),
principal: {
id: actor.id,
displayName: actor.display_name,
publicId: "",
admin: !!actor.admin,
},
};
}
export async function contentSubscriptionConfig(
db: DB,
source: Pick<
Schema["knowledge_subscriptions"],
"source_kind" | "source_id" | "library_id" | "creator_id"
>,
) {
if (source.source_kind !== "content") fail(400, "Not a content subscription");
const group = await db
.selectFrom("knowledge_source_groups")
.selectAll()
.where("id", "=", source.source_id)
.where("library_id", "=", source.library_id)
.where("source_kind", "=", "content")
.executeTakeFirst();
if (!group?.config)
fail(409, "Content subscription configuration is missing");
const parsed = configSchema.safeParse(JSON.parse(group.config));
if (!parsed.success || parsed.data.principalId !== source.creator_id)
fail(409, "Invalid content subscription configuration");
return { group, value: parsed.data };
}
export async function createContentSubscription(
db: DB,
actor: Actor,
libraryId: string,
input: { sourceId: string; config: JsonObject; title: string },
) {
await authorize(db, actor, libraryId, 4);
const library = await db
.selectFrom("resources")
.select("kind")
.where("id", "=", libraryId)
.executeTakeFirst();
if (library?.kind !== "library") fail(400, "A library is required");
const config = configSchema.parse({
protocol: "content.v1",
sourceId: input.sourceId,
config: input.config,
principalId: actor.id,
});
// Validate provider scope before persisting; do not acknowledge any blocks until analysis succeeds.
await readContentInventory(createContentService(db), contentRequest(actor), {
sourceId: input.sourceId,
purpose: "knowledge",
config: input.config,
});
const id = randomUUID(),
groupId = randomUUID(),
now = new Date().toISOString();
await transact(db, async (tx) => {
await authorize(tx, actor, libraryId, 4);
await tx
.insertInto("knowledge_source_groups")
.values({
id: groupId,
library_id: libraryId,
title: input.title,
source_kind: "content",
created_at: now,
config: JSON.stringify(config),
})
.execute();
await tx
.insertInto("knowledge_subscriptions")
.values({
id,
group_id: groupId,
library_id: libraryId,
name: input.title,
creator_id: actor.id,
source_kind: "content",
source_id: groupId,
url: "",
source_version: "",
status: "active",
created_at: now,
})
.execute();
});
return { id, groupId };
}
export async function contentSubscriptionInventory(
db: DB,
actor: Actor,
source: Schema["knowledge_subscriptions"],
) {
const { value } = await contentSubscriptionConfig(db, source);
return readContentInventory(createContentService(db), contentRequest(actor), {
sourceId: value.sourceId,
purpose: "knowledge",
config: value.config as JsonObject,
});
}
export async function updateContentSubscription(
db: DB,
actor: Actor,
libraryId: string,
groupId: string,
input: { sourceId: string; config: JsonObject; title: string },
) {
await authorize(db, actor, libraryId, 4);
const source = await db
.selectFrom("knowledge_subscriptions")
.selectAll()
.where("library_id", "=", libraryId)
.where("source_id", "=", groupId)
.where("source_kind", "=", "content")
.executeTakeFirst();
if (!source || source.status === "detached")
fail(404, "Content subscription unavailable");
if (source.creator_id !== actor.id)
fail(403, "Only the subscribing user can change the content scope");
const { group, value } = await contentSubscriptionConfig(db, source);
if (input.sourceId !== value.sourceId)
fail(400, "Create a separate subscription for a different source");
await readContentInventory(createContentService(db), contentRequest(actor), {
sourceId: input.sourceId,
purpose: "knowledge",
config: input.config,
});
const scopeChanged =
JSON.stringify(value.config) !== JSON.stringify(input.config);
await transact(db, async (tx) => {
await authorize(tx, actor, libraryId, 4);
const saved = await tx
.updateTable("knowledge_source_groups")
.set({
title: input.title,
config: JSON.stringify({
...value,
config: input.config,
}),
})
.where("id", "=", group.id)
.where("config", "=", group.config!)
.executeTakeFirst();
if (!Number(saved.numUpdatedRows))
fail(409, "Content subscription changed");
await tx
.updateTable("knowledge_subscriptions")
.set({
name: input.title,
source_version: scopeChanged ? "" : source.source_version,
})
.where("id", "=", source.id)
.execute();
});
return { id: source.id, groupId };
}