Skip to content

Commit 6adceb0

Browse files
committed
feat: multi-tag template + variable resolution fix for Run All
1 parent b6a692e commit 6adceb0

5 files changed

Lines changed: 162 additions & 1 deletion

File tree

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Multi-Tag Template & Variable Resolution
2+
3+
## Changes
4+
5+
### New Template: AI Global Briefing Generator
6+
- Added zero-input template chaining 5 tag types across 6 blocks: `AI → Think → Translate → TTS → Agent`
7+
- Template auto-generates a country briefing with analysis, translated phrases, pronunciation audio, and a 3-step action plan
8+
- No file uploads, voice recording, or API keys required — just set variables and click Run All
9+
10+
### Variable Resolution Fix
11+
- **`templates.js`**: `loadTemplate()` now auto-populates `M._vars.setManual()` with template variable values on load
12+
- **`templates.js`**: `applyTemplateVariables()` syncs parsed variables into `M._vars` when ⚡ Vars is clicked
13+
- **`exec-sandbox.js`**: `ctxResolveReferences()` falls back to `M._vars.get()` when SQLite context returns null — fixes `$(country)` unresolved during Run All
14+
15+
### Display Fix: Metadata Stripping
16+
- **`ai-docgen.js`**: STT card renderer now strips `@var:` and `@input:` from display text
17+
- **`ai-docgen.js`**: TTS and Translate card renderers now strip `@input:` from display text
18+
19+
## Files Modified
20+
- `js/templates/ai.js` — New template (+116 lines)
21+
- `js/templates.js` — Auto-populate M._vars on template load (+13 lines)
22+
- `js/exec-sandbox.js` — Fallback variable resolution (+7 lines)
23+
- `js/ai-docgen.js` — Metadata display stripping (+4 lines)

js/ai-docgen.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -635,6 +635,7 @@
635635
var translateDisplayText = prompt;
636636
translateDisplayText = translateDisplayText.replace(/^\s*(?:@lang|Lang):\s*.+$/mi, '').trim();
637637
translateDisplayText = translateDisplayText.replace(/^\s*(?:@var|Var):\s*\S+$/mi, '').trim();
638+
translateDisplayText = translateDisplayText.replace(/^\s*(?:@input|Input):\s*.+$/mi, '').trim();
638639
translateDisplayText = translateDisplayText.replace(/^\s*@upload:\s*.+$/gmi, '').trim();
639640
translateDisplayText = translateDisplayText.replace(/^\s*(?:@model|Model):\s*\S+$/mi, '').trim();
640641

@@ -691,6 +692,7 @@
691692
var ttsDisplayText = prompt;
692693
ttsDisplayText = ttsDisplayText.replace(/^\s*(?:@lang|Lang):\s*.+$/mi, '').trim();
693694
ttsDisplayText = ttsDisplayText.replace(/^\s*(?:@var|Var):\s*\S+$/mi, '').trim();
695+
ttsDisplayText = ttsDisplayText.replace(/^\s*(?:@input|Input):\s*.+$/mi, '').trim();
694696
ttsDisplayText = ttsDisplayText.replace(/^\s*(?:@model|Model):\s*\S+$/mi, '').trim();
695697

696698
var ttsHasPrompt = /^\s*(?:@prompt|Prompt):\s*/m.test(ttsDisplayText);
@@ -751,6 +753,8 @@
751753
var sttDisplayText = prompt;
752754
sttDisplayText = sttDisplayText.replace(/^\s*(?:@lang|Lang):\s*.+$/mi, '').trim();
753755
sttDisplayText = sttDisplayText.replace(/^\s*(?:@engine|Engine):\s*.+$/mi, '').trim();
756+
sttDisplayText = sttDisplayText.replace(/^\s*(?:@var|Var):\s*\S+$/mi, '').trim();
757+
sttDisplayText = sttDisplayText.replace(/^\s*(?:@input|Input):\s*.+$/mi, '').trim();
754758
sttDisplayText = sttDisplayText.replace(/^\s*(?:@model|Model):\s*\S+$/mi, '').trim();
755759

