Improve local evidence search and AI capabilities
This commit is contained in:
@@ -60,9 +60,9 @@ export function createSidebarPageAiSessionRuntime(context) {
|
||||
acpRuntime: pageUiState.pageAiAcpRuntime || 'reasonix',
|
||||
createdAt: now,
|
||||
updatedAt: now,
|
||||
source: 'local',
|
||||
source: 'draft',
|
||||
usage: null,
|
||||
status: 'idle',
|
||||
status: 'draft',
|
||||
messages: []
|
||||
};
|
||||
}
|
||||
@@ -117,6 +117,10 @@ export function createSidebarPageAiSessionRuntime(context) {
|
||||
if (title.length > 28) title = title.slice(0, 28) + '…';
|
||||
var persistence = String(row.persistence || payload.persistence || '').trim();
|
||||
var sessionStorage = String(row.sessionStorage || row.session_storage || payload.sessionStorage || '').trim();
|
||||
var status = String(row.status || (row.runtime && row.runtime.status) || '').trim();
|
||||
var hasConversationSignal = String(payload.message || payload.input || row.snippet || '').trim()
|
||||
|| pageAiNormalizeArray(row.messages).length > 0;
|
||||
if (status === 'session.created' && !hasConversationSignal) return null;
|
||||
var agentId = pageAiNormalizeAgentId(row.agentId || row.agent_id || payload.agentId || payload.agent_id);
|
||||
var profileId = String(row.profileId || row.profile_id || payload.profileId || payload.profile_id || '').trim();
|
||||
var profile = String(row.profile || payload.profile || pageAiRunProfile()).trim() || 'default';
|
||||
@@ -143,7 +147,7 @@ export function createSidebarPageAiSessionRuntime(context) {
|
||||
shareId: String(row.shareId || row.share_id || payload.shareId || '').trim(),
|
||||
acpSessionId: String(row.acpSessionId || row.acp_session_id || payload.acpSessionId || payload.acp_session_id || '').trim(),
|
||||
runId: String(row.runId || row.run_id || '').trim(),
|
||||
status: String(row.status || (row.runtime && row.runtime.status) || '').trim(),
|
||||
status: status,
|
||||
usage: row.usage && typeof row.usage === 'object' ? row.usage : null,
|
||||
preview: String(payload.message || row.snippet || '').trim(),
|
||||
messages: []
|
||||
@@ -164,11 +168,24 @@ export function createSidebarPageAiSessionRuntime(context) {
|
||||
return pageAiNormalizeSessions(Object.keys(byId).map(function(id) { return byId[id]; }));
|
||||
}
|
||||
|
||||
function pageAiDedupeSessions(sessions) {
|
||||
var byId = {};
|
||||
var ordered = [];
|
||||
pageAiNormalizeSessions(sessions).forEach(function(session) {
|
||||
var id = String(session && session.id || '').trim();
|
||||
if (!id || byId[id]) return;
|
||||
byId[id] = true;
|
||||
ordered.push(session);
|
||||
});
|
||||
return ordered;
|
||||
}
|
||||
|
||||
function pageAiSessionStorageLabel(session) {
|
||||
var storage = String(session && (session.sessionStorage || session.session_storage) || '').trim();
|
||||
var persistence = String(session && session.persistence || '').trim();
|
||||
if (storage === 'local_shared') return '共享会话';
|
||||
if (storage === 'local_private') return '本地私有';
|
||||
if (storage === 'sqlite_control_plane' || persistence === 'sqlite_acp_runtime_store') return '账号会话';
|
||||
if (storage === 'cloud' || persistence === 'convex_acp_runtime_store') return '云端会话';
|
||||
if (persistence === 'local_ai_session_jsonl') return '本地私有';
|
||||
return String(session && session.source || '').trim() === 'acp' ? '云端会话' : '本地私有';
|
||||
@@ -179,30 +196,11 @@ export function createSidebarPageAiSessionRuntime(context) {
|
||||
try {
|
||||
var raw = win.localStorage.getItem(pageAiStorageKey());
|
||||
var parsed = raw ? JSON.parse(raw) : null;
|
||||
var activeId = String(parsed && parsed.activeSessionId || '').trim();
|
||||
var activeProfile = String(parsed && parsed.activeProfileName || '').trim();
|
||||
var activeAcpRuntime = String(parsed && parsed.activeAcpRuntime || '').trim();
|
||||
var sessions = pageAiNormalizeSessions(parsed && parsed.sessions);
|
||||
var storageVersion = Number(parsed && parsed.version || 0);
|
||||
if (storageVersion >= sessionStorageVersion && activeAcpRuntime) pageUiState.pageAiAcpRuntime = activeAcpRuntime;
|
||||
if (activeProfile) pageAiSetActiveProfile(activeProfile);
|
||||
if (sessions.length) {
|
||||
pageUiState.pageAiSessions = sessions;
|
||||
var activeSession = sessions.find(function(session) { return session.id === activeId; }) || sessions[0];
|
||||
pageUiState.pageAiActiveSessionId = activeSession.id;
|
||||
if (activeSession.profile) pageAiSetActiveProfile(activeSession.profile);
|
||||
if (activeSession.agentId) pageUiState.pageAiAgentId = pageAiNormalizeAgentId(activeSession.agentId);
|
||||
if (!pageUiState.pageAiAcpRuntime && activeSession.acpRuntime) pageUiState.pageAiAcpRuntime = activeSession.acpRuntime;
|
||||
pageUiState.pageAiMessages = Array.isArray(activeSession.messages) ? activeSession.messages.slice() : [];
|
||||
return;
|
||||
}
|
||||
if (activeId) {
|
||||
pageUiState.pageAiSessions = [pageAiNewSession('当前页问答')];
|
||||
pageUiState.pageAiSessions[0].id = activeId;
|
||||
pageUiState.pageAiActiveSessionId = activeId;
|
||||
pageUiState.pageAiMessages = [];
|
||||
return;
|
||||
}
|
||||
} catch (_) {}
|
||||
var fresh = pageAiNewSession();
|
||||
pageUiState.pageAiSessions = [fresh];
|
||||
@@ -219,8 +217,24 @@ export function createSidebarPageAiSessionRuntime(context) {
|
||||
throw new Error(pageAiErrorMessage(payload, 'session_list_failed_' + response.status));
|
||||
}
|
||||
var backendSessions = pageAiNormalizeArray(payload.sessions).map(pageAiNormalizeBackendSessionRow).filter(Boolean);
|
||||
if (!backendSessions.length) return [];
|
||||
pageUiState.pageAiSessions = pageAiMergeSessions(pageUiState.pageAiSessions, backendSessions);
|
||||
var draftSessions = pageAiNormalizeArray(pageUiState.pageAiSessions).filter(function(session) {
|
||||
var id = String(session && session.id || '').trim();
|
||||
return id && !id.startsWith('mnote_')
|
||||
&& (id === pageUiState.pageAiActiveSessionId || (Array.isArray(session.messages) && session.messages.length > 0));
|
||||
});
|
||||
if (!backendSessions.length) {
|
||||
if (!draftSessions.length && !pageUiState.pageAiActiveSessionId) {
|
||||
var fresh = pageAiNewSession();
|
||||
pageUiState.pageAiSessions = [fresh];
|
||||
pageUiState.pageAiActiveSessionId = fresh.id;
|
||||
pageUiState.pageAiMessages = [];
|
||||
}
|
||||
pageUiState.pageAiSessionError = '';
|
||||
renderPageAiConversation();
|
||||
renderPageAiControls();
|
||||
return [];
|
||||
}
|
||||
pageUiState.pageAiSessions = pageAiDedupeSessions(backendSessions.concat(draftSessions));
|
||||
if (!pageUiState.pageAiActiveSessionId || !pageUiState.pageAiSessions.find(function(session) { return session.id === pageUiState.pageAiActiveSessionId; })) {
|
||||
pageUiState.pageAiActiveSessionId = pageUiState.pageAiSessions[0].id;
|
||||
}
|
||||
@@ -242,7 +256,7 @@ export function createSidebarPageAiSessionRuntime(context) {
|
||||
var eventType = String(event.eventType || event.event_type || event.event || '').trim();
|
||||
var payload = event.payload && typeof event.payload === 'object' ? event.payload : event;
|
||||
if (eventType === 'message.delta') {
|
||||
var delta = String(payload.delta || payload.text || payload.output_text || '').trim();
|
||||
var delta = String(payload.delta || payload.text || payload.output_text || '');
|
||||
return delta ? { role: 'assistant', content: delta } : null;
|
||||
}
|
||||
if (eventType === 'thought.delta') {
|
||||
@@ -301,10 +315,34 @@ export function createSidebarPageAiSessionRuntime(context) {
|
||||
var userMessage = String(runPayload.message || runPayload.input || '').trim();
|
||||
if (userMessage) messages.push({ role: 'user', content: userMessage });
|
||||
var runId = String(run && (run.runId || run.run_id) || '').trim();
|
||||
var assistantDelta = '';
|
||||
var completedOutput = '';
|
||||
function flushAssistantDelta() {
|
||||
var content = assistantDelta.trim();
|
||||
if (content) messages.push({ role: 'assistant', content: content });
|
||||
assistantDelta = '';
|
||||
}
|
||||
pageAiNormalizeArray(eventsByRunId[runId]).forEach(function(event) {
|
||||
var message = pageAiMessageFromRuntimeEvent(event);
|
||||
if (message) messages.push(message);
|
||||
if (!message) return;
|
||||
if (message.role === 'assistant' && !message.kind) {
|
||||
var eventType = String(event && (event.eventType || event.event_type || event.event) || '').trim();
|
||||
if (eventType === 'run.completed') {
|
||||
completedOutput = String(message.content || '').trim();
|
||||
return;
|
||||
}
|
||||
assistantDelta += String(message.content || '');
|
||||
return;
|
||||
}
|
||||
flushAssistantDelta();
|
||||
messages.push(message);
|
||||
});
|
||||
flushAssistantDelta();
|
||||
if (completedOutput && !messages.some(function(message) {
|
||||
return message.role === 'assistant' && String(message.content || '').trim() === completedOutput;
|
||||
})) {
|
||||
messages.push({ role: 'assistant', content: completedOutput });
|
||||
}
|
||||
});
|
||||
}
|
||||
if (!messages.length) messages = storedMessages;
|
||||
@@ -381,8 +419,7 @@ export function createSidebarPageAiSessionRuntime(context) {
|
||||
version: sessionStorageVersion,
|
||||
activeSessionId: pageUiState.pageAiActiveSessionId,
|
||||
activeProfileName: pageAiCurrentProfile(),
|
||||
activeAcpRuntime: pageUiState.pageAiAcpRuntime || 'reasonix',
|
||||
sessions: pageAiNormalizeSessions(pageUiState.pageAiSessions)
|
||||
activeAcpRuntime: pageUiState.pageAiAcpRuntime || 'reasonix'
|
||||
}));
|
||||
} catch (_) {}
|
||||
}
|
||||
@@ -427,7 +464,12 @@ export function createSidebarPageAiSessionRuntime(context) {
|
||||
updatedAt: Date.now(),
|
||||
messages: pageUiState.pageAiMessages.slice()
|
||||
};
|
||||
pageUiState.pageAiSessions = pageAiNormalizeSessions([session]);
|
||||
var previousSessionId = String(current && current.id || '').trim();
|
||||
var retainedSessions = pageAiNormalizeArray(pageUiState.pageAiSessions).filter(function(item) {
|
||||
var itemId = String(item && item.id || '').trim();
|
||||
return itemId && itemId !== session.id && itemId !== previousSessionId;
|
||||
});
|
||||
pageUiState.pageAiSessions = pageAiNormalizeSessions([session].concat(retainedSessions));
|
||||
pageUiState.pageAiActiveSessionId = session.id;
|
||||
pageAiPersistSessions();
|
||||
renderPageAiConversation();
|
||||
|
||||
Reference in New Issue
Block a user