Files
autogen/python/packages/autogen-studio/frontend/src/components/utils.ts
Victor Dibia 0e985d4b40 v1 of AutoGen Studio on AgentChat (#4097)
* add skeleton worflow manager

* add test notebook

* update test nb

* add sample team spec

* refactor requirements to agentchat and ext

* add base provider to return agentchat agents from json spec

* initial api refactor, update dbmanager

* api refactor

* refactor tests

* ags api tutorial update

* ui refactor

* general refactor

* minor refactor updates

* backend api refaactor

* ui refactor and update

* implement v1 for streaming connection with ui updates

* backend refactor

* ui refactor

* minor ui tweak

* minor refactor and tweaks

* general refactor

* update tests

* sync uv.lock with main

* uv lock update
2024-11-09 14:32:24 -08:00

119 lines
2.9 KiB
TypeScript

import { IStatus } from "./types";
export const getServerUrl = () => {
return process.env.GATSBY_API_URL || "/api";
};
export function setCookie(name: string, value: any, days: number) {
let expires = "";
if (days) {
const date = new Date();
date.setTime(date.getTime() + days * 24 * 60 * 60 * 1000);
expires = "; expires=" + date.toUTCString();
}
document.cookie = name + "=" + (value || "") + expires + "; path=/";
}
export function getCookie(name: string) {
const nameEQ = name + "=";
const ca = document.cookie.split(";");
for (let i = 0; i < ca.length; i++) {
let c = ca[i];
while (c.charAt(0) == " ") c = c.substring(1, c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
}
return null;
}
export function setLocalStorage(
name: string,
value: any,
stringify: boolean = true
) {
if (stringify) {
localStorage.setItem(name, JSON.stringify(value));
} else {
localStorage.setItem(name, value);
}
}
export function getLocalStorage(name: string, stringify: boolean = true): any {
if (typeof window !== "undefined") {
const value = localStorage.getItem(name);
try {
if (stringify) {
return JSON.parse(value!);
} else {
return value;
}
} catch (e) {
return null;
}
} else {
return null;
}
}
export function fetchJSON(
url: string | URL,
payload: any = {},
onSuccess: (data: any) => void,
onError: (error: IStatus) => void,
onFinal: () => void = () => {}
) {
return fetch(url, payload)
.then(function (response) {
if (response.status !== 200) {
console.log(
"Looks like there was a problem. Status Code: " + response.status,
response
);
response.json().then(function (data) {
console.log("Error data", data);
});
onError({
status: false,
message:
"Connection error " + response.status + " " + response.statusText,
});
return;
}
return response.json().then(function (data) {
onSuccess(data);
});
})
.catch(function (err) {
console.log("Fetch Error :-S", err);
onError({
status: false,
message: `There was an error connecting to server. (${err}) `,
});
})
.finally(() => {
onFinal();
});
}
export function eraseCookie(name: string) {
document.cookie = name + "=; Path=/; Expires=Thu, 01 Jan 1970 00:00:01 GMT;";
}
export function truncateText(text: string, length = 50) {
if (text.length > length) {
return text.substring(0, length) + " ...";
}
return text;
}
export const fetchVersion = () => {
const versionUrl = getServerUrl() + "/version";
return fetch(versionUrl)
.then((response) => response.json())
.then((data) => {
return data;
})
.catch((error) => {
console.error("Error:", error);
return null;
});
};