603 lines
16 KiB
TypeScript
603 lines
16 KiB
TypeScript
import { ModelConfig } from "@continuedev/config-yaml";
|
|
import { BaseLlmApi } from "@continuedev/openai-adapters";
|
|
import type { ChatHistoryItem, ToolStatus } from "core/index.js";
|
|
import * as dotenv from "dotenv";
|
|
import type { ChatCompletionTool } from "openai/resources.mjs";
|
|
|
|
import {
|
|
convertFromUnifiedHistory,
|
|
createHistoryItem,
|
|
} from "./messageConversion.js";
|
|
import { filterExcludedTools } from "./permissions/index.js";
|
|
import {
|
|
getServiceSync,
|
|
MCPServiceState,
|
|
MCPTool,
|
|
SERVICE_NAMES,
|
|
} from "./services/index.js";
|
|
import type { ToolPermissionServiceState } from "./services/ToolPermissionService.js";
|
|
import { handleAutoCompaction } from "./streamChatResponse.autoCompaction.js";
|
|
import {
|
|
executeStreamedToolCalls,
|
|
preprocessStreamedToolCalls,
|
|
processChunkContent,
|
|
processToolCallDelta,
|
|
recordStreamTelemetry,
|
|
trackFirstTokenTime,
|
|
} from "./streamChatResponse.helpers.js";
|
|
import {
|
|
getDefaultCompletionOptions,
|
|
StreamCallbacks,
|
|
} from "./streamChatResponse.types.js";
|
|
import { telemetryService } from "./telemetry/telemetryService.js";
|
|
import { getAllBuiltinTools, ToolCall } from "./tools/index.js";
|
|
import {
|
|
chatCompletionStreamWithBackoff,
|
|
withExponentialBackoff,
|
|
} from "./util/exponentialBackoff.js";
|
|
import { logger } from "./util/logger.js";
|
|
|
|
dotenv.config();
|
|
|
|
function updateFinalResponse(
|
|
content: string,
|
|
shouldContinue: boolean,
|
|
isHeadless: boolean,
|
|
currentFinalResponse: string,
|
|
): string {
|
|
if (!shouldContinue) {
|
|
return content;
|
|
} else if (isHeadless && content) {
|
|
return content;
|
|
}
|
|
return currentFinalResponse;
|
|
}
|
|
|
|
function handleContentDisplay(
|
|
content: string,
|
|
callbacks: StreamCallbacks | undefined,
|
|
isHeadless: boolean,
|
|
): void {
|
|
// Add newline after content if needed
|
|
if (!callbacks?.onContent && !isHeadless && content) {
|
|
logger.info("");
|
|
}
|
|
|
|
// Notify content complete
|
|
if (content && callbacks?.onContentComplete) {
|
|
callbacks.onContentComplete(content);
|
|
}
|
|
}
|
|
|
|
async function handleToolCalls(
|
|
toolCalls: ToolCall[],
|
|
chatHistory: ChatHistoryItem[],
|
|
content: string,
|
|
callbacks: StreamCallbacks | undefined,
|
|
isHeadless: boolean,
|
|
): Promise<boolean> {
|
|
if (toolCalls.length === 0) {
|
|
if (content) {
|
|
chatHistory.push(
|
|
createHistoryItem({
|
|
role: "assistant",
|
|
content,
|
|
}),
|
|
);
|
|
}
|
|
return false;
|
|
}
|
|
|
|
// Create tool call states for the ChatHistoryItem
|
|
const toolCallStates = toolCalls.map((tc) => ({
|
|
toolCallId: tc.id,
|
|
toolCall: {
|
|
id: tc.id,
|
|
type: "function" as const,
|
|
function: {
|
|
name: tc.name,
|
|
arguments: JSON.stringify(tc.arguments),
|
|
},
|
|
},
|
|
status: "generated" as ToolStatus,
|
|
parsedArgs: tc.arguments,
|
|
}));
|
|
|
|
// Create assistant message with tool calls
|
|
const assistantMessage = {
|
|
role: "assistant" as const,
|
|
content: content || "",
|
|
toolCalls: toolCalls.map((tc) => ({
|
|
id: tc.id,
|
|
type: "function" as const,
|
|
function: {
|
|
name: tc.name,
|
|
arguments: JSON.stringify(tc.arguments),
|
|
},
|
|
})),
|
|
};
|
|
|
|
chatHistory.push(createHistoryItem(assistantMessage, [], toolCallStates));
|
|
|
|
// First preprocess the tool calls
|
|
const { preprocessedCalls, errorChatEntries } =
|
|
await preprocessStreamedToolCalls(toolCalls, callbacks);
|
|
|
|
// Add any preprocessing errors to chat history
|
|
// Convert error entries from OpenAI format to ChatHistoryItem format
|
|
errorChatEntries.forEach((errorEntry) => {
|
|
chatHistory.push(
|
|
createHistoryItem({
|
|
role: "assistant",
|
|
content: errorEntry.content || "",
|
|
}),
|
|
);
|
|
});
|
|
|
|
// Execute the valid preprocessed tool calls
|
|
const { chatHistoryEntries: toolResults, hasRejection } =
|
|
await executeStreamedToolCalls(preprocessedCalls, callbacks, isHeadless);
|
|
|
|
if (isHeadless && hasRejection) {
|
|
logger.debug(
|
|
"Tool call rejected in headless mode - returning current content",
|
|
);
|
|
return true; // Signal early return needed
|
|
}
|
|
|
|
// Convert tool results from OpenAI format to ChatHistoryItem format
|
|
// and add them to the chat history
|
|
toolResults.forEach((toolResult) => {
|
|
// Find the corresponding tool call state to update
|
|
const lastAssistantIndex = chatHistory.findLastIndex(
|
|
(item) => item.message.role === "assistant" && item.toolCallStates,
|
|
);
|
|
|
|
if (
|
|
lastAssistantIndex >= 0 &&
|
|
chatHistory[lastAssistantIndex].toolCallStates
|
|
) {
|
|
const toolState = chatHistory[lastAssistantIndex].toolCallStates.find(
|
|
(ts) => ts.toolCallId === toolResult.tool_call_id,
|
|
);
|
|
|
|
if (toolState) {
|
|
toolState.status = hasRejection ? "canceled" : "done";
|
|
toolState.output = [
|
|
{
|
|
content:
|
|
typeof toolResult.content === "string" ? toolResult.content : "",
|
|
name: `Tool Result`,
|
|
description: "Tool execution result",
|
|
},
|
|
];
|
|
}
|
|
}
|
|
});
|
|
return false;
|
|
}
|
|
|
|
export async function getAllTools() {
|
|
// Get all available tool names
|
|
const allBuiltinTools = getAllBuiltinTools();
|
|
const builtinToolNames = allBuiltinTools.map((tool) => tool.name);
|
|
|
|
let mcpTools: MCPTool[] = [];
|
|
let mcpToolNames: string[] = [];
|
|
const mcpServiceResult = getServiceSync<MCPServiceState>(SERVICE_NAMES.MCP);
|
|
if (mcpServiceResult.state === "ready") {
|
|
mcpTools = mcpServiceResult?.value?.tools ?? [];
|
|
mcpToolNames = mcpTools.map((t) => t.name);
|
|
} else {
|
|
// MCP is lazy
|
|
// throw new Error("MCP Service not initialized");
|
|
}
|
|
|
|
const allToolNames = [...builtinToolNames, ...mcpToolNames];
|
|
|
|
// Check if the ToolPermissionService is ready
|
|
const permissionsServiceResult = getServiceSync<ToolPermissionServiceState>(
|
|
SERVICE_NAMES.TOOL_PERMISSIONS,
|
|
);
|
|
|
|
let allowedToolNames: string[];
|
|
if (
|
|
permissionsServiceResult.state === "ready" &&
|
|
permissionsServiceResult.value
|
|
) {
|
|
// Filter out excluded tools based on permissions
|
|
allowedToolNames = filterExcludedTools(
|
|
allToolNames,
|
|
permissionsServiceResult.value.permissions,
|
|
);
|
|
} else {
|
|
// Service not ready - this is a critical error since tools should only be
|
|
// requested after services are properly initialized
|
|
logger.error(
|
|
"ToolPermissionService not ready in getAllTools - this indicates a service initialization timing issue",
|
|
);
|
|
throw new Error(
|
|
"ToolPermissionService not initialized. Services must be initialized before requesting tools.",
|
|
);
|
|
}
|
|
|
|
const allowedToolNamesSet = new Set(allowedToolNames);
|
|
|
|
// Filter builtin tools
|
|
const allowedBuiltinTools = allBuiltinTools.filter((tool) =>
|
|
allowedToolNamesSet.has(tool.name),
|
|
);
|
|
|
|
const allTools: ChatCompletionTool[] = allowedBuiltinTools.map((tool) => ({
|
|
type: "function" as const,
|
|
function: {
|
|
name: tool.name,
|
|
description: tool.description,
|
|
parameters: {
|
|
type: "object",
|
|
properties: Object.fromEntries(
|
|
Object.entries(tool.parameters).map(([key, param]) => [
|
|
key,
|
|
{
|
|
type: param.type,
|
|
description: param.description,
|
|
items: param.items,
|
|
},
|
|
]),
|
|
),
|
|
required: Object.entries(tool.parameters)
|
|
.filter(([_, param]) => param.required)
|
|
.map(([key, _]) => key),
|
|
},
|
|
},
|
|
}));
|
|
|
|
// Add filtered MCP tools
|
|
const allowedMcpTools = mcpTools.filter((tool) =>
|
|
allowedToolNamesSet.has(tool.name),
|
|
);
|
|
|
|
allTools.push(
|
|
...allowedMcpTools.map((tool) => ({
|
|
type: "function" as const,
|
|
function: {
|
|
name: tool.name,
|
|
description: tool.description,
|
|
parameters: tool.inputSchema,
|
|
},
|
|
})),
|
|
);
|
|
|
|
return allTools;
|
|
}
|
|
|
|
// Helper function to process a single chunk
|
|
interface ProcessChunkOptions {
|
|
chunk: any;
|
|
aiResponse: string;
|
|
toolCallsMap: Map<string, ToolCall>;
|
|
indexToIdMap: Map<number, string>;
|
|
callbacks?: StreamCallbacks;
|
|
isHeadless?: boolean;
|
|
}
|
|
|
|
function processChunk(options: ProcessChunkOptions): {
|
|
aiResponse: string;
|
|
shouldContinue: boolean;
|
|
} {
|
|
const {
|
|
chunk,
|
|
aiResponse,
|
|
toolCallsMap,
|
|
indexToIdMap,
|
|
callbacks,
|
|
isHeadless,
|
|
} = options;
|
|
// Safety check: ensure chunk has the expected structure
|
|
if (!chunk.choices || !chunk.choices[0]) {
|
|
return { aiResponse, shouldContinue: true };
|
|
}
|
|
|
|
const choice = chunk.choices[0];
|
|
if (!choice.delta) {
|
|
return { aiResponse, shouldContinue: true };
|
|
}
|
|
|
|
let updatedResponse = aiResponse;
|
|
|
|
// Handle content streaming
|
|
if (choice.delta.content) {
|
|
updatedResponse = processChunkContent(
|
|
choice.delta.content,
|
|
aiResponse,
|
|
callbacks,
|
|
isHeadless,
|
|
);
|
|
}
|
|
|
|
// Handle tool calls
|
|
if (choice.delta.tool_calls) {
|
|
for (const toolCallDelta of choice.delta.tool_calls) {
|
|
processToolCallDelta(toolCallDelta, toolCallsMap, indexToIdMap);
|
|
}
|
|
}
|
|
|
|
return { aiResponse: updatedResponse, shouldContinue: true };
|
|
}
|
|
|
|
interface ProcessStreamingResponseOptions {
|
|
chatHistory: ChatHistoryItem[];
|
|
model: ModelConfig;
|
|
llmApi: BaseLlmApi;
|
|
abortController: AbortController;
|
|
callbacks?: StreamCallbacks;
|
|
isHeadless?: boolean;
|
|
tools?: ChatCompletionTool[];
|
|
}
|
|
|
|
// Process a single streaming response and return whether we need to continue
|
|
export async function processStreamingResponse(
|
|
options: ProcessStreamingResponseOptions,
|
|
): Promise<{
|
|
content: string;
|
|
finalContent: string; // Added field for final content only
|
|
toolCalls: ToolCall[];
|
|
shouldContinue: boolean;
|
|
}> {
|
|
const {
|
|
chatHistory,
|
|
model,
|
|
llmApi,
|
|
abortController,
|
|
callbacks,
|
|
isHeadless,
|
|
tools,
|
|
} = options;
|
|
const openaiChatHistory = convertFromUnifiedHistory(chatHistory);
|
|
const requestStartTime = Date.now();
|
|
|
|
const streamFactory = async (retryAbortSignal: AbortSignal) => {
|
|
logger.debug("Creating chat completion stream", {
|
|
model,
|
|
messageCount: chatHistory.length,
|
|
toolCount: tools?.length || 0,
|
|
});
|
|
return await chatCompletionStreamWithBackoff(
|
|
llmApi,
|
|
{
|
|
model: model.model,
|
|
messages: openaiChatHistory,
|
|
stream: true,
|
|
tools,
|
|
...getDefaultCompletionOptions(model.defaultCompletionOptions),
|
|
},
|
|
retryAbortSignal,
|
|
);
|
|
};
|
|
|
|
let aiResponse = "";
|
|
let finalContent = "";
|
|
const toolCallsMap = new Map<string, ToolCall>();
|
|
const indexToIdMap = new Map<number, string>(); // Track index to ID mapping
|
|
let firstTokenTime: number | null = null;
|
|
let inputTokens = 0;
|
|
let outputTokens = 0;
|
|
|
|
try {
|
|
const streamWithBackoff = withExponentialBackoff(
|
|
streamFactory,
|
|
abortController.signal,
|
|
);
|
|
|
|
let chunkCount = 0;
|
|
for await (const chunk of streamWithBackoff) {
|
|
chunkCount++;
|
|
|
|
logger.debug("Received chunk", { chunkCount, chunk });
|
|
|
|
// Track token usage if available
|
|
if (chunk.usage) {
|
|
inputTokens = chunk.usage.prompt_tokens || 0;
|
|
outputTokens = chunk.usage.completion_tokens || 0;
|
|
}
|
|
|
|
// Check if we should abort
|
|
if (abortController?.signal.aborted) {
|
|
logger.debug("Stream aborted");
|
|
break;
|
|
}
|
|
|
|
// Track first token time
|
|
firstTokenTime = trackFirstTokenTime(
|
|
firstTokenTime,
|
|
chunk,
|
|
requestStartTime,
|
|
model,
|
|
tools,
|
|
);
|
|
|
|
const result = processChunk({
|
|
chunk,
|
|
aiResponse,
|
|
toolCallsMap,
|
|
indexToIdMap,
|
|
callbacks,
|
|
isHeadless,
|
|
});
|
|
aiResponse = result.aiResponse;
|
|
if (!result.shouldContinue) break;
|
|
}
|
|
|
|
const responseEndTime = Date.now();
|
|
const cost = recordStreamTelemetry({
|
|
requestStartTime,
|
|
responseEndTime,
|
|
inputTokens,
|
|
outputTokens,
|
|
model,
|
|
tools,
|
|
});
|
|
const totalDuration = responseEndTime - requestStartTime;
|
|
|
|
logger.debug("Stream complete", {
|
|
chunkCount,
|
|
responseLength: aiResponse.length,
|
|
toolCallsCount: toolCallsMap.size,
|
|
inputTokens,
|
|
outputTokens,
|
|
cost,
|
|
duration: totalDuration,
|
|
});
|
|
} catch (error: any) {
|
|
const errorDuration = Date.now() - requestStartTime;
|
|
|
|
// Log failed API request
|
|
telemetryService.logApiRequest({
|
|
model: model.model,
|
|
durationMs: errorDuration,
|
|
success: false,
|
|
error: error.message || String(error),
|
|
});
|
|
|
|
if (error.name === "AbortError" || abortController?.signal.aborted) {
|
|
logger.debug("Stream aborted by user");
|
|
return {
|
|
content: aiResponse,
|
|
finalContent: aiResponse,
|
|
toolCalls: [],
|
|
shouldContinue: false,
|
|
};
|
|
}
|
|
throw error;
|
|
}
|
|
|
|
const toolCalls = Array.from(toolCallsMap.values());
|
|
|
|
// Validate tool calls have complete arguments
|
|
const validToolCalls = toolCalls.filter((tc) => {
|
|
if (!tc.arguments || !tc.name) {
|
|
logger.error("Incomplete tool call", {
|
|
id: tc.id,
|
|
name: tc.name,
|
|
hasArguments: !!tc.arguments,
|
|
argumentsStr: tc.argumentsStr,
|
|
});
|
|
return false;
|
|
}
|
|
return true;
|
|
});
|
|
|
|
// Always preserve the content - it should be displayed regardless of tool calls
|
|
finalContent = aiResponse;
|
|
|
|
return {
|
|
content: aiResponse,
|
|
finalContent: finalContent,
|
|
toolCalls: validToolCalls,
|
|
shouldContinue: validToolCalls.length > 0,
|
|
};
|
|
}
|
|
|
|
// Main function that handles the conversation loop
|
|
export async function streamChatResponse(
|
|
chatHistory: ChatHistoryItem[],
|
|
model: ModelConfig,
|
|
llmApi: BaseLlmApi,
|
|
abortController: AbortController,
|
|
callbacks?: StreamCallbacks,
|
|
) {
|
|
logger.debug("streamChatResponse called", {
|
|
model,
|
|
historyLength: chatHistory.length,
|
|
hasCallbacks: !!callbacks,
|
|
});
|
|
|
|
const serviceResult = getServiceSync<ToolPermissionServiceState>(
|
|
SERVICE_NAMES.TOOL_PERMISSIONS,
|
|
);
|
|
const isHeadless = serviceResult.value?.isHeadless ?? false;
|
|
|
|
let fullResponse = "";
|
|
let finalResponse = "";
|
|
|
|
while (true) {
|
|
logger.debug("Starting conversation iteration");
|
|
|
|
// Recompute tools on each iteration to handle mode changes during streaming
|
|
const tools = await getAllTools();
|
|
|
|
logger.debug("Tools prepared", {
|
|
toolCount: tools.length,
|
|
toolNames: tools.map((t) => t.function.name),
|
|
});
|
|
|
|
// Get response from LLM
|
|
const { content, toolCalls, shouldContinue } =
|
|
await processStreamingResponse({
|
|
chatHistory,
|
|
model,
|
|
llmApi,
|
|
abortController,
|
|
callbacks,
|
|
isHeadless,
|
|
tools,
|
|
});
|
|
|
|
fullResponse += content;
|
|
|
|
// Update final response based on mode
|
|
finalResponse = updateFinalResponse(
|
|
content,
|
|
shouldContinue,
|
|
isHeadless,
|
|
finalResponse,
|
|
);
|
|
|
|
// Handle content display
|
|
handleContentDisplay(content, callbacks, isHeadless);
|
|
|
|
// Handle tool calls and check for early return
|
|
const shouldReturn = await handleToolCalls(
|
|
toolCalls,
|
|
chatHistory,
|
|
content,
|
|
callbacks,
|
|
isHeadless,
|
|
);
|
|
|
|
if (shouldReturn) {
|
|
return finalResponse || content || fullResponse;
|
|
}
|
|
|
|
// Check for auto-compaction after tool execution
|
|
if (shouldContinue) {
|
|
const { wasCompacted, chatHistory: updatedChatHistory } =
|
|
await handleAutoCompaction(chatHistory, model, llmApi, {
|
|
isHeadless,
|
|
callbacks: {
|
|
onSystemMessage: callbacks?.onSystemMessage,
|
|
onContent: callbacks?.onContent,
|
|
},
|
|
});
|
|
|
|
// Only update chat history if compaction actually occurred
|
|
if (wasCompacted) {
|
|
chatHistory = [...updatedChatHistory];
|
|
}
|
|
}
|
|
|
|
// Check if we should continue
|
|
if (!shouldContinue) {
|
|
break;
|
|
}
|
|
}
|
|
|
|
logger.debug("streamChatResponse complete", {
|
|
totalResponseLength: fullResponse.length,
|
|
totalMessages: chatHistory.length,
|
|
});
|
|
|
|
// For headless mode, we return only the final response
|
|
// Otherwise, return the full response
|
|
return isHeadless ? finalResponse : fullResponse;
|
|
}
|