(
input: CreateConnectionInput,
)
| 2716 | // ------------------------------------------------------------------ |
| 2717 | |
| 2718 | const connectionsCreate = ( |
| 2719 | input: CreateConnectionInput, |
| 2720 | ): Effect.Effect< |
| 2721 | Connection, |
| 2722 | | IntegrationNotFoundError |
| 2723 | | CredentialProviderNotRegisteredError |
| 2724 | | InvalidConnectionInputError |
| 2725 | | StorageFailure |
| 2726 | > => |
| 2727 | Effect.gen(function* () { |
| 2728 | const name = connectionIdentifier(String(input.name)); |
| 2729 | // Typed (not StorageError) so the HTTP edge can answer 400 with the |
| 2730 | // reason instead of an opaque 500 — callers can act on it. |
| 2731 | if (input.owner === "user" && subject == null) { |
| 2732 | return yield* new InvalidConnectionInputError({ |
| 2733 | message: |
| 2734 | 'Cannot create a personal connection: this context has no user subject. Create it with owner "org", or connect as a signed-in user.', |
| 2735 | }); |
| 2736 | } |
| 2737 | const integrationRow = yield* findIntegrationRow(input.integration); |
| 2738 | if (!integrationRow) { |
| 2739 | return yield* new IntegrationNotFoundError({ |
| 2740 | slug: input.integration, |
| 2741 | }); |
| 2742 | } |
| 2743 | |
| 2744 | // Resolve the value origin(s) → one provider + an item_ids map (one entry |
| 2745 | // per named input). All of a connection's inputs share a single provider: |
| 2746 | // pasted inputs go to the default writable store, external `from` inputs to |
| 2747 | // their provider. Mixing pasted + external, or two external providers, is |
| 2748 | // rejected (the connection row carries one `provider`). |
| 2749 | const inputs = normalizeConnectionInputs(input); |
| 2750 | const pasted = inputs.filter((i) => "value" in i.origin); |
| 2751 | const external = inputs.filter((i) => "from" in i.origin); |
| 2752 | // A credentialed connection is born wired: it must reference at least |
| 2753 | // one credential input. An empty binding (no inputs at all — e.g. an |
| 2754 | // empty `values`/`inputs` map) is a credential with no credential: it |
| 2755 | // would persist, produce a full tool catalog, and then fail every |
| 2756 | // invocation with `connection_value_missing`. Reject it here — EXCEPT |
| 2757 | // for the no-auth template ("none"), where zero inputs and an empty |
| 2758 | // `item_ids` map are the canonical shape (public MCP servers; the UI |
| 2759 | // submits `values: {}` for them). OAuth connections are minted via |
| 2760 | // `mintOAuthConnection`, not this path; an external `from` reference |
| 2761 | // may resolve to null and is surfaced at invoke time, not here. |
| 2762 | const isNoAuth = String(input.template) === String(NO_AUTH_TEMPLATE); |
| 2763 | if (inputs.length === 0 && !isNoAuth) { |
| 2764 | return yield* new InvalidConnectionInputError({ |
| 2765 | message: "A connection must supply at least one credential input.", |
| 2766 | }); |
| 2767 | } |
| 2768 | let providerKey: string; |
| 2769 | const itemIds: Record<string, string> = {}; |
| 2770 | if (external.length > 0 && pasted.length > 0) { |
| 2771 | return yield* new InvalidConnectionInputError({ |
| 2772 | message: "A connection cannot mix pasted and external-provider inputs.", |
| 2773 | }); |
| 2774 | } |
| 2775 | if (external.length > 0) { |
no test coverage detected