bingocode 1.1.200-beta.17 → 1.1.200-beta.19
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/package.json
CHANGED
|
@@ -9,6 +9,7 @@
|
|
|
9
9
|
import * as fs from 'fs/promises'
|
|
10
10
|
import * as path from 'path'
|
|
11
11
|
import * as os from 'os'
|
|
12
|
+
import { parse as parseJsonc } from 'jsonc-parser/lib/esm/main.js'
|
|
12
13
|
import { getDirectFetchOptions } from '../../utils/proxy.ts'
|
|
13
14
|
import { ApiError } from '../middleware/errorHandler.js'
|
|
14
15
|
import { anthropicToOpenaiChat } from '../proxy/transform/anthropicToOpenaiChat.js'
|
|
@@ -620,7 +621,9 @@ export class ProviderService {
|
|
|
620
621
|
let originalContent: string | null = null
|
|
621
622
|
try {
|
|
622
623
|
originalContent = await fs.readFile(settingsPath, "utf-8")
|
|
623
|
-
settings
|
|
624
|
+
// VS Code settings.json uses JSONC (JSON with comments) —
|
|
625
|
+
// parseJsonc handles comments/trailing commas, unlike JSON.parse
|
|
626
|
+
settings = parseJsonc(originalContent) as Record<string, unknown>
|
|
624
627
|
} catch {
|
|
625
628
|
// File does not exist yet — start fresh
|
|
626
629
|
}
|
|
@@ -650,7 +653,9 @@ export class ProviderService {
|
|
|
650
653
|
|
|
651
654
|
try {
|
|
652
655
|
await fs.mkdir(path.dirname(settingsPath), { recursive: true })
|
|
653
|
-
|
|
656
|
+
const tmpPath = `${settingsPath}.tmp.${Date.now()}`
|
|
657
|
+
await fs.writeFile(tmpPath, JSON.stringify(settings, null, 4) + '\n')
|
|
658
|
+
await fs.rename(tmpPath, settingsPath)
|
|
654
659
|
writtenPaths.push(settingsPath)
|
|
655
660
|
} catch (err) {
|
|
656
661
|
failedPaths.push(settingsPath)
|
|
@@ -661,7 +666,9 @@ export class ProviderService {
|
|
|
661
666
|
// Fix #11: Attempt restore from backup on write failure
|
|
662
667
|
if (originalContent !== null) {
|
|
663
668
|
try {
|
|
664
|
-
|
|
669
|
+
const tmpPath2 = `${settingsPath}.tmp.${Date.now()}`
|
|
670
|
+
await fs.writeFile(tmpPath2, originalContent, "utf-8")
|
|
671
|
+
await fs.rename(tmpPath2, settingsPath)
|
|
665
672
|
console.error(`[ProviderService] Restored ${settingsPath} from in-memory copy after write failure`)
|
|
666
673
|
} catch {
|
|
667
674
|
// In-memory restore failed, try .bak file
|
|
@@ -694,7 +701,9 @@ export class ProviderService {
|
|
|
694
701
|
const original = originalContents.get(p)
|
|
695
702
|
try {
|
|
696
703
|
if (original !== null) {
|
|
697
|
-
|
|
704
|
+
const tmpPath = `${p}.tmp.${Date.now()}`
|
|
705
|
+
await fs.writeFile(tmpPath, original, "utf-8")
|
|
706
|
+
await fs.rename(tmpPath, p)
|
|
698
707
|
} else {
|
|
699
708
|
await fs.unlink(p).catch(() => {})
|
|
700
709
|
}
|
|
@@ -754,14 +763,16 @@ export class ProviderService {
|
|
|
754
763
|
for (const settingsPath of linkedPaths) {
|
|
755
764
|
try {
|
|
756
765
|
const raw = await fs.readFile(settingsPath, "utf-8")
|
|
757
|
-
const settings =
|
|
766
|
+
const settings = parseJsonc(raw) as Record<string, unknown>
|
|
758
767
|
settings.claudeCode = preLinkClaudeCode[settingsPath] ?? {
|
|
759
768
|
preferredLocation: "panel",
|
|
760
769
|
environmentVariables: [
|
|
761
770
|
{ name: "ANTHROPIC_BASE_URL", value: "https://api.anthropic.com" },
|
|
762
771
|
],
|
|
763
772
|
}
|
|
764
|
-
|
|
773
|
+
const tmpPath = `${settingsPath}.tmp.${Date.now()}`
|
|
774
|
+
await fs.writeFile(tmpPath, JSON.stringify(settings, null, 4) + '\n')
|
|
775
|
+
await fs.rename(tmpPath, settingsPath)
|
|
765
776
|
} catch {
|
|
766
777
|
// File may have been moved or deleted since linking — harmless
|
|
767
778
|
}
|
|
@@ -55,7 +55,7 @@ import {
|
|
|
55
55
|
} from '../../utils/permissions/yoloClassifier.js'
|
|
56
56
|
import { emitTaskProgress as emitTaskProgressEvent } from '../../utils/task/sdkProgress.js'
|
|
57
57
|
import { isInProcessTeammate } from '../../utils/teammateContext.js'
|
|
58
|
-
import {
|
|
58
|
+
import { getTokenCountFromMessages } from '../../utils/tokens.js'
|
|
59
59
|
import { EXIT_PLAN_MODE_V2_TOOL_NAME } from '../ExitPlanModeTool/constants.js'
|
|
60
60
|
import { AGENT_TOOL_NAME, LEGACY_AGENT_TOOL_NAME } from './constants.js'
|
|
61
61
|
import type { AgentDefinition } from './loadAgentsDir.js'
|
|
@@ -316,7 +316,7 @@ export function finalizeAgentTool(
|
|
|
316
316
|
}
|
|
317
317
|
}
|
|
318
318
|
|
|
319
|
-
const totalTokens =
|
|
319
|
+
const totalTokens = getTokenCountFromMessages(agentMessages)
|
|
320
320
|
const totalToolUseCount = countToolUses(agentMessages)
|
|
321
321
|
|
|
322
322
|
logEvent('tengu_agent_tool_completed', {
|
package/src/utils/tokens.ts
CHANGED
|
@@ -52,6 +52,29 @@ export function getTokenCountFromUsage(usage: Usage): number {
|
|
|
52
52
|
)
|
|
53
53
|
}
|
|
54
54
|
|
|
55
|
+
/**
|
|
56
|
+
* Calculate total tokens across all agent messages, matching the ProgressTracker
|
|
57
|
+
* accumulation logic in LocalAgentTask. In Claude's API:
|
|
58
|
+
* - input_tokens (and cache tokens) are cumulative per turn — each call already
|
|
59
|
+
* includes all previous context, so we keep only the LATEST value.
|
|
60
|
+
* - output_tokens are per-turn independent, so we SUM them across all turns.
|
|
61
|
+
*/
|
|
62
|
+
export function getTokenCountFromMessages(messages: Message[]): number {
|
|
63
|
+
let latestInputTokens = 0
|
|
64
|
+
let cumulativeOutputTokens = 0
|
|
65
|
+
for (const message of messages) {
|
|
66
|
+
const usage = getTokenUsage(message)
|
|
67
|
+
if (usage) {
|
|
68
|
+
latestInputTokens =
|
|
69
|
+
usage.input_tokens +
|
|
70
|
+
(usage.cache_creation_input_tokens ?? 0) +
|
|
71
|
+
(usage.cache_read_input_tokens ?? 0)
|
|
72
|
+
cumulativeOutputTokens += usage.output_tokens
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
return latestInputTokens + cumulativeOutputTokens
|
|
76
|
+
}
|
|
77
|
+
|
|
55
78
|
export function tokenCountFromLastAPIResponse(messages: Message[]): number {
|
|
56
79
|
let i = messages.length - 1
|
|
57
80
|
while (i >= 0) {
|