1- //! Workspace context: `/v1/context` sync with `./{NAME}.md` in the current directory.
1+ //! Database context: `/v1/databases/{id} /context` sync with `./{NAME}.md` in the current directory.
22
33use crate :: api:: ApiClient ;
44use crossterm:: style:: Stylize ;
@@ -28,25 +28,25 @@ static RESERVED_WORDS: LazyLock<HashSet<&'static str>> = LazyLock::new(|| {
2828} ) ;
2929
3030#[ derive( Debug , Deserialize , Serialize ) ]
31- struct WorkspaceContextEntry {
31+ struct DatabaseContextEntry {
3232 name : String ,
3333 content : String ,
3434 updated_at : String ,
3535}
3636
3737#[ derive( Deserialize ) ]
3838struct ListResponse {
39- contexts : Vec < WorkspaceContextEntry > ,
39+ contexts : Vec < DatabaseContextEntry > ,
4040}
4141
4242#[ derive( Deserialize ) ]
4343struct GetResponse {
44- context : WorkspaceContextEntry ,
44+ context : DatabaseContextEntry ,
4545}
4646
4747#[ derive( Deserialize ) ]
4848struct UpsertResponse {
49- context : WorkspaceContextEntry ,
49+ context : DatabaseContextEntry ,
5050}
5151
5252/// Normalizes a context name from the CLI: trims, takes the final path segment, and strips a
@@ -124,9 +124,10 @@ fn local_md_path(name: &str) -> PathBuf {
124124
125125fn fetch_context (
126126 api : & ApiClient ,
127+ database_id : & str ,
127128 name : & str ,
128- ) -> Result < WorkspaceContextEntry , reqwest:: StatusCode > {
129- let path = format ! ( "/context/{name}" ) ;
129+ ) -> Result < DatabaseContextEntry , reqwest:: StatusCode > {
130+ let path = format ! ( "/databases/{database_id}/ context/{name}" ) ;
130131 let ( status, body) = api. get_raw ( & path) ;
131132 if status == reqwest:: StatusCode :: NOT_FOUND {
132133 return Err ( status) ;
@@ -143,11 +144,11 @@ fn fetch_context(
143144 Ok ( parsed. context )
144145}
145146
146- pub fn list ( workspace_id : & str , prefix : Option < & str > , format : & str ) {
147+ pub fn list ( workspace_id : & str , database_id : & str , prefix : Option < & str > , format : & str ) {
147148 let api = ApiClient :: new ( Some ( workspace_id) ) ;
148- let body: ListResponse = api. get ( "/ context") ;
149+ let body: ListResponse = api. get ( & format ! ( "/databases/{database_id}/ context") ) ;
149150
150- let mut rows: Vec < WorkspaceContextEntry > = body. contexts ;
151+ let mut rows: Vec < DatabaseContextEntry > = body. contexts ;
151152 if let Some ( p) = prefix {
152153 rows. retain ( |c| c. name . starts_with ( p) ) ;
153154 }
@@ -176,15 +177,15 @@ pub fn list(workspace_id: &str, prefix: Option<&str>, format: &str) {
176177 }
177178}
178179
179- pub fn show ( workspace_id : & str , name : & str ) {
180+ pub fn show ( workspace_id : & str , database_id : & str , name : & str ) {
180181 let name = normalize_context_cli_name ( name) ;
181182 if let Err ( e) = validate_context_stem ( & name) {
182183 eprintln ! ( "error: {e}" ) ;
183184 std:: process:: exit ( 1 ) ;
184185 }
185186
186187 let api = ApiClient :: new ( Some ( workspace_id) ) ;
187- match fetch_context ( & api, & name) {
188+ match fetch_context ( & api, database_id , & name) {
188189 Ok ( ctx) => {
189190 print ! ( "{}" , ctx. content) ;
190191 if !ctx. content . ends_with ( '\n' ) {
@@ -194,7 +195,7 @@ pub fn show(workspace_id: &str, name: &str) {
194195 Err ( reqwest:: StatusCode :: NOT_FOUND ) => {
195196 eprintln ! (
196197 "{}" ,
197- format!( "error: no context named '{name}' in this workspace ." ) . red( )
198+ format!( "error: no context named '{name}' in this database ." ) . red( )
198199 ) ;
199200 eprintln ! (
200201 "{}" ,
@@ -207,7 +208,7 @@ pub fn show(workspace_id: &str, name: &str) {
207208 }
208209}
209210
210- pub fn pull ( workspace_id : & str , name : & str , force : bool , dry_run : bool ) {
211+ pub fn pull ( workspace_id : & str , database_id : & str , name : & str , force : bool , dry_run : bool ) {
211212 let name = normalize_context_cli_name ( name) ;
212213 if let Err ( e) = validate_context_stem ( & name) {
213214 eprintln ! ( "error: {e}" ) ;
@@ -229,12 +230,12 @@ pub fn pull(workspace_id: &str, name: &str, force: bool, dry_run: bool) {
229230 }
230231
231232 let api = ApiClient :: new ( Some ( workspace_id) ) ;
232- let ctx = match fetch_context ( & api, & name) {
233+ let ctx = match fetch_context ( & api, database_id , & name) {
233234 Ok ( c) => c,
234235 Err ( reqwest:: StatusCode :: NOT_FOUND ) => {
235236 eprintln ! (
236237 "{}" ,
237- format!( "error: no context named '{name}' in this workspace ." ) . red( )
238+ format!( "error: no context named '{name}' in this database ." ) . red( )
238239 ) ;
239240 std:: process:: exit ( 1 ) ;
240241 }
@@ -270,7 +271,7 @@ pub fn pull(workspace_id: &str, name: &str, force: bool, dry_run: bool) {
270271 ) ;
271272}
272273
273- pub fn push ( workspace_id : & str , name : & str , dry_run : bool ) {
274+ pub fn push ( workspace_id : & str , database_id : & str , name : & str , dry_run : bool ) {
274275 let name = normalize_context_cli_name ( name) ;
275276 if let Err ( e) = validate_context_stem ( & name) {
276277 eprintln ! ( "error: {e}" ) ;
@@ -310,7 +311,7 @@ pub fn push(workspace_id: &str, name: &str, dry_run: bool) {
310311
311312 let api = ApiClient :: new ( Some ( workspace_id) ) ;
312313 let body = json ! ( { "name" : & name, "content" : content } ) ;
313- let resp: UpsertResponse = api. post ( "/ context", & body) ;
314+ let resp: UpsertResponse = api. post ( & format ! ( "/databases/{database_id}/ context") , & body) ;
314315
315316 println ! (
316317 "{}" ,
0 commit comments