File size: 16,372 Bytes
43309f8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f302ce8
43309f8
 
 
6187fb4
 
 
 
43309f8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f302ce8
 
 
 
 
 
 
43309f8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6187fb4
 
43309f8
 
 
 
7fdbdaa
 
43309f8
 
6187fb4
 
 
43309f8
 
 
 
 
 
 
 
 
 
 
 
 
6187fb4
43309f8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6187fb4
 
43309f8
 
 
 
 
 
 
f302ce8
 
 
 
 
 
43309f8
 
6187fb4
f302ce8
 
 
43309f8
f302ce8
 
43309f8
f302ce8
 
43309f8
f302ce8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6187fb4
 
f302ce8
6187fb4
 
98cec4b
 
 
 
f302ce8
 
43309f8
 
 
 
6187fb4
43309f8
f302ce8
 
43309f8
154a09c
 
f302ce8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43309f8
 
f302ce8
 
43309f8
f302ce8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43309f8
 
 
 
f302ce8
98cec4b
 
43309f8
 
f302ce8
 
 
 
43309f8
 
 
6187fb4
43309f8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
"""LABEQ V16: context-grounded communication planning and guided generation.

Hot-install in the already running Colab process AFTER loading Qwen/LoRA/Faisal:

    from google.colab import files
    uploaded = files.upload()
    exec(uploaded['LABEQ_V16_Coaching_Engine.py'].decode('utf-8'))

This wraps the existing engine class. The installed Qwen3.5 and Faisal adapters
are NOT modified or retrained. The planner and reviewer use the Qwen base model
with adapters disabled; the actual response uses the Saudi LoRA. Quality is
heuristic/model-reviewed, not externally validated or guaranteed.
"""
from __future__ import annotations

from semantic_review import parse_review, review_prompt
from difflib import SequenceMatcher
import re

from saudi_register import (
    SAUDI_EXAMPLES, SAUDI_SYSTEM, clean_writing, explicit_fusha,
    normalize_saudi, register_instruction, register_warnings, writing_style,
)


def _clean(s):
    return re.sub(r"\s+", " ", str(s or "")).strip()


def _field(text, labels):
    for line in (text or "").splitlines():
        line = line.strip().lstrip("-• ")
        for label in labels:
            found = re.match(rf"^(?:{re.escape(label)})\s*[::-]\s*(.+)$", line)
            if found:
                return _clean(found.group(1))[:180]
    return ""


def parse_plan(text):
    """Read a compact plan; never invent a value when the model omits it."""
    return {
        "core": _field(text, ("جوهر الموقف", "الجوهر", "المشكلة")),
        "objective": _field(text, ("الهدف", "المطلوب")),
        "strategy": _field(text, ("الخطوة", "الاستراتيجية", "الطريقة")),
        "risk": _field(text, ("انتبهي", "الخطر", "احذري")),
    }


def style_instruction(style, goal, message):
    # Style describes HOW to say it, never replaces WHAT the user needs.
    defaults = {
        "polite": "نبرة لطيفة ومحترمة. حافظ على كل ما طلبه الشخص، ولا تضف اعتذارا أو وعدا أو طلبا من عندك.",
        "firm": "نبرة واضحة وثابتة باحترام. التزم بنفس حدود المستخدم ونطاقها ومدتها؛ لا تخترع حصة مساعدة أو موعدا أو بديلا.",
        "diplomatic": "نبرة هادئة ومراعية للعلاقة. نفذ مقصد المستخدم كامل بدون افتراض اختلاف آراء أو استبدال طلبه بحوار عام.",
        "clarify": "نبرة استيضاح مهذبة. اطلب نفس التوضيح أو الإجراء اللي يريده المستخدم، ولا تحول طلب تنفيذ إجراء إلى سؤال عن سببه.",
        "apology": "نبرة صادقة ومراعية للأثر. إذا طلب الشخص الاعتذار فاكتب اعتذاره هو، واحفظ تحمله للمسؤولية وحدوده؛ لا تنسب له خطأ أو عذرا غير مذكور.",
        "formal": "نبرة سعودية مهنية ومحترمة، واضحة وطبيعية بدون تكلف، مع حفظ كامل المقصد.",
        "مختصر": "صياغة مختصرة تحفظ كل المطلوب والقيود؛ لا تختصر على حساب المعنى.",
    }
    if style in defaults: return defaults[style]
    return defaults["polite"]


