Skip to content

Commit b1f038d

Browse files
jeremymanningclaude
andcommitted
feat(demo-01): Handle goto statements in Rule Breakdown
The Rule Breakdown now properly handles "goto" response templates: - Detects when a template starts with "goto <keyword>" - Follows the goto chain to find the target keyword's rule - Shows a new "Goto Resolution" step with: - The original goto statement (highlighted) - The target keyword and its available templates - A dropdown to see all target templates - Continues processing with the resolved template - Handles chained gotos (up to 10 levels deep) Example: "apologise" → "goto sorry" → "Please don't apologise." 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <[email protected]>
1 parent 011f2b5 commit b1f038d

File tree

2 files changed

+70
-4
lines changed

2 files changed

+70
-4
lines changed

demos/01-eliza/index.html

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,24 @@ <h3>Synonyms</h3>
472472
</div>
473473
`;
474474
stepHTML += `<div class="step-details">Select a different template to see how it changes the response</div>`;
475+
} else if (step.name === 'Goto Resolution' && step.targetTemplates && step.targetTemplates.length > 0) {
476+
// Special handling for Goto Resolution - show the chain and target templates
477+
stepHTML += `
478+
<div class="step-io">
479+
<div style="flex: 1;">
480+
<div class="io-label">Goto Statement</div>
481+
<div class="io-box" style="background: var(--warning-bg, #fff3cd); border-color: var(--warning-border, #ffc107);">${escapeHtml(step.input)}</div>
482+
</div>
483+
<div class="arrow" style="color: var(--warning-color, #856404);">↪</div>
484+
<div style="flex: 1;">
485+
<div class="io-label">Target: "${escapeHtml(step.targetKeyword)}" (${step.targetTemplates.length} template(s))</div>
486+
<select class="template-dropdown" id="goto-template-selector" style="width: 100%; padding: 10px; border: 2px solid var(--border-color); border-radius: 6px; font-size: 0.9rem; background: var(--surface-color); color: var(--text-primary); cursor: pointer;">
487+
${step.targetTemplates.map((t, i) => `<option value="${i}" ${escapeHtml(t) === escapeHtml(step.output.replace(/^"|"$/g, '')) ? 'selected' : ''}>${escapeHtml(t)}</option>`).join('')}
488+
</select>
489+
</div>
490+
</div>
491+
`;
492+
stepHTML += `<div class="step-details">${step.details}</div>`;
475493
} else if (step.input !== undefined && step.output !== undefined) {
476494
// Add input/output visualization for other steps
477495
stepHTML += `
@@ -489,9 +507,9 @@ <h3>Synonyms</h3>
489507
`;
490508
}
491509

492-
// Add details (but not for template selection since we add custom details)
510+
// Add details (but not for template selection or goto resolution since we add custom details)
493511
// For Keyword Detection, make it expandable
494-
if (step.details && step.name !== 'Template Selection') {
512+
if (step.details && step.name !== 'Template Selection' && step.name !== 'Goto Resolution') {
495513
if (step.name === 'Keyword Detection' && step.keywordsFound && step.keywordsFound.length > 0) {
496514
const keywordStepId = `keywords-${index}`;
497515
stepHTML += `<div class="pattern-tests-header" onclick="togglePatternTests('${keywordStepId}')" style="cursor: pointer; color: var(--primary-color); margin-bottom: 8px;">

demos/01-eliza/js/pattern-matcher.js

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -544,10 +544,10 @@ export class PatternMatcher {
544544

545545
// Step 5: Response template selection
546546
const allTemplates = matchedPattern ? matchedPattern.responses : [];
547-
const selectedTemplateIndex = matchedPattern
547+
let selectedTemplateIndex = matchedPattern
548548
? Math.floor(Math.random() * matchedPattern.responses.length)
549549
: 0;
550-
const selectedTemplate = allTemplates[selectedTemplateIndex] || null;
550+
let selectedTemplate = allTemplates[selectedTemplateIndex] || null;
551551

552552
if (matchedPattern && allTemplates.length > 0) {
553553
breakdown.steps.push({
@@ -561,6 +561,54 @@ export class PatternMatcher {
561561
});
562562
}
563563

564+
// Step 5b: Handle "goto" statements - follow the reference to another keyword
565+
let gotoChain = [];
566+
let finalTemplate = selectedTemplate;
567+
let gotoTargetRule = null;
568+
let gotoTargetPattern = null;
569+
570+
while (finalTemplate && finalTemplate.startsWith('goto ')) {
571+
const targetKeyword = finalTemplate.substring(5).trim();
572+
gotoChain.push(targetKeyword);
573+
574+
// Find the target rule
575+
const targetRule = rules.find(r => r.keyword === targetKeyword);
576+
if (targetRule && targetRule.patterns && targetRule.patterns.length > 0) {
577+
gotoTargetRule = targetRule;
578+
gotoTargetPattern = targetRule.patterns[0];
579+
const targetTemplates = gotoTargetPattern.responses;
580+
const targetTemplateIndex = Math.floor(Math.random() * targetTemplates.length);
581+
finalTemplate = targetTemplates[targetTemplateIndex];
582+
} else {
583+
// Target not found, break out
584+
break;
585+
}
586+
587+
// Prevent infinite loops (max 10 gotos)
588+
if (gotoChain.length >= 10) break;
589+
}
590+
591+
// Add goto resolution step if we followed any gotos
592+
if (gotoChain.length > 0) {
593+
breakdown.steps.push({
594+
name: 'Goto Resolution',
595+
description: 'Following reference to another keyword',
596+
input: `"${selectedTemplate}"`,
597+
output: gotoTargetRule ? `"${finalTemplate}"` : 'Target not found',
598+
details: gotoChain.length === 1
599+
? `Redirected to keyword "${gotoChain[0]}"`
600+
: `Followed chain: ${gotoChain.map(k => `"${k}"`).join(' → ')}`,
601+
gotoChain: gotoChain,
602+
targetKeyword: gotoChain[gotoChain.length - 1],
603+
targetRule: gotoTargetRule,
604+
targetPattern: gotoTargetPattern,
605+
targetTemplates: gotoTargetPattern ? gotoTargetPattern.responses : []
606+
});
607+
608+
// Update selected template to the resolved one
609+
selectedTemplate = finalTemplate;
610+
}
611+
564612
// Step 6: Assembly with post-substitutions
565613
if (selectedTemplate && matchResult) {
566614
const postSubSteps = [];

0 commit comments

Comments
 (0)