Skip to main content

Overview

Returns a function to send model context updates to the host. Unlike useAppState (which automatically syncs structured state), this hook gives direct control over when and what is sent to the model context — including text content blocks. Each call overwrites the previous context. The host includes the last update in the model’s next turn.

Import

import { useUpdateModelContext } from 'sunpeak';

Signature

function useUpdateModelContext(): (params: UpdateModelContextParams) => Promise<void>

UpdateModelContextParams

content
ContentBlock[]
Array of content blocks to include in model context.
structuredContent
Record<string, unknown>
Structured data to include in model context. Serialized as JSON.

Returns

updateModelContext
(params: UpdateModelContextParams) => Promise<void>
Function to push context updates to the host. Each call replaces the previous context.

Usage

Structured Content

import { useUpdateModelContext } from 'sunpeak';

function MapResource() {
  const updateModelContext = useUpdateModelContext();

  const handleSelect = (place: Place) => {
    updateModelContext({
      structuredContent: { selectedPlace: place },
    });
  };

  return <PlaceList onSelect={handleSelect} />;
}

Text Content

import { useUpdateModelContext } from 'sunpeak';

function EditorResource() {
  const updateModelContext = useUpdateModelContext();

  const handleChange = (text: string) => {
    updateModelContext({
      content: [{ type: 'text', text }],
    });
  };

  return <textarea onChange={(e) => handleChange(e.target.value)} />;
}

Comparison with useAppState

FeatureuseAppStateuseUpdateModelContext
Automatic syncYes — syncs on every state changeNo — you call it explicitly
Content typesStructured data onlyText blocks + structured data
State managementBuilt-in (like useState)Bring your own
Use caseSimple persistent stateCustom sync logic, text content

See Also

useAppState

Automatic state persistence and sync.