@agent-native/core 0.102.2 → 0.103.1
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.
- package/corpus/README.md +1 -1
- package/corpus/core/CHANGELOG.md +12 -0
- package/corpus/core/docs/content/local-file-mode.mdx +99 -489
- package/corpus/core/docs/content/locales/ar-SA/local-file-mode.mdx +35 -490
- package/corpus/core/docs/content/locales/ar-SA/template-content.mdx +10 -24
- package/corpus/core/docs/content/locales/de-DE/local-file-mode.mdx +37 -491
- package/corpus/core/docs/content/locales/de-DE/template-content.mdx +10 -24
- package/corpus/core/docs/content/locales/es-ES/local-file-mode.mdx +35 -493
- package/corpus/core/docs/content/locales/es-ES/template-content.mdx +10 -24
- package/corpus/core/docs/content/locales/fr-FR/local-file-mode.mdx +38 -491
- package/corpus/core/docs/content/locales/fr-FR/template-content.mdx +10 -24
- package/corpus/core/docs/content/locales/hi-IN/local-file-mode.mdx +33 -492
- package/corpus/core/docs/content/locales/hi-IN/template-content.mdx +10 -24
- package/corpus/core/docs/content/locales/ja-JP/local-file-mode.mdx +36 -492
- package/corpus/core/docs/content/locales/ja-JP/template-content.mdx +10 -24
- package/corpus/core/docs/content/locales/ko-KR/local-file-mode.mdx +35 -490
- package/corpus/core/docs/content/locales/ko-KR/template-content.mdx +10 -24
- package/corpus/core/docs/content/locales/pt-BR/local-file-mode.mdx +35 -493
- package/corpus/core/docs/content/locales/pt-BR/template-content.mdx +10 -24
- package/corpus/core/docs/content/locales/zh-CN/local-file-mode.mdx +31 -489
- package/corpus/core/docs/content/locales/zh-CN/template-content.mdx +9 -24
- package/corpus/core/docs/content/locales/zh-TW/local-file-mode.mdx +31 -489
- package/corpus/core/docs/content/locales/zh-TW/template-content.mdx +9 -24
- package/corpus/core/docs/content/template-content.mdx +26 -42
- package/corpus/core/package.json +1 -1
- package/corpus/core/src/cli/content-local.ts +86 -18
- package/corpus/core/src/cli/index.ts +2 -2
- package/corpus/core/src/cli/skills-content/content-skill.ts +36 -25
- package/corpus/core/src/cli/skills.ts +47 -13
- package/corpus/core/src/local-artifacts/index.ts +65 -0
- package/corpus/core/src/vite/client.ts +2 -0
- package/corpus/templates/assets/app/components/library/GenerationPresetsPanel.tsx +325 -0
- package/corpus/templates/assets/app/hooks/use-navigation-state.ts +12 -0
- package/corpus/templates/assets/app/i18n/zh-TW.ts +21 -0
- package/corpus/templates/assets/app/i18n-data.ts +215 -0
- package/corpus/templates/assets/app/routes/brand-kits.$id.tsx +19 -479
- package/corpus/templates/assets/app/routes/brand-kits.$id_.presets.$presetId.tsx +1 -1
- package/corpus/templates/assets/app/routes/brand-kits.$id_.settings.tsx +446 -0
- package/corpus/templates/clips/app/components/editor/editor-layout.tsx +12 -2
- package/corpus/templates/clips/app/components/player/settings-panel.tsx +30 -12
- package/corpus/templates/clips/app/components/player/video-player.tsx +5 -1
- package/corpus/templates/clips/app/components/recorder/microphone-visualizer.tsx +22 -3
- package/corpus/templates/clips/app/i18n/ar-SA.ts +3 -2
- package/corpus/templates/clips/app/i18n/de-DE.ts +4 -2
- package/corpus/templates/clips/app/i18n/en-US.ts +3 -2
- package/corpus/templates/clips/app/i18n/es-ES.ts +4 -2
- package/corpus/templates/clips/app/i18n/fr-FR.ts +3 -2
- package/corpus/templates/clips/app/i18n/hi-IN.ts +3 -2
- package/corpus/templates/clips/app/i18n/ja-JP.ts +4 -1
- package/corpus/templates/clips/app/i18n/ko-KR.ts +3 -2
- package/corpus/templates/clips/app/i18n/pt-BR.ts +4 -2
- package/corpus/templates/clips/app/i18n/zh-CN.ts +3 -2
- package/corpus/templates/clips/app/i18n/zh-TW.ts +3 -2
- package/corpus/templates/clips/app/routes/r.$recordingId.tsx +27 -1
- package/corpus/templates/clips/server/lib/share-password.ts +6 -3
- package/corpus/templates/content/.agents/skills/content/SKILL.md +26 -39
- package/corpus/templates/content/AGENTS.md +25 -30
- package/corpus/templates/content/actions/_builder-docs-client.ts +23 -0
- package/corpus/templates/content/actions/_content-database-source-adapters.ts +21 -3
- package/corpus/templates/content/actions/_content-files.ts +338 -0
- package/corpus/templates/content/actions/_content-space-access.ts +135 -0
- package/corpus/templates/content/actions/_content-spaces.ts +640 -0
- package/corpus/templates/content/actions/_database-source-utils.ts +90 -7
- package/corpus/templates/content/actions/_database-utils.ts +7 -1
- package/corpus/templates/content/actions/_local-file-documents.ts +0 -9
- package/corpus/templates/content/actions/_local-folder-source.ts +53 -0
- package/corpus/templates/content/actions/_property-utils.ts +9 -4
- package/corpus/templates/content/actions/add-database-item.ts +108 -65
- package/corpus/templates/content/actions/backfill-content-files.ts +22 -0
- package/corpus/templates/content/actions/connect-local-folder-source.ts +221 -0
- package/corpus/templates/content/actions/create-content-database.ts +129 -3
- package/corpus/templates/content/actions/create-document.ts +65 -55
- package/corpus/templates/content/actions/create-inline-content-database.ts +6 -1
- package/corpus/templates/content/actions/delete-content-database.ts +3 -0
- package/corpus/templates/content/actions/delete-document.ts +7 -11
- package/corpus/templates/content/actions/disconnect-local-folder-source.ts +175 -0
- package/corpus/templates/content/actions/duplicate-database-item.ts +10 -0
- package/corpus/templates/content/actions/duplicate-database-items.ts +13 -0
- package/corpus/templates/content/actions/edit-document.ts +2 -28
- package/corpus/templates/content/actions/ensure-content-spaces.ts +26 -0
- package/corpus/templates/content/actions/export-content-source.ts +47 -9
- package/corpus/templates/content/actions/get-document.ts +0 -14
- package/corpus/templates/content/actions/import-content-source.ts +43 -3
- package/corpus/templates/content/actions/list-content-spaces.ts +103 -0
- package/corpus/templates/content/actions/list-documents.ts +12 -9
- package/corpus/templates/content/actions/move-document.ts +3 -14
- package/corpus/templates/content/actions/pull-document.ts +0 -23
- package/corpus/templates/content/actions/remove-local-file-source.ts +50 -29
- package/corpus/templates/content/actions/resolve-local-folder-conflict.ts +442 -0
- package/corpus/templates/content/actions/reveal-local-source-file.ts +6 -17
- package/corpus/templates/content/actions/search-documents.ts +0 -34
- package/corpus/templates/content/actions/share-local-file-document.ts +25 -1
- package/corpus/templates/content/actions/submit-content-database-form.ts +12 -0
- package/corpus/templates/content/actions/sync-local-folder-source.ts +731 -0
- package/corpus/templates/content/actions/sync-manifest-local-folder-source.ts +123 -0
- package/corpus/templates/content/actions/update-content-database-view.ts +10 -1
- package/corpus/templates/content/actions/update-document.ts +0 -15
- package/corpus/templates/content/actions/view-screen.ts +2 -50
- package/corpus/templates/content/app/components/editor/SlashCommandMenu.tsx +76 -62
- package/corpus/templates/content/app/components/editor/database/DatabaseView.tsx +70 -22
- package/corpus/templates/content/app/components/editor/database/grouping.ts +2 -1
- package/corpus/templates/content/app/components/editor/database/settings.tsx +23 -20
- package/corpus/templates/content/app/components/editor/database/shared.tsx +2 -0
- package/corpus/templates/content/app/components/editor/database/sidebar.tsx +330 -0
- package/corpus/templates/content/app/components/editor/database/types.ts +1 -0
- package/corpus/templates/content/app/components/editor/database/view-config.ts +3 -1
- package/corpus/templates/content/app/components/layout/Layout.tsx +25 -1
- package/corpus/templates/content/app/components/sidebar/DocumentSidebar.tsx +209 -113
- package/corpus/templates/content/app/components/sidebar/select-content-space.ts +31 -0
- package/corpus/templates/content/app/hooks/use-content-database.ts +16 -0
- package/corpus/templates/content/app/hooks/use-content-spaces.ts +39 -0
- package/corpus/templates/content/app/i18n/zh-TW.ts +11 -0
- package/corpus/templates/content/app/i18n-data.ts +75 -0
- package/corpus/templates/content/app/routes/_app.local-files.tsx +174 -65
- package/corpus/templates/content/changelog/2026-07-16-content-callouts-can-now-be-reviewed-and-published-to-builder.md +6 -0
- package/corpus/templates/content/changelog/2026-07-16-heading-5-and-heading-6-are-available-from-the-editor-slash-.md +6 -0
- package/corpus/templates/content/changelog/2026-07-16-organize-every-workspace-through-a-customizable-files-databa.md +6 -0
- package/corpus/templates/content/parity/matrix.md +2 -1
- package/corpus/templates/content/parity/matrix.ts +37 -1
- package/corpus/templates/content/scripts/dev-database.mjs +7 -5
- package/corpus/templates/content/server/db/schema.ts +113 -40
- package/corpus/templates/content/server/lib/document-context.ts +15 -1
- package/corpus/templates/content/server/lib/notion-sync.ts +6 -0
- package/corpus/templates/content/server/plugins/db.ts +153 -0
- package/corpus/templates/content/shared/api.ts +17 -3
- package/corpus/templates/content/shared/builder-mdx.ts +163 -16
- package/corpus/templates/design/.agents/skills/design-templates/SKILL.md +59 -0
- package/corpus/templates/design/AGENTS.md +6 -1
- package/corpus/templates/design/actions/create-design-from-template.ts +19 -7
- package/corpus/templates/design/actions/list-design-templates.ts +69 -21
- package/corpus/templates/design/actions/navigate.ts +2 -2
- package/corpus/templates/design/actions/view-screen.ts +15 -2
- package/corpus/templates/design/agent-native.app-skill.json +5 -0
- package/corpus/templates/design/app/components/editor/PromptDialog.tsx +281 -56
- package/corpus/templates/design/app/components/templates/TemplatePreview.tsx +14 -2
- package/corpus/templates/design/app/hooks/use-navigation-state.ts +3 -2
- package/corpus/templates/design/app/i18n/zh-TW.ts +3 -3
- package/corpus/templates/design/app/i18n-data.ts +21 -2
- package/corpus/templates/design/app/i18n-template-feature.ts +218 -10
- package/corpus/templates/design/app/pages/Index.tsx +195 -24
- package/corpus/templates/design/app/pages/Templates.tsx +144 -55
- package/corpus/templates/design/app/pages/design-editor/generation-prompt-directives.ts +12 -1
- package/corpus/templates/design/changelog/2026-07-14-templates-now-appear-in-the-new-design-picker-with-previews-.md +6 -0
- package/corpus/templates/design/changelog/2026-07-15-templates-now-open-immediately-after-copying-even-while-the-.md +6 -0
- package/corpus/templates/design/server/plugins/agent-chat.ts +1 -1
- package/corpus/templates/mail/AGENTS.md +23 -18
- package/corpus/templates/mail/actions/bulk-archive.ts +32 -28
- package/corpus/templates/mail/actions/mark-read.ts +131 -20
- package/corpus/templates/mail/actions/mark-thread-read.ts +1 -1
- package/corpus/templates/mail/actions/move-email.ts +30 -24
- package/corpus/templates/mail/actions/send-email.ts +73 -68
- package/corpus/templates/mail/changelog/2026-07-15-unread-inbox-cleanup-now-completes-reliably.md +6 -0
- package/corpus/templates/mail/server/handlers/emails.ts +218 -209
- package/corpus/templates/mail/server/lib/bulk-mark-read.ts +30 -0
- package/corpus/templates/mail/server/lib/email-state.ts +198 -113
- package/corpus/templates/mail/server/lib/google-auth.ts +218 -25
- package/corpus/templates/mail/server/lib/jobs.ts +69 -74
- package/corpus/templates/mail/server/lib/local-email-store.ts +265 -0
- package/corpus/templates/mail/server/plugins/agent-chat.ts +6 -0
- package/dist/cli/content-local.d.ts +1 -0
- package/dist/cli/content-local.d.ts.map +1 -1
- package/dist/cli/content-local.js +75 -17
- package/dist/cli/content-local.js.map +1 -1
- package/dist/cli/index.js +2 -2
- package/dist/cli/index.js.map +1 -1
- package/dist/cli/skills-content/content-skill.d.ts +1 -1
- package/dist/cli/skills-content/content-skill.d.ts.map +1 -1
- package/dist/cli/skills-content/content-skill.js +36 -25
- package/dist/cli/skills-content/content-skill.js.map +1 -1
- package/dist/cli/skills.d.ts.map +1 -1
- package/dist/cli/skills.js +47 -14
- package/dist/cli/skills.js.map +1 -1
- package/dist/collab/struct-routes.d.ts +1 -1
- package/dist/local-artifacts/index.d.ts +10 -0
- package/dist/local-artifacts/index.d.ts.map +1 -1
- package/dist/local-artifacts/index.js +39 -0
- package/dist/local-artifacts/index.js.map +1 -1
- package/dist/server/agent-engine-api-key-route.d.ts +1 -1
- package/dist/vite/client.d.ts.map +1 -1
- package/dist/vite/client.js +2 -0
- package/dist/vite/client.js.map +1 -1
- package/docs/content/local-file-mode.mdx +99 -489
- package/docs/content/locales/ar-SA/local-file-mode.mdx +35 -490
- package/docs/content/locales/ar-SA/template-content.mdx +10 -24
- package/docs/content/locales/de-DE/local-file-mode.mdx +37 -491
- package/docs/content/locales/de-DE/template-content.mdx +10 -24
- package/docs/content/locales/es-ES/local-file-mode.mdx +35 -493
- package/docs/content/locales/es-ES/template-content.mdx +10 -24
- package/docs/content/locales/fr-FR/local-file-mode.mdx +38 -491
- package/docs/content/locales/fr-FR/template-content.mdx +10 -24
- package/docs/content/locales/hi-IN/local-file-mode.mdx +33 -492
- package/docs/content/locales/hi-IN/template-content.mdx +10 -24
- package/docs/content/locales/ja-JP/local-file-mode.mdx +36 -492
- package/docs/content/locales/ja-JP/template-content.mdx +10 -24
- package/docs/content/locales/ko-KR/local-file-mode.mdx +35 -490
- package/docs/content/locales/ko-KR/template-content.mdx +10 -24
- package/docs/content/locales/pt-BR/local-file-mode.mdx +35 -493
- package/docs/content/locales/pt-BR/template-content.mdx +10 -24
- package/docs/content/locales/zh-CN/local-file-mode.mdx +31 -489
- package/docs/content/locales/zh-CN/template-content.mdx +9 -24
- package/docs/content/locales/zh-TW/local-file-mode.mdx +31 -489
- package/docs/content/locales/zh-TW/template-content.mdx +9 -24
- package/docs/content/template-content.mdx +26 -42
- package/package.json +1 -1
- package/corpus/templates/assets/app/components/library/EditLibraryDialog.tsx +0 -126
|
@@ -0,0 +1,640 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
|
|
3
|
+
import { and, eq, sql } from "drizzle-orm";
|
|
4
|
+
|
|
5
|
+
import { schema } from "../server/db/index.js";
|
|
6
|
+
import {
|
|
7
|
+
listContentOrganizationMemberships,
|
|
8
|
+
normalizeContentSpaceEmail,
|
|
9
|
+
} from "./_content-space-access.js";
|
|
10
|
+
import {
|
|
11
|
+
defaultDatabaseViewConfig,
|
|
12
|
+
seedDefaultBlocksField,
|
|
13
|
+
serializeDatabaseViewConfig,
|
|
14
|
+
} from "./_property-utils.js";
|
|
15
|
+
|
|
16
|
+
type Db = any;
|
|
17
|
+
|
|
18
|
+
export type ProvisionedContentSpaces = {
|
|
19
|
+
personalSpaceId: string;
|
|
20
|
+
personalFilesDatabaseId: string;
|
|
21
|
+
catalogDatabaseId: string;
|
|
22
|
+
spaceIds: string[];
|
|
23
|
+
created: {
|
|
24
|
+
spaces: number;
|
|
25
|
+
databases: number;
|
|
26
|
+
documents: number;
|
|
27
|
+
catalogItems: number;
|
|
28
|
+
};
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
function opaqueId(kind: string, value: string): string {
|
|
32
|
+
return `${kind}_${createHash("sha256").update(value).digest("hex").slice(0, 32)}`;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function personalContentSpaceId(email: string) {
|
|
36
|
+
return opaqueId("content_space_personal", normalizeContentSpaceEmail(email));
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export function organizationContentSpaceId(orgId: string) {
|
|
40
|
+
return opaqueId("content_space_org", orgId.trim());
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export function sourceBackedContentSpaceId(
|
|
44
|
+
email: string,
|
|
45
|
+
connectionId: string,
|
|
46
|
+
) {
|
|
47
|
+
return opaqueId(
|
|
48
|
+
"content_space_source",
|
|
49
|
+
`${normalizeContentSpaceEmail(email)}:${connectionId.trim()}`,
|
|
50
|
+
);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export function systemIdsForContentSpace(
|
|
54
|
+
scope: string,
|
|
55
|
+
role: "files" | "workspaces",
|
|
56
|
+
) {
|
|
57
|
+
return {
|
|
58
|
+
databaseId: opaqueId(`content_database_${role}`, scope),
|
|
59
|
+
documentId: opaqueId(`content_document_${role}`, scope),
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
async function ensureDocument(
|
|
64
|
+
db: Db,
|
|
65
|
+
values: Omit<typeof schema.documents.$inferInsert, "ownerEmail"> & {
|
|
66
|
+
ownerEmail: string;
|
|
67
|
+
},
|
|
68
|
+
created: ProvisionedContentSpaces["created"],
|
|
69
|
+
) {
|
|
70
|
+
const [existing] = await db
|
|
71
|
+
.select({ id: schema.documents.id })
|
|
72
|
+
.from(schema.documents)
|
|
73
|
+
.where(
|
|
74
|
+
and(
|
|
75
|
+
eq(schema.documents.id, values.id),
|
|
76
|
+
eq(schema.documents.ownerEmail, values.ownerEmail),
|
|
77
|
+
),
|
|
78
|
+
);
|
|
79
|
+
if (existing) return;
|
|
80
|
+
await db
|
|
81
|
+
.insert(schema.documents)
|
|
82
|
+
.values({ ...values, ownerEmail: values.ownerEmail })
|
|
83
|
+
.onConflictDoNothing();
|
|
84
|
+
created.documents += 1;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
async function ensureSystemDatabase(args: {
|
|
88
|
+
db: Db;
|
|
89
|
+
spaceId: string;
|
|
90
|
+
ownerEmail: string;
|
|
91
|
+
orgId: string | null;
|
|
92
|
+
title: string;
|
|
93
|
+
role: "files" | "workspaces";
|
|
94
|
+
visibility: "private" | "org";
|
|
95
|
+
now: string;
|
|
96
|
+
created: ProvisionedContentSpaces["created"];
|
|
97
|
+
}) {
|
|
98
|
+
const ids = systemIdsForContentSpace(args.spaceId, args.role);
|
|
99
|
+
await ensureDocument(
|
|
100
|
+
args.db,
|
|
101
|
+
{
|
|
102
|
+
id: ids.documentId,
|
|
103
|
+
spaceId: args.spaceId,
|
|
104
|
+
ownerEmail: args.ownerEmail,
|
|
105
|
+
orgId: args.orgId,
|
|
106
|
+
parentId: null,
|
|
107
|
+
title: args.title,
|
|
108
|
+
content: "",
|
|
109
|
+
description: "",
|
|
110
|
+
position: 0,
|
|
111
|
+
isFavorite: 0,
|
|
112
|
+
hideFromSearch: 1,
|
|
113
|
+
visibility: args.visibility,
|
|
114
|
+
createdAt: args.now,
|
|
115
|
+
updatedAt: args.now,
|
|
116
|
+
},
|
|
117
|
+
args.created,
|
|
118
|
+
);
|
|
119
|
+
const [existing] = await args.db
|
|
120
|
+
.select({ id: schema.contentDatabases.id })
|
|
121
|
+
.from(schema.contentDatabases)
|
|
122
|
+
.where(
|
|
123
|
+
and(
|
|
124
|
+
eq(schema.contentDatabases.spaceId, args.spaceId),
|
|
125
|
+
eq(schema.contentDatabases.systemRole, args.role),
|
|
126
|
+
),
|
|
127
|
+
);
|
|
128
|
+
if (!existing) {
|
|
129
|
+
await args.db
|
|
130
|
+
.insert(schema.contentDatabases)
|
|
131
|
+
.values({
|
|
132
|
+
id: ids.databaseId,
|
|
133
|
+
spaceId: args.spaceId,
|
|
134
|
+
ownerEmail: args.ownerEmail,
|
|
135
|
+
orgId: args.orgId,
|
|
136
|
+
documentId: ids.documentId,
|
|
137
|
+
title: args.title,
|
|
138
|
+
systemRole: args.role,
|
|
139
|
+
viewConfigJson: serializeDatabaseViewConfig(
|
|
140
|
+
defaultDatabaseViewConfig("sidebar"),
|
|
141
|
+
),
|
|
142
|
+
createdAt: args.now,
|
|
143
|
+
updatedAt: args.now,
|
|
144
|
+
})
|
|
145
|
+
.onConflictDoNothing();
|
|
146
|
+
args.created.databases += 1;
|
|
147
|
+
}
|
|
148
|
+
const [database] = await args.db
|
|
149
|
+
.select()
|
|
150
|
+
.from(schema.contentDatabases)
|
|
151
|
+
.where(
|
|
152
|
+
and(
|
|
153
|
+
eq(schema.contentDatabases.spaceId, args.spaceId),
|
|
154
|
+
eq(schema.contentDatabases.systemRole, args.role),
|
|
155
|
+
),
|
|
156
|
+
);
|
|
157
|
+
if (!database)
|
|
158
|
+
throw new Error(
|
|
159
|
+
`Unable to provision ${args.role} database for Content space`,
|
|
160
|
+
);
|
|
161
|
+
return database;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
async function ensureDatabaseItem(args: {
|
|
165
|
+
db: Db;
|
|
166
|
+
databaseId: string;
|
|
167
|
+
documentId: string;
|
|
168
|
+
ownerEmail: string;
|
|
169
|
+
orgId: string | null;
|
|
170
|
+
position: number;
|
|
171
|
+
now: string;
|
|
172
|
+
}) {
|
|
173
|
+
const [existing] = await args.db
|
|
174
|
+
.select({ id: schema.contentDatabaseItems.id })
|
|
175
|
+
.from(schema.contentDatabaseItems)
|
|
176
|
+
.where(
|
|
177
|
+
and(
|
|
178
|
+
eq(schema.contentDatabaseItems.databaseId, args.databaseId),
|
|
179
|
+
eq(schema.contentDatabaseItems.documentId, args.documentId),
|
|
180
|
+
),
|
|
181
|
+
);
|
|
182
|
+
if (existing) return existing.id;
|
|
183
|
+
const id = opaqueId(
|
|
184
|
+
"content_database_item",
|
|
185
|
+
`${args.databaseId}:${args.documentId}`,
|
|
186
|
+
);
|
|
187
|
+
await args.db
|
|
188
|
+
.insert(schema.contentDatabaseItems)
|
|
189
|
+
.values({
|
|
190
|
+
id,
|
|
191
|
+
ownerEmail: args.ownerEmail,
|
|
192
|
+
orgId: args.orgId,
|
|
193
|
+
databaseId: args.databaseId,
|
|
194
|
+
documentId: args.documentId,
|
|
195
|
+
position: args.position,
|
|
196
|
+
createdAt: args.now,
|
|
197
|
+
updatedAt: args.now,
|
|
198
|
+
})
|
|
199
|
+
.onConflictDoNothing();
|
|
200
|
+
return id;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
export async function provisionContentSpaces(
|
|
204
|
+
db: Db,
|
|
205
|
+
userEmail: string,
|
|
206
|
+
): Promise<ProvisionedContentSpaces> {
|
|
207
|
+
const email = normalizeContentSpaceEmail(userEmail);
|
|
208
|
+
const memberships = await listContentOrganizationMemberships(email);
|
|
209
|
+
const now = new Date().toISOString();
|
|
210
|
+
const personalSpaceId = personalContentSpaceId(email);
|
|
211
|
+
const result: ProvisionedContentSpaces = {
|
|
212
|
+
personalSpaceId,
|
|
213
|
+
personalFilesDatabaseId: systemIdsForContentSpace(personalSpaceId, "files")
|
|
214
|
+
.databaseId,
|
|
215
|
+
catalogDatabaseId: systemIdsForContentSpace(personalSpaceId, "workspaces")
|
|
216
|
+
.databaseId,
|
|
217
|
+
spaceIds: [],
|
|
218
|
+
created: { spaces: 0, databases: 0, documents: 0, catalogItems: 0 },
|
|
219
|
+
};
|
|
220
|
+
|
|
221
|
+
await db.transaction(async (tx: Db) => {
|
|
222
|
+
const personalFiles = await ensureSystemDatabase({
|
|
223
|
+
db: tx,
|
|
224
|
+
spaceId: personalSpaceId,
|
|
225
|
+
ownerEmail: email,
|
|
226
|
+
orgId: null,
|
|
227
|
+
title: "Files",
|
|
228
|
+
role: "files",
|
|
229
|
+
visibility: "private",
|
|
230
|
+
now,
|
|
231
|
+
created: result.created,
|
|
232
|
+
});
|
|
233
|
+
const [personalSpace] = await tx
|
|
234
|
+
.select()
|
|
235
|
+
.from(schema.contentSpaces)
|
|
236
|
+
.where(eq(schema.contentSpaces.id, personalSpaceId));
|
|
237
|
+
if (!personalSpace) {
|
|
238
|
+
await tx
|
|
239
|
+
.insert(schema.contentSpaces)
|
|
240
|
+
.values({
|
|
241
|
+
id: personalSpaceId,
|
|
242
|
+
name: "Personal",
|
|
243
|
+
kind: "personal",
|
|
244
|
+
ownerEmail: email,
|
|
245
|
+
orgId: null,
|
|
246
|
+
filesDatabaseId: personalFiles.id,
|
|
247
|
+
createdBy: email,
|
|
248
|
+
createdAt: now,
|
|
249
|
+
updatedAt: now,
|
|
250
|
+
})
|
|
251
|
+
.onConflictDoNothing();
|
|
252
|
+
result.created.spaces += 1;
|
|
253
|
+
} else if (personalSpace.filesDatabaseId !== personalFiles.id) {
|
|
254
|
+
await tx
|
|
255
|
+
.update(schema.contentSpaces)
|
|
256
|
+
.set({ filesDatabaseId: personalFiles.id, updatedAt: now })
|
|
257
|
+
.where(eq(schema.contentSpaces.id, personalSpaceId));
|
|
258
|
+
}
|
|
259
|
+
const catalog = await ensureSystemDatabase({
|
|
260
|
+
db: tx,
|
|
261
|
+
spaceId: personalSpaceId,
|
|
262
|
+
ownerEmail: email,
|
|
263
|
+
orgId: null,
|
|
264
|
+
title: "Workspaces",
|
|
265
|
+
role: "workspaces",
|
|
266
|
+
visibility: "private",
|
|
267
|
+
now,
|
|
268
|
+
created: result.created,
|
|
269
|
+
});
|
|
270
|
+
await ensureDatabaseItem({
|
|
271
|
+
db: tx,
|
|
272
|
+
databaseId: personalFiles.id,
|
|
273
|
+
documentId: catalog.documentId,
|
|
274
|
+
ownerEmail: email,
|
|
275
|
+
orgId: null,
|
|
276
|
+
position: 0,
|
|
277
|
+
now,
|
|
278
|
+
});
|
|
279
|
+
|
|
280
|
+
const spaces = [
|
|
281
|
+
{
|
|
282
|
+
id: personalSpaceId,
|
|
283
|
+
name: "Personal",
|
|
284
|
+
ownerEmail: email,
|
|
285
|
+
orgId: null as string | null,
|
|
286
|
+
createdBy: email,
|
|
287
|
+
filesDatabaseId: personalFiles.id,
|
|
288
|
+
},
|
|
289
|
+
...memberships.map((membership) => ({
|
|
290
|
+
id: organizationContentSpaceId(membership.orgId),
|
|
291
|
+
name: membership.name,
|
|
292
|
+
ownerEmail: membership.createdBy,
|
|
293
|
+
orgId: membership.orgId,
|
|
294
|
+
createdBy: membership.createdBy,
|
|
295
|
+
filesDatabaseId: systemIdsForContentSpace(
|
|
296
|
+
organizationContentSpaceId(membership.orgId),
|
|
297
|
+
"files",
|
|
298
|
+
).databaseId,
|
|
299
|
+
})),
|
|
300
|
+
];
|
|
301
|
+
const provisionableOrganizationSpaceIds = new Set(
|
|
302
|
+
memberships
|
|
303
|
+
.filter(
|
|
304
|
+
(membership) =>
|
|
305
|
+
membership.role === "owner" || membership.role === "admin",
|
|
306
|
+
)
|
|
307
|
+
.map((membership) => organizationContentSpaceId(membership.orgId)),
|
|
308
|
+
);
|
|
309
|
+
for (const space of spaces.slice(1)) {
|
|
310
|
+
if (!provisionableOrganizationSpaceIds.has(space.id)) continue;
|
|
311
|
+
const files = await ensureSystemDatabase({
|
|
312
|
+
db: tx,
|
|
313
|
+
spaceId: space.id,
|
|
314
|
+
ownerEmail: space.ownerEmail,
|
|
315
|
+
orgId: space.orgId,
|
|
316
|
+
title: "Files",
|
|
317
|
+
role: "files",
|
|
318
|
+
visibility: "org",
|
|
319
|
+
now,
|
|
320
|
+
created: result.created,
|
|
321
|
+
});
|
|
322
|
+
const [existingSpace] = await tx
|
|
323
|
+
.select()
|
|
324
|
+
.from(schema.contentSpaces)
|
|
325
|
+
.where(eq(schema.contentSpaces.id, space.id));
|
|
326
|
+
if (!existingSpace) {
|
|
327
|
+
await tx
|
|
328
|
+
.insert(schema.contentSpaces)
|
|
329
|
+
.values({
|
|
330
|
+
...space,
|
|
331
|
+
filesDatabaseId: files.id,
|
|
332
|
+
kind: "organization",
|
|
333
|
+
createdAt: now,
|
|
334
|
+
updatedAt: now,
|
|
335
|
+
})
|
|
336
|
+
.onConflictDoNothing();
|
|
337
|
+
result.created.spaces += 1;
|
|
338
|
+
} else if (
|
|
339
|
+
existingSpace.name !== space.name ||
|
|
340
|
+
existingSpace.filesDatabaseId !== files.id
|
|
341
|
+
) {
|
|
342
|
+
await tx
|
|
343
|
+
.update(schema.contentSpaces)
|
|
344
|
+
.set({
|
|
345
|
+
name: space.name,
|
|
346
|
+
filesDatabaseId: files.id,
|
|
347
|
+
updatedAt: now,
|
|
348
|
+
})
|
|
349
|
+
.where(eq(schema.contentSpaces.id, space.id));
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
const accessibleIds = new Set(spaces.map((space) => space.id));
|
|
353
|
+
for (const [index, space] of spaces.entries()) {
|
|
354
|
+
if (!accessibleIds.has(space.id)) continue;
|
|
355
|
+
const referenceDocumentId = opaqueId(
|
|
356
|
+
"content_workspace_reference",
|
|
357
|
+
`${email}:${space.id}`,
|
|
358
|
+
);
|
|
359
|
+
await ensureDocument(
|
|
360
|
+
tx,
|
|
361
|
+
{
|
|
362
|
+
id: referenceDocumentId,
|
|
363
|
+
spaceId: personalSpaceId,
|
|
364
|
+
ownerEmail: email,
|
|
365
|
+
orgId: null,
|
|
366
|
+
parentId: catalog.documentId,
|
|
367
|
+
title: space.name,
|
|
368
|
+
content: "",
|
|
369
|
+
description: "",
|
|
370
|
+
position: index,
|
|
371
|
+
isFavorite: 0,
|
|
372
|
+
hideFromSearch: 0,
|
|
373
|
+
visibility: "private",
|
|
374
|
+
createdAt: now,
|
|
375
|
+
updatedAt: now,
|
|
376
|
+
},
|
|
377
|
+
result.created,
|
|
378
|
+
);
|
|
379
|
+
await tx
|
|
380
|
+
.update(schema.documents)
|
|
381
|
+
.set({ title: space.name, updatedAt: now })
|
|
382
|
+
.where(
|
|
383
|
+
and(
|
|
384
|
+
eq(schema.documents.id, referenceDocumentId),
|
|
385
|
+
eq(schema.documents.ownerEmail, email),
|
|
386
|
+
sql`${schema.documents.title} <> ${space.name}`,
|
|
387
|
+
),
|
|
388
|
+
);
|
|
389
|
+
const catalogItemId = await ensureDatabaseItem({
|
|
390
|
+
db: tx,
|
|
391
|
+
databaseId: catalog.id,
|
|
392
|
+
documentId: referenceDocumentId,
|
|
393
|
+
ownerEmail: email,
|
|
394
|
+
orgId: null,
|
|
395
|
+
position: index,
|
|
396
|
+
now,
|
|
397
|
+
});
|
|
398
|
+
await ensureDatabaseItem({
|
|
399
|
+
db: tx,
|
|
400
|
+
databaseId: personalFiles.id,
|
|
401
|
+
documentId: referenceDocumentId,
|
|
402
|
+
ownerEmail: email,
|
|
403
|
+
orgId: null,
|
|
404
|
+
position: index + 1,
|
|
405
|
+
now,
|
|
406
|
+
});
|
|
407
|
+
const [existingCatalogItem] = await tx
|
|
408
|
+
.select({ id: schema.contentSpaceCatalogItems.id })
|
|
409
|
+
.from(schema.contentSpaceCatalogItems)
|
|
410
|
+
.where(
|
|
411
|
+
and(
|
|
412
|
+
eq(schema.contentSpaceCatalogItems.catalogDatabaseId, catalog.id),
|
|
413
|
+
eq(schema.contentSpaceCatalogItems.spaceId, space.id),
|
|
414
|
+
),
|
|
415
|
+
);
|
|
416
|
+
if (!existingCatalogItem) {
|
|
417
|
+
await tx
|
|
418
|
+
.insert(schema.contentSpaceCatalogItems)
|
|
419
|
+
.values({
|
|
420
|
+
id: opaqueId("content_space_catalog", `${email}:${space.id}`),
|
|
421
|
+
ownerEmail: email,
|
|
422
|
+
catalogDatabaseId: catalog.id,
|
|
423
|
+
databaseItemId: catalogItemId,
|
|
424
|
+
documentId: referenceDocumentId,
|
|
425
|
+
spaceId: space.id,
|
|
426
|
+
createdAt: now,
|
|
427
|
+
updatedAt: now,
|
|
428
|
+
})
|
|
429
|
+
.onConflictDoNothing();
|
|
430
|
+
result.created.catalogItems += 1;
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
});
|
|
434
|
+
|
|
435
|
+
// The established seeder uses its own lock/atomic claim, so call it after
|
|
436
|
+
// the provisioning transaction rather than nesting transaction machinery.
|
|
437
|
+
const records = await db
|
|
438
|
+
.select({
|
|
439
|
+
id: schema.contentDatabases.id,
|
|
440
|
+
ownerEmail: schema.contentDatabases.ownerEmail,
|
|
441
|
+
orgId: schema.contentDatabases.orgId,
|
|
442
|
+
})
|
|
443
|
+
.from(schema.contentDatabases)
|
|
444
|
+
.where(and(eq(schema.contentDatabases.spaceId, personalSpaceId)));
|
|
445
|
+
for (const database of records)
|
|
446
|
+
await seedDefaultBlocksField({
|
|
447
|
+
databaseId: database.id,
|
|
448
|
+
ownerEmail: database.ownerEmail,
|
|
449
|
+
orgId: database.orgId,
|
|
450
|
+
now,
|
|
451
|
+
db,
|
|
452
|
+
});
|
|
453
|
+
for (const membership of memberships) {
|
|
454
|
+
if (membership.role !== "owner" && membership.role !== "admin") continue;
|
|
455
|
+
const spaceId = organizationContentSpaceId(membership.orgId);
|
|
456
|
+
const [database] = await db
|
|
457
|
+
.select({
|
|
458
|
+
id: schema.contentDatabases.id,
|
|
459
|
+
ownerEmail: schema.contentDatabases.ownerEmail,
|
|
460
|
+
orgId: schema.contentDatabases.orgId,
|
|
461
|
+
})
|
|
462
|
+
.from(schema.contentDatabases)
|
|
463
|
+
.where(
|
|
464
|
+
and(
|
|
465
|
+
eq(schema.contentDatabases.spaceId, spaceId),
|
|
466
|
+
eq(schema.contentDatabases.systemRole, "files"),
|
|
467
|
+
),
|
|
468
|
+
);
|
|
469
|
+
if (database)
|
|
470
|
+
await seedDefaultBlocksField({
|
|
471
|
+
databaseId: database.id,
|
|
472
|
+
ownerEmail: database.ownerEmail,
|
|
473
|
+
orgId: database.orgId,
|
|
474
|
+
now,
|
|
475
|
+
db,
|
|
476
|
+
});
|
|
477
|
+
}
|
|
478
|
+
result.spaceIds = [
|
|
479
|
+
personalSpaceId,
|
|
480
|
+
...memberships.map((membership) =>
|
|
481
|
+
organizationContentSpaceId(membership.orgId),
|
|
482
|
+
),
|
|
483
|
+
];
|
|
484
|
+
return result;
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
export async function provisionSourceBackedContentSpace(
|
|
488
|
+
db: Db,
|
|
489
|
+
userEmail: string,
|
|
490
|
+
input: { connectionId: string; name: string },
|
|
491
|
+
) {
|
|
492
|
+
const email = normalizeContentSpaceEmail(userEmail);
|
|
493
|
+
const connectionId = input.connectionId.trim();
|
|
494
|
+
const name = input.name.trim() || "Local folder";
|
|
495
|
+
if (!connectionId) throw new Error("Local folder connection ID is required");
|
|
496
|
+
await provisionContentSpaces(db, email);
|
|
497
|
+
|
|
498
|
+
const now = new Date().toISOString();
|
|
499
|
+
const spaceId = sourceBackedContentSpaceId(email, connectionId);
|
|
500
|
+
const personalSpaceId = personalContentSpaceId(email);
|
|
501
|
+
const personalFilesIds = systemIdsForContentSpace(personalSpaceId, "files");
|
|
502
|
+
const catalogIds = systemIdsForContentSpace(personalSpaceId, "workspaces");
|
|
503
|
+
const created: ProvisionedContentSpaces["created"] = {
|
|
504
|
+
spaces: 0,
|
|
505
|
+
databases: 0,
|
|
506
|
+
documents: 0,
|
|
507
|
+
catalogItems: 0,
|
|
508
|
+
};
|
|
509
|
+
|
|
510
|
+
const files = await db.transaction(async (tx: Db) => {
|
|
511
|
+
const sourceFiles = await ensureSystemDatabase({
|
|
512
|
+
db: tx,
|
|
513
|
+
spaceId,
|
|
514
|
+
ownerEmail: email,
|
|
515
|
+
orgId: null,
|
|
516
|
+
title: "Files",
|
|
517
|
+
role: "files",
|
|
518
|
+
visibility: "private",
|
|
519
|
+
now,
|
|
520
|
+
created,
|
|
521
|
+
});
|
|
522
|
+
const [existingSpace] = await tx
|
|
523
|
+
.select()
|
|
524
|
+
.from(schema.contentSpaces)
|
|
525
|
+
.where(eq(schema.contentSpaces.id, spaceId));
|
|
526
|
+
if (!existingSpace) {
|
|
527
|
+
await tx
|
|
528
|
+
.insert(schema.contentSpaces)
|
|
529
|
+
.values({
|
|
530
|
+
id: spaceId,
|
|
531
|
+
name,
|
|
532
|
+
kind: "source_backed",
|
|
533
|
+
ownerEmail: email,
|
|
534
|
+
orgId: null,
|
|
535
|
+
filesDatabaseId: sourceFiles.id,
|
|
536
|
+
createdBy: email,
|
|
537
|
+
createdAt: now,
|
|
538
|
+
updatedAt: now,
|
|
539
|
+
})
|
|
540
|
+
.onConflictDoNothing();
|
|
541
|
+
} else if (
|
|
542
|
+
existingSpace.name !== name ||
|
|
543
|
+
existingSpace.filesDatabaseId !== sourceFiles.id
|
|
544
|
+
) {
|
|
545
|
+
await tx
|
|
546
|
+
.update(schema.contentSpaces)
|
|
547
|
+
.set({ name, filesDatabaseId: sourceFiles.id, updatedAt: now })
|
|
548
|
+
.where(eq(schema.contentSpaces.id, spaceId));
|
|
549
|
+
}
|
|
550
|
+
|
|
551
|
+
const referenceDocumentId = opaqueId(
|
|
552
|
+
"content_workspace_reference",
|
|
553
|
+
`${email}:${spaceId}`,
|
|
554
|
+
);
|
|
555
|
+
await ensureDocument(
|
|
556
|
+
tx,
|
|
557
|
+
{
|
|
558
|
+
id: referenceDocumentId,
|
|
559
|
+
spaceId: personalSpaceId,
|
|
560
|
+
ownerEmail: email,
|
|
561
|
+
orgId: null,
|
|
562
|
+
parentId: catalogIds.documentId,
|
|
563
|
+
title: name,
|
|
564
|
+
content: "",
|
|
565
|
+
description: "",
|
|
566
|
+
position: 0,
|
|
567
|
+
isFavorite: 0,
|
|
568
|
+
hideFromSearch: 0,
|
|
569
|
+
visibility: "private",
|
|
570
|
+
createdAt: now,
|
|
571
|
+
updatedAt: now,
|
|
572
|
+
},
|
|
573
|
+
created,
|
|
574
|
+
);
|
|
575
|
+
await tx
|
|
576
|
+
.update(schema.documents)
|
|
577
|
+
.set({ title: name, updatedAt: now })
|
|
578
|
+
.where(eq(schema.documents.id, referenceDocumentId));
|
|
579
|
+
|
|
580
|
+
const [maxCatalogPosition] = await tx
|
|
581
|
+
.select({ max: sql<number>`COALESCE(MAX(position), -1)` })
|
|
582
|
+
.from(schema.contentDatabaseItems)
|
|
583
|
+
.where(eq(schema.contentDatabaseItems.databaseId, catalogIds.databaseId));
|
|
584
|
+
const catalogItemId = await ensureDatabaseItem({
|
|
585
|
+
db: tx,
|
|
586
|
+
databaseId: catalogIds.databaseId,
|
|
587
|
+
documentId: referenceDocumentId,
|
|
588
|
+
ownerEmail: email,
|
|
589
|
+
orgId: null,
|
|
590
|
+
position: (maxCatalogPosition?.max ?? -1) + 1,
|
|
591
|
+
now,
|
|
592
|
+
});
|
|
593
|
+
await ensureDatabaseItem({
|
|
594
|
+
db: tx,
|
|
595
|
+
databaseId: personalFilesIds.databaseId,
|
|
596
|
+
documentId: referenceDocumentId,
|
|
597
|
+
ownerEmail: email,
|
|
598
|
+
orgId: null,
|
|
599
|
+
position: (maxCatalogPosition?.max ?? -1) + 1,
|
|
600
|
+
now,
|
|
601
|
+
});
|
|
602
|
+
const [mapping] = await tx
|
|
603
|
+
.select({ id: schema.contentSpaceCatalogItems.id })
|
|
604
|
+
.from(schema.contentSpaceCatalogItems)
|
|
605
|
+
.where(
|
|
606
|
+
and(
|
|
607
|
+
eq(
|
|
608
|
+
schema.contentSpaceCatalogItems.catalogDatabaseId,
|
|
609
|
+
catalogIds.databaseId,
|
|
610
|
+
),
|
|
611
|
+
eq(schema.contentSpaceCatalogItems.spaceId, spaceId),
|
|
612
|
+
),
|
|
613
|
+
);
|
|
614
|
+
if (!mapping) {
|
|
615
|
+
await tx
|
|
616
|
+
.insert(schema.contentSpaceCatalogItems)
|
|
617
|
+
.values({
|
|
618
|
+
id: opaqueId("content_space_catalog", `${email}:${spaceId}`),
|
|
619
|
+
ownerEmail: email,
|
|
620
|
+
catalogDatabaseId: catalogIds.databaseId,
|
|
621
|
+
databaseItemId: catalogItemId,
|
|
622
|
+
documentId: referenceDocumentId,
|
|
623
|
+
spaceId,
|
|
624
|
+
createdAt: now,
|
|
625
|
+
updatedAt: now,
|
|
626
|
+
})
|
|
627
|
+
.onConflictDoNothing();
|
|
628
|
+
}
|
|
629
|
+
return sourceFiles;
|
|
630
|
+
});
|
|
631
|
+
|
|
632
|
+
await seedDefaultBlocksField({
|
|
633
|
+
databaseId: files.id,
|
|
634
|
+
ownerEmail: files.ownerEmail,
|
|
635
|
+
orgId: files.orgId,
|
|
636
|
+
now,
|
|
637
|
+
db,
|
|
638
|
+
});
|
|
639
|
+
return { spaceId, filesDatabaseId: files.id };
|
|
640
|
+
}
|