Skip to content

Commit

Permalink
feat: support production mode for playground.
Browse files Browse the repository at this point in the history
  • Loading branch information
plutoless committed Dec 25, 2024
1 parent 264f550 commit 837374a
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 26 deletions.
5 changes: 4 additions & 1 deletion playground/src/common/constant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,4 +128,7 @@ export const MOBILE_ACTIVE_TAB_MAP = {

export const isLLM = (extensionName: string) => {
return extensionName === "llm" || extensionName === "v2v";
}
}

// export const isProduction = process.env.NODE_ENV === "production";
export const isProduction = true;
2 changes: 1 addition & 1 deletion playground/src/common/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ export const apiCheckCompatibleMessages = async (payload: {
}

export const apiFetchGraphs = async (): Promise<Graph[]> => {
let resp: any = await axios.get(`/api/dev/v1/graphs`)
let resp: any = await axios.get(`/api/agents/graphs`)
return resp.data.data.map((graph: any) => ({
id: graph.name,
autoStart: graph.auto_start,
Expand Down
12 changes: 9 additions & 3 deletions playground/src/components/Chat/ChatCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
useAppSelector,
GRAPH_OPTIONS,
isRagGraph,
isProduction,
} from "@/common";
import {
setRtmConnected,
Expand Down Expand Up @@ -111,9 +112,14 @@ export default function ChatCard(props: { className?: string }) {
{/* Action Bar */}
<div className="flex w-full flex-wrap items-center justify-end gap-x-2 gap-y-2">
<RemoteGraphSelect />
<RemoteModuleCfgSheet />
<RemotePropertyCfgSheet />
{isRagGraph(graphName) && <PdfSelect />}
{
!isProduction ? (
<>
<RemoteModuleCfgSheet />
<RemotePropertyCfgSheet />
</>
) : null
}
</div>
{/* Chat messages would go here */}
<MessageList />
Expand Down
67 changes: 46 additions & 21 deletions playground/src/store/reducers/global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import { createAsyncThunk, createSlice, PayloadAction } from "@reduxjs/toolkit";
import {
EMobileActiveTab,
DEFAULT_OPTIONS,
COLOR_LIST
COLOR_LIST,
isProduction
} from "@/common/constant";
import {
apiReloadPackage,
Expand Down Expand Up @@ -172,27 +173,47 @@ export const globalSlice = createSlice({
});

// Initialize graph data
export const initializeGraphData = createAsyncThunk(
"global/initializeGraphData",
async (_, { dispatch }) => {
await apiReloadPackage();
const [fetchedGraphs, modules] = await Promise.all([
apiFetchGraphs(),
apiFetchInstalledAddons(),
]);
dispatch(setGraphList(fetchedGraphs.map((graph) => graph.id)));
dispatch(setAddonModules(modules));
}
);

let initializeGraphData;
// Fetch graph details
export const fetchGraphDetails = createAsyncThunk(
"global/fetchGraphDetails",
async (graphId: string, { dispatch }) => {
const graph = await apiFetchGraphDetails(graphId);
dispatch(setGraph(graph));
}
);
let fetchGraphDetails;

if (!isProduction) {
// only for development, below requests depend on dev-server
initializeGraphData = createAsyncThunk(
"global/initializeGraphData",
async (_, { dispatch }) => {
await apiReloadPackage();
const [fetchedGraphs, modules] = await Promise.all([
apiFetchGraphs(),
apiFetchInstalledAddons(),
]);
dispatch(setGraphList(fetchedGraphs.map((graph) => graph.id)));
dispatch(setAddonModules(modules));
}
);
fetchGraphDetails = createAsyncThunk(
"global/fetchGraphDetails",
async (graphId: string, { dispatch }) => {
const graph = await apiFetchGraphDetails(graphId);
dispatch(setGraph(graph));
}
);
} else {
initializeGraphData = createAsyncThunk(
"global/initializeGraphData",
async (_, { dispatch }) => {
const fetchedGraphs = await apiFetchGraphs();
dispatch(setGraphList(fetchedGraphs.map((graph) => graph.id)));
}
);
fetchGraphDetails = createAsyncThunk(
"global/fetchGraphDetails",
async (graphId: string, { dispatch }) => {
// Do nothing in production
return
}
);
}

// Update a graph
export const updateGraph = createAsyncThunk(
Expand Down Expand Up @@ -232,4 +253,8 @@ export const {
setAddonModules,
} = globalSlice.actions;

export {
initializeGraphData, fetchGraphDetails
}

export default globalSlice.reducer;
48 changes: 48 additions & 0 deletions server/internal/http_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,53 @@ func (s *HttpServer) handlerList(c *gin.Context) {
s.output(c, codeSuccess, filtered)
}

func (s *HttpServer) handleGraphs(c *gin.Context) {
// read the property.json file and get the graph list from predefined_graphs, return the result as response
// for every graph object returned, only keep the name and auto_start fields
content, err := os.ReadFile(PropertyJsonFile)
if err != nil {
slog.Error("failed to read property.json file", "err", err, logTag)
s.output(c, codeErrReadFileFailed, http.StatusInternalServerError)
return
}

var propertyJson map[string]interface{}
err = json.Unmarshal(content, &propertyJson)
if err != nil {
slog.Error("failed to parse property.json file", "err", err, logTag)
s.output(c, codeErrParseJsonFailed, http.StatusInternalServerError)
return
}

tenSection, ok := propertyJson["_ten"].(map[string]interface{})
if !ok {
slog.Error("Invalid format: _ten section missing", logTag)
s.output(c, codeErrParseJsonFailed, http.StatusInternalServerError)
return
}

predefinedGraphs, ok := tenSection["predefined_graphs"].([]interface{})
if !ok {
slog.Error("Invalid format: predefined_graphs missing or not an array", logTag)
s.output(c, codeErrParseJsonFailed, http.StatusInternalServerError)
return
}

// Filter the graph with the matching name
var graphs []map[string]interface{}
for _, graph := range predefinedGraphs {
graphMap, ok := graph.(map[string]interface{})
if ok {
graphs = append(graphs, map[string]interface{}{
"name": graphMap["name"],
"auto_start": graphMap["auto_start"],
})
}
}

s.output(c, codeSuccess, graphs)
}

func (s *HttpServer) handleAddonDefaultProperties(c *gin.Context) {
// Get the base directory path
baseDir := "./agents/ten_packages/extension"
Expand Down Expand Up @@ -623,6 +670,7 @@ func (s *HttpServer) Start() {
r.POST("/start", s.handlerStart)
r.POST("/stop", s.handlerStop)
r.POST("/ping", s.handlerPing)
r.GET("/graphs", s.handleGraphs)
r.GET("/dev-tmp/addons/default-properties", s.handleAddonDefaultProperties)
r.POST("/token/generate", s.handlerGenerateToken)
r.GET("/vector/document/preset/list", s.handlerVectorDocumentPresetList)
Expand Down

0 comments on commit 837374a

Please sign in to comment.