Big News: Socket raises $60M Series C at a $1B valuation to secure software supply chains for AI-driven development.Announcement
Sign In

@astro-minimax/ai

Package Overview
Dependencies
Maintainers
1
Versions
15
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@astro-minimax/ai

Vendor-agnostic AI integration package with full RAG pipeline for astro-minimax blogs — supports OpenAI, Cloudflare AI, and custom providers.

latest
Source
npmnpm
Version
0.9.3
Version published
Maintainers
1
Created
Source

@astro-minimax/ai

Vendor-agnostic AI integration package with full RAG pipeline for astro-minimax blogs. Supports OpenAI-compatible APIs, Cloudflare Workers AI, and mock fallback.

Architecture

┌─────────────────────────────────────────────────────────┐
│  Components (ChatPanel / AIChatWidget / AIChatContainer) │
│  → useChat + DefaultChatTransport                        │
└──────────────────────────┬──────────────────────────────┘
                           │ POST /api/chat
┌──────────────────────────▼──────────────────────────────┐
│  Server (chat-handler.ts)                                │
│  Rate Limit → Validate → Search → Evidence → Prompt →   │
│  Provider Manager → streamText → SSE Response            │
└──────────────────────────┬──────────────────────────────┘
                           │
     ┌─────────────────────┼──────────────────────┐
     │                     │                      │
 ┌───▼───┐          ┌─────▼─────┐          ┌─────▼────┐
 │OpenAI │          │Workers AI │          │   Mock   │
 │Compat │          │ Binding   │          │ Fallback │
 └───────┘          └───────────┘          └──────────┘

Modules

ModulePurpose
server/Reusable API handlers (handleChatRequest, initializeMetadata)
provider-manager/Multi-provider management with priority, failover, health tracking
search/In-memory article/project search with session caching
intelligence/Keyword extraction, evidence analysis, citation guard, answer mode, dynamic evidence budget
prompt/Three-layer system prompt builder (static → semi-static → dynamic)
data/Bundle-backed runtime metadata loading and shared data types
components/Preact UI components (ChatPanel, AIChatWidget, AIChatContainer)
extensions/Search/prompt extensions and semantic fallback rules
structured-output/Schema-validated structured generation helpers
cache/Response/session/injection cache utilities
fact-registry/Verified facts used for grounded prompt assembly
tools/Runtime tool registry and built-in action/search tools

Features

Dynamic Evidence Budget

The system dynamically adjusts retrieval and analysis resources based on query complexity:

ComplexityMax ArticlesSummary LengthKey PointsDeep Content
simple448 chars2No
moderate656 chars3Yes
complex864 chars4Yes

Budget is further adjusted by answer mode (count, list, opinion, recommendation):

import { getEvidenceBudget, applyBudgetToArticles } from '@astro-minimax/ai/intelligence';

const budget = getEvidenceBudget('moderate', 'list');
// → { maxArticles: 8, summaryMaxLength: 80, ... }

const trimmedArticles = applyBudgetToArticles(articles, budget);

Answer Mode Detection

Automatically detects the expected response format from user queries:

ModeTrigger PatternsResponse Style
fact"是什么", "what is"Conclusion first, then evidence
count"多少", "how many"Number in first sentence
list"哪些", "what are"2-6 items directly
opinion"怎么看", "what do you think""I think..." + 2-3 points
recommendation"推荐", "suggest"2-4 recommendations + reasons

Answer mode hints are injected into the dynamic prompt layer, guiding the LLM toward the appropriate format.

Reading Time Display

Article reading time is now displayed in the dynamic prompt layer:

**[Article Title](/posts/article)**
阅读时间:约 5 分钟
摘要:Article summary...

Enhanced Citation Guard

Improved URL validation prevents hallucinated links:

  • Scheme whitelist: Only http:// and https:// allowed
  • Domain validation: Blocks localhost, private IPs, internal networks
  • XSS prevention: Sanitizes dangerous URL patterns
import { createCitationGuardTransform } from '@astro-minimax/ai/intelligence';

const guard = createCitationGuardTransform({
  articles,
  projects,
  siteUrl: 'https://example.com',
  onApplied: ({ actions }) => console.log('Rewrote:', actions),
});

Installation

pnpm add @astro-minimax/ai

The @astro-minimax/core integration auto-detects this package and renders the AI chat widget.

Configuration

In src/config.ts:

export const SITE = {
  ai: {
    enabled: true,
    mockMode: false,
    apiEndpoint: "/api/chat",
    welcomeMessage: undefined, // auto-generated
    placeholder: undefined,
  },
};

Environment Variables

VariableRequiredDescription
AI_BASE_URLFor OpenAIBase URL of OpenAI-compatible API
AI_API_KEYFor OpenAIAPI key
AI_MODELRecommendedModel name for OpenAI provider (default: gpt-4o-mini)
AI_KEYWORD_MODELOptionalModel for keyword extraction (defaults to AI_MODEL)
AI_EVIDENCE_MODELOptionalModel for evidence analysis (defaults to keyword model)
AI_BINDING_NAMEFor WorkersCloudflare AI binding name (default: minimaxAI)
AI_WORKERS_MODELFor WorkersModel for Workers AI (default: @cf/zai-org/glm-4.7-flash)
SITE_AUTHORRecommendedAuthor name for prompts
SITE_URLRecommendedSite URL for article links

