Skip to content

Commit

Permalink
READY : Continue work on assistant (#1489)
Browse files Browse the repository at this point in the history
* Refactor table formatting

* Partial refactoring

* Filter columns

* Improve commands
  • Loading branch information
InAnYan authored Nov 18, 2024
1 parent 5b52e5f commit d3bf267
Show file tree
Hide file tree
Showing 15 changed files with 238 additions and 86 deletions.
26 changes: 24 additions & 2 deletions module/move/assistant/src/actions/openai.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ mod private
use crate::*;
use ser::DisplayFromStr;

use commands::TableConfig;

/// Collective enum for errors in OpenAI actions.
#[ ser::serde_as ]
#[ derive( Debug, Error, AsRefStr, ser::Serialize ) ]
Expand All @@ -26,19 +28,39 @@ mod private
#[ from ]
#[ serde_as( as = "DisplayFromStr" ) ]
openai_api_rs::v1::error::APIError
)
),

/// User chosen a mix of table styles instead of a single one.
/// E.g.: both `--as-table` and `--as-records` were set, however only one style must be chosen
#[ error( "Select only one table style: either `--as-table`, `--as-records`, or `--columns`" ) ]
WrongTableStyle,
}

/// Shorthand for `Result` in OpenAI actions.
pub type Result< T > = core::result::Result< T, Error >;

/// Check the CLI arguments for table style.
/// There are 3 arguments: `--as-table`, `--as-records`, `--columns`. Only one argument
/// should be active a time.
pub fn check_table_style( table_config: &TableConfig ) -> Result< () >
{
if table_config.as_table && ( table_config.as_records || table_config.columns )
|| table_config.as_records && ( table_config.as_table || table_config.columns )
|| table_config.columns && ( table_config.as_records || table_config.as_table )
{
return Err( Error::WrongTableStyle )
}

Ok( () )
}
}

crate::mod_interface!
{
own use
{
Error,
Result
Result,
check_table_style,
};
}
33 changes: 14 additions & 19 deletions module/move/assistant/src/actions/openai_assistants_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,24 @@ mod private

use std::fmt;

use format_tools::
{
AsTable,
TableFormatter,
output_format,
};
use format_tools::AsTable;

use crate::*;
use client::Client;

use debug::AssistantObjectWrap;
use actions::openai::Result;

use actions::openai::{ Result, check_table_style };

use commands::TableConfig;
use util::display_table::display_tabular_data;

/// Report for `openai assistants list`.
#[ derive( Debug ) ]
pub struct ListReport
{
/// Show records as separate tables.
pub show_records_as_tables : bool,
/// Configure table formatting.
pub table_config : TableConfig,

/// OpenAI assistants.
pub assistants: Vec< AssistantObjectWrap >
Expand All @@ -38,27 +38,22 @@ mod private
f : &mut fmt::Formatter< '_ >
) -> fmt::Result
{
if self.show_records_as_tables
{
writeln!(f, "{}", AsTable::new( &self.assistants ).table_to_string_with_format( &output_format::Records::default() ) )
}
else
{
writeln!(f, "{}", AsTable::new( &self.assistants ).table_to_string_with_format( &output_format::Table::default() ) )
}
display_tabular_data( &AsTable::new( &self.assistants ), f, &self.table_config )
}
}

/// List OpenAI assistants action.
pub async fn action
(
client : &Client,
show_records_as_tables : bool,
table_config : TableConfig,
) -> Result < ListReport >
{
check_table_style( &table_config )?;

let response = client.list_assistant( None, None, None, None ).await?;
let assistants = response.data.into_iter().map( AssistantObjectWrap ).collect();
Ok( ListReport { show_records_as_tables, assistants } )
Ok( ListReport { table_config, assistants } )
}
}

Expand Down
33 changes: 14 additions & 19 deletions module/move/assistant/src/actions/openai_files_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,24 @@ mod private

use std::fmt;

use format_tools::
{
AsTable,
TableFormatter,
output_format,
};
use format_tools::AsTable;

use crate::*;
use client::Client;

use debug::FileDataWrap;
use actions::openai::Result;

use actions::openai::{ Result, check_table_style };

use commands::TableConfig;
use util::display_table::display_tabular_data;

/// Report for `openai files list`.
#[ derive( Debug ) ]
pub struct ListReport
{
/// Show records as separate tables.
pub show_records_as_tables : bool,
/// Configure table formatting.
pub table_config : TableConfig,

/// Files in OpenAI.
pub files : Vec< FileDataWrap >
Expand All @@ -38,27 +38,22 @@ mod private
f : &mut fmt::Formatter< '_ >
) -> fmt::Result
{
if self.show_records_as_tables
{
writeln!(f, "{}", AsTable::new( &self.files ).table_to_string_with_format( &output_format::Records::default() ) )
}
else
{
writeln!(f, "{}", AsTable::new( &self.files ).table_to_string_with_format( &output_format::Table::default() ) )
}
display_tabular_data( &AsTable::new( &self.files ), f, &self.table_config )
}
}

/// List OpenAI files action.
pub async fn action
(
client : &Client,
show_records_as_tables : bool,
table_config : TableConfig,
) -> Result < ListReport >
{
check_table_style( &table_config )?;

let response = client.file_list().await?;
let files = response.data.into_iter().map( FileDataWrap ).collect();
Ok( ListReport { show_records_as_tables, files } )
Ok( ListReport { table_config, files } )
}

}
Expand Down
35 changes: 15 additions & 20 deletions module/move/assistant/src/actions/openai_runs_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,27 @@ mod private

use std::fmt;

use format_tools::
{
AsTable,
TableFormatter,
output_format,
};
use format_tools::AsTable;

use crate::*;
use client::Client;

use debug::RunObjectWrap;
use actions::openai::Result;

use actions::openai::{ Result, check_table_style };

use commands::TableConfig;
use util::display_table::display_tabular_data;

/// Report for `openai runs list`.
#[ derive( Debug ) ]
pub struct ListReport
{
/// Show records as separate tables.
pub show_records_as_tables : bool,
/// Configure table formatting.
pub table_config : TableConfig,

/// Current OpenAI runs.
pub runs : Vec< RunObjectWrap >
pub runs : Vec< RunObjectWrap >,
}

impl fmt::Display for ListReport
Expand All @@ -38,14 +38,7 @@ mod private
f : &mut fmt::Formatter< '_ >
) -> fmt::Result
{
if self.show_records_as_tables
{
writeln!(f, "{}", AsTable::new( &self.runs ).table_to_string_with_format( &output_format::Records::default() ) )
}
else
{
writeln!(f, "{}", AsTable::new( &self.runs ).table_to_string_with_format( &output_format::Table::default() ) )
}
display_tabular_data( &AsTable::new( &self.runs ), f, &self.table_config )
}
}

Expand All @@ -54,12 +47,14 @@ mod private
(
client : &Client,
thread_id : String,
show_records_as_tables : bool,
table_config : TableConfig,
) -> Result < ListReport >
{
check_table_style( &table_config )?;

let response = client.list_run( thread_id, None, None, None, None ).await?;
let runs = response.data.into_iter().map( RunObjectWrap ).collect();
Ok( ListReport { show_records_as_tables, runs } )
Ok( ListReport { table_config, runs } )
}

}
Expand Down
2 changes: 1 addition & 1 deletion module/move/assistant/src/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ async fn main() -> Result< (), Box< dyn Error > >

let secret = Secret::load()?;

let client = client::client( &secret )?;
let client = client( &secret )?;

let cli = Cli::parse();

Expand Down
30 changes: 30 additions & 0 deletions module/move/assistant/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,35 @@ mod private
OpenAi( openai::Command ),
}

// const DEFAULT_MAX_TABLE_WIDTH: usize = 130;
// Commented out as not yet implemented in `format_tools`.

/// Common collection of arguments for formatting tabular data.
#[ derive( Debug, Parser ) ]
pub struct TableConfig
{
/// Limit table widht.
// #[ arg( long, default_value_t = DEFAULT_MAX_TABLE_WIDTH ) ]
// pub max_table_width : usize,
// Commented out as not yet implemented in `format_tools`.

/// Show tabular data as an ordinary table.
#[ arg( long ) ]
pub as_table : bool,

/// Show each record of a tabular data as a separate table.
#[ arg( long ) ]
pub as_records : bool,

/// Show only keys (columns) of tabular data.
#[ arg( long ) ]
pub columns : bool,

/// Filter columns of tabular data.
#[ arg( long, value_delimiter( ',' ) ) ]
pub filter_columns : Vec< String >,
}

}

crate::mod_interface!
Expand All @@ -45,5 +74,6 @@ crate::mod_interface!
{
Cli,
CliCommand,
TableConfig,
};
}
12 changes: 6 additions & 6 deletions module/move/assistant/src/commands/openai_assistants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ mod private

use crate::*;
use client::Client;
use commands::openai_assistants_list;
use commands::{ openai_assistants_list, TableConfig };

/// OpenAI assistants.
#[ derive ( Debug, Subcommand ) ]
Expand All @@ -18,9 +18,9 @@ mod private
/// List OpenAI assistants.
List
{
/// Show records as separate tables.
#[ arg( long, default_value_t = false ) ]
show_records_as_tables : bool
/// Configure table formatting.
#[ clap( flatten ) ]
table_config : TableConfig,
},
}

Expand All @@ -33,9 +33,9 @@ mod private
{
match command
{
Command::List{ show_records_as_tables } =>
Command::List{ table_config } =>
{
openai_assistants_list::command( client, show_records_as_tables ).await;
openai_assistants_list::command( client, table_config ).await;
}
}
}
Expand Down
5 changes: 3 additions & 2 deletions module/move/assistant/src/commands/openai_assistants_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,16 @@ mod private
use crate::*;
use client::Client;
use actions;
use commands::TableConfig;

/// List OpenAI assistants command.
pub async fn command
(
client : &Client,
show_records_as_tables : bool,
table_config : TableConfig,
)
{
let result = actions::openai_assistants_list::action( client, show_records_as_tables ).await;
let result = actions::openai_assistants_list::action( client, table_config ).await;

match result
{
Expand Down
12 changes: 6 additions & 6 deletions module/move/assistant/src/commands/openai_files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ mod private

use crate::*;
use client::Client;
use commands::openai_files_list;
use commands::{ openai_files_list, TableConfig };

/// OpenAI files.
#[ derive ( Debug, Subcommand ) ]
Expand All @@ -18,9 +18,9 @@ mod private
/// List OpenAI files.
List
{
/// Show records as separate tables.
#[ arg( long, default_value_t = false ) ]
show_records_as_tables : bool
/// Configure table formatting.
#[ clap( flatten ) ]
table_config : TableConfig,
},
}

Expand All @@ -33,9 +33,9 @@ mod private
{
match command
{
Command::List{ show_records_as_tables } =>
Command::List{ table_config } =>
{
openai_files_list::command( client, show_records_as_tables ).await;
openai_files_list::command( client, table_config ).await;
}
}
}
Expand Down
Loading

0 comments on commit d3bf267

Please sign in to comment.