@secondlayer/shared 0.10.1 → 0.11.0

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 (48) hide show
  1. package/dist/src/db/index.d.ts +181 -2
  2. package/dist/src/db/queries/accounts.d.ts +158 -2
  3. package/dist/src/db/queries/accounts.js +17 -1
  4. package/dist/src/db/queries/accounts.js.map +3 -3
  5. package/dist/src/db/queries/integrity.d.ts +151 -1
  6. package/dist/src/db/queries/marketplace.d.ts +463 -0
  7. package/dist/src/db/queries/marketplace.js +142 -0
  8. package/dist/src/db/queries/marketplace.js.map +10 -0
  9. package/dist/src/db/queries/metrics.d.ts +151 -1
  10. package/dist/src/db/queries/projects.d.ts +423 -0
  11. package/dist/src/db/queries/projects.js +47 -0
  12. package/dist/src/db/queries/projects.js.map +10 -0
  13. package/dist/src/db/queries/subgraph-gaps.d.ts +151 -1
  14. package/dist/src/db/queries/subgraphs.d.ts +158 -6
  15. package/dist/src/db/queries/subgraphs.js +16 -13
  16. package/dist/src/db/queries/subgraphs.js.map +3 -3
  17. package/dist/src/db/queries/usage.d.ts +151 -1
  18. package/dist/src/db/queries/workflows.d.ts +439 -0
  19. package/dist/src/db/queries/workflows.js +115 -0
  20. package/dist/src/db/queries/workflows.js.map +11 -0
  21. package/dist/src/db/schema.d.ts +181 -2
  22. package/dist/src/index.d.ts +251 -10
  23. package/dist/src/index.js +91 -72
  24. package/dist/src/index.js.map +4 -3
  25. package/dist/src/node/hiro-pg-client.js +5 -3
  26. package/dist/src/node/hiro-pg-client.js.map +3 -3
  27. package/dist/src/node/local-client.d.ts +155 -1
  28. package/dist/src/node/local-client.js +19 -9
  29. package/dist/src/node/local-client.js.map +3 -3
  30. package/dist/src/schemas/index.d.ts +71 -9
  31. package/dist/src/schemas/index.js +93 -74
  32. package/dist/src/schemas/index.js.map +4 -3
  33. package/dist/src/schemas/marketplace.d.ts +63 -0
  34. package/dist/src/schemas/marketplace.js +39 -0
  35. package/dist/src/schemas/marketplace.js.map +10 -0
  36. package/dist/src/schemas/workflows.d.ts +66 -0
  37. package/dist/src/schemas/workflows.js +39 -0
  38. package/dist/src/schemas/workflows.js.map +10 -0
  39. package/dist/src/types.d.ts +3 -0
  40. package/migrations/0021_tx_function_args_result.ts +27 -0
  41. package/migrations/0022_marketplace.ts +88 -0
  42. package/migrations/0023_projects.ts +149 -0
  43. package/migrations/0024_chat_sessions.ts +51 -0
  44. package/migrations/0025_chat_session_summary.ts +15 -0
  45. package/migrations/0026_workflows.ts +204 -0
  46. package/migrations/0027_workflow_cursors.ts +16 -0
  47. package/migrations/0028_subgraph_account_scoping.ts +116 -0
  48. package/package.json +22 -2
@@ -0,0 +1,15 @@
1
+ import type { Kysely } from "kysely";
2
+
3
+ export async function up(db: Kysely<any>): Promise<void> {
4
+ await db.schema
5
+ .alterTable("chat_sessions")
6
+ .addColumn("summary", "jsonb")
7
+ .execute();
8
+ }
9
+
10
+ export async function down(db: Kysely<any>): Promise<void> {
11
+ await db.schema
12
+ .alterTable("chat_sessions")
13
+ .dropColumn("summary")
14
+ .execute();
15
+ }
@@ -0,0 +1,204 @@
1
+ import { sql, type Kysely } from "kysely";
2
+
3
+ export async function up(db: Kysely<any>): Promise<void> {
4
+ // ── workflow_definitions ──────────────────────────────────────────
5
+ await db.schema
6
+ .createTable("workflow_definitions")
7
+ .addColumn("id", "uuid", (c) =>
8
+ c.primaryKey().defaultTo(sql`gen_random_uuid()`),
9
+ )
10
+ .addColumn("name", "text", (c) => c.notNull())
11
+ .addColumn("version", "text", (c) => c.notNull().defaultTo("1.0.0"))
12
+ .addColumn("status", "text", (c) => c.notNull().defaultTo("active"))
13
+ .addColumn("trigger_type", "text", (c) => c.notNull())
14
+ .addColumn("trigger_config", "jsonb", (c) => c.notNull())
15
+ .addColumn("handler_path", "text", (c) => c.notNull())
16
+ .addColumn("retries_config", "jsonb")
17
+ .addColumn("timeout_ms", "integer")
18
+ .addColumn("api_key_id", "uuid", (c) =>
19
+ c.notNull().references("api_keys.id"),
20
+ )
21
+ .addColumn("project_id", "uuid", (c) =>
22
+ c.references("projects.id").onDelete("set null"),
23
+ )
24
+ .addColumn("created_at", "timestamptz", (c) =>
25
+ c.notNull().defaultTo(sql`now()`),
26
+ )
27
+ .addColumn("updated_at", "timestamptz", (c) =>
28
+ c.notNull().defaultTo(sql`now()`),
29
+ )
30
+ .execute();
31
+
32
+ await sql`CREATE UNIQUE INDEX workflow_definitions_name_key_idx ON workflow_definitions (name, api_key_id)`.execute(
33
+ db,
34
+ );
35
+ await sql`CREATE INDEX workflow_definitions_status_idx ON workflow_definitions (status)`.execute(
36
+ db,
37
+ );
38
+
39
+ // ── workflow_runs ─────────────────────────────────────────────────
40
+ await db.schema
41
+ .createTable("workflow_runs")
42
+ .addColumn("id", "uuid", (c) =>
43
+ c.primaryKey().defaultTo(sql`gen_random_uuid()`),
44
+ )
45
+ .addColumn("definition_id", "uuid", (c) =>
46
+ c.notNull().references("workflow_definitions.id").onDelete("cascade"),
47
+ )
48
+ .addColumn("status", "text", (c) => c.notNull().defaultTo("pending"))
49
+ .addColumn("trigger_type", "text", (c) => c.notNull())
50
+ .addColumn("trigger_data", "jsonb")
51
+ .addColumn("dedup_key", "text")
52
+ .addColumn("error", "text")
53
+ .addColumn("started_at", "timestamptz")
54
+ .addColumn("completed_at", "timestamptz")
55
+ .addColumn("duration_ms", "integer")
56
+ .addColumn("total_ai_tokens", "integer", (c) => c.notNull().defaultTo(0))
57
+ .addColumn("created_at", "timestamptz", (c) =>
58
+ c.notNull().defaultTo(sql`now()`),
59
+ )
60
+ .execute();
61
+
62
+ await sql`CREATE INDEX workflow_runs_definition_idx ON workflow_runs (definition_id, created_at DESC)`.execute(
63
+ db,
64
+ );
65
+ await sql`CREATE INDEX workflow_runs_status_idx ON workflow_runs (status)`.execute(
66
+ db,
67
+ );
68
+ await sql`CREATE UNIQUE INDEX workflow_runs_dedup_idx ON workflow_runs (definition_id, dedup_key) WHERE dedup_key IS NOT NULL`.execute(
69
+ db,
70
+ );
71
+
72
+ // ── workflow_steps ────────────────────────────────────────────────
73
+ await db.schema
74
+ .createTable("workflow_steps")
75
+ .addColumn("id", "uuid", (c) =>
76
+ c.primaryKey().defaultTo(sql`gen_random_uuid()`),
77
+ )
78
+ .addColumn("run_id", "uuid", (c) =>
79
+ c.notNull().references("workflow_runs.id").onDelete("cascade"),
80
+ )
81
+ .addColumn("step_index", "integer", (c) => c.notNull())
82
+ .addColumn("step_id", "text", (c) => c.notNull())
83
+ .addColumn("step_type", "text", (c) => c.notNull())
84
+ .addColumn("status", "text", (c) => c.notNull().defaultTo("pending"))
85
+ .addColumn("input", "jsonb")
86
+ .addColumn("output", "jsonb")
87
+ .addColumn("error", "text")
88
+ .addColumn("retry_count", "integer", (c) => c.notNull().defaultTo(0))
89
+ .addColumn("ai_tokens_used", "integer", (c) => c.notNull().defaultTo(0))
90
+ .addColumn("started_at", "timestamptz")
91
+ .addColumn("completed_at", "timestamptz")
92
+ .addColumn("duration_ms", "integer")
93
+ .addColumn("created_at", "timestamptz", (c) =>
94
+ c.notNull().defaultTo(sql`now()`),
95
+ )
96
+ .execute();
97
+
98
+ await sql`CREATE INDEX workflow_steps_run_idx ON workflow_steps (run_id, step_index)`.execute(
99
+ db,
100
+ );
101
+ await sql`CREATE UNIQUE INDEX workflow_steps_dedup_idx ON workflow_steps (run_id, step_id)`.execute(
102
+ db,
103
+ );
104
+
105
+ // ── workflow_queue ────────────────────────────────────────────────
106
+ await db.schema
107
+ .createTable("workflow_queue")
108
+ .addColumn("id", "uuid", (c) =>
109
+ c.primaryKey().defaultTo(sql`gen_random_uuid()`),
110
+ )
111
+ .addColumn("run_id", "uuid", (c) =>
112
+ c.notNull().references("workflow_runs.id").onDelete("cascade"),
113
+ )
114
+ .addColumn("status", "text", (c) => c.notNull().defaultTo("pending"))
115
+ .addColumn("attempts", "integer", (c) => c.notNull().defaultTo(0))
116
+ .addColumn("max_attempts", "integer", (c) => c.notNull().defaultTo(3))
117
+ .addColumn("scheduled_for", "timestamptz", (c) =>
118
+ c.notNull().defaultTo(sql`now()`),
119
+ )
120
+ .addColumn("locked_at", "timestamptz")
121
+ .addColumn("locked_by", "text")
122
+ .addColumn("error", "text")
123
+ .addColumn("created_at", "timestamptz", (c) =>
124
+ c.notNull().defaultTo(sql`now()`),
125
+ )
126
+ .addColumn("completed_at", "timestamptz")
127
+ .execute();
128
+
129
+ await sql`CREATE INDEX workflow_queue_poll_idx ON workflow_queue (scheduled_for, status, locked_at)`.execute(
130
+ db,
131
+ );
132
+
133
+ // ── workflow_schedules ────────────────────────────────────────────
134
+ await db.schema
135
+ .createTable("workflow_schedules")
136
+ .addColumn("id", "uuid", (c) =>
137
+ c.primaryKey().defaultTo(sql`gen_random_uuid()`),
138
+ )
139
+ .addColumn("definition_id", "uuid", (c) =>
140
+ c
141
+ .notNull()
142
+ .references("workflow_definitions.id")
143
+ .onDelete("cascade"),
144
+ )
145
+ .addColumn("cron_expr", "text", (c) => c.notNull())
146
+ .addColumn("timezone", "text", (c) => c.notNull().defaultTo("UTC"))
147
+ .addColumn("next_run_at", "timestamptz", (c) => c.notNull())
148
+ .addColumn("last_run_at", "timestamptz")
149
+ .addColumn("enabled", "boolean", (c) => c.notNull().defaultTo(true))
150
+ .addColumn("created_at", "timestamptz", (c) =>
151
+ c.notNull().defaultTo(sql`now()`),
152
+ )
153
+ .execute();
154
+
155
+ await sql`CREATE UNIQUE INDEX workflow_schedules_definition_idx ON workflow_schedules (definition_id)`.execute(
156
+ db,
157
+ );
158
+ await sql`CREATE INDEX workflow_schedules_poll_idx ON workflow_schedules (enabled, next_run_at)`.execute(
159
+ db,
160
+ );
161
+
162
+ // ── PG NOTIFY trigger on workflow_queue ───────────────────────────
163
+ await sql`
164
+ CREATE OR REPLACE FUNCTION notify_workflow_job() RETURNS trigger AS $$
165
+ BEGIN
166
+ PERFORM pg_notify('workflows:new_job', NEW.run_id::text);
167
+ RETURN NEW;
168
+ END;
169
+ $$ LANGUAGE plpgsql
170
+ `.execute(db);
171
+
172
+ await sql`
173
+ CREATE TRIGGER workflow_queue_notify
174
+ AFTER INSERT ON workflow_queue
175
+ FOR EACH ROW EXECUTE FUNCTION notify_workflow_job()
176
+ `.execute(db);
177
+ }
178
+
179
+ export async function down(db: Kysely<any>): Promise<void> {
180
+ await sql`DROP TRIGGER IF EXISTS workflow_queue_notify ON workflow_queue`.execute(
181
+ db,
182
+ );
183
+ await sql`DROP FUNCTION IF EXISTS notify_workflow_job`.execute(db);
184
+
185
+ await sql`DROP INDEX IF EXISTS workflow_schedules_poll_idx`.execute(db);
186
+ await sql`DROP INDEX IF EXISTS workflow_schedules_definition_idx`.execute(db);
187
+ await db.schema.dropTable("workflow_schedules").execute();
188
+
189
+ await sql`DROP INDEX IF EXISTS workflow_queue_poll_idx`.execute(db);
190
+ await db.schema.dropTable("workflow_queue").execute();
191
+
192
+ await sql`DROP INDEX IF EXISTS workflow_steps_dedup_idx`.execute(db);
193
+ await sql`DROP INDEX IF EXISTS workflow_steps_run_idx`.execute(db);
194
+ await db.schema.dropTable("workflow_steps").execute();
195
+
196
+ await sql`DROP INDEX IF EXISTS workflow_runs_dedup_idx`.execute(db);
197
+ await sql`DROP INDEX IF EXISTS workflow_runs_status_idx`.execute(db);
198
+ await sql`DROP INDEX IF EXISTS workflow_runs_definition_idx`.execute(db);
199
+ await db.schema.dropTable("workflow_runs").execute();
200
+
201
+ await sql`DROP INDEX IF EXISTS workflow_definitions_status_idx`.execute(db);
202
+ await sql`DROP INDEX IF EXISTS workflow_definitions_name_key_idx`.execute(db);
203
+ await db.schema.dropTable("workflow_definitions").execute();
204
+ }
@@ -0,0 +1,16 @@
1
+ import { sql, type Kysely } from "kysely";
2
+
3
+ export async function up(db: Kysely<any>): Promise<void> {
4
+ await db.schema
5
+ .createTable("workflow_cursors")
6
+ .addColumn("name", "text", (c) => c.primaryKey())
7
+ .addColumn("block_height", "integer", (c) => c.notNull().defaultTo(0))
8
+ .addColumn("updated_at", "timestamptz", (c) =>
9
+ c.notNull().defaultTo(sql`now()`),
10
+ )
11
+ .execute();
12
+ }
13
+
14
+ export async function down(db: Kysely<any>): Promise<void> {
15
+ await db.schema.dropTable("workflow_cursors").execute();
16
+ }
@@ -0,0 +1,116 @@
1
+ import { sql, type Kysely } from "kysely";
2
+
3
+ /**
4
+ * Account-wide subgraph scoping.
5
+ *
6
+ * Changes:
7
+ * - Add `account_id` column to `subgraphs`, backfilled from `api_keys.account_id`
8
+ * - Swap unique index from (name, api_key_id) → (name, account_id)
9
+ * - Make api_key_id FK nullable (ON DELETE SET NULL) for audit trail
10
+ * - Rename existing PG schemas from key-prefix to account-prefix
11
+ * - Update schema_name column to match
12
+ *
13
+ * After this migration, any API key on the same account can deploy/update
14
+ * the same named subgraph without creating duplicates.
15
+ */
16
+ export async function up(db: Kysely<any>): Promise<void> {
17
+ // 1. Add account_id column (nullable first so we can backfill)
18
+ await db.schema
19
+ .alterTable("subgraphs")
20
+ .addColumn("account_id", "text")
21
+ .execute();
22
+
23
+ // 2. Backfill account_id from api_keys
24
+ await sql`
25
+ UPDATE subgraphs s
26
+ SET account_id = k.account_id
27
+ FROM api_keys k
28
+ WHERE k.id = s.api_key_id
29
+ `.execute(db);
30
+
31
+ // 3. Set NOT NULL after backfill (default '' for any orphaned rows)
32
+ await sql`
33
+ UPDATE subgraphs SET account_id = '' WHERE account_id IS NULL
34
+ `.execute(db);
35
+
36
+ await db.schema
37
+ .alterTable("subgraphs")
38
+ .alterColumn("account_id", (c) => c.setNotNull())
39
+ .execute();
40
+
41
+ // 4. Drop old unique index on (name, api_key_id)
42
+ await db.schema
43
+ .dropIndex("subgraphs_name_api_key_id_unique")
44
+ .ifExists()
45
+ .execute();
46
+
47
+ // 5. Create new unique index on (name, account_id)
48
+ await db.schema
49
+ .createIndex("subgraphs_name_account_id_unique")
50
+ .unique()
51
+ .on("subgraphs")
52
+ .columns(["name", "account_id"])
53
+ .execute();
54
+
55
+ // 6. Add index on account_id for fast lookups
56
+ await db.schema
57
+ .createIndex("subgraphs_account_id_idx")
58
+ .on("subgraphs")
59
+ .column("account_id")
60
+ .execute();
61
+
62
+ // 7. Make api_key_id nullable (keep for audit, but allow key deletion)
63
+ await db.schema
64
+ .alterTable("subgraphs")
65
+ .alterColumn("api_key_id", (c) => c.dropNotNull())
66
+ .execute();
67
+
68
+ // 8. Rename existing PG schemas from key-prefix to account-prefix
69
+ // and update schema_name column accordingly.
70
+ // New format: subgraph_{first8charsOfAccountId}_{name}
71
+ const rows = await sql<{
72
+ id: string;
73
+ name: string;
74
+ schema_name: string | null;
75
+ account_id: string;
76
+ }>`
77
+ SELECT id, name, schema_name, account_id FROM subgraphs
78
+ WHERE schema_name IS NOT NULL
79
+ `.execute(db);
80
+
81
+ for (const row of rows.rows) {
82
+ const oldSchema = row.schema_name!;
83
+ const accountPrefix = row.account_id.slice(0, 8).replace(/-/g, "_");
84
+ const safeName = row.name.replace(/-/g, "_");
85
+ const newSchema = `subgraph_${accountPrefix}_${safeName}`;
86
+
87
+ if (oldSchema === newSchema) continue;
88
+
89
+ // Check if old schema exists before renaming
90
+ const exists = await sql<{ exists: boolean }>`
91
+ SELECT EXISTS(
92
+ SELECT 1 FROM information_schema.schemata
93
+ WHERE schema_name = ${oldSchema}
94
+ ) AS exists
95
+ `.execute(db);
96
+
97
+ if (exists.rows[0]?.exists) {
98
+ await sql`ALTER SCHEMA ${sql.raw(`"${oldSchema}"`)} RENAME TO ${sql.raw(`"${newSchema}"`)}`.execute(db);
99
+ }
100
+
101
+ // Update schema_name column regardless of whether schema existed
102
+ await sql`
103
+ UPDATE subgraphs SET schema_name = ${newSchema} WHERE id = ${row.id}
104
+ `.execute(db);
105
+ }
106
+ }
107
+
108
+ export async function down(db: Kysely<any>): Promise<void> {
109
+ await db.schema.dropIndex("subgraphs_account_id_idx").ifExists().execute();
110
+ await db.schema
111
+ .dropIndex("subgraphs_name_account_id_unique")
112
+ .ifExists()
113
+ .execute();
114
+ await db.schema.alterTable("subgraphs").dropColumn("account_id").execute();
115
+ // Note: unique index on (name, api_key_id) is not restored — use a subsequent migration if needed
116
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@secondlayer/shared",
3
- "version": "0.10.1",
3
+ "version": "0.11.0",
4
4
  "type": "module",
5
5
  "main": "./dist/src/index.js",
6
6
  "types": "./dist/src/index.d.ts",
@@ -73,6 +73,26 @@
73
73
  "types": "./dist/src/schemas/subgraphs.d.ts",
74
74
  "import": "./dist/src/schemas/subgraphs.js"
75
75
  },