def _style_key(style, fallback):
    if style == "تلقائي": return fallback
    return {"لبق":"polite","لطيف":"polite","عفوي":"polite","واضح":"firm","حازم":"firm",
        "دبلوماسي":"diplomatic","اعتذار":"apology","طلب توضيح":"clarify", "رسمي":"formal", "مختصر":"مختصر"}.get(style,"polite")


def _style_display(key):
    return {"polite":"لبق","firm":"حازم","diplomatic":"دبلوماسي","apology":"اعتذار",
            "clarify":"طلب توضيح","formal":"رسمي","مختصر":"مختصر"}.get(key,"لبق")


def _get_last_assistant(history):
    for item in reversed(history or []):
        if item.get("role") == "assistant": return str(item.get("content", ""))[:500]
    return ""


def _get_instructions(history):
    return "\n".join(
        item["content"][:500] for item in history or []
        if item.get("role") == "user" and item.get("content", "").startswith("تعليمات تعديل الرد:")
    )


def local_warnings(reply, message, goal, style, last_reply=""):
    flags=[]
    text=_clean(reply)
    if len(text)<22:flags.append("الصياغة قصيرة جدًا وقد لا توصل الهدف")
    if len(text)>450:flags.append("الرد طويل على رسالة محادثة")
    if re.search(r"(?:تقدرين تقولين|تقدرين تقول|يمكنك الرد|الرد المقترح|كذكاء اصطناعي)",text):
        flags.append("الرد تعليمات للمستخدم بدل رسالة للطرف الآخر")
    if text.startswith("اعتذري") and re.search(r"(?:أبي\s+أعتذر|أعتذر عن|أبغى\s+أعتذر)", message + " " + goal):
        flags.append("الرد يطلب من المستخدم الاعتذار بدل كتابة اعتذاره للطرف الآخر")
    if re.search(r"(?:صرخت|ضربت|أهنت|كذبت عليك|خنتك)",text) and not re.search(r"(?:صرخ|ضرب|أهان|كذب|خان|خنت)",message):
        flags.append("الرد قد ينسب للمستخدم خطأ غير مذكور")
    if style == "clarify" and not re.search(r"؟|\?|(?:^|\s)(?:وش|إيش|ايش|هل|ممكن)(?:\s|$)", text):
        flags.append("صياغة طلب التوضيح تحتاج سؤالًا محددًا للطرف الآخر")
    if style == "clarify" and re.search(r"ممكن\s+أ(?:وضح|شرح)\s+لي", text):
        flags.append("السؤال خلط بين المتحدث والمخاطب؛ اطلب من الطرف الآخر هو يوضح لك المطلوب")
    if last_reply and SequenceMatcher(None,_clean(last_reply),text).ratio() > 0.88:
        flags.append("الصياغة الجديدة تشبه الرد السابق جدًا ولم تتغير بشكل مفيد")
    if re.search(r"(?:إني|أني)\s+(?:بأتابع|بتابع|براجع)|أبي\s+أتابع", message):
        if not re.search(r"(?:أتابع|بتابع|نتابع|بنتابع|أراجع|براجع|نراجع|بنراجع)", text):
            flags.append("الرد أغفل متابعة الموضوع التي طلب المستخدم توضيحها")
    return flags


def _parse_review(text):
    """Base-model reviewer signals: uncertain outputs are not a pass."""
    t=(text or "").strip()
    if re.search(r"(?:^|\n)\s*(?:النتيجة|الحكم)\s*[::]\s*(?:غير مناسب|يحتاج تعديل)",t):
        return False, _field(t,("السبب", "المشكلة")) or "المراجعة رصدت ضعفًا في الصياغة"
    if re.search(r"(?:^|\n)\s*(?:النتيجة|الحكم)\s*[::]\s*مناسب",t):
        return True,""
    return None,""



