mirror of
https://github.com/pewdiepie-archdaemon/odysseus.git
synced 2026-11-07 13:42:20 +01:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
40880f8fdb | ||
|
|
68aa830485 | ||
|
|
8257c661ad | ||
|
|
84c7eabfce | ||
|
|
fd79776964 | ||
|
|
0eb6b9eb1d |
@@ -0,0 +1,17 @@
|
|||||||
|
"""Reject skipped/empty native CI runs without conflating driver child counts."""
|
||||||
|
import argparse
|
||||||
|
from pathlib import Path
|
||||||
|
import xml.etree.ElementTree as ET
|
||||||
|
|
||||||
|
parser = argparse.ArgumentParser(description=__doc__)
|
||||||
|
parser.add_argument('report', type=Path)
|
||||||
|
parser.add_argument('--minimum', type=int, required=True)
|
||||||
|
args = parser.parse_args()
|
||||||
|
root = ET.parse(args.report).getroot()
|
||||||
|
cases = list(root.iter('testcase'))
|
||||||
|
if len(cases) < args.minimum:
|
||||||
|
raise SystemExit(f'Incomplete execution: {len(cases)} cases, expected at least {args.minimum}')
|
||||||
|
for case in cases:
|
||||||
|
if any(case.find(tag) is not None for tag in ('skipped', 'failure', 'error')):
|
||||||
|
raise SystemExit(f'Case did not pass: {case.get("classname")}::{case.get("name")}')
|
||||||
|
print(f'Verified {len(cases)} executed cases; no skipped, failed or errored cases')
|
||||||
@@ -0,0 +1,46 @@
|
|||||||
|
import fs from 'node:fs';
|
||||||
|
import path from 'node:path';
|
||||||
|
import { fileURLToPath } from 'node:url';
|
||||||
|
import { spawnSync } from 'node:child_process';
|
||||||
|
import { parse } from 'acorn';
|
||||||
|
|
||||||
|
const root = fileURLToPath(new URL('../../', import.meta.url));
|
||||||
|
const manifest = JSON.parse(fs.readFileSync(path.join(root, 'tests/native-ci-manifest.json'), 'utf8'));
|
||||||
|
function files(directory) {
|
||||||
|
return fs.readdirSync(directory, { withFileTypes: true }).flatMap(entry => {
|
||||||
|
const target = path.join(directory, entry.name);
|
||||||
|
return entry.isDirectory() ? files(target) : /\.(?:mjs|cjs|js)$/.test(entry.name) ? [target] : [];
|
||||||
|
});
|
||||||
|
}
|
||||||
|
function importsNativeTest(node) {
|
||||||
|
if (!node || typeof node !== 'object') return false;
|
||||||
|
if (node.type === 'ImportDeclaration' && node.source.value === 'node:test') return true;
|
||||||
|
if (node.type === 'CallExpression' && node.callee.name === 'require' && node.arguments[0]?.value === 'node:test') return true;
|
||||||
|
return Object.values(node).some(value => Array.isArray(value) ? value.some(importsNativeTest) : importsNativeTest(value));
|
||||||
|
}
|
||||||
|
const discovered = files(path.join(root, 'tests')).filter(file => {
|
||||||
|
const source = fs.readFileSync(file, 'utf8');
|
||||||
|
return source.includes('node:test') && importsNativeTest(parse(source, { ecmaVersion: 'latest', sourceType: 'module' }));
|
||||||
|
}).map(file => path.relative(root, file).split(path.sep).join('/')).sort();
|
||||||
|
const assigned = Object.values(manifest).flatMap(group => Array.isArray(group) ? group : Object.keys(group));
|
||||||
|
if (new Set(assigned).size !== assigned.length || JSON.stringify([...assigned].sort()) !== JSON.stringify(discovered)) {
|
||||||
|
throw new Error('Native test coverage must be exhaustive and disjoint; update tests/native-ci-manifest.json for added, removed or duplicated files');
|
||||||
|
}
|
||||||
|
for (const wrappers of Object.values(manifest['python-ci'])) {
|
||||||
|
for (const wrapper of wrappers) if (!fs.existsSync(path.join(root, wrapper))) throw new Error(`Missing pytest wrapper: ${wrapper}`);
|
||||||
|
}
|
||||||
|
if (process.argv.includes('--list')) {
|
||||||
|
console.log(JSON.stringify({ discovered, runners: manifest }, null, 2));
|
||||||
|
} else {
|
||||||
|
const reports = path.join(root, 'test-results/native-node');
|
||||||
|
fs.mkdirSync(reports, { recursive: true });
|
||||||
|
const result = spawnSync(process.execPath, ['--test', '--test-concurrency=1',
|
||||||
|
'--test-reporter=spec', '--test-reporter-destination=stdout',
|
||||||
|
'--test-reporter=junit', `--test-reporter-destination=${path.join(reports, 'junit.xml')}`,
|
||||||
|
...manifest['native-ci']], {
|
||||||
|
cwd: root, stdio: 'inherit',
|
||||||
|
env: { ...process.env, ENDPOINT_URL: 'http://127.0.0.1:1' },
|
||||||
|
});
|
||||||
|
if (result.error) throw result.error;
|
||||||
|
process.exitCode = result.status ?? 1;
|
||||||
|
}
|
||||||
+465
@@ -0,0 +1,465 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
"""Fail-closed independent verification and sanitization gate for test diagnostics.
|
||||||
|
|
||||||
|
Runs after test suite completion or abnormal runner termination (e.g. failures, timeouts, crashes).
|
||||||
|
Ensures that no unsanitized traces, raw credentials, temporary archives, or unauthorized
|
||||||
|
diagnostics can ever be published as CI artifacts, while preserving safe JUnit XML reporting.
|
||||||
|
"""
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import argparse
|
||||||
|
import json
|
||||||
|
import logging
|
||||||
|
import os
|
||||||
|
import re
|
||||||
|
import shutil
|
||||||
|
import sys
|
||||||
|
import tempfile
|
||||||
|
import xml.etree.ElementTree as ET
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
# Add repository root to Python path to import sanitizer
|
||||||
|
REPO_ROOT = Path(__file__).resolve().parents[2]
|
||||||
|
if str(REPO_ROOT) not in sys.path:
|
||||||
|
sys.path.insert(0, str(REPO_ROOT))
|
||||||
|
|
||||||
|
try:
|
||||||
|
from scripts.sanitize_trace_artifacts import sanitize_trace_archive
|
||||||
|
except ImportError:
|
||||||
|
# If import fails, define fail-closed fallback
|
||||||
|
def sanitize_trace_archive(zp: str) -> bool:
|
||||||
|
p = Path(zp)
|
||||||
|
if p.exists():
|
||||||
|
try:
|
||||||
|
p.unlink(missing_ok=True)
|
||||||
|
except OSError:
|
||||||
|
pass
|
||||||
|
return False
|
||||||
|
|
||||||
|
logging.basicConfig(level=logging.INFO, format="%(levelname)s: %(message)s")
|
||||||
|
logger = logging.getLogger("verify_sanitized_artifacts")
|
||||||
|
|
||||||
|
PNG_MAGIC = b"\x89PNG\r\n\x1a\n"
|
||||||
|
|
||||||
|
ALLOWED_JUNIT_ROOT_TAGS = frozenset({"testsuites", "testsuite"})
|
||||||
|
ALLOWED_PUBLISHED_JUNIT_TAGS = frozenset({"testsuites", "testsuite", "testcase", "failure", "error", "skipped"})
|
||||||
|
DISALLOWED_JUNIT_TAGS = frozenset({"properties", "property", "system-out", "system-err"})
|
||||||
|
|
||||||
|
ALLOWED_ATTRIBUTES_BY_TAG = {
|
||||||
|
"testsuites": frozenset({"name", "tests", "failures", "errors", "skipped", "time", "id", "disabled"}),
|
||||||
|
"testsuite": frozenset({
|
||||||
|
"name", "tests", "failures", "errors", "skipped", "time", "timestamp", "hostname",
|
||||||
|
"id", "package", "disabled", "file"
|
||||||
|
}),
|
||||||
|
"testcase": frozenset({"name", "classname", "time", "file", "line", "assertions", "status"}),
|
||||||
|
"failure": frozenset({"message", "type"}),
|
||||||
|
"error": frozenset({"message", "type"}),
|
||||||
|
"skipped": frozenset({"message", "type"}),
|
||||||
|
}
|
||||||
|
|
||||||
|
SAFE_SKIPPED_STATUSES = frozenset({"skipped", "todo", "ignored", "disabled", "pending", "omitted", "test skipped"})
|
||||||
|
|
||||||
|
SENSITIVE_KEY_PATTERN = re.compile(
|
||||||
|
r"(?i)(password|passwd|secret|token|api_key|apikey|access_token|refresh_token|"
|
||||||
|
r"private_key|auth_token|client_secret|session|session_id|sid|csrf|xsrf|jwt|credential|authorization)"
|
||||||
|
)
|
||||||
|
|
||||||
|
SECRET_REDACTION_PATTERNS = [
|
||||||
|
# 1. Authorization headers (Bearer / Basic)
|
||||||
|
re.compile(r"(?i)\b(bearer|basic)\s+([^\s,;\'\"<>]+)"),
|
||||||
|
# 2. Sensitive key-value pairs (key=value or key: value)
|
||||||
|
re.compile(
|
||||||
|
r"(?i)\b([a-z0-9_.-]*(?:password|passwd|secret|token|api_key|apikey|access_token|refresh_token|"
|
||||||
|
r"auth_token|client_secret|session|session_id|sid|csrf|xsrf|jwt|credential|authorization)[a-z0-9_.-]*)"
|
||||||
|
r"\s*([:=])\s*([^\s,;\'\"<>]+)"
|
||||||
|
),
|
||||||
|
# 3. URL credentials (scheme://user:pass@host)
|
||||||
|
re.compile(r"([a-zA-Z][a-zA-Z0-9+.-]*://[^:\s]+):([^@\s]+)@"),
|
||||||
|
# 4. JSON-formatted key-value pairs
|
||||||
|
re.compile(
|
||||||
|
r'(?i)("?(?:[a-z0-9_.-]*(?:password|passwd|secret|token|key|auth)[a-z0-9_.-]*)"?\s*:\s*")([^"]+)(")'
|
||||||
|
),
|
||||||
|
# 5. Synthetic secret keywords or tokens
|
||||||
|
re.compile(r"(?i)\b[a-z0-9_.-]*synthetic[-_]?[a-z0-9_.-]*\b"),
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
def safe_remove(filepath: str | Path) -> bool:
|
||||||
|
"""Safely remove a file or directory fail-closed.
|
||||||
|
|
||||||
|
Returns True if the target does not exist or was successfully removed.
|
||||||
|
Returns False if removal failed and the target still exists on disk.
|
||||||
|
"""
|
||||||
|
p = Path(filepath)
|
||||||
|
if not p.exists():
|
||||||
|
return True
|
||||||
|
try:
|
||||||
|
if p.is_dir():
|
||||||
|
shutil.rmtree(p, ignore_errors=False)
|
||||||
|
else:
|
||||||
|
p.unlink(missing_ok=True)
|
||||||
|
except OSError as exc:
|
||||||
|
logger.error("Failed to delete unauthorized file %s: %s", filepath, exc)
|
||||||
|
return False
|
||||||
|
|
||||||
|
if p.exists():
|
||||||
|
logger.error("Target %s still exists on disk after deletion attempt", filepath)
|
||||||
|
return False
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
def sanitize_text_content(text: str) -> tuple[str, bool]:
|
||||||
|
"""Scrub sensitive credentials, tokens, and synthetic secrets from text."""
|
||||||
|
if not text or not isinstance(text, str):
|
||||||
|
return text, False
|
||||||
|
|
||||||
|
modified = False
|
||||||
|
out = text
|
||||||
|
|
||||||
|
# 1. Replace Bearer / Basic tokens
|
||||||
|
def _sub_auth(m: re.Match) -> str:
|
||||||
|
nonlocal modified
|
||||||
|
modified = True
|
||||||
|
return f"{m.group(1)} [REDACTED]"
|
||||||
|
|
||||||
|
out = SECRET_REDACTION_PATTERNS[0].sub(_sub_auth, out)
|
||||||
|
|
||||||
|
# 2. Replace key-value pairs
|
||||||
|
def _sub_kv(m: re.Match) -> str:
|
||||||
|
nonlocal modified
|
||||||
|
modified = True
|
||||||
|
return f"{m.group(1)}{m.group(2)}[REDACTED]"
|
||||||
|
|
||||||
|
out = SECRET_REDACTION_PATTERNS[1].sub(_sub_kv, out)
|
||||||
|
|
||||||
|
# 3. Replace URL credentials
|
||||||
|
def _sub_url(m: re.Match) -> str:
|
||||||
|
nonlocal modified
|
||||||
|
modified = True
|
||||||
|
return f"{m.group(1)}:[REDACTED]@"
|
||||||
|
|
||||||
|
out = SECRET_REDACTION_PATTERNS[2].sub(_sub_url, out)
|
||||||
|
|
||||||
|
# 4. Replace JSON key-value secrets
|
||||||
|
def _sub_json(m: re.Match) -> str:
|
||||||
|
nonlocal modified
|
||||||
|
modified = True
|
||||||
|
return f"{m.group(1)}[REDACTED]{m.group(3)}"
|
||||||
|
|
||||||
|
out = SECRET_REDACTION_PATTERNS[3].sub(_sub_json, out)
|
||||||
|
|
||||||
|
# 5. Replace synthetic secret markers
|
||||||
|
def _sub_synthetic(m: re.Match) -> str:
|
||||||
|
nonlocal modified
|
||||||
|
modified = True
|
||||||
|
return "[REDACTED]"
|
||||||
|
|
||||||
|
out = SECRET_REDACTION_PATTERNS[4].sub(_sub_synthetic, out)
|
||||||
|
|
||||||
|
return out, modified
|
||||||
|
|
||||||
|
|
||||||
|
def prune_and_sanitize_junit_element(elem: ET.Element) -> bool:
|
||||||
|
"""Recursively enforce restrictive published-JUnit representation.
|
||||||
|
|
||||||
|
Removes properties, property entries, system-out, system-err, and any
|
||||||
|
unnecessary free-form content. Preserves test identities, counts, outcomes,
|
||||||
|
and safe failure classifications.
|
||||||
|
Returns True if any modifications were made.
|
||||||
|
"""
|
||||||
|
modified = False
|
||||||
|
|
||||||
|
# 1. Prune child elements not permitted in restrictive published representation
|
||||||
|
for child in list(elem):
|
||||||
|
tag_lower = child.tag.lower()
|
||||||
|
if tag_lower in DISALLOWED_JUNIT_TAGS or tag_lower not in ALLOWED_PUBLISHED_JUNIT_TAGS:
|
||||||
|
elem.remove(child)
|
||||||
|
modified = True
|
||||||
|
else:
|
||||||
|
if prune_and_sanitize_junit_element(child):
|
||||||
|
modified = True
|
||||||
|
|
||||||
|
tag_lower = elem.tag.lower()
|
||||||
|
|
||||||
|
# 2. Prune attributes not permitted in restrictive published representation
|
||||||
|
allowed_attrs = ALLOWED_ATTRIBUTES_BY_TAG.get(tag_lower, frozenset())
|
||||||
|
for attr_name in list(elem.attrib.keys()):
|
||||||
|
attr_lower = attr_name.lower()
|
||||||
|
if attr_lower not in allowed_attrs:
|
||||||
|
del elem.attrib[attr_name]
|
||||||
|
modified = True
|
||||||
|
elif tag_lower == "skipped":
|
||||||
|
val = elem.attrib[attr_name].strip()
|
||||||
|
if val.lower() in SAFE_SKIPPED_STATUSES:
|
||||||
|
if elem.attrib[attr_name] != val.lower():
|
||||||
|
elem.attrib[attr_name] = val.lower()
|
||||||
|
modified = True
|
||||||
|
else:
|
||||||
|
elem.attrib[attr_name] = "skipped"
|
||||||
|
modified = True
|
||||||
|
else:
|
||||||
|
val = elem.attrib[attr_name]
|
||||||
|
if isinstance(val, str):
|
||||||
|
new_val, changed = sanitize_text_content(val)
|
||||||
|
if changed:
|
||||||
|
elem.attrib[attr_name] = new_val
|
||||||
|
modified = True
|
||||||
|
|
||||||
|
# 3. Handle text content: structural and skipped elements do not carry free-form text
|
||||||
|
if tag_lower in ("testsuites", "testsuite", "testcase", "skipped"):
|
||||||
|
if elem.text:
|
||||||
|
elem.text = None
|
||||||
|
modified = True
|
||||||
|
elif tag_lower in ("failure", "error"):
|
||||||
|
if elem.text:
|
||||||
|
new_text, changed = sanitize_text_content(elem.text)
|
||||||
|
if changed:
|
||||||
|
elem.text = new_text
|
||||||
|
modified = True
|
||||||
|
|
||||||
|
# 4. Clean up tails: strip any non-whitespace characters from tails to prevent leaks
|
||||||
|
if elem.tail and elem.tail.strip():
|
||||||
|
cleaned_tail = re.sub(r"\S+", "", elem.tail)
|
||||||
|
if cleaned_tail != elem.tail:
|
||||||
|
elem.tail = cleaned_tail
|
||||||
|
modified = True
|
||||||
|
|
||||||
|
return modified
|
||||||
|
|
||||||
|
|
||||||
|
def sanitize_and_verify_junit_xml(filepath: str | Path) -> bool:
|
||||||
|
"""Verify that an XML file is an authentic JUnit report and enforce published representation.
|
||||||
|
|
||||||
|
Returns True if the XML file is a valid recognized JUnit report and was safely sanitized.
|
||||||
|
Returns False if the file is invalid, unrecognized, corrupted, or failed sanitization.
|
||||||
|
"""
|
||||||
|
p = Path(filepath)
|
||||||
|
name_lower = p.name.lower()
|
||||||
|
|
||||||
|
# 1. Filename restriction: must match standard JUnit report naming
|
||||||
|
if not (name_lower == "junit.xml" or name_lower == "browser-junit.xml" or name_lower.endswith("-junit.xml") or name_lower.endswith(".junit.xml")):
|
||||||
|
logger.warning("Unrecognized XML report filename %s; not an authorized JUnit report", filepath)
|
||||||
|
return False
|
||||||
|
|
||||||
|
# 2. Parse XML
|
||||||
|
try:
|
||||||
|
tree = ET.parse(p)
|
||||||
|
except Exception as exc:
|
||||||
|
logger.warning("Corrupted XML report %s: %s", filepath, exc)
|
||||||
|
return False
|
||||||
|
|
||||||
|
root = tree.getroot()
|
||||||
|
# 3. Root tag restriction
|
||||||
|
if root.tag.lower() not in ALLOWED_JUNIT_ROOT_TAGS:
|
||||||
|
logger.warning("XML report %s has invalid root element <%s>; not a JUnit report", filepath, root.tag)
|
||||||
|
return False
|
||||||
|
|
||||||
|
# 4. Enforce restrictive published representation on root and all descendants
|
||||||
|
prune_and_sanitize_junit_element(root)
|
||||||
|
|
||||||
|
# 5. Normalize and rewrite sanitized report
|
||||||
|
try:
|
||||||
|
tree.write(str(p), encoding="utf-8", xml_declaration=True)
|
||||||
|
# Verify rewritten XML parses cleanly
|
||||||
|
ET.parse(p)
|
||||||
|
except Exception as exc:
|
||||||
|
logger.error("Failed to write sanitized XML report %s: %s", filepath, exc)
|
||||||
|
return False
|
||||||
|
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
def is_valid_disposable_ci_fixture(target_dir: str | Path | None = None) -> bool:
|
||||||
|
"""Verify that execution ran inside an authorized disposable synthetic fixture."""
|
||||||
|
is_ci = bool(os.environ.get("CI") or os.environ.get("GITHUB_ACTIONS"))
|
||||||
|
if not is_ci:
|
||||||
|
# Local runs require explicit trace opt-in
|
||||||
|
trace_opt_in = os.environ.get("PLAYWRIGHT_ENABLE_TRACE", "").lower() in ("1", "true")
|
||||||
|
return trace_opt_in
|
||||||
|
|
||||||
|
if os.environ.get("ODYSSEUS_E2E_DISPOSABLE_ARTIFACTS") != "1":
|
||||||
|
return False
|
||||||
|
if os.environ.get("PHOTO_EDITOR_E2E_AUTH") == "0":
|
||||||
|
return False
|
||||||
|
|
||||||
|
allowed_tmp = [os.path.abspath(p) for p in (tempfile.gettempdir(), "/tmp", "/var/tmp")]
|
||||||
|
|
||||||
|
# Check 1: Verify fixture ownership via PHOTO_EDITOR_E2E_DATA_DIR
|
||||||
|
data_dir = os.environ.get("PHOTO_EDITOR_E2E_DATA_DIR")
|
||||||
|
if data_dir:
|
||||||
|
resolved = os.path.abspath(data_dir)
|
||||||
|
base_name = os.path.basename(resolved)
|
||||||
|
is_tmp = any(resolved.startswith(b + os.sep) or resolved == b for b in allowed_tmp)
|
||||||
|
if base_name.startswith("odysseus-photo-editor-e2e-") and is_tmp:
|
||||||
|
expected_db = os.path.join(resolved, "app.db")
|
||||||
|
db_path = os.environ.get("PHOTO_EDITOR_E2E_DB_PATH")
|
||||||
|
if not db_path or db_path == expected_db:
|
||||||
|
marker_path = os.path.join(resolved, ".disposable-ci-fixture.json")
|
||||||
|
if os.path.exists(marker_path):
|
||||||
|
try:
|
||||||
|
with open(marker_path, "r", encoding="utf-8") as f:
|
||||||
|
fixture = json.load(f)
|
||||||
|
if (
|
||||||
|
fixture.get("synthetic") is True
|
||||||
|
and fixture.get("authenticated") is True
|
||||||
|
and fixture.get("database") == expected_db
|
||||||
|
):
|
||||||
|
return True
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
|
||||||
|
# Check 2: Verify fixture proof in target_dir if provided
|
||||||
|
if target_dir:
|
||||||
|
target_marker = os.path.join(str(target_dir), ".disposable-ci-fixture.json")
|
||||||
|
if os.path.exists(target_marker):
|
||||||
|
try:
|
||||||
|
with open(target_marker, "r", encoding="utf-8") as f:
|
||||||
|
fixture = json.load(f)
|
||||||
|
db_in_fixture = fixture.get("database", "")
|
||||||
|
resolved_db = os.path.abspath(db_in_fixture)
|
||||||
|
db_dir = os.path.dirname(resolved_db)
|
||||||
|
db_base = os.path.basename(db_dir)
|
||||||
|
is_tmp = any(db_dir.startswith(b + os.sep) or db_dir == b for b in allowed_tmp)
|
||||||
|
if (
|
||||||
|
fixture.get("synthetic") is True
|
||||||
|
and fixture.get("authenticated") is True
|
||||||
|
and db_base.startswith("odysseus-photo-editor-e2e-")
|
||||||
|
and is_tmp
|
||||||
|
and os.path.basename(resolved_db) == "app.db"
|
||||||
|
):
|
||||||
|
return True
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
def verify_and_sanitize_artifacts(target_dir: str) -> bool:
|
||||||
|
"""Independently verify and sanitize diagnostics fail-closed, preserving safe JUnit XML.
|
||||||
|
|
||||||
|
Returns True if verification succeeded and artifacts are safe for publication.
|
||||||
|
Returns False if verification failed or deletion could not be confirmed.
|
||||||
|
"""
|
||||||
|
if not os.path.exists(target_dir):
|
||||||
|
logger.info("Target directory %s does not exist; nothing to sanitize.", target_dir)
|
||||||
|
return True
|
||||||
|
|
||||||
|
disposable_ci = is_valid_disposable_ci_fixture(target_dir)
|
||||||
|
preserved_xmls = 0
|
||||||
|
sanitized_archives = 0
|
||||||
|
purged_files = 0
|
||||||
|
overall_success = True
|
||||||
|
|
||||||
|
try:
|
||||||
|
for root, dirs, files in os.walk(target_dir, topdown=False):
|
||||||
|
for filename in files:
|
||||||
|
filepath = os.path.join(root, filename)
|
||||||
|
name_lower = filename.lower()
|
||||||
|
|
||||||
|
# Cleanup disposable fixture marker so it is not published
|
||||||
|
if filename == ".disposable-ci-fixture.json":
|
||||||
|
if not safe_remove(filepath):
|
||||||
|
overall_success = False
|
||||||
|
continue
|
||||||
|
|
||||||
|
# 1. XML reports: verify JUnit structure and sanitize credentials
|
||||||
|
if name_lower.endswith(".xml"):
|
||||||
|
if sanitize_and_verify_junit_xml(filepath):
|
||||||
|
preserved_xmls += 1
|
||||||
|
continue
|
||||||
|
else:
|
||||||
|
logger.warning("Rejecting invalid/unrecognized XML report %s; purging fail-closed", filepath)
|
||||||
|
if not safe_remove(filepath):
|
||||||
|
overall_success = False
|
||||||
|
purged_files += 1
|
||||||
|
continue
|
||||||
|
|
||||||
|
# 2. Check if diagnostics are authorized (disposable synthetic fixture)
|
||||||
|
if not disposable_ci:
|
||||||
|
logger.info("Purging diagnostic artifact %s (disposable CI fixture not verified)", filepath)
|
||||||
|
if not safe_remove(filepath):
|
||||||
|
overall_success = False
|
||||||
|
purged_files += 1
|
||||||
|
continue
|
||||||
|
|
||||||
|
# 3. Disposable CI is authorized: enforce strict sanitization
|
||||||
|
if name_lower.endswith(".zip"):
|
||||||
|
ok = sanitize_trace_archive(filepath)
|
||||||
|
if ok and os.path.exists(filepath):
|
||||||
|
sanitized_archives += 1
|
||||||
|
else:
|
||||||
|
logger.warning("Trace archive %s failed sanitization; purging fail-closed", filepath)
|
||||||
|
if not safe_remove(filepath):
|
||||||
|
overall_success = False
|
||||||
|
purged_files += 1
|
||||||
|
|
||||||
|
elif name_lower.endswith(".png"):
|
||||||
|
# Validate PNG magic bytes to ensure not a secret payload disguised as an image
|
||||||
|
is_png = False
|
||||||
|
try:
|
||||||
|
with open(filepath, "rb") as pf:
|
||||||
|
header = pf.read(8)
|
||||||
|
if header == PNG_MAGIC:
|
||||||
|
is_png = True
|
||||||
|
except Exception:
|
||||||
|
is_png = False
|
||||||
|
|
||||||
|
if is_png:
|
||||||
|
continue
|
||||||
|
|
||||||
|
logger.warning("Invalid or suspicious screenshot %s; purging fail-closed", filepath)
|
||||||
|
if not safe_remove(filepath):
|
||||||
|
overall_success = False
|
||||||
|
purged_files += 1
|
||||||
|
|
||||||
|
else:
|
||||||
|
# Purge any .tmp, .har, .db, .sqlite, .log or unrecognized files
|
||||||
|
logger.info("Purging unauthorized diagnostic file %s", filepath)
|
||||||
|
if not safe_remove(filepath):
|
||||||
|
overall_success = False
|
||||||
|
purged_files += 1
|
||||||
|
|
||||||
|
# Remove empty subdirectories
|
||||||
|
for d in dirs:
|
||||||
|
dirpath = os.path.join(root, d)
|
||||||
|
try:
|
||||||
|
if not os.listdir(dirpath):
|
||||||
|
os.rmdir(dirpath)
|
||||||
|
except OSError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
if not overall_success:
|
||||||
|
logger.error("Verification failed: one or more unauthorized files could not be purged.")
|
||||||
|
return False
|
||||||
|
|
||||||
|
logger.info(
|
||||||
|
"Artifact verification complete for %s: preserved %d XML reports, sanitized %d archives, purged %d files.",
|
||||||
|
target_dir,
|
||||||
|
preserved_xmls,
|
||||||
|
sanitized_archives,
|
||||||
|
purged_files,
|
||||||
|
)
|
||||||
|
return True
|
||||||
|
|
||||||
|
except Exception as exc:
|
||||||
|
logger.error("Unexpected error during artifact verification: %s; purging all non-XML files fail-closed", exc)
|
||||||
|
try:
|
||||||
|
for p in Path(target_dir).rglob("*"):
|
||||||
|
if p.is_file() and not p.name.lower().endswith(".xml"):
|
||||||
|
safe_remove(p)
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
def main() -> None:
|
||||||
|
parser = argparse.ArgumentParser(description=__doc__)
|
||||||
|
parser.add_argument("target_dir", nargs="?", default="test-results", help="Directory containing test results")
|
||||||
|
args = parser.parse_args()
|
||||||
|
|
||||||
|
ok = verify_and_sanitize_artifacts(args.target_dir)
|
||||||
|
sys.exit(0 if ok else 1)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
@@ -179,7 +179,6 @@ jobs:
|
|||||||
uv pip install --system --require-hashes --only-binary=:all: --no-deps -r requirements-bootstrap.txt
|
uv pip install --system --require-hashes --only-binary=:all: --no-deps -r requirements-bootstrap.txt
|
||||||
python scripts/check_ca_trust.py --python
|
python scripts/check_ca_trust.py --python
|
||||||
uv pip install --system -r requirements.txt
|
uv pip install --system -r requirements.txt
|
||||||
uv pip install --system "playwright==1.62.0"
|
|
||||||
if: steps.docs-check.outputs.docs_only != 'true'
|
if: steps.docs-check.outputs.docs_only != 'true'
|
||||||
- uses: actions/setup-node@820762786026740c76f36085b0efc47a31fe5020 # v7.0.0
|
- uses: actions/setup-node@820762786026740c76f36085b0efc47a31fe5020 # v7.0.0
|
||||||
if: steps.docs-check.outputs.docs_only != 'true'
|
if: steps.docs-check.outputs.docs_only != 'true'
|
||||||
|
|||||||
@@ -0,0 +1,129 @@
|
|||||||
|
name: Native test suites
|
||||||
|
|
||||||
|
on:
|
||||||
|
pull_request:
|
||||||
|
push:
|
||||||
|
branches: [main, dev]
|
||||||
|
workflow_dispatch:
|
||||||
|
|
||||||
|
permissions:
|
||||||
|
contents: read
|
||||||
|
|
||||||
|
concurrency:
|
||||||
|
group: native-tests-${{ github.ref }}
|
||||||
|
cancel-in-progress: true
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
native-node:
|
||||||
|
name: Native Node regressions
|
||||||
|
runs-on: ubuntu-24.04
|
||||||
|
timeout-minutes: 15
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
||||||
|
with:
|
||||||
|
persist-credentials: false
|
||||||
|
- uses: actions/setup-python@5fda3b95a4ea91299a34e894583c3862153e4b97 # v7.0.0
|
||||||
|
with:
|
||||||
|
python-version: '3.11'
|
||||||
|
- uses: actions/setup-node@820762786026740c76f36085b0efc47a31fe5020 # v7.0.0
|
||||||
|
with:
|
||||||
|
node-version: '22.23.4'
|
||||||
|
cache: npm
|
||||||
|
- run: npm ci --ignore-scripts --no-audit --no-fund
|
||||||
|
- uses: actions/cache@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0
|
||||||
|
with:
|
||||||
|
path: ~/.cache/ms-playwright
|
||||||
|
key: ms-playwright-${{ runner.os }}-${{ hashFiles('package-lock.json') }}
|
||||||
|
- run: npx playwright install --with-deps chromium
|
||||||
|
- name: Validate exhaustive disjoint runner mapping and execute omitted suites
|
||||||
|
run: |
|
||||||
|
node .github/scripts/run_native_tests.mjs
|
||||||
|
python .github/scripts/check_test_execution.py test-results/native-node/junit.xml --minimum 43
|
||||||
|
- name: Verify and sanitize native test results fail-closed
|
||||||
|
id: verify-native-results
|
||||||
|
if: always()
|
||||||
|
run: |
|
||||||
|
python .github/scripts/verify_sanitized_artifacts.py test-results/native-node/
|
||||||
|
- name: Save native test identities and results
|
||||||
|
if: always() && steps.verify-native-results.outcome == 'success'
|
||||||
|
uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
|
||||||
|
with:
|
||||||
|
name: native-node-results
|
||||||
|
path: test-results/native-node/
|
||||||
|
retention-days: 7
|
||||||
|
if-no-files-found: warn
|
||||||
|
|
||||||
|
browser:
|
||||||
|
name: Chromium application regressions
|
||||||
|
runs-on: ubuntu-24.04
|
||||||
|
timeout-minutes: 30
|
||||||
|
env:
|
||||||
|
ODYSSEUS_E2E_DISPOSABLE_ARTIFACTS: '1'
|
||||||
|
ODYSSEUS_TEST_PYTHON: python
|
||||||
|
CHROMADB_HOST: 127.0.0.1
|
||||||
|
CHROMADB_PORT: '1'
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
||||||
|
with:
|
||||||
|
persist-credentials: false
|
||||||
|
- uses: actions/setup-python@5fda3b95a4ea91299a34e894583c3862153e4b97 # v7.0.0
|
||||||
|
with:
|
||||||
|
python-version: '3.11'
|
||||||
|
- name: Install FFmpeg and establish system trust
|
||||||
|
run: |
|
||||||
|
sudo apt-get update
|
||||||
|
sudo apt-get install -y --no-install-recommends ffmpeg ca-certificates
|
||||||
|
python scripts/check_ca_trust.py --system
|
||||||
|
- uses: astral-sh/setup-uv@c18668ad3cf93ea998bef934396af7bb5c839dc7 # v10.2.0
|
||||||
|
with:
|
||||||
|
version: '0.13.5'
|
||||||
|
enable-cache: true
|
||||||
|
cache-dependency-glob: requirements.txt
|
||||||
|
prune-cache: false
|
||||||
|
- run: |
|
||||||
|
uv pip install --system --require-hashes --only-binary=:all: --no-deps -r requirements-bootstrap.txt
|
||||||
|
python scripts/check_ca_trust.py --python
|
||||||
|
uv pip install --system -r requirements.txt
|
||||||
|
- uses: actions/setup-node@820762786026740c76f36085b0efc47a31fe5020 # v7.0.0
|
||||||
|
with:
|
||||||
|
node-version: '22.23.4'
|
||||||
|
cache: npm
|
||||||
|
- run: npm ci --ignore-scripts --no-audit --no-fund
|
||||||
|
- uses: actions/cache@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0
|
||||||
|
with:
|
||||||
|
path: ~/.cache/ms-playwright
|
||||||
|
key: ms-playwright-${{ runner.os }}-${{ hashFiles('package-lock.json') }}
|
||||||
|
- run: npx playwright install --with-deps chromium
|
||||||
|
- name: Run complete Chromium suite against a fresh authenticated app
|
||||||
|
env:
|
||||||
|
PLAYWRIGHT_JUNIT_OUTPUT_FILE: test-results/browser-junit.xml
|
||||||
|
run: |
|
||||||
|
npm run test:photo-editor -- --reporter=line,junit
|
||||||
|
python .github/scripts/check_test_execution.py test-results/browser-junit.xml --minimum 137
|
||||||
|
- name: Verify and sanitize failure diagnostics fail-closed
|
||||||
|
id: verify-browser-results
|
||||||
|
if: always()
|
||||||
|
run: |
|
||||||
|
python .github/scripts/verify_sanitized_artifacts.py test-results/
|
||||||
|
- name: Save per-test results and sanitized failure diagnostics
|
||||||
|
if: always() && steps.verify-browser-results.outcome == 'success'
|
||||||
|
uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
|
||||||
|
with:
|
||||||
|
name: chromium-regression-results
|
||||||
|
path: test-results/
|
||||||
|
retention-days: 7
|
||||||
|
if-no-files-found: warn
|
||||||
|
|
||||||
|
authoritative-native-suites:
|
||||||
|
name: Authoritative native suites
|
||||||
|
if: always()
|
||||||
|
needs: [native-node, browser]
|
||||||
|
runs-on: ubuntu-24.04
|
||||||
|
steps:
|
||||||
|
- name: Require both complete suites to succeed
|
||||||
|
env:
|
||||||
|
NODE_RESULT: ${{ needs.native-node.result }}
|
||||||
|
BROWSER_RESULT: ${{ needs.browser.result }}
|
||||||
|
run: |
|
||||||
|
test "$NODE_RESULT" = success
|
||||||
|
test "$BROWSER_RESULT" = success
|
||||||
Binary file not shown.
|
Before Width: | Height: | Size: 91 KiB |
Binary file not shown.
|
Before Width: | Height: | Size: 97 KiB |
Binary file not shown.
|
Before Width: | Height: | Size: 146 KiB |
Binary file not shown.
|
Before Width: | Height: | Size: 98 KiB |
Binary file not shown.
|
Before Width: | Height: | Size: 105 KiB |
@@ -0,0 +1,46 @@
|
|||||||
|
# Native test merge gate
|
||||||
|
|
||||||
|
`Native test suites` runs on pull requests, dev/main pushes and manual dispatch.
|
||||||
|
`Authoritative native suites` fails unless both the native Node and complete
|
||||||
|
Chromium jobs succeed; it has no continue-on-error, retries or path bypass.
|
||||||
|
Repository administrators must require this check alongside the four pytest
|
||||||
|
checks in branch protection. The audited public dev branch had no protection or
|
||||||
|
ruleset, so merely adding workflow jobs cannot prevent a manual merge.
|
||||||
|
|
||||||
|
`tests/native-ci-manifest.json` exhaustively partitions all native Node files:
|
||||||
|
16 run in the native job,14 through existing pytest wrappers, one through the
|
||||||
|
existing frontend architecture CI step, and the formerly live email-reply file
|
||||||
|
through an explicitly identified Playwright driver. The manifest validator fails
|
||||||
|
for omitted/duplicate/unreachable entries. Node emits individual JUnit identities
|
||||||
|
and propagates its failure exit. No native file is newly run in each pytest shard.
|
||||||
|
|
||||||
|
The browser job uses the existing one-worker Chromium config and all 136 Photo
|
||||||
|
Editor cases (including cursor and brush regressions), plus one driver for the native
|
||||||
|
email-reply contract (137 cases total). Its ephemeral account,
|
||||||
|
SQLite DB and server are created by the config/setup; reuseExistingServer remains
|
||||||
|
false. NPX startup is offline/cache-only in a private cache; Notes/editor behavior
|
||||||
|
and Python MCP services remain real. No developer app or external account is used.
|
||||||
|
|
||||||
|
Default CI trace suppression remains intact. Only this job's explicit opt-in plus
|
||||||
|
a newly generated authenticated synthetic fixture marker permits failure traces.
|
||||||
|
The existing structured sanitizer runs before artifacts are uploaded; invalid
|
||||||
|
ownership, failed sanitization and missing tooling purge sensitive artifacts.
|
||||||
|
Unsupported or malformed sensitive records cause the affected archive to be removed
|
||||||
|
fail-closed. An independent fail-closed verification step (.github/scripts/verify_sanitized_artifacts.py)
|
||||||
|
runs after test suite execution or abnormal runner termination to prevent publishing
|
||||||
|
unsanitized diagnostics while preserving safe JUnit reporting. The verifier returns a nonzero
|
||||||
|
exit code upon any inspection or deletion failure, publishable XML is restricted to recognized
|
||||||
|
JUnit reports with sensitive tokens redacted, and workflow upload steps execute only when
|
||||||
|
independent verification succeeds (steps.<id>.outcome == 'success').
|
||||||
|
Never upload the fixture directory, database or browser storage state.
|
||||||
|
|
||||||
|
The stroke budget now excludes measured synthetic event-delivery pacing and also
|
||||||
|
bounds actual Chromium main-thread work at the original5s. All240 samples and
|
||||||
|
pixel/thumbnail/undo/redo assertions remain. Per-case timing attachments expose
|
||||||
|
both clocks rather than treating driver frame cadence as application work.
|
||||||
|
|
||||||
|
Three Bombadil properties remain opt-in: they require a Bombadil/browser scenario
|
||||||
|
and bounded fuzz campaign; no authoritative execution is claimed. The two model
|
||||||
|
smoke scripts require supplied model endpoints/models and remain explicit live
|
||||||
|
operations. No credentials or live-service authorization are implied by CI setup.
|
||||||
|
Firefox/WebKit alternatives remain opt-in and are not certified by Chromium CI.
|
||||||
@@ -2,4 +2,3 @@
|
|||||||
-r requirements.txt
|
-r requirements.txt
|
||||||
# psutil lets `-n auto` use physical cores instead of logical CPU threads.
|
# psutil lets `-n auto` use physical cores instead of logical CPU threads.
|
||||||
pytest-xdist[psutil]>=3.8,<4
|
pytest-xdist[psutil]>=3.8,<4
|
||||||
playwright==1.62.0
|
|
||||||
|
|||||||
@@ -846,64 +846,6 @@ def _plan_psd_steps(prompt: str) -> Optional[List[Dict[str, Any]]]:
|
|||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
def _is_background_removal_intent(prompt: str) -> bool:
|
|
||||||
"""Linear-time, bounded intent parser for background removal commands.
|
|
||||||
|
|
||||||
Replaces the previous backtracking regular expression flagged by CodeQL
|
|
||||||
(#1212) while preserving the exact command grammar.
|
|
||||||
"""
|
|
||||||
if not prompt or not isinstance(prompt, str):
|
|
||||||
return False
|
|
||||||
i = len(prompt)
|
|
||||||
while i > 0 and prompt[i - 1] in ".! \t\r\n":
|
|
||||||
i -= 1
|
|
||||||
cleaned = prompt[:i].strip()
|
|
||||||
if not cleaned:
|
|
||||||
return False
|
|
||||||
|
|
||||||
tokens = cleaned.split()
|
|
||||||
if not tokens:
|
|
||||||
return False
|
|
||||||
|
|
||||||
idx = 0
|
|
||||||
if tokens[idx].lower() == "please":
|
|
||||||
idx += 1
|
|
||||||
if idx >= len(tokens):
|
|
||||||
return False
|
|
||||||
|
|
||||||
if tokens[idx].lower() not in {"remove", "erase", "delete", "clear", "strip"}:
|
|
||||||
return False
|
|
||||||
idx += 1
|
|
||||||
if idx >= len(tokens):
|
|
||||||
return False
|
|
||||||
|
|
||||||
if tokens[idx].lower() == "the":
|
|
||||||
idx += 1
|
|
||||||
if idx >= len(tokens):
|
|
||||||
return False
|
|
||||||
|
|
||||||
if tokens[idx].lower() not in {"bg", "background", "backdrop"}:
|
|
||||||
return False
|
|
||||||
idx += 1
|
|
||||||
|
|
||||||
if idx == len(tokens):
|
|
||||||
return True
|
|
||||||
|
|
||||||
if tokens[idx].lower() in {"from", "of"}:
|
|
||||||
idx += 1
|
|
||||||
if idx < len(tokens) and tokens[idx].lower() == "the":
|
|
||||||
idx += 1
|
|
||||||
if (
|
|
||||||
idx + 1 < len(tokens)
|
|
||||||
and tokens[idx].lower() in {"active", "current", "selected"}
|
|
||||||
and tokens[idx + 1].lower() == "layer"
|
|
||||||
and idx + 2 == len(tokens)
|
|
||||||
):
|
|
||||||
return True
|
|
||||||
|
|
||||||
return False
|
|
||||||
|
|
||||||
|
|
||||||
def _plan_special(
|
def _plan_special(
|
||||||
prompt: str, state: Any, provider_available: bool,
|
prompt: str, state: Any, provider_available: bool,
|
||||||
) -> Optional[Tuple[List[Dict[str, Any]], str, str]]:
|
) -> Optional[Tuple[List[Dict[str, Any]], str, str]]:
|
||||||
@@ -913,8 +855,6 @@ def _plan_special(
|
|||||||
left to the model. Returns (steps, reply, note) or None. These never need an
|
left to the model. Returns (steps, reply, note) or None. These never need an
|
||||||
image provider except generative border fill, used only when one exists.
|
image provider except generative border fill, used only when one exists.
|
||||||
"""
|
"""
|
||||||
if _is_background_removal_intent(prompt):
|
|
||||||
return [{"op": "ai.removeBackground", "params": {}}], "Removing the active layer's background.", ""
|
|
||||||
# Targeted recolour is fully deterministic (object mask + masked hue shift)
|
# Targeted recolour is fully deterministic (object mask + masked hue shift)
|
||||||
# and must never be left to the model, which sometimes substitutes a
|
# and must never be left to the model, which sometimes substitutes a
|
||||||
# generative placeImage. Highest priority so it can never vary.
|
# generative placeImage. Highest priority so it can never vary.
|
||||||
@@ -1707,13 +1647,6 @@ def setup_editor_agent_routes() -> APIRouter:
|
|||||||
screenshot = body.get("screenshot")
|
screenshot = body.get("screenshot")
|
||||||
# Optional user-picked image model (from the editor model selector).
|
# Optional user-picked image model (from the editor model selector).
|
||||||
model_choice = str(body.get("model") or body.get("image_model") or "").strip()
|
model_choice = str(body.get("model") or body.get("image_model") or "").strip()
|
||||||
generation_model_choice = str(body.get("generation_model") or "").strip()
|
|
||||||
use_image_settings = body.get("_use_image_settings") is True
|
|
||||||
if use_image_settings:
|
|
||||||
from src.editor_image_settings import editor_image_models
|
|
||||||
image_models = editor_image_models(owner=owner)
|
|
||||||
model_choice = image_models["edit"]
|
|
||||||
generation_model_choice = image_models["generation"]
|
|
||||||
# Optional user-picked PLANNER model (the editor agent/planner model).
|
# Optional user-picked PLANNER model (the editor agent/planner model).
|
||||||
agent_model = str(body.get("agent_model") or "").strip()
|
agent_model = str(body.get("agent_model") or "").strip()
|
||||||
tools_offered = [tool for tool in _scoped_editor_tools() if tool not in disabled]
|
tools_offered = [tool for tool in _scoped_editor_tools() if tool not in disabled]
|
||||||
@@ -1864,22 +1797,9 @@ def setup_editor_agent_routes() -> APIRouter:
|
|||||||
# Planner output is untrusted: it cannot redirect this turn to
|
# Planner output is untrusted: it cannot redirect this turn to
|
||||||
# another editor session or override an explicit model choice.
|
# another editor session or override an explicit model choice.
|
||||||
tool_payload["session"] = session
|
tool_payload["session"] = session
|
||||||
if use_image_settings:
|
|
||||||
tool_payload["_use_image_settings"] = True
|
|
||||||
if op == "generate_image" and state.get("width") and state.get("height"):
|
|
||||||
try:
|
|
||||||
width, height = int(state["width"]), int(state["height"])
|
|
||||||
if width > 0 and height > 0:
|
|
||||||
tool_payload["size"] = f"{width}x{height}"
|
|
||||||
except (TypeError, ValueError):
|
|
||||||
pass
|
|
||||||
# Route to the user-picked model when one is selected.
|
# Route to the user-picked model when one is selected.
|
||||||
selected_model = generation_model_choice if op == "generate_image" else model_choice
|
if model_choice:
|
||||||
if selected_model:
|
tool_payload["model"] = model_choice
|
||||||
tool_payload["model"] = selected_model
|
|
||||||
elif op == "generate_image" or use_image_settings:
|
|
||||||
# Let server settings resolve Auto, not a planner-invented model.
|
|
||||||
tool_payload.pop("model", None)
|
|
||||||
# Hand the AI step the canvas image the client already captured
|
# Hand the AI step the canvas image the client already captured
|
||||||
# for this request. Without it the tool falls back to a separate
|
# for this request. Without it the tool falls back to a separate
|
||||||
# bridge screenshot, which is not reliably answered right after a
|
# bridge screenshot, which is not reliably answered right after a
|
||||||
|
|||||||
@@ -37,24 +37,6 @@ _SAM_STATE: Dict[str, Any] = {}
|
|||||||
_GROUNDING_STATE: Dict[str, Any] = {}
|
_GROUNDING_STATE: Dict[str, Any] = {}
|
||||||
|
|
||||||
|
|
||||||
def _compose_cloud_image_edit(source, generated, mask, *, instruction=False):
|
|
||||||
from PIL import Image, ImageChops, ImageOps
|
|
||||||
|
|
||||||
full_frame = instruction or mask.getextrema() == (255, 255)
|
|
||||||
if full_frame:
|
|
||||||
return generated, True
|
|
||||||
# Partial edits must share the source coordinate system, but resizing
|
|
||||||
# width/height independently distorts the subject when the provider changes ratio.
|
|
||||||
fitted = ImageOps.contain(generated, source.size, Image.Resampling.LANCZOS)
|
|
||||||
offset = ((source.width - fitted.width) // 2, (source.height - fitted.height) // 2)
|
|
||||||
aligned = Image.new("RGBA", source.size)
|
|
||||||
aligned.paste(fitted, offset)
|
|
||||||
coverage = Image.new("L", source.size)
|
|
||||||
coverage.paste(255, (offset[0], offset[1], offset[0] + fitted.width, offset[1] + fitted.height))
|
|
||||||
effective_mask = ImageChops.multiply(mask, coverage)
|
|
||||||
return Image.composite(aligned, source, effective_mask), False
|
|
||||||
|
|
||||||
|
|
||||||
def _rss_mb() -> float:
|
def _rss_mb() -> float:
|
||||||
"""Current process RSS in MB (from /proc; -1.0 when unavailable)."""
|
"""Current process RSS in MB (from /proc; -1.0 when unavailable)."""
|
||||||
try:
|
try:
|
||||||
@@ -586,7 +568,6 @@ def _is_openai_api_base(url: str) -> bool:
|
|||||||
|
|
||||||
|
|
||||||
_GALLERY_ENDPOINT_PATHS = frozenset({
|
_GALLERY_ENDPOINT_PATHS = frozenset({
|
||||||
"/chat/completions",
|
|
||||||
"/images",
|
"/images",
|
||||||
"/images/edits",
|
"/images/edits",
|
||||||
"/images/generations",
|
"/images/generations",
|
||||||
@@ -1764,17 +1745,6 @@ def setup_gallery_routes() -> APIRouter:
|
|||||||
if not ok:
|
if not ok:
|
||||||
raise HTTPException(400, f"Rejected endpoint URL: {reason}")
|
raise HTTPException(400, f"Rejected endpoint URL: {reason}")
|
||||||
chosen_model = (body.pop("_model", "") or "").strip()
|
chosen_model = (body.pop("_model", "") or "").strip()
|
||||||
if body.pop("_use_image_settings", False) is True:
|
|
||||||
from src.editor_image_settings import editor_image_models
|
|
||||||
from src.ai_interaction import _resolve_model
|
|
||||||
chosen_model = editor_image_models(owner=user)["edit"]
|
|
||||||
if not chosen_model:
|
|
||||||
raise HTTPException(400, "Choose an image edit model in AI settings")
|
|
||||||
try:
|
|
||||||
edit_url, chosen_model, _ = await asyncio.to_thread(_resolve_model, chosen_model, owner=user)
|
|
||||||
except ValueError as exc:
|
|
||||||
raise HTTPException(400, str(exc)) from exc
|
|
||||||
requested_base = edit_url.removesuffix("/chat/completions").rstrip("/")
|
|
||||||
# Optional client-supplied id so GET /api/image/edit-progress can poll the
|
# Optional client-supplied id so GET /api/image/edit-progress can poll the
|
||||||
# provider's step progress for this op. Ignored by providers that do not
|
# provider's step progress for this op. Ignored by providers that do not
|
||||||
# implement the progress contract.
|
# implement the progress contract.
|
||||||
@@ -1892,30 +1862,14 @@ def setup_gallery_routes() -> APIRouter:
|
|||||||
mask_png.save(reference_mask, format="PNG")
|
mask_png.save(reference_mask, format="PNG")
|
||||||
references = [src_buf.getvalue(), reference_mask.getvalue()]
|
references = [src_buf.getvalue(), reference_mask.getvalue()]
|
||||||
payload = {
|
payload = {
|
||||||
"model": oa_model, "n": 1,
|
"model": oa_model, "size": size, "n": 1,
|
||||||
"output_format": "png",
|
"output_format": "png",
|
||||||
"prompt": "Edit the first image. The second image is a mask: white marks the region to edit, black marks the region to preserve. Keep composition and framing unchanged. Requested edit: " + str(body.get("prompt", "")),
|
"prompt": "Edit the first image. The second image is a mask: white marks the region to edit, black marks the region to preserve. Keep composition and framing unchanged. Requested edit: " + str(body.get("prompt", "")),
|
||||||
"input_references": [{"type": "image_url", "image_url": {
|
"input_references": [{"type": "image_url", "image_url": {
|
||||||
"url": "data:image/png;base64," + base64.b64encode(value).decode(),
|
"url": "data:image/png;base64," + base64.b64encode(value).decode(),
|
||||||
}} for value in references],
|
}} for value in references],
|
||||||
}
|
}
|
||||||
from src.image_model_ids import image_aspect_ratio
|
r = await client.post(_join_checked_gallery_endpoint(base, "/images"), headers=headers, json=payload)
|
||||||
payload["aspect_ratio"] = image_aspect_ratio(source_png.width, source_png.height)
|
|
||||||
from src.ai_interaction import _uses_openrouter_chat_images
|
|
||||||
if _uses_openrouter_chat_images(oa_model):
|
|
||||||
payload = {
|
|
||||||
"model": oa_model,
|
|
||||||
"messages": [{"role": "user", "content": [
|
|
||||||
{"type": "text", "text": payload["prompt"]},
|
|
||||||
*[{"type": "image_url", "image_url": ref["image_url"]}
|
|
||||||
for ref in payload["input_references"]],
|
|
||||||
]}],
|
|
||||||
"modalities": ["image", "text"], "stream": False,
|
|
||||||
"image_config": {"aspect_ratio": payload["aspect_ratio"]},
|
|
||||||
}
|
|
||||||
r = await client.post(_join_checked_gallery_endpoint(base, "/chat/completions"), headers=headers, json=payload)
|
|
||||||
else:
|
|
||||||
r = await client.post(_join_checked_gallery_endpoint(base, "/images"), headers=headers, json=payload)
|
|
||||||
else:
|
else:
|
||||||
r = await client.post(_join_checked_gallery_endpoint(base, "/images/edits"), headers=headers, data=data, files=files)
|
r = await client.post(_join_checked_gallery_endpoint(base, "/images/edits"), headers=headers, data=data, files=files)
|
||||||
if r.status_code != 200:
|
if r.status_code != 200:
|
||||||
@@ -1931,12 +1885,6 @@ def setup_gallery_routes() -> APIRouter:
|
|||||||
pass
|
pass
|
||||||
raise HTTPException(r.status_code, f"{_detail} (provider HTTP {r.status_code})")
|
raise HTTPException(r.status_code, f"{_detail} (provider HTTP {r.status_code})")
|
||||||
result = r.json()
|
result = r.json()
|
||||||
from src.ai_interaction import _chat_image_results, _image_response_error
|
|
||||||
provider_error = _image_response_error(result, oa_model)
|
|
||||||
if provider_error:
|
|
||||||
raise HTTPException(400 if "deprecated" in provider_error.lower() else 502, provider_error)
|
|
||||||
if is_openrouter and _uses_openrouter_chat_images(oa_model):
|
|
||||||
result = {"data": _chat_image_results(result)}
|
|
||||||
raw_b64 = None
|
raw_b64 = None
|
||||||
if result.get("data"):
|
if result.get("data"):
|
||||||
item = result["data"][0]
|
item = result["data"][0]
|
||||||
@@ -1956,13 +1904,17 @@ def setup_gallery_routes() -> APIRouter:
|
|||||||
# the masked region actually changes.
|
# the masked region actually changes.
|
||||||
try:
|
try:
|
||||||
generated = Image.open(io.BytesIO(base64.b64decode(raw_b64))).convert("RGBA")
|
generated = Image.open(io.BytesIO(base64.b64decode(raw_b64))).convert("RGBA")
|
||||||
blended, full_frame = _compose_cloud_image_edit(
|
# Match the generated image to the source dims.
|
||||||
source_png, generated, mask_png,
|
if generated.size != source_png.size:
|
||||||
instruction=body.get("mode") == "instruction",
|
generated = generated.resize(source_png.size, Image.LANCZOS)
|
||||||
)
|
# mask_png: white = regenerate (use generated),
|
||||||
|
# black = keep (use source).
|
||||||
|
# Composite: result = source * (1 - mask_norm) + generated * mask_norm
|
||||||
|
# Image.composite does exactly that with `mask`.
|
||||||
|
blended = Image.composite(generated, source_png, mask_png)
|
||||||
out_buf = io.BytesIO()
|
out_buf = io.BytesIO()
|
||||||
blended.save(out_buf, format="PNG")
|
blended.save(out_buf, format="PNG")
|
||||||
return {"image": base64.b64encode(out_buf.getvalue()).decode(), "model": oa_model, "full_frame": full_frame}
|
return {"image": base64.b64encode(out_buf.getvalue()).decode(), "model": oa_model}
|
||||||
except Exception as comp_err:
|
except Exception as comp_err:
|
||||||
# Never return a full-image edit when masking fails.
|
# Never return a full-image edit when masking fails.
|
||||||
logger.warning(f"Inpaint compose failed: {comp_err}")
|
logger.warning(f"Inpaint compose failed: {comp_err}")
|
||||||
@@ -2619,9 +2571,7 @@ def setup_gallery_routes() -> APIRouter:
|
|||||||
height = int((body or {}).get("height") or 0)
|
height = int((body or {}).get("height") or 0)
|
||||||
except (TypeError, ValueError):
|
except (TypeError, ValueError):
|
||||||
width = height = 0
|
width = height = 0
|
||||||
if width > 0 and height > 0:
|
if height > width:
|
||||||
size = f"{width}x{height}"
|
|
||||||
elif height > width:
|
|
||||||
size = "1024x1536"
|
size = "1024x1536"
|
||||||
elif width > height:
|
elif width > height:
|
||||||
size = "1536x1024"
|
size = "1536x1024"
|
||||||
@@ -2634,9 +2584,6 @@ def setup_gallery_routes() -> APIRouter:
|
|||||||
# The editor's model picker may choose a specific (cloud) image model;
|
# The editor's model picker may choose a specific (cloud) image model;
|
||||||
# forward it so settings defaults only apply when none is selected.
|
# forward it so settings defaults only apply when none is selected.
|
||||||
model_choice = str((body or {}).get("model") or "").strip()
|
model_choice = str((body or {}).get("model") or "").strip()
|
||||||
if body.get("_use_image_settings") is True:
|
|
||||||
from src.editor_image_settings import editor_image_models
|
|
||||||
model_choice = editor_image_models(owner=user)["generation"]
|
|
||||||
payload = {"prompt": prompt, "size": size}
|
payload = {"prompt": prompt, "size": size}
|
||||||
if model_choice:
|
if model_choice:
|
||||||
payload["model"] = model_choice
|
payload["model"] = model_choice
|
||||||
|
|||||||
@@ -118,12 +118,8 @@ def setup_prefs_routes():
|
|||||||
async def set_pref(request: Request, key: str, body: dict):
|
async def set_pref(request: Request, key: str, body: dict):
|
||||||
user = get_current_user(request)
|
user = get_current_user(request)
|
||||||
prefs = _load_for_user(user)
|
prefs = _load_for_user(user)
|
||||||
val = body.get("value")
|
prefs[key] = body.get("value")
|
||||||
if val is None:
|
|
||||||
prefs.pop(key, None)
|
|
||||||
else:
|
|
||||||
prefs[key] = val
|
|
||||||
_save_for_user(user, prefs)
|
_save_for_user(user, prefs)
|
||||||
return {"key": key, "value": prefs.get(key)}
|
return {"key": key, "value": prefs[key]}
|
||||||
|
|
||||||
return router
|
return router
|
||||||
|
|||||||
@@ -77,13 +77,14 @@ def sanitize_body_text(text: str) -> str:
|
|||||||
|
|
||||||
# 1. Try JSON
|
# 1. Try JSON
|
||||||
trimmed = text.strip()
|
trimmed = text.strip()
|
||||||
if (trimmed.startswith("{") and trimmed.endswith("}")) or (trimmed.startswith("[") and trimmed.endswith("]")):
|
if trimmed.startswith(("{", "[")):
|
||||||
try:
|
try:
|
||||||
parsed = json.loads(trimmed)
|
parsed = json.loads(trimmed)
|
||||||
sanitized = sanitize_structured_obj(parsed)
|
sanitized = sanitize_structured_obj(parsed)
|
||||||
return json.dumps(sanitized, ensure_ascii=False)
|
return json.dumps(sanitized, ensure_ascii=False)
|
||||||
except Exception:
|
except Exception as exc:
|
||||||
pass
|
if SENSITIVE_KEY_PATTERN.search(trimmed) or any(p in trimmed.lower() for p in SENSITIVE_PARAM_NAMES):
|
||||||
|
raise ValueError(f"Malformed sensitive JSON request body: {exc}") from exc
|
||||||
|
|
||||||
# 2. Try URL-encoded form data
|
# 2. Try URL-encoded form data
|
||||||
if "=" in trimmed and not trimmed.startswith("<"):
|
if "=" in trimmed and not trimmed.startswith("<"):
|
||||||
@@ -98,8 +99,13 @@ def sanitize_body_text(text: str) -> str:
|
|||||||
else:
|
else:
|
||||||
out_pairs.append((k, v))
|
out_pairs.append((k, v))
|
||||||
return urllib.parse.urlencode(out_pairs)
|
return urllib.parse.urlencode(out_pairs)
|
||||||
except Exception:
|
except Exception as exc:
|
||||||
pass
|
if SENSITIVE_KEY_PATTERN.search(trimmed):
|
||||||
|
raise ValueError(f"Malformed sensitive form request body: {exc}") from exc
|
||||||
|
|
||||||
|
# If unparsed body text contains sensitive keys or tokens, fail closed
|
||||||
|
if SENSITIVE_KEY_PATTERN.search(trimmed) or any(p in trimmed.lower() for p in SENSITIVE_PARAM_NAMES):
|
||||||
|
raise ValueError("Malformed or unsupported sensitive request body in trace entry")
|
||||||
|
|
||||||
return text
|
return text
|
||||||
|
|
||||||
@@ -207,7 +213,11 @@ def sanitize_structured_obj(obj: Any) -> Any:
|
|||||||
|
|
||||||
|
|
||||||
def sanitize_jsonl_content(data: bytes) -> bytes:
|
def sanitize_jsonl_content(data: bytes) -> bytes:
|
||||||
"""Sanitize newline-delimited JSON Lines (.trace, .network, .stacks)."""
|
"""Sanitize newline-delimited JSON Lines (.trace, .network, .stacks).
|
||||||
|
|
||||||
|
If any non-empty record cannot be decoded as valid UTF-8 or parsed as valid JSON,
|
||||||
|
or is malformed, raise ValueError fail-closed so the affected archive is permanently removed.
|
||||||
|
"""
|
||||||
lines = data.split(b"\n")
|
lines = data.split(b"\n")
|
||||||
out_lines = []
|
out_lines = []
|
||||||
for line in lines:
|
for line in lines:
|
||||||
@@ -216,10 +226,8 @@ def sanitize_jsonl_content(data: bytes) -> bytes:
|
|||||||
continue
|
continue
|
||||||
try:
|
try:
|
||||||
parsed = json.loads(line.decode("utf-8"))
|
parsed = json.loads(line.decode("utf-8"))
|
||||||
except (json.JSONDecodeError, UnicodeDecodeError):
|
except (json.JSONDecodeError, UnicodeDecodeError) as err:
|
||||||
# Not valid JSON, keep as-is
|
raise ValueError(f"Malformed or unsupported JSONL trace record: {err}")
|
||||||
out_lines.append(line)
|
|
||||||
continue
|
|
||||||
sanitized = sanitize_structured_obj(parsed)
|
sanitized = sanitize_structured_obj(parsed)
|
||||||
out_lines.append(json.dumps(sanitized, ensure_ascii=False).encode("utf-8"))
|
out_lines.append(json.dumps(sanitized, ensure_ascii=False).encode("utf-8"))
|
||||||
return b"\n".join(out_lines)
|
return b"\n".join(out_lines)
|
||||||
@@ -275,11 +283,10 @@ def sanitize_trace_archive(zp: str) -> bool:
|
|||||||
elif filename_lower.endswith((".json", ".har")):
|
elif filename_lower.endswith((".json", ".har")):
|
||||||
try:
|
try:
|
||||||
parsed = json.loads(content.decode("utf-8"))
|
parsed = json.loads(content.decode("utf-8"))
|
||||||
sanitized = sanitize_structured_obj(parsed)
|
except (json.JSONDecodeError, UnicodeDecodeError) as exc:
|
||||||
zout.writestr(item, json.dumps(sanitized, ensure_ascii=False).encode("utf-8"))
|
raise ValueError(f"Malformed JSON in archive member {item.filename}: {exc}") from exc
|
||||||
except Exception:
|
sanitized = sanitize_structured_obj(parsed)
|
||||||
# If invalid JSON, treat as text line-by-line
|
zout.writestr(item, json.dumps(sanitized, ensure_ascii=False).encode("utf-8"))
|
||||||
zout.writestr(item, sanitize_jsonl_content(content))
|
|
||||||
else:
|
else:
|
||||||
# Unknown binary or text member: exclude to fail closed
|
# Unknown binary or text member: exclude to fail closed
|
||||||
continue
|
continue
|
||||||
|
|||||||
@@ -191,7 +191,7 @@ def _persist_png(image_bytes: bytes, owner: Optional[str], session_id: Optional[
|
|||||||
db.close()
|
db.close()
|
||||||
|
|
||||||
|
|
||||||
async def _place_layer(data_url: str, name: str, owner: Optional[str], session: str, *, masked: bool = False, blend: str = "normal", opacity: float = 100.0, mode: Optional[str] = None, native_size: bool = False) -> bool:
|
async def _place_layer(data_url: str, name: str, owner: Optional[str], session: str, *, masked: bool = False, blend: str = "normal", opacity: float = 100.0, mode: Optional[str] = None) -> bool:
|
||||||
params: Dict[str, Any] = {
|
params: Dict[str, Any] = {
|
||||||
"dataUrl": data_url,
|
"dataUrl": data_url,
|
||||||
"name": name,
|
"name": name,
|
||||||
@@ -199,8 +199,6 @@ async def _place_layer(data_url: str, name: str, owner: Optional[str], session:
|
|||||||
"blend": blend or "normal",
|
"blend": blend or "normal",
|
||||||
"opacity": opacity,
|
"opacity": opacity,
|
||||||
}
|
}
|
||||||
if native_size:
|
|
||||||
params["nativeSize"] = True
|
|
||||||
# Forward the edit mode so the client can build a difference mask for
|
# Forward the edit mode so the client can build a difference mask for
|
||||||
# instruction-mode results: the whole image was regenerated under an
|
# instruction-mode results: the whole image was regenerated under an
|
||||||
# instruction prompt, so untouched pixels must stay pixel-identical (the
|
# instruction prompt, so untouched pixels must stay pixel-identical (the
|
||||||
@@ -508,8 +506,6 @@ class AiEditImageTool:
|
|||||||
}
|
}
|
||||||
_t["image"] = time.perf_counter()
|
_t["image"] = time.perf_counter()
|
||||||
payload: Dict[str, Any] = {"image": image}
|
payload: Dict[str, Any] = {"image": image}
|
||||||
if args.get("_use_image_settings") is True:
|
|
||||||
payload["_use_image_settings"] = True
|
|
||||||
model_choice = str(args.get("model") or "").strip()
|
model_choice = str(args.get("model") or "").strip()
|
||||||
mask_used = False
|
mask_used = False
|
||||||
place_mode: Optional[str] = None
|
place_mode: Optional[str] = None
|
||||||
@@ -613,11 +609,10 @@ class AiEditImageTool:
|
|||||||
f"AI {op}",
|
f"AI {op}",
|
||||||
owner,
|
owner,
|
||||||
session,
|
session,
|
||||||
masked=mask_used and data.get("full_frame") is not True,
|
masked=mask_used,
|
||||||
blend=str(args.get("blend") or "normal"),
|
blend=str(args.get("blend") or "normal"),
|
||||||
opacity=float(args.get("opacity") or 100),
|
opacity=float(args.get("opacity") or 100),
|
||||||
mode=place_mode,
|
mode=place_mode,
|
||||||
native_size=data.get("full_frame") is True,
|
|
||||||
)
|
)
|
||||||
info["placed"] = placed
|
info["placed"] = placed
|
||||||
if placed:
|
if placed:
|
||||||
@@ -692,7 +687,6 @@ class GenerateImageLayerTool:
|
|||||||
str(args.get("name") or "Generated image"),
|
str(args.get("name") or "Generated image"),
|
||||||
owner,
|
owner,
|
||||||
session,
|
session,
|
||||||
native_size=True,
|
|
||||||
masked=bool(args.get("mask")),
|
masked=bool(args.get("mask")),
|
||||||
blend=str(args.get("blend") or "normal"),
|
blend=str(args.get("blend") or "normal"),
|
||||||
opacity=float(args.get("opacity") or 100),
|
opacity=float(args.get("opacity") or 100),
|
||||||
|
|||||||
+17
-130
@@ -179,13 +179,8 @@ def _resolve_model(spec: str, owner: Optional[str] = None, model_type: Optional[
|
|||||||
# served internal id. Include pinned/cached ids in the match set
|
# served internal id. Include pinned/cached ids in the match set
|
||||||
# so chat sessions using the HF repo id still resolve. Do not use
|
# so chat sessions using the HF repo id still resolve. Do not use
|
||||||
# stale cached aliases when the endpoint itself is unreachable.
|
# stale cached aliases when the endpoint itself is unreachable.
|
||||||
if endpoint_reachable:
|
if model_type == "image" and endpoint_reachable:
|
||||||
# Gateway image models may live in a dedicated catalogue,
|
for extra in _json_list(getattr(ep, "pinned_models", None)) + _json_list(getattr(ep, "cached_models", None)):
|
||||||
# so explicitly pinned IDs are valid on mixed endpoints too.
|
|
||||||
extras = _json_list(getattr(ep, "pinned_models", None))
|
|
||||||
if model_type == "image":
|
|
||||||
extras += _json_list(getattr(ep, "cached_models", None))
|
|
||||||
for extra in extras:
|
|
||||||
if extra not in model_ids:
|
if extra not in model_ids:
|
||||||
model_ids.append(extra)
|
model_ids.append(extra)
|
||||||
|
|
||||||
@@ -1181,9 +1176,9 @@ def image_editing_provider_available(owner: Optional[str] = None) -> bool:
|
|||||||
request on this so it never falls back to deterministic approximations.
|
request on this so it never falls back to deterministic approximations.
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
from src.settings import get_user_setting, load_settings
|
from src.settings import load_settings
|
||||||
|
|
||||||
if not load_settings().get("image_gen_enabled", True) or not get_user_setting("image_gen_enabled", owner=owner or "", default=True):
|
if not load_settings().get("image_gen_enabled", True):
|
||||||
return False
|
return False
|
||||||
except Exception:
|
except Exception:
|
||||||
pass
|
pass
|
||||||
@@ -1205,39 +1200,6 @@ def image_editing_provider_available(owner: Optional[str] = None) -> bool:
|
|||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
def _uses_openrouter_chat_images(model_id: str) -> bool:
|
|
||||||
# GPT-5 Image is an LLM wrapper, unlike native gpt-image-* image models.
|
|
||||||
leaf = model_id_leaf(model_id).lower()
|
|
||||||
return leaf.startswith("gpt-") and "-image" in leaf and not leaf.startswith("gpt-image-")
|
|
||||||
|
|
||||||
|
|
||||||
def _chat_image_results(data: dict) -> list:
|
|
||||||
images = []
|
|
||||||
for choice in data.get("choices") or []:
|
|
||||||
message = choice.get("message") or {}
|
|
||||||
for item in message.get("images") or []:
|
|
||||||
url = (item.get("image_url") or {}).get("url")
|
|
||||||
if not isinstance(url, str):
|
|
||||||
continue
|
|
||||||
if url.startswith("data:image/") and ";base64," in url:
|
|
||||||
images.append({"b64_json": url.split(",", 1)[1]})
|
|
||||||
elif url.startswith(("https://", "http://")):
|
|
||||||
images.append({"url": url})
|
|
||||||
return images
|
|
||||||
|
|
||||||
|
|
||||||
def _image_response_error(data: dict, model_id: str) -> Optional[str]:
|
|
||||||
errors = [data.get("error")]
|
|
||||||
errors.extend(choice.get("error") for choice in data.get("choices") or [])
|
|
||||||
for error in errors:
|
|
||||||
if not error:
|
|
||||||
continue
|
|
||||||
message = str(error.get("message") or "Image provider failed") if isinstance(error, dict) else str(error)
|
|
||||||
hint = " Choose a currently supported image model in AI settings." if "deprecated" in message.lower() else ""
|
|
||||||
return f"Image provider failed for '{model_id}': {message}{hint}"
|
|
||||||
return None
|
|
||||||
|
|
||||||
|
|
||||||
async def do_generate_image(content: str, session_id: Optional[str] = None, owner: Optional[str] = None, *, incognito: bool = False) -> Dict:
|
async def do_generate_image(content: str, session_id: Optional[str] = None, owner: Optional[str] = None, *, incognito: bool = False) -> Dict:
|
||||||
"""Generate an image using an image-capable model (e.g. gpt-image-1).
|
"""Generate an image using an image-capable model (e.g. gpt-image-1).
|
||||||
|
|
||||||
@@ -1274,27 +1236,21 @@ async def do_generate_image(content: str, session_id: Optional[str] = None, owne
|
|||||||
if not prompt:
|
if not prompt:
|
||||||
return {"error": "Image prompt is required (line 1)"}
|
return {"error": "Image prompt is required (line 1)"}
|
||||||
|
|
||||||
# Load settings for defaults
|
# Load admin settings for defaults
|
||||||
try:
|
try:
|
||||||
from src.settings import get_user_setting, load_settings
|
from src.settings import load_settings
|
||||||
_settings = load_settings()
|
_settings = load_settings()
|
||||||
except Exception:
|
except Exception:
|
||||||
_settings = {}
|
_settings = {}
|
||||||
def get_user_setting(key, owner="", default=None):
|
|
||||||
return default
|
|
||||||
|
|
||||||
# The administrator's global image-generation disable switch is authoritative.
|
|
||||||
# Non-admin preferences cannot re-enable generation when disabled globally.
|
|
||||||
if not _settings.get("image_gen_enabled", True):
|
if not _settings.get("image_gen_enabled", True):
|
||||||
return {"error": "Image generation is disabled by the administrator."}
|
return {"error": "Image generation is disabled by the administrator."}
|
||||||
if not get_user_setting("image_gen_enabled", owner=owner or "", default=True):
|
|
||||||
return {"error": "Image generation is disabled by user preference."}
|
|
||||||
|
|
||||||
# Use user- or admin-configured model/quality if not specified by the tool call
|
# Use admin-configured model/quality if not specified by the tool call
|
||||||
if not model_spec:
|
if not model_spec:
|
||||||
model_spec = get_user_setting("image_model", owner=owner, default="") or ""
|
model_spec = _settings.get("image_model", "")
|
||||||
if not quality:
|
if not quality:
|
||||||
quality = get_user_setting("image_quality", owner=owner, default="medium") or "medium"
|
quality = _settings.get("image_quality") or "medium"
|
||||||
|
|
||||||
# Auto-detect best available image model if still not set
|
# Auto-detect best available image model if still not set
|
||||||
if not model_spec:
|
if not model_spec:
|
||||||
@@ -1386,19 +1342,8 @@ async def do_generate_image(content: str, session_id: Optional[str] = None, owne
|
|||||||
base_url = url.replace("/chat/completions", "").replace("/v1/messages", "").rstrip("/")
|
base_url = url.replace("/chat/completions", "").replace("/v1/messages", "").rstrip("/")
|
||||||
images_url = base_url + "/images/generations"
|
images_url = base_url + "/images/generations"
|
||||||
from src.model_capability_readers.base import detect_vendor
|
from src.model_capability_readers.base import detect_vendor
|
||||||
chat_images = detect_vendor(url) == "openrouter" and _uses_openrouter_chat_images(model_id)
|
|
||||||
if detect_vendor(url) == "openrouter":
|
if detect_vendor(url) == "openrouter":
|
||||||
images_url = base_url + "/images"
|
images_url = base_url + "/images"
|
||||||
if chat_images:
|
|
||||||
images_url = base_url + "/chat/completions"
|
|
||||||
|
|
||||||
# Capture the requested geometry before older OpenAI size restrictions.
|
|
||||||
from src.image_model_ids import image_aspect_ratio
|
|
||||||
try:
|
|
||||||
requested_width, requested_height = (int(value) for value in size.lower().split("x"))
|
|
||||||
requested_ratio = image_aspect_ratio(requested_width, requested_height)
|
|
||||||
except (TypeError, ValueError):
|
|
||||||
requested_ratio = "1:1"
|
|
||||||
|
|
||||||
# Validate size for cloud image models (local diffusion accepts any WxH)
|
# Validate size for cloud image models (local diffusion accepts any WxH)
|
||||||
valid_gpt_sizes = {"1024x1024", "1024x1536", "1536x1024", "auto"}
|
valid_gpt_sizes = {"1024x1024", "1024x1536", "1536x1024", "auto"}
|
||||||
@@ -1414,9 +1359,6 @@ async def do_generate_image(content: str, session_id: Optional[str] = None, owne
|
|||||||
"n": 1,
|
"n": 1,
|
||||||
"size": size,
|
"size": size,
|
||||||
}
|
}
|
||||||
if detect_vendor(url) == "openrouter":
|
|
||||||
payload.pop("size", None)
|
|
||||||
payload["aspect_ratio"] = requested_ratio
|
|
||||||
|
|
||||||
# GPT image models and local diffusion support quality; DALL-E does not
|
# GPT image models and local diffusion support quality; DALL-E does not
|
||||||
if is_gpt_image or is_local_diffusion:
|
if is_gpt_image or is_local_diffusion:
|
||||||
@@ -1430,16 +1372,7 @@ async def do_generate_image(content: str, session_id: Optional[str] = None, owne
|
|||||||
try:
|
try:
|
||||||
# GPT image models can take 30-120s+ depending on quality
|
# GPT image models can take 30-120s+ depending on quality
|
||||||
async with httpx.AsyncClient(timeout=httpx.Timeout(connect=30.0, read=300.0, write=30.0, pool=30.0)) as client:
|
async with httpx.AsyncClient(timeout=httpx.Timeout(connect=30.0, read=300.0, write=30.0, pool=30.0)) as client:
|
||||||
request_payload = payload
|
resp = await client.post(images_url, json=payload, headers=headers)
|
||||||
if chat_images:
|
|
||||||
request_payload = {
|
|
||||||
"model": model_id,
|
|
||||||
"messages": [{"role": "user", "content": prompt}],
|
|
||||||
"modalities": ["image", "text"],
|
|
||||||
"stream": False,
|
|
||||||
"image_config": {"aspect_ratio": requested_ratio},
|
|
||||||
}
|
|
||||||
resp = await client.post(images_url, json=request_payload, headers=headers)
|
|
||||||
|
|
||||||
if resp.status_code != 200:
|
if resp.status_code != 200:
|
||||||
error_text = resp.text[:500]
|
error_text = resp.text[:500]
|
||||||
@@ -1454,15 +1387,9 @@ async def do_generate_image(content: str, session_id: Optional[str] = None, owne
|
|||||||
}
|
}
|
||||||
|
|
||||||
data = resp.json()
|
data = resp.json()
|
||||||
provider_error = _image_response_error(data, model_id)
|
images = data.get("data", [])
|
||||||
if provider_error:
|
|
||||||
return {"error": provider_error, "untrusted_content": True}
|
|
||||||
images = _chat_image_results(data) if chat_images else data.get("data", [])
|
|
||||||
if not images:
|
if not images:
|
||||||
return {"error": (
|
return {"error": "No images returned from API"}
|
||||||
f"Image model '{model_id}' returned no image. "
|
|
||||||
"Choose another image generation model in AI settings."
|
|
||||||
)}
|
|
||||||
|
|
||||||
img = images[0]
|
img = images[0]
|
||||||
image_url = None
|
image_url = None
|
||||||
@@ -1575,27 +1502,21 @@ async def do_edit_image(
|
|||||||
return {"error": "Attached image file was not found"}
|
return {"error": "Attached image file was not found"}
|
||||||
|
|
||||||
try:
|
try:
|
||||||
from src.settings import get_user_setting, load_settings
|
from src.settings import load_settings
|
||||||
_settings = load_settings()
|
_settings = load_settings()
|
||||||
except Exception:
|
except Exception:
|
||||||
_settings = {}
|
_settings = {}
|
||||||
def get_user_setting(key, owner="", default=None):
|
|
||||||
return default
|
|
||||||
|
|
||||||
if not model_spec:
|
if not model_spec:
|
||||||
model_spec = get_user_setting("image_edit_model", owner=owner) or get_user_setting("image_model", owner=owner, default="")
|
model_spec = _settings.get("image_model", "")
|
||||||
if quality is None:
|
if quality is None:
|
||||||
quality = get_user_setting("image_quality", owner=owner, default="medium") or "medium"
|
quality = _settings.get("image_quality") or "medium"
|
||||||
if not model_spec:
|
if not model_spec:
|
||||||
return {"error": "No image model selected for image editing"}
|
return {"error": "No image model selected for image editing"}
|
||||||
|
|
||||||
try:
|
try:
|
||||||
try:
|
try:
|
||||||
def _call():
|
def _call():
|
||||||
try:
|
|
||||||
return _resolve_model(model_spec, owner=owner)
|
|
||||||
except ValueError:
|
|
||||||
pass
|
|
||||||
try:
|
try:
|
||||||
return _resolve_model(model_spec, owner=owner, model_type="image")
|
return _resolve_model(model_spec, owner=owner, model_type="image")
|
||||||
except TypeError as exc:
|
except TypeError as exc:
|
||||||
@@ -1608,7 +1529,6 @@ async def do_edit_image(
|
|||||||
except ValueError:
|
except ValueError:
|
||||||
return {"error": f"No endpoint found with image model '{model_spec}'."}
|
return {"error": f"No endpoint found with image model '{model_spec}'."}
|
||||||
|
|
||||||
requested_edit_size = size
|
|
||||||
if not size or size == "auto":
|
if not size or size == "auto":
|
||||||
from PIL import Image
|
from PIL import Image
|
||||||
from src.image_model_ids import image_edit_size
|
from src.image_model_ids import image_edit_size
|
||||||
@@ -1784,35 +1704,7 @@ async def do_edit_image(
|
|||||||
"image_url": {"url": f"data:{mime};base64,{image_b64}"},
|
"image_url": {"url": f"data:{mime};base64,{image_b64}"},
|
||||||
}],
|
}],
|
||||||
}
|
}
|
||||||
from src.image_model_ids import image_aspect_ratio
|
resp = await client.post(base_url + "/images", json=edit_payload, headers=headers)
|
||||||
from PIL import Image as ReferenceImage
|
|
||||||
with ReferenceImage.open(path) as reference:
|
|
||||||
reference_width, reference_height = reference.size
|
|
||||||
if reference.getexif().get(274) in {5, 6, 7, 8}:
|
|
||||||
reference_width, reference_height = reference_height, reference_width
|
|
||||||
if requested_edit_size and requested_edit_size != "auto":
|
|
||||||
try:
|
|
||||||
reference_width, reference_height = (int(value) for value in requested_edit_size.lower().split("x"))
|
|
||||||
except (TypeError, ValueError):
|
|
||||||
pass
|
|
||||||
edit_payload.pop("size", None)
|
|
||||||
edit_payload["aspect_ratio"] = image_aspect_ratio(reference_width, reference_height)
|
|
||||||
if _uses_openrouter_chat_images(model_id):
|
|
||||||
edit_payload = {
|
|
||||||
"model": model_id,
|
|
||||||
"messages": [{"role": "user", "content": [
|
|
||||||
{"type": "text", "text": prompt},
|
|
||||||
{"type": "image_url", "image_url": {
|
|
||||||
"url": f"data:{mime};base64,{image_b64}",
|
|
||||||
}},
|
|
||||||
]}],
|
|
||||||
"modalities": ["image", "text"],
|
|
||||||
"stream": False,
|
|
||||||
"image_config": {"aspect_ratio": edit_payload["aspect_ratio"]},
|
|
||||||
}
|
|
||||||
resp = await client.post(base_url + "/chat/completions", json=edit_payload, headers=headers)
|
|
||||||
else:
|
|
||||||
resp = await client.post(base_url + "/images", json=edit_payload, headers=headers)
|
|
||||||
else:
|
else:
|
||||||
with path.open("rb") as f:
|
with path.open("rb") as f:
|
||||||
files = {"image": (path.name, f, mime)}
|
files = {"image": (path.name, f, mime)}
|
||||||
@@ -1855,12 +1747,7 @@ async def do_edit_image(
|
|||||||
}
|
}
|
||||||
|
|
||||||
data = resp.json()
|
data = resp.json()
|
||||||
provider_error = _image_response_error(data, model_id)
|
images = data.get("data", [])
|
||||||
if provider_error:
|
|
||||||
return {"error": provider_error, "untrusted_content": True}
|
|
||||||
images = (_chat_image_results(data)
|
|
||||||
if is_openrouter and _uses_openrouter_chat_images(model_id)
|
|
||||||
else data.get("data", []))
|
|
||||||
if not images:
|
if not images:
|
||||||
return {"error": "No image returned from edit API"}
|
return {"error": "No image returned from edit API"}
|
||||||
|
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ def compact_browser_observation(value, budget=8000):
|
|||||||
# Snapshot text already contains labels and refs in DOM order.
|
# Snapshot text already contains labels and refs in DOM order.
|
||||||
# The refs mapping repeats them and buries menus in raw JSON.
|
# The refs mapping repeats them and buries menus in raw JSON.
|
||||||
lines = [line for line in snapshot.splitlines()
|
lines = [line for line in snapshot.splitlines()
|
||||||
if not re.fullmatch(r'\s*(?:-\s*)?generic(?:\s+\[ref=e\d+\])?:?\s*', line)]
|
if not re.fullmatch(r'\s*-?\s*generic(?:\s+\[ref=e\d+\])?:?\s*', line)]
|
||||||
pages.append(('URL: ' + str(url) + '\n' if url else '') + '\n'.join(lines))
|
pages.append(('URL: ' + str(url) + '\n' if url else '') + '\n'.join(lines))
|
||||||
elif url:
|
elif url:
|
||||||
notices.append('URL: ' + str(url))
|
notices.append('URL: ' + str(url))
|
||||||
@@ -37,8 +37,7 @@ def compact_browser_observation(value, budget=8000):
|
|||||||
parsed = json.loads(item)
|
parsed = json.loads(item)
|
||||||
except (ValueError, TypeError):
|
except (ValueError, TypeError):
|
||||||
# CLI status text precedes the JSON post-interaction state.
|
# CLI status text precedes the JSON post-interaction state.
|
||||||
# Labels are one line; stopping at \n keeps the split linear.
|
parts = re.split(r'\n\n\[(?:post-[^\]]+|page state after failed [^\]]+)\]\n', item)
|
||||||
parts = re.split(r'\n\n\[(?:post-[^\]\n]+|page state after failed [^\]\n]+)\]\n', item)
|
|
||||||
if len(parts) > 1:
|
if len(parts) > 1:
|
||||||
for part in parts:
|
for part in parts:
|
||||||
visit(part)
|
visit(part)
|
||||||
|
|||||||
@@ -1229,7 +1229,7 @@ def tasks_terminal_response(raw, *, user_text='', max_items=20):
|
|||||||
text = str(payload or '').strip()
|
text = str(payload or '').strip()
|
||||||
rows = []
|
rows = []
|
||||||
for line in text.splitlines():
|
for line in text.splitlines():
|
||||||
match = re.match(r'^\s*\d+\.\s(.*?)\s\(([^()]+)\)\s+—\s+(.+)$', line)
|
match = re.match(r'^\s*\d+\.\s+(.*?)\s+\(([^)]+)\)\s+—\s+(.+)$', line)
|
||||||
if match:
|
if match:
|
||||||
details = match[3].strip()
|
details = match[3].strip()
|
||||||
rows.append({
|
rows.append({
|
||||||
@@ -1346,7 +1346,7 @@ def skills_terminal_response(raw, *, user_text='', max_items=20):
|
|||||||
continue
|
continue
|
||||||
match = re.match(r'^\s*-\s+\*\*([^*]+)\*\*(?:\s+\(([^)]+)\))?', line)
|
match = re.match(r'^\s*-\s+\*\*([^*]+)\*\*(?:\s+\(([^)]+)\))?', line)
|
||||||
if not match:
|
if not match:
|
||||||
match = re.match(r'^\s*-\s+([^\s:][^:]*?)(?:\s\(([^()]+)\)(?:\s*:|$)|:|$)', line)
|
match = re.match(r'^\s*-\s+([^:]+?)(?:\s+\(([^)]+)\))?(?:\s*:|$)', line)
|
||||||
if match and status:
|
if match and status:
|
||||||
rows.append((status, match.group(1).strip(), (match.group(2) or '').strip()))
|
rows.append((status, match.group(1).strip(), (match.group(2) or '').strip()))
|
||||||
if not rows:
|
if not rows:
|
||||||
@@ -5065,7 +5065,7 @@ def email_search_result_empty(value):
|
|||||||
try:
|
try:
|
||||||
decoded = json.loads(value)
|
decoded = json.loads(value)
|
||||||
except (TypeError, ValueError):
|
except (TypeError, ValueError):
|
||||||
return bool(re.fullmatch(r'\s*No emails matched [^\n]+(?:\n\s*)?', value, re.I))
|
return bool(re.fullmatch(r'\s*No emails matched [^\n]+\.?\s*', value, re.I))
|
||||||
if decoded == value:
|
if decoded == value:
|
||||||
return False
|
return False
|
||||||
value = decoded
|
value = decoded
|
||||||
@@ -5339,16 +5339,6 @@ def private_browser_effective_url(result):
|
|||||||
return urls[-1] if urls else ''
|
return urls[-1] if urls else ''
|
||||||
|
|
||||||
|
|
||||||
def is_google_sorry_url(url):
|
|
||||||
"""Recognize Google's rate-limit interstitial."""
|
|
||||||
# Matching the host separately keeps this linear; one combined pattern
|
|
||||||
# rescans the host from every dot.
|
|
||||||
return any(
|
|
||||||
re.fullmatch(r'(?:[^/]+\.)?google\.[^/]+', host[1], re.I)
|
|
||||||
for host in re.finditer(r'https?://([^/]+)/sorry/', url, re.I)
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def browser_observation_page_missing(raw):
|
def browser_observation_page_missing(raw):
|
||||||
"""Recognize a rendered error page, rather than an article mentioning 404."""
|
"""Recognize a rendered error page, rather than an article mentioning 404."""
|
||||||
text = str(raw or '')
|
text = str(raw or '')
|
||||||
@@ -8223,7 +8213,11 @@ async def stream_preview(*, endpoint_url, model, messages, headers, turn_contrac
|
|||||||
browser_search_url = requested_browser_url or effective_browser_url
|
browser_search_url = requested_browser_url or effective_browser_url
|
||||||
is_search_engine_navigation = contains_search_engine_navigation(
|
is_search_engine_navigation = contains_search_engine_navigation(
|
||||||
browser_search_url
|
browser_search_url
|
||||||
) or is_google_sorry_url(effective_browser_url)
|
) or bool(re.search(
|
||||||
|
r'https?://(?:[^/]+\.)?google\.[^/]+/sorry/',
|
||||||
|
effective_browser_url,
|
||||||
|
re.I,
|
||||||
|
))
|
||||||
has_native_search = any(
|
has_native_search = any(
|
||||||
canonical(schema['function']['name']) == 'web_search'
|
canonical(schema['function']['name']) == 'web_search'
|
||||||
for schema in offered
|
for schema in offered
|
||||||
|
|||||||
@@ -1,19 +0,0 @@
|
|||||||
"""The shared model defaults used by Quick Edit's image operations."""
|
|
||||||
from src.settings import get_user_setting, load_settings
|
|
||||||
|
|
||||||
|
|
||||||
def editor_image_models(owner: str = "") -> dict[str, str]:
|
|
||||||
settings = load_settings()
|
|
||||||
generation = ""
|
|
||||||
edit_model = ""
|
|
||||||
if owner:
|
|
||||||
generation = str(get_user_setting("image_model", owner=owner, default="") or "").strip()
|
|
||||||
edit_model = str(get_user_setting("image_edit_model", owner=owner, default="") or "").strip()
|
|
||||||
if not generation:
|
|
||||||
generation = str(settings.get("image_model") or "").strip()
|
|
||||||
if not edit_model:
|
|
||||||
edit_model = str(settings.get("image_edit_model") or generation).strip()
|
|
||||||
return {
|
|
||||||
"generation": generation,
|
|
||||||
"edit": edit_model or generation,
|
|
||||||
}
|
|
||||||
@@ -40,15 +40,6 @@ def image_edit_size(model_id: str, width: int, height: int) -> str:
|
|||||||
return f"{width}x{height}"
|
return f"{width}x{height}"
|
||||||
|
|
||||||
|
|
||||||
def image_aspect_ratio(width: int, height: int) -> str:
|
|
||||||
"""Choose the nearest common Image API ratio, without distorting pixels."""
|
|
||||||
if width <= 0 or height <= 0:
|
|
||||||
raise ValueError("Image dimensions must be positive")
|
|
||||||
ratios = ((1, 1), (16, 9), (9, 16), (3, 2), (2, 3), (4, 3), (3, 4), (21, 9))
|
|
||||||
w, h = min(ratios, key=lambda ratio: abs(math.log((ratio[0] / ratio[1]) / (width / height))))
|
|
||||||
return f"{w}:{h}"
|
|
||||||
|
|
||||||
|
|
||||||
def looks_like_image_generation_model(model_id: str) -> bool:
|
def looks_like_image_generation_model(model_id: str) -> bool:
|
||||||
"""Return True when a model id should use image generation routes.
|
"""Return True when a model id should use image generation routes.
|
||||||
|
|
||||||
|
|||||||
+12
-37
@@ -422,18 +422,7 @@ def _stream_delta_event(text: str, *, thinking: bool = False) -> str:
|
|||||||
return f"data: {json.dumps(payload)}\n\n"
|
return f"data: {json.dumps(payload)}\n\n"
|
||||||
|
|
||||||
|
|
||||||
_WORD_CHARS = r"A-Za-z0-9_\u0370-\u03ff\u0400-\u04ff"
|
_DEGENERATE_WORD_RE = re.compile(r"[A-Za-z0-9_\u0370-\u03ff\u0400-\u04ff]+")
|
||||||
_DELIM_RE = re.compile(rf"[^{_WORD_CHARS}+\-.]")
|
|
||||||
_NUMERIC_PATTERN = rf"(?<![{_WORD_CHARS}])[+-]?(?:[0-9]+\.[0-9]+|\.[0-9]+|[0-9]+)(?:[eE][+-]?[0-9]+)?(?![{_WORD_CHARS}])"
|
|
||||||
_NUMERIC_FULLMATCH_RE = re.compile(rf"^[+-]?(?:[0-9]+\.[0-9]+|\.[0-9]+|[0-9]+)(?:[eE][+-]?[0-9]+)?$")
|
|
||||||
_DEGENERATE_WORD_RE = re.compile(rf"[{_WORD_CHARS}]+")
|
|
||||||
_TOKEN_RE = re.compile(rf"(?P<NUM>{_NUMERIC_PATTERN})|(?P<WORD>[{_WORD_CHARS}]+)")
|
|
||||||
|
|
||||||
|
|
||||||
def _is_numeric_token(token: str) -> bool:
|
|
||||||
if token.isdigit():
|
|
||||||
return True
|
|
||||||
return _NUMERIC_FULLMATCH_RE.fullmatch(token) is not None
|
|
||||||
|
|
||||||
|
|
||||||
class _DegenerateStreamGuard:
|
class _DegenerateStreamGuard:
|
||||||
@@ -455,38 +444,24 @@ class _DegenerateStreamGuard:
|
|||||||
self.recent_tokens: List[str] = []
|
self.recent_tokens: List[str] = []
|
||||||
self.periodic_tokens: List[str] = []
|
self.periodic_tokens: List[str] = []
|
||||||
self.total_chars = 0
|
self.total_chars = 0
|
||||||
self._buffer = ""
|
|
||||||
|
|
||||||
def check(self, text: str) -> Optional[str]:
|
def check(self, text: str) -> Optional[str]:
|
||||||
if not text:
|
if not text:
|
||||||
return None
|
return None
|
||||||
self.total_chars += len(text)
|
self.total_chars += len(text)
|
||||||
self._buffer += text
|
# Keep one-character tokens. Structured output often varies through
|
||||||
|
# numeric selectors or coordinates; dropping those tokens can make
|
||||||
m_delims = list(_DELIM_RE.finditer(self._buffer))
|
# valid CSS/SVG rows look like an exact repeated phrase.
|
||||||
if m_delims:
|
tokens = [t.lower() for t in _DEGENERATE_WORD_RE.findall(text)]
|
||||||
split_idx = m_delims[-1].end()
|
|
||||||
head = self._buffer[:split_idx]
|
|
||||||
self._buffer = self._buffer[split_idx:]
|
|
||||||
elif len(self._buffer) >= 256:
|
|
||||||
head = self._buffer
|
|
||||||
self._buffer = ""
|
|
||||||
else:
|
|
||||||
return None
|
|
||||||
|
|
||||||
tokens = []
|
|
||||||
for m in _TOKEN_RE.finditer(head):
|
|
||||||
tokens.append((m.group(0).lower(), m.lastgroup == "NUM"))
|
|
||||||
if not tokens:
|
if not tokens:
|
||||||
return None
|
return None
|
||||||
|
for token in tokens:
|
||||||
for token, is_num in tokens:
|
|
||||||
if token == self.last_token:
|
if token == self.last_token:
|
||||||
self.same_run += 1
|
self.same_run += 1
|
||||||
else:
|
else:
|
||||||
self.last_token = token
|
self.last_token = token
|
||||||
self.same_run = 1
|
self.same_run = 1
|
||||||
self.numeric_run = self.numeric_run + 1 if is_num else 0
|
self.numeric_run = self.numeric_run + 1 if token.isdigit() else 0
|
||||||
self.recent_tokens.append(token)
|
self.recent_tokens.append(token)
|
||||||
self.periodic_tokens.append(token)
|
self.periodic_tokens.append(token)
|
||||||
if len(self.recent_tokens) > 96:
|
if len(self.recent_tokens) > 96:
|
||||||
@@ -495,19 +470,19 @@ class _DegenerateStreamGuard:
|
|||||||
self.periodic_tokens = self.periodic_tokens[-2048:]
|
self.periodic_tokens = self.periodic_tokens[-2048:]
|
||||||
|
|
||||||
reason = None
|
reason = None
|
||||||
if self.same_run >= 28 and self.total_chars >= 100 and not _is_numeric_token(self.last_token):
|
if self.same_run >= 28 and self.total_chars >= 100 and not self.last_token.isdigit():
|
||||||
reason = f"repeated '{self.last_token}' {self.same_run} times"
|
reason = f"repeated '{self.last_token}' {self.same_run} times"
|
||||||
elif len(self.recent_tokens) >= 72:
|
elif len(self.recent_tokens) >= 72:
|
||||||
top = max(set(self.recent_tokens), key=self.recent_tokens.count)
|
top = max(set(self.recent_tokens), key=self.recent_tokens.count)
|
||||||
count = self.recent_tokens.count(top)
|
count = self.recent_tokens.count(top)
|
||||||
if count >= 60 and count / max(len(self.recent_tokens), 1) >= 0.78 and not _is_numeric_token(top):
|
if count >= 60 and count / max(len(self.recent_tokens), 1) >= 0.78 and not top.isdigit():
|
||||||
reason = f"repeated '{top}' {count}/{len(self.recent_tokens)} recent tokens"
|
reason = f"repeated '{top}' {count}/{len(self.recent_tokens)} recent tokens"
|
||||||
if (
|
if (
|
||||||
not reason
|
not reason
|
||||||
and len(self.recent_tokens) >= 48
|
and len(self.recent_tokens) >= 48
|
||||||
# A window that is mostly numbers is matrix/table data; its
|
# A window that is mostly numbers is matrix/table data; its
|
||||||
# periodicity is real. Mixed loops still hit the exact-block check.
|
# periodicity is real. Mixed loops still hit the exact-block check.
|
||||||
and sum(_is_numeric_token(t) for t in self.recent_tokens) * 2 < len(self.recent_tokens)
|
and sum(t.isdigit() for t in self.recent_tokens) * 2 < len(self.recent_tokens)
|
||||||
):
|
):
|
||||||
# Detect a periodic suffix instead of counting repeated n-grams.
|
# Detect a periodic suffix instead of counting repeated n-grams.
|
||||||
# Reused property names are normal in CSS/SVG/JSON; a collapsed
|
# Reused property names are normal in CSS/SVG/JSON; a collapsed
|
||||||
@@ -543,9 +518,9 @@ class _DegenerateStreamGuard:
|
|||||||
if history[previous:previous + anchor_size] != anchor:
|
if history[previous:previous + anchor_size] != anchor:
|
||||||
continue
|
continue
|
||||||
period = current_anchor - previous
|
period = current_anchor - previous
|
||||||
if period < 24 or len(history) < period * 2 or self.numeric_run >= period:
|
if period < 24 or len(history) < period * 2:
|
||||||
continue
|
continue
|
||||||
if history[-2 * period:-period] == history[-period:] and not all(_is_numeric_token(t) for t in history[-period:]):
|
if history[-2 * period:-period] == history[-period:] and not all(t.isdigit() for t in history[-period:]):
|
||||||
reason = f"repeated an exact {period}-token block twice"
|
reason = f"repeated an exact {period}-token block twice"
|
||||||
break
|
break
|
||||||
|
|
||||||
|
|||||||
+1
-16
@@ -46,7 +46,6 @@ DEFAULT_SETTINGS = {
|
|||||||
"agent_email_confirm": True,
|
"agent_email_confirm": True,
|
||||||
"image_gen_enabled": False,
|
"image_gen_enabled": False,
|
||||||
"image_model": "",
|
"image_model": "",
|
||||||
"image_edit_model": "",
|
|
||||||
"image_quality": "medium",
|
"image_quality": "medium",
|
||||||
# Optional editor planner/caption endpoints. Empty uses configured chat
|
# Optional editor planner/caption endpoints. Empty uses configured chat
|
||||||
# fallback without probing a developer-specific host.
|
# fallback without probing a developer-specific host.
|
||||||
@@ -295,7 +294,7 @@ def is_setting_overridden(key: str) -> bool:
|
|||||||
# resolved by FastAPI deps; an empty/None owner falls through to the global.
|
# resolved by FastAPI deps; an empty/None owner falls through to the global.
|
||||||
_PER_USER_KEYS = {
|
_PER_USER_KEYS = {
|
||||||
"vision_model", "vision_enabled", "vision_model_fallbacks",
|
"vision_model", "vision_enabled", "vision_model_fallbacks",
|
||||||
"image_model", "image_edit_model", "image_gen_enabled", "image_quality",
|
"image_model", "image_gen_enabled", "image_quality",
|
||||||
# Default chat endpoint / model — without per-user resolution every new
|
# Default chat endpoint / model — without per-user resolution every new
|
||||||
# account inherited whatever the most-recent admin picked, which then
|
# account inherited whatever the most-recent admin picked, which then
|
||||||
# got injected into the chat composer on first open.
|
# got injected into the chat composer on first open.
|
||||||
@@ -314,20 +313,6 @@ def get_user_setting(key: str, owner: str = "", default: Any = None) -> Any:
|
|||||||
Falls back gracefully if the prefs module can't be imported (cycle/early
|
Falls back gracefully if the prefs module can't be imported (cycle/early
|
||||||
boot) — admin-global settings keep working.
|
boot) — admin-global settings keep working.
|
||||||
"""
|
"""
|
||||||
if key == "image_gen_enabled":
|
|
||||||
global_val = get_setting("image_gen_enabled", default if default is not None else False)
|
|
||||||
if not global_val:
|
|
||||||
return False
|
|
||||||
if owner:
|
|
||||||
try:
|
|
||||||
from routes.prefs_routes import _load_for_user
|
|
||||||
prefs = _load_for_user(owner) or {}
|
|
||||||
if "image_gen_enabled" in prefs and prefs["image_gen_enabled"] is not None:
|
|
||||||
return bool(prefs["image_gen_enabled"])
|
|
||||||
except Exception:
|
|
||||||
pass
|
|
||||||
return bool(global_val)
|
|
||||||
|
|
||||||
if owner and key in _PER_USER_KEYS:
|
if owner and key in _PER_USER_KEYS:
|
||||||
try:
|
try:
|
||||||
from routes.prefs_routes import _load_for_user
|
from routes.prefs_routes import _load_for_user
|
||||||
|
|||||||
@@ -264,42 +264,7 @@
|
|||||||
/* The brush controls (Color/Size/preset/caret) hug their content; only the
|
/* The brush controls (Color/Size/preset/caret) hug their content; only the
|
||||||
trailing slider section grows, so the Opacity/Flow/Softness group sits
|
trailing slider section grows, so the Opacity/Flow/Softness group sits
|
||||||
right after the caret instead of leaving a wide gap. */
|
right after the caret instead of leaving a wide gap. */
|
||||||
.ge-controls--in-options #ge-brush-controls { flex: 0 0 auto; flex-direction: row; align-items: center; gap: 8px; }
|
.ge-controls--in-options #ge-brush-controls { flex: 0 0 auto; }
|
||||||
.ge-controls--in-options #ge-brush-softness-row input[type="range"] { width: 88px; flex: 0 0 88px; }
|
|
||||||
#ge-brush-softness-row {
|
|
||||||
display: grid !important;
|
|
||||||
grid-template-columns: 14px max-content 88px;
|
|
||||||
column-gap: 8px;
|
|
||||||
align-items: center;
|
|
||||||
}
|
|
||||||
#ge-brush-softness-row:has(> .ge-slider-value) {
|
|
||||||
grid-template-columns: 14px max-content 88px 6ch;
|
|
||||||
}
|
|
||||||
#ge-brush-softness-row label {
|
|
||||||
flex: 0 0 auto;
|
|
||||||
min-width: max-content;
|
|
||||||
gap: 6px;
|
|
||||||
}
|
|
||||||
#ge-brush-softness-row #ge-brush-softness-label {
|
|
||||||
position: static;
|
|
||||||
min-width: 6ch;
|
|
||||||
margin: 0;
|
|
||||||
padding: 0;
|
|
||||||
white-space: nowrap;
|
|
||||||
text-align: right;
|
|
||||||
font-variant-numeric: tabular-nums;
|
|
||||||
}
|
|
||||||
.ge-controls--in-options #ge-brush-controls .ge-brush-size-row {
|
|
||||||
display: grid;
|
|
||||||
grid-template-columns: minmax(6ch, max-content) 96px auto;
|
|
||||||
column-gap: 8px;
|
|
||||||
}
|
|
||||||
.ge-controls--in-options .ge-brush-size-row label,
|
|
||||||
.ge-controls--in-options .ge-brush-size-row .ge-size-label {
|
|
||||||
min-width: 6ch;
|
|
||||||
white-space: nowrap;
|
|
||||||
font-variant-numeric: tabular-nums;
|
|
||||||
}
|
|
||||||
.ge-controls--in-options .ge-control-row {
|
.ge-controls--in-options .ge-control-row {
|
||||||
margin: 0 !important;
|
margin: 0 !important;
|
||||||
flex: 0 0 auto;
|
flex: 0 0 auto;
|
||||||
@@ -4642,8 +4607,6 @@ body.ge-editor-open textarea.ge-direct-text-editor {
|
|||||||
pointer-events: auto;
|
pointer-events: auto;
|
||||||
}
|
}
|
||||||
.ge-filter-modal {
|
.ge-filter-modal {
|
||||||
max-height: calc(100dvh - 40px);
|
|
||||||
overflow-y: auto;
|
|
||||||
width: 240px;
|
width: 240px;
|
||||||
background: color-mix(in srgb, var(--panel) 95%, transparent);
|
background: color-mix(in srgb, var(--panel) 95%, transparent);
|
||||||
border: 1px solid var(--border);
|
border: 1px solid var(--border);
|
||||||
@@ -4668,9 +4631,6 @@ body.ge-editor-open textarea.ge-direct-text-editor {
|
|||||||
gap: 2px;
|
gap: 2px;
|
||||||
}
|
}
|
||||||
.ge-filter-row label {
|
.ge-filter-row label {
|
||||||
flex: 0 0 auto;
|
|
||||||
min-width: 0;
|
|
||||||
width: 100%;
|
|
||||||
font-size: 10px;
|
font-size: 10px;
|
||||||
opacity: 0.7;
|
opacity: 0.7;
|
||||||
display: flex;
|
display: flex;
|
||||||
@@ -9772,28 +9732,6 @@ body.ge-editor-open .tour-hint {
|
|||||||
margin-left: auto;
|
margin-left: auto;
|
||||||
color: var(--color-muted);
|
color: var(--color-muted);
|
||||||
}
|
}
|
||||||
.ge-submenu .dropdown-item-compact {
|
|
||||||
box-sizing: border-box;
|
|
||||||
display: flex;
|
|
||||||
align-items: center;
|
|
||||||
height: 28px;
|
|
||||||
min-height: 28px;
|
|
||||||
max-height: 28px;
|
|
||||||
padding: 0 10px;
|
|
||||||
line-height: 1.2;
|
|
||||||
white-space: nowrap;
|
|
||||||
}
|
|
||||||
.ge-submenu .dropdown-item-compact svg {
|
|
||||||
flex-shrink: 0;
|
|
||||||
max-height: 16px;
|
|
||||||
}
|
|
||||||
@media (max-width: 820px) {
|
|
||||||
.ge-submenu .dropdown-item-compact {
|
|
||||||
height: 32px;
|
|
||||||
min-height: 32px;
|
|
||||||
max-height: 32px;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
.ge-submenu {
|
.ge-submenu {
|
||||||
display: none;
|
display: none;
|
||||||
position: absolute;
|
position: absolute;
|
||||||
|
|||||||
@@ -1677,10 +1677,6 @@
|
|||||||
<label class="settings-label">Model</label>
|
<label class="settings-label">Model</label>
|
||||||
<select id="set-imgModelSelect" class="settings-select"><option value="">Auto-detect</option></select>
|
<select id="set-imgModelSelect" class="settings-select"><option value="">Auto-detect</option></select>
|
||||||
</div>
|
</div>
|
||||||
<div style="display:flex;align-items:center;gap:0.75rem;">
|
|
||||||
<label class="settings-label" for="set-imgEditModelSelect">Image edit model</label>
|
|
||||||
<select id="set-imgEditModelSelect" class="settings-select"><option value="">Same as generation</option></select>
|
|
||||||
</div>
|
|
||||||
<div style="display:flex;align-items:center;gap:0.75rem;">
|
<div style="display:flex;align-items:center;gap:0.75rem;">
|
||||||
<label class="settings-label">Quality</label>
|
<label class="settings-label">Quality</label>
|
||||||
<select id="set-imgQualitySelect" class="settings-select">
|
<select id="set-imgQualitySelect" class="settings-select">
|
||||||
|
|||||||
@@ -41,7 +41,6 @@
|
|||||||
* }} deps
|
* }} deps
|
||||||
*/
|
*/
|
||||||
import { state } from './state.js';
|
import { state } from './state.js';
|
||||||
import { imageResultGeometry } from './image-result-geometry.js';
|
|
||||||
import { beginAIOperation, decodeAIImage } from './ai-operation.js';
|
import { beginAIOperation, decodeAIImage } from './ai-operation.js';
|
||||||
|
|
||||||
export function wireInpaintButtons({
|
export function wireInpaintButtons({
|
||||||
@@ -185,30 +184,17 @@ export function wireInpaintButtons({
|
|||||||
if (!state.editorOpen) return; // user closed mid-decode
|
if (!state.editorOpen) return; // user closed mid-decode
|
||||||
try {
|
try {
|
||||||
saveState('Inpaint result');
|
saveState('Inpaint result');
|
||||||
// Whole-image edits keep the provider's native frame; only masked
|
// OpenAI returns at one of its allowed sizes (1024²,
|
||||||
// edits need registration with the source document's coordinates.
|
// 1024×1536, 1536×1024) which often differs from our
|
||||||
|
// canvas. Scale to canvas size with smoothing so the
|
||||||
|
// inpaint blends in regardless of source dims.
|
||||||
const shortPrompt = (prompt || '').trim().replace(/\s+/g, ' ').slice(0, 40);
|
const shortPrompt = (prompt || '').trim().replace(/\s+/g, ' ').slice(0, 40);
|
||||||
const layerName = shortPrompt ? `Inpaint: ${shortPrompt}` : 'Inpaint Result';
|
const layerName = shortPrompt ? `Inpaint: ${shortPrompt}` : 'Inpaint Result';
|
||||||
const fullFrame = data.full_frame === true;
|
const resultLayer = createLayer(layerName, state.imgWidth, state.imgHeight);
|
||||||
const geometry = imageResultGeometry(resultImg.naturalWidth, resultImg.naturalHeight, state.imgWidth, state.imgHeight);
|
|
||||||
const resultLayer = createLayer(layerName,
|
|
||||||
fullFrame ? geometry.width : state.imgWidth,
|
|
||||||
fullFrame ? geometry.height : state.imgHeight);
|
|
||||||
resultLayer.ctx.imageSmoothingEnabled = true;
|
resultLayer.ctx.imageSmoothingEnabled = true;
|
||||||
resultLayer.ctx.imageSmoothingQuality = 'high';
|
resultLayer.ctx.imageSmoothingQuality = 'high';
|
||||||
if (fullFrame) {
|
resultLayer.ctx.drawImage(resultImg, 0, 0, state.imgWidth, state.imgHeight);
|
||||||
resultLayer.ctx.drawImage(resultImg, geometry.drawX, geometry.drawY, geometry.drawWidth, geometry.drawHeight);
|
|
||||||
state.layerOffsets.set(resultLayer.id, {
|
|
||||||
x: geometry.offsetX,
|
|
||||||
y: geometry.offsetY,
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
const scale = Math.min(state.imgWidth / resultImg.naturalWidth, state.imgHeight / resultImg.naturalHeight);
|
|
||||||
const width = resultImg.naturalWidth * scale, height = resultImg.naturalHeight * scale;
|
|
||||||
resultLayer.ctx.drawImage(resultImg, (state.imgWidth - width) / 2, (state.imgHeight - height) / 2, width, height);
|
|
||||||
}
|
|
||||||
// Snapshot the AI result + hard mask used for this run.
|
// Snapshot the AI result + hard mask used for this run.
|
||||||
if (!fullFrame) {
|
|
||||||
const aiSnap = document.createElement('canvas');
|
const aiSnap = document.createElement('canvas');
|
||||||
aiSnap.width = state.imgWidth; aiSnap.height = state.imgHeight;
|
aiSnap.width = state.imgWidth; aiSnap.height = state.imgHeight;
|
||||||
aiSnap.getContext('2d').drawImage(resultLayer.canvas, 0, 0);
|
aiSnap.getContext('2d').drawImage(resultLayer.canvas, 0, 0);
|
||||||
@@ -219,7 +205,6 @@ export function wireInpaintButtons({
|
|||||||
resultLayer.inpaintSource = { ai: aiSnap, mask: maskSnap, base: baseSnap, padPx };
|
resultLayer.inpaintSource = { ai: aiSnap, mask: maskSnap, base: baseSnap, padPx };
|
||||||
// Apply initial alpha = hard mask (no feather, no edge shift).
|
// Apply initial alpha = hard mask (no feather, no edge shift).
|
||||||
applyInpaintFeather(resultLayer, 0, 0);
|
applyInpaintFeather(resultLayer, 0, 0);
|
||||||
}
|
|
||||||
state.layers.push(resultLayer);
|
state.layers.push(resultLayer);
|
||||||
state.activeLayerId = resultLayer.id;
|
state.activeLayerId = resultLayer.id;
|
||||||
state.lastInpaintLayerId = resultLayer.id;
|
state.lastInpaintLayerId = resultLayer.id;
|
||||||
@@ -235,10 +220,6 @@ export function wireInpaintButtons({
|
|||||||
}
|
}
|
||||||
composite();
|
composite();
|
||||||
renderLayerPanel();
|
renderLayerPanel();
|
||||||
if (fullFrame) {
|
|
||||||
if (uiModule) uiModule.showToast('Image edit complete', 4500);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
// Reveal post-generation Feather + Edge Stroke sliders.
|
// Reveal post-generation Feather + Edge Stroke sliders.
|
||||||
// Cap Edge Stroke at ±padPx so the slider can't ask for
|
// Cap Edge Stroke at ±padPx so the slider can't ask for
|
||||||
// more AI buffer than we generated.
|
// more AI buffer than we generated.
|
||||||
|
|||||||
@@ -54,16 +54,16 @@ export function resolveQuickEditModel() {
|
|||||||
label: ((opt && opt.textContent) || '').trim(),
|
label: ((opt && opt.textContent) || '').trim(),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
const generation = document.getElementById('ge-ai-model');
|
const inp = document.getElementById('ge-ai-inpaint');
|
||||||
if (generation?.value && generation.value !== '__serve_cookbook__') {
|
const opt = resolveInpaintModel(inp);
|
||||||
const index = generation.value.indexOf('::');
|
if (!opt) return { endpoint: '', model: '', label: '' };
|
||||||
const model = index < 0 ? '' : generation.value.slice(index + 2);
|
const raw = opt.value || '';
|
||||||
if (model && modelCaps(model, '', '').inpaint) {
|
const idx = raw.indexOf('::');
|
||||||
return { endpoint: generation.value.slice(0, index), model,
|
return {
|
||||||
label: generation.options[generation.selectedIndex]?.textContent?.trim() || model };
|
endpoint: idx < 0 ? raw : raw.slice(0, idx),
|
||||||
}
|
model: idx < 0 ? '' : raw.slice(idx + 2),
|
||||||
}
|
label: ((opt.textContent || '').trim().replace(/^Auto\s*\((.*)\)$/, '$1')),
|
||||||
return { endpoint: '', model: '', label: 'Auto (saved settings)' };
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
// Resolve the Quick Edit AGENT (planner) model — the vision-language model
|
// Resolve the Quick Edit AGENT (planner) model — the vision-language model
|
||||||
@@ -144,19 +144,21 @@ export function modelCaps(modelId, endpointName, endpointType) {
|
|||||||
function syncCommandModel(value) {
|
function syncCommandModel(value) {
|
||||||
const select = document.getElementById('ge-ai-command-model');
|
const select = document.getElementById('ge-ai-command-model');
|
||||||
if (!select || !select._modelsReady) return;
|
if (!select || !select._modelsReady) return;
|
||||||
|
if (value && ![...select.options].some(o => !o.disabled && o.value === value && value !== '__serve_cookbook__')) {
|
||||||
|
if (localStorage.getItem('ge-ai-command-model') === value) localStorage.removeItem('ge-ai-command-model');
|
||||||
|
value = '';
|
||||||
|
}
|
||||||
if (select.value !== value) {
|
if (select.value !== value) {
|
||||||
select.value = value;
|
select.value = value;
|
||||||
if (select.value !== value) select.value = '';
|
if (select.value !== value) select.value = '';
|
||||||
updateFooterEditModel();
|
updateFooterEditModel();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (typeof window !== 'undefined') {
|
window.addEventListener('ge-ai-command-model-change', e => syncCommandModel(e.detail?.value || ''));
|
||||||
window.addEventListener('ge-ai-command-model-change', e => syncCommandModel(e.detail?.value || ''));
|
window.addEventListener('storage', e => {
|
||||||
window.addEventListener('storage', e => {
|
if (e.key === 'ge-ai-command-model') syncCommandModel(e.newValue || '');
|
||||||
if (e.key === 'ge-ai-command-model') syncCommandModel(e.newValue || '');
|
});
|
||||||
});
|
window.addEventListener('ge:model-endpoints-updated', e => {
|
||||||
}
|
|
||||||
globalThis.window?.addEventListener('ge:model-endpoints-updated', e => {
|
|
||||||
document.getElementById('ge-ai-command-model')?._refreshAIModels?.(e.detail?.baseUrl || '');
|
document.getElementById('ge-ai-command-model')?._refreshAIModels?.(e.detail?.baseUrl || '');
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -254,9 +256,9 @@ export function wireAIModelSelectors({ container, apiBase = '', openCookbookForI
|
|||||||
const data = await res.json();
|
const data = await res.json();
|
||||||
if (listeners.signal.aborted || !root.isConnected) return;
|
if (listeners.signal.aborted || !root.isConnected) return;
|
||||||
const modelsList = data.models || [];
|
const modelsList = data.models || [];
|
||||||
if (aiGenSelect) aiGenSelect.innerHTML = '<option value="">Auto-detect</option>';
|
if (aiGenSelect) aiGenSelect.innerHTML = '<option value="">None</option>';
|
||||||
if (aiInpaintSelect) aiInpaintSelect.innerHTML = '<option value="">Auto</option>';
|
if (aiInpaintSelect) aiInpaintSelect.innerHTML = '<option value="">Auto</option>';
|
||||||
if (aiCommandSelect) aiCommandSelect.innerHTML = '<option value="">Same as generation</option>';
|
if (aiCommandSelect) aiCommandSelect.innerHTML = '<option value="">Auto (default)</option>';
|
||||||
const perToolSelects = Array.from(document.querySelectorAll('select.ge-tool-model'));
|
const perToolSelects = Array.from(document.querySelectorAll('select.ge-tool-model'));
|
||||||
for (const ts of perToolSelects) ts.innerHTML = '<option value="">Auto</option>';
|
for (const ts of perToolSelects) ts.innerHTML = '<option value="">Auto</option>';
|
||||||
let firstGen = null;
|
let firstGen = null;
|
||||||
@@ -344,15 +346,45 @@ export function wireAIModelSelectors({ container, apiBase = '', openCookbookForI
|
|||||||
// Sentinel changes are handled by the component's delegated listener.
|
// Sentinel changes are handled by the component's delegated listener.
|
||||||
if (aiCommandSelect) {
|
if (aiCommandSelect) {
|
||||||
aiCommandSelect._modelsReady = true;
|
aiCommandSelect._modelsReady = true;
|
||||||
// Legacy URL migration check: legacy endpoints with raw URLs are scrubbed.
|
try {
|
||||||
const scrubModelUrl = (saved) => (saved && saved.startsWith('http://') ? '' : saved);
|
const rawSaved = localStorage.getItem('ge-ai-command-model') || '';
|
||||||
|
let saved = rawSaved;
|
||||||
|
if (saved.startsWith('http://') || saved.startsWith('https://') || saved.includes('://') || saved.startsWith('::')) {
|
||||||
|
const modelPart = saved.includes('::') ? saved.split('::').pop() : '';
|
||||||
|
const matching = loaded.filter(l => l.model === modelPart);
|
||||||
|
if (matching.length === 1) {
|
||||||
|
saved = matching[0].value;
|
||||||
|
} else {
|
||||||
|
saved = '';
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
if (saved) localStorage.setItem('ge-ai-command-model', saved);
|
||||||
|
else localStorage.removeItem('ge-ai-command-model');
|
||||||
|
} catch {}
|
||||||
|
}
|
||||||
|
if (saved && [...aiCommandSelect.options].some(o => o.value === saved)) {
|
||||||
|
aiCommandSelect.value = saved;
|
||||||
|
} else {
|
||||||
|
aiCommandSelect.value = '';
|
||||||
|
}
|
||||||
|
if (aiCommandSelect.value !== rawSaved) {
|
||||||
|
if (aiCommandSelect.value) localStorage.setItem('ge-ai-command-model', aiCommandSelect.value);
|
||||||
|
else localStorage.removeItem('ge-ai-command-model');
|
||||||
|
window.dispatchEvent(new CustomEvent('ge-ai-command-model-change', { detail: { value: aiCommandSelect.value } }));
|
||||||
|
}
|
||||||
|
} catch {}
|
||||||
|
|
||||||
if (aiCommandSelect._commandWired !== listeners) {
|
if (aiCommandSelect._commandWired !== listeners) {
|
||||||
aiCommandSelect._commandWired = listeners;
|
aiCommandSelect._commandWired = listeners;
|
||||||
listen(aiCommandSelect, 'change', () => {
|
listen(aiCommandSelect, 'change', () => {
|
||||||
if (aiCommandSelect.value === '__serve_cookbook__') return;
|
if (aiCommandSelect.value === '__serve_cookbook__') return;
|
||||||
const val = scrubModelUrl(aiCommandSelect.value) || '';
|
const val = aiCommandSelect.value || '';
|
||||||
|
try {
|
||||||
|
if (val) localStorage.setItem('ge-ai-command-model', aiCommandSelect.value);
|
||||||
|
else localStorage.removeItem('ge-ai-command-model');
|
||||||
|
} catch {}
|
||||||
updateFooterEditModel();
|
updateFooterEditModel();
|
||||||
globalThis.window?.dispatchEvent?.(new CustomEvent('ge-ai-command-model-change', { detail: { value: val } }));
|
window.dispatchEvent(new CustomEvent('ge-ai-command-model-change', { detail: { value: val } }));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -377,7 +409,6 @@ export function wireAIModelSelectors({ container, apiBase = '', openCookbookForI
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
_aiModelsStatus = { ready: true, models: loaded, error: null, at: Date.now() };
|
_aiModelsStatus = { ready: true, models: loaded, error: null, at: Date.now() };
|
||||||
container.dispatchEvent(new Event('ge-image-models-ready'));
|
|
||||||
try { window.dispatchEvent(new CustomEvent('ge:ai-models-status')); } catch {}
|
try { window.dispatchEvent(new CustomEvent('ge:ai-models-status')); } catch {}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
if (listeners.signal.aborted || !root.isConnected) return;
|
if (listeners.signal.aborted || !root.isConnected) return;
|
||||||
|
|||||||
@@ -2,10 +2,11 @@
|
|||||||
* Background Remove (rembg) + Sharpen wiring + the live edge-cleanup
|
* Background Remove (rembg) + Sharpen wiring + the live edge-cleanup
|
||||||
* tuner that runs on the most-recent rembg cutout.
|
* tuner that runs on the most-recent rembg cutout.
|
||||||
*
|
*
|
||||||
* rembg-run button: active layer + POST to /api/image/remove-bg with an
|
* rembg-run button: flatten + POST to /api/image/remove-bg with an
|
||||||
* optional hint_mask if the user has a wand/lasso selection
|
* optional hint_mask if the user has a wand/lasso selection
|
||||||
* active. Updates only that layer and binds its live edge tuner;
|
* active. After the new layer lands, hides every previously-
|
||||||
* other layers and their visibility remain unchanged.
|
* visible layer so the cutout reads cleanly, and binds the
|
||||||
|
* live-tuner to the new layer.
|
||||||
*
|
*
|
||||||
* Live edge-cleanup tuner: snapshots the pristine cutout the
|
* Live edge-cleanup tuner: snapshots the pristine cutout the
|
||||||
* moment it lands; subsequent feather/grow slider tweaks
|
* moment it lands; subsequent feather/grow slider tweaks
|
||||||
@@ -64,13 +65,37 @@ export function wireRembgAndSharpen({
|
|||||||
// NB: edge_feather / edge_grow are applied CLIENT-side so the
|
// NB: edge_feather / edge_grow are applied CLIENT-side so the
|
||||||
// sliders can re-tune the cutout without re-running the model.
|
// sliders can re-tune the cutout without re-running the model.
|
||||||
const btn = document.getElementById('ge-rembg-run');
|
const btn = document.getElementById('ge-rembg-run');
|
||||||
const editedLayer = await applyImageTool('/api/image/remove-bg', payload, 'BG Removed', btn);
|
const before = state.layers.length;
|
||||||
|
// Snapshot which layers were visible BEFORE the run so we know
|
||||||
|
// which to hide after a successful cutout.
|
||||||
|
const prevVisible = state.layers.filter(l => l.visible).map(l => l.id);
|
||||||
|
await applyImageTool('/api/image/remove-bg', payload, 'BG Removed', btn);
|
||||||
|
// applyImageTool resolves after the returned image is decoded and the
|
||||||
|
// new layer is inserted. Keep a short poll for defensive compatibility
|
||||||
|
// with alternate runners that may still commit on the next frame.
|
||||||
|
let frames = 0;
|
||||||
|
while (state.layers.length <= before && frames < 60) {
|
||||||
|
await new Promise(r => requestAnimationFrame(r));
|
||||||
|
frames++;
|
||||||
|
}
|
||||||
|
if (state.layers.length > before) {
|
||||||
|
const newLayer = state.layers[state.layers.length - 1];
|
||||||
|
bindRembgLiveTuner(newLayer);
|
||||||
|
// Auto-hide underlying layers so the user sees just the
|
||||||
|
// cutout — the eye toggles back on if they re-enable manually.
|
||||||
|
for (const layer of state.layers) {
|
||||||
|
if (prevVisible.includes(layer.id) && layer.id !== newLayer.id) {
|
||||||
|
layer.visible = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
composite();
|
||||||
|
renderLayerPanel();
|
||||||
|
}
|
||||||
// Reset sliders so the new cutout starts clean.
|
// Reset sliders so the new cutout starts clean.
|
||||||
const f = document.getElementById('ge-rembg-feather');
|
const f = document.getElementById('ge-rembg-feather');
|
||||||
const g = document.getElementById('ge-rembg-grow');
|
const g = document.getElementById('ge-rembg-grow');
|
||||||
if (f) { f.value = 0; document.getElementById('ge-rembg-feather-label').textContent = '0px'; syncRembgFeather(0); }
|
if (f) { f.value = 0; document.getElementById('ge-rembg-feather-label').textContent = '0px'; syncRembgFeather(0); }
|
||||||
if (g) { g.value = 0; document.getElementById('ge-rembg-grow-label').textContent = '0px'; syncRembgGrow(0); }
|
if (g) { g.value = 0; document.getElementById('ge-rembg-grow-label').textContent = '0px'; syncRembgGrow(0); }
|
||||||
if (editedLayer) bindRembgLiveTuner(editedLayer);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// ── Live edge-cleanup tuner ──
|
// ── Live edge-cleanup tuner ──
|
||||||
|
|||||||
@@ -5,9 +5,6 @@
|
|||||||
* "Double-check AI edits" toggle. Opened from the Quick Edit gear button and
|
* "Double-check AI edits" toggle. Opened from the Quick Edit gear button and
|
||||||
* from the editor setup checklist; keeps the Quick Edit box itself uncluttered.
|
* from the editor setup checklist; keeps the Quick Edit box itself uncluttered.
|
||||||
*/
|
*/
|
||||||
import { readImagePreferences, saveImagePreferences } from '../settings/imagePreferences.js';
|
|
||||||
import { updateFooterEditModel } from './ai-models.js';
|
|
||||||
|
|
||||||
export function createAiSettings({ container }) {
|
export function createAiSettings({ container }) {
|
||||||
const el = document.createElement('div');
|
const el = document.createElement('div');
|
||||||
el.className = 'ge-ai-settings';
|
el.className = 'ge-ai-settings';
|
||||||
@@ -22,14 +19,11 @@ export function createAiSettings({ container }) {
|
|||||||
<button type="button" class="ge-ai-settings-close" data-ai-settings-close aria-label="Close">×</button>
|
<button type="button" class="ge-ai-settings-close" data-ai-settings-close aria-label="Close">×</button>
|
||||||
</div>
|
</div>
|
||||||
<div class="ge-ai-settings-body">
|
<div class="ge-ai-settings-body">
|
||||||
<label class="ge-ai-settings-label" for="ge-ai-model">Image generation model</label>
|
|
||||||
<select class="ge-ai-command-model" id="ge-ai-model" title="Image generation model" aria-label="Image generation model"></select>
|
|
||||||
<label class="ge-ai-settings-label" for="ge-ai-command-model">Image edit model</label>
|
<label class="ge-ai-settings-label" for="ge-ai-command-model">Image edit model</label>
|
||||||
<select class="ge-ai-command-model" id="ge-ai-command-model" title="Image edit model" aria-label="Image edit model"></select>
|
<select class="ge-ai-command-model" id="ge-ai-command-model" title="Image edit model" aria-label="Image edit model"></select>
|
||||||
<label class="ge-ai-settings-label" for="ge-ai-command-agent-model">AI agent (vision) model</label>
|
<label class="ge-ai-settings-label" for="ge-ai-command-agent-model">AI agent (vision) model</label>
|
||||||
<select class="ge-ai-command-model" id="ge-ai-command-agent-model" title="Quick Edit agent model" aria-label="Quick Edit agent model"></select>
|
<select class="ge-ai-command-model" id="ge-ai-command-agent-model" title="Quick Edit agent model" aria-label="Quick Edit agent model"></select>
|
||||||
<p class="ge-ai-settings-note">Recommended, not required — the vision agent plans edits and can double-check results.</p>
|
<p class="ge-ai-settings-note">Recommended, not required — the vision agent plans edits and can double-check results.</p>
|
||||||
<p class="ge-ai-settings-note" id="ge-ai-settings-save-status" role="status"></p>
|
|
||||||
<label class="ge-ai-settings-label" for="ge-look-strength">Look strength</label>
|
<label class="ge-ai-settings-label" for="ge-look-strength">Look strength</label>
|
||||||
<div class="ge-ai-settings-strength">
|
<div class="ge-ai-settings-strength">
|
||||||
<input type="range" id="ge-look-strength" min="0.5" max="2" step="0.1" value="1.4" aria-label="Look grade strength" />
|
<input type="range" id="ge-look-strength" min="0.5" max="2" step="0.1" value="1.4" aria-label="Look grade strength" />
|
||||||
@@ -47,7 +41,6 @@ export function createAiSettings({ container }) {
|
|||||||
|
|
||||||
function open(focusId) {
|
function open(focusId) {
|
||||||
el.hidden = false;
|
el.hidden = false;
|
||||||
syncModels();
|
|
||||||
if (focusId) {
|
if (focusId) {
|
||||||
const target = document.getElementById(focusId);
|
const target = document.getElementById(focusId);
|
||||||
if (target) {
|
if (target) {
|
||||||
@@ -63,59 +56,10 @@ export function createAiSettings({ container }) {
|
|||||||
close();
|
close();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
const onKey = (e) => {
|
const onKey = (e) => { if (e.key === 'Escape' && !el.hidden) close(); };
|
||||||
if (e.key !== 'Escape' || el.hidden) return;
|
document.addEventListener('keydown', onKey);
|
||||||
e.preventDefault();
|
|
||||||
e.stopImmediatePropagation();
|
|
||||||
close();
|
|
||||||
};
|
|
||||||
window.addEventListener('keydown', onKey, true);
|
|
||||||
|
|
||||||
container.appendChild(el);
|
container.appendChild(el);
|
||||||
const status = el.querySelector('#ge-ai-settings-save-status');
|
|
||||||
const modelFields = [['ge-ai-model', 'image_model'], ['ge-ai-command-model', 'image_edit_model']];
|
|
||||||
function applyModels(settings) {
|
|
||||||
for (const [id, key] of modelFields) {
|
|
||||||
if (!(key in settings)) continue;
|
|
||||||
const select = el.querySelector(`#${id}`);
|
|
||||||
const model = settings[key] || '';
|
|
||||||
let option = [...select.options].find(option => {
|
|
||||||
const value = option.value;
|
|
||||||
return model ? value.slice(value.indexOf('::') + 2) === model : value === '';
|
|
||||||
});
|
|
||||||
if (!option && model && select.options.length) {
|
|
||||||
option = document.createElement('option');
|
|
||||||
option.value = `::${model}`;
|
|
||||||
option.textContent = `${model} (unavailable)`;
|
|
||||||
option.disabled = true;
|
|
||||||
select.append(option);
|
|
||||||
}
|
|
||||||
if (option) select.value = option.value;
|
|
||||||
}
|
|
||||||
updateFooterEditModel();
|
|
||||||
}
|
|
||||||
async function syncModels() {
|
|
||||||
try { applyModels(await readImagePreferences()); }
|
|
||||||
catch (error) { status.textContent = error.message; }
|
|
||||||
}
|
|
||||||
for (const [id, key] of modelFields) {
|
|
||||||
el.querySelector(`#${id}`).addEventListener('change', async (event) => {
|
|
||||||
const raw = event.target.value;
|
|
||||||
if (raw === '__serve_cookbook__') return;
|
|
||||||
const model = raw.includes('::') ? raw.slice(raw.indexOf('::') + 2) : '';
|
|
||||||
try {
|
|
||||||
status.textContent = 'Saving…';
|
|
||||||
await saveImagePreferences({ [key]: model });
|
|
||||||
status.textContent = 'Saved';
|
|
||||||
} catch (error) {
|
|
||||||
await syncModels();
|
|
||||||
status.textContent = error.message;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
const onPreferencesChanged = event => applyModels(event.detail);
|
|
||||||
window.addEventListener('image-preferences-changed', onPreferencesChanged);
|
|
||||||
container.addEventListener('ge-image-models-ready', syncModels);
|
|
||||||
|
|
||||||
const strengthInput = el.querySelector('#ge-look-strength');
|
const strengthInput = el.querySelector('#ge-look-strength');
|
||||||
const strengthVal = el.querySelector('#ge-look-strength-val');
|
const strengthVal = el.querySelector('#ge-look-strength-val');
|
||||||
@@ -133,9 +77,7 @@ export function createAiSettings({ container }) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function destroy() {
|
function destroy() {
|
||||||
window.removeEventListener('keydown', onKey, true);
|
document.removeEventListener('keydown', onKey);
|
||||||
window.removeEventListener('image-preferences-changed', onPreferencesChanged);
|
|
||||||
container.removeEventListener('ge-image-models-ready', syncModels);
|
|
||||||
el.remove();
|
el.remove();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -76,12 +76,7 @@ export function createApplyImageTool({
|
|||||||
if (sel.endpoint) extraPayload._endpoint = sel.endpoint;
|
if (sel.endpoint) extraPayload._endpoint = sel.endpoint;
|
||||||
if (sel.model && !extraPayload._model) extraPayload._model = sel.model;
|
if (sel.model && !extraPayload._model) extraPayload._model = sel.model;
|
||||||
}
|
}
|
||||||
const removeBackground = endpoint === '/api/image/remove-bg';
|
const flatCanvas = flatten();
|
||||||
const targetLayer = removeBackground ? state.layers.find(layer => layer.id === state.activeLayerId) : null;
|
|
||||||
if (removeBackground && (!targetLayer?.canvas || targetLayer.kind === 'adjustment' || targetLayer.locks?.pixels)) {
|
|
||||||
throw new Error('Select an unlocked image layer first');
|
|
||||||
}
|
|
||||||
const flatCanvas = removeBackground ? targetLayer.canvas : flatten();
|
|
||||||
const imageB64 = flatCanvas.toDataURL('image/png').split(',')[1];
|
const imageB64 = flatCanvas.toDataURL('image/png').split(',')[1];
|
||||||
const body = { image: imageB64, ...extraPayload };
|
const body = { image: imageB64, ...extraPayload };
|
||||||
const res = await fetch(endpoint, {
|
const res = await fetch(endpoint, {
|
||||||
@@ -101,23 +96,6 @@ export function createApplyImageTool({
|
|||||||
const img = await decodeAIImage(data.image, operation.signal);
|
const img = await decodeAIImage(data.image, operation.signal);
|
||||||
operation.signal.throwIfAborted();
|
operation.signal.throwIfAborted();
|
||||||
if (!state.editorOpen) return; // user closed mid-decode (v2 review HIGH-4)
|
if (!state.editorOpen) return; // user closed mid-decode (v2 review HIGH-4)
|
||||||
if (removeBackground) {
|
|
||||||
if (!state.layers.includes(targetLayer)) return;
|
|
||||||
saveState('Remove background');
|
|
||||||
// The returned pixels replace the editable source, not just its preview.
|
|
||||||
targetLayer.kind = 'raster';
|
|
||||||
targetLayer.placed = null;
|
|
||||||
targetLayer.text = null;
|
|
||||||
targetLayer.shape = null;
|
|
||||||
targetLayer.ctx.clearRect(0, 0, targetLayer.canvas.width, targetLayer.canvas.height);
|
|
||||||
targetLayer.ctx.drawImage(img, 0, 0, targetLayer.canvas.width, targetLayer.canvas.height);
|
|
||||||
targetLayer._adjCacheKey = null;
|
|
||||||
targetLayer._adjFinalKey = null;
|
|
||||||
composite();
|
|
||||||
renderLayerPanel();
|
|
||||||
uiModule?.showToast('Background removed');
|
|
||||||
return targetLayer;
|
|
||||||
}
|
|
||||||
{
|
{
|
||||||
saveState();
|
saveState();
|
||||||
const layer = createLayer(layerName, state.imgWidth, state.imgHeight);
|
const layer = createLayer(layerName, state.imgWidth, state.imgHeight);
|
||||||
|
|||||||
Vendored
+5
-5
@@ -55,11 +55,6 @@ export function controlsHTML({ color, brushSize, wandTolerance, bucketTolerance
|
|||||||
<label>Color</label>
|
<label>Color</label>
|
||||||
<input type="color" class="ge-color-picker" value="${color}" />
|
<input type="color" class="ge-color-picker" value="${color}" />
|
||||||
</div>
|
</div>
|
||||||
<div class="ge-control-row ge-eraser-row" id="ge-brush-softness-row">
|
|
||||||
<span class="ge-eraser-preview" id="ge-brush-preview-softness" aria-hidden="true"></span>
|
|
||||||
<label>Softness <span id="ge-brush-softness-label">100%</span></label>
|
|
||||||
<input type="range" id="ge-brush-softness" min="0" max="1200" value="100" title="Brush edge softness and feathered falloff" />
|
|
||||||
</div>
|
|
||||||
<div class="ge-control-row ge-brush-size-row">
|
<div class="ge-control-row ge-brush-size-row">
|
||||||
<label><span class="ge-size-label">${brushSize}px</span></label>
|
<label><span class="ge-size-label">${brushSize}px</span></label>
|
||||||
<input type="range" class="ge-size-slider" min="0" max="1000" value="${brushSliderValue}" />
|
<input type="range" class="ge-size-slider" min="0" max="1000" value="${brushSliderValue}" />
|
||||||
@@ -99,6 +94,11 @@ export function controlsHTML({ color, brushSize, wandTolerance, bucketTolerance
|
|||||||
<label>Flow <span id="ge-brush-flow-label">100%</span></label>
|
<label>Flow <span id="ge-brush-flow-label">100%</span></label>
|
||||||
<input type="range" id="ge-brush-flow" min="5" max="100" value="100" />
|
<input type="range" id="ge-brush-flow" min="5" max="100" value="100" />
|
||||||
</div>
|
</div>
|
||||||
|
<div class="ge-control-row ge-eraser-row" id="ge-brush-softness-row">
|
||||||
|
<span class="ge-eraser-preview" id="ge-brush-preview-softness" aria-hidden="true"></span>
|
||||||
|
<label>Softness <span id="ge-brush-softness-label">100%</span></label>
|
||||||
|
<input type="range" id="ge-brush-softness" min="0" max="300" value="100" title="Soft brush edge — blurs the stroke's alpha for a feathered fade at the perimeter." />
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div class="ge-brush-stroke-group" data-stroke-tool="eraser">
|
<div class="ge-brush-stroke-group" data-stroke-tool="eraser">
|
||||||
<div class="ge-section-title">Eraser</div>
|
<div class="ge-section-title">Eraser</div>
|
||||||
|
|||||||
@@ -78,7 +78,6 @@ export function wireCanvasEvents({ canvasArea, beginDraw, continueDraw, endDraw,
|
|||||||
const UI_CHROME_SELECTOR = '.ge-topbar, .ge-toolbar, .ge-controls, .ge-right-panel, '
|
const UI_CHROME_SELECTOR = '.ge-topbar, .ge-toolbar, .ge-controls, .ge-right-panel, '
|
||||||
+ '.ge-dock, .ge-options-bar, .ge-canvas-toolbar, button, input, select, textarea';
|
+ '.ge-dock, .ge-options-bar, .ge-canvas-toolbar, button, input, select, textarea';
|
||||||
const isUiChrome = e => !!e.target?.closest?.(UI_CHROME_SELECTOR);
|
const isUiChrome = e => !!e.target?.closest?.(UI_CHROME_SELECTOR);
|
||||||
const canStartOutside = () => ['marquee', 'lasso', 'crop'].includes(state.tool);
|
|
||||||
// Only in-progress selection/crop drags are affected by the chrome
|
// Only in-progress selection/crop drags are affected by the chrome
|
||||||
// guard. A committed cropRect/selection is a resting state — clicking
|
// guard. A committed cropRect/selection is a resting state — clicking
|
||||||
// its Apply button must not be treated as "strayed onto chrome".
|
// its Apply button must not be treated as "strayed onto chrome".
|
||||||
@@ -144,16 +143,15 @@ export function wireCanvasEvents({ canvasArea, beginDraw, continueDraw, endDraw,
|
|||||||
};
|
};
|
||||||
state.mainCanvas.addEventListener('pointerup', endPen);
|
state.mainCanvas.addEventListener('pointerup', endPen);
|
||||||
state.mainCanvas.addEventListener('pointercancel', endPen);
|
state.mainCanvas.addEventListener('pointercancel', endPen);
|
||||||
// Selection and Crop can start OUTSIDE the canvas — fallback mousedown on the
|
// Lasso and Crop can start OUTSIDE the canvas — fallback mousedown on the
|
||||||
// surrounding canvas-area so the user can begin a path/rect in the empty
|
// surrounding canvas-area so the user can begin a path/rect in the empty
|
||||||
// space around the image. Other tools stay canvas-only.
|
// space around the image. Other tools stay canvas-only.
|
||||||
canvasArea.addEventListener('mousedown', (e) => {
|
canvasArea.addEventListener('mousedown', (e) => {
|
||||||
if (!canStartOutside() || Date.now() < suppressMouseUntil) return;
|
if (state.tool !== 'lasso' && state.tool !== 'crop') return;
|
||||||
if (e.button !== 0 || isDirectPanIntent(state.tool, state.spacePanActive, e.button)) return;
|
|
||||||
if (e.target === state.mainCanvas) return; // already handled
|
if (e.target === state.mainCanvas) return; // already handled
|
||||||
// Ignore presses on editor chrome inside the area (the crop Apply panel,
|
// Ignore presses on editor chrome inside the area (the crop Apply panel,
|
||||||
// popups, buttons) so clicking Apply does not start a fresh crop.
|
// popups, buttons) so clicking Apply does not start a fresh crop.
|
||||||
if (isUiChrome(e) || e.target.closest?.('.ge-crop-apply, .ge-adj-popup, .ge-transform-popup, .ge-fx-popup, .ge-inpaint-popup, .ge-fx-menu')) return;
|
if (e.target.closest?.('button, input, select, textarea, .ge-crop-apply, .ge-adj-popup, .ge-transform-popup, .ge-fx-popup, .ge-inpaint-popup, .ge-controls, .ge-right-panel, .ge-fx-menu')) return;
|
||||||
beginDraw(e);
|
beginDraw(e);
|
||||||
});
|
});
|
||||||
state.mainCanvas.addEventListener('mouseenter', (e) => {
|
state.mainCanvas.addEventListener('mouseenter', (e) => {
|
||||||
@@ -307,7 +305,6 @@ export function wireCanvasEvents({ canvasArea, beginDraw, continueDraw, endDraw,
|
|||||||
let panning = false;
|
let panning = false;
|
||||||
let pid = null;
|
let pid = null;
|
||||||
let transformPointerId = null;
|
let transformPointerId = null;
|
||||||
let outsideDrawPointerId = null;
|
|
||||||
let startX = 0, startY = 0;
|
let startX = 0, startY = 0;
|
||||||
let startPanX = 0, startPanY = 0;
|
let startPanX = 0, startPanY = 0;
|
||||||
const getOffset = () => ({ x: state.panX || 0, y: state.panY || 0 });
|
const getOffset = () => ({ x: state.panX || 0, y: state.panY || 0 });
|
||||||
@@ -318,23 +315,10 @@ export function wireCanvasEvents({ canvasArea, beginDraw, continueDraw, endDraw,
|
|||||||
};
|
};
|
||||||
canvasArea.addEventListener('pointerdown', (e) => {
|
canvasArea.addEventListener('pointerdown', (e) => {
|
||||||
const directPan = isDirectPanIntent(state.tool, state.spacePanActive, e.button);
|
const directPan = isDirectPanIntent(state.tool, state.spacePanActive, e.button);
|
||||||
// With a selection or crop tool, an empty-area press draws a rect/path
|
// With the crop (or lasso) tool, an empty-area press draws a rect/path
|
||||||
// rather than panning — so the crop can be pulled out past the image.
|
// rather than panning — so the crop can be pulled out past the image.
|
||||||
// Space-held / middle-mouse (directPan) still pans.
|
// Space-held / middle-mouse (directPan) still pans.
|
||||||
if (canStartOutside() && !directPan) {
|
if ((state.tool === 'lasso' || state.tool === 'crop') && !directPan) return;
|
||||||
if (e.target === state.mainCanvas || isUiChrome(e) ||
|
|
||||||
e.target.closest?.('.ge-crop-apply, .ge-adj-popup, .ge-transform-popup, .ge-fx-popup, .ge-inpaint-popup, .ge-fx-menu')) return;
|
|
||||||
// Mouse starts through mousedown; pen/touch need their own captured path.
|
|
||||||
if (e.pointerType === 'pen' || e.pointerType === 'touch') {
|
|
||||||
if (outsideDrawPointerId !== null || e.button !== 0) return;
|
|
||||||
outsideDrawPointerId = e.pointerId;
|
|
||||||
suppressMouseUntil = Date.now() + 500;
|
|
||||||
try { canvasArea.setPointerCapture(e.pointerId); } catch {}
|
|
||||||
beginDraw(e);
|
|
||||||
e.preventDefault();
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (e.target.closest('button, input, .ge-adj-popup, .ge-transform-popup, .ge-fx-popup, .ge-inpaint-popup, .ge-controls, .ge-right-panel, .ge-fx-menu')) return;
|
if (e.target.closest('button, input, .ge-adj-popup, .ge-transform-popup, .ge-fx-popup, .ge-inpaint-popup, .ge-controls, .ge-right-panel, .ge-fx-menu')) return;
|
||||||
const onImage = e.target === state.mainCanvas || e.target === state.transformOverlay;
|
const onImage = e.target === state.mainCanvas || e.target === state.transformOverlay;
|
||||||
if (onImage && !directPan) return;
|
if (onImage && !directPan) return;
|
||||||
@@ -373,12 +357,6 @@ export function wireCanvasEvents({ canvasArea, beginDraw, continueDraw, endDraw,
|
|||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
});
|
});
|
||||||
canvasArea.addEventListener('pointermove', (e) => {
|
canvasArea.addEventListener('pointermove', (e) => {
|
||||||
if (outsideDrawPointerId !== null && e.pointerId === outsideDrawPointerId) {
|
|
||||||
suppressMouseUntil = Date.now() + 500;
|
|
||||||
if (!isUiChrome(e)) continueDraw(e);
|
|
||||||
e.preventDefault();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (transformPointerId !== null && e.pointerId === transformPointerId) {
|
if (transformPointerId !== null && e.pointerId === transformPointerId) {
|
||||||
continueDraw(e);
|
continueDraw(e);
|
||||||
return;
|
return;
|
||||||
@@ -392,14 +370,6 @@ export function wireCanvasEvents({ canvasArea, beginDraw, continueDraw, endDraw,
|
|||||||
applyOffset(next.x, next.y);
|
applyOffset(next.x, next.y);
|
||||||
});
|
});
|
||||||
const endPan = (e) => {
|
const endPan = (e) => {
|
||||||
if (outsideDrawPointerId !== null && (!e || e.pointerId === outsideDrawPointerId)) {
|
|
||||||
const capturedId = outsideDrawPointerId;
|
|
||||||
outsideDrawPointerId = null;
|
|
||||||
suppressMouseUntil = Date.now() + 500;
|
|
||||||
if (e?.type === 'pointercancel') cancelDraw?.();
|
|
||||||
else if (e) endDraw(e);
|
|
||||||
try { canvasArea.releasePointerCapture(capturedId); } catch {}
|
|
||||||
}
|
|
||||||
if (transformPointerId !== null && (!e || e.pointerId === transformPointerId)) {
|
if (transformPointerId !== null && (!e || e.pointerId === transformPointerId)) {
|
||||||
const capturedId = transformPointerId;
|
const capturedId = transformPointerId;
|
||||||
transformPointerId = null;
|
transformPointerId = null;
|
||||||
|
|||||||
@@ -21,7 +21,6 @@ export const EDITOR_AGENT_VERSION = '1.0';
|
|||||||
export const DEFAULT_AGENT_ENDPOINT = '/api/editor/agent';
|
export const DEFAULT_AGENT_ENDPOINT = '/api/editor/agent';
|
||||||
|
|
||||||
import { formatCropRect, isGenericIntent } from './nl-edit.js';
|
import { formatCropRect, isGenericIntent } from './nl-edit.js';
|
||||||
import { waitForImagePreferences } from '../settings/imagePreferences.js';
|
|
||||||
|
|
||||||
export function createEditorAgent({
|
export function createEditorAgent({
|
||||||
control,
|
control,
|
||||||
@@ -29,7 +28,6 @@ export function createEditorAgent({
|
|||||||
fetchImpl = null,
|
fetchImpl = null,
|
||||||
getSession = null,
|
getSession = null,
|
||||||
getModel = null,
|
getModel = null,
|
||||||
getGenerationModel = null,
|
|
||||||
getAgentModel = null,
|
getAgentModel = null,
|
||||||
onStep = null,
|
onStep = null,
|
||||||
} = {}) {
|
} = {}) {
|
||||||
@@ -147,7 +145,6 @@ export function createEditorAgent({
|
|||||||
activeStep = report;
|
activeStep = report;
|
||||||
executed = [];
|
executed = [];
|
||||||
running = true;
|
running = true;
|
||||||
await waitForImagePreferences();
|
|
||||||
|
|
||||||
// Honest before-image (captured before any client pre-step) for the
|
// Honest before-image (captured before any client pre-step) for the
|
||||||
// imperceptible-edit check, then the history mark scoping the undo group.
|
// imperceptible-edit check, then the history mark scoping the undo group.
|
||||||
@@ -197,8 +194,6 @@ export function createEditorAgent({
|
|||||||
signal: signal || undefined,
|
signal: signal || undefined,
|
||||||
body: JSON.stringify({
|
body: JSON.stringify({
|
||||||
prompt, session, editor_session: session, state, screenshot,
|
prompt, session, editor_session: session, state, screenshot,
|
||||||
_use_image_settings: true,
|
|
||||||
generation_model: getGenerationModel ? getGenerationModel() : '',
|
|
||||||
model: (modelInfo && modelInfo.model) || '',
|
model: (modelInfo && modelInfo.model) || '',
|
||||||
endpoint: (modelInfo && modelInfo.endpoint) || '',
|
endpoint: (modelInfo && modelInfo.endpoint) || '',
|
||||||
model_label: (modelInfo && modelInfo.label) || '',
|
model_label: (modelInfo && modelInfo.label) || '',
|
||||||
|
|||||||
@@ -132,7 +132,7 @@ function renderEffects(source, effects, masks) {
|
|||||||
const contribution = maskedContribution(next, masks[index]);
|
const contribution = maskedContribution(next, masks[index]);
|
||||||
const blended = copyCanvas(current);
|
const blended = copyCanvas(current);
|
||||||
const blendCtx = blended.getContext('2d');
|
const blendCtx = blended.getContext('2d');
|
||||||
if (effect.type === 'drop-shadow' && !effect.params.inside) blendCtx.globalCompositeOperation = 'destination-over';
|
if (effect.type === 'drop-shadow') blendCtx.globalCompositeOperation = 'destination-over';
|
||||||
blendCtx.globalAlpha = effect.opacity;
|
blendCtx.globalAlpha = effect.opacity;
|
||||||
blendCtx.drawImage(contribution, 0, 0);
|
blendCtx.drawImage(contribution, 0, 0);
|
||||||
blendCtx.globalAlpha = 1;
|
blendCtx.globalAlpha = 1;
|
||||||
|
|||||||
Vendored
+1
-2
@@ -81,7 +81,6 @@ export function normalizeEffect(effect) {
|
|||||||
params.blendMode = typeof params.blendMode === 'string' ? params.blendMode : defaults.blendMode;
|
params.blendMode = typeof params.blendMode === 'string' ? params.blendMode : defaults.blendMode;
|
||||||
}
|
}
|
||||||
if (type === 'drop-shadow') {
|
if (type === 'drop-shadow') {
|
||||||
params.inside = params.inside === true || params.inside === 1;
|
|
||||||
params.color = /^#[0-9a-f]{6}$/i.test(params.color) ? params.color : defaults.color;
|
params.color = /^#[0-9a-f]{6}$/i.test(params.color) ? params.color : defaults.color;
|
||||||
for (const key of ['opacity', 'blur', 'x', 'y']) params[key] = Number(params[key]) || 0;
|
for (const key of ['opacity', 'blur', 'x', 'y']) params[key] = Number(params[key]) || 0;
|
||||||
params.opacity = Math.max(0, Math.min(1, params.opacity));
|
params.opacity = Math.max(0, Math.min(1, params.opacity));
|
||||||
@@ -292,7 +291,7 @@ export function renderEffects(source, effects = [], shouldContinue = () => true)
|
|||||||
: next;
|
: next;
|
||||||
const blended = copyCanvas(current);
|
const blended = copyCanvas(current);
|
||||||
const blendCtx = blended.getContext('2d');
|
const blendCtx = blended.getContext('2d');
|
||||||
if (effect.type === 'drop-shadow' && !effect.params.inside) blendCtx.globalCompositeOperation = 'destination-over';
|
if (effect.type === 'drop-shadow') blendCtx.globalCompositeOperation = 'destination-over';
|
||||||
blendCtx.globalAlpha = effect.opacity;
|
blendCtx.globalAlpha = effect.opacity;
|
||||||
blendCtx.drawImage(contribution, 0, 0);
|
blendCtx.drawImage(contribution, 0, 0);
|
||||||
blendCtx.globalAlpha = 1;
|
blendCtx.globalAlpha = 1;
|
||||||
|
|||||||
@@ -1,71 +0,0 @@
|
|||||||
import { decodeAIImage } from './ai-operation.js';
|
|
||||||
import { waitForImagePreferences } from '../settings/imagePreferences.js';
|
|
||||||
|
|
||||||
export function createImageGenerationAction({
|
|
||||||
getDocument, placeResult, getSignal = () => null, onProgress = () => {},
|
|
||||||
formatError = info => info.detail || 'Image generation failed',
|
|
||||||
fetchImpl = (...args) => fetch(...args), timeoutMs = 330000,
|
|
||||||
}) {
|
|
||||||
return async function generateImage(params = {}) {
|
|
||||||
const document = getDocument();
|
|
||||||
if (!document.key) throw new Error('Open a document before generating an image');
|
|
||||||
const controller = new AbortController();
|
|
||||||
const parentSignal = getSignal();
|
|
||||||
const cancel = () => controller.abort();
|
|
||||||
if (parentSignal?.aborted) cancel();
|
|
||||||
parentSignal?.addEventListener('abort', cancel, { once: true });
|
|
||||||
let timedOut = false;
|
|
||||||
const deadline = setTimeout(() => { timedOut = true; controller.abort(); }, timeoutMs);
|
|
||||||
const started = Date.now();
|
|
||||||
const progress = setInterval(() => onProgress(Math.floor((Date.now() - started) / 1000)), 1000);
|
|
||||||
// A selection can change while the provider works; preserve this request's mask.
|
|
||||||
let mask = null;
|
|
||||||
try {
|
|
||||||
if (params.masked && document.mask) {
|
|
||||||
mask = globalThis.document.createElement('canvas');
|
|
||||||
mask.width = document.mask.width; mask.height = document.mask.height;
|
|
||||||
mask.getContext('2d').drawImage(document.mask, 0, 0);
|
|
||||||
}
|
|
||||||
await waitForImagePreferences();
|
|
||||||
controller.signal.throwIfAborted();
|
|
||||||
let response;
|
|
||||||
try { response = await fetchImpl(window.geGenerateImageEndpoint || '/api/image/generate', {
|
|
||||||
method: 'POST', credentials: 'same-origin', signal: controller.signal,
|
|
||||||
headers: { 'Content-Type': 'application/json' },
|
|
||||||
body: JSON.stringify({ _use_image_settings: true, prompt: params.prompt || '',
|
|
||||||
width: Math.max(1, Math.round(Number(params.width) || document.width)),
|
|
||||||
height: Math.max(1, Math.round(Number(params.height) || document.height)) }),
|
|
||||||
}); } catch (error) {
|
|
||||||
if (controller.signal.aborted) throw error;
|
|
||||||
throw new Error(formatError({ network: true, detail: error.message }));
|
|
||||||
}
|
|
||||||
let data;
|
|
||||||
try { data = await response.json(); }
|
|
||||||
catch { throw new Error(response.ok ? 'Image server returned an invalid response' : `Image server failed (${response.status})`); }
|
|
||||||
if (!response.ok || data.error) throw new Error(formatError({ status: response.status,
|
|
||||||
detail: data.detail || data.error || `Image server failed (${response.status})` }));
|
|
||||||
const image = data.image || data.images?.[0];
|
|
||||||
if (!image) throw new Error('Image generation returned no image');
|
|
||||||
const decoded = await decodeAIImage(image, controller.signal);
|
|
||||||
controller.signal.throwIfAborted();
|
|
||||||
const current = getDocument();
|
|
||||||
if (current.key !== document.key || current.width !== document.width || current.height !== document.height) {
|
|
||||||
throw new Error('The document changed during generation. The result was not placed in another document.');
|
|
||||||
}
|
|
||||||
const canvas = globalThis.document.createElement('canvas');
|
|
||||||
canvas.width = decoded.naturalWidth; canvas.height = decoded.naturalHeight;
|
|
||||||
const context = canvas.getContext('2d');
|
|
||||||
context.imageSmoothingEnabled = true;
|
|
||||||
context.imageSmoothingQuality = 'high';
|
|
||||||
context.drawImage(decoded, 0, 0);
|
|
||||||
return placeResult(canvas, 'Generated', mask);
|
|
||||||
} catch (error) {
|
|
||||||
if (timedOut) throw new Error('Image generation timed out. Try again or choose a faster image model.');
|
|
||||||
throw error;
|
|
||||||
} finally {
|
|
||||||
clearTimeout(deadline);
|
|
||||||
clearInterval(progress);
|
|
||||||
parentSignal?.removeEventListener('abort', cancel);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
@@ -1,11 +0,0 @@
|
|||||||
export function imageResultGeometry(width, height, canvasWidth, canvasHeight) {
|
|
||||||
const matchingRatio = Math.abs(width * canvasHeight - height * canvasWidth) <= Math.max(canvasWidth, canvasHeight);
|
|
||||||
if (matchingRatio) {
|
|
||||||
const scale = Math.max(canvasWidth / width, canvasHeight / height);
|
|
||||||
const drawWidth = width * scale, drawHeight = height * scale;
|
|
||||||
return { width: canvasWidth, height: canvasHeight, offsetX: 0, offsetY: 0,
|
|
||||||
drawX: (canvasWidth - drawWidth) / 2, drawY: (canvasHeight - drawHeight) / 2, drawWidth, drawHeight };
|
|
||||||
}
|
|
||||||
return { width, height, offsetX: Math.round((canvasWidth - width) / 2), offsetY: Math.round((canvasHeight - height) / 2),
|
|
||||||
drawX: 0, drawY: 0, drawWidth: width, drawHeight: height };
|
|
||||||
}
|
|
||||||
@@ -67,7 +67,7 @@ export function wireKeyboardShortcuts(deps) {
|
|||||||
toolbar, toolKeyMap,
|
toolbar, toolKeyMap,
|
||||||
composite, saveState, undo, redo,
|
composite, saveState, undo, redo,
|
||||||
toggleShortcuts, confirmTransform, cancelTransform, startTransform, nudgeTransform,
|
toggleShortcuts, confirmTransform, cancelTransform, startTransform, nudgeTransform,
|
||||||
resizeCustomPrompt, addEmptyLayer, brushSizeSync, openLayerAdjustment,
|
resizeCustomPrompt, addEmptyLayer, brushSizeSync,
|
||||||
invertSelection,
|
invertSelection,
|
||||||
wandDeleteSelection, wandCopyToNewLayer, copyPixelsToClipboard,
|
wandDeleteSelection, wandCopyToNewLayer, copyPixelsToClipboard,
|
||||||
lassoDeleteSelection, lassoCopyToLayer,
|
lassoDeleteSelection, lassoCopyToLayer,
|
||||||
@@ -181,12 +181,6 @@ export function wireKeyboardShortcuts(deps) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// Save shortcuts — match the hints shown in the Save dropdown.
|
// Save shortcuts — match the hints shown in the Save dropdown.
|
||||||
if (e.shiftKey && !e.altKey && e.code === 'KeyU') {
|
|
||||||
e.preventDefault();
|
|
||||||
e.stopPropagation();
|
|
||||||
openLayerAdjustment?.('hue-saturation');
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if ((e.key === 's' || e.key === 'S') && !e.altKey) {
|
if ((e.key === 's' || e.key === 'S') && !e.altKey) {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
document.getElementById(e.shiftKey ? 'ge-export-gallery' : 'ge-save')?.click();
|
document.getElementById(e.shiftKey ? 'ge-export-gallery' : 'ge-save')?.click();
|
||||||
@@ -227,12 +221,11 @@ export function wireKeyboardShortcuts(deps) {
|
|||||||
addEmptyLayer();
|
addEmptyLayer();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// Ctrl+Alt+L edits the current layer, leaving Levels controls open.
|
// Ctrl+Alt+L — Levels adjustment. The Image menu owns the row and its
|
||||||
|
// dialog, so click it instead of duplicating the adjustment here.
|
||||||
if (e.altKey && e.code === 'KeyL') {
|
if (e.altKey && e.code === 'KeyL') {
|
||||||
e.preventDefault();
|
const row = document.querySelector('[data-image-action="adj-levels"]');
|
||||||
e.stopPropagation();
|
if (row) { e.preventDefault(); e.stopPropagation(); row.click(); return; }
|
||||||
openLayerAdjustment?.('levels');
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
// Ctrl/Cmd+J duplicates the active layer through the layer panel's
|
// Ctrl/Cmd+J duplicates the active layer through the layer panel's
|
||||||
// existing implementation, which preserves masks and effects.
|
// existing implementation, which preserves masks and effects.
|
||||||
|
|||||||
@@ -2233,7 +2233,6 @@ export function createLayerPanelRenderer(deps) {
|
|||||||
if (dragSortModule) {
|
if (dragSortModule) {
|
||||||
dragSortModule.enable('ge-layers-list', '.ge-layer-item', {
|
dragSortModule.enable('ge-layers-list', '.ge-layer-item', {
|
||||||
instanceKey: 'ge-layers',
|
instanceKey: 'ge-layers',
|
||||||
excludeSelector: '.ge-adj-sub-item',
|
|
||||||
threshold: 4,
|
threshold: 4,
|
||||||
ignoreSelector: 'button, input, select, textarea, a, [contenteditable="true"], .ge-adj-sub-item',
|
ignoreSelector: 'button, input, select, textarea, a, [contenteditable="true"], .ge-adj-sub-item',
|
||||||
onReorder: (orderedItems) => {
|
onReorder: (orderedItems) => {
|
||||||
@@ -2259,9 +2258,6 @@ export function createLayerPanelRenderer(deps) {
|
|||||||
state.layers = newLayers;
|
state.layers = newLayers;
|
||||||
normalizeLayerClipping(state);
|
normalizeLayerClipping(state);
|
||||||
composite();
|
composite();
|
||||||
// Effects, adjustments and masks are sibling rows, not children
|
|
||||||
// of the dragged row. Rebuild them under their reordered owner.
|
|
||||||
render();
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ const size = () => number('size', 'Size', 8, 0, 100);
|
|||||||
const angle = () => number('angle', 'Angle', 120, -180, 180, 'deg');
|
const angle = () => number('angle', 'Angle', 120, -180, 180, 'deg');
|
||||||
export const LAYER_STYLES = {
|
export const LAYER_STYLES = {
|
||||||
'bevel-emboss': { label: 'Bevel & Emboss', controls: [size(), angle(), number('depth', 'Depth', 100, 0, 300, '%'), color('highlight', 'Highlight', '#ffffff'), color('color', 'Shadow', '#000000'), opacity()] },
|
'bevel-emboss': { label: 'Bevel & Emboss', controls: [size(), angle(), number('depth', 'Depth', 100, 0, 300, '%'), color('highlight', 'Highlight', '#ffffff'), color('color', 'Shadow', '#000000'), opacity()] },
|
||||||
'inner-shadow': { label: 'Inner Shadow', controls: [color('color', 'Color', '#000000'), opacity(), size(), angle(), number('distance', 'Distance', 6, 0, 300)] },
|
'inner-shadow': { label: 'Inner Shadow', controls: [color('color', 'Color', '#000000'), opacity(), size(), angle(), number('distance', 'Distance', 6, 0, 100)] },
|
||||||
'inner-glow': { label: 'Inner Glow', controls: [color('color', 'Color', '#ffffff'), opacity(), size()] },
|
'inner-glow': { label: 'Inner Glow', controls: [color('color', 'Color', '#ffffff'), opacity(), size()] },
|
||||||
satin: { label: 'Satin', controls: [color('color', 'Color', '#000000'), opacity(), size(), angle(), number('distance', 'Distance', 12, 0, 100)] },
|
satin: { label: 'Satin', controls: [color('color', 'Color', '#000000'), opacity(), size(), angle(), number('distance', 'Distance', 12, 0, 100)] },
|
||||||
'gradient-overlay': { label: 'Gradient Overlay', controls: [color('color', 'Start', '#e06c75'), color('endColor', 'End', '#ffffff'), opacity(), angle(), number('scale', 'Scale', 100, 10, 300, '%')] },
|
'gradient-overlay': { label: 'Gradient Overlay', controls: [color('color', 'Start', '#e06c75'), color('endColor', 'End', '#ffffff'), opacity(), angle(), number('scale', 'Scale', 100, 10, 300, '%')] },
|
||||||
@@ -32,13 +32,6 @@ function surface(width, height) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function renderDropShadow(source, params) {
|
export function renderDropShadow(source, params) {
|
||||||
if (params.inside) {
|
|
||||||
return renderLayerStyle(source, 'inner-shadow', {
|
|
||||||
color: params.color, opacity: params.opacity * 100, size: params.blur / 2,
|
|
||||||
distance: Math.hypot(params.x, params.y),
|
|
||||||
angle: Math.atan2(-params.y, params.x) * 180 / Math.PI,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
// Tint alpha first so RGB content cannot leak into the shadow. Draw only
|
// Tint alpha first so RGB content cannot leak into the shadow. Draw only
|
||||||
// this silhouette; compositing the source again thickens translucent edges.
|
// this silhouette; compositing the source again thickens translucent edges.
|
||||||
const silhouette = surface(source.width, source.height);
|
const silhouette = surface(source.width, source.height);
|
||||||
|
|||||||
@@ -1,13 +0,0 @@
|
|||||||
export function finishQuickEditJob(job, result) {
|
|
||||||
if (job.cancelled || result?.cancelled) {
|
|
||||||
job.cancelled = true;
|
|
||||||
job.status = 'cancelled';
|
|
||||||
job.detail = 'Cancelled';
|
|
||||||
} else if (job.status === 'error' || result?.ok === false) {
|
|
||||||
job.status = 'error';
|
|
||||||
job.detail = result?.error || result?.message || (job.detail !== 'Working…' && job.detail) || 'Edit failed';
|
|
||||||
} else {
|
|
||||||
job.status = 'done';
|
|
||||||
job.detail = result?.message || 'Done';
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -14,26 +14,19 @@ function toolSettings(tool) {
|
|||||||
|
|
||||||
let _lastColor = null;
|
let _lastColor = null;
|
||||||
let _lastTransparentColor = null;
|
let _lastTransparentColor = null;
|
||||||
function transparentColor(color, alpha = 0) {
|
function transparentColor(color) {
|
||||||
const key = `${color}|${alpha}`;
|
if (color === _lastColor && _lastTransparentColor) return _lastTransparentColor;
|
||||||
if (key === _lastColor && _lastTransparentColor) return _lastTransparentColor;
|
_lastColor = color;
|
||||||
_lastColor = key;
|
|
||||||
_lastTransparentColor = colorWithAlpha(color, alpha);
|
|
||||||
return _lastTransparentColor;
|
|
||||||
}
|
|
||||||
|
|
||||||
function colorWithAlpha(color, alpha) {
|
|
||||||
if (color.startsWith('#')) {
|
if (color.startsWith('#')) {
|
||||||
const raw = color.slice(1);
|
const raw = color.slice(1);
|
||||||
const expanded = raw.length === 3 ? raw.split('').map(char => char + char).join('') : raw;
|
const expanded = raw.length === 3 ? raw.split('').map(char => char + char).join('') : raw;
|
||||||
const rgb = [0, 2, 4].map(index => parseInt(expanded.slice(index, index + 2), 16));
|
const rgb = [0, 2, 4].map(index => parseInt(expanded.slice(index, index + 2), 16));
|
||||||
return `rgba(${rgb.join(',')},${alpha})`;
|
_lastTransparentColor = `rgba(${rgb.join(',')},0)`;
|
||||||
|
return _lastTransparentColor;
|
||||||
}
|
}
|
||||||
const match = color.match(/rgba?\(([^)]+)\)/);
|
const match = color.match(/rgba?\(([^)]+)\)/);
|
||||||
if (!match) return `rgba(0,0,0,${alpha})`;
|
_lastTransparentColor = match ? `rgba(${match[1].split(',').slice(0, 3).join(',')},0)` : 'rgba(0,0,0,0)';
|
||||||
const components = match[1].split(',');
|
return _lastTransparentColor;
|
||||||
const originalAlpha = components.length > 3 ? Number(components[3]) : 1;
|
|
||||||
return `rgba(${components.slice(0, 3).join(',')},${alpha * originalAlpha})`;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function circleStamp(ctx, x, y, radius, softness, color, alpha, operation) {
|
function circleStamp(ctx, x, y, radius, softness, color, alpha, operation) {
|
||||||
@@ -45,19 +38,8 @@ function circleStamp(ctx, x, y, radius, softness, color, alpha, operation) {
|
|||||||
ctx.fillStyle = color;
|
ctx.fillStyle = color;
|
||||||
} else {
|
} else {
|
||||||
const gradient = ctx.createRadialGradient(x, y, radius * hard, x, y, radius);
|
const gradient = ctx.createRadialGradient(x, y, radius * hard, x, y, radius);
|
||||||
if (softness > 1) {
|
gradient.addColorStop(0, color);
|
||||||
// Above the old maximum, remove the firm core with a progressively
|
gradient.addColorStop(1, transparentColor(color));
|
||||||
// gentler alpha profile while keeping the selected brush diameter.
|
|
||||||
ctx.fillStyle = color;
|
|
||||||
const normalized = ctx.fillStyle;
|
|
||||||
for (let step = 0; step <= 16; step++) {
|
|
||||||
const t = step / 16;
|
|
||||||
gradient.addColorStop(t, transparentColor(normalized, (1 - t) ** softness));
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
gradient.addColorStop(0, color);
|
|
||||||
gradient.addColorStop(1, transparentColor(color));
|
|
||||||
}
|
|
||||||
ctx.fillStyle = gradient;
|
ctx.fillStyle = gradient;
|
||||||
}
|
}
|
||||||
ctx.beginPath();
|
ctx.beginPath();
|
||||||
@@ -292,10 +274,6 @@ export function createStrokePipeline({ activeLayer, getActiveMaskLayer, composit
|
|||||||
if (!PAINT_TOOLS.has(tool)) return;
|
if (!PAINT_TOOLS.has(tool)) return;
|
||||||
const layer = activeLayer();
|
const layer = activeLayer();
|
||||||
if (!layer) return;
|
if (!layer) return;
|
||||||
// Pixel writes invalidate adjusted previews even when adjustment settings
|
|
||||||
// themselves have not changed (otherwise retouch strokes appear late).
|
|
||||||
layer._adjFinalKey = null;
|
|
||||||
layer._adjCacheKey = null;
|
|
||||||
if (tool === 'clone') return cloneStamp(sample, layer);
|
if (tool === 'clone') return cloneStamp(sample, layer);
|
||||||
if (tool === 'heal') {
|
if (tool === 'heal') {
|
||||||
// A healing stroke without a picked source uses the convenient local
|
// A healing stroke without a picked source uses the convenient local
|
||||||
@@ -316,7 +294,7 @@ export function createStrokePipeline({ activeLayer, getActiveMaskLayer, composit
|
|||||||
let operation = 'source-over';
|
let operation = 'source-over';
|
||||||
let color = state.color;
|
let color = state.color;
|
||||||
let alpha = opacity * flow;
|
let alpha = opacity * flow;
|
||||||
let softness = Math.max(0, Math.min(4, settings.softness / 300));
|
let softness = Math.max(0, Math.min(1, settings.softness / 300));
|
||||||
if (tool === 'eraser') { operation = 'destination-out'; color = 'rgba(0,0,0,1)'; }
|
if (tool === 'eraser') { operation = 'destination-out'; color = 'rgba(0,0,0,1)'; }
|
||||||
else if (tool === 'inpaint') {
|
else if (tool === 'inpaint') {
|
||||||
operation = state.inpaintEraseStroke ? 'destination-out' : 'source-over';
|
operation = state.inpaintEraseStroke ? 'destination-out' : 'source-over';
|
||||||
|
|||||||
@@ -57,11 +57,8 @@ function wireToolSliders(prefix, fields) {
|
|||||||
// softness rises (the CSS already sets the radial gradient — we
|
// softness rises (the CSS already sets the radial gradient — we
|
||||||
// just tween the inner solid radius to communicate the falloff).
|
// just tween the inner solid radius to communicate the falloff).
|
||||||
if (softPrev) {
|
if (softPrev) {
|
||||||
const amount = state[fields.softness];
|
const innerStop = Math.max(0, 60 - state[fields.softness] * 0.55);
|
||||||
const innerStop = Math.max(0, 60 - amount * 0.55);
|
softPrev.style.background = `radial-gradient(circle, var(--fg) 0%, var(--fg) ${innerStop}%, transparent 90%)`;
|
||||||
softPrev.style.background = amount > 300
|
|
||||||
? `radial-gradient(circle, var(--fg), color-mix(in srgb, var(--fg) ${Math.round(100 * 0.5 ** (amount / 300))}%, transparent) 50%, transparent 100%)`
|
|
||||||
: `radial-gradient(circle, var(--fg) 0%, var(--fg) ${innerStop}%, transparent 90%)`;
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -20,23 +20,6 @@ export function wireSubmenuToggles(menu) {
|
|||||||
menu.classList.remove('ge-flyout-open');
|
menu.classList.remove('ge-flyout-open');
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
function positionPanel(parent) {
|
|
||||||
const panel = parent.nextElementSibling;
|
|
||||||
if (!panel?.classList.contains('ge-submenu')) return;
|
|
||||||
const row = parent.getBoundingClientRect();
|
|
||||||
const origin = (panel.offsetParent || menu).getBoundingClientRect();
|
|
||||||
const top = Math.max(8, Math.min(row.top, window.innerHeight - panel.offsetHeight - 8));
|
|
||||||
const left = row.right + panel.offsetWidth > window.innerWidth - 8
|
|
||||||
? Math.max(8, row.left - panel.offsetWidth) : row.right - 2;
|
|
||||||
panel.style.top = `${top - origin.top + (panel.offsetParent?.scrollTop || 0)}px`;
|
|
||||||
panel.style.left = `${left - origin.left + (panel.offsetParent?.scrollLeft || 0)}px`;
|
|
||||||
}
|
|
||||||
for (const eventName of ['pointerover', 'focusin']) {
|
|
||||||
menu.addEventListener(eventName, (event) => {
|
|
||||||
const parent = event.target.closest('.ge-menu-parent');
|
|
||||||
if (parent) positionPanel(parent);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
menu.addEventListener('click', (e) => {
|
menu.addEventListener('click', (e) => {
|
||||||
const parent = e.target.closest('.ge-menu-parent');
|
const parent = e.target.closest('.ge-menu-parent');
|
||||||
if (!parent) return;
|
if (!parent) return;
|
||||||
@@ -51,7 +34,10 @@ export function wireSubmenuToggles(menu) {
|
|||||||
parent.setAttribute('aria-expanded', String(willOpen));
|
parent.setAttribute('aria-expanded', String(willOpen));
|
||||||
if (willOpen) {
|
if (willOpen) {
|
||||||
// Align the flyout with the clicked row rather than the menu top.
|
// Align the flyout with the clicked row rather than the menu top.
|
||||||
positionPanel(parent);
|
const panel = parent.nextElementSibling;
|
||||||
|
if (panel && panel.classList.contains('ge-submenu')) {
|
||||||
|
panel.style.top = parent.offsetTop + 'px';
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -37,7 +37,6 @@
|
|||||||
* }} deps
|
* }} deps
|
||||||
*/
|
*/
|
||||||
import { state } from './state.js';
|
import { state } from './state.js';
|
||||||
import { topPortalZ } from '../toolWindowZOrder.js';
|
|
||||||
import { isLayerPixelLocked, isLayerTransparencyLocked } from './layer-groups.js';
|
import { isLayerPixelLocked, isLayerTransparencyLocked } from './layer-groups.js';
|
||||||
|
|
||||||
const TOPBAR_MENU_IDS = ['ge-file-menu', 'ge-edit-menu', 'ge-view-menu', 'ge-image-menu', 'ge-layer-menu', 'ge-type-menu', 'ge-selection-menu', 'ge-filter-menu', 'ge-window-menu', 'ge-help-menu', 'ge-resize-menu', 'ge-save-menu'];
|
const TOPBAR_MENU_IDS = ['ge-file-menu', 'ge-edit-menu', 'ge-view-menu', 'ge-image-menu', 'ge-layer-menu', 'ge-type-menu', 'ge-selection-menu', 'ge-filter-menu', 'ge-window-menu', 'ge-help-menu', 'ge-resize-menu', 'ge-save-menu'];
|
||||||
@@ -102,7 +101,6 @@ export function wireTopbar(deps) {
|
|||||||
};
|
};
|
||||||
const positionSaveMenu = () => {
|
const positionSaveMenu = () => {
|
||||||
const r = saveBtn.getBoundingClientRect();
|
const r = saveBtn.getBoundingClientRect();
|
||||||
saveMenu.style.zIndex = String(topPortalZ());
|
|
||||||
saveMenu.style.top = `${r.bottom + 2}px`;
|
saveMenu.style.top = `${r.bottom + 2}px`;
|
||||||
saveMenu.style.right = `${Math.max(8, window.innerWidth - r.right)}px`;
|
saveMenu.style.right = `${Math.max(8, window.innerWidth - r.right)}px`;
|
||||||
saveMenu.style.left = 'auto';
|
saveMenu.style.left = 'auto';
|
||||||
|
|||||||
@@ -1321,12 +1321,7 @@ function _renderEditorLanding() {
|
|||||||
// openEditor() now returns a Promise — it's async because the size
|
// openEditor() now returns a Promise — it's async because the size
|
||||||
// prompt is a styled modal. Await it before checking whether the
|
// prompt is a styled modal. Await it before checking whether the
|
||||||
// editor actually opened (the user may have cancelled).
|
// editor actually opened (the user may have cancelled).
|
||||||
try {
|
await openEditor(null, null, null, 'New canvas');
|
||||||
await openEditor(null, null, null, 'New canvas');
|
|
||||||
} catch (error) {
|
|
||||||
// openEditor reports the initialization error and tears down its shell.
|
|
||||||
console.error('[gallery] New canvas failed', error);
|
|
||||||
}
|
|
||||||
if (!document.querySelector('#gallery-editor-container .gallery-editor')) {
|
if (!document.querySelector('#gallery-editor-container .gallery-editor')) {
|
||||||
_renderEditorLanding();
|
_renderEditorLanding();
|
||||||
}
|
}
|
||||||
|
|||||||
+57
-123
@@ -277,11 +277,8 @@ import { alphaBounds as _maskAlphaBounds, featherMaskCanvas as _featherMaskCanva
|
|||||||
import { analyzeAutoTone as _analyzeAutoTone, applyAutoTone as _applyAutoTone, boxBlurRGB as _boxBlurRGB, skinMaskFromData as _skinMaskFromData, frequencySeparationSmooth as _frequencySeparationSmooth, blemishRemoval as _blemishRemoval, teethWhitening as _teethWhitening, featureExclusionMask as _featureExclusionMask, dilateAlpha as _dilateAlpha, subtractMask as _subtractMask, guidedClarity as _guidedClarity } from './editor/retouch.js';
|
import { analyzeAutoTone as _analyzeAutoTone, applyAutoTone as _applyAutoTone, boxBlurRGB as _boxBlurRGB, skinMaskFromData as _skinMaskFromData, frequencySeparationSmooth as _frequencySeparationSmooth, blemishRemoval as _blemishRemoval, teethWhitening as _teethWhitening, featureExclusionMask as _featureExclusionMask, dilateAlpha as _dilateAlpha, subtractMask as _subtractMask, guidedClarity as _guidedClarity } from './editor/retouch.js';
|
||||||
import { wireClipboardAndDrop } from './editor/clipboard-and-drop.js';
|
import { wireClipboardAndDrop } from './editor/clipboard-and-drop.js';
|
||||||
import { wireAIModelSelectors, resolveInpaintModel, resolveQuickEditModel, resolveQuickEditAgentModel } from './editor/ai-models.js';
|
import { wireAIModelSelectors, resolveInpaintModel, resolveQuickEditModel, resolveQuickEditAgentModel } from './editor/ai-models.js';
|
||||||
|
import { createPreflight } from './editor/preflight.js';
|
||||||
import { createAiSettings } from './editor/ai-settings.js';
|
import { createAiSettings } from './editor/ai-settings.js';
|
||||||
import { waitForImagePreferences } from './settings/imagePreferences.js';
|
|
||||||
import { createImageGenerationAction } from './editor/image-generation.js';
|
|
||||||
import { finishQuickEditJob } from './editor/quick-edit-job.js';
|
|
||||||
import { imageResultGeometry } from './editor/image-result-geometry.js';
|
|
||||||
import { lookRecipeSteps } from './editor/look-recipes.js';
|
import { lookRecipeSteps } from './editor/look-recipes.js';
|
||||||
import { wireInpaintButtons } from './editor/ai-inpaint.js?v=20260708match1';
|
import { wireInpaintButtons } from './editor/ai-inpaint.js?v=20260708match1';
|
||||||
import { wireAIToolsMisc } from './editor/ai-tools-misc.js';
|
import { wireAIToolsMisc } from './editor/ai-tools-misc.js';
|
||||||
@@ -359,6 +356,7 @@ function _computeTransformSnap(frame, center) {
|
|||||||
const _INPAINT_DEFAULT_BRUSH = 100;
|
const _INPAINT_DEFAULT_BRUSH = 100;
|
||||||
let _samAbortController = null;
|
let _samAbortController = null;
|
||||||
let _precisionGuides = null;
|
let _precisionGuides = null;
|
||||||
|
let _preflight = null;
|
||||||
let _aiSettings = null;
|
let _aiSettings = null;
|
||||||
let _selectionAnimationFrame = null;
|
let _selectionAnimationFrame = null;
|
||||||
let _selectionAnimationLast = 0;
|
let _selectionAnimationLast = 0;
|
||||||
@@ -396,12 +394,6 @@ if (!window.__galleryEditEscHardGuardInstalled) {
|
|||||||
}
|
}
|
||||||
if (e.target?.closest?.('#styled-confirm-overlay')) return;
|
if (e.target?.closest?.('#styled-confirm-overlay')) return;
|
||||||
if (e.key === 'Escape') {
|
if (e.key === 'Escape') {
|
||||||
const aiSettings = document.querySelector('.ge-ai-settings:not([hidden])');
|
|
||||||
if (aiSettings) {
|
|
||||||
e.preventDefault(); e.stopImmediatePropagation();
|
|
||||||
aiSettings.querySelector('[data-ai-settings-close]')?.click();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
// Floating dialogs/popups that live outside the editor modal must close on
|
// Floating dialogs/popups that live outside the editor modal must close on
|
||||||
// Escape reliably (their document-capture handlers can miss when focus is
|
// Escape reliably (their document-capture handlers can miss when focus is
|
||||||
// outside them). Close the top-most one and stop here.
|
// outside them). Close the top-most one and stop here.
|
||||||
@@ -846,7 +838,6 @@ function _setCanvasAiBusy(busy) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let _aiProgressTimer = null;
|
let _aiProgressTimer = null;
|
||||||
let _aiProgressEpoch = 0;
|
|
||||||
function _setAiProgress(frac, label) {
|
function _setAiProgress(frac, label) {
|
||||||
const wrap = document.getElementById('ge-ai-command-progress');
|
const wrap = document.getElementById('ge-ai-command-progress');
|
||||||
if (!wrap) return;
|
if (!wrap) return;
|
||||||
@@ -866,17 +857,12 @@ function _startAiProgress(requestId) {
|
|||||||
const endpointId = arguments[1] || '';
|
const endpointId = arguments[1] || '';
|
||||||
_stopAiProgress();
|
_stopAiProgress();
|
||||||
if (!requestId) return;
|
if (!requestId) return;
|
||||||
const epoch = _aiProgressEpoch;
|
|
||||||
let pending = false;
|
|
||||||
const tick = async () => {
|
const tick = async () => {
|
||||||
if (pending || epoch !== _aiProgressEpoch) return;
|
|
||||||
pending = true;
|
|
||||||
try {
|
try {
|
||||||
const epQuery = endpointId ? `&endpoint_id=${encodeURIComponent(endpointId)}` : '';
|
const epQuery = endpointId ? `&endpoint_id=${encodeURIComponent(endpointId)}` : '';
|
||||||
const r = await fetch(`/api/image/edit-progress?request_id=${encodeURIComponent(requestId)}` + epQuery, { credentials: 'same-origin' });
|
const r = await fetch(`/api/image/edit-progress?request_id=${encodeURIComponent(requestId)}` + epQuery, { credentials: 'same-origin' });
|
||||||
if (!r.ok) return;
|
if (!r.ok) return;
|
||||||
const p = await r.json();
|
const p = await r.json();
|
||||||
if (epoch !== _aiProgressEpoch) return;
|
|
||||||
const st = p.stage || '';
|
const st = p.stage || '';
|
||||||
if (st === 'done' || st === 'error') { _setAiProgress(1, st === 'done' ? 'Done' : 'Failed'); return; }
|
if (st === 'done' || st === 'error') { _setAiProgress(1, st === 'done' ? 'Done' : 'Failed'); return; }
|
||||||
const frac = typeof p.progress === 'number' ? p.progress : 0;
|
const frac = typeof p.progress === 'number' ? p.progress : 0;
|
||||||
@@ -885,13 +871,12 @@ function _startAiProgress(requestId) {
|
|||||||
const verb = st === 'editing' ? 'Generating' : (st ? st[0].toUpperCase() + st.slice(1) : 'Working');
|
const verb = st === 'editing' ? 'Generating' : (st ? st[0].toUpperCase() + st.slice(1) : 'Working');
|
||||||
_setAiProgress(frac, `${verb}…${stepTxt}${eta}`);
|
_setAiProgress(frac, `${verb}…${stepTxt}${eta}`);
|
||||||
_setAiCommandStatus(`${verb}…${stepTxt}${eta}`, 'running');
|
_setAiCommandStatus(`${verb}…${stepTxt}${eta}`, 'running');
|
||||||
} catch (_) {} finally { pending = false; }
|
} catch (_) {}
|
||||||
};
|
};
|
||||||
_aiProgressTimer = setInterval(tick, 1000);
|
_aiProgressTimer = setInterval(tick, 1000);
|
||||||
tick();
|
tick();
|
||||||
}
|
}
|
||||||
function _stopAiProgress() {
|
function _stopAiProgress() {
|
||||||
_aiProgressEpoch++;
|
|
||||||
if (_aiProgressTimer) { clearInterval(_aiProgressTimer); _aiProgressTimer = null; }
|
if (_aiProgressTimer) { clearInterval(_aiProgressTimer); _aiProgressTimer = null; }
|
||||||
_setAiProgress(null);
|
_setAiProgress(null);
|
||||||
}
|
}
|
||||||
@@ -1089,7 +1074,6 @@ const _QUICK_EDIT_TOOL_SUGGESTIONS = Object.entries(_TOOLS).map(([id, t]) => ({
|
|||||||
kind: 'Tool',
|
kind: 'Tool',
|
||||||
icon: t.icon || '',
|
icon: t.icon || '',
|
||||||
toolId: id,
|
toolId: id,
|
||||||
runPrompt: id === 'rembg' ? 'remove background' : null,
|
|
||||||
aliases: [id],
|
aliases: [id],
|
||||||
}));
|
}));
|
||||||
const _QUICK_EDIT_CATALOG = [..._QUICK_EDIT_TOOL_SUGGESTIONS, ..._AI_COMMAND_SUGGESTIONS];
|
const _QUICK_EDIT_CATALOG = [..._QUICK_EDIT_TOOL_SUGGESTIONS, ..._AI_COMMAND_SUGGESTIONS];
|
||||||
@@ -1297,13 +1281,8 @@ function _wireAiCommandBox() {
|
|||||||
if (item.toolId) {
|
if (item.toolId) {
|
||||||
_clickToolButton(item.toolId);
|
_clickToolButton(item.toolId);
|
||||||
_setAiCommandStatus(`${item.label} tool`, 'done');
|
_setAiCommandStatus(`${item.label} tool`, 'done');
|
||||||
if (item.runPrompt) {
|
|
||||||
input.value = item.runPrompt;
|
|
||||||
syncClearButton();
|
|
||||||
}
|
|
||||||
hideSuggestions();
|
hideSuggestions();
|
||||||
input.focus();
|
input.focus();
|
||||||
if (run && item.runPrompt) form.requestSubmit();
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
input.value = item.insert;
|
input.value = item.insert;
|
||||||
@@ -1492,24 +1471,6 @@ function _wireAiCommandBox() {
|
|||||||
// model (window.geEditorAI) just like the server would route it.
|
// model (window.geEditorAI) just like the server would route it.
|
||||||
const _executeQuickEditLocal = async (prompt) => {
|
const _executeQuickEditLocal = async (prompt) => {
|
||||||
const p = prompt.toLowerCase();
|
const p = prompt.toLowerCase();
|
||||||
// Background-removal intent (shared with the routing guard so the regex
|
|
||||||
// can never drift between the two call sites).
|
|
||||||
if (_looksBackgroundRemoval(p)) {
|
|
||||||
await _runQuickControlOp('ai.removeBackground', {});
|
|
||||||
// Chain a tone step when the prompt also asks for one
|
|
||||||
// ("remove the background and make it brighter", "...and warm it up").
|
|
||||||
if (/\b(bright(?:er|en)?|lighten|exposure|well[-\s]*lit)\b/.test(p)) {
|
|
||||||
await _runQuickControlOp('adjustments.apply', { type: 'brightness-contrast', params: { brightness: 16, contrast: 4 } });
|
|
||||||
} else if (/\b(dark(?:er|en)?|dimmer)\b/.test(p)) {
|
|
||||||
await _runQuickControlOp('adjustments.apply', { type: 'brightness-contrast', params: { brightness: -14, contrast: 4 } });
|
|
||||||
} else if (/\b(warm(?:er)?)\b/.test(p)) {
|
|
||||||
await _runQuickControlOp('adjustments.apply', { type: 'white-balance', params: { temperature: 18 } });
|
|
||||||
} else if (/\b(cool(?:er)?)\b/.test(p)) {
|
|
||||||
await _runQuickControlOp('adjustments.apply', { type: 'white-balance', params: { temperature: -18 } });
|
|
||||||
}
|
|
||||||
_setAiCommandStatus('Background removal finished.', 'done');
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
// A targeted recolour ("make only the red car blue", "recolour the rock to
|
// A targeted recolour ("make only the red car blue", "recolour the rock to
|
||||||
// teal") needs the model to repaint a specific object — never fake it with a
|
// teal") needs the model to repaint a specific object — never fake it with a
|
||||||
// global colour tweak from the deterministic fast path.
|
// global colour tweak from the deterministic fast path.
|
||||||
@@ -1972,6 +1933,24 @@ function _wireAiCommandBox() {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// Background-removal intent (shared with the routing guard so the regex
|
||||||
|
// can never drift between the two call sites).
|
||||||
|
if (_looksBackgroundRemoval(p)) {
|
||||||
|
await _runQuickControlOp('ai.removeBackground', {});
|
||||||
|
// Chain a tone step when the prompt also asks for one
|
||||||
|
// ("remove the background and make it brighter", "...and warm it up").
|
||||||
|
if (/\b(bright(?:er|en)?|lighten|exposure|well[-\s]*lit)\b/.test(p)) {
|
||||||
|
await _runQuickControlOp('adjustments.apply', { type: 'brightness-contrast', params: { brightness: 16, contrast: 4 } });
|
||||||
|
} else if (/\b(dark(?:er|en)?|dimmer)\b/.test(p)) {
|
||||||
|
await _runQuickControlOp('adjustments.apply', { type: 'brightness-contrast', params: { brightness: -14, contrast: 4 } });
|
||||||
|
} else if (/\b(warm(?:er)?)\b/.test(p)) {
|
||||||
|
await _runQuickControlOp('adjustments.apply', { type: 'white-balance', params: { temperature: 18 } });
|
||||||
|
} else if (/\b(cool(?:er)?)\b/.test(p)) {
|
||||||
|
await _runQuickControlOp('adjustments.apply', { type: 'white-balance', params: { temperature: -18 } });
|
||||||
|
}
|
||||||
|
_setAiCommandStatus('Background removal finished.', 'done');
|
||||||
|
return;
|
||||||
|
}
|
||||||
if (!/\b(text|title|caption|font|heading|subtitle)\b/.test(p)
|
if (!/\b(text|title|caption|font|heading|subtitle)\b/.test(p)
|
||||||
&& /\b(upscale|higher\s*res|increase\s*resolution|bigger|2x|4x)\b/.test(p)) {
|
&& /\b(upscale|higher\s*res|increase\s*resolution|bigger|2x|4x)\b/.test(p)) {
|
||||||
// Use the real upscale op (Real-ESRGAN, basic-upscale fallback). The
|
// Use the real upscale op (Real-ESRGAN, basic-upscale fallback). The
|
||||||
@@ -2835,8 +2814,8 @@ function _wireAiCommandBox() {
|
|||||||
if (_looksBackgroundRemoval(prompt)) {
|
if (_looksBackgroundRemoval(prompt)) {
|
||||||
_setAiCommandStatus('Removing the background…', 'running');
|
_setAiCommandStatus('Removing the background…', 'running');
|
||||||
try {
|
try {
|
||||||
await _executeQuickEditLocal(prompt);
|
const done = await _executeQuickEditLocal(prompt);
|
||||||
return { ok: true };
|
if (done !== false) return { ok: true };
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
if (signal && signal.aborted) { _setAiCommandStatus('', ''); return { ok: false, cancelled: true }; }
|
if (signal && signal.aborted) { _setAiCommandStatus('', ''); return { ok: false, cancelled: true }; }
|
||||||
const msg = err && err.message ? err.message : String(err);
|
const msg = err && err.message ? err.message : String(err);
|
||||||
@@ -2862,7 +2841,6 @@ function _wireAiCommandBox() {
|
|||||||
_setAiCommandStatus('Generated an image.', 'done');
|
_setAiCommandStatus('Generated an image.', 'done');
|
||||||
return { ok: true, image: res };
|
return { ok: true, image: res };
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
if (signal?.aborted) return { ok: false, cancelled: true };
|
|
||||||
const msg = err && err.message ? err.message : String(err);
|
const msg = err && err.message ? err.message : String(err);
|
||||||
_setAiCommandStatus(_isGenOfflineMsg(msg) ? msg : `Could not generate an image: ${msg}`, 'error');
|
_setAiCommandStatus(_isGenOfflineMsg(msg) ? msg : `Could not generate an image: ${msg}`, 'error');
|
||||||
return { ok: false, error: msg };
|
return { ok: false, error: msg };
|
||||||
@@ -3105,22 +3083,19 @@ function _wireAiCommandBox() {
|
|||||||
job.controller = controller;
|
job.controller = controller;
|
||||||
renderJobStack();
|
renderJobStack();
|
||||||
_quickEditActiveJob = job;
|
_quickEditActiveJob = job;
|
||||||
let result;
|
|
||||||
try {
|
try {
|
||||||
result = await executeQuickEdit(job.prompt, { forceAi: !!job.forceAi, signal: controller ? controller.signal : null });
|
await executeQuickEdit(job.prompt, { forceAi: !!job.forceAi, signal: controller ? controller.signal : null });
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
if (!job.cancelled) {
|
if (!job.cancelled) {
|
||||||
job.status = 'error';
|
job.status = 'error';
|
||||||
job.detail = err?.message || 'AI edit failed';
|
job.detail = err?.message || 'AI edit failed';
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
_stopAiProgress();
|
|
||||||
finishQuickEditJob(job, result);
|
|
||||||
_setAiCommandStatus(job.cancelled ? '' : job.detail, job.cancelled ? '' : job.status);
|
|
||||||
if (_quickEditActiveJob === job) _quickEditActiveJob = null;
|
if (_quickEditActiveJob === job) _quickEditActiveJob = null;
|
||||||
job.onChange = null;
|
job.onChange = null;
|
||||||
job.controller = null;
|
job.controller = null;
|
||||||
if (job.cancelled) continue;
|
if (job.cancelled) continue;
|
||||||
|
if (job.status === 'running') job.status = 'done';
|
||||||
renderJobStack();
|
renderJobStack();
|
||||||
setTimeout(() => dismissJob(job), job.status === 'error' ? 3200 : 1600);
|
setTimeout(() => dismissJob(job), job.status === 'error' ? 3200 : 1600);
|
||||||
}
|
}
|
||||||
@@ -5329,14 +5304,9 @@ function _restoreState(snap) {
|
|||||||
// inside _buildEditor, so we touch the DOM here).
|
// inside _buildEditor, so we touch the DOM here).
|
||||||
const sizeLabel = document.getElementById('ge-canvas-size');
|
const sizeLabel = document.getElementById('ge-canvas-size');
|
||||||
if (sizeLabel) sizeLabel.textContent = `${state.imgWidth}×${state.imgHeight} px`;
|
if (sizeLabel) sizeLabel.textContent = `${state.imgWidth}×${state.imgHeight} px`;
|
||||||
state.rembgLiveLayer = null;
|
|
||||||
state.rembgLiveSnap = null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function undo() {
|
function undo() {
|
||||||
// End sampling before restoring pixels; pointer-up must not replay the
|
|
||||||
// unfinished stroke into the restored layer without a new history entry.
|
|
||||||
if (state.drawing) _strokeTool.tryEnd();
|
|
||||||
if (state.undoStack.length === 0) return;
|
if (state.undoStack.length === 0) return;
|
||||||
const target = state.undoStack.pop();
|
const target = state.undoStack.pop();
|
||||||
const cur = _snapshotState();
|
const cur = _snapshotState();
|
||||||
@@ -6565,11 +6535,6 @@ const _nudgeTransform = _transformSession.nudgeTransform;
|
|||||||
const _confirmTransform = _transformSession.confirmTransform;
|
const _confirmTransform = _transformSession.confirmTransform;
|
||||||
const _cancelTransform = _transformSession.cancelTransform;
|
const _cancelTransform = _transformSession.cancelTransform;
|
||||||
window.__galleryEditorHandleEscape = () => {
|
window.__galleryEditorHandleEscape = () => {
|
||||||
const aiSettings = document.querySelector('.ge-ai-settings:not([hidden])');
|
|
||||||
if (aiSettings) {
|
|
||||||
aiSettings.querySelector('[data-ai-settings-close]')?.click();
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
const sizePrompt = document.getElementById('ge-canvas-size-overlay');
|
const sizePrompt = document.getElementById('ge-canvas-size-overlay');
|
||||||
if (sizePrompt && getComputedStyle(sizePrompt).display !== 'none') {
|
if (sizePrompt && getComputedStyle(sizePrompt).display !== 'none') {
|
||||||
sizePrompt._cancelCanvasSize?.();
|
sizePrompt._cancelCanvasSize?.();
|
||||||
@@ -8099,7 +8064,6 @@ async function _canMutateLayerPixels(layer, action = 'editing pixels') {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (!['placed', 'text', 'shape'].includes(layer.kind)) return true;
|
if (!['placed', 'text', 'shape'].includes(layer.kind)) return true;
|
||||||
if (action === 'cutting pixels') return _rasterizeLayerNow(layer);
|
|
||||||
return _confirmRasterizeLayer(layer);
|
return _confirmRasterizeLayer(layer);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -8298,13 +8262,10 @@ function _filterSliderPrompt(title, params, onPreview, onCancel) {
|
|||||||
// standard slim red-thumb styling instead of the bare browser
|
// standard slim red-thumb styling instead of the bare browser
|
||||||
// default. Value chip on the same line as the label.
|
// default. Value chip on the same line as the label.
|
||||||
const isColor = p.type === 'color';
|
const isColor = p.type === 'color';
|
||||||
const isSelect = p.type === 'select';
|
|
||||||
rows += `
|
rows += `
|
||||||
<div class="ge-filter-row ge-eraser-row">
|
<div class="ge-filter-row ge-eraser-row">
|
||||||
<label>${p.label}${isColor || isSelect ? '' : ` <span class="ge-filter-row-value" data-val-for="${p.key}">${p.value}${p.suffix || ''}</span>`}</label>
|
<label>${p.label}${isColor ? '' : ` <span class="ge-filter-row-value" data-val-for="${p.key}">${p.value}${p.suffix || ''}</span>`}</label>
|
||||||
${isSelect
|
${isColor
|
||||||
? `<select data-key="${p.key}" class="ge-ctl-select" aria-label="${p.label}">${p.options.map(option => `<option value="${option.value}"${String(option.value) === String(p.value) ? ' selected' : ''}>${option.label}</option>`).join('')}</select>`
|
|
||||||
: isColor
|
|
||||||
? `<input type="color" data-key="${p.key}" value="${p.value}" aria-label="${p.label}" />`
|
? `<input type="color" data-key="${p.key}" value="${p.value}" aria-label="${p.label}" />`
|
||||||
: `<input type="range" data-key="${p.key}" min="${p.min}" max="${p.max}" step="${p.step || 1}" value="${p.value}" />`}
|
: `<input type="range" data-key="${p.key}" min="${p.min}" max="${p.max}" step="${p.step || 1}" value="${p.value}" />`}
|
||||||
</div>
|
</div>
|
||||||
@@ -8325,7 +8286,7 @@ function _filterSliderPrompt(title, params, onPreview, onCancel) {
|
|||||||
for (const p of params) values[p.key] = p.value;
|
for (const p of params) values[p.key] = p.value;
|
||||||
// Initial preview render.
|
// Initial preview render.
|
||||||
try { onPreview(values); } catch {}
|
try { onPreview(values); } catch {}
|
||||||
overlay.querySelectorAll('input[data-key], select[data-key]').forEach(inp => {
|
overlay.querySelectorAll('input[data-key]').forEach(inp => {
|
||||||
inp.addEventListener('input', (e) => {
|
inp.addEventListener('input', (e) => {
|
||||||
if (!isCurrent()) { cleanup(null); return; }
|
if (!isCurrent()) { cleanup(null); return; }
|
||||||
const k = e.target.dataset.key;
|
const k = e.target.dataset.key;
|
||||||
@@ -8356,7 +8317,7 @@ function _filterSliderPrompt(title, params, onPreview, onCancel) {
|
|||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
cleanup(e.target?.dataset.action === 'cancel' ? null : values);
|
cleanup(e.target?.dataset.action === 'cancel' ? null : values);
|
||||||
} else if (e.key === 'Tab') {
|
} else if (e.key === 'Tab') {
|
||||||
const fields = [...overlay.querySelectorAll('input, select, button')];
|
const fields = [...overlay.querySelectorAll('input, button')];
|
||||||
const index = fields.indexOf(document.activeElement);
|
const index = fields.indexOf(document.activeElement);
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
fields[(index + (e.shiftKey ? -1 : 1) + fields.length) % fields.length]?.focus();
|
fields[(index + (e.shiftKey ? -1 : 1) + fields.length) % fields.length]?.focus();
|
||||||
@@ -8368,7 +8329,7 @@ function _filterSliderPrompt(title, params, onPreview, onCancel) {
|
|||||||
if (!overlay.isConnected || !isCurrent()) cleanup(null);
|
if (!overlay.isConnected || !isCurrent()) cleanup(null);
|
||||||
});
|
});
|
||||||
observer.observe(state.container, { childList: true, subtree: true });
|
observer.observe(state.container, { childList: true, subtree: true });
|
||||||
overlay.querySelector('input, select, button')?.focus({ preventScroll: true });
|
overlay.querySelector('input, button')?.focus({ preventScroll: true });
|
||||||
overlay.querySelector('[data-action="apply"]').addEventListener('click', () => cleanup(values));
|
overlay.querySelector('[data-action="apply"]').addEventListener('click', () => cleanup(values));
|
||||||
overlay.querySelector('[data-action="cancel"]').addEventListener('click', () => cleanup(null));
|
overlay.querySelector('[data-action="cancel"]').addEventListener('click', () => cleanup(null));
|
||||||
// Click outside the modal (on the dim backdrop) = cancel.
|
// Click outside the modal (on the dim backdrop) = cancel.
|
||||||
@@ -8534,7 +8495,6 @@ function _shadowOffsets(values = {}) {
|
|||||||
return {
|
return {
|
||||||
x: Math.round(Math.cos(radians) * distance),
|
x: Math.round(Math.cos(radians) * distance),
|
||||||
y: Math.round(-Math.sin(radians) * distance),
|
y: Math.round(-Math.sin(radians) * distance),
|
||||||
inside: Number(values.inside) === 1,
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -8596,7 +8556,6 @@ async function _addRetainedEffect(type, presetName = null) {
|
|||||||
{ key: 'color', label: 'Color', type: 'color', value: '#000000' },
|
{ key: 'color', label: 'Color', type: 'color', value: '#000000' },
|
||||||
{ key: 'opacity', label: 'Opacity', min: 0, max: 100, step: 1, value: 45, suffix: '%' },
|
{ key: 'opacity', label: 'Opacity', min: 0, max: 100, step: 1, value: 45, suffix: '%' },
|
||||||
{ key: 'blur', label: 'Blur', min: 0, max: 100, step: 1, value: 12, suffix: 'px' },
|
{ key: 'blur', label: 'Blur', min: 0, max: 100, step: 1, value: 12, suffix: 'px' },
|
||||||
{ key: 'inside', label: 'Position', type: 'select', value: 0, options: [{ value: 0, label: 'Outside' }, { value: 1, label: 'Inside' }] },
|
|
||||||
{ key: 'distance', label: 'Distance', min: 0, max: 200, step: 1, value: _shadowAngleDistance({ x: 4, y: 6 }).distance, suffix: 'px' },
|
{ key: 'distance', label: 'Distance', min: 0, max: 200, step: 1, value: _shadowAngleDistance({ x: 4, y: 6 }).distance, suffix: 'px' },
|
||||||
{ key: 'angle', label: 'Direction', min: -180, max: 180, step: 1, value: _shadowAngleDistance({ x: 4, y: 6 }).angle, suffix: '\u00b0' },
|
{ key: 'angle', label: 'Direction', min: -180, max: 180, step: 1, value: _shadowAngleDistance({ x: 4, y: 6 }).angle, suffix: '\u00b0' },
|
||||||
];
|
];
|
||||||
@@ -8810,7 +8769,6 @@ async function _editRetainedEffect(layer, effect) {
|
|||||||
{ key: 'color', label: 'Color', type: 'color', value: p.color || '#000000' },
|
{ key: 'color', label: 'Color', type: 'color', value: p.color || '#000000' },
|
||||||
{ key: 'opacity', label: 'Opacity', min: 0, max: 100, step: 1, value: Math.round((Number(p.opacity) || 0) * 100), suffix: '%' },
|
{ key: 'opacity', label: 'Opacity', min: 0, max: 100, step: 1, value: Math.round((Number(p.opacity) || 0) * 100), suffix: '%' },
|
||||||
{ key: 'blur', label: 'Blur', min: 0, max: 100, step: 1, value: Number(p.blur) || 0, suffix: 'px' },
|
{ key: 'blur', label: 'Blur', min: 0, max: 100, step: 1, value: Number(p.blur) || 0, suffix: 'px' },
|
||||||
{ key: 'inside', label: 'Position', type: 'select', value: p.inside ? 1 : 0, options: [{ value: 0, label: 'Outside' }, { value: 1, label: 'Inside' }] },
|
|
||||||
{ key: 'distance', label: 'Distance', min: 0, max: 200, step: 1, value: _shadowAngleDistance(p).distance, suffix: 'px' },
|
{ key: 'distance', label: 'Distance', min: 0, max: 200, step: 1, value: _shadowAngleDistance(p).distance, suffix: 'px' },
|
||||||
{ key: 'angle', label: 'Direction', min: -180, max: 180, step: 1, value: _shadowAngleDistance(p).angle, suffix: '\u00b0' },
|
{ key: 'angle', label: 'Direction', min: -180, max: 180, step: 1, value: _shadowAngleDistance(p).angle, suffix: '\u00b0' },
|
||||||
];
|
];
|
||||||
@@ -10035,8 +9993,9 @@ function _buildEditor(container) {
|
|||||||
if (colorRow) colorRow.style.display = ['eraser', 'clone', 'heal', 'smudge', 'dodge', 'burn', 'liquify'].includes(toolId) ? 'none' : '';
|
if (colorRow) colorRow.style.display = ['eraser', 'clone', 'heal', 'smudge', 'dodge', 'burn', 'liquify'].includes(toolId) ? 'none' : '';
|
||||||
const colorLabel = colorRow?.querySelector('label');
|
const colorLabel = colorRow?.querySelector('label');
|
||||||
if (colorLabel) colorLabel.textContent = 'Color';
|
if (colorLabel) colorLabel.textContent = 'Color';
|
||||||
// Secondary modifiers stay in More; brush softness remains in the main
|
// Per-tool stroke modifiers all live in the shared brush "More" menu so
|
||||||
// strip. Only the active tool's secondary group is shown.
|
// every brush-family tool has the same compact strip as the Brush. Only
|
||||||
|
// the group for the active tool is shown; the rest stay hidden.
|
||||||
const strokeTool = (toolId === 'brush' || toolId === 'pixel' || toolId === 'dodge' || toolId === 'burn') ? 'brush'
|
const strokeTool = (toolId === 'brush' || toolId === 'pixel' || toolId === 'dodge' || toolId === 'burn') ? 'brush'
|
||||||
: toolId === 'eraser' ? 'eraser'
|
: toolId === 'eraser' ? 'eraser'
|
||||||
: (toolId === 'clone' || toolId === 'heal') ? 'clone'
|
: (toolId === 'clone' || toolId === 'heal') ? 'clone'
|
||||||
@@ -10046,8 +10005,6 @@ function _buildEditor(container) {
|
|||||||
row.style.display = row.dataset.strokeTool === strokeTool ? '' : 'none';
|
row.style.display = row.dataset.strokeTool === strokeTool ? '' : 'none';
|
||||||
});
|
});
|
||||||
const dodgeBurnRow = document.getElementById('ge-dodgeburn-intensity-row');
|
const dodgeBurnRow = document.getElementById('ge-dodgeburn-intensity-row');
|
||||||
const brushSoftnessRow = document.getElementById('ge-brush-softness-row');
|
|
||||||
if (brushSoftnessRow) brushSoftnessRow.style.display = ['brush', 'dodge', 'burn'].includes(toolId) ? '' : 'none';
|
|
||||||
// Intensity sits in the strip next to Size, so it must show for Dodge and
|
// Intensity sits in the strip next to Size, so it must show for Dodge and
|
||||||
// Burn only (and never for the other brush-family tools).
|
// Burn only (and never for the other brush-family tools).
|
||||||
if (dodgeBurnRow) dodgeBurnRow.style.display = (toolId === 'dodge' || toolId === 'burn') ? '' : 'none';
|
if (dodgeBurnRow) dodgeBurnRow.style.display = (toolId === 'dodge' || toolId === 'burn') ? '' : 'none';
|
||||||
@@ -10500,6 +10457,7 @@ function _buildEditor(container) {
|
|||||||
canvasArea.appendChild(state.transformOverlay);
|
canvasArea.appendChild(state.transformOverlay);
|
||||||
canvasArea.appendChild(_buildQuickMaskBar());
|
canvasArea.appendChild(_buildQuickMaskBar());
|
||||||
canvasArea.appendChild(_buildAiCommandBox());
|
canvasArea.appendChild(_buildAiCommandBox());
|
||||||
|
_preflight = createPreflight({ canvasArea });
|
||||||
_aiSettings = createAiSettings({ container });
|
_aiSettings = createAiSettings({ container });
|
||||||
try { window.geEditorAiSettings = _aiSettings; } catch (_) {}
|
try { window.geEditorAiSettings = _aiSettings; } catch (_) {}
|
||||||
// On phones the rulers eat scarce canvas width; default them off.
|
// On phones the rulers eat scarce canvas width; default them off.
|
||||||
@@ -10692,10 +10650,19 @@ function _buildEditor(container) {
|
|||||||
const initialStatusTool = document.getElementById('ge-status-tool');
|
const initialStatusTool = document.getElementById('ge-status-tool');
|
||||||
if (initialStatusTool) initialStatusTool.textContent = _TOOLS[state.tool]?.label || state.tool || '';
|
if (initialStatusTool) initialStatusTool.textContent = _TOOLS[state.tool]?.label || state.tool || '';
|
||||||
}
|
}
|
||||||
// Keep model configuration in the AI settings dialog.
|
// Preflight checklist: greet the user on open (unless dismissed) and
|
||||||
|
// expose a footer button to re-open it.
|
||||||
try {
|
try {
|
||||||
const footer = container.querySelector('.ge-editor-footer');
|
const footer = container.querySelector('.ge-editor-footer');
|
||||||
if (footer) {
|
if (footer) {
|
||||||
|
const setupBtn = document.createElement('button');
|
||||||
|
setupBtn.type = 'button';
|
||||||
|
setupBtn.id = 'ge-setup-btn';
|
||||||
|
setupBtn.className = 'ge-setup-btn';
|
||||||
|
setupBtn.title = 'Editor setup checklist';
|
||||||
|
setupBtn.textContent = 'Setup';
|
||||||
|
setupBtn.addEventListener('click', () => _preflight?.open());
|
||||||
|
footer.appendChild(setupBtn);
|
||||||
const aiBtn = document.createElement('button');
|
const aiBtn = document.createElement('button');
|
||||||
aiBtn.type = 'button';
|
aiBtn.type = 'button';
|
||||||
aiBtn.id = 'ge-ai-settings-btn';
|
aiBtn.id = 'ge-ai-settings-btn';
|
||||||
@@ -10705,6 +10672,7 @@ function _buildEditor(container) {
|
|||||||
aiBtn.addEventListener('click', () => _aiSettings?.open());
|
aiBtn.addEventListener('click', () => _aiSettings?.open());
|
||||||
footer.appendChild(aiBtn);
|
footer.appendChild(aiBtn);
|
||||||
}
|
}
|
||||||
|
_preflight?.maybeOpen();
|
||||||
} catch (_) {}
|
} catch (_) {}
|
||||||
try { _initControlKit(container); } catch (_) {}
|
try { _initControlKit(container); } catch (_) {}
|
||||||
let _lastLayoutDesktop = null;
|
let _lastLayoutDesktop = null;
|
||||||
@@ -11824,14 +11792,6 @@ function _buildEditor(container) {
|
|||||||
// Keyboard shortcuts — full implementation in
|
// Keyboard shortcuts — full implementation in
|
||||||
// editor/keyboard-shortcuts.js.
|
// editor/keyboard-shortcuts.js.
|
||||||
wireKeyboardShortcuts({
|
wireKeyboardShortcuts({
|
||||||
openLayerAdjustment: (type) => {
|
|
||||||
const layer = activeLayer();
|
|
||||||
if (!layer || !layer.canvas || layer.kind === 'adjustment') {
|
|
||||||
uiModule?.showToast('Select an image layer first');
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
_openAdjPopup(layer, type, document.getElementById('ge-image-menu-btn'));
|
|
||||||
},
|
|
||||||
fillSelection: _doFillSelection,
|
fillSelection: _doFillSelection,
|
||||||
copyPixelsToClipboard: _copyPixelsToClipboard,
|
copyPixelsToClipboard: _copyPixelsToClipboard,
|
||||||
toolbar, toolKeyMap: _toolKeyMap,
|
toolbar, toolKeyMap: _toolKeyMap,
|
||||||
@@ -12196,9 +12156,7 @@ function _buildEditor(container) {
|
|||||||
out.getContext('2d').drawImage(layer.canvas, off.x, off.y);
|
out.getContext('2d').drawImage(layer.canvas, off.x, off.y);
|
||||||
return out;
|
return out;
|
||||||
};
|
};
|
||||||
const _aiEditCanvas = async (endpoint, payload = {}, { keepNaturalSize = true } = {}) => {
|
const _aiEditCanvas = async (endpoint, payload = {}, { keepNaturalSize = false } = {}) => {
|
||||||
await waitForImagePreferences();
|
|
||||||
if (endpoint === '/api/image/inpaint') payload = { ...payload, _use_image_settings: true };
|
|
||||||
const flat = _aiEditSourceCanvas();
|
const flat = _aiEditSourceCanvas();
|
||||||
const image = flat.toDataURL('image/png').split(',')[1];
|
const image = flat.toDataURL('image/png').split(',')[1];
|
||||||
const requestId = 'qe-' + Date.now().toString(36) + '-' + Math.random().toString(36).slice(2, 8);
|
const requestId = 'qe-' + Date.now().toString(36) + '-' + Math.random().toString(36).slice(2, 8);
|
||||||
@@ -12209,7 +12167,7 @@ function _buildEditor(container) {
|
|||||||
} else {
|
} else {
|
||||||
_startAiProgress(requestId);
|
_startAiProgress(requestId);
|
||||||
}
|
}
|
||||||
const body = JSON.stringify({ image, width: flat.width, height: flat.height, request_id: requestId, ...payload });
|
const body = JSON.stringify({ image, request_id: requestId, ...payload });
|
||||||
// The image servers occasionally reject a request transiently (503 while a
|
// The image servers occasionally reject a request transiently (503 while a
|
||||||
// model swaps, 502/504 gateways, dropped connections). Retry those a couple
|
// model swaps, 502/504 gateways, dropped connections). Retry those a couple
|
||||||
// of times with backoff so a hiccup doesn't silently leave the canvas
|
// of times with backoff so a hiccup doesn't silently leave the canvas
|
||||||
@@ -12267,7 +12225,7 @@ function _buildEditor(container) {
|
|||||||
c.height = state.imgHeight;
|
c.height = state.imgHeight;
|
||||||
}
|
}
|
||||||
c.getContext('2d').drawImage(img, 0, 0, c.width, c.height);
|
c.getContext('2d').drawImage(img, 0, 0, c.width, c.height);
|
||||||
return { canvas: c, dataUrl: c.toDataURL('image/png'), fullFrame: data.full_frame === true };
|
return { canvas: c, dataUrl: c.toDataURL('image/png') };
|
||||||
} finally {
|
} finally {
|
||||||
if (_timer) clearTimeout(_timer);
|
if (_timer) clearTimeout(_timer);
|
||||||
if (_aiEditAbort === _ctl) _aiEditAbort = null;
|
if (_aiEditAbort === _ctl) _aiEditAbort = null;
|
||||||
@@ -12276,17 +12234,12 @@ function _buildEditor(container) {
|
|||||||
};
|
};
|
||||||
const _placeResultLayer = (canvas, name, maskCanvas) => {
|
const _placeResultLayer = (canvas, name, maskCanvas) => {
|
||||||
_saveState(`AI: ${name}`);
|
_saveState(`AI: ${name}`);
|
||||||
const geometry = imageResultGeometry(canvas.width, canvas.height, state.imgWidth, state.imgHeight);
|
const layer = createLayer(name, state.imgWidth, state.imgHeight);
|
||||||
const layer = createLayer(name, geometry.width, geometry.height);
|
layer.ctx.drawImage(canvas, 0, 0);
|
||||||
const offset = { x: geometry.offsetX, y: geometry.offsetY };
|
|
||||||
state.layerOffsets.set(layer.id, offset);
|
|
||||||
layer.ctx.imageSmoothingEnabled = true;
|
|
||||||
layer.ctx.imageSmoothingQuality = 'high';
|
|
||||||
layer.ctx.drawImage(canvas, geometry.drawX, geometry.drawY, geometry.drawWidth, geometry.drawHeight);
|
|
||||||
if (maskCanvas) {
|
if (maskCanvas) {
|
||||||
layer.ctx.save();
|
layer.ctx.save();
|
||||||
layer.ctx.globalCompositeOperation = 'destination-in';
|
layer.ctx.globalCompositeOperation = 'destination-in';
|
||||||
layer.ctx.drawImage(maskCanvas, -offset.x, -offset.y);
|
layer.ctx.drawImage(maskCanvas, 0, 0);
|
||||||
layer.ctx.restore();
|
layer.ctx.restore();
|
||||||
}
|
}
|
||||||
state.layers.push(layer);
|
state.layers.push(layer);
|
||||||
@@ -12296,24 +12249,13 @@ function _buildEditor(container) {
|
|||||||
_renderLayerPanel();
|
_renderLayerPanel();
|
||||||
return layer.id;
|
return layer.id;
|
||||||
};
|
};
|
||||||
const _generateImage = createImageGenerationAction({
|
|
||||||
formatError: _generationErrorMessage,
|
|
||||||
getDocument: () => ({
|
|
||||||
key: state.editorOpen ? `${state.editorSessionToken}:${state.documentId}` : null,
|
|
||||||
width: state.imgWidth, height: state.imgHeight, mask: state.wandMask,
|
|
||||||
}),
|
|
||||||
getSignal: () => _quickEditActiveJob?.controller?.signal,
|
|
||||||
onProgress: seconds => _setAiCommandStatus(`Generating an image… ${seconds}s`, 'running'),
|
|
||||||
placeResult: _placeResultLayer,
|
|
||||||
});
|
|
||||||
const _aiInpaintSelection = async (prompt) => {
|
const _aiInpaintSelection = async (prompt) => {
|
||||||
if (!state.wandMask) throw new Error('make a selection first');
|
if (!state.wandMask) throw new Error('make a selection first');
|
||||||
const layer = activeLayer() || _activeParentLayer();
|
const layer = activeLayer() || _activeParentLayer();
|
||||||
if (!layer) throw new Error('no layer');
|
if (!layer) throw new Error('no layer');
|
||||||
const mask = state.wandMask.toDataURL('image/png').split(',')[1];
|
const mask = state.wandMask.toDataURL('image/png').split(',')[1];
|
||||||
const selection = state.wandMask;
|
const { canvas } = await _aiEditCanvas('/api/image/inpaint', { mask, prompt: prompt || '', steps: 4 });
|
||||||
const { canvas, fullFrame } = await _aiEditCanvas('/api/image/inpaint', { mask, prompt: prompt || '', steps: 4 });
|
return _placeResultLayer(canvas, 'Generative fill', state.wandMask);
|
||||||
return _placeResultLayer(canvas, 'Generative fill', fullFrame ? null : selection);
|
|
||||||
};
|
};
|
||||||
// Replace the background/scene while keeping the subject: the server derives
|
// Replace the background/scene while keeping the subject: the server derives
|
||||||
// the subject mask, repaints the inverse (new background), harmonises colour
|
// the subject mask, repaints the inverse (new background), harmonises colour
|
||||||
@@ -12606,7 +12548,7 @@ function _buildEditor(container) {
|
|||||||
'layers.delete': { description: 'Delete layers (defaults to selection).', run: (p) => { if (p && (p.id || p.index || p.name)) _controlSelect(_controlFindLayer(p)); _layerPanelRenderer.deleteSelectedLayers?.(); return true; } },
|
'layers.delete': { description: 'Delete layers (defaults to selection).', run: (p) => { if (p && (p.id || p.index || p.name)) _controlSelect(_controlFindLayer(p)); _layerPanelRenderer.deleteSelectedLayers?.(); return true; } },
|
||||||
'layers.setOpacity': { description: 'Set a layer opacity (0-1).', run: (p) => { const l = _controlFindLayer(p) || activeLayer(); if (!l) throw new Error('layer not found'); _saveState(`Change opacity of ${l.name}`); l.opacity = Math.max(0, Math.min(1, Number(p.opacity))); composite(); _renderLayerPanel(); return l.id; } },
|
'layers.setOpacity': { description: 'Set a layer opacity (0-1).', run: (p) => { const l = _controlFindLayer(p) || activeLayer(); if (!l) throw new Error('layer not found'); _saveState(`Change opacity of ${l.name}`); l.opacity = Math.max(0, Math.min(1, Number(p.opacity))); composite(); _renderLayerPanel(); return l.id; } },
|
||||||
'layers.setBlend': { description: 'Set a layer blend mode.', run: (p) => { const l = _controlFindLayer(p) || activeLayer(); if (!l) throw new Error('layer not found'); _saveState(`Change blend mode of ${l.name}`); l.blendMode = p.blendMode || p.mode || 'source-over'; composite(); _renderLayerPanel(); return l.id; } },
|
'layers.setBlend': { description: 'Set a layer blend mode.', run: (p) => { const l = _controlFindLayer(p) || activeLayer(); if (!l) throw new Error('layer not found'); _saveState(`Change blend mode of ${l.name}`); l.blendMode = p.blendMode || p.mode || 'source-over'; composite(); _renderLayerPanel(); return l.id; } },
|
||||||
'layers.placeImage': { description: 'Place an image (data URL) as a NEW layer, masked to the current selection when one exists; never overwrites the original. Instruction mode (mode:"instruction" or differenceMask:true) keeps untouched areas pixel-identical via a difference map. {dataUrl,name,masked,blend,opacity,x,y,w,h,mode,differenceMask,diffThreshold,diffGrow,diffFeather}.', run: (p) => new Promise((resolve, reject) => { if (!p || !p.dataUrl) { reject(new Error('dataUrl required')); return; } const img = new Image(); img.onload = () => { try { _saveState(`Place ${p.name || 'image'}`); const dw = state.imgWidth, dh = state.imgHeight; const useDiff = p.nativeSize !== true && ((p.differenceMask === true) || String(p.mode || '').toLowerCase() === 'instruction'); const beforeSnap = useDiff ? flatten() : null; const nativeSize = p.nativeSize === true && !useDiff; const geometry = imageResultGeometry(img.naturalWidth, img.naturalHeight, dw, dh); const layer = createLayer(p.name || 'Placed image', nativeSize ? geometry.width : dw, nativeSize ? geometry.height : dh); const nativeOffset = { x: geometry.offsetX, y: geometry.offsetY }; if (nativeSize) state.layerOffsets.set(layer.id, nativeOffset); let tw = Number(p.w), th = Number(p.h); if (!(tw > 0) || !(th > 0)) { if (nativeSize) { tw = geometry.drawWidth; th = geometry.drawHeight; } else if (useDiff) { tw = dw; th = dh; } else { const scale = Math.min(dw / img.naturalWidth, dh / img.naturalHeight); tw = Math.max(1, Math.round(img.naturalWidth * scale)); th = Math.max(1, Math.round(img.naturalHeight * scale)); } } let tx = Number(p.x), ty = Number(p.y); if (!Number.isFinite(tx)) tx = nativeSize ? geometry.drawX : useDiff ? 0 : Math.round((dw - tw) / 2); if (!Number.isFinite(ty)) ty = nativeSize ? geometry.drawY : useDiff ? 0 : Math.round((dh - th) / 2); layer.ctx.imageSmoothingEnabled = true; layer.ctx.imageSmoothingQuality = 'high'; layer.ctx.drawImage(img, tx, ty, tw, th); if (p.masked !== false && state.wandMask) { layer.ctx.save(); layer.ctx.globalCompositeOperation = 'destination-in'; layer.ctx.drawImage(state.wandMask, nativeSize ? -nativeOffset.x : 0, nativeSize ? -nativeOffset.y : 0); layer.ctx.restore(); } else if (useDiff && beforeSnap) { const diff = _differenceMaskCanvas(beforeSnap, layer.canvas, { threshold: p.diffThreshold, grow: p.diffGrow, feather: p.diffFeather, edgeExcludePx: 0 }); const explicitEdge = p.edgeExcludePx != null ? Number(p.edgeExcludePx) : null; const sceneReplace = diff.changedFraction > 0.5; const edgeExclude = sceneReplace ? 0 : (explicitEdge != null ? explicitEdge : 3); if (edgeExclude > 0) _clearMaskEdgeBand(diff.mask, edgeExclude); layer.ctx.save(); layer.ctx.globalCompositeOperation = 'destination-in'; layer.ctx.drawImage(diff.mask, 0, 0); layer.ctx.restore(); if (sceneReplace) _edgeFillFromInterior(layer.ctx, dw, dh, 3); } if (p.blend) layer.blendMode = p.blend; if (p.opacity != null) layer.opacity = Math.max(0, Math.min(1, Number(p.opacity))); state.layers.push(layer); state.activeLayerId = layer.id; state.selectedLayerIds = [layer.id]; _renderLayerPanel(); composite(); resolve(layer.id); } catch (e) { reject(e); } }; img.onerror = () => reject(new Error('image load failed')); img.src = p.dataUrl; }) },
|
'layers.placeImage': { description: 'Place an image (data URL) as a NEW layer, masked to the current selection when one exists; never overwrites the original. Instruction mode (mode:"instruction" or differenceMask:true) keeps untouched areas pixel-identical via a difference map. {dataUrl,name,masked,blend,opacity,x,y,w,h,mode,differenceMask,diffThreshold,diffGrow,diffFeather}.', run: (p) => new Promise((resolve, reject) => { if (!p || !p.dataUrl) { reject(new Error('dataUrl required')); return; } const img = new Image(); img.onload = () => { try { _saveState(`Place ${p.name || 'image'}`); const dw = state.imgWidth, dh = state.imgHeight; const useDiff = (p.differenceMask === true) || String(p.mode || '').toLowerCase() === 'instruction'; const beforeSnap = useDiff ? flatten() : null; const layer = createLayer(p.name || 'Placed image', dw, dh); let tw = Number(p.w), th = Number(p.h); if (!(tw > 0) || !(th > 0)) { if (useDiff) { tw = dw; th = dh; } else { const scale = Math.min(dw / img.naturalWidth, dh / img.naturalHeight); tw = Math.max(1, Math.round(img.naturalWidth * scale)); th = Math.max(1, Math.round(img.naturalHeight * scale)); } } let tx = Number(p.x), ty = Number(p.y); if (!Number.isFinite(tx)) tx = useDiff ? 0 : Math.round((dw - tw) / 2); if (!Number.isFinite(ty)) ty = useDiff ? 0 : Math.round((dh - th) / 2); layer.ctx.imageSmoothingEnabled = true; layer.ctx.imageSmoothingQuality = 'high'; layer.ctx.drawImage(img, tx, ty, tw, th); if (p.masked !== false && state.wandMask) { layer.ctx.save(); layer.ctx.globalCompositeOperation = 'destination-in'; layer.ctx.drawImage(state.wandMask, 0, 0); layer.ctx.restore(); } else if (useDiff && beforeSnap) { const diff = _differenceMaskCanvas(beforeSnap, layer.canvas, { threshold: p.diffThreshold, grow: p.diffGrow, feather: p.diffFeather, edgeExcludePx: 0 }); const explicitEdge = p.edgeExcludePx != null ? Number(p.edgeExcludePx) : null; const sceneReplace = diff.changedFraction > 0.5; const edgeExclude = sceneReplace ? 0 : (explicitEdge != null ? explicitEdge : 3); if (edgeExclude > 0) _clearMaskEdgeBand(diff.mask, edgeExclude); layer.ctx.save(); layer.ctx.globalCompositeOperation = 'destination-in'; layer.ctx.drawImage(diff.mask, 0, 0); layer.ctx.restore(); if (sceneReplace) _edgeFillFromInterior(layer.ctx, dw, dh, 3); } if (p.blend) layer.blendMode = p.blend; if (p.opacity != null) layer.opacity = Math.max(0, Math.min(1, Number(p.opacity))); state.layers.push(layer); state.activeLayerId = layer.id; state.selectedLayerIds = [layer.id]; _renderLayerPanel(); composite(); resolve(layer.id); } catch (e) { reject(e); } }; img.onerror = () => reject(new Error('image load failed')); img.src = p.dataUrl; }) },
|
||||||
'layers.addMaskFromSelection': { description: 'Add a layer mask from the current selection (reveals the selection). {invert}.', run: (p) => { const parent = _activeParentLayer(); if (!parent) throw new Error('no layer'); _saveState('Add layer mask'); const mask = _ensureActiveMaskLayer(); if (!mask) throw new Error('could not create mask'); const w = mask.canvas.width, h = mask.canvas.height; const mctx = mask.ctx; const off = state.layerOffsets.get(parent.id) || { x: 0, y: 0 }; const ox = -(off.x || 0), oy = -(off.y || 0); mctx.globalCompositeOperation = 'source-over'; mctx.clearRect(0, 0, w, h); if (state.wandMask) { if (p && p.invert) { mctx.fillStyle = '#fff'; mctx.fillRect(0, 0, w, h); mctx.globalCompositeOperation = 'destination-out'; mctx.drawImage(state.wandMask, ox, oy); mctx.globalCompositeOperation = 'source-over'; } else { mctx.drawImage(state.wandMask, ox, oy); } } else { mctx.fillStyle = (p && p.invert) ? '#000' : '#fff'; mctx.fillRect(0, 0, w, h); } composite(); _renderLayerPanel(); return mask.id; } },
|
'layers.addMaskFromSelection': { description: 'Add a layer mask from the current selection (reveals the selection). {invert}.', run: (p) => { const parent = _activeParentLayer(); if (!parent) throw new Error('no layer'); _saveState('Add layer mask'); const mask = _ensureActiveMaskLayer(); if (!mask) throw new Error('could not create mask'); const w = mask.canvas.width, h = mask.canvas.height; const mctx = mask.ctx; const off = state.layerOffsets.get(parent.id) || { x: 0, y: 0 }; const ox = -(off.x || 0), oy = -(off.y || 0); mctx.globalCompositeOperation = 'source-over'; mctx.clearRect(0, 0, w, h); if (state.wandMask) { if (p && p.invert) { mctx.fillStyle = '#fff'; mctx.fillRect(0, 0, w, h); mctx.globalCompositeOperation = 'destination-out'; mctx.drawImage(state.wandMask, ox, oy); mctx.globalCompositeOperation = 'source-over'; } else { mctx.drawImage(state.wandMask, ox, oy); } } else { mctx.fillStyle = (p && p.invert) ? '#000' : '#fff'; mctx.fillRect(0, 0, w, h); } composite(); _renderLayerPanel(); return mask.id; } },
|
||||||
'layers.removeMask': { description: 'Remove the active layer mask (does not affect pixels).', run: () => { const parent = _activeParentLayer(); if (!parent || !parent.masks || !parent.masks.length) throw new Error('no mask'); _saveState('Remove layer mask'); parent.masks = parent.masks.filter((m) => m.id !== parent.activeMaskId); parent.activeMaskId = parent.masks[0]?.id || null; composite(); _renderLayerPanel(); return true; } },
|
'layers.removeMask': { description: 'Remove the active layer mask (does not affect pixels).', run: () => { const parent = _activeParentLayer(); if (!parent || !parent.masks || !parent.masks.length) throw new Error('no mask'); _saveState('Remove layer mask'); parent.masks = parent.masks.filter((m) => m.id !== parent.activeMaskId); parent.activeMaskId = parent.masks[0]?.id || null; composite(); _renderLayerPanel(); return true; } },
|
||||||
'layers.applyMask': { description: 'Bake the active layer mask into the layer alpha and remove the mask.', run: () => { const parent = _activeParentLayer(); const mask = _getActiveMaskLayer(); if (!parent || !parent.ctx || !mask) throw new Error('no mask to apply'); _saveState('Apply layer mask'); _bakeMaskIntoLayer(parent.ctx, mask.canvas, 0, 0); parent.masks = parent.masks.filter((m) => m.id !== mask.id); parent.activeMaskId = parent.masks[0]?.id || null; composite(); _renderLayerPanel(); return true; } },
|
'layers.applyMask': { description: 'Bake the active layer mask into the layer alpha and remove the mask.', run: () => { const parent = _activeParentLayer(); const mask = _getActiveMaskLayer(); if (!parent || !parent.ctx || !mask) throw new Error('no mask to apply'); _saveState('Apply layer mask'); _bakeMaskIntoLayer(parent.ctx, mask.canvas, 0, 0); parent.masks = parent.masks.filter((m) => m.id !== mask.id); parent.activeMaskId = parent.masks[0]?.id || null; composite(); _renderLayerPanel(); return true; } },
|
||||||
@@ -12622,7 +12564,7 @@ function _buildEditor(container) {
|
|||||||
'ai.removeObject': { description: 'Remove the selected object and fill naturally. {prompt}.', run: (p) => _aiInpaintSelection((p && p.prompt) || 'remove the object, fill the area naturally') },
|
'ai.removeObject': { description: 'Remove the selected object and fill naturally. {prompt}.', run: (p) => _aiInpaintSelection((p && p.prompt) || 'remove the object, fill the area naturally') },
|
||||||
'ai.harmonize': { description: 'Harmonize the layer/selection lighting with the rest of the image; result is a NEW layer. {strength}.', run: async (p) => { const { canvas } = await _aiEditCanvas('/api/image/harmonize', { strength: Number(p && p.strength) || 0.4 }); return _placeResultLayer(canvas, 'Harmonized', state.wandMask || null); } },
|
'ai.harmonize': { description: 'Harmonize the layer/selection lighting with the rest of the image; result is a NEW layer. {strength}.', run: async (p) => { const { canvas } = await _aiEditCanvas('/api/image/harmonize', { strength: Number(p && p.strength) || 0.4 }); return _placeResultLayer(canvas, 'Harmonized', state.wandMask || null); } },
|
||||||
'ai.generativeUpscale': { description: 'Upscale the image 2x/4x; result is a NEW layer. Falls back to a high-quality canvas upscale labelled "basic upscale" when no Real-ESRGAN model is served. {scale}.', run: async (p) => { const scale = Number(p && p.scale) === 4 ? 4 : 2; try { const { canvas } = await _aiEditCanvas('/api/image/upscale-local', { scale }); return _placeResultLayer(canvas, `Upscaled ${scale}x`, null); } catch (e) { const src = flatten(); const c = document.createElement('canvas'); c.width = Math.max(1, Math.round(src.width * scale)); c.height = Math.max(1, Math.round(src.height * scale)); const cx = c.getContext('2d'); cx.imageSmoothingEnabled = true; cx.imageSmoothingQuality = 'high'; cx.drawImage(src, 0, 0, c.width, c.height); _saveState('Upscale (basic)'); _resizeCanvasDocument(state, c.width, c.height, { anchorX: 0, anchorY: 0 }); const layerId = _placeResultLayer(c, `Upscaled ${scale}x (basic upscale)`, null); return { layerId, scale, method: 'basic', reason: String(e && e.message || e) }; } } },
|
'ai.generativeUpscale': { description: 'Upscale the image 2x/4x; result is a NEW layer. Falls back to a high-quality canvas upscale labelled "basic upscale" when no Real-ESRGAN model is served. {scale}.', run: async (p) => { const scale = Number(p && p.scale) === 4 ? 4 : 2; try { const { canvas } = await _aiEditCanvas('/api/image/upscale-local', { scale }); return _placeResultLayer(canvas, `Upscaled ${scale}x`, null); } catch (e) { const src = flatten(); const c = document.createElement('canvas'); c.width = Math.max(1, Math.round(src.width * scale)); c.height = Math.max(1, Math.round(src.height * scale)); const cx = c.getContext('2d'); cx.imageSmoothingEnabled = true; cx.imageSmoothingQuality = 'high'; cx.drawImage(src, 0, 0, c.width, c.height); _saveState('Upscale (basic)'); _resizeCanvasDocument(state, c.width, c.height, { anchorX: 0, anchorY: 0 }); const layerId = _placeResultLayer(c, `Upscaled ${scale}x (basic upscale)`, null); return { layerId, scale, method: 'basic', reason: String(e && e.message || e) }; } } },
|
||||||
'ai.generateImage': { description: 'Generate an image with the saved generation model and fit it proportionally as a new layer. {prompt,width,height}.', run: (p) => _generateImage(p || {}) },
|
'ai.generateImage': { description: 'Generate an image from a prompt (current canvas as reference) and place it as a NEW layer. {prompt,width,height}.', run: async (p) => { const endpoint = (typeof window !== 'undefined' && window.geGenerateImageEndpoint) || '/api/image/generate'; const reference = flatten().toDataURL('image/png').split(',')[1]; const _pickedModel = (() => { try { const v = localStorage.getItem('ge-ai-command-model') || ''; return v.includes('::') ? v.split('::').pop() : ''; } catch (_) { return ''; } })(); const body = { prompt: (p && p.prompt) || '', width: Math.max(1, Math.round(Number(p && p.width) || state.imgWidth)), height: Math.max(1, Math.round(Number(p && p.height) || state.imgHeight)), reference, ...(_pickedModel ? { model: _pickedModel } : {}) }; let _gres = null; try { _gres = await fetch(endpoint, { method: 'POST', credentials: 'same-origin', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(body) }); } catch (_e) { throw new Error(_generationErrorMessage({ network: true, detail: (_e && _e.message) || 'network error' })); } const res = _gres; if (!res.ok) { let _gd = res.statusText; try { const e = await res.json(); _gd = e.detail || e.error || _gd; } catch (_) {} throw new Error(_generationErrorMessage({ status: res.status, detail: _gd })); } const data = await res.json(); if (data.error) throw new Error(_generationErrorMessage({ detail: data.error })); const b64 = data.image || (Array.isArray(data.images) && data.images[0]); if (!b64) throw new Error('No image returned'); try { window.__geGenerationDisabled = 0; } catch (_) {} const img = await _decodeAIImage(b64, _aiSignal); const c = document.createElement('canvas'); c.width = state.imgWidth; c.height = state.imgHeight; c.getContext('2d').drawImage(img, 0, 0, state.imgWidth, state.imgHeight); return _placeResultLayer(c, 'Generated', state.wandMask || null); } },
|
||||||
'ai.generativeExpand': { description: 'Generatively expand the canvas to width×height; only the new border is inpainted, the original pixels are kept pixel-identical, and the seam is feathered. {width,height,anchor,prompt}.', run: async (p) => { const tw = Math.max(1, Math.round(Number(p && p.width) || state.imgWidth)); const th = Math.max(1, Math.round(Number(p && p.height) || state.imgHeight)); if (tw <= state.imgWidth && th <= state.imgHeight) throw new Error('target must be larger than the current canvas'); const anchor = (p && p.anchor) || 'center'; const anchorX = anchor === 'left' ? 0 : anchor === 'right' ? 1 : 0.5; const anchorY = anchor === 'top' ? 0 : anchor === 'bottom' ? 1 : 0.5; _saveState('Generative expand'); const steps = Math.max(1, Math.round(Number(p && p.steps) || 4)); const sw = state.imgWidth; const sh = state.imgHeight; _resizeCanvasDocument(state, tw, th, { anchorX, anchorY }); const flat = flatten(); const b = _contentBounds(); const mask = document.createElement('canvas'); mask.width = tw; mask.height = th; const mctx = mask.getContext('2d'); mctx.fillStyle = '#ffffff'; mctx.fillRect(0, 0, tw, th); const prefill = document.createElement('canvas'); prefill.width = tw; prefill.height = th; const pctx = prefill.getContext('2d'); pctx.imageSmoothingEnabled = true; pctx.drawImage(flat, 0, 0, tw, th); if (b) { mctx.clearRect(b.x, b.y, b.w, b.h); const bx = b.x, by = b.y, bw = b.w, bh = b.h, rx = bx + bw, by2 = by + bh; if (bx > 0) pctx.drawImage(flat, bx, by, 1, bh, 0, by, bx, bh); if (rx < tw) pctx.drawImage(flat, rx - 1, by, 1, bh, rx, by, tw - rx, bh); if (by > 0) pctx.drawImage(flat, bx, by, bw, 1, bx, 0, bw, by); if (by2 < th) pctx.drawImage(flat, bx, by2 - 1, bw, 1, bx, by2, bw, th - by2); if (bx > 0 && by > 0) pctx.drawImage(flat, bx, by, 1, 1, 0, 0, bx, by); if (rx < tw && by > 0) pctx.drawImage(flat, rx - 1, by, 1, 1, rx, 0, tw - rx, by); if (bx > 0 && by2 < th) pctx.drawImage(flat, bx, by2 - 1, 1, 1, 0, by2, bx, th - by2); if (rx < tw && by2 < th) pctx.drawImage(flat, rx - 1, by2 - 1, 1, 1, rx, by2, tw - rx, th - by2); } else { const tx = Math.round((tw - sw) * anchorX); const ty = Math.round((th - sh) * anchorY); mctx.clearRect(tx, ty, sw, sh); } const image = prefill.toDataURL('image/png').split(',')[1]; const maskB64 = mask.toDataURL('image/png').split(',')[1]; const res = await fetch('/api/image/inpaint', { method: 'POST', credentials: 'same-origin', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ image, mask: maskB64, prompt: (p && p.prompt) || 'extend the scene seamlessly, continuing the surrounding content and lighting naturally', steps }) }); if (!res.ok) { let err = res.statusText; try { const e = await res.json(); err = e.detail || e.error || err; } catch (_) {} throw new Error(err); } const data = await res.json(); if (data.error) throw new Error(data.error); if (!data.image) throw new Error('No image returned'); const img = await _decodeAIImage(data.image, _aiSignal); const seam = Math.max(12, Math.round(Math.min(tw, th) * 0.02)); let blendMask = _growMaskCanvas(mask, seam, true); blendMask = _featherMaskCanvas((cw, ch) => { const c = document.createElement('canvas'); c.width = cw; c.height = ch; return c; }, blendMask, Math.round(seam * 1.4)); const layer = createLayer('Generative expand', tw, th); layer.ctx.imageSmoothingEnabled = true; layer.ctx.imageSmoothingQuality = 'high'; layer.ctx.drawImage(img, 0, 0, tw, th); if (b) { const edge = _edgeBandAverage(flat, b); if (edge) { layer.ctx.save(); layer.ctx.globalAlpha = 0.14; layer.ctx.fillStyle = `rgb(${edge[0]}, ${edge[1]}, ${edge[2]})`; layer.ctx.fillRect(0, 0, tw, th); layer.ctx.restore(); } } _bakeMaskIntoLayer(layer.ctx, blendMask); state.layers.push(layer); state.activeLayerId = layer.id; state.selectedLayerIds = [layer.id]; composite(); _renderLayerPanel(); return { width: tw, height: th, layerId: layer.id, seam, steps }; } },
|
'ai.generativeExpand': { description: 'Generatively expand the canvas to width×height; only the new border is inpainted, the original pixels are kept pixel-identical, and the seam is feathered. {width,height,anchor,prompt}.', run: async (p) => { const tw = Math.max(1, Math.round(Number(p && p.width) || state.imgWidth)); const th = Math.max(1, Math.round(Number(p && p.height) || state.imgHeight)); if (tw <= state.imgWidth && th <= state.imgHeight) throw new Error('target must be larger than the current canvas'); const anchor = (p && p.anchor) || 'center'; const anchorX = anchor === 'left' ? 0 : anchor === 'right' ? 1 : 0.5; const anchorY = anchor === 'top' ? 0 : anchor === 'bottom' ? 1 : 0.5; _saveState('Generative expand'); const steps = Math.max(1, Math.round(Number(p && p.steps) || 4)); const sw = state.imgWidth; const sh = state.imgHeight; _resizeCanvasDocument(state, tw, th, { anchorX, anchorY }); const flat = flatten(); const b = _contentBounds(); const mask = document.createElement('canvas'); mask.width = tw; mask.height = th; const mctx = mask.getContext('2d'); mctx.fillStyle = '#ffffff'; mctx.fillRect(0, 0, tw, th); const prefill = document.createElement('canvas'); prefill.width = tw; prefill.height = th; const pctx = prefill.getContext('2d'); pctx.imageSmoothingEnabled = true; pctx.drawImage(flat, 0, 0, tw, th); if (b) { mctx.clearRect(b.x, b.y, b.w, b.h); const bx = b.x, by = b.y, bw = b.w, bh = b.h, rx = bx + bw, by2 = by + bh; if (bx > 0) pctx.drawImage(flat, bx, by, 1, bh, 0, by, bx, bh); if (rx < tw) pctx.drawImage(flat, rx - 1, by, 1, bh, rx, by, tw - rx, bh); if (by > 0) pctx.drawImage(flat, bx, by, bw, 1, bx, 0, bw, by); if (by2 < th) pctx.drawImage(flat, bx, by2 - 1, bw, 1, bx, by2, bw, th - by2); if (bx > 0 && by > 0) pctx.drawImage(flat, bx, by, 1, 1, 0, 0, bx, by); if (rx < tw && by > 0) pctx.drawImage(flat, rx - 1, by, 1, 1, rx, 0, tw - rx, by); if (bx > 0 && by2 < th) pctx.drawImage(flat, bx, by2 - 1, 1, 1, 0, by2, bx, th - by2); if (rx < tw && by2 < th) pctx.drawImage(flat, rx - 1, by2 - 1, 1, 1, rx, by2, tw - rx, th - by2); } else { const tx = Math.round((tw - sw) * anchorX); const ty = Math.round((th - sh) * anchorY); mctx.clearRect(tx, ty, sw, sh); } const image = prefill.toDataURL('image/png').split(',')[1]; const maskB64 = mask.toDataURL('image/png').split(',')[1]; const res = await fetch('/api/image/inpaint', { method: 'POST', credentials: 'same-origin', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ image, mask: maskB64, prompt: (p && p.prompt) || 'extend the scene seamlessly, continuing the surrounding content and lighting naturally', steps }) }); if (!res.ok) { let err = res.statusText; try { const e = await res.json(); err = e.detail || e.error || err; } catch (_) {} throw new Error(err); } const data = await res.json(); if (data.error) throw new Error(data.error); if (!data.image) throw new Error('No image returned'); const img = await _decodeAIImage(data.image, _aiSignal); const seam = Math.max(12, Math.round(Math.min(tw, th) * 0.02)); let blendMask = _growMaskCanvas(mask, seam, true); blendMask = _featherMaskCanvas((cw, ch) => { const c = document.createElement('canvas'); c.width = cw; c.height = ch; return c; }, blendMask, Math.round(seam * 1.4)); const layer = createLayer('Generative expand', tw, th); layer.ctx.imageSmoothingEnabled = true; layer.ctx.imageSmoothingQuality = 'high'; layer.ctx.drawImage(img, 0, 0, tw, th); if (b) { const edge = _edgeBandAverage(flat, b); if (edge) { layer.ctx.save(); layer.ctx.globalAlpha = 0.14; layer.ctx.fillStyle = `rgb(${edge[0]}, ${edge[1]}, ${edge[2]})`; layer.ctx.fillRect(0, 0, tw, th); layer.ctx.restore(); } } _bakeMaskIntoLayer(layer.ctx, blendMask); state.layers.push(layer); state.activeLayerId = layer.id; state.selectedLayerIds = [layer.id]; composite(); _renderLayerPanel(); return { width: tw, height: th, layerId: layer.id, seam, steps }; } },
|
||||||
'layers.rename': { description: 'Rename a layer {id|index|name, name|newName}.', run: (p) => { const l = _controlFindLayer(p) || activeLayer(); if (!l) throw new Error('layer not found'); const next = String(p.name || p.newName || '').trim(); if (!next) throw new Error('name required'); _saveState('Rename layer'); l.name = next; _renderLayerPanel(); return l.id; } },
|
'layers.rename': { description: 'Rename a layer {id|index|name, name|newName}.', run: (p) => { const l = _controlFindLayer(p) || activeLayer(); if (!l) throw new Error('layer not found'); const next = String(p.name || p.newName || '').trim(); if (!next) throw new Error('name required'); _saveState('Rename layer'); l.name = next; _renderLayerPanel(); return l.id; } },
|
||||||
'layers.setVisibility': { description: 'Show/hide a layer {id|index|name, visible}.', run: (p) => { const l = _controlFindLayer(p) || activeLayer(); if (!l) throw new Error('layer not found'); _saveState('Toggle layer visibility'); l.visible = (p.visible === undefined) ? !l.visible : !!p.visible; composite(); _renderLayerPanel(); return l.visible; } },
|
'layers.setVisibility': { description: 'Show/hide a layer {id|index|name, visible}.', run: (p) => { const l = _controlFindLayer(p) || activeLayer(); if (!l) throw new Error('layer not found'); _saveState('Toggle layer visibility'); l.visible = (p.visible === undefined) ? !l.visible : !!p.visible; composite(); _renderLayerPanel(); return l.visible; } },
|
||||||
@@ -12867,10 +12809,6 @@ function _buildEditor(container) {
|
|||||||
endpoint: window.geEditorAgentEndpoint || '/api/editor/agent',
|
endpoint: window.geEditorAgentEndpoint || '/api/editor/agent',
|
||||||
getSession: () => agentBridgeSession,
|
getSession: () => agentBridgeSession,
|
||||||
getModel: () => _getSelectedQuickEditModel(),
|
getModel: () => _getSelectedQuickEditModel(),
|
||||||
getGenerationModel: () => {
|
|
||||||
const value = document.getElementById('ge-ai-model')?.value || '';
|
|
||||||
return value.includes('::') ? value.slice(value.indexOf('::') + 2) : '';
|
|
||||||
},
|
|
||||||
getAgentModel: () => _getSelectedQuickEditAgentModel(),
|
getAgentModel: () => _getSelectedQuickEditAgentModel(),
|
||||||
});
|
});
|
||||||
} catch (e) { console.error('[ge-editor] agent init failed', e); }
|
} catch (e) { console.error('[ge-editor] agent init failed', e); }
|
||||||
@@ -13970,12 +13908,6 @@ export function openEditor(imageUrl, imageId, presetSize, displayName, draftId)
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
_finishBlank(size.w, size.h);
|
_finishBlank(size.w, size.h);
|
||||||
}).catch(error => {
|
|
||||||
console.error('[openEditor] New project failed:', error);
|
|
||||||
closeEditor({ force: true });
|
|
||||||
window.dispatchEvent(new CustomEvent('gallery-editor-setup-cancelled'));
|
|
||||||
uiModule.showError(`Could not create project: ${error?.message || 'Canvas initialization failed'}`);
|
|
||||||
throw error;
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -14130,6 +14062,8 @@ export function closeEditor(options = {}) {
|
|||||||
_selectionNudgeTimer = null;
|
_selectionNudgeTimer = null;
|
||||||
_selectionNudgeHistorySaved = false;
|
_selectionNudgeHistorySaved = false;
|
||||||
_precisionGuides = null;
|
_precisionGuides = null;
|
||||||
|
try { _preflight?.destroy(); } catch {}
|
||||||
|
_preflight = null;
|
||||||
try { _aiSettings?.destroy(); } catch {}
|
try { _aiSettings?.destroy(); } catch {}
|
||||||
_aiSettings = null;
|
_aiSettings = null;
|
||||||
try { delete window.geEditorAiSettings; } catch (_) { window.geEditorAiSettings = null; }
|
try { delete window.geEditorAiSettings; } catch (_) { window.geEditorAiSettings = null; }
|
||||||
|
|||||||
@@ -4,14 +4,12 @@
|
|||||||
// them from the same places, so load order and init sequence are untouched.
|
// them from the same places, so load order and init sequence are untouched.
|
||||||
|
|
||||||
import { byId as el } from './dom.js';
|
import { byId as el } from './dom.js';
|
||||||
import { saveImagePreferences as _postSettings, readImagePreferences } from './imagePreferences.js';
|
import { postSettings as _postSettings } from './api.js';
|
||||||
import { modelCaps } from '../editor/ai-models.js';
|
import { modelCaps } from '../editor/ai-models.js';
|
||||||
import { sortModelIds } from '../modelSort.js';
|
import { sortModelIds } from '../modelSort.js';
|
||||||
let preferencesListener = null;
|
|
||||||
|
|
||||||
export async function initImageSettings() {
|
export async function initImageSettings() {
|
||||||
const modelSel = el('set-imgModelSelect');
|
const modelSel = el('set-imgModelSelect');
|
||||||
const editModelSel = el('set-imgEditModelSelect');
|
|
||||||
const qualSel = el('set-imgQualitySelect');
|
const qualSel = el('set-imgQualitySelect');
|
||||||
const msg = el('set-imgSettingsMsg');
|
const msg = el('set-imgSettingsMsg');
|
||||||
const enabledToggle = el('set-imgEnabledToggle');
|
const enabledToggle = el('set-imgEnabledToggle');
|
||||||
@@ -20,41 +18,25 @@ export async function initImageSettings() {
|
|||||||
const endpointsRes = await fetch('/api/model-endpoints', { credentials: 'same-origin' });
|
const endpointsRes = await fetch('/api/model-endpoints', { credentials: 'same-origin' });
|
||||||
const endpoints = await endpointsRes.json();
|
const endpoints = await endpointsRes.json();
|
||||||
const imageModels = new Set();
|
const imageModels = new Set();
|
||||||
const editModels = new Set();
|
|
||||||
(Array.isArray(endpoints) ? endpoints : []).forEach(endpoint => {
|
(Array.isArray(endpoints) ? endpoints : []).forEach(endpoint => {
|
||||||
if (!endpoint.is_enabled || !endpoint.online) return;
|
if (!endpoint.is_enabled || !endpoint.online) return;
|
||||||
(Array.isArray(endpoint.models) ? endpoint.models : []).forEach(modelId => {
|
(Array.isArray(endpoint.models) ? endpoint.models : []).forEach(modelId => {
|
||||||
if (modelId && modelCaps(modelId, '', endpoint.model_type).gen) imageModels.add(String(modelId));
|
if (modelId && modelCaps(modelId, '', endpoint.model_type).gen) imageModels.add(String(modelId));
|
||||||
if (modelId && modelCaps(modelId, '', endpoint.model_type).inpaint) editModels.add(String(modelId));
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
modelSel.length = 1;
|
|
||||||
sortModelIds(Array.from(imageModels)).forEach(mid => {
|
sortModelIds(Array.from(imageModels)).forEach(mid => {
|
||||||
const opt = document.createElement('option');
|
const opt = document.createElement('option');
|
||||||
opt.value = mid;
|
opt.value = mid;
|
||||||
opt.textContent = mid;
|
opt.textContent = mid;
|
||||||
modelSel.appendChild(opt);
|
modelSel.appendChild(opt);
|
||||||
});
|
});
|
||||||
if (editModelSel) {
|
|
||||||
editModelSel.length = 1;
|
|
||||||
for (const mid of sortModelIds(Array.from(editModels))) {
|
|
||||||
const option = document.createElement('option');
|
|
||||||
option.value = mid;
|
|
||||||
option.textContent = mid;
|
|
||||||
editModelSel.appendChild(option);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (e) { console.warn('Failed to load models for image settings', e); }
|
} catch (e) { console.warn('Failed to load models for image settings', e); }
|
||||||
let currentEnabled = enabledToggle ? enabledToggle.checked : false;
|
|
||||||
try {
|
try {
|
||||||
const settings = await readImagePreferences();
|
const settingsRes = await fetch('/api/auth/settings', { credentials: 'same-origin' });
|
||||||
if (modelSel && 'image_model' in settings) modelSel.value = settings.image_model || '';
|
const settings = await settingsRes.json();
|
||||||
if (editModelSel && 'image_edit_model' in settings) editModelSel.value = settings.image_edit_model || '';
|
if (settings.image_model) modelSel.value = settings.image_model;
|
||||||
if (qualSel && 'image_quality' in settings) qualSel.value = settings.image_quality || 'medium';
|
if (settings.image_quality) qualSel.value = settings.image_quality;
|
||||||
if (enabledToggle) {
|
if (enabledToggle) enabledToggle.checked = settings.image_gen_enabled === true;
|
||||||
enabledToggle.checked = settings.image_gen_enabled === true;
|
|
||||||
currentEnabled = enabledToggle.checked;
|
|
||||||
}
|
|
||||||
} catch (e) { console.warn('Failed to load settings', e); }
|
} catch (e) { console.warn('Failed to load settings', e); }
|
||||||
|
|
||||||
function syncImgDisabled() {
|
function syncImgDisabled() {
|
||||||
@@ -65,115 +47,14 @@ export async function initImageSettings() {
|
|||||||
}
|
}
|
||||||
syncImgDisabled();
|
syncImgDisabled();
|
||||||
|
|
||||||
function checkIsAdmin() {
|
async function saveSettings() {
|
||||||
return typeof window !== 'undefined' && !!window._isAdmin;
|
|
||||||
}
|
|
||||||
|
|
||||||
function applyAdminToggleState() {
|
|
||||||
if (!enabledToggle) return;
|
|
||||||
const admin = checkIsAdmin();
|
|
||||||
if (!admin) {
|
|
||||||
enabledToggle.disabled = true;
|
|
||||||
const adminTitle = 'Admin access is required to change image generation availability.';
|
|
||||||
enabledToggle.title = adminTitle;
|
|
||||||
if (enabledToggle.parentElement) enabledToggle.parentElement.title = adminTitle;
|
|
||||||
} else {
|
|
||||||
enabledToggle.disabled = false;
|
|
||||||
const adminTitle = 'Enable or disable image generation globally';
|
|
||||||
enabledToggle.title = adminTitle;
|
|
||||||
if (enabledToggle.parentElement) enabledToggle.parentElement.title = adminTitle;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
applyAdminToggleState();
|
|
||||||
|
|
||||||
async function saveModelPreferences() {
|
|
||||||
try {
|
try {
|
||||||
const res = await _postSettings({
|
const res = await _postSettings({ image_gen_enabled: enabledToggle ? enabledToggle.checked : false, image_model: modelSel.value, image_quality: qualSel.value });
|
||||||
image_model: modelSel ? modelSel.value : '',
|
|
||||||
image_edit_model: editModelSel?.value || '',
|
|
||||||
image_quality: qualSel ? qualSel.value : 'medium',
|
|
||||||
});
|
|
||||||
if (!res.ok) throw new Error(await res.text().catch(() => `HTTP ${res.status}`));
|
if (!res.ok) throw new Error(await res.text().catch(() => `HTTP ${res.status}`));
|
||||||
msg.textContent = 'Saved'; msg.style.color = 'var(--fg)'; setTimeout(() => { msg.textContent = ''; }, 2000);
|
msg.textContent = 'Saved'; msg.style.color = 'var(--fg)'; setTimeout(() => { msg.textContent = ''; }, 2000);
|
||||||
} catch (e) {
|
} catch (e) { msg.textContent = e.status === 403 ? 'Admin access is required to change these settings.' : 'Failed to save'; msg.style.color = 'var(--red)'; }
|
||||||
if (e.globalSaved) {
|
|
||||||
msg.textContent = 'Policy saved, but failed to clear preferences: ' + (e.message || 'cleanup error');
|
|
||||||
msg.style.color = 'var(--red)';
|
|
||||||
} else {
|
|
||||||
msg.textContent = e.status === 403 ? 'Admin access is required to change these settings.' : 'Failed to save';
|
|
||||||
msg.style.color = 'var(--red)';
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
modelSel.addEventListener('change', saveSettings);
|
||||||
async function saveToggle() {
|
qualSel.addEventListener('change', saveSettings);
|
||||||
if (!checkIsAdmin()) {
|
if (enabledToggle) enabledToggle.addEventListener('change', function() { syncImgDisabled(); saveSettings(); });
|
||||||
if (enabledToggle) enabledToggle.checked = currentEnabled;
|
|
||||||
syncImgDisabled();
|
|
||||||
msg.textContent = 'Admin access is required to change these settings.';
|
|
||||||
msg.style.color = 'var(--red)';
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
const targetState = enabledToggle ? enabledToggle.checked : false;
|
|
||||||
try {
|
|
||||||
const res = await _postSettings({ image_gen_enabled: targetState });
|
|
||||||
if (!res.ok) throw new Error(await res.text().catch(() => `HTTP ${res.status}`));
|
|
||||||
if (enabledToggle) currentEnabled = enabledToggle.checked;
|
|
||||||
msg.textContent = 'Saved'; msg.style.color = 'var(--fg)'; setTimeout(() => { msg.textContent = ''; }, 2000);
|
|
||||||
} catch (e) {
|
|
||||||
if (e.globalSaved) {
|
|
||||||
currentEnabled = targetState;
|
|
||||||
try {
|
|
||||||
const authSettings = await readImagePreferences();
|
|
||||||
if (typeof authSettings.image_gen_enabled === 'boolean') {
|
|
||||||
currentEnabled = authSettings.image_gen_enabled;
|
|
||||||
}
|
|
||||||
} catch (_) {}
|
|
||||||
if (enabledToggle) enabledToggle.checked = currentEnabled;
|
|
||||||
syncImgDisabled();
|
|
||||||
msg.textContent = 'Policy saved, but failed to clear preferences: ' + (e.message || 'cleanup error');
|
|
||||||
msg.style.color = 'var(--red)';
|
|
||||||
} else {
|
|
||||||
try {
|
|
||||||
const authSettings = await readImagePreferences();
|
|
||||||
if (typeof authSettings.image_gen_enabled === 'boolean') {
|
|
||||||
currentEnabled = authSettings.image_gen_enabled;
|
|
||||||
}
|
|
||||||
} catch (_) {}
|
|
||||||
if (enabledToggle) enabledToggle.checked = currentEnabled;
|
|
||||||
syncImgDisabled();
|
|
||||||
msg.textContent = e.status === 403 ? 'Admin access is required to change these settings.' : 'Failed to save';
|
|
||||||
msg.style.color = 'var(--red)';
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (modelSel) modelSel.addEventListener('change', saveModelPreferences);
|
|
||||||
if (editModelSel) editModelSel.addEventListener('change', saveModelPreferences);
|
|
||||||
if (qualSel) qualSel.addEventListener('change', saveModelPreferences);
|
|
||||||
if (enabledToggle) {
|
|
||||||
enabledToggle.addEventListener('change', async function() {
|
|
||||||
if (!checkIsAdmin()) {
|
|
||||||
enabledToggle.checked = currentEnabled;
|
|
||||||
syncImgDisabled();
|
|
||||||
msg.textContent = 'Admin access is required to change these settings.';
|
|
||||||
msg.style.color = 'var(--red)';
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
syncImgDisabled();
|
|
||||||
await saveToggle();
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
if (preferencesListener) window.removeEventListener('image-preferences-changed', preferencesListener);
|
|
||||||
preferencesListener = event => {
|
|
||||||
if ('image_model' in event.detail && modelSel) modelSel.value = event.detail.image_model || '';
|
|
||||||
if (editModelSel && 'image_edit_model' in event.detail) editModelSel.value = event.detail.image_edit_model || '';
|
|
||||||
if ('image_gen_enabled' in event.detail && enabledToggle) {
|
|
||||||
enabledToggle.checked = event.detail.image_gen_enabled === true;
|
|
||||||
currentEnabled = enabledToggle.checked;
|
|
||||||
syncImgDisabled();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
window.addEventListener('image-preferences-changed', preferencesListener);
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,107 +0,0 @@
|
|||||||
import { invalidateSettings } from '../appConfig.js';
|
|
||||||
|
|
||||||
const USER_PREFERENCE_KEYS = ['image_model', 'image_edit_model', 'image_quality'];
|
|
||||||
|
|
||||||
export async function readImagePreferences() {
|
|
||||||
const [prefsRes, settingsRes] = await Promise.all([
|
|
||||||
fetch('/api/prefs', { credentials: 'same-origin' }).catch(() => null),
|
|
||||||
fetch('/api/auth/settings', { credentials: 'same-origin' }).catch(() => null),
|
|
||||||
]);
|
|
||||||
const prefs = (prefsRes && prefsRes.ok) ? await prefsRes.json().catch(() => ({})) : {};
|
|
||||||
const settings = (settingsRes && settingsRes.ok) ? await settingsRes.json().catch(() => ({})) : {};
|
|
||||||
if (!prefsRes?.ok && !settingsRes?.ok) throw new Error('Could not load image settings');
|
|
||||||
return {
|
|
||||||
...settings,
|
|
||||||
image_model: prefs?.image_model || settings?.image_model || '',
|
|
||||||
image_edit_model: prefs?.image_edit_model || settings?.image_edit_model || '',
|
|
||||||
image_quality: prefs?.image_quality || settings?.image_quality || 'medium',
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
let saveQueue = Promise.resolve();
|
|
||||||
export function waitForImagePreferences() { return saveQueue; }
|
|
||||||
export function saveImagePreferences(patch) {
|
|
||||||
const save = async () => {
|
|
||||||
let response;
|
|
||||||
try {
|
|
||||||
response = await fetch('/api/auth/settings', {
|
|
||||||
method: 'POST', credentials: 'same-origin',
|
|
||||||
headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(patch),
|
|
||||||
});
|
|
||||||
} catch (netErr) {
|
|
||||||
invalidateSettings();
|
|
||||||
netErr.globalSaved = false;
|
|
||||||
throw netErr;
|
|
||||||
}
|
|
||||||
invalidateSettings();
|
|
||||||
if (response.status === 403) {
|
|
||||||
const userEntries = Object.entries(patch).filter(([k]) => USER_PREFERENCE_KEYS.includes(k));
|
|
||||||
if (userEntries.length === 0) {
|
|
||||||
const err = new Error('Admin access is required to change these settings.');
|
|
||||||
err.status = 403;
|
|
||||||
err.globalSaved = false;
|
|
||||||
throw err;
|
|
||||||
}
|
|
||||||
for (const [key, value] of userEntries) {
|
|
||||||
const prefRes = await fetch(`/api/prefs/${encodeURIComponent(key)}`, {
|
|
||||||
method: 'PUT', credentials: 'same-origin',
|
|
||||||
headers: { 'Content-Type': 'application/json' },
|
|
||||||
body: JSON.stringify({ value }),
|
|
||||||
});
|
|
||||||
if (!prefRes.ok) {
|
|
||||||
const err = new Error(await prefRes.text().catch(() => 'Could not save image preferences'));
|
|
||||||
err.status = prefRes.status;
|
|
||||||
err.globalSaved = false;
|
|
||||||
throw err;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
response = typeof Response !== 'undefined'
|
|
||||||
? new Response(JSON.stringify({ ok: true }), { status: 200, headers: { 'Content-Type': 'application/json' } })
|
|
||||||
: { ok: true, status: 200, json: async () => ({ ok: true }), text: async () => '{"ok":true}' };
|
|
||||||
} else if (response.ok) {
|
|
||||||
const clearResults = await Promise.all(USER_PREFERENCE_KEYS.map(async key => {
|
|
||||||
try {
|
|
||||||
const res = await fetch(`/api/prefs/${encodeURIComponent(key)}`, {
|
|
||||||
method: 'PUT', credentials: 'same-origin',
|
|
||||||
headers: { 'Content-Type': 'application/json' },
|
|
||||||
body: JSON.stringify({ value: null }),
|
|
||||||
});
|
|
||||||
if (!res.ok) {
|
|
||||||
const err = new Error(await res.text().catch(() => `HTTP ${res.status}`));
|
|
||||||
err.status = res.status;
|
|
||||||
return { ok: false, error: err };
|
|
||||||
}
|
|
||||||
return { ok: true };
|
|
||||||
} catch (err) {
|
|
||||||
return { ok: false, error: err };
|
|
||||||
}
|
|
||||||
}));
|
|
||||||
const failure = clearResults.find(r => !r.ok);
|
|
||||||
if (failure) {
|
|
||||||
invalidateSettings();
|
|
||||||
if (typeof window !== 'undefined' && typeof CustomEvent !== 'undefined') {
|
|
||||||
window.dispatchEvent(new CustomEvent('image-preferences-changed', { detail: patch }));
|
|
||||||
}
|
|
||||||
const err = new Error(failure.error?.message || 'Could not clear personal preferences');
|
|
||||||
err.status = failure.error?.status || 500;
|
|
||||||
err.cause = failure.error;
|
|
||||||
err.globalSaved = true;
|
|
||||||
err.phase = 'cleanup';
|
|
||||||
throw err;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
const err = new Error('Could not save image settings');
|
|
||||||
err.status = response.status;
|
|
||||||
err.globalSaved = false;
|
|
||||||
throw err;
|
|
||||||
}
|
|
||||||
invalidateSettings();
|
|
||||||
if (typeof window !== 'undefined' && typeof CustomEvent !== 'undefined') {
|
|
||||||
window.dispatchEvent(new CustomEvent('image-preferences-changed', { detail: patch }));
|
|
||||||
}
|
|
||||||
return response;
|
|
||||||
};
|
|
||||||
const result = saveQueue.then(save);
|
|
||||||
saveQueue = result.catch(() => {});
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
+1
-5
@@ -7,7 +7,7 @@
|
|||||||
// - Other static assets (images/fonts/libs): cache-first with bg refresh.
|
// - Other static assets (images/fonts/libs): cache-first with bg refresh.
|
||||||
// - API / non-GET: never cached.
|
// - API / non-GET: never cached.
|
||||||
// Bump CACHE_NAME whenever the precache list or SW logic changes.
|
// Bump CACHE_NAME whenever the precache list or SW logic changes.
|
||||||
const CACHE_NAME = 'odysseus-v1134-pr40-editor-precache';
|
const CACHE_NAME = 'odysseus-v1115-pr40-editor-precache';
|
||||||
|
|
||||||
// KaTeX resolves these from its own stylesheet, so caching the CSS without them
|
// KaTeX resolves these from its own stylesheet, so caching the CSS without them
|
||||||
// gives offline math fallback glyphs instead of proper typesetting.
|
// gives offline math fallback glyphs instead of proper typesetting.
|
||||||
@@ -237,10 +237,6 @@ const PANEL_PRECACHE = [
|
|||||||
'/static/js/editor/aspect-fit.js',
|
'/static/js/editor/aspect-fit.js',
|
||||||
'/static/js/editor/preflight.js',
|
'/static/js/editor/preflight.js',
|
||||||
'/static/js/editor/ai-settings.js',
|
'/static/js/editor/ai-settings.js',
|
||||||
'/static/js/editor/image-generation.js',
|
|
||||||
'/static/js/editor/image-result-geometry.js',
|
|
||||||
'/static/js/editor/quick-edit-job.js',
|
|
||||||
'/static/js/settings/imagePreferences.js',
|
|
||||||
'/static/js/editor/look-recipes.js',
|
'/static/js/editor/look-recipes.js',
|
||||||
'/static/js/editor/editor-control.js',
|
'/static/js/editor/editor-control.js',
|
||||||
'/static/js/editor/editor-bridge.js',
|
'/static/js/editor/editor-bridge.js',
|
||||||
|
|||||||
@@ -0,0 +1,19 @@
|
|||||||
|
const fs = require('node:fs');
|
||||||
|
const os = require('node:os');
|
||||||
|
const path = require('node:path');
|
||||||
|
const marker = '.disposable-ci-fixture.json';
|
||||||
|
|
||||||
|
function disposableCIArtifacts() {
|
||||||
|
if (!(process.env.CI || process.env.GITHUB_ACTIONS) || process.env.ODYSSEUS_E2E_DISPOSABLE_ARTIFACTS !== '1') return false;
|
||||||
|
const directory = process.env.PHOTO_EDITOR_E2E_DATA_DIR;
|
||||||
|
if (!directory || process.env.PHOTO_EDITOR_E2E_AUTH === '0') return false;
|
||||||
|
const resolved = path.resolve(directory);
|
||||||
|
if (!path.basename(resolved).startsWith('odysseus-photo-editor-e2e-')) return false;
|
||||||
|
if (![os.tmpdir(), '/tmp', '/var/tmp'].some(base => resolved.startsWith(path.resolve(base) + path.sep))) return false;
|
||||||
|
if (process.env.PHOTO_EDITOR_E2E_DB_PATH !== path.join(resolved, 'app.db')) return false;
|
||||||
|
try {
|
||||||
|
const fixture = JSON.parse(fs.readFileSync(path.join(resolved, marker), 'utf8'));
|
||||||
|
return fixture.synthetic === true && fixture.authenticated === true && fixture.database === path.join(resolved, 'app.db');
|
||||||
|
} catch { return false; }
|
||||||
|
}
|
||||||
|
module.exports = { disposableCIArtifacts, marker };
|
||||||
@@ -35,7 +35,7 @@ test('brush presets and shared stroke controls remain reusable', async ({ page }
|
|||||||
await expect(page.locator('#ge-brush-preset option', { hasText: 'My Detail Brush' })).toHaveCount(0);
|
await expect(page.locator('#ge-brush-preset option', { hasText: 'My Detail Brush' })).toHaveCount(0);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('long sampled strokes stay responsive and undo atomically', async ({ page }) => {
|
test('long sampled strokes stay responsive and undo atomically', async ({ page, browserName }, testInfo) => {
|
||||||
await openBlankEditor(page);
|
await openBlankEditor(page);
|
||||||
await selectTool(page, 'brush');
|
await selectTool(page, 'brush');
|
||||||
await page.locator('.ge-size-slider').fill('700');
|
await page.locator('.ge-size-slider').fill('700');
|
||||||
@@ -49,13 +49,40 @@ test('long sampled strokes stay responsive and undo atomically', async ({ page }
|
|||||||
const before = await flattenedPixelDigest(page);
|
const before = await flattenedPixelDigest(page);
|
||||||
const start = await canvasPoint(page, 0.08, 0.5);
|
const start = await canvasPoint(page, 0.08, 0.5);
|
||||||
const end = await canvasPoint(page, 0.92, 0.5);
|
const end = await canvasPoint(page, 0.92, 0.5);
|
||||||
|
// Playwright delivers stepped moves at frame cadence: 240 events already
|
||||||
|
// cost ~4s on an inert page. Measure that transport cost with the same input,
|
||||||
|
// and separately bound real Chromium main-thread work at the original5s.
|
||||||
|
const control = await page.context().newPage();
|
||||||
|
let controlDuration;
|
||||||
|
try {
|
||||||
|
await control.setContent('<canvas style="position:fixed;inset:0;width:100vw;height:100vh"></canvas>');
|
||||||
|
const started = Date.now();
|
||||||
|
await control.mouse.move(start.x, start.y);
|
||||||
|
await control.mouse.down();
|
||||||
|
await control.mouse.move(end.x, end.y, { steps: 240 });
|
||||||
|
await control.mouse.up();
|
||||||
|
controlDuration = Date.now() - started;
|
||||||
|
} finally {
|
||||||
|
await control.close();
|
||||||
|
await page.bringToFront();
|
||||||
|
}
|
||||||
|
const metrics = browserName === 'chromium' ? await page.context().newCDPSession(page) : null;
|
||||||
|
if (metrics) await metrics.send('Performance.enable');
|
||||||
|
const taskTime = async () => (await metrics.send('Performance.getMetrics')).metrics.find(item => item.name === 'TaskDuration').value *1000;
|
||||||
|
const beforeTask = metrics ? await taskTime() : null;
|
||||||
const startedAt = Date.now();
|
const startedAt = Date.now();
|
||||||
await page.mouse.move(start.x, start.y);
|
await page.mouse.move(start.x, start.y);
|
||||||
await page.mouse.down();
|
await page.mouse.down();
|
||||||
await page.mouse.move(end.x, end.y, { steps: 240 });
|
await page.mouse.move(end.x, end.y, { steps: 240 });
|
||||||
await page.mouse.up();
|
await page.mouse.up();
|
||||||
const strokeDuration = Date.now() - startedAt;
|
const strokeDuration = Date.now() - startedAt;
|
||||||
expect(strokeDuration).toBeLessThan(5000);
|
const mainThreadDuration = metrics ? (await taskTime()) - beforeTask : null;
|
||||||
|
if (metrics) await metrics.detach();
|
||||||
|
await testInfo.attach('stroke-timing', { body: Buffer.from(JSON.stringify({
|
||||||
|
samples: 240, strokeDuration, controlDuration, mainThreadDuration,
|
||||||
|
})), contentType: 'application/json' });
|
||||||
|
expect(strokeDuration - controlDuration).toBeLessThan(5000);
|
||||||
|
if (mainThreadDuration !== null) expect(mainThreadDuration).toBeLessThan(5000);
|
||||||
const after = await flattenedPixelDigest(page);
|
const after = await flattenedPixelDigest(page);
|
||||||
expect(after).not.toEqual(before);
|
expect(after).not.toEqual(before);
|
||||||
await expect(thumb).toHaveAttribute('width', '68');
|
await expect(thumb).toHaveAttribute('width', '68');
|
||||||
|
|||||||
@@ -0,0 +1,21 @@
|
|||||||
|
const { test, expect } = require('@playwright/test');
|
||||||
|
const { spawnSync } = require('node:child_process');
|
||||||
|
const path = require('node:path');
|
||||||
|
|
||||||
|
// The previously opt-in node:test now uses only this config's owned app/account.
|
||||||
|
// Its native JUnit report retains its authored identity; this case is a driver.
|
||||||
|
test('native email reply regression on the disposable authenticated app', async ({ context, baseURL }, testInfo) => {
|
||||||
|
const origin = new URL(baseURL);
|
||||||
|
expect(origin.hostname).toBe('127.0.0.1');
|
||||||
|
const cookie = (await context.cookies(baseURL)).find(item => item.name === 'odysseus_session');
|
||||||
|
expect(cookie?.value).toBeTruthy();
|
||||||
|
const report = testInfo.outputPath('native-email-junit.xml');
|
||||||
|
const result = spawnSync(process.execPath, ['--test', '--test-reporter=spec', '--test-reporter-destination=stdout',
|
||||||
|
'--test-reporter=junit', `--test-reporter-destination=${report}`, 'tests/emailReplyBrowser.test.mjs'], {
|
||||||
|
cwd: path.resolve(__dirname, '../../..'), encoding: 'utf8', timeout: 80_000,
|
||||||
|
env: { ...process.env, ODYSSEUS_UI_TEST_URL: baseURL, ODYSSEUS_UI_TEST_COOKIE: cookie.value },
|
||||||
|
});
|
||||||
|
console.log(result.stdout);
|
||||||
|
expect(result.error, String(result.error)).toBeUndefined();
|
||||||
|
expect(result.status, result.stdout + result.stderr).toBe(0);
|
||||||
|
});
|
||||||
@@ -2,6 +2,12 @@ const fs = require('node:fs');
|
|||||||
const os = require('node:os');
|
const os = require('node:os');
|
||||||
const path = require('node:path');
|
const path = require('node:path');
|
||||||
const { defineConfig } = require('@playwright/test');
|
const { defineConfig } = require('@playwright/test');
|
||||||
|
const { disposableCIArtifacts, marker } = require('./artifact-policy');
|
||||||
|
const isCI = Boolean(process.env.CI) || Boolean(process.env.GITHUB_ACTIONS);
|
||||||
|
const disposableOptIn = isCI && process.env.ODYSSEUS_E2E_DISPOSABLE_ARTIFACTS === '1';
|
||||||
|
if (disposableOptIn && (process.env.PHOTO_EDITOR_E2E_DATA_DIR || process.env.PHOTO_EDITOR_E2E_DB_PATH || process.env.PHOTO_EDITOR_E2E_AUTH === '0') && !disposableCIArtifacts()) {
|
||||||
|
throw new Error('CI artifact opt-in requires fresh authenticated fixture paths');
|
||||||
|
}
|
||||||
|
|
||||||
const port = Number(process.env.PHOTO_EDITOR_E2E_PORT || 7013);
|
const port = Number(process.env.PHOTO_EDITOR_E2E_PORT || 7013);
|
||||||
const baseURL = `http://127.0.0.1:${port}`;
|
const baseURL = `http://127.0.0.1:${port}`;
|
||||||
@@ -24,7 +30,25 @@ if (!['chromium', 'firefox', 'webkit'].includes(browserName)) {
|
|||||||
throw new Error(`Unsupported PHOTO_EDITOR_E2E_BROWSER: ${browserName}`);
|
throw new Error(`Unsupported PHOTO_EDITOR_E2E_BROWSER: ${browserName}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
const isCI = Boolean(process.env.CI) || Boolean(process.env.GITHUB_ACTIONS);
|
if (disposableOptIn) {
|
||||||
|
const fixturePayload = JSON.stringify({ synthetic: true, authenticated: true, database: databasePath });
|
||||||
|
fs.writeFileSync(path.join(dataDirectory, marker), fixturePayload);
|
||||||
|
const resultsDir = path.join(repositoryRoot, 'test-results');
|
||||||
|
if (!fs.existsSync(resultsDir)) {
|
||||||
|
try { fs.mkdirSync(resultsDir, { recursive: true }); } catch (_) {}
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
fs.writeFileSync(path.join(resultsDir, marker), fixturePayload);
|
||||||
|
} catch (_) {}
|
||||||
|
if (process.env.GITHUB_ENV) {
|
||||||
|
try {
|
||||||
|
fs.appendFileSync(
|
||||||
|
process.env.GITHUB_ENV,
|
||||||
|
`PHOTO_EDITOR_E2E_DATA_DIR=${dataDirectory}\nPHOTO_EDITOR_E2E_DB_PATH=${databasePath}\n`
|
||||||
|
);
|
||||||
|
} catch (_) {}
|
||||||
|
}
|
||||||
|
}
|
||||||
const traceOptIn = !isCI && ['1', 'true'].includes(String(process.env.PLAYWRIGHT_ENABLE_TRACE || '').toLowerCase());
|
const traceOptIn = !isCI && ['1', 'true'].includes(String(process.env.PLAYWRIGHT_ENABLE_TRACE || '').toLowerCase());
|
||||||
|
|
||||||
module.exports = defineConfig({
|
module.exports = defineConfig({
|
||||||
@@ -32,6 +56,8 @@ module.exports = defineConfig({
|
|||||||
outputDir: path.join(__dirname, '..', '..', 'test-results', 'photo-editor'),
|
outputDir: path.join(__dirname, '..', '..', 'test-results', 'photo-editor'),
|
||||||
timeout: 90_000,
|
timeout: 90_000,
|
||||||
expect: { timeout: 10_000 },
|
expect: { timeout: 10_000 },
|
||||||
|
forbidOnly: isCI,
|
||||||
|
retries: 0,
|
||||||
fullyParallel: false,
|
fullyParallel: false,
|
||||||
workers: 1,
|
workers: 1,
|
||||||
reporter: isCI ? 'line' : 'line',
|
reporter: isCI ? 'line' : 'line',
|
||||||
@@ -49,9 +75,9 @@ module.exports = defineConfig({
|
|||||||
serviceWorkers: 'block',
|
serviceWorkers: 'block',
|
||||||
...(authenticated ? { storageState: path.join(dataDirectory, 'browser-auth.json') } : {}),
|
...(authenticated ? { storageState: path.join(dataDirectory, 'browser-auth.json') } : {}),
|
||||||
screenshot: 'only-on-failure',
|
screenshot: 'only-on-failure',
|
||||||
// Unconditional CI trace suppression: Playwright traces are NEVER recorded in CI.
|
// Default CI suppression remains; only the fresh synthetic fixture opts in.
|
||||||
// Local developer tracing requires explicit PLAYWRIGHT_ENABLE_TRACE=1.
|
// Local developer tracing requires explicit PLAYWRIGHT_ENABLE_TRACE=1.
|
||||||
trace: traceOptIn ? 'retain-on-failure' : 'off',
|
trace: (traceOptIn || disposableCIArtifacts()) ? 'retain-on-failure' : 'off',
|
||||||
},
|
},
|
||||||
webServer: {
|
webServer: {
|
||||||
command: `${JSON.stringify(python)} -m uvicorn app:app --host 127.0.0.1 --port ${port}`,
|
command: `${JSON.stringify(python)} -m uvicorn app:app --host 127.0.0.1 --port ${port}`,
|
||||||
@@ -65,6 +91,10 @@ module.exports = defineConfig({
|
|||||||
DATABASE_URL: `sqlite:///${databasePath}`,
|
DATABASE_URL: `sqlite:///${databasePath}`,
|
||||||
ODYSSEUS_DATA_DIR: dataDirectory,
|
ODYSSEUS_DATA_DIR: dataDirectory,
|
||||||
ODYSSEUS_STARTUP_WARMUPS: '0',
|
ODYSSEUS_STARTUP_WARMUPS: '0',
|
||||||
|
ODYSSEUS_TOOL_INDEX_PREWARM: '0',
|
||||||
|
ODYSSEUS_BROWSER_MCP_REQUIRE_CACHE: '1',
|
||||||
|
npm_config_offline: 'true',
|
||||||
|
npm_config_cache: path.join(dataDirectory, 'npm-cache'),
|
||||||
RESPONSE_COMPRESSION_ENABLED: 'false',
|
RESPONSE_COMPRESSION_ENABLED: 'false',
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|||||||
+16
-2
@@ -2,6 +2,7 @@ const fs = require('node:fs');
|
|||||||
const os = require('node:os');
|
const os = require('node:os');
|
||||||
const path = require('node:path');
|
const path = require('node:path');
|
||||||
const { execFileSync } = require('node:child_process');
|
const { execFileSync } = require('node:child_process');
|
||||||
|
const { disposableCIArtifacts, marker } = require('./artifact-policy');
|
||||||
|
|
||||||
const repoRoot = path.resolve(__dirname, '..', '..');
|
const repoRoot = path.resolve(__dirname, '..', '..');
|
||||||
const defaultResultsDir = path.join(repoRoot, 'test-results');
|
const defaultResultsDir = path.join(repoRoot, 'test-results');
|
||||||
@@ -29,6 +30,9 @@ module.exports = async (options = {}) => {
|
|||||||
const isPlaywrightConfig = options && (options.configFile || options.rootDir);
|
const isPlaywrightConfig = options && (options.configFile || options.rootDir);
|
||||||
const testOpts = (!isPlaywrightConfig && typeof options === 'object') ? options : {};
|
const testOpts = (!isPlaywrightConfig && typeof options === 'object') ? options : {};
|
||||||
|
|
||||||
|
// Validate fixture ownership before removing its temporary directory.
|
||||||
|
const disposableCI = disposableCIArtifacts();
|
||||||
|
|
||||||
// Confine cleanup of PHOTO_EDITOR_E2E_DATA_DIR:
|
// Confine cleanup of PHOTO_EDITOR_E2E_DATA_DIR:
|
||||||
// Strictly require it to be within an OS temporary directory and match the expected test prefix
|
// Strictly require it to be within an OS temporary directory and match the expected test prefix
|
||||||
const dataDirectory = process.env.PHOTO_EDITOR_E2E_DATA_DIR;
|
const dataDirectory = process.env.PHOTO_EDITOR_E2E_DATA_DIR;
|
||||||
@@ -100,7 +104,7 @@ module.exports = async (options = {}) => {
|
|||||||
const isCI = Boolean(process.env.CI) || Boolean(process.env.GITHUB_ACTIONS);
|
const isCI = Boolean(process.env.CI) || Boolean(process.env.GITHUB_ACTIONS);
|
||||||
const traceOptIn = !isCI && ['1', 'true'].includes(String(process.env.PLAYWRIGHT_ENABLE_TRACE || '').toLowerCase());
|
const traceOptIn = !isCI && ['1', 'true'].includes(String(process.env.PLAYWRIGHT_ENABLE_TRACE || '').toLowerCase());
|
||||||
|
|
||||||
if (isCI || !traceOptIn) {
|
if ((isCI && !disposableCI) || (!traceOptIn && !disposableCI)) {
|
||||||
// In CI or default local runs, purge all sensitive trace archives and artifacts
|
// In CI or default local runs, purge all sensitive trace archives and artifacts
|
||||||
purgeSensitiveArtifacts(resultsDir);
|
purgeSensitiveArtifacts(resultsDir);
|
||||||
} else {
|
} else {
|
||||||
@@ -124,7 +128,17 @@ module.exports = async (options = {}) => {
|
|||||||
|
|
||||||
if (!sanitizerSucceeded) {
|
if (!sanitizerSucceeded) {
|
||||||
// Fail closed: purge all remaining zip or tmp archives
|
// Fail closed: purge all remaining zip or tmp archives
|
||||||
purgeZipAndTmp(resultsDir);
|
if (disposableCI) purgeSensitiveArtifacts(resultsDir);
|
||||||
|
else purgeZipAndTmp(resultsDir);
|
||||||
|
} else if (disposableCI) {
|
||||||
|
// Re-affirm fixture proof in resultsDir so independent verifier can establish ownership
|
||||||
|
try {
|
||||||
|
fs.writeFileSync(path.join(resultsDir, marker), JSON.stringify({
|
||||||
|
synthetic: true,
|
||||||
|
authenticated: true,
|
||||||
|
database: process.env.PHOTO_EDITOR_E2E_DB_PATH || path.join(dataDirectory || '', 'app.db'),
|
||||||
|
}));
|
||||||
|
} catch (_) {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -24,7 +24,6 @@ const REAL_MODULES = new Set([
|
|||||||
path.join(JS, 'settings/speech.js'),
|
path.join(JS, 'settings/speech.js'),
|
||||||
path.join(JS, 'settings/writingStyle.js'),
|
path.join(JS, 'settings/writingStyle.js'),
|
||||||
path.join(JS, 'settings/imageModels.js'),
|
path.join(JS, 'settings/imageModels.js'),
|
||||||
path.join(JS, 'settings/imagePreferences.js'),
|
|
||||||
path.join(JS, 'settings/agent.js'),
|
path.join(JS, 'settings/agent.js'),
|
||||||
path.join(JS, 'settings/shell.js'),
|
path.join(JS, 'settings/shell.js'),
|
||||||
path.join(JS, 'settings/peek.js'),
|
path.join(JS, 'settings/peek.js'),
|
||||||
|
|||||||
@@ -0,0 +1,71 @@
|
|||||||
|
{
|
||||||
|
"native-ci": [
|
||||||
|
"tests/backgroundToolJobs.test.mjs",
|
||||||
|
"tests/chatEditorProgress.test.mjs",
|
||||||
|
"tests/chatImageDeletion.test.mjs",
|
||||||
|
"tests/chatPhotoEditorIntegrity.test.mjs",
|
||||||
|
"tests/chatProcessingHandoff.test.mjs",
|
||||||
|
"tests/copyChatHeader.test.mjs",
|
||||||
|
"tests/emailReplyStream.test.mjs",
|
||||||
|
"tests/galleryModelPickerSync.test.mjs",
|
||||||
|
"tests/generatedImageResult.test.mjs",
|
||||||
|
"tests/researchMobileButtons.test.mjs",
|
||||||
|
"tests/schemaThinkingProbe.test.mjs",
|
||||||
|
"tests/sidebarNewChat.test.mjs",
|
||||||
|
"tests/skillsApproval.test.mjs",
|
||||||
|
"tests/streamingRendererRaf.test.mjs",
|
||||||
|
"tests/toolFollowupOracle.test.mjs",
|
||||||
|
"tests/tool_followup_oracle.test.mjs"
|
||||||
|
],
|
||||||
|
"browser-ci": [
|
||||||
|
"tests/emailReplyBrowser.test.mjs"
|
||||||
|
],
|
||||||
|
"python-ci": {
|
||||||
|
"tests/historyResumeRendering.test.mjs": [
|
||||||
|
"tests/test_history_resume_rendering_js.py",
|
||||||
|
"tests/test_live_fallback_round_attribution.py"
|
||||||
|
],
|
||||||
|
"tests/live_thinking_scheduler.test.mjs": [
|
||||||
|
"tests/test_live_thinking_scheduler_js.py"
|
||||||
|
],
|
||||||
|
"tests/noteTestOracle.test.mjs": [
|
||||||
|
"tests/test_notes_reference_contracts_js.py"
|
||||||
|
],
|
||||||
|
"tests/notesApplication.test.mjs": [
|
||||||
|
"tests/test_notes_reference_contracts_js.py"
|
||||||
|
],
|
||||||
|
"tests/notesCharacterization.test.mjs": [
|
||||||
|
"tests/test_notes_reference_contracts_js.py"
|
||||||
|
],
|
||||||
|
"tests/notesDraftAutosave.test.mjs": [
|
||||||
|
"tests/test_notes_reference_contracts_js.py"
|
||||||
|
],
|
||||||
|
"tests/notesInfrastructure.test.mjs": [
|
||||||
|
"tests/test_notes_reference_contracts_js.py"
|
||||||
|
],
|
||||||
|
"tests/notesRecurrence.test.mjs": [
|
||||||
|
"tests/test_notes_reference_contracts_js.py"
|
||||||
|
],
|
||||||
|
"tests/notesReminders.test.mjs": [
|
||||||
|
"tests/test_notes_reference_contracts_js.py"
|
||||||
|
],
|
||||||
|
"tests/notesWorkflows.test.mjs": [
|
||||||
|
"tests/test_notes_reference_contracts_js.py"
|
||||||
|
],
|
||||||
|
"tests/selectedNotesContext.test.mjs": [
|
||||||
|
"tests/test_notes_reference_contracts_js.py"
|
||||||
|
],
|
||||||
|
"tests/streaming/invariant.test.mjs": [
|
||||||
|
"tests/test_streaming_segmenter_js.py"
|
||||||
|
],
|
||||||
|
"tests/streaming/segmenter.test.mjs": [
|
||||||
|
"tests/test_streaming_segmenter_js.py"
|
||||||
|
],
|
||||||
|
"tests/turnRendering.test.mjs": [
|
||||||
|
"tests/test_turn_rendering_js.py"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"architecture-ci": [
|
||||||
|
"tests/frontendArchitecture.test.mjs"
|
||||||
|
]
|
||||||
|
}
|
||||||
@@ -29,10 +29,8 @@ def test_secondary_brush_controls_live_after_the_menu_open():
|
|||||||
block = CONTROLS[i:j]
|
block = CONTROLS[i:j]
|
||||||
for marker in ('id="ge-brush-spacing"', 'id="ge-brush-smoothing"',
|
for marker in ('id="ge-brush-spacing"', 'id="ge-brush-smoothing"',
|
||||||
'id="ge-brush-blend"', 'ge-brush-pressure-row',
|
'id="ge-brush-blend"', 'ge-brush-pressure-row',
|
||||||
'id="ge-brush-flow-row"'):
|
'id="ge-brush-flow-row"', 'id="ge-brush-softness-row"'):
|
||||||
assert marker in block, marker
|
assert marker in block, marker
|
||||||
assert 'id="ge-brush-softness-row"' not in block
|
|
||||||
assert CONTROLS.index('id="ge-brush-softness-row"') < i
|
|
||||||
|
|
||||||
|
|
||||||
def test_brush_size_label_drops_the_redundant_word():
|
def test_brush_size_label_drops_the_redundant_word():
|
||||||
@@ -100,7 +98,7 @@ def test_brush_strip_fits_and_smudge_row_respects_inline_hide():
|
|||||||
# The Opacity/Flow/Softness sliders are wound tighter so the strip fits.
|
# The Opacity/Flow/Softness sliders are wound tighter so the strip fits.
|
||||||
assert ".ge-controls--in-options #ge-brush-section { gap: 6px; }" in CSS
|
assert ".ge-controls--in-options #ge-brush-section { gap: 6px; }" in CSS
|
||||||
# The brush controls hug their content so the sliders sit next to the caret.
|
# The brush controls hug their content so the sliders sit next to the caret.
|
||||||
assert ".ge-controls--in-options #ge-brush-controls { flex: 0 0 auto; flex-direction: row;" in CSS
|
assert ".ge-controls--in-options #ge-brush-controls { flex: 0 0 auto; }" in CSS
|
||||||
# The smudge Strength row is hidden with an inline display:none on other
|
# The smudge Strength row is hidden with an inline display:none on other
|
||||||
# tools; the `.ge-eraser-row { display:flex !important }` rule must not
|
# tools; the `.ge-eraser-row { display:flex !important }` rule must not
|
||||||
# override that.
|
# override that.
|
||||||
|
|||||||
@@ -44,11 +44,9 @@ def test_quick_edit_shows_the_generation_message_verbatim():
|
|||||||
assert "_isGenOfflineMsg" in EDITOR
|
assert "_isGenOfflineMsg" in EDITOR
|
||||||
assert "_setAiCommandStatus(_isGenOfflineMsg(_genErr) ? _genErr : `Could not generate that edit: ${_genErr}`, 'error');" in EDITOR
|
assert "_setAiCommandStatus(_isGenOfflineMsg(_genErr) ? _genErr : `Could not generate that edit: ${_genErr}`, 'error');" in EDITOR
|
||||||
assert "_setAiCommandStatus(_isGenOfflineMsg(msg) ? msg : `Could not generate an image: ${msg}`, 'error');" in EDITOR
|
assert "_setAiCommandStatus(_isGenOfflineMsg(msg) ? msg : `Could not generate an image: ${msg}`, 'error');" in EDITOR
|
||||||
# The extracted generation action keeps the shared failure classifier.
|
# the generateImage op maps its own fetch/HTTP failures through the classifier
|
||||||
generation = (ROOT / 'static/js/editor/image-generation.js').read_text()
|
assert "_generationErrorMessage({ network: true" in EDITOR
|
||||||
assert "formatError: _generationErrorMessage" in EDITOR
|
assert "_generationErrorMessage({ status: res.status, detail: _gd })" in EDITOR
|
||||||
assert "formatError({ network: true" in generation
|
|
||||||
assert "formatError({ status: response.status" in generation
|
|
||||||
|
|
||||||
|
|
||||||
def test_sw_cache_is_bumped():
|
def test_sw_cache_is_bumped():
|
||||||
|
|||||||
@@ -1,500 +0,0 @@
|
|||||||
import json
|
|
||||||
|
|
||||||
import pytest
|
|
||||||
from fastapi import FastAPI
|
|
||||||
from fastapi.testclient import TestClient
|
|
||||||
|
|
||||||
from routes import editor_agent_routes as routes
|
|
||||||
from src.agent_tools import editor_tools
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(autouse=True)
|
|
||||||
def allow_image_generation_privilege(monkeypatch):
|
|
||||||
monkeypatch.setattr('src.auth_helpers.require_privilege', lambda *args: 'test-user')
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize("chosen", ["openai/gpt-5-image", ""])
|
|
||||||
def test_quick_edit_generation_uses_generation_selection_not_planner_model(monkeypatch, chosen):
|
|
||||||
seen = []
|
|
||||||
|
|
||||||
async def execute(self, content, ctx):
|
|
||||||
seen.append(json.loads(content))
|
|
||||||
return "provider stub", {"exit_code": 1, "error": "test stops before canvas verification"}
|
|
||||||
|
|
||||||
monkeypatch.setattr(editor_tools.GenerateImageLayerTool, "execute", execute)
|
|
||||||
monkeypatch.setattr(routes, "_user", lambda request: None)
|
|
||||||
monkeypatch.setattr(routes.editor_agent_model, "startup_probe", lambda: None)
|
|
||||||
monkeypatch.setattr(routes.style_guide, "strengthen", lambda prompt, steps: steps)
|
|
||||||
monkeypatch.setattr(routes.style_guide, "enforce_guards", lambda steps: steps)
|
|
||||||
monkeypatch.setattr(routes, "_image_provider_available", lambda owner: True)
|
|
||||||
monkeypatch.setattr(routes, "_plan_special", lambda *args: None)
|
|
||||||
monkeypatch.setattr(routes, "_ensure_generative", lambda prompt, steps: steps)
|
|
||||||
monkeypatch.setattr(routes, "_unsure_plan", lambda prompt, plan: plan)
|
|
||||||
routes.set_editor_planner(lambda messages, owner: json.dumps({
|
|
||||||
"reply": "Generate", "steps": [{"op": "generate_image", "params": {
|
|
||||||
"prompt": "a cow", "model": "gpt-image-1",
|
|
||||||
}}],
|
|
||||||
}))
|
|
||||||
app = FastAPI()
|
|
||||||
app.include_router(routes.setup_editor_agent_routes())
|
|
||||||
try:
|
|
||||||
with TestClient(app) as client:
|
|
||||||
response = client.post("/api/editor/agent", json={
|
|
||||||
"prompt": "create a new image of a cow",
|
|
||||||
"model": "edit-only-model", "generation_model": chosen,
|
|
||||||
})
|
|
||||||
assert response.status_code == 200, response.text
|
|
||||||
assert len(seen) == 1, response.text
|
|
||||||
assert seen[0].get("model", "") == chosen
|
|
||||||
finally:
|
|
||||||
routes.set_editor_planner(None)
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize('op,expected', [('generate_image','saved-generation'),('ai_edit_image','saved-editor')])
|
|
||||||
def test_quick_edit_agent_uses_saved_models_over_browser_and_planner(monkeypatch, op, expected):
|
|
||||||
seen = []
|
|
||||||
|
|
||||||
async def execute(self, content, ctx):
|
|
||||||
seen.append(json.loads(content))
|
|
||||||
return 'stub', {'exit_code':1, 'error':'stop before verification'}
|
|
||||||
|
|
||||||
monkeypatch.setattr('src.editor_image_settings.load_settings', lambda: {
|
|
||||||
'image_model':'saved-generation', 'image_edit_model':'saved-editor'})
|
|
||||||
monkeypatch.setattr(editor_tools.GenerateImageLayerTool, 'execute', execute)
|
|
||||||
monkeypatch.setattr(editor_tools.AiEditImageTool, 'execute', execute)
|
|
||||||
monkeypatch.setattr(routes, '_user', lambda request: None)
|
|
||||||
monkeypatch.setattr(routes.editor_agent_model, 'startup_probe', lambda: None)
|
|
||||||
monkeypatch.setattr(routes.style_guide, 'strengthen', lambda prompt, steps: steps)
|
|
||||||
monkeypatch.setattr(routes.style_guide, 'enforce_guards', lambda steps: steps)
|
|
||||||
monkeypatch.setattr(routes, '_image_provider_available', lambda owner: True)
|
|
||||||
monkeypatch.setattr(routes, '_plan_special', lambda *args: None)
|
|
||||||
monkeypatch.setattr(routes, '_ensure_generative', lambda prompt, steps: steps)
|
|
||||||
monkeypatch.setattr(routes, '_unsure_plan', lambda prompt, plan: plan)
|
|
||||||
routes.set_editor_planner(lambda messages, owner: json.dumps({
|
|
||||||
'reply':'Edit', 'steps':[{'op':op,'params':{'prompt':'a cow','model':'planner-model','size':'1024x1024'}}]}))
|
|
||||||
app = FastAPI()
|
|
||||||
app.include_router(routes.setup_editor_agent_routes())
|
|
||||||
try:
|
|
||||||
with TestClient(app) as client:
|
|
||||||
response = client.post('/api/editor/agent', json={
|
|
||||||
'prompt':'create an image of a cow','_use_image_settings':True,
|
|
||||||
'model':'stale-editor','generation_model':'stale-generator','state':{'width':1280,'height':720}})
|
|
||||||
assert response.status_code == 200, response.text
|
|
||||||
assert len(seen) == 1, response.text
|
|
||||||
assert seen[0]['model'] == expected
|
|
||||||
assert seen[0]['_use_image_settings'] is True
|
|
||||||
if op == 'generate_image':
|
|
||||||
assert seen[0]['size'] == '1280x720'
|
|
||||||
finally:
|
|
||||||
routes.set_editor_planner(None)
|
|
||||||
|
|
||||||
|
|
||||||
def test_quick_generation_route_uses_saved_setting(monkeypatch):
|
|
||||||
import asyncio
|
|
||||||
from routes.gallery import gallery_routes as gallery
|
|
||||||
from src import ai_interaction
|
|
||||||
|
|
||||||
seen = []
|
|
||||||
|
|
||||||
async def generate(content, **kwargs):
|
|
||||||
seen.append(json.loads(content))
|
|
||||||
return {'image_url':'data:image/png;base64,dGVzdA=='}
|
|
||||||
|
|
||||||
monkeypatch.setattr(gallery, 'require_privilege', lambda *args: 'alice')
|
|
||||||
monkeypatch.setattr(ai_interaction, 'do_generate_image', generate)
|
|
||||||
monkeypatch.setattr('src.editor_image_settings.load_settings', lambda: {'image_model':'saved-generation'})
|
|
||||||
|
|
||||||
class Request:
|
|
||||||
async def json(self):
|
|
||||||
return {'prompt':'a cow','model':'stale-generator','_use_image_settings':True,'width':1280,'height':720}
|
|
||||||
|
|
||||||
endpoint = next(route.endpoint for route in gallery.setup_gallery_routes().routes if route.path == '/api/image/generate')
|
|
||||||
result = asyncio.run(endpoint(Request()))
|
|
||||||
assert result['image'] == 'dGVzdA=='
|
|
||||||
assert seen[0]['model'] == 'saved-generation'
|
|
||||||
assert seen[0]['size'] == '1280x720'
|
|
||||||
|
|
||||||
|
|
||||||
def test_edit_default_uses_generation_only_when_edit_setting_is_empty(monkeypatch):
|
|
||||||
from src import editor_image_settings
|
|
||||||
monkeypatch.setattr(editor_image_settings, 'load_settings', lambda: {'image_model':'generator','image_edit_model':''})
|
|
||||||
assert editor_image_settings.editor_image_models() == {'generation':'generator','edit':'generator'}
|
|
||||||
|
|
||||||
|
|
||||||
def test_editor_image_models_per_user_isolation_and_fallback(tmp_path, monkeypatch):
|
|
||||||
from routes import prefs_routes
|
|
||||||
from src import editor_image_settings, settings
|
|
||||||
|
|
||||||
prefs_file = str(tmp_path / "user_prefs.json")
|
|
||||||
monkeypatch.setattr(prefs_routes, "PREFS_FILE", prefs_file)
|
|
||||||
|
|
||||||
global_config = {"image_model": "global-gen", "image_edit_model": "global-edit"}
|
|
||||||
monkeypatch.setattr(settings, "load_settings", lambda: global_config)
|
|
||||||
monkeypatch.setattr(editor_image_settings, "load_settings", lambda: global_config)
|
|
||||||
|
|
||||||
# Save per-user preferences
|
|
||||||
prefs_routes._save_for_user("alice", {"image_model": "alice-gen", "image_edit_model": "alice-edit"})
|
|
||||||
prefs_routes._save_for_user("bob", {"image_model": "bob-gen"}) # no edit model
|
|
||||||
|
|
||||||
# Alice gets Alice's distinct models
|
|
||||||
alice_models = editor_image_settings.editor_image_models(owner="alice")
|
|
||||||
assert alice_models == {"generation": "alice-gen", "edit": "alice-edit"}
|
|
||||||
|
|
||||||
# Bob gets Bob's generation model, and edit model falls back to global edit model
|
|
||||||
bob_models = editor_image_settings.editor_image_models(owner="bob")
|
|
||||||
assert bob_models == {"generation": "bob-gen", "edit": "global-edit"}
|
|
||||||
|
|
||||||
# When global edit model is empty, edit model falls back to Bob's generation model
|
|
||||||
empty_edit = {"image_model": "global-gen", "image_edit_model": ""}
|
|
||||||
monkeypatch.setattr(settings, "load_settings", lambda: empty_edit)
|
|
||||||
monkeypatch.setattr(editor_image_settings, "load_settings", lambda: empty_edit)
|
|
||||||
bob_fallback_models = editor_image_settings.editor_image_models(owner="bob")
|
|
||||||
assert bob_fallback_models == {"generation": "bob-gen", "edit": "bob-gen"}
|
|
||||||
|
|
||||||
# Restore global settings with global-edit
|
|
||||||
monkeypatch.setattr(settings, "load_settings", lambda: global_config)
|
|
||||||
monkeypatch.setattr(editor_image_settings, "load_settings", lambda: global_config)
|
|
||||||
|
|
||||||
# Charlie (no preferences) falls back to global admin settings
|
|
||||||
charlie_models = editor_image_settings.editor_image_models(owner="charlie")
|
|
||||||
assert charlie_models == {"generation": "global-gen", "edit": "global-edit"}
|
|
||||||
|
|
||||||
# Anonymous/unauthenticated owner falls back to global admin settings
|
|
||||||
anon_models = editor_image_settings.editor_image_models(owner="")
|
|
||||||
assert anon_models == {"generation": "global-gen", "edit": "global-edit"}
|
|
||||||
|
|
||||||
|
|
||||||
def test_non_admin_user_prefs_api_routing_and_isolation(tmp_path, monkeypatch):
|
|
||||||
import time
|
|
||||||
from routes import editor_agent_routes, prefs_routes
|
|
||||||
from src import editor_image_settings, settings
|
|
||||||
|
|
||||||
prefs_file = str(tmp_path / "user_prefs.json")
|
|
||||||
monkeypatch.setattr(prefs_routes, "PREFS_FILE", prefs_file)
|
|
||||||
|
|
||||||
global_config = {"image_model": "admin-global-gen", "image_edit_model": "admin-global-edit"}
|
|
||||||
monkeypatch.setattr(settings, "load_settings", lambda: global_config)
|
|
||||||
monkeypatch.setattr(editor_image_settings, "load_settings", lambda: global_config)
|
|
||||||
|
|
||||||
current_user = "alice"
|
|
||||||
app = FastAPI()
|
|
||||||
app.include_router(prefs_routes.setup_prefs_routes())
|
|
||||||
app.include_router(editor_agent_routes.setup_editor_agent_routes())
|
|
||||||
|
|
||||||
monkeypatch.setattr("src.auth_helpers.get_current_user", lambda request: current_user)
|
|
||||||
monkeypatch.setattr(prefs_routes, "get_current_user", lambda request: current_user)
|
|
||||||
monkeypatch.setattr(editor_agent_routes, "_user", lambda request: current_user)
|
|
||||||
monkeypatch.setattr(editor_agent_routes.editor_agent_model, "startup_probe", lambda: None)
|
|
||||||
monkeypatch.setattr(editor_agent_routes.style_guide, "strengthen", lambda prompt, steps: steps)
|
|
||||||
monkeypatch.setattr(editor_agent_routes.style_guide, "enforce_guards", lambda steps: steps)
|
|
||||||
monkeypatch.setattr(editor_agent_routes, "_image_provider_available", lambda owner: True)
|
|
||||||
monkeypatch.setattr(editor_agent_routes, "_plan_special", lambda *args: None)
|
|
||||||
monkeypatch.setattr(editor_agent_routes, "_ensure_generative", lambda prompt, steps: steps)
|
|
||||||
monkeypatch.setattr(editor_agent_routes, "_unsure_plan", lambda prompt, plan: plan)
|
|
||||||
|
|
||||||
seen = []
|
|
||||||
|
|
||||||
async def execute_gen(self, content, ctx):
|
|
||||||
seen.append(("gen", json.loads(content)))
|
|
||||||
return "stub", {"exit_code": 1, "error": "stop"}
|
|
||||||
|
|
||||||
async def execute_edit(self, content, ctx):
|
|
||||||
seen.append(("edit", json.loads(content)))
|
|
||||||
return "stub", {"exit_code": 1, "error": "stop"}
|
|
||||||
|
|
||||||
monkeypatch.setattr(editor_tools.GenerateImageLayerTool, "execute", execute_gen)
|
|
||||||
monkeypatch.setattr(editor_tools.AiEditImageTool, "execute", execute_edit)
|
|
||||||
|
|
||||||
editor_agent_routes.set_editor_planner(lambda messages, owner: json.dumps({
|
|
||||||
"reply": "Gen", "steps": [
|
|
||||||
{"op": "generate_image", "params": {"prompt": "cat", "model": "stale-planner", "size": "512x512"}}
|
|
||||||
]
|
|
||||||
}))
|
|
||||||
|
|
||||||
try:
|
|
||||||
with TestClient(app) as client:
|
|
||||||
# Alice configures her preferences via PUT /api/prefs/{key}
|
|
||||||
put_gen = client.put("/api/prefs/image_model", json={"value": "alice-fav-gen"})
|
|
||||||
assert put_gen.status_code == 200
|
|
||||||
put_edit = client.put("/api/prefs/image_edit_model", json={"value": "alice-fav-edit"})
|
|
||||||
assert put_edit.status_code == 200
|
|
||||||
|
|
||||||
# Verify GET /api/prefs reflects Alice's choices
|
|
||||||
get_prefs = client.get("/api/prefs")
|
|
||||||
assert get_prefs.status_code == 200
|
|
||||||
assert get_prefs.json()["image_model"] == "alice-fav-gen"
|
|
||||||
assert get_prefs.json()["image_edit_model"] == "alice-fav-edit"
|
|
||||||
|
|
||||||
# Alice invokes quick edit with _use_image_settings: True
|
|
||||||
res = client.post("/api/editor/agent", json={
|
|
||||||
"prompt": "generate a cat",
|
|
||||||
"_use_image_settings": True,
|
|
||||||
"model": "stale-browser-edit",
|
|
||||||
"generation_model": "stale-browser-gen",
|
|
||||||
})
|
|
||||||
assert res.status_code == 200
|
|
||||||
assert len(seen) == 1
|
|
||||||
# Authoritative Alice preference overrides stale planner and browser models
|
|
||||||
assert seen[0][1]["model"] == "alice-fav-gen"
|
|
||||||
|
|
||||||
# Now test Bob (who has no preferences) - should receive global admin settings
|
|
||||||
current_user = "bob"
|
|
||||||
seen.clear()
|
|
||||||
res_bob = client.post("/api/editor/agent", json={
|
|
||||||
"prompt": "generate a dog",
|
|
||||||
"_use_image_settings": True,
|
|
||||||
"model": "stale-browser-edit",
|
|
||||||
"generation_model": "stale-browser-gen",
|
|
||||||
})
|
|
||||||
assert res_bob.status_code == 200
|
|
||||||
assert len(seen) == 1
|
|
||||||
# Bob falls back to global default, not Alice's preference
|
|
||||||
assert seen[0][1]["model"] == "admin-global-gen"
|
|
||||||
finally:
|
|
||||||
editor_agent_routes.set_editor_planner(None)
|
|
||||||
|
|
||||||
|
|
||||||
def test_is_background_removal_intent_grammar_and_dos_safety():
|
|
||||||
from routes.editor_agent_routes import _is_background_removal_intent
|
|
||||||
import time
|
|
||||||
|
|
||||||
# Valid commands across all verbs and targets
|
|
||||||
assert _is_background_removal_intent("remove background")
|
|
||||||
assert _is_background_removal_intent("please remove the background")
|
|
||||||
assert _is_background_removal_intent("erase bg")
|
|
||||||
assert _is_background_removal_intent("delete backdrop")
|
|
||||||
assert _is_background_removal_intent("clear the bg from the active layer")
|
|
||||||
assert _is_background_removal_intent("strip background of current layer!")
|
|
||||||
assert _is_background_removal_intent("erase backdrop from selected layer...")
|
|
||||||
assert _is_background_removal_intent(" PLEASE REMOVE THE BG FROM THE SELECTED LAYER . ! ")
|
|
||||||
|
|
||||||
# Invalid commands rejected
|
|
||||||
assert not _is_background_removal_intent("")
|
|
||||||
assert not _is_background_removal_intent("remove foreground")
|
|
||||||
assert not _is_background_removal_intent("please blur the background")
|
|
||||||
assert not _is_background_removal_intent("remove background and make it dark")
|
|
||||||
assert not _is_background_removal_intent("remove")
|
|
||||||
assert not _is_background_removal_intent("background")
|
|
||||||
assert not _is_background_removal_intent("please remove")
|
|
||||||
assert not _is_background_removal_intent("remove the")
|
|
||||||
assert not _is_background_removal_intent("remove background from layer") # missing active/current/selected
|
|
||||||
assert not _is_background_removal_intent("remove background?") # unallowed punctuation
|
|
||||||
|
|
||||||
# ReDoS safety test: large repeated whitespace must run linearly in < 50ms
|
|
||||||
evil_padding = " \t " * 10000
|
|
||||||
evil_prompt = evil_padding + "please" + evil_padding + "remove" + evil_padding + "the" + evil_padding + "bg" + evil_padding + "!"
|
|
||||||
start = time.perf_counter()
|
|
||||||
assert _is_background_removal_intent(evil_prompt)
|
|
||||||
elapsed = time.perf_counter() - start
|
|
||||||
assert elapsed < 0.1, f"Intent parsing took too long: {elapsed:.4f}s (polynomial backtracking risk)"
|
|
||||||
|
|
||||||
evil_invalid = evil_padding + "remove" + evil_padding + "everything"
|
|
||||||
start = time.perf_counter()
|
|
||||||
assert not _is_background_removal_intent(evil_invalid)
|
|
||||||
elapsed_invalid = time.perf_counter() - start
|
|
||||||
assert elapsed_invalid < 0.1, f"Invalid intent parsing took too long: {elapsed_invalid:.4f}s"
|
|
||||||
|
|
||||||
|
|
||||||
def test_image_generation_global_disabled_user_enabled_rejected(tmp_path, monkeypatch):
|
|
||||||
import asyncio
|
|
||||||
from fastapi import HTTPException
|
|
||||||
from routes.gallery import gallery_routes as gallery
|
|
||||||
from routes import editor_agent_routes, prefs_routes
|
|
||||||
from src import ai_interaction, settings
|
|
||||||
|
|
||||||
prefs_file = str(tmp_path / "user_prefs.json")
|
|
||||||
monkeypatch.setattr(prefs_routes, "PREFS_FILE", prefs_file)
|
|
||||||
|
|
||||||
# Admin globally disables image generation
|
|
||||||
monkeypatch.setattr(settings, "load_settings", lambda: {"image_gen_enabled": False, "image_model": "gpt-image-1"})
|
|
||||||
|
|
||||||
# User 'alice' has image_gen_enabled = True in her preferences
|
|
||||||
prefs_routes._save_for_user("alice", {"image_gen_enabled": True, "image_model": "alice-model"})
|
|
||||||
|
|
||||||
# 1. get_user_setting must be False despite user preference
|
|
||||||
assert settings.get_user_setting("image_gen_enabled", owner="alice", default=True) is False
|
|
||||||
|
|
||||||
# 2. image_editing_provider_available must be False
|
|
||||||
assert ai_interaction.image_editing_provider_available(owner="alice") is False
|
|
||||||
|
|
||||||
# 3. Direct generation must be rejected with admin disabled message
|
|
||||||
direct_res = asyncio.run(ai_interaction.do_generate_image(
|
|
||||||
json.dumps({"prompt": "a dog"}), owner="alice"
|
|
||||||
))
|
|
||||||
assert "error" in direct_res
|
|
||||||
assert "disabled by the administrator" in direct_res["error"]
|
|
||||||
|
|
||||||
# 4. Gallery route must reject with 503
|
|
||||||
app = FastAPI()
|
|
||||||
app.include_router(gallery.setup_gallery_routes())
|
|
||||||
monkeypatch.setattr(gallery, "require_privilege", lambda request, priv: "alice")
|
|
||||||
with TestClient(app) as client:
|
|
||||||
gal_res = client.post("/api/image/generate", json={"prompt": "a dog"})
|
|
||||||
assert gal_res.status_code == 503
|
|
||||||
assert "switched off" in gal_res.json()["detail"].lower()
|
|
||||||
|
|
||||||
# 5. Editor agent route must be blocked
|
|
||||||
app_editor = FastAPI()
|
|
||||||
app_editor.include_router(editor_agent_routes.setup_editor_agent_routes())
|
|
||||||
monkeypatch.setattr(editor_agent_routes, "_user", lambda request: "alice")
|
|
||||||
routes.set_editor_planner(lambda messages, owner: json.dumps({
|
|
||||||
"reply": "Gen", "steps": [{"op": "generate_image", "params": {"prompt": "a dog"}}]
|
|
||||||
}))
|
|
||||||
try:
|
|
||||||
with TestClient(app_editor) as client:
|
|
||||||
ed_res = client.post("/api/editor/agent", json={"prompt": "create a dog"})
|
|
||||||
assert ed_res.status_code == 200
|
|
||||||
data = ed_res.json()
|
|
||||||
assert data.get("blocked") is True
|
|
||||||
assert data.get("needs_provider") is True
|
|
||||||
finally:
|
|
||||||
routes.set_editor_planner(None)
|
|
||||||
|
|
||||||
|
|
||||||
def test_image_generation_global_enabled_user_disabled_rejected(tmp_path, monkeypatch):
|
|
||||||
import asyncio
|
|
||||||
from routes.gallery import gallery_routes as gallery
|
|
||||||
from routes import editor_agent_routes, prefs_routes
|
|
||||||
from src import ai_interaction, settings
|
|
||||||
|
|
||||||
prefs_file = str(tmp_path / "user_prefs.json")
|
|
||||||
monkeypatch.setattr(prefs_routes, "PREFS_FILE", prefs_file)
|
|
||||||
|
|
||||||
# Admin globally enables image generation
|
|
||||||
monkeypatch.setattr(settings, "load_settings", lambda: {"image_gen_enabled": True, "image_model": "gpt-image-1"})
|
|
||||||
|
|
||||||
# User 'bob' has image_gen_enabled = False in his preferences
|
|
||||||
prefs_routes._save_for_user("bob", {"image_gen_enabled": False, "image_model": "bob-model"})
|
|
||||||
|
|
||||||
# 1. get_user_setting must be False per user preference
|
|
||||||
assert settings.get_user_setting("image_gen_enabled", owner="bob", default=True) is False
|
|
||||||
|
|
||||||
# 2. image_editing_provider_available must be False for bob
|
|
||||||
assert ai_interaction.image_editing_provider_available(owner="bob") is False
|
|
||||||
|
|
||||||
# 3. Direct generation must be rejected with user preference message
|
|
||||||
direct_res = asyncio.run(ai_interaction.do_generate_image(
|
|
||||||
json.dumps({"prompt": "a cat"}), owner="bob"
|
|
||||||
))
|
|
||||||
assert "error" in direct_res
|
|
||||||
assert "disabled by user preference" in direct_res["error"]
|
|
||||||
|
|
||||||
# 4. Gallery route must reject with 503
|
|
||||||
app = FastAPI()
|
|
||||||
app.include_router(gallery.setup_gallery_routes())
|
|
||||||
monkeypatch.setattr(gallery, "require_privilege", lambda request, priv: "bob")
|
|
||||||
with TestClient(app) as client:
|
|
||||||
gal_res = client.post("/api/image/generate", json={"prompt": "a cat"})
|
|
||||||
assert gal_res.status_code == 503
|
|
||||||
|
|
||||||
# 5. Editor agent route must be blocked for bob
|
|
||||||
app_editor = FastAPI()
|
|
||||||
app_editor.include_router(editor_agent_routes.setup_editor_agent_routes())
|
|
||||||
monkeypatch.setattr(editor_agent_routes, "_user", lambda request: "bob")
|
|
||||||
routes.set_editor_planner(lambda messages, owner: json.dumps({
|
|
||||||
"reply": "Gen", "steps": [{"op": "generate_image", "params": {"prompt": "a cat"}}]
|
|
||||||
}))
|
|
||||||
try:
|
|
||||||
with TestClient(app_editor) as client:
|
|
||||||
ed_res = client.post("/api/editor/agent", json={"prompt": "create a cat"})
|
|
||||||
assert ed_res.status_code == 200
|
|
||||||
data = ed_res.json()
|
|
||||||
assert data.get("blocked") is True
|
|
||||||
assert data.get("needs_provider") is True
|
|
||||||
finally:
|
|
||||||
routes.set_editor_planner(None)
|
|
||||||
|
|
||||||
|
|
||||||
def test_image_generation_authorized_generation_and_privilege_checks(tmp_path, monkeypatch):
|
|
||||||
import asyncio
|
|
||||||
from fastapi import HTTPException
|
|
||||||
from routes.gallery import gallery_routes as gallery
|
|
||||||
from routes import editor_agent_routes, prefs_routes
|
|
||||||
from src import ai_interaction, settings
|
|
||||||
|
|
||||||
prefs_file = str(tmp_path / "user_prefs.json")
|
|
||||||
monkeypatch.setattr(prefs_routes, "PREFS_FILE", prefs_file)
|
|
||||||
|
|
||||||
# Admin globally enables image generation
|
|
||||||
monkeypatch.setattr(settings, "load_settings", lambda: {"image_gen_enabled": True, "image_model": "gpt-image-1"})
|
|
||||||
|
|
||||||
# User 'charlie' has image generation enabled (no disabling pref)
|
|
||||||
prefs_routes._save_for_user("charlie", {"image_model": "charlie-gen"})
|
|
||||||
|
|
||||||
assert settings.get_user_setting("image_gen_enabled", owner="charlie", default=True) is True
|
|
||||||
|
|
||||||
# Check privilege enforcement: user lacking can_generate_images is forbidden
|
|
||||||
def require_privilege_mock(request, privilege):
|
|
||||||
if privilege == "can_generate_images":
|
|
||||||
raise HTTPException(403, "Permission denied")
|
|
||||||
return "charlie"
|
|
||||||
|
|
||||||
monkeypatch.setattr("src.auth_helpers.require_privilege", require_privilege_mock)
|
|
||||||
monkeypatch.setattr(gallery, "require_privilege", require_privilege_mock)
|
|
||||||
|
|
||||||
# Gallery checks privilege and returns 403
|
|
||||||
app = FastAPI()
|
|
||||||
app.include_router(gallery.setup_gallery_routes())
|
|
||||||
with TestClient(app) as client:
|
|
||||||
gal_res = client.post("/api/image/generate", json={"prompt": "a bird"})
|
|
||||||
assert gal_res.status_code == 403
|
|
||||||
assert "Permission denied" in gal_res.text
|
|
||||||
|
|
||||||
# Editor checks privilege on AI content and raises 403
|
|
||||||
app_editor = FastAPI()
|
|
||||||
app_editor.include_router(editor_agent_routes.setup_editor_agent_routes())
|
|
||||||
monkeypatch.setattr(editor_agent_routes, "_user", lambda request: "charlie")
|
|
||||||
monkeypatch.setattr(editor_agent_routes, "_image_provider_available", lambda owner: True)
|
|
||||||
routes.set_editor_planner(lambda messages, owner: json.dumps({
|
|
||||||
"reply": "Gen", "steps": [{"op": "generate_image", "params": {"prompt": "a bird"}}]
|
|
||||||
}))
|
|
||||||
try:
|
|
||||||
with TestClient(app_editor) as client:
|
|
||||||
ed_res = client.post("/api/editor/agent", json={"prompt": "create a bird"})
|
|
||||||
assert ed_res.status_code == 403
|
|
||||||
finally:
|
|
||||||
routes.set_editor_planner(None)
|
|
||||||
|
|
||||||
# Now grant privilege: generation proceeds
|
|
||||||
monkeypatch.setattr("src.auth_helpers.require_privilege", lambda request, priv: "charlie")
|
|
||||||
monkeypatch.setattr(gallery, "require_privilege", lambda request, priv: "charlie")
|
|
||||||
seen = []
|
|
||||||
async def fake_do_generate(content, **kwargs):
|
|
||||||
seen.append(kwargs)
|
|
||||||
return {"image_url": "data:image/png;base64,dGVzdA=="}
|
|
||||||
monkeypatch.setattr(ai_interaction, "do_generate_image", fake_do_generate)
|
|
||||||
|
|
||||||
with TestClient(app) as client:
|
|
||||||
gal_ok = client.post("/api/image/generate", json={"prompt": "a bird"})
|
|
||||||
assert gal_ok.status_code == 200
|
|
||||||
assert seen[0]["owner"] == "charlie"
|
|
||||||
|
|
||||||
|
|
||||||
def test_put_pref_clears_key_when_value_is_null(tmp_path, monkeypatch):
|
|
||||||
from routes import prefs_routes
|
|
||||||
from src import settings
|
|
||||||
|
|
||||||
prefs_file = str(tmp_path / "user_prefs.json")
|
|
||||||
monkeypatch.setattr(prefs_routes, "PREFS_FILE", prefs_file)
|
|
||||||
|
|
||||||
# Set initial preference
|
|
||||||
prefs_routes._save_for_user("admin_user", {"image_model": "old-admin-model"})
|
|
||||||
assert prefs_routes._load_for_user("admin_user").get("image_model") == "old-admin-model"
|
|
||||||
|
|
||||||
app = FastAPI()
|
|
||||||
app.include_router(prefs_routes.setup_prefs_routes())
|
|
||||||
monkeypatch.setattr("src.auth_helpers.get_current_user", lambda request: "admin_user")
|
|
||||||
monkeypatch.setattr(prefs_routes, "get_current_user", lambda request: "admin_user")
|
|
||||||
|
|
||||||
with TestClient(app) as client:
|
|
||||||
res = client.put("/api/prefs/image_model", json={"value": None})
|
|
||||||
assert res.status_code == 200
|
|
||||||
assert res.json() == {"key": "image_model", "value": None}
|
|
||||||
|
|
||||||
# Verify key is deleted from user's record
|
|
||||||
user_prefs = prefs_routes._load_for_user("admin_user")
|
|
||||||
assert "image_model" not in user_prefs
|
|
||||||
|
|
||||||
# Verify get_user_setting now falls back to global setting
|
|
||||||
monkeypatch.setattr(settings, "load_settings", lambda: {"image_model": "new-global-model"})
|
|
||||||
assert settings.get_user_setting("image_model", owner="admin_user") == "new-global-model"
|
|
||||||
@@ -49,9 +49,10 @@ def test_ctrl_zoom_shortcuts_drive_the_toolbar_and_override_the_browser():
|
|||||||
|
|
||||||
def test_ctrl_alt_l_opens_the_levels_adjustment():
|
def test_ctrl_alt_l_opens_the_levels_adjustment():
|
||||||
keyboard = (ROOT / "static/js/editor/keyboard-shortcuts.js").read_text(encoding="utf-8")
|
keyboard = (ROOT / "static/js/editor/keyboard-shortcuts.js").read_text(encoding="utf-8")
|
||||||
# Open layer-local controls without creating/committing a new adjustment layer.
|
# Photoshop-style chord: reuse the Image menu row so the dialog and the
|
||||||
|
# adjustment-layer plumbing stay in one place.
|
||||||
assert "e.altKey && e.code === 'KeyL'" in keyboard
|
assert "e.altKey && e.code === 'KeyL'" in keyboard
|
||||||
assert "openLayerAdjustment?.('levels');" in keyboard
|
assert "document.querySelector('[data-image-action=\"adj-levels\"]')" in keyboard
|
||||||
|
|
||||||
|
|
||||||
def test_ctrl_alt_shift_e_stamps_the_visible_layers():
|
def test_ctrl_alt_shift_e_stamps_the_visible_layers():
|
||||||
|
|||||||
@@ -1,992 +0,0 @@
|
|||||||
import os
|
|
||||||
from pathlib import Path
|
|
||||||
|
|
||||||
import pytest
|
|
||||||
|
|
||||||
try:
|
|
||||||
from playwright.sync_api import sync_playwright
|
|
||||||
except ImportError:
|
|
||||||
sync_playwright = None
|
|
||||||
|
|
||||||
ROOT = Path(__file__).resolve().parents[1]
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
|
||||||
def page():
|
|
||||||
if sync_playwright is None:
|
|
||||||
pytest.fail("playwright is required for editor browser tests. Install requirements-dev.txt")
|
|
||||||
with sync_playwright() as playwright:
|
|
||||||
launch_kwargs = {"headless": True, "args": ["--disable-dev-shm-usage", "--no-sandbox"]}
|
|
||||||
if os.environ.get("PLAYWRIGHT_CHROMIUM_EXECUTABLE_PATH"):
|
|
||||||
launch_kwargs["executable_path"] = os.environ["PLAYWRIGHT_CHROMIUM_EXECUTABLE_PATH"]
|
|
||||||
browser = playwright.chromium.launch(**launch_kwargs)
|
|
||||||
page = browser.new_page(viewport={"width": 1000, "height": 700})
|
|
||||||
|
|
||||||
def serve(route):
|
|
||||||
url = route.request.url
|
|
||||||
if '/api/' in url:
|
|
||||||
route.fulfill(status=404, body='{}', content_type='application/json')
|
|
||||||
return
|
|
||||||
relative = url.split('http://editor.test/', 1)[1].split('?', 1)[0]
|
|
||||||
if not relative:
|
|
||||||
route.fulfill(body='<html><body></body></html>', content_type='text/html')
|
|
||||||
elif (ROOT / relative).is_file():
|
|
||||||
route.fulfill(path=str(ROOT / relative))
|
|
||||||
else:
|
|
||||||
route.fulfill(status=404, body='Not found')
|
|
||||||
|
|
||||||
page.route('http://editor.test/**', serve)
|
|
||||||
page.goto('http://editor.test/')
|
|
||||||
yield page
|
|
||||||
browser.close()
|
|
||||||
|
|
||||||
|
|
||||||
def test_remove_background_sends_only_active_layer_and_preserves_others(page):
|
|
||||||
result = page.evaluate("""async () => {
|
|
||||||
const {state} = await import('/static/js/editor/state.js');
|
|
||||||
const {createApplyImageTool} = await import('/static/js/editor/ai-tool-runner.js');
|
|
||||||
function layer(id, color) {
|
|
||||||
const canvas = document.createElement('canvas'); canvas.width = canvas.height = 4;
|
|
||||||
const ctx = canvas.getContext('2d'); ctx.fillStyle = color; ctx.fillRect(0,0,4,4);
|
|
||||||
return {id, canvas, ctx, visible:true, locks:{}};
|
|
||||||
}
|
|
||||||
const red = layer('red','red'), blue = layer('blue','blue');
|
|
||||||
red.kind = 'placed'; red.placed = {sourceName:'original'};
|
|
||||||
state.layers = [red, blue]; state.activeLayerId = red.id; state.editorOpen = true;
|
|
||||||
const beforeBlue = blue.canvas.toDataURL(); const expected = red.canvas.toDataURL().split(',')[1];
|
|
||||||
let sent, snapshots = 0;
|
|
||||||
const clear = document.createElement('canvas'); clear.width = clear.height = 4;
|
|
||||||
window.fetch = async (url, opts) => {
|
|
||||||
sent = JSON.parse(opts.body);
|
|
||||||
state.activeLayerId = blue.id; // Changing selection during the request must not change its target.
|
|
||||||
return {ok:true, json:async () => ({image:clear.toDataURL()})};
|
|
||||||
};
|
|
||||||
const run = createApplyImageTool({
|
|
||||||
flatten: () => {throw Error('Must not flatten');}, saveState: () => snapshots++,
|
|
||||||
createLayer: () => {throw Error('Must not create a merged layer');},
|
|
||||||
composite: () => {}, renderLayerPanel: () => {}, deriveBusyLabel: () => 'Removing',
|
|
||||||
getSelectedAIEndpoint: () => ({}), spinnerModule: {}, uiModule: {showToast(){}},
|
|
||||||
});
|
|
||||||
await run('/api/image/remove-bg', {}, 'BG Removed', document.createElement('button'));
|
|
||||||
return {correctInput:sent.image === expected, blueUnchanged:blue.canvas.toDataURL() === beforeBlue,
|
|
||||||
count:state.layers.length, visible:state.layers.every(l => l.visible), snapshots,
|
|
||||||
alpha:red.ctx.getImageData(0,0,1,1).data[3], kind:red.kind, placed:red.placed};
|
|
||||||
}""")
|
|
||||||
assert result == dict(correctInput=True, blueUnchanged=True, count=2, visible=True, snapshots=1, alpha=0, kind='raster', placed=None)
|
|
||||||
|
|
||||||
|
|
||||||
def test_levels_shortcut_opens_controls_without_menu_commit(page):
|
|
||||||
result = page.evaluate("""async () => {
|
|
||||||
const {state} = await import('/static/js/editor/state.js');
|
|
||||||
const {wireKeyboardShortcuts} = await import('/static/js/editor/keyboard-shortcuts.js');
|
|
||||||
state.editorOpen = true;
|
|
||||||
let selected;
|
|
||||||
wireKeyboardShortcuts({toolbar:document.createElement('div'), toolKeyMap:{},
|
|
||||||
openLayerAdjustment:type => selected = type});
|
|
||||||
const event = new KeyboardEvent('keydown', {key:'l', code:'KeyL', ctrlKey:true, altKey:true,
|
|
||||||
bubbles:true, cancelable:true});
|
|
||||||
document.dispatchEvent(event);
|
|
||||||
return {selected, prevented:event.defaultPrevented};
|
|
||||||
}""")
|
|
||||||
assert result == dict(selected='levels', prevented=True)
|
|
||||||
|
|
||||||
|
|
||||||
def test_hue_saturation_shortcut_opens_adjustment(page):
|
|
||||||
result = page.evaluate("""async () => {
|
|
||||||
const {state} = await import('/static/js/editor/state.js');
|
|
||||||
const {wireKeyboardShortcuts} = await import('/static/js/editor/keyboard-shortcuts.js');
|
|
||||||
state.editorOpen = true;
|
|
||||||
let selected;
|
|
||||||
wireKeyboardShortcuts({toolbar:document.createElement('div'), toolKeyMap:{},
|
|
||||||
openLayerAdjustment:type => selected = type});
|
|
||||||
const event = new KeyboardEvent('keydown', {key:'U', code:'KeyU', ctrlKey:true, shiftKey:true,
|
|
||||||
bubbles:true, cancelable:true});
|
|
||||||
document.dispatchEvent(event);
|
|
||||||
return {selected, prevented:event.defaultPrevented};
|
|
||||||
}""")
|
|
||||||
assert result == dict(selected='hue-saturation', prevented=True)
|
|
||||||
|
|
||||||
|
|
||||||
def test_inner_shadow_clips_inside_and_reverses_direction(page):
|
|
||||||
result = page.evaluate("""async () => {
|
|
||||||
const {renderEffects} = await import('/static/js/editor/effects.js');
|
|
||||||
const source = document.createElement('canvas'); source.width=source.height=60;
|
|
||||||
const ctx=source.getContext('2d');ctx.fillStyle='white';ctx.fillRect(15,15,30,30);
|
|
||||||
function sample(inside,x) {
|
|
||||||
const out=renderEffects(source,[{type:'drop-shadow',params:{inside,x,y:0,blur:0,opacity:1,color:'#000000'}}]);
|
|
||||||
const c=out.getContext('2d');
|
|
||||||
return {left:[...c.getImageData(16,30,1,1).data],right:[...c.getImageData(43,30,1,1).data],
|
|
||||||
outside:[...c.getImageData(47,30,1,1).data]};
|
|
||||||
}
|
|
||||||
return {right:sample(true,6),left:sample(true,-6),outer:sample(false,6)};
|
|
||||||
}""")
|
|
||||||
assert result['right']['left'] == [0,0,0,255]
|
|
||||||
assert result['right']['right'] == [255,255,255,255]
|
|
||||||
assert result['left']['right'] == [0,0,0,255]
|
|
||||||
assert result['right']['outside'][3] == 0
|
|
||||||
assert result['outer']['outside'] == [0,0,0,255]
|
|
||||||
|
|
||||||
|
|
||||||
def test_cut_placed_image_rasterizes_once_without_confirmation(page):
|
|
||||||
page.route('http://editor.test/api/**', lambda route: route.fulfill(json={}))
|
|
||||||
page.evaluate("""async () => {
|
|
||||||
document.body.innerHTML='<div id="toast"></div><div id="gallery-editor-container"></div>';
|
|
||||||
const editor=await import('/static/js/galleryEditor.js');
|
|
||||||
const {state}=await import('/static/js/editor/state.js');
|
|
||||||
editor.openEditor(null,null,{w:32,h:32},'Cut test');
|
|
||||||
const layer=state.layers.at(-1);
|
|
||||||
layer.ctx.fillStyle='red';layer.ctx.fillRect(0,0,32,32);
|
|
||||||
layer.kind='placed';layer.placed={sourceName:'Original image'};
|
|
||||||
document.activeElement?.blur();
|
|
||||||
document.dispatchEvent(new KeyboardEvent('keydown',{key:'x',code:'KeyX',ctrlKey:true,bubbles:true,cancelable:true}));
|
|
||||||
}""")
|
|
||||||
page.wait_for_function("""async () => {
|
|
||||||
const {state}=await import('/static/js/editor/state.js');
|
|
||||||
return !!state.internalClipboard && state.layers.at(-1).ctx.getImageData(0,0,1,1).data[3]===0;
|
|
||||||
}""")
|
|
||||||
result=page.evaluate("""async () => {
|
|
||||||
const {state}=await import('/static/js/editor/state.js');
|
|
||||||
return {kind:state.layers.at(-1).kind,placed:state.layers.at(-1).placed,
|
|
||||||
copied:[...state.internalClipboard.getContext('2d').getImageData(0,0,1,1).data],
|
|
||||||
prompted:document.querySelector('#styled-confirm-modal')?.textContent?.includes('Rasterize layer')||false};
|
|
||||||
}""")
|
|
||||||
assert result==dict(kind='raster',placed=None,copied=[255,0,0,255],prompted=False)
|
|
||||||
|
|
||||||
|
|
||||||
def test_drop_shadow_dialog_inside_and_direction_apply(page):
|
|
||||||
page.route('http://editor.test/api/**', lambda route: route.fulfill(json={}))
|
|
||||||
page.add_style_tag(content=(ROOT/'static/css/13-image-editor.css').read_text())
|
|
||||||
page.add_style_tag(content=':root{--panel:#242426;--bg:#171719;--fg:#eee;--border:#454549;--red:#d47965;}body{background:var(--bg);color:var(--fg);font-family:Arial,sans-serif;}')
|
|
||||||
page.evaluate("""async () => {
|
|
||||||
document.body.innerHTML='<div id="toast"></div><div id="gallery-editor-container" style="height:680px"></div>';
|
|
||||||
const editor=await import('/static/js/galleryEditor.js');
|
|
||||||
editor.openEditor(null,null,{w:64,h:64},'Shadow test');
|
|
||||||
document.querySelector('[data-filter-action="effect-drop-shadow"]').click();
|
|
||||||
}""")
|
|
||||||
page.locator('.ge-filter-overlay select[data-key="inside"]').select_option('1')
|
|
||||||
page.locator('.ge-filter-overlay input[data-key="angle"]').evaluate("el=>{el.value='90';el.dispatchEvent(new Event('input',{bubbles:true}));}")
|
|
||||||
page.screenshot(path='/tmp/editor-shadow-inside-direction.png')
|
|
||||||
page.locator('.ge-filter-overlay [data-action="apply"]').click()
|
|
||||||
page.wait_for_function("""async () => {
|
|
||||||
const {state}=await import('/static/js/editor/state.js');
|
|
||||||
return state.layers.at(-1).effects.length===1;
|
|
||||||
}""")
|
|
||||||
result=page.evaluate("""async () => {
|
|
||||||
const {state}=await import('/static/js/editor/state.js');
|
|
||||||
const {inside,x,y}=state.layers.at(-1).effects[0].params;
|
|
||||||
return {inside,x,y};
|
|
||||||
}""")
|
|
||||||
assert result==dict(inside=True,x=0,y=-7)
|
|
||||||
|
|
||||||
|
|
||||||
def test_reorder_rebuilds_effect_rows_under_their_parent(page):
|
|
||||||
result=page.evaluate("""async () => {
|
|
||||||
const {state}=await import('/static/js/editor/state.js');
|
|
||||||
const {createLayerPanelRenderer}=await import('/static/js/editor/layer-panel.js');
|
|
||||||
document.body.innerHTML='<div id="ge-layers-list"></div>';
|
|
||||||
state.container=document.body;state.editorOpen=true;
|
|
||||||
function layer(id,effects=[]) {
|
|
||||||
const canvas=document.createElement('canvas');canvas.width=canvas.height=16;
|
|
||||||
return {id,name:id,kind:'raster',canvas,ctx:canvas.getContext('2d'),visible:true,
|
|
||||||
opacity:1,locks:{},effects,masks:[],adjLayers:[]};
|
|
||||||
}
|
|
||||||
const a=layer('a',[{id:'shadow-a',type:'drop-shadow',params:{}}]), b=layer('b');
|
|
||||||
state.layers=[a,b];state.activeLayerId=a.id;state.layerOffsets=new Map();state.layerGroups=[];
|
|
||||||
let options;
|
|
||||||
const renderer=createLayerPanelRenderer({composite(){},saveState(){},
|
|
||||||
dragSortModule:{enable:(id,selector,config)=>{options=config;}},uiModule:{showToast(){}}});
|
|
||||||
renderer.render();
|
|
||||||
const list=document.getElementById('ge-layers-list');
|
|
||||||
const row=id=>list.querySelector(`.ge-layer-item[data-layer-id="${id}"]`);
|
|
||||||
list.insertBefore(row('a'),row('b'));
|
|
||||||
options.onReorder([...list.querySelectorAll('.ge-layer-item[data-layer-id]')]);
|
|
||||||
return {order:state.layers.map(layer=>layer.id),excluded:options.excludeSelector,
|
|
||||||
rows:[...list.children].map(el=>el.dataset.layerId||el.dataset.effectId)};
|
|
||||||
}""")
|
|
||||||
assert result==dict(order=['b','a'],excluded='.ge-adj-sub-item',rows=['a','shadow-a','b'])
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize('tool', ['marquee','lasso'])
|
|
||||||
@pytest.mark.parametrize('pointer_type', ['mouse','pen','touch'])
|
|
||||||
@pytest.mark.parametrize('pan', [False,True])
|
|
||||||
def test_selections_start_outside_canvas_unless_explicitly_panning(page,tool,pointer_type,pan):
|
|
||||||
result=page.evaluate("""async ({tool,pointerType,pan}) => {
|
|
||||||
const {state}=await import('/static/js/editor/state.js');
|
|
||||||
const {wireCanvasEvents}=await import('/static/js/editor/canvas-events.js');
|
|
||||||
document.body.innerHTML='<div id="area"><canvas width="100" height="100"></canvas><button>Chrome</button></div>';
|
|
||||||
const area=document.getElementById('area');
|
|
||||||
state.mainCanvas=area.querySelector('canvas');state.tool=tool;
|
|
||||||
state.editorOpen=true;state.spacePanActive=pan;state.panX=state.panY=0;
|
|
||||||
let starts=0,moves=0,ends=0;
|
|
||||||
wireCanvasEvents({canvasArea:area,beginDraw:()=>starts++,continueDraw:()=>moves++,
|
|
||||||
endDraw:()=>ends++,updateBrushCursor(){},syncZoomControls(){}});
|
|
||||||
function pointer(type,target=area) {
|
|
||||||
target.dispatchEvent(new PointerEvent(type,{pointerId:5,pointerType,button:0,
|
|
||||||
clientX:type==='pointerdown'?10:30,clientY:type==='pointerdown'?10:30,bubbles:true,cancelable:true}));
|
|
||||||
}
|
|
||||||
pointer('pointerdown');
|
|
||||||
if(pointerType==='mouse')area.dispatchEvent(new MouseEvent('mousedown',{button:0,bubbles:true}));
|
|
||||||
pointer('pointermove');
|
|
||||||
if(pointerType==='mouse')window.dispatchEvent(new MouseEvent('mousemove'));
|
|
||||||
pointer('pointerup');
|
|
||||||
if(pointerType==='mouse')window.dispatchEvent(new MouseEvent('mouseup'));
|
|
||||||
const after={starts,moves,ends,panX:state.panX};
|
|
||||||
pointer('pointerdown',area.querySelector('button'));
|
|
||||||
return {...after,chromeStarted:starts!==after.starts};
|
|
||||||
}""",dict(tool=tool,pointerType=pointer_type,pan=pan))
|
|
||||||
assert result['starts']==(0 if pan else 1)
|
|
||||||
assert result['panX']==(20 if pan else 0)
|
|
||||||
assert not result['chromeStarted']
|
|
||||||
if not pan:
|
|
||||||
assert result['moves']==1
|
|
||||||
assert result['ends']==1
|
|
||||||
|
|
||||||
|
|
||||||
def test_marquee_drag_from_workspace_creates_clipped_selection(page):
|
|
||||||
result=page.evaluate("""async () => {
|
|
||||||
const {state}=await import('/static/js/editor/state.js');
|
|
||||||
const {wireCanvasEvents}=await import('/static/js/editor/canvas-events.js');
|
|
||||||
const {createMarqueeTool}=await import('/static/js/editor/tools/marquee.js');
|
|
||||||
document.body.innerHTML='<div id="area"><canvas width="100" height="100" style="position:absolute;left:100px;top:100px;width:100px;height:100px"></canvas></div>';
|
|
||||||
const area=document.getElementById('area');state.mainCanvas=area.querySelector('canvas');
|
|
||||||
state.tool='marquee';state.imgWidth=state.imgHeight=100;state.editorOpen=true;
|
|
||||||
state.spacePanActive=false;state.panX=state.panY=0;state.wandMask=null;
|
|
||||||
const layer={id:'a'};state.layers=[layer];state.activeLayerId='a';state.layerOffsets=new Map();
|
|
||||||
const tool=createMarqueeTool({activeLayer:()=>layer,saveState(){},composite(){},drawOverlay(){},syncSelectionUi(){}});
|
|
||||||
wireCanvasEvents({canvasArea:area,beginDraw:e=>tool.begin(e),continueDraw:e=>tool.drag(e),
|
|
||||||
endDraw:e=>tool.end(e),updateBrushCursor(){},syncZoomControls(){}});
|
|
||||||
area.dispatchEvent(new PointerEvent('pointerdown',{pointerType:'mouse',pointerId:1,button:0,clientX:80,clientY:80,bubbles:true}));
|
|
||||||
area.dispatchEvent(new MouseEvent('mousedown',{button:0,clientX:80,clientY:80,bubbles:true}));
|
|
||||||
window.dispatchEvent(new MouseEvent('mousemove',{clientX:150,clientY:150}));
|
|
||||||
window.dispatchEvent(new MouseEvent('mouseup',{clientX:150,clientY:150}));
|
|
||||||
const ctx=state.wandMask.getContext('2d');
|
|
||||||
return {inside:ctx.getImageData(0,0,1,1).data[3],outside:ctx.getImageData(70,70,1,1).data[3],pan:state.panX};
|
|
||||||
}""")
|
|
||||||
assert result==dict(inside=255,outside=0,pan=0)
|
|
||||||
|
|
||||||
|
|
||||||
def test_save_menu_remains_clickable_after_png_export_and_window_raise(page):
|
|
||||||
page.add_style_tag(content='''
|
|
||||||
.modal {position:fixed;inset:0;z-index:20000;background:#222;}
|
|
||||||
#ge-save-menu-btn {position:absolute;right:20px;top:20px;}
|
|
||||||
.ge-save-menu {position:fixed;z-index:10005;width:200px;background:#333;}
|
|
||||||
.ge-save-menu[hidden] {display:none;}
|
|
||||||
.ge-save-menu button {display:block;width:100%;height:30px;}
|
|
||||||
''')
|
|
||||||
page.evaluate("""async () => {
|
|
||||||
const {wireTopbar}=await import('/static/js/editor/wire-topbar.js');
|
|
||||||
document.body.innerHTML='<div id="gallery-modal" class="modal"><div id="gallery-editor-container"><div class="ge-topbar"><div class="ge-save-wrap"><button id="ge-save-menu-btn">Save</button><div id="ge-save-menu" class="ge-save-menu" hidden><button id="ge-save">Save image</button><button id="ge-export-png">Save as PNG</button></div></div></div></div></div>';
|
|
||||||
window.pngExports=0;window.saves=0;
|
|
||||||
document.getElementById('ge-save').addEventListener('click',()=>window.saves++);
|
|
||||||
wireTopbar({registerDocClickAway(){},exportPNGFile:()=>window.pngExports++});
|
|
||||||
}""")
|
|
||||||
page.locator('#ge-save-menu-btn').click()
|
|
||||||
page.locator('#ge-export-png').click(timeout=2000)
|
|
||||||
page.evaluate("document.getElementById('gallery-modal').style.zIndex='50000'")
|
|
||||||
page.locator('#ge-save-menu-btn').click()
|
|
||||||
page.locator('#ge-save').click(timeout=2000)
|
|
||||||
assert page.evaluate('({png:window.pngExports,saves:window.saves})') == dict(png=1,saves=1)
|
|
||||||
|
|
||||||
|
|
||||||
def test_submenu_hover_aligns_to_row_and_flips_at_right_edge(page):
|
|
||||||
page.add_style_tag(content='''
|
|
||||||
#menu {position:absolute; left:780px; top:100px; width:180px;}
|
|
||||||
button {display:block; height:35px; width:180px;}
|
|
||||||
.ge-submenu {display:none; position:absolute; width:160px; height:120px; top:0; left:100%;}
|
|
||||||
.ge-menu-parent:hover + .ge-submenu {display:block;}
|
|
||||||
''')
|
|
||||||
page.evaluate("""async () => {
|
|
||||||
document.body.innerHTML = '<div id="menu"><button>First</button><button>Second</button>'
|
|
||||||
+ '<button class="ge-menu-parent">Blur ></button><div class="ge-submenu">Gaussian</div></div>';
|
|
||||||
const {wireSubmenuToggles} = await import('/static/js/editor/submenu-toggles.js');
|
|
||||||
wireSubmenuToggles(document.getElementById('menu'));
|
|
||||||
}""")
|
|
||||||
page.locator('.ge-menu-parent').hover()
|
|
||||||
row = page.locator('.ge-menu-parent').bounding_box()
|
|
||||||
panel = page.locator('.ge-submenu').bounding_box()
|
|
||||||
assert abs(panel['y'] - row['y']) <= 1
|
|
||||||
assert panel['x'] + panel['width'] <= row['x'] + 1
|
|
||||||
page.screenshot(path='/tmp/editor-submenu-alignment.png')
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize('prompt', ['remove bg', 'remove background', 'remove the background from the selected layer'])
|
|
||||||
def test_quick_edit_planner_uses_background_tool(prompt):
|
|
||||||
from routes.editor_agent_routes import _plan_special
|
|
||||||
steps, _, _ = _plan_special(prompt, {}, True)
|
|
||||||
assert steps == [{'op': 'ai.removeBackground', 'params': {}}]
|
|
||||||
|
|
||||||
|
|
||||||
def test_quick_edit_background_removal_precedes_creative_routing():
|
|
||||||
source = (ROOT / 'static/js/galleryEditor.js').read_text()
|
|
||||||
local = source.split('const _executeQuickEditLocal = async (prompt) => {', 1)[1]
|
|
||||||
assert local.index('if (_looksBackgroundRemoval(p))') < local.index('if (_looksRecolor(p))')
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize('run', [False, True])
|
|
||||||
def test_background_tool_suggestion_prepares_command_and_enter_submits(page, run):
|
|
||||||
source = (ROOT / 'static/js/galleryEditor.js').read_text()
|
|
||||||
picker = 'const pickSuggestion = ' + source.split('const pickSuggestion = ', 1)[1].split(" wrap.addEventListener('pointerdown'", 1)[0]
|
|
||||||
assert "runPrompt: id === 'rembg' ? 'remove background' : null" in source
|
|
||||||
result = page.evaluate("""({picker, run}) => {
|
|
||||||
const suggestionItems = [{toolId:'rembg', label:'Bg Remove', runPrompt:'remove background'}];
|
|
||||||
const input = document.createElement('input'); input.value = 'bg';
|
|
||||||
let submitted = 0, selected = null, clearSynced = false;
|
|
||||||
const form = {requestSubmit: () => submitted++};
|
|
||||||
const _clickToolButton = id => selected = id;
|
|
||||||
const _setAiCommandStatus = () => {};
|
|
||||||
const hideSuggestions = () => {};
|
|
||||||
const syncClearButton = () => clearSynced = true;
|
|
||||||
eval(picker + '; pickSuggestion(0, run);');
|
|
||||||
return {prompt:input.value, submitted, selected, clearSynced};
|
|
||||||
}""", {'picker':picker, 'run':run})
|
|
||||||
assert result == dict(prompt='remove background', submitted=int(run), selected='rembg', clearSynced=True)
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize('tool', ['dodge', 'burn'])
|
|
||||||
def test_retouch_invalidates_adjustment_preview_and_stops_before_undo(page, tool):
|
|
||||||
result = page.evaluate("""async tool => {
|
|
||||||
const {state} = await import('/static/js/editor/state.js');
|
|
||||||
const {createStrokePipeline} = await import('/static/js/editor/stroke-pipeline.js');
|
|
||||||
const {renderLayerWithAdjLayers} = await import('/static/js/editor/fx/pixel-pass.js');
|
|
||||||
const canvas = document.createElement('canvas'); canvas.width = canvas.height = 30;
|
|
||||||
const ctx = canvas.getContext('2d'); ctx.fillStyle = 'rgb(100,100,100)'; ctx.fillRect(0,0,30,30);
|
|
||||||
const layer = {id:'target', canvas, ctx, adjLayers:[{id:'adj', type:'brightness-contrast',
|
|
||||||
params:{brightness:1.1,contrast:1}, visible:true, opacity:1}]};
|
|
||||||
state.layers = [layer]; state.layerOffsets = new Map(); state.tool = tool;
|
|
||||||
Object.assign(state,{brushSize:12, brushOpacity:100, brushFlow:100, brushSoftness:0,
|
|
||||||
brushSpacing:20, brushSmoothing:0, dodgeBurnIntensity:50});
|
|
||||||
const original = ctx.getImageData(0,0,30,30);
|
|
||||||
const pixel = () => renderLayerWithAdjLayers(layer).getContext('2d').getImageData(15,15,1,1).data[0];
|
|
||||||
const before = pixel();
|
|
||||||
const pipeline = createStrokePipeline({activeLayer:()=>layer, getActiveMaskLayer:()=>null, composite:()=>{}});
|
|
||||||
pipeline.beginStroke({x:15,y:15,pressure:1},tool);
|
|
||||||
const during = pixel();
|
|
||||||
pipeline.endStroke(null); // Finish before Undo restores the saved pixels.
|
|
||||||
ctx.putImageData(original,0,0); layer._adjFinalKey = null; layer._adjCacheKey = null;
|
|
||||||
pipeline.endStroke({x:28,y:28,pressure:1}); // A late pointer-up cannot replay this stroke.
|
|
||||||
return {before,during,after:pixel(),corner:ctx.getImageData(28,28,1,1).data[0]};
|
|
||||||
}""", tool)
|
|
||||||
assert result['during'] > result['before'] if tool == 'dodge' else result['during'] < result['before']
|
|
||||||
assert result['after'] == result['before']
|
|
||||||
assert result['corner'] == 100
|
|
||||||
|
|
||||||
|
|
||||||
def test_undo_finishes_active_stroke_before_restoring_history():
|
|
||||||
source = (ROOT / 'static/js/galleryEditor.js').read_text()
|
|
||||||
undo = source.split('function undo() {', 1)[1].split('function redo()', 1)[0]
|
|
||||||
assert undo.index('if (state.drawing) _strokeTool.tryEnd();') < undo.index('state.undoStack.pop()')
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize('width,height', [(1000,700), (390,844)])
|
|
||||||
def test_filter_submenu_rows_have_equal_heights_with_and_without_icons(page, width, height):
|
|
||||||
page.set_viewport_size({'width':width,'height':height})
|
|
||||||
page.add_style_tag(content=(ROOT / 'static/css/13-image-editor.css').read_text())
|
|
||||||
page.evaluate("""() => {
|
|
||||||
document.body.innerHTML = '<div class="ge-submenu" style="display:block;left:8px;top:8px;">'
|
|
||||||
+ '<button class="dropdown-item-compact"><span>Plain</span></button>'
|
|
||||||
+ '<button class="dropdown-item-compact"><svg width="24" height="24"><circle cx="12" cy="12" r="8"/></svg><span>Icon</span></button>'
|
|
||||||
+ '<button class="dropdown-item-compact"><span>Longer label</span><svg width="10" height="10"></svg></button></div>';
|
|
||||||
}""")
|
|
||||||
heights = page.locator('.dropdown-item-compact').evaluate_all('(rows)=>rows.map(row=>row.getBoundingClientRect().height)')
|
|
||||||
assert heights == ([28]*3 if width > 820 else [32]*3)
|
|
||||||
page.screenshot(path=f'/tmp/editor-filter-row-heights-{width}.png')
|
|
||||||
|
|
||||||
|
|
||||||
def test_ai_settings_escape_closes_only_the_dialog(page):
|
|
||||||
page.route('**/api/auth/settings', lambda route: route.fulfill(json={'image_model':'', 'image_edit_model':''}))
|
|
||||||
result = page.evaluate("""async () => {
|
|
||||||
const {createAiSettings} = await import('/static/js/editor/ai-settings.js');
|
|
||||||
const container = document.createElement('div'); document.body.append(container);
|
|
||||||
const settings = createAiSettings({container}); settings.open();
|
|
||||||
let reachedEditor = false;
|
|
||||||
document.addEventListener('keydown', () => reachedEditor = true, {capture:true, once:true});
|
|
||||||
document.dispatchEvent(new KeyboardEvent('keydown', {key:'Escape', bubbles:true, cancelable:true}));
|
|
||||||
const result = {closed:settings.element.hidden, reachedEditor}; settings.destroy();
|
|
||||||
return result;
|
|
||||||
}""")
|
|
||||||
assert result == dict(closed=True, reachedEditor=False)
|
|
||||||
|
|
||||||
|
|
||||||
def test_image_model_settings_sync_both_directions_and_survive_reopening(page):
|
|
||||||
saved = {'image_model':'openai/gpt-5-image', 'image_edit_model':'', 'image_quality':'medium', 'image_gen_enabled':True}
|
|
||||||
writes = []
|
|
||||||
user_prefs = {}
|
|
||||||
|
|
||||||
def settings_api(route):
|
|
||||||
if route.request.method == 'POST':
|
|
||||||
patch = route.request.post_data_json
|
|
||||||
writes.append(patch)
|
|
||||||
saved.update(patch)
|
|
||||||
route.fulfill(json=dict(saved))
|
|
||||||
|
|
||||||
def prefs_api(route):
|
|
||||||
url = route.request.url
|
|
||||||
if route.request.method == 'PUT':
|
|
||||||
key = url.split('/api/prefs/')[-1]
|
|
||||||
data = route.request.post_data_json or {}
|
|
||||||
val = data.get('value')
|
|
||||||
if val is None:
|
|
||||||
user_prefs.pop(key, None)
|
|
||||||
else:
|
|
||||||
user_prefs[key] = val
|
|
||||||
route.fulfill(status=200, json={'key': key, 'value': val})
|
|
||||||
return
|
|
||||||
route.fulfill(status=200, json=dict(user_prefs))
|
|
||||||
|
|
||||||
page.route('**/api/auth/settings', settings_api)
|
|
||||||
page.route('**/api/prefs**', prefs_api)
|
|
||||||
page.route('**/api/model-endpoints', lambda route: route.fulfill(json=[{
|
|
||||||
'is_enabled':True, 'online':True, 'model_type':'llm',
|
|
||||||
'models':['openai/gpt-5-image','openai/gpt-image-2'],
|
|
||||||
}]))
|
|
||||||
page.evaluate("""async () => {
|
|
||||||
document.body.innerHTML = '<select id="set-imgModelSelect"><option value="">Auto</option></select>'
|
|
||||||
+ '<select id="set-imgEditModelSelect"><option value="">Same</option></select>'
|
|
||||||
+ '<select id="set-imgQualitySelect"><option value="medium">Medium</option></select>'
|
|
||||||
+ '<input type="checkbox" id="set-imgEnabledToggle"><span id="set-imgSettingsMsg"></span>';
|
|
||||||
const {initImageSettings} = await import('/static/js/settings/imageModels.js');
|
|
||||||
await initImageSettings();
|
|
||||||
const {createAiSettings} = await import('/static/js/editor/ai-settings.js');
|
|
||||||
window.dialog = createAiSettings({container:document.body});
|
|
||||||
for (const id of ['ge-ai-model','ge-ai-command-model']) {
|
|
||||||
const select = document.getElementById(id);
|
|
||||||
for (const model of ['', 'openai/gpt-5-image','openai/gpt-image-2']) {
|
|
||||||
const option = document.createElement('option');
|
|
||||||
option.value = model ? 'https://openrouter.ai/api/v1::'+model : '';
|
|
||||||
option.textContent = model || 'Auto'; select.append(option);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
dialog.open();
|
|
||||||
}""")
|
|
||||||
page.wait_for_function("document.getElementById('ge-ai-model').value.endsWith('::openai/gpt-5-image')")
|
|
||||||
page.select_option('#ge-ai-model', 'https://openrouter.ai/api/v1::openai/gpt-image-2')
|
|
||||||
page.wait_for_function("document.getElementById('set-imgModelSelect').value === 'openai/gpt-image-2'")
|
|
||||||
assert saved['image_model'] == 'openai/gpt-image-2'
|
|
||||||
page.select_option('#set-imgEditModelSelect', 'openai/gpt-image-2')
|
|
||||||
page.wait_for_function("document.getElementById('ge-ai-command-model').value.endsWith('::openai/gpt-image-2')")
|
|
||||||
page.evaluate('dialog.close(); dialog.open();')
|
|
||||||
page.wait_for_function("document.getElementById('ge-ai-command-model').value.endsWith('::openai/gpt-image-2')")
|
|
||||||
assert saved['image_edit_model'] == 'openai/gpt-image-2'
|
|
||||||
assert len(writes) == 2
|
|
||||||
|
|
||||||
|
|
||||||
def test_non_admin_image_model_preferences_persist_and_reload(page):
|
|
||||||
global_settings = {
|
|
||||||
'image_model': 'global-default-gen',
|
|
||||||
'image_edit_model': '',
|
|
||||||
'image_quality': 'medium',
|
|
||||||
'image_gen_enabled': True,
|
|
||||||
}
|
|
||||||
user_prefs = {}
|
|
||||||
auth_posts = []
|
|
||||||
pref_puts = []
|
|
||||||
is_admin = False
|
|
||||||
|
|
||||||
def handle_auth_settings(route):
|
|
||||||
if route.request.method == 'POST':
|
|
||||||
auth_posts.append(route.request.post_data_json)
|
|
||||||
if not is_admin:
|
|
||||||
route.fulfill(status=403, json={'error': 'Admin access is required to change these settings.'})
|
|
||||||
return
|
|
||||||
global_settings.update(route.request.post_data_json)
|
|
||||||
route.fulfill(status=200, json=dict(global_settings))
|
|
||||||
return
|
|
||||||
route.fulfill(status=200, json=dict(global_settings))
|
|
||||||
|
|
||||||
def handle_prefs(route):
|
|
||||||
url = route.request.url
|
|
||||||
if route.request.method == 'PUT':
|
|
||||||
key = url.split('/api/prefs/')[-1]
|
|
||||||
data = route.request.post_data_json or {}
|
|
||||||
val = data.get('value')
|
|
||||||
pref_puts.append((key, val))
|
|
||||||
if val is None:
|
|
||||||
user_prefs.pop(key, None)
|
|
||||||
else:
|
|
||||||
user_prefs[key] = val
|
|
||||||
route.fulfill(status=200, json={'key': key, 'value': val})
|
|
||||||
return
|
|
||||||
route.fulfill(status=200, json=dict(user_prefs))
|
|
||||||
|
|
||||||
page.route('**/api/auth/settings', handle_auth_settings)
|
|
||||||
page.route('**/api/prefs**', handle_prefs)
|
|
||||||
page.route('**/api/model-endpoints', lambda route: route.fulfill(json=[{
|
|
||||||
'is_enabled': True, 'online': True, 'model_type': 'llm',
|
|
||||||
'models': ['global-default-gen', 'openai/gpt-5-image', 'openai/gpt-image-2'],
|
|
||||||
}]))
|
|
||||||
|
|
||||||
# Step 1: Non-admin loads image settings, selects a preference
|
|
||||||
page.evaluate("""async () => {
|
|
||||||
document.body.innerHTML = '<select id="set-imgModelSelect"><option value="">Auto</option></select>'
|
|
||||||
+ '<select id="set-imgEditModelSelect"><option value="">Same</option></select>'
|
|
||||||
+ '<select id="set-imgQualitySelect"><option value="medium">Medium</option></select>'
|
|
||||||
+ '<input type="checkbox" id="set-imgEnabledToggle" checked><span id="set-imgSettingsMsg"></span>';
|
|
||||||
const {initImageSettings} = await import('/static/js/settings/imageModels.js');
|
|
||||||
await initImageSettings();
|
|
||||||
}""")
|
|
||||||
|
|
||||||
# Non-admin changes generation model
|
|
||||||
page.select_option('#set-imgModelSelect', 'openai/gpt-image-2')
|
|
||||||
|
|
||||||
# Wait for status message to show 'Saved'
|
|
||||||
page.wait_for_function("document.getElementById('set-imgSettingsMsg').textContent === 'Saved'")
|
|
||||||
msg_color = page.evaluate("document.getElementById('set-imgSettingsMsg').style.color")
|
|
||||||
assert msg_color == 'var(--fg)', f"Expected var(--fg), got {msg_color}"
|
|
||||||
|
|
||||||
# Verify per-user preference was saved and global toggle was NOT propagated
|
|
||||||
assert user_prefs.get('image_model') == 'openai/gpt-image-2'
|
|
||||||
assert 'image_gen_enabled' not in user_prefs, "Global policy toggle must not be saved to user prefs"
|
|
||||||
|
|
||||||
# Step 2: Simulate page reload / re-initialization for non-admin
|
|
||||||
page.evaluate("""async () => {
|
|
||||||
document.getElementById('set-imgModelSelect').value = '';
|
|
||||||
const {initImageSettings} = await import('/static/js/settings/imageModels.js');
|
|
||||||
await initImageSettings();
|
|
||||||
}""")
|
|
||||||
page.wait_for_function("document.getElementById('set-imgModelSelect').value === 'openai/gpt-image-2'")
|
|
||||||
assert page.evaluate("document.getElementById('set-imgModelSelect').value") == 'openai/gpt-image-2'
|
|
||||||
|
|
||||||
# Step 3: Admin saves settings - pre-existing per-user preferences must be cleared
|
|
||||||
is_admin = True
|
|
||||||
page.evaluate("""async () => {
|
|
||||||
const {saveImagePreferences} = await import('/static/js/settings/imagePreferences.js');
|
|
||||||
await saveImagePreferences({ image_model: 'admin-chosen-model', image_edit_model: '', image_quality: 'high', image_gen_enabled: true });
|
|
||||||
}""")
|
|
||||||
# Verify per-user preference for image_model was cleared (PUT with value: null)
|
|
||||||
assert 'image_model' not in user_prefs, "Admin save must clear pre-existing per-user preference"
|
|
||||||
assert global_settings['image_model'] == 'admin-chosen-model'
|
|
||||||
|
|
||||||
# Admin reloads settings - receives newly saved global setting, not stale pref
|
|
||||||
page.evaluate("""async () => {
|
|
||||||
const {initImageSettings} = await import('/static/js/settings/imageModels.js');
|
|
||||||
await initImageSettings();
|
|
||||||
}""")
|
|
||||||
assert global_settings['image_model'] == 'admin-chosen-model'
|
|
||||||
|
|
||||||
|
|
||||||
def test_generation_places_result_without_stretching_or_refresh(page):
|
|
||||||
result = page.evaluate("""async () => {
|
|
||||||
const {createImageGenerationAction} = await import('/static/js/editor/image-generation.js');
|
|
||||||
const image = document.createElement('canvas'); image.width = 80; image.height = 20;
|
|
||||||
const ctx = image.getContext('2d'); ctx.fillStyle = 'red'; ctx.fillRect(0,0,80,20);
|
|
||||||
const selection = document.createElement('canvas'); selection.width = selection.height = 40;
|
|
||||||
selection.getContext('2d').fillRect(0,0,40,40);
|
|
||||||
let placed = null, body = null;
|
|
||||||
const run = createImageGenerationAction({
|
|
||||||
getDocument:()=>({key:'one',width:40,height:40,mask:selection}),
|
|
||||||
fetchImpl:async (url,opts) => {
|
|
||||||
body = JSON.parse(opts.body);
|
|
||||||
selection.getContext('2d').clearRect(0,0,40,40);
|
|
||||||
return {ok:true,json:async()=>({image:image.toDataURL()})};
|
|
||||||
},
|
|
||||||
placeResult:(canvas,name,mask)=>{
|
|
||||||
const data = canvas.getContext('2d');
|
|
||||||
placed = {width:canvas.width,height:canvas.height,left:data.getImageData(0,10,1,1).data[0],
|
|
||||||
right:data.getImageData(79,10,1,1).data[0], mask:mask.getContext('2d').getImageData(20,20,1,1).data[3]};
|
|
||||||
return 'new-layer';
|
|
||||||
},
|
|
||||||
});
|
|
||||||
const id = await run({prompt:'a cow',masked:true});
|
|
||||||
return {id,placed,body};
|
|
||||||
}""")
|
|
||||||
assert result['id'] == 'new-layer'
|
|
||||||
assert result['placed'] == dict(width=80,height=20,left=255,right=255,mask=255)
|
|
||||||
assert result['body'] == dict(_use_image_settings=True,prompt='a cow',width=40,height=40)
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize('action', ['cancel','timeout','invalid-image','document-switch'])
|
|
||||||
def test_generation_finishes_cleanly_on_failure_without_late_placement(page, action):
|
|
||||||
result = page.evaluate("""async action => {
|
|
||||||
const {createImageGenerationAction} = await import('/static/js/editor/image-generation.js');
|
|
||||||
let key = 'original', placements = 0;
|
|
||||||
const controller = new AbortController();
|
|
||||||
const run = createImageGenerationAction({
|
|
||||||
getDocument:()=>({key,width:40,height:40}), getSignal:()=>controller.signal,
|
|
||||||
timeoutMs:action === 'timeout' ? 20 : 2000,
|
|
||||||
fetchImpl:async (url,opts)=>{
|
|
||||||
if (action === 'timeout' || action === 'cancel') {
|
|
||||||
if (action === 'cancel') setTimeout(()=>controller.abort(),10);
|
|
||||||
return new Promise((resolve,reject)=>opts.signal.addEventListener('abort',
|
|
||||||
()=>reject(new DOMException('Aborted','AbortError')), {once:true}));
|
|
||||||
}
|
|
||||||
if (action === 'invalid-image') return {ok:true,json:async()=>({image:'bad-image-data'})};
|
|
||||||
const image = document.createElement('canvas'); image.width = image.height = 4;
|
|
||||||
key = 'different';
|
|
||||||
return {ok:true,json:async()=>({image:image.toDataURL()})};
|
|
||||||
},
|
|
||||||
placeResult:()=>placements++,
|
|
||||||
});
|
|
||||||
let error = '';
|
|
||||||
try {await run({prompt:'a cow'});} catch (e) {error = e.message;}
|
|
||||||
return {error,placements};
|
|
||||||
}""", action)
|
|
||||||
assert result['placements'] == 0
|
|
||||||
assert result['error']
|
|
||||||
if action == 'timeout':
|
|
||||||
assert 'timed out' in result['error']
|
|
||||||
if action == 'document-switch':
|
|
||||||
assert 'document changed' in result['error']
|
|
||||||
|
|
||||||
|
|
||||||
def test_generation_provider_error_is_not_retried_and_next_request_can_succeed(page):
|
|
||||||
result = page.evaluate("""async () => {
|
|
||||||
const {createImageGenerationAction} = await import('/static/js/editor/image-generation.js');
|
|
||||||
let calls = 0, placements = 0;
|
|
||||||
const image = document.createElement('canvas'); image.width = image.height = 4;
|
|
||||||
const run = createImageGenerationAction({
|
|
||||||
getDocument:()=>({key:'one',width:40,height:40}),
|
|
||||||
formatError:({status})=>status === 503 ? 'Generation is disabled' : 'error',
|
|
||||||
fetchImpl:async ()=> ++calls === 1
|
|
||||||
? {ok:false,status:503,json:async()=>({detail:'Disabled'})}
|
|
||||||
: {ok:true,json:async()=>({image:image.toDataURL()})},
|
|
||||||
placeResult:()=>++placements,
|
|
||||||
});
|
|
||||||
let error;
|
|
||||||
try {await run({prompt:'one'});} catch (e) {error = e.message;}
|
|
||||||
const callsAfterFailure = calls;
|
|
||||||
await run({prompt:'two'});
|
|
||||||
return {error,callsAfterFailure,calls,placements};
|
|
||||||
}""")
|
|
||||||
assert result == dict(error='Generation is disabled',callsAfterFailure=1,calls=2,placements=1)
|
|
||||||
|
|
||||||
|
|
||||||
def test_late_progress_response_cannot_restore_working_after_completion(page):
|
|
||||||
source = (ROOT / 'static/js/galleryEditor.js').read_text()
|
|
||||||
functions = source.split('function _startAiProgress(requestId) {',1)[1].split('function _setAiCommandStatus(',1)[0]
|
|
||||||
functions = 'function _startAiProgress(requestId) {' + functions
|
|
||||||
result = page.evaluate("""async functions => {
|
|
||||||
let _aiProgressTimer = null, _aiProgressEpoch = 0, status = 'Working';
|
|
||||||
let resolveRequest;
|
|
||||||
const fetch = () => new Promise(resolve => resolveRequest = resolve);
|
|
||||||
const _setAiProgress = () => {};
|
|
||||||
const _setAiCommandStatus = text => status = text;
|
|
||||||
const api = eval(functions + '; ({start:_startAiProgress,stop:_stopAiProgress});');
|
|
||||||
api.start('old-request');
|
|
||||||
api.stop(); status = 'Done';
|
|
||||||
resolveRequest({ok:true,json:async()=>({stage:'editing',progress:0.5})});
|
|
||||||
await new Promise(resolve=>setTimeout(resolve,20));
|
|
||||||
return status;
|
|
||||||
}""", functions)
|
|
||||||
assert result == 'Done'
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize('success', [True,False])
|
|
||||||
def test_quick_edit_queue_clears_working_on_completion(page, success):
|
|
||||||
source = (ROOT / 'static/js/galleryEditor.js').read_text()
|
|
||||||
pump = 'const pumpQuickEdit = ' + source.split('const pumpQuickEdit = ',1)[1].split(' const enqueueQuickEditJob',1)[0]
|
|
||||||
result = page.evaluate("""async ({pump,success}) => {
|
|
||||||
const {finishQuickEditJob} = await import('/static/js/editor/quick-edit-job.js');
|
|
||||||
const jobs = [{id:'one',prompt:'fill',status:'queued'}];
|
|
||||||
let pumping = false, _quickEditActiveJob = null, stopped = 0, terminal;
|
|
||||||
const renderJobStack = () => {};
|
|
||||||
const dismissJob = () => {};
|
|
||||||
const _stopAiProgress = () => stopped++;
|
|
||||||
const _setAiCommandStatus = (text,kind) => terminal = {text,kind};
|
|
||||||
const executeQuickEdit = async () => success ? {ok:true} : {ok:false,error:'Provider failed'};
|
|
||||||
await eval(pump + '; pumpQuickEdit();');
|
|
||||||
return {status:jobs[0].status,detail:jobs[0].detail,terminal,stopped,pumping};
|
|
||||||
}""", {'pump':pump,'success':success})
|
|
||||||
expected = 'done' if success else 'error'
|
|
||||||
message = 'Done' if success else 'Provider failed'
|
|
||||||
assert result == dict(status=expected,detail=message,terminal=dict(text=message,kind=expected),stopped=1,pumping=False)
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize('bridge', [False,True])
|
|
||||||
@pytest.mark.parametrize('instruction', [False,True])
|
|
||||||
@pytest.mark.parametrize('matching_ratio', [False,True])
|
|
||||||
def test_generated_layer_can_keep_pixels_outside_document_bounds(page, bridge, instruction, matching_ratio):
|
|
||||||
source = (ROOT / 'static/js/galleryEditor.js').read_text()
|
|
||||||
placement = 'const _placeResultLayer = ' + source.split('const _placeResultLayer = ',1)[1].split(' const _generateImage',1)[0]
|
|
||||||
bridge_line = next(line for line in source.splitlines() if "'layers.placeImage':" in line)
|
|
||||||
result = page.evaluate("""async ({placement,bridgeLine,bridge,instruction,matchingRatio}) => {
|
|
||||||
const {imageResultGeometry} = await import('/static/js/editor/image-result-geometry.js');
|
|
||||||
const state = {imgWidth:matchingRatio ? 1280 : 40,imgHeight:matchingRatio ? 720 : 40,layers:[],layerOffsets:new Map()};
|
|
||||||
let saved = 0;
|
|
||||||
const _saveState = ()=>saved++;
|
|
||||||
const composite = ()=>{};
|
|
||||||
const _renderLayerPanel = ()=>{};
|
|
||||||
const createLayer = (name,w,h)=>{
|
|
||||||
const canvas = document.createElement('canvas'); canvas.width=w;canvas.height=h;
|
|
||||||
return {id:'new',name,canvas,ctx:canvas.getContext('2d')};
|
|
||||||
};
|
|
||||||
const image = document.createElement('canvas'); image.width=matchingRatio ? 1536 : 80;image.height=matchingRatio ? 864 : 20;
|
|
||||||
image.getContext('2d').fillRect(0,0,image.width,image.height);
|
|
||||||
if (bridge) {
|
|
||||||
const run = eval('({' + bridgeLine + '})')["layers.placeImage"].run;
|
|
||||||
await run({dataUrl:image.toDataURL(),nativeSize:true,masked:false,
|
|
||||||
mode:instruction ? 'instruction' : undefined,differenceMask:instruction});
|
|
||||||
} else {
|
|
||||||
eval(placement + '; _placeResultLayer(image,"Generated",null);');
|
|
||||||
}
|
|
||||||
const layer = state.layers[0];
|
|
||||||
return {width:layer.canvas.width,height:layer.canvas.height,offset:state.layerOffsets.get(layer.id),
|
|
||||||
outsideAlpha:layer.ctx.getImageData(79,10,1,1).data[3],saved};
|
|
||||||
}""", {'placement':placement,'bridgeLine':bridge_line,'bridge':bridge,'instruction':instruction,'matchingRatio':matching_ratio})
|
|
||||||
expected = dict(width=1280,height=720,offset=dict(x=0,y=0),outsideAlpha=255,saved=1) if matching_ratio else dict(width=80,height=20,offset=dict(x=-20,y=10),outsideAlpha=255,saved=1)
|
|
||||||
assert result == expected
|
|
||||||
|
|
||||||
|
|
||||||
def test_whole_image_edit_does_not_clip_native_result_to_select_all(page):
|
|
||||||
source = (ROOT / 'static/js/galleryEditor.js').read_text()
|
|
||||||
operation = 'const _aiInpaintSelection = ' + source.split('const _aiInpaintSelection = ',1)[1].split(' // Replace the background/scene',1)[0]
|
|
||||||
result = page.evaluate("""async operation => {
|
|
||||||
const mask = document.createElement('canvas'); mask.width=40;mask.height=40;
|
|
||||||
const canvas = document.createElement('canvas'); canvas.width=80;canvas.height=20;
|
|
||||||
const state = {wandMask:mask};
|
|
||||||
const activeLayer = ()=>({id:'one'});
|
|
||||||
const _aiEditCanvas = async ()=>({canvas,fullFrame:true});
|
|
||||||
const _placeResultLayer = (result,name,resultMask)=>({width:result.width,height:result.height,masked:!!resultMask});
|
|
||||||
return await eval(operation + '; _aiInpaintSelection("change the sky");');
|
|
||||||
}""", operation)
|
|
||||||
assert result == dict(width=80,height=20,masked=False)
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize('matching_ratio', [False,True])
|
|
||||||
def test_inpaint_panel_preserves_native_whole_edit_dimensions(page, matching_ratio):
|
|
||||||
source = (ROOT / 'static/js/editor/ai-inpaint.js').read_text()
|
|
||||||
body = 'const fullFrame = ' + source.split('const fullFrame = ',1)[1].split(' // Snapshot the AI result',1)[0]
|
|
||||||
result = page.evaluate("""async ({body,matchingRatio}) => {
|
|
||||||
const {imageResultGeometry} = await import('/static/js/editor/image-result-geometry.js');
|
|
||||||
const state = {imgWidth:matchingRatio ? 1280 : 40,imgHeight:matchingRatio ? 720 : 40,layerOffsets:new Map()};
|
|
||||||
const data = {full_frame:true}, layerName = 'Edit';
|
|
||||||
const createLayer = (name,w,h)=>{
|
|
||||||
const canvas=document.createElement('canvas');canvas.width=w;canvas.height=h;
|
|
||||||
return {id:'result',canvas,ctx:canvas.getContext('2d')};
|
|
||||||
};
|
|
||||||
const source = document.createElement('canvas'); source.width=matchingRatio ? 1536 : 80;source.height=matchingRatio ? 864 : 20;
|
|
||||||
source.getContext('2d').fillRect(0,0,source.width,source.height);
|
|
||||||
const resultImg = new Image(); resultImg.src=source.toDataURL(); await resultImg.decode();
|
|
||||||
return eval(body + '; ({width:resultLayer.canvas.width,height:resultLayer.canvas.height,offset:state.layerOffsets.get(resultLayer.id)});');
|
|
||||||
}""", {'body':body,'matchingRatio':matching_ratio})
|
|
||||||
assert result == (dict(width=1280,height=720,offset=dict(x=0,y=0)) if matching_ratio else dict(width=80,height=20,offset=dict(x=-20,y=10)))
|
|
||||||
|
|
||||||
|
|
||||||
def test_brush_softness_is_in_main_bar_and_saves_extended_value(page):
|
|
||||||
page.add_style_tag(content=(ROOT / 'static/css/13-image-editor.css').read_text())
|
|
||||||
result = page.evaluate("""async () => {
|
|
||||||
const {controlsHTML} = await import('/static/js/editor/build/controls.js');
|
|
||||||
const {wireStrokeToolSliders} = await import('/static/js/editor/stroke-tool-sliders.js');
|
|
||||||
const {state} = await import('/static/js/editor/state.js');
|
|
||||||
const bar = document.createElement('div');bar.className='ge-options-bar';
|
|
||||||
const controls = document.createElement('div');controls.className='ge-controls ge-controls--in-options';
|
|
||||||
controls.innerHTML=controlsHTML({color:'#000000',brushSize:40,wandTolerance:20});
|
|
||||||
for(const child of controls.children) if(child.id !== 'ge-brush-controls') child.style.display='none';
|
|
||||||
bar.append(controls);document.body.append(bar);
|
|
||||||
wireStrokeToolSliders();
|
|
||||||
const slider=document.getElementById('ge-brush-softness');
|
|
||||||
slider.value='1200';slider.dispatchEvent(new Event('input',{bubbles:true}));
|
|
||||||
return {inMore:!!slider.closest('#ge-brush-more-menu'),max:slider.max,value:state.brushSoftness,
|
|
||||||
visible:slider.getBoundingClientRect().height>0,label:document.getElementById('ge-brush-softness-label').textContent};
|
|
||||||
}""")
|
|
||||||
assert result == dict(inMore=False,max='1200',value=1200,visible=True,label='1200%')
|
|
||||||
page.screenshot(path='/tmp/editor-brush-softness-main-bar.png')
|
|
||||||
|
|
||||||
|
|
||||||
def test_extended_softness_produces_softer_pixels_not_just_larger_slider_values(page):
|
|
||||||
result = page.evaluate("""async () => {
|
|
||||||
const {state}=await import('/static/js/editor/state.js');
|
|
||||||
const {createStrokePipeline}=await import('/static/js/editor/stroke-pipeline.js');
|
|
||||||
const results=[];
|
|
||||||
for(const softness of [300,1200]) {
|
|
||||||
const canvas=document.createElement('canvas');canvas.width=canvas.height=100;
|
|
||||||
const layer={id:'brush',canvas,ctx:canvas.getContext('2d')};
|
|
||||||
Object.assign(state,{tool:'brush',brushSize:60,brushSoftness:softness,brushOpacity:100,brushFlow:100,
|
|
||||||
brushBlendMode:'source-over',color:'#000000',brushSpacing:15,brushSmoothing:0,
|
|
||||||
brushPixel:false,pressureSize:false,pressureOpacity:false,pressureFlow:false,layerOffsets:new Map()});
|
|
||||||
const pipeline=createStrokePipeline({activeLayer:()=>layer,getActiveMaskLayer:()=>null,composite:()=>{}});
|
|
||||||
pipeline.beginStroke({x:50,y:50,pressure:1},'brush');pipeline.endStroke(null);
|
|
||||||
const alpha=x=>layer.ctx.getImageData(x,50,1,1).data[3];
|
|
||||||
results.push({middle:alpha(65),center:alpha(50),outside:alpha(85)});
|
|
||||||
document.body.append(canvas);
|
|
||||||
}
|
|
||||||
return results;
|
|
||||||
}""")
|
|
||||||
assert result[1]['middle'] < result[0]['middle'] / 3
|
|
||||||
assert result[1]['center'] > 150
|
|
||||||
assert result[0]['outside'] == result[1]['outside'] == 0
|
|
||||||
page.screenshot(path='/tmp/editor-brush-softness-falloff.png')
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize('width', [1000,840])
|
|
||||||
def test_brush_numbers_do_not_overlap_after_real_panel_normalization(page,width):
|
|
||||||
page.set_viewport_size({'width':width,'height':700})
|
|
||||||
for stylesheet in ['static/css/13-image-editor.css','static/css/documents-gallery-editor.css']:
|
|
||||||
page.add_style_tag(content=(ROOT / stylesheet).read_text())
|
|
||||||
page.add_style_tag(content=':root{--bg:#171719;--panel:#242426;--fg:#eee;--red:#d47965;--border:#454549;}body{background:var(--bg);}')
|
|
||||||
result = page.evaluate("""async () => {
|
|
||||||
const {state}=await import('/static/js/editor/state.js');
|
|
||||||
const {controlsHTML}=await import('/static/js/editor/build/controls.js');
|
|
||||||
const {buildRightPanel}=await import('/static/js/editor/build/right-panel.js');
|
|
||||||
const {wireStrokeToolSliders}=await import('/static/js/editor/stroke-tool-sliders.js');
|
|
||||||
state.container=document.createElement('div');document.body.append(state.container);
|
|
||||||
state.brushSize=800;
|
|
||||||
const {controls}=buildRightPanel({controlsHTML,layerPanelHTML:()=>'<div></div>'});
|
|
||||||
controls.classList.add('ge-controls--in-options');
|
|
||||||
const bar=document.createElement('div');bar.className='ge-options-bar';bar.append(controls);state.container.append(bar);
|
|
||||||
for(const child of controls.children) if(child.id !== 'ge-brush-controls') child.style.display='none';
|
|
||||||
wireStrokeToolSliders();
|
|
||||||
const slider=document.getElementById('ge-brush-softness');
|
|
||||||
const elements=[document.querySelector('#ge-brush-softness-row label'),slider,
|
|
||||||
document.getElementById('ge-brush-softness-label'),document.querySelector('.ge-brush-size-row label'),
|
|
||||||
document.querySelector('.ge-brush-size-row input')];
|
|
||||||
const bounds=()=>elements.map(element=>{
|
|
||||||
const r=element.getBoundingClientRect();return {x:r.x,right:r.right,width:r.width};
|
|
||||||
});
|
|
||||||
slider.value='1';slider.dispatchEvent(new Event('input'));const small=bounds();
|
|
||||||
slider.value='1200';slider.dispatchEvent(new Event('input'));const large=bounds();
|
|
||||||
return {small,large,value:document.getElementById('ge-brush-softness-label').textContent};
|
|
||||||
}""")
|
|
||||||
assert result['value'] == '1200%'
|
|
||||||
assert all(bounds['width'] > 0 for bounds in result['large']), result
|
|
||||||
for left,right in zip(result['large'],result['large'][1:]):
|
|
||||||
assert left['right'] <= right['x'] + 1, result
|
|
||||||
for small,large in zip(result['small'],result['large']):
|
|
||||||
assert abs(small['x']-large['x']) <= 1, result
|
|
||||||
page.screenshot(path=f'/tmp/editor-brush-number-layout-{width}.png')
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize('fail_once', [False, True])
|
|
||||||
def test_new_project_create_initializes_canvas(page, fail_once):
|
|
||||||
errors = []
|
|
||||||
page.on('pageerror', lambda error: errors.append(str(error)))
|
|
||||||
page.route('http://editor.test/api/**', lambda route: route.fulfill(json={}))
|
|
||||||
page.add_style_tag(content='''
|
|
||||||
.modal {position:fixed;inset:0;display:flex;align-items:center;justify-content:center;z-index:250;pointer-events:none;}
|
|
||||||
.modal-content {pointer-events:auto;background:#333;padding:20px;}
|
|
||||||
#gallery-modal {z-index:700;}
|
|
||||||
#gallery-modal > .modal-content {width:90vw;height:90vh;}
|
|
||||||
''')
|
|
||||||
page.evaluate("""async () => {
|
|
||||||
document.body.innerHTML = '<div id="toast"></div><div class="modal" id="gallery-modal"><div class="modal-content"><div id="gallery-editor-container"></div></div></div>';
|
|
||||||
const editor = await import('/static/js/galleryEditor.js');
|
|
||||||
window.newProject = editor.openEditor(null, null, null, 'New canvas');
|
|
||||||
window.newProject.catch(error => { window.createError = error.stack; });
|
|
||||||
}""")
|
|
||||||
if fail_once:
|
|
||||||
page.evaluate("""() => {
|
|
||||||
const fill = CanvasRenderingContext2D.prototype.fillRect;
|
|
||||||
CanvasRenderingContext2D.prototype.fillRect = function(...args) {
|
|
||||||
if (this.canvas.width === 1024 && this.canvas.height === 1024) {
|
|
||||||
CanvasRenderingContext2D.prototype.fillRect = fill;
|
|
||||||
throw Error('Canvas allocation test failure');
|
|
||||||
}
|
|
||||||
return fill.apply(this, args);
|
|
||||||
};
|
|
||||||
}""")
|
|
||||||
page.locator('#ge-canvas-prompt-ok').click(timeout=3000)
|
|
||||||
if fail_once:
|
|
||||||
page.wait_for_function("window.createError")
|
|
||||||
assert 'Canvas allocation test failure' in page.evaluate('window.createError')
|
|
||||||
assert 'Could not create project' in page.locator('body').inner_text()
|
|
||||||
page.evaluate("""async () => {
|
|
||||||
window.createError = null;
|
|
||||||
const editor = await import('/static/js/galleryEditor.js');
|
|
||||||
window.newProject = editor.openEditor(null, null, null, 'New canvas');
|
|
||||||
window.newProject.catch(error => { window.createError = error.stack; });
|
|
||||||
}""")
|
|
||||||
page.locator('#ge-canvas-prompt-ok').click(timeout=3000)
|
|
||||||
page.wait_for_timeout(500)
|
|
||||||
result = page.evaluate("""async () => {
|
|
||||||
const {state} = await import('/static/js/editor/state.js');
|
|
||||||
return {error:window.createError, width:state.mainCanvas?.width,
|
|
||||||
height:state.mainCanvas?.height, layers:state.layers.map(layer=>layer.name)};
|
|
||||||
}""")
|
|
||||||
assert not errors, errors
|
|
||||||
assert not result.get('error'), result
|
|
||||||
assert result == {'error':None,'width':1024,'height':1024,'layers':['Background','Edit']}
|
|
||||||
|
|
||||||
|
|
||||||
def test_undo_clears_rembg_live_layer_and_edge_tuner_cannot_corrupt(page):
|
|
||||||
source = (ROOT / 'static/js/galleryEditor.js').read_text()
|
|
||||||
restore_fn = source.split('function _restoreState(snap) {', 1)[1].split('function undo() {', 1)[0]
|
|
||||||
assert 'state.rembgLiveLayer = null;' in restore_fn
|
|
||||||
assert 'state.rembgLiveSnap = null;' in restore_fn
|
|
||||||
|
|
||||||
page.route('http://editor.test/api/**', lambda route: route.fulfill(json={}))
|
|
||||||
result = page.evaluate("""async () => {
|
|
||||||
document.body.innerHTML = '<div id="toast"></div><div id="gallery-editor-container"></div>';
|
|
||||||
const editor = await import('/static/js/galleryEditor.js');
|
|
||||||
const {state} = await import('/static/js/editor/state.js');
|
|
||||||
editor.openEditor(null, null, {w: 16, h: 16}, 'Rembg edge tuner undo test');
|
|
||||||
|
|
||||||
const layer = state.layers.at(-1);
|
|
||||||
layer.ctx.fillStyle = 'rgba(0, 255, 0, 1)';
|
|
||||||
layer.ctx.fillRect(0, 0, 16, 16);
|
|
||||||
const originalPixel = [...layer.ctx.getImageData(0, 0, 1, 1).data];
|
|
||||||
|
|
||||||
// Save a valid pre-edit snapshot to undoStack
|
|
||||||
const origCanvas = document.createElement('canvas');
|
|
||||||
origCanvas.width = origCanvas.height = 16;
|
|
||||||
origCanvas.getContext('2d').drawImage(layer.canvas, 0, 0);
|
|
||||||
const preEditSnap = {
|
|
||||||
imgWidth: 16, imgHeight: 16,
|
|
||||||
activeLayerId: layer.id,
|
|
||||||
selectedLayerIds: [layer.id],
|
|
||||||
layers: [{
|
|
||||||
id: layer.id,
|
|
||||||
name: 'Layer 1',
|
|
||||||
canvas: origCanvas,
|
|
||||||
imageData: origCanvas.getContext('2d').getImageData(0, 0, 16, 16),
|
|
||||||
visible: true,
|
|
||||||
opacity: 1,
|
|
||||||
blendMode: 'source-over',
|
|
||||||
locks: {},
|
|
||||||
}],
|
|
||||||
};
|
|
||||||
state.undoStack.push(preEditSnap);
|
|
||||||
|
|
||||||
// Create tuner snapshot for background removal
|
|
||||||
const snapCanvas = document.createElement('canvas');
|
|
||||||
snapCanvas.width = snapCanvas.height = 16;
|
|
||||||
const snapCtx = snapCanvas.getContext('2d');
|
|
||||||
snapCtx.fillStyle = 'rgba(255, 0, 0, 1)';
|
|
||||||
snapCtx.fillRect(4, 4, 8, 8); // cutout shape
|
|
||||||
|
|
||||||
// Attach live tuner
|
|
||||||
state.rembgLiveLayer = layer;
|
|
||||||
state.rembgLiveSnap = snapCanvas;
|
|
||||||
const tunerAttached = state.rembgLiveLayer === layer && state.rembgLiveSnap === snapCanvas;
|
|
||||||
|
|
||||||
// Mutate layer pixels to simulate background removal result
|
|
||||||
layer.ctx.clearRect(0, 0, 16, 16);
|
|
||||||
layer.ctx.drawImage(snapCanvas, 0, 0);
|
|
||||||
|
|
||||||
// Trigger Undo via Ctrl+Z
|
|
||||||
document.dispatchEvent(new KeyboardEvent('keydown', {
|
|
||||||
key: 'z', code: 'KeyZ', ctrlKey: true, bubbles: true, cancelable: true,
|
|
||||||
}));
|
|
||||||
|
|
||||||
const clearedAfterUndo = state.rembgLiveLayer === null && state.rembgLiveSnap === null;
|
|
||||||
const restoredLayer = state.layers.at(-1);
|
|
||||||
const restoredPixel = [...restoredLayer.ctx.getImageData(0, 0, 1, 1).data];
|
|
||||||
|
|
||||||
// Now interact with edge tuner sliders after undo
|
|
||||||
const featherInput = document.getElementById('ge-rembg-feather');
|
|
||||||
const growInput = document.getElementById('ge-rembg-grow');
|
|
||||||
if (featherInput) {
|
|
||||||
featherInput.value = '5';
|
|
||||||
featherInput.dispatchEvent(new Event('input', { bubbles: true }));
|
|
||||||
}
|
|
||||||
if (growInput) {
|
|
||||||
growInput.value = '5';
|
|
||||||
growInput.dispatchEvent(new Event('input', { bubbles: true }));
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check pixel again after tuner events: restored pixels must remain untouched
|
|
||||||
const pixelAfterTuner = [...restoredLayer.ctx.getImageData(0, 0, 1, 1).data];
|
|
||||||
return {
|
|
||||||
tunerAttached,
|
|
||||||
clearedAfterUndo,
|
|
||||||
restoredPixel,
|
|
||||||
originalPixel,
|
|
||||||
pixelAfterTuner,
|
|
||||||
rembgLiveLayerNull: state.rembgLiveLayer === null,
|
|
||||||
rembgLiveSnapNull: state.rembgLiveSnap === null,
|
|
||||||
};
|
|
||||||
}""")
|
|
||||||
assert result['tunerAttached'] is True
|
|
||||||
assert result['clearedAfterUndo'] is True
|
|
||||||
assert result['rembgLiveLayerNull'] is True
|
|
||||||
assert result['rembgLiveSnapNull'] is True
|
|
||||||
assert result['restoredPixel'] == result['originalPixel']
|
|
||||||
assert result['pixelAfterTuner'] == result['originalPixel']
|
|
||||||
@@ -143,8 +143,8 @@ def test_crop_can_start_outside_the_canvas_without_panning():
|
|||||||
# image (so it can be pulled out past the edge) instead of the canvas-area
|
# image (so it can be pulled out past the edge) instead of the canvas-area
|
||||||
# pan grabbing the pointer and switching to hand mode.
|
# pan grabbing the pointer and switching to hand mode.
|
||||||
CE = (ROOT / "static/js/editor/canvas-events.js").read_text(encoding="utf-8")
|
CE = (ROOT / "static/js/editor/canvas-events.js").read_text(encoding="utf-8")
|
||||||
assert "['marquee', 'lasso', 'crop'].includes(state.tool)" in CE
|
assert "if (state.tool !== 'lasso' && state.tool !== 'crop') return;" in CE
|
||||||
assert "if (canStartOutside() && !directPan)" in CE
|
assert "(state.tool === 'lasso' || state.tool === 'crop') && !directPan" in CE
|
||||||
# Pressing editor chrome inside the area (e.g. the crop Apply panel) must
|
# Pressing editor chrome inside the area (e.g. the crop Apply panel) must
|
||||||
# not start a fresh crop.
|
# not start a fresh crop.
|
||||||
assert ".ge-crop-apply, .ge-adj-popup, .ge-transform-popup, .ge-fx-popup, .ge-inpaint-popup" in CE
|
assert ".ge-crop-apply, .ge-adj-popup, .ge-transform-popup, .ge-fx-popup, .ge-inpaint-popup" in CE
|
||||||
|
|||||||
@@ -32,12 +32,13 @@ def test_preflight_module_has_the_four_setup_steps():
|
|||||||
assert "required ready" in src
|
assert "required ready" in src
|
||||||
|
|
||||||
|
|
||||||
def test_editor_uses_ai_settings_without_redundant_setup():
|
def test_preflight_is_wired_into_the_editor():
|
||||||
editor = _read("static/js/galleryEditor.js")
|
editor = _read("static/js/galleryEditor.js")
|
||||||
assert "createPreflight" not in editor
|
assert "import { createPreflight } from './editor/preflight.js';" in editor
|
||||||
assert "setupBtn.id = 'ge-setup-btn';" not in editor
|
assert "createPreflight({ canvasArea })" in editor
|
||||||
assert "createAiSettings({ container })" in editor
|
assert "_preflight?.maybeOpen();" in editor
|
||||||
assert "aiBtn.addEventListener('click', () => _aiSettings?.open());" in editor
|
assert "setupBtn.id = 'ge-setup-btn';" in editor
|
||||||
|
assert "_preflight?.destroy();" in editor
|
||||||
|
|
||||||
|
|
||||||
def test_ai_models_exposes_status_for_the_checklist():
|
def test_ai_models_exposes_status_for_the_checklist():
|
||||||
|
|||||||
@@ -49,42 +49,7 @@ def test_generate_route_forwards_the_picked_model():
|
|||||||
assert 'model_choice = str((body or {}).get("model") or "").strip()' in route
|
assert 'model_choice = str((body or {}).get("model") or "").strip()' in route
|
||||||
assert 'payload["model"] = model_choice' in route
|
assert 'payload["model"] = model_choice' in route
|
||||||
assert "content = _json.dumps(payload)" in route
|
assert "content = _json.dumps(payload)" in route
|
||||||
operation = JS.split("'ai.generateImage':", 1)[1].split("\n", 1)[0]
|
assert "localStorage.getItem('ge-ai-command-model')" in JS
|
||||||
generation = (ROOT / 'static/js/editor/image-generation.js').read_text()
|
|
||||||
assert '_generateImage(p || {})' in operation
|
|
||||||
assert "_use_image_settings: true" in generation
|
|
||||||
assert "document.getElementById('ge-ai-model')" not in operation
|
|
||||||
assert "_getSelectedQuickEditModel()" not in operation
|
|
||||||
assert "localStorage.getItem" not in operation
|
|
||||||
|
|
||||||
|
|
||||||
def test_ai_settings_separates_generation_from_editing_without_setup():
|
|
||||||
settings = (ROOT / "static/js/editor/ai-settings.js").read_text()
|
|
||||||
assert 'id="ge-ai-model"' in settings
|
|
||||||
assert 'id="ge-ai-command-model"' in settings
|
|
||||||
assert "Image generation model" in settings
|
|
||||||
assert "createPreflight" not in JS
|
|
||||||
assert "ge-setup-btn';" not in JS.split("const aiBtn", 1)[0]
|
|
||||||
|
|
||||||
|
|
||||||
def test_generate_uses_live_picker_and_auto_edit_fallback():
|
|
||||||
import subprocess
|
|
||||||
|
|
||||||
module = (ROOT / "static/js/editor/ai-models.js").as_uri()
|
|
||||||
script = """
|
|
||||||
import assert from 'node:assert/strict';
|
|
||||||
const {resolveQuickEditModel} = await import(MODULE);
|
|
||||||
const selected = 'https://openrouter.ai/api/v1::openai/gpt-5-image';
|
|
||||||
const option = {value: selected, textContent: 'GPT-5 Image'};
|
|
||||||
const picker = {value: selected, selectedIndex: 0, options: [option]};
|
|
||||||
const fallback = {value: '', options: [option]};
|
|
||||||
globalThis.document = {getElementById: id => id === 'ge-ai-command-model' ? picker : fallback};
|
|
||||||
globalThis.localStorage = {getItem: () => {throw new Error('must not read stale storage');}};
|
|
||||||
assert.equal(resolveQuickEditModel().model, 'openai/gpt-5-image');
|
|
||||||
picker.value = '';
|
|
||||||
assert.equal(resolveQuickEditModel().model, '');
|
|
||||||
""".replace("MODULE", repr(module))
|
|
||||||
subprocess.run(["node", "--input-type=module", "-e", script], check=True)
|
|
||||||
|
|
||||||
|
|
||||||
def test_cloud_image_model_is_resolved_before_the_local_fallback():
|
def test_cloud_image_model_is_resolved_before_the_local_fallback():
|
||||||
|
|||||||
@@ -52,7 +52,7 @@ def test_quick_edit_flip_and_rotate_accept_numeric_degrees():
|
|||||||
|
|
||||||
def test_quick_edit_transforms_do_not_add_a_second_undo_snapshot():
|
def test_quick_edit_transforms_do_not_add_a_second_undo_snapshot():
|
||||||
start = EDITOR.index("if (/\\brotate\\b.*\\b180\\b")
|
start = EDITOR.index("if (/\\brotate\\b.*\\b180\\b")
|
||||||
end = EDITOR.index("if (!/\\b(text|title|caption|font|heading|subtitle)", start)
|
end = EDITOR.index("if (_looksBackgroundRemoval(p)) {", start)
|
||||||
transform_block = EDITOR[start:end]
|
transform_block = EDITOR[start:end]
|
||||||
assert "_saveState('Rotate" not in transform_block
|
assert "_saveState('Rotate" not in transform_block
|
||||||
assert "_saveState('Flip" not in transform_block
|
assert "_saveState('Flip" not in transform_block
|
||||||
@@ -372,10 +372,9 @@ def test_quick_edit_has_a_model_picker():
|
|||||||
assert "function _getSelectedQuickEditModel()" in EDITOR
|
assert "function _getSelectedQuickEditModel()" in EDITOR
|
||||||
assert "getModel: () => _getSelectedQuickEditModel()," in EDITOR
|
assert "getModel: () => _getSelectedQuickEditModel()," in EDITOR
|
||||||
# Populated from the provider list and persisted per user.
|
# Populated from the provider list and persisted per user.
|
||||||
assert "aiCommandSelect.innerHTML = '<option value=\"\">Same as generation</option>';" in models
|
assert "aiCommandSelect.innerHTML = '<option value=\"\">Auto (default)</option>';" in models
|
||||||
assert "localStorage.setItem('ge-ai-command-model'" not in models
|
assert "localStorage.setItem('ge-ai-command-model', aiCommandSelect.value)" in models
|
||||||
assert "localStorage.getItem('ge-ai-command-model')" not in models
|
assert "localStorage.getItem('ge-ai-command-model')" in models
|
||||||
assert 'saveImagePreferences({ [key]: model })' in AI_SETTINGS
|
|
||||||
# Styled.
|
# Styled.
|
||||||
assert ".ge-ai-settings-card" in STYLE
|
assert ".ge-ai-settings-card" in STYLE
|
||||||
assert ".ge-ai-command-model" in STYLE
|
assert ".ge-ai-command-model" in STYLE
|
||||||
@@ -699,7 +698,7 @@ def test_quick_edit_shows_a_real_progress_bar():
|
|||||||
assert "fetch(`/api/image/edit-progress?request_id=${encodeURIComponent(requestId)}`" in EDITOR
|
assert "fetch(`/api/image/edit-progress?request_id=${encodeURIComponent(requestId)}`" in EDITOR
|
||||||
# The edit request carries the request_id the server keys progress on.
|
# The edit request carries the request_id the server keys progress on.
|
||||||
assert "const requestId = 'qe-' + Date.now().toString(36)" in EDITOR
|
assert "const requestId = 'qe-' + Date.now().toString(36)" in EDITOR
|
||||||
assert "const body = JSON.stringify({ image, width: flat.width, height: flat.height, request_id: requestId, ...payload });" in EDITOR
|
assert "const body = JSON.stringify({ image, request_id: requestId, ...payload });" in EDITOR
|
||||||
assert "_startAiProgress(requestId);" in EDITOR
|
assert "_startAiProgress(requestId);" in EDITOR
|
||||||
assert "_stopAiProgress();" in EDITOR
|
assert "_stopAiProgress();" in EDITOR
|
||||||
# Styling exists.
|
# Styling exists.
|
||||||
@@ -719,7 +718,7 @@ def test_generation_503_is_handled_gracefully():
|
|||||||
# (returns 503). Surface a clear message and do not retry immediately.
|
# (returns 503). Surface a clear message and do not retry immediately.
|
||||||
assert "if (status === 503) return _GEN_DISABLED_MSG;" in EDITOR
|
assert "if (status === 503) return _GEN_DISABLED_MSG;" in EDITOR
|
||||||
assert "_GEN_DISABLED_MSG = 'Image generation is switched off on the server at the moment.'" in EDITOR
|
assert "_GEN_DISABLED_MSG = 'Image generation is switched off on the server at the moment.'" in EDITOR
|
||||||
assert "formatError: _generationErrorMessage" in EDITOR
|
assert "window.__geGenerationDisabled" in EDITOR
|
||||||
|
|
||||||
|
|
||||||
def test_sending_a_prompt_returns_focus_to_the_document():
|
def test_sending_a_prompt_returns_focus_to_the_document():
|
||||||
|
|||||||
@@ -5,7 +5,6 @@ import json
|
|||||||
from types import SimpleNamespace
|
from types import SimpleNamespace
|
||||||
|
|
||||||
import httpx
|
import httpx
|
||||||
import pytest
|
|
||||||
from PIL import Image
|
from PIL import Image
|
||||||
|
|
||||||
import routes.gallery_routes as gallery
|
import routes.gallery_routes as gallery
|
||||||
@@ -17,10 +16,7 @@ def png(image):
|
|||||||
return base64.b64encode(output.getvalue()).decode()
|
return base64.b64encode(output.getvalue()).decode()
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize('model', ['openai/gpt-5-image', 'openai/gpt-image-2'])
|
def test_openrouter_inpaint_keeps_unmasked_pixels(monkeypatch, caplog):
|
||||||
@pytest.mark.parametrize('saved_settings', [False, True])
|
|
||||||
@pytest.mark.parametrize('full_frame', [False, True])
|
|
||||||
def test_openrouter_inpaint_keeps_unmasked_pixels(monkeypatch, caplog, model, saved_settings, full_frame):
|
|
||||||
import logging
|
import logging
|
||||||
caplog.set_level(logging.DEBUG)
|
caplog.set_level(logging.DEBUG)
|
||||||
endpoint = SimpleNamespace(base_url='https://openrouter.ai/api/v1', api_key='test')
|
endpoint = SimpleNamespace(base_url='https://openrouter.ai/api/v1', api_key='test')
|
||||||
@@ -33,97 +29,36 @@ def test_openrouter_inpaint_keeps_unmasked_pixels(monkeypatch, caplog, model, sa
|
|||||||
monkeypatch.setattr(gallery, 'require_privilege', lambda *args: 'alice')
|
monkeypatch.setattr(gallery, 'require_privilege', lambda *args: 'alice')
|
||||||
monkeypatch.setattr(gallery, '_visible_image_endpoint_for_base', lambda *args: endpoint)
|
monkeypatch.setattr(gallery, '_visible_image_endpoint_for_base', lambda *args: endpoint)
|
||||||
monkeypatch.setattr('src.url_safety.check_outbound_url', lambda *args, **kwargs: (True, ''))
|
monkeypatch.setattr('src.url_safety.check_outbound_url', lambda *args, **kwargs: (True, ''))
|
||||||
monkeypatch.setattr('src.editor_image_settings.load_settings', lambda: {'image_model':'wrong-generation-model', 'image_edit_model':model})
|
|
||||||
def resolve_model(selected, **kwargs):
|
|
||||||
assert selected == model
|
|
||||||
assert kwargs['owner'] == 'alice'
|
|
||||||
return endpoint.base_url + '/chat/completions', model, {}
|
|
||||||
monkeypatch.setattr('src.ai_interaction._resolve_model', resolve_model)
|
|
||||||
requests = []
|
requests = []
|
||||||
|
|
||||||
def respond(request):
|
def respond(request):
|
||||||
requests.append(request)
|
requests.append(request)
|
||||||
assert str(request.url) == endpoint.base_url + ('/chat/completions' if model == 'openai/gpt-5-image' else '/images')
|
assert str(request.url) == endpoint.base_url + '/images'
|
||||||
payload = json.loads(request.content)
|
payload = json.loads(request.content)
|
||||||
assert payload['model'] == model
|
assert payload['model'] == 'openai/gpt-5-image'
|
||||||
if model == 'openai/gpt-5-image':
|
assert len(payload['input_references']) == 2
|
||||||
assert len(payload['messages'][0]['content']) == 3
|
|
||||||
else:
|
|
||||||
assert len(payload['input_references']) == 2
|
|
||||||
assert request.headers['authorization'] == 'Bearer test'
|
assert request.headers['authorization'] == 'Bearer test'
|
||||||
image = png(Image.new('RGB', (4, 4) if full_frame else (2, 1), 'blue'))
|
return httpx.Response(200, json={'data': [{'b64_json': png(Image.new('RGB', (2, 1), 'blue'))}]})
|
||||||
if model == 'openai/gpt-5-image':
|
|
||||||
return httpx.Response(200, json={'choices':[{'message':{'images':[{'image_url':{'url':'data:image/png;base64,'+image}}]}}]})
|
|
||||||
return httpx.Response(200, json={'data': [{'b64_json': image}]})
|
|
||||||
|
|
||||||
client = httpx.AsyncClient
|
client = httpx.AsyncClient
|
||||||
monkeypatch.setattr(httpx, 'AsyncClient', lambda **kwargs: client(transport=httpx.MockTransport(respond), **kwargs))
|
monkeypatch.setattr(httpx, 'AsyncClient', lambda **kwargs: client(transport=httpx.MockTransport(respond), **kwargs))
|
||||||
mask = Image.new('L', (2, 1))
|
mask = Image.new('L', (2, 1))
|
||||||
if full_frame:
|
|
||||||
mask.paste(255, (0,0,2,1))
|
|
||||||
mask.putpixel((1, 0), 255)
|
mask.putpixel((1, 0), 255)
|
||||||
|
|
||||||
class Request:
|
class Request:
|
||||||
async def json(self):
|
async def json(self):
|
||||||
return {'_endpoint': endpoint.base_url, '_model': 'stale-model' if saved_settings else model,
|
return {'_endpoint': endpoint.base_url, '_model': 'openai/gpt-5-image',
|
||||||
'_use_image_settings':saved_settings,
|
|
||||||
'image': png(Image.new('RGB', (2, 1), 'red')), 'mask': png(mask), 'prompt': 'private-cloud-prompt'}
|
'image': png(Image.new('RGB', (2, 1), 'red')), 'mask': png(mask), 'prompt': 'private-cloud-prompt'}
|
||||||
|
|
||||||
route = next(r for r in gallery.setup_gallery_routes().routes if r.path == '/api/image/inpaint')
|
route = next(r for r in gallery.setup_gallery_routes().routes if r.path == '/api/image/inpaint')
|
||||||
result = asyncio.run(route.endpoint(Request()))
|
result = asyncio.run(route.endpoint(Request()))
|
||||||
output = Image.open(io.BytesIO(base64.b64decode(result['image'])))
|
output = Image.open(io.BytesIO(base64.b64decode(result['image'])))
|
||||||
if full_frame:
|
assert output.getpixel((0, 0)) == (255, 0, 0, 255)
|
||||||
assert output.size == (4,4)
|
assert output.getpixel((1, 0)) == (0, 0, 255, 255)
|
||||||
assert result['full_frame'] is True
|
|
||||||
else:
|
|
||||||
assert output.getpixel((0, 0)) == (255, 0, 0, 255)
|
|
||||||
assert output.getpixel((1, 0)) == (0, 0, 255, 255)
|
|
||||||
assert len(requests) == 1
|
assert len(requests) == 1
|
||||||
assert 'private-cloud-prompt' not in caplog.text
|
assert 'private-cloud-prompt' not in caplog.text
|
||||||
|
|
||||||
|
|
||||||
def test_partial_image_edit_preserves_ratio_and_unmasked_pixels():
|
|
||||||
source = Image.new('RGBA',(4,4),'red')
|
|
||||||
generated = Image.new('RGBA',(4,2),'blue')
|
|
||||||
mask = Image.new('L',(4,4),255)
|
|
||||||
mask.putpixel((0,0),0)
|
|
||||||
result, full = gallery._compose_cloud_image_edit(source,generated,mask)
|
|
||||||
assert full is False
|
|
||||||
assert result.size == (4,4)
|
|
||||||
assert result.getpixel((2,0)) == source.getpixel((2,0))
|
|
||||||
assert result.getpixel((2,3)) == source.getpixel((2,3))
|
|
||||||
assert result.getpixel((2,1)) == (0,0,255,255)
|
|
||||||
|
|
||||||
|
|
||||||
def test_quick_edit_inpaint_requests_source_ratio_without_width_height_fields(monkeypatch):
|
|
||||||
endpoint = SimpleNamespace(base_url='https://openrouter.ai/api/v1',api_key='test')
|
|
||||||
monkeypatch.setattr(gallery,'SessionLocal',lambda:SimpleNamespace(close=lambda:None))
|
|
||||||
monkeypatch.setattr(gallery,'require_privilege',lambda *args:'alice')
|
|
||||||
monkeypatch.setattr(gallery,'_visible_image_endpoint_for_base',lambda *args:endpoint)
|
|
||||||
monkeypatch.setattr('src.url_safety.check_outbound_url',lambda *args,**kwargs:(True,''))
|
|
||||||
sent = []
|
|
||||||
original = httpx.AsyncClient
|
|
||||||
|
|
||||||
def respond(request):
|
|
||||||
payload = json.loads(request.content)
|
|
||||||
sent.append(payload)
|
|
||||||
return httpx.Response(200,json={'data':[{'b64_json':png(Image.new('RGB',(1536,864),'blue'))}]})
|
|
||||||
|
|
||||||
monkeypatch.setattr(httpx,'AsyncClient',lambda **kwargs:original(transport=httpx.MockTransport(respond),**kwargs))
|
|
||||||
|
|
||||||
class Request:
|
|
||||||
async def json(self):
|
|
||||||
return {'_endpoint':endpoint.base_url,'_model':'openai/gpt-image-2','prompt':'change the sky',
|
|
||||||
'image':png(Image.new('RGB',(1280,720),'red')),'mask':png(Image.new('L',(1280,720),255))}
|
|
||||||
|
|
||||||
route = next(route for route in gallery.setup_gallery_routes().routes if route.path == '/api/image/inpaint')
|
|
||||||
result = asyncio.run(route.endpoint(Request()))
|
|
||||||
assert sent[0]['aspect_ratio'] == '16:9'
|
|
||||||
assert 'size' not in sent[0]
|
|
||||||
image = Image.open(io.BytesIO(base64.b64decode(result['image'])))
|
|
||||||
assert image.size == (1536,864)
|
|
||||||
|
|
||||||
|
|
||||||
def test_mixed_endpoint_requires_configured_image_model(monkeypatch):
|
def test_mixed_endpoint_requires_configured_image_model(monkeypatch):
|
||||||
endpoint = SimpleNamespace(base_url='https://openrouter.ai/api/v1', owner='alice',
|
endpoint = SimpleNamespace(base_url='https://openrouter.ai/api/v1', owner='alice',
|
||||||
model_type='llm', cached_models='["openai/gpt-5-image", "text-model"]', pinned_models=None)
|
model_type='llm', cached_models='["openai/gpt-5-image", "text-model"]', pinned_models=None)
|
||||||
|
|||||||
@@ -23,7 +23,7 @@ def test_native_generation_uses_configured_model_and_multiline_prompt(image_tran
|
|||||||
json.dumps({'prompt': prompt}), session_id='fixture', owner='alice'))
|
json.dumps({'prompt': prompt}), session_id='fixture', owner='alice'))
|
||||||
assert 'error' not in result
|
assert 'error' not in result
|
||||||
assert seen == [('openai/gpt-5-image', 'alice')]
|
assert seen == [('openai/gpt-5-image', 'alice')]
|
||||||
assert json.loads(requests[0].content)['messages'][0]['content'] == prompt
|
assert json.loads(requests[0].content)['prompt'] == prompt
|
||||||
assert result['image_id']
|
assert result['image_id']
|
||||||
|
|
||||||
|
|
||||||
@@ -87,17 +87,13 @@ def image_transport(monkeypatch, tmp_path):
|
|||||||
original_client = httpx.AsyncClient
|
original_client = httpx.AsyncClient
|
||||||
requests = []
|
requests = []
|
||||||
|
|
||||||
def configure(base, status=200, body=None, model='openai/gpt-5-image'):
|
def configure(base, status=200, body=None):
|
||||||
monkeypatch.setattr(ai_interaction, '_resolve_model', lambda *args, **kwargs: (
|
monkeypatch.setattr(ai_interaction, '_resolve_model', lambda *args, **kwargs: (
|
||||||
base + '/chat/completions', model, {'Authorization': 'Bearer test'},
|
base + '/chat/completions', 'openai/gpt-5-image', {'Authorization': 'Bearer test'},
|
||||||
))
|
))
|
||||||
|
|
||||||
def handle(request):
|
def handle(request):
|
||||||
requests.append(request)
|
requests.append(request)
|
||||||
if body is None and 'openrouter.ai' in base and request.url.path.endswith('/chat/completions'):
|
|
||||||
return httpx.Response(status, json={'choices': [{'message': {'images': [{
|
|
||||||
'image_url': {'url': 'data:image/png;base64,' + base64.b64encode(source.read_bytes()).decode()},
|
|
||||||
}]}}]})
|
|
||||||
return httpx.Response(status, json=body if body is not None else {
|
return httpx.Response(status, json=body if body is not None else {
|
||||||
'data': [{'b64_json': base64.b64encode(source.read_bytes()).decode()}],
|
'data': [{'b64_json': base64.b64encode(source.read_bytes()).decode()}],
|
||||||
})
|
})
|
||||||
@@ -134,13 +130,12 @@ def test_image_provider_protocol(image_transport, editing, base):
|
|||||||
assert request.headers['authorization'] == 'Bearer test'
|
assert request.headers['authorization'] == 'Bearer test'
|
||||||
if 'openrouter.ai' in base:
|
if 'openrouter.ai' in base:
|
||||||
assert len(requests) == 1 # No unsupported local progress/fallback probes.
|
assert len(requests) == 1 # No unsupported local progress/fallback probes.
|
||||||
assert str(request.url) == base + '/chat/completions'
|
assert str(request.url) == base + '/images'
|
||||||
payload = json.loads(request.content)
|
payload = json.loads(request.content)
|
||||||
assert payload['model'] == 'openai/gpt-5-image'
|
assert payload['n'] == 1
|
||||||
assert payload['modalities'] == ['image', 'text']
|
|
||||||
assert 'prompt' not in payload
|
|
||||||
if editing:
|
if editing:
|
||||||
reference = payload['messages'][0]['content'][1]['image_url']['url']
|
assert payload['size'] == '1536x1024'
|
||||||
|
reference = payload['input_references'][0]['image_url']['url']
|
||||||
assert reference.startswith('data:image/png;base64,')
|
assert reference.startswith('data:image/png;base64,')
|
||||||
assert base64.b64decode(reference.split(',', 1)[1]) == source.read_bytes()
|
assert base64.b64decode(reference.split(',', 1)[1]) == source.read_bytes()
|
||||||
assert 'request_id' not in payload
|
assert 'request_id' not in payload
|
||||||
@@ -186,65 +181,13 @@ def test_edit_dimensions_respect_exif_and_explicit_size(image_transport, explici
|
|||||||
exif = Image.Exif()
|
exif = Image.Exif()
|
||||||
exif[274] = 6
|
exif[274] = 6
|
||||||
Image.new('RGB', (120, 80)).save(source, exif=exif)
|
Image.new('RGB', (120, 80)).save(source, exif=exif)
|
||||||
configure('https://openrouter.ai/api/v1', model='openai/gpt-image-1')
|
configure('https://openrouter.ai/api/v1')
|
||||||
result = asyncio.run(ai_interaction.do_edit_image(
|
result = asyncio.run(ai_interaction.do_edit_image(
|
||||||
'Edit', str(source), model_spec='openai/gpt-image-1',
|
'Edit', str(source), model_spec='openai/gpt-5-image',
|
||||||
size='1024x1024' if explicit else 'auto',
|
size='1024x1024' if explicit else 'auto',
|
||||||
))
|
))
|
||||||
assert 'error' not in result
|
assert 'error' not in result
|
||||||
assert json.loads(requests[0].content)['aspect_ratio'] == ('1:1' if explicit else '2:3')
|
assert json.loads(requests[0].content)['size'] == ('1024x1024' if explicit else '1024x1536')
|
||||||
|
|
||||||
|
|
||||||
def test_native_openrouter_image_model_keeps_dedicated_image_api(image_transport):
|
|
||||||
configure, source, requests, rows = image_transport
|
|
||||||
configure('https://openrouter.ai/api/v1', model='openai/gpt-image-1')
|
|
||||||
result = asyncio.run(ai_interaction.do_generate_image('A cow\nopenai/gpt-image-1', incognito=True))
|
|
||||||
assert 'error' not in result
|
|
||||||
assert str(requests[0].url).endswith('/api/v1/images')
|
|
||||||
assert json.loads(requests[0].content)['model'] == 'openai/gpt-image-1'
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize('editing', [False, True])
|
|
||||||
def test_http_200_provider_deprecation_is_not_reported_as_empty_image(image_transport, editing):
|
|
||||||
configure, source, requests, rows = image_transport
|
|
||||||
configure('https://openrouter.ai/api/v1', body={'choices': [{
|
|
||||||
'finish_reason': 'error', 'message': {'content': None},
|
|
||||||
'error': {'code': 502, 'message': 'The model gpt-image-1 has been deprecated'},
|
|
||||||
}]})
|
|
||||||
if editing:
|
|
||||||
result = asyncio.run(ai_interaction.do_edit_image('Edit', str(source), model_spec='openai/gpt-5-image'))
|
|
||||||
else:
|
|
||||||
result = asyncio.run(ai_interaction.do_generate_image('A cow\nopenai/gpt-5-image'))
|
|
||||||
assert 'deprecated' in result['error']
|
|
||||||
assert 'openai/gpt-5-image' in result['error']
|
|
||||||
assert 'AI settings' in result['error']
|
|
||||||
assert len(requests) == 1
|
|
||||||
assert not rows
|
|
||||||
|
|
||||||
|
|
||||||
def test_empty_image_response_identifies_the_selected_model(image_transport):
|
|
||||||
configure, source, requests, rows = image_transport
|
|
||||||
configure('https://openrouter.ai/api/v1', body={'choices': [{'message': {'content': 'No image.'}}]})
|
|
||||||
result = asyncio.run(ai_interaction.do_generate_image('A cow\nopenai/gpt-5-image'))
|
|
||||||
assert "'openai/gpt-5-image' returned no image" in result['error']
|
|
||||||
assert 'AI settings' in result['error']
|
|
||||||
assert len(requests) == 1
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize('editing', [False,True])
|
|
||||||
def test_openrouter_1280_by_720_requests_16_by_9_not_square(image_transport, editing):
|
|
||||||
configure, source, requests, rows = image_transport
|
|
||||||
Image.new('RGB',(1280,720),'red').save(source)
|
|
||||||
configure('https://openrouter.ai/api/v1',model='openai/gpt-image-2')
|
|
||||||
if editing:
|
|
||||||
result = asyncio.run(ai_interaction.do_edit_image('Change the sky',str(source),model_spec='openai/gpt-image-2',incognito=True))
|
|
||||||
else:
|
|
||||||
result = asyncio.run(ai_interaction.do_generate_image(json.dumps({
|
|
||||||
'prompt':'a landscape','model':'openai/gpt-image-2','size':'1280x720'}),incognito=True))
|
|
||||||
assert 'error' not in result
|
|
||||||
body = json.loads(requests[0].content)
|
|
||||||
assert body['aspect_ratio'] == '16:9'
|
|
||||||
assert 'size' not in body
|
|
||||||
|
|
||||||
|
|
||||||
def test_unreadable_image_does_not_call_provider(image_transport):
|
def test_unreadable_image_does_not_call_provider(image_transport):
|
||||||
|
|||||||
@@ -1093,117 +1093,39 @@ def test_degenerate_stream_guard_allows_long_rows_with_changing_values():
|
|||||||
|
|
||||||
def _latex_matrix(cell):
|
def _latex_matrix(cell):
|
||||||
rows = (" & ".join(cell(i, j) for j in range(8)) for i in range(8))
|
rows = (" & ".join(cell(i, j) for j in range(8)) for i in range(8))
|
||||||
return "$$\\begin{pmatrix}\n" + " \\\\\n".join(rows) + "\n\\end{pmatrix}$$\n"
|
return "$$\\begin{pmatrix}\n" + " \\\\\n".join(rows) + "\n\\end{pmatrix}$$"
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize("text", [
|
@pytest.mark.parametrize("text", [
|
||||||
_latex_matrix(lambda i, j: "0"),
|
_latex_matrix(lambda i, j: "0"),
|
||||||
_latex_matrix(lambda i, j: "1" if i == j else "0"),
|
_latex_matrix(lambda i, j: "1" if i == j else "0"),
|
||||||
_latex_matrix(lambda i, j: "2e10"),
|
"buf = [" + ", ".join("0" for _ in range(64)) + "]",
|
||||||
_latex_matrix(lambda i, j: "1e-5"),
|
|
||||||
_latex_matrix(lambda i, j: "1.25e-3"),
|
|
||||||
_latex_matrix(lambda i, j: "-0.125"),
|
|
||||||
_latex_matrix(lambda i, j: "0.0"),
|
|
||||||
_latex_matrix(lambda i, j: "3.14"),
|
|
||||||
"buf = [" + ", ".join("0" for _ in range(64)) + "]\n",
|
|
||||||
"buf = [" + ", ".join("1.25e-3" for _ in range(64)) + "]\n",
|
|
||||||
"buf = [" + ", ".join("-0.125" for _ in range(64)) + "]\n",
|
|
||||||
"t,v1,v2,v3\n" + "\n".join(f"{i},1.25e-3,-0.125,2e10" for i in range(25)) + "\n",
|
|
||||||
"\n".join(f"{i}. Solve: $\\frac{{{i}}}{{{i+1}}}x + \\frac{{1}}{{{i+2}}} = \\frac{{{i+3}}}{{{i+4}}}$" for i in range(1, 20)) + "\n",
|
|
||||||
"<svg>" + "".join(f'<rect x="{i*10}" y="{j*10}" width="10" height="10" fill="#abc"/>' for i in range(4) for j in range(4)) + "</svg>\n",
|
|
||||||
"{" + ", ".join(f'"key_{i}": "translation_{i}"' for i in range(40)) + "}\n",
|
|
||||||
])
|
])
|
||||||
@pytest.mark.parametrize("chunk", [1, 3, 6, 8, 16, 32, 64, 128])
|
@pytest.mark.parametrize("chunk", [1, 6, 64])
|
||||||
def test_degenerate_stream_guard_allows_numeric_data(text, chunk):
|
def test_degenerate_stream_guard_allows_numeric_data(text, chunk):
|
||||||
"""#5560: legitimate matrices, scientific numbers, tables, and structured data
|
"""#5560: zero/identity matrices and padded buffers repeat numbers by
|
||||||
repeat values or formatting by design; they are not token collapse."""
|
design; they are not token collapse."""
|
||||||
guard = llm_core._DegenerateStreamGuard("math-model")
|
guard = llm_core._DegenerateStreamGuard("math-model")
|
||||||
|
|
||||||
assert all(guard.check(text[i:i + chunk]) is None for i in range(0, len(text), chunk))
|
assert all(guard.check(text[i:i + chunk]) is None for i in range(0, len(text), chunk))
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize("chunk", [1, 6, 64, 3000])
|
def test_degenerate_stream_guard_still_stops_an_endless_number_run():
|
||||||
def test_degenerate_stream_guard_allows_numeric_data_full_and_char_stream(chunk):
|
|
||||||
text = _latex_matrix(lambda i, j: "1.25e-3")
|
|
||||||
guard = llm_core._DegenerateStreamGuard("math-model")
|
|
||||||
|
|
||||||
assert all(guard.check(text[i:i + chunk]) is None for i in range(0, len(text), chunk))
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize("loop_text", [
|
|
||||||
"0 " * 3000,
|
|
||||||
"1.25e-3 " * 3000,
|
|
||||||
"1 2 " * 1500,
|
|
||||||
])
|
|
||||||
@pytest.mark.parametrize("chunk", [1, 6, 32, 64, 3000])
|
|
||||||
def test_degenerate_stream_guard_still_stops_an_endless_number_run(loop_text, chunk):
|
|
||||||
guard = llm_core._DegenerateStreamGuard("looping-model")
|
guard = llm_core._DegenerateStreamGuard("looping-model")
|
||||||
|
|
||||||
stopped = False
|
chunk = guard.check("0 " * 3000)
|
||||||
for i in range(0, len(loop_text), chunk):
|
|
||||||
res = guard.check(loop_text[i:i + chunk])
|
assert chunk is not None
|
||||||
if res is not None:
|
assert "numbers in a row" in chunk
|
||||||
assert "numbers in a row" in res
|
|
||||||
stopped = True
|
|
||||||
break
|
|
||||||
assert stopped
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize("loop_text", [
|
def test_degenerate_stream_guard_still_stops_a_mixed_word_number_loop():
|
||||||
"row 0 0 0 0 " * 300,
|
|
||||||
"Step 1 2 3 " * 200,
|
|
||||||
])
|
|
||||||
@pytest.mark.parametrize("chunk", [1, 6, 32, 64, 1500])
|
|
||||||
def test_degenerate_stream_guard_still_stops_a_mixed_word_number_loop(loop_text, chunk):
|
|
||||||
guard = llm_core._DegenerateStreamGuard("looping-model")
|
guard = llm_core._DegenerateStreamGuard("looping-model")
|
||||||
|
|
||||||
stopped = False
|
chunk = guard.check("row 0 0 0 0 " * 300)
|
||||||
for i in range(0, len(loop_text), chunk):
|
|
||||||
res = guard.check(loop_text[i:i + chunk])
|
|
||||||
if res is not None:
|
|
||||||
assert "repeated" in res
|
|
||||||
stopped = True
|
|
||||||
break
|
|
||||||
assert stopped
|
|
||||||
|
|
||||||
|
assert chunk is not None
|
||||||
@pytest.mark.parametrize("loop_text", [
|
assert "repeated" in chunk
|
||||||
"Summer " * 80,
|
|
||||||
"also be a software developer mode " * 30,
|
|
||||||
])
|
|
||||||
@pytest.mark.parametrize("chunk", [1, 6, 32, 64, 500])
|
|
||||||
def test_degenerate_stream_guard_still_stops_repeated_words_and_phrases(loop_text, chunk):
|
|
||||||
guard = llm_core._DegenerateStreamGuard("looping-model")
|
|
||||||
|
|
||||||
stopped = False
|
|
||||||
for i in range(0, len(loop_text), chunk):
|
|
||||||
res = guard.check(loop_text[i:i + chunk])
|
|
||||||
if res is not None:
|
|
||||||
assert "repeated" in res
|
|
||||||
stopped = True
|
|
||||||
break
|
|
||||||
assert stopped
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize("bad_token", [
|
|
||||||
"1face ",
|
|
||||||
"1e ",
|
|
||||||
"25e ",
|
|
||||||
"0x12 ",
|
|
||||||
])
|
|
||||||
@pytest.mark.parametrize("chunk", [1, 6, 32, 64, 250])
|
|
||||||
def test_degenerate_stream_guard_stops_alphanumeric_repetition_resembling_numbers(bad_token, chunk):
|
|
||||||
guard = llm_core._DegenerateStreamGuard("looping-model")
|
|
||||||
text = bad_token * 50
|
|
||||||
|
|
||||||
stopped = False
|
|
||||||
for i in range(0, len(text), chunk):
|
|
||||||
res = guard.check(text[i:i + chunk])
|
|
||||||
if res is not None:
|
|
||||||
assert "repeated" in res
|
|
||||||
stopped = True
|
|
||||||
break
|
|
||||||
assert stopped
|
|
||||||
|
|
||||||
|
|
||||||
def test_terminal_stream_retries_degenerate_generation_before_emitting(monkeypatch):
|
def test_terminal_stream_retries_degenerate_generation_before_emitting(monkeypatch):
|
||||||
|
|||||||
@@ -303,3 +303,52 @@ def test_teardown_rejects_unconfined_programmatic_paths():
|
|||||||
res = _run_teardown(options={"resultsDir": "/etc"})
|
res = _run_teardown(options={"resultsDir": "/etc"})
|
||||||
assert res.returncode == 1
|
assert res.returncode == 1
|
||||||
assert "Security rejection" in res.stderr
|
assert "Security rejection" in res.stderr
|
||||||
|
|
||||||
|
|
||||||
|
def _disposable_ci_fixture(tmp_path):
|
||||||
|
data = tmp_path / "odysseus-photo-editor-e2e-fixture"
|
||||||
|
data.mkdir()
|
||||||
|
database = data / "app.db"
|
||||||
|
(data / ".disposable-ci-fixture.json").write_text(json.dumps({
|
||||||
|
"synthetic": True, "authenticated": True, "database": str(database),
|
||||||
|
}))
|
||||||
|
return {"CI": "true", "GITHUB_ACTIONS": "true",
|
||||||
|
"ODYSSEUS_E2E_DISPOSABLE_ARTIFACTS": "1",
|
||||||
|
"PHOTO_EDITOR_E2E_DATA_DIR": str(data), "PHOTO_EDITOR_E2E_DB_PATH": str(database),
|
||||||
|
"PHOTO_EDITOR_E2E_AUTH": "1", "ODYSSEUS_TEST_PYTHON": sys.executable}
|
||||||
|
|
||||||
|
|
||||||
|
def test_disposable_ci_retains_only_sanitized_failure_artifacts(tmp_path):
|
||||||
|
results = tmp_path / "results"
|
||||||
|
results.mkdir()
|
||||||
|
trace = results / "trace.zip"
|
||||||
|
with zipfile.ZipFile(trace, "w") as archive:
|
||||||
|
archive.writestr("0-trace.trace", json.dumps({"type": "action", "name": "fill", "params": {"value": "synthetic-secret"}}) + "\n")
|
||||||
|
screenshot = results / "failure.png"
|
||||||
|
screenshot.write_bytes(b"synthetic screenshot")
|
||||||
|
response = _run_teardown(_disposable_ci_fixture(tmp_path), {"resultsDir": str(results)})
|
||||||
|
assert response.returncode == 0, response.stderr
|
||||||
|
with zipfile.ZipFile(trace) as archive:
|
||||||
|
assert json.loads(archive.read("0-trace.trace"))["params"]["value"] == "[REDACTED]"
|
||||||
|
assert screenshot.exists()
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("defect", ["missing-marker", "wrong-database", "unauthenticated", "sanitizer-failure"])
|
||||||
|
def test_disposable_ci_artifact_failures_purge_closed(tmp_path, defect):
|
||||||
|
results = tmp_path / "results"
|
||||||
|
results.mkdir()
|
||||||
|
(results / "trace.zip").write_bytes(b"invalid sensitive archive")
|
||||||
|
(results / "failure.png").write_bytes(b"sensitive screenshot")
|
||||||
|
environment = _disposable_ci_fixture(tmp_path)
|
||||||
|
if defect == "missing-marker":
|
||||||
|
(Path(environment["PHOTO_EDITOR_E2E_DATA_DIR"]) / ".disposable-ci-fixture.json").unlink()
|
||||||
|
elif defect == "wrong-database":
|
||||||
|
environment["PHOTO_EDITOR_E2E_DB_PATH"] = str(tmp_path / "other.db")
|
||||||
|
elif defect == "unauthenticated":
|
||||||
|
environment["PHOTO_EDITOR_E2E_AUTH"] = "0"
|
||||||
|
else:
|
||||||
|
environment["ODYSSEUS_TEST_PYTHON"] = "/missing-python"
|
||||||
|
response = _run_teardown(environment, {"resultsDir": str(results)})
|
||||||
|
assert response.returncode == 0, response.stderr
|
||||||
|
assert not (results / "trace.zip").exists()
|
||||||
|
assert not (results / "failure.png").exists()
|
||||||
|
|||||||
@@ -1,141 +0,0 @@
|
|||||||
"""Regression tests for py/polynomial-redos sinks in the agent preview parsers.
|
|
||||||
|
|
||||||
Each pattern had overlapping quantifiers (adjacent `\\s` runs, or a lazy group
|
|
||||||
that rescanned the rest of the line from every candidate) that backtrack
|
|
||||||
polynomially when the tail fails. The rewrites keep the same matches for the
|
|
||||||
text these tools emit. The old patterns took 5-20s on the flood inputs below;
|
|
||||||
the new ones finish in milliseconds against a 4s budget.
|
|
||||||
"""
|
|
||||||
|
|
||||||
import time
|
|
||||||
|
|
||||||
import pytest
|
|
||||||
|
|
||||||
from src.browser_observation import compact_browser_observation
|
|
||||||
from src.clean_agent_preview import (
|
|
||||||
email_search_result_empty,
|
|
||||||
is_google_sorry_url,
|
|
||||||
skills_terminal_response,
|
|
||||||
tasks_terminal_response,
|
|
||||||
)
|
|
||||||
|
|
||||||
_BUDGET_S = 4.0
|
|
||||||
_DASH = chr(0x2014) # the separator src/tools/system.py writes into task rows
|
|
||||||
|
|
||||||
|
|
||||||
def _timed(fn, *args):
|
|
||||||
start = time.perf_counter()
|
|
||||||
result = fn(*args)
|
|
||||||
return result, time.perf_counter() - start
|
|
||||||
|
|
||||||
|
|
||||||
# Browser snapshot: bare `generic` container lines are dropped, others kept.
|
|
||||||
|
|
||||||
def test_generic_snapshot_lines_are_dropped():
|
|
||||||
snapshot = '- generic [ref=e1]:\n - generic\n-generic:\n- generic button\n- button "Go" [ref=e2]'
|
|
||||||
output = compact_browser_observation({'snapshot': snapshot})
|
|
||||||
assert output == '- generic button\n- button "Go" [ref=e2]'
|
|
||||||
|
|
||||||
|
|
||||||
def test_generic_filter_whitespace_flood_is_fast():
|
|
||||||
(output, dt) = _timed(compact_browser_observation, {'snapshot': '\t' * 120000 + '!'})
|
|
||||||
assert dt < _BUDGET_S, f'generic filter took {dt:.2f}s'
|
|
||||||
assert output.endswith('!')
|
|
||||||
|
|
||||||
|
|
||||||
# Browser CLI text: status text is split from the post-interaction state.
|
|
||||||
|
|
||||||
@pytest.mark.parametrize('label', ['post-click page state', 'page state after failed click'])
|
|
||||||
def test_post_interaction_state_is_split(label):
|
|
||||||
raw = f'Clicked\n\n[{label}]\n{{"snapshot": "- link \\"A\\" [ref=e3]"}}'
|
|
||||||
assert compact_browser_observation(raw) == 'Clicked\n\n- link "A" [ref=e3]'
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize('opener', ['\n\n[post-', '\n\n[page state after failed '])
|
|
||||||
def test_post_interaction_split_flood_is_fast(opener):
|
|
||||||
(_, dt) = _timed(compact_browser_observation, 'x' + opener * 15000)
|
|
||||||
assert dt < _BUDGET_S, f'post-interaction split took {dt:.2f}s'
|
|
||||||
|
|
||||||
|
|
||||||
# Task rows: `N. name (id) <dash> detail`, where the name may contain parentheses.
|
|
||||||
|
|
||||||
def test_task_rows_parse():
|
|
||||||
raw = (f'1. Backup (nightly) (abc123) {_DASH} active, daily at 02:00\n'
|
|
||||||
f'2. (e2) {_DASH} paused\n'
|
|
||||||
f'not a task row')
|
|
||||||
assert tasks_terminal_response(raw) == (
|
|
||||||
f'Tasks (2):\n- [Backup (nightly)](#task-abc123) {_DASH} active\n'
|
|
||||||
f'- [](#task-e2) {_DASH} paused'
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize('evil', ['0.' + '\t' * 3000 + '!', '0.\t\t(' + '\t((' * 20000])
|
|
||||||
def test_task_row_flood_is_fast(evil):
|
|
||||||
(output, dt) = _timed(tasks_terminal_response, evil)
|
|
||||||
assert dt < _BUDGET_S, f'task row parse took {dt:.2f}s'
|
|
||||||
assert output == evil.strip()
|
|
||||||
|
|
||||||
|
|
||||||
# Skill inventory: `- name (category): summary` under a status heading.
|
|
||||||
|
|
||||||
def test_skill_rows_parse():
|
|
||||||
raw = '## Published\n- pdf-tools (documents): Read PDFs\n- web\n## Drafts\n- notes (writing)'
|
|
||||||
assert skills_terminal_response(raw) == (
|
|
||||||
'Skills (3):\n\n**Published**\n- [pdf-tools](#skill-pdf-tools) (documents)\n'
|
|
||||||
'- [web](#skill-web)\n\n**Drafts**\n- [notes](#skill-notes) (writing)'
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def test_nameless_skill_bullet_is_not_a_row():
|
|
||||||
raw = '## Published\n- : x'
|
|
||||||
assert skills_terminal_response(raw) == raw
|
|
||||||
|
|
||||||
|
|
||||||
def test_skill_row_flood_is_fast():
|
|
||||||
(_, dt) = _timed(skills_terminal_response, '## Published\n- \t9' + '\t\t' * 30000 + '!')
|
|
||||||
assert dt < _BUDGET_S, f'skill row parse took {dt:.2f}s'
|
|
||||||
|
|
||||||
|
|
||||||
# Email search: the plain-text "no results" message.
|
|
||||||
|
|
||||||
@pytest.mark.parametrize('value,expected', [
|
|
||||||
('No emails matched from:bob.', True),
|
|
||||||
(' no emails matched "invoice"\n\n', True),
|
|
||||||
('No emails matched ', False),
|
|
||||||
('No emails matched x\nmore', False),
|
|
||||||
('Found 2 emails', False),
|
|
||||||
])
|
|
||||||
def test_email_no_results_message(value, expected):
|
|
||||||
assert email_search_result_empty(value) is expected
|
|
||||||
|
|
||||||
|
|
||||||
def test_email_no_results_flood_is_fast():
|
|
||||||
(result, dt) = _timed(email_search_result_empty, 'No emails matched x' + '\t' * 60000 + '\n!')
|
|
||||||
assert dt < _BUDGET_S, f'email no-results check took {dt:.2f}s'
|
|
||||||
assert result is False
|
|
||||||
|
|
||||||
|
|
||||||
# Google rate-limit interstitial.
|
|
||||||
|
|
||||||
@pytest.mark.parametrize('url,expected', [
|
|
||||||
('https://www.google.com/sorry/index?continue=x', True),
|
|
||||||
('http://google.co.uk/sorry/', True),
|
|
||||||
('HTTPS://IPV4.GOOGLE.COM/SORRY/', True),
|
|
||||||
('https://example.com/sorry/?next=https://www.google.de/sorry/', True),
|
|
||||||
('https://example.com/sorry/', False),
|
|
||||||
('https://notgoogle.com/sorry/', False),
|
|
||||||
('https://www.google.com/search?q=sorry', False),
|
|
||||||
('', False),
|
|
||||||
])
|
|
||||||
def test_is_google_sorry_url(url, expected):
|
|
||||||
assert is_google_sorry_url(url) is expected
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize('evil', [
|
|
||||||
'http://google.' + '..google.' * 50000,
|
|
||||||
'http://' + 'x.' * 200000 + 'google./sorry/', # reaches the host match
|
|
||||||
])
|
|
||||||
def test_google_sorry_flood_is_fast(evil):
|
|
||||||
(result, dt) = _timed(is_google_sorry_url, evil)
|
|
||||||
assert dt < _BUDGET_S, f'google sorry check took {dt:.2f}s'
|
|
||||||
assert result is False
|
|
||||||
@@ -118,554 +118,3 @@ def test_failed_save_shows_failure_in_simulated_dom():
|
|||||||
assert data["serverErrResult"] == {"text": "Failed to save", "color": "var(--red)"}
|
assert data["serverErrResult"] == {"text": "Failed to save", "color": "var(--red)"}
|
||||||
assert data["okResult"] == {"text": "Saved", "color": "var(--fg)"}
|
assert data["okResult"] == {"text": "Saved", "color": "var(--fg)"}
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.skipif(not _HAS_NODE, reason="node binary not on PATH")
|
|
||||||
def test_image_preferences_js_policy_and_non_admin_fallback():
|
|
||||||
"""Verify imagePreferences.js non-admin fallback, policy toggle isolation, and admin clearing."""
|
|
||||||
js = """
|
|
||||||
globalThis.window = { dispatchEvent: () => {} };
|
|
||||||
globalThis.CustomEvent = class CustomEvent {};
|
|
||||||
|
|
||||||
const { saveImagePreferences, readImagePreferences } = await import('./static/js/settings/imagePreferences.js');
|
|
||||||
|
|
||||||
const results = {};
|
|
||||||
|
|
||||||
// Scenario 1: Non-admin save (POST /api/auth/settings returns 403)
|
|
||||||
const prefCalls = [];
|
|
||||||
globalThis.fetch = async (url, opts = {}) => {
|
|
||||||
if (url === '/api/auth/settings') {
|
|
||||||
return { status: 403, ok: false, text: async () => 'Forbidden' };
|
|
||||||
}
|
|
||||||
if (url.startsWith('/api/prefs/')) {
|
|
||||||
prefCalls.push({ url, body: JSON.parse(opts.body) });
|
|
||||||
return { status: 200, ok: true, json: async () => ({ ok: true }) };
|
|
||||||
}
|
|
||||||
throw new Error('Unexpected URL: ' + url);
|
|
||||||
};
|
|
||||||
|
|
||||||
const patch = {
|
|
||||||
image_gen_enabled: true,
|
|
||||||
image_model: 'user-gen-model',
|
|
||||||
image_edit_model: 'user-edit-model',
|
|
||||||
image_quality: 'high',
|
|
||||||
};
|
|
||||||
|
|
||||||
const res = await saveImagePreferences(patch);
|
|
||||||
results.fallbackResOk = res.ok;
|
|
||||||
results.fallbackResStatus = res.status;
|
|
||||||
results.prefCalls = prefCalls;
|
|
||||||
|
|
||||||
// Scenario 2: Non-admin save fails on prefs PUT (keeps actual failure visible)
|
|
||||||
globalThis.fetch = async (url) => {
|
|
||||||
if (url === '/api/auth/settings') return { status: 403, ok: false };
|
|
||||||
if (url.startsWith('/api/prefs/')) return { status: 500, ok: false, text: async () => 'Disk error' };
|
|
||||||
throw new Error('Unexpected URL: ' + url);
|
|
||||||
};
|
|
||||||
try {
|
|
||||||
await saveImagePreferences({ image_model: 'fail-model' });
|
|
||||||
results.prefFailureThrew = false;
|
|
||||||
} catch (err) {
|
|
||||||
results.prefFailureThrew = true;
|
|
||||||
results.prefFailureStatus = err.status;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Scenario 3: Admin save clears pre-existing per-user preferences
|
|
||||||
const adminPrefClears = [];
|
|
||||||
globalThis.fetch = async (url, opts = {}) => {
|
|
||||||
if (url === '/api/auth/settings') {
|
|
||||||
return { status: 200, ok: true, json: async () => ({ ok: true }) };
|
|
||||||
}
|
|
||||||
if (url.startsWith('/api/prefs/')) {
|
|
||||||
adminPrefClears.push({ url, body: JSON.parse(opts.body) });
|
|
||||||
return { status: 200, ok: true, json: async () => ({ ok: true }) };
|
|
||||||
}
|
|
||||||
throw new Error('Unexpected URL: ' + url);
|
|
||||||
};
|
|
||||||
const adminRes = await saveImagePreferences({ image_model: 'admin-model', image_gen_enabled: true });
|
|
||||||
results.adminResOk = adminRes.ok;
|
|
||||||
results.adminPrefClears = adminPrefClears;
|
|
||||||
|
|
||||||
// Scenario 4: readImagePreferences loads image_quality along with models
|
|
||||||
globalThis.fetch = async (url) => {
|
|
||||||
if (url === '/api/prefs') {
|
|
||||||
return {
|
|
||||||
ok: true,
|
|
||||||
json: async () => ({ image_model: 'pref-gen', image_quality: 'low' }),
|
|
||||||
};
|
|
||||||
}
|
|
||||||
if (url === '/api/auth/settings') {
|
|
||||||
return {
|
|
||||||
ok: true,
|
|
||||||
json: async () => ({ image_model: 'global-gen', image_edit_model: 'global-edit', image_quality: 'medium' }),
|
|
||||||
};
|
|
||||||
}
|
|
||||||
throw new Error('Unexpected URL: ' + url);
|
|
||||||
};
|
|
||||||
const loaded = await readImagePreferences();
|
|
||||||
results.loaded = loaded;
|
|
||||||
|
|
||||||
console.log(JSON.stringify(results));
|
|
||||||
"""
|
|
||||||
proc = subprocess.run(["node", "--input-type=module", "-e", js], capture_output=True, text=True, check=True)
|
|
||||||
import json
|
|
||||||
data = json.loads(proc.stdout)
|
|
||||||
|
|
||||||
# 1. Non-admin fallback returns ok=True, status=200
|
|
||||||
assert data["fallbackResOk"] is True
|
|
||||||
assert data["fallbackResStatus"] == 200
|
|
||||||
|
|
||||||
# 2. image_gen_enabled was filtered out; only image_model, image_edit_model, image_quality written
|
|
||||||
written_keys = [c["url"].split("/api/prefs/")[1] for c in data["prefCalls"]]
|
|
||||||
assert "image_gen_enabled" not in written_keys
|
|
||||||
assert "image_model" in written_keys
|
|
||||||
assert "image_edit_model" in written_keys
|
|
||||||
assert "image_quality" in written_keys
|
|
||||||
|
|
||||||
# 3. Preference failure threw error and preserved status
|
|
||||||
assert data["prefFailureThrew"] is True
|
|
||||||
assert data["prefFailureStatus"] == 500
|
|
||||||
|
|
||||||
# 4. Admin save cleared per-user preferences with value: null
|
|
||||||
assert data["adminResOk"] is True
|
|
||||||
cleared_keys = [c["url"].split("/api/prefs/")[1] for c in data["adminPrefClears"]]
|
|
||||||
assert "image_model" in cleared_keys
|
|
||||||
for call in data["adminPrefClears"]:
|
|
||||||
assert call["body"] == {"value": None}
|
|
||||||
|
|
||||||
# 5. readImagePreferences includes image_quality from prefs
|
|
||||||
assert data["loaded"]["image_model"] == "pref-gen"
|
|
||||||
assert data["loaded"]["image_edit_model"] == "global-edit"
|
|
||||||
assert data["loaded"]["image_quality"] == "low"
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.skipif(not _HAS_NODE, reason="node binary not on PATH")
|
|
||||||
def test_admin_cleanup_failure_and_rejection_surfaced():
|
|
||||||
"""Verify saveImagePreferences surfaces HTTP failures and rejected network requests during admin cleanup."""
|
|
||||||
js = """
|
|
||||||
globalThis.window = { dispatchEvent: () => {} };
|
|
||||||
globalThis.CustomEvent = class CustomEvent {};
|
|
||||||
|
|
||||||
const { saveImagePreferences } = await import('./static/js/settings/imagePreferences.js');
|
|
||||||
|
|
||||||
const results = {};
|
|
||||||
|
|
||||||
// 1. HTTP 500 failure during admin preference cleanup
|
|
||||||
globalThis.fetch = async (url) => {
|
|
||||||
if (url === '/api/auth/settings') {
|
|
||||||
return { status: 200, ok: true, json: async () => ({ ok: true }) };
|
|
||||||
}
|
|
||||||
if (url.startsWith('/api/prefs/')) {
|
|
||||||
return { status: 500, ok: false, text: async () => 'Database lock error' };
|
|
||||||
}
|
|
||||||
throw new Error('Unexpected URL: ' + url);
|
|
||||||
};
|
|
||||||
try {
|
|
||||||
await saveImagePreferences({ image_model: 'admin-model', image_gen_enabled: true });
|
|
||||||
results.http500Threw = false;
|
|
||||||
} catch (err) {
|
|
||||||
results.http500Threw = true;
|
|
||||||
results.http500Status = err.status;
|
|
||||||
results.http500GlobalSaved = err.globalSaved;
|
|
||||||
results.http500Phase = err.phase;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 2. Network rejection during admin preference cleanup
|
|
||||||
globalThis.fetch = async (url) => {
|
|
||||||
if (url === '/api/auth/settings') {
|
|
||||||
return { status: 200, ok: true, json: async () => ({ ok: true }) };
|
|
||||||
}
|
|
||||||
if (url.startsWith('/api/prefs/')) {
|
|
||||||
throw new Error('Network offline');
|
|
||||||
}
|
|
||||||
throw new Error('Unexpected URL: ' + url);
|
|
||||||
};
|
|
||||||
try {
|
|
||||||
await saveImagePreferences({ image_model: 'admin-model', image_gen_enabled: true });
|
|
||||||
results.networkRejectThrew = false;
|
|
||||||
} catch (err) {
|
|
||||||
results.networkRejectThrew = true;
|
|
||||||
results.networkRejectStatus = err.status;
|
|
||||||
results.networkRejectGlobalSaved = err.globalSaved;
|
|
||||||
results.networkRejectPhase = err.phase;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 3. Successful cleanup
|
|
||||||
const clearedKeys = [];
|
|
||||||
globalThis.fetch = async (url, opts = {}) => {
|
|
||||||
if (url === '/api/auth/settings') {
|
|
||||||
return { status: 200, ok: true, json: async () => ({ ok: true }) };
|
|
||||||
}
|
|
||||||
if (url.startsWith('/api/prefs/')) {
|
|
||||||
clearedKeys.push({ url, body: JSON.parse(opts.body) });
|
|
||||||
return { status: 200, ok: true, json: async () => ({ ok: true }) };
|
|
||||||
}
|
|
||||||
throw new Error('Unexpected URL: ' + url);
|
|
||||||
};
|
|
||||||
const okRes = await saveImagePreferences({ image_model: 'admin-model', image_gen_enabled: true });
|
|
||||||
results.okResStatus = okRes.status;
|
|
||||||
results.clearedKeysCount = clearedKeys.length;
|
|
||||||
|
|
||||||
console.log(JSON.stringify(results));
|
|
||||||
"""
|
|
||||||
proc = subprocess.run(["node", "--input-type=module", "-e", js], capture_output=True, text=True, check=True)
|
|
||||||
import json
|
|
||||||
data = json.loads(proc.stdout)
|
|
||||||
assert data["http500Threw"] is True
|
|
||||||
assert data["http500Status"] == 500
|
|
||||||
assert data["http500GlobalSaved"] is True
|
|
||||||
assert data["http500Phase"] == "cleanup"
|
|
||||||
assert data["networkRejectThrew"] is True
|
|
||||||
assert data["networkRejectStatus"] == 500
|
|
||||||
assert data["networkRejectGlobalSaved"] is True
|
|
||||||
assert data["networkRejectPhase"] == "cleanup"
|
|
||||||
assert data["okResStatus"] == 200
|
|
||||||
assert data["clearedKeysCount"] == 3
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.skipif(not _HAS_NODE, reason="node binary not on PATH")
|
|
||||||
def test_admin_toggle_reconciliation_and_partial_failure_reporting():
|
|
||||||
"""Verify admin toggle reconciles with authoritative server state and reports partial success accurately."""
|
|
||||||
js = """
|
|
||||||
const { initImageSettings } = await import('./static/js/settings/imageModels.js');
|
|
||||||
|
|
||||||
function createEnv() {
|
|
||||||
const elements = {};
|
|
||||||
function makeElement(id, tag = 'div') {
|
|
||||||
const listeners = {};
|
|
||||||
const el = {
|
|
||||||
id, tagName: tag.toUpperCase(),
|
|
||||||
value: '', checked: false, disabled: false, title: '',
|
|
||||||
style: {},
|
|
||||||
parentElement: { title: '', style: {} },
|
|
||||||
closest: () => ({ style: {} }),
|
|
||||||
addEventListener: (evt, cb) => { listeners[evt] = listeners[evt] || []; listeners[evt].push(cb); },
|
|
||||||
dispatchEvent: (evt) => Promise.all((listeners[evt] || []).map(cb => cb())),
|
|
||||||
length: 0, appendChild: () => {}, remove: () => {}
|
|
||||||
};
|
|
||||||
elements[id] = el;
|
|
||||||
return el;
|
|
||||||
}
|
|
||||||
|
|
||||||
const modelSel = makeElement('set-imgModelSelect', 'select');
|
|
||||||
const editModelSel = makeElement('set-imgEditModelSelect', 'select');
|
|
||||||
const qualSel = makeElement('set-imgQualitySelect', 'select');
|
|
||||||
const msg = makeElement('set-imgSettingsMsg', 'span');
|
|
||||||
const enabledToggle = makeElement('set-imgEnabledToggle', 'input');
|
|
||||||
|
|
||||||
globalThis.document = {
|
|
||||||
getElementById: (id) => elements[id] || null,
|
|
||||||
createElement: (tag) => makeElement('anon-' + Math.random(), tag),
|
|
||||||
};
|
|
||||||
globalThis.window = {
|
|
||||||
_isAdmin: true,
|
|
||||||
addEventListener: () => {},
|
|
||||||
removeEventListener: () => {},
|
|
||||||
dispatchEvent: () => {}
|
|
||||||
};
|
|
||||||
globalThis.CustomEvent = class CustomEvent {};
|
|
||||||
|
|
||||||
return { modelSel, editModelSel, qualSel, msg, enabledToggle };
|
|
||||||
}
|
|
||||||
|
|
||||||
const testResults = {};
|
|
||||||
|
|
||||||
// 1. Global POST 200 + cleanup HTTP 500 failure
|
|
||||||
{
|
|
||||||
const { enabledToggle, msg } = createEnv();
|
|
||||||
let serverGlobal = true;
|
|
||||||
globalThis.fetch = async (url, opts = {}) => {
|
|
||||||
if (url === '/api/model-endpoints') return { ok: true, json: async () => [] };
|
|
||||||
if (url === '/api/prefs') return { ok: true, json: async () => ({}) };
|
|
||||||
if (url === '/api/auth/settings') {
|
|
||||||
if (opts.method === 'POST') {
|
|
||||||
const body = JSON.parse(opts.body);
|
|
||||||
serverGlobal = body.image_gen_enabled;
|
|
||||||
return { status: 200, ok: true, json: async () => ({ ok: true }) };
|
|
||||||
}
|
|
||||||
return { ok: true, json: async () => ({ image_gen_enabled: serverGlobal }) };
|
|
||||||
}
|
|
||||||
if (url.startsWith('/api/prefs/')) {
|
|
||||||
return { status: 500, ok: false, text: async () => 'DB lock' };
|
|
||||||
}
|
|
||||||
throw new Error('Unexpected URL: ' + url);
|
|
||||||
};
|
|
||||||
await initImageSettings();
|
|
||||||
enabledToggle.checked = false;
|
|
||||||
await enabledToggle.dispatchEvent('change');
|
|
||||||
testResults.s1_checked = enabledToggle.checked;
|
|
||||||
testResults.s1_server = serverGlobal;
|
|
||||||
testResults.s1_msg = msg.textContent;
|
|
||||||
testResults.s1_color = msg.style.color;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 2. Global POST 200 + cleanup network rejection
|
|
||||||
{
|
|
||||||
const { enabledToggle, msg } = createEnv();
|
|
||||||
let serverGlobal = true;
|
|
||||||
globalThis.fetch = async (url, opts = {}) => {
|
|
||||||
if (url === '/api/model-endpoints') return { ok: true, json: async () => [] };
|
|
||||||
if (url === '/api/prefs') return { ok: true, json: async () => ({}) };
|
|
||||||
if (url === '/api/auth/settings') {
|
|
||||||
if (opts.method === 'POST') {
|
|
||||||
const body = JSON.parse(opts.body);
|
|
||||||
serverGlobal = body.image_gen_enabled;
|
|
||||||
return { status: 200, ok: true, json: async () => ({ ok: true }) };
|
|
||||||
}
|
|
||||||
return { ok: true, json: async () => ({ image_gen_enabled: serverGlobal }) };
|
|
||||||
}
|
|
||||||
if (url.startsWith('/api/prefs/')) {
|
|
||||||
throw new Error('Network offline');
|
|
||||||
}
|
|
||||||
throw new Error('Unexpected URL: ' + url);
|
|
||||||
};
|
|
||||||
await initImageSettings();
|
|
||||||
enabledToggle.checked = false;
|
|
||||||
await enabledToggle.dispatchEvent('change');
|
|
||||||
testResults.s2_checked = enabledToggle.checked;
|
|
||||||
testResults.s2_server = serverGlobal;
|
|
||||||
testResults.s2_msg = msg.textContent;
|
|
||||||
testResults.s2_color = msg.style.color;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 3. Successful cleanup
|
|
||||||
{
|
|
||||||
const { enabledToggle, msg } = createEnv();
|
|
||||||
let serverGlobal = true;
|
|
||||||
globalThis.fetch = async (url, opts = {}) => {
|
|
||||||
if (url === '/api/model-endpoints') return { ok: true, json: async () => [] };
|
|
||||||
if (url === '/api/prefs') return { ok: true, json: async () => ({}) };
|
|
||||||
if (url === '/api/auth/settings') {
|
|
||||||
if (opts.method === 'POST') {
|
|
||||||
const body = JSON.parse(opts.body);
|
|
||||||
serverGlobal = body.image_gen_enabled;
|
|
||||||
return { status: 200, ok: true, json: async () => ({ ok: true }) };
|
|
||||||
}
|
|
||||||
return { ok: true, json: async () => ({ image_gen_enabled: serverGlobal }) };
|
|
||||||
}
|
|
||||||
if (url.startsWith('/api/prefs/')) {
|
|
||||||
return { status: 200, ok: true, json: async () => ({ ok: true }) };
|
|
||||||
}
|
|
||||||
throw new Error('Unexpected URL: ' + url);
|
|
||||||
};
|
|
||||||
await initImageSettings();
|
|
||||||
enabledToggle.checked = false;
|
|
||||||
await enabledToggle.dispatchEvent('change');
|
|
||||||
testResults.s3_checked = enabledToggle.checked;
|
|
||||||
testResults.s3_server = serverGlobal;
|
|
||||||
testResults.s3_msg = msg.textContent;
|
|
||||||
testResults.s3_color = msg.style.color;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 4. Failed global POST (HTTP 500)
|
|
||||||
{
|
|
||||||
const { enabledToggle, msg } = createEnv();
|
|
||||||
let serverGlobal = true;
|
|
||||||
globalThis.fetch = async (url, opts = {}) => {
|
|
||||||
if (url === '/api/model-endpoints') return { ok: true, json: async () => [] };
|
|
||||||
if (url === '/api/prefs') return { ok: true, json: async () => ({}) };
|
|
||||||
if (url === '/api/auth/settings') {
|
|
||||||
if (opts.method === 'POST') {
|
|
||||||
return { status: 500, ok: false, text: async () => 'Internal Server Error' };
|
|
||||||
}
|
|
||||||
return { ok: true, json: async () => ({ image_gen_enabled: serverGlobal }) };
|
|
||||||
}
|
|
||||||
if (url.startsWith('/api/prefs/')) {
|
|
||||||
return { status: 200, ok: true, json: async () => ({ ok: true }) };
|
|
||||||
}
|
|
||||||
throw new Error('Unexpected URL: ' + url);
|
|
||||||
};
|
|
||||||
await initImageSettings();
|
|
||||||
enabledToggle.checked = false;
|
|
||||||
await enabledToggle.dispatchEvent('change');
|
|
||||||
testResults.s4_checked = enabledToggle.checked;
|
|
||||||
testResults.s4_server = serverGlobal;
|
|
||||||
testResults.s4_msg = msg.textContent;
|
|
||||||
testResults.s4_color = msg.style.color;
|
|
||||||
}
|
|
||||||
|
|
||||||
console.log(JSON.stringify(testResults));
|
|
||||||
"""
|
|
||||||
proc = subprocess.run(["node", "--input-type=module", "-e", js], capture_output=True, text=True, check=True)
|
|
||||||
import json
|
|
||||||
data = json.loads(proc.stdout)
|
|
||||||
|
|
||||||
# 1. Successful global POST followed by HTTP 500 cleanup failure:
|
|
||||||
# Toggle does not revert, server policy was persisted, and message reports partial success + cleanup error
|
|
||||||
assert data["s1_checked"] is False
|
|
||||||
assert data["s1_server"] is False
|
|
||||||
assert "Policy saved, but failed to clear preferences" in data["s1_msg"]
|
|
||||||
assert "DB lock" in data["s1_msg"]
|
|
||||||
assert data["s1_color"] == "var(--red)"
|
|
||||||
|
|
||||||
# 2. Successful global POST followed by network rejection during cleanup:
|
|
||||||
# Toggle does not revert, server policy was persisted, and message reports network failure
|
|
||||||
assert data["s2_checked"] is False
|
|
||||||
assert data["s2_server"] is False
|
|
||||||
assert "Policy saved, but failed to clear preferences" in data["s2_msg"]
|
|
||||||
assert "Network offline" in data["s2_msg"]
|
|
||||||
assert data["s2_color"] == "var(--red)"
|
|
||||||
|
|
||||||
# 3. Successful cleanup:
|
|
||||||
# Toggle saved, server updated, success message displayed
|
|
||||||
assert data["s3_checked"] is False
|
|
||||||
assert data["s3_server"] is False
|
|
||||||
assert data["s3_msg"] == "Saved"
|
|
||||||
assert data["s3_color"] == "var(--fg)"
|
|
||||||
|
|
||||||
# 4. Failed global POST:
|
|
||||||
# Toggle reverts to authoritative server state, error displayed
|
|
||||||
assert data["s4_checked"] is True
|
|
||||||
assert data["s4_server"] is True
|
|
||||||
assert data["s4_msg"] == "Failed to save"
|
|
||||||
assert data["s4_color"] == "var(--red)"
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.skipif(not _HAS_NODE, reason="node binary not on PATH")
|
|
||||||
def test_non_admin_toggle_rejection_and_ui_behavior():
|
|
||||||
"""Verify non-admin cannot change image_gen_enabled via API or UI, and receives unambiguous feedback."""
|
|
||||||
js = """
|
|
||||||
const { saveImagePreferences } = await import('./static/js/settings/imagePreferences.js');
|
|
||||||
|
|
||||||
const results = {};
|
|
||||||
|
|
||||||
// 1. Direct call to saveImagePreferences with only non-admin excluded toggle
|
|
||||||
globalThis.window = { dispatchEvent: () => {} };
|
|
||||||
globalThis.CustomEvent = class CustomEvent {};
|
|
||||||
globalThis.fetch = async (url) => {
|
|
||||||
if (url === '/api/auth/settings') return { status: 403, ok: false, text: async () => 'Forbidden' };
|
|
||||||
throw new Error('Unexpected URL: ' + url);
|
|
||||||
};
|
|
||||||
try {
|
|
||||||
await saveImagePreferences({ image_gen_enabled: true });
|
|
||||||
results.toggleOnlyThrew = false;
|
|
||||||
} catch (err) {
|
|
||||||
results.toggleOnlyThrew = true;
|
|
||||||
results.toggleOnlyStatus = err.status;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 2. Simulated UI interaction in imageModels.js for non-admin
|
|
||||||
const elements = {};
|
|
||||||
function makeElement(id, tag = 'div') {
|
|
||||||
const listeners = {};
|
|
||||||
const el = {
|
|
||||||
id, tagName: tag.toUpperCase(),
|
|
||||||
value: '', checked: false, disabled: false, title: '',
|
|
||||||
style: {},
|
|
||||||
parentElement: { title: '', style: {} },
|
|
||||||
closest: () => ({ style: {} }),
|
|
||||||
addEventListener: (evt, cb) => { listeners[evt] = listeners[evt] || []; listeners[evt].push(cb); },
|
|
||||||
dispatchEvent: (evt) => Promise.all((listeners[evt] || []).map(cb => cb())),
|
|
||||||
length: 0, appendChild: () => {}, remove: () => {}
|
|
||||||
};
|
|
||||||
elements[id] = el;
|
|
||||||
return el;
|
|
||||||
}
|
|
||||||
|
|
||||||
const modelSel = makeElement('set-imgModelSelect', 'select');
|
|
||||||
const editModelSel = makeElement('set-imgEditModelSelect', 'select');
|
|
||||||
const qualSel = makeElement('set-imgQualitySelect', 'select');
|
|
||||||
const msg = makeElement('set-imgSettingsMsg', 'span');
|
|
||||||
const enabledToggle = makeElement('set-imgEnabledToggle', 'input');
|
|
||||||
|
|
||||||
globalThis.document = {
|
|
||||||
getElementById: (id) => elements[id] || null,
|
|
||||||
createElement: (tag) => makeElement('anon-' + Math.random(), tag),
|
|
||||||
};
|
|
||||||
globalThis.window._isAdmin = false;
|
|
||||||
globalThis.window.addEventListener = () => {};
|
|
||||||
globalThis.window.removeEventListener = () => {};
|
|
||||||
|
|
||||||
const apiCalls = [];
|
|
||||||
globalThis.fetch = async (url, opts = {}) => {
|
|
||||||
apiCalls.push({ url, method: opts.method || 'GET' });
|
|
||||||
if (url === '/api/model-endpoints') return { ok: true, json: async () => [] };
|
|
||||||
if (url === '/api/prefs') return { ok: true, json: async () => ({}) };
|
|
||||||
if (url === '/api/auth/settings') {
|
|
||||||
if (opts.method === 'POST') return { status: 403, ok: false, text: async () => 'Forbidden' };
|
|
||||||
return { ok: true, json: async () => ({ image_gen_enabled: true, image_model: 'global-model' }) };
|
|
||||||
}
|
|
||||||
if (url.startsWith('/api/prefs/')) {
|
|
||||||
return { status: 200, ok: true, json: async () => ({ ok: true }) };
|
|
||||||
}
|
|
||||||
throw new Error('Unexpected URL: ' + url);
|
|
||||||
};
|
|
||||||
|
|
||||||
const { initImageSettings } = await import('./static/js/settings/imageModels.js');
|
|
||||||
await initImageSettings();
|
|
||||||
|
|
||||||
// Check non-admin initial UI state
|
|
||||||
results.nonAdminToggleDisabled = enabledToggle.disabled;
|
|
||||||
results.nonAdminToggleTitle = enabledToggle.title;
|
|
||||||
results.nonAdminToggleChecked = enabledToggle.checked;
|
|
||||||
|
|
||||||
// Simulate non-admin attempting to toggle image generation
|
|
||||||
enabledToggle.checked = false; // user clicked switch to turn off
|
|
||||||
enabledToggle.dispatchEvent('change');
|
|
||||||
results.revertedChecked = enabledToggle.checked;
|
|
||||||
results.revertedMsgText = msg.textContent;
|
|
||||||
results.revertedMsgColor = msg.style.color;
|
|
||||||
|
|
||||||
// Simulate non-admin changing model preference
|
|
||||||
modelSel.value = 'user-selected-model';
|
|
||||||
await modelSel.dispatchEvent('change');
|
|
||||||
results.modelChangeMsg = msg.textContent;
|
|
||||||
|
|
||||||
// 3. Admin UI interaction in imageModels.js
|
|
||||||
globalThis.window._isAdmin = true;
|
|
||||||
const adminToggle = makeElement('set-imgEnabledToggle', 'input');
|
|
||||||
const adminMsg = makeElement('set-imgSettingsMsg', 'span');
|
|
||||||
elements['set-imgEnabledToggle'] = adminToggle;
|
|
||||||
elements['set-imgSettingsMsg'] = adminMsg;
|
|
||||||
|
|
||||||
let adminSavedToggle = null;
|
|
||||||
globalThis.fetch = async (url, opts = {}) => {
|
|
||||||
if (url === '/api/model-endpoints') return { ok: true, json: async () => [] };
|
|
||||||
if (url === '/api/prefs') return { ok: true, json: async () => ({}) };
|
|
||||||
if (url === '/api/auth/settings') {
|
|
||||||
if (opts.method === 'POST') {
|
|
||||||
const body = JSON.parse(opts.body);
|
|
||||||
adminSavedToggle = body.image_gen_enabled;
|
|
||||||
return { status: 200, ok: true, json: async () => ({ ok: true }) };
|
|
||||||
}
|
|
||||||
return { ok: true, json: async () => ({ image_gen_enabled: true, image_model: 'global-model' }) };
|
|
||||||
}
|
|
||||||
if (url.startsWith('/api/prefs/')) {
|
|
||||||
return { status: 200, ok: true, json: async () => ({ ok: true }) };
|
|
||||||
}
|
|
||||||
throw new Error('Unexpected URL: ' + url);
|
|
||||||
};
|
|
||||||
|
|
||||||
await initImageSettings();
|
|
||||||
results.adminToggleDisabled = adminToggle.disabled;
|
|
||||||
adminToggle.checked = false;
|
|
||||||
await adminToggle.dispatchEvent('change');
|
|
||||||
results.adminSavedToggle = adminSavedToggle;
|
|
||||||
results.adminMsg = adminMsg.textContent;
|
|
||||||
|
|
||||||
console.log(JSON.stringify(results));
|
|
||||||
"""
|
|
||||||
proc = subprocess.run(["node", "--input-type=module", "-e", js], capture_output=True, text=True, check=True)
|
|
||||||
import json
|
|
||||||
data = json.loads(proc.stdout)
|
|
||||||
|
|
||||||
# 1. Non-admin toggle-only save threw 403
|
|
||||||
assert data["toggleOnlyThrew"] is True
|
|
||||||
assert data["toggleOnlyStatus"] == 403
|
|
||||||
|
|
||||||
# 2. Non-admin UI has disabled toggle and rejects changes with warning
|
|
||||||
assert data["nonAdminToggleDisabled"] is True
|
|
||||||
assert "Admin access is required" in data["nonAdminToggleTitle"]
|
|
||||||
assert data["nonAdminToggleChecked"] is True
|
|
||||||
assert data["revertedChecked"] is True
|
|
||||||
assert "Admin access is required" in data["revertedMsgText"]
|
|
||||||
assert data["revertedMsgColor"] == "var(--red)"
|
|
||||||
|
|
||||||
# 3. Non-admin model changes succeed
|
|
||||||
assert data["modelChangeMsg"] == "Saved"
|
|
||||||
|
|
||||||
# 4. Admin UI allows toggle changes and persists to auth settings
|
|
||||||
assert data["adminToggleDisabled"] is False
|
|
||||||
assert data["adminSavedToggle"] is False
|
|
||||||
assert data["adminMsg"] == "Saved"
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -48,7 +48,6 @@ def _patch_loop_basics(monkeypatch):
|
|||||||
monkeypatch.setattr(al, "get_setting", lambda key, default=None: default, raising=False)
|
monkeypatch.setattr(al, "get_setting", lambda key, default=None: default, raising=False)
|
||||||
monkeypatch.setattr(al, "get_mcp_manager", lambda: None, raising=False)
|
monkeypatch.setattr(al, "get_mcp_manager", lambda: None, raising=False)
|
||||||
monkeypatch.setattr(al, "estimate_tokens", lambda *a, **k: 10, raising=False)
|
monkeypatch.setattr(al, "estimate_tokens", lambda *a, **k: 10, raising=False)
|
||||||
monkeypatch.setattr("src.tool_capabilities.TOOL_APPROVAL_GATE_ENABLED", False)
|
|
||||||
|
|
||||||
|
|
||||||
def test_detects_strong_guide_only_turns():
|
def test_detects_strong_guide_only_turns():
|
||||||
|
|||||||
@@ -242,3 +242,72 @@ def test_multiple_trace_archives_in_directory(tmp_path):
|
|||||||
with zipfile.ZipFile(z2, "r") as z:
|
with zipfile.ZipFile(z2, "r") as z:
|
||||||
content = json.loads(z.read("0.trace").decode().strip())
|
content = json.loads(z.read("0.trace").decode().strip())
|
||||||
assert content["params"]["value"] == "[REDACTED]"
|
assert content["params"]["value"] == "[REDACTED]"
|
||||||
|
|
||||||
|
|
||||||
|
def test_fail_closed_malformed_jsonl_record(tmp_path):
|
||||||
|
"""Malformed JSONL lines in .trace archives fail closed and delete the archive."""
|
||||||
|
zp = str(tmp_path / "malformed_jsonl.zip")
|
||||||
|
with zipfile.ZipFile(zp, "w") as z:
|
||||||
|
z.writestr(
|
||||||
|
"0-trace.trace",
|
||||||
|
b'{"type": "action", "params": {"value": "secret123"}}\n'
|
||||||
|
b'this is invalid json {"password": "leak_raw_pass"}\n'
|
||||||
|
)
|
||||||
|
ok = sanitize_trace_archive(zp)
|
||||||
|
assert ok is False
|
||||||
|
assert not os.path.exists(zp)
|
||||||
|
assert not os.path.exists(zp + ".tmp")
|
||||||
|
|
||||||
|
|
||||||
|
def test_fail_closed_malformed_jsonl_truncated(tmp_path):
|
||||||
|
"""Truncated JSONL records in .network archives fail closed and delete the archive."""
|
||||||
|
zp = str(tmp_path / "truncated_jsonl.zip")
|
||||||
|
with zipfile.ZipFile(zp, "w") as z:
|
||||||
|
z.writestr(
|
||||||
|
"0-trace.network",
|
||||||
|
b'{"type": "request", "request": {"url": "https://example.com/api/auth", "headers": [{"name": "Authorization", "value": "Bearer secret_tok"'
|
||||||
|
)
|
||||||
|
ok = sanitize_trace_archive(zp)
|
||||||
|
assert ok is False
|
||||||
|
assert not os.path.exists(zp)
|
||||||
|
assert not os.path.exists(zp + ".tmp")
|
||||||
|
|
||||||
|
|
||||||
|
def test_fail_closed_malformed_sensitive_body(tmp_path):
|
||||||
|
"""Malformed sensitive request body in trace archives fails closed and deletes archive."""
|
||||||
|
zp = str(tmp_path / "malformed_sensitive_body.zip")
|
||||||
|
with zipfile.ZipFile(zp, "w") as z:
|
||||||
|
record = {
|
||||||
|
"type": "request",
|
||||||
|
"request": {
|
||||||
|
"url": "https://example.com/api/login",
|
||||||
|
"postData": '{"password": "super_secret_pw", "corrupt_trailing": ',
|
||||||
|
},
|
||||||
|
}
|
||||||
|
z.writestr("0-trace.network", json.dumps(record).encode() + b"\n")
|
||||||
|
ok = sanitize_trace_archive(zp)
|
||||||
|
assert ok is False
|
||||||
|
assert not os.path.exists(zp)
|
||||||
|
assert not os.path.exists(zp + ".tmp")
|
||||||
|
|
||||||
|
|
||||||
|
def test_fail_closed_malformed_har_member(tmp_path):
|
||||||
|
"""Malformed JSON in .har archives fails closed and deletes the archive."""
|
||||||
|
zp = str(tmp_path / "malformed_har.zip")
|
||||||
|
with zipfile.ZipFile(zp, "w") as z:
|
||||||
|
z.writestr("network.har", b'{"log": {"entries": [ malformed JSON content')
|
||||||
|
ok = sanitize_trace_archive(zp)
|
||||||
|
assert ok is False
|
||||||
|
assert not os.path.exists(zp)
|
||||||
|
assert not os.path.exists(zp + ".tmp")
|
||||||
|
|
||||||
|
|
||||||
|
def test_fail_closed_invalid_utf8_jsonl_record(tmp_path):
|
||||||
|
"""Non-UTF8 byte sequences in JSONL trace records fail closed and delete the archive."""
|
||||||
|
zp = str(tmp_path / "invalid_utf8.zip")
|
||||||
|
with zipfile.ZipFile(zp, "w") as z:
|
||||||
|
z.writestr("0-trace.trace", b"\xff\xfe\x00\x00 invalid utf8 raw bytes\n")
|
||||||
|
ok = sanitize_trace_archive(zp)
|
||||||
|
assert ok is False
|
||||||
|
assert not os.path.exists(zp)
|
||||||
|
assert not os.path.exists(zp + ".tmp")
|
||||||
|
|||||||
@@ -0,0 +1,591 @@
|
|||||||
|
"""Tests for the fail-closed artifact verification step (.github/scripts/verify_sanitized_artifacts.py).
|
||||||
|
|
||||||
|
Validates that:
|
||||||
|
1. Safe JUnit XML reporting is preserved across all scenarios.
|
||||||
|
2. Abnormal runner termination without verified disposable CI fixture purges all diagnostics.
|
||||||
|
3. Valid disposable CI environments sanitize trace archives fail-closed.
|
||||||
|
4. Malformed trace archives are deleted fail-closed without corrupting or deleting JUnit XML reports.
|
||||||
|
5. In-progress temporary files, HAR files, and database dumps are purged unconditionally.
|
||||||
|
6. Top-level exceptions fail closed to purge all non-XML diagnostics.
|
||||||
|
7. Negative controls:
|
||||||
|
- Verifier failure exits nonzero.
|
||||||
|
- Verifier failure prevents upload in workflow contract.
|
||||||
|
- Missing verifier execution prevents upload.
|
||||||
|
- Valid XML containing a synthetic secret is not published unchanged.
|
||||||
|
- Malformed traces are removed.
|
||||||
|
- Failed artifact deletion prevents authorization.
|
||||||
|
- A normal successful run retains safe test results.
|
||||||
|
"""
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import importlib.util
|
||||||
|
import json
|
||||||
|
import os
|
||||||
|
import subprocess
|
||||||
|
import sys
|
||||||
|
import tempfile
|
||||||
|
import xml.etree.ElementTree as ET
|
||||||
|
import zipfile
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
import yaml
|
||||||
|
|
||||||
|
REPO_ROOT = Path(__file__).resolve().parents[1]
|
||||||
|
VERIFY_SCRIPT = REPO_ROOT / ".github" / "scripts" / "verify_sanitized_artifacts.py"
|
||||||
|
WORKFLOW_PATH = REPO_ROOT / ".github" / "workflows" / "native-test-suites.yml"
|
||||||
|
|
||||||
|
spec = importlib.util.spec_from_file_location("verify_sanitized_artifacts", str(VERIFY_SCRIPT))
|
||||||
|
verify_module = importlib.util.module_from_spec(spec)
|
||||||
|
sys.modules["verify_sanitized_artifacts"] = verify_module
|
||||||
|
spec.loader.exec_module(verify_module)
|
||||||
|
|
||||||
|
is_valid_disposable_ci_fixture = verify_module.is_valid_disposable_ci_fixture
|
||||||
|
verify_and_sanitize_artifacts = verify_module.verify_and_sanitize_artifacts
|
||||||
|
sanitize_and_verify_junit_xml = verify_module.sanitize_and_verify_junit_xml
|
||||||
|
safe_remove = verify_module.safe_remove
|
||||||
|
|
||||||
|
|
||||||
|
def _setup_disposable_ci(tmp_path: Path) -> dict[str, str]:
|
||||||
|
data_dir = tmp_path / "odysseus-photo-editor-e2e-fixture"
|
||||||
|
data_dir.mkdir(parents=True, exist_ok=True)
|
||||||
|
db_path = data_dir / "app.db"
|
||||||
|
db_path.write_bytes(b"SQLite format 3\x00")
|
||||||
|
marker_file = data_dir / ".disposable-ci-fixture.json"
|
||||||
|
marker_file.write_text(
|
||||||
|
json.dumps({
|
||||||
|
"synthetic": True,
|
||||||
|
"authenticated": True,
|
||||||
|
"database": str(db_path),
|
||||||
|
})
|
||||||
|
)
|
||||||
|
return {
|
||||||
|
"CI": "true",
|
||||||
|
"GITHUB_ACTIONS": "true",
|
||||||
|
"ODYSSEUS_E2E_DISPOSABLE_ARTIFACTS": "1",
|
||||||
|
"PHOTO_EDITOR_E2E_DATA_DIR": str(data_dir),
|
||||||
|
"PHOTO_EDITOR_E2E_DB_PATH": str(db_path),
|
||||||
|
"PHOTO_EDITOR_E2E_AUTH": "1",
|
||||||
|
"ODYSSEUS_TEST_PYTHON": sys.executable,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def test_preserves_valid_junit_xml_cleanly(tmp_path):
|
||||||
|
"""Valid JUnit XML files are preserved intact."""
|
||||||
|
results_dir = tmp_path / "test-results"
|
||||||
|
results_dir.mkdir()
|
||||||
|
junit = results_dir / "browser-junit.xml"
|
||||||
|
junit.write_text('<testsuites><testsuite name="photo-editor" tests="137" failures="0"/></testsuites>')
|
||||||
|
|
||||||
|
ok = verify_and_sanitize_artifacts(str(results_dir))
|
||||||
|
assert ok is True
|
||||||
|
assert junit.exists()
|
||||||
|
assert 'tests="137"' in junit.read_text()
|
||||||
|
|
||||||
|
|
||||||
|
def test_normal_successful_run_retains_safe_test_results(tmp_path):
|
||||||
|
"""A normal successful run retains safe test results without modification."""
|
||||||
|
results_dir = tmp_path / "test-results"
|
||||||
|
results_dir.mkdir()
|
||||||
|
junit = results_dir / "junit.xml"
|
||||||
|
original_xml = '<testsuites><testsuite name="native-node" tests="43" failures="0"><testcase name="test-1"/></testsuite></testsuites>'
|
||||||
|
junit.write_text(original_xml)
|
||||||
|
|
||||||
|
res = subprocess.run(
|
||||||
|
[sys.executable, str(VERIFY_SCRIPT), str(results_dir)],
|
||||||
|
capture_output=True,
|
||||||
|
text=True,
|
||||||
|
)
|
||||||
|
assert res.returncode == 0
|
||||||
|
assert junit.exists()
|
||||||
|
content = junit.read_text()
|
||||||
|
assert 'name="native-node"' in content
|
||||||
|
assert 'tests="43"' in content
|
||||||
|
assert 'test-1' in content
|
||||||
|
|
||||||
|
|
||||||
|
def test_valid_xml_containing_synthetic_secret_not_published_unchanged(tmp_path):
|
||||||
|
"""Negative control: Valid XML containing a synthetic secret is not published unchanged."""
|
||||||
|
results_dir = tmp_path / "test-results"
|
||||||
|
results_dir.mkdir()
|
||||||
|
junit = results_dir / "browser-junit.xml"
|
||||||
|
secret_text = "SuperSecretSyntheticToken98765"
|
||||||
|
xml_with_secret = f"""<testsuites>
|
||||||
|
<testsuite name="photo-editor" tests="1" failures="1">
|
||||||
|
<testcase name="auth-test">
|
||||||
|
<failure message="Encountered password={secret_text} in log">
|
||||||
|
Stack trace containing synthetic-secret authorization: Bearer {secret_text}
|
||||||
|
</failure>
|
||||||
|
<system-out>session_token={secret_text}</system-out>
|
||||||
|
</testcase>
|
||||||
|
</testsuite>
|
||||||
|
</testsuites>"""
|
||||||
|
junit.write_text(xml_with_secret)
|
||||||
|
|
||||||
|
ok = verify_and_sanitize_artifacts(str(results_dir))
|
||||||
|
assert ok is True
|
||||||
|
assert junit.exists()
|
||||||
|
|
||||||
|
sanitized_content = junit.read_text()
|
||||||
|
# The raw secret MUST NOT appear anywhere in the published report
|
||||||
|
assert secret_text not in sanitized_content
|
||||||
|
# The redaction marker MUST appear
|
||||||
|
assert "[REDACTED]" in sanitized_content
|
||||||
|
# The XML must still be structurally valid JUnit XML
|
||||||
|
tree = ET.parse(junit)
|
||||||
|
assert tree.getroot().tag == "testsuites"
|
||||||
|
|
||||||
|
|
||||||
|
def test_exact_property_api_key_cannot_appear_unchanged_in_uploaded_report(tmp_path):
|
||||||
|
"""Regression: <property name="api_key" value="sk-example-123" /> cannot appear unchanged in uploaded report."""
|
||||||
|
results_dir = tmp_path / "test-results"
|
||||||
|
results_dir.mkdir()
|
||||||
|
junit = results_dir / "browser-junit.xml"
|
||||||
|
xml_with_property = """<testsuites name="photo-editor" tests="1" failures="0">
|
||||||
|
<testsuite name="brush-engine.spec.js" tests="1" failures="0">
|
||||||
|
<properties>
|
||||||
|
<property name="api_key" value="sk-example-123" />
|
||||||
|
</properties>
|
||||||
|
<property name="api_key" value="sk-example-123" />
|
||||||
|
<testcase name="brush presets and shared stroke controls remain reusable" classname="photo-editor/brush-engine.spec.js" time="0.5" />
|
||||||
|
</testsuite>
|
||||||
|
</testsuites>"""
|
||||||
|
junit.write_text(xml_with_property)
|
||||||
|
|
||||||
|
ok = verify_and_sanitize_artifacts(str(results_dir))
|
||||||
|
assert ok is True
|
||||||
|
assert junit.exists()
|
||||||
|
|
||||||
|
sanitized_xml = junit.read_text()
|
||||||
|
# The exact sensitive property and value MUST NOT appear anywhere in the published report
|
||||||
|
assert "sk-example-123" not in sanitized_xml
|
||||||
|
assert "api_key" not in sanitized_xml
|
||||||
|
assert "<property" not in sanitized_xml
|
||||||
|
assert "<properties" not in sanitized_xml
|
||||||
|
|
||||||
|
# Test identity and structure must remain valid
|
||||||
|
tree = ET.parse(junit)
|
||||||
|
root = tree.getroot()
|
||||||
|
assert root.tag == "testsuites"
|
||||||
|
cases = list(root.iter("testcase"))
|
||||||
|
assert len(cases) == 1
|
||||||
|
assert cases[0].get("name") == "brush presets and shared stroke controls remain reusable"
|
||||||
|
assert cases[0].get("classname") == "photo-editor/brush-engine.spec.js"
|
||||||
|
|
||||||
|
|
||||||
|
def test_standard_node_and_playwright_reports_remain_valid_and_retain_identities(tmp_path):
|
||||||
|
"""Standard Node and Playwright JUnit reports remain structurally valid and retain test identities."""
|
||||||
|
results_dir = tmp_path / "test-results"
|
||||||
|
results_dir.mkdir()
|
||||||
|
|
||||||
|
# 1. Standard Node report representation (similar to node:test runner output)
|
||||||
|
node_dir = results_dir / "native-node"
|
||||||
|
node_dir.mkdir()
|
||||||
|
node_junit = node_dir / "junit.xml"
|
||||||
|
node_junit.write_text("""<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<testsuites>
|
||||||
|
<testcase name="research cards distinguish live work, handoff, completion and no evidence" time="0.000549" classname="test" file="tests/backgroundToolJobs.test.mjs" />
|
||||||
|
<testcase name="Chat Photo Editor Action Integrity and Turn Scoping" time="0.499862" classname="test" file="tests/chatPhotoEditorIntegrity.test.mjs" />
|
||||||
|
</testsuites>""")
|
||||||
|
|
||||||
|
# 2. Standard Playwright report representation (with suites, timing, counts, and failure classifications)
|
||||||
|
browser_junit = results_dir / "browser-junit.xml"
|
||||||
|
browser_junit.write_text("""<testsuites id="" name="" tests="2" failures="1" skipped="0" errors="0" time="12.5">
|
||||||
|
<testsuite name="photo-editor/brush-engine.spec.js" timestamp="2026-10-31T00:00:00Z" hostname="chromium" tests="2" failures="1" skipped="0" time="12.5" errors="0">
|
||||||
|
<properties>
|
||||||
|
<property name="platform" value="linux" />
|
||||||
|
<property name="api_key" value="sk-example-123" />
|
||||||
|
</properties>
|
||||||
|
<testcase name="brush presets and shared stroke controls remain reusable" classname="photo-editor/brush-engine.spec.js" time="1.2" />
|
||||||
|
<testcase name="long sampled strokes stay responsive and undo atomically" classname="photo-editor/brush-engine.spec.js" time="2.4">
|
||||||
|
<failure message="expect(strokeDuration - controlDuration).toBeLessThan(5000)" type="FAILURE">
|
||||||
|
Error: expect(strokeDuration - controlDuration).toBeLessThan(5000)
|
||||||
|
at brush-engine.spec.js:84:10
|
||||||
|
</failure>
|
||||||
|
<system-out>Captured console stdout dump with session_token=abc12345</system-out>
|
||||||
|
<system-err>Captured console stderr dump</system-err>
|
||||||
|
</testcase>
|
||||||
|
</testsuite>
|
||||||
|
</testsuites>""")
|
||||||
|
|
||||||
|
ok_node = verify_and_sanitize_artifacts(str(node_dir))
|
||||||
|
assert ok_node is True
|
||||||
|
ok_browser = verify_and_sanitize_artifacts(str(results_dir))
|
||||||
|
assert ok_browser is True
|
||||||
|
|
||||||
|
# Validate Node report
|
||||||
|
node_tree = ET.parse(node_junit)
|
||||||
|
node_cases = list(node_tree.iter("testcase"))
|
||||||
|
assert len(node_cases) == 2
|
||||||
|
assert node_cases[0].get("name") == "research cards distinguish live work, handoff, completion and no evidence"
|
||||||
|
assert node_cases[1].get("name") == "Chat Photo Editor Action Integrity and Turn Scoping"
|
||||||
|
assert node_cases[0].get("file") == "tests/backgroundToolJobs.test.mjs"
|
||||||
|
|
||||||
|
# Validate Playwright report
|
||||||
|
pw_tree = ET.parse(browser_junit)
|
||||||
|
pw_root = pw_tree.getroot()
|
||||||
|
assert pw_root.tag == "testsuites"
|
||||||
|
assert pw_root.get("tests") == "2"
|
||||||
|
assert pw_root.get("failures") == "1"
|
||||||
|
|
||||||
|
pw_cases = list(pw_tree.iter("testcase"))
|
||||||
|
assert len(pw_cases) == 2
|
||||||
|
assert pw_cases[0].get("name") == "brush presets and shared stroke controls remain reusable"
|
||||||
|
assert pw_cases[1].get("name") == "long sampled strokes stay responsive and undo atomically"
|
||||||
|
|
||||||
|
# Verify failure classification is preserved
|
||||||
|
failures = list(pw_cases[1].iter("failure"))
|
||||||
|
assert len(failures) == 1
|
||||||
|
assert "toBeLessThan(5000)" in failures[0].get("message")
|
||||||
|
assert failures[0].get("type") == "FAILURE"
|
||||||
|
|
||||||
|
# Verify properties and system-out/err were pruned
|
||||||
|
pw_text = browser_junit.read_text()
|
||||||
|
assert "sk-example-123" not in pw_text
|
||||||
|
assert "api_key" not in pw_text
|
||||||
|
assert "<properties" not in pw_text
|
||||||
|
assert "<property" not in pw_text
|
||||||
|
assert "<system-out" not in pw_text
|
||||||
|
assert "<system-err" not in pw_text
|
||||||
|
assert "session_token" not in pw_text
|
||||||
|
|
||||||
|
|
||||||
|
def test_skipped_element_content_tail_and_attributes_leak_prevented(tmp_path):
|
||||||
|
"""Regression: <skipped>PRIVATE_VALUE_123</skipped>, attributes, and tails cannot leak sensitive values."""
|
||||||
|
results_dir = tmp_path / "test-results"
|
||||||
|
results_dir.mkdir()
|
||||||
|
junit = results_dir / "browser-junit.xml"
|
||||||
|
xml_with_skipped = """<testsuites>
|
||||||
|
<testsuite name="sample" tests="1" skipped="1">
|
||||||
|
<testcase name="skipped-case" classname="sample">
|
||||||
|
<skipped message="PRIVATE_VALUE_123" type="UNOFFICIAL_SECRET_456">PRIVATE_VALUE_123</skipped>PRIVATE_VALUE_123
|
||||||
|
</testcase>
|
||||||
|
</testsuite>
|
||||||
|
</testsuites>"""
|
||||||
|
junit.write_text(xml_with_skipped)
|
||||||
|
|
||||||
|
ok = verify_and_sanitize_artifacts(str(results_dir))
|
||||||
|
assert ok is True
|
||||||
|
assert junit.exists()
|
||||||
|
|
||||||
|
sanitized_xml = junit.read_text()
|
||||||
|
# Synthetic sensitive values that do not match secret regexes must be absent
|
||||||
|
assert "PRIVATE_VALUE_123" not in sanitized_xml
|
||||||
|
assert "UNOFFICIAL_SECRET_456" not in sanitized_xml
|
||||||
|
|
||||||
|
# The output must remain valid JUnit XML
|
||||||
|
tree = ET.parse(junit)
|
||||||
|
root = tree.getroot()
|
||||||
|
assert root.tag == "testsuites"
|
||||||
|
cases = list(root.iter("testcase"))
|
||||||
|
assert len(cases) == 1
|
||||||
|
|
||||||
|
# Testcase identity remains intact
|
||||||
|
testcase = cases[0]
|
||||||
|
assert testcase.get("name") == "skipped-case"
|
||||||
|
assert testcase.get("classname") == "sample"
|
||||||
|
|
||||||
|
# Skipped element remains present and classified as skipped
|
||||||
|
skipped_elems = list(testcase.iter("skipped"))
|
||||||
|
assert len(skipped_elems) == 1
|
||||||
|
skipped_elem = skipped_elems[0]
|
||||||
|
assert skipped_elem.text is None or not skipped_elem.text.strip()
|
||||||
|
assert skipped_elem.get("message") == "skipped"
|
||||||
|
assert skipped_elem.get("type") == "skipped"
|
||||||
|
|
||||||
|
|
||||||
|
def test_unrecognized_xml_purged(tmp_path):
|
||||||
|
"""Negative control: XML reports that are not recognized JUnit reports are purged."""
|
||||||
|
results_dir = tmp_path / "test-results"
|
||||||
|
results_dir.mkdir()
|
||||||
|
# Non-JUnit filename
|
||||||
|
creds_xml = results_dir / "credentials.xml"
|
||||||
|
creds_xml.write_text("<credentials><token>secret</token></credentials>")
|
||||||
|
# Non-JUnit root element
|
||||||
|
bad_root_xml = results_dir / "junit.xml"
|
||||||
|
bad_root_xml.write_text("<app_config><password>secret</password></app_config>")
|
||||||
|
|
||||||
|
ok = verify_and_sanitize_artifacts(str(results_dir))
|
||||||
|
# Non-JUnit XML files must be purged
|
||||||
|
assert not creds_xml.exists()
|
||||||
|
assert not bad_root_xml.exists()
|
||||||
|
|
||||||
|
|
||||||
|
def test_verifier_failure_exits_nonzero(tmp_path):
|
||||||
|
"""Negative control: Verifier failure exits nonzero."""
|
||||||
|
results_dir = tmp_path / "test-results"
|
||||||
|
results_dir.mkdir()
|
||||||
|
|
||||||
|
# Pass an invalid option or target where verification fails
|
||||||
|
# Create an un-deletable file to force verification failure
|
||||||
|
bad_file = results_dir / "unauthorized.tmp"
|
||||||
|
bad_file.write_text("sensitive")
|
||||||
|
|
||||||
|
# Run CLI with an invalid directory or simulating failure
|
||||||
|
res = subprocess.run(
|
||||||
|
[sys.executable, str(VERIFY_SCRIPT), str(results_dir), "--extra-arg"],
|
||||||
|
capture_output=True,
|
||||||
|
text=True,
|
||||||
|
)
|
||||||
|
assert res.returncode != 0
|
||||||
|
|
||||||
|
|
||||||
|
def test_failed_artifact_deletion_prevents_authorization(tmp_path, monkeypatch):
|
||||||
|
"""Negative control: Failed artifact deletion prevents authorization (exits nonzero)."""
|
||||||
|
results_dir = tmp_path / "test-results"
|
||||||
|
results_dir.mkdir()
|
||||||
|
bad_file = results_dir / "leak.har"
|
||||||
|
bad_file.write_text("network log with tokens")
|
||||||
|
|
||||||
|
# Simulate deletion failure
|
||||||
|
def failing_safe_remove(fp):
|
||||||
|
return False
|
||||||
|
|
||||||
|
monkeypatch.setattr(verify_module, "safe_remove", failing_safe_remove)
|
||||||
|
|
||||||
|
ok = verify_and_sanitize_artifacts(str(results_dir))
|
||||||
|
# When deletion fails, verification MUST return False!
|
||||||
|
assert ok is False
|
||||||
|
|
||||||
|
# CLI invocation must exit nonzero
|
||||||
|
res = subprocess.run(
|
||||||
|
[sys.executable, "-c", f"""
|
||||||
|
import sys
|
||||||
|
from pathlib import Path
|
||||||
|
sys.path.insert(0, '{REPO_ROOT}')
|
||||||
|
import verify_sanitized_artifacts as vsa
|
||||||
|
vsa.safe_remove = lambda fp: False
|
||||||
|
ok = vsa.verify_and_sanitize_artifacts('{results_dir}')
|
||||||
|
sys.exit(0 if ok else 1)
|
||||||
|
"""],
|
||||||
|
capture_output=True,
|
||||||
|
text=True,
|
||||||
|
)
|
||||||
|
assert res.returncode != 0
|
||||||
|
|
||||||
|
|
||||||
|
def test_workflow_upload_steps_require_successful_verification():
|
||||||
|
"""Negative control: Workflow enforces that upload steps execute ONLY if verification succeeded."""
|
||||||
|
with open(WORKFLOW_PATH, "r", encoding="utf-8") as f:
|
||||||
|
workflow = yaml.safe_load(f)
|
||||||
|
|
||||||
|
jobs = workflow.get("jobs", {})
|
||||||
|
|
||||||
|
# 1. Native Node job
|
||||||
|
node_job = jobs.get("native-node", {})
|
||||||
|
node_steps = node_job.get("steps", [])
|
||||||
|
|
||||||
|
verify_node_step = None
|
||||||
|
upload_node_step = None
|
||||||
|
for step in node_steps:
|
||||||
|
if step.get("id") == "verify-native-results":
|
||||||
|
verify_node_step = step
|
||||||
|
if step.get("uses", "").startswith("actions/upload-artifact"):
|
||||||
|
upload_node_step = step
|
||||||
|
|
||||||
|
assert verify_node_step is not None, "verify-native-results step must exist with explicit id"
|
||||||
|
assert verify_node_step.get("if") == "always()", "verify step must run with if: always()"
|
||||||
|
|
||||||
|
assert upload_node_step is not None, "upload step in native-node must exist"
|
||||||
|
node_upload_if = upload_node_step.get("if", "")
|
||||||
|
assert "steps.verify-native-results.outcome == 'success'" in node_upload_if, (
|
||||||
|
f"Node upload step must depend on verify step success; got if: {node_upload_if}"
|
||||||
|
)
|
||||||
|
assert node_upload_if != "always()", "Node upload step must not use plain if: always()"
|
||||||
|
|
||||||
|
# 2. Browser job
|
||||||
|
browser_job = jobs.get("browser", {})
|
||||||
|
browser_steps = browser_job.get("steps", [])
|
||||||
|
|
||||||
|
verify_browser_step = None
|
||||||
|
upload_browser_step = None
|
||||||
|
for step in browser_steps:
|
||||||
|
if step.get("id") == "verify-browser-results":
|
||||||
|
verify_browser_step = step
|
||||||
|
if step.get("uses", "").startswith("actions/upload-artifact"):
|
||||||
|
upload_browser_step = step
|
||||||
|
|
||||||
|
assert verify_browser_step is not None, "verify-browser-results step must exist with explicit id"
|
||||||
|
assert verify_browser_step.get("if") == "always()", "verify step must run with if: always()"
|
||||||
|
|
||||||
|
assert upload_browser_step is not None, "upload step in browser must exist"
|
||||||
|
browser_upload_if = upload_browser_step.get("if", "")
|
||||||
|
assert "steps.verify-browser-results.outcome == 'success'" in browser_upload_if, (
|
||||||
|
f"Browser upload step must depend on verify step success; got if: {browser_upload_if}"
|
||||||
|
)
|
||||||
|
assert browser_upload_if != "always()", "Browser upload step must not use plain if: always()"
|
||||||
|
|
||||||
|
|
||||||
|
def test_fail_closed_abnormal_termination_purges_all_diagnostics(tmp_path, monkeypatch):
|
||||||
|
"""Abnormal runner termination without disposable fixture purges all diagnostics, keeping JUnit."""
|
||||||
|
monkeypatch.setenv("CI", "true")
|
||||||
|
monkeypatch.setenv("GITHUB_ACTIONS", "true")
|
||||||
|
monkeypatch.delenv("ODYSSEUS_E2E_DISPOSABLE_ARTIFACTS", raising=False)
|
||||||
|
monkeypatch.delenv("PHOTO_EDITOR_E2E_DATA_DIR", raising=False)
|
||||||
|
|
||||||
|
results_dir = tmp_path / "test-results"
|
||||||
|
results_dir.mkdir()
|
||||||
|
junit = results_dir / "browser-junit.xml"
|
||||||
|
junit.write_text('<testsuites><testsuite name="photo-editor" tests="10" failures="1"/></testsuites>')
|
||||||
|
|
||||||
|
trace = results_dir / "trace.zip"
|
||||||
|
trace.write_bytes(b"PK0304 sensitive trace archive")
|
||||||
|
har = results_dir / "session.har"
|
||||||
|
har.write_text('{"log": {"entries": ["secret"]}}')
|
||||||
|
png = results_dir / "failure.png"
|
||||||
|
png.write_bytes(b"\x89PNG\r\n\x1a\nfake screenshot")
|
||||||
|
tmp = results_dir / "temp.tmp"
|
||||||
|
tmp.write_text("in-progress trace")
|
||||||
|
db = results_dir / "app.db"
|
||||||
|
db.write_bytes(b"SQLite format 3 database")
|
||||||
|
|
||||||
|
ok = verify_and_sanitize_artifacts(str(results_dir))
|
||||||
|
assert ok is True
|
||||||
|
|
||||||
|
# Safe JUnit reporting MUST be preserved
|
||||||
|
assert junit.exists()
|
||||||
|
assert 'failures="1"' in junit.read_text()
|
||||||
|
|
||||||
|
# All sensitive diagnostics MUST be purged
|
||||||
|
assert not trace.exists()
|
||||||
|
assert not har.exists()
|
||||||
|
assert not png.exists()
|
||||||
|
assert not tmp.exists()
|
||||||
|
assert not db.exists()
|
||||||
|
|
||||||
|
|
||||||
|
def test_sanitizes_trace_archive_when_disposable_ci_verified(tmp_path, monkeypatch):
|
||||||
|
"""When running inside a verified disposable CI fixture, trace archives are sanitized fail-closed."""
|
||||||
|
env = _setup_disposable_ci(tmp_path)
|
||||||
|
for k, v in env.items():
|
||||||
|
monkeypatch.setenv(k, v)
|
||||||
|
|
||||||
|
results_dir = tmp_path / "test-results"
|
||||||
|
results_dir.mkdir()
|
||||||
|
junit = results_dir / "browser-junit.xml"
|
||||||
|
junit.write_text('<testsuites><testsuite name="photo-editor" tests="137" failures="0"/></testsuites>')
|
||||||
|
|
||||||
|
trace = results_dir / "trace.zip"
|
||||||
|
with zipfile.ZipFile(trace, "w") as z:
|
||||||
|
action = {"type": "action", "name": "fill", "params": {"value": "super_secret_password"}}
|
||||||
|
z.writestr("0-trace.trace", json.dumps(action).encode() + b"\n")
|
||||||
|
|
||||||
|
png = results_dir / "failure.png"
|
||||||
|
png.write_bytes(b"\x89PNG\r\n\x1a\nvalid screenshot payload")
|
||||||
|
|
||||||
|
ok = verify_and_sanitize_artifacts(str(results_dir))
|
||||||
|
assert ok is True
|
||||||
|
|
||||||
|
assert junit.exists()
|
||||||
|
assert trace.exists()
|
||||||
|
assert png.exists()
|
||||||
|
|
||||||
|
with zipfile.ZipFile(trace, "r") as z:
|
||||||
|
content = json.loads(z.read("0-trace.trace").decode().strip())
|
||||||
|
assert content["params"]["value"] == "[REDACTED]"
|
||||||
|
|
||||||
|
|
||||||
|
def test_malformed_traces_are_removed(tmp_path, monkeypatch):
|
||||||
|
"""Negative control: Malformed trace archive is purged fail-closed without affecting JUnit XML."""
|
||||||
|
env = _setup_disposable_ci(tmp_path)
|
||||||
|
for k, v in env.items():
|
||||||
|
monkeypatch.setenv(k, v)
|
||||||
|
|
||||||
|
results_dir = tmp_path / "test-results"
|
||||||
|
results_dir.mkdir()
|
||||||
|
junit = results_dir / "browser-junit.xml"
|
||||||
|
junit.write_text('<testsuites><testsuite name="photo-editor" tests="137" failures="1"/></testsuites>')
|
||||||
|
|
||||||
|
malformed_trace = results_dir / "trace.zip"
|
||||||
|
with zipfile.ZipFile(malformed_trace, "w") as z:
|
||||||
|
z.writestr("0-trace.trace", b'{"type": "action"}\nmalformed not json line\n')
|
||||||
|
|
||||||
|
ok = verify_and_sanitize_artifacts(str(results_dir))
|
||||||
|
assert ok is True
|
||||||
|
|
||||||
|
# JUnit XML is preserved
|
||||||
|
assert junit.exists()
|
||||||
|
# Malformed archive is purged!
|
||||||
|
assert not malformed_trace.exists()
|
||||||
|
|
||||||
|
|
||||||
|
def test_fixture_marker_in_target_dir_establishes_ownership(tmp_path, monkeypatch):
|
||||||
|
"""Fixture proof in target_dir establishes fixture ownership and is purged before upload."""
|
||||||
|
data_dir = tmp_path / "odysseus-photo-editor-e2e-data"
|
||||||
|
data_dir.mkdir(parents=True, exist_ok=True)
|
||||||
|
db_path = data_dir / "app.db"
|
||||||
|
db_path.write_bytes(b"SQLite format 3\x00")
|
||||||
|
|
||||||
|
monkeypatch.setenv("CI", "true")
|
||||||
|
monkeypatch.setenv("GITHUB_ACTIONS", "true")
|
||||||
|
monkeypatch.setenv("ODYSSEUS_E2E_DISPOSABLE_ARTIFACTS", "1")
|
||||||
|
monkeypatch.setenv("PHOTO_EDITOR_E2E_AUTH", "1")
|
||||||
|
monkeypatch.delenv("PHOTO_EDITOR_E2E_DATA_DIR", raising=False)
|
||||||
|
|
||||||
|
results_dir = tmp_path / "test-results"
|
||||||
|
results_dir.mkdir()
|
||||||
|
junit = results_dir / "browser-junit.xml"
|
||||||
|
junit.write_text('<testsuites><testsuite name="photo-editor" tests="137" failures="0"/></testsuites>')
|
||||||
|
|
||||||
|
marker_file = results_dir / ".disposable-ci-fixture.json"
|
||||||
|
marker_file.write_text(json.dumps({
|
||||||
|
"synthetic": True,
|
||||||
|
"authenticated": True,
|
||||||
|
"database": str(db_path),
|
||||||
|
}))
|
||||||
|
|
||||||
|
trace = results_dir / "trace.zip"
|
||||||
|
with zipfile.ZipFile(trace, "w") as z:
|
||||||
|
action = {"type": "action", "name": "fill", "params": {"value": "sensitive_val"}}
|
||||||
|
z.writestr("0-trace.trace", json.dumps(action).encode() + b"\n")
|
||||||
|
|
||||||
|
ok = verify_and_sanitize_artifacts(str(results_dir))
|
||||||
|
assert ok is True
|
||||||
|
|
||||||
|
assert junit.exists()
|
||||||
|
assert trace.exists()
|
||||||
|
# Marker file itself must be purged so it is never published as an artifact
|
||||||
|
assert not marker_file.exists()
|
||||||
|
|
||||||
|
|
||||||
|
def test_unhandled_exception_fails_closed_purges_all_non_xml(tmp_path, monkeypatch):
|
||||||
|
"""Unexpected exception during verification triggers emergency fail-closed purge of non-XML files."""
|
||||||
|
env = _setup_disposable_ci(tmp_path)
|
||||||
|
for k, v in env.items():
|
||||||
|
monkeypatch.setenv(k, v)
|
||||||
|
|
||||||
|
results_dir = tmp_path / "test-results"
|
||||||
|
results_dir.mkdir()
|
||||||
|
junit = results_dir / "browser-junit.xml"
|
||||||
|
junit.write_text('<testsuites><testsuite name="photo-editor" tests="137" failures="0"/></testsuites>')
|
||||||
|
|
||||||
|
trace = results_dir / "trace.zip"
|
||||||
|
trace.write_bytes(b"PK0304 trace")
|
||||||
|
|
||||||
|
def broken_sanitize(zp):
|
||||||
|
raise RuntimeError("Simulated unexpected crash in sanitizer")
|
||||||
|
|
||||||
|
monkeypatch.setattr(verify_module, "sanitize_trace_archive", broken_sanitize)
|
||||||
|
|
||||||
|
ok = verify_and_sanitize_artifacts(str(results_dir))
|
||||||
|
assert ok is False
|
||||||
|
# Safe JUnit reporting is preserved
|
||||||
|
assert junit.exists()
|
||||||
|
# Sensitive non-XML file is purged fail-closed!
|
||||||
|
assert not trace.exists()
|
||||||
|
|
||||||
|
|
||||||
|
def test_cli_invocation_returns_cleanly(tmp_path):
|
||||||
|
"""Running the script as a CLI entrypoint succeeds with exit code 0."""
|
||||||
|
results_dir = tmp_path / "test-results"
|
||||||
|
results_dir.mkdir()
|
||||||
|
junit = results_dir / "junit.xml"
|
||||||
|
junit.write_text('<testsuites><testsuite name="native-node" tests="43" failures="0"/></testsuites>')
|
||||||
|
|
||||||
|
res = subprocess.run(
|
||||||
|
[sys.executable, str(VERIFY_SCRIPT), str(results_dir)],
|
||||||
|
capture_output=True,
|
||||||
|
text=True,
|
||||||
|
)
|
||||||
|
assert res.returncode == 0
|
||||||
|
assert junit.exists()
|
||||||
Reference in New Issue
Block a user