pi-blablador 0.1.2 → 0.1.4

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.
Files changed (3) hide show
  1. package/README.md +23 -3
  2. package/blablador.ts +69 -1
  3. package/package.json +7 -3
package/README.md CHANGED
@@ -34,6 +34,12 @@ which is the fastest way to tell a missing token from a throttled one.
34
34
 
35
35
  Needs Pi 1.4.0 or newer — the only version this was tested against.
36
36
 
37
+ ## Web search
38
+
39
+ `web_search` searches through Blablador's anonymous SearXNG instance, so it needs no extra key. It
40
+ takes a `query` and an optional `category` (`general`, `news`, `science`, `it`), and returns six
41
+ results with title, URL, and snippet.
42
+
37
43
  ## Thinking levels
38
44
 
39
45
  Every backend has its own `reasoning_effort` vocabulary and answers HTTP 400 for anything outside it,
@@ -46,23 +52,37 @@ so the map is per model family:
46
52
  | kimi | none \| low \| high \| max |
47
53
  | deepseek | none \| low \| high \| xhigh \| max |
48
54
  | muse | low \| medium \| high \| xhigh |
55
+ | mistral (`alias-mistral-large`) | low \| medium \| high \| xhigh \| max |
49
56
  | generic (glm, minimax, mimo, aleph, …) | none \| low \| medium \| high \| xhigh |
50
57
  | none (apertus, eve, ocr) | — (the field is never sent) |
51
58
 
52
59
  Pi's own levels are folded onto these, so you always pick from Pi's list: `--thinking high` on a Qwen
53
60
  model sends `xhigh`, `--thinking medium` on DeepSeek sends `high`, `--thinking minimal` sends `low`.
54
61
 
62
+ ## Count tokens
63
+
64
+ `count_tokens` sends a text to the gateway's `/tokenize` endpoint and returns the token count plus the
65
+ model's context ceiling, defaulting to the current model. Use it to check a prompt fits before
66
+ spending a chat request — an over-long prompt 400s, and that request is still charged.
67
+
68
+ `/tokenize` is implemented per backend, so it works for some aliases and not others (measured
69
+ 2026-11-16: `alias-fast`, `alias-code`, `alias-large`, `alias-eve` and the Qwen 3.8 aliases answer;
70
+ the 1M deployments do not). For the rest the tool says the endpoint is not available instead of
71
+ returning a count.
72
+
55
73
  ## Context windows
56
74
 
57
75
  The context windows the gateway enforces: 1,048,576 for the 1M deployments, 262,144 for the Qwen 3.8,
58
- Apertus and OCR families, and 131,072 for `alias-fast` and `alias-eve`. Output is capped at 16k
76
+ Apertus, OCR and Mistral families, and 131,072 for `alias-fast` and `alias-eve`. Output is capped at 16k
59
77
  tokens so reasoning cannot consume the whole answer budget.
60
78
 
61
79
  ## Which models appear
62
80
 
63
81
  `alias-*` ids, plus two verified unaliased ids (`deepseek-v4.1-flash-at-hzdr`, `Swift-Qwen3.8-27b`).
64
- The alias is the stable handle — JSC's guide says to use it so the model behind it can change. The rest
65
- of the catalog is either a descriptive deployment name ("90 - Kimi-K3 1M on Juwels Booster"), an
82
+ The alias is the stable handle — JSC's guide says to use it so the model behind it can change. Rows
83
+ read `alias (deployment)`, alias first, so what you type leads the row:
84
+ `alias-large (nvidia/Qwen3.8-Flash-Next-NVFP4)`. The rest of the catalog is either a descriptive
85
+ deployment name ("90 - Kimi-K3 1M on Juwels Booster"), an
66
86
  OpenAI-name shim for the *embedding* endpoint (`gpt-3.5-turbo`, `text-davinci-003`), or a model that
67
87
  cannot call tools (`trustllm-*`). Embeddings, rerankers, image/video/voice and the anonymizer are not
68
88
  chat models and never appear.
package/blablador.ts CHANGED
@@ -10,7 +10,8 @@
10
10
  *
11
11
  * Auth: BLABLADOR_API_TOKEN (or /login blablador once a credential is stored for the provider).
12
12
  */
