Skip to content

Commit

Permalink
front: add origin to UserMessageType [take 2] (#5763)
Browse files Browse the repository at this point in the history
* Revert "Revert "front: add origin to UserMessageType (#5734)" (#5758)"

This reverts commit e8581a9.

* remove from internal api signature

* web

* fix
  • Loading branch information
spolu authored Jun 20, 2024
1 parent 06c932c commit aa04f0d
Show file tree
Hide file tree
Showing 16 changed files with 41 additions and 13 deletions.
1 change: 1 addition & 0 deletions connectors/src/connectors/slack/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,7 @@ async function botAnswerMessage(
slackChatBotMessage.slackFullName || slackChatBotMessage.slackUserName,
email: slackChatBotMessage.slackEmail,
profilePictureUrl: slackChatBotMessage.slackAvatar || null,
origin: "slack" as const,
},
};

Expand Down
1 change: 1 addition & 0 deletions front/components/assistant/conversation/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ export function createPlaceholderUserMessage({
profilePictureUrl: image,
timezone: Intl.DateTimeFormat().resolvedOptions().timeZone ?? "UTC",
username,
origin: "web",
},
};
}
Expand Down
2 changes: 1 addition & 1 deletion front/components/assistant_builder/AssistantBuilder.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ export default function AssistantBuilder({
icon,
})
),
[screen]
[screen, multiActionsEnabled]
);
const modalTitle = agentConfigurationId
? `Edit @${builderState.handle}`
Expand Down
2 changes: 2 additions & 0 deletions front/lib/api/assistant/conversation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -727,6 +727,7 @@ export async function* postUserMessage(
userContextFullName: context.fullName,
userContextEmail: context.email,
userContextProfilePictureUrl: context.profilePictureUrl,
userContextOrigin: context.origin,
userId: user ? user.id : null,
},
{ transaction: t }
Expand Down Expand Up @@ -1189,6 +1190,7 @@ export async function* editUserMessage(
userContextEmail: userMessageRow.userContextEmail,
userContextProfilePictureUrl:
userMessageRow.userContextProfilePictureUrl,
userContextOrigin: userMessageRow.userContextOrigin,
userId: userMessageRow.userId,
},
{ transaction: t }
Expand Down
1 change: 1 addition & 0 deletions front/lib/api/assistant/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ export async function batchRenderUserMessages(
fullName: userMessage.userContextFullName,
email: userMessage.userContextEmail,
profilePictureUrl: userMessage.userContextProfilePictureUrl,
origin: userMessage.userContextOrigin,
},
} satisfies UserMessageType;
return { m, rank: message.rank, version: message.version };
Expand Down
6 changes: 6 additions & 0 deletions front/lib/models/assistant/conversation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type {
ConversationVisibility,
MessageVisibility,
ParticipantActionType,
UserMessageOrigin,
} from "@dust-tt/types";
import type {
CreationOptional,
Expand Down Expand Up @@ -175,6 +176,7 @@ export class UserMessage extends Model<
declare userContextFullName: string | null;
declare userContextEmail: string | null;
declare userContextProfilePictureUrl: string | null;
declare userContextOrigin: UserMessageOrigin | null;

declare userId: ForeignKey<User["id"]> | null;
}
Expand Down Expand Up @@ -220,6 +222,10 @@ UserMessage.init(
type: DataTypes.STRING(2048),
allowNull: true,
},
userContextOrigin: {
type: DataTypes.STRING,
allowNull: true,
},
},
{
modelName: "user_message",
Expand Down
8 changes: 1 addition & 7 deletions front/lib/workspace_usage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,7 @@ export async function unsafeGetUsageData(
WHEN COUNT(DISTINCT arc."id") + COUNT(DISTINCT atqc."id") + COUNT(DISTINCT adarc."id") > 1 THEN 'multiActions'
ELSE NULL
END AS "actionType",
CASE
WHEN um."id" IS NOT NULL THEN
CASE
WHEN um."userId" IS NOT NULL THEN 'web'
ELSE 'slack'
END
END AS "source"
um."userContextOrigin" AS "source"
FROM
"messages" m
JOIN
Expand Down
5 changes: 5 additions & 0 deletions front/migrations/20240620_backfill_user_message_origin.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
UPDATE user_messages SET "userContextOrigin" = 'slack' WHERE "userContextOrigin" IS NULL AND "userContextProfilePictureUrl" ILIKE '%slack%';
UPDATE user_messages SET "userContextOrigin" = 'web' WHERE "userContextOrigin" IS NULL AND "userId" IS NOT NULL;
UPDATE user_messages SET "userContextOrigin" = 'api' WHERE "userContextOrigin" IS NULL AND "userId" IS NULL;

SELECT "userContextOrigin", COUNT(*) FROM user_messages GROUP BY "userContextOrigin";
2 changes: 2 additions & 0 deletions front/migrations/db/migration_24.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- Migration created on Jun 20, 2024
ALTER TABLE "public"."user_messages" ADD COLUMN "userContextOrigin" VARCHAR(255);
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,10 @@ async function handler(
conversation,
content,
mentions,
context,
context: {
...context,
origin: context.origin ?? "api",
},
},
{ resolveAfterFullGeneration: blocking === true }
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ async function handler(
fullName: message.context.fullName,
email: message.context.email,
profilePictureUrl: message.context.profilePictureUrl,
origin: message.context.origin ?? "api",
},
},
{ resolveAfterFullGeneration: blocking === true }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ async function handler(
fullName: user.fullName,
email: user.email,
profilePictureUrl: context.profilePictureUrl ?? user.image,
origin: "web",
},
},
{ resolveAfterFullGeneration: false }
Expand Down
8 changes: 4 additions & 4 deletions front/pages/api/w/[wId]/assistant/conversations/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,10 +149,9 @@ async function handler(
}

