dsh-agent-sdk-bridge 1.1.0 → 1.1.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/lib/index.js +241 -25
- package/lib/types/codebuddy-adapter.d.ts +24 -0
- package/lib/types/codebuddy.d.ts +3 -2
- package/lib/types/minimax-adapter.d.ts +17 -0
- package/lib/types/minimax.d.ts +24 -4
- package/lib/types/render.d.ts +25 -0
- package/package.json +1 -1
- package/src/codebuddy-adapter.ts +92 -3
- package/src/codebuddy.ts +40 -5
- package/src/index.ts +2 -0
- package/src/minimax-adapter.ts +77 -4
- package/src/minimax.ts +47 -12
- package/src/render.ts +35 -2
package/lib/index.js
CHANGED
|
@@ -1070,6 +1070,44 @@ function feedToText(feed) {
|
|
|
1070
1070
|
if (typeof feed === "string") return feed;
|
|
1071
1071
|
return feed.map((part) => part.type === "text" ? part.text : textOnlyImageText(part.attachment)).filter((text) => text.length > 0).join("\n\n");
|
|
1072
1072
|
}
|
|
1073
|
+
/**
|
|
1074
|
+
* Split a feed into text and resolved images for providers with a vision API.
|
|
1075
|
+
*
|
|
1076
|
+
* The two non-Qoder adapters (CodeBuddy, MiniMax) speak their own content-block
|
|
1077
|
+
* shapes, so this returns a neutral list rather than a provider shape: text
|
|
1078
|
+
* parts pass through, an image uses its bytes when the host could read them,
|
|
1079
|
+
* and an unreadable image keeps a note naming what was dropped instead of
|
|
1080
|
+
* silently disappearing from the prompt.
|
|
1081
|
+
*/
|
|
1082
|
+
function resolvedFeedParts(feed, images) {
|
|
1083
|
+
const parts = [];
|
|
1084
|
+
if (typeof feed === "string") {
|
|
1085
|
+
if (feed.length > 0) parts.push({
|
|
1086
|
+
type: "text",
|
|
1087
|
+
text: feed
|
|
1088
|
+
});
|
|
1089
|
+
return parts;
|
|
1090
|
+
}
|
|
1091
|
+
for (const part of feed) {
|
|
1092
|
+
if (part.type === "text") {
|
|
1093
|
+
if (part.text.length > 0) parts.push({
|
|
1094
|
+
type: "text",
|
|
1095
|
+
text: part.text
|
|
1096
|
+
});
|
|
1097
|
+
continue;
|
|
1098
|
+
}
|
|
1099
|
+
const resolved = images.get(String(part.attachment.attachmentId));
|
|
1100
|
+
parts.push(resolved === void 0 ? {
|
|
1101
|
+
type: "image-unreadable",
|
|
1102
|
+
attachment: part.attachment
|
|
1103
|
+
} : {
|
|
1104
|
+
type: "image",
|
|
1105
|
+
data: resolved.data,
|
|
1106
|
+
mediaType: resolved.mediaType
|
|
1107
|
+
});
|
|
1108
|
+
}
|
|
1109
|
+
return parts;
|
|
1110
|
+
}
|
|
1073
1111
|
/** Literal characters in a feed; images contribute their pixels, not text. */
|
|
1074
1112
|
function feedTextLength(feed) {
|
|
1075
1113
|
if (typeof feed === "string") return feed.length;
|
|
@@ -1952,7 +1990,7 @@ var QoderSession = class {
|
|
|
1952
1990
|
text: feed
|
|
1953
1991
|
}] : [...feed];
|
|
1954
1992
|
this.feedFallback = fallback !== void 0 && content.some((block) => block.type === "image") ? fallback : void 0;
|
|
1955
|
-
this.turnInputChars += contentChars(content);
|
|
1993
|
+
this.turnInputChars += contentChars$1(content);
|
|
1956
1994
|
this.channel.push({
|
|
1957
1995
|
type: "user",
|
|
1958
1996
|
message: {
|
|
@@ -2408,7 +2446,7 @@ function estimateImageTokens(width, height) {
|
|
|
2408
2446
|
return Math.max(1, Math.ceil(width * height / 750));
|
|
2409
2447
|
}
|
|
2410
2448
|
/** Literal characters one channel content list contributes to the fallback estimate. */
|
|
2411
|
-
function contentChars(content) {
|
|
2449
|
+
function contentChars$1(content) {
|
|
2412
2450
|
return content.reduce((total, block) => total + (block.type === "text" ? block.text.length : 0), 0);
|
|
2413
2451
|
}
|
|
2414
2452
|
/** Safely stringify the SDK error payload for turn diagnostics. */
|
|
@@ -3311,6 +3349,34 @@ var CodeBuddyStateStore = class {
|
|
|
3311
3349
|
*
|
|
3312
3350
|
* @module dsh-agent-sdk-bridge/codebuddy
|
|
3313
3351
|
*/
|
|
3352
|
+
/**
|
|
3353
|
+
* Literal characters in a channel payload; images contribute pixels, not text.
|
|
3354
|
+
*
|
|
3355
|
+
* The per-turn input estimate charges text only, matching how the Qoder path
|
|
3356
|
+
* counts a feed, so a picture cannot inflate the reported prompt size.
|
|
3357
|
+
*/
|
|
3358
|
+
function contentChars(content) {
|
|
3359
|
+
return content.reduce((total, block) => total + (block.type === "text" ? block.text.length : 0), 0);
|
|
3360
|
+
}
|
|
3361
|
+
/**
|
|
3362
|
+
* Wrap one turn's content the way the SDK's `send()` expects it.
|
|
3363
|
+
*
|
|
3364
|
+
* A plain string keeps the historical shape; image turns are wrapped in a
|
|
3365
|
+
* `UserMessage`, because a bare `ContentBlock[]` is not accepted and dropping
|
|
3366
|
+
* the wrapper silently loses the picture.
|
|
3367
|
+
*/
|
|
3368
|
+
function toCodeBuddyUserMessage(sessionId, content) {
|
|
3369
|
+
if (typeof content === "string") return content;
|
|
3370
|
+
return {
|
|
3371
|
+
type: "user",
|
|
3372
|
+
session_id: sessionId,
|
|
3373
|
+
message: {
|
|
3374
|
+
role: "user",
|
|
3375
|
+
content
|
|
3376
|
+
},
|
|
3377
|
+
parent_tool_use_id: null
|
|
3378
|
+
};
|
|
3379
|
+
}
|
|
3314
3380
|
/** Default context capacity assumed for a CodeBuddy model that declares none. */
|
|
3315
3381
|
const CODEBUDDY_DEFAULT_CONTEXT_WINDOW = 2e5;
|
|
3316
3382
|
/** Default output cap when the catalog declares none. */
|
|
@@ -4047,7 +4113,7 @@ var CodeBuddySession = class {
|
|
|
4047
4113
|
}
|
|
4048
4114
|
this.pairing.beginHostRequest(delivered, freshUserTurn);
|
|
4049
4115
|
}
|
|
4050
|
-
/** Run one host turn; `feed` is new user
|
|
4116
|
+
/** Run one host turn; `feed` is new user content, or null for a tool continuation. */
|
|
4051
4117
|
async *stream(options, feed) {
|
|
4052
4118
|
if (this.queue !== null) throw new Error(`CodeBuddy session ${this.sessionId} already has a turn in flight`);
|
|
4053
4119
|
if (this.disposed) throw new Error(`CodeBuddy session ${this.sessionId} was disposed`);
|
|
@@ -4071,8 +4137,11 @@ var CodeBuddySession = class {
|
|
|
4071
4137
|
try {
|
|
4072
4138
|
await session.setModel(this.model);
|
|
4073
4139
|
if (feed !== null && feed.length > 0) {
|
|
4074
|
-
|
|
4075
|
-
|
|
4140
|
+
const content = typeof feed === "string" ? feed : feed.filter((block) => block.type !== "text" || block.text.length > 0);
|
|
4141
|
+
if (content.length > 0) {
|
|
4142
|
+
this.inputChars += typeof content === "string" ? content.length : contentChars(content);
|
|
4143
|
+
await session.send(toCodeBuddyUserMessage(this.sessionId, content));
|
|
4144
|
+
}
|
|
4076
4145
|
}
|
|
4077
4146
|
this.consume(session);
|
|
4078
4147
|
for await (const item of this.queue) {
|
|
@@ -4711,6 +4780,74 @@ var CodeBuddyAdapter = class extends LlmAdapter {
|
|
|
4711
4780
|
isModelDisabled(id, region = this.region) {
|
|
4712
4781
|
return this.disabledFor(region).has(id);
|
|
4713
4782
|
}
|
|
4783
|
+
/** Whether the resolved model accepts images (drives whether pixels are sent). */
|
|
4784
|
+
async modelVision(model) {
|
|
4785
|
+
const target = resolveCodeBuddyModelId(model, this.aliases);
|
|
4786
|
+
return (await this.catalog.models().catch(() => [])).find((entry) => entry.id === target)?.vision === true;
|
|
4787
|
+
}
|
|
4788
|
+
/**
|
|
4789
|
+
* Resolve a feed's images into base64 for the CLI's vision blocks.
|
|
4790
|
+
*
|
|
4791
|
+
* Only references the host can actually read become pixels; the rest keep
|
|
4792
|
+
* their handle text, so one unreadable attachment degrades that part of the
|
|
4793
|
+
* prompt instead of failing the whole turn.
|
|
4794
|
+
*/
|
|
4795
|
+
async resolveFeedImages(feed, vision, signal) {
|
|
4796
|
+
const map = /* @__PURE__ */ new Map();
|
|
4797
|
+
const readImage = this.options.readImage;
|
|
4798
|
+
if (!vision || readImage === void 0) return map;
|
|
4799
|
+
for (const ref of feedImageRefs(feed)) {
|
|
4800
|
+
const key = String(ref.attachmentId);
|
|
4801
|
+
if (map.has(key)) continue;
|
|
4802
|
+
try {
|
|
4803
|
+
const image = await readImage(ref, signal);
|
|
4804
|
+
if (image !== void 0) map.set(key, {
|
|
4805
|
+
data: Buffer.from(image.bytes).toString("base64"),
|
|
4806
|
+
mediaType: image.mediaType
|
|
4807
|
+
});
|
|
4808
|
+
} catch {}
|
|
4809
|
+
}
|
|
4810
|
+
return map;
|
|
4811
|
+
}
|
|
4812
|
+
/**
|
|
4813
|
+
* Turn a feed into what one CLI turn receives.
|
|
4814
|
+
*
|
|
4815
|
+
* A text-only feed stays a plain string (the historical shape). A feed with
|
|
4816
|
+
* readable images becomes the SDK's `ContentBlock[]`, because that is the
|
|
4817
|
+
* only shape the CLI sends to the model as pixels.
|
|
4818
|
+
*/
|
|
4819
|
+
async buildFeed(feed, vision, signal) {
|
|
4820
|
+
if (typeof feed === "string") return feed;
|
|
4821
|
+
if (!vision) return feedToText(feed);
|
|
4822
|
+
const parts = resolvedFeedParts(feed, await this.resolveFeedImages(feed, vision, signal));
|
|
4823
|
+
if (!parts.some((part) => part.type !== "text")) return parts.map((part) => part.type === "text" ? part.text : "").join("\n\n");
|
|
4824
|
+
const content = [];
|
|
4825
|
+
for (const part of parts) {
|
|
4826
|
+
if (part.type === "text") {
|
|
4827
|
+
content.push({
|
|
4828
|
+
type: "text",
|
|
4829
|
+
text: part.text
|
|
4830
|
+
});
|
|
4831
|
+
continue;
|
|
4832
|
+
}
|
|
4833
|
+
if (part.type === "image-unreadable") {
|
|
4834
|
+
content.push({
|
|
4835
|
+
type: "text",
|
|
4836
|
+
text: textOnlyImageText(part.attachment)
|
|
4837
|
+
});
|
|
4838
|
+
continue;
|
|
4839
|
+
}
|
|
4840
|
+
content.push({
|
|
4841
|
+
type: "image",
|
|
4842
|
+
source: {
|
|
4843
|
+
type: "base64",
|
|
4844
|
+
media_type: part.mediaType,
|
|
4845
|
+
data: part.data
|
|
4846
|
+
}
|
|
4847
|
+
});
|
|
4848
|
+
}
|
|
4849
|
+
return content;
|
|
4850
|
+
}
|
|
4714
4851
|
contextWindowFor(id, region = this.region) {
|
|
4715
4852
|
return this.options.modelContextWindows?.(region)[id];
|
|
4716
4853
|
}
|
|
@@ -4790,7 +4927,8 @@ var CodeBuddyAdapter = class extends LlmAdapter {
|
|
|
4790
4927
|
session.ensureTools(options.tools ?? []);
|
|
4791
4928
|
session.fedMessages = options.messages;
|
|
4792
4929
|
session.fedSystem = options.system;
|
|
4793
|
-
const
|
|
4930
|
+
const vision = await this.modelVision(options.model);
|
|
4931
|
+
const feed = plan.feed === null ? null : await this.buildFeed(plan.feed, vision, options.signal);
|
|
4794
4932
|
yield* session.stream(options, feed);
|
|
4795
4933
|
}
|
|
4796
4934
|
async *firstTurn(session, options, region, model, effort) {
|
|
@@ -4802,7 +4940,8 @@ var CodeBuddyAdapter = class extends LlmAdapter {
|
|
|
4802
4940
|
session.ensureTools(options.tools ?? []);
|
|
4803
4941
|
session.fedMessages = options.messages;
|
|
4804
4942
|
session.fedSystem = options.system;
|
|
4805
|
-
const
|
|
4943
|
+
const vision = await this.modelVision(options.model);
|
|
4944
|
+
const feed = await this.buildFeed(renderInitialFeed(void 0, options.messages), vision, options.signal);
|
|
4806
4945
|
yield* session.stream(options, feed);
|
|
4807
4946
|
}
|
|
4808
4947
|
cwdOf(options) {
|
|
@@ -5391,6 +5530,17 @@ async function minimaxAccountCall(region, path, call) {
|
|
|
5391
5530
|
if (baseCode !== void 0 && baseCode !== 0) throw new Error(asString(baseResp?.status_msg) ?? `MiniMax 账号接口错误 ${baseCode}。`);
|
|
5392
5531
|
return payload;
|
|
5393
5532
|
}
|
|
5533
|
+
/**
|
|
5534
|
+
* Literal characters in a prompt payload; images contribute pixels, not text.
|
|
5535
|
+
*
|
|
5536
|
+
* The per-turn input estimate must charge text only, exactly as the Qoder path
|
|
5537
|
+
* counts a feed, so sending a picture cannot inflate the reported prompt size.
|
|
5538
|
+
*/
|
|
5539
|
+
function feedTextLengthOf(prompt) {
|
|
5540
|
+
if (prompt === null) return 0;
|
|
5541
|
+
if (typeof prompt === "string") return prompt.length;
|
|
5542
|
+
return prompt.reduce((total, block) => total + (block.type === "text" ? block.text.length : 0), 0);
|
|
5543
|
+
}
|
|
5394
5544
|
/** One window's numbers, named the way the coding-plan endpoint reports them. */
|
|
5395
5545
|
function quotaWindow(bucket, kind) {
|
|
5396
5546
|
const prefix = kind === "interval" ? "current_interval" : "current_weekly";
|
|
@@ -6060,15 +6210,22 @@ var MinimaxAcp = class {
|
|
|
6060
6210
|
this.sessionId = session.sessionId;
|
|
6061
6211
|
this.configOptions = Array.isArray(session.configOptions) ? [...session.configOptions] : [];
|
|
6062
6212
|
}
|
|
6063
|
-
/**
|
|
6064
|
-
|
|
6213
|
+
/**
|
|
6214
|
+
* Run one prompt; resolves when the agent's turn ends.
|
|
6215
|
+
*
|
|
6216
|
+
* ACP carries images as `{ type: 'image', data, mimeType }` blocks next to
|
|
6217
|
+
* text, which is what makes a pasted picture reach the model at all; a plain
|
|
6218
|
+
* string keeps the historical text-only shape.
|
|
6219
|
+
*/
|
|
6220
|
+
async prompt(prompt) {
|
|
6065
6221
|
if (this.conn === null || this.sessionId === void 0) throw new Error("ACP 会话尚未启动。");
|
|
6222
|
+
const blocks = typeof prompt === "string" ? [{
|
|
6223
|
+
type: "text",
|
|
6224
|
+
text: prompt
|
|
6225
|
+
}] : [...prompt];
|
|
6066
6226
|
const response = await this.conn.prompt({
|
|
6067
6227
|
sessionId: this.sessionId,
|
|
6068
|
-
prompt:
|
|
6069
|
-
type: "text",
|
|
6070
|
-
text
|
|
6071
|
-
}]
|
|
6228
|
+
prompt: blocks
|
|
6072
6229
|
});
|
|
6073
6230
|
return { stopReason: String(response.stopReason) };
|
|
6074
6231
|
}
|
|
@@ -6611,7 +6768,7 @@ var MinimaxSession = class {
|
|
|
6611
6768
|
this.awaitingToolResults = false;
|
|
6612
6769
|
}
|
|
6613
6770
|
}
|
|
6614
|
-
/** Run one harness turn. `feed` is new user
|
|
6771
|
+
/** Run one harness turn. `feed` is new user content, or null for a continuation. */
|
|
6615
6772
|
async *stream(options, feed) {
|
|
6616
6773
|
if (this.disposed) throw new Error(`MiniMax session ${this.sessionId} was disposed`);
|
|
6617
6774
|
if (this.activeTurn) throw new Error(`MiniMax session ${this.sessionId} already has a turn in flight`);
|
|
@@ -6619,7 +6776,7 @@ var MinimaxSession = class {
|
|
|
6619
6776
|
const signal = options.signal;
|
|
6620
6777
|
const turn = {
|
|
6621
6778
|
blockIndex: 0,
|
|
6622
|
-
inputChars: feed
|
|
6779
|
+
inputChars: feedTextLengthOf(feed),
|
|
6623
6780
|
outputChars: 0,
|
|
6624
6781
|
reasoningChars: 0
|
|
6625
6782
|
};
|
|
@@ -6631,7 +6788,7 @@ var MinimaxSession = class {
|
|
|
6631
6788
|
if (!this.promptInFlight) {
|
|
6632
6789
|
await this.startPrompt(feed ?? "");
|
|
6633
6790
|
this.awaitingToolResults = false;
|
|
6634
|
-
} else if (feed !== null && feed
|
|
6791
|
+
} else if (feed !== null && feedTextLengthOf(feed) > 0 && !this.awaitingToolResults) throw new Error("MiniMax 会话正忙:上一轮尚未结束。");
|
|
6635
6792
|
const onAbort = () => {
|
|
6636
6793
|
abortRequested = true;
|
|
6637
6794
|
this.acp?.cancel();
|
|
@@ -6922,12 +7079,12 @@ var MinimaxSession = class {
|
|
|
6922
7079
|
throw error;
|
|
6923
7080
|
}
|
|
6924
7081
|
}
|
|
6925
|
-
async startPrompt(
|
|
7082
|
+
async startPrompt(prompt) {
|
|
6926
7083
|
const acp = this.acp;
|
|
6927
7084
|
if (acp === null) throw new Error("MiniMax ACP 尚未启动。");
|
|
6928
7085
|
this.promptInFlight = true;
|
|
6929
7086
|
this.cancelSent = false;
|
|
6930
|
-
this.launchPrompt(acp,
|
|
7087
|
+
this.launchPrompt(acp, prompt, 0);
|
|
6931
7088
|
}
|
|
6932
7089
|
/**
|
|
6933
7090
|
* Start one ACP prompt and publish its outcome.
|
|
@@ -6938,9 +7095,9 @@ var MinimaxSession = class {
|
|
|
6938
7095
|
* correction is sent immediately after such a cancel, so it is retried with
|
|
6939
7096
|
* a short backoff instead of being reported as a failed turn.
|
|
6940
7097
|
*/
|
|
6941
|
-
async launchPrompt(acp,
|
|
7098
|
+
async launchPrompt(acp, prompt, attempt) {
|
|
6942
7099
|
try {
|
|
6943
|
-
const response = await acp.prompt(
|
|
7100
|
+
const response = await acp.prompt(prompt);
|
|
6944
7101
|
this.events.push({
|
|
6945
7102
|
kind: "prompt-done",
|
|
6946
7103
|
stopReason: response.stopReason
|
|
@@ -6959,7 +7116,7 @@ var MinimaxSession = class {
|
|
|
6959
7116
|
if (/active Turn|already has an active/i.test(message) && attempt < 4 && !this.disposed && this.acp === acp) {
|
|
6960
7117
|
await new Promise((resolve) => setTimeout(resolve, 200 * 2 ** attempt));
|
|
6961
7118
|
if (!this.disposed && this.acp === acp) {
|
|
6962
|
-
await this.launchPrompt(acp,
|
|
7119
|
+
await this.launchPrompt(acp, prompt, attempt + 1);
|
|
6963
7120
|
return;
|
|
6964
7121
|
}
|
|
6965
7122
|
}
|
|
@@ -7483,7 +7640,8 @@ var MinimaxAdapter = class extends LlmAdapter {
|
|
|
7483
7640
|
session.ensureTools(options.tools ?? []);
|
|
7484
7641
|
session.fedMessages = options.messages;
|
|
7485
7642
|
session.fedSystem = options.system;
|
|
7486
|
-
const
|
|
7643
|
+
const vision = await this.modelVision(model, region);
|
|
7644
|
+
const feed = plan.feed === null ? null : await this.buildFeed(plan.feed, vision, options.signal);
|
|
7487
7645
|
yield* session.stream(options, feed);
|
|
7488
7646
|
}
|
|
7489
7647
|
async *firstTurn(session, options, region, model, effort) {
|
|
@@ -7495,9 +7653,65 @@ var MinimaxAdapter = class extends LlmAdapter {
|
|
|
7495
7653
|
session.ensureTools(options.tools ?? []);
|
|
7496
7654
|
session.fedMessages = options.messages;
|
|
7497
7655
|
session.fedSystem = options.system;
|
|
7498
|
-
const
|
|
7656
|
+
const vision = await this.modelVision(model, region);
|
|
7657
|
+
const feed = await this.buildFeed(renderInitialFeed(void 0, options.messages), vision, options.signal);
|
|
7499
7658
|
yield* session.stream(options, feed);
|
|
7500
7659
|
}
|
|
7660
|
+
/** Whether the resolved model accepts images (drives whether pixels are sent). */
|
|
7661
|
+
async modelVision(model, region) {
|
|
7662
|
+
const target = resolveMinimaxModelId(model, this.aliases);
|
|
7663
|
+
return (await this.catalog.models(region).catch(() => [])).find((entry) => entry.id === target)?.vision === true;
|
|
7664
|
+
}
|
|
7665
|
+
/**
|
|
7666
|
+
* Turn a feed into what one ACP prompt carries.
|
|
7667
|
+
*
|
|
7668
|
+
* A text-only feed stays a plain string (the historical shape). A feed with
|
|
7669
|
+
* readable images becomes ACP content blocks, because an image block is the
|
|
7670
|
+
* only thing the agent receives as pixels. An image the host could not read
|
|
7671
|
+
* keeps its handle text, so it degrades one part instead of failing the turn.
|
|
7672
|
+
*/
|
|
7673
|
+
async buildFeed(feed, vision, signal) {
|
|
7674
|
+
if (typeof feed === "string") return feed;
|
|
7675
|
+
if (!vision) return feedToText(feed);
|
|
7676
|
+
const images = /* @__PURE__ */ new Map();
|
|
7677
|
+
const readImage = this.options.readImage;
|
|
7678
|
+
if (readImage !== void 0) for (const ref of feedImageRefs(feed)) {
|
|
7679
|
+
const key = String(ref.attachmentId);
|
|
7680
|
+
if (images.has(key)) continue;
|
|
7681
|
+
try {
|
|
7682
|
+
const image = await readImage(ref, signal);
|
|
7683
|
+
if (image !== void 0) images.set(key, {
|
|
7684
|
+
data: Buffer.from(image.bytes).toString("base64"),
|
|
7685
|
+
mediaType: image.mediaType
|
|
7686
|
+
});
|
|
7687
|
+
} catch {}
|
|
7688
|
+
}
|
|
7689
|
+
const parts = resolvedFeedParts(feed, images);
|
|
7690
|
+
if (!parts.some((part) => part.type !== "text")) return parts.map((part) => part.type === "text" ? part.text : "").join("\n\n");
|
|
7691
|
+
const content = [];
|
|
7692
|
+
for (const part of parts) {
|
|
7693
|
+
if (part.type === "text") {
|
|
7694
|
+
content.push({
|
|
7695
|
+
type: "text",
|
|
7696
|
+
text: part.text
|
|
7697
|
+
});
|
|
7698
|
+
continue;
|
|
7699
|
+
}
|
|
7700
|
+
if (part.type === "image-unreadable") {
|
|
7701
|
+
content.push({
|
|
7702
|
+
type: "text",
|
|
7703
|
+
text: textOnlyImageText(part.attachment)
|
|
7704
|
+
});
|
|
7705
|
+
continue;
|
|
7706
|
+
}
|
|
7707
|
+
content.push({
|
|
7708
|
+
type: "image",
|
|
7709
|
+
data: part.data,
|
|
7710
|
+
mimeType: part.mediaType
|
|
7711
|
+
});
|
|
7712
|
+
}
|
|
7713
|
+
return content;
|
|
7714
|
+
}
|
|
7501
7715
|
cwdOf(options) {
|
|
7502
7716
|
return options.cwd;
|
|
7503
7717
|
}
|
|
@@ -8392,7 +8606,8 @@ function apply(ctx, config) {
|
|
|
8392
8606
|
region: live.codebuddyRegion,
|
|
8393
8607
|
disabledModels: (region) => region === "cn" ? live.codebuddyCnDisabledModels() : live.codebuddyGlobalDisabledModels(),
|
|
8394
8608
|
modelContextWindows: (region) => region === "cn" ? live.codebuddyCnModelContextWindows() : live.codebuddyGlobalModelContextWindows(),
|
|
8395
|
-
modelAliases: live.codebuddyModelAliases
|
|
8609
|
+
modelAliases: live.codebuddyModelAliases,
|
|
8610
|
+
readImage: shared.readImage
|
|
8396
8611
|
}),
|
|
8397
8612
|
minimax: new MinimaxAdapter({
|
|
8398
8613
|
maxSessions: live.maxSessions,
|
|
@@ -8406,7 +8621,8 @@ function apply(ctx, config) {
|
|
|
8406
8621
|
region: live.minimaxRegion,
|
|
8407
8622
|
disabledModels: (region) => region === "cn" ? live.minimaxCnDisabledModels() : live.minimaxGlobalDisabledModels(),
|
|
8408
8623
|
modelContextWindows: (region) => region === "cn" ? live.minimaxCnModelContextWindows() : live.minimaxGlobalModelContextWindows(),
|
|
8409
|
-
modelAliases: live.minimaxModelAliases
|
|
8624
|
+
modelAliases: live.minimaxModelAliases,
|
|
8625
|
+
readImage: shared.readImage
|
|
8410
8626
|
})
|
|
8411
8627
|
};
|
|
8412
8628
|
ctx.inject(["attachments"], (scope) => {
|
|
@@ -13,6 +13,7 @@ import type { GenerateOptions, LlmModelInfo, LlmProviderInfo, LlmResolvedModelIn
|
|
|
13
13
|
import type { CodeBuddyRegion } from './brand.ts';
|
|
14
14
|
import { CodeBuddyAuth } from './codebuddy.ts';
|
|
15
15
|
import type { CodeBuddyModel, CodeBuddyUsageSnapshot } from './codebuddy.ts';
|
|
16
|
+
import type { ImageRequestReader } from './adapter.ts';
|
|
16
17
|
import type { Live } from './live.ts';
|
|
17
18
|
/** The CodeBuddy provider route. */
|
|
18
19
|
export declare const CODEBUDDY_PROVIDER = "codebuddy";
|
|
@@ -32,6 +33,11 @@ export interface CodeBuddyAdapterOptions {
|
|
|
32
33
|
/** Per-region context-window overrides, keyed by model id. */
|
|
33
34
|
modelContextWindows?: (region: CodeBuddyRegion) => Readonly<Record<string, number>>;
|
|
34
35
|
modelAliases?: Live<Readonly<Record<string, string>>>;
|
|
36
|
+
/**
|
|
37
|
+
* Reads image attachment bytes. Absent (a deployment with no attachment
|
|
38
|
+
* store) → images degrade to their handle text instead of failing the turn.
|
|
39
|
+
*/
|
|
40
|
+
readImage?: ImageRequestReader;
|
|
35
41
|
}
|
|
36
42
|
/** Map a harness-side id onto a CodeBuddy model value. */
|
|
37
43
|
export declare function resolveCodeBuddyModelId(model: string, aliases?: Readonly<Record<string, string>>): string;
|
|
@@ -64,6 +70,24 @@ export declare class CodeBuddyAdapter extends LlmAdapter {
|
|
|
64
70
|
startLogin(region: CodeBuddyRegion): ReturnType<CodeBuddyAuth['start']>;
|
|
65
71
|
logout(region: CodeBuddyRegion): ReturnType<CodeBuddyAuth['logout']>;
|
|
66
72
|
isModelDisabled(id: string, region?: CodeBuddyRegion): boolean;
|
|
73
|
+
/** Whether the resolved model accepts images (drives whether pixels are sent). */
|
|
74
|
+
private modelVision;
|
|
75
|
+
/**
|
|
76
|
+
* Resolve a feed's images into base64 for the CLI's vision blocks.
|
|
77
|
+
*
|
|
78
|
+
* Only references the host can actually read become pixels; the rest keep
|
|
79
|
+
* their handle text, so one unreadable attachment degrades that part of the
|
|
80
|
+
* prompt instead of failing the whole turn.
|
|
81
|
+
*/
|
|
82
|
+
private resolveFeedImages;
|
|
83
|
+
/**
|
|
84
|
+
* Turn a feed into what one CLI turn receives.
|
|
85
|
+
*
|
|
86
|
+
* A text-only feed stays a plain string (the historical shape). A feed with
|
|
87
|
+
* readable images becomes the SDK's `ContentBlock[]`, because that is the
|
|
88
|
+
* only shape the CLI sends to the model as pixels.
|
|
89
|
+
*/
|
|
90
|
+
private buildFeed;
|
|
67
91
|
contextWindowFor(id: string, region?: CodeBuddyRegion): number | undefined;
|
|
68
92
|
listModels(provider: string): Promise<readonly LlmModelInfo[]>;
|
|
69
93
|
resolveModel(provider: string, model: string): Promise<LlmResolvedModelInfo>;
|
package/lib/types/codebuddy.d.ts
CHANGED
|
@@ -16,6 +16,7 @@ import type { RawLanguageModel } from '@tencent-ai/agent-sdk';
|
|
|
16
16
|
import type { CodeBuddyRegion } from './brand.ts';
|
|
17
17
|
import { CodeBuddyStateStore } from './codebuddy-state.ts';
|
|
18
18
|
import type { Live } from './live.ts';
|
|
19
|
+
import type { ChannelContent } from './session.ts';
|
|
19
20
|
/** Default context capacity assumed for a CodeBuddy model that declares none. */
|
|
20
21
|
export declare const CODEBUDDY_DEFAULT_CONTEXT_WINDOW = 200000;
|
|
21
22
|
/** Default output cap when the catalog declares none. */
|
|
@@ -254,8 +255,8 @@ export declare class CodeBuddySession {
|
|
|
254
255
|
content?: unknown;
|
|
255
256
|
isError?: boolean;
|
|
256
257
|
}[]): void;
|
|
257
|
-
/** Run one host turn; `feed` is new user
|
|
258
|
-
stream(options: GenerateOptions, feed: string | null): AsyncGenerator<StreamChunk>;
|
|
258
|
+
/** Run one host turn; `feed` is new user content, or null for a tool continuation. */
|
|
259
|
+
stream(options: GenerateOptions, feed: string | ChannelContent[] | null): AsyncGenerator<StreamChunk>;
|
|
259
260
|
/** Tear the inner CLI down; parked handlers settle with a cancellation. */
|
|
260
261
|
close(): void;
|
|
261
262
|
private ensureStarted;
|
|
@@ -13,6 +13,7 @@ import type { GenerateOptions, LlmModelInfo, LlmProviderInfo, LlmResolvedModelIn
|
|
|
13
13
|
import type { MinimaxRegion } from './brand.ts';
|
|
14
14
|
import { MinimaxAuth } from './minimax.ts';
|
|
15
15
|
import type { MinimaxModel, MinimaxUsageSnapshot } from './minimax.ts';
|
|
16
|
+
import type { ImageRequestReader } from './adapter.ts';
|
|
16
17
|
import type { Live } from './live.ts';
|
|
17
18
|
/** The MiniMax Code provider route. */
|
|
18
19
|
export declare const MINIMAX_PROVIDER = "minimax-acp";
|
|
@@ -32,6 +33,11 @@ export interface MinimaxAdapterOptions {
|
|
|
32
33
|
/** Per-region context-window overrides, keyed by model id. */
|
|
33
34
|
modelContextWindows?: (region: MinimaxRegion) => Readonly<Record<string, number>>;
|
|
34
35
|
modelAliases?: Live<Readonly<Record<string, string>>>;
|
|
36
|
+
/**
|
|
37
|
+
* Reads image attachment bytes. Absent (a deployment with no attachment
|
|
38
|
+
* store) → images degrade to their handle text instead of failing the turn.
|
|
39
|
+
*/
|
|
40
|
+
readImage?: ImageRequestReader;
|
|
35
41
|
}
|
|
36
42
|
/** Map a harness-side id onto an ACP model value. */
|
|
37
43
|
export declare function resolveMinimaxModelId(model: string, aliases?: Readonly<Record<string, string>>): string;
|
|
@@ -81,6 +87,17 @@ export declare class MinimaxAdapter extends LlmAdapter {
|
|
|
81
87
|
stream(options: GenerateOptions): AsyncIterable<StreamChunk>;
|
|
82
88
|
private dispatch;
|
|
83
89
|
private firstTurn;
|
|
90
|
+
/** Whether the resolved model accepts images (drives whether pixels are sent). */
|
|
91
|
+
private modelVision;
|
|
92
|
+
/**
|
|
93
|
+
* Turn a feed into what one ACP prompt carries.
|
|
94
|
+
*
|
|
95
|
+
* A text-only feed stays a plain string (the historical shape). A feed with
|
|
96
|
+
* readable images becomes ACP content blocks, because an image block is the
|
|
97
|
+
* only thing the agent receives as pixels. An image the host could not read
|
|
98
|
+
* keeps its handle text, so it degrades one part instead of failing the turn.
|
|
99
|
+
*/
|
|
100
|
+
private buildFeed;
|
|
84
101
|
private cwdOf;
|
|
85
102
|
close(): void;
|
|
86
103
|
}
|
package/lib/types/minimax.d.ts
CHANGED
|
@@ -54,6 +54,20 @@ export interface MinimaxMcpToolResult {
|
|
|
54
54
|
}[];
|
|
55
55
|
isError?: boolean;
|
|
56
56
|
}
|
|
57
|
+
/**
|
|
58
|
+
* One ACP prompt content block.
|
|
59
|
+
*
|
|
60
|
+
* ACP defines image blocks as base64 `data` plus a `mimeType`; text blocks are
|
|
61
|
+
* the historical shape and remain the only kind a text-only feed produces.
|
|
62
|
+
*/
|
|
63
|
+
export type MinimaxPromptBlock = {
|
|
64
|
+
type: 'text';
|
|
65
|
+
text: string;
|
|
66
|
+
} | {
|
|
67
|
+
type: 'image';
|
|
68
|
+
data: string;
|
|
69
|
+
mimeType: string;
|
|
70
|
+
};
|
|
57
71
|
/** Login progress and state for one region. */
|
|
58
72
|
export interface MinimaxAuthStatus {
|
|
59
73
|
running: boolean;
|
|
@@ -290,8 +304,14 @@ export declare class MinimaxAcp {
|
|
|
290
304
|
get currentModel(): string | undefined;
|
|
291
305
|
/** Start the process and create one ACP session. */
|
|
292
306
|
start(): Promise<void>;
|
|
293
|
-
/**
|
|
294
|
-
|
|
307
|
+
/**
|
|
308
|
+
* Run one prompt; resolves when the agent's turn ends.
|
|
309
|
+
*
|
|
310
|
+
* ACP carries images as `{ type: 'image', data, mimeType }` blocks next to
|
|
311
|
+
* text, which is what makes a pasted picture reach the model at all; a plain
|
|
312
|
+
* string keeps the historical text-only shape.
|
|
313
|
+
*/
|
|
314
|
+
prompt(prompt: string | readonly MinimaxPromptBlock[]): Promise<{
|
|
295
315
|
stopReason: string;
|
|
296
316
|
}>;
|
|
297
317
|
/** Ask the agent to stop the active turn. */
|
|
@@ -454,8 +474,8 @@ export declare class MinimaxSession {
|
|
|
454
474
|
content?: unknown;
|
|
455
475
|
isError?: boolean;
|
|
456
476
|
}[]): void;
|
|
457
|
-
/** Run one harness turn. `feed` is new user
|
|
458
|
-
stream(options: GenerateOptions, feed: string | null): AsyncGenerator<StreamChunk>;
|
|
477
|
+
/** Run one harness turn. `feed` is new user content, or null for a continuation. */
|
|
478
|
+
stream(options: GenerateOptions, feed: string | readonly MinimaxPromptBlock[] | null): AsyncGenerator<StreamChunk>;
|
|
459
479
|
/** Tear the ACP process down; parked handlers settle with a cancellation. */
|
|
460
480
|
close(): void;
|
|
461
481
|
private ensureStarted;
|
package/lib/types/render.d.ts
CHANGED
|
@@ -56,6 +56,31 @@ export declare function currentTurnStart(messages: readonly RequestMessage[]): n
|
|
|
56
56
|
export declare function feedImageRefs(feed: Feed): ImageAttachmentRef[];
|
|
57
57
|
/** Flatten a feed back to text, replacing images with the harness handle text. */
|
|
58
58
|
export declare function feedToText(feed: Feed): string;
|
|
59
|
+
/** One feed element after image bytes have been resolved (base64). */
|
|
60
|
+
export type ResolvedFeedPart = {
|
|
61
|
+
type: 'text';
|
|
62
|
+
text: string;
|
|
63
|
+
} | {
|
|
64
|
+
type: 'image';
|
|
65
|
+
data: string;
|
|
66
|
+
mediaType: string;
|
|
67
|
+
} | {
|
|
68
|
+
type: 'image-unreadable';
|
|
69
|
+
attachment: ImageAttachmentRef;
|
|
70
|
+
};
|
|
71
|
+
/**
|
|
72
|
+
* Split a feed into text and resolved images for providers with a vision API.
|
|
73
|
+
*
|
|
74
|
+
* The two non-Qoder adapters (CodeBuddy, MiniMax) speak their own content-block
|
|
75
|
+
* shapes, so this returns a neutral list rather than a provider shape: text
|
|
76
|
+
* parts pass through, an image uses its bytes when the host could read them,
|
|
77
|
+
* and an unreadable image keeps a note naming what was dropped instead of
|
|
78
|
+
* silently disappearing from the prompt.
|
|
79
|
+
*/
|
|
80
|
+
export declare function resolvedFeedParts(feed: Feed, images: ReadonlyMap<string, {
|
|
81
|
+
data: string;
|
|
82
|
+
mediaType: string;
|
|
83
|
+
}>): ResolvedFeedPart[];
|
|
59
84
|
/** Literal characters in a feed; images contribute their pixels, not text. */
|
|
60
85
|
export declare function feedTextLength(feed: Feed): number;
|
|
61
86
|
/**
|
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.1",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"license": "MIT",
|
|
8
8
|
"author": "k1-h2",
|
package/src/codebuddy-adapter.ts
CHANGED
|
@@ -14,13 +14,17 @@ import type {
|
|
|
14
14
|
GenerateOptions, LlmModelInfo, LlmProviderInfo, LlmResolvedModelInfo,
|
|
15
15
|
PreparedAdapterCall, RequestMessage, StreamChunk,
|
|
16
16
|
} from '@deepseek-ai/dsh-llm'
|
|
17
|
+
import { textOnlyImageText } from '@deepseek-ai/dsh-llm'
|
|
17
18
|
import type { CodeBuddyRegion } from './brand.ts'
|
|
18
19
|
import { codeBuddyColdStream, CodeBuddyAccountReader, CodeBuddyAuth, CodeBuddyCatalog, CodeBuddySessionManager, CODEBUDDY_DEFAULT_CONTEXT_WINDOW, CODEBUDDY_DEFAULT_MAX_TOKENS, CODEBUDDY_CACHE_TTL_MS, CODEBUDDY_FETCH_TIMEOUT_MS } from './codebuddy.ts'
|
|
19
20
|
import type { CodeBuddyModel, CodeBuddyUsageSnapshot } from './codebuddy.ts'
|
|
20
21
|
import { reasoningInfo, planContinuation } from './adapter.ts'
|
|
22
|
+
import type { ImageRequestReader } from './adapter.ts'
|
|
21
23
|
import type { Live } from './live.ts'
|
|
22
24
|
import { readLive, readLiveOptional } from './live.ts'
|
|
23
|
-
import {
|
|
25
|
+
import type { Feed } from './render.ts'
|
|
26
|
+
import { feedImageRefs, feedToText, renderInitialFeed, resolvedFeedParts } from './render.ts'
|
|
27
|
+
import type { ChannelContent } from './session.ts'
|
|
24
28
|
|
|
25
29
|
/** The CodeBuddy provider route. */
|
|
26
30
|
export const CODEBUDDY_PROVIDER = 'codebuddy'
|
|
@@ -41,6 +45,11 @@ export interface CodeBuddyAdapterOptions {
|
|
|
41
45
|
/** Per-region context-window overrides, keyed by model id. */
|
|
42
46
|
modelContextWindows?: (region: CodeBuddyRegion) => Readonly<Record<string, number>>
|
|
43
47
|
modelAliases?: Live<Readonly<Record<string, string>>>
|
|
48
|
+
/**
|
|
49
|
+
* Reads image attachment bytes. Absent (a deployment with no attachment
|
|
50
|
+
* store) → images degrade to their handle text instead of failing the turn.
|
|
51
|
+
*/
|
|
52
|
+
readImage?: ImageRequestReader
|
|
44
53
|
}
|
|
45
54
|
|
|
46
55
|
const NO_MODELS: readonly string[] = []
|
|
@@ -178,6 +187,78 @@ export class CodeBuddyAdapter extends LlmAdapter {
|
|
|
178
187
|
return this.disabledFor(region).has(id)
|
|
179
188
|
}
|
|
180
189
|
|
|
190
|
+
/** Whether the resolved model accepts images (drives whether pixels are sent). */
|
|
191
|
+
private async modelVision(model: string): Promise<boolean> {
|
|
192
|
+
const target = resolveCodeBuddyModelId(model, this.aliases)
|
|
193
|
+
const live = (await this.catalog.models().catch(() => [])).find(entry => entry.id === target)
|
|
194
|
+
return live?.vision === true
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/**
|
|
198
|
+
* Resolve a feed's images into base64 for the CLI's vision blocks.
|
|
199
|
+
*
|
|
200
|
+
* Only references the host can actually read become pixels; the rest keep
|
|
201
|
+
* their handle text, so one unreadable attachment degrades that part of the
|
|
202
|
+
* prompt instead of failing the whole turn.
|
|
203
|
+
*/
|
|
204
|
+
private async resolveFeedImages(
|
|
205
|
+
feed: Feed,
|
|
206
|
+
vision: boolean,
|
|
207
|
+
signal?: AbortSignal,
|
|
208
|
+
): Promise<ReadonlyMap<string, { data: string, mediaType: string }>> {
|
|
209
|
+
const map = new Map<string, { data: string, mediaType: string }>()
|
|
210
|
+
const readImage = this.options.readImage
|
|
211
|
+
if (!vision || readImage === undefined) return map
|
|
212
|
+
for (const ref of feedImageRefs(feed)) {
|
|
213
|
+
const key = String(ref.attachmentId)
|
|
214
|
+
if (map.has(key)) continue
|
|
215
|
+
try {
|
|
216
|
+
const image = await readImage(ref, signal)
|
|
217
|
+
if (image !== undefined) {
|
|
218
|
+
map.set(key, { data: Buffer.from(image.bytes).toString('base64'), mediaType: image.mediaType })
|
|
219
|
+
}
|
|
220
|
+
} catch {
|
|
221
|
+
// Degrade this attachment to its handle text.
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
return map
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/**
|
|
228
|
+
* Turn a feed into what one CLI turn receives.
|
|
229
|
+
*
|
|
230
|
+
* A text-only feed stays a plain string (the historical shape). A feed with
|
|
231
|
+
* readable images becomes the SDK's `ContentBlock[]`, because that is the
|
|
232
|
+
* only shape the CLI sends to the model as pixels.
|
|
233
|
+
*/
|
|
234
|
+
private async buildFeed(
|
|
235
|
+
feed: Feed,
|
|
236
|
+
vision: boolean,
|
|
237
|
+
signal?: AbortSignal,
|
|
238
|
+
): Promise<string | ChannelContent[]> {
|
|
239
|
+
if (typeof feed === 'string') return feed
|
|
240
|
+
if (!vision) return feedToText(feed)
|
|
241
|
+
const images = await this.resolveFeedImages(feed, vision, signal)
|
|
242
|
+
const parts = resolvedFeedParts(feed, images)
|
|
243
|
+
if (!parts.some(part => part.type !== 'text')) return parts.map(part => part.type === 'text' ? part.text : '').join('\n\n')
|
|
244
|
+
const content: ChannelContent[] = []
|
|
245
|
+
for (const part of parts) {
|
|
246
|
+
if (part.type === 'text') {
|
|
247
|
+
content.push({ type: 'text', text: part.text })
|
|
248
|
+
continue
|
|
249
|
+
}
|
|
250
|
+
if (part.type === 'image-unreadable') {
|
|
251
|
+
content.push({ type: 'text', text: textOnlyImageText(part.attachment) })
|
|
252
|
+
continue
|
|
253
|
+
}
|
|
254
|
+
content.push({
|
|
255
|
+
type: 'image',
|
|
256
|
+
source: { type: 'base64', media_type: part.mediaType, data: part.data },
|
|
257
|
+
})
|
|
258
|
+
}
|
|
259
|
+
return content
|
|
260
|
+
}
|
|
261
|
+
|
|
181
262
|
contextWindowFor(id: string, region: CodeBuddyRegion = this.region): number | undefined {
|
|
182
263
|
return this.options.modelContextWindows?.(region)[id]
|
|
183
264
|
}
|
|
@@ -265,7 +346,10 @@ export class CodeBuddyAdapter extends LlmAdapter {
|
|
|
265
346
|
session.ensureTools(options.tools ?? [])
|
|
266
347
|
session.fedMessages = options.messages
|
|
267
348
|
session.fedSystem = options.system
|
|
268
|
-
const
|
|
349
|
+
const vision = await this.modelVision(options.model)
|
|
350
|
+
const feed = plan.feed === null
|
|
351
|
+
? null
|
|
352
|
+
: await this.buildFeed(plan.feed, vision, options.signal)
|
|
269
353
|
yield* session.stream(options, feed)
|
|
270
354
|
}
|
|
271
355
|
|
|
@@ -280,7 +364,12 @@ export class CodeBuddyAdapter extends LlmAdapter {
|
|
|
280
364
|
session.ensureTools(options.tools ?? [])
|
|
281
365
|
session.fedMessages = options.messages
|
|
282
366
|
session.fedSystem = options.system
|
|
283
|
-
const
|
|
367
|
+
const vision = await this.modelVision(options.model)
|
|
368
|
+
const feed = await this.buildFeed(
|
|
369
|
+
renderInitialFeed(undefined, options.messages),
|
|
370
|
+
vision,
|
|
371
|
+
options.signal,
|
|
372
|
+
)
|
|
284
373
|
yield* session.stream(options, feed)
|
|
285
374
|
}
|
|
286
375
|
|
package/src/codebuddy.ts
CHANGED
|
@@ -27,7 +27,37 @@ import { jsonSchemaToShape } from './jsonschema.ts'
|
|
|
27
27
|
import type { Live } from './live.ts'
|
|
28
28
|
import { readLive } from './live.ts'
|
|
29
29
|
import { codeBuddyEnvironment, loadCodeBuddySdk } from './sdk.ts'
|
|
30
|
-
import type { ToolResultPayload } from './session.ts'
|
|
30
|
+
import type { ChannelContent, ToolResultPayload } from './session.ts'
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Literal characters in a channel payload; images contribute pixels, not text.
|
|
34
|
+
*
|
|
35
|
+
* The per-turn input estimate charges text only, matching how the Qoder path
|
|
36
|
+
* counts a feed, so a picture cannot inflate the reported prompt size.
|
|
37
|
+
*/
|
|
38
|
+
function contentChars(content: readonly ChannelContent[]): number {
|
|
39
|
+
return content.reduce((total, block) => total + (block.type === 'text' ? block.text.length : 0), 0)
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Wrap one turn's content the way the SDK's `send()` expects it.
|
|
44
|
+
*
|
|
45
|
+
* A plain string keeps the historical shape; image turns are wrapped in a
|
|
46
|
+
* `UserMessage`, because a bare `ContentBlock[]` is not accepted and dropping
|
|
47
|
+
* the wrapper silently loses the picture.
|
|
48
|
+
*/
|
|
49
|
+
function toCodeBuddyUserMessage(
|
|
50
|
+
sessionId: string,
|
|
51
|
+
content: string | ChannelContent[],
|
|
52
|
+
): string | UserMessage {
|
|
53
|
+
if (typeof content === 'string') return content
|
|
54
|
+
return {
|
|
55
|
+
type: 'user',
|
|
56
|
+
session_id: sessionId,
|
|
57
|
+
message: { role: 'user', content: content as never },
|
|
58
|
+
parent_tool_use_id: null,
|
|
59
|
+
}
|
|
60
|
+
}
|
|
31
61
|
import { MCP_SERVER_NAME, ToolResultPairing, TurnQueue } from './session.ts'
|
|
32
62
|
import { TurnOutputAccounting, estimateTextTokens } from './tokens.ts'
|
|
33
63
|
|
|
@@ -911,8 +941,8 @@ export class CodeBuddySession {
|
|
|
911
941
|
this.pairing.beginHostRequest(delivered, freshUserTurn)
|
|
912
942
|
}
|
|
913
943
|
|
|
914
|
-
/** Run one host turn; `feed` is new user
|
|
915
|
-
async *stream(options: GenerateOptions, feed: string | null): AsyncGenerator<StreamChunk> {
|
|
944
|
+
/** Run one host turn; `feed` is new user content, or null for a tool continuation. */
|
|
945
|
+
async *stream(options: GenerateOptions, feed: string | ChannelContent[] | null): AsyncGenerator<StreamChunk> {
|
|
916
946
|
if (this.queue !== null) throw new Error(`CodeBuddy session ${this.sessionId} already has a turn in flight`)
|
|
917
947
|
if (this.disposed) throw new Error(`CodeBuddy session ${this.sessionId} was disposed`)
|
|
918
948
|
const session = await this.ensureStarted()
|
|
@@ -931,8 +961,13 @@ export class CodeBuddySession {
|
|
|
931
961
|
// keeps the next prompt on the model the host just resolved.
|
|
932
962
|
await session.setModel(this.model)
|
|
933
963
|
if (feed !== null && feed.length > 0) {
|
|
934
|
-
|
|
935
|
-
|
|
964
|
+
const content = typeof feed === 'string'
|
|
965
|
+
? feed
|
|
966
|
+
: feed.filter(block => block.type !== 'text' || block.text.length > 0)
|
|
967
|
+
if (content.length > 0) {
|
|
968
|
+
this.inputChars += typeof content === 'string' ? content.length : contentChars(content)
|
|
969
|
+
await session.send(toCodeBuddyUserMessage(this.sessionId, content))
|
|
970
|
+
}
|
|
936
971
|
}
|
|
937
972
|
void this.consume(session)
|
|
938
973
|
for await (const item of this.queue) {
|
package/src/index.ts
CHANGED
|
@@ -278,6 +278,7 @@ export function apply(ctx: Context, config: ParsedConfig): void {
|
|
|
278
278
|
? live.codebuddyCnModelContextWindows()
|
|
279
279
|
: live.codebuddyGlobalModelContextWindows(),
|
|
280
280
|
modelAliases: live.codebuddyModelAliases,
|
|
281
|
+
readImage: shared.readImage,
|
|
281
282
|
}),
|
|
282
283
|
minimax: new MinimaxAdapter({
|
|
283
284
|
maxSessions: live.maxSessions,
|
|
@@ -296,6 +297,7 @@ export function apply(ctx: Context, config: ParsedConfig): void {
|
|
|
296
297
|
? live.minimaxCnModelContextWindows()
|
|
297
298
|
: live.minimaxGlobalModelContextWindows(),
|
|
298
299
|
modelAliases: live.minimaxModelAliases,
|
|
300
|
+
readImage: shared.readImage,
|
|
299
301
|
}),
|
|
300
302
|
}
|
|
301
303
|
|
package/src/minimax-adapter.ts
CHANGED
|
@@ -22,12 +22,15 @@ import {
|
|
|
22
22
|
minimaxColdStream, minimaxRegionBlockMessage, minimaxRegionBlocked,
|
|
23
23
|
minimaxRegionBlockedCached, minimaxSignedIn, minimaxUsage,
|
|
24
24
|
} from './minimax.ts'
|
|
25
|
-
import
|
|
25
|
+
import { textOnlyImageText } from '@deepseek-ai/dsh-llm'
|
|
26
|
+
import type { MinimaxModel, MinimaxPromptBlock, MinimaxUsageSnapshot } from './minimax.ts'
|
|
26
27
|
import { resolveMinimaxCli } from './minimax-cli.ts'
|
|
27
28
|
import { reasoningInfo, planContinuation } from './adapter.ts'
|
|
29
|
+
import type { ImageRequestReader } from './adapter.ts'
|
|
28
30
|
import type { Live } from './live.ts'
|
|
29
31
|
import { readLive, readLiveOptional } from './live.ts'
|
|
30
|
-
import {
|
|
32
|
+
import type { Feed } from './render.ts'
|
|
33
|
+
import { feedImageRefs, feedToText, renderInitialFeed, resolvedFeedParts } from './render.ts'
|
|
31
34
|
|
|
32
35
|
/** The MiniMax Code provider route. */
|
|
33
36
|
export const MINIMAX_PROVIDER = 'minimax-acp'
|
|
@@ -48,6 +51,11 @@ export interface MinimaxAdapterOptions {
|
|
|
48
51
|
/** Per-region context-window overrides, keyed by model id. */
|
|
49
52
|
modelContextWindows?: (region: MinimaxRegion) => Readonly<Record<string, number>>
|
|
50
53
|
modelAliases?: Live<Readonly<Record<string, string>>>
|
|
54
|
+
/**
|
|
55
|
+
* Reads image attachment bytes. Absent (a deployment with no attachment
|
|
56
|
+
* store) → images degrade to their handle text instead of failing the turn.
|
|
57
|
+
*/
|
|
58
|
+
readImage?: ImageRequestReader
|
|
51
59
|
}
|
|
52
60
|
|
|
53
61
|
const NO_MODELS: readonly string[] = []
|
|
@@ -335,7 +343,10 @@ export class MinimaxAdapter extends LlmAdapter {
|
|
|
335
343
|
session.ensureTools(options.tools ?? [])
|
|
336
344
|
session.fedMessages = options.messages
|
|
337
345
|
session.fedSystem = options.system
|
|
338
|
-
const
|
|
346
|
+
const vision = await this.modelVision(model, region)
|
|
347
|
+
const feed = plan.feed === null
|
|
348
|
+
? null
|
|
349
|
+
: await this.buildFeed(plan.feed, vision, options.signal)
|
|
339
350
|
yield* session.stream(options, feed)
|
|
340
351
|
}
|
|
341
352
|
|
|
@@ -354,10 +365,72 @@ export class MinimaxAdapter extends LlmAdapter {
|
|
|
354
365
|
session.ensureTools(options.tools ?? [])
|
|
355
366
|
session.fedMessages = options.messages
|
|
356
367
|
session.fedSystem = options.system
|
|
357
|
-
const
|
|
368
|
+
const vision = await this.modelVision(model, region)
|
|
369
|
+
const feed = await this.buildFeed(
|
|
370
|
+
renderInitialFeed(undefined, options.messages),
|
|
371
|
+
vision,
|
|
372
|
+
options.signal,
|
|
373
|
+
)
|
|
358
374
|
yield* session.stream(options, feed)
|
|
359
375
|
}
|
|
360
376
|
|
|
377
|
+
/** Whether the resolved model accepts images (drives whether pixels are sent). */
|
|
378
|
+
private async modelVision(model: string, region: MinimaxRegion): Promise<boolean> {
|
|
379
|
+
const target = resolveMinimaxModelId(model, this.aliases)
|
|
380
|
+
const live = (await this.catalog.models(region).catch(() => [])).find(entry => entry.id === target)
|
|
381
|
+
return live?.vision === true
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
/**
|
|
385
|
+
* Turn a feed into what one ACP prompt carries.
|
|
386
|
+
*
|
|
387
|
+
* A text-only feed stays a plain string (the historical shape). A feed with
|
|
388
|
+
* readable images becomes ACP content blocks, because an image block is the
|
|
389
|
+
* only thing the agent receives as pixels. An image the host could not read
|
|
390
|
+
* keeps its handle text, so it degrades one part instead of failing the turn.
|
|
391
|
+
*/
|
|
392
|
+
private async buildFeed(
|
|
393
|
+
feed: Feed,
|
|
394
|
+
vision: boolean,
|
|
395
|
+
signal?: AbortSignal,
|
|
396
|
+
): Promise<string | MinimaxPromptBlock[]> {
|
|
397
|
+
if (typeof feed === 'string') return feed
|
|
398
|
+
if (!vision) return feedToText(feed)
|
|
399
|
+
const images = new Map<string, { data: string, mediaType: string }>()
|
|
400
|
+
const readImage = this.options.readImage
|
|
401
|
+
if (readImage !== undefined) {
|
|
402
|
+
for (const ref of feedImageRefs(feed)) {
|
|
403
|
+
const key = String(ref.attachmentId)
|
|
404
|
+
if (images.has(key)) continue
|
|
405
|
+
try {
|
|
406
|
+
const image = await readImage(ref, signal)
|
|
407
|
+
if (image !== undefined) {
|
|
408
|
+
images.set(key, { data: Buffer.from(image.bytes).toString('base64'), mediaType: image.mediaType })
|
|
409
|
+
}
|
|
410
|
+
} catch {
|
|
411
|
+
// Degrade this attachment to its handle text.
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
const parts = resolvedFeedParts(feed, images)
|
|
416
|
+
if (!parts.some(part => part.type !== 'text')) {
|
|
417
|
+
return parts.map(part => part.type === 'text' ? part.text : '').join('\n\n')
|
|
418
|
+
}
|
|
419
|
+
const content: MinimaxPromptBlock[] = []
|
|
420
|
+
for (const part of parts) {
|
|
421
|
+
if (part.type === 'text') {
|
|
422
|
+
content.push({ type: 'text', text: part.text })
|
|
423
|
+
continue
|
|
424
|
+
}
|
|
425
|
+
if (part.type === 'image-unreadable') {
|
|
426
|
+
content.push({ type: 'text', text: textOnlyImageText(part.attachment) })
|
|
427
|
+
continue
|
|
428
|
+
}
|
|
429
|
+
content.push({ type: 'image', data: part.data, mimeType: part.mediaType })
|
|
430
|
+
}
|
|
431
|
+
return content
|
|
432
|
+
}
|
|
433
|
+
|
|
361
434
|
private cwdOf(options: GenerateOptions): string | undefined {
|
|
362
435
|
return (options as GenerateOptions & { cwd?: string }).cwd
|
|
363
436
|
}
|
package/src/minimax.ts
CHANGED
|
@@ -77,6 +77,16 @@ export interface MinimaxMcpToolResult {
|
|
|
77
77
|
isError?: boolean
|
|
78
78
|
}
|
|
79
79
|
|
|
80
|
+
/**
|
|
81
|
+
* One ACP prompt content block.
|
|
82
|
+
*
|
|
83
|
+
* ACP defines image blocks as base64 `data` plus a `mimeType`; text blocks are
|
|
84
|
+
* the historical shape and remain the only kind a text-only feed produces.
|
|
85
|
+
*/
|
|
86
|
+
export type MinimaxPromptBlock =
|
|
87
|
+
| { type: 'text', text: string }
|
|
88
|
+
| { type: 'image', data: string, mimeType: string }
|
|
89
|
+
|
|
80
90
|
/** Login progress and state for one region. */
|
|
81
91
|
export interface MinimaxAuthStatus {
|
|
82
92
|
running: boolean
|
|
@@ -557,6 +567,18 @@ async function minimaxAccountCall(
|
|
|
557
567
|
return payload
|
|
558
568
|
}
|
|
559
569
|
|
|
570
|
+
/**
|
|
571
|
+
* Literal characters in a prompt payload; images contribute pixels, not text.
|
|
572
|
+
*
|
|
573
|
+
* The per-turn input estimate must charge text only, exactly as the Qoder path
|
|
574
|
+
* counts a feed, so sending a picture cannot inflate the reported prompt size.
|
|
575
|
+
*/
|
|
576
|
+
function feedTextLengthOf(prompt: string | readonly MinimaxPromptBlock[] | null): number {
|
|
577
|
+
if (prompt === null) return 0
|
|
578
|
+
if (typeof prompt === 'string') return prompt.length
|
|
579
|
+
return prompt.reduce((total, block) => total + (block.type === 'text' ? block.text.length : 0), 0)
|
|
580
|
+
}
|
|
581
|
+
|
|
560
582
|
/** One window's numbers, named the way the coding-plan endpoint reports them. */
|
|
561
583
|
function quotaWindow(
|
|
562
584
|
bucket: Record<string, unknown>,
|
|
@@ -1307,12 +1329,21 @@ export class MinimaxAcp {
|
|
|
1307
1329
|
this.configOptions = Array.isArray(session.configOptions) ? [...session.configOptions] : []
|
|
1308
1330
|
}
|
|
1309
1331
|
|
|
1310
|
-
/**
|
|
1311
|
-
|
|
1332
|
+
/**
|
|
1333
|
+
* Run one prompt; resolves when the agent's turn ends.
|
|
1334
|
+
*
|
|
1335
|
+
* ACP carries images as `{ type: 'image', data, mimeType }` blocks next to
|
|
1336
|
+
* text, which is what makes a pasted picture reach the model at all; a plain
|
|
1337
|
+
* string keeps the historical text-only shape.
|
|
1338
|
+
*/
|
|
1339
|
+
async prompt(prompt: string | readonly MinimaxPromptBlock[]): Promise<{ stopReason: string }> {
|
|
1312
1340
|
if (this.conn === null || this.sessionId === undefined) throw new Error('ACP 会话尚未启动。')
|
|
1341
|
+
const blocks: MinimaxPromptBlock[] = typeof prompt === 'string'
|
|
1342
|
+
? [{ type: 'text', text: prompt }]
|
|
1343
|
+
: [...prompt]
|
|
1313
1344
|
const response = await this.conn.prompt({
|
|
1314
1345
|
sessionId: this.sessionId,
|
|
1315
|
-
prompt:
|
|
1346
|
+
prompt: blocks,
|
|
1316
1347
|
} as any)
|
|
1317
1348
|
return { stopReason: String(response.stopReason) }
|
|
1318
1349
|
}
|
|
@@ -1915,15 +1946,15 @@ export class MinimaxSession {
|
|
|
1915
1946
|
}
|
|
1916
1947
|
}
|
|
1917
1948
|
|
|
1918
|
-
/** Run one harness turn. `feed` is new user
|
|
1919
|
-
async *stream(options: GenerateOptions, feed: string | null): AsyncGenerator<StreamChunk> {
|
|
1949
|
+
/** Run one harness turn. `feed` is new user content, or null for a continuation. */
|
|
1950
|
+
async *stream(options: GenerateOptions, feed: string | readonly MinimaxPromptBlock[] | null): AsyncGenerator<StreamChunk> {
|
|
1920
1951
|
if (this.disposed) throw new Error(`MiniMax session ${this.sessionId} was disposed`)
|
|
1921
1952
|
if (this.activeTurn) throw new Error(`MiniMax session ${this.sessionId} already has a turn in flight`)
|
|
1922
1953
|
this.activeTurn = true
|
|
1923
1954
|
const signal = options.signal
|
|
1924
1955
|
const turn: TurnState = {
|
|
1925
1956
|
blockIndex: 0,
|
|
1926
|
-
inputChars: feed
|
|
1957
|
+
inputChars: feedTextLengthOf(feed),
|
|
1927
1958
|
outputChars: 0,
|
|
1928
1959
|
reasoningChars: 0,
|
|
1929
1960
|
}
|
|
@@ -1936,7 +1967,7 @@ export class MinimaxSession {
|
|
|
1936
1967
|
if (!this.promptInFlight) {
|
|
1937
1968
|
await this.startPrompt(feed ?? '')
|
|
1938
1969
|
this.awaitingToolResults = false
|
|
1939
|
-
} else if (feed !== null && feed
|
|
1970
|
+
} else if (feed !== null && feedTextLengthOf(feed) > 0 && !this.awaitingToolResults) {
|
|
1940
1971
|
throw new Error('MiniMax 会话正忙:上一轮尚未结束。')
|
|
1941
1972
|
}
|
|
1942
1973
|
|
|
@@ -2160,12 +2191,12 @@ export class MinimaxSession {
|
|
|
2160
2191
|
}
|
|
2161
2192
|
}
|
|
2162
2193
|
|
|
2163
|
-
private async startPrompt(
|
|
2194
|
+
private async startPrompt(prompt: string | readonly MinimaxPromptBlock[]): Promise<void> {
|
|
2164
2195
|
const acp = this.acp
|
|
2165
2196
|
if (acp === null) throw new Error('MiniMax ACP 尚未启动。')
|
|
2166
2197
|
this.promptInFlight = true
|
|
2167
2198
|
this.cancelSent = false
|
|
2168
|
-
void this.launchPrompt(acp,
|
|
2199
|
+
void this.launchPrompt(acp, prompt, 0)
|
|
2169
2200
|
}
|
|
2170
2201
|
|
|
2171
2202
|
/**
|
|
@@ -2177,9 +2208,13 @@ export class MinimaxSession {
|
|
|
2177
2208
|
* correction is sent immediately after such a cancel, so it is retried with
|
|
2178
2209
|
* a short backoff instead of being reported as a failed turn.
|
|
2179
2210
|
*/
|
|
2180
|
-
private async launchPrompt(
|
|
2211
|
+
private async launchPrompt(
|
|
2212
|
+
acp: MinimaxAcp,
|
|
2213
|
+
prompt: string | readonly MinimaxPromptBlock[],
|
|
2214
|
+
attempt: number,
|
|
2215
|
+
): Promise<void> {
|
|
2181
2216
|
try {
|
|
2182
|
-
const response = await acp.prompt(
|
|
2217
|
+
const response = await acp.prompt(prompt)
|
|
2183
2218
|
this.events.push({ kind: 'prompt-done', stopReason: response.stopReason })
|
|
2184
2219
|
} catch (error) {
|
|
2185
2220
|
const message = error instanceof Error ? error.message : String(error)
|
|
@@ -2201,7 +2236,7 @@ export class MinimaxSession {
|
|
|
2201
2236
|
if (retryable && attempt < 4 && !this.disposed && this.acp === acp) {
|
|
2202
2237
|
await new Promise(resolve => setTimeout(resolve, 200 * 2 ** attempt))
|
|
2203
2238
|
if (!this.disposed && this.acp === acp) {
|
|
2204
|
-
await this.launchPrompt(acp,
|
|
2239
|
+
await this.launchPrompt(acp, prompt, attempt + 1)
|
|
2205
2240
|
return
|
|
2206
2241
|
}
|
|
2207
2242
|
}
|
package/src/render.ts
CHANGED
|
@@ -40,8 +40,7 @@ export function imageRefs(blocks: readonly ContentBlock[]): ImageAttachmentRef[]
|
|
|
40
40
|
}
|
|
41
41
|
|
|
42
42
|
/** Render one content-block list as plain text; non-text blocks get handles. */
|
|
43
|
-
export function renderBlocks(blocks: readonly ContentBlock[]): string {
|
|
44
|
-
const parts: string[] = []
|
|
43
|
+
export function renderBlocks(blocks: readonly ContentBlock[]): string { const parts: string[] = []
|
|
45
44
|
for (const block of blocks) {
|
|
46
45
|
switch (block.type) {
|
|
47
46
|
case 'text': parts.push(block.text); break
|
|
@@ -167,6 +166,40 @@ export function feedToText(feed: Feed): string {
|
|
|
167
166
|
.join('\n\n')
|
|
168
167
|
}
|
|
169
168
|
|
|
169
|
+
/** One feed element after image bytes have been resolved (base64). */
|
|
170
|
+
export type ResolvedFeedPart =
|
|
171
|
+
| { type: 'text', text: string }
|
|
172
|
+
| { type: 'image', data: string, mediaType: string }
|
|
173
|
+
| { type: 'image-unreadable', attachment: ImageAttachmentRef }
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* Split a feed into text and resolved images for providers with a vision API.
|
|
177
|
+
*
|
|
178
|
+
* The two non-Qoder adapters (CodeBuddy, MiniMax) speak their own content-block
|
|
179
|
+
* shapes, so this returns a neutral list rather than a provider shape: text
|
|
180
|
+
* parts pass through, an image uses its bytes when the host could read them,
|
|
181
|
+
* and an unreadable image keeps a note naming what was dropped instead of
|
|
182
|
+
* silently disappearing from the prompt.
|
|
183
|
+
*/
|
|
184
|
+
export function resolvedFeedParts(feed: Feed, images: ReadonlyMap<string, { data: string, mediaType: string }>): ResolvedFeedPart[] {
|
|
185
|
+
const parts: ResolvedFeedPart[] = []
|
|
186
|
+
if (typeof feed === 'string') {
|
|
187
|
+
if (feed.length > 0) parts.push({ type: 'text', text: feed })
|
|
188
|
+
return parts
|
|
189
|
+
}
|
|
190
|
+
for (const part of feed) {
|
|
191
|
+
if (part.type === 'text') {
|
|
192
|
+
if (part.text.length > 0) parts.push({ type: 'text', text: part.text })
|
|
193
|
+
continue
|
|
194
|
+
}
|
|
195
|
+
const resolved = images.get(String(part.attachment.attachmentId))
|
|
196
|
+
parts.push(resolved === undefined
|
|
197
|
+
? { type: 'image-unreadable', attachment: part.attachment }
|
|
198
|
+
: { type: 'image', data: resolved.data, mediaType: resolved.mediaType })
|
|
199
|
+
}
|
|
200
|
+
return parts
|
|
201
|
+
}
|
|
202
|
+
|
|
170
203
|
/** Literal characters in a feed; images contribute their pixels, not text. */
|
|
171
204
|
export function feedTextLength(feed: Feed): number {
|
|
172
205
|
if (typeof feed === 'string') return feed.length
|