Skip to content

Commit 7463c05

Browse files
committed
fix(ollama): keep CJK estimator plugin-private
1 parent 4401e61 commit 7463c05

4 files changed

Lines changed: 49 additions & 3 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ Docs: https://docs.openclaw.ai
5757

5858
### Fixes
5959

60-
- **Ollama CJK usage fallback:** use the shared CJK-aware character estimate when native streams omit prompt or completion counts, while preserving provider-reported usage and missing-cache-telemetry provenance. (#110073) Thanks @hugenshen.
6160
- **Control UI session refreshes:** preserve explicitly queued list filters and background hydration across later Gateway event invalidation, while keeping append pagination followed by a canonical refresh. Fixes #116697. Thanks @shakkernerd.
6261
- **Control UI dynamic deep links:** reuse the initial route loader result when publishing real agent, session, dashboard, Workboard, Memory, and Plugins paths, avoiding redundant route-loader work during startup. Thanks @shakkernerd.
6362
- **Linux gateway service ownership:** refuse user-scope systemd publication and activation when the same gateway unit name is already owned or cannot be verified in the system scope, including `--force`, with actionable recovery guidance instead of creating restart-looping dual managers. Fixes #116129.
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
* CJK-aware character weighting for Ollama usage fallback estimates.
3+
*
4+
* This stays plugin-private because exposing it through the Plugin SDK would
5+
* create a stable public contract for one provider-specific fallback. Keep
6+
* the weighting aligned with normalization-core's CJK budget heuristic.
7+
*/
8+
9+
const CHARS_PER_TOKEN_ESTIMATE = 4;
10+
11+
const NON_ASCII_RE = /[\u0080-\u{10FFFF}]/u;
12+
const COMMON_CJK_RE = /[\u00B7\u3000-\u319F\u4E00-\u9FA5\uAC00-\uD7AF\uFF01-\uFF60]/gu;
13+
const RARE_BMP_CJK_RE =
14+
/[\u1100-\u11FF\u2E80-\u2FFF\u31A0-\u4DFF\u9FA6-\u9FFF\uA000-\uA4FF\uA700-\uA707\uA960-\uA97F\uD7B0-\uD7FF\uF900-\uFAFF]/gu;
15+
const TWO_TOKEN_CJK_RE =
16+
/[\u{02C7}\u{02C9}-\u{02CB}\u{02D9}\u{02EA}-\u{02EB}\uFE10-\uFE4F\uFF61-\uFFDC\uFFE0-\uFFE6]|\u{0305}|\u{0323}/gu;
17+
const THREE_TOKEN_SUPPLEMENTARY_CJK_RE = /[\u{1D360}-\u{1D371}]/gu;
18+
const SUPPLEMENTARY_CJK_RE =
19+
/[\u{16FE0}-\u{16FFF}\u{1AFF0}-\u{1AFFF}\u{1B000}-\u{1B16F}\u{1F200}-\u{1F2FF}\u{20000}-\u{2FA1F}\u{30000}-\u{3347F}]/gu;
20+
const SPECIAL_CJK_RE =
21+
/[\u{02C7}\u{02C9}-\u{02CB}\u{02D9}\u{02EA}-\u{02EB}\u1100-\u11FF\u2E80-\u2FFF\u31A0-\u4DFF\u9FA6-\u9FFF\uA000-\uA4FF\uA700-\uA707\uA960-\uA97F\uD7B0-\uD7FF\uF900-\uFAFF\uFE10-\uFE4F\uFF61-\uFFDC\uFFE0-\uFFE6\u{16FE0}-\u{16FFF}\u{1AFF0}-\u{1AFFF}\u{1B000}-\u{1B16F}\u{1D360}-\u{1D371}\u{1F200}-\u{1F2FF}\u{20000}-\u{2FA1F}\u{30000}-\u{3347F}]|\u{0305}|\u{0323}/u;
22+
23+
function countMatches(text: string, pattern: RegExp): number {
24+
return (text.match(pattern) ?? []).length;
25+
}
26+
27+
export function estimateStringChars(text: string): number {
28+
if (!NON_ASCII_RE.test(text)) {
29+
return text.length;
30+
}
31+
const commonCjkCount = countMatches(text, COMMON_CJK_RE);
32+
const commonEstimate = text.length + commonCjkCount * (CHARS_PER_TOKEN_ESTIMATE - 1);
33+
if (!SPECIAL_CJK_RE.test(text)) {
34+
return commonEstimate;
35+
}
36+
const rareBmpCjkCount = countMatches(text, RARE_BMP_CJK_RE);
37+
const twoTokenCjkCount = countMatches(text, TWO_TOKEN_CJK_RE);
38+
const threeTokenSupplementaryCjkCount = countMatches(text, THREE_TOKEN_SUPPLEMENTARY_CJK_RE);
39+
const supplementaryCjkCount = countMatches(text, SUPPLEMENTARY_CJK_RE);
40+
return (
41+
commonEstimate +
42+
rareBmpCjkCount * (CHARS_PER_TOKEN_ESTIMATE * 3 - 1) +
43+
twoTokenCjkCount * (CHARS_PER_TOKEN_ESTIMATE * 2 - 1) +
44+
threeTokenSupplementaryCjkCount * (CHARS_PER_TOKEN_ESTIMATE * 3 - 2) +
45+
supplementaryCjkCount * (CHARS_PER_TOKEN_ESTIMATE * 4 - 2)
46+
);
47+
}

‎extensions/ollama/src/stream.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ import {
3232
import { createSubsystemLogger } from "openclaw/plugin-sdk/runtime-env";
3333
import { fetchWithSsrFGuard, isLoopbackHost } from "openclaw/plugin-sdk/ssrf-runtime";
3434
import { isRecord, readStringValue } from "openclaw/plugin-sdk/string-coerce-runtime";
35-
import { estimateStringChars, truncateUtf16Safe } from "openclaw/plugin-sdk/text-utility-runtime";
35+
import { truncateUtf16Safe } from "openclaw/plugin-sdk/text-utility-runtime";
36+
import { estimateStringChars } from "./cjk-char-estimate.js";
3637
import { OLLAMA_CLOUD_BASE_URL, OLLAMA_DEFAULT_BASE_URL } from "./defaults.js";
3738
import { shouldWrapOllamaCompatMoonshotThinking } from "./model-behavior.js";
3839
import { normalizeOllamaWireModelId } from "./model-id.js";

‎src/plugin-sdk/text-utility-runtime.ts‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import type { BaseProbeResult } from "../channels/plugins/types.public.js";
33
import { withTimeout } from "../utils/with-timeout.js";
44

5-
export { estimateStringChars } from "@openclaw/normalization-core/cjk-chars";
65
export { escapeHtml } from "../shared/html-escape.js";
76

87
type ChannelProbeResult = BaseProbeResult & { elapsedMs?: number };

0 commit comments

Comments
 (0)