feat(web): Document/Endpoint/Resource artifact rewrite (ADR 0007)

Replace the monolithic compose_*/configure author surface with file-level
web|endpoint artifacts, split ext/{web,data,security,net}, and lock
no-degrade rules so the model cannot regress to God-object APIs.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
cflmyandCursor committed 2026-09-28 11:06:38 +08:00
1 parent 33f8b94bb0
commit 67bac1bf02
54 files changed
+4642 -2505

No files matched your search

+224
View File
@@ -0,0 +1,224 @@
package main
/*
#cgo CFLAGS: -I${SRCDIR}/abi
#include "marqdo_abi_cgo.h"
extern int web_render_nodes(char *args_json, char **out_json, char **err_msg);
extern int web_render_document(char *args_json, char **out_json, char **err_msg);
extern int web_dom_markdown(char *args_json, char **out_json, char **err_msg);
extern int web_serve_root(char *args_json, char **out_json, char **err_msg);
extern int web_inspect(char *args_json, char **out_json, char **err_msg);
extern int web_app_use(char *args_json, char **out_json, char **err_msg);
extern int web_route_use(char *args_json, char **out_json, char **err_msg);
*/
import "C"
import (
"fmt"
"os"
"path/filepath"
"strconv"
"github.com/marqdo/marqdo/plugins/web/internal/app"
"github.com/marqdo/marqdo/plugins/web/internal/artifact"
"github.com/marqdo/marqdo/plugins/web/internal/httpx"
"github.com/marqdo/marqdo/plugins/web/internal/middleware"
"github.com/marqdo/marqdo/plugins/web/internal/page"
)
//export web_render_nodes
func web_render_nodes(argsJSON *C.char, outJSON **C.char, errMsg **C.char) C.int {
args, err := parseArgs(argsJSON)
if err != nil {
return replyJSON(outJSON, errMsg, nil, err)
}
frag, err := artifact.RenderNodes(args["nodes"])
if err != nil {
return replyJSON(outJSON, errMsg, nil, err)
}
pg := asPageMap(args["page"])
title := ""
if pg != nil {
if t, ok := pg["title"].(string); ok {
title = t
}
}
if t, ok := argStr(args, "title"); ok && t != "" {
title = t
}
return replyJSON(outJSON, errMsg, artifact.RenderHTMLPage(title, frag), nil)
}
//export web_render_document
func web_render_document(argsJSON *C.char, outJSON **C.char, errMsg **C.char) C.int {
args, err := parseArgs(argsJSON)
if err != nil {
return replyJSON(outJSON, errMsg, nil, err)
}
pg := asPageMap(args["page"])
title := ""
body := ""
if pg != nil {
if t, ok := pg["title"].(string); ok {
title = t
}
if b, ok := pg["body"].(string); ok {
body = b
}
}
if b, ok := argStr(args, "body"); ok && b != "" {
body = b
}
if t, ok := argStr(args, "title"); ok && t != "" {
title = t
}
return replyJSON(outJSON, errMsg, artifact.RenderDocument(title, body), nil)
}
//export web_dom_markdown
func web_dom_markdown(argsJSON *C.char, outJSON **C.char, errMsg **C.char) C.int {
args, err := parseArgs(argsJSON)
if err != nil {
return replyJSON(outJSON, errMsg, nil, err)
}
text, _ := argStr(args, "text")
return replyJSON(outJSON, errMsg, artifact.MarkdownHTML(text), nil)
}
//export web_serve_root
func web_serve_root(argsJSON *C.char, outJSON **C.char, errMsg **C.char) C.int {
args, err := parseArgs(argsJSON)
if err != nil {
return replyJSON(outJSON, errMsg, nil, err)
}
root, _ := argStr(args, "root")
if root == "" {
root = "."
}
abs, err := filepath.Abs(root)
if err != nil {
return replyJSON(outJSON, errMsg, nil, err)
}
if st, err := os.Stat(abs); err != nil || !st.IsDir() {
return replyJSON(outJSON, errMsg, nil, fmt.Errorf("web.serve: root not a directory: %s", abs))
}
files, err := artifact.ScanRoot(abs)
if err != nil {
return replyJSON(outJSON, errMsg, nil, err)
}
host, _ := argStr(args, "host")
if host == "" {
host = "127.0.0.1"
}
port := float64(18081)
if v, ok := args["port"]; ok && v != nil {
switch t := v.(type) {
case float64:
port = t
case string:
if n, e := strconv.Atoi(t); e == nil {
port = float64(n)
}
}
}
homeArgs := map[string]any{"title": "Marqdo"}
for _, f := range files {
if f.Kind == "web" && (f.Route == "/" || f.Rel == "index.mq.md") {
title := f.Meta["title"]
if title == "" {
title = f.Meta["标题"]
}
if title == "" {
title = "Marqdo"
}
homeArgs = map[string]any{
"title": title,
"intro": artifact.MarkdownHTML(f.Body),
}
break
}
}
home := page.New(homeArgs)
a := app.New(map[string]any{
"page": home,
"db": args["db"],
"host": host,
"port": port,
})
for _, f := range files {
if f.Kind != "web" || f.Route == "/" {
continue
}
title := f.Meta["title"]
if title == "" {
title = f.Meta["标题"]
}
pg := page.New(map[string]any{
"title": title,
"intro": artifact.MarkdownHTML(f.Body),
})
out, err := app.Route(a, f.Route, pg)
if err != nil {
return replyJSON(outJSON, errMsg, nil, err)
}
a = out
}
if mw := args["middleware"]; mw != nil {
out, err := middleware.Configure(a, map[string]any{"security": mw})
if err != nil {
return replyJSON(outJSON, errMsg, nil, err)
}
a = out
}
if sd, ok := argStr(args, "static_dir"); ok && sd != "" {
out, err := app.Static(a, sd, "/static")
if err != nil {
return replyJSON(outJSON, errMsg, nil, err)
}
a = out
}
a["inspect"] = artifact.InspectBag(a, files)
_, err = httpx.Listen(a, abs)
return replyJSON(outJSON, errMsg, map[string]any{"ok": true, "value": a["inspect"], "error": nil}, err)
}
//export web_inspect
func web_inspect(argsJSON *C.char, outJSON **C.char, errMsg **C.char) C.int {
args, err := parseArgs(argsJSON)
if err != nil {
return replyJSON(outJSON, errMsg, nil, err)
}
a := asPageMap(args["app"])
if a != nil {
if insp, ok := a["inspect"].(map[string]any); ok {
return replyJSON(outJSON, errMsg, insp, nil)
}
}
return replyJSON(outJSON, errMsg, artifact.InspectBag(a, nil), nil)
}
//export web_app_use
func web_app_use(argsJSON *C.char, outJSON **C.char, errMsg **C.char) C.int {
args, err := parseArgs(argsJSON)
if err != nil {
return replyJSON(outJSON, errMsg, nil, err)
}
a := asPageMap(args["app"])
out, err := middleware.Configure(a, map[string]any{"security": args["middleware"]})
return replyJSON(outJSON, errMsg, out, err)
}
//export web_route_use
func web_route_use(argsJSON *C.char, outJSON **C.char, errMsg **C.char) C.int {
args, err := parseArgs(argsJSON)
if err != nil {
return replyJSON(outJSON, errMsg, nil, err)
}
rt := asPageMap(args["route"])
if rt == nil {
rt = map[string]any{}
}
rt["middleware"] = args["middleware"]
return replyJSON(outJSON, errMsg, rt, nil)
}
+287
View File
@@ -0,0 +1,287 @@
// Package artifact implements Document/Endpoint View render, serve scan, and inspect (ADR 0007).
package artifact
import (
"fmt"
"html"
"os"
"path/filepath"
"strings"
"github.com/marqdo/marqdo/plugins/web/internal/render"
"github.com/marqdo/marqdo/plugins/web/internal/table"
)
// MetaType returns canonical kind: web | endpoint | "" from flat frontmatter map.
func MetaType(meta map[string]string) string {
if v := strings.TrimSpace(meta["type"]); v != "" {
return normalizeType(v)
}
if v := strings.TrimSpace(meta["类型"]); v != "" {
return normalizeType(v)
}
return ""
}
func normalizeType(v string) string {
switch v {
case "web", "网页":
return "web"
case "endpoint", "端点":
return "endpoint"
default:
return v
}
}
// ParseFrontmatter reads flat key: value pairs from a .mq.md file head.
func ParseFrontmatter(src string) (meta map[string]string, body string) {
meta = map[string]string{}
s := strings.TrimPrefix(src, "\ufeff")
if !strings.HasPrefix(s, "---\n") && !strings.HasPrefix(s, "---\r\n") {
return meta, src
}
rest := s[4:]
if strings.HasPrefix(rest, "\r\n") {
rest = rest[2:]
} else if strings.HasPrefix(rest, "\n") {
rest = rest[1:]
}
end := strings.Index(rest, "\n---\n")
if end < 0 {
end = strings.Index(rest, "\r\n---\r\n")
}
if end < 0 {
end = strings.Index(rest, "\n---\r\n")
}
if end < 0 {
return meta, src
}
fm := rest[:end]
body = rest[end:]
if i := strings.Index(body, "---"); i >= 0 {
body = body[i+3:]
body = strings.TrimPrefix(body, "\r")
body = strings.TrimPrefix(body, "\n")
}
for _, line := range strings.Split(fm, "\n") {
line = strings.TrimSpace(strings.TrimSuffix(line, "\r"))
if line == "" || strings.HasPrefix(line, "#") || strings.HasPrefix(line, "import ") || strings.HasPrefix(line, "导入 ") {
continue
}
k, v, ok := strings.Cut(line, ":")
if !ok {
continue
}
k = strings.TrimSpace(k)
v = strings.TrimSpace(v)
if len(v) >= 2 {
if (v[0] == '"' && v[len(v)-1] == '"') || (v[0] == '\'' && v[len(v)-1] == '\'') {
v = v[1 : len(v)-1]
}
}
if k != "" {
meta[k] = v
}
}
return meta, body
}
// RenderNodes turns a unified View table into an HTML fragment.
// Columns: type|slot|value|attrs|style (ZH: 类型|槽|值|属性|样式).
func RenderNodes(nodes any) (string, error) {
raw := table.AsRows(nodes)
list, _ := raw.([]any)
var b strings.Builder
b.WriteString(`<div class="mq-view">`)
for _, item := range list {
row, ok := item.(map[string]any)
if !ok {
continue
}
ty := first(row, "type", "类型")
slot := first(row, "slot", "槽")
val := first(row, "value", "值")
attrs := first(row, "attrs", "属性")
style := first(row, "style", "样式")
class := "mq-node"
if style != "" {
class += " " + html.EscapeString(style)
}
if slot != "" {
class += " slot-" + html.EscapeString(slot)
}
switch strings.ToLower(ty) {
case "title", "标题":
b.WriteString(`<h1 class="` + class + `">` + inline(val) + `</h1>`)
case "link", "链接":
href := attrs
if href == "" {
href = "#"
}
b.WriteString(`<a class="` + class + `" href="` + html.EscapeString(href) + `">` + inline(val) + `</a>`)
case "html", "原文":
b.WriteString(`<div class="` + class + `">` + val + `</div>`)
default: // text
b.WriteString(`<p class="` + class + `">` + inline(val) + `</p>`)
}
}
b.WriteString(`</div>`)
return b.String(), nil
}
func inline(s string) string {
return html.EscapeString(s)
}
func first(row map[string]any, keys ...string) string {
for _, k := range keys {
if v, ok := row[k]; ok && v != nil {
return fmt.Sprint(v)
}
}
return ""
}
// RenderDocument builds a minimal HTML page from title + markdown body.
func RenderDocument(title, body string) string {
return RenderHTMLPage(title, render.MarkdownToHTML(body))
}
// RenderHTMLPage wraps an HTML fragment in a minimal document shell.
func RenderHTMLPage(title, innerHTML string) string {
t := html.EscapeString(title)
if t == "" {
t = "Marqdo"
}
return "<!DOCTYPE html><html><head><meta charset=\"utf-8\"><title>" + t +
"</title></head><body class=\"mq-document\"><main>" + innerHTML + "</main></body></html>"
}
// MarkdownHTML exposes render helper.
func MarkdownHTML(md string) string {
return render.MarkdownToHTML(md)
}
// ScanRoot finds Artifact .mq.md files under root.
func ScanRoot(root string) ([]ArtifactFile, error) {
var out []ArtifactFile
err := filepath.WalkDir(root, func(path string, d os.DirEntry, err error) error {
if err != nil {
return err
}
name := d.Name()
if d.IsDir() {
if name == "data" || name == ".git" || name == "node_modules" || strings.HasPrefix(name, ".") {
return filepath.SkipDir
}
return nil
}
if !strings.HasSuffix(name, ".mq.md") {
return nil
}
raw, err := os.ReadFile(path)
if err != nil {
return err
}
meta, body := ParseFrontmatter(string(raw))
kind := MetaType(meta)
if kind != "web" && kind != "endpoint" {
return nil
}
rel, _ := filepath.Rel(root, path)
af := ArtifactFile{Path: path, Rel: rel, Kind: kind, Meta: meta, Body: body}
if kind == "web" {
af.Route = metaString(meta, "route", "路由")
if af.Route == "" {
af.Route = routeFromRel(rel)
}
af.Method = metaString(meta, "method", "方法")
if af.Method == "" {
af.Method = "GET"
}
} else {
af.Route = metaString(meta, "path", "路径")
af.Method = metaString(meta, "method", "方法")
if af.Method == "" {
af.Method = "GET"
}
if af.Route == "" {
return fmt.Errorf("endpoint %s: missing path/路径", rel)
}
}
out = append(out, af)
return nil
})
return out, err
}
// ArtifactFile is one discovered Document or Endpoint.
type ArtifactFile struct {
Path string
Rel string
Kind string
Route string
Method string
Meta map[string]string
Body string
}
func metaString(meta map[string]string, keys ...string) string {
for _, k := range keys {
if v := strings.TrimSpace(meta[k]); v != "" {
return v
}
}
return ""
}
func routeFromRel(rel string) string {
rel = filepath.ToSlash(rel)
if rel == "index.mq.md" {
return "/"
}
rel = strings.TrimSuffix(rel, ".mq.md")
rel = strings.TrimSuffix(rel, "/index")
if !strings.HasPrefix(rel, "/") {
rel = "/" + rel
}
return rel
}
// SystemRoutes lists runtime-owned routes for inspect.
func SystemRoutes() []map[string]any {
return []map[string]any{
{"method": "GET", "path": "/_part/*", "kind": "system"},
{"method": "GET|POST", "path": "/_form/*", "kind": "system"},
}
}
// InspectBag builds {declared, system} from an app bag and/or scanned files.
func InspectBag(app map[string]any, declared []ArtifactFile) map[string]any {
var dec []map[string]any
for _, a := range declared {
dec = append(dec, map[string]any{
"method": a.Method,
"path": a.Route,
"kind": a.Kind,
"file": a.Rel,
})
}
if app != nil {
if routes, ok := app["routes"].(map[string]any); ok {
for path := range routes {
dec = append(dec, map[string]any{"method": "GET", "path": path, "kind": "declared"})
}
}
if inv, ok := app["invoke_routes"].(map[string]any); ok {
for key := range inv {
dec = append(dec, map[string]any{"path": key, "kind": "endpoint"})
}
}
}
return map[string]any{
"declared": dec,
"system": SystemRoutes(),
}
}
@@ -0,0 +1,55 @@
package artifact
import "testing"
func TestMetaType(t *testing.T) {
if MetaType(map[string]string{"type": "web"}) != "web" {
t.Fatal("en web")
}
if MetaType(map[string]string{"类型": "网页"}) != "web" {
t.Fatal("zh web")
}
if MetaType(map[string]string{"type": "endpoint"}) != "endpoint" {
t.Fatal("endpoint")
}
}
func TestParseFrontmatter(t *testing.T) {
src := "---\ntype: web\nroute: /x\n---\n\n# Hi\n"
meta, body := ParseFrontmatter(src)
if meta["type"] != "web" || meta["route"] != "/x" {
t.Fatalf("meta=%v", meta)
}
if body == "" || body[0] != '#' {
t.Fatalf("body=%q", body)
}
}
func TestRenderNodes(t *testing.T) {
nodes := map[string]any{
"type": []any{"title", "text"},
"slot": []any{"title", "lede"},
"value": []any{"Marqdo", "Hello"},
"attrs": []any{"", ""},
"style": []any{"title", "lede"},
}
html, err := RenderNodes(nodes)
if err != nil {
t.Fatal(err)
}
if !contains(html, "Marqdo") || !contains(html, "mq-view") {
t.Fatalf("html=%s", html)
}
}
func contains(s, sub string) bool {
return len(s) >= len(sub) && (s == sub || len(sub) == 0 ||
(func() bool {
for i := 0; i+len(sub) <= len(s); i++ {
if s[i:i+len(sub)] == sub {
return true
}
}
return false
})())
}
+14
View File
@@ -158,6 +158,13 @@ extern int web_cache_ttl(char *args_json, char **out_json, char **err_msg);
extern int web_app_proxy(char *args_json, char **out_json, char **err_msg);
extern int web_app_invoke(char *args_json, char **out_json, char **err_msg);
extern int web_api_key_check(char *args_json, char **out_json, char **err_msg);
extern int web_render_nodes(char *args_json, char **out_json, char **err_msg);
extern int web_render_document(char *args_json, char **out_json, char **err_msg);
extern int web_dom_markdown(char *args_json, char **out_json, char **err_msg);
extern int web_serve_root(char *args_json, char **out_json, char **err_msg);
extern int web_inspect(char *args_json, char **out_json, char **err_msg);
extern int web_app_use(char *args_json, char **out_json, char **err_msg);
extern int web_route_use(char *args_json, char **out_json, char **err_msg);
static int register_core(void) {
if (host_register((char *)"web_go_ready", (char *)"", web_go_ready) != 0) return 1;
@@ -269,6 +276,13 @@ static int register_core(void) {
if (host_register((char *)"web_app_proxy", (char *)"app,path,upstream,stream,strip_prefix,methods,headers_from_env,timeout_ms", web_app_proxy) != 0) return 1;
if (host_register((char *)"web_app_invoke", (char *)"app,path,method,fn,body,return", web_app_invoke) != 0) return 1;
if (host_register((char *)"web_api_key_check", (char *)"key,authorization,pepper,keys", web_api_key_check) != 0) return 1;
if (host_register((char *)"web_render_nodes", (char *)"page,nodes,db,title", web_render_nodes) != 0) return 1;
if (host_register((char *)"web_render_document", (char *)"page,body,db,title", web_render_document) != 0) return 1;
if (host_register((char *)"web_dom_markdown", (char *)"text", web_dom_markdown) != 0) return 1;
if (host_register((char *)"web_serve_root", (char *)"root,host,port,db,static_dir,middleware", web_serve_root) != 0) return 1;
if (host_register((char *)"web_inspect", (char *)"app", web_inspect) != 0) return 1;
if (host_register((char *)"web_app_use", (char *)"app,middleware", web_app_use) != 0) return 1;
if (host_register((char *)"web_route_use", (char *)"route,middleware", web_route_use) != 0) return 1;
return 0;
}
*/