feat: add AI cost tracking for social publisher content and hashtag generation
Some checks are pending
NordaBiz Tests / Unit & Integration Tests (push) Waiting to run
NordaBiz Tests / E2E Tests (Playwright) (push) Blocked by required conditions
NordaBiz Tests / Smoke Tests (Production) (push) Blocked by required conditions
NordaBiz Tests / Send Failure Notification (push) Blocked by required conditions

Pass user_id, company_id, and feature name to generate_text() calls:
- 'social_publisher_content' for post content generation
- 'social_publisher_hashtags' for hashtag generation
All costs logged to AIUsageLog and AIAPICostLog with user/company context.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Maciej Pienczyn 2026-02-19 09:58:29 +01:00
parent 95adbd8896
commit 0fa2ea9e14
3 changed files with 32 additions and 7 deletions

View File

@ -369,7 +369,12 @@ def social_publisher_generate():
for key, default in defaults.items():
context.setdefault(key, default)
content, hashtags, model = social_publisher.generate_content(post_type, context, tone=tone, ai_model=ai_model)
pub_company_id = request.json.get('publishing_company_id')
content, hashtags, model = social_publisher.generate_content(
post_type, context, tone=tone, ai_model=ai_model,
user_id=current_user.id,
company_id=int(pub_company_id) if pub_company_id else (int(company_id) if company_id else None),
)
return jsonify({'success': True, 'content': content, 'hashtags': hashtags, 'model': model})
except Exception as e:
logger.error(f"AI generation failed: {e}")
@ -391,7 +396,10 @@ def social_publisher_generate_hashtags():
return jsonify({'success': False, 'error': 'Wpisz najpierw tresc posta, aby wygenerowac hashtagi.'}), 400
try:
hashtags, model = social_publisher.generate_hashtags(content, post_type, ai_model=ai_model)
hashtags, model = social_publisher.generate_hashtags(
content, post_type, ai_model=ai_model,
user_id=current_user.id,
)
return jsonify({'success': True, 'hashtags': hashtags, 'model': model})
except Exception as e:
logger.error(f"Hashtag generation failed: {e}")

View File

@ -534,7 +534,8 @@ class SocialPublisherService:
return content, ' '.join(unique_tags)
def generate_content(self, post_type: str, context: dict, tone: str = None,
ai_model: str = None) -> Tuple[str, str, str]:
ai_model: str = None,
user_id: int = None, company_id: int = None) -> Tuple[str, str, str]:
"""Generate post content using AI.
Args:
@ -542,6 +543,8 @@ class SocialPublisherService:
context: Dict with template variables
tone: One of POST_TONES keys (default: DEFAULT_TONE)
ai_model: One of AI_MODELS keys (default: DEFAULT_AI_MODEL)
user_id: User ID for cost tracking
company_id: Company ID for cost tracking
Returns:
(content: str, hashtags: str, ai_model: str)
@ -564,9 +567,15 @@ class SocialPublisherService:
# Select model
model_key = ai_model if ai_model in AI_MODELS else DEFAULT_AI_MODEL
# Generate with Gemini
# Generate with Gemini — with cost tracking
from gemini_service import generate_text
result = generate_text(prompt, model=model_key)
result = generate_text(
prompt, model=model_key,
feature='social_publisher_content',
user_id=user_id,
company_id=company_id,
related_entity_type='social_post',
)
if not result:
raise RuntimeError("AI nie wygenerował treści. Spróbuj ponownie.")
@ -577,7 +586,8 @@ class SocialPublisherService:
return content, hashtags, model_label
def generate_hashtags(self, content: str, post_type: str = '',
ai_model: str = None) -> Tuple[str, str]:
ai_model: str = None,
user_id: int = None, company_id: int = None) -> Tuple[str, str]:
"""Generate hashtags for given post content using AI.
Returns:
@ -599,7 +609,13 @@ Zasady:
- Odpowiedz WYŁĄCZNIE hashtagami, nic więcej"""
from gemini_service import generate_text
result = generate_text(prompt, model=model_key)
result = generate_text(
prompt, model=model_key,
feature='social_publisher_hashtags',
user_id=user_id,
company_id=company_id,
related_entity_type='social_post',
)
if not result:
raise RuntimeError("AI nie wygenerował hashtagów. Spróbuj ponownie.")

View File

@ -467,6 +467,7 @@
post_type: postType,
company_id: companyId || null,
event_id: eventId || null,
publishing_company_id: document.getElementById('publishing_company_id')?.value || null,
tone: tone,
ai_model: aiModel,
custom_context: customContext