| 1522 | }; |
| 1523 | |
| 1524 | export const createExecutor = <const TPlugins extends readonly AnyPlugin[] = readonly []>( |
| 1525 | config: ExecutorConfig<TPlugins>, |
| 1526 | ): Effect.Effect<Executor<TPlugins>, StorageFailure> => |
| 1527 | Effect.gen(function* () { |
| 1528 | const defaultPlugins = (): TPlugins => { |
| 1529 | const empty: readonly AnyPlugin[] = []; |
| 1530 | return empty as TPlugins; |
| 1531 | }; |
| 1532 | const { plugins: userPlugins = defaultPlugins() } = config; |
| 1533 | |
| 1534 | const tenant = String(config.tenant); |
| 1535 | const subject = config.subject != null ? String(config.subject) : null; |
| 1536 | |
| 1537 | const ownerBinding: OwnerBinding = { |
| 1538 | tenant: config.tenant, |
| 1539 | subject: config.subject ?? null, |
| 1540 | }; |
| 1541 | |
| 1542 | const ownedKeys = (owner: Owner): OwnedKeys => { |
| 1543 | if (owner === "org") return { tenant, owner, subject: ORG_SUBJECT }; |
| 1544 | if (subject == null) { |
| 1545 | // oxlint-disable-next-line executor/no-try-catch-or-throw -- boundary: programmer error caught and surfaced as StorageError below by callers |
| 1546 | throw new StorageError({ |
| 1547 | message: `Cannot target owner "user": executor has no subject.`, |
| 1548 | cause: undefined, |
| 1549 | }); |
| 1550 | } |
| 1551 | return { tenant, owner, subject }; |
| 1552 | }; |
| 1553 | |
| 1554 | const requireUserSubject = (owner: Owner): Effect.Effect<void, StorageFailure> => |
| 1555 | owner === "user" && subject == null |
| 1556 | ? Effect.fail( |
| 1557 | new StorageError({ |
| 1558 | message: `Cannot target owner "user": executor has no subject.`, |
| 1559 | cause: undefined, |
| 1560 | }), |
| 1561 | ) |
| 1562 | : Effect.void; |
| 1563 | |
| 1564 | // Built-in core-tools plugin: agent-facing static tools over the v2 surface. |
| 1565 | const plugins: readonly AnyPlugin[] = config.coreTools |
| 1566 | ? ([ |
| 1567 | coreToolsPlugin({ |
| 1568 | webBaseUrl: config.coreTools.webBaseUrl, |
| 1569 | orgSlug: config.coreTools.orgSlug, |
| 1570 | includeProviders: config.coreTools.includeProviders, |
| 1571 | }), |
| 1572 | ...userPlugins, |
| 1573 | ] as readonly AnyPlugin[]) |
| 1574 | : (userPlugins as readonly AnyPlugin[]); |
| 1575 | |
| 1576 | const tables = yield* Effect.try({ |
| 1577 | try: () => collectTables(), |
| 1578 | catch: (cause) => storageFailureFromUnknown("Failed to collect executor tables", cause), |
| 1579 | }); |
| 1580 | const dbInput = yield* Effect.suspend(() => { |
| 1581 | if (!config.db) return Effect.succeed(createDefaultMemoryDb(tables)); |