@apicity/kimicoding 0.1.0-alpha.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 +21 -0
- package/README.md +119 -0
- package/dist/src/example.d.ts +8 -0
- package/dist/src/example.d.ts.map +1 -0
- package/dist/src/example.js +107 -0
- package/dist/src/example.js.map +1 -0
- package/dist/src/index.d.ts +7 -0
- package/dist/src/index.d.ts.map +1 -0
- package/dist/src/index.js +5 -0
- package/dist/src/index.js.map +1 -0
- package/dist/src/kimicoding.d.ts +6 -0
- package/dist/src/kimicoding.d.ts.map +1 -0
- package/dist/src/kimicoding.js +260 -0
- package/dist/src/kimicoding.js.map +1 -0
- package/dist/src/middleware.d.ts +36 -0
- package/dist/src/middleware.d.ts.map +1 -0
- package/dist/src/middleware.js +302 -0
- package/dist/src/middleware.js.map +1 -0
- package/dist/src/sse.d.ts +6 -0
- package/dist/src/sse.d.ts.map +1 -0
- package/dist/src/sse.js +55 -0
- package/dist/src/sse.js.map +1 -0
- package/dist/src/types.d.ts +117 -0
- package/dist/src/types.d.ts.map +1 -0
- package/dist/src/types.js +11 -0
- package/dist/src/types.js.map +1 -0
- package/dist/src/zod.d.ts +628 -0
- package/dist/src/zod.d.ts.map +1 -0
- package/dist/src/zod.js +95 -0
- package/dist/src/zod.js.map +1 -0
- package/package.json +62 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Justin Tanner
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
# @apicity/kimicoding
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/@apicity/kimicoding)
|
|
4
|
+
[](package.json)
|
|
5
|
+
[](tsconfig.json)
|
|
6
|
+
|
|
7
|
+
Kimi for Coding provider for Apicity - completely standalone with Anthropic Messages API format, streaming-first, and built-in middleware.
|
|
8
|
+
|
|
9
|
+
## Installation
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
npm install @apicity/kimicoding
|
|
13
|
+
# or
|
|
14
|
+
pnpm add @apicity/kimicoding
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## Quick Start
|
|
18
|
+
|
|
19
|
+
```typescript
|
|
20
|
+
import { kimicoding as createKimicoding } from "@apicity/kimicoding";
|
|
21
|
+
|
|
22
|
+
const kimicoding = createKimicoding({ apiKey: process.env.KIMICODING_API_KEY! });
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
## API Reference
|
|
26
|
+
|
|
27
|
+
5 endpoints across 1 group. Each method mirrors an upstream URL path.
|
|
28
|
+
|
|
29
|
+
### coding
|
|
30
|
+
|
|
31
|
+
<details>
|
|
32
|
+
<summary><code>GET</code> <b><code>kimicoding.coding.v1.models</code></b></summary>
|
|
33
|
+
|
|
34
|
+
<code>GET https://api.kimi.com/coding/v1/models</code>
|
|
35
|
+
|
|
36
|
+
[Upstream docs ↗](https://platform.moonshot.ai/docs)
|
|
37
|
+
|
|
38
|
+
```typescript
|
|
39
|
+
const res = await kimicoding.coding.v1.models({ /* ... */ });
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
Source: [`packages/provider/kimicoding/src/kimicoding.ts`](src/kimicoding.ts)
|
|
43
|
+
|
|
44
|
+
</details>
|
|
45
|
+
|
|
46
|
+
<details>
|
|
47
|
+
<summary><code>POST</code> <b><code>kimicoding.coding.v1.countTokens</code></b></summary>
|
|
48
|
+
|
|
49
|
+
<code>POST https://api.kimi.com/coding/v1/tokens/count</code>
|
|
50
|
+
|
|
51
|
+
[Upstream docs ↗](https://platform.moonshot.ai/docs)
|
|
52
|
+
|
|
53
|
+
```typescript
|
|
54
|
+
const res = await kimicoding.coding.v1.countTokens({ /* ... */ });
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
Source: [`packages/provider/kimicoding/src/kimicoding.ts`](src/kimicoding.ts)
|
|
58
|
+
|
|
59
|
+
</details>
|
|
60
|
+
|
|
61
|
+
<details>
|
|
62
|
+
<summary><code>POST</code> <b><code>kimicoding.coding.v1.embeddings</code></b></summary>
|
|
63
|
+
|
|
64
|
+
<code>POST https://api.kimi.com/coding/v1/embeddings</code>
|
|
65
|
+
|
|
66
|
+
[Upstream docs ↗](https://platform.moonshot.ai/docs)
|
|
67
|
+
|
|
68
|
+
```typescript
|
|
69
|
+
const res = await kimicoding.coding.v1.embeddings({ /* ... */ });
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
Source: [`packages/provider/kimicoding/src/kimicoding.ts`](src/kimicoding.ts)
|
|
73
|
+
|
|
74
|
+
</details>
|
|
75
|
+
|
|
76
|
+
<details>
|
|
77
|
+
<summary><code>POST</code> <b><code>kimicoding.coding.v1.messages</code></b></summary>
|
|
78
|
+
|
|
79
|
+
<code>POST https://api.kimi.com/coding/v1/messages</code>
|
|
80
|
+
|
|
81
|
+
[Upstream docs ↗](https://platform.moonshot.ai/docs)
|
|
82
|
+
|
|
83
|
+
```typescript
|
|
84
|
+
const res = await kimicoding.coding.v1.messages({ /* ... */ });
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
Source: [`packages/provider/kimicoding/src/kimicoding.ts`](src/kimicoding.ts)
|
|
88
|
+
|
|
89
|
+
</details>
|
|
90
|
+
|
|
91
|
+
<details>
|
|
92
|
+
<summary><code>POST</code> <b><code>kimicoding.coding.v1.messages</code></b></summary>
|
|
93
|
+
|
|
94
|
+
<code>POST https://api.kimi.com/coding/v1/messages</code>
|
|
95
|
+
|
|
96
|
+
[Upstream docs ↗](https://platform.moonshot.ai/docs)
|
|
97
|
+
|
|
98
|
+
```typescript
|
|
99
|
+
const res = await kimicoding.coding.v1.messages({ /* ... */ });
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
Source: [`packages/provider/kimicoding/src/kimicoding.ts`](src/kimicoding.ts)
|
|
103
|
+
|
|
104
|
+
</details>
|
|
105
|
+
|
|
106
|
+
## Middleware
|
|
107
|
+
|
|
108
|
+
```typescript
|
|
109
|
+
import { kimicoding as createKimicoding, withRetry } from "@apicity/kimicoding";
|
|
110
|
+
|
|
111
|
+
const kimicoding = createKimicoding({ apiKey: process.env.KIMICODING_API_KEY! });
|
|
112
|
+
const models = withRetry(kimicoding.get.v1.models, { retries: 3 });
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
Part of the [apicity](https://github.com/justintanner/apicity) monorepo.
|
|
116
|
+
|
|
117
|
+
## License
|
|
118
|
+
|
|
119
|
+
MIT — see [LICENSE](LICENSE).
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"example.d.ts","sourceRoot":"","sources":["../../src/example.ts"],"names":[],"mappings":"AAOA,MAAM,WAAW,eAAe;IAC9B,MAAM,EAAE,MAAM,CAAC;IACf,OAAO,EAAE,OAAO,CAAC;CAClB;AAED,QAAA,MAAM,QAAQ,EAAE,MAAM,CAAC,MAAM,EAAE,eAAe,CAsB7C,CAAC;AAEF,eAAe,QAAQ,CAAC;AAOxB,wBAAgB,cAAc,CAAC,CAAC,EAAE,QAAQ,EAAE,CAAC,GAAG,CAAC,CA6ChD"}
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
// Auto-generated by `pnpm run gen:examples` — do not edit by hand.
|
|
2
|
+
// Source: tests/recordings/<provider>_*/<test>_*/recording.har
|
|
3
|
+
//
|
|
4
|
+
// Each entry is the green-path payload for one endpoint, mined from a real
|
|
5
|
+
// integration-test recording. `attachExamples` walks the provider tree and
|
|
6
|
+
// hangs the matching entry off each endpoint function as `.example`.
|
|
7
|
+
const EXAMPLES = {
|
|
8
|
+
"POST coding.v1.embeddings": {
|
|
9
|
+
"source": "kimicoding/embeddings-hello",
|
|
10
|
+
"payload": {
|
|
11
|
+
"model": "k2p5",
|
|
12
|
+
"input": "Hello world"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"POST coding.v1.messages": {
|
|
16
|
+
"source": "kimicoding/chat-hi",
|
|
17
|
+
"payload": {
|
|
18
|
+
"model": "k2p5",
|
|
19
|
+
"max_tokens": 32768,
|
|
20
|
+
"messages": [
|
|
21
|
+
{
|
|
22
|
+
"role": "user",
|
|
23
|
+
"content": "hi"
|
|
24
|
+
}
|
|
25
|
+
],
|
|
26
|
+
"temperature": 0
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
export default EXAMPLES;
|
|
31
|
+
// Walks each "<METHOD> <dotPath>" key onto the provider's tree. Tries the
|
|
32
|
+
// standard `provider.<method>.<dotPath>` shape first, then a few fallbacks
|
|
33
|
+
// to cover providers with non-standard layouts (fal's `.run.` namespace,
|
|
34
|
+
// kie's sub-providers, `free`'s flat root). Returns the same provider for
|
|
35
|
+
// drop-in use as `return attachExamples({ ... });`.
|
|
36
|
+
export function attachExamples(provider) {
|
|
37
|
+
const root = provider;
|
|
38
|
+
const HTTP_KEYS = new Set(["post", "get", "put", "delete", "patch", "head"]);
|
|
39
|
+
for (const [key, example] of Object.entries(EXAMPLES)) {
|
|
40
|
+
const sp = key.indexOf(" ");
|
|
41
|
+
if (sp < 0)
|
|
42
|
+
continue;
|
|
43
|
+
const method = key.slice(0, sp).toLowerCase();
|
|
44
|
+
const segs = key.slice(sp + 1).split(".");
|
|
45
|
+
const candidates = [
|
|
46
|
+
root[method],
|
|
47
|
+
root[method]?.run,
|
|
48
|
+
root,
|
|
49
|
+
];
|
|
50
|
+
if (segs.length > 1) {
|
|
51
|
+
const sub = root[segs[0]];
|
|
52
|
+
if (sub && typeof sub === "object") {
|
|
53
|
+
const subMethod = sub[method];
|
|
54
|
+
if (subMethod)
|
|
55
|
+
candidates.push({ __nested: subMethod, __segs: segs.slice(1) });
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
let attached = false;
|
|
59
|
+
for (const c of candidates) {
|
|
60
|
+
const fn = walkToFn(c, segs);
|
|
61
|
+
if (fn) {
|
|
62
|
+
Object.assign(fn, { example });
|
|
63
|
+
attached = true;
|
|
64
|
+
break;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
if (attached)
|
|
68
|
+
continue;
|
|
69
|
+
for (const k of Object.keys(root)) {
|
|
70
|
+
if (HTTP_KEYS.has(k))
|
|
71
|
+
continue;
|
|
72
|
+
if (!segs.includes(k))
|
|
73
|
+
continue;
|
|
74
|
+
const sub = root[k];
|
|
75
|
+
if (!sub || typeof sub !== "object")
|
|
76
|
+
continue;
|
|
77
|
+
const subMethod = sub[method];
|
|
78
|
+
if (!subMethod)
|
|
79
|
+
continue;
|
|
80
|
+
const fn = walkToFn(subMethod, segs);
|
|
81
|
+
if (fn) {
|
|
82
|
+
Object.assign(fn, { example });
|
|
83
|
+
break;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
return provider;
|
|
88
|
+
}
|
|
89
|
+
function walkToFn(start, segs) {
|
|
90
|
+
if (start && typeof start === "object" && "__nested" in start) {
|
|
91
|
+
const wrapper = start;
|
|
92
|
+
return walkToFn(wrapper.__nested, wrapper.__segs);
|
|
93
|
+
}
|
|
94
|
+
let cur = start;
|
|
95
|
+
for (const seg of segs) {
|
|
96
|
+
if (cur === null || cur === undefined)
|
|
97
|
+
return null;
|
|
98
|
+
const t = typeof cur;
|
|
99
|
+
if (t !== "object" && t !== "function")
|
|
100
|
+
return null;
|
|
101
|
+
cur = cur[seg];
|
|
102
|
+
}
|
|
103
|
+
return typeof cur === "function"
|
|
104
|
+
? cur
|
|
105
|
+
: null;
|
|
106
|
+
}
|
|
107
|
+
//# sourceMappingURL=example.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"example.js","sourceRoot":"","sources":["../../src/example.ts"],"names":[],"mappings":"AAAA,mEAAmE;AACnE,+DAA+D;AAC/D,EAAE;AACF,2EAA2E;AAC3E,2EAA2E;AAC3E,qEAAqE;AAOrE,MAAM,QAAQ,GAAoC;IAChD,2BAA2B,EAAE;QAC3B,QAAQ,EAAE,6BAA6B;QACvC,SAAS,EAAE;YACT,OAAO,EAAE,MAAM;YACf,OAAO,EAAE,aAAa;SACvB;KACF;IACD,yBAAyB,EAAE;QACzB,QAAQ,EAAE,oBAAoB;QAC9B,SAAS,EAAE;YACT,OAAO,EAAE,MAAM;YACf,YAAY,EAAE,KAAK;YACnB,UAAU,EAAE;gBACV;oBACE,MAAM,EAAE,MAAM;oBACd,SAAS,EAAE,IAAI;iBAChB;aACF;YACD,aAAa,EAAE,CAAC;SACjB;KACF;CACF,CAAC;AAEF,eAAe,QAAQ,CAAC;AAExB,0EAA0E;AAC1E,2EAA2E;AAC3E,yEAAyE;AACzE,0EAA0E;AAC1E,oDAAoD;AACpD,MAAM,UAAU,cAAc,CA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,CAAC;IACzB,KAAK,MAAM,GAAG,IAAI,IAAI,EAAE,CAAC;QACvB,IAAI,GAAG,KAAK,IAAI,IAAI,GAAG,KAAK,SAAS;YAAE,OAAO,IAAI,CAAC;QACnD,MAAM,CAAC,GAAG,OAAO,GAAG,CAAC;QACrB,IAAI,CAAC,KAAK,QAAQ,IAAI,CAAC,KAAK,UAAU;YAAE,OAAO,IAAI,CAAC;QACpD,GAAG,GAAI,GAA+B,CAAC,GAAG,CAAC,CAAC;IAC9C,CAAC;IACD,OAAO,OAAO,GAAG,KAAK,UAAU;QAC9B,CAAC,CAAE,GAAuC;QAC1C,CAAC,CAAC,IAAI,CAAC;AACX,CAAC"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export { kimicoding, textBlock, imageBase64, imageUrl } from "./kimicoding";
|
|
2
|
+
export { KimiCodingError } from "./types";
|
|
3
|
+
export { withRetry, withFallback, withStreamRetry, withStreamFallback, withRateLimit, createRateLimiter, } from "./middleware";
|
|
4
|
+
export type { RetryOptions, FallbackOptions, RateLimiterOptions, RateLimiter, RateLimitOptions, } from "./middleware";
|
|
5
|
+
export { sseToIterable } from "./sse";
|
|
6
|
+
export type { ChatRequest, ChatMessage, AnthropicMessage, AnthropicStreamEvent, AnthropicContentBlock, EmbeddingRequest, EmbeddingResponse, EmbeddingData, CountTokensRequest, CountTokensResponse, Provider, KimiCodingOptions, KimiCodingProvider, ContentBlock, TextContentBlock, ImageContentBlock, ImageSource, Base64ImageSource, UrlImageSource, MessageContent, Role, KimiCodingModel, KimiCodingModelListResponse, } from "./types";
|
|
7
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,SAAS,EAAE,WAAW,EAAE,QAAQ,EAAE,MAAM,cAAc,CAAC;AAE5E,OAAO,EAAE,eAAe,EAAE,MAAM,SAAS,CAAC;AAE1C,OAAO,EACL,SAAS,EACT,YAAY,EACZ,eAAe,EACf,kBAAkB,EAClB,aAAa,EACb,iBAAiB,GAClB,MAAM,cAAc,CAAC;AAEtB,YAAY,EACV,YAAY,EACZ,eAAe,EACf,kBAAkB,EAClB,WAAW,EACX,gBAAgB,GACjB,MAAM,cAAc,CAAC;AAEtB,OAAO,EAAE,aAAa,EAAE,MAAM,OAAO,CAAC;AAEtC,YAAY,EACV,WAAW,EACX,WAAW,EACX,gBAAgB,EAChB,oBAAoB,EACpB,qBAAqB,EACrB,gBAAgB,EAChB,iBAAiB,EACjB,aAAa,EACb,kBAAkB,EAClB,mBAAmB,EACnB,QAAQ,EACR,iBAAiB,EACjB,kBAAkB,EAClB,YAAY,EACZ,gBAAgB,EAChB,iBAAiB,EACjB,WAAW,EACX,iBAAiB,EACjB,cAAc,EACd,cAAc,EACd,IAAI,EACJ,eAAe,EACf,2BAA2B,GAC5B,MAAM,SAAS,CAAC"}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { kimicoding, textBlock, imageBase64, imageUrl } from "./kimicoding.js";
|
|
2
|
+
export { KimiCodingError } from "./types.js";
|
|
3
|
+
export { withRetry, withFallback, withStreamRetry, withStreamFallback, withRateLimit, createRateLimiter, } from "./middleware.js";
|
|
4
|
+
export { sseToIterable } from "./sse.js";
|
|
5
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,SAAS,EAAE,WAAW,EAAE,QAAQ,EAAE,MAAM,cAAc,CAAC;AAE5E,OAAO,EAAE,eAAe,EAAE,MAAM,SAAS,CAAC;AAE1C,OAAO,EACL,SAAS,EACT,YAAY,EACZ,eAAe,EACf,kBAAkB,EAClB,aAAa,EACb,iBAAiB,GAClB,MAAM,cAAc,CAAC;AAUtB,OAAO,EAAE,aAAa,EAAE,MAAM,OAAO,CAAC"}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { TextContentBlock, ImageContentBlock, KimiCodingOptions, KimiCodingProvider } from "./types";
|
|
2
|
+
export declare function textBlock(text: string): TextContentBlock;
|
|
3
|
+
export declare function imageBase64(data: string, mediaType: "image/jpeg" | "image/png" | "image/gif" | "image/webp"): ImageContentBlock;
|
|
4
|
+
export declare function imageUrl(url: string): ImageContentBlock;
|
|
5
|
+
export declare function kimicoding(opts: KimiCodingOptions): KimiCodingProvider;
|
|
6
|
+
//# sourceMappingURL=kimicoding.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"kimicoding.d.ts","sourceRoot":"","sources":["../../src/kimicoding.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,gBAAgB,EAChB,iBAAiB,EACjB,iBAAiB,EAEjB,kBAAkB,EAQnB,MAAM,SAAS,CAAC;AAsCjB,wBAAgB,SAAS,CAAC,IAAI,EAAE,MAAM,GAAG,gBAAgB,CAExD;AAED,wBAAgB,WAAW,CACzB,IAAI,EAAE,MAAM,EACZ,SAAS,EAAE,YAAY,GAAG,WAAW,GAAG,WAAW,GAAG,YAAY,GACjE,iBAAiB,CAKnB;AAED,wBAAgB,QAAQ,CAAC,GAAG,EAAE,MAAM,GAAG,iBAAiB,CAEvD;AAED,wBAAgB,UAAU,CAAC,IAAI,EAAE,iBAAiB,GAAG,kBAAkB,CAmRtE"}
|
|
@@ -0,0 +1,260 @@
|
|
|
1
|
+
import { KimiCodingError, } from "./types.js";
|
|
2
|
+
import { sseToIterable } from "./sse.js";
|
|
3
|
+
import { ChatRequestSchema, EmbeddingRequestSchema, CountTokensRequestSchema, } from "./zod.js";
|
|
4
|
+
import { attachExamples } from "./example.js";
|
|
5
|
+
// Helper function to safely handle AbortSignal across different environments
|
|
6
|
+
function attachAbortHandler(signal, controller) {
|
|
7
|
+
if (!signal)
|
|
8
|
+
return;
|
|
9
|
+
// Handle both standard AbortSignal and node-fetch's AbortSignal
|
|
10
|
+
if (typeof signal.addEventListener === "function") {
|
|
11
|
+
signal.addEventListener("abort", () => controller.abort(), { once: true });
|
|
12
|
+
}
|
|
13
|
+
else if (signal.aborted) {
|
|
14
|
+
// Already aborted, abort our controller too
|
|
15
|
+
controller.abort();
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
function isAnthropicErrorBody(x) {
|
|
19
|
+
return (typeof x === "object" &&
|
|
20
|
+
x !== null &&
|
|
21
|
+
"error" in x &&
|
|
22
|
+
typeof x.error === "object");
|
|
23
|
+
}
|
|
24
|
+
export function textBlock(text) {
|
|
25
|
+
return { type: "text", text };
|
|
26
|
+
}
|
|
27
|
+
export function imageBase64(data, mediaType) {
|
|
28
|
+
return {
|
|
29
|
+
type: "image",
|
|
30
|
+
source: { type: "base64", media_type: mediaType, data },
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
export function imageUrl(url) {
|
|
34
|
+
return { type: "image", source: { type: "url", url } };
|
|
35
|
+
}
|
|
36
|
+
export function kimicoding(opts) {
|
|
37
|
+
const baseURL = opts.baseURL ?? "https://api.kimi.com/coding/";
|
|
38
|
+
const doFetch = opts.fetch ?? fetch;
|
|
39
|
+
const timeout = opts.timeout ?? 30000;
|
|
40
|
+
function buildHeaders() {
|
|
41
|
+
return {
|
|
42
|
+
Authorization: `Bearer ${opts.apiKey}`,
|
|
43
|
+
"x-api-key": opts.apiKey,
|
|
44
|
+
"Content-Type": "application/json",
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
async function makeGetRequest(path, signal) {
|
|
48
|
+
const controller = new AbortController();
|
|
49
|
+
const timeoutId = setTimeout(() => controller.abort(), timeout);
|
|
50
|
+
if (signal) {
|
|
51
|
+
attachAbortHandler(signal, controller);
|
|
52
|
+
}
|
|
53
|
+
try {
|
|
54
|
+
const res = await doFetch(`${baseURL}${path}`, {
|
|
55
|
+
method: "GET",
|
|
56
|
+
headers: {
|
|
57
|
+
Authorization: `Bearer ${opts.apiKey}`,
|
|
58
|
+
"x-api-key": opts.apiKey,
|
|
59
|
+
},
|
|
60
|
+
signal: controller.signal,
|
|
61
|
+
});
|
|
62
|
+
clearTimeout(timeoutId);
|
|
63
|
+
if (!res.ok) {
|
|
64
|
+
let message = `KimiCoding error: ${res.status}`;
|
|
65
|
+
let body = null;
|
|
66
|
+
try {
|
|
67
|
+
body = await res.json();
|
|
68
|
+
if (isAnthropicErrorBody(body) &&
|
|
69
|
+
typeof body.error?.message === "string") {
|
|
70
|
+
message = `KimiCoding error ${res.status}: ${body.error.message}`;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
catch {
|
|
74
|
+
// ignore parse errors
|
|
75
|
+
}
|
|
76
|
+
throw new KimiCodingError(message, res.status, body);
|
|
77
|
+
}
|
|
78
|
+
return (await res.json());
|
|
79
|
+
}
|
|
80
|
+
catch (error) {
|
|
81
|
+
clearTimeout(timeoutId);
|
|
82
|
+
if (error instanceof KimiCodingError)
|
|
83
|
+
throw error;
|
|
84
|
+
throw new KimiCodingError(`KimiCoding request failed: ${error}`, 500);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
// POST https://api.kimi.com/coding/v1/messages
|
|
88
|
+
// Docs: https://platform.moonshot.ai/docs
|
|
89
|
+
async function* streamImpl(req, signal) {
|
|
90
|
+
const controller = new AbortController();
|
|
91
|
+
const timeoutId = setTimeout(() => controller.abort(), timeout);
|
|
92
|
+
try {
|
|
93
|
+
const res = await doFetch(`${baseURL}v1/messages`, {
|
|
94
|
+
method: "POST",
|
|
95
|
+
headers: buildHeaders(),
|
|
96
|
+
body: JSON.stringify(req),
|
|
97
|
+
signal: signal || controller.signal,
|
|
98
|
+
});
|
|
99
|
+
clearTimeout(timeoutId);
|
|
100
|
+
if (!res.ok) {
|
|
101
|
+
let message = `KimiCoding error: ${res.status}`;
|
|
102
|
+
let body = null;
|
|
103
|
+
try {
|
|
104
|
+
body = await res.json();
|
|
105
|
+
if (isAnthropicErrorBody(body) &&
|
|
106
|
+
typeof body.error?.message === "string") {
|
|
107
|
+
message = `KimiCoding error ${res.status}: ${body.error.message}`;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
catch {
|
|
111
|
+
// ignore parse errors
|
|
112
|
+
}
|
|
113
|
+
throw new KimiCodingError(message, res.status, body);
|
|
114
|
+
}
|
|
115
|
+
for await (const { event, data } of sseToIterable(res)) {
|
|
116
|
+
if (event === "message_stop") {
|
|
117
|
+
break;
|
|
118
|
+
}
|
|
119
|
+
try {
|
|
120
|
+
const parsed = JSON.parse(data);
|
|
121
|
+
yield parsed;
|
|
122
|
+
}
|
|
123
|
+
catch {
|
|
124
|
+
// ignore non-JSON lines
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
finally {
|
|
129
|
+
clearTimeout(timeoutId);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
// POST https://api.kimi.com/coding/v1/messages
|
|
133
|
+
// Docs: https://platform.moonshot.ai/docs
|
|
134
|
+
async function chatImpl(req, signal) {
|
|
135
|
+
const controller = new AbortController();
|
|
136
|
+
const timeoutId = setTimeout(() => controller.abort(), timeout);
|
|
137
|
+
try {
|
|
138
|
+
const res = await doFetch(`${baseURL}v1/messages`, {
|
|
139
|
+
method: "POST",
|
|
140
|
+
headers: buildHeaders(),
|
|
141
|
+
body: JSON.stringify(req),
|
|
142
|
+
signal: signal || controller.signal,
|
|
143
|
+
});
|
|
144
|
+
clearTimeout(timeoutId);
|
|
145
|
+
if (!res.ok) {
|
|
146
|
+
let message = `KimiCoding error: ${res.status}`;
|
|
147
|
+
let body = null;
|
|
148
|
+
try {
|
|
149
|
+
body = await res.json();
|
|
150
|
+
if (isAnthropicErrorBody(body) &&
|
|
151
|
+
typeof body.error?.message === "string") {
|
|
152
|
+
message = `KimiCoding error ${res.status}: ${body.error.message}`;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
catch {
|
|
156
|
+
// ignore parse errors
|
|
157
|
+
}
|
|
158
|
+
throw new KimiCodingError(message, res.status, body);
|
|
159
|
+
}
|
|
160
|
+
return (await res.json());
|
|
161
|
+
}
|
|
162
|
+
finally {
|
|
163
|
+
clearTimeout(timeoutId);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
// POST https://api.kimi.com/coding/v1/embeddings
|
|
167
|
+
// Docs: https://platform.moonshot.ai/docs
|
|
168
|
+
async function embeddingsImpl(req, signal) {
|
|
169
|
+
const controller = new AbortController();
|
|
170
|
+
const timeoutId = setTimeout(() => controller.abort(), timeout);
|
|
171
|
+
try {
|
|
172
|
+
const res = await doFetch(`${baseURL}v1/embeddings`, {
|
|
173
|
+
method: "POST",
|
|
174
|
+
headers: buildHeaders(),
|
|
175
|
+
body: JSON.stringify(req),
|
|
176
|
+
signal: signal || controller.signal,
|
|
177
|
+
});
|
|
178
|
+
clearTimeout(timeoutId);
|
|
179
|
+
if (!res.ok) {
|
|
180
|
+
let message = `KimiCoding error: ${res.status}`;
|
|
181
|
+
let body = null;
|
|
182
|
+
try {
|
|
183
|
+
body = await res.json();
|
|
184
|
+
if (isAnthropicErrorBody(body) &&
|
|
185
|
+
typeof body.error?.message === "string") {
|
|
186
|
+
message = `KimiCoding error ${res.status}: ${body.error.message}`;
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
catch {
|
|
190
|
+
// ignore parse errors
|
|
191
|
+
}
|
|
192
|
+
throw new KimiCodingError(message, res.status, body);
|
|
193
|
+
}
|
|
194
|
+
return (await res.json());
|
|
195
|
+
}
|
|
196
|
+
finally {
|
|
197
|
+
clearTimeout(timeoutId);
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
// sig-ok: ergonomic name (URL is /tokens/count)
|
|
201
|
+
// POST https://api.kimi.com/coding/v1/tokens/count
|
|
202
|
+
// Docs: https://platform.moonshot.ai/docs
|
|
203
|
+
async function countTokensImpl(req, signal) {
|
|
204
|
+
const controller = new AbortController();
|
|
205
|
+
const timeoutId = setTimeout(() => controller.abort(), timeout);
|
|
206
|
+
try {
|
|
207
|
+
const res = await doFetch(`${baseURL}v1/tokens/count`, {
|
|
208
|
+
method: "POST",
|
|
209
|
+
headers: buildHeaders(),
|
|
210
|
+
body: JSON.stringify(req),
|
|
211
|
+
signal: signal || controller.signal,
|
|
212
|
+
});
|
|
213
|
+
clearTimeout(timeoutId);
|
|
214
|
+
if (!res.ok) {
|
|
215
|
+
let message = `KimiCoding error: ${res.status}`;
|
|
216
|
+
let body = null;
|
|
217
|
+
try {
|
|
218
|
+
body = await res.json();
|
|
219
|
+
if (isAnthropicErrorBody(body) &&
|
|
220
|
+
typeof body.error?.message === "string") {
|
|
221
|
+
message = `KimiCoding error ${res.status}: ${body.error.message}`;
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
catch {
|
|
225
|
+
// ignore parse errors
|
|
226
|
+
}
|
|
227
|
+
throw new KimiCodingError(message, res.status, body);
|
|
228
|
+
}
|
|
229
|
+
return (await res.json());
|
|
230
|
+
}
|
|
231
|
+
finally {
|
|
232
|
+
clearTimeout(timeoutId);
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
const messages = Object.assign(chatImpl, {
|
|
236
|
+
schema: ChatRequestSchema,
|
|
237
|
+
});
|
|
238
|
+
const streamMessages = Object.assign(streamImpl, {
|
|
239
|
+
schema: ChatRequestSchema,
|
|
240
|
+
});
|
|
241
|
+
const embeddings = Object.assign(embeddingsImpl, {
|
|
242
|
+
schema: EmbeddingRequestSchema,
|
|
243
|
+
});
|
|
244
|
+
const countTokens = Object.assign(countTokensImpl, {
|
|
245
|
+
schema: CountTokensRequestSchema,
|
|
246
|
+
});
|
|
247
|
+
// GET https://api.kimi.com/coding/v1/models
|
|
248
|
+
// Docs: https://platform.moonshot.ai/docs
|
|
249
|
+
async function listModelsFn(signal) {
|
|
250
|
+
return await makeGetRequest("v1/models", signal);
|
|
251
|
+
}
|
|
252
|
+
return attachExamples({
|
|
253
|
+
post: {
|
|
254
|
+
coding: { v1: { messages, embeddings, countTokens } },
|
|
255
|
+
stream: { coding: { v1: { messages: streamMessages } } },
|
|
256
|
+
},
|
|
257
|
+
get: { coding: { v1: { models: listModelsFn } } },
|
|
258
|
+
});
|
|
259
|
+
}
|
|
260
|
+
//# sourceMappingURL=kimicoding.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"kimicoding.js","sourceRoot":"","sources":["../../src/kimicoding.ts"],"names":[],"mappings":"AAAA,OAAO,EAKL,eAAe,GAShB,MAAM,SAAS,CAAC;AACjB,OAAO,EAAE,aAAa,EAAE,MAAM,OAAO,CAAC;AACtC,OAAO,EACL,iBAAiB,EACjB,sBAAsB,EACtB,wBAAwB,GACzB,MAAM,OAAO,CAAC;AACf,OAAO,EAAE,cAAc,EAAE,MAAM,WAAW,CAAC;AAE3C,6EAA6E;AAC7E,SAAS,kBAAkB,CACzB,MAA+B,EAC/B,UAA2B;IAE3B,IAAI,CAAC,MAAM;QAAE,OAAO;IAEpB,gEAAgE;IAChE,IAAI,OAAO,MAAM,CAAC,gBAAgB,KAAK,UAAU,EAAE,CAAC;QAClD,MAAM,CAAC,gBAAgB,CAAC,OAAO,EAAE,GAAG,EAAE,CAAC,UAAU,CAAC,KAAK,EAAE,EAAE,EAAE,IAAI,EAAE,IAAI,EAAE,CAAC,CAAC;IAC7E,CAAC;SAAM,IAAI,MAAM,CAAC,OAAO,EAAE,CAAC;QAC1B,4CAA4C;QAC5C,UAAU,CAAC,KAAK,EAAE,CAAC;IACrB,CAAC;AACH,CAAC;AAMD,SAAS,oBAAoB,CAAC,CAAU;IACtC,OAAO,CACL,OAAO,CAAC,KAAK,QAAQ;QACrB,CAAC,KAAK,IAAI;QACV,OAAO,IAAI,CAAC;QACZ,OAAQ,CAAyB,CAAC,KAAK,KAAK,QAAQ,CACrD,CAAC;AACJ,CAAC;AAED,MAAM,UAAU,SAAS,CAAC,IAAY;IACpC,OAAO,EAAE,IAAI,EAAE,MAAM,E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{
|
|
2
|
+
retries?: number;
|
|
3
|
+
baseMs?: number;
|
|
4
|
+
factor?: number;
|
|
5
|
+
jitter?: boolean;
|
|
6
|
+
}
|
|
7
|
+
export interface FallbackOptions {
|
|
8
|
+
onFallback?: (error: unknown, index: number) => void;
|
|
9
|
+
}
|
|
10
|
+
export declare function withRetry<TReq, TRes>(fn: (req: TReq, signal?: AbortSignal) => Promise<TRes>, opts?: RetryOptions): (req: TReq, signal?: AbortSignal) => Promise<TRes>;
|
|
11
|
+
export declare function withFallback<TReq, TRes>(fns: Array<(req: TReq, signal?: AbortSignal) => Promise<TRes>>, opts?: FallbackOptions): (req: TReq, signal?: AbortSignal) => Promise<TRes>;
|
|
12
|
+
export declare function withStreamRetry<TReq, TChunk>(fn: (req: TReq, signal?: AbortSignal) => AsyncIterable<TChunk>, opts?: RetryOptions): (req: TReq, signal?: AbortSignal) => AsyncIterable<TChunk>;
|
|
13
|
+
export declare function withStreamFallback<TReq, TChunk>(fns: Array<(req: TReq, signal?: AbortSignal) => AsyncIterable<TChunk>>, opts?: FallbackOptions): (req: TReq, signal?: AbortSignal) => AsyncIterable<TChunk>;
|
|
14
|
+
export interface RateLimiterOptions {
|
|
15
|
+
/** Maximum requests per minute (sliding window). Default: Infinity */
|
|
16
|
+
rpm?: number;
|
|
17
|
+
/** Maximum concurrent in-flight requests. Default: Infinity */
|
|
18
|
+
concurrent?: number;
|
|
19
|
+
/** Maximum time (ms) a request will wait in queue. Default: 60000 */
|
|
20
|
+
maxQueueMs?: number;
|
|
21
|
+
}
|
|
22
|
+
export interface RateLimiter {
|
|
23
|
+
/** Current number of in-flight requests */
|
|
24
|
+
readonly active: number;
|
|
25
|
+
/** Current number of queued requests waiting for a slot */
|
|
26
|
+
readonly queued: number;
|
|
27
|
+
/** Reject all queued requests and clean up timers */
|
|
28
|
+
dispose(): void;
|
|
29
|
+
}
|
|
30
|
+
export interface RateLimitOptions {
|
|
31
|
+
/** Override maxQueueMs for this particular wrapped function */
|
|
32
|
+
maxQueueMs?: number;
|
|
33
|
+
}
|
|
34
|
+
export declare function createRateLimiter(opts?: RateLimiterOptions): RateLimiter;
|
|
35
|
+
export declare function withRateLimit<TReq, TRes>(fn: (req: TReq, signal?: AbortSignal) => Promise<TRes>, limiter: RateLimiter, opts?: RateLimitOptions): (req: TReq, signal?: AbortSignal) => Promise<TRes>;
|
|
36
|
+
//# sourceMappingURL=middleware.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"middleware.d.ts","sourceRoot":"","sources":["../../src/middleware.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,YAAY;IAC3B,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,MAAM,CAAC,EAAE,OAAO,CAAC;CAClB;AAED,MAAM,WAAW,eAAe;IAC9B,UAAU,CAAC,EAAE,CAAC,KAAK,EAAE,OAAO,EAAE,KAAK,EAAE,MAAM,KAAK,IAAI,CAAC;CACtD;AA+BD,wBAAgB,SAAS,CAAC,IAAI,EAAE,IAAI,EAClC,EAAE,EAAE,CAAC,GAAG,EAAE,IAAI,EAAE,MAAM,CAAC,EAAE,WAAW,KAAK,OAAO,CAAC,IAAI,CAAC,EACtD,IAAI,GAAE,YAAiB,GACtB,CAAC,GAAG,EAAE,IAAI,EAAE,MAAM,CAAC,EAAE,WAAW,KAAK,OAAO,CAAC,IAAI,CAAC,CA2BpD;AAED,wBAAgB,YAAY,CAAC,IAAI,EAAE,IAAI,EACrC,GAAG,EAAE,KAAK,CAAC,CAAC,GAAG,EAAE,IAAI,EAAE,MAAM,CAAC,EAAE,WAAW,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC,EAC9D,IAAI,GAAE,eAAoB,GACzB,CAAC,GAAG,EAAE,IAAI,EAAE,MAAM,CAAC,EAAE,WAAW,KAAK,OAAO,CAAC,IAAI,CAAC,CAiBpD;AAED,wBAAgB,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