chasm-cli 2.0.0

Universal chat session manager - harvest, merge, and analyze AI chat history from VS Code, Cursor, and other editors
Documentation
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
// Copyright (c) 2024-2026 Nervosys LLC
// SPDX-License-Identifier: AGPL-3.0-only
//! GraphQL API module
//!
//! Provides a GraphQL endpoint alongside REST for flexible querying.

use actix_web::{web, HttpResponse, Responder};
use async_graphql::{
    Context, EmptySubscription, FieldResult, InputObject, Object, Schema, SimpleObject, ID,
};
use async_graphql_actix_web::{GraphQLRequest, GraphQLResponse};
use chrono::{DateTime, Utc};
use std::sync::Arc;

use super::state::AppState;

/// GraphQL schema type
pub type ChasmSchema = Schema<QueryRoot, MutationRoot, EmptySubscription>;

// ============================================================================
// GraphQL Types
// ============================================================================

/// Workspace type for GraphQL
#[derive(SimpleObject, Clone)]
pub struct Workspace {
    /// Unique identifier
    pub id: ID,
    /// Workspace name
    pub name: String,
    /// Workspace path
    pub path: String,
    /// Provider (e.g., "vscode", "cursor")
    pub provider: String,
    /// Number of sessions
    pub session_count: i32,
    /// Last harvested timestamp
    pub last_harvested: Option<DateTime<Utc>>,
    /// Creation timestamp
    pub created_at: DateTime<Utc>,
}

/// Session type for GraphQL
#[derive(SimpleObject, Clone)]
pub struct Session {
    /// Unique identifier
    pub id: ID,
    /// Session title
    pub title: String,
    /// Workspace ID
    pub workspace_id: Option<ID>,
    /// Provider (e.g., "copilot", "cursor")
    pub provider: String,
    /// Model used
    pub model: Option<String>,
    /// Number of messages
    pub message_count: i32,
    /// Total tokens
    pub token_count: i32,
    /// Whether archived
    pub archived: bool,
    /// Tags
    pub tags: Vec<String>,
    /// Creation timestamp
    pub created_at: DateTime<Utc>,
    /// Last updated timestamp
    pub updated_at: DateTime<Utc>,
}

/// Message type for GraphQL
#[derive(SimpleObject, Clone)]
pub struct Message {
    /// Unique identifier
    pub id: ID,
    /// Session ID
    pub session_id: ID,
    /// Message role (user, assistant, system)
    pub role: String,
    /// Message content
    pub content: String,
    /// Model used
    pub model: Option<String>,
    /// Token count
    pub token_count: i32,
    /// Creation timestamp
    pub created_at: DateTime<Utc>,
}

/// Provider type for GraphQL
#[derive(SimpleObject, Clone)]
pub struct Provider {
    /// Unique identifier
    pub id: ID,
    /// Provider name
    pub name: String,
    /// Provider type
    pub provider_type: String,
    /// Whether enabled
    pub enabled: bool,
    /// Base URL
    pub base_url: Option<String>,
}

/// Agent type for GraphQL
#[derive(SimpleObject, Clone)]
pub struct Agent {
    /// Unique identifier
    pub id: ID,
    /// Agent name
    pub name: String,
    /// Description
    pub description: Option<String>,
    /// System prompt
    pub system_prompt: Option<String>,
    /// Provider ID
    pub provider_id: Option<ID>,
    /// Model
    pub model: String,
    /// Temperature
    pub temperature: f64,
    /// Creation timestamp
    pub created_at: DateTime<Utc>,
}

/// Statistics overview
#[derive(SimpleObject, Clone)]
pub struct StatsOverview {
    /// Total sessions
    pub total_sessions: i32,
    /// Total messages
    pub total_messages: i32,
    /// Total tokens
    pub total_tokens: i64,
    /// Active providers
    pub active_providers: i32,
    /// Total workspaces
    pub workspaces: i32,
    /// Sessions today
    pub sessions_today: i32,
    /// Messages today
    pub messages_today: i32,
}

/// Search result
#[derive(SimpleObject, Clone)]
pub struct SearchResult {
    /// Sessions matching query
    pub sessions: Vec<Session>,
    /// Messages matching query
    pub messages: Vec<Message>,
    /// Total count
    pub total: i32,
}

// ============================================================================
// Input Types
// ============================================================================

