diff --git a/frontend/src/utils/embedContext.test.ts b/frontend/src/utils/embedContext.test.ts new file mode 100644 index 00000000..215f2811 --- /dev/null +++ b/frontend/src/utils/embedContext.test.ts @@ -0,0 +1,24 @@ +import assert from 'node:assert/strict' +import test from 'node:test' + +import { buildQueryWithHostContext, stripHostContextFromQuery } from './embedContext.ts' + +test('strips injected host context from persisted user messages without changing the query', () => { + const query = '我的userId是多少' + const outboundQuery = buildQueryWithHostContext(query, { userId: '21', page: '/ai-agent' }) + + assert.equal( + outboundQuery, + `[Host context]\nuserId: 21\npage: /ai-agent\n\n${query}`, + ) + assert.equal(stripHostContextFromQuery(outboundQuery), query) +}) + +test('preserves ordinary user-message content without a host-context prefix', () => { + assert.equal(stripHostContextFromQuery('我的userId是多少'), '我的userId是多少') +}) + +test('preserves malformed host-context content when its query separator is missing', () => { + const malformed = '[Host context]\nuserId: 21' + assert.equal(stripHostContextFromQuery(malformed), malformed) +}) diff --git a/frontend/src/utils/embedContext.ts b/frontend/src/utils/embedContext.ts index ec07b6cd..f51fff4a 100644 --- a/frontend/src/utils/embedContext.ts +++ b/frontend/src/utils/embedContext.ts @@ -10,3 +10,13 @@ export function buildQueryWithHostContext( if (!lines.length) return query return `[Host context]\n${lines.join('\n')}\n\n${query}` } + +/** Remove the injected host-context block from a query for user-visible display. */ +export function stripHostContextFromQuery(query: string): string { + const prefix = '[Host context]\n' + if (!query.startsWith(prefix)) return query + + const separatorIndex = query.indexOf('\n\n', prefix.length) + if (separatorIndex === -1) return query + return query.slice(separatorIndex + 2) +} diff --git a/frontend/src/views/embed/EmbedChatCore.vue b/frontend/src/views/embed/EmbedChatCore.vue index afac806a..854e5201 100644 --- a/frontend/src/views/embed/EmbedChatCore.vue +++ b/frontend/src/views/embed/EmbedChatCore.vue @@ -52,7 +52,7 @@ />