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。

feat(feishu): 群上下文消息带时间标注 + 相关性判断加时间限定规则

Lizie2fcc0c66bd6
diff --git a/apps/desktop/src/main/im/feishu/__tests__/feishuAdapter.test.ts b/apps/desktop/src/main/im/feishu/__tests__/feishuAdapter.test.ts
index 9a329a25a0..76eae1077b 100644
--- a/apps/desktop/src/main/im/feishu/__tests__/feishuAdapter.test.ts
+++ b/apps/desktop/src/main/im/feishu/__tests__/feishuAdapter.test.ts
@@ -50,6 +50,7 @@ import type {
IMStatus,
} from '@cindy/im';
import { buildFeishuAdapter } from '../adapter';
+import { formatHistoryTime } from '../groupContext';
const getService = vi.fn<() => 'feishu' | 'lark'>(() => 'feishu');
const fetchChatHistoryPage = vi.fn<
@@ -258,7 +259,7 @@ describe('feishu group lane adapter hooks', () => {
expect(dmPolicy).toBeUndefined();
});
- it('prepareAgentTurnText: 群 lane 拉历史拼上下文前缀, 剔除触发消息', async () => {
+ it('prepareAgentTurnText: 群 lane 拉历史拼上下文前缀(带时间标注), 剔除触发消息', async () => {
fetchChatHistoryPage.mockResolvedValueOnce(
historyPage([
historyEntry({ messageId: 'om_h1', senderName: 'Alice', text: '部署挂了' }),
@@ -267,9 +268,13 @@ describe('feishu group lane adapter hooks', () => {
);
const result = await adapter.prepareAgentTurnText?.(groupEvent());
expect(result?.agentText).toContain('<group_chat_context>');
- expect(result?.agentText).toContain('[Alice] 部署挂了');
+ expect(result?.agentText).toContain(`[Alice] ${formatHistoryTime(1)} 部署挂了`);
expect(result?.agentText).not.toContain('触发消息自己');
expect(result?.agentText.endsWith('上面说的问题怎么解决')).toBe(true);
+ // 相关性判断的提示词带时间限定规则 — 「今天/昨天」类问题靠它卡时间窗。
+ const judgePrompt = String(scopeMocks.utilityText.mock.calls[0][1] ?? '');
+ expect(judgePrompt).toContain('时间限定');
+ expect(judgePrompt).toContain('月-日 时:分');
});
it('prepareAgentTurnText: 群主流 @ 开新话题时按 groupContextLane(群主流)拉上下文', async () => {
diff --git a/apps/desktop/src/main/im/feishu/adapter.ts b/apps/desktop/src/main/im/feishu/adapter.ts
index 7bdf4e506d..55bd9ad164 100644
--- a/apps/desktop/src/main/im/feishu/adapter.ts
+++ b/apps/desktop/src/main/im/feishu/adapter.ts
@@ -67,10 +67,13 @@ async function judgeHistoryPageRelevant(question: string, pageLines: string[]):
'你在为一个群聊机器人决定上下文窗口。用户在群里 @ 机器人提了一个问题, ' +
'机器人正在按时间倒序分页回翻聊天记录, 需要判断当前这一页是否仍属于与问题相关的对话上下文。\n\n' +
`[用户的问题]\n${question.trim().slice(0, 500)}\n\n` +
- `[本页聊天记录(时间正序)]\n${pageLines.join('\n')}\n\n` +
- '只回答一个词:\n' +
+ `[本页聊天记录(时间正序, 每条消息带 月-日 时:分 的时间标注)]\n${pageLines.join('\n')}\n\n` +
+ '判定规则:\n' +
'- RELATED: 本页消息与用户问题相关(同一话题/讨论串, 或对理解问题有帮助)\n' +
- '- UNRELATED: 本页消息已经与用户问题无关(属于更早的其它话题)';
+ '- UNRELATED: 本页消息已经与用户问题无关(属于更早的其它话题)\n' +
+ '- **时间限定优先**: 用户问题若包含时间限定(如「今天/昨天/本周/最近 N 小时」), ' +
+ '消息时间不在该范围内的页一律判定 UNRELATED, 时间不符比话题相似更优先。\n\n' +
+ '只回答一个词:';
const r = await requestUtilityText(getMaker(), prompt, {
maxTokens: 8,
timeoutMs: 12_000,
diff --git a/apps/desktop/src/main/im/feishu/groupContext.ts b/apps/desktop/src/main/im/feishu/groupContext.ts
index 10c1623699..758ad6eab2 100644
--- a/apps/desktop/src/main/im/feishu/groupContext.ts
+++ b/apps/desktop/src/main/im/feishu/groupContext.ts
@@ -103,7 +103,17 @@ export interface FeishuGroupContextResult {
contextAttachments: IMAttachment[];
}
-/** 渲染一条历史消息为上下文行(附件给占位标注, 内容经 fence 中和)。 */
+/**
+ * 历史消息时间标注(本地时区) — 上下文行与相关性判断都靠它回答「今天聊了啥」
+ * 这类时间问题。月-日 时:分 够用; 跨年的旧消息时间退化为同格式(年份缺省)。
+ */
+export function formatHistoryTime(ms: number): string {
+ const d = new Date(ms);
+ const pad = (n: number) => String(n).padStart(2, '0');
+ return `${pad(d.getMonth() + 1)}-${pad(d.getDate())} ${pad(d.getHours())}:${pad(d.getMinutes())}`;
+}
+
+/** 渲染一条历史消息为上下文行(带时间, 附件给占位标注, 内容经 fence 中和)。 */
function renderHistoryLine(m: FeishuRecentChatMessage): string {
const name = sanitizeDisplayText(m.senderName) || (m.senderIsBot ? 'bot' : 'user');
const parts: string[] = [];
@@ -111,7 +121,9 @@ function renderHistoryLine(m: FeishuRecentChatMessage): string {
for (const att of m.attachments) {
parts.push(att.kind === 'image' ? '[图片]' : `[文件: ${att.fileName}]`);
}
- return neutralizeFenceTags(`[${name}${m.senderIsBot ? ' (bot)' : ''}] ${parts.join(' ')}`);
+ return neutralizeFenceTags(
+ `[${name}${m.senderIsBot ? ' (bot)' : ''}] ${formatHistoryTime(m.createTimeMs)} ${parts.join(' ')}`,
+ );
}
function isTextLikeFile(att: IMAttachment): boolean {