(
integrationRow: IntegrationRow,
ref: ConnectionRef,
mode: "explicit" | "background" = "explicit",
)
| 2507 | result.incompleteReason ?? "plugin returned an incomplete tool catalog"; |
| 2508 | |
| 2509 | const produceConnectionTools = ( |
| 2510 | integrationRow: IntegrationRow, |
| 2511 | ref: ConnectionRef, |
| 2512 | mode: "explicit" | "background" = "explicit", |
| 2513 | ): Effect.Effect<readonly Tool[], IntegrationNotFoundError | StorageFailure> => |
| 2514 | Effect.gen(function* () { |
| 2515 | const runtime = runtimes.get(integrationRow.plugin_id); |
| 2516 | const keys = yield* Effect.try({ |
| 2517 | try: () => ownedKeys(ref.owner), |
| 2518 | catch: (cause) => storageFailureFromUnknown("invalid owner", cause), |
| 2519 | }); |
| 2520 | const owner = ref.owner; |
| 2521 | const where = (b: AnyCb) => |
| 2522 | b.and( |
| 2523 | byOwner(owner)(b), |
| 2524 | b("integration", "=", String(ref.integration)), |
| 2525 | b("connection", "=", String(ref.name)), |
| 2526 | ); |
| 2527 | const connectionWhere = (b: AnyCb) => |
| 2528 | b.and( |
| 2529 | byOwner(owner)(b), |
| 2530 | b("integration", "=", String(ref.integration)), |
| 2531 | b("name", "=", String(ref.name)), |
| 2532 | ); |
| 2533 | const isToolSyncHealth = (health: HealthCheckResult | null): boolean => |
| 2534 | health?.detail?.startsWith(toolSyncHealthDetailPrefix) === true; |
| 2535 | const syncedSet = (row: ConnectionRow | null) => { |
| 2536 | const health = row ? Option.getOrNull(decodeLastHealth(row.last_health)) : null; |
| 2537 | return isToolSyncHealth(health) |
| 2538 | ? { tools_synced_at: Date.now(), last_health: null, updated_at: new Date() } |
| 2539 | : { tools_synced_at: Date.now() }; |
| 2540 | }; |
| 2541 | // Every exit stamps the sync time — including the cleanup paths that |
| 2542 | // produce zero tools — so the stale-catalog check (`config_revised_at` |
| 2543 | // vs `tools_synced_at`) doesn't re-attempt this connection per read. |
| 2544 | // Successful syncs also clear stale sync-failure health records, while |
| 2545 | // preserving genuine health-check outcomes. |
| 2546 | const stampSynced = (row: ConnectionRow | null) => |
| 2547 | core.updateMany("connection", { |
| 2548 | where: connectionWhere, |
| 2549 | set: syncedSet(row), |
| 2550 | }); |
| 2551 | const stampSyncedWithHealth = (reason: string) => |
| 2552 | core.updateMany("connection", { |
| 2553 | where: connectionWhere, |
| 2554 | set: { |
| 2555 | tools_synced_at: Date.now(), |
| 2556 | last_health: toolSyncHealth(reason), |
| 2557 | updated_at: new Date(), |
| 2558 | }, |
| 2559 | }); |
| 2560 | |
| 2561 | // Defense in depth (and cleanup for rows created before the create-time |
| 2562 | // guard, or emptied by an external edit): a credentialed non-OAuth |
| 2563 | // connection with no bound credential inputs can never resolve a value, |
| 2564 | // so never advertise tools for it — every call would fail with |
| 2565 | // `connection_value_missing`. OAuth connections resolve via refresh and |
| 2566 | // carry their token outside `item_ids`; no-auth (`"none"` template) |
no test coverage detected