e307a048d1
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>
38 lines
1.7 KiB
SQL
38 lines
1.7 KiB
SQL
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);
|