modelmix 5.0.4 → 5.0.5
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 +218 -153
- package/index.d.ts +2 -0
- package/index.js +56 -0
- package/package.json +1 -1
- package/skills/modelmix/SKILL.md +11 -0
- package/test/fallback.test.js +34 -0
package/README.md
CHANGED
|
@@ -4,6 +4,25 @@
|
|
|
4
4
|
|
|
5
5
|
Ever found yourself wanting to integrate AI models into your projects but worried about reliability? ModelMix helps you build resilient AI applications by chaining multiple models together. If one model fails, it automatically switches to the next one, ensuring your application keeps running smoothly.
|
|
6
6
|
|
|
7
|
+
## 📑 Table of Contents
|
|
8
|
+
|
|
9
|
+
- [Features](#-features)
|
|
10
|
+
- [Usage](#-usage)
|
|
11
|
+
- [Shorthand Methods](#-shorthand-methods)
|
|
12
|
+
- [Unified Effort Scale](#-unified-effort-scale)
|
|
13
|
+
- [Templates](#-templates)
|
|
14
|
+
- [JSON Structured Output](#-json-structured-output)
|
|
15
|
+
- [Token Usage Tracking](#-token-usage-tracking)
|
|
16
|
+
- [Prompt Caching](#-prompt-caching)
|
|
17
|
+
- [Model Context Protocol (MCP) Integration](#-model-context-protocol-mcp-integration)
|
|
18
|
+
- [Enabling Debug Mode](#-enabling-debug-mode)
|
|
19
|
+
- [Bottleneck Integration](#-bottleneck-integration)
|
|
20
|
+
- [Retry (Opt-In)](#-retry-optin)
|
|
21
|
+
- [Instance Plugins](#-instance-plugins)
|
|
22
|
+
- [ModelMix Class Overview](#-modelmix-class-overview)
|
|
23
|
+
- [Contributing](#-contributing)
|
|
24
|
+
- [License](#-license)
|
|
25
|
+
|
|
7
26
|
## ✨ Features
|
|
8
27
|
|
|
9
28
|
- **Unified Interface**: Interact with multiple AI models through a single, coherent API.
|
|
@@ -76,6 +95,19 @@ const model = await ModelMix.new(setup)
|
|
|
76
95
|
console.log(await model.message());
|
|
77
96
|
```
|
|
78
97
|
|
|
98
|
+
The same ordered chain can be attached by passing model shortcuts directly to
|
|
99
|
+
`chain()`. Add `@effort` to override unified effort for one model; entries
|
|
100
|
+
without it inherit the chain effort, or use the provider default when the chain
|
|
101
|
+
has no configured effort:
|
|
102
|
+
|
|
103
|
+
```javascript
|
|
104
|
+
const model = ModelMix.new(setup)
|
|
105
|
+
.chain('sonnet5', 'gpt56luna@20', 'gemini37flash@-1')
|
|
106
|
+
.addText("What's your name?");
|
|
107
|
+
|
|
108
|
+
console.log(await model.message());
|
|
109
|
+
```
|
|
110
|
+
|
|
79
111
|
**Use Perplexity to get the price of ETH**
|
|
80
112
|
```javascript
|
|
81
113
|
const ETH = ModelMix.new()
|
|
@@ -101,159 +133,6 @@ This pattern allows you to:
|
|
|
101
133
|
- Track token usage across all providers
|
|
102
134
|
- Keep your code clean and maintainable
|
|
103
135
|
|
|
104
|
-
## 🔌 Instance Plugins
|
|
105
|
-
|
|
106
|
-
Plugins wrap one ModelMix instance without changing global behavior. They run in registration order after templates are rendered and before provider-specific request conversion:
|
|
107
|
-
|
|
108
|
-
```javascript
|
|
109
|
-
const metrics = {
|
|
110
|
-
name: 'metrics',
|
|
111
|
-
async execute(context, next) {
|
|
112
|
-
const startedAt = Date.now();
|
|
113
|
-
const result = await next();
|
|
114
|
-
return { ...result, elapsedMs: Date.now() - startedAt };
|
|
115
|
-
}
|
|
116
|
-
};
|
|
117
|
-
|
|
118
|
-
const model = ModelMix.new()
|
|
119
|
-
.gpt56luna()
|
|
120
|
-
.use(metrics)
|
|
121
|
-
.addText('Summarize this request.');
|
|
122
|
-
```
|
|
123
|
-
|
|
124
|
-
A plugin may edit `context.request`, call `next()`, or return a complete ModelMix result itself. It can also create history-free child executions with `context.invoke()` and choose plugin inheritance:
|
|
125
|
-
|
|
126
|
-
```javascript
|
|
127
|
-
const child = await context.invoke({
|
|
128
|
-
systemFile: './prompts/extract-entities.md',
|
|
129
|
-
assign: { outputLanguage: 'Spanish' },
|
|
130
|
-
messages: [{ role: 'user', content: section }],
|
|
131
|
-
plugins: { exclude: ['recursive-plugin'] },
|
|
132
|
-
history: false
|
|
133
|
-
});
|
|
134
|
-
```
|
|
135
|
-
|
|
136
|
-
Supported policies are `'inherit'`, `'none'`, `{ include: [...] }`, and `{ exclude: [...] }`. Child metadata exposes `executionId`, `parentExecutionId`, and `depth` to middleware. `.new()` inherits registered plugins but not message history.
|
|
137
|
-
|
|
138
|
-
Child `systemFile` templates use the same EJS engine, `assign()` data contract, and relative Markdown includes as ordinary ModelMix templates. Use either `system` or `systemFile`, not both.
|
|
139
|
-
|
|
140
|
-
### Recursive Language Model plugin
|
|
141
|
-
|
|
142
|
-
The separately publishable `@modelmix/rlm` workspace package keeps document parsing, planner prompts, and `isolated-vm` out of the core `modelmix` dependency tree. It requires Node.js 22 or newer.
|
|
143
|
-
|
|
144
|
-
```javascript
|
|
145
|
-
const { ModelMix } = require('modelmix');
|
|
146
|
-
const { rlm } = require('@modelmix/rlm');
|
|
147
|
-
|
|
148
|
-
const fast = ModelMix.new().gpt41mini();
|
|
149
|
-
|
|
150
|
-
const result = await ModelMix.new()
|
|
151
|
-
.gpt56luna()
|
|
152
|
-
.use(rlm({
|
|
153
|
-
maxDepth: 2,
|
|
154
|
-
documents: {
|
|
155
|
-
book: {
|
|
156
|
-
format: 'markdown',
|
|
157
|
-
content: markdownBook
|
|
158
|
-
}
|
|
159
|
-
},
|
|
160
|
-
workers: {
|
|
161
|
-
fast: {
|
|
162
|
-
model: fast,
|
|
163
|
-
intelligence: 2,
|
|
164
|
-
cost: 1,
|
|
165
|
-
speed: 4,
|
|
166
|
-
description: 'Translation, extraction, and simple transformations'
|
|
167
|
-
}
|
|
168
|
-
},
|
|
169
|
-
limits: {
|
|
170
|
-
maxQueryBytes: 64 * 1024,
|
|
171
|
-
sandboxMemoryBytes: 64 * 1024 * 1024,
|
|
172
|
-
maxConcurrentQueries: 4,
|
|
173
|
-
maxCalls: 100,
|
|
174
|
-
maxOutputBytes: 8 * 1024 * 1024,
|
|
175
|
-
maxGeneratedTokens: 100000,
|
|
176
|
-
maxWallTimeMs: 120000
|
|
177
|
-
}
|
|
178
|
-
}))
|
|
179
|
-
.addText('Translate this book to neutral Latin American Spanish.')
|
|
180
|
-
.message();
|
|
181
|
-
```
|
|
182
|
-
|
|
183
|
-
Markdown headings become stable nested sections, lists expose item arrays, and the original source order remains reconstructable. The planner receives only a content-free variable manifest: paths, types, array item counts, serialized UTF-8 byte estimates, string lengths, line and paragraph counts, structural summaries, and partition hints. The document values enter only the isolated sandbox, where generated JavaScript can inspect `variables` and call registered workers through `query()`.
|
|
184
|
-
|
|
185
|
-
A worker normally supplies `model: anotherModelMixInstance`. To offer the current parent chain under a name, register it with `useParent: true` instead; defining both is rejected.
|
|
186
|
-
|
|
187
|
-
Planner instructions live in Markdown templates under `plugins/rlm/prompts/`. The plugin supplies manifests and limits through ModelMix `assign()` and loads the system prompt through `systemFile`, so relative includes work and runtime values are rendered exactly once.
|
|
188
|
-
|
|
189
|
-
## 🎛️ Unified Effort Scale
|
|
190
|
-
|
|
191
|
-
Control reasoning depth with one ModelMix policy value (`-1` adaptive, or `0`–`100`). It lives **outside** native `options` and is mapped to each provider’s effort API at request time.
|
|
192
|
-
|
|
193
|
-
```javascript
|
|
194
|
-
// In config (ModelMix.new or per-model shorthand)
|
|
195
|
-
ModelMix.new({ config: { effort: 50 } }).opus5().addText('...').message();
|
|
196
|
-
ModelMix.new().deepseekV4Flash({ config: { effort: 100 } }).addText('...').message();
|
|
197
|
-
|
|
198
|
-
// Fluent
|
|
199
|
-
ModelMix.new().effort(-1).minimaxM3().addText('...').message();
|
|
200
|
-
```
|
|
201
|
-
|
|
202
|
-
**Native wins:** if you already set a provider-native field (`reasoning_effort`, `output_config.effort`, `thinkingConfig`, etc.), unified `effort` is ignored for that request.
|
|
203
|
-
|
|
204
|
-
| | 0–19 | 20–39 | 40–59 | 60–79 | 80–100 | `-1` |
|
|
205
|
-
|--|------|-------|-------|-------|--------|------|
|
|
206
|
-
| OpenAI | `none` | `low` | `medium` | `high` | `xhigh` | — |
|
|
207
|
-
| Anthropic | `low` | `medium` | `high` | `xhigh` | `max` | adaptive |
|
|
208
|
-
| Gemini 3+ | `minimal` | `low` | `medium` | `high` | — | dynamic |
|
|
209
|
-
| DeepSeek V4 | off | `low`↑ | `high`↑ | `high`↑ | `max`↑ | — |
|
|
210
|
-
| MiniMax M3 | off | adaptive | adaptive | adaptive | adaptive | adaptive |
|
|
211
|
-
|
|
212
|
-
### Provider-specific behavior
|
|
213
|
-
|
|
214
|
-
- **Gemini:** Gemini 3+ uses bands 0–24 / 25–49 / 50–74 / 75–100. Gemini 3.7 Flash clamps these bands to `low` / `low` / `medium` / `high`; `-1` leaves its native `medium` default unchanged. Gemini 2.5 maps 0–100 to `thinkingBudget`.
|
|
215
|
-
- **DeepSeek:** `↑` means thinking is enabled; `off` means it is disabled.
|
|
216
|
-
- **MiniMax:** `off` maps to `thinking.disabled`; `adaptive` maps to `thinking.type=adaptive`.
|
|
217
|
-
- **Anthropic:** Claude 5, Fable, Opus 4.6+, and Sonnet 4.6+ use adaptive thinking with `output_config.effort`. Sonnet 4.5 and Haiku 4.5 use `thinking.type=enabled` with `budget_tokens`.
|
|
218
|
-
- **Grok 4.6:** 0–39 / 40–59 / 60–79 / 80–100 map to `low` / `medium` / `high` / `xhigh`. Without effort, Grok uses its native `high` default.
|
|
219
|
-
|
|
220
|
-
`-1` uses the provider's adaptive or dynamic mode when available; otherwise it is a no-op. Effort levels are clamped to each model's supported range.
|
|
221
|
-
|
|
222
|
-
### Migrating from thinking shorthands
|
|
223
|
-
|
|
224
|
-
The former `*think()` methods were removed. Use `.effort(n).<model>()` with `0`–`100` or `-1` instead.
|
|
225
|
-
|
|
226
|
-
- **Kimi:** use `kimiK25()` or `kimiK26()`.
|
|
227
|
-
- **Grok 4.20:** `.grok420()` selects the non-reasoning model. Use `.effort(20+).grok420()` or `.effort(-1).grok420()` to select the reasoning model.
|
|
228
|
-
|
|
229
|
-
## 🔧 Model Context Protocol (MCP) Integration
|
|
230
|
-
|
|
231
|
-
ModelMix makes it incredibly easy to enhance your AI models with powerful capabilities through the Model Context Protocol. With just a few lines of code, you can add features like web search, code execution, or any custom functionality to your models.
|
|
232
|
-
|
|
233
|
-
### Example: Adding Web Search Capability
|
|
234
|
-
|
|
235
|
-
Include the API key for Brave Search in your .env file.
|
|
236
|
-
```
|
|
237
|
-
BRAVE_API_KEY="BSA0..._fm"
|
|
238
|
-
```
|
|
239
|
-
|
|
240
|
-
```javascript
|
|
241
|
-
const mmix = ModelMix.new({ config: { max_history: 10 } }).gpt56sol();
|
|
242
|
-
mmix.setSystem('You are an assistant and today is ' + new Date().toISOString());
|
|
243
|
-
|
|
244
|
-
// Add web search capability through MCP
|
|
245
|
-
await mmix.addMCP('@modelcontextprotocol/server-brave-search');
|
|
246
|
-
mmix.addText('Use Internet: When did the last Christian pope die?');
|
|
247
|
-
console.log(await mmix.message());
|
|
248
|
-
```
|
|
249
|
-
|
|
250
|
-
This simple integration allows your model to:
|
|
251
|
-
- Search the web in real-time
|
|
252
|
-
- Access up-to-date information
|
|
253
|
-
- Combine AI reasoning with external data
|
|
254
|
-
|
|
255
|
-
The Model Context Protocol makes it easy to add any capability to your models, from web search to code execution, database queries, or custom functions. All with just a few lines of code!
|
|
256
|
-
|
|
257
136
|
## ⚡️ Shorthand Methods
|
|
258
137
|
|
|
259
138
|
ModelMix provides convenient shorthand methods for quickly accessing different AI models.
|
|
@@ -339,6 +218,46 @@ const result = await ModelMix.new({
|
|
|
339
218
|
.message();
|
|
340
219
|
```
|
|
341
220
|
|
|
221
|
+
## 🎛️ Unified Effort Scale
|
|
222
|
+
|
|
223
|
+
Control reasoning depth with one ModelMix policy value (`-1` adaptive, or `0`–`100`). It lives **outside** native `options` and is mapped to each provider’s effort API at request time.
|
|
224
|
+
|
|
225
|
+
```javascript
|
|
226
|
+
// In config (ModelMix.new or per-model shorthand)
|
|
227
|
+
ModelMix.new({ config: { effort: 50 } }).opus5().addText('...').message();
|
|
228
|
+
ModelMix.new().deepseekV4Flash({ config: { effort: 100 } }).addText('...').message();
|
|
229
|
+
|
|
230
|
+
// Fluent
|
|
231
|
+
ModelMix.new().effort(-1).minimaxM3().addText('...').message();
|
|
232
|
+
```
|
|
233
|
+
|
|
234
|
+
**Native wins:** if you already set a provider-native field (`reasoning_effort`, `output_config.effort`, `thinkingConfig`, etc.), unified `effort` is ignored for that request.
|
|
235
|
+
|
|
236
|
+
| | 0–19 | 20–39 | 40–59 | 60–79 | 80–100 | `-1` |
|
|
237
|
+
|--|------|-------|-------|-------|--------|------|
|
|
238
|
+
| OpenAI | `none` | `low` | `medium` | `high` | `xhigh` | — |
|
|
239
|
+
| Anthropic | `low` | `medium` | `high` | `xhigh` | `max` | adaptive |
|
|
240
|
+
| Gemini 3+ | `minimal` | `low` | `medium` | `high` | — | dynamic |
|
|
241
|
+
| DeepSeek V4 | off | `low`↑ | `high`↑ | `high`↑ | `max`↑ | — |
|
|
242
|
+
| MiniMax M3 | off | adaptive | adaptive | adaptive | adaptive | adaptive |
|
|
243
|
+
|
|
244
|
+
### Provider-specific behavior
|
|
245
|
+
|
|
246
|
+
- **Gemini:** Gemini 3+ uses bands 0–24 / 25–49 / 50–74 / 75–100. Gemini 3.7 Flash clamps these bands to `low` / `low` / `medium` / `high`; `-1` leaves its native `medium` default unchanged. Gemini 2.5 maps 0–100 to `thinkingBudget`.
|
|
247
|
+
- **DeepSeek:** `↑` means thinking is enabled; `off` means it is disabled.
|
|
248
|
+
- **MiniMax:** `off` maps to `thinking.disabled`; `adaptive` maps to `thinking.type=adaptive`.
|
|
249
|
+
- **Anthropic:** Claude 5, Fable, Opus 4.6+, and Sonnet 4.6+ use adaptive thinking with `output_config.effort`. Sonnet 4.5 and Haiku 4.5 use `thinking.type=enabled` with `budget_tokens`.
|
|
250
|
+
- **Grok 4.6:** 0–39 / 40–59 / 60–79 / 80–100 map to `low` / `medium` / `high` / `xhigh`. Without effort, Grok uses its native `high` default.
|
|
251
|
+
|
|
252
|
+
`-1` uses the provider's adaptive or dynamic mode when available; otherwise it is a no-op. Effort levels are clamped to each model's supported range.
|
|
253
|
+
|
|
254
|
+
### Migrating from thinking shorthands
|
|
255
|
+
|
|
256
|
+
The former `*think()` methods were removed. Use `.effort(n).<model>()` with `0`–`100` or `-1` instead.
|
|
257
|
+
|
|
258
|
+
- **Kimi:** use `kimiK25()` or `kimiK26()`.
|
|
259
|
+
- **Grok 4.20:** `.grok420()` selects the non-reasoning model. Use `.effort(20+).grok420()` or `.effort(-1).grok420()` to select the reasoning model.
|
|
260
|
+
|
|
342
261
|
## 🔄 Templates
|
|
343
262
|
|
|
344
263
|
ModelMix renders system prompts and user messages with [EJS](https://ejs.co/). Templates can be inline or stored in external files, and support variables, conditionals, loops, and relative includes.
|
|
@@ -800,6 +719,39 @@ console.log(model.lastRaw.tokens);
|
|
|
800
719
|
|
|
801
720
|
`thinking` contains internal reasoning tokens when a provider reports them separately; cost calculation bills them at the output rate. `cached` aggregates cache reads reported by the provider, while `cacheWrite` aggregates cache writes. Anthropic additionally exposes `cacheWrite5m` and `cacheWrite1h` because those writes cost 1.25× and 2× the normal input rate, respectively. `cacheSavings` compares cache reads with the normal input rate, `cacheWritePremium` compares writes with that rate, and `breakEvenHits` estimates how many complete future hits recover the current write premium. For Anthropic, `input` is normalized to include uncached input, cache reads, and cache writes. Missing usage or pricing categories return `0`. The `speed` field is the generation speed measured in output tokens per second (integer).
|
|
802
721
|
|
|
722
|
+
## 🧠 Prompt Caching
|
|
723
|
+
|
|
724
|
+
Prompt caching reuses the stable beginning of a prompt at the provider level. It does not cache the answer: every call still generates a new response.
|
|
725
|
+
|
|
726
|
+
For GPT-5.6, keep the long, reusable instructions first, mark the end of that stable prefix, and add the changing request afterward:
|
|
727
|
+
|
|
728
|
+
```javascript
|
|
729
|
+
async function ask(question) {
|
|
730
|
+
const model = ModelMix.new()
|
|
731
|
+
.gpt56luna({
|
|
732
|
+
options: {
|
|
733
|
+
prompt_cache_key: 'support-rules-v1',
|
|
734
|
+
prompt_cache_options: { mode: 'explicit', ttl: '30m' }
|
|
735
|
+
}
|
|
736
|
+
})
|
|
737
|
+
.addTextFromFile('./prompts/support.md', {
|
|
738
|
+
role: 'developer',
|
|
739
|
+
cache: { breakpoint: true }
|
|
740
|
+
})
|
|
741
|
+
.addText(question);
|
|
742
|
+
|
|
743
|
+
const answer = await model.message();
|
|
744
|
+
const { cached, cacheWrite, cacheHitRate } = model.lastRaw.tokens;
|
|
745
|
+
console.log({ cached, cacheWrite, cacheHitRate });
|
|
746
|
+
return answer;
|
|
747
|
+
}
|
|
748
|
+
|
|
749
|
+
await ask('Summarize support ticket 123.');
|
|
750
|
+
await ask('Summarize support ticket 456.');
|
|
751
|
+
```
|
|
752
|
+
|
|
753
|
+
The contents of `support.md` and the cache key stay the same between calls; only the final question changes. The first request may report `cacheWrite > 0`, while later requests confirm reuse with `cached > 0`. For GPT-5.6, the stable prefix must contain at least 1,024 tokens. Keep all variable content after the breakpoint, and change `prompt_cache_key` when the stable instructions change.
|
|
754
|
+
|
|
803
755
|
### GPT-5.6 prompt caching
|
|
804
756
|
|
|
805
757
|
GPT-5.6 supports implicit or explicit caching through `prompt_cache_options`. Put the explicit breakpoint at the end of the stable prefix; the provider only caches prompts with at least 1,024 tokens.
|
|
@@ -856,6 +808,34 @@ const model = ModelMix.new()
|
|
|
856
808
|
|
|
857
809
|
GPT-5.6 receives `prompt_cache_breakpoint`; Anthropic receives `cache_control`; older OpenAI models and providers without an equivalent omit the marker. When a neutral explicit breakpoint is present for Anthropic, its model-scoped `cache_control` becomes that block's policy instead of adding an automatic breakpoint after the variable suffix.
|
|
858
810
|
|
|
811
|
+
## 🔧 Model Context Protocol (MCP) Integration
|
|
812
|
+
|
|
813
|
+
ModelMix makes it incredibly easy to enhance your AI models with powerful capabilities through the Model Context Protocol. With just a few lines of code, you can add features like web search, code execution, or any custom functionality to your models.
|
|
814
|
+
|
|
815
|
+
### Example: Adding Web Search Capability
|
|
816
|
+
|
|
817
|
+
Include the API key for Brave Search in your .env file.
|
|
818
|
+
```
|
|
819
|
+
BRAVE_API_KEY="BSA0..._fm"
|
|
820
|
+
```
|
|
821
|
+
|
|
822
|
+
```javascript
|
|
823
|
+
const mmix = ModelMix.new({ config: { max_history: 10 } }).gpt56sol();
|
|
824
|
+
mmix.setSystem('You are an assistant and today is ' + new Date().toISOString());
|
|
825
|
+
|
|
826
|
+
// Add web search capability through MCP
|
|
827
|
+
await mmix.addMCP('@modelcontextprotocol/server-brave-search');
|
|
828
|
+
mmix.addText('Use Internet: When did the last Christian pope die?');
|
|
829
|
+
console.log(await mmix.message());
|
|
830
|
+
```
|
|
831
|
+
|
|
832
|
+
This simple integration allows your model to:
|
|
833
|
+
- Search the web in real-time
|
|
834
|
+
- Access up-to-date information
|
|
835
|
+
- Combine AI reasoning with external data
|
|
836
|
+
|
|
837
|
+
The Model Context Protocol makes it easy to add any capability to your models, from web search to code execution, database queries, or custom functions. All with just a few lines of code!
|
|
838
|
+
|
|
859
839
|
## 🐛 Enabling Debug Mode
|
|
860
840
|
|
|
861
841
|
To activate debug mode in ModelMix and view detailed request information, follow these two steps:
|
|
@@ -929,6 +909,91 @@ Behavior summary:
|
|
|
929
909
|
- If retry is enabled, ModelMix retries the same model only for configured transient status codes.
|
|
930
910
|
- After retries are exhausted (or for non-retryable errors), ModelMix continues with normal fallback chain.
|
|
931
911
|
|
|
912
|
+
## 🔌 Instance Plugins
|
|
913
|
+
|
|
914
|
+
Plugins wrap one ModelMix instance without changing global behavior. They run in registration order after templates are rendered and before provider-specific request conversion:
|
|
915
|
+
|
|
916
|
+
```javascript
|
|
917
|
+
const metrics = {
|
|
918
|
+
name: 'metrics',
|
|
919
|
+
async execute(context, next) {
|
|
920
|
+
const startedAt = Date.now();
|
|
921
|
+
const result = await next();
|
|
922
|
+
return { ...result, elapsedMs: Date.now() - startedAt };
|
|
923
|
+
}
|
|
924
|
+
};
|
|
925
|
+
|
|
926
|
+
const model = ModelMix.new()
|
|
927
|
+
.gpt56luna()
|
|
928
|
+
.use(metrics)
|
|
929
|
+
.addText('Summarize this request.');
|
|
930
|
+
```
|
|
931
|
+
|
|
932
|
+
A plugin may edit `context.request`, call `next()`, or return a complete ModelMix result itself. It can also create history-free child executions with `context.invoke()` and choose plugin inheritance:
|
|
933
|
+
|
|
934
|
+
```javascript
|
|
935
|
+
const child = await context.invoke({
|
|
936
|
+
systemFile: './prompts/extract-entities.md',
|
|
937
|
+
assign: { outputLanguage: 'Spanish' },
|
|
938
|
+
messages: [{ role: 'user', content: section }],
|
|
939
|
+
plugins: { exclude: ['recursive-plugin'] },
|
|
940
|
+
history: false
|
|
941
|
+
});
|
|
942
|
+
```
|
|
943
|
+
|
|
944
|
+
Supported policies are `'inherit'`, `'none'`, `{ include: [...] }`, and `{ exclude: [...] }`. Child metadata exposes `executionId`, `parentExecutionId`, and `depth` to middleware. `.new()` inherits registered plugins but not message history.
|
|
945
|
+
|
|
946
|
+
Child `systemFile` templates use the same EJS engine, `assign()` data contract, and relative Markdown includes as ordinary ModelMix templates. Use either `system` or `systemFile`, not both.
|
|
947
|
+
|
|
948
|
+
### Recursive Language Model plugin
|
|
949
|
+
|
|
950
|
+
The separately publishable `@modelmix/rlm` workspace package keeps document parsing, planner prompts, and `isolated-vm` out of the core `modelmix` dependency tree. It requires Node.js 22 or newer.
|
|
951
|
+
|
|
952
|
+
```javascript
|
|
953
|
+
const { ModelMix } = require('modelmix');
|
|
954
|
+
const { rlm } = require('@modelmix/rlm');
|
|
955
|
+
|
|
956
|
+
const fast = ModelMix.new().gpt41mini();
|
|
957
|
+
|
|
958
|
+
const result = await ModelMix.new()
|
|
959
|
+
.gpt56luna()
|
|
960
|
+
.use(rlm({
|
|
961
|
+
maxDepth: 2,
|
|
962
|
+
documents: {
|
|
963
|
+
book: {
|
|
964
|
+
format: 'markdown',
|
|
965
|
+
content: markdownBook
|
|
966
|
+
}
|
|
967
|
+
},
|
|
968
|
+
workers: {
|
|
969
|
+
fast: {
|
|
970
|
+
model: fast,
|
|
971
|
+
intelligence: 2,
|
|
972
|
+
cost: 1,
|
|
973
|
+
speed: 4,
|
|
974
|
+
description: 'Translation, extraction, and simple transformations'
|
|
975
|
+
}
|
|
976
|
+
},
|
|
977
|
+
limits: {
|
|
978
|
+
maxQueryBytes: 64 * 1024,
|
|
979
|
+
sandboxMemoryBytes: 64 * 1024 * 1024,
|
|
980
|
+
maxConcurrentQueries: 4,
|
|
981
|
+
maxCalls: 100,
|
|
982
|
+
maxOutputBytes: 8 * 1024 * 1024,
|
|
983
|
+
maxGeneratedTokens: 100000,
|
|
984
|
+
maxWallTimeMs: 120000
|
|
985
|
+
}
|
|
986
|
+
}))
|
|
987
|
+
.addText('Translate this book to neutral Latin American Spanish.')
|
|
988
|
+
.message();
|
|
989
|
+
```
|
|
990
|
+
|
|
991
|
+
Markdown headings become stable nested sections, lists expose item arrays, and the original source order remains reconstructable. The planner receives only a content-free variable manifest: paths, types, array item counts, serialized UTF-8 byte estimates, string lengths, line and paragraph counts, structural summaries, and partition hints. The document values enter only the isolated sandbox, where generated JavaScript can inspect `variables` and call registered workers through `query()`.
|
|
992
|
+
|
|
993
|
+
A worker normally supplies `model: anotherModelMixInstance`. To offer the current parent chain under a name, register it with `useParent: true` instead; defining both is rejected.
|
|
994
|
+
|
|
995
|
+
Planner instructions live in Markdown templates under `plugins/rlm/prompts/`. The plugin supplies manifests and limits through ModelMix `assign()` and loads the system prompt through `systemFile`, so relative includes work and runtime values are rendered exactly once.
|
|
996
|
+
|
|
932
997
|
## 📚 ModelMix Class Overview
|
|
933
998
|
|
|
934
999
|
```javascript
|
package/index.d.ts
CHANGED
|
@@ -436,6 +436,8 @@ export declare class ModelMix {
|
|
|
436
436
|
assign(keyValues: Record<string, unknown>): this;
|
|
437
437
|
assignKey(key: string, value: unknown): this;
|
|
438
438
|
effort(value: EffortValue): this;
|
|
439
|
+
/** Attach an ordered model chain. Use `shortcut@effort` for a per-model override. */
|
|
440
|
+
chain(...modelSpecs: string[]): this;
|
|
439
441
|
attach(key: string, provider: MixCustom): this;
|
|
440
442
|
|
|
441
443
|
// OpenAI
|
package/index.js
CHANGED
|
@@ -401,6 +401,50 @@ const MODEL_PRICING = {
|
|
|
401
401
|
'zai-glm-4.7': { input: 0.55, output: 2.19 },
|
|
402
402
|
};
|
|
403
403
|
|
|
404
|
+
const CHAIN_MODEL_SHORTCUTS = new Set([
|
|
405
|
+
'gpt41', 'gpt41mini', 'gpt41nano', 'gpt5', 'gpt5mini', 'gpt5nano',
|
|
406
|
+
'gpt51', 'gpt52', 'gpt54', 'gpt54mini', 'gpt54nano', 'gpt54pro',
|
|
407
|
+
'gpt55', 'gpt55pro', 'gpt56sol', 'gpt56terra', 'gpt56luna',
|
|
408
|
+
'gptRealtime', 'gptRealtimeMini', 'gpt53codex', 'gpt53chat', 'gptOss',
|
|
409
|
+
'fable50', 'fable5', 'opus50', 'opus5', 'opus48', 'opus47', 'opus46',
|
|
410
|
+
'sonnet50', 'sonnet5', 'sonnet46', 'sonnet45', 'haiku45',
|
|
411
|
+
'gemini25flash', 'gemini31pro', 'gemini3pro', 'gemini3flash',
|
|
412
|
+
'gemini37flash', 'gemini36flash', 'gemini35flash', 'gemini35flashLite',
|
|
413
|
+
'gemini31flashLite', 'gemini25pro', 'sonarPro', 'sonar',
|
|
414
|
+
'grok46', 'grok45', 'grok43', 'grok420multiAgent', 'grok420',
|
|
415
|
+
'qwen3', 'qwen36plus', 'qwen37plus', 'qwen38max', 'hermes3',
|
|
416
|
+
'kimiK26', 'kimiK27Code', 'kimiK3', 'kimiK25',
|
|
417
|
+
'minimaxM25', 'minimaxM27', 'minimaxM3', 'mimo25', 'mimo25pro',
|
|
418
|
+
'deepseekV4Pro', 'deepseekV4Flash', 'GLM51', 'GLM52'
|
|
419
|
+
]);
|
|
420
|
+
|
|
421
|
+
function parseChainModels(modelSpecs) {
|
|
422
|
+
if (modelSpecs.length === 0) {
|
|
423
|
+
throw new TypeError('chain() requires at least one model shortcut string.');
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
return modelSpecs.map((modelSpec, index) => {
|
|
427
|
+
if (typeof modelSpec !== 'string') {
|
|
428
|
+
throw new TypeError(`Invalid chain model at index ${index}: expected a model shortcut string.`);
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
const match = /^([A-Za-z_$][A-Za-z0-9_$]*)(?:@(-?\d+))?$/.exec(modelSpec);
|
|
432
|
+
if (!match) {
|
|
433
|
+
throw new TypeError(`Invalid chain model "${modelSpec}": expected "shortcut" or "shortcut@effort".`);
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
const shortcut = match[1];
|
|
437
|
+
if (!CHAIN_MODEL_SHORTCUTS.has(shortcut)) {
|
|
438
|
+
throw new Error(`Unknown model shortcut "${shortcut}" in chain().`);
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
return {
|
|
442
|
+
shortcut,
|
|
443
|
+
effort: match[2] === undefined ? undefined : normalizeEffort(Number(match[2]))
|
|
444
|
+
};
|
|
445
|
+
});
|
|
446
|
+
}
|
|
447
|
+
|
|
404
448
|
class ModelMix {
|
|
405
449
|
|
|
406
450
|
constructor({ options = {}, config = {}, mix = {} } = {}) {
|
|
@@ -483,6 +527,18 @@ class ModelMix {
|
|
|
483
527
|
return this;
|
|
484
528
|
}
|
|
485
529
|
|
|
530
|
+
chain(...modelSpecs) {
|
|
531
|
+
const models = parseChainModels(modelSpecs);
|
|
532
|
+
for (const { shortcut, effort } of models) {
|
|
533
|
+
if (effort === undefined) {
|
|
534
|
+
this[shortcut]();
|
|
535
|
+
} else {
|
|
536
|
+
this[shortcut]({ config: { effort } });
|
|
537
|
+
}
|
|
538
|
+
}
|
|
539
|
+
return this;
|
|
540
|
+
}
|
|
541
|
+
|
|
486
542
|
use(plugin) {
|
|
487
543
|
if (!isPlainObject(plugin)) {
|
|
488
544
|
throw new TypeError('plugin must be a plain object.');
|
package/package.json
CHANGED
package/skills/modelmix/SKILL.md
CHANGED
|
@@ -92,6 +92,17 @@ const model = ModelMix.new()
|
|
|
92
92
|
|
|
93
93
|
If `sonnet46` fails, it automatically tries `gpt52`, then `gemini3flash`.
|
|
94
94
|
|
|
95
|
+
The equivalent `chain()` form accepts public shortcut names directly in the
|
|
96
|
+
same order. Append `@effort` for a per-model unified effort override (`-1` or
|
|
97
|
+
`0`–`100`). Without the suffix, the entry inherits `config.effort`, or keeps the
|
|
98
|
+
provider default when no chain effort is configured:
|
|
99
|
+
|
|
100
|
+
```javascript
|
|
101
|
+
const model = ModelMix.new()
|
|
102
|
+
.chain('sonnet46', 'gpt52@20', 'gemini3flash@-1')
|
|
103
|
+
.addText('Hello!');
|
|
104
|
+
```
|
|
105
|
+
|
|
95
106
|
### Instance plugins
|
|
96
107
|
|
|
97
108
|
Register middleware with `.use({ name, execute })`. Plugins are scoped to the instance, run in registration order, and receive the rendered provider-neutral request. They may edit `context.request`, call `next()`, or return a complete ModelMix result.
|
package/test/fallback.test.js
CHANGED
|
@@ -26,6 +26,40 @@ describe('Provider Fallback Chain Tests', () => {
|
|
|
26
26
|
});
|
|
27
27
|
});
|
|
28
28
|
|
|
29
|
+
it('should attach chain shortcuts from arguments with optional per-model effort', () => {
|
|
30
|
+
model.chain('sonnet5', 'gpt56luna@20', 'gemini37flash@-1');
|
|
31
|
+
|
|
32
|
+
expect(model.models.map(({ key }) => key)).to.deep.equal([
|
|
33
|
+
'claude-sonnet-5',
|
|
34
|
+
'gpt-5.6-luna',
|
|
35
|
+
'gemini-3.7-flash'
|
|
36
|
+
]);
|
|
37
|
+
expect(model.models[0].provider.config).to.not.have.property('effort');
|
|
38
|
+
expect(model.models[1].provider.config.effort).to.equal(20);
|
|
39
|
+
expect(model.models[2].provider.config.effort).to.equal(-1);
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
it('should keep the chain default effort when an entry omits it', () => {
|
|
43
|
+
model.effort(60).chain('gpt56luna', 'sonnet5@20');
|
|
44
|
+
|
|
45
|
+
expect(model.config.effort).to.equal(60);
|
|
46
|
+
expect(model.models[0].provider.config).to.not.have.property('effort');
|
|
47
|
+
expect(model.models[1].provider.config.effort).to.equal(20);
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
it('should reject invalid chains before attaching any model', () => {
|
|
51
|
+
expect(() => model.chain('gpt56luna', 'unknownModel'))
|
|
52
|
+
.to.throw('Unknown model shortcut "unknownModel" in chain().');
|
|
53
|
+
expect(model.models).to.have.length(0);
|
|
54
|
+
|
|
55
|
+
expect(() => model.chain('gpt56luna@101'))
|
|
56
|
+
.to.throw(/Invalid effort/);
|
|
57
|
+
expect(() => model.chain())
|
|
58
|
+
.to.throw('chain() requires at least one model shortcut string.');
|
|
59
|
+
expect(() => model.chain(['gpt56luna']))
|
|
60
|
+
.to.throw('Invalid chain model at index 0: expected a model shortcut string.');
|
|
61
|
+
});
|
|
62
|
+
|
|
29
63
|
it('should use primary provider when available', async () => {
|
|
30
64
|
model.gpt5mini().sonnet46().addText('Hello');
|
|
31
65
|
|