Compare commits
No commits in common. "243711e717d2554c90e3a689393a61180052303b" and "3064ef1b4058faed61cfc5038b628ac77b2906ef" have entirely different histories.
243711e717
...
3064ef1b40
39
block.go
39
block.go
@ -2,41 +2,38 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"html"
|
"html"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
type smartBlockGeneration struct {
|
|
||||||
ID string `json:"id"`
|
|
||||||
Template string `json:"template"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type smartBlockMeta struct {
|
|
||||||
Generations []smartBlockGeneration `json:"generations"`
|
|
||||||
CurrentGenerationID string `json:"currentGenerationId"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// SmartBlockFunc renders a Smart Block by substituting {{field}} placeholders with content values.
|
// SmartBlockFunc renders a Smart Block by substituting {{field}} placeholders with content values.
|
||||||
func SmartBlockFunc(ctx context.Context, content map[string]any) string {
|
func SmartBlockFunc(ctx context.Context, content map[string]any) string {
|
||||||
rawMeta, err := json.Marshal(content["_meta"])
|
// Get metadata
|
||||||
if err != nil {
|
meta, ok := content["_meta"].(map[string]any)
|
||||||
return renderEmptyState()
|
if !ok {
|
||||||
}
|
|
||||||
var meta smartBlockMeta
|
|
||||||
if err := json.Unmarshal(rawMeta, &meta); err != nil {
|
|
||||||
return renderEmptyState()
|
return renderEmptyState()
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(meta.Generations) == 0 || meta.CurrentGenerationID == "" {
|
generations, ok := meta["generations"].([]any)
|
||||||
|
if !ok || len(generations) == 0 {
|
||||||
return renderEmptyState()
|
return renderEmptyState()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
currentID, _ := meta["currentGenerationId"].(string)
|
||||||
|
if currentID == "" {
|
||||||
|
return renderEmptyState()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Find the current generation
|
||||||
var template string
|
var template string
|
||||||
for _, gen := range meta.Generations {
|
for _, gen := range generations {
|
||||||
if gen.ID == meta.CurrentGenerationID {
|
genMap, ok := gen.(map[string]any)
|
||||||
template = gen.Template
|
if !ok {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if genMap["id"] == currentID {
|
||||||
|
template, _ = genMap["template"].(string)
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
2
go.mod
2
go.mod
@ -2,7 +2,7 @@ module git.dev.alexdunmow.com/block/smartblock
|
|||||||
|
|
||||||
go 1.26.4
|
go 1.26.4
|
||||||
|
|
||||||
require git.dev.alexdunmow.com/block/pluginsdk v0.3.5
|
require git.dev.alexdunmow.com/block/pluginsdk v0.2.5
|
||||||
|
|
||||||
require (
|
require (
|
||||||
connectrpc.com/connect v1.20.0 // indirect
|
connectrpc.com/connect v1.20.0 // indirect
|
||||||
|
|||||||
4
go.sum
4
go.sum
@ -1,7 +1,7 @@
|
|||||||
connectrpc.com/connect v1.20.0 h1:6TNDAB+WeNd2uolWNlYczB5E0KNNaVMNUEx8JEUsPmQ=
|
connectrpc.com/connect v1.20.0 h1:6TNDAB+WeNd2uolWNlYczB5E0KNNaVMNUEx8JEUsPmQ=
|
||||||
connectrpc.com/connect v1.20.0/go.mod h1:A2ygJrukXwWy32vkCAAHNVguZrqZ+jeZ9rGRnGR4dN4=
|
connectrpc.com/connect v1.20.0/go.mod h1:A2ygJrukXwWy32vkCAAHNVguZrqZ+jeZ9rGRnGR4dN4=
|
||||||
git.dev.alexdunmow.com/block/pluginsdk v0.3.5 h1:h8YZTgnB/+WQ1yPS5MbJr2aPKuv/kudr93nQowwH8nM=
|
git.dev.alexdunmow.com/block/pluginsdk v0.2.5 h1:01d+5stAywSENScafnNKyMc2ZM5GGFt+hCBKuKTFJW4=
|
||||||
git.dev.alexdunmow.com/block/pluginsdk v0.3.5/go.mod h1:Z+eG+WZxAP0jfreLqlGcc0kkWKt8RWevzWyWn8d+dhM=
|
git.dev.alexdunmow.com/block/pluginsdk v0.2.5/go.mod h1:Z+eG+WZxAP0jfreLqlGcc0kkWKt8RWevzWyWn8d+dhM=
|
||||||
github.com/BurntSushi/toml v1.6.0 h1:dRaEfpa2VI55EwlIW72hMRHdWouJeRF7TPYhI+AUQjk=
|
github.com/BurntSushi/toml v1.6.0 h1:dRaEfpa2VI55EwlIW72hMRHdWouJeRF7TPYhI+AUQjk=
|
||||||
github.com/BurntSushi/toml v1.6.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho=
|
github.com/BurntSushi/toml v1.6.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho=
|
||||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
|
|||||||
@ -2,11 +2,8 @@
|
|||||||
name = "smartblock"
|
name = "smartblock"
|
||||||
display_name = "Smart Block"
|
display_name = "Smart Block"
|
||||||
scope = "@ninja"
|
scope = "@ninja"
|
||||||
version = "1.0.3"
|
version = "1.0.1"
|
||||||
description = "AI-powered block that generates custom content structures from natural-language prompts and renders them by substituting {{field}} placeholders."
|
description = "AI-powered block that generates custom content structures from natural-language prompts and renders them by substituting {{field}} placeholders."
|
||||||
kind = "plugin"
|
kind = "plugin"
|
||||||
categories = ["developer"]
|
categories = ["developer"]
|
||||||
tags = ["ai", "content", "blocks", "generative"]
|
tags = ["ai", "content", "blocks", "generative"]
|
||||||
|
|
||||||
[compatibility]
|
|
||||||
block_core = ">=0.3.4"
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user