@dan-ai-studio/dshopencodego 0.1.63 → 0.1.64
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/README.en.md +10 -2
- package/README.md +10 -2
- package/lib/client.js +901 -199
- package/lib/index.js +465 -124
- package/lib/types/adapter.d.ts +7 -0
- package/lib/types/config.d.ts +19 -0
- package/lib/types/network/pinning.d.ts +15 -0
- package/lib/types/request-log/contract.d.ts +23 -0
- package/lib/types/request-log/index.d.ts +10 -0
- package/lib/types/request-log/limits.d.ts +15 -0
- package/lib/types/request-log/service.d.ts +26 -0
- package/lib/types/request-log/store.d.ts +194 -0
- package/package.json +1 -1
package/lib/index.js
CHANGED
|
@@ -365,17 +365,17 @@ function contentText(content, separator = "\n") {
|
|
|
365
365
|
}
|
|
366
366
|
function getSystemMessageText(message) {
|
|
367
367
|
const parts = [contentText(message.content)];
|
|
368
|
-
for (const
|
|
369
|
-
if (
|
|
370
|
-
parts.push(
|
|
368
|
+
for (const text2 of Object.values(message.sections ?? {})) {
|
|
369
|
+
if (text2 !== null)
|
|
370
|
+
parts.push(text2);
|
|
371
371
|
}
|
|
372
372
|
return parts.filter((part) => part.length > 0).join("\n\n");
|
|
373
373
|
}
|
|
374
374
|
function renderSystemMessageUpdate(message) {
|
|
375
375
|
const parts = [];
|
|
376
|
-
const
|
|
377
|
-
if (
|
|
378
|
-
parts.push(
|
|
376
|
+
const text2 = contentText(message.content);
|
|
377
|
+
if (text2.length > 0)
|
|
378
|
+
parts.push(text2);
|
|
379
379
|
for (const [name2, value] of Object.entries(message.sections ?? {})) {
|
|
380
380
|
parts.push(value === null ? `Removed system prompt section "${name2}".` : `Updated system prompt section "${name2}":
|
|
381
381
|
|
|
@@ -433,9 +433,9 @@ function getCurrentSystemMessage(messages) {
|
|
|
433
433
|
if (!isSystemMessage(message))
|
|
434
434
|
continue;
|
|
435
435
|
timestamp ??= message.timestamp;
|
|
436
|
-
const
|
|
437
|
-
if (
|
|
438
|
-
content.push(
|
|
436
|
+
const text2 = contentText(message.content);
|
|
437
|
+
if (text2.length > 0)
|
|
438
|
+
content.push(text2);
|
|
439
439
|
for (const [name2, value] of Object.entries(message.sections ?? {})) {
|
|
440
440
|
if (value === null)
|
|
441
441
|
sections.delete(name2);
|
|
@@ -884,9 +884,9 @@ var init_values = __esm({
|
|
|
884
884
|
}
|
|
885
885
|
return n;
|
|
886
886
|
};
|
|
887
|
-
safeJSON = (
|
|
887
|
+
safeJSON = (text2) => {
|
|
888
888
|
try {
|
|
889
|
-
return JSON.parse(
|
|
889
|
+
return JSON.parse(text2);
|
|
890
890
|
} catch (err) {
|
|
891
891
|
return void 0;
|
|
892
892
|
}
|
|
@@ -1616,12 +1616,12 @@ function requireSecureTokenEndpoint(baseURL) {
|
|
|
1616
1616
|
throw new WorkloadIdentityError(`Refusing to send credential over non-https token endpoint "${baseURL}"`);
|
|
1617
1617
|
}
|
|
1618
1618
|
async function parseTokenResponse(resp, requestId) {
|
|
1619
|
-
const
|
|
1619
|
+
const text2 = await readLimitedText(resp);
|
|
1620
1620
|
let data;
|
|
1621
1621
|
try {
|
|
1622
|
-
data = JSON.parse(
|
|
1622
|
+
data = JSON.parse(text2);
|
|
1623
1623
|
} catch {
|
|
1624
|
-
throw new WorkloadIdentityError(`Token endpoint returned non-JSON response (status ${resp.status})`, resp.status, redactSensitive(
|
|
1624
|
+
throw new WorkloadIdentityError(`Token endpoint returned non-JSON response (status ${resp.status})`, resp.status, redactSensitive(text2), requestId);
|
|
1625
1625
|
}
|
|
1626
1626
|
if (!data.access_token) {
|
|
1627
1627
|
throw new WorkloadIdentityError(`Token endpoint response missing access_token: ${JSON.stringify(redactSensitive(data))}`, resp.status, redactSensitive(data), requestId);
|
|
@@ -2315,8 +2315,8 @@ function oidcFederationProvider(config) {
|
|
|
2315
2315
|
}
|
|
2316
2316
|
const requestId = resp.headers.get("Request-Id");
|
|
2317
2317
|
if (!resp.ok) {
|
|
2318
|
-
const
|
|
2319
|
-
const redacted = redactSensitive(
|
|
2318
|
+
const text2 = await resp.text().catch(() => "");
|
|
2319
|
+
const redacted = redactSensitive(text2);
|
|
2320
2320
|
let hint = "";
|
|
2321
2321
|
if (resp.status === 401) {
|
|
2322
2322
|
const hintMiddle = config.workspaceId ? "" : "If your federation rule is scoped to multiple workspaces, set the ANTHROPIC_WORKSPACE_ID environment variable, the 'workspace_id' config key, or the `workspaceId` option. ";
|
|
@@ -2395,8 +2395,8 @@ function userOAuthProvider(config) {
|
|
|
2395
2395
|
}
|
|
2396
2396
|
const requestId = resp.headers.get("Request-Id");
|
|
2397
2397
|
if (!resp.ok) {
|
|
2398
|
-
const
|
|
2399
|
-
throw new WorkloadIdentityError(`User OAuth refresh failed (HTTP ${resp.status}): ${redactSensitive(
|
|
2398
|
+
const text2 = await resp.text().catch(() => "");
|
|
2399
|
+
throw new WorkloadIdentityError(`User OAuth refresh failed (HTTP ${resp.status}): ${redactSensitive(text2)}`, resp.status, redactSensitive(text2), requestId);
|
|
2400
2400
|
}
|
|
2401
2401
|
const data = await parseTokenResponse(resp, requestId);
|
|
2402
2402
|
const expiresIn = Number(data.expires_in);
|
|
@@ -2923,8 +2923,8 @@ async function defaultParseResponse(client, props) {
|
|
|
2923
2923
|
const json = await response.json();
|
|
2924
2924
|
return addResponseIDs(json, response);
|
|
2925
2925
|
}
|
|
2926
|
-
const
|
|
2927
|
-
return
|
|
2926
|
+
const text2 = await response.text();
|
|
2927
|
+
return text2;
|
|
2928
2928
|
})().finally(() => {
|
|
2929
2929
|
if (!props.options.stream && !props.options.__binaryResponse) {
|
|
2930
2930
|
releaseRequestSignal(props.controller);
|
|
@@ -8198,15 +8198,15 @@ function betaEditTool(ctx) {
|
|
|
8198
8198
|
throw e;
|
|
8199
8199
|
throw new ToolError(`edit: ${fsErrorMessage(e, file_path)}`);
|
|
8200
8200
|
}
|
|
8201
|
-
const
|
|
8202
|
-
if (
|
|
8201
|
+
const count2 = data.split(old_string).length - 1;
|
|
8202
|
+
if (count2 === 0)
|
|
8203
8203
|
throw new ToolError(`edit: old_string not found in ${file_path}`);
|
|
8204
8204
|
let updated;
|
|
8205
8205
|
if (replace_all) {
|
|
8206
8206
|
updated = data.split(old_string).join(new_string);
|
|
8207
8207
|
} else {
|
|
8208
|
-
if (
|
|
8209
|
-
throw new ToolError(`edit: old_string appears ${
|
|
8208
|
+
if (count2 > 1)
|
|
8209
|
+
throw new ToolError(`edit: old_string appears ${count2} times in ${file_path} (must be unique)`);
|
|
8210
8210
|
updated = data.replace(old_string, () => new_string);
|
|
8211
8211
|
}
|
|
8212
8212
|
try {
|
|
@@ -8214,7 +8214,7 @@ function betaEditTool(ctx) {
|
|
|
8214
8214
|
} catch (e) {
|
|
8215
8215
|
throw new ToolError(`edit: write: ${fsErrorMessage(e, file_path)}`);
|
|
8216
8216
|
}
|
|
8217
|
-
return `edited ${file_path} (${replace_all ?
|
|
8217
|
+
return `edited ${file_path} (${replace_all ? count2 : 1} replacement(s))`;
|
|
8218
8218
|
}
|
|
8219
8219
|
});
|
|
8220
8220
|
}
|
|
@@ -18327,8 +18327,8 @@ var init_provider_retry = __esm({
|
|
|
18327
18327
|
});
|
|
18328
18328
|
|
|
18329
18329
|
// node_modules/@earendil-works/pi-ai/dist/utils/sanitize-unicode.js
|
|
18330
|
-
function sanitizeSurrogates(
|
|
18331
|
-
return
|
|
18330
|
+
function sanitizeSurrogates(text2) {
|
|
18331
|
+
return text2.replace(/[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(?<![\uD800-\uDBFF])[\uDC00-\uDFFF]/g, "");
|
|
18332
18332
|
}
|
|
18333
18333
|
var init_sanitize_unicode = __esm({
|
|
18334
18334
|
"node_modules/@earendil-works/pi-ai/dist/utils/sanitize-unicode.js"() {
|
|
@@ -18613,8 +18613,8 @@ function estimateTextAndImageContentChars(content) {
|
|
|
18613
18613
|
chars += block.type === "text" ? block.text.length : ESTIMATED_IMAGE_CHARS;
|
|
18614
18614
|
return chars;
|
|
18615
18615
|
}
|
|
18616
|
-
function estimateTextTokens(
|
|
18617
|
-
return Math.ceil(
|
|
18616
|
+
function estimateTextTokens(text2) {
|
|
18617
|
+
return Math.ceil(text2.length / CHARS_PER_TOKEN);
|
|
18618
18618
|
}
|
|
18619
18619
|
function estimateTextAndImageContentTokens(content) {
|
|
18620
18620
|
return Math.ceil(estimateTextAndImageContentChars(content) / CHARS_PER_TOKEN);
|
|
@@ -19094,9 +19094,9 @@ function decodeSseLine(line, state2) {
|
|
|
19094
19094
|
}
|
|
19095
19095
|
return null;
|
|
19096
19096
|
}
|
|
19097
|
-
function nextLineBreakIndex(
|
|
19098
|
-
const carriageReturnIndex =
|
|
19099
|
-
const newlineIndex =
|
|
19097
|
+
function nextLineBreakIndex(text2) {
|
|
19098
|
+
const carriageReturnIndex = text2.indexOf("\r");
|
|
19099
|
+
const newlineIndex = text2.indexOf("\n");
|
|
19100
19100
|
if (carriageReturnIndex === -1) {
|
|
19101
19101
|
return newlineIndex;
|
|
19102
19102
|
}
|
|
@@ -19105,18 +19105,18 @@ function nextLineBreakIndex(text) {
|
|
|
19105
19105
|
}
|
|
19106
19106
|
return Math.min(carriageReturnIndex, newlineIndex);
|
|
19107
19107
|
}
|
|
19108
|
-
function consumeLine(
|
|
19109
|
-
const lineBreakIndex = nextLineBreakIndex(
|
|
19108
|
+
function consumeLine(text2) {
|
|
19109
|
+
const lineBreakIndex = nextLineBreakIndex(text2);
|
|
19110
19110
|
if (lineBreakIndex === -1) {
|
|
19111
19111
|
return null;
|
|
19112
19112
|
}
|
|
19113
19113
|
let nextIndex = lineBreakIndex + 1;
|
|
19114
|
-
if (
|
|
19114
|
+
if (text2[lineBreakIndex] === "\r" && text2[nextIndex] === "\n") {
|
|
19115
19115
|
nextIndex += 1;
|
|
19116
19116
|
}
|
|
19117
19117
|
return {
|
|
19118
|
-
line:
|
|
19119
|
-
rest:
|
|
19118
|
+
line: text2.slice(0, lineBreakIndex),
|
|
19119
|
+
rest: text2.slice(nextIndex)
|
|
19120
19120
|
};
|
|
19121
19121
|
}
|
|
19122
19122
|
async function* iterateSseMessages(body, signal) {
|
|
@@ -19448,10 +19448,10 @@ function convertMessages(transformedMessages, isOAuthToken2, cacheControl, allow
|
|
|
19448
19448
|
for (let i = 0; i < transformedMessages.length; i++) {
|
|
19449
19449
|
const msg = transformedMessages[i];
|
|
19450
19450
|
if (msg.role === "system") {
|
|
19451
|
-
const
|
|
19451
|
+
const text2 = renderSystemMessageUpdate(msg);
|
|
19452
19452
|
const blocks = [];
|
|
19453
|
-
if (
|
|
19454
|
-
blocks.push({ type: "text", text: sanitizeSurrogates(
|
|
19453
|
+
if (text2.length > 0)
|
|
19454
|
+
blocks.push({ type: "text", text: sanitizeSurrogates(text2) });
|
|
19455
19455
|
if (convertToolDefinitions) {
|
|
19456
19456
|
const added = msg.toolsAdded ?? [];
|
|
19457
19457
|
const redefined = new Set(added.map((tool) => tool.name));
|
|
@@ -20378,9 +20378,9 @@ var init_values2 = __esm({
|
|
|
20378
20378
|
}
|
|
20379
20379
|
return n;
|
|
20380
20380
|
};
|
|
20381
|
-
safeJSON2 = (
|
|
20381
|
+
safeJSON2 = (text2) => {
|
|
20382
20382
|
try {
|
|
20383
|
-
return JSON.parse(
|
|
20383
|
+
return JSON.parse(text2);
|
|
20384
20384
|
} catch (err) {
|
|
20385
20385
|
return void 0;
|
|
20386
20386
|
}
|
|
@@ -21539,8 +21539,8 @@ async function defaultParseResponse2(client, props) {
|
|
|
21539
21539
|
jsonBodyLength = bodyText.length;
|
|
21540
21540
|
return addRequestID(json, response);
|
|
21541
21541
|
}
|
|
21542
|
-
const
|
|
21543
|
-
return
|
|
21542
|
+
const text2 = await response.text();
|
|
21543
|
+
return text2;
|
|
21544
21544
|
})().catch((error) => {
|
|
21545
21545
|
throw asAbortError(error, props.controller.signal);
|
|
21546
21546
|
});
|
|
@@ -21728,19 +21728,19 @@ var init_detect_platform2 = __esm({
|
|
|
21728
21728
|
});
|
|
21729
21729
|
|
|
21730
21730
|
// node_modules/openai/internal/request-options.mjs
|
|
21731
|
-
function observeJSONRequestBody(body,
|
|
21731
|
+
function observeJSONRequestBody(body, observer2) {
|
|
21732
21732
|
let observers = jsonRequestBodyObservers.get(body);
|
|
21733
21733
|
if (!observers) {
|
|
21734
21734
|
observers = /* @__PURE__ */ new Set();
|
|
21735
21735
|
jsonRequestBodyObservers.set(body, observers);
|
|
21736
21736
|
}
|
|
21737
|
-
observers.add(
|
|
21737
|
+
observers.add(observer2);
|
|
21738
21738
|
return () => {
|
|
21739
21739
|
const active = jsonRequestBodyObservers.get(body);
|
|
21740
21740
|
if (!active) {
|
|
21741
21741
|
return;
|
|
21742
21742
|
}
|
|
21743
|
-
active.delete(
|
|
21743
|
+
active.delete(observer2);
|
|
21744
21744
|
if (active.size === 0) {
|
|
21745
21745
|
jsonRequestBodyObservers.delete(body);
|
|
21746
21746
|
}
|
|
@@ -21759,16 +21759,16 @@ var init_request_options2 = __esm({
|
|
|
21759
21759
|
const active = [...observers];
|
|
21760
21760
|
encoded = JSON.stringify(body, function(key, value) {
|
|
21761
21761
|
let observed = value;
|
|
21762
|
-
for (const
|
|
21763
|
-
const replacement =
|
|
21762
|
+
for (const observer2 of active) {
|
|
21763
|
+
const replacement = observer2.value(this, key, observed);
|
|
21764
21764
|
if (replacement !== void 0) {
|
|
21765
21765
|
observed = replacement;
|
|
21766
21766
|
}
|
|
21767
21767
|
}
|
|
21768
21768
|
return observed;
|
|
21769
21769
|
});
|
|
21770
|
-
for (const
|
|
21771
|
-
|
|
21770
|
+
for (const observer2 of active) {
|
|
21771
|
+
observer2.complete();
|
|
21772
21772
|
}
|
|
21773
21773
|
}
|
|
21774
21774
|
return {
|
|
@@ -33412,14 +33412,14 @@ function isPrimitiveAssistantStreamArrayDelta(accumulator, delta) {
|
|
|
33412
33412
|
return delta.every(isPrimitiveAssistantStreamValue) && accumulator.every(isPrimitiveAssistantStreamValue);
|
|
33413
33413
|
}
|
|
33414
33414
|
function countOwnAssistantStreamArrayEntries(accumulator) {
|
|
33415
|
-
let
|
|
33415
|
+
let count2 = 0;
|
|
33416
33416
|
for (const key of Object.keys(accumulator)) {
|
|
33417
33417
|
const index = Number(key);
|
|
33418
33418
|
if (Number.isSafeInteger(index) && index >= 0 && index < accumulator.length && String(index) === key) {
|
|
33419
|
-
|
|
33419
|
+
count2 += 1;
|
|
33420
33420
|
}
|
|
33421
33421
|
}
|
|
33422
|
-
return
|
|
33422
|
+
return count2;
|
|
33423
33423
|
}
|
|
33424
33424
|
function getAssistantStreamArrayOwnEntryCount(accumulator, enforceSparseHoleBudget, cachedState) {
|
|
33425
33425
|
if (!enforceSparseHoleBudget) {
|
|
@@ -36481,27 +36481,27 @@ function getOutputText(context, output) {
|
|
|
36481
36481
|
if (output.type !== "message") {
|
|
36482
36482
|
return "";
|
|
36483
36483
|
}
|
|
36484
|
-
let
|
|
36484
|
+
let text2 = "";
|
|
36485
36485
|
for (const content of output.content) {
|
|
36486
36486
|
if (content.type === "output_text") {
|
|
36487
|
-
|
|
36487
|
+
text2 += content.text;
|
|
36488
36488
|
}
|
|
36489
36489
|
}
|
|
36490
|
-
context.outputTextLengths.set(output,
|
|
36491
|
-
return
|
|
36490
|
+
context.outputTextLengths.set(output, text2.length);
|
|
36491
|
+
return text2;
|
|
36492
36492
|
}
|
|
36493
36493
|
function ensureCanonicalOutputText(context, snapshot2) {
|
|
36494
36494
|
if (context.canonicalSnapshot === snapshot2) {
|
|
36495
36495
|
return;
|
|
36496
36496
|
}
|
|
36497
36497
|
const outputTextIndex = new OutputTextIndex();
|
|
36498
|
-
let
|
|
36498
|
+
let text2 = "";
|
|
36499
36499
|
for (const output of snapshot2.output) {
|
|
36500
36500
|
const outputText = getOutputText(context, output);
|
|
36501
|
-
|
|
36501
|
+
text2 += outputText;
|
|
36502
36502
|
outputTextIndex.append(outputText.length);
|
|
36503
36503
|
}
|
|
36504
|
-
snapshot2.output_text =
|
|
36504
|
+
snapshot2.output_text = text2;
|
|
36505
36505
|
context.outputTextIndex = outputTextIndex;
|
|
36506
36506
|
context.canonicalSnapshot = snapshot2;
|
|
36507
36507
|
}
|
|
@@ -36802,12 +36802,12 @@ function accumulateOutputItemEvent(event, snapshot2, context) {
|
|
|
36802
36802
|
snapshot2.output.push(output);
|
|
36803
36803
|
addOutputItemIdentities(identityIndex.identities, identities);
|
|
36804
36804
|
identityIndex.length = snapshot2.output.length;
|
|
36805
|
-
const
|
|
36805
|
+
const text2 = getOutputText(context, output);
|
|
36806
36806
|
if (context.canonicalSnapshot === snapshot2) {
|
|
36807
|
-
context.outputTextIndex.append(
|
|
36807
|
+
context.outputTextIndex.append(text2.length);
|
|
36808
36808
|
}
|
|
36809
|
-
if (
|
|
36810
|
-
snapshot2.output_text +=
|
|
36809
|
+
if (text2) {
|
|
36810
|
+
snapshot2.output_text += text2;
|
|
36811
36811
|
}
|
|
36812
36812
|
return true;
|
|
36813
36813
|
}
|
|
@@ -40289,10 +40289,10 @@ function formatProviderError(norm, prefix) {
|
|
|
40289
40289
|
}
|
|
40290
40290
|
return prefix !== void 0 ? `${prefix} (${norm.status}): ${norm.body}` : `${norm.status}: ${norm.body}`;
|
|
40291
40291
|
}
|
|
40292
|
-
function truncateErrorText(
|
|
40293
|
-
if (
|
|
40294
|
-
return
|
|
40295
|
-
return `${
|
|
40292
|
+
function truncateErrorText(text2, maxChars) {
|
|
40293
|
+
if (text2.length <= maxChars)
|
|
40294
|
+
return text2;
|
|
40295
|
+
return `${text2.slice(0, maxChars)}... [truncated ${text2.length - maxChars} chars]`;
|
|
40296
40296
|
}
|
|
40297
40297
|
function safeJsonStringify2(value) {
|
|
40298
40298
|
try {
|
|
@@ -40810,9 +40810,9 @@ function convertMessages2(model, context, compat, options) {
|
|
|
40810
40810
|
};
|
|
40811
40811
|
params.push(kimiToolMessage);
|
|
40812
40812
|
}
|
|
40813
|
-
const
|
|
40814
|
-
if (
|
|
40815
|
-
params.push({ role: instructionRole, content: sanitizeSurrogates(
|
|
40813
|
+
const text2 = i === 0 ? getSystemMessageText(msg) : renderSystemMessageUpdate(msg);
|
|
40814
|
+
if (text2.length > 0) {
|
|
40815
|
+
params.push({ role: instructionRole, content: sanitizeSurrogates(text2) });
|
|
40816
40816
|
}
|
|
40817
40817
|
} else if (msg.role === "user") {
|
|
40818
40818
|
if (typeof msg.content === "string") {
|
|
@@ -41623,9 +41623,9 @@ function convertResponsesMessages(model, context, allowedToolCallProviders, opti
|
|
|
41623
41623
|
if (!isLeadingSystemMessage)
|
|
41624
41624
|
appendSystemToolAdditions(msg, `system:${msgIndex}`);
|
|
41625
41625
|
if (!isLeadingSystemMessage || includeInitialSystemMessage) {
|
|
41626
|
-
const
|
|
41627
|
-
if (
|
|
41628
|
-
messages.push({ role: instructionRole, content: sanitizeSurrogates(
|
|
41626
|
+
const text2 = isLeadingSystemMessage ? getSystemMessageText(msg) : renderSystemMessageUpdate(msg);
|
|
41627
|
+
if (text2.length > 0) {
|
|
41628
|
+
messages.push({ role: instructionRole, content: sanitizeSurrogates(text2) });
|
|
41629
41629
|
}
|
|
41630
41630
|
}
|
|
41631
41631
|
} else if (msg.role === "user") {
|
|
@@ -63484,12 +63484,12 @@ var require_cache_handler = __commonJS({
|
|
|
63484
63484
|
return void 0;
|
|
63485
63485
|
}
|
|
63486
63486
|
let originUrl;
|
|
63487
|
-
let
|
|
63487
|
+
let requestUrl2;
|
|
63488
63488
|
let locationUrl;
|
|
63489
63489
|
try {
|
|
63490
63490
|
originUrl = new URL(cacheKey.origin);
|
|
63491
|
-
|
|
63492
|
-
locationUrl = new URL(location,
|
|
63491
|
+
requestUrl2 = new URL(cacheKey.path, originUrl);
|
|
63492
|
+
locationUrl = new URL(location, requestUrl2);
|
|
63493
63493
|
} catch {
|
|
63494
63494
|
return void 0;
|
|
63495
63495
|
}
|
|
@@ -72962,15 +72962,15 @@ ${value}`;
|
|
|
72962
72962
|
pos = 0;
|
|
72963
72963
|
}
|
|
72964
72964
|
}
|
|
72965
|
-
discardLeadingBytes(
|
|
72966
|
-
while (
|
|
72965
|
+
discardLeadingBytes(count2) {
|
|
72966
|
+
while (count2 > 0 && this.lineChunkIndex < this.chunks.length) {
|
|
72967
72967
|
const chunk = this.chunks[this.lineChunkIndex];
|
|
72968
72968
|
const remaining = chunk.length - this.linePos;
|
|
72969
|
-
if (
|
|
72970
|
-
this.linePos +=
|
|
72971
|
-
|
|
72969
|
+
if (count2 < remaining) {
|
|
72970
|
+
this.linePos += count2;
|
|
72971
|
+
count2 = 0;
|
|
72972
72972
|
} else {
|
|
72973
|
-
|
|
72973
|
+
count2 -= remaining;
|
|
72974
72974
|
this.lineChunkIndex++;
|
|
72975
72975
|
this.linePos = 0;
|
|
72976
72976
|
}
|
|
@@ -73544,6 +73544,7 @@ ${captureLines}` : capture.stack;
|
|
|
73544
73544
|
});
|
|
73545
73545
|
|
|
73546
73546
|
// src/index.ts
|
|
73547
|
+
import dc from "node:diagnostics_channel";
|
|
73547
73548
|
import { credentialRef } from "@deepseek-ai/dsh-credentials";
|
|
73548
73549
|
import { launchEnvironmentOf } from "@deepseek-ai/dsh-launch-environment";
|
|
73549
73550
|
import * as llm2 from "@deepseek-ai/dsh-llm";
|
|
@@ -73555,6 +73556,7 @@ init_transcript();
|
|
|
73555
73556
|
import {
|
|
73556
73557
|
attributionHeaders as attributionHeaders5,
|
|
73557
73558
|
contentHasImage as contentHasImage2,
|
|
73559
|
+
isTokenDelta,
|
|
73558
73560
|
LlmAdapter,
|
|
73559
73561
|
LlmError as LlmError8,
|
|
73560
73562
|
ReasoningEffortId,
|
|
@@ -73629,16 +73631,16 @@ async function readBoundedJson(response, url, maxBytes) {
|
|
|
73629
73631
|
});
|
|
73630
73632
|
throw new LlmError(`${url} declares ${declared} bytes, over the ${maxBytes}-byte cap`, "DISCOVERY_FAILED");
|
|
73631
73633
|
}
|
|
73632
|
-
let
|
|
73634
|
+
let text2;
|
|
73633
73635
|
try {
|
|
73634
|
-
|
|
73636
|
+
text2 = await readBoundedText(response, maxBytes);
|
|
73635
73637
|
} catch (error) {
|
|
73636
73638
|
if (error instanceof LlmError) throw error;
|
|
73637
73639
|
const detail = error instanceof Error ? error.message : String(error);
|
|
73638
73640
|
throw new LlmError(`could not read ${url}: ${detail}`, "DISCOVERY_FAILED", { cause: error });
|
|
73639
73641
|
}
|
|
73640
73642
|
try {
|
|
73641
|
-
return JSON.parse(
|
|
73643
|
+
return JSON.parse(text2);
|
|
73642
73644
|
} catch (error) {
|
|
73643
73645
|
throw new LlmError(`${url} did not answer with JSON`, "DISCOVERY_FAILED", { cause: error });
|
|
73644
73646
|
}
|
|
@@ -75722,6 +75724,11 @@ var MAX_KEEP_ALIVE_TIMEOUT_SECONDS = 1800;
|
|
|
75722
75724
|
var DEFAULT_KEEP_ALIVE_TIMEOUT_MS = DEFAULT_KEEP_ALIVE_TIMEOUT_SECONDS * 1e3;
|
|
75723
75725
|
var MAX_KEEP_ALIVE_TIMEOUT_MS = MAX_KEEP_ALIVE_TIMEOUT_SECONDS * 1e3;
|
|
75724
75726
|
|
|
75727
|
+
// src/request-log/limits.ts
|
|
75728
|
+
var DEFAULT_REQUEST_LOG_MAX_ENTRIES = 200;
|
|
75729
|
+
var MIN_REQUEST_LOG_MAX_ENTRIES = 50;
|
|
75730
|
+
var MAX_REQUEST_LOG_MAX_ENTRIES = 500;
|
|
75731
|
+
|
|
75725
75732
|
// src/config.ts
|
|
75726
75733
|
var DEFAULT_API_KEY_ENV = "OPENCODE_GO_API_KEY";
|
|
75727
75734
|
var DEFAULT_REFRESH_MINUTES = 60;
|
|
@@ -75750,6 +75757,11 @@ var fields = {
|
|
|
75750
75757
|
maxTokens: Schema.union([Schema.const(null), Schema.number().step(1).min(1).max(Number.MAX_SAFE_INTEGER)])
|
|
75751
75758
|
})])).default({}),
|
|
75752
75759
|
modelProtocols: Schema.dict(Schema.string()).default({}),
|
|
75760
|
+
// Off by default: the request path must read exactly as it does without
|
|
75761
|
+
// the log, so the recorder is constructed only when this is true.
|
|
75762
|
+
requestLogEnabled: Schema.boolean().default(false),
|
|
75763
|
+
requestLogDebug: Schema.boolean().default(false),
|
|
75764
|
+
requestLogMaxEntries: Schema.number().step(1).min(MIN_REQUEST_LOG_MAX_ENTRIES).max(MAX_REQUEST_LOG_MAX_ENTRIES).default(DEFAULT_REQUEST_LOG_MAX_ENTRIES),
|
|
75753
75765
|
pinnedAddresses: Schema.dict(Schema.string()).default({}),
|
|
75754
75766
|
retryPolicy: RetryPolicySchema
|
|
75755
75767
|
};
|
|
@@ -76022,8 +76034,8 @@ function splitSystemPrompt(options) {
|
|
|
76022
76034
|
if (options.system !== void 0) return { systemPrompt: options.system, messages: options.messages };
|
|
76023
76035
|
const [first, ...rest] = options.messages;
|
|
76024
76036
|
if (first?.role !== "system") return { systemPrompt: void 0, messages: options.messages };
|
|
76025
|
-
const
|
|
76026
|
-
return { systemPrompt:
|
|
76037
|
+
const text2 = flattenText(first);
|
|
76038
|
+
return { systemPrompt: text2.length > 0 ? text2 : void 0, messages: rest };
|
|
76027
76039
|
}
|
|
76028
76040
|
function collectImageRefs(blocks, refs) {
|
|
76029
76041
|
for (const block of blocks) {
|
|
@@ -76301,8 +76313,8 @@ function mapStopReason4(message, contextWindow) {
|
|
|
76301
76313
|
failure: { message: message.errorMessage ?? "pi-ai stream aborted", code: "ABORTED" }
|
|
76302
76314
|
};
|
|
76303
76315
|
case "error": {
|
|
76304
|
-
const
|
|
76305
|
-
return { kind: "error", failure: { message:
|
|
76316
|
+
const text2 = message.errorMessage ?? "pi-ai stream error";
|
|
76317
|
+
return { kind: "error", failure: { message: text2, code: classifyPiAiError(text2) } };
|
|
76306
76318
|
}
|
|
76307
76319
|
}
|
|
76308
76320
|
}
|
|
@@ -76581,6 +76593,11 @@ function stopSequencesFor(model, stop) {
|
|
|
76581
76593
|
"UNSUPPORTED_OPTION"
|
|
76582
76594
|
);
|
|
76583
76595
|
}
|
|
76596
|
+
function requestUrl(input) {
|
|
76597
|
+
if (typeof input === "string") return input;
|
|
76598
|
+
if (input instanceof URL) return input.toString();
|
|
76599
|
+
return input.url;
|
|
76600
|
+
}
|
|
76584
76601
|
async function* observingTerminalUsage(events, observe) {
|
|
76585
76602
|
for await (const event of events) {
|
|
76586
76603
|
if (event.type === "done") observe(event.message.usage);
|
|
@@ -76749,8 +76766,32 @@ var OpencodeGoAdapter = class extends LlmAdapter {
|
|
|
76749
76766
|
const upstream = options.signal === void 0 ? consumer.signal : AbortSignal.any([options.signal, consumer.signal]);
|
|
76750
76767
|
const watchdog = __using(_stack, idleWatchdog(upstream, config.streamIdleTimeoutMs, "LLM_STREAM_IDLE_TIMEOUT"));
|
|
76751
76768
|
let timedOut = false;
|
|
76769
|
+
const containsImage = options.messages.some((message) => contentHasImage2(message.content));
|
|
76770
|
+
const headers = providerHeaders(options.sessionId === void 0 ? void 0 : String(options.sessionId));
|
|
76771
|
+
const span = this.options.requestLog?.start({
|
|
76772
|
+
model: options.model,
|
|
76773
|
+
sessionId: options.sessionId === void 0 ? void 0 : String(options.sessionId),
|
|
76774
|
+
method: "POST",
|
|
76775
|
+
url: config.baseURL,
|
|
76776
|
+
messageCount: options.messages.length,
|
|
76777
|
+
hasImage: containsImage,
|
|
76778
|
+
toolCount: options.tools?.length ?? 0,
|
|
76779
|
+
headers: {
|
|
76780
|
+
...options.sessionId === void 0 ? {} : { session: String(options.sessionId) },
|
|
76781
|
+
...typeof headers["user-agent"] === "string" ? { userAgent: headers["user-agent"] } : {}
|
|
76782
|
+
}
|
|
76783
|
+
});
|
|
76784
|
+
const fetchFactory = this.options.fetch;
|
|
76785
|
+
const requestFetch = span === void 0 || fetchFactory === void 0 ? fetchFactory : () => {
|
|
76786
|
+
const base = fetchFactory();
|
|
76787
|
+
return (async (input, init) => {
|
|
76788
|
+
const response = await base(input, init);
|
|
76789
|
+
span.request(typeof init?.method === "string" ? init.method : "POST", requestUrl(input));
|
|
76790
|
+
span.status(response.status);
|
|
76791
|
+
return response;
|
|
76792
|
+
});
|
|
76793
|
+
};
|
|
76752
76794
|
try {
|
|
76753
|
-
const containsImage = options.messages.some((message) => contentHasImage2(message.content));
|
|
76754
76795
|
if (containsImage && !model.input.includes("image")) {
|
|
76755
76796
|
throw new LlmError8(`opencode-go model "${model.id}" does not support image input`, "UNSUPPORTED_CONTENT");
|
|
76756
76797
|
}
|
|
@@ -76770,6 +76811,7 @@ var OpencodeGoAdapter = class extends LlmAdapter {
|
|
|
76770
76811
|
}
|
|
76771
76812
|
const context = imageRequest === void 0 ? await toPiContext(options, void 0, this.options.onReplayDegrade) : await toPiContext({ ...options, signal: watchdog.signal }, imageRequest, this.options.onReplayDegrade);
|
|
76772
76813
|
let reportedReasoning;
|
|
76814
|
+
let pendingUsage;
|
|
76773
76815
|
const events = observingTerminalUsage(snapshot2.provider.streamSimple(model, normalizeContext(context), {
|
|
76774
76816
|
apiKey,
|
|
76775
76817
|
...reasoning === void 0 || reasoning === "off" ? {} : { reasoning },
|
|
@@ -76782,12 +76824,14 @@ var OpencodeGoAdapter = class extends LlmAdapter {
|
|
|
76782
76824
|
...options.sessionId === void 0 ? {} : { sessionId: String(options.sessionId) },
|
|
76783
76825
|
signal: watchdog.signal,
|
|
76784
76826
|
// The gateway refuses a request without `x-opencode-session`; the
|
|
76785
|
-
// attribution user-agent rides along on the same object.
|
|
76786
|
-
|
|
76827
|
+
// attribution user-agent rides along on the same object. Computed
|
|
76828
|
+
// once above so the request log can reuse the allowlisted values.
|
|
76829
|
+
headers,
|
|
76787
76830
|
// The request goes to the gateway, so it goes through the fetch that
|
|
76788
76831
|
// honours the gateway's pin — and through the system's own resolution
|
|
76789
|
-
// when the pinned address turns out to be gone.
|
|
76790
|
-
|
|
76832
|
+
// when the pinned address turns out to be gone. Invoked once per
|
|
76833
|
+
// request, exactly as the factory was before.
|
|
76834
|
+
...requestFetch === void 0 ? {} : { fetch: requestFetch() },
|
|
76791
76835
|
// The agent recovery layer owns visible attempts; one adapter call is
|
|
76792
76836
|
// one SDK attempt.
|
|
76793
76837
|
maxRetries: 0
|
|
@@ -76806,7 +76850,9 @@ var OpencodeGoAdapter = class extends LlmAdapter {
|
|
|
76806
76850
|
exhausted = true;
|
|
76807
76851
|
return;
|
|
76808
76852
|
}
|
|
76853
|
+
if (span !== void 0 && isTokenDelta(result.value)) span.firstToken();
|
|
76809
76854
|
if (result.value.type === "usage") {
|
|
76855
|
+
pendingUsage = result.value.usage;
|
|
76810
76856
|
this.options.onUsage?.({
|
|
76811
76857
|
model: options.model,
|
|
76812
76858
|
usage: {
|
|
@@ -76817,6 +76863,18 @@ var OpencodeGoAdapter = class extends LlmAdapter {
|
|
|
76817
76863
|
...rates2 === void 0 ? {} : { rates: rates2 }
|
|
76818
76864
|
}
|
|
76819
76865
|
});
|
|
76866
|
+
} else if (result.value.type === "finish") {
|
|
76867
|
+
const reason = result.value.reason;
|
|
76868
|
+
const ok = reason.kind === "stop" || reason.kind === "tool-calls" || reason.kind === "max-tokens";
|
|
76869
|
+
span?.finish(
|
|
76870
|
+
ok,
|
|
76871
|
+
ok ? void 0 : reason.failure?.code,
|
|
76872
|
+
pendingUsage === void 0 ? void 0 : {
|
|
76873
|
+
inputTokens: pendingUsage.inputTokens,
|
|
76874
|
+
outputTokens: pendingUsage.outputTokens,
|
|
76875
|
+
...pendingUsage.cacheReadTokens === void 0 ? {} : { cacheReadTokens: pendingUsage.cacheReadTokens }
|
|
76876
|
+
}
|
|
76877
|
+
);
|
|
76820
76878
|
}
|
|
76821
76879
|
yield result.value;
|
|
76822
76880
|
}
|
|
@@ -76833,11 +76891,14 @@ var OpencodeGoAdapter = class extends LlmAdapter {
|
|
|
76833
76891
|
}
|
|
76834
76892
|
} catch (error) {
|
|
76835
76893
|
if (timedOut || timeoutOf(watchdog.signal, "LLM_STREAM_IDLE_TIMEOUT") !== void 0) {
|
|
76894
|
+
span?.finish(false, "TIMEOUT");
|
|
76836
76895
|
throw new LlmError8("opencode-go stream idle timeout", "TIMEOUT", { cause: error });
|
|
76837
76896
|
}
|
|
76838
76897
|
if (options.signal?.aborted) {
|
|
76898
|
+
span?.finish(false, "ABORTED");
|
|
76839
76899
|
throw new LlmError8("opencode-go request aborted by caller", "ABORTED", { cause: error });
|
|
76840
76900
|
}
|
|
76901
|
+
span?.finish(false, error instanceof LlmError8 ? error.code : void 0);
|
|
76841
76902
|
throw error;
|
|
76842
76903
|
} finally {
|
|
76843
76904
|
consumer.abort("opencode-go stream consumer stopped");
|
|
@@ -76930,7 +76991,7 @@ function parseCatalogReading(value) {
|
|
|
76930
76991
|
throw new Error("invalid catalog reading");
|
|
76931
76992
|
}
|
|
76932
76993
|
const numbers = counts;
|
|
76933
|
-
const
|
|
76994
|
+
const count2 = (key) => {
|
|
76934
76995
|
const entry = numbers[key];
|
|
76935
76996
|
if (typeof entry !== "number" || !Number.isFinite(entry) || entry < 0) throw new Error(`invalid catalog count "${key}"`);
|
|
76936
76997
|
return entry;
|
|
@@ -76949,11 +77010,11 @@ function parseCatalogReading(value) {
|
|
|
76949
77010
|
fetchedAtMs: row["fetchedAtMs"],
|
|
76950
77011
|
quotaSource: quotaSource ?? "seed",
|
|
76951
77012
|
counts: {
|
|
76952
|
-
total:
|
|
76953
|
-
enabled:
|
|
76954
|
-
deprecated:
|
|
76955
|
-
unconfigured:
|
|
76956
|
-
inferred:
|
|
77013
|
+
total: count2("total"),
|
|
77014
|
+
enabled: count2("enabled"),
|
|
77015
|
+
deprecated: count2("deprecated"),
|
|
77016
|
+
unconfigured: count2("unconfigured"),
|
|
77017
|
+
inferred: count2("inferred")
|
|
76957
77018
|
}
|
|
76958
77019
|
};
|
|
76959
77020
|
}
|
|
@@ -77022,6 +77083,106 @@ var networkRemote = {
|
|
|
77022
77083
|
]
|
|
77023
77084
|
};
|
|
77024
77085
|
|
|
77086
|
+
// src/request-log/contract.ts
|
|
77087
|
+
function count(key, row, optional) {
|
|
77088
|
+
const entry = row[key];
|
|
77089
|
+
if (entry === void 0) {
|
|
77090
|
+
if (optional === true) return void 0;
|
|
77091
|
+
throw new Error(`invalid request-log entry "${key}"`);
|
|
77092
|
+
}
|
|
77093
|
+
if (typeof entry !== "number" || !Number.isFinite(entry) || entry < 0) {
|
|
77094
|
+
throw new Error(`invalid request-log entry "${key}"`);
|
|
77095
|
+
}
|
|
77096
|
+
return entry;
|
|
77097
|
+
}
|
|
77098
|
+
function text(key, row, optional) {
|
|
77099
|
+
const entry = row[key];
|
|
77100
|
+
if (entry === void 0) {
|
|
77101
|
+
if (optional === true) return void 0;
|
|
77102
|
+
throw new Error(`invalid request-log entry "${key}"`);
|
|
77103
|
+
}
|
|
77104
|
+
if (typeof entry !== "string") throw new Error(`invalid request-log entry "${key}"`);
|
|
77105
|
+
return entry;
|
|
77106
|
+
}
|
|
77107
|
+
function parseEntry(value) {
|
|
77108
|
+
if (value === null || typeof value !== "object") throw new Error("invalid request-log entry");
|
|
77109
|
+
const row = value;
|
|
77110
|
+
const shape = row["shape"];
|
|
77111
|
+
const headers = row["headers"];
|
|
77112
|
+
return {
|
|
77113
|
+
seq: count("seq", row),
|
|
77114
|
+
atMs: count("atMs", row),
|
|
77115
|
+
model: text("model", row),
|
|
77116
|
+
session: text("session", row),
|
|
77117
|
+
method: text("method", row),
|
|
77118
|
+
url: text("url", row),
|
|
77119
|
+
...row["status"] === void 0 ? {} : { status: count("status", row) },
|
|
77120
|
+
...row["inputTokens"] === void 0 ? {} : { inputTokens: count("inputTokens", row) },
|
|
77121
|
+
...row["outputTokens"] === void 0 ? {} : { outputTokens: count("outputTokens", row) },
|
|
77122
|
+
...row["cacheReadTokens"] === void 0 ? {} : { cacheReadTokens: count("cacheReadTokens", row) },
|
|
77123
|
+
...row["durationMs"] === void 0 ? {} : { durationMs: count("durationMs", row) },
|
|
77124
|
+
...row["firstTokenMs"] === void 0 ? {} : { firstTokenMs: count("firstTokenMs", row) },
|
|
77125
|
+
ok: row["ok"] === true,
|
|
77126
|
+
...typeof row["code"] === "string" ? { code: row["code"] } : {},
|
|
77127
|
+
...typeof row["poolKey"] === "string" ? { poolKey: row["poolKey"] } : {},
|
|
77128
|
+
...shape !== null && typeof shape === "object" ? {
|
|
77129
|
+
shape: {
|
|
77130
|
+
messages: count("messages", shape),
|
|
77131
|
+
hasImage: shape["hasImage"] === true,
|
|
77132
|
+
tools: count("tools", shape)
|
|
77133
|
+
}
|
|
77134
|
+
} : {},
|
|
77135
|
+
...headers !== null && typeof headers === "object" ? {
|
|
77136
|
+
headers: {
|
|
77137
|
+
...typeof headers["session"] === "string" ? { session: headers["session"] } : {},
|
|
77138
|
+
...typeof headers["userAgent"] === "string" ? { userAgent: headers["userAgent"] } : {}
|
|
77139
|
+
}
|
|
77140
|
+
} : {}
|
|
77141
|
+
};
|
|
77142
|
+
}
|
|
77143
|
+
function parsePoolEvent(value) {
|
|
77144
|
+
if (value === null || typeof value !== "object") throw new Error("invalid request-log pool event");
|
|
77145
|
+
const row = value;
|
|
77146
|
+
const type = row["type"];
|
|
77147
|
+
if (type !== "create" && type !== "close") throw new Error("invalid request-log pool event");
|
|
77148
|
+
return { atMs: count("atMs", row), type, key: text("key", row) };
|
|
77149
|
+
}
|
|
77150
|
+
function parseRequestLogSnapshot(value) {
|
|
77151
|
+
if (value === null || typeof value !== "object") throw new Error("invalid request-log snapshot");
|
|
77152
|
+
const row = value;
|
|
77153
|
+
if (!Array.isArray(row["entries"]) || !Array.isArray(row["poolEvents"])) {
|
|
77154
|
+
throw new Error("invalid request-log snapshot");
|
|
77155
|
+
}
|
|
77156
|
+
return {
|
|
77157
|
+
entries: row["entries"].map(parseEntry),
|
|
77158
|
+
poolEvents: row["poolEvents"].map(parsePoolEvent),
|
|
77159
|
+
totalRecords: count("totalRecords", row),
|
|
77160
|
+
droppedRecords: count("droppedRecords", row),
|
|
77161
|
+
newConnections: count("newConnections", row),
|
|
77162
|
+
...typeof row["poolKey"] === "string" ? { poolKey: row["poolKey"] } : {}
|
|
77163
|
+
};
|
|
77164
|
+
}
|
|
77165
|
+
var codec3 = {
|
|
77166
|
+
mode: "strict",
|
|
77167
|
+
typeSymbol: "@dan-ai-studio/dshopencodego#RequestLogSnapshot",
|
|
77168
|
+
schema: { parse: parseRequestLogSnapshot },
|
|
77169
|
+
create: () => ({ parse: parseRequestLogSnapshot })
|
|
77170
|
+
};
|
|
77171
|
+
var requestLogRemote = {
|
|
77172
|
+
package: "@dan-ai-studio/dshopencodego",
|
|
77173
|
+
descriptors: [
|
|
77174
|
+
{
|
|
77175
|
+
id: "@dan-ai-studio/dshopencodego#opencodeGoRequestLog/readLog",
|
|
77176
|
+
service: "opencodeGoRequestLog",
|
|
77177
|
+
namespace: "opencodeGoRequestLog",
|
|
77178
|
+
method: "readLog",
|
|
77179
|
+
invocation: { kind: "direct" },
|
|
77180
|
+
parameters: [],
|
|
77181
|
+
result: codec3
|
|
77182
|
+
}
|
|
77183
|
+
]
|
|
77184
|
+
};
|
|
77185
|
+
|
|
77025
77186
|
// src/update/contract.ts
|
|
77026
77187
|
function parseUpdateStatus(value) {
|
|
77027
77188
|
if (value === null || typeof value !== "object") throw new Error("invalid update reading");
|
|
@@ -77041,7 +77202,7 @@ function parseUpdateStatus(value) {
|
|
|
77041
77202
|
checked: row["checked"]
|
|
77042
77203
|
};
|
|
77043
77204
|
}
|
|
77044
|
-
var
|
|
77205
|
+
var codec4 = (typeSymbol, parse) => ({
|
|
77045
77206
|
mode: "strict",
|
|
77046
77207
|
typeSymbol,
|
|
77047
77208
|
schema: { parse },
|
|
@@ -77057,7 +77218,7 @@ var updateRemote = {
|
|
|
77057
77218
|
method: "readUpdate",
|
|
77058
77219
|
invocation: { kind: "direct" },
|
|
77059
77220
|
parameters: [],
|
|
77060
|
-
result:
|
|
77221
|
+
result: codec4("@dan-ai-studio/dshopencodego#UpdateStatus", parseUpdateStatus)
|
|
77061
77222
|
},
|
|
77062
77223
|
{
|
|
77063
77224
|
id: "@dan-ai-studio/dshopencodego#opencodeGoUpdate/refreshUpdate",
|
|
@@ -77066,7 +77227,7 @@ var updateRemote = {
|
|
|
77066
77227
|
method: "refreshUpdate",
|
|
77067
77228
|
invocation: { kind: "direct" },
|
|
77068
77229
|
parameters: [],
|
|
77069
|
-
result:
|
|
77230
|
+
result: codec4("@dan-ai-studio/dshopencodego#UpdateStatus", parseUpdateStatus)
|
|
77070
77231
|
}
|
|
77071
77232
|
]
|
|
77072
77233
|
};
|
|
@@ -77093,22 +77254,22 @@ function parseUsageWindows(value) {
|
|
|
77093
77254
|
function parseTotals(value) {
|
|
77094
77255
|
if (value === null || typeof value !== "object") throw new Error("invalid meter totals");
|
|
77095
77256
|
const row = value;
|
|
77096
|
-
const
|
|
77257
|
+
const count2 = (key) => {
|
|
77097
77258
|
const entry = row[key];
|
|
77098
77259
|
if (typeof entry !== "number" || !Number.isFinite(entry) || entry < 0) throw new Error(`invalid meter field "${key}"`);
|
|
77099
77260
|
return entry;
|
|
77100
77261
|
};
|
|
77101
|
-
const optionalCount = (key) => row[key] === void 0 ? void 0 :
|
|
77262
|
+
const optionalCount = (key) => row[key] === void 0 ? void 0 : count2(key);
|
|
77102
77263
|
const reasoningTokens = optionalCount("reasoningTokens");
|
|
77103
77264
|
const estimatedUSD = optionalCount("estimatedUSD");
|
|
77104
77265
|
const unpricedTokens = optionalCount("unpricedTokens");
|
|
77105
77266
|
return {
|
|
77106
|
-
calls:
|
|
77107
|
-
inputTokens:
|
|
77108
|
-
outputTokens:
|
|
77109
|
-
cacheReadTokens:
|
|
77110
|
-
cacheWriteTokens:
|
|
77111
|
-
totalTokens:
|
|
77267
|
+
calls: count2("calls"),
|
|
77268
|
+
inputTokens: count2("inputTokens"),
|
|
77269
|
+
outputTokens: count2("outputTokens"),
|
|
77270
|
+
cacheReadTokens: count2("cacheReadTokens"),
|
|
77271
|
+
cacheWriteTokens: count2("cacheWriteTokens"),
|
|
77272
|
+
totalTokens: count2("totalTokens"),
|
|
77112
77273
|
...reasoningTokens === void 0 ? {} : { reasoningTokens },
|
|
77113
77274
|
...estimatedUSD === void 0 ? {} : { estimatedUSD },
|
|
77114
77275
|
...unpricedTokens === void 0 ? {} : { unpricedTokens }
|
|
@@ -77132,7 +77293,7 @@ function parseGoMeter(value) {
|
|
|
77132
77293
|
})
|
|
77133
77294
|
};
|
|
77134
77295
|
}
|
|
77135
|
-
var
|
|
77296
|
+
var codec5 = (typeSymbol, parse) => ({
|
|
77136
77297
|
mode: "strict",
|
|
77137
77298
|
typeSymbol,
|
|
77138
77299
|
schema: { parse },
|
|
@@ -77148,7 +77309,7 @@ var usageRemote = {
|
|
|
77148
77309
|
method: "readWindows",
|
|
77149
77310
|
invocation: { kind: "direct" },
|
|
77150
77311
|
parameters: [],
|
|
77151
|
-
result:
|
|
77312
|
+
result: codec5("@dan-ai-studio/dshopencodego#GoUsageWindows", parseUsageWindows)
|
|
77152
77313
|
},
|
|
77153
77314
|
{
|
|
77154
77315
|
id: "@dan-ai-studio/dshopencodego#opencodeGoUsage/readMeter",
|
|
@@ -77157,7 +77318,7 @@ var usageRemote = {
|
|
|
77157
77318
|
method: "readMeter",
|
|
77158
77319
|
invocation: { kind: "direct" },
|
|
77159
77320
|
parameters: [],
|
|
77160
|
-
result:
|
|
77321
|
+
result: codec5("@dan-ai-studio/dshopencodego#GoMeter", parseGoMeter)
|
|
77161
77322
|
}
|
|
77162
77323
|
]
|
|
77163
77324
|
};
|
|
@@ -77169,7 +77330,8 @@ var contribution = {
|
|
|
77169
77330
|
...usageRemote.descriptors,
|
|
77170
77331
|
...catalogRemote.descriptors,
|
|
77171
77332
|
...updateRemote.descriptors,
|
|
77172
|
-
...networkRemote.descriptors
|
|
77333
|
+
...networkRemote.descriptors,
|
|
77334
|
+
...requestLogRemote.descriptors
|
|
77173
77335
|
]
|
|
77174
77336
|
};
|
|
77175
77337
|
function registerRemotes(ctx) {
|
|
@@ -77339,21 +77501,30 @@ function pinnedLookup(address) {
|
|
|
77339
77501
|
};
|
|
77340
77502
|
}
|
|
77341
77503
|
var current;
|
|
77504
|
+
var observer;
|
|
77505
|
+
function observePool(next) {
|
|
77506
|
+
observer = next;
|
|
77507
|
+
}
|
|
77342
77508
|
function dispatcherFor(host, address, keepAliveTimeoutMs) {
|
|
77343
77509
|
const key = `${host}@${address}@${String(keepAliveTimeoutMs)}`;
|
|
77344
77510
|
if (current?.key === key) return current.agent;
|
|
77345
|
-
|
|
77511
|
+
const previous = current;
|
|
77512
|
+
void previous?.agent.close();
|
|
77513
|
+
if (previous !== void 0) observer?.close(previous.key);
|
|
77346
77514
|
const agent = new import_undici.Agent({
|
|
77347
77515
|
connect: { lookup: pinnedLookup(address) },
|
|
77348
77516
|
keepAliveTimeout: keepAliveTimeoutMs,
|
|
77349
77517
|
keepAliveMaxTimeout: keepAliveTimeoutMs
|
|
77350
77518
|
});
|
|
77351
77519
|
current = { key, agent };
|
|
77520
|
+
observer?.create(key);
|
|
77352
77521
|
return agent;
|
|
77353
77522
|
}
|
|
77354
77523
|
async function closeDispatcher() {
|
|
77355
77524
|
const agent = current?.agent;
|
|
77525
|
+
const key = current?.key;
|
|
77356
77526
|
current = void 0;
|
|
77527
|
+
if (key !== void 0) observer?.close(key);
|
|
77357
77528
|
if (agent !== void 0) await agent.close();
|
|
77358
77529
|
}
|
|
77359
77530
|
var SPENT_ADDRESS_CODES = /* @__PURE__ */ new Set([
|
|
@@ -77469,6 +77640,148 @@ var OpencodeGoNetworkService = class extends TypertRemoteService2 {
|
|
|
77469
77640
|
}
|
|
77470
77641
|
};
|
|
77471
77642
|
|
|
77643
|
+
// src/request-log/service.ts
|
|
77644
|
+
import { TypertRemoteService as TypertRemoteService3 } from "@deepseek-ai/dsh-typert-protocol";
|
|
77645
|
+
var OpencodeGoRequestLogService = class extends TypertRemoteService3 {
|
|
77646
|
+
options;
|
|
77647
|
+
constructor(ctx, options) {
|
|
77648
|
+
super(ctx, "opencodeGoRequestLog");
|
|
77649
|
+
this.options = options;
|
|
77650
|
+
}
|
|
77651
|
+
/** The current reading, trimmed to the configured bound first. */
|
|
77652
|
+
readLog() {
|
|
77653
|
+
this.options.log.setMaxEntries(this.options.maxEntries());
|
|
77654
|
+
return this.options.log.snapshot();
|
|
77655
|
+
}
|
|
77656
|
+
};
|
|
77657
|
+
|
|
77658
|
+
// src/request-log/store.ts
|
|
77659
|
+
var SESSION_PREFIX_LENGTH = 8;
|
|
77660
|
+
var MAX_POOL_EVENTS = 20;
|
|
77661
|
+
var RequestLog = class {
|
|
77662
|
+
gates;
|
|
77663
|
+
entries = [];
|
|
77664
|
+
poolEvents = [];
|
|
77665
|
+
seq = 0;
|
|
77666
|
+
totalRecords = 0;
|
|
77667
|
+
droppedRecords = 0;
|
|
77668
|
+
newConnections = 0;
|
|
77669
|
+
poolKey;
|
|
77670
|
+
constructor(gates) {
|
|
77671
|
+
this.gates = gates;
|
|
77672
|
+
}
|
|
77673
|
+
/**
|
|
77674
|
+
* Count one accepted socket. Called from the diagnostics channel, not the
|
|
77675
|
+
* request path, so its cost never lands on a request.
|
|
77676
|
+
*/
|
|
77677
|
+
connectionOpened() {
|
|
77678
|
+
this.newConnections += 1;
|
|
77679
|
+
}
|
|
77680
|
+
/**
|
|
77681
|
+
* Record one pool lifecycle event. Pools live for minutes, so this is rare
|
|
77682
|
+
* by construction; still gated on debug so the common log stays request-only.
|
|
77683
|
+
*/
|
|
77684
|
+
poolEvent(type, key) {
|
|
77685
|
+
if (!this.gates.enabled() || !this.gates.debug()) return;
|
|
77686
|
+
this.poolKey = type === "create" ? key : this.poolKey;
|
|
77687
|
+
if (type === "close" && this.poolKey === key) this.poolKey = void 0;
|
|
77688
|
+
this.poolEvents = [{ atMs: Date.now(), type, key }, ...this.poolEvents].slice(0, MAX_POOL_EVENTS);
|
|
77689
|
+
}
|
|
77690
|
+
/**
|
|
77691
|
+
* Begin one request's span, or nothing when the log is disabled.
|
|
77692
|
+
*
|
|
77693
|
+
* This is the single gate on the request path: a disabled log costs one
|
|
77694
|
+
* method call and one boolean read, and constructs nothing.
|
|
77695
|
+
*/
|
|
77696
|
+
start(detail) {
|
|
77697
|
+
if (!this.gates.enabled()) return void 0;
|
|
77698
|
+
const debug = this.gates.debug();
|
|
77699
|
+
const startedAt = Date.now();
|
|
77700
|
+
const full = detail.sessionId ?? "";
|
|
77701
|
+
const session = debug || full.length === 0 ? full : full.slice(0, SESSION_PREFIX_LENGTH);
|
|
77702
|
+
let firstTokenAt;
|
|
77703
|
+
let method;
|
|
77704
|
+
let url;
|
|
77705
|
+
let status;
|
|
77706
|
+
let finished = false;
|
|
77707
|
+
return {
|
|
77708
|
+
firstToken: () => {
|
|
77709
|
+
if (firstTokenAt === void 0) firstTokenAt = Date.now();
|
|
77710
|
+
},
|
|
77711
|
+
request: (nextMethod, nextUrl) => {
|
|
77712
|
+
if (method === void 0) {
|
|
77713
|
+
method = nextMethod;
|
|
77714
|
+
url = nextUrl;
|
|
77715
|
+
}
|
|
77716
|
+
},
|
|
77717
|
+
status: (value) => {
|
|
77718
|
+
status = value;
|
|
77719
|
+
},
|
|
77720
|
+
finish: (ok, code, usage) => {
|
|
77721
|
+
if (finished) return;
|
|
77722
|
+
finished = true;
|
|
77723
|
+
const endedAt = Date.now();
|
|
77724
|
+
const entry = {
|
|
77725
|
+
seq: ++this.seq,
|
|
77726
|
+
atMs: startedAt,
|
|
77727
|
+
model: detail.model,
|
|
77728
|
+
session,
|
|
77729
|
+
method: method ?? detail.method ?? "POST",
|
|
77730
|
+
url: url ?? detail.url ?? "",
|
|
77731
|
+
...status === void 0 ? {} : { status },
|
|
77732
|
+
...usage === void 0 ? {} : {
|
|
77733
|
+
inputTokens: usage.inputTokens,
|
|
77734
|
+
outputTokens: usage.outputTokens,
|
|
77735
|
+
...usage.cacheReadTokens === void 0 || usage.cacheReadTokens <= 0 ? {} : { cacheReadTokens: usage.cacheReadTokens }
|
|
77736
|
+
},
|
|
77737
|
+
durationMs: Math.max(0, endedAt - startedAt),
|
|
77738
|
+
...firstTokenAt === void 0 ? {} : { firstTokenMs: Math.max(0, firstTokenAt - startedAt) },
|
|
77739
|
+
ok,
|
|
77740
|
+
...code === void 0 ? {} : { code },
|
|
77741
|
+
// The pool in force when the request finished. Pool switches are
|
|
77742
|
+
// rare enough that attributing the finishing pool is honest; the
|
|
77743
|
+
// create/close events carry the exact history.
|
|
77744
|
+
...this.poolKey === void 0 ? {} : { poolKey: this.poolKey },
|
|
77745
|
+
shape: { messages: detail.messageCount, hasImage: detail.hasImage, tools: detail.toolCount },
|
|
77746
|
+
...!debug ? {} : {
|
|
77747
|
+
headers: {
|
|
77748
|
+
...detail.headers.session === void 0 ? {} : { session: detail.headers.session },
|
|
77749
|
+
...detail.headers.userAgent === void 0 ? {} : { userAgent: detail.headers.userAgent }
|
|
77750
|
+
}
|
|
77751
|
+
}
|
|
77752
|
+
};
|
|
77753
|
+
this.append(entry);
|
|
77754
|
+
}
|
|
77755
|
+
};
|
|
77756
|
+
}
|
|
77757
|
+
append(entry) {
|
|
77758
|
+
this.totalRecords += 1;
|
|
77759
|
+
const bound = Math.max(1, Math.floor(this.gates.maxEntries()));
|
|
77760
|
+
const next = [entry, ...this.entries].slice(0, bound);
|
|
77761
|
+
this.droppedRecords += this.entries.length + 1 - next.length;
|
|
77762
|
+
this.entries = next;
|
|
77763
|
+
}
|
|
77764
|
+
/** Shrink the ring to a new bound immediately; growing backfills nothing. */
|
|
77765
|
+
setMaxEntries(bound) {
|
|
77766
|
+
if (!Number.isSafeInteger(bound) || bound < 1) return;
|
|
77767
|
+
if (this.entries.length > bound) {
|
|
77768
|
+
this.droppedRecords += this.entries.length - bound;
|
|
77769
|
+
this.entries = this.entries.slice(0, bound);
|
|
77770
|
+
}
|
|
77771
|
+
}
|
|
77772
|
+
/** The current reading, newest first. Computed lazily: nothing on the hot path. */
|
|
77773
|
+
snapshot() {
|
|
77774
|
+
return {
|
|
77775
|
+
entries: this.entries,
|
|
77776
|
+
poolEvents: this.poolEvents,
|
|
77777
|
+
totalRecords: this.totalRecords,
|
|
77778
|
+
droppedRecords: this.droppedRecords,
|
|
77779
|
+
newConnections: this.newConnections,
|
|
77780
|
+
...this.poolKey === void 0 ? {} : { poolKey: this.poolKey }
|
|
77781
|
+
};
|
|
77782
|
+
}
|
|
77783
|
+
};
|
|
77784
|
+
|
|
77472
77785
|
// src/update/check.ts
|
|
77473
77786
|
var PACKAGE_NAME = "@dan-ai-studio/dshopencodego";
|
|
77474
77787
|
var REGISTRY_LATEST_URL = `https://registry.npmjs.org/${encodeURIComponent(PACKAGE_NAME)}/latest`;
|
|
@@ -77562,11 +77875,11 @@ function createUpdateChecker(deps) {
|
|
|
77562
77875
|
}
|
|
77563
77876
|
|
|
77564
77877
|
// src/update/version.ts
|
|
77565
|
-
var PLUGIN_VERSION = true ? "0.1.
|
|
77878
|
+
var PLUGIN_VERSION = true ? "0.1.64" : "0.0.0-dev";
|
|
77566
77879
|
|
|
77567
77880
|
// src/update/service.ts
|
|
77568
|
-
import { TypertRemoteService as
|
|
77569
|
-
var OpencodeGoUpdateService = class extends
|
|
77881
|
+
import { TypertRemoteService as TypertRemoteService4 } from "@deepseek-ai/dsh-typert-protocol";
|
|
77882
|
+
var OpencodeGoUpdateService = class extends TypertRemoteService4 {
|
|
77570
77883
|
check;
|
|
77571
77884
|
constructor(ctx, options) {
|
|
77572
77885
|
super(ctx, "opencodeGoUpdate");
|
|
@@ -77646,7 +77959,7 @@ async function readUsageWindows(options) {
|
|
|
77646
77959
|
|
|
77647
77960
|
// src/usage/service.ts
|
|
77648
77961
|
import { randomUUID as randomUUID3 } from "node:crypto";
|
|
77649
|
-
import { RemoteError as RemoteError2, TypertRemoteService as
|
|
77962
|
+
import { RemoteError as RemoteError2, TypertRemoteService as TypertRemoteService5 } from "@deepseek-ai/dsh-typert-protocol";
|
|
77650
77963
|
import { LlmError as LlmError11 } from "@deepseek-ai/dsh-llm";
|
|
77651
77964
|
function usageFailure(error, source) {
|
|
77652
77965
|
const missing = error instanceof LlmError11 && error.code === "MISSING_CREDENTIAL";
|
|
@@ -77657,7 +77970,7 @@ function usageFailure(error, source) {
|
|
|
77657
77970
|
{ cause: error }
|
|
77658
77971
|
);
|
|
77659
77972
|
}
|
|
77660
|
-
var OpencodeGoUsageService = class extends
|
|
77973
|
+
var OpencodeGoUsageService = class extends TypertRemoteService5 {
|
|
77661
77974
|
identity;
|
|
77662
77975
|
options;
|
|
77663
77976
|
constructor(ctx, options) {
|
|
@@ -77732,6 +78045,29 @@ function apply(ctx, raw) {
|
|
|
77732
78045
|
};
|
|
77733
78046
|
const meter = new UsageMeter();
|
|
77734
78047
|
const cacheDir = resolveCacheDir(ctx);
|
|
78048
|
+
const requestLog = new RequestLog({
|
|
78049
|
+
enabled: () => current2().requestLogEnabled,
|
|
78050
|
+
debug: () => current2().requestLogDebug,
|
|
78051
|
+
maxEntries: () => current2().requestLogMaxEntries
|
|
78052
|
+
});
|
|
78053
|
+
observePool({
|
|
78054
|
+
create: (key) => {
|
|
78055
|
+
requestLog.poolEvent("create", key);
|
|
78056
|
+
},
|
|
78057
|
+
close: (key) => {
|
|
78058
|
+
requestLog.poolEvent("close", key);
|
|
78059
|
+
}
|
|
78060
|
+
});
|
|
78061
|
+
ctx.effect(() => {
|
|
78062
|
+
const channel = dc.channel("undici:client:connected");
|
|
78063
|
+
const onConnect = () => {
|
|
78064
|
+
requestLog.connectionOpened();
|
|
78065
|
+
};
|
|
78066
|
+
channel.subscribe(onConnect);
|
|
78067
|
+
return () => {
|
|
78068
|
+
channel.unsubscribe(onConnect);
|
|
78069
|
+
};
|
|
78070
|
+
}, "dshopencodego: connection counter");
|
|
77735
78071
|
const gatewayRequest = () => gatewayFetch(
|
|
77736
78072
|
resolveGatewayPin(current2().pinnedAddresses, current2().baseURL),
|
|
77737
78073
|
current2().keepAliveTimeoutMs
|
|
@@ -77760,7 +78096,8 @@ function apply(ctx, raw) {
|
|
|
77760
78096
|
},
|
|
77761
78097
|
onUsage: ({ model, usage }) => {
|
|
77762
78098
|
meter.record(model, usage);
|
|
77763
|
-
}
|
|
78099
|
+
},
|
|
78100
|
+
requestLog
|
|
77764
78101
|
});
|
|
77765
78102
|
registerRemotes(ctx);
|
|
77766
78103
|
ctx.plugin(OpencodeGoUsageService, {
|
|
@@ -77769,6 +78106,10 @@ function apply(ctx, raw) {
|
|
|
77769
78106
|
meter,
|
|
77770
78107
|
fetch: gatewayRequest
|
|
77771
78108
|
});
|
|
78109
|
+
ctx.plugin(OpencodeGoRequestLogService, {
|
|
78110
|
+
log: requestLog,
|
|
78111
|
+
maxEntries: () => current2().requestLogMaxEntries
|
|
78112
|
+
});
|
|
77772
78113
|
ctx.plugin(OpencodeGoNetworkService, {
|
|
77773
78114
|
baseURL: () => current2().baseURL,
|
|
77774
78115
|
pinnedAddresses: () => current2().pinnedAddresses
|