Skip to content

Commit 1079b74

Browse files
committed
AI Chat improvements #951
1 parent 5c1891d commit 1079b74

File tree

5 files changed

+41
-48
lines changed

5 files changed

+41
-48
lines changed

browser/data-browser/src/components/AI/AISettings.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ const AISettings: React.FC = () => {
2121
setOpenRouterApiKey,
2222
mcpServers,
2323
setMcpServers,
24+
showTokenUsage,
25+
setShowTokenUsage,
2426
} = useSettings();
2527

2628
const [creditUsage, setCreditUsage] = useState<CreditUsage | undefined>();
@@ -71,6 +73,10 @@ const AISettings: React.FC = () => {
7173
)}
7274
</Column>
7375
</label>
76+
<CheckboxLabel>
77+
<Checkbox checked={showTokenUsage} onChange={setShowTokenUsage} />
78+
Show token usage in chats
79+
</CheckboxLabel>
7480
<Heading>MCP Servers</Heading>
7581
<MCPServersManager servers={mcpServers} setServers={setMcpServers} />
7682
</ConditionalSettings>

browser/data-browser/src/components/AI/SimpleAIChat.tsx

Lines changed: 8 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@ import toast from 'react-hot-toast';
55
import { useAtomicMCPTools } from './useAtomicTools';
66
import { AIChatMessage } from './AIChatMessage';
77
import {
8-
generateObject,
98
InvalidToolArgumentsError,
10-
NoSuchToolError,
119
streamText,
1210
TypeValidationError,
1311
type CoreMessage,
@@ -112,7 +110,7 @@ export const SimpleAIChat: React.FC<
112110
const [userSelectedContextItems, setUserSelectedContextItems] = useState<
113111
AIMessageContext[]
114112
>([]);
115-
const { openRouterApiKey } = useSettings() as { openRouterApiKey?: string };
113+
const { openRouterApiKey, showTokenUsage } = useSettings();
116114
const openrouter = createOpenRouter({
117115
apiKey: openRouterApiKey,
118116
compatibility: 'strict',
@@ -287,38 +285,6 @@ export const SimpleAIChat: React.FC<
287285
});
288286
}
289287
},
290-
experimental_repairToolCall: async ({
291-
toolCall,
292-
parameterSchema,
293-
error,
294-
}) => {
295-
if (NoSuchToolError.isInstance(error)) {
296-
console.log('No such tool error', error);
297-
298-
return null; // do not attempt to fix invalid tool names
299-
}
300-
301-
console.log('arg error', error);
302-
console.log('Repairing tool call', toolCall.toolName);
303-
304-
const { object: repairedArgs } = await generateObject({
305-
model: openrouter('qwen/qwq-32b:free'),
306-
output: 'no-schema',
307-
mode: 'json',
308-
prompt: [
309-
`The model tried to call the tool "${toolCall.toolName}"` +
310-
` with the following arguments:`,
311-
JSON.stringify(toolCall.args),
312-
`The tool accepts the following schema:`,
313-
JSON.stringify(parameterSchema(toolCall)),
314-
'Please fix the arguments.',
315-
].join('\n'),
316-
});
317-
318-
console.log('Repaired tool call', repairedArgs);
319-
320-
return { ...toolCall, args: JSON.stringify(repairedArgs) };
321-
},
322288
});
323289
} catch (err) {
324290
console.error(err);
@@ -354,7 +320,6 @@ export const SimpleAIChat: React.FC<
354320
try {
355321
for await (const part of textStream.fullStream) {
356322
// Update ongoing message with streamed chunks
357-
console.log('Part', part);
358323

359324
if (part.type === 'tool-call') {
360325
const toolCallMessage: AIChatDisplayMessage = {
@@ -379,13 +344,15 @@ export const SimpleAIChat: React.FC<
379344
pendingToolCalls.push(part);
380345
}
381346

347+
// @ts-expect-error 'tool-result' is not in the types but there are definitely parts that have this type, removing this would break tool use.
382348
if (part.type === 'tool-result') {
383349
onNewMessage({
384350
role: 'tool',
385351
content: [part],
386352
});
387353

388354
pendingToolCalls = pendingToolCalls.filter(
355+
// @ts-expect-error part has type never do to incorrect types.
389356
call => call.toolCallId !== part.toolCallId,
390357
);
391358
}
@@ -672,14 +639,14 @@ export const SimpleAIChat: React.FC<
672639
</Column>
673640
<NoKeyOverlay />
674641
</ChatInputWrapper>
675-
<TokensUsed>
676-
Tokens used: {tokensUsed[0]} input, {tokensUsed[1]} output
677-
</TokensUsed>
642+
{showTokenUsage && (
643+
<TokensUsed>
644+
Tokens used: {tokensUsed[0]} input, {tokensUsed[1]} output
645+
</TokensUsed>
646+
)}
678647
</>
679648
)}
680649
</Column>
681-
682-
{/* Agent configuration dialog */}
683650
<AgentConfig
684651
open={agentConfigOpen}
685652
onOpenChange={setAgentConfigOpen}

browser/data-browser/src/components/IconButton/IconButton.tsx

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
} from 'react';
66
import { styled, DefaultTheme, keyframes } from 'styled-components';
77
import { transition } from '../../helpers/transition';
8+
import { adjustHue } from 'polished';
89

910
export enum IconButtonVariant {
1011
Simple,
@@ -197,7 +198,7 @@ const cycle = keyframes`
197198
filter: hue-rotate(0deg) brightness(var(--brightness-factor)) saturate(1.5);
198199
}
199200
to {
200-
filter: hue-rotate(360deg) brightness(var(--brightness-factor)) saturate(1.5);
201+
filter: hue-rotate(360deg) brightness(var(--brightness-factor)) saturate(2);
201202
}
202203
`;
203204

@@ -225,11 +226,17 @@ const MagicIconButton = styled(IconButtonBase)<ButtonStyleProps>`
225226
inset: 0;
226227
opacity: 0;
227228
z-index: -2;
228-
background: linear-gradient(
229-
45deg,
230-
${p => p.theme.colors.main},
231-
${p => p.theme.colors.complementary}
232-
);
229+
background: radial-gradient(ellipse at top right, #365ccd, transparent),
230+
radial-gradient(
231+
ellipse at bottom left,
232+
${adjustHue(-45, '#365ccd')},
233+
transparent
234+
),
235+
radial-gradient(
236+
ellipse at bottom right,
237+
${adjustHue(180, '#365ccd')},
238+
transparent
239+
);
233240
}
234241
&::after {
235242
content: '';

browser/data-browser/src/components/MCPServersManager.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ export const MCPServersManager: React.FC<MCPServersManagerProps> = ({
6464
)}
6565
</ServerList>
6666
<Column gap='0.5rem'>
67+
<h4>Add Server</h4>
6768
<Row gap='1rem' center>
6869
<Column gap='0.5rem' style={{ flex: 1 }}>
6970
<label htmlFor='server-name'>Server Name</label>
@@ -107,7 +108,6 @@ export const MCPServersManager: React.FC<MCPServersManagerProps> = ({
107108
);
108109
};
109110

110-
// Styled components for MCP servers UI
111111
const ServerList = styled.div`
112112
display: flex;
113113
flex-direction: column;

browser/data-browser/src/helpers/AppSettings.tsx

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ export const AppSettingsContextProvider = (
5959
const [drive, innerSetDrive] = useLocalStorage('drive', baseURL);
6060

6161
const [enableAI, setEnableAI] = useLocalStorage('atomic.ai.enabled', true);
62+
const [showTokenUsage, setShowTokenUsage] = useLocalStorage(
63+
'atomic.ai.showTokenUsage',
64+
true,
65+
);
6266

6367
const setDrive = useCallback(
6468
(newDrive: string) => {
@@ -112,6 +116,8 @@ export const AppSettingsContextProvider = (
112116
setMcpServers,
113117
enableAI,
114118
setEnableAI,
119+
showTokenUsage,
120+
setShowTokenUsage,
115121
}),
116122
[
117123
drive,
@@ -141,6 +147,8 @@ export const AppSettingsContextProvider = (
141147
setMcpServers,
142148
enableAI,
143149
setEnableAI,
150+
showTokenUsage,
151+
setShowTokenUsage,
144152
],
145153
);
146154

@@ -195,6 +203,9 @@ export interface AppSettings {
195203
/** Enable all AI features in the app */
196204
enableAI: boolean;
197205
setEnableAI: (b: boolean) => void;
206+
/** Whether to show the token usage in AI chats */
207+
showTokenUsage: boolean;
208+
setShowTokenUsage: (b: boolean) => void;
198209
}
199210

200211
const initialState: AppSettings = {
@@ -225,6 +236,8 @@ const initialState: AppSettings = {
225236
setMcpServers: () => undefined,
226237
enableAI: true,
227238
setEnableAI: () => undefined,
239+
showTokenUsage: true,
240+
setShowTokenUsage: () => undefined,
228241
};
229242

230243
/** Hook for using App Settings, such as theme and darkmode */

0 commit comments

Comments
 (0)