Files
openccb/services/lms-service/migrations/20260320000001_add_log_ai_usage_function.sql
T
Nurfog 2ff06ee7ae feat: i18n full support, responsive UI, multi-model AI config, and bug fixes
Major Features:
- Internationalization (i18n) with auto-detection for ES/EN/PT
- Mobile-first responsive design for Studio and Experience
- Multi-model AI configuration (llama3.2:3b, qwen3.5:9b, gpt-oss:latest)
- Course language configuration (auto-detect or fixed per course)

Backend Changes:
- shared/common: ModelType enum for intelligent model selection
- LMS: log_ai_usage function migration (fix chat tutor 500 error)
- LMS/CMS: course language config fields (language_setting, fixed_language)
- LMS: /courses/{id}/language-config endpoint for language detection

Frontend Changes:
- Experience: Enhanced i18n with browser language detection
- Experience: Audio recording with HTTPS check and error handling
- Studio: Memory game with unique pair IDs and debug logging
- Studio: Expanded translations (250+ keys for ES, EN, PT)
- Both: Language selector in headers (mobile responsive)

Documentation:
- AI_MODELS_CONFIG.md: Multi-model configuration guide
- RESPONSIVIDAD_GUIA.md: Mobile-first design patterns
- I18N_RESPONSIVIDAD_IMPLEMENTACION.md: Implementation details
- DEBUG_AUDIO_RECORDING.md: Audio troubleshooting guide
- DEBUG_MEMORY_GAME.md: Memory game debugging steps

Bug Fixes:
- Fix chat tutor 500 error (missing log_ai_usage function)
- Fix audio recording (HTTPS check, browser compatibility)
- Fix memory game pair IDs (unique ID generation)
- Fix HotspotBlock TypeScript errors

Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>
2026-03-23 12:24:22 -03:00

68 lines
2.2 KiB
PL/PgSQL

-- AI Usage Tracking: Add log_ai_usage function
-- Required for chat with tutor and other AI features
-- Create ai_usage_logs table if not exists
CREATE TABLE IF NOT EXISTS ai_usage_logs (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
user_id UUID NOT NULL,
organization_id UUID NOT NULL,
tokens_used INTEGER NOT NULL,
input_tokens INTEGER NOT NULL,
output_tokens INTEGER NOT NULL,
endpoint VARCHAR(255) NOT NULL,
model VARCHAR(100) NOT NULL,
request_type VARCHAR(50) NOT NULL,
request_metadata JSONB,
estimated_cost_usd NUMERIC(10, 6),
created_at TIMESTAMPTZ DEFAULT NOW(),
prompt TEXT,
response TEXT
);
-- Create indexes for performance
CREATE INDEX IF NOT EXISTS idx_ai_usage_logs_user_id ON ai_usage_logs(user_id);
CREATE INDEX IF NOT EXISTS idx_ai_usage_logs_org_id ON ai_usage_logs(organization_id);
CREATE INDEX IF NOT EXISTS idx_ai_usage_logs_created_at ON ai_usage_logs(created_at);
CREATE INDEX IF NOT EXISTS idx_ai_usage_logs_endpoint ON ai_usage_logs(endpoint);
-- Create log_ai_usage function
CREATE OR REPLACE FUNCTION log_ai_usage(
p_user_id UUID,
p_org_id UUID,
p_tokens INTEGER,
p_input_tokens INTEGER,
p_output_tokens INTEGER,
p_endpoint VARCHAR,
p_model VARCHAR,
p_request_type VARCHAR,
p_metadata JSONB,
p_prompt TEXT DEFAULT NULL,
p_response TEXT DEFAULT NULL
)
RETURNS UUID AS $$
DECLARE
v_log_id UUID;
v_cost NUMERIC(10, 6);
BEGIN
-- Calculate estimated cost (OpenAI-like pricing)
v_cost := (p_input_tokens::NUMERIC * 0.000001) + (p_output_tokens::NUMERIC * 0.000003);
INSERT INTO ai_usage_logs (
user_id, organization_id, tokens_used, input_tokens, output_tokens,
endpoint, model, request_type, request_metadata, estimated_cost_usd,
prompt, response
)
VALUES (
p_user_id, p_org_id, p_tokens, p_input_tokens, p_output_tokens,
p_endpoint, p_model, p_request_type, p_metadata, v_cost,
p_prompt, p_response
)
RETURNING id INTO v_log_id;
RETURN v_log_id;
END;
$$ LANGUAGE plpgsql;
COMMENT ON COLUMN ai_usage_logs.prompt IS 'The actual prompt sent to the AI model';
COMMENT ON COLUMN ai_usage_logs.response IS 'The AI model response content';