返回 Skills
caffeinelabs/skills· Apache-2.0 内容可用

extension-core-infrastructure

Core infrastructure providing backend connection configuration, storage client, and React app entry point.

安装

与 skills.sh 相同的 Command / Prompt 安装方式


name: extension-core-infrastructure description: Core infrastructure providing backend connection configuration, storage client, and React app entry point. version: 1.1.0 compatibility: npm: "@caffeineai/core-infrastructure": "^1.1.0" "@caffeineai/object-storage": "^1.1.0" caffeineai-subscription: [none]

Core Infrastructure

Core infrastructure extension for Caffeine AI.

Overview

This component provides the foundational infrastructure for all projects: backend connection configuration, Internet Identity authentication hooks, and actor management utilities.

Requirements

"@caffeineai/core-infrastructure": "^1.1.0"
"@caffeineai/object-storage": "^1.1.0"
"@icp-sdk/auth": "^7.1.0"
"@icp-sdk/core": "^5.3.0"

@caffeineai/object-storage is a peer dependency of core-infrastructure. Every project must install it as a direct npm dependency (the build template includes both packages).

Integration

Core infrastructure is automatically included in every project. No manual integration steps are required.

Frontend

The core-infrastructure frontend package (@caffeineai/core-infrastructure) is automatically included in every project.

App Entry Point

Wrap the app with InternetIdentityProvider and QueryClientProvider:

import { InternetIdentityProvider } from "@caffeineai/core-infrastructure";
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import ReactDOM from "react-dom/client";
import App from "./App";

const queryClient = new QueryClient();

ReactDOM.createRoot(document.getElementById("root")!).render(
  <QueryClientProvider client={queryClient}>
    <InternetIdentityProvider>
      <App />
    </InternetIdentityProvider>
  </QueryClientProvider>,
);

useInternetIdentity() — Authentication Hook

Provides identity state, login, and logout for Internet Identity.

Return Values

FieldTypeDescription
identityIdentity | undefinedThe user's identity (available after login or session restore)
login() => voidOpens the II popup. Fire-and-forget — do not await.
clear() => voidLogs out and clears stored identity. Fire-and-forget.
isAuthenticatedbooleantrue when user has a valid identity. Use this for UI gating.
isInitializingbooleantrue while AuthClient is loading from IndexedDB
isLoggingInbooleantrue while the II popup is open
isLoginSuccessbooleantrue only after interactive login (NOT after page reload restore)
isLoginErrorbooleantrue if login or initialization failed
loginErrorError | undefinedThe error object when isLoginError is true

Auth State Lifecycle

ScenariologinStatusisAuthenticated
Page load, no stored session"idle"false
Restoring stored session"initializing"falsetrue
Stored session restored after reload"idle"true
Interactive login in progress"logging-in"false
Interactive login just completed"success"true
Login popup failed / cancelled"loginError"false

IMPORTANT: isLoginSuccess is only true after an interactive login via the popup — NOT when a stored identity is restored on page reload. Always use isAuthenticated for conditional rendering.

Usage

Gate authenticated UI on isAuthenticated:

const { isAuthenticated } = useInternetIdentity();

{isAuthenticated ? <AuthenticatedApp /> : <LoginScreen />}

Disable the login button while initializing or logging in:

const { login, isInitializing, isLoggingIn } = useInternetIdentity();

<button onClick={() => login()} disabled={isInitializing || isLoggingIn}>
  Sign in
</button>

login() and clear() are fire-and-forget — the hook's state fields (isLoggingIn, isInitializing) track the async lifecycle. Do not wrap them in local useState / isPending logic.

useActor() — Backend Actor Hook

Creates and manages a typed backend actor instance. Automatically re-creates the actor when the user's identity changes (login/logout).

import { useActor } from "@caffeineai/core-infrastructure";
import { createActor } from "declarations/backend";

function MyComponent() {
  const { actor, isFetching } = useActor(createActor);

  // actor is null while loading, then the typed backend actor
  if (!actor || isFetching) return <Loading />;

  // Call backend methods directly
  const data = await actor.myBackendMethod();
}

Return Values

FieldTypeDescription
actorT | nullThe typed backend actor, or null while loading
isFetchingbooleantrue while the actor is being created

When the identity changes (login, logout, or session restore), the actor is automatically re-created with the new identity and all dependent queries are invalidated and refetched.

附带文件

migration/v0.x.y-to-v1.x.y.md
# Migration: v0.x.y -> v1.x.y

## Summary

v1 replaces the legacy `@dfinity/*` packages with the unified `@icp-sdk/*` SDK. All `@dfinity/*` direct dependencies must be removed from `package.json`; their functionality is re-exported from `@icp-sdk/core` and `@icp-sdk/auth` sub-paths.

`InternetIdentityProvider` now enables attribute verification by default. Existing apps that do not need email attributes must opt out explicitly.

## Breaking changes

### 1. Package changes in `package.json`

Remove all `@dfinity/*` packages and the pinned `@icp-sdk/core@~4.1.0`:

```diff
-    "@dfinity/agent": "~3.3.0",
-    "@dfinity/identity": "~3.3.0",
-    "@dfinity/auth-client": "~3.3.0",
-    "@dfinity/candid": "~3.3.0",
-    "@dfinity/principal": "~3.3.0",
-    "@icp-sdk/core": "~4.1.0",
+    "@icp-sdk/auth": "^7.1.0",
+    "@icp-sdk/core": "^5.3.0",
```

### 2. Import paths

Replace every `@dfinity/*` import in app code with the equivalent `@icp-sdk` sub-path:

| Old import | New import |
|---|---|
| `@dfinity/agent` | `@icp-sdk/core/agent` |
| `@dfinity/principal` | `@icp-sdk/core/principal` |
| `@dfinity/candid` | `@icp-sdk/core/candid` |
| `@dfinity/identity` | `@icp-sdk/core/identity` |
| `@dfinity/auth-client` | `@icp-sdk/auth/client` |

### 3. `InternetIdentityProvider` attribute verification is now on by default

`withAttributes` now defaults to `{}` (enabled, requesting `verified_email`). This is a non-breaking addition from Internet Identity — existing apps continue to work as before, and users are simply prompted to share their email during sign-in. No code change is required unless you want to opt out entirely:

```tsx
// opt out — plain sign-in, no attribute request
<InternetIdentityProvider withAttributes={false}>
```

### 4. `vite.config.js` dedupe

```diff
-  dedupe: ["@dfinity/agent"]
+  dedupe: ["@icp-sdk/core"]
```

## Upgrade checklist

- [ ] Remove `@dfinity/agent`, `@dfinity/identity`, `@dfinity/auth-client`, `@dfinity/candid`, `@dfinity/principal`, and `@icp-sdk/core@~4.1.0` from `package.json`
- [ ] Add `@icp-sdk/auth@^7.1.0` and `@icp-sdk/core@^5.3.0` to `package.json`
- [ ] Update all `@dfinity/*` import paths to their `@icp-sdk` equivalents (see table above)
- [ ] Update `vite.config.js` dedupe from `@dfinity/agent` to `@icp-sdk/core`
- [ ] Optionally pass `withAttributes={false}` to `InternetIdentityProvider` to disable the II attribute request
- [ ] Run `pnpm install` and verify the frontend builds without errors
    extension-core-infrastructure | Prompt Minder