MCPcopy Create free account
hub / github.com/ephraimduncan/opencode-cursor / parseMessages

Function parseMessages

src/proxy.ts:528–575  ·  view source on GitHub ↗
(messages: OpenAIMessage[])

Source from the content-addressed store, hash-verified

526}
527
528function parseMessages(messages: OpenAIMessage[]): ParsedMessages {
529 let systemPrompt = "You are a helpful assistant.";
530 const pairs: Array<{ userText: string; assistantText: string }> = [];
531 const toolResults: ToolResultInfo[] = [];
532
533 // Collect system messages
534 const systemParts = messages
535 .filter((m) => m.role === "system")
536 .map((m) => textContent(m.content));
537 if (systemParts.length > 0) {
538 systemPrompt = systemParts.join("\n");
539 }
540
541 // Separate tool results from conversation turns
542 const nonSystem = messages.filter((m) => m.role !== "system");
543 let pendingUser = "";
544
545 for (const msg of nonSystem) {
546 if (msg.role === "tool") {
547 toolResults.push({
548 toolCallId: msg.tool_call_id ?? "",
549 content: textContent(msg.content),
550 });
551 } else if (msg.role === "user") {
552 if (pendingUser) {
553 pairs.push({ userText: pendingUser, assistantText: "" });
554 }
555 pendingUser = textContent(msg.content);
556 } else if (msg.role === "assistant") {
557 // Skip assistant messages that are just tool_calls with no text
558 const text = textContent(msg.content);
559 if (pendingUser) {
560 pairs.push({ userText: pendingUser, assistantText: text });
561 pendingUser = "";
562 }
563 }
564 }
565
566 let lastUserText = "";
567 if (pendingUser) {
568 lastUserText = pendingUser;
569 } else if (pairs.length > 0 && toolResults.length === 0) {
570 const last = pairs.pop()!;
571 lastUserText = last.userText;
572 }
573
574 return { systemPrompt, userText: lastUserText, turns: pairs, toolResults };
575}
576
577/** Convert OpenAI tool definitions to Cursor's MCP tool protobuf format. */
578function buildMcpToolDefinitions(tools: OpenAIToolDef[]): McpToolDefinition[] {

Callers 1

handleChatCompletionFunction · 0.85

Calls 1

textContentFunction · 0.85

Tested by

no test coverage detected