Svelte AI Elements

repository·master·Indexed 43 days ago

https://github.com/sikandarjodd/ai-elements

An unofficial Svelte port of Vercel AI Elements providing a suite of components and utilities for building AI-powered applications. It includes specialized UI elements for chat interfaces, streaming markdown responses via streamdown-svelte, reasoning processes (ChainOfThought), AI-generated artifacts, tool-use confirmations, and prompt inputs.

Tokens
55.2K
Snippets
201
Records
277
Agent score
54%

What's inside Svelte AI Elements

  1. Message component overview

    master

    The Message component is a set of composable chat message primitives designed for Svelte applications. Key capabilities include:

    • Markdown Support: Renders markdown-formatted AI responses.
    • Attachments: Handles file or media attachments within the message flow.
    • Branching: Supports message branching logic.
    • Action Slots: Provides slots for interactive elements or user actions within a message.
  2. What is the Confirmation component?

    master

    The Confirmation component is an alert-based UI element used to manage tool execution approval workflows. It provides a mechanism for users to interact with requested actions through three specific states:

    1. Request: The initial state where a tool execution is proposed.
    2. Accept: The state triggered when the user approves the action.
    3. Reject: The state triggered when the user denies the action.
  3. Understand synchronous text generation concepts

    master

    When using the generateText pattern in the Vercel AI SDK, keep the following concepts in mind:

    • Model Selection: You specify the model via provider functions like openai('gpt-4o'). You can swap providers or model versions (e.g., gpt-3.5-turbo) as needed.
    • System Prompt: The system parameter acts as instructions for the AI, setting its persona and constraints.
    • User Prompt: The prompt parameter is the actual query or instruction provided by the user.
    • Synchronous Generation: Unlike streaming APIs, generateText is a blocking operation that waits for the entire response to be generated by the model before resolving. For real-time UI updates, consider using the Stream Text pattern instead.
  4. When to use AI Elements vs Prompt Kit

    master

    Deciding between AI Elements and Prompt Kit depends on the complexity of your application:

    • Use AI Elements when: You are building full-featured, production-ready AI applications. This includes needs for file attachments/uploads, model selection UI, workflow visualization (agent processes), rich response handling (artifacts, tools, reasoning), and complex multi-modal interactions.
    • Use Prompt Kit when: You need a simpler, lightweight interface focused primarily on prompt engineering.
  5. Key concepts for rendering visual tool interfaces

    master

    When building visual interfaces for tools, understand these four core concepts:

    1. Tool Part Types: Every tool generates a specific part.type (e.g., tool-getWeather). Use this to switch between different UI components.
    2. State Handling: Use part.state to distinguish between a tool that is currently running and one where the output is ready to be displayed.
    3. Interactive Tools: Tools can be designed to pause and wait for user input (like a confirmation dialog) before the assistant continues.
    4. addToolOutput: This method is used to programmatically inject the result of a tool execution back into the chat flow, which is essential for both client-side tools and interactive UI buttons.
  6. Key concepts for multi-step tool execution

    master

    When working with multi-step tool calls, understand these three core concepts:

    1. Tool Chaining: The AI automatically uses the output from one tool as the input for another tool in the sequence.
    2. Step Limit: Using functions like stepCountIs(n) (e.g., stepCountIs(5)) is critical to prevent runaway tool chains and control costs/execution time.
    3. Autonomous Decisions: The AI model determines the order of tool calls and decides when the sequence is complete based on the user's prompt and tool outputs.
  7. Key concepts for building a PDF chat assistant

    master

    When building an assistant capable of reading PDFs, keep these three concepts in mind:

    1. FileReader API: Essential for converting uploaded files into base64 data URLs that can be transmitted.
    2. Multi-part Messages: The mechanism used to send both text queries and file data together in a single message payload.
    3. Vision Models: You must use models that specifically support document understanding or vision capabilities to process the file content.
  8. Key concepts for structured object generation

    master

    When using generateObject, keep these core concepts in mind:

    1. Zod Schema: This defines the exact structure of your expected output.
    2. Type Safety: The responses are fully typed to match your Zod schema.
    3. Descriptions: Use the .describe() method on Zod fields to provide context to the AI, helping it understand the purpose of each field.
    4. toJsonResponse(): A helper method on the result object that returns the structured JSON formatted for client consumption.
  9. Key concepts of AI tool calling

    master

    When working with tools in this ecosystem, understand these four core concepts:

    1. tool(): The primary function used to define a tool's metadata, schema, and execution logic.
    2. inputSchema: A Zod schema used to provide type-safe parameters to the tool.
    3. stopWhen: A control mechanism (often using stepCountIs) used to prevent infinite tool-calling loops.
    4. Tool Invocation Parts: The mechanism used to access specific tool calls within the message parts of a conversation.
  10. Compare simple prompts vs chat prompts

    master

    When deciding between a simple text prompt and a chat prompt, consider the following differences:

    FeatureSimple PromptChat Prompt
    Input Formatprompt: "Hello"messages: [{role: "user", content: "Hello"}]
    ContextSingle inputFull conversation history
    System InstructionsNo system message supportDedicated system message support
    StateStatelessStateful conversation