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