-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinteraction.ts
More file actions
292 lines (280 loc) · 8.93 KB
/
interaction.ts
File metadata and controls
292 lines (280 loc) · 8.93 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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
import type { FileMetadata, TokenUsage, ToolUsageStats } from './types.ts';
import type { LLMMessageContentParts } from './message.ts';
import type { IProjectEditor } from './project_editor.ts';
/**
* Interaction management module for the BB Tools Framework.
* Provides interfaces and types for managing interaction state, resource handling,
* and tool usage tracking within an interaction context.
*
* Note: This interface represents capabilities available to tools during execution.
* It is extracted from BB's LLMConversationInteraction implementation.
*
* @module
*/
/**
* Core interface representing the interaction capabilities needed by tools.
* Provides methods for managing resources, tracking tool usage, and handling interaction state.
*
* This interface is what tools receive when they execute - it provides access to:
* - Resource management (reading/writing files and other resources)
* - Conversation/interaction context
* - Project editing capabilities
* - Tool usage statistics
*
* @example
* ```ts
* class MyTool extends LLMTool {
* async runTool(
* interaction: IConversationInteraction,
* toolUse: LLMAnswerToolUse,
* projectEditor: IProjectEditor
* ) {
* // Access interaction capabilities
* const stats = interaction.getToolUsageStats();
*
* // Read resource content
* const content = await interaction.readResourceContent(
* 'bb+filesystem+project+file:./src/config.ts',
* 'rev-123',
* metadata
* );
*
* return { toolResults, toolResponse, bbResponse };
* }
* }
* ```
*/
export interface IConversationInteraction {
/**
* Reference to the project editor.
* Provides access to project-level operations and configuration.
*
* @example
* ```ts
* const projectRoot = interaction.projectEditor.projectRoot;
* const dsConnections = interaction.projectEditor.dsConnections;
* ```
*/
projectEditor: IProjectEditor;
/**
* Retrieves metadata for a specific resource revision.
* Used to get information about a resource without reading its contents.
*
* @param resourceUri - URI of the resource (e.g., 'bb+filesystem+project+file:./src/config.ts')
* @param revisionId - Unique identifier for the resource revision
* @returns Resource metadata if found, undefined otherwise
*
* @example
* ```ts
* const metadata = interaction.getFileMetadata(
* 'bb+filesystem+project+file:./src/config.ts',
* 'rev-123'
* );
* if (metadata) {
* console.log(`Resource size: ${metadata.size} bytes`);
* }
* ```
*/
getFileMetadata(
resourceUri: string,
revisionId: string,
): FileMetadata | undefined;
/**
* Reads the content of a resource for a specific revision.
* Supports both text and binary resources.
*
* @param resourceUri - URI of the resource
* @param revisionId - Unique identifier for the resource revision
* @param resourceMetadata - Optional metadata for the resource
* @returns Promise resolving to resource content as string or Uint8Array
*
* @example
* ```ts
* const content = await interaction.readResourceContent(
* 'bb+filesystem+project+file:./src/config.ts',
* 'rev-123',
* metadata
* );
* if (typeof content === 'string') {
* console.log('Resource content:', content);
* }
* ```
*/
readResourceContent(
resourceUri: string,
revisionId: string,
resourceMetadata: FileMetadata | undefined,
): Promise<string | Uint8Array>;
/**
* Stores a new revision of a resource.
* Creates or updates resource content while maintaining revision history.
*
* @param resourceUri - URI of the resource
* @param revisionId - Unique identifier for the new revision
* @param content - New resource content as string or Uint8Array
* @param resourceMetadata - Metadata for the resource
*
* @example
* ```ts
* await interaction.storeResourceRevision(
* 'bb+filesystem+project+file:./src/config.ts',
* 'rev-124',
* 'export const config = { debug: true };',
* metadata
* );
* ```
*/
storeResourceRevision(
resourceUri: string,
revisionId: string,
content: string | Uint8Array,
resourceMetadata: FileMetadata | undefined,
): Promise<void>;
/**
* Retrieves a specific revision of a resource.
* Used to access historical versions of resources.
*
* @param resourceUri - URI of the resource
* @param revisionId - Unique identifier for the resource revision
* @param resourceMetadata - Optional metadata for the resource
* @returns Promise resolving to resource content or null if not found
*
* @example
* ```ts
* const oldContent = await interaction.getResourceRevision(
* 'bb+filesystem+project+file:./src/config.ts',
* 'rev-100',
* metadata
* );
* if (oldContent) {
* console.log('Previous version:', oldContent);
* }
* ```
*/
getResourceRevision(
resourceUri: string,
revisionId: string,
resourceMetadata: FileMetadata | undefined,
): Promise<string | Uint8Array | null>;
/**
* Retrieves current tool usage statistics.
* Used to track and analyze tool usage patterns.
*
* @returns Current tool usage statistics
*
* @example
* ```ts
* const stats = interaction.getToolUsageStats();
* console.log(`Most used tool: ${[...stats.toolCounts.entries()]
* .sort((a, b) => b[1] - a[1])[0][0]}`);
* ```
*/
getToolUsageStats(): ToolUsageStats;
/**
* Updates statistics for a specific tool.
* Records tool usage and success/failure status.
*
* @param toolName - Name of the tool being tracked
* @param success - Whether the tool operation succeeded
*
* @example
* ```ts
* interaction.updateToolStats('search_files', true);
* ```
*/
updateToolStats(toolName: string, success: boolean): void;
/**
* Current token usage statistics for the interaction.
* Tracks input, output, and cache-related token usage.
*
* @example
* ```ts
* const usage = interaction.tokenUsageConversation;
* console.log(`Total tokens used: ${usage.totalTokens}`);
* ```
*/
tokenUsageConversation: TokenUsage;
/**
* Adds a single resource to the interaction context.
* Associates resource with a specific message and optional tool use.
*
* @param resourceUri - URI of the resource
* @param metadata - Resource metadata excluding URI
* @param messageId - ID of the message this resource is associated with
* @param toolUseId - Optional ID of the tool use that added this resource
* @returns Object containing resource URI and complete metadata
*
* @example
* ```ts
* const { resourceMetadata } = interaction.addResourceForMessage(
* 'bb+filesystem+project+file:./src/config.ts',
* { type: 'text', size: 1024, lastModified: new Date() },
* 'msg-123'
* );
* ```
*/
addResourceForMessage(
resourceUri: string,
metadata: Omit<FileMetadata, 'path'>,
messageId: string,
toolUseId?: string,
): { resourceUri: string; resourceMetadata: FileMetadata };
/**
* Adds multiple resources to the interaction context.
* Efficiently handles batch resource additions.
*
* @param resourcesToAdd - Array of resources with their metadata
* @param messageId - ID of the message these resources are associated with
* @param toolUseId - Optional ID of the tool use that added these resources
* @returns Array of objects containing resource URIs and complete metadata
*
* @example
* ```ts
* const resources = await interaction.addResourcesForMessage([
* {
* resourceUri: 'bb+filesystem+project+file:./src/config.ts',
* metadata: { type: 'text', size: 1024, lastModified: new Date() }
* },
* {
* resourceUri: 'bb+filesystem+project+file:./src/types.ts',
* metadata: { type: 'text', size: 2048, lastModified: new Date() }
* }
* ], 'msg-123');
* ```
*/
addResourcesForMessage(
resourcesToAdd: Array<{
resourceName?: string;
resourceUri: string;
metadata: Omit<FileMetadata, 'path'>;
}>,
messageId: string,
toolUseId?: string,
): Array<{ resourceUri: string; resourceMetadata: FileMetadata }>;
/**
* Creates message content blocks for a specific resource revision.
* Used to include resource content in conversation messages.
*
* @param resourceUri - URI of the resource
* @param revisionId - Unique identifier for the resource revision
* @param turnIndex - Index of the conversation turn
* @returns Promise resolving to content blocks or null if resource cannot be processed
*
* @example
* ```ts
* const blocks = await interaction.createResourceContentBlocks(
* 'bb+filesystem+project+file:./src/config.ts',
* 'rev-123',
* 5
* );
* if (blocks) {
* console.log(`Created ${blocks.length} content blocks`);
* }
* ```
*/
createResourceContentBlocks(
resourceUri: string,
revisionId: string,
turnIndex: number,
): Promise<LLMMessageContentParts | null>;
}