File size: 7,515 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
import { matchSingleMediaPathTag } from '@moonshot-ai/agent-core-v2/agent/media/mediaRef';

export interface ExtractedWireMessage {
  readonly role: 'user' | 'assistant';
  readonly text: string;
  readonly time?: number;
  readonly stepUuid?: string;
}

export type TurnEffect =
  | { readonly kind: 'open'; readonly anchor: boolean }
  | { readonly kind: 'ensure' }
  | { readonly kind: 'undo'; readonly count: number }
  | { readonly kind: 'none' };

export type StepEffect =
  | { readonly kind: 'begin'; readonly uuid: string; readonly ordinal?: number }
  | { readonly kind: 'none' };

export interface WireLineAnalysis {
  readonly messages: ExtractedWireMessage[];
  readonly turn: TurnEffect;
  readonly step: StepEffect;
}

const NONE: TurnEffect = { kind: 'none' };
const ENSURE: TurnEffect = { kind: 'ensure' };
const STEP_NONE: StepEffect = { kind: 'none' };

const NON_USER_ORIGIN_KINDS: ReadonlySet<string> = new Set([
  'injection',
  'system_trigger',
  'retry',
  'compaction_summary',
]);

const HIDDEN_USER_ORIGINS: ReadonlySet<string> = new Set(['injection', 'system_trigger', 'retry']);
const TURN_OPENING_SYSTEM_TRIGGERS: ReadonlySet<string> = new Set([
  'goal_continuation',
  'subagent',
]);
const MARKER_USER_ORIGINS: ReadonlySet<string> = new Set([
  'skill_activation',
  'plugin_command',
  'compaction_summary',
]);

interface OriginLike {
  readonly kind?: unknown;
  readonly trigger?: unknown;
  readonly name?: unknown;
}

function isUserSlashPrompt(origin: OriginLike): boolean {
  return (
    (origin.kind === 'skill_activation' || origin.kind === 'plugin_command') &&
    origin.trigger === 'user-slash'
  );
}

function isUserTypedOrigin(origin: OriginLike): boolean {
  if (origin.kind === 'skill_activation' || origin.kind === 'plugin_command') {
    return origin.trigger === 'user-slash';
  }
  if (typeof origin.kind === 'string' && NON_USER_ORIGIN_KINDS.has(origin.kind)) return false;
  return true;
}

function normalizeTimestampMs(value: unknown): number | undefined {
  if (typeof value !== 'number' || !Number.isFinite(value) || value <= 0) return undefined;
  return value > 1e12 ? Math.floor(value) : Math.floor(value * 1000);
}

interface ContentPartLike {
  readonly type?: unknown;
  readonly text?: unknown;
}

function textOfContent(content: unknown): string {
  if (!Array.isArray(content)) return '';
  let text = '';
  for (const raw of content as readonly unknown[]) {
    if (raw === null || typeof raw !== 'object') continue;
    const part = raw as ContentPartLike;
    if (part.type !== 'text' || typeof part.text !== 'string') continue;
    if (matchSingleMediaPathTag(part.text) !== undefined) continue;
    text += part.text;
  }
  return text;
}

interface ParsedWireRecord {
  readonly type?: unknown;
  readonly time?: unknown;
  readonly message?: unknown;
  readonly event?: unknown;
  readonly count?: unknown;
}

function parseWireLine(line: string): ParsedWireRecord | undefined {
  const trimmed = line.trim();
  if (trimmed.length === 0) return undefined;
  let record: unknown;
  try {
    record = JSON.parse(trimmed);
  } catch {
    return undefined;
  }
  if (record === null || typeof record !== 'object' || Array.isArray(record)) return undefined;
  return record as ParsedWireRecord;
}