/// Input for creating a session
#[derive(InputObject)]
pub struct CreateSessionInput {
    pub title: String,
    pub workspace_id: Option<ID>,
    pub provider: String,
}

/// Input for updating a session
#[derive(InputObject)]
pub struct UpdateSessionInput {
    pub title: Option<String>,
    pub tags: Option<Vec<String>>,
    pub archived: Option<bool>,
}

/// Input for creating an agent
#[derive(InputObject)]
pub struct CreateAgentInput {
    pub name: String,
    pub description: Option<String>,
    pub system_prompt: Option<String>,
    pub provider_id: Option<ID>,
    pub model: String,
    pub temperature: Option<f64>,
}

/// Filter for sessions
#[derive(InputObject, Default)]
pub struct SessionFilter {
    pub workspace_id: Option<ID>,
    pub provider: Option<String>,
    pub archived: Option<bool>,
    pub search: Option<String>,
}

/// Pagination input
#[derive(InputObject, Default)]
pub struct Pagination {
    pub limit: Option<i32>,
    pub offset: Option<i32>,
}

// ============================================================================
// Query Root
// ============================================================================

pub struct QueryRoot;

#[Object]
impl QueryRoot {
    /// Get all workspaces
    async fn workspaces(
        &self,
        ctx: &Context<'_>,
        pagination: Option<Pagination>,
    ) -> FieldResult<Vec<Workspace>> {
        let _state = ctx.data::<Arc<AppState>>()?;
        let pagination = pagination.unwrap_or_default();
        let _limit = pagination.limit.unwrap_or(20);
        let _offset = pagination.offset.unwrap_or(0);

        // TODO: Implement actual database query
        Ok(vec![])
    }

    /// Get a workspace by ID
    async fn workspace(&self, ctx: &Context<'_>, id: ID) -> FieldResult<Option<Workspace>> {
        let _state = ctx.data::<Arc<AppState>>()?;
        let _id = id.to_string();

        // TODO: Implement actual database query
        Ok(None)
    }

    /// Get all sessions with optional filtering
    async fn sessions(
        &self,
        ctx: &Context<'_>,
        filter: Option<SessionFilter>,
        pagination: Option<Pagination>,
    ) -> FieldResult<Vec<Session>> {
        let _state = ctx.data::<Arc<AppState>>()?;
        let _filter = filter.unwrap_or_default();
        let pagination = pagination.unwrap_or_default();
        let _limit = pagination.limit.unwrap_or(20);
        let _offset = pagination.offset.unwrap_or(0);

        // TODO: Implement actual database query
        Ok(vec![])
    }

    /// Get a session by ID
    async fn session(&self, ctx: &Context<'_>, id: ID) -> FieldResult<Option<Session>> {
        let _state = ctx.data::<Arc<AppState>>()?;
        let _id = id.to_string();

        // TODO: Implement actual database query
        Ok(None)
    }

    /// Get messages for a session
    async fn messages(
        &self,
        ctx: &Context<'_>,
        session_id: ID,
        pagination: Option<Pagination>,
    ) -> FieldResult<Vec<Message>> {
        let _state = ctx.data::<Arc<AppState>>()?;
        let _session_id = session_id.to_string();
        let pagination = pagination.unwrap_or_default();
        let _limit = pagination.limit.unwrap_or(100);
        let _offset = pagination.offset.unwrap_or(0);

        // TODO: Implement actual database query
        Ok(vec![])
    }

    /// Get all providers
    async fn providers(&self, ctx: &Context<'_>) -> FieldResult<Vec<Provider>> {
        let _state = ctx.data::<Arc<AppState>>()?;

        // Return common providers
        Ok(vec![
            Provider {
                id: "copilot".into(),
                name: "GitHub Copilot".to_string(),
                provider_type: "copilot".to_string(),
                enabled: true,
                base_url: None,
            },
            Provider {
                id: "cursor".into(),
                name: "Cursor".to_string(),
                provider_type: "cursor".to_string(),
                enabled: true,
                base_url: None,
            },
            Provider {
                id: "chatgpt".into(),
                name: "ChatGPT".to_string(),
                provider_type: "chatgpt".to_string(),
                enabled: true,
                base_url: Some("https://chatgpt.com".to_string()),
            },
        ])
    }

