npm package: @quantiya/codevibe-core
Description: Core library for CodeVibe plugins - shared keychain, crypto, AppSync, and auth functionality
Author: Quantiya
Latest version: 1.0.2

# @quantiya/codevibe-core

Core library for CodeVibe plugins - shared keychain, crypto, AppSync, and authentication functionality.

## Installation

```bash
npm install @quantiya/codevibe-core
```

## Features

- **Keychain Management** - Secure storage for device keys and OAuth tokens using native keychain (macOS Keychain, Linux libsecret, Windows Credential Manager)
- **Cryptographic Services** - E2E encryption using ECDH P-256 and AES-256-GCM
- **AppSync Client** - GraphQL API and WebSocket subscriptions for AWS AppSync
- **Authentication CLI** - Browser-based OAuth login/logout commands
- **Shared Types** - TypeScript interfaces for events, sessions, encryption

## CLI Usage

```bash
# Sign in via browser (opens Cognito Hosted UI)
codevibe login

# Sign out
codevibe logout

# Check authentication status
codevibe status

# Reset device identity (destructive - old sessions become inaccessible)
codevibe reset-device

# Use specific environment
codevibe --env development login
```

## API Usage

### Keychain Manager

```typescript
import { keychainManager, DeviceIdentity } from '@quantiya/codevibe-core';

// Get or create device identity (stored in native keychain)
const identity = await keychainManager.getOrCreateDeviceIdentity();
console.log(identity.deviceId, identity.publicKey);

// Get OAuth tokens
const tokens = await keychainManager.getTokens('production');
if (tokens) {
  console.log(tokens.email, tokens.userId);
}
```

### Crypto Service

```typescript
import { cryptoService } from '@quantiya/codevibe-core';

// Generate key pair
const keyPair = cryptoService.generateKeyPair();

// Generate session key
const sessionKey = cryptoService.generateSessionKey();

// Encrypt content
const encrypted = cryptoService.encryptContent('Hello World', sessionKey);

// Decrypt content
const decrypted = cryptoService.decryptContent(encrypted, sessionKey);
```

### AppSync Client

```typescript
import { AppSyncClient, loadConfig } from '@quantiya/codevibe-core';

// Load environment configuration
loadConfig('production');

// Create client
const client = new AppSyncClient('production');

// Authenticate with stored tokens
const authenticated = await client.authenticateWithStoredTokens();
if (!authenticated) {
  console.log('Run "codevibe login" first');
  process.exit(1);
}

// Create session
const session = await client.createSession({
  userId: client.getCurrentUserId(),
  agentType: 'CLAUDE',
  projectPath: '/path/to/project',
});

// Subscribe to events
const unsubscribe = client.subscribeToEvents(session.sessionId, (event) => {
  console.log('Event received:', event);
});

// Clean up
unsubscribe();
```

### Auth Service

```typescript
import { authService, loadConfig } from '@quantiya/codevibe-core';

// Set environment
loadConfig('production');
authService.setEnvironment('production');

// Login (opens browser)
const tokens = await authService.login();

// Check status
const status = await authService.getStatus();
console.log(status.authenticated, status.tokens?.email);

// Logout
await authService.logout();
```

## Keychain Storage

All sensitive data is stored in the native system keychain:

- **Service Name:** `ai.quantiya.app.codevibe`
- **Device Identity:** Stored as `device-identity` account
- **Tokens:** Stored as `tokens-{environment}` accounts (e.g., `tokens-production`)

This ensures:
- Data persists across terminal sessions
- Data is encrypted at rest by the OS
- Data survives plugin reinstalls/updates
- Device identity is shared across all CodeVibe plugins

## Security

- Device keys are ECDH P-256 key pairs
- Session keys are 256-bit AES keys
- Content is encrypted with AES-256-GCM
- Session keys are encrypted per-device using ECDH
- OAuth tokens are stored securely in native keychain

## Development

```bash
# Install dependencies
npm install

# Build
npm run build

# Watch mode
npm run watch

# Local development with plugin
npm link
cd ../codevibe-claude-plugin
npm link @quantiya/codevibe-core
```

## License

MIT