feat: allow using chat to modify page titles

This commit is contained in:
LIlGG
2025-10-11 16:36:20 +08:00
parent a672fcad1c
commit 7acc4949fb
8 changed files with 88 additions and 23 deletions

View File

@@ -6,6 +6,12 @@ import type { UPageUIMessage } from '~/types/message';
const logger = createScopedLogger('select-context');
export type SelectContextResult = {
sections: string[];
pageName: string;
pageTitle: string;
};
export async function selectContext({
messages,
pages,
@@ -31,26 +37,29 @@ export async function selectContext({
---
页面名称:${page.name}
---
页面标题:${page.title}
---
页面内容:${page.content}
`;
});
const resp = await generateText({
system: `
你是一名软件工程师。你正在从事一个 HTML 项目,该项目包含多个页面,每个页面内容中包含多个 Section。这些 Section 可能是 HTML、style、JavaScript 片段。
提供给你的为 Body 内容,每个处于根节点下的 HTML 标签,都包含一个唯一的 domId 属性,并且为单独的一个 Section。
你是一名软件工程师。你正在从事一个 HTML 项目,该项目包含多个页面,每个页面内容中包含 HTML 片段,可能是 HTML、style、JavaScript 片段。
${pagesContent.join('\n')}
---
现在,你将获得一个任务。你需要从上述页面列表中选择与任务相关的页面与其相关的 Section。
现在,你将获得一个任务。你需要从上述页面列表中选择与任务相关的页面与用户任务相关的 HTML 片段。请务必保证:
- 如果涉及到脚本,则需要选择可能相关的所有脚本的完整内容。
- 如果涉及到样式,则需要选择可能相关的所有样式,包括内联样式、外部样式表和 style 标签中的样式。
RESPONSE FORMAT:
你的回复应严格遵循以下格式:
---
<updateContextBuffer>
<selectPage pageName="pageName">
<selectPage pageName="pageName" pageTitle="pageTitle">
<selectSection>
...section content...
</selectSection>
@@ -61,8 +70,8 @@ export async function selectContext({
---
* 你应该从 <updateContextBuffer> 开始,以 </updateContextBuffer> 结束。
* 你可以在回复中包含多个 <selectPage> 标签,每个 <selectPage> 标签中也可以包含多个 <selectSection> 标签。
* 你需要在 <selectPage> 标签中包含页面名称,但每个页面名称只能出现一次。
* 你需要在 <selectSection> 标签中包含完整的 Section 内容,只做选择,但不要对 Section 内容进行任何修改。
* 你需要在 <selectPage> 标签中包含页面名称和页面标题,但每个页面只能出现一次。
* 你需要在 <selectSection> 标签中包含完整的 HTML 内容,只做选择,但不要对 HTML 内容进行任何修改。
* 如果不需要任何更改,你可以留下空的 updateContextBuffer 标签。
`,
prompt: `
@@ -70,7 +79,7 @@ export async function selectContext({
用户当前任务: ${extractTextContent(lastUserMessage)}
请根据当前页面与 Section 的详细代码,选择与任务相关的页面以及 Section
请根据当前页面、页面属性以及内容,选择与任务相关的页面以及 HTML 片段
`,
model,
abortSignal,
@@ -84,14 +93,15 @@ export async function selectContext({
}
const updateContextBufferContent = updateContextBuffer[1];
const selectedPages: Record<string, string[]> = {};
const selectedPages: Record<string, SelectContextResult> = {};
const selectPageRegex = /<selectPage\s+pageName="([^"]+)">([\s\S]*?)<\/selectPage>/g;
const selectPageRegex = /<selectPage\s+pageName="([^"]+)"\s+pageTitle="([^"]+)">([\s\S]*?)<\/selectPage>/g;
let selectPageMatch;
while ((selectPageMatch = selectPageRegex.exec(updateContextBufferContent)) !== null) {
const pageName = selectPageMatch[1];
const pageContent = selectPageMatch[2];
const pageTitle = selectPageMatch[2];
const pageContent = selectPageMatch[3];
if (!pageName) {
logger.warn('页面名称为空');
@@ -110,7 +120,11 @@ export async function selectContext({
}
if (sections.length > 0) {
selectedPages[pageName] = sections;
selectedPages[pageName] = {
sections,
pageName,
pageTitle,
};
}
}