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.

rename on send from chat (#1962)

Eric Hayesddf0849ca8bb
diff --git a/js/app/packages/app/component/SoupChatInput.tsx b/js/app/packages/app/component/SoupChatInput.tsx
index 20d3cf4d41..9d850f63c2 100644
--- a/js/app/packages/app/component/SoupChatInput.tsx
+++ b/js/app/packages/app/component/SoupChatInput.tsx
@@ -14,7 +14,8 @@ import { ENABLE_SNAPSHOT_NODE } from '@core/constant/featureFlags';
import { pressedKeys } from '@core/hotkey/state';
import { TOKENS } from '@core/hotkey/tokens';
import { isErr } from '@core/util/maybeResult';
-import { markdownToPlainText } from '@lexical-core/utils/parsers';
+import { deriveChatName } from '@core/component/AI/util/deriveName';
+import { createRenameDssEntityMutation } from '@macro-entity';
import { invalidateAllSoup } from '@queries/soup/cache';
import { cognitionApiServiceClient } from '@service-cognition/client';
import { ChatInput } from 'core/component/AI/component/input/ChatInput';
@@ -63,28 +64,27 @@ function SoupChatInputInner() {
},
});
- function deriveChatName(userQuery: string): string | undefined {
- const MAX_LENGTH = 80;
- const plainText = markdownToPlainText(userQuery);
- const firstLine = plainText
- .split('\n')
- .map((line) => line.trim())
- .filter((line) => line.length > 0)[0];
- return firstLine ? firstLine.slice(0, MAX_LENGTH) : undefined;
- }
+ const renameMutation = createRenameDssEntityMutation();
const handleSend = async (request: ChatSendInput) => {
const backgroundSend = request.metaKey;
// Create a new persistent chat
- const name = deriveChatName(request.content);
-
- const response = await cognitionApiServiceClient.createChat({ name });
+ const response = await cognitionApiServiceClient.createChat({});
if (isErr(response)) {
return;
}
const [, { id: chatId }] = response;
+ // Rename via mutation for optimistic cache updates (history, preview, soup)
+ const name = deriveChatName(request.content);
+ if (name) {
+ renameMutation.mutate({
+ entity: { type: 'chat', id: chatId, name: '', ownerId: '' },
+ newName: name,
+ });
+ }
+
if (backgroundSend) {
// Send the message in the background without navigating
cognitionApiServiceClient.sendStreamChatMessage({
diff --git a/js/app/packages/block-chat/component/Chat.tsx b/js/app/packages/block-chat/component/Chat.tsx
index 789a1a03a0..3f75d28c26 100644
--- a/js/app/packages/block-chat/component/Chat.tsx
+++ b/js/app/packages/block-chat/component/Chat.tsx
@@ -40,6 +40,8 @@ import {
import { blockHandleSignal } from '@core/signal/load';
import { useCanEdit } from '@core/signal/permissions';
import { withAnalytics } from '@coparse/analytics';
+import { deriveChatName } from '@core/component/AI/util/deriveName';
+import { createRenameDssEntityMutation } from '@macro-entity';
import { invalidateUserQuota } from '@queries/auth';
import { createCallback } from '@solid-primitives/rootless';
import { ChatInput } from 'core/component/AI/component/input/ChatInput';
@@ -125,8 +127,11 @@ function ChatInner(props: {
const { showPaywall } = usePaywallState();
const sendChatMessage = useSendChatMessage();
+ const renameMutation = createRenameDssEntityMutation();
const onSend = createCallback(async (request: ChatSendInput) => {
+ const isFirstMessage = chat.messages().length === 0;
+
chat.addMessage({
id: crypto.randomUUID(),
content: request.content,
@@ -135,6 +140,16 @@ function ChatInner(props: {
});
chat.setWaitingForStream(true);
+ if (isFirstMessage) {
+ const name = deriveChatName(request.content);
+ if (name) {
+ renameMutation.mutate({
+ entity: { type: 'chat', id: chat.chatId(), name: '', ownerId: '' },
+ newName: name,
+ });
+ }
+ }
+
const result = await sendChatMessage({
...request,
chatId: chat.chatId(),
diff --git a/js/app/packages/core/component/AI/util/deriveName.ts b/js/app/packages/core/component/AI/util/deriveName.ts
new file mode 100644
index 0000000000..17cdb78a77
--- /dev/null
+++ b/js/app/packages/core/component/AI/util/deriveName.ts
@@ -0,0 +1,12 @@
+import { markdownToPlainText } from '@lexical-core/utils/parsers';
+
+const MAX_LENGTH = 80;
+
+export function deriveChatName(userQuery: string): string | undefined {
+ const plainText = markdownToPlainText(userQuery);
+ const firstLine = plainText
+ .split('\n')
+ .map((line) => line.trim())
+ .filter((line) => line.length > 0)[0];
+ return firstLine ? firstLine.slice(0, MAX_LENGTH) : undefined;
+}