(
input: CreateConnectionInput,
)
| 2762 | // ------------------------------------------------------------------ |
| 2763 | |
| 2764 | const connectionsCreate = ( |
| 2765 | input: CreateConnectionInput, |
| 2766 | ): Effect.Effect< |
| 2767 | Connection, |
| 2768 | | IntegrationNotFoundError |
| 2769 | | CredentialProviderNotRegisteredError |
| 2770 | | InvalidConnectionInputError |
| 2771 | | StorageFailure |
| 2772 | > => |
| 2773 | Effect.gen(function* () { |
| 2774 | const name = connectionIdentifier(String(input.name)); |
| 2775 | // Typed (not StorageError) so the HTTP edge can answer 400 with the |
| 2776 | // reason instead of an opaque 500 — callers can act on it. |
| 2777 | if (input.owner === "user" && subject == null) { |
| 2778 | return yield* new InvalidConnectionInputError({ |
| 2779 | message: |
| 2780 | 'Cannot create a personal connection: this context has no user subject. Create it with owner "org", or connect as a signed-in user.', |
| 2781 | }); |
| 2782 | } |
| 2783 | const integrationRow = yield* findIntegrationRow(input.integration); |
| 2784 | if (!integrationRow) { |
| 2785 | return yield* new IntegrationNotFoundError({ |
| 2786 | slug: input.integration, |
| 2787 | }); |
| 2788 | } |
| 2789 | |
| 2790 | // Resolve the value origin(s) → one provider + an item_ids map (one entry |
| 2791 | // per named input). All of a connection's inputs share a single provider: |
| 2792 | // pasted inputs go to the default writable store, external `from` inputs to |
| 2793 | // their provider. Mixing pasted + external, or two external providers, is |
| 2794 | // rejected (the connection row carries one `provider`). |
| 2795 | const inputs = normalizeConnectionInputs(input); |
| 2796 | const pasted = inputs.filter((i) => "value" in i.origin); |
| 2797 | const external = inputs.filter((i) => "from" in i.origin); |
| 2798 | // A credentialed connection is born wired: it must reference at least |
| 2799 | // one credential input. An empty binding (no inputs at all — e.g. an |
| 2800 | // empty `values`/`inputs` map) is a credential with no credential: it |
| 2801 | // would persist, produce a full tool catalog, and then fail every |
| 2802 | // invocation with `connection_value_missing`. Reject it here — EXCEPT |
| 2803 | // for the no-auth template ("none"), where zero inputs and an empty |
| 2804 | // `item_ids` map are the canonical shape (public MCP servers; the UI |
| 2805 | // submits `values: {}` for them). OAuth connections are minted via |
| 2806 | // `mintOAuthConnection`, not this path; an external `from` reference |
| 2807 | // may resolve to null and is surfaced at invoke time, not here. |
| 2808 | const isNoAuth = String(input.template) === String(NO_AUTH_TEMPLATE); |
| 2809 | if (inputs.length === 0 && !isNoAuth) { |
| 2810 | return yield* new InvalidConnectionInputError({ |
| 2811 | message: "A connection must supply at least one credential input.", |
| 2812 | }); |
| 2813 | } |
| 2814 | let providerKey: string; |
| 2815 | const itemIds: Record<string, string> = {}; |
| 2816 | if (external.length > 0 && pasted.length > 0) { |
| 2817 | return yield* new InvalidConnectionInputError({ |
| 2818 | message: "A connection cannot mix pasted and external-provider inputs.", |
| 2819 | }); |
| 2820 | } |
| 2821 | if (external.length > 0) { |
no test coverage detected