Compare commits
22 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| e40f3d4534 | |||
| 89b3f5844e | |||
| 37a8b67b95 | |||
| 39d9093bf6 | |||
| b05e1c8488 | |||
| b313c96955 | |||
| 2a03425514 | |||
| f400f2fec7 | |||
| 405ceb7684 | |||
| 2569003f33 | |||
| 289a9cf27f | |||
| 1bb0c06345 | |||
| 59fc8acdc4 | |||
| 586b543a4c | |||
| c3212e71fe | |||
| f84b2f1cef | |||
| 2a8046b737 | |||
| f319f981f9 | |||
| bf98495198 | |||
| c541069cb5 | |||
| 7480e7db35 | |||
| 3c6e54d233 |
@@ -0,0 +1,2 @@
|
|||||||
|
*.zip
|
||||||
|
build/
|
||||||
@@ -0,0 +1,46 @@
|
|||||||
|
{
|
||||||
|
"models": {
|
||||||
|
"main": {
|
||||||
|
"provider": "claude-code",
|
||||||
|
"modelId": "sonnet",
|
||||||
|
"maxTokens": 64000,
|
||||||
|
"temperature": 0.2,
|
||||||
|
"id": "sonnet"
|
||||||
|
},
|
||||||
|
"research": {
|
||||||
|
"provider": "claude-code",
|
||||||
|
"modelId": "sonnet",
|
||||||
|
"maxTokens": 8700,
|
||||||
|
"temperature": 0.1,
|
||||||
|
"id": "claude-sonnet-4-20250514"
|
||||||
|
},
|
||||||
|
"fallback": {
|
||||||
|
"provider": "claude-code",
|
||||||
|
"modelId": "sonnet",
|
||||||
|
"maxTokens": 120000,
|
||||||
|
"temperature": 0.2
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"global": {
|
||||||
|
"logLevel": "info",
|
||||||
|
"debug": false,
|
||||||
|
"defaultNumTasks": 10,
|
||||||
|
"defaultSubtasks": 5,
|
||||||
|
"defaultPriority": "medium",
|
||||||
|
"projectName": "Task Master",
|
||||||
|
"ollamaBaseURL": "http://localhost:11434/api",
|
||||||
|
"bedrockBaseURL": "https://bedrock.us-east-1.amazonaws.com",
|
||||||
|
"responseLanguage": "English",
|
||||||
|
"enableCodebaseAnalysis": true,
|
||||||
|
"enableProxy": false,
|
||||||
|
"anonymousTelemetry": true,
|
||||||
|
"userId": "1234567890"
|
||||||
|
},
|
||||||
|
"claudeCode": {},
|
||||||
|
"codexCli": {},
|
||||||
|
"grokCli": {
|
||||||
|
"timeout": 120000,
|
||||||
|
"workingDirectory": null,
|
||||||
|
"defaultModel": "grok-4-latest"
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
{
|
||||||
|
"currentTag": "master",
|
||||||
|
"lastSwitched": "2026-04-14T06:05:06.044Z",
|
||||||
|
"branchTagMapping": {},
|
||||||
|
"migrationNoticeShown": true
|
||||||
|
}
|
||||||
File diff suppressed because one or more lines are too long
@@ -0,0 +1,454 @@
|
|||||||
|
# SmartAssistant v1.0.3 — סיכום מנהלים לפריסה בפיתוח
|
||||||
|
|
||||||
|
**תאריך:** 2026-03-31
|
||||||
|
**גרסה:** 1.0.3 | **תאריך שחרור:** 2026-03-29
|
||||||
|
**סביבת יעד:** EspoCRM Development (https://espocrm.dev.marcus-law.co.il)
|
||||||
|
**מחבר:** klear | **דרישות:** EspoCRM >= 8.0.0, PHP >= 8.1
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 1. תיאור כללי
|
||||||
|
|
||||||
|
SmartAssistant הוא מודול עוזר AI מאוחד למשרד עורכי דין.
|
||||||
|
|
||||||
|
המודול מספק:
|
||||||
|
- **ממשק צ'אט צף** (Floating Action Button) בכל דפי המערכת
|
||||||
|
- **מצב משרד** — סקירה כללית, התראות, סטטיסטיקות עומס עבודה
|
||||||
|
- **מצב תיק** — סיוע מעמיק בתיק ספציפי עם הקשר מלא
|
||||||
|
- **מערכת זיכרון תיק** (CaseMemory) — מאגר ידע מובנה לכל תיק ב-7 קטגוריות
|
||||||
|
- **ביצוע פעולות באישור** — יצירת משימות, פתקים, דיונים, שינוי סטטוס
|
||||||
|
- **אינטגרציה עם Stream** — כל אינטראקציה מופיעה בציר הזמן של התיק
|
||||||
|
- **התראות בסרגל ניווט** — תיקים לא פעילים, משימות באיחור, דיונים קרובים
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 2. מה בוצע
|
||||||
|
|
||||||
|
### 2.1 Backend (PHP) — 16 קבצים
|
||||||
|
|
||||||
|
| רכיב | קובץ | תפקיד |
|
||||||
|
|-------|------|--------|
|
||||||
|
| Controller | `Controllers/SmartAssistant.php` | 10 נקודות קצה API (chat, execute, summary, alerts, history, conversations, conversationMessages, searchHistory, caseMemory, saveMemory) |
|
||||||
|
| שירות ראשי | `Services/SmartAssistantService.php` | תזמור מלא: בניית הקשר → קריאת webhook → טיפול בפעולות → שמירת שיחה |
|
||||||
|
| בונה הקשר תיק | `Services/CaseContextBuilder.php` | אוסף: נתוני תיק, אנשי קשר, משימות פתוחות, דיונים קרובים, פתקים אחרונים, זיכרון |
|
||||||
|
| בונה הקשר משרד | `Services/OfficeContextBuilder.php` | סטטיסטיקות כלליות, התראות, עומס עבודה לפי משתמש, drill-down לתיקים |
|
||||||
|
| מחשבון התראות | `Services/AlertCalculator.php` | 6 סוגי התראות: תיקים לא פעילים (14/30 יום), משימות באיחור, דיונים קרובים, תיקים ללא שיוך, משימות בעדיפות גבוהה |
|
||||||
|
| מבצע פעולות | `Services/ActionExecutor.php` | 8 כלים: create_task, add_note, change_status, create_meeting, schedule_hearing, list_documents, analyze_document, save_memory, rename_document |
|
||||||
|
| שירות זיכרון | `Services/CaseMemoryService.php` | CRUD מלא לאנטיטי CaseMemory |
|
||||||
|
| ספק הקשר זיכרון | `Services/CaseMemoryContextProvider.php` | הזרקת זיכרון מוצמד/חשוב להקשר AI |
|
||||||
|
| מאגר שיחות | `Services/ConversationRepository.php` | שמירה ואחזור שיחות ב-AssistantConversation |
|
||||||
|
| מנתח מסמכים | `Services/DocumentAnalyzer.php` | סריקת מסמכים דרך FileStorage |
|
||||||
|
| Hook - שינוי תיק | `Hooks/Case/CaseFieldChangeMemory.php` | זיכרון אוטומטי בשינוי: סטטוס, שופט, בית משפט, מועד דיון |
|
||||||
|
| Hook - דיון | `Hooks/Meeting/HearingScheduledMemory.php` | זיכרון אוטומטי בקביעת דיון חדש |
|
||||||
|
| FileStorage | `Classes/FileStorage/*` | 4 קבצים: Interface, Factory, WebDav, NextCloud |
|
||||||
|
|
||||||
|
### 2.2 Frontend (JavaScript) — 9 קבצים
|
||||||
|
|
||||||
|
| רכיב | תפקיד |
|
||||||
|
|-------|--------|
|
||||||
|
| `floating-chat.js` | ממשק צ'אט צף (~1500 שורות): שליחת הודעות, היסטוריה, זיכרון, אישור/דחיית פעולות, עיצוב RTL, Material Design |
|
||||||
|
| `site/navbar.js` | אייקון התראות בסרגל ניווט עם badge מספרי |
|
||||||
|
| `dashlets/smart-assistant.js` | דשלט לדשבורד: סיכום מדדים + התראות אחרונות |
|
||||||
|
| `dashlets/options/smart-assistant.js` | הגדרות דשלט |
|
||||||
|
| `case/modals/add-memory.js` | מודל להוספת זיכרון ידנית לתיק |
|
||||||
|
| `stream/notes/smart-request.js` | תצוגת הודעת משתמש ב-Stream |
|
||||||
|
| `stream/notes/smart-response.js` | תצוגת תשובת עוזר ב-Stream |
|
||||||
|
| `stream/notes/smart-action.js` | תצוגת פעולה שבוצעה ב-Stream |
|
||||||
|
|
||||||
|
### 2.3 מטא-נתונים וקונפיגורציה — 17 קבצי JSON
|
||||||
|
|
||||||
|
- **routes.json** — 10 נתיבי API
|
||||||
|
- **module.json** — רישום מודול עם clientModule
|
||||||
|
- **entityDefs/** — CaseMemory (אנטיטי חדש, 15 שדות, 4 אינדקסים), Case (שדה cNextHearing), Note (סוגי stream חדשים)
|
||||||
|
- **scopes/CaseMemory.json** — הגדרות scope
|
||||||
|
- **layouts/CaseMemory/** — 4 layouts (list, detail, listSmall, detailSmall)
|
||||||
|
- **dashlets/SmartAssistant.json** — הגדרת דשלט
|
||||||
|
- **integrations/SmartAssistant.json** — 6 שדות הגדרה (webhookUrl, apiKey, maxMessagesPerHour, inactivityWarningDays, inactivityCriticalDays, upcomingHearingDays)
|
||||||
|
- **clientDefs/App.json** — רישום navbar
|
||||||
|
|
||||||
|
### 2.4 תרגומים — 10 קבצים
|
||||||
|
|
||||||
|
- **en_US** — 5 קבצי תרגום (SmartAssistant, CaseMemory, Case, Global, Integration)
|
||||||
|
- **fa_IR** — 5 קבצי תרגום מקבילים (עברית/פרסית)
|
||||||
|
|
||||||
|
### 2.5 בנייה והפצה
|
||||||
|
|
||||||
|
- **build.sh** — סקריפט בנייה: קורא גרסה מ-manifest.json, מעדכן README, יוצר ZIP
|
||||||
|
- **composer.json** — הגדרת חבילה ל-Composer Registry
|
||||||
|
- **4 גרסאות ZIP** — 1.0.0, 1.0.1, 1.0.2, 1.0.3
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 3. היסטוריית גרסאות ותיקוני באגים
|
||||||
|
|
||||||
|
### v1.0.0 — `a148e0f`
|
||||||
|
- **פיצ'ר ראשוני** — מודול SmartAssistant מלא עם case memory
|
||||||
|
|
||||||
|
### v1.0.0 → v1.0.1 — תיקונים
|
||||||
|
| Commit | תקלה | תיקון |
|
||||||
|
|--------|-------|--------|
|
||||||
|
| `0cdbf6d` | דשלט ירש מ-DashletView שלא קיים בגרסאות חדשות | שונה ל-base View + הוספת getTitle() |
|
||||||
|
| `ffd966a` | לא היה composer.json לפרסום אוטומטי | נוסף composer.json |
|
||||||
|
| `bd1cd17` | גרסה ב-README לא מתעדכנת אוטומטית | build.sh מעדכן README |
|
||||||
|
| `95ca8a0` | חסר תיעוד מקיף | נוסף README מלא בעברית |
|
||||||
|
| `fbe4f82` | נתיבי client לא תואמים לפורמט מודולים של EspoCRM | מיגרציה לנתיבי מודול תקניים |
|
||||||
|
|
||||||
|
### v1.0.1 → v1.0.2 — `f380542`
|
||||||
|
| תקלה | תיקון |
|
||||||
|
|-------|--------|
|
||||||
|
| נתיבי CSS ו-script שגויים ב-app/client.json metadata | תיקון הנתיבים ל-format הנכון |
|
||||||
|
|
||||||
|
### v1.0.2 → v1.0.3 — `d5c8a60`
|
||||||
|
| תקלה | תיקון |
|
||||||
|
|-------|--------|
|
||||||
|
| EspoCRM לא מצליח לרזולב templates בצד לקוח כי חסר clientModule | הוספת `clientModule` ל-module.json |
|
||||||
|
|
||||||
|
### סיכום תקלות שנמצאו ותוקנו
|
||||||
|
|
||||||
|
| # | תקלה | חומרה | סטטוס |
|
||||||
|
|---|-------|--------|--------|
|
||||||
|
| 1 | דשלט ירש מ-class לא קיים | גבוהה | תוקן v1.0.1 |
|
||||||
|
| 2 | נתיבי client לא תקניים לפורמט מודולים | גבוהה | תוקן v1.0.1 |
|
||||||
|
| 3 | נתיבי CSS/script שגויים ב-metadata | בינונית | תוקן v1.0.2 |
|
||||||
|
| 4 | חסר clientModule לרזולוציית templates | גבוהה | תוקן v1.0.3 |
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 4. מה נבדק
|
||||||
|
|
||||||
|
### נבדק ידנית (על סמך תיקוני הבאגים)
|
||||||
|
- רישום מודול וטעינת client-side assets
|
||||||
|
- תצוגת דשלט בדשבורד
|
||||||
|
- רזולוציית templates בצד לקוח
|
||||||
|
- נתיבי metadata (CSS, scripts)
|
||||||
|
|
||||||
|
### לא נבדק (אין קבצי טסט בפרויקט)
|
||||||
|
- **אין בדיקות יחידה (unit tests)** לקוד PHP
|
||||||
|
- **אין בדיקות אינטגרציה** ל-API endpoints
|
||||||
|
- **אין בדיקות E2E** לממשק הצ'אט
|
||||||
|
- **אין CI/CD pipeline** (אין GitHub Actions / Gitea Actions)
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 5. דרישות קדם לפריסה בפיתוח
|
||||||
|
|
||||||
|
| דרישה | סטטוס | פירוט |
|
||||||
|
|--------|--------|--------|
|
||||||
|
| EspoCRM >= 8.0.0 | לאמת | לוודא גרסה בסביבת dev |
|
||||||
|
| PHP >= 8.1 | לאמת | לוודא גרסה בשרת |
|
||||||
|
| Webhook URL | **נדרש הגדרה** | יש להגדיר n8n workflow שיקבל את הפניות |
|
||||||
|
| API Key | אופציונלי | לאימות webhook |
|
||||||
|
| AssistantConversation entity | לאמת | נדרש שהאנטיטי קיים |
|
||||||
|
| WebDAV/NextCloud | אופציונלי | רק לפיצ'ר ניתוח מסמכים |
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 6. מה נשאר לעשות
|
||||||
|
|
||||||
|
### קריטי לפני פריסה
|
||||||
|
| # | משימה | עדיפות |
|
||||||
|
|---|--------|---------|
|
||||||
|
| 1 | **הגדרת n8n workflow** לקבלת webhook מהעוזר (chat endpoint) | קריטי |
|
||||||
|
| 2 | **בדיקת תאימות** עם גרסת EspoCRM בסביבת dev | קריטי |
|
||||||
|
| 3 | **לוודא שאין שאריות** של מודולים ישנים בסביבת dev | קריטי |
|
||||||
|
| 4 | **הגדרת Integration** — webhookUrl + apiKey ב-Admin > Integrations | קריטי |
|
||||||
|
| 5 | **בדיקה שאנטיטי AssistantConversation קיים** או שצריך ליצור אותו | קריטי |
|
||||||
|
|
||||||
|
### חשוב אחרי פריסה
|
||||||
|
| # | משימה | עדיפות |
|
||||||
|
|---|--------|---------|
|
||||||
|
| 6 | **בדיקות ידניות מקיפות** — צ'אט במצב משרד ומצב תיק | גבוהה |
|
||||||
|
| 7 | **בדיקת פעולות** — create_task, add_note, schedule_hearing, change_status | גבוהה |
|
||||||
|
| 8 | **בדיקת hooks** — שינוי סטטוס תיק → זיכרון אוטומטי | גבוהה |
|
||||||
|
| 9 | **בדיקת התראות** — navbar badge + דשלט | בינונית |
|
||||||
|
| 10 | **בדיקת היסטוריה** — שמירה ואחזור שיחות, חיפוש | בינונית |
|
||||||
|
|
||||||
|
### שיפורים עתידיים
|
||||||
|
| # | משימה | עדיפות |
|
||||||
|
|---|--------|---------|
|
||||||
|
| 11 | כתיבת בדיקות יחידה ל-PHP | בינונית |
|
||||||
|
| 12 | הוספת CI/CD pipeline (Gitea Actions) | בינונית |
|
||||||
|
| 13 | הוספת תרגום עברי (he_IL) — כרגע en_US + fa_IR בלבד | בינונית |
|
||||||
|
| 14 | Rate limiting בצד client (כרגע רק בצד server) | נמוכה |
|
||||||
|
| 15 | הצפנת שיחות | נמוכה |
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 7. תיעוד מפורט של הקוד והקבצים
|
||||||
|
|
||||||
|
### 7.1 ארכיטקטורה כללית
|
||||||
|
|
||||||
|
```
|
||||||
|
┌─────────────────────────────────────────────────┐
|
||||||
|
│ Frontend (JS) │
|
||||||
|
│ floating-chat.js ←→ navbar.js ←→ dashlet.js │
|
||||||
|
│ │ │ │ │
|
||||||
|
│ [צ'אט צף] [התראות navbar] [דשלט דשבורד] │
|
||||||
|
└───────────────────────┬─────────────────────────┘
|
||||||
|
│ REST API (10 endpoints)
|
||||||
|
┌───────────────────────┴─────────────────────────┐
|
||||||
|
│ Controller/SmartAssistant.php │
|
||||||
|
│ chat | execute | summary | alerts | history ... │
|
||||||
|
└───────────────────────┬─────────────────────────┘
|
||||||
|
│
|
||||||
|
┌───────────────────────┴─────────────────────────┐
|
||||||
|
│ SmartAssistantService.php │
|
||||||
|
│ תזמור: הקשר → webhook → פעולות → שמירה │
|
||||||
|
├──────────┬──────────┬──────────┬─────────────────┤
|
||||||
|
│ Context │ Action │ Memory │ Conversation │
|
||||||
|
│ Builders │ Executor │ Service │ Repository │
|
||||||
|
├──────────┴──────────┴──────────┴─────────────────┤
|
||||||
|
│ CaseContext │ OfficeContext │ AlertCalculator │
|
||||||
|
│ Builder │ Builder │ │
|
||||||
|
└───────────────┴──────────────┴───────────────────┘
|
||||||
|
│
|
||||||
|
┌─────────┴──────────┐
|
||||||
|
│ External n8n │
|
||||||
|
│ Webhook (AI) │
|
||||||
|
└────────────────────┘
|
||||||
|
```
|
||||||
|
|
||||||
|
### 7.2 זרימת צ'אט (Chat Flow)
|
||||||
|
|
||||||
|
```
|
||||||
|
1. משתמש שולח הודעה ← floating-chat.js
|
||||||
|
2. POST /SmartAssistant/action/chat { message, caseId?, conversationId }
|
||||||
|
3. Controller → SmartAssistantService::chat()
|
||||||
|
4. בניית הקשר:
|
||||||
|
├─ מצב תיק → CaseContextBuilder (נתוני תיק + אנשי קשר + משימות + דיונים + זיכרון)
|
||||||
|
└─ מצב משרד → OfficeContextBuilder (סטטיסטיקות + התראות)
|
||||||
|
5. קריאת webhook חיצוני (n8n) עם: message + context + history
|
||||||
|
6. קבלת תשובה: { text, actions[] }
|
||||||
|
7. פעולות מיידיות (list_documents, save_memory) → ביצוע אוטומטי
|
||||||
|
8. פעולות הדורשות אישור → שמירה + החזרה למשתמש
|
||||||
|
9. שמירת שיחה ב-ConversationRepository
|
||||||
|
10. שמירת הודעות ב-Stream (smart-request + smart-response notes)
|
||||||
|
```
|
||||||
|
|
||||||
|
### 7.3 פירוט קבצי Backend
|
||||||
|
|
||||||
|
#### `Controllers/SmartAssistant.php` (168 שורות)
|
||||||
|
- **actionChat()** — מקבל message, caseId, conversationId. בודק ACL ל-Case. קורא ל-SmartAssistantService::chat()
|
||||||
|
- **actionExecute()** — מקבל actionId, conversationId. מבצע פעולה שאושרה
|
||||||
|
- **actionSummary()** — מחזיר סיכום משרדי + התראות
|
||||||
|
- **actionAlerts()** — רשימת התראות מלאה
|
||||||
|
- **actionHistory()** — היסטוריית שיחות (לפי caseId או אחרונה)
|
||||||
|
- **actionConversations()** — רשימת שיחות אחרונות
|
||||||
|
- **actionConversationMessages()** — הודעות שיחה ספציפית
|
||||||
|
- **actionSearchHistory()** — חיפוש חופשי בשיחות
|
||||||
|
- **actionCaseMemory()** — זיכרון תיק לפי קטגוריה
|
||||||
|
- **actionSaveMemory()** — שמירת זיכרון ידנית
|
||||||
|
|
||||||
|
#### `Services/SmartAssistantService.php` (~500 שורות)
|
||||||
|
הקובץ המרכזי ביותר. אחראי על:
|
||||||
|
- **chat()** — הזרימה המלאה: בניית הקשר, קריאת webhook, עיבוד פעולות, שמירת שיחה
|
||||||
|
- **callWebhook()** — קריאת CURL ל-n8n עם timeout, headers (X-Api-Key), JSON payload
|
||||||
|
- **processActions()** — מיון פעולות: מיידיות vs. דורשות אישור
|
||||||
|
- **executeAction()** — ביצוע פעולה שאושרה דרך ActionExecutor
|
||||||
|
- **getSummary()** / **getAlerts()** — מצב משרד
|
||||||
|
- **getConversations()** / **getHistory()** / **searchHistory()** — ניהול שיחות
|
||||||
|
|
||||||
|
#### `Services/CaseContextBuilder.php` (~150 שורות)
|
||||||
|
בונה הקשר עשיר לתיק:
|
||||||
|
```php
|
||||||
|
[
|
||||||
|
'case' => [...], // שדות תיק מרכזיים
|
||||||
|
'contacts' => [...], // אנשי קשר מקושרים
|
||||||
|
'openTasks' => [...], // משימות פתוחות (עד 10)
|
||||||
|
'upcomingMeetings' => [...], // דיונים קרובים (עד 5)
|
||||||
|
'recentNotes' => [...], // פתקים אחרונים (עד 10)
|
||||||
|
'memories' => [...], // זיכרון מוצמד + חשוב
|
||||||
|
'stats' => [...] // מספרי סיכום
|
||||||
|
]
|
||||||
|
```
|
||||||
|
|
||||||
|
#### `Services/OfficeContextBuilder.php` (~80 שורות)
|
||||||
|
בונה סיכום משרדי:
|
||||||
|
```php
|
||||||
|
[
|
||||||
|
'totalActiveCases' => int,
|
||||||
|
'casesByStatus' => [...],
|
||||||
|
'overdueTasks' => int,
|
||||||
|
'upcomingHearings' => int,
|
||||||
|
'userWorkload' => [...], // עומס לפי משתמש
|
||||||
|
'alerts' => [...]
|
||||||
|
]
|
||||||
|
```
|
||||||
|
|
||||||
|
#### `Services/ActionExecutor.php` (~250 שורות)
|
||||||
|
מבצע 8 סוגי פעולות:
|
||||||
|
|
||||||
|
| כלי | פרמטרים | פעולה |
|
||||||
|
|------|----------|--------|
|
||||||
|
| `create_task` | name, dateEnd, priority, description | יצירת Task מקושר לתיק |
|
||||||
|
| `add_note` | text | הוספת Post ב-Stream של התיק |
|
||||||
|
| `change_status` | status | שינוי סטטוס (עם validation מול enum) |
|
||||||
|
| `create_meeting` | name, dateStart, dateEnd, description | יצירת Meeting |
|
||||||
|
| `schedule_hearing` | date, description | עדכון cNextHearing + יצירת Meeting |
|
||||||
|
| `list_documents` | — | רשימת מסמכים (read-only, ללא אישור) |
|
||||||
|
| `analyze_document` | documentId | ניתוח מסמך דרך DocumentAnalyzer |
|
||||||
|
| `save_memory` | name, content, category, importance | שמירת CaseMemory (ללא אישור) |
|
||||||
|
| `rename_document` | documentId, newName | שינוי שם קובץ |
|
||||||
|
|
||||||
|
#### `Services/AlertCalculator.php` (~200 שורות)
|
||||||
|
6 סוגי התראות:
|
||||||
|
|
||||||
|
| סוג | לוגיקה | חומרה |
|
||||||
|
|------|--------|--------|
|
||||||
|
| תיק לא פעיל (אזהרה) | modifiedAt < now - 14 יום | warning |
|
||||||
|
| תיק לא פעיל (קריטי) | modifiedAt < now - 30 יום | critical |
|
||||||
|
| משימה באיחור | dateEnd < now, status != Completed | critical |
|
||||||
|
| דיון קרוב | dateStart < now + 7 ימים | warning |
|
||||||
|
| תיק ללא שיוך | status = New, assignedUserId = null | warning |
|
||||||
|
| משימה בעדיפות גבוהה | priority = Urgent, dateEnd < now + 5 ימים | warning |
|
||||||
|
|
||||||
|
#### `Services/CaseMemoryService.php` (106 שורות)
|
||||||
|
CRUD פשוט:
|
||||||
|
- **getMemories()** — לפי caseId, עם סינון אופציונלי לפי category
|
||||||
|
- **saveMemory()** — יצירת רשומת CaseMemory
|
||||||
|
- **updateMemory()** — עדכון
|
||||||
|
- **deleteMemory()** — מחיקה רכה
|
||||||
|
|
||||||
|
#### `Services/CaseMemoryContextProvider.php`
|
||||||
|
מזריק זיכרון להקשר AI:
|
||||||
|
- זיכרון מוצמד (isPinned = true)
|
||||||
|
- זיכרון בחשיבות high/critical
|
||||||
|
- ממוין לפי importance DESC, modifiedAt DESC
|
||||||
|
|
||||||
|
#### `Services/ConversationRepository.php` (~120 שורות)
|
||||||
|
- **save()** — שמירת שיחה באנטיטי AssistantConversation
|
||||||
|
- **getHistory()** — אחזור לפי caseId או אחרון
|
||||||
|
- **getConversations()** — רשימה ממוינת (עד limit)
|
||||||
|
- **getMessages()** — הודעות שיחה ספציפית
|
||||||
|
- **search()** — חיפוש חופשי בתוכן
|
||||||
|
|
||||||
|
#### Hooks
|
||||||
|
- **CaseFieldChangeMemory** — afterSave hook על Case. בודק אם שדות key השתנו (status, cJudge, cCourt, cNextHearing). אם כן, יוצר CaseMemory אוטומטי מסוג `auto` בקטגוריה המתאימה.
|
||||||
|
- **HearingScheduledMemory** — afterSave hook על Meeting. אם סוג Meeting = "Hearing", יוצר CaseMemory בקטגוריה timeline.
|
||||||
|
|
||||||
|
### 7.4 פירוט קבצי Frontend
|
||||||
|
|
||||||
|
#### `floating-chat.js` (~1500 שורות)
|
||||||
|
הקובץ הגדול ביותר. View יחיד שמנהל:
|
||||||
|
- **תצוגת צ'אט** — קלט הודעה, בועות הודעות (משתמש/עוזר), פעולות בהמתנה
|
||||||
|
- **תצוגת היסטוריה** — רשימת שיחות קודמות, טעינת שיחה
|
||||||
|
- **תצוגת זיכרון** — tabs לפי קטגוריה, הוספה/מחיקה/הצמדה
|
||||||
|
- **RTL layout** — כל ה-CSS מותאם לעברית
|
||||||
|
- **עיצוב Material Design** — צבע ראשי #5c6bc0 (אינדיגו), אנימציות slide-up
|
||||||
|
- **רספונסיבי** — breakpoint ב-480px למובייל
|
||||||
|
- **Inline CSS** — כל הסגנונות מוטמעים בקובץ (לא CSS חיצוני)
|
||||||
|
|
||||||
|
#### `site/navbar.js`
|
||||||
|
- מרחיב את navbar הרגיל של EspoCRM
|
||||||
|
- מוסיף אייקון התראות עם badge מספרי
|
||||||
|
- קורא ל-GET /SmartAssistant/action/alerts ומציג ספירה
|
||||||
|
- רענון אוטומטי כל X דקות
|
||||||
|
|
||||||
|
#### `dashlets/smart-assistant.js`
|
||||||
|
- דשלט לדשבורד הראשי
|
||||||
|
- מציג: סה"כ תיקים פעילים, משימות באיחור, דיונים קרובים
|
||||||
|
- רשימת 5 התראות אחרונות
|
||||||
|
- ACL scope = Case
|
||||||
|
|
||||||
|
#### Stream Notes (3 קבצים)
|
||||||
|
- **smart-request.js** — מציג הודעת משתמש עם אייקון שאלה
|
||||||
|
- **smart-response.js** — מציג תשובת עוזר עם markdown rendering
|
||||||
|
- **smart-action.js** — מציג פעולה שבוצעה עם סטטוס (approved/rejected/executed)
|
||||||
|
|
||||||
|
#### `case/modals/add-memory.js`
|
||||||
|
- מודל Espo.Views.Modal סטנדרטי
|
||||||
|
- שדות: name, category (dropdown), content (textarea), importance (dropdown)
|
||||||
|
- POST ל-/SmartAssistant/action/saveMemory
|
||||||
|
|
||||||
|
### 7.5 אנטיטי CaseMemory — מבנה מלא
|
||||||
|
|
||||||
|
```
|
||||||
|
שדות:
|
||||||
|
├── name (varchar, required) — כותרת הזיכרון
|
||||||
|
├── case (link → Case, required) — תיק משויך
|
||||||
|
├── category (enum) — key_facts | strategy | decisions | contacts_notes | timeline | documents_notes | billing_notes
|
||||||
|
├── content (text, required) — תוכן מלא
|
||||||
|
├── source (enum, readOnly) — manual | assistant | auto
|
||||||
|
├── importance (enum) — low | normal | high | critical
|
||||||
|
├── isPinned (bool) — הצמדה לגישה מהירה
|
||||||
|
├── isArchived (bool) — הסתרת רשומות ישנות
|
||||||
|
├── sourceEntityType (varchar, readOnly) — סוג מקור (למעקב)
|
||||||
|
├── sourceEntityId (varchar, readOnly) — ID מקור
|
||||||
|
├── assignedUser (link → User)
|
||||||
|
├── teams (linkMultiple → Team)
|
||||||
|
├── createdAt, modifiedAt (datetime)
|
||||||
|
└── createdBy, modifiedBy (link → User)
|
||||||
|
|
||||||
|
אינדקסים:
|
||||||
|
├── caseId + deleted
|
||||||
|
├── caseId + category + deleted
|
||||||
|
├── caseId + isPinned + deleted
|
||||||
|
└── importance + deleted
|
||||||
|
```
|
||||||
|
|
||||||
|
### 7.6 הגדרות אינטגרציה
|
||||||
|
|
||||||
|
**Admin > Integrations > Smart Assistant:**
|
||||||
|
|
||||||
|
| שדה | סוג | ברירת מחדל | תיאור |
|
||||||
|
|------|------|------------|--------|
|
||||||
|
| webhookUrl | url | — | כתובת webhook (n8n) |
|
||||||
|
| apiKey | varchar(255) | — | מפתח אימות, נשלח כ-X-Api-Key header |
|
||||||
|
| maxMessagesPerHour | int | 30 | הגבלת קצב |
|
||||||
|
| inactivityWarningDays | int | 14 | סף אזהרה לחוסר פעילות |
|
||||||
|
| inactivityCriticalDays | int | 30 | סף קריטי |
|
||||||
|
| upcomingHearingDays | int | 7 | חלון דיונים קרובים |
|
||||||
|
|
||||||
|
### 7.7 פורמט Webhook
|
||||||
|
|
||||||
|
**Request (נשלח ל-n8n):**
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"message": "מה המצב של תיק כהן?",
|
||||||
|
"context": {
|
||||||
|
"case": { "name": "כהן נ' לוי", "status": "Active", ... },
|
||||||
|
"contacts": [...],
|
||||||
|
"openTasks": [...],
|
||||||
|
"memories": [...]
|
||||||
|
},
|
||||||
|
"conversationId": "conv_abc123",
|
||||||
|
"conversationHistory": [
|
||||||
|
{ "role": "user", "content": "..." },
|
||||||
|
{ "role": "assistant", "content": "..." }
|
||||||
|
],
|
||||||
|
"mode": "case"
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
**Response (נדרש מ-n8n):**
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"text": "תיק כהן פעיל. יש 3 משימות פתוחות ודיון ב-15 באפריל.",
|
||||||
|
"actions": [
|
||||||
|
{
|
||||||
|
"tool": "create_task",
|
||||||
|
"params": { "name": "הכנת סיכומים לדיון", "dateEnd": "2026-04-14" },
|
||||||
|
"actionId": "action_xyz789",
|
||||||
|
"displayText": "יצירת משימה: הכנת סיכומים לדיון"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 8. סיכום מוכנות לפריסה
|
||||||
|
|
||||||
|
| קריטריון | סטטוס | הערות |
|
||||||
|
|-----------|--------|--------|
|
||||||
|
| קוד backend | מוכן | 16 קבצי PHP, ארכיטקטורה נקייה |
|
||||||
|
| קוד frontend | מוכן | 9 קבצי JS, עיצוב RTL |
|
||||||
|
| מטא-נתונים | מוכן | תוקן ב-v1.0.1-1.0.3 |
|
||||||
|
| תרגומים | חלקי | en_US + fa_IR, חסר he_IL ייעודי |
|
||||||
|
| קובץ התקנה | מוכן | SmartAssistant-1.0.3.zip |
|
||||||
|
| n8n workflow | **חסר** | צריך לבנות workflow שיטפל ב-chat |
|
||||||
|
| בדיקות אוטומטיות | **אין** | אין unit/integration/E2E tests |
|
||||||
|
| CI/CD | **אין** | אין pipeline |
|
||||||
|
| תיעוד | מוכן | README מקיף בעברית |
|
||||||
|
|
||||||
|
**המלצה:** המודול מוכן טכנית לפריסה בסביבת פיתוח. יש להגדיר webhook URL (n8n workflow) לפני שהצ'אט יהיה פונקציונלי. מומלץ לבצע בדיקות ידניות מקיפות לאחר ההתקנה.
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
# SmartAssistant - עוזר חכם
|
# SmartAssistant - עוזר חכם
|
||||||
|
|
||||||
**גרסה:** 1.1.7 | **מחבר:** klear | **EspoCRM:** >= 8.0.0
|
**גרסה:** 2.8.0 | **מחבר:** klear | **EspoCRM:** >= 8.0.0
|
||||||
|
|
||||||
## תיאור
|
## תיאור
|
||||||
עוזר AI מאוחד למשרד עורכי דין. מספק ממשק צ'אט צף עם שני מצבי עבודה: **מצב משרד** (סקירה כללית, התראות, סטטיסטיקות) ו**מצב תיק** (סיוע מעמיק בתיק ספציפי). כולל מערכת זיכרון תיק מובנית, ביצוע פעולות באישור המשתמש, ואינטגרציה עם Stream של EspoCRM.
|
עוזר AI מאוחד למשרד עורכי דין. מספק ממשק צ'אט צף עם שני מצבי עבודה: **מצב משרד** (סקירה כללית, התראות, סטטיסטיקות) ו**מצב תיק** (סיוע מעמיק בתיק ספציפי). כולל מערכת זיכרון תיק מובנית, ביצוע פעולות באישור המשתמש, ואינטגרציה עם Stream של EspoCRM.
|
||||||
|
|||||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -4,13 +4,16 @@ set -euo pipefail
|
|||||||
VERSION=$(python3 -c "import json; print(json.load(open('manifest.json'))['version'])")
|
VERSION=$(python3 -c "import json; print(json.load(open('manifest.json'))['version'])")
|
||||||
MODULE=$(python3 -c "import json; m=json.load(open('manifest.json')); print(m.get('module', m['name']))")
|
MODULE=$(python3 -c "import json; m=json.load(open('manifest.json')); print(m.get('module', m['name']))")
|
||||||
ZIPNAME="${MODULE}-${VERSION}.zip"
|
ZIPNAME="${MODULE}-${VERSION}.zip"
|
||||||
|
BUILDDIR="build"
|
||||||
|
|
||||||
# Update version in README.md if it exists
|
# Update version in README.md if it exists
|
||||||
if [[ -f README.md ]]; then
|
if [[ -f README.md ]]; then
|
||||||
sed -i "s/\*\*גרסה:\*\* [^ |]*/\*\*גרסה:\*\* ${VERSION}/" README.md
|
sed -i "s/\*\*גרסה:\*\* [^ |]*/\*\*גרסה:\*\* ${VERSION}/" README.md
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
mkdir -p "$BUILDDIR"
|
||||||
echo "Building $ZIPNAME..."
|
echo "Building $ZIPNAME..."
|
||||||
rm -f "$ZIPNAME"
|
rm -f "$BUILDDIR/$ZIPNAME"
|
||||||
zip -r "$ZIPNAME" manifest.json files/ \
|
zip -r "$BUILDDIR/$ZIPNAME" manifest.json files/ \
|
||||||
-x "*.DS_Store" "*__MACOSX*" "*.zip"
|
-x "*.DS_Store" "*__MACOSX*"
|
||||||
echo "✓ Built: $ZIPNAME ($(du -h "$ZIPNAME" | cut -f1))"
|
echo "✓ Built: $BUILDDIR/$ZIPNAME ($(du -h "$BUILDDIR/$ZIPNAME" | cut -f1))"
|
||||||
|
|||||||
@@ -457,14 +457,23 @@ define('modules/smart-assistant/views/floating-chat', ['view'], function (View)
|
|||||||
var $btn = this.$el.find('.sa-chat-send');
|
var $btn = this.$el.find('.sa-chat-send');
|
||||||
|
|
||||||
$messages.append('<div class="sa-msg sa-msg-user">' + this.escapeHtml(message) + '</div>');
|
$messages.append('<div class="sa-msg sa-msg-user">' + this.escapeHtml(message) + '</div>');
|
||||||
|
var thinkingText = this.translate('Thinking', 'labels', 'SmartAssistant') || 'חושבת...';
|
||||||
var $loading = $('<div class="sa-msg sa-msg-loading"><span class="fas fa-spinner fa-spin" style="margin-left: 5px;"></span> ' +
|
var $loading = $('<div class="sa-msg sa-msg-loading"><span class="fas fa-spinner fa-spin" style="margin-left: 5px;"></span> ' +
|
||||||
this.escapeHtml(this.translate('Thinking', 'labels', 'SmartAssistant') || 'חושבת...') + '</div>');
|
'<span class="sa-thinking-text">' + this.escapeHtml(thinkingText) + '</span></div>');
|
||||||
$messages.append($loading);
|
$messages.append($loading);
|
||||||
this.scrollToBottom();
|
this.scrollToBottom();
|
||||||
|
|
||||||
$btn.prop('disabled', true);
|
$btn.prop('disabled', true);
|
||||||
$input.prop('disabled', true).val('').css('height', 'auto');
|
$input.prop('disabled', true).val('').css('height', 'auto');
|
||||||
|
|
||||||
|
// After 60s, swap the spinner text to a "still working" hint so users
|
||||||
|
// know the longer requests (multi-tool flows) haven't stalled.
|
||||||
|
var slowHintText = this.translate('SlowHint', 'labels', 'SmartAssistant') ||
|
||||||
|
'חושבת... זה לוקח קצת יותר זמן כי אני מעבדת כמה מקורות במקביל';
|
||||||
|
var slowHintTimer = setTimeout(function () {
|
||||||
|
$loading.find('.sa-thinking-text').text(slowHintText);
|
||||||
|
}, 60000);
|
||||||
|
|
||||||
var payload = {
|
var payload = {
|
||||||
message: message,
|
message: message,
|
||||||
mode: this.currentMode,
|
mode: this.currentMode,
|
||||||
@@ -474,7 +483,8 @@ define('modules/smart-assistant/views/floating-chat', ['view'], function (View)
|
|||||||
payload.caseId = this.currentCaseId;
|
payload.caseId = this.currentCaseId;
|
||||||
}
|
}
|
||||||
|
|
||||||
Espo.Ajax.postRequest('SmartAssistant/action/chat', payload).then(function (response) {
|
Espo.Ajax.postRequest('SmartAssistant/action/chat', payload, {timeout: 600000}).then(function (response) {
|
||||||
|
clearTimeout(slowHintTimer);
|
||||||
self.conversationId = response.conversationId;
|
self.conversationId = response.conversationId;
|
||||||
$loading.remove();
|
$loading.remove();
|
||||||
|
|
||||||
@@ -492,6 +502,7 @@ define('modules/smart-assistant/views/floating-chat', ['view'], function (View)
|
|||||||
self.scrollToBottom();
|
self.scrollToBottom();
|
||||||
$input.prop('disabled', false).focus();
|
$input.prop('disabled', false).focus();
|
||||||
}).catch(function () {
|
}).catch(function () {
|
||||||
|
clearTimeout(slowHintTimer);
|
||||||
$loading.remove();
|
$loading.remove();
|
||||||
$messages.append('<div class="sa-msg sa-msg-error">' +
|
$messages.append('<div class="sa-msg sa-msg-error">' +
|
||||||
self.escapeHtml(self.translate('Error', 'labels', 'SmartAssistant') || 'שגיאה בתקשורת') + '</div>');
|
self.escapeHtml(self.translate('Error', 'labels', 'SmartAssistant') || 'שגיאה בתקשורת') + '</div>');
|
||||||
|
|||||||
@@ -0,0 +1,16 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Espo\Modules\SmartAssistant\Controllers;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Exposes the AssistantRule entity over the standard REST API
|
||||||
|
* (GET/POST/PUT/DELETE /api/v1/AssistantRule).
|
||||||
|
*
|
||||||
|
* Without this controller class EspoCRM responds 404 "Controller does not exist"
|
||||||
|
* even when the entity, object, and acl flags in scopes/AssistantRule.json are
|
||||||
|
* set. Inheriting from Record gives us the full CRUD surface — list/search,
|
||||||
|
* read, create, update, delete — with ACL enforcement.
|
||||||
|
*/
|
||||||
|
class AssistantRule extends \Espo\Core\Controllers\Record
|
||||||
|
{
|
||||||
|
}
|
||||||
@@ -9,17 +9,25 @@ use Espo\Core\Exceptions\Forbidden;
|
|||||||
use Espo\Core\InjectableFactory;
|
use Espo\Core\InjectableFactory;
|
||||||
use Espo\Core\Acl;
|
use Espo\Core\Acl;
|
||||||
use Espo\Modules\SmartAssistant\Services\SmartAssistantService;
|
use Espo\Modules\SmartAssistant\Services\SmartAssistantService;
|
||||||
|
use Espo\Modules\SmartAssistant\Services\ActionExecutor;
|
||||||
use Espo\Modules\SmartAssistant\Services\CaseMemoryService;
|
use Espo\Modules\SmartAssistant\Services\CaseMemoryService;
|
||||||
|
use Espo\Modules\SmartAssistant\Services\DocumentAnalyzer;
|
||||||
|
use Espo\Modules\SmartAssistant\Services\GenericTemplateGenerator;
|
||||||
|
use Espo\Modules\SmartAssistant\Services\FreeDocumentGenerator;
|
||||||
|
use Espo\Modules\SmartAssistant\Services\CaseFolderManager;
|
||||||
|
use Espo\Entities\User;
|
||||||
|
|
||||||
class SmartAssistant
|
class SmartAssistant
|
||||||
{
|
{
|
||||||
private InjectableFactory $injectableFactory;
|
private InjectableFactory $injectableFactory;
|
||||||
private Acl $acl;
|
private Acl $acl;
|
||||||
|
private User $user;
|
||||||
|
|
||||||
public function __construct(InjectableFactory $injectableFactory, Acl $acl)
|
public function __construct(InjectableFactory $injectableFactory, Acl $acl, User $user)
|
||||||
{
|
{
|
||||||
$this->injectableFactory = $injectableFactory;
|
$this->injectableFactory = $injectableFactory;
|
||||||
$this->acl = $acl;
|
$this->acl = $acl;
|
||||||
|
$this->user = $user;
|
||||||
}
|
}
|
||||||
|
|
||||||
private function getService(): SmartAssistantService
|
private function getService(): SmartAssistantService
|
||||||
@@ -169,4 +177,326 @@ class SmartAssistant
|
|||||||
'id' => $entry->get('id'),
|
'id' => $entry->get('id'),
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function postActionExecuteTool(Request $request, Response $response): array
|
||||||
|
{
|
||||||
|
$this->checkAccess();
|
||||||
|
$data = $request->getParsedBody();
|
||||||
|
|
||||||
|
if (empty($data->tool)) {
|
||||||
|
throw new BadRequest('tool is required.');
|
||||||
|
}
|
||||||
|
|
||||||
|
$tool = $data->tool;
|
||||||
|
$params = json_decode(json_encode($data->params ?? []), true) ?? [];
|
||||||
|
$caseId = $data->caseId ?? null;
|
||||||
|
|
||||||
|
$executor = $this->injectableFactory->create(ActionExecutor::class);
|
||||||
|
|
||||||
|
return $executor->execute($tool, $params, $caseId, $this->user->getId());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function postActionReadDocument(Request $request, Response $response): array
|
||||||
|
{
|
||||||
|
$this->checkAccess();
|
||||||
|
$data = $request->getParsedBody();
|
||||||
|
|
||||||
|
$filePath = $data->filePath ?? null;
|
||||||
|
if (empty($filePath)) {
|
||||||
|
throw new BadRequest('filePath is required.');
|
||||||
|
}
|
||||||
|
|
||||||
|
$maxLength = isset($data->maxLength) ? (int) $data->maxLength : null;
|
||||||
|
|
||||||
|
$analyzer = $this->injectableFactory->create(DocumentAnalyzer::class);
|
||||||
|
$text = $analyzer->extractFullText($filePath, $maxLength);
|
||||||
|
|
||||||
|
return [
|
||||||
|
'success' => $text !== null,
|
||||||
|
'text' => $text,
|
||||||
|
'fileName' => basename($filePath),
|
||||||
|
'charCount' => $text !== null ? mb_strlen($text) : 0,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
public function postActionGetDocumentBytes(Request $request, Response $response): array
|
||||||
|
{
|
||||||
|
$this->checkAccess();
|
||||||
|
$data = $request->getParsedBody();
|
||||||
|
|
||||||
|
$filePath = $data->filePath ?? null;
|
||||||
|
if (empty($filePath)) {
|
||||||
|
throw new BadRequest('filePath is required.');
|
||||||
|
}
|
||||||
|
|
||||||
|
$analyzer = $this->injectableFactory->create(DocumentAnalyzer::class);
|
||||||
|
return $analyzer->getDocumentBytes($filePath);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function postActionReadMultipleDocuments(Request $request, Response $response): array
|
||||||
|
{
|
||||||
|
$this->checkAccess();
|
||||||
|
$data = $request->getParsedBody();
|
||||||
|
|
||||||
|
$filePaths = $data->filePaths ?? [];
|
||||||
|
if (empty($filePaths)) {
|
||||||
|
throw new BadRequest('filePaths is required (array of file paths).');
|
||||||
|
}
|
||||||
|
|
||||||
|
$maxPerFile = isset($data->maxCharsPerFile) ? (int) $data->maxCharsPerFile : 50000;
|
||||||
|
|
||||||
|
$analyzer = $this->injectableFactory->create(DocumentAnalyzer::class);
|
||||||
|
$results = $analyzer->extractMultipleDocuments((array) $filePaths, $maxPerFile);
|
||||||
|
|
||||||
|
return [
|
||||||
|
'success' => true,
|
||||||
|
'documents' => $results,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
public function postActionBatchRename(Request $request, Response $response): array
|
||||||
|
{
|
||||||
|
$this->checkAccess();
|
||||||
|
$data = $request->getParsedBody();
|
||||||
|
|
||||||
|
$renames = $data->renames ?? [];
|
||||||
|
if (empty($renames)) {
|
||||||
|
throw new BadRequest('renames is required (array of {sourcePath, newName}).');
|
||||||
|
}
|
||||||
|
|
||||||
|
$analyzer = $this->injectableFactory->create(DocumentAnalyzer::class);
|
||||||
|
$results = [];
|
||||||
|
$successCount = 0;
|
||||||
|
|
||||||
|
foreach ($renames as $rename) {
|
||||||
|
$rename = (array) $rename;
|
||||||
|
$sourcePath = $rename['sourcePath'] ?? null;
|
||||||
|
$newName = $rename['newName'] ?? null;
|
||||||
|
|
||||||
|
if (!$sourcePath || !$newName) {
|
||||||
|
$results[] = ['success' => false, 'error' => 'Missing sourcePath or newName'];
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
$result = $analyzer->renameDocument($sourcePath, $newName);
|
||||||
|
$results[] = ['success' => true, 'oldName' => $result['oldName'], 'newName' => $result['newName']];
|
||||||
|
$successCount++;
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
$results[] = ['success' => false, 'oldName' => basename($sourcePath), 'error' => $e->getMessage()];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return [
|
||||||
|
'success' => true,
|
||||||
|
'totalCount' => count($renames),
|
||||||
|
'successCount' => $successCount,
|
||||||
|
'results' => $results,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
public function postActionListDocuments(Request $request, Response $response): array
|
||||||
|
{
|
||||||
|
$this->checkAccess();
|
||||||
|
$data = $request->getParsedBody();
|
||||||
|
|
||||||
|
$caseId = $data->caseId ?? null;
|
||||||
|
if (empty($caseId)) {
|
||||||
|
throw new BadRequest('caseId is required.');
|
||||||
|
}
|
||||||
|
|
||||||
|
$analyzer = $this->injectableFactory->create(DocumentAnalyzer::class);
|
||||||
|
return $analyzer->listDocumentsRecursive($caseId);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function postActionBrowseFolder(Request $request, Response $response): array
|
||||||
|
{
|
||||||
|
$this->checkAccess();
|
||||||
|
$data = $request->getParsedBody();
|
||||||
|
|
||||||
|
$path = $data->path ?? null;
|
||||||
|
if ($path === null) {
|
||||||
|
throw new BadRequest('path is required.');
|
||||||
|
}
|
||||||
|
|
||||||
|
$analyzer = $this->injectableFactory->create(DocumentAnalyzer::class);
|
||||||
|
return [
|
||||||
|
'path' => $path,
|
||||||
|
'entries' => $analyzer->browseFolder((string) $path),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
public function postActionFindCaseFolder(Request $request, Response $response): array
|
||||||
|
{
|
||||||
|
$this->checkAccess();
|
||||||
|
$data = $request->getParsedBody();
|
||||||
|
|
||||||
|
$query = $data->query ?? null;
|
||||||
|
if (empty($query)) {
|
||||||
|
throw new BadRequest('query is required.');
|
||||||
|
}
|
||||||
|
|
||||||
|
$limit = isset($data->limit) ? max(1, min((int) $data->limit, 50)) : 20;
|
||||||
|
|
||||||
|
$analyzer = $this->injectableFactory->create(DocumentAnalyzer::class);
|
||||||
|
return [
|
||||||
|
'query' => $query,
|
||||||
|
'matches' => $analyzer->findCaseFolder((string) $query, $limit),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
public function postActionSetCaseFolderPath(Request $request, Response $response): array
|
||||||
|
{
|
||||||
|
$this->checkAccess();
|
||||||
|
if (!$this->acl->checkScope('Case', 'edit')) {
|
||||||
|
throw new Forbidden('No edit access to Case.');
|
||||||
|
}
|
||||||
|
|
||||||
|
$data = $request->getParsedBody();
|
||||||
|
$caseId = $data->caseId ?? null;
|
||||||
|
$path = $data->path ?? null;
|
||||||
|
|
||||||
|
if (empty($caseId) || empty($path)) {
|
||||||
|
throw new BadRequest('caseId and path are required.');
|
||||||
|
}
|
||||||
|
|
||||||
|
$analyzer = $this->injectableFactory->create(DocumentAnalyzer::class);
|
||||||
|
return $analyzer->setCaseFolderPath((string) $caseId, (string) $path);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function postActionGenerateFromTemplate(Request $request, Response $response): array
|
||||||
|
{
|
||||||
|
$this->checkAccess();
|
||||||
|
$data = $request->getParsedBody();
|
||||||
|
|
||||||
|
$templateId = $data->templateId ?? null;
|
||||||
|
$caseId = $data->caseId ?? null;
|
||||||
|
|
||||||
|
if (empty($templateId)) {
|
||||||
|
throw new BadRequest('templateId is required.');
|
||||||
|
}
|
||||||
|
if (empty($caseId)) {
|
||||||
|
throw new BadRequest('caseId is required.');
|
||||||
|
}
|
||||||
|
|
||||||
|
$documentSubject = $data->documentSubject ?? null;
|
||||||
|
$customPlaceholders = (array) ($data->customPlaceholders ?? []);
|
||||||
|
|
||||||
|
$generator = $this->injectableFactory->create(GenericTemplateGenerator::class);
|
||||||
|
|
||||||
|
return $generator->generate($templateId, $caseId, $documentSubject, $customPlaceholders);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function postActionCreateDocument(Request $request, Response $response): array
|
||||||
|
{
|
||||||
|
$this->checkAccess();
|
||||||
|
$data = $request->getParsedBody();
|
||||||
|
|
||||||
|
$caseId = $data->caseId ?? null;
|
||||||
|
$title = $data->title ?? null;
|
||||||
|
$body = $data->body ?? null;
|
||||||
|
|
||||||
|
if (empty($caseId)) {
|
||||||
|
throw new BadRequest('caseId is required.');
|
||||||
|
}
|
||||||
|
if (empty($title)) {
|
||||||
|
throw new BadRequest('title is required.');
|
||||||
|
}
|
||||||
|
if (empty($body)) {
|
||||||
|
throw new BadRequest('body is required.');
|
||||||
|
}
|
||||||
|
|
||||||
|
$documentType = $data->documentType ?? 'letter';
|
||||||
|
$recipient = $data->recipient ?? null;
|
||||||
|
|
||||||
|
$generator = $this->injectableFactory->create(FreeDocumentGenerator::class);
|
||||||
|
|
||||||
|
return $generator->generate($caseId, $title, $body, $documentType, $recipient);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function postActionWriteToFolder(Request $request, Response $response): array
|
||||||
|
{
|
||||||
|
$this->checkAccess();
|
||||||
|
$data = $request->getParsedBody();
|
||||||
|
|
||||||
|
$caseId = $data->caseId ?? null;
|
||||||
|
$relPath = $data->relPath ?? null;
|
||||||
|
$contentBase64 = $data->contentBase64 ?? null;
|
||||||
|
|
||||||
|
if (empty($caseId)) throw new BadRequest('caseId is required.');
|
||||||
|
if (empty($relPath)) throw new BadRequest('relPath is required.');
|
||||||
|
if (!isset($contentBase64)) throw new BadRequest('contentBase64 is required.');
|
||||||
|
|
||||||
|
$content = base64_decode((string) $contentBase64, true);
|
||||||
|
if ($content === false) {
|
||||||
|
throw new BadRequest('contentBase64 is not valid base64.');
|
||||||
|
}
|
||||||
|
|
||||||
|
$manager = $this->injectableFactory->create(CaseFolderManager::class);
|
||||||
|
return $manager->writeFile($caseId, $relPath, $content);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function postActionCreateFolder(Request $request, Response $response): array
|
||||||
|
{
|
||||||
|
$this->checkAccess();
|
||||||
|
$data = $request->getParsedBody();
|
||||||
|
|
||||||
|
$caseId = $data->caseId ?? null;
|
||||||
|
$relPath = $data->relPath ?? null;
|
||||||
|
|
||||||
|
if (empty($caseId)) throw new BadRequest('caseId is required.');
|
||||||
|
if (empty($relPath)) throw new BadRequest('relPath is required.');
|
||||||
|
|
||||||
|
$manager = $this->injectableFactory->create(CaseFolderManager::class);
|
||||||
|
return $manager->createFolder($caseId, $relPath);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function postActionRenameItem(Request $request, Response $response): array
|
||||||
|
{
|
||||||
|
$this->checkAccess();
|
||||||
|
$data = $request->getParsedBody();
|
||||||
|
|
||||||
|
$caseId = $data->caseId ?? null;
|
||||||
|
$currentRelPath = $data->currentRelPath ?? null;
|
||||||
|
$newName = $data->newName ?? null;
|
||||||
|
|
||||||
|
if (empty($caseId)) throw new BadRequest('caseId is required.');
|
||||||
|
if (empty($currentRelPath)) throw new BadRequest('currentRelPath is required.');
|
||||||
|
if (empty($newName)) throw new BadRequest('newName is required.');
|
||||||
|
|
||||||
|
$manager = $this->injectableFactory->create(CaseFolderManager::class);
|
||||||
|
return $manager->renameItem($caseId, $currentRelPath, $newName);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function postActionMoveItem(Request $request, Response $response): array
|
||||||
|
{
|
||||||
|
$this->checkAccess();
|
||||||
|
$data = $request->getParsedBody();
|
||||||
|
|
||||||
|
$caseId = $data->caseId ?? null;
|
||||||
|
$sourceRelPath = $data->sourceRelPath ?? null;
|
||||||
|
$targetRelPath = $data->targetRelPath ?? null;
|
||||||
|
|
||||||
|
if (empty($caseId)) throw new BadRequest('caseId is required.');
|
||||||
|
if (empty($sourceRelPath)) throw new BadRequest('sourceRelPath is required.');
|
||||||
|
if (empty($targetRelPath)) throw new BadRequest('targetRelPath is required.');
|
||||||
|
|
||||||
|
$manager = $this->injectableFactory->create(CaseFolderManager::class);
|
||||||
|
return $manager->moveItem($caseId, $sourceRelPath, $targetRelPath);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function postActionMarkForDeletion(Request $request, Response $response): array
|
||||||
|
{
|
||||||
|
$this->checkAccess();
|
||||||
|
$data = $request->getParsedBody();
|
||||||
|
|
||||||
|
$caseId = $data->caseId ?? null;
|
||||||
|
$fileRelPath = $data->fileRelPath ?? null;
|
||||||
|
|
||||||
|
if (empty($caseId)) throw new BadRequest('caseId is required.');
|
||||||
|
if (empty($fileRelPath)) throw new BadRequest('fileRelPath is required.');
|
||||||
|
|
||||||
|
$manager = $this->injectableFactory->create(CaseFolderManager::class);
|
||||||
|
return $manager->markForDeletion($caseId, $fileRelPath);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,6 +11,7 @@
|
|||||||
"No previous conversations": "No previous conversations",
|
"No previous conversations": "No previous conversations",
|
||||||
"Ask the assistant...": "Ask the assistant...",
|
"Ask the assistant...": "Ask the assistant...",
|
||||||
"Thinking": "Thinking...",
|
"Thinking": "Thinking...",
|
||||||
|
"SlowHint": "Still thinking… this one is taking a bit longer because I'm processing multiple sources",
|
||||||
"Error": "Communication error",
|
"Error": "Communication error",
|
||||||
"Back": "Back",
|
"Back": "Back",
|
||||||
"Chat with the assistant": "Chat with the assistant",
|
"Chat with the assistant": "Chat with the assistant",
|
||||||
|
|||||||
@@ -0,0 +1,9 @@
|
|||||||
|
{
|
||||||
|
"fields": {
|
||||||
|
"markedForDeletionAt": "סומן למחיקה בתאריך",
|
||||||
|
"markedForDeletionBy": "סומן למחיקה על ידי"
|
||||||
|
},
|
||||||
|
"tooltips": {
|
||||||
|
"markedForDeletionAt": "מסמך זה סומן למחיקה על ידי שירה ומחכה לאישור ידני של עו\"ד למחיקה הפיזית."
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -11,6 +11,7 @@
|
|||||||
"No previous conversations": "אין שיחות קודמות",
|
"No previous conversations": "אין שיחות קודמות",
|
||||||
"Ask the assistant...": "שאל/י את שירה...",
|
"Ask the assistant...": "שאל/י את שירה...",
|
||||||
"Thinking": "חושבת...",
|
"Thinking": "חושבת...",
|
||||||
|
"SlowHint": "חושבת... זה לוקח קצת יותר זמן כי אני מעבדת כמה מקורות במקביל",
|
||||||
"Error": "שגיאה בתקשורת",
|
"Error": "שגיאה בתקשורת",
|
||||||
"Back": "חזרה",
|
"Back": "חזרה",
|
||||||
"Chat with the assistant": "שוחח/י עם שירה",
|
"Chat with the assistant": "שוחח/י עם שירה",
|
||||||
|
|||||||
@@ -0,0 +1,19 @@
|
|||||||
|
{
|
||||||
|
"fields": {
|
||||||
|
"markedForDeletionAt": {
|
||||||
|
"type": "datetime",
|
||||||
|
"readOnly": true,
|
||||||
|
"tooltip": true
|
||||||
|
},
|
||||||
|
"markedForDeletionBy": {
|
||||||
|
"type": "link",
|
||||||
|
"readOnly": true
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"links": {
|
||||||
|
"markedForDeletionBy": {
|
||||||
|
"type": "belongsTo",
|
||||||
|
"entity": "User"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+12
@@ -27,6 +27,18 @@
|
|||||||
"upcomingHearingDays": {
|
"upcomingHearingDays": {
|
||||||
"type": "int",
|
"type": "int",
|
||||||
"default": 7
|
"default": 7
|
||||||
|
},
|
||||||
|
"maxDocumentChars": {
|
||||||
|
"type": "int",
|
||||||
|
"default": 100000
|
||||||
|
},
|
||||||
|
"agenticLoopEnabled": {
|
||||||
|
"type": "bool",
|
||||||
|
"default": true
|
||||||
|
},
|
||||||
|
"maxAgenticLoops": {
|
||||||
|
"type": "int",
|
||||||
|
"default": 3
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"allowUserAccess": false,
|
"allowUserAccess": false,
|
||||||
|
|||||||
@@ -86,5 +86,13 @@
|
|||||||
"controller": "SmartAssistant",
|
"controller": "SmartAssistant",
|
||||||
"action": "saveMemory"
|
"action": "saveMemory"
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"route": "/SmartAssistant/action/executeTool",
|
||||||
|
"method": "post",
|
||||||
|
"params": {
|
||||||
|
"controller": "SmartAssistant",
|
||||||
|
"action": "executeTool"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -49,7 +49,10 @@ class ActionExecutor
|
|||||||
'schedule_hearing' => $this->scheduleHearing($params, $caseId, $userId),
|
'schedule_hearing' => $this->scheduleHearing($params, $caseId, $userId),
|
||||||
'list_documents' => $this->listDocuments($caseId),
|
'list_documents' => $this->listDocuments($caseId),
|
||||||
'analyze_document' => $this->analyzeDocument($params, $caseId),
|
'analyze_document' => $this->analyzeDocument($params, $caseId),
|
||||||
|
'read_document' => $this->readDocument($params, $caseId),
|
||||||
|
'read_multiple_documents' => $this->readMultipleDocuments($params, $caseId),
|
||||||
'rename_document' => $this->renameDocument($params),
|
'rename_document' => $this->renameDocument($params),
|
||||||
|
'batch_rename_documents' => $this->batchRenameDocuments($params),
|
||||||
'upload_document' => $this->uploadDocument($params, $caseId),
|
'upload_document' => $this->uploadDocument($params, $caseId),
|
||||||
'generate_document' => $this->generateDocument($params, $caseId),
|
'generate_document' => $this->generateDocument($params, $caseId),
|
||||||
'merge_documents' => $this->mergeDocuments($params, $caseId),
|
'merge_documents' => $this->mergeDocuments($params, $caseId),
|
||||||
@@ -383,6 +386,65 @@ class ActionExecutor
|
|||||||
return ['success' => true, 'message' => implode("\n", $lines), 'data' => ['filePath' => $filePath, 'hasTextContent' => $text !== null]];
|
return ['success' => true, 'message' => implode("\n", $lines), 'data' => ['filePath' => $filePath, 'hasTextContent' => $text !== null]];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function readDocument(array $params, ?string $caseId): array
|
||||||
|
{
|
||||||
|
$filePath = $params['filePath'] ?? null;
|
||||||
|
if (!$filePath) throw new Error('filePath parameter is required.');
|
||||||
|
|
||||||
|
$analyzer = $this->injectableFactory->create(DocumentAnalyzer::class);
|
||||||
|
$text = $analyzer->extractFullText($filePath);
|
||||||
|
$fileName = basename($filePath);
|
||||||
|
|
||||||
|
if ($text === null) {
|
||||||
|
return ['success' => false, 'message' => "לא ניתן לחלץ טקסט מ-\"{$fileName}\""];
|
||||||
|
}
|
||||||
|
|
||||||
|
return [
|
||||||
|
'success' => true,
|
||||||
|
'message' => $text,
|
||||||
|
'data' => [
|
||||||
|
'filePath' => $filePath,
|
||||||
|
'fileName' => $fileName,
|
||||||
|
'charCount' => mb_strlen($text),
|
||||||
|
],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
private function readMultipleDocuments(array $params, ?string $caseId): array
|
||||||
|
{
|
||||||
|
$filePaths = $params['filePaths'] ?? [];
|
||||||
|
if (empty($filePaths)) throw new Error('filePaths parameter is required (array of file paths).');
|
||||||
|
|
||||||
|
$maxPerFile = $params['maxCharsPerFile'] ?? 50000;
|
||||||
|
$analyzer = $this->injectableFactory->create(DocumentAnalyzer::class);
|
||||||
|
$results = $analyzer->extractMultipleDocuments($filePaths, $maxPerFile);
|
||||||
|
|
||||||
|
$lines = [];
|
||||||
|
$successCount = 0;
|
||||||
|
|
||||||
|
foreach ($results as $r) {
|
||||||
|
$lines[] = "=== {$r['fileName']} ===";
|
||||||
|
if (!empty($r['error'])) {
|
||||||
|
$lines[] = "שגיאה: {$r['error']}";
|
||||||
|
} elseif ($r['text'] === null) {
|
||||||
|
$lines[] = "לא ניתן לחלץ טקסט";
|
||||||
|
} else {
|
||||||
|
$lines[] = $r['text'];
|
||||||
|
$successCount++;
|
||||||
|
}
|
||||||
|
$lines[] = '';
|
||||||
|
}
|
||||||
|
|
||||||
|
return [
|
||||||
|
'success' => true,
|
||||||
|
'message' => implode("\n", $lines),
|
||||||
|
'data' => [
|
||||||
|
'totalFiles' => count($filePaths),
|
||||||
|
'successCount' => $successCount,
|
||||||
|
],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
private function renameDocument(array $params): array
|
private function renameDocument(array $params): array
|
||||||
{
|
{
|
||||||
$sourcePath = $params['sourcePath'] ?? null;
|
$sourcePath = $params['sourcePath'] ?? null;
|
||||||
@@ -395,6 +457,37 @@ class ActionExecutor
|
|||||||
return ['success' => true, 'message' => "המסמך שונה מ-\"{$result['oldName']}\" ל-\"{$result['newName']}\"", 'data' => $result];
|
return ['success' => true, 'message' => "המסמך שונה מ-\"{$result['oldName']}\" ל-\"{$result['newName']}\"", 'data' => $result];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function batchRenameDocuments(array $params): array
|
||||||
|
{
|
||||||
|
$renames = $params['renames'] ?? [];
|
||||||
|
if (empty($renames)) throw new Error('renames parameter is required (array of {sourcePath, newName}).');
|
||||||
|
|
||||||
|
$analyzer = $this->injectableFactory->create(DocumentAnalyzer::class);
|
||||||
|
$results = [];
|
||||||
|
$successCount = 0;
|
||||||
|
|
||||||
|
foreach ($renames as $rename) {
|
||||||
|
$sourcePath = $rename['sourcePath'] ?? null;
|
||||||
|
$newName = $rename['newName'] ?? null;
|
||||||
|
|
||||||
|
if (!$sourcePath || !$newName) {
|
||||||
|
$results[] = "שגיאה: חסר sourcePath או newName";
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
$result = $analyzer->renameDocument($sourcePath, $newName);
|
||||||
|
$results[] = "\"{$result['oldName']}\" → \"{$result['newName']}\"";
|
||||||
|
$successCount++;
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
$results[] = "שגיאה בשינוי שם \"" . basename($sourcePath) . "\": " . $e->getMessage();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$msg = "שינוי שמות: {$successCount}/" . count($renames) . " הצליחו\n" . implode("\n", $results);
|
||||||
|
return ['success' => true, 'message' => $msg];
|
||||||
|
}
|
||||||
|
|
||||||
private function uploadDocument(array $params, ?string $caseId): array
|
private function uploadDocument(array $params, ?string $caseId): array
|
||||||
{
|
{
|
||||||
if (!$caseId) {
|
if (!$caseId) {
|
||||||
|
|||||||
@@ -46,7 +46,7 @@ class AlertCalculator
|
|||||||
->where(['status!=' => self::CLOSED_STATUSES, 'deleted' => false])->count();
|
->where(['status!=' => self::CLOSED_STATUSES, 'deleted' => false])->count();
|
||||||
|
|
||||||
$totalOverdueTasks = $this->entityManager->getRDBRepository('Task')
|
$totalOverdueTasks = $this->entityManager->getRDBRepository('Task')
|
||||||
->where(['status!=' => ['Completed', 'Canceled', 'Deferred'], 'dateEnd<' => $today, 'dateEnd!=' => null, 'deleted' => false, 'parentType' => 'Case'])->count();
|
->where(['status!=' => ['Completed', 'Canceled', 'Deferred'], 'dateEnd<' => $today, 'dateEnd!=' => null, 'deleted' => false, 'parentType' => ['Case', 'Contact']])->count();
|
||||||
|
|
||||||
$upcomingHearings = $this->entityManager->getRDBRepository('Case')
|
$upcomingHearings = $this->entityManager->getRDBRepository('Case')
|
||||||
->where(['cNextHearing>=' => $today, 'cNextHearing<=' => $weekEnd, 'status!=' => self::CLOSED_STATUSES, 'deleted' => false])->count();
|
->where(['cNextHearing>=' => $today, 'cNextHearing<=' => $weekEnd, 'status!=' => self::CLOSED_STATUSES, 'deleted' => false])->count();
|
||||||
@@ -69,11 +69,12 @@ class AlertCalculator
|
|||||||
{
|
{
|
||||||
$alerts = [];
|
$alerts = [];
|
||||||
$now = new \DateTime();
|
$now = new \DateTime();
|
||||||
|
$today = $now->format('Y-m-d');
|
||||||
$warningThreshold = (new \DateTime())->modify("-{$warningDays} days")->format('Y-m-d H:i:s');
|
$warningThreshold = (new \DateTime())->modify("-{$warningDays} days")->format('Y-m-d H:i:s');
|
||||||
$criticalThreshold = (new \DateTime())->modify("-{$criticalDays} days")->format('Y-m-d H:i:s');
|
$criticalThreshold = (new \DateTime())->modify("-{$criticalDays} days")->format('Y-m-d H:i:s');
|
||||||
|
|
||||||
$cases = $this->entityManager->getRDBRepository('Case')
|
$cases = $this->entityManager->getRDBRepository('Case')
|
||||||
->select(['id', 'name', 'status', 'assignedUserName', 'cLastActivityAt', 'createdAt'])
|
->select(['id', 'name', 'status', 'assignedUserName', 'cLastActivityAt', 'createdAt', 'cNextHearing'])
|
||||||
->where([
|
->where([
|
||||||
'status!=' => self::CLOSED_STATUSES, 'deleted' => false,
|
'status!=' => self::CLOSED_STATUSES, 'deleted' => false,
|
||||||
'OR' => [
|
'OR' => [
|
||||||
@@ -83,6 +84,17 @@ class AlertCalculator
|
|||||||
])->order('cLastActivityAt', 'ASC')->limit(0, 50)->find();
|
])->order('cLastActivityAt', 'ASC')->limit(0, 50)->find();
|
||||||
|
|
||||||
foreach ($cases as $case) {
|
foreach ($cases as $case) {
|
||||||
|
$status = $case->get('status');
|
||||||
|
|
||||||
|
// Suppress inactivity alerts for cases legitimately awaiting court action.
|
||||||
|
// PendingDecision: nothing the lawyer can do until the court rules.
|
||||||
|
// PendingHearing: nothing to do if a future hearing is already scheduled.
|
||||||
|
if ($status === 'PendingDecision') continue;
|
||||||
|
if ($status === 'PendingHearing') {
|
||||||
|
$nextHearing = $case->get('cNextHearing');
|
||||||
|
if ($nextHearing && $nextHearing >= $today) continue;
|
||||||
|
}
|
||||||
|
|
||||||
$lastActivity = $case->get('cLastActivityAt') ?? $case->get('createdAt');
|
$lastActivity = $case->get('cLastActivityAt') ?? $case->get('createdAt');
|
||||||
if (!$lastActivity) continue;
|
if (!$lastActivity) continue;
|
||||||
$daysSince = $now->diff(new \DateTime($lastActivity))->days;
|
$daysSince = $now->diff(new \DateTime($lastActivity))->days;
|
||||||
@@ -106,7 +118,7 @@ class AlertCalculator
|
|||||||
|
|
||||||
$tasks = $this->entityManager->getRDBRepository('Task')
|
$tasks = $this->entityManager->getRDBRepository('Task')
|
||||||
->select(['id', 'name', 'dateEnd', 'status', 'parentId', 'parentType', 'parentName', 'assignedUserName'])
|
->select(['id', 'name', 'dateEnd', 'status', 'parentId', 'parentType', 'parentName', 'assignedUserName'])
|
||||||
->where(['status!=' => ['Completed', 'Canceled', 'Deferred'], 'dateEnd<' => $today, 'dateEnd!=' => null, 'deleted' => false, 'parentType' => 'Case'])
|
->where(['status!=' => ['Completed', 'Canceled', 'Deferred'], 'dateEnd<' => $today, 'dateEnd!=' => null, 'deleted' => false, 'parentType' => ['Case', 'Contact']])
|
||||||
->order('dateEnd', 'ASC')->limit(0, 50)->find();
|
->order('dateEnd', 'ASC')->limit(0, 50)->find();
|
||||||
|
|
||||||
foreach ($tasks as $task) {
|
foreach ($tasks as $task) {
|
||||||
@@ -132,7 +144,7 @@ class AlertCalculator
|
|||||||
$recentTaskThreshold = date('Y-m-d H:i:s', strtotime('-7 days'));
|
$recentTaskThreshold = date('Y-m-d H:i:s', strtotime('-7 days'));
|
||||||
|
|
||||||
$cases = $this->entityManager->getRDBRepository('Case')
|
$cases = $this->entityManager->getRDBRepository('Case')
|
||||||
->select(['id', 'name', 'cNextHearing', 'assignedUserName', 'cLastActivityAt'])
|
->select(['id', 'name', 'cNextHearing', 'assignedUserName', 'cLastActivityAt', 'contactId'])
|
||||||
->where(['cNextHearing>=' => $today, 'cNextHearing<=' => $threshold, 'status!=' => self::CLOSED_STATUSES, 'deleted' => false])
|
->where(['cNextHearing>=' => $today, 'cNextHearing<=' => $threshold, 'status!=' => self::CLOSED_STATUSES, 'deleted' => false])
|
||||||
->order('cNextHearing', 'ASC')->find();
|
->order('cNextHearing', 'ASC')->find();
|
||||||
|
|
||||||
@@ -140,13 +152,7 @@ class AlertCalculator
|
|||||||
$lastActivity = $case->get('cLastActivityAt');
|
$lastActivity = $case->get('cLastActivityAt');
|
||||||
if ($lastActivity && $lastActivity >= $recentActivityThreshold) continue;
|
if ($lastActivity && $lastActivity >= $recentActivityThreshold) continue;
|
||||||
|
|
||||||
$openTaskCount = $this->entityManager->getRDBRepository('Task')
|
if ($this->caseHasPreparation($case, $recentTaskThreshold)) continue;
|
||||||
->where(['parentId' => $case->get('id'), 'parentType' => 'Case', 'status!=' => ['Completed', 'Canceled', 'Deferred'], 'deleted' => false])->count();
|
|
||||||
if ($openTaskCount > 0) continue;
|
|
||||||
|
|
||||||
$recentCompleted = $this->entityManager->getRDBRepository('Task')
|
|
||||||
->where(['parentId' => $case->get('id'), 'parentType' => 'Case', 'status' => 'Completed', 'modifiedAt>=' => $recentTaskThreshold, 'deleted' => false])->count();
|
|
||||||
if ($recentCompleted > 0) continue;
|
|
||||||
|
|
||||||
$daysUntil = (new \DateTime())->diff(new \DateTime($case->get('cNextHearing')))->days;
|
$daysUntil = (new \DateTime())->diff(new \DateTime($case->get('cNextHearing')))->days;
|
||||||
$severity = ($daysUntil <= 1) ? 'critical' : 'warning';
|
$severity = ($daysUntil <= 1) ? 'critical' : 'warning';
|
||||||
@@ -162,6 +168,104 @@ class AlertCalculator
|
|||||||
return $alerts;
|
return $alerts;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if a case has any evidence of hearing preparation:
|
||||||
|
* 1. Tasks linked directly to the case (parentType='Case')
|
||||||
|
* 2. Tasks linked to the case's contacts (parentType='Contact')
|
||||||
|
* 3. Tasks linked through NhActivity records for this case
|
||||||
|
*/
|
||||||
|
private function caseHasPreparation(\Espo\ORM\Entity $case, string $recentTaskThreshold): bool
|
||||||
|
{
|
||||||
|
$caseId = $case->get('id');
|
||||||
|
|
||||||
|
// Collect all parentId+parentType pairs that relate to this case
|
||||||
|
$parentConditions = [
|
||||||
|
['parentType' => 'Case', 'parentId' => $caseId],
|
||||||
|
];
|
||||||
|
|
||||||
|
// Also check tasks linked to the case's contacts
|
||||||
|
$contactIds = $this->getCaseContactIds($case);
|
||||||
|
if (!empty($contactIds)) {
|
||||||
|
$parentConditions[] = ['parentType' => 'Contact', 'parentId' => $contactIds];
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check 1: Any open tasks linked to the case or its contacts
|
||||||
|
$openTaskCount = $this->entityManager->getRDBRepository('Task')
|
||||||
|
->where([
|
||||||
|
'OR' => $parentConditions,
|
||||||
|
'status!=' => ['Completed', 'Canceled', 'Deferred'],
|
||||||
|
'deleted' => false,
|
||||||
|
])->count();
|
||||||
|
if ($openTaskCount > 0) return true;
|
||||||
|
|
||||||
|
// Check 2: Any recently completed tasks linked to the case or its contacts
|
||||||
|
$recentCompleted = $this->entityManager->getRDBRepository('Task')
|
||||||
|
->where([
|
||||||
|
'OR' => $parentConditions,
|
||||||
|
'status' => 'Completed',
|
||||||
|
'modifiedAt>=' => $recentTaskThreshold,
|
||||||
|
'deleted' => false,
|
||||||
|
])->count();
|
||||||
|
if ($recentCompleted > 0) return true;
|
||||||
|
|
||||||
|
// Check 3: Tasks linked through NhActivity records for this case
|
||||||
|
$nhActivityTaskCount = $this->entityManager->getRDBRepository('NhActivity')
|
||||||
|
->where([
|
||||||
|
'caseId' => $caseId,
|
||||||
|
'taskId!=' => null,
|
||||||
|
'deleted' => false,
|
||||||
|
])->count();
|
||||||
|
|
||||||
|
if ($nhActivityTaskCount > 0) {
|
||||||
|
$nhActivities = $this->entityManager->getRDBRepository('NhActivity')
|
||||||
|
->select(['taskId'])
|
||||||
|
->where([
|
||||||
|
'caseId' => $caseId,
|
||||||
|
'taskId!=' => null,
|
||||||
|
'deleted' => false,
|
||||||
|
])->find();
|
||||||
|
|
||||||
|
$taskIds = [];
|
||||||
|
foreach ($nhActivities as $nha) {
|
||||||
|
$taskIds[] = $nha->get('taskId');
|
||||||
|
}
|
||||||
|
|
||||||
|
$activeNhTasks = $this->entityManager->getRDBRepository('Task')
|
||||||
|
->where([
|
||||||
|
'id' => $taskIds,
|
||||||
|
'status!=' => ['Canceled'],
|
||||||
|
'deleted' => false,
|
||||||
|
])->count();
|
||||||
|
if ($activeNhTasks > 0) return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function getCaseContactIds(\Espo\ORM\Entity $case): array
|
||||||
|
{
|
||||||
|
$contactIds = [];
|
||||||
|
|
||||||
|
$primaryContactId = $case->get('contactId');
|
||||||
|
if ($primaryContactId) {
|
||||||
|
$contactIds[] = $primaryContactId;
|
||||||
|
}
|
||||||
|
|
||||||
|
$contacts = $this->entityManager->getRDBRepository('Case')
|
||||||
|
->getRelation($case, 'contacts')
|
||||||
|
->select(['id'])
|
||||||
|
->find();
|
||||||
|
|
||||||
|
foreach ($contacts as $contact) {
|
||||||
|
$id = $contact->get('id');
|
||||||
|
if (!in_array($id, $contactIds)) {
|
||||||
|
$contactIds[] = $id;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $contactIds;
|
||||||
|
}
|
||||||
|
|
||||||
private function findUnassignedNewCases(): array
|
private function findUnassignedNewCases(): array
|
||||||
{
|
{
|
||||||
$alerts = [];
|
$alerts = [];
|
||||||
@@ -191,7 +295,7 @@ class AlertCalculator
|
|||||||
|
|
||||||
$tasks = $this->entityManager->getRDBRepository('Task')
|
$tasks = $this->entityManager->getRDBRepository('Task')
|
||||||
->select(['id', 'name', 'dateEnd', 'parentId', 'parentType', 'parentName', 'assignedUserName'])
|
->select(['id', 'name', 'dateEnd', 'parentId', 'parentType', 'parentName', 'assignedUserName'])
|
||||||
->where(['status!=' => ['Completed', 'Canceled', 'Deferred'], 'dateEnd>=' => $today, 'dateEnd<=' => $threshold, 'deleted' => false, 'parentType' => 'Case'])
|
->where(['status!=' => ['Completed', 'Canceled', 'Deferred'], 'dateEnd>=' => $today, 'dateEnd<=' => $threshold, 'deleted' => false, 'parentType' => ['Case', 'Contact']])
|
||||||
->order('dateEnd', 'ASC')->limit(0, 30)->find();
|
->order('dateEnd', 'ASC')->limit(0, 30)->find();
|
||||||
|
|
||||||
foreach ($tasks as $task) {
|
foreach ($tasks as $task) {
|
||||||
@@ -237,7 +341,7 @@ class AlertCalculator
|
|||||||
|
|
||||||
foreach (array_keys($userCounts) as $uid) {
|
foreach (array_keys($userCounts) as $uid) {
|
||||||
$userCounts[$uid]['openTasks'] = $this->entityManager->getRDBRepository('Task')
|
$userCounts[$uid]['openTasks'] = $this->entityManager->getRDBRepository('Task')
|
||||||
->where(['assignedUserId' => $uid, 'status!=' => ['Completed', 'Canceled', 'Deferred'], 'deleted' => false, 'parentType' => 'Case'])->count();
|
->where(['assignedUserId' => $uid, 'status!=' => ['Completed', 'Canceled', 'Deferred'], 'deleted' => false, 'parentType' => ['Case', 'Contact']])->count();
|
||||||
}
|
}
|
||||||
|
|
||||||
return array_values($userCounts);
|
return array_values($userCounts);
|
||||||
|
|||||||
@@ -33,6 +33,7 @@ class CaseContextBuilder
|
|||||||
'recentNotes' => $this->getRecentNotes($caseId),
|
'recentNotes' => $this->getRecentNotes($caseId),
|
||||||
'availableTemplates' => $this->getAvailableTemplates(),
|
'availableTemplates' => $this->getAvailableTemplates(),
|
||||||
'documents' => $this->getDocumentListing($caseId),
|
'documents' => $this->getDocumentListing($caseId),
|
||||||
|
'signatureRequests' => $this->getSignatureRequests($caseId),
|
||||||
'currentUser' => $this->getUserData($userId),
|
'currentUser' => $this->getUserData($userId),
|
||||||
'validStatuses' => ActionExecutor::VALID_STATUSES,
|
'validStatuses' => ActionExecutor::VALID_STATUSES,
|
||||||
];
|
];
|
||||||
@@ -82,9 +83,18 @@ class CaseContextBuilder
|
|||||||
|
|
||||||
private function getOpenTasks(string $caseId): array
|
private function getOpenTasks(string $caseId): array
|
||||||
{
|
{
|
||||||
|
$parentConditions = [
|
||||||
|
['parentType' => 'Case', 'parentId' => $caseId],
|
||||||
|
];
|
||||||
|
|
||||||
|
$contactIds = $this->getCaseContactIds($caseId);
|
||||||
|
if (!empty($contactIds)) {
|
||||||
|
$parentConditions[] = ['parentType' => 'Contact', 'parentId' => $contactIds];
|
||||||
|
}
|
||||||
|
|
||||||
$tasks = [];
|
$tasks = [];
|
||||||
$collection = $this->entityManager->getRDBRepository('Task')
|
$collection = $this->entityManager->getRDBRepository('Task')
|
||||||
->where(['parentType' => 'Case', 'parentId' => $caseId, 'status!=' => ['Completed', 'Canceled']])
|
->where(['OR' => $parentConditions, 'status!=' => ['Completed', 'Canceled']])
|
||||||
->order('dateEnd', 'ASC')->limit(0, 10)->find();
|
->order('dateEnd', 'ASC')->limit(0, 10)->find();
|
||||||
|
|
||||||
foreach ($collection as $t) {
|
foreach ($collection as $t) {
|
||||||
@@ -97,6 +107,32 @@ class CaseContextBuilder
|
|||||||
return $tasks;
|
return $tasks;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function getCaseContactIds(string $caseId): array
|
||||||
|
{
|
||||||
|
$case = $this->entityManager->getEntityById('Case', $caseId);
|
||||||
|
if (!$case) return [];
|
||||||
|
|
||||||
|
$contactIds = [];
|
||||||
|
$primaryContactId = $case->get('contactId');
|
||||||
|
if ($primaryContactId) {
|
||||||
|
$contactIds[] = $primaryContactId;
|
||||||
|
}
|
||||||
|
|
||||||
|
$contacts = $this->entityManager->getRDBRepository('Case')
|
||||||
|
->getRelation($case, 'contacts')
|
||||||
|
->select(['id'])
|
||||||
|
->find();
|
||||||
|
|
||||||
|
foreach ($contacts as $contact) {
|
||||||
|
$id = $contact->get('id');
|
||||||
|
if (!in_array($id, $contactIds)) {
|
||||||
|
$contactIds[] = $id;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $contactIds;
|
||||||
|
}
|
||||||
|
|
||||||
private function getUpcomingMeetings(string $caseId): array
|
private function getUpcomingMeetings(string $caseId): array
|
||||||
{
|
{
|
||||||
$meetings = [];
|
$meetings = [];
|
||||||
@@ -152,10 +188,20 @@ class CaseContextBuilder
|
|||||||
{
|
{
|
||||||
$templates = [];
|
$templates = [];
|
||||||
$collection = $this->entityManager->getRDBRepository('DocumentTemplate')
|
$collection = $this->entityManager->getRDBRepository('DocumentTemplate')
|
||||||
->where(['entityType' => 'Case'])->order('name', 'ASC')->find();
|
->where([
|
||||||
|
'targetEntityType' => 'Case',
|
||||||
|
'isActive' => true,
|
||||||
|
])
|
||||||
|
->order('name', 'ASC')
|
||||||
|
->find();
|
||||||
|
|
||||||
foreach ($collection as $t) {
|
foreach ($collection as $t) {
|
||||||
$templates[] = ['id' => $t->get('id'), 'name' => $t->get('name')];
|
$templates[] = [
|
||||||
|
'id' => $t->get('id'),
|
||||||
|
'name' => $t->get('name'),
|
||||||
|
'category' => $t->get('category'),
|
||||||
|
'description' => $t->get('description'),
|
||||||
|
];
|
||||||
}
|
}
|
||||||
return $templates;
|
return $templates;
|
||||||
}
|
}
|
||||||
@@ -167,13 +213,51 @@ class CaseContextBuilder
|
|||||||
return ['id' => $user->get('id'), 'name' => $user->get('name')];
|
return ['id' => $user->get('id'), 'name' => $user->get('name')];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function getSignatureRequests(string $caseId): array
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
$collection = $this->entityManager->getRDBRepository('SignatureRequest')
|
||||||
|
->where([
|
||||||
|
'caseId' => $caseId,
|
||||||
|
'status!=' => ['Completed', 'Voided'],
|
||||||
|
])
|
||||||
|
->order('createdAt', 'DESC')
|
||||||
|
->limit(0, 10)
|
||||||
|
->find();
|
||||||
|
|
||||||
|
$requests = [];
|
||||||
|
|
||||||
|
foreach ($collection as $sr) {
|
||||||
|
$requests[] = [
|
||||||
|
'id' => $sr->get('id'),
|
||||||
|
'name' => $sr->get('name'),
|
||||||
|
'status' => $sr->get('status'),
|
||||||
|
'sentAt' => $sr->get('sentAt'),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
return $requests;
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
// SignatureRequest entity might not exist if DigitalSignature not installed
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private function getDocumentListing(string $caseId): array
|
private function getDocumentListing(string $caseId): array
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
$analyzer = $this->injectableFactory->create(DocumentAnalyzer::class);
|
$analyzer = $this->injectableFactory->create(DocumentAnalyzer::class);
|
||||||
$result = $analyzer->listDocumentsRecursive($caseId);
|
$result = $analyzer->listDocumentsRecursive($caseId);
|
||||||
|
|
||||||
$simplified = ['totalFiles' => $result['totalFiles'], 'folders' => [], 'rootFiles' => []];
|
$simplified = [
|
||||||
|
'totalFiles' => $result['totalFiles'],
|
||||||
|
'folders' => [],
|
||||||
|
'rootFiles' => [],
|
||||||
|
'resolvedPath' => $result['resolvedPath'] ?? null,
|
||||||
|
'pathSource' => $result['pathSource'] ?? null,
|
||||||
|
'folderExists' => $result['folderExists'] ?? null,
|
||||||
|
'diagnostic' => $result['reason'] ?? null,
|
||||||
|
];
|
||||||
|
|
||||||
foreach ($result['folders'] as $folder) {
|
foreach ($result['folders'] as $folder) {
|
||||||
$files = [];
|
$files = [];
|
||||||
|
|||||||
@@ -0,0 +1,334 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Espo\Modules\SmartAssistant\Services;
|
||||||
|
|
||||||
|
use Espo\Core\Exceptions\BadRequest;
|
||||||
|
use Espo\Core\Exceptions\Error;
|
||||||
|
use Espo\Core\Exceptions\Forbidden;
|
||||||
|
use Espo\Core\Exceptions\NotFound;
|
||||||
|
use Espo\Core\InjectableFactory;
|
||||||
|
use Espo\Core\Utils\Log;
|
||||||
|
use Espo\ORM\EntityManager;
|
||||||
|
use Espo\Entities\User;
|
||||||
|
use Espo\Modules\NetworkStorageIntegration\Services\NetworkDocumentService;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Wraps NetworkDocumentService and constrains every operation to the case's
|
||||||
|
* own folder. Never exposes a physical delete — only soft-delete via
|
||||||
|
* markForDeletion().
|
||||||
|
*/
|
||||||
|
class CaseFolderManager
|
||||||
|
{
|
||||||
|
private const DELETION_FOLDER_NAME = 'מסמכים למחיקה';
|
||||||
|
private const DELETION_PREFIX = 'למחיקה - ';
|
||||||
|
|
||||||
|
public function __construct(
|
||||||
|
private EntityManager $entityManager,
|
||||||
|
private InjectableFactory $injectableFactory,
|
||||||
|
private Log $log,
|
||||||
|
private User $user
|
||||||
|
) {}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array{success: bool, path: string, size: int}
|
||||||
|
*/
|
||||||
|
public function writeFile(string $caseId, string $relPath, string $content): array
|
||||||
|
{
|
||||||
|
$absInsideStorage = $this->resolveSafePath($caseId, $relPath, requireExists: false);
|
||||||
|
$client = $this->getNds()->getClient();
|
||||||
|
|
||||||
|
$parent = $this->dirnameRel($absInsideStorage);
|
||||||
|
if ($parent !== '') {
|
||||||
|
$client->createFolderRecursive($parent);
|
||||||
|
}
|
||||||
|
|
||||||
|
$ok = $client->uploadFile($absInsideStorage, $content);
|
||||||
|
if (!$ok) {
|
||||||
|
throw new Error("Failed to write file: {$absInsideStorage}");
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->log->info("CaseFolderManager: wrote '{$absInsideStorage}' (case {$caseId})");
|
||||||
|
|
||||||
|
return [
|
||||||
|
'success' => true,
|
||||||
|
'path' => $absInsideStorage,
|
||||||
|
'size' => strlen($content),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array{success: bool, path: string, alreadyExisted: bool}
|
||||||
|
*/
|
||||||
|
public function createFolder(string $caseId, string $relPath): array
|
||||||
|
{
|
||||||
|
$abs = $this->resolveSafePath($caseId, $relPath, requireExists: false);
|
||||||
|
$client = $this->getNds()->getClient();
|
||||||
|
|
||||||
|
$alreadyExisted = $client->exists($abs);
|
||||||
|
if (!$alreadyExisted) {
|
||||||
|
$ok = $client->createFolderRecursive($abs);
|
||||||
|
if (!$ok) {
|
||||||
|
throw new Error("Failed to create folder: {$abs}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->log->info("CaseFolderManager: created folder '{$abs}' (case {$caseId})");
|
||||||
|
|
||||||
|
return [
|
||||||
|
'success' => true,
|
||||||
|
'path' => $abs,
|
||||||
|
'alreadyExisted' => $alreadyExisted,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array{success: bool, oldPath: string, newPath: string}
|
||||||
|
*/
|
||||||
|
public function renameItem(string $caseId, string $currentRelPath, string $newName): array
|
||||||
|
{
|
||||||
|
$newName = trim($newName);
|
||||||
|
if ($newName === '' || str_contains($newName, '/') || str_contains($newName, '\\')) {
|
||||||
|
throw new BadRequest('newName must be a plain file/folder name without slashes.');
|
||||||
|
}
|
||||||
|
|
||||||
|
$absCurrent = $this->resolveSafePath($caseId, $currentRelPath, requireExists: true);
|
||||||
|
|
||||||
|
$parent = $this->dirnameRel($absCurrent);
|
||||||
|
$absNew = $parent === '' ? $newName : ($parent . '/' . $newName);
|
||||||
|
|
||||||
|
// Confirm the new path is still inside the case folder.
|
||||||
|
$this->assertWithinCaseRoot($caseId, $absNew);
|
||||||
|
|
||||||
|
$client = $this->getNds()->getClient();
|
||||||
|
if ($client->exists($absNew)) {
|
||||||
|
throw new Error("Target already exists: {$absNew}");
|
||||||
|
}
|
||||||
|
|
||||||
|
$ok = $client->move($absCurrent, $absNew);
|
||||||
|
if (!$ok) {
|
||||||
|
throw new Error("Failed to rename: {$absCurrent} -> {$absNew}");
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->log->info("CaseFolderManager: renamed '{$absCurrent}' -> '{$absNew}' (case {$caseId})");
|
||||||
|
|
||||||
|
return [
|
||||||
|
'success' => true,
|
||||||
|
'oldPath' => $absCurrent,
|
||||||
|
'newPath' => $absNew,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array{success: bool, oldPath: string, newPath: string}
|
||||||
|
*/
|
||||||
|
public function moveItem(string $caseId, string $sourceRelPath, string $targetRelPath): array
|
||||||
|
{
|
||||||
|
$absSource = $this->resolveSafePath($caseId, $sourceRelPath, requireExists: true);
|
||||||
|
$absTarget = $this->resolveSafePath($caseId, $targetRelPath, requireExists: false);
|
||||||
|
|
||||||
|
$client = $this->getNds()->getClient();
|
||||||
|
|
||||||
|
$parent = $this->dirnameRel($absTarget);
|
||||||
|
if ($parent !== '' && !$client->exists($parent)) {
|
||||||
|
$client->createFolderRecursive($parent);
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($client->exists($absTarget)) {
|
||||||
|
throw new Error("Target already exists: {$absTarget}");
|
||||||
|
}
|
||||||
|
|
||||||
|
$ok = $client->move($absSource, $absTarget);
|
||||||
|
if (!$ok) {
|
||||||
|
throw new Error("Failed to move: {$absSource} -> {$absTarget}");
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->log->info("CaseFolderManager: moved '{$absSource}' -> '{$absTarget}' (case {$caseId})");
|
||||||
|
|
||||||
|
return [
|
||||||
|
'success' => true,
|
||||||
|
'oldPath' => $absSource,
|
||||||
|
'newPath' => $absTarget,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Soft-delete: move file into the case's "מסמכים למחיקה" folder with a
|
||||||
|
* "למחיקה - " prefix. Never physically deletes. Also updates the linked
|
||||||
|
* Document entity (if any) so admins can audit pending deletions.
|
||||||
|
*
|
||||||
|
* @return array{success: bool, originalPath: string, newPath: string, deletionFolder: string}
|
||||||
|
*/
|
||||||
|
public function markForDeletion(string $caseId, string $fileRelPath): array
|
||||||
|
{
|
||||||
|
$absSource = $this->resolveSafePath($caseId, $fileRelPath, requireExists: true);
|
||||||
|
$caseRoot = $this->getCaseRoot($caseId);
|
||||||
|
|
||||||
|
$deletionFolder = $caseRoot . '/' . self::DELETION_FOLDER_NAME;
|
||||||
|
$client = $this->getNds()->getClient();
|
||||||
|
|
||||||
|
if (!$client->exists($deletionFolder)) {
|
||||||
|
$client->createFolderRecursive($deletionFolder);
|
||||||
|
}
|
||||||
|
|
||||||
|
$baseName = basename($absSource);
|
||||||
|
$targetName = self::DELETION_PREFIX . $baseName;
|
||||||
|
$absTarget = $deletionFolder . '/' . $targetName;
|
||||||
|
|
||||||
|
// Avoid name collisions with already-marked files.
|
||||||
|
if ($client->exists($absTarget)) {
|
||||||
|
$stamp = date('Y-m-d_H-i-s');
|
||||||
|
$targetName = self::DELETION_PREFIX . $stamp . ' - ' . $baseName;
|
||||||
|
$absTarget = $deletionFolder . '/' . $targetName;
|
||||||
|
}
|
||||||
|
|
||||||
|
$ok = $client->move($absSource, $absTarget);
|
||||||
|
if (!$ok) {
|
||||||
|
throw new Error("Failed to mark for deletion: {$absSource}");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Best-effort: update Document entity if we can find one pointing to this file.
|
||||||
|
$this->touchDocumentForDeletion($absSource, $absTarget);
|
||||||
|
|
||||||
|
$this->log->info(
|
||||||
|
"CaseFolderManager: marked for deletion '{$absSource}' -> '{$absTarget}' " .
|
||||||
|
"(case {$caseId}, by user {$this->user->getId()})"
|
||||||
|
);
|
||||||
|
|
||||||
|
return [
|
||||||
|
'success' => true,
|
||||||
|
'originalPath' => $absSource,
|
||||||
|
'newPath' => $absTarget,
|
||||||
|
'deletionFolder' => $deletionFolder,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- internals ---------------------------------------------------------
|
||||||
|
|
||||||
|
private function getNds(): NetworkDocumentService
|
||||||
|
{
|
||||||
|
return $this->injectableFactory->create(NetworkDocumentService::class);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return the storage-relative root folder of the case.
|
||||||
|
*/
|
||||||
|
private function getCaseRoot(string $caseId): string
|
||||||
|
{
|
||||||
|
$root = $this->getNds()->getEntityFolderPath('Case', $caseId);
|
||||||
|
if (!$root) {
|
||||||
|
throw new NotFound("No folder is configured for Case {$caseId}.");
|
||||||
|
}
|
||||||
|
$root = $this->normalize($root);
|
||||||
|
if ($root === '') {
|
||||||
|
throw new Error("Case {$caseId} resolved to an empty folder path; refusing.");
|
||||||
|
}
|
||||||
|
return $root;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Normalize + reject traversal, return storage-relative path inside the case folder.
|
||||||
|
* If $requireExists, ensures the resolved path exists in storage.
|
||||||
|
*/
|
||||||
|
private function resolveSafePath(string $caseId, string $relPath, bool $requireExists): string
|
||||||
|
{
|
||||||
|
$relPath = trim((string) $relPath);
|
||||||
|
if ($relPath === '') {
|
||||||
|
throw new BadRequest('Path is required.');
|
||||||
|
}
|
||||||
|
if (str_starts_with($relPath, '/') || str_starts_with($relPath, '\\')) {
|
||||||
|
throw new BadRequest('Absolute paths are not allowed.');
|
||||||
|
}
|
||||||
|
|
||||||
|
$caseRoot = $this->getCaseRoot($caseId);
|
||||||
|
|
||||||
|
// Allow the caller to either pass a path relative to the case root, OR a
|
||||||
|
// full storage-relative path that already includes the case root.
|
||||||
|
$normalizedRel = $this->normalize($relPath);
|
||||||
|
$candidate = str_starts_with($normalizedRel, $caseRoot . '/') || $normalizedRel === $caseRoot
|
||||||
|
? $normalizedRel
|
||||||
|
: $this->normalize($caseRoot . '/' . $normalizedRel);
|
||||||
|
|
||||||
|
$this->assertWithinCaseRoot($caseId, $candidate);
|
||||||
|
|
||||||
|
if ($requireExists) {
|
||||||
|
$client = $this->getNds()->getClient();
|
||||||
|
if (!$client->exists($candidate)) {
|
||||||
|
throw new NotFound("Path not found: {$candidate}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $candidate;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function assertWithinCaseRoot(string $caseId, string $absInStorage): void
|
||||||
|
{
|
||||||
|
$caseRoot = $this->getCaseRoot($caseId);
|
||||||
|
if ($absInStorage !== $caseRoot && !str_starts_with($absInStorage, $caseRoot . '/')) {
|
||||||
|
throw new Forbidden("Path '{$absInStorage}' is outside the case folder '{$caseRoot}'.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Canonicalize a storage-relative path:
|
||||||
|
* - convert '\' to '/'
|
||||||
|
* - collapse repeated '/'
|
||||||
|
* - resolve '.' segments
|
||||||
|
* - reject '..' segments (refuses traversal)
|
||||||
|
* - strip trailing '/'
|
||||||
|
*/
|
||||||
|
private function normalize(string $path): string
|
||||||
|
{
|
||||||
|
$path = str_replace('\\', '/', $path);
|
||||||
|
$segments = explode('/', $path);
|
||||||
|
$out = [];
|
||||||
|
foreach ($segments as $seg) {
|
||||||
|
if ($seg === '' || $seg === '.') {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if ($seg === '..') {
|
||||||
|
throw new Forbidden('Parent-directory traversal is not allowed.');
|
||||||
|
}
|
||||||
|
// Block null bytes and other suspicious characters.
|
||||||
|
if (preg_match('/[\x00-\x1F]/', $seg)) {
|
||||||
|
throw new BadRequest('Path contains invalid control characters.');
|
||||||
|
}
|
||||||
|
$out[] = $seg;
|
||||||
|
}
|
||||||
|
return implode('/', $out);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function dirnameRel(string $path): string
|
||||||
|
{
|
||||||
|
$pos = strrpos($path, '/');
|
||||||
|
if ($pos === false) {
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
return substr($path, 0, $pos);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Update the Document entity (if any) that pointed to this file, so the
|
||||||
|
* CRM reflects the soft-delete state.
|
||||||
|
*/
|
||||||
|
private function touchDocumentForDeletion(string $oldPath, string $newPath): void
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
$doc = $this->entityManager->getRDBRepository('Document')
|
||||||
|
->where(['networkStoragePath' => $oldPath])
|
||||||
|
->findOne();
|
||||||
|
if (!$doc) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
$doc->set([
|
||||||
|
'networkStoragePath' => $newPath,
|
||||||
|
'markedForDeletionAt' => date('Y-m-d H:i:s'),
|
||||||
|
'markedForDeletionById' => $this->user->getId(),
|
||||||
|
]);
|
||||||
|
$this->entityManager->saveEntity($doc);
|
||||||
|
} catch (\Throwable $e) {
|
||||||
|
$this->log->warning(
|
||||||
|
"CaseFolderManager: could not update Document entity for '{$oldPath}': " . $e->getMessage()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -11,13 +11,14 @@ use Espo\Modules\NetworkStorageIntegration\Classes\StorageClientInterface;
|
|||||||
|
|
||||||
class DocumentAnalyzer
|
class DocumentAnalyzer
|
||||||
{
|
{
|
||||||
private const MAX_TEXT_LENGTH = 2000;
|
private const DEFAULT_MAX_TEXT_LENGTH = 100000;
|
||||||
private const MAX_FILE_SIZE = 10 * 1024 * 1024;
|
private const MAX_FILE_SIZE = 10 * 1024 * 1024;
|
||||||
private const TMP_DIR = 'data/tmp/assistant-docs';
|
private const TMP_DIR = 'data/tmp/assistant-docs';
|
||||||
|
|
||||||
private EntityManager $entityManager;
|
private EntityManager $entityManager;
|
||||||
private InjectableFactory $injectableFactory;
|
private InjectableFactory $injectableFactory;
|
||||||
private Log $log;
|
private Log $log;
|
||||||
|
private ?int $maxTextLength = null;
|
||||||
|
|
||||||
private const CATEGORY_PATTERNS = [
|
private const CATEGORY_PATTERNS = [
|
||||||
'כתבי_טענות' => ['כתב_תביעה', 'כתב תביעה', 'כתב_הגנה', 'כתב הגנה', 'סיכומים', 'תביעה', 'הגנה', 'כתב_ערעור', 'כתב ערעור', 'בקשה', 'תגובה', 'תשובה'],
|
'כתבי_טענות' => ['כתב_תביעה', 'כתב תביעה', 'כתב_הגנה', 'כתב הגנה', 'סיכומים', 'תביעה', 'הגנה', 'כתב_ערעור', 'כתב ערעור', 'בקשה', 'תגובה', 'תשובה'],
|
||||||
@@ -40,11 +41,67 @@ class DocumentAnalyzer
|
|||||||
|
|
||||||
public function listDocumentsRecursive(string $caseId): array
|
public function listDocumentsRecursive(string $caseId): array
|
||||||
{
|
{
|
||||||
$casePath = $this->getCaseFolderPath($caseId);
|
$description = $this->describeCaseFolderPath($caseId);
|
||||||
if (!$casePath) return ['casePath' => null, 'totalFiles' => 0, 'folders' => [], 'rootFiles' => []];
|
$casePath = $description['path'];
|
||||||
|
|
||||||
|
if (!$casePath) {
|
||||||
|
return [
|
||||||
|
'casePath' => null,
|
||||||
|
'totalFiles' => 0,
|
||||||
|
'folders' => [],
|
||||||
|
'rootFiles' => [],
|
||||||
|
'resolvedPath' => null,
|
||||||
|
'pathSource' => $description['source'],
|
||||||
|
'folderExists' => false,
|
||||||
|
'reason' => $description['error'] ?? 'Case has no networkStorageFolderPath and no default could be computed.',
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
$client = $this->getStorageClient();
|
$client = $this->getStorageClient();
|
||||||
$topLevel = $client->listFolder($casePath);
|
|
||||||
|
try {
|
||||||
|
$folderExists = $client->exists($casePath);
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
return [
|
||||||
|
'casePath' => $casePath,
|
||||||
|
'totalFiles' => 0,
|
||||||
|
'folders' => [],
|
||||||
|
'rootFiles' => [],
|
||||||
|
'resolvedPath' => $casePath,
|
||||||
|
'pathSource' => $description['source'],
|
||||||
|
'folderExists' => false,
|
||||||
|
'reason' => "Storage client failed while checking '{$casePath}': " . $e->getMessage(),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!$folderExists) {
|
||||||
|
return [
|
||||||
|
'casePath' => $casePath,
|
||||||
|
'totalFiles' => 0,
|
||||||
|
'folders' => [],
|
||||||
|
'rootFiles' => [],
|
||||||
|
'resolvedPath' => $casePath,
|
||||||
|
'pathSource' => $description['source'],
|
||||||
|
'folderExists' => false,
|
||||||
|
'reason' => "Folder '{$casePath}' (source: {$description['source']}) does not exist on storage.",
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
$topLevel = $client->listFolder($casePath);
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
return [
|
||||||
|
'casePath' => $casePath,
|
||||||
|
'totalFiles' => 0,
|
||||||
|
'folders' => [],
|
||||||
|
'rootFiles' => [],
|
||||||
|
'resolvedPath' => $casePath,
|
||||||
|
'pathSource' => $description['source'],
|
||||||
|
'folderExists' => true,
|
||||||
|
'reason' => "Failed to list folder '{$casePath}': " . $e->getMessage(),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
$folders = []; $rootFiles = []; $totalFiles = 0;
|
$folders = []; $rootFiles = []; $totalFiles = 0;
|
||||||
|
|
||||||
foreach ($topLevel as $item) {
|
foreach ($topLevel as $item) {
|
||||||
@@ -69,10 +126,26 @@ class DocumentAnalyzer
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return ['casePath' => $casePath, 'totalFiles' => $totalFiles, 'folders' => $folders, 'rootFiles' => $rootFiles];
|
return [
|
||||||
|
'casePath' => $casePath,
|
||||||
|
'totalFiles' => $totalFiles,
|
||||||
|
'folders' => $folders,
|
||||||
|
'rootFiles' => $rootFiles,
|
||||||
|
'resolvedPath' => $casePath,
|
||||||
|
'pathSource' => $description['source'],
|
||||||
|
'folderExists' => true,
|
||||||
|
'reason' => $totalFiles === 0 ? "Folder '{$casePath}' exists but is empty." : null,
|
||||||
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
public function extractTextContent(string $filePath): ?string
|
public function extractTextContent(string $filePath): ?string
|
||||||
|
{
|
||||||
|
$maxLen = $this->getMaxTextLength();
|
||||||
|
$text = $this->extractFullText($filePath, $maxLen);
|
||||||
|
return $text;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function extractFullText(string $filePath, ?int $maxLength = null): ?string
|
||||||
{
|
{
|
||||||
$client = $this->getStorageClient();
|
$client = $this->getStorageClient();
|
||||||
$filename = basename($filePath);
|
$filename = basename($filePath);
|
||||||
@@ -98,8 +171,120 @@ class DocumentAnalyzer
|
|||||||
|
|
||||||
if (file_exists($tmpFile)) unlink($tmpFile);
|
if (file_exists($tmpFile)) unlink($tmpFile);
|
||||||
if ($text === null) return null;
|
if ($text === null) return null;
|
||||||
if (mb_strlen($text) > self::MAX_TEXT_LENGTH) $text = mb_substr($text, 0, self::MAX_TEXT_LENGTH) . '...';
|
|
||||||
return trim($text);
|
$text = trim($text);
|
||||||
|
if ($maxLength !== null && mb_strlen($text) > $maxLength) {
|
||||||
|
$text = mb_substr($text, 0, $maxLength) . '...';
|
||||||
|
}
|
||||||
|
|
||||||
|
return $text;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fetch raw file bytes + metadata for OCR/vision fallback.
|
||||||
|
*
|
||||||
|
* @return array{success: bool, fileName: string, mimeType: string, sizeBytes: int, base64: ?string, error: ?string}
|
||||||
|
*/
|
||||||
|
public function getDocumentBytes(string $filePath): array
|
||||||
|
{
|
||||||
|
$fileName = basename($filePath);
|
||||||
|
$client = $this->getStorageClient();
|
||||||
|
|
||||||
|
try {
|
||||||
|
$content = $client->downloadFile($filePath);
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
return [
|
||||||
|
'success' => false,
|
||||||
|
'fileName' => $fileName,
|
||||||
|
'mimeType' => '',
|
||||||
|
'sizeBytes' => 0,
|
||||||
|
'base64' => null,
|
||||||
|
'error' => 'Failed to download: ' . $e->getMessage(),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
$sizeBytes = strlen($content);
|
||||||
|
if ($sizeBytes > self::MAX_FILE_SIZE) {
|
||||||
|
return [
|
||||||
|
'success' => false,
|
||||||
|
'fileName' => $fileName,
|
||||||
|
'mimeType' => '',
|
||||||
|
'sizeBytes' => $sizeBytes,
|
||||||
|
'base64' => null,
|
||||||
|
'error' => 'File too large (max 10MB).',
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
$mimeType = $this->guessMimeType($fileName, $content);
|
||||||
|
|
||||||
|
return [
|
||||||
|
'success' => true,
|
||||||
|
'fileName' => $fileName,
|
||||||
|
'mimeType' => $mimeType,
|
||||||
|
'sizeBytes' => $sizeBytes,
|
||||||
|
'base64' => base64_encode($content),
|
||||||
|
'error' => null,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
private function guessMimeType(string $fileName, string $content): string
|
||||||
|
{
|
||||||
|
$ext = strtolower(pathinfo($fileName, PATHINFO_EXTENSION));
|
||||||
|
$map = [
|
||||||
|
'pdf' => 'application/pdf',
|
||||||
|
'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
|
||||||
|
'doc' => 'application/msword',
|
||||||
|
'txt' => 'text/plain',
|
||||||
|
'csv' => 'text/csv',
|
||||||
|
'log' => 'text/plain',
|
||||||
|
'png' => 'image/png',
|
||||||
|
'jpg' => 'image/jpeg',
|
||||||
|
'jpeg' => 'image/jpeg',
|
||||||
|
'gif' => 'image/gif',
|
||||||
|
'bmp' => 'image/bmp',
|
||||||
|
'tiff' => 'image/tiff',
|
||||||
|
'tif' => 'image/tiff',
|
||||||
|
'webp' => 'image/webp',
|
||||||
|
];
|
||||||
|
if (isset($map[$ext])) {
|
||||||
|
return $map[$ext];
|
||||||
|
}
|
||||||
|
// Fallback: finfo
|
||||||
|
if (function_exists('finfo_buffer')) {
|
||||||
|
$finfo = finfo_open(FILEINFO_MIME_TYPE);
|
||||||
|
$mime = finfo_buffer($finfo, $content);
|
||||||
|
finfo_close($finfo);
|
||||||
|
if ($mime) return $mime;
|
||||||
|
}
|
||||||
|
return 'application/octet-stream';
|
||||||
|
}
|
||||||
|
|
||||||
|
public function extractMultipleDocuments(array $filePaths, int $maxPerFile = 50000): array
|
||||||
|
{
|
||||||
|
$results = [];
|
||||||
|
|
||||||
|
foreach ($filePaths as $filePath) {
|
||||||
|
$fileName = basename($filePath);
|
||||||
|
try {
|
||||||
|
$text = $this->extractFullText($filePath, $maxPerFile);
|
||||||
|
$results[] = [
|
||||||
|
'path' => $filePath,
|
||||||
|
'fileName' => $fileName,
|
||||||
|
'text' => $text,
|
||||||
|
'charCount' => $text !== null ? mb_strlen($text) : 0,
|
||||||
|
];
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
$results[] = [
|
||||||
|
'path' => $filePath,
|
||||||
|
'fileName' => $fileName,
|
||||||
|
'text' => null,
|
||||||
|
'charCount' => 0,
|
||||||
|
'error' => $e->getMessage(),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $results;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function renameDocument(string $sourcePath, string $newName): array
|
public function renameDocument(string $sourcePath, string $newName): array
|
||||||
@@ -151,24 +336,181 @@ class DocumentAnalyzer
|
|||||||
}
|
}
|
||||||
|
|
||||||
public function getCaseFolderPath(string $caseId): ?string
|
public function getCaseFolderPath(string $caseId): ?string
|
||||||
|
{
|
||||||
|
return $this->describeCaseFolderPath($caseId)['path'];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Resolve the case folder path with diagnostic metadata.
|
||||||
|
*
|
||||||
|
* @return array{path: ?string, source: string, error: ?string, caseExists: bool}
|
||||||
|
* source: 'stored' | 'computed' | 'none'
|
||||||
|
*/
|
||||||
|
public function describeCaseFolderPath(string $caseId): array
|
||||||
{
|
{
|
||||||
$case = $this->entityManager->getEntityById('Case', $caseId);
|
$case = $this->entityManager->getEntityById('Case', $caseId);
|
||||||
if (!$case) return null;
|
if (!$case) {
|
||||||
|
return ['path' => null, 'source' => 'none', 'error' => "Case '{$caseId}' not found.", 'caseExists' => false];
|
||||||
|
}
|
||||||
|
|
||||||
// Try stored path first
|
|
||||||
$storedPath = $case->get('networkStorageFolderPath') ?: null;
|
$storedPath = $case->get('networkStorageFolderPath') ?: null;
|
||||||
if ($storedPath) return $storedPath;
|
if ($storedPath) {
|
||||||
|
return ['path' => $storedPath, 'source' => 'stored', 'error' => null, 'caseExists' => true];
|
||||||
|
}
|
||||||
|
|
||||||
// Fall back to computed path from NetworkDocumentService
|
|
||||||
try {
|
try {
|
||||||
$nds = $this->getNetworkDocumentService();
|
$nds = $this->getNetworkDocumentService();
|
||||||
return $nds->getEntityFolderPath('Case', $caseId);
|
$computed = $nds->getEntityFolderPath('Case', $caseId);
|
||||||
|
if ($computed) {
|
||||||
|
return ['path' => $computed, 'source' => 'computed', 'error' => null, 'caseExists' => true];
|
||||||
|
}
|
||||||
|
return ['path' => null, 'source' => 'none', 'error' => 'No stored path, and default path computation returned null.', 'caseExists' => true];
|
||||||
} catch (\Exception $e) {
|
} catch (\Exception $e) {
|
||||||
$this->log->warning("SmartAssistant: Failed to get case folder path: " . $e->getMessage());
|
$this->log->warning("SmartAssistant: Failed to compute case folder path: " . $e->getMessage());
|
||||||
return null;
|
return ['path' => null, 'source' => 'none', 'error' => $e->getMessage(), 'caseExists' => true];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* List the immediate contents of an arbitrary folder path on network storage.
|
||||||
|
* Manual override for when auto-resolution of a case folder fails.
|
||||||
|
*
|
||||||
|
* @return array<int, array{name: string, path: string, type: string, size: ?int, mimeType: ?string, modified: ?string}>
|
||||||
|
*/
|
||||||
|
public function browseFolder(string $path): array
|
||||||
|
{
|
||||||
|
$client = $this->getStorageClient();
|
||||||
|
$entries = [];
|
||||||
|
foreach ($client->listFolder($path) as $item) {
|
||||||
|
$isFolder = ($item['type'] ?? null) === 'folder' || !empty($item['isFolder']);
|
||||||
|
$entries[] = [
|
||||||
|
'name' => $item['name'] ?? '',
|
||||||
|
'path' => $item['path'] ?? '',
|
||||||
|
'type' => $isFolder ? 'folder' : 'file',
|
||||||
|
'size' => $item['size'] ?? null,
|
||||||
|
'mimeType' => $item['mimeType'] ?? null,
|
||||||
|
'modified' => $item['modified'] ?? null,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
return $entries;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Search the storage for folders matching a query (case-insensitive substring).
|
||||||
|
* Scans root and one level deep. Returns sorted matches.
|
||||||
|
*
|
||||||
|
* @return array<int, array{path: string, name: string, level: int, score: int}>
|
||||||
|
*/
|
||||||
|
public function findCaseFolder(string $query, int $limit = 20): array
|
||||||
|
{
|
||||||
|
$normalized = trim($query);
|
||||||
|
if ($normalized === '') return [];
|
||||||
|
|
||||||
|
$client = $this->getStorageClient();
|
||||||
|
$needle = mb_strtolower($normalized);
|
||||||
|
$matches = [];
|
||||||
|
|
||||||
|
try {
|
||||||
|
$roots = $client->listFolder('');
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
$this->log->warning("SmartAssistant: findCaseFolder root listing failed: " . $e->getMessage());
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach ($roots as $item) {
|
||||||
|
$isFolder = ($item['type'] ?? null) === 'folder' || !empty($item['isFolder']);
|
||||||
|
if (!$isFolder) continue;
|
||||||
|
|
||||||
|
$name = (string) ($item['name'] ?? '');
|
||||||
|
$path = (string) ($item['path'] ?? $name);
|
||||||
|
|
||||||
|
$score = $this->fuzzyScore($name, $needle);
|
||||||
|
if ($score > 0) {
|
||||||
|
$matches[] = ['path' => $path, 'name' => $name, 'level' => 0, 'score' => $score];
|
||||||
|
}
|
||||||
|
|
||||||
|
// One level deeper, but only inspect folder names — don't list files to keep it cheap.
|
||||||
|
try {
|
||||||
|
foreach ($client->listFolder($path) as $sub) {
|
||||||
|
$subIsFolder = ($sub['type'] ?? null) === 'folder' || !empty($sub['isFolder']);
|
||||||
|
if (!$subIsFolder) continue;
|
||||||
|
$subName = (string) ($sub['name'] ?? '');
|
||||||
|
$subPath = (string) ($sub['path'] ?? ($path . '/' . $subName));
|
||||||
|
$subScore = $this->fuzzyScore($subName, $needle);
|
||||||
|
if ($subScore > 0) {
|
||||||
|
$matches[] = ['path' => $subPath, 'name' => $subName, 'level' => 1, 'score' => $subScore];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
// Non-fatal: skip unreadable subfolders silently.
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
usort($matches, fn($a, $b) => $b['score'] <=> $a['score']);
|
||||||
|
return array_slice($matches, 0, $limit);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Persist the resolved folder path on the Case so future calls skip discovery.
|
||||||
|
*/
|
||||||
|
public function setCaseFolderPath(string $caseId, string $path): array
|
||||||
|
{
|
||||||
|
$case = $this->entityManager->getEntityById('Case', $caseId);
|
||||||
|
if (!$case) {
|
||||||
|
throw new Error("Case '{$caseId}' not found.");
|
||||||
|
}
|
||||||
|
|
||||||
|
$client = $this->getStorageClient();
|
||||||
|
if (!$client->exists($path)) {
|
||||||
|
throw new Error("Path '{$path}' does not exist on storage.");
|
||||||
|
}
|
||||||
|
|
||||||
|
$case->set('networkStorageFolderPath', $path);
|
||||||
|
$this->entityManager->saveEntity($case);
|
||||||
|
|
||||||
|
return ['success' => true, 'caseId' => $caseId, 'path' => $path];
|
||||||
|
}
|
||||||
|
|
||||||
|
private function fuzzyScore(string $haystack, string $needleLower): int
|
||||||
|
{
|
||||||
|
$hay = mb_strtolower($haystack);
|
||||||
|
if ($hay === $needleLower) return 100;
|
||||||
|
if (mb_strpos($hay, $needleLower) !== false) return 80;
|
||||||
|
|
||||||
|
// Token overlap (whitespace/dash/underscore).
|
||||||
|
$hayTokens = preg_split('/[\s\-_]+/u', $hay) ?: [];
|
||||||
|
$needleTokens = preg_split('/[\s\-_]+/u', $needleLower) ?: [];
|
||||||
|
$hits = 0;
|
||||||
|
foreach ($needleTokens as $nt) {
|
||||||
|
if ($nt === '') continue;
|
||||||
|
foreach ($hayTokens as $ht) {
|
||||||
|
if ($ht !== '' && mb_strpos($ht, $nt) !== false) { $hits++; break; }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return $hits > 0 ? 40 + $hits * 5 : 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function getMaxTextLength(): int
|
||||||
|
{
|
||||||
|
if ($this->maxTextLength !== null) {
|
||||||
|
return $this->maxTextLength;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
$integration = $this->entityManager->getEntityById('Integration', 'SmartAssistant');
|
||||||
|
if ($integration && $integration->get('enabled')) {
|
||||||
|
$data = (array) ($integration->get('data') ?? []);
|
||||||
|
$this->maxTextLength = (int) ($data['maxDocumentChars'] ?? self::DEFAULT_MAX_TEXT_LENGTH);
|
||||||
|
return $this->maxTextLength;
|
||||||
|
}
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
// Ignore
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->maxTextLength = self::DEFAULT_MAX_TEXT_LENGTH;
|
||||||
|
return $this->maxTextLength;
|
||||||
|
}
|
||||||
|
|
||||||
private function getStorageClient(): StorageClientInterface
|
private function getStorageClient(): StorageClientInterface
|
||||||
{
|
{
|
||||||
return $this->getNetworkDocumentService()->getClient();
|
return $this->getNetworkDocumentService()->getClient();
|
||||||
@@ -191,20 +533,37 @@ class DocumentAnalyzer
|
|||||||
|
|
||||||
private function extractFromDocx(string $tmpFile): ?string
|
private function extractFromDocx(string $tmpFile): ?string
|
||||||
{
|
{
|
||||||
if (!class_exists(\PhpOffice\PhpWord\IOFactory::class)) return null;
|
// PHPWord (vendor/phpoffice/phpword) ships with the EspoCRM image but
|
||||||
$phpWord = \PhpOffice\PhpWord\IOFactory::load($tmpFile);
|
// is NOT in the composer PSR-4 autoload map — class_exists silently
|
||||||
$text = '';
|
// returns false. Rather than fix the autoloader (rebuild of the image)
|
||||||
foreach ($phpWord->getSections() as $section) {
|
// we parse word/document.xml directly. This is also more robust:
|
||||||
foreach ($section->getElements() as $element) {
|
// PHPWord's element walker only goes one level deep, missing text in
|
||||||
if (method_exists($element, 'getText')) $text .= $element->getText() . "\n";
|
// tables, hyperlinks, and footnotes.
|
||||||
elseif (method_exists($element, 'getElements')) {
|
$zip = new \ZipArchive();
|
||||||
foreach ($element->getElements() as $child) {
|
if ($zip->open($tmpFile) !== true) {
|
||||||
if (method_exists($child, 'getText')) $text .= $child->getText() . "\n";
|
$this->log->warning("SmartAssistant: extractFromDocx: not a valid zip: $tmpFile");
|
||||||
}
|
return null;
|
||||||
|
}
|
||||||
|
$xml = $zip->getFromName('word/document.xml');
|
||||||
|
$zip->close();
|
||||||
|
if ($xml === false || $xml === '') {
|
||||||
|
$this->log->warning("SmartAssistant: extractFromDocx: word/document.xml missing in $tmpFile");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Each <w:p> is a paragraph; concatenate <w:t> runs inside it.
|
||||||
|
// `(?:\s[^>]*)?` keeps us from also matching <w:tab> / <w:tbl>.
|
||||||
|
$paragraphs = [];
|
||||||
|
if (preg_match_all('#<w:p(?:\s[^>]*)?>(.*?)</w:p>#s', $xml, $pMatches)) {
|
||||||
|
foreach ($pMatches[1] as $block) {
|
||||||
|
if (preg_match_all('#<w:t(?:\s[^>]*)?>(.*?)</w:t>#s', $block, $tMatches)) {
|
||||||
|
$line = trim(implode('', $tMatches[1]));
|
||||||
|
if ($line !== '') $paragraphs[] = html_entity_decode($line, ENT_XML1 | ENT_QUOTES, 'UTF-8');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return $text ?: null;
|
$text = trim(implode("\n", $paragraphs));
|
||||||
|
return $text !== '' ? $text : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
private function updateDocumentEntity(string $oldPath, string $newPath): void
|
private function updateDocumentEntity(string $oldPath, string $newPath): void
|
||||||
|
|||||||
@@ -0,0 +1,275 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Espo\Modules\SmartAssistant\Services;
|
||||||
|
|
||||||
|
use Espo\Core\Exceptions\BadRequest;
|
||||||
|
use Espo\Core\Exceptions\NotFound;
|
||||||
|
use Espo\Core\InjectableFactory;
|
||||||
|
use Espo\Core\Utils\Log;
|
||||||
|
use Espo\ORM\EntityManager;
|
||||||
|
use Espo\Entities\User;
|
||||||
|
use Espo\Modules\NetworkStorageIntegration\Services\NetworkDocumentService;
|
||||||
|
use PhpOffice\PhpWord\PhpWord;
|
||||||
|
use PhpOffice\PhpWord\IOFactory;
|
||||||
|
|
||||||
|
class FreeDocumentGenerator
|
||||||
|
{
|
||||||
|
private const TEMP_DIR = 'data/tmp/';
|
||||||
|
private const FONT_NAME = 'David';
|
||||||
|
private const FONT_SIZE = 12;
|
||||||
|
|
||||||
|
public function __construct(
|
||||||
|
private EntityManager $entityManager,
|
||||||
|
private InjectableFactory $injectableFactory,
|
||||||
|
private Log $log,
|
||||||
|
private User $user
|
||||||
|
) {}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array{success: bool, documentId: string, documentName: string, fileName: string, networkPath: ?string, message: string}
|
||||||
|
*/
|
||||||
|
public function generate(
|
||||||
|
string $caseId,
|
||||||
|
string $title,
|
||||||
|
string $body,
|
||||||
|
string $documentType = 'letter',
|
||||||
|
?string $recipient = null
|
||||||
|
): array {
|
||||||
|
$title = trim($title);
|
||||||
|
if ($title === '') {
|
||||||
|
throw new BadRequest('title is required.');
|
||||||
|
}
|
||||||
|
if (trim($body) === '') {
|
||||||
|
throw new BadRequest('body is required.');
|
||||||
|
}
|
||||||
|
|
||||||
|
$case = $this->entityManager->getEntityById('Case', $caseId);
|
||||||
|
if (!$case) {
|
||||||
|
throw new NotFound("Case {$caseId} not found.");
|
||||||
|
}
|
||||||
|
|
||||||
|
$docxBinary = $this->buildDocx($title, $body, $recipient);
|
||||||
|
|
||||||
|
$sanitizedTitle = $this->sanitizeFileName($title);
|
||||||
|
$today = date('Y-m-d');
|
||||||
|
$fileName = "{$today} - {$sanitizedTitle}.docx";
|
||||||
|
|
||||||
|
$attachment = $this->entityManager->getNewEntity('Attachment');
|
||||||
|
$attachment->set([
|
||||||
|
'name' => $fileName,
|
||||||
|
'type' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
|
||||||
|
'role' => 'Attachment',
|
||||||
|
'size' => strlen($docxBinary),
|
||||||
|
'relatedType' => 'Document',
|
||||||
|
]);
|
||||||
|
$this->entityManager->saveEntity($attachment);
|
||||||
|
file_put_contents('data/upload/' . $attachment->getId(), $docxBinary);
|
||||||
|
|
||||||
|
$document = $this->entityManager->getNewEntity('Document');
|
||||||
|
$document->set([
|
||||||
|
'name' => $title,
|
||||||
|
'fileId' => $attachment->getId(),
|
||||||
|
'fileName' => $fileName,
|
||||||
|
'assignedUserId' => $this->user->getId(),
|
||||||
|
]);
|
||||||
|
$this->entityManager->saveEntity($document);
|
||||||
|
|
||||||
|
$this->entityManager->getRDBRepository('Case')
|
||||||
|
->getRelation($case, 'documents')
|
||||||
|
->relate($document);
|
||||||
|
|
||||||
|
$networkPath = null;
|
||||||
|
try {
|
||||||
|
$nds = $this->injectableFactory->create(NetworkDocumentService::class);
|
||||||
|
if ($nds->isEnabled()) {
|
||||||
|
$result = $nds->uploadDocument($document, $attachment, 'Case', $caseId);
|
||||||
|
$networkPath = $result['path'] ?? null;
|
||||||
|
}
|
||||||
|
} catch (\Throwable $e) {
|
||||||
|
// Document is safely persisted in Espo; the network-storage copy is best-effort.
|
||||||
|
$this->log->warning(
|
||||||
|
"FreeDocumentGenerator: failed to copy to network storage for Case {$caseId}: " .
|
||||||
|
$e->getMessage()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->log->info(
|
||||||
|
"FreeDocumentGenerator: Created '{$title}' (type={$documentType}) for Case {$caseId}"
|
||||||
|
);
|
||||||
|
|
||||||
|
$message = "✅ המסמך \"{$title}\" נוצר בהצלחה וצורף לתיק";
|
||||||
|
if ($networkPath) {
|
||||||
|
$message .= " ולתיקיית הרשת";
|
||||||
|
}
|
||||||
|
$message .= ".";
|
||||||
|
|
||||||
|
return [
|
||||||
|
'success' => true,
|
||||||
|
'documentId' => $document->getId(),
|
||||||
|
'documentName' => $title,
|
||||||
|
'fileName' => $fileName,
|
||||||
|
'networkPath' => $networkPath,
|
||||||
|
'message' => $message,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
private function buildDocx(string $title, string $body, ?string $recipient): string
|
||||||
|
{
|
||||||
|
$phpWord = new PhpWord();
|
||||||
|
$phpWord->setDefaultFontName(self::FONT_NAME);
|
||||||
|
$phpWord->setDefaultFontSize(self::FONT_SIZE);
|
||||||
|
|
||||||
|
$section = $phpWord->addSection([
|
||||||
|
'marginLeft' => 1440,
|
||||||
|
'marginRight' => 1440,
|
||||||
|
'marginTop' => 1440,
|
||||||
|
'marginBottom' => 1440,
|
||||||
|
]);
|
||||||
|
|
||||||
|
$rtlPara = ['bidi' => true, 'alignment' => 'right'];
|
||||||
|
$centerPara = ['bidi' => true, 'alignment' => 'center'];
|
||||||
|
$titleFont = ['name' => self::FONT_NAME, 'size' => 16, 'bold' => true, 'rtl' => true];
|
||||||
|
$boldFont = ['name' => self::FONT_NAME, 'size' => self::FONT_SIZE, 'bold' => true, 'rtl' => true];
|
||||||
|
$normalFont = ['name' => self::FONT_NAME, 'size' => self::FONT_SIZE, 'rtl' => true];
|
||||||
|
|
||||||
|
$section->addText($this->formatHebrewDate(new \DateTime()), $normalFont, $rtlPara);
|
||||||
|
$section->addTextBreak(1);
|
||||||
|
|
||||||
|
if ($recipient !== null && trim($recipient) !== '') {
|
||||||
|
$section->addText('אל: ' . trim($recipient), $boldFont, $rtlPara);
|
||||||
|
$section->addTextBreak(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
$section->addText($title, $titleFont, $centerPara);
|
||||||
|
$section->addTextBreak(1);
|
||||||
|
|
||||||
|
$this->renderMarkdown($section, $body);
|
||||||
|
|
||||||
|
if (!is_dir(self::TEMP_DIR)) {
|
||||||
|
mkdir(self::TEMP_DIR, 0775, true);
|
||||||
|
}
|
||||||
|
$tmpPath = self::TEMP_DIR . 'free_' . uniqid() . '.docx';
|
||||||
|
$writer = IOFactory::createWriter($phpWord, 'Word2007');
|
||||||
|
$writer->save($tmpPath);
|
||||||
|
$content = file_get_contents($tmpPath);
|
||||||
|
@unlink($tmpPath);
|
||||||
|
|
||||||
|
return $content;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function renderMarkdown($section, string $body): void
|
||||||
|
{
|
||||||
|
$rtlPara = ['bidi' => true, 'alignment' => 'right'];
|
||||||
|
$bulletStyle = ['listType' => \PhpOffice\PhpWord\Style\ListItem::TYPE_BULLET_FILLED];
|
||||||
|
$bulletFont = ['name' => self::FONT_NAME, 'size' => self::FONT_SIZE, 'rtl' => true];
|
||||||
|
$h1Size = 16; $h2Size = 14; $h3Size = 13;
|
||||||
|
|
||||||
|
$body = str_replace(["\r\n", "\r"], "\n", $body);
|
||||||
|
$lines = explode("\n", $body);
|
||||||
|
|
||||||
|
$i = 0;
|
||||||
|
$n = count($lines);
|
||||||
|
while ($i < $n) {
|
||||||
|
$line = $lines[$i];
|
||||||
|
$trimmed = ltrim($line);
|
||||||
|
|
||||||
|
if (trim($line) === '') {
|
||||||
|
$section->addTextBreak(1);
|
||||||
|
$i++;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (preg_match('/^(#{1,3})\s+(.+)$/u', $trimmed, $m)) {
|
||||||
|
$level = strlen($m[1]);
|
||||||
|
$size = match ($level) { 1 => $h1Size, 2 => $h2Size, default => $h3Size };
|
||||||
|
$section->addText(
|
||||||
|
$m[2],
|
||||||
|
['name' => self::FONT_NAME, 'size' => $size, 'bold' => true, 'rtl' => true],
|
||||||
|
$rtlPara
|
||||||
|
);
|
||||||
|
$i++;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (preg_match('/^[\-\*]\s+(.+)$/u', $trimmed)) {
|
||||||
|
while ($i < $n && preg_match('/^[\-\*]\s+(.+)$/u', ltrim($lines[$i]), $m2)) {
|
||||||
|
$plain = preg_replace('/(\*\*|\*)/u', '', $m2[1]);
|
||||||
|
$section->addListItem($plain, 0, $bulletFont, $bulletStyle, $rtlPara);
|
||||||
|
$i++;
|
||||||
|
}
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
$textRun = $section->addTextRun($rtlPara);
|
||||||
|
$this->addInlineRuns($textRun, $trimmed);
|
||||||
|
$i++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Parse **bold** and *italic* into multiple PhpWord runs.
|
||||||
|
*/
|
||||||
|
private function addInlineRuns($textRun, string $text): void
|
||||||
|
{
|
||||||
|
$base = ['name' => self::FONT_NAME, 'size' => self::FONT_SIZE, 'rtl' => true];
|
||||||
|
$i = 0;
|
||||||
|
$len = mb_strlen($text);
|
||||||
|
$buffer = '';
|
||||||
|
|
||||||
|
$flush = function () use (&$buffer, $textRun, $base) {
|
||||||
|
if ($buffer !== '') {
|
||||||
|
$textRun->addText($buffer, $base);
|
||||||
|
$buffer = '';
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
while ($i < $len) {
|
||||||
|
$two = mb_substr($text, $i, 2);
|
||||||
|
$one = mb_substr($text, $i, 1);
|
||||||
|
|
||||||
|
if ($two === '**') {
|
||||||
|
$end = mb_strpos($text, '**', $i + 2);
|
||||||
|
if ($end !== false) {
|
||||||
|
$flush();
|
||||||
|
$inner = mb_substr($text, $i + 2, $end - $i - 2);
|
||||||
|
$textRun->addText($inner, array_merge($base, ['bold' => true]));
|
||||||
|
$i = $end + 2;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
} elseif ($one === '*') {
|
||||||
|
$end = mb_strpos($text, '*', $i + 1);
|
||||||
|
if ($end !== false) {
|
||||||
|
$flush();
|
||||||
|
$inner = mb_substr($text, $i + 1, $end - $i - 1);
|
||||||
|
$textRun->addText($inner, array_merge($base, ['italic' => true]));
|
||||||
|
$i = $end + 1;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$buffer .= $one;
|
||||||
|
$i++;
|
||||||
|
}
|
||||||
|
$flush();
|
||||||
|
}
|
||||||
|
|
||||||
|
private function formatHebrewDate(\DateTime $dt): string
|
||||||
|
{
|
||||||
|
$months = [
|
||||||
|
1 => 'ינואר', 2 => 'פברואר', 3 => 'מרץ', 4 => 'אפריל',
|
||||||
|
5 => 'מאי', 6 => 'יוני', 7 => 'יולי', 8 => 'אוגוסט',
|
||||||
|
9 => 'ספטמבר', 10 => 'אוקטובר', 11 => 'נובמבר', 12 => 'דצמבר',
|
||||||
|
];
|
||||||
|
$day = (int) $dt->format('j');
|
||||||
|
$month = $months[(int) $dt->format('n')];
|
||||||
|
$year = $dt->format('Y');
|
||||||
|
return "{$day} ב{$month} {$year}";
|
||||||
|
}
|
||||||
|
|
||||||
|
private function sanitizeFileName(string $name): string
|
||||||
|
{
|
||||||
|
$name = preg_replace('/[\\/:*?"<>|]/', '', $name);
|
||||||
|
$name = preg_replace('/\s+/', ' ', $name);
|
||||||
|
return trim(mb_substr($name, 0, 100));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,213 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Espo\Modules\SmartAssistant\Services;
|
||||||
|
|
||||||
|
use Espo\Core\Exceptions\Error;
|
||||||
|
use Espo\Core\Exceptions\NotFound;
|
||||||
|
use Espo\Core\InjectableFactory;
|
||||||
|
use Espo\Core\Utils\Log;
|
||||||
|
use Espo\ORM\EntityManager;
|
||||||
|
use Espo\Entities\User;
|
||||||
|
use Espo\Modules\NetworkStorageIntegration\Services\DocumentTemplateService;
|
||||||
|
use Espo\Modules\NetworkStorageIntegration\Services\NetworkDocumentService;
|
||||||
|
use PhpOffice\PhpWord\TemplateProcessor;
|
||||||
|
|
||||||
|
class GenericTemplateGenerator
|
||||||
|
{
|
||||||
|
private const TEMP_DIR = 'data/tmp/';
|
||||||
|
|
||||||
|
public function __construct(
|
||||||
|
private EntityManager $entityManager,
|
||||||
|
private InjectableFactory $injectableFactory,
|
||||||
|
private Log $log,
|
||||||
|
private User $user
|
||||||
|
) {}
|
||||||
|
|
||||||
|
public function generate(string $templateId, string $caseId, ?string $documentSubject = null, array $customPlaceholders = []): array
|
||||||
|
{
|
||||||
|
$template = $this->entityManager->getEntityById('DocumentTemplate', $templateId);
|
||||||
|
if (!$template) {
|
||||||
|
throw new NotFound("Template {$templateId} not found.");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!$template->get('isActive')) {
|
||||||
|
throw new Error("Template is not active.");
|
||||||
|
}
|
||||||
|
|
||||||
|
$templatePath = $template->get('templatePath');
|
||||||
|
if (!$templatePath) {
|
||||||
|
throw new Error("Template path is not configured.");
|
||||||
|
}
|
||||||
|
|
||||||
|
$case = $this->entityManager->getEntityById('Case', $caseId);
|
||||||
|
if (!$case) {
|
||||||
|
throw new NotFound("Case {$caseId} not found.");
|
||||||
|
}
|
||||||
|
|
||||||
|
$templateName = $template->get('name') ?? 'document';
|
||||||
|
|
||||||
|
// Step 1: Download template from WebDAV
|
||||||
|
$localTemplatePath = $this->downloadTemplate($templatePath);
|
||||||
|
|
||||||
|
try {
|
||||||
|
// Step 2: Build placeholder data from entity fields
|
||||||
|
$templateService = $this->injectableFactory->create(DocumentTemplateService::class);
|
||||||
|
$placeholderData = $templateService->buildPlaceholderData($case);
|
||||||
|
|
||||||
|
// Step 3: Merge custom placeholders (override entity data)
|
||||||
|
foreach ($customPlaceholders as $key => $value) {
|
||||||
|
$placeholderData[$key] = $value;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Step 4: Process template
|
||||||
|
$outputPath = self::TEMP_DIR . uniqid('gen_') . '.docx';
|
||||||
|
if (!is_dir(self::TEMP_DIR)) {
|
||||||
|
mkdir(self::TEMP_DIR, 0775, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->processTemplate($localTemplatePath, $outputPath, $placeholderData);
|
||||||
|
|
||||||
|
// Step 5: Read generated content
|
||||||
|
$content = file_get_contents($outputPath);
|
||||||
|
@unlink($outputPath);
|
||||||
|
@unlink($localTemplatePath);
|
||||||
|
|
||||||
|
// Step 6: Build document name
|
||||||
|
$contactName = $this->getContactName($case);
|
||||||
|
$docName = $documentSubject ?? "{$templateName} — {$contactName}";
|
||||||
|
|
||||||
|
// Step 7: Create Attachment + Document
|
||||||
|
$attachment = $this->entityManager->getNewEntity('Attachment');
|
||||||
|
$attachment->set([
|
||||||
|
'name' => $docName . '.docx',
|
||||||
|
'type' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
|
||||||
|
'role' => 'Attachment',
|
||||||
|
'size' => strlen($content),
|
||||||
|
'relatedType' => 'Document',
|
||||||
|
]);
|
||||||
|
$this->entityManager->saveEntity($attachment);
|
||||||
|
file_put_contents('data/upload/' . $attachment->getId(), $content);
|
||||||
|
|
||||||
|
$document = $this->entityManager->getNewEntity('Document');
|
||||||
|
$document->set([
|
||||||
|
'name' => $docName,
|
||||||
|
'fileId' => $attachment->getId(),
|
||||||
|
'fileName' => $docName . '.docx',
|
||||||
|
'assignedUserId' => $this->user->getId(),
|
||||||
|
]);
|
||||||
|
$this->entityManager->saveEntity($document);
|
||||||
|
|
||||||
|
// Link to Case
|
||||||
|
$this->entityManager->getRDBRepository('Case')
|
||||||
|
->getRelation($case, 'documents')
|
||||||
|
->relate($document);
|
||||||
|
|
||||||
|
$this->log->info("GenericTemplateGenerator: Created '{$docName}' from template '{$templateName}' for Case {$caseId}");
|
||||||
|
|
||||||
|
return [
|
||||||
|
'success' => true,
|
||||||
|
'message' => "✅ מסמך \"{$docName}\" נוצר בהצלחה מתבנית \"{$templateName}\"\n📄 המסמך צורף לתיק.",
|
||||||
|
'entityType' => 'Document',
|
||||||
|
'entityId' => $document->getId(),
|
||||||
|
];
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
if (isset($localTemplatePath) && file_exists($localTemplatePath)) {
|
||||||
|
@unlink($localTemplatePath);
|
||||||
|
}
|
||||||
|
throw $e;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private function downloadTemplate(string $templatePath): string
|
||||||
|
{
|
||||||
|
// Try local paths first
|
||||||
|
if (file_exists($templatePath)) {
|
||||||
|
return $templatePath;
|
||||||
|
}
|
||||||
|
|
||||||
|
$localPath = 'data/document-templates/' . ltrim($templatePath, '/');
|
||||||
|
if (file_exists($localPath)) {
|
||||||
|
return $localPath;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Download from NetworkStorage (WebDAV)
|
||||||
|
try {
|
||||||
|
$nds = $this->injectableFactory->create(NetworkDocumentService::class);
|
||||||
|
$client = $nds->getClient();
|
||||||
|
|
||||||
|
$integration = $this->entityManager->getEntityById('Integration', 'NetworkStorage');
|
||||||
|
$templatesFolderPath = 'Templates';
|
||||||
|
if ($integration && $integration->get('enabled')) {
|
||||||
|
$data = (array) ($integration->get('data') ?? []);
|
||||||
|
$templatesFolderPath = $data['templatesFolderPath'] ?? 'Templates';
|
||||||
|
}
|
||||||
|
|
||||||
|
$storagePath = $templatesFolderPath . '/' . $templatePath;
|
||||||
|
|
||||||
|
if (!$client->exists($storagePath)) {
|
||||||
|
throw new Error("Template file not found on storage: {$storagePath}");
|
||||||
|
}
|
||||||
|
|
||||||
|
$content = $client->downloadFile($storagePath);
|
||||||
|
|
||||||
|
if (!is_dir(self::TEMP_DIR)) {
|
||||||
|
mkdir(self::TEMP_DIR, 0775, true);
|
||||||
|
}
|
||||||
|
$tmpPath = self::TEMP_DIR . 'tpl_' . uniqid() . '.docx';
|
||||||
|
file_put_contents($tmpPath, $content);
|
||||||
|
|
||||||
|
return $tmpPath;
|
||||||
|
} catch (Error $e) {
|
||||||
|
throw $e;
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
throw new Error("Failed to download template: " . $e->getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private function processTemplate(string $inputPath, string $outputPath, array $data): void
|
||||||
|
{
|
||||||
|
if (!class_exists(TemplateProcessor::class, false)) {
|
||||||
|
$autoloader = 'vendor/phpoffice/phpword/src/PhpWord/Autoloader.php';
|
||||||
|
if (file_exists($autoloader)) {
|
||||||
|
require_once $autoloader;
|
||||||
|
\PhpOffice\PhpWord\Autoloader::register();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$processor = new TemplateProcessor($inputPath);
|
||||||
|
|
||||||
|
foreach ($data as $key => $value) {
|
||||||
|
if ($value === null) {
|
||||||
|
$value = '';
|
||||||
|
}
|
||||||
|
if (is_array($value)) {
|
||||||
|
$value = implode(', ', $value);
|
||||||
|
} elseif (is_bool($value)) {
|
||||||
|
$value = $value ? 'כן' : 'לא';
|
||||||
|
}
|
||||||
|
$processor->setValue($key, (string) $value);
|
||||||
|
}
|
||||||
|
|
||||||
|
$processor->saveAs($outputPath);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function getContactName($case): string
|
||||||
|
{
|
||||||
|
$contactId = $case->get('contactId');
|
||||||
|
if (!$contactId) {
|
||||||
|
$ids = $case->getLinkMultipleIdList('contacts');
|
||||||
|
if (!empty($ids)) {
|
||||||
|
$contactId = $ids[0];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($contactId) {
|
||||||
|
$contact = $this->entityManager->getEntityById('Contact', $contactId);
|
||||||
|
if ($contact) {
|
||||||
|
return trim($contact->get('firstName') . ' ' . $contact->get('lastName'));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $case->get('name') ?? '';
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -58,10 +58,18 @@ class OfficeContextBuilder
|
|||||||
$contacts[] = ['name' => $c->get('name'), 'phone' => $c->get('phoneNumber'), 'email' => $c->get('emailAddress')];
|
$contacts[] = ['name' => $c->get('name'), 'phone' => $c->get('phoneNumber'), 'email' => $c->get('emailAddress')];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$parentConditions = [
|
||||||
|
['parentType' => 'Case', 'parentId' => $caseId],
|
||||||
|
];
|
||||||
|
$contactIds = $this->getCaseContactIds($case);
|
||||||
|
if (!empty($contactIds)) {
|
||||||
|
$parentConditions[] = ['parentType' => 'Contact', 'parentId' => $contactIds];
|
||||||
|
}
|
||||||
|
|
||||||
$tasks = [];
|
$tasks = [];
|
||||||
foreach ($this->entityManager->getRDBRepository('Task')
|
foreach ($this->entityManager->getRDBRepository('Task')
|
||||||
->select(['id', 'name', 'status', 'dateEnd', 'assignedUserName'])
|
->select(['id', 'name', 'status', 'dateEnd', 'assignedUserName'])
|
||||||
->where(['parentId' => $caseId, 'parentType' => 'Case', 'status!=' => ['Completed', 'Canceled'], 'deleted' => false])
|
->where(['OR' => $parentConditions, 'status!=' => ['Completed', 'Canceled'], 'deleted' => false])
|
||||||
->order('dateEnd', 'ASC')->limit(0, 20)->find() as $t) {
|
->order('dateEnd', 'ASC')->limit(0, 20)->find() as $t) {
|
||||||
$tasks[] = ['name' => $t->get('name'), 'status' => $t->get('status'), 'dateEnd' => $t->get('dateEnd'), 'assignedUser' => $t->get('assignedUserName')];
|
$tasks[] = ['name' => $t->get('name'), 'status' => $t->get('status'), 'dateEnd' => $t->get('dateEnd'), 'assignedUser' => $t->get('assignedUserName')];
|
||||||
}
|
}
|
||||||
@@ -76,4 +84,27 @@ class OfficeContextBuilder
|
|||||||
|
|
||||||
return ['case' => $caseData, 'contacts' => $contacts, 'openTasks' => $tasks, 'recentNotes' => $notes];
|
return ['case' => $caseData, 'contacts' => $contacts, 'openTasks' => $tasks, 'recentNotes' => $notes];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function getCaseContactIds(\Espo\ORM\Entity $case): array
|
||||||
|
{
|
||||||
|
$contactIds = [];
|
||||||
|
$primaryContactId = $case->get('contactId');
|
||||||
|
if ($primaryContactId) {
|
||||||
|
$contactIds[] = $primaryContactId;
|
||||||
|
}
|
||||||
|
|
||||||
|
$contacts = $this->entityManager->getRDBRepository('Case')
|
||||||
|
->getRelation($case, 'contacts')
|
||||||
|
->select(['id'])
|
||||||
|
->find();
|
||||||
|
|
||||||
|
foreach ($contacts as $contact) {
|
||||||
|
$id = $contact->get('id');
|
||||||
|
if (!in_array($id, $contactIds)) {
|
||||||
|
$contactIds[] = $id;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $contactIds;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,7 +18,17 @@ class SmartAssistantService
|
|||||||
public const NOTE_TYPE_ACTION = 'SmartAction';
|
public const NOTE_TYPE_ACTION = 'SmartAction';
|
||||||
|
|
||||||
// All tools execute inline — Shira has full permissions
|
// All tools execute inline — Shira has full permissions
|
||||||
private const TOOLS_REQUIRING_CASE = ['change_status', 'schedule_hearing', 'list_documents', 'analyze_document', 'upload_document', 'generate_document', 'merge_documents', 'save_memory'];
|
private const TOOLS_REQUIRING_CASE = [
|
||||||
|
'change_status', 'schedule_hearing', 'list_documents', 'analyze_document',
|
||||||
|
'read_document', 'read_multiple_documents', 'upload_document',
|
||||||
|
'generate_document', 'merge_documents', 'save_memory',
|
||||||
|
'batch_rename_documents',
|
||||||
|
];
|
||||||
|
|
||||||
|
// Tools whose output is fed back to the AI for a follow-up webhook call
|
||||||
|
private const AGENTIC_TOOLS = ['analyze_document', 'read_document', 'read_multiple_documents', 'list_documents'];
|
||||||
|
|
||||||
|
private const DEFAULT_MAX_AGENTIC_LOOPS = 3;
|
||||||
|
|
||||||
private EntityManager $entityManager;
|
private EntityManager $entityManager;
|
||||||
private InjectableFactory $injectableFactory;
|
private InjectableFactory $injectableFactory;
|
||||||
@@ -105,68 +115,28 @@ class SmartAssistantService
|
|||||||
// Ignore
|
// Ignore
|
||||||
}
|
}
|
||||||
|
|
||||||
// Call webhook
|
|
||||||
$response = $this->callWebhook($message, $context, $conversationId, $conversationHistory, $mode);
|
|
||||||
|
|
||||||
// Process actions — execute all inline (Shira has full permissions)
|
|
||||||
$actions = $response['actions'] ?? [];
|
|
||||||
$responseText = $response['text'] ?? '';
|
|
||||||
$inlineResults = [];
|
|
||||||
$executedActions = [];
|
|
||||||
|
|
||||||
// Resolve effective caseId: explicit case mode, or drill-down detection in office mode
|
// Resolve effective caseId: explicit case mode, or drill-down detection in office mode
|
||||||
$effectiveCaseId = $caseId;
|
$effectiveCaseId = $caseId;
|
||||||
if (!$effectiveCaseId && !empty($context['drillDown']['case']['id'])) {
|
if (!$effectiveCaseId && !empty($context['drillDown']['case']['id'])) {
|
||||||
$effectiveCaseId = $context['drillDown']['case']['id'];
|
$effectiveCaseId = $context['drillDown']['case']['id'];
|
||||||
}
|
}
|
||||||
|
|
||||||
$executor = $this->injectableFactory->create(ActionExecutor::class);
|
// Execute tools with agentic loop — read tools feed results back to AI
|
||||||
|
try {
|
||||||
foreach ($actions as $action) {
|
$loopResult = $this->executeActionsWithLoop(
|
||||||
$tool = $action['tool'] ?? '';
|
$message, $context, $conversationId, $conversationHistory,
|
||||||
$actionCaseId = $effectiveCaseId;
|
$mode, $effectiveCaseId, $userId
|
||||||
|
);
|
||||||
// Resolve case from action params if not available
|
} catch (\Exception $e) {
|
||||||
if (!$actionCaseId && !empty($action['params']['caseId'])) {
|
$this->log->error("SmartAssistant: Chat failed: " . $e->getMessage());
|
||||||
$actionCaseId = $action['params']['caseId'];
|
$loopResult = [
|
||||||
}
|
'responseText' => "⚠️ אירעה שגיאה בתקשורת עם המערכת. אנא נסה שוב בעוד מספר דקות.\n\nפרטי השגיאה: " . $e->getMessage(),
|
||||||
|
'executedActions' => [],
|
||||||
// Skip tools that require a case if no case context available
|
];
|
||||||
if (!$actionCaseId && in_array($tool, self::TOOLS_REQUIRING_CASE, true)) {
|
|
||||||
$inlineResults[] = "⚠️ לא ניתן לבצע {$tool} — לא זוהה תיק. נסה מתוך מסך התיק.";
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
$result = $executor->execute($tool, $action['params'] ?? [], $actionCaseId, $userId);
|
|
||||||
if (!empty($result['message'])) {
|
|
||||||
$inlineResults[] = $result['message'];
|
|
||||||
}
|
|
||||||
$executedActions[] = [
|
|
||||||
'tool' => $tool,
|
|
||||||
'status' => 'executed',
|
|
||||||
'entityType' => $result['entityType'] ?? null,
|
|
||||||
'entityId' => $result['entityId'] ?? null,
|
|
||||||
];
|
|
||||||
|
|
||||||
// Create stream note for executed action in case context
|
|
||||||
if ($actionCaseId) {
|
|
||||||
$this->createStreamNote(self::NOTE_TYPE_ACTION, $result['message'] ?? 'פעולה בוצעה', $actionCaseId, [
|
|
||||||
'conversationId' => $conversationId,
|
|
||||||
'status' => 'executed',
|
|
||||||
'entityType' => $result['entityType'] ?? null,
|
|
||||||
'entityId' => $result['entityId'] ?? null,
|
|
||||||
]);
|
|
||||||
}
|
|
||||||
} catch (\Exception $e) {
|
|
||||||
$this->log->warning("SmartAssistant: Execution of {$tool} failed: " . $e->getMessage());
|
|
||||||
$inlineResults[] = "שגיאה בביצוע {$tool}: " . $e->getMessage();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!empty($inlineResults)) {
|
$responseText = $loopResult['responseText'];
|
||||||
$responseText .= "\n\n" . implode("\n\n", $inlineResults);
|
$executedActions = $loopResult['executedActions'];
|
||||||
}
|
|
||||||
|
|
||||||
// Persist response
|
// Persist response
|
||||||
try {
|
try {
|
||||||
@@ -191,6 +161,148 @@ class SmartAssistantService
|
|||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function executeActionsWithLoop(
|
||||||
|
string $message,
|
||||||
|
array $context,
|
||||||
|
string $conversationId,
|
||||||
|
array $conversationHistory,
|
||||||
|
string $mode,
|
||||||
|
?string $effectiveCaseId,
|
||||||
|
string $userId
|
||||||
|
): array {
|
||||||
|
$agenticEnabled = $this->getIntegrationSetting('agenticLoopEnabled', true);
|
||||||
|
$maxLoops = (int) $this->getIntegrationSetting('maxAgenticLoops', self::DEFAULT_MAX_AGENTIC_LOOPS);
|
||||||
|
$executor = $this->injectableFactory->create(ActionExecutor::class);
|
||||||
|
|
||||||
|
$loopCount = 0;
|
||||||
|
$allExecutedActions = [];
|
||||||
|
$currentHistory = $conversationHistory;
|
||||||
|
$finalResponseText = '';
|
||||||
|
|
||||||
|
while ($loopCount <= $maxLoops) {
|
||||||
|
// Call webhook (first call uses original message, subsequent calls use empty message)
|
||||||
|
$currentMessage = $loopCount === 0 ? $message : '';
|
||||||
|
$response = $this->callWebhook($currentMessage, $context, $conversationId, $currentHistory, $mode);
|
||||||
|
|
||||||
|
$actions = $response['actions'] ?? [];
|
||||||
|
$responseText = $response['text'] ?? '';
|
||||||
|
|
||||||
|
// If no actions, we're done
|
||||||
|
if (empty($actions)) {
|
||||||
|
$finalResponseText = $responseText;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
$agenticResults = [];
|
||||||
|
$inlineResults = [];
|
||||||
|
|
||||||
|
foreach ($actions as $action) {
|
||||||
|
$tool = $action['tool'] ?? '';
|
||||||
|
$actionCaseId = $effectiveCaseId;
|
||||||
|
|
||||||
|
if (!$actionCaseId && !empty($action['params']['caseId'])) {
|
||||||
|
$actionCaseId = $action['params']['caseId'];
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!$actionCaseId && in_array($tool, self::TOOLS_REQUIRING_CASE, true)) {
|
||||||
|
$inlineResults[] = "⚠️ לא ניתן לבצע {$tool} — לא זוהה תיק. נסה מתוך מסך התיק.";
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
$result = $executor->execute($tool, $action['params'] ?? [], $actionCaseId, $userId);
|
||||||
|
|
||||||
|
$allExecutedActions[] = [
|
||||||
|
'tool' => $tool,
|
||||||
|
'status' => 'executed',
|
||||||
|
'entityType' => $result['entityType'] ?? null,
|
||||||
|
'entityId' => $result['entityId'] ?? null,
|
||||||
|
];
|
||||||
|
|
||||||
|
if ($actionCaseId) {
|
||||||
|
$this->createStreamNote(self::NOTE_TYPE_ACTION, $result['message'] ?? 'פעולה בוצעה', $actionCaseId, [
|
||||||
|
'conversationId' => $conversationId,
|
||||||
|
'status' => 'executed',
|
||||||
|
'entityType' => $result['entityType'] ?? null,
|
||||||
|
'entityId' => $result['entityId'] ?? null,
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Separate agentic tools (need re-invocation) from inline tools
|
||||||
|
if ($agenticEnabled && in_array($tool, self::AGENTIC_TOOLS, true)) {
|
||||||
|
$agenticResults[] = [
|
||||||
|
'tool' => $tool,
|
||||||
|
'params' => $action['params'] ?? [],
|
||||||
|
'result' => $result['message'] ?? '',
|
||||||
|
];
|
||||||
|
} else {
|
||||||
|
if (!empty($result['message'])) {
|
||||||
|
$inlineResults[] = $result['message'];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
$this->log->warning("SmartAssistant: Execution of {$tool} failed: " . $e->getMessage());
|
||||||
|
$inlineResults[] = "שגיאה בביצוע {$tool}: " . $e->getMessage();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// If no agentic results, we're done — return response with inline results
|
||||||
|
if (empty($agenticResults)) {
|
||||||
|
$finalResponseText = $responseText;
|
||||||
|
if (!empty($inlineResults)) {
|
||||||
|
$finalResponseText .= "\n\n" . implode("\n\n", $inlineResults);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Feed agentic results back to AI for next iteration
|
||||||
|
$this->log->debug("SmartAssistant: Agentic loop iteration " . ($loopCount + 1) . " — " . count($agenticResults) . " tool results to feed back");
|
||||||
|
|
||||||
|
// Add the AI response to history
|
||||||
|
$currentHistory[] = ['role' => 'assistant', 'content' => $responseText];
|
||||||
|
|
||||||
|
// Add each tool result to history
|
||||||
|
foreach ($agenticResults as $ar) {
|
||||||
|
$currentHistory[] = [
|
||||||
|
'role' => 'tool_result',
|
||||||
|
'toolName' => $ar['tool'],
|
||||||
|
'content' => $ar['result'],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
// Also append any inline results
|
||||||
|
if (!empty($inlineResults)) {
|
||||||
|
$currentHistory[] = [
|
||||||
|
'role' => 'tool_result',
|
||||||
|
'toolName' => '_inline_actions',
|
||||||
|
'content' => implode("\n\n", $inlineResults),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
$loopCount++;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Safety: if we exhausted the loop, use the last response
|
||||||
|
if ($loopCount > $maxLoops && empty($finalResponseText)) {
|
||||||
|
$finalResponseText = $response['text'] ?? 'הגעתי למגבלת הסיבובים. אנא נסה שוב.';
|
||||||
|
}
|
||||||
|
|
||||||
|
return [
|
||||||
|
'responseText' => $finalResponseText,
|
||||||
|
'executedActions' => $allExecutedActions,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
private function getIntegrationSetting(string $key, $default = null)
|
||||||
|
{
|
||||||
|
$integration = $this->entityManager->getEntityById('Integration', 'SmartAssistant');
|
||||||
|
if ($integration && $integration->get('enabled')) {
|
||||||
|
$data = (array) ($integration->get('data') ?? []);
|
||||||
|
return $data[$key] ?? $default;
|
||||||
|
}
|
||||||
|
return $default;
|
||||||
|
}
|
||||||
|
|
||||||
public function executeAction(string $conversationId, string $actionId, bool $approved): array
|
public function executeAction(string $conversationId, string $actionId, bool $approved): array
|
||||||
{
|
{
|
||||||
$conversation = $this->loadConversation($conversationId);
|
$conversation = $this->loadConversation($conversationId);
|
||||||
@@ -406,7 +518,7 @@ class SmartAssistantService
|
|||||||
CURLOPT_POSTFIELDS => $payload,
|
CURLOPT_POSTFIELDS => $payload,
|
||||||
CURLOPT_RETURNTRANSFER => true,
|
CURLOPT_RETURNTRANSFER => true,
|
||||||
CURLOPT_HTTPHEADER => $headers,
|
CURLOPT_HTTPHEADER => $headers,
|
||||||
CURLOPT_TIMEOUT => 120,
|
CURLOPT_TIMEOUT => 600,
|
||||||
CURLOPT_CONNECTTIMEOUT => 10,
|
CURLOPT_CONNECTTIMEOUT => 10,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
|||||||
Binary file not shown.
|
After Width: | Height: | Size: 750 KiB |
+2
-2
@@ -3,11 +3,11 @@
|
|||||||
"module": "SmartAssistant",
|
"module": "SmartAssistant",
|
||||||
"description": "Unified AI Assistant for Legal CRM — floating chat with case memory, office alerts, and AI Gateway integration",
|
"description": "Unified AI Assistant for Legal CRM — floating chat with case memory, office alerts, and AI Gateway integration",
|
||||||
"author": "klear",
|
"author": "klear",
|
||||||
"version": "2.5.2",
|
"version": "2.9.2",
|
||||||
"acceptableVersions": [
|
"acceptableVersions": [
|
||||||
">=8.0.0"
|
">=8.0.0"
|
||||||
],
|
],
|
||||||
"releaseDate": "2026-04-06",
|
"releaseDate": "2026-05-27",
|
||||||
"php": [
|
"php": [
|
||||||
">=8.1"
|
">=8.1"
|
||||||
]
|
]
|
||||||
|
|||||||
Reference in New Issue
Block a user