@holdem-api/holdem-sdk 0.1.2 → 0.1.3
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 +247 -153
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,155 +1,212 @@
|
|
|
1
|
-
# Holdem SDK
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
##
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
```
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
```
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
1
|
+
# Holdem SDK
|
|
2
|
+
|
|
3
|
+
**Poker decisions, equity, and range tracking for your Node.js backend.**
|
|
4
|
+
|
|
5
|
+
[](https://www.npmjs.com/package/@holdem-api/holdem-sdk)
|
|
6
|
+

|
|
7
|
+
[](./LICENSE)
|
|
8
|
+
|
|
9
|
+
A typed JavaScript/TypeScript client for Holdem API with zero runtime dependencies. Ships as ESM with TypeScript declarations. Live decisions use `POST /api/actions`; range storage and complete hand-history parsing run locally.
|
|
10
|
+
|
|
11
|
+
| Start with | Use it to |
|
|
12
|
+
| --- | --- |
|
|
13
|
+
| `HoldemClient.actions()` | Request a decision from an encoded public game state |
|
|
14
|
+
| `HoldemClient.actionsFromHistory()` | Request the next decision from unfinished PokerStars hand-history text |
|
|
15
|
+
| `RangeStore` | Keep player ranges across turns in your server process |
|
|
16
|
+
| `parseHandHistories()` | Turn complete hand-history files into recorded decisions locally |
|
|
17
|
+
|
|
18
|
+
**Requires:** Node.js 20+, an API key, and access to a Holdem API deployment for live requests. Keep API credentials on your backend.
|
|
19
|
+
|
|
20
|
+
[Install](#install) · [API access](#configure-your-api-access) · [First request](#first-live-request) · [Integration](#integrate-decisions-and-track-ranges) · [Ranges](#ranges) · [Hand histories](#convert-hand-history-files) · [Encoding reference](#public-policy-encoding-reference)
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
## Install
|
|
24
|
+
|
|
25
|
+
In your **backend/server project** (for Blitzfold, use `blitzfold-server`):
|
|
26
|
+
|
|
27
|
+
```sh
|
|
28
|
+
npm install @holdem-api/holdem-sdk
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
No repository clone, local archive, or SDK build step is required. Node.js 20+ is required. The package is ESM-only. JavaScript `.mjs` files can import it directly. CommonJS applications can use native `await import('@holdem-api/holdem-sdk')` inside an async function; TypeScript compiling to CommonJS must preserve that native import rather than rewriting it to require().
|
|
32
|
+
|
|
33
|
+
Verify installation without an API key or network request:
|
|
34
|
+
|
|
35
|
+
```sh
|
|
36
|
+
node --input-type=module -e "import { HoldemClient, RangeStore } from '@holdem-api/holdem-sdk'; console.log('SDK ready:', typeof HoldemClient, typeof RangeStore)"
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
Expected output: `SDK ready: function function`.
|
|
40
|
+
|
|
41
|
+
## Configure your API access
|
|
42
|
+
|
|
43
|
+
Obtain a Holdem API key and the origin of the API deployment you are authorized to use. Installing this package does not create an API account, provision a server, or include credentials. Configure these variables on your backend:
|
|
44
|
+
|
|
45
|
+
```dotenv
|
|
46
|
+
HOLDEM_API_KEY=your_api_key
|
|
47
|
+
HOLDEM_API_URL=https://your-api-host.example
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
Replace the example host with your actual deployment origin, without `/api`. Keep `.env` out of version control. Node 20.6+ can load it using `node --env-file=.env your-script.mjs`; on earlier Node 20 versions set environment variables in your shell or use your application's environment loader. Never put the key in browser code.
|
|
51
|
+
|
|
52
|
+
## First live request
|
|
53
|
+
|
|
54
|
+
Save this as `holdem-smoke.mjs` in the project where you installed the SDK:
|
|
55
|
+
|
|
56
|
+
```js
|
|
57
|
+
import { readFile } from 'node:fs/promises'
|
|
58
|
+
import { HoldemClient } from '@holdem-api/holdem-sdk'
|
|
59
|
+
|
|
60
|
+
const { HOLDEM_API_KEY: apiKey, HOLDEM_API_URL: baseUrl } = process.env
|
|
61
|
+
if (!apiKey || !baseUrl || !process.argv[2]) {
|
|
62
|
+
throw new Error('Set HOLDEM_API_KEY and HOLDEM_API_URL; supply a request JSON file')
|
|
63
|
+
}
|
|
64
|
+
const client = new HoldemClient({ apiKey, baseUrl })
|
|
65
|
+
const request = JSON.parse(await readFile(process.argv[2], 'utf8'))
|
|
66
|
+
const result = await client.actions(request)
|
|
67
|
+
console.log({
|
|
68
|
+
chosenAction: result.chosenAction,
|
|
69
|
+
probabilities: result.output,
|
|
70
|
+
equity: result.equity,
|
|
71
|
+
expectedValues: result.expectedValues,
|
|
72
|
+
rangeCount: Object.keys(result.range).length,
|
|
73
|
+
})
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
Export a real pre-decision game state using the policy encoding below into `actions-request.json`, with top-level `{ "hand": "AsAh", "policy": { "schemaVersion": "holdem-public-board-joint-v1", "context": [...], "history": [...], "legalMask": [0,1,1] } }`. The ellipses describe encoder output; replace them with actual arrays. Do not use made-up features or an empty context. Then run:
|
|
77
|
+
|
|
78
|
+
```sh
|
|
79
|
+
node --env-file=.env holdem-smoke.mjs actions-request.json
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
This makes one live request and may consume API quota. It does not execute an action in your game. If this fails, 401/403 indicates an access/authentication issue; 400 indicates invalid input; 502/503 can indicate unavailable equity/model services. Inspect the SDK error status and your API configuration.
|
|
83
|
+
|
|
84
|
+
## Integrate decisions and track ranges
|
|
85
|
+
|
|
86
|
+
```ts
|
|
87
|
+
import { HoldemClient, RangeStore } from '@holdem-api/holdem-sdk'
|
|
88
|
+
import type { PublicPolicy } from '@holdem-api/holdem-sdk'
|
|
89
|
+
|
|
90
|
+
const holdem = new HoldemClient({
|
|
91
|
+
apiKey: process.env.HOLDEM_API_KEY!,
|
|
92
|
+
baseUrl: process.env.HOLDEM_API_URL!, // origin without /api
|
|
93
|
+
})
|
|
94
|
+
const ranges = new RangeStore()
|
|
95
|
+
|
|
96
|
+
async function decide(handId: string, playerId: string, policy: PublicPolicy) {
|
|
97
|
+
const result = await holdem.actions({
|
|
98
|
+
policy,
|
|
99
|
+
hand: 'AsAh',
|
|
100
|
+
range: ranges.get(handId, playerId),
|
|
101
|
+
// opponentRanges: [ranges.get(handId, opponentId) ?? null],
|
|
102
|
+
})
|
|
103
|
+
// Your engine executes result.chosenAction, sampling sizes/weights on aggression.
|
|
104
|
+
// Only AFTER successful execution of that action:
|
|
105
|
+
// ranges.record(handId, playerId, result, executedAction)
|
|
106
|
+
return result
|
|
107
|
+
}
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
`predictActions` is an alias with the same request and response. This replaces the original unpublished SDK's legacy state-based prediction, separate range-update, and equity methods.
|
|
111
|
+
|
|
112
|
+
Supply the exact public encoder output as `policy`: schemaVersion `holdem-public-board-joint-v1`, 316 context features, chronological history of at most 512 events with 32 features each, and legalMask `[fold, passive, aggressive]`. Use the API/parser public encoder; an ordinary game snapshot is not that encoding. Context has board slots (260), actor (9), street (4), six log-scaled amounts/counts, nine public seat records (36), and remaining opponents / 9. Amount scaling is `log1p(max(0,value))/log1p(400)`.
|
|
113
|
+
|
|
114
|
+
## Response at a glance
|
|
115
|
+
|
|
116
|
+
- `equity`, `uniformEquity`, `equityBasis`, optional `opponentEquity`.
|
|
117
|
+
- `output`: hand-specific action probabilities; `population`: public model targets.
|
|
118
|
+
- `chosenAction`: sampled fold/call/raise or check/bet. No size is sampled.
|
|
119
|
+
- `sizes` and `weights`: conditional aggression sizing distribution. Your engine applies legal payments, raises, all-ins and side pots.
|
|
120
|
+
- `range`: normalized actor posterior conditioned on chosenAction.
|
|
121
|
+
- `expectedValues`: simplified uniform heads-up showdown benchmark in BB. Assumes one caller, no fold equity, no future betting, no rake or side pots; not strategic or opponent-range-aware EV.
|
|
122
|
+
- Optional `learnedExpectedValues`: experimental observed-action historical returns, only when configured on the API server. Not validated counterfactual action values.
|
|
123
|
+
- `improvementPotential`, `improvement`, allocation identifiers, and all additional server fields are preserved.
|
|
124
|
+
|
|
125
|
+
## Ranges
|
|
126
|
+
|
|
127
|
+
`WeightedRange` maps exact two-card combos to nonnegative weights. Omit `range` for a uniform board-compatible actor population. Pass a prior to track a reached range; the actual hand must have positive mass. Do not initialize a range with just the known hand. Optional `opponentRanges` has one map or null (uniform) per remaining opponent; never send revealed opponents' actual cards.
|
|
128
|
+
|
|
129
|
+
### Local range storage
|
|
130
|
+
|
|
131
|
+
`RangeStore` keeps ranges in your backend server's memory. It keeps a JavaScript `Map` keyed by hand ID and player ID. Players do not need to manage range files or enter ranges themselves; your backend handles that work.
|
|
132
|
+
|
|
133
|
+
`HoldemClient` does not save ranges automatically. Create one store for the lifetime of your room/server, read the prior before each request, and save the returned range only after the chosen action executes successfully. Do not create a new store inside each decision.
|
|
134
|
+
|
|
135
|
+
### Example: reuse a player's range across turns
|
|
136
|
+
|
|
137
|
+
```ts
|
|
138
|
+
import { HoldemClient, RangeStore } from '@holdem-api/holdem-sdk'
|
|
139
|
+
import type { ActionsResult, ActionType, PublicPolicy } from '@holdem-api/holdem-sdk'
|
|
140
|
+
|
|
141
|
+
const client = new HoldemClient({
|
|
142
|
+
apiKey: process.env.HOLDEM_API_KEY!,
|
|
143
|
+
baseUrl: process.env.HOLDEM_API_URL!,
|
|
144
|
+
})
|
|
145
|
+
const ranges = new RangeStore() // Shared across this room's decisions.
|
|
146
|
+
|
|
147
|
+
// Your engine adapter executes result.chosenAction and returns the category
|
|
148
|
+
// actually executed. Throw on failure. For bet/raise, sample the conditional
|
|
149
|
+
// sizes/weights and enforce your engine's legal sizing limits.
|
|
150
|
+
type ExecuteAction = (result: ActionsResult) => Promise<ActionType>
|
|
151
|
+
|
|
152
|
+
async function decideAndExecute(
|
|
153
|
+
handId: string,
|
|
154
|
+
playerId: string,
|
|
155
|
+
hand: string,
|
|
156
|
+
policy: PublicPolicy,
|
|
157
|
+
executeAction: ExecuteAction,
|
|
158
|
+
) {
|
|
159
|
+
const result = await client.actions({
|
|
160
|
+
hand,
|
|
161
|
+
policy,
|
|
162
|
+
// First turn: undefined means a uniform board-compatible range.
|
|
163
|
+
// Later turns: reuse this player's last saved range for this hand.
|
|
164
|
+
range: ranges.get(handId, playerId),
|
|
165
|
+
})
|
|
166
|
+
|
|
167
|
+
// Execute chosenAction directly; do not sample the category a second time.
|
|
168
|
+
const executedAction = await executeAction(result)
|
|
169
|
+
// Save only after successful execution. A different category throws and
|
|
170
|
+
// leaves the store unchanged: this posterior belongs to chosenAction.
|
|
171
|
+
ranges.record(handId, playerId, result, executedAction)
|
|
172
|
+
return result
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
function onHandFinished(handId: string) {
|
|
176
|
+
ranges.clear(handId) // Remove every player's range for this hand.
|
|
177
|
+
}
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
Call `decideAndExecute` on each turn with the same hand/player IDs, the actor's actual hand, and freshly encoded public policy. Your application supplies the engine adapter and calls `onHandFinished` when the hand ends. Serialize the request, execution, and save per player, and reject expired hand/turn responses before execution. If the engine executes a different category, stop this flow and explicitly resolve the player's prior before another decision; do not save this response's range for that different action.
|
|
181
|
+
|
|
182
|
+
### Lifetime and persistence
|
|
183
|
+
|
|
184
|
+
- `get(handId, playerId)` returns a copy of the stored range, or `undefined` if none exists.
|
|
185
|
+
- `record(handId, playerId, result, executedAction)` saves a copy after verifying that the executed category matches `chosenAction`.
|
|
186
|
+
- `set(handId, playerId, range)` explicitly stores a validated range, including a snapshot restored from your database.
|
|
187
|
+
- `clear(handId)` clears one hand; `clear()` clears the entire store, for example when disposing of a room.
|
|
188
|
+
|
|
189
|
+
This is in-process server memory, not browser local storage or a file on disk. Ranges disappear when the process restarts and are not shared across workers or server instances. If you need restart recovery or shared state, persist `get()` snapshots in your application's database and restore them with `set()`. The SDK does not include a persistence layer.
|
|
190
|
+
|
|
191
|
+
The API itself stores no ranges. The SDK does not infer ranges for arbitrary observed opponent actions. Optional `opponentRanges` must contain valid tracked priors or `null` for uniform; never substitute an opponent's revealed actual hand.
|
|
192
|
+
|
|
193
|
+
## Errors and cancellation
|
|
194
|
+
|
|
195
|
+
| Error | Meaning |
|
|
196
|
+
| --- | --- |
|
|
197
|
+
| `HoldemValidationError` | Invalid local input; the request was not sent |
|
|
198
|
+
| `HoldemApiError` | The API rejected the request; inspect `status` and `body` |
|
|
199
|
+
| `HoldemResponseError` | A successful HTTP response did not match the expected shape |
|
|
200
|
+
| `HoldemTransportError` | Network failure, timeout, or cancellation |
|
|
201
|
+
|
|
202
|
+
Request methods accept `{ signal }` as the second argument:
|
|
203
|
+
|
|
204
|
+
```ts
|
|
205
|
+
const controller = new AbortController()
|
|
206
|
+
const result = await client.actions(request, { signal: controller.signal })
|
|
207
|
+
```
|
|
208
|
+
|
|
209
|
+
The default timeout is 60 seconds and includes reading the response body. Override it with `timeoutMs` in the client constructor. There are no automatic retries. API keys are sent in the `x-api-key` header.
|
|
153
210
|
|
|
154
211
|
## Convert hand-history files
|
|
155
212
|
|
|
@@ -223,4 +280,41 @@ may be used for retrospective analysis; private cards, future board cards and
|
|
|
223
280
|
future actions never enter earlier public policies. The helper makes no API calls.
|
|
224
281
|
It returns recorded decisions, not pending decisions from unfinished live hands.
|
|
225
282
|
It does not infer reached ranges. Omitted ranges use uniform defaults; do not
|
|
226
|
-
carry a sampled-action posterior through a history that took a different action.
|
|
283
|
+
carry a sampled-action posterior through a history that took a different action.
|
|
284
|
+
|
|
285
|
+
## Public policy encoding reference
|
|
286
|
+
|
|
287
|
+
The SDK accepts encoded public features; it does not reconstruct betting state. Use consistent seat IDs 0-8 throughout a hand. Cards are indexed by `rankIndex * 4 + suitIndex`, ranks `23456789TJQKA`, suits `cdhs`. Street indexes are preflop=0, flop=1, turn=2, river=3. A one-hot block has exactly one 1; an empty board slot is all zeros. Board slots retain deal order. All amounts below are BB unless a count is specified; scale amounts/counts using `log1p(max(0,x))/log1p(400)`.
|
|
288
|
+
|
|
289
|
+
| Context indexes | Encoding |
|
|
290
|
+
| --- | --- |
|
|
291
|
+
| 0-259 | Five ordered 52-value board one-hot slots |
|
|
292
|
+
| 260-268 | Actor seat one-hot |
|
|
293
|
+
| 269-272 | Street one-hot |
|
|
294
|
+
| 273-278 | Scaled pot, facing amount, call payment, min raise-to, max raise-to, number of seats |
|
|
295
|
+
| 279-314 | Nine seat records: present flag, scaled remaining stack, scaled current-street contribution, active flag; absent seats have four zeros |
|
|
296
|
+
| 315 | Remaining active opponent count divided by 9 |
|
|
297
|
+
|
|
298
|
+
Each history event has 32 values: actor seat one-hot (9), street one-hot (4), action one-hot (6, ordered posts/folds/checks/calls/bets/raises), four scaled values (payment added, raise-to or 0, live post or 0, dead post or 0), and nine scaled refund amounts indexed by seat. Sum multiple refunds to the same seat after the event before scaling. History must contain all actual preceding events in order, across streets, excluding the current decision. Send no artificial padding; empty history is supported. Maximum history length is 512. LegalMask is `[fold, passive, aggressive]` derived from actual engine legality, with only 0/1 values and at least one allowed action. Do not include opponents' private cards or future events. Encode call payment, contributions, and raise limits from actual engine state; do not infer them from incomplete logs.
|
|
299
|
+
|
|
300
|
+
## Blitzfold migration checklist
|
|
301
|
+
|
|
302
|
+
Install in the server. Replace the `/api/equity_actions` request with `client.actions(payload)` using the existing public encoder. Preserve the full response, including `range`; remove any code deleting it. Use `chosenAction` directly instead of sampling the action category a second time. Sample only the conditional size after aggression and enforce engine legality. Commit the actor range only after that same category executes successfully; reject stale hand/turn responses and clear completed hands. Select optional player styles locally and send them with the request, so decisions need only `/api/actions`. Keep error/cooldown handling and the game's timeout budget. Verify both the development runner and the compiled server can load this ESM package.
|
|
303
|
+
|
|
304
|
+
## SDK maintainers: build and publish
|
|
305
|
+
|
|
306
|
+
```sh
|
|
307
|
+
npm ci
|
|
308
|
+
npm test
|
|
309
|
+
npm pack
|
|
310
|
+
```
|
|
311
|
+
|
|
312
|
+
The package is configured for public publication as `@holdem-api/holdem-sdk` under the MIT license. For publication:
|
|
313
|
+
|
|
314
|
+
1. Create or obtain access to the `holdem-api` npm organization. The package name is configured as `@holdem-api/holdem-sdk`. The public access setting and MIT license are already configured.
|
|
315
|
+
2. Run `npm login` and complete npm's browser authentication/2FA.
|
|
316
|
+
3. Run `npm test` and `npm pack --dry-run` to inspect contents.
|
|
317
|
+
4. Run `npm publish --access public` and complete any npm verification prompt.
|
|
318
|
+
5. In the consuming project, run `npm install @holdem-api/holdem-sdk`.
|
|
319
|
+
|
|
320
|
+
Do not put credentials in source or the package. Before publishing another release, bump the version with `npm version patch` (requires a clean git checkout) or edit the version and update the lockfile.
|