dsh-thinking-levels 0.6.0 → 0.7.0-beta.1
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/CHANGELOG.ja.md +117 -89
- package/CHANGELOG.ko.md +117 -89
- package/CHANGELOG.md +37 -0
- package/README.md +27 -0
- package/lib/client.js +764 -106
- package/lib/client.js.map +1 -1
- package/lib/context-window.d.ts +41 -0
- package/lib/context-window.js +54 -0
- package/lib/index.d.ts +6 -0
- package/lib/index.js +32 -26
- package/lib/takeover-sync.d.ts +57 -32
- package/lib/takeover-sync.js +151 -45
- package/package.json +1 -1
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pure context-window helpers for the dsh-thinking-levels plugin.
|
|
3
|
+
*
|
|
4
|
+
* The context-window limit of a custom gateway model is declared in the
|
|
5
|
+
* llm-pi-ai model entry (`contextWindow`, a positive integer). The harness
|
|
6
|
+
* consumes it through `resolveModelInfo(...).context.contextWindow` for
|
|
7
|
+
* compaction thresholds, context-overflow detection and context-pressure
|
|
8
|
+
* projections — a settings write takes effect on the next request without a
|
|
9
|
+
* restart. This module centralizes the legal range, the common presets and
|
|
10
|
+
* the display formatting shared by the config schema, the settings card and
|
|
11
|
+
* the tests. Kept dependency-free (pure inputs -> output).
|
|
12
|
+
*/
|
|
13
|
+
/** Smallest accepted context window (tokens), aligned with dsh-thinking-effort. */
|
|
14
|
+
export declare const CONTEXT_WINDOW_MIN = 2000;
|
|
15
|
+
/** Largest accepted context window (tokens), e.g. third-party DeepSeek 1M. */
|
|
16
|
+
export declare const CONTEXT_WINDOW_MAX = 1000000;
|
|
17
|
+
/** One selectable context-window preset. */
|
|
18
|
+
export interface ContextWindowPreset {
|
|
19
|
+
/** Human-readable label shown on the card button and the model badge. */
|
|
20
|
+
label: string;
|
|
21
|
+
/** The token count written to the llm-pi-ai model entry. */
|
|
22
|
+
value: number;
|
|
23
|
+
}
|
|
24
|
+
/** The multi-level context-window presets, in ascending order. */
|
|
25
|
+
export declare const CONTEXT_WINDOW_PRESETS: readonly ContextWindowPreset[];
|
|
26
|
+
/** Format a token count as a readable label (exact presets keep their label). */
|
|
27
|
+
export declare function formatContextWindow(value: number): string;
|
|
28
|
+
/** The result of validating a context-window value. */
|
|
29
|
+
export type ContextWindowValidation = {
|
|
30
|
+
ok: true;
|
|
31
|
+
value: number;
|
|
32
|
+
} | {
|
|
33
|
+
ok: false;
|
|
34
|
+
reason: 'integer' | 'range';
|
|
35
|
+
};
|
|
36
|
+
/**
|
|
37
|
+
* Validate a context-window candidate (number or numeric string). Accepts only
|
|
38
|
+
* base-10 integers inside [CONTEXT_WINDOW_MIN, CONTEXT_WINDOW_MAX]; anything
|
|
39
|
+
* else (float, 'abc', '1e3', out-of-range) is rejected with a reason.
|
|
40
|
+
*/
|
|
41
|
+
export declare function validateContextWindow(value: unknown): ContextWindowValidation;
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pure context-window helpers for the dsh-thinking-levels plugin.
|
|
3
|
+
*
|
|
4
|
+
* The context-window limit of a custom gateway model is declared in the
|
|
5
|
+
* llm-pi-ai model entry (`contextWindow`, a positive integer). The harness
|
|
6
|
+
* consumes it through `resolveModelInfo(...).context.contextWindow` for
|
|
7
|
+
* compaction thresholds, context-overflow detection and context-pressure
|
|
8
|
+
* projections — a settings write takes effect on the next request without a
|
|
9
|
+
* restart. This module centralizes the legal range, the common presets and
|
|
10
|
+
* the display formatting shared by the config schema, the settings card and
|
|
11
|
+
* the tests. Kept dependency-free (pure inputs -> output).
|
|
12
|
+
*/
|
|
13
|
+
/** Smallest accepted context window (tokens), aligned with dsh-thinking-effort. */
|
|
14
|
+
export const CONTEXT_WINDOW_MIN = 2000;
|
|
15
|
+
/** Largest accepted context window (tokens), e.g. third-party DeepSeek 1M. */
|
|
16
|
+
export const CONTEXT_WINDOW_MAX = 1_000_000;
|
|
17
|
+
/** The multi-level context-window presets, in ascending order. */
|
|
18
|
+
export const CONTEXT_WINDOW_PRESETS = [
|
|
19
|
+
{ label: '64K', value: 64_000 },
|
|
20
|
+
{ label: '128K', value: 131_072 },
|
|
21
|
+
{ label: '256K', value: 256_000 },
|
|
22
|
+
{ label: '400K', value: 400_000 },
|
|
23
|
+
{ label: '512K', value: 524_288 },
|
|
24
|
+
{ label: '1M', value: 1_000_000 },
|
|
25
|
+
];
|
|
26
|
+
/** Format a token count as a readable label (exact presets keep their label). */
|
|
27
|
+
export function formatContextWindow(value) {
|
|
28
|
+
const preset = CONTEXT_WINDOW_PRESETS.find(candidate => candidate.value === value);
|
|
29
|
+
if (preset !== undefined)
|
|
30
|
+
return preset.label;
|
|
31
|
+
if (value >= CONTEXT_WINDOW_MAX)
|
|
32
|
+
return '1M';
|
|
33
|
+
if (value >= 1024)
|
|
34
|
+
return `${Math.round(value / 1024)}K`;
|
|
35
|
+
return String(value);
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Validate a context-window candidate (number or numeric string). Accepts only
|
|
39
|
+
* base-10 integers inside [CONTEXT_WINDOW_MIN, CONTEXT_WINDOW_MAX]; anything
|
|
40
|
+
* else (float, 'abc', '1e3', out-of-range) is rejected with a reason.
|
|
41
|
+
*/
|
|
42
|
+
export function validateContextWindow(value) {
|
|
43
|
+
const raw = typeof value === 'string' ? value.trim() : value;
|
|
44
|
+
const asString = typeof raw === 'string' ? raw : String(raw);
|
|
45
|
+
if (!/^\d+$/.test(asString))
|
|
46
|
+
return { ok: false, reason: 'integer' };
|
|
47
|
+
const numeric = Number(asString);
|
|
48
|
+
if (!Number.isSafeInteger(numeric))
|
|
49
|
+
return { ok: false, reason: 'integer' };
|
|
50
|
+
if (numeric < CONTEXT_WINDOW_MIN || numeric > CONTEXT_WINDOW_MAX) {
|
|
51
|
+
return { ok: false, reason: 'range' };
|
|
52
|
+
}
|
|
53
|
+
return { ok: true, value: numeric };
|
|
54
|
+
}
|
package/lib/index.d.ts
CHANGED
|
@@ -43,6 +43,12 @@ export interface ModelCapabilityOverride {
|
|
|
43
43
|
* adapter-advertised list.
|
|
44
44
|
*/
|
|
45
45
|
efforts?: false | Exclude<EffortId, 'auto'>[];
|
|
46
|
+
/**
|
|
47
|
+
* Declared context-window limit in tokens (2000–1_000_000). Declaration-only:
|
|
48
|
+
* the runtime harness reads it from the llm-pi-ai model entry, so this
|
|
49
|
+
* override is a validated config-surface declaration.
|
|
50
|
+
*/
|
|
51
|
+
contextWindow?: number;
|
|
46
52
|
}
|
|
47
53
|
/** Plugin settings. */
|
|
48
54
|
export interface ThinkingLevelsConfig {
|
package/lib/index.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import z from '@deepseek-ai/schemastery';
|
|
2
2
|
import { assertEffortId, reasoningEffortSupported, resolveEffortInjection } from "./thinking-level.js";
|
|
3
3
|
import { recentToolCalls } from "./session-events.js";
|
|
4
|
-
import {
|
|
4
|
+
import { CONTEXT_WINDOW_MAX, CONTEXT_WINDOW_MIN } from "./context-window.js";
|
|
5
|
+
import { PI_AI_NAMESPACE, TAKEOVER_NAMESPACE, takeoverProvidersOf, withOfficialCompatFixes, } from "./takeover-sync.js";
|
|
5
6
|
const effortId = z.union(['off', 'on', 'minimal', 'low', 'medium', 'high', 'xhigh', 'max']);
|
|
6
7
|
/**
|
|
7
8
|
* Composition-entry schema: what a dsh profile may configure at assembly
|
|
@@ -19,6 +20,7 @@ export const Config = z.object({
|
|
|
19
20
|
vision: z.boolean(),
|
|
20
21
|
thinking: z.boolean(),
|
|
21
22
|
efforts: z.union([z.const(false), z.array(effortId)]),
|
|
23
|
+
contextWindow: z.number().step(1).min(CONTEXT_WINDOW_MIN).max(CONTEXT_WINDOW_MAX),
|
|
22
24
|
})).default({}),
|
|
23
25
|
});
|
|
24
26
|
/** Settings defaults, kept in lockstep with the schema defaults above. */
|
|
@@ -238,42 +240,46 @@ export function apply(ctx, config = DEFAULT_CONFIG) {
|
|
|
238
240
|
// re-registration.
|
|
239
241
|
onChange: () => { },
|
|
240
242
|
});
|
|
241
|
-
//
|
|
242
|
-
//
|
|
243
|
-
// openai-completions gateway AND declares thinking (reasoningEfforts table)
|
|
244
|
-
//
|
|
245
|
-
//
|
|
246
|
-
//
|
|
247
|
-
//
|
|
248
|
-
//
|
|
249
|
-
//
|
|
250
|
-
//
|
|
243
|
+
// Official-compat bridge (check branch): write the OFFICIAL compat surface
|
|
244
|
+
// into the llm-pi-ai namespace for every provider that targets a custom
|
|
245
|
+
// openai-completions gateway AND declares thinking (reasoningEfforts table):
|
|
246
|
+
// route-level `supportsDeveloperRole: false` plus model-level
|
|
247
|
+
// `thinkingFormat: qwen` on toggle-style thinking rows. This replaces the
|
|
248
|
+
// retired short-circuit route (maintaining the dsh-llm-openai-completions
|
|
249
|
+
// takeover list): since dsh v0.1.0-rc.8 the declarative compat fixes the
|
|
250
|
+
// developer-role 400 AND drives enable_thinking at the pi-ai adapter itself,
|
|
251
|
+
// so pi-ai keeps serving the route and no transport takeover is needed.
|
|
252
|
+
// Storage pattern follows
|
|
253
|
+
// hytime/dsh-thinking-effort's host side: read → pure transform (identity
|
|
254
|
+
// when nothing to change) → whole-section update, so dsh's llm-pi-ai schema
|
|
255
|
+
// validator gates the write where it is WRITTEN; an installed dsh predating
|
|
256
|
+
// rc.8 rejects the unknown field and we log and keep the previous section.
|
|
257
|
+
// The read is lazy and writes are queued by the service, so no race with a
|
|
258
|
+
// concurrent Settings → Models edit.
|
|
251
259
|
let syncTail = Promise.resolve();
|
|
252
|
-
const
|
|
260
|
+
const syncDeveloperRole = () => {
|
|
253
261
|
syncTail = syncTail.then(async () => {
|
|
254
262
|
const settings = ctx.get('settings');
|
|
255
|
-
const previous = settings?.get?.(TAKEOVER_NAMESPACE);
|
|
256
|
-
// Unregistered namespace → the adapter plugin is absent; never write.
|
|
257
|
-
if (previous === undefined)
|
|
258
|
-
return;
|
|
259
263
|
const piAi = settings?.get?.(PI_AI_NAMESPACE);
|
|
260
|
-
const next =
|
|
261
|
-
if (next ===
|
|
264
|
+
const next = withOfficialCompatFixes(piAi);
|
|
265
|
+
if (next === undefined || next === piAi)
|
|
262
266
|
return;
|
|
263
|
-
await settings?.update?.(
|
|
264
|
-
ctx.logger?.info?.('[thinking-levels]
|
|
267
|
+
await settings?.update?.(PI_AI_NAMESPACE, { providers: next.providers });
|
|
268
|
+
ctx.logger?.info?.('[thinking-levels] official-compat: supportsDeveloperRole=false (route) and thinkingFormat=qwen (toggle-style models) written to llm-pi-ai for custom thinking routes');
|
|
265
269
|
}).catch((error) => {
|
|
266
|
-
ctx.logger?.warn?.('[thinking-levels]
|
|
270
|
+
ctx.logger?.warn?.('[thinking-levels] developer-role compat sync rejected (schema gate); kept previous section', error);
|
|
267
271
|
});
|
|
268
272
|
};
|
|
269
273
|
// Initial sync (adapters may register later; re-run on every adapters update
|
|
270
|
-
// and every settings
|
|
271
|
-
|
|
274
|
+
// and every llm-pi-ai settings update so a Settings → Models edit propagates.
|
|
275
|
+
// The write is identity-gated, so the re-entry triggered by our own update
|
|
276
|
+
// settles immediately).
|
|
277
|
+
syncDeveloperRole();
|
|
272
278
|
const onSyncAny = ctx.on;
|
|
273
|
-
onSyncAny('llm/adapters-updated', () =>
|
|
279
|
+
onSyncAny('llm/adapters-updated', () => syncDeveloperRole());
|
|
274
280
|
onSyncAny('settings/document-updated', (ns) => {
|
|
275
|
-
if (ns === PI_AI_NAMESPACE
|
|
276
|
-
|
|
281
|
+
if (ns === PI_AI_NAMESPACE)
|
|
282
|
+
syncDeveloperRole();
|
|
277
283
|
});
|
|
278
284
|
// Inject the level decision into every model request of a step.
|
|
279
285
|
// The 'agent/request' event key is augmented onto cordis Events by the
|
package/lib/takeover-sync.d.ts
CHANGED
|
@@ -1,51 +1,58 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Auto-
|
|
3
|
-
*
|
|
2
|
+
* Auto-flag sync (check branch): bridge from dsh-thinking-levels to the
|
|
3
|
+
* OFFICIAL llm-pi-ai compat surface.
|
|
4
4
|
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
* the
|
|
5
|
+
* CHECK-BRANCH REPLACEMENT: the previous bridge maintained the
|
|
6
|
+
* `llm-openai-completions` takeover list of the dsh-llm-openai-completions
|
|
7
|
+
* adapter plugin (the short-circuit route). Since dsh v0.1.0-rc.8 the official
|
|
8
|
+
* `compat.supportsDeveloperRole` flag (commit 884f7b9c41) fixes the developer
|
|
9
|
+
* role 400 declaratively, so the short-circuit list is no longer needed: the
|
|
10
|
+
* sync now writes the flag into the OFFICIAL `llm-pi-ai` namespace instead.
|
|
11
11
|
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
*
|
|
12
|
+
* Storage pattern learned from hytime/dsh-thinking-effort's host side
|
|
13
|
+
* (src/host/settings.ts): read the live section → pure transform with
|
|
14
|
+
* immutable clones → `settings.update('llm-pi-ai', { providers })` whole-section
|
|
15
|
+
* writeback, so dsh's `llm-pi-ai` schema validator (assertServiceable) gates
|
|
16
|
+
* the write where it is WRITTEN (settings-rejected names route and model)
|
|
17
|
+
* instead of storing something the reader cannot serve. The flag is written at
|
|
18
|
+
* the ROUTE level (`providers.<route>.compat`) — the official inheritance chain
|
|
19
|
+
* (model → provider → catalog → protocol) means a model-level explicit value
|
|
20
|
+
* still wins, and explicit values are never clobbered by this sync.
|
|
17
21
|
*
|
|
18
|
-
*
|
|
19
|
-
*
|
|
20
|
-
*
|
|
22
|
+
* Identification is unchanged: a provider that (a) targets a custom
|
|
23
|
+
* openai-completions gateway and (b) declares thinking (a reasoningEfforts
|
|
24
|
+
* table) is exactly the route the short-circuit used to take over. Pure
|
|
25
|
+
* functions here are unit-tested; the host plugin wires them to the settings
|
|
26
|
+
* service.
|
|
27
|
+
*
|
|
28
|
+
* Deliberately soft-coupled: nothing here value-imports dsh-settings. If the
|
|
29
|
+
* installed dsh predates rc.8 the schema rejects the unknown field and the
|
|
30
|
+
* host catches and logs the rejection — no error, no log spam.
|
|
21
31
|
* @module dsh-thinking-levels/takeover-sync
|
|
22
32
|
*/
|
|
23
|
-
/** The settings namespace owned by dsh-llm-openai-completions. */
|
|
24
|
-
export declare const TAKEOVER_NAMESPACE = "llm-openai-completions";
|
|
25
33
|
/** The settings namespace holding the provider/model configs (llm-pi-ai). */
|
|
26
34
|
export declare const PI_AI_NAMESPACE = "llm-pi-ai";
|
|
35
|
+
/** The settings namespace of the retired short-circuit adapter, still READ to gate posture. */
|
|
36
|
+
export declare const TAKEOVER_NAMESPACE = "llm-openai-completions";
|
|
27
37
|
/** One model row of the llm-pi-ai section (minimal face). */
|
|
28
38
|
export interface PiAiModelRow {
|
|
29
39
|
id?: unknown;
|
|
30
40
|
reasoningEfforts?: unknown;
|
|
41
|
+
compat?: unknown;
|
|
31
42
|
}
|
|
32
43
|
/** One provider profile of the llm-pi-ai section (minimal face). */
|
|
33
44
|
export interface PiAiProviderProfile {
|
|
34
45
|
api?: unknown;
|
|
35
46
|
baseURL?: unknown;
|
|
36
47
|
models?: unknown;
|
|
48
|
+
compat?: unknown;
|
|
37
49
|
/** Other profile fields (apiKeyEnv, displayName, …) exist but are unused. */
|
|
38
50
|
[key: string]: unknown;
|
|
39
51
|
}
|
|
40
|
-
/** The llm-pi-ai section slice this module reads. */
|
|
52
|
+
/** The llm-pi-ai section slice this module reads and transforms. */
|
|
41
53
|
export interface PiAiSection {
|
|
42
54
|
providers?: Record<string, PiAiProviderProfile>;
|
|
43
55
|
}
|
|
44
|
-
/** The llm-openai-completions section this module writes. */
|
|
45
|
-
export interface TakeoverSection {
|
|
46
|
-
enabled: boolean;
|
|
47
|
-
providers: string[];
|
|
48
|
-
}
|
|
49
56
|
/**
|
|
50
57
|
* Whether a provider profile targets a CUSTOM openai-completions gateway:
|
|
51
58
|
* explicit `api: openai-completions`, or a baseURL that is not an official
|
|
@@ -72,15 +79,33 @@ export declare function declaresThinking(profile: PiAiProviderProfile | undefine
|
|
|
72
79
|
*/
|
|
73
80
|
export declare function identifyTakeoverProviders(section: PiAiSection | undefined): string[];
|
|
74
81
|
/**
|
|
75
|
-
* Compute the
|
|
76
|
-
* identified provider (
|
|
77
|
-
*
|
|
78
|
-
*
|
|
79
|
-
*
|
|
80
|
-
*
|
|
81
|
-
*
|
|
82
|
+
* Compute the llm-pi-ai section with the OFFICIAL compat fixes written for
|
|
83
|
+
* every identified provider (custom openai-completions gateway AND thinking
|
|
84
|
+
* declared):
|
|
85
|
+
*
|
|
86
|
+
* 1. ROUTE level: `compat.supportsDeveloperRole: false` when the route does
|
|
87
|
+
* not carry an explicit value — the system prompt goes out as `system`
|
|
88
|
+
* (fixes the Unexpected message role 400 on vLLM/SGLang gateways).
|
|
89
|
+
* 2. MODEL level: `compat.thinkingFormat: 'qwen-chat-template'` on TOGGLE-STYLE thinking
|
|
90
|
+
* model rows (a reasoningEfforts table, no row-level
|
|
91
|
+
* `supportsReasoningEffort: true`, no explicit thinkingFormat) — pi-ai
|
|
92
|
+
* then sends the `enable_thinking` flag these early vLLM thinking models
|
|
93
|
+
* need, instead of a reasoning_effort they do not support. Effort-capable
|
|
94
|
+
* rows are untouched (their reasoning_effort wire needs the default/openai
|
|
95
|
+
* or declared format), and the route level is deliberately NOT written —
|
|
96
|
+
* a route-wide thinkingFormat would flip effort models onto
|
|
97
|
+
* enable_thinking too.
|
|
98
|
+
*
|
|
99
|
+
* Semantics (learned from dsh-thinking-effort's storage handling):
|
|
100
|
+
* - An EXPLICIT value on any layer is respected and left alone; only ABSENT
|
|
101
|
+
* fields are filled. This keeps the sync idempotent and never overrides
|
|
102
|
+
* user intent.
|
|
103
|
+
* - Immutable: unchanged profiles/rows and the unchanged section are returned
|
|
104
|
+
* by reference, so the host can skip the write on identity.
|
|
105
|
+
* @param section - the live llm-pi-ai section.
|
|
106
|
+
* @returns the next section, or the previous value (identity) when no change.
|
|
82
107
|
*/
|
|
83
|
-
export declare function
|
|
108
|
+
export declare function withOfficialCompatFixes(section: PiAiSection | undefined): PiAiSection | undefined;
|
|
84
109
|
/**
|
|
85
110
|
* The providers the openai-completions adapter is currently set up to take
|
|
86
111
|
* over, from the live `llm-openai-completions` settings namespace. Used to
|
package/lib/takeover-sync.js
CHANGED
|
@@ -1,29 +1,39 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Auto-
|
|
3
|
-
*
|
|
2
|
+
* Auto-flag sync (check branch): bridge from dsh-thinking-levels to the
|
|
3
|
+
* OFFICIAL llm-pi-ai compat surface.
|
|
4
4
|
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
* the
|
|
5
|
+
* CHECK-BRANCH REPLACEMENT: the previous bridge maintained the
|
|
6
|
+
* `llm-openai-completions` takeover list of the dsh-llm-openai-completions
|
|
7
|
+
* adapter plugin (the short-circuit route). Since dsh v0.1.0-rc.8 the official
|
|
8
|
+
* `compat.supportsDeveloperRole` flag (commit 884f7b9c41) fixes the developer
|
|
9
|
+
* role 400 declaratively, so the short-circuit list is no longer needed: the
|
|
10
|
+
* sync now writes the flag into the OFFICIAL `llm-pi-ai` namespace instead.
|
|
11
11
|
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
*
|
|
12
|
+
* Storage pattern learned from hytime/dsh-thinking-effort's host side
|
|
13
|
+
* (src/host/settings.ts): read the live section → pure transform with
|
|
14
|
+
* immutable clones → `settings.update('llm-pi-ai', { providers })` whole-section
|
|
15
|
+
* writeback, so dsh's `llm-pi-ai` schema validator (assertServiceable) gates
|
|
16
|
+
* the write where it is WRITTEN (settings-rejected names route and model)
|
|
17
|
+
* instead of storing something the reader cannot serve. The flag is written at
|
|
18
|
+
* the ROUTE level (`providers.<route>.compat`) — the official inheritance chain
|
|
19
|
+
* (model → provider → catalog → protocol) means a model-level explicit value
|
|
20
|
+
* still wins, and explicit values are never clobbered by this sync.
|
|
17
21
|
*
|
|
18
|
-
*
|
|
19
|
-
*
|
|
20
|
-
*
|
|
22
|
+
* Identification is unchanged: a provider that (a) targets a custom
|
|
23
|
+
* openai-completions gateway and (b) declares thinking (a reasoningEfforts
|
|
24
|
+
* table) is exactly the route the short-circuit used to take over. Pure
|
|
25
|
+
* functions here are unit-tested; the host plugin wires them to the settings
|
|
26
|
+
* service.
|
|
27
|
+
*
|
|
28
|
+
* Deliberately soft-coupled: nothing here value-imports dsh-settings. If the
|
|
29
|
+
* installed dsh predates rc.8 the schema rejects the unknown field and the
|
|
30
|
+
* host catches and logs the rejection — no error, no log spam.
|
|
21
31
|
* @module dsh-thinking-levels/takeover-sync
|
|
22
32
|
*/
|
|
23
|
-
/** The settings namespace owned by dsh-llm-openai-completions. */
|
|
24
|
-
export const TAKEOVER_NAMESPACE = 'llm-openai-completions';
|
|
25
33
|
/** The settings namespace holding the provider/model configs (llm-pi-ai). */
|
|
26
34
|
export const PI_AI_NAMESPACE = 'llm-pi-ai';
|
|
35
|
+
/** The settings namespace of the retired short-circuit adapter, still READ to gate posture. */
|
|
36
|
+
export const TAKEOVER_NAMESPACE = 'llm-openai-completions';
|
|
27
37
|
/** Hosts that are NOT a custom gateway (official OpenAI-compatible endpoints). */
|
|
28
38
|
const OFFICIAL_HOST_RE = /(?:^|\.)(?:deepseek\.com|openai\.com|openrouter\.ai|anthropic\.com|googleapis\.com|ai\.google\.dev|mistral\.ai|x\.ai)$/i;
|
|
29
39
|
/** Whether a value looks like a declared reasoningEfforts table. */
|
|
@@ -53,6 +63,17 @@ export function isCustomOpenAiGateway(profile) {
|
|
|
53
63
|
return false;
|
|
54
64
|
}
|
|
55
65
|
}
|
|
66
|
+
/** The model rows a profile declares, from models[] then modelOverrides (effort's modelRows pattern). */
|
|
67
|
+
function modelRows(profile) {
|
|
68
|
+
const rows = [];
|
|
69
|
+
if (Array.isArray(profile.models)) {
|
|
70
|
+
rows.push(...profile.models.filter((row) => typeof row === 'object' && row !== null));
|
|
71
|
+
}
|
|
72
|
+
if (typeof profile.modelOverrides === 'object' && profile.modelOverrides !== null && !Array.isArray(profile.modelOverrides)) {
|
|
73
|
+
rows.push(...Object.values(profile.modelOverrides).filter((row) => typeof row === 'object' && row !== null));
|
|
74
|
+
}
|
|
75
|
+
return rows;
|
|
76
|
+
}
|
|
56
77
|
/**
|
|
57
78
|
* Whether a provider declares thinking: at least one model row carries a
|
|
58
79
|
* reasoningEfforts table (thinking on). `false` (thinking off) and absent
|
|
@@ -61,13 +82,9 @@ export function isCustomOpenAiGateway(profile) {
|
|
|
61
82
|
* @returns true when any model declares a reasoningEfforts table.
|
|
62
83
|
*/
|
|
63
84
|
export function declaresThinking(profile) {
|
|
64
|
-
if (profile === undefined
|
|
85
|
+
if (profile === undefined)
|
|
65
86
|
return false;
|
|
66
|
-
return profile.
|
|
67
|
-
if (typeof row !== 'object' || row === null)
|
|
68
|
-
return false;
|
|
69
|
-
return isEffortsTable(row.reasoningEfforts);
|
|
70
|
-
});
|
|
87
|
+
return modelRows(profile).some((row) => isEffortsTable(row.reasoningEfforts));
|
|
71
88
|
}
|
|
72
89
|
/**
|
|
73
90
|
* Identify the providers that should be taken over by the openai-completions
|
|
@@ -84,30 +101,119 @@ export function identifyTakeoverProviders(section) {
|
|
|
84
101
|
.filter(([, profile]) => isCustomOpenAiGateway(profile) && declaresThinking(profile))
|
|
85
102
|
.map(([id]) => id);
|
|
86
103
|
}
|
|
104
|
+
/** A row's own `compat` record, when it declares one. */
|
|
105
|
+
function ownCompat(row) {
|
|
106
|
+
return typeof row.compat === 'object' && row.compat !== null && !Array.isArray(row.compat)
|
|
107
|
+
? row.compat
|
|
108
|
+
: undefined;
|
|
109
|
+
}
|
|
110
|
+
/** Whether the ROW ITSELF declares `supportsReasoningEffort: true` (route-level does not count here). */
|
|
111
|
+
function rowEffortCapable(row) {
|
|
112
|
+
return ownCompat(row)?.['supportsReasoningEffort'] === true;
|
|
113
|
+
}
|
|
114
|
+
/** The row's own `thinkingFormat`, when explicitly declared. */
|
|
115
|
+
function rowThinkingFormat(row) {
|
|
116
|
+
return ownCompat(row)?.['thinkingFormat'];
|
|
117
|
+
}
|
|
87
118
|
/**
|
|
88
|
-
*
|
|
89
|
-
*
|
|
90
|
-
* changes, the previous section is returned unchanged (identity — the host
|
|
91
|
-
* compares before writing).
|
|
92
|
-
* @param previous - the current llm-openai-completions value, if registered.
|
|
93
|
-
* @param identified - providers identified for takeover.
|
|
94
|
-
* @returns the section to write, or the previous value when no change.
|
|
119
|
+
* Rewrite one model row's compat for the toggle-thinking fix. Returns the row
|
|
120
|
+
* by reference when nothing to change.
|
|
95
121
|
*/
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
122
|
+
function withToggleThinkingFormat(row) {
|
|
123
|
+
// Only TOGGLE-STYLE thinking models (a reasoningEfforts table but no
|
|
124
|
+
// row-level effort support) need `thinkingFormat: qwen-chat-template`:
|
|
125
|
+
// pi-ai's default `openai` format sends reasoning_effort — which an early
|
|
126
|
+
// vLLM thinking model rejects or ignores — and the plain `qwen` format's
|
|
127
|
+
// TOP-LEVEL enable_thinking is silently dropped by bare vLLM servers, whose
|
|
128
|
+
// Qwen3 templates only honor chat_template_kwargs.enable_thinking. An
|
|
129
|
+
// explicit thinkingFormat is respected and never clobbered.
|
|
130
|
+
if (!isEffortsTable(row.reasoningEfforts))
|
|
131
|
+
return row;
|
|
132
|
+
if (rowEffortCapable(row))
|
|
133
|
+
return row;
|
|
134
|
+
if (rowThinkingFormat(row) !== undefined)
|
|
135
|
+
return row;
|
|
136
|
+
return { ...row, compat: { ...ownCompat(row), thinkingFormat: 'qwen-chat-template' } };
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* Compute the llm-pi-ai section with the OFFICIAL compat fixes written for
|
|
140
|
+
* every identified provider (custom openai-completions gateway AND thinking
|
|
141
|
+
* declared):
|
|
142
|
+
*
|
|
143
|
+
* 1. ROUTE level: `compat.supportsDeveloperRole: false` when the route does
|
|
144
|
+
* not carry an explicit value — the system prompt goes out as `system`
|
|
145
|
+
* (fixes the Unexpected message role 400 on vLLM/SGLang gateways).
|
|
146
|
+
* 2. MODEL level: `compat.thinkingFormat: 'qwen-chat-template'` on TOGGLE-STYLE thinking
|
|
147
|
+
* model rows (a reasoningEfforts table, no row-level
|
|
148
|
+
* `supportsReasoningEffort: true`, no explicit thinkingFormat) — pi-ai
|
|
149
|
+
* then sends the `enable_thinking` flag these early vLLM thinking models
|
|
150
|
+
* need, instead of a reasoning_effort they do not support. Effort-capable
|
|
151
|
+
* rows are untouched (their reasoning_effort wire needs the default/openai
|
|
152
|
+
* or declared format), and the route level is deliberately NOT written —
|
|
153
|
+
* a route-wide thinkingFormat would flip effort models onto
|
|
154
|
+
* enable_thinking too.
|
|
155
|
+
*
|
|
156
|
+
* Semantics (learned from dsh-thinking-effort's storage handling):
|
|
157
|
+
* - An EXPLICIT value on any layer is respected and left alone; only ABSENT
|
|
158
|
+
* fields are filled. This keeps the sync idempotent and never overrides
|
|
159
|
+
* user intent.
|
|
160
|
+
* - Immutable: unchanged profiles/rows and the unchanged section are returned
|
|
161
|
+
* by reference, so the host can skip the write on identity.
|
|
162
|
+
* @param section - the live llm-pi-ai section.
|
|
163
|
+
* @returns the next section, or the previous value (identity) when no change.
|
|
164
|
+
*/
|
|
165
|
+
export function withOfficialCompatFixes(section) {
|
|
166
|
+
const providers = section?.providers;
|
|
167
|
+
if (typeof providers !== 'object' || providers === null)
|
|
168
|
+
return section;
|
|
169
|
+
let nextProviders;
|
|
170
|
+
for (const [id, profile] of Object.entries(providers)) {
|
|
171
|
+
if (!isCustomOpenAiGateway(profile) || !declaresThinking(profile))
|
|
172
|
+
continue;
|
|
173
|
+
let nextProfile = profile;
|
|
174
|
+
// Route-level developer-role flag (absent only).
|
|
175
|
+
if (profile.compat?.['supportsDeveloperRole'] === undefined) {
|
|
176
|
+
nextProfile = {
|
|
177
|
+
...nextProfile,
|
|
178
|
+
compat: { ...(typeof nextProfile.compat === 'object' && nextProfile.compat !== null ? nextProfile.compat : {}), supportsDeveloperRole: false },
|
|
179
|
+
};
|
|
180
|
+
}
|
|
181
|
+
// Model-level toggle-thinking format (models[] rows).
|
|
182
|
+
if (Array.isArray(profile.models)) {
|
|
183
|
+
let nextModels;
|
|
184
|
+
profile.models.forEach((row, index) => {
|
|
185
|
+
if (typeof row !== 'object' || row === null)
|
|
186
|
+
return;
|
|
187
|
+
const fixed = withToggleThinkingFormat(row);
|
|
188
|
+
if (fixed !== row) {
|
|
189
|
+
nextModels ??= [...profile.models];
|
|
190
|
+
nextModels[index] = fixed;
|
|
191
|
+
}
|
|
192
|
+
});
|
|
193
|
+
if (nextModels !== undefined)
|
|
194
|
+
nextProfile = { ...nextProfile, models: nextModels };
|
|
195
|
+
}
|
|
196
|
+
// Model-level toggle-thinking format (modelOverrides entries).
|
|
197
|
+
if (typeof profile.modelOverrides === 'object' && profile.modelOverrides !== null && !Array.isArray(profile.modelOverrides)) {
|
|
198
|
+
let nextOverrides;
|
|
199
|
+
for (const [modelId, row] of Object.entries(profile.modelOverrides)) {
|
|
200
|
+
if (typeof row !== 'object' || row === null)
|
|
201
|
+
continue;
|
|
202
|
+
const fixed = withToggleThinkingFormat(row);
|
|
203
|
+
if (fixed !== row) {
|
|
204
|
+
nextOverrides ??= { ...profile.modelOverrides };
|
|
205
|
+
nextOverrides[modelId] = fixed;
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
if (nextOverrides !== undefined)
|
|
209
|
+
nextProfile = { ...nextProfile, modelOverrides: nextOverrides };
|
|
210
|
+
}
|
|
211
|
+
if (nextProfile !== profile) {
|
|
212
|
+
nextProviders ??= { ...providers };
|
|
213
|
+
nextProviders[id] = nextProfile;
|
|
214
|
+
}
|
|
107
215
|
}
|
|
108
|
-
|
|
109
|
-
return base;
|
|
110
|
-
return { enabled: true, providers: merged };
|
|
216
|
+
return nextProviders === undefined ? section : { ...section, providers: nextProviders };
|
|
111
217
|
}
|
|
112
218
|
/**
|
|
113
219
|
* The providers the openai-completions adapter is currently set up to take
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-thinking-levels",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.7.0-beta.1",
|
|
4
4
|
"description": "DSH host plugin: thinking-level (reasoning_effort) control for dsh — auto-adjust per tool round, with optional manual lock and per-tool timing telemetry.",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|