dsh-agent-sdk-bridge 1.1.6 → 1.1.7
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/DEVELOPMENT.md +34 -9
- package/FAQ.md +23 -8
- package/client.js +115 -50
- package/lib/index.js +248 -56
- package/lib/types/adapter.d.ts +59 -12
- package/lib/types/codebuddy-adapter.d.ts +20 -0
- package/lib/types/codebuddy.d.ts +23 -0
- package/lib/types/index.d.ts +10 -0
- package/lib/types/minimax-adapter.d.ts +8 -0
- package/lib/types/render.d.ts +17 -4
- package/package.json +1 -1
- package/src/adapter.ts +125 -25
- package/src/api.ts +3 -0
- package/src/codebuddy-adapter.ts +66 -15
- package/src/codebuddy.ts +43 -2
- package/src/index.ts +33 -0
- package/src/minimax-adapter.ts +37 -13
- package/src/render.ts +42 -12
package/lib/types/adapter.d.ts
CHANGED
|
@@ -13,7 +13,7 @@ import type { ImageAttachmentRef } from '@deepseek-ai/dsh-attachment';
|
|
|
13
13
|
import type { QoderBrand } from './brand.ts';
|
|
14
14
|
import type { Live } from './live.ts';
|
|
15
15
|
import { QoderModelCatalog } from './models.ts';
|
|
16
|
-
import type { Feed } from './render.ts';
|
|
16
|
+
import type { Feed, ImagePathResolver } from './render.ts';
|
|
17
17
|
import type { UsageSnapshot } from './usage.ts';
|
|
18
18
|
/**
|
|
19
19
|
* Read one durable image occurrence into the bytes a request sends.
|
|
@@ -40,6 +40,11 @@ export interface QoderAdapterOptions {
|
|
|
40
40
|
modelCacheTtlMs?: Live<number>;
|
|
41
41
|
/** Request-image reader; absent → images are never forwarded as pixels. */
|
|
42
42
|
readImage?: ImageRequestReader;
|
|
43
|
+
/**
|
|
44
|
+
* The tool-world path of one image, for the handle text that lets the model
|
|
45
|
+
* edit it. Absent → an unreadable image keeps the harness placeholder.
|
|
46
|
+
*/
|
|
47
|
+
imagePath?: ImagePathResolver;
|
|
43
48
|
/** How long an unpaired MCP tool handler waits before failing its call. */
|
|
44
49
|
toolResultTimeoutMs?: Live<number>;
|
|
45
50
|
/** How long the inner process gets to acknowledge an abort. */
|
|
@@ -72,6 +77,12 @@ export interface QoderAdapterOptions {
|
|
|
72
77
|
export declare const QODER_PROVIDER = "qoder-cn";
|
|
73
78
|
/** The international edition's route; a second adapter instance serves it. */
|
|
74
79
|
export declare const QODER_INTL_PROVIDER = "qoder-intl";
|
|
80
|
+
/** The window facts one live catalog entry carries. */
|
|
81
|
+
export type WindowFacts = {
|
|
82
|
+
maxInputTokens?: number;
|
|
83
|
+
defaultContextWindow?: number;
|
|
84
|
+
availableContextWindows?: readonly number[];
|
|
85
|
+
};
|
|
75
86
|
/**
|
|
76
87
|
* Decide the context window to report for one live model.
|
|
77
88
|
*
|
|
@@ -80,17 +91,15 @@ export declare const QODER_INTL_PROVIDER = "qoder-intl";
|
|
|
80
91
|
* actually runs with. `maxInputTokens` is NOT a substitute — it is the ceiling
|
|
81
92
|
* for a single request and is often lower than the session window (e.g. 180 000
|
|
82
93
|
* against a 200 000 session), so falling back to it would understate capacity.
|
|
94
|
+
* Callers that accept a user override should validate it against
|
|
95
|
+
* `availableContextWindows` first — see {@link resolveDeclaredWindow}.
|
|
83
96
|
*
|
|
84
97
|
* @param live - the live catalog entry, when the CLI answered.
|
|
85
98
|
* @param override - the user's chosen window for this model, if any.
|
|
86
99
|
* @param fallback - window used when the catalog has nothing for this model.
|
|
87
100
|
* @returns the window in tokens.
|
|
88
101
|
*/
|
|
89
|
-
export declare function pickContextWindow(live:
|
|
90
|
-
maxInputTokens?: number;
|
|
91
|
-
defaultContextWindow?: number;
|
|
92
|
-
availableContextWindows?: readonly number[];
|
|
93
|
-
} | undefined, override: number | undefined, fallback: number): number;
|
|
102
|
+
export declare function pickContextWindow(live: WindowFacts | undefined, override: number | undefined, fallback: number): number;
|
|
94
103
|
/**
|
|
95
104
|
* The window the CLI will actually run this route with.
|
|
96
105
|
*
|
|
@@ -107,11 +116,21 @@ export declare function pickContextWindow(live: {
|
|
|
107
116
|
* @param declared - the window this route publishes to the harness.
|
|
108
117
|
* @returns the window the CLI runs with, or undefined when unknown.
|
|
109
118
|
*/
|
|
110
|
-
export declare function cliContextWindow(live:
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
119
|
+
export declare function cliContextWindow(live: WindowFacts | undefined, declared: number): number | undefined;
|
|
120
|
+
/**
|
|
121
|
+
* The window to publish for one route, which is also the one the CLI is told
|
|
122
|
+
* to run with.
|
|
123
|
+
*
|
|
124
|
+
* A live entry is authoritative: the user's override counts only when the
|
|
125
|
+
* model advertises it. The CLI silently ignores anything else (a 1M choice on
|
|
126
|
+
* a 200K-only route), and dividing by it would understate every reading
|
|
127
|
+
* fivefold. When the catalog is momentarily unavailable, the last successful
|
|
128
|
+
* answer is reused instead of the static 200K default — that fallback used to
|
|
129
|
+
* change the denominator, and the compaction budget, mid-session: a 1M
|
|
130
|
+
* conversation read 1M one turn and 200K the next, and then compacted at what
|
|
131
|
+
* looked like 10%.
|
|
132
|
+
*/
|
|
133
|
+
export declare function resolveDeclaredWindow(live: WindowFacts | undefined, override: number | undefined, remembered: number | undefined, fallback: number): number;
|
|
115
134
|
/**
|
|
116
135
|
* Build the `reasoning` metadata block for a resolved model, or undefined
|
|
117
136
|
* when the model does not support reasoning.
|
|
@@ -138,6 +157,7 @@ export declare class QoderAdapter extends LlmAdapter {
|
|
|
138
157
|
private readonly catalog;
|
|
139
158
|
private readonly usage;
|
|
140
159
|
private readonly readImage;
|
|
160
|
+
private readonly imagePath;
|
|
141
161
|
private readonly brand;
|
|
142
162
|
/**
|
|
143
163
|
* Memoized `Set` view of the disabled-model list.
|
|
@@ -147,6 +167,24 @@ export declare class QoderAdapter extends LlmAdapter {
|
|
|
147
167
|
* hash probe instead of rebuilding a set per call, with no staleness.
|
|
148
168
|
*/
|
|
149
169
|
private disabledCache;
|
|
170
|
+
/**
|
|
171
|
+
* The last window each route resolved to, plus what the model advertised.
|
|
172
|
+
*
|
|
173
|
+
* Reused only when a live entry is missing. Without it a failed refresh fell
|
|
174
|
+
* back to the static 200K, so a 1M session suddenly declared 200K: the ring
|
|
175
|
+
* denominator changed and the compaction engine budgeted against 200K,
|
|
176
|
+
* compacting a history that was still only ~10% of the real window.
|
|
177
|
+
*/
|
|
178
|
+
private readonly knownWindows;
|
|
179
|
+
/**
|
|
180
|
+
* Last verified image capability per route.
|
|
181
|
+
*
|
|
182
|
+
* A missed catalog refresh used to fall back to text-only, and DSH then
|
|
183
|
+
* stripped the pixels before this adapter ran: a vision model silently lost
|
|
184
|
+
* the image for that turn. Remembering the last answer keeps the declared
|
|
185
|
+
* capability stable across a transient miss.
|
|
186
|
+
*/
|
|
187
|
+
private readonly knownVision;
|
|
150
188
|
constructor(options?: QoderAdapterOptions);
|
|
151
189
|
private get defaultMaxTokens();
|
|
152
190
|
private get modelContextWindows();
|
|
@@ -195,8 +233,17 @@ export declare class QoderAdapter extends LlmAdapter {
|
|
|
195
233
|
isModelDisabled(id: string): boolean;
|
|
196
234
|
/** The stored disabled-model set, as a list for rewriting the field. */
|
|
197
235
|
disabledModelIds(): readonly string[];
|
|
198
|
-
/**
|
|
236
|
+
/**
|
|
237
|
+
* The user's window for one model, when the provider accepts it.
|
|
238
|
+
*
|
|
239
|
+
* A stored choice the model does not advertise is not a real choice: the CLI
|
|
240
|
+
* ignores it and keeps its own window, so publishing it would only make the
|
|
241
|
+
* meter divide by a capacity the route never runs with. Until the catalog
|
|
242
|
+
* has answered once the choice is trusted (there is nothing to check yet).
|
|
243
|
+
*/
|
|
199
244
|
contextWindowFor(id: string): number | undefined;
|
|
245
|
+
/** The window this route publishes, remembering it for catalog misses. */
|
|
246
|
+
private declaredWindow;
|
|
200
247
|
/** The stored per-model window map, for rewriting the field. */
|
|
201
248
|
modelWindowChoices(): Readonly<Record<string, number>>;
|
|
202
249
|
listModels(provider: string): Promise<readonly LlmModelInfo[]>;
|
|
@@ -15,6 +15,7 @@ import { CodeBuddyAuth } from './codebuddy.ts';
|
|
|
15
15
|
import type { CodeBuddyModel, CodeBuddyUsageSnapshot } from './codebuddy.ts';
|
|
16
16
|
import type { ImageRequestReader } from './adapter.ts';
|
|
17
17
|
import type { Live } from './live.ts';
|
|
18
|
+
import type { ImagePathResolver } from './render.ts';
|
|
18
19
|
/** The CodeBuddy provider route. */
|
|
19
20
|
export declare const CODEBUDDY_PROVIDER = "codebuddy";
|
|
20
21
|
/** Everything the adapter reads lazily from the plugin config. */
|
|
@@ -38,6 +39,11 @@ export interface CodeBuddyAdapterOptions {
|
|
|
38
39
|
* store) → images degrade to their handle text instead of failing the turn.
|
|
39
40
|
*/
|
|
40
41
|
readImage?: ImageRequestReader;
|
|
42
|
+
/**
|
|
43
|
+
* The tool-world path of one image, for the handle text that lets the model
|
|
44
|
+
* edit it. Absent → an unreadable image keeps the harness placeholder.
|
|
45
|
+
*/
|
|
46
|
+
imagePath?: ImagePathResolver;
|
|
41
47
|
}
|
|
42
48
|
/** Map a harness-side id onto a CodeBuddy model value. */
|
|
43
49
|
export declare function resolveCodeBuddyModelId(model: string, aliases?: Readonly<Record<string, string>>): string;
|
|
@@ -49,6 +55,8 @@ export declare class CodeBuddyAdapter extends LlmAdapter {
|
|
|
49
55
|
private readonly accounts;
|
|
50
56
|
private readonly auth;
|
|
51
57
|
private readonly disabledCache;
|
|
58
|
+
/** Last verified image capability, keyed `${region}:${model}`. */
|
|
59
|
+
private readonly knownVision;
|
|
52
60
|
constructor(options?: CodeBuddyAdapterOptions);
|
|
53
61
|
private get region();
|
|
54
62
|
private disabledFor;
|
|
@@ -108,6 +116,18 @@ export declare class CodeBuddyAdapter extends LlmAdapter {
|
|
|
108
116
|
stream(options: GenerateOptions): AsyncIterable<StreamChunk>;
|
|
109
117
|
private dispatch;
|
|
110
118
|
private firstTurn;
|
|
119
|
+
/**
|
|
120
|
+
* The context window the host and the inner session must agree on.
|
|
121
|
+
*
|
|
122
|
+
* Only a model that offers selectable budgets is configured: one fixed
|
|
123
|
+
* window rejects every value, so the CLI's own default already matches what
|
|
124
|
+
* the host shows. For a model with choices the adapter sends the picked
|
|
125
|
+
* value; returning to "default" sends the model's own default budget, since
|
|
126
|
+
* the CLI rejects a null clear (`contextWindow must be a positive safe
|
|
127
|
+
* integer`). The override lives for the life of the CLI process, so the
|
|
128
|
+
* default has to be sent explicitly to drop a previous choice.
|
|
129
|
+
*/
|
|
130
|
+
private windowOption;
|
|
111
131
|
private cwdOf;
|
|
112
132
|
close(): void;
|
|
113
133
|
}
|
package/lib/types/codebuddy.d.ts
CHANGED
|
@@ -253,6 +253,10 @@ export declare class CodeBuddySession {
|
|
|
253
253
|
private effort;
|
|
254
254
|
private systemPrompt;
|
|
255
255
|
private cwd;
|
|
256
|
+
/** Context budget the host shows for this session, when a model offers one. */
|
|
257
|
+
private contextWindow;
|
|
258
|
+
/** Budget already pushed to the CLI, keyed by model and value. */
|
|
259
|
+
private appliedWindow;
|
|
256
260
|
/** Host messages already fed to the inner session, for continuation planning. */
|
|
257
261
|
fedMessages: readonly import('@deepseek-ai/dsh-llm').RequestMessage[] | undefined;
|
|
258
262
|
/** Host system prompt last applied; informational for the adapter. */
|
|
@@ -266,7 +270,26 @@ export declare class CodeBuddySession {
|
|
|
266
270
|
effort?: string;
|
|
267
271
|
system?: string;
|
|
268
272
|
cwd?: string;
|
|
273
|
+
contextWindow?: number;
|
|
269
274
|
}): void;
|
|
275
|
+
/**
|
|
276
|
+
* Push the host-visible context window into the inner session.
|
|
277
|
+
*
|
|
278
|
+
* The CLI resolves its own budget (`override > model default > physical
|
|
279
|
+
* limit`) and both its compaction trigger and the upstream gateway enforce
|
|
280
|
+
* that budget. A global-catalog model such as `deepseek-v4.1-flash` declares
|
|
281
|
+
* `supportedLengths [300K, 1M]` with a 300K default, so a session left alone
|
|
282
|
+
* compacts (and the gateway rejects) anything past 276K — while the host,
|
|
283
|
+
* told 1M, keeps showing and feeding a larger history. Replaying that history
|
|
284
|
+
* then collapses to a summary. Applying the same number the host divides by
|
|
285
|
+
* keeps the two in step; models that expose no choice reject it and keep
|
|
286
|
+
* their default.
|
|
287
|
+
*
|
|
288
|
+
* The CLI refuses to clear the override (`contextWindow must be a positive
|
|
289
|
+
* safe integer`), so the adapter sends the model's default instead when the
|
|
290
|
+
* page returns to "default".
|
|
291
|
+
*/
|
|
292
|
+
private applyContextWindow;
|
|
270
293
|
/**
|
|
271
294
|
* Remember the host tools and register them with this session's MCP server.
|
|
272
295
|
*
|
package/lib/types/index.d.ts
CHANGED
|
@@ -41,10 +41,20 @@ interface WebServerService {
|
|
|
41
41
|
handler: (req: IncomingMessage, res: ServerResponse) => void | Promise<void>;
|
|
42
42
|
}): () => void;
|
|
43
43
|
}
|
|
44
|
+
/**
|
|
45
|
+
* The mounted filesystem provider, used only to translate a host path into
|
|
46
|
+
* the execution world the host tools run in. Declared minimally so the plugin
|
|
47
|
+
* keeps only the peer dependencies it actually needs.
|
|
48
|
+
*/
|
|
49
|
+
interface FileSystemService {
|
|
50
|
+
processPathFromHostPath?(hostPath: string): string | undefined;
|
|
51
|
+
}
|
|
44
52
|
declare module '@deepseek-ai/cordis' {
|
|
45
53
|
interface Context {
|
|
46
54
|
/** Present only in web compositions; reached through a nested inject. */
|
|
47
55
|
webServer?: WebServerService;
|
|
56
|
+
/** Present when a filesystem provider is mounted; reached by inject. */
|
|
57
|
+
fs?: FileSystemService;
|
|
48
58
|
}
|
|
49
59
|
interface Events {
|
|
50
60
|
/**
|
|
@@ -15,6 +15,7 @@ import { MinimaxAuth } from './minimax.ts';
|
|
|
15
15
|
import type { MinimaxModel, MinimaxUsageSnapshot } from './minimax.ts';
|
|
16
16
|
import type { ImageRequestReader } from './adapter.ts';
|
|
17
17
|
import type { Live } from './live.ts';
|
|
18
|
+
import type { ImagePathResolver } from './render.ts';
|
|
18
19
|
/** The MiniMax Code provider route. */
|
|
19
20
|
export declare const MINIMAX_PROVIDER = "minimax-acp";
|
|
20
21
|
/** Everything the adapter reads lazily from the plugin config. */
|
|
@@ -38,6 +39,11 @@ export interface MinimaxAdapterOptions {
|
|
|
38
39
|
* store) → images degrade to their handle text instead of failing the turn.
|
|
39
40
|
*/
|
|
40
41
|
readImage?: ImageRequestReader;
|
|
42
|
+
/**
|
|
43
|
+
* The tool-world path of one image, for the handle text that lets the model
|
|
44
|
+
* edit it. Absent → an unreadable image keeps the harness placeholder.
|
|
45
|
+
*/
|
|
46
|
+
imagePath?: ImagePathResolver;
|
|
41
47
|
}
|
|
42
48
|
/** Map a harness-side id onto an ACP model value. */
|
|
43
49
|
export declare function resolveMinimaxModelId(model: string, aliases?: Readonly<Record<string, string>>): string;
|
|
@@ -49,6 +55,8 @@ export declare class MinimaxAdapter extends LlmAdapter {
|
|
|
49
55
|
private readonly auth;
|
|
50
56
|
private readonly mcpServer;
|
|
51
57
|
private readonly disabledCache;
|
|
58
|
+
/** Last verified image capability, keyed `${region}:${model}`. */
|
|
59
|
+
private readonly knownVision;
|
|
52
60
|
constructor(options?: MinimaxAdapterOptions);
|
|
53
61
|
private get region();
|
|
54
62
|
private disabledFor;
|
package/lib/types/render.d.ts
CHANGED
|
@@ -22,6 +22,18 @@ export type FeedPart = {
|
|
|
22
22
|
type: 'image';
|
|
23
23
|
attachment: ImageAttachmentRef;
|
|
24
24
|
};
|
|
25
|
+
/** Resolve the tool-world path of one durable image attachment. */
|
|
26
|
+
export type ImagePathResolver = (ref: ImageAttachmentRef) => string | undefined;
|
|
27
|
+
/**
|
|
28
|
+
* Model-facing handle for one image.
|
|
29
|
+
*
|
|
30
|
+
* The host's native adapters use the same text: it names the attachment, its
|
|
31
|
+
* pixel size and media type, and the read-only normalized copy on disk, and it
|
|
32
|
+
* tells the model to copy that file to a writable path before editing. Pixels
|
|
33
|
+
* alone let a model describe an image but not process it — without this path
|
|
34
|
+
* the model goes looking through the workspace for a file that was never there.
|
|
35
|
+
*/
|
|
36
|
+
export declare function imageRefText(ref: ImageAttachmentRef, path: string | undefined): string;
|
|
25
37
|
/** What one inner turn receives: text (the historical shape) or text + images. */
|
|
26
38
|
export type Feed = string | FeedPart[];
|
|
27
39
|
/**
|
|
@@ -31,12 +43,12 @@ export type Feed = string | FeedPart[];
|
|
|
31
43
|
*/
|
|
32
44
|
export declare function imageRefs(blocks: readonly ContentBlock[]): ImageAttachmentRef[];
|
|
33
45
|
/** Render one content-block list as plain text; non-text blocks get handles. */
|
|
34
|
-
export declare function renderBlocks(blocks: readonly ContentBlock[]): string;
|
|
46
|
+
export declare function renderBlocks(blocks: readonly ContentBlock[], imagePath?: ImagePathResolver): string;
|
|
35
47
|
/**
|
|
36
48
|
* Render one request message with its role tag. A request-only user input has
|
|
37
49
|
* no durable identity or source, so it reads exactly like a user message.
|
|
38
50
|
*/
|
|
39
|
-
export declare function renderMessage(message: RequestMessage): string;
|
|
51
|
+
export declare function renderMessage(message: RequestMessage, imagePath?: ImagePathResolver): string;
|
|
40
52
|
/**
|
|
41
53
|
* Join feed sections into one feed. While no section carries an image the
|
|
42
54
|
* result is the historical text shape, sections separated by a blank line;
|
|
@@ -55,13 +67,14 @@ export declare function currentTurnStart(messages: readonly RequestMessage[]): n
|
|
|
55
67
|
/** Image references carried by a feed, in order. */
|
|
56
68
|
export declare function feedImageRefs(feed: Feed): ImageAttachmentRef[];
|
|
57
69
|
/** Flatten a feed back to text, replacing images with the harness handle text. */
|
|
58
|
-
export declare function feedToText(feed: Feed): string;
|
|
70
|
+
export declare function feedToText(feed: Feed, imagePath?: ImagePathResolver): string;
|
|
59
71
|
/** One feed element after image bytes have been resolved (base64). */
|
|
60
72
|
export type ResolvedFeedPart = {
|
|
61
73
|
type: 'text';
|
|
62
74
|
text: string;
|
|
63
75
|
} | {
|
|
64
76
|
type: 'image';
|
|
77
|
+
attachment: ImageAttachmentRef;
|
|
65
78
|
data: string;
|
|
66
79
|
mediaType: string;
|
|
67
80
|
} | {
|
|
@@ -95,7 +108,7 @@ export declare function feedWideCharCount(feed: Feed): number;
|
|
|
95
108
|
* prompt, and the existing conversation as compact context. Messages of the
|
|
96
109
|
* current turn keep their image references; earlier ones are handles.
|
|
97
110
|
*/
|
|
98
|
-
export declare function renderInitialFeed(system: string | undefined, messages: readonly RequestMessage[]): Feed;
|
|
111
|
+
export declare function renderInitialFeed(system: string | undefined, messages: readonly RequestMessage[], imagePath?: ImagePathResolver): Feed;
|
|
99
112
|
/** Render a brand-new host user turn; images stay references, text-only stays a string. */
|
|
100
113
|
export declare function renderUserTurn(blocks: readonly ContentBlock[]): Feed;
|
|
101
114
|
/** Render an in-place-updated message (runtime-context snapshots and the like). */
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "dsh-agent-sdk-bridge",
|
|
3
3
|
"displayName": "Agent SDK Bridge",
|
|
4
4
|
"description": "DeepSeek Harness LLM adapter bridge for Qoder CN, Qoder International, Tencent CodeBuddy and MiniMax Code subscriptions. It reuses each provider's own SDK or official ACP server and CLI login, shows available quota, and exposes per-provider settings pages with independent international and mainland login regions.",
|
|
5
|
-
"version": "1.1.
|
|
5
|
+
"version": "1.1.7",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"license": "MIT",
|
|
8
8
|
"author": "k1-h2",
|
package/src/adapter.ts
CHANGED
|
@@ -23,8 +23,8 @@ import type { ResolvedConfig } from './config.ts'
|
|
|
23
23
|
import type { Live } from './live.ts'
|
|
24
24
|
import { readLive, readLiveOptional } from './live.ts'
|
|
25
25
|
import { DEFAULT_MODEL_CACHE_TTL_MS, DEFAULT_FETCH_TIMEOUT_MS, QoderModelCatalog } from './models.ts'
|
|
26
|
-
import { feedImageRefs, feedToText, imageRefs, joinFeeds, renderInitialFeed, renderRefreshed, renderUserTurn } from './render.ts'
|
|
27
|
-
import type { Feed } from './render.ts'
|
|
26
|
+
import { feedImageRefs, feedToText, imageRefText, imageRefs, joinFeeds, renderInitialFeed, renderRefreshed, renderUserTurn } from './render.ts'
|
|
27
|
+
import type { Feed, ImagePathResolver } from './render.ts'
|
|
28
28
|
import { QoderSession, QoderSessionManager, DEFAULT_MAX_SESSIONS } from './session.ts'
|
|
29
29
|
import type { ChannelContent, ResolvedImages, SessionModelPolicy } from './session.ts'
|
|
30
30
|
import type { UsageSnapshot } from './usage.ts'
|
|
@@ -56,6 +56,11 @@ export interface QoderAdapterOptions {
|
|
|
56
56
|
modelCacheTtlMs?: Live<number>
|
|
57
57
|
/** Request-image reader; absent → images are never forwarded as pixels. */
|
|
58
58
|
readImage?: ImageRequestReader
|
|
59
|
+
/**
|
|
60
|
+
* The tool-world path of one image, for the handle text that lets the model
|
|
61
|
+
* edit it. Absent → an unreadable image keeps the harness placeholder.
|
|
62
|
+
*/
|
|
63
|
+
imagePath?: ImagePathResolver
|
|
59
64
|
/** How long an unpaired MCP tool handler waits before failing its call. */
|
|
60
65
|
toolResultTimeoutMs?: Live<number>
|
|
61
66
|
/** How long the inner process gets to acknowledge an abort. */
|
|
@@ -95,6 +100,13 @@ export const QODER_PROVIDER = 'qoder-cn'
|
|
|
95
100
|
/** The international edition's route; a second adapter instance serves it. */
|
|
96
101
|
export const QODER_INTL_PROVIDER = 'qoder-intl'
|
|
97
102
|
|
|
103
|
+
/** The window facts one live catalog entry carries. */
|
|
104
|
+
export type WindowFacts = {
|
|
105
|
+
maxInputTokens?: number
|
|
106
|
+
defaultContextWindow?: number
|
|
107
|
+
availableContextWindows?: readonly number[]
|
|
108
|
+
}
|
|
109
|
+
|
|
98
110
|
/**
|
|
99
111
|
* Decide the context window to report for one live model.
|
|
100
112
|
*
|
|
@@ -103,6 +115,8 @@ export const QODER_INTL_PROVIDER = 'qoder-intl'
|
|
|
103
115
|
* actually runs with. `maxInputTokens` is NOT a substitute — it is the ceiling
|
|
104
116
|
* for a single request and is often lower than the session window (e.g. 180 000
|
|
105
117
|
* against a 200 000 session), so falling back to it would understate capacity.
|
|
118
|
+
* Callers that accept a user override should validate it against
|
|
119
|
+
* `availableContextWindows` first — see {@link resolveDeclaredWindow}.
|
|
106
120
|
*
|
|
107
121
|
* @param live - the live catalog entry, when the CLI answered.
|
|
108
122
|
* @param override - the user's chosen window for this model, if any.
|
|
@@ -110,7 +124,7 @@ export const QODER_INTL_PROVIDER = 'qoder-intl'
|
|
|
110
124
|
* @returns the window in tokens.
|
|
111
125
|
*/
|
|
112
126
|
export function pickContextWindow(
|
|
113
|
-
live:
|
|
127
|
+
live: WindowFacts | undefined,
|
|
114
128
|
override: number | undefined,
|
|
115
129
|
fallback: number,
|
|
116
130
|
): number {
|
|
@@ -144,16 +158,39 @@ function largestWindow(available: readonly number[] | undefined): number | undef
|
|
|
144
158
|
* @param declared - the window this route publishes to the harness.
|
|
145
159
|
* @returns the window the CLI runs with, or undefined when unknown.
|
|
146
160
|
*/
|
|
147
|
-
export function cliContextWindow(
|
|
148
|
-
live: { maxInputTokens?: number, defaultContextWindow?: number, availableContextWindows?: readonly number[] } | undefined,
|
|
149
|
-
declared: number,
|
|
150
|
-
): number | undefined {
|
|
161
|
+
export function cliContextWindow(live: WindowFacts | undefined, declared: number): number | undefined {
|
|
151
162
|
if (live === undefined) return undefined
|
|
152
163
|
const available = live.availableContextWindows
|
|
153
164
|
if (available !== undefined && available.includes(declared)) return declared
|
|
154
165
|
return live.defaultContextWindow ?? largestWindow(available) ?? live.maxInputTokens
|
|
155
166
|
}
|
|
156
167
|
|
|
168
|
+
/**
|
|
169
|
+
* The window to publish for one route, which is also the one the CLI is told
|
|
170
|
+
* to run with.
|
|
171
|
+
*
|
|
172
|
+
* A live entry is authoritative: the user's override counts only when the
|
|
173
|
+
* model advertises it. The CLI silently ignores anything else (a 1M choice on
|
|
174
|
+
* a 200K-only route), and dividing by it would understate every reading
|
|
175
|
+
* fivefold. When the catalog is momentarily unavailable, the last successful
|
|
176
|
+
* answer is reused instead of the static 200K default — that fallback used to
|
|
177
|
+
* change the denominator, and the compaction budget, mid-session: a 1M
|
|
178
|
+
* conversation read 1M one turn and 200K the next, and then compacted at what
|
|
179
|
+
* looked like 10%.
|
|
180
|
+
*/
|
|
181
|
+
export function resolveDeclaredWindow(
|
|
182
|
+
live: WindowFacts | undefined,
|
|
183
|
+
override: number | undefined,
|
|
184
|
+
remembered: number | undefined,
|
|
185
|
+
fallback: number,
|
|
186
|
+
): number {
|
|
187
|
+
if (live === undefined) return remembered ?? override ?? fallback
|
|
188
|
+
const available = live.availableContextWindows
|
|
189
|
+
const honored = override !== undefined
|
|
190
|
+
&& (available === undefined || available.length === 0 || available.includes(override))
|
|
191
|
+
return pickContextWindow(live, honored ? override : undefined, remembered ?? fallback)
|
|
192
|
+
}
|
|
193
|
+
|
|
157
194
|
function modelInfo(provider: string, entry: { id: string, name: string, description?: string, isVl?: boolean }): LlmModelInfo {
|
|
158
195
|
return {
|
|
159
196
|
provider,
|
|
@@ -237,6 +274,7 @@ export class QoderAdapter extends LlmAdapter {
|
|
|
237
274
|
private readonly catalog: QoderModelCatalog
|
|
238
275
|
private readonly usage: QoderUsage
|
|
239
276
|
private readonly readImage: ImageRequestReader | undefined
|
|
277
|
+
private readonly imagePath: ImagePathResolver | undefined
|
|
240
278
|
private readonly brand: QoderBrand
|
|
241
279
|
/**
|
|
242
280
|
* Memoized `Set` view of the disabled-model list.
|
|
@@ -246,6 +284,24 @@ export class QoderAdapter extends LlmAdapter {
|
|
|
246
284
|
* hash probe instead of rebuilding a set per call, with no staleness.
|
|
247
285
|
*/
|
|
248
286
|
private disabledCache: { source: readonly string[], set: ReadonlySet<string> } | undefined
|
|
287
|
+
/**
|
|
288
|
+
* The last window each route resolved to, plus what the model advertised.
|
|
289
|
+
*
|
|
290
|
+
* Reused only when a live entry is missing. Without it a failed refresh fell
|
|
291
|
+
* back to the static 200K, so a 1M session suddenly declared 200K: the ring
|
|
292
|
+
* denominator changed and the compaction engine budgeted against 200K,
|
|
293
|
+
* compacting a history that was still only ~10% of the real window.
|
|
294
|
+
*/
|
|
295
|
+
private readonly knownWindows = new Map<string, { window: number, available?: readonly number[] }>()
|
|
296
|
+
/**
|
|
297
|
+
* Last verified image capability per route.
|
|
298
|
+
*
|
|
299
|
+
* A missed catalog refresh used to fall back to text-only, and DSH then
|
|
300
|
+
* stripped the pixels before this adapter ran: a vision model silently lost
|
|
301
|
+
* the image for that turn. Remembering the last answer keeps the declared
|
|
302
|
+
* capability stable across a transient miss.
|
|
303
|
+
*/
|
|
304
|
+
private readonly knownVision = new Map<string, boolean>()
|
|
249
305
|
|
|
250
306
|
constructor(options: QoderAdapterOptions = {}) {
|
|
251
307
|
super()
|
|
@@ -264,6 +320,7 @@ export class QoderAdapter extends LlmAdapter {
|
|
|
264
320
|
)
|
|
265
321
|
this.usage = new QoderUsage(this.brand, options.usageCacheTtlMs)
|
|
266
322
|
this.readImage = options.readImage
|
|
323
|
+
this.imagePath = options.imagePath
|
|
267
324
|
}
|
|
268
325
|
|
|
269
326
|
private get defaultMaxTokens(): number {
|
|
@@ -338,7 +395,14 @@ export class QoderAdapter extends LlmAdapter {
|
|
|
338
395
|
* @returns live catalog entries in upstream order.
|
|
339
396
|
*/
|
|
340
397
|
async describeCatalog(): Promise<Awaited<ReturnType<QoderModelCatalog['liveModels']>>> {
|
|
341
|
-
|
|
398
|
+
const live = await this.catalog.liveModels()
|
|
399
|
+
// Seed the remembered windows and capabilities here too, so the settings
|
|
400
|
+
// page's rows and the request path validate against the same catalog.
|
|
401
|
+
for (const entry of live) {
|
|
402
|
+
this.declaredWindow(entry.value, entry)
|
|
403
|
+
this.knownVision.set(entry.value, entry.isVl === true)
|
|
404
|
+
}
|
|
405
|
+
return live
|
|
342
406
|
}
|
|
343
407
|
|
|
344
408
|
/** Forget the cached catalog; the next read asks the CLI again. */
|
|
@@ -375,9 +439,33 @@ export class QoderAdapter extends LlmAdapter {
|
|
|
375
439
|
return [...this.disabledModels]
|
|
376
440
|
}
|
|
377
441
|
|
|
378
|
-
/**
|
|
442
|
+
/**
|
|
443
|
+
* The user's window for one model, when the provider accepts it.
|
|
444
|
+
*
|
|
445
|
+
* A stored choice the model does not advertise is not a real choice: the CLI
|
|
446
|
+
* ignores it and keeps its own window, so publishing it would only make the
|
|
447
|
+
* meter divide by a capacity the route never runs with. Until the catalog
|
|
448
|
+
* has answered once the choice is trusted (there is nothing to check yet).
|
|
449
|
+
*/
|
|
379
450
|
contextWindowFor(id: string): number | undefined {
|
|
380
|
-
|
|
451
|
+
const override = this.windowOverride(id)
|
|
452
|
+
if (override === undefined) return undefined
|
|
453
|
+
const target = resolveQoderModelId(id, this.modelAliases)
|
|
454
|
+
const available = this.knownWindows.get(target)?.available
|
|
455
|
+
if (available === undefined || available.length === 0) return override
|
|
456
|
+
return available.includes(override) ? override : undefined
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
/** The window this route publishes, remembering it for catalog misses. */
|
|
460
|
+
private declaredWindow(id: string, live: WindowFacts | undefined): number {
|
|
461
|
+
const window = resolveDeclaredWindow(
|
|
462
|
+
live,
|
|
463
|
+
this.windowOverride(id),
|
|
464
|
+
this.knownWindows.get(id)?.window,
|
|
465
|
+
DEFAULT_CONTEXT_WINDOW,
|
|
466
|
+
)
|
|
467
|
+
if (live !== undefined) this.knownWindows.set(id, { window, available: live.availableContextWindows })
|
|
468
|
+
return window
|
|
381
469
|
}
|
|
382
470
|
|
|
383
471
|
/** The stored per-model window map, for rewriting the field. */
|
|
@@ -406,6 +494,8 @@ export class QoderAdapter extends LlmAdapter {
|
|
|
406
494
|
const target = resolveQoderModelId(model, this.modelAliases)
|
|
407
495
|
const live = (await this.catalog.liveModels()).find(entry => entry.value === target)
|
|
408
496
|
if (live !== undefined) {
|
|
497
|
+
const vision = live.isVl === true
|
|
498
|
+
this.knownVision.set(target, vision)
|
|
409
499
|
const reasoning = reasoningInfo(live.efforts, live.defaultEffort ?? this.defaultReasoningEffort, live.isReasoning)
|
|
410
500
|
return {
|
|
411
501
|
provider,
|
|
@@ -416,7 +506,7 @@ export class QoderAdapter extends LlmAdapter {
|
|
|
416
506
|
// projects images to handle text for a route that omits it, so a claim
|
|
417
507
|
// the CLI did not make would silently drop pixels; the reverse error
|
|
418
508
|
// would send pixels to a model that cannot read them.
|
|
419
|
-
inputModalities:
|
|
509
|
+
inputModalities: vision ? ['text' as const, 'image' as const] : ['text' as const],
|
|
420
510
|
context: {
|
|
421
511
|
// The compaction engine and context meter price against the window a
|
|
422
512
|
// request actually uses. Upstream reports three different numbers
|
|
@@ -425,11 +515,7 @@ export class QoderAdapter extends LlmAdapter {
|
|
|
425
515
|
// then upstream's own session window. Reporting a window larger than
|
|
426
516
|
// the session really runs with would push the auto-compaction
|
|
427
517
|
// threshold past what the provider accepts.
|
|
428
|
-
contextWindow:
|
|
429
|
-
live,
|
|
430
|
-
this.windowOverride(target),
|
|
431
|
-
DEFAULT_CONTEXT_WINDOW,
|
|
432
|
-
),
|
|
518
|
+
contextWindow: this.declaredWindow(target, live),
|
|
433
519
|
},
|
|
434
520
|
defaultMaxTokens: live.maxOutputTokens ?? this.defaultMaxTokens,
|
|
435
521
|
...reasoning === undefined ? {} : { reasoning },
|
|
@@ -441,12 +527,17 @@ export class QoderAdapter extends LlmAdapter {
|
|
|
441
527
|
? QODER_MODELS.find(entry => entry.id === target)
|
|
442
528
|
: undefined
|
|
443
529
|
const reasoning = reasoningInfo(undefined, this.defaultReasoningEffort, true)
|
|
530
|
+
// The live catalog is momentarily unavailable. Reuse the last verified
|
|
531
|
+
// capability rather than declaring text-only, which would make the harness
|
|
532
|
+
// drop pixels from a vision model for this turn.
|
|
533
|
+
const rememberedVision = this.knownVision.get(target) === true
|
|
444
534
|
return Promise.resolve({
|
|
445
535
|
...configured === undefined
|
|
446
|
-
? { provider, id: model, name: model
|
|
536
|
+
? { provider, id: model, name: model }
|
|
447
537
|
: { ...modelInfo(provider, configured), id: model },
|
|
538
|
+
inputModalities: rememberedVision ? ['text' as const, 'image' as const] : ['text' as const],
|
|
448
539
|
context: {
|
|
449
|
-
contextWindow: this.
|
|
540
|
+
contextWindow: this.declaredWindow(target, undefined),
|
|
450
541
|
},
|
|
451
542
|
defaultMaxTokens: this.defaultMaxTokens,
|
|
452
543
|
...reasoning === undefined ? {} : { reasoning },
|
|
@@ -488,7 +579,7 @@ export class QoderAdapter extends LlmAdapter {
|
|
|
488
579
|
const model = resolveQoderModelId(options.model, this.modelAliases)
|
|
489
580
|
if (options.sessionId === undefined || options.purpose !== undefined) {
|
|
490
581
|
// Side channels summarise text; pixels would only cost them context.
|
|
491
|
-
const prompt = feedToText(renderInitialFeed(options.system, options.messages))
|
|
582
|
+
const prompt = feedToText(renderInitialFeed(options.system, options.messages, this.imagePath), this.imagePath)
|
|
492
583
|
+ '\n(这是一次性旁路请求,直接输出下一条助手回复。)'
|
|
493
584
|
// Side channels (titles, compaction summaries) reuse the main session's
|
|
494
585
|
// model so dsh's recorded summarization target matches what qodercli
|
|
@@ -534,7 +625,7 @@ export class QoderAdapter extends LlmAdapter {
|
|
|
534
625
|
session.fedMessages = options.messages
|
|
535
626
|
session.fedSystem = options.system
|
|
536
627
|
const feed = plan.feed === null ? null : await this.buildFeed(plan.feed, generation.vision, images)
|
|
537
|
-
yield* session.stream(options, feed, plan.feed === null ? undefined : feedToText(plan.feed))
|
|
628
|
+
yield* session.stream(options, feed, plan.feed === null ? undefined : feedToText(plan.feed, this.imagePath))
|
|
538
629
|
}
|
|
539
630
|
|
|
540
631
|
private async *firstTurn(
|
|
@@ -554,9 +645,9 @@ export class QoderAdapter extends LlmAdapter {
|
|
|
554
645
|
session.recordRequestInput(options.system, options.messages)
|
|
555
646
|
session.fedMessages = options.messages
|
|
556
647
|
session.fedSystem = options.system
|
|
557
|
-
const feed = renderInitialFeed(options.system, options.messages)
|
|
648
|
+
const feed = renderInitialFeed(options.system, options.messages, this.imagePath)
|
|
558
649
|
const images = await this.resolveImages(feedImageRefs(feed), vision, options.signal)
|
|
559
|
-
yield* session.stream(options, await this.buildFeed(feed, vision, images), feedToText(feed))
|
|
650
|
+
yield* session.stream(options, await this.buildFeed(feed, vision, images), feedToText(feed, this.imagePath))
|
|
560
651
|
}
|
|
561
652
|
|
|
562
653
|
/** Image references carried by a message tail, skipping assistant content. */
|
|
@@ -604,7 +695,7 @@ export class QoderAdapter extends LlmAdapter {
|
|
|
604
695
|
images: ResolvedImages,
|
|
605
696
|
): Promise<string | ChannelContent[]> {
|
|
606
697
|
if (typeof feed === 'string') return feed
|
|
607
|
-
if (!vision) return feedToText(feed)
|
|
698
|
+
if (!vision) return feedToText(feed, this.imagePath)
|
|
608
699
|
const content: ChannelContent[] = []
|
|
609
700
|
let pixels = 0
|
|
610
701
|
for (const part of feed) {
|
|
@@ -612,11 +703,20 @@ export class QoderAdapter extends LlmAdapter {
|
|
|
612
703
|
if (part.text.length > 0) content.push({ type: 'text', text: part.text })
|
|
613
704
|
continue
|
|
614
705
|
}
|
|
615
|
-
const
|
|
706
|
+
const key = String(part.attachment.attachmentId)
|
|
707
|
+
const resolved = images.get(key)
|
|
708
|
+
const path = this.imagePath?.(part.attachment)
|
|
616
709
|
if (resolved === undefined) {
|
|
617
|
-
content.push({
|
|
710
|
+
content.push({
|
|
711
|
+
type: 'text',
|
|
712
|
+
text: `[图片附件 ${key} 本次未能读取${path === undefined ? '' : `;本机文件:${path}`},请基于文字内容继续]`,
|
|
713
|
+
})
|
|
618
714
|
continue
|
|
619
715
|
}
|
|
716
|
+
// The pixels let the model read the image; this handle lets it find and
|
|
717
|
+
// edit the source file. Without it a "modify this image" turn searches
|
|
718
|
+
// the workspace for a file that was never there.
|
|
719
|
+
content.push({ type: 'text', text: imageRefText(part.attachment, path) })
|
|
620
720
|
pixels += 1
|
|
621
721
|
content.push({
|
|
622
722
|
type: 'image',
|
package/src/api.ts
CHANGED
|
@@ -94,6 +94,9 @@ export async function buildRows(adapter: QoderAdapter): Promise<ModelRow[]> {
|
|
|
94
94
|
...override === undefined ? {} : { override },
|
|
95
95
|
...priceFactor === undefined ? {} : { priceFactor },
|
|
96
96
|
reasoning: entry.isReasoning !== false,
|
|
97
|
+
// One advertised window is a fixed window: the CLI ignores anything
|
|
98
|
+
// else, so the page should show the value instead of a dead selector.
|
|
99
|
+
contextWindowSelectable: (entry.availableContextWindows?.length ?? 0) > 1,
|
|
97
100
|
}
|
|
98
101
|
})
|
|
99
102
|
}
|