mirror of
https://github.com/siteboon/claudecodeui.git
synced 2026-06-04 03:25:37 +08:00
fix(cursor): remove user_info, system_reminder, and user_query tags
This commit is contained in:
@@ -25,6 +25,47 @@ type CursorMessageBlob = {
|
|||||||
content: AnyRecord;
|
content: AnyRecord;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
function isInternalCursorText(value: unknown): boolean {
|
||||||
|
if (typeof value !== 'string') {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
const normalized = value.trim();
|
||||||
|
return normalized.startsWith('<user_info>') || normalized.startsWith('<system_reminder>');
|
||||||
|
}
|
||||||
|
|
||||||
|
function isInternalCursorPart(part: unknown): boolean {
|
||||||
|
if (!part || typeof part !== 'object') {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
const record = part as AnyRecord;
|
||||||
|
const type = typeof record.type === 'string' ? record.type : '';
|
||||||
|
if (type === 'user_info' || type === 'system_reminder') {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return isInternalCursorText(record.text);
|
||||||
|
}
|
||||||
|
|
||||||
|
function unwrapUserQueryText(value: string, role: 'user' | 'assistant'): string {
|
||||||
|
if (role !== 'user') {
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
const normalized = value.trimStart();
|
||||||
|
const openTag = '<user_query>';
|
||||||
|
const closeTag = '</user_query>';
|
||||||
|
if (!normalized.startsWith(openTag)) {
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
const afterOpen = normalized.slice(openTag.length);
|
||||||
|
const closeIndex = afterOpen.lastIndexOf(closeTag);
|
||||||
|
const inner = closeIndex >= 0 ? afterOpen.slice(0, closeIndex) : afterOpen;
|
||||||
|
return inner.trim();
|
||||||
|
}
|
||||||
|
|
||||||
function sanitizeCursorSessionId(sessionId: string): string {
|
function sanitizeCursorSessionId(sessionId: string): string {
|
||||||
const normalized = sessionId.trim();
|
const normalized = sessionId.trim();
|
||||||
if (!normalized) {
|
if (!normalized) {
|
||||||
@@ -192,6 +233,7 @@ export class CursorSessionsProvider implements IProviderSessions {
|
|||||||
*/
|
*/
|
||||||
normalizeMessage(rawMessage: unknown, sessionId: string | null): NormalizedMessage[] {
|
normalizeMessage(rawMessage: unknown, sessionId: string | null): NormalizedMessage[] {
|
||||||
const raw = readObjectRecord(rawMessage);
|
const raw = readObjectRecord(rawMessage);
|
||||||
|
console.log('Normalizing Cursor message:', raw);
|
||||||
if (raw?.type === 'assistant' && raw.message?.content?.[0]?.text) {
|
if (raw?.type === 'assistant' && raw.message?.content?.[0]?.text) {
|
||||||
return [createNormalizedMessage({
|
return [createNormalizedMessage({
|
||||||
kind: 'stream_delta',
|
kind: 'stream_delta',
|
||||||
@@ -274,6 +316,7 @@ export class CursorSessionsProvider implements IProviderSessions {
|
|||||||
const baseId = blob.id || generateMessageId('cursor');
|
const baseId = blob.id || generateMessageId('cursor');
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
console.log('Normalizing Cursor blob content:', content);
|
||||||
if (!content?.role || !content?.content) {
|
if (!content?.role || !content?.content) {
|
||||||
if (content?.message?.role && content?.message?.content) {
|
if (content?.message?.role && content?.message?.content) {
|
||||||
if (content.message.role === 'system') {
|
if (content.message.role === 'system') {
|
||||||
@@ -283,11 +326,24 @@ export class CursorSessionsProvider implements IProviderSessions {
|
|||||||
let text = '';
|
let text = '';
|
||||||
if (Array.isArray(content.message.content)) {
|
if (Array.isArray(content.message.content)) {
|
||||||
text = content.message.content
|
text = content.message.content
|
||||||
.map((part: string | AnyRecord) => typeof part === 'string' ? part : part?.text || '')
|
.map((part: string | AnyRecord) => {
|
||||||
|
if (typeof part === 'string') {
|
||||||
|
if (isInternalCursorText(part)) {
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
return unwrapUserQueryText(part, role);
|
||||||
|
}
|
||||||
|
if (isInternalCursorPart(part)) {
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
return unwrapUserQueryText(part?.text || '', role);
|
||||||
|
})
|
||||||
.filter(Boolean)
|
.filter(Boolean)
|
||||||
.join('\n');
|
.join('\n');
|
||||||
} else if (typeof content.message.content === 'string') {
|
} else if (typeof content.message.content === 'string') {
|
||||||
text = content.message.content;
|
if (!isInternalCursorText(content.message.content)) {
|
||||||
|
text = unwrapUserQueryText(content.message.content, role);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (text?.trim()) {
|
if (text?.trim()) {
|
||||||
messages.push(createNormalizedMessage({
|
messages.push(createNormalizedMessage({
|
||||||
@@ -336,8 +392,15 @@ export class CursorSessionsProvider implements IProviderSessions {
|
|||||||
if (Array.isArray(content.content)) {
|
if (Array.isArray(content.content)) {
|
||||||
for (let partIdx = 0; partIdx < content.content.length; partIdx++) {
|
for (let partIdx = 0; partIdx < content.content.length; partIdx++) {
|
||||||
const part = content.content[partIdx];
|
const part = content.content[partIdx];
|
||||||
|
if (isInternalCursorPart(part)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
if (part?.type === 'text' && part?.text) {
|
if (part?.type === 'text' && part?.text) {
|
||||||
|
const normalizedPartText = unwrapUserQueryText(part.text, role);
|
||||||
|
if (!normalizedPartText) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
messages.push(createNormalizedMessage({
|
messages.push(createNormalizedMessage({
|
||||||
id: `${baseId}_${partIdx}`,
|
id: `${baseId}_${partIdx}`,
|
||||||
sessionId,
|
sessionId,
|
||||||
@@ -345,7 +408,7 @@ export class CursorSessionsProvider implements IProviderSessions {
|
|||||||
provider: PROVIDER,
|
provider: PROVIDER,
|
||||||
kind: 'text',
|
kind: 'text',
|
||||||
role,
|
role,
|
||||||
content: part.text,
|
content: normalizedPartText,
|
||||||
sequence: blob.sequence,
|
sequence: blob.sequence,
|
||||||
rowid: blob.rowid,
|
rowid: blob.rowid,
|
||||||
}));
|
}));
|
||||||
@@ -376,7 +439,15 @@ export class CursorSessionsProvider implements IProviderSessions {
|
|||||||
toolUseMap.set(toolId, message);
|
toolUseMap.set(toolId, message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if (typeof content.content === 'string' && content.content.trim()) {
|
} else if (
|
||||||
|
typeof content.content === 'string'
|
||||||
|
&& content.content.trim()
|
||||||
|
&& !isInternalCursorText(content.content)
|
||||||
|
) {
|
||||||
|
const normalizedText = unwrapUserQueryText(content.content, role);
|
||||||
|
if (!normalizedText) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
messages.push(createNormalizedMessage({
|
messages.push(createNormalizedMessage({
|
||||||
id: baseId,
|
id: baseId,
|
||||||
sessionId,
|
sessionId,
|
||||||
@@ -384,7 +455,7 @@ export class CursorSessionsProvider implements IProviderSessions {
|
|||||||
provider: PROVIDER,
|
provider: PROVIDER,
|
||||||
kind: 'text',
|
kind: 'text',
|
||||||
role,
|
role,
|
||||||
content: content.content,
|
content: normalizedText,
|
||||||
sequence: blob.sequence,
|
sequence: blob.sequence,
|
||||||
rowid: blob.rowid,
|
rowid: blob.rowid,
|
||||||
}));
|
}));
|
||||||
|
|||||||
Reference in New Issue
Block a user