Skip to content

armgit / jan

Public Imported from GitHub
0

Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.

feat(progress): show token counts + ETA on prompt reading indicator

Akarshan Biswas02465e33a7bd
diff --git a/web-app/src/components/PromptProgress.tsx b/web-app/src/components/PromptProgress.tsx
index 011538662..88a1b588d 100644
--- a/web-app/src/components/PromptProgress.tsx
+++ b/web-app/src/components/PromptProgress.tsx
@@ -1,6 +1,7 @@
import { useAppState } from '@/hooks/useAppState'
import { Loader } from 'lucide-react'
import { useParams } from '@tanstack/react-router'
+import { Progress } from '@/components/ui/progress'
export function PromptProgress() {
const params = useParams({ strict: false })
@@ -30,10 +31,58 @@ export function PromptProgress() {
? `Reading: ${percentage}%`
: 'Working…'
+ const detail =
+ showReading && !loadingModel ? buildDetail(promptProgress) : undefined
+
return (
- <div className="flex items-center gap-2 text-sm text-muted-foreground">
- <Loader className="animate-spin w-4 h-4" />
- <span>{label}</span>
+ <div className="inline-flex flex-col gap-2 rounded-lg border border-border/40 bg-muted/30 px-3 py-2 min-w-56">
+ <div className="flex items-center gap-2 text-sm">
+ <Loader className="animate-spin w-3.5 h-3.5 text-primary shrink-0" />
+ <span className="font-medium text-foreground">{label}</span>
+ </div>
+ {showReading && (
+ <Progress value={percentage} className="h-1 bg-secondary/60" />
+ )}
+ {detail && (
+ <div className="text-xs text-muted-foreground tabular-nums">
+ {detail}
+ </div>
+ )}
</div>
)
}
+
+function buildDetail(progress: {
+ processed: number
+ total: number
+ time_ms: number
+}): string {
+ const parts = [
+ `${formatTokens(progress.processed)} / ${formatTokens(progress.total)} tokens`,
+ ]
+
+ // Extrapolate remaining time from the processing rate so far.
+ if (progress.time_ms > 0 && progress.processed > 0) {
+ const remaining = progress.total - progress.processed
+ const etaMs = (progress.time_ms / progress.processed) * remaining
+ if (remaining > 0) parts.push(`~${formatEta(etaMs)} left`)
+ }
+
+ return parts.join(' · ')
+}
+
+function formatTokens(n: number): string {
+ if (n >= 1000) {
+ const k = n / 1000
+ return `${k >= 10 ? Math.round(k) : k.toFixed(1)}k`
+ }
+ return `${n}`
+}
+
+function formatEta(ms: number): string {
+ const seconds = Math.ceil(ms / 1000)
+ if (seconds < 60) return `${seconds}s`
+ const minutes = Math.floor(seconds / 60)
+ const rem = seconds % 60
+ return rem > 0 ? `${minutes}m ${rem}s` : `${minutes}m`
+}
diff --git a/web-app/src/components/__tests__/PromptProgress.test.tsx b/web-app/src/components/__tests__/PromptProgress.test.tsx
index e2a00b2f1..d103e05c9 100644
--- a/web-app/src/components/__tests__/PromptProgress.test.tsx
+++ b/web-app/src/components/__tests__/PromptProgress.test.tsx
@@ -36,6 +36,27 @@ describe('PromptProgress', () => {
expect(screen.getByText('Reading: 50%')).toBeInTheDocument()
})
+ it('should show token counts and ETA while reading', () => {
+ const mockProgress = {
+ cache: 0,
+ processed: 1200,
+ time_ms: 3000,
+ total: 2600,
+ }
+
+ mockUseAppState.mockImplementation((selector) =>
+ selector({ promptProgress: mockProgress, loadingModel: false })
+ )
+
+ render(<PromptProgress />)
+
+ expect(screen.getByText('Reading: 46%')).toBeInTheDocument()
+ // 1200/2600 tokens, ETA = (3000/1200)*1400 = 3500ms -> 4s
+ expect(
+ screen.getByText('1.2k / 2.6k tokens · ~4s left')
+ ).toBeInTheDocument()
+ })
+
it('should handle zero total gracefully', () => {
const mockProgress = {
cache: 0,
diff --git a/web-app/src/containers/MessageItem.tsx b/web-app/src/containers/MessageItem.tsx
index 2d14877e7..34a484043 100644
--- a/web-app/src/containers/MessageItem.tsx
+++ b/web-app/src/containers/MessageItem.tsx
@@ -38,6 +38,7 @@ import { useMemo } from 'react'
import { Button } from '@/components/ui/button'
import { PromptProgress } from '@/components/PromptProgress'
import { useServiceHub } from '@/hooks/useServiceHub'
+import { useToolApproval } from '@/hooks/useToolApproval'
import { parseCitationsFromToolOutput } from '@/lib/citation-parser'
import type { RagCitation } from '@/components/Citations'
import { useGroundingStore } from '@/stores/grounding-store'
@@ -147,6 +148,15 @@ export const MessageItem = memo(
})
}, [isLastMessage, message.role, message.parts])
+ const pendingApprovals = useToolApproval((s) => s.pending)
+ const awaitingApproval = useMemo(() => {
+ if (!hasPendingToolCall) return false
+ return message.parts.some((part) => {
+ const toolCallId = (part as { toolCallId?: string }).toolCallId
+ return Boolean(toolCallId && pendingApprovals[toolCallId])
+ })
+ }, [hasPendingToolCall, message.parts, pendingApprovals])
+
const isStreaming =
(isLastMessage &&
(status === CHAT_STATUS.STREAMING ||
@@ -613,6 +623,7 @@ export const MessageItem = memo(
{isLastMessage &&
message.role === 'assistant' &&
+ !awaitingApproval &&
(hasPendingToolCall || status === CHAT_STATUS.SUBMITTED) && (
<PromptProgress />
)}
diff --git a/web-app/src/containers/__tests__/MessageItem.test.tsx b/web-app/src/containers/__tests__/MessageItem.test.tsx
index 51faabedc..793982a4d 100644
--- a/web-app/src/containers/__tests__/MessageItem.test.tsx
+++ b/web-app/src/containers/__tests__/MessageItem.test.tsx
@@ -100,6 +100,16 @@ vi.mock('@/components/ui/button', () => ({
),
}))
+vi.mock('@/components/PromptProgress', () => ({
+ PromptProgress: () => <div data-testid="prompt-progress" />,
+}))
+
+const pendingApprovalsRef = vi.hoisted(() => ({ current: {} as any }))
+vi.mock('@/hooks/useToolApproval', () => ({
+ useToolApproval: (selector: any) =>
+ selector({ pending: pendingApprovalsRef.current }),
+}))
+
// Import after mocks
import { MessageItem } from '../MessageItem'
@@ -115,6 +125,7 @@ describe('MessageItem', () => {
beforeEach(() => {
vi.clearAllMocks()
selectedModelRef.current = { id: 'm1' }
+ pendingApprovalsRef.current = {}
})
it('renders assistant text via RenderMarkdown', () => {
@@ -347,6 +358,43 @@ describe('MessageItem', () => {
expect(screen.getByTestId('tool-output')).toHaveTextContent('boom')
})
+ it('shows progress for an executing tool call (not awaiting approval)', () => {
+ render(
+ <MessageItem
+ message={
+ makeMsg({
+ parts: [
+ { type: 'tool-search', state: 'input-available', toolCallId: 'tc1', input: {} },
+ ],
+ }) as any
+ }
+ isFirstMessage
+ isLastMessage
+ status={'ready' as any}
+ />
+ )
+ expect(screen.getByTestId('prompt-progress')).toBeInTheDocument()
+ })
+
+ it('hides progress while a tool call awaits approval', () => {
+ pendingApprovalsRef.current = { tc1: {} }
+ render(
+ <MessageItem
+ message={
+ makeMsg({
+ parts: [
+ { type: 'tool-search', state: 'input-available', toolCallId: 'tc1', input: {} },
+ ],
+ }) as any
+ }
+ isFirstMessage
+ isLastMessage
+ status={'ready' as any}
+ />
+ )
+ expect(screen.queryByTestId('prompt-progress')).not.toBeInTheDocument()
+ })
+
it('passes full text to copy button', () => {
render(
<MessageItem