cfw-mailbox-ai-assistant
GitHubCFW邮箱AI助手扩展,支持多模型配置、密钥加密存储及速率限制。提供流式摘要、写作、重写和聊天功能,集成于邮件撰写、线程总结和顶部栏对话界面。
Trigger Scenarios
Install
npx skills add Prastavna/cfw-mailbox --skill cfw-mailbox-ai-assistant -g -y
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
- Settings are per user in
registry.user_extensionswith idai.settingsholds provider/model/baseUrl/keyHint as JSON;secretholds the encrypted API key.enabledgates the UI and the routes. - Saving —
PUT /api/extensions/aimerges the update. Switching provider clears the key and resets model/baseUrl to that provider's defaults. The key is sealed withENCRYPTION_KEYand bound to the user's email (secretContext) viaworkers/lib/crypto.ts. The browser only ever seeshasKeyand the last characters (keyHint). - Readiness —
isReady()requires a model and any provider-specific requirements (key for Anthropic/OpenAI/Google; base URL for OpenAI-compatible).requireAiConfigthrows a readableHttpErrorwhen the extension is off or incomplete. - Requests —
streamTextfor rewrite/write/summarize returns plain text (textResponsewaits 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 usemaxRetries: 1. - Limits —
AI_LIMITERis 20 requests/minute/user. Every AI route callsrateLimitbefore doing work. - UI — hooks in
app/lib/extensions.ts(useAiReady,useAiSettings,useSaveAiSettings,useAiModels,useAiRun,streamAiText). The composer/thread/top bar only show AI controls whenuseAiReady()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 theAIbinding 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
- Add the entry to
AI_PROVIDERSinshared/extensions.ts(label, needs, default model, key URL, placeholder). - Add a factory case in
extensions/ai/models.tsand make surelistModelshandles it (or returns an empty list). - Check
isReady,saveAiSettings's provider switch, andAiSettingsDialogrendering for the new fields. - Add a scripted test in
test/ai.test.tsif 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
AiSettingsDialogsaves before testing a connection; a successful test shows the provider's reply.- If the server cannot store keys (
ENCRYPTION_KEYmissing), the dialog says so and only Workers AI is offered. useAiRunallows one request at a time and cancels on unmount.ComposeAssistworks on the current selection or the whole body; empty output is an error, not a silent no-op.- Thread summaries are rendered in a
SummaryCardmarked "Written by AI".
Testing
test/ai.test.tsusesMockLanguageModelV4fromai/testto script text and tool steps. It also verifies the key is encrypted at rest and never returned.e2e/ai.spec.tsuses 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 inbeforeEach.- 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.
AiSettingsonly carrieshasKey/keyHint. ENCRYPTION_KEYis optional. Without it,canStoreKeysis false and keys cannot be saved; Workers AI must still work.- Changing
ENCRYPTION_KEYmakes all saved keys unreadable; users must re-enter them. - Do not call the provider before
requireAiConfighas checkedenabledand readiness. - Streaming errors can arrive as stream parts, not throws;
textResponsehandles that. Keep the first-token wait so invalid keys produce real statuses. - The
AIbinding is remote and billed. Tests must not depend on it. - Keep the AI routes'
maxRetrieslow; provider rate limits are explained to the user bydescribeAiError.
Version History
- d9871cd Current 2026-12-01 07:51


