Services
- summary:
תיעוד של שירותי הליבה של המערכת.
Code Service
Code Service Module
שירות עיבוד וניתוח קוד עבור Code Keeper Bot.
מודול זה מספק wrapper לפונקציונליות עיבוד קוד, כולל: - זיהוי שפות תכנות - הדגשת תחביר - ניתוח קוד - חיפוש בקוד
- services.code_service.detect_language(code, filename)[מקור]
מקור אמת לזיהוי שפה: דטקטור דומייני. נשמרים fallback-ים ישנים לתאימות, אך החלטה סופית מגיעה מהדומיין כאשר אפשר.
- services.code_service.validate_code_input(code, file_name, user_id)[מקור]
בודק ומנקה קלט קוד.
- services.code_service.clean_pasted_code(code, file_name)[מקור]
הניקוי המינימלי לקוד שמודבק בבוט — הדלת של ה-handlers אליו.
ה-handlers אינם מייבאים את שכבת הדומיין (
tests/unit/architecture/test_layer_boundaries.py), ולכן הם עוברים כאן. מה מנוקה ומה לא — ב-docstring שלsrc.domain.services.code_normalizer.clean_pasted_code(); כאן רק נקבע אם הקובץ הוא Markdown, לפי השם (is_markdown_filename()).כל זרימה בבוט קוראת לזה פעם אחת, ממש לפני השמירה, כשגם הקוד וגם השם ידועים.
Noneהוא טקסט ריק:context.user_dataיכול לאבד את הקוד בין שלבי השיחה, והזרימות תמיד שמרו אז מחרוזת ריקה.
- services.code_service.format_cleanup_notice(cleanup)[מקור]
השורות שהבוט מוסיף להודעת ההצלחה: מה נוקה, ואזהרה על תווי כיווניות.
מחרוזת ריקה כשאין מה לומר. בלי השורה הזו הניקוי היה קורה בשקט, וזה בדיוק מה שהוחלף כאן. הטקסט אינו מכיל אף תו מבין
_*`[]<>&: הוא נכנס להודעות עםparse_modeשל Markdown וגם של HTML, ותו כזה היה משנה את העיצוב או מפיל את השליחה ב-BadRequest.- Return type:
- פרמטרים:
cleanup (PasteCleanup)
- services.code_service.format_bidi_warning(line_numbers)[מקור]
המשפט האחד על תווי embedding, override ו-isolate — לבוט ולוובאפ.
מחרוזת ריקה כשאין שורות. בלי אייקון ובלי סיומת: הבוט מוסיף ”⚠️“ ו“לא נגעתי בהם“ (
format_cleanup_notice()), והוובאפ מציג אותו בבאנר משלו (bidi_warning_for_display()). כך הניסוח חי במקום אחד.
- services.code_service.bidi_warning_for_display(code)[מקור]
האזהרה שמוצגת לצד קוד בוובאפ, או מחרוזת ריקה כשאין בו תווים כאלה.
התווים נשמרים כמו שנשלחו, ולכן עמוד שמציג קוד צריך להזהיר עליהם בעצמו. אילו עמודים כבר מזהירים ואילו עוד לא — ב-
docs/quality/code-normalization.md, ו-tests/test_bidi_warning_pages_match_the_doc.pyמשווה את הרשימה לקוד. מספרי השורות הם של הטקסט שמקבלים כאן; עמוד שממספר אחרת (Pygments חותך שורות ריקות מההתחלה) מעביר את הטקסט שהוא מציג.
- services.code_service.analyze_code(code, language)[מקור]
מבצע ניתוח על קטע קוד עבור שפה נתונה.
- services.code_service.extract_functions(code, language)[מקור]
Extract function definitions from code.
- services.code_service.get_code_stats(code)[מקור]
Compute simple statistics for a code snippet.
GitHub Service
Backup Service
- services.backup_service.save_backup_bytes(data, metadata)[מקור]
- services.backup_service.restore_from_backup(user_id, backup_path, overwrite=True, purge=True)[מקור]
AI Explain Service
שירות observability שמפיק הסברים להתראות על בסיס Anthropic Claude עם fallback בין מודלים, סניטציה של לוגים וחיתוך שדות רגישים לפני שליחת ההקשר.
ראו גם 🧠 Observability AI Explain API.
Community Library Service
ניהול ספריית קוד קהילתית: קליטת מועמדים, אישור/דחייה מול MongoDB, רשימות פומביות עם חיפוש, תגיות וסימון Featured.
Image Generator Service
מחולל תמונות קוד מקצועיות עם Pygments, Playwright/WeasyPrint/PIL, מגוון תמות (dark/light/github/monokai וכו«) ותמיכה בבחירת פונטים.
שירות ליצירת תמונות קוד עם היילייטינג (Playwright → WeasyPrint → PIL).
Code Image Generator Service
מסלול מועדף:
Playwright (אם מותקן) – רינדור HTML בדפדפן headless באיכות גבוהה. Playwright רץ ב-subprocess נפרד כדי להימנע מקונפליקט עם gevent monkey patching.
WeasyPrint (אם מותקן) – רינדור HTML איכותי.
PIL fallback – ציור ידני עם סקייל x3 לשיפור חדות.
- class services.image_generator.TechGuideStyle[מקור]
Bases:
Styleערכת הצבעים של Tech Guide: רקע כהה מאוד, צבעים חזקים וברורים לקוד.
- default_style = ''
- styles = {(): '', ('Comment',): '#6a9955 italic', ('Comment', 'Hashbang'): '', ('Comment', 'Multiline'): '', ('Comment', 'Preproc'): '', ('Comment', 'PreprocFile'): '', ('Comment', 'Single'): '', ('Comment', 'Special'): '', ('Error',): '#f44747', ('Escape',): '', ('Generic',): '', ('Generic', 'Deleted'): '', ('Generic', 'Emph'): '', ('Generic', 'EmphStrong'): '', ('Generic', 'Error'): '', ('Generic', 'Heading'): '#569cd6 bold', ('Generic', 'Inserted'): '', ('Generic', 'Output'): '', ('Generic', 'Prompt'): '', ('Generic', 'Strong'): '', ('Generic', 'Subheading'): '#569cd6 bold', ('Generic', 'Traceback'): '', ('Keyword',): '#569cd6', ('Keyword', 'Constant'): '', ('Keyword', 'Declaration'): '', ('Keyword', 'Namespace'): '', ('Keyword', 'Pseudo'): '', ('Keyword', 'Reserved'): '', ('Keyword', 'Type'): '#4ec9b0', ('Literal',): '', ('Literal', 'Date'): '', ('Literal', 'Number'): '#b5cea8', ('Literal', 'Number', 'Bin'): '', ('Literal', 'Number', 'Float'): '', ('Literal', 'Number', 'Hex'): '', ('Literal', 'Number', 'Integer'): '', ('Literal', 'Number', 'Integer', 'Long'): '', ('Literal', 'Number', 'Oct'): '', ('Literal', 'String'): '#ce9178', ('Literal', 'String', 'Affix'): '', ('Literal', 'String', 'Backtick'): '', ('Literal', 'String', 'Char'): '', ('Literal', 'String', 'Delimiter'): '', ('Literal', 'String', 'Doc'): '', ('Literal', 'String', 'Double'): '', ('Literal', 'String', 'Escape'): '', ('Literal', 'String', 'Heredoc'): '', ('Literal', 'String', 'Interpol'): '', ('Literal', 'String', 'Other'): '', ('Literal', 'String', 'Regex'): '', ('Literal', 'String', 'Single'): '', ('Literal', 'String', 'Symbol'): '', ('Name',): '#dcdcaa', ('Name', 'Attribute'): '', ('Name', 'Builtin'): '', ('Name', 'Builtin', 'Pseudo'): '', ('Name', 'Class'): '#4ec9b0 bold', ('Name', 'Constant'): '', ('Name', 'Decorator'): '', ('Name', 'Entity'): '', ('Name', 'Exception'): '', ('Name', 'Function'): '#dcdcaa', ('Name', 'Function', 'Magic'): '', ('Name', 'Label'): '', ('Name', 'Namespace'): '', ('Name', 'Other'): '', ('Name', 'Property'): '', ('Name', 'Tag'): '', ('Name', 'Variable'): '', ('Name', 'Variable', 'Class'): '', ('Name', 'Variable', 'Global'): '', ('Name', 'Variable', 'Instance'): '', ('Name', 'Variable', 'Magic'): '', ('Operator',): '#d4d4d4', ('Operator', 'Word'): '', ('Other',): '', ('Punctuation',): '', ('Punctuation', 'Marker'): '', ('Text',): '#d4d4d4', ('Text', 'Whitespace'): ''}
Style definitions for individual token types.
- class services.image_generator.CodeImageGenerator(style='monokai', theme='dark', *, font_family=None)[מקור]
Bases:
objectמחולל תמונות לקוד עם הדגשת תחביר.
מבוסס על PIL לציור טקסט ואזורי מספרי שורות, ועל Pygments ליצירת HTML מודגש שממנו מחולצים צבעים בסיסיים.
- DEFAULT_WIDTH = 1200
- DEFAULT_PADDING = 40
- LINE_HEIGHT = 24
- FONT_SIZE = 14
- LINE_NUMBER_WIDTH = 60
- LOGO_SIZE = (80, 20)
- LOGO_PADDING = 10
- CARD_MARGIN = 18
- TITLE_BAR_HEIGHT = 28
- CODE_GUTTER_SPACING = 20
- THEMES = {'banner_tech': {'background': '#1d1a26', 'border': '#4b4363', 'line_number_bg': '#14121a', 'line_number_text': '#6e6290', 'text': '#d4d4d4', 'watermark': '#7cd827'}, 'dark': {'background': '#1e1e1e', 'border': '#3e3e42', 'line_number_bg': '#252526', 'line_number_text': '#858585', 'text': '#d4d4d4'}, 'dracula': {'background': '#282a36', 'border': '#44475a', 'line_number_bg': '#1e1f29', 'line_number_text': '#6272a4', 'text': '#f8f8f2'}, 'github': {'background': '#0d1117', 'border': '#30363d', 'line_number_bg': '#161b22', 'line_number_text': '#7d8590', 'text': '#c9d1d9'}, 'gruvbox': {'background': '#282828', 'border': '#504945', 'line_number_bg': '#3c3836', 'line_number_text': '#bdae93', 'text': '#ebdbb2'}, 'light': {'background': '#ffffff', 'border': '#e0e0e0', 'line_number_bg': '#f5f5f5', 'line_number_text': '#999999', 'text': '#333333'}, 'monokai': {'background': '#272822', 'border': '#49483e', 'line_number_bg': '#3e3d32', 'line_number_text': '#75715e', 'text': '#f8f8f2'}, 'one_dark': {'background': '#282c34', 'border': '#3b4048', 'line_number_bg': '#21252b', 'line_number_text': '#5c6370', 'text': '#abb2bf'}}
- __init__(style='monokai', theme='dark', *, font_family=None)[מקור]
- configure(*, style=None, theme=None, font_family=None)[מקור]
עדכון דינמי של ההעדפות בלי יצירת מופע חדש.
- optimize_image_size(img)[מקור]
- פרמטרים:
img (Image.Image)
- Return type:
Image.Image
- save_optimized_png(img)[מקור]
Always return PNG for crisp code images; keep optimization, avoid JPEG.
- פרמטרים:
img (Image.Image)
- Return type:
Snippet Library Service
מנהל את ספריית הסניפטים המובנית: Built-in snippets, שליחת הצעות, אישור מנהלים, חיפוש לפי שפה/תגיות וסנכרון עם ה-Repo הקיים.
Observability HTTP Service
עטיפה ליצירת בקשות HTTP מאובטחות אל Grafana/Prometheus תוך מניעת SSRF, אימות DNS Rebinding ושימור Host header עבור SNI.
- exception services.observability_http.SecurityError[מקור]
Bases:
RuntimeErrorRaised when an unsafe network request is detected.
- services.observability_http.is_private_ip(ip)[מקור]
Return True if the IP address belongs to a private or unsafe range.
- services.observability_http.resolve_and_validate_domain(domain)[מקור]
Resolve the given domain and verify that all IPs are public.
- class services.observability_http.HostHeaderSSLAdapter(hostname, *args, **kwargs)[מקור]
Bases:
HTTPAdapterAdapter that preserves the original hostname for HTTPS + SNI.
- פרמטרים:
hostname (str)
- init_poolmanager(*args, **kwargs)[מקור]
Initializes a urllib3 PoolManager.
This method should not be called from user code, and is only exposed for use when subclassing the
HTTPAdapter.- פרמטרים:
connections – The number of urllib3 connection pools to cache.
maxsize – The maximum number of connections to save in the pool.
block – Block when no free connections are available.
pool_kwargs – Extra keyword arguments used to initialize the Pool Manager.
- proxy_manager_for(*args, **kwargs)[מקור]
Return urllib3 ProxyManager for the given proxy.
This method should not be called from user code, and is only exposed for use when subclassing the
HTTPAdapter.- פרמטרים:
proxy – The proxy to return a urllib3 ProxyManager for.
proxy_kwargs – Extra keyword arguments used to configure the Proxy Manager.
- Returns:
ProxyManager
- Return type:
urllib3.ProxyManager
- services.observability_http.fetch_url_securely(url, *, timeout=10, allow_redirects=False, headers=None)[מקור]
Fetch an already-built URL while protecting against SSRF/DNS rebinding.
- services.observability_http.fetch_graph_securely(graph_url_template, *, timeout=10, allow_redirects=False, headers=None, **url_params)[מקור]
Fetch a graph/JSON endpoint while protecting against DNS rebinding / SSRF.
- פרמטרים:
graph_url_template (
str) – URL template (format-style) from configuration.timeout (
int) – Request timeout in seconds.allow_redirects (
bool) – Whether to follow redirects (default False).headers (
Optional[Dict[str,str]]) – Optional base headers to attach to the request.**url_params – Values used to render the template.
- Return type: