@volter/twin-moonshot 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +202 -0
- package/README.md +164 -0
- package/dist/src/cli.d.ts +2 -0
- package/dist/src/cli.js +25 -0
- package/dist/src/index.d.ts +14 -0
- package/dist/src/index.js +86 -0
- package/dist/src/moonshot-budget.d.ts +57 -0
- package/dist/src/moonshot-budget.js +142 -0
- package/dist/src/moonshot-capabilities.d.ts +4 -0
- package/dist/src/moonshot-capabilities.js +1200 -0
- package/dist/src/moonshot-conformance.d.ts +14 -0
- package/dist/src/moonshot-conformance.js +405 -0
- package/dist/src/moonshot-connector.d.ts +168 -0
- package/dist/src/moonshot-connector.js +416 -0
- package/dist/src/moonshot-models.d.ts +36 -0
- package/dist/src/moonshot-models.js +37 -0
- package/dist/src/moonshot-scenario.d.ts +54 -0
- package/dist/src/moonshot-scenario.js +175 -0
- package/dist/src/moonshot-server.d.ts +13 -0
- package/dist/src/moonshot-server.js +202 -0
- package/dist/src/moonshot-stub.d.ts +70 -0
- package/dist/src/moonshot-stub.js +222 -0
- package/dist/src/moonshot-twin.d.ts +144 -0
- package/dist/src/moonshot-twin.js +1647 -0
- package/dist/src/moonshot-types.d.ts +251 -0
- package/dist/src/moonshot-types.js +19 -0
- package/package.json +53 -0
- package/src/cli.ts +25 -0
- package/src/index.ts +129 -0
- package/src/moonshot-budget.ts +163 -0
- package/src/moonshot-capabilities.ts +1220 -0
- package/src/moonshot-conformance.ts +416 -0
- package/src/moonshot-connector.ts +465 -0
- package/src/moonshot-models.ts +89 -0
- package/src/moonshot-scenario.ts +194 -0
- package/src/moonshot-server.ts +220 -0
- package/src/moonshot-stub.ts +230 -0
- package/src/moonshot-twin.ts +1670 -0
- package/src/moonshot-types.ts +225 -0
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
// Shared wire-shape types for the Moonshot (Kimi) API surface, transcribed from Moonshot's own
|
|
2
|
+
// OpenAPI 3.1.0 document (https://platform.kimi.ai/docs/openapi.json, read 2026-09-16) — the
|
|
3
|
+
// first-party artifact, not an SDK's internal types (no official Moonshot SDK exists; clients
|
|
4
|
+
// are the standard `openai` / `anthropic` SDKs pointed at Moonshot's base URLs, so THIS document
|
|
5
|
+
// is the only generated contract).
|
|
6
|
+
//
|
|
7
|
+
// MOONSHOT IS NOT OPENAI, and the differences below are load-bearing:
|
|
8
|
+
// • THREE inference protocols share one host: OpenAI-compatible `/v1/chat/completions` +
|
|
9
|
+
// `/v1/responses`, and an Anthropic-compatible `/anthropic/v1/messages` whose envelope,
|
|
10
|
+
// content blocks and SSE event grammar are Anthropic's, not OpenAI's;
|
|
11
|
+
// • chat responses carry `reasoning_content` (thinking mode) — not Groq's `reasoning`, not
|
|
12
|
+
// OpenAI's `refusal`;
|
|
13
|
+
// • `usage` carries `cached_tokens` (Moonshot's automatic context caching);
|
|
14
|
+
// • the error envelope is `{ error: { message, type, code? } }` (ErrorResponse schema);
|
|
15
|
+
// • the Messages surface answers `{ type:'error', error:{type,message}, request_id? }`
|
|
16
|
+
// (MessagesErrorResponse schema) — a DIFFERENT envelope on the same host.
|
|
17
|
+
|
|
18
|
+
// ── Chat Completions (/v1/chat/completions) ─────────────────────────────────────────────
|
|
19
|
+
/** A chat message param as the caller sends it (Message schema). Content is a string OR a
|
|
20
|
+
* content-part array (text / image_url / video_url — Moonshot's multimodal form). */
|
|
21
|
+
export type MoonshotMessageParam = {
|
|
22
|
+
role: 'system' | 'user' | 'assistant' | 'tool';
|
|
23
|
+
content?: string | Array<Record<string, unknown>> | null;
|
|
24
|
+
name?: string;
|
|
25
|
+
/** Partial Mode: set `partial: true` on the last ASSISTANT message to continue it. */
|
|
26
|
+
partial?: boolean;
|
|
27
|
+
tool_calls?: MoonshotToolCall[];
|
|
28
|
+
tool_call_id?: string;
|
|
29
|
+
/** kimi-k3's dynamic tool loading message: role 'system' with `tools` and NO content. */
|
|
30
|
+
tools?: unknown[];
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
/** A function tool_call (faithful shape). */
|
|
34
|
+
export type MoonshotToolCall = {
|
|
35
|
+
id: string;
|
|
36
|
+
type: 'function';
|
|
37
|
+
function: { name: string; arguments: string };
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
/** The chat `usage` object: token counts plus Moonshot's automatic context-cache split. */
|
|
41
|
+
export type MoonshotUsage = {
|
|
42
|
+
prompt_tokens: number;
|
|
43
|
+
completion_tokens: number;
|
|
44
|
+
total_tokens: number;
|
|
45
|
+
/** Tokens served from the automatic context cache (>256 prompt tokens to hit). */
|
|
46
|
+
cached_tokens?: number;
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
/** The assistant message Moonshot returns. `reasoning_content` is present when thinking mode
|
|
50
|
+
* is enabled (kimi-k3 always; kimi-k2.6/k2.7-code when `thinking.type` is 'enabled'). */
|
|
51
|
+
export type MoonshotAssistantMessage = {
|
|
52
|
+
role: 'assistant';
|
|
53
|
+
content: string | null;
|
|
54
|
+
tool_calls?: MoonshotToolCall[];
|
|
55
|
+
reasoning_content?: string | null;
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
export type MoonshotChoice = {
|
|
59
|
+
index: number;
|
|
60
|
+
message: MoonshotAssistantMessage;
|
|
61
|
+
logprobs?: unknown;
|
|
62
|
+
finish_reason: 'stop' | 'length' | 'tool_calls';
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
/** The unary chat.completion response envelope (faithful shape). */
|
|
66
|
+
export type MoonshotChatCompletion = {
|
|
67
|
+
id: string;
|
|
68
|
+
object: 'chat.completion';
|
|
69
|
+
created: number;
|
|
70
|
+
model: string;
|
|
71
|
+
choices: MoonshotChoice[];
|
|
72
|
+
usage: MoonshotUsage;
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
/** A streaming chat chunk (ChatCompletionChunk schema). */
|
|
76
|
+
export type MoonshotChatChunk = {
|
|
77
|
+
id: string;
|
|
78
|
+
object: 'chat.completion.chunk';
|
|
79
|
+
created: number;
|
|
80
|
+
model: string;
|
|
81
|
+
choices: Array<{
|
|
82
|
+
index: number;
|
|
83
|
+
delta: {
|
|
84
|
+
role?: string;
|
|
85
|
+
content?: string;
|
|
86
|
+
reasoning_content?: string;
|
|
87
|
+
tool_calls?: Array<{ index?: number; id?: string; type?: 'function'; function?: { name?: string; arguments?: string } }>;
|
|
88
|
+
};
|
|
89
|
+
finish_reason: 'stop' | 'length' | 'tool_calls' | null;
|
|
90
|
+
usage: MoonshotUsage | null;
|
|
91
|
+
}>;
|
|
92
|
+
/** The FINAL chunk carries the whole usage object (stream_options.include_usage / Moonshot's
|
|
93
|
+
* documented final-chunk usage); ordinary chunks carry null. */
|
|
94
|
+
usage?: MoonshotUsage | null;
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
// ── Models ──────────────────────────────────────────────────────────────────────────────
|
|
98
|
+
/** The served model object (OpenAI-shaped; Moonshot's /v1/models rows). */
|
|
99
|
+
export type MoonshotModelObject = {
|
|
100
|
+
id: string;
|
|
101
|
+
object: 'model';
|
|
102
|
+
created: number;
|
|
103
|
+
owned_by: string;
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
// ── Files ───────────────────────────────────────────────────────────────────────────────
|
|
107
|
+
/** File purposes — a CLOSED documented set (FileObject schema). */
|
|
108
|
+
export const FILE_PURPOSES = ['file-extract', 'image', 'video', 'batch'] as const;
|
|
109
|
+
export type MoonshotFilePurpose = (typeof FILE_PURPOSES)[number];
|
|
110
|
+
|
|
111
|
+
export type MoonshotFile = {
|
|
112
|
+
id: string;
|
|
113
|
+
object: 'file';
|
|
114
|
+
bytes: number;
|
|
115
|
+
created_at: number;
|
|
116
|
+
filename: string;
|
|
117
|
+
purpose: MoonshotFilePurpose;
|
|
118
|
+
status: string;
|
|
119
|
+
status_details?: string;
|
|
120
|
+
};
|
|
121
|
+
|
|
122
|
+
// ── Batches ─────────────────────────────────────────────────────────────────────────────
|
|
123
|
+
export type MoonshotBatchCounts = { completed: number; failed: number; total: number };
|
|
124
|
+
|
|
125
|
+
export type MoonshotBatch = {
|
|
126
|
+
id: string;
|
|
127
|
+
object: 'batch';
|
|
128
|
+
endpoint: string;
|
|
129
|
+
input_file_id: string;
|
|
130
|
+
completion_window: string;
|
|
131
|
+
status: 'validating' | 'failed' | 'in_progress' | 'finalizing' | 'completed' | 'expired' | 'cancelling' | 'cancelled';
|
|
132
|
+
output_file_id: string | null;
|
|
133
|
+
error_file_id: string | null;
|
|
134
|
+
created_at: number;
|
|
135
|
+
in_progress_at: number | null;
|
|
136
|
+
expires_at: number | null;
|
|
137
|
+
finalizing_at: number | null;
|
|
138
|
+
completed_at: number | null;
|
|
139
|
+
failed_at: number | null;
|
|
140
|
+
cancelling_at: number | null;
|
|
141
|
+
cancelled_at: number | null;
|
|
142
|
+
request_counts: MoonshotBatchCounts;
|
|
143
|
+
metadata: Record<string, string> | null;
|
|
144
|
+
};
|
|
145
|
+
|
|
146
|
+
// ── Errors ──────────────────────────────────────────────────────────────────────────────
|
|
147
|
+
/**
|
|
148
|
+
* Moonshot's OpenAI-surface error envelope (ErrorResponse schema): `error.message` REQUIRED,
|
|
149
|
+
* `type` and `code` optional. The `type` values are Moonshot's documented error-code page
|
|
150
|
+
* (platform.kimi.ai/docs/api/errors, read 2026-09-16) — see moonshot-twin.ts for the mapping.
|
|
151
|
+
*/
|
|
152
|
+
export type MoonshotError = {
|
|
153
|
+
error: {
|
|
154
|
+
message: string;
|
|
155
|
+
type?: string;
|
|
156
|
+
code?: string;
|
|
157
|
+
};
|
|
158
|
+
};
|
|
159
|
+
|
|
160
|
+
// ── Anthropic-compatible Messages (/anthropic/v1/messages) ──────────────────────────────
|
|
161
|
+
/** A Messages content block the RESPONSE emits, ordered thinking → text → tool_use. */
|
|
162
|
+
export type MoonshotMessagesBlock =
|
|
163
|
+
| { type: 'thinking'; thinking: string; signature?: string }
|
|
164
|
+
| { type: 'text'; text: string }
|
|
165
|
+
| { type: 'tool_use'; id: string; name: string; input: Record<string, unknown> };
|
|
166
|
+
|
|
167
|
+
export type MoonshotMessagesResponse = {
|
|
168
|
+
id: string;
|
|
169
|
+
type: 'message';
|
|
170
|
+
role: 'assistant';
|
|
171
|
+
model: string;
|
|
172
|
+
content: MoonshotMessagesBlock[];
|
|
173
|
+
stop_reason: 'end_turn' | 'max_tokens' | 'stop_sequence' | 'tool_use' | 'refusal' | null;
|
|
174
|
+
stop_sequence: string | null;
|
|
175
|
+
usage: {
|
|
176
|
+
input_tokens: number;
|
|
177
|
+
output_tokens: number;
|
|
178
|
+
cache_read_input_tokens?: number;
|
|
179
|
+
cache_creation_input_tokens?: number;
|
|
180
|
+
};
|
|
181
|
+
};
|
|
182
|
+
|
|
183
|
+
/** A Messages SSE event. `data` is the JSON payload; there is no [DONE] sentinel — the stream
|
|
184
|
+
* ends after `message_stop` (Anthropic's grammar, which this surface follows). */
|
|
185
|
+
export type MessagesSseEvent = { event?: string; data?: Record<string, unknown>; done?: boolean };
|
|
186
|
+
|
|
187
|
+
// ── Responses (/v1/responses) ───────────────────────────────────────────────────────────
|
|
188
|
+
/** An output item, ordered web_search_call → reasoning → message → tool calls. */
|
|
189
|
+
export type MoonshotResponsesOutputItem =
|
|
190
|
+
| { type: 'reasoning'; id: string; summary: Array<{ type: 'summary_text'; text: string }>; status?: 'completed' }
|
|
191
|
+
| { type: 'message'; id: string; role: 'assistant'; status: 'completed'; content: Array<{ type: 'output_text'; text: string; annotations: unknown[] }> }
|
|
192
|
+
| { type: 'function_call'; id: string; call_id: string; name: string; arguments: string; status?: 'completed' }
|
|
193
|
+
| { type: 'custom_tool_call'; id: string; call_id: string; name: string; input: string }
|
|
194
|
+
| { type: 'web_search_call'; id: string; status: 'completed' };
|
|
195
|
+
|
|
196
|
+
export type MoonshotResponsesUsage = {
|
|
197
|
+
input_tokens: number;
|
|
198
|
+
input_tokens_details?: { cached_tokens: number; cache_write_tokens: number };
|
|
199
|
+
output_tokens: number;
|
|
200
|
+
output_tokens_details?: { reasoning_tokens: number };
|
|
201
|
+
total_tokens: number;
|
|
202
|
+
};
|
|
203
|
+
|
|
204
|
+
export type MoonshotResponsesResponse = {
|
|
205
|
+
id: string;
|
|
206
|
+
object: 'response';
|
|
207
|
+
created_at: number;
|
|
208
|
+
completed_at: number | null;
|
|
209
|
+
status: 'in_progress' | 'completed' | 'incomplete' | 'failed';
|
|
210
|
+
model: string;
|
|
211
|
+
output: MoonshotResponsesOutputItem[];
|
|
212
|
+
usage: MoonshotResponsesUsage | null;
|
|
213
|
+
incomplete_details: { reason: 'max_output_tokens' | 'content_filter' } | null;
|
|
214
|
+
error: { code: string; message: string } | null;
|
|
215
|
+
store: false;
|
|
216
|
+
};
|
|
217
|
+
|
|
218
|
+
// ── Streaming ───────────────────────────────────────────────────────────────────────────
|
|
219
|
+
/** A single Server-Sent Event the OpenAI-compatible streaming paths emit. `data` is the JSON
|
|
220
|
+
* payload; `[DONE]` is signalled with `done: true` (no data object). */
|
|
221
|
+
export type SseEvent = { data?: Record<string, unknown>; done?: boolean };
|
|
222
|
+
|
|
223
|
+
/** A sink the streaming path writes events into (an injected collector in tests / a real
|
|
224
|
+
* HTTP SSE writer in the server). NO real sockets or setTimeout in the handler. */
|
|
225
|
+
export type SseSink = (event: SseEvent) => void;
|