pilothub 0.0.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.
Files changed (272) hide show
  1. package/.env.local.example +19 -0
  2. package/.github/workflows/ci.yml +40 -0
  3. package/.oxlintrc.json +3 -0
  4. package/AGENTS.md +45 -0
  5. package/CHANGELOG.md +138 -0
  6. package/DEPRECATIONS.md +7 -0
  7. package/LICENSE +21 -0
  8. package/README.md +150 -0
  9. package/biome.json +41 -0
  10. package/convex/_generated/api.d.ts +153 -0
  11. package/convex/_generated/api.js +23 -0
  12. package/convex/_generated/dataModel.d.ts +60 -0
  13. package/convex/_generated/server.d.ts +143 -0
  14. package/convex/_generated/server.js +93 -0
  15. package/convex/auth.config.ts +8 -0
  16. package/convex/auth.ts +19 -0
  17. package/convex/comments.ts +88 -0
  18. package/convex/crons.ts +34 -0
  19. package/convex/devSeed.ts +459 -0
  20. package/convex/devSeedExtra.ts +541 -0
  21. package/convex/downloads.ts +78 -0
  22. package/convex/githubBackups.ts +170 -0
  23. package/convex/githubBackupsNode.ts +183 -0
  24. package/convex/githubImport.ts +317 -0
  25. package/convex/githubSoulBackups.ts +170 -0
  26. package/convex/githubSoulBackupsNode.ts +186 -0
  27. package/convex/http.ts +194 -0
  28. package/convex/httpApi.handlers.test.ts +488 -0
  29. package/convex/httpApi.test.ts +70 -0
  30. package/convex/httpApi.ts +305 -0
  31. package/convex/httpApiV1.handlers.test.ts +584 -0
  32. package/convex/httpApiV1.ts +1172 -0
  33. package/convex/leaderboards.ts +39 -0
  34. package/convex/lib/access.ts +36 -0
  35. package/convex/lib/apiTokenAuth.ts +36 -0
  36. package/convex/lib/badges.ts +50 -0
  37. package/convex/lib/changelog.test.ts +34 -0
  38. package/convex/lib/changelog.ts +278 -0
  39. package/convex/lib/embeddings.ts +38 -0
  40. package/convex/lib/githubBackup.ts +443 -0
  41. package/convex/lib/githubImport.test.ts +247 -0
  42. package/convex/lib/githubImport.ts +425 -0
  43. package/convex/lib/githubSoulBackup.ts +443 -0
  44. package/convex/lib/leaderboards.ts +103 -0
  45. package/convex/lib/moderation.ts +42 -0
  46. package/convex/lib/public.ts +89 -0
  47. package/convex/lib/searchText.test.ts +46 -0
  48. package/convex/lib/searchText.ts +27 -0
  49. package/convex/lib/skillBackfill.test.ts +34 -0
  50. package/convex/lib/skillBackfill.ts +67 -0
  51. package/convex/lib/skillPublish.test.ts +28 -0
  52. package/convex/lib/skillPublish.ts +284 -0
  53. package/convex/lib/skillStats.ts +80 -0
  54. package/convex/lib/skills.test.ts +197 -0
  55. package/convex/lib/skills.ts +273 -0
  56. package/convex/lib/soulChangelog.ts +273 -0
  57. package/convex/lib/soulPublish.ts +236 -0
  58. package/convex/lib/tokens.test.ts +33 -0
  59. package/convex/lib/tokens.ts +51 -0
  60. package/convex/lib/webhooks.test.ts +91 -0
  61. package/convex/lib/webhooks.ts +112 -0
  62. package/convex/maintenance.test.ts +270 -0
  63. package/convex/maintenance.ts +840 -0
  64. package/convex/rateLimits.ts +50 -0
  65. package/convex/schema.ts +472 -0
  66. package/convex/search.test.ts +12 -0
  67. package/convex/search.ts +254 -0
  68. package/convex/seed.test.ts +37 -0
  69. package/convex/seed.ts +254 -0
  70. package/convex/seedSouls.ts +111 -0
  71. package/convex/skillStatEvents.ts +568 -0
  72. package/convex/skills.ts +1606 -0
  73. package/convex/soulComments.ts +88 -0
  74. package/convex/soulDownloads.ts +14 -0
  75. package/convex/soulStars.ts +71 -0
  76. package/convex/souls.ts +570 -0
  77. package/convex/stars.ts +108 -0
  78. package/convex/statsMaintenance.ts +205 -0
  79. package/convex/telemetry.ts +434 -0
  80. package/convex/tokens.ts +88 -0
  81. package/convex/tsconfig.json +7 -0
  82. package/convex/uploads.ts +20 -0
  83. package/convex/users.ts +122 -0
  84. package/convex/webhooks.ts +50 -0
  85. package/convex.json +3 -0
  86. package/docs/README.md +32 -0
  87. package/docs/api.md +51 -0
  88. package/docs/architecture.md +61 -0
  89. package/docs/auth.md +54 -0
  90. package/docs/cli.md +117 -0
  91. package/docs/deploy.md +78 -0
  92. package/docs/diffing.md +84 -0
  93. package/docs/github-import.md +171 -0
  94. package/docs/http-api.md +187 -0
  95. package/docs/manual-testing.md +64 -0
  96. package/docs/mintlify.md +43 -0
  97. package/docs/quickstart.md +120 -0
  98. package/docs/skill-format.md +58 -0
  99. package/docs/soul-format.md +37 -0
  100. package/docs/spec.md +177 -0
  101. package/docs/telemetry.md +91 -0
  102. package/docs/troubleshooting.md +49 -0
  103. package/docs/webhook.md +51 -0
  104. package/e2e/menu-smoke.pw.test.ts +49 -0
  105. package/e2e/pilothub.e2e.test.ts +494 -0
  106. package/e2e/search-exact.pw.test.ts +97 -0
  107. package/package.json +84 -0
  108. package/packages/pilothub/LICENSE +22 -0
  109. package/packages/pilothub/README.md +57 -0
  110. package/packages/pilothub/bin/pilothub.js +2 -0
  111. package/packages/pilothub/package.json +41 -0
  112. package/packages/pilothub/src/browserAuth.test.ts +96 -0
  113. package/packages/pilothub/src/browserAuth.ts +174 -0
  114. package/packages/pilothub/src/cli/buildInfo.ts +94 -0
  115. package/packages/pilothub/src/cli/commands/auth.ts +97 -0
  116. package/packages/pilothub/src/cli/commands/delete.test.ts +73 -0
  117. package/packages/pilothub/src/cli/commands/delete.ts +83 -0
  118. package/packages/pilothub/src/cli/commands/publish.test.ts +122 -0
  119. package/packages/pilothub/src/cli/commands/publish.ts +108 -0
  120. package/packages/pilothub/src/cli/commands/skills.test.ts +191 -0
  121. package/packages/pilothub/src/cli/commands/skills.ts +380 -0
  122. package/packages/pilothub/src/cli/commands/star.ts +46 -0
  123. package/packages/pilothub/src/cli/commands/sync.test.ts +310 -0
  124. package/packages/pilothub/src/cli/commands/sync.ts +200 -0
  125. package/packages/pilothub/src/cli/commands/syncHelpers.test.ts +26 -0
  126. package/packages/pilothub/src/cli/commands/syncHelpers.ts +427 -0
  127. package/packages/pilothub/src/cli/commands/syncTypes.ts +27 -0
  128. package/packages/pilothub/src/cli/commands/unstar.ts +48 -0
  129. package/packages/pilothub/src/cli/helpStyle.ts +45 -0
  130. package/packages/pilothub/src/cli/pilotbotConfig.test.ts +159 -0
  131. package/packages/pilothub/src/cli/pilotbotConfig.ts +147 -0
  132. package/packages/pilothub/src/cli/registry.test.ts +63 -0
  133. package/packages/pilothub/src/cli/registry.ts +43 -0
  134. package/packages/pilothub/src/cli/scanSkills.test.ts +64 -0
  135. package/packages/pilothub/src/cli/scanSkills.ts +84 -0
  136. package/packages/pilothub/src/cli/slug.ts +16 -0
  137. package/packages/pilothub/src/cli/types.ts +12 -0
  138. package/packages/pilothub/src/cli/ui.ts +75 -0
  139. package/packages/pilothub/src/cli.ts +311 -0
  140. package/packages/pilothub/src/config.ts +36 -0
  141. package/packages/pilothub/src/discovery.test.ts +75 -0
  142. package/packages/pilothub/src/discovery.ts +19 -0
  143. package/packages/pilothub/src/http.test.ts +156 -0
  144. package/packages/pilothub/src/http.ts +301 -0
  145. package/packages/pilothub/src/schema/ark.ts +29 -0
  146. package/packages/pilothub/src/schema/index.ts +5 -0
  147. package/packages/pilothub/src/schema/routes.ts +22 -0
  148. package/packages/pilothub/src/schema/schemas.ts +260 -0
  149. package/packages/pilothub/src/schema/textFiles.test.ts +23 -0
  150. package/packages/pilothub/src/schema/textFiles.ts +66 -0
  151. package/packages/pilothub/src/skills.test.ts +191 -0
  152. package/packages/pilothub/src/skills.ts +172 -0
  153. package/packages/pilothub/src/types.ts +10 -0
  154. package/packages/pilothub/tsconfig.json +14 -0
  155. package/packages/schema/README.md +3 -0
  156. package/packages/schema/dist/ark.d.ts +4 -0
  157. package/packages/schema/dist/ark.js +26 -0
  158. package/packages/schema/dist/ark.js.map +1 -0
  159. package/packages/schema/dist/index.d.ts +5 -0
  160. package/packages/schema/dist/index.js +5 -0
  161. package/packages/schema/dist/index.js.map +1 -0
  162. package/packages/schema/dist/routes.d.ts +21 -0
  163. package/packages/schema/dist/routes.js +22 -0
  164. package/packages/schema/dist/routes.js.map +1 -0
  165. package/packages/schema/dist/schemas.d.ts +297 -0
  166. package/packages/schema/dist/schemas.js +243 -0
  167. package/packages/schema/dist/schemas.js.map +1 -0
  168. package/packages/schema/dist/textFiles.d.ts +5 -0
  169. package/packages/schema/dist/textFiles.js +66 -0
  170. package/packages/schema/dist/textFiles.js.map +1 -0
  171. package/packages/schema/package.json +26 -0
  172. package/packages/schema/src/ark.ts +29 -0
  173. package/packages/schema/src/index.ts +5 -0
  174. package/packages/schema/src/routes.ts +22 -0
  175. package/packages/schema/src/schemas.test.ts +123 -0
  176. package/packages/schema/src/schemas.ts +287 -0
  177. package/packages/schema/src/textFiles.test.ts +23 -0
  178. package/packages/schema/src/textFiles.ts +66 -0
  179. package/packages/schema/tsconfig.json +15 -0
  180. package/pilothub +46 -0
  181. package/playwright.config.ts +33 -0
  182. package/public/.well-known/pilothub.json +6 -0
  183. package/public/api/v1/openapi.json +379 -0
  184. package/public/favicon.ico +0 -0
  185. package/public/logo192.png +0 -0
  186. package/public/logo512.png +0 -0
  187. package/public/manifest.json +25 -0
  188. package/public/og.png +0 -0
  189. package/public/og.svg +98 -0
  190. package/public/pilot-logo.png +0 -0
  191. package/public/pilot-mark.png +0 -0
  192. package/public/robots.txt +3 -0
  193. package/public/tanstack-circle-logo.png +0 -0
  194. package/public/tanstack-word-logo-white.svg +1 -0
  195. package/scripts/check-peer-deps.ts +56 -0
  196. package/scripts/docs-list.ts +148 -0
  197. package/scripts/run-playwright-local.sh +14 -0
  198. package/server/og/fetchSkillOgMeta.ts +27 -0
  199. package/server/og/fetchSoulOgMeta.ts +27 -0
  200. package/server/og/ogAssets.ts +80 -0
  201. package/server/og/skillOgSvg.test.ts +59 -0
  202. package/server/og/skillOgSvg.ts +258 -0
  203. package/server/og/soulOgSvg.ts +209 -0
  204. package/server/routes/og/skill.png.ts +103 -0
  205. package/server/routes/og/soul.png.ts +111 -0
  206. package/src/__tests__/skill-detail-page.test.tsx +86 -0
  207. package/src/__tests__/skills-index.test.tsx +145 -0
  208. package/src/__tests__/upload.route.test.tsx +228 -0
  209. package/src/components/AppProviders.tsx +19 -0
  210. package/src/components/ClientOnly.tsx +18 -0
  211. package/src/components/Footer.tsx +29 -0
  212. package/src/components/Header.tsx +295 -0
  213. package/src/components/InstallSwitcher.tsx +53 -0
  214. package/src/components/SkillCard.tsx +36 -0
  215. package/src/components/SkillDetailPage.tsx +817 -0
  216. package/src/components/SkillDiffCard.tsx +485 -0
  217. package/src/components/SoulCard.tsx +19 -0
  218. package/src/components/SoulDetailPage.tsx +263 -0
  219. package/src/components/UserBootstrap.tsx +18 -0
  220. package/src/components/ui/dropdown-menu.tsx +67 -0
  221. package/src/components/ui/toggle-group.tsx +35 -0
  222. package/src/convex/client.ts +3 -0
  223. package/src/lib/badges.ts +29 -0
  224. package/src/lib/diffing.test.ts +163 -0
  225. package/src/lib/diffing.ts +106 -0
  226. package/src/lib/gravatar.test.ts +9 -0
  227. package/src/lib/gravatar.ts +158 -0
  228. package/src/lib/og.test.ts +142 -0
  229. package/src/lib/og.ts +156 -0
  230. package/src/lib/publicUser.ts +39 -0
  231. package/src/lib/roles.ts +19 -0
  232. package/src/lib/site.test.ts +130 -0
  233. package/src/lib/site.ts +84 -0
  234. package/src/lib/theme-transition.test.ts +134 -0
  235. package/src/lib/theme-transition.ts +134 -0
  236. package/src/lib/theme.test.tsx +88 -0
  237. package/src/lib/theme.ts +43 -0
  238. package/src/lib/uploadFiles.jsdom.test.ts +33 -0
  239. package/src/lib/uploadFiles.test.ts +123 -0
  240. package/src/lib/uploadFiles.ts +245 -0
  241. package/src/lib/uploadUtils.test.ts +78 -0
  242. package/src/lib/uploadUtils.ts +93 -0
  243. package/src/lib/useAuthStatus.ts +12 -0
  244. package/src/lib/utils.test.ts +9 -0
  245. package/src/lib/utils.ts +6 -0
  246. package/src/logo.svg +12 -0
  247. package/src/routeTree.gen.ts +345 -0
  248. package/src/router.tsx +17 -0
  249. package/src/routes/$owner/$slug.tsx +55 -0
  250. package/src/routes/__root.tsx +136 -0
  251. package/src/routes/admin.tsx +11 -0
  252. package/src/routes/cli/auth.tsx +168 -0
  253. package/src/routes/dashboard.tsx +97 -0
  254. package/src/routes/import.tsx +415 -0
  255. package/src/routes/index.tsx +252 -0
  256. package/src/routes/management.tsx +529 -0
  257. package/src/routes/settings.tsx +203 -0
  258. package/src/routes/skills/index.tsx +422 -0
  259. package/src/routes/souls/$slug.tsx +55 -0
  260. package/src/routes/souls/index.tsx +243 -0
  261. package/src/routes/stars.tsx +68 -0
  262. package/src/routes/u/$handle.tsx +307 -0
  263. package/src/routes/upload/utils.ts +81 -0
  264. package/src/routes/upload.tsx +499 -0
  265. package/src/styles.css +2718 -0
  266. package/tsconfig.json +24 -0
  267. package/tsconfig.oxlint.json +16 -0
  268. package/vercel.json +8 -0
  269. package/vite.config.ts +48 -0
  270. package/vitest.config.ts +47 -0
  271. package/vitest.e2e.config.ts +11 -0
  272. package/vitest.setup.ts +1 -0
