@larkup/marketplace 0.1.25 → 0.2.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 (65) hide show
  1. package/.env +6 -0
  2. package/.env.example +3 -0
  3. package/.turbo/turbo-test.log +17 -0
  4. package/.turbo/turbo-type-check.log +4 -0
  5. package/CHANGELOG.md +183 -0
  6. package/README.md +34 -0
  7. package/db/dist/builtin-tools.d.ts +2 -0
  8. package/db/dist/builtin-tools.js +116 -0
  9. package/db/dist/client.d.ts +7 -0
  10. package/db/dist/client.js +20 -0
  11. package/db/dist/repo.d.ts +67 -0
  12. package/db/dist/repo.js +236 -0
  13. package/db/dist/schema/audit.d.ts +128 -0
  14. package/db/dist/schema/audit.js +18 -0
  15. package/db/dist/schema/extensions.d.ts +326 -0
  16. package/db/dist/schema/extensions.js +36 -0
  17. package/db/dist/schema/index.d.ts +5 -0
  18. package/db/dist/schema/index.js +5 -0
  19. package/db/dist/schema/installations.d.ts +93 -0
  20. package/db/dist/schema/installations.js +15 -0
  21. package/db/dist/schema/publishers.d.ts +110 -0
  22. package/db/dist/schema/publishers.js +12 -0
  23. package/db/dist/schema/versions.d.ts +127 -0
  24. package/db/dist/schema/versions.js +17 -0
  25. package/db/dist/seed.d.ts +2 -0
  26. package/db/dist/seed.js +31 -0
  27. package/db/dist/validate.d.ts +6 -0
  28. package/db/dist/validate.js +51 -0
  29. package/db/drizzle/0000_hesitant_joseph.sql +69 -0
  30. package/db/drizzle/0001_deep_phil_sheldon.sql +1 -0
  31. package/db/drizzle/0002_jittery_living_mummy.sql +18 -0
  32. package/db/drizzle/meta/0000_snapshot.json +483 -0
  33. package/db/drizzle/meta/0001_snapshot.json +490 -0
  34. package/db/drizzle/meta/0002_snapshot.json +615 -0
  35. package/db/drizzle/meta/_journal.json +27 -0
  36. package/db/drizzle.config.ts +19 -0
  37. package/db/tsconfig.build.json +11 -0
  38. package/db/tsconfig.json +17 -0
  39. package/db/vitest.config.ts +11 -0
  40. package/package.json +31 -4
  41. package/src/db/builtin-tools.ts +613 -0
  42. package/src/db/client.ts +26 -0
  43. package/src/db/repo.test.ts +309 -0
  44. package/src/db/repo.ts +558 -0
  45. package/src/db/schema/audit.ts +27 -0
  46. package/src/db/schema/extensions.ts +73 -0
  47. package/src/db/schema/index.ts +5 -0
  48. package/src/db/schema/installations.ts +23 -0
  49. package/src/db/schema/publishers.ts +13 -0
  50. package/src/db/schema/versions.ts +22 -0
  51. package/src/db/seed.ts +33 -0
  52. package/src/db/validate.test.ts +79 -0
  53. package/src/db/validate.ts +84 -0
  54. package/src/output-sanitizer.test.ts +17 -0
  55. package/src/output-sanitizer.ts +8 -0
  56. package/src/storage-provider-s3.test.ts +110 -0
  57. package/src/storage-provider.ts +164 -13
  58. package/src/tool-extension.ts +247 -0
  59. package/src/tool-installer.ts +80 -109
  60. package/src/tool-loader.test.ts +120 -0
  61. package/src/tool-loader.ts +99 -37
  62. package/src/tool-manifest.schema.ts +89 -32
  63. package/src/tool-registry.ts +34 -111
  64. package/src/types.ts +217 -87
  65. package/vitest.config.ts +13 -0
