Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hot fix for missing conversation log entries #14

Merged
merged 31 commits into from
Aug 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
1542722
Implemented RewriteFileTool class to rewrite or create files within t…
cngarrison Aug 15, 2024
4d81be1
Added RewriteFileTool to the list of registered tools in LLMToolManager
cngarrison Aug 15, 2024
e50e2f3
Added unit tests for the RewriteFileTool class
cngarrison Aug 15, 2024
0cbd46a
Updated imports to use fully qualified URLs for Deno standard library…
cngarrison Aug 15, 2024
5da3a77
Updated import paths for Deno standard library modules
cngarrison Aug 15, 2024
7a0777d
Refactored RewriteFileTool to use async/await and updated input schema
cngarrison Aug 15, 2024
5361fe4
Added ConversationTokenUsage type to conversationLogger.utils.ts
cngarrison Aug 15, 2024
f50022e
Refactored the `rewriteFileTool` to accept an `LLMConversationInterac…
cngarrison Aug 15, 2024
6a5dd0b
Refactored conversation logger to handle missing conversation metrics…
cngarrison Aug 15, 2024
6dd17a3
Fixes for rewrite file tool
cngarrison Aug 15, 2024
ce2dd54
Added LLMToolRunResultContent, LLMToolFinalizeResult, and ToolFormatt…
cngarrison Aug 15, 2024
4974713
Added tool formatter registration and retrieval functionality to LLMT…
cngarrison Aug 15, 2024
9784fb7
Added tool formatter support to conversation logger
cngarrison Aug 15, 2024
23fc03d
Added RequestFilesToolFormatter and registered it with LLMToolManager
cngarrison Aug 15, 2024
6eb12f1
Added error handling to `logToolUse` and `logToolResult` functions in…
cngarrison Aug 15, 2024
0f3692f
Registered RequestFilesTool formatter and added static formatter prop…
cngarrison Aug 15, 2024
1c58a9d
Implemented LLMToolManager class with tool loading, registration, and…
cngarrison Aug 15, 2024
1b8b370
Implemented RequestFilesToolFormatter and LLMToolRequestFiles to hand…
cngarrison Aug 15, 2024
4629195
Added last commit information to file metadata in `prepareFilesForCon…
cngarrison Aug 15, 2024
4791ba1
Added method to get last commit hash for a file
cngarrison Aug 15, 2024
7236ce2
Refactored git utility imports and usage in projectEditor.ts
cngarrison Aug 15, 2024
6dbd6d2
Updated API documentation with new endpoints and details
cngarrison Aug 15, 2024
9fe8651
Improved logging in conversationLogger.utils.ts
cngarrison Aug 15, 2024
2d8fbf7
Added test file to trigger tool use logging.
cngarrison Aug 15, 2024
886c46f
Added logging and error handling to the `logEntry` method in `convers…
cngarrison Aug 15, 2024
814d834
Updated test file to trigger tool use logging
cngarrison Aug 15, 2024
33a5a2b
Refactored conversation start display to use a more compact format fo…
cngarrison Aug 15, 2024
8f60ba1
Added left padding to conversation start display for better readability
cngarrison Aug 15, 2024
df5bd20
Wrote test message to confirm tool use is working as expected
cngarrison Aug 15, 2024
442dd8e
hotfix for console logging; started custom log formatters for tools
cngarrison Aug 15, 2024
8e2a040
version bump
cngarrison Aug 15, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 74 additions & 35 deletions API.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# BBai API Documentation

This document provides details about the endpoints available in the bbai API.
This document provides details about the endpoints available in the BBai API.

## Base URL

Expand All @@ -20,60 +20,99 @@ All endpoints are relative to: `http://localhost:<port>/api/v1`
```

### Conversation Management
- **POST** `/conversation`
- Start a new conversation.
- Request Body:
```json
{
"prompt": "string",
"provider": "string" (optional),
"model": "string" (optional),
"startDir": "string"
}
```
- Response: LLM-generated response

#### List Conversations
- **GET** `/conversation`
- Retrieve a list of conversations with pagination and filtering options.
- Query Parameters:
- `page` (integer, default: 1): Page number for pagination
- `pageSize` (integer, default: 10): Number of items per page
- `startDate` (string, format: date): Filter conversations starting from this date
- `endDate` (string, format: date): Filter conversations up to this date
- `llmProviderName` (string): Filter conversations by LLM provider name
- `startDir` (string, required): The starting directory for the project
- Response: List of conversations with pagination details

#### Get Conversation
- **GET** `/conversation/:id`
- Get details of a specific conversation.
- Response: Conversation details (to be implemented)
- Retrieve details of a specific conversation.
- Query Parameters:
- `startDir` (string, required): The starting directory for the project
- Response: Conversation details including messages, LLM provider, and token usage

#### Continue Conversation
- **POST** `/conversation/:id`
- Continue an existing conversation.
- Request Body:
```json
{
"prompt": "string",
"statement": "string",
"startDir": "string"
}
```
- Response: LLM-generated response
- Response: LLM-generated response with conversation details

#### Delete Conversation
- **DELETE** `/conversation/:id`
- Delete a conversation.
- Delete a specific conversation.
- Query Parameters:
- `startDir` (string, required): The starting directory for the project
- Response: Deletion confirmation message

#### Clear Conversation
- **POST** `/conversation/:id/clear`
- Clear the history of a conversation.
- Clear the history of a specific conversation.
- Query Parameters:
- `startDir` (string, required): The starting directory for the project
- Response: Confirmation message

- **POST** `/conversation/:id/undo`
- Undo the last change in a conversation.
- Response: Confirmation message
### WebSocket Connection
- **GET** `/ws/conversation/:id`
- Establish a WebSocket connection for real-time conversation updates.
- The client can send messages with the following format:
```json
{
"task": "greeting" | "converse" | "cancel",
"statement": "string",
"startDir": "string"
}
```
- The server will emit events for conversation updates, including:
- `conversationReady`
- `conversationEntry`
- `conversationAnswer`
- `conversationError`
- `conversationCancelled`

## Note on Unimplemented Features

The following features are mentioned in the codebase but are not fully implemented or exposed through the API:

- Adding files to a conversation
- Removing files from a conversation
- Listing files in a conversation
- Retrieving token usage
- Running CLI commands
- Loading external content
- Retrieving conversation logs
- Undoing the last change in a conversation

These features may be implemented in future versions of the API.

## Error Handling

All endpoints may return appropriate HTTP status codes for various error conditions. Common error responses include:

- 400 Bad Request: For invalid input or missing required parameters
- 404 Not Found: When a requested resource (e.g., conversation) is not found
- 500 Internal Server Error: For unexpected server-side errors

### File Management
- **POST** `/conversation/:id/file`
- Add a file to the conversation.
- Request Body: FormData with 'file' field
- Response: File addition confirmation
Detailed error messages will be provided in the response body when applicable.

- **DELETE** `/conversation/:id/file/:fileId`
- Remove a file from the conversation.
- Response: File removal confirmation
## Authentication

- **GET** `/conversation/:id/files`
- List files in the conversation.
- Response: Array of file names
The current implementation does not include authentication. It is designed for local use only. Ensure proper security measures are in place when deploying this API in a production environment.

Note: Some endpoints like Token Usage, CLI Command, External Content, Logs, and Persistence are not currently implemented in the provided code and have been removed from this documentation.
## Versioning

Detailed request/response schemas and examples for each endpoint will be added in future updates.
This documentation is for API version 1 (`v1`). Future versions may introduce changes to the endpoint structure or functionality.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0



## [0.0.8a-alpha] - 2024-08-15

### Changed

- Hot fix for missing conversation log entries


## [0.0.8-alpha] - 2024-08-15

### Changed
Expand Down
2 changes: 1 addition & 1 deletion api/deno.jsonc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "bbai-api",
"version": "0.0.8-alpha",
"version": "0.0.8a-alpha",
"exports": "./src/main.ts",
"tasks": {
"start": "deno run --allow-read --allow-write --allow-run --allow-net --allow-env src/main.ts",
Expand Down
33 changes: 33 additions & 0 deletions api/deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 20 additions & 17 deletions api/src/controllers/orchestratorController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class OrchestratorController {
public llmProvider: LLM;
public eventManager!: EventManager;
private projectEditorRef!: WeakRef<ProjectEditor>;
private _providerRequestCount: number = 0;
//private _providerRequestCount: number = 0;
// counts across all interactions
// count of turns for most recent statement in most recent interaction
private _turnCount: number = 0;
Expand Down Expand Up @@ -267,13 +267,10 @@ class OrchestratorController {
const persistence = await new ConversationPersistence(interaction.id, this.projectEditor).init();

// Include the latest stats and usage in the saved conversation
const conversationData = {
...interaction,
stats: this.interactionStats.get(interaction.id),
tokenUsage: this._tokenUsageTotals,
} as unknown as LLMConversationInteraction;
//interaction.conversationStats = this.interactionStats.get(interaction.id),
//interaction.tokenUsageConversation = this.interactionTokenUsage.get(interaction.id),

await persistence.saveConversation(conversationData);
await persistence.saveConversation(interaction);

// Save system prompt and project info if running in local development
if (config.api?.environment === 'localdev') {
Expand Down Expand Up @@ -362,7 +359,9 @@ class OrchestratorController {
timestamp: string,
content: string,
conversationStats: ConversationMetrics,
tokenUsage: TokenUsage,
tokenUsageTurn: TokenUsage,
tokenUsageStatement: TokenUsage,
tokenUsageConversation: ConversationTokenUsage,
): Promise<void> => {
const conversationEntry: ConversationEntry = {
type,
Expand All @@ -371,8 +370,9 @@ class OrchestratorController {
conversationTitle: this.primaryInteraction.title,
content,
conversationStats,
tokenUsageConversation: this.tokenUsageTotals,
tokenUsageStatement: tokenUsage,
tokenUsageTurn: tokenUsageTurn,
tokenUsageStatement: tokenUsageStatement,
tokenUsageConversation: tokenUsageConversation,
};
this.eventManager.emit(
'projectEditor:conversationEntry',
Expand Down Expand Up @@ -423,25 +423,28 @@ class OrchestratorController {
toolUse: LLMAnswerToolUse,
_response: unknown,
): Promise<string> {
interaction.conversationLogger?.logToolUse(
logger.error(`Handling tool use for: ${toolUse.toolName}`);
await interaction.conversationLogger.logToolUse(
toolUse.toolName,
toolUse.toolInput,
interaction.conversationStats,
interaction.tokenUsageStatement,
//interaction.conversationStats,
//interaction.tokenUsageTurn,
//interaction.tokenUsageStatement,
//interaction.tokenUsageConversation,
);
const { messageId: _messageId, toolResponse, bbaiResponse, isError } = await this.toolManager.handleToolUse(
interaction,
toolUse,
this.projectEditor,
);
if (isError) {
interaction.conversationLogger?.logError(`Tool Result (${toolUse.toolName}): ${toolResponse}`);
interaction.conversationLogger.logError(`Tool Result (${toolUse.toolName}): ${toolResponse}`);
}
interaction.conversationLogger?.logToolResult(
await interaction.conversationLogger.logToolResult(
toolUse.toolName,
`BBai was ${isError ? 'unsuccessful' : 'successful'} with tool run: \n${bbaiResponse}`,
interaction.conversationStats,
interaction.tokenUsageStatement,
//interaction.conversationStats,
//interaction.tokenUsageStatement, // token usage is recorded with the tool use
);

return toolResponse;
Expand Down
4 changes: 4 additions & 0 deletions api/src/editor/projectEditor.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { join } from '@std/path';

import { FILE_LISTING_TIERS, generateFileListing, isPathWithinProject } from '../utils/fileHandling.utils.ts';
import { GitUtils } from 'shared/git.ts';
import LLMConversationInteraction, { FileMetadata, ProjectInfo } from '../llms/interactions/conversationInteraction.ts';
import OrchestratorController from '../controllers/orchestratorController.ts';
import { logger } from 'shared/logger.ts';
Expand Down Expand Up @@ -155,10 +156,12 @@ class ProjectEditor {

const fullFilePath = join(this.projectRoot, fileName);
const content = await Deno.readTextFile(fullFilePath);
const lastCommit = await GitUtils.getLastCommitForFile(this.projectRoot, fileName) || '';
const metadata: Omit<FileMetadata, 'path' | 'inSystemPrompt'> = {
size: new TextEncoder().encode(content).length,
lastModified: new Date(),
error: null,
lastCommit: lastCommit,
};
filesAdded.push({ fileName, metadata });

Expand All @@ -174,6 +177,7 @@ class ProjectEditor {
size: 0,
lastModified: new Date(),
error: errorMessage,
lastCommit: '',
},
});
}
Expand Down
1 change: 1 addition & 0 deletions api/src/errors/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ export interface FileHandlingErrorOptions extends ErrorOptions {
| 'patch'
| 'search-project'
| 'search-replace'
| 'rewrite-file'
| 'request-files'
| 'remove-files';
}
Expand Down
Loading
Loading