refactor: remove the billing tier model (P0.1)
The community edition has no monetization (ADR-0001). Remove the tier model (packages/shared/src/tiers.ts), the requireTier middleware and every tier gate, GET/PATCH /api/tier and the trial start, and the Stripe integration, which depends on the tier model. Personal features a tier used to limit are open to everyone: unlimited workspaces and connectors, every embedding provider including LiteLLM, per-workspace costs, Approvals. The workspace session cap is one constant (100). Team and enterprise surfaces are hidden instead of unlocked (team dock zone, Settings Team/Enterprise/Plan tabs, workspace Team tab); cloud sync reports unavailable, as it did for free users. The sidecar ignores the tier keys in config.json but does not delete them: both editions may still share ~/.waggle. Web billing UI is deleted: plan cards, upgrade and trial modals, payment success route, the KVARK sales block, tier 403 handling.
This commit is contained in:
@@ -59,7 +59,7 @@ export { createLiteLLMEmbedder, type LiteLLMEmbedderConfig } from './mind/litell
|
||||
export { createInProcessEmbedder, normalizeDimensions, type InProcessEmbedderConfig } from './mind/inprocess-embedder.js';
|
||||
export { createOllamaEmbedder, type OllamaEmbedderConfig } from './mind/ollama-embedder.js';
|
||||
export { createApiEmbedder, type ApiEmbedderConfig } from './mind/api-embedder.js';
|
||||
export { createEmbeddingProvider, EmbeddingQuotaExceededError, getMinimumTierForProvider, maxEmbedCharsForModel, capEmbedText, reembedPerText, type EmbeddingProviderConfig, type EmbeddingProviderStatus, type EmbeddingProviderType, type EmbeddingProviderInstance, type EmbeddingQuotaStatus } from './mind/embedding-provider.js';
|
||||
export { createEmbeddingProvider, maxEmbedCharsForModel, capEmbedText, reembedPerText, type EmbeddingProviderConfig, type EmbeddingProviderStatus, type EmbeddingProviderType, type EmbeddingProviderInstance, type EmbeddingQuotaStatus } from './mind/embedding-provider.js';
|
||||
export { normalizeEntityName, findDuplicates, isNoiseName, isLikelyAcronym } from './mind/entity-normalizer.js';
|
||||
export { Ontology, validateEntity, type EntitySchema, type ValidationResult } from './mind/ontology.js';
|
||||
export {
|
||||
|
||||
@@ -3,13 +3,11 @@
|
||||
* Single entry point for all embedding operations in Waggle.
|
||||
* Implements the Embedder interface — drop-in replacement everywhere.
|
||||
*
|
||||
* Tier enforcement: provider selection is gated by TIER_CAPABILITIES.embeddingProviders.
|
||||
* Quota enforcement: monthly embed count tracked in embedding_usage table.
|
||||
* Usage: the monthly embed count is tracked in the embedding_usage table (no limit).
|
||||
*/
|
||||
|
||||
import type { Embedder } from './embeddings.js';
|
||||
import type { Database as DatabaseType } from 'better-sqlite3';
|
||||
import { type Tier, TIERS, TIER_CAPABILITIES, TierError } from '@waggle/shared';
|
||||
import { createCoreLogger } from '../logger.js';
|
||||
|
||||
const log = createCoreLogger('embedding');
|
||||
@@ -19,11 +17,9 @@ export type EmbeddingProviderType = 'inprocess' | 'ollama' | 'voyage' | 'openai'
|
||||
export interface EmbeddingProviderConfig {
|
||||
provider?: EmbeddingProviderType | 'auto';
|
||||
targetDimensions?: number;
|
||||
/** User tier — gates which providers are available and monthly quota. Defaults to SOLO. */
|
||||
userTier?: Tier;
|
||||
/** User ID for quota tracking. Defaults to 'local'. */
|
||||
userId?: string;
|
||||
/** Raw SQLite database for quota tracking. Optional — quota not enforced without it. */
|
||||
/** Raw SQLite database for usage tracking. Optional — usage is not recorded without it. */
|
||||
quotaDb?: DatabaseType;
|
||||
inprocess?: { model?: string; cacheDir?: string };
|
||||
ollama?: { baseUrl?: string; model?: string };
|
||||
@@ -32,17 +28,6 @@ export interface EmbeddingProviderConfig {
|
||||
litellm?: { url: string; apiKey?: string; model?: string };
|
||||
}
|
||||
|
||||
// ── Tier enforcement helpers ──────────────────────────────────────────
|
||||
|
||||
/** Find the lowest tier that allows a given embedding provider. */
|
||||
export function getMinimumTierForProvider(provider: EmbeddingProviderType): Tier {
|
||||
for (const tier of TIERS) {
|
||||
const allowed = TIER_CAPABILITIES[tier].embeddingProviders as readonly string[];
|
||||
if (allowed.includes(provider)) return tier;
|
||||
}
|
||||
return 'ENTERPRISE';
|
||||
}
|
||||
|
||||
// ── Quota tracking ────────────────────────────────────────────────────
|
||||
|
||||
const EMBEDDING_USAGE_SCHEMA = `
|
||||
@@ -65,13 +50,6 @@ function getCurrentYearMonth(): string {
|
||||
return `${d.getFullYear()}-${String(d.getMonth() + 1).padStart(2, '0')}`;
|
||||
}
|
||||
|
||||
function getUsageCount(db: DatabaseType, userId: string, yearMonth: string): number {
|
||||
const row = db.prepare(
|
||||
'SELECT count FROM embedding_usage WHERE user_id = ? AND year_month = ?'
|
||||
).get(userId, yearMonth) as { count: number } | undefined;
|
||||
return row?.count ?? 0;
|
||||
}
|
||||
|
||||
function incrementUsage(db: DatabaseType, userId: string, yearMonth: string, amount: number): void {
|
||||
db.prepare(`
|
||||
INSERT INTO embedding_usage (user_id, year_month, count, updated_at)
|
||||
@@ -80,23 +58,7 @@ function incrementUsage(db: DatabaseType, userId: string, yearMonth: string, amo
|
||||
`).run(userId, yearMonth, amount, Date.now(), amount, Date.now());
|
||||
}
|
||||
|
||||
export class EmbeddingQuotaExceededError extends Error {
|
||||
public readonly tier: Tier;
|
||||
public readonly quota: number;
|
||||
public readonly current: number;
|
||||
public readonly upgradeUrl = 'https://waggle-os.ai/upgrade';
|
||||
|
||||
constructor(tier: Tier, quota: number, current: number) {
|
||||
super(`Embedding quota exceeded: ${current}/${quota} for ${tier} tier`);
|
||||
this.name = 'EmbeddingQuotaExceededError';
|
||||
this.tier = tier;
|
||||
this.quota = quota;
|
||||
this.current = current;
|
||||
}
|
||||
}
|
||||
|
||||
export interface EmbeddingQuotaStatus {
|
||||
tier: Tier;
|
||||
quota: number;
|
||||
used: number;
|
||||
remaining: number;
|
||||
@@ -124,7 +86,7 @@ export interface EmbeddingProviderInstance extends Embedder {
|
||||
getStatus(): EmbeddingProviderStatus;
|
||||
getActiveProvider(): EmbeddingProviderType;
|
||||
reprobe(): Promise<EmbeddingProviderStatus>;
|
||||
/** Get current quota status for the user. Returns unlimited values if no quotaDb configured. */
|
||||
/** Get the usage status. Embedding is unlimited, so quota and remaining are -1. */
|
||||
getQuotaStatus(): EmbeddingQuotaStatus;
|
||||
}
|
||||
|
||||
@@ -289,20 +251,10 @@ async function probeProvider(
|
||||
export async function createEmbeddingProvider(config?: EmbeddingProviderConfig): Promise<EmbeddingProviderInstance> {
|
||||
const cfg: EmbeddingProviderConfig = { provider: 'auto', targetDimensions: 1024, ...config };
|
||||
const dims = cfg.targetDimensions ?? 1024;
|
||||
const userTier: Tier = cfg.userTier ?? 'FREE';
|
||||
const userId = cfg.userId ?? 'local';
|
||||
const quotaDb = cfg.quotaDb ?? null;
|
||||
// Tier enforcement is normally ON only when userTier is explicitly passed.
|
||||
// WAGGLE_EVAL_MODE=1 disables it unconditionally — the PA v5 eval harness
|
||||
// sets this so user-tier gates never confound measurement validity. This
|
||||
// env-var is eval-path-only; never set it in production code paths.
|
||||
// See PromptAssembler v5 brief §11.3.
|
||||
const evalModeActive = process.env.WAGGLE_EVAL_MODE === '1';
|
||||
const tierEnforced = evalModeActive ? false : cfg.userTier !== undefined;
|
||||
const tierCaps = TIER_CAPABILITIES[userTier];
|
||||
const allowedProviders = tierCaps.embeddingProviders as readonly string[];
|
||||
|
||||
// Initialize quota table if DB provided
|
||||
// Initialize the usage table if DB provided
|
||||
if (quotaDb) {
|
||||
ensureQuotaTable(quotaDb);
|
||||
}
|
||||
@@ -315,21 +267,6 @@ export async function createEmbeddingProvider(config?: EmbeddingProviderConfig):
|
||||
let availableProviders: EmbeddingProviderType[] = [];
|
||||
let probeTimestamp = new Date().toISOString();
|
||||
|
||||
/** Check quota before embedding. Throws if exceeded. Warns at 80%. */
|
||||
function checkQuota(count: number): void {
|
||||
if (!quotaDb || !tierEnforced) return;
|
||||
const quota = tierCaps.embeddingQuotaPerMonth;
|
||||
if (quota === -1) return; // unlimited
|
||||
const ym = getCurrentYearMonth();
|
||||
const used = getUsageCount(quotaDb, userId, ym);
|
||||
if (used + count > quota) {
|
||||
throw new EmbeddingQuotaExceededError(userTier, quota, used);
|
||||
}
|
||||
if (used + count >= quota * 0.8) {
|
||||
log.warn(`Embedding quota warning: ${used + count}/${quota} (${Math.round(((used + count) / quota) * 100)}%) for ${userTier} tier`);
|
||||
}
|
||||
}
|
||||
|
||||
/** Record usage after successful embedding. */
|
||||
function recordUsage(count: number): void {
|
||||
if (!quotaDb) return;
|
||||
@@ -345,11 +282,6 @@ export async function createEmbeddingProvider(config?: EmbeddingProviderConfig):
|
||||
const requestedProvider = cfg.provider ?? 'auto';
|
||||
|
||||
if (requestedProvider !== 'auto' && requestedProvider !== 'mock') {
|
||||
// Explicit provider — tier-check only when tier is explicitly configured
|
||||
if (tierEnforced && !allowedProviders.includes(requestedProvider)) {
|
||||
const required = getMinimumTierForProvider(requestedProvider);
|
||||
throw new TierError(required, userTier);
|
||||
}
|
||||
log.info(`Trying ${requestedProvider}...`);
|
||||
const result = await probeProvider(requestedProvider, cfg);
|
||||
if (result) {
|
||||
@@ -358,15 +290,10 @@ export async function createEmbeddingProvider(config?: EmbeddingProviderConfig):
|
||||
log.info(`Trying ${requestedProvider}... OK`);
|
||||
}
|
||||
} else if (requestedProvider === 'auto') {
|
||||
// Auto: iterate chain, skip providers not allowed by tier
|
||||
// Auto: iterate the chain in order
|
||||
const chain: EmbeddingProviderType[] = ['inprocess', 'ollama', 'voyage', 'openai'];
|
||||
|
||||
for (const providerType of chain) {
|
||||
// Tier gate — skip providers not allowed (only when tier is explicitly configured)
|
||||
if (tierEnforced && !allowedProviders.includes(providerType)) {
|
||||
log.info(`Skipping ${providerType} (not available on ${userTier} tier)`);
|
||||
continue;
|
||||
}
|
||||
// Skip API providers without keys
|
||||
if (providerType === 'voyage' && !cfg.voyage?.apiKey) {
|
||||
log.info('Skipping voyage (no API key in Vault)');
|
||||
@@ -439,8 +366,6 @@ export async function createEmbeddingProvider(config?: EmbeddingProviderConfig):
|
||||
try {
|
||||
await runProbe();
|
||||
} catch (err) {
|
||||
// Re-throw tier errors — these are intentional enforcement, not probe failures
|
||||
if (err instanceof TierError) throw err;
|
||||
lastError = err instanceof Error ? err.message : String(err);
|
||||
activeEmbedder = createMockEmbedder(dims);
|
||||
activeType = 'mock';
|
||||
@@ -455,7 +380,6 @@ export async function createEmbeddingProvider(config?: EmbeddingProviderConfig):
|
||||
dimensions: dims,
|
||||
|
||||
async embed(text: string): Promise<Float32Array> {
|
||||
checkQuota(1);
|
||||
// Cap input to the active model's context so the backend never rejects
|
||||
// an oversized frame. Reverse-ported from OSS hive-mind (oss-drift triage R5, 2026-06-11).
|
||||
const capped = capEmbedText(text, maxEmbedCharsForModel(activeModelName));
|
||||
@@ -464,7 +388,6 @@ export async function createEmbeddingProvider(config?: EmbeddingProviderConfig):
|
||||
recordUsage(1);
|
||||
return result;
|
||||
} catch (err) {
|
||||
if (err instanceof EmbeddingQuotaExceededError) throw err;
|
||||
log.warn(`Embedding failed with ${activeType}, falling back to mock: ${(err as Error).message}`);
|
||||
lastError = (err as Error).message;
|
||||
const fallback = mockEmbed(capped, dims);
|
||||
@@ -475,7 +398,6 @@ export async function createEmbeddingProvider(config?: EmbeddingProviderConfig):
|
||||
|
||||
async embedBatch(texts: string[]): Promise<Float32Array[]> {
|
||||
if (texts.length === 0) return [];
|
||||
checkQuota(texts.length);
|
||||
// Cap each input first so one oversized frame can't make the backend
|
||||
// reject the whole request. Reverse-ported from OSS hive-mind
|
||||
// (oss-drift triage R5, 2026-06-11).
|
||||
@@ -485,7 +407,6 @@ export async function createEmbeddingProvider(config?: EmbeddingProviderConfig):
|
||||
recordUsage(texts.length);
|
||||
return result;
|
||||
} catch (err) {
|
||||
if (err instanceof EmbeddingQuotaExceededError) throw err;
|
||||
// Skip-not-abort: re-embed per-text so a single backend-rejected input
|
||||
// degrades alone instead of mock-poisoning the WHOLE batch.
|
||||
log.warn(`Batch embedding failed with ${activeType}, re-embedding per-text: ${(err as Error).message}`);
|
||||
@@ -517,19 +438,7 @@ export async function createEmbeddingProvider(config?: EmbeddingProviderConfig):
|
||||
},
|
||||
|
||||
getQuotaStatus(): EmbeddingQuotaStatus {
|
||||
const quota = tierCaps.embeddingQuotaPerMonth;
|
||||
if (!quotaDb || quota === -1) {
|
||||
return { tier: userTier, quota: -1, used: 0, remaining: -1, percentage: 0, resetsAt: getNextMonthReset() };
|
||||
}
|
||||
const used = getUsageCount(quotaDb, userId, getCurrentYearMonth());
|
||||
return {
|
||||
tier: userTier,
|
||||
quota,
|
||||
used,
|
||||
remaining: Math.max(0, quota - used),
|
||||
percentage: Math.round((used / quota) * 100),
|
||||
resetsAt: getNextMonthReset(),
|
||||
};
|
||||
return { quota: -1, used: 0, remaining: -1, percentage: 0, resetsAt: getNextMonthReset() };
|
||||
},
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user