Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

fix(mobile): preserve unicode at reference budget cuts

Zhao Dengfeng343bbdd2d646
diff --git a/apps/mobile/src/__tests__/sessionReferences.test.ts b/apps/mobile/src/__tests__/sessionReferences.test.ts
index 12b9b0dc3e..9f349bf484 100644
--- a/apps/mobile/src/__tests__/sessionReferences.test.ts
+++ b/apps/mobile/src/__tests__/sessionReferences.test.ts
@@ -51,6 +51,20 @@ function asInvoke(
return (deviceId, channel, args = []) => implementation(deviceId, channel, args) as Promise<never>;
}
+function hasIsolatedSurrogate(text: string): boolean {
+ for (let index = 0; index < text.length; index += 1) {
+ const code = text.charCodeAt(index);
+ if (code >= 0xd800 && code <= 0xdbff) {
+ const next = text.charCodeAt(index + 1);
+ if (!(next >= 0xdc00 && next <= 0xdfff)) return true;
+ index += 1;
+ } else if (code >= 0xdc00 && code <= 0xdfff) {
+ return true;
+ }
+ }
+ return false;
+}
+
describe('mobile session-reference links', () => {
it('only permits stored-snapshot fallback for transient source outages', () => {
expect(canFallbackToStoredMobileSessionReferenceSnapshot(
@@ -675,6 +689,35 @@ describe('resolveMobileSessionReferences', () => {
expect(partialNeighbor?.content.length).toBeLessThan(20_000);
});
+ it.each([
+ { cutPoint: 'binary tail', payloadBody: '😀"' },
+ { cutPoint: 'serialized payload', payloadBody: '"😀' },
+ ])('keeps emoji whole at the $cutPoint multi-reference budget cut', async ({ payloadBody }) => {
+ const invoke = asInvoke(async (_deviceId, channel, args) => {
+ if (channel === 'local-db:sessions:get') return { id: args[0] };
+ const request = args[0] as { sessionId: string };
+ const content = request.sessionId === 's-tail'
+ ? '😀'.repeat(5_000)
+ : payloadBody.repeat(3_000);
+ return {
+ items: [message(request.sessionId, '1', 'c1', content)],
+ hasMore: false,
+ };
+ });
+
+ const contexts = await resolveMobileSessionReferences([
+ { sessionId: 's-tail', deviceId: 'dev-a' },
+ { sessionId: 's-payload', deviceId: 'dev-a' },
+ ], invoke);
+
+ const contents = contexts.flatMap((context) =>
+ context.messages.map((item) => item.content));
+ expect(contents.map(hasIsolatedSurrogate)).toEqual([false, false]);
+ expect(contents.every((content) => content.startsWith('…'))).toBe(true);
+ expect(estimateMobileReferenceTokens(serializeMobileSessionReferencePayload(contexts)))
+ .toBeLessThanOrEqual(8_000 - 128);
+ });
+
it('routes each reference to its own authorized source and shares the 20-message limit', async () => {
const calls: Array<{ deviceId: string; sessionId: string; limit: number }> = [];
const invoke = asInvoke(async (deviceId, channel, args) => {
diff --git a/apps/mobile/src/session/sessionReferences.ts b/apps/mobile/src/session/sessionReferences.ts
index 0fbec3865d..6da0db685e 100644
--- a/apps/mobile/src/session/sessionReferences.ts
+++ b/apps/mobile/src/session/sessionReferences.ts
@@ -259,6 +259,19 @@ function toReferenceMessage(row: Record<string, unknown>): MobileSessionReferenc
};
}
+function sliceFromCodePointBoundary(text: string, start: number): string {
+ const code = text.charCodeAt(start);
+ const previous = text.charCodeAt(start - 1);
+ // 截点落在低代理项时连同高代理项一起丢弃;向后包含它可能超出预算,
+ // 还会让已带省略号的内容在序列化预算循环中不再缩短。
+ const safeStart = start > 0 &&
+ code >= 0xdc00 && code <= 0xdfff &&
+ previous >= 0xd800 && previous <= 0xdbff
+ ? start + 1
+ : start;
+ return text.slice(safeStart);
+}
+
function sliceTailToTokenBudget(text: string, budget: number): string {
if (budget <= 0) return '';
if (estimateMobileReferenceTokens(text) <= budget) return text;
@@ -271,7 +284,7 @@ function sliceTailToTokenBudget(text: string, budget: number): string {
if (estimateMobileReferenceTokens(text.slice(text.length - middle)) <= payloadBudget) low = middle;
else high = middle - 1;
}
- return `…${text.slice(text.length - low)}`;
+ return `…${sliceFromCodePointBoundary(text, text.length - low)}`;
}
function fitMessagesToTokenBudget(
@@ -412,7 +425,7 @@ function fitSerializedPayloadBudget(
if (targetMessage) {
const excess = tokens - FINAL_REFERENCE_PAYLOAD_TOKENS;
const drop = Math.min(targetMessage.content.length - 1, Math.max(1, excess));
- targetMessage.content = `…${targetMessage.content.slice(drop + 1)}`;
+ targetMessage.content = `…${sliceFromCodePointBoundary(targetMessage.content, drop + 1)}`;
if (targetContext) targetContext.truncated = true;
} else {
const titled = contexts.find((context) => context.title);