Agent Skills › Prastavna/cfw-mailbox › cfw-mailbox-ai-assistant

cfw-mailbox-ai-assistant

GitHub

Cloudflare Mailbox AI助手扩展,支持多提供商模型配置、加密密钥管理、流式AI功能(总结/写作/聊天)及速率限制。

skills/cfw-mailbox-ai-assistant/SKILL.md Prastavna/cfw-mailbox

Trigger Scenarios

配置AI提供商或模型设置 处理加密API密钥 实现AI路由如摘要或聊天 调整AI提示词 测试AI连接

Install

npx skills add Prastavna/cfw-mailbox --skill cfw-mailbox-ai-assistant -g -y
More Options

Use without installing

npx skills use Prastavna/cfw-mailbox@cfw-mailbox-ai-assistant

指定 Agent (Claude Code)

npx skills add Prastavna/cfw-mailbox --skill cfw-mailbox-ai-assistant -a claude-code -g -y

安装 repo 全部 skill

npx skills add Prastavna/cfw-mailbox --all -g -y

预览 repo 内 skill

npx skills add Prastavna/cfw-mailbox --list

SKILL.md

Frontmatter
{
    "name": "cfw-mailbox-ai-assistant",
    "license": "Apache-2.0",
    "description": "Work on the cfw-mailbox AI Assistant extension. Use for AI provider or model settings, encrypted API keys, Workers AI, prompt changes, streaming summarize\/write\/rewrite\/chat routes, the AI settings dialog, composer assist, thread summaries, or AI tests."
}

cfw-mailbox AI Assistant