@@ -0,0 +1,570 @@
1
+ import { ConvexError, v } from 'convex/values'
2
+ import { internal } from './_generated/api'
3
+ import type { Doc, Id } from './_generated/dataModel'
4
+ import { action, internalMutation, internalQuery, mutation, query } from './_generated/server'
5
+ import { assertModerator, requireUser, requireUserFromAction } from './lib/access'
6
+ import { toPublicSoul, toPublicUser } from './lib/public'
7
+ import { getFrontmatterValue, hashSkillFiles } from './lib/skills'
8
+ import { generateSoulChangelogPreview } from './lib/soulChangelog'
9
+ import { fetchText, type PublishResult, publishSoulVersionForUser } from './lib/soulPublish'
10
+
11
+ export { publishSoulVersionForUser } from './lib/soulPublish'
12
+
13
+ type ReadmeResult = { path: string; text: string }
14
+
15
+ type FileTextResult = { path: string; text: string; size: number; sha256: string }
16
+
17
+ const MAX_DIFF_FILE_BYTES = 200 * 1024
18
+ const MAX_LIST_LIMIT = 50
19
+
20
+ export const getBySlug = query({
21
+ args: { slug: v.string() },
22
+ handler: async (ctx, args) => {
23
+ const matches = await ctx.db
24
+ .query('souls')
25
+ .withIndex('by_slug', (q) => q.eq('slug', args.slug))
26
+ .order('desc')
27
+ .take(2)
28
+ const soul = matches[0] ?? null
29
+ if (!soul || soul.softDeletedAt) return null
30
+ const latestVersion = soul.latestVersionId ? await ctx.db.get(soul.latestVersionId) : null
31
+ const owner = toPublicUser(await ctx.db.get(soul.ownerUserId))
32
+ const publicSoul = toPublicSoul(soul)
33
+ if (!publicSoul) return null
34
+
35
+ return { soul: publicSoul, latestVersion, owner }
36
+ },
37
+ })
38
+
39
+ export const getSoulBySlugInternal = internalQuery({
40
+ args: { slug: v.string() },
41
+ handler: async (ctx, args) => {
42
+ const matches = await ctx.db
43
+ .query('souls')
44
+ .withIndex('by_slug', (q) => q.eq('slug', args.slug))
45
+ .order('desc')
46
+ .take(2)
47
+ return matches[0] ?? null
48
+ },
49
+ })
50
+
51
+ export const list = query({
52
+ args: {
53
+ ownerUserId: v.optional(v.id('users')),
54
+ limit: v.optional(v.number()),
55
+ },
56
+ handler: async (ctx, args) => {
57
+ const limit = args.limit ?? 24
58
+ const ownerUserId = args.ownerUserId
59
+ if (ownerUserId) {
60
+ const entries = await ctx.db
61
+ .query('souls')
62
+ .withIndex('by_owner', (q) => q.eq('ownerUserId', ownerUserId))
63
+ .order('desc')
64
+ .take(limit * 5)
65
+ return entries
66
+ .filter((soul) => !soul.softDeletedAt)
67
+ .slice(0, limit)
68
+ .map((soul) => toPublicSoul(soul))
69
+ .filter((soul): soul is NonNullable<typeof soul> => Boolean(soul))
70
+ }
71
+ const entries = await ctx.db
72
+ .query('souls')
73
+ .order('desc')
74
+ .take(limit * 5)
75
+ return entries
76
+ .filter((soul) => !soul.softDeletedAt)
77
+ .slice(0, limit)
78
+ .map((soul) => toPublicSoul(soul))
79
+ .filter((soul): soul is NonNullable<typeof soul> => Boolean(soul))
80
+ },
81
+ })
82
+
83
+ export const listPublicPage = query({
84
+ args: {
85
+ cursor: v.optional(v.string()),
86
+ limit: v.optional(v.number()),
87
+ },
88
+ handler: async (ctx, args) => {
89
+ const limit = clampInt(args.limit ?? 24, 1, MAX_LIST_LIMIT)
90
+ const { page, isDone, continueCursor } = await ctx.db
91
+ .query('souls')
92
+ .withIndex('by_updated', (q) => q)
93
+ .order('desc')
94
+ .paginate({ cursor: args.cursor ?? null, numItems: limit })
95
+
96
+ const items: Array<{
97
+ soul: NonNullable<ReturnType<typeof toPublicSoul>>
98
+ latestVersion: Doc<'soulVersions'> | null
99
+ }> = []
100
+
101
+ for (const soul of page) {
102
+ if (soul.softDeletedAt) continue
103
+ const latestVersion = soul.latestVersionId ? await ctx.db.get(soul.latestVersionId) : null
104
+ const publicSoul = toPublicSoul(soul)
105
+ if (!publicSoul) continue
106
+ items.push({ soul: publicSoul, latestVersion })
107
+ }
108
+
109
+ return { items, nextCursor: isDone ? null : continueCursor }
110
+ },
111
+ })
112
+
113
+ export const listVersions = query({
114
+ args: { soulId: v.id('souls'), limit: v.optional(v.number()) },
115
+ handler: async (ctx, args) => {
116
+ const limit = args.limit ?? 20
117
+ return ctx.db
118
+ .query('soulVersions')
119
+ .withIndex('by_soul', (q) => q.eq('soulId', args.soulId))
120
+ .order('desc')
121
+ .take(limit)
122
+ },
123
+ })
124
+
125
+ export const listVersionsPage = query({
126
+ args: {
127
+ soulId: v.id('souls'),
128
+ cursor: v.optional(v.string()),
129
+ limit: v.optional(v.number()),
130
+ },
131
+ handler: async (ctx, args) => {
132
+ const limit = clampInt(args.limit ?? 20, 1, MAX_LIST_LIMIT)
133
+ const { page, isDone, continueCursor } = await ctx.db
134
+ .query('soulVersions')
135
+ .withIndex('by_soul', (q) => q.eq('soulId', args.soulId))
136
+ .order('desc')
137
+ .paginate({ cursor: args.cursor ?? null, numItems: limit })
138
+ const items = page.filter((version) => !version.softDeletedAt)
139
+ return { items, nextCursor: isDone ? null : continueCursor }
140
+ },
141
+ })
142
+
143
+ export const getVersionById = query({
144
+ args: { versionId: v.id('soulVersions') },
145
+ handler: async (ctx, args) => ctx.db.get(args.versionId),
146
+ })
147
+
148
+ export const getVersionByIdInternal = internalQuery({
149
+ args: { versionId: v.id('soulVersions') },
150
+ handler: async (ctx, args) => ctx.db.get(args.versionId),
151
+ })
152
+
153
+ export const getVersionBySoulAndVersion = query({
154
+ args: { soulId: v.id('souls'), version: v.string() },
155
+ handler: async (ctx, args) => {
156
+ return ctx.db
157
+ .query('soulVersions')
158
+ .withIndex('by_soul_version', (q) => q.eq('soulId', args.soulId).eq('version', args.version))
159
+ .unique()
160
+ },
161
+ })
162
+
163
+ export const publishVersion: ReturnType<typeof action> = action({
164
+ args: {
165
+ slug: v.string(),
166
+ displayName: v.string(),
167
+ version: v.string(),
168
+ changelog: v.string(),
169
+ tags: v.optional(v.array(v.string())),
170
+ source: v.optional(
171
+ v.object({
172
+ kind: v.literal('github'),
173
+ url: v.string(),
174
+ repo: v.string(),
175
+ ref: v.string(),
176
+ commit: v.string(),
177
+ path: v.string(),
178
+ importedAt: v.number(),
179
+ }),
180
+ ),
181
+ files: v.array(
182
+ v.object({
183
+ path: v.string(),
184
+ size: v.number(),
185
+ storageId: v.id('_storage'),
186
+ sha256: v.string(),
187
+ contentType: v.optional(v.string()),
188
+ }),
189
+ ),
190
+ },
191
+ handler: async (ctx, args): Promise<PublishResult> => {
192
+ const { userId } = await requireUserFromAction(ctx)
193
+ return publishSoulVersionForUser(ctx, userId, args)
194
+ },
195
+ })
196
+
197
+ export const generateChangelogPreview = action({
198
+ args: {
199
+ slug: v.string(),
200
+ version: v.string(),
201
+ readmeText: v.string(),
202
+ filePaths: v.optional(v.array(v.string())),
203
+ },
204
+ handler: async (ctx, args) => {
205
+ await requireUserFromAction(ctx)
206
+ const changelog = await generateSoulChangelogPreview(ctx, {
207
+ slug: args.slug.trim().toLowerCase(),
208
+ version: args.version.trim(),
209
+ readmeText: args.readmeText,
210
+ filePaths: args.filePaths?.map((value) => value.trim()).filter(Boolean),
211
+ })
212
+ return { changelog, source: 'auto' as const }
213
+ },
214
+ })
215
+
216
+ export const getReadme: ReturnType<typeof action> = action({
217
+ args: { versionId: v.id('soulVersions') },
218
+ handler: async (ctx, args): Promise<ReadmeResult> => {
219
+ const version = (await ctx.runQuery(internal.souls.getVersionByIdInternal, {
220
+ versionId: args.versionId,
221
+ })) as Doc<'soulVersions'> | null
222
+ if (!version) throw new ConvexError('Version not found')
223
+ const readmeFile = version.files.find((file) => file.path.toLowerCase() === 'soul.md')
224
+ if (!readmeFile) throw new ConvexError('SOUL.md not found')
225
+ const text = await fetchText(ctx, readmeFile.storageId)
226
+ return { path: readmeFile.path, text }
227
+ },
228
+ })
229
+
230
+ export const getFileText: ReturnType<typeof action> = action({
231
+ args: { versionId: v.id('soulVersions'), path: v.string() },
232
+ handler: async (ctx, args): Promise<FileTextResult> => {
233
+ const version = (await ctx.runQuery(internal.souls.getVersionByIdInternal, {
234
+ versionId: args.versionId,
235
+ })) as Doc<'soulVersions'> | null
236
+ if (!version) throw new ConvexError('Version not found')
237
+
238
+ const normalizedPath = args.path.trim()
239
+ const normalizedLower = normalizedPath.toLowerCase()
240
+ const file =
241
+ version.files.find((entry) => entry.path === normalizedPath) ??
242
+ version.files.find((entry) => entry.path.toLowerCase() === normalizedLower)
243
+ if (!file) throw new ConvexError('File not found')
244
+ if (file.size > MAX_DIFF_FILE_BYTES) {
245
+ throw new ConvexError('File exceeds 200KB limit')
246
+ }
247
+
248
+ const text = await fetchText(ctx, file.storageId)
249
+ return { path: file.path, text, size: file.size, sha256: file.sha256 }
250
+ },
251
+ })
252
+
253
+ export const resolveVersionByHash = query({
254
+ args: { slug: v.string(), hash: v.string() },
255
+ handler: async (ctx, args) => {
256
+ const slug = args.slug.trim().toLowerCase()
257
+ const hash = args.hash.trim().toLowerCase()
258
+ if (!slug || !/^[a-f0-9]{64}$/.test(hash)) return null
259
+
260
+ const soulMatches = await ctx.db
261
+ .query('souls')
262
+ .withIndex('by_slug', (q) => q.eq('slug', slug))
263
+ .order('desc')
264
+ .take(2)
265
+ const soul = soulMatches[0] ?? null
266
+ if (!soul || soul.softDeletedAt) return null
267
+
268
+ const latestVersion = soul.latestVersionId ? await ctx.db.get(soul.latestVersionId) : null
269
+
270
+ const fingerprintMatches = await ctx.db
271
+ .query('soulVersionFingerprints')
272
+ .withIndex('by_soul_fingerprint', (q) => q.eq('soulId', soul._id).eq('fingerprint', hash))
273
+ .take(25)
274
+
275
+ let match: { version: string } | null = null
276
+ if (fingerprintMatches.length > 0) {
277
+ const newest = fingerprintMatches.reduce(
278
+ (best, entry) => (entry.createdAt > best.createdAt ? entry : best),
279
+ fingerprintMatches[0] as (typeof fingerprintMatches)[number],
280
+ )
281
+ const version = await ctx.db.get(newest.versionId)
282
+ if (version && !version.softDeletedAt) {
283
+ match = { version: version.version }
284
+ }
285
+ }
286
+
287
+ if (!match) {
288
+ const versions = await ctx.db
289
+ .query('soulVersions')
290
+ .withIndex('by_soul', (q) => q.eq('soulId', soul._id))
291
+ .order('desc')
292
+ .take(200)
293
+
294
+ for (const version of versions) {
295
+ if (version.softDeletedAt) continue
296
+ if (typeof version.fingerprint === 'string' && version.fingerprint === hash) {
297
+ match = { version: version.version }
298
+ break
299
+ }
300
+
301
+ const fingerprint = await hashSkillFiles(
302
+ version.files.map((file) => ({ path: file.path, sha256: file.sha256 })),
303
+ )
304
+ if (fingerprint === hash) {
305
+ match = { version: version.version }
306
+ break
307
+ }
308
+ }
309
+ }
310
+
311
+ return {
312
+ match,
313
+ latestVersion: latestVersion ? { version: latestVersion.version } : null,
314
+ }
315
+ },
316
+ })
317
+
318
+ export const updateTags = mutation({
319
+ args: {
320
+ soulId: v.id('souls'),
321
+ tags: v.array(v.object({ tag: v.string(), versionId: v.id('soulVersions') })),
322
+ },
323
+ handler: async (ctx, args) => {
324
+ const { user } = await requireUser(ctx)
325
+ const soul = await ctx.db.get(args.soulId)
326
+ if (!soul) throw new Error('Soul not found')
327
+ if (soul.ownerUserId !== user._id) {
328
+ assertModerator(user)
329
+ }
330
+
331
+ const nextTags = { ...soul.tags }
332
+ for (const entry of args.tags) {
333
+ nextTags[entry.tag] = entry.versionId
334
+ }
335
+
336
+ const latestEntry = args.tags.find((entry) => entry.tag === 'latest')
337
+ await ctx.db.patch(soul._id, {
338
+ tags: nextTags,
339
+ latestVersionId: latestEntry ? latestEntry.versionId : soul.latestVersionId,
340
+ updatedAt: Date.now(),
341
+ })
342
+
343
+ if (latestEntry) {
344
+ const embeddings = await ctx.db
345
+ .query('soulEmbeddings')
346
+ .withIndex('by_soul', (q) => q.eq('soulId', soul._id))
347
+ .collect()
348
+ for (const embedding of embeddings) {
349
+ const isLatest = embedding.versionId === latestEntry.versionId
350
+ await ctx.db.patch(embedding._id, {
351
+ isLatest,
352
+ visibility: visibilityFor(isLatest, embedding.isApproved),
353
+ updatedAt: Date.now(),
354
+ })
355
+ }
356
+ }
357
+ },
358
+ })
359
+
360
+ export const insertVersion = internalMutation({
361
+ args: {
362
+ userId: v.id('users'),
363
+ slug: v.string(),
364
+ displayName: v.string(),
365
+ version: v.string(),
366
+ changelog: v.string(),
367
+ changelogSource: v.optional(v.union(v.literal('auto'), v.literal('user'))),
368
+ tags: v.optional(v.array(v.string())),
369
+ fingerprint: v.string(),
370
+ summary: v.optional(v.string()),
371
+ files: v.array(
372
+ v.object({
373
+ path: v.string(),
374
+ size: v.number(),
375
+ storageId: v.id('_storage'),
376
+ sha256: v.string(),
377
+ contentType: v.optional(v.string()),
378
+ }),
379
+ ),
380
+ parsed: v.object({
381
+ frontmatter: v.record(v.string(), v.any()),
382
+ metadata: v.optional(v.any()),
383
+ }),
384
+ embedding: v.array(v.number()),
385
+ },
386
+ handler: async (ctx, args) => {
387
+ const userId = args.userId
388
+ const user = await ctx.db.get(userId)
389
+ if (!user || user.deletedAt) throw new Error('User not found')
390
+
391
+ const soulMatches = await ctx.db
392
+ .query('souls')
393
+ .withIndex('by_slug', (q) => q.eq('slug', args.slug))
394
+ .order('desc')
395
+ .take(2)
396
+ let soul: Doc<'souls'> | null = soulMatches[0] ?? null
397
+
398
+ if (soul && soul.ownerUserId !== userId) {
399
+ throw new Error('Only the owner can publish updates')
400
+ }
401
+
402
+ const now = Date.now()
403
+ if (!soul) {
404
+ const summary = args.summary ?? getFrontmatterValue(args.parsed.frontmatter, 'description')
405
+ const soulId = await ctx.db.insert('souls', {
406
+ slug: args.slug,
407
+ displayName: args.displayName,
408
+ summary: summary ?? undefined,
409
+ ownerUserId: userId,
410
+ latestVersionId: undefined,
411
+ tags: {},
412
+ softDeletedAt: undefined,
413
+ stats: {
414
+ downloads: 0,
415
+ stars: 0,
416
+ versions: 0,
417
+ comments: 0,
418
+ },
419
+ createdAt: now,
420
+ updatedAt: now,
421
+ })
422
+ soul = await ctx.db.get(soulId)
423
+ }
424
+
425
+ if (!soul) throw new Error('Soul creation failed')
426
+
427
+ const existingVersion = await ctx.db
428
+ .query('soulVersions')
429
+ .withIndex('by_soul_version', (q) => q.eq('soulId', soul._id).eq('version', args.version))
430
+ .unique()
431
+ if (existingVersion) {
432
+ throw new Error('Version already exists')
433
+ }
434
+
435
+ const versionId = await ctx.db.insert('soulVersions', {
436
+ soulId: soul._id,
437
+ version: args.version,
438
+ fingerprint: args.fingerprint,
439
+ changelog: args.changelog,
440
+ changelogSource: args.changelogSource,
441
+ files: args.files,
442
+ parsed: args.parsed,
443
+ createdBy: userId,
444
+ createdAt: now,
445
+ softDeletedAt: undefined,
446
+ })
447
+
448
+ const nextTags: Record<string, Id<'soulVersions'>> = { ...soul.tags }
449
+ nextTags.latest = versionId
450
+ for (const tag of args.tags ?? []) {
451
+ nextTags[tag] = versionId
452
+ }
453
+
454
+ const latestBefore = soul.latestVersionId
455
+
456
+ await ctx.db.patch(soul._id, {
457
+ displayName: args.displayName,
458
+ summary:
459
+ args.summary ?? getFrontmatterValue(args.parsed.frontmatter, 'description') ?? soul.summary,
460
+ latestVersionId: versionId,
461
+ tags: nextTags,
462
+ stats: { ...soul.stats, versions: soul.stats.versions + 1 },
463
+ softDeletedAt: undefined,
464
+ updatedAt: now,
465
+ })
466
+
467
+ const embeddingId = await ctx.db.insert('soulEmbeddings', {
468
+ soulId: soul._id,
469
+ versionId,
470
+ ownerId: userId,
471
+ embedding: args.embedding,
472
+ isLatest: true,
473
+ isApproved: true,
474
+ visibility: visibilityFor(true, true),
475
+ updatedAt: now,
476
+ })
477
+
478
+ if (latestBefore) {
479
+ const previousEmbedding = await ctx.db
480
+ .query('soulEmbeddings')
481
+ .withIndex('by_version', (q) => q.eq('versionId', latestBefore))
482
+ .unique()
483
+ if (previousEmbedding) {
484
+ await ctx.db.patch(previousEmbedding._id, {
485
+ isLatest: false,
486
+ visibility: visibilityFor(false, previousEmbedding.isApproved),
487
+ updatedAt: now,
488
+ })
489
+ }
490
+ }
491
+
492
+ await ctx.db.insert('soulVersionFingerprints', {
493
+ soulId: soul._id,
494
+ versionId,
495
+ fingerprint: args.fingerprint,
496
+ createdAt: now,
497
+ })
498
+
499
+ return { soulId: soul._id, versionId, embeddingId }
500
+ },
501
+ })
502
+
503
+ export const setSoulSoftDeletedInternal = internalMutation({
504
+ args: {
505
+ userId: v.id('users'),
506
+ slug: v.string(),
507
+ deleted: v.boolean(),
508
+ },
509
+ handler: async (ctx, args) => {
510
+ const user = await ctx.db.get(args.userId)
511
+ if (!user || user.deletedAt) throw new Error('User not found')
512
+
513
+ const slug = args.slug.trim().toLowerCase()
514
+ if (!slug) throw new Error('Slug required')
515
+
516
+ const soulMatches = await ctx.db
517
+ .query('souls')
518
+ .withIndex('by_slug', (q) => q.eq('slug', slug))
519
+ .order('desc')
520
+ .take(2)
521
+ const soul = soulMatches[0] ?? null
522
+ if (!soul) throw new Error('Soul not found')
523
+
524
+ if (soul.ownerUserId !== args.userId) {
525
+ assertModerator(user)
526
+ }
527
+
528
+ const now = Date.now()
529
+ await ctx.db.patch(soul._id, {
530
+ softDeletedAt: args.deleted ? now : undefined,
531
+ updatedAt: now,
532
+ })
533
+
534
+ const embeddings = await ctx.db
535
+ .query('soulEmbeddings')
536
+ .withIndex('by_soul', (q) => q.eq('soulId', soul._id))
537
+ .collect()
538
+ for (const embedding of embeddings) {
539
+ await ctx.db.patch(embedding._id, {
540
+ visibility: args.deleted
541
+ ? 'deleted'
542
+ : visibilityFor(embedding.isLatest, embedding.isApproved),
543
+ updatedAt: now,
544
+ })
545
+ }
546
+
547
+ await ctx.db.insert('auditLogs', {
548
+ actorUserId: args.userId,
549
+ action: args.deleted ? 'soul.delete' : 'soul.undelete',
550
+ targetType: 'soul',
551
+ targetId: soul._id,
552
+ metadata: { slug, softDeletedAt: args.deleted ? now : null },
553
+ createdAt: now,
554
+ })
555
+
556
+ return { ok: true as const }
557
+ },
558
+ })
559
+
560
+ function visibilityFor(isLatest: boolean, isApproved: boolean) {
561
+ if (isLatest && isApproved) return 'latest-approved'
562
+ if (isLatest) return 'latest'
563
+ if (isApproved) return 'archived-approved'
564
+ return 'archived'
565
+ }
566
+
567
+ function clampInt(value: number, min: number, max: number) {
568
+ const rounded = Number.isFinite(value) ? Math.round(value) : min
569
+ return Math.min(max, Math.max(min, rounded))
570
+ }
@@ -0,0 +1,108 @@
1
+ import { v } from 'convex/values'
2
+ import { internalMutation, mutation, query } from './_generated/server'
3
+ import { requireUser } from './lib/access'
4
+ import { toPublicSkill } from './lib/public'
5
+ import { insertStatEvent } from './skillStatEvents'
6
+
7
+ export const isStarred = query({
8
+ args: { skillId: v.id('skills') },
9
+ handler: async (ctx, args) => {
10
+ const { userId } = await requireUser(ctx)
11
+ const existing = await ctx.db
12
+ .query('stars')
13
+ .withIndex('by_skill_user', (q) => q.eq('skillId', args.skillId).eq('userId', userId))
14
+ .unique()
15
+ return Boolean(existing)
16
+ },
17
+ })
18
+
19
+ export const toggle = mutation({
20
+ args: { skillId: v.id('skills') },
21
+ handler: async (ctx, args) => {
22
+ const { userId } = await requireUser(ctx)
23
+ const skill = await ctx.db.get(args.skillId)
24
+ if (!skill) throw new Error('Skill not found')
25
+
26
+ const existing = await ctx.db
27
+ .query('stars')
28
+ .withIndex('by_skill_user', (q) => q.eq('skillId', args.skillId).eq('userId', userId))
29
+ .unique()
30
+
31
+ if (existing) {
32
+ await ctx.db.delete(existing._id)
33
+ await insertStatEvent(ctx, { skillId: skill._id, kind: 'unstar' })
34
+ return { starred: false }
35
+ }
36
+
37
+ await ctx.db.insert('stars', {
38
+ skillId: args.skillId,
39
+ userId,
40
+ createdAt: Date.now(),
41
+ })
42
+
43
+ await insertStatEvent(ctx, { skillId: skill._id, kind: 'star' })
44
+
45
+ return { starred: true }
46
+ },
47
+ })
48
+
49
+ export const listByUser = query({
50
+ args: { userId: v.id('users'), limit: v.optional(v.number()) },
51
+ handler: async (ctx, args) => {
52
+ const limit = args.limit ?? 50
53
+ const stars = await ctx.db
54
+ .query('stars')
55
+ .withIndex('by_user', (q) => q.eq('userId', args.userId))
56
+ .order('desc')
57
+ .take(limit)
58
+ const skills: NonNullable<ReturnType<typeof toPublicSkill>>[] = []
59
+ for (const star of stars) {
60
+ const skill = await ctx.db.get(star.skillId)
61
+ const publicSkill = toPublicSkill(skill)
62
+ if (!publicSkill) continue
63
+ skills.push(publicSkill)
64
+ }
65
+ return skills
66
+ },
67
+ })
68
+
69
+ export const addStarInternal = internalMutation({
70
+ args: { userId: v.id('users'), skillId: v.id('skills') },
71
+ handler: async (ctx, args) => {
72
+ const skill = await ctx.db.get(args.skillId)
73
+ if (!skill) throw new Error('Skill not found')
74
+ const existing = await ctx.db
75
+ .query('stars')
76
+ .withIndex('by_skill_user', (q) => q.eq('skillId', args.skillId).eq('userId', args.userId))
77
+ .unique()
78
+ if (existing) return { ok: true as const, starred: true, alreadyStarred: true }
79
+
80
+ await ctx.db.insert('stars', {
81
+ skillId: args.skillId,
82
+ userId: args.userId,
83
+ createdAt: Date.now(),
84
+ })
85
+
86
+ await insertStatEvent(ctx, { skillId: skill._id, kind: 'star' })
87
+
88
+ return { ok: true as const, starred: true, alreadyStarred: false }
89
+ },
90
+ })
91
+
92
+ export const removeStarInternal = internalMutation({
93
+ args: { userId: v.id('users'), skillId: v.id('skills') },
94
+ handler: async (ctx, args) => {
95
+ const skill = await ctx.db.get(args.skillId)
96
+ if (!skill) throw new Error('Skill not found')
97
+ const existing = await ctx.db
98
+ .query('stars')
99
+ .withIndex('by_skill_user', (q) => q.eq('skillId', args.skillId).eq('userId', args.userId))
100
+ .unique()
101
+ if (!existing) return { ok: true as const, unstarred: false, alreadyUnstarred: true }
102
+
103
+ await ctx.db.delete(existing._id)
104
+ await insertStatEvent(ctx, { skillId: skill._id, kind: 'unstar' })
105
+
106
+ return { ok: true as const, unstarred: true, alreadyUnstarred: false }
107
+ },
108
+ })