cocoon-ai-provider 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/CHANGELOG.md +7 -0
- package/LICENSE +21 -0
- package/README.md +135 -0
- package/dist/index.cjs +819 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +26 -0
- package/dist/index.d.ts +26 -0
- package/dist/index.js +803 -0
- package/dist/index.js.map +1 -0
- package/package.json +58 -0
- package/src/cocoon-client-manager.ts +199 -0
- package/src/cocoon-errors.ts +128 -0
- package/src/cocoon-language-model-options.ts +99 -0
- package/src/cocoon-language-model.ts +399 -0
- package/src/cocoon-provider.ts +55 -0
- package/src/convert-cocoon-usage.ts +53 -0
- package/src/convert-to-cocoon-chat-messages.ts +104 -0
- package/src/index.ts +7 -0
- package/src/map-cocoon-finish-reason.ts +19 -0
- package/src/types.ts +24 -0
package/CHANGELOG.md
ADDED
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026
|
|
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,135 @@
|
|
|
1
|
+
# cocoon-ai-provider
|
|
2
|
+
|
|
3
|
+
AI SDK v6 provider for [Cocoon](https://github.com/indie-cto/telegram-cocoon-ts-sdk).
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pnpm add cocoon-ai-provider ai
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Quick Start
|
|
12
|
+
|
|
13
|
+
```ts
|
|
14
|
+
import { cocoon } from 'cocoon-ai-provider';
|
|
15
|
+
import { generateText } from 'ai';
|
|
16
|
+
|
|
17
|
+
const result = await generateText({
|
|
18
|
+
model: cocoon('Qwen/Qwen3-32B'),
|
|
19
|
+
prompt: 'Write a one-line welcome message.',
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
console.log(result.text);
|
|
23
|
+
await cocoon.close();
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
## Streaming
|
|
27
|
+
|
|
28
|
+
```ts
|
|
29
|
+
import { cocoon } from 'cocoon-ai-provider';
|
|
30
|
+
import { streamText } from 'ai';
|
|
31
|
+
|
|
32
|
+
const result = streamText({
|
|
33
|
+
model: cocoon('Qwen/Qwen3-32B'),
|
|
34
|
+
prompt: 'Write a short product tagline.',
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
for await (const textPart of result.textStream) {
|
|
38
|
+
process.stdout.write(textPart);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
await cocoon.close();
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
## Runnable Example
|
|
45
|
+
|
|
46
|
+
This repository includes a runnable end-to-end example at `examples/basic.mjs`.
|
|
47
|
+
|
|
48
|
+
```bash
|
|
49
|
+
COCOON_WALLET="your 24-word mnemonic" pnpm example:basic
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
Optional:
|
|
53
|
+
|
|
54
|
+
- `COCOON_MODEL_ID` to override the model (default: `Qwen/Qwen3-32B`)
|
|
55
|
+
- Any other Cocoon env vars described below
|
|
56
|
+
|
|
57
|
+
## Custom Provider Instance
|
|
58
|
+
|
|
59
|
+
You can pass an existing `Cocoon` client or let the provider build one from `clientOptions`.
|
|
60
|
+
|
|
61
|
+
```ts
|
|
62
|
+
import { createCocoon } from 'cocoon-ai-provider';
|
|
63
|
+
|
|
64
|
+
const provider = createCocoon({
|
|
65
|
+
clientOptions: {
|
|
66
|
+
wallet: process.env.MNEMONIC!,
|
|
67
|
+
secretString: process.env.SECRET,
|
|
68
|
+
proxyUrl: process.env.PROXY_URL,
|
|
69
|
+
},
|
|
70
|
+
defaults: {
|
|
71
|
+
maxCoefficient: 1.2,
|
|
72
|
+
timeout: 30_000,
|
|
73
|
+
},
|
|
74
|
+
});
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
## Provider Options
|
|
78
|
+
|
|
79
|
+
Use call-level Cocoon options with `providerOptions.cocoon`:
|
|
80
|
+
|
|
81
|
+
```ts
|
|
82
|
+
import { generateText } from 'ai';
|
|
83
|
+
import { cocoon } from 'cocoon-ai-provider';
|
|
84
|
+
|
|
85
|
+
const result = await generateText({
|
|
86
|
+
model: cocoon('Qwen/Qwen3-32B'),
|
|
87
|
+
prompt: 'Summarize this in one sentence.',
|
|
88
|
+
providerOptions: {
|
|
89
|
+
cocoon: {
|
|
90
|
+
maxCoefficient: 1.1,
|
|
91
|
+
timeout: 20_000,
|
|
92
|
+
},
|
|
93
|
+
},
|
|
94
|
+
});
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
Call-level `providerOptions.cocoon` override provider-level `defaults`.
|
|
98
|
+
|
|
99
|
+
## Environment Variables
|
|
100
|
+
|
|
101
|
+
When `createCocoon()` is called without `client` and without `clientOptions`, the provider builds a Cocoon client from env values.
|
|
102
|
+
|
|
103
|
+
- `wallet`: `COCOON_WALLET` or `MNEMONIC` (required)
|
|
104
|
+
- `secretString`: `COCOON_SECRET_STRING` or `SECRET`
|
|
105
|
+
- `proxyUrl`: `COCOON_PROXY_URL` or `PROXY_URL`
|
|
106
|
+
- `tonEndpoint`: `COCOON_TON_ENDPOINT` or `TON_ENDPOINT`
|
|
107
|
+
- `tonV4Endpoint`: `COCOON_TON_V4_ENDPOINT` or `TON_V4_ENDPOINT`
|
|
108
|
+
- `network`: `COCOON_NETWORK` (`mainnet` or `testnet`)
|
|
109
|
+
- `timeout`: `COCOON_TIMEOUT` (positive number)
|
|
110
|
+
- `useTls`: `COCOON_USE_TLS` (`true`/`false` or `1`/`0`)
|
|
111
|
+
- `autoRegisterOnLongAuth`: `COCOON_AUTO_REGISTER_ON_LONG_AUTH` (`true`/`false` or `1`/`0`, default `false`)
|
|
112
|
+
- `longAuthRegisterAmountTon`: `COCOON_LONG_AUTH_REGISTER_AMOUNT_TON`
|
|
113
|
+
- TLS credential values: `COCOON_TLS_CERT` + `COCOON_TLS_KEY`
|
|
114
|
+
- TLS credential files: `COCOON_TLS_CERT_PATH` + `COCOON_TLS_KEY_PATH`
|
|
115
|
+
|
|
116
|
+
## Runtime and Limitations
|
|
117
|
+
|
|
118
|
+
- Node.js `>=18` only.
|
|
119
|
+
- v1 supports language-model generation and streaming only.
|
|
120
|
+
- Tool calling is not supported.
|
|
121
|
+
- Multimodal file input is not supported.
|
|
122
|
+
- Embedding and image model methods intentionally throw `NoSuchModelError`.
|
|
123
|
+
- Abort behavior is best-effort. Cocoon cancellation is limited by the underlying SDK.
|
|
124
|
+
|
|
125
|
+
## Testing
|
|
126
|
+
|
|
127
|
+
```bash
|
|
128
|
+
pnpm test
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
Optional real-network integration tests:
|
|
132
|
+
|
|
133
|
+
```bash
|
|
134
|
+
pnpm test:integration
|
|
135
|
+
```
|