    /// Get all agents
    async fn agents(&self, ctx: &Context<'_>) -> FieldResult<Vec<Agent>> {
        let _state = ctx.data::<Arc<AppState>>()?;

        // TODO: Implement actual database query
        Ok(vec![])
    }

    /// Get an agent by ID
    async fn agent(&self, ctx: &Context<'_>, id: ID) -> FieldResult<Option<Agent>> {
        let _state = ctx.data::<Arc<AppState>>()?;
        let _id = id.to_string();

        // TODO: Implement actual database query
        Ok(None)
    }

    /// Get statistics overview
    async fn stats(&self, ctx: &Context<'_>) -> FieldResult<StatsOverview> {
        let _state = ctx.data::<Arc<AppState>>()?;

        // TODO: Implement actual database query
        Ok(StatsOverview {
            total_sessions: 0,
            total_messages: 0,
            total_tokens: 0,
            active_providers: 3,
            workspaces: 0,
            sessions_today: 0,
            messages_today: 0,
        })
    }

    /// Search across sessions and messages
    async fn search(
        &self,
        ctx: &Context<'_>,
        query: String,
        limit: Option<i32>,
    ) -> FieldResult<SearchResult> {
        let _state = ctx.data::<Arc<AppState>>()?;
        let _limit = limit.unwrap_or(20);
        let _query = query;

        // TODO: Implement actual search
        Ok(SearchResult {
            sessions: vec![],
            messages: vec![],
            total: 0,
        })
    }
}

// ============================================================================
// Mutation Root
// ============================================================================

pub struct MutationRoot;

#[Object]
impl MutationRoot {
    /// Create a new session
    async fn create_session(
        &self,
        ctx: &Context<'_>,
        input: CreateSessionInput,
    ) -> FieldResult<Session> {
        let _state = ctx.data::<Arc<AppState>>()?;

        // TODO: Implement actual creation
        Ok(Session {
            id: uuid::Uuid::new_v4().to_string().into(),
            title: input.title,
            workspace_id: input.workspace_id,
            provider: input.provider,
            model: None,
            message_count: 0,
            token_count: 0,
            archived: false,
            tags: vec![],
            created_at: Utc::now(),
            updated_at: Utc::now(),
        })
    }

    /// Update a session
    async fn update_session(
        &self,
        ctx: &Context<'_>,
        id: ID,
        input: UpdateSessionInput,
    ) -> FieldResult<Session> {
        let _state = ctx.data::<Arc<AppState>>()?;
        let _id = id.to_string();

        // TODO: Implement actual update
        Ok(Session {
            id,
            title: input.title.unwrap_or_default(),
            workspace_id: None,
            provider: "unknown".to_string(),
            model: None,
            message_count: 0,
            token_count: 0,
            archived: input.archived.unwrap_or(false),
            tags: input.tags.unwrap_or_default(),
            created_at: Utc::now(),
            updated_at: Utc::now(),
        })
    }

    /// Delete a session
    async fn delete_session(&self, ctx: &Context<'_>, id: ID) -> FieldResult<bool> {
        let _state = ctx.data::<Arc<AppState>>()?;
        let _id = id.to_string();

        // TODO: Implement actual deletion
        Ok(true)
    }

    /// Archive a session
    async fn archive_session(&self, ctx: &Context<'_>, id: ID) -> FieldResult<Session> {
        let _state = ctx.data::<Arc<AppState>>()?;

        // TODO: Implement actual archiving
        Ok(Session {
            id,
            title: "Archived".to_string(),
            workspace_id: None,
            provider: "unknown".to_string(),
            model: None,
            message_count: 0,
            token_count: 0,
            archived: true,
            tags: vec![],
            created_at: Utc::now(),
            updated_at: Utc::now(),
        })
    }

    /// Create an agent
    async fn create_agent(&self, ctx: &Context<'_>, input: CreateAgentInput) -> FieldResult<Agent> {
        let _state = ctx.data::<Arc<AppState>>()?;

        Ok(Agent {
            id: uuid::Uuid::new_v4().to_string().into(),
            name: input.name,
            description: input.description,
            system_prompt: input.system_prompt,
            provider_id: input.provider_id,
            model: input.model,
            temperature: input.temperature.unwrap_or(0.7),
            created_at: Utc::now(),
        })
    }

