Skip to content

GuideBrain

The GuideBrain class orchestrates all AI interactions for a GuideFlow instance. It is created internally by createAI() and exposed via instance.ai.

Constructor

ts
new GuideBrain(provider: AIProvider, opts?: GuideBrainOptions)

You rarely need to instantiate this directly — use createAI() instead.

Methods

generate(prompt?, root?)

Captures the current DOM, sends it to the AI provider, and returns generated steps.

ts
async generate(prompt?: string, root?: Element | null): Promise<Step[]>
ParameterTypeDescription
promptstringNatural language description of the tour goal
rootElement | nullRoot element to capture (defaults to document.body)
ts
const steps = await gf.ai.generate('Show the user how to create a project')
await gf.start({ id: 'ai-tour', steps })

chat(question)

Answers a free-form question about the current page using the AI provider.

ts
async chat(question: string): Promise<GuidedAnswer>
ts
const answer = await gf.ai.chat('Where do I find my billing settings?')
console.log(answer.answer)      // human-readable answer string
console.log(answer.stepId)      // optional step to highlight

watch()

Starts passively monitoring user events (clicks, focus, scroll) to emit intent signals.

ts
watch(): void

Safe to call multiple times — duplicate listeners are guarded internally.


stopWatch()

Stops monitoring user events and clears the internal event buffer.

ts
stopWatch(): void

compress(steps)

Removes steps the AI determines the user has already mastered based on their event history.

ts
async compress(steps: Step[]): Promise<Step[]>

on(event, listener)

Subscribe to brain events. Returns an unsubscribe function.

ts
on<K extends keyof BrainEventMap>(event: K, listener: (payload: BrainEventMap[K]) => void): () => void

BrainEventMap

EventPayloadDescription
intent:detectedIntentSignalFired when user intent is detected after a period of inactivity
steps:generatedStep[]Fired after generate() resolves
answer:readyGuidedAnswerFired after chat() resolves
errorErrorFired when any AI operation fails
ts
const off = gf.ai.on('intent:detected', (signal) => {
  console.log('Detected intent:', signal)
})

// Later, unsubscribe:
off()

destroy()

Removes all DOM event listeners, clears the event buffer, and cleans up internal state.

ts
destroy(): void

See Also

Released under the MIT License.