Response Cache Configuration

VariableDefaultDescription
AI_CACHE_ENABLEDfalseEnable AI response caching
AI_CACHE_TTL3600Cache TTL in seconds (1 hour)
AI_CACHE_PLAYBACK_DELAY20Delay between chunks during playback (ms)
AI_CACHE_CHUNK_SIZE15Characters per chunk during playback
AI_CACHE_THINKING_DELAY5Delay for thinking content playback (ms)

When enabled, the system caches complete AI responses (including thinking/reasoning content) for public questions like "What tech stack does this blog use?". Subsequent identical queries are served from cache with simulated streaming playback, reducing API costs and response time.

Server Module

The server module provides reusable request handlers, decoupled from any specific runtime (Cloudflare, Node.js, etc.).

Usage in Cloudflare Pages Functions

// functions/api/chat.ts
import { handleChatRequest, initializeMetadata } from '@astro-minimax/ai/server';
import knowledgeBundle from '../../datas/knowledge/runtime/knowledge-bundle.json';

export const onRequest: PagesFunction = async (context) => {
  initializeMetadata({ knowledgeBundle }, context.env);

  return handleChatRequest({ env: context.env, request: context.request });
};

Chat API Contract

Request: POST /api/chat

{
  "context": {
    "scope": "article",
    "article": {
      "slug": "my-post",
      "title": "My Post Title",
      "summary": "Brief summary...",
      "keyPoints": ["Point 1", "Point 2"],
      "categories": ["tech"]
    }
  },
  "id": "article:my-post",
  "messages": [...]
}

context.scope values:

  • "global" — General blog chat (default)
  • "article" — Reading companion mode, focused on a specific article

Response: UI Message Stream Protocol (SSE)

  • text-start / text-delta / text-end — Streaming text content
  • source — RAG article references
  • message-metadata — Processing status updates
  • finish — Stream completion

Error Response:

{
  "error": "请求太频繁,请稍后再试",
  "code": "RATE_LIMITED",
  "retryable": true,
  "retryAfter": 10
}
CodeStatusRetryableDescription
RATE_LIMITED429YesToo many requests
PROVIDER_UNAVAILABLE503YesAll providers failed
TIMEOUT504YesRequest timeout
INPUT_TOO_LONG400NoMessage exceeds limit
INVALID_REQUEST400NoMalformed request
INTERNAL_ERROR500YesServer error

Provider System

Priority & Failover

Workers AI (weight: 100) → OpenAI Compatible (weight: 90) → Mock (weight: 0)

When a provider fails, the next one is tried automatically. Mock fallback ensures users always get a response.

Timeout Budget (per request: 45s total)

StageTimeoutBehavior on timeout
Keyword extraction5sFalls back to local search query
Evidence analysis8sSkipped
LLM streaming30sTries next provider, then mock

"Read & Chat" (边读边聊)

When a user opens the AI chat on an article page, the system enters reading companion mode:

  • Article context flows from PostDetails.astroLayout.astroAIChatWidgetChatPanel
  • Welcome message references the current article title
  • Quick prompts are article-specific (summarize, explain, related topics)
  • API request includes context: { scope: "article", article: {...} }
  • Server enhances the prompt with article summary, key points, and reading companion instructions

Components

AIChatWidget.astro

Astro entry point. Accepts lang and optional articleContext props. Renders AIChatContainer with client:idle.

AIChatContainer.tsx

Manages open/close state. Exposes window.__aiChatToggle for the floating action button.

ChatPanel.tsx

Core chat UI built on useChat from @ai-sdk/react:

  • DefaultChatTransport with prepareSendMessagesRequest for context injection
  • Parts-based message rendering (text, source, custom data parts)
  • Error display with retry button (regenerate())
  • Status indicators from message metadata
  • Mock mode with character-by-character streaming simulation

Exports

PathContents
.All modules
./serverhandleChatRequest, initializeMetadata, error helpers, types
./middlewareRate limiting
./searchArticle/project search, session cache
./intelligenceKeyword extraction, evidence analysis, citation guard, answer mode, evidence budget
./promptSystem prompt builder
./cacheCache adapters and response/session cache utilities
./dataMetadata loading
./fact-registryVerified facts registry
./extensionsExtension registry, loader, and injector
./structured-outputStructured output helpers
./toolsTool registry and built-in AI tools
./components/ChatPanelPreact chat panel component
./components/AIChatContainerPreact chat container component
./components/AIChatWidget.astroAstro chat widget entry point

Testing

The package includes comprehensive unit tests with Vitest:

cd packages/ai
pnpm test

Test coverage includes:

  • Citation guard (10 tests)
  • Intent detection (7 tests)
  • Keyword extraction (7 tests)
  • Evidence analysis (7 tests)
  • Evidence budget (5 tests)

Keywords

astro

FAQs

Package last updated on 28 Mar 2026

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts