File size: 5,430 Bytes
4e23b01 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 | import type {
PermissionMode,
SessionActivityState,
TokenUsage,
UsageStatus,
} from '@moonshot-ai/agent-core-v2';
import type {
SessionStateGoal,
SessionStateMessage,
SessionStateModes,
SessionStateUsage,
StepUsage,
} from '../../protocol/messages';
interface GoalSnapshotLike {
readonly objective: string;
readonly status: 'active' | 'paused' | 'blocked' | 'complete';
readonly completionCriterion?: string;
readonly tokensUsed: number;
readonly budget: { readonly tokenBudget: number | null };
}
export class SessionStateAggregator {
private sessionActivity: SessionActivityState | undefined;
private model: string | undefined;
private thinkingEffort: string | undefined;
private permission: 'manual' | 'yolo' | 'auto' | undefined;
private usage: SessionStateUsage | undefined;
private contextTokens: number | undefined;
private maxContextTokens: number | undefined;
private goal: SessionStateGoal | null | undefined;
private planMode = false;
private swarmMode = false;
private planRevision: { path: string; version: number } | undefined;
private lastEmittedJson: string | undefined;
feedSessionActivity(state: SessionActivityState): void {
this.sessionActivity = state;
}
feedMainStatus(event: {
model?: string;
thinkingEffort?: string;
usage?: UsageStatus;
contextTokens?: number;
maxContextTokens?: number;
planMode?: boolean;
swarmMode?: boolean;
permission?: 'manual' | 'yolo' | 'auto';
}): void {
if (event.model !== undefined) this.model = event.model;
if (event.thinkingEffort !== undefined) this.thinkingEffort = event.thinkingEffort;
if (event.usage !== undefined) this.usage = usageToWire(event.usage);
if (event.contextTokens !== undefined) this.contextTokens = event.contextTokens;
if (event.maxContextTokens !== undefined) this.maxContextTokens = event.maxContextTokens;
if (event.planMode !== undefined) this.planMode = event.planMode;
if (event.swarmMode !== undefined) this.swarmMode = event.swarmMode;
if (event.permission !== undefined) this.permission = event.permission;
}
feedSeed(seed: {
model?: string;
thinkingEffort?: string;
usage?: UsageStatus;
contextTokens?: number;
maxContextTokens?: number;
permission?: PermissionMode;
}): void {
if (seed.model !== undefined) this.model = seed.model;
if (seed.thinkingEffort !== undefined) this.thinkingEffort = seed.thinkingEffort;
if (seed.usage !== undefined) this.usage = usageToWire(seed.usage);
if (seed.contextTokens !== undefined) this.contextTokens = seed.contextTokens;
if (seed.maxContextTokens !== undefined) this.maxContextTokens = seed.maxContextTokens;
if (seed.permission !== undefined) this.permission = seed.permission;
}
feedGoal(snapshot: GoalSnapshotLike | null): void {
this.goal =
snapshot === null
? null
: {
objective: snapshot.objective,
status: snapshot.status,
completion_criterion: snapshot.completionCriterion,
budget_used: snapshot.tokensUsed,
budget_limit: snapshot.budget.tokenBudget ?? undefined,
};
}
feedPlanRevision(path: string, version: number): void {
this.planRevision = { path, version };
}
snapshot(sessionId: string): SessionStateMessage {
return this.build(sessionId);
}
changed(sessionId: string): SessionStateMessage | undefined {
const next = this.build(sessionId);
const { timestamp: _timestamp, ...comparable } = next;
const json = JSON.stringify(comparable);
if (json === this.lastEmittedJson) return undefined;
this.lastEmittedJson = json;
return next;
}
private build(sessionId: string): SessionStateMessage {
const busy = this.sessionActivity?.busy ?? false;
const modes = this.computeModes();
return {
type: 'session.state',
session_id: sessionId,
timestamp: Date.now(),
status: busy ? 'running' : 'idle',
pending_interaction: this.sessionActivity?.pendingInteraction,
model: this.model,
thinking_effort: this.thinkingEffort,
permission: this.permission,
usage: this.usage,
context_tokens: this.contextTokens,
max_context_tokens: this.maxContextTokens,
goal: this.goal ?? undefined,
modes,
};
}
private computeModes(): SessionStateModes | undefined {
const modes: SessionStateModes = {};
if (this.planMode) {
modes.plan = {
review_path: this.planRevision?.path,
version: this.planRevision?.version,
};
}
if (this.swarmMode) modes.swarm = {};
return modes.plan === undefined && modes.swarm === undefined ? undefined : modes;
}
}
function usageToWire(usage: UsageStatus): SessionStateUsage {
return {
by_model:
usage.byModel === undefined
? undefined
: Object.fromEntries(
Object.entries(usage.byModel).map(([model, u]) => [model, toSnakeUsage(u)]),
),
current_turn: usage.currentTurn === undefined ? undefined : toSnakeUsage(usage.currentTurn),
total: usage.total === undefined ? undefined : toSnakeUsage(usage.total),
};
}
function toSnakeUsage(usage: TokenUsage): StepUsage {
return {
input_other: usage.inputOther,
output: usage.output,
input_cache_read: usage.inputCacheRead,
input_cache_creation: usage.inputCacheCreation,
};
}
|