cocoon-sdk 0.1.0 → 0.1.2

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/README.md CHANGED
@@ -1,159 +1,117 @@
1
1
  # Cocoon TypeScript SDK
2
2
 
3
- OpenAI-compatible TypeScript SDK for Telegram Cocoon.
3
+ OpenAI-style TypeScript SDK for Telegram Cocoon.
4
4
 
5
- This guide is intentionally step-by-step so you can get to a working request without reading Cocoon docs.
5
+ - npm: `cocoon-sdk`
6
+ - repo: `https://github.com/indie-cto/telegram-cocoon-ts-sdk`
6
7
 
7
- ## Quickstart (From Zero to First Response)
8
-
9
- ### Prerequisites
10
-
11
- - Node.js `>=18`
12
- - `openssl`
13
- - TON wallet mnemonic (24 words)
14
- - Small TON balance for one-time setup transactions
15
-
16
- ### TON Balance Requirements
17
-
18
- Two different numbers matter:
19
-
20
- 1) **Setup transaction spend** (what setup sends from wallet by default):
21
- - `REGISTER_TON=1.0` TON
22
- - `CHANGE_SECRET_TON=0.7` TON
23
- - plus network fees
24
-
25
- 2) **Protocol stake parameter** (`minClientStake`) from root config.
26
- As of **February 22, 2026** on mainnet:
27
- - `minClientStake = 15 TON`
28
-
29
- So:
30
- - **2-3 TON** is usually enough to complete setup transactions.
31
- - For stable inference usage, plan for **~15-20 TON** total working balance (stake + buffer).
32
- - Current live values can change; check with `npm run cocoon:discover`.
33
-
34
- Optional for heavier usage:
35
-
36
- - set `TOP_UP_TON` (for example `15` or `20`) to increase available client balance for inference traffic.
37
-
38
- ### 1) Install dependencies
8
+ ## Install
39
9
 
40
10
  ```bash
41
- npm install
11
+ npm i cocoon-sdk
42
12
  ```
43
13
 
44
- ### 2) Create `.env`
14
+ ## Minimal Usage
45
15
 
46
- ```bash
47
- cp .env.example .env
48
- ```
16
+ ```ts
17
+ import { Cocoon } from 'cocoon-sdk';
49
18
 
50
- Edit `.env` and set only:
19
+ const client = new Cocoon({
20
+ wallet: process.env.MNEMONIC!, // 24 words
21
+ secretString: process.env.SECRET!, // from your setup flow
22
+ proxyUrl: process.env.PROXY_URL, // optional (discovery if omitted)
23
+ tonV4Endpoint: process.env.TON_V4_ENDPOINT,
24
+ });
51
25
 
52
- ```env
53
- MNEMONIC=your 24 words here
54
- ```
26
+ const models = await client.models.list();
27
+ const model = models.data[0]!.id;
55
28
 
56
- ### 3) Run one-time setup
29
+ const result = await client.chat.completions.create({
30
+ model,
31
+ messages: [{ role: 'user', content: 'Say OK' }],
32
+ });
57
33
 
58
- ```bash
59
- npm run cocoon:setup
34
+ console.log(result.choices[0]?.message.content);
35
+ await client.disconnect();
60
36
  ```
61
37
 
62
- What setup does for you:
63
-
64
- - generates TLS client cert/key automatically,
65
- - performs long-auth registration if needed,
66
- - sets on-chain secret hash,
67
- - prints ready-to-paste `.env` values.
38
+ ## Streaming
68
39
 
69
- Example final output:
40
+ ```ts
41
+ const stream = await client.chat.completions.create({
42
+ model: 'Qwen/Qwen3-32B',
43
+ messages: [{ role: 'user', content: 'Write one short sentence' }],
44
+ stream: true,
45
+ });
70
46
 
71
- ```text
72
- === Setup Complete ===
73
- Put these into your .env:
74
- SECRET=...
75
- PROXY_URL=91.108.4.11:8888
76
- COCOON_TLS_CERT_PATH=/tmp/cocoon-client-xxxx.pem
77
- COCOON_TLS_KEY_PATH=/tmp/cocoon-client-xxxx.key.pem
78
- TON_V4_ENDPOINT=https://mainnet-v4.tonhubapi.com
47
+ for await (const chunk of stream) {
48
+ process.stdout.write(chunk.choices[0]?.delta?.content ?? '');
49
+ }
79
50
  ```
80
51
 
81
- ### 4) Paste setup output into `.env`
82
-
83
- Copy those lines exactly into `.env`.
52
+ ## Important Defaults
84
53
 
85
- Important: you do not need to find or create certificates manually.
86
- `scripts/setup.ts` creates them and gives you the file paths.
54
+ - `autoRegisterOnLongAuth` default is `false` (safety-first).
55
+ - SDK does **not** submit on-chain registration tx unless you explicitly enable it.
56
+ - Recommended production path: run setup once, then use `SECRET` for short auth.
87
57
 
88
- ### 5) Run inference
58
+ ## One-Time Setup (Repo Helper Scripts)
89
59
 
90
- ```bash
91
- npm run cocoon:inference
92
- ```
93
-
94
- You should see:
60
+ The npm package is a library.
61
+ If you want the fully automated setup flow (cert/key generation + register + secret hash), use repo scripts:
95
62
 
96
- - connected models list,
97
- - generated model output.
63
+ - `https://github.com/indie-cto/telegram-cocoon-ts-sdk`
64
+ - `scripts/setup.ts`
65
+ - `scripts/inference.ts`
98
66
 
