(
input: CreateConnectionInput,
)
| 2163 | // ------------------------------------------------------------------ |
| 2164 | |
| 2165 | const connectionsCreate = ( |
| 2166 | input: CreateConnectionInput, |
| 2167 | ): Effect.Effect< |
| 2168 | Connection, |
| 2169 | | IntegrationNotFoundError |
| 2170 | | CredentialProviderNotRegisteredError |
| 2171 | | InvalidConnectionInputError |
| 2172 | | StorageFailure |
| 2173 | > => |
| 2174 | Effect.gen(function* () { |
| 2175 | const name = connectionIdentifier(String(input.name)); |
| 2176 | // Typed (not StorageError) so the HTTP edge can answer 400 with the |
| 2177 | // reason instead of an opaque 500 — callers can act on it. |
| 2178 | if (input.owner === "user" && subject == null) { |
| 2179 | return yield* new InvalidConnectionInputError({ |
| 2180 | message: |
| 2181 | 'Cannot create a personal connection: this context has no user subject. Create it with owner "org", or connect as a signed-in user.', |
| 2182 | }); |
| 2183 | } |
| 2184 | const integrationRow = yield* findIntegrationRow(input.integration); |
| 2185 | if (!integrationRow) { |
| 2186 | return yield* new IntegrationNotFoundError({ |
| 2187 | slug: input.integration, |
| 2188 | }); |
| 2189 | } |
| 2190 | |
| 2191 | // Resolve the value origin(s) → one provider + an item_ids map (one entry |
| 2192 | // per named input). All of a connection's inputs share a single provider: |
| 2193 | // pasted inputs go to the default writable store, external `from` inputs to |
| 2194 | // their provider. Mixing pasted + external, or two external providers, is |
| 2195 | // rejected (the connection row carries one `provider`). |
| 2196 | const inputs = normalizeConnectionInputs(input); |
| 2197 | const pasted = inputs.filter((i) => "value" in i.origin); |
| 2198 | const external = inputs.filter((i) => "from" in i.origin); |
| 2199 | // A credentialed connection is born wired: it must reference at least |
| 2200 | // one credential input. An empty binding (no inputs at all — e.g. an |
| 2201 | // empty `values`/`inputs` map) is a credential with no credential: it |
| 2202 | // would persist, produce a full tool catalog, and then fail every |
| 2203 | // invocation with `connection_value_missing`. Reject it here — EXCEPT |
| 2204 | // for the no-auth template ("none"), where zero inputs and an empty |
| 2205 | // `item_ids` map are the canonical shape (public MCP servers; the UI |
| 2206 | // submits `values: {}` for them). OAuth connections are minted via |
| 2207 | // `mintOAuthConnection`, not this path; an external `from` reference |
| 2208 | // may resolve to null and is surfaced at invoke time, not here. |
| 2209 | const isNoAuth = String(input.template) === String(NO_AUTH_TEMPLATE); |
| 2210 | if (inputs.length === 0 && !isNoAuth) { |
| 2211 | return yield* new InvalidConnectionInputError({ |
| 2212 | message: "A connection must supply at least one credential input.", |
| 2213 | }); |
| 2214 | } |
| 2215 | let providerKey: string; |
| 2216 | const itemIds: Record<string, string> = {}; |
| 2217 | if (external.length > 0 && pasted.length > 0) { |
| 2218 | return yield* new InvalidConnectionInputError({ |
| 2219 | message: "A connection cannot mix pasted and external-provider inputs.", |
| 2220 | }); |
| 2221 | } |
| 2222 | if (external.length > 0) { |
no test coverage detected