function turnEffectOfAppendMessage(message: unknown): TurnEffect {
  if (message === null || typeof message !== 'object') return NONE;
  const m = message as { role?: unknown; origin?: unknown };
  if (m.role === 'system') return NONE;
  if (m.role === 'assistant') return ENSURE;
  if (m.role !== 'user') return NONE;

  const origin =
    m.origin !== null && typeof m.origin === 'object' ? (m.origin as OriginLike) : undefined;
  const kind = origin?.kind;
  if (typeof kind === 'string' && HIDDEN_USER_ORIGINS.has(kind)) {
    if (
      kind === 'system_trigger' &&
      typeof origin?.name === 'string' &&
      TURN_OPENING_SYSTEM_TRIGGERS.has(origin.name)
    ) {
      return { kind: 'open', anchor: false };
    }
    return NONE;
  }
  if (typeof kind === 'string' && MARKER_USER_ORIGINS.has(kind)) {
    if (origin !== undefined && isUserSlashPrompt(origin)) return { kind: 'open', anchor: true };
    return NONE;
  }
  const anchor = kind === undefined || kind === 'user';
  return { kind: 'open', anchor };
}

export function analyzeWireLine(line: string): WireLineAnalysis {
  const r = parseWireLine(line);
  if (r === undefined) return { messages: [], turn: NONE, step: STEP_NONE };
  const time = normalizeTimestampMs(r.time);

  if (r.type === 'context.append_message') {
    const turn = turnEffectOfAppendMessage(r.message);
    const message = r.message;
    const messages: ExtractedWireMessage[] = [];
    if (message !== null && typeof message === 'object') {
      const m = message as { role?: unknown; content?: unknown; origin?: unknown };
      if (m.role === 'user') {
        const origin = m.origin;
        const userTyped =
          origin === null ||
          origin === undefined ||
          (typeof origin === 'object' && isUserTypedOrigin(origin as OriginLike));
        if (userTyped) {
          const text = textOfContent(m.content).trim();
          if (text.length > 0) messages.push({ role: 'user', text, time });
        }
      }
    }
    return { messages, turn, step: STEP_NONE };
  }

  if (r.type === 'context.append_loop_event') {
    const event = r.event;
    if (event === null || typeof event !== 'object') {
      return { messages: [], turn: NONE, step: STEP_NONE };
    }
    const e = event as {
      type?: unknown;
      part?: unknown;
      uuid?: unknown;
      step?: unknown;
      stepUuid?: unknown;
    };
    const messages: ExtractedWireMessage[] = [];
    if (e.type === 'step.begin') {
      if (typeof e.uuid !== 'string' || e.uuid.length === 0) {
        return { messages: [], turn: NONE, step: STEP_NONE };
      }
      const ordinal =
        typeof e.step === 'number' && Number.isSafeInteger(e.step) && e.step > 0
          ? e.step
          : undefined;
      return { messages: [], turn: NONE, step: { kind: 'begin', uuid: e.uuid, ordinal } };
    }
    const stepUuid =
      typeof e.stepUuid === 'string' && e.stepUuid.length > 0 ? e.stepUuid : undefined;
    let turn: TurnEffect = NONE;
    if (e.type === 'content.part') {
      const part = e.part;
      if (part !== null && typeof part === 'object') {
        const p = part as ContentPartLike & { think?: unknown; encrypted?: unknown };
        if (p.type === 'text' && typeof p.text === 'string') {
          const text = p.text.trim();
          if (text.length > 0) {
            messages.push({ role: 'assistant', text, time, stepUuid });
            turn = ENSURE;
          }
        } else if (p.type === 'think' && typeof p.think === 'string') {
          if (p.think.trim().length > 0 || p.encrypted !== undefined) turn = ENSURE;
        } else {
          turn = ENSURE;
        }
      }
    } else if (e.type === 'tool.call') {
      turn = ENSURE;
    }
    return { messages, turn, step: STEP_NONE };
  }

  if (r.type === 'context.undo') {
    const count = r.count;
    if (typeof count === 'number' && Number.isSafeInteger(count) && count > 0) {
      return { messages: [], turn: { kind: 'undo', count }, step: STEP_NONE };
    }
    return { messages: [], turn: NONE, step: STEP_NONE };
  }

  return { messages: [], turn: NONE, step: STEP_NONE };
}

export function extractFromWireLine(line: string): ExtractedWireMessage[] {
  return analyzeWireLine(line).messages;
}