feat(server): meter AI point usage across tenant AI features
Adds an ai_point_usage_logs ledger and a reserve/refund/complete pipeline that charges AI points for article selection optimize, template analyze /title/outline, and KOL prompt generate/optimize. Pending reservations are reconciled when the kol-assist worker and template-assist tasks finish or fail, so points refund automatically on errors. Workspace now exposes the AI quota status and a paginated usage ledger. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,3 @@
|
||||
DROP INDEX IF EXISTS idx_quota_reservations_tenant_type_created_status;
|
||||
|
||||
DROP TABLE IF EXISTS ai_point_usage_logs;
|
||||
@@ -0,0 +1,37 @@
|
||||
CREATE TABLE ai_point_usage_logs (
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
tenant_id BIGINT NOT NULL REFERENCES tenants(id),
|
||||
operator_id BIGINT REFERENCES users(id),
|
||||
quota_reservation_id BIGINT REFERENCES quota_reservations(id),
|
||||
usage_type VARCHAR(64) NOT NULL,
|
||||
resource_type VARCHAR(64),
|
||||
resource_id BIGINT,
|
||||
resource_uid VARCHAR(128),
|
||||
request_chars INT NOT NULL DEFAULT 0,
|
||||
base_chars INT NOT NULL DEFAULT 1000,
|
||||
points INT NOT NULL,
|
||||
status VARCHAR(20) NOT NULL DEFAULT 'pending',
|
||||
model VARCHAR(128),
|
||||
metadata_json JSONB NOT NULL DEFAULT '{}'::jsonb,
|
||||
error_message TEXT,
|
||||
completed_at TIMESTAMPTZ,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
||||
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
||||
CONSTRAINT chk_ai_point_usage_status
|
||||
CHECK (status IN ('pending', 'completed', 'refunded', 'failed'))
|
||||
);
|
||||
|
||||
CREATE INDEX idx_ai_point_usage_tenant_created
|
||||
ON ai_point_usage_logs(tenant_id, created_at DESC);
|
||||
|
||||
CREATE INDEX idx_ai_point_usage_tenant_operator_created
|
||||
ON ai_point_usage_logs(tenant_id, operator_id, created_at DESC);
|
||||
|
||||
CREATE INDEX idx_ai_point_usage_reservation
|
||||
ON ai_point_usage_logs(quota_reservation_id);
|
||||
|
||||
CREATE INDEX idx_ai_point_usage_resource
|
||||
ON ai_point_usage_logs(tenant_id, resource_type, resource_id, resource_uid);
|
||||
|
||||
CREATE INDEX idx_quota_reservations_tenant_type_created_status
|
||||
ON quota_reservations(tenant_id, quota_type, created_at, status);
|
||||
Reference in New Issue
Block a user