@@ -0,0 +1,236 @@
1
+ /** Marketplace catalog queries. */
2
+ import { and, count, eq, ilike, or, sql } from 'drizzle-orm';
3
+ import { auditEvents } from './schema/audit.js';
4
+ import { extensionWorkspaceGrants, extensions } from './schema/extensions.js';
5
+ import { extensionVersions } from './schema/versions.js';
6
+ import { publishers } from './schema/publishers.js';
7
+ import { workspaceInstallations } from './schema/installations.js';
8
+ import { validateToolManifest } from './validate.js';
9
+ export class ManifestInvalidError extends Error {
10
+ errors;
11
+ constructor(errors) {
12
+ super(`Invalid manifest: ${errors.join('; ')}`);
13
+ this.errors = errors;
14
+ this.name = 'ManifestInvalidError';
15
+ }
16
+ }
17
+ /** Thrown when another publisher owns the extension id. */
18
+ export class NotOwnerError extends Error {
19
+ constructor(extensionId) {
20
+ super(`"${extensionId}" is published by a different publisher.`);
21
+ this.name = 'NotOwnerError';
22
+ }
23
+ }
24
+ /** Thrown when an extension version already exists. */
25
+ export class VersionExistsError extends Error {
26
+ constructor(extensionId, version) {
27
+ super(`"${extensionId}@${version}" is already published. Publish a new version instead.`);
28
+ this.name = 'VersionExistsError';
29
+ }
30
+ }
31
+ /** Limits private extensions to granted workspaces. */
32
+ function visibilityFilter(workspaceId) {
33
+ return workspaceId
34
+ ? or(eq(extensions.distribution, 'public'), sql `exists (select 1 from ${extensionWorkspaceGrants}
35
+ where ${extensionWorkspaceGrants.extensionId} = ${extensions.id}
36
+ and ${extensionWorkspaceGrants.workspaceId} = ${workspaceId})`)
37
+ : eq(extensions.distribution, 'public');
38
+ }
39
+ async function latestVersionSubquery(db, extensionId) {
40
+ const [latest] = await db
41
+ .select()
42
+ .from(extensionVersions)
43
+ .where(eq(extensionVersions.extensionId, extensionId))
44
+ .orderBy(sql `${extensionVersions.publishedAt} desc`)
45
+ .limit(1);
46
+ return latest;
47
+ }
48
+ export async function listExtensions(db, options = {}) {
49
+ const conditions = [visibilityFilter(options.workspaceId)];
50
+ if (options.category)
51
+ conditions.push(eq(extensions.category, options.category));
52
+ if (options.search) {
53
+ const q = `%${options.search}%`;
54
+ conditions.push(or(ilike(extensions.name, q), ilike(extensions.description, q), ilike(extensions.packageName, q)));
55
+ }
56
+ const where = and(...conditions);
57
+ const rows = await db
58
+ .select({ ext: extensions, publisher: publishers })
59
+ .from(extensions)
60
+ .innerJoin(publishers, eq(extensions.publisherId, publishers.id))
61
+ .where(where)
62
+ .orderBy(extensions.name)
63
+ .limit(options.limit ?? 50)
64
+ .offset(options.offset ?? 0);
65
+ const [{ value: total }] = await db.select({ value: count() }).from(extensions).where(where);
66
+ const entries = await Promise.all(rows.map(async ({ ext, publisher }) => {
67
+ const latest = await latestVersionSubquery(db, ext.id);
68
+ const [{ value: installs }] = await db
69
+ .select({ value: count() })
70
+ .from(workspaceInstallations)
71
+ .where(eq(workspaceInstallations.extensionId, ext.id));
72
+ return toEntry(ext, publisher, latest, installs);
73
+ }));
74
+ return { entries, total };
75
+ }
76
+ export async function getExtension(db, id, options = {}) {
77
+ const [row] = await db
78
+ .select({ ext: extensions, publisher: publishers })
79
+ .from(extensions)
80
+ .innerJoin(publishers, eq(extensions.publisherId, publishers.id))
81
+ .where(and(eq(extensions.id, id), visibilityFilter(options.workspaceId)))
82
+ .limit(1);
83
+ if (!row)
84
+ return null;
85
+ const versionRows = await db
86
+ .select({ version: extensionVersions.version, publishedAt: extensionVersions.publishedAt })
87
+ .from(extensionVersions)
88
+ .where(eq(extensionVersions.extensionId, id))
89
+ .orderBy(sql `${extensionVersions.publishedAt} desc`);
90
+ const latest = await latestVersionSubquery(db, id);
91
+ const [{ value: installs }] = await db
92
+ .select({ value: count() })
93
+ .from(workspaceInstallations)
94
+ .where(eq(workspaceInstallations.extensionId, id));
95
+ return { entry: toEntry(row.ext, row.publisher, latest, installs), versions: versionRows };
96
+ }
97
+ function toEntry(ext, publisher, latest, installs) {
98
+ return {
99
+ id: ext.id,
100
+ publisherId: ext.publisherId,
101
+ publisherName: publisher.name,
102
+ publisherVerification: publisher.verification,
103
+ kind: ext.kind,
104
+ name: ext.name,
105
+ description: ext.description,
106
+ category: ext.category,
107
+ packageName: ext.packageName,
108
+ requiresSandbox: ext.requiresSandbox,
109
+ distribution: ext.distribution,
110
+ repositoryUrl: ext.repositoryUrl,
111
+ license: ext.license,
112
+ version: latest?.version ?? '0.0.0',
113
+ manifest: latest?.manifest ?? {},
114
+ installs,
115
+ updatedAt: ext.updatedAt,
116
+ };
117
+ }
118
+ async function sha256Hex(input) {
119
+ const digest = await crypto.subtle.digest('SHA-256', new TextEncoder().encode(input));
120
+ return Array.from(new Uint8Array(digest))
121
+ .map((b) => b.toString(16).padStart(2, '0'))
122
+ .join('');
123
+ }
124
+ /** Publishes an immutable extension version. */
125
+ export async function publishExtension(db, input) {
126
+ const manifest = {
127
+ ...input.manifest,
128
+ requiresSandbox: input.manifest.requiresSandbox !== false,
129
+ };
130
+ const validation = validateToolManifest(manifest);
131
+ if (!validation.valid)
132
+ throw new ManifestInvalidError(validation.errors);
133
+ const m = manifest;
134
+ await db
135
+ .insert(publishers)
136
+ .values({ id: input.publisherId, name: input.publisherName ?? input.publisherId })
137
+ .onConflictDoNothing();
138
+ const [existing] = await db.select().from(extensions).where(eq(extensions.id, m.id)).limit(1);
139
+ if (existing && existing.publisherId !== input.publisherId) {
140
+ throw new NotOwnerError(m.id);
141
+ }
142
+ const [existingVersion] = await db
143
+ .select({ id: extensionVersions.id })
144
+ .from(extensionVersions)
145
+ .where(and(eq(extensionVersions.extensionId, m.id), eq(extensionVersions.version, m.version)))
146
+ .limit(1);
147
+ if (existingVersion)
148
+ throw new VersionExistsError(m.id, m.version);
149
+ const now = new Date();
150
+ if (existing) {
151
+ await db
152
+ .update(extensions)
153
+ .set({
154
+ name: m.name,
155
+ description: m.description,
156
+ category: m.category,
157
+ packageName: m.packageName,
158
+ requiresSandbox: m.requiresSandbox,
159
+ repositoryUrl: m.repositoryUrl ?? null,
160
+ license: m.license ?? null,
161
+ updatedAt: now,
162
+ })
163
+ .where(eq(extensions.id, m.id));
164
+ }
165
+ else {
166
+ await db.insert(extensions).values({
167
+ id: m.id,
168
+ publisherId: input.publisherId,
169
+ kind: m.kind ?? 'tool',
170
+ name: m.name,
171
+ description: m.description,
172
+ category: m.category,
173
+ packageName: m.packageName,
174
+ requiresSandbox: m.requiresSandbox,
175
+ repositoryUrl: m.repositoryUrl ?? null,
176
+ license: m.license ?? null,
177
+ });
178
+ }
179
+ const integrity = await sha256Hex(JSON.stringify(manifest));
180
+ await db.insert(extensionVersions).values({
181
+ id: crypto.randomUUID(),
182
+ extensionId: m.id,
183
+ version: m.version,
184
+ manifest,
185
+ integrity,
186
+ publishedBy: input.publisherId,
187
+ });
188
+ await db.insert(auditEvents).values({
189
+ id: crypto.randomUUID(),
190
+ actor: input.publisherId,
191
+ action: 'extension.published',
192
+ extensionId: m.id,
193
+ metadata: { version: m.version },
194
+ });
195
+ return { extensionId: m.id, version: m.version };
196
+ }
197
+ export async function recordInstall(db, extensionId, workspaceId) {
198
+ const [ext] = await db
199
+ .select({ id: extensions.id })
200
+ .from(extensions)
201
+ .where(eq(extensions.id, extensionId));
202
+ if (!ext)
203
+ return null;
204
+ await db
205
+ .insert(workspaceInstallations)
206
+ .values({ id: crypto.randomUUID(), extensionId, workspaceId })
207
+ .onConflictDoUpdate({
208
+ target: [workspaceInstallations.extensionId, workspaceInstallations.workspaceId],
209
+ set: { updatedAt: new Date() },
210
+ });
211
+ await db.insert(auditEvents).values({
212
+ id: crypto.randomUUID(),
213
+ actor: workspaceId,
214
+ action: 'extension.installed',
215
+ extensionId,
216
+ workspaceId,
217
+ });
218
+ const [{ value: installs }] = await db
219
+ .select({ value: count() })
220
+ .from(workspaceInstallations)
221
+ .where(eq(workspaceInstallations.extensionId, extensionId));
222
+ return { installs };
223
+ }
224
+ export async function grantWorkspaceAccess(db, extensionId, workspaceId) {
225
+ await db
226
+ .insert(extensionWorkspaceGrants)
227
+ .values({ id: crypto.randomUUID(), extensionId, workspaceId })
228
+ .onConflictDoNothing();
229
+ await db.insert(auditEvents).values({
230
+ id: crypto.randomUUID(),
231
+ actor: 'system',
232
+ action: 'extension.grant_added',
233
+ extensionId,
234
+ workspaceId,
235
+ });
236
+ }
@@ -0,0 +1,128 @@
1
+ export declare const AUDIT_ACTIONS: readonly ["extension.published", "extension.installed", "extension.grant_added", "extension.grant_removed", "extension.deprecated"];
2
+ /** Catalog audit trail. */
3
+ export declare const auditEvents: import("drizzle-orm/pg-core").PgTableWithColumns<{
4
+ name: "audit_events";
5
+ schema: undefined;
6
+ columns: {
7
+ id: import("drizzle-orm/pg-core").PgColumn<{
8
+ name: "id";
9
+ tableName: "audit_events";
10
+ dataType: "string";
11
+ columnType: "PgText";
12
+ data: string;
13
+ driverParam: string;
14
+ notNull: true;
15
+ hasDefault: false;
16
+ isPrimaryKey: true;
17
+ isAutoincrement: false;
18
+ hasRuntimeDefault: false;
19
+ enumValues: [string, ...string[]];
20
+ baseColumn: never;
21
+ identity: undefined;
22
+ generated: undefined;
23
+ }, {}, {}>;
24
+ actor: import("drizzle-orm/pg-core").PgColumn<{
25
+ name: "actor";
26
+ tableName: "audit_events";
27
+ dataType: "string";
28
+ columnType: "PgText";
29
+ data: string;
30
+ driverParam: string;
31
+ notNull: true;
32
+ hasDefault: false;
33
+ isPrimaryKey: false;
34
+ isAutoincrement: false;
35
+ hasRuntimeDefault: false;
36
+ enumValues: [string, ...string[]];
37
+ baseColumn: never;
38
+ identity: undefined;
39
+ generated: undefined;
40
+ }, {}, {}>;
41
+ action: import("drizzle-orm/pg-core").PgColumn<{
42
+ name: "action";
43
+ tableName: "audit_events";
44
+ dataType: "string";
45
+ columnType: "PgText";
46
+ data: "extension.published" | "extension.installed" | "extension.grant_added" | "extension.grant_removed" | "extension.deprecated";
47
+ driverParam: string;
48
+ notNull: true;
49
+ hasDefault: false;
50
+ isPrimaryKey: false;
51
+ isAutoincrement: false;
52
+ hasRuntimeDefault: false;
53
+ enumValues: ["extension.published", "extension.installed", "extension.grant_added", "extension.grant_removed", "extension.deprecated"];
54
+ baseColumn: never;
55
+ identity: undefined;
56
+ generated: undefined;
57
+ }, {}, {}>;
58
+ extensionId: import("drizzle-orm/pg-core").PgColumn<{
59
+ name: "extension_id";
60
+ tableName: "audit_events";
61
+ dataType: "string";
62
+ columnType: "PgText";
63
+ data: string;
64
+ driverParam: string;
65
+ notNull: false;
66
+ hasDefault: false;
67
+ isPrimaryKey: false;
68
+ isAutoincrement: false;
69
+ hasRuntimeDefault: false;
70
+ enumValues: [string, ...string[]];
71
+ baseColumn: never;
72
+ identity: undefined;
73
+ generated: undefined;
74
+ }, {}, {}>;
75
+ workspaceId: import("drizzle-orm/pg-core").PgColumn<{
76
+ name: "workspace_id";
77
+ tableName: "audit_events";
78
+ dataType: "string";
79
+ columnType: "PgText";
80
+ data: string;
81
+ driverParam: string;
82
+ notNull: false;
83
+ hasDefault: false;
84
+ isPrimaryKey: false;
85
+ isAutoincrement: false;
86
+ hasRuntimeDefault: false;
87
+ enumValues: [string, ...string[]];
88
+ baseColumn: never;
89
+ identity: undefined;
90
+ generated: undefined;
91
+ }, {}, {}>;
92
+ metadata: import("drizzle-orm/pg-core").PgColumn<{
93
+ name: "metadata";
94
+ tableName: "audit_events";
95
+ dataType: "json";
96
+ columnType: "PgJsonb";
97
+ data: unknown;
98
+ driverParam: unknown;
99
+ notNull: false;
100
+ hasDefault: false;
101
+ isPrimaryKey: false;
102
+ isAutoincrement: false;
103
+ hasRuntimeDefault: false;
104
+ enumValues: undefined;
105
+ baseColumn: never;
106
+ identity: undefined;
107
+ generated: undefined;
108
+ }, {}, {}>;
109
+ createdAt: import("drizzle-orm/pg-core").PgColumn<{
110
+ name: "created_at";
111
+ tableName: "audit_events";
112
+ dataType: "date";
113
+ columnType: "PgTimestamp";
114
+ data: Date;
115
+ driverParam: string;
116
+ notNull: true;
117
+ hasDefault: true;
118
+ isPrimaryKey: false;
119
+ isAutoincrement: false;
120
+ hasRuntimeDefault: false;
121
+ enumValues: undefined;
122
+ baseColumn: never;
123
+ identity: undefined;
124
+ generated: undefined;
125
+ }, {}, {}>;
126
+ };
127
+ dialect: "pg";
128
+ }>;
@@ -0,0 +1,18 @@
1
+ import { index, jsonb, pgTable, text, timestamp } from 'drizzle-orm/pg-core';
2
+ export const AUDIT_ACTIONS = [
3
+ 'extension.published',
4
+ 'extension.installed',
5
+ 'extension.grant_added',
6
+ 'extension.grant_removed',
7
+ 'extension.deprecated',
8
+ ];
9
+ /** Catalog audit trail. */
10
+ export const auditEvents = pgTable('audit_events', {
11
+ id: text('id').primaryKey(),
12
+ actor: text('actor').notNull(),
13
+ action: text('action', { enum: AUDIT_ACTIONS }).notNull(),
14
+ extensionId: text('extension_id'),
15
+ workspaceId: text('workspace_id'),
16
+ metadata: jsonb('metadata'),
17
+ createdAt: timestamp('created_at', { withTimezone: true }).notNull().defaultNow(),
18
+ }, (table) => [index('audit_events_extension_id_idx').on(table.extensionId)]);