Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

chore(ai): pay (#2282)

Eric Hayesed6061acb6a0
diff --git a/js/app/packages/app/component/SoupChatInput.tsx b/js/app/packages/app/component/SoupChatInput.tsx
index c47830737d..cc599affd8 100644
--- a/js/app/packages/app/component/SoupChatInput.tsx
+++ b/js/app/packages/app/component/SoupChatInput.tsx
@@ -12,8 +12,10 @@ import { deriveChatName } from '@core/component/AI/util/deriveName';
import { Hotkey } from '@core/component/Hotkey';
import { Tooltip } from '@core/component/Tooltip';
import { ENABLE_SNAPSHOT_NODE } from '@core/constant/featureFlags';
+import { PaywallKey, usePaywallState } from '@core/constant/PaywallState';
import { pressedKeys } from '@core/hotkey/state';
import { TOKENS } from '@core/hotkey/tokens';
+import { isPaymentError } from '@core/util/handlePaymentError';
import { isErr } from '@core/util/maybeResult';
import { createRenameDssEntityMutation } from '@macro-entity';
import { invalidateAllSoup } from '@queries/soup/cache';
@@ -71,6 +73,10 @@ function SoupChatInputInner() {
// Create a new persistent chat
const response = await cognitionApiServiceClient.createChat({});
if (isErr(response)) {
+ if (isPaymentError(response)) {
+ const { showPaywall } = usePaywallState();
+ showPaywall(PaywallKey.CHAT_LIMIT);
+ }
return;
}
const [, { id: chatId }] = response;
diff --git a/js/app/packages/core/util/create.ts b/js/app/packages/core/util/create.ts
index 0ea5f24b3f..2d0e15b18f 100644
--- a/js/app/packages/core/util/create.ts
+++ b/js/app/packages/core/util/create.ts
@@ -18,6 +18,7 @@ import {
isCodeEditorExtensionSupported,
isCodeEditorLanguageSupported,
} from './languageQuery';
+import { isPaymentError } from './handlePaymentError';
import { err, isErr, ok } from './maybeResult';
import { refetchSoupEntity } from '@queries/soup/cache';
@@ -241,7 +242,7 @@ export async function createChat(args?: CreateChatRequest) {
invalidateUserQuota();
if (isErr(maybeChat)) {
- if (maybeChat[0][0].message.includes('403')) {
+ if (isPaymentError(maybeChat)) {
showPaywall(PaywallKey.CHAT_LIMIT);
}
return { error: 'Failed to create chat.' };
diff --git a/rust/cloud-storage/chat/src/inbound/http/router.rs b/rust/cloud-storage/chat/src/inbound/http/router.rs
index 76bd9f6e61..c674df6015 100644
--- a/rust/cloud-storage/chat/src/inbound/http/router.rs
+++ b/rust/cloud-storage/chat/src/inbound/http/router.rs
@@ -20,6 +20,7 @@ use utoipa::ToSchema;
use crate::domain::models::{ChatErr, CreateChatArgs, GetChatResponse, PatchChatArgs};
use crate::domain::ports::ChatService;
+use crate::inbound::http::extractors::ChatModelAccess;
/// Shared state for the chat router, wrapping a [`ChatService`] implementation
/// and an [`EntityAccessService`] for authorization.
@@ -160,6 +161,8 @@ pub struct CreateChatRequest {
pub async fn create_chat_handler<S: ChatService, Svc: EntityAccessService>(
State(state): State<ChatRouterState<S, Svc>>,
user: MacroUserExtractor,
+ // 402 on no perms
+ _model_access: ChatModelAccess,
Json(req): Json<CreateChatRequest>,
) -> Result<Json<StringIDResponse>, ChatHandlerErr> {
let id = state
diff --git a/rust/cloud-storage/chat/src/inbound/test.rs b/rust/cloud-storage/chat/src/inbound/test.rs
index a6de7b9d51..13ba2661aa 100644
--- a/rust/cloud-storage/chat/src/inbound/test.rs
+++ b/rust/cloud-storage/chat/src/inbound/test.rs
@@ -314,10 +314,6 @@ fn chat_basic_extension() -> Extension<ChatBasic> {
})
}
-fn mock_create_router() -> Router {
- chat_create_router(ChatRouterState::new(MockService, MockAccessService)).layer(user_extension())
-}
-
fn mock_id_router() -> Router {
chat_id_router(ChatRouterState::new(MockService, MockAccessService))
.layer(chat_basic_extension())
@@ -336,71 +332,6 @@ fn not_found_id_router() -> Router {
.layer(user_extension())
}
-fn error_create_router() -> Router {
- chat_create_router(ChatRouterState::new(ErrorService, MockAccessService))
- .layer(user_extension())
-}
-
-// -- create_chat tests --
-
-#[tokio::test]
-async fn create_chat_returns_id() {
- let req = Request::builder()
- .method("POST")
- .uri("/")
- .header("content-type", "application/json")
- .body(Body::from(r#"{"name": "My Chat"}"#))
- .unwrap();
-
- let res = mock_create_router().oneshot(req).await.unwrap();
- assert_eq!(res.status(), StatusCode::OK);
-
- let body = res.into_body().collect().await.unwrap().to_bytes();
- let response: StringIDResponse = serde_json::from_slice(&body).unwrap();
- assert_eq!(response.id, "test-chat-id");
-}
-
-#[tokio::test]
-async fn create_chat_with_default_name() {
- let req = Request::builder()
- .method("POST")
- .uri("/")
- .header("content-type", "application/json")
- .body(Body::from(r#"{}"#))
- .unwrap();
-
- let res = mock_create_router().oneshot(req).await.unwrap();
- assert_eq!(res.status(), StatusCode::OK);
-}
-
-#[tokio::test]
-async fn create_chat_repo_error_returns_500() {
- let req = Request::builder()
- .method("POST")
- .uri("/")
- .header("content-type", "application/json")
- .body(Body::from(r#"{"name": "My Chat"}"#))
- .unwrap();
-
- let res = error_create_router().oneshot(req).await.unwrap();
- assert_eq!(res.status(), StatusCode::INTERNAL_SERVER_ERROR);
-}
-
-#[tokio::test]
-async fn create_chat_without_auth_returns_401() {
- let router: Router = chat_create_router(ChatRouterState::new(MockService, MockAccessService));
-
- let req = Request::builder()
- .method("POST")
- .uri("/")
- .header("content-type", "application/json")
- .body(Body::from(r#"{"name": "My Chat"}"#))
- .unwrap();
-
- let res = router.oneshot(req).await.unwrap();
- assert_eq!(res.status(), StatusCode::UNAUTHORIZED);
-}
-
// -- get_chat tests --
#[tokio::test]