| 1297 | }; |
| 1298 | |
| 1299 | export const createExecutor = <const TPlugins extends readonly AnyPlugin[] = readonly []>( |
| 1300 | config: ExecutorConfig<TPlugins>, |
| 1301 | ): Effect.Effect<Executor<TPlugins>, StorageFailure> => |
| 1302 | Effect.gen(function* () { |
| 1303 | const defaultPlugins = (): TPlugins => { |
| 1304 | const empty: readonly AnyPlugin[] = []; |
| 1305 | return empty as TPlugins; |
| 1306 | }; |
| 1307 | const { plugins: userPlugins = defaultPlugins() } = config; |
| 1308 | |
| 1309 | const tenant = String(config.tenant); |
| 1310 | const subject = config.subject != null ? String(config.subject) : null; |
| 1311 | |
| 1312 | const ownerBinding: OwnerBinding = { |
| 1313 | tenant: config.tenant, |
| 1314 | subject: config.subject ?? null, |
| 1315 | }; |
| 1316 | |
| 1317 | const ownedKeys = (owner: Owner): OwnedKeys => { |
| 1318 | if (owner === "org") return { tenant, owner, subject: ORG_SUBJECT }; |
| 1319 | if (subject == null) { |
| 1320 | // oxlint-disable-next-line executor/no-try-catch-or-throw -- boundary: programmer error caught and surfaced as StorageError below by callers |
| 1321 | throw new StorageError({ |
| 1322 | message: `Cannot target owner "user": executor has no subject.`, |
| 1323 | cause: undefined, |
| 1324 | }); |
| 1325 | } |
| 1326 | return { tenant, owner, subject }; |
| 1327 | }; |
| 1328 | |
| 1329 | const requireUserSubject = (owner: Owner): Effect.Effect<void, StorageFailure> => |
| 1330 | owner === "user" && subject == null |
| 1331 | ? Effect.fail( |
| 1332 | new StorageError({ |
| 1333 | message: `Cannot target owner "user": executor has no subject.`, |
| 1334 | cause: undefined, |
| 1335 | }), |
| 1336 | ) |
| 1337 | : Effect.void; |
| 1338 | |
| 1339 | // Built-in core-tools plugin: agent-facing static tools over the v2 surface. |
| 1340 | const plugins: readonly AnyPlugin[] = config.coreTools |
| 1341 | ? ([ |
| 1342 | coreToolsPlugin({ |
| 1343 | webBaseUrl: config.coreTools.webBaseUrl, |
| 1344 | orgSlug: config.coreTools.orgSlug, |
| 1345 | includeProviders: config.coreTools.includeProviders, |
| 1346 | }), |
| 1347 | ...userPlugins, |
| 1348 | ] as readonly AnyPlugin[]) |
| 1349 | : (userPlugins as readonly AnyPlugin[]); |
| 1350 | |
| 1351 | const tables = yield* Effect.try({ |
| 1352 | try: () => collectTables(), |
| 1353 | catch: (cause) => storageFailureFromUnknown("Failed to collect executor tables", cause), |
| 1354 | }); |
| 1355 | const dbInput = yield* Effect.suspend(() => { |
| 1356 | if (!config.db) return Effect.succeed(createDefaultMemoryDb(tables)); |