File size: 8,874 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
230
231
232
233
234
235
import { describe, expect, it } from 'vitest';

import type { ContextMessage } from '@moonshot-ai/agent-core-v2';

import { projectPromptContentParts, toProtocolMessage } from '../../../src/services/messages/messageProjection';

const SESSION_ID = 'session_1';
const CREATED_AT = 1_700_000_000_000;

function userText(text: string): ContextMessage {
  return { role: 'user', content: [{ type: 'text', text }], toolCalls: [] };
}

describe('toProtocolMessage', () => {
  it('maps text/think/image/audio/video content parts', () => {
    const msg: ContextMessage = {
      role: 'user',
      content: [
        { type: 'text', text: 'hello' },
        { type: 'think', think: 'hmm', encrypted: 'sig-1' },
        { type: 'image_url', imageUrl: { url: 'https://example.com/a.png' } },
        { type: 'audio_url', audioUrl: { url: 'https://example.com/a.mp3' } },
        { type: 'video_url', videoUrl: { url: 'https://example.com/a.mp4' } },
      ],
      toolCalls: [],
    };

    expect(toProtocolMessage(SESSION_ID, 0, msg, CREATED_AT).content).toEqual([
      { type: 'text', text: 'hello' },
      { type: 'thinking', thinking: 'hmm', signature: 'sig-1' },
      { type: 'image', source: { kind: 'url', url: 'https://example.com/a.png' } },
      { type: 'text', text: '[audio:https://example.com/a.mp3]' },
      { type: 'video', source: { kind: 'url', url: 'https://example.com/a.mp4' } },
    ]);
  });

  it('projects a daemon-ref image part to a session_media source', () => {
    const msg: ContextMessage = {
      role: 'user',
      content: [
        { type: 'text', text: 'what is this?' },
        { type: 'image_url', imageUrl: { url: 'kimi-file://file_9?path=%2Fcache%2Fpic.png' } },
      ],
      toolCalls: [],
    };

    expect(toProtocolMessage(SESSION_ID, 0, msg, CREATED_AT).content).toEqual([
      { type: 'text', text: 'what is this?' },
      { type: 'image', source: { kind: 'session_media', file_id: 'file_9' } },
    ]);
  });

  it('preserves media names in live and prompt projections', () => {
    const part = {
      type: 'image_url' as const,
      imageUrl: { url: 'kimi-file://file_9', id: 'file_9', name: 'photo.png' },
    };
    const msg: ContextMessage = { role: 'user', content: [part], toolCalls: [] };

    expect(toProtocolMessage(SESSION_ID, 0, msg, CREATED_AT).content).toEqual([
      { type: 'image', source: { kind: 'session_media', file_id: 'file_9' }, name: 'photo.png' },
    ]);
    expect(projectPromptContentParts([part])).toEqual([
      { type: 'image', source: { kind: 'session_media', file_id: 'file_9' }, name: 'photo.png' },
    ]);
  });

  it('keeps a legacy tag+ref pair as text plus the ref projection', () => {
    const msg: ContextMessage = {
      role: 'user',
      content: [
        { type: 'text', text: '<image path="/cache/pic.png"></image>' },
        { type: 'image_url', imageUrl: { url: 'kimi-file://file_9?path=%2Fcache%2Fpic.png' } },
      ],
      toolCalls: [],
    };

    expect(toProtocolMessage(SESSION_ID, 0, msg, CREATED_AT).content).toEqual([
      { type: 'text', text: '<image path="/cache/pic.png"></image>' },
      { type: 'image', source: { kind: 'session_media', file_id: 'file_9' } },
    ]);
  });

  it('keeps a bare <media path> tag as text in user messages', () => {
    const msg: ContextMessage = {
      role: 'user',
      content: [
        { type: 'text', text: '<video path="/cache/clip.mp4">' },
        { type: 'text', text: 'watch this' },
      ],
      toolCalls: [],
    };

    expect(toProtocolMessage(SESSION_ID, 0, msg, CREATED_AT).content).toEqual([
      { type: 'text', text: '<video path="/cache/clip.mp4">' },
      { type: 'text', text: 'watch this' },
    ]);
  });

  it('passes assistant tag-shaped text through verbatim', () => {
    const msg: ContextMessage = {
      role: 'assistant',
      content: [{ type: 'text', text: '<image path="/cache/out.png"></image>' }],
      toolCalls: [],
    };

    expect(toProtocolMessage(SESSION_ID, 0, msg, CREATED_AT).content).toEqual([
      { type: 'text', text: '<image path="/cache/out.png"></image>' },
    ]);
  });

  it('projects a kimi-file video reference to a structured file source without leaking the path', () => {
    const msg: ContextMessage = {
      role: 'user',
      content: [
        { type: 'video_url', videoUrl: { url: 'kimi-file://file_9?path=%2Fcache%2Fclip.mp4' } },
      ],
      toolCalls: [],
    };

    expect(toProtocolMessage(SESSION_ID, 0, msg, CREATED_AT).content).toEqual([
      { type: 'video', source: { kind: 'session_media', file_id: 'file_9' } },
    ]);
  });

  it('projects a provider video url to a structured url source carrying its id', () => {
    const msg: ContextMessage = {
      role: 'user',
      content: [{ type: 'video_url', videoUrl: { url: 'ms://prov-7', id: 'prov-7' } }],
      toolCalls: [],
    };

    expect(toProtocolMessage(SESSION_ID, 0, msg, CREATED_AT).content).toEqual([
      { type: 'video', source: { kind: 'url', url: 'ms://prov-7', id: 'prov-7' } },
    ]);
  });

  it('appends assistant tool calls as tool_use parts with parsed input', () => {
    const msg: ContextMessage = {
      role: 'assistant',
      content: [{ type: 'text', text: 'running' }],
      toolCalls: [
        { type: 'function', id: 'call_1', name: 'Bash', arguments: '{"cmd":"ls"}' },
        { type: 'function', id: 'call_2', name: 'Broken', arguments: '{not json' },
      ],
    };

    expect(toProtocolMessage(SESSION_ID, 0, msg, CREATED_AT).content).toEqual([
      { type: 'text', text: 'running' },
      { type: 'tool_use', tool_call_id: 'call_1', tool_name: 'Bash', input: { cmd: 'ls' } },
      { type: 'tool_use', tool_call_id: 'call_2', tool_name: 'Broken', input: '{not json' },
    ]);
  });

  it('flattens a plain-text tool result into the tool_result output', () => {
    const result: ContextMessage = {
      role: 'tool',
      content: [{ type: 'text', text: 'image result' }],
      toolCalls: [],
      toolCallId: 'call_image',
      note: '<system>Image compressed.</system>',
    };

    expect(toProtocolMessage(SESSION_ID, 0, result, 0).content).toEqual([
      { type: 'tool_result', tool_call_id: 'call_image', output: 'image result' },
    ]);
  });

  it('passes raw media parts through as the tool_result output', () => {
    const result: ContextMessage = {
      role: 'tool',
      content: [
        { type: 'text', text: 'image result' },
        { type: 'image_url', imageUrl: { url: 'data:image/png;base64,AAAA' } },
      ],
      toolCalls: [],
      toolCallId: 'call_media',
    };

    expect(toProtocolMessage(SESSION_ID, 0, result, 0).content).toEqual([
      { type: 'tool_result', tool_call_id: 'call_media', output: result.content },
    ]);
  });

  it('marks failed tool results with is_error', () => {
    const result: ContextMessage = {
      role: 'tool',
      content: [{ type: 'text', text: 'boom' }],
      toolCalls: [],
      toolCallId: 'call_err',
      isError: true,
    };

    expect(toProtocolMessage(SESSION_ID, 0, result, 0).content).toEqual([
      { type: 'tool_result', tool_call_id: 'call_err', output: 'boom', is_error: true },
    ]);
  });

  it('prefers the stored message id and falls back to the transcript index', () => {
    const withId: ContextMessage = { ...userText('a'), id: 'msg_custom' };
    expect(toProtocolMessage(SESSION_ID, 7, withId, CREATED_AT).id).toBe('msg_custom');
    expect(toProtocolMessage(SESSION_ID, 7, userText('a'), CREATED_AT).id).toBe(
      `msg_${SESSION_ID}_000007`,
    );
  });

  it('stamps created_at from the override or the session-created fallback', () => {
    const msg = userText('a');
    expect(toProtocolMessage(SESSION_ID, 3, msg, CREATED_AT, CREATED_AT + 999).created_at).toBe(
      new Date(CREATED_AT + 999).toISOString(),
    );
    expect(toProtocolMessage(SESSION_ID, 3, msg, CREATED_AT).created_at).toBe(
      new Date(CREATED_AT + 3).toISOString(),
    );
  });

  it('carries origin into metadata and omits metadata otherwise', () => {
    const withOrigin: ContextMessage = { ...userText('a'), origin: { kind: 'user' } };
    expect(toProtocolMessage(SESSION_ID, 0, withOrigin, CREATED_AT).metadata).toEqual({
      origin: { kind: 'user' },
    });
    expect(toProtocolMessage(SESSION_ID, 0, userText('a'), CREATED_AT)).not.toHaveProperty(
      'metadata',
    );
  });

  it('retains composer metadata separately from the projected model content', () => {
    const clientMetadata = [{ composer: { version: 1, refs: ['example-ref'] } }];
    const msg: ContextMessage = { ...userText('visible'), origin: { kind: 'user', clientMetadata } };
    const projected = toProtocolMessage(SESSION_ID, 0, msg, CREATED_AT);
    expect(projected.content).toEqual([{ type: 'text', text: 'visible' }]);
    expect(projected.metadata).toEqual({ origin: { kind: 'user', clientMetadata } });
  });
});