mohdel 1.2.0 → 1.3.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/config/curated.schema.json +8 -3
- package/js/factory/bridge.js +9 -2
- package/js/session/_rate_limiter.js +33 -7
- package/js/session/run_embedding.js +47 -4
- package/package.json +2 -2
- package/src/cli/complete.js +1 -0
- package/src/cli/index.js +2 -2
- package/src/cli/ratelimit.js +118 -47
- package/src/lib/index.js +25 -20
- package/src/lib/schema.js +1 -0
- package/src/lib/rate-limiter.js +0 -50
|
@@ -553,12 +553,17 @@
|
|
|
553
553
|
"rpmLimit": {
|
|
554
554
|
"type": "integer",
|
|
555
555
|
"minimum": 1,
|
|
556
|
-
"description": "Requests per minute. Overrides provider default."
|
|
556
|
+
"description": "Requests per minute this key may send. Overrides the provider-level default."
|
|
557
557
|
},
|
|
558
558
|
"tpmLimit": {
|
|
559
559
|
"type": "integer",
|
|
560
560
|
"minimum": 1,
|
|
561
|
-
"description": "Tokens per minute. Overrides provider default."
|
|
561
|
+
"description": "Tokens per minute this key may spend. Overrides the provider-level default."
|
|
562
|
+
},
|
|
563
|
+
"inpmLimit": {
|
|
564
|
+
"type": "integer",
|
|
565
|
+
"minimum": 1,
|
|
566
|
+
"description": "Inputs per minute this key may send to an embedding endpoint, for a provider that meters the endpoint in inputs rather than requests or tokens (Cohere publishes '2,000 inputs / min'). Counted exactly before dispatch from the batch size; a batch larger than the whole allowance is sent rather than delayed, since waiting cannot make it fit."
|
|
562
567
|
},
|
|
563
568
|
"rateLimitScope": {
|
|
564
569
|
"type": "string",
|
|
@@ -566,7 +571,7 @@
|
|
|
566
571
|
"model",
|
|
567
572
|
"provider"
|
|
568
573
|
],
|
|
569
|
-
"description": "'model' = private
|
|
574
|
+
"description": "Whose budget this key's calls draw on: 'model' = a private bucket for this entry, 'provider' = the pool shared with every other model of the provider."
|
|
570
575
|
},
|
|
571
576
|
"deprecated": {
|
|
572
577
|
"type": "string",
|
package/js/factory/bridge.js
CHANGED
|
@@ -187,6 +187,8 @@ export async function runAnswerTranscription ({ provider, model, configuration,
|
|
|
187
187
|
* @param {object} args
|
|
188
188
|
* @param {string} args.provider
|
|
189
189
|
* @param {string} args.model
|
|
190
|
+
* @param {string} [args.modelKey] Mohdel catalog key, for the rate-limit
|
|
191
|
+
* bucket when the entry is model-scoped.
|
|
190
192
|
* @param {any} args.configuration
|
|
191
193
|
* @param {string | string[]} args.input One text or a batch; normalized to an
|
|
192
194
|
* array so the result shape never
|
|
@@ -195,9 +197,10 @@ export async function runAnswerTranscription ({ provider, model, configuration,
|
|
|
195
197
|
* the envelope; `callId` / `authId` are
|
|
196
198
|
* transport metadata.
|
|
197
199
|
* @param {any} [args.spec]
|
|
200
|
+
* @param {BridgeDeps} [deps]
|
|
198
201
|
* @returns {Promise<any>}
|
|
199
202
|
*/
|
|
200
|
-
export async function runAnswerEmbedding ({ provider, model, configuration, input, options = {}, spec }) {
|
|
203
|
+
export async function runAnswerEmbedding ({ provider, model, modelKey, configuration, input, options = {}, spec }, deps = {}) {
|
|
201
204
|
const callId = options.callId || newCallId()
|
|
202
205
|
const authId = options.authId || 'local'
|
|
203
206
|
assertValidIds(callId, authId, `${provider}/${model}`)
|
|
@@ -212,7 +215,11 @@ export async function runAnswerEmbedding ({ provider, model, configuration, inpu
|
|
|
212
215
|
if (options.inputType) envelope.inputType = options.inputType
|
|
213
216
|
if (options.dimensions !== undefined) envelope.dimensions = options.dimensions
|
|
214
217
|
|
|
215
|
-
const out = await runEmbedding(envelope,
|
|
218
|
+
const out = await runEmbedding(envelope, {
|
|
219
|
+
...deps,
|
|
220
|
+
...(modelKey ? { modelKey } : {}),
|
|
221
|
+
...(spec ? { spec } : {})
|
|
222
|
+
})
|
|
216
223
|
if (!out.ok) throw MohdelError.fromJSON(out.error, { provider, model })
|
|
217
224
|
return out.result
|
|
218
225
|
}
|
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Minute-bucket rate limiter (per-key: provider or provider/model).
|
|
3
3
|
*
|
|
4
|
-
* Tracks RPM and
|
|
4
|
+
* Tracks RPM, TPM and INPM — inputs per minute, the unit an embedding
|
|
5
|
+
* endpoint is metered in when the provider counts inputs rather than
|
|
6
|
+
* requests or tokens. Returns ms to wait if over limit — throttles
|
|
5
7
|
* rather than rejecting, so the caller can absorb small bursts
|
|
6
8
|
* without a 429 round-trip.
|
|
7
9
|
*
|
|
@@ -14,7 +16,7 @@
|
|
|
14
16
|
*/
|
|
15
17
|
|
|
16
18
|
export function createRateLimiter () {
|
|
17
|
-
/** @type {Map<string, {count: number, tokens: number, minute: number}>} */
|
|
19
|
+
/** @type {Map<string, {count: number, tokens: number, inputs: number, minute: number}>} */
|
|
18
20
|
const buckets = new Map()
|
|
19
21
|
|
|
20
22
|
const currentMinute = () => Math.floor(Date.now() / 60000)
|
|
@@ -24,7 +26,7 @@ export function createRateLimiter () {
|
|
|
24
26
|
const minute = currentMinute()
|
|
25
27
|
const b = buckets.get(key)
|
|
26
28
|
if (b && b.minute === minute) return b
|
|
27
|
-
const fresh = { count: 0, tokens: 0, minute }
|
|
29
|
+
const fresh = { count: 0, tokens: 0, inputs: 0, minute }
|
|
28
30
|
buckets.set(key, fresh)
|
|
29
31
|
return fresh
|
|
30
32
|
}
|
|
@@ -42,15 +44,30 @@ export function createRateLimiter () {
|
|
|
42
44
|
* returned regardless of the current bucket.
|
|
43
45
|
* - positive number → throttle at that value.
|
|
44
46
|
*
|
|
47
|
+
* `rpmLimit` and `tpmLimit` gate on what the bucket already holds:
|
|
48
|
+
* the size of the call ahead is not known until it returns.
|
|
49
|
+
* `inpmLimit` gates on what the call is about to add, which
|
|
50
|
+
* `pending.inputs` carries — a batch size is exact before dispatch,
|
|
51
|
+
* so the call is admitted only if the whole batch fits.
|
|
52
|
+
*
|
|
45
53
|
* @param {string} key
|
|
46
|
-
* @param {{rpmLimit?: number, tpmLimit?: number}} limits
|
|
54
|
+
* @param {{rpmLimit?: number, tpmLimit?: number, inpmLimit?: number}} limits
|
|
55
|
+
* @param {{inputs?: number}} [pending]
|
|
47
56
|
* @returns {number}
|
|
48
57
|
*/
|
|
49
|
-
const check = (key, { rpmLimit, tpmLimit } = {}) => {
|
|
50
|
-
if (rpmLimit == null && tpmLimit == null) return 0
|
|
58
|
+
const check = (key, { rpmLimit, tpmLimit, inpmLimit } = {}, pending = {}) => {
|
|
59
|
+
if (rpmLimit == null && tpmLimit == null && inpmLimit == null) return 0
|
|
51
60
|
const b = getBucket(key)
|
|
52
61
|
if (rpmLimit != null && b.count >= rpmLimit) return msUntilNextMinute(b.minute)
|
|
53
62
|
if (tpmLimit != null && b.tokens >= tpmLimit) return msUntilNextMinute(b.minute)
|
|
63
|
+
if (inpmLimit != null) {
|
|
64
|
+
const adding = pending.inputs ?? 0
|
|
65
|
+
if (inpmLimit === 0) return msUntilNextMinute(b.minute)
|
|
66
|
+
// A batch larger than the whole allowance never fits, so waiting out the
|
|
67
|
+
// minute buys nothing: send it and take the provider's answer. Splitting
|
|
68
|
+
// it is the caller's call, not mohdel's.
|
|
69
|
+
if (adding <= inpmLimit && b.inputs + adding > inpmLimit) return msUntilNextMinute(b.minute)
|
|
70
|
+
}
|
|
54
71
|
return 0
|
|
55
72
|
}
|
|
56
73
|
|
|
@@ -67,7 +84,15 @@ export function createRateLimiter () {
|
|
|
67
84
|
getBucket(key).tokens += tokens
|
|
68
85
|
}
|
|
69
86
|
|
|
70
|
-
|
|
87
|
+
/**
|
|
88
|
+
* @param {string} key
|
|
89
|
+
* @param {number} inputs
|
|
90
|
+
*/
|
|
91
|
+
const recordInputs = (key, inputs) => {
|
|
92
|
+
getBucket(key).inputs += inputs
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
return { check, recordRequest, recordTokens, recordInputs }
|
|
71
96
|
}
|
|
72
97
|
|
|
73
98
|
// Single session-local instance.
|
|
@@ -75,3 +100,4 @@ const defaultLimiter = createRateLimiter()
|
|
|
75
100
|
export const check = defaultLimiter.check
|
|
76
101
|
export const recordRequest = defaultLimiter.recordRequest
|
|
77
102
|
export const recordTokens = defaultLimiter.recordTokens
|
|
103
|
+
export const recordInputs = defaultLimiter.recordInputs
|
|
@@ -3,27 +3,46 @@
|
|
|
3
3
|
* returns either a result or a typed error, never throwing.
|
|
4
4
|
*
|
|
5
5
|
* Mirrors `run_transcription.js`: one synchronous request, no streaming, no
|
|
6
|
-
* cancellation path beyond the caller's own signal.
|
|
6
|
+
* cancellation path beyond the caller's own signal. Rate limits are enforced
|
|
7
|
+
* as in `run.js`, minus the speed lanes embeddings do not have.
|
|
7
8
|
*
|
|
8
9
|
* @module session/run_embedding
|
|
9
10
|
*/
|
|
10
11
|
|
|
11
12
|
import { getEmbeddingAdapter } from './adapters/embedding/index.js'
|
|
12
13
|
import { classifyProviderError } from './adapters/_errors.js'
|
|
14
|
+
import { getProviderLimits } from './adapters/_providers.js'
|
|
15
|
+
import * as defaultLimiter from './_rate_limiter.js'
|
|
13
16
|
import { providerOf } from '#core/model-id.js'
|
|
14
17
|
|
|
15
18
|
/**
|
|
16
19
|
* @param {import('#core/embedding.js').EmbedEnvelope} envelope
|
|
17
|
-
* @param {{
|
|
20
|
+
* @param {{
|
|
21
|
+
* resolveAdapter?: (provider: string) => any,
|
|
22
|
+
* resolveProviderLimits?: (provider: string) => any,
|
|
23
|
+
* limiter?: any,
|
|
24
|
+
* sleep?: (ms: number) => Promise<void>,
|
|
25
|
+
* modelKey?: string,
|
|
26
|
+
* spec?: any
|
|
27
|
+
* }} [options]
|
|
18
28
|
* @returns {Promise<
|
|
19
29
|
* | {ok: true, result: import('#core/embedding.js').EmbedResult}
|
|
20
30
|
* | {ok: false, error: import('#core/errors.js').TypedError}
|
|
21
31
|
* >}
|
|
22
32
|
*/
|
|
23
|
-
export async function runEmbedding (envelope, {
|
|
33
|
+
export async function runEmbedding (envelope, {
|
|
34
|
+
resolveAdapter = getEmbeddingAdapter,
|
|
35
|
+
resolveProviderLimits = getProviderLimits,
|
|
36
|
+
limiter = defaultLimiter,
|
|
37
|
+
sleep = defaultSleep,
|
|
38
|
+
modelKey = envelope.model,
|
|
39
|
+
spec
|
|
40
|
+
} = {}) {
|
|
41
|
+
const provider = providerOf(envelope.model)
|
|
42
|
+
|
|
24
43
|
let adapter
|
|
25
44
|
try {
|
|
26
|
-
adapter = resolveAdapter(
|
|
45
|
+
adapter = resolveAdapter(provider)
|
|
27
46
|
} catch (e) {
|
|
28
47
|
return {
|
|
29
48
|
ok: false,
|
|
@@ -36,8 +55,27 @@ export async function runEmbedding (envelope, { resolveAdapter = getEmbeddingAda
|
|
|
36
55
|
}
|
|
37
56
|
}
|
|
38
57
|
|
|
58
|
+
const providerCfg = resolveProviderLimits(provider) || {}
|
|
59
|
+
const rpmLimit = spec?.rpmLimit ?? providerCfg.rpmLimit
|
|
60
|
+
const tpmLimit = spec?.tpmLimit ?? providerCfg.tpmLimit
|
|
61
|
+
const inpmLimit = spec?.inpmLimit ?? providerCfg.inpmLimit
|
|
62
|
+
// `modelKey` is the catalog key, which the envelope carries over the wire but
|
|
63
|
+
// not on the in-process path, where it holds the upstream id instead.
|
|
64
|
+
const bucketKey = spec?.rateLimitScope === 'model' ? modelKey : provider
|
|
65
|
+
// A malformed envelope is metered as nothing: `checkBatch` rejects it inside
|
|
66
|
+
// the adapter, and a call that never reaches the provider must not spend quota.
|
|
67
|
+
const inputs = Array.isArray(envelope.input) ? envelope.input.length : 0
|
|
68
|
+
|
|
69
|
+
if (inputs > 0 && (rpmLimit != null || tpmLimit != null || inpmLimit != null)) {
|
|
70
|
+
const delay = limiter.check(bucketKey, { rpmLimit, tpmLimit, inpmLimit }, { inputs })
|
|
71
|
+
if (delay > 0) await sleep(delay)
|
|
72
|
+
limiter.recordRequest(bucketKey)
|
|
73
|
+
if (inpmLimit != null) limiter.recordInputs(bucketKey, inputs)
|
|
74
|
+
}
|
|
75
|
+
|
|
39
76
|
try {
|
|
40
77
|
const result = await adapter(envelope, spec ? { spec } : {})
|
|
78
|
+
if (tpmLimit != null && result.inputTokens) limiter.recordTokens(bucketKey, result.inputTokens)
|
|
41
79
|
return { ok: true, result }
|
|
42
80
|
} catch (e) {
|
|
43
81
|
const typed = /** @type {any} */(e).typed || classifyProviderError(e, envelope.auth?.key)
|
|
@@ -49,3 +87,8 @@ export async function runEmbedding (envelope, { resolveAdapter = getEmbeddingAda
|
|
|
49
87
|
function messageOf (e) {
|
|
50
88
|
return e instanceof Error ? e.message : String(e)
|
|
51
89
|
}
|
|
90
|
+
|
|
91
|
+
/** @param {number} ms */
|
|
92
|
+
function defaultSleep (ms) {
|
|
93
|
+
return new Promise(resolve => setTimeout(resolve, ms))
|
|
94
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mohdel",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.3.0",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "Christophe Le Bars",
|
|
@@ -135,7 +135,7 @@
|
|
|
135
135
|
"@opentelemetry/exporter-trace-otlp-grpc": "^0.222.0",
|
|
136
136
|
"@opentelemetry/sdk-node": "^0.222.0",
|
|
137
137
|
"chalk": "^6.0.0",
|
|
138
|
-
"mohdel-thin-gate-linux-x64-gnu": "1.
|
|
138
|
+
"mohdel-thin-gate-linux-x64-gnu": "1.3.0"
|
|
139
139
|
},
|
|
140
140
|
"dependencies": {
|
|
141
141
|
"@anthropic-ai/sdk": "^0.125.0",
|
package/src/cli/complete.js
CHANGED
package/src/cli/index.js
CHANGED
|
@@ -80,9 +80,9 @@ Commands:
|
|
|
80
80
|
tag rm <model> <tag> Remove a tag
|
|
81
81
|
|
|
82
82
|
ratelimit show <model|provider> Show effective limits (mo rl show)
|
|
83
|
-
ratelimit set <model>
|
|
83
|
+
ratelimit set <model> <limit> <value> Set limits: rpm, tpm, inpm
|
|
84
84
|
ratelimit rm <model> Remove model-level limits
|
|
85
|
-
ratelimit provider set <p>
|
|
85
|
+
ratelimit provider set <p> <limit> <v> Set provider-level limits
|
|
86
86
|
ratelimit provider rm <p> Remove provider-level limits
|
|
87
87
|
|
|
88
88
|
ask <provider/model> [prompt] One-shot inference (pipeable)
|
package/src/cli/ratelimit.js
CHANGED
|
@@ -4,24 +4,111 @@ import { parseJsonFlag, jsonOutputOne } from './json-output.js'
|
|
|
4
4
|
// CLI logger: silent for noisy levels, console.error for errors and fatals.
|
|
5
5
|
const cliLogger = { ...silent, error: console.error, fatal: console.error }
|
|
6
6
|
|
|
7
|
+
const LIMIT_NAMES = ['rpm', 'tpm', 'inpm']
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* `0` is a killswitch, not "unset", so read nullability rather than truth.
|
|
11
|
+
*
|
|
12
|
+
* @param {{rpmLimit?: number, tpmLimit?: number, inpmLimit?: number} | null | undefined} entry
|
|
13
|
+
* @returns {string[]}
|
|
14
|
+
*/
|
|
15
|
+
function limitParts (entry) {
|
|
16
|
+
if (!entry) return []
|
|
17
|
+
return LIMIT_NAMES
|
|
18
|
+
.filter(name => entry[`${name}Limit`] != null)
|
|
19
|
+
.map(name => `${name}=${entry[`${name}Limit`]}`)
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* @param {string[]} cleared Limits named on the command line; empty means all.
|
|
24
|
+
* @param {string[]} parts What is left afterwards.
|
|
25
|
+
*/
|
|
26
|
+
function clearedLine (cleared, parts) {
|
|
27
|
+
if (cleared.length === 0) return 'limits cleared'
|
|
28
|
+
return `${cleared.join(', ')} cleared; ${parts.length ? `${parts.join(' ')} remain` : 'no limits remain'}`
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** @param {string[]} names */
|
|
32
|
+
function parseLimitNames (names) {
|
|
33
|
+
for (const name of names) {
|
|
34
|
+
if (!LIMIT_NAMES.includes(name)) {
|
|
35
|
+
console.error(`Unknown limit '${name}'. Known: ${LIMIT_NAMES.join(', ')}`)
|
|
36
|
+
process.exit(1)
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
return names
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** @param {string} raw */
|
|
43
|
+
function toCount (raw) {
|
|
44
|
+
const n = parseInt(raw, 10)
|
|
45
|
+
if (!Number.isInteger(n) || n < 0 || String(n) !== String(raw).trim()) {
|
|
46
|
+
console.error(`'${raw}' is not a whole number`)
|
|
47
|
+
process.exit(1)
|
|
48
|
+
}
|
|
49
|
+
return n
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Two forms. Named pairs — `rpm 60 inpm 2000` — reach every limit, including
|
|
54
|
+
* one on its own. The positional `<rpm> [tpm]` covers the common pair; a
|
|
55
|
+
* leading digit picks that form, since no limit is named one.
|
|
56
|
+
*
|
|
57
|
+
* @param {string[]} args
|
|
58
|
+
* @param {string} usage
|
|
59
|
+
* @returns {{rpm?: number, tpm?: number, inpm?: number}}
|
|
60
|
+
*/
|
|
61
|
+
function parseLimits (args, usage) {
|
|
62
|
+
if (args.length === 0) { console.error(usage); process.exit(1) }
|
|
63
|
+
|
|
64
|
+
if (/^\d/.test(args[0])) {
|
|
65
|
+
const [rpm, tpm] = args
|
|
66
|
+
return tpm ? { rpm: toCount(rpm), tpm: toCount(tpm) } : { rpm: toCount(rpm) }
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** @type {Record<string, number>} */
|
|
70
|
+
const limits = {}
|
|
71
|
+
for (let i = 0; i < args.length; i += 2) {
|
|
72
|
+
const name = args[i]
|
|
73
|
+
if (!LIMIT_NAMES.includes(name)) {
|
|
74
|
+
console.error(`Unknown limit '${name}'. Known: ${LIMIT_NAMES.join(', ')}`)
|
|
75
|
+
process.exit(1)
|
|
76
|
+
}
|
|
77
|
+
if (args[i + 1] == null) { console.error(`'${name}' needs a value`); process.exit(1) }
|
|
78
|
+
limits[name] = toCount(args[i + 1])
|
|
79
|
+
}
|
|
80
|
+
return limits
|
|
81
|
+
}
|
|
82
|
+
|
|
7
83
|
export async function runRateLimit (args) {
|
|
8
84
|
const jsonFlag = parseJsonFlag(args)
|
|
9
|
-
const [action, arg1
|
|
85
|
+
const [action, arg1] = args
|
|
10
86
|
|
|
11
87
|
if (!action || action === '-h' || action === '--help') {
|
|
12
88
|
console.log(`mohdel ratelimit — manage rate limits
|
|
13
89
|
|
|
14
90
|
Usage:
|
|
15
91
|
ratelimit show <model|provider> [--json] Show effective limits
|
|
16
|
-
ratelimit set <model>
|
|
17
|
-
ratelimit
|
|
18
|
-
ratelimit
|
|
19
|
-
ratelimit provider
|
|
92
|
+
ratelimit set <model> <limit> <value> … Set limits by name
|
|
93
|
+
ratelimit set <model> <rpm> [tpm] Shortcut for the two common ones
|
|
94
|
+
ratelimit rm <model> [limit …] Remove limits, or all of them
|
|
95
|
+
ratelimit provider set <provider> <limit> <value> …
|
|
96
|
+
ratelimit provider set <provider> <rpm> [tpm]
|
|
97
|
+
ratelimit provider rm <provider> [limit …] Remove limits, or all of them
|
|
98
|
+
|
|
99
|
+
Limits:
|
|
100
|
+
rpm requests per minute
|
|
101
|
+
tpm tokens per minute
|
|
102
|
+
inpm inputs per minute — what an embedding endpoint is metered in when
|
|
103
|
+
the provider counts inputs rather than requests or tokens
|
|
20
104
|
|
|
21
105
|
Examples:
|
|
22
106
|
ratelimit show anthropic Provider limits
|
|
23
107
|
ratelimit show gemini/gemini-flash-latest Model limits, then provider
|
|
108
|
+
ratelimit set cohere/embed-v4.0 inpm 2000
|
|
109
|
+
ratelimit set gemini/gemini-flash-latest rpm 15 tpm 1000000
|
|
24
110
|
ratelimit set gemini/gemini-flash-latest 15 1000000
|
|
111
|
+
ratelimit rm cohere/embed-v4.0 inpm
|
|
25
112
|
ratelimit provider set anthropic 60 100000
|
|
26
113
|
|
|
27
114
|
Aliases:
|
|
@@ -49,35 +136,24 @@ Configuration:
|
|
|
49
136
|
if (providerAction === 'show') {
|
|
50
137
|
if (!providerName) { console.error('Usage: ratelimit provider show <provider>'); process.exit(1) }
|
|
51
138
|
const entry = mo.getProviderRateLimit(providerName)
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
} else {
|
|
55
|
-
const parts = []
|
|
56
|
-
if (entry.rpmLimit) parts.push(`rpm=${entry.rpmLimit}`)
|
|
57
|
-
if (entry.tpmLimit) parts.push(`tpm=${entry.tpmLimit}`)
|
|
58
|
-
console.log(`${providerName}: ${parts.join(' ')}`)
|
|
59
|
-
}
|
|
139
|
+
const parts = limitParts(entry)
|
|
140
|
+
console.log(parts.length ? `${providerName}: ${parts.join(' ')}` : `${providerName}: no limits set`)
|
|
60
141
|
return
|
|
61
142
|
}
|
|
62
143
|
|
|
63
144
|
if (providerAction === 'set') {
|
|
64
145
|
if (!providerName) { console.error('Usage: ratelimit provider set <provider> [rpm] [tpm]'); process.exit(1) }
|
|
65
|
-
const
|
|
66
|
-
const
|
|
67
|
-
|
|
68
|
-
if (rpm == null && tpm == null) { console.error('Provide at least rpm or tpm'); process.exit(1) }
|
|
69
|
-
const result = await mo.setProviderRateLimit(providerName, { rpm, tpm })
|
|
70
|
-
const parts = []
|
|
71
|
-
if (result.rpmLimit) parts.push(`rpm=${result.rpmLimit}`)
|
|
72
|
-
if (result.tpmLimit) parts.push(`tpm=${result.tpmLimit}`)
|
|
73
|
-
console.log(`${providerName}: ${parts.join(' ')}`)
|
|
146
|
+
const limits = parseLimits(providerArgs, 'Usage: ratelimit provider set <provider> <limit> <value> … | <rpm> [tpm]')
|
|
147
|
+
const result = await mo.setProviderRateLimit(providerName, limits)
|
|
148
|
+
console.log(`${providerName}: ${limitParts(result).join(' ')}`)
|
|
74
149
|
return
|
|
75
150
|
}
|
|
76
151
|
|
|
77
152
|
if (providerAction === 'rm' || providerAction === 'remove') {
|
|
78
|
-
if (!providerName) { console.error('Usage: ratelimit provider rm <provider>'); process.exit(1) }
|
|
79
|
-
|
|
80
|
-
|
|
153
|
+
if (!providerName) { console.error('Usage: ratelimit provider rm <provider> [limit …]'); process.exit(1) }
|
|
154
|
+
const names = parseLimitNames(providerArgs)
|
|
155
|
+
const remaining = await mo.clearProviderRateLimit(providerName, names)
|
|
156
|
+
console.log(`${providerName}: ${clearedLine(names, limitParts(remaining))}`)
|
|
81
157
|
return
|
|
82
158
|
}
|
|
83
159
|
|
|
@@ -98,27 +174,24 @@ Configuration:
|
|
|
98
174
|
const providerEntry = mo.getProviderRateLimit(info.provider) || {}
|
|
99
175
|
const rpmLimit = info.rpmLimit ?? providerEntry.rpmLimit
|
|
100
176
|
const tpmLimit = info.tpmLimit ?? providerEntry.tpmLimit
|
|
177
|
+
const inpmLimit = info.inpmLimit ?? providerEntry.inpmLimit
|
|
101
178
|
const scope = info.rateLimitScope || 'provider'
|
|
102
|
-
const source = (info.
|
|
179
|
+
const source = limitParts(info).length ? 'model' : 'provider'
|
|
103
180
|
if (jsonFlag.json) {
|
|
104
|
-
jsonOutputOne({ id: arg1, rpmLimit: rpmLimit || null, tpmLimit: tpmLimit || null, scope, source })
|
|
181
|
+
jsonOutputOne({ id: arg1, rpmLimit: rpmLimit || null, tpmLimit: tpmLimit || null, inpmLimit: inpmLimit || null, scope, source })
|
|
105
182
|
return
|
|
106
183
|
}
|
|
107
|
-
|
|
184
|
+
const parts = limitParts({ rpmLimit, tpmLimit, inpmLimit })
|
|
185
|
+
if (parts.length === 0) {
|
|
108
186
|
console.log(`${arg1}: no limits`)
|
|
109
187
|
} else {
|
|
110
|
-
|
|
111
|
-
if (rpmLimit) parts.push(`rpm=${rpmLimit}`)
|
|
112
|
-
if (tpmLimit) parts.push(`tpm=${tpmLimit}`)
|
|
113
|
-
parts.push(`scope=${scope}`)
|
|
114
|
-
parts.push(`(${source})`)
|
|
115
|
-
console.log(`${arg1}: ${parts.join(' ')}`)
|
|
188
|
+
console.log(`${arg1}: ${[...parts, `scope=${scope}`, `(${source})`].join(' ')}`)
|
|
116
189
|
}
|
|
117
190
|
} else {
|
|
118
191
|
// Treat as provider name
|
|
119
192
|
const entry = mo.getProviderRateLimit(arg1)
|
|
120
193
|
if (jsonFlag.json) {
|
|
121
|
-
jsonOutputOne({ provider: arg1, rpmLimit: entry?.rpmLimit || null, tpmLimit: entry?.tpmLimit || null })
|
|
194
|
+
jsonOutputOne({ provider: arg1, rpmLimit: entry?.rpmLimit || null, tpmLimit: entry?.tpmLimit || null, inpmLimit: entry?.inpmLimit || null })
|
|
122
195
|
return
|
|
123
196
|
}
|
|
124
197
|
if (!entry) {
|
|
@@ -127,6 +200,7 @@ Configuration:
|
|
|
127
200
|
const parts = []
|
|
128
201
|
if (entry.rpmLimit) parts.push(`rpm=${entry.rpmLimit}`)
|
|
129
202
|
if (entry.tpmLimit) parts.push(`tpm=${entry.tpmLimit}`)
|
|
203
|
+
if (entry.inpmLimit) parts.push(`inpm=${entry.inpmLimit}`)
|
|
130
204
|
console.log(`${arg1}: ${parts.join(' ')}`)
|
|
131
205
|
}
|
|
132
206
|
}
|
|
@@ -134,24 +208,21 @@ Configuration:
|
|
|
134
208
|
}
|
|
135
209
|
|
|
136
210
|
if (action === 'set') {
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
const
|
|
140
|
-
if (rpm == null && tpm == null) { console.error('Provide at least rpm or tpm'); process.exit(1) }
|
|
211
|
+
const usage = 'Usage: ratelimit set <model> <limit> <value> … | <rpm> [tpm]'
|
|
212
|
+
if (!arg1) { console.error(usage); process.exit(1) }
|
|
213
|
+
const limits = parseLimits(args.slice(2), usage)
|
|
141
214
|
const model = useModel(arg1)
|
|
142
|
-
const result = await model.setRateLimit(
|
|
143
|
-
|
|
144
|
-
if (result.rpmLimit) parts.push(`rpm=${result.rpmLimit}`)
|
|
145
|
-
if (result.tpmLimit) parts.push(`tpm=${result.tpmLimit}`)
|
|
146
|
-
console.log(`${arg1}: ${parts.join(' ')} scope=model`)
|
|
215
|
+
const result = await model.setRateLimit(limits)
|
|
216
|
+
console.log(`${arg1}: ${limitParts(result).join(' ')} scope=model`)
|
|
147
217
|
return
|
|
148
218
|
}
|
|
149
219
|
|
|
150
220
|
if (action === 'rm' || action === 'remove') {
|
|
151
|
-
if (!arg1) { console.error('Usage: ratelimit rm <model>'); process.exit(1) }
|
|
221
|
+
if (!arg1) { console.error('Usage: ratelimit rm <model> [limit …]'); process.exit(1) }
|
|
222
|
+
const names = parseLimitNames(args.slice(2))
|
|
152
223
|
const model = useModel(arg1)
|
|
153
|
-
await model.clearRateLimit()
|
|
154
|
-
console.log(`${arg1}:
|
|
224
|
+
const remaining = await model.clearRateLimit(names)
|
|
225
|
+
console.log(`${arg1}: ${clearedLine(names, limitParts(remaining))}`)
|
|
155
226
|
return
|
|
156
227
|
}
|
|
157
228
|
|
package/src/lib/index.js
CHANGED
|
@@ -26,6 +26,8 @@ export const version = createRequire(import.meta.url)('../../package.json').vers
|
|
|
26
26
|
|
|
27
27
|
const noop = () => {}
|
|
28
28
|
|
|
29
|
+
const LIMIT_FIELDS = ['rpmLimit', 'tpmLimit', 'inpmLimit']
|
|
30
|
+
|
|
29
31
|
// Verbosity tiers — controls which mohdel internal log lines fire.
|
|
30
32
|
//
|
|
31
33
|
// 0 Anomaly-only. Failures, throttling, deprecation, server lifecycle.
|
|
@@ -413,30 +415,31 @@ const mohdel = async ({ logger, verbosity: verbosityOpt, onSuccess, onFailure, c
|
|
|
413
415
|
return (providerName) => {
|
|
414
416
|
const entry = providersConfig[providerName]
|
|
415
417
|
if (!entry) return null
|
|
416
|
-
const { rpmLimit, tpmLimit } = entry
|
|
417
|
-
return (rpmLimit || tpmLimit) ? { rpmLimit, tpmLimit } : null
|
|
418
|
+
const { rpmLimit, tpmLimit, inpmLimit } = entry
|
|
419
|
+
return (rpmLimit || tpmLimit || inpmLimit) ? { rpmLimit, tpmLimit, inpmLimit } : null
|
|
418
420
|
}
|
|
419
421
|
}
|
|
420
422
|
|
|
421
423
|
if (prop === 'setProviderRateLimit') {
|
|
422
|
-
return async (providerName, { rpm, tpm } = {}) => {
|
|
424
|
+
return async (providerName, { rpm, tpm, inpm } = {}) => {
|
|
423
425
|
const entry = providersConfig[providerName] || (providersConfig[providerName] = {})
|
|
424
426
|
if (rpm != null) entry.rpmLimit = rpm
|
|
425
427
|
if (tpm != null) entry.tpmLimit = tpm
|
|
428
|
+
if (inpm != null) entry.inpmLimit = inpm
|
|
426
429
|
await saveProvidersConfig(providersConfig)
|
|
427
430
|
return entry
|
|
428
431
|
}
|
|
429
432
|
}
|
|
430
433
|
|
|
431
434
|
if (prop === 'clearProviderRateLimit') {
|
|
432
|
-
return async (providerName) => {
|
|
435
|
+
return async (providerName, names = []) => {
|
|
433
436
|
const entry = providersConfig[providerName]
|
|
434
|
-
if (entry)
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
437
|
+
if (!entry) return null
|
|
438
|
+
const fields = names.length ? names.map(n => `${n}Limit`) : LIMIT_FIELDS
|
|
439
|
+
for (const field of fields) delete entry[field]
|
|
440
|
+
if (Object.keys(entry).length === 0) delete providersConfig[providerName]
|
|
441
|
+
await saveProvidersConfig(providersConfig)
|
|
442
|
+
return providersConfig[providerName] || null
|
|
440
443
|
}
|
|
441
444
|
}
|
|
442
445
|
|
|
@@ -735,36 +738,38 @@ const createModelProxy = (resolvedModelId, modelSpec, handlers, aliasOutputEffor
|
|
|
735
738
|
return runAnswerEmbedding({
|
|
736
739
|
provider: modelSpec.provider,
|
|
737
740
|
model: modelSpec.model ?? resolvedModelId.split('/').pop(),
|
|
741
|
+
modelKey: resolvedModelId,
|
|
738
742
|
configuration,
|
|
739
743
|
input,
|
|
740
744
|
options,
|
|
741
745
|
spec: modelSpec
|
|
742
|
-
})
|
|
746
|
+
}, { limiter: rateLimiter, resolveProviderLimits })
|
|
743
747
|
}
|
|
744
748
|
}
|
|
745
749
|
|
|
746
750
|
if (prop === 'setRateLimit') {
|
|
747
|
-
return async ({ rpm, tpm } = {}) => {
|
|
751
|
+
return async ({ rpm, tpm, inpm } = {}) => {
|
|
748
752
|
const curatedCache = getCuratedCacheSnapshot()
|
|
749
753
|
const model = curatedCache[resolvedModelId] || (curatedCache[resolvedModelId] = { ...modelSpec })
|
|
750
754
|
if (rpm != null) model.rpmLimit = rpm
|
|
751
755
|
if (tpm != null) model.tpmLimit = tpm
|
|
756
|
+
if (inpm != null) model.inpmLimit = inpm
|
|
752
757
|
model.rateLimitScope = 'model'
|
|
753
758
|
await persistCuratedCache()
|
|
754
|
-
return { rpmLimit: model.rpmLimit, tpmLimit: model.tpmLimit }
|
|
759
|
+
return { rpmLimit: model.rpmLimit, tpmLimit: model.tpmLimit, inpmLimit: model.inpmLimit }
|
|
755
760
|
}
|
|
756
761
|
}
|
|
757
762
|
|
|
758
763
|
if (prop === 'clearRateLimit') {
|
|
759
|
-
return async () => {
|
|
764
|
+
return async (names = []) => {
|
|
760
765
|
const curatedCache = getCuratedCacheSnapshot()
|
|
761
766
|
const model = curatedCache[resolvedModelId]
|
|
762
|
-
if (model) {
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
}
|
|
767
|
+
if (!model) return {}
|
|
768
|
+
const fields = names.length ? names.map(n => `${n}Limit`) : LIMIT_FIELDS
|
|
769
|
+
for (const field of fields) delete model[field]
|
|
770
|
+
if (!LIMIT_FIELDS.some(field => model[field] != null)) delete model.rateLimitScope
|
|
771
|
+
await persistCuratedCache()
|
|
772
|
+
return { rpmLimit: model.rpmLimit, tpmLimit: model.tpmLimit, inpmLimit: model.inpmLimit }
|
|
768
773
|
}
|
|
769
774
|
}
|
|
770
775
|
|
package/src/lib/schema.js
CHANGED
|
@@ -69,6 +69,7 @@ const fieldDefs = {
|
|
|
69
69
|
suspended: { type: 'string' },
|
|
70
70
|
rpmLimit: { type: 'number' },
|
|
71
71
|
tpmLimit: { type: 'number' },
|
|
72
|
+
inpmLimit: { type: 'number' },
|
|
72
73
|
rateLimitScope: { type: 'string', validate: (v) => ['model', 'provider'].includes(v) ? null : 'must be "model" or "provider"' },
|
|
73
74
|
outputCapStrategy: { type: 'string', validate: (v) => ['error', 'accept'].includes(v) ? null : "must be 'error' or 'accept'" },
|
|
74
75
|
supportsTools: { type: 'boolean' },
|
package/src/lib/rate-limiter.js
DELETED
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
// Lightweight per-minute rate limiter.
|
|
2
|
-
// Tracks RPM and TPM with minute-bucket granularity.
|
|
3
|
-
// Throttles (delays) rather than rejects — returns ms to wait.
|
|
4
|
-
|
|
5
|
-
const createRateLimiter = () => {
|
|
6
|
-
// key → { count, tokens, minute }
|
|
7
|
-
const buckets = new Map()
|
|
8
|
-
|
|
9
|
-
const currentMinute = () => Math.floor(Date.now() / 60000)
|
|
10
|
-
|
|
11
|
-
const getBucket = (key) => {
|
|
12
|
-
const minute = currentMinute()
|
|
13
|
-
const bucket = buckets.get(key)
|
|
14
|
-
if (bucket && bucket.minute === minute) return bucket
|
|
15
|
-
const fresh = { count: 0, tokens: 0, minute }
|
|
16
|
-
buckets.set(key, fresh)
|
|
17
|
-
return fresh
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
const msUntilNextMinute = (minute) => Math.max(0, (minute + 1) * 60000 - Date.now())
|
|
21
|
-
|
|
22
|
-
// Returns ms to wait before sending (0 = go ahead)
|
|
23
|
-
const check = (key, { rpmLimit, tpmLimit } = {}) => {
|
|
24
|
-
if (!rpmLimit && !tpmLimit) return 0
|
|
25
|
-
const bucket = getBucket(key)
|
|
26
|
-
if (rpmLimit && bucket.count >= rpmLimit) {
|
|
27
|
-
return msUntilNextMinute(bucket.minute)
|
|
28
|
-
}
|
|
29
|
-
if (tpmLimit && bucket.tokens >= tpmLimit) {
|
|
30
|
-
return msUntilNextMinute(bucket.minute)
|
|
31
|
-
}
|
|
32
|
-
return 0
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
// Record a request count (call before sending — RPM tracking)
|
|
36
|
-
const recordRequest = (key) => {
|
|
37
|
-
const bucket = getBucket(key)
|
|
38
|
-
bucket.count++
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
// Record token usage (call after response — TPM tracking)
|
|
42
|
-
const recordTokens = (key, tokens) => {
|
|
43
|
-
const bucket = getBucket(key)
|
|
44
|
-
bucket.tokens += tokens
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
return { check, recordRequest, recordTokens }
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
export default createRateLimiter
|