@jeffreycao/copilot-api 1.6.3 → 1.6.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/main.js CHANGED
@@ -23,7 +23,7 @@ if (typeof args["enterprise-url"] === "string") process.env.COPILOT_API_ENTERPRI
23
23
  const { auth } = await import("./auth-I-jV5RwF.js");
24
24
  const { checkUsage } = await import("./check-usage-DYLFqYhN.js");
25
25
  const { debug } = await import("./debug-DcC7ZPH0.js");
26
- const { start } = await import("./start-DzdGfvGi.js");
26
+ const { start } = await import("./start-BSKFZL9O.js");
27
27
  const main = defineCommand({
28
28
  meta: {
29
29
  name: "copilot-api",
@@ -1958,6 +1958,7 @@ const TOOL_REFERENCE_TURN_BOUNDARY = "Tool loaded.";
1958
1958
  const IDE_EXECUTE_CODE_TOOL = "mcp__ide__executeCode";
1959
1959
  const IDE_GET_DIAGNOSTICS_TOOL = "mcp__ide__getDiagnostics";
1960
1960
  const IDE_GET_DIAGNOSTICS_DESCRIPTION = "Get language diagnostics from VS Code. Returns errors, warnings, information, and hints for files in the workspace.";
1961
+ const PDF_FILE_READ_PREFIX = "PDF file read:";
1961
1962
  const getCompactCandidateText = (message) => {
1962
1963
  if (message.role !== "user") return "";
1963
1964
  if (typeof message.content === "string") return message.content;
@@ -2023,31 +2024,109 @@ const mergeContentWithAttachments = (tr, attachments) => {
2023
2024
  const isAttachmentBlock = (block) => {
2024
2025
  return block.type === "image" || block.type === "document";
2025
2026
  };
2026
- const mergeAttachmentsIntoLastToolResult = (content) => {
2027
- const attachments = content.filter((block) => isAttachmentBlock(block));
2028
- if (attachments.length === 0) return content;
2029
- const mergeableToolResultIndices = content.flatMap((block, index) => block.type === "tool_result" && !hasToolRef(block) ? [index] : []);
2030
- if (mergeableToolResultIndices.length === 0) return content;
2031
- const attachmentsByToolResultIndex = /* @__PURE__ */ new Map();
2032
- if (mergeableToolResultIndices.length === attachments.length) for (const [index, toolResultIndex] of mergeableToolResultIndices.entries()) attachmentsByToolResultIndex.set(toolResultIndex, [attachments[index]]);
2033
- else {
2034
- const lastToolResultIndex = mergeableToolResultIndices.at(-1);
2035
- if (lastToolResultIndex === void 0) return content;
2036
- attachmentsByToolResultIndex.set(lastToolResultIndex, attachments);
2037
- }
2038
- const mergedContent = [];
2039
- for (const [index, block] of content.entries()) {
2040
- if (isAttachmentBlock(block)) continue;
2041
- if (block.type === "tool_result") {
2042
- const matchedAttachments = attachmentsByToolResultIndex.get(index);
2043
- if (matchedAttachments) {
2044
- mergedContent.push(mergeContentWithAttachments(block, matchedAttachments));
2027
+ const getMergeableToolResultIndices = (toolResults) => {
2028
+ return toolResults.flatMap((block, index) => block.is_error || hasToolRef(block) ? [] : [index]);
2029
+ };
2030
+ const mergeAttachmentsIntoToolResults = (toolResults, attachmentsByToolResultIndex) => {
2031
+ if (attachmentsByToolResultIndex.size === 0) return toolResults;
2032
+ return toolResults.map((block, index) => {
2033
+ const matchedAttachments = attachmentsByToolResultIndex.get(index);
2034
+ if (!matchedAttachments) return block;
2035
+ const orderedAttachments = [...matchedAttachments].sort((left, right) => left.order - right.order).map(({ attachment }) => attachment);
2036
+ return mergeContentWithAttachments(block, orderedAttachments);
2037
+ });
2038
+ };
2039
+ const assignAttachmentsToToolResults = (target, attachments, options) => {
2040
+ const { toolResultIndices } = options;
2041
+ const fallbackToolResultIndices = options.fallbackToolResultIndices ?? toolResultIndices;
2042
+ if (attachments.length === 0) return;
2043
+ if (toolResultIndices.length > 0 && toolResultIndices.length === attachments.length) {
2044
+ for (const [index, toolResultIndex] of toolResultIndices.entries()) {
2045
+ const currentAttachments$1 = target.get(toolResultIndex);
2046
+ if (currentAttachments$1) {
2047
+ currentAttachments$1.push(attachments[index]);
2045
2048
  continue;
2046
2049
  }
2050
+ target.set(toolResultIndex, [attachments[index]]);
2047
2051
  }
2048
- mergedContent.push(block);
2052
+ return;
2053
+ }
2054
+ const lastToolResultIndex = fallbackToolResultIndices.at(-1);
2055
+ if (lastToolResultIndex === void 0) return;
2056
+ const currentAttachments = target.get(lastToolResultIndex);
2057
+ if (currentAttachments) {
2058
+ currentAttachments.push(...attachments);
2059
+ return;
2049
2060
  }
2050
- return mergedContent;
2061
+ target.set(lastToolResultIndex, [...attachments]);
2062
+ };
2063
+ const startsWithPdfFileRead = (toolResult) => {
2064
+ if (typeof toolResult.content === "string") return toolResult.content.startsWith(PDF_FILE_READ_PREFIX);
2065
+ if (toolResult.content.some((block) => block.type === "document")) return false;
2066
+ if (toolResult.content.length === 0) return false;
2067
+ const firstBlock = toolResult.content[0];
2068
+ if (firstBlock.type !== "text") return false;
2069
+ return firstBlock.text.startsWith(PDF_FILE_READ_PREFIX);
2070
+ };
2071
+ const collectMergeableUserContent = (content) => {
2072
+ const toolResults = [];
2073
+ const textBlocks = [];
2074
+ const attachments = [];
2075
+ for (const [order, block] of content.entries()) {
2076
+ if (block.type === "tool_result") {
2077
+ toolResults.push(block);
2078
+ continue;
2079
+ }
2080
+ if (block.type === "text") {
2081
+ textBlocks.push(block);
2082
+ continue;
2083
+ }
2084
+ if (isAttachmentBlock(block)) {
2085
+ attachments.push({
2086
+ attachment: block,
2087
+ order
2088
+ });
2089
+ continue;
2090
+ }
2091
+ return null;
2092
+ }
2093
+ return {
2094
+ toolResults,
2095
+ textBlocks,
2096
+ attachments
2097
+ };
2098
+ };
2099
+ const mergeAttachmentsForToolResults = (toolResults, attachments) => {
2100
+ if (attachments.length === 0) return toolResults;
2101
+ const documentBlocks = attachments.filter(({ attachment }) => attachment.type === "document");
2102
+ const mergeableToolResultIndices = getMergeableToolResultIndices(toolResults);
2103
+ const pdfReadToolResultIndices = mergeableToolResultIndices.filter((index) => startsWithPdfFileRead(toolResults[index]));
2104
+ const attachmentsByToolResultIndex = /* @__PURE__ */ new Map();
2105
+ let remainingAttachments = attachments;
2106
+ let countMatchToolResultIndices = mergeableToolResultIndices;
2107
+ if (documentBlocks.length > 0 && pdfReadToolResultIndices.length > 0) {
2108
+ const matchedDocumentCount = Math.min(pdfReadToolResultIndices.length, documentBlocks.length);
2109
+ const matchedDocuments = documentBlocks.slice(0, matchedDocumentCount);
2110
+ const matchedDocumentOrders = new Set(matchedDocuments.map(({ order }) => order));
2111
+ const matchedPdfToolResultIndices = pdfReadToolResultIndices.slice(0, matchedDocumentCount);
2112
+ const matchedPdfToolResultIndexSet = new Set(matchedPdfToolResultIndices);
2113
+ assignAttachmentsToToolResults(attachmentsByToolResultIndex, matchedDocuments, { toolResultIndices: matchedPdfToolResultIndices });
2114
+ countMatchToolResultIndices = mergeableToolResultIndices.filter((index) => !matchedPdfToolResultIndexSet.has(index));
2115
+ remainingAttachments = attachments.filter(({ attachment, order }) => attachment.type !== "document" || !matchedDocumentOrders.has(order));
2116
+ }
2117
+ assignAttachmentsToToolResults(attachmentsByToolResultIndex, remainingAttachments, {
2118
+ toolResultIndices: countMatchToolResultIndices,
2119
+ fallbackToolResultIndices: mergeableToolResultIndices
2120
+ });
2121
+ return mergeAttachmentsIntoToolResults(toolResults, attachmentsByToolResultIndex);
2122
+ };
2123
+ const mergeUserMessageContent = (content) => {
2124
+ const mergeableContent = collectMergeableUserContent(content);
2125
+ if (!mergeableContent) return null;
2126
+ const { toolResults, textBlocks, attachments } = mergeableContent;
2127
+ if (toolResults.length === 0 || textBlocks.length === 0 && attachments.length === 0) return null;
2128
+ const mergedToolResults = textBlocks.length === 0 ? toolResults : mergeToolResult(toolResults, textBlocks);
2129
+ return mergeAttachmentsForToolResults(mergedToolResults, attachments);
2051
2130
  };
2052
2131
  const mergeToolResult = (toolResults, textBlocks) => {
2053
2132
  if (toolResults.length === textBlocks.length) return toolResults.map((tr, i) => mergeContentWithText(tr, textBlocks[i]));
@@ -2066,18 +2145,8 @@ const mergeToolResultForClaude = (anthropicPayload, options) => {
2066
2145
  for (const [index, msg] of anthropicPayload.messages.entries()) {
2067
2146
  if (options?.skipLastMessage && index === lastMessageIndex) continue;
2068
2147
  if (msg.role !== "user" || !Array.isArray(msg.content)) continue;
2069
- msg.content = mergeAttachmentsIntoLastToolResult(msg.content);
2070
- const toolResults = [];
2071
- const textBlocks = [];
2072
- let valid = true;
2073
- for (const block of msg.content) if (block.type === "tool_result") toolResults.push(block);
2074
- else if (block.type === "text") textBlocks.push(block);
2075
- else {
2076
- valid = false;
2077
- break;
2078
- }
2079
- if (!valid || toolResults.length === 0 || textBlocks.length === 0) continue;
2080
- msg.content = mergeToolResult(toolResults, textBlocks);
2148
+ const mergedContent = mergeUserMessageContent(msg.content);
2149
+ if (mergedContent) msg.content = mergedContent;
2081
2150
  }
2082
2151
  };
2083
2152
  const sanitizeIdeTools = (payload) => {
@@ -3096,4 +3165,4 @@ server.route("/:provider/v1/models", providerModelRoutes);
3096
3165
 
3097
3166
  //#endregion
3098
3167
  export { server };
3099
- //# sourceMappingURL=server-wOqxNjw_.js.map
3168
+ //# sourceMappingURL=server-COPd_KDa.js.map