File size: 1,812 Bytes
99c6072
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a76c39e
99c6072
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
"""
title: Context Detector
author: nerdur
version: 0.1
description: Automatski prepoznaje kontekst svake poruke i injektuje tag u system prompt.
"""

from pydantic import BaseModel
from typing import Optional


class Filter:
    class Valves(BaseModel):
        enabled: bool = True

    def __init__(self):
        self.valves = self.Valves()

    def inlet(self, body: dict, __user__: Optional[dict] = None) -> dict:
        if not self.valves.enabled:
            return body

        messages = body.get("messages", [])
        if not messages:
            return body

        last_msg = messages[-1].get("content", "").lower()

        context_map = {
            "QUESTION": ["što", "kako", "zašto", "kada", "gdje", "koji", "what", "how", "why", "when", "where"],
            "CODE": ["kod", "funkcija", "debug", "greška", "error", "script", "python", "javascript", "code"],
            "SEARCH": ["pronađi", "traži", "pretraži", "find", "search", "lookup"],
            "COMPARISON": ["usporedi", "razlika", "bolje", "compare", "vs", "versus", "difference"],
            "TASK": ["napravi", "kreiraj", "generiraj", "uradi", "create", "make", "generate", "do"],
        }

        detected = "GENERAL"
        for ctx, keywords in context_map.items():
            if any(kw in last_msg for kw in keywords):
                detected = ctx
                break

        system_inject = f"\n\n[CTX: {detected}]"

        for msg in messages:
            if msg.get("role") == "system":
                msg["content"] += system_inject
                break
        else:
            messages.insert(0, {"role": "system", "content": system_inject})

        body["messages"] = messages
        return body

    def outlet(self, body: dict, __user__: Optional[dict] = None) -> dict:
        return body