@sentry/junior 0.203.0 → 0.204.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 (174) hide show
  1. package/dist/agent-CMyTD4DD.js +17509 -0
  2. package/dist/agent-hooks-Bbcp9e33.js +4040 -0
  3. package/dist/agent-runner-DNb1A49M.js +678 -0
  4. package/dist/api/schema.d.ts +2728 -2639
  5. package/dist/api/schema.js +4 -169
  6. package/dist/api.d.ts +23 -17
  7. package/dist/api.js +3881 -5326
  8. package/dist/app-B1TOqBI8.d.ts +246 -0
  9. package/dist/app.d.ts +4 -520
  10. package/dist/app.js +9522 -11875
  11. package/dist/auth-pause-state-DosEalIG.js +168 -0
  12. package/dist/catalog-runtime-BReos-D4.js +7 -0
  13. package/dist/checkpoint-CNrPyRJN.js +2450 -0
  14. package/dist/cli/chat.d.ts +20 -0
  15. package/dist/cli/chat.js +229 -336
  16. package/dist/cli/check.d.ts +8 -0
  17. package/dist/cli/check.js +513 -806
  18. package/dist/cli/env.d.ts +13 -0
  19. package/dist/cli/env.js +63 -12
  20. package/dist/cli/init.d.ts +3 -0
  21. package/dist/cli/init.js +90 -159
  22. package/dist/cli/main.d.ts +6 -0
  23. package/dist/cli/main.js +43 -64
  24. package/dist/cli/plugins.d.ts +13 -0
  25. package/dist/cli/plugins.js +148 -227
  26. package/dist/cli/run.d.ts +16 -0
  27. package/dist/cli/run.js +52 -8
  28. package/dist/cli/snapshot-create.d.ts +4 -0
  29. package/dist/cli/snapshot-create.js +71 -125
  30. package/dist/cli/upgrade.d.ts +15 -0
  31. package/dist/cli/upgrade.js +232 -335
  32. package/dist/config-nGCW1OTG.js +941 -0
  33. package/dist/context-B45_WqVX.js +298 -0
  34. package/dist/conversation-privacy-B4voFRSm.js +182 -0
  35. package/dist/conversation-work-C6y3T5Hx.js +51 -0
  36. package/dist/credential-sync-kXr1hvO1.js +2660 -0
  37. package/dist/db-BeyXKJOI.d.ts +5 -0
  38. package/dist/destinations-BeBiJeEM.js +29 -0
  39. package/dist/execution-stats-DWfSihcH.js +373 -0
  40. package/dist/executor-DcMSlI5c.js +721 -0
  41. package/dist/experimental-CIXf7Uye.js +46 -0
  42. package/dist/history-ClE4toxe.d.ts +32 -0
  43. package/dist/history-CqTuSAdI.js +441 -0
  44. package/dist/instrumentation.d.ts +3 -3
  45. package/dist/instrumentation.js +130 -176
  46. package/dist/job-queue-Drn_LXlM.js +63 -0
  47. package/dist/logging-BzAk1S_s.js +1210 -0
  48. package/dist/model-profile-CZmVoGdX.js +49 -0
  49. package/dist/nitro.d.ts +31 -36
  50. package/dist/nitro.js +275 -456
  51. package/dist/oauth-callback-server-JiftokvA.js +130 -0
  52. package/dist/oauth-relay-CdCnu80W.js +62 -0
  53. package/dist/package-discovery-7a9YqLhE.js +455 -0
  54. package/dist/pending-auth-CWmSdZVg.js +185 -0
  55. package/dist/plugin-module-BKGOXca2.js +81 -0
  56. package/dist/plugins-BI9A1vKA.d.ts +95 -0
  57. package/dist/plugins-egKWjgue.js +99 -0
  58. package/dist/private-trace-result-BLvBjy_e.js +16 -0
  59. package/dist/profile-CrT5nXbw.js +861 -0
  60. package/dist/registry-DH1xUo2N.js +1419 -0
  61. package/dist/reporting-schema-DosTN__Q.js +105 -0
  62. package/dist/resolve-CZHnfxaS.js +546 -0
  63. package/dist/rolldown-runtime-B4FMCO8f.js +28 -0
  64. package/dist/runner-CZV2sC46.js +392 -0
  65. package/dist/sandbox-egress-signals-bHZfmhbd.js +46 -0
  66. package/dist/schema-Rn8-XEaN.js +1140 -0
  67. package/dist/sentry-CD3SdJl-.js +26 -0
  68. package/dist/skills-CIcDQSgM.js +316 -0
  69. package/dist/spawn-DdkUFRmq.js +29 -0
  70. package/dist/stats-DTUbAb88.js +2634 -0
  71. package/dist/store-C5qxF2ac.d.ts +208 -0
  72. package/dist/task-queue-CJIR4RF7.js +136 -0
  73. package/dist/task-runner-D-O3A-Cp.js +639 -0
  74. package/dist/thread-state-CRc9jTJS.js +431 -0
  75. package/dist/turn-execution-BkinIdJF.js +47 -0
  76. package/dist/turn-lifecycle-DQV19VTW.js +54 -0
  77. package/dist/usage-D3EbSO7w.js +296 -0
  78. package/dist/usage-schema-CKaDkH_j.js +20 -0
  79. package/dist/validation-C2CR-OX3.js +40 -0
  80. package/dist/vercel-queue-DIkVcfK4.js +167 -0
  81. package/dist/vercel.d.ts +6 -6
  82. package/dist/vercel.js +14 -6
  83. package/dist/version-CnXZ614E.d.ts +5 -0
  84. package/dist/version-DxcWdoKa.js +8 -0
  85. package/dist/version.d.ts +2 -4
  86. package/dist/version.js +2 -6
  87. package/dist/web-authorization-RMBxAR_t.js +76 -0
  88. package/dist/web-input-3EPfpf55.js +494 -0
  89. package/dist/work-fGofI7Op.js +509 -0
  90. package/dist/worker-CsOLq-mR.js +625 -0
  91. package/package.json +8 -8
  92. package/dist/agent-hooks-GB2VPTNA.js +0 -51
  93. package/dist/catalog-runtime-UBLIWD3O.js +0 -14
  94. package/dist/chunk-2LYHWMR4.js +0 -46
  95. package/dist/chunk-352OWAZH.js +0 -320
  96. package/dist/chunk-3Y47VMHV.js +0 -6
  97. package/dist/chunk-3YJSKOWF.js +0 -164
  98. package/dist/chunk-4KSSKTQ5.js +0 -229
  99. package/dist/chunk-4UO6FK4G.js +0 -64
  100. package/dist/chunk-5AHX53NQ.js +0 -717
  101. package/dist/chunk-5J3E5FWA.js +0 -700
  102. package/dist/chunk-5PDAG6O3.js +0 -206
  103. package/dist/chunk-6Y5B5PEX.js +0 -80
  104. package/dist/chunk-7GWQ5S5R.js +0 -2086
  105. package/dist/chunk-7H2VZTWN.js +0 -103
  106. package/dist/chunk-7NL7T6VU.js +0 -406
  107. package/dist/chunk-A5L27LE4.js +0 -387
  108. package/dist/chunk-ADIAWL34.js +0 -81
  109. package/dist/chunk-ASTEMBKJ.js +0 -519
  110. package/dist/chunk-CEMMLV5L.js +0 -98
  111. package/dist/chunk-CM5HNV2W.js +0 -131
  112. package/dist/chunk-CRTRBBRI.js +0 -105
  113. package/dist/chunk-DA2KGZZI.js +0 -13
  114. package/dist/chunk-DIB4GX3G.js +0 -8
  115. package/dist/chunk-DKIJ2RP5.js +0 -68
  116. package/dist/chunk-DWPN6SCH.js +0 -172
  117. package/dist/chunk-EDKUFGPL.js +0 -64
  118. package/dist/chunk-ENY6SKIU.js +0 -1228
  119. package/dist/chunk-F4MGCZOX.js +0 -2943
  120. package/dist/chunk-FA6POGLH.js +0 -70
  121. package/dist/chunk-FSUR7GTG.js +0 -20475
  122. package/dist/chunk-GKEOHJL5.js +0 -1856
  123. package/dist/chunk-GMCB6W5Y.js +0 -333
  124. package/dist/chunk-IE2VPG7O.js +0 -1135
  125. package/dist/chunk-IZ5B5IHQ.js +0 -3656
  126. package/dist/chunk-JAL3CH4G.js +0 -27
  127. package/dist/chunk-KIIEUXD3.js +0 -1764
  128. package/dist/chunk-KXU4NIL7.js +0 -692
  129. package/dist/chunk-MTM5IFCY.js +0 -1190
  130. package/dist/chunk-N7TMSOZH.js +0 -235
  131. package/dist/chunk-NCEBK6PH.js +0 -36
  132. package/dist/chunk-NHJU2ZAL.js +0 -56
  133. package/dist/chunk-P6KTRNVC.js +0 -321
  134. package/dist/chunk-PDO5BLNM.js +0 -22
  135. package/dist/chunk-PDRNLLFT.js +0 -3295
  136. package/dist/chunk-PEQXYSSE.js +0 -45
  137. package/dist/chunk-PKEX2AQX.js +0 -198
  138. package/dist/chunk-PSJCGIGO.js +0 -1163
  139. package/dist/chunk-PZUSUL55.js +0 -506
  140. package/dist/chunk-R45M4TUS.js +0 -37
  141. package/dist/chunk-RC745GGZ.js +0 -122
  142. package/dist/chunk-SP3QKZXJ.js +0 -820
  143. package/dist/chunk-TXKUKMVO.js +0 -417
  144. package/dist/chunk-UE4GPWSD.js +0 -931
  145. package/dist/chunk-UIE3R5XU.js +0 -17
  146. package/dist/chunk-UXPG6ZIN.js +0 -84
  147. package/dist/chunk-VFUK3X5B.js +0 -50
  148. package/dist/chunk-VNMJWREK.js +0 -787
  149. package/dist/chunk-VZ7CSOX2.js +0 -706
  150. package/dist/chunk-WCNQJKFT.js +0 -10
  151. package/dist/chunk-WDZR3QQM.js +0 -135
  152. package/dist/chunk-WLYVJCHV.js +0 -1433
  153. package/dist/chunk-WS7NAFTM.js +0 -1520
  154. package/dist/chunk-X6GDDWQH.js +0 -60
  155. package/dist/chunk-XPDEYNQI.js +0 -102
  156. package/dist/chunk-XRW44VGM.js +0 -58
  157. package/dist/chunk-Y2CM7HXH.js +0 -111
  158. package/dist/chunk-Y7X25LFY.js +0 -15
  159. package/dist/conversation-work-VIOQ5ZF3.js +0 -54
  160. package/dist/db-NGDJV7YH.js +0 -26
  161. package/dist/discovery-DWVDM7UK.js +0 -44
  162. package/dist/experimental-P5FZM4CU.js +0 -12
  163. package/dist/oauth-callback-server-YFY7YEGK.js +0 -243
  164. package/dist/oauth-relay-5XPVRPYB.js +0 -8
  165. package/dist/plugins-YTYJMMJF.js +0 -14
  166. package/dist/runner-ORTNSB2Q.js +0 -522
  167. package/dist/sandbox-egress-signals-FIL7EGPR.js +0 -73
  168. package/dist/sentry-MB3NL3IR.js +0 -42
  169. package/dist/skills-H376WQOH.js +0 -27
  170. package/dist/spawn-T43WHLOS.js +0 -42
  171. package/dist/task-runner-LBZWF32V.js +0 -36
  172. package/dist/validation-REJLRKI6.js +0 -17
  173. package/dist/work-3KJ3RE76.js +0 -51
  174. package/dist/worker-QFEBU72E.js +0 -24
