Skip to content

Commit 01d7aa0

Browse files
committed
fix: resolve TypeScript errors and update test assertions
- Add type guards for AgentOutput.message access in SDK retry tests - Add optional chaining for possibly undefined sessionState in evals - Update context-pruner tests to use correct message structure (toolName at message level, not in content)
1 parent 23f80b1 commit 01d7aa0

File tree

6 files changed

+54
-47
lines changed

6 files changed

+54
-47
lines changed

.agents/__tests__/context-pruner.test.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -140,20 +140,20 @@ describe('context-pruner handleSteps', () => {
140140
const firstTerminalMessage = resultMessages.find(
141141
(m: any) =>
142142
m.role === 'tool' &&
143-
m.content?.toolName === 'run_terminal_command' &&
144-
m.content?.output?.[0]?.value?.command === 'command-1',
143+
m.toolName === 'run_terminal_command' &&
144+
m.content?.[0]?.value?.command === 'command-1',
145145
)
146146
expect(
147-
firstTerminalMessage?.content?.output?.[0]?.value?.stdoutOmittedForLength,
147+
firstTerminalMessage?.content?.[0]?.value?.stdoutOmittedForLength,
148148
).toBe(true)
149149

150150
// Check that recent terminal commands are preserved (but may be processed by large tool result pass)
151151
const recentTerminalMessage = resultMessages.find(
152152
(m: any) =>
153153
m.role === 'tool' &&
154-
m.content?.toolName === 'run_terminal_command' &&
155-
(m.content?.output?.[0]?.value?.command === 'command-7' ||
156-
m.content?.output?.[0]?.value?.message ===
154+
m.toolName === 'run_terminal_command' &&
155+
(m.content?.[0]?.value?.command === 'command-7' ||
156+
m.content?.[0]?.value?.message ===
157157
'[LARGE_TOOL_RESULT_OMITTED]'),
158158
)
159159
expect(recentTerminalMessage).toBeDefined()
@@ -181,17 +181,17 @@ describe('context-pruner handleSteps', () => {
181181

182182
// Large tool result should be simplified
183183
const largeResultMessage = resultMessages.find(
184-
(m: any) => m.role === 'tool' && m.content?.toolName === 'read_files',
184+
(m: any) => m.role === 'tool' && m.toolName === 'read_files',
185185
)
186-
expect(largeResultMessage?.content?.output?.[0]?.value?.message).toBe(
186+
expect(largeResultMessage?.content?.[0]?.value?.message).toBe(
187187
'[LARGE_TOOL_RESULT_OMITTED]',
188188
)
189189

190190
// Small tool result should be preserved
191191
const smallResultMessage = resultMessages.find(
192-
(m: any) => m.role === 'tool' && m.content?.toolName === 'code_search',
192+
(m: any) => m.role === 'tool' && m.toolName === 'code_search',
193193
)
194-
expect(smallResultMessage?.content?.output?.[0]?.value?.data).toBe(
194+
expect(smallResultMessage?.content?.[0]?.value?.data).toBe(
195195
'Small result',
196196
)
197197
})
@@ -367,7 +367,7 @@ describe('context-pruner edge cases', () => {
367367
// Valid terminal command should be processed correctly
368368
const validCommand = resultMessages.find(
369369
(m: any) =>
370-
m.role === 'tool' && m.content?.toolName === 'run_terminal_command',
370+
m.role === 'tool' && m.toolName === 'run_terminal_command',
371371
)
372372
expect(validCommand).toBeDefined()
373373
})
@@ -481,7 +481,7 @@ describe('context-pruner edge cases', () => {
481481
const hasLargeToolResultReplacement = resultMessages.some(
482482
(m: any) =>
483483
m.role === 'tool' &&
484-
m.content?.output?.[0]?.value?.message ===
484+
m.content?.[0]?.value?.message ===
485485
'[LARGE_TOOL_RESULT_OMITTED]',
486486
)
487487
expect(hasLargeToolResultReplacement).toBe(true)

cli/src/chat.tsx

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
1+
import { RECONNECTION_MESSAGE_DURATION_MS } from '@codebuff/sdk'
12
import { useKeyboard } from '@opentui/react'
2-
import { useCallback, useEffect, useMemo, useRef, useState } from 'react'
3+
import { useQueryClient } from '@tanstack/react-query'
4+
import {
5+
useCallback,
6+
useEffect,
7+
useMemo,
8+
useRef,
9+
useState,
10+
useTransition,
11+
} from 'react'
312
import { useShallow } from 'zustand/react/shallow'
413

514
import { routeUserPrompt } from './commands/router'
@@ -9,11 +18,12 @@ import { MessageWithAgents } from './components/message-with-agents'
918
import { StatusBar } from './components/status-bar'
1019
import { SLASH_COMMANDS } from './data/slash-commands'
1120
import { useAgentValidation } from './hooks/use-agent-validation'
21+
import { authQueryKeys } from './hooks/use-auth-query'
1222
import { useChatInput } from './hooks/use-chat-input'
1323
import { useClipboard } from './hooks/use-clipboard'
1424
import { useConnectionStatus } from './hooks/use-connection-status'
1525
import { useElapsedTime } from './hooks/use-elapsed-time'
16-
import { useTimeout } from './hooks/use-timeout'
26+
import { useEvent } from './hooks/use-event'
1727
import { useExitHandler } from './hooks/use-exit-handler'
1828
import { useInputHistory } from './hooks/use-input-history'
1929
import { useKeyboardHandlers } from './hooks/use-keyboard-handlers'
@@ -26,6 +36,7 @@ import { useSuggestionEngine } from './hooks/use-suggestion-engine'
2636
import { useSuggestionMenuHandlers } from './hooks/use-suggestion-menu-handlers'
2737
import { useTerminalDimensions } from './hooks/use-terminal-dimensions'
2838
import { useTheme } from './hooks/use-theme'
39+
import { useTimeout } from './hooks/use-timeout'
2940
import { useValidationBanner } from './hooks/use-validation-banner'
3041
import { useChatStore } from './state/chat-store'
3142
import { useFeedbackStore } from './state/feedback-store'
@@ -36,23 +47,18 @@ import {
3647
getStatusIndicatorState,
3748
type AuthStatus,
3849
} from './utils/status-indicator-state'
39-
import { authQueryKeys } from './hooks/use-auth-query'
40-
import { RECONNECTION_MESSAGE_DURATION_MS } from '@codebuff/sdk'
41-
import { useQueryClient } from '@tanstack/react-query'
42-
import { useTransition } from 'react'
4350
import { computeInputLayoutMetrics } from './utils/text-layout'
4451
import { createMarkdownPalette } from './utils/theme-system'
4552

4653
import type { MultilineInputHandle } from './components/multiline-input'
4754
import type { ContentBlock } from './types/chat'
4855
import type { SendMessageFn } from './types/contracts/send-message'
4956
import type { User } from './utils/auth'
57+
import type { AgentMode } from './utils/constants'
5058
import type { FileTreeNode } from '@codebuff/common/util/file'
5159
import type { ScrollBoxRenderable } from '@opentui/core'
5260
import type { UseMutationResult } from '@tanstack/react-query'
5361
import type { Dispatch, SetStateAction } from 'react'
54-
import { useEvent } from './hooks/use-event'
55-
import { AgentMode } from './utils/constants'
5662

5763
export const Chat = ({
5864
headerContent,
@@ -133,16 +139,16 @@ export const Chat = ({
133139
setLastMessageMode,
134140
addSessionCredits,
135141
resetChatStore,
136-
sessionCreditsUsed,
137-
setRunState,
138-
isAnnouncementVisible,
139-
setIsAnnouncementVisible,
140-
isRetrying,
141-
} = useChatStore(
142-
useShallow((store) => ({
143-
inputValue: store.inputValue,
144-
cursorPosition: store.cursorPosition,
145-
lastEditDueToNav: store.lastEditDueToNav,
142+
sessionCreditsUsed,
143+
setRunState,
144+
isAnnouncementVisible,
145+
setIsAnnouncementVisible,
146+
isRetrying,
147+
} = useChatStore(
148+
useShallow((store) => ({
149+
inputValue: store.inputValue,
150+
cursorPosition: store.cursorPosition,
151+
lastEditDueToNav: store.lastEditDueToNav,
146152
setInputValue: store.setInputValue,
147153
inputFocused: store.inputFocused,
148154
setInputFocused: store.setInputFocused,
@@ -169,13 +175,13 @@ export const Chat = ({
169175
setLastMessageMode: store.setLastMessageMode,
170176
addSessionCredits: store.addSessionCredits,
171177
resetChatStore: store.reset,
172-
sessionCreditsUsed: store.sessionCreditsUsed,
173-
setRunState: store.setRunState,
174-
isAnnouncementVisible: store.isAnnouncementVisible,
175-
setIsAnnouncementVisible: store.setIsAnnouncementVisible,
176-
isRetrying: store.isRetrying,
177-
})),
178-
)
178+
sessionCreditsUsed: store.sessionCreditsUsed,
179+
setRunState: store.setRunState,
180+
isAnnouncementVisible: store.isAnnouncementVisible,
181+
setIsAnnouncementVisible: store.setIsAnnouncementVisible,
182+
isRetrying: store.isRetrying,
183+
})),
184+
)
179185

180186
// Memoize toggle IDs extraction - only recompute when messages change
181187
const allToggleIds = useMemo(() => {
@@ -534,10 +540,7 @@ export const Chat = ({
534540
// Timer events are currently tracked but not used for UI updates
535541
// Future: Could be used for analytics or debugging
536542

537-
const {
538-
sendMessage,
539-
clearMessages,
540-
} = useSendMessage({
543+
const { sendMessage, clearMessages } = useSendMessage({
541544
messages,
542545
allToggleIds,
543546
setMessages,

evals/buffbench/agent-runner.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ export async function runAgentOnCommit({
112112
trace.push(event)
113113
},
114114
})
115-
cost = result.sessionState.mainAgentState.creditsUsed / 100
115+
cost = (result.sessionState?.mainAgentState.creditsUsed ?? 0) / 100
116116

117117
execSync('git add .', { cwd: repoDir, stdio: 'ignore' })
118118
diff = execSync(`git diff ${commit.parentSha}`, {

evals/git-evals/prompting-agent.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ Analyze the conversation and decide your next action.`
9393
},
9494
})
9595

96-
if (!result) {
96+
if (!result || !result.sessionState) {
9797
return {
9898
decision: 'halt',
9999
reasoning: `No valid response from prompting agent. Error:\n${lastErrorMessage}`,

evals/git-evals/runners/codebuff.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export class CodebuffRunner implements Runner {
3939

4040
const client = new CodebuffClient({
4141
apiKey,
42-
cwd: this.runState.sessionState.fileContext.cwd,
42+
cwd: this.runState.sessionState?.fileContext.cwd ?? process.cwd(),
4343
})
4444

4545
const agentsPath = path.join(__dirname, '../../../.agents')
@@ -103,7 +103,7 @@ export class CodebuffRunner implements Runner {
103103

104104
return {
105105
steps,
106-
totalCostUsd: this.runState.sessionState.mainAgentState.creditsUsed / 100,
106+
totalCostUsd: (this.runState.sessionState?.mainAgentState.creditsUsed ?? 0) / 100,
107107
}
108108
}
109109
}

sdk/src/__tests__/run-with-retry.test.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,9 @@ describe('run retry wrapper', () => {
6363

6464
// Should return error output after exhausting retries
6565
expect(result.output.type).toBe('error')
66-
expect(result.output.message).toContain('timeout')
66+
if (result.output.type === 'error') {
67+
expect(result.output.message).toContain('timeout')
68+
}
6769
// Initial attempt + one retry
6870
expect(runSpy).toHaveBeenCalledTimes(2)
6971
})
@@ -140,7 +142,9 @@ describe('run retry wrapper', () => {
140142
})
141143

142144
expect(result.output.type).toBe('error')
143-
expect(result.output.message).toContain('Aborted')
145+
if (result.output.type === 'error') {
146+
expect(result.output.message).toContain('Aborted')
147+
}
144148
expect(runSpy).toHaveBeenCalledTimes(0)
145149
})
146150

0 commit comments

Comments
 (0)