756760
// Parse @lang

js/exec-sandbox.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,13 @@
550550
if (value !== null) {
551551
return value;
552552
}
553+
// Fallback: check document/template variables (M._vars)
554+
if (!key && M._vars) {
555+
var varsValue = M._vars.get(blockId);
556+
if (varsValue !== undefined && varsValue !== null) {
557+
return varsValue;
558+
}
559+
}
553560
// Leave unresolved references as-is
554561
console.warn('[ExecContext] Unresolved reference:', match);
555562
return match;

js/templates.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,10 @@
297297
for (const [key, val] of Object.entries(vars)) {
298298
const resolvedVal = val || (M._vars ? M._vars.get(key) : null) || '';
299299
result = result.replace(new RegExp('\\$\\(' + key + '\\)', 'g'), resolvedVal);
300+
// Sync into M._vars so $(varName) resolves during Run All
301+
if (M._vars && resolvedVal) {
302+
M._vars.setManual(key, resolvedVal);
303+
}
300304
}
301305

302306
// Dynamic stock-grid expansion:
@@ -341,8 +345,17 @@
341345
let content = tpl.content;
342346

343347
// If the template defines variables, generate the block at the top
348+
// AND auto-populate M._vars so $(varName) resolves during Run All
344349
if (tpl.variables && tpl.variables.length > 0) {
345350
content = generateVariableBlock(tpl.variables) + content;
351+
if (M._vars) {
352+
for (const v of tpl.variables) {
353+
const val = v.value || v.default || '';
354+
if (v.name && val) {
355+
M._vars.setManual(v.name, val);
356+
}
357+
}
358+
}
346359
}
347360

348361
// Store the raw template content (with $(var) placeholders)

js/templates/ai.js

