| 1574 | }; |
| 1575 | |
| 1576 | export const createExecutor = <const TPlugins extends readonly AnyPlugin[] = readonly []>( |
| 1577 | config: ExecutorConfig<TPlugins>, |
| 1578 | ): Effect.Effect<Executor<TPlugins>, StorageFailure> => |
| 1579 | Effect.gen(function* () { |
| 1580 | const defaultPlugins = (): TPlugins => { |
| 1581 | const empty: readonly AnyPlugin[] = []; |
| 1582 | return empty as TPlugins; |
| 1583 | }; |
| 1584 | const { plugins: userPlugins = defaultPlugins() } = config; |
| 1585 | |
| 1586 | const tenant = String(config.tenant); |
| 1587 | const subject = config.subject != null ? String(config.subject) : null; |
| 1588 | |
| 1589 | const ownerBinding: OwnerBinding = { |
| 1590 | tenant: config.tenant, |
| 1591 | subject: config.subject ?? null, |
| 1592 | }; |
| 1593 | |
| 1594 | const ownedKeys = (owner: Owner): OwnedKeys => { |
| 1595 | if (owner === "org") return { tenant, owner, subject: ORG_SUBJECT }; |
| 1596 | if (subject == null) { |
| 1597 | // oxlint-disable-next-line executor/no-try-catch-or-throw -- boundary: programmer error caught and surfaced as StorageError below by callers |
| 1598 | throw new StorageError({ |
| 1599 | message: `Cannot target owner "user": executor has no subject.`, |
| 1600 | cause: undefined, |
| 1601 | }); |
| 1602 | } |
| 1603 | return { tenant, owner, subject }; |
| 1604 | }; |
| 1605 | |
| 1606 | const requireUserSubject = (owner: Owner): Effect.Effect<void, StorageFailure> => |
| 1607 | owner === "user" && subject == null |
| 1608 | ? Effect.fail( |
| 1609 | new StorageError({ |
| 1610 | message: `Cannot target owner "user": executor has no subject.`, |
| 1611 | cause: undefined, |
| 1612 | }), |
| 1613 | ) |
| 1614 | : Effect.void; |
| 1615 | |
| 1616 | // Built-in core-tools plugin: agent-facing static tools over the v2 surface. |
| 1617 | const plugins: readonly AnyPlugin[] = config.coreTools |
| 1618 | ? ([ |
| 1619 | coreToolsPlugin({ |
| 1620 | webBaseUrl: config.coreTools.webBaseUrl, |
| 1621 | orgSlug: config.coreTools.orgSlug, |
| 1622 | includeProviders: config.coreTools.includeProviders, |
| 1623 | }), |
| 1624 | ...userPlugins, |
| 1625 | ] as readonly AnyPlugin[]) |
| 1626 | : (userPlugins as readonly AnyPlugin[]); |
| 1627 | |
| 1628 | const tables = yield* Effect.try({ |
| 1629 | try: () => collectTables(), |
| 1630 | catch: (cause) => storageFailureFromUnknown("Failed to collect executor tables", cause), |
| 1631 | }); |
| 1632 | const dbInput = yield* Effect.suspend(() => { |
| 1633 | if (!config.db) return Effect.succeed(createDefaultMemoryDb(tables)); |