def reply_v16(self, message, relation="صديق", goal="أبي رد مناسب وواضح",
              style="تلقائي", speaker_gender="غير محدد", recipient_gender="غير محدد",
              history=None, generation_mode="موجّه"):
    message=_clean(message);relation=_clean(relation);goal=_clean(goal)
    if not message:raise ValueError("اكتبي الموقف أولًا")
    if len(message)>2000:raise ValueError("الموقف طويل جدًا")
    from labeq_engine import ALLOWED_USER_STYLES, quality_flags
    if style not in ALLOWED_USER_STYLES:raise ValueError("أسلوب غير مدعوم")
    self.load()
    if style=="تلقائي":
        selected,router_source=self.predict_style(message,relation,goal)
    else:
        selected,router_source=None,"user_selected"
    style_key=_style_key(style,selected or "polite")
    previous=_get_last_assistant(history)
    modifier=_get_instructions(history)
    # Phase 1: grounded short brief using the base model; this is NOT Faisal's explanation.
    plan_prompt=(
        "أنتِ مستشارة تواصل سعودية. لخصي بكلام سعودي بسيط ما قاله المستخدم فقط، دون افتراض مشاعر أو أخطاء أو وعود. "
        "حددي مين اللي تأخر أو غلط بدقة، ولا تعكسي الأدوار. اجمعي الطلبات والقيود من نص الموقف كله. "
        "اكتبي أربع سطور قصيرة بهذه التسميات بالضبط، ولا تكتبي رسالة الرد:\n"
        "جوهر الموقف: [المشكلة المحددة كما وردت]\n"
        "الهدف: [ما الذي يريد تحقيقه فعلاً]\n"
        "الخطوة: [التصرف التواصلي الذي يحقق الهدف دون تصعيد]\n"
        "انتبهي: [تفسير خاطئ واحد يجب تجنبه]\n"
        f"الموقف: {message}\nالعلاقة: {relation}\nهدف المستخدم: {goal}"
    )
    coaching_error = None
    try:
        plan_raw=self._one_generation([{"role":"user","content":plan_prompt}],use_adapter=False,max_tokens=256)
    except Exception as exc:
        plan_raw=""
        coaching_error=type(exc).__name__
    plan=parse_plan(plan_raw)
    # Phase 2: real response by Saudi LoRA. Keep the user's facts as the source of truth.
    register = register_instruction(relation, style_key, modifier)
    gen_system = getattr(self, "writer_system", SAUDI_SYSTEM)
    selected_examples = getattr(self, "writer_examples", SAUDI_EXAMPLES)
    examples = [] if explicit_fusha(modifier) else [dict(item) for item in selected_examples]
    style_line=style_instruction(style_key,goal,message)
    def gen_prompt(extra="", reference=""):
        prompt = (
            f"الموقف: {message}\nالعلاقة: {relation}\nالهدف: {goal}\n"
            f"الأسلوب: {writing_style(style_key, modifier)}\n"
            "اكتب الرسالة للطرف الآخر، واحفظ كل الطلبات والقيود بدون اختصار يضيع المعنى."
        )
        # Faisal's selected action controls tone; original facts and all user
        # requirements stay authoritative, including for a manual override.
        prompt += f"\nطريقة التواصل: {_style_display(style_key)}\n{style_line}"
        if speaker_gender != "غير محدد":
            prompt += f"\nجنس المتحدث: {speaker_gender}"
        if recipient_gender != "غير محدد":
            prompt += f"\nجنس المخاطب: {recipient_gender}"
        if modifier:
            prompt += f"\n{modifier}\nطريقة الكتابة: {register}"
        if reference:
            prompt += (
                "\nنص سابق مطلوب تعديل صياغته فقط، وليس مصدرا للحقائق؛ الموقف والهدف هما الأصل:\n"
                + reference
            )
        if extra:
            prompt += f"\nملاحظة للمراجعة: {extra}"
        return prompt
    def generate(extra="", retry=False, reference=""):
        raw = clean_writing(self._one_generation(
            [{"role":"system","content":gen_system}] + examples
            + [{"role":"user","content":gen_prompt(extra, reference or (previous if modifier else ""))}], retry=retry, max_tokens=256,
        ), modifier)
        text, edits = normalize_saudi(raw, modifier, message + " " + goal)
        record={"reply":text,"draft_before_register_normalization":raw,"register_normalization":edits}
        provenance=getattr(self,'last_writer_provenance',None)
        if provenance:record['writer_provenance']=dict(provenance)
        return record
    selected_record=generate()
    first=selected_record["reply"]
    def check(candidate):
        return list(dict.fromkeys(
            local_warnings(candidate, message, goal, style_key, previous)
            + quality_flags(candidate, relation, goal, message)
            + register_warnings(candidate, modifier)
        ))
    # A grounded failure triggers repair regardless of the words used in its reason.
    # This is same-family model feedback, not independent semantic validation.
    def review(candidate):
        builder=getattr(self,"review_prompt_builder",review_prompt)
        prompt = builder(message, relation, goal, candidate, register)
        raw = self._one_generation([{"role":"user","content":prompt}], use_adapter=False, max_tokens=640)
        return parse_review(raw, message, goal, candidate)

    def assess(candidate):
        hard = check(candidate)
        try:
            judgment = review(candidate)
        except Exception as exc:
            judgment = {"valid":False,"checks":[],"unsupported":[],"issues":[],
                        "error":"review_generation_failed","error_type":type(exc).__name__}
        issues = list(dict.fromkeys(hard + judgment["issues"]))
        return hard, judgment, issues

    hard, judgment, flags = assess(first)
    attempts = [{**selected_record, "review":judgment, "warnings":flags}]
    was_retried = bool(flags)
    retry_error = None
    if was_retried:
        try:
            revised_record = generate("أصلح الملاحظات مع حفظ كل قيود النص الأصلي: " + "؛ ".join(flags), retry=True, reference=first)
            revised = revised_record["reply"]
            if revised.strip():
                new_hard, new_judgment, new_flags = assess(revised)
                attempts.append({**revised_record, "review":new_judgment, "warnings":new_flags})
                # An invalid review can never clear a previous grounded concern.
                old_rank = (not judgment["valid"], len(flags))
                new_rank = (not new_judgment["valid"], len(new_flags))
                if not first.strip() or new_rank < old_rank:
                    first, hard, judgment, flags = revised, new_hard, new_judgment, new_flags
                    selected_record = revised_record
        except Exception as exc:
            retry_error = type(exc).__name__
    reviewed = judgment["valid"] and not judgment["issues"]
    if not judgment["valid"]:
        flags.append("تعذر التحقق من المراجعة الدلالية؛ الصياغة تحتاج مراجعة")
    if retry_error:
        flags.append("لم تكتمل محاولة إعادة الصياغة")
    if not first.strip():raise RuntimeError("المودل لم يولد ردًا")
    coach={k:v for k,v in plan.items() if v}
    return {
        "reply":first.strip(),"style_used":_style_display(style_key),"router_source":router_source,
        "reply_source":getattr(self,"reply_source","qwen_lora_generated"), "style_key":style_key if style_key in {"polite","firm","diplomatic","apology","clarify"} else (selected or "polite"),
        "generator":selected_record.get('writer_provenance',{}).get('generator',getattr(self,'generator',"Qwen3.5-4B + Saudi LoRA")),
        "writer_provenance":selected_record.get('writer_provenance',{}),
        "coaching":coach,"review_status":"flagged" if flags else ("model_reviewed" if reviewed else "unverified"),
        "needs_review":bool(flags), "quality_warnings":list(dict.fromkeys(flags)),"was_retried":was_retried,
        "semantic_review":judgment, "generation_candidates":attempts, "retry_error":retry_error,
        "coaching_status":"model_generated" if coach else "unavailable", "coaching_error":coaching_error,
        "draft_before_register_normalization":selected_record["draft_before_register_normalization"],
        "register_normalization":selected_record["register_normalization"],
    }



def install(engine=None):
    import labeq_engine as mod
    cls=mod.LabeqEngine
    if getattr(cls,"_labeq_v16_installed",False):
        print("✅ LABEQ V16 مثبت مسبقًا")
        return

    cls.reply=reply_v16
    cls._labeq_v16_installed=True
    print("✅ V16: Context plan → Saudi LoRA response → model review, preserving actual Faisal routing")
    print("ℹ️ Improvement in pipeline, NOT a new fine-tuning or proof of quality. Test with real scenarios.")
    if engine is not None:
        print("Qwen loaded:",bool(getattr(engine,'model',None) is not None))
        print("Faisal loaded:",bool(getattr(engine,'faisal_ready',False)))

if __name__ == "__main__" or "engine" in globals():
    install(globals().get("engine"))