(
input: CreateConnectionInput,
)
| 3116 | // ------------------------------------------------------------------ |
| 3117 | |
| 3118 | const connectionsCreate = ( |
| 3119 | input: CreateConnectionInput, |
| 3120 | ): Effect.Effect< |
| 3121 | Connection, |
| 3122 | | IntegrationNotFoundError |
| 3123 | | CredentialProviderNotRegisteredError |
| 3124 | | InvalidConnectionInputError |
| 3125 | | StorageFailure |
| 3126 | > => |
| 3127 | Effect.gen(function* () { |
| 3128 | const name = connectionIdentifier(String(input.name)); |
| 3129 | // Typed (not StorageError) so the HTTP edge can answer 400 with the |
| 3130 | // reason instead of an opaque 500 — callers can act on it. |
| 3131 | if (input.owner === "user" && subject == null) { |
| 3132 | return yield* new InvalidConnectionInputError({ |
| 3133 | message: |
| 3134 | 'Cannot create a personal connection: this context has no user subject. Create it with owner "org", or connect as a signed-in user.', |
| 3135 | }); |
| 3136 | } |
| 3137 | const integrationRow = yield* findIntegrationRow(input.integration); |
| 3138 | if (!integrationRow) { |
| 3139 | return yield* new IntegrationNotFoundError({ |
| 3140 | slug: input.integration, |
| 3141 | }); |
| 3142 | } |
| 3143 | |
| 3144 | // Resolve the value origin(s) → one provider + an item_ids map (one entry |
| 3145 | // per named input). All of a connection's inputs share a single provider: |
| 3146 | // pasted inputs go to the default writable store, external `from` inputs to |
| 3147 | // their provider. Mixing pasted + external, or two external providers, is |
| 3148 | // rejected (the connection row carries one `provider`). |
| 3149 | const inputs = normalizeConnectionInputs(input); |
| 3150 | const pasted = inputs.filter((i) => "value" in i.origin); |
| 3151 | const external = inputs.filter((i) => "from" in i.origin); |
| 3152 | // A credentialed connection is born wired: it must reference at least |
| 3153 | // one credential input. An empty binding (no inputs at all — e.g. an |
| 3154 | // empty `values`/`inputs` map) is a credential with no credential: it |
| 3155 | // would persist, produce a full tool catalog, and then fail every |
| 3156 | // invocation with `connection_value_missing`. Reject it here — EXCEPT |
| 3157 | // for the no-auth template ("none"), where zero inputs and an empty |
| 3158 | // `item_ids` map are the canonical shape (public MCP servers; the UI |
| 3159 | // submits `values: {}` for them). OAuth connections are minted via |
| 3160 | // `mintOAuthConnection`, not this path; an external `from` reference |
| 3161 | // may resolve to null and is surfaced at invoke time, not here. |
| 3162 | const isNoAuth = String(input.template) === String(NO_AUTH_TEMPLATE); |
| 3163 | if (inputs.length === 0 && !isNoAuth) { |
| 3164 | return yield* new InvalidConnectionInputError({ |
| 3165 | message: "A connection must supply at least one credential input.", |
| 3166 | }); |
| 3167 | } |
| 3168 | let providerKey: string; |
| 3169 | const itemIds: Record<string, string> = {}; |
| 3170 | if (external.length > 0 && pasted.length > 0) { |
| 3171 | return yield* new InvalidConnectionInputError({ |
| 3172 | message: "A connection cannot mix pasted and external-provider inputs.", |
| 3173 | }); |
| 3174 | } |
| 3175 | if (external.length > 0) { |
no test coverage detected