feat: refactor message filtering to use filterSyntheticParts for improved clarity

This commit is contained in:
Bohdan Triapitsyn
2025-12-27 02:37:29 +02:00
parent 8f9facb561
commit 9241fed442
2 changed files with 41 additions and 11 deletions
+26 -1
View File
@@ -1,12 +1,16 @@
import type { Part } from "@opencode-ai/sdk";
const isSyntheticPart = (part: Part | undefined): boolean => {
export const isSyntheticPart = (part: Part | undefined): boolean => {
if (!part || typeof part !== "object") {
return false;
}
return Boolean((part as { synthetic?: boolean }).synthetic);
};
/**
* Checks if a message consists entirely of synthetic parts.
* Used for status/completion logic (not display filtering).
*/
export const isFullySyntheticMessage = (parts: Part[] | undefined): boolean => {
if (!Array.isArray(parts) || parts.length === 0) {
return false;
@@ -14,3 +18,24 @@ export const isFullySyntheticMessage = (parts: Part[] | undefined): boolean => {
return parts.every((part) => isSyntheticPart(part));
};
/**
* Filters out synthetic parts from a message, but only if there are
* non-synthetic parts present. If all parts are synthetic, returns
* them as-is so the message can still be displayed.
*/
export const filterSyntheticParts = (parts: Part[] | undefined): Part[] => {
if (!Array.isArray(parts) || parts.length === 0) {
return [];
}
const hasNonSynthetic = parts.some((part) => !isSyntheticPart(part));
// If there are non-synthetic parts, filter out synthetic ones
if (hasNonSynthetic) {
return parts.filter((part) => !isSyntheticPart(part));
}
// If all parts are synthetic, return them all (so message is displayed)
return parts;
};