if (message) {
/* If a message was provided we do await for the message to be created
before returning the conversation along with the message.
PostUserMessageWithPubSub returns swiftly since it only waits for the
initial message creation event (or error) */
// If a message was provided we do await for the message to be created before returning the
// conversation along with the message. PostUserMessageWithPubSub returns swiftly since it
// only waits for the initial message creation event (or error) */
const messageRes = await postUserMessageWithPubSub(
auth,
{
Expand All @@ -165,6 +164,7 @@ async function handler(
fullName: user.fullName,
email: user.email,
profilePictureUrl: message.context.profilePictureUrl,
origin: "web",
},
},
{ resolveAfterFullGeneration: false }
Expand Down
1 change: 1 addition & 0 deletions front/temporal/labs/activities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ export async function processTranscriptActivity(
fullName: user.name,
email: user.email,
profilePictureUrl: user.imageUrl,
origin: null,
},
},
contentFragment: {
Expand Down
7 changes: 7 additions & 0 deletions types/src/front/api_handlers/public/assistant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ export const PublicPostMessagesRequestBodySchema = t.intersection([
fullName: t.union([t.string, t.null]),
email: t.union([t.string, t.null]),
profilePictureUrl: t.union([t.string, t.null]),
origin: t.union([
t.literal("slack"),
t.literal("web"),
t.literal("api"),
t.null,
t.undefined,
]),
}),
}),
t.partial({
Expand Down
3 changes: 3 additions & 0 deletions types/src/front/assistant/conversation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,15 @@ export type MessageWithRankType = WithRank<MessageType>;
* User messages
*/

export type UserMessageOrigin = "slack" | "web" | "api";

export type UserMessageContext = {
username: string;
timezone: string;
fullName: string | null;
email: string | null;
profilePictureUrl: string | null;
origin: UserMessageOrigin | null;
};

export type UserMessageType = {
Expand Down

0 comments on commit aa04f0d

Please sign in to comment.