Skill

Build Cross-Platform AI Chat Interfaces

Best practices for building cross-platform AI chat UIs in Makepad with MolyKit: async patterns, widgets, and SSE streaming.

Works with openai

91
Spark score
out of 100
Updated 20 days ago
Version 14.1.0
Models
gpt 4universal

Add to Favorites

Why it matters

Develop sophisticated, cross-platform AI chat applications using Makepad and MolyKit. Integrate with LLM APIs like OpenAI, handle streaming responses, and build reusable UI components for a seamless user experience.

Outcomes

What it gets done

01

Implement cross-platform asynchronous patterns for native and WASM.

02

Integrate OpenAI or other LLM APIs with a flexible BotClient trait.

03

Create reusable chat widgets including message displays, prompt inputs, and avatars.

04

Handle Server-Sent Events (SSE) for real-time streaming responses.

Install

Add it to your toolbox

Run in your project directory:

curl -fsSL https://spark.entire.vc/get/ag-molykit | bash

Overview

MolyKit Skill

A MolyKit reference for cross-platform AI chat apps built on Makepad, covering async primitives for native/WASM, the BotClient provider trait, chat widgets, and SSE stream parsing. Use when building or extending an AI chat interface in Makepad, integrating an LLM provider, or handling streaming responses across native and WASM targets.

What it does

MolyKit Skill covers best practices for building cross-platform AI chat interfaces with Makepad using MolyKit - a toolkit providing cross-platform async utilities, ready-to-use chat widgets, AI provider integration via a BotClient trait, an OpenAI-compatible client with SSE streaming, message/bot protocol types, and MCP (Model Context Protocol) support.

When to use - and when NOT to

Use this when building AI chat interfaces with Makepad, integrating OpenAI or other LLM APIs, implementing cross-platform async for native and WASM targets, creating chat widgets such as messages, prompts, or avatars, or handling SSE streaming responses.

Inputs and outputs

Cross-platform async patterns: PlatformSend (a marker implying Send only on native, implemented by all types on WASM), spawn() (platform-agnostic task spawning - tokio on native, wasm-bindgen-futures on WASM, requiring no Send bound on WASM), AbortOnDropHandle (cancels its future automatically when a widget holding it is dropped), and ThreadToken (stores a non-Send value in thread-local storage, accessed safely via peek/peek_mut across Send boundaries). AI provider integration goes through the BotClient trait (send returns a streamed response, bots lists available bots/models, clone_box clones the trait object), wrapped by BotContext, a shareable Arc<Mutex<...>> handle that loads bots asynchronously and exposes synchronous UI-side accessors. Protocol types include Message (sender EntityId, MessageMetadata, MessageContent), MessageContent (text, reasoning, citations, attachments, tool calls, tool results), and BotId, a globally unique <len>;<id>@<provider> identifier, e.g. BotId::new("gpt-4", "api.openai.com").

Integrations

Ready-made chat widgets: Slot (runtime content replacement via replace/restore, letting a BotClient supply custom widget content), Avatar (toggles between a text grapheme and an image dependency), PromptInput (send/stop task state, enable/disable, submission detection), and Messages (holds the conversation, scrolls to bottom, tracks bot context). Async work reaches the UI thread via the UiRunner pattern (ui_runner().handle(...), defer_with_redraw), and streaming responses are parsed with parse_sse, which splits an SSE byte stream on blank lines, extracts data: payloads, and filters comments and [DONE] markers before being consumed inside a BotClient::send implementation.

Who it's for

Rust developers building AI chat UIs on Makepad who need the cross-platform async primitives, a concrete BotClient implementation pattern demonstrated against an OpenAI-compatible endpoint, and the chat-specific widget set, guided by eight stated best practices: prefer PlatformSend and spawn() over platform-specific APIs, use AbortOnDropHandle and ThreadToken for cancellation and non-Send WASM state, use Slot for custom content, follow the read()/write() borrow pattern via WidgetRef, update widgets from async contexts through UiRunner::defer_with_redraw, and handle ClientResult's partial-success case where a value and errors can both be present.

Source README

MolyKit provides:

  • Cross-platform async utilities (PlatformSend, spawn(), ThreadToken)
  • Ready-to-use chat widgets (Chat, Messages, PromptInput, Avatar)
  • BotClient trait for AI provider integration
  • OpenAI-compatible client with SSE streaming
  • Protocol types for messages, bots, and tool calls
  • MCP (Model Context Protocol) support

FAQ

Common questions

Discussion

Questions & comments ยท 0

Sign In Sign in to leave a comment.