Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/Aquaticat/Monochromatic. TypeScript and Rust monorepo: Claude Code and Pi coding-agent plugins, a m
Something went wrong. Try again.
2 folders · 5 files
README.md
module-llm-type #
Ready to publish.
Shared OpenAI-compatible LLM client types.
The canonical home for the chat-completion shapes that recur across every package wrapping an OpenAI-compatible endpoint (openai,
openrouter,
anthropic-compatible,
local llama-server),
so the role / content envelope and the response shape are defined once and composed per consumer.
Exports #
CHAT_ROLES: runtime tuple['system', 'user', 'assistant']; the source of truthChatRolederives from.ChatRole: union of valid author roles, derived fromCHAT_ROLES.ChatMessage: plain-text message envelope (role+ stringcontent).ContentPart: multimodal content part (text run or image URL) for vision requests.ChatCompletionChoice,ChatCompletionResponse: non-streaming response shapes.CompletionUsage: prompt and completion token counts.
Usage #
// text chat
import type {
ChatCompletionResponse,
ChatMessage,
} from '@monochromatic-dev/module-llm-type';
const messages: readonly ChatMessage[] = [
{ role: 'system', content: 'You are concise.', },
{ role: 'user', content: 'Summarise this.', },
];
const data = (await response.json()) as ChatCompletionResponse;
const text = data.choices[0]?.message.content ?? '';
// vision chat: compose over ContentPart instead of redeclaring the envelope
import type {
ChatRole,
ContentPart,
} from '@monochromatic-dev/module-llm-type';
type VisionMessage = {
readonly role: Extract<ChatRole, 'user'>;
readonly content: readonly ContentPart[];
};
// usage-aware response: intersect rather than redeclare
import type {
ChatCompletionResponse,
CompletionUsage,
} from '@monochromatic-dev/module-llm-type';
type CompletionResponse = ChatCompletionResponse & { readonly usage: CompletionUsage; };
Design decisions #
- Text-first envelope.
ChatMessage.contentis astring. Multimodal callers compose a message overContentPartarrays so the common text path never branches on content type. - Response superset.
Every field past
choices[].message.contentis optional, so a consumer's uncheckedas ChatCompletionResponsecast keeps compiling and content reads stay non-nullstring. - Snake_case usage fields.
CompletionUsagemirrors the wire format (prompt_tokens,completion_tokens) so a parsed body satisfies the type without remapping. - Roles derive from a runtime tuple.
ChatRole = typeof CHAT_ROLES[number]keeps the union and the validation array in one place, and gives the build a non-empty bundle.
Out of scope #
- Streaming chunk types.
No consumer streams through a shared type today;
inference-canarystreams via the OpenAI SDK's own types. AddChatCompletionChunkwhen a second streaming consumer appears. - A single client implementation. Each consumer keeps its own openai / anthropic / fetch client.
paper2vn. It lives underpackage-paused/(not a workspace member), so it cannot resolve aworkspace:*dependency. When un-paused it should adopt these types, composing a nullable variant (content: string | null) for its response.