Files
memind/session-stream.mjs
john 14a00774d9 feat(h5): web 联网能力、实时查询路由与 session Finish 对齐
- 新增 web 能力并挂载 platform/web(web_search/fetch_url)
- 实时查询强制 web skill,router fallback 与 await session Finish
- Session Broker 覆盖率/指标、stream replay 与相关单测/E2E 脚本

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-07-06 16:06:26 +08:00

57 lines
1.8 KiB
JavaScript

export const SESSION_STREAM_TERMINAL_TYPES = new Set(['Finish', 'Error']);
function envFlag(value, fallback = false) {
const raw = String(value ?? '').trim().toLowerCase();
if (!raw) return fallback;
return ['1', 'true', 'yes', 'on'].includes(raw);
}
export function isSessionStreamReplayEnabled(env = process.env) {
return envFlag(env.MEMIND_SESSION_STREAM_REPLAY, false);
}
export function parseSessionStreamLastEventId(value) {
const id = String(value ?? '').trim();
return id || null;
}
export function isTerminalSessionEvent(event) {
const type = String(event?.type ?? '').trim();
return SESSION_STREAM_TERMINAL_TYPES.has(type);
}
export function shouldPersistSessionStreamEvent(event) {
const type = String(event?.type ?? '').trim();
if (!type) return false;
return type !== 'Ping';
}
export function parseSessionSseBlock(block) {
const lines = String(block ?? '').split('\n');
let id = null;
let eventName = null;
let data = null;
for (const line of lines) {
if (line.startsWith('id:')) id = line.slice(3).trim() || null;
if (line.startsWith('event:')) eventName = line.slice(6).trim() || null;
if (line.startsWith('data:')) data = line.slice(5).trim();
}
return { id, eventName, data };
}
export function formatSessionStreamSseChunk({ id = null, event = null, data }) {
const lines = [];
if (id) lines.push(`id: ${id}`);
if (event) lines.push(`event: ${event}`);
lines.push(`data: ${JSON.stringify(data)}`);
return `${lines.join('\n')}\n\n`;
}
export function shouldSkipUpstreamAfterSessionReplay(events, { cursorMiss = false } = {}) {
if (!Array.isArray(events) || events.length === 0) {
return cursorMiss ? false : false;
}
const last = events[events.length - 1];
return isTerminalSessionEvent(last?.payload ?? last);
}