mirror of
https://github.com/Memo-2023/mana-monorepo.git
synced 2026-05-20 04:43:40 +02:00
Create @manacore/shared-credit-service with:
- createCreditService() factory function
- Credit balance fetching with auth token support
- Operation pricing with caching (30min default)
- Fallback pricing for all standard operations
- Credit check before operations
- Credit consumption notification system
- Sync and async cost calculation methods
Standard operations supported:
- Memoro: transcription, headline, memory, blueprint, etc.
- Maerchenzauber: character creation, story creation
- ManaDeck: deck creation, card generation, AI review
- Generic: AI processing, export, import
Apps can use this service with their own configuration:
```ts
const creditService = createCreditService({
apiUrl: 'https://api.myapp.com',
getAuthToken: () => auth.getToken()
});
```
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
75 lines
1.9 KiB
TypeScript
75 lines
1.9 KiB
TypeScript
/**
|
|
* @manacore/shared-credit-service
|
|
*
|
|
* Shared credit/mana service for the ManaCore monorepo.
|
|
*
|
|
* Provides:
|
|
* - Credit balance fetching and caching
|
|
* - Operation pricing with fallbacks
|
|
* - Credit check before operations
|
|
* - Credit consumption notifications
|
|
*
|
|
* @example Basic usage
|
|
* ```ts
|
|
* import { createCreditService } from '@manacore/shared-credit-service';
|
|
*
|
|
* const creditService = createCreditService({
|
|
* apiUrl: 'https://api.myapp.com',
|
|
* getAuthToken: async () => localStorage.getItem('token')
|
|
* });
|
|
*
|
|
* // Initialize on app startup
|
|
* await creditService.initialize();
|
|
*
|
|
* // Check balance before operation
|
|
* const check = await creditService.checkOperationBalance('STORY_CREATION');
|
|
* if (!check.hasEnoughCredits) {
|
|
* showInsufficientCreditsModal(check.deficit);
|
|
* return;
|
|
* }
|
|
*
|
|
* // After successful operation, notify listeners
|
|
* creditService.triggerCreditUpdate(10, 'STORY_CREATION');
|
|
* ```
|
|
*
|
|
* @example With Svelte store integration
|
|
* ```ts
|
|
* // creditService.ts
|
|
* import { createCreditService } from '@manacore/shared-credit-service';
|
|
* import { auth } from '$lib/stores/auth';
|
|
*
|
|
* export const creditService = createCreditService({
|
|
* apiUrl: import.meta.env.VITE_API_URL,
|
|
* pricingEndpoint: '/credits/pricing',
|
|
* getAuthToken: () => auth.getToken()
|
|
* });
|
|
*
|
|
* // creditStore.svelte.ts
|
|
* import { creditService } from './creditService';
|
|
*
|
|
* let balance = $state<number>(0);
|
|
*
|
|
* // Listen for credit updates
|
|
* creditService.onCreditUpdate((consumed) => {
|
|
* balance -= consumed;
|
|
* });
|
|
* ```
|
|
*/
|
|
|
|
// Factory function
|
|
export { createCreditService } from './createCreditService';
|
|
export type { CreditService } from './createCreditService';
|
|
|
|
// Types
|
|
export type {
|
|
CreditServiceConfig,
|
|
CreditBalance,
|
|
CreditCheckResponse,
|
|
CreditConsumptionResponse,
|
|
PricingResponse,
|
|
CreditUpdateCallback,
|
|
StandardOperationType
|
|
} from './types';
|
|
|
|
// Constants
|
|
export { DEFAULT_OPERATION_PRICING } from './types';
|