LaySite 1.5.4 · MCP 支持 raw_html 草稿编辑 #24
@@ -16,6 +16,14 @@ Use the configured LaySite MCP connection to the target site's `/mcp` endpoint.
|
|||||||
|
|
||||||
Treat a version conflict as a signal to reread the draft and reconcile any intervening changes. Leave published pages, shared headers and footers, and site settings under human control. For a published page revision, create a separate draft when the user wants a reviewable version.
|
Treat a version conflict as a signal to reread the draft and reconcile any intervening changes. Leave published pages, shared headers and footers, and site settings under human control. For a published page revision, create a separate draft when the user wants a reviewable version.
|
||||||
|
|
||||||
|
## Edit draft source
|
||||||
|
|
||||||
|
Use the `raw_html` returned by `get_page` when a task calls for HTML or page-level CSS. Pass only the source fragments that need to change into `create_draft` or `save_draft`.
|
||||||
|
|
||||||
|
- Set `raw_html.hero` to an HTML fragment to build a custom hero or to `null` to restore the template hero. A string passed directly as `raw_html` is shorthand for a hero fragment.
|
||||||
|
- Set `raw_html.sections` to a map from section keys to new body HTML. Use this for source edits that preserve section order, headings and dynamic feed settings.
|
||||||
|
- Set `raw_html.head` to the complete page Head after merging the desired change into the current source. Scope selectors to the region being changed. Existing scripts can be carried over verbatim. The visual editor provides script management for administrators.
|
||||||
|
|
||||||
## Keep page design consistent
|
## Keep page design consistent
|
||||||
|
|
||||||
- Follow the existing site's palette, spacing, button styles, navigation and terminology. Start with a matching template or reusable component. Use `header_id`, `footer_id`, and `palette_id` only when the task calls for a specific saved design.
|
- Follow the existing site's palette, spacing, button styles, navigation and terminology. Start with a matching template or reusable component. Use `header_id`, `footer_id`, and `palette_id` only when the task calls for a specific saved design.
|
||||||
|
|||||||
@@ -116,13 +116,31 @@ php start.php start
|
|||||||
|
|
||||||
| 工具 | 功能 |
|
| 工具 | 功能 |
|
||||||
| --- | --- |
|
| --- | --- |
|
||||||
| `list_pages` / `get_page` | 读取页面、区块及当前修订号 |
|
| `list_pages` / `get_page` | 读取页面、区块、`raw_html` 源码及当前修订号 |
|
||||||
| `list_templates` / `list_designs` | 查阅整页模板、组件、页头、页脚和配色 |
|
| `list_templates` / `list_designs` | 查阅整页模板、组件、页头、页脚和配色 |
|
||||||
| `create_draft` | 按模板或传入内容创建草稿,返回编辑地址 |
|
| `create_draft` | 按模板、结构化内容或 `raw_html` 创建草稿,返回编辑地址 |
|
||||||
| `save_draft` | 用最新修订号更新草稿。传入 `sections` 时替换区块列表 |
|
| `save_draft` | 用最新修订号更新草稿。`sections` 替换区块列表,`raw_html.sections` 更新指定区块的正文 |
|
||||||
|
|
||||||
Agent 可以编辑草稿,管理员在画布里检查、细改及发布。草稿 HTML 使用与人工画布相同的 HTML 过滤器。按钮参数为 `[href, label, style, opensNewTab]`,样式可选 `solid`、`outline`、`underline`、`plain`。MCP 与站点发布共用 SQLite 和编辑器修订号。
|
Agent 可以编辑草稿,管理员在画布里检查、细改及发布。草稿 HTML 使用与人工画布相同的 HTML 过滤器。按钮参数为 `[href, label, style, opensNewTab]`,样式可选 `solid`、`outline`、`underline`、`plain`。MCP 与站点发布共用 SQLite 和编辑器修订号。
|
||||||
|
|
||||||
|
`raw_html` 可以写成首屏 HTML 字符串,也可以用对象按范围修改源码。省略的范围保留当前内容。
|
||||||
|
|
||||||
|
- `hero` 填写自定义首屏 HTML。传入 `null` 会恢复模板首屏。首屏的显示状态由 `visibility.hero` 控制。
|
||||||
|
- `head` 替换本页 Head。可使用 `meta`、样式表 `link` 和 `style`,也可原样保留本页已有的脚本。添加或修改脚本使用后台的「本页 Head」。
|
||||||
|
- `sections` 以区块锚点为键,值为该区块的新正文 HTML。更新保留标题、顺序、样式及动态来源。若同一次调用也传入顶层 `sections`,则先构建新的区块列表,再应用源码修改。
|
||||||
|
|
||||||
|
例如,`save_draft` 可以只更新产品页的首屏样式,保留已有首屏内容。请使用 `get_page` 返回的最新 `version`。如本页已有 Head 代码,先读取 `raw_html.head`,在保留所需内容的基础上编辑。
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"slug": "product",
|
||||||
|
"version": 0,
|
||||||
|
"raw_html": {
|
||||||
|
"head": "<style>#main-content .hero-product { background: radial-gradient(ellipse at 77% 28%, #c9dafa 0, transparent 45%), var(--paper); }</style>"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
仓库自带 [`laysite-mcp` Agent Skill](.agents/skills/laysite-mcp/SKILL.md),涵盖模板复用、设计规范、草稿修订和人工交接。使用支持 `.agents/skills/` 的客户端时,可在项目中调用该 Skill。其他支持 Agent Skills 的客户端可将整个 `laysite-mcp` 目录导入其技能目录。
|
仓库自带 [`laysite-mcp` Agent Skill](.agents/skills/laysite-mcp/SKILL.md),涵盖模板复用、设计规范、草稿修订和人工交接。使用支持 `.agents/skills/` 的客户端时,可在项目中调用该 Skill。其他支持 Agent Skills 的客户端可将整个 `laysite-mcp` 目录导入其技能目录。
|
||||||
|
|
||||||
## 站点备份与更新
|
## 站点备份与更新
|
||||||
|
|||||||
@@ -77,9 +77,62 @@ final class PageDraft
|
|||||||
$page['quicklinks'] = array_map(static fn (array $section): array => ['#' . $section['key'], $section['title']], array_slice($page['sections'], 0, 20));
|
$page['quicklinks'] = array_map(static fn (array $section): array => ['#' . $section['key'], $section['title']], array_slice($page['sections'], 0, 20));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return array_key_exists('raw_html', $input) ? self::applyRawHtml($page, $input['raw_html']) : $page;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function rawHtml(array $page): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'hero' => $page['presentation']['heroHtml'] ?? null,
|
||||||
|
'head' => $page['head_html'] ?? '',
|
||||||
|
'sections' => (object) array_column($page['sections'], 'body', 'key'),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
private static function applyRawHtml(array $page, mixed $input): array
|
||||||
|
{
|
||||||
|
if (is_string($input)) {
|
||||||
|
$input = ['hero' => $input];
|
||||||
|
}
|
||||||
|
if (!is_array($input) || array_diff(array_keys($input), ['hero', 'head', 'sections'])) {
|
||||||
|
throw new RuntimeException('raw_html 须为首屏 HTML 字符串或包含 hero、head、sections 的对象。');
|
||||||
|
}
|
||||||
|
if (array_key_exists('hero', $input)) {
|
||||||
|
if ($input['hero'] === null) {
|
||||||
|
unset($page['presentation']['heroHtml']);
|
||||||
|
} else {
|
||||||
|
$page['presentation']['heroHtml'] = self::sanitizeRawHtml($input['hero']);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (array_key_exists('head', $input)) {
|
||||||
|
$page['head_html'] = PageHead::normalize($input['head'], restrictScriptsTo: $page['head_html'] ?? '');
|
||||||
|
}
|
||||||
|
if (array_key_exists('sections', $input)) {
|
||||||
|
$patches = $input['sections'];
|
||||||
|
if (!is_array($patches) || count($patches) > 50) {
|
||||||
|
throw new RuntimeException('raw_html.sections 须为区块锚点与 HTML 的映射,最多 50 项。');
|
||||||
|
}
|
||||||
|
$positions = array_flip(array_column($page['sections'], 'key'));
|
||||||
|
foreach ($patches as $key => $html) {
|
||||||
|
if (!isset($positions[$key])) {
|
||||||
|
throw new RuntimeException('raw_html.sections 中的区块不存在。');
|
||||||
|
}
|
||||||
|
$page['sections'][$positions[$key]]['body'] = self::sanitizeRawHtml($html);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return $page;
|
return $page;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static function sanitizeRawHtml(mixed $value): string
|
||||||
|
{
|
||||||
|
if (!is_string($value) || strlen($value) > 300000 || !mb_check_encoding($value, 'UTF-8') || str_contains($value, "\0")) {
|
||||||
|
throw new RuntimeException('HTML 片段须为 UTF-8,最多 300 KB。');
|
||||||
|
}
|
||||||
|
|
||||||
|
return Html::sanitize($value);
|
||||||
|
}
|
||||||
|
|
||||||
private static function sections(mixed $items): array
|
private static function sections(mixed $items): array
|
||||||
{
|
{
|
||||||
if (!is_array($items) || !array_is_list($items) || count($items) > 50) {
|
if (!is_array($items) || !array_is_list($items) || count($items) > 50) {
|
||||||
|
|||||||
@@ -18,13 +18,26 @@ final class PageHead
|
|||||||
'script' => ['src', 'type', 'async', 'defer', 'integrity', 'crossorigin', 'referrerpolicy'],
|
'script' => ['src', 'type', 'async', 'defer', 'integrity', 'crossorigin', 'referrerpolicy'],
|
||||||
];
|
];
|
||||||
|
|
||||||
public static function normalize(mixed $value): string
|
public static function normalize(mixed $value, ?string $restrictScriptsTo = null): string
|
||||||
{
|
{
|
||||||
if (!is_string($value) || strlen($value) > self::MAX_LENGTH || !mb_check_encoding($value, 'UTF-8') || str_contains($value, "\0")) {
|
if (!is_string($value) || strlen($value) > self::MAX_LENGTH || !mb_check_encoding($value, 'UTF-8') || str_contains($value, "\0")) {
|
||||||
throw new RuntimeException('Head 代码须使用 UTF-8,最多 32 KB。');
|
throw new RuntimeException('Head 代码须使用 UTF-8,最多 32 KB。');
|
||||||
}
|
}
|
||||||
$value = trim($value);
|
$value = trim($value);
|
||||||
self::parse($value);
|
$entries = self::parse($value);
|
||||||
|
if ($restrictScriptsTo !== null) {
|
||||||
|
$existing = self::parse($restrictScriptsTo);
|
||||||
|
foreach ($entries as $entry) {
|
||||||
|
if ($entry[0] !== 'script') {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
$index = array_search($entry, $existing, true);
|
||||||
|
if ($index === false) {
|
||||||
|
throw new RuntimeException('MCP 只能保留本页已有的脚本。请在管理后台添加或修改脚本。');
|
||||||
|
}
|
||||||
|
unset($existing[$index]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return $value;
|
return $value;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -61,14 +61,16 @@ final class Repository
|
|||||||
public static function insertPage(PDO $pdo, string $slug, string $name, array $template = [], ?int $layoutId = null, string $type = 'page'): void
|
public static function insertPage(PDO $pdo, string $slug, string $name, array $template = [], ?int $layoutId = null, string $type = 'page'): void
|
||||||
{
|
{
|
||||||
$query = $pdo->prepare("INSERT OR IGNORE INTO pages (slug, name, title, description, heading, accent,
|
$query = $pdo->prepare("INSERT OR IGNORE INTO pages (slug, name, title, description, heading, accent,
|
||||||
lead, image, alt, actions, quicklinks, theme, visibility, header_id, footer_id, palette_id, layout_id, page_type, status, updated_at)
|
lead, image, alt, actions, quicklinks, theme, visibility, header_id, footer_id, palette_id, layout_id, page_type, status, updated_at,
|
||||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
|
presentation, head_html)
|
||||||
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
|
||||||
$query->execute([
|
$query->execute([
|
||||||
$slug, $name, $template['title'] ?? $name, $template['description'] ?? '', $template['heading'] ?? $name,
|
$slug, $name, $template['title'] ?? $name, $template['description'] ?? '', $template['heading'] ?? $name,
|
||||||
$template['accent'] ?? '', $template['lead'] ?? '', $template['image'] ?? '', $template['alt'] ?? '',
|
$template['accent'] ?? '', $template['lead'] ?? '', $template['image'] ?? '', $template['alt'] ?? '',
|
||||||
Database::json($template['actions'] ?? []), Database::json($template['quicklinks'] ?? []),
|
Database::json($template['actions'] ?? []), Database::json($template['quicklinks'] ?? []),
|
||||||
$template['theme'] ?? '', Database::json($template['visibility'] ?? []), $template['header_id'] ?? null,
|
$template['theme'] ?? '', Database::json($template['visibility'] ?? []), $template['header_id'] ?? null,
|
||||||
$template['footer_id'] ?? null, $template['palette_id'] ?? null, $layoutId, $type, $layoutId === null ? 'draft' : 'published', self::now(),
|
$template['footer_id'] ?? null, $template['palette_id'] ?? null, $layoutId, $type, $layoutId === null ? 'draft' : 'published', self::now(),
|
||||||
|
CanvasStyle::encode($template['presentation'] ?? []), $template['head_html'] ?? '',
|
||||||
]);
|
]);
|
||||||
if ($query->rowCount() !== 1) {
|
if ($query->rowCount() !== 1) {
|
||||||
throw new RuntimeException('此页面地址已存在。');
|
throw new RuntimeException('此页面地址已存在。');
|
||||||
|
|||||||
@@ -95,6 +95,15 @@ final class McpController
|
|||||||
$optional = [
|
$optional = [
|
||||||
'name' => $string, 'title' => $string, 'heading' => $string, 'description' => $string, 'lead' => $string,
|
'name' => $string, 'title' => $string, 'heading' => $string, 'description' => $string, 'lead' => $string,
|
||||||
'accent' => $string, 'image' => $string, 'alt' => $string,
|
'accent' => $string, 'image' => $string, 'alt' => $string,
|
||||||
|
'raw_html' => ['oneOf' => [
|
||||||
|
$string + ['description' => 'Replace the custom hero HTML.'],
|
||||||
|
$object([
|
||||||
|
'hero' => ['type' => ['string', 'null'], 'description' => 'Custom hero fragment. Null restores the template hero.'],
|
||||||
|
'head' => $string + ['description' => 'Page Head, including style tags. Only existing scripts may be retained.'],
|
||||||
|
'sections' => ['type' => 'object', 'maxProperties' => 50, 'additionalProperties' => $string,
|
||||||
|
'description' => 'Section keys mapped to replacement body HTML. Preserves other section data.'],
|
||||||
|
]),
|
||||||
|
]],
|
||||||
'header_id' => ['type' => ['integer', 'null']], 'footer_id' => ['type' => ['integer', 'null']], 'palette_id' => ['type' => ['integer', 'null']],
|
'header_id' => ['type' => ['integer', 'null']], 'footer_id' => ['type' => ['integer', 'null']], 'palette_id' => ['type' => ['integer', 'null']],
|
||||||
'visibility' => $object(['hero' => ['type' => 'boolean'], 'header' => ['type' => 'boolean'], 'footer' => ['type' => 'boolean']]),
|
'visibility' => $object(['hero' => ['type' => 'boolean'], 'header' => ['type' => 'boolean'], 'footer' => ['type' => 'boolean']]),
|
||||||
'actions' => ['type' => 'array', 'maxItems' => 8, 'items' => ['type' => 'array', 'description' => '[URL, label, style, boolean opensNewTab]. Style: solid, outline, underline, plain.']],
|
'actions' => ['type' => 'array', 'maxItems' => 8, 'items' => ['type' => 'array', 'description' => '[URL, label, style, boolean opensNewTab]. Style: solid, outline, underline, plain.']],
|
||||||
@@ -105,7 +114,7 @@ final class McpController
|
|||||||
];
|
];
|
||||||
$definitions = [
|
$definitions = [
|
||||||
'list_pages' => ['List pages and their current revisions.', $object([])],
|
'list_pages' => ['List pages and their current revisions.', $object([])],
|
||||||
'get_page' => ['Read a page and its editable sections.', $object(['slug' => $string], ['slug'])],
|
'get_page' => ['Read a page, editable sections and raw_html.', $object(['slug' => $string], ['slug'])],
|
||||||
'list_templates' => ['List whole-page templates and reusable components.', $object([])],
|
'list_templates' => ['List whole-page templates and reusable components.', $object([])],
|
||||||
'list_designs' => ['List saved header, footer and palette IDs. Authentication settings are private.', $object([])],
|
'list_designs' => ['List saved header, footer and palette IDs. Authentication settings are private.', $object([])],
|
||||||
'create_draft' => ['Create a draft from content or a template. Returns the editor URL.', $object(['slug' => $string, 'template' => $string] + $optional, ['slug', 'name'])],
|
'create_draft' => ['Create a draft from content or a template. Returns the editor URL.', $object(['slug' => $string, 'template' => $string] + $optional, ['slug', 'name'])],
|
||||||
@@ -131,7 +140,7 @@ final class McpController
|
|||||||
try {
|
try {
|
||||||
$data = match ($name) {
|
$data = match ($name) {
|
||||||
'list_pages' => $repo->pages(),
|
'list_pages' => $repo->pages(),
|
||||||
'get_page' => $this->read($repo, PageDraft::slug($args['slug'] ?? null, [], includeSpecial: true)),
|
'get_page' => $this->readSource($repo, PageDraft::slug($args['slug'] ?? null, [], includeSpecial: true)),
|
||||||
'list_templates' => $this->templates(),
|
'list_templates' => $this->templates(),
|
||||||
'list_designs' => DesignLibrary::all(),
|
'list_designs' => DesignLibrary::all(),
|
||||||
'create_draft' => $this->create($repo, $args),
|
'create_draft' => $this->create($repo, $args),
|
||||||
@@ -171,6 +180,13 @@ final class McpController
|
|||||||
return $this->summary($repo, $slug);
|
return $this->summary($repo, $slug);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function readSource(Repository $repo, string $slug): array
|
||||||
|
{
|
||||||
|
$page = $this->read($repo, $slug);
|
||||||
|
|
||||||
|
return $page + ['raw_html' => PageDraft::rawHtml($page)];
|
||||||
|
}
|
||||||
|
|
||||||
private function save(Repository $repo, array $args): array
|
private function save(Repository $repo, array $args): array
|
||||||
{
|
{
|
||||||
$slug = PageDraft::slug($args['slug'] ?? null);
|
$slug = PageDraft::slug($args['slug'] ?? null);
|
||||||
@@ -181,7 +197,11 @@ final class McpController
|
|||||||
}
|
}
|
||||||
$page = PageDraft::content($args, $page);
|
$page = PageDraft::content($args, $page);
|
||||||
$replace = array_key_exists('sections', $args);
|
$replace = array_key_exists('sections', $args);
|
||||||
$repo->savePage($slug, $page, $version, $replace ? $page['sections'] : [], replaceDraftSections: $replace);
|
$raw = $args['raw_html'] ?? null;
|
||||||
|
$patches = is_array($raw) ? ($raw['sections'] ?? []) : [];
|
||||||
|
$sections = $replace ? $page['sections'] : array_filter($page['sections'],
|
||||||
|
static fn (array $section): bool => array_key_exists($section['key'], $patches));
|
||||||
|
$repo->savePage($slug, $page, $version, $sections, replaceDraftSections: $replace);
|
||||||
|
|
||||||
return $this->summary($repo, $slug);
|
return $this->summary($repo, $slug);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user