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(memory): 为对话搜索结果附上任务链接

Dash56d581d03e46
diff --git a/apps/desktop/src/main/maker-host/__tests__/sessionSearch.test.ts b/apps/desktop/src/main/maker-host/__tests__/sessionSearch.test.ts
index 02f6b297ee..100ff97ada 100644
--- a/apps/desktop/src/main/maker-host/__tests__/sessionSearch.test.ts
+++ b/apps/desktop/src/main/maker-host/__tests__/sessionSearch.test.ts
@@ -9,6 +9,8 @@ const mocks = vi.hoisted(() => ({
vi.mock('../../localDb/client/current.js', () => ({
getDbClient: mocks.getDbClient,
}));
+vi.mock('../../authManager.js', () => ({ getDeviceId: () => 'host-device' }));
+vi.mock('electron', () => ({ app: {}, BrowserWindow: class {} }));
import { searchSessionsFn, searchSessionsWithBotScope } from '../session-search.js';
@@ -22,6 +24,20 @@ describe('session_search Bot ownership boundary', () => {
mocks.query.mockResolvedValue([]);
});
+ it('returns a task URL targeting the history host with an encoded session ID', async () => {
+ mocks.query.mockResolvedValue([
+ { messageId: 'm1', sessionId: 'task /?#中文', role: 'user', ts: 1, content: 'release', score: -1 },
+ { messageId: 'm2', sessionId: 'task-2', role: 'assistant', ts: 2, content: 'release', score: -2 },
+ ]);
+
+ const hits = await searchSessionsFn('release');
+
+ expect(hits.map((hit) => hit.sessionUrl)).toEqual([
+ 'cindy://session/task%20%2F%3F%23%E4%B8%AD%E6%96%87?device=host-device',
+ 'cindy://session/task-2?device=host-device',
+ ]);
+ });
+
it('restricts a Bot caller to Sessions linked to the same Bot', async () => {
mocks.queryOne.mockResolvedValue({ source: 'bot', botId: 'bot-a' });
diff --git a/apps/desktop/src/main/maker-host/session-search.ts b/apps/desktop/src/main/maker-host/session-search.ts
index 2d683e8d99..525169cea6 100644
--- a/apps/desktop/src/main/maker-host/session-search.ts
+++ b/apps/desktop/src/main/maker-host/session-search.ts
@@ -18,6 +18,8 @@
import type { SessionSearchOptions, SessionSearchHit, SessionSearchFn } from '@cindy/mcps';
+import { getDeviceId } from '../authManager.js';
+import { buildSessionDeepLink } from '../deepLink.js';
import { getDbClient } from '../localDb/client/current.js';
import {
buildMessagesFtsMatch,
@@ -114,9 +116,11 @@ export async function searchSessionsWithBotScope(
content: string;
score: number;
}>(sql, params);
+ const deviceId = getDeviceId();
return rows.map((r) => ({
messageId: r.messageId,
sessionId: r.sessionId,
+ sessionUrl: buildSessionDeepLink(r.sessionId, { deviceId }),
role: r.role,
ts: r.ts,
snippet: buildSnippetFromContent(r.content, queryTokens),
diff --git a/packages/lizi-mcps/src/__tests__/sessionContext.test.ts b/packages/lizi-mcps/src/__tests__/sessionContext.test.ts
index b6780e3926..c8709fb088 100644
--- a/packages/lizi-mcps/src/__tests__/sessionContext.test.ts
+++ b/packages/lizi-mcps/src/__tests__/sessionContext.test.ts
@@ -787,7 +787,12 @@ describe('dynamic lizi MCP session context', () => {
});
it('passes host-owned Bot identity to session_search without exposing it as tool input', async () => {
- const searchSessions = vi.fn(async () => []);
+ const hits = [{
+ sessionId: 'history-session',
+ sessionUrl: 'cindy://session/history-session?device=host-device',
+ messageId: 'm1', role: 'user', snippet: 'release', ts: 1, score: -1,
+ }];
+ const searchSessions = vi.fn(async () => hits);
const getManager = () => ({
isEnabled: () => true,
getStore: vi.fn(),
@@ -804,11 +809,12 @@ describe('dynamic lizi MCP session context', () => {
vendorOptions: {},
}) as { type: 'sdk'; instance: unknown };
- await tools(cfg.instance).call_tool.handler({
+ const result = await tools(cfg.instance).call_tool.handler({
name: 'session_search',
args: { query: 'release' },
});
+ expect(parse(result as never)).toMatchObject({ ok: true, data: { hits, count: 1 } });
expect(searchSessions).toHaveBeenCalledWith('release', {
callerMemoryScopeKey: 'bot:release-helper',
callerSessionId: 'bot-session-a',
diff --git a/packages/lizi-mcps/src/memory/sessionSearch.ts b/packages/lizi-mcps/src/memory/sessionSearch.ts
index 351901104c..0258ef44d2 100644
--- a/packages/lizi-mcps/src/memory/sessionSearch.ts
+++ b/packages/lizi-mcps/src/memory/sessionSearch.ts
@@ -36,7 +36,8 @@ export function registerSessionSearchTool(
description:
'FTS5 全文检索当前身份可访问的本地历史对话。普通任务按现有全局历史权限检索;' +
'Cindy Bot 只检索同一 Bot 关联的主任务、通道任务和归档任务。返回按相关度排序的 message ' +
- '命中 (sessionId / messageId / role / snippet 高亮 / ts)。' +
+ '命中 (sessionId / sessionUrl / messageId / role / snippet 高亮 / ts)。' +
+ ' 需要引用原对话时,可使用返回的 sessionUrl。' +
' 跟 memory_search 区别: memory_search 搜提炼后的 memory 分片, session_search 搜原始对话。' +
' 用于 "上次怎么解决这个 bug" / "用户提到过 X 在哪轮" 类场景。',
inputShape: {
diff --git a/packages/lizi-mcps/src/types.ts b/packages/lizi-mcps/src/types.ts
index ebf45afc63..85924897a1 100644
--- a/packages/lizi-mcps/src/types.ts
+++ b/packages/lizi-mcps/src/types.ts
@@ -474,6 +474,8 @@ export interface SessionSearchOptions {
export interface SessionSearchHit {
sessionId: string;
+ /** 宿主生成的任务深链,包含所属设备,供按需引用原对话。 */
+ sessionUrl: string;
messageId: string;
role: string;
/** FTS5 snippet() 高亮片段 */