-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmessage.ts
More file actions
executable file
·242 lines (230 loc) · 6.07 KB
/
message.ts
File metadata and controls
executable file
·242 lines (230 loc) · 6.07 KB
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
236
237
238
239
240
241
242
/**
* Message content types and structures for the BB Tools Framework.
* Defines the core types used to represent different kinds of content
* in messages, including text, images, tool uses, and tool results.
*
* @module
*/
/**
* Supported MIME types for image content blocks.
* Defines the allowed image formats that can be included in messages.
*
* @example
* ```ts
* const mediaType: LLMMessageContentPartImageBlockSourceMediaType = 'image/png';
* ```
*/
export type LLMMessageContentPartImageBlockSourceMediaType =
| 'image/jpeg'
| 'image/png'
| 'image/gif'
| 'image/webp';
/**
* Base interface that all message content parts must implement.
* Provides the common 'type' field used to discriminate between different content types.
*
* @example
* ```ts
* const basePart: LLMMessageContentPartBase = {
* type: 'text'
* };
* ```
*/
export interface LLMMessageContentPartBase {
type: string;
}
/**
* Represents a block of text content in a message.
* The most basic and common content type used in messages.
*
* @example
* ```ts
* const textBlock: LLMMessageContentPartTextBlock = {
* type: 'text',
* text: 'Hello world!'
* };
* ```
*/
export interface LLMMessageContentPartTextBlock extends LLMMessageContentPartBase {
messageId?: string;
type: 'text';
text: string;
}
/**
* Represents a thinking block in a message.
* Contains the model's reasoning process (e.g., Claude's extended thinking).
*
* @example
* ```ts
* const thinkingBlock: LLMMessageContentPartThinkingBlock = {
* type: 'thinking',
* thinking: 'Let me analyze this step by step...'
* };
* ```
*/
export interface LLMMessageContentPartThinkingBlock extends LLMMessageContentPartBase {
messageId?: string;
type: 'thinking';
thinking: string;
signature?: string;
}
/**
* Represents a redacted thinking block in a message.
* Contains encrypted or obfuscated reasoning data.
*
* @example
* ```ts
* const redactedBlock: LLMMessageContentPartRedactedThinkingBlock = {
* type: 'redacted_thinking',
* data: 'base64encodeddata...'
* };
* ```
*/
export interface LLMMessageContentPartRedactedThinkingBlock extends LLMMessageContentPartBase {
messageId?: string;
type: 'redacted_thinking';
data: string;
}
/**
* Represents an image in a message.
* Images are stored as base64-encoded data with associated media type.
*
* @example
* ```ts
* const imageBlock: LLMMessageContentPartImageBlock = {
* type: 'image',
* source: {
* type: 'base64',
* media_type: 'image/png',
* data: 'base64EncodedImageData...'
* }
* };
* ```
*/
export interface LLMMessageContentPartImageBlock extends LLMMessageContentPartBase {
messageId?: string;
type: 'image';
source: {
type: 'base64';
media_type: LLMMessageContentPartImageBlockSourceMediaType;
data: string;
};
}
/**
* Represents an audio block in a message (OpenAI).
* References a previous audio response from the model.
*
* @example
* ```ts
* const audioBlock: LLMMessageContentPartAudioBlock = {
* type: 'audio',
* id: 'audio_abc123'
* };
* ```
*/
export interface LLMMessageContentPartAudioBlock extends LLMMessageContentPartBase {
messageId?: string;
type: 'audio';
id: string; // Unique identifier for a previous audio response from the model
}
/**
* Represents a tool invocation in a message.
* Contains the tool name and input parameters used for the invocation.
*
* @example
* ```ts
* const toolUseBlock: LLMMessageContentPartToolUseBlock = {
* type: 'tool_use',
* id: 'tool-123',
* name: 'search_files',
* input: { pattern: '*.ts' }
* };
* ```
*/
export interface LLMMessageContentPartToolUseBlock extends LLMMessageContentPartBase {
messageId?: string;
type: 'tool_use';
id: string;
input: object;
name: string;
}
/**
* Represents the result of a tool invocation.
* Includes success status and resulting content which may be nested content parts.
*
* @example
* ```ts
* const resultBlock: LLMMessageContentPartToolResultBlock = {
* type: 'tool_result',
* tool_use_id: 'tool-123',
* content: [{ type: 'text', text: 'Found 5 files' }],
* is_error: false
* };
* ```
*/
export interface LLMMessageContentPartToolResultBlock extends LLMMessageContentPartBase {
messageId?: string;
type: 'tool_result';
tool_use_id?: string;
content?: Array<LLMMessageContentPartTextBlock | LLMMessageContentPartImageBlock>;
is_error?: boolean;
}
/**
* Union type encompassing all possible message content part types.
* Used when a value could be any valid content part.
*
* @example
* ```ts
* function processContent(part: LLMMessageContentPart) {
* switch(part.type) {
* case 'text': return part.text;
* case 'image': return part.source.data;
* case 'thinking': return part.thinking;
* // ...
* }
* }
* ```
*/
export type LLMMessageContentPart =
| LLMMessageContentPartTextBlock
| LLMMessageContentPartThinkingBlock
| LLMMessageContentPartRedactedThinkingBlock
| LLMMessageContentPartImageBlock
| LLMMessageContentPartAudioBlock
| LLMMessageContentPartToolUseBlock
| LLMMessageContentPartToolResultBlock;
/**
* Represents a sequence of message content parts.
* Used when a message contains multiple content blocks.
*
* @example
* ```ts
* const parts: LLMMessageContentParts = [
* { type: 'text', text: 'Here is an image:' },
* { type: 'image', source: { type: 'base64', media_type: 'image/png', data: '...' } }
* ];
* ```
*/
export type LLMMessageContentParts = LLMMessageContentPart[];
/**
* Contains complete information about a tool use in an LLM answer.
* Tracks the tool invocation, input, and validation status.
*
* @example
* ```ts
* const toolUse: LLMAnswerToolUse = {
* toolThinking: 'I need to search for TypeScript files...',
* toolInput: { pattern: '*.ts' },
* toolUseId: 'tool-123',
* toolName: 'search_files',
* toolValidation: { validated: true, results: 'Input is valid' }
* };
* ```
*/
export interface LLMAnswerToolUse {
toolThinking?: string;
toolInput: Record<string, unknown>;
toolUseId: string;
toolName: string;
toolValidation: { validated: boolean; results: string };
}