99
- ## Minimal SDK Usage
67
+ Important for users integrating from another project:
100
68
 
101
- ```ts
102
- import { Cocoon } from 'cocoon-sdk';
69
+ - The intended path is to run `scripts/setup.ts` once and copy `SECRET` + TLS paths.
70
+ - Do **not** rely on ad-hoc certificate generation commands from agents.
71
+ - A generic command like `openssl req -x509 -newkey ec ...` is not the tested/default SDK path.
103
72
 
104
- const client = new Cocoon({
105
- wallet: process.env.MNEMONIC!,
106
- secretString: process.env.SECRET,
107
- proxyUrl: process.env.PROXY_URL,
108
- tonV4Endpoint: process.env.TON_V4_ENDPOINT,
109
- });
73
+ If you cannot run `scripts/setup.ts`, use this tested certificate generation format:
110
74
 
111
- const models = await client.models.list();
112
- const model = models.data[0]!.id;
75
+ ```bash
76
+ openssl genpkey -algorithm ed25519 -out /tmp/cocoon-client.key.pem
77
+ openssl req -x509 \
78
+ -key /tmp/cocoon-client.key.pem \
79
+ -out /tmp/cocoon-client-cert.pem \
80
+ -days 1 \
81
+ -subj "/C=AE/ST=DUBAI/O=TDLib Development/OU=Security/CN=localhost"
82
+ ```
113
83
 
114
- const res = await client.chat.completions.create({
115
- model,
116
- messages: [{ role: 'user', content: 'Say OK' }],
117
- });
84
+ Then set:
118
85
 
119
- console.log(res.choices[0]?.message.content);
120
- await client.disconnect();
86
+ ```env
87
+ COCOON_TLS_CERT_PATH=/tmp/cocoon-client-cert.pem
88
+ COCOON_TLS_KEY_PATH=/tmp/cocoon-client.key.pem
121
89
  ```
122
90
 
123
- ## Defaults (Designed for the Common Path)
91
+ Note: this only covers TLS material. You still need a valid `SECRET` (recommended via setup flow).
92
+
93
+ ## TON Balance Guidance
124
94
 
125
- - `autoRegisterOnLongAuth` is enabled by default.
126
- - If long auth is required, SDK can auto-submit registration transaction.
127
- - You usually should not change advanced auth options.
95
+ Two values matter:
128
96
 
129
- ## Useful Commands
97
+ 1. Setup spend defaults:
98
+ - `REGISTER_TON=1.0`
99
+ - `CHANGE_SECRET_TON=0.7`
100
+ - plus fees
130
101
 
131
- - Setup once:
132
- - `npm run cocoon:setup`
133
- - Inference test:
134
- - `npm run cocoon:inference`
135
- - Discover proxies/models:
136
- - `npm run cocoon:discover`
137
- - Register only (advanced):
138
- - `npm run cocoon:register`
139
- - Create wallet helper:
140
- - `npm run cocoon:create-wallet`
141
- - Pure library example:
142
- - `npx tsx --env-file=.env examples/basic.ts`
102
+ 2. Network stake parameter (`minClientStake`) from root config:
103
+ - on mainnet, this can be much higher (for example 15 TON on Feb 22, 2026)
143
104
 
144
- ## Troubleshooting
105
+ Practical guidance:
145
106
 
146
- - `Proxy requires long auth` or secret mismatch:
147
- - run `npm run cocoon:setup` again and update `.env` with fresh output.
148
- - TLS handshake closes immediately:
149
- - verify `COCOON_TLS_CERT_PATH` and `COCOON_TLS_KEY_PATH` exist and match.
150
- - TON RPC `429`:
151
- - set `TON_V4_ENDPOINT=https://mainnet-v4.tonhubapi.com`.
107
+ - smoke setup: typically `2-3 TON` can be enough
108
+ - stable usage: plan around `15-20 TON` total working balance
109
+ - verify live values with repo helper: `scripts/discover.ts`
152
110
 
153
111
  ## Security
154
112
 
155
- - Never commit `.env`, mnemonics, secrets, or private keys.
156
- - Treat `/tmp/cocoon-client-*.key.pem` as sensitive.
113
+ - Never commit mnemonic, secrets, or private keys.
114
+ - Treat TLS key files as sensitive.
157
115
 
158
116
  ## License
159
117
 
package/dist/index.cjs CHANGED
@@ -1364,7 +1364,7 @@ function buildHandshakeCloseError(stage, error) {
1364
1364
  async function performLongAuth(conn, connected, nonce, onLongAuthRequired, missingHandlerMessage) {
1365
1365
  if (!onLongAuthRequired) {
1366
1366
  throw new AuthenticationError(
1367
- `${missingHandlerMessage}. Provide SECRET for short auth, or keep automatic long-auth registration enabled`
1367
+ `${missingHandlerMessage}. Provide SECRET for short auth, or explicitly enable automatic long-auth registration`
1368
1368
  );
1369
1369
  }
1370
1370
  await onLongAuthRequired({
@@ -2434,7 +2434,8 @@ var Cocoon = class {
2434
2434
  network: "mainnet",
2435
2435
  timeout: 12e4,
2436
2436
  useTls: true,
2437
- autoRegisterOnLongAuth: true,
2437
+ // Safety-first default for SDK consumers: don't submit on-chain tx implicitly.
2438
+ autoRegisterOnLongAuth: false,
2438
2439
  longAuthRegisterAmountTon: "1",
2439
2440
  ...options
2440
2441
  };