diff --git a/README.md b/README.md index 19e00e2..02c2948 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ 把 DeepSeek Harness 接到 LongMemory 云端记忆服务:**每轮对话结束自动上报一条活动记录**(只上传元数据),并把云端记忆库变成 agent 可用的工具。 -当前版本 **0.1.0**,含宿主端与浏览器端两部分:host 半负责活动上报与工具注册,`client/index.js` 提供「设置 → 插件 → 云端记忆」配置卡片。 +当前版本 **0.1.1**,含宿主端与浏览器端两部分:host 半负责活动上报与工具注册,`client/index.js` 提供「设置 → 插件 → 云端记忆」配置卡片。 ## 它做什么 diff --git a/lib/api.js b/lib/api.js index a35845f..21e2479 100644 --- a/lib/api.js +++ b/lib/api.js @@ -115,6 +115,24 @@ export class CloudClient { reportTurn(payload, token) { return this.request('POST', '/activity/turn', payload, token); } + /** Stage one turn summary as pending proposals (summary:write token). */ + submitSummary(payload, token) { + return this.request('POST', '/activity/summary', payload, token); + } + /** + * Verify a summary token without staging anything. + * + * The endpoint authenticates before it validates the body, so an empty + * object coming back `400 invalid_input` proves the scope is right. + */ + async probeSummary(token) { + const result = await this.request('POST', '/activity/summary', {}, token); + if (result.ok) + return { ok: true, status: result.status, data: { authenticated: true } }; + if (result.error.status === 400) + return { ok: true, status: 400, data: { authenticated: true } }; + return { ok: false, error: result.error }; + } /** * Verify an activity token without recording anything. * diff --git a/lib/config.js b/lib/config.js index 7fc077f..d718d54 100644 --- a/lib/config.js +++ b/lib/config.js @@ -6,10 +6,12 @@ /** Defaults written into cordis.patch.yml; also the fallback for missing keys. */ export const DEFAULT_SETTINGS = { enabled: true, - baseUrl: 'https://your-memory-host/api/longmemory', + baseUrl: 'https://mem.qyhhh.top/api/longmemory', clientId: 'dsh-desktop', - requestTimeoutMs: 8000, + requestTimeoutMs: 5000, defaultK: 10, + summaryEnabled: true, + summaryMaxChars: 6000, }; const MIN_TIMEOUT_MS = 1000; const MAX_TIMEOUT_MS = 60_000; @@ -65,5 +67,10 @@ export function settingsFromConfig(raw) { const defaultK = boundedInt(source.defaultK, 1, 100); if (defaultK !== undefined) result.defaultK = defaultK; + if (typeof source.summaryEnabled === 'boolean') + result.summaryEnabled = source.summaryEnabled; + const summaryMaxChars = boundedInt(source.summaryMaxChars, 1000, 16_000); + if (summaryMaxChars !== undefined) + result.summaryMaxChars = summaryMaxChars; return result; } diff --git a/lib/index.js b/lib/index.js index c20d145..1a05463 100644 --- a/lib/index.js +++ b/lib/index.js @@ -37,6 +37,8 @@ export function apply(ctx, config) { client, settings: () => ({ enabled: settings().enabled, clientId: settings().clientId }), activityToken: () => store.activityToken(), + summary: () => ({ enabled: settings().summaryEnabled, maxChars: settings().summaryMaxChars }), + summaryToken: () => store.summaryToken(), log: (message) => logger.warn(message), }); // Announce at most once per activation so a missing token is obvious in logs. @@ -50,6 +52,9 @@ export function apply(ctx, config) { else { logger.info(`reporting to ${effective.baseUrl} as ${effective.clientId}`); } + if (effective.summaryEnabled && store.summaryToken() === '') { + logger.info('turn summaries are enabled but no summary token is configured; nothing will be staged'); + } // The host's `Events` map is augmented by @deepseek-ai/dsh-session, which a // third-party bundle cannot rely on; bind the listener through a local shape. const onEvent = ctx.on; diff --git a/lib/page.js b/lib/page.js index ccfe9f0..71c75e9 100644 --- a/lib/page.js +++ b/lib/page.js @@ -117,7 +117,7 @@ code { font-family: ui-monospace, SFMono-Regular, Consolas, monospace; font-size
- +
不带结尾斜杠,路径为 /api/longmemory。
@@ -139,6 +139,15 @@ code { font-family: ui-monospace, SFMono-Regular, Consolas, monospace; font-size 启用插件(关闭后不上报、不提供工具)
+
+ +
+
+ + +
@@ -176,6 +185,11 @@ code { font-family: ui-monospace, SFMono-Regular, Consolas, monospace; font-size
memory:write 同时允许读取,一个令牌即可覆盖检索与写入;只有 memory:read 时写入会被拒。
+
+ + +
用于 POST /activity/summary:只把已脱敏摘要送进待确认队列。
+
@@ -246,6 +260,8 @@ code { font-family: ui-monospace, SFMono-Regular, Consolas, monospace; font-size el('requestTimeoutMs').value = settings.requestTimeoutMs || ''; el('defaultK').value = settings.defaultK || ''; el('enabled').checked = settings.enabled === true; + el('summaryEnabled').checked = settings.summaryEnabled === true; + el('summaryMaxChars').value = settings.summaryMaxChars || ''; el('activityTokenHint').innerHTML = '用于 POST /activity/turn。当前 ' + (tokens.activity && tokens.activity.configured @@ -255,13 +271,19 @@ code { font-family: ui-monospace, SFMono-Regular, Consolas, monospace; font-size (tokens.memory && tokens.memory.configured ? pill('ok', '已配置 ' + tokens.memory.masked) : pill('bad', '未配置')); + el('summaryTokenHint').innerHTML = '用于轮次结束后的自动摘要(只进待确认队列)。当前 ' + + (tokens.summary && tokens.summary.configured + ? pill('ok', '已配置 ' + tokens.summary.masked) + : pill('sk', '未配置,自动摘要不会触发')); var stats = [ ['上报成功', reporter.recorded || 0], ['重复忽略', reporter.duplicate || 0], ['失败', reporter.failed || 0], ['已跳过', reporter.skipped || 0], - ['待重试', reporter.queued || 0] + ['待重试', reporter.queued || 0], + ['摘要待确认', reporter.summaryStaged || 0], + ['摘要失败', reporter.summaryFailed || 0] ]; el('stats').innerHTML = stats.map(function (pair) { return '
' + pair[0] + '
' + pair[1] + '
'; @@ -312,12 +334,16 @@ code { font-family: ui-monospace, SFMono-Regular, Consolas, monospace; font-size clientId: el('clientId').value.trim(), requestTimeoutMs: Number(el('requestTimeoutMs').value), defaultK: Number(el('defaultK').value), - enabled: el('enabled').checked + enabled: el('enabled').checked, + summaryEnabled: el('summaryEnabled').checked, + summaryMaxChars: Number(el('summaryMaxChars').value) }; var activity = el('activityToken').value.trim(); var memory = el('memoryToken').value.trim(); + var summary = el('summaryToken').value.trim(); if (activity !== '') patch.activityToken = activity; if (memory !== '') patch.memoryToken = memory; + if (summary !== '') patch.summaryToken = summary; fetch('/cloud-memory/config', { method: 'PUT', headers: { 'content-type': 'application/json', accept: 'application/json' }, @@ -326,6 +352,7 @@ code { font-family: ui-monospace, SFMono-Regular, Consolas, monospace; font-size if (!body || body.ok !== true) { toast('保存失败:' + ((body && body.error) || 'unknown')); return; } el('activityToken').value = ''; el('memoryToken').value = ''; + el('summaryToken').value = ''; render(body); toast('已保存'); }).catch(function (error) { toast('保存失败:' + error.message); }); diff --git a/lib/reporter.js b/lib/reporter.js index 5b64a01..5946895 100644 --- a/lib/reporter.js +++ b/lib/reporter.js @@ -59,16 +59,68 @@ export function conversationIdOf(session) { const value = session.id ?? session.sessionId; return typeof value === 'string' && value !== '' ? value : undefined; } +/** Text pulled out of one `user/message` or `assistant/message` payload. */ +export function textOf(value, depth = 0) { + if (depth > 5) + return undefined; + if (typeof value === 'string') + return value.trim() === '' ? undefined : value; + if (Array.isArray(value)) { + const parts = []; + for (const item of value) { + if (item !== null && typeof item === 'object') { + const part = item; + // Tool calls/results are never summarized: they can carry files and secrets. + if (part.type === 'tool_use' || part.type === 'tool_result' || part.type === 'image') + continue; + if (part.type === 'text' && typeof part.text === 'string') { + parts.push(part.text); + continue; + } + } + const nested = textOf(item, depth + 1); + if (nested !== undefined) + parts.push(nested); + } + return parts.length === 0 ? undefined : parts.join('\n'); + } + if (value === null || typeof value !== 'object') + return undefined; + const record = value; + for (const key of ['text', 'message', 'content', 'body']) { + const nested = textOf(record[key], depth + 1); + if (nested !== undefined) + return nested; + } + return undefined; +} +/** Local redaction: a summary must not carry credentials to the cloud. */ +const REDACTIONS = [ + [/-----BEGIN [A-Z ]+-----[\s\S]*?-----END [A-Z ]+-----/g, '[已脱敏:私钥]'], + [/\b(sk|ghp|gho|ghs|xox[abprs]|cmt|cmr)_[A-Za-z0-9_-]{12,}/g, '[已脱敏:令牌]'], + [/\bBearer\s+[A-Za-z0-9._-]{16,}/gi, 'Bearer [已脱敏]'], + [/\beyJ[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,}/g, '[已脱敏:JWT]'], + [/((?:password|passwd|pwd|secret|api[_-]?key|access[_-]?token)\s*[:=]\s*)[^\s,;]{6,}/gi, '$1[已脱敏]'], +]; +export function redactSecrets(text) { + let out = text; + for (const [pattern, replacement] of REDACTIONS) + out = out.replace(pattern, replacement); + return out; +} /** * Tracks open turns and reports each finished one. * * Reporting never blocks the agent loop: a failed POST is queued and retried by * {@link flush}, and a service outage only shows up in the status card. */ +/** A queued report is retried at most this many times before being dropped. */ +const MAX_RETRY_ATTEMPTS = 5; export class Reporter { options; startedAt = new Map(); queue = []; + retries = new Map(); recent = []; attempted = 0; recorded = 0; @@ -78,6 +130,12 @@ export class Reporter { lastSentAt; lastError; draining = false; + /** Redacted message text per conversation, consumed at turn/end. */ + transcripts = new Map(); + summaryAttempted = 0; + summaryStaged = 0; + summaryFailed = 0; + lastSummaryError; constructor(options) { this.options = options; } @@ -100,6 +158,10 @@ export class Reporter { handleEvent(session, event) { if (event === null || typeof event !== 'object') return; + if (event.type === 'user/message' || event.type === 'assistant/message') { + this.collect(session, event); + return; + } if (event.type === 'turn/start') { this.handleStart(session, event); return; @@ -145,6 +207,74 @@ export class Reporter { completed_at: completedAt, }; void this.send(payload); + void this.sendSummary(conversationId, key, status); + } + /** Buffer one redacted message for the turn that is still open. */ + collect(session, event) { + const conversationId = conversationIdOf(session); + if (conversationId === undefined) + return; + const raw = textOf(event.data); + if (raw === undefined) + return; + const value = redactSecrets(raw).slice(0, 4000); + const buffer = this.transcripts.get(conversationId) ?? []; + buffer.push({ role: event.type === 'user/message' ? 'user' : 'assistant', text: value, turn: turnOf(event.data) }); + if (buffer.length > 120) + buffer.splice(0, buffer.length - 120); + this.transcripts.set(conversationId, buffer); + } + /** Take this turn's messages out of the buffer, capped by the char budget. */ + takeTranscript(conversationId, turnKey) { + const buffer = this.transcripts.get(conversationId) ?? []; + const turn = Number(turnKey.slice(turnKey.lastIndexOf(':') + 1)); + const picked = []; + const rest = []; + for (const item of buffer) { + const matches = Number.isFinite(turn) && (item.turn === undefined || item.turn === turn); + if (matches) + picked.push({ role: item.role, text: item.text }); + else + rest.push(item); + } + this.transcripts.set(conversationId, rest); + const maxChars = Math.max(1000, this.options.summary().maxChars); + let total = 0; + const kept = []; + for (const message of picked) { + if (total + message.text.length > maxChars) + break; + kept.push(message); + total += message.text.length; + } + return kept; + } + /** Stage one summary; failures only surface in the status card. */ + async sendSummary(conversationId, turnKey, status) { + const summary = this.options.summary(); + const token = this.options.summaryToken(); + if (!summary.enabled || token === '' || status !== 'completed') + return; + const messages = this.takeTranscript(conversationId, turnKey); + if (messages.length === 0) + return; + const payload = { conversation_id: conversationId, turn_id: turnKey, messages }; + this.summaryAttempted += 1; + const result = await this.options.client.submitSummary(payload, token); + if (result.ok) { + this.summaryStaged += typeof result.data.staged === 'number' ? result.data.staged : 0; + return; + } + if (result.error.status === 0 || result.error.status >= 500) { + const retry = await this.options.client.submitSummary(payload, token); + if (retry.ok) { + this.summaryStaged += typeof retry.data.staged === 'number' ? retry.data.staged : 0; + return; + } + } + this.summaryFailed += 1; + this.lastSummaryError = describeError(result.error); + this.options.log(`cloud-memory: turn summary failed: ${this.lastSummaryError}`); } push(record) { this.recent.unshift(record); @@ -163,6 +293,7 @@ export class Reporter { this.duplicate += 1; else this.recorded += 1; + this.retries.delete(payload.turn_id); this.lastSentAt = now; this.push({ turnId: payload.turn_id, @@ -213,9 +344,20 @@ export class Reporter { if (!result.ok) { this.lastError = describeError(result.error); this.failed += 1; + const attempt = (this.retries.get(payload.turn_id) ?? 0) + 1; + if (attempt >= MAX_RETRY_ATTEMPTS) { + // Give up on this turn: a permanently failing endpoint must not keep + // an unbounded queue of metadata forever. + this.queue.shift(); + this.retries.delete(payload.turn_id); + this.options.log(`cloud-memory: dropping turn report after ${attempt} attempts: ${this.lastError}`); + continue; + } + this.retries.set(payload.turn_id, attempt); break; } this.queue.shift(); + this.retries.delete(payload.turn_id); sent += 1; if (result.data.duplicate === true || result.status === 200) this.duplicate += 1; @@ -238,6 +380,10 @@ export class Reporter { failed: this.failed, skipped: this.skipped, queued: this.queue.length, + summaryAttempted: this.summaryAttempted, + summaryStaged: this.summaryStaged, + summaryFailed: this.summaryFailed, + ...(this.lastSummaryError === undefined ? {} : { lastSummaryError: this.lastSummaryError }), ...(this.lastSentAt === undefined ? {} : { lastSentAt: this.lastSentAt }), ...(this.lastError === undefined ? {} : { lastError: this.lastError }), recent: [...this.recent], diff --git a/lib/routes.js b/lib/routes.js index 5b47cc6..f6460df 100644 --- a/lib/routes.js +++ b/lib/routes.js @@ -59,6 +59,7 @@ function statusPayload(deps) { const tokens = { activity: view.activityToken, memory: view.memoryToken, + summary: view.summaryToken, }; const settings = { enabled: view.enabled, @@ -66,6 +67,8 @@ function statusPayload(deps) { clientId: view.clientId, requestTimeoutMs: view.requestTimeoutMs, defaultK: view.defaultK, + summaryEnabled: view.summaryEnabled, + summaryMaxChars: view.summaryMaxChars, }; return { ok: true, settings, tokens, settingsPath: view.statePath, reporter: deps.reporter.snapshot() }; } @@ -157,7 +160,18 @@ async function serveTest(deps, req, res) { ? { ok: true, message: `令牌有效(${(probe.data.projects ?? []).length} 个项目)` } : { ok: false, message: describeError(probe.error) }; })(); - json(res, 200, { ok: true, activity, memory }); + const summaryToken = deps.store.summaryToken(); + const summary = !deps.store.settings().summaryEnabled + ? { ok: false, message: '自动摘要已关闭(可在下方勾选开启)' } + : summaryToken === '' + ? { ok: false, message: '未配置摘要令牌:留空则不做自动摘要' } + : await (async () => { + const probe = await client.probeSummary(summaryToken); + return probe.ok + ? { ok: true, message: '摘要令牌有效(只进待确认队列)' } + : { ok: false, message: describeError(probe.error) }; + })(); + json(res, 200, { ok: true, activity, memory, summary }); } /** `POST /cloud-memory/flush`. */ async function serveFlush(deps, req, res) { @@ -176,6 +190,7 @@ async function serveFlush(deps, req, res) { const TOKEN_LABELS = { activity: 'DSH desktop 活动上报', memory: 'DSH desktop 记忆读写', + summary: 'DSH desktop 对话摘要', }; /** * `POST /cloud-memory/login` — sign in with the cloud account and exchange it @@ -239,6 +254,7 @@ async function serveLogin(deps, req, res) { } const activity = await client.mintToken(session.data.sessionCookie, 'activity:write', TOKEN_LABELS.activity); const memory = await client.mintToken(session.data.sessionCookie, 'memory:write', TOKEN_LABELS.memory); + const summary = await client.mintToken(session.data.sessionCookie, 'summary:write', TOKEN_LABELS.summary); if (!activity.ok && !memory.ok) { const first = activity.ok ? memory : activity; const reason = first.ok ? '无法创建令牌' : describeError(first.error); @@ -250,6 +266,8 @@ async function serveLogin(deps, req, res) { patch.activityToken = activity.data.token; if (memory.ok && typeof memory.data.token === 'string') patch.memoryToken = memory.data.token; + if (summary.ok && typeof summary.data.token === 'string') + patch.summaryToken = summary.data.token; const applied = deps.store.patch(patch); if (!applied.ok) { json(res, 500, { ok: false, error: applied.error }); @@ -268,6 +286,7 @@ async function serveLogin(deps, req, res) { }, activity: activity.ok, memory: memory.ok, + summary: summary.ok, warning: failed.ok ? undefined : `部分令牌没有创建成功:${describeError(failed.error)}`, }, }); diff --git a/lib/store.js b/lib/store.js index bf0631f..b8d0cba 100644 --- a/lib/store.js +++ b/lib/store.js @@ -81,6 +81,14 @@ function parseState(raw) { const memoryToken = cleanString(source.memoryToken); if (memoryToken !== undefined) state.memoryToken = memoryToken; + if (typeof source.summaryEnabled === 'boolean') + state.summaryEnabled = source.summaryEnabled; + const summaryMaxChars = clampInt(source.summaryMaxChars, 1000, 16_000); + if (summaryMaxChars !== undefined) + state.summaryMaxChars = summaryMaxChars; + const summaryToken = cleanString(source.summaryToken); + if (summaryToken !== undefined) + state.summaryToken = summaryToken; return state; } catch { @@ -141,6 +149,10 @@ export class SettingsStore { resolved.requestTimeoutMs = this.state.requestTimeoutMs; if (this.state.defaultK !== undefined) resolved.defaultK = this.state.defaultK; + if (this.state.summaryEnabled !== undefined) + resolved.summaryEnabled = this.state.summaryEnabled; + if (this.state.summaryMaxChars !== undefined) + resolved.summaryMaxChars = this.state.summaryMaxChars; return resolved; } /** The activity:write bearer token (sidecar first, env fallback). */ @@ -151,15 +163,21 @@ export class SettingsStore { memoryToken() { return this.state.memoryToken ?? envToken('DSH_CLOUD_MEMORY_MEMORY_TOKEN') ?? ''; } + /** The summary:write bearer token (optional; absent disables summaries). */ + summaryToken() { + return this.state.summaryToken ?? envToken('DSH_CLOUD_MEMORY_SUMMARY_TOKEN') ?? ''; + } /** Masked snapshot for the browser card. */ describe() { const settings = this.settings(); const activity = this.activityToken(); const memory = this.memoryToken(); + const summary = this.summaryToken(); return { ...settings, activityToken: { configured: activity !== '', masked: activity === '' ? '' : maskToken(activity) }, memoryToken: { configured: memory !== '', masked: memory === '' ? '' : maskToken(memory) }, + summaryToken: { configured: summary !== '', masked: summary === '' ? '' : maskToken(summary) }, statePath: this.file, }; } @@ -213,6 +231,25 @@ export class SettingsStore { return { ok: false, error: 'defaultK must be a number' }; next.defaultK = defaultK; } + if (patch.summaryEnabled !== undefined) { + if (typeof patch.summaryEnabled !== 'boolean') + return { ok: false, error: 'summaryEnabled must be a boolean' }; + next.summaryEnabled = patch.summaryEnabled; + } + if (patch.summaryMaxChars !== undefined) { + const summaryMaxChars = clampInt(patch.summaryMaxChars, 1000, 16_000); + if (summaryMaxChars === undefined) + return { ok: false, error: 'summaryMaxChars must be a number' }; + next.summaryMaxChars = summaryMaxChars; + } + const summaryToken = cleanString(patch.summaryToken); + if (summaryToken !== undefined) { + if (!summaryToken.startsWith('cmt_')) + return { ok: false, error: 'summary token must start with cmt_' }; + next.summaryToken = summaryToken; + } + if (patch.clearSummaryToken === true) + delete next.summaryToken; if (patch.clearActivityToken === true) delete next.activityToken; if (patch.clearMemoryToken === true) diff --git a/package.json b/package.json index 0ffadc2..ca4cd73 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "dsh-cloud-memory", - "version": "0.1.0", + "version": "0.1.1", "description": "Cloud memory for DSH: report one activity turn per conversation round and expose memory_search / memory_read / memory_add tools against a LongMemory cloud endpoint.", "type": "module", "license": "MIT", diff --git a/src/api.ts b/src/api.ts index 537985f..3a3708b 100644 --- a/src/api.ts +++ b/src/api.ts @@ -41,6 +41,26 @@ export interface TurnAck { duplicate?: boolean } +/** One transcript message sent for turn summarization (text only). */ +export interface SummaryMessage { + role: 'user' | 'assistant' + text: string +} + +/** `POST /activity/summary` request body. */ +export interface SummaryPayload { + conversation_id: string + turn_id: string + messages: SummaryMessage[] +} + +/** Staging outcome: how many proposals were queued for confirmation. */ +export interface SummaryAck { + staged?: number + skipped?: number + full?: boolean +} + /** One search hit as the service indexes it. */ export interface MemoryHit { id?: string @@ -226,6 +246,24 @@ export class CloudClient { return this.request('POST', '/activity/turn', payload, token) } + /** Stage one turn summary as pending proposals (summary:write token). */ + submitSummary(payload: SummaryPayload, token: string): Promise> { + return this.request('POST', '/activity/summary', payload, token) + } + + /** + * Verify a summary token without staging anything. + * + * The endpoint authenticates before it validates the body, so an empty + * object coming back `400 invalid_input` proves the scope is right. + */ + async probeSummary(token: string): Promise> { + const result = await this.request('POST', '/activity/summary', {}, token) + if (result.ok) return { ok: true, status: result.status, data: { authenticated: true } } + if (result.error.status === 400) return { ok: true, status: 400, data: { authenticated: true } } + return { ok: false, error: result.error } + } + /** * Verify an activity token without recording anything. * diff --git a/src/config.ts b/src/config.ts index c1c0700..b432871 100644 --- a/src/config.ts +++ b/src/config.ts @@ -7,10 +7,12 @@ /** Defaults written into cordis.patch.yml; also the fallback for missing keys. */ export const DEFAULT_SETTINGS: Settings = { enabled: true, - baseUrl: 'https://your-memory-host/api/longmemory', + baseUrl: 'https://mem.qyhhh.top/api/longmemory', clientId: 'dsh-desktop', - requestTimeoutMs: 8000, + requestTimeoutMs: 5000, defaultK: 10, + summaryEnabled: true, + summaryMaxChars: 6000, } /** Effective, fully-resolved settings. */ @@ -25,6 +27,10 @@ export interface Settings { requestTimeoutMs: number /** Default result count for `memory_search`. */ defaultK: number + /** Stage a turn summary as pending proposals after each completed round. */ + summaryEnabled: boolean + /** Maximum transcript characters sent for one summary (privacy/cost cap). */ + summaryMaxChars: number } /** Raw row config as it comes out of the cordis loader. */ @@ -76,5 +82,8 @@ export function settingsFromConfig(raw: unknown): Partial { if (timeout !== undefined) result.requestTimeoutMs = timeout const defaultK = boundedInt(source.defaultK, 1, 100) if (defaultK !== undefined) result.defaultK = defaultK + if (typeof source.summaryEnabled === 'boolean') result.summaryEnabled = source.summaryEnabled + const summaryMaxChars = boundedInt(source.summaryMaxChars, 1000, 16_000) + if (summaryMaxChars !== undefined) result.summaryMaxChars = summaryMaxChars return result } diff --git a/src/index.ts b/src/index.ts index a5eb76c..eaceb95 100644 --- a/src/index.ts +++ b/src/index.ts @@ -45,6 +45,8 @@ export function apply(ctx: CloudMemoryContext, config: unknown): void { client, settings: () => ({ enabled: settings().enabled, clientId: settings().clientId }), activityToken: () => store.activityToken(), + summary: () => ({ enabled: settings().summaryEnabled, maxChars: settings().summaryMaxChars }), + summaryToken: () => store.summaryToken(), log: (message) => logger.warn(message), }) @@ -57,6 +59,9 @@ export function apply(ctx: CloudMemoryContext, config: unknown): void { } else { logger.info(`reporting to ${effective.baseUrl} as ${effective.clientId}`) } + if (effective.summaryEnabled && store.summaryToken() === '') { + logger.info('turn summaries are enabled but no summary token is configured; nothing will be staged') + } // The host's `Events` map is augmented by @deepseek-ai/dsh-session, which a // third-party bundle cannot rely on; bind the listener through a local shape. diff --git a/src/page.ts b/src/page.ts index 2e8fd66..8a38c49 100644 --- a/src/page.ts +++ b/src/page.ts @@ -119,7 +119,7 @@ code { font-family: ui-monospace, SFMono-Regular, Consolas, monospace; font-size
- +
不带结尾斜杠,路径为 /api/longmemory。
@@ -141,6 +141,15 @@ code { font-family: ui-monospace, SFMono-Regular, Consolas, monospace; font-size 启用插件(关闭后不上报、不提供工具)
+
+ +
+
+ + +
@@ -178,6 +187,11 @@ code { font-family: ui-monospace, SFMono-Regular, Consolas, monospace; font-size
memory:write 同时允许读取,一个令牌即可覆盖检索与写入;只有 memory:read 时写入会被拒。
+
+ + +
用于 POST /activity/summary:只把已脱敏摘要送进待确认队列。
+
@@ -248,6 +262,8 @@ code { font-family: ui-monospace, SFMono-Regular, Consolas, monospace; font-size el('requestTimeoutMs').value = settings.requestTimeoutMs || ''; el('defaultK').value = settings.defaultK || ''; el('enabled').checked = settings.enabled === true; + el('summaryEnabled').checked = settings.summaryEnabled === true; + el('summaryMaxChars').value = settings.summaryMaxChars || ''; el('activityTokenHint').innerHTML = '用于 POST /activity/turn。当前 ' + (tokens.activity && tokens.activity.configured @@ -257,13 +273,19 @@ code { font-family: ui-monospace, SFMono-Regular, Consolas, monospace; font-size (tokens.memory && tokens.memory.configured ? pill('ok', '已配置 ' + tokens.memory.masked) : pill('bad', '未配置')); + el('summaryTokenHint').innerHTML = '用于轮次结束后的自动摘要(只进待确认队列)。当前 ' + + (tokens.summary && tokens.summary.configured + ? pill('ok', '已配置 ' + tokens.summary.masked) + : pill('sk', '未配置,自动摘要不会触发')); var stats = [ ['上报成功', reporter.recorded || 0], ['重复忽略', reporter.duplicate || 0], ['失败', reporter.failed || 0], ['已跳过', reporter.skipped || 0], - ['待重试', reporter.queued || 0] + ['待重试', reporter.queued || 0], + ['摘要待确认', reporter.summaryStaged || 0], + ['摘要失败', reporter.summaryFailed || 0] ]; el('stats').innerHTML = stats.map(function (pair) { return '
' + pair[0] + '
' + pair[1] + '
'; @@ -314,12 +336,16 @@ code { font-family: ui-monospace, SFMono-Regular, Consolas, monospace; font-size clientId: el('clientId').value.trim(), requestTimeoutMs: Number(el('requestTimeoutMs').value), defaultK: Number(el('defaultK').value), - enabled: el('enabled').checked + enabled: el('enabled').checked, + summaryEnabled: el('summaryEnabled').checked, + summaryMaxChars: Number(el('summaryMaxChars').value) }; var activity = el('activityToken').value.trim(); var memory = el('memoryToken').value.trim(); + var summary = el('summaryToken').value.trim(); if (activity !== '') patch.activityToken = activity; if (memory !== '') patch.memoryToken = memory; + if (summary !== '') patch.summaryToken = summary; fetch('/cloud-memory/config', { method: 'PUT', headers: { 'content-type': 'application/json', accept: 'application/json' }, @@ -328,6 +354,7 @@ code { font-family: ui-monospace, SFMono-Regular, Consolas, monospace; font-size if (!body || body.ok !== true) { toast('保存失败:' + ((body && body.error) || 'unknown')); return; } el('activityToken').value = ''; el('memoryToken').value = ''; + el('summaryToken').value = ''; render(body); toast('已保存'); }).catch(function (error) { toast('保存失败:' + error.message); }); diff --git a/src/reporter.ts b/src/reporter.ts index e231cdc..d02143c 100644 --- a/src/reporter.ts +++ b/src/reporter.ts @@ -10,7 +10,7 @@ * Only metadata leaves the process. Prompt text and tool output are never part * of a report — the payload builder cannot see them. */ -import type { CloudClient, TurnPayload } from './api.ts' +import type { CloudClient, SummaryMessage, SummaryPayload, TurnPayload } from './api.ts' import { describeError } from './api.ts' import type { SessionEventLike, SessionLike } from './context-types.ts' @@ -39,6 +39,10 @@ export interface ReporterSnapshot { failed: number skipped: number queued: number + summaryAttempted?: number + summaryStaged?: number + summaryFailed?: number + lastSummaryError?: string lastSentAt?: number lastError?: string recent: ReportRecord[] @@ -51,6 +55,10 @@ export interface ReporterOptions { settings: () => { enabled: boolean; clientId: string } /** Activity token, or empty when unconfigured. */ activityToken: () => string + /** Effective summary settings (never a token). */ + summary: () => { enabled: boolean; maxChars: number } + /** summary:write token, or empty when summaries are not configured. */ + summaryToken: () => string /** Log sink for operator-visible diagnostics. */ log: (message: string) => void } @@ -114,15 +122,70 @@ export function conversationIdOf(session: SessionLike | undefined): string | und return typeof value === 'string' && value !== '' ? value : undefined } + +/** Text pulled out of one `user/message` or `assistant/message` payload. */ +export function textOf(value: unknown, depth = 0): string | undefined { + if (depth > 5) return undefined + if (typeof value === 'string') return value.trim() === '' ? undefined : value + if (Array.isArray(value)) { + const parts: string[] = [] + for (const item of value) { + if (item !== null && typeof item === 'object') { + const part = item as Record + // Tool calls/results are never summarized: they can carry files and secrets. + if (part.type === 'tool_use' || part.type === 'tool_result' || part.type === 'image') continue + if (part.type === 'text' && typeof part.text === 'string') { + parts.push(part.text) + continue + } + } + const nested = textOf(item, depth + 1) + if (nested !== undefined) parts.push(nested) + } + return parts.length === 0 ? undefined : parts.join('\n') + } + if (value === null || typeof value !== 'object') return undefined + const record = value as Record + for (const key of ['text', 'message', 'content', 'body']) { + const nested = textOf(record[key], depth + 1) + if (nested !== undefined) return nested + } + return undefined +} + +/** Local redaction: a summary must not carry credentials to the cloud. */ +const REDACTIONS: [RegExp, string][] = [ + [/-----BEGIN [A-Z ]+-----[\s\S]*?-----END [A-Z ]+-----/g, '[已脱敏:私钥]'], + [/\b(sk|ghp|gho|ghs|xox[abprs]|cmt|cmr)_[A-Za-z0-9_-]{12,}/g, '[已脱敏:令牌]'], + [/\bBearer\s+[A-Za-z0-9._-]{16,}/gi, 'Bearer [已脱敏]'], + [/\beyJ[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,}/g, '[已脱敏:JWT]'], + [/((?:password|passwd|pwd|secret|api[_-]?key|access[_-]?token)\s*[:=]\s*)[^\s,;]{6,}/gi, '$1[已脱敏]'], +] + +export function redactSecrets(text: string): string { + let out = text + for (const [pattern, replacement] of REDACTIONS) out = out.replace(pattern, replacement) + return out +} + +/** One buffered message, tagged with the turn it belongs to when known. */ +interface BufferedMessage extends SummaryMessage { + turn?: number +} + /** * Tracks open turns and reports each finished one. * * Reporting never blocks the agent loop: a failed POST is queued and retried by * {@link flush}, and a service outage only shows up in the status card. */ +/** A queued report is retried at most this many times before being dropped. */ +const MAX_RETRY_ATTEMPTS = 5 + export class Reporter { private readonly startedAt = new Map() private readonly queue: TurnPayload[] = [] + private readonly retries = new Map() private readonly recent: ReportRecord[] = [] private attempted = 0 private recorded = 0 @@ -132,6 +195,12 @@ export class Reporter { private lastSentAt: number | undefined private lastError: string | undefined private draining = false + /** Redacted message text per conversation, consumed at turn/end. */ + private readonly transcripts = new Map() + private summaryAttempted = 0 + private summaryStaged = 0 + private summaryFailed = 0 + private lastSummaryError: string | undefined constructor(private readonly options: ReporterOptions) {} @@ -152,6 +221,10 @@ export class Reporter { /** Handle any `session/event`; only `turn/start` and `turn/end` matter here. */ handleEvent(session: SessionLike | undefined, event: SessionEventLike): void { if (event === null || typeof event !== 'object') return + if (event.type === 'user/message' || event.type === 'assistant/message') { + this.collect(session, event) + return + } if (event.type === 'turn/start') { this.handleStart(session, event) return @@ -196,6 +269,69 @@ export class Reporter { completed_at: completedAt, } void this.send(payload) + void this.sendSummary(conversationId, key, status) + } + + /** Buffer one redacted message for the turn that is still open. */ + private collect(session: SessionLike | undefined, event: SessionEventLike): void { + const conversationId = conversationIdOf(session) + if (conversationId === undefined) return + const raw = textOf(event.data) + if (raw === undefined) return + const value = redactSecrets(raw).slice(0, 4000) + const buffer = this.transcripts.get(conversationId) ?? [] + buffer.push({ role: event.type === 'user/message' ? 'user' : 'assistant', text: value, turn: turnOf(event.data) }) + if (buffer.length > 120) buffer.splice(0, buffer.length - 120) + this.transcripts.set(conversationId, buffer) + } + + /** Take this turn's messages out of the buffer, capped by the char budget. */ + private takeTranscript(conversationId: string, turnKey: string): SummaryMessage[] { + const buffer = this.transcripts.get(conversationId) ?? [] + const turn = Number(turnKey.slice(turnKey.lastIndexOf(':') + 1)) + const picked: SummaryMessage[] = [] + const rest: BufferedMessage[] = [] + for (const item of buffer) { + const matches = Number.isFinite(turn) && (item.turn === undefined || item.turn === turn) + if (matches) picked.push({ role: item.role, text: item.text }) + else rest.push(item) + } + this.transcripts.set(conversationId, rest) + const maxChars = Math.max(1000, this.options.summary().maxChars) + let total = 0 + const kept: SummaryMessage[] = [] + for (const message of picked) { + if (total + message.text.length > maxChars) break + kept.push(message) + total += message.text.length + } + return kept + } + + /** Stage one summary; failures only surface in the status card. */ + private async sendSummary(conversationId: string, turnKey: string, status: TurnStatus): Promise { + const summary = this.options.summary() + const token = this.options.summaryToken() + if (!summary.enabled || token === '' || status !== 'completed') return + const messages = this.takeTranscript(conversationId, turnKey) + if (messages.length === 0) return + const payload: SummaryPayload = { conversation_id: conversationId, turn_id: turnKey, messages } + this.summaryAttempted += 1 + const result = await this.options.client.submitSummary(payload, token) + if (result.ok) { + this.summaryStaged += typeof result.data.staged === 'number' ? result.data.staged : 0 + return + } + if (result.error.status === 0 || result.error.status >= 500) { + const retry = await this.options.client.submitSummary(payload, token) + if (retry.ok) { + this.summaryStaged += typeof retry.data.staged === 'number' ? retry.data.staged : 0 + return + } + } + this.summaryFailed += 1 + this.lastSummaryError = describeError(result.error) + this.options.log(`cloud-memory: turn summary failed: ${this.lastSummaryError}`) } private push(record: ReportRecord): void { @@ -213,6 +349,7 @@ export class Reporter { const duplicate = result.data.duplicate === true || result.status === 200 if (duplicate) this.duplicate += 1 else this.recorded += 1 + this.retries.delete(payload.turn_id) this.lastSentAt = now this.push({ turnId: payload.turn_id, @@ -261,9 +398,20 @@ export class Reporter { if (!result.ok) { this.lastError = describeError(result.error) this.failed += 1 + const attempt = (this.retries.get(payload.turn_id) ?? 0) + 1 + if (attempt >= MAX_RETRY_ATTEMPTS) { + // Give up on this turn: a permanently failing endpoint must not keep + // an unbounded queue of metadata forever. + this.queue.shift() + this.retries.delete(payload.turn_id) + this.options.log(`cloud-memory: dropping turn report after ${attempt} attempts: ${this.lastError}`) + continue + } + this.retries.set(payload.turn_id, attempt) break } this.queue.shift() + this.retries.delete(payload.turn_id) sent += 1 if (result.data.duplicate === true || result.status === 200) this.duplicate += 1 else this.recorded += 1 @@ -284,6 +432,10 @@ export class Reporter { failed: this.failed, skipped: this.skipped, queued: this.queue.length, + summaryAttempted: this.summaryAttempted, + summaryStaged: this.summaryStaged, + summaryFailed: this.summaryFailed, + ...(this.lastSummaryError === undefined ? {} : { lastSummaryError: this.lastSummaryError }), ...(this.lastSentAt === undefined ? {} : { lastSentAt: this.lastSentAt }), ...(this.lastError === undefined ? {} : { lastError: this.lastError }), recent: [...this.recent], diff --git a/src/routes.ts b/src/routes.ts index d7542b8..90ad635 100644 --- a/src/routes.ts +++ b/src/routes.ts @@ -89,9 +89,10 @@ function fail(res: ServerResponse, status: number, message: string, asHtml = fal /** The payload the page and the client card both consume. */ function statusPayload(deps: RouteDeps): Record { const view = deps.store.describe() - const tokens: Record<'activity' | 'memory', TokenView> = { + const tokens: Record<'activity' | 'memory' | 'summary', TokenView> = { activity: view.activityToken, memory: view.memoryToken, + summary: view.summaryToken, } const settings = { enabled: view.enabled, @@ -99,6 +100,8 @@ function statusPayload(deps: RouteDeps): Record { clientId: view.clientId, requestTimeoutMs: view.requestTimeoutMs, defaultK: view.defaultK, + summaryEnabled: view.summaryEnabled, + summaryMaxChars: view.summaryMaxChars, } return { ok: true, settings, tokens, settingsPath: view.statePath, reporter: deps.reporter.snapshot() } } @@ -194,7 +197,19 @@ async function serveTest(deps: RouteDeps, req: IncomingMessage, res: ServerRespo : { ok: false, message: describeError(probe.error) } })() - json(res, 200, { ok: true, activity, memory }) + const summaryToken = deps.store.summaryToken() + const summary = !deps.store.settings().summaryEnabled + ? { ok: false, message: '自动摘要已关闭(可在下方勾选开启)' } + : summaryToken === '' + ? { ok: false, message: '未配置摘要令牌:留空则不做自动摘要' } + : await (async () => { + const probe = await client.probeSummary(summaryToken) + return probe.ok + ? { ok: true, message: '摘要令牌有效(只进待确认队列)' } + : { ok: false, message: describeError(probe.error) } + })() + + json(res, 200, { ok: true, activity, memory, summary }) } /** `POST /cloud-memory/flush`. */ @@ -215,6 +230,7 @@ async function serveFlush(deps: RouteDeps, req: IncomingMessage, res: ServerResp const TOKEN_LABELS = { activity: 'DSH desktop 活动上报', memory: 'DSH desktop 记忆读写', + summary: 'DSH desktop 对话摘要', } as const /** @@ -278,6 +294,7 @@ async function serveLogin(deps: RouteDeps, req: IncomingMessage, res: ServerResp const activity = await client.mintToken(session.data.sessionCookie, 'activity:write', TOKEN_LABELS.activity) const memory = await client.mintToken(session.data.sessionCookie, 'memory:write', TOKEN_LABELS.memory) + const summary = await client.mintToken(session.data.sessionCookie, 'summary:write', TOKEN_LABELS.summary) if (!activity.ok && !memory.ok) { const first = activity.ok ? memory : activity const reason = first.ok ? '无法创建令牌' : describeError(first.error) @@ -288,6 +305,7 @@ async function serveLogin(deps: RouteDeps, req: IncomingMessage, res: ServerResp const patch: Parameters[0] = {} if (activity.ok && typeof activity.data.token === 'string') patch.activityToken = activity.data.token if (memory.ok && typeof memory.data.token === 'string') patch.memoryToken = memory.data.token + if (summary.ok && typeof summary.data.token === 'string') patch.summaryToken = summary.data.token const applied = deps.store.patch(patch) if (!applied.ok) { json(res, 500, { ok: false, error: applied.error }) @@ -307,6 +325,7 @@ async function serveLogin(deps: RouteDeps, req: IncomingMessage, res: ServerResp }, activity: activity.ok, memory: memory.ok, + summary: summary.ok, warning: failed.ok ? undefined : `部分令牌没有创建成功:${describeError(failed.error)}`, }, }) diff --git a/src/store.ts b/src/store.ts index c7449f6..4223c74 100644 --- a/src/store.ts +++ b/src/store.ts @@ -26,6 +26,9 @@ export interface PersistedState { defaultK?: number activityToken?: string memoryToken?: string + summaryEnabled?: boolean + summaryMaxChars?: number + summaryToken?: string } /** Masked token view handed to the browser. */ @@ -38,6 +41,7 @@ export interface TokenView { export interface SettingsView extends Settings { activityToken: TokenView memoryToken: TokenView + summaryToken: TokenView statePath: string } @@ -100,6 +104,11 @@ function parseState(raw: string): PersistedState { if (activityToken !== undefined) state.activityToken = activityToken const memoryToken = cleanString(source.memoryToken) if (memoryToken !== undefined) state.memoryToken = memoryToken + if (typeof source.summaryEnabled === 'boolean') state.summaryEnabled = source.summaryEnabled + const summaryMaxChars = clampInt(source.summaryMaxChars, 1000, 16_000) + if (summaryMaxChars !== undefined) state.summaryMaxChars = summaryMaxChars + const summaryToken = cleanString(source.summaryToken) + if (summaryToken !== undefined) state.summaryToken = summaryToken return state } catch { return { version: 1 } @@ -158,6 +167,8 @@ export class SettingsStore { if (this.state.clientId !== undefined) resolved.clientId = this.state.clientId if (this.state.requestTimeoutMs !== undefined) resolved.requestTimeoutMs = this.state.requestTimeoutMs if (this.state.defaultK !== undefined) resolved.defaultK = this.state.defaultK + if (this.state.summaryEnabled !== undefined) resolved.summaryEnabled = this.state.summaryEnabled + if (this.state.summaryMaxChars !== undefined) resolved.summaryMaxChars = this.state.summaryMaxChars return resolved } @@ -171,15 +182,22 @@ export class SettingsStore { return this.state.memoryToken ?? envToken('DSH_CLOUD_MEMORY_MEMORY_TOKEN') ?? '' } + /** The summary:write bearer token (optional; absent disables summaries). */ + summaryToken(): string { + return this.state.summaryToken ?? envToken('DSH_CLOUD_MEMORY_SUMMARY_TOKEN') ?? '' + } + /** Masked snapshot for the browser card. */ describe(): SettingsView { const settings = this.settings() const activity = this.activityToken() const memory = this.memoryToken() + const summary = this.summaryToken() return { ...settings, activityToken: { configured: activity !== '', masked: activity === '' ? '' : maskToken(activity) }, memoryToken: { configured: memory !== '', masked: memory === '' ? '' : maskToken(memory) }, + summaryToken: { configured: summary !== '', masked: summary === '' ? '' : maskToken(summary) }, statePath: this.file, } } @@ -227,6 +245,21 @@ export class SettingsStore { if (defaultK === undefined) return { ok: false, error: 'defaultK must be a number' } next.defaultK = defaultK } + if (patch.summaryEnabled !== undefined) { + if (typeof patch.summaryEnabled !== 'boolean') return { ok: false, error: 'summaryEnabled must be a boolean' } + next.summaryEnabled = patch.summaryEnabled + } + if (patch.summaryMaxChars !== undefined) { + const summaryMaxChars = clampInt(patch.summaryMaxChars, 1000, 16_000) + if (summaryMaxChars === undefined) return { ok: false, error: 'summaryMaxChars must be a number' } + next.summaryMaxChars = summaryMaxChars + } + const summaryToken = cleanString(patch.summaryToken) + if (summaryToken !== undefined) { + if (!summaryToken.startsWith('cmt_')) return { ok: false, error: 'summary token must start with cmt_' } + next.summaryToken = summaryToken + } + if (patch.clearSummaryToken === true) delete next.summaryToken if (patch.clearActivityToken === true) delete next.activityToken if (patch.clearMemoryToken === true) delete next.memoryToken this.write(next) diff --git a/verify-reporter.mjs b/verify-reporter.mjs new file mode 100644 index 0000000..fc10c9a --- /dev/null +++ b/verify-reporter.mjs @@ -0,0 +1,108 @@ +// Runtime verification for the P0-4 client (turn reporter + CloudClient). +// Runs against the built lib/ output; no DSH host, no network, no real token. +import http from 'node:http' +import { Reporter } from './lib/reporter.js' +import { CloudClient } from './lib/api.js' +import { DEFAULT_SETTINGS, settingsFromConfig } from './lib/config.js' + +const results = [] +function check(name, ok, detail) { + results.push({ name, ok, detail }) + console.log(`${ok ? 'PASS' : 'FAIL'} ${name}${detail === undefined ? '' : ` :: ${detail}`}`) +} + +const session = { id: 'conv-1' } +const startEvent = { type: 'turn/start', time: 1_700_000_000_000, data: { turn: 7 } } +const endEvent = { type: 'turn/end', time: 1_700_000_010_000, data: { turn: 7 } } + +function reporterWith(client, overrides = {}) { + return new Reporter({ + client, + settings: () => ({ enabled: true, clientId: 'dsh-desktop' }), + activityToken: () => 'cmt_activity_token_for_test', + log: () => {}, + ...overrides, + }) +} + +// ---- 1. Payload shape: exactly six fields, no conversation content ---------- +{ + const sent = [] + const client = { reportTurn: async (payload, token) => { sent.push({ payload, token }); return { ok: true, status: 201, data: { recorded: true, duplicate: false } } } } + const reporter = reporterWith(client) + reporter.handleEvent(session, startEvent) + reporter.handleEvent(session, endEvent) + await new Promise(r => setTimeout(r, 20)) + const payload = sent[0]?.payload + const keys = payload === undefined ? [] : Object.keys(payload).sort() + check('payload has exactly 6 fields', JSON.stringify(keys) === JSON.stringify(['client_id', 'completed_at', 'conversation_id', 'started_at', 'status', 'turn_id']), keys.join(',')) + check('payload carries the activity token only', sent[0]?.token === 'cmt_activity_token_for_test') + check('payload has no chat body/tool/path fields', !keys.some(k => /content|prompt|message|tool|path|text|body/i.test(k)), keys.join(',')) + check('started_at <= completed_at', payload.started_at <= payload.completed_at && payload.status === 'completed', `${payload.started_at}..${payload.completed_at}`) + check('conversation and turn ids are the contract shape', payload.conversation_id === 'conv-1' && payload.turn_id === 'conv-1:7', payload.turn_id) +} + +// ---- 2. Disabled settings never touch the network --------------------------- +{ + let called = 0 + const reporter = reporterWith({ reportTurn: async () => { called += 1; return { ok: true, status: 201, data: {} } } }, { settings: () => ({ enabled: false, clientId: 'dsh-desktop' }) }) + reporter.handleEvent(session, endEvent) + await new Promise(r => setTimeout(r, 20)) + check('disabled settings skip without sending', called === 0 && reporter.snapshot().skipped === 1) +} + +// ---- 3. Missing token never sends ------------------------------------------- +{ + let called = 0 + const reporter = reporterWith({ reportTurn: async () => { called += 1; return { ok: true, status: 201, data: {} } } }, { activityToken: () => '' }) + reporter.handleEvent(session, endEvent) + await new Promise(r => setTimeout(r, 20)) + check('missing activity token skips without sending', called === 0 && reporter.snapshot().skipped === 1) +} + +// ---- 4. 4xx (other than 429) is not retried -------------------------------- +{ + let called = 0 + const reporter = reporterWith({ reportTurn: async () => { called += 1; return { ok: false, error: { status: 400, code: 'invalid_input', message: 'bad' } } } }) + reporter.handleEvent(session, endEvent) + await new Promise(r => setTimeout(r, 20)) + const flush = await reporter.flush() + check('400 is not queued for retry', called === 1 && flush.remaining === 0 && reporter.snapshot().queued === 0, `calls=${called}`) +} + +// ---- 5. 503 is queued and retried, then bounded ---------------------------- +{ + let calls = 0 + const client = { reportTurn: async () => { calls += 1; return { ok: false, error: { status: 503, code: 'upstream_unavailable', message: 'try later' } } } } + const reporter = reporterWith(client) + reporter.handleEvent(session, endEvent) + await new Promise(r => setTimeout(r, 20)) + check('503 is queued', reporter.snapshot().queued === 1) + for (let i = 0; i < 10; i += 1) await reporter.flush() + check('retries are bounded (queue drains after 5 attempts)', reporter.snapshot().queued === 0 && calls === 6, `calls=${calls}`) +} + +// ---- 6. Transport timeout is enforced by CloudClient ----------------------- +{ + const server = http.createServer((req, res) => { /* never answer */ }) + await new Promise(r => server.listen(0, '127.0.0.1', r)) + const port = server.address().port + const client = new CloudClient({ baseUrl: () => `http://127.0.0.1:${port}`, token: () => '', timeoutMs: () => 1000 }) + const started = Date.now() + const result = await client.reportTurn({ client_id: 'c', conversation_id: 'v', turn_id: 'v:1', status: 'completed', started_at: 1, completed_at: 2 }, 'cmt_activity') + const elapsed = Date.now() - started + server.close() + check('timeout aborts the request', result.ok === false && result.error.status === 0, `elapsed=${elapsed}ms code=${result.ok ? '-' : result.error.code}`) + check('timeout lands near the configured 1s', elapsed >= 900 && elapsed < 3000, `${elapsed}ms`) +} + +// ---- 7. Default and configurable timeout settings ------------------------- +{ + check('default request timeout is 5s', DEFAULT_SETTINGS.requestTimeoutMs === 5000, String(DEFAULT_SETTINGS.requestTimeoutMs)) + check('config cannot raise timeout beyond 60s', settingsFromConfig({ requestTimeoutMs: 120000 }).requestTimeoutMs === 60000) + check('config cannot lower timeout below 1s', settingsFromConfig({ requestTimeoutMs: 10 }).requestTimeoutMs === 1000) +} + +const failed = results.filter(r => !r.ok) +console.log(`\n${results.length - failed.length}/${results.length} checks passed`) +process.exit(failed.length === 0 ? 0 : 1)