(
input: CreateConnectionInput,
)
| 2621 | // ------------------------------------------------------------------ |
| 2622 | |
| 2623 | const connectionsCreate = ( |
| 2624 | input: CreateConnectionInput, |
| 2625 | ): Effect.Effect< |
| 2626 | Connection, |
| 2627 | | IntegrationNotFoundError |
| 2628 | | CredentialProviderNotRegisteredError |
| 2629 | | InvalidConnectionInputError |
| 2630 | | StorageFailure |
| 2631 | > => |
| 2632 | Effect.gen(function* () { |
| 2633 | const name = connectionIdentifier(String(input.name)); |
| 2634 | // Typed (not StorageError) so the HTTP edge can answer 400 with the |
| 2635 | // reason instead of an opaque 500 — callers can act on it. |
| 2636 | if (input.owner === "user" && subject == null) { |
| 2637 | return yield* new InvalidConnectionInputError({ |
| 2638 | message: |
| 2639 | 'Cannot create a personal connection: this context has no user subject. Create it with owner "org", or connect as a signed-in user.', |
| 2640 | }); |
| 2641 | } |
| 2642 | const integrationRow = yield* findIntegrationRow(input.integration); |
| 2643 | if (!integrationRow) { |
| 2644 | return yield* new IntegrationNotFoundError({ |
| 2645 | slug: input.integration, |
| 2646 | }); |
| 2647 | } |
| 2648 | |
| 2649 | // Resolve the value origin(s) → one provider + an item_ids map (one entry |
| 2650 | // per named input). All of a connection's inputs share a single provider: |
| 2651 | // pasted inputs go to the default writable store, external `from` inputs to |
| 2652 | // their provider. Mixing pasted + external, or two external providers, is |
| 2653 | // rejected (the connection row carries one `provider`). |
| 2654 | const inputs = normalizeConnectionInputs(input); |
| 2655 | const pasted = inputs.filter((i) => "value" in i.origin); |
| 2656 | const external = inputs.filter((i) => "from" in i.origin); |
| 2657 | // A credentialed connection is born wired: it must reference at least |
| 2658 | // one credential input. An empty binding (no inputs at all — e.g. an |
| 2659 | // empty `values`/`inputs` map) is a credential with no credential: it |
| 2660 | // would persist, produce a full tool catalog, and then fail every |
| 2661 | // invocation with `connection_value_missing`. Reject it here — EXCEPT |
| 2662 | // for the no-auth template ("none"), where zero inputs and an empty |
| 2663 | // `item_ids` map are the canonical shape (public MCP servers; the UI |
| 2664 | // submits `values: {}` for them). OAuth connections are minted via |
| 2665 | // `mintOAuthConnection`, not this path; an external `from` reference |
| 2666 | // may resolve to null and is surfaced at invoke time, not here. |
| 2667 | const isNoAuth = String(input.template) === String(NO_AUTH_TEMPLATE); |
| 2668 | if (inputs.length === 0 && !isNoAuth) { |
| 2669 | return yield* new InvalidConnectionInputError({ |
| 2670 | message: "A connection must supply at least one credential input.", |
| 2671 | }); |
| 2672 | } |
| 2673 | let providerKey: string; |
| 2674 | const itemIds: Record<string, string> = {}; |
| 2675 | if (external.length > 0 && pasted.length > 0) { |
| 2676 | return yield* new InvalidConnectionInputError({ |
| 2677 | message: "A connection cannot mix pasted and external-provider inputs.", |
| 2678 | }); |
| 2679 | } |
| 2680 | if (external.length > 0) { |
no test coverage detected