fix: better openai usage for big pickle
This commit is contained in:
@@ -268,6 +268,7 @@ export class OpenCodeManager {
|
||||
const toolCallsCollected: Array<{ name: string; args: unknown }> = [];
|
||||
|
||||
try {
|
||||
console.log('[OpenCodeManager] Sending to provider:', provider, 'model:', modelId);
|
||||
if (provider === 'anthropic') {
|
||||
const result = await this.sendAnthropicMessage(
|
||||
modelId,
|
||||
@@ -288,7 +289,9 @@ export class OpenCodeManager {
|
||||
);
|
||||
fullResponse = result.content;
|
||||
}
|
||||
console.log('[OpenCodeManager] fullResponse length:', fullResponse.length);
|
||||
} catch (error) {
|
||||
console.error('[OpenCodeManager] Request error:', (error as Error).message);
|
||||
const isAborted = abortController.signal.aborted || (error as Error).message === 'Request cancelled';
|
||||
if (!isAborted) {
|
||||
throw error;
|
||||
@@ -467,7 +470,7 @@ export class OpenCodeManager {
|
||||
callbacks: { onDelta?: (delta: string) => void }
|
||||
): Promise<{ content: string }> {
|
||||
// Build OpenAI-format messages
|
||||
const messages = [
|
||||
const messages: Array<Record<string, unknown>> = [
|
||||
{ role: 'system', content: systemPrompt },
|
||||
...dbMessages
|
||||
.filter(m => m.role === 'user' || m.role === 'assistant')
|
||||
@@ -488,93 +491,90 @@ export class OpenCodeManager {
|
||||
},
|
||||
}));
|
||||
|
||||
const body: Record<string, unknown> = {
|
||||
model: modelId,
|
||||
max_tokens: 4096,
|
||||
messages,
|
||||
tools: openaiTools,
|
||||
};
|
||||
let accumulatedText = '';
|
||||
const MAX_TOOL_ROUNDS = 10;
|
||||
let round = 0;
|
||||
|
||||
const response = await this.httpRequest(ZEN_OPENAI_URL, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Authorization': `Bearer ${this.apiKey}`,
|
||||
},
|
||||
body: JSON.stringify(body),
|
||||
signal,
|
||||
});
|
||||
while (round < MAX_TOOL_ROUNDS) {
|
||||
round++;
|
||||
|
||||
if (response.statusCode >= 400) {
|
||||
const errorMsg = this.parseErrorResponse(response);
|
||||
throw new Error(errorMsg);
|
||||
}
|
||||
|
||||
const data = JSON.parse(response.body);
|
||||
const choice = data.choices?.[0];
|
||||
|
||||
if (!choice?.message) {
|
||||
throw new Error('API response missing expected message content');
|
||||
}
|
||||
|
||||
// Handle tool calls in OpenAI format
|
||||
if (choice.message.tool_calls && choice.message.tool_calls.length > 0) {
|
||||
// Execute tools and do follow-up call
|
||||
const toolMessages = [
|
||||
...messages,
|
||||
choice.message,
|
||||
];
|
||||
|
||||
for (const toolCall of choice.message.tool_calls) {
|
||||
const toolName = toolCall.function.name;
|
||||
const toolArgs = JSON.parse(toolCall.function.arguments || '{}');
|
||||
const result = await this.executeTool(toolName, toolArgs);
|
||||
|
||||
toolMessages.push({
|
||||
role: 'tool',
|
||||
content: JSON.stringify(result),
|
||||
tool_call_id: toolCall.id,
|
||||
} as Record<string, unknown> as typeof messages[0]);
|
||||
}
|
||||
|
||||
// Make follow-up call with tool results
|
||||
const followUpBody: Record<string, unknown> = {
|
||||
const body: Record<string, unknown> = {
|
||||
model: modelId,
|
||||
max_tokens: 4096,
|
||||
messages: toolMessages,
|
||||
messages,
|
||||
tools: openaiTools,
|
||||
};
|
||||
|
||||
const followUpResponse = await this.httpRequest(ZEN_OPENAI_URL, {
|
||||
const response = await this.httpRequest(ZEN_OPENAI_URL, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Authorization': `Bearer ${this.apiKey}`,
|
||||
},
|
||||
body: JSON.stringify(followUpBody),
|
||||
body: JSON.stringify(body),
|
||||
signal,
|
||||
});
|
||||
|
||||
if (followUpResponse.statusCode >= 400) {
|
||||
throw new Error(this.parseErrorResponse(followUpResponse));
|
||||
if (response.statusCode >= 400) {
|
||||
const errorMsg = this.parseErrorResponse(response);
|
||||
throw new Error(errorMsg);
|
||||
}
|
||||
|
||||
const followUpData = JSON.parse(followUpResponse.body);
|
||||
const content = followUpData.choices?.[0]?.message?.content || '';
|
||||
const data = JSON.parse(response.body);
|
||||
const choice = data.choices?.[0];
|
||||
|
||||
if (callbacks.onDelta) {
|
||||
callbacks.onDelta(content);
|
||||
console.log('[OpenCodeManager:OpenAI] Round', round, 'status:', response.statusCode, 'content type:', typeof choice?.message?.content, 'content length:', choice?.message?.content?.length, 'tool_calls:', choice?.message?.tool_calls?.length);
|
||||
|
||||
if (!choice?.message) {
|
||||
throw new Error('API response missing expected message content');
|
||||
}
|
||||
|
||||
return { content };
|
||||
// Handle content that might be a string or an array of content parts
|
||||
let textContent = '';
|
||||
const content = choice.message.content;
|
||||
if (typeof content === 'string') {
|
||||
textContent = content;
|
||||
} else if (Array.isArray(content)) {
|
||||
// Handle array of content parts (some models return this format)
|
||||
textContent = content
|
||||
.filter((part: { type?: string; text?: string }) => part.type === 'text' && part.text)
|
||||
.map((part: { text: string }) => part.text)
|
||||
.join('');
|
||||
}
|
||||
|
||||
if (textContent) {
|
||||
accumulatedText += textContent;
|
||||
if (callbacks.onDelta) {
|
||||
callbacks.onDelta(textContent);
|
||||
}
|
||||
}
|
||||
|
||||
// If no tool calls, we're done
|
||||
if (!choice.message.tool_calls || choice.message.tool_calls.length === 0) {
|
||||
console.log('[OpenCodeManager:OpenAI] Done. Accumulated text length:', accumulatedText.length);
|
||||
return { content: accumulatedText };
|
||||
}
|
||||
|
||||
// Add assistant message (with tool_calls) to conversation
|
||||
messages.push(choice.message);
|
||||
|
||||
// Execute tool calls and add results
|
||||
for (const toolCall of choice.message.tool_calls) {
|
||||
const toolName = toolCall.function.name;
|
||||
const toolArgs = JSON.parse(toolCall.function.arguments || '{}');
|
||||
const result = await this.executeTool(toolName, toolArgs);
|
||||
|
||||
messages.push({
|
||||
role: 'tool',
|
||||
content: JSON.stringify(result),
|
||||
tool_call_id: toolCall.id,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
const content = choice.message.content || '';
|
||||
if (callbacks.onDelta) {
|
||||
callbacks.onDelta(content);
|
||||
}
|
||||
|
||||
return { content };
|
||||
// Hit max rounds
|
||||
const fallbackText = accumulatedText || 'I reached the maximum number of tool calls. Please try again.';
|
||||
return { content: fallbackText };
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -135,7 +135,8 @@ export const ChatPanel: React.FC<ChatPanelProps> = ({ conversationId }) => {
|
||||
const result = await window.electronAPI?.chat.sendMessage(conversationId, message);
|
||||
|
||||
// Use the streamed content we accumulated via onStreamDelta
|
||||
const assistantContent = streamingRef.current;
|
||||
// Fall back to the backend result message if streaming didn't capture the content
|
||||
const assistantContent = streamingRef.current || (result?.success ? result.message : '');
|
||||
|
||||
if (assistantContent) {
|
||||
const assistantMessage: ChatMessage = {
|
||||
@@ -156,6 +157,17 @@ export const ChatPanel: React.FC<ChatPanelProps> = ({ conversationId }) => {
|
||||
createdAt: new Date().toISOString()
|
||||
};
|
||||
setMessages(prev => [...prev, errorMessage]);
|
||||
} else {
|
||||
// No content from streaming AND no error, but also no success message
|
||||
// This can happen with some models that don't return content properly
|
||||
const noContentMessage: ChatMessage = {
|
||||
id: `empty-${Date.now()}`,
|
||||
conversationId,
|
||||
role: 'assistant',
|
||||
content: 'The model returned an empty response. Try a different model or rephrase your question.',
|
||||
createdAt: new Date().toISOString()
|
||||
};
|
||||
setMessages(prev => [...prev, noContentMessage]);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Failed to send message:', error);
|
||||
|
||||
@@ -750,7 +750,7 @@ const SettingsNav: React.FC = () => {
|
||||
|
||||
// Chat conversations list
|
||||
const ChatList: React.FC = () => {
|
||||
const { openTab } = useAppStore();
|
||||
const { openTab, closeTab } = useAppStore();
|
||||
const [conversations, setConversations] = useState<ChatConversation[]>([]);
|
||||
const [isLoading, setIsLoading] = useState(true);
|
||||
const [isReady, setIsReady] = useState(false);
|
||||
@@ -819,6 +819,8 @@ const ChatList: React.FC = () => {
|
||||
try {
|
||||
await window.electronAPI?.chat.deleteConversation(conversationId);
|
||||
setConversations(prev => prev.filter(c => c.id !== conversationId));
|
||||
// Close the tab for the deleted chat
|
||||
closeTab(conversationId);
|
||||
} catch (error) {
|
||||
console.error('Failed to delete conversation:', error);
|
||||
showToast.error('Failed to delete chat');
|
||||
|
||||
@@ -2,7 +2,14 @@ import React, { useRef, useState, useEffect, useCallback } from 'react';
|
||||
import { useAppStore, Tab } from '../../store';
|
||||
import './TabBar.css';
|
||||
|
||||
const getTabTitle = (tab: Tab, posts: { id: string; title: string }[], media: { id: string; originalName: string }[]): string => {
|
||||
const MAX_CHAT_TITLE_LENGTH = 25;
|
||||
|
||||
const getTabTitle = (
|
||||
tab: Tab,
|
||||
posts: { id: string; title: string }[],
|
||||
media: { id: string; originalName: string }[],
|
||||
chatTitles: Map<string, string>
|
||||
): string => {
|
||||
if (tab.type === 'settings') {
|
||||
return 'Settings';
|
||||
}
|
||||
@@ -21,6 +28,17 @@ const getTabTitle = (tab: Tab, posts: { id: string; title: string }[], media: {
|
||||
return mediaItem?.originalName || 'Media';
|
||||
}
|
||||
|
||||
if (tab.type === 'chat') {
|
||||
const title = chatTitles.get(tab.id);
|
||||
if (title && title !== 'New Chat') {
|
||||
// Truncate long titles for display
|
||||
return title.length > MAX_CHAT_TITLE_LENGTH
|
||||
? title.substring(0, MAX_CHAT_TITLE_LENGTH) + '…'
|
||||
: title;
|
||||
}
|
||||
return 'New Chat';
|
||||
}
|
||||
|
||||
return 'Unknown';
|
||||
};
|
||||
|
||||
@@ -50,6 +68,12 @@ const getTabIcon = (tab: Tab): React.ReactNode => {
|
||||
<path d="M14.28 7.72l-6-6A1 1 0 007.57 1.5H2.5A1 1 0 001.5 2.5v5.07a1 1 0 00.22.56l6 6a1 1 0 001.41 0l5.15-5a1 1 0 000-1.41zM4 5a1 1 0 110-2 1 1 0 010 2z"/>
|
||||
</svg>
|
||||
);
|
||||
case 'chat':
|
||||
return (
|
||||
<svg width="16" height="16" viewBox="0 0 16 16" fill="currentColor">
|
||||
<path d="M14 1H2a1 1 0 00-1 1v10a1 1 0 001 1h3v2.5l4-2.5h5a1 1 0 001-1V2a1 1 0 00-1-1zm0 11H8.5L5 14v-2H2V2h12v10z"/>
|
||||
</svg>
|
||||
);
|
||||
default:
|
||||
return (
|
||||
<svg width="16" height="16" viewBox="0 0 16 16" fill="currentColor">
|
||||
@@ -94,6 +118,52 @@ export const TabBar: React.FC = () => {
|
||||
const tabsContainerRef = useRef<HTMLDivElement>(null);
|
||||
const [showLeftArrow, setShowLeftArrow] = useState(false);
|
||||
const [showRightArrow, setShowRightArrow] = useState(false);
|
||||
const [chatTitles, setChatTitles] = useState<Map<string, string>>(new Map());
|
||||
|
||||
// Fetch chat titles for chat tabs
|
||||
useEffect(() => {
|
||||
const chatTabs = tabs.filter(t => t.type === 'chat');
|
||||
if (chatTabs.length === 0) return;
|
||||
|
||||
// Fetch titles for chat tabs that don't have a title yet
|
||||
const fetchTitles = async () => {
|
||||
const newTitles = new Map(chatTitles);
|
||||
|
||||
for (const tab of chatTabs) {
|
||||
if (!chatTitles.has(tab.id)) {
|
||||
try {
|
||||
const conversation = await window.electronAPI?.chat.getConversation(tab.id);
|
||||
if (conversation) {
|
||||
newTitles.set(tab.id, conversation.title);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Failed to fetch chat title:', error);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (newTitles.size !== chatTitles.size) {
|
||||
setChatTitles(newTitles);
|
||||
}
|
||||
};
|
||||
|
||||
fetchTitles();
|
||||
}, [tabs]); // Note: intentionally not including chatTitles to avoid infinite loops
|
||||
|
||||
// Listen for chat title updates
|
||||
useEffect(() => {
|
||||
const unsub = window.electronAPI?.chat.onTitleUpdated((data) => {
|
||||
setChatTitles(prev => {
|
||||
const newTitles = new Map(prev);
|
||||
newTitles.set(data.conversationId, data.title);
|
||||
return newTitles;
|
||||
});
|
||||
});
|
||||
|
||||
return () => {
|
||||
unsub?.();
|
||||
};
|
||||
}, []);
|
||||
|
||||
// Check if arrows are needed based on scroll position
|
||||
const updateArrowVisibility = useCallback(() => {
|
||||
@@ -229,7 +299,7 @@ export const TabBar: React.FC = () => {
|
||||
{tabs.map((tab) => {
|
||||
const isActive = tab.id === activeTabId;
|
||||
const isDirty = tab.type === 'post' && dirtyPosts.has(tab.id);
|
||||
const title = getTabTitle(tab, posts, media);
|
||||
const title = getTabTitle(tab, posts, media, chatTitles);
|
||||
const icon = getTabIcon(tab);
|
||||
|
||||
return (
|
||||
|
||||
Reference in New Issue
Block a user