76
+ "./schemas/marketplace": {
77
+ "types": "./dist/src/schemas/marketplace.d.ts",
78
+ "import": "./dist/src/schemas/marketplace.js"
79
+ },
80
+ "./schemas/workflows": {
81
+ "types": "./dist/src/schemas/workflows.d.ts",
82
+ "import": "./dist/src/schemas/workflows.js"
83
+ },
84
+ "./db/queries/workflows": {
85
+ "types": "./dist/src/db/queries/workflows.d.ts",
86
+ "import": "./dist/src/db/queries/workflows.js"
87
+ },
88
+ "./db/queries/marketplace": {
89
+ "types": "./dist/src/db/queries/marketplace.d.ts",
90
+ "import": "./dist/src/db/queries/marketplace.js"
91
+ },
92
+ "./db/queries/projects": {
93
+ "types": "./dist/src/db/queries/projects.d.ts",
94
+ "import": "./dist/src/db/queries/projects.js"
95
+ },
76
96
  "./types": {
77
97
  "types": "./dist/src/types.d.ts",
78
98
  "import": "./dist/src/types.js"
@@ -137,7 +157,7 @@
137
157
  },
138
158
  "dependencies": {
139
159
  "@secondlayer/stacks": "^0.2.2",
140
- "kysely": "0.28.10",
160
+ "kysely": "0.28.15",
141
161
  "kysely-postgres-js": "3.0.0",
142
162
  "postgres": "^3.4.6",
143
163
  "zod": "^4.3.6"