Files
Agent-Workbench/src/core/companion/projection.mjs
T
Agent Workbench Dev e306f40cae
Official contracts and Windows source CI / windows-source (push) Has been cancelled
feat(memory): release Mnemo project bridge and schema2 recovery
2026-11-11 03:03:43 +08:00

53 lines
5.7 KiB
JavaScript

import { approvalBinding } from '../../shared/contracts/companion.mjs';
import { approvalKind } from '../../shared/approval-presentation.mjs';
import { FileResults } from '../tasks/file-results.mjs';
export const pick = (value, names) => Object.fromEntries(names.filter(key => value?.[key] !== undefined).map(key => [key, structuredClone(value[key])]));
export const reportKinds = new Set(['delivery-report', 'delivery-report-markdown']);
export const sessionBrief = (session, redact = String) => ({ ...pick(session, ['id', 'projectId', 'taskId', 'runId', 'status', 'revision', 'eventSeq', 'updatedAt']), title: redact(String(session.title).slice(0, 240)) });
export function mobileSession(store, runtime, session, redact = String) {
const task = store.core.task.get(session.taskId), run = store.core.run.get(session.runId);
const tools = [...store.core.tool.values()].filter(value => value.sessionId === session.id && value.taskId === session.taskId);
const checks = [...store.core.check_job.values()].filter(value => value.sessionId === session.id && value.taskId === session.taskId);
const result = {
...sessionBrief(session, redact),
browserObservations: [...(store.core.browsing_observation?.values() ?? [])].filter(value => value.sessionId === session.id && value.taskId === session.taskId).slice(-4)
.map(value => pick(value, ['id','tabId','url','title','createdAt','revision','scope','screenshotSha256'])),
inputAttachments:[...store.core.input_attachment.values()].filter(row=>row.sessionId===session.id&&row.taskId===session.taskId&&row.status==='attached')
.map(row=>pick(row,['id','name','status','mediaType','taskId','runId','sha256','bytes','sentSha256','sentBytes','width','height','sentWidth','sentHeight','orientation'])),
budgetPause:run?.budgetPause?pick(run.budgetPause,['reason','requiredInputTokens','contextLimit','inputBudget','outputReservation','safetyTokens','imageReservationTokens','breakdown']):null,
task: task ? { ...pick(task, ['id', 'status', 'budget', 'usage', 'verification']), goal: String(task.goal).slice(0, 12000) } : null,
run: run ? pick(run, ['id', 'taskId', 'status', 'budget', 'usage', 'steps', 'toolCalls', 'stopReason', 'startedAt', 'finishedAt']) : null,
progress: { total: tools.length, completed: tools.filter(value => value.state === 'completed').length,
unknown: tools.filter(value => value.requiresVerification).map(value => pick(value, ['id', 'tool', 'state', 'runId'])) },
events: session.events.slice(-60).map(value => ({ ...pick(value, ['id', 'kind', 'phase', 'taskId', 'runId', 'createdAt', 'exitCode', 'durationMs']),
label: String(value.label ?? value.tool ?? '').slice(0, 240), output: String(value.output ?? '').slice(-4000),
...(value.stream ? { stream: { ...pick(value.stream, ['status', 'protocol', 'sequence', 'bytes', 'textCharacters', 'toolFragments']), textPreview: String(value.stream.textPreview ?? '').slice(0, 4000) } } : {}) })),
messages: session.messages.filter(value => ['assistant', 'system'].includes(value.role)).slice(-8).map(value => ({ ...pick(value, ['id', 'role', 'createdAt']), content: String(value.content).slice(0, 12000) })),
approvals: session.approvals.filter(value => value.status === 'pending' && value.taskId === session.taskId && value.runId === session.runId).map(value => ({
...pick(value, ['id', 'taskId', 'runId', 'projectId', 'command', 'reason', 'shell', 'createdAt']),
kind: approvalKind(value), fingerprint: approvalBinding(session, value), actionable: runtime.waiters.get(value.id)?.runId === session.runId })),
changes: session.changes.filter(value => value.taskId === session.taskId).slice(-100).map(value => pick(value, ['id', 'path', 'reason', 'status', 'sha256', 'baseSha256', 'additions', 'deletions'])),
checks: checks.slice(-12).map(value => ({ ...pick(value, ['id', 'status', 'summary', 'requiresVerification', 'createdAt', 'finishedAt']),
results: (value.results ?? []).map(item => pick(item, ['name', 'status', 'exitCode', 'tests', 'durationMs'])) })),
reports: [...store.core.artifact.values()].filter(value => value.sessionId === session.id && value.taskId === session.taskId && reportKinds.has(value.kind))
.slice(-10).map(value => pick(value, ['id', 'kind', 'sha256', 'bytes', 'createdAt', 'taskId', 'runId'])),
};
if (task?.delivery) result.task.delivery = pick(task.delivery, ['status', 'paths', 'at', 'checks']);
const mnemoPlans = [...store.core.context_plan.values()].filter(plan => plan.sessionId === session.id && plan.taskId === session.taskId
&& (plan.mnemoRecallArtifactId || plan.mnemoIssue));
if (mnemoPlans.length) result.memoryReceipts = mnemoPlans.map(plan => pick(plan, ['id', 'runId', 'mnemoRecallArtifactId', 'mnemoMemoryArtifactId',
'mnemoRecallContextDigest', 'mnemoSelectedContextDigest', 'mnemoSelectedRefs', 'mnemoIssue', 'mnemoDispatches']));
if ([...store.core.file_result.values()].some(row => row.sessionId === session.id && row.taskId === session.taskId)) {
result.fileResults = new FileResults(store).inspect(session).results.slice(0, 8).map(row => ({
...pick(row, ['id', 'taskId', 'runId', 'createdAt', 'verification', 'contractSha256']), reason: row.reason,
files: row.files.map(file => ({ ...pick(file, ['path', 'sha256', 'bytes']), origin: file.origin ? pick(file.origin, ['taskId', 'runId', 'sha256']) : null })),
}));
}
// Only this explicit projection crosses the remote boundary. Never expose
// providerMessages, model configuration, tool input, host paths or full exports.
const clean = value => typeof value === 'string' ? redact(value) : Array.isArray(value) ? value.map(clean)
: value && typeof value === 'object' ? Object.fromEntries(Object.entries(value).map(([key, entry]) => [key, clean(entry)])) : value;
return clean(result);
}