Twilight AI
When To Use
Use this skill when the task involves twilight-ai, especially:
- implementing or refactoring SDK APIs in
sdk/
- adding or updating providers under
provider/
- working on
GenerateText, GenerateTextResult, StreamText, Embed, EmbedMany, GenerateImage, or EditImage
- adding tool-calling, streaming, reasoning, embedding, or image generation support
- writing examples, docs, or usage guidance for this library
Project Snapshot
Twilight AI is a lightweight Go AI SDK with a provider-agnostic core API.
- Text generation:
sdk.GenerateText, sdk.GenerateTextResult, sdk.StreamText
- Image generation:
sdk.GenerateImage, sdk.EditImage
- Embeddings:
sdk.Embed, sdk.EmbedMany
- Tool calling:
sdk.Tool, sdk.NewTool[T], WithMaxSteps, approval flow
- MCP tool integration:
sdk.CreateMCPClient, sdk.MCPClient, sdk.MCPClientConfig
- Streaming: typed
StreamPart events over Go channels
- Current providers:
provider/openai/completions
provider/openai/responses
provider/openai/codex
provider/openai/images
provider/anthropic/messages
provider/google/generativeai
provider/openai/embedding
provider/google/embedding
Default Mental Model
Prefer the high-level SDK API first, then drop to provider details only when needed.
sdk.Model binds a chat model to a sdk.Provider
sdk.EmbeddingModel binds an embedding model to an sdk.EmbeddingProvider
sdk.ImageGenerationModel binds an image generation model to an sdk.ImageGenerationProvider
sdk.ImageEditModel binds an image edit model to an sdk.ImageEditProvider
- The client orchestrates tool loops, callbacks, approvals, and streaming lifecycle
- MCP clients can load remote MCP tools and turn them into ordinary
sdk.Tool values
- Providers handle backend-specific HTTP, request mapping, response parsing, and SSE translation
Core API Guidance
Choose the narrowest API that matches the task:
- Need only final text: use
sdk.GenerateText
- Need usage, finish reason, steps, sources, files, or tool details: use
sdk.GenerateTextResult
- Need live output: use
sdk.StreamText
- Need one vector: use
sdk.Embed
- Need multiple vectors or embedding token usage: use
sdk.EmbedMany
- Need image generation from a text prompt: use
sdk.GenerateImage
- Need image editing or inpainting: use
sdk.EditImage
If the task introduces examples or docs, prefer simple end-to-end snippets that start with:
- construct provider
- get model
- call SDK API
- handle error
Provider Selection Rules
- Use
openai/completions for broad OpenAI-compatible support such as DeepSeek, Groq, Ollama, Azure-style compatible endpoints, and generic /chat/completions backends.
- Use
openai/responses when the task needs OpenAI Responses API features such as first-class reasoning models, reasoning summaries, URL citation annotations, or flat input mapping.
- Use
openai/codex when the task needs OpenAI Codex coding agent models (gpt-5.x-codex series) with ChatGPT access token authentication and encrypted reasoning content.
- Use
anthropic/messages for Claude and Anthropic extended thinking via WithThinking.
- Use
google/generativeai for Gemini chat, tool calling, vision, streaming, and Gemini reasoning.
- Use
openai/images for image generation (dall-e-2, dall-e-3, gpt-image-1) and image editing via the OpenAI Images API.
- Use
openai/embedding or google/embedding for embeddings. Keep embedding-provider work separate from chat-provider work.
Implementation Rules
Chat Providers
If adding or changing a chat provider, preserve the sdk.Provider contract:
Name()
ListModels(ctx)
Test(ctx)
TestModel(ctx, modelID)
DoGenerate(ctx, params)
DoStream(ctx, params)
Keep provider responsibilities focused:
- translate SDK messages/options into backend request format
- parse backend responses into
sdk.GenerateResult
- map backend streaming events into typed
sdk.StreamPart values
- report usage, finish reasons, reasoning, tool calls, sources, and files when supported
Embedding Providers
Embedding providers are separate from chat providers. Use sdk.EmbeddingProvider and return an sdk.EmbeddingModel via EmbeddingModel(id).
When updating embeddings:
- keep
sdk.Embed for single-string convenience
- keep
sdk.EmbedMany for batched requests
- preserve
Usage.Tokens
- only expose dimensions/task-type behavior when the backend supports it
Image Providers
Image providers are separate from chat, embedding, and speech providers. Use sdk.ImageGenerationProvider and/or sdk.ImageEditProvider.
When updating image providers:
- keep
sdk.GenerateImage for generation convenience
- keep
sdk.EditImage for editing convenience
- preserve
ImageUsage token details when the backend supports them
- support both multipart file upload and JSON reference modes for edit inputs
Tool Calling
Prefer sdk.NewTool[T] for new tool examples and integrations. It gives typed input and inferred JSON Schema.
Use these defaults unless the task requires something else:
WithToolChoice("auto") for normal use
WithMaxSteps(0) for inspection-only tool calls
WithMaxSteps(N) for automatic execution loops
RequireApproval: true only for sensitive side effects
When streaming with tools, ensure the implementation can emit:
- tool input construction parts
- tool execution parts
- progress updates
- denial/error events when applicable
MCP Tool Calling
Use MCP when the task needs remote tools exposed by an MCP server rather than locally implemented Execute handlers.
Default guidance:
- use
sdk.CreateMCPClient(ctx, &sdk.MCPClientConfig{...})
- use
sdk.MCPTransportHTTP for streamable HTTP MCP servers
- use
sdk.MCPTransportSSE only when the server exposes legacy SSE transport
- for stdio, build the transport with the official MCP Go SDK and pass
Transport: ...
- call
mcpClient.Tools(ctx) and pass the result into sdk.WithTools(...)
- call
defer mcpClient.Close() after successful creation
Important behavior:
- MCP tools become ordinary
sdk.Tool values from the caller's perspective
- Twilight AI converts MCP
InputSchema into *jsonschema.Schema
- MCP tool execution is delegated to
tools/call on the remote server
- remote MCP text output becomes the tool result visible to the model
Streaming
Twilight AI streaming is channel-first and type-safe. Prefer type switches over loosely typed event parsing.
Important expectations:
StreamText returns *sdk.StreamResult
sr.Stream must be consumed before relying on sr.Steps or sr.Messages
Text() and ToResult() are the convenience paths when callers do not want manual event handling
Messages And Results
Preserve the SDK message model and avoid backend-specific shapes leaking into public usage.
- user, assistant, system, and tool messages should stay in SDK types
- support rich parts where relevant: text, image, file, reasoning, tool call, tool result
- keep finish reason mapping aligned with SDK constants such as
stop, length, content-filter, and tool-calls
Common Task Patterns
Add A New Usage Example
Use this structure:
- pick the correct provider package
- create provider with explicit options
- create model via
ChatModel, EmbeddingModel, GenerationModel, or EditModel
- call the top-level
sdk function
- show minimal but idiomatic result handling
Add Or Update A Provider Feature
Check all affected layers:
- request mapping
- non-streaming response mapping
- streaming event mapping
- finish-reason and usage mapping
- reasoning/tool/source/file support if the backend exposes them
- model discovery and provider health checks if endpoints exist
Add A Custom Provider
Use the built-in providers as the template. A custom provider should feel identical to existing ones from the caller's perspective.
Minimum behavior:
- return a provider-bound model from
ChatModel
- implement discovery and health-check methods
- support
DoGenerate
- support
DoStream with correct lifecycle parts
Documentation Rules
When writing Twilight AI docs or README content:
- prefer provider-agnostic phrasing first, provider-specific details second
- use Go examples, not pseudocode, unless explaining an interface contract
- keep examples small and runnable in spirit
- mention exact package paths for imports
- explain when to choose Completions vs Responses vs Codex when OpenAI is involved
- keep embeddings, tool calling, and streaming as separate concerns unless the example truly combines them
Terminology
Use these terms consistently:
- Provider: backend implementation for chat generation
- Embedding provider: backend implementation for embeddings
- Image generation provider: backend implementation for image generation
- Image edit provider: backend implementation for image editing
- Model: provider-bound chat model
- Embedding model: provider-bound embedding model
- Image generation model: provider-bound image generation model
- Image edit model: provider-bound image edit model
- Tool calling: model requests a tool invocation
- Multi-step execution: automatic tool loop controlled by
WithMaxSteps
- Stream part: a typed event from
StreamText
Quick Checklist
Before finishing work in this repo, verify:
- the chosen provider package matches the intended backend capabilities
- chat, embedding, and image concerns are not mixed accidentally
- public examples use top-level
sdk APIs unless lower-level behavior is the point
- streaming logic uses typed
StreamPart handling
- tool-calling changes cover both inspection mode and multi-step mode when relevant
- MCP examples show both transport setup and normal
WithTools(...) usage when relevant
- provider work includes health checks or model discovery behavior if the backend supports them
Additional Resources
- For exported APIs, signatures, provider options, and stream/event types, see reference.md
Source: memohai/twilight-ai — distributed by TomeVault.
1---2name: twilight-ai3description: Assist with development in the Twilight AI Go SDK. Use when working in this repository, adding or updating providers, embeddings, tool calling, streaming, examples, or docs for Twilight AI. Use when this capability is needed.4---56# Twilight AI78## When To Use910Use this skill when the task involves `twilight-ai`, especially:1112- implementing or refactoring SDK APIs in `sdk/`13- adding or updating providers under `provider/`14- working on `GenerateText`, `GenerateTextResult`, `StreamText`, `Embed`, `EmbedMany`, `GenerateImage`, or `EditImage`15- adding tool-calling, streaming, reasoning, embedding, or image generation support16- writing examples, docs, or usage guidance for this library1718## Project Snapshot1920Twilight AI is a lightweight Go AI SDK with a provider-agnostic core API.2122- Text generation: `sdk.GenerateText`, `sdk.GenerateTextResult`, `sdk.StreamText`23- Image generation: `sdk.GenerateImage`, `sdk.EditImage`24- Embeddings: `sdk.Embed`, `sdk.EmbedMany`25- Tool calling: `sdk.Tool`, `sdk.NewTool[T]`, `WithMaxSteps`, approval flow26- MCP tool integration: `sdk.CreateMCPClient`, `sdk.MCPClient`, `sdk.MCPClientConfig`27- Streaming: typed `StreamPart` events over Go channels28- Current providers:29 - `provider/openai/completions`30 - `provider/openai/responses`31 - `provider/openai/codex`32 - `provider/openai/images`33 - `provider/anthropic/messages`34 - `provider/google/generativeai`35 - `provider/openai/embedding`36 - `provider/google/embedding`3738## Default Mental Model3940Prefer the high-level SDK API first, then drop to provider details only when needed.4142- `sdk.Model` binds a chat model to a `sdk.Provider`43- `sdk.EmbeddingModel` binds an embedding model to an `sdk.EmbeddingProvider`44- `sdk.ImageGenerationModel` binds an image generation model to an `sdk.ImageGenerationProvider`45- `sdk.ImageEditModel` binds an image edit model to an `sdk.ImageEditProvider`46- The client orchestrates tool loops, callbacks, approvals, and streaming lifecycle47- MCP clients can load remote MCP tools and turn them into ordinary `sdk.Tool` values48- Providers handle backend-specific HTTP, request mapping, response parsing, and SSE translation4950## Core API Guidance5152Choose the narrowest API that matches the task:5354- Need only final text: use `sdk.GenerateText`55- Need usage, finish reason, steps, sources, files, or tool details: use `sdk.GenerateTextResult`56- Need live output: use `sdk.StreamText`57- Need one vector: use `sdk.Embed`58- Need multiple vectors or embedding token usage: use `sdk.EmbedMany`59- Need image generation from a text prompt: use `sdk.GenerateImage`60- Need image editing or inpainting: use `sdk.EditImage`6162If the task introduces examples or docs, prefer simple end-to-end snippets that start with:63641. construct provider652. get model663. call SDK API674. handle error6869## Provider Selection Rules7071- Use `openai/completions` for broad OpenAI-compatible support such as DeepSeek, Groq, Ollama, Azure-style compatible endpoints, and generic `/chat/completions` backends.72- Use `openai/responses` when the task needs OpenAI Responses API features such as first-class reasoning models, reasoning summaries, URL citation annotations, or flat input mapping.73- Use `openai/codex` when the task needs OpenAI Codex coding agent models (gpt-5.x-codex series) with ChatGPT access token authentication and encrypted reasoning content.74- Use `anthropic/messages` for Claude and Anthropic extended thinking via `WithThinking`.75- Use `google/generativeai` for Gemini chat, tool calling, vision, streaming, and Gemini reasoning.76- Use `openai/images` for image generation (dall-e-2, dall-e-3, gpt-image-1) and image editing via the OpenAI Images API.77- Use `openai/embedding` or `google/embedding` for embeddings. Keep embedding-provider work separate from chat-provider work.7879## Implementation Rules8081### Chat Providers8283If adding or changing a chat provider, preserve the `sdk.Provider` contract:8485- `Name()`86- `ListModels(ctx)`87- `Test(ctx)`88- `TestModel(ctx, modelID)`89- `DoGenerate(ctx, params)`90- `DoStream(ctx, params)`9192Keep provider responsibilities focused:9394- translate SDK messages/options into backend request format95- parse backend responses into `sdk.GenerateResult`96- map backend streaming events into typed `sdk.StreamPart` values97- report usage, finish reasons, reasoning, tool calls, sources, and files when supported9899### Embedding Providers100101Embedding providers are separate from chat providers. Use `sdk.EmbeddingProvider` and return an `sdk.EmbeddingModel` via `EmbeddingModel(id)`.102103When updating embeddings:104105- keep `sdk.Embed` for single-string convenience106- keep `sdk.EmbedMany` for batched requests107- preserve `Usage.Tokens`108- only expose dimensions/task-type behavior when the backend supports it109110### Image Providers111112Image providers are separate from chat, embedding, and speech providers. Use `sdk.ImageGenerationProvider` and/or `sdk.ImageEditProvider`.113114When updating image providers:115116- keep `sdk.GenerateImage` for generation convenience117- keep `sdk.EditImage` for editing convenience118- preserve `ImageUsage` token details when the backend supports them119- support both multipart file upload and JSON reference modes for edit inputs120121### Tool Calling122123Prefer `sdk.NewTool[T]` for new tool examples and integrations. It gives typed input and inferred JSON Schema.124125Use these defaults unless the task requires something else:126127- `WithToolChoice("auto")` for normal use128- `WithMaxSteps(0)` for inspection-only tool calls129- `WithMaxSteps(N)` for automatic execution loops130- `RequireApproval: true` only for sensitive side effects131132When streaming with tools, ensure the implementation can emit:133134- tool input construction parts135- tool execution parts136- progress updates137- denial/error events when applicable138139### MCP Tool Calling140141Use MCP when the task needs remote tools exposed by an MCP server rather than locally implemented `Execute` handlers.142143Default guidance:144145- use `sdk.CreateMCPClient(ctx, &sdk.MCPClientConfig{...})`146- use `sdk.MCPTransportHTTP` for streamable HTTP MCP servers147- use `sdk.MCPTransportSSE` only when the server exposes legacy SSE transport148- for stdio, build the transport with the official MCP Go SDK and pass `Transport: ...`149- call `mcpClient.Tools(ctx)` and pass the result into `sdk.WithTools(...)`150- call `defer mcpClient.Close()` after successful creation151152Important behavior:153154- MCP tools become ordinary `sdk.Tool` values from the caller's perspective155- Twilight AI converts MCP `InputSchema` into `*jsonschema.Schema`156- MCP tool execution is delegated to `tools/call` on the remote server157- remote MCP text output becomes the tool result visible to the model158159### Streaming160161Twilight AI streaming is channel-first and type-safe. Prefer type switches over loosely typed event parsing.162163Important expectations:164165- `StreamText` returns `*sdk.StreamResult`166- `sr.Stream` must be consumed before relying on `sr.Steps` or `sr.Messages`167- `Text()` and `ToResult()` are the convenience paths when callers do not want manual event handling168169### Messages And Results170171Preserve the SDK message model and avoid backend-specific shapes leaking into public usage.172173- user, assistant, system, and tool messages should stay in SDK types174- support rich parts where relevant: text, image, file, reasoning, tool call, tool result175- keep finish reason mapping aligned with SDK constants such as `stop`, `length`, `content-filter`, and `tool-calls`176177## Common Task Patterns178179### Add A New Usage Example180181Use this structure:1821831. pick the correct provider package1842. create provider with explicit options1853. create model via `ChatModel`, `EmbeddingModel`, `GenerationModel`, or `EditModel`1864. call the top-level `sdk` function1875. show minimal but idiomatic result handling188189### Add Or Update A Provider Feature190191Check all affected layers:1921931. request mapping1942. non-streaming response mapping1953. streaming event mapping1964. finish-reason and usage mapping1975. reasoning/tool/source/file support if the backend exposes them1986. model discovery and provider health checks if endpoints exist199200### Add A Custom Provider201202Use the built-in providers as the template. A custom provider should feel identical to existing ones from the caller's perspective.203204Minimum behavior:2052061. return a provider-bound model from `ChatModel`2072. implement discovery and health-check methods2083. support `DoGenerate`2094. support `DoStream` with correct lifecycle parts210211## Documentation Rules212213When writing Twilight AI docs or README content:214215- prefer provider-agnostic phrasing first, provider-specific details second216- use Go examples, not pseudocode, unless explaining an interface contract217- keep examples small and runnable in spirit218- mention exact package paths for imports219- explain when to choose Completions vs Responses vs Codex when OpenAI is involved220- keep embeddings, tool calling, and streaming as separate concerns unless the example truly combines them221222## Terminology223224Use these terms consistently:225226- Provider: backend implementation for chat generation227- Embedding provider: backend implementation for embeddings228- Image generation provider: backend implementation for image generation229- Image edit provider: backend implementation for image editing230- Model: provider-bound chat model231- Embedding model: provider-bound embedding model232- Image generation model: provider-bound image generation model233- Image edit model: provider-bound image edit model234- Tool calling: model requests a tool invocation235- Multi-step execution: automatic tool loop controlled by `WithMaxSteps`236- Stream part: a typed event from `StreamText`237238## Quick Checklist239240Before finishing work in this repo, verify:241242- the chosen provider package matches the intended backend capabilities243- chat, embedding, and image concerns are not mixed accidentally244- public examples use top-level `sdk` APIs unless lower-level behavior is the point245- streaming logic uses typed `StreamPart` handling246- tool-calling changes cover both inspection mode and multi-step mode when relevant247- MCP examples show both transport setup and normal `WithTools(...)` usage when relevant248- provider work includes health checks or model discovery behavior if the backend supports them249250## Additional Resources251252- For exported APIs, signatures, provider options, and stream/event types, see [reference.md](reference.md)253254---255> Source: [memohai/twilight-ai](https://github.com/memohai/twilight-ai) — distributed by [TomeVault](https://tomevault.io).256<!-- tomevault:4.0:skill_md:2026-06-17 -->