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