Files
discord-decomp/modules/vibegrations/stores/VibegrationsChatStore.tsx
T
2026-11-01 12:12:55 +02:00

389 lines
13 KiB
TypeScript

// Metro module 15751 (f15752).
import FamilyCenterStore from "../../parent_tools/FamilyCenterStore";
import NotificationSettingsStore from "../../../stores/NotificationSettingsStore";
import SelectedChannelStore from "../../../stores/SelectedChannelStore";
import SelectedGuildStore from "../../../stores/SelectedGuildStore";
import SelfPresenceStore from "../../../stores/SelfPresenceStore";
import VibegrationsProjectStore from "./VibegrationsProjectStore";
import { DesktopNotificationTypes, Routes, StatusTypes } from "../../../Constants";
import { StaticChannelRoute } from "../../channel/ChannelConstants";
import { intl } from "../../../intl/index.native";
import VibegrationsUntranslated_messages from "../intl/VibegrationsUntranslated.messages";
import { FocusMode } from "../../user_settings/UserSettings";
import VibegrationsPlatformUtils_native from "../lib/VibegrationsPlatformUtils.native";
import { playSound } from "../../sound_playback/SoundUtils";
import NotificationActionCreators from "../../../actions/NotificationActionCreators";
import * as _Module15752 from "../../../_bundle/Module15752";
import { transitionTo } from "../../routing/router_utils";
import Module589 from "../../../_bundle/Module589";
import Dispatcher from "../../../Dispatcher";
import { HermesBuiltin } from "../../../_hermes/prelude";
let sound, c11255_14, c11255_15, c11255_16, c11255_17, c11255_18, width, applicationId, c11255_21, c11255_22, recordThinkingTransition, purgeProject, $mk97677 = function() {
return (a0) => c11255_14.has(a0.kind);
};
const newMessageFromHistory = function newMessageFromHistory(a0) {
const { role, content, ts } = a0;
const r0 = c11255_22 + 1;
c11255_22 = r0;
const r2 = {
id: "m" + r0,
role,
content,
steps: [],
created_at: ts != null ? Date.parse(ts) : Date.now()
};
if (a0.kind != null) {
r2.kind = a0.kind;
}
if (a0.proposal != null) {
r2.proposal = a0.proposal;
}
return r2;
};
recordThinkingTransition = function recordThinkingTransition(projectId) {
let r31 = c11255_17.get(projectId);
if (r31 == null) {
r31 = false;
}
const r32 = c11255_15.get(projectId);
const r7 = r32 == null;
let r6 = false;
if (!r7) {
const length = r32.length;
r6 = false;
if (length !== 0) {
const r36 = r32[r32.length - 1];
r6 = r36.role === "assistant" && !(r36.content !== "" || r36.proposal != null || r36.steps.some($mk97677()));
}
}
if (r31 !== r6) {
c11255_17.set(projectId, r6);
if (r6) {
c11255_16.delete(projectId);
} else {
const r47 = c11255_15.get(projectId);
const r50 = r47 == null ? void 0 : r47[r47.length - 1];
if (r50?.role === "assistant" && (r50.content.trim() !== "" || r50.proposal != null || r50.steps.some((step) => c11255_14.has(step.kind) && step.kind !== "terminal_error"))) {
c11255_16.set(projectId, Date.now());
} else {
c11255_16.delete(projectId);
}
const project = VibegrationsProjectStore.getProject(projectId);
if (project != null && !(NotificationSettingsStore.getDesktopType() === DesktopNotificationTypes.NEVER || SelfPresenceStore.getStatus() === StatusTypes.DND || !!FocusMode.getSetting() || !!FamilyCenterStore.isCurrentUserInRestrictedHours())) {
const isSoundDisabled = NotificationSettingsStore.isSoundDisabled(sound);
const guildId = SelectedGuildStore.getGuildId();
let r108 = guildId != null ? VibegrationsProjectStore.getSelectedProjectId(guildId) === projectId ? guildId : null : null;
const r111 = r108 != null && SelectedChannelStore.getChannelId() === StaticChannelRoute.VIBEGRATIONS && VibegrationsPlatformUtils_native.isWindowFocused();
if (r108 == null) {
r108 = project.guild_id;
}
if (r108 == null) {
r108 = project.preview_guild_id;
}
const r123 = c11255_15.get(projectId);
const r124 = r123 == null;
let r125 = null;
if (!r124) {
const length2 = r123.length;
r125 = null;
if (length2 !== 0) {
const r129 = r123[r123.length - 1];
const role = r129.role;
r125 = null;
if (role === "assistant") {
if (r129.content.trim() !== "") {
r125 = r129.content;
} else if (r129.proposal != null) {
r125 = r129.proposal.summary;
} else {
let r137 = r129.steps.length - 1;
r125 = null;
while (0 <= r137) {
const r140 = r129.steps[r137];
const kind = r140.kind;
const r1 = r137;
if (!(kind !== "error" && (r140.kind !== "terminal_error" && (r140.kind !== "build_error" && r140.kind !== "healthcheck_failed"))) && r140.message != null && r140.message !== "") {
r125 = r140.message;
break;
}
if (r140.kind === "preview_ready") {
r125 = intl.string(VibegrationsUntranslated_messages["78YNh7"]);
break;
}
r137 = r1 - 1;
r125 = null;
}
}
}
}
}
if (r125 != null) {
if (r111) {
if (!isSoundDisabled) {
playSound(sound, 0.4);
}
} else {
const r159 = r108 == null ? null : Routes.CHANNEL(r108, StaticChannelRoute.VIBEGRATIONS, projectId);
let c22417_0 = r159;
const name = project.name;
const r179 = {
tag: `vibegrations-${projectId}`,
sound: isSoundDisabled ? void 0 : sound,
volume: 0.4,
fallbackDeepLink: r159 == null ? void 0 : VibegrationsPlatformUtils_native.createNotificationDeepLink(r159),
onClick: null,
isUserAvatar: false
};
const onClick = r159 == null ? void 0 : () => transitionTo(c22417_0);
const r26 = {
notif_type: "VIBEGRATIONS_ASSISTANT_FINISHED"
};
r179.onClick = onClick;
NotificationActionCreators.showNotification(_Module15752, name, r125, r26, r179);
}
}
}
}
}
};
purgeProject = function purgeProject(item) {
let r6 = c11255_15.delete(item);
const r8 = c11255_16.delete(item);
const r10 = c11255_17.delete(item);
const r13 = c11255_18.delete(item);
if (!r6) {
r6 = r8;
}
if (!r6) {
r6 = r10;
}
if (!r6) {
r6 = r13;
}
return r6;
};
sound = "message1";
c11255_14 = new Set(["reply", "plan_proposed", "preview_ready", "terminal_error"]);
c11255_15 = new Map();
c11255_16 = new Map();
c11255_17 = new Map();
c11255_18 = new Map();
width = 0;
applicationId = null;
c11255_21 = [];
c11255_22 = 0;
class VibegrationsChatStore extends Module589.Store {
initialize() {
this.waitFor(FamilyCenterStore, NotificationSettingsStore, SelectedChannelStore, SelectedGuildStore, SelfPresenceStore, VibegrationsProjectStore);
}
getMessages(a0) {
let r5 = c11255_15.get(a0);
if (r5 == null) {
r5 = c11255_21;
}
return r5;
}
isThinking(a0) {
const r9 = c11255_15.get(a0);
const r4 = r9 == null;
let r2 = false;
if (!r4) {
const length = r9.length;
r2 = false;
if (length !== 0) {
const r14 = r9[r9.length - 1];
r2 = r14.role === "assistant" && !(r14.content !== "" || r14.proposal != null || r14.steps.some($mk97677()));
}
}
return r2;
}
getFinishedAt(a0) {
const r11 = c11255_15.get(a0);
const r4 = r11 == null;
let r2 = false;
if (!r4) {
const length = r11.length;
r2 = false;
if (length !== 0) {
const r16 = r11[r11.length - 1];
r2 = r16.role === "assistant" && !(r16.content !== "" || r16.proposal != null || r16.steps.some($mk97677()));
}
}
let r24 = null;
if (!r2) {
let r27 = c11255_16.get(a0);
if (r27 == null) {
r27 = null;
}
r24 = r27;
}
return r24;
}
getProjectUsage(a0) {
let r5 = c11255_18.get(a0);
if (r5 == null) {
r5 = null;
}
return r5;
}
getSidebarWidth() {
return width;
}
getBuilderPreviewApplicationId() {
return applicationId;
}
isAnyThinking() {
for (const item of c11255_15.keys()) {
if (!this.isThinking(item)) {
continue;
}
return true;
}
return false;
}
}
const vibegrationsChatStore = new VibegrationsChatStore(Dispatcher, {
LOGOUT: function handleLogout() {
if (c11255_15.size === 0 && c11255_16.size === 0 && c11255_17.size === 0 && c11255_18.size === 0 && width === 0 && applicationId == null) {
return false;
}
c11255_15.clear();
c11255_16.clear();
c11255_17.clear();
c11255_18.clear();
width = 0;
applicationId = null;
},
VIBEGRATIONS_CHAT_HISTORY_SET: function handleChatHistorySet(a0) {
const projectId = a0.projectId;
c11255_15.set(projectId, a0.entries.map(newMessageFromHistory));
recordThinkingTransition(projectId);
},
VIBEGRATIONS_CHAT_MESSAGE_APPEND: function handleChatMessageAppend({ projectId, content }) {
let r9 = c11255_15.get(projectId);
if (r9 == null) {
r9 = [];
}
const r18 = [, ,];
const r2 = HermesBuiltin.arraySpread(r18, r9, 0);
const r0 = c11255_22 + 1;
c11255_22 = r0;
r18[r2] = {
id: "m" + r0,
role: "user",
content,
steps: [],
created_at: Date.now()
};
const r26 = c11255_22 + 1;
c11255_22 = r26;
r18[r2 + 1] = {
id: "m" + r26,
role: "assistant",
content: "",
steps: [],
created_at: Date.now()
};
c11255_15.set(projectId, r18);
recordThinkingTransition(projectId);
},
VIBEGRATIONS_CHAT_STEP_APPEND: function handleChatStepAppend({ projectId, step }) {
const r8 = c11255_15.get(projectId);
if (r8 != null && r8.length !== 0) {
const r9 = r8[r8.length - 1];
if (r9.role === "assistant") {
c11255_15.set(projectId, [...r8.slice(0, -1), {
...r9,
steps: [...r9.steps, step]
}]);
}
}
recordThinkingTransition(projectId);
},
VIBEGRATIONS_CHAT_USAGE_SET: function handleChatUsageSet(a0) {
c11255_18.set(a0.projectId, a0.project);
},
VIBEGRATIONS_CHAT_SIDEBAR_WIDTH_SET: function handleChatSidebarWidthSet({ width: width2 }) {
if (width === width2) {
return false;
}
width = width2;
},
VIBEGRATIONS_BUILDER_PREVIEW_APPLICATION_SET: function handleBuilderPreviewApplicationSet({ applicationId: applicationId2 }) {
if (applicationId === applicationId2) {
return false;
}
applicationId = applicationId2;
},
VIBEGRATIONS_CHAT_TURN_PATCH: function handleChatTurnPatch({ projectId, patch }) {
const r8 = c11255_15.get(projectId);
if (r8 != null && r8.length !== 0) {
const r10 = r8[r8.length - 1];
if (r10.role === "assistant") {
c11255_15.set(projectId, [...r8.slice(0, -1), {
...r10,
...patch
}]);
}
}
recordThinkingTransition(projectId);
},
VIBEGRATIONS_CHAT_CONN_STATE: function handleChatConnState({ projectId, connState }) {
if (connState !== "closed" && connState !== "failed") {
return false;
}
const r8 = c11255_15.get(projectId);
if (r8 != null && r8.some((item) => item.role === "assistant" && !(item.content !== "" || item.proposal != null || item.steps.some($mk97677())))) {
c11255_15.set(projectId, r8.map((item) => item.role === "assistant" ? item.content !== "" || item.proposal != null || item.steps.some($mk97677()) ? item : {
...item,
steps: [...item.steps, {
type: "step",
kind: "terminal_error",
message: intl.string(VibegrationsUntranslated_messages["wjWm+/"])
}]
} : item));
recordThinkingTransition(projectId);
return void 0;
}
return false;
},
VIBEGRATIONS_PROJECT_DELETE_SUCCESS: function handleProjectDeleteSuccess({ projectId }) {
let r8 = c11255_15.delete(projectId);
const r10 = c11255_16.delete(projectId);
const r12 = c11255_17.delete(projectId);
const r15 = c11255_18.delete(projectId);
if (!r8) {
r8 = r10;
}
if (!r8) {
r8 = r12;
}
if (!r8) {
r8 = r15;
}
if (r8) {
return void 0;
}
return false;
},
VIBEGRATIONS_PROJECTS_FETCH_SUCCESS: function handleProjectsFetchSuccess(a0) {
const set = new Set(a0.projects.map((project) => project.id));
const set2 = new Set([...c11255_15.keys(), ...c11255_16.keys(), ...c11255_17.keys(), ...c11255_18.keys()]);
let r55 = false;
for (const item of set2) {
const r63 = set.has(item);
if (r63 ? !r63 : purgeProject(item)) {
r55 = true;
}
}
if (r55) {
return void 0;
}
return false;
}
});
export default vibegrationsChatStore;
export function turnSettled(a0) {
return a0.content !== "" || a0.proposal != null || a0.steps.some($mk97677());
}