diff --git a/.agents/skills/manage-requirements/SKILL.md b/.agents/skills/manage-requirements/SKILL.md new file mode 100644 index 0000000000..e02c9500f1 --- /dev/null +++ b/.agents/skills/manage-requirements/SKILL.md @@ -0,0 +1,134 @@ +--- +name: manage-requirements +description: >- + Author and validate FR/NFR requirement catalogs (MD + YAML) for any TCE + project. Use when creating, migrating, editing, or validating requirements, + specs with acceptance criteria, requirements.yaml, FR-/NFR- IDs, or + component/screen/script requirement catalogs. +--- + +# Manage requirements + +Cross-project skill. Schemas live here and apply to CollegeV2, Military, High School, Shared, or any new module. + +## File contract + +Each feature is a folder with exactly two artifacts: + +| File | Role | +| --- | --- | +| `.md` (or `README.md`) | Human/AI narrative: purpose, actors, config, links | +| `requirements.yaml` | Machine-checked catalog (`requirements[]`) | + +Schemas (source of truth): + +- [schemas/requirement.schema.json](schemas/requirement.schema.json) — single Requirement +- [schemas/requirements-catalog.schema.json](schemas/requirements-catalog.schema.json) — catalog document + +Templates: [templates/](templates/). + +## Catalog shape + +```yaml +specId: my-feature +kind: component # component | screen | script +title: My feature +config: {} # props / ports / section ids — keep FRs reusable +dependsOn: [] # other specId values +requirements: + - id: FR-001 + requirementType: Functional + title: ... + statement: ... + priority: High + status: Draft + acceptanceCriteria: + - given: ... + when: ... + then: ... +``` + +Rules: + +- Local IDs `FR-NNN` / `NFR-NNN` unique **within** one catalog +- Cross-feature refs: `specId#FR-001` in `dependencies` +- Functional → `acceptanceCriteria` required +- NonFunctional → `category`, `metrics`, `verificationMethod` required +- Edge-case scenario IDs (e.g. `CE-001`) go in `traceability.testCases` only + +## Authoring by kind + +1. **component** — Behavior given `config`. No screen names in `statement` / `acceptanceCriteria`. Screens list the component in `dependsOn` and describe their config in the MD. +2. **script** — Domain outcomes via named ports. No vendor URLs or ProcessMaker HTTP in statements; adapters belong in `config` / `assumptions`. +3. **screen** — Workflow composition, permissions, Continue rules. Reuse components via `dependsOn`; do not duplicate component FRs. + +### CollegeV2 course comments (`comments_log`) + +- Prefer a **component** catalog for shared `CommentsLogTable` (read-only thread); screen RFs `dependsOn` it and describe when the section is visible (`comments_log.length > 0`). +- In screen MD **assumptions**, state whether comments are stored in **case JSON** (`course.comments_log[]`), appended only on **save** from a draft field (`comments_note` / student `comments`), and/or loaded later via **`acol_applicant_course_comments`** HTTP — avoid contradictory `then` clauses across catalogs. +- Acceptance criteria should spell out **role** (`tco`, `student`), **read vs write**, and **append-on-save** (draft is not in the log until save succeeds). +- Multi-external articulation: if one save updates linked courses, say whether companions receive the same `comments_log` entries or only articulation/inbox fields. + +Suggested project layout (optional; any tree of `requirements.yaml` validates): + +```text +ai-docs//requirements/ + components//{.md,requirements.yaml} + screens//{.md,requirements.yaml} + scripts//{.md,requirements.yaml} +``` + +## Validate + +Primary gate (schema + heuristic quality): + +```bash +npm run check:requirements # all ai-docs/*/requirements +npm run check:requirements -- college-v2 # one project +npm run check:requirements -- military +``` + +Schema-only (path optional; default is every `ai-docs/*/requirements` tree): + +```bash +npm run validate:requirements +npm run validate:requirements -- path/to/requirements.yaml +npm run validate:requirements -- path/to/feature-or-tree +``` + +Schema checks required fields, unique IDs, `dependsOn`, companion markdown (`.md` or `README.md`), and non-empty Given/When/Then. Tautological acceptance criteria (placeholder given/when, or `then` repeating the title) are warnings, not hard failures. + +Quality harness (any project) lives in [quality/](quality/). Project text fixtures stay under `ai-docs//quality/` (`*.yaml` questions, optional `vocabulary.md`). `check:requirements` / `judge_catalog.py` exits non-zero while any FR fails (for example stub acceptance criteria). + +Harness unit tests (tooling, not catalog gates): + +```bash +npm run test:requirements-schema +npm run test:requirements-quality +``` + +Laya (downloads a local checkpoint): + +```bash +# pip install -r .agents/skills/manage-requirements/quality/requirements.txt +# REQUIREMENTS_PROJECT=college-v2 LAYA_SOURCE=laya .agents/skills/manage-requirements/quality/run_laya_eval.sh +``` + +Global tests are `ai-docs//quality/*.yaml` with `applies.all`. Domain extras are more YAML in that folder with `applies.ids` (or kinds/tags). + +Checklist before finishing: + +- [ ] MD + `requirements.yaml` pair exists +- [ ] `npm run check:requirements -- ` passes (or `validate:requirements -- ` for schema-only) +- [ ] IDs unique in catalog; Functional/NonFunctional fields complete +- [ ] Component/script statements stay screen-/vendor-agnostic +- [ ] Screen catalogs `dependsOn` shared components instead of copying FRs +- [ ] Edge cases linked via `traceability.testCases` when known + +## Create / migrate workflow + +1. Choose `kind` and `specId` (stable folder basename). +2. Copy templates from `templates/`; rename the MD to match `specId`. +3. Extract FRs from existing design docs / edge cases; map scenarios to acceptance criteria and `testCases`. +4. For screens that embed a reusable UI, create/reuse a **component** catalog first, then `dependsOn` it. +5. Validate; fix schema errors before committing. diff --git a/.agents/skills/manage-requirements/quality/README.md b/.agents/skills/manage-requirements/quality/README.md new file mode 100644 index 0000000000..413ab2b3d0 --- /dev/null +++ b/.agents/skills/manage-requirements/quality/README.md @@ -0,0 +1,30 @@ +# Requirement quality harness + +Cross-project Python tools for FR/NFR quality (schema companion to `scripts/validate-requirements.js`). + +Project-specific text stays under `ai-docs//quality/`: + +| Path | Role | +| --- | --- | +| `*.yaml` | Global QC (`applies.all`) and targeted extras | +| `vocabulary.md` | Optional domain terms prepended to each Laya state | + +```bash +# Heuristic gate (any project) +python3 judge_catalog.py --project college-v2 --source heuristic + +# Or explicit paths +python3 judge_catalog.py \ + --root ai-docs/military/requirements \ + --questions ai-docs/military/quality \ + --source heuristic + +# Unit tests: synthetic catalogs + global QC from ai-docs//quality +# Discovers every pack with quality/*.yaml, or pin one: +# REQUIREMENTS_PROJECT=military python3 -m unittest discover -s . -p 'test_*.py' +python3 -m unittest discover -s . -p 'test_*.py' + +# Laya (optional) +pip install -r requirements.txt +REQUIREMENTS_PROJECT=college-v2 LAYA_SOURCE=laya ./run_laya_eval.sh +``` diff --git a/.agents/skills/manage-requirements/quality/build_gold.py b/.agents/skills/manage-requirements/quality/build_gold.py new file mode 100644 index 0000000000..2c77743ecf --- /dev/null +++ b/.agents/skills/manage-requirements/quality/build_gold.py @@ -0,0 +1,65 @@ +"""Generate targeted-extra JSONL from catalogs + question YAML (optional Laya input).""" + +from __future__ import annotations + +import argparse +import json +from pathlib import Path + +from extract_requirement_states import iter_requirement_rows, to_jsonl_record +from load_questions import ( + global_laya_questions, + load_question_documents, + targeted_expected_by_requirement, + targeted_laya_by_requirement, + validate_questions, +) +from project import add_project_args, load_vocabulary, paths_from_args + + +def write_jsonl(path: Path, records: list[dict]) -> None: + path.parent.mkdir(parents=True, exist_ok=True) + with path.open("w", encoding="utf-8") as handle: + for record in records: + handle.write(json.dumps(record, ensure_ascii=False) + "\n") + + +def main(argv: list[str] | None = None) -> int: + parser = argparse.ArgumentParser() + add_project_args(parser) + parser.add_argument("--extra-out", type=Path, help="Default: /extra.jsonl") + args = parser.parse_args(argv) + + paths = paths_from_args(args) + extra_out = args.extra_out or (paths.questions_dir / "extra.jsonl") + + questions = load_question_documents(paths.questions_dir) + quality_questions = global_laya_questions(questions) + vocabulary = load_vocabulary(paths.vocabulary_path) + rows = iter_requirement_rows( + paths.requirements_root, + vocabulary=vocabulary, + questions=quality_questions, + ) + errors = validate_questions(rows, paths.questions_dir) + if errors: + for err in errors: + print(f"FAIL {err}") + return 1 + + rows_by_id = {row["id"]: row for row in rows} + extra_questions = targeted_laya_by_requirement(rows, questions) + extra_expected = targeted_expected_by_requirement(rows, questions) + extra_records = [ + to_jsonl_record(rows_by_id[key], extra_expected[key], questions=extra_questions[key]) + for key in extra_questions + if key in extra_expected + ] + + write_jsonl(extra_out, extra_records) + print(f"Wrote {len(extra_records)} extra-question rows to {extra_out}") + return 0 + + +if __name__ == "__main__": + raise SystemExit(main()) diff --git a/.agents/skills/manage-requirements/quality/extract_requirement_states.py b/.agents/skills/manage-requirements/quality/extract_requirement_states.py new file mode 100644 index 0000000000..80fd8c8214 --- /dev/null +++ b/.agents/skills/manage-requirements/quality/extract_requirement_states.py @@ -0,0 +1,199 @@ +"""Walk requirements.yaml catalogs and emit one Laya state per requirement.""" + +from __future__ import annotations + +import argparse +import json +import sys +from pathlib import Path + +import yaml + +from project import REPO_ROOT, add_project_args, load_vocabulary, paths_from_args + +PLACEHOLDER_GIVEN = "the feature is available" +PLACEHOLDER_WHEN = "the behavior is exercised" + + +def collect_yaml_files(root: Path) -> list[Path]: + if root.is_file(): + return [root] + return sorted(root.rglob("requirements.yaml")) + + +def _normalize(value: object) -> str: + return " ".join(str(value or "").lower().split()) + + +def is_stub_criterion(req: dict, criterion: dict) -> bool: + given = str(criterion.get("given") or "") + when = str(criterion.get("when") or "") + then = _normalize(criterion.get("then")) + title = _normalize(req.get("title")) + placeholder = PLACEHOLDER_GIVEN in given.lower() and PLACEHOLDER_WHEN in when.lower() + then_repeats_title = bool(then) and then == title + return placeholder or then_repeats_title + + +def requirement_has_stub_ac(req: dict) -> bool: + criteria = req.get("acceptanceCriteria") or [] + return any(is_stub_criterion(req, item) for item in criteria if isinstance(item, dict)) + + +# Backward-compatible aliases. +is_tautological_criterion = is_stub_criterion +requirement_is_tautological = requirement_has_stub_ac + + +def compact_requirement(req: dict) -> dict: + payload = { + "id": req.get("id"), + "requirementType": req.get("requirementType"), + "title": req.get("title"), + "statement": req.get("statement"), + "priority": req.get("priority"), + "status": req.get("status"), + } + if req.get("acceptanceCriteria"): + payload["acceptanceCriteria"] = req["acceptanceCriteria"] + if req.get("metrics"): + payload["metrics"] = req["metrics"] + if req.get("verificationMethod"): + payload["verificationMethod"] = req["verificationMethod"] + if req.get("category"): + payload["category"] = req["category"] + return payload + + +def build_state(catalog: dict, req: dict, vocabulary: str = "") -> str: + body = yaml.safe_dump(compact_requirement(req), sort_keys=False, allow_unicode=True).strip() + parts = [] + if vocabulary.strip(): + parts.append(vocabulary.strip()) + parts.append("") + parts.extend( + [ + "Requirement catalog:", + f"specId: {catalog.get('specId')}", + f"kind: {catalog.get('kind')}", + f"title: {catalog.get('title')}", + "", + body, + "", + ] + ) + return "\n".join(parts) + + +def requirement_key(spec_id: str, req_id: str) -> str: + return f"{spec_id}#{req_id}" + + +def relative_repo_path(path: Path, repo_root: Path | None = None) -> str: + root = repo_root or REPO_ROOT + resolved = path.resolve() + try: + return resolved.relative_to(root.resolve()).as_posix() + except ValueError: + return resolved.as_posix() + + +def requirement_line_numbers(text: str) -> dict[str, int]: + """Map requirement id -> 1-based line of the `- id:` entry.""" + lines: dict[str, int] = {} + for index, line in enumerate(text.splitlines(), start=1): + stripped = line.strip() + if not stripped.startswith("- id:"): + continue + req_id = stripped.split(":", 1)[1].strip().strip("\"'") + if req_id and req_id not in lines: + lines[req_id] = index + return lines + + +def iter_requirement_rows( + root: Path, + *, + vocabulary: str = "", + questions: dict | None = None, + repo_root: Path | None = None, +) -> list[dict]: + rows = [] + for yaml_path in collect_yaml_files(root): + text = yaml_path.read_text(encoding="utf-8") + catalog = yaml.safe_load(text) + if not isinstance(catalog, dict): + continue + spec_id = catalog.get("specId") + line_by_id = requirement_line_numbers(text) + source_path = relative_repo_path(yaml_path, repo_root=repo_root) + for req in catalog.get("requirements") or []: + if not isinstance(req, dict) or not req.get("id"): + continue + req_id = str(req["id"]) + key = requirement_key(str(spec_id), req_id) + tags = req.get("tags") or [] + if not isinstance(tags, list): + tags = [tags] + rows.append( + { + "id": key, + "specId": spec_id, + "requirementId": req_id, + "title": str(req.get("title") or "").strip(), + "sourcePath": source_path, + "sourceLine": line_by_id.get(req_id), + "requirementType": req.get("requirementType"), + "kind": catalog.get("kind"), + "reqTags": [str(tag) for tag in tags], + "stub_ac": requirement_has_stub_ac(req), + "tautological": requirement_has_stub_ac(req), + "state": build_state(catalog, req, vocabulary=vocabulary), + "questions": questions if questions is not None else {}, + "tags": [str(catalog.get("kind") or "unknown"), str(spec_id)], + "language": "en", + "model": "english", + } + ) + return rows + + +def to_jsonl_record(row: dict, expected: dict | None = None, questions: dict | None = None) -> dict: + record = { + "state": row["state"], + "questions": questions if questions is not None else row["questions"], + "tags": row["tags"] + ([row["id"]]), + "language": row["language"], + "model": row["model"], + } + if expected is not None: + record["expected"] = expected + return record + + +def main(argv: list[str] | None = None) -> int: + parser = argparse.ArgumentParser(description="Extract requirements as Laya states.") + add_project_args(parser) + parser.add_argument("--jsonl", type=Path, help="Write one JSON object per requirement") + args = parser.parse_args(argv) + + paths = paths_from_args(args) + vocabulary = load_vocabulary(paths.vocabulary_path) + rows = iter_requirement_rows(paths.requirements_root, vocabulary=vocabulary) + if args.jsonl: + args.jsonl.parent.mkdir(parents=True, exist_ok=True) + with args.jsonl.open("w", encoding="utf-8") as handle: + for row in rows: + handle.write(json.dumps(to_jsonl_record(row), ensure_ascii=False) + "\n") + else: + json.dump( + [{"id": row["id"], "stub_ac": row["stub_ac"]} for row in rows], + sys.stdout, + indent=2, + ) + sys.stdout.write("\n") + return 0 + + +if __name__ == "__main__": + raise SystemExit(main()) diff --git a/.agents/skills/manage-requirements/quality/judge_catalog.py b/.agents/skills/manage-requirements/quality/judge_catalog.py new file mode 100644 index 0000000000..11d615e874 --- /dev/null +++ b/.agents/skills/manage-requirements/quality/judge_catalog.py @@ -0,0 +1,347 @@ +"""Judge requirements and fail when any global QC check fails.""" + +from __future__ import annotations + +import argparse +import json +import os +import sys +from pathlib import Path + +from extract_requirement_states import iter_requirement_rows +from load_questions import ( + defect_criteria_from_questions, + global_laya_questions, + load_question_documents, + noul_question_ids, +) +from project import add_project_args, load_vocabulary, paths_from_args + +NOUL_FAIL_THRESHOLD = 0.5 +MAX_LOW_CONFIDENCE = 0 +_ANSI_RED = "\033[31m" +_ANSI_RESET = "\033[0m" + +DEFECT_LABELS = { + "stub_ac": "stub acceptance criteria", + "vague_then": "vague then/metric", + "vocab_drift": "vocabulary drift", + "missing_observable": "missing observable outcome", + "unbounded": "unbounded / TBD scope", +} + + +def heuristic_answers(row: dict) -> dict: + stub_ac = bool(row.get("stub_ac") if "stub_ac" in row else row.get("tautological")) + yes = 0.95 + no = 0.05 + return { + "clear": {"noul": yes, "answer_confidence": yes, "low_confidence": False}, + "verifiable": { + "noul": no if stub_ac else yes, + "answer_confidence": yes, + "low_confidence": False, + }, + "testable": { + "noul": no if stub_ac else yes, + "answer_confidence": yes, + "low_confidence": False, + }, + "consistent": {"noul": yes, "answer_confidence": yes, "low_confidence": False}, + "achievable": {"noul": yes, "answer_confidence": yes, "low_confidence": False}, + "primary_defect": { + "choice": "stub_ac" if stub_ac else "none", + "answer_confidence": yes, + "low_confidence": False, + }, + } + + +def noul_value(answer: dict) -> float: + if "noul" in answer and isinstance(answer["noul"], (int, float)): + return float(answer["noul"]) + choice = str(answer.get("choice") or "").upper() + if choice == "A": + return 1.0 + if choice == "B": + return 0.0 + return 0.0 + + +def failed_quality_reasons( + answers: dict, + noul_ids: tuple[str, ...], + min_confidence: float = 0.45, +) -> list[str]: + """Return every failed global QC question id (and low_confidence when gated).""" + reasons = [] + for key in noul_ids: + answer = answers.get(key) or {} + confidence = float(answer.get("answer_confidence") or noul_value(answer) or 0.0) + if answer.get("low_confidence") or confidence < min_confidence: + reasons.append("low_confidence") + continue + if noul_value(answer) < NOUL_FAIL_THRESHOLD: + reasons.append(key) + seen = set() + ordered = [] + for reason in reasons: + if reason not in seen: + seen.add(reason) + ordered.append(reason) + return ordered + + +def requirement_quality_failure( + answers: dict, + noul_ids: tuple[str, ...], + min_confidence: float = 0.45, +) -> tuple[bool, str | None]: + reasons = failed_quality_reasons(answers, noul_ids, min_confidence) + if not reasons: + return False, None + return True, reasons[0] + + +def primary_defect_of(answers: dict) -> str | None: + defect = answers.get("primary_defect") or {} + choice = defect.get("choice") + return str(choice) if choice else None + + +def defect_label(primary_defect: str | None) -> str | None: + if not primary_defect or primary_defect == "none": + return None + return DEFECT_LABELS.get(primary_defect, primary_defect.replace("_", " ")) + + +def failure_reason_text(primary_defect: str | None) -> str: + return defect_label(primary_defect) or "quality check failed" + + +def use_color(stream=None) -> bool: + force = (os.environ.get("FORCE_COLOR") or "").strip().lower() + if force in {"1", "true", "yes", "on"}: + return True + if force in {"0", "false", "no", "off"}: + return False + if os.environ.get("NO_COLOR"): + return False + target = stream if stream is not None else sys.stdout + return bool(getattr(target, "isatty", lambda: False)()) + + +def failed_badge(color: bool | None = None) -> str: + if color is None: + color = use_color() + if color: + return f"{_ANSI_RED}[FAILED]{_ANSI_RESET}" + return "[FAILED]" + + +def format_failure_heading(item: dict, *, color: bool | None = None) -> str: + """Build: path#line FR-ID: Title [FAILED] (falls back to catalog#id).""" + source_path = item.get("sourcePath") + source_line = item.get("sourceLine") + req_id = item.get("requirementId") or ( + str(item.get("id") or "").split("#", 1)[-1] if item.get("id") else "UNKNOWN" + ) + title = str(item.get("title") or "").strip() + if source_path and source_line: + location = f"{source_path}#{source_line}" + elif source_path: + location = str(source_path) + else: + location = str(item.get("id") or req_id) + badge = failed_badge(color) + if title: + return f"✖ {location} {req_id}: {title} {badge}" + return f"✖ {location} {req_id} {badge}" + + +def format_failure_detail( + item: dict | str, + reasons: list[str] | None = None, + primary_defect: str | None = None, + *, + color: bool | None = None, + **kwargs, +) -> list[str]: + if isinstance(item, str): + item = { + "id": item, + "reasons": reasons or [], + "primary_defect": primary_defect or kwargs.get("primary_defect"), + } + else: + reasons = reasons if reasons is not None else item.get("reasons") or [] + if primary_defect is None: + primary_defect = item.get("primary_defect") + checks = ", ".join(reasons or []) if reasons else "unknown" + return [ + format_failure_heading(item, color=color), + f"Failed checks: [{checks}]", + f"Reason: {failure_reason_text(primary_defect)}", + ] + + +def snapshot_from_rows( + rows: list[dict], + answers_by_id: dict[str, dict], + source: str, + noul_ids: tuple[str, ...], +) -> dict: + failures = [] + low_confidence = 0 + for row in rows: + answers = answers_by_id[row["id"]] + reasons = failed_quality_reasons(answers, noul_ids) + if "low_confidence" in reasons: + low_confidence += 1 + if reasons: + failures.append( + { + "id": row["id"], + "requirementId": row.get("requirementId"), + "title": row.get("title") or "", + "sourcePath": row.get("sourcePath"), + "sourceLine": row.get("sourceLine"), + "reasons": reasons, + "primary_defect": primary_defect_of(answers), + } + ) + failures.sort(key=lambda item: item["id"]) + return { + "source": source, + "failedIds": [item["id"] for item in failures], + "failures": failures, + "lowConfidenceCount": low_confidence, + "requirementCount": len(rows), + } + + +RULE = "─" * 60 + + +def format_summary(current: dict) -> list[str]: + total = int(current.get("requirementCount") or 0) + failed = len(current.get("failures") or []) + warnings = int(current.get("lowConfidenceCount") or 0) + passed = max(total - failed, 0) + return [ + RULE, + f"Requirements analyzed: {total}", + f"Passed: {passed}", + f"Failed: {failed}", + f"Warnings: {warnings}", + RULE, + ] + + +def format_quality_report( + current: dict, + defect_info: dict[str, str] | None = None, + *, + color: bool | None = None, +) -> list[str]: + """Human-readable report lines for quality failures (empty when clean).""" + failures = list(current.get("failures") or []) + if not failures and current.get("lowConfidenceCount", 0) <= MAX_LOW_CONFIDENCE: + return [] + + lines = format_summary(current) + lines.append("") + for index, item in enumerate(failures): + if index: + lines.append("") + lines.extend(format_failure_detail(item, defect_info=defect_info, color=color)) + lines.append("") + return lines + + +def report_quality_failures( + current: dict, + defect_info: dict[str, str] | None = None, + *, + color: bool | None = None, +) -> list[str]: + """Return error lines when the catalog has quality failures.""" + return format_quality_report(current, defect_info=defect_info, color=color) + + +def compare_snapshot(current: dict, committed: dict | None = None, **_kwargs) -> list[str]: + del committed + return report_quality_failures(current) + + +def predict_with_laya(rows: list[dict], min_confidence: float) -> dict[str, dict]: + from laya import Router + + router = Router() + answers_by_id = {} + for row in rows: + result = router.predict( + row["state"], + row["questions"], + model="english", + min_confidence=min_confidence, + ) + answers_by_id[row["id"]] = result["answers"] + return answers_by_id + + +def main(argv: list[str] | None = None) -> int: + parser = argparse.ArgumentParser( + description="Judge requirement quality; fails if any FR fails global QC." + ) + add_project_args(parser) + parser.add_argument("--source", choices=("heuristic", "laya"), default="heuristic") + parser.add_argument("--min-confidence", type=float, default=0.45) + parser.add_argument("--report-only", action="store_true") + parser.add_argument("--report", type=Path) + args = parser.parse_args(argv) + + paths = paths_from_args(args) + questions = load_question_documents(paths.questions_dir) + quality_questions = global_laya_questions(questions) + noul_ids = noul_question_ids(questions) + defect_info = defect_criteria_from_questions(questions) + vocabulary = load_vocabulary(paths.vocabulary_path) + rows = iter_requirement_rows( + paths.requirements_root, + vocabulary=vocabulary, + questions=quality_questions, + ) + + if args.source == "laya": + answers_by_id = predict_with_laya(rows, args.min_confidence) + source = "laya" + else: + answers_by_id = {row["id"]: heuristic_answers(row) for row in rows} + source = "heuristic" + + current = snapshot_from_rows(rows, answers_by_id, source, noul_ids) + if args.report: + args.report.parent.mkdir(parents=True, exist_ok=True) + args.report.write_text(json.dumps(current, indent=2) + "\n", encoding="utf-8") + + report = report_quality_failures(current, defect_info=defect_info) + if args.report_only: + for line in report or format_summary(current): + print(line) + return 0 + + if report: + for line in report: + print(line) + return 1 + + for line in format_summary(current): + print(line) + print("") + print("All requirements passed global QC.") + return 0 + + +if __name__ == "__main__": + raise SystemExit(main()) diff --git a/.agents/skills/manage-requirements/quality/judge_extra.py b/.agents/skills/manage-requirements/quality/judge_extra.py new file mode 100644 index 0000000000..b90fe5cd9e --- /dev/null +++ b/.agents/skills/manage-requirements/quality/judge_extra.py @@ -0,0 +1,98 @@ +"""Run targeted Laya questions (applies.ids / kinds / tags, not global QC).""" + +from __future__ import annotations + +import argparse +import json +from pathlib import Path + +from extract_requirement_states import iter_requirement_rows +from load_questions import ( + global_laya_questions, + load_question_documents, + targeted_expected_by_requirement, + targeted_laya_by_requirement, + validate_questions, +) +from project import add_project_args, load_vocabulary, paths_from_args + + +def _matches(expected, answer: dict) -> bool: + if "noul" in answer and isinstance(expected, bool): + return (float(answer["noul"]) >= 0.5) is expected + if "choice" in answer: + return answer["choice"] == expected + if "score" in answer and isinstance(expected, (int, float)): + return int(round(float(answer["score"]))) == int(expected) + return False + + +def judge_extra_answers( + answers_by_id: dict[str, dict], + expected_by_id: dict[str, dict], + reserved_ids: set[str] | None = None, +) -> list[str]: + errors = [] + reserved = reserved_ids or set() + for row_id, expected in expected_by_id.items(): + answers = answers_by_id.get(row_id) or {} + for qid, value in expected.items(): + if qid in reserved: + errors.append(f"{row_id}: extra id {qid} collides with global QC") + continue + if not _matches(value, answers.get(qid) or {}): + errors.append(f"{row_id} extra.{qid}: expected {value!r}, got {answers.get(qid)!r}") + return errors + + +def main(argv: list[str] | None = None) -> int: + parser = argparse.ArgumentParser(description="Judge targeted per-requirement Laya questions.") + add_project_args(parser) + parser.add_argument("--report", type=Path) + args = parser.parse_args(argv) + + paths = paths_from_args(args) + questions = load_question_documents(paths.questions_dir) + quality_questions = global_laya_questions(questions) + vocabulary = load_vocabulary(paths.vocabulary_path) + rows = iter_requirement_rows( + paths.requirements_root, + vocabulary=vocabulary, + questions=quality_questions, + ) + errors = validate_questions(rows, paths.questions_dir) + if errors: + for err in errors: + print(f"FAIL {err}") + return 1 + + extra_questions = targeted_laya_by_requirement(rows, questions) + extra_expected = targeted_expected_by_requirement(rows, questions) + if not extra_questions: + print("No targeted Laya questions.") + return 0 + + from laya import Router + + rows_by_id = {row["id"]: row for row in rows} + router = Router() + answers_by_id = {} + for row_id, qset in extra_questions.items(): + result = router.predict(rows_by_id[row_id]["state"], qset, model="english") + answers_by_id[row_id] = result["answers"] + + if args.report: + args.report.parent.mkdir(parents=True, exist_ok=True) + args.report.write_text(json.dumps(answers_by_id, indent=2, default=str) + "\n", encoding="utf-8") + + mismatches = judge_extra_answers(answers_by_id, extra_expected, set(quality_questions)) + if mismatches: + for err in mismatches: + print(f"FAIL {err}") + return 1 + print(f"{len(extra_questions)} targeted Laya requirement(s) matched expected labels.") + return 0 + + +if __name__ == "__main__": + raise SystemExit(main()) diff --git a/.agents/skills/manage-requirements/quality/load_questions.py b/.agents/skills/manage-requirements/quality/load_questions.py new file mode 100644 index 0000000000..07fc709c57 --- /dev/null +++ b/.agents/skills/manage-requirements/quality/load_questions.py @@ -0,0 +1,234 @@ +"""Load quality-question YAML tests (global QC and targeted extras).""" + +from __future__ import annotations + +import argparse +from pathlib import Path + +import yaml + +from extract_requirement_states import iter_requirement_rows +from project import add_project_args, load_vocabulary, paths_from_args + +LAYA_TYPES = {"noul", "choice", "score"} +CHECK_TYPE = "check" +METADATA_KEYS = {"id", "applies", "exclude", "expected", "checks", "path"} + + +def _as_list(value) -> list: + if value is None: + return [] + if isinstance(value, list): + return value + return [value] + + +def load_question_documents(questions_dir: Path) -> list[dict]: + if not questions_dir.is_dir(): + raise FileNotFoundError(f"Questions directory not found: {questions_dir}") + documents = [] + for path in sorted(questions_dir.glob("*.yaml")): + data = yaml.safe_load(path.read_text(encoding="utf-8")) or {} + if not isinstance(data, dict): + raise ValueError(f"{path} must be a YAML mapping") + qid = data.get("id") + if qid != path.stem: + raise ValueError(f"{path.name}: id {qid!r} must match filename stem {path.stem!r}") + qtype = data.get("type") + if qtype not in LAYA_TYPES | {CHECK_TYPE}: + raise ValueError(f"{path.name}: type must be noul, choice, score, or check") + data = dict(data) + data["path"] = path + documents.append(data) + if not documents: + raise ValueError(f"No question YAML files in {questions_dir}") + return documents + + +def is_global_qc(question: dict) -> bool: + applies = question.get("applies") or {} + return bool(applies.get("all")) and question.get("type") in LAYA_TYPES + + +def laya_payload(question: dict) -> dict: + payload = {} + for key, value in question.items(): + if key not in METADATA_KEYS: + payload[key] = value + return payload + + +def global_laya_questions(questions: list[dict]) -> dict: + return {q["id"]: laya_payload(q) for q in questions if is_global_qc(q)} + + +def noul_question_ids(questions: list[dict]) -> tuple[str, ...]: + return tuple(q["id"] for q in questions if is_global_qc(q) and q.get("type") == "noul") + + +def quality_expected_keys(questions: list[dict]) -> tuple[str, ...]: + return tuple(q["id"] for q in questions if is_global_qc(q)) + + +def question_applies(question: dict, row: dict) -> bool: + exclude = question.get("exclude") or {} + if row["id"] in _as_list(exclude.get("ids")): + return False + if row.get("specId") in _as_list(exclude.get("specIds")): + return False + applies = question.get("applies") or {} + if applies.get("all"): + return True + filters = False + if applies.get("ids"): + filters = True + if row["id"] not in _as_list(applies.get("ids")): + return False + if applies.get("specIds"): + filters = True + if row.get("specId") not in _as_list(applies.get("specIds")): + return False + if applies.get("kinds"): + filters = True + if row.get("kind") not in _as_list(applies.get("kinds")): + return False + if applies.get("requirementTypes"): + filters = True + if row.get("requirementType") not in _as_list(applies.get("requirementTypes")): + return False + if applies.get("tags"): + filters = True + row_tags = {str(tag) for tag in (row.get("reqTags") or [])} + if not row_tags.intersection({str(tag) for tag in _as_list(applies.get("tags"))}): + return False + return filters + + +def expected_for(question: dict, row_id: str): + expected = question.get("expected") + if isinstance(expected, dict): + return expected.get(row_id) + return expected + + +def targeted_laya_by_requirement(rows: list[dict], questions: list[dict]) -> dict[str, dict]: + by_id: dict[str, dict] = {} + for question in questions: + if question.get("type") not in LAYA_TYPES or is_global_qc(question): + continue + for row in rows: + if not question_applies(question, row): + continue + by_id.setdefault(row["id"], {})[question["id"]] = laya_payload(question) + return by_id + + +def targeted_expected_by_requirement(rows: list[dict], questions: list[dict]) -> dict[str, dict]: + by_id: dict[str, dict] = {} + for question in questions: + if question.get("type") not in LAYA_TYPES or is_global_qc(question): + continue + for row in rows: + if not question_applies(question, row): + continue + value = expected_for(question, row["id"]) + if value is None: + continue + by_id.setdefault(row["id"], {})[question["id"]] = value + return by_id + + +def defect_criteria_from_questions(questions: list[dict]) -> dict[str, str]: + for question in questions: + if question["id"] == "primary_defect": + criteria = question.get("criteria") or {} + return {str(key): str(value) for key, value in criteria.items() if key != "none"} + return {} + + +def _haystack(row: dict, check: dict) -> str: + target = check.get("in", "state") + if target == "state": + return row["state"] + raise ValueError(f"Unsupported check.in {target!r}") + + +def run_question_checks(row: dict, question: dict) -> list[str]: + errors = [] + for index, check in enumerate(question.get("checks") or []): + if not isinstance(check, dict) or "text" not in check: + errors.append(f"{question['id']}@{row['id']} checks[{index}]: needs type and text") + continue + kind = check.get("type") + needle = str(check["text"]) + found = needle.lower() in _haystack(row, check).lower() + if kind == "contains" and not found: + errors.append(f"{question['id']}@{row['id']} checks[{index}]: expected to contain {needle!r}") + elif kind == "notContains" and found: + errors.append(f"{question['id']}@{row['id']} checks[{index}]: must not contain {needle!r}") + elif kind not in {"contains", "notContains"}: + errors.append(f"{question['id']}@{row['id']} checks[{index}]: unknown type {kind!r}") + return errors + + +def validate_questions(rows: list[dict], questions_dir: Path) -> list[str]: + questions = load_question_documents(questions_dir) + errors: list[str] = [] + global_ids = [q["id"] for q in questions if is_global_qc(q)] + if not global_ids: + errors.append("no global QC questions (applies.all + noul/choice/score)") + seen = set() + rows_by_id = {row["id"]: row for row in rows} + for question in questions: + qid = question["id"] + if qid in seen: + errors.append(f"duplicate question id {qid}") + seen.add(qid) + applies = question.get("applies") or {} + if not applies.get("all") and not any( + applies.get(key) for key in ("ids", "specIds", "kinds", "requirementTypes", "tags") + ): + errors.append(f"{qid}: applies must set all: true or at least one selector") + for key in ("ids", "specIds"): + for ref in _as_list((applies.get(key) or []) + _as_list((question.get("exclude") or {}).get(key))): + if key == "ids" and ref not in rows_by_id: + errors.append(f"{qid}: applies/exclude id {ref} not found in catalogs") + if question.get("type") == CHECK_TYPE and not question.get("checks"): + errors.append(f"{qid}: check questions need checks[]") + if question.get("type") in LAYA_TYPES and not is_global_qc(question): + targets = [row for row in rows if question_applies(question, row)] + if not targets: + errors.append(f"{qid}: targeted Laya question matches no requirements") + for row in targets: + if expected_for(question, row["id"]) is None: + errors.append(f"{qid}: missing expected for {row['id']}") + for row in rows: + if question_applies(question, row) and question.get("checks"): + errors.extend(run_question_checks(row, question)) + return errors + + +def main(argv: list[str] | None = None) -> int: + parser = argparse.ArgumentParser(description="Validate quality question YAML against catalogs.") + add_project_args(parser) + args = parser.parse_args(argv) + paths = paths_from_args(args) + vocabulary = load_vocabulary(paths.vocabulary_path) + rows = iter_requirement_rows(paths.requirements_root, vocabulary=vocabulary) + errors = validate_questions(rows, paths.questions_dir) + if errors: + for err in errors: + print(f"FAIL {err}") + return 1 + questions = load_question_documents(paths.questions_dir) + targeted = sum(1 for q in questions if q.get("type") in LAYA_TYPES and not is_global_qc(q)) + checks = sum(1 for q in questions if q.get("type") == CHECK_TYPE) + print( + f"{len(global_laya_questions(questions))} global QC questions, " + f"{targeted} targeted Laya question(s), {checks} check question(s)." + ) + return 0 + + +if __name__ == "__main__": + raise SystemExit(main()) diff --git a/.agents/skills/manage-requirements/quality/project.py b/.agents/skills/manage-requirements/quality/project.py new file mode 100644 index 0000000000..77ca6381ee --- /dev/null +++ b/.agents/skills/manage-requirements/quality/project.py @@ -0,0 +1,142 @@ +"""Resolve per-project requirements + quality-question paths under ai-docs/.""" + +from __future__ import annotations + +import argparse +import os +from dataclasses import dataclass +from pathlib import Path + +QUALITY_PKG = Path(__file__).resolve().parent +# quality/ -> manage-requirements/ -> skills/ -> .agents/ -> +REPO_ROOT = QUALITY_PKG.parents[3] + + +@dataclass(frozen=True) +class ProjectPaths: + name: str | None + requirements_root: Path + questions_dir: Path + vocabulary_path: Path + + +def resolve_project(name: str, repo_root: Path | None = None) -> ProjectPaths: + root = repo_root or REPO_ROOT + questions_dir = root / "ai-docs" / name / "quality" + return ProjectPaths( + name=name, + requirements_root=root / "ai-docs" / name / "requirements", + questions_dir=questions_dir, + vocabulary_path=questions_dir / "vocabulary.md", + ) + + +def discover_quality_projects(repo_root: Path | None = None) -> list[str]: + """Return ai-docs/ folders that contain quality/*.yaml.""" + docs = (repo_root or REPO_ROOT) / "ai-docs" + if not docs.is_dir(): + return [] + names: list[str] = [] + for path in sorted(docs.iterdir()): + if not path.is_dir() or path.name.startswith("."): + continue + quality = path / "quality" + if quality.is_dir() and any(quality.glob("*.yaml")): + names.append(path.name) + return names + + +def quality_packs_for_tests( + project: str | None = None, + *, + repo_root: Path | None = None, + environ: dict[str, str] | None = None, +) -> list[ProjectPaths]: + """ + Quality packs to exercise in harness unit tests. + + Resolution order: + 1. Explicit ``project`` argument + 2. ``REQUIREMENTS_PROJECT`` env + 3. Every discovered ``ai-docs/*/quality`` pack + """ + env = environ if environ is not None else os.environ + chosen = (project or env.get("REQUIREMENTS_PROJECT") or "").strip() + if chosen: + paths = resolve_project(chosen, repo_root=repo_root) + if not paths.questions_dir.is_dir(): + raise FileNotFoundError( + f"No quality directory for project {chosen!r}: {paths.questions_dir}" + ) + if not any(paths.questions_dir.glob("*.yaml")): + raise FileNotFoundError( + f"No quality/*.yaml for project {chosen!r}: {paths.questions_dir}" + ) + return [paths] + + names = discover_quality_projects(repo_root=repo_root) + if not names: + raise FileNotFoundError( + "No ai-docs//quality/*.yaml packs found; " + "add questions or set REQUIREMENTS_PROJECT" + ) + return [resolve_project(name, repo_root=repo_root) for name in names] + + +def load_vocabulary(path: Path | None) -> str: + if path is None or not path.is_file(): + return "" + return path.read_text(encoding="utf-8").strip() + + +def add_project_args(parser: argparse.ArgumentParser) -> None: + parser.add_argument( + "--project", + help="Project folder under ai-docs/ (sets --root/--questions/--vocabulary)", + ) + parser.add_argument("--root", type=Path, help="Tree of requirements.yaml catalogs") + parser.add_argument( + "--questions", + type=Path, + help="Directory of quality question *.yaml files", + ) + parser.add_argument( + "--vocabulary", + type=Path, + help="Optional vocabulary markdown prepended to each Laya state", + ) + + +def paths_from_args(args: argparse.Namespace) -> ProjectPaths: + if getattr(args, "project", None): + paths = resolve_project(args.project) + root = Path(args.root) if getattr(args, "root", None) else paths.requirements_root + questions = ( + Path(args.questions) if getattr(args, "questions", None) else paths.questions_dir + ) + vocabulary = ( + Path(args.vocabulary) if getattr(args, "vocabulary", None) else paths.vocabulary_path + ) + return ProjectPaths( + name=paths.name, + requirements_root=root, + questions_dir=questions, + vocabulary_path=vocabulary, + ) + + root = getattr(args, "root", None) + questions = getattr(args, "questions", None) + if not root or not questions: + raise SystemExit("Pass --project NAME, or both --root and --questions") + + root = Path(root) + questions = Path(questions) + vocabulary = Path(args.vocabulary) if getattr(args, "vocabulary", None) else ( + questions / "vocabulary.md" + ) + return ProjectPaths( + name=None, + requirements_root=root, + questions_dir=questions, + vocabulary_path=vocabulary, + ) diff --git a/.agents/skills/manage-requirements/quality/requirements-schema.txt b/.agents/skills/manage-requirements/quality/requirements-schema.txt new file mode 100644 index 0000000000..5b9e0426eb --- /dev/null +++ b/.agents/skills/manage-requirements/quality/requirements-schema.txt @@ -0,0 +1,2 @@ +# Pinned deps for the schema/heuristic CI job (wheels only via --only-binary). +PyYAML==6.0.3 diff --git a/.agents/skills/manage-requirements/quality/requirements.txt b/.agents/skills/manage-requirements/quality/requirements.txt new file mode 100644 index 0000000000..4cc69d6646 --- /dev/null +++ b/.agents/skills/manage-requirements/quality/requirements.txt @@ -0,0 +1,5 @@ +# Optional local/CI extras for the Laya quality job. +# Schema/heuristic gates use requirements-schema.txt (pinned PyYAML). +PyYAML==6.0.3 +laya>=0.3.21,<0.4 +laya-evals>=0.1.0 diff --git a/.agents/skills/manage-requirements/quality/run_laya_eval.sh b/.agents/skills/manage-requirements/quality/run_laya_eval.sh new file mode 100755 index 0000000000..2dd5c6a39c --- /dev/null +++ b/.agents/skills/manage-requirements/quality/run_laya_eval.sh @@ -0,0 +1,33 @@ +#!/usr/bin/env bash +# Run requirement quality evals for a project (heuristic gate always; Laya when requested). +set -euo pipefail + +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +cd "$SCRIPT_DIR" + +PYTHON="${PYTHON:-python3}" +PROJECT="${REQUIREMENTS_PROJECT:-college-v2}" +SOURCE="${LAYA_SOURCE:-heuristic}" +REPORT_DIR="${REPORT_DIR:-$SCRIPT_DIR/report}" +mkdir -p "$REPORT_DIR" + +PROJECT_ARGS=(--project "$PROJECT") + +"$PYTHON" load_questions.py "${PROJECT_ARGS[@]}" +"$PYTHON" build_gold.py "${PROJECT_ARGS[@]}" +"$PYTHON" -m unittest test_quality_harness.py + +"$PYTHON" judge_catalog.py "${PROJECT_ARGS[@]}" --source heuristic --report "$REPORT_DIR/catalog_snapshot.json" + +if [[ "$SOURCE" != "laya" ]]; then + echo "Skipping Laya catalog eval (LAYA_SOURCE=$SOURCE)." + exit 0 +fi + +if ! "$PYTHON" -c "import laya" >/dev/null 2>&1; then + echo "Laya is not installed. pip install -r requirements.txt" >&2 + exit 1 +fi + +"$PYTHON" judge_catalog.py "${PROJECT_ARGS[@]}" --source laya --report "$REPORT_DIR/catalog_snapshot_laya.json" +"$PYTHON" judge_extra.py "${PROJECT_ARGS[@]}" --report "$REPORT_DIR/extra-answers.json" diff --git a/.agents/skills/manage-requirements/quality/test_quality_harness.py b/.agents/skills/manage-requirements/quality/test_quality_harness.py new file mode 100644 index 0000000000..7c2ab3b3a2 --- /dev/null +++ b/.agents/skills/manage-requirements/quality/test_quality_harness.py @@ -0,0 +1,453 @@ +from __future__ import annotations + +import json +import shutil +import sys +import tempfile +import unittest +from pathlib import Path + +import yaml + +DIR = Path(__file__).resolve().parent +sys.path.insert(0, str(DIR)) + +from extract_requirement_states import ( + iter_requirement_rows, + requirement_has_stub_ac, + requirement_line_numbers, +) +from judge_catalog import ( + format_failure_detail, + format_quality_report, + heuristic_answers, + report_quality_failures, + snapshot_from_rows, +) +from judge_extra import judge_extra_answers +from load_questions import ( + defect_criteria_from_questions, + is_global_qc, + load_question_documents, + noul_question_ids, + question_applies, + targeted_laya_by_requirement, + validate_questions, +) +from project import ProjectPaths, quality_packs_for_tests + + +def write_yaml(path: Path, data: dict) -> None: + path.parent.mkdir(parents=True, exist_ok=True) + path.write_text(yaml.safe_dump(data, sort_keys=False), encoding="utf-8") + + +def pack_questions(pack: ProjectPaths) -> list[dict]: + return load_question_documents(pack.questions_dir) + + +def pack_noul_ids(pack: ProjectPaths) -> tuple[str, ...]: + return noul_question_ids(pack_questions(pack)) + + +def install_global_questions( + questions_dir: Path, + pack: ProjectPaths, + extra: dict[str, dict] | None = None, +) -> None: + """Copy applies.all question YAML from the project quality pack into the fixture.""" + questions_dir.mkdir(parents=True, exist_ok=True) + copied = 0 + for question in pack_questions(pack): + if not is_global_qc(question): + continue + src = Path(question["path"]) + shutil.copy2(src, questions_dir / src.name) + copied += 1 + if copied == 0: + raise RuntimeError( + f"No global QC questions (applies.all) in {pack.questions_dir}" + ) + for name, payload in (extra or {}).items(): + write_yaml(questions_dir / f"{name}.yaml", payload) + + +def sample_catalog(*, stub: bool = False) -> dict: + then = "X" if stub else "The matcher writes matchMode Auto or Manual" + given = ( + "The feature is available in an active sample case" + if stub + else "A course with a resolved institution is match-eligible" + ) + when = "The behavior is exercised" if stub else "Matching runs" + return { + "specId": "sample-feature", + "kind": "component", + "title": "Sample feature", + "requirements": [ + { + "id": "FR-001", + "requirementType": "Functional", + "title": "X", + "statement": "Matching records a concrete matchMode.", + "priority": "High", + "status": "Draft", + "acceptanceCriteria": [ + {"given": given, "when": when, "then": then}, + ], + }, + { + "id": "FR-002", + "requirementType": "Functional", + "title": "Observable outcome", + "statement": "Save persists the chosen articulation type.", + "priority": "High", + "status": "Draft", + "acceptanceCriteria": [ + { + "given": "A course is open in evaluation", + "when": "The reviewer saves Direct transferable equivalency", + "then": "articulation.type is Direct and acronym is T-EQ", + } + ], + }, + ], + } + + +def build_fixture( + tmp: str | Path, + pack: ProjectPaths, + *, + stub: bool = False, + extra_questions: dict[str, dict] | None = None, +) -> tuple[Path, Path, list[dict]]: + root = Path(tmp) + req_root = root / "requirements" + questions_dir = root / "quality" + write_yaml( + req_root / "components" / "sample-feature" / "requirements.yaml", + sample_catalog(stub=stub), + ) + install_global_questions(questions_dir, pack, extra_questions) + rows = iter_requirement_rows(req_root, repo_root=root) + return req_root, questions_dir, rows + + +class ProjectPackTestCase(unittest.TestCase): + """Harness tests that need a project's quality/*.yaml as the question source.""" + + def quality_packs(self) -> list[ProjectPaths]: + return quality_packs_for_tests() + + +class ExtractTests(ProjectPackTestCase): + def test_iter_rows_from_catalog(self): + for pack in self.quality_packs(): + with self.subTest(project=pack.name): + with tempfile.TemporaryDirectory() as tmp: + _, _, rows = build_fixture(tmp, pack) + self.assertEqual(len(rows), 2) + self.assertEqual( + {row["id"] for row in rows}, + {"sample-feature#FR-001", "sample-feature#FR-002"}, + ) + self.assertTrue(all(row["sourceLine"] for row in rows)) + + def test_stub_ac_detection(self): + for pack in self.quality_packs(): + with self.subTest(project=pack.name): + with tempfile.TemporaryDirectory() as good_tmp, tempfile.TemporaryDirectory() as bad_tmp: + _, _, good_rows = build_fixture(good_tmp, pack, stub=False) + _, _, bad_rows = build_fixture(bad_tmp, pack, stub=True) + by_good = {row["id"]: row for row in good_rows} + by_bad = {row["id"]: row for row in bad_rows} + self.assertFalse(by_good["sample-feature#FR-001"]["stub_ac"]) + self.assertTrue(by_bad["sample-feature#FR-001"]["stub_ac"]) + self.assertTrue( + requirement_has_stub_ac( + { + "title": "X", + "acceptanceCriteria": [ + { + "given": "The feature is available in an active sample case", + "when": "The behavior is exercised", + "then": "X", + } + ], + } + ) + ) + + +class QualityGateTests(ProjectPackTestCase): + def test_heuristic_marks_stub_ac(self): + for pack in self.quality_packs(): + with self.subTest(project=pack.name): + rows = [ + {"id": "good#FR-001", "stub_ac": False}, + {"id": "bad#FR-001", "stub_ac": True}, + ] + answers = {row["id"]: heuristic_answers(row) for row in rows} + snap = snapshot_from_rows( + rows, answers, "heuristic", pack_noul_ids(pack) + ) + self.assertEqual(snap["failedIds"], ["bad#FR-001"]) + self.assertEqual(snap["lowConfidenceCount"], 0) + + def test_report_includes_rich_failure_location(self): + current = { + "requirementCount": 2, + "failedIds": ["sample-feature#FR-001", "other-feature#FR-001"], + "failures": [ + { + "id": "sample-feature#FR-001", + "requirementId": "FR-001", + "title": "Sample rows", + "sourcePath": "requirements/components/sample-feature/requirements.yaml", + "sourceLine": 12, + "reasons": ["testable", "verifiable"], + "primary_defect": "stub_ac", + }, + { + "id": "other-feature#FR-001", + "requirementId": "FR-001", + "title": "Other entry", + "sourcePath": "requirements/screens/other-feature/requirements.yaml", + "sourceLine": 10, + "reasons": ["testable", "verifiable"], + "primary_defect": "stub_ac", + }, + ], + "lowConfidenceCount": 0, + } + report = "\n".join(format_quality_report(current, color=False)) + self.assertIn("Requirements analyzed: 2", report) + self.assertIn("Passed: 0", report) + self.assertIn("Failed: 2", report) + self.assertIn( + "✖ requirements/components/sample-feature/requirements.yaml#12 FR-001: Sample rows [FAILED]", + report, + ) + self.assertIn("Failed checks: [testable, verifiable]", report) + self.assertIn("Reason: stub acceptance criteria", report) + + def test_clean_catalog_has_no_report(self): + current = { + "requirementCount": 1, + "failedIds": [], + "failures": [], + "lowConfidenceCount": 0, + } + self.assertEqual(report_quality_failures(current), []) + + def test_format_failure_detail_includes_quality_ids(self): + lines = format_failure_detail( + { + "id": "sample-feature#FR-002", + "requirementId": "FR-002", + "title": "Observable outcome", + "sourcePath": "requirements/components/sample-feature/requirements.yaml", + "sourceLine": 28, + "reasons": ["verifiable", "testable"], + "primary_defect": "stub_ac", + }, + color=False, + ) + self.assertEqual( + lines, + [ + "✖ requirements/components/sample-feature/requirements.yaml#28 FR-002: Observable outcome [FAILED]", + "Failed checks: [verifiable, testable]", + "Reason: stub acceptance criteria", + ], + ) + + def test_source_line_matches_yaml_id_entry(self): + text = ( + "requirements:\n" + " - id: FR-001\n" + " title: First\n" + " - id: FR-002\n" + " title: Second\n" + ) + lines = requirement_line_numbers(text) + self.assertEqual(lines["FR-001"], 2) + self.assertEqual(lines["FR-002"], 4) + + def test_fixture_stub_failures_appear_in_report(self): + for pack in self.quality_packs(): + with self.subTest(project=pack.name): + with tempfile.TemporaryDirectory() as tmp: + _, questions_dir, rows = build_fixture(tmp, pack, stub=True) + questions = load_question_documents(questions_dir) + answers = {row["id"]: heuristic_answers(row) for row in rows} + current = snapshot_from_rows( + rows, answers, "heuristic", noul_question_ids(questions) + ) + report = report_quality_failures( + current, defect_info=defect_criteria_from_questions(questions) + ) + self.assertTrue(report) + self.assertTrue( + any("stub acceptance criteria" in line for line in report) + ) + self.assertIn("sample-feature#FR-001", current["failedIds"]) + self.assertNotIn("sample-feature#FR-002", current["failedIds"]) + + +class QuestionYamlTests(ProjectPackTestCase): + def test_quality_packs_have_global_qc(self): + packs = self.quality_packs() + self.assertTrue(packs) + for pack in packs: + with self.subTest(project=pack.name): + questions = pack_questions(pack) + globals_ = [q for q in questions if is_global_qc(q)] + self.assertTrue(pack.questions_dir.is_dir()) + self.assertGreaterEqual(len(globals_), 1) + self.assertTrue(pack_noul_ids(pack)) + + def test_questions_validate_against_fixture_catalog(self): + for pack in self.quality_packs(): + with self.subTest(project=pack.name): + with tempfile.TemporaryDirectory() as tmp: + _, questions_dir, rows = build_fixture( + tmp, + pack, + extra_questions={ + "uses_match_mode": { + "id": "uses_match_mode", + "type": "check", + "applies": {"ids": ["sample-feature#FR-001"]}, + "checks": [{"type": "contains", "text": "matchMode"}], + }, + "save_writes_type": { + "id": "save_writes_type", + "type": "noul", + "applies": {"ids": ["sample-feature#FR-002"]}, + "instructions": "Does save write articulation.type?", + "expected": True, + }, + }, + ) + self.assertEqual(validate_questions(rows, questions_dir), []) + questions = load_question_documents(questions_dir) + extra = targeted_laya_by_requirement(rows, questions) + self.assertEqual(set(extra), {"sample-feature#FR-002"}) + self.assertIn("save_writes_type", extra["sample-feature#FR-002"]) + + def test_global_applies_to_every_requirement(self): + row = { + "id": "x#FR-001", + "specId": "x", + "kind": "script", + "requirementType": "NonFunctional", + "reqTags": [], + } + self.assertTrue(question_applies({"applies": {"all": True}}, row)) + self.assertFalse(question_applies({"applies": {"ids": ["other#FR-001"]}}, row)) + self.assertTrue(question_applies({"applies": {"kinds": ["script"]}}, row)) + self.assertFalse(question_applies({"applies": {"kinds": ["screen"]}}, row)) + + def test_unknown_applies_id_fails(self): + for pack in self.quality_packs(): + with self.subTest(project=pack.name): + with tempfile.TemporaryDirectory() as tmp: + _, questions_dir, rows = build_fixture(tmp, pack) + write_yaml( + questions_dir / "scoped.yaml", + { + "id": "scoped", + "type": "noul", + "applies": {"ids": ["sample-feature#FR-999"]}, + "instructions": "x", + "expected": True, + }, + ) + errors = validate_questions(rows, questions_dir) + self.assertTrue(any("FR-999" in err for err in errors)) + + def test_check_contains_missing_text_fails(self): + for pack in self.quality_packs(): + with self.subTest(project=pack.name): + with tempfile.TemporaryDirectory() as tmp: + _, questions_dir, rows = build_fixture(tmp, pack) + write_yaml( + questions_dir / "bad_check.yaml", + { + "id": "bad_check", + "type": "check", + "applies": {"ids": ["sample-feature#FR-001"]}, + "checks": [{"type": "contains", "text": "ace_id"}], + }, + ) + errors = validate_questions(rows, questions_dir) + self.assertTrue(any("expected to contain" in err for err in errors)) + + def test_extra_answer_mismatch(self): + errors = judge_extra_answers( + {"sample-feature#FR-002": {"save_writes_type": {"noul": 0.1}}}, + {"sample-feature#FR-002": {"save_writes_type": True}}, + ) + self.assertEqual(len(errors), 1) + + def test_extra_cannot_reuse_global_ids(self): + for pack in self.quality_packs(): + with self.subTest(project=pack.name): + reserved = pack_noul_ids(pack)[0] + errors = judge_extra_answers( + {"x#FR-001": {reserved: {"noul": 0.9}}}, + {"x#FR-001": {reserved: True}}, + reserved_ids={reserved}, + ) + self.assertTrue(any("collides" in err for err in errors)) + + +class GoldBuildTests(ProjectPackTestCase): + def test_build_gold_writes_extra_jsonl(self): + import build_gold + + for pack in self.quality_packs(): + with self.subTest(project=pack.name): + with tempfile.TemporaryDirectory() as tmp: + req_root, questions_dir, _ = build_fixture( + tmp, + pack, + extra_questions={ + "save_writes_type": { + "id": "save_writes_type", + "type": "noul", + "applies": {"ids": ["sample-feature#FR-002"]}, + "instructions": "Does save write articulation.type?", + "expected": True, + }, + }, + ) + extra = Path(tmp) / "extra.jsonl" + self.assertEqual( + build_gold.main( + [ + "--root", + str(req_root), + "--questions", + str(questions_dir), + "--extra-out", + str(extra), + ] + ), + 0, + ) + extra_lines = [ + json.loads(line) + for line in extra.read_text(encoding="utf-8").splitlines() + if line + ] + self.assertEqual(len(extra_lines), 1) + self.assertIn("save_writes_type", extra_lines[0]["questions"]) + for global_id in pack_noul_ids(pack): + self.assertNotIn(global_id, extra_lines[0]["questions"]) + + +if __name__ == "__main__": + unittest.main() diff --git a/.agents/skills/manage-requirements/schemas/requirement.schema.json b/.agents/skills/manage-requirements/schemas/requirement.schema.json new file mode 100644 index 0000000000..164d2e2245 --- /dev/null +++ b/.agents/skills/manage-requirements/schemas/requirement.schema.json @@ -0,0 +1,196 @@ +{ + "$schema": "https://json-schema.org/draft/2020-12/schema", + "$id": "requirement.schema.json", + "title": "Requirement", + "type": "object", + "required": [ + "id", + "requirementType", + "title", + "statement", + "priority", + "status" + ], + "properties": { + "id": { + "type": "string", + "pattern": "^(FR|NFR)-[0-9]{3,}$" + }, + "requirementType": { + "type": "string", + "enum": ["Functional", "NonFunctional"] + }, + "title": { + "type": "string", + "minLength": 3 + }, + "statement": { + "type": "string", + "minLength": 10 + }, + "description": { + "type": "string" + }, + "priority": { + "type": "string", + "enum": ["Critical", "High", "Medium", "Low"] + }, + "status": { + "type": "string", + "enum": [ + "Draft", + "Proposed", + "Approved", + "Implemented", + "Verified", + "Deprecated" + ] + }, + "source": { + "type": "string" + }, + "rationale": { + "type": "string" + }, + "owner": { + "type": "string" + }, + "tags": { + "type": "array", + "items": { + "type": "string" + } + }, + "category": { + "type": "string" + }, + "acceptanceCriteria": { + "type": "array", + "items": { + "$ref": "#/$defs/acceptanceCriterion" + } + }, + "metrics": { + "type": "array", + "items": { + "$ref": "#/$defs/metric" + } + }, + "verificationMethod": { + "type": "string", + "enum": ["Test", "Inspection", "Analysis", "Demonstration", "Audit"] + }, + "dependencies": { + "type": "array", + "items": { + "type": "string" + } + }, + "constraints": { + "type": "array", + "items": { + "type": "string" + } + }, + "assumptions": { + "type": "array", + "items": { + "type": "string" + } + }, + "traceability": { + "$ref": "#/$defs/traceability" + } + }, + "$defs": { + "acceptanceCriterion": { + "type": "object", + "required": ["given", "when", "then"], + "properties": { + "given": { + "type": "string" + }, + "when": { + "type": "string" + }, + "then": { + "type": "string" + } + } + }, + "metric": { + "type": "object", + "required": ["name", "operator", "value"], + "properties": { + "name": { + "type": "string" + }, + "operator": { + "type": "string", + "enum": ["<", "<=", "=", ">=", ">"] + }, + "value": { + "type": ["number", "string"] + }, + "unit": { + "type": "string" + } + } + }, + "traceability": { + "type": "object", + "properties": { + "businessObjectives": { + "type": "array", + "items": { + "type": "string" + } + }, + "epics": { + "type": "array", + "items": { + "type": "string" + } + }, + "features": { + "type": "array", + "items": { + "type": "string" + } + }, + "testCases": { + "type": "array", + "items": { + "type": "string" + } + } + } + } + }, + "allOf": [ + { + "if": { + "properties": { + "requirementType": { + "const": "Functional" + } + } + }, + "then": { + "required": ["acceptanceCriteria"] + } + }, + { + "if": { + "properties": { + "requirementType": { + "const": "NonFunctional" + } + } + }, + "then": { + "required": ["category", "metrics", "verificationMethod"] + } + } + ] +} diff --git a/.agents/skills/manage-requirements/schemas/requirements-catalog.schema.json b/.agents/skills/manage-requirements/schemas/requirements-catalog.schema.json new file mode 100644 index 0000000000..73214d09ad --- /dev/null +++ b/.agents/skills/manage-requirements/schemas/requirements-catalog.schema.json @@ -0,0 +1,47 @@ +{ + "$schema": "https://json-schema.org/draft/2020-12/schema", + "$id": "requirements-catalog.schema.json", + "title": "RequirementsCatalog", + "description": "Feature-level catalog of requirements for a reusable component, screen, or script. Cross-project; not tied to a single domain.", + "type": "object", + "required": ["specId", "kind", "title", "requirements"], + "additionalProperties": false, + "properties": { + "specId": { + "type": "string", + "minLength": 2, + "description": "Stable feature id, usually the folder basename (e.g. articulation-section)." + }, + "kind": { + "type": "string", + "enum": ["component", "screen", "script"] + }, + "title": { + "type": "string", + "minLength": 3 + }, + "description": { + "type": "string" + }, + "config": { + "type": "object", + "description": "Declarative knobs the feature exposes so requirements stay reusable (props, ports, section ids).", + "additionalProperties": true + }, + "dependsOn": { + "type": "array", + "items": { + "type": "string", + "minLength": 1 + }, + "description": "Other catalog specId values this feature composes (e.g. screens depending on components)." + }, + "requirements": { + "type": "array", + "minItems": 1, + "items": { + "$ref": "requirement.schema.json" + } + } + } +} diff --git a/.agents/skills/manage-requirements/scripts/__tests__/validate-requirements.test.js b/.agents/skills/manage-requirements/scripts/__tests__/validate-requirements.test.js new file mode 100644 index 0000000000..948ad890dc --- /dev/null +++ b/.agents/skills/manage-requirements/scripts/__tests__/validate-requirements.test.js @@ -0,0 +1,209 @@ +const assert = require('node:assert/strict'); +const fs = require('node:fs'); +const os = require('node:os'); +const path = require('node:path'); +const { test, beforeEach, afterEach } = require('node:test'); + +const { + collectAcceptanceQuality, + collectYamlFiles, + isStubCriterion, + main, + validateFiles, + validateMarkdownPair, +} = require('../validate-requirements'); + +const VALID_YAML = `specId: example-feature +kind: component +title: Example feature +description: Short summary of what this catalog covers. +config: + props: + - readonly +dependsOn: [] +requirements: + - id: FR-001 + requirementType: Functional + title: Example behavior + statement: The component must expose the configured behavior without hard-coding a specific screen. + priority: High + status: Draft + tags: [example] + acceptanceCriteria: + - given: The component is configured with readonly true + when: The user opens the feature + then: Editing controls are disabled +`; + +function writeCatalog(dir, { specId = 'example-feature', yaml = VALID_YAML, markdown = true } = {}) { + const featureDir = path.join(dir, specId); + fs.mkdirSync(featureDir, { recursive: true }); + fs.writeFileSync(path.join(featureDir, 'requirements.yaml'), yaml, 'utf8'); + if (markdown) { + fs.writeFileSync(path.join(featureDir, `${specId}.md`), `# ${specId}\n\nPurpose.\n`, 'utf8'); + } + return path.join(featureDir, 'requirements.yaml'); +} + +let tmpDir; + +beforeEach(() => { + tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'req-validate-')); +}); + +afterEach(() => { + fs.rmSync(tmpDir, { recursive: true, force: true }); +}); + +test('collectYamlFiles finds nested requirements.yaml', () => { + writeCatalog(tmpDir); + const files = collectYamlFiles(tmpDir); + assert.equal(files.length, 1); + assert.match(files[0], /example-feature\/requirements\.yaml$/); +}); + +test('valid catalog with companion markdown passes', () => { + const yamlPath = writeCatalog(tmpDir); + const [result] = validateFiles([yamlPath]); + assert.equal(result.ok, true); + assert.deepEqual(result.errors, []); + assert.deepEqual(result.warnings, []); +}); + +test('YAML parse error fails the catalog', () => { + const yamlPath = writeCatalog(tmpDir, { + yaml: 'specId: [this is: broken', + }); + const [result] = validateFiles([yamlPath]); + assert.equal(result.ok, false); + assert.match(result.errors[0], /YAML parse error/); +}); + +test('duplicate requirement ids fail', () => { + const yaml = VALID_YAML.replace( + 'then: Editing controls are disabled\n', + `then: Editing controls are disabled + - id: FR-001 + requirementType: Functional + title: Duplicate behavior + statement: Another statement that is long enough for the schema. + priority: High + status: Draft + acceptanceCriteria: + - given: The component is configured with readonly false + when: The user opens the feature + then: Editing controls remain enabled +` + ); + const yamlPath = writeCatalog(tmpDir, { yaml }); + const [result] = validateFiles([yamlPath]); + assert.equal(result.ok, false); + assert.ok(result.errors.some((err) => /duplicate id "FR-001"/.test(err))); +}); + +test('functional requirement without acceptanceCriteria fails schema', () => { + const yaml = `specId: example-feature +kind: component +title: Example feature +requirements: + - id: FR-001 + requirementType: Functional + title: Example behavior + statement: The component must expose the configured behavior without hard-coding a specific screen. + priority: High + status: Draft +`; + const yamlPath = writeCatalog(tmpDir, { yaml }); + const [result] = validateFiles([yamlPath]); + assert.equal(result.ok, false); + assert.ok(result.errors.some((err) => /required/.test(err))); +}); + +test('missing companion markdown fails', () => { + const yamlPath = writeCatalog(tmpDir, { markdown: false }); + const [result] = validateFiles([yamlPath]); + assert.equal(result.ok, false); + assert.ok(result.errors.some((err) => /missing companion markdown/.test(err))); + assert.ok(validateMarkdownPair(yamlPath, { specId: 'example-feature' }).length > 0); +}); + +test('README.md satisfies the markdown pair', () => { + const yamlPath = writeCatalog(tmpDir, { markdown: false }); + fs.writeFileSync(path.join(path.dirname(yamlPath), 'README.md'), '# Example\n', 'utf8'); + const [result] = validateFiles([yamlPath]); + assert.equal(result.ok, true); +}); + +test('short given/when/then warn instead of failing', () => { + const yaml = VALID_YAML.replace( + 'given: The component is configured with readonly true', + 'given: short' + ); + const yamlPath = writeCatalog(tmpDir, { yaml }); + const [result] = validateFiles([yamlPath]); + assert.equal(result.ok, true); + assert.ok(result.warnings.some((warn) => /given shorter than 8/.test(warn))); +}); + +test('empty given/when/then fail', () => { + const yaml = VALID_YAML.replace( + 'given: The component is configured with readonly true', + 'given: " "' + ); + const yamlPath = writeCatalog(tmpDir, { yaml }); + const [result] = validateFiles([yamlPath]); + assert.equal(result.ok, false); + assert.ok(result.errors.some((err) => /given must not be empty/.test(err))); +}); + +test('placeholder given/when is a stub AC warning, not a hard fail', () => { + const yaml = VALID_YAML.replace( + ` - given: The component is configured with readonly true + when: The user opens the feature + then: Editing controls are disabled +`, + ` - given: The feature is available in an active CollegeV2 case + when: The behavior is exercised + then: Editing controls are disabled +` + ); + const yamlPath = writeCatalog(tmpDir, { yaml }); + const [result] = validateFiles([yamlPath]); + assert.equal(result.ok, true); + assert.ok(result.warnings.some((warn) => /stub acceptance criterion/.test(warn))); +}); + +test('then that repeats the title is a stub AC', () => { + assert.equal( + isStubCriterion( + { title: 'Hide student search in Web Entry' }, + { given: 'A screen is open today', when: 'The reviewer continues', then: 'Hide student search in Web Entry' } + ), + true + ); + const quality = collectAcceptanceQuality({ + requirements: [ + { + id: 'FR-001', + title: 'Hide student search in Web Entry', + acceptanceCriteria: [ + { + given: 'A screen is open today', + when: 'The reviewer continues', + then: 'Hide student search in Web Entry', + }, + ], + }, + ], + }); + assert.equal(quality.errors.length, 0); + assert.equal(quality.warnings.length, 1); +}); + +test('CLI main returns 0 for a valid tree and 1 when markdown is missing', () => { + writeCatalog(tmpDir); + assert.equal(main([tmpDir]), 0); + const brokenDir = path.join(tmpDir, 'broken'); + writeCatalog(brokenDir, { specId: 'missing-md', markdown: false }); + assert.equal(main([brokenDir]), 1); +}); diff --git a/.agents/skills/manage-requirements/scripts/check-requirements.js b/.agents/skills/manage-requirements/scripts/check-requirements.js new file mode 100644 index 0000000000..3f18b65bf6 --- /dev/null +++ b/.agents/skills/manage-requirements/scripts/check-requirements.js @@ -0,0 +1,154 @@ +#!/usr/bin/env node +/** + * Run schema validation + heuristic quality judging for requirement catalogs. + * + * Usage: + * node check-requirements.js + * node check-requirements.js college-v2 + * node check-requirements.js --project military + * + * With no project arg, checks every ai-docs//requirements tree. + * Project may also come from REQUIREMENTS_PROJECT when no CLI project is given. + */ + +const { spawnSync } = require('child_process'); +const fs = require('fs'); +const path = require('path'); + +const SKILL_ROOT = path.resolve(__dirname, '..'); +const REPO_ROOT = path.resolve(SKILL_ROOT, '../../..'); +const AI_DOCS = path.join(REPO_ROOT, 'ai-docs'); +const VALIDATE_JS = path.join(__dirname, 'validate-requirements.js'); +const JUDGE_PY = path.join(SKILL_ROOT, 'quality', 'judge_catalog.py'); + +const colors = { + reset: '\x1b[0m', + red: '\x1b[31m', + green: '\x1b[32m', + yellow: '\x1b[33m', + cyan: '\x1b[36m', +}; + +function log(message, color = 'reset') { + console.log(`${colors[color]}${message}${colors.reset}`); +} + +function parseArgs(argv) { + let project = null; + const rest = []; + for (let i = 0; i < argv.length; i += 1) { + const arg = argv[i]; + if (arg === '--project' || arg === '-p') { + project = argv[i + 1] || null; + i += 1; + continue; + } + if (arg.startsWith('--project=')) { + project = arg.slice('--project='.length) || null; + continue; + } + if (arg.startsWith('-')) { + rest.push(arg); + continue; + } + if (project === null) { + project = arg; + continue; + } + rest.push(arg); + } + if (!project && process.env.REQUIREMENTS_PROJECT) { + project = process.env.REQUIREMENTS_PROJECT; + } + return { project, rest }; +} + +function listRequirementProjects() { + if (!fs.existsSync(AI_DOCS)) { + return []; + } + return fs + .readdirSync(AI_DOCS, { withFileTypes: true }) + .filter((entry) => entry.isDirectory()) + .map((entry) => entry.name) + .filter((name) => fs.existsSync(path.join(AI_DOCS, name, 'requirements'))) + .sort(); +} + +function hasQualityFixtures(project) { + const qualityDir = path.join(AI_DOCS, project, 'quality'); + if (!fs.existsSync(qualityDir)) { + return false; + } + return fs.readdirSync(qualityDir).some((name) => name.endsWith('.yaml')); +} + +function run(command, args, options = {}) { + const result = spawnSync(command, args, { + cwd: options.cwd || REPO_ROOT, + env: { ...process.env, FORCE_COLOR: process.env.FORCE_COLOR || '1' }, + stdio: 'inherit', + }); + if (result.error) { + log(result.error.message, 'red'); + return 1; + } + return typeof result.status === 'number' ? result.status : 1; +} + +function main(argv = process.argv.slice(2)) { + const { project } = parseArgs(argv); + const projects = project ? [project] : listRequirementProjects(); + + if (projects.length === 0) { + log('No ai-docs/*/requirements trees found.', 'red'); + return 1; + } + + if (project) { + const reqRoot = path.join(AI_DOCS, project, 'requirements'); + if (!fs.existsSync(reqRoot)) { + log(`Requirements tree missing: ${path.relative(REPO_ROOT, reqRoot)}`, 'red'); + return 1; + } + } + + const schemaTargets = projects.map((name) => path.join(AI_DOCS, name, 'requirements')); + log(`Schema: validating ${projects.join(', ')}...`, 'cyan'); + const schemaStatus = run(process.execPath, [VALIDATE_JS, ...schemaTargets]); + if (schemaStatus !== 0) { + return schemaStatus; + } + + let qualityFailed = 0; + for (const name of projects) { + if (!hasQualityFixtures(name)) { + log(`Quality: skip ${name} (no ai-docs/${name}/quality/*.yaml)`, 'yellow'); + continue; + } + log(`Quality: judging ${name} (heuristic)...`, 'cyan'); + const status = run('python3', [JUDGE_PY, '--project', name, '--source', 'heuristic']); + if (status !== 0) { + qualityFailed += 1; + } + } + + if (qualityFailed > 0) { + log(`${qualityFailed} project quality gate(s) failed.`, 'red'); + return 1; + } + + log('Schema and quality checks passed.', 'green'); + return 0; +} + +module.exports = { + hasQualityFixtures, + listRequirementProjects, + main, + parseArgs, +}; + +if (require.main === module) { + process.exit(main()); +} diff --git a/.agents/skills/manage-requirements/scripts/validate-requirements.js b/.agents/skills/manage-requirements/scripts/validate-requirements.js new file mode 100644 index 0000000000..692dcb927b --- /dev/null +++ b/.agents/skills/manage-requirements/scripts/validate-requirements.js @@ -0,0 +1,390 @@ +#!/usr/bin/env node +/** + * Validate requirements.yaml catalogs against the manage-requirements schemas. + * + * Usage: + * node .agents/skills/manage-requirements/scripts/validate-requirements.js [path...] + * + * Paths may be a requirements.yaml file, a feature directory, or a tree root. + * Default: every ai-docs//requirements tree that exists. + */ + +const fs = require('fs'); +const path = require('path'); +const yaml = require('js-yaml'); +const Ajv = require('ajv/dist/2020'); +const addFormats = require('ajv-formats'); + +const SKILL_ROOT = path.resolve(__dirname, '..'); +const SCHEMA_DIR = path.join(SKILL_ROOT, 'schemas'); +const REPO_ROOT = path.resolve(SKILL_ROOT, '../../..'); +const AI_DOCS = path.join(REPO_ROOT, 'ai-docs'); +const AC_FIELD_MIN_LENGTH = 8; + +function defaultTargets() { + if (!fs.existsSync(AI_DOCS)) { + return []; + } + return fs + .readdirSync(AI_DOCS, { withFileTypes: true }) + .filter((entry) => entry.isDirectory()) + .map((entry) => path.join(AI_DOCS, entry.name, 'requirements')) + .filter((dir) => fs.existsSync(dir)) + .sort(); +} + +const DEFAULT_TARGET = path.join(REPO_ROOT, 'ai-docs/college-v2/requirements'); +const PLACEHOLDER_GIVEN = /the feature is available/i; +const PLACEHOLDER_WHEN = /the behavior is exercised/i; + +const colors = { + reset: '\x1b[0m', + red: '\x1b[31m', + green: '\x1b[32m', + yellow: '\x1b[33m', + cyan: '\x1b[36m', + bright: '\x1b[1m', +}; + +function log(message, color = 'reset') { + console.log(`${colors[color]}${message}${colors.reset}`); +} + +function loadJson(filePath) { + return JSON.parse(fs.readFileSync(filePath, 'utf8')); +} + +function createAjv() { + const ajv = new Ajv({ + allErrors: true, + strict: false, + validateSchema: true, + }); + addFormats(ajv); + + const requirementSchema = loadJson(path.join(SCHEMA_DIR, 'requirement.schema.json')); + const catalogSchema = loadJson(path.join(SCHEMA_DIR, 'requirements-catalog.schema.json')); + + ajv.addSchema(requirementSchema); + ajv.addSchema(catalogSchema); + + return { + ajv, + validateCatalog: ajv.getSchema('requirements-catalog.schema.json'), + }; +} + +function collectYamlFiles(targetPath) { + const abs = path.resolve(targetPath); + if (!fs.existsSync(abs)) { + throw new Error(`Path not found: ${abs}`); + } + + const stat = fs.statSync(abs); + if (stat.isFile()) { + if (!abs.endsWith('.yaml') && !abs.endsWith('.yml')) { + throw new Error(`Not a YAML file: ${abs}`); + } + return [abs]; + } + + if (stat.isDirectory()) { + const direct = path.join(abs, 'requirements.yaml'); + if (fs.existsSync(direct)) { + return [direct]; + } + + const found = []; + const walk = (dir) => { + for (const entry of fs.readdirSync(dir, { withFileTypes: true })) { + const full = path.join(dir, entry.name); + if (entry.isDirectory()) { + walk(full); + } else if (entry.name === 'requirements.yaml' || entry.name === 'requirements.yml') { + found.push(full); + } + } + }; + walk(abs); + return found.sort(); + } + + throw new Error(`Unsupported path: ${abs}`); +} + +function formatAjvErrors(errors) { + if (!errors || errors.length === 0) { + return ['Unknown validation error']; + } + return errors.map((err) => { + const instance = err.instancePath || '/'; + return `${instance} ${err.message}${err.params ? ` ${JSON.stringify(err.params)}` : ''}`; + }); +} + +function normalizeComparable(value) { + return String(value || '') + .toLowerCase() + .replace(/\s+/g, ' ') + .trim(); +} + +function companionMarkdownPath(filePath, specId) { + const dir = path.dirname(filePath); + const candidates = []; + if (typeof specId === 'string' && specId.length > 0) { + candidates.push(path.join(dir, `${specId}.md`)); + } + candidates.push(path.join(dir, 'README.md')); + return candidates.find((candidate) => fs.existsSync(candidate)) || null; +} + +function validateMarkdownPair(filePath, data) { + const specId = data && typeof data.specId === 'string' ? data.specId : null; + if (companionMarkdownPath(filePath, specId)) { + return []; + } + const expected = specId ? `${specId}.md or README.md` : '.md or README.md'; + return [`missing companion markdown (${expected}) in ${path.dirname(filePath)}`]; +} + +function isStubCriterion(req, criterion) { + const given = String(criterion.given || ''); + const when = String(criterion.when || ''); + const then = normalizeComparable(criterion.then); + const title = normalizeComparable(req.title); + const placeholderPair = PLACEHOLDER_GIVEN.test(given) && PLACEHOLDER_WHEN.test(when); + const thenRepeatsTitle = then.length > 0 && then === title; + return placeholderPair || thenRepeatsTitle; +} + +/** @deprecated Use isStubCriterion */ +function isTautologicalCriterion(req, criterion) { + return isStubCriterion(req, criterion); +} + +function collectAcceptanceQuality(data) { + const errors = []; + const warnings = []; + if (!data || !Array.isArray(data.requirements)) { + return { errors, warnings }; + } + + data.requirements.forEach((req, reqIndex) => { + if (!req || !Array.isArray(req.acceptanceCriteria)) { + return; + } + req.acceptanceCriteria.forEach((criterion, acIndex) => { + if (!criterion || typeof criterion !== 'object') { + return; + } + ['given', 'when', 'then'].forEach((field) => { + const value = typeof criterion[field] === 'string' ? criterion[field].trim() : ''; + const instance = `/requirements/${reqIndex}/acceptanceCriteria/${acIndex}/${field}`; + if (value.length === 0) { + errors.push(`${instance} must not be empty`); + } else if (value.length < AC_FIELD_MIN_LENGTH) { + warnings.push( + `${instance} shorter than ${AC_FIELD_MIN_LENGTH} characters` + ); + } + }); + if (isStubCriterion(req, criterion)) { + const id = typeof req.id === 'string' ? req.id : `#${reqIndex}`; + warnings.push( + `/requirements/${reqIndex} ${id} stub acceptance criterion (placeholder given/when or then repeats title)` + ); + } + }); + }); + + return { errors, warnings }; +} + +function validateCatalogDocument(filePath, data, validateCatalog) { + const errors = []; + + if (!validateCatalog(data)) { + for (const msg of formatAjvErrors(validateCatalog.errors)) { + errors.push(msg); + } + } + + if (data && Array.isArray(data.requirements)) { + const seen = new Set(); + data.requirements.forEach((req, index) => { + if (!req || typeof req.id !== 'string') { + return; + } + if (seen.has(req.id)) { + errors.push(`/requirements/${index}/id duplicate id "${req.id}" within catalog`); + } + seen.add(req.id); + }); + } + + return errors; +} + +function indexSpecIds(yamlFiles) { + const bySpecId = new Map(); + for (const file of yamlFiles) { + try { + const data = yaml.load(fs.readFileSync(file, 'utf8')); + if (data && typeof data.specId === 'string') { + bySpecId.set(data.specId, file); + } + } catch { + // parse errors reported during validation + } + } + return bySpecId; +} + +function validateDependsOn(data, filePath, specIndex) { + const errors = []; + if (!data || !Array.isArray(data.dependsOn)) { + return errors; + } + for (const dep of data.dependsOn) { + if (!specIndex.has(dep)) { + errors.push( + `dependsOn "${dep}" not found among catalogs in this validation run (referenced from ${path.relative(REPO_ROOT, filePath)})` + ); + } + } + return errors; +} + +function validateFiles(files, options = {}) { + const { validateCatalog } = options.validateCatalog + ? { validateCatalog: options.validateCatalog } + : createAjv(); + if (!validateCatalog) { + throw new Error('Failed to compile requirements-catalog.schema.json'); + } + + const uniqueFiles = [...new Set(files)]; + const specIndex = indexSpecIds(uniqueFiles); + const results = []; + + for (const file of uniqueFiles) { + let data; + try { + data = yaml.load(fs.readFileSync(file, 'utf8')); + } catch (err) { + results.push({ + file, + ok: false, + errors: [`YAML parse error: ${err.message}`], + warnings: [], + data: null, + }); + continue; + } + + const quality = collectAcceptanceQuality(data); + const errors = [ + ...validateCatalogDocument(file, data, validateCatalog), + ...validateMarkdownPair(file, data), + ...validateDependsOn(data, file, specIndex), + ...quality.errors, + ]; + + results.push({ + file, + ok: errors.length === 0, + errors, + warnings: quality.warnings, + data, + }); + } + + return results; +} + +function main(argv = process.argv.slice(2)) { + const targets = argv.length > 0 ? argv : defaultTargets(); + + if (targets.length === 0) { + log('No ai-docs/*/requirements trees found.', 'red'); + log('Pass an explicit path to a requirements.yaml, feature dir, or tree.', 'yellow'); + return 1; + } + + let files = []; + try { + for (const target of targets) { + files = files.concat(collectYamlFiles(target)); + } + } catch (err) { + log(err.message, 'red'); + return 1; + } + + files = [...new Set(files)]; + + if (files.length === 0) { + log('No requirements.yaml files found.', 'yellow'); + return 1; + } + + log(`Validating ${files.length} catalog(s)...`, 'cyan'); + const results = validateFiles(files); + let failed = 0; + let warningCount = 0; + + for (const result of results) { + const rel = path.relative(REPO_ROOT, result.file) || result.file; + if (!result.ok) { + failed += 1; + log(`FAIL ${rel}`, 'red'); + for (const err of result.errors) { + log(` ${err}`, 'red'); + } + } else { + const count = result.data && Array.isArray(result.data.requirements) + ? result.data.requirements.length + : 0; + const specId = result.data && result.data.specId ? result.data.specId : 'unknown'; + log(`OK ${rel} (${specId}, ${count} requirements)`, 'green'); + } + for (const warning of result.warnings) { + warningCount += 1; + log(` WARN ${warning}`, 'yellow'); + } + } + + console.log(''); + if (failed > 0) { + log(`${failed} of ${results.length} catalog(s) failed.`, 'red'); + return 1; + } + + log(`All ${results.length} catalog(s) passed.`, 'green'); + if (warningCount > 0) { + log(`${warningCount} acceptance-criterion warning(s).`, 'yellow'); + } + return 0; +} + +module.exports = { + AC_FIELD_MIN_LENGTH, + DEFAULT_TARGET, + REPO_ROOT, + collectAcceptanceQuality, + collectYamlFiles, + companionMarkdownPath, + createAjv, + isStubCriterion, + isTautologicalCriterion, + main, + validateCatalogDocument, + validateDependsOn, + validateFiles, + validateMarkdownPair, +}; + +if (require.main === module) { + process.exit(main()); +} diff --git a/.agents/skills/manage-requirements/templates/feature.md.example b/.agents/skills/manage-requirements/templates/feature.md.example new file mode 100644 index 0000000000..2d9d02915e --- /dev/null +++ b/.agents/skills/manage-requirements/templates/feature.md.example @@ -0,0 +1,22 @@ +# Example feature + +Narrative for humans and agents. Do not duplicate every FR statement here. + +## Purpose + +What this feature does and who uses it. + +## Kind and reuse + +- `kind`: component | screen | script +- Configurable via `config` in `requirements.yaml` +- Screens compose components through `dependsOn` + +## Related assets + +- Vue / BPMN / script paths as applicable +- Legacy design or edge-case docs during migration + +## Configuration + +Describe how consumers configure this feature (props, ports, section order). diff --git a/.agents/skills/manage-requirements/templates/requirements.yaml.example b/.agents/skills/manage-requirements/templates/requirements.yaml.example new file mode 100644 index 0000000000..1f6cd435f0 --- /dev/null +++ b/.agents/skills/manage-requirements/templates/requirements.yaml.example @@ -0,0 +1,23 @@ +specId: example-feature +kind: component +title: Example feature +description: Short summary of what this catalog covers. +config: + props: + - readonly + - allowedActions +dependsOn: [] +requirements: + - id: FR-001 + requirementType: Functional + title: Example behavior + statement: The component must expose the configured behavior without hard-coding a specific screen. + priority: High + status: Draft + tags: [example, reusable] + acceptanceCriteria: + - given: The component is configured with readonly true + when: The user opens the feature + then: Editing controls are disabled + traceability: + testCases: [] diff --git a/.env.example b/.env.example index c451173583..db64cc3770 100644 --- a/.env.example +++ b/.env.example @@ -35,6 +35,8 @@ REDIS_PREFIX= HORIZON_PREFIX=horizon: PM4_HORIZON_SUPERVISOR_BPMN_MAX_JOBS=10 PM4_HORIZON_SUPERVISOR_1_MAX_JOBS=100 +# Local-directory plugin installs (artisan plugin:install); API always requires https:// URLs +PLUGIN_INSTALL_ALLOW_LOCAL_PATH=false DOCKER_SHARED_MEMORY=256m # Comma-separated load balancer / proxy IPs only (do not use * in production) PROXIES= diff --git a/ProcessMaker/Assets/DataSourcesInProcess.php b/ProcessMaker/Assets/DataSourcesInProcess.php index 0a44452214..2a45c90be3 100644 --- a/ProcessMaker/Assets/DataSourcesInProcess.php +++ b/ProcessMaker/Assets/DataSourcesInProcess.php @@ -25,7 +25,7 @@ public function referencesToExport(Process $process, array $dataSources = []) // Data Sources used in BPMN Service Tasks $xpath = new DOMXPath($process->getDefinitions()); $xpath->registerNamespace('pm', WorkflowServiceProvider::PROCESS_MAKER_NS); - + // Find all nodes with pm:config $nodes = $xpath->query("//*[@pm:config!='']"); foreach ($nodes as $node) { diff --git a/ProcessMaker/Assets/DataSourcesInScreen.php b/ProcessMaker/Assets/DataSourcesInScreen.php index 4a7494d74f..c10e9b8c9e 100644 --- a/ProcessMaker/Assets/DataSourcesInScreen.php +++ b/ProcessMaker/Assets/DataSourcesInScreen.php @@ -2,9 +2,9 @@ namespace ProcessMaker\Assets; +use Illuminate\Support\Arr; use ProcessMaker\Managers\ExportManager; use ProcessMaker\Models\Screen; -use Illuminate\Support\Arr; class DataSourcesInScreen { diff --git a/ProcessMaker/Console/Commands/ChangeKeyEncryptedData.php b/ProcessMaker/Console/Commands/ChangeKeyEncryptedData.php index feab536017..4804129199 100644 --- a/ProcessMaker/Console/Commands/ChangeKeyEncryptedData.php +++ b/ProcessMaker/Console/Commands/ChangeKeyEncryptedData.php @@ -4,8 +4,8 @@ use Exception; use Illuminate\Console\Command; -use ProcessMaker\Facades\EncryptedData; use Illuminate\Support\Facades\Log; +use ProcessMaker\Facades\EncryptedData; class ChangeKeyEncryptedData extends Command { diff --git a/ProcessMaker/Console/Commands/CreateSamlCertificate.php b/ProcessMaker/Console/Commands/CreateSamlCertificate.php index edfc5c34e9..99b0bf5433 100644 --- a/ProcessMaker/Console/Commands/CreateSamlCertificate.php +++ b/ProcessMaker/Console/Commands/CreateSamlCertificate.php @@ -16,7 +16,7 @@ class CreateSamlCertificate extends CreateCertificate {--keyname=key.pem : Full name of the certificate key file} {--certname=cert.pem : Full name to the certificate file} {--subject= : Set subject of request or cert ' - . '(e.g. /C=US/ST=New York/L=New York City/O=Example Inc/CN=example.com)}'; + . '(e.g. /C=US/ST=New York/L=New York City/O=Example Inc/CN=example.com)}'; /** * The console command description. diff --git a/ProcessMaker/Console/Commands/PluginInstallCommand.php b/ProcessMaker/Console/Commands/PluginInstallCommand.php new file mode 100644 index 0000000000..29eb3f58dc --- /dev/null +++ b/ProcessMaker/Console/Commands/PluginInstallCommand.php @@ -0,0 +1,81 @@ +argument('repo'); + $branch = $this->option('branch'); + $tag = $this->option('tag'); + + // Set up event listener to output log messages + $listener = function (PluginLog $event) { + $message = $event->message; + if ($event->pluginName) { + $message = "[{$event->pluginName}] {$message}"; + } + + switch ($event->status) { + case 'done': + $this->info($message); + break; + case 'error': + $this->error($message); + break; + case 'running': + default: + $this->line($message); + break; + } + + // Stop listening if we get a 'done' or 'error' status + if (in_array($event->status, ['done', 'error'])) { + Event::forget(PluginLog::class); + } + }; + + Event::listen(PluginLog::class, $listener); + + try { + $manager = new PluginManager(); + $manager->install($repo, $branch, $tag); + + return Command::SUCCESS; + } catch (RuntimeException $e) { + $this->error('Installation failed: ' . $e->getMessage()); + event(new PluginLog('Installation failed: ' . $e->getMessage(), 'error')); + + return Command::FAILURE; + } finally { + // Clean up listener + Event::forget(PluginLog::class); + } + } +} diff --git a/ProcessMaker/Console/Commands/PluginListCommand.php b/ProcessMaker/Console/Commands/PluginListCommand.php new file mode 100644 index 0000000000..5e7d43bde5 --- /dev/null +++ b/ProcessMaker/Console/Commands/PluginListCommand.php @@ -0,0 +1,55 @@ +list(); + + if (empty($plugins)) { + $this->info('No plugins installed.'); + + return Command::SUCCESS; + } + + $headers = ['Name', 'Description', 'Branch/Tag']; + $rows = []; + + foreach ($plugins as $plugin) { + $rows[] = [ + $plugin['name'], + $plugin['description'], + $plugin['branch'] ?? 'N/A', + ]; + } + + $this->table($headers, $rows); + + return Command::SUCCESS; + } +} diff --git a/ProcessMaker/Console/Commands/PluginPublishTenantAssets.php b/ProcessMaker/Console/Commands/PluginPublishTenantAssets.php new file mode 100644 index 0000000000..a85db53648 --- /dev/null +++ b/ProcessMaker/Console/Commands/PluginPublishTenantAssets.php @@ -0,0 +1,73 @@ +error('Multitenancy is disabled. This command only applies when MULTITENANCY=true.'); + + return self::FAILURE; + } + + $tenant = app()->bound('currentTenant') ? app('currentTenant') : null; + if (!$tenant || !isset($tenant->id)) { + $this->error('This command must be run within a tenant context (TENANT= or tenants:artisan).'); + + return self::FAILURE; + } + + $pluginFolder = $this->argument('pluginFolder'); + + if ($pluginFolder === null) { + $this->info('Publishing plugin assets for tenant ' . $tenant->id . '...'); + $results = $publisher->publishAllInstalledPlugins((int) $tenant->id); + + if ($results === []) { + $this->warn('No plugin directories found under ' . storage_path('plugins')); + + return self::SUCCESS; + } + + foreach ($results as $folder => $published) { + $this->line( + $folder . ': ' . ($published ? 'published' : 'skipped (no public/)') + ); + } + + return self::SUCCESS; + } + + $pluginPath = storage_path('plugins/' . $pluginFolder); + if (!is_dir($pluginPath)) { + $this->error('Plugin not found: ' . $pluginPath); + + return self::FAILURE; + } + + $published = $publisher->publishPlugin($pluginPath, (int) $tenant->id); + + if ($published) { + $this->info('Published assets for plugin ' . $pluginFolder . ' (tenant ' . $tenant->id . ').'); + } else { + $this->warn('Plugin has no public/ directory; nothing copied.'); + } + + return self::SUCCESS; + } +} diff --git a/ProcessMaker/Console/Commands/PluginReloadListenCommand.php b/ProcessMaker/Console/Commands/PluginReloadListenCommand.php new file mode 100644 index 0000000000..a33720cd3f --- /dev/null +++ b/ProcessMaker/Console/Commands/PluginReloadListenCommand.php @@ -0,0 +1,122 @@ +shouldListen($cycle)) { + $cycle++; + + try { + $this->catchUp($reload); + $this->disableReadTimeout(); + $this->info('Listening for plugin runtime reloads on [' . $reload->channel() . '].'); + $this->subscribe($reload); + $this->warn('Plugin reload subscription ended. Reconnecting.'); + } catch (Throwable $exception) { + $this->warn( + 'Plugin reload listener lost Redis: ' . $exception->getMessage() . ' Reconnecting.' + ); + } + + if (!$this->shouldListen($cycle)) { + break; + } + + $this->releaseRedis(); + $this->pauseBeforeReconnect(); + } + + return self::SUCCESS; + } + + private function shouldListen(int $cycle): bool + { + $maxCycles = (int) config('plugin_manager.reload_listen_max_cycles', 0); + + return $maxCycles === 0 || $cycle < $maxCycles; + } + + /** + * PhpRedis treats a read timeout of 0 as PHP's default_socket_timeout. + * A subscribe with no messages then dies with "read error on connection". + */ + private function disableReadTimeout(): void + { + ini_set('default_socket_timeout', '-1'); + + $client = Redis::connection()->client(); + if (is_object($client) && method_exists($client, 'setOption')) { + $client->setOption(\Redis::OPT_READ_TIMEOUT, -1); + } + } + + private function catchUp(PluginRuntimeReload $reload): void + { + $remoteVersion = $reload->remoteVersion(); + if ($remoteVersion > $reload->appliedVersion()) { + $this->applyReload($reload, $remoteVersion); + } + } + + private function subscribe(PluginRuntimeReload $reload): void + { + Redis::subscribe([$reload->channel()], function (string $message) use ($reload): void { + $version = $reload->versionFromMessage($message); + if ($version <= $reload->appliedVersion()) { + return; + } + + $this->applyReload($reload, $version); + }); + } + + private function releaseRedis(): void + { + try { + Redis::purge('default'); + } catch (Throwable) { + // The broken connection is discarded on the next attempt. + } + } + + private function pauseBeforeReconnect(): void + { + $seconds = max(0, (int) config('plugin_manager.reload_reconnect_delay', 1)); + if ($seconds > 0) { + sleep($seconds); + } + } + + private function applyReload(PluginRuntimeReload $reload, int $version): bool + { + if ($version > 0 && !$reload->rememberApplied($version)) { + $this->error('Could not record the applied plugin reload version.'); + + return false; + } + + if (!$reload->scheduleSupervisorRestart()) { + $this->error('Could not schedule the local supervisor restart.'); + + return false; + } + + $this->info('Scheduled supervisorctl restart on this pod.'); + + return true; + } +} diff --git a/ProcessMaker/Console/Commands/PluginUninstallCommand.php b/ProcessMaker/Console/Commands/PluginUninstallCommand.php new file mode 100644 index 0000000000..4b3038a7f6 --- /dev/null +++ b/ProcessMaker/Console/Commands/PluginUninstallCommand.php @@ -0,0 +1,79 @@ +argument('name'); + + // Set up event listener to output log messages + $listener = function (PluginLog $event) { + $message = $event->message; + if ($event->pluginName) { + $message = "[{$event->pluginName}] {$message}"; + } + + switch ($event->status) { + case 'done': + $this->info($message); + break; + case 'error': + $this->error($message); + break; + case 'running': + default: + $this->line($message); + break; + } + + // Stop listening if we get a 'done' or 'error' status + if (in_array($event->status, ['done', 'error'])) { + Event::forget(PluginLog::class); + } + }; + + Event::listen(PluginLog::class, $listener); + + try { + $manager = new PluginManager(); + $manager->uninstall($name); + + return Command::SUCCESS; + } catch (RuntimeException $e) { + $this->error('Uninstallation failed: ' . $e->getMessage()); + event(new PluginLog('Uninstallation failed: ' . $e->getMessage(), 'error', $name)); + + return Command::FAILURE; + } finally { + // Clean up listener + Event::forget(PluginLog::class); + } + } +} diff --git a/ProcessMaker/Console/Commands/TransitionExecutors.php b/ProcessMaker/Console/Commands/TransitionExecutors.php index c235d528aa..dd8eb386a4 100644 --- a/ProcessMaker/Console/Commands/TransitionExecutors.php +++ b/ProcessMaker/Console/Commands/TransitionExecutors.php @@ -53,7 +53,7 @@ public function handle(): int // Get the optional timeout $timeout = $this->option('timeout') ?? 300; - if ((int)$timeout <= 60) { + if ((int) $timeout <= 60) { $this->error('Timeout must be a greater or equal to 60'); return 1; @@ -88,14 +88,14 @@ public function handle(): int $response = $this->scriptMicroserviceService->updateCustomExecutor($executors[$index]); Log::debug('Response', ['response' => $response]); - $status = strtolower((string)($response['status'] ?? '')); + $status = strtolower((string) ($response['status'] ?? '')); // Send subscription message (Pusher protocol example) $client->send(json_encode([ 'event' => 'pusher:subscribe', 'data' => [ - 'channel' => "build-image-" . $executors[$index]->uuid - ] + 'channel' => 'build-image-' . $executors[$index]->uuid, + ], ])); $running = true; @@ -123,7 +123,6 @@ public function handle(): int if (!$error) { $this->info("Executor {$executors[$index]->uuid} transitioned successfully." . PHP_EOL); } - } catch (RequestException $e) { $this->error("Request failed for executor {$executors[$index]->uuid}"); $this->line(PHP_EOL); @@ -136,47 +135,45 @@ public function handle(): int $client->close(); $index++; - } while ($index < $total); } - return 0; } private function getExecutors($uuid): Collection { $query = ScriptExecutor::query() - ->where("is_system", 0) + ->where('is_system', 0) ->where(function ($query) { $query - ->whereNotIn("title", [ - "PHP Executor", - "Node Executor", - "Python Executor", - "C# Executor", - "Java Executor" + ->whereNotIn('title', [ + 'PHP Executor', + 'Node Executor', + 'Python Executor', + 'C# Executor', + 'Java Executor', ]) - ->orWhereNotIn("description", [ - "Default PHP Executor", - "Default Javascript/Node Executor", - "Default Python Executor", - "Default C# Executor", - "Default Java Executor" + ->orWhereNotIn('description', [ + 'Default PHP Executor', + 'Default Javascript/Node Executor', + 'Default Python Executor', + 'Default C# Executor', + 'Default Java Executor', ]); }) - ->whereNotIn("language", ["php-nayra", "lua", "javascript-ssr", "sql"]) - ->whereNotNull("config") + ->whereNotIn('language', ['php-nayra', 'lua', 'javascript-ssr', 'sql']) + ->whereNotNull('config') ->where(function ($query) { $query - ->where("type", ScriptExecutorType::Custom) - ->orWhereNull("type"); + ->where('type', ScriptExecutorType::Custom) + ->orWhereNull('type'); }); if (is_array($uuid) && !empty($uuid)) { - $query->whereIn("uuid", $uuid); - } else if (is_string($uuid)) { - $query->where("uuid", $uuid); + $query->whereIn('uuid', $uuid); + } elseif (is_string($uuid)) { + $query->where('uuid', $uuid); } return $query->get(); diff --git a/ProcessMaker/Console/Commands/UpdateSettingsGroupId.php b/ProcessMaker/Console/Commands/UpdateSettingsGroupId.php index 02804f9ccb..fa213146c7 100644 --- a/ProcessMaker/Console/Commands/UpdateSettingsGroupId.php +++ b/ProcessMaker/Console/Commands/UpdateSettingsGroupId.php @@ -32,6 +32,7 @@ public function handle() { // Update the setting.group_id with the corresponding category created in settings_menus Setting::updateAllSettingsGroupId(); - return $this->info("Settings group_id updated successfully"); + + return $this->info('Settings group_id updated successfully'); } } diff --git a/ProcessMaker/Contracts/ConditionalRedirectServiceInterface.php b/ProcessMaker/Contracts/ConditionalRedirectServiceInterface.php index e46e390096..fdece68e6e 100644 --- a/ProcessMaker/Contracts/ConditionalRedirectServiceInterface.php +++ b/ProcessMaker/Contracts/ConditionalRedirectServiceInterface.php @@ -6,7 +6,6 @@ /** * @see \ProcessMaker\Services\ConditionalRedirectService - * @package ProcessMaker\Contracts */ interface ConditionalRedirectServiceInterface { diff --git a/ProcessMaker/Contracts/ScriptModuleInterface.php b/ProcessMaker/Contracts/ScriptModuleInterface.php new file mode 100644 index 0000000000..c0e72fadd2 --- /dev/null +++ b/ProcessMaker/Contracts/ScriptModuleInterface.php @@ -0,0 +1,35 @@ +all(). + */ +interface ScriptModuleInterface +{ + /** + * Unique key used as the script variable name (e.g. "collections"). + */ + public static function key(): string; + + /** + * Human-readable label for docs / UI catalog. + */ + public static function label(): string; + + /** + * Method catalog for discovery (key => meta). + * + * @return array> + */ + public static function catalog(): array; + + /** + * Prepare the module for a single script execution. + */ + public function boot(ScriptExecutionContext $context): void; +} diff --git a/ProcessMaker/Contracts/ServiceTaskImplementationInterface.php b/ProcessMaker/Contracts/ServiceTaskImplementationInterface.php index a1800f7995..96eb345e01 100644 --- a/ProcessMaker/Contracts/ServiceTaskImplementationInterface.php +++ b/ProcessMaker/Contracts/ServiceTaskImplementationInterface.php @@ -14,7 +14,10 @@ interface ServiceTaskImplementationInterface * @param array $config * @param string $tokenId * - * @return string $tokenId + * @return mixed + * + * Implementations may accept an optional 4th argument `$timeout` (seconds). + * WorkflowManager passes it when available (see CoreServiceTask). */ public function run(array $data, array $config, $tokenId = ''); } diff --git a/ProcessMaker/Contracts/WorkflowManagerInterface.php b/ProcessMaker/Contracts/WorkflowManagerInterface.php index 1776f30245..9b1be65189 100644 --- a/ProcessMaker/Contracts/WorkflowManagerInterface.php +++ b/ProcessMaker/Contracts/WorkflowManagerInterface.php @@ -181,10 +181,11 @@ public function existsServiceImplementation($implementation); * @param array $data * @param array $config * @param string $tokenId + * @param int|float $timeout * * @return mixed */ - public function runServiceImplementation($implementation, array $data, array $config, $tokenId = ''); + public function runServiceImplementation($implementation, array $data, array $config, $tokenId = '', $timeout = 0); /** * Get the service task class implementation diff --git a/ProcessMaker/Events/ActivityAssigned.php b/ProcessMaker/Events/ActivityAssigned.php index c9a379d9bf..e235a7df15 100644 --- a/ProcessMaker/Events/ActivityAssigned.php +++ b/ProcessMaker/Events/ActivityAssigned.php @@ -64,7 +64,7 @@ public function getProcessRequest() /** * Return the process request token. * - * @return \ProcessMaker\Models\ProcessRequestToken + * @return ProcessRequestToken */ public function getProcessRequestToken() { diff --git a/ProcessMaker/Events/ActivityCompleted.php b/ProcessMaker/Events/ActivityCompleted.php index 83e99c8b36..7e69679741 100644 --- a/ProcessMaker/Events/ActivityCompleted.php +++ b/ProcessMaker/Events/ActivityCompleted.php @@ -51,7 +51,7 @@ public function broadcastOn() /** * Return the process request. * - * @return \ProcessMaker\Models\ProcessRequestToken + * @return ProcessRequestToken */ public function getProcessRequestToken() { diff --git a/ProcessMaker/Events/CustomizeUiUpdated.php b/ProcessMaker/Events/CustomizeUiUpdated.php index 17137eade7..86db58a684 100644 --- a/ProcessMaker/Events/CustomizeUiUpdated.php +++ b/ProcessMaker/Events/CustomizeUiUpdated.php @@ -31,7 +31,7 @@ class CustomizeUiUpdated implements SecurityLogEventInterface private function defaultVariables(): string { $defaults = config('app.default_colors'); - + $objects = []; foreach ($defaults as $name => $value) { $objects[] = [ @@ -40,10 +40,10 @@ private function defaultVariables(): string 'title' => ucfirst($name), ]; } - + return json_encode($objects); } - + /** * Create a new event instance. * diff --git a/ProcessMaker/Events/PluginLog.php b/ProcessMaker/Events/PluginLog.php new file mode 100644 index 0000000000..6855c8f0b9 --- /dev/null +++ b/ProcessMaker/Events/PluginLog.php @@ -0,0 +1,65 @@ +message = $message; + $this->status = $status; + $this->pluginName = $pluginName; + $this->userId = $userId; + } + + public function broadcastAs() + { + return 'PluginLog'; + } + + public function broadcastOn() + { + return new PrivateChannel('ProcessMaker.Models.User.' . $this->userId); + } +} diff --git a/ProcessMaker/Events/ProcessCompleted.php b/ProcessMaker/Events/ProcessCompleted.php index 566fd59730..af37cbd0cb 100644 --- a/ProcessMaker/Events/ProcessCompleted.php +++ b/ProcessMaker/Events/ProcessCompleted.php @@ -57,7 +57,7 @@ public function broadcastOn() /** * Return the process request. * - * @return \ProcessMaker\Models\ProcessRequest + * @return ProcessRequest */ public function getProcessRequest() { diff --git a/ProcessMaker/Events/RedirectToEvent.php b/ProcessMaker/Events/RedirectToEvent.php index df05b8b010..4b2e1b6639 100644 --- a/ProcessMaker/Events/RedirectToEvent.php +++ b/ProcessMaker/Events/RedirectToEvent.php @@ -45,16 +45,17 @@ public function broadcastOn(): array { $channels = [ // Current request - new PrivateChannel('ProcessMaker.Models.ProcessRequest.' . $this->processRequest->getKey()) + new PrivateChannel('ProcessMaker.Models.ProcessRequest.' . $this->processRequest->getKey()), ]; // include child requests if any - foreach($this->processRequest->childRequests()->pluck('id') as $childRequestId) { + foreach ($this->processRequest->childRequests()->pluck('id') as $childRequestId) { $channels[] = new PrivateChannel('ProcessMaker.Models.ProcessRequest.' . $childRequestId); } // include parent request if any if ($this->processRequest->parent_request_id) { $channels[] = new PrivateChannel('ProcessMaker.Models.ProcessRequest.' . $this->processRequest->parent_request_id); } + return $channels; } } diff --git a/ProcessMaker/Events/SessionStarted.php b/ProcessMaker/Events/SessionStarted.php index c55631656c..9ee5aa96ac 100644 --- a/ProcessMaker/Events/SessionStarted.php +++ b/ProcessMaker/Events/SessionStarted.php @@ -32,7 +32,7 @@ public function __construct(User $user) /** * Get the channels the event should broadcast on. * - * @return \Illuminate\Broadcasting\Channel|array + * @return Channel|array */ public function broadcastOn() { diff --git a/ProcessMaker/Exception/MissingBpmnReferenceException.php b/ProcessMaker/Exception/MissingBpmnReferenceException.php new file mode 100644 index 0000000000..5aec7b716e --- /dev/null +++ b/ProcessMaker/Exception/MissingBpmnReferenceException.php @@ -0,0 +1,11 @@ +route('file-manager.index'); case 'view-settings': return redirect()->route('settings.index'); + case 'view-plugins': + return redirect()->route('plugins.index'); default: throw new AuthorizationException(); } diff --git a/ProcessMaker/Http/Controllers/Api/DeployProcess.php b/ProcessMaker/Http/Controllers/Api/DeployProcess.php new file mode 100644 index 0000000000..7ff6134846 --- /dev/null +++ b/ProcessMaker/Http/Controllers/Api/DeployProcess.php @@ -0,0 +1,173 @@ +all(), [ + 'bpmn' => 'required|string', + 'name' => 'required|string|max:255', + 'description' => 'nullable|string|max:65535', + ]); + + if ($validator->fails()) { + return response()->json([ + 'success' => false, + 'errors' => $validator->errors(), + ], 422); + } + + return $this->saveDeployedProcess($request); + } + + private function saveDeployedProcess(Request $request): JsonResponse + { + try { + $bpmn = $this->references->resolveNamedReferences($request->input('bpmn')); + $xmlErrors = $this->validateXml($bpmn); + if ($xmlErrors !== []) { + return response()->json([ + 'success' => false, + 'message' => 'Invalid BPMN XML.', + 'errors' => ['bpmn' => $xmlErrors], + ], 422); + } + + [$process, $created] = $this->upsertProcessByName($request, $bpmn); + + return response()->json([ + 'success' => true, + 'action' => $created ? 'created' : 'updated', + 'process_id' => $process->id, + 'name' => $process->name, + ]); + } catch (Throwable $exception) { + return $this->deployProcessFailureResponse($exception); + } + } + + /** + * @return array{0: Process, 1: bool} + */ + private function upsertProcessByName(Request $request, string $bpmn): array + { + return DB::transaction(function () use ($request, $bpmn): array { + $process = Process::where('name', $request->input('name'))->first(); + $created = $process === null; + + if ($created) { + $process = new Process(); + $process->name = $request->input('name'); + $process->description = (string) $request->input('description', ''); + $process->status = 'ACTIVE'; + $process->user_id = $request->user()->id; + } elseif ($request->has('description')) { + $process->description = (string) $request->input('description'); + } + + $process->bpmn = $bpmn; + $process->saveOrFail(); + + return [$process->refresh(), $created]; + }); + } + + private function deployProcessFailureResponse(Throwable $exception): JsonResponse + { + if ($exception instanceof MissingBpmnReferenceException) { + return response()->json([ + 'success' => false, + 'message' => $exception->getMessage(), + ], 422); + } + + report($exception); + + return response()->json([ + 'success' => false, + 'message' => 'The process could not be saved.', + 'error' => $exception->getMessage(), + ], 422); + } + + public function getProcessBpmn(Request $request): JsonResponse + { + $validator = Validator::make($request->all(), [ + 'name' => 'required|string|max:255', + ]); + + if ($validator->fails()) { + return response()->json([ + 'success' => false, + 'errors' => $validator->errors(), + ], 422); + } + + $process = Process::where('name', $request->input('name'))->first(); + if ($process === null) { + return response()->json([ + 'success' => false, + 'message' => 'Process not found.', + 'name' => $request->input('name'), + ], 404); + } + + return response()->json([ + 'success' => true, + 'process_id' => $process->id, + 'name' => $process->name, + 'bpmn' => $this->references->restoreNamedReferences((string) $process->bpmn), + ]); + } + + /** + * @return list + */ + private function validateXml(string $bpmn): array + { + $previous = libxml_use_internal_errors(true); + libxml_clear_errors(); + + $document = new DOMDocument(); + $valid = $document->loadXML($bpmn, LIBXML_NONET); + $errors = array_map( + static fn (\LibXMLError $error): string => trim($error->message), + libxml_get_errors() + ); + + libxml_clear_errors(); + libxml_use_internal_errors($previous); + + return $valid ? [] : $errors; + } +} diff --git a/ProcessMaker/Http/Controllers/Api/DeployScreen.php b/ProcessMaker/Http/Controllers/Api/DeployScreen.php new file mode 100644 index 0000000000..d063c6da21 --- /dev/null +++ b/ProcessMaker/Http/Controllers/Api/DeployScreen.php @@ -0,0 +1,184 @@ +all(), [ + 'yml_content' => 'required|string', + 'name' => 'required|string', + ]); + + if ($validator->fails()) { + return response()->json([ + 'success' => false, + 'errors' => $validator->errors(), + ], 422); + } + + $ymlContent = $request->input('yml_content'); + $name = $request->input('name'); + + $parsedData = $this->parseDeployYaml($ymlContent); + if ($parsedData instanceof JsonResponse) { + return $parsedData; + } + + $screen = Screen::where('title', $name)->first(); + $created = $screen === null; + + if ($created) { + $screen = new Screen(); + $screen->title = $name; + $screen->description = $parsedData['description'] ?? ''; + $screen->type = $parsedData['type'] ?? 'FORM'; + } + + $this->applyYamlFields($screen, $parsedData, $created); + $screen->save(); + $screen->refresh(); + + return response()->json([ + 'success' => true, + 'action' => $created ? 'created' : 'updated', + 'screen_id' => $screen->id, + 'name' => $screen->title, + 'message' => $created + ? 'Screen created from YAML.' + : 'Screen updated from YAML.', + ]); + } + + /** + * @return \Illuminate\Http\Response|JsonResponse + */ + public function getScreenYaml(Request $request) + { + $validator = Validator::make($request->all(), [ + 'name' => 'required|string', + ]); + + if ($validator->fails()) { + return response()->json([ + 'success' => false, + 'errors' => $validator->errors(), + ], 422); + } + + $name = $request->input('name'); + $resolved = $this->resolvePublishedScreenByTitle($name); + if ($resolved instanceof JsonResponse) { + return $resolved; + } + + [$screen, $screenVersion] = $resolved; + $config = $this->removeInspectorMetadata($screenVersion->config ?? []); + $data = [ + 'config' => $config, + 'computed' => $screenVersion->computed, + 'watchers' => $screenVersion->watchers, + 'custom_css' => $screenVersion->custom_css, + ]; + $yaml = Yaml::dump($data, 100, 2, Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK); + + return response($yaml, 200) + ->header('Content-Type', 'text/yaml') + ->header('X-Screen-Id', (string) $screen->id) + ->header('X-Screen-Name', $screen->title); + } + + /** + * @return array{0: Screen, 1: object}|JsonResponse + */ + private function resolvePublishedScreenByTitle(string $name): array|JsonResponse + { + $screen = Screen::where('title', $name)->first(); + if ($screen === null) { + return response()->json([ + 'success' => false, + 'message' => 'Screen not found', + 'name' => $name, + ], 404); + } + + $screenVersion = $screen->getPublishedVersion([]); + if ($screenVersion === null) { + return response()->json([ + 'success' => false, + 'message' => 'Screen does not have a published version', + 'name' => $name, + 'screen_id' => $screen->id, + ], 404); + } + + return [$screen, $screenVersion]; + } + + /** + * @return array|JsonResponse + */ + private function parseDeployYaml(string $ymlContent): array|JsonResponse + { + try { + $parsedData = Yaml::parse($ymlContent) ?? []; + } catch (\Exception $e) { + return response()->json([ + 'success' => false, + 'message' => 'Invalid YAML content', + 'error' => $e->getMessage(), + ], 400); + } + + if (!is_array($parsedData)) { + return response()->json([ + 'success' => false, + 'message' => 'Invalid YAML content', + 'error' => 'YAML must parse to a mapping of screen fields', + ], 400); + } + + return $parsedData; + } + + private function applyYamlFields(Screen $screen, array $parsedData, bool $created): void + { + $defaults = [ + 'config' => [], + 'computed' => [], + 'watchers' => [], + 'custom_css' => '', + ]; + + foreach ($defaults as $field => $default) { + if (array_key_exists($field, $parsedData)) { + $screen->{$field} = $parsedData[$field]; + } elseif ($created) { + $screen->{$field} = $default; + } + } + } +} diff --git a/ProcessMaker/Http/Controllers/Api/DeployScript.php b/ProcessMaker/Http/Controllers/Api/DeployScript.php new file mode 100644 index 0000000000..8d978ab0c9 --- /dev/null +++ b/ProcessMaker/Http/Controllers/Api/DeployScript.php @@ -0,0 +1,135 @@ +all(), [ + 'code' => 'required|string', + 'name' => 'required|string', + ]); + + if ($validator->fails()) { + return response()->json([ + 'success' => false, + 'errors' => $validator->errors(), + ], 422); + } + + $name = $request->input('name'); + $code = $request->input('code'); + + $script = Script::where('title', $name)->first(); + $created = $script === null; + + if ($created) { + $executor = $this->executors->resolveForNewScript(); + if ($executor === null) { + return response()->json([ + 'success' => false, + 'message' => 'No script executor is available to create a script.', + ], 422); + } + + $script = new Script(); + $script->title = $name; + $script->description = ''; + $script->language = $executor->language; + $script->script_executor_id = $executor->id; + $script->run_as_user_id = $request->user()->id; + $script->timeout = self::DEFAULT_TIMEOUT; + $script->retry_attempts = self::DEFAULT_RETRY_ATTEMPTS; + $script->retry_wait_time = self::DEFAULT_RETRY_WAIT_TIME; + } + + $script->code = $code; + $script->save(); + $script->refresh(); + + return response()->json([ + 'success' => true, + 'action' => $created ? 'created' : 'updated', + 'script_id' => $script->id, + 'name' => $script->title, + 'message' => $created + ? 'Script created from PHP.' + : 'Script updated from PHP.', + ]); + } + + /** + * Return the published script PHP source, looked up by title. + * + * @return \Illuminate\Http\Response|JsonResponse + */ + public function getScriptPhp(Request $request) + { + $validator = Validator::make($request->all(), [ + 'name' => 'required|string', + ]); + + if ($validator->fails()) { + return response()->json([ + 'success' => false, + 'errors' => $validator->errors(), + ], 422); + } + + $name = $request->input('name'); + $script = Script::where('title', $name)->first(); + $scriptVersion = $script?->getPublishedVersion([]); + if ($script === null || $scriptVersion === null) { + $payload = [ + 'success' => false, + 'message' => $script === null + ? 'Script not found' + : 'Script does not have a published version', + 'name' => $name, + ]; + if ($script !== null) { + $payload['script_id'] = $script->id; + } + + return response()->json($payload, 404); + } + + return response((string) ($scriptVersion->code ?? ''), 200) + ->header('Content-Type', 'text/plain; charset=UTF-8') + ->header('X-Script-Id', (string) $script->id) + ->header('X-Script-Name', $script->title); + } +} diff --git a/ProcessMaker/Http/Controllers/Api/GroupMemberController.php b/ProcessMaker/Http/Controllers/Api/GroupMemberController.php index b5b95207f1..face7d9e48 100644 --- a/ProcessMaker/Http/Controllers/Api/GroupMemberController.php +++ b/ProcessMaker/Http/Controllers/Api/GroupMemberController.php @@ -214,7 +214,7 @@ public function destroy(GroupMember $group_member) /** * Display a listing of groups available * - * @param \Illuminate\Http\Request $request + * @param Request $request * @return ApiCollection * * @OA\Get( @@ -338,7 +338,7 @@ public function groupsAvailable(Request $request) /** * Display a listing of users available * - * @param \Illuminate\Http\Request $request + * @param Request $request * @return ApiCollection * * @OA\Get( diff --git a/ProcessMaker/Http/Controllers/Api/ImportController.php b/ProcessMaker/Http/Controllers/Api/ImportController.php index e008bf2e1c..a6c598afa0 100644 --- a/ProcessMaker/Http/Controllers/Api/ImportController.php +++ b/ProcessMaker/Http/Controllers/Api/ImportController.php @@ -41,7 +41,7 @@ public function preview(Request $request, $version = null): JsonResponse return response()->json(['error' => $e->getMessage()], 400); } - $options = new Options([]); + $options = new Options([], $request->user()?->id); $importer = new Importer($payload, $options); $manifest = $importer->previewImport(); @@ -80,7 +80,10 @@ public function import(Request $request): JsonResponse return response()->json(['error' => $e->getMessage()], 400); } - $options = new Options(json_decode(file_get_contents(utf8_decode($request->file('options'))), true)); + $options = new Options( + json_decode(file_get_contents(utf8_decode($request->file('options'))), true), + $request->user()?->id + ); $importer = new Importer($payload, $options); $manifest = $importer->doImport(); diff --git a/ProcessMaker/Http/Controllers/Api/PluginController.php b/ProcessMaker/Http/Controllers/Api/PluginController.php new file mode 100644 index 0000000000..57752fd872 --- /dev/null +++ b/ProcessMaker/Http/Controllers/Api/PluginController.php @@ -0,0 +1,94 @@ +manager->list(); + + return response()->json(['data' => $plugins]); + } + + public function install(Request $request): JsonResponse + { + $request->validate([ + 'url' => 'required_without:zip|nullable|string|starts_with:https://', + 'zip' => 'required_without:url|nullable|file|mimes:zip', + 'branch' => 'prohibits:tag|nullable|string', + 'tag' => 'prohibits:branch|nullable|string', + 'git_username' => 'nullable|string|max:255', + 'git_token' => 'nullable|string|max:4096', + ]); + + $userId = $request->user()->id; + + if ($request->hasFile('zip')) { + $tmpPath = $request->file('zip')->store('plugin-uploads', 'local'); + $fullPath = storage_path('app/' . $tmpPath); + + InstallPlugin::dispatch( + userId: $userId, + zipPath: $fullPath, + ); + } else { + InstallPlugin::dispatch( + userId: $userId, + url: $request->input('url'), + branch: $request->input('branch'), + tag: $request->input('tag'), + gitUsername: $request->input('git_username'), + gitToken: $request->input('git_token'), + ); + } + + return response()->json([ + 'queued' => true, + 'message' => 'Plugin installation queued', + ]); + } + + public function reinstall(string $name, Request $request): JsonResponse + { + ReinstallPlugin::dispatch($name, $request->user()->id); + + return response()->json([ + 'queued' => true, + 'message' => 'Plugin reinstall queued', + ]); + } + + public function destroy(string $name, Request $request): JsonResponse + { + UninstallPlugin::dispatch($name, $request->user()->id); + + return response()->json([ + 'queued' => true, + 'message' => 'Plugin uninstallation queued', + ]); + } + + public function toggle(string $name, Request $request): JsonResponse + { + TogglePlugin::dispatch($name, $request->user()->id); + + return response()->json([ + 'queued' => true, + 'message' => 'Plugin toggle queued', + ]); + } +} diff --git a/ProcessMaker/Http/Controllers/Api/ScriptController.php b/ProcessMaker/Http/Controllers/Api/ScriptController.php index e3cca93814..7fa45f84f7 100644 --- a/ProcessMaker/Http/Controllers/Api/ScriptController.php +++ b/ProcessMaker/Http/Controllers/Api/ScriptController.php @@ -173,6 +173,11 @@ public function index(Request $request) * property="nonce", * type="string", * ), + * @OA\Property( + * property="run_in_process", + * type="boolean", + * description="When true (and Core Service Task enabled), preview via in-process PHP runner instead of Docker/microservice. Also auto-enabled when the script has allow_in_process=true.", + * ), * ), * ), * @@ -189,10 +194,27 @@ public function preview(Request $request, Script $script) $config = $this->getRequestArray($request->get('config')); $code = $request->get('code'); $nonce = $request->get('nonce'); + $runInProcess = $this->shouldPreviewInProcess($request, $script); + + TestScript::dispatch($script, $request->user(), $code, $data, $config, $nonce, $runInProcess)->onQueue('bpmn'); + + return ['status' => 'success', 'run_in_process' => $runInProcess]; + } + + /** + * Decide if script preview should use Core Service Task in-process runner. + */ + private function shouldPreviewInProcess(Request $request, Script $script): bool + { + if (!config('core-service-task.enabled') || strtolower((string) $script->language) !== 'php') { + return false; + } - TestScript::dispatch($script, $request->user(), $code, $data, $config, $nonce)->onQueue('bpmn'); + if ($request->boolean('run_in_process')) { + return true; + } - return ['status' => 'success']; + return (bool) $script->allow_in_process; } /** @@ -347,6 +369,7 @@ public function show(Script $script) */ public function store(Request $request) { + $this->authorizeModulesExecution($request); $request->validate(Script::rules()); $script = new Script(); $script->fill($request->input()); @@ -399,6 +422,7 @@ public function store(Request $request) */ public function update(Script $script, Request $request) { + $this->authorizeModulesExecution($request, $script); $request->validate(Script::rules($script)); $script->fill($request->input()); $original = array_intersect_key($script->getOriginal(), $script->getDirty()); @@ -415,6 +439,20 @@ public function update(Script $script, Request $request) return response($request, 204); } + private function authorizeModulesExecution(Request $request, ?Script $script = null): void + { + $executionMode = $request->input('execution_mode', $script?->execution_mode); + if ($executionMode !== Script::EXECUTION_MODE_MODULES) { + return; + } + + if ($this->canUseModules($request)) { + return; + } + + $request->merge(['execution_mode' => Script::EXECUTION_MODE_BARE]); + } + /** * Update a draft script. * @@ -492,6 +530,7 @@ public function draft(Script $script, Request $request) */ public function duplicate(Script $script, Request $request) { + $this->authorizeModulesExecution($request, $script); $request->validate(Script::rules()); $newScript = new Script(); @@ -510,12 +549,27 @@ public function duplicate(Script $script, Request $request) $newScript->description = $request->input('description'); } + $newScript->execution_mode = $request->input( + 'execution_mode', + $script->execution_mode === Script::EXECUTION_MODE_MODULES + && $this->canUseModules($request) + ? Script::EXECUTION_MODE_MODULES + : Script::EXECUTION_MODE_BARE + ); $newScript->saveOrFail(); ScriptDuplicated::dispatch($newScript, $newScript->getChanges()); return new ScriptResource($newScript); } + private function canUseModules(Request $request): bool + { + return (bool) ( + $request->user()?->is_administrator + || $request->user()?->can('run-in-process-modules') + ); + } + /** * Delete a script in a process. * diff --git a/ProcessMaker/Http/Controllers/Api/SignalController.php b/ProcessMaker/Http/Controllers/Api/SignalController.php index b4432e8fa0..798b642838 100644 --- a/ProcessMaker/Http/Controllers/Api/SignalController.php +++ b/ProcessMaker/Http/Controllers/Api/SignalController.php @@ -24,7 +24,7 @@ class SignalController extends Controller * * @param Request $request * - * @return \Illuminate\Http\Response + * @return Response * * @OA\Get( * path="/signals", diff --git a/ProcessMaker/Http/Controllers/Process/ScreenBuilderController.php b/ProcessMaker/Http/Controllers/Process/ScreenBuilderController.php index 76727289ef..5e9aa2c40f 100644 --- a/ProcessMaker/Http/Controllers/Process/ScreenBuilderController.php +++ b/ProcessMaker/Http/Controllers/Process/ScreenBuilderController.php @@ -36,6 +36,7 @@ public function edit(ScreenBuilderManager $manager, Screen $screen, $processId = 'computed', 'custom_css', 'watchers', + 'preview_presets', ])); } diff --git a/ProcessMaker/Http/Controllers/Process/ScreenController.php b/ProcessMaker/Http/Controllers/Process/ScreenController.php index 65ba86dbb3..182a130400 100644 --- a/ProcessMaker/Http/Controllers/Process/ScreenController.php +++ b/ProcessMaker/Http/Controllers/Process/ScreenController.php @@ -83,7 +83,7 @@ public function edit(Screen $screen) { $addons = $this->getPluginAddons('edit', compact(['screen'])); $assignedProjects = json_decode($screen->projects, true); - + $lastDraftOrPublishedVersion = $screen->getDraftOrPublishedLatestVersion(); $isDraft = 0; @@ -92,11 +92,11 @@ public function edit(Screen $screen) } return view('processes.screens.edit', compact( - 'screen', - 'addons', - 'assignedProjects', - 'isDraft' - ) + 'screen', + 'addons', + 'assignedProjects', + 'isDraft' + ) ); } diff --git a/ProcessMaker/Http/Controllers/Process/SignalController.php b/ProcessMaker/Http/Controllers/Process/SignalController.php index d248e832b3..a632bc7561 100644 --- a/ProcessMaker/Http/Controllers/Process/SignalController.php +++ b/ProcessMaker/Http/Controllers/Process/SignalController.php @@ -28,7 +28,7 @@ public function index(Request $request) /** * Get a specific signal * - * @return \Illuminate\View\View|\Illuminate\Contracts\View + * @return View|\Illuminate\Contracts\View */ public function edit($id) { diff --git a/ProcessMaker/Http/Middleware/EncryptCookies.php b/ProcessMaker/Http/Middleware/EncryptCookies.php index 56273fb944..a242982d36 100644 --- a/ProcessMaker/Http/Middleware/EncryptCookies.php +++ b/ProcessMaker/Http/Middleware/EncryptCookies.php @@ -21,6 +21,6 @@ class EncryptCookies extends BaseEncrypter */ 'device_id', 'fromTriggerStartEvent', - 'language' + 'language', ]; } diff --git a/ProcessMaker/Http/Middleware/GenerateMenus.php b/ProcessMaker/Http/Middleware/GenerateMenus.php index f60536d283..caf1cf5e7b 100644 --- a/ProcessMaker/Http/Middleware/GenerateMenus.php +++ b/ProcessMaker/Http/Middleware/GenerateMenus.php @@ -122,6 +122,10 @@ public function handle(Request $request, Closure $next) 'icon' => 'fa-code', ]); } + $submenu->add(__('Plugins'), [ + 'route' => 'plugins.index', + 'icon' => 'fa-plug', + ]); if (config('app.case_retention_policy_enabled')) { $submenu->add(__('Cases Retention Logs'), [ diff --git a/ProcessMaker/Http/Middleware/IgnoreMapFiles.php b/ProcessMaker/Http/Middleware/IgnoreMapFiles.php index 46ed2eaee0..1fdeab0388 100644 --- a/ProcessMaker/Http/Middleware/IgnoreMapFiles.php +++ b/ProcessMaker/Http/Middleware/IgnoreMapFiles.php @@ -10,7 +10,7 @@ class IgnoreMapFiles * Handle an incoming request. * * @param \Illuminate\Http\Request $request - * @param \Closure $next + * @param Closure $next * @return mixed */ public function handle($request, Closure $next) diff --git a/ProcessMaker/Http/Middleware/IsAdmin.php b/ProcessMaker/Http/Middleware/IsAdmin.php index 232347f356..5e52f70156 100644 --- a/ProcessMaker/Http/Middleware/IsAdmin.php +++ b/ProcessMaker/Http/Middleware/IsAdmin.php @@ -11,7 +11,7 @@ class IsAdmin /** * Handle an incoming request. * - * @param \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response) $next + * @param Closure(Request): (Response) $next */ public function handle(Request $request, Closure $next): Response { diff --git a/ProcessMaker/Http/Middleware/NoCache.php b/ProcessMaker/Http/Middleware/NoCache.php index d85df5ff3b..ca7694ecbf 100644 --- a/ProcessMaker/Http/Middleware/NoCache.php +++ b/ProcessMaker/Http/Middleware/NoCache.php @@ -11,7 +11,7 @@ class NoCache /** * Handle an incoming request. * - * @param \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response) $next + * @param Closure(Request): (Response) $next */ public function handle(Request $request, Closure $next): Response { diff --git a/ProcessMaker/Http/Middleware/PermissionCheck.php b/ProcessMaker/Http/Middleware/PermissionCheck.php index 8b55c9348b..40d2154ac0 100644 --- a/ProcessMaker/Http/Middleware/PermissionCheck.php +++ b/ProcessMaker/Http/Middleware/PermissionCheck.php @@ -14,22 +14,22 @@ class PermissionCheck /** * The authentication factory instance. * - * @var \Illuminate\Contracts\Auth\Factory + * @var Auth */ protected $auth; /** * The gate instance. * - * @var \Illuminate\Contracts\Auth\Access\Gate + * @var Gate */ protected $gate; /** * Create a new middleware instance. * - * @param \Illuminate\Contracts\Auth\Factory $auth - * @param \Illuminate\Contracts\Auth\Access\Gate $gate + * @param Auth $auth + * @param Gate $gate * @return void */ public function __construct(Auth $auth, Gate $gate) @@ -42,7 +42,7 @@ public function __construct(Auth $auth, Gate $gate) * Handle an incoming request. * * @param \Illuminate\Http\Request $request - * @param \Closure $next + * @param Closure $next * @param string $ability * @param array|null $models * @return mixed diff --git a/ProcessMaker/Http/Middleware/RedirectIfAuthenticated.php b/ProcessMaker/Http/Middleware/RedirectIfAuthenticated.php index 390d42bbd5..e80eae30ea 100644 --- a/ProcessMaker/Http/Middleware/RedirectIfAuthenticated.php +++ b/ProcessMaker/Http/Middleware/RedirectIfAuthenticated.php @@ -11,7 +11,7 @@ class RedirectIfAuthenticated * Handle an incoming request. * * @param \Illuminate\Http\Request $request - * @param \Closure $next + * @param Closure $next * @param string|null $guard * @return mixed */ diff --git a/ProcessMaker/Http/Middleware/SamlRequest.php b/ProcessMaker/Http/Middleware/SamlRequest.php index 1ab1b40740..65f3ecb763 100644 --- a/ProcessMaker/Http/Middleware/SamlRequest.php +++ b/ProcessMaker/Http/Middleware/SamlRequest.php @@ -12,7 +12,7 @@ class SamlRequest /** * Handle an incoming request. * - * @param \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response) $next + * @param Closure(Request): (Response) $next */ public function handle(Request $request, Closure $next): Response { diff --git a/ProcessMaker/Http/Middleware/ServerTimingMiddleware.php b/ProcessMaker/Http/Middleware/ServerTimingMiddleware.php index 2a15e84a6d..f0be97d658 100644 --- a/ProcessMaker/Http/Middleware/ServerTimingMiddleware.php +++ b/ProcessMaker/Http/Middleware/ServerTimingMiddleware.php @@ -20,7 +20,7 @@ public function __construct() /** * Handle an incoming request. * - * @param \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response) $next + * @param Closure(Request): (Response) $next */ public function handle(Request $request, Closure $next): Response { diff --git a/ProcessMaker/Http/Middleware/SessionControlBlock.php b/ProcessMaker/Http/Middleware/SessionControlBlock.php index 40cc8d9b7c..94d8ad7782 100644 --- a/ProcessMaker/Http/Middleware/SessionControlBlock.php +++ b/ProcessMaker/Http/Middleware/SessionControlBlock.php @@ -21,7 +21,7 @@ class SessionControlBlock /** * Handle an incoming request. * - * @param \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response) $next + * @param Closure(Request): (Response) $next */ public function handle(Request $request, Closure $next): Response { diff --git a/ProcessMaker/Http/Middleware/SessionControlKill.php b/ProcessMaker/Http/Middleware/SessionControlKill.php index 30646faf1b..687d8b3082 100644 --- a/ProcessMaker/Http/Middleware/SessionControlKill.php +++ b/ProcessMaker/Http/Middleware/SessionControlKill.php @@ -21,7 +21,7 @@ class SessionControlKill /** * Handle an incoming request. * - * @param \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response) $next + * @param Closure(Request): (Response) $next */ public function handle(Request $request, Closure $next): Response { diff --git a/ProcessMaker/Http/Middleware/TrustHosts.php b/ProcessMaker/Http/Middleware/TrustHosts.php index 0afcc7c242..84737ea3c3 100644 --- a/ProcessMaker/Http/Middleware/TrustHosts.php +++ b/ProcessMaker/Http/Middleware/TrustHosts.php @@ -2,8 +2,8 @@ namespace ProcessMaker\Http\Middleware; -use Illuminate\Http\Middleware\TrustHosts as Middleware; use Closure; +use Illuminate\Http\Middleware\TrustHosts as Middleware; use Illuminate\Http\Request; class TrustHosts extends Middleware @@ -49,6 +49,7 @@ public function handle(Request $request, $next) if (! $trusted) { \Log::warning('Rejected request with untrusted X-Forwarded-Host', [ 'forwarded_host' => $forwardedHost, + 'trusted_pattern' => $this->hosts(), ]); abort(400, 'Invalid Host Header'); } @@ -67,4 +68,4 @@ protected function hostIsValid(string $host, string $pattern): bool return preg_match('/' . str_replace('/', '\/', $pattern) . '/i', $host) === 1; } -} \ No newline at end of file +} diff --git a/ProcessMaker/Http/Middleware/TwoFactorAuthentication.php b/ProcessMaker/Http/Middleware/TwoFactorAuthentication.php index 2dd7f74042..ca9fa124b8 100644 --- a/ProcessMaker/Http/Middleware/TwoFactorAuthentication.php +++ b/ProcessMaker/Http/Middleware/TwoFactorAuthentication.php @@ -11,7 +11,7 @@ class TwoFactorAuthentication * Handle an incoming request. * * @param \Illuminate\Http\Request $request - * @param \Closure $next + * @param Closure $next * @return mixed */ public function handle($request, Closure $next) diff --git a/ProcessMaker/Http/Resources/ApiCollection.php b/ProcessMaker/Http/Resources/ApiCollection.php index 07f3b7d529..c4b2a7788c 100644 --- a/ProcessMaker/Http/Resources/ApiCollection.php +++ b/ProcessMaker/Http/Resources/ApiCollection.php @@ -62,7 +62,7 @@ public function __construct($resource, $total = null) /** * Generic collection to add sorting and filtering metadata. * - * @param \Illuminate\Http\Request $request + * @param Request $request * @return array */ public function toArray($request) @@ -92,7 +92,7 @@ public function toArray($request) /** * Create an HTTP response that represents the object. * - * @param \Illuminate\Http\Request $request + * @param Request $request * @return \Illuminate\Http\JsonResponse */ public function toResponse($request) @@ -109,9 +109,9 @@ public function toResponse($request) /** * Convert a Collection to a LengthAwarePaginator * - * @param \Illuminate\Support\Collection $collection - * @param \Illuminate\Http\Request $request - * @return \Illuminate\Pagination\LengthAwarePaginator + * @param Collection $collection + * @param Request $request + * @return LengthAwarePaginator */ public function collectionToPaginator(Collection $collection, Request $request) { diff --git a/ProcessMaker/ImportExport/Exporters/ScriptExporter.php b/ProcessMaker/ImportExport/Exporters/ScriptExporter.php index c1083bbca5..6fc731eace 100644 --- a/ProcessMaker/ImportExport/Exporters/ScriptExporter.php +++ b/ProcessMaker/ImportExport/Exporters/ScriptExporter.php @@ -6,6 +6,7 @@ use Illuminate\Support\Facades\Log; use ProcessMaker\ImportExport\DependentType; use ProcessMaker\Models\EnvironmentVariable; +use ProcessMaker\Models\Script; use ProcessMaker\Models\ScriptCategory; use ProcessMaker\Models\User; @@ -51,6 +52,13 @@ public function import() : bool // Update environment variable references in script code $this->updateEnvironmentVariableReferences(); + if ( + $this->model->execution_mode === Script::EXECUTION_MODE_MODULES + && !$this->canUseModules() + ) { + $this->model->execution_mode = Script::EXECUTION_MODE_BARE; + } + // Pre-save cleanup for data source scripts to prevent constraint violations if ($this->mode === 'update' && $this->model->exists) { // Check if this script has any data source relationships that might cause conflicts @@ -70,6 +78,15 @@ public function import() : bool return $this->model->save(); } + private function canUseModules(): bool + { + $user = $this->options->importingUserId + ? User::find($this->options->importingUserId) + : auth()->user(); + + return (bool) ($user?->is_administrator || $user?->can('run-in-process-modules')); + } + private function getEnvironmentVariables() : array { $environmentVariables = EnvironmentVariable::get(); diff --git a/ProcessMaker/Jobs/BoundaryEvent.php b/ProcessMaker/Jobs/BoundaryEvent.php index d5a56a7f0c..c42276a306 100644 --- a/ProcessMaker/Jobs/BoundaryEvent.php +++ b/ProcessMaker/Jobs/BoundaryEvent.php @@ -42,7 +42,7 @@ public function __construct(Definitions $definitions, ExecutionInstanceInterface * * @param TokenInterface $token * @param CatchEventInterface $element - * @return \ProcessMaker\Nayra\Contracts\Engine\ExecutionInstanceInterface + * @return ExecutionInstanceInterface */ public function action(BpmnDocumentInterface $definitions, TokenInterface $token = null) { diff --git a/ProcessMaker/Jobs/CatchEvent.php b/ProcessMaker/Jobs/CatchEvent.php index bd0aefd676..961580bbfc 100644 --- a/ProcessMaker/Jobs/CatchEvent.php +++ b/ProcessMaker/Jobs/CatchEvent.php @@ -37,7 +37,7 @@ public function __construct(Definitions $definitions, ExecutionInstanceInterface * * @param TokenInterface $token * @param CatchEventInterface $element - * @return \ProcessMaker\Nayra\Contracts\Engine\ExecutionInstanceInterface + * @return ExecutionInstanceInterface */ public function action(TokenInterface $token, CatchEventInterface $element) { diff --git a/ProcessMaker/Jobs/CatchSignalEventInRequest.php b/ProcessMaker/Jobs/CatchSignalEventInRequest.php index ca8c0c4f0b..f177113ee3 100644 --- a/ProcessMaker/Jobs/CatchSignalEventInRequest.php +++ b/ProcessMaker/Jobs/CatchSignalEventInRequest.php @@ -27,7 +27,7 @@ class CatchSignalEventInRequest extends BpmnAction implements ShouldQueue /** * Create a new job instance. * - * @param \ProcessMaker\Models\ProcessRequest $instance + * @param ProcessRequest $instance * @param array $data * @param string $signalRef */ diff --git a/ProcessMaker/Jobs/ExecuteScript.php b/ProcessMaker/Jobs/ExecuteScript.php index f82147a07c..e57eb6427f 100644 --- a/ProcessMaker/Jobs/ExecuteScript.php +++ b/ProcessMaker/Jobs/ExecuteScript.php @@ -115,6 +115,7 @@ public function handleNayraDocker() ]); $result = curl_exec($ch); curl_close($ch); + return $result; } diff --git a/ProcessMaker/Jobs/ImportProcess.php b/ProcessMaker/Jobs/ImportProcess.php index 03b4768916..1e31e763ae 100644 --- a/ProcessMaker/Jobs/ImportProcess.php +++ b/ProcessMaker/Jobs/ImportProcess.php @@ -518,6 +518,7 @@ protected function saveScreen($screen) return $new; } catch (\Exception $e) { Log::error('Import Screen: ' . $e->getMessage()); + return false; } } diff --git a/ProcessMaker/Jobs/ImportV2.php b/ProcessMaker/Jobs/ImportV2.php index 8937976082..ac857031bf 100644 --- a/ProcessMaker/Jobs/ImportV2.php +++ b/ProcessMaker/Jobs/ImportV2.php @@ -83,7 +83,7 @@ private function runImport($logger) return; } - $options = new Options($options); + $options = new Options($options, $this->userId); $importer = new Importer($payload, $options, $logger); if ($this->isPreview) { $manifest = $importer->previewImport(); diff --git a/ProcessMaker/Jobs/InstallPlugin.php b/ProcessMaker/Jobs/InstallPlugin.php new file mode 100644 index 0000000000..12d53f84c0 --- /dev/null +++ b/ProcessMaker/Jobs/InstallPlugin.php @@ -0,0 +1,90 @@ +releaseAfter(30) + ->expireAfter(630), + ]; + } + + public function handle(PluginManager $manager): void + { + try { + $label = $this->zipPath ? basename($this->zipPath) : ($this->url ?? 'plugin'); + event(new PluginLog('Starting queued plugin installation...', 'starting', $label, $this->userId)); + + if ($this->zipPath) { + $manager->installFromZip($this->zipPath, $this->userId); + + return; + } + + $manager->install( + $this->url, + $this->branch, + $this->tag, + $this->userId, + $this->gitUsername, + $this->gitToken + ); + } catch (Throwable $exception) { + $manager->revertFailedInstall($this->userId); + + throw $exception; + } + } + + public function failed(Throwable $exception): void + { + $label = $this->zipPath ? basename((string) $this->zipPath) : ($this->url ?? 'plugin'); + event(new PluginLog( + 'Plugin installation failed: ' . $this->publicErrorMessage($exception), + 'error', + $label, + $this->userId + )); + } + + /** + * Drop credentials that a process timeout can embed in its command line. + */ + private function publicErrorMessage(Throwable $exception): string + { + return PluginManager::redactHttpUserInfoFromString($exception->getMessage()); + } +} diff --git a/ProcessMaker/Jobs/MoveScriptExecutorToMicroservice.php b/ProcessMaker/Jobs/MoveScriptExecutorToMicroservice.php index 584b7b6f01..1ca70568c9 100644 --- a/ProcessMaker/Jobs/MoveScriptExecutorToMicroservice.php +++ b/ProcessMaker/Jobs/MoveScriptExecutorToMicroservice.php @@ -22,6 +22,6 @@ public function __construct(protected string $uuid) */ public function handle(): void { - \Artisan::call('processmaker:transition-executors --uuid=' . $this->uuid ); + \Artisan::call('processmaker:transition-executors --uuid=' . $this->uuid); } } diff --git a/ProcessMaker/Jobs/ReinstallPlugin.php b/ProcessMaker/Jobs/ReinstallPlugin.php new file mode 100644 index 0000000000..43978150ef --- /dev/null +++ b/ProcessMaker/Jobs/ReinstallPlugin.php @@ -0,0 +1,61 @@ +releaseAfter(30) + ->expireAfter(630), + ]; + } + + public function handle(PluginManager $manager): void + { + event(new PluginLog( + 'Starting queued plugin reinstall...', + 'starting', + $this->pluginName, + $this->userId + )); + + $manager->reinstall($this->pluginName, $this->userId); + } + + public function failed(Throwable $exception): void + { + event(new PluginLog( + 'Plugin reinstall failed: ' . $exception->getMessage(), + 'error', + $this->pluginName, + $this->userId + )); + } +} diff --git a/ProcessMaker/Jobs/RunNayraServiceTask.php b/ProcessMaker/Jobs/RunNayraServiceTask.php index 5cd7583234..b1f7d15bb5 100644 --- a/ProcessMaker/Jobs/RunNayraServiceTask.php +++ b/ProcessMaker/Jobs/RunNayraServiceTask.php @@ -25,7 +25,7 @@ class RunNayraServiceTask implements ShouldQueue /** * Create a new job instance. * - * @param \ProcessMaker\Models\ProcessRequestToken $token + * @param ProcessRequestToken $token * @param array $data */ public function __construct(TokenInterface $token) diff --git a/ProcessMaker/Jobs/SmartInboxExistingTasks.php b/ProcessMaker/Jobs/SmartInboxExistingTasks.php index 371596b7f4..6f50d306ed 100644 --- a/ProcessMaker/Jobs/SmartInboxExistingTasks.php +++ b/ProcessMaker/Jobs/SmartInboxExistingTasks.php @@ -2,22 +2,23 @@ namespace ProcessMaker\Jobs; +use Facades\ProcessMaker\InboxRules\ApplyAction; +use Facades\ProcessMaker\InboxRules\MatchingTasks; +use Facades\ProcessMaker\Models\InboxRule; use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldBeUnique; use Illuminate\Contracts\Queue\ShouldQueue; +use Illuminate\Database\Eloquent\ModelNotFoundException; use Illuminate\Foundation\Bus\Dispatchable; use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\SerializesModels; -use Facades\ProcessMaker\InboxRules\MatchingTasks; -use Facades\ProcessMaker\InboxRules\ApplyAction; -use Facades\ProcessMaker\Models\InboxRule; -use Illuminate\Database\Eloquent\ModelNotFoundException; class SmartInboxExistingTasks implements ShouldQueue { use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; public int $inboxRuleId; + /** * Create a new job instance. */ diff --git a/ProcessMaker/Jobs/TestScript.php b/ProcessMaker/Jobs/TestScript.php index 4a5050d2d0..48ce1cbc82 100644 --- a/ProcessMaker/Jobs/TestScript.php +++ b/ProcessMaker/Jobs/TestScript.php @@ -9,8 +9,13 @@ use Illuminate\Queue\SerializesModels; use ProcessMaker\Enums\ScriptExecutorType; use ProcessMaker\Events\ScriptResponseEvent; +use ProcessMaker\Exception\ConfigurationException; +use ProcessMaker\Facades\ScriptRuntime; use ProcessMaker\Models\Script; use ProcessMaker\Models\User; +use ProcessMaker\ScriptRunners\InProcessPhpRunner; +use ProcessMaker\ScriptRuntime\ScriptExecutionContext; +use ProcessMaker\Services\ScriptExecutionModeResolver; use Throwable; class TestScript implements ShouldQueue @@ -32,6 +37,8 @@ class TestScript implements ShouldQueue protected $nonce; + protected bool $runInProcess; + /** * Create a new job instance to execute a script. * @@ -40,15 +47,25 @@ class TestScript implements ShouldQueue * @param string $code * @param array $data * @param array $configuration + * @param string|null $nonce + * @param bool $runInProcess Use Core Service Task in-process runner (no Docker) */ - public function __construct(Script $script, User $current_user, $code, array $data, array $configuration, $nonce = null) - { + public function __construct( + Script $script, + User $current_user, + $code, + array $data, + array $configuration, + $nonce = null, + bool $runInProcess = false + ) { $this->script = $script; $this->current_user = $current_user; $this->code = $code; $this->data = $data; $this->configuration = $configuration; $this->nonce = $nonce; + $this->runInProcess = $runInProcess; } /** @@ -61,6 +78,15 @@ public function handle() try { // Just set the code but do not save the object (preview only) $this->script->code = $this->code; + + if ($this->runInProcess) { + $response = $this->runInProcessPreview(); + \Log::debug('Response from in-process preview: ' . print_r($response, true)); + $this->sendResponse(200, $response); + + return; + } + $metadata = [ 'nonce' => $this->nonce, 'current_user' => $this->current_user?->id, @@ -79,6 +105,76 @@ public function handle() } } + /** + * Preview using ScriptRuntime modules (default) or bare InProcessPhpRunner. + * + * @return array{output: mixed} + */ + private function runInProcessPreview(): array + { + if (!config('core-service-task.enabled')) { + throw new ConfigurationException( + 'Core Service Task is disabled. Set CORE_SERVICE_TASK_ENABLED=true to enable.' + ); + } + + if (strtolower((string) $this->script->language) !== 'php') { + throw new ConfigurationException('In-process preview only supports PHP scripts'); + } + + $user = User::find($this->script->run_as_user_id); + if (!$user) { + throw new ConfigurationException('A user is required to run scripts'); + } + + $timeout = (int) ($this->script->timeout ?: config('core-service-task.default_timeout', 60)); + $max = (int) config('core-service-task.max_timeout', 300); + if ($max > 0 && $timeout > $max) { + $timeout = $max; + } + $timeout = max(1, $timeout); + + $executionMode = app(ScriptExecutionModeResolver::class)->resolve($this->script); + if ( + $executionMode === Script::EXECUTION_MODE_MODULES + && !$this->canUseModules() + ) { + $executionMode = Script::EXECUTION_MODE_BARE; + } + + if ($executionMode === Script::EXECUTION_MODE_MODULES) { + $context = new ScriptExecutionContext( + data: $this->data, + config: $this->configuration, + user: $user, + tokenId: '', + timeout: $timeout, + scriptId: (int) $this->script->id, + source: 'preview', + ); + $output = ScriptRuntime::run($this->code, $context); + } else { + $output = app(InProcessPhpRunner::class)->run( + $this->code, + $this->data, + $this->configuration, + $timeout, + $user + ); + } + + // Match runScript() response shape expected by the Script Editor UI + return ['output' => $output]; + } + + private function canUseModules(): bool + { + return (bool) ( + $this->current_user?->is_administrator + || $this->current_user?->can('run-in-process-modules') + ); + } + /** * Send a response to the user interface * diff --git a/ProcessMaker/Jobs/TogglePlugin.php b/ProcessMaker/Jobs/TogglePlugin.php new file mode 100644 index 0000000000..63998fe9e7 --- /dev/null +++ b/ProcessMaker/Jobs/TogglePlugin.php @@ -0,0 +1,68 @@ +releaseAfter(30) + ->expireAfter(630), + ]; + } + + public function handle(PluginManager $manager): void + { + event(new PluginLog( + "Starting queued plugin toggle: {$this->pluginName}", + 'starting', + $this->pluginName, + $this->userId + )); + + $enabled = $manager->toggle($this->pluginName, $this->userId); + $state = $enabled ? 'enabled' : 'disabled'; + event(new PluginLog( + "Plugin {$this->pluginName} {$state}", + 'done', + $this->pluginName, + $this->userId + )); + } + + public function failed(Throwable $exception): void + { + event(new PluginLog( + 'Plugin toggle failed: ' . $exception->getMessage(), + 'error', + $this->pluginName, + $this->userId + )); + } +} diff --git a/ProcessMaker/Jobs/UninstallPlugin.php b/ProcessMaker/Jobs/UninstallPlugin.php new file mode 100644 index 0000000000..e024471ab3 --- /dev/null +++ b/ProcessMaker/Jobs/UninstallPlugin.php @@ -0,0 +1,61 @@ +releaseAfter(30) + ->expireAfter(630), + ]; + } + + public function handle(PluginManager $manager): void + { + event(new PluginLog( + "Starting queued plugin uninstallation: {$this->pluginName}", + 'starting', + $this->pluginName, + $this->userId + )); + + $manager->uninstall($this->pluginName, $this->userId); + } + + public function failed(Throwable $exception): void + { + event(new PluginLog( + 'Plugin uninstallation failed: ' . $exception->getMessage(), + 'error', + $this->pluginName, + $this->userId + )); + } +} diff --git a/ProcessMaker/JsonColumnIndex.php b/ProcessMaker/JsonColumnIndex.php index bcdae6e57d..a7322dfe10 100644 --- a/ProcessMaker/JsonColumnIndex.php +++ b/ProcessMaker/JsonColumnIndex.php @@ -23,6 +23,7 @@ public function add(string $table, string $column, string $path) if (!config('database.enable_index_json_columns')) { Log::warning('Indexing JSON columns is disabled. The following index was not created: ' . $sql); + return false; } diff --git a/ProcessMaker/Listeners/HandleActivityCompletedRedirect.php b/ProcessMaker/Listeners/HandleActivityCompletedRedirect.php index 95c9cd253f..91ad7e2a57 100644 --- a/ProcessMaker/Listeners/HandleActivityCompletedRedirect.php +++ b/ProcessMaker/Listeners/HandleActivityCompletedRedirect.php @@ -6,7 +6,6 @@ class HandleActivityCompletedRedirect extends HandleRedirectListener { - /** * Handle the event. */ diff --git a/ProcessMaker/Logging/CreateTestLogger.php b/ProcessMaker/Logging/CreateTestLogger.php index b73409d56d..3ddce5a3c7 100644 --- a/ProcessMaker/Logging/CreateTestLogger.php +++ b/ProcessMaker/Logging/CreateTestLogger.php @@ -11,7 +11,7 @@ class CreateTestLogger * Create a custom Monolog instance. * * @param array $config - * @return \Monolog\Logger + * @return Logger */ public function __invoke(array $config) { diff --git a/ProcessMaker/Managers/PluginManager.php b/ProcessMaker/Managers/PluginManager.php new file mode 100644 index 0000000000..c65763a44f --- /dev/null +++ b/ProcessMaker/Managers/PluginManager.php @@ -0,0 +1,1241 @@ +incompleteInstallPath = null; + + try { + $this->installPluginFiles($repoUrl, $branch, $tag, $userId, $gitUsername, $gitToken); + } catch (Throwable $exception) { + $this->revertFailedInstall($userId); + throw $exception; + } + } + + /** + * Copy or clone the plugin, then run setup. + * + * @throws RuntimeException + */ + private function installPluginFiles(string $repoUrl, ?string $branch, ?string $tag, ?int $userId, ?string $gitUsername, ?string $gitToken): void + { + // Check if this is a local path (CLI only when enabled; API must send https URLs) + $localPath = config('plugin_manager.allow_local_install_path', false) + ? $this->resolveLocalPath($repoUrl) + : null; + $isLocalPath = $localPath !== null; + + if ($isLocalPath) { + // Extract plugin name from path + $repoName = basename($localPath); + $pluginPath = storage_path('plugins') . '/' . $repoName; + + $this->logRunning("Starting installation of plugin from local path: {$repoName}", $repoName, $userId); + + $resolvedLocalPath = realpath($localPath); + $resolvedPluginPath = realpath($pluginPath); + + $alreadyAtTarget = $resolvedLocalPath + && $resolvedPluginPath + && $resolvedLocalPath === $resolvedPluginPath + && !is_link($pluginPath); + + if ($alreadyAtTarget) { + $this->logRunning('Plugin is already installed at the target path', $repoName, $userId); + } else { + if (!is_dir(storage_path('plugins'))) { + mkdir(storage_path('plugins'), 0755, true); + } + + $this->markNewInstallForRollback($pluginPath); + + if (is_link($pluginPath)) { + if (!@unlink($pluginPath)) { + throw new RuntimeException("Failed to remove existing plugin symlink: {$pluginPath}"); + } + } elseif (is_dir($pluginPath)) { + $this->logRunning('Removing existing plugin directory...', $repoName, $userId); + $this->deleteDirectory($pluginPath, $repoName, $userId); + } + + $this->logRunning('Copying plugin from local path...', $repoName, $userId); + $this->copyDirectory($localPath, $pluginPath, $repoName, $userId); + } + } else { + // Extract repository name from URL + $repoName = $this->extractRepoName($repoUrl); + $pluginPath = storage_path('plugins') . '/' . $repoName; + $authenticatedRepoUrl = $this->prepareGitUrl($repoUrl, $gitUsername, $gitToken); + + $this->logRunning("Starting installation of plugin: {$repoName}", $repoName, $userId); + + if (is_dir($pluginPath)) { + $this->updateExistingRepository( + $pluginPath, + $repoName, + $branch, + $tag, + $userId, + $repoUrl, + $authenticatedRepoUrl + ); + } else { + $this->logRunning('Cloning repository...', $repoName, $userId); + $this->markNewInstallForRollback($pluginPath); + $this->cloneRepository($authenticatedRepoUrl, $pluginPath, $repoName, $userId, $repoUrl); + + if ($branch) { + $this->logRunning("Checking out branch: {$branch}", $repoName, $userId); + $this->checkoutBranch($pluginPath, $branch, $repoName, $userId); + } elseif ($tag) { + $this->logRunning("Checking out tag: {$tag}", $repoName, $userId); + $this->checkoutTag($pluginPath, $tag, $repoName, $userId); + } + } + } + + $this->finishPluginSetup($pluginPath, $repoName, $userId); + + $this->logDone('Plugin installed successfully', $repoName, $userId); + } + + /** + * Pull latest changes on the plugin's current git branch and re-run setup. + * + * @throws RuntimeException + */ + public function reinstall(string $pluginName, ?int $userId = null): void + { + $pluginPath = $this->resolveInstalledPluginPath($pluginName); + + if (!is_dir($pluginPath . '/.git')) { + throw new RuntimeException( + "Plugin {$pluginName} is not a git repository and cannot be reinstalled from remote" + ); + } + + $branch = $this->resolveCurrentBranch($pluginPath); + if ($branch === null) { + throw new RuntimeException( + 'Cannot reinstall: plugin is not on a named branch (detached HEAD or unknown state)' + ); + } + + $this->logRunning("Starting reinstall of plugin: {$pluginName} on branch {$branch}", $pluginName, $userId); + $this->updateExistingRepository($pluginPath, $pluginName, $branch, null, $userId); + $this->finishPluginSetup($pluginPath, $pluginName, $userId); + $this->logDone('Plugin reinstalled successfully', $pluginName, $userId); + } + + /** + * Uninstall a plugin. + * + * @param string $pluginName + * @return void + * @throws RuntimeException + */ + public function uninstall(string $pluginName, ?int $userId = null): void + { + $pluginDir = storage_path('plugins'); + $pluginPath = "{$pluginDir}/{$pluginName}"; + $pluginDisabledPath = "{$pluginDir}/_{$pluginName}"; + + if (is_dir($pluginDisabledPath) || is_link($pluginDisabledPath)) { + $pluginPath = $pluginDisabledPath; + } elseif (!is_dir($pluginPath) && !is_link($pluginPath)) { + throw new RuntimeException("Plugin not found: {$pluginName}"); + } + + $this->logRunning("Starting uninstallation of plugin: {$pluginName}", $pluginName, $userId); + + // Run plugin uninstall command if it exists + $uninstallCommand = "{$pluginName}:uninstall"; + $this->logRunning('Running plugin uninstall command...', $pluginName, $userId); + $this->runCommand($uninstallCommand, $pluginName, $userId); + + $this->removeTenantPluginAssetsIfApplicable($pluginPath, $pluginName, $userId); + + $this->logRunning('Removing plugin directory...', $pluginName, $userId); + $this->removePluginPath($pluginPath, $pluginName, $userId); + + $this->refreshRuntimeAfterPluginChange($pluginName, $userId); + + $this->logDone('Plugin uninstalled successfully', $pluginName, $userId); + } + + private function runCommand(string $command, string $pluginName, ?int $userId = null): void + { + // Use absolute path: web SAPI (php-fpm) cwd is not the app root, so plain "artisan" fails. + //TODO: Change artisan_binary() for base_path('artisan') + $artisan = base_path('artisan'); + $result = Process::env($this->artisanEnvironment()) + ->timeout(300) + ->run(array_filter([ + php_binary(), + $artisan, + $command, + ])); + + if (!$result->successful()) { + if (str_contains($result->output(), 'is not defined')) { + $this->logRunning("Plugin does not have a {$command} command", $pluginName, $userId); + } else { + $this->logError("Plugin {$command} command failed. Got output:\n\n{$result->output()}", $pluginName, $userId); + } + } else { + $this->logRunning("Plugin {$command} command output:\n\n{$result->output()}", $pluginName, $userId); + } + } + + /** + * Refresh routes and queue workers after plugin code changes. + */ + private function refreshRuntimeAfterPluginChange(string $pluginName, ?int $userId = null): void + { + $this->rebuildRouteCache($pluginName, $userId); + $this->publishRuntimeReload($pluginName, $userId); + } + + /** + * Rebuild the tenant's route cache in a fresh artisan process. + * + * The current request already booted without the plugin's providers, so + * Artisan::call('route:cache') would persist stale routes. A subprocess + * with TENANT set loads plugins from the tenant storage path and writes + * the cache for that tenant. + */ + private function rebuildRouteCache(string $pluginName, ?int $userId = null): void + { + $this->logRunning('Rebuilding route cache...', $pluginName, $userId); + $this->runCommand('route:cache', $pluginName, $userId); + $this->reloadCachedRoutes(); + } + + private function publishRuntimeReload(string $pluginName, ?int $userId = null): void + { + $this->logRunning('Publishing plugin runtime reload...', $pluginName, $userId); + + try { + $version = app(PluginRuntimeReload::class)->publish($pluginName); + } catch (Throwable $exception) { + $this->logError( + 'Could not publish the plugin runtime reload: ' . $exception->getMessage(), + $pluginName, + $userId + ); + + return; + } + + $this->logRunning( + 'Plugin runtime reload published (version ' . $version . '). Each pod restarts its own supervisor programs.', + $pluginName, + $userId + ); + } + + /** + * Environment variables for artisan subprocesses started from a web request. + */ + private function artisanEnvironment(): array + { + $environment = []; + $tenant = app()->bound('currentTenant') ? app('currentTenant') : null; + + if (config('app.multitenancy') && $tenant) { + $environment['TENANT'] = (string) $tenant->id; + } + + return $environment; + } + + /** + * Load the freshly written route cache into the current process. + * + * Needed for Octane workers that keep the router in memory after + * `route:cache` finishes in a subprocess. + */ + private function reloadCachedRoutes(): void + { + $cachedRoutesPath = app()->getCachedRoutesPath(); + if (!is_file($cachedRoutesPath)) { + return; + } + + require $cachedRoutesPath; + } + + /** + * Install a plugin from a zip file path. + * + * @param string $zipPath Full path to the uploaded zip file + * @return void + * @throws RuntimeException + */ + public function installFromZip(string $zipPath, ?int $userId = null): void + { + if (!file_exists($zipPath)) { + throw new RuntimeException("Zip file not found: {$zipPath}"); + } + + $zip = new \ZipArchive(); + $openResult = $zip->open($zipPath); + + if ($openResult !== true) { + throw new RuntimeException("Failed to open zip file: {$zipPath} (error code: {$openResult})"); + } + + $tmpExtractDir = sys_get_temp_dir() . '/pm-plugin-' . uniqid(); + + if (!mkdir($tmpExtractDir, 0755, true)) { + throw new RuntimeException("Failed to create temp directory: {$tmpExtractDir}"); + } + + $this->incompleteInstallPath = null; + + try { + $zip->extractTo($tmpExtractDir); + $zip->close(); + + // Find the actual plugin directory (zip may have a top-level folder) + $entries = array_diff(scandir($tmpExtractDir), ['.', '..']); + $pluginSourceDir = $tmpExtractDir; + + if (count($entries) === 1) { + $singleEntry = $tmpExtractDir . '/' . reset($entries); + if (is_dir($singleEntry)) { + $pluginSourceDir = $singleEntry; + } + } + + // Validate before copying + $repoName = basename($pluginSourceDir); + $this->validatePlugin($pluginSourceDir, $repoName, $userId); + + $pluginPath = storage_path('plugins') . '/' . $repoName; + + $this->logRunning("Installing plugin from zip: {$repoName}", $repoName, $userId); + // Ensure plugins directory exists + if (!is_dir(storage_path('plugins'))) { + mkdir(storage_path('plugins'), 0755, true); + } + + $this->markNewInstallForRollback($pluginPath); + + // Remove existing plugin directory if present + if (is_dir($pluginPath)) { + $this->deleteDirectory($pluginPath, $repoName, $userId); + } + + // Copy extracted directory to plugins folder + $this->copyDirectory($pluginSourceDir, $pluginPath, $repoName, $userId); + + $this->finishPluginSetup($pluginPath, $repoName, $userId); + + $this->logDone('Plugin installed successfully', $repoName, $userId); + } catch (Throwable $exception) { + $this->revertFailedInstall($userId); + throw $exception; + } finally { + // Always clean up temp directory and zip file + if (is_dir($tmpExtractDir)) { + $this->deleteDirectory($tmpExtractDir, $repoName, $userId); + } + if (file_exists($zipPath)) { + unlink($zipPath); + } + } + } + + /** + * Toggle a plugin enabled/disabled by renaming its directory. + * Disabled plugins have a `_` prefix on their folder name. + * + * @param string $pluginName + * @return bool True if now enabled, false if now disabled + * @throws RuntimeException + */ + public function toggle(string $pluginName, ?int $userId = null): bool + { + $pluginsFolder = storage_path('plugins'); + + // Check if currently enabled (no underscore prefix) + $enabledPath = $pluginsFolder . '/' . $pluginName; + $disabledName = '_' . ltrim($pluginName, '_'); + $disabledPath = $pluginsFolder . '/' . $disabledName; + + if (is_dir($enabledPath)) { + // Disable it + if (!rename($enabledPath, $disabledPath)) { + $this->logError("Failed to disable plugin: {$pluginName}", $pluginName, $userId); + throw new RuntimeException("Failed to disable plugin: {$pluginName}"); + } + + $this->refreshRuntimeAfterPluginChange($pluginName, $userId); + + return false; + } + + if (is_dir($disabledPath)) { + // Enable it + $enabledName = ltrim($pluginName, '_'); + $enabledPath = $pluginsFolder . '/' . $enabledName; + if (!rename($disabledPath, $enabledPath)) { + $this->logError("Failed to enable plugin: {$pluginName}", $pluginName, $userId); + throw new RuntimeException("Failed to enable plugin: {$pluginName}"); + } + + $this->refreshRuntimeAfterPluginChange($enabledName, $userId); + + return true; + } + + $this->logError("Plugin not found: {$pluginName}", $pluginName, $userId); + throw new RuntimeException("Plugin not found: {$pluginName}"); + } + + /** + * List all installed plugins, including disabled ones. + * + * @return array + */ + public function list(): array + { + $pluginsFolder = storage_path('plugins'); + $plugins = []; + + if (!is_dir($pluginsFolder)) { + return $plugins; + } + + $pluginPaths = glob($pluginsFolder . '/*', GLOB_ONLYDIR); + + foreach ($pluginPaths as $pluginPath) { + $dirName = basename($pluginPath); + $isDisabled = str_starts_with($dirName, '_'); + + try { + $plugin = Plugin::fromPath($pluginPath); + $reference = $plugin->getReference(); + $composerJson = $plugin->getComposerJson(); + + $plugins[] = [ + 'name' => $isDisabled ? ltrim($dirName, '_') : $dirName, + 'url' => $this->stripEmbeddedCredentialsFromHttpUrl($plugin->getUrl()), + 'folder' => $dirName, + 'description' => $plugin->getDescription() ?? 'No description', + 'version' => $composerJson['version'] ?? null, + 'branch' => $reference, + 'source' => $plugin->isGitRepository() ? 'git' : 'filesystem', + 'enabled' => !$isDisabled ? 'Enabled' : 'Disabled', + ]; + } catch (\Exception $e) { + // Skip invalid plugins + continue; + } + } + + return $plugins; + } + + /** + * Extract repository name from URL. + * + * @param string $repoUrl + * @return string + */ + protected function extractRepoName(string $repoUrl): string + { + // Remove only a single trailing '.git' or '/' if present + $repoUrl = preg_replace('/(\.git|\/)$/', '', $repoUrl); + + // Extract name from URL + if (preg_match('#/([^/]+)$#', $repoUrl, $matches)) { + return $matches[1]; + } + + throw new RuntimeException("Could not extract repository name from URL: {$repoUrl}"); + } + + /** + * Clone a repository. + * + * @param string $repoUrl + * @param string $destination + * @return void + * @throws RuntimeException + */ + protected function cloneRepository( + string $repoUrl, + string $destination, + string $repoName, + ?int $userId = null, + ?string $canonicalRepoUrl = null, + ): void { + try { + $process = Process::timeout(3600)->run(['git', 'clone', $repoUrl, $destination]); + } catch (ProcessTimedOutException) { + throw new RuntimeException("Git clone exceeded the timeout while installing {$repoName}."); + } + + if (!$process->successful()) { + $stderr = self::redactHttpUserInfoFromString($process->errorOutput()); + $this->logError('Failed to clone repository: ' . $stderr, $repoName, $userId); + throw new RuntimeException('Failed to clone repository: ' . $stderr); + } + + $cleanOrigin = $this->stripEmbeddedCredentialsFromHttpUrl($canonicalRepoUrl ?? $repoUrl); + Process::path($destination)->timeout(60)->run(['git', 'remote', 'set-url', 'origin', $cleanOrigin]); + } + + protected function finishPluginSetup(string $pluginPath, string $repoName, ?int $userId = null): void + { + $this->logRunning('Validating plugin...', $repoName, $userId); + $this->validatePlugin($pluginPath, $repoName, $userId); + + $this->logRunning('Installing dependencies...', $repoName, $userId); + $this->runComposerInstall($pluginPath, $repoName, $userId); + + $installCommand = "{$repoName}:install"; + $this->logRunning('installCommand: ' . $installCommand, $repoName, $userId); + $this->logRunning('Running plugin install command...', $repoName, $userId); + $this->runCommand($installCommand, $repoName, $userId); + + $this->publishTenantPluginAssetsIfApplicable($pluginPath, $repoName, $userId); + + // Setup finished. Keep the directory even if a later route refresh fails. + $this->incompleteInstallPath = null; + + $this->refreshRuntimeAfterPluginChange($repoName, $userId); + } + + protected function publishTenantPluginAssetsIfApplicable( + string $pluginPath, + string $pluginName, + ?int $userId = null, + ): void { + if (!$this->shouldPublishTenantPluginAssets()) { + return; + } + + try { + $publisher = app(TenantPluginAssetPublisher::class); + if ($publisher->publishPlugin($pluginPath)) { + $this->logRunning('Tenant plugin public assets published.', $pluginName, $userId); + } else { + $this->logRunning( + 'Tenant plugin public assets skipped (no public/ directory).', + $pluginName, + $userId + ); + } + } catch (Throwable $exception) { + $this->logError( + 'Failed to publish tenant plugin assets: ' . $exception->getMessage(), + $pluginName, + $userId + ); + } + } + + protected function removeTenantPluginAssetsIfApplicable( + string $pluginPath, + string $pluginName, + ?int $userId = null, + ): void { + if (!$this->shouldPublishTenantPluginAssets()) { + return; + } + + try { + app(TenantPluginAssetPublisher::class)->removePublishedPlugin($pluginPath); + $this->logRunning('Tenant plugin public assets removed.', $pluginName, $userId); + } catch (Throwable $exception) { + $this->logError( + 'Failed to remove tenant plugin public assets: ' . $exception->getMessage(), + $pluginName, + $userId + ); + } + } + + private function shouldPublishTenantPluginAssets(): bool + { + if (!config('app.multitenancy')) { + return false; + } + + if (!filter_var(config('plugin_manager.tenant_plugin_assets_enabled', true), FILTER_VALIDATE_BOOL)) { + return false; + } + + return PluginVendorAssets::currentTenantId() !== null; + } + + /** + * Remember a plugin path that did not exist before this install attempt. + */ + private function markNewInstallForRollback(string $pluginPath): void + { + if (is_dir($pluginPath) || is_link($pluginPath)) { + return; + } + + $this->incompleteInstallPath = $pluginPath; + } + + /** + * Remove a plugin directory left behind by a failed install. + */ + public function revertFailedInstall(?int $userId = null): void + { + $pluginPath = $this->incompleteInstallPath; + $this->incompleteInstallPath = null; + + if ($pluginPath === null || (!is_dir($pluginPath) && !is_link($pluginPath))) { + return; + } + + $pluginName = basename($pluginPath); + $this->logRunning( + 'Installation failed. Removing the incomplete plugin directory.', + $pluginName, + $userId + ); + + try { + $this->removePluginPath($pluginPath, $pluginName, $userId); + } catch (Throwable $cleanupException) { + $this->logError( + 'Could not remove the incomplete plugin directory: ' . $cleanupException->getMessage(), + $pluginName, + $userId + ); + } + } + + protected function resolveInstalledPluginPath(string $pluginName): string + { + $pluginDir = storage_path('plugins'); + $pluginPath = "{$pluginDir}/{$pluginName}"; + $pluginDisabledPath = "{$pluginDir}/_{$pluginName}"; + + if (is_dir($pluginDisabledPath) || is_link($pluginDisabledPath)) { + return $pluginDisabledPath; + } + + if (is_dir($pluginPath) || is_link($pluginPath)) { + return $pluginPath; + } + + throw new RuntimeException("Plugin not found: {$pluginName}"); + } + + protected function resolveCurrentBranch(string $pluginPath): ?string + { + $process = Process::path($pluginPath)->run(['git', 'rev-parse', '--abbrev-ref', 'HEAD']); + + if (!$process->successful()) { + return null; + } + + $branch = trim($process->output()); + + if ($branch === '' || $branch === 'HEAD') { + return null; + } + + return $branch; + } + + /** + * Fetch remotes and check out the requested ref when the plugin clone already exists. + */ + protected function updateExistingRepository( + string $pluginPath, + string $repoName, + ?string $branch, + ?string $tag, + ?int $userId = null, + ?string $canonicalRepoUrl = null, + ?string $authenticatedRepoUrl = null, + ): void { + $this->logRunning('Plugin directory exists, fetching from remote...', $repoName, $userId); + + $restoreOriginUrl = $canonicalRepoUrl !== null + ? $this->stripEmbeddedCredentialsFromHttpUrl($canonicalRepoUrl) + : null; + + $previousOriginProcess = Process::path($pluginPath)->timeout(60)->run([ + 'git', 'remote', 'get-url', 'origin', + ]); + $previousOrigin = $previousOriginProcess->successful() + ? trim($previousOriginProcess->output()) + : ''; + + $temporaryAuth = $authenticatedRepoUrl !== null + && $restoreOriginUrl !== null + && str_starts_with($authenticatedRepoUrl, 'https://'); + + if ($temporaryAuth) { + Process::path($pluginPath)->timeout(60)->run([ + 'git', 'remote', 'set-url', 'origin', $authenticatedRepoUrl, + ]); + } + + try { + $this->fetchRepository($pluginPath, $repoName, $userId); + + if ($tag) { + $this->logRunning("Checking out tag: {$tag}", $repoName, $userId); + $this->checkoutTag($pluginPath, $tag, $repoName, $userId); + + return; + } + + $branchToCheckout = $branch ?? $this->resolveRemoteDefaultBranch($pluginPath, $repoName, $userId); + $this->logRunning("Checking out branch: {$branchToCheckout}", $repoName, $userId); + $this->checkoutBranch($pluginPath, $branchToCheckout, $repoName, $userId); + $this->logRunning("Pulling latest changes for branch {$branchToCheckout}...", $repoName, $userId); + $this->pullRepository($pluginPath, $repoName, $userId, $branchToCheckout); + } finally { + $cleanOrigin = $restoreOriginUrl ?? ''; + if ($cleanOrigin === '' && $previousOrigin !== '') { + $cleanOrigin = $this->stripEmbeddedCredentialsFromHttpUrl($previousOrigin); + } + + if ($cleanOrigin !== '' && is_dir($pluginPath . '/.git')) { + Process::path($pluginPath)->timeout(60)->run([ + 'git', 'remote', 'set-url', 'origin', $cleanOrigin, + ]); + } + } + } + + protected function fetchRepository(string $pluginPath, string $repoName, ?int $userId = null): void + { + $process = Process::path($pluginPath)->timeout(300)->run(['git', 'fetch', '--all', '--tags']); + + if (!$process->successful()) { + $stderr = self::redactHttpUserInfoFromString($process->errorOutput()); + $this->logError('Failed to fetch repository: ' . $stderr, $repoName, $userId); + throw new RuntimeException('Failed to fetch repository: ' . $stderr); + } + } + + protected function resolveRemoteDefaultBranch(string $pluginPath, string $repoName, ?int $userId = null): string + { + $process = Process::path($pluginPath)->timeout(60)->run(['git', 'rev-parse', '--abbrev-ref', 'origin/HEAD']); + + if ($process->successful()) { + $ref = trim($process->output()); + if (str_starts_with($ref, 'origin/')) { + return substr($ref, strlen('origin/')); + } + if ($ref !== '' && $ref !== 'HEAD') { + return $ref; + } + } + + foreach (['main', 'master', 'develop'] as $fallbackBranch) { + $check = Process::path($pluginPath)->run([ + 'git', + 'show-ref', + '--verify', + '--quiet', + "refs/remotes/origin/{$fallbackBranch}", + ]); + if ($check->successful()) { + return $fallbackBranch; + } + } + + $this->logError('Could not determine default branch for plugin repository', $repoName, $userId); + throw new RuntimeException('Could not determine default branch for plugin repository'); + } + + protected function pullRepository(string $pluginPath, string $repoName, ?int $userId = null, ?string $branch = null): void + { + $command = $branch === null + ? ['git', 'pull'] + : ['git', 'pull', 'origin', $branch]; + + $process = Process::path($pluginPath)->timeout(300)->run($command); + + if (!$process->successful()) { + $stderr = self::redactHttpUserInfoFromString($process->errorOutput()); + $this->logError('Failed to pull repository: ' . $stderr, $repoName, $userId); + throw new RuntimeException('Failed to pull repository: ' . $stderr); + } + } + + /** + * Checkout a branch. + * + * @param string $pluginPath + * @param string $branch + * @return void + * @throws RuntimeException + */ + protected function checkoutBranch(string $pluginPath, string $branch, string $repoName, ?int $userId = null): void + { + $process = Process::path($pluginPath)->timeout(120)->run(['git', 'checkout', $branch]); + + if (!$process->successful()) { + $stderr = self::redactHttpUserInfoFromString($process->errorOutput()); + $this->logError('Failed to checkout branch: ' . $stderr, $repoName, $userId); + throw new RuntimeException("Failed to checkout branch {$branch}: " . $stderr); + } + } + + /** + * Checkout a tag. + * + * @param string $pluginPath + * @param string $tag + * @return void + * @throws RuntimeException + */ + protected function checkoutTag(string $pluginPath, string $tag, string $repoName, ?int $userId = null): void + { + $process = Process::path($pluginPath)->timeout(120)->run(['git', 'checkout', "tags/{$tag}"]); + + if (!$process->successful()) { + $stderr = self::redactHttpUserInfoFromString($process->errorOutput()); + $this->logError('Failed to checkout tag: ' . $stderr, $repoName, $userId); + throw new RuntimeException("Failed to checkout tag {$tag}: " . $stderr); + } + } + + /** + * Validate that the plugin has a valid composer.json structure. + * + * @param string $pluginPath + * @return void + * @throws RuntimeException + */ + protected function validatePlugin(string $pluginPath, string $repoName, ?int $userId = null): void + { + $composerJsonPath = $pluginPath . '/composer.json'; + + if (!file_exists($composerJsonPath)) { + $this->logError('composer.json not found in plugin: ' . $pluginPath, $repoName, $userId); + throw new RuntimeException("composer.json not found in plugin: {$pluginPath}"); + } + + $content = file_get_contents($composerJsonPath); + $composerJson = json_decode($content, true); + + if (json_last_error() !== JSON_ERROR_NONE) { + $this->logError('Invalid JSON in composer.json at ' . $composerJsonPath . ': ' . json_last_error_msg(), $repoName, $userId); + throw new RuntimeException( + "Invalid JSON in composer.json at {$composerJsonPath}: " . json_last_error_msg() + ); + } + + // Check if require or require-dev is an empty array ([]), if so change for empty object and save to file + $changed = false; + if (Arr::get($composerJson, 'require', []) === []) { + $composerJson['require'] = new \stdClass(); + $changed = true; + } + if (Arr::get($composerJson, 'require-dev', []) === []) { + $composerJson['require-dev'] = new \stdClass(); + $changed = true; + } + if ($changed) { + file_put_contents($composerJsonPath, json_encode($composerJson, JSON_PRETTY_PRINT)); + } + + // Check PSR-4 autoload namespace + $psr4Namespaces = Arr::get($composerJson, 'autoload.psr-4', []); + + if (empty($psr4Namespaces)) { + $this->logError('Plugin must have PSR-4 autoload configuration in composer.json', $repoName, $userId); + throw new RuntimeException('Plugin must have PSR-4 autoload configuration in composer.json'); + } + + $valid = false; + foreach ($psr4Namespaces as $namespace => $path) { + $prefixes = [ + 'ProcessMaker\\Plugins\\', + 'ProcessMaker\\Package\\', + 'ProcessMaker\\Packages\\', + ]; + $valid = (bool) collect($prefixes)->first(fn ($prefix) => str_starts_with($namespace, $prefix)); + if ($valid) { + break; + } + // if (str_starts_with($namespace, 'ProcessMaker\\Plugins\\')) { + // $valid = true; + // break; + // } + // if (str_starts_with($namespace, 'ProcessMaker\\Package\\')) { + // $valid = true; + // break; + // } + // //TODO: Check if needs to be package or packages or both + // if (str_starts_with($namespace, 'ProcessMaker\\Packages\\')) { + // $valid = true; + // break; + // } + } + + if (!$valid) { + $this->logError('Plugin PSR-4 namespace must start with ' . implode(', ', $prefixes) . '. Found: ' . implode(', ', array_keys($psr4Namespaces)), $repoName, $userId); + throw new RuntimeException( + "Plugin PSR-4 namespace must start with 'ProcessMaker\\Plugins\\'. Found: " . + implode(', ', array_keys($psr4Namespaces)) + ); + } + } + + /** + * Fetch branches from the repository. + * + * @param string $pluginPath + * @return void + * @throws RuntimeException + */ + protected function fetchBranches(string $pluginPath, string $repoName, ?int $userId = null): void + { + $process = Process::path($pluginPath)->timeout(60)->run(['git', 'branch', '-a']); + if (!$process->successful()) { + $stderr = self::redactHttpUserInfoFromString($process->errorOutput()); + $this->logError('Failed to fetch branches: ' . $stderr, $repoName, $userId); + throw new RuntimeException('Failed to fetch branches: ' . $stderr); + } + } + + /** + * Run composer install in the plugin directory. + * + * @param string $pluginPath + * @return void + * @throws RuntimeException + */ + protected function runComposerInstall(string $pluginPath, string $repoName, ?int $userId = null): void + { + set_time_limit(0); + $process = Process::path($pluginPath) + ->forever() + ->env([ + 'PATH' => '/opt/homebrew/bin:/usr/local/bin:/usr/bin:/bin:' . (getenv('PATH') ?: ''), + ]) + ->run(['composer', 'install', '--no-interaction']); + + if (!$process->successful()) { + $this->logError('Failed to run composer install: ' . $process->errorOutput(), $repoName, $userId); + throw new RuntimeException('Failed to run composer install: ' . $process->errorOutput()); + } + } + + /** + * Copy a directory recursively. + * + * @param string $source + * @param string $destination + * @return void + * @throws RuntimeException + */ + protected function copyDirectory(string $source, string $destination, string $repoName, ?int $userId = null): void + { + if (!is_dir($destination)) { + mkdir($destination, 0755, true); + } + + $files = array_diff(scandir($source), ['.', '..']); + + foreach ($files as $file) { + $srcPath = $source . '/' . $file; + $destPath = $destination . '/' . $file; + + if (is_dir($srcPath)) { + $this->copyDirectory($srcPath, $destPath, $repoName, $userId); + } else { + if (!copy($srcPath, $destPath)) { + $this->logError('Failed to copy file: ' . $srcPath . ' to ' . $destPath, $repoName, $userId); + throw new RuntimeException("Failed to copy file: {$srcPath} to {$destPath}"); + } + } + } + } + + /** + * Remove a plugin from storage/plugins without deleting symlink targets. + * + * @throws RuntimeException + */ + protected function removePluginPath(string $pluginPath, string $pluginName, ?int $userId = null): void + { + if (is_link($pluginPath)) { + $this->logRunning('Removing plugin symlink (source directory is kept)...', $pluginName, $userId); + if (!@unlink($pluginPath)) { + $this->logError('Failed to remove plugin symlink: ' . $pluginPath, $pluginName, $userId); + throw new RuntimeException("Failed to remove plugin symlink: {$pluginPath}"); + } + + return; + } + + $this->deleteDirectory($pluginPath, $pluginName, $userId); + } + + /** + * Delete a directory recursively. + * + * @param string $directory + * @return void + * @throws RuntimeException + */ + protected function deleteDirectory(string $directory, string $repoName, ?int $userId = null): void + { + if (is_link($directory)) { + if (!@unlink($directory)) { + $this->logError('Failed to remove symlink: ' . $directory, $repoName, $userId); + throw new RuntimeException("Failed to remove symlink: {$directory}"); + } + + return; + } + + if (!is_dir($directory)) { + return; + } + + $files = array_diff(scandir($directory), ['.', '..']); + + foreach ($files as $file) { + $path = $directory . '/' . $file; + if (is_dir($path) && !is_link($path)) { + $this->deleteDirectory($path, $repoName, $userId); + } else { + if (!@unlink($path) && file_exists($path)) { + $this->logError('Failed to delete file: ' . $path, $repoName, $userId); + throw new RuntimeException("Failed to delete file: {$path}"); + } + } + } + + clearstatcache(true, $directory); + + if (count(array_diff(scandir($directory), ['.', '..'])) !== 0) { + $this->logError('Directory not empty after attempting to delete: ' . $directory, $repoName, $userId); + throw new RuntimeException("Directory not empty after attempting to delete: {$directory}"); + } + + if (!@rmdir($directory) && is_dir($directory)) { + $this->logError('Failed to delete directory: ' . $directory, $repoName, $userId); + throw new RuntimeException("Failed to delete directory: {$directory}"); + } + } + + /** + * Resolve and check if the given path is a valid local directory. + * + * @param string $path + * @return string|null Returns the resolved absolute path if it exists, null otherwise + */ + protected function resolveLocalPath(string $path): ?string + { + // Check if it's already an absolute path + if (is_dir($path)) { + return realpath($path); + } + + // Try as relative path from current working directory + $resolved = realpath($path); + if ($resolved && is_dir($resolved)) { + return $resolved; + } + + // Try as relative path from base path + $basePath = base_path($path); + $resolved = realpath($basePath); + if ($resolved && is_dir($resolved)) { + return $resolved; + } + + return null; + } + + /** + * Prepare HTTPS git URL: optional UI username/password, else GITHUB_TOKEN for github.com. + */ + protected function prepareGitUrl( + string $url, + ?string $username = null, + ?string $token = null, + ): string { + if (!str_starts_with($url, 'https://')) { + return $url; + } + + $hasUser = $username !== null && trim($username) !== ''; + $hasToken = $token !== null && $token !== ''; + + if ($hasUser || $hasToken) { + $parts = parse_url($url); + if ($parts === false || !isset($parts['scheme'], $parts['host'])) { + return $url; + } + + $user = rawurlencode((string) $username); + $pass = $hasToken ? rawurlencode((string) $token) : ''; + + $auth = $pass !== '' ? "{$user}:{$pass}@" : "{$user}@"; + + $path = ($parts['path'] ?? '') . (isset($parts['query']) ? '?' . $parts['query'] : ''); + $fragment = isset($parts['fragment']) ? '#' . $parts['fragment'] : ''; + + return 'https://' . $auth . $parts['host'] + . (isset($parts['port']) ? ':' . $parts['port'] : '') + . $path + . $fragment; + } + + $token = env('GITHUB_TOKEN'); + if ($token && str_starts_with($url, 'https://github.com/')) { + return str_replace('https://github.com/', "https://{$token}@github.com/", $url); + } + + return $url; + } + + /** + * Remove user:pass from an HTTPS URL (for restoring git remote after authenticated fetch). + */ + protected function stripEmbeddedCredentialsFromHttpUrl(string $url): string + { + if (!str_starts_with($url, 'https://')) { + return $url; + } + + $parts = parse_url($url); + if ($parts === false || !isset($parts['scheme'], $parts['host'])) { + return $url; + } + + $path = ($parts['path'] ?? '') . (isset($parts['query']) ? '?' . $parts['query'] : ''); + $fragment = isset($parts['fragment']) ? '#' . $parts['fragment'] : ''; + + return 'https://' . $parts['host'] + . (isset($parts['port']) ? ':' . $parts['port'] : '') + . $path + . $fragment; + } + + /** + * Remove embedded HTTP(S) credentials from log and error text (user:pass or token-only). + */ + public static function redactHttpUserInfoFromString(string $message): string + { + $message = preg_replace('#://[^/\s:@]+:[^/\s@]+@#', '://', $message) ?? $message; + $message = preg_replace('#://[^/\s@]+@#', '://', $message) ?? $message; + + return $message; + } + + /** + * Log a running status message. + * + * @param string $message + * @param string $pluginName + * @return void + */ + protected function logRunning(string $message, string $pluginName, ?int $userId = null): void + { + $this->log($message, 'running', $pluginName, $userId); + } + + /** + * Log an error status message. + * + * @param string $message + * @param string $pluginName + * @return void + */ + protected function logError(string $message, string $pluginName, ?int $userId = null): void + { + $this->log($message, 'error', $pluginName, $userId); + } + + /** + * Log a done status message. + * + * @param string $message + * @param string $pluginName + * @return void + */ + protected function logDone(string $message, string $pluginName, ?int $userId = null): void + { + $this->log($message, 'done', $pluginName, $userId); + } + + /** + * Log a plugin event. + * + * @param string $message + * @param string $type + * @param string $pluginName + * @return void + */ + protected function log(string $message, string $type, string $pluginName, ?int $userId = null): void + { + $message = self::redactHttpUserInfoFromString($message); + + if ($type === 'error') { + \Log::error($message); + } else { + \Log::info($message); + } + + if ($userId) { + event(new PluginLog($message, $type, $pluginName, $userId)); + } + } +} diff --git a/ProcessMaker/Managers/ScreenBuilderManager.php b/ProcessMaker/Managers/ScreenBuilderManager.php index fb4e1d88e2..c84c14dd9b 100644 --- a/ProcessMaker/Managers/ScreenBuilderManager.php +++ b/ProcessMaker/Managers/ScreenBuilderManager.php @@ -3,6 +3,7 @@ namespace ProcessMaker\Managers; use Illuminate\Foundation\PackageManifest; +use ProcessMaker\Support\PluginVendorAssets; class ScreenBuilderManager { @@ -42,7 +43,7 @@ public function getScripts() { $scripts = []; foreach ($this->javascriptRegistry as $script) { - $path = public_path($script); + $path = $this->filesystemPathForWebScript($script); $time = file_exists($path) ? filemtime($path) : 0; $scripts[] = $script . ($time ? "?t=$time" : ''); } @@ -52,8 +53,16 @@ public function getScripts() public function addPackageScripts($type = 'DISPLAY') { - // Depending of the form type we load the correct file with the components to display - $extensionsFile = 'screen-builder-' . strtolower($type) . '-components.js'; + $this->addComposerPackageScripts($type); + $this->addStoragePluginScripts($type); + } + + /** + * Load screen-builder extension scripts from Composer packages under vendor/processmaker/packages. + */ + private function addComposerPackageScripts(string $type): void + { + $extensionsFile = $this->screenBuilderExtensionsFile($type); $directories = glob('vendor/processmaker/packages/*', GLOB_ONLYDIR); $installed = app(PackageManifest::class)->list(); @@ -64,10 +73,102 @@ public function addPackageScripts($type = 'DISPLAY') })); foreach ($directories as $directory) { $extensionsFullName = $directory . '/js/' . $extensionsFile; - $files = glob($extensionsFullName); - if (count($files) > 0) { - $this->addScript('/' . $files[0]); + foreach (glob($extensionsFullName) ?: [] as $file) { + $publicPath = $this->normalizePublicScriptPath($file); + if ($publicPath !== null) { + $this->addScript($publicPath); + } } } } + + /** + * Load screen-builder extension scripts for enabled plugins in storage/plugins when + * assets were published to public/vendor/processmaker/packages/{name}/js/. + */ + private function addStoragePluginScripts(string $type): void + { + $extensionsFile = $this->screenBuilderExtensionsFile($type); + $pluginsFolder = storage_path('plugins'); + + if (!is_dir($pluginsFolder)) { + return; + } + + foreach (glob($pluginsFolder . '/*', GLOB_ONLYDIR) ?: [] as $pluginPath) { + $folderName = basename($pluginPath); + if (str_starts_with($folderName, '_')) { + continue; + } + + $packageName = PluginVendorAssets::packageNameFromPluginPath($pluginPath, $folderName); + $webPath = $this->publishedPluginScriptWebPath($packageName, $extensionsFile); + + if ($webPath !== null) { + $this->addScript($webPath); + } + } + } + + /** + * Prefer tenant-published plugin assets when available, else global public/vendor. + */ + private function publishedPluginScriptWebPath(string $packageName, string $extensionsFile): ?string + { + $relativeJs = '/js/' . $extensionsFile; + + if (PluginVendorAssets::isEnabled()) { + $tenantId = PluginVendorAssets::currentTenantId(); + $tenantFile = PluginVendorAssets::tenantPublicRoot($tenantId, $packageName) . $relativeJs; + if (is_file($tenantFile)) { + return PluginVendorAssets::webPath($tenantId, $packageName, $relativeJs); + } + } + + $globalWeb = '/vendor/processmaker/packages/' . $packageName . $relativeJs; + if (is_file(public_path(ltrim($globalWeb, '/')))) { + return $globalWeb; + } + + return null; + } + + private function filesystemPathForWebScript(string $webScript): string + { + $pathOnly = explode('?', $webScript, 2)[0]; + + return public_path(ltrim($pathOnly, '/')); + } + + private function screenBuilderExtensionsFile(string $type): string + { + return 'screen-builder-' . strtolower($type) . '-components.js'; + } + + /** + * Convert a filesystem path to a public-relative script path with a leading slash. + */ + private function normalizePublicScriptPath(string $filesystemPath): ?string + { + $path = str_replace('\\', '/', $filesystemPath); + if ($path === '') { + return null; + } + + if (str_starts_with($path, '/')) { + $publicRoot = str_replace('\\', '/', realpath(public_path()) ?: public_path()); + if (str_starts_with($path, $publicRoot)) { + return '/' . ltrim(substr($path, strlen($publicRoot)), '/'); + } + + $baseRoot = str_replace('\\', '/', realpath(base_path()) ?: base_path()); + if (str_starts_with($path, $baseRoot)) { + return '/' . ltrim(substr($path, strlen($baseRoot)), '/'); + } + + return null; + } + + return '/' . ltrim($path, '/'); + } } diff --git a/ProcessMaker/Managers/TenantPluginAssetPublisher.php b/ProcessMaker/Managers/TenantPluginAssetPublisher.php new file mode 100644 index 0000000000..dfbdba19a1 --- /dev/null +++ b/ProcessMaker/Managers/TenantPluginAssetPublisher.php @@ -0,0 +1,110 @@ + $tenantId, + 'plugin_path' => $pluginPath, + ]); + + return false; + } + + $packageName = PluginVendorAssets::packageNameFromPluginPath($pluginPath); + $destination = PluginVendorAssets::tenantPublicRoot($tenantId, $packageName); + + $this->copyPublicDirectory($source, $destination); + + Log::info('Tenant plugin assets published', [ + 'tenant_id' => $tenantId, + 'package' => $packageName, + 'source' => $source, + 'destination' => $destination, + ]); + + return true; + } + + /** + * @return array plugin folder name => published (true) or skipped (false) + */ + public function publishAllInstalledPlugins(?int $tenantId = null): array + { + $tenantId = PluginVendorAssets::requireTenantId($tenantId); + $results = []; + $pluginsFolder = storage_path('plugins'); + + if (!is_dir($pluginsFolder)) { + return $results; + } + + foreach (glob($pluginsFolder . '/*', GLOB_ONLYDIR) ?: [] as $pluginPath) { + $folderName = basename($pluginPath); + if (str_starts_with($folderName, '_')) { + continue; + } + + try { + $results[$folderName] = $this->publishPlugin($pluginPath, $tenantId); + } catch (Throwable $exception) { + Log::error('Tenant plugin assets: failed to publish plugin', [ + 'tenant_id' => $tenantId, + 'plugin_path' => $pluginPath, + 'message' => $exception->getMessage(), + ]); + $results[$folderName] = false; + } + } + + return $results; + } + + public function removePublishedPlugin(string $pluginPath, ?int $tenantId = null): void + { + $tenantId = PluginVendorAssets::requireTenantId($tenantId); + $packageName = PluginVendorAssets::packageNameFromPluginPath($pluginPath); + $destination = PluginVendorAssets::tenantPublicRoot($tenantId, $packageName); + + if (!File::isDirectory($destination)) { + return; + } + + File::deleteDirectory($destination); + + Log::info('Tenant plugin assets removed', [ + 'tenant_id' => $tenantId, + 'package' => $packageName, + 'destination' => $destination, + ]); + } + + private function copyPublicDirectory(string $source, string $destination): void + { + if (File::isDirectory($destination)) { + File::deleteDirectory($destination); + } + + File::ensureDirectoryExists(dirname($destination)); + File::copyDirectory($source, $destination); + } +} diff --git a/ProcessMaker/Mcp/DataConnectors/CollectionCatalog.php b/ProcessMaker/Mcp/DataConnectors/CollectionCatalog.php new file mode 100644 index 0000000000..aae3759c94 --- /dev/null +++ b/ProcessMaker/Mcp/DataConnectors/CollectionCatalog.php @@ -0,0 +1,367 @@ + + */ + private const ORDER_COLUMNS = ['name', 'updated_at', 'created_at', 'id']; + + public function actor(?Authenticatable $user): User + { + return McpActor::resolve($user, 'collection'); + } + + /** + * @param array $input + * @return array + */ + public function list(User $user, array $input): array + { + $this->assertPackage(); + McpActor::assertCan($user, self::VIEW); + $filters = validator($input, [ + 'filter' => 'nullable|string|max:255', + 'page' => 'nullable|integer|min:1', + 'per_page' => 'nullable|integer|min:1|max:50', + 'order_by' => 'nullable|in:' . implode(',', self::ORDER_COLUMNS), + 'order_direction' => 'nullable|in:asc,desc', + ])->validate(); + + $page = $this->filteredQuery($filters) + ->orderBy($filters['order_by'] ?? 'name', $filters['order_direction'] ?? 'asc') + ->paginate( + $filters['per_page'] ?? self::DEFAULT_PER_PAGE, + ['id', 'name', 'description', 'create_screen_id', 'read_screen_id', 'update_screen_id', 'type', 'updated_at'], + 'page', + $filters['page'] ?? 1, + ); + + return [ + 'data' => collect($page->items())->map(fn (Collection $row): array => $this->summary($row))->all(), + 'meta' => [ + 'current_page' => $page->currentPage(), + 'last_page' => $page->lastPage(), + 'per_page' => $page->perPage(), + 'total' => $page->total(), + ], + ]; + } + + /** + * @return array + */ + public function find(User $user, int $id): array + { + $this->assertPackage(); + McpActor::assertCan($user, self::VIEW); + + return $this->detail(Collection::query()->findOrFail($id)); + } + + /** + * @param array $input + * @return array + */ + public function create(User $user, array $input): array + { + $this->assertPackage(); + McpActor::assertCan($user, self::CREATE); + $validated = validator($input, Collection::rules())->validate(); + + $collection = new Collection(); + $collection->fill($validated); + if (isset($input['type'])) { + $collection->type = $input['type']; + } + $collection->created_by_id = $user->id; + $collection->updated_by_id = $user->id; + $collection->saveOrFail(); + + return $this->detail($collection->refresh()); + } + + /** + * @param array $input + * @return array + */ + public function update(User $user, array $input): array + { + $this->assertPackage(); + McpActor::assertCan($user, self::EDIT); + $validated = validator($input, array_merge([ + 'id' => 'required|integer|min:1', + ], $this->updateRules($input)))->validate(); + + $collection = Collection::query()->findOrFail((int) $validated['id']); + $attributes = array_intersect_key($validated, array_flip([ + 'name', + 'description', + 'create_screen_id', + 'read_screen_id', + 'update_screen_id', + 'custom_title', + 'obfuscate', + 'signal_create', + 'signal_update', + 'signal_delete', + 'include_records_on_export', + 'type', + ])); + if ($attributes === []) { + throw ValidationException::withMessages([ + 'collection' => 'Provide at least one field to update.', + ]); + } + + $collection->fill($attributes); + $collection->updated_by_id = $user->id; + $collection->saveOrFail(); + + return $this->detail($collection->refresh()); + } + + /** + * @param array $input + * @return array + */ + public function listRecords(User $user, array $input): array + { + $this->assertPackage(); + McpActor::assertCan($user, self::VIEW); + $validated = validator($input, [ + 'collection_id' => 'required|integer|min:1', + 'page' => 'nullable|integer|min:1', + 'per_page' => 'nullable|integer|min:1|max:50', + ])->validate(); + + $collection = Collection::query()->findOrFail((int) $validated['collection_id']); + $this->assertCanOnCollection($user, $collection, 'view'); + + $page = Record::fromCollection($collection) + ->orderBy('id') + ->paginate( + $validated['per_page'] ?? self::DEFAULT_PER_PAGE, + ['*'], + 'page', + $validated['page'] ?? 1, + ); + + return [ + 'collection_id' => $collection->id, + 'data' => collect($page->items())->map(fn (Record $record): array => $this->recordSummary($record))->all(), + 'meta' => [ + 'current_page' => $page->currentPage(), + 'last_page' => $page->lastPage(), + 'per_page' => $page->perPage(), + 'total' => $page->total(), + ], + ]; + } + + /** + * @param array $input + * @return array + */ + public function createRecords(User $user, array $input): array + { + $this->assertPackage(); + McpActor::assertCan($user, self::VIEW); + $validated = validator($input, [ + 'collection_id' => 'required|integer|min:1', + 'records' => 'nullable|array|max:100', + 'records.*' => 'array', + 'data' => 'nullable|array', + ])->validate(); + + $rows = $validated['records'] ?? null; + if ($rows === null && isset($validated['data'])) { + $rows = [$validated['data']]; + } + if ($rows === null || $rows === []) { + throw ValidationException::withMessages([ + 'records' => 'Provide records (array of objects) or data (single object).', + ]); + } + + $collection = Collection::query()->findOrFail((int) $validated['collection_id']); + $this->assertCanOnCollection($user, $collection, 'create'); + + $created = []; + foreach ($rows as $row) { + $record = $collection->createRecord(['data' => $row]); + $created[] = $this->recordSummary($record); + } + + return [ + 'collection_id' => $collection->id, + 'created_count' => count($created), + 'data' => $created, + ]; + } + + /** + * @param array $filters + */ + private function filteredQuery(array $filters): Builder + { + $query = Collection::query()->nonSystem(); + $filter = $filters['filter'] ?? ''; + if ($filter !== '') { + $like = '%' . $filter . '%'; + $query->where(function ($inner) use ($like): void { + $inner->where('name', 'like', $like) + ->orWhere('description', 'like', $like); + }); + } + + return $query; + } + + /** + * @param array $input + * @return array + */ + private function updateRules(array $input): array + { + $rules = []; + if (array_key_exists('name', $input)) { + $rules['name'] = ['required', Rule::unique('collections')->ignore($input['id'] ?? null), 'alpha_spaces']; + } + if (array_key_exists('description', $input)) { + $rules['description'] = 'required|string'; + } + foreach (['create_screen_id', 'read_screen_id', 'update_screen_id'] as $field) { + if (array_key_exists($field, $input)) { + $rules[$field] = 'required|integer|min:1'; + } + } + if (array_key_exists('custom_title', $input)) { + $rules['custom_title'] = 'nullable|string'; + } + if (array_key_exists('obfuscate', $input)) { + $rules['obfuscate'] = 'nullable|boolean'; + } + foreach (['signal_create', 'signal_update', 'signal_delete', 'include_records_on_export'] as $field) { + if (array_key_exists($field, $input)) { + $rules[$field] = 'nullable|boolean'; + } + } + if (array_key_exists('type', $input)) { + $rules['type'] = 'nullable|in:NORMAL,RAG'; + } + + return $rules; + } + + /** + * @return array + */ + private function summary(Collection $collection): array + { + $count = 0; + $table = 'collection_' . $collection->id; + if (DB::getSchemaBuilder()->hasTable($table)) { + $count = (int) DB::table($table)->count(); + } + + return [ + 'id' => $collection->id, + 'name' => $collection->name, + 'description' => $collection->description, + 'type' => $collection->type, + 'create_screen_id' => $collection->create_screen_id, + 'read_screen_id' => $collection->read_screen_id, + 'update_screen_id' => $collection->update_screen_id, + 'record_count' => $count, + 'updated_at' => $this->timestamp($collection->updated_at), + ]; + } + + /** + * @return array + */ + private function detail(Collection $collection): array + { + return [ + ...$this->summary($collection), + 'custom_title' => $collection->custom_title, + 'obfuscate' => (bool) $collection->obfuscate, + 'columns' => $collection->columns, + 'signal_create' => (bool) $collection->signal_create, + 'signal_update' => (bool) $collection->signal_update, + 'signal_delete' => (bool) $collection->signal_delete, + 'created_at' => $this->timestamp($collection->created_at), + ]; + } + + /** + * @return array + */ + private function recordSummary(Record $record): array + { + return [ + 'id' => $record->id, + 'uuid' => $record->uuid ?? null, + 'data' => $record->data, + 'created_at' => $this->timestamp($record->created_at), + 'updated_at' => $this->timestamp($record->updated_at), + ]; + } + + private function assertCanOnCollection(User $user, Collection $collection, string $ability): void + { + // Policy abilities: view, create, update, delete + if ($user->can($ability, $collection)) { + return; + } + + throw ValidationException::withMessages([ + 'collection_id' => 'You do not have ' . $ability . ' permission on this collection.', + ]); + } + + private function timestamp(mixed $value): ?string + { + if ($value instanceof DateTimeInterface) { + return $value->format('c'); + } + + return $value === null ? null : (string) $value; + } + + private function assertPackage(): void + { + if (PackageHelper::isPackageInstalled(PackageHelper::PM_PACKAGE_COLLECTIONS)) { + return; + } + + throw ValidationException::withMessages([ + 'package' => 'package-collections is not installed on this ProcessMaker instance.', + ]); + } +} diff --git a/ProcessMaker/Mcp/DataConnectors/DataConnectorCatalog.php b/ProcessMaker/Mcp/DataConnectors/DataConnectorCatalog.php new file mode 100644 index 0000000000..686ec10ecf --- /dev/null +++ b/ProcessMaker/Mcp/DataConnectors/DataConnectorCatalog.php @@ -0,0 +1,466 @@ + + */ + private const ORDER_COLUMNS = ['name', 'type', 'status', 'updated_at', 'created_at', 'id']; + + public function actor(?Authenticatable $user): User + { + return McpActor::resolve($user, 'data connector'); + } + + /** + * @param array $input + * @return array + */ + public function list(User $user, array $input): array + { + $this->assertPackage(); + McpActor::assertCan($user, self::VIEW); + $filters = validator($input, [ + 'filter' => 'nullable|string|max:255', + 'type' => 'nullable|in:REST,SOAP', + 'status' => 'nullable|in:ACTIVE,INACTIVE', + 'page' => 'nullable|integer|min:1', + 'per_page' => 'nullable|integer|min:1|max:50', + 'order_by' => 'nullable|in:' . implode(',', self::ORDER_COLUMNS), + 'order_direction' => 'nullable|in:asc,desc', + 'include_templates' => 'nullable|boolean', + ])->validate(); + + $page = $this->filteredQuery($filters) + ->orderBy($filters['order_by'] ?? 'name', $filters['order_direction'] ?? 'asc') + ->paginate( + $filters['per_page'] ?? self::DEFAULT_PER_PAGE, + ['id', 'name', 'description', 'type', 'authtype', 'status', 'data_source_category_id', 'api_base_url', 'endpoints', 'updated_at'], + 'page', + $filters['page'] ?? 1, + ); + + return [ + 'data' => collect($page->items())->map(fn (DataSource $row): array => $this->summary($row))->all(), + 'meta' => [ + 'current_page' => $page->currentPage(), + 'last_page' => $page->lastPage(), + 'per_page' => $page->perPage(), + 'total' => $page->total(), + ], + ]; + } + + /** + * @return array + */ + public function find(User $user, int $id): array + { + $this->assertPackage(); + McpActor::assertCan($user, self::VIEW); + + return $this->detail(DataSource::query()->findOrFail($id)); + } + + /** + * @param array $input + * @return array + */ + public function create(User $user, array $input): array + { + $this->assertPackage(); + McpActor::assertCan($user, self::CREATE); + $input = $this->normalize($input); + $validated = validator($input, $this->createRules($input))->validate(); + $this->assertEndpoints($validated['endpoints'] ?? null); + + $dataSource = new DataSource(); + $dataSource->fill($this->writable($validated)); + if (!isset($validated['status'])) { + $dataSource->status = 'ACTIVE'; + } + if (!isset($validated['endpoints'])) { + $dataSource->endpoints = []; + } + $dataSource->saveOrFail(); + + return $this->detail($dataSource->refresh()); + } + + /** + * @param array $input + * @return array + */ + public function update(User $user, array $input): array + { + $this->assertPackage(); + McpActor::assertCan($user, self::EDIT); + $input = $this->normalize($input); + $validated = validator($input, array_merge([ + 'id' => 'required|integer|min:1', + ], $this->updateRules($input)))->validate(); + + $dataSource = DataSource::query()->findOrFail((int) $validated['id']); + $attributes = $this->writable($validated); + if ($attributes === []) { + throw ValidationException::withMessages([ + 'data_connector' => 'Provide at least one field to update: name, description, type, authtype, status, data_source_category_id, api_base_url, endpoints, credentials, or debug_mode.', + ]); + } + + if (array_key_exists('endpoints', $attributes)) { + $this->assertEndpoints($attributes['endpoints']); + } + + $dataSource->fill($attributes); + $dataSource->saveOrFail(); + + return $this->detail($dataSource->refresh()); + } + + /** + * @param array $input + * @return array + */ + public function listCategories(User $user, array $input): array + { + $this->assertPackage(); + McpActor::assertCan($user, self::VIEW); + $filters = validator($input, [ + 'filter' => 'nullable|string|max:255', + 'page' => 'nullable|integer|min:1', + 'per_page' => 'nullable|integer|min:1|max:50', + ])->validate(); + + $query = DataSourceCategory::query()->where('is_system', false)->where('status', 'ACTIVE'); + $filter = $filters['filter'] ?? ''; + if ($filter !== '') { + $like = '%' . $filter . '%'; + $query->where('name', 'like', $like); + } + + $page = $query->orderBy('name') + ->paginate( + $filters['per_page'] ?? self::DEFAULT_PER_PAGE, + ['id', 'name', 'status'], + 'page', + $filters['page'] ?? 1, + ); + + return [ + 'data' => collect($page->items())->map(static fn (DataSourceCategory $category): array => [ + 'id' => $category->id, + 'name' => $category->name, + 'status' => $category->status, + ])->all(), + 'meta' => [ + 'current_page' => $page->currentPage(), + 'last_page' => $page->lastPage(), + 'per_page' => $page->perPage(), + 'total' => $page->total(), + ], + ]; + } + + /** + * @param array $filters + */ + private function filteredQuery(array $filters): Builder + { + $query = DataSource::query() + ->where('is_template', false) + ->whereNull('asset_type'); + + if (empty($filters['include_templates'])) { + // already excluded templates above + } + + $filter = $filters['filter'] ?? ''; + if ($filter !== '') { + $like = '%' . $filter . '%'; + $query->where(function ($inner) use ($like): void { + $inner->where('name', 'like', $like) + ->orWhere('description', 'like', $like); + }); + } + + if (!empty($filters['type'])) { + $query->where('type', $filters['type']); + } else { + $query->where('type', 'REST'); + } + + if (!empty($filters['status'])) { + $query->where('status', $filters['status']); + } + + return $query; + } + + /** + * @param array $input + * @return array + */ + private function createRules(array $input): array + { + return [ + 'name' => ['required', Rule::unique('data_sources'), 'alpha_spaces'], + 'description' => 'nullable|string', + 'type' => 'required|in:REST,SOAP', + 'authtype' => $this->authtypeRule($input['type'] ?? 'REST'), + 'status' => 'nullable|in:ACTIVE,INACTIVE', + 'data_source_category_id' => 'required', + 'api_base_url' => 'nullable|string|max:2048', + 'endpoints' => 'nullable|array', + 'credentials' => 'nullable|array', + 'debug_mode' => 'nullable|boolean', + ]; + } + + /** + * @param array $input + * @return array + */ + private function updateRules(array $input): array + { + $rules = []; + if (array_key_exists('name', $input)) { + $rules['name'] = ['required', Rule::unique('data_sources')->ignore($input['id'] ?? null), 'alpha_spaces']; + } + if (array_key_exists('description', $input)) { + $rules['description'] = 'nullable|string'; + } + if (array_key_exists('type', $input)) { + $rules['type'] = 'required|in:REST,SOAP'; + } + if (array_key_exists('authtype', $input) || array_key_exists('type', $input)) { + $type = $input['type'] ?? DataSource::query()->find($input['id'] ?? null)?->type ?? 'REST'; + $rules['authtype'] = $this->authtypeRule((string) $type); + } + if (array_key_exists('status', $input)) { + $rules['status'] = 'required|in:ACTIVE,INACTIVE'; + } + if (array_key_exists('data_source_category_id', $input)) { + $rules['data_source_category_id'] = 'required'; + } + if (array_key_exists('api_base_url', $input)) { + $rules['api_base_url'] = 'nullable|string|max:2048'; + } + if (array_key_exists('endpoints', $input)) { + $rules['endpoints'] = 'nullable|array'; + } + if (array_key_exists('credentials', $input)) { + $rules['credentials'] = 'nullable|array'; + } + if (array_key_exists('debug_mode', $input)) { + $rules['debug_mode'] = 'nullable|boolean'; + } + + return $rules; + } + + private function authtypeRule(string $type): string + { + return match ($type) { + 'REST' => 'required|in:NONE,BASIC,OAUTH2_BEARER,OAUTH2_PASSWORD,OAUTH2_AUTHORIZATION_CODE', + 'SOAP' => 'required|in:PASSWORD,LOCAL_CERTIFICATE,WSDL_FILE', + default => 'required', + }; + } + + /** + * @param array $input + * @return array + */ + private function normalize(array $input): array + { + if (isset($input['data_source_category_id']) && is_int($input['data_source_category_id'])) { + $input['data_source_category_id'] = (string) $input['data_source_category_id']; + } + + if (isset($input['endpoints']) && is_string($input['endpoints'])) { + $decoded = json_decode($input['endpoints'], true); + if (!is_array($decoded)) { + throw ValidationException::withMessages([ + 'endpoints' => 'endpoints must be a JSON object map keyed by purpose.', + ]); + } + $input['endpoints'] = $decoded; + } + + if (isset($input['credentials']) && is_string($input['credentials'])) { + $decoded = json_decode($input['credentials'], true); + if (!is_array($decoded)) { + throw ValidationException::withMessages([ + 'credentials' => 'credentials must be a JSON object.', + ]); + } + $input['credentials'] = $decoded; + } + + if (isset($input['endpoints']) && is_array($input['endpoints'])) { + $input['endpoints'] = $this->normalizeEndpoints($input['endpoints']); + } + + return $input; + } + + /** + * @param array $endpoints + * @return array + */ + private function normalizeEndpoints(array $endpoints): array + { + $normalized = []; + foreach ($endpoints as $key => $endpoint) { + if (!is_array($endpoint)) { + continue; + } + $purpose = (string) ($endpoint['purpose'] ?? $key); + if ($purpose === '') { + continue; + } + if ((string) $key !== $purpose) { + throw ValidationException::withMessages([ + 'endpoints' => 'Endpoint map key must equal purpose for "' . $purpose . '".', + ]); + } + $endpoint['purpose'] = $purpose; + $normalized[$purpose] = $endpoint; + } + + return $normalized; + } + + /** + * @param array|null $endpoints + */ + private function assertEndpoints(?array $endpoints): void + { + if ($endpoints === null) { + return; + } + + foreach ($endpoints as $key => $endpoint) { + if (!is_array($endpoint)) { + throw ValidationException::withMessages([ + 'endpoints' => 'Each endpoint must be an object.', + ]); + } + $purpose = $endpoint['purpose'] ?? null; + if ($purpose === null || $purpose === '') { + throw ValidationException::withMessages([ + 'endpoints' => 'Each endpoint requires a purpose (resource name).', + ]); + } + if (empty($endpoint['url'])) { + throw ValidationException::withMessages([ + 'endpoints' => 'Endpoint "' . $purpose . '" requires a url.', + ]); + } + if (empty($endpoint['method'])) { + throw ValidationException::withMessages([ + 'endpoints' => 'Endpoint "' . $purpose . '" requires a method.', + ]); + } + } + } + + /** + * @param array $input + * @return array + */ + private function writable(array $input): array + { + $fields = [ + 'name', + 'description', + 'type', + 'authtype', + 'status', + 'data_source_category_id', + 'api_base_url', + 'endpoints', + 'credentials', + 'debug_mode', + ]; + + return array_intersect_key($input, array_flip($fields)); + } + + /** + * @return array + */ + private function summary(DataSource $dataSource): array + { + return [ + 'id' => $dataSource->id, + 'name' => $dataSource->name, + 'description' => $dataSource->description, + 'type' => $dataSource->type, + 'authtype' => $dataSource->authtype, + 'status' => $dataSource->status, + 'data_source_category_id' => $dataSource->data_source_category_id, + 'api_base_url' => $dataSource->api_base_url, + 'endpoint_purposes' => array_keys(is_array($dataSource->endpoints) ? $dataSource->endpoints : []), + 'updated_at' => $this->timestamp($dataSource->updated_at), + ]; + } + + /** + * @return array + */ + private function detail(DataSource $dataSource): array + { + return [ + ...$this->summary($dataSource), + 'endpoints' => $dataSource->endpoints, + 'debug_mode' => (bool) $dataSource->debug_mode, + 'created_at' => $this->timestamp($dataSource->created_at), + // credentials omitted on purpose — set via create/update only + ]; + } + + private function timestamp(mixed $value): ?string + { + if ($value instanceof DateTimeInterface) { + return $value->format('c'); + } + + return $value === null ? null : (string) $value; + } + + private function assertPackage(): void + { + if (PackageHelper::isPackageInstalled(PackageHelper::PM_PACKAGE_DATA_SOURCES)) { + return; + } + + throw ValidationException::withMessages([ + 'package' => 'package-data-sources is not installed on this ProcessMaker instance.', + ]); + } +} diff --git a/ProcessMaker/Mcp/FlowGenies/FlowGenieCatalog.php b/ProcessMaker/Mcp/FlowGenies/FlowGenieCatalog.php new file mode 100644 index 0000000000..2e98d8e4bd --- /dev/null +++ b/ProcessMaker/Mcp/FlowGenies/FlowGenieCatalog.php @@ -0,0 +1,385 @@ + + */ + private const ORDER_COLUMNS = ['name', 'updated_at', 'created_at', 'id']; + + public function __construct(private readonly FlowGenieConfigValidator $validator) + { + } + + public function actor(?Authenticatable $user): User + { + return McpActor::resolve($user, 'FlowGenie'); + } + + /** + * @param array $input + * @return array + */ + public function list(User $user, array $input): array + { + $this->assertPackage(); + McpActor::assertCan($user, self::VIEW); + $filters = validator($input, [ + 'filter' => 'nullable|string|max:255', + 'page' => 'nullable|integer|min:1', + 'per_page' => 'nullable|integer|min:1|max:50', + 'order_by' => 'nullable|in:' . implode(',', self::ORDER_COLUMNS), + 'order_direction' => 'nullable|in:asc,desc', + ])->validate(); + + $page = $this->filteredQuery($filters) + ->orderBy($filters['order_by'] ?? 'name', $filters['order_direction'] ?? 'asc') + ->paginate( + $filters['per_page'] ?? self::DEFAULT_PER_PAGE, + ['id', 'uuid', 'name', 'description', 'flow_genie_category_id', 'updated_at'], + 'page', + $filters['page'] ?? 1, + ); + + return [ + 'data' => collect($page->items())->map(fn (FlowGenie $row): array => $this->summary($row))->all(), + 'meta' => [ + 'current_page' => $page->currentPage(), + 'last_page' => $page->lastPage(), + 'per_page' => $page->perPage(), + 'total' => $page->total(), + ], + ]; + } + + /** + * @return array + */ + public function find(User $user, int $id): array + { + $this->assertPackage(); + McpActor::assertCan($user, self::VIEW); + + return $this->detail(FlowGenie::query()->findOrFail($id)); + } + + /** + * @param array $input + * @return array + */ + public function create(User $user, array $input): array + { + $this->assertPackage(); + McpActor::assertCan($user, self::CREATE); + $input = $this->normalizeInput($input); + $validated = validator($input, $this->createRules($input))->validate(); + $config = $this->validator->normalize($validated['config'] ?? null); + + $flowGenie = new FlowGenie(); + $flowGenie->fill([ + 'name' => $validated['name'], + 'description' => $validated['description'], + 'flow_genie_category_id' => $validated['flow_genie_category_id'], + 'config' => $config, + ]); + $flowGenie->saveOrFail(); + + return $this->detail($flowGenie->refresh()); + } + + /** + * @param array $input + * @return array + */ + public function update(User $user, array $input): array + { + $this->assertPackage(); + McpActor::assertCan($user, self::EDIT); + $input = $this->normalizeInput($input); + $validated = validator($input, array_merge([ + 'id' => 'required|integer|min:1', + ], $this->updateRules($input)))->validate(); + + $flowGenie = FlowGenie::query()->findOrFail((int) $validated['id']); + $attributes = []; + foreach (['name', 'description', 'flow_genie_category_id'] as $field) { + if (array_key_exists($field, $validated)) { + $attributes[$field] = $validated[$field]; + } + } + if (array_key_exists('config', $validated)) { + $attributes['config'] = $this->validator->normalize($validated['config']); + } + + if ($attributes === []) { + throw ValidationException::withMessages([ + 'flow_genie' => 'Provide at least one field to update: name, description, flow_genie_category_id, or config.', + ]); + } + + $flowGenie->fill($attributes); + $flowGenie->saveOrFail(); + + return $this->detail($flowGenie->refresh()); + } + + /** + * @param array $input + * @return array{ok: bool, deleted: bool, id: int} + */ + public function delete(User $user, array $input): array + { + $this->assertPackage(); + McpActor::assertCan($user, self::DELETE); + $validated = validator($input, [ + 'id' => 'required|integer|min:1', + ])->validate(); + + $flowGenie = FlowGenie::query()->findOrFail((int) $validated['id']); + $id = (int) $flowGenie->id; + $flowGenie->delete(); + + return [ + 'ok' => true, + 'deleted' => true, + 'id' => $id, + ]; + } + + /** + * @param array $input + * @return array + */ + public function listCategories(User $user, array $input): array + { + $this->assertPackage(); + McpActor::assertCan($user, self::VIEW); + $filters = validator($input, [ + 'filter' => 'nullable|string|max:255', + 'page' => 'nullable|integer|min:1', + 'per_page' => 'nullable|integer|min:1|max:50', + ])->validate(); + + $query = FlowGenieCategory::query()->nonSystem()->where('status', 'ACTIVE'); + + $filter = $filters['filter'] ?? ''; + if ($filter !== '') { + $query->where('name', 'like', '%' . $filter . '%'); + } + + $page = $query->orderBy('name') + ->paginate( + $filters['per_page'] ?? self::DEFAULT_PER_PAGE, + ['id', 'name', 'status'], + 'page', + $filters['page'] ?? 1, + ); + + return [ + 'data' => collect($page->items())->map(static fn (FlowGenieCategory $category): array => [ + 'id' => $category->id, + 'name' => $category->name, + 'status' => $category->status, + ])->all(), + 'meta' => [ + 'current_page' => $page->currentPage(), + 'last_page' => $page->lastPage(), + 'per_page' => $page->perPage(), + 'total' => $page->total(), + ], + ]; + } + + /** + * @param array $input + * @return array{ok: bool, errors: list>, warnings: list>, normalized?: array} + */ + public function validateConfig(User $user, array $input): array + { + $this->assertPackage(); + McpActor::assertCan($user, self::VIEW); + $validated = validator($input, [ + 'config' => 'nullable|array', + ])->validate(); + + $result = $this->validator->validate($validated['config'] ?? null); + if ($result['ok'] && array_key_exists('config', $validated)) { + $result['normalized'] = $this->validator->normalize($validated['config']); + } + + return $result; + } + + /** + * @param array $filters + */ + private function filteredQuery(array $filters): Builder + { + $query = FlowGenie::query(); + $filter = $filters['filter'] ?? ''; + if ($filter !== '') { + $query->filter($filter); + } + + return $query; + } + + /** + * @param array $input + * @return array + */ + private function createRules(array $input): array + { + return [ + 'name' => ['required', 'string', 'alpha_spaces', Rule::unique('flow_genies', 'name')], + 'description' => 'required|string|max:300', + 'flow_genie_category_id' => 'required', + 'config' => 'nullable|array', + ]; + } + + /** + * @param array $input + * @return array + */ + private function updateRules(array $input): array + { + $id = $input['id'] ?? null; + + return [ + 'name' => ['sometimes', 'string', 'alpha_spaces', Rule::unique('flow_genies', 'name')->ignore($id)], + 'description' => 'sometimes|string|max:300', + 'flow_genie_category_id' => 'sometimes', + 'config' => 'sometimes|array', + ]; + } + + /** + * @param array $input + * @return array + */ + private function normalizeInput(array $input): array + { + if (isset($input['flow_genie_category_id']) && is_int($input['flow_genie_category_id'])) { + $input['flow_genie_category_id'] = (string) $input['flow_genie_category_id']; + } + + return $input; + } + + /** + * @return array + */ + private function summary(FlowGenie $flowGenie): array + { + $config = is_array($flowGenie->config) ? $flowGenie->config : []; + + return [ + 'id' => $flowGenie->id, + 'name' => $flowGenie->name, + 'description' => $flowGenie->description, + 'flow_genie_category_id' => $flowGenie->flow_genie_category_id, + 'model' => $config['model'] ?? null, + 'provider' => $config['provider'] ?? null, + 'output_type' => $config['output_type'] ?? ($config['response_format'] ?? null), + 'has_instructions' => trim((string) ($config['instructions'] ?? $config['prompt'] ?? '')) !== '', + 'updated_at' => $this->timestamp($flowGenie->updated_at), + ]; + } + + /** + * @return array + */ + private function detail(FlowGenie $flowGenie): array + { + return [ + ...$this->summary($flowGenie), + 'uuid' => $flowGenie->uuid, + 'config' => $this->redactConfig(is_array($flowGenie->config) ? $flowGenie->config : []), + 'created_at' => $this->timestamp($flowGenie->created_at), + ]; + } + + /** + * @param array $config + * @return array + */ + private function redactConfig(array $config): array + { + if (!empty($config['provider_api_key'])) { + $config['provider_api_key'] = '[redacted]'; + } + + if (!empty($config['mcp_servers']) && is_array($config['mcp_servers'])) { + $config['mcp_servers'] = array_map(function ($server) { + if (!is_array($server)) { + return $server; + } + $auth = $server['config']['http_config']['authentication'] ?? null; + if (!is_array($auth)) { + return $server; + } + foreach (['access_token', 'client_secret', 'cached_access_token', 'cached_refresh_token'] as $key) { + if (!empty($auth[$key])) { + $auth[$key] = '[redacted]'; + } + if (!empty($auth['oauth_pkce'][$key])) { + $auth['oauth_pkce'][$key] = '[redacted]'; + } + if (!empty($auth['oauth_client_credentials'][$key])) { + $auth['oauth_client_credentials'][$key] = '[redacted]'; + } + } + $server['config']['http_config']['authentication'] = $auth; + + return $server; + }, $config['mcp_servers']); + } + + return $config; + } + + private function timestamp(mixed $value): ?string + { + if ($value instanceof DateTimeInterface) { + return $value->format('c'); + } + + return $value === null ? null : (string) $value; + } + + private function assertPackage(): void + { + if (class_exists(FlowGenie::class)) { + return; + } + + throw ValidationException::withMessages([ + 'package' => 'package-ai is not installed on this ProcessMaker instance.', + ]); + } +} diff --git a/ProcessMaker/Mcp/FlowGenies/FlowGenieConfigValidator.php b/ProcessMaker/Mcp/FlowGenies/FlowGenieConfigValidator.php new file mode 100644 index 0000000000..5ade19f02a --- /dev/null +++ b/ProcessMaker/Mcp/FlowGenies/FlowGenieConfigValidator.php @@ -0,0 +1,197 @@ +|null $config + * @return array{ok: bool, errors: list, warnings: list} + */ + public function validate(?array $config): array + { + $errors = []; + $warnings = []; + + if ($config === null) { + return [ + 'ok' => true, + 'errors' => [], + 'warnings' => [[ + 'code' => 'config_empty', + 'message' => 'No config provided. create/update will apply agent defaults (instructions empty until set).', + ]], + ]; + } + + if ($config === []) { + $warnings[] = [ + 'code' => 'config_empty', + 'message' => 'Empty config object. Prefer instructions + model + output_type for runnable FlowGenies.', + ]; + + return ['ok' => true, 'errors' => $errors, 'warnings' => $warnings]; + } + + $instructions = $this->instructionsText($config); + if ($instructions === '') { + $warnings[] = [ + 'code' => 'instructions_missing', + 'message' => 'Set config.instructions (or legacy config.prompt). Runtime agents need a system prompt.', + 'path' => 'config.instructions', + ]; + } + + if (isset($config['output_type']) && isset($config['response_format']) && $config['output_type'] !== $config['response_format']) { + $warnings[] = [ + 'code' => 'response_format_alias', + 'message' => 'Both output_type and response_format are set. Prefer output_type (Designer key); response_format is normalized into it.', + 'path' => 'config.output_type', + ]; + } + + if (isset($config['mcp_servers']) && !is_array($config['mcp_servers'])) { + $errors[] = [ + 'code' => 'mcp_servers_invalid', + 'message' => 'config.mcp_servers must be an array.', + 'path' => 'config.mcp_servers', + ]; + } + + if (isset($config['tools']) && !is_array($config['tools'])) { + $errors[] = [ + 'code' => 'tools_invalid', + 'message' => 'config.tools must be an array.', + 'path' => 'config.tools', + ]; + } + + if (isset($config['guardrails']) && !is_array($config['guardrails'])) { + $errors[] = [ + 'code' => 'guardrails_invalid', + 'message' => 'config.guardrails must be an object/array.', + 'path' => 'config.guardrails', + ]; + } + + if (isset($config['model_settings']) && !is_array($config['model_settings'])) { + $errors[] = [ + 'code' => 'model_settings_invalid', + 'message' => 'config.model_settings must be an object/array.', + 'path' => 'config.model_settings', + ]; + } + + return [ + 'ok' => $errors === [], + 'errors' => $errors, + 'warnings' => $warnings, + ]; + } + + /** + * @param array|null $config + * @return array + */ + public function normalize(?array $config): array + { + $config = is_array($config) ? $config : []; + $result = $this->validate($config); + if (!$result['ok']) { + $messages = []; + foreach ($result['errors'] as $error) { + $messages[$error['path'] ?? 'config'][] = $error['message']; + } + throw ValidationException::withMessages($messages); + } + + if (isset($config['prompt']) && !isset($config['instructions'])) { + $config['instructions'] = $config['prompt']; + unset($config['prompt']); + } + + if (isset($config['response_format']) && !isset($config['output_type'])) { + $config['output_type'] = $this->normalizeOutputType((string) $config['response_format']); + unset($config['response_format']); + } elseif (isset($config['responseFormat']) && !isset($config['output_type'])) { + $config['output_type'] = $this->normalizeOutputType((string) $config['responseFormat']); + unset($config['responseFormat']); + } elseif (isset($config['output_type'])) { + $config['output_type'] = $this->normalizeOutputType((string) $config['output_type']); + } + + return array_replace($this->defaults(), $config); + } + + /** + * @return array + */ + public function defaults(): array + { + $defaultModel = config('ai.default_model'); + if (!is_string($defaultModel) || $defaultModel === '') { + $defaultModel = 'gpt-4o'; + } + + return [ + 'instructions' => '', + 'model' => $defaultModel, + 'provider' => 'openai', + 'provider_api_key' => '', + 'output_type' => 'Text (default)', + 'mcp_servers' => [], + 'tools' => [], + 'dynamic_tools' => [], + 'collections' => [], + 'subagents' => [], + 'max_turns' => 30, + 'limit' => 15, + 'match_threshold' => 0.3, + 'enhance_pdf_quality' => false, + 'model_settings' => [ + 'temperature' => 0.5, + 'top_p' => 1, + 'tool_choice' => 'auto', + 'parallel_tool_calls' => false, + ], + 'guardrails' => [ + 'enabled' => true, + 'policies' => [], + ], + ]; + } + + /** + * @param array $config + */ + private function instructionsText(array $config): string + { + if (isset($config['instructions'])) { + return trim((string) $config['instructions']); + } + if (isset($config['prompt'])) { + return trim((string) $config['prompt']); + } + + return ''; + } + + private function normalizeOutputType(string $value): string + { + $trimmed = trim($value); + $map = [ + 'text' => 'Text (default)', + 'json' => 'JSON', + 'table' => 'Table', + 'Text (default)' => 'Text (default)', + 'JSON' => 'JSON', + 'Table' => 'Table', + ]; + + return $map[$trimmed] ?? $trimmed; + } +} diff --git a/ProcessMaker/Mcp/McpGuidance.php b/ProcessMaker/Mcp/McpGuidance.php new file mode 100644 index 0000000000..01282c8b1e --- /dev/null +++ b/ProcessMaker/Mcp/McpGuidance.php @@ -0,0 +1,227 @@ + + */ + public function skills(?string $scope = null): array + { + $raw = Setting::configByKey(self::SKILLS_KEY); + if (!is_array($raw)) { + return []; + } + + $activeScope = $scope ?? $this->boundScope(); + $skills = []; + foreach ($raw as $index => $item) { + if (!is_array($item)) { + continue; + } + + $normalized = $this->normalizeSkill($item, $index); + if ($normalized === null) { + continue; + } + + if (!$this->matchesScope($normalized['scope'], $activeScope)) { + continue; + } + + $skills[] = $normalized; + } + + return $skills; + } + + /** + * @return array{id: string, name: string, slug: string, when: string, content: string, system: bool, scope: string}|null + */ + public function skillBySlug(string $slug): ?array + { + $slug = trim($slug); + if ($slug === '') { + return null; + } + + // Lookup ignores server scope so an agent can load a known slug on demand. + foreach ($this->skillsWithoutScopeFilter() as $skill) { + if ($skill['slug'] === $slug) { + return $skill; + } + } + + return null; + } + + public function instructionsAppendix(): string + { + $sections = []; + + $prompt = $this->systemPrompt(); + if ($prompt !== '') { + $sections[] = "## System Prompt\n\n" . $prompt; + } + + $skills = $this->skills(); + if ($skills !== []) { + $lines = [ + '## Skills catalog', + '', + 'Load only skills whose `when` matches the current task.', + 'Call `get-mcp-skill` with the skill slug (or read the Resource URI).', + 'Do not load every skill by default.', + '', + ]; + + foreach ($skills as $skill) { + $when = $skill['when'] !== '' ? $skill['when'] : 'Use when relevant to the task'; + $lines[] = "- **{$skill['name']}** — when: {$when} — slug: `{$skill['slug']}` — uri: `pm://mcp/skills/{$skill['slug']}`"; + } + + $sections[] = implode("\n", $lines); + } + + return implode("\n\n", $sections); + } + + /** + * Catalog only — full skill bodies are loaded on demand via get-mcp-skill / resources. + * + * @return array{system_prompt: string, skills: list} + */ + public function payload(): array + { + return [ + 'system_prompt' => $this->systemPrompt(), + 'skills' => array_map( + static fn (array $skill): array => [ + 'id' => $skill['id'], + 'name' => $skill['name'], + 'slug' => $skill['slug'], + 'when' => $skill['when'], + 'uri' => 'pm://mcp/skills/' . $skill['slug'], + ], + $this->skills(), + ), + ]; + } + + /** + * @return list + */ + private function skillsWithoutScopeFilter(): array + { + $raw = Setting::configByKey(self::SKILLS_KEY); + if (!is_array($raw)) { + return []; + } + + $skills = []; + foreach ($raw as $index => $item) { + if (!is_array($item)) { + continue; + } + $normalized = $this->normalizeSkill($item, $index); + if ($normalized !== null) { + $skills[] = $normalized; + } + } + + return $skills; + } + + /** + * @param array $item + * @return array{id: string, name: string, slug: string, when: string, content: string, system: bool, scope: string}|null + */ + private function normalizeSkill(array $item, int $index): ?array + { + $name = trim((string) ($item['name'] ?? '')); + $content = trim((string) ($item['content'] ?? '')); + if ($name === '' && $content === '') { + return null; + } + + if ($name === '') { + $name = 'Skill ' . ($index + 1); + } + + $slug = trim((string) ($item['slug'] ?? '')); + if ($slug === '') { + $slug = Str::slug($name); + } + if ($slug === '') { + $slug = 'skill-' . ($index + 1); + } + + $id = trim((string) ($item['id'] ?? '')); + if ($id === '') { + $id = $slug; + } + + $scope = trim((string) ($item['scope'] ?? '')); + + return [ + 'id' => $id, + 'name' => $name, + 'slug' => $slug, + 'when' => trim((string) ($item['when'] ?? '')), + 'content' => $content, + 'system' => !empty($item['system']), + 'scope' => $scope, + ]; + } + + private function matchesScope(string $skillScope, ?string $activeScope): bool + { + if ($activeScope === null || $activeScope === '') { + return true; + } + + if ($skillScope === '' || $skillScope === 'all') { + return true; + } + + return $skillScope === $activeScope; + } + + private function boundScope(): ?string + { + if (!app()->bound(self::SKILL_SCOPE_KEY)) { + return null; + } + + $scope = app(self::SKILL_SCOPE_KEY); + + return is_string($scope) ? $scope : null; + } +} diff --git a/ProcessMaker/Mcp/Processes/GroupCatalog.php b/ProcessMaker/Mcp/Processes/GroupCatalog.php new file mode 100644 index 0000000000..36f0f59ba9 --- /dev/null +++ b/ProcessMaker/Mcp/Processes/GroupCatalog.php @@ -0,0 +1,92 @@ + + */ + private const ORDER_COLUMNS = ['id', 'name', 'status', 'updated_at', 'created_at']; + + /** + * Same gate shape as Api\GroupController::index for process authors. + * + * @param array $input + * @return array + */ + public function list(User $actor, array $input): array + { + $this->assertCanList($actor); + + $filters = validator($input, [ + 'filter' => 'nullable|string|max:255', + 'status' => 'nullable|in:ACTIVE,INACTIVE', + 'page' => 'nullable|integer|min:1', + 'per_page' => 'nullable|integer|min:1|max:50', + 'order_by' => 'nullable|in:' . implode(',', self::ORDER_COLUMNS), + 'order_direction' => 'nullable|in:asc,desc', + ])->validate(); + + $query = Group::query(); + $filter = trim((string) ($filters['filter'] ?? '')); + if ($filter !== '') { + $like = '%' . $filter . '%'; + $query->where(function ($builder) use ($like): void { + $builder->where('name', 'like', $like) + ->orWhere('description', 'like', $like); + }); + } + + if (!empty($filters['status'])) { + $query->where('status', $filters['status']); + } else { + $query->where('status', 'ACTIVE'); + } + + $page = $query + ->orderBy($filters['order_by'] ?? 'name', $filters['order_direction'] ?? 'asc') + ->paginate( + $filters['per_page'] ?? self::DEFAULT_PER_PAGE, + ['id', 'name', 'description', 'status', 'manager_id', 'updated_at'], + 'page', + $filters['page'] ?? 1, + ); + + return [ + 'data' => collect($page->items())->map(static fn (Group $group): array => [ + 'id' => $group->id, + 'name' => $group->name, + 'description' => $group->description, + 'status' => $group->status, + 'manager_id' => $group->manager_id, + 'updated_at' => optional($group->updated_at)?->format('c'), + ])->all(), + 'meta' => [ + 'current_page' => $page->currentPage(), + 'last_page' => $page->lastPage(), + 'per_page' => $page->perPage(), + 'total' => $page->total(), + ], + ]; + } + + private function assertCanList(User $user): void + { + if ($user->can('view-groups') + || $user->can('create-processes') + || $user->can('edit-processes')) { + return; + } + + throw new AuthorizationException('Not authorized to view groups.'); + } +} diff --git a/ProcessMaker/Mcp/Processes/ProcessBpmnValidator.php b/ProcessMaker/Mcp/Processes/ProcessBpmnValidator.php new file mode 100644 index 0000000000..e557b1ca06 --- /dev/null +++ b/ProcessMaker/Mcp/Processes/ProcessBpmnValidator.php @@ -0,0 +1,366 @@ +, warnings: list} + */ + public function validate(?string $bpmn): array + { + $errors = []; + $warnings = []; + + if ($bpmn === null || trim($bpmn) === '') { + $errors[] = $this->issue(null, 'empty_bpmn', 'BPMN XML is required for validation.'); + + return $this->result($errors, $warnings); + } + + $bpmn = BpmnDocument::replaceHtmlEntities($bpmn); + $schemaIssues = $this->schemaErrors($bpmn); + foreach ($schemaIssues as $message) { + $errors[] = $this->issue(null, 'schema', $message); + } + + $dom = new DOMDocument(); + if (@$dom->loadXML($bpmn) !== true) { + $errors[] = $this->issue(null, 'malformed_xml', 'BPMN XML could not be parsed.'); + + return $this->result($errors, $warnings); + } + + $xpath = new DOMXPath($dom); + $xpath->registerNamespace('bpmn', self::BPMN_NS); + $xpath->registerNamespace('bpmndi', self::DI_NS); + $xpath->registerNamespace('pm', self::PM_NS); + + $this->collectSemanticIssues($xpath, $errors, $warnings); + + return $this->result($errors, $warnings); + } + + /** + * @return list + */ + public function schemaErrors(string $bpmn): array + { + $schemaErrors = []; + $document = new BpmnDocument(); + try { + $document->loadXML($bpmn); + } catch (Throwable $e) { + return [$e->getMessage()]; + } + + try { + $document->validateBPMNSchema(public_path('definitions/ProcessMaker.xsd')); + } catch (Throwable $e) { + $schemaErrors = $document->getValidationErrors(); + $schemaErrors[] = $e->getMessage(); + } + + $diagrams = $document->getElementsByTagNameNS(self::DI_NS, 'BPMNDiagram'); + if ($diagrams->length > 1) { + $schemaErrors[] = __('Multiple diagrams are not supported'); + } + + $rulesValidation = new BPMNValidation(); + if (!$rulesValidation->passes('document', $document)) { + $schemaErrors[] = 'BPMN Validation failed: some bpmn elements do not comply with the validation.'; + } + + return array_values(array_filter(array_map( + static fn (mixed $error): string => is_string($error) ? $error : json_encode($error), + $schemaErrors, + ))); + } + + /** + * @param list $errors + * @param list $warnings + */ + private function collectSemanticIssues(DOMXPath $xpath, array &$errors, array &$warnings): void + { + if ($xpath->query('//bpmn:startEvent')->length === 0) { + $warnings[] = $this->issue(null, 'missing_start_event', 'Process has no start event; it cannot be started.'); + } + + $this->requireFlowWiring($xpath, $errors); + $this->requireInterstitialScreen($xpath, $errors); + + $this->requireAttr($xpath, '//bpmn:task', 'screenRef', 'missing_screen_ref', 'Form Task requires pm:screenRef.', $errors); + $this->requireAttr($xpath, '//bpmn:manualTask', 'screenRef', 'missing_screen_ref', 'Manual Task requires pm:screenRef.', $errors); + $this->requireAttr($xpath, '//bpmn:scriptTask', 'scriptRef', 'missing_script_ref', 'Script Task requires pm:scriptRef.', $errors); + $this->rejectUnresolvedPortableRefs($xpath, $errors); + + foreach ($xpath->query('//bpmn:callActivity') ?: [] as $node) { + if (!$node instanceof DOMElement) { + continue; + } + $called = trim($node->getAttribute('calledElement')); + if ($called === '') { + $errors[] = $this->issue( + $node->getAttribute('id') ?: null, + 'missing_called_element', + 'Call Activity requires calledElement.', + ); + } + } + + foreach ($xpath->query('//bpmn:exclusiveGateway') ?: [] as $gateway) { + if (!$gateway instanceof DOMElement) { + continue; + } + $id = $gateway->getAttribute('id'); + $default = $gateway->getAttribute('default'); + $outflows = $xpath->query('//bpmn:sequenceFlow[@sourceRef="' . $id . '"]'); + if ($outflows === false || $outflows->length < 2) { + continue; + } + $hasCondition = false; + foreach ($outflows as $flow) { + if (!$flow instanceof DOMElement) { + continue; + } + if ($flow->getElementsByTagNameNS(self::BPMN_NS, 'conditionExpression')->length > 0) { + $hasCondition = true; + break; + } + } + if ($default === '' && !$hasCondition) { + $warnings[] = $this->issue( + $id !== '' ? $id : null, + 'xor_no_default', + 'Exclusive gateway with multiple outflows has no default and no conditions.', + ); + } + } + } + + /** + * allowInterstitial=true without interstitialScreenRef leaves the task UI stuck loading. + * + * @param list $errors + */ + private function requireInterstitialScreen(DOMXPath $xpath, array &$errors): void + { + foreach ($xpath->query('//*[@pm:allowInterstitial]') ?: [] as $node) { + if (!$node instanceof DOMElement) { + continue; + } + $allow = strtolower(trim($node->getAttributeNS(self::PM_NS, 'allowInterstitial'))); + if ($allow === '') { + $allow = strtolower(trim($node->getAttribute('pm:allowInterstitial'))); + } + if ($allow !== 'true' && $allow !== '1') { + continue; + } + $ref = trim($node->getAttributeNS(self::PM_NS, 'interstitialScreenRef')); + if ($ref === '') { + $ref = trim($node->getAttribute('pm:interstitialScreenRef')); + } + if ($ref === '') { + $errors[] = $this->issue( + $node->getAttribute('id') ?: null, + 'missing_interstitial_screen', + 'allowInterstitial=true requires pm:interstitialScreenRef (DISPLAY screen; system key "interstitial" is valid).', + ); + } + } + } + + /** + * Nayra wires runtime flows from bpmn:outgoing / bpmn:incoming children. + * sourceRef/targetRef alone leave Start Events with 0 outgoing flows. + * + * @param list $errors + */ + private function requireFlowWiring(DOMXPath $xpath, array &$errors): void + { + foreach ($xpath->query('//bpmn:sequenceFlow') ?: [] as $flow) { + if (!$flow instanceof DOMElement) { + continue; + } + $flowId = $flow->getAttribute('id'); + $sourceId = $flow->getAttribute('sourceRef'); + $targetId = $flow->getAttribute('targetRef'); + if ($flowId === '' || $sourceId === '' || $targetId === '') { + continue; + } + + $source = $xpath->query('//*[@id="' . $sourceId . '"]')->item(0); + if ($source instanceof DOMElement + && $source->getElementsByTagNameNS(self::BPMN_NS, 'outgoing')->length === 0 + ) { + $errors[] = $this->issue( + $sourceId, + 'missing_outgoing', + 'Node is source of sequence flow ' . $flowId . ' but has no bpmn:outgoing child; Nayra will not leave this node.', + ); + } elseif ($source instanceof DOMElement) { + $hasOutgoing = false; + foreach ($source->getElementsByTagNameNS(self::BPMN_NS, 'outgoing') as $outgoing) { + if (trim($outgoing->textContent) === $flowId) { + $hasOutgoing = true; + break; + } + } + if (!$hasOutgoing) { + $errors[] = $this->issue( + $sourceId, + 'missing_outgoing_ref', + 'Node is source of sequence flow ' . $flowId . ' but bpmn:outgoing does not reference it.', + ); + } + } + + $target = $xpath->query('//*[@id="' . $targetId . '"]')->item(0); + if ($target instanceof DOMElement + && $target->getElementsByTagNameNS(self::BPMN_NS, 'incoming')->length === 0 + ) { + $errors[] = $this->issue( + $targetId, + 'missing_incoming', + 'Node is target of sequence flow ' . $flowId . ' but has no bpmn:incoming child; Nayra will not enter this node.', + ); + } elseif ($target instanceof DOMElement) { + $hasIncoming = false; + foreach ($target->getElementsByTagNameNS(self::BPMN_NS, 'incoming') as $incoming) { + if (trim($incoming->textContent) === $flowId) { + $hasIncoming = true; + break; + } + } + if (!$hasIncoming) { + $errors[] = $this->issue( + $targetId, + 'missing_incoming_ref', + 'Node is target of sequence flow ' . $flowId . ' but bpmn:incoming does not reference it.', + ); + } + } + } + } + + /** + * MCP create/update-process does not resolve ${screen:title} / ${script:title} / ${process:name}. + * Those portable refs resolve via core deploy-process. Leave them as hard errors here. + * + * @param list $errors + */ + private function rejectUnresolvedPortableRefs(DOMXPath $xpath, array &$errors): void + { + $checks = [ + ['//bpmn:task', 'screenRef', 'unresolved_screen_ref', 'Form Task pm:screenRef still uses ${screen:title}. Resolve to a numeric screen id (list-screens / create-screen) before create-process or update-process.'], + ['//bpmn:manualTask', 'screenRef', 'unresolved_screen_ref', 'Manual Task pm:screenRef still uses ${screen:title}. Resolve to a numeric screen id before create-process or update-process.'], + ['//bpmn:scriptTask', 'scriptRef', 'unresolved_script_ref', 'Script Task pm:scriptRef still uses ${script:title}. Resolve to a numeric script id before create-process or update-process.'], + ['//bpmn:callActivity', 'calledElement', 'unresolved_process_ref', 'Call Activity calledElement still uses ${process:name}. Resolve to the child process bpmn id before create-process or update-process.'], + ]; + + foreach ($checks as [$query, $localName, $code, $message]) { + foreach ($xpath->query($query) ?: [] as $node) { + if (!$node instanceof DOMElement) { + continue; + } + $value = $node->getAttributeNS(self::PM_NS, $localName); + if ($value === '') { + $value = $node->getAttribute('pm:' . $localName); + } + if ($value === '' && $localName === 'calledElement') { + $value = $node->getAttribute('calledElement'); + } + if (str_contains($value, '${')) { + $errors[] = $this->issue($node->getAttribute('id') ?: null, $code, $message); + } + } + } + + foreach ($xpath->query('//*[@pm:interstitialScreenRef]') ?: [] as $node) { + if (!$node instanceof DOMElement) { + continue; + } + $value = $node->getAttributeNS(self::PM_NS, 'interstitialScreenRef'); + if ($value === '') { + $value = $node->getAttribute('pm:interstitialScreenRef'); + } + if (str_contains($value, '${')) { + $errors[] = $this->issue( + $node->getAttribute('id') ?: null, + 'unresolved_screen_ref', + 'pm:interstitialScreenRef still uses ${screen:title}. Use numeric id or system key interstitial.', + ); + } + } + } + + /** + * @param list $errors + */ + private function requireAttr( + DOMXPath $xpath, + string $query, + string $localName, + string $code, + string $message, + array &$errors, + ): void { + foreach ($xpath->query($query) ?: [] as $node) { + if (!$node instanceof DOMElement) { + continue; + } + $value = $node->getAttributeNS(self::PM_NS, $localName); + if ($value === '') { + $value = $node->getAttribute('pm:' . $localName); + } + if (trim($value) === '') { + $errors[] = $this->issue($node->getAttribute('id') ?: null, $code, $message); + } + } + } + + /** + * @return array{nodeId: ?string, code: string, message: string} + */ + private function issue(?string $nodeId, string $code, string $message): array + { + return [ + 'nodeId' => $nodeId, + 'code' => $code, + 'message' => $message, + ]; + } + + /** + * @param list $errors + * @param list $warnings + * @return array{ok: bool, errors: list, warnings: list} + */ + private function result(array $errors, array $warnings): array + { + return [ + 'ok' => $errors === [], + 'errors' => $errors, + 'warnings' => $warnings, + ]; + } +} diff --git a/ProcessMaker/Mcp/Processes/ProcessCatalog.php b/ProcessMaker/Mcp/Processes/ProcessCatalog.php new file mode 100644 index 0000000000..1d60a696a1 --- /dev/null +++ b/ProcessMaker/Mcp/Processes/ProcessCatalog.php @@ -0,0 +1,363 @@ + + */ + private const ORDER_COLUMNS = ['name', 'status', 'updated_at', 'created_at', 'id']; + + public function __construct(private readonly ProcessBpmnValidator $validator) + { + } + + public function actor(?Authenticatable $user): User + { + if ($user instanceof User) { + return $user; + } + + $admin = $this->consoleAdministrator(); + if ($admin instanceof User) { + Auth::setUser($admin); + + return $admin; + } + + throw new AuthorizationException('Authenticate with a ProcessMaker API token before using process tools.'); + } + + /** + * @param array $input + * @return array + */ + public function list(User $user, array $input): array + { + $this->assertCan($user, self::VIEW); + $filters = validator($input, [ + 'filter' => 'nullable|string|max:255', + 'status' => 'nullable|in:ACTIVE,INACTIVE,ARCHIVED', + 'page' => 'nullable|integer|min:1', + 'per_page' => 'nullable|integer|min:1|max:50', + 'order_by' => 'nullable|in:' . implode(',', self::ORDER_COLUMNS), + 'order_direction' => 'nullable|in:asc,desc', + 'include_templates' => 'nullable|boolean', + ])->validate(); + + $page = $this->filteredQuery($filters) + ->orderBy($filters['order_by'] ?? 'name', $filters['order_direction'] ?? 'asc') + ->paginate( + $filters['per_page'] ?? self::DEFAULT_PER_PAGE, + ['id', 'name', 'description', 'status', 'process_category_id', 'updated_at'], + 'page', + $filters['page'] ?? 1, + ); + + return [ + 'data' => collect($page->items())->map(fn (Process $process): array => $this->summary($process))->all(), + 'meta' => [ + 'current_page' => $page->currentPage(), + 'last_page' => $page->lastPage(), + 'per_page' => $page->perPage(), + 'total' => $page->total(), + ], + ]; + } + + /** + * @return array + */ + public function find(User $user, int $id): array + { + $this->assertCan($user, self::VIEW); + + return $this->detail(Process::query()->findOrFail($id)); + } + + /** + * @param array $input + * @return array + */ + public function create(User $user, array $input): array + { + $this->assertCan($user, self::CREATE); + $input = $this->normalize($input); + $validated = validator($input, $this->createRules($input))->validate(); + + if (array_key_exists('bpmn', $validated) && is_string($validated['bpmn']) && $validated['bpmn'] !== '') { + $this->assertBpmnAcceptable($validated['bpmn']); + $validated['bpmn'] = BpmnDocument::replaceHtmlEntities($validated['bpmn']); + } + + $process = new Process(); + $process->fill($validated); + $process->user_id = $user->id; + if (empty($process->status)) { + $process->status = 'ACTIVE'; + } + if (empty($process->bpmn)) { + $process->bpmn = Process::getProcessTemplate('OnlyStartElement.bpmn'); + } + $process->saveOrFail(); + $this->forgetProjectAssetCache($user); + + $type = array_key_exists('bpmn', $input) && is_string($input['bpmn']) && $input['bpmn'] !== '' + ? ProcessCreated::BPMN_CREATION + : ProcessCreated::BLANK_CREATION; + ProcessCreated::dispatch($process->refresh(), $type); + + return $this->detail($process->refresh()); + } + + /** + * @param array $input + * @return array + */ + public function update(User $user, array $input): array + { + $this->assertCan($user, self::EDIT); + $input = $this->normalize($input); + $validated = validator($input, array_merge([ + 'id' => 'required|integer|min:1', + ], $this->updateRules($input)))->validate(); + + $process = Process::query()->findOrFail((int) $validated['id']); + $attributes = $this->writable($validated); + if ($attributes === []) { + throw ValidationException::withMessages([ + 'process' => 'Provide at least one field to update: name, description, status, process_category_id, or bpmn.', + ]); + } + + if (isset($attributes['bpmn'])) { + $this->assertBpmnAcceptable($attributes['bpmn']); + $attributes['bpmn'] = BpmnDocument::replaceHtmlEntities($attributes['bpmn']); + } + + $process->fill($attributes); + $process->saveOrFail(); + + return $this->detail($process->refresh()); + } + + /** + * @param array $input + * @return array{ok: bool, errors: list, warnings: list} + */ + public function validate(User $user, array $input): array + { + $this->assertCan($user, self::VIEW); + $validated = validator($input, [ + 'bpmn' => 'nullable|string', + 'id' => 'nullable|integer|min:1', + ])->validate(); + + $bpmn = $validated['bpmn'] ?? null; + if (($bpmn === null || $bpmn === '') && isset($validated['id'])) { + $bpmn = Process::query()->findOrFail((int) $validated['id'])->bpmn; + } + + return $this->validator->validate(is_string($bpmn) ? $bpmn : null); + } + + /** + * @param array $filters + */ + private function filteredQuery(array $filters): Builder + { + $query = Process::query()->notArchived(); + if (empty($filters['include_templates'])) { + $query->where(function (Builder $inner): void { + $inner->where('is_template', false)->orWhereNull('is_template'); + }); + } + + $filter = $filters['filter'] ?? ''; + if ($filter !== '') { + $like = '%' . $filter . '%'; + $query->where(function ($inner) use ($like): void { + $inner->where('name', 'like', $like) + ->orWhere('description', 'like', $like); + }); + } + + if (!empty($filters['status'])) { + $query->where('status', $filters['status']); + } + + return $query; + } + + /** + * @param array $input + * @return array + */ + private function createRules(array $input): array + { + $rules = Process::rules(); + if (array_key_exists('process_category_id', $input)) { + $rules['process_category_id'] = [new CategoryRule(null)]; + } + + return $rules; + } + + /** + * @param array $input + * @return array + */ + private function updateRules(array $input): array + { + $rules = []; + if (array_key_exists('name', $input)) { + $rules['name'] = ['required', Rule::unique('processes')->ignore($input['id'] ?? null), 'alpha_spaces']; + } + if (array_key_exists('description', $input)) { + $rules['description'] = 'required|string'; + } + if (array_key_exists('status', $input)) { + $rules['status'] = 'in:ACTIVE,INACTIVE,ARCHIVED'; + } + if (array_key_exists('process_category_id', $input)) { + $rules['process_category_id'] = [new CategoryRule(Process::query()->find($input['id'] ?? null))]; + } + if (array_key_exists('bpmn', $input)) { + $rules['bpmn'] = 'nullable|string'; + } + + return $rules; + } + + /** + * @param array $input + * @return array + */ + private function normalize(array $input): array + { + if (isset($input['process_category_id']) && is_int($input['process_category_id'])) { + $input['process_category_id'] = (string) $input['process_category_id']; + } + + return $input; + } + + /** + * @param array $input + * @return array + */ + private function writable(array $input): array + { + return array_intersect_key($input, array_flip([ + 'name', + 'description', + 'status', + 'process_category_id', + 'bpmn', + ])); + } + + private function assertBpmnAcceptable(string $bpmn): void + { + $result = $this->validator->validate($bpmn); + if ($result['ok']) { + return; + } + + $messages = collect($result['errors'])->pluck('message')->unique()->implode(' '); + + throw ValidationException::withMessages([ + 'bpmn' => $messages !== '' ? $messages : 'The bpm definition is not valid.', + ]); + } + + /** + * @return array + */ + private function summary(Process $process): array + { + return [ + 'id' => $process->id, + 'name' => $process->name, + 'description' => $process->description, + 'status' => $process->status, + 'process_category_id' => $process->process_category_id, + 'updated_at' => $this->timestamp($process->updated_at), + ]; + } + + /** + * @return array + */ + private function detail(Process $process): array + { + return [ + ...$this->summary($process), + 'bpmn' => $process->bpmn, + 'created_at' => $this->timestamp($process->created_at), + ]; + } + + private function timestamp(mixed $value): ?string + { + if ($value instanceof DateTimeInterface) { + return $value->format('c'); + } + + return $value === null ? null : (string) $value; + } + + private function assertCan(User $user, string $permission): void + { + if ($user->can($permission)) { + return; + } + + throw new AuthorizationException('You do not have the ' . $permission . ' permission.'); + } + + private function consoleAdministrator(): ?User + { + if (!app()->runningInConsole() || app()->runningUnitTests() || app()->environment('production')) { + return null; + } + + $admin = User::query()->where('is_administrator', true)->orderBy('id')->first(); + + return $admin instanceof User ? $admin : null; + } + + private function forgetProjectAssetCache(User $user): void + { + if (!hasPackage('package-projects')) { + return; + } + + Cache::forget('user_' . $user->id . '_project_assets'); + } +} diff --git a/ProcessMaker/Mcp/Processes/ProcessPlanner.php b/ProcessMaker/Mcp/Processes/ProcessPlanner.php new file mode 100644 index 0000000000..36c27cc70a --- /dev/null +++ b/ProcessMaker/Mcp/Processes/ProcessPlanner.php @@ -0,0 +1,192 @@ + + */ + private const QUESTIONS = [ + [ + 'id' => 'name', + 'ask' => 'What should the process be named? (unique, letters/numbers/spaces/dashes)', + 'why' => 'Required by create-process.', + ], + [ + 'id' => 'purpose', + 'ask' => 'In one or two sentences, what business outcome does this process deliver?', + 'why' => 'Becomes the process description and guides screen/task wording.', + ], + [ + 'id' => 'tasks', + 'ask' => 'List each step in order. For each: name, type (form task / manual / script / service / none), and what data is collected or decided.', + 'why' => 'Maps to BPMN tasks and screens.', + ], + [ + 'id' => 'decisions', + 'ask' => 'Where do paths split or join? For each decision: condition field/expression, and what happens on yes vs no (or each branch). Say "none" if linear.', + 'why' => 'Maps to exclusive/inclusive gateways and sequence-flow conditions.', + ], + [ + 'id' => 'assignments', + 'ask' => 'For each human task, who works it? Prefer assignment types: requester, process_manager, user_group (group ids), or user (user ids). Call list-users / list-groups to show options and pick real ids.', + 'why' => 'Sets pm:assignment / assignedUsers / assignedGroups on Form Tasks.', + ], + [ + 'id' => 'process_manager', + 'ask' => 'Who is the process manager (user id or username)? Required when any task uses process_manager assignment. Use list-users to pick one.', + 'why' => 'Process-level manager; used by pm:assignment="process_manager".', + ], + [ + 'id' => 'screens', + 'ask' => 'For each form/manual/end summary: reuse an existing screen title, or create a new one (title + FORM vs DISPLAY + key fields)?', + 'why' => 'Screens must exist (or be created) before BPMN pm:screenRef.', + ], + [ + 'id' => 'start', + 'ask' => 'How does the process start? (requester from Requests UI, web entry anonymous/authenticated, timer, signal). Any interstitial after start?', + 'why' => 'Configures Start Event and optional Web Entry.', + ], + ]; + + /** + * @param array $input + * @return array + */ + public function plan(User $user, array $input): array + { + $this->assertCanView($user); + + $goal = trim((string) ($input['goal'] ?? '')); + $answers = $this->collectAnswers($input); + $open = $this->openQuestions($answers); + $ready = $open === []; + + return [ + 'ready' => $ready, + 'goal' => $goal, + 'agent_instructions' => $ready + ? 'Blueprint is complete enough to design screens (if needed), then author BPMN and call validate-process before create-process. Follow the pm4-process skill (and the connector skills you need). Use list-users / list-groups if any assignment id is still unclear.' + : 'Do not call create-process yet. Ask the human the open_questions (batch a few related ones). After they answer, call plan-process again with the same goal and filled answer fields. Use list-users and list-groups when discussing assignees so the human can pick real people/groups.', + 'open_questions' => $open, + 'answered' => $answers, + 'assignment_types' => [ + 'requester', + 'process_manager', + 'user_group', + 'user', + 'previous_task_assignee', + 'process_variable', + 'rule_expression', + ], + 'helper_tools' => [ + 'list-users' => 'Search active users (id, username, name, email) for user / process_manager assignment.', + 'list-groups' => 'Search groups (id, name) for user_group assignment (pm:assignedGroups).', + 'list-screens' => 'On the Screens MCP server — search before creating screens.', + 'list-processes' => 'Search before creating a duplicate process name.', + ], + 'blueprint' => $ready ? $this->blueprint($goal, $answers) : null, + ]; + } + + /** + * @param array $input + * @return array + */ + private function collectAnswers(array $input): array + { + $answers = []; + foreach (self::QUESTIONS as $question) { + $id = $question['id']; + $value = trim((string) ($input[$id] ?? '')); + if ($value !== '') { + $answers[$id] = $value; + } + } + + return $answers; + } + + /** + * @param array $answers + * @return list + */ + private function openQuestions(array $answers): array + { + $open = []; + foreach (self::QUESTIONS as $question) { + if (!isset($answers[$question['id']])) { + $open[] = $question; + } + } + + // process_manager only required when assignments mention process_manager + if ($this->needsProcessManager($answers) === false) { + $open = array_values(array_filter( + $open, + static fn (array $q): bool => $q['id'] !== 'process_manager', + )); + } + + return $open; + } + + /** + * @param array $answers + */ + private function needsProcessManager(array $answers): bool + { + $assignments = strtolower($answers['assignments'] ?? ''); + $tasks = strtolower($answers['tasks'] ?? ''); + + return str_contains($assignments, 'process_manager') + || str_contains($tasks, 'process_manager') + || str_contains($assignments, 'process manager'); + } + + /** + * @param array $answers + * @return array + */ + private function blueprint(string $goal, array $answers): array + { + return [ + 'name' => $answers['name'], + 'description' => $answers['purpose'], + 'goal' => $goal, + 'start' => $answers['start'], + 'tasks' => $answers['tasks'], + 'decisions' => $answers['decisions'], + 'assignments' => $answers['assignments'], + 'process_manager' => $answers['process_manager'] ?? null, + 'screens' => $answers['screens'], + 'next_steps' => [ + 'Create or reuse screens listed under screens (Screens MCP).', + 'Author BPMN with numeric pm:screenRef (from create-screen/list-screens ids — MCP does not resolve ${screen:title}) and assignment attrs from assignments.', + 'Call validate-process, then create-process (or update-process).', + 'If process_manager is set, ensure the process manager user is assigned on the process after create (Modeler / process settings).', + ], + ]; + } + + private function assertCanView(User $user): void + { + if ($user->can(self::VIEW)) { + return; + } + + throw new AuthorizationException('You do not have the ' . self::VIEW . ' permission.'); + } +} diff --git a/ProcessMaker/Mcp/Processes/UserCatalog.php b/ProcessMaker/Mcp/Processes/UserCatalog.php new file mode 100644 index 0000000000..7a69557e03 --- /dev/null +++ b/ProcessMaker/Mcp/Processes/UserCatalog.php @@ -0,0 +1,94 @@ + + */ + private const ORDER_COLUMNS = ['id', 'username', 'firstname', 'lastname', 'email', 'status']; + + /** + * Same gate shape as Api\UserController::index for process authors. + * + * @param array $input + * @return array + */ + public function list(User $actor, array $input): array + { + $this->assertCanList($actor); + + $filters = validator($input, [ + 'filter' => 'nullable|string|max:255', + 'status' => 'nullable|in:ACTIVE,INACTIVE', + 'page' => 'nullable|integer|min:1', + 'per_page' => 'nullable|integer|min:1|max:50', + 'order_by' => 'nullable|in:' . implode(',', self::ORDER_COLUMNS), + 'order_direction' => 'nullable|in:asc,desc', + ])->validate(); + + $query = User::nonSystem(); + $filter = trim((string) ($filters['filter'] ?? '')); + if ($filter !== '') { + $like = '%' . $filter . '%'; + $query->where(function ($builder) use ($like): void { + $builder->where('username', 'like', $like) + ->orWhere('firstname', 'like', $like) + ->orWhere('lastname', 'like', $like) + ->orWhere('email', 'like', $like); + }); + } + + if (!empty($filters['status'])) { + $query->where('status', $filters['status']); + } else { + $query->where('status', 'ACTIVE'); + } + + $page = $query + ->orderBy($filters['order_by'] ?? 'username', $filters['order_direction'] ?? 'asc') + ->paginate( + $filters['per_page'] ?? self::DEFAULT_PER_PAGE, + ['id', 'username', 'firstname', 'lastname', 'email', 'status', 'is_administrator'], + 'page', + $filters['page'] ?? 1, + ); + + return [ + 'data' => collect($page->items())->map(static fn (User $user): array => [ + 'id' => $user->id, + 'username' => $user->username, + 'firstname' => $user->firstname, + 'lastname' => $user->lastname, + 'email' => $user->email, + 'status' => $user->status, + 'is_administrator' => (bool) $user->is_administrator, + ])->all(), + 'meta' => [ + 'current_page' => $page->currentPage(), + 'last_page' => $page->lastPage(), + 'per_page' => $page->perPage(), + 'total' => $page->total(), + ], + ]; + } + + private function assertCanList(User $user): void + { + if ($user->can('view-users') + || $user->can('create-processes') + || $user->can('edit-processes')) { + return; + } + + throw new AuthorizationException('Not authorized to view users.'); + } +} diff --git a/ProcessMaker/Mcp/Resources/McpSkillResource.php b/ProcessMaker/Mcp/Resources/McpSkillResource.php new file mode 100644 index 0000000000..ccc296380f --- /dev/null +++ b/ProcessMaker/Mcp/Resources/McpSkillResource.php @@ -0,0 +1,32 @@ +name = 'mcp-skill-' . $slug; + $this->title = $skillName; + $this->description = $when !== '' + ? 'MCP skill (when: ' . $when . ')' + : 'MCP skill: ' . $skillName; + $this->uri = 'pm://mcp/skills/' . $slug; + $this->mimeType = 'text/markdown'; + } + + public function handle(Request $request): Response + { + return Response::text($this->content !== '' ? $this->content : '_No content._'); + } +} diff --git a/ProcessMaker/Mcp/Screens/ScreenCatalog.php b/ProcessMaker/Mcp/Screens/ScreenCatalog.php new file mode 100644 index 0000000000..be1320f912 --- /dev/null +++ b/ProcessMaker/Mcp/Screens/ScreenCatalog.php @@ -0,0 +1,539 @@ + + */ + private const ORDER_COLUMNS = ['title', 'type', 'updated_at', 'created_at', 'id']; + + /** + * @var list + */ + private const JSON_FIELDS = ['config', 'computed', 'watchers', 'preview_presets']; + + private const JSON_FIELD_MESSAGE = ' must be a JSON object or array.'; + + public function actor(?Authenticatable $user): User + { + if ($user instanceof User) { + return $user; + } + + $admin = $this->consoleAdministrator(); + if ($admin instanceof User) { + Auth::setUser($admin); + + return $admin; + } + + throw new AuthorizationException('Authenticate with a ProcessMaker API token before using screen tools.'); + } + + /** + * @param array $input + * @return array + */ + public function list(User $user, array $input): array + { + $this->assertCan($user, self::VIEW); + $filters = validator($input, [ + 'filter' => 'nullable|string|max:255', + 'type' => 'nullable|string|max:20', + 'page' => 'nullable|integer|min:1', + 'per_page' => 'nullable|integer|min:1|max:50', + 'order_by' => 'nullable|in:' . implode(',', self::ORDER_COLUMNS), + 'order_direction' => 'nullable|in:asc,desc', + 'include_system' => 'nullable|boolean', + ])->validate(); + + $page = $this->filteredQuery($filters) + ->orderBy($filters['order_by'] ?? 'title', $filters['order_direction'] ?? 'asc') + ->paginate( + $filters['per_page'] ?? self::DEFAULT_PER_PAGE, + ['id', 'title', 'description', 'type', 'screen_category_id', 'updated_at'], + 'page', + $filters['page'] ?? 1, + ); + + return [ + 'data' => collect($page->items())->map(fn (Screen $screen): array => $this->summary($screen))->all(), + 'meta' => [ + 'current_page' => $page->currentPage(), + 'last_page' => $page->lastPage(), + 'per_page' => $page->perPage(), + 'total' => $page->total(), + ], + ]; + } + + /** + * @return array + */ + public function find(User $user, int $id): array + { + $this->assertCan($user, self::VIEW); + + return $this->detail(Screen::query()->findOrFail($id)); + } + + /** + * @param array $input + * @return array + */ + public function create(User $user, array $input): array + { + $this->assertCan($user, self::CREATE); + $input = $this->normalize($input); + $validated = validator($input, $this->createRules($input))->validate(); + $this->assertConfigValid($validated['config'] ?? null, $validated['type'] ?? null); + + $screen = new Screen(); + $screen->fill($validated); + $screen->saveOrFail(); + $this->forgetProjectAssetCache($user); + + $payload = $screen->getAttributes(); + $payload['tmp_screen_category_id'] = $validated['screen_category_id'] ?? null; + ScreenCreated::dispatch($payload); + + return $this->detail($screen->refresh()); + } + + /** + * @param array $input + * @return array + */ + public function update(User $user, array $input): array + { + $this->assertCan($user, self::EDIT); + $input = $this->normalize($input); + $validated = validator($input, array_merge([ + 'id' => 'required|integer|min:1', + ], $this->updateRules($input)))->validate(); + + $screen = Screen::query()->findOrFail((int) $validated['id']); + $attributes = $this->writable($validated); + if ($attributes === []) { + throw ValidationException::withMessages([ + 'screen' => 'Provide at least one field to update: title, description, type, screen_category_id, config, computed, watchers, preview_presets, or custom_css.', + ]); + } + + if (array_key_exists('config', $attributes)) { + $this->assertConfigValid( + $attributes['config'], + $attributes['type'] ?? $screen->type, + ); + } + + $original = $screen->getOriginal(); + $screen->fill($attributes); + $screen->saveOrFail(); + ScreenUpdated::dispatch($screen, $screen->getChanges(), $original); + ScreenCacheFactory::getScreenCache()->clearCompiledAssets(); + + return $this->detail($screen->refresh()); + } + + /** + * @param array $input + * @return array{ok: bool, errors: list, warnings: list} + */ + public function validate(User $user, array $input): array + { + $this->assertCan($user, self::VIEW); + $validated = validator($input, [ + 'config' => 'nullable|array', + 'type' => 'nullable|string|max:20', + 'id' => 'nullable|integer|min:1', + ])->validate(); + + $config = $validated['config'] ?? null; + $type = $validated['type'] ?? null; + if (($config === null || $config === []) && isset($validated['id'])) { + $screen = Screen::query()->findOrFail((int) $validated['id']); + $config = is_array($screen->config) ? $screen->config : []; + $type = $type ?? $screen->type; + } + + return $this->validator->validate(is_array($config) ? $config : null, is_string($type) ? $type : null); + } + + /** + * @return array{id: int, deleted: true} + */ + public function delete(User $user, int $id): array + { + $this->assertCan($user, self::DELETE); + $screen = Screen::query()->findOrFail($id); + if ((int) $screen->is_default === 1) { + throw ValidationException::withMessages([ + 'is_default' => 'Default screens cannot be deleted.', + ]); + } + + $screen->delete(); + ScreenDeleted::dispatch($screen); + $this->forgetProjectAssetCache($user); + + return [ + 'id' => $id, + 'deleted' => true, + ]; + } + + /** + * @param array $filters + */ + private function filteredQuery(array $filters): Builder + { + $query = Screen::query(); + if (empty($filters['include_system'])) { + $query->nonSystem(); + } + + $filter = $filters['filter'] ?? ''; + if ($filter !== '') { + $like = '%' . $filter . '%'; + $query->where(function ($inner) use ($like): void { + $inner->where('title', 'like', $like) + ->orWhere('description', 'like', $like); + }); + } + + if (!empty($filters['type'])) { + $query->where('type', $filters['type']); + } + + return $query; + } + + /** + * @param array $input + * @return array + */ + private function createRules(array $input): array + { + return array_merge(Screen::rules(), $this->optionalFieldRules($input)); + } + + /** + * @param array $input + * @return array + */ + private function updateRules(array $input): array + { + $rules = []; + if (array_key_exists('title', $input)) { + $rules['title'] = ['required', Rule::unique('screens')->ignore($input['id'] ?? null), 'alpha_spaces']; + } + if (array_key_exists('description', $input)) { + $rules['description'] = 'required|string'; + } + if (array_key_exists('type', $input)) { + $rules['type'] = 'required|string|max:20'; + } + if (array_key_exists('screen_category_id', $input)) { + $rules['screen_category_id'] = [new CategoryRule(Screen::query()->find($input['id'] ?? null))]; + } + + return array_merge($rules, $this->optionalFieldRules($input)); + } + + /** + * @param array $input + * @return array + */ + private function optionalFieldRules(array $input): array + { + $rules = []; + foreach (self::JSON_FIELDS as $field) { + if (array_key_exists($field, $input)) { + $rules[$field] = 'array'; + } + } + if (array_key_exists('custom_css', $input)) { + $rules['custom_css'] = 'nullable|string'; + } + + return $rules; + } + + /** + * @param array $input + * @return array + */ + private function normalize(array $input): array + { + if (isset($input['screen_category_id']) && is_int($input['screen_category_id'])) { + $input['screen_category_id'] = (string) $input['screen_category_id']; + } + + foreach (self::JSON_FIELDS as $field) { + if (!array_key_exists($field, $input)) { + continue; + } + if (is_string($input[$field])) { + $input[$field] = $this->decodeJson($field, $input[$field]); + } + if ($input[$field] === null) { + $input[$field] = []; + } + } + + if (isset($input['config']) && is_array($input['config'])) { + $input['config'] = $this->normalizeConfig($input['config']); + } + + if (isset($input['preview_presets']) && is_array($input['preview_presets'])) { + $input['preview_presets'] = $this->normalizePreviewPresets($input['preview_presets']); + } + + return $input; + } + + /** + * Designer stores each preset.data as a JSON string so empty objects survive PHP json_decode. + * + * @param array $presets + * @return list + */ + private function normalizePreviewPresets(array $presets): array + { + $normalized = []; + foreach ($presets as $preset) { + if (!is_array($preset)) { + continue; + } + $title = isset($preset['title']) && is_string($preset['title']) ? $preset['title'] : ''; + $data = $preset['data'] ?? '{}'; + if (is_array($data)) { + $data = json_encode($data, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) ?: '{}'; + } elseif (!is_string($data)) { + $data = '{}'; + } else { + try { + json_decode($data, true, 512, JSON_THROW_ON_ERROR); + } catch (JsonException) { + $data = '{}'; + } + } + $normalized[] = [ + 'title' => $title, + 'data' => $data, + ]; + } + + return $normalized; + } + + /** + * Ensure pages/items are Screen Builder–safe (missing inspector crashes the designer). + * + * @param array $pages + * @return array + */ + private function normalizeConfig(array $pages): array + { + foreach ($pages as $pageIndex => $page) { + if (!is_array($page)) { + continue; + } + $items = $page['items'] ?? []; + $pages[$pageIndex]['items'] = is_array($items) + ? array_map(fn (mixed $item): mixed => $this->normalizeConfigItem($item), $items) + : []; + } + + return $pages; + } + + /** + * @param mixed $item + * @return mixed + */ + private function normalizeConfigItem(mixed $item): mixed + { + if (!is_array($item)) { + return $item; + } + + if (!isset($item['inspector']) || !is_array($item['inspector'])) { + $item['inspector'] = []; + } + + if (empty($item['container']) || !isset($item['items']) || !is_array($item['items'])) { + return $item; + } + + foreach ($item['items'] as $columnIndex => $columnItems) { + if (!is_array($columnItems)) { + continue; + } + $item['items'][$columnIndex] = array_map( + fn (mixed $nested): mixed => $this->normalizeConfigItem($nested), + $columnItems, + ); + } + + return $item; + } + + /** + * @return array + */ + private function decodeJson(string $field, string $value): array + { + try { + $decoded = json_decode($value, true, 512, JSON_THROW_ON_ERROR); + } catch (JsonException) { + throw ValidationException::withMessages([ + $field => $field . self::JSON_FIELD_MESSAGE, + ]); + } + + if (!is_array($decoded)) { + throw ValidationException::withMessages([ + $field => $field . self::JSON_FIELD_MESSAGE, + ]); + } + + return $decoded; + } + + /** + * @param array $input + * @return array + */ + private function writable(array $input): array + { + $fields = ['title', 'description', 'type', 'screen_category_id', 'config', 'computed', 'custom_css', 'watchers', 'preview_presets']; + + return array_intersect_key($input, array_flip($fields)); + } + + /** + * @return array + */ + private function summary(Screen $screen): array + { + return [ + 'id' => $screen->id, + 'title' => $screen->title, + 'description' => $screen->description, + 'type' => $screen->type, + 'screen_category_id' => $screen->screen_category_id, + 'updated_at' => $this->timestamp($screen->updated_at), + ]; + } + + /** + * @return array + */ + private function detail(Screen $screen): array + { + return [ + ...$this->summary($screen), + 'config' => $screen->config, + 'computed' => $screen->computed, + 'watchers' => $screen->watchers, + 'preview_presets' => $screen->preview_presets, + 'custom_css' => $screen->custom_css, + 'created_at' => $this->timestamp($screen->created_at), + ]; + } + + private function timestamp(mixed $value): ?string + { + if ($value instanceof DateTimeInterface) { + return $value->format('c'); + } + + return $value === null ? null : (string) $value; + } + + private function assertCan(User $user, string $permission): void + { + if ($user->can($permission)) { + return; + } + + throw new AuthorizationException('You do not have the ' . $permission . ' permission.'); + } + + private function consoleAdministrator(): ?User + { + if (!app()->runningInConsole() || app()->runningUnitTests() || app()->environment('production')) { + return null; + } + + $admin = User::query()->where('is_administrator', true)->orderBy('id')->first(); + + return $admin instanceof User ? $admin : null; + } + + private function forgetProjectAssetCache(User $user): void + { + if (!hasPackage('package-projects')) { + return; + } + + Cache::forget('user_' . $user->id . '_project_assets'); + } + + /** + * @param array|null $config + */ + private function assertConfigValid(?array $config, ?string $type): void + { + $result = $this->validator->validate($config, $type); + if ($result['ok']) { + return; + } + + $messages = array_map( + static fn (array $issue): string => $issue['message'], + $result['errors'], + ); + + throw ValidationException::withMessages([ + 'config' => $messages, + ]); + } +} diff --git a/ProcessMaker/Mcp/Screens/ScreenConfigValidator.php b/ProcessMaker/Mcp/Screens/ScreenConfigValidator.php new file mode 100644 index 0000000000..867f835206 --- /dev/null +++ b/ProcessMaker/Mcp/Screens/ScreenConfigValidator.php @@ -0,0 +1,253 @@ +|null $config + * @return array{ok: bool, errors: list, warnings: list} + */ + public function validate(?array $config, ?string $type = null): array + { + $errors = []; + $warnings = []; + + if ($config === null) { + return $this->result($errors, $warnings); + } + + $hasVueEditor = false; + $hasNativeFields = false; + + foreach ($config as $pageIndex => $page) { + if (!is_array($page)) { + $errors[] = $this->issue("config.{$pageIndex}", 'invalid_page', 'Each config page must be an object.'); + continue; + } + + $items = $page['items'] ?? []; + if (!is_array($items)) { + $errors[] = $this->issue("config.{$pageIndex}.items", 'invalid_items', 'Page items must be an array.'); + continue; + } + + $this->walkItems( + $items, + "config.{$pageIndex}.items", + $errors, + $warnings, + $hasVueEditor, + $hasNativeFields, + ); + } + + if ($type === 'FORM' && $hasNativeFields && !$hasVueEditor) { + $warnings[] = $this->issue( + null, + 'prefer_vue_component_editor', + 'FORM screens should use VueComponentRenderTemplate as the main UI (see the pm4-screen skill). Native FormSelectList/FormInput layouts are fragile and often miss required options.', + ); + } + + return $this->result($errors, $warnings); + } + + /** + * @param list $items + * @param list $errors + * @param list $warnings + */ + private function walkItems( + array $items, + string $path, + array &$errors, + array &$warnings, + bool &$hasVueEditor, + bool &$hasNativeFields, + ): void { + foreach ($items as $index => $item) { + if (!is_array($item)) { + continue; + } + + $itemPath = "{$path}.{$index}"; + $component = (string) ($item['component'] ?? ''); + + if ($component === 'VueComponentRenderTemplate') { + $hasVueEditor = true; + $this->validateVueEditor($item, $itemPath, $errors, $warnings); + } elseif (in_array($component, self::NATIVE_FIELD_COMPONENTS, true)) { + $hasNativeFields = true; + } + + if ($component === 'FormSelectList') { + $this->validateSelectList($item, $itemPath, $errors); + } + + if (!empty($item['container']) && isset($item['items']) && is_array($item['items'])) { + foreach ($item['items'] as $columnIndex => $columnItems) { + if (!is_array($columnItems)) { + continue; + } + $this->walkItems( + $columnItems, + "{$itemPath}.items.{$columnIndex}", + $errors, + $warnings, + $hasVueEditor, + $hasNativeFields, + ); + } + } + } + } + + /** + * @param array $item + * @param list $errors + * @param list $warnings + */ + private function validateVueEditor(array $item, string $path, array &$errors, array &$warnings): void + { + $config = is_array($item['config'] ?? null) ? $item['config'] : []; + $contentVue = trim((string) ($config['contentVue'] ?? '')); + if ($contentVue === '') { + $errors[] = $this->issue("{$path}.config.contentVue", 'vue_content_missing', 'VueComponentRenderTemplate requires non-empty contentVue.'); + } + + $components = $config['components'] ?? null; + if (!is_array($components) || $components === []) { + $errors[] = $this->issue( + "{$path}.config.components", + 'vue_components_missing', + 'VueComponentRenderTemplate requires config.components with {name,key} entries for mounted tags.', + ); + } else { + foreach ($components as $compIndex => $entry) { + if (!is_array($entry) || trim((string) ($entry['name'] ?? '')) === '' || trim((string) ($entry['key'] ?? '')) === '') { + $errors[] = $this->issue( + "{$path}.config.components.{$compIndex}", + 'vue_component_entry_invalid', + 'Each components entry must include non-empty name and key.', + ); + } + } + } + + $handlerVue = trim((string) ($config['handlerVue'] ?? '')); + if ($handlerVue !== '' && !str_starts_with($handlerVue, 'return')) { + $warnings[] = $this->issue( + "{$path}.config.handlerVue", + 'vue_handler_missing_return', + 'handlerVue should start with return { ... }.', + ); + } + + if (($config['ignoreMustache'] ?? null) !== true) { + $warnings[] = $this->issue( + "{$path}.config.ignoreMustache", + 'vue_ignore_mustache', + 'Set ignoreMustache to true for VueComponentRenderTemplate.', + ); + } + } + + /** + * @param array $item + * @param list $errors + */ + private function validateSelectList(array $item, string $path, array &$errors): void + { + $config = is_array($item['config'] ?? null) ? $item['config'] : []; + $name = (string) ($config['name'] ?? 'unnamed'); + $options = is_array($config['options'] ?? null) ? $config['options'] : []; + $dataSource = (string) ($options['dataSource'] ?? 'provideData'); + + if ($dataSource !== 'provideData') { + return; + } + + $optionsList = $options['optionsList'] ?? null; + if (is_array($optionsList) && $optionsList !== []) { + return; + } + + $fromJson = $this->optionsFromJsonData($options['jsonData'] ?? null); + if ($fromJson !== []) { + $errors[] = $this->issue( + "{$path}.config.options.optionsList", + 'select_options_list_missing', + "FormSelectList \"{$name}\" has jsonData but empty/missing optionsList. Screen Builder needs optionsList (array of {value,content}) or the dropdown renders empty and required selects block submit.", + ); + + return; + } + + $errors[] = $this->issue( + "{$path}.config.options", + 'select_options_empty', + "FormSelectList \"{$name}\" with dataSource provideData has no options. Provide optionsList with at least one {value,content} entry.", + ); + } + + /** + * @return list> + */ + private function optionsFromJsonData(mixed $jsonData): array + { + if (!is_string($jsonData) || trim($jsonData) === '') { + return []; + } + + $decoded = json_decode($jsonData, true); + if (!is_array($decoded)) { + return []; + } + + return array_values(array_filter($decoded, 'is_array')); + } + + /** + * @return array{path: ?string, code: string, message: string} + */ + private function issue(?string $path, string $code, string $message): array + { + return [ + 'path' => $path, + 'code' => $code, + 'message' => $message, + ]; + } + + /** + * @param list $errors + * @param list $warnings + * @return array{ok: bool, errors: list, warnings: list} + */ + private function result(array $errors, array $warnings): array + { + return [ + 'ok' => $errors === [], + 'errors' => $errors, + 'warnings' => $warnings, + ]; + } +} diff --git a/ProcessMaker/Mcp/Servers/DataConnectorsServer.php b/ProcessMaker/Mcp/Servers/DataConnectorsServer.php new file mode 100644 index 0000000000..3c7fd418b7 --- /dev/null +++ b/ProcessMaker/Mcp/Servers/DataConnectorsServer.php @@ -0,0 +1,88 @@ +> + */ + protected array $tools = [ + GetMcpGuidanceTool::class, + GetMcpSkillTool::class, + ListDataConnectorCategoriesTool::class, + ListDataConnectorsTool::class, + GetDataConnectorTool::class, + CreateDataConnectorTool::class, + UpdateDataConnectorTool::class, + ListCollectionsTool::class, + GetCollectionTool::class, + CreateCollectionTool::class, + UpdateCollectionTool::class, + ListCollectionRecordsTool::class, + CreateCollectionRecordsTool::class, + ]; + + public function createContext(): ServerContext + { + app()->instance(McpGuidance::SKILL_SCOPE_KEY, McpGuidance::SCOPE_DATA_CONNECTORS); + + $guidance = app(McpGuidance::class); + $this->resources = McpServerSupport::skillResources($guidance); + + $context = parent::createContext(); + $appendix = $guidance->instructionsAppendix(); + if ($appendix !== '') { + $context->instructions = rtrim($context->instructions) . "\n\n" . $appendix; + } + + return $context; + } +} diff --git a/ProcessMaker/Mcp/Servers/FlowGenieServer.php b/ProcessMaker/Mcp/Servers/FlowGenieServer.php new file mode 100644 index 0000000000..5fe0f99ad6 --- /dev/null +++ b/ProcessMaker/Mcp/Servers/FlowGenieServer.php @@ -0,0 +1,74 @@ +> + */ + protected array $tools = [ + GetMcpGuidanceTool::class, + GetMcpSkillTool::class, + ListFlowGenieCategoriesTool::class, + ListFlowGeniesTool::class, + GetFlowGenieTool::class, + ValidateFlowGenieTool::class, + CreateFlowGenieTool::class, + UpdateFlowGenieTool::class, + DeleteFlowGenieTool::class, + ]; + + public function createContext(): ServerContext + { + app()->instance(McpGuidance::SKILL_SCOPE_KEY, McpGuidance::SCOPE_FLOW_GENIES); + + $guidance = app(McpGuidance::class); + $this->resources = McpServerSupport::skillResources($guidance); + + $context = parent::createContext(); + $appendix = $guidance->instructionsAppendix(); + if ($appendix !== '') { + $context->instructions = rtrim($context->instructions) . "\n\n" . $appendix; + } + + return $context; + } +} diff --git a/ProcessMaker/Mcp/Servers/McpServerSupport.php b/ProcessMaker/Mcp/Servers/McpServerSupport.php new file mode 100644 index 0000000000..18a9f4dd5d --- /dev/null +++ b/ProcessMaker/Mcp/Servers/McpServerSupport.php @@ -0,0 +1,29 @@ + + */ + public static function skillResources(McpGuidance $guidance): array + { + $resources = []; + foreach ($guidance->skills() as $skill) { + $resources[] = new McpSkillResource( + $skill['name'], + $skill['slug'], + $skill['content'], + $skill['when'], + ); + } + + return $resources; + } +} diff --git a/ProcessMaker/Mcp/Servers/ProcessServer.php b/ProcessMaker/Mcp/Servers/ProcessServer.php new file mode 100644 index 0000000000..65b4745b17 --- /dev/null +++ b/ProcessMaker/Mcp/Servers/ProcessServer.php @@ -0,0 +1,80 @@ +> + */ + protected array $tools = [ + GetMcpGuidanceTool::class, + GetMcpSkillTool::class, + PlanProcessTool::class, + ListUsersTool::class, + ListGroupsTool::class, + ListProcessesTool::class, + GetProcessTool::class, + CreateProcessTool::class, + UpdateProcessTool::class, + ValidateProcessTool::class, + ]; + + public function createContext(): ServerContext + { + app()->instance(McpGuidance::SKILL_SCOPE_KEY, McpGuidance::SCOPE_PROCESSES); + + $guidance = app(McpGuidance::class); + $this->resources = McpServerSupport::skillResources($guidance); + + $context = parent::createContext(); + $appendix = $guidance->instructionsAppendix(); + if ($appendix !== '') { + $context->instructions = rtrim($context->instructions) . "\n\n" . $appendix; + } + + return $context; + } +} diff --git a/ProcessMaker/Mcp/Servers/ScreenServer.php b/ProcessMaker/Mcp/Servers/ScreenServer.php new file mode 100644 index 0000000000..7a561d8bd2 --- /dev/null +++ b/ProcessMaker/Mcp/Servers/ScreenServer.php @@ -0,0 +1,77 @@ +" }` — `data` must be a JSON string so `{}` survives PHP decode. +For the full rules (control shape, component registration, domain fields, refactor parity, preview presets), follow the `pm4-screen` skill from the ProcessMaker Skills repository (`npx skills add processmaker/skills`), especially `preview-presets.md`. +update-screen changes only the fields you send. +delete-screen cannot remove a default screen. +MARKDOWN)] +class ScreenServer extends Server +{ + /** + * The tools registered with this MCP server. + * + * @var array> + */ + protected array $tools = [ + GetMcpGuidanceTool::class, + GetMcpSkillTool::class, + ListScreensTool::class, + GetScreenTool::class, + ValidateScreenTool::class, + CreateScreenTool::class, + UpdateScreenTool::class, + DeleteScreenTool::class, + ]; + + public function createContext(): ServerContext + { + app()->instance(McpGuidance::SKILL_SCOPE_KEY, McpGuidance::SCOPE_SCREENS); + + $guidance = app(McpGuidance::class); + $this->resources = McpServerSupport::skillResources($guidance); + + $context = parent::createContext(); + $appendix = $guidance->instructionsAppendix(); + if ($appendix !== '') { + $context->instructions = rtrim($context->instructions) . "\n\n" . $appendix; + } + + return $context; + } +} diff --git a/ProcessMaker/Mcp/Support/McpActor.php b/ProcessMaker/Mcp/Support/McpActor.php new file mode 100644 index 0000000000..248d898dcc --- /dev/null +++ b/ProcessMaker/Mcp/Support/McpActor.php @@ -0,0 +1,51 @@ +can($permission)) { + return; + } + + throw new AuthorizationException('You do not have the ' . $permission . ' permission.'); + } + + private static function consoleAdministrator(): ?User + { + if (!app()->runningInConsole() || app()->runningUnitTests() || app()->environment('production')) { + return null; + } + + $admin = User::query()->where('is_administrator', true)->orderBy('id')->first(); + + return $admin instanceof User ? $admin : null; + } +} diff --git a/ProcessMaker/Mcp/Tools/Collections/CollectionFields.php b/ProcessMaker/Mcp/Tools/Collections/CollectionFields.php new file mode 100644 index 0000000000..a552a54e28 --- /dev/null +++ b/ProcessMaker/Mcp/Tools/Collections/CollectionFields.php @@ -0,0 +1,38 @@ +integer()->description('Collection id.')->required(); + } + + public static function name(JsonSchema $schema, bool $required): StringType + { + $field = $schema->string()->description('Unique collection name (alpha_spaces).'); + + return $required ? $field->required() : $field; + } + + public static function description(JsonSchema $schema, bool $required): StringType + { + $field = $schema->string()->description('Collection description.'); + + return $required ? $field->required() : $field; + } + + public static function screenId(JsonSchema $schema, string $role, bool $required): IntegerType + { + $field = $schema->integer()->description($role . ' screen id.'); + + return $required ? $field->required() : $field; + } +} diff --git a/ProcessMaker/Mcp/Tools/Collections/CreateCollectionRecordsTool.php b/ProcessMaker/Mcp/Tools/Collections/CreateCollectionRecordsTool.php new file mode 100644 index 0000000000..b111384f92 --- /dev/null +++ b/ProcessMaker/Mcp/Tools/Collections/CreateCollectionRecordsTool.php @@ -0,0 +1,38 @@ +respond(fn (): array => $catalog->createRecords( + $catalog->actor($request->user()), + $request->all(), + )); + } + + public function schema(JsonSchema $schema): array + { + return [ + 'collection_id' => $schema->integer()->description('Collection id.')->required(), + 'records' => $schema->array()->items($schema->object())->description('Array of record data objects (field => value). Max 100.'), + 'data' => $schema->object()->description('Single record data object when seeding one row.'), + ]; + } +} diff --git a/ProcessMaker/Mcp/Tools/Collections/CreateCollectionTool.php b/ProcessMaker/Mcp/Tools/Collections/CreateCollectionTool.php new file mode 100644 index 0000000000..98e28d79ab --- /dev/null +++ b/ProcessMaker/Mcp/Tools/Collections/CreateCollectionTool.php @@ -0,0 +1,43 @@ +respond(fn (): array => $catalog->create( + $catalog->actor($request->user()), + $request->all(), + )); + } + + public function schema(JsonSchema $schema): array + { + return [ + 'name' => CollectionFields::name($schema, true), + 'description' => CollectionFields::description($schema, true), + 'create_screen_id' => CollectionFields::screenId($schema, 'Create', true), + 'read_screen_id' => CollectionFields::screenId($schema, 'Read/view', true), + 'update_screen_id' => CollectionFields::screenId($schema, 'Update', true), + 'custom_title' => $schema->string()->description('Optional Mustache title template for records.'), + 'obfuscate' => $schema->boolean()->default(false), + 'type' => $schema->string()->enum(['NORMAL', 'RAG'])->default('NORMAL'), + ]; + } +} diff --git a/ProcessMaker/Mcp/Tools/Collections/GetCollectionTool.php b/ProcessMaker/Mcp/Tools/Collections/GetCollectionTool.php new file mode 100644 index 0000000000..19db320387 --- /dev/null +++ b/ProcessMaker/Mcp/Tools/Collections/GetCollectionTool.php @@ -0,0 +1,41 @@ +respond(function () use ($request, $catalog): array { + $validated = $request->validate(['id' => 'required|integer|min:1']); + + return $catalog->find($catalog->actor($request->user()), (int) $validated['id']); + }); + } + + public function schema(JsonSchema $schema): array + { + return [ + 'id' => CollectionFields::id($schema), + ]; + } +} diff --git a/ProcessMaker/Mcp/Tools/Collections/ListCollectionRecordsTool.php b/ProcessMaker/Mcp/Tools/Collections/ListCollectionRecordsTool.php new file mode 100644 index 0000000000..7bd73a0cc0 --- /dev/null +++ b/ProcessMaker/Mcp/Tools/Collections/ListCollectionRecordsTool.php @@ -0,0 +1,42 @@ +respond(fn (): array => $catalog->listRecords( + $catalog->actor($request->user()), + $request->all(), + )); + } + + public function schema(JsonSchema $schema): array + { + return [ + 'collection_id' => $schema->integer()->description('Collection id.')->required(), + 'page' => $schema->integer()->default(1), + 'per_page' => $schema->integer()->default(15), + ]; + } +} diff --git a/ProcessMaker/Mcp/Tools/Collections/ListCollectionsTool.php b/ProcessMaker/Mcp/Tools/Collections/ListCollectionsTool.php new file mode 100644 index 0000000000..f01beb97f4 --- /dev/null +++ b/ProcessMaker/Mcp/Tools/Collections/ListCollectionsTool.php @@ -0,0 +1,44 @@ +respond(fn (): array => $catalog->list( + $catalog->actor($request->user()), + $request->all(), + )); + } + + public function schema(JsonSchema $schema): array + { + return [ + 'filter' => $schema->string()->description('Case-insensitive match against name or description.'), + 'page' => $schema->integer()->default(1), + 'per_page' => $schema->integer()->default(15), + 'order_by' => $schema->string()->enum(['name', 'updated_at', 'created_at', 'id'])->default('name'), + 'order_direction' => $schema->string()->enum(['asc', 'desc'])->default('asc'), + ]; + } +} diff --git a/ProcessMaker/Mcp/Tools/Collections/UpdateCollectionTool.php b/ProcessMaker/Mcp/Tools/Collections/UpdateCollectionTool.php new file mode 100644 index 0000000000..be2ef75a38 --- /dev/null +++ b/ProcessMaker/Mcp/Tools/Collections/UpdateCollectionTool.php @@ -0,0 +1,46 @@ +respond(fn (): array => $catalog->update( + $catalog->actor($request->user()), + $request->all(), + )); + } + + public function schema(JsonSchema $schema): array + { + return [ + 'id' => CollectionFields::id($schema), + 'name' => CollectionFields::name($schema, false), + 'description' => CollectionFields::description($schema, false), + 'create_screen_id' => CollectionFields::screenId($schema, 'Create', false), + 'read_screen_id' => CollectionFields::screenId($schema, 'Read/view', false), + 'update_screen_id' => CollectionFields::screenId($schema, 'Update', false), + 'custom_title' => $schema->string(), + 'obfuscate' => $schema->boolean(), + 'type' => $schema->string()->enum(['NORMAL', 'RAG']), + ]; + } +} diff --git a/ProcessMaker/Mcp/Tools/DataConnectors/CreateDataConnectorTool.php b/ProcessMaker/Mcp/Tools/DataConnectors/CreateDataConnectorTool.php new file mode 100644 index 0000000000..88c72acdb8 --- /dev/null +++ b/ProcessMaker/Mcp/Tools/DataConnectors/CreateDataConnectorTool.php @@ -0,0 +1,45 @@ +respond(fn (): array => $catalog->create( + $catalog->actor($request->user()), + $request->all(), + )); + } + + public function schema(JsonSchema $schema): array + { + return [ + 'name' => DataConnectorFields::name($schema, true), + 'description' => DataConnectorFields::description($schema, false), + 'type' => DataConnectorFields::type($schema, true), + 'authtype' => DataConnectorFields::authtype($schema, true), + 'data_source_category_id' => DataConnectorFields::category($schema, true), + 'status' => $schema->string()->enum(['ACTIVE', 'INACTIVE'])->default('ACTIVE'), + 'api_base_url' => $schema->string()->description('Base URL for {{__api_base_url__}} in endpoint urls.'), + 'endpoints' => DataConnectorFields::endpoints($schema), + 'credentials' => DataConnectorFields::credentials($schema), + 'debug_mode' => $schema->boolean()->default(false), + ]; + } +} diff --git a/ProcessMaker/Mcp/Tools/DataConnectors/DataConnectorFields.php b/ProcessMaker/Mcp/Tools/DataConnectors/DataConnectorFields.php new file mode 100644 index 0000000000..0eec6f5f8d --- /dev/null +++ b/ProcessMaker/Mcp/Tools/DataConnectors/DataConnectorFields.php @@ -0,0 +1,63 @@ +integer()->description('Data Connector id.')->required(); + } + + public static function name(JsonSchema $schema, bool $required): StringType + { + $field = $schema->string()->description('Unique connector name (alpha_spaces).'); + + return $required ? $field->required() : $field; + } + + public static function description(JsonSchema $schema, bool $required): StringType + { + $field = $schema->string()->description('Connector description.'); + + return $required ? $field->required() : $field; + } + + public static function type(JsonSchema $schema, bool $required): StringType + { + $field = $schema->string()->enum(['REST', 'SOAP'])->description('Connector type. Prefer REST.'); + + return $required ? $field->required() : $field; + } + + public static function authtype(JsonSchema $schema, bool $required): StringType + { + $field = $schema->string()->description('Auth type. REST: NONE, BASIC, OAUTH2_BEARER, OAUTH2_PASSWORD, OAUTH2_AUTHORIZATION_CODE.'); + + return $required ? $field->required() : $field; + } + + public static function category(JsonSchema $schema, bool $required): StringType + { + $field = $schema->string()->description('data_source_category_id from list-data-connector-categories.'); + + return $required ? $field->required() : $field; + } + + public static function endpoints(JsonSchema $schema): ObjectType + { + return $schema->object()->description('Map of endpoints keyed by purpose. Each value needs purpose, method, url.'); + } + + public static function credentials(JsonSchema $schema): ObjectType + { + return $schema->object()->description('Auth credentials object (never returned by get/list).'); + } +} diff --git a/ProcessMaker/Mcp/Tools/DataConnectors/GetDataConnectorTool.php b/ProcessMaker/Mcp/Tools/DataConnectors/GetDataConnectorTool.php new file mode 100644 index 0000000000..517bfc3f4f --- /dev/null +++ b/ProcessMaker/Mcp/Tools/DataConnectors/GetDataConnectorTool.php @@ -0,0 +1,41 @@ +respond(function () use ($request, $catalog): array { + $validated = $request->validate(['id' => 'required|integer|min:1']); + + return $catalog->find($catalog->actor($request->user()), (int) $validated['id']); + }); + } + + public function schema(JsonSchema $schema): array + { + return [ + 'id' => DataConnectorFields::id($schema), + ]; + } +} diff --git a/ProcessMaker/Mcp/Tools/DataConnectors/ListDataConnectorCategoriesTool.php b/ProcessMaker/Mcp/Tools/DataConnectors/ListDataConnectorCategoriesTool.php new file mode 100644 index 0000000000..9cad91ec63 --- /dev/null +++ b/ProcessMaker/Mcp/Tools/DataConnectors/ListDataConnectorCategoriesTool.php @@ -0,0 +1,42 @@ +respond(fn (): array => $catalog->listCategories( + $catalog->actor($request->user()), + $request->all(), + )); + } + + public function schema(JsonSchema $schema): array + { + return [ + 'filter' => $schema->string()->description('Case-insensitive match against category name.'), + 'page' => $schema->integer()->default(1), + 'per_page' => $schema->integer()->default(15), + ]; + } +} diff --git a/ProcessMaker/Mcp/Tools/DataConnectors/ListDataConnectorsTool.php b/ProcessMaker/Mcp/Tools/DataConnectors/ListDataConnectorsTool.php new file mode 100644 index 0000000000..e4890f6011 --- /dev/null +++ b/ProcessMaker/Mcp/Tools/DataConnectors/ListDataConnectorsTool.php @@ -0,0 +1,46 @@ +respond(fn (): array => $catalog->list( + $catalog->actor($request->user()), + $request->all(), + )); + } + + public function schema(JsonSchema $schema): array + { + return [ + 'filter' => $schema->string()->description('Case-insensitive match against name or description.'), + 'type' => $schema->string()->enum(['REST', 'SOAP'])->description('Connector type. Default REST.'), + 'status' => $schema->string()->enum(['ACTIVE', 'INACTIVE']), + 'page' => $schema->integer()->description('Page number, starting at 1.')->default(1), + 'per_page' => $schema->integer()->description('Page size. Maximum 50.')->default(15), + 'order_by' => $schema->string()->enum(['name', 'type', 'status', 'updated_at', 'created_at', 'id'])->default('name'), + 'order_direction' => $schema->string()->enum(['asc', 'desc'])->default('asc'), + ]; + } +} diff --git a/ProcessMaker/Mcp/Tools/DataConnectors/UpdateDataConnectorTool.php b/ProcessMaker/Mcp/Tools/DataConnectors/UpdateDataConnectorTool.php new file mode 100644 index 0000000000..694d13fb55 --- /dev/null +++ b/ProcessMaker/Mcp/Tools/DataConnectors/UpdateDataConnectorTool.php @@ -0,0 +1,48 @@ +respond(fn (): array => $catalog->update( + $catalog->actor($request->user()), + $request->all(), + )); + } + + public function schema(JsonSchema $schema): array + { + return [ + 'id' => DataConnectorFields::id($schema), + 'name' => DataConnectorFields::name($schema, false), + 'description' => DataConnectorFields::description($schema, false), + 'type' => DataConnectorFields::type($schema, false), + 'authtype' => DataConnectorFields::authtype($schema, false), + 'data_source_category_id' => DataConnectorFields::category($schema, false), + 'status' => $schema->string()->enum(['ACTIVE', 'INACTIVE']), + 'api_base_url' => $schema->string()->description('Base URL for {{__api_base_url__}}.'), + 'endpoints' => DataConnectorFields::endpoints($schema), + 'credentials' => DataConnectorFields::credentials($schema), + 'debug_mode' => $schema->boolean(), + ]; + } +} diff --git a/ProcessMaker/Mcp/Tools/FlowGenies/CreateFlowGenieTool.php b/ProcessMaker/Mcp/Tools/FlowGenies/CreateFlowGenieTool.php new file mode 100644 index 0000000000..3499e9df7c --- /dev/null +++ b/ProcessMaker/Mcp/Tools/FlowGenies/CreateFlowGenieTool.php @@ -0,0 +1,39 @@ +respond(fn (): array => $catalog->create( + $catalog->actor($request->user()), + $request->all(), + )); + } + + public function schema(JsonSchema $schema): array + { + return [ + 'name' => FlowGenieFields::name($schema, true), + 'description' => FlowGenieFields::description($schema, true), + 'flow_genie_category_id' => FlowGenieFields::category($schema, true), + 'config' => FlowGenieFields::config($schema), + ]; + } +} diff --git a/ProcessMaker/Mcp/Tools/FlowGenies/DeleteFlowGenieTool.php b/ProcessMaker/Mcp/Tools/FlowGenies/DeleteFlowGenieTool.php new file mode 100644 index 0000000000..9f96288f4c --- /dev/null +++ b/ProcessMaker/Mcp/Tools/FlowGenies/DeleteFlowGenieTool.php @@ -0,0 +1,36 @@ +respond(fn (): array => $catalog->delete( + $catalog->actor($request->user()), + $request->all(), + )); + } + + public function schema(JsonSchema $schema): array + { + return [ + 'id' => FlowGenieFields::id($schema), + ]; + } +} diff --git a/ProcessMaker/Mcp/Tools/FlowGenies/FlowGenieFields.php b/ProcessMaker/Mcp/Tools/FlowGenies/FlowGenieFields.php new file mode 100644 index 0000000000..18e1c7f395 --- /dev/null +++ b/ProcessMaker/Mcp/Tools/FlowGenies/FlowGenieFields.php @@ -0,0 +1,50 @@ +integer()->description('FlowGenie id.')->required(); + } + + public static function name(JsonSchema $schema, bool $required): StringType + { + $field = $schema->string()->description('Unique FlowGenie name (alpha_spaces).'); + + return $required ? $field->required() : $field; + } + + public static function description(JsonSchema $schema, bool $required): StringType + { + $field = $schema->string()->description('FlowGenie description (max 300).'); + + return $required ? $field->required() : $field; + } + + public static function category(JsonSchema $schema, bool $required): StringType + { + $field = $schema->string()->description('flow_genie_category_id from list-flow-genie-categories (comma-separated ids OK).'); + + return $required ? $field->required() : $field; + } + + public static function config(JsonSchema $schema, bool $required = false): ObjectType + { + $field = $schema->object()->description( + 'Agent config. Key fields: instructions (system prompt), model, provider, output_type ' + . '(Text (default)|JSON|Table), mcp_servers, tools, dynamic_tools, model_settings, guardrails, ' + . 'collections, max_turns. Legacy aliases prompt → instructions, response_format → output_type.' + ); + + return $required ? $field->required() : $field; + } +} diff --git a/ProcessMaker/Mcp/Tools/FlowGenies/GetFlowGenieTool.php b/ProcessMaker/Mcp/Tools/FlowGenies/GetFlowGenieTool.php new file mode 100644 index 0000000000..56961a6c43 --- /dev/null +++ b/ProcessMaker/Mcp/Tools/FlowGenies/GetFlowGenieTool.php @@ -0,0 +1,36 @@ +respond(fn (): array => $catalog->find( + $catalog->actor($request->user()), + (int) $request->get('id'), + )); + } + + public function schema(JsonSchema $schema): array + { + return [ + 'id' => FlowGenieFields::id($schema), + ]; + } +} diff --git a/ProcessMaker/Mcp/Tools/FlowGenies/ListFlowGenieCategoriesTool.php b/ProcessMaker/Mcp/Tools/FlowGenies/ListFlowGenieCategoriesTool.php new file mode 100644 index 0000000000..d95ba90c91 --- /dev/null +++ b/ProcessMaker/Mcp/Tools/FlowGenies/ListFlowGenieCategoriesTool.php @@ -0,0 +1,38 @@ +respond(fn (): array => $catalog->listCategories( + $catalog->actor($request->user()), + $request->all(), + )); + } + + public function schema(JsonSchema $schema): array + { + return [ + 'filter' => $schema->string()->description('Search category name.'), + 'page' => $schema->integer()->min(1)->default(1), + 'per_page' => $schema->integer()->min(1)->max(50)->default(15), + ]; + } +} diff --git a/ProcessMaker/Mcp/Tools/FlowGenies/ListFlowGeniesTool.php b/ProcessMaker/Mcp/Tools/FlowGenies/ListFlowGeniesTool.php new file mode 100644 index 0000000000..7502ecfd2e --- /dev/null +++ b/ProcessMaker/Mcp/Tools/FlowGenies/ListFlowGeniesTool.php @@ -0,0 +1,40 @@ +respond(fn (): array => $catalog->list( + $catalog->actor($request->user()), + $request->all(), + )); + } + + public function schema(JsonSchema $schema): array + { + return [ + 'filter' => $schema->string()->description('Search name, description, or category.'), + 'page' => $schema->integer()->min(1)->default(1), + 'per_page' => $schema->integer()->min(1)->max(50)->default(15), + 'order_by' => $schema->string()->enum(['name', 'updated_at', 'created_at', 'id'])->default('name'), + 'order_direction' => $schema->string()->enum(['asc', 'desc'])->default('asc'), + ]; + } +} diff --git a/ProcessMaker/Mcp/Tools/FlowGenies/UpdateFlowGenieTool.php b/ProcessMaker/Mcp/Tools/FlowGenies/UpdateFlowGenieTool.php new file mode 100644 index 0000000000..e7574ad243 --- /dev/null +++ b/ProcessMaker/Mcp/Tools/FlowGenies/UpdateFlowGenieTool.php @@ -0,0 +1,40 @@ +respond(fn (): array => $catalog->update( + $catalog->actor($request->user()), + $request->all(), + )); + } + + public function schema(JsonSchema $schema): array + { + return [ + 'id' => FlowGenieFields::id($schema), + 'name' => FlowGenieFields::name($schema, false), + 'description' => FlowGenieFields::description($schema, false), + 'flow_genie_category_id' => FlowGenieFields::category($schema, false), + 'config' => FlowGenieFields::config($schema), + ]; + } +} diff --git a/ProcessMaker/Mcp/Tools/FlowGenies/ValidateFlowGenieTool.php b/ProcessMaker/Mcp/Tools/FlowGenies/ValidateFlowGenieTool.php new file mode 100644 index 0000000000..d331159ed8 --- /dev/null +++ b/ProcessMaker/Mcp/Tools/FlowGenies/ValidateFlowGenieTool.php @@ -0,0 +1,36 @@ +respond(fn (): array => $catalog->validateConfig( + $catalog->actor($request->user()), + $request->all(), + )); + } + + public function schema(JsonSchema $schema): array + { + return [ + 'config' => FlowGenieFields::config($schema), + ]; + } +} diff --git a/ProcessMaker/Mcp/Tools/GetMcpGuidanceTool.php b/ProcessMaker/Mcp/Tools/GetMcpGuidanceTool.php new file mode 100644 index 0000000000..6f3917fb0e --- /dev/null +++ b/ProcessMaker/Mcp/Tools/GetMcpGuidanceTool.php @@ -0,0 +1,38 @@ +payload()); + } + + /** + * @return array + */ + public function schema(JsonSchema $schema): array + { + return []; + } +} diff --git a/ProcessMaker/Mcp/Tools/GetMcpSkillTool.php b/ProcessMaker/Mcp/Tools/GetMcpSkillTool.php new file mode 100644 index 0000000000..c13cfceb77 --- /dev/null +++ b/ProcessMaker/Mcp/Tools/GetMcpSkillTool.php @@ -0,0 +1,58 @@ +validate([ + 'slug' => 'required|string|max:255', + ]); + + $skill = $guidance->skillBySlug((string) $validated['slug']); + if ($skill === null) { + return Response::error('Skill not found for slug: ' . $validated['slug']); + } + + return Response::structured([ + 'id' => $skill['id'], + 'name' => $skill['name'], + 'slug' => $skill['slug'], + 'when' => $skill['when'], + 'uri' => 'pm://mcp/skills/' . $skill['slug'], + 'content' => $skill['content'], + ]); + } + + /** + * @return array + */ + public function schema(JsonSchema $schema): array + { + return [ + 'slug' => $schema->string() + ->description('Skill slug from the get-mcp-guidance catalog') + ->required(), + ]; + } +} diff --git a/ProcessMaker/Mcp/Tools/Processes/CreateProcessTool.php b/ProcessMaker/Mcp/Tools/Processes/CreateProcessTool.php new file mode 100644 index 0000000000..7b0b7c9c14 --- /dev/null +++ b/ProcessMaker/Mcp/Tools/Processes/CreateProcessTool.php @@ -0,0 +1,42 @@ +respond(fn (): array => $processes->create( + $processes->actor($request->user()), + $request->all(), + )); + } + + /** + * @return array + */ + public function schema(JsonSchema $schema): array + { + return [ + 'name' => ProcessFields::name($schema, true), + 'description' => ProcessFields::description($schema, true), + 'status' => ProcessFields::status($schema), + 'process_category_id' => ProcessFields::category($schema, false), + 'bpmn' => ProcessFields::bpmn($schema, false), + ]; + } +} diff --git a/ProcessMaker/Mcp/Tools/Processes/GetProcessTool.php b/ProcessMaker/Mcp/Tools/Processes/GetProcessTool.php new file mode 100644 index 0000000000..971c7722bb --- /dev/null +++ b/ProcessMaker/Mcp/Tools/Processes/GetProcessTool.php @@ -0,0 +1,48 @@ +respond(function () use ($request, $processes): array { + $validated = $request->validate([ + 'id' => 'required|integer|min:1', + ]); + + return $processes->find( + $processes->actor($request->user()), + (int) $validated['id'], + ); + }); + } + + /** + * @return array + */ + public function schema(JsonSchema $schema): array + { + return [ + 'id' => ProcessFields::id($schema), + ]; + } +} diff --git a/ProcessMaker/Mcp/Tools/Processes/ListGroupsTool.php b/ProcessMaker/Mcp/Tools/Processes/ListGroupsTool.php new file mode 100644 index 0000000000..b3ddcc60a6 --- /dev/null +++ b/ProcessMaker/Mcp/Tools/Processes/ListGroupsTool.php @@ -0,0 +1,51 @@ +respond(fn (): array => $groups->list( + $processes->actor($request->user()), + $request->all(), + )); + } + + /** + * @return array + */ + public function schema(JsonSchema $schema): array + { + return [ + 'filter' => $schema->string()->description('Search name or description.'), + 'status' => $schema->string()->description('ACTIVE or INACTIVE (default ACTIVE).'), + 'page' => $schema->integer()->description('Page number (default 1).'), + 'per_page' => $schema->integer()->description('Results per page (max 50).'), + 'order_by' => $schema->string()->description('Order column: id, name, status, updated_at, created_at.'), + 'order_direction' => $schema->string()->description('asc or desc.'), + ]; + } +} diff --git a/ProcessMaker/Mcp/Tools/Processes/ListProcessesTool.php b/ProcessMaker/Mcp/Tools/Processes/ListProcessesTool.php new file mode 100644 index 0000000000..22632de230 --- /dev/null +++ b/ProcessMaker/Mcp/Tools/Processes/ListProcessesTool.php @@ -0,0 +1,48 @@ +respond(fn (): array => $processes->list( + $processes->actor($request->user()), + $request->all(), + )); + } + + /** + * @return array + */ + public function schema(JsonSchema $schema): array + { + return [ + 'filter' => $schema->string()->description('Search name or description.'), + 'status' => $schema->string()->description(ProcessFields::STATUS), + 'page' => $schema->integer()->description('Page number (default 1).'), + 'per_page' => $schema->integer()->description('Results per page (max 50).'), + 'order_by' => $schema->string()->description('Order column: name, status, updated_at, created_at, id.'), + 'order_direction' => $schema->string()->description('asc or desc.'), + 'include_templates' => $schema->boolean()->description('Include process templates (default false).'), + ]; + } +} diff --git a/ProcessMaker/Mcp/Tools/Processes/ListUsersTool.php b/ProcessMaker/Mcp/Tools/Processes/ListUsersTool.php new file mode 100644 index 0000000000..63293f1963 --- /dev/null +++ b/ProcessMaker/Mcp/Tools/Processes/ListUsersTool.php @@ -0,0 +1,51 @@ +respond(fn (): array => $users->list( + $processes->actor($request->user()), + $request->all(), + )); + } + + /** + * @return array + */ + public function schema(JsonSchema $schema): array + { + return [ + 'filter' => $schema->string()->description('Search username, firstname, lastname, or email.'), + 'status' => $schema->string()->description('ACTIVE or INACTIVE (default ACTIVE).'), + 'page' => $schema->integer()->description('Page number (default 1).'), + 'per_page' => $schema->integer()->description('Results per page (max 50).'), + 'order_by' => $schema->string()->description('Order column: id, username, firstname, lastname, email, status.'), + 'order_direction' => $schema->string()->description('asc or desc.'), + ]; + } +} diff --git a/ProcessMaker/Mcp/Tools/Processes/PlanProcessTool.php b/ProcessMaker/Mcp/Tools/Processes/PlanProcessTool.php new file mode 100644 index 0000000000..f63aaec8c9 --- /dev/null +++ b/ProcessMaker/Mcp/Tools/Processes/PlanProcessTool.php @@ -0,0 +1,56 @@ +respond(fn (): array => $planner->plan( + $processes->actor($request->user()), + $request->all(), + )); + } + + /** + * @return array + */ + public function schema(JsonSchema $schema): array + { + return [ + 'goal' => $schema->string() + ->description('What the human wants to automate (free text). Required on every call.') + ->required(), + 'name' => $schema->string()->description('Process name (once known).'), + 'purpose' => $schema->string()->description('Business purpose / description.'), + 'tasks' => $schema->string()->description('Ordered steps: name, type, data collected.'), + 'decisions' => $schema->string()->description('Gateways / branch conditions, or "none".'), + 'assignments' => $schema->string()->description('Per-task assignee type and user/group ids from list-users / list-groups.'), + 'process_manager' => $schema->string()->description('Process manager user id or username (when needed).'), + 'screens' => $schema->string()->description('Screens to reuse or create (title, FORM/DISPLAY, fields).'), + 'start' => $schema->string()->description('How the process starts (requester UI, web entry, timer, …).'), + ]; + } +} diff --git a/ProcessMaker/Mcp/Tools/Processes/ProcessFields.php b/ProcessMaker/Mcp/Tools/Processes/ProcessFields.php new file mode 100644 index 0000000000..fb3f67a97e --- /dev/null +++ b/ProcessMaker/Mcp/Tools/Processes/ProcessFields.php @@ -0,0 +1,62 @@ +integer() + ->description('Process id.') + ->required(); + } + + public static function name(JsonSchema $schema, bool $required): StringType + { + $field = $schema->string()->description(self::NAME); + + return $required ? $field->required() : $field; + } + + public static function description(JsonSchema $schema, bool $required): StringType + { + $field = $schema->string()->description(self::DESCRIPTION); + + return $required ? $field->required() : $field; + } + + public static function status(JsonSchema $schema): StringType + { + return $schema->string()->description(self::STATUS); + } + + public static function category(JsonSchema $schema, bool $required): StringType + { + $field = $schema->string()->description(self::CATEGORY); + + return $required ? $field->required() : $field; + } + + public static function bpmn(JsonSchema $schema, bool $required): StringType + { + $field = $schema->string()->description(self::BPMN); + + return $required ? $field->required() : $field; + } +} diff --git a/ProcessMaker/Mcp/Tools/Processes/ProcessTool.php b/ProcessMaker/Mcp/Tools/Processes/ProcessTool.php new file mode 100644 index 0000000000..75888033b4 --- /dev/null +++ b/ProcessMaker/Mcp/Tools/Processes/ProcessTool.php @@ -0,0 +1,47 @@ + $action + */ + protected function respond(callable $action): Response|ResponseFactory + { + try { + return Response::structured($action()); + } catch (Throwable $exception) { + return $this->errorResponse($exception); + } + } + + private function errorResponse(Throwable $exception): Response + { + $message = match (true) { + $exception instanceof ValidationException => $this->validationMessage($exception), + $exception instanceof AuthorizationException => $exception->getMessage(), + $exception instanceof ModelNotFoundException => 'Process not found.', + default => throw $exception, + }; + + return Response::error($message); + } + + private function validationMessage(ValidationException $exception): string + { + $messages = collect($exception->errors())->flatten()->unique()->implode(' '); + + return $messages !== '' ? $messages : $exception->getMessage(); + } +} diff --git a/ProcessMaker/Mcp/Tools/Processes/UpdateProcessTool.php b/ProcessMaker/Mcp/Tools/Processes/UpdateProcessTool.php new file mode 100644 index 0000000000..5d97c40091 --- /dev/null +++ b/ProcessMaker/Mcp/Tools/Processes/UpdateProcessTool.php @@ -0,0 +1,43 @@ +respond(fn (): array => $processes->update( + $processes->actor($request->user()), + $request->all(), + )); + } + + /** + * @return array + */ + public function schema(JsonSchema $schema): array + { + return [ + 'id' => ProcessFields::id($schema), + 'name' => ProcessFields::name($schema, false), + 'description' => ProcessFields::description($schema, false), + 'status' => ProcessFields::status($schema), + 'process_category_id' => ProcessFields::category($schema, false), + 'bpmn' => ProcessFields::bpmn($schema, false), + ]; + } +} diff --git a/ProcessMaker/Mcp/Tools/Processes/ValidateProcessTool.php b/ProcessMaker/Mcp/Tools/Processes/ValidateProcessTool.php new file mode 100644 index 0000000000..bec615f01a --- /dev/null +++ b/ProcessMaker/Mcp/Tools/Processes/ValidateProcessTool.php @@ -0,0 +1,43 @@ +respond(fn (): array => $processes->validate( + $processes->actor($request->user()), + $request->all(), + )); + } + + /** + * @return array + */ + public function schema(JsonSchema $schema): array + { + return [ + 'bpmn' => ProcessFields::bpmn($schema, false), + 'id' => $schema->integer()->description('Existing process id whose BPMN should be validated when bpmn is omitted.'), + ]; + } +} diff --git a/ProcessMaker/Mcp/Tools/RespondingTool.php b/ProcessMaker/Mcp/Tools/RespondingTool.php new file mode 100644 index 0000000000..16dda01565 --- /dev/null +++ b/ProcessMaker/Mcp/Tools/RespondingTool.php @@ -0,0 +1,49 @@ + $action + */ + protected function respond(callable $action): Response|ResponseFactory + { + try { + return Response::structured($action()); + } catch (Throwable $exception) { + return $this->errorResponse($exception); + } + } + + private function errorResponse(Throwable $exception): Response + { + $message = match (true) { + $exception instanceof ValidationException => $this->validationMessage($exception), + $exception instanceof AuthorizationException => $exception->getMessage(), + $exception instanceof ModelNotFoundException => $exception->getMessage() !== '' + ? $exception->getMessage() + : 'Resource not found.', + default => throw $exception, + }; + + return Response::error($message); + } + + private function validationMessage(ValidationException $exception): string + { + $messages = collect($exception->errors())->flatten()->unique()->implode(' '); + + return $messages !== '' ? $messages : $exception->getMessage(); + } +} diff --git a/ProcessMaker/Mcp/Tools/Screens/CreateScreenTool.php b/ProcessMaker/Mcp/Tools/Screens/CreateScreenTool.php new file mode 100644 index 0000000000..da670ca41a --- /dev/null +++ b/ProcessMaker/Mcp/Tools/Screens/CreateScreenTool.php @@ -0,0 +1,46 @@ +respond(fn (): array => $screens->create( + $screens->actor($request->user()), + $request->all(), + )); + } + + /** + * @return array + */ + public function schema(JsonSchema $schema): array + { + return [ + 'title' => ScreenFields::title($schema, true), + 'description' => ScreenFields::description($schema, true), + 'type' => ScreenFields::type($schema, true), + 'screen_category_id' => ScreenFields::category($schema, true), + 'config' => ScreenFields::config($schema), + 'computed' => $schema->array()->description('Computed properties for the screen.'), + 'watchers' => $schema->array()->description('Screen watchers.'), + 'preview_presets' => ScreenFields::previewPresets($schema), + 'custom_css' => $schema->string()->description('Custom CSS applied when the screen renders.'), + ]; + } +} diff --git a/ProcessMaker/Mcp/Tools/Screens/DeleteScreenTool.php b/ProcessMaker/Mcp/Tools/Screens/DeleteScreenTool.php new file mode 100644 index 0000000000..57dbc104e1 --- /dev/null +++ b/ProcessMaker/Mcp/Tools/Screens/DeleteScreenTool.php @@ -0,0 +1,43 @@ +respond(function () use ($request, $screens): array { + $validated = $request->validate([ + 'id' => 'required|integer|min:1', + ]); + + return $screens->delete($screens->actor($request->user()), (int) $validated['id']); + }); + } + + /** + * @return array + */ + public function schema(JsonSchema $schema): array + { + return [ + 'id' => ScreenFields::id($schema), + ]; + } +} diff --git a/ProcessMaker/Mcp/Tools/Screens/GetScreenTool.php b/ProcessMaker/Mcp/Tools/Screens/GetScreenTool.php new file mode 100644 index 0000000000..0885fd7ea0 --- /dev/null +++ b/ProcessMaker/Mcp/Tools/Screens/GetScreenTool.php @@ -0,0 +1,45 @@ +respond(function () use ($request, $screens): array { + $validated = $request->validate([ + 'id' => 'required|integer|min:1', + ]); + + return $screens->find($screens->actor($request->user()), (int) $validated['id']); + }); + } + + /** + * @return array + */ + public function schema(JsonSchema $schema): array + { + return [ + 'id' => ScreenFields::id($schema), + ]; + } +} diff --git a/ProcessMaker/Mcp/Tools/Screens/ListScreensTool.php b/ProcessMaker/Mcp/Tools/Screens/ListScreensTool.php new file mode 100644 index 0000000000..a1c6502d29 --- /dev/null +++ b/ProcessMaker/Mcp/Tools/Screens/ListScreensTool.php @@ -0,0 +1,61 @@ +respond(fn (): array => $screens->list( + $screens->actor($request->user()), + $request->all(), + )); + } + + /** + * @return array + */ + public function schema(JsonSchema $schema): array + { + return [ + 'filter' => $schema->string() + ->description('Case-insensitive match against title or description.'), + 'type' => ScreenFields::type($schema, false), + 'page' => $schema->integer() + ->description('Page number, starting at 1.') + ->default(1), + 'per_page' => $schema->integer() + ->description('Page size. Maximum 50.') + ->default(15), + 'order_by' => $schema->string() + ->enum(['title', 'type', 'updated_at', 'created_at', 'id']) + ->description('Column to sort by.') + ->default('title'), + 'order_direction' => $schema->string() + ->enum(['asc', 'desc']) + ->description('Sort direction.') + ->default('asc'), + 'include_system' => $schema->boolean() + ->description('Include system screens.') + ->default(false), + ]; + } +} diff --git a/ProcessMaker/Mcp/Tools/Screens/ScreenFields.php b/ProcessMaker/Mcp/Tools/Screens/ScreenFields.php new file mode 100644 index 0000000000..dc7befde07 --- /dev/null +++ b/ProcessMaker/Mcp/Tools/Screens/ScreenFields.php @@ -0,0 +1,74 @@ +integer() + ->description('Screen id.') + ->required(); + } + + public static function title(JsonSchema $schema, bool $required): StringType + { + $field = $schema->string()->description(self::TITLE); + + return $required ? $field->required() : $field; + } + + public static function description(JsonSchema $schema, bool $required): StringType + { + $field = $schema->string()->description(self::DESCRIPTION); + + return $required ? $field->required() : $field; + } + + public static function type(JsonSchema $schema, bool $required): StringType + { + $field = $schema->string()->description(self::TYPE); + + return $required ? $field->required() : $field; + } + + public static function category(JsonSchema $schema, bool $required): StringType + { + $field = $schema->string()->description(self::CATEGORY); + + return $required ? $field->required() : $field; + } + + public static function config(JsonSchema $schema): ArrayType + { + return $schema->array() + ->items($schema->object()) + ->description(self::CONFIG); + } + + public static function previewPresets(JsonSchema $schema): ArrayType + { + return $schema->array() + ->items($schema->object()) + ->description(self::PREVIEW_PRESETS); + } +} diff --git a/ProcessMaker/Mcp/Tools/Screens/ScreenTool.php b/ProcessMaker/Mcp/Tools/Screens/ScreenTool.php new file mode 100644 index 0000000000..77cbe94aa4 --- /dev/null +++ b/ProcessMaker/Mcp/Tools/Screens/ScreenTool.php @@ -0,0 +1,47 @@ + $action + */ + protected function respond(callable $action): Response|ResponseFactory + { + try { + return Response::structured($action()); + } catch (Throwable $exception) { + return $this->errorResponse($exception); + } + } + + private function errorResponse(Throwable $exception): Response + { + $message = match (true) { + $exception instanceof ValidationException => $this->validationMessage($exception), + $exception instanceof AuthorizationException => $exception->getMessage(), + $exception instanceof ModelNotFoundException => 'Screen not found.', + default => throw $exception, + }; + + return Response::error($message); + } + + private function validationMessage(ValidationException $exception): string + { + $messages = collect($exception->errors())->flatten()->unique()->implode(' '); + + return $messages !== '' ? $messages : $exception->getMessage(); + } +} diff --git a/ProcessMaker/Mcp/Tools/Screens/UpdateScreenTool.php b/ProcessMaker/Mcp/Tools/Screens/UpdateScreenTool.php new file mode 100644 index 0000000000..e91a926f47 --- /dev/null +++ b/ProcessMaker/Mcp/Tools/Screens/UpdateScreenTool.php @@ -0,0 +1,49 @@ +respond(fn (): array => $screens->update( + $screens->actor($request->user()), + $request->all(), + )); + } + + /** + * @return array + */ + public function schema(JsonSchema $schema): array + { + return [ + 'id' => ScreenFields::id($schema), + 'title' => ScreenFields::title($schema, false), + 'description' => ScreenFields::description($schema, false), + 'type' => ScreenFields::type($schema, false), + 'screen_category_id' => ScreenFields::category($schema, false), + 'config' => ScreenFields::config($schema), + 'computed' => $schema->array()->description('Computed properties for the screen.'), + 'watchers' => $schema->array()->description('Screen watchers.'), + 'preview_presets' => ScreenFields::previewPresets($schema), + 'custom_css' => $schema->string()->description('Custom CSS applied when the screen renders.'), + ]; + } +} diff --git a/ProcessMaker/Mcp/Tools/Screens/ValidateScreenTool.php b/ProcessMaker/Mcp/Tools/Screens/ValidateScreenTool.php new file mode 100644 index 0000000000..f0d993d900 --- /dev/null +++ b/ProcessMaker/Mcp/Tools/Screens/ValidateScreenTool.php @@ -0,0 +1,44 @@ +respond(fn (): array => $screens->validate( + $screens->actor($request->user()), + $request->all(), + )); + } + + /** + * @return array + */ + public function schema(JsonSchema $schema): array + { + return [ + 'config' => ScreenFields::config($schema), + 'type' => ScreenFields::type($schema, false), + 'id' => $schema->integer()->description('Existing screen id whose config should be validated when config is omitted.'), + ]; + } +} diff --git a/ProcessMaker/Models/MediaPathGenerator.php b/ProcessMaker/Models/MediaPathGenerator.php index 732b4b1f25..15ea3e97b2 100644 --- a/ProcessMaker/Models/MediaPathGenerator.php +++ b/ProcessMaker/Models/MediaPathGenerator.php @@ -1,4 +1,5 @@ path = $path; + $plugin->name = basename($path); + + return $plugin; + } + + /** + * Get the plugin name. + * + * @return string + */ + public function getName(): string + { + return $this->name; + } + + /** + * Get the plugin path. + * + * @return string + */ + public function getPath(): string + { + return $this->path; + } + + /** + * Get the composer.json data. + * + * @return array + * @throws RuntimeException + */ + public function getComposerJson(): array + { + if ($this->composerJson !== null) { + return $this->composerJson; + } + + $composerJsonPath = $this->path . '/composer.json'; + + if (!file_exists($composerJsonPath)) { + throw new RuntimeException("composer.json not found in plugin: {$this->path}"); + } + + $content = file_get_contents($composerJsonPath); + $composerJson = json_decode($content, true); + + if (json_last_error() !== JSON_ERROR_NONE) { + throw new RuntimeException( + "Invalid JSON in composer.json at {$composerJsonPath}: " . json_last_error_msg() + ); + } + + $this->composerJson = $composerJson; + + return $this->composerJson; + } + + /** + * Get the plugin description from composer.json. + * + * @return string|null + */ + public function getDescription(): ?string + { + try { + $composerJson = $this->getComposerJson(); + + return $composerJson['description'] ?? null; + } catch (RuntimeException $e) { + return null; + } + } + + /** + * Get the current git branch. + * + * @return string|null + */ + public function getBranch(): ?string + { + if (!is_dir($this->path . '/.git')) { + return null; + } + + try { + $process = Process::path($this->path) + ->run('git rev-parse --abbrev-ref HEAD'); + + if ($process->successful()) { + return trim($process->output()); + } + } catch (\Exception $e) { + // Ignore errors + } + + return null; + } + + /** + * Get the current git tag if on a tag. + * + * @return string|null + */ + public function getTag(): ?string + { + if (!is_dir($this->path . '/.git')) { + return null; + } + + try { + $process = Process::path($this->path) + ->run('git describe --exact-match --tags HEAD 2>/dev/null'); + + if ($process->successful()) { + return trim($process->output()); + } + } catch (\Exception $e) { + // Ignore errors + } + + return null; + } + + /** + * Get the current git reference (branch or tag). + * + * @return string|null + */ + public function getReference(): ?string + { + $tag = $this->getTag(); + if ($tag !== null) { + return $tag; + } + + return $this->getBranch(); + } + + /** + * Get the plugin URL (git remote origin when available). + * + * @return string + */ + public function getUrl(): string + { + return $this->getRemoteUrl() ?? 'https://github.com/ProcessMaker/' . $this->name; + } + + /** + * @return string|null + */ + public function getRemoteUrl(): ?string + { + if (!$this->isGitRepository()) { + return null; + } + + try { + $process = Process::path($this->path) + ->run(['git', 'remote', 'get-url', 'origin']); + + if ($process->successful()) { + $url = trim($process->output()); + + return $url !== '' ? $url : null; + } + } catch (\Exception $e) { + // Ignore errors + } + + return null; + } + + public function isGitRepository(): bool + { + return is_dir($this->path . '/.git'); + } + + /** + * Override getTable to return null since this model doesn't use a database table. + * + * @return string|null + */ + public function getTable() + { + return null; + } +} diff --git a/ProcessMaker/Models/Process.php b/ProcessMaker/Models/Process.php index a953928f69..782038e048 100644 --- a/ProcessMaker/Models/Process.php +++ b/ProcessMaker/Models/Process.php @@ -32,6 +32,7 @@ use ProcessMaker\Nayra\Managers\WorkflowManagerDefault; use ProcessMaker\Nayra\Storage\BpmnDocument; use ProcessMaker\Package\WebEntry\Models\WebentryRoute; +use ProcessMaker\Providers\WorkflowServiceProvider; use ProcessMaker\Repositories\ProcessExecutionRawRepository; use ProcessMaker\Rules\BPMNValidation; use ProcessMaker\Traits\Exportable; @@ -1485,18 +1486,6 @@ public function convertFromExternalBPM() $document = new BpmnDocument(); $document->loadXML($this->bpmn); $conversions = 0; - // Replace subProcess by callActivity - $subProcesses = $document->getElementsByTagNameNS(BpmnDocument::BPMN_MODEL, 'subProcess'); - while ($subProcess = $subProcesses->item(0)) { - $conversions++; - $name = $subProcess->getAttribute('name'); - $callActivity = $this->createCallActivityFrom($subProcess); - $subProcess->parentNode->replaceChild($callActivity, $subProcess); - $warnings[] = [ - 'title' => __('Element conversion'), - 'text' => __('SubProcess Conversion', ['name' => $name]), - ]; - } // Replace sendTask to scriptTask $sendTasks = $document->getElementsByTagNameNS(BpmnDocument::BPMN_MODEL, 'sendTask'); while ($sendTask = $sendTasks->item(0)) { @@ -1510,51 +1499,19 @@ public function convertFromExternalBPM() ]; } if ($conversions) { + $this->ensurePmNamespaceOnDefinitions($document); $this->bpmn = $document->saveXml(); $this->bpmnDefinitions = null; } $this->warnings = $warnings; } - /** - * Convert a subProcess into a callActivity - * - * @param BPMNElement $subProcess - * @return void - */ - private function createCallActivityFrom($subProcess) + private function ensurePmNamespaceOnDefinitions(BpmnDocument $document): void { - $element = $this->cloneNodeAs($subProcess, 'callActivity', ['outgoing', 'incoming'], [], ['triggeredByEvent']); - - $definitions = $subProcess->ownerDocument->firstChild->cloneNode(false); - $diagram = $subProcess->ownerDocument->getElementsByTagName('BPMNDiagram')->item(0)->cloneNode(true); - $subProcessClone = $this->cloneNodeAs($subProcess, 'process', [], ['outgoing', 'incoming'], ['triggeredByEvent']); - $definitions->appendChild($subProcessClone); - $definitions->appendChild($diagram); - - $subProcessBpmn = $subProcessClone->ownerDocument->saveXml($definitions); - - $name = $subProcessClone->getAttribute('name'); - $duplicated = self::where('name', 'like', $name . '%') - ->orderBy(DB::raw('LENGTH(name), name')) - ->get(); - if ($duplicated->count()) { - $duplicated = $duplicated->last(); - $number = intval(substr($duplicated->name, strlen($name))) + 1; - $name = $name . ' (' . $number . ')'; + $root = $document->documentElement; + if ($root && !$root->hasAttribute('xmlns:pm')) { + $root->setAttribute('xmlns:pm', WorkflowServiceProvider::PROCESS_MAKER_NS); } - $process = new self([ - 'name' => $name, - 'bpmn' => $subProcessBpmn, - 'description' => $subProcessClone->getAttribute('name'), - ]); - $process->user_id = $this->user_id; - $process->process_category_id = $this->process_category_id; - $process->save(); - $bpmnProcess = $process->getDefinitions()->getElementsByTagNameNS(BpmnDocument::BPMN_MODEL, 'process')->item(0); - $element->setAttribute('calledElement', $bpmnProcess->getAttribute('id') . '-' . $process->id); - - return $element; } /** diff --git a/ProcessMaker/Models/ProcessEvents.php b/ProcessMaker/Models/ProcessEvents.php index bb4fc2ea95..87a2335491 100644 --- a/ProcessMaker/Models/ProcessEvents.php +++ b/ProcessMaker/Models/ProcessEvents.php @@ -62,7 +62,7 @@ public function initRelation(array $models, $relation) * Match the eagerly loaded results to their parents. * * @param Process[] $models - * @param \Illuminate\Database\Eloquent\Collection $results + * @param Collection $results * @param string $relation * @return array */ diff --git a/ProcessMaker/Models/Screen.php b/ProcessMaker/Models/Screen.php index 734af25e52..2740a7cef4 100644 --- a/ProcessMaker/Models/Screen.php +++ b/ProcessMaker/Models/Screen.php @@ -31,6 +31,7 @@ * @property array computed * @property array custom_css * @property array watchers + * @property array preview_presets * @property string label * @property Carbon type * @property Carbon $updated_at @@ -44,6 +45,7 @@ * @OA\Property(property="config", type="array", @OA\Items(type="object")), * @OA\Property(property="computed", type="array", @OA\Items(type="object")), * @OA\Property(property="watchers", type="array", @OA\Items(type="object")), + * @OA\Property(property="preview_presets", type="array", @OA\Items(type="object")), * @OA\Property(property="custom_css", type="string"), * @OA\Property(property="screen_category_id", type="string"), * ), @@ -89,6 +91,7 @@ class Screen extends ProcessMakerModel implements ScreenInterface, PrometheusMet 'config' => 'array', 'computed' => 'array', 'watchers' => 'array', + 'preview_presets' => 'array', 'translations' => 'array', ]; diff --git a/ProcessMaker/Models/ScreenVersion.php b/ProcessMaker/Models/ScreenVersion.php index ead25ce27b..fb4567583e 100644 --- a/ProcessMaker/Models/ScreenVersion.php +++ b/ProcessMaker/Models/ScreenVersion.php @@ -32,6 +32,7 @@ class ScreenVersion extends ProcessMakerModel implements ScreenInterface, Promet 'config' => 'array', 'computed' => 'array', 'watchers' => 'array', + 'preview_presets' => 'array', 'translations' => 'array', ]; diff --git a/ProcessMaker/Models/Script.php b/ProcessMaker/Models/Script.php index 2223b21d8f..c9d461ee00 100644 --- a/ProcessMaker/Models/Script.php +++ b/ProcessMaker/Models/Script.php @@ -30,6 +30,8 @@ * @property string language * @property text code * @property int timeout + * @property bool allow_in_process + * @property string execution_mode * * @OA\Schema( * schema="scriptsEditable", @@ -38,6 +40,8 @@ * @OA\Property(property="language", type="string"), * @OA\Property(property="code", type="string"), * @OA\Property(property="timeout", type="integer"), + * @OA\Property(property="allow_in_process", type="boolean"), + * @OA\Property(property="execution_mode", type="string", enum={"bare", "modules"}), * @OA\Property(property="run_as_user_id", type="integer"), * @OA\Property(property="key", type="string"), * @OA\Property(property="script_category_id", type="integer"), @@ -63,6 +67,15 @@ */ class Script extends ProcessMakerModel implements ScriptInterface { + public const EXECUTION_MODE_BARE = 'bare'; + + public const EXECUTION_MODE_MODULES = 'modules'; + + public const EXECUTION_MODES = [ + self::EXECUTION_MODE_BARE, + self::EXECUTION_MODE_MODULES, + ]; + use SerializeToIso8601; use HideSystemResources; use HasCategories; @@ -87,6 +100,8 @@ class Script extends ProcessMakerModel implements ScriptInterface 'timeout' => 'integer', 'retry_attempts' => 'integer', 'retry_wait_time' => 'integer', + 'allow_in_process' => 'boolean', + 'execution_mode' => 'string', ]; protected $appends = [ @@ -111,6 +126,21 @@ public static function boot() // automatically based on the scripts set language $script->setDefaultExecutor(); + if ($script->allow_in_process && strtolower((string) $script->language) !== 'php') { + throw \Illuminate\Validation\ValidationException::withMessages([ + 'allow_in_process' => [__('Only PHP scripts can allow in-process execution.')], + ]); + } + + if ( + $script->execution_mode === self::EXECUTION_MODE_MODULES + && (!$script->allow_in_process || strtolower((string) $script->language) !== 'php') + ) { + throw \Illuminate\Validation\ValidationException::withMessages([ + 'execution_mode' => [__('Modules execution requires an allowed PHP script.')], + ]); + } + // Execute the clear cache callback $clearCacheCallback($script); }); @@ -154,6 +184,8 @@ public static function rules($existing = null) 'description' => 'required', 'run_as_user_id' => 'required', 'timeout' => 'integer|min:0|max:65535', + 'allow_in_process' => 'sometimes|boolean', + 'execution_mode' => ['sometimes', Rule::in(self::EXECUTION_MODES)], 'script_category_id' => [new CategoryRule($existing)], ]; } diff --git a/ProcessMaker/Models/ScriptDockerNayraTrait.php b/ProcessMaker/Models/ScriptDockerNayraTrait.php index 8a2aa83625..5d2c5ee874 100644 --- a/ProcessMaker/Models/ScriptDockerNayraTrait.php +++ b/ProcessMaker/Models/ScriptDockerNayraTrait.php @@ -11,9 +11,9 @@ use ProcessMaker\Exception\ScriptException; use ProcessMaker\Facades\Docker; use ProcessMaker\ScriptRunners\Base; -use RuntimeException; -use Psr\Container\NotFoundExceptionInterface; use Psr\Container\ContainerExceptionInterface; +use Psr\Container\NotFoundExceptionInterface; +use RuntimeException; use UnexpectedValueException; /** @@ -21,7 +21,6 @@ */ trait ScriptDockerNayraTrait { - private $schema = 'http'; /** @@ -75,12 +74,14 @@ public function handleNayraDocker(string $code, array $data, array $config, $tim ]); throw new ScriptException($result); } + return $result; } private function getNayraInstanceUrl() { $servers = self::getNayraAddresses(); + return $this->schema . '://' . $servers[0] . ':' . $this->getNayraPort(); } @@ -92,6 +93,7 @@ private function getDockerLogs($instanceName) if ($status) { return 'Error getting logs from Nayra Docker: ' . implode("\n", $logs); } + return implode("\n", $logs); } @@ -206,7 +208,7 @@ private static function findNayraAddresses($docker, $instanceName, $times): bool . ($nayraDockerNetwork ? "'{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}'" : "'{{ .NetworkSettings.IPAddress }}'" - ) + ) . " {$instanceName}_nayra 2>/dev/null", $output, $status @@ -217,6 +219,7 @@ private static function findNayraAddresses($docker, $instanceName, $times): bool } if ($ip) { self::setNayraAddresses([$ip]); + return true; } } @@ -280,6 +283,7 @@ public static function clearNayraAddresses() private static function isCacheArrayStore(): bool { $cacheDriver = Cache::getFacadeRoot()->getStore(); + return $cacheDriver instanceof ArrayStore; } diff --git a/ProcessMaker/Models/Setting.php b/ProcessMaker/Models/Setting.php index 87395b44d8..2143e14a32 100644 --- a/ProcessMaker/Models/Setting.php +++ b/ProcessMaker/Models/Setting.php @@ -75,12 +75,12 @@ class Setting extends ProcessMakerModel implements HasMedia, PrometheusMetricInt public const SESSION_CONTROL_GROUP = 'Session Control'; + public const READY_KEY = 'readyToUseSettingsDatabase'; + private static bool $redisAvailable = false; private static bool $settingsTableExists = false; - private static bool $readyToUseSettingsDatabase = false; - /** * The attributes that aren't mass assignable. * @@ -535,15 +535,15 @@ public function getPrometheusMetricLabel(): string public static function readyToUseSettingsDatabase() { - if (!self::$readyToUseSettingsDatabase) { - self::$readyToUseSettingsDatabase = + if (!app()->has(self::READY_KEY) || !app(self::READY_KEY)) { + app()->instance(self::READY_KEY, app('tenant-resolved') && self::databaseAvailable() && self::redisAvailable() && - self::settingsTableExists(); + self::settingsTableExists()); } - return self::$readyToUseSettingsDatabase; + return app(self::READY_KEY); } private static function databaseAvailable() diff --git a/ProcessMaker/Models/SettingsMenus.php b/ProcessMaker/Models/SettingsMenus.php index f24aa2f2aa..40b976fc86 100644 --- a/ProcessMaker/Models/SettingsMenus.php +++ b/ProcessMaker/Models/SettingsMenus.php @@ -77,38 +77,38 @@ public function groups() */ public static function getId($menuName) { - return SettingsMenus::where('menu_group', $menuName)->pluck('id')->first(); + return self::where('menu_group', $menuName)->pluck('id')->first(); } public static function populateSettingMenus() { // Menu 1. Email SettingsMenus::EMAIL_GROUP_ID = 1 - SettingsMenus::firstOrCreate([ - 'menu_group' => SettingsMenus::EMAIL_MENU_GROUP + self::firstOrCreate([ + 'menu_group' => self::EMAIL_MENU_GROUP, ], [ - 'menu_group_order' => SettingsMenus::EMAIL_MENU_ORDER, - 'ui' => json_encode(["icon" => SettingsMenus::EMAIL_MENU_ICON]), + 'menu_group_order' => self::EMAIL_MENU_ORDER, + 'ui' => json_encode(['icon' => self::EMAIL_MENU_ICON]), ]); // Menu 2. Integrations SettingsMenus::INTEGRATIONS_GROUP_ID = 2 - SettingsMenus::firstOrCreate([ - 'menu_group' => SettingsMenus::INTEGRATIONS_MENU_GROUP + self::firstOrCreate([ + 'menu_group' => self::INTEGRATIONS_MENU_GROUP, ], [ - 'menu_group_order' => SettingsMenus::INTEGRATIONS_MENU_ORDER, - 'ui' => json_encode(["icon" => SettingsMenus::INTEGRATIONS_MENU_ICON]), + 'menu_group_order' => self::INTEGRATIONS_MENU_ORDER, + 'ui' => json_encode(['icon' => self::INTEGRATIONS_MENU_ICON]), ]); // Menu 3. Log-in & Auth SettingsMenus::LOG_IN_AUTH_GROUP_ID = 3 - SettingsMenus::firstOrCreate([ - 'menu_group' => SettingsMenus::LOG_IN_AUTH_MENU_GROUP + self::firstOrCreate([ + 'menu_group' => self::LOG_IN_AUTH_MENU_GROUP, ], [ - 'menu_group_order' => SettingsMenus::LOG_IN_AUTH_MENU_ORDER, - 'ui' => json_encode(["icon" => SettingsMenus::LOG_IN_AUTH_MENU_ICON]), + 'menu_group_order' => self::LOG_IN_AUTH_MENU_ORDER, + 'ui' => json_encode(['icon' => self::LOG_IN_AUTH_MENU_ICON]), ]); // Menu 4. Users Settings SettingsMenus::USER_SETTINGS_GROUP_ID = 4 - SettingsMenus::firstOrCreate([ - 'menu_group' => SettingsMenus::USER_SETTINGS_MENU_GROUP + self::firstOrCreate([ + 'menu_group' => self::USER_SETTINGS_MENU_GROUP, ], [ - 'menu_group_order' => SettingsMenus::USER_SETTINGS_MENU_ORDER, - 'ui' => json_encode(["icon" => SettingsMenus::USER_SETTINGS_MENU_ICON]), + 'menu_group_order' => self::USER_SETTINGS_MENU_ORDER, + 'ui' => json_encode(['icon' => self::USER_SETTINGS_MENU_ICON]), ]); } } diff --git a/ProcessMaker/Multitenancy/SwitchTenant.php b/ProcessMaker/Multitenancy/SwitchTenant.php index 72619d3a82..33fe7bd29e 100644 --- a/ProcessMaker/Multitenancy/SwitchTenant.php +++ b/ProcessMaker/Multitenancy/SwitchTenant.php @@ -42,6 +42,17 @@ public function makeCurrent(IsTenant $tenant): void request()->headers->set('host', $tenant->domain); $this->overrideConfigs($app, $tenant); + + // Extend BroadcastManager to our custom implementation that prefixes the channel names with the tenant id. + $app->extend(BroadcastManager::class, function ($manager, $app) use ($tenant) { + return new TenantAwareBroadcastManager($app, $tenant->id); + }); + + // Configure the tenant-specific route cache before Laravel checks whether it + // exists. This lets `TENANT= php artisan route:cache` create or refresh + // the cache for a tenant after a plugin is installed, enabled, or disabled. + Env::getRepository()->set('APP_ROUTES_CACHE', storage_path('routes-v7.php')); + $this->reloadRouteCache($app); } /** @@ -53,6 +64,7 @@ public function forgetCurrent(): void { $app = app(); $app->useStoragePath(base_path('storage')); + Env::getRepository()->clear('APP_ROUTES_CACHE'); // Restore landlord disk roots. Cached adapters still point at the previous // tenant until flushFilesystemDisks() runs below. @@ -133,6 +145,29 @@ private function setEnvironmentVariable($key, $value) $_ENV[$key] = $value; } + /** + * Reload the tenant's cached routes when the router can persist in memory. + * + * Octane keeps the router in memory between requests. The console and test + * environments may also initialize the router before a tenant is made + * current. Updating APP_ROUTES_CACHE alone is insufficient in those cases. + */ + private function reloadRouteCache(Application $app): void + { + if (!$app->routesAreCached() || !$this->shouldReinitializeRouter()) { + return; + } + + require $app->getCachedRoutesPath(); + } + + private function shouldReinitializeRouter(): bool + { + return isset($_SERVER['LARAVEL_OCTANE']) + || app()->runningInConsole() + || app()->runningUnitTests(); + } + private function overrideConfigs(Application $app, IsTenant $tenant) { $this->setEnvironmentVariable('APP_URL', $tenant->config['app.url']); diff --git a/ProcessMaker/Nayra/Services/FixBpmnSchemaService.php b/ProcessMaker/Nayra/Services/FixBpmnSchemaService.php index 4c3cf3ffff..46d5ae14eb 100644 --- a/ProcessMaker/Nayra/Services/FixBpmnSchemaService.php +++ b/ProcessMaker/Nayra/Services/FixBpmnSchemaService.php @@ -13,7 +13,7 @@ class FixBpmnSchemaService * * @param string $bpmn * @return string - * @throws \Exception + * @throws Exception */ public static function fix(string $bpmn): string { @@ -47,7 +47,7 @@ public static function fix(string $bpmn): string $taskNodes = $xpath->query('//*[local-name()="task"]'); foreach ($taskNodes as $task) { - $taskId = $task->getAttribute("id"); + $taskId = $task->getAttribute('id'); $taskPrefix = !empty($task->prefix) ? "$task->prefix:" : ''; $taskNS = $task->namespaceURI; @@ -72,22 +72,22 @@ public static function fix(string $bpmn): string // Create ioSpecification and children $ioSpec = $document->createElementNS($taskNS, "{$taskPrefix}ioSpecification"); $ioSpecId = "{$taskId}_inner_" . round(microtime(true) * 1000); - $ioSpec->setAttribute("id", $ioSpecId); + $ioSpec->setAttribute('id', $ioSpecId); $dataInputId = "data_input_{$sourceId}"; $dataInput = $document->createElementNS($taskNS, "{$taskPrefix}dataInput"); - $dataInput->setAttribute("id", $dataInputId); - $dataInput->setAttribute("name", "Template for protocol"); + $dataInput->setAttribute('id', $dataInputId); + $dataInput->setAttribute('name', 'Template for protocol'); $ioSpec->appendChild($dataInput); $inputSet = $document->createElementNS($taskNS, "{$taskPrefix}inputSet"); - $inputSet->setAttribute("id", "{$taskId}_inner_" . (round(microtime(true) * 1000) + 2)); + $inputSet->setAttribute('id', "{$taskId}_inner_" . (round(microtime(true) * 1000) + 2)); $dataInputRefs = $document->createElementNS($taskNS, "{$taskPrefix}dataInputRefs", $dataInputId); $inputSet->appendChild($dataInputRefs); $ioSpec->appendChild($inputSet); $outputSet = $document->createElementNS($taskNS, "{$taskPrefix}outputSet"); - $outputSet->setAttribute("id", "{$taskId}_inner_" . (round(microtime(true) * 1000) + 3)); + $outputSet->setAttribute('id', "{$taskId}_inner_" . (round(microtime(true) * 1000) + 3)); $ioSpec->appendChild($outputSet); $task->insertBefore($ioSpec, $dataInputAssociation); @@ -99,7 +99,7 @@ public static function fix(string $bpmn): string // Add BPMNEdge to BPMNDiagram $diagramNodes = $xpath->query('//*[local-name() = "BPMNDiagram"]'); if ($diagramNodes->length === 0) { - throw new Exception("No BPMNDiagram node found in the BPMN file."); + throw new Exception('No BPMNDiagram node found in the BPMN file.'); } $bpmnDiagram = $diagramNodes->item(0); $diagramPrefix = $bpmnDiagram->prefix; @@ -107,23 +107,23 @@ public static function fix(string $bpmn): string $bpmnPlaneNodes = $xpath->query('.//*[local-name() = "BPMNPlane"]', $bpmnDiagram); if ($bpmnPlaneNodes->length === 0) { - throw new Exception("No BPMNPlane found inside BPMNDiagram."); + throw new Exception('No BPMNPlane found inside BPMNDiagram.'); } $bpmnPlane = $bpmnPlaneNodes->item(0); - $edgeId = 'BPMNEdge_' . $dataInputAssociation->getAttribute("id"); + $edgeId = 'BPMNEdge_' . $dataInputAssociation->getAttribute('id'); $bpmnEdge = $document->createElementNS($diagramNS, "{$diagramPrefix}:BPMNEdge"); - $bpmnEdge->setAttribute("id", $edgeId); - $bpmnEdge->setAttribute("bpmnElement", $dataInputAssociation->getAttribute("id")); + $bpmnEdge->setAttribute('id', $edgeId); + $bpmnEdge->setAttribute('bpmnElement', $dataInputAssociation->getAttribute('id')); $diNS = 'http://www.omg.org/spec/DD/20100524/DI'; - $waypoint1 = $document->createElementNS($diNS, "di:waypoint"); - $waypoint1->setAttribute("x", "100"); - $waypoint1->setAttribute("y", "100"); + $waypoint1 = $document->createElementNS($diNS, 'di:waypoint'); + $waypoint1->setAttribute('x', '100'); + $waypoint1->setAttribute('y', '100'); - $waypoint2 = $document->createElementNS($diNS, "di:waypoint"); - $waypoint2->setAttribute("x", "200"); - $waypoint2->setAttribute("y", "200"); + $waypoint2 = $document->createElementNS($diNS, 'di:waypoint'); + $waypoint2->setAttribute('x', '200'); + $waypoint2->setAttribute('y', '200'); $bpmnEdge->appendChild($waypoint1); $bpmnEdge->appendChild($waypoint2); diff --git a/ProcessMaker/Notifications/ActivityActivatedNotification.php b/ProcessMaker/Notifications/ActivityActivatedNotification.php index 457fd8b542..bf0f625ffc 100644 --- a/ProcessMaker/Notifications/ActivityActivatedNotification.php +++ b/ProcessMaker/Notifications/ActivityActivatedNotification.php @@ -53,7 +53,7 @@ public function via($notifiable) * Get the mail representation of the notification. * * @param mixed $notifiable - * @return \Illuminate\Notifications\Messages\MailMessage + * @return MailMessage */ public function toMail($notifiable) { diff --git a/ProcessMaker/Notifications/ActivityCompletedNotification.php b/ProcessMaker/Notifications/ActivityCompletedNotification.php index 55e42de096..8e7a6e121f 100644 --- a/ProcessMaker/Notifications/ActivityCompletedNotification.php +++ b/ProcessMaker/Notifications/ActivityCompletedNotification.php @@ -53,7 +53,7 @@ public function via($notifiable) * Get the mail representation of the notification. * * @param mixed $notifiable - * @return \Illuminate\Notifications\Messages\MailMessage + * @return MailMessage */ public function toMail($notifiable) { diff --git a/ProcessMaker/Notifications/ErrorExecutionNotification.php b/ProcessMaker/Notifications/ErrorExecutionNotification.php index b470618875..5a9422ffde 100644 --- a/ProcessMaker/Notifications/ErrorExecutionNotification.php +++ b/ProcessMaker/Notifications/ErrorExecutionNotification.php @@ -55,7 +55,7 @@ public function via($notifiable) * Get the mail representation of the notification. * * @param mixed $notifiable - * @return \Illuminate\Notifications\Messages\MailMessage + * @return MailMessage */ public function toMail($notifiable) { @@ -77,7 +77,7 @@ public function toMail($notifiable) * Get the broadcast representation of the notification. * * @param mixed $notifiable - * @return \Illuminate\Notifications\Messages\BroadcastMessage + * @return BroadcastMessage */ public function toBroadcast($notifiable) { diff --git a/ProcessMaker/Notifications/ImportReady.php b/ProcessMaker/Notifications/ImportReady.php index a408eb651f..467c12322e 100644 --- a/ProcessMaker/Notifications/ImportReady.php +++ b/ProcessMaker/Notifications/ImportReady.php @@ -67,7 +67,7 @@ public function toArray($notifiable) * * @param mixed $notifiable * - * @return \Illuminate\Notifications\Messages\BroadcastMessage + * @return BroadcastMessage */ public function toBroadcast($notifiable) { diff --git a/ProcessMaker/Notifications/ProcessCanceledNotification.php b/ProcessMaker/Notifications/ProcessCanceledNotification.php index 38fb616989..f7bfd7f72b 100644 --- a/ProcessMaker/Notifications/ProcessCanceledNotification.php +++ b/ProcessMaker/Notifications/ProcessCanceledNotification.php @@ -46,7 +46,7 @@ public function via($notifiable) * Get the mail representation of the notification. * * @param mixed $notifiable - * @return \Illuminate\Notifications\Messages\MailMessage + * @return MailMessage */ public function toMail($notifiable) { diff --git a/ProcessMaker/Notifications/ProcessCompletedNotification.php b/ProcessMaker/Notifications/ProcessCompletedNotification.php index c3cc0e655b..cfc21fd912 100644 --- a/ProcessMaker/Notifications/ProcessCompletedNotification.php +++ b/ProcessMaker/Notifications/ProcessCompletedNotification.php @@ -46,7 +46,7 @@ public function via($notifiable) * Get the mail representation of the notification. * * @param mixed $notifiable - * @return \Illuminate\Notifications\Messages\MailMessage + * @return MailMessage */ public function toMail($notifiable) { diff --git a/ProcessMaker/Notifications/ProcessCreatedNotification.php b/ProcessMaker/Notifications/ProcessCreatedNotification.php index c5cc3ccf7c..88de7d7eaf 100644 --- a/ProcessMaker/Notifications/ProcessCreatedNotification.php +++ b/ProcessMaker/Notifications/ProcessCreatedNotification.php @@ -46,7 +46,7 @@ public function via($notifiable) * Get the mail representation of the notification. * * @param mixed $notifiable - * @return \Illuminate\Notifications\Messages\MailMessage + * @return MailMessage */ public function toMail($notifiable) { diff --git a/ProcessMaker/Notifications/ProcessTranslationReady.php b/ProcessMaker/Notifications/ProcessTranslationReady.php index 5bdb8b9b92..58a7eeeb26 100644 --- a/ProcessMaker/Notifications/ProcessTranslationReady.php +++ b/ProcessMaker/Notifications/ProcessTranslationReady.php @@ -68,7 +68,7 @@ public function toArray($notifiable) * * @param mixed $notifiable * - * @return \Illuminate\Notifications\Messages\BroadcastMessage + * @return BroadcastMessage */ public function toBroadcast($notifiable) { diff --git a/ProcessMaker/Notifications/ResetPassword.php b/ProcessMaker/Notifications/ResetPassword.php index eefc8793c4..8a2fddc8a1 100644 --- a/ProcessMaker/Notifications/ResetPassword.php +++ b/ProcessMaker/Notifications/ResetPassword.php @@ -27,7 +27,7 @@ protected function resetUrl($notifiable): string * Get the reset password notification mail message for the given URL. * * @param string $url - * @return \Illuminate\Notifications\Messages\MailMessage + * @return MailMessage */ protected function buildMailMessage($url) { diff --git a/ProcessMaker/Notifications/SassCompiledNotification.php b/ProcessMaker/Notifications/SassCompiledNotification.php index ccb747521c..5453830683 100644 --- a/ProcessMaker/Notifications/SassCompiledNotification.php +++ b/ProcessMaker/Notifications/SassCompiledNotification.php @@ -44,7 +44,7 @@ public function via($notifiable) * Get the mail representation of the notification. * * @param mixed $notifiable - * @return \Illuminate\Notifications\Messages\MailMessage + * @return MailMessage */ public function toMail($notifiable) { diff --git a/ProcessMaker/Notifications/ScriptResponseNotification.php b/ProcessMaker/Notifications/ScriptResponseNotification.php index 31e880da27..5601583931 100644 --- a/ProcessMaker/Notifications/ScriptResponseNotification.php +++ b/ProcessMaker/Notifications/ScriptResponseNotification.php @@ -76,7 +76,7 @@ public function toArray($notifiable) * * @param mixed $notifiable * - * @return \Illuminate\Notifications\Messages\BroadcastMessage + * @return BroadcastMessage */ public function toBroadcast($notifiable) { diff --git a/ProcessMaker/Notifications/TaskReassignmentNotification.php b/ProcessMaker/Notifications/TaskReassignmentNotification.php index c2d59867f9..d1337fcddc 100644 --- a/ProcessMaker/Notifications/TaskReassignmentNotification.php +++ b/ProcessMaker/Notifications/TaskReassignmentNotification.php @@ -53,7 +53,7 @@ public function via($notifiable) * Get the mail representation of the notification. * * @param mixed $notifiable - * @return \Illuminate\Notifications\Messages\MailMessage + * @return MailMessage */ public function toMail($notifiable) { diff --git a/ProcessMaker/Policies/MediaPolicy.php b/ProcessMaker/Policies/MediaPolicy.php index 627ae09835..dd7de7be7a 100644 --- a/ProcessMaker/Policies/MediaPolicy.php +++ b/ProcessMaker/Policies/MediaPolicy.php @@ -14,7 +14,7 @@ class MediaPolicy * Run before all methods to determine if the * user is an admin and can do everything. * - * @param \ProcessMaker\Models\User $user + * @param User $user * @return mixed */ public function before(User $user) @@ -27,8 +27,8 @@ public function before(User $user) /** * Determine whether the user can view the media. * - * @param \ProcessMaker\Models\User $user - * @param \ProcessMaker\Models\Media $media + * @param User $user + * @param Media $media * @return mixed */ public function view(User $user, Media $media) @@ -43,7 +43,7 @@ public function view(User $user, Media $media) /** * Determine whether the user can create media. * - * @param \ProcessMaker\Models\User $user + * @param User $user * @return mixed */ public function create(User $user) @@ -78,8 +78,8 @@ public function create(User $user) /** * Determine whether the user can update the media. * - * @param \ProcessMaker\Models\User $user - * @param \ProcessMaker\Models\Media $media + * @param User $user + * @param Media $media * @return mixed */ public function update(User $user, Media $media) @@ -94,8 +94,8 @@ public function update(User $user, Media $media) /** * Determine whether the user can delete the media. * - * @param \ProcessMaker\Models\User $user - * @param \ProcessMaker\Models\Media $media + * @param User $user + * @param Media $media * @return mixed */ public function delete(User $user, Media $media) diff --git a/ProcessMaker/Policies/ProcessRequestPolicy.php b/ProcessMaker/Policies/ProcessRequestPolicy.php index 508523ee6f..d55762cef1 100644 --- a/ProcessMaker/Policies/ProcessRequestPolicy.php +++ b/ProcessMaker/Policies/ProcessRequestPolicy.php @@ -14,7 +14,7 @@ class ProcessRequestPolicy * Run before all methods to determine if the * user is an admin and can do everything. * - * @param \ProcessMaker\Models\User $user + * @param User $user * @return mixed */ public function before(User $user) @@ -27,8 +27,8 @@ public function before(User $user) /** * Determine whether the user can view the process request. * - * @param \ProcessMaker\Models\User $user - * @param \ProcessMaker\Models\ProcessRequest $processRequest + * @param User $user + * @param ProcessRequest $processRequest * @return mixed */ public function view(User $user, ProcessRequest $processRequest) @@ -46,8 +46,8 @@ public function view(User $user, ProcessRequest $processRequest) /** * Determine whether the user can update the process request. * - * @param \ProcessMaker\Models\User $user - * @param \ProcessMaker\Models\ProcessRequest $processRequest + * @param User $user + * @param ProcessRequest $processRequest * @return mixed */ public function update(User $user, ProcessRequest $processRequest) @@ -64,8 +64,8 @@ public function update(User $user, ProcessRequest $processRequest) /** * Determine whether the user can update the process request. * - * @param \ProcessMaker\Models\User $user - * @param \ProcessMaker\Models\ProcessRequest $processRequest + * @param User $user + * @param ProcessRequest $processRequest * @return mixed */ public function destroy(User $user, ProcessRequest $processRequest) @@ -78,7 +78,7 @@ public function destroy(User $user, ProcessRequest $processRequest) /** * Determine whether the user can edit request data. * - * @param \ProcessMaker\Models\User $user + * @param User $user * @param \ProcessMaker\Process $process * @return bool */ @@ -98,8 +98,8 @@ public function editData(User $user, ProcessRequest $request) /** * User has access if participates in the request. * - * @param \ProcessMaker\Models\User $user - * @param \ProcessMaker\Models\ProcessRequest $processRequest + * @param User $user + * @param ProcessRequest $processRequest * * @return mixed */ diff --git a/ProcessMaker/Policies/ScreenPolicy.php b/ProcessMaker/Policies/ScreenPolicy.php index 37c24c1217..367f1acbd7 100644 --- a/ProcessMaker/Policies/ScreenPolicy.php +++ b/ProcessMaker/Policies/ScreenPolicy.php @@ -15,7 +15,7 @@ class ScreenPolicy /** * Determine whether the user can view the screen. * - * @param \ProcessMaker\Models\User $user + * @param User $user * @param \ProcessMaker\Screen $screen * @return mixed */ diff --git a/ProcessMaker/Policies/UserPolicy.php b/ProcessMaker/Policies/UserPolicy.php index d84995f07a..d3cf2785f5 100644 --- a/ProcessMaker/Policies/UserPolicy.php +++ b/ProcessMaker/Policies/UserPolicy.php @@ -13,7 +13,7 @@ class UserPolicy * Run before all methods to determine if the * user is an admin and can do everything. * - * @param \ProcessMaker\Models\User $user + * @param User $user * @return mixed */ public function before(User $user) @@ -26,8 +26,8 @@ public function before(User $user) /** * Determine whether the user can view the user. * - * @param \ProcessMaker\Models\User $user - * @param \ProcessMaker\Models\User $targetUser + * @param User $user + * @param User $targetUser * @return mixed */ public function view(User $user, User $targetUser) @@ -42,8 +42,8 @@ public function view(User $user, User $targetUser) /** * Determine whether the user can update the user. * - * @param \ProcessMaker\Models\User $user - * @param \ProcessMaker\Models\User $targetUser + * @param User $user + * @param User $targetUser * @return mixed */ public function edit(User $user, User $targetUser) @@ -58,8 +58,8 @@ public function edit(User $user, User $targetUser) /** * Determine whether the user can update the user. * - * @param \ProcessMaker\Models\User $user - * @param \ProcessMaker\Models\User $targetUser + * @param User $user + * @param User $targetUser * @return mixed */ public function destroy(User $user, User $targetUser) diff --git a/ProcessMaker/Providers/MixServiceProvider.php b/ProcessMaker/Providers/MixServiceProvider.php index b1d916552f..b8399a3664 100644 --- a/ProcessMaker/Providers/MixServiceProvider.php +++ b/ProcessMaker/Providers/MixServiceProvider.php @@ -3,9 +3,12 @@ namespace ProcessMaker\Providers; use Illuminate\Foundation\Mix; +use Illuminate\Foundation\MixFileNotFoundException; use Illuminate\Support\Facades\Cache; use Illuminate\Support\ServiceProvider; +use Illuminate\Support\HtmlString; use Illuminate\Support\Str; +use ProcessMaker\Support\PluginVendorAssets; class MixServiceProvider extends ServiceProvider { @@ -16,6 +19,9 @@ public function register(): void { $this->app->singleton(Mix::class, function ($app) { return new class extends Mix { + /** @var array|null> */ + private static array $tenantPluginManifests = []; + /** * Get the path to a versioned Mix file. * @@ -27,6 +33,15 @@ public function register(): void */ public function __invoke($path, $manifestDirectory = '') { + if (!str_starts_with($path, '/')) { + $path = '/' . $path; + } + + $tenantPluginUrl = $this->resolveTenantPluginVendorUrl($path, $manifestDirectory); + if ($tenantPluginUrl !== null) { + return $tenantPluginUrl; + } + $tenant = app()->bound('currentTenant') ? app('currentTenant') : null; if (!$tenant) { @@ -64,11 +79,86 @@ public function __invoke($path, $manifestDirectory = '') if (count($matchingKeys) === 1) { $path = $matchingKeys[0]; - // \Log::info('MixServiceProvider: ' . $path); } return parent::__invoke($path, $manifestDirectory); } + + /** + * @return HtmlString|null Tenant plugin asset URL, or null to fall back to default mix(). + */ + private function resolveTenantPluginVendorUrl(string $path, string $manifestDirectory): ?HtmlString + { + if ($manifestDirectory === '') { + return null; + } + + if (!PluginVendorAssets::isEnabled()) { + return null; + } + + $packageName = PluginVendorAssets::parsePackageFromManifestDirectory( + trim(str_replace('\\', '/', $manifestDirectory), '/') + ); + if ($packageName === null) { + return null; + } + + $tenantId = PluginVendorAssets::currentTenantId(); + if ($tenantId === null) { + return null; + } + + $relativeDirectory = PluginVendorAssets::manifestRelativeDirectory($tenantId, $packageName); + $manifestFilesystemPath = PluginVendorAssets::mixManifestFilesystemPath($tenantId, $packageName); + + if (!is_file($manifestFilesystemPath)) { + return null; + } + + if (is_file(public_path($relativeDirectory . '/hot'))) { + return $this->resolveHotUrl($path, public_path($relativeDirectory . '/hot')); + } + + if (!isset(self::$tenantPluginManifests[$manifestFilesystemPath])) { + self::$tenantPluginManifests[$manifestFilesystemPath] = json_decode( + file_get_contents($manifestFilesystemPath), + true + ); + } + + $manifest = self::$tenantPluginManifests[$manifestFilesystemPath]; + if (!is_array($manifest) || !isset($manifest[$path])) { + if (!app('config')->get('app.debug')) { + report(new MixFileNotFoundException("Unable to locate Mix file: {$path}.")); + + return new HtmlString($path); + } + + throw new MixFileNotFoundException("Unable to locate Mix file: {$path}."); + } + + $url = app('config')->get('app.mix_url') + . PluginVendorAssets::webPath($tenantId, $packageName, (string) $manifest[$path]); + + return new HtmlString($url); + } + + private function resolveHotUrl(string $path, string $hotFilePath): HtmlString + { + $url = rtrim(file_get_contents($hotFilePath)); + + $customUrl = app('config')->get('app.mix_hot_proxy_url'); + if (!empty($customUrl)) { + return new HtmlString("{$customUrl}{$path}"); + } + + if (Str::startsWith($url, ['http://', 'https://'])) { + return new HtmlString(Str::after($url, ':') . $path); + } + + return new HtmlString('//localhost:8080' . $path); + } }; }); } diff --git a/ProcessMaker/Providers/PluginServiceProvider.php b/ProcessMaker/Providers/PluginServiceProvider.php new file mode 100644 index 0000000000..726ea4b80a --- /dev/null +++ b/ProcessMaker/Providers/PluginServiceProvider.php @@ -0,0 +1,152 @@ +loadPlugins(); + }); + } + + /** + * Load all plugins from the storage/plugins directory. + */ + protected function loadPlugins(): void + { + $pluginsFolder = storage_path('plugins'); + $pluginPaths = glob($pluginsFolder . '/*', GLOB_ONLYDIR); + + foreach ($pluginPaths as $pluginPath) { + // Ignore plugins that start with _ + if (str_starts_with(basename($pluginPath), '_')) { + continue; + } + + try { + $this->loadPlugin($pluginPath); + } catch (Throwable $exception) { + Log::error('Skipping plugin at ' . $pluginPath . ': ' . $exception->getMessage()); + } + } + } + + /** + * Load a single plugin from the given path. + * + * @param string $pluginPath + */ + protected function loadPlugin(string $pluginPath): void + { + $composerJsonPath = $pluginPath . '/composer.json'; + + if (!file_exists($composerJsonPath)) { + return; + } + + $composerJson = $this->loadComposerJson($composerJsonPath); + if (!$this->registerAutoloader($pluginPath, $composerJson)) { + Log::warning( + 'Skipping plugin at ' . $pluginPath . ': dependencies are not installed.' + ); + + return; + } + + $this->registerServiceProviders($composerJson); + } + + /** + * Load and decode composer.json file. + * + * @param string $composerJsonPath + * @return array + */ + protected function loadComposerJson(string $composerJsonPath): array + { + $content = file_get_contents($composerJsonPath); + $composerJson = json_decode($content, true); + + if (json_last_error() !== JSON_ERROR_NONE) { + throw new RuntimeException( + "Invalid JSON in composer.json at {$composerJsonPath}: " . json_last_error_msg() + ); + } + + return $composerJson; + } + + /** + * Register PSR-4 autoloader for the plugin. + * + * @param string $pluginPath + * @param array $composerJson + * @return bool False when the plugin has no autoload file yet + */ + protected function registerAutoloader(string $pluginPath, array $composerJson): bool + { + $autoloadPath = $pluginPath . '/vendor/autoload.php'; + + if (!file_exists($autoloadPath)) { + return false; + } + + $loader = require_once $autoloadPath; + if ($loader === true) { + // Already loaded and presumably registered + return true; + } + + $psr4Namespaces = Arr::get($composerJson, 'autoload.psr-4', []); + + foreach ($psr4Namespaces as $namespace => $path) { + $fullPath = $pluginPath . '/' . $path; + + if (!is_dir($fullPath)) { + throw new RuntimeException( + "Plugin {$pluginPath} has a PSR-4 path that does not exist: {$path}" + ); + } + + $loader->addPsr4($namespace, $fullPath); + } + + return true; + } + + /** + * Register Laravel service providers defined in the plugin's composer.json. + * + * @param array $composerJson + */ + protected function registerServiceProviders(array $composerJson): void + { + $providers = Arr::get($composerJson, 'extra.laravel.providers', []); + + foreach ($providers as $provider) { + if (!class_exists($provider)) { + Log::warning('Skipping plugin provider ' . $provider . ': class not found.'); + continue; + } + + app()->register($provider); + } + } +} diff --git a/ProcessMaker/Providers/WorkflowServiceProvider.php b/ProcessMaker/Providers/WorkflowServiceProvider.php index fffbff740f..d224c2bbd6 100644 --- a/ProcessMaker/Providers/WorkflowServiceProvider.php +++ b/ProcessMaker/Providers/WorkflowServiceProvider.php @@ -5,12 +5,12 @@ use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider; use Mustache_Engine; use Mustache_LambdaHelper; +use ProcessMaker\Assets\DataSourcesInProcess; +use ProcessMaker\Assets\DataSourcesInScreen; use ProcessMaker\Assets\ScreensInProcess; use ProcessMaker\Assets\ScreensInScreen; use ProcessMaker\Assets\ScriptsInProcess; use ProcessMaker\Assets\ScriptsInScreen; -use ProcessMaker\Assets\DataSourcesInScreen; -use ProcessMaker\Assets\DataSourcesInProcess; use ProcessMaker\Bpmn\MustacheOptions; use ProcessMaker\BpmnEngine; use ProcessMaker\Contracts\TimerExpressionInterface; @@ -38,6 +38,9 @@ use ProcessMaker\Nayra\MessageBrokers\ServiceInterface; use ProcessMaker\Repositories\BpmnDocument; use ProcessMaker\Repositories\DefinitionsRepository; +use ProcessMaker\ScriptRuntime\ScriptModuleRegistry; +use ProcessMaker\ScriptRuntime\ScriptRuntime; +use ProcessMaker\ServiceTaskImplementations\CoreServiceTask; use ProcessMaker\WebServices\Contracts\SoapClientInterface; use ProcessMaker\WebServices\NativeSoapClient; use ProcessMaker\WebServices\SoapConfigBuilder; @@ -239,6 +242,25 @@ function (ThrowEventInterface $source, EventDefinitionInterface $sourceEventDefi return ServiceFactory::create(); }); + $this->app->singleton(ScriptModuleRegistry::class); + $this->app->singleton(ScriptRuntime::class, function ($app) { + return new ScriptRuntime($app->make(ScriptModuleRegistry::class)); + }); + $this->app->alias(ScriptRuntime::class, 'script.runtime'); + parent::register(); } + + /** + * Bootstrap workflow services. + */ + public function boot(): void + { + parent::boot(); + + WorkflowManagerFacade::registerServiceImplementation( + CoreServiceTask::IMPLEMENTATION, + CoreServiceTask::class + ); + } } diff --git a/ProcessMaker/Rules/SortBy.php b/ProcessMaker/Rules/SortBy.php index 2fb3398141..610e3b92e4 100644 --- a/ProcessMaker/Rules/SortBy.php +++ b/ProcessMaker/Rules/SortBy.php @@ -10,7 +10,7 @@ class SortBy implements ValidationRule /** * Run the validation rule. * - * @param \Closure(string): \Illuminate\Translation\PotentiallyTranslatedString $fail + * @param Closure(string): \Illuminate\Translation\PotentiallyTranslatedString $fail */ public function validate(string $attribute, mixed $value, Closure $fail): void { diff --git a/ProcessMaker/ScriptRunners/InProcessPhpRunner.php b/ProcessMaker/ScriptRunners/InProcessPhpRunner.php new file mode 100644 index 0000000000..5dc74138db --- /dev/null +++ b/ProcessMaker/ScriptRunners/InProcessPhpRunner.php @@ -0,0 +1,188 @@ +createWorkDir(); + $accessToken = null; + + try { + file_put_contents($workDir . '/data.json', json_encode($data)); + file_put_contents($workDir . '/config.json', json_encode($config)); + file_put_contents($workDir . '/script.php', $this->normalizeCode($code)); + + if ($user) { + $accessToken = new GenerateAccessToken($user); + } + + $phpBinary = config('core-service-task.php_binary', PHP_BINARY); + $memoryLimit = config('core-service-task.memory_limit', '256M'); + $bootstrap = $this->bootstrapPath(); + + $env = $this->buildEnvironment($user, $accessToken); + + Log::debug('Executing in-process PHP script', [ + 'timeout' => $timeout, + 'php' => $phpBinary, + ]); + + $result = Process::timeout($timeout) + ->env($env) + ->run([ + $phpBinary, + '-d', + 'memory_limit=' . $memoryLimit, + $bootstrap, + $workDir, + ]); + + if (!$result->successful()) { + $message = trim($result->errorOutput() . "\n" . $result->output()); + throw new ScriptException($message !== '' ? $message : 'In-process PHP script failed'); + } + + $decoded = json_decode($result->output(), true); + if (!is_array($decoded) || !array_key_exists('output', $decoded)) { + throw new ScriptException('Invalid in-process script output'); + } + + return $decoded['output']; + } catch (ProcessTimedOutException $exception) { + Log::error('In-process PHP script timed out', ['timeout' => $timeout]); + throw new ScriptTimeoutException( + __('Script took too long to complete. Consider increasing the timeout.') + . "\n" + . __('Timeout: :timeout seconds', ['timeout' => $timeout]) + ); + } catch (ScriptException|ScriptTimeoutException|InProcessScriptException $exception) { + throw $exception; + } catch (Throwable $exception) { + throw new ScriptException($exception->getMessage(), (int) $exception->getCode(), $exception); + } finally { + if ($accessToken) { + $accessToken->delete(); + } + $this->removeWorkDir($workDir); + } + } + + private function normalizeCode(string $code): string + { + $trimmed = ltrim($code); + if (Str::startsWith($trimmed, ' $value) { + if (!is_string($key) || (!is_string($value) && !is_numeric($value))) { + unset($env[$key]); + } else { + $env[$key] = (string) $value; + } + } + + EnvironmentVariable::chunk(50, function ($variables) use (&$env) { + foreach ($variables as $variable) { + $name = str_replace(' ', '_', $variable->name); + $env[$name] = (string) $variable->value; + } + }); + + $env['HOST_URL'] = (string) config('app.docker_host_url'); + $env['APP_URL'] = (string) config('app.docker_host_url'); + + if (config('smart-extract.api_host') !== null) { + $env['SMART_EXTRACT_API_HOST'] = (string) config('smart-extract.api_host'); + } + if (config('smart-extract.request_timeout') !== null) { + $env['SMART_EXTRACT_REQUEST_TIMEOUT'] = (string) config('smart-extract.request_timeout'); + } + + if ($user && $accessToken) { + $env['API_TOKEN'] = $accessToken->getToken(); + $env['API_HOST'] = config('app.docker_host_url') . '/api/1.0'; + $env['API_SSL_VERIFY'] = config('app.api_ssl_verify') ? '1' : '0'; + } + + return $env; + } +} diff --git a/ProcessMaker/ScriptRunners/ScriptRunner.php b/ProcessMaker/ScriptRunners/ScriptRunner.php index 0393f68c4b..161cdd3e26 100644 --- a/ProcessMaker/ScriptRunners/ScriptRunner.php +++ b/ProcessMaker/ScriptRunners/ScriptRunner.php @@ -5,18 +5,24 @@ use Illuminate\Contracts\Container\BindingResolutionException; use ProcessMaker\Enums\ScriptExecutorType; use ProcessMaker\Exception\ScriptLanguageNotSupported; +use ProcessMaker\Facades\ScriptRuntime; use ProcessMaker\Models\Script; use ProcessMaker\Models\ScriptExecutor; +use ProcessMaker\Models\User; +use ProcessMaker\ScriptRuntime\ScriptExecutionContext; +use ProcessMaker\Services\ScriptExecutionModeResolver; class ScriptRunner { /** - * Concrete script runner + * Concrete script runner (Docker / microservice / mock). * - * @var Base + * @var Base|ScriptMicroserviceRunner|MockRunner */ private $runner; + private string $tokenId = ''; + public function __construct(protected Script $script) { $this->runner = $this->getScriptRunner($this->script->scriptExecutor); @@ -25,17 +31,27 @@ public function __construct(protected Script $script) /** * Run a script code. * + * When core-service-task is enabled and the script has allow_in_process=true (PHP), + * runs via ScriptRuntime / InProcessPhpRunner and skips Docker/microservice. + * * @param string $code * @param array $data * @param array $config * @param int $timeout - * @param \ProcessMaker\Models\User $user + * @param User $user + * @param mixed $sync + * @param array $metadata + * + * @return array{output: mixed} * - * @return array * @throws \RuntimeException */ public function run($code, array $data, array $config, $timeout, $user, $sync, $metadata) { + if ($this->shouldRunInProcess()) { + return $this->runInProcess($code, $data, $config, $timeout, $user); + } + return $this->runner->run($code, $data, $config, $timeout, $user, $sync, $metadata); } @@ -77,6 +93,72 @@ private function getScriptRunner(ScriptExecutor $executor): Base|ScriptMicroserv */ public function setTokenId($tokenId) { - $this->runner->setTokenId($tokenId); + $this->tokenId = (string) $tokenId; + if (method_exists($this->runner, 'setTokenId')) { + $this->runner->setTokenId($tokenId); + } + } + + private function shouldRunInProcess(): bool + { + if (!config('core-service-task.enabled')) { + return false; + } + + if (!$this->script->allow_in_process) { + return false; + } + + return strtolower((string) $this->script->language) === 'php'; + } + + /** + * @return array{output: mixed} + */ + private function runInProcess($code, array $data, array $config, $timeout, ?User $user): array + { + $effectiveTimeout = $this->resolveTimeout((int) $timeout, (int) $this->script->timeout); + + if ( + app(ScriptExecutionModeResolver::class)->resolve($this->script) + === Script::EXECUTION_MODE_MODULES + ) { + $context = new ScriptExecutionContext( + data: $data, + config: $config, + user: $user, + tokenId: $this->tokenId, + timeout: $effectiveTimeout, + scriptId: (int) $this->script->id, + source: 'script-task', + ); + + $output = ScriptRuntime::run($code, $context); + } else { + $output = app(InProcessPhpRunner::class)->run( + $code, + $data, + $config, + $effectiveTimeout, + $user + ); + } + + // Match Docker / runScript() response shape + return ['output' => ScriptRuntime::normalizeOutput($output)]; + } + + private function resolveTimeout(int $requestedTimeout, int $scriptTimeout): int + { + $timeout = $requestedTimeout > 0 + ? $requestedTimeout + : ($scriptTimeout > 0 ? $scriptTimeout : (int) config('core-service-task.default_timeout', 60)); + + $max = (int) config('core-service-task.max_timeout', 300); + if ($max > 0 && $timeout > $max) { + return $max; + } + + return max(1, $timeout); } } diff --git a/ProcessMaker/ScriptRunners/resources/in-process-bootstrap.php b/ProcessMaker/ScriptRunners/resources/in-process-bootstrap.php new file mode 100644 index 0000000000..efca7fe2ab --- /dev/null +++ b/ProcessMaker/ScriptRunners/resources/in-process-bootstrap.php @@ -0,0 +1,60 @@ +setAccessToken(getenv('API_TOKEN')); + $apiConfig->setHost(getenv('API_HOST')); + if (class_exists('ProcessMaker\\Client\\Api\\ScriptsApi')) { + // SDK clients vary by generated package; leave $api as configuration helper when full Executor\Api is absent. + $api = $apiConfig; + } + if (class_exists('Executor\\Api')) { + $api = new Executor\Api( + $apiConfig, + isset($_ENV['API_SSL_VERIFY']) ? (bool) $_ENV['API_SSL_VERIFY'] : true + ); + } +} + +$response = require_once $scriptPath; + +echo json_encode(['output' => $response]); diff --git a/ProcessMaker/ScriptRuntime/ScriptExecutionContext.php b/ProcessMaker/ScriptRuntime/ScriptExecutionContext.php new file mode 100644 index 0000000000..cf1060c18b --- /dev/null +++ b/ProcessMaker/ScriptRuntime/ScriptExecutionContext.php @@ -0,0 +1,22 @@ +> + */ + private array $modules = []; + + /** + * Register a module class. Key is taken from Module::key() unless overridden. + * + * @param class-string $moduleClass + * @param string|null $key Optional override + */ + public function register(string $moduleClass, ?string $key = null): void + { + if (!is_subclass_of($moduleClass, ScriptModuleInterface::class)) { + throw new InvalidArgumentException( + $moduleClass . ' must implement ' . ScriptModuleInterface::class + ); + } + + $resolvedKey = $key ?: $moduleClass::key(); + $resolvedKey = strtolower(trim($resolvedKey)); + + if ($resolvedKey === '' || !preg_match('/^[a-z][a-z0-9_]*$/', $resolvedKey)) { + throw new InvalidArgumentException('Invalid script module key: ' . $resolvedKey); + } + + if (in_array($resolvedKey, self::RESERVED_KEYS, true)) { + throw new InvalidArgumentException( + 'Script module key is reserved: ' . $resolvedKey + ); + } + + $this->modules[$resolvedKey] = $moduleClass; + } + + public function has(string $key): bool + { + return isset($this->modules[strtolower($key)]); + } + + /** + * @return class-string|null + */ + public function get(string $key): ?string + { + return $this->modules[strtolower($key)] ?? null; + } + + /** + * @return array> + */ + public function all(): array + { + return $this->modules; + } + + /** + * @return list + */ + public function keys(): array + { + return array_keys($this->modules); + } + + /** + * Aggregated catalog for discovery APIs / docs. + * + * @return list> + */ + public function catalog(): array + { + $items = []; + foreach ($this->modules as $key => $class) { + $items[] = [ + 'key' => $key, + 'label' => $class::label(), + 'class' => $class, + 'methods' => $class::catalog(), + ]; + } + + return $items; + } +} diff --git a/ProcessMaker/ScriptRuntime/ScriptRuntime.php b/ProcessMaker/ScriptRuntime/ScriptRuntime.php new file mode 100644 index 0000000000..43443d004e --- /dev/null +++ b/ProcessMaker/ScriptRuntime/ScriptRuntime.php @@ -0,0 +1,318 @@ + $moduleClass + */ + public function registerModule(string $moduleClass, ?string $key = null): void + { + $this->registry->register($moduleClass, $key); + } + + public function registry(): ScriptModuleRegistry + { + return $this->registry; + } + + /** + * @return list> + */ + public function catalog(): array + { + return $this->registry->catalog(); + } + + /** + * Run script code with $data, $config, $modules and per-module variables. + * + * Also exposes Docker-compatible env vars for the duration of the run: + * API_TOKEN, API_HOST, APP_URL, HOST_URL, EnvironmentVariable rows, etc. + * Readable via getenv('API_TOKEN') inside the script. + * + * @return mixed Script return value + * + * @throws ScriptException + */ + public function run(string $code, ScriptExecutionContext $context): mixed + { + $instances = $this->instantiateModules($context); + $scriptPath = $this->writeTempScript($code); + $hasExplicitReturn = $this->hasExplicitReturn($code); + $accessToken = null; + $envSnapshot = null; + + try { + if ($context->user) { + $accessToken = new GenerateAccessToken($context->user); + } + + $envSnapshot = $this->applyScriptEnvironment($context, $accessToken); + + Log::debug('Executing script via ScriptRuntime (modules)', [ + 'script_id' => $context->scriptId, + 'source' => $context->source, + 'modules' => array_keys($instances), + ]); + + $result = (static function ( + string $__scriptPath, + ScriptExecutionContext $__context, + array $__instances, + bool $__hasExplicitReturn + ) { + $result = ['response' => 'Error: add a result array']; + extract([ + 'data' => $__context->data, + 'config' => $__context->config, + 'modules' => $__instances, + ], EXTR_SKIP); + extract($__instances, EXTR_SKIP); + + $output = include $__scriptPath; + + return (($output === 1 || $output === null) && !$__hasExplicitReturn) + ? $result + : $output; + })($scriptPath, $context, $instances, $hasExplicitReturn); + + return $result; + } catch (Throwable $exception) { + throw new ScriptException($exception->getMessage(), (int) $exception->getCode(), $exception); + } finally { + if ($envSnapshot !== null) { + $this->restoreScriptEnvironment($envSnapshot); + } + if ($accessToken) { + $accessToken->delete(); + } + if (is_file($scriptPath)) { + @unlink($scriptPath); + } + } + } + + /** + * Normalize script return to an array for process data merge. + */ + public function normalizeOutput(mixed $output): array + { + if ($output === null) { + return []; + } + + if (!is_array($output)) { + return ['response' => $output]; + } + + return $output; + } + + /** + * @return array + */ + private function instantiateModules(ScriptExecutionContext $context): array + { + $instances = []; + foreach ($this->registry->all() as $key => $class) { + /** @var ScriptModuleInterface $module */ + $module = app()->make($class); + $module->boot($context); + $instances[$key] = $module; + } + + return $instances; + } + + /** + * Apply script env vars (same contract as Docker / bare InProcessPhpRunner). + * + * @return array{previous: array, keys: list} + */ + private function applyScriptEnvironment(ScriptExecutionContext $context, ?GenerateAccessToken $accessToken): array + { + $values = []; + + EnvironmentVariable::chunk(50, function ($variables) use (&$values) { + foreach ($variables as $variable) { + $name = str_replace(' ', '_', $variable->name); + $values[$name] = (string) $variable->value; + } + }); + + $hostUrl = (string) config('app.docker_host_url'); + $values['HOST_URL'] = $hostUrl; + $values['APP_URL'] = $hostUrl; + + if (config('smart-extract.api_host') !== null) { + $values['SMART_EXTRACT_API_HOST'] = (string) config('smart-extract.api_host'); + } + if (config('smart-extract.request_timeout') !== null) { + $values['SMART_EXTRACT_REQUEST_TIMEOUT'] = (string) config('smart-extract.request_timeout'); + } + + if ($context->user && $accessToken) { + $values['API_TOKEN'] = $accessToken->getToken(); + $values['API_HOST'] = $hostUrl . '/api/1.0'; + $values['API_SSL_VERIFY'] = config('app.api_ssl_verify') ? '1' : '0'; + } + + $previous = []; + foreach ($values as $name => $value) { + $existing = getenv($name); + $previous[$name] = $existing === false ? false : (string) $existing; + putenv($name . '=' . $value); + $_ENV[$name] = $value; + $_SERVER[$name] = $value; + } + + return [ + 'previous' => $previous, + 'keys' => array_keys($values), + ]; + } + + /** + * @param array{previous: array, keys: list} $snapshot + */ + private function restoreScriptEnvironment(array $snapshot): void + { + foreach ($snapshot['keys'] as $name) { + $previous = $snapshot['previous'][$name] ?? false; + if ($previous === false) { + putenv($name); + unset($_ENV[$name], $_SERVER[$name]); + } else { + putenv($name . '=' . $previous); + $_ENV[$name] = $previous; + $_SERVER[$name] = $previous; + } + } + } + + private function writeTempScript(string $code): string + { + $base = config('app.processmaker_scripts_home', storage_path('app')); + $dir = rtrim($base, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . 'script-runtime'; + if (!is_dir($dir) && !mkdir($dir, 0700, true) && !is_dir($dir)) { + throw new RuntimeException('Unable to create script-runtime work directory'); + } + + $path = $dir . DIRECTORY_SEPARATOR . 'script-' . Str::uuid() . '.php'; + $normalized = $this->normalizeCode($code); + if (file_put_contents($path, $normalized) === false) { + throw new RuntimeException('Unable to write temporary script file'); + } + + return $path; + } + + private function normalizeCode(string $code): string + { + $trimmed = ltrim($code); + if (Str::startsWith($trimmed, 'isTopLevelReturn($token, $functionBodyDepths)) { + return true; + } + + $this->processArrayToken($token, $braceDepth, $pendingFunctionBody); + continue; + } + + if ($token === '{') { + $this->openBrace( + $braceDepth, + $functionBodyDepths, + $pendingFunctionBody + ); + continue; + } + + if ($token === '}') { + $this->closeBrace($braceDepth, $functionBodyDepths); + continue; + } + + if ($token === ';') { + $pendingFunctionBody = false; + } + } + + return false; + } + + private function isTopLevelReturn(array $token, array $functionBodyDepths): bool + { + return $token[0] === T_RETURN && empty($functionBodyDepths); + } + + private function processArrayToken( + array $token, + int &$braceDepth, + bool &$pendingFunctionBody + ): void { + if ($token[0] === T_FUNCTION) { + $pendingFunctionBody = true; + } + + if ($token[0] === T_CURLY_OPEN) { + $braceDepth++; + } + } + + private function openBrace( + int &$braceDepth, + array &$functionBodyDepths, + bool &$pendingFunctionBody + ): void { + $braceDepth++; + if ($pendingFunctionBody) { + $functionBodyDepths[] = $braceDepth; + $pendingFunctionBody = false; + } + } + + private function closeBrace(int &$braceDepth, array &$functionBodyDepths): void + { + if (!empty($functionBodyDepths) && end($functionBodyDepths) === $braceDepth) { + array_pop($functionBodyDepths); + } + + $braceDepth--; + } +} diff --git a/ProcessMaker/ServiceTaskImplementations/CoreServiceTask.php b/ProcessMaker/ServiceTaskImplementations/CoreServiceTask.php new file mode 100644 index 0000000000..eb739c884c --- /dev/null +++ b/ProcessMaker/ServiceTaskImplementations/CoreServiceTask.php @@ -0,0 +1,120 @@ +allow_in_process) { + throw new ConfigurationException( + 'Script "' . $script->id . '" is not allowed for in-process execution. Enable allow_in_process on the script.' + ); + } + + if (strtolower((string) $script->language) !== 'php') { + throw new ConfigurationException('Core Service Task only supports PHP scripts'); + } + + $user = User::find($script->run_as_user_id); + if (!$user) { + throw new ConfigurationException('A user is required to run scripts'); + } + + $effectiveTimeout = $this->resolveTimeout((int) $timeout, (int) $script->timeout); + + if ($this->usesModulesExecution($script)) { + $context = new ScriptExecutionContext( + data: $data, + config: $config, + user: $user, + tokenId: (string) $tokenId, + timeout: $effectiveTimeout, + scriptId: (int) $script->id, + source: 'service-task', + ); + + $output = ScriptRuntime::run($script->code, $context); + + return ScriptRuntime::normalizeOutput($output); + } + + $output = app(InProcessPhpRunner::class)->run( + $script->code, + $data, + $config, + $effectiveTimeout, + $user + ); + + return ScriptRuntime::normalizeOutput($output); + } + + private function usesModulesExecution(Script $script): bool + { + return app(ScriptExecutionModeResolver::class)->resolve($script) + === Script::EXECUTION_MODE_MODULES; + } + + /** + * Precedence: requested timeout > script timeout > default; then hard cap. + */ + private function resolveTimeout(int $requestedTimeout, int $scriptTimeout): int + { + if ($requestedTimeout > 0) { + $timeout = $requestedTimeout; + } elseif ($scriptTimeout > 0) { + $timeout = $scriptTimeout; + } else { + $timeout = (int) config('core-service-task.default_timeout', 60); + } + + $max = (int) config('core-service-task.max_timeout', 300); + if ($max > 0 && $timeout > $max) { + return $max; + } + + return max(1, $timeout); + } +} diff --git a/ProcessMaker/Services/PluginRuntimeReload.php b/ProcessMaker/Services/PluginRuntimeReload.php new file mode 100644 index 0000000000..252025fda3 --- /dev/null +++ b/ProcessMaker/Services/PluginRuntimeReload.php @@ -0,0 +1,170 @@ +versionKey()); + + Redis::publish($this->channel(), json_encode([ + 'version' => $version, + 'plugin' => $pluginName, + ])); + + return $version; + } + + public function remoteVersion(): int + { + return (int) Redis::get($this->versionKey()); + } + + public function appliedVersion(): int + { + $path = $this->stateFile(); + if (!is_file($path)) { + return 0; + } + + return (int) file_get_contents($path); + } + + /** + * Record the version on this pod so a later startup does not restart again. + */ + public function rememberApplied(int $version): bool + { + if ($version <= $this->appliedVersion()) { + return true; + } + + return file_put_contents($this->stateFile(), (string) $version) !== false; + } + + public function versionFromMessage(string $message): int + { + $payload = json_decode($message, true); + if (!is_array($payload)) { + return 0; + } + + return (int) ($payload['version'] ?? 0); + } + + /** + * Run supervisorctl restart in a detached process. + * + * The listener must not wait for supervisorctl. `restart all` stops every + * program on this pod, including this listener when supervisor manages it. + */ + public function scheduleSupervisorRestart(): bool + { + $delay = max(0, (int) config('plugin_manager.supervisor_restart_delay', 5)); + $launcher = 'sleep ' . $delay . '; ' . implode(' ', array_map('escapeshellarg', $this->supervisorCommand())); + $script = 'setsid nohup bash -c ' . escapeshellarg($launcher) . ' >/dev/null 2>&1 run(['bash', '-c', $script]); + } catch (ProcessTimedOutException) { + return false; + } + + return $process->successful(); + } + + /** + * @return list + */ + public function supervisorCommand(): array + { + $command = [ + config('plugin_manager.supervisorctl_bin', 'supervisorctl'), + ]; + + $configFile = config('plugin_manager.supervisorctl_config'); + if ($configFile) { + $command[] = '-c'; + $command[] = $configFile; + } + + $command[] = 'restart'; + $command[] = config('plugin_manager.supervisor_restart_target', 'all'); + + return $command; + } + + /** + * Pub/sub channel names are not prefixed by PhpRedis. Include REDIS_PREFIX + * and the tenant prefix so other instances on the shared Redis do not hear it. + */ + public function channel(): string + { + return $this->redisPrefix() . $this->tenantPrefix() . config('plugin_manager.reload_channel', 'plugin-runtime-reload'); + } + + /** + * INCR/GET receive REDIS_PREFIX from the PhpRedis client. Add only the tenant + * prefix here so two tenants in one instance do not share the version. + */ + public function versionKey(): string + { + return $this->tenantPrefix() . config('plugin_manager.reload_version_key', 'plugin-runtime-reload:version'); + } + + public function stateFile(): string + { + $path = (string) config('plugin_manager.reload_state_file', '/tmp/processmaker-plugin-reload-version'); + $tenantId = $this->tenantId(); + if ($tenantId === null) { + return $path; + } + + return $path . '-' . $tenantId; + } + + private function redisPrefix(): string + { + return (string) config('database.redis.options.prefix', ''); + } + + private function tenantPrefix(): string + { + $tenantId = $this->tenantId(); + if ($tenantId === null) { + return ''; + } + + return 'tenant_' . $tenantId . ':'; + } + + private function tenantId(): ?string + { + if (!app()->bound('currentTenant')) { + return null; + } + + $tenant = app('currentTenant'); + if (!$tenant) { + return null; + } + + $tenantId = method_exists($tenant, 'getKey') ? $tenant->getKey() : ($tenant->id ?? null); + if ($tenantId === null || $tenantId === '') { + return null; + } + + return (string) $tenantId; + } +} diff --git a/ProcessMaker/Services/ProcessBpmnReferenceService.php b/ProcessMaker/Services/ProcessBpmnReferenceService.php new file mode 100644 index 0000000000..142c007dfd --- /dev/null +++ b/ProcessMaker/Services/ProcessBpmnReferenceService.php @@ -0,0 +1,314 @@ +replaceNamedReferences( + $bpmn, + 'screen', + fn (array $names) => Screen::query() + ->whereIn('title', $names) + ->pluck('id', 'title') + ->all() + ); + + $bpmn = $this->replaceNamedReferences( + $bpmn, + 'script', + fn (array $names) => Script::query() + ->whereIn('title', $names) + ->pluck('id', 'title') + ->all() + ); + + $bpmn = $this->resolveWebEntryScreenReferences($bpmn); + + return $this->resolveNamedProcessCalls($bpmn); + } + + public function restoreNamedReferences(string $bpmn): string + { + $bpmn = $this->replaceIdReferences( + $bpmn, + 'screen', + fn (array $ids) => Screen::query() + ->whereIn('id', $ids) + ->pluck('title', 'id') + ->all() + ); + + $bpmn = $this->replaceIdReferences( + $bpmn, + 'script', + fn (array $ids) => Script::query() + ->whereIn('id', $ids) + ->pluck('title', 'id') + ->all() + ); + + $bpmn = $this->restoreWebEntryScreenReferences($bpmn); + + return $this->restoreNamedProcessCalls($bpmn); + } + + private function resolveWebEntryScreenReferences(string $bpmn): string + { + $names = []; + + $this->transformWebEntryConfigs($bpmn, function (array $config) use (&$names): array { + foreach (['screen_id', 'completed_screen_id'] as $field) { + $name = $this->screenReferenceName($config['web_entry'][$field] ?? null); + if ($name !== null) { + $names[] = $name; + } + } + + return $config; + }); + + $names = array_values(array_unique($names)); + if ($names === []) { + return $bpmn; + } + + $idsByName = Screen::query() + ->whereIn('title', $names) + ->pluck('id', 'title') + ->all(); + $missing = array_values(array_diff($names, array_keys($idsByName))); + if ($missing !== []) { + throw new MissingBpmnReferenceException( + 'Missing screen reference(s): ' . implode(', ', $missing) + ); + } + + return $this->transformWebEntryConfigs($bpmn, function (array $config) use ($idsByName): array { + foreach (['screen_id', 'completed_screen_id'] as $field) { + $name = $this->screenReferenceName($config['web_entry'][$field] ?? null); + if ($name !== null) { + $config['web_entry'][$field] = $idsByName[$name]; + } + } + + return $config; + }); + } + + private function restoreWebEntryScreenReferences(string $bpmn): string + { + $ids = []; + + $this->transformWebEntryConfigs($bpmn, function (array $config) use (&$ids): array { + foreach (['screen_id', 'completed_screen_id'] as $field) { + $id = $this->screenId($config['web_entry'][$field] ?? null); + if ($id !== null) { + $ids[] = $id; + } + } + + return $config; + }); + + $ids = array_values(array_unique($ids)); + if ($ids === []) { + return $bpmn; + } + + $titlesById = Screen::query() + ->whereIn('id', $ids) + ->pluck('title', 'id') + ->all(); + + return $this->transformWebEntryConfigs($bpmn, function (array $config) use ($titlesById): array { + foreach (['screen_id', 'completed_screen_id'] as $field) { + $id = $this->screenId($config['web_entry'][$field] ?? null); + if ($id !== null && isset($titlesById[$id])) { + $config['web_entry'][$field] = '${screen:' . $titlesById[$id] . '}'; + } + } + + return $config; + }); + } + + private function transformWebEntryConfigs(string $bpmn, callable $transform): string + { + $pattern = '/(pm:config\s*=\s*)(["\'])(.*?)\2/s'; + + return preg_replace_callback($pattern, static function (array $match) use ($transform): string { + $json = html_entity_decode($match[3], ENT_QUOTES | ENT_XML1, 'UTF-8'); + $config = json_decode($json, true); + if (!is_array($config) || !is_array($config['web_entry'] ?? null)) { + return $match[0]; + } + + $transformed = $transform($config); + if ($transformed === $config) { + return $match[0]; + } + + $encoded = htmlspecialchars( + json_encode($transformed, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE), + ENT_QUOTES | ENT_XML1, + 'UTF-8' + ); + + return $match[1] . $match[2] . $encoded . $match[2]; + }, $bpmn) ?? $bpmn; + } + + private function screenReferenceName(mixed $value): ?string + { + if (is_string($value) && preg_match('/^\$\{screen:([^}]+)\}$/', $value, $match)) { + return $match[1]; + } + + return null; + } + + private function screenId(mixed $value): ?int + { + if (is_int($value) || (is_string($value) && ctype_digit($value))) { + return (int) $value; + } + + return null; + } + + private function replaceNamedReferences( + string $bpmn, + string $type, + callable $loadIds + ): string { + $pattern = '/(pm:' . $type . 'Ref\s*=\s*)(["\'])\$\{' . $type . ':([^}]+)\}\2/'; + preg_match_all($pattern, $bpmn, $matches); + $names = array_values(array_unique($matches[3] ?? [])); + + if ($names === []) { + return $bpmn; + } + + $idsByName = $loadIds($names); + $missing = array_values(array_diff($names, array_keys($idsByName))); + if ($missing !== []) { + throw new MissingBpmnReferenceException( + 'Missing ' . $type . ' reference(s): ' . implode(', ', $missing) + ); + } + + return preg_replace_callback( + $pattern, + static fn (array $match): string => $match[1] . $match[2] + . $idsByName[$match[3]] . $match[2], + $bpmn + ); + } + + private function replaceIdReferences( + string $bpmn, + string $type, + callable $loadNames + ): string { + $pattern = '/(pm:' . $type . 'Ref\s*=\s*)(["\'])(\d+)\2/'; + preg_match_all($pattern, $bpmn, $matches); + $ids = array_values(array_unique($matches[3] ?? [])); + + if ($ids === []) { + return $bpmn; + } + + $namesById = $loadNames($ids); + + return preg_replace_callback( + $pattern, + static function (array $match) use ($namesById, $type): string { + $name = $namesById[$match[3]] ?? null; + if ($name === null) { + return $match[0]; + } + + return $match[1] . $match[2] . '${' . $type . ':' . $name . '}' . $match[2]; + }, + $bpmn + ); + } + + private function resolveNamedProcessCalls(string $bpmn): string + { + $pattern = '/(calledElement\s*=\s*)(["\'])\$\{process:([^}]+)\}\2/'; + preg_match_all($pattern, $bpmn, $matches); + $names = array_values(array_unique($matches[3] ?? [])); + if ($names === []) { + return $bpmn; + } + + $processes = Process::query()->whereIn('name', $names)->get()->keyBy('name'); + $missing = array_values(array_diff($names, $processes->keys()->all())); + if ($missing !== []) { + throw new MissingBpmnReferenceException( + 'Missing process reference(s): ' . implode(', ', $missing) + ); + } + + $calledElementByName = []; + foreach ($processes as $name => $process) { + $calledElementByName[$name] = $this->bpmnProcessXmlId((string) $process->bpmn) + . '-' . $process->id; + } + + return preg_replace_callback( + $pattern, + static fn (array $match): string => $match[1] . $match[2] + . $calledElementByName[$match[3]] . $match[2], + $bpmn + ); + } + + private function restoreNamedProcessCalls(string $bpmn): string + { + $pattern = '/(calledElement\s*=\s*)(["\'])([^"\']+)-(\d+)\2/'; + preg_match_all($pattern, $bpmn, $matches); + $ids = array_values(array_unique($matches[4] ?? [])); + if ($ids === []) { + return $bpmn; + } + + $namesById = Process::query()->whereIn('id', $ids)->pluck('name', 'id')->all(); + + return preg_replace_callback( + $pattern, + static function (array $match) use ($namesById): string { + $name = $namesById[(int) $match[4]] ?? $namesById[$match[4]] ?? null; + if ($name === null) { + return $match[0]; + } + + return $match[1] . $match[2] . '${process:' . $name . '}' . $match[2]; + }, + $bpmn + ); + } + + private function bpmnProcessXmlId(string $childBpmn): string + { + if ($childBpmn !== '' && preg_match( + '/]*\bid\s*=\s*["\']([^"\']+)["\']/', + $childBpmn, + $match + )) { + return $match[1]; + } + + return 'ProcessId'; + } +} diff --git a/ProcessMaker/Services/ScriptExecutionModeResolver.php b/ProcessMaker/Services/ScriptExecutionModeResolver.php new file mode 100644 index 0000000000..8ea025cca0 --- /dev/null +++ b/ProcessMaker/Services/ScriptExecutionModeResolver.php @@ -0,0 +1,26 @@ +allow_in_process) { + return Script::EXECUTION_MODE_BARE; + } + + $mode = $script->execution_mode ?: config('core-service-task.execution', Script::EXECUTION_MODE_BARE); + + if ( + $mode !== Script::EXECUTION_MODE_MODULES + || !config('core-service-task.modules_enabled', false) + ) { + return Script::EXECUTION_MODE_BARE; + } + + return Script::EXECUTION_MODE_MODULES; + } +} diff --git a/ProcessMaker/Services/ScriptExecutorResolver.php b/ProcessMaker/Services/ScriptExecutorResolver.php new file mode 100644 index 0000000000..c50f3fbcc7 --- /dev/null +++ b/ProcessMaker/Services/ScriptExecutorResolver.php @@ -0,0 +1,31 @@ +where('language', 'php-nayra') + ->orderBy('id') + ->first(); + if ($nayra !== null) { + return $nayra; + } + + $php = ScriptExecutor::query() + ->where('language', 'php') + ->orderBy('id') + ->first(); + if ($php !== null) { + return $php; + } + + return ScriptExecutor::query()->orderBy('id')->first(); + } +} diff --git a/ProcessMaker/Services/ScriptMicroserviceService.php b/ProcessMaker/Services/ScriptMicroserviceService.php index b3a8a114ce..87770bf16e 100644 --- a/ProcessMaker/Services/ScriptMicroserviceService.php +++ b/ProcessMaker/Services/ScriptMicroserviceService.php @@ -195,6 +195,7 @@ public function getScriptRunner(string $language, string $executorUuid, bool $cu if (Cache::has($cacheKey)) { Log::debug('Cache hit for script runner', ['cacheKey' => $cacheKey]); + return Cache::get($cacheKey); } @@ -211,7 +212,9 @@ public function getScriptRunner(string $language, string $executorUuid, bool $cu return isset($item['language'], $item['id']) && $item['language'] === $language && $item['id'] === $executorUuid; })->first(); - if (!empty($result)) Cache::put($cacheKey, $result, now()->addHour()); + if (!empty($result)) { + Cache::put($cacheKey, $result, now()->addHour()); + } return $result; } diff --git a/ProcessMaker/Support/PluginVendorAssets.php b/ProcessMaker/Support/PluginVendorAssets.php new file mode 100644 index 0000000000..10abb43abd --- /dev/null +++ b/ProcessMaker/Support/PluginVendorAssets.php @@ -0,0 +1,221 @@ +bound('currentTenant')) { + return null; + } + + $tenant = app('currentTenant'); + if (!is_object($tenant) || !isset($tenant->id)) { + return null; + } + + return (int) $tenant->id; + } + + /** + * Web package folder name (e.g. package-plg) from a plugin directory. + */ + public static function packageNameFromPluginPath(string $pluginPath, ?string $folderName = null): string + { + $folderName = $folderName ?? basename(rtrim($pluginPath, '/\\')); + $composerPath = rtrim($pluginPath, '/\\') . '/composer.json'; + + if (!is_file($composerPath)) { + return $folderName; + } + + $content = file_get_contents($composerPath); + $data = json_decode($content, true); + if (!is_array($data)) { + return $folderName; + } + + $name = $data['name'] ?? null; + if (is_string($name) && str_contains($name, '/')) { + return substr($name, strrpos($name, '/') + 1); + } + + return $folderName; + } + + public static function normalizePackageName(string $packageName): string + { + return trim(str_replace('\\', '/', $packageName), '/'); + } + + public static function globalVendorRelativePath(string $packageName): string + { + return self::VENDOR_PACKAGES_PREFIX . '/' . self::normalizePackageName($packageName); + } + + public static function tenantStorageSegment(int $tenantId): string + { + return 'storage/tenant_' . $tenantId; + } + + public static function tenantVendorRelativePath(int $tenantId, string $packageName): string + { + return self::tenantStorageSegment($tenantId) . '/' . self::globalVendorRelativePath($packageName); + } + + /** + * Absolute filesystem path to published plugin public assets for a tenant. + */ + public static function tenantPublicRoot(int $tenantId, string $packageName): string + { + return public_path(self::tenantVendorRelativePath($tenantId, $packageName)); + } + + public static function globalPublicRoot(string $packageName): string + { + return public_path(self::globalVendorRelativePath($packageName)); + } + + /** + * Path relative to public_path() where the tenant copy of mix-manifest.json lives. + */ + public static function manifestRelativeDirectory(int $tenantId, string $packageName): string + { + return self::tenantVendorRelativePath($tenantId, $packageName); + } + + public static function mixManifestFilesystemPath(int $tenantId, string $packageName): string + { + return self::tenantPublicRoot($tenantId, $packageName) . DIRECTORY_SEPARATOR . 'mix-manifest.json'; + } + + /** + * Second argument to mix(), e.g. vendor/processmaker/packages/package-plg (no leading slash). + */ + public static function mixManifestDirectoryArgument(string $packageName): string + { + return self::globalVendorRelativePath($packageName); + } + + public static function isPluginVendorManifestDirectory(string $manifestDirectory): bool + { + return self::parsePackageFromManifestDirectory($manifestDirectory) !== null; + } + + public static function parsePackageFromManifestDirectory(string $manifestDirectory): ?string + { + $manifestDirectory = trim(str_replace('\\', '/', $manifestDirectory), '/'); + $prefix = self::VENDOR_PACKAGES_PREFIX . '/'; + + if (!str_starts_with($manifestDirectory, $prefix)) { + return null; + } + + $rest = substr($manifestDirectory, strlen($prefix)); + $package = explode('/', $rest, 2)[0] ?? ''; + + return $package !== '' ? $package : null; + } + + /** + * URL path prefix for browser requests (/storage/tenant_X/vendor/...). + */ + public static function webPrefix(int $tenantId, string $packageName): string + { + return '/' . self::tenantVendorRelativePath($tenantId, $packageName); + } + + /** + * Build a public URL path for an entry from mix-manifest (value side, usually /js/file.js?id=...). + */ + public static function webPath(int $tenantId, string $packageName, string $manifestEntry): string + { + $entry = '/' . ltrim(str_replace('\\', '/', $manifestEntry), '/'); + + return self::webPrefix($tenantId, $packageName) . $entry; + } + + /** + * Global (landlord / legacy) web path for the same manifest entry. + */ + public static function globalWebPath(string $packageName, string $manifestEntry): string + { + $entry = '/' . ltrim(str_replace('\\', '/', $manifestEntry), '/'); + + return '/' . self::globalVendorRelativePath($packageName) . $entry; + } + + public static function pluginSourcePublicPath(string $pluginPath): string + { + return rtrim($pluginPath, '/\\') . '/public'; + } + + /** + * Whether mix() can resolve assets for a vendor package directory (global publish or tenant copy). + */ + public static function publishedVendorPackageIsAvailable(string $manifestDirectory): bool + { + $manifestDirectory = trim(str_replace('\\', '/', $manifestDirectory), '/'); + if ($manifestDirectory === '') { + return false; + } + + if (is_dir(public_path($manifestDirectory))) { + return true; + } + + if (!self::isEnabled()) { + return false; + } + + $packageName = self::parsePackageFromManifestDirectory($manifestDirectory); + if ($packageName === null) { + return false; + } + + $tenantId = self::currentTenantId(); + if ($tenantId === null) { + return false; + } + + return is_file(self::mixManifestFilesystemPath($tenantId, $packageName)); + } + + public static function requireTenantId(?int $tenantId): int + { + if ($tenantId !== null) { + return $tenantId; + } + + $current = self::currentTenantId(); + if ($current === null) { + throw new InvalidArgumentException('No current tenant in application context.'); + } + + return $current; + } +} diff --git a/ProcessMaker/Traits/InteractsWithRawFilter.php b/ProcessMaker/Traits/InteractsWithRawFilter.php index 15a5c5fd3b..c192534f92 100644 --- a/ProcessMaker/Traits/InteractsWithRawFilter.php +++ b/ProcessMaker/Traits/InteractsWithRawFilter.php @@ -4,6 +4,7 @@ use Illuminate\Contracts\Database\Query\Expression; use Illuminate\Support\Facades\DB; + trait InteractsWithRawFilter { private const MAX_INTERVAL = 365; diff --git a/ProcessMaker/Traits/PluginServiceProviderTrait.php b/ProcessMaker/Traits/PluginServiceProviderTrait.php index b03160e575..c816dbf29e 100644 --- a/ProcessMaker/Traits/PluginServiceProviderTrait.php +++ b/ProcessMaker/Traits/PluginServiceProviderTrait.php @@ -12,6 +12,7 @@ use ProcessMaker\Managers\LoginManager; use ProcessMaker\Managers\PackageManager; use ProcessMaker\Providers\ProcessMakerServiceProvider; +use ProcessMaker\Support\PluginVendorAssets; /** * Add functionality to control a PM plug-in @@ -97,7 +98,7 @@ protected function completePluginBoot() public function modelerStarting(ModelerStarting $event) { foreach ($this->modelerScripts as $path => $config) { - if (File::exists(public_path() . '/' . $config['script_src'])) { + if (PluginVendorAssets::publishedVendorPackageIsAvailable($config['script_src'])) { $event->manager->addScript(mix($path, $config['script_src']), $config['script_params']); } } @@ -224,7 +225,7 @@ public function removePackage($package) public function scriptBuilderStarting(ScriptBuilderStarting $event) { foreach ($this->scriptBuilderScripts as $path => $public) { - if (File::exists(public_path() . '/' . $public)) { + if (PluginVendorAssets::publishedVendorPackageIsAvailable($public)) { $event->manager->addScript(mix($path, $public)); } } diff --git a/ProcessMaker/Upgrades/Commands/UpgradeInstallCommand.php b/ProcessMaker/Upgrades/Commands/UpgradeInstallCommand.php index 1058babf98..405bdbc7b8 100755 --- a/ProcessMaker/Upgrades/Commands/UpgradeInstallCommand.php +++ b/ProcessMaker/Upgrades/Commands/UpgradeInstallCommand.php @@ -24,14 +24,14 @@ class UpgradeInstallCommand extends BaseCommand /** * The repository instance. * - * @var \Illuminate\Database\Migrations\MigrationRepositoryInterface + * @var MigrationRepositoryInterface */ protected $repository; /** * Create a new migration install command instance. * - * @param \Illuminate\Database\Migrations\MigrationRepositoryInterface $repository + * @param MigrationRepositoryInterface $repository * @return void */ public function __construct(MigrationRepositoryInterface $repository) diff --git a/ProcessMaker/Upgrades/Commands/UpgradeMakeCommand.php b/ProcessMaker/Upgrades/Commands/UpgradeMakeCommand.php index 5471586e81..45bbb7bf15 100644 --- a/ProcessMaker/Upgrades/Commands/UpgradeMakeCommand.php +++ b/ProcessMaker/Upgrades/Commands/UpgradeMakeCommand.php @@ -33,7 +33,7 @@ class UpgradeMakeCommand extends BaseCommand /** * The Composer instance. * - * @var \Illuminate\Support\Composer + * @var Composer */ protected $composer; @@ -41,7 +41,7 @@ class UpgradeMakeCommand extends BaseCommand * Create a new migration install command instance. * * @param \Illuminate\Database\Migrations\MigrationCreator $creator - * @param \Illuminate\Support\Composer $composer + * @param Composer $composer * @return void */ public function __construct(UpgradeCreator $creator, Composer $composer) diff --git a/ProcessMaker/Upgrades/Commands/UpgradeStatusCommand.php b/ProcessMaker/Upgrades/Commands/UpgradeStatusCommand.php index 121882cbed..c21f45e7ed 100644 --- a/ProcessMaker/Upgrades/Commands/UpgradeStatusCommand.php +++ b/ProcessMaker/Upgrades/Commands/UpgradeStatusCommand.php @@ -64,7 +64,7 @@ public function handle() * * @param array $ran * @param array $batches - * @return \Illuminate\Support\Collection + * @return Collection */ protected function getStatusFor(array $ran, array $batches) { diff --git a/ProcessMaker/Upgrades/UpgradeCreator.php b/ProcessMaker/Upgrades/UpgradeCreator.php index bad23a27b2..076e340938 100644 --- a/ProcessMaker/Upgrades/UpgradeCreator.php +++ b/ProcessMaker/Upgrades/UpgradeCreator.php @@ -17,7 +17,7 @@ class UpgradeCreator extends MigrationCreator * * @return string * - * @throws \ProcessMaker\Exception\InvalidSemanticVersion + * @throws InvalidSemanticVersion */ public function createUpgrade($name, $path) { diff --git a/ai-docs/processmaker/requirements/components/plugin-manager/plugin-manager.md b/ai-docs/processmaker/requirements/components/plugin-manager/plugin-manager.md new file mode 100644 index 0000000000..83f5bbfe27 --- /dev/null +++ b/ai-docs/processmaker/requirements/components/plugin-manager/plugin-manager.md @@ -0,0 +1,31 @@ +# Plugin manager + +Lifecycle management for ProcessMaker plugins stored under the tenant plugins directory. + +## Purpose + +Allow authorized operators to install plugins from a Git repository URL or zip archive, uninstall them, enable/disable them, and observe progress without blocking the HTTP request. + +## Kind and reuse + +- `kind`: component +- Configurable via `config` in `requirements.yaml` (sources, progress channel, queue settings) +- Admin UI screens compose this component through `dependsOn` + +## Actors + +- Operator with `manage-plugins` permission +- Queue worker (Horizon) executing plugin jobs +- Runtime refresh after plugin code changes (route cache, worker restart) + +## Related assets + +- `ProcessMaker/Managers/PluginManager.php` +- `ProcessMaker/Jobs/InstallPlugin.php`, `UninstallPlugin.php`, `TogglePlugin.php` +- `ProcessMaker/Http/Controllers/Api/PluginController.php` +- `ProcessMaker/Events/PluginLog.php` +- `resources/js/admin/plugins/` + +## Configuration + +Consumers configure allowed install sources (`url`, `zip`), optional Git `branch`/`tag` (mutually exclusive), progress broadcast target (authenticated user channel), and job timeout / overlap policy for concurrent plugin operations. diff --git a/ai-docs/processmaker/requirements/components/plugin-manager/requirements.yaml b/ai-docs/processmaker/requirements/components/plugin-manager/requirements.yaml new file mode 100644 index 0000000000..c8b8290e34 --- /dev/null +++ b/ai-docs/processmaker/requirements/components/plugin-manager/requirements.yaml @@ -0,0 +1,243 @@ +specId: plugin-manager +kind: component +title: Plugin manager +description: >- + Install, uninstall, and toggle ProcessMaker plugins asynchronously with + progress events for the requesting operator. +config: + sources: + - url + - zip + gitRefOptions: + - branch + - tag + progressEvent: PluginLog + queue: + timeoutSeconds: 600 + withoutOverlapping: true + permission: manage-plugins +dependsOn: [] +requirements: + - id: FR-001 + requirementType: Functional + title: Queue plugin install from repository URL + statement: >- + When an authorized operator requests install from a repository URL, the + component must enqueue the install work and return immediately without + waiting for clone or dependency installation to finish. + priority: Critical + status: Draft + tags: [plugins, install, queue] + acceptanceCriteria: + - given: An operator with manage-plugins permission submits a valid repository URL + when: The install request is accepted + then: An install job is queued and the HTTP response indicates the work was queued + - given: An install job is queued with an optional branch or tag + when: The worker runs the job + then: The plugin is cloned or updated and the requested git reference is checked out before dependency install + traceability: + testCases: [] + + - id: FR-002 + requirementType: Functional + title: Queue plugin install from zip archive + statement: >- + When an authorized operator uploads a zip archive, the component must + persist the archive for the worker, enqueue install, and return without + blocking on extraction or dependency installation. + priority: High + status: Draft + tags: [plugins, install, zip, queue] + acceptanceCriteria: + - given: An operator uploads a valid plugin zip file + when: The install request is accepted + then: The zip is stored for the worker, an install job is queued, and the response indicates queued status + - given: A queued zip install job runs + when: Extraction and validation succeed + then: The plugin is copied into the plugins directory and dependencies are installed + traceability: + testCases: [] + + - id: FR-003 + requirementType: Functional + title: Queue plugin uninstall + statement: >- + When an authorized operator requests uninstall by plugin name, the + component must enqueue uninstallation and return without waiting for the + uninstall command or directory removal to finish. + priority: High + status: Draft + tags: [plugins, uninstall, queue] + acceptanceCriteria: + - given: An installed plugin exists by name + when: The operator requests uninstall + then: An uninstall job is queued and the response indicates queued status + - given: An uninstall job runs for a known plugin + when: The job completes successfully + then: The plugin directory is removed and a done progress event is emitted + traceability: + testCases: [] + + - id: FR-004 + requirementType: Functional + title: Queue plugin enable and disable toggle + statement: >- + When an authorized operator toggles a plugin, the component must enqueue + the enable or disable rename and return without waiting for runtime refresh. + priority: High + status: Draft + tags: [plugins, toggle, queue] + acceptanceCriteria: + - given: A plugin directory exists in enabled or disabled form + when: The operator requests toggle + then: A toggle job is queued and the response indicates queued status + - given: A toggle job disables an enabled plugin + when: The job completes + then: The plugin folder is renamed with a leading underscore and a done progress event is emitted + traceability: + testCases: [] + + - id: FR-005 + requirementType: Functional + title: Broadcast install progress to the requesting operator + statement: >- + During queued plugin operations the component must emit progress events + to the requesting operator so the client can show running, done, and error + states without polling the HTTP install endpoint. + priority: Critical + status: Draft + tags: [plugins, progress, broadcast] + acceptanceCriteria: + - given: A plugin job is running for a known user id + when: The manager logs a running step + then: A PluginLog progress event is broadcast on that user's private channel + - given: A plugin job fails with an exception + when: The failure is handled + then: An error progress event with a failure message is broadcast to the requesting user + traceability: + testCases: [] + + - id: FR-006 + requirementType: Functional + title: Validate plugin composer structure before install completes + statement: >- + The component must reject plugins whose composer.json is missing, invalid, + or whose PSR-4 namespace does not start with an allowed ProcessMaker plugin + or package prefix. + priority: High + status: Draft + tags: [plugins, validation] + acceptanceCriteria: + - given: A cloned or extracted plugin lacks a valid composer.json PSR-4 prefix + when: Validation runs during install + then: Install fails with an error progress event and the plugin is not marked installed successfully + - given: A plugin composer.json uses an allowed ProcessMaker Plugins or Package namespace + when: Validation runs + then: Validation succeeds and dependency installation may proceed + traceability: + testCases: [] + + - id: FR-007 + requirementType: Functional + title: Refresh runtime after plugin code changes + statement: >- + After a successful install, uninstall, or toggle, the component must + rebuild the route cache for the current tenant context and signal queue + workers to restart so new plugin code is loaded. + priority: High + status: Draft + tags: [plugins, runtime, cache] + acceptanceCriteria: + - given: A plugin install job finishes successfully + when: Runtime refresh runs + then: Route cache is rebuilt and queue workers are signaled to terminate after the current job + - given: Multitenancy is enabled and a tenant is active + when: Artisan subprocesses run for refresh + then: The tenant id is passed in the subprocess environment + traceability: + testCases: [] + + - id: FR-008 + requirementType: Functional + title: List installed plugins including disabled ones + statement: >- + The component must list plugins from the plugins directory, including + disabled folders, with name, description, version, reference, and enabled + state derived from folder naming. + priority: Medium + status: Draft + tags: [plugins, list] + acceptanceCriteria: + - given: Enabled and underscore-prefixed disabled plugin folders exist + when: The list operation runs + then: Both appear with enabled state reflecting the underscore prefix + - given: A folder under plugins is not a valid plugin + when: The list operation runs + then: The invalid folder is skipped without failing the whole list + traceability: + testCases: [] + + - id: NFR-001 + requirementType: NonFunctional + title: Install request returns within API timeout budget + statement: >- + The HTTP install, uninstall, and toggle endpoints must acknowledge the + queued operation quickly so clients are not blocked by git clone or + composer install duration. + priority: Critical + status: Draft + category: Performance + tags: [plugins, latency, queue] + metrics: + - name: queue_ack_latency + operator: "<=" + value: 5 + unit: seconds + verificationMethod: Test + acceptanceCriteria: + - given: A valid install request is submitted while the queue is available + when: The endpoint responds + then: The response is returned in at most five seconds without waiting for composer install + + - id: NFR-002 + requirementType: NonFunctional + title: Plugin jobs allow long-running dependency install + statement: >- + Queued plugin jobs must allow sufficient wall-clock time for git and + composer operations, and subprocess timeouts must not be shorter than the + job budget for those steps. + priority: Critical + status: Draft + category: Reliability + tags: [plugins, timeout, composer] + metrics: + - name: job_timeout + operator: ">=" + value: 600 + unit: seconds + verificationMethod: Inspection + acceptanceCriteria: + - given: A plugin install job is configured + when: Job timeout settings are inspected + then: The job timeout is at least 600 seconds and composer subprocesses are not capped at the default 60-second process timeout + + - id: NFR-003 + requirementType: NonFunctional + title: Serialize concurrent plugin mutations + statement: >- + Concurrent install, uninstall, or toggle jobs must not overlap mutating + the plugins directory for the same deployment context. + priority: High + status: Draft + category: Reliability + tags: [plugins, concurrency] + metrics: + - name: overlapping_plugin_mutations + operator: "=" + value: 0 + unit: count + verificationMethod: Test + acceptanceCriteria: + - given: A plugin install job is already running + when: Another plugin mutation job is dispatched + then: The second job waits or is deferred until the overlapping lock is released diff --git a/ai-docs/processmaker/requirements/screens/admin-plugins/admin-plugins.md b/ai-docs/processmaker/requirements/screens/admin-plugins/admin-plugins.md new file mode 100644 index 0000000000..d800f1475c --- /dev/null +++ b/ai-docs/processmaker/requirements/screens/admin-plugins/admin-plugins.md @@ -0,0 +1,26 @@ +# Admin plugins screen + +Administrative screen for listing and managing ProcessMaker plugins. + +## Purpose + +Give operators with `manage-plugins` a UI to list plugins, start install from a repository URL (branch or tag), uninstall, and toggle enable/disable, while showing live install output. + +## Kind and reuse + +- `kind`: screen +- Depends on `plugin-manager` for lifecycle behavior; does not redefine install/uninstall FRs +- Progress UI listens to `PluginLog` on the authenticated user's private channel + +## Related assets + +- `resources/views` admin plugins entry +- `resources/js/admin/plugins/Plugins.vue` +- `resources/js/admin/plugins/components/PluginOutput.vue` +- Web route `plugins.index`, API routes under `api/1.0/plugins*` + +## Assumptions + +- Horizon (or equivalent queue workers) is running so queued plugin jobs execute. +- Echo / broadcasting is available so `PluginLog` updates appear in `PluginOutput`. +- The screen does not wait for clone/composer to finish on the HTTP response; it stays open until a done or error progress event. diff --git a/ai-docs/processmaker/requirements/screens/admin-plugins/requirements.yaml b/ai-docs/processmaker/requirements/screens/admin-plugins/requirements.yaml new file mode 100644 index 0000000000..d4ae64df70 --- /dev/null +++ b/ai-docs/processmaker/requirements/screens/admin-plugins/requirements.yaml @@ -0,0 +1,129 @@ +specId: admin-plugins +kind: screen +title: Admin plugins screen +description: >- + Admin UI to list plugins and start queued install, uninstall, and toggle + operations with live PluginLog output. +config: + permission: manage-plugins + installFields: + - url + - branch + - tag + progressComponent: PluginOutput +dependsOn: + - plugin-manager +requirements: + - id: FR-001 + requirementType: Functional + title: Restrict screen to manage-plugins operators + statement: >- + The screen must only be available to authenticated operators who have the + manage-plugins permission. + priority: Critical + status: Draft + tags: [plugins, authz] + dependencies: + - plugin-manager#FR-001 + acceptanceCriteria: + - given: An authenticated operator without manage-plugins permission + when: The operator opens the admin plugins route + then: Access is denied + - given: An authenticated operator with manage-plugins permission + when: The operator opens the admin plugins route + then: The plugins list screen is shown + traceability: + testCases: [] + + - id: FR-002 + requirementType: Functional + title: List plugins in a searchable table + statement: >- + The screen must show installed plugins from the plugin-manager list, + including enabled state, and support client search and pagination. + priority: High + status: Draft + tags: [plugins, list, ui] + dependencies: + - plugin-manager#FR-008 + acceptanceCriteria: + - given: The plugin-manager list returns enabled and disabled plugins + when: The screen finishes loading + then: Each plugin appears with name and enabled state + - given: The operator enters a search filter + when: The filter is applied + then: The table shows only matching plugin rows + traceability: + testCases: [] + + - id: FR-003 + requirementType: Functional + title: Start queued install and keep modal open for progress + statement: >- + When the operator submits the install form, the screen must call the + install API, treat a queued acknowledgment as success for dispatch, keep + the modal open, and rely on PluginLog events for completion instead of + reloading on the HTTP response alone. + priority: Critical + status: Draft + tags: [plugins, install, ui, progress] + dependencies: + - plugin-manager#FR-001 + - plugin-manager#FR-005 + acceptanceCriteria: + - given: The operator fills a valid repository URL and submits install + when: The API returns a queued acknowledgment + then: The modal stays open and PluginOutput shows progress from PluginLog events + - given: A PluginLog done event arrives for a successful install + when: The screen handles the event + then: The operator is informed of success and may close or refresh the list + - given: A PluginLog error event arrives during install + when: The screen handles the event + then: The error is visible in the output panel and the list is not treated as successfully updated + traceability: + testCases: [] + + - id: FR-004 + requirementType: Functional + title: Trigger queued uninstall and toggle from row actions + statement: >- + Row actions must call uninstall and toggle APIs that enqueue work, then + refresh or reload after a successful completion signal rather than + assuming the HTTP acknowledgment finished the mutation. + priority: High + status: Draft + tags: [plugins, uninstall, toggle, ui] + dependencies: + - plugin-manager#FR-003 + - plugin-manager#FR-004 + acceptanceCriteria: + - given: The operator confirms delete on a plugin row + when: The uninstall API returns queued acknowledgment + then: The UI waits for completion feedback before treating the plugin as removed + - given: The operator clicks toggle on a plugin row + when: The toggle API returns queued acknowledgment + then: The UI waits for completion feedback before showing the new enabled state as final + traceability: + testCases: [] + + - id: NFR-001 + requirementType: NonFunctional + title: Install UI must not use the short default API timeout as completion + statement: >- + The install flow must not interpret the default short API client timeout + as install failure when the server has already queued the job; completion + is determined by PluginLog events. + priority: Critical + status: Draft + category: Usability + tags: [plugins, timeout, ui] + metrics: + - name: install_completion_signal + operator: "=" + value: PluginLog + unit: event + verificationMethod: Demonstration + acceptanceCriteria: + - given: Composer install takes longer than the default API client timeout + when: The operator has submitted install and the job was queued + then: The UI continues showing progress from PluginLog instead of marking install failed solely due to the HTTP client timeout diff --git a/config/app.php b/config/app.php index 7f0909b7db..0830457eb8 100644 --- a/config/app.php +++ b/config/app.php @@ -201,6 +201,7 @@ * ProcessMaker Service Providers */ ProcessMaker\Providers\ProcessMakerServiceProvider::class, + ProcessMaker\Providers\PluginServiceProvider::class, ProcessMaker\Providers\RecommendationsServiceProvider::class, ProcessMaker\Providers\AuthServiceProvider::class, ProcessMaker\Providers\EventServiceProvider::class, diff --git a/config/core-service-task.php b/config/core-service-task.php new file mode 100644 index 0000000000..326ffbe984 --- /dev/null +++ b/config/core-service-task.php @@ -0,0 +1,43 @@ + env('CORE_SERVICE_TASK_ENABLED', false), + + 'default_timeout' => (int) env('CORE_SERVICE_TASK_DEFAULT_TIMEOUT', 60), + + 'max_timeout' => (int) env('CORE_SERVICE_TASK_MAX_TIMEOUT', 300), + + 'memory_limit' => env('CORE_SERVICE_TASK_MEMORY_LIMIT', '256M'), + + 'php_binary' => env('CORE_SERVICE_TASK_PHP_BINARY', PHP_BINARY), + + /* + |-------------------------------------------------------------------------- + | Execution mode + |-------------------------------------------------------------------------- + | + | modules: run in the Laravel worker with registered ScriptRuntime modules + | (packages call ScriptRuntime::registerModule(...)). + | bare: legacy PHP subprocess without Laravel/autoload (no modules). + | + */ + 'execution' => env('CORE_SERVICE_TASK_EXECUTION', 'bare'), + + // Modules must be enabled globally before a script can select them. + 'modules_enabled' => env('CORE_SERVICE_TASK_MODULES_ENABLED', false), + + /* + | Optional dedicated queue name for RunServiceTask nodes that set + | config.queue. Defaults to bpmn when not overridden on the node. + */ + 'queue' => env('CORE_SERVICE_TASK_QUEUE', 'bpmn'), +]; diff --git a/config/kafka.php b/config/kafka.php index 5a1dfc5d86..ce97f99724 100644 --- a/config/kafka.php +++ b/config/kafka.php @@ -49,7 +49,7 @@ | Repository for batching messages together | Implement BatchRepositoryInterface to save batches in different storage */ - 'batch_repository' => env('KAFKA_BATCH_REPOSITORY', \Junges\Kafka\BatchRepositories\InMemoryBatchRepository::class), + 'batch_repository' => env('KAFKA_BATCH_REPOSITORY', Junges\Kafka\BatchRepositories\InMemoryBatchRepository::class), /* | The sleep time in milliseconds that will be used when retrying flush diff --git a/config/plugin_manager.php b/config/plugin_manager.php new file mode 100644 index 0000000000..8bb16e1488 --- /dev/null +++ b/config/plugin_manager.php @@ -0,0 +1,33 @@ + env('PLUGIN_SUPERVISORCTL_BIN', 'supervisorctl'), + + /* + | Optional supervisord config (-c). Leave empty on macOS after running + | dev/supervisor/link-homebrew-default.sh so `supervisorctl restart all` works. + | On Linux servers, set to e.g. /etc/supervisord.conf. + */ + 'supervisorctl_config' => env('PLUGIN_SUPERVISORCTL_CONFIG'), + + /* + | supervisorctl restart target: "all" or a program name (e.g. processmaker-horizon). + */ + 'supervisor_restart_target' => env('PLUGIN_SUPERVISOR_RESTART_TARGET', 'all'), + + /* + | Allow install() to treat repo argument as a local directory (CLI operator use). + | Keep false in production; API install always requires https URLs regardless. + */ + 'allow_local_install_path' => env('PLUGIN_INSTALL_ALLOW_LOCAL_PATH', false), + + /* + | When MULTITENANCY is true, later phases publish plugin public assets under + | public/storage/tenant_{id}/vendor/... (Phase 0 only defines path helpers). + */ + 'tenant_plugin_assets_enabled' => env('PLUGIN_TENANT_ASSETS_ENABLED', true), +]; diff --git a/config/rabbitmq.php b/config/rabbitmq.php index 71fcaa7e0c..d71947d4b0 100644 --- a/config/rabbitmq.php +++ b/config/rabbitmq.php @@ -48,7 +48,7 @@ * Read more about possible values at https://www.rabbitmq.com/tutorials/amqp-concepts.html */ - 'type' => env('RABBITMQ_EXCHANGE_TYPE', \PhpAmqpLib\Exchange\AMQPExchangeType::DIRECT), + 'type' => env('RABBITMQ_EXCHANGE_TYPE', PhpAmqpLib\Exchange\AMQPExchangeType::DIRECT), 'passive' => env('RABBITMQ_EXCHANGE_PASSIVE', false), 'durable' => env('RABBITMQ_EXCHANGE_DURABLE', true), 'auto_delete' => env('RABBITMQ_EXCHANGE_AUTODELETE', false), diff --git a/config/samlidp.php b/config/samlidp.php index 2373f0697a..f4280682dd 100644 --- a/config/samlidp.php +++ b/config/samlidp.php @@ -34,7 +34,7 @@ // Make sure messages are signed 'messages_signed' => true, // Defind what digital algorithm you want to use - 'digest_algorithm' => \RobRichards\XMLSecLibs\XMLSecurityDSig::SHA1, + 'digest_algorithm' => RobRichards\XMLSecLibs\XMLSecurityDSig::SHA1, // list of all service providers 'sp' => [ // Base64 encoded ACS URL diff --git a/config/trustedproxy.php b/config/trustedproxy.php index 0cb1d72bf8..ef299f01d3 100644 --- a/config/trustedproxy.php +++ b/config/trustedproxy.php @@ -41,5 +41,5 @@ * * @link https://symfony.com/doc/current/deployment/proxies.html */ - 'headers' => env('PROXIES_AWS', false) ? Illuminate\Http\Request::HEADER_X_FORWARDED_AWS_ELB : Illuminate\Http\Request::HEADER_X_FORWARDED_FOR | Request::HEADER_X_FORWARDED_HOST | Request::HEADER_X_FORWARDED_PORT | Request::HEADER_X_FORWARDED_PROTO | Request::HEADER_X_FORWARDED_AWS_ELB, + 'headers' => env('PROXIES_AWS', false) ? Request::HEADER_X_FORWARDED_AWS_ELB : Request::HEADER_X_FORWARDED_FOR | Request::HEADER_X_FORWARDED_HOST | Request::HEADER_X_FORWARDED_PORT | Request::HEADER_X_FORWARDED_PROTO | Request::HEADER_X_FORWARDED_AWS_ELB, ]; diff --git a/database/factories/ProcessMaker/Models/EncryptedDataFactory.php b/database/factories/ProcessMaker/Models/EncryptedDataFactory.php index c1943e4bf5..a8515a4657 100644 --- a/database/factories/ProcessMaker/Models/EncryptedDataFactory.php +++ b/database/factories/ProcessMaker/Models/EncryptedDataFactory.php @@ -21,6 +21,7 @@ public function definition() $driver = config('app.encrypted_data.driver'); $cipherText = EncryptedData::driver($driver)->encryptText($this->faker->sentence(3)); + return [ 'field_name' => $this->faker->word(), 'iv' => base64_encode(EncryptedData::driver($driver)->getIv()), diff --git a/database/factories/ProcessMaker/Models/ProcessAbeRequestTokenFactory.php b/database/factories/ProcessMaker/Models/ProcessAbeRequestTokenFactory.php index 2e31b4c71f..f3bcd96431 100644 --- a/database/factories/ProcessMaker/Models/ProcessAbeRequestTokenFactory.php +++ b/database/factories/ProcessMaker/Models/ProcessAbeRequestTokenFactory.php @@ -27,7 +27,7 @@ public function definition() ]); $processRequestToken = ProcessRequestToken::factory()->create([ 'process_id' => $process->getKey(), - 'process_request_id' => $processRequest->getKey() + 'process_request_id' => $processRequest->getKey(), ]); return [ @@ -35,7 +35,7 @@ public function definition() 'process_request_token_id' => $processRequestToken->getKey(), 'completed_screen_id' => $screen->getKey(), 'is_answered' => 0, - 'require_login' => 0 + 'require_login' => 0, ]; } } diff --git a/database/factories/ProcessMaker/Models/RecommendationFactory.php b/database/factories/ProcessMaker/Models/RecommendationFactory.php index 37507d3794..24e06b80cd 100644 --- a/database/factories/ProcessMaker/Models/RecommendationFactory.php +++ b/database/factories/ProcessMaker/Models/RecommendationFactory.php @@ -2,8 +2,8 @@ namespace Database\Factories\ProcessMaker\Models; -use Illuminate\Support\Str; use Illuminate\Database\Eloquent\Factories\Factory; +use Illuminate\Support\Str; /** * @extends \Illuminate\Database\Eloquent\Factories\Factory<\ProcessMaker\Models\Recommendation> diff --git a/database/factories/ProcessMaker/Models/SettingsMenusFactory.php b/database/factories/ProcessMaker/Models/SettingsMenusFactory.php index a1fcbd81c9..c0ff2d58f1 100644 --- a/database/factories/ProcessMaker/Models/SettingsMenusFactory.php +++ b/database/factories/ProcessMaker/Models/SettingsMenusFactory.php @@ -22,10 +22,11 @@ public function definition() 'Log-In & Auth', 'User Settings', ]); + return [ 'menu_group' => $menus, 'menu_group_order' => 1, - 'ui' => json_encode(["icon" => "envelope-open-text"]), + 'ui' => json_encode(['icon' => 'envelope-open-text']), ]; } } diff --git a/database/factories/ProcessMaker/Models/TaskDraftFactory.php b/database/factories/ProcessMaker/Models/TaskDraftFactory.php index bed5e26e10..ee342dc89e 100644 --- a/database/factories/ProcessMaker/Models/TaskDraftFactory.php +++ b/database/factories/ProcessMaker/Models/TaskDraftFactory.php @@ -2,12 +2,11 @@ namespace Database\Factories\ProcessMaker\Models; -use ProcessMaker\Models\TaskDraft; use Illuminate\Database\Eloquent\Factories\Factory; +use ProcessMaker\Models\TaskDraft; class TaskDraftFactory extends Factory { - protected $model = TaskDraft::class; /** diff --git a/database/migrations/2019_07_03_174209_update_bpmn_column_to_mediumtext.php b/database/migrations/2019_07_03_174209_update_bpmn_column_to_mediumtext.php index 49547722af..7a7d16272a 100644 --- a/database/migrations/2019_07_03_174209_update_bpmn_column_to_mediumtext.php +++ b/database/migrations/2019_07_03_174209_update_bpmn_column_to_mediumtext.php @@ -8,7 +8,6 @@ return new class extends Migration { public function __construct() { - } /** diff --git a/database/migrations/2022_12_15_014415_add_is_self_service_index_to_process_request_tokens_table.php b/database/migrations/2022_12_15_014415_add_is_self_service_index_to_process_request_tokens_table.php index 2a44237d21..5b095c842e 100644 --- a/database/migrations/2022_12_15_014415_add_is_self_service_index_to_process_request_tokens_table.php +++ b/database/migrations/2022_12_15_014415_add_is_self_service_index_to_process_request_tokens_table.php @@ -20,7 +20,6 @@ public function __construct() { - } public function up() diff --git a/database/migrations/2024_01_17_183702_add_2fa_field_to_groups_table.php b/database/migrations/2024_01_17_183702_add_2fa_field_to_groups_table.php index f19ab100a9..f49cb5ddb3 100644 --- a/database/migrations/2024_01_17_183702_add_2fa_field_to_groups_table.php +++ b/database/migrations/2024_01_17_183702_add_2fa_field_to_groups_table.php @@ -4,8 +4,7 @@ use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; -return new class extends Migration -{ +return new class extends Migration { /** * Run the migrations. */ diff --git a/database/migrations/2024_02_14_140435_add_is_priority_to_process_request_tokens_table.php b/database/migrations/2024_02_14_140435_add_is_priority_to_process_request_tokens_table.php index 0da26463f0..b46670bd6b 100644 --- a/database/migrations/2024_02_14_140435_add_is_priority_to_process_request_tokens_table.php +++ b/database/migrations/2024_02_14_140435_add_is_priority_to_process_request_tokens_table.php @@ -4,8 +4,7 @@ use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; -return new class extends Migration -{ +return new class extends Migration { /** * Run the migrations. */ diff --git a/database/migrations/2024_02_26_182828_create_settings_menus_table.php b/database/migrations/2024_02_26_182828_create_settings_menus_table.php index 52da8e67f1..02254ee117 100644 --- a/database/migrations/2024_02_26_182828_create_settings_menus_table.php +++ b/database/migrations/2024_02_26_182828_create_settings_menus_table.php @@ -5,8 +5,7 @@ use Illuminate\Support\Facades\Schema; use ProcessMaker\Models\SettingsMenus; -return new class extends Migration -{ +return new class extends Migration { /** * Run the migrations. * diff --git a/database/migrations/2024_02_27_182828_add_group_id_column_to_settings_table.php b/database/migrations/2024_02_27_182828_add_group_id_column_to_settings_table.php index 1b60071c86..c11e082728 100644 --- a/database/migrations/2024_02_27_182828_add_group_id_column_to_settings_table.php +++ b/database/migrations/2024_02_27_182828_add_group_id_column_to_settings_table.php @@ -4,8 +4,7 @@ use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; -return new class extends Migration -{ +return new class extends Migration { /** * Run the migrations. */ diff --git a/database/migrations/2024_03_06_213416_create_alternatives_table.php b/database/migrations/2024_03_06_213416_create_alternatives_table.php index ea54f147fa..9f42f340d8 100644 --- a/database/migrations/2024_03_06_213416_create_alternatives_table.php +++ b/database/migrations/2024_03_06_213416_create_alternatives_table.php @@ -4,8 +4,7 @@ use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; -return new class extends Migration -{ +return new class extends Migration { /** * Run the migrations. */ diff --git a/database/migrations/2024_03_28_164852_create_embed_table.php b/database/migrations/2024_03_28_164852_create_embed_table.php index 68a5a66231..a257b5f5ba 100644 --- a/database/migrations/2024_03_28_164852_create_embed_table.php +++ b/database/migrations/2024_03_28_164852_create_embed_table.php @@ -4,8 +4,7 @@ use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; -return new class extends Migration -{ +return new class extends Migration { /** * Run the migrations. */ diff --git a/database/migrations/2024_04_01_161132_create_process_launchpad.php b/database/migrations/2024_04_01_161132_create_process_launchpad.php index 5d56b758bd..5fea7668e3 100644 --- a/database/migrations/2024_04_01_161132_create_process_launchpad.php +++ b/database/migrations/2024_04_01_161132_create_process_launchpad.php @@ -4,8 +4,7 @@ use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; -return new class extends Migration -{ +return new class extends Migration { /** * Run the migrations. */ diff --git a/database/migrations/2024_04_26_183624_add_index_to_case_number_column_in_process_requests_table.php b/database/migrations/2024_04_26_183624_add_index_to_case_number_column_in_process_requests_table.php index e195033835..64766267f6 100644 --- a/database/migrations/2024_04_26_183624_add_index_to_case_number_column_in_process_requests_table.php +++ b/database/migrations/2024_04_26_183624_add_index_to_case_number_column_in_process_requests_table.php @@ -4,8 +4,7 @@ use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; -return new class extends Migration -{ +return new class extends Migration { /** * Run the migrations. */ diff --git a/database/migrations/2024_05_29_171650_create_recommendations_table.php b/database/migrations/2024_05_29_171650_create_recommendations_table.php index 7b23e9ab47..109e9fa72a 100644 --- a/database/migrations/2024_05_29_171650_create_recommendations_table.php +++ b/database/migrations/2024_05_29_171650_create_recommendations_table.php @@ -4,8 +4,7 @@ use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; -return new class extends Migration -{ +return new class extends Migration { /** * Run the migrations. */ diff --git a/database/migrations/2024_05_29_171659_create_recommendation_users_table.php b/database/migrations/2024_05_29_171659_create_recommendation_users_table.php index dc0555f31f..dfd0730cff 100644 --- a/database/migrations/2024_05_29_171659_create_recommendation_users_table.php +++ b/database/migrations/2024_05_29_171659_create_recommendation_users_table.php @@ -4,8 +4,7 @@ use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; -return new class extends Migration -{ +return new class extends Migration { /** * Run the migrations. */ diff --git a/database/migrations/2024_06_14_143814_add_index_to_updated_at_column_for_process_request_tokens_table.php b/database/migrations/2024_06_14_143814_add_index_to_updated_at_column_for_process_request_tokens_table.php index e96815257f..8eeb9d8483 100644 --- a/database/migrations/2024_06_14_143814_add_index_to_updated_at_column_for_process_request_tokens_table.php +++ b/database/migrations/2024_06_14_143814_add_index_to_updated_at_column_for_process_request_tokens_table.php @@ -4,8 +4,7 @@ use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; -return new class extends Migration -{ +return new class extends Migration { /** * Run the migrations. */ diff --git a/database/migrations/2024_06_18_191149_create_process_slideshow.php b/database/migrations/2024_06_18_191149_create_process_slideshow.php index 081ed3f963..6a53c9f184 100644 --- a/database/migrations/2024_06_18_191149_create_process_slideshow.php +++ b/database/migrations/2024_06_18_191149_create_process_slideshow.php @@ -4,8 +4,7 @@ use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; -return new class extends Migration -{ +return new class extends Migration { /** * Run the migrations. */ diff --git a/database/migrations/2024_07_02_122418_add_process_abe_request_tokens_table.php b/database/migrations/2024_07_02_122418_add_process_abe_request_tokens_table.php index 909ee3ea26..2e4b4b2de3 100644 --- a/database/migrations/2024_07_02_122418_add_process_abe_request_tokens_table.php +++ b/database/migrations/2024_07_02_122418_add_process_abe_request_tokens_table.php @@ -4,8 +4,7 @@ use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; -return new class extends Migration -{ +return new class extends Migration { /** * Run the migrations. */ diff --git a/database/migrations/2024_07_30_120225_modify_watchers_column_in_screens_table.php b/database/migrations/2024_07_30_120225_modify_watchers_column_in_screens_table.php index 9d7d9046a8..b3fa159d4f 100644 --- a/database/migrations/2024_07_30_120225_modify_watchers_column_in_screens_table.php +++ b/database/migrations/2024_07_30_120225_modify_watchers_column_in_screens_table.php @@ -4,8 +4,7 @@ use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; -return new class extends Migration -{ +return new class extends Migration { /** * Run the migrations. */ diff --git a/database/migrations/2024_07_30_203849_update_code_in_scripts_table.php b/database/migrations/2024_07_30_203849_update_code_in_scripts_table.php index e8b4103951..ad2df169bf 100644 --- a/database/migrations/2024_07_30_203849_update_code_in_scripts_table.php +++ b/database/migrations/2024_07_30_203849_update_code_in_scripts_table.php @@ -4,8 +4,7 @@ use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; -return new class extends Migration -{ +return new class extends Migration { /** * Run the migrations. */ @@ -26,4 +25,3 @@ public function down(): void }); } }; - diff --git a/database/migrations/2024_07_30_203903_update_code_in_script_versions_table.php b/database/migrations/2024_07_30_203903_update_code_in_script_versions_table.php index bada803947..b87eb929e5 100644 --- a/database/migrations/2024_07_30_203903_update_code_in_script_versions_table.php +++ b/database/migrations/2024_07_30_203903_update_code_in_script_versions_table.php @@ -4,8 +4,7 @@ use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; -return new class extends Migration -{ +return new class extends Migration { /** * Run the migrations. */ diff --git a/database/migrations/2024_08_26_190935_create_encrypted_data.php b/database/migrations/2024_08_26_190935_create_encrypted_data.php index f64e4a369b..9a670da2b0 100644 --- a/database/migrations/2024_08_26_190935_create_encrypted_data.php +++ b/database/migrations/2024_08_26_190935_create_encrypted_data.php @@ -4,8 +4,7 @@ use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; -return new class extends Migration -{ +return new class extends Migration { /** * Run the migrations. */ diff --git a/database/migrations/2024_09_12_172734_create_cases_participated_table.php b/database/migrations/2024_09_12_172734_create_cases_participated_table.php index 2d0eef7dc3..74d12af280 100644 --- a/database/migrations/2024_09_12_172734_create_cases_participated_table.php +++ b/database/migrations/2024_09_12_172734_create_cases_participated_table.php @@ -4,8 +4,7 @@ use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; -return new class extends Migration -{ +return new class extends Migration { /** * Run the migrations. */ diff --git a/database/migrations/2026_01_12_000000_add_claim_fields_to_scheduled_tasks_table.php b/database/migrations/2026_01_12_000000_add_claim_fields_to_scheduled_tasks_table.php index 6627e4b657..3f3a2abff2 100644 --- a/database/migrations/2026_01_12_000000_add_claim_fields_to_scheduled_tasks_table.php +++ b/database/migrations/2026_01_12_000000_add_claim_fields_to_scheduled_tasks_table.php @@ -4,8 +4,7 @@ use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; -return new class extends Migration -{ +return new class extends Migration { /** * Run the migrations. * @@ -16,7 +15,7 @@ public function up() Schema::table('scheduled_tasks', function (Blueprint $table) { $table->string('claimed_by', 36)->nullable()->after('configuration'); $table->dateTime('claimed_at')->nullable()->after('claimed_by'); - + // Index for faster queries when claiming tasks $table->index(['claimed_by', 'claimed_at']); }); diff --git a/database/migrations/2026_07_16_000000_add_allow_in_process_to_scripts_tables.php b/database/migrations/2026_07_16_000000_add_allow_in_process_to_scripts_tables.php new file mode 100644 index 0000000000..676f364405 --- /dev/null +++ b/database/migrations/2026_07_16_000000_add_allow_in_process_to_scripts_tables.php @@ -0,0 +1,35 @@ +boolean('allow_in_process')->default(false)->after('timeout'); + }); + + Schema::table('script_versions', function (Blueprint $table) { + $table->boolean('allow_in_process')->default(false)->after('timeout'); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::table('scripts', function (Blueprint $table) { + $table->dropColumn('allow_in_process'); + }); + + Schema::table('script_versions', function (Blueprint $table) { + $table->dropColumn('allow_in_process'); + }); + } +}; diff --git a/database/migrations/2026_07_20_000000_add_asset_link_to_environment_variables_table.php b/database/migrations/2026_07_20_000000_add_asset_link_to_environment_variables_table.php index 370df325dd..eff0bacc73 100644 --- a/database/migrations/2026_07_20_000000_add_asset_link_to_environment_variables_table.php +++ b/database/migrations/2026_07_20_000000_add_asset_link_to_environment_variables_table.php @@ -4,8 +4,7 @@ use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; -return new class extends Migration -{ +return new class extends Migration { /** * Run the migrations. */ diff --git a/database/migrations/2026_07_23_000000_add_do_not_update_to_environment_variables_table.php b/database/migrations/2026_07_23_000000_add_do_not_update_to_environment_variables_table.php index 122397d593..7ed1231a11 100644 --- a/database/migrations/2026_07_23_000000_add_do_not_update_to_environment_variables_table.php +++ b/database/migrations/2026_07_23_000000_add_do_not_update_to_environment_variables_table.php @@ -4,8 +4,7 @@ use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; -return new class extends Migration -{ +return new class extends Migration { /** * Run the migrations. */ diff --git a/database/migrations/2026_09_08_143906_drop_data_lake_views.php b/database/migrations/2026_09_08_143906_drop_data_lake_views.php index 06212eb08e..5df6ed27d6 100644 --- a/database/migrations/2026_09_08_143906_drop_data_lake_views.php +++ b/database/migrations/2026_09_08_143906_drop_data_lake_views.php @@ -4,8 +4,7 @@ use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; -return new class extends Migration -{ +return new class extends Migration { /** * Run the migrations. */ diff --git a/database/migrations/2026_11_09_141900_change_settings_config_to_longtext.php b/database/migrations/2026_11_09_141900_change_settings_config_to_longtext.php new file mode 100644 index 0000000000..616fc56637 --- /dev/null +++ b/database/migrations/2026_11_09_141900_change_settings_config_to_longtext.php @@ -0,0 +1,24 @@ +longText('config')->nullable()->change(); + }); + } + + public function down(): void + { + Schema::table('settings', function (Blueprint $table) { + $table->text('config')->nullable()->change(); + }); + } +}; diff --git a/database/migrations/2026_11_12_000000_add_execution_mode_to_scripts_tables.php b/database/migrations/2026_11_12_000000_add_execution_mode_to_scripts_tables.php new file mode 100644 index 0000000000..bde692a11b --- /dev/null +++ b/database/migrations/2026_11_12_000000_add_execution_mode_to_scripts_tables.php @@ -0,0 +1,34 @@ +string('execution_mode', 16) + ->default('bare') + ->after('allow_in_process'); + }); + + Schema::table('script_versions', function (Blueprint $table): void { + $table->string('execution_mode', 16) + ->default('bare') + ->after('allow_in_process'); + }); + } + + public function down(): void + { + Schema::table('scripts', function (Blueprint $table): void { + $table->dropColumn('execution_mode'); + }); + + Schema::table('script_versions', function (Blueprint $table): void { + $table->dropColumn('execution_mode'); + }); + } +}; diff --git a/database/migrations/2026_11_13_110400_add_preview_presets_to_screens_table.php b/database/migrations/2026_11_13_110400_add_preview_presets_to_screens_table.php new file mode 100644 index 0000000000..d39a69ceb3 --- /dev/null +++ b/database/migrations/2026_11_13_110400_add_preview_presets_to_screens_table.php @@ -0,0 +1,39 @@ +mediumText('preview_presets')->nullable()->after('watchers'); + }); + + Schema::table('screen_versions', function (Blueprint $table) { + $table->mediumText('preview_presets')->nullable()->after('watchers'); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('screen_versions', function (Blueprint $table) { + $table->dropColumn('preview_presets'); + }); + + Schema::table('screens', function (Blueprint $table) { + $table->dropColumn('preview_presets'); + }); + } +}; diff --git a/database/seeders/DatabaseSeeder.php b/database/seeders/DatabaseSeeder.php index d99cbf8465..05d15aa04c 100644 --- a/database/seeders/DatabaseSeeder.php +++ b/database/seeders/DatabaseSeeder.php @@ -31,6 +31,7 @@ public function run() SettingsMenusSeeder::class, ScreenEmailSeeder::class, MetricsApiEnvironmentVariableSeeder::class, + McpSettingsSeeder::class, ]); $this->callPluginSeeders(); } diff --git a/database/seeders/McpSettingsSeeder.php b/database/seeders/McpSettingsSeeder.php new file mode 100644 index 0000000000..7965696fcf --- /dev/null +++ b/database/seeders/McpSettingsSeeder.php @@ -0,0 +1,57 @@ + McpGuidance::SYSTEM_PROMPT_KEY], [ + 'format' => 'textarea', + 'config' => '', + 'name' => 'System Prompt', + 'helper' => 'Short global guidance injected into every ProcessMaker MCP session (all clients).', + 'group' => 'MCP', + 'group_id' => $groupId, + 'hidden' => false, + 'ui' => ['order' => 100], + ]); + + $skillsSetting = Setting::firstOrCreate(['key' => McpGuidance::SKILLS_KEY], [ + 'format' => 'component', + 'config' => [], + 'name' => 'Skills', + 'helper' => self::SKILLS_HELPER, + 'group' => 'MCP', + 'group_id' => $groupId, + 'hidden' => false, + 'ui' => [ + 'order' => 200, + 'component' => 'McpSkills', + ], + ]); + + if ($skillsSetting->helper !== self::SKILLS_HELPER) { + $skillsSetting->helper = self::SKILLS_HELPER; + $skillsSetting->saveOrFail(); + } + + Setting::reguard(); + } +} diff --git a/database/seeders/PermissionSeeder.php b/database/seeders/PermissionSeeder.php index 7ba4437677..1009208ec6 100644 --- a/database/seeders/PermissionSeeder.php +++ b/database/seeders/PermissionSeeder.php @@ -66,6 +66,7 @@ class PermissionSeeder extends Seeder 'delete-scripts', 'edit-scripts', 'view-scripts', + 'run-in-process-modules', 'create-script-categories', 'delete-script-categories', 'edit-script-categories', @@ -114,6 +115,14 @@ class PermissionSeeder extends Seeder 'edit-signals', 'delete-signals', ], + 'Plugins' => [ + 'manage-plugins', + 'create-plugins', + 'view-plugins', + 'install-plugins', + 'uninstall-plugins', + 'toggle-plugins', + ], ]; public function run($seedUser = null) diff --git a/package.json b/package.json index 1398279a10..c34a47e9c6 100644 --- a/package.json +++ b/package.json @@ -24,7 +24,9 @@ "stats": "STATS=1 NODE_OPTIONS=\"--max-old-space-size=8000\" mix", "vite:dev": "vite", "vite:build": "vite build", - "build:all": "npm run development && npm run vite:build" + "build:all": "npm run development && npm run vite:build", + "check:requirements": "node .agents/skills/manage-requirements/scripts/check-requirements.js", + "validate:requirements": "node .agents/skills/manage-requirements/scripts/validate-requirements.js" }, "devDependencies": { "@babel/eslint-parser": "^7.15.8", diff --git a/resources/js/admin/plugins/Plugins.vue b/resources/js/admin/plugins/Plugins.vue new file mode 100644 index 0000000000..c07f006b35 --- /dev/null +++ b/resources/js/admin/plugins/Plugins.vue @@ -0,0 +1,927 @@ + + + + + diff --git a/resources/js/admin/plugins/components/PluginOutput.vue b/resources/js/admin/plugins/components/PluginOutput.vue new file mode 100644 index 0000000000..42d3f4801d --- /dev/null +++ b/resources/js/admin/plugins/components/PluginOutput.vue @@ -0,0 +1,191 @@ + + + + + \ No newline at end of file diff --git a/resources/js/admin/plugins/index.js b/resources/js/admin/plugins/index.js new file mode 100644 index 0000000000..ad6dec3376 --- /dev/null +++ b/resources/js/admin/plugins/index.js @@ -0,0 +1,6 @@ +import Plugins from "./Plugins"; + +new Vue({ + el: "#plugins", + components: { Plugins }, +}); diff --git a/resources/js/admin/plugins/output.js b/resources/js/admin/plugins/output.js new file mode 100644 index 0000000000..46a5a78e44 --- /dev/null +++ b/resources/js/admin/plugins/output.js @@ -0,0 +1,6 @@ +import PluginOutput from "./components/PluginOutput"; + +new Vue({ + el: "#plugin-output", + components: { PluginOutput }, +}); diff --git a/resources/js/admin/settings/components/McpSkills.vue b/resources/js/admin/settings/components/McpSkills.vue new file mode 100644 index 0000000000..de526d769d --- /dev/null +++ b/resources/js/admin/settings/components/McpSkills.vue @@ -0,0 +1,277 @@ + + + diff --git a/resources/js/admin/settings/index.js b/resources/js/admin/settings/index.js index 3a7cc2fa6b..20ab672c03 100644 --- a/resources/js/admin/settings/index.js +++ b/resources/js/admin/settings/index.js @@ -3,9 +3,11 @@ import * as ScreenBuilder from "@processmaker/screen-builder"; import "@processmaker/screen-builder/dist/vue-form-builder.css"; import SettingsGroups from "./components/SettingsGroups.vue"; import SettingsMain from "./components/SettingsMain.vue"; +import McpSkills from "./components/McpSkills.vue"; window.ScreenBuilder = ScreenBuilder; window.Vue.use(ScreenBuilder.default); +window.__setting_component_McpSkills = McpSkills; new Vue({ el: "#settings", diff --git a/resources/js/components/shared/Modal.vue b/resources/js/components/shared/Modal.vue index 5e3b273977..ce20062f8d 100644 --- a/resources/js/components/shared/Modal.vue +++ b/resources/js/components/shared/Modal.vue @@ -14,6 +14,8 @@ :header-class="headerClass" :footer-class="footerClass" :content-class="contentClass" + :no-close-on-esc="noCloseOnEsc" + :hide-header-close="hideHeaderClose" :size="size" :ok-only="okOnly" no-close-on-backdrop @@ -134,7 +136,9 @@ "titleIcon", "headerClass", "footerClass", - "contentClass" + "contentClass", + "noCloseOnEsc", + "hideHeaderClose" ], data() { return { diff --git a/resources/js/processes/screen-builder/screen.vue b/resources/js/processes/screen-builder/screen.vue index a9912c5f7c..2d13af2a82 100644 --- a/resources/js/processes/screen-builder/screen.vue +++ b/resources/js/processes/screen-builder/screen.vue @@ -122,6 +122,133 @@ id="showDataInput" v-model="showDataInput" > + + +
+ + +
+
= this.previewPresets.length) { + return null; + } + return index; + }, + previewPresetDirty() { + const index = this.normalizedActivePreviewPresetIndex; + if (index === null || !this.previewInputValid) { + return false; + } + return !_.isEqual( + JSON.parse(this.previewInput), + this.getPresetDataObject(this.previewPresets[index]), + ); + }, + previewPresetsForApi() { + return this.previewPresets.map((preset) => ({ + title: preset.title, + // string: PHP json_decode turns {} into [] if data is an object + data: this.serializePresetData(preset.data), + })); + }, displayBuilder() { return this.mode === "editor"; }, @@ -687,6 +875,7 @@ export default { computed: this.computed, custom_css: this.customCSS, watchers: this.watchers, + preview_presets: this.previewPresetsForApi, }) .then(() => { // Set draft status. @@ -759,6 +948,138 @@ export default { this.updateDataPreview(); } }, 1000), + previewPresetRowCss(index) { + return index % 2 === 0 ? "striped" : "bg-default"; + }, + normalizePreviewPresets(presets) { + if (!Array.isArray(presets)) { + return []; + } + return presets.map((preset) => ({ + title: preset.title || "", + data: this.serializePresetData(preset.data), + })); + }, + serializePresetData(data) { + if (typeof data === "string") { + try { + return JSON.stringify(JSON.parse(data)); + } catch (err) { + return "{}"; + } + } + return JSON.stringify(data ?? {}); + }, + getPresetDataObject(preset) { + if (!preset) { + return {}; + } + if (typeof preset.data === "string") { + try { + return JSON.parse(preset.data); + } catch (err) { + return {}; + } + } + return preset.data ?? {}; + }, + persistPreviewPresets() { + this.screen.preview_presets = this.previewPresetsForApi; + this.handleAutosave(true); + }, + applyPreviewPreset(index) { + const presetIndex = Number(index); + const preset = this.previewPresets[presetIndex]; + if (!preset || typeof preset.data === "undefined") { + return; + } + this.activePreviewPresetIndex = presetIndex; + this.previewInput = JSON.stringify(this.getPresetDataObject(preset), null, 2); + this.previewData = JSON.parse(this.previewInput); + this.previewDataStringify = this.previewInput; + this.rendererKey += 1; + if (this.mode === "preview") { + this.$dataProvider.flushScreenCache(); + } + }, + updateActivePreviewPreset() { + const index = this.normalizedActivePreviewPresetIndex; + if (index === null || !this.previewInputValid) { + return; + } + this.$set( + this.previewPresets[index], + "data", + this.serializePresetData(JSON.parse(this.previewInput)), + ); + this.persistPreviewPresets(); + }, + discardPreviewPresetChanges() { + const index = this.normalizedActivePreviewPresetIndex; + if (index === null) { + return; + } + this.applyPreviewPreset(index); + }, + addPreviewPreset() { + if (!this.previewInputValid) { + ProcessMaker.alert(this.$t("Invalid JSON Data Object"), "danger"); + return; + } + this.previewPresetCardType = "insert"; + this.previewPresetTitle = ""; + this.previewPresetEditIndex = null; + this.showPreviewPresetCard = true; + }, + showEditPreviewPreset(index) { + this.previewPresetCardType = "edit"; + this.previewPresetEditIndex = Number(index); + this.previewPresetTitle = this.previewPresets[this.previewPresetEditIndex].title; + this.showPreviewPresetCard = true; + }, + cancelPreviewPresetCard() { + this.showPreviewPresetCard = false; + this.previewPresetEditIndex = null; + this.previewPresetTitle = ""; + }, + savePreviewPresetCard() { + const title = (this.previewPresetTitle || "").trim(); + if (!title) { + return; + } + if (this.previewPresetCardType === "insert") { + if (!this.previewInputValid) { + return; + } + this.previewPresets.push({ + title, + data: this.serializePresetData(JSON.parse(this.previewInput)), + }); + this.activePreviewPresetIndex = this.previewPresets.length - 1; + } else { + const preset = this.previewPresets[this.previewPresetEditIndex]; + if (!preset) { + return; + } + this.$set(preset, "title", title); + } + this.cancelPreviewPresetCard(); + this.persistPreviewPresets(); + }, + removePreviewPreset(index) { + const removeIndex = Number(index); + const active = this.normalizedActivePreviewPresetIndex; + this.previewPresets.splice(removeIndex, 1); + if (active === removeIndex) { + this.activePreviewPresetIndex = null; + } else if (active !== null && active > removeIndex) { + this.activePreviewPresetIndex = active - 1; + } + this.persistPreviewPresets(); + }, + onPreviewPresetsReorder() { + this.persistPreviewPresets(); + }, // eslint-disable-next-line func-names updateDataPreview: debounce(function () { this.previewDataStringify = JSON.stringify(this.previewData, null, 2); @@ -880,6 +1201,9 @@ export default { that.computed = that.screen.computed ? that.screen.computed : []; that.customCSS = that.screen.custom_css ? that.screen.custom_css : ""; that.watchers = that.screen.watchers ? that.screen.watchers : []; + that.previewPresets = that.normalizePreviewPresets( + that.screen.preview_presets, + ); that.previewInput = "{}"; that.preview.custom_css = that.customCSS; ProcessMaker.EventBus.$emit("screen-builder-start", that); @@ -976,7 +1300,7 @@ export default { const data = item.config || {}; const rules = {}; - item.inspector.forEach((property) => { + (item.inspector || []).forEach((property) => { if (property.config.validation) { if ( property.config.validation.includes( @@ -1167,6 +1491,7 @@ export default { computed: this.computed, custom_css: this.customCSS, watchers: this.watchers, + preview_presets: this.previewPresetsForApi, }) .then((response) => { if (exportScreen) { @@ -1289,6 +1614,20 @@ body { max-width: 265px; } +.preview-presets { + max-height: 220px; + overflow-y: auto; + font-size: 0.875rem; + + .striped { + background-color: rgba(0, 0, 0, 0.05); + } + + .fa-plus-square { + cursor: pointer; + } +} + .data-collapse { height: 225px; } diff --git a/resources/lang/en.json b/resources/lang/en.json index 87674aa096..63e6131518 100644 --- a/resources/lang/en.json +++ b/resources/lang/en.json @@ -193,6 +193,7 @@ "Are you sure you want to delete the auth client": "Are you sure you want to delete the auth client", "Are you sure you want to delete the calc ?:": "Are you sure you want to delete the calc ?", "Are you sure you want to delete the environment variable {{ name }} ?": "Are you sure you want to delete the environment variable {{ name }} ?", + "Are you sure you want to delete the plugin {{ name }}?": "Are you sure you want to delete the plugin {{ name }}?", "Are you sure you want to delete the screen {{item}}? Deleting this asset will break any active tasks that are assigned.": "Are you sure you want to delete the screen {{item}}? Deleting this asset will break any active tasks that are assigned.", "Are you sure you want to delete the token ": "Are you sure you want to delete the token ", "Are you sure you want to delete the user": "Are you sure you want to delete the user", @@ -304,6 +305,8 @@ "BPMN Issues": "BPMN Issues", "BPMN validation issues. Request cannot be started.": "BPMN validation issues. Request cannot be started.", "BPMN": "BPMN", + "Branch": "Branch", + "Branch must be a valid branch name": "Branch must be a valid branch name", "Browse": "Browse", "Browser": "Browser", "Build Command Output": "Build Command Output", @@ -518,6 +521,7 @@ "Create new copies of bundle assets.": "Create new copies of bundle assets.", "Create new DevLink": "Create new DevLink", "Create Notifications": "Create Notifications", + "Create Plugin": "Create Plugin", "Create Process Categories": "Create Process Categories", "Create Process Category": "Create Process Category", "Create Process": "Create Process", @@ -635,6 +639,7 @@ "Delete Groups": "Delete Groups", "Delete Notifications": "Delete Notifications", "Delete Page": "Delete Page", + "Delete Plugin": "Delete Plugin", "Delete Process Categories": "Delete Process Categories", "Delete Record": "Delete Record", "Delete Screen Categories": "Delete Screen Categories", @@ -744,6 +749,7 @@ "Edit Option": "Edit Option", "Edit Page Title": "Edit Page Title", "Edit Page": "Edit Page", + "Edit Plugin": "Edit Plugin", "Edit Process Categories": "Edit Process Categories", "Edit Process Category": "Edit Process Category", "Edit Process": "Edit Process", @@ -1589,6 +1595,17 @@ "Please wait while the files are generated. The screen will be updated when finished.": "Please wait while the files are generated. The screen will be updated when finished.", "Please wait while your content is loaded": "Please wait while your content is loaded", "Please wait": "Please wait", + "Plugin": "Plugin", + "Plugins": "Plugins", + "Plugin Installation Output": "Plugin Installation Output", + "Plugin Uninstallation Output": "Plugin Uninstallation Output", + "Plugin Toggle Output": "Plugin Toggle Output", + "Uninstall Plugin": "Uninstall Plugin", + "Plugin successfully uninstalled. You can now close this window.": "Plugin successfully uninstalled. You can now close this window.", + "Plugin successfully updated. You can now close this window.": "Plugin successfully updated. You can now close this window.", + "Error uninstalling plugin. See output above.": "Error uninstalling plugin. See output above.", + "Error toggling plugin. See output above.": "Error toggling plugin. See output above.", + "Plugin Successfully Installed. You can now close this window. ": "Plugin Successfully Installed. You can now close this window. ", "PM Block Name": "PM Block Name", "PM Blocks": "PM Blocks", "Pm_block_category": "PM Block Category", @@ -1717,6 +1734,8 @@ "Repeat on": "Repeat on", "Report an issue": "Report an issue", "Repository Name": "Repository Name", + "Repository URL": "Repository URL", + "Repository URL must be a valid URL": "Repository URL must be a valid URL", "Request All": "Request All", "Request Canceled": "Request Canceled", "Request Commented": "Request Commented", @@ -2140,6 +2159,7 @@ "Table Prompt Suffix": "Table Prompt Suffix", "Table": "Table", "Tag": "Tag", + "Tag must be a valid tag name": "Tag must be a valid tag name", "Takes precedence over value set in data.": "Takes precedence over value set in data.", "Task :task_id could not be reassigned because the task settings prevent it from being reassigned": "Task :task_id could not be reassigned because the task settings prevent it from being reassigned", "Task {{- subject }} completed by {{- user }}": "Task {{- subject }} completed by {{- user }}", @@ -2280,6 +2300,8 @@ "The package is not installed": "The package is not installed", "The page you are looking for could not be found": "The page you are looking for could not be found", "The placeholder is what is shown in the field when no value is provided yet": "The placeholder is what is shown in the field when no value is provided yet", + "The plugin was toggled.": "The plugin was toggled.", + "The plugin was deleted.": "The plugin was deleted.", "The PM Block name must be unique.": "The PM Block name must be unique.", "The Process field is required.": "The Process field is required.", "The process name must be unique.": "The process name must be unique.", @@ -2413,6 +2435,7 @@ "Toggle Configuration": "Toggle Configuration", "Toggle Notifications": "Toggle Notifications", "Toggle Show Password": "Toggle Show Password", + "Toggle": "Toggle", "Toggle Style": "Toggle Style", "Token URL": "Token URL", "Token_id": "Token ID", @@ -2420,6 +2443,7 @@ "TokenCreated": "Token Created", "TokenDeleted": "Token Deleted", "Tokens": "Tokens", + "Toggle Plugin": "Toggle Plugin", "Too many login attempts. Please try again in :seconds seconds.": "Too many login attempts. Please try again in :seconds seconds.", "Tooltip Contains": "Tooltip Contains", "Tooltip": "Tooltip", diff --git a/resources/lang/es.json b/resources/lang/es.json index cb2e96816f..6e51100555 100644 --- a/resources/lang/es.json +++ b/resources/lang/es.json @@ -289,6 +289,8 @@ "BPMN Issues": "Problemas de BPMN", "BPMN validation issues. Request cannot be started.": "Problemas de validación de BPMN. No se puede iniciar la solicitud.", "BPMN": "BPMN", + "Branch": "Rama", + "Branch must be a valid branch name": "La rama debe ser una rama válida", "Browse": "Examinar", "Browser": "Navegador", "Build Command Output": "Generar salida de comando", @@ -494,6 +496,7 @@ "Create new copies of bundle assets.": "Crear nuevas copias de los activos del paquete.", "Create new DevLink": "Crear nuevo DevLink", "Create Notifications": "Crear notificaciones", + "Create Plugin": "Crear plugin", "Create Process Categories": "Crear categorías de procesos", "Create Process Category": "Crear categoría de procesos", "Create Process": "Crear proceso", @@ -610,6 +613,7 @@ "Delete Groups": "Eliminar grupos", "Delete Notifications": "Eliminar notificaciones", "Delete Page": "Eliminar página", + "Delete Plugin": "Eliminar plugin", "Delete Process Categories": "Borrar las categorías de proceso", "Delete Record": "Eliminar registro", "Delete Screen Categories": "Eliminar categorías de pantalla", @@ -715,6 +719,7 @@ "Edit Option": "Editar opción", "Edit Page Title": "Editar título de página", "Edit Page": "Editar página", + "Edit Plugin": "Editar plugin", "Edit Process Categories": "Editar categorías de procesos", "Edit Process Category": "Editar categoría de procesos", "Edit Process": "Editar proceso", @@ -1527,6 +1532,10 @@ "Please wait while the files are generated. The screen will be updated when finished.": "Espere mientras se generan los archivos. La pantalla se actualizará cuando termine.", "Please wait while your content is loaded": "Espere mientras se carga su contenido", "Please wait": "Por favor espera", + "Plugin": "Plugin", + "Plugins": "Plugins", + "Plugin Installation Output": "Resultado de la instalación de plugin", + "Plugin Successfully Installed. You can now close this window. ": "Plugin instalado correctamente. Puede cerrar esta ventana.", "PM Block Name": "Nombre del Bloque PM", "PM Blocks": "Bloques PM", "Pm_block_category": "Categoría de Bloque PM", @@ -1652,6 +1661,8 @@ "Repeat on": "Repetir en", "Report an issue": "Reportar un problema", "Repository Name": "Nombre del Repositorio", + "Repository URL": "URL del Repositorio", + "Repository URL must be a valid URL": "La URL del repositorio debe ser una URL válida", "Request All": "Todas las solicitudes", "Request Canceled": "Solicitud cancelada", "Request Commented": "Solicitud Comentada", @@ -2068,6 +2079,7 @@ "Table": "Tabla", "Table Prompt Suffix": "Sufijo del Mensaje de Tabla", "Tag": "Etiqueta", + "Tag must be a valid tag name": "La etiqueta debe ser una etiqueta válida", "take care of things automatically.": "se encargan de las cosas automáticamente.", "Takes precedence over value set in data.": "Tiene prioridad sobre el conjunto de valores en los datos.", "Task :task_id could not be reassigned because the task settings prevent it from being reassigned": "La tarea :task_id no pudo ser reasignada porque la configuración de la tarea lo impide", @@ -2204,6 +2216,8 @@ "The package is not installed": "El paquete no esta instalado", "The page you are looking for could not be found": "No se pudo encontrar la página que está buscando", "The placeholder is what is shown in the field when no value is provided yet": "El marcador de posición es lo que se muestra en el campo cuando aún no se proporciona ningún valor", + "The plugin was toggled.": "El plugin fue activado/desactivado.", + "The plugin was deleted.": "El plugin fue eliminado.", "The PM Block name must be unique.": "El nombre del bloque PM debe ser único.", "The Process field is required.": "El campo Proceso es obligatorio.", "The process name must be unique.": "El nombre del proceso debe ser único.", @@ -2342,6 +2356,8 @@ "TokenCreated": "Token Creado", "TokenDeleted": "Token Eliminado", "Tokens": "Tokens", + "Toggle": "Alternar", + "Toggle Plugin": "Activar/desactivar plugin", "Too many login attempts. Please try again in :seconds seconds.": "Se han realizado demasiados intentos de inicio de sesión. Inténtelo de nuevo en: segundos segundos.", "Tooltip Contains": "La descripción emergente contiene", "Tooltip": "Descripción emergente", diff --git a/resources/views/admin/plugins/index.blade.php b/resources/views/admin/plugins/index.blade.php new file mode 100644 index 0000000000..775beff3c0 --- /dev/null +++ b/resources/views/admin/plugins/index.blade.php @@ -0,0 +1,31 @@ +@extends('layouts.layoutnextvite') + +@section('title') + {{__('Plugins')}} +@endsection + +@section('sidebar') + @include('layouts.sidebar', ['sidebar'=> Menu::get('sidebar_admin')]) +@endsection + +@section('breadcrumbs') + @include('shared.breadcrumbs', ['routes' => [ + __('Admin') => route('admin.index'), + __('Plugins') => null, + ]]) +@endsection +@section('content') +
+ +
+@endsection + +@section('js') + @vite('resources/js/loader/loaderMinimal.js') + + @vite(['resources/js/admin/plugins/index.js']) +@endsection \ No newline at end of file diff --git a/resources/views/processes/scripts/edit.blade.php b/resources/views/processes/scripts/edit.blade.php index 1ef36139d1..0b25291bee 100644 --- a/resources/views/processes/scripts/edit.blade.php +++ b/resources/views/processes/scripts/edit.blade.php @@ -81,6 +81,38 @@ {{ html()->textarea('description')->id('description')->rows(4)->class('form-control')->attribute('v-model', 'formData.description')->attribute('v-bind:class', '{"form-control":true, "is-invalid":errors.description}')->required()->attribute('aria-required', 'true') }}
+
+
+ + +
+ + {{ __('Run this trusted PHP script in the ProcessMaker worker (no Docker). Requires CORE_SERVICE_TASK_ENABLED. Use Script Runtime modules for package helpers.') }} + + +
+ {{ html()->label(__('Execution mode'), 'execution_mode') }} + {{ html()->select('execution_mode', [ + 'bare' => __('Bare subprocess'), + 'modules' => __('Laravel modules'), + ])->class('form-control')->attribute('v-model', 'formData.execution_mode')->attribute('v-bind:class', '{"form-control":true, "is-invalid":errors.execution_mode}') }} + + {{ __('Laravel modules run inside the worker and should only be used for trusted scripts.') }} + + +
+
user()?->is_administrator || auth()->user()?->can('run-in-process-modules')) + ), editScriptHooks: [], } }, + computed: { + isPhpScript() { + const executor = this.scriptExecutors[this.formData.script_executor_id]; + return executor && String(executor.language).toLowerCase() === 'php'; + }, + }, watch: { selectedProjects: { handler() { this.formData.projects = this.selectedProjects; } - } + }, + isPhpScript(isPhp) { + if (!isPhp) { + this.formData.allow_in_process = false; + this.formData.execution_mode = 'bare'; + } + }, }, methods: { resetErrors() { @@ -186,6 +236,8 @@ title: null, language: null, description: null, + allow_in_process: null, + execution_mode: null, status: null }); }, @@ -207,6 +259,8 @@ retry_attempts: this.formData.retry_attempts, retry_wait_time: this.formData.retry_wait_time, script_executor_id: this.formData.script_executor_id, + allow_in_process: this.isPhpScript ? !!this.formData.allow_in_process : false, + execution_mode: this.isPhpScript ? (this.formData.execution_mode || 'bare') : 'bare', }) .then(() => { ProcessMaker.alert(this.$t('The script was saved.'), 'success'); @@ -226,6 +280,8 @@ }, mounted() { this.selectedProjects = this.assignedProjects.length > 0 ? this.assignedProjects.map(project => project.id) : null; + this.formData.allow_in_process = !!this.formData.allow_in_process; + this.formData.execution_mode = this.formData.execution_mode || 'bare'; } }); }); diff --git a/routes/ai.php b/routes/ai.php new file mode 100644 index 0000000000..c5b8e8ede1 --- /dev/null +++ b/routes/ai.php @@ -0,0 +1,38 @@ +middleware(['auth:api']); + +Mcp::local('screens', ScreenServer::class); + +Mcp::web('/mcp/processes', ProcessServer::class) + ->middleware(['auth:api']); + +Mcp::local('processes', ProcessServer::class); + +Mcp::web('/mcp/data-connectors', DataConnectorsServer::class) + ->middleware(['auth:api']); + +Mcp::local('data-connectors', DataConnectorsServer::class); + +Mcp::web('/mcp/flow-genies', FlowGenieServer::class) + ->middleware(['auth:api']); + +Mcp::local('flow-genies', FlowGenieServer::class); diff --git a/routes/api.php b/routes/api.php index f544b47090..14648d0ccf 100644 --- a/routes/api.php +++ b/routes/api.php @@ -10,6 +10,9 @@ use ProcessMaker\Http\Controllers\Api\CommentController; use ProcessMaker\Http\Controllers\Api\CssOverrideController; use ProcessMaker\Http\Controllers\Api\DebugController; +use ProcessMaker\Http\Controllers\Api\DeployProcess; +use ProcessMaker\Http\Controllers\Api\DeployScreen; +use ProcessMaker\Http\Controllers\Api\DeployScript; use ProcessMaker\Http\Controllers\Api\DevLinkController; use ProcessMaker\Http\Controllers\Api\EncryptedDataController; use ProcessMaker\Http\Controllers\Api\EnvironmentVariablesController; @@ -22,6 +25,7 @@ use ProcessMaker\Http\Controllers\Api\NotificationController; use ProcessMaker\Http\Controllers\Api\OpenAIController; use ProcessMaker\Http\Controllers\Api\PermissionController; +use ProcessMaker\Http\Controllers\Api\PluginController; use ProcessMaker\Http\Controllers\Api\ProcessCategoryController; use ProcessMaker\Http\Controllers\Api\ProcessController; use ProcessMaker\Http\Controllers\Api\ProcessLaunchpadController; @@ -47,6 +51,13 @@ use ProcessMaker\Http\Controllers\TestStatusController; Route::middleware('auth:api', 'setlocale', 'bindings', 'sanitize', 'manager')->prefix('api/1.0')->name('api.')->group(function () { + // Plugins + Route::get('plugins', [PluginController::class, 'index'])->name('plugins.index')->middleware('can:manage-plugins'); + Route::post('plugins/install', [PluginController::class, 'install'])->name('plugins.install')->middleware('can:manage-plugins'); + Route::post('plugins/{name}/reinstall', [PluginController::class, 'reinstall'])->name('plugins.reinstall')->middleware('can:manage-plugins'); + Route::delete('plugins/{name}', [PluginController::class, 'destroy'])->name('plugins.destroy')->middleware('can:manage-plugins'); + Route::patch('plugins/{name}/toggle', [PluginController::class, 'toggle'])->name('plugins.toggle')->middleware('can:manage-plugins'); + // Users Route::get('users', [UserController::class, 'index'])->name('users.index'); // Permissions handled in the controller Route::get('users/{user}', [UserController::class, 'show'])->name('users.show'); // Permissions handled in the controller @@ -133,6 +144,26 @@ Route::get('scripts/execution/{key}', [ScriptController::class, 'execution'])->name('scripts.execution'); Route::post('scripts/microservice/execution', [ScriptController::class, 'microserviceExecution']); + // Deploy / sync helpers (core). Canonical paths + package-skeleton aliases for BC. + Route::post('deploy-script', [DeployScript::class, 'deployScript'])->name('deploy-script'); + Route::get('script-php', [DeployScript::class, 'getScriptPhp'])->name('script-php'); + Route::post('deploy-screen', [DeployScreen::class, 'deployScreen'])->name('deploy-screen'); + Route::get('screen-yaml', [DeployScreen::class, 'getScreenYaml'])->name('screen-yaml'); + Route::post('deploy-process', [DeployProcess::class, 'deployProcess'])->name('deploy-process'); + Route::get('process-bpmn', [DeployProcess::class, 'getProcessBpmn'])->name('process-bpmn'); + Route::post('package-skeleton/deploy-script', [DeployScript::class, 'deployScript']) + ->name('package-skeleton.deploy-script'); + Route::get('package-skeleton/script-php', [DeployScript::class, 'getScriptPhp']) + ->name('package-skeleton.script-php'); + Route::post('package-skeleton/deploy-screen', [DeployScreen::class, 'deployScreen']) + ->name('package-skeleton.deploy-screen'); + Route::get('package-skeleton/screen-yaml', [DeployScreen::class, 'getScreenYaml']) + ->name('package-skeleton.screen-yaml'); + Route::post('package-skeleton/deploy-process', [DeployProcess::class, 'deployProcess']) + ->name('package-skeleton.deploy-process'); + Route::get('package-skeleton/process-bpmn', [DeployProcess::class, 'getProcessBpmn']) + ->name('package-skeleton.process-bpmn'); + // Script Categories Route::get('script_categories', [ScriptCategoryController::class, 'index'])->name('script_categories.index')->middleware('can:view-script-categories'); Route::get('script_categories/{script_category}', [ScriptCategoryController::class, 'show'])->name('script_categories.show')->middleware('can:view-script-categories'); diff --git a/routes/web.php b/routes/web.php index c2273f4208..e0c786dac3 100644 --- a/routes/web.php +++ b/routes/web.php @@ -11,6 +11,7 @@ use ProcessMaker\Http\Controllers\Admin\GroupController; use ProcessMaker\Http\Controllers\Admin\LdapLogsController; use ProcessMaker\Http\Controllers\Admin\LogsController; +use ProcessMaker\Http\Controllers\Admin\PluginController; use ProcessMaker\Http\Controllers\Admin\QueuesController; use ProcessMaker\Http\Controllers\Admin\ScriptExecutorController; use ProcessMaker\Http\Controllers\Admin\SettingsController; @@ -78,6 +79,8 @@ Route::get('script-executors', [ScriptExecutorController::class, 'index'])->name('script-executors.index'); + Route::get('plugins', [PluginController::class, 'index'])->name('plugins.index')->middleware('can:manage-plugins'); + // Tenant Jobs Dashboard Route::get('tenant-queues', [TenantQueueController::class, 'index'])->name('tenant-queue.index'); diff --git a/tests/DuskTestCase.php b/tests/DuskTestCase.php index 2af55ba64f..4ab5816933 100644 --- a/tests/DuskTestCase.php +++ b/tests/DuskTestCase.php @@ -25,7 +25,7 @@ public static function prepare() /** * Create the RemoteWebDriver instance. * - * @return \Facebook\WebDriver\Remote\RemoteWebDriver + * @return RemoteWebDriver */ protected function driver() { diff --git a/tests/Feature/Api/ConvertBPMNTest.php b/tests/Feature/Api/ConvertBPMNTest.php index 4dd36be1ea..2c23a64c3e 100644 --- a/tests/Feature/Api/ConvertBPMNTest.php +++ b/tests/Feature/Api/ConvertBPMNTest.php @@ -24,7 +24,7 @@ class ConvertBPMNTest extends TestCase public $withPermissions = true; /** - * Test convert subProcess to callActivity + * Embedded subProcess BPMN is persisted as-is on save (no callActivity conversion). */ public function testConvertSubProcess() { @@ -32,8 +32,14 @@ public function testConvertSubProcess() 'status' => 'ACTIVE', 'bpmn' => file_get_contents(__DIR__ . '/processes/CP.01.00 Create new customer.bpmn'), ]); - $this->assertStringNotContainsString('subProcess', $process->bpmn); - $this->assertEquals(2, Process::count()); + $this->assertEquals(1, Process::count()); + + $document = new BpmnDocument(); + $document->loadXML($process->bpmn); + $subProcesses = $document->getElementsByTagNameNS(BpmnDocument::BPMN_MODEL, 'subProcess'); + $this->assertGreaterThan(0, $subProcesses->length); + $callActivities = $document->getElementsByTagNameNS(BpmnDocument::BPMN_MODEL, 'callActivity'); + $this->assertSame(0, $callActivities->length); } /** diff --git a/tests/Feature/Api/DeployProcessControllerTest.php b/tests/Feature/Api/DeployProcessControllerTest.php new file mode 100644 index 0000000000..265f624e13 --- /dev/null +++ b/tests/Feature/Api/DeployProcessControllerTest.php @@ -0,0 +1,101 @@ +create(['title' => 'sample-screen-ref']); + $script = Script::factory()->create(['title' => 'sample-script-ref']); + + $this->apiCall('POST', self::DEPLOY_ENDPOINT, [ + 'name' => 'sample-process', + 'bpmn' => $this->sampleBpmn( + '${screen:sample-screen-ref}', + '${script:sample-script-ref}' + ), + ]) + ->assertOk() + ->assertJson([ + 'success' => true, + 'action' => 'created', + 'name' => 'sample-process', + ]); + + $process = Process::where('name', 'sample-process')->firstOrFail(); + $this->assertStringContainsString('pm:screenRef="' . $screen->id . '"', $process->bpmn); + $this->assertStringContainsString('pm:scriptRef="' . $script->id . '"', $process->bpmn); + } + + public function testDeployProcessAcceptsDescription(): void + { + $summary = 'Brief summary for the process catalog.'; + + $this->apiCall('POST', self::DEPLOY_ENDPOINT, [ + 'name' => 'described-process', + 'description' => $summary, + 'bpmn' => $this->sampleBpmn('', ''), + ])->assertOk(); + + $process = Process::where('name', 'described-process')->firstOrFail(); + $this->assertSame($summary, $process->description); + + $updated = $summary . ' Updated.'; + $this->apiCall('POST', self::DEPLOY_ENDPOINT, [ + 'name' => 'described-process', + 'description' => $updated, + 'bpmn' => $this->sampleBpmn('', ''), + ])->assertOk(); + + $this->assertSame($updated, $process->refresh()->description); + } + + public function testGetProcessBpmnRestoresNamedReferences(): void + { + Screen::factory()->create(['title' => 'restore-screen']); + Script::factory()->create(['title' => 'restore-script']); + + $this->apiCall('POST', self::DEPLOY_ENDPOINT, [ + 'name' => 'restore-process', + 'bpmn' => $this->sampleBpmn( + '${screen:restore-screen}', + '${script:restore-script}' + ), + ])->assertOk(); + + $response = $this->apiCall('GET', self::GET_ENDPOINT . '?name=restore-process'); + + $response->assertOk()->assertJsonPath('success', true); + $this->assertStringContainsString('${screen:restore-screen}', $response->json('bpmn')); + $this->assertStringContainsString('${script:restore-script}', $response->json('bpmn')); + } + + private function sampleBpmn(string $screenReference, string $scriptReference): string + { + return << + + + + + + +XML; + } +} diff --git a/tests/Feature/Api/DeployScreenControllerTest.php b/tests/Feature/Api/DeployScreenControllerTest.php new file mode 100644 index 0000000000..c6a5f02a10 --- /dev/null +++ b/tests/Feature/Api/DeployScreenControllerTest.php @@ -0,0 +1,64 @@ +postJson('/api/1.0' . self::API_ENDPOINT, [ + 'yml_content' => "config: []\n", + 'name' => 'sample-screen', + ])->assertStatus(401); + } + + public function testDeployScreenCreatesFromYaml(): void + { + $yaml = <<<'YAML' +config: + - component: FormBlank + items: [] +computed: [] +watchers: [] +custom_css: '' +YAML; + + $this->apiCall('POST', self::API_ENDPOINT, [ + 'yml_content' => $yaml, + 'name' => 'sample-screen', + ]) + ->assertOk() + ->assertJson([ + 'success' => true, + 'action' => 'created', + 'name' => 'sample-screen', + ]); + + $this->assertNotNull(Screen::where('title', 'sample-screen')->first()); + } + + public function testGetScreenYamlReturnsYamlForPublishedScreen(): void + { + $yaml = "config: []\n"; + $this->apiCall('POST', self::API_ENDPOINT, [ + 'yml_content' => $yaml, + 'name' => 'yaml-pull-screen', + ])->assertOk(); + + $response = $this->actingAs($this->user, 'api') + ->get('/api/1.0' . self::GET_ENDPOINT . '?name=yaml-pull-screen'); + + $response->assertOk(); + $this->assertStringContainsString('yaml', (string) $response->headers->get('Content-Type')); + } +} diff --git a/tests/Feature/Api/DeployScriptControllerTest.php b/tests/Feature/Api/DeployScriptControllerTest.php new file mode 100644 index 0000000000..cd1740d451 --- /dev/null +++ b/tests/Feature/Api/DeployScriptControllerTest.php @@ -0,0 +1,141 @@ +postJson('/api/1.0' . self::API_ENDPOINT, [ + 'code' => ' 'sample-sync-script', + ])->assertStatus(401); + } + + public function testDeployScriptValidatesCodeRequired(): void + { + $this->apiCall('POST', self::API_ENDPOINT, [ + 'name' => 'sample-sync-script', + ]) + ->assertStatus(422) + ->assertJson(['success' => false]) + ->assertJsonValidationErrors(['code']); + } + + public function testDeployScriptCreatesWhenTitleDoesNotExist(): void + { + $this->ensureExecutor(); + $code = " true];\n"; + $name = 'sample-sync-script'; + + $response = $this->apiCall('POST', self::API_ENDPOINT, [ + 'code' => $code, + 'name' => $name, + ]); + + $response->assertStatus(200) + ->assertJson([ + 'success' => true, + 'action' => 'created', + 'name' => $name, + ]); + + $script = Script::where('title', $name)->first(); + $this->assertNotNull($script); + $this->assertSame(rtrim($code), rtrim((string) $script->code)); + $this->assertSame($this->user->id, $script->run_as_user_id); + } + + public function testCanonicalDeployScriptRouteCreatesScript(): void + { + $this->ensureExecutor(); + + $this->apiCall('POST', self::CANONICAL_ENDPOINT, [ + 'code' => ' true];', + 'name' => 'canonical-sync-script', + ]) + ->assertStatus(200) + ->assertJson([ + 'success' => true, + 'action' => 'created', + 'name' => 'canonical-sync-script', + ]); + } + + public function testDeployScriptUpdatesExistingScriptByTitle(): void + { + $existing = Script::factory()->create([ + 'title' => 'sample-update-script', + 'code' => ' true];', + 'run_as_user_id' => $this->user->id, + ]); + + $newCode = " true];\n"; + + $this->apiCall('POST', self::API_ENDPOINT, [ + 'code' => $newCode, + 'name' => 'sample-update-script', + ]) + ->assertStatus(200) + ->assertJson([ + 'success' => true, + 'action' => 'updated', + 'script_id' => $existing->id, + ]); + + $existing->refresh(); + $this->assertSame(rtrim($newCode), rtrim((string) $existing->code)); + } + + public function testGetScriptPhpByNameReturnsPhpCode(): void + { + $code = " true];\n"; + $script = Script::factory()->create([ + 'title' => 'sample-get-script', + 'code' => $code, + 'run_as_user_id' => $this->user->id, + ]); + + $response = $this->actingAs($this->user, 'api') + ->get('/api/1.0' . self::GET_ENDPOINT . '?name=sample-get-script'); + + $response->assertStatus(200); + $this->assertStringContainsString('text/plain', (string) $response->headers->get('Content-Type')); + $this->assertSame((string) $script->id, $response->headers->get('X-Script-Id')); + $this->assertSame($code, $response->getContent()); + } + + public function testDeployScriptPrefersPhpNayraExecutorOnCreate(): void + { + ScriptExecutor::factory()->create(['language' => 'php']); + $nayra = ScriptExecutor::factory()->create(['language' => 'php-nayra']); + + $this->apiCall('POST', self::API_ENDPOINT, [ + 'code' => ' 'new-nayra-script', + ]) + ->assertStatus(200) + ->assertJson(['action' => 'created']); + + $script = Script::where('title', 'new-nayra-script')->first(); + $this->assertSame($nayra->id, $script->script_executor_id); + } + + private function ensureExecutor(): ScriptExecutor + { + return ScriptExecutor::query()->first() ?? ScriptExecutor::factory()->create(); + } +} diff --git a/tests/Feature/Api/OAuthTest.php b/tests/Feature/Api/OAuthTest.php index 491609ee30..c3c23bb0f2 100644 --- a/tests/Feature/Api/OAuthTest.php +++ b/tests/Feature/Api/OAuthTest.php @@ -11,6 +11,7 @@ class OAuthTest extends TestCase use RequestHelper; private const OAUTH_CLIENTS_URL = '/oauth/clients'; + private const OAUTH_CLIENT_URL = '/oauth/clients/'; public $json = null; diff --git a/tests/Feature/Api/PluginsTest.php b/tests/Feature/Api/PluginsTest.php new file mode 100644 index 0000000000..6caa962605 --- /dev/null +++ b/tests/Feature/Api/PluginsTest.php @@ -0,0 +1,98 @@ +apiCall('POST', '/plugins/install', [ + 'url' => 'https://github.com/ProcessMaker/package-translations.git', + 'branch' => 'develop', + ]); + + $response->assertStatus(200); + $response->assertJson([ + 'queued' => true, + 'message' => 'Plugin installation queued', + ]); + + Bus::assertDispatched(InstallPlugin::class, function (InstallPlugin $job) { + return $job->userId === $this->user->id + && $job->url === 'https://github.com/ProcessMaker/package-translations.git' + && $job->branch === 'develop' + && $job->tag === null + && $job->zipPath === null + && $job->gitUsername === null + && $job->gitToken === null + && $job->timeout >= 600; + }); + } + + public function testUninstallQueuesJob(): void + { + Bus::fake([UninstallPlugin::class]); + + $response = $this->apiCall('DELETE', '/plugins/package-translations'); + + $response->assertStatus(200); + $response->assertJson(['queued' => true]); + + Bus::assertDispatched(UninstallPlugin::class, function (UninstallPlugin $job) { + return $job->pluginName === 'package-translations' + && $job->userId === $this->user->id; + }); + } + + public function testToggleQueuesJob(): void + { + Bus::fake([TogglePlugin::class]); + + $response = $this->apiCall('PATCH', '/plugins/package-translations/toggle'); + + $response->assertStatus(200); + $response->assertJson(['queued' => true]); + + Bus::assertDispatched(TogglePlugin::class, function (TogglePlugin $job) { + return $job->pluginName === 'package-translations' + && $job->userId === $this->user->id; + }); + } + + public function testInstallRejectsNonHttpsUrl(): void + { + Bus::fake([InstallPlugin::class]); + + $response = $this->apiCall('POST', '/plugins/install', [ + 'url' => storage_path('plugins'), + ]); + + $response->assertStatus(422); + Bus::assertNotDispatched(InstallPlugin::class); + } + + public function testInstallRejectsBranchAndTagTogether(): void + { + Bus::fake([InstallPlugin::class]); + + $response = $this->apiCall('POST', '/plugins/install', [ + 'url' => 'https://github.com/ProcessMaker/package-translations.git', + 'branch' => 'develop', + 'tag' => 'v1.0.0', + ]); + + $response->assertStatus(422); + Bus::assertNotDispatched(InstallPlugin::class); + } +} diff --git a/tests/Feature/Api/RequestFileUploadTest.php b/tests/Feature/Api/RequestFileUploadTest.php index 15446b8696..992cf11529 100644 --- a/tests/Feature/Api/RequestFileUploadTest.php +++ b/tests/Feature/Api/RequestFileUploadTest.php @@ -28,7 +28,7 @@ class RequestFileUploadTest extends TestCase protected $task; /** - * @var \ProcessMaker\Models\User[] + * @var User[] */ protected $assigned = []; diff --git a/tests/Feature/Api/ScreenTest.php b/tests/Feature/Api/ScreenTest.php index d6b15608b3..c6455de761 100644 --- a/tests/Feature/Api/ScreenTest.php +++ b/tests/Feature/Api/ScreenTest.php @@ -299,6 +299,13 @@ public function testUpdateDraftScreen() 'computed' => [['id' => 2, 'name' => 'test', 'type' => 'expression', 'formula' => 'test', 'property' => 'test']], 'custom_css' => '.class { font-size: large; }', 'watchers' => '[{"input_data":"{}","script_configuration":"{}","synchronous":false,"show_async_loading":false,"run_onload":false,"name":"Watchers","watching":"form_input_1","script":{"id":"script-4","uuid":"98a4376e-efb8-4c10-83fd-01199398eddc","key":null,"title":"Autosave","description":"Autosave","language":"php","code":"","timeout":60,"run_as_user_id":1,"status":"ACTIVE","script_category_id":"1","script_executor_id":3},"script_id":"4","script_key":null,"uid":"16794186944621"}]', + 'preview_presets' => [ + [ + 'title' => 'Empty contact', + // data stored as JSON string so empty objects ({}) survive PHP json_decode + 'data' => '{"email":"jane@example.com","contact":{}}', + ], + ], 'type' => $screen->type, 'screen_category_id' => $screen->screen_category_id, ]); @@ -313,6 +320,15 @@ public function testUpdateDraftScreen() $this->assertEquals($draft->computed, [['id' => 2, 'name' => 'test', 'type' => 'expression', 'formula' => 'test', 'property' => 'test']]); $this->assertEquals($draft->custom_css, '.class { font-size: large; }'); $this->assertEquals($draft->watchers, '[{"input_data":"{}","script_configuration":"{}","synchronous":false,"show_async_loading":false,"run_onload":false,"name":"Watchers","watching":"form_input_1","script":{"id":"script-4","uuid":"98a4376e-efb8-4c10-83fd-01199398eddc","key":null,"title":"Autosave","description":"Autosave","language":"php","code":"","timeout":60,"run_as_user_id":1,"status":"ACTIVE","script_category_id":"1","script_executor_id":3},"script_id":"4","script_key":null,"uid":"16794186944621"}]'); + $this->assertEquals( + [ + [ + 'title' => 'Empty contact', + 'data' => '{"email":"jane@example.com","contact":{}}', + ], + ], + $draft->preview_presets + ); $this->assertLessThan(3, $draft->updated_at->diffInSeconds($screen->updated_at)); } diff --git a/tests/Feature/Api/ScriptsTest.php b/tests/Feature/Api/ScriptsTest.php index 508196c364..d851cc0a42 100644 --- a/tests/Feature/Api/ScriptsTest.php +++ b/tests/Feature/Api/ScriptsTest.php @@ -29,6 +29,8 @@ class ScriptsTest extends TestCase { use RequestHelper; + public $withPermissions = true; + const API_TEST_SCRIPT = '/scripts'; const STRUCTURE = [ @@ -425,6 +427,178 @@ public function testPreviewScriptFail() }); } + /** + * Preview via Core Service Task in-process runner when allow_in_process is set. + */ + public function testPreviewScriptInProcess() + { + config(['core-service-task.enabled' => true]); + $this->withPersonalAccessClient(); + + Event::fake([ + ScriptResponseEvent::class, + ]); + + $script = $this->getScript('php'); + $script->allow_in_process = true; + $script->saveOrFail(); + + $url = route('api.scripts.preview', $script->id); + $response = $this->apiCall('POST', $url, [ + 'data' => '{"ping":"1"}', + 'code' => ' $data["ping"]];', + 'nonce' => 'in-process-nonce', + ]); + $response->assertStatus(200); + $response->assertJson(['status' => 'success', 'run_in_process' => true]); + + Event::assertDispatched(ScriptResponseEvent::class, function ($event) { + return ($event->response['output']['pong'] ?? null) === '1' + && $event->nonce === 'in-process-nonce'; + }); + } + + /** + * Explicit run_in_process request flag for preview (before saving allow_in_process). + */ + public function testPreviewScriptInProcessWithRequestFlag() + { + config(['core-service-task.enabled' => true]); + $this->withPersonalAccessClient(); + + Event::fake([ + ScriptResponseEvent::class, + ]); + + $script = $this->getScript('php'); + $script->allow_in_process = false; + $script->saveOrFail(); + + $url = route('api.scripts.preview', $script->id); + $response = $this->apiCall('POST', $url, [ + 'data' => '{}', + 'code' => ' "flag"];', + 'nonce' => 'flag-nonce', + 'run_in_process' => true, + ]); + $response->assertStatus(200); + $response->assertJson(['run_in_process' => true]); + + Event::assertDispatched(ScriptResponseEvent::class, function ($event) { + return ($event->response['output']['via'] ?? null) === 'flag' + && $event->nonce === 'flag-nonce'; + }); + } + + public function testAdminCanSelectModulesExecutionMode(): void + { + config([ + 'core-service-task.enabled' => true, + 'core-service-task.modules_enabled' => true, + ]); + + $script = $this->getScript('php'); + $response = $this->apiCall('PUT', route('api.scripts.update', $script), [ + 'title' => $script->title, + 'language' => 'php', + 'code' => $script->code, + 'description' => $script->description, + 'run_as_user_id' => $script->run_as_user_id, + 'script_executor_id' => $script->script_executor_id, + 'script_category_id' => $script->script_category_id, + 'allow_in_process' => true, + 'execution_mode' => Script::EXECUTION_MODE_MODULES, + ]); + + $response->assertNoContent(); + $this->assertSame( + Script::EXECUTION_MODE_MODULES, + $script->refresh()->execution_mode + ); + } + + public function testNonPrivilegedUserFallsBackToBareWhenSelectingModules(): void + { + $editor = User::factory()->create(['is_administrator' => false]); + $editor->giveDirectPermission('edit-scripts'); + $script = $this->getScript('php'); + + $response = $this->actingAs($editor, 'api')->putJson( + route('api.scripts.update', $script), + [ + 'title' => $script->title, + 'language' => 'php', + 'code' => $script->code, + 'description' => $script->description, + 'run_as_user_id' => $script->run_as_user_id, + 'script_executor_id' => $script->script_executor_id, + 'script_category_id' => $script->script_category_id, + 'allow_in_process' => true, + 'execution_mode' => Script::EXECUTION_MODE_MODULES, + ] + ); + + $response->assertNoContent(); + $this->assertSame( + Script::EXECUTION_MODE_BARE, + $script->refresh()->execution_mode + ); + } + + public function testNonPrivilegedUserEditingModulesScriptFallsBackToBare(): void + { + $editor = User::factory()->create(['is_administrator' => false]); + $editor->giveDirectPermission('edit-scripts'); + $editor->giveDirectPermission('create-scripts'); + $script = $this->getScript('php'); + $script->update([ + 'allow_in_process' => true, + 'execution_mode' => Script::EXECUTION_MODE_MODULES, + ]); + + $response = $this->actingAs($editor, 'api')->putJson( + route('api.scripts.update', $script), + [ + 'title' => 'Updated without modules', + 'language' => 'php', + 'code' => $script->code, + 'description' => $script->description, + 'run_as_user_id' => $script->run_as_user_id, + 'script_executor_id' => $script->script_executor_id, + 'script_category_id' => $script->script_category_id, + 'allow_in_process' => true, + ] + ); + + $response->assertNoContent(); + $this->assertSame( + Script::EXECUTION_MODE_BARE, + $script->refresh()->execution_mode + ); + } + + public function testNonPrivilegedUserDuplicatesModulesScriptAsBare(): void + { + $editor = User::factory()->create(['is_administrator' => false]); + $editor->giveDirectPermission('edit-scripts'); + $script = $this->getScript('php'); + $script->update([ + 'allow_in_process' => true, + 'execution_mode' => Script::EXECUTION_MODE_MODULES, + ]); + + $response = $this->actingAs($editor, 'api')->putJson( + route('api.scripts.duplicate', $script), + ['title' => 'Duplicate without modules'] + ); + + $response->assertCreated(); + $this->assertSame( + Script::EXECUTION_MODE_BARE, + Script::findOrFail($response->json('id'))->execution_mode + ); + } + /** * A user with only view-scripts must not be able to run arbitrary code through preview (FOUR-34242) */ diff --git a/tests/Feature/Api/TaskAssignmentPreviousOwnerTest.php b/tests/Feature/Api/TaskAssignmentPreviousOwnerTest.php index ea87216196..1852b96726 100644 --- a/tests/Feature/Api/TaskAssignmentPreviousOwnerTest.php +++ b/tests/Feature/Api/TaskAssignmentPreviousOwnerTest.php @@ -25,7 +25,7 @@ class TaskAssignmentPreviousOwnerTest extends TestCase protected $task; /** - * @var \ProcessMaker\Models\User + * @var User */ protected $assigned; @@ -109,10 +109,10 @@ public function testSingleUserAssignment() /** * Complete task * - * @param \ProcessMaker\Models\ProcessRequestToken $task + * @param ProcessRequestToken $task * @param array $data * - * @return \ProcessMaker\Models\ProcessRequestToken + * @return ProcessRequestToken */ private function completeTask(ProcessRequestToken $task, $data = []) { diff --git a/tests/Feature/Api/TaskLockAssignmentTest.php b/tests/Feature/Api/TaskLockAssignmentTest.php index c312e5c3aa..2abb4af07e 100644 --- a/tests/Feature/Api/TaskLockAssignmentTest.php +++ b/tests/Feature/Api/TaskLockAssignmentTest.php @@ -17,12 +17,12 @@ class TaskLockAssignmentTest extends TestCase use RequestHelper; /** - * @var \ProcessMaker\Models\User + * @var User */ protected $user1; /** - * @var \ProcessMaker\Models\User + * @var User */ protected $user2; @@ -160,10 +160,10 @@ public function testWithNoLockAssignment() /** * Complete task * - * @param \ProcessMaker\Models\ProcessRequestToken $task + * @param ProcessRequestToken $task * @param array $data * - * @return \ProcessMaker\Models\ProcessRequestToken + * @return ProcessRequestToken */ private function completeTask(ProcessRequestToken $task, $data = []) { diff --git a/tests/Feature/Api/V1_1/CaseUtilsTest.php b/tests/Feature/Api/V1_1/CaseUtilsTest.php index b8be5d38a5..d0f3df3a60 100644 --- a/tests/Feature/Api/V1_1/CaseUtilsTest.php +++ b/tests/Feature/Api/V1_1/CaseUtilsTest.php @@ -123,8 +123,8 @@ public function test_store_participants_with_empty_data() public function test_extract_data_process() { - $object = (object)[ - 'process' => (object)[ + $object = (object) [ + 'process' => (object) [ 'id' => 1, 'name' => 'Process 1', ], @@ -138,11 +138,11 @@ public function test_extract_data_process() public function test_extract_data_request() { - $object = (object)[ - 'processRequest' => (object)[ + $object = (object) [ + 'processRequest' => (object) [ 'id' => 1, 'name' => 'Request 1', - 'parentRequest' => (object)[ + 'parentRequest' => (object) [ 'id' => 2, ], ], @@ -157,7 +157,7 @@ public function test_extract_data_request() public function test_extract_data_task() { - $object = (object)[ + $object = (object) [ 'id' => 1, 'element_id' => 101, 'element_name' => 'Task 1', diff --git a/tests/Feature/Cases/CaseSyncRepositoryTest.php b/tests/Feature/Cases/CaseSyncRepositoryTest.php index c91cd2b4fa..7eef39f43f 100644 --- a/tests/Feature/Cases/CaseSyncRepositoryTest.php +++ b/tests/Feature/Cases/CaseSyncRepositoryTest.php @@ -2,11 +2,11 @@ namespace Tests\Feature\Cases; -use ProcessMaker\Repositories\CaseSyncRepository; +use Illuminate\Support\Facades\DB; use ProcessMaker\Models\ProcessRequest; use ProcessMaker\Models\ProcessRequestToken; +use ProcessMaker\Repositories\CaseSyncRepository; use Tests\TestCase; -use Illuminate\Support\Facades\DB; class CaseSyncRepositoryTest extends TestCase { @@ -24,7 +24,7 @@ public function testSyncCases() 'status' => 'ACTIVE', 'element_id' => 'task-1', 'process_request_id' => $processRequest1->id, - 'process_id' => $processRequest1->process_id + 'process_id' => $processRequest1->process_id, ])->toArray(), ProcessRequestToken::factory()->make([ 'element_name' => 'Task 2', @@ -32,7 +32,7 @@ public function testSyncCases() 'status' => 'CLOSED', 'element_id' => 'task-2', 'process_request_id' => $processRequest1->id, - 'process_id' => $processRequest1->process_id + 'process_id' => $processRequest1->process_id, ])->toArray(), ProcessRequestToken::factory()->make([ 'element_name' => 'Task 3', @@ -40,7 +40,7 @@ public function testSyncCases() 'status' => 'ACTIVE', 'element_id' => 'call-activity-1', 'process_request_id' => $processRequest1->id, - 'process_id' => $processRequest1->process_id + 'process_id' => $processRequest1->process_id, ])->toArray(), ]); @@ -51,7 +51,7 @@ public function testSyncCases() 'status' => 'CLOSED', 'element_id' => 'task-4', 'process_request_id' => $processRequest2->id, - 'process_id' => $processRequest2->process_id + 'process_id' => $processRequest2->process_id, ])->toArray(), ProcessRequestToken::factory()->make([ 'element_name' => 'Task 5', @@ -59,7 +59,7 @@ public function testSyncCases() 'status' => 'ACTIVE', 'element_id' => 'task-5', 'process_request_id' => $processRequest2->id, - 'process_id' => $processRequest2->process_id + 'process_id' => $processRequest2->process_id, ])->toArray(), ]); @@ -85,21 +85,21 @@ public function testSyncCases() 'name' => 'Task 5', 'status' => 'ACTIVE', 'element_id' => 'task-5', - 'process_id' => $processRequest2->process_id + 'process_id' => $processRequest2->process_id, ], [ 'id' => $processRequest1->tokens()->where('element_id', 'task-2')->first()->id, 'name' => 'Task 2', 'status' => 'CLOSED', 'element_id' => 'task-2', - 'process_id' => $processRequest1->process_id + 'process_id' => $processRequest1->process_id, ], [ 'id' => $processRequest1->tokens()->where('element_id', 'task-1')->first()->id, 'name' => 'Task 1', 'status' => 'ACTIVE', 'element_id' => 'task-1', - 'process_id' => $processRequest1->process_id + 'process_id' => $processRequest1->process_id, ], ]; $expectedParticipants = [ diff --git a/tests/Feature/ConditionalRedirect/ConditionalRedirectTest.php b/tests/Feature/ConditionalRedirect/ConditionalRedirectTest.php index e1a5384209..0e01ff91a0 100644 --- a/tests/Feature/ConditionalRedirect/ConditionalRedirectTest.php +++ b/tests/Feature/ConditionalRedirect/ConditionalRedirectTest.php @@ -174,7 +174,7 @@ private function conditionalRedirectTest(array $requestData, array $expectedElem [ 'tokenId' => $tasks[0]['id'], 'nodeId' => $tasks[0]['element_id'], - ] + ], ], ]; $dispatched[] = $expectedEvent; @@ -203,7 +203,7 @@ private function conditionalRedirectTest(array $requestData, array $expectedElem 'params' => [ [ 'tokenId' => $tasks[0]['id'], - ] + ], ], ]; $dispatched[] = $expectedEvent; @@ -218,6 +218,7 @@ private function getActiveTasks() //Get the active tasks of the request $route = route('api.tasks.index', ['status' => 'ACTIVE']); $response = $this->apiCall('GET', $route); + return $response->json('data'); } @@ -225,6 +226,7 @@ private function getTaskById($id) { $route = route('api.tasks.show', [$id, 'include' => 'data,elementDestination']); $response = $this->apiCall('GET', $route); + return $response->json(); } } diff --git a/tests/Feature/Console/DuplicateProcessRequestTest.php b/tests/Feature/Console/DuplicateProcessRequestTest.php new file mode 100644 index 0000000000..1323cd4f3e --- /dev/null +++ b/tests/Feature/Console/DuplicateProcessRequestTest.php @@ -0,0 +1,59 @@ +create([ + 'data' => ['customer' => 'Acme'], + ]); + $tokens = ProcessRequestToken::factory()->count(2)->create([ + 'process_id' => $request->process_id, + 'process_request_id' => $request->id, + ]); + + $this->artisan("processmaker:duplicate-request {$request->id} 2") + ->expectsOutputToContain('Created 2 request duplicate(s):') + ->assertExitCode(0); + + $duplicates = ProcessRequest::query() + ->where('process_id', $request->process_id) + ->where('id', '!=', $request->id) + ->get(); + + $this->assertCount(2, $duplicates); + + foreach ($duplicates as $duplicate) { + $this->assertSame('Acme', $duplicate->data['customer']); + $this->assertCount(2, $duplicate->tokens); + $this->assertNotSame($request->id, $duplicate->id); + $this->assertNotSame($request->uuid, $duplicate->uuid); + $this->assertSame( + $tokens->pluck('element_id')->sort()->values()->all(), + $duplicate->tokens->pluck('element_id')->sort()->values()->all() + ); + } + } + + public function testItFailsWhenTheRequestDoesNotExist(): void + { + $this->artisan('processmaker:duplicate-request 999999 1') + ->expectsOutput('The specified request does not exist.') + ->assertExitCode(1); + } + + public function testItFailsWhenTheNumberOfCopiesIsInvalid(): void + { + $request = ProcessRequest::factory()->create(); + + $this->artisan("processmaker:duplicate-request {$request->id} 0") + ->expectsOutput('The number of copies must be a positive integer.') + ->assertExitCode(1); + } +} diff --git a/tests/Feature/Console/PluginReloadListenCommandTest.php b/tests/Feature/Console/PluginReloadListenCommandTest.php new file mode 100644 index 0000000000..7f00aa5339 --- /dev/null +++ b/tests/Feature/Console/PluginReloadListenCommandTest.php @@ -0,0 +1,189 @@ +previousSocketTimeout = (string) ini_get('default_socket_timeout'); + $this->stateFile = sys_get_temp_dir() . '/pm-plugin-reload-' . uniqid(); + config([ + 'plugin_manager.supervisorctl_bin' => 'supervisorctl', + 'plugin_manager.supervisorctl_config' => null, + 'plugin_manager.supervisor_restart_target' => 'all', + 'plugin_manager.supervisor_restart_delay' => 5, + 'plugin_manager.reload_channel' => 'plugin-runtime-reload', + 'plugin_manager.reload_version_key' => 'plugin-runtime-reload:version', + 'plugin_manager.reload_state_file' => $this->stateFile, + 'plugin_manager.reload_reconnect_delay' => 0, + 'plugin_manager.reload_listen_max_cycles' => 1, + ]); + } + + protected function tearDown(): void + { + ini_set('default_socket_timeout', $this->previousSocketTimeout); + + if (is_file($this->stateFile)) { + unlink($this->stateFile); + } + + parent::tearDown(); + } + + private function expectSubscriptionClient(): void + { + $client = \Mockery::mock(); + $client->shouldReceive('setOption') + ->with(\Redis::OPT_READ_TIMEOUT, -1) + ->andReturnTrue(); + + $connection = \Mockery::mock(); + $connection->shouldReceive('client')->andReturn($client); + + Redis::shouldReceive('connection')->andReturn($connection); + } + + public function testListenerCatchesUpWhenAReloadWasMissed(): void + { + Process::fake([ + '*' => Process::result('', exitCode: 0), + ]); + $this->expectSubscriptionClient(); + Redis::shouldReceive('get') + ->once() + ->with('plugin-runtime-reload:version') + ->andReturn('4'); + Redis::shouldReceive('subscribe') + ->once() + ->with(['test:plugin-runtime-reload'], \Mockery::type('Closure')); + + $this->artisan('plugin:reload-listen')->assertOk(); + + $this->assertSame('4', file_get_contents($this->stateFile)); + Process::assertRan(function ($process) { + $command = is_array($process->command) ? implode(' ', $process->command) : (string) $process->command; + + return str_contains($command, 'setsid') + && str_contains($command, 'supervisorctl') + && str_contains($command, 'restart') + && str_contains($command, 'all'); + }); + } + + public function testListenerSchedulesRestartForANewMessageAndSkipsTheSameVersion(): void + { + Process::fake([ + '*' => Process::result('', exitCode: 0), + ]); + file_put_contents($this->stateFile, '2'); + + $this->expectSubscriptionClient(); + Redis::shouldReceive('get')->once()->andReturn('2'); + Redis::shouldReceive('subscribe') + ->once() + ->andReturnUsing(function (array $channels, \Closure $callback): void { + $this->assertSame(['test:plugin-runtime-reload'], $channels); + $callback(json_encode(['version' => 3, 'plugin' => 'package-plg']), 'plugin-runtime-reload'); + $callback(json_encode(['version' => 3, 'plugin' => 'package-plg']), 'plugin-runtime-reload'); + }); + + $this->artisan('plugin:reload-listen')->assertOk(); + + $this->assertSame('3', file_get_contents($this->stateFile)); + Process::assertRanTimes(function ($process) { + $command = is_array($process->command) ? implode(' ', $process->command) : (string) $process->command; + + return str_contains($command, 'supervisorctl'); + }, 1); + } + + public function testPublishStoresTheVersionAndNotifiesTheChannel(): void + { + Redis::shouldReceive('incr') + ->once() + ->with('plugin-runtime-reload:version') + ->andReturn(7); + Redis::shouldReceive('publish') + ->once() + ->withArgs(function (string $channel, string $message): bool { + $payload = json_decode($message, true); + + return $channel === 'test:plugin-runtime-reload' + && $payload['version'] === 7 + && $payload['plugin'] === 'package-plg'; + }) + ->andReturn(1); + + $version = (new PluginRuntimeReload())->publish('package-plg'); + + $this->assertSame(7, $version); + } + + public function testChannelAndVersionKeyIncludeRedisAndTenantPrefixes(): void + { + config([ + 'database.redis.options.prefix' => 'instance:', + 'plugin_manager.reload_channel' => 'plugin-runtime-reload', + 'plugin_manager.reload_version_key' => 'plugin-runtime-reload:version', + 'plugin_manager.reload_state_file' => '/tmp/processmaker-plugin-reload-version', + ]); + app()->instance('currentTenant', new class { + public function getKey(): int + { + return 9; + } + }); + + $reload = new PluginRuntimeReload(); + + $this->assertSame('instance:tenant_9:plugin-runtime-reload', $reload->channel()); + $this->assertSame('tenant_9:plugin-runtime-reload:version', $reload->versionKey()); + $this->assertSame('/tmp/processmaker-plugin-reload-version-9', $reload->stateFile()); + } + + public function testListenerReconnectsAfterRedisReadError(): void + { + config(['plugin_manager.reload_listen_max_cycles' => 2]); + Process::fake([ + '*' => Process::result('', exitCode: 0), + ]); + file_put_contents($this->stateFile, '4'); + + $this->expectSubscriptionClient(); + Redis::shouldReceive('get')->twice()->andReturn('4', '5'); + Redis::shouldReceive('purge')->once()->with('default'); + Redis::shouldReceive('subscribe') + ->twice() + ->andReturnUsing(function (): void { + static $attempt = 0; + $attempt++; + if ($attempt === 1) { + throw new \RedisException('read error on connection to 127.0.0.1:6379'); + } + }); + + $this->artisan('plugin:reload-listen') + ->expectsOutputToContain('Reconnecting.') + ->assertOk(); + + $this->assertSame('5', file_get_contents($this->stateFile)); + Process::assertRanTimes(function ($process) { + $command = is_array($process->command) ? implode(' ', $process->command) : (string) $process->command; + + return str_contains($command, 'supervisorctl'); + }, 1); + } +} diff --git a/tests/Feature/EditDataTest.php b/tests/Feature/EditDataTest.php index 1c2011fb1b..1c3b618b34 100644 --- a/tests/Feature/EditDataTest.php +++ b/tests/Feature/EditDataTest.php @@ -103,7 +103,7 @@ private function addProcessPermission(Process $process, array $users, array $gro * * @param User $userAssigned * - * @return \ProcessMaker\Models\Process + * @return Process */ private function createSingleTaskProcessUserAssignment(User $userAssigned) { @@ -132,7 +132,7 @@ private function createSingleTaskProcessUserAssignment(User $userAssigned) /** * Start a process * - * @param \ProcessMaker\Models\Process $process + * @param Process $process * @param string $startEvent * @param array $data * @@ -149,10 +149,10 @@ private function startProcess($process, $startEvent, $data = []) /** * Complete task * - * @param \ProcessMaker\Models\ProcessRequestToken $task + * @param ProcessRequestToken $task * @param array $data * - * @return \ProcessMaker\Models\ProcessRequestToken + * @return ProcessRequestToken */ private function completeTask(ProcessRequestToken $task, $data = []) { diff --git a/tests/Feature/Http/Middleware/TrustHostsTest.php b/tests/Feature/Http/Middleware/TrustHostsTest.php index 8e896191f3..3a3955b8aa 100644 --- a/tests/Feature/Http/Middleware/TrustHostsTest.php +++ b/tests/Feature/Http/Middleware/TrustHostsTest.php @@ -2,10 +2,10 @@ namespace Tests\Feature\Http\Middleware; -use Tests\TestCase; -use ProcessMaker\Http\Middleware\TrustHosts; use Illuminate\Http\Request; use Illuminate\Support\Facades\Config; +use ProcessMaker\Http\Middleware\TrustHosts; +use Tests\TestCase; class TrustHostsTest extends TestCase { @@ -108,11 +108,11 @@ public function test_invalid_trusted_host_with_port_still_rejected() public function test_missing_forwarded_host() { $request = Request::create('https://example.processmaker.net'); - + $response = $this->middleware->handle($request, function ($req) { return response()->json(['status' => 'success']); }); $this->assertEquals(200, $response->status()); } -} \ No newline at end of file +} diff --git a/tests/Feature/ImportExport/Exporters/ProcessExporterWithScreenDataSourceTest.php b/tests/Feature/ImportExport/Exporters/ProcessExporterWithScreenDataSourceTest.php index f02fc39898..41a2657de5 100644 --- a/tests/Feature/ImportExport/Exporters/ProcessExporterWithScreenDataSourceTest.php +++ b/tests/Feature/ImportExport/Exporters/ProcessExporterWithScreenDataSourceTest.php @@ -24,7 +24,7 @@ public function testProcessExportWithScreenDataSource() // Create a script first to consume some IDs $script = Script::factory()->create(['title' => 'A Script']); - + $category = new $dataSourceCategoryClass(); $category->name = 'Test Category ' . uniqid(); $category->save(); @@ -68,12 +68,12 @@ public function testProcessExportWithScreenDataSource() $process = Process::factory()->create([ 'name' => 'Process with screen using data source', - 'bpmn' => $bpmn + 'bpmn' => $bpmn, ]); $exportJob = new ExportProcess($process); $exportJob->handle(); - + // Access private package property via reflection $reflection = new \ReflectionClass($exportJob); $property = $reflection->getProperty('package'); @@ -90,7 +90,7 @@ public function testProcessExportWithScreenDataSource() $this->assertNotEquals('data_source-' . $dataSource->id, $script['id']); $this->assertNotEquals($dataSource->id, $script['id']); // Also check that it's not present as a numeric ID if it's the same - if (is_numeric($script['id']) && (int)$script['id'] === $dataSource->id) { + if (is_numeric($script['id']) && (int) $script['id'] === $dataSource->id) { $this->fail('Data source ID ' . $dataSource->id . ' found in scripts section'); } } diff --git a/tests/Feature/ImportExport/Exporters/ScriptExporterTest.php b/tests/Feature/ImportExport/Exporters/ScriptExporterTest.php index 437eb5e912..0cc411ead6 100644 --- a/tests/Feature/ImportExport/Exporters/ScriptExporterTest.php +++ b/tests/Feature/ImportExport/Exporters/ScriptExporterTest.php @@ -62,7 +62,7 @@ public function testExportUncategorized() { DB::beginTransaction(); (new CategorySystemSeeder)->run(); - $uncategorizedCategory = \ProcessMaker\Models\ScriptCategory::first(); + $uncategorizedCategory = ScriptCategory::first(); $script = Script::factory()->create(['title' => 'test']); $script->categories()->sync([$uncategorizedCategory->id]); @@ -70,7 +70,7 @@ public function testExportUncategorized() DB::rollBack(); // Delete all created items since DB::beginTransaction (new CategorySystemSeeder)->run(); - $existingUncategorizedCategory = \ProcessMaker\Models\ScriptCategory::first(); + $existingUncategorizedCategory = ScriptCategory::first(); $existingUuid = $existingUncategorizedCategory->uuid; $this->import($payload); @@ -79,6 +79,46 @@ public function testExportUncategorized() $this->assertEquals($script->categories->first()->uuid, $existingUuid); } + public function testImportDowngradesModulesForUnauthorizedImportingUser(): void + { + $script = Script::factory()->create([ + 'title' => 'modules script', + 'language' => 'php', + 'allow_in_process' => true, + 'execution_mode' => Script::EXECUTION_MODE_MODULES, + ]); + $payload = $this->export($script, ScriptExporter::class); + $user = User::factory()->create(['is_administrator' => false]); + $options = new Options([ + $script->uuid => ['mode' => 'copy'], + ], $user->id); + + $this->import($payload, $options); + + $copy = Script::where('title', 'modules script 2')->firstOrFail(); + $this->assertSame(Script::EXECUTION_MODE_BARE, $copy->execution_mode); + } + + public function testImportPreservesModulesForAuthorizedImportingUser(): void + { + $script = Script::factory()->create([ + 'title' => 'authorized modules script', + 'language' => 'php', + 'allow_in_process' => true, + 'execution_mode' => Script::EXECUTION_MODE_MODULES, + ]); + $payload = $this->export($script, ScriptExporter::class); + $admin = User::where('is_administrator', true)->firstOrFail(); + $options = new Options([ + $script->uuid => ['mode' => 'copy'], + ], $admin->id); + + $this->import($payload, $options); + + $copy = Script::where('title', 'authorized modules script 2')->firstOrFail(); + $this->assertSame(Script::EXECUTION_MODE_MODULES, $copy->execution_mode); + } + public function testHiddenUsesParentMode() { $scriptCategory1 = ScriptCategory::factory()->create(['name' => 'test category A']); diff --git a/tests/Feature/ImportExport/Exporters/ScriptsInScreenDataSourceTest.php b/tests/Feature/ImportExport/Exporters/ScriptsInScreenDataSourceTest.php index f22ac082ce..e3ea31bf90 100644 --- a/tests/Feature/ImportExport/Exporters/ScriptsInScreenDataSourceTest.php +++ b/tests/Feature/ImportExport/Exporters/ScriptsInScreenDataSourceTest.php @@ -4,9 +4,9 @@ use ProcessMaker\Assets\DataSourcesInScreen; use ProcessMaker\Assets\ScriptsInScreen; +use ProcessMaker\Jobs\ExportScreen; use ProcessMaker\Models\Screen; use ProcessMaker\Models\Script; -use ProcessMaker\Jobs\ExportScreen; use Tests\TestCase; class ScriptsInScreenDataSourceTest extends TestCase @@ -101,7 +101,7 @@ public function testExportScreenIncludesDataSources() $exportJob = new ExportScreen($screen); $exportJob->handle(); - + // Access private package property via reflection $reflection = new \ReflectionClass($exportJob); // package is protected in ExportProcess diff --git a/tests/Feature/InterstitialTest.php b/tests/Feature/InterstitialTest.php index 5afb765ace..9545bb8a7e 100644 --- a/tests/Feature/InterstitialTest.php +++ b/tests/Feature/InterstitialTest.php @@ -45,6 +45,7 @@ private function loadProcess($file) { $data = []; $data['bpmn'] = Process::getProcessTemplate($file); + return Process::factory()->create($data); } @@ -56,6 +57,7 @@ private function importProcessPackage(string $file) $maxId = Process::max('id') ?? 0; $data = json_decode(file_get_contents(base_path('tests/Fixtures/' . $file)), true); $this->import($data); + return Process::where('id', '>', $maxId)->get(); } @@ -63,6 +65,7 @@ private function startProcess(Process $process, string $event, array $data = []) { $route = route('api.process_events.trigger', [$process->id, 'event' => $event]); $response = $this->apiCall('POST', $route, $data); + return $response->json(); } @@ -71,8 +74,10 @@ private function getActiveTasks() //Get the active tasks of the request $route = route('api.tasks.index', ['status' => 'ACTIVE']); $response = $this->apiCall('GET', $route); + return $response->json('data'); } + private function completeActiveTask(array $data = []) { //Get the active tasks of the request @@ -80,6 +85,7 @@ private function completeActiveTask(array $data = []) //Complete the task $route = route('api.tasks.update', [$tasks[0]['id'], 'status' => 'COMPLETED']); $response = $this->apiCall('PUT', $route, ['data' => $data]); + return $response->json(); } @@ -91,6 +97,7 @@ private function broadcastedTo(RedirectToEvent $event, array $expectedChannels): return false; } } + return true; } @@ -118,7 +125,7 @@ public function testExecuteProcessRedirects() [ 'tokenId' => $tasks[0]['id'], 'nodeId' => $tasks[0]['element_id'], - ] + ], ], ]; $dispatched[] = $expectedEvent; @@ -136,14 +143,13 @@ public function testExecuteProcessRedirects() 'params' => [ [ 'tokenId' => $tasks[0]['id'], - ] + ], ], ]; $dispatched[] = $expectedEvent; Event::assertDispatched(RedirectToEvent::class, function ($event) use ($expectedEvent) { return $event->method === $expectedEvent['method'] && $event->params[0]['tokenId'] === $expectedEvent['params'][0]['tokenId']; - }); // Complete active task (sub process - Task 2) and check RedirectToEvent dispatched to next Task not to subprocess summary @@ -154,12 +160,12 @@ public function testExecuteProcessRedirects() 'params' => [ [ 'tokenId' => $tasks[0]['id'], - ] + ], ], 'broadcastTo' => [ 'private-ProcessMaker.Models.ProcessRequest.' . $tasks[0]['process_request_id'], // active task: parent request 'private-ProcessMaker.Models.ProcessRequest.' . $task['process_request_id'], // completed task: child request - ] + ], ]; $dispatched[] = $expectedEvent; Event::assertDispatched(RedirectToEvent::class, function ($event) use ($expectedEvent, $task) { diff --git a/tests/Feature/Jobs/DevLinkInstallTest.php b/tests/Feature/Jobs/DevLinkInstallTest.php index 67cc45dd66..b08919a0da 100644 --- a/tests/Feature/Jobs/DevLinkInstallTest.php +++ b/tests/Feature/Jobs/DevLinkInstallTest.php @@ -6,8 +6,8 @@ use Illuminate\Support\Facades\Event; use Illuminate\Support\Facades\Storage; use Mockery; -use ProcessMaker\Exception\DevLinkRemoteBundleException; use ProcessMaker\Events\ImportLog; +use ProcessMaker\Exception\DevLinkRemoteBundleException; use ProcessMaker\Jobs\DevLinkInstall; use ProcessMaker\Jobs\ImportV2; use ProcessMaker\Models\Bundle; diff --git a/tests/Feature/LoginRedirectTest.php b/tests/Feature/LoginRedirectTest.php index eb3b0800b2..4c80492c40 100644 --- a/tests/Feature/LoginRedirectTest.php +++ b/tests/Feature/LoginRedirectTest.php @@ -9,7 +9,6 @@ class LoginRedirectTest extends TestCase { /** - * * Test to verify that the login page redirects to the about page */ public function testLoginRedirect() @@ -25,7 +24,7 @@ public function testLoginRedirect() // Verify it redirects to the login page $response->assertRedirect('/login'); - // When we try to open the about page, we should be redirected to the login page + // When we try to open the about page, we should be redirected to the login page $response = $this->get(route('about.index')); $response->assertRedirect('/login'); @@ -50,7 +49,7 @@ public function testLoginRedirectWithDevtoolsOpeningMapFile() // Verify it redirects to the login page $response->assertRedirect('/login'); - // When we try to open the about page, we should be redirected to the login page + // When we try to open the about page, we should be redirected to the login page $response = $this->get(route('about.index')); $response->assertRedirect('/login'); diff --git a/tests/Feature/Mcp/DataConnectorsCollectionRecordsTest.php b/tests/Feature/Mcp/DataConnectorsCollectionRecordsTest.php new file mode 100644 index 0000000000..df0c9882d7 --- /dev/null +++ b/tests/Feature/Mcp/DataConnectorsCollectionRecordsTest.php @@ -0,0 +1,84 @@ + + */ + protected $connectionsToTransact = []; + + protected function setUp(): void + { + parent::setUp(); + + $this->user = User::factory()->create([ + 'password' => Hash::make('password'), + 'is_administrator' => true, + ]); + + if (!PackageHelper::isPackageInstalled(PackageHelper::PM_PACKAGE_COLLECTIONS)) { + $this->markTestSkipped('package-collections is not installed'); + } + } + + public function testAdminCanSeedCollectionRecords(): void + { + ProcessCategory::updateOrCreate( + ['name' => 'System'], + ['status' => 'ACTIVE', 'is_system' => true], + ); + + $createScreen = Screen::factory()->create(['title' => 'MCP Rec Create', 'type' => 'FORM', 'config' => [['items' => []]]]); + $readScreen = Screen::factory()->create(['title' => 'MCP Rec Read', 'type' => 'DISPLAY', 'config' => [['items' => []]]]); + $updateScreen = Screen::factory()->create(['title' => 'MCP Rec Update', 'type' => 'FORM', 'config' => [['items' => []]]]); + + $created = DataConnectorsServer::actingAs($this->user)->tool(CreateCollectionTool::class, [ + 'name' => 'MCP Records Collection', + 'description' => 'For record seeding', + 'create_screen_id' => $createScreen->id, + 'read_screen_id' => $readScreen->id, + 'update_screen_id' => $updateScreen->id, + ]); + $created->assertOk()->assertHasNoErrors()->assertSee('MCP Records Collection'); + + $collectionId = Collection::where('name', 'MCP Records Collection')->value('id'); + $this->assertNotNull($collectionId); + + DataConnectorsServer::actingAs($this->user)->tool(CreateCollectionRecordsTool::class, [ + 'collection_id' => $collectionId, + 'records' => [ + ['first_name' => 'Ada', 'last_name' => 'Lovelace'], + ['first_name' => 'Grace', 'last_name' => 'Hopper'], + ], + ])->assertOk() + ->assertStructuredContent(fn (AssertableJson $json) => $json + ->where('created_count', 2) + ->has('data', 2) + ->etc()); + + DataConnectorsServer::actingAs($this->user)->tool(ListCollectionRecordsTool::class, [ + 'collection_id' => $collectionId, + ])->assertOk()->assertSee('Ada'); + } +} diff --git a/tests/Feature/Mcp/DataConnectorsServerTest.php b/tests/Feature/Mcp/DataConnectorsServerTest.php new file mode 100644 index 0000000000..bed8616ff7 --- /dev/null +++ b/tests/Feature/Mcp/DataConnectorsServerTest.php @@ -0,0 +1,269 @@ +user = User::factory()->create([ + 'password' => Hash::make('password'), + 'is_administrator' => true, + ]); + + if (!PackageHelper::isPackageInstalled(PackageHelper::PM_PACKAGE_DATA_SOURCES)) { + $this->markTestSkipped('package-data-sources is not installed'); + } + if (!PackageHelper::isPackageInstalled(PackageHelper::PM_PACKAGE_COLLECTIONS)) { + $this->markTestSkipped('package-collections is not installed'); + } + } + + public function testAdminCanCreateListAndUpdateADataConnector(): void + { + $category = DataSourceCategory::factory()->create([ + 'status' => 'ACTIVE', + 'is_system' => false, + ]); + + $created = DataConnectorsServer::actingAs($this->user)->tool(CreateDataConnectorTool::class, [ + 'name' => 'MCP Demo API', + 'description' => 'Demo connector', + 'type' => 'REST', + 'authtype' => 'NONE', + 'data_source_category_id' => (string) $category->id, + 'api_base_url' => 'https://example.test', + 'endpoints' => [ + 'listItems' => [ + 'purpose' => 'listItems', + 'method' => 'GET', + 'url' => '{{__api_base_url__}}/items', + ], + ], + ]); + + $created->assertOk() + ->assertHasNoErrors() + ->assertSee('MCP Demo API') + ->assertStructuredContent(fn (AssertableJson $json) => $json + ->where('name', 'MCP Demo API') + ->where('type', 'REST') + ->where('authtype', 'NONE') + ->has('endpoints.listItems') + ->etc()); + + $connector = DataSource::where('name', 'MCP Demo API')->firstOrFail(); + + DataConnectorsServer::actingAs($this->user)->tool(ListDataConnectorsTool::class, [ + 'filter' => 'MCP Demo', + ])->assertOk()->assertSee('MCP Demo API'); + + DataConnectorsServer::actingAs($this->user)->tool(ListDataConnectorCategoriesTool::class, [ + 'filter' => $category->name, + ])->assertOk()->assertSee($category->name); + + DataConnectorsServer::actingAs($this->user)->tool(GetDataConnectorTool::class, [ + 'id' => $connector->id, + ])->assertOk()->assertSee('listItems'); + + DataConnectorsServer::actingAs($this->user)->tool(UpdateDataConnectorTool::class, [ + 'id' => $connector->id, + 'description' => 'Updated demo connector', + ])->assertOk()->assertSee('Updated demo connector'); + + $this->assertSame('Updated demo connector', $connector->refresh()->description); + } + + public function testAdminCanCreateListAndUpdateACollection(): void + { + ProcessCategory::factory()->create([ + 'name' => 'System', + 'status' => 'ACTIVE', + 'is_system' => true, + ]); + + $createScreen = Screen::factory()->create(['title' => 'MCP Create', 'type' => 'FORM', 'config' => [['items' => []]]]); + $readScreen = Screen::factory()->create(['title' => 'MCP Read', 'type' => 'DISPLAY', 'config' => [['items' => []]]]); + $updateScreen = Screen::factory()->create(['title' => 'MCP Update', 'type' => 'FORM', 'config' => [['items' => []]]]); + + $created = DataConnectorsServer::actingAs($this->user)->tool(CreateCollectionTool::class, [ + 'name' => 'MCP Demo Collection', + 'description' => 'Collection for MCP tests', + 'create_screen_id' => $createScreen->id, + 'read_screen_id' => $readScreen->id, + 'update_screen_id' => $updateScreen->id, + ]); + + $created->assertOk() + ->assertHasNoErrors() + ->assertSee('MCP Demo Collection'); + + $collection = Collection::where('name', 'MCP Demo Collection')->firstOrFail(); + + DataConnectorsServer::actingAs($this->user)->tool(ListCollectionsTool::class, [ + 'filter' => 'MCP Demo Collection', + ])->assertOk()->assertSee('MCP Demo Collection'); + + DataConnectorsServer::actingAs($this->user)->tool(GetCollectionTool::class, [ + 'id' => $collection->id, + ])->assertOk()->assertSee('Collection for MCP tests'); + + DataConnectorsServer::actingAs($this->user)->tool(UpdateCollectionTool::class, [ + 'id' => $collection->id, + 'description' => 'Updated collection description', + ])->assertOk()->assertSee('Updated collection description'); + } + + public function testDataConnectorToolsRequirePermission(): void + { + DataConnectorsServer::tool(ListDataConnectorsTool::class) + ->assertHasErrors(['Authenticate with a ProcessMaker API token']); + + $member = User::factory()->create([ + 'is_administrator' => false, + ]); + + DataConnectorsServer::actingAs($member)->tool(ListDataConnectorsTool::class) + ->assertHasErrors(['view-data-sources']); + } + + public function testCreateRejectsEndpointWithoutPurposeKeyMatch(): void + { + $category = DataSourceCategory::factory()->create([ + 'status' => 'ACTIVE', + 'is_system' => false, + ]); + + DataConnectorsServer::actingAs($this->user)->tool(CreateDataConnectorTool::class, [ + 'name' => 'Bad Endpoint Map', + 'type' => 'REST', + 'authtype' => 'NONE', + 'data_source_category_id' => (string) $category->id, + 'endpoints' => [ + 'wrongKey' => [ + 'purpose' => 'listItems', + 'method' => 'GET', + 'url' => 'https://example.test/items', + ], + ], + ])->assertHasErrors(['map key must equal purpose']); + } + + public function testServerInstructionsIncludeDataConnectorCatalogWithoutSkillBodies(): void + { + $this->seedMcpGuidance(); + + $server = app(DataConnectorsServer::class, ['transport' => new FakeTransporter()]); + $instructions = $server->createContext()->instructions; + + $this->assertStringContainsString('get-mcp-guidance', $instructions); + $this->assertStringContainsString('create-data-connector', $instructions); + $this->assertStringContainsString('Prefer clear connector names', $instructions); + $this->assertStringContainsString('connector-naming', $instructions); + $this->assertStringNotContainsString('Name connectors by target system', $instructions); + $this->assertStringNotContainsString('screen-styles', $instructions); + $this->assertStringNotContainsString('process-naming', $instructions); + } + + public function testSkillResourcesAreDataConnectorScoped(): void + { + $this->seedMcpGuidance(); + + $server = app(DataConnectorsServer::class, ['transport' => new FakeTransporter()]); + $resources = $server->createContext()->resources(); + + $this->assertCount(1, $resources); + $this->assertInstanceOf(McpSkillResource::class, $resources->first()); + $this->assertSame('pm://mcp/skills/connector-naming', $resources->first()->uri()); + } + + public function testGetMcpGuidanceReturnsScopedCatalog(): void + { + $this->seedMcpGuidance(); + + DataConnectorsServer::actingAs($this->user) + ->tool(GetMcpGuidanceTool::class) + ->assertOk() + ->assertStructuredContent(fn (AssertableJson $json) => $json + ->where('system_prompt', 'Prefer clear connector names') + ->has('skills', 1) + ->where('skills.0.slug', 'connector-naming') + ->missing('skills.0.content') + ->etc()); + } + + private function seedMcpGuidance(): void + { + Setting::updateOrCreate(['key' => McpGuidance::SYSTEM_PROMPT_KEY], [ + 'format' => 'textarea', + 'config' => 'Prefer clear connector names', + 'name' => 'System Prompt', + 'group' => 'MCP', + ]); + + Setting::updateOrCreate(['key' => McpGuidance::SKILLS_KEY], [ + 'format' => 'component', + 'config' => [ + [ + 'id' => 'custom-screen-styles', + 'name' => 'Screen styles', + 'slug' => 'screen-styles', + 'when' => 'Before create-screen when styling matters', + 'content' => 'Use calm colors', + 'scope' => McpGuidance::SCOPE_SCREENS, + ], + [ + 'id' => 'custom-process-naming', + 'name' => 'Process naming', + 'slug' => 'process-naming', + 'when' => 'Before create-process', + 'content' => 'Name processes by outcome', + 'scope' => McpGuidance::SCOPE_PROCESSES, + ], + [ + 'id' => 'custom-connector-naming', + 'name' => 'Connector naming', + 'slug' => 'connector-naming', + 'when' => 'Before create-data-connector', + 'content' => 'Name connectors by target system', + 'scope' => McpGuidance::SCOPE_DATA_CONNECTORS, + ], + ], + 'name' => 'Skills', + 'group' => 'MCP', + 'ui' => ['component' => 'McpSkills'], + ]); + } +} diff --git a/tests/Feature/Mcp/FlowGenieServerTest.php b/tests/Feature/Mcp/FlowGenieServerTest.php new file mode 100644 index 0000000000..0f2347683d --- /dev/null +++ b/tests/Feature/Mcp/FlowGenieServerTest.php @@ -0,0 +1,166 @@ +user = User::factory()->create([ + 'password' => Hash::make('password'), + 'is_administrator' => true, + ]); + + if (!class_exists(FlowGenie::class)) { + $this->markTestSkipped('package-ai is not installed'); + } + } + + public function testAdminCanCreateListUpdateAndDeleteAFlowGenie(): void + { + $category = FlowGenieCategory::query()->create([ + 'name' => 'MCP FlowGenie Category', + 'status' => 'ACTIVE', + 'is_system' => false, + ]); + + $created = FlowGenieServer::actingAs($this->user)->tool(CreateFlowGenieTool::class, [ + 'name' => 'MCP Review Assistant', + 'description' => 'Summarize requests and suggest approve or reject', + 'flow_genie_category_id' => (string) $category->id, + 'config' => [ + 'prompt' => 'Summarize the request data and suggest APPROVED or REJECTED.', + 'response_format' => 'json', + 'model' => 'gpt-4o-mini', + 'provider' => 'openai', + ], + ]); + + $created->assertOk() + ->assertHasNoErrors() + ->assertSee('MCP Review Assistant') + ->assertStructuredContent(fn (AssertableJson $json) => $json + ->where('name', 'MCP Review Assistant') + ->where('has_instructions', true) + ->where('output_type', 'JSON') + ->where('config.instructions', 'Summarize the request data and suggest APPROVED or REJECTED.') + ->etc()); + + $genie = FlowGenie::query()->where('name', 'MCP Review Assistant')->firstOrFail(); + + FlowGenieServer::actingAs($this->user)->tool(ListFlowGeniesTool::class, [ + 'filter' => 'MCP Review', + ])->assertOk()->assertSee('MCP Review Assistant'); + + FlowGenieServer::actingAs($this->user)->tool(ListFlowGenieCategoriesTool::class, [ + 'filter' => 'MCP FlowGenie', + ])->assertOk()->assertSee('MCP FlowGenie Category'); + + FlowGenieServer::actingAs($this->user)->tool(GetFlowGenieTool::class, [ + 'id' => $genie->id, + ])->assertOk()->assertSee('gpt-4o-mini'); + + FlowGenieServer::actingAs($this->user)->tool(UpdateFlowGenieTool::class, [ + 'id' => $genie->id, + 'description' => 'Updated description for MCP FlowGenie', + 'config' => [ + 'instructions' => 'Return only a short JSON decision.', + 'output_type' => 'JSON', + 'model' => 'gpt-4o-mini', + 'provider' => 'openai', + ], + ])->assertOk() + ->assertSee('Updated description for MCP FlowGenie') + ->assertSee('Return only a short JSON decision.'); + + FlowGenieServer::actingAs($this->user)->tool(DeleteFlowGenieTool::class, [ + 'id' => $genie->id, + ])->assertOk()->assertSee('deleted'); + + $this->assertDatabaseMissing('flow_genies', ['id' => $genie->id]); + } + + public function testValidateFlowGenieReturnsNormalizedConfig(): void + { + FlowGenieServer::actingAs($this->user)->tool(ValidateFlowGenieTool::class, [ + 'config' => [ + 'prompt' => 'Hello agent', + 'response_format' => 'text', + ], + ])->assertOk() + ->assertStructuredContent(fn (AssertableJson $json) => $json + ->where('ok', true) + ->where('normalized.instructions', 'Hello agent') + ->where('normalized.output_type', 'Text (default)') + ->etc()); + } + + public function testGuidanceListsFlowGenieScopedCustomSkill(): void + { + Setting::updateOrCreate([ + 'key' => McpGuidance::SKILLS_KEY, + ], [ + 'format' => 'component', + 'config' => [ + [ + 'id' => 'custom-screen-styles', + 'name' => 'Screen styles', + 'slug' => 'screen-styles', + 'when' => 'Before create-screen when styling matters', + 'content' => 'Use calm colors', + 'scope' => McpGuidance::SCOPE_SCREENS, + ], + [ + 'id' => 'custom-genie-tone', + 'name' => 'Genie tone', + 'slug' => 'genie-tone', + 'when' => 'Before create-flow-genie', + 'content' => 'Keep a friendly tone', + 'scope' => McpGuidance::SCOPE_FLOW_GENIES, + ], + ], + 'name' => 'Skills', + 'group' => 'MCP', + ]); + + FlowGenieServer::actingAs($this->user)->tool(GetMcpGuidanceTool::class) + ->assertOk() + ->assertStructuredContent(fn (AssertableJson $json) => $json + ->has('skills', 1) + ->where('skills.0.slug', 'genie-tone') + ->missing('skills.0.content') + ->etc()); + + $server = app(FlowGenieServer::class, ['transport' => new FakeTransporter()]); + $resources = $server->createContext()->resources(); + + $this->assertCount(1, $resources); + $this->assertInstanceOf(McpSkillResource::class, $resources->first()); + $this->assertSame('pm://mcp/skills/genie-tone', $resources->first()->uri()); + } +} diff --git a/tests/Feature/Mcp/McpSettingsSeederTest.php b/tests/Feature/Mcp/McpSettingsSeederTest.php new file mode 100644 index 0000000000..2fdedaa961 --- /dev/null +++ b/tests/Feature/Mcp/McpSettingsSeederTest.php @@ -0,0 +1,69 @@ + SettingsMenus::INTEGRATIONS_MENU_GROUP, + ], [ + 'menu_group_order' => 2, + 'ui' => json_encode(['icon' => 'puzzle-piece']), + ]); + } + + public function testSeederCreatesEmptySettingsWithoutBuiltInSkills(): void + { + $this->createIntegrationsMenu(); + + $this->seed(McpSettingsSeeder::class); + + $this->assertSame([], Setting::configByKey(McpGuidance::SKILLS_KEY)); + $this->assertNotNull(Setting::where('key', McpGuidance::SYSTEM_PROMPT_KEY)->first()); + } + + public function testSeederKeepsExistingCustomSkillsAndSystemPrompt(): void + { + $this->createIntegrationsMenu(); + + Setting::factory()->create([ + 'key' => McpGuidance::SYSTEM_PROMPT_KEY, + 'format' => 'textarea', + 'config' => 'Always be brief', + 'name' => 'System Prompt', + 'group' => 'MCP', + ]); + Setting::factory()->create([ + 'key' => McpGuidance::SKILLS_KEY, + 'format' => 'component', + 'config' => [ + [ + 'id' => 'custom-1', + 'name' => 'Japanese labels', + 'slug' => 'japanese-labels', + 'when' => 'Before create-screen', + 'content' => 'Always use Japanese labels', + ], + ], + 'name' => 'Skills', + 'group' => 'MCP', + 'ui' => ['component' => 'McpSkills'], + ]); + + $this->seed(McpSettingsSeeder::class); + $this->seed(McpSettingsSeeder::class); + + $skills = Setting::configByKey(McpGuidance::SKILLS_KEY); + $this->assertCount(1, $skills); + $this->assertSame('custom-1', $skills[0]['id']); + $this->assertSame('Always be brief', Setting::configByKey(McpGuidance::SYSTEM_PROMPT_KEY)); + } +} diff --git a/tests/Feature/Mcp/ProcessServerTest.php b/tests/Feature/Mcp/ProcessServerTest.php new file mode 100644 index 0000000000..2715bc800b --- /dev/null +++ b/tests/Feature/Mcp/ProcessServerTest.php @@ -0,0 +1,298 @@ +create(); + $bpmn = Process::getProcessTemplate('OnlyStartElement.bpmn'); + + $created = ProcessServer::actingAs($this->user)->tool(CreateProcessTool::class, [ + 'name' => 'MCP Intake Flow', + 'description' => 'Created via MCP', + 'process_category_id' => (string) $category->id, + 'bpmn' => $bpmn, + ]); + + $created->assertOk() + ->assertHasNoErrors() + ->assertSee('MCP Intake Flow') + ->assertStructuredContent(fn (AssertableJson $json) => $json + ->where('name', 'MCP Intake Flow') + ->where('description', 'Created via MCP') + ->whereType('bpmn', 'string') + ->etc()); + + $process = Process::where('name', 'MCP Intake Flow')->firstOrFail(); + Event::assertDispatched(ProcessCreated::class); + + ProcessServer::actingAs($this->user)->tool(ListProcessesTool::class, [ + 'filter' => 'MCP Intake', + ])->assertOk()->assertSee('MCP Intake Flow'); + + ProcessServer::actingAs($this->user)->tool(GetProcessTool::class, [ + 'id' => $process->id, + ])->assertOk()->assertSee('Created via MCP'); + + ProcessServer::actingAs($this->user)->tool(UpdateProcessTool::class, [ + 'id' => $process->id, + 'description' => 'Updated via MCP', + ])->assertOk()->assertSee('Updated via MCP'); + + $this->assertSame('Updated via MCP', $process->refresh()->description); + } + + public function testProcessToolsRequireAnAuthenticatedUserWithPermission(): void + { + ProcessServer::tool(ListProcessesTool::class) + ->assertHasErrors(['Authenticate with a ProcessMaker API token']); + + $member = User::factory()->create([ + 'is_administrator' => false, + ]); + + ProcessServer::actingAs($member)->tool(ListProcessesTool::class) + ->assertHasErrors(['view-processes']); + } + + public function testPlanProcessReturnsOpenQuestionsThenBlueprint(): void + { + ProcessServer::actingAs($this->user)->tool(PlanProcessTool::class, [ + 'goal' => 'Employee leave request with manager approval', + ])->assertOk() + ->assertStructuredContent(fn (AssertableJson $json) => $json + ->where('ready', false) + ->whereType('open_questions', 'array') + ->where('open_questions.0.id', 'name') + ->etc()); + + ProcessServer::actingAs($this->user)->tool(PlanProcessTool::class, [ + 'goal' => 'Employee leave request with manager approval', + 'name' => 'Leave Request', + 'purpose' => 'Employees request PTO; managers approve or reject.', + 'tasks' => '1) Submit request (form) 2) Manager review (form)', + 'decisions' => 'After review: Approved vs Rejected on approval_status', + 'assignments' => 'Submit=requester; Review=process_manager', + 'process_manager' => 'admin', + 'screens' => 'Create FORM submit + FORM approval + DISPLAY summary', + 'start' => 'Requester from Requests UI with interstitial', + ])->assertOk() + ->assertStructuredContent(fn (AssertableJson $json) => $json + ->where('ready', true) + ->where('blueprint.name', 'Leave Request') + ->whereType('blueprint.next_steps', 'array') + ->etc()); + } + + public function testListUsersAndGroupsForAssignment(): void + { + $peer = User::factory()->create([ + 'username' => 'pto.manager', + 'firstname' => 'Pto', + 'lastname' => 'Manager', + 'status' => 'ACTIVE', + 'is_administrator' => false, + ]); + $group = Group::factory()->create([ + 'name' => 'PTO Approvers', + 'status' => 'ACTIVE', + ]); + + ProcessServer::actingAs($this->user)->tool(ListUsersTool::class, [ + 'filter' => 'pto.manager', + ])->assertOk() + ->assertStructuredContent(fn (AssertableJson $json) => $json + ->where('data.0.id', $peer->id) + ->where('data.0.username', 'pto.manager') + ->etc()); + + ProcessServer::actingAs($this->user)->tool(ListGroupsTool::class, [ + 'filter' => 'PTO Approvers', + ])->assertOk() + ->assertStructuredContent(fn (AssertableJson $json) => $json + ->where('data.0.id', $group->id) + ->where('data.0.name', 'PTO Approvers') + ->etc()); + } + + public function testValidateProcessReportsMissingScreenRef(): void + { + $bpmn = <<<'XML' + + + + + node_4 + + + node_4 + node_5 + + + node_5 + + + + + + + + +XML; + + ProcessServer::actingAs($this->user)->tool(ValidateProcessTool::class, [ + 'bpmn' => $bpmn, + ])->assertOk() + ->assertStructuredContent(fn (AssertableJson $json) => $json + ->where('ok', false) + ->where('errors.0.code', 'missing_screen_ref') + ->etc()); + } + + public function testHttpServerRequiresABearerToken(): void + { + $this->postJson('/mcp/processes', [ + 'jsonrpc' => '2.0', + 'id' => 1, + 'method' => 'initialize', + 'params' => [], + ])->assertUnauthorized(); + } + + public function testGetMcpGuidanceReturnsProcessScopedCatalog(): void + { + $this->seedMcpGuidance(); + + ProcessServer::actingAs($this->user) + ->tool(GetMcpGuidanceTool::class) + ->assertOk() + ->assertHasNoErrors() + ->assertSee([ + 'Prefer clear process names', + 'Process naming', + ]) + ->assertDontSee('screen-styles') + ->assertStructuredContent(fn (AssertableJson $json) => $json + ->where('system_prompt', 'Prefer clear process names') + ->where('skills.0.slug', 'process-naming') + ->missing('skills.0.content') + ->etc()); + } + + public function testGetMcpSkillReturnsFullSkillBodyBySlug(): void + { + $this->seedMcpGuidance(); + + ProcessServer::actingAs($this->user) + ->tool(GetMcpSkillTool::class, ['slug' => 'process-naming']) + ->assertOk() + ->assertHasNoErrors() + ->assertSee('Name processes by outcome') + ->assertStructuredContent(fn (AssertableJson $json) => $json + ->where('slug', 'process-naming') + ->where('content', 'Name processes by outcome') + ->where('uri', 'pm://mcp/skills/process-naming') + ->etc()); + } + + public function testServerInstructionsIncludeProcessCatalogWithoutSkillBodies(): void + { + $this->seedMcpGuidance(); + + $server = app(ProcessServer::class, ['transport' => new FakeTransporter()]); + $instructions = $server->createContext()->instructions; + + $this->assertStringContainsString('get-mcp-guidance', $instructions); + $this->assertStringContainsString('validate-process', $instructions); + $this->assertStringContainsString('Prefer clear process names', $instructions); + $this->assertStringContainsString('process-naming', $instructions); + $this->assertStringNotContainsString('Name processes by outcome', $instructions); + $this->assertStringNotContainsString('screen-styles', $instructions); + } + + public function testSkillResourcesAreProcessScoped(): void + { + $this->seedMcpGuidance(); + + $server = app(ProcessServer::class, ['transport' => new FakeTransporter()]); + $resources = $server->createContext()->resources(); + + $this->assertCount(1, $resources); + $this->assertInstanceOf(McpSkillResource::class, $resources->first()); + $this->assertSame('pm://mcp/skills/process-naming', $resources->first()->uri()); + } + + private function seedMcpGuidance(): void + { + Setting::factory()->create([ + 'key' => McpGuidance::SYSTEM_PROMPT_KEY, + 'format' => 'textarea', + 'config' => 'Prefer clear process names', + 'name' => 'System Prompt', + 'group' => 'MCP', + ]); + + Setting::factory()->create([ + 'key' => McpGuidance::SKILLS_KEY, + 'format' => 'component', + 'config' => [ + [ + 'id' => 'custom-screen-styles', + 'name' => 'Screen styles', + 'slug' => 'screen-styles', + 'when' => 'Before create-screen when styling matters', + 'content' => 'Use calm colors', + 'scope' => McpGuidance::SCOPE_SCREENS, + ], + [ + 'id' => 'custom-process-naming', + 'name' => 'Process naming', + 'slug' => 'process-naming', + 'when' => 'Before create-process', + 'content' => 'Name processes by outcome', + 'scope' => McpGuidance::SCOPE_PROCESSES, + ], + ], + 'name' => 'Skills', + 'group' => 'MCP', + 'ui' => ['component' => 'McpSkills'], + ]); + } +} diff --git a/tests/Feature/Mcp/ScreenServerTest.php b/tests/Feature/Mcp/ScreenServerTest.php new file mode 100644 index 0000000000..2e1daabf4f --- /dev/null +++ b/tests/Feature/Mcp/ScreenServerTest.php @@ -0,0 +1,336 @@ +create(); + $created = ScreenServer::actingAs($this->user)->tool(CreateScreenTool::class, [ + 'title' => 'Intake Form', + 'description' => 'Collects intake details', + 'type' => 'FORM', + 'screen_category_id' => $category->id, + 'config' => [ + ['name' => 'Intake'], + ], + ]); + + $created->assertOk() + ->assertHasNoErrors() + ->assertSee('Intake Form') + ->assertStructuredContent(fn (AssertableJson $json) => $json + ->where('title', 'Intake Form') + ->where('type', 'FORM') + ->where('config.0.name', 'Intake') + ->etc()); + + $screen = Screen::where('title', 'Intake Form')->firstOrFail(); + Event::assertDispatched(ScreenCreated::class); + + ScreenServer::actingAs($this->user)->tool(ListScreensTool::class, [ + 'filter' => 'Intake', + ])->assertOk()->assertSee('Intake Form'); + + ScreenServer::actingAs($this->user)->tool(GetScreenTool::class, [ + 'id' => $screen->id, + ])->assertOk()->assertSee('Collects intake details'); + + ScreenServer::actingAs($this->user)->tool(UpdateScreenTool::class, [ + 'id' => $screen->id, + 'description' => 'Updated intake details', + ])->assertOk()->assertSee('Updated intake details'); + + $this->assertSame('Updated intake details', $screen->refresh()->description); + Event::assertDispatched(ScreenUpdated::class); + + ScreenServer::actingAs($this->user)->tool(DeleteScreenTool::class, [ + 'id' => $screen->id, + ])->assertOk()->assertStructuredContent([ + 'id' => $screen->id, + 'deleted' => true, + ]); + + $this->assertNull(Screen::find($screen->id)); + Event::assertDispatched(ScreenDeleted::class); + } + + public function testScreenToolsRequireAnAuthenticatedUserWithPermission(): void + { + ScreenServer::tool(ListScreensTool::class) + ->assertHasErrors(['Authenticate with a ProcessMaker API token']); + + $member = User::factory()->create([ + 'is_administrator' => false, + ]); + + ScreenServer::actingAs($member)->tool(ListScreensTool::class) + ->assertHasErrors(['view-screens']); + } + + public function testDefaultScreensCannotBeDeleted(): void + { + $screen = Screen::factory()->create([ + 'is_default' => 1, + ]); + + ScreenServer::actingAs($this->user)->tool(DeleteScreenTool::class, [ + 'id' => $screen->id, + ])->assertHasErrors(['Default screens cannot be deleted.']); + + $this->assertNotNull(Screen::find($screen->id)); + } + + public function testCreateScreenNormalizesMissingInspectorAndItems(): void + { + Event::fake([ScreenCreated::class]); + + $category = ScreenCategory::factory()->create(); + ScreenServer::actingAs($this->user)->tool(CreateScreenTool::class, [ + 'title' => 'MCP Safe Config', + 'description' => 'Ensures inspector defaults', + 'type' => 'FORM', + 'screen_category_id' => $category->id, + 'config' => [ + [ + 'name' => 'Page 1', + 'items' => [ + [ + 'component' => 'FormInput', + 'label' => 'Name', + 'config' => ['name' => 'name', 'label' => 'Name'], + ], + ], + ], + ], + 'computed' => null, + 'watchers' => null, + ])->assertOk()->assertHasNoErrors(); + + $screen = Screen::where('title', 'MCP Safe Config')->firstOrFail(); + $this->assertSame([], $screen->config[0]['items'][0]['inspector']); + $this->assertSame([], $screen->computed); + $this->assertSame([], $screen->watchers); + } + + public function testCreateScreenRejectsSelectListWithoutOptionsList(): void + { + $category = ScreenCategory::factory()->create(); + + ScreenServer::actingAs($this->user)->tool(CreateScreenTool::class, [ + 'title' => 'Broken Select Screen', + 'description' => 'Select missing optionsList', + 'type' => 'FORM', + 'screen_category_id' => $category->id, + 'config' => [ + [ + 'name' => 'Page 1', + 'items' => [ + [ + 'component' => 'FormSelectList', + 'inspector' => [], + 'config' => [ + 'name' => 'category', + 'label' => 'Category', + 'validation' => 'required', + 'options' => [ + 'dataSource' => 'provideData', + 'jsonData' => '[{"value":"Travel","content":"Travel"}]', + ], + ], + ], + ], + ], + ], + ])->assertHasErrors(['optionsList']); + } + + public function testValidateScreenReportsSelectAndVueIssues(): void + { + ScreenServer::actingAs($this->user)->tool(ValidateScreenTool::class, [ + 'type' => 'FORM', + 'config' => [ + [ + 'name' => 'Page 1', + 'items' => [ + [ + 'component' => 'FormSelectList', + 'inspector' => [], + 'config' => [ + 'name' => 'category', + 'options' => [ + 'dataSource' => 'provideData', + ], + ], + ], + [ + 'component' => 'VueComponentRenderTemplate', + 'inspector' => [], + 'config' => [ + 'contentVue' => '', + 'components' => [], + ], + ], + ], + ], + ], + ])->assertOk()->assertStructuredContent(fn (AssertableJson $json) => $json + ->where('ok', false) + ->has('errors') + ->etc()); + } + + public function testHttpServerRequiresABearerToken(): void + { + $this->postJson('/mcp/screens', [ + 'jsonrpc' => '2.0', + 'id' => 1, + 'method' => 'initialize', + 'params' => [], + ])->assertUnauthorized(); + } + + public function testGetMcpGuidanceReturnsCatalogWithoutSkillBodies(): void + { + $this->seedMcpGuidance(); + + ScreenServer::actingAs($this->user) + ->tool(GetMcpGuidanceTool::class) + ->assertOk() + ->assertHasNoErrors() + ->assertSee([ + 'Always use FormInput for customer names', + 'Screen Styles', + 'Before create-screen when styling matters', + ]) + ->assertStructuredContent(fn (AssertableJson $json) => $json + ->where('system_prompt', 'Always use FormInput for customer names') + ->where('skills.0.name', 'Screen Styles') + ->where('skills.0.slug', 'screen-styles') + ->where('skills.0.when', 'Before create-screen when styling matters') + ->where('skills.0.uri', 'pm://mcp/skills/screen-styles') + ->missing('skills.0.content') + ->etc()); + } + + public function testGetMcpSkillReturnsFullSkillBodyBySlug(): void + { + $this->seedMcpGuidance(); + + ScreenServer::actingAs($this->user) + ->tool(GetMcpSkillTool::class, ['slug' => 'screen-styles']) + ->assertOk() + ->assertHasNoErrors() + ->assertSee('Prefer primary buttons') + ->assertStructuredContent(fn (AssertableJson $json) => $json + ->where('slug', 'screen-styles') + ->where('when', 'Before create-screen when styling matters') + ->where('content', 'Prefer primary buttons') + ->where('uri', 'pm://mcp/skills/screen-styles') + ->etc()); + + ScreenServer::actingAs($this->user) + ->tool(GetMcpSkillTool::class, ['slug' => 'missing-skill']) + ->assertHasErrors(['Skill not found for slug: missing-skill']); + } + + public function testServerInstructionsIncludeCatalogWithoutSkillBodies(): void + { + $this->seedMcpGuidance(); + + $server = app(ScreenServer::class, ['transport' => new FakeTransporter()]); + $instructions = $server->createContext()->instructions; + + $this->assertStringContainsString('get-mcp-guidance', $instructions); + $this->assertStringContainsString('get-mcp-skill', $instructions); + $this->assertStringContainsString('Always use FormInput for customer names', $instructions); + $this->assertStringContainsString('Skills catalog', $instructions); + $this->assertStringContainsString('Screen Styles', $instructions); + $this->assertStringContainsString('Before create-screen when styling matters', $instructions); + $this->assertStringContainsString('pm://mcp/skills/screen-styles', $instructions); + $this->assertStringNotContainsString('Prefer primary buttons', $instructions); + } + + public function testSkillResourcesAreRegisteredFromSettings(): void + { + $this->seedMcpGuidance(); + + $server = app(ScreenServer::class, ['transport' => new FakeTransporter()]); + $resources = $server->createContext()->resources(); + + $this->assertCount(1, $resources); + $this->assertInstanceOf(McpSkillResource::class, $resources->first()); + $this->assertSame('pm://mcp/skills/screen-styles', $resources->first()->uri()); + + ScreenServer::actingAs($this->user) + ->resource(new McpSkillResource( + 'Screen Styles', + 'screen-styles', + 'Prefer primary buttons', + 'Before create-screen when styling matters', + )) + ->assertOk() + ->assertSee('Prefer primary buttons'); + } + + private function seedMcpGuidance(): void + { + Setting::factory()->create([ + 'key' => McpGuidance::SYSTEM_PROMPT_KEY, + 'format' => 'textarea', + 'config' => 'Always use FormInput for customer names', + 'name' => 'System Prompt', + 'group' => 'MCP', + ]); + + Setting::factory()->create([ + 'key' => McpGuidance::SKILLS_KEY, + 'format' => 'component', + 'config' => [ + [ + 'id' => 'skill-1', + 'name' => 'Screen Styles', + 'slug' => 'screen-styles', + 'when' => 'Before create-screen when styling matters', + 'content' => 'Prefer primary buttons', + ], + ], + 'name' => 'Skills', + 'group' => 'MCP', + 'ui' => ['component' => 'McpSkills'], + ]); + } +} diff --git a/tests/Feature/ScheduledTaskDuplicationTest.php b/tests/Feature/ScheduledTaskDuplicationTest.php index a3dd928051..aa116cd6a5 100644 --- a/tests/Feature/ScheduledTaskDuplicationTest.php +++ b/tests/Feature/ScheduledTaskDuplicationTest.php @@ -309,7 +309,7 @@ public function testTaskNotExecutedIfNextDateInFuture() // Calculate nextDate using the manager $config = json_decode($task->configuration); - $lastExecution = new \DateTime($task->last_execution, new \DateTimeZone('UTC')); + $lastExecution = new \DateTime($task->last_execution, new DateTimeZone('UTC')); $nextDate = $manager->nextDate($today, $config, $lastExecution, null); // nextDate should be 12:55 which is 55 minutes after "today" (12:00) @@ -352,7 +352,7 @@ public function testTaskExecutedIfNextDatePassed() // Calculate nextDate $config = json_decode($task->configuration); - $lastExecution = new \DateTime($task->last_execution, new \DateTimeZone('UTC')); + $lastExecution = new \DateTime($task->last_execution, new DateTimeZone('UTC')); $nextDate = $manager->nextDate($today, $config, $lastExecution, null); // nextDate should be 12:01 which is before "today" (12:30) diff --git a/tests/Feature/Shared/ResourceAssertionsTrait.php b/tests/Feature/Shared/ResourceAssertionsTrait.php index dd65b22734..4fbed6dce9 100644 --- a/tests/Feature/Shared/ResourceAssertionsTrait.php +++ b/tests/Feature/Shared/ResourceAssertionsTrait.php @@ -22,7 +22,7 @@ trait ResourceAssertionsTrait * @param type $query * @param type $expectedMeta * - * @return \Illuminate\Testing\TestResponse + * @return TestResponse */ protected function assertCorrectModelListing($query, $expectedMeta = []) { @@ -56,7 +56,7 @@ protected function assertModelSorting($query, $expectedFirstRow) * @param string $modelClass * @param array $attributes * - * @return \Illuminate\Testing\TestResponse + * @return TestResponse */ protected function assertCorrectModelCreation($modelClass, array $attributes = []) { @@ -84,7 +84,7 @@ protected function assertCorrectModelCreation($modelClass, array $attributes = [ * @param array $attributes * @param array $errors * - * @return \Illuminate\Testing\TestResponse + * @return TestResponse */ protected function assertModelCreationFails($modelClass, array $attributes = [], array $errors = []) { @@ -105,7 +105,7 @@ protected function assertModelCreationFails($modelClass, array $attributes = [], * @param string $id * @param array $includes * - * @return \Illuminate\Testing\TestResponse + * @return TestResponse */ protected function assertModelShow($id, array $includes = []) { @@ -127,7 +127,7 @@ protected function assertModelShow($id, array $includes = []) * * @param type $id * - * @return \Illuminate\Testing\TestResponse + * @return TestResponse */ protected function assertCorrectModelDeletion($id) { @@ -144,7 +144,7 @@ protected function assertCorrectModelDeletion($id) * * @param type $id * - * @return \Illuminate\Testing\TestResponse + * @return TestResponse */ protected function assertModelDeletionFails($id, array $errors = []) { @@ -236,7 +236,7 @@ private function getDataAttributes($row) * Assert that the response has the given status code. * * @param string $expected - * @param \Illuminate\Testing\TestResponse $response + * @param TestResponse $response */ protected function assertStatus($expected, TestResponse $response) { diff --git a/tests/Feature/Upgrade/RemoveMcpSystemSkillsTest.php b/tests/Feature/Upgrade/RemoveMcpSystemSkillsTest.php new file mode 100644 index 0000000000..183062c383 --- /dev/null +++ b/tests/Feature/Upgrade/RemoveMcpSystemSkillsTest.php @@ -0,0 +1,59 @@ +up(); + } + + public function testRemovesSystemSkillsAndKeepsCustomSkills(): void + { + Setting::factory()->create([ + 'key' => McpGuidance::SKILLS_KEY, + 'format' => 'component', + 'config' => [ + ['id' => 'mcp-system-screen-creation-rules', 'slug' => 'screen-creation-rules', 'system' => true], + ['id' => 'flagged-system', 'slug' => 'flagged', 'system' => true], + ['id' => 'skill-1793976475109-l6si55', 'slug' => 'skill-1793976475109-l6si55'], + ['id' => 'custom-1', 'name' => 'Japanese labels', 'slug' => 'japanese-labels', 'content' => 'x'], + ['id' => 'custom-2', 'name' => 'Tone', 'slug' => 'tone', 'content' => 'y', 'scope' => 'screens'], + ], + 'name' => 'Skills', + 'group' => 'MCP', + 'ui' => ['component' => 'McpSkills'], + ]); + + $this->runUpgrade(); + + $skills = Setting::configByKey(McpGuidance::SKILLS_KEY); + $this->assertSame(['custom-1', 'custom-2'], array_column($skills, 'id')); + $this->assertSame('screens', $skills[1]['scope']); + } + + public function testIsIdempotentAndSkipsMissingSetting(): void + { + $this->runUpgrade(); + $this->assertNull(Setting::where('key', McpGuidance::SKILLS_KEY)->first()); + + Setting::factory()->create([ + 'key' => McpGuidance::SKILLS_KEY, + 'format' => 'component', + 'config' => [['id' => 'custom-1', 'slug' => 'a', 'content' => 'x']], + 'name' => 'Skills', + 'group' => 'MCP', + ]); + + $this->runUpgrade(); + $this->runUpgrade(); + + $this->assertCount(1, Setting::configByKey(McpGuidance::SKILLS_KEY)); + } +} diff --git a/tests/Jobs/InstallPluginTest.php b/tests/Jobs/InstallPluginTest.php new file mode 100644 index 0000000000..cd6848a9cb --- /dev/null +++ b/tests/Jobs/InstallPluginTest.php @@ -0,0 +1,107 @@ +shouldReceive('install') + ->once() + ->with( + 'https://github.com/ProcessMaker/package-translations.git', + 'develop', + null, + 7, + null, + null, + ); + + $job = new InstallPlugin( + userId: 7, + url: 'https://github.com/ProcessMaker/package-translations.git', + branch: 'develop', + ); + $job->handle($manager); + + Event::assertDispatched(PluginLog::class, function (PluginLog $event) { + return $event->status === 'starting' + && $event->userId === 7; + }); + } + + public function testHandleInstallsFromZip(): void + { + Event::fake([PluginLog::class]); + + $zipPath = storage_path('app/plugin-uploads/example.zip'); + $manager = Mockery::mock(PluginManager::class); + $manager->shouldReceive('installFromZip') + ->once() + ->with($zipPath, 7); + + $job = new InstallPlugin( + userId: 7, + zipPath: $zipPath, + ); + $job->handle($manager); + + Event::assertDispatched(PluginLog::class, function (PluginLog $event) { + return $event->status === 'starting'; + }); + } + + public function testHandleRevertsAndRethrowsWhenInstallFails(): void + { + Event::fake([PluginLog::class]); + + $manager = Mockery::mock(PluginManager::class); + $manager->shouldReceive('install') + ->once() + ->andThrow(new \RuntimeException('Git clone exceeded the timeout while installing package-plg.')); + $manager->shouldReceive('revertFailedInstall') + ->once() + ->with(7); + + $job = new InstallPlugin( + userId: 7, + url: 'https://github.com/ProcessMaker/package-plg.git', + ); + + $this->expectException(\RuntimeException::class); + $this->expectExceptionMessage('exceeded the timeout'); + + $job->handle($manager); + } + + public function testFailedBroadcastsError(): void + { + Event::fake([PluginLog::class]); + + $job = new InstallPlugin( + userId: 7, + url: 'https://github.com/ProcessMaker/package-translations.git', + ); + $job->failed(new \RuntimeException( + "The process \"'git' 'clone' 'https://token:secret@github.com/ProcessMaker/package-plg'\" exceeded the timeout of 300 seconds." + )); + + Event::assertDispatched(PluginLog::class, function (PluginLog $event) { + return $event->status === 'error' + && str_contains($event->message, 'exceeded the timeout') + && !str_contains($event->message, 'secret') + && str_contains($event->message, 'https://github.com/ProcessMaker/package-plg') + && $event->userId === 7; + }); + } +} diff --git a/tests/Managers/PluginManagerTest.php b/tests/Managers/PluginManagerTest.php new file mode 100644 index 0000000000..3902683e36 --- /dev/null +++ b/tests/Managers/PluginManagerTest.php @@ -0,0 +1,728 @@ +pluginsDir = storage_path('plugins'); + if (!is_dir($this->pluginsDir)) { + mkdir($this->pluginsDir, 0755, true); + } + + config([ + 'plugin_manager.supervisorctl_bin' => 'supervisorctl', + 'plugin_manager.supervisorctl_config' => null, + 'plugin_manager.supervisor_restart_target' => 'all', + 'plugin_manager.allow_local_install_path' => true, + ]); + + $this->runtimeReload = Mockery::spy(PluginRuntimeReload::class); + app()->instance(PluginRuntimeReload::class, $this->runtimeReload); + } + + protected function tearDown(): void + { + $this->tearDownTestPluginManager(); + + parent::tearDown(); + } + + public function tearDownTestPluginManager() + { + if (!$this->pluginsDir) { + return; + } + + foreach (array_merge($this->temporaryPluginDirs(), $this->leftoverToggleDirs()) as $dir) { + if (is_link($dir)) { + unlink($dir); + continue; + } + + if (is_dir($dir) && realpath($dir) !== realpath($this->pluginsDir)) { + $this->deleteDirectory($dir); + } + } + } + + /** + * Toggle renames the folder by adding or removing a leading underscore. + * Remove whichever path is left. + * + * @return list + */ + private function temporaryPluginDirs(): array + { + if (!$this->tempPluginDir || realpath((string) $this->tempPluginDir) === realpath($this->pluginsDir)) { + return [ + $this->pluginsDir . '/plugin-one', + $this->pluginsDir . '/plugin-two', + $this->pluginsDir . '/valid-plugin', + $this->pluginsDir . '/_hidden-plugin', + ]; + } + + $base = basename($this->tempPluginDir); + $parent = dirname($this->tempPluginDir); + $toggled = str_starts_with($base, '_') + ? $parent . '/' . substr($base, 1) + : $parent . '/_' . $base; + + return [$this->tempPluginDir, $toggled]; + } + + /** + * @return list + */ + private function leftoverToggleDirs(): array + { + return array_merge( + glob($this->pluginsDir . '/supervisor-toggle-*') ?: [], + glob($this->pluginsDir . '/_supervisor-toggle-*') ?: [] + ); + } + + public function testInstallExistingRepositoryChecksOutRequestedBranchWithoutMain() + { + $this->setUpTestPluginManager(); + + $repoUrl = 'https://github.com/ProcessMaker/plugin-develop.git'; + $pluginName = 'plugin-develop'; + $pluginPath = $this->pluginsDir . '/' . $pluginName; + + $this->tempPluginDir = $pluginPath; + mkdir($this->tempPluginDir, 0755, true); + file_put_contents( + $this->tempPluginDir . '/composer.json', + json_encode([ + 'name' => 'processmaker/plugin-develop', + 'autoload' => [ + 'psr-4' => [ + 'ProcessMaker\\Plugins\\Develop\\' => 'src/', + ], + ], + ], JSON_PRETTY_PRINT) + ); + + Process::fake([ + '*' => Process::result('', exitCode: 0), + ]); + + Event::fake([PluginLog::class]); + + $manager = new PluginManager(); + $manager->install($repoUrl, 'develop', null, 1); + + Process::assertRan(fn ($process) => $this->processRunsGitCommand($process, ['git', 'fetch', '--all', '--tags'])); + Process::assertRan(fn ($process) => $this->processRunsGitCommand($process, ['git', 'checkout', 'develop'])); + Process::assertRan(fn ($process) => $this->processRunsGitCommand($process, ['git', 'pull', 'origin', 'develop'])); + Process::assertNotRan(fn ($process) => $this->processRunsGitCommand($process, ['git', 'checkout', 'main'])); + } + + public function testReinstallUsesCurrentBranchForGitPull() + { + $this->setUpTestPluginManager(); + + $pluginName = 'plugin-reinstall-' . uniqid(); + $pluginPath = $this->pluginsDir . '/' . $pluginName; + $this->tempPluginDir = $pluginPath; + + mkdir($pluginPath, 0755, true); + mkdir($pluginPath . '/.git', 0755, true); + file_put_contents( + $pluginPath . '/composer.json', + json_encode([ + 'name' => 'processmaker/' . $pluginName, + 'autoload' => [ + 'psr-4' => [ + 'ProcessMaker\\Plugins\\Reinstall\\' => 'src/', + ], + ], + ], JSON_PRETTY_PRINT) + ); + + Process::fake([ + '*' => Process::result("develop\n", 0), + ]); + + Event::fake([PluginLog::class]); + + $manager = new PluginManager(); + $manager->reinstall($pluginName, 1); + + Process::assertRan(fn ($process) => $this->processRunsGitCommand($process, ['git', 'rev-parse', '--abbrev-ref', 'HEAD'])); + Process::assertRan(fn ($process) => $this->processRunsGitCommand($process, ['git', 'pull', 'origin', 'develop'])); + } + + public function testInstallExistingRepositoryWithTagSkipsBranchCheckoutAndPull() + { + $this->setUpTestPluginManager(); + + $repoUrl = 'https://github.com/ProcessMaker/plugin-tagged.git'; + $pluginName = 'plugin-tagged'; + $pluginPath = $this->pluginsDir . '/' . $pluginName; + + $this->tempPluginDir = $pluginPath; + mkdir($this->tempPluginDir, 0755, true); + file_put_contents( + $this->tempPluginDir . '/composer.json', + json_encode([ + 'name' => 'processmaker/plugin-tagged', + 'autoload' => [ + 'psr-4' => [ + 'ProcessMaker\\Plugins\\Tagged\\' => 'src/', + ], + ], + ], JSON_PRETTY_PRINT) + ); + + Process::fake([ + '*' => Process::result('', exitCode: 0), + ]); + + Event::fake([PluginLog::class]); + + $manager = new PluginManager(); + $manager->install($repoUrl, null, 'v1.0.0', 1); + + Process::assertRan(fn ($process) => $this->processRunsGitCommand($process, ['git', 'checkout', 'tags/v1.0.0'])); + Process::assertNotRan(fn ($process) => $this->processRunsGitCommand($process, ['git', 'checkout', 'main'])); + Process::assertNotRan(fn ($process) => $this->processRunsGitCommand($process, ['git', 'pull'])); + } + + public function testInstallClonesRepositoryAndValidatesPlugin() + { + $this->setUpTestPluginManager(); + + $repoUrl = 'https://github.com/ProcessMaker/plugin-example.git'; + $pluginName = 'plugin-example'; + $pluginPath = $this->pluginsDir . '/' . $pluginName; + + // Create temporary plugin directory with valid composer.json + $this->tempPluginDir = $pluginPath; + mkdir($this->tempPluginDir, 0755, true); + $composerJson = [ + 'name' => 'processmaker/plugin-example', + 'description' => 'Test plugin', + 'autoload' => [ + 'psr-4' => [ + 'ProcessMaker\\Plugins\\Example\\' => 'src/', + ], + ], + ]; + file_put_contents( + $this->tempPluginDir . '/composer.json', + json_encode($composerJson, JSON_PRETTY_PRINT) + ); + + // Fake Process to return successful results + Process::fake([ + '*' => Process::result('', exitCode: 0), + ]); + + Event::fake([PluginLog::class]); + + $manager = new PluginManager(); + $manager->install($repoUrl, null, null, 1); + + Event::assertDispatched(PluginLog::class, function ($event) { + return str_contains($event->message, 'installed successfully'); + }); + $this->assertRuntimeReloadPublished(); + } + + public function testInstallValidatesComposerJsonNamespace() + { + $this->setUpTestPluginManager(); + + $repoUrl = 'https://github.com/ProcessMaker/invalid-plugin.git'; + $pluginName = 'invalid-plugin'; + $pluginPath = $this->pluginsDir . '/' . $pluginName; + + // Create temporary plugin directory with invalid composer.json + $this->tempPluginDir = $pluginPath; + mkdir($this->tempPluginDir, 0755, true); + $composerJson = [ + 'name' => 'invalid/plugin', + 'autoload' => [ + 'psr-4' => [ + 'Invalid\\Namespace\\' => 'src/', + ], + ], + ]; + file_put_contents( + $this->tempPluginDir . '/composer.json', + json_encode($composerJson, JSON_PRETTY_PRINT) + ); + + // Fake Process to return successful results + Process::fake([ + '*' => Process::result('', exitCode: 0), + ]); + + Event::fake([PluginLog::class]); + + $manager = new PluginManager(); + + $this->expectException(RuntimeException::class); + $this->expectExceptionMessage("Plugin PSR-4 namespace must start with 'ProcessMaker\\Plugins\\'"); + + $manager->install($repoUrl); + } + + public function testInstallFailureRemovesPluginDirectoryCreatedByTheAttempt(): void + { + $this->setUpTestPluginManager(); + + $sourceName = 'plugin-rollback-' . uniqid(); + $source = sys_get_temp_dir() . '/' . $sourceName; + mkdir($source, 0755, true); + file_put_contents($source . '/composer.json', json_encode([ + 'name' => 'invalid/plugin', + 'autoload' => [ + 'psr-4' => [ + 'Invalid\\Namespace\\' => 'src/', + ], + ], + ])); + + $pluginPath = $this->pluginsDir . '/' . $sourceName; + $this->tempPluginDir = $pluginPath; + + Process::fake([ + '*' => Process::result('', exitCode: 0), + ]); + Event::fake([PluginLog::class]); + + $manager = new PluginManager(); + + try { + $manager->install($source, null, null, 1); + $this->fail('Install was expected to fail validation.'); + } catch (RuntimeException $exception) { + $this->assertStringContainsString('PSR-4', $exception->getMessage()); + } finally { + $this->deleteDirectory($source); + } + + $this->assertFalse(is_dir($pluginPath) || is_link($pluginPath)); + Event::assertDispatched(PluginLog::class, function (PluginLog $event) { + return str_contains($event->message, 'Removing the incomplete plugin directory'); + }); + } + + public function testUninstallRemovesPluginDirectory() + { + $this->setUpTestPluginManager(); + + $pluginName = 'test-plugin'; + $pluginPath = $this->pluginsDir . '/' . $pluginName; + + // Create temporary plugin directory + $this->tempPluginDir = $pluginPath; + mkdir($this->tempPluginDir, 0755, true); + file_put_contents($this->tempPluginDir . '/test.txt', 'test'); + + Event::fake([PluginLog::class]); + + Process::fake([ + '*' => Process::result('', exitCode: 0), + ]); + + $manager = new PluginManager(); + $manager->uninstall($pluginName, 1); + + $this->assertFalse(is_dir($pluginPath)); + Event::assertDispatched(PluginLog::class, function ($event) { + return str_contains($event->message, 'uninstalled successfully'); + }); + $this->assertRuntimeReloadPublished(); + } + + public function testUninstallThrowsExceptionForNonExistentPlugin() + { + $this->setUpTestPluginManager(); + + $manager = new PluginManager(); + + $this->expectException(RuntimeException::class); + $this->expectExceptionMessage('Plugin not found: non-existent-plugin'); + + $manager->uninstall('non-existent-plugin'); + } + + public function testListReturnsInstalledPlugins() + { + $this->setUpTestPluginManager(); + + // Create test plugin directories + $plugin1Path = $this->pluginsDir . '/plugin-one'; + $plugin2Path = $this->pluginsDir . '/plugin-two'; + $this->tempPluginDir = $this->pluginsDir; + + mkdir($plugin1Path, 0755, true); + mkdir($plugin2Path, 0755, true); + + $composerJson1 = [ + 'name' => 'test/plugin-one', + 'description' => 'First test plugin', + 'autoload' => [ + 'psr-4' => [ + 'ProcessMaker\\Plugins\\One\\' => 'src/', + ], + ], + ]; + + $composerJson2 = [ + 'name' => 'test/plugin-two', + 'description' => 'Second test plugin', + 'autoload' => [ + 'psr-4' => [ + 'ProcessMaker\\Plugins\\Two\\' => 'src/', + ], + ], + ]; + + file_put_contents( + $plugin1Path . '/composer.json', + json_encode($composerJson1, JSON_PRETTY_PRINT) + ); + file_put_contents( + $plugin2Path . '/composer.json', + json_encode($composerJson2, JSON_PRETTY_PRINT) + ); + + // Mock git commands to return null (no git repo) + Process::fake([ + '*' => Process::result(''), + ]); + + $manager = new PluginManager(); + $plugins = $manager->list(); + + $this->assertCount(2, $plugins); + $this->assertEquals('plugin-one', $plugins[0]['name']); + $this->assertEquals('First test plugin', $plugins[0]['description']); + $this->assertEquals('plugin-two', $plugins[1]['name']); + $this->assertEquals('Second test plugin', $plugins[1]['description']); + } + + public function testListIncludesDisabledPlugins() + { + $this->setUpTestPluginManager(); + + $validPluginPath = $this->pluginsDir . '/valid-plugin'; + $hiddenPluginPath = $this->pluginsDir . '/_hidden-plugin'; + $this->tempPluginDir = $this->pluginsDir; + + mkdir($validPluginPath, 0755, true); + mkdir($hiddenPluginPath, 0755, true); + + $composerJson = [ + 'name' => 'test/plugin', + 'description' => 'Test plugin', + 'autoload' => [ + 'psr-4' => [ + 'ProcessMaker\\Plugins\\Test\\' => 'src/', + ], + ], + ]; + + file_put_contents( + $validPluginPath . '/composer.json', + json_encode($composerJson, JSON_PRETTY_PRINT) + ); + file_put_contents( + $hiddenPluginPath . '/composer.json', + json_encode($composerJson, JSON_PRETTY_PRINT) + ); + + Process::fake(); + + $manager = new PluginManager(); + $plugins = $manager->list(); + $pluginsByName = collect($plugins)->keyBy('name'); + + $this->assertCount(2, $plugins); + $this->assertEquals('Enabled', $pluginsByName['valid-plugin']['enabled']); + $this->assertEquals('Disabled', $pluginsByName['hidden-plugin']['enabled']); + } + + public function testArtisanEnvironmentIncludesCurrentTenant() + { + config(['app.multitenancy' => true]); + $this->app->instance('currentTenant', (object) ['id' => 7]); + + $method = new \ReflectionMethod(PluginManager::class, 'artisanEnvironment'); + $environment = $method->invoke(new PluginManager()); + + $this->assertSame(['TENANT' => '7'], $environment); + } + + public function testToggleRebuildsRouteCacheAndRestartsSupervisor() + { + $this->setUpTestPluginManager(); + + $pluginName = 'toggle-plugin'; + $pluginPath = $this->pluginsDir . '/' . $pluginName; + $this->tempPluginDir = $this->pluginsDir . '/_' . $pluginName; + mkdir($pluginPath, 0755, true); + + Process::fake([ + '*' => Process::result('', exitCode: 0), + ]); + + $manager = new PluginManager(); + $enabled = $manager->toggle($pluginName); + + $this->assertFalse($enabled); + $this->assertFalse(is_dir($pluginPath)); + $this->assertTrue(is_dir($this->pluginsDir . '/_' . $pluginName)); + + $this->assertProcessRanCommand('route:cache'); + $this->assertRuntimeReloadPublished(); + } + + public function testToggleEnableRestartsSupervisor() + { + $this->setUpTestPluginManager(); + + $pluginName = 'toggle-plugin'; + $disabledPath = $this->pluginsDir . '/_' . $pluginName; + $this->tempPluginDir = $this->pluginsDir . '/' . $pluginName; + mkdir($disabledPath, 0755, true); + + Process::fake([ + '*' => Process::result('', exitCode: 0), + ]); + + $manager = new PluginManager(); + $enabled = $manager->toggle($pluginName); + + $this->assertTrue($enabled); + $this->assertTrue(is_dir($this->pluginsDir . '/' . $pluginName)); + $this->assertRuntimeReloadPublished(); + } + + public function testScheduleSupervisorRestartUsesConfigFileWhenSet(): void + { + config([ + 'plugin_manager.supervisorctl_bin' => 'supervisorctl', + 'plugin_manager.supervisorctl_config' => '/tmp/processmaker-supervisord.conf', + 'plugin_manager.supervisor_restart_target' => 'all', + 'plugin_manager.supervisor_restart_delay' => 5, + ]); + + Process::fake([ + '*' => Process::result('', exitCode: 0), + ]); + + $this->assertTrue((new PluginRuntimeReload())->scheduleSupervisorRestart()); + $this->assertSupervisorRestartRan('/tmp/processmaker-supervisord.conf'); + } + + public function testLocalInstallReplacesLeftoverSymlinkWithCopy(): void + { + $this->setUpTestPluginManager(); + + $pluginName = 'local-symlink-plugin-' . uniqid(); + $sourceDir = sys_get_temp_dir() . '/' . $pluginName; + $pluginPath = $this->pluginsDir . '/' . $pluginName; + + mkdir($sourceDir, 0755, true); + mkdir($sourceDir . '/src', 0755, true); + file_put_contents($sourceDir . '/composer.json', json_encode([ + 'name' => 'processmaker/' . $pluginName, + 'autoload' => [ + 'psr-4' => [ + 'ProcessMaker\\Plugins\\SymlinkTest\\' => 'src/', + ], + ], + ], JSON_PRETTY_PRINT)); + file_put_contents($sourceDir . '/source-only.marker', 'source'); + + $this->assertTrue(symlink($sourceDir, $pluginPath)); + $this->assertTrue(is_link($pluginPath)); + + Process::fake([ + '*' => Process::result('', exitCode: 0), + ]); + Event::fake([PluginLog::class]); + + $manager = new PluginManager(); + $manager->install($sourceDir, null, null, 1); + + $this->assertFalse(is_link($pluginPath)); + $this->assertTrue(is_dir($pluginPath)); + $this->assertFileExists($pluginPath . '/source-only.marker'); + + if (is_dir($pluginPath)) { + $this->deleteDirectory($pluginPath); + } + if (is_dir($sourceDir)) { + $this->deleteDirectory($sourceDir); + } + } + + public function testRunCommandLogsSuccessfulOutputOnce() + { + Process::fake([ + '*' => Process::result("Routes cached successfully.\n", exitCode: 0), + ]); + Event::fake([PluginLog::class]); + + $method = new \ReflectionMethod(PluginManager::class, 'runCommand'); + $method->invoke(new PluginManager(), 'route:cache', 'test-plugin', 1); + + Event::assertDispatchedTimes(PluginLog::class, 1); + Event::assertDispatched(PluginLog::class, function (PluginLog $event) { + return str_contains($event->message, 'Plugin route:cache command output'); + }); + } + + public function testStripEmbeddedCredentialsFromHttpUrlRemovesTokenUserinfo() + { + $manager = new PluginManager(); + $method = new \ReflectionMethod($manager, 'stripEmbeddedCredentialsFromHttpUrl'); + $method->setAccessible(true); + + $this->assertSame( + 'https://github.com/org/repo.git', + $method->invoke($manager, 'https://ghp_xxx@github.com/org/repo.git') + ); + $this->assertSame( + 'https://github.com/org/repo.git', + $method->invoke($manager, 'https://user:secret@github.com/org/repo.git') + ); + } + + public function testRedactHttpUserInfoFromString() + { + $dirty = 'clone failed for https://ghp_xxx@github.com/org/repo.git'; + $this->assertSame( + 'clone failed for https://github.com/org/repo.git', + PluginManager::redactHttpUserInfoFromString($dirty) + ); + } + + public function testCloneRepositorySetsStrippedOriginAfterSuccess() + { + $this->setUpTestPluginManager(); + + $dest = $this->pluginsDir . '/clone-origin-test'; + mkdir($dest, 0755, true); + + Process::fake([ + '*' => Process::result('', exitCode: 0), + ]); + + $manager = new PluginManager(); + $method = new \ReflectionMethod($manager, 'cloneRepository'); + $method->setAccessible(true); + + $authUrl = 'https://ghp_xxx@github.com/org/repo.git'; + $canonical = 'https://github.com/org/repo.git'; + + $method->invoke($manager, $authUrl, $dest, 'repo', null, $canonical); + + Process::assertRan(fn ($process) => is_array($process->command) + && $process->command === ['git', 'clone', $authUrl, $dest]); + Process::assertRan(fn ($process) => is_array($process->command) + && $process->command === ['git', 'remote', 'set-url', 'origin', $canonical]); + } + + private function assertProcessRanCommand(string $artisanCommand): void + { + Process::assertRan(function ($process) use ($artisanCommand) { + $command = is_array($process->command) + ? implode(' ', $process->command) + : (string) $process->command; + + return str_contains($command, $artisanCommand); + }); + } + + private function assertRuntimeReloadPublished(): void + { + $this->runtimeReload->shouldHaveReceived('publish')->atLeast()->once(); + } + + private function assertSupervisorRestartRan(?string $configFile = null): void + { + $bin = config('plugin_manager.supervisorctl_bin', 'supervisorctl'); + $target = config('plugin_manager.supervisor_restart_target', 'all'); + $configFile = $configFile ?? config('plugin_manager.supervisorctl_config'); + + $expected = [$bin]; + if ($configFile) { + $expected[] = '-c'; + $expected[] = $configFile; + } + $expected[] = 'restart'; + $expected[] = $target; + + Process::assertRan(function ($process) use ($expected, $configFile) { + if (!is_array($process->command)) { + return false; + } + + $command = implode(' ', $process->command); + + if (!str_contains($command, 'setsid') || !str_contains($command, 'nohup')) { + return false; + } + + foreach ($expected as $part) { + if (!str_contains($command, $part)) { + return false; + } + } + + return $configFile === null || str_contains($command, (string) $configFile); + }); + } + + private function processRunsGitCommand($process, array $expectedCommand): bool + { + if (!is_array($process->command)) { + return false; + } + + return $process->command === $expectedCommand; + } + + private function deleteDirectory($dir) + { + if (!is_dir($dir)) { + return; + } + + $files = array_diff(scandir($dir), ['.', '..']); + foreach ($files as $file) { + $path = $dir . '/' . $file; + if (is_dir($path)) { + $this->deleteDirectory($path); + } else { + unlink($path); + } + } + rmdir($dir); + } +} diff --git a/tests/Managers/ScreenBuilderManagerTest.php b/tests/Managers/ScreenBuilderManagerTest.php new file mode 100644 index 0000000000..ad1ba3fc05 --- /dev/null +++ b/tests/Managers/ScreenBuilderManagerTest.php @@ -0,0 +1,214 @@ + */ + private array $pluginDirsToRemove = []; + + /** @var list */ + private array $publicAssetPaths = []; + + /** @var list */ + private array $tenantPublicAssetPaths = []; + + protected function setUp(): void + { + chdir(realpath(__DIR__ . '/../..')); + parent::setUp(); + + $this->pluginsDir = storage_path('plugins'); + if (!is_dir($this->pluginsDir)) { + mkdir($this->pluginsDir, 0755, true); + } + } + + protected function tearDown(): void + { + foreach ($this->pluginDirsToRemove as $dir) { + if (is_dir($dir)) { + $this->deleteDirectory($dir); + } + } + foreach ($this->publicAssetPaths as $path) { + if (is_file($path)) { + unlink($path); + } + } + foreach ($this->tenantPublicAssetPaths as $path) { + if (is_dir($path)) { + $this->deleteDirectory($path); + } elseif (is_file($path)) { + unlink($path); + } + } + + chdir(realpath(__DIR__ . '/../..')); + + parent::tearDown(); + } + + public function testStoragePluginScreenBuilderScripts(): void + { + $enabledPackage = 'test-plg-plugin-' . uniqid(); + $enabledDir = $this->pluginsDir . '/' . $enabledPackage; + $this->pluginDirsToRemove[] = $enabledDir; + mkdir($enabledDir, 0755, true); + file_put_contents($enabledDir . '/composer.json', json_encode([ + 'name' => 'processmaker/' . $enabledPackage, + ])); + $this->touchPublicAsset($enabledPackage, 'screen-builder-form-components.js'); + + $manager = new ScreenBuilderManager(); + $manager->addPackageScripts('FORM'); + $scripts = $manager->getScripts(); + $this->assertTrue($this->scriptsContainPath( + $scripts, + '/vendor/processmaker/packages/' . $enabledPackage . '/js/screen-builder-form-components.js' + )); + + $disabledPackage = 'disabled-plg-plugin-' . uniqid(); + $disabledDir = $this->pluginsDir . '/_' . $disabledPackage; + $this->pluginDirsToRemove[] = $disabledDir; + mkdir($disabledDir, 0755, true); + file_put_contents($disabledDir . '/composer.json', json_encode([ + 'name' => 'processmaker/' . $disabledPackage, + ])); + $this->touchPublicAsset($disabledPackage, 'screen-builder-form-components.js'); + + $managerDisabled = new ScreenBuilderManager(); + $managerDisabled->addPackageScripts('FORM'); + $disabledScripts = $managerDisabled->getScripts(); + $this->assertFalse($this->scriptsContainPath( + $disabledScripts, + '/vendor/processmaker/packages/' . $disabledPackage . '/js/screen-builder-form-components.js' + )); + + $displayOnlyPackage = 'test-plg-display-only-' . uniqid(); + $displayDir = $this->pluginsDir . '/' . $displayOnlyPackage; + $this->pluginDirsToRemove[] = $displayDir; + mkdir($displayDir, 0755, true); + file_put_contents($displayDir . '/composer.json', json_encode([ + 'name' => 'processmaker/' . $displayOnlyPackage, + ])); + $this->touchPublicAsset($displayOnlyPackage, 'screen-builder-form-components.js'); + + $managerDisplay = new ScreenBuilderManager(); + $managerDisplay->addPackageScripts('DISPLAY'); + $displayScripts = $managerDisplay->getScripts(); + $this->assertFalse($this->scriptsContainPath( + $displayScripts, + '/vendor/processmaker/packages/' . $displayOnlyPackage . '/js/screen-builder-form-components.js' + )); + + $folderName = 'my-plugin-folder-' . uniqid(); + $resolvedName = 'resolved-plg-name-' . uniqid(); + $folderDir = $this->pluginsDir . '/' . $folderName; + $this->pluginDirsToRemove[] = $folderDir; + mkdir($folderDir, 0755, true); + file_put_contents($folderDir . '/composer.json', json_encode([ + 'name' => 'processmaker/' . $resolvedName, + ])); + $this->touchPublicAsset($resolvedName, 'screen-builder-form-components.js'); + + $managerResolved = new ScreenBuilderManager(); + $managerResolved->addPackageScripts('FORM'); + $resolvedScripts = $managerResolved->getScripts(); + $this->assertTrue($this->scriptsContainPath( + $resolvedScripts, + '/vendor/processmaker/packages/' . $resolvedName . '/js/screen-builder-form-components.js' + )); + + config(['app.multitenancy' => true]); + config(['plugin_manager.tenant_plugin_assets_enabled' => true]); + $this->app->instance('currentTenant', (object) ['id' => 7]); + + $tenantPackage = 'tenant-sb-plugin-' . uniqid(); + $tenantDir = $this->pluginsDir . '/' . $tenantPackage; + $this->pluginDirsToRemove[] = $tenantDir; + mkdir($tenantDir, 0755, true); + file_put_contents($tenantDir . '/composer.json', json_encode([ + 'name' => 'processmaker/' . $tenantPackage, + ])); + $this->touchTenantPublicAsset(7, $tenantPackage, 'screen-builder-form-components.js'); + + $managerTenant = new ScreenBuilderManager(); + $managerTenant->addPackageScripts('FORM'); + $tenantScripts = $managerTenant->getScripts(); + $tenantWebPath = PluginVendorAssets::webPath( + 7, + $tenantPackage, + '/js/screen-builder-form-components.js' + ); + $this->assertTrue($this->scriptsContainPath($tenantScripts, $tenantWebPath)); + foreach ($tenantScripts as $entry) { + if (str_starts_with($entry, $tenantWebPath)) { + $this->assertStringContainsString('?t=', $entry); + break; + } + } + } + + private function touchTenantPublicAsset(int $tenantId, string $packageName, string $fileName): string + { + $dir = PluginVendorAssets::tenantPublicRoot($tenantId, $packageName) . '/js'; + if (!is_dir($dir)) { + mkdir($dir, 0755, true); + } + $path = $dir . '/' . $fileName; + file_put_contents($path, '// tenant test asset'); + $this->tenantPublicAssetPaths[] = PluginVendorAssets::tenantPublicRoot($tenantId, $packageName); + + return $path; + } + + private function touchPublicAsset(string $packageName, string $fileName): string + { + $dir = public_path('vendor/processmaker/packages/' . $packageName . '/js'); + if (!is_dir($dir)) { + mkdir($dir, 0755, true); + } + $path = $dir . '/' . $fileName; + file_put_contents($path, '// test asset'); + $this->publicAssetPaths[] = $path; + + return $path; + } + + private function scriptsContainPath(array $scripts, string $path): bool + { + foreach ($scripts as $script) { + if (str_starts_with($script, $path)) { + return true; + } + } + + return false; + } + + private function deleteDirectory(string $directory): void + { + if (!is_dir($directory)) { + return; + } + + $files = array_diff(scandir($directory), ['.', '..']); + foreach ($files as $file) { + $path = $directory . '/' . $file; + if (is_dir($path) && !is_link($path)) { + $this->deleteDirectory($path); + } else { + unlink($path); + } + } + + rmdir($directory); + } +} diff --git a/tests/Model/PluginTest.php b/tests/Model/PluginTest.php new file mode 100644 index 0000000000..f0dac6c99c --- /dev/null +++ b/tests/Model/PluginTest.php @@ -0,0 +1,141 @@ +tempDir = sys_get_temp_dir() . '/plugin-test-' . uniqid(); + mkdir($this->tempDir, 0755, true); + } + + public function tearDownTestPlugin() + { + if ($this->tempDir && is_dir($this->tempDir)) { + $this->deleteDirectory($this->tempDir); + } + } + + public function testFromPathCreatesPluginInstance() + { + $this->setUpTestPlugin(); + $plugin = Plugin::fromPath($this->tempDir); + + $this->assertInstanceOf(Plugin::class, $plugin); + $this->assertEquals(basename($this->tempDir), $plugin->getName()); + $this->assertEquals($this->tempDir, $plugin->getPath()); + } + + public function testFromPathThrowsExceptionForNonExistentDirectory() + { + $this->expectException(RuntimeException::class); + Plugin::fromPath('/non/existent/path'); + } + + public function testGetComposerJsonReadsAndParsesFile() + { + $this->setUpTestPlugin(); + $composerJson = [ + 'name' => 'test/plugin', + 'description' => 'Test plugin', + 'autoload' => [ + 'psr-4' => [ + 'ProcessMaker\\Plugins\\Test\\' => 'src/', + ], + ], + ]; + + file_put_contents( + $this->tempDir . '/composer.json', + json_encode($composerJson, JSON_PRETTY_PRINT) + ); + + $plugin = Plugin::fromPath($this->tempDir); + $result = $plugin->getComposerJson(); + + $this->assertEquals($composerJson, $result); + } + + public function testGetComposerJsonThrowsExceptionForMissingFile() + { + $this->setUpTestPlugin(); + $plugin = Plugin::fromPath($this->tempDir); + + $this->expectException(RuntimeException::class); + $plugin->getComposerJson(); + } + + public function testGetComposerJsonThrowsExceptionForInvalidJson() + { + $this->setUpTestPlugin(); + file_put_contents($this->tempDir . '/composer.json', '{ invalid json }'); + + $plugin = Plugin::fromPath($this->tempDir); + + $this->expectException(RuntimeException::class); + $plugin->getComposerJson(); + } + + public function testGetDescriptionReturnsDescriptionFromComposerJson() + { + $this->setUpTestPlugin(); + $composerJson = [ + 'name' => 'test/plugin', + 'description' => 'Test plugin description', + ]; + + file_put_contents( + $this->tempDir . '/composer.json', + json_encode($composerJson, JSON_PRETTY_PRINT) + ); + + $plugin = Plugin::fromPath($this->tempDir); + $this->assertEquals('Test plugin description', $plugin->getDescription()); + } + + public function testGetDescriptionReturnsNullWhenMissing() + { + $this->setUpTestPlugin(); + $composerJson = ['name' => 'test/plugin']; + + file_put_contents( + $this->tempDir . '/composer.json', + json_encode($composerJson, JSON_PRETTY_PRINT) + ); + + $plugin = Plugin::fromPath($this->tempDir); + $this->assertNull($plugin->getDescription()); + } + + public function testGetNameReturnsDirectoryName() + { + $this->setUpTestPlugin(); + $plugin = Plugin::fromPath($this->tempDir); + $this->assertEquals(basename($this->tempDir), $plugin->getName()); + } + + private function deleteDirectory($dir) + { + if (!is_dir($dir)) { + return; + } + + $files = array_diff(scandir($dir), ['.', '..']); + foreach ($files as $file) { + $path = $dir . '/' . $file; + if (is_dir($path)) { + $this->deleteDirectory($path); + } else { + unlink($path); + } + } + rmdir($dir); + } +} diff --git a/tests/ScriptExecutors/BuildScriptExecutorsTest.php b/tests/ScriptExecutors/BuildScriptExecutorsTest.php index 91597623cd..00a2096999 100644 --- a/tests/ScriptExecutors/BuildScriptExecutorsTest.php +++ b/tests/ScriptExecutors/BuildScriptExecutorsTest.php @@ -8,7 +8,6 @@ class BuildScriptExecutorsTest extends TestCase { - public function testBuildScriptExecutorsDockerfile() { $scriptExecutor = ScriptExecutor::factory()->create([ @@ -19,7 +18,7 @@ public function testBuildScriptExecutorsDockerfile() $code = $builder->getDockerfileContent($scriptExecutor); // Check the $code contains 'WORKDIR /opt/executor/src' - $expectedCode = << true]); + config(['plugin_manager.tenant_plugin_assets_enabled' => true]); + $this->app->instance('currentTenant', (object) ['id' => 6]); + + $pluginDir = storage_path('plugins/pm-tenant-assets-' . uniqid()); + mkdir($pluginDir, 0755, true); + file_put_contents($pluginDir . '/composer.json', json_encode([ + 'name' => 'processmaker/tenant-assets-plugin', + ])); + mkdir($pluginDir . '/public/js', 0755, true); + file_put_contents($pluginDir . '/public/js/app.js', '// published by install hook'); + + $manager = new PluginManager(); + $publish = new ReflectionMethod($manager, 'publishTenantPluginAssetsIfApplicable'); + $publish->invoke($manager, $pluginDir, 'tenant-assets-plugin', null); + + $published = PluginVendorAssets::tenantPublicRoot(6, 'tenant-assets-plugin') . '/js/app.js'; + $this->assertFileExists($published); + + $remove = new ReflectionMethod($manager, 'removeTenantPluginAssetsIfApplicable'); + $remove->invoke($manager, $pluginDir, 'tenant-assets-plugin', null); + $this->assertFileDoesNotExist($published); + + config(['app.multitenancy' => false]); + $offDir = storage_path('plugins/pm-tenant-assets-off-' . uniqid()); + mkdir($offDir . '/public/js', 0755, true); + file_put_contents($offDir . '/composer.json', json_encode([ + 'name' => 'processmaker/off-tenant-plugin', + ])); + file_put_contents($offDir . '/public/js/app.js', '// noop'); + $publish->invoke($manager, $offDir, 'off-tenant-plugin', null); + $this->assertFileDoesNotExist( + PluginVendorAssets::tenantPublicRoot(6, 'off-tenant-plugin') . '/js/app.js' + ); + + $this->deleteDirectory($pluginDir); + $this->deleteDirectory($offDir); + } + + private function deleteDirectory(string $dir): void + { + if (!is_dir($dir)) { + return; + } + foreach (scandir($dir) ?: [] as $entry) { + if ($entry === '.' || $entry === '..') { + continue; + } + $path = $dir . DIRECTORY_SEPARATOR . $entry; + if (is_dir($path)) { + $this->deleteDirectory($path); + } else { + unlink($path); + } + } + rmdir($dir); + } +} diff --git a/tests/unit/Managers/TenantPluginAssetPublisherTest.php b/tests/unit/Managers/TenantPluginAssetPublisherTest.php new file mode 100644 index 0000000000..b0c21a065e --- /dev/null +++ b/tests/unit/Managers/TenantPluginAssetPublisherTest.php @@ -0,0 +1,71 @@ + 'processmaker/test-tenant-pkg', + ])); + + $publisher = new TenantPluginAssetPublisher(); + + $this->assertFalse($publisher->publishPlugin($pluginDir, $tenantId)); + $this->assertDirectoryDoesNotExist($publishedRoot); + + $publicDir = $pluginDir . '/public'; + mkdir($publicDir . '/js', 0755, true); + file_put_contents($publicDir . '/js/app.js', 'console.log("tenant-pkg");'); + file_put_contents($publicDir . '/mix-manifest.json', '{"\/js/app.js":"\/js/app.js?id=1"}'); + + $this->assertTrue($publisher->publishPlugin($pluginDir, $tenantId)); + $this->assertFileExists($publishedRoot . '/js/app.js'); + $this->assertFileExists($publishedRoot . '/mix-manifest.json'); + $this->assertSame( + 'console.log("tenant-pkg");', + file_get_contents($publishedRoot . '/js/app.js') + ); + + file_put_contents($publicDir . '/js/app.js', 'console.log("updated");'); + $this->assertTrue($publisher->publishPlugin($pluginDir, $tenantId)); + $this->assertSame( + 'console.log("updated");', + file_get_contents($publishedRoot . '/js/app.js') + ); + + $publisher->removePublishedPlugin($pluginDir, $tenantId); + $this->assertDirectoryDoesNotExist($publishedRoot); + + $this->deleteDirectory($pluginDir); + } + + private function deleteDirectory(string $dir): void + { + if (!is_dir($dir)) { + return; + } + foreach (scandir($dir) ?: [] as $entry) { + if ($entry === '.' || $entry === '..') { + continue; + } + $path = $dir . DIRECTORY_SEPARATOR . $entry; + if (is_dir($path)) { + $this->deleteDirectory($path); + } else { + unlink($path); + } + } + rmdir($dir); + } +} diff --git a/tests/unit/Mcp/ProcessBpmnValidatorPortableRefsTest.php b/tests/unit/Mcp/ProcessBpmnValidatorPortableRefsTest.php new file mode 100644 index 0000000000..919648c27a --- /dev/null +++ b/tests/unit/Mcp/ProcessBpmnValidatorPortableRefsTest.php @@ -0,0 +1,95 @@ + + + + + node_4 + + + node_4 + node_5 + + + node_5 + + + + + + + + + + + + + + +XML; + + $result = (new ProcessBpmnValidator())->validate($bpmn); + + $this->assertFalse($result['ok']); + $codes = array_column($result['errors'], 'code'); + $this->assertContains('unresolved_screen_ref', $codes); + } + + public function testNumericScreenRefPassesPortableCheck(): void + { + $bpmn = <<<'XML' + + + + + node_4 + + + node_4 + node_5 + + + node_5 + + + + + + + + + + + + + + +XML; + + $result = (new ProcessBpmnValidator())->validate($bpmn); + $codes = array_column($result['errors'], 'code'); + $this->assertNotContains('unresolved_screen_ref', $codes); + } +} diff --git a/tests/unit/MetricsServiceTest.php b/tests/unit/MetricsServiceTest.php index c215543b72..9b02765e0a 100644 --- a/tests/unit/MetricsServiceTest.php +++ b/tests/unit/MetricsServiceTest.php @@ -15,7 +15,6 @@ use ReflectionClass; use Tests\TestCase; - class MetricsServiceTest extends TestCase { /** @@ -47,7 +46,7 @@ public function testCounterRegistrationAndIncrement(): void $counter = $this->metricsService->counter('test_counter', 'Test Counter', ['label1']); // Assert the counter is registered - $this->assertInstanceOf(\Prometheus\Counter::class, $counter); + $this->assertInstanceOf(Counter::class, $counter); // Increment the counter and assert the value $counter->inc(['value1']); @@ -86,7 +85,7 @@ public function testHistogramRegistrationAndObserve(): void ); // Assert the histogram is registered - $this->assertInstanceOf(\Prometheus\Histogram::class, $histogram); + $this->assertInstanceOf(Histogram::class, $histogram); // Observe a value and assert it is recorded $histogram->observe(0.5, ['value1']); @@ -115,7 +114,7 @@ public function testDefaultNamespace(): void $counter = $this->metricsService->counter('namespace_test'); // Assert default namespace is applied - $this->assertInstanceOf(\Prometheus\Counter::class, $counter); + $this->assertInstanceOf(Counter::class, $counter); $counter->inc(); $samples = $this->metricsService->renderMetrics(); @@ -134,6 +133,7 @@ public function testSetGaugeValue(): void $this->assertStringContainsString('test_set_gauge', $samples); $this->assertStringContainsString('5', $samples); } + /** * Test that counterInc calls Metrics::counter() and then inc() with the correct labels. */ diff --git a/tests/unit/ProcessMaker/Nayra/Services/FixBpmnSchemaServiceTest.php b/tests/unit/ProcessMaker/Nayra/Services/FixBpmnSchemaServiceTest.php index 2b3ec5adaf..c334cf5f30 100644 --- a/tests/unit/ProcessMaker/Nayra/Services/FixBpmnSchemaServiceTest.php +++ b/tests/unit/ProcessMaker/Nayra/Services/FixBpmnSchemaServiceTest.php @@ -22,7 +22,7 @@ public function testExceptionInIncompleteProcess() { $bpmn = file_get_contents( __DIR__ . - "/../../../../Fixtures/process_data_input_without_targetref.bpmn" + '/../../../../Fixtures/process_data_input_without_targetref.bpmn' ); $document = new BpmnDocument(); @@ -30,7 +30,7 @@ public function testExceptionInIncompleteProcess() $this->expectException(Exception::class); $validation = $document->validateBPMNSchema( - public_path("definitions/ProcessMaker.xsd") + public_path('definitions/ProcessMaker.xsd') ); } @@ -43,7 +43,7 @@ public function testFixIncompleteProcess() { $bpmn = file_get_contents( __DIR__ . - "/../../../../Fixtures/process_data_input_without_targetref.bpmn" + '/../../../../Fixtures/process_data_input_without_targetref.bpmn' ); $fixBpmnSchemaService = app(FixBpmnSchemaService::class); @@ -52,7 +52,7 @@ public function testFixIncompleteProcess() $document = new BpmnDocument(); $document->loadXML($bpmn); $validation = $document->validateBPMNSchema( - public_path("definitions/ProcessMaker.xsd") + public_path('definitions/ProcessMaker.xsd') ); $this->assertTrue($validation); @@ -67,7 +67,7 @@ public function testFixPm4Process() { $bpmn = file_get_contents( __DIR__ . - "/../../../../Fixtures/process_data_input_generated_in_pm4.bpmn" + '/../../../../Fixtures/process_data_input_generated_in_pm4.bpmn' ); $fixBpmnSchemaService = app(FixBpmnSchemaService::class); @@ -76,7 +76,7 @@ public function testFixPm4Process() $document = new BpmnDocument(); $document->loadXML($bpmn); $validation = $document->validateBPMNSchema( - public_path("definitions/ProcessMaker.xsd") + public_path('definitions/ProcessMaker.xsd') ); $this->assertTrue($validation); diff --git a/tests/unit/ProcessMaker/Providers/PluginServiceProviderTest.php b/tests/unit/ProcessMaker/Providers/PluginServiceProviderTest.php new file mode 100644 index 0000000000..9b5f22a618 --- /dev/null +++ b/tests/unit/ProcessMaker/Providers/PluginServiceProviderTest.php @@ -0,0 +1,42 @@ + [ + 'laravel' => [ + 'providers' => [ + 'ProcessMaker\\Package\\Missing\\PackageServiceProvider', + ], + ], + ], + ])); + + try { + $provider = new PluginServiceProvider(app()); + $method = new ReflectionMethod($provider, 'loadPlugin'); + $method->invoke($provider, $pluginPath); + + $this->assertNull(app()->getProvider('ProcessMaker\\Package\\Missing\\PackageServiceProvider')); + Log::shouldHaveReceived('warning') + ->once() + ->withArgs(fn (string $message) => str_contains($message, 'dependencies are not installed')); + } finally { + @unlink($pluginPath . '/composer.json'); + @rmdir($pluginPath); + } + } +} diff --git a/tests/unit/ProcessMaker/ScriptRunners/SmartExtractEnvironmentVariablesTest.php b/tests/unit/ProcessMaker/ScriptRunners/SmartExtractEnvironmentVariablesTest.php index 1349a02ee4..03e84a2416 100644 --- a/tests/unit/ProcessMaker/ScriptRunners/SmartExtractEnvironmentVariablesTest.php +++ b/tests/unit/ProcessMaker/ScriptRunners/SmartExtractEnvironmentVariablesTest.php @@ -22,7 +22,7 @@ public function test_local_runner_preserves_database_api_host_without_duplicates { $this->createApiHost(); $executor = ScriptExecutor::factory()->create(['language' => 'php']); - $runner = new class ($executor) extends Base { + $runner = new class($executor) extends Base { public function config($code, array $dockerConfig) { return $dockerConfig; @@ -66,7 +66,7 @@ public function test_both_runners_propagate_the_legacy_api_host_fallback(): void config(['smart-extract.api_host' => 'https://legacy.example.com']); $executor = ScriptExecutor::factory()->create(['language' => 'php']); - $localRunner = new class ($executor) extends Base { + $localRunner = new class($executor) extends Base { public function config($code, array $dockerConfig) { return $dockerConfig; @@ -106,7 +106,7 @@ public function test_existing_empty_database_api_host_suppresses_fallback_in_bot ]); $executor = ScriptExecutor::factory()->create(['language' => 'php']); - $localRunner = new class ($executor) extends Base { + $localRunner = new class($executor) extends Base { public function config($code, array $dockerConfig) { return $dockerConfig; diff --git a/tests/unit/ProcessMaker/Services/ConditionalRedirectServiceTest.php b/tests/unit/ProcessMaker/Services/ConditionalRedirectServiceTest.php index ae5eac6cd9..5c2a04c9ee 100644 --- a/tests/unit/ProcessMaker/Services/ConditionalRedirectServiceTest.php +++ b/tests/unit/ProcessMaker/Services/ConditionalRedirectServiceTest.php @@ -3,9 +3,9 @@ namespace Tests\Unit\ProcessMaker\Services; use InvalidArgumentException; +use ProcessMaker\Models\Process; use ProcessMaker\Models\ProcessRequest; use ProcessMaker\Models\ProcessRequestToken; -use ProcessMaker\Models\Process; use ProcessMaker\Models\User; use ProcessMaker\Services\ConditionalRedirectService; use Tests\TestCase; @@ -29,13 +29,13 @@ public function testResolveWithBasicConditions() [ 'condition' => 'amount > 1000', 'type' => 'externalURL', - 'value' => 'https://example.com/approval' + 'value' => 'https://example.com/approval', ], [ 'condition' => 'amount <= 1000', 'type' => 'taskList', - 'value' => null - ] + 'value' => null, + ], ]; $data = ['amount' => 1500, 'status' => 'pending']; @@ -57,13 +57,13 @@ public function testResolveWithSecondConditionMatching() [ 'condition' => 'amount > 1000', 'type' => 'externalURL', - 'value' => 'https://example.com/approval' + 'value' => 'https://example.com/approval', ], [ 'condition' => 'amount <= 1000', 'type' => 'taskList', - 'value' => null - ] + 'value' => null, + ], ]; $data = ['amount' => 500, 'status' => 'pending']; @@ -85,13 +85,13 @@ public function testResolveWithNoConditionsMatching() [ 'condition' => 'amount > 1000', 'type' => 'externalURL', - 'value' => 'https://example.com/approval' + 'value' => 'https://example.com/approval', ], [ 'condition' => 'status == "urgent"', 'type' => 'taskList', - 'value' => null - ] + 'value' => null, + ], ]; $data = ['amount' => 500, 'status' => 'pending']; @@ -110,13 +110,13 @@ public function testResolveWithStringConditions() [ 'condition' => 'status == "urgent"', 'type' => 'externalURL', - 'value' => 'https://example.com/urgent' + 'value' => 'https://example.com/urgent', ], [ 'condition' => 'status == "normal"', 'type' => 'taskList', - 'value' => null - ] + 'value' => null, + ], ]; $data = ['status' => 'urgent', 'priority' => 'high']; @@ -137,13 +137,13 @@ public function testResolveWithBooleanConditions() [ 'condition' => 'isApproved == true', 'type' => 'homepageDashboard', - 'value' => null + 'value' => null, ], [ 'condition' => 'isApproved == false', 'type' => 'taskList', - 'value' => null - ] + 'value' => null, + ], ]; $data = ['isApproved' => true, 'user' => 'john']; @@ -164,13 +164,13 @@ public function testResolveWithComplexConditions() [ 'condition' => 'amount > 1000 and status == "pending"', 'type' => 'externalURL', - 'value' => 'https://example.com/approval' + 'value' => 'https://example.com/approval', ], [ 'condition' => 'amount <= 1000 or status == "urgent"', 'type' => 'taskList', - 'value' => null - ] + 'value' => null, + ], ]; $data = ['amount' => 1500, 'status' => 'pending']; @@ -206,8 +206,8 @@ public function testResolveWithMissingConditionKey() $conditionalRedirect = [ [ 'type' => 'externalURL', - 'value' => 'https://example.com/approval' - ] + 'value' => 'https://example.com/approval', + ], ]; $data = ['amount' => 1500]; @@ -227,8 +227,8 @@ public function testResolveWithNullCondition() [ 'condition' => null, 'type' => 'externalURL', - 'value' => 'https://example.com/approval' - ] + 'value' => 'https://example.com/approval', + ], ]; $data = ['amount' => 1500]; @@ -245,8 +245,8 @@ public function testResolveWithEmptyCondition() [ 'condition' => '', 'type' => 'externalURL', - 'value' => 'https://example.com/approval' - ] + 'value' => 'https://example.com/approval', + ], ]; $data = ['amount' => 1500]; @@ -266,21 +266,21 @@ public function testResolveWithNestedData() [ 'condition' => 'user.role == "admin"', 'type' => 'externalURL', - 'value' => 'https://example.com/admin' + 'value' => 'https://example.com/admin', ], [ 'condition' => 'user.role == "user"', 'type' => 'taskList', - 'value' => null - ] + 'value' => null, + ], ]; $data = [ 'user' => [ 'role' => 'admin', - 'name' => 'John Doe' + 'name' => 'John Doe', ], - 'status' => 'active' + 'status' => 'active', ]; $result = $this->service->resolve($conditionalRedirect, $data); @@ -300,7 +300,7 @@ public function testResolveWithTokenData() // Create a process $process = Process::factory()->create([ - 'user_id' => $user->id + 'user_id' => $user->id, ]); // Create a process request with test data @@ -310,8 +310,8 @@ public function testResolveWithTokenData() 'data' => [ 'amount' => 1500, 'status' => 'pending', - 'priority' => 'high' - ] + 'priority' => 'high', + ], ]); // Create a process request token @@ -321,20 +321,20 @@ public function testResolveWithTokenData() 'element_id' => 'task_1', 'element_type' => 'task', 'element_name' => 'Review Task', - 'status' => 'ACTIVE' + 'status' => 'ACTIVE', ]); $conditionalRedirect = [ [ 'condition' => 'amount > 1000', 'type' => 'externalURL', - 'value' => 'https://example.com/approval' + 'value' => 'https://example.com/approval', ], [ 'condition' => 'amount <= 1000', 'type' => 'taskList', - 'value' => null - ] + 'value' => null, + ], ]; // Get the data from the process request and test with resolve() method @@ -356,7 +356,7 @@ public function testResolveWithNoMatchingConditionsUsingTokenData() // Create a process $process = Process::factory()->create([ - 'user_id' => $user->id + 'user_id' => $user->id, ]); // Create a process request with data that won't match any conditions @@ -365,8 +365,8 @@ public function testResolveWithNoMatchingConditionsUsingTokenData() 'user_id' => $user->id, 'data' => [ 'amount' => 500, - 'status' => 'pending' - ] + 'status' => 'pending', + ], ]); // Create a process request token @@ -376,20 +376,20 @@ public function testResolveWithNoMatchingConditionsUsingTokenData() 'element_id' => 'task_1', 'element_type' => 'task', 'element_name' => 'Review Task', - 'status' => 'ACTIVE' + 'status' => 'ACTIVE', ]); $conditionalRedirect = [ [ 'condition' => 'amount > 1000', 'type' => 'externalURL', - 'value' => 'https://example.com/approval' + 'value' => 'https://example.com/approval', ], [ 'condition' => 'status == "urgent"', 'type' => 'taskList', - 'value' => null - ] + 'value' => null, + ], ]; // Get the data from the process request and test with resolve() method @@ -409,7 +409,7 @@ public function testResolveWithMultiInstanceTaskData() // Create a process $process = Process::factory()->create([ - 'user_id' => $user->id + 'user_id' => $user->id, ]); // Create a process request with multi-instance data @@ -418,8 +418,8 @@ public function testResolveWithMultiInstanceTaskData() 'user_id' => $user->id, 'data' => [ 'items' => ['item1', 'item2', 'item3'], - 'status' => 'processing' - ] + 'status' => 'processing', + ], ]); // Create a process request token with multi-instance properties @@ -433,22 +433,22 @@ public function testResolveWithMultiInstanceTaskData() 'token_properties' => [ 'data' => [ 'currentItem' => 'item1', - 'loopCounter' => 0 - ] - ] + 'loopCounter' => 0, + ], + ], ]); $conditionalRedirect = [ [ 'condition' => 'loopCounter == 0', 'type' => 'externalURL', - 'value' => 'https://example.com/first-item' + 'value' => 'https://example.com/first-item', ], [ 'condition' => 'loopCounter > 0', 'type' => 'taskList', - 'value' => null - ] + 'value' => null, + ], ]; // For multi-instance, we need to combine the process request data with token properties @@ -469,13 +469,13 @@ public function testResolveWithNumericConditions() [ 'condition' => 'count >= 5', 'type' => 'externalURL', - 'value' => 'https://example.com/bulk' + 'value' => 'https://example.com/bulk', ], [ 'condition' => 'count < 5', 'type' => 'taskList', - 'value' => null - ] + 'value' => null, + ], ]; $data = ['count' => 7, 'type' => 'batch']; @@ -496,13 +496,13 @@ public function testResolveWithDateConditions() [ 'condition' => 'dueDate < date("2024-01-01")', 'type' => 'externalURL', - 'value' => 'https://example.com/overdue' + 'value' => 'https://example.com/overdue', ], [ 'condition' => 'dueDate >= date("2024-01-01")', 'type' => 'taskList', - 'value' => null - ] + 'value' => null, + ], ]; $data = ['dueDate' => '2023-12-15', 'status' => 'pending']; @@ -523,13 +523,13 @@ public function testResolveWithMultipleConditionsInOneItem() [ 'condition' => 'amount > 1000 and status == "pending" and priority == "high"', 'type' => 'externalURL', - 'value' => 'https://example.com/urgent-approval' + 'value' => 'https://example.com/urgent-approval', ], [ 'condition' => 'amount <= 1000 or status = "completed"', 'type' => 'taskList', - 'value' => null - ] + 'value' => null, + ], ]; $data = ['amount' => 1500, 'status' => 'pending', 'priority' => 'high']; @@ -550,13 +550,13 @@ public function testResolveWithNullDataValues() [ 'condition' => 'value == null', 'type' => 'externalURL', - 'value' => 'https://example.com/null-value' + 'value' => 'https://example.com/null-value', ], [ 'condition' => 'value != null', 'type' => 'taskList', - 'value' => null - ] + 'value' => null, + ], ]; $data = ['value' => null, 'status' => 'pending']; @@ -577,13 +577,13 @@ public function testResolveWithArrayDataValues() [ 'condition' => '"urgent" in items', 'type' => 'externalURL', - 'value' => 'https://example.com/urgent-items' + 'value' => 'https://example.com/urgent-items', ], [ 'condition' => '"normal" in items', 'type' => 'taskList', - 'value' => null - ] + 'value' => null, + ], ]; $data = ['items' => ['urgent', 'normal', 'low'], 'status' => 'processing']; diff --git a/tests/unit/Providers/MixTenantPluginVendorTest.php b/tests/unit/Providers/MixTenantPluginVendorTest.php new file mode 100644 index 0000000000..6022c431bb --- /dev/null +++ b/tests/unit/Providers/MixTenantPluginVendorTest.php @@ -0,0 +1,81 @@ + true]); + config(['plugin_manager.tenant_plugin_assets_enabled' => true]); + $this->app->instance('currentTenant', (object) ['id' => 4]); + + $tenantPackage = 'mix-test-pkg'; + $tenantRoot = PluginVendorAssets::tenantPublicRoot(4, $tenantPackage); + mkdir($tenantRoot . '/js', 0755, true); + file_put_contents($tenantRoot . '/mix-manifest.json', json_encode([ + '/js/widget.js' => '/js/widget.js?id=tenant-hash-4', + ])); + + $globalRoot = PluginVendorAssets::globalPublicRoot($tenantPackage); + if (!is_dir(dirname($globalRoot))) { + mkdir(dirname($globalRoot), 0755, true); + } + if (!is_dir($globalRoot)) { + mkdir($globalRoot . '/js', 0755, true); + } + file_put_contents($globalRoot . '/mix-manifest.json', json_encode([ + '/js/widget.js' => '/js/widget.js?id=global-hash', + ])); + + $tenantUrl = (string) app(Mix::class)( + '/js/widget.js', + 'vendor/processmaker/packages/mix-test-pkg' + ); + $this->assertStringContainsString('/storage/tenant_4/vendor/processmaker/packages/mix-test-pkg', $tenantUrl); + $this->assertStringContainsString('tenant-hash-4', $tenantUrl); + $this->assertStringNotContainsString('global-hash', $tenantUrl); + + $this->deleteDirectory($tenantRoot); + + $fallbackPackage = 'mix-fallback-pkg'; + $fallbackGlobalRoot = PluginVendorAssets::globalPublicRoot($fallbackPackage); + mkdir($fallbackGlobalRoot . '/js', 0755, true); + file_put_contents($fallbackGlobalRoot . '/mix-manifest.json', json_encode([ + '/js/fallback.js' => '/js/fallback.js?id=global-only', + ])); + + $fallbackUrl = (string) app(Mix::class)( + '/js/fallback.js', + 'vendor/processmaker/packages/mix-fallback-pkg' + ); + $this->assertStringContainsString('/vendor/processmaker/packages/mix-fallback-pkg', $fallbackUrl); + $this->assertStringContainsString('global-only', $fallbackUrl); + + $this->deleteDirectory($globalRoot); + $this->deleteDirectory($fallbackGlobalRoot); + } + + private function deleteDirectory(string $dir): void + { + if (!is_dir($dir)) { + return; + } + foreach (scandir($dir) ?: [] as $entry) { + if ($entry === '.' || $entry === '..') { + continue; + } + $path = $dir . DIRECTORY_SEPARATOR . $entry; + if (is_dir($path)) { + $this->deleteDirectory($path); + } else { + unlink($path); + } + } + rmdir($dir); + } +} diff --git a/tests/unit/ScriptMicroserviceServiceTest.php b/tests/unit/ScriptMicroserviceServiceTest.php index b2850ee79e..620f64042d 100644 --- a/tests/unit/ScriptMicroserviceServiceTest.php +++ b/tests/unit/ScriptMicroserviceServiceTest.php @@ -70,7 +70,7 @@ public function testHandlePreviewError() 'file' => 'Test error file', 'line' => 'Test line number', 'trace' => 'Test trace', - ] + ], ]); $this->service->handle($request); diff --git a/tests/unit/ScriptRunners/InProcessPhpRunnerTest.php b/tests/unit/ScriptRunners/InProcessPhpRunnerTest.php new file mode 100644 index 0000000000..430ab647eb --- /dev/null +++ b/tests/unit/ScriptRunners/InProcessPhpRunnerTest.php @@ -0,0 +1,85 @@ +withPersonalAccessClient(); + } + + public function testRunsPhpScriptAndReturnsArray(): void + { + $runner = new InProcessPhpRunner(); + $user = User::factory()->create(); + + $output = $runner->run( + ' $data["ping"], "cfg" => $config["mode"] ?? null];', + ['ping' => 'hello'], + ['mode' => 'test'], + 30, + $user + ); + + $this->assertSame('hello', $output['pong']); + $this->assertSame('test', $output['cfg']); + } + + public function testThrowsOnInvalidPhp(): void + { + $this->expectException(ScriptException::class); + + $runner = new InProcessPhpRunner(); + $runner->run( + 'create() + ); + } + + public function testRespectsTimeout(): void + { + $this->expectException(ScriptTimeoutException::class); + + $runner = new InProcessPhpRunner(); + $runner->run( + ' true];', + [], + [], + 1, + User::factory()->create() + ); + } + + public function testExposesApiHostAndTokenViaGetenv(): void + { + config(['app.docker_host_url' => 'https://pm.example.test']); + + $runner = new InProcessPhpRunner(); + $output = $runner->run( + ' getenv("API_HOST"), + "has_token" => is_string(getenv("API_TOKEN")) && getenv("API_TOKEN") !== "", + "app_url" => getenv("APP_URL"), + ];', + [], + [], + 30, + User::factory()->create() + ); + + $this->assertSame('https://pm.example.test/api/1.0', $output['host']); + $this->assertTrue($output['has_token']); + $this->assertSame('https://pm.example.test', $output['app_url']); + } +} diff --git a/tests/unit/ScriptRunners/ScriptRunnerInProcessTest.php b/tests/unit/ScriptRunners/ScriptRunnerInProcessTest.php new file mode 100644 index 0000000000..344ca83cec --- /dev/null +++ b/tests/unit/ScriptRunners/ScriptRunnerInProcessTest.php @@ -0,0 +1,103 @@ +withPersonalAccessClient(); + config([ + 'core-service-task.enabled' => true, + 'core-service-task.execution' => 'modules', + 'core-service-task.modules_enabled' => true, + // Ensure local runner path is available for non-in-process fallback construction + 'script-runner-microservice.enabled' => false, + 'script-runners.php.runner' => 'MockRunner', + ]); + } + + public function testRunsAllowInProcessScriptViaScriptRuntime(): void + { + $user = User::factory()->create(); + $script = Script::factory()->create([ + 'language' => 'php', + 'code' => ' "docker"];', + 'run_as_user_id' => $user->id, + 'allow_in_process' => true, + 'execution_mode' => Script::EXECUTION_MODE_MODULES, + 'timeout' => 30, + ]); + + $runner = new ScriptRunner($script); + $response = $runner->run( + ' "in-process", "ping" => $data["ping"]];', + ['ping' => '1'], + [], + 30, + $user, + 1, + [] + ); + + $this->assertSame('in-process', $response['output']['via']); + $this->assertSame('1', $response['output']['ping']); + } + + public function testSkipsInProcessWhenFlagDisabled(): void + { + config(['core-service-task.enabled' => false]); + + $user = User::factory()->create(); + $script = Script::factory()->create([ + 'language' => 'php', + 'code' => ' "stored"];', + 'run_as_user_id' => $user->id, + 'allow_in_process' => true, + ]); + + $runner = new ScriptRunner($script); + // MockRunner evals the passed $code + $response = $runner->run( + ' "mock"];', + [], + [], + 30, + $user, + 1, + [] + ); + + $this->assertSame('mock', $response['output']['via']); + } + + public function testSkipsInProcessWhenScriptNotAllowlisted(): void + { + $user = User::factory()->create(); + $script = Script::factory()->create([ + 'language' => 'php', + 'code' => ' "stored"];', + 'run_as_user_id' => $user->id, + 'allow_in_process' => false, + ]); + + $runner = new ScriptRunner($script); + $response = $runner->run( + ' "mock"];', + [], + [], + 30, + $user, + 1, + [] + ); + + $this->assertSame('mock', $response['output']['via']); + } +} diff --git a/tests/unit/ScriptRuntime/ScriptRuntimeTest.php b/tests/unit/ScriptRuntime/ScriptRuntimeTest.php new file mode 100644 index 0000000000..b02507efe5 --- /dev/null +++ b/tests/unit/ScriptRuntime/ScriptRuntimeTest.php @@ -0,0 +1,233 @@ +withPersonalAccessClient(); + // Fresh registry per test (clear Facade cache so bindings apply) + ScriptRuntime::clearResolvedInstances(); + $registry = new ScriptModuleRegistry(); + $runtime = new \ProcessMaker\ScriptRuntime\ScriptRuntime($registry); + $this->app->instance(ScriptModuleRegistry::class, $registry); + $this->app->instance(\ProcessMaker\ScriptRuntime\ScriptRuntime::class, $runtime); + $this->app->instance('script.runtime', $runtime); + } + + public function testRegisterModuleAndCatalog(): void + { + ScriptRuntime::registerModule(FakeEchoModule::class); + + $this->assertTrue(ScriptRuntime::registry()->has('echo')); + $catalog = ScriptRuntime::catalog(); + $this->assertSame('echo', $catalog[0]['key']); + $this->assertArrayHasKey('ping', $catalog[0]['methods']); + } + + public function testRunScriptWithModuleVariable(): void + { + ScriptRuntime::registerModule(FakeEchoModule::class); + $user = User::factory()->create(); + + $output = ScriptRuntime::run( + ' $echo->ping($data["ping"]), "keys" => array_keys($modules)];', + new ScriptExecutionContext( + data: ['ping' => 'hello'], + config: [], + user: $user, + source: 'preview', + ) + ); + + $this->assertSame('hello', $output['pong']); + $this->assertContains('echo', $output['keys']); + } + + public function testRunScriptWithLegacyResultVariable(): void + { + $output = ScriptRuntime::run( + ' $data["ping"]];', + new ScriptExecutionContext( + data: ['ping' => 'hello'], + config: [], + user: null, + source: 'preview', + ) + ); + + $this->assertSame(['pong' => 'hello'], $output); + } + + public function testLegacyResultIgnoresNestedReturns(): void + { + $output = ScriptRuntime::run( + <<<'PHP' + helper()]; +PHP, + new ScriptExecutionContext( + data: [], + config: [], + user: null, + source: 'preview', + ) + ); + + $this->assertSame(['ok' => true], $output); + } + + public function testLegacyResultIgnoresReturnsInsideInterpolatedStrings(): void + { + $output = ScriptRuntime::run( + <<<'PHP' + $helper('ok')]; +PHP, + new ScriptExecutionContext( + data: [], + config: [], + user: null, + source: 'preview', + ) + ); + + $this->assertSame(['value' => 'value: ok'], $output); + } + + public function testRunScriptWithExplicitScalarReturn(): void + { + $output = ScriptRuntime::run( + 'assertSame(1, $output); + } + + public function testRunScriptWithExplicitNullReturn(): void + { + $output = ScriptRuntime::run( + 'assertNull($output); + } + + public function testRejectsReservedModuleKey(): void + { + $this->expectException(\InvalidArgumentException::class); + ScriptRuntime::registerModule(FakeReservedModule::class); + } + + public function testNormalizeOutput(): void + { + $this->assertSame([], ScriptRuntime::normalizeOutput(null)); + $this->assertSame(['response' => 1], ScriptRuntime::normalizeOutput(1)); + $this->assertSame(['a' => 1], ScriptRuntime::normalizeOutput(['a' => 1])); + } + + public function testExposesApiHostAndTokenViaGetenv(): void + { + config(['app.docker_host_url' => 'https://pm.example.test']); + $user = User::factory()->create(); + + $output = ScriptRuntime::run( + ' getenv("API_HOST"), + "has_token" => is_string(getenv("API_TOKEN")) && getenv("API_TOKEN") !== "", + "app_url" => getenv("APP_URL"), + ];', + new ScriptExecutionContext( + data: [], + config: [], + user: $user, + source: 'preview', + ) + ); + + $this->assertSame('https://pm.example.test/api/1.0', $output['host']); + $this->assertTrue($output['has_token']); + $this->assertSame('https://pm.example.test', $output['app_url']); + // Token must not leak into the parent process after the run + $this->assertFalse(getenv('API_TOKEN')); + } +} + +class FakeEchoModule implements ScriptModuleInterface +{ + public static function key(): string + { + return 'echo'; + } + + public static function label(): string + { + return 'Echo'; + } + + public static function catalog(): array + { + return ['ping' => ['params' => ['value' => 'string']]]; + } + + public function boot(ScriptExecutionContext $context): void + { + } + + public function ping(string $value): string + { + return $value; + } +} + +class FakeReservedModule implements ScriptModuleInterface +{ + public static function key(): string + { + return 'data'; + } + + public static function label(): string + { + return 'Reserved'; + } + + public static function catalog(): array + { + return []; + } + + public function boot(ScriptExecutionContext $context): void + { + } +} diff --git a/tests/unit/ServiceTaskImplementations/CoreServiceTaskModulesTest.php b/tests/unit/ServiceTaskImplementations/CoreServiceTaskModulesTest.php new file mode 100644 index 0000000000..cbbf2b391d --- /dev/null +++ b/tests/unit/ServiceTaskImplementations/CoreServiceTaskModulesTest.php @@ -0,0 +1,85 @@ +withPersonalAccessClient(); + config([ + 'core-service-task.enabled' => true, + 'core-service-task.execution' => 'modules', + 'core-service-task.modules_enabled' => true, + ]); + + ScriptRuntime::clearResolvedInstances(); + $registry = new ScriptModuleRegistry(); + $runtime = new \ProcessMaker\ScriptRuntime\ScriptRuntime($registry); + $this->app->instance(ScriptModuleRegistry::class, $registry); + $this->app->instance(\ProcessMaker\ScriptRuntime\ScriptRuntime::class, $runtime); + $this->app->instance('script.runtime', $runtime); + + ScriptRuntime::registerModule(CoreTaskFakeModule::class); + } + + public function testRunsWithRegisteredModule(): void + { + $user = User::factory()->create(); + $script = Script::factory()->create([ + 'language' => 'php', + 'code' => ' $demo->value()];', + 'run_as_user_id' => $user->id, + 'allow_in_process' => true, + 'execution_mode' => Script::EXECUTION_MODE_MODULES, + 'timeout' => 30, + ]); + + $output = (new CoreServiceTask())->run( + [], + ['script_id' => $script->id], + 'tok', + 30 + ); + + $this->assertSame('ok', $output['out']); + } +} + +class CoreTaskFakeModule implements ScriptModuleInterface +{ + public static function key(): string + { + return 'demo'; + } + + public static function label(): string + { + return 'Demo'; + } + + public static function catalog(): array + { + return ['value' => []]; + } + + public function boot(ScriptExecutionContext $context): void + { + } + + public function value(): string + { + return 'ok'; + } +} diff --git a/tests/unit/ServiceTaskImplementations/CoreServiceTaskTest.php b/tests/unit/ServiceTaskImplementations/CoreServiceTaskTest.php new file mode 100644 index 0000000000..36b688f5c2 --- /dev/null +++ b/tests/unit/ServiceTaskImplementations/CoreServiceTaskTest.php @@ -0,0 +1,144 @@ +withPersonalAccessClient(); + config(['core-service-task.enabled' => true]); + } + + public function testIsRegistered(): void + { + $this->assertTrue( + WorkflowManager::existsServiceImplementation(CoreServiceTask::IMPLEMENTATION) + ); + } + + public function testRunsAllowlistedPhpScript(): void + { + $user = User::factory()->create(); + $script = Script::factory()->create([ + 'language' => 'php', + 'code' => ' $data["ping"]];', + 'run_as_user_id' => $user->id, + 'allow_in_process' => true, + 'timeout' => 30, + ]); + + $task = new CoreServiceTask(); + $output = $task->run( + ['ping' => '1'], + ['script_id' => $script->id], + 'token-1', + 30 + ); + + $this->assertSame('1', $output['pong']); + } + + public function testFailsWhenDisabled(): void + { + config(['core-service-task.enabled' => false]); + + $this->expectException(ConfigurationException::class); + $this->expectExceptionMessage('Core Service Task is disabled'); + + (new CoreServiceTask())->run([], ['script_id' => 1]); + } + + public function testFailsWithoutScriptId(): void + { + $this->expectException(ConfigurationException::class); + $this->expectExceptionMessage('script_id'); + + (new CoreServiceTask())->run([], []); + } + + public function testFailsWhenScriptNotAllowlisted(): void + { + $script = Script::factory()->create([ + 'language' => 'php', + 'code' => ' false, + ]); + + $this->expectException(ConfigurationException::class); + $this->expectExceptionMessage('not allowed for in-process'); + + (new CoreServiceTask())->run([], ['script_id' => $script->id]); + } + + public function testFailsForNonPhpScript(): void + { + $script = Script::factory()->create([ + 'language' => 'php', + 'code' => ' true, + ]); + + // Bypass model events to simulate a non-PHP allowlisted row + Script::whereKey($script->id)->update([ + 'language' => 'javascript', + 'allow_in_process' => true, + ]); + + $this->expectException(ConfigurationException::class); + $this->expectExceptionMessage('only supports PHP'); + + (new CoreServiceTask())->run([], ['script_id' => $script->id]); + } + + public function testRejectsAllowInProcessForNonPhpOnSave(): void + { + $script = Script::factory()->create([ + 'language' => 'php', + 'code' => ' false, + ]); + + $this->expectException(\Illuminate\Validation\ValidationException::class); + + $script->language = 'javascript'; + $script->allow_in_process = true; + $script->save(); + } + + public function testCapsTimeoutToMax(): void + { + config([ + 'core-service-task.max_timeout' => 2, + 'core-service-task.default_timeout' => 60, + // Timeout hard-kill is enforced by the bare PHP subprocess runner + 'core-service-task.execution' => 'bare', + ]); + + $user = User::factory()->create(); + $script = Script::factory()->create([ + 'language' => 'php', + 'code' => ' true];', + 'run_as_user_id' => $user->id, + 'allow_in_process' => true, + 'timeout' => 60, + ]); + + $this->expectException(\ProcessMaker\Exception\ScriptTimeoutException::class); + + (new CoreServiceTask())->run( + [], + ['script_id' => $script->id], + '', + 999 + ); + } +} diff --git a/tests/unit/Services/ScriptExecutionModeResolverTest.php b/tests/unit/Services/ScriptExecutionModeResolverTest.php new file mode 100644 index 0000000000..24f546e4d7 --- /dev/null +++ b/tests/unit/Services/ScriptExecutionModeResolverTest.php @@ -0,0 +1,80 @@ +create(); + + $this->assertSame( + Script::EXECUTION_MODE_BARE, + $script->fresh()->execution_mode + ); + } + + public function testDefaultsToBare(): void + { + config(['core-service-task.modules_enabled' => true]); + + $script = new Script([ + 'allow_in_process' => true, + 'execution_mode' => Script::EXECUTION_MODE_BARE, + ]); + + $this->assertSame( + Script::EXECUTION_MODE_BARE, + (new ScriptExecutionModeResolver())->resolve($script) + ); + } + + public function testAllowsModulesForExplicitlyConfiguredScript(): void + { + config(['core-service-task.modules_enabled' => true]); + + $script = new Script([ + 'allow_in_process' => true, + 'execution_mode' => Script::EXECUTION_MODE_MODULES, + ]); + + $this->assertSame( + Script::EXECUTION_MODE_MODULES, + (new ScriptExecutionModeResolver())->resolve($script) + ); + } + + public function testFallsBackToBareWhenModulesAreDisabled(): void + { + config(['core-service-task.modules_enabled' => false]); + + $script = new Script([ + 'allow_in_process' => true, + 'execution_mode' => Script::EXECUTION_MODE_MODULES, + ]); + + $this->assertSame( + Script::EXECUTION_MODE_BARE, + (new ScriptExecutionModeResolver())->resolve($script) + ); + } + + public function testDisallowsModulesForScriptsNotAllowedInProcess(): void + { + config(['core-service-task.modules_enabled' => true]); + + $script = new Script([ + 'allow_in_process' => false, + 'execution_mode' => Script::EXECUTION_MODE_MODULES, + ]); + + $this->assertSame( + Script::EXECUTION_MODE_BARE, + (new ScriptExecutionModeResolver())->resolve($script) + ); + } +} diff --git a/tests/unit/Support/PluginVendorAssetsTest.php b/tests/unit/Support/PluginVendorAssetsTest.php new file mode 100644 index 0000000000..fea35b4a18 --- /dev/null +++ b/tests/unit/Support/PluginVendorAssetsTest.php @@ -0,0 +1,107 @@ + 'processmaker/package-plg', + ])); + + $this->assertSame( + 'package-plg', + PluginVendorAssets::packageNameFromPluginPath($pluginDir, 'package-plg2') + ); + + $emptyDir = sys_get_temp_dir() . '/pm-plugin-vendor-assets-empty-' . uniqid(); + mkdir($emptyDir, 0755, true); + $this->assertSame( + 'my-folder', + PluginVendorAssets::packageNameFromPluginPath($emptyDir, 'my-folder') + ); + + $tenantId = 2; + $package = 'package-plg'; + $relative = PluginVendorAssets::tenantVendorRelativePath($tenantId, $package); + $this->assertSame('storage/tenant_2/vendor/processmaker/packages/package-plg', $relative); + $this->assertSame(public_path($relative), PluginVendorAssets::tenantPublicRoot($tenantId, $package)); + $this->assertSame('/' . $relative, PluginVendorAssets::webPrefix($tenantId, $package)); + + $this->assertSame( + '/storage/tenant_2/vendor/processmaker/packages/package-plg/js/screen-builder-form-components.js?id=abc', + PluginVendorAssets::webPath($tenantId, $package, '/js/screen-builder-form-components.js?id=abc') + ); + + $this->assertSame( + '/vendor/processmaker/packages/package-plg/js/custom-components-admin.js?id=xyz', + PluginVendorAssets::globalWebPath($package, '/js/custom-components-admin.js?id=xyz') + ); + + $this->assertSame( + 'package-plg', + PluginVendorAssets::parsePackageFromManifestDirectory('vendor/processmaker/packages/package-plg') + ); + $this->assertSame( + 'package-plg', + PluginVendorAssets::parsePackageFromManifestDirectory('/vendor/processmaker/packages/package-plg/') + ); + $this->assertNull(PluginVendorAssets::parsePackageFromManifestDirectory('')); + $this->assertNull(PluginVendorAssets::parsePackageFromManifestDirectory('vendor/other')); + $this->assertTrue(PluginVendorAssets::isPluginVendorManifestDirectory('vendor/processmaker/packages/package-plg')); + + config(['app.multitenancy' => false]); + config(['plugin_manager.tenant_plugin_assets_enabled' => true]); + $this->app->instance('currentTenant', (object) ['id' => 1]); + $this->assertFalse(PluginVendorAssets::isEnabled()); + + config(['app.multitenancy' => true]); + $this->assertTrue(PluginVendorAssets::isEnabled()); + + $this->app->offsetUnset('currentTenant'); + $this->assertFalse(PluginVendorAssets::isEnabled()); + + config(['plugin_manager.tenant_plugin_assets_enabled' => false]); + $this->app->instance('currentTenant', (object) ['id' => 3]); + $this->assertFalse(PluginVendorAssets::isEnabled()); + + config(['plugin_manager.tenant_plugin_assets_enabled' => true]); + $this->app->instance('currentTenant', (object) ['id' => 5]); + $tenantAvailabilityDir = PluginVendorAssets::tenantPublicRoot(5, 'avail-pkg'); + mkdir($tenantAvailabilityDir, 0755, true); + file_put_contents($tenantAvailabilityDir . '/mix-manifest.json', '{}'); + $this->assertDirectoryDoesNotExist(public_path('vendor/processmaker/packages/avail-pkg')); + $this->assertTrue( + PluginVendorAssets::publishedVendorPackageIsAvailable('vendor/processmaker/packages/avail-pkg') + ); + $this->deleteDirectory($tenantAvailabilityDir); + + $this->deleteDirectory($pluginDir); + $this->deleteDirectory($emptyDir); + } + + private function deleteDirectory(string $dir): void + { + if (!is_dir($dir)) { + return; + } + foreach (scandir($dir) ?: [] as $entry) { + if ($entry === '.' || $entry === '..') { + continue; + } + $path = $dir . DIRECTORY_SEPARATOR . $entry; + if (is_dir($path)) { + $this->deleteDirectory($path); + } else { + unlink($path); + } + } + rmdir($dir); + } +} diff --git a/tests/unit/Traits/PluginServiceProviderTraitModelerTest.php b/tests/unit/Traits/PluginServiceProviderTraitModelerTest.php new file mode 100644 index 0000000000..cae9432be8 --- /dev/null +++ b/tests/unit/Traits/PluginServiceProviderTraitModelerTest.php @@ -0,0 +1,75 @@ + true]); + config(['plugin_manager.tenant_plugin_assets_enabled' => true]); + $this->app->instance('currentTenant', (object) ['id' => 8]); + + $package = 'modeler-trait-pkg-' . uniqid(); + $tenantRoot = PluginVendorAssets::tenantPublicRoot(8, $package); + mkdir($tenantRoot . '/js', 0755, true); + file_put_contents($tenantRoot . '/mix-manifest.json', json_encode([ + '/js/modeler-schema-profile.js' => '/js/modeler-schema-profile.js?id=trait-test', + ])); + + $provider = new class(app()) extends ServiceProvider { + use PluginServiceProviderTrait; + + public function runModelerStarting(ModelerManager $manager, string $package): void + { + $this->registerModelerScript( + 'js/modeler-schema-profile.js', + 'vendor/processmaker/packages/' . $package, + ['type' => 'module'] + ); + $this->modelerStarting(new ModelerStarting($manager)); + } + }; + + $manager = new ModelerManager(); + $provider->runModelerStarting($manager, $package); + + $scripts = $manager->getScriptWithParams(); + $found = false; + foreach ($scripts as $script) { + if (str_contains($script['src'], '/storage/tenant_8/vendor/processmaker/packages/' . $package)) { + $found = true; + break; + } + } + $this->assertTrue($found, 'Expected tenant modeler script URL in manager registry'); + + $this->deleteDirectory($tenantRoot); + } + + private function deleteDirectory(string $dir): void + { + if (!is_dir($dir)) { + return; + } + foreach (scandir($dir) ?: [] as $entry) { + if ($entry === '.' || $entry === '..') { + continue; + } + $path = $dir . DIRECTORY_SEPARATOR . $entry; + if (is_dir($path)) { + $this->deleteDirectory($path); + } else { + unlink($path); + } + } + rmdir($dir); + } +} diff --git a/upgrades/2026_11_06_143900_add_mcp_settings_menu.php b/upgrades/2026_11_06_143900_add_mcp_settings_menu.php new file mode 100644 index 0000000000..69a0c181c3 --- /dev/null +++ b/upgrades/2026_11_06_143900_add_mcp_settings_menu.php @@ -0,0 +1,28 @@ +run(); + } + + /** + * Reverse the upgrade migration. + */ + public function down(): void + { + Setting::whereIn('key', [ + McpGuidance::SYSTEM_PROMPT_KEY, + McpGuidance::SKILLS_KEY, + ])->delete(); + } +} diff --git a/upgrades/2026_11_09_141900_change_settings_config_to_longtext.php b/upgrades/2026_11_09_141900_change_settings_config_to_longtext.php new file mode 100644 index 0000000000..20894b46be --- /dev/null +++ b/upgrades/2026_11_09_141900_change_settings_config_to_longtext.php @@ -0,0 +1,25 @@ +longText('config')->nullable()->change(); + }); + } + + public function down(): void + { + Schema::table('settings', function (Blueprint $table) { + $table->text('config')->nullable()->change(); + }); + } +} diff --git a/upgrades/2026_11_11_190000_remove_mcp_system_skills.php b/upgrades/2026_11_11_190000_remove_mcp_system_skills.php new file mode 100644 index 0000000000..25e26639b2 --- /dev/null +++ b/upgrades/2026_11_11_190000_remove_mcp_system_skills.php @@ -0,0 +1,67 @@ +first(); + if ($setting === null) { + return; + } + + $skills = is_array($setting->config) ? $setting->config : []; + $custom = array_values(array_filter( + $skills, + fn ($skill) => !$this->isSystemSkill($skill) + )); + + $setting->config = $custom; + $setting->helper = McpSettingsSeeder::SKILLS_HELPER; + $setting->saveOrFail(); + } + + /** + * System skills cannot be restored from this repository, so there is + * nothing to reverse. Custom skills are untouched. + */ + public function down(): void + { + // + } + + private function isSystemSkill(mixed $skill): bool + { + if (!is_array($skill)) { + return false; + } + + $id = trim((string) ($skill['id'] ?? '')); + + return !empty($skill['system']) + || str_starts_with($id, self::SYSTEM_ID_PREFIX) + || in_array($id, self::LEGACY_SYSTEM_IDS, true); + } +} diff --git a/vite.config.js b/vite.config.js index 1fb13e6cde..c25023fd92 100644 --- a/vite.config.js +++ b/vite.config.js @@ -206,6 +206,8 @@ export default defineConfig(({ mode }) => { "resources/js/vite/tasks/loaderTasks.js", "resources/js/vite/tasks/tasks.js", + "resources/js/admin/plugins/index.js", + "resources/js/tasks/loaderEdit.js", "resources/js/tasks/edit.js", "resources/js/tasks/loaderPreview.js",