(
input: CreateConnectionInput,
)
| 2354 | // ------------------------------------------------------------------ |
| 2355 | |
| 2356 | const connectionsCreate = ( |
| 2357 | input: CreateConnectionInput, |
| 2358 | ): Effect.Effect< |
| 2359 | Connection, |
| 2360 | | IntegrationNotFoundError |
| 2361 | | CredentialProviderNotRegisteredError |
| 2362 | | InvalidConnectionInputError |
| 2363 | | StorageFailure |
| 2364 | > => |
| 2365 | Effect.gen(function* () { |
| 2366 | const name = connectionIdentifier(String(input.name)); |
| 2367 | // Typed (not StorageError) so the HTTP edge can answer 400 with the |
| 2368 | // reason instead of an opaque 500 — callers can act on it. |
| 2369 | if (input.owner === "user" && subject == null) { |
| 2370 | return yield* new InvalidConnectionInputError({ |
| 2371 | message: |
| 2372 | 'Cannot create a personal connection: this context has no user subject. Create it with owner "org", or connect as a signed-in user.', |
| 2373 | }); |
| 2374 | } |
| 2375 | const integrationRow = yield* findIntegrationRow(input.integration); |
| 2376 | if (!integrationRow) { |
| 2377 | return yield* new IntegrationNotFoundError({ |
| 2378 | slug: input.integration, |
| 2379 | }); |
| 2380 | } |
| 2381 | |
| 2382 | // Resolve the value origin(s) → one provider + an item_ids map (one entry |
| 2383 | // per named input). All of a connection's inputs share a single provider: |
| 2384 | // pasted inputs go to the default writable store, external `from` inputs to |
| 2385 | // their provider. Mixing pasted + external, or two external providers, is |
| 2386 | // rejected (the connection row carries one `provider`). |
| 2387 | const inputs = normalizeConnectionInputs(input); |
| 2388 | const pasted = inputs.filter((i) => "value" in i.origin); |
| 2389 | const external = inputs.filter((i) => "from" in i.origin); |
| 2390 | // A credentialed connection is born wired: it must reference at least |
| 2391 | // one credential input. An empty binding (no inputs at all — e.g. an |
| 2392 | // empty `values`/`inputs` map) is a credential with no credential: it |
| 2393 | // would persist, produce a full tool catalog, and then fail every |
| 2394 | // invocation with `connection_value_missing`. Reject it here — EXCEPT |
| 2395 | // for the no-auth template ("none"), where zero inputs and an empty |
| 2396 | // `item_ids` map are the canonical shape (public MCP servers; the UI |
| 2397 | // submits `values: {}` for them). OAuth connections are minted via |
| 2398 | // `mintOAuthConnection`, not this path; an external `from` reference |
| 2399 | // may resolve to null and is surfaced at invoke time, not here. |
| 2400 | const isNoAuth = String(input.template) === String(NO_AUTH_TEMPLATE); |
| 2401 | if (inputs.length === 0 && !isNoAuth) { |
| 2402 | return yield* new InvalidConnectionInputError({ |
| 2403 | message: "A connection must supply at least one credential input.", |
| 2404 | }); |
| 2405 | } |
| 2406 | let providerKey: string; |
| 2407 | const itemIds: Record<string, string> = {}; |
| 2408 | if (external.length > 0 && pasted.length > 0) { |
| 2409 | return yield* new InvalidConnectionInputError({ |
| 2410 | message: "A connection cannot mix pasted and external-provider inputs.", |
| 2411 | }); |
| 2412 | } |
| 2413 | if (external.length > 0) { |
no test coverage detected