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