Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import { createDeepCopy, scrollToTab } from "@sparrow/common/utils";
import {
startLoading,
stopLoading,
updateAiChatBotModelforTeam,
getAiChatBotModelForTeam,
} from "../../../../../../../packages/@sparrow-common/src/store";
import {
CompareArray,
Expand Down Expand Up @@ -111,6 +113,7 @@ import { UserService } from "@app/services/user.service";
import { getClientUser, getSelfhostUrls } from "@app/utils/jwt";
import constants from "@app/constants/constants";
import * as curlconverter from "curlconverter";
import { TeamRepository } from "@app/repositories/team.repository";

import * as Sentry from "@sentry/svelte";
import { CollectionNavigationTabEnum } from "@sparrow/common/types/workspace/collection-tab";
Expand All @@ -133,6 +136,7 @@ class RestExplorerViewModel {
private guestUserRepository = new GuestUserRepository();
private compareArray = new CompareArray();
private initTab = new InitTab();
private teamRepository = new TeamRepository();

/**
* Service
Expand Down Expand Up @@ -1445,9 +1449,24 @@ class RestExplorerViewModel {
*/
public updateAIModel = async (_modelName: string) => {
const progressiveTab = createDeepCopy(this._tab.getValue());
// update tab's ai model
progressiveTab.property.request.ai.aiModelName = _modelName;
this.tab = progressiveTab;
this.tabRepository.updateTab(progressiveTab.tabId, progressiveTab);
await this.tabRepository.updateTab(progressiveTab.tabId, progressiveTab);

// persist selection globally mapped by teamId
try {
const workspaceId = progressiveTab.path?.workspaceId;
if (!workspaceId) return;

const workspaceDoc = await this.readWorkspace(workspaceId);
const teamId = workspaceDoc?.team?.teamId || "";
if (teamId) {
updateAiChatBotModelforTeam(teamId, _modelName);
}
} catch (err) {
console.error("updateAIModel: failed to update model store", err);
}
};

/**
Expand Down Expand Up @@ -3244,9 +3263,9 @@ class RestExplorerViewModel {

const [selfhostBackendUrl] = getSelfhostUrls();
if (selfhostBackendUrl) {
return selfhostBackendUrl;
return selfhostBackendUrl;
}

if (hubUrl && constants.APP_ENVIRONMENT_PATH !== "local") {
const envSuffix = constants.APP_ENVIRONMENT_PATH;
return `${hubUrl}/${envSuffix}`;
Expand Down Expand Up @@ -3715,6 +3734,7 @@ class RestExplorerViewModel {
* @param Prompt - Prompt from the user
*/
public generateAIResponseWS = async (prompt = "") => {
debugger;
await this.updateRequestState({ isChatbotGeneratingResponse: true });
const componentData = this._tab.getValue();

Expand Down Expand Up @@ -3750,6 +3770,7 @@ class RestExplorerViewModel {
let responseMessageId = uuidv4(); // Generate a single message ID for the entire response
let accumulatedMessage = ""; // Track the accumulated message content
let messageCreated = false; // Flag to track if we've created the initial message
let selectedAIModel = getAiChatBotModelForTeam(teamId) || "deepseek";

const socketResponse = await this.aiAssistentWebSocketService.sendMessage(
componentData.tabId,
Expand All @@ -3758,7 +3779,7 @@ class RestExplorerViewModel {
prompt,
JSON.stringify(apiData),
formattedConversations,
"deepseek",
selectedAIModel,
"chat",
teamId,
);
Expand Down Expand Up @@ -3980,6 +4001,7 @@ class RestExplorerViewModel {
* @returns A promise that resolves to the response from the AI assistant service.
*/
public generateAiResponse = async (prompt = "") => {
debugger;
// Set the request state to indicate that a response is being generated
await this.updateRequestState({ isChatbotGeneratingResponse: true });
const componentData = this._tab.getValue();
Expand Down Expand Up @@ -4819,6 +4841,19 @@ class RestExplorerViewModel {
await this.fetchCollections(progressiveTab?.path?.workspaceId);
}
};

/**
* @description - This function will provide the plan name for hub.
*/
public getPlanName = async () => {
const response = await this.workspaceRepository.getActiveWorkspaceDoc();
const teamId = response?._data?.team?.teamId || "";
const teamData = await this.teamRepository.getTeamDoc(teamId);
const teamDoc = teamData.toMutableJSON();
if (teamDoc) {
return teamDoc?.plan?.name;
}
};
}

export default RestExplorerViewModel;
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
import type { CollectionDocType } from "@app/models/collection.model";
import { RequestDatasetEnum } from "@sparrow/common/types/workspace";
import type { KeyValuePair } from "@sparrow/common/interfaces/request.interface";
import {
getAiChatBotModelForTeam,
aiChatBotModelByTeam,
} from "@sparrow/common/store";

export let tab;
export let isTourGuideOpen = false;
Expand Down Expand Up @@ -55,6 +59,14 @@
let newModifiedContent: string | KeyValuePair[];
let mergeViewRequestDatasetType: RequestDatasetEnum;
let scriptComponent = null;
let planName: string = "";
let teamId: string = "";
let selectedAIModel: string = "deepseek";

$: teamId = currentWorkspace ? currentWorkspace?.team?.teamId : "";

// reactive selected model for this team (fallback to "deepseek")
$: selectedAIModel = teamId ? $aiChatBotModelByTeam.get(teamId) : "deepseek";

const restExplorerDataStoreSubscriber = restExplorerDataStore.subscribe(
(_webSocketMap) => {
Expand Down Expand Up @@ -90,6 +102,13 @@
});
};

/**
* Find the plan of the hub
*/
const getPlanName = async () => {
planName = await _viewModel.getPlanName();
};

let activeWorkspaceSubscriber;
let collectionSubscriber;

Expand Down Expand Up @@ -151,6 +170,7 @@
}
debouncedAPIUpdater(tab);
findUserRole();
getPlanName();
prevTabName = tab?.name || "";
prevTabId = tab?.tabId || "";
}
Expand Down Expand Up @@ -230,6 +250,7 @@
{isGuestUser}
{isLoginBannerActive}
{isSharedWorkspace}
{planName}
bind:isMergeViewEnableForRequestBody
bind:isMergeViewEnableForParams
bind:isMergeViewEnableForHeaders
Expand Down Expand Up @@ -284,4 +305,5 @@
isCloseRequestTestScriptDemo={_viewModel.updateIsRequestTabScriptDemo}
requestTabTestScriptDemoCompleted={_viewModel.handleRequestTestScriptDemoCompleted}
onGenerateTestCases={_viewModel.generateTestCases}
selectedModel={selectedAIModel}
/>
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import { XMLParser, XMLBuilder } from "fast-xml-parser";
import {
startLoading,
stopLoading,
updateAiChatBotModelforTeam,
getAiChatBotModelForTeam,
} from "../../../../../../../packages/@sparrow-common/src/store";
import {
CompareArray,
Expand Down Expand Up @@ -118,6 +120,7 @@ import { UserService } from "src/services/user.service";
import * as xpath from "xpath";
import { DOMParser } from "xmldom";
import { JSONPath } from "jsonpath-plus";
import { TeamRepository } from "@app/repositories/team.repository";

class RestExplorerViewModel {
/**
Expand All @@ -132,6 +135,7 @@ class RestExplorerViewModel {
private guestUserRepository = new GuestUserRepository();
private compareArray = new CompareArray();
private initTab = new InitTab();
private teamRepository = new TeamRepository();

/**
* Service
Expand Down Expand Up @@ -1456,9 +1460,24 @@ class RestExplorerViewModel {
*/
public updateAIModel = async (_modelName: string) => {
const progressiveTab = createDeepCopy(this._tab.getValue());
// update tab's ai model
progressiveTab.property.request.ai.aiModelName = _modelName;
this.tab = progressiveTab;
this.tabRepository.updateTab(progressiveTab.tabId, progressiveTab);
await this.tabRepository.updateTab(progressiveTab.tabId, progressiveTab);

// persist selection globally mapped by teamId
try {
const workspaceId = progressiveTab.path?.workspaceId;
if (!workspaceId) return;

const workspaceDoc = await this.readWorkspace(workspaceId);
const teamId = workspaceDoc?.team?.teamId || "";
if (teamId) {
updateAiChatBotModelforTeam(teamId, _modelName);
}
} catch (err) {
console.error("updateAIModel: failed to update model store", err);
}
};

/**
Expand Down Expand Up @@ -3743,6 +3762,7 @@ class RestExplorerViewModel {
let responseMessageId = uuidv4(); // Generate a single message ID for the entire response
let accumulatedMessage = ""; // Track the accumulated message content
let messageCreated = false; // Flag to track if we've created the initial message
let selectedAIModel = getAiChatBotModelForTeam(teamId) || "deepseek";

const socketResponse = await this.aiAssistentWebSocketService.sendMessage(
componentData.tabId,
Expand All @@ -3751,7 +3771,7 @@ class RestExplorerViewModel {
prompt,
JSON.stringify(apiData),
formattedConversations,
"deepseek",
selectedAIModel,
"chat",
teamId,
);
Expand Down Expand Up @@ -4774,6 +4794,18 @@ class RestExplorerViewModel {
await this.fetchCollections(progressiveTab?.path?.workspaceId);
}
};
/**
* @description - This function will provide the plan Name for the hub.
*/
public getPlanName = async () => {
const response = await this.workspaceRepository.getActiveWorkspaceDoc();
const teamId = response?._data?.team?.teamId || "";
const teamData = await this.teamRepository.getTeamDoc(teamId);
const teamDoc = teamData.toMutableJSON();
if (teamDoc) {
return teamDoc?.plan?.name;
}
};

/**
* Fixes the test script for the current request tab using AI assistance.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import type { CollectionDocType } from "@app/models/collection.model";
import { RequestDatasetEnum } from "@sparrow/common/types/workspace";
import type { KeyValuePair } from "@sparrow/common/interfaces/request.interface";
import { aiChatBotModelByTeam } from "@sparrow/common/store";

export let tab;
export let isTourGuideOpen = false;
Expand Down Expand Up @@ -54,7 +55,10 @@
let isMergeViewLoading = false;
let newModifiedContent: string | KeyValuePair[];
let mergeViewRequestDatasetType: RequestDatasetEnum;
let planName: string = "";
let scriptComponent = null;
let teamId: string = "";
let selectedAIModel: string = "deepseek";

const restExplorerDataStoreSubscriber = restExplorerDataStore.subscribe(
(_webSocketMap) => {
Expand All @@ -72,6 +76,10 @@
}
});

$: teamId = currentWorkspace ? currentWorkspace?.team?.teamId : "";

$: selectedAIModel = teamId ? $aiChatBotModelByTeam.get(teamId) : "deepseek";

$: {
restExplorerData = webSocketMap?.get(tab?.tabId);
}
Expand All @@ -90,6 +98,13 @@
});
};

/**
* Find the plan of the hub
*/
const getPlanName = async () => {
planName = await _viewModel.getPlanName();
};

let activeWorkspaceSubscriber;
let collectionSubscriber;

Expand Down Expand Up @@ -151,6 +166,7 @@
}
debouncedAPIUpdater(tab);
findUserRole();
getPlanName();
prevTabName = tab?.name || "";
prevTabId = tab?.tabId || "";
}
Expand Down Expand Up @@ -237,6 +253,7 @@
{isGuestUser}
{isSharedWorkspace}
{isLoginBannerActive}
{planName}
onOpenCollection={_viewModel.openCollection}
onSendRequest={_viewModel.sendRequest}
onCancelRequest={_viewModel.cancelRequest}
Expand Down Expand Up @@ -285,4 +302,5 @@
isCloseRequestTestScriptDemo={_viewModel.updateIsRequestTabScriptDemo}
requestTabTestScriptDemoCompleted={_viewModel.handleRequestTestScriptDemoCompleted}
onGenerateTestCases={_viewModel.generateTestCases}
selectedModel={selectedAIModel}
/>
Loading
Loading