13
- import type { ExtensionAPI, ProviderModelConfig } from "@earendil-works/pi-coding-agent";
13
+ import { Type } from "@earendil-works/pi-ai";
14
+ import { defineTool, type ExtensionAPI, type ProviderModelConfig } from "@earendil-works/pi-coding-agent";
14
15
  import {
15
16
  BASE_URL,
16
17
  EFFORT,
@@ -22,6 +23,8 @@ import {
22
23
  fetchLiveModels,
23
24
  outputCap,
24
25
  resolveToken,
26
+ tokenize,
27
+ webSearch,
25
28
  type CatalogModel,
26
29
  } from "blablador-core";
27
30
 
@@ -58,6 +61,9 @@ function chatModel(model: CatalogModel): ProviderModelConfig {
58
61
  // Free research API; a real rate card would make /usage meaningful.
59
62
  cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
60
63
  contextWindow: model.contextWindow,
64
+ // The gateway answers HTTP 400 ("Unknown message role: developer") to anything but
65
+ // system/user/assistant/tool, so pi must never use the newer role here.
66
+ compat: { supportsDeveloperRole: false },
61
67
  // Reasoning shares the output budget; outputCap keeps the prompt room inside the window
62
68
  // even if the gateway ever reports one smaller than the ceiling (README "Context windows").
63
69
  maxTokens: outputCap(model.contextWindow),
@@ -69,6 +75,64 @@ export function buildModels(catalog: readonly CatalogModel[]): ProviderModelConf
69
75
  return catalog.map(chatModel);
70
76
  }
71
77
 
78
+ // Blablador ships an anonymous SearXNG instance, so a web_search tool needs no extra key. The search
79
+ // logic is shared with opencode-blablador through blablador-core; this is the pi-side registration.
80
+ const webSearchTool = defineTool({
81
+ name: "web_search",
82
+ label: "Web search",
83
+ description:
84
+ "Search the web via Blablador's SearXNG instance. Returns titles, URLs, and snippets. Use it for current information the model does not already know.",
85
+ parameters: Type.Object({
86
+ query: Type.String({ description: "The search query" }),
87
+ category: Type.Optional(
88
+ Type.Union(
89
+ [Type.Literal("general"), Type.Literal("news"), Type.Literal("science"), Type.Literal("it")],
90
+ { description: "Result category (default: general)" },
91
+ ),
92
+ ),
93
+ }),
94
+ async execute(_toolCallId, params, signal) {
95
+ const results = await webSearch(params.query, { categories: params.category, signal });
96
+ const text =
97
+ results
98
+ .map((result, index) => `${index + 1}. ${result.title}\n ${result.url}\n ${result.content}`)
99
+ .join("\n\n") || "No results.";
100
+ return { content: [{ type: "text", text }], details: results };
101
+ },
102
+ });
103
+
104
+ // count_tokens: check a text fits a model's window before processing it. The current model comes
105
+ // from ctx (pi exposes it); the token from pi's auth resolution, the same source the provider uses.
106
+ const countTokensTool = defineTool({
107
+ name: "count_tokens",
108
+ label: "Count tokens",
109
+ description:
110
+ "Count how many tokens a text uses against a Blablador model's context window. Use it to check a large input fits before processing it. Returns the token count and the model's max context length.",
111
+ parameters: Type.Object({
112
+ text: Type.String({ description: "The text to count" }),
113
+ model: Type.Optional(Type.String({ description: "Blablador alias to check against (default: the current model)" })),
114
+ }),
115
+ async execute(_toolCallId, params, _signal, _onUpdate, ctx) {
116
+ // Resolve the token the same way the provider does: pi's auth store first, then the env var;
117
+ // getApiKeyForProvider already does that fallback. Trim, because the gateway 401s on whitespace.
118
+ const token = (await ctx.modelRegistry.getApiKeyForProvider("blablador").catch(() => undefined))?.trim();
119
+ if (!token)
120
+ return { content: [{ type: "text", text: "No Blablador API token configured (set BLABLADOR_API_TOKEN)." }], details: {} };
121
+ const modelId = params.model ?? ctx.model?.id;
122
+ if (!modelId) return { content: [{ type: "text", text: "No model to count against; pass a model." }], details: {} };
123
+ try {
124
+ const { count, maxModelLen } = await tokenize(modelId, params.text, token, ctx.signal);
125
+ // Reserve the answer budget: a prompt that fills the window still 400s once output is asked for.
126
+ const fits =
127
+ count !== undefined && maxModelLen !== undefined ? count <= maxModelLen - outputCap(maxModelLen) : undefined;
128
+ const text = `Counted ${count ?? "?"} tokens for "${modelId}" (max context ${maxModelLen ?? "?"}).${fits === false ? " Does NOT fit." : fits ? " Fits." : ""}`;
129
+ return { content: [{ type: "text", text }], details: { model: modelId, count, maxModelLen, fits } };
130
+ } catch (error) {
131
+ return { content: [{ type: "text", text: `Token count failed: ${error instanceof Error ? error.message : String(error)}` }], details: {} };
132
+ }
133
+ },
134
+ });
135
+
72
136
  export default function blablador(pi: ExtensionAPI): void {
73
137
  // pi interpolates "$BLABLADOR_API_TOKEN" into the Authorization header verbatim, so a key
74
138
  // pasted with surrounding whitespace 401s every request. The extension shares the CLI process,
@@ -112,4 +176,8 @@ export default function blablador(pi: ExtensionAPI): void {
112
176
  return models;
113
177
  },
114
178
  });
179
+
180
+ // Registered alongside the provider: a tool, not a model, so it shows in every blablador session.
181
+ pi.registerTool(webSearchTool);
182
+ pi.registerTool(countTokensTool);
115
183
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pi-blablador",
3
- "version": "0.1.2",
3
+ "version": "0.1.4",
4
4
  "description": "Blablador (Helmholtz / JSC) provider for Pi: live alias discovery, per-family thinking effort, measured context windows",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -23,18 +23,22 @@
23
23
  ]
24
24
  },
25
25
  "peerDependencies": {
26
- "@earendil-works/pi-coding-agent": ">=1.4.0 <2.0.0"
26
+ "@earendil-works/pi-coding-agent": ">=1.4.0 <2.0.0",
27
+ "@earendil-works/pi-ai": ">=1.4.0 <2.0.0"
27
28
  },
28
29
  "peerDependenciesMeta": {
29
30
  "@earendil-works/pi-coding-agent": {
30
31
  "optional": true
32
+ },
33
+ "@earendil-works/pi-ai": {
34
+ "optional": true
31
35
  }
32
36
  },
33
37
  "engines": {
34
38
  "node": ">=22.18.0"
35
39
  },
36
40
  "dependencies": {
37
- "blablador-core": "0.1.2"
41
+ "blablador-core": "0.1.4"
38
42
  },
39
43
  "scripts": {
40
44
  "test": "node --test \"test/**/*.test.mjs\"",