An optional, per-user extension. Each user brings their own provider/key (or uses the deployment's Workers AI binding). Nothing runs until the user enables it and the configuration is complete.

Key files

extensions/ai/routes.ts       /api/extensions/ai* and /api/mailboxes/:id/ai/*
extensions/ai/settings.ts     settings load/save, secret encryption, requireAiConfig
extensions/ai/models.ts       provider factories, model listing
extensions/ai/prompts.ts      summary/write/rewrite/chat prompts and thread formatting
app/components/extensions/
  AiSettingsDialog.tsx        provider, key, base URL, model, test connection
  ai/AskInbox.tsx             top-bar chat over the mailbox
  ai/ComposeAssist.tsx        compose-window writing and rewriting
  ai/ThreadSummary.tsx        summary card in a conversation
app/lib/extensions.ts         React Query hooks + streaming helper
shared/extensions.ts          AI_PROVIDERS, AiSettings, REWRITE_MODES, AI_LIMITS
test/ai.test.ts               scripted model tests
e2e/ai.spec.ts + fake-llm.ts  end-to-end tests against a local fake provider

Data flow

  1. Settings are per user in registry.user_extensions with id ai. settings holds provider/model/baseUrl/keyHint as JSON; secret holds the encrypted API key. enabled gates the UI and the routes.
  2. Saving — PUT /api/extensions/ai merges the update. Switching provider clears the key and resets model/baseUrl to that provider's defaults. The key is sealed with ENCRYPTION_KEY and bound to the user's email (secretContext) via workers/lib/crypto.ts. The browser only ever sees hasKey and the last characters (keyHint).
  3. Readiness — isReady() requires a model and any provider-specific requirements (key for Anthropic/OpenAI/Google; base URL for OpenAI-compatible). requireAiConfig throws a readable HttpError when the extension is off or incomplete.
  4. Requests — streamText for rewrite/write/summarize returns plain text (textResponse waits for the first token so bad keys/models surface as errors); chat returns an AI SDK UI message stream. All accept the request's abort signal and use maxRetries: 1.
  5. Limits — AI_LIMITER is 20 requests/minute/user. Every AI route calls rateLimit before doing work.
  6. UI — hooks in app/lib/extensions.ts (useAiReady, useAiSettings, useSaveAiSettings, useAiModels, useAiRun, streamAiText). The composer/thread/top bar only show AI controls when useAiReady() is true.

Providers

AI_PROVIDERS in shared/extensions.ts defines label, key/base-URL needs, default model, key URL and placeholder. Factories live in extensions/ai/models.ts:

  • Anthropic, OpenAI, Google — key required.
  • openai-compatible — base URL required, key optional; HTTPS, or HTTP only for localhost. Works with OpenRouter, Groq, Together, DeepSeek, Ollama, LM Studio.
  • workers-ai — no key; uses the AI binding and is billed to the deployment account. It is always a remote binding, even in local dev.

Model IDs are free text; GET /api/extensions/ai/models can list them from the provider when the saved configuration allows it.

Routes

Route Purpose
GET /api/extensions Extension tiles, including AI enabled/ready
GET/PUT /api/extensions/ai Read/update settings
GET /api/extensions/ai/models List the provider's models
POST /api/extensions/ai/test Tiny generation to validate key/model
POST /api/extensions/ai/rewrite Rewrite text (streams plain text)
POST /api/mailboxes/:id/ai/summarize Summarize a thread
POST /api/mailboxes/:id/ai/write Draft a message from instructions
POST /api/mailboxes/:id/ai/chat Ask the inbox (UI message stream)

The mailbox AI routes sit behind the normal mailbox role check. Viewers may use the read-only AI routes; everything else remains blocked.

Adding or changing a provider

  1. Add the entry to AI_PROVIDERS in shared/extensions.ts (label, needs, default model, key URL, placeholder).
  2. Add a factory case in extensions/ai/models.ts and make sure listModels handles it (or returns an empty list).
  3. Check isReady, saveAiSettings's provider switch, and AiSettingsDialog rendering for the new fields.
  4. Add a scripted test in test/ai.test.ts if the provider needs special handling.

Use the installed AI SDK version's APIs (ai, @ai-sdk/*). Do not guess model IDs; use the provider's current names.

UI notes

  • AiSettingsDialog saves before testing a connection; a successful test shows the provider's reply.
  • If the server cannot store keys (ENCRYPTION_KEY missing), the dialog says so and only Workers AI is offered.
  • useAiRun allows one request at a time and cancels on unmount.
  • ComposeAssist works on the current selection or the whole body; empty output is an error, not a silent no-op.
  • Thread summaries are rendered in a SummaryCard marked "Written by AI".

Testing

  • test/ai.test.ts uses MockLanguageModelV4 from ai/test to script text and tool steps. It also verifies the key is encrypted at rest and never returned.
  • e2e/ai.spec.ts uses a local fake OpenAI-compatible server (e2e/fake-llm.ts). AI settings belong to the single dev user, so the suite is serial and resets settings in beforeEach.
  • The Worker cold-starts with the AI SDK bundled; test timeouts are raised in vitest.config.ts.

Pitfalls

  • Never log, return or store a plaintext API key. AiSettings only carries hasKey/keyHint.
  • ENCRYPTION_KEY is optional. Without it, canStoreKeys is false and keys cannot be saved; Workers AI must still work.
  • Changing ENCRYPTION_KEY makes all saved keys unreadable; users must re-enter them.
  • Do not call the provider before requireAiConfig has checked enabled and readiness.
  • Streaming errors can arrive as stream parts, not throws; textResponse handles that. Keep the first-token wait so invalid keys produce real statuses.
  • The AI binding is remote and billed. Tests must not depend on it.
  • Keep the AI routes' maxRetries low; provider rate limits are explained to the user by describeAiError.

Version History

  • d9871cd Current 2026-12-01 07:51

Same Skill Collection

.agents/skills/react-router/SKILL.md
skills/cfw-mailbox-open-tracking/SKILL.md
skills/cfw-mailbox/SKILL.md

Metadata

Files
0
Version
85a5bf1
Hash
46139db7
Indexed
2026-12-01 07:51

Главная - Вики-сайт
Copyright © 2011-2026 iteam. Current version is 2.156.0. UTC+08:00, 2026-12-02 00:13
浙ICP备14020137号-1