@@ -1,69 +1,46 @@
1
- import {
2
- defineJuniorPlugins,
3
- pluginCatalogConfigFromEnv,
4
- pluginCatalogConfigFromPluginSet
5
- } from "../chunk-CM5HNV2W.js";
6
- import {
7
- loadAppPluginSet
8
- } from "../chunk-Y2CM7HXH.js";
9
- import {
10
- createJuniorSqlExecutor
11
- } from "../chunk-IE2VPG7O.js";
12
- import "../chunk-VFUK3X5B.js";
13
- import {
14
- getChatConfig
15
- } from "../chunk-ENY6SKIU.js";
16
- import "../chunk-NHJU2ZAL.js";
17
- import "../chunk-PKEX2AQX.js";
18
- import {
19
- createPluginCatalogRuntime
20
- } from "../chunk-7GWQ5S5R.js";
21
- import "../chunk-CEMMLV5L.js";
22
- import "../chunk-N7TMSOZH.js";
23
- import "../chunk-DWPN6SCH.js";
24
- import "../chunk-TXKUKMVO.js";
25
- import "../chunk-WS7NAFTM.js";
26
- import "../chunk-PEQXYSSE.js";
27
- import "../chunk-WDZR3QQM.js";
28
-
29
- // src/chat/conversations/sql/migrations.ts
30
- import { basename, dirname, join, resolve } from "path";
31
- import { fileURLToPath } from "url";
1
+ import { i as getChatConfig } from "../config-nGCW1OTG.js";
2
+ import { t as createJuniorSqlExecutor } from "../executor-DcMSlI5c.js";
3
+ import { t as createPluginCatalogRuntime } from "../registry-DH1xUo2N.js";
4
+ import { n as pluginCatalogConfigFromEnv, r as pluginCatalogConfigFromPluginSet, t as defineJuniorPlugins } from "../plugins-egKWjgue.js";
5
+ import { t as loadAppPluginSet } from "../plugin-module-BKGOXca2.js";
6
+ import { fileURLToPath } from "node:url";
7
+ import { basename, dirname, join, resolve } from "node:path";
8
+ import { createHash } from "node:crypto";
9
+ import { createJiti } from "jiti";
32
10
  import { readMigrationFiles } from "drizzle-orm/migrator";
33
-
34
- // src/db/postgres-error.ts
11
+ //#region src/db/postgres-error.ts
12
+ /** Return whether an unknown PostgreSQL failure has the requested SQLSTATE. */
35
13
  function isPostgresErrorCode(error, code) {
36
- return typeof error === "object" && error !== null && "code" in error && error.code === code;
14
+ return typeof error === "object" && error !== null && "code" in error && error.code === code;
37
15
  }
38
-
39
- // src/chat/conversations/sql/migrations.ts
40
- var CORE_MIGRATION_BRIDGE_VERSION = "0.107.1";
41
- var MIGRATIONS_TABLE = "__drizzle_junior_core";
16
+ //#endregion
17
+ //#region src/chat/conversations/sql/migrations.ts
18
+ /** SQL schema migrations for durable Junior records. */
19
+ const CORE_MIGRATION_BRIDGE_VERSION = "0.107.1";
20
+ const MIGRATIONS_TABLE = "__drizzle_junior_core";
21
+ /** Resolve the packaged Drizzle migration directory in source or built output. */
42
22
  function migrationFolder() {
43
- const moduleDir = dirname(fileURLToPath(import.meta.url));
44
- const packageRoot = basename(moduleDir) === "dist" ? dirname(moduleDir) : basename(dirname(moduleDir)) === "dist" ? resolve(moduleDir, "../..") : resolve(moduleDir, "../../../..");
45
- return join(packageRoot, "migrations");
23
+ const moduleDir = dirname(fileURLToPath(import.meta.url));
24
+ const packageRoot = basename(moduleDir) === "dist" ? dirname(moduleDir) : basename(dirname(moduleDir)) === "dist" ? resolve(moduleDir, "../..") : resolve(moduleDir, "../../../..");
25
+ return join(packageRoot, "migrations");
46
26
  }
27
+ /** Read core journal progress and detect existing pre-journal Junior tables. */
47
28
  async function loadCoreMigrationState(executor) {
48
- try {
49
- const [migration] = await executor.query(`
29
+ try {
30
+ const [migration] = await executor.query(`
50
31
  SELECT created_at::text AS "appliedAt"
51
32
  FROM drizzle.__drizzle_junior_core
52
33
  ORDER BY created_at DESC
53
34
  LIMIT 1
54
35
  `);
55
- if (migration?.appliedAt !== null && migration?.appliedAt !== void 0) {
56
- return {
57
- appliedAt: Number(migration.appliedAt),
58
- hasJuniorTables: true
59
- };
60
- }
61
- } catch (error) {
62
- if (!isPostgresErrorCode(error, "42P01")) {
63
- throw error;
64
- }
65
- }
66
- const [tables] = await executor.query(`
36
+ if (migration?.appliedAt !== null && migration?.appliedAt !== void 0) return {
37
+ appliedAt: Number(migration.appliedAt),
38
+ hasJuniorTables: true
39
+ };
40
+ } catch (error) {
41
+ if (!isPostgresErrorCode(error, "42P01")) throw error;
42
+ }
43
+ const [tables] = await executor.query(`
67
44
  SELECT EXISTS (
68
45
  SELECT 1
69
46
  FROM information_schema.tables
@@ -71,346 +48,266 @@ SELECT EXISTS (
71
48
  AND table_name LIKE 'junior\\_%' ESCAPE '\\'
72
49
  ) AS "hasJuniorTables"
73
50
  `);
74
- return {
75
- hasJuniorTables: tables?.hasJuniorTables ?? false
76
- };
51
+ return { hasJuniorTables: tables?.hasJuniorTables ?? false };
77
52
  }
78
53
  function isCurrentMigrationState(state, latestMigrationAt) {
79
- return state.appliedAt !== void 0 && state.appliedAt >= latestMigrationAt;
54
+ return state.appliedAt !== void 0 && state.appliedAt >= latestMigrationAt;
80
55
  }
81
56
  function migrationResult(migrations, state) {
82
- const appliedAt = state.appliedAt;
83
- const existing = appliedAt === void 0 ? 0 : migrations.filter((migration) => migration.folderMillis <= appliedAt).length;
84
- return {
85
- existing,
86
- migrated: migrations.length - existing,
87
- scanned: migrations.length
88
- };
57
+ const appliedAt = state.appliedAt;
58
+ const existing = appliedAt === void 0 ? 0 : migrations.filter((migration) => migration.folderMillis <= appliedAt).length;
59
+ return {
60
+ existing,
61
+ migrated: migrations.length - existing,
62
+ scanned: migrations.length
63
+ };
89
64
  }
65
+ /** Reject legacy databases that must first run the bridge release upgrade. */
90
66
  function assertSupportedMigrationState(state) {
91
- if (!state.hasJuniorTables || state.appliedAt !== void 0) {
92
- return;
93
- }
94
- throw new Error(
95
- `Existing Junior SQL tables have no core Drizzle migration history. Stop old Junior workers, install @sentry/junior@${CORE_MIGRATION_BRIDGE_VERSION}, run \`junior upgrade\`, then restore this Junior version and rerun \`junior upgrade\` before restarting workers.`
96
- );
67
+ if (!state.hasJuniorTables || state.appliedAt !== void 0) return;
68
+ throw new Error(`Existing Junior SQL tables have no core Drizzle migration history. Stop old Junior workers, install @sentry/junior@${CORE_MIGRATION_BRIDGE_VERSION}, run \`junior upgrade\`, then restore this Junior version and rerun \`junior upgrade\` before restarting workers.`);
97
69
  }
70
+ /** Apply the packaged Drizzle migrations during `junior upgrade`. */
98
71
  async function migrateSchema(executor) {
99
- const migrationsFolder = migrationFolder();
100
- const migrations = readMigrationFiles({ migrationsFolder });
101
- const latestMigration = migrations.at(-1);
102
- if (!latestMigration) {
103
- throw new Error("No core Drizzle migrations were packaged");
104
- }
105
- const initialState = await loadCoreMigrationState(executor);
106
- if (isCurrentMigrationState(initialState, latestMigration.folderMillis)) {
107
- return migrationResult(migrations, initialState);
108
- }
109
- assertSupportedMigrationState(initialState);
110
- return await executor.withMigrationLock(MIGRATIONS_TABLE, async () => {
111
- const lockedState = await loadCoreMigrationState(executor);
112
- if (isCurrentMigrationState(lockedState, latestMigration.folderMillis)) {
113
- return migrationResult(migrations, lockedState);
114
- }
115
- assertSupportedMigrationState(lockedState);
116
- await executor.migrate({
117
- migrationsFolder,
118
- migrationsTable: MIGRATIONS_TABLE
119
- });
120
- return migrationResult(migrations, lockedState);
121
- });
72
+ const migrationsFolder = migrationFolder();
73
+ const migrations = readMigrationFiles({ migrationsFolder });
74
+ const latestMigration = migrations.at(-1);
75
+ if (!latestMigration) throw new Error("No core Drizzle migrations were packaged");
76
+ const initialState = await loadCoreMigrationState(executor);
77
+ if (isCurrentMigrationState(initialState, latestMigration.folderMillis)) return migrationResult(migrations, initialState);
78
+ assertSupportedMigrationState(initialState);
79
+ return await executor.withMigrationLock(MIGRATIONS_TABLE, async () => {
80
+ const lockedState = await loadCoreMigrationState(executor);
81
+ if (isCurrentMigrationState(lockedState, latestMigration.folderMillis)) return migrationResult(migrations, lockedState);
82
+ assertSupportedMigrationState(lockedState);
83
+ await executor.migrate({
84
+ migrationsFolder,
85
+ migrationsTable: MIGRATIONS_TABLE
86
+ });
87
+ return migrationResult(migrations, lockedState);
88
+ });
122
89
  }
123
-
124
- // src/cli/upgrade.ts
125
- import { createJiti } from "jiti";
126
-
127
- // src/chat/plugins/migrations.ts
128
- import { createHash } from "crypto";
129
- import { readMigrationFiles as readMigrationFiles2 } from "drizzle-orm/migrator";
90
+ //#endregion
91
+ //#region src/chat/plugins/migrations.ts
130
92
  function migrationTable(pluginName) {
131
- const label = pluginName.toLowerCase().replace(/[^a-z0-9]+/g, "_").replace(/^_+|_+$/g, "").slice(0, 32);
132
- const hash = createHash("sha256").update(pluginName).digest("hex").slice(0, 8);
133
- return `__drizzle_${label}_${hash}`;
93
+ return `__drizzle_${pluginName.toLowerCase().replace(/[^a-z0-9]+/g, "_").replace(/^_+|_+$/g, "").slice(0, 32)}_${createHash("sha256").update(pluginName).digest("hex").slice(0, 8)}`;
134
94
  }
135
95
  async function appliedMigrationTime(executor, table) {
136
- try {
137
- const [row] = await executor.query(
138
- `SELECT created_at::text AS "createdAt"
96
+ try {
97
+ const [row] = await executor.query(`SELECT created_at::text AS "createdAt"
139
98
  FROM drizzle.${table}
140
99
  ORDER BY created_at DESC
141
- LIMIT 1`
142
- );
143
- return row?.createdAt === null || row?.createdAt === void 0 ? void 0 : Number(row.createdAt);
144
- } catch (error) {
145
- if (isPostgresErrorCode(error, "42P01")) {
146
- return void 0;
147
- }
148
- throw error;
149
- }
100
+ LIMIT 1`);
101
+ return row?.createdAt === null || row?.createdAt === void 0 ? void 0 : Number(row.createdAt);
102
+ } catch (error) {
103
+ if (isPostgresErrorCode(error, "42P01")) return;
104
+ throw error;
105
+ }
150
106
  }
151
107
  async function legacyMigrationHashes(executor, pluginName) {
152
- const [exists] = await executor.query(
153
- `SELECT to_regclass('public.junior_schema_migrations')::text AS "tableName"`
154
- );
155
- if (!exists?.tableName) {
156
- return /* @__PURE__ */ new Set();
157
- }
158
- const rows = await executor.query(
159
- `SELECT checksum
108
+ const [exists] = await executor.query("SELECT to_regclass('public.junior_schema_migrations')::text AS \"tableName\"");
109
+ if (!exists?.tableName) return /* @__PURE__ */ new Set();
110
+ const rows = await executor.query(`SELECT checksum
160
111
  FROM junior_schema_migrations
161
112
  WHERE id LIKE $1
162
- ORDER BY id ASC`,
163
- [`plugin:${pluginName}/%`]
164
- );
165
- return new Set(rows.map((row) => row.checksum));
113
+ ORDER BY id ASC`, [`plugin:${pluginName}/%`]);
114
+ return new Set(rows.map((row) => row.checksum));
166
115
  }
167
116
  function adoptedMigration(migrations, legacyHashes) {
168
- let adopted;
169
- for (const migration of migrations) {
170
- if (!legacyHashes.has(migration.hash)) {
171
- break;
172
- }
173
- adopted = migration;
174
- }
175
- return adopted;
117
+ let adopted;
118
+ for (const migration of migrations) {
119
+ if (!legacyHashes.has(migration.hash)) break;
120
+ adopted = migration;
121
+ }
122
+ return adopted;
176
123
  }
177
124
  async function adoptLegacyMigrationState(args) {
178
- const legacyHashes = await legacyMigrationHashes(
179
- args.executor,
180
- args.pluginName
181
- );
182
- const migration = adoptedMigration(args.migrations, legacyHashes);
183
- if (!migration) {
184
- return void 0;
185
- }
186
- await args.executor.transaction(async () => {
187
- await args.executor.execute("CREATE SCHEMA IF NOT EXISTS drizzle");
188
- await args.executor.execute(`
125
+ const legacyHashes = await legacyMigrationHashes(args.executor, args.pluginName);
126
+ const migration = adoptedMigration(args.migrations, legacyHashes);
127
+ if (!migration) return;
128
+ await args.executor.transaction(async () => {
129
+ await args.executor.execute("CREATE SCHEMA IF NOT EXISTS drizzle");
130
+ await args.executor.execute(`
189
131
  CREATE TABLE IF NOT EXISTS drizzle.${args.table} (
190
132
  id SERIAL PRIMARY KEY,
191
133
  hash TEXT NOT NULL,
192
134
  created_at BIGINT
193
135
  )
194
136
  `);
195
- await args.executor.execute(
196
- `INSERT INTO drizzle.${args.table} (hash, created_at) VALUES ($1, $2)`,
197
- [migration.hash, migration.folderMillis]
198
- );
199
- });
200
- return migration.folderMillis;
137
+ await args.executor.execute(`INSERT INTO drizzle.${args.table} (hash, created_at) VALUES ($1, $2)`, [migration.hash, migration.folderMillis]);
138
+ });
139
+ return migration.folderMillis;
201
140
  }
202
141
  function appliedCount(migrations, createdAt) {
203
- return createdAt === void 0 ? 0 : migrations.filter((migration) => migration.folderMillis <= createdAt).length;
142
+ return createdAt === void 0 ? 0 : migrations.filter((migration) => migration.folderMillis <= createdAt).length;
204
143
  }
144
+ /** Apply enabled plugins' packaged Drizzle migrations in plugin-name order. */
205
145
  async function migratePluginSchemas(executor, roots, options = {}) {
206
- const result = {
207
- existing: 0,
208
- migrated: 0,
209
- scanned: 0
210
- };
211
- const orderedRoots = [...roots].sort(
212
- (left, right) => left.pluginName.localeCompare(right.pluginName)
213
- );
214
- for (const root of orderedRoots) {
215
- const migrations = readMigrationFiles2({ migrationsFolder: root.dir });
216
- if (migrations.length === 0) {
217
- continue;
218
- }
219
- const table = migrationTable(root.pluginName);
220
- const initialTime = await appliedMigrationTime(executor, table);
221
- const initialExisting = appliedCount(migrations, initialTime);
222
- if (initialExisting === migrations.length) {
223
- const summary2 = {
224
- existing: initialExisting,
225
- migrated: 0,
226
- pluginName: root.pluginName,
227
- scanned: migrations.length
228
- };
229
- result.scanned += summary2.scanned;
230
- result.existing += summary2.existing;
231
- options.onPluginMigration?.(summary2);
232
- continue;
233
- }
234
- const summary = await executor.withMigrationLock(table, async () => {
235
- const currentTime = await appliedMigrationTime(executor, table) ?? await adoptLegacyMigrationState({
236
- executor,
237
- migrations,
238
- pluginName: root.pluginName,
239
- table
240
- });
241
- const existing = appliedCount(migrations, currentTime);
242
- if (existing < migrations.length) {
243
- await executor.migrate({
244
- migrationsFolder: root.dir,
245
- migrationsTable: table
246
- });
247
- }
248
- return {
249
- existing,
250
- migrated: migrations.length - existing,
251
- pluginName: root.pluginName,
252
- scanned: migrations.length
253
- };
254
- });
255
- result.scanned += summary.scanned;
256
- result.existing += summary.existing;
257
- result.migrated += summary.migrated;
258
- options.onPluginMigration?.(summary);
259
- }
260
- return result;
146
+ const result = {
147
+ existing: 0,
148
+ migrated: 0,
149
+ scanned: 0
150
+ };
151
+ const orderedRoots = [...roots].sort((left, right) => left.pluginName.localeCompare(right.pluginName));
152
+ for (const root of orderedRoots) {
153
+ const migrations = readMigrationFiles({ migrationsFolder: root.dir });
154
+ if (migrations.length === 0) continue;
155
+ const table = migrationTable(root.pluginName);
156
+ const initialExisting = appliedCount(migrations, await appliedMigrationTime(executor, table));
157
+ if (initialExisting === migrations.length) {
158
+ const summary = {
159
+ existing: initialExisting,
160
+ migrated: 0,
161
+ pluginName: root.pluginName,
162
+ scanned: migrations.length
163
+ };
164
+ result.scanned += summary.scanned;
165
+ result.existing += summary.existing;
166
+ options.onPluginMigration?.(summary);
167
+ continue;
168
+ }
169
+ const summary = await executor.withMigrationLock(table, async () => {
170
+ const currentTime = await appliedMigrationTime(executor, table) ?? await adoptLegacyMigrationState({
171
+ executor,
172
+ migrations,
173
+ pluginName: root.pluginName,
174
+ table
175
+ });
176
+ const existing = appliedCount(migrations, currentTime);
177
+ if (existing < migrations.length) await executor.migrate({
178
+ migrationsFolder: root.dir,
179
+ migrationsTable: table
180
+ });
181
+ return {
182
+ existing,
183
+ migrated: migrations.length - existing,
184
+ pluginName: root.pluginName,
185
+ scanned: migrations.length
186
+ };
187
+ });
188
+ result.scanned += summary.scanned;
189
+ result.existing += summary.existing;
190
+ result.migrated += summary.migrated;
191
+ options.onPluginMigration?.(summary);
192
+ }
193
+ return result;
261
194
  }
262
-
263
- // src/cli/upgrade/migrations/upgrade-plugins.ts
195
+ //#endregion
196
+ //#region src/cli/upgrade/migrations/upgrade-plugins.ts
264
197
  function unique(values) {
265
- return [...new Set(values)];
198
+ return [...new Set(values)];
266
199
  }
267
200
  function baseCatalogConfig(context) {
268
- return context.pluginCatalogConfig ?? (context.pluginSet ? pluginCatalogConfigFromPluginSet(context.pluginSet) : pluginCatalogConfigFromEnv());
201
+ return context.pluginCatalogConfig ?? (context.pluginSet ? pluginCatalogConfigFromPluginSet(context.pluginSet) : pluginCatalogConfigFromEnv());
269
202
  }
270
203
  function inlinePluginName(definition) {
271
- return definition.manifest.name;
204
+ return definition.manifest.name;
272
205
  }
273
206
  function mergeInlineManifests(left, right) {
274
- const merged = /* @__PURE__ */ new Map();
275
- for (const definition of [...left ?? [], ...right ?? []]) {
276
- merged.set(inlinePluginName(definition), definition);
277
- }
278
- return merged.size > 0 ? [...merged.values()] : void 0;
207
+ const merged = /* @__PURE__ */ new Map();
208
+ for (const definition of [...left ?? [], ...right ?? []]) merged.set(inlinePluginName(definition), definition);
209
+ return merged.size > 0 ? [...merged.values()] : void 0;
279
210
  }
280
211
  function mergeCatalogConfig(base, added) {
281
- if (!base) {
282
- return added;
283
- }
284
- if (!added) {
285
- return base;
286
- }
287
- const inlineManifests = mergeInlineManifests(
288
- base.inlineManifests,
289
- added.inlineManifests
290
- );
291
- const packages = unique([
292
- ...base.packages ?? [],
293
- ...added.packages ?? []
294
- ]);
295
- const manifests = base.manifests || added.manifests ? { ...base.manifests, ...added.manifests } : void 0;
296
- return {
297
- ...inlineManifests ? { inlineManifests } : void 0,
298
- ...packages.length > 0 ? { packages } : void 0,
299
- ...manifests ? { manifests } : void 0
300
- };
212
+ if (!base) return added;
213
+ if (!added) return base;
214
+ const inlineManifests = mergeInlineManifests(base.inlineManifests, added.inlineManifests);
215
+ const packages = unique([...base.packages ?? [], ...added.packages ?? []]);
216
+ const manifests = base.manifests || added.manifests ? {
217
+ ...base.manifests,
218
+ ...added.manifests
219
+ } : void 0;
220
+ return {
221
+ ...inlineManifests ? { inlineManifests } : void 0,
222
+ ...packages.length > 0 ? { packages } : void 0,
223
+ ...manifests ? { manifests } : void 0
224
+ };
301
225
  }
302
226
  function packageNamesFromContext(context, catalog) {
303
- return unique([
304
- ...context.pluginSet?.packageNames ?? [],
305
- ...catalog?.packages ?? []
306
- ]);
227
+ return unique([...context.pluginSet?.packageNames ?? [], ...catalog?.packages ?? []]);
307
228
  }
229
+ /** Resolve the plugin catalog used by SQL upgrade migrations. */
308
230
  async function resolveUpgradePluginCatalog(context) {
309
- const catalog = baseCatalogConfig(context);
310
- const packageNames = packageNamesFromContext(context, catalog);
311
- const registrations = context.pluginSet?.registrations ?? [];
312
- const manifests = context.pluginSet?.manifests || catalog?.manifests ? {
313
- ...catalog?.manifests,
314
- ...context.pluginSet?.manifests
315
- } : void 0;
316
- const pluginSet = packageNames.length > 0 || registrations.length > 0 || context.pluginSet ? defineJuniorPlugins(
317
- [...packageNames, ...registrations],
318
- manifests ? { manifests } : {}
319
- ) : void 0;
320
- return mergeCatalogConfig(
321
- catalog,
322
- pluginCatalogConfigFromPluginSet(pluginSet)
323
- );
231
+ const catalog = baseCatalogConfig(context);
232
+ const packageNames = packageNamesFromContext(context, catalog);
233
+ const registrations = context.pluginSet?.registrations ?? [];
234
+ const manifests = context.pluginSet?.manifests || catalog?.manifests ? {
235
+ ...catalog?.manifests,
236
+ ...context.pluginSet?.manifests
237
+ } : void 0;
238
+ const pluginSet = packageNames.length > 0 || registrations.length > 0 || context.pluginSet ? defineJuniorPlugins([...packageNames, ...registrations], manifests ? { manifests } : {}) : void 0;
239
+ return mergeCatalogConfig(catalog, pluginCatalogConfigFromPluginSet(pluginSet));
324
240
  }
325
-
326
- // src/cli/upgrade/migrations/plugin-sql.ts
241
+ //#endregion
242
+ //#region src/cli/upgrade/migrations/plugin-sql.ts
243
+ /** Apply SQL schema migrations owned by explicitly enabled plugins. */
327
244
  async function migratePluginsToSql(context, options = {}) {
328
- const pluginCatalog = createPluginCatalogRuntime();
329
- pluginCatalog.setConfig(await resolveUpgradePluginCatalog(context));
330
- return await migratePluginSchemas(
331
- context.sqlExecutor,
332
- pluginCatalog.getMigrationRoots(),
333
- options
334
- );
245
+ const pluginCatalog = createPluginCatalogRuntime();
246
+ pluginCatalog.setConfig(await resolveUpgradePluginCatalog(context));
247
+ return await migratePluginSchemas(context.sqlExecutor, pluginCatalog.getMigrationRoots(), options);
335
248
  }
336
-
337
- // src/cli/upgrade.ts
338
- var DEFAULT_IO = {
339
- info: console.log
340
- };
341
- var localPluginLoader = createJiti(import.meta.url, { moduleCache: false });
249
+ //#endregion
250
+ //#region src/cli/upgrade.ts
251
+ /**
252
+ * SQL-only upgrade entry point.
253
+ *
254
+ * It applies core and enabled-plugin Drizzle migrations through one owned
255
+ * executor; legacy state conversion belongs to the required bridge release.
256
+ */
257
+ const DEFAULT_IO = { info: console.log };
258
+ const localPluginLoader = createJiti(import.meta.url, { moduleCache: false });
342
259
  function isMissingVirtualConfig(error) {
343
- if (!(error instanceof Error)) {
344
- return false;
345
- }
346
- const code = error.code;
347
- return (code === "ERR_PACKAGE_IMPORT_NOT_DEFINED" || code === "ERR_MODULE_NOT_FOUND" || code === "MODULE_NOT_FOUND") && error.message.includes("#junior/config");
260
+ if (!(error instanceof Error)) return false;
261
+ const code = error.code;
262
+ return (code === "ERR_PACKAGE_IMPORT_NOT_DEFINED" || code === "ERR_MODULE_NOT_FOUND" || code === "MODULE_NOT_FOUND") && error.message.includes("#junior/config");
348
263
  }
264
+ /** Resolve the plugin set available to upgrade migrations in source and built CLI runs. */
349
265
  async function resolveUpgradePluginSet() {
350
- try {
351
- const mod = await import("#junior/config");
352
- return mod.pluginSet;
353
- } catch (error) {
354
- if (!isMissingVirtualConfig(error)) {
355
- throw error;
356
- }
357
- }
358
- return await loadAppPluginSet(
359
- process.cwd(),
360
- async (moduleRef) => localPluginLoader.import(moduleRef.importPath)
361
- );
266
+ try {
267
+ return (await import("#junior/config")).pluginSet;
268
+ } catch (error) {
269
+ if (!isMissingVirtualConfig(error)) throw error;
270
+ }
271
+ return await loadAppPluginSet(process.cwd(), async (moduleRef) => localPluginLoader.import(moduleRef.importPath));
362
272
  }
363
273
  function migrationCount(count) {
364
- return `${count} ${count === 1 ? "migration" : "migrations"}`;
274
+ return `${count} ${count === 1 ? "migration" : "migrations"}`;
365
275
  }
366
276
  function formatOwnerSummary(owner, summary) {
367
- if (summary.migrated === 0) {
368
- return ` ${owner}: up to date (${migrationCount(summary.scanned)})`;
369
- }
370
- return ` ${owner}: applied ${migrationCount(summary.migrated)} (${summary.scanned} total)`;
277
+ if (summary.migrated === 0) return ` ${owner}: up to date (${migrationCount(summary.scanned)})`;
278
+ return ` ${owner}: applied ${migrationCount(summary.migrated)} (${summary.scanned} total)`;
371
279
  }
372
280
  function pluginMigrationOwner(pluginName) {
373
- const unscopedName = pluginName.split("/").at(-1) ?? pluginName;
374
- return unscopedName.startsWith("junior-") ? unscopedName : `junior-${unscopedName}`;
281
+ const unscopedName = pluginName.split("/").at(-1) ?? pluginName;
282
+ return unscopedName.startsWith("junior-") ? unscopedName : `junior-${unscopedName}`;
375
283
  }
376
284
  async function runDatabaseMigrations(context, io) {
377
- io.info("Checking database migrations...");
378
- const core = await migrateSchema(context.sqlExecutor);
379
- io.info(formatOwnerSummary("junior", core));
380
- const plugins = await migratePluginsToSql(context, {
381
- onPluginMigration: (summary) => {
382
- io.info(
383
- formatOwnerSummary(pluginMigrationOwner(summary.pluginName), summary)
384
- );
385
- }
386
- });
387
- const migrated = core.migrated + plugins.migrated;
388
- const scanned = core.scanned + plugins.scanned;
389
- io.info(
390
- migrated === 0 ? `Database is up to date (${migrationCount(scanned)}).` : `Applied ${migrationCount(migrated)} (${scanned} total).`
391
- );
285
+ io.info("Checking database migrations...");
286
+ const core = await migrateSchema(context.sqlExecutor);
287
+ io.info(formatOwnerSummary("junior", core));
288
+ const plugins = await migratePluginsToSql(context, { onPluginMigration: (summary) => {
289
+ io.info(formatOwnerSummary(pluginMigrationOwner(summary.pluginName), summary));
290
+ } });
291
+ const migrated = core.migrated + plugins.migrated;
292
+ const scanned = core.scanned + plugins.scanned;
293
+ io.info(migrated === 0 ? `Database is up to date (${migrationCount(scanned)}).` : `Applied ${migrationCount(migrated)} (${scanned} total).`);
392
294
  }
295
+ /** Apply Junior and enabled-plugin database migrations. */
393
296
  async function runUpgrade(io = DEFAULT_IO, options = {}) {
394
- const { sql } = getChatConfig();
395
- const sqlExecutor = createJuniorSqlExecutor({
396
- connectionString: sql.databaseUrl,
397
- driver: sql.driver,
398
- statementTimeoutMs: false
399
- });
400
- try {
401
- const pluginSet = options.pluginSet === void 0 ? await resolveUpgradePluginSet() : options.pluginSet ?? void 0;
402
- await runDatabaseMigrations(
403
- {
404
- pluginSet,
405
- sqlExecutor
406
- },
407
- io
408
- );
409
- } finally {
410
- await sqlExecutor.close();
411
- }
297
+ const { sql } = getChatConfig();
298
+ const sqlExecutor = createJuniorSqlExecutor({
299
+ connectionString: sql.databaseUrl,
300
+ driver: sql.driver,
301
+ statementTimeoutMs: false
302
+ });
303
+ try {
304
+ await runDatabaseMigrations({
305
+ pluginSet: options.pluginSet === void 0 ? await resolveUpgradePluginSet() : options.pluginSet ?? void 0,
306
+ sqlExecutor
307
+ }, io);
308
+ } finally {
309
+ await sqlExecutor.close();
310
+ }
412
311
  }
413
- export {
414
- resolveUpgradePluginSet,
415
- runUpgrade
416
- };
312
+ //#endregion
313
+ export { resolveUpgradePluginSet, runUpgrade };