    /// Delete an agent
    async fn delete_agent(&self, ctx: &Context<'_>, id: ID) -> FieldResult<bool> {
        let _state = ctx.data::<Arc<AppState>>()?;
        let _id = id.to_string();

        // TODO: Implement actual deletion
        Ok(true)
    }

    /// Trigger a harvest
    async fn harvest(&self, ctx: &Context<'_>, providers: Option<Vec<String>>) -> FieldResult<i32> {
        let _state = ctx.data::<Arc<AppState>>()?;
        let _providers = providers;

        // TODO: Implement actual harvest
        Ok(0)
    }

    /// Trigger sync
    async fn sync(&self, ctx: &Context<'_>) -> FieldResult<bool> {
        let _state = ctx.data::<Arc<AppState>>()?;

        // TODO: Implement actual sync
        Ok(true)
    }
}

// ============================================================================
// HTTP Handlers
// ============================================================================

/// GraphQL endpoint handler
pub async fn graphql_handler(
    schema: web::Data<ChasmSchema>,
    req: GraphQLRequest,
) -> GraphQLResponse {
    schema.execute(req.into_inner()).await.into()
}

/// GraphQL Playground UI
pub async fn graphql_playground() -> impl Responder {
    HttpResponse::Ok()
        .content_type("text/html")
        .body(GRAPHQL_PLAYGROUND_HTML)
}

/// GraphQL introspection endpoint
pub async fn graphql_sdl(schema: web::Data<ChasmSchema>) -> impl Responder {
    HttpResponse::Ok()
        .content_type("text/plain")
        .body(schema.sdl())
}

/// Create the GraphQL schema
pub fn create_schema(state: Arc<AppState>) -> ChasmSchema {
    Schema::build(QueryRoot, MutationRoot, EmptySubscription)
        .data(state)
        .finish()
}

/// Configure GraphQL routes
pub fn configure_graphql_routes(cfg: &mut web::ServiceConfig, schema: ChasmSchema) {
    cfg.app_data(web::Data::new(schema))
        .service(
            web::resource("/graphql")
                .route(web::get().to(graphql_handler))
                .route(web::post().to(graphql_handler)),
        )
        .route("/graphql/playground", web::get().to(graphql_playground))
        .route("/graphql/sdl", web::get().to(graphql_sdl));
}

/// Embedded GraphQL Playground HTML
const GRAPHQL_PLAYGROUND_HTML: &str = r#"<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Chasm GraphQL Playground</title>
    <style>
        body {
            margin: 0;
            padding: 0;
            height: 100vh;
            overflow: hidden;
        }
        .custom-header {
            background: linear-gradient(135deg, #e535ab 0%, #9c27b0 100%);
            color: white;
            padding: 12px 24px;
            display: flex;
            align-items: center;
            gap: 16px;
            font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
        }
        .custom-header h1 {
            margin: 0;
            font-size: 20px;
            font-weight: 600;
        }
        .custom-header .badge {
            background: rgba(255,255,255,0.2);
            padding: 4px 12px;
            border-radius: 12px;
            font-size: 12px;
        }
        .custom-header a {
            color: white;
            text-decoration: none;
            margin-left: auto;
            opacity: 0.9;
            font-size: 14px;
        }
        .custom-header a:hover {
            opacity: 1;
        }
        #graphiql {
            height: calc(100vh - 48px);
        }
    </style>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/graphiql@3/graphiql.min.css" />
</head>
<body>
    <div class="custom-header">
        <h1>◈ Chasm GraphQL</h1>
        <span class="badge">v1.3.0</span>
        <a href="/docs">REST API →</a>
    </div>
    <div id="graphiql"></div>
    <script crossorigin src="https://cdn.jsdelivr.net/npm/react@18/umd/react.production.min.js"></script>
    <script crossorigin src="https://cdn.jsdelivr.net/npm/react-dom@18/umd/react-dom.production.min.js"></script>
    <script crossorigin src="https://cdn.jsdelivr.net/npm/graphiql@3/graphiql.min.js"></script>
    <script>
        const fetcher = GraphiQL.createFetcher({
            url: '/graphql',
        });

        const root = ReactDOM.createRoot(document.getElementById('graphiql'));
        root.render(
            React.createElement(GraphiQL, {
                fetcher,
                defaultEditorToolsVisibility: true,
            })
        );
    </script>
</body>
</html>
"#;