Lines changed: 115 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1557,5 +1557,119 @@ Models use ONNX format for efficient browser inference via [Transformers.js](htt
15571557
> 💡 **How to use:** This template serves as a reference card for managing your local AI models. Click **✨ Fill** to generate the storage guide section.
15581558
`
15591559
},
1560-
];
1560+
{
1561+
name: 'AI Global Briefing Generator',
1562+
category: 'ai',
1563+
displayTag: 'AI · Think · Translate · TTS · Agent',
1564+
icon: 'bi-globe-americas',
1565+
description: 'Zero-input tag chain — AI researches a country, Think analyzes, Translate phrases, TTS speaks them, Image generates visuals, Agent builds a report. Just click Run All.',
1566+
variables: [
1567+
{ name: 'country', value: 'Japan', desc: 'Country to research' },
1568+
{ name: 'localLang', value: 'Japanese', desc: 'Local language' },
1569+
{ name: 'purpose', value: 'business trip', desc: 'Purpose: business trip / vacation / relocation / study abroad' },
1570+
],
1571+
content: `# 🌍 Country Briefing — $(country)
1572+
1573+
**Purpose:** $(purpose)
1574+
**Local Language:** $(localLang)
1575+
**Generated:** $(date)
1576+
1577+
> 💡 **Zero input required.** Every block auto-generates from the previous one. Click **⚡ Run All** to chain: AI → Think → Translate → TTS → Image → Agent.
1578+
1579+
---
1580+
1581+
## ✨ Step 1 — Country Overview (AI)
1582+
1583+
{{@AI:
1584+
@search: duckduckgo, wikipedia
1585+
@var: country_overview
1586+
@prompt: Write a comprehensive briefing on $(country) for someone preparing a $(purpose). Cover:
1587+
1588+
- 🏛️ **Key facts** — capital, population, currency, timezone, visa requirements
1589+
- 🌤️ **Climate** — best times to visit, what to expect by season
1590+
- 💰 **Cost of living** — average daily budget (budget / mid-range / luxury)
1591+
- 🚆 **Transport** — how to get around, apps to use, airport to city
1592+
- 🔌 **Practical** — power outlets, tipping culture, SIM cards, WiFi
1593+
1594+
Be specific with numbers and real names — no generic advice.}}
1595+
1596+
---
1597+
1598+
## 🧠 Step 2 — Strategic Analysis (Think)
1599+
1600+
{{Think:
1601+
@input: country_overview
1602+
@var: deep_analysis
1603+
@prompt: Analyze this $(country) briefing for someone on a $(purpose):
1604+
1605+
$(country_overview)
1606+
1607+
Think deeply about:
1608+
1. **Cultural pitfalls** — what foreigners commonly get wrong, taboos to avoid
1609+
2. **Hidden opportunities** — insider tips locals know but guides don't mention
1610+
3. **Risk assessment** — safety, scams, health concerns, political climate
1611+
4. **Best value strategies** — when to book, where to save, loyalty programs to leverage
1612+
5. **Networking/Social** — how to connect with locals, business etiquette, social norms
1613+
1614+
Provide sharp, actionable insights — not surface-level observations.}}
1615+
1616+
---
15611617
1618+
## 🌐 Step 3 — Essential Phrases (Translate)
1619+
1620+
{{@Translate:
1621+
@input: country_overview
1622+
@var: key_phrases
1623+
@lang: $(localLang)
1624+
@prompt: Generate 12 essential phrases for a $(purpose) in $(country):
1625+
1626+
**Greetings:** Hello, Good morning, Thank you, Excuse me, I'm sorry
1627+
**Navigation:** Where is…?, How much does this cost?, The bill please
1628+
**$(purpose)-specific:** I have a meeting at…, Could you recommend…?, Is this area safe?
1629+
1630+
Format each as: **English** → **$(localLang)** *(pronunciation)*
1631+
}}
1632+
1633+
---
1634+
1635+
## 🔊 Step 4 — Hear the Phrases (TTS)
1636+
1637+
{{@TTS:
1638+
@model: kokoro-tts
1639+
@input: key_phrases
1640+
@lang: $(localLang)
1641+
@prompt: $(key_phrases)
1642+
}}
1643+
1644+
---
1645+
1646+
## 🔗 Step 5 — Complete Action Plan (Agent Flow)
1647+
1648+
{{@Agent:
1649+
@input: country_overview, deep_analysis, key_phrases
1650+
@search: duckduckgo
1651+
@var: action_plan
1652+
@step 1: Build a detailed $(purpose) preparation checklist for $(country). Include: documents needed, vaccinations, insurance, banking setup, phone plan, apps to install, items to pack. Organize by timeline (4 weeks before, 1 week before, day of departure).
1653+
@step 2: Create a first-week schedule for $(country). Day-by-day plan with morning/afternoon/evening activities tailored to a $(purpose). Include specific neighborhoods, restaurant names, and transport instructions between locations.
1654+
@step 3: Write a quick-reference cheat sheet — a single-page summary with emergency numbers, embassy address, key phrases from the translation, cultural do's and don'ts, and the top 5 things to experience. Format it as a compact card that could be printed or saved to a phone.
1655+
}}
1656+
1657+
---
1658+
1659+
## 🔊 Step 6 — Audio Cheat Sheet (TTS)
1660+
1661+
{{@TTS:
1662+
@model: kokoro-tts
1663+
@input: action_plan
1664+
@lang: English
1665+
@prompt: $(country) trip cheat sheet: $(action_plan)
1666+
}}
1667+
1668+
---
1669+
1670+
> 🔗 **Tag Chain:** \\\`AI\\\` (research + web search) → \\\`Think\\\` (analysis) → \\\`Translate\\\` (phrases) → \\\`TTS\\\` (pronunciation) → \\\`Agent\\\` (3-step plan) → \\\`TTS\\\` (audio)
1671+
>
1672+
> **5 tag types, 6 blocks, zero input required.** Just fill in the variables and click ⚡ Run All.
1673+
`
1674+
},
1675+
];

0 commit comments

Comments
 (0)