File size: 2,833 Bytes
682b227
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { describe, it, expect } from 'vitest';
import { MessageRole } from '$lib/enums';

/**
 * Tests for the new reasoning content handling.
 * In the new architecture, reasoning content is stored in a dedicated
 * `reasoningContent` field on DatabaseMessage, not embedded in content with tags.
 * The API sends it as `reasoning_content` on ApiChatMessageData.
 */

describe('reasoning content in new structured format', () => {
	it('reasoning is stored as separate field, not in content', () => {
		// Simulate what the new chat store does
		const message = {
			content: 'The answer is 4.',
			reasoningContent: 'Let me think: 2+2=4, basic arithmetic.'
		};

		// Content should be clean
		expect(message.content).not.toContain('<<<');
		expect(message.content).toBe('The answer is 4.');

		// Reasoning in dedicated field
		expect(message.reasoningContent).toBe('Let me think: 2+2=4, basic arithmetic.');
	});

	it('convertDbMessageToApiChatMessageData includes reasoning_content', () => {
		// Simulate the conversion logic
		const dbMessage = {
			role: MessageRole.ASSISTANT,
			content: 'The answer is 4.',
			reasoningContent: 'Let me think: 2+2=4, basic arithmetic.'
		};

		const apiMessage: Record<string, unknown> = {
			role: dbMessage.role,
			content: dbMessage.content
		};
		if (dbMessage.reasoningContent) {
			apiMessage.reasoning_content = dbMessage.reasoningContent;
		}

		expect(apiMessage.content).toBe('The answer is 4.');
		expect(apiMessage.reasoning_content).toBe('Let me think: 2+2=4, basic arithmetic.');
		// No internal tags leak into either field
		expect(apiMessage.content).not.toContain('<<<');
		expect(apiMessage.reasoning_content).not.toContain('<<<');
	});

	it('API message excludes reasoning when excludeReasoningFromContext is true', () => {
		const dbMessage = {
			role: MessageRole.ASSISTANT,
			content: 'The answer is 4.',
			reasoningContent: 'internal thinking'
		};

		const excludeReasoningFromContext = true;

		const apiMessage: Record<string, unknown> = {
			role: dbMessage.role,
			content: dbMessage.content
		};
		if (!excludeReasoningFromContext && dbMessage.reasoningContent) {
			apiMessage.reasoning_content = dbMessage.reasoningContent;
		}

		expect(apiMessage.content).toBe('The answer is 4.');
		expect(apiMessage.reasoning_content).toBeUndefined();
	});

	it('handles messages with no reasoning', () => {
		const dbMessage = {
			role: MessageRole.ASSISTANT,
			content: 'No reasoning here.',
			reasoningContent: undefined
		};

		const apiMessage: Record<string, unknown> = {
			role: dbMessage.role,
			content: dbMessage.content
		};
		if (dbMessage.reasoningContent) {
			apiMessage.reasoning_content = dbMessage.reasoningContent;
		}

		expect(apiMessage.content).toBe('No reasoning here.');
		expect(apiMessage.reasoning_content).toBeUndefined();
	});
});