@molroo-ai/sdk
    Preparing search index...

    Class MolrooWorld

    Main SDK entry point for interacting with a molroo world instance.

    Provides methods for chat, events, ticking, time jumps, mutations, snapshots, and querying world state. Optionally integrates with LLM, storage, memory, and event adapters for richer functionality.

    import { MolrooWorld } from '@molroo-ai/sdk';

    const world = await MolrooWorld.create(
    { baseUrl: 'https://api.molroo.io', apiKey: 'key' },
    {
    definition: { identity: { name: 'Cafe World' } },
    entities: [
    { name: 'user', type: 'user' },
    { name: 'Sera', type: 'persona' },
    ],
    personaConfigs: { Sera: { identity: { name: 'Sera', role: 'barista' } } },
    },
    );

    const result = await world.chat('Sera', 'Hello!');
    Index

    Constructors

    Accessors

    • get id(): string

      Unique identifier for this world instance.

      Returns string

    • get worldId(): string

      Unique identifier for this world instance (alias for id).

      Returns string

    Methods

    • Add a new entity to the world.

      Parameters

      • input: { name: string; profile?: UserProfile; spaceId?: string; type?: string }

        Entity name, type, optional profile and initial space.

      Returns Promise<MutateOp & { data?: unknown }>

      The mutate operation result with optional data.

    • Add a new space to the world.

      Parameters

      • input: { capacity?: number; name: string; spaceType?: string }

        Space name, optional type and capacity.

      Returns Promise<MutateOp & { data?: unknown }>

      The mutate operation result with optional data.

    • Send a message to an entity and get an emotion-processed response.

      With LLM adapter: Fetches world context, generates a response via the configured LLM, then sends it to the API for emotion processing.

      Without LLM adapter: Requires options.appraisal to be provided. Sends the message directly to the API.

      Parameters

      • to: string

        Target entity name (e.g., 'Sera').

      • message: string

        The message text to send.

      • Optionaloptions: ChatOptions

        Chat options (from, appraisal, history, propagateToSpace).

      Returns Promise<ChatResult>

      Chat result with emotion data and optional observer reactions.

      // With LLM (automatic appraisal)
      const result = await world.chat('Sera', 'How are you?');
      console.log(result.response.emotion.discrete.primary); // 'joy'

      // Without LLM (manual appraisal)
      const result2 = await world.chat('Sera', 'Hello', {
      appraisal: { goal_relevance: 0.5, goal_congruence: 0.8, ... },
      });
    • Soft-delete this world. Can be restored with restore.

      Returns Promise<void>

    • Dispatch a world event that affects one or more entities.

      Events can target specific entities or broadcast to a space. Optional stimulus parameters allow direct manipulation of emotion state.

      Parameters

      • input: EventInput

        Event type, target, payload, and optional appraisal/stimulus.

      Returns Promise<EventResult>

      Affected entities with their emotion responses.

    • Get the world context from the perspective of a specific entity.

      Returns spatial awareness, nearby entities, recent events, and world state.

      Parameters

      • entityNameOrId: string

        Entity name or ID to get context for.

      Returns Promise<WorldContext>

      World context data used for LLM prompt building.

    • Restore the world state from a previously saved snapshot.

      Parameters

      Returns Promise<void>

    • Load a world snapshot from external storage and apply it. Requires a StorageAdapter to be configured.

      Parameters

      • Optionalkey: string

        Storage key. Defaults to snapshots/{worldId}/latest.json.

      Returns Promise<void>

    • Move an entity to a different space.

      Parameters

      • target: string

        Entity name or ID to move.

      • toSpace: string

        Destination space name or ID.

      Returns Promise<void>

    • Execute one or more mutation operations on the world atomically.

      Supports: add/remove entities, move entities, set relationships, add spaces/connections, update environment, manage facts/phases.

      Parameters

      • ops: MutateOp[]

        Array of mutation operations.

      Returns Promise<MutateResult>

      Results for each operation (ok/error).

    • Query world state, selecting which data to include.

      Parameters

      • Optionalinclude: QueryInclude[]

        Data sections to include (entities, spaces, environment, definition, phase). If omitted, returns all sections.

      Returns Promise<QueryData>

      Selected world state data.

    • Remove an entity from the world.

      Parameters

      • target: string

        Entity name or ID to remove.

      Returns Promise<void>

    • Restore a previously soft-deleted world.

      Returns Promise<void>

    • Save current world snapshot to external storage. Requires a StorageAdapter to be configured.

      Parameters

      • Optionalkey: string

        Storage key. Defaults to snapshots/{worldId}/latest.json.

      Returns Promise<WorldSnapshot>

      The snapshot that was saved.

    • Set a relationship between two entities.

      Parameters

      • entityA: string

        First entity name.

      • entityB: string

        Second entity name.

      • relationship: RelationshipInput

        Relationship data (type, strength, trust).

      Returns Promise<void>

    • Get a full snapshot of the world state, including all entities, spaces, environment, knowledge, progression, and persona snapshots.

      Returns Promise<WorldSnapshot>

      Complete world snapshot that can be saved and restored.

    • Advance world time by the specified number of seconds.

      Processes emotion decay, pending events, and phase transitions.

      Parameters

      • seconds: number

        Number of seconds to advance.

      Returns Promise<TickResult>

      Tick result with elapsed time and any phase changes.

    • Jump forward in time with an optional milestone event.

      Unlike tick, time jumps can span large periods and optionally inject a milestone event at the destination time.

      Parameters

      • seconds: number

        Number of seconds to jump forward.

      • OptionalmilestoneEvent: Record<string, unknown>

        Optional event to inject at the destination time.

      Returns Promise<TickResult>

      Tick result with elapsed time and any phase changes.

    • Update the world definition (identity, rules, culture, etc.).

      Parameters

      • definition: Record<string, unknown>

        Partial definition to merge with the existing one.

      Returns Promise<void>

    • Update the world environment (weather, location, ambiance, etc.).

      Parameters

      Returns Promise<void>

    • Connect to an existing world by ID.

      Verifies the world exists and fetches persona configs.

      Parameters

      • config: {
            apiKey: string;
            baseUrl: string;
            events?: EventAdapter;
            llm?: LLMAdapter;
            memory?: MemoryAdapter;
            storage?: StorageAdapter;
        }

        API connection config and optional adapters.

      • worldId: string

        The ID of the world to connect to.

      Returns Promise<MolrooWorld>

      A connected MolrooWorld instance.

    • Create a new world on the molroo API and return a connected instance.

      Parameters

      Returns Promise<MolrooWorld>

      A connected MolrooWorld instance.

      const world = await MolrooWorld.create(
      { baseUrl: 'https://api.molroo.io', apiKey: 'key', llm },
      {
      definition: { identity: { name: 'My World' } },
      entities: [{ name: 'Sera', type: 'persona' }],
      personaConfigs: { Sera: { identity: { name: 'Sera' } } },
      },
      );
    • List all worlds for the authenticated tenant.

      Parameters

      • config: { apiKey: string; baseUrl: string }

        API connection config (baseUrl + apiKey).

      Returns Promise<{ nextCursor?: string; worlds: WorldSummary[] }>

      List of world summaries with optional pagination cursor.