Skip to content

Commit

Permalink
Merge pull request #350 from Lumerin-protocol/dev
Browse files Browse the repository at this point in the history
Fix passthrough and indexing
  • Loading branch information
abs2023 authored Dec 10, 2024
2 parents 60c954d + 6d623cc commit 6dab567
Show file tree
Hide file tree
Showing 11 changed files with 211 additions and 87 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ on:
branches:
- main
- test
- concom
- dev
paths: ['.github/workflows/**', '**/Makefile', '**/*.go', '**/*.json', '**/*.yml', '**/*.ts', '**/*.js']

Expand Down Expand Up @@ -413,7 +414,7 @@ jobs:
name: mor-launch-win-x64.zip

release:
if: ${{ github.repository != 'MorpheusAIs/Morpheus-Lumerin-Node' && (github.event_name == 'push' && (github.ref == 'refs/heads/main' || github.ref == 'refs/heads/test' )) || github.event.inputs.create_release == 'true' }}
if: ${{ github.repository != 'MorpheusAIs/Morpheus-Lumerin-Node' && (github.event_name == 'push' && (github.ref == 'refs/heads/main' || github.ref == 'refs/heads/test' || github.ref == 'refs/heads/concom' )) || github.event.inputs.create_release == 'true' }}
runs-on: ubuntu-latest
needs:
- Ubuntu-22-x64
Expand Down
30 changes: 30 additions & 0 deletions ui-desktop/src/main/src/client/apiGateway.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,35 @@ const updateChatHistoryTitle = async ({ id, title}) => {
}
}

/**
* @param {string} address
* @param {string} endpoint
* @returns {Promise<boolean>}
*/
const checkProviderConnectivity = async ({ address, endpoint}) => {
try {
const path = `${config.chain.localProxyRouterUrl}/proxy/provider/ping`;
const response = await fetch(path, {
method: "POST",
body: JSON.stringify({
providerAddr: address,
providerUrl: endpoint
}),
});

if(!response.ok) {
return false;
}

const body = await response.json();
return !!body.ping;
}
catch (e) {
console.log("checkProviderConnectivity: Error", e)
return false;
}
}

export default {
getAllModels,
getBalances,
Expand All @@ -229,4 +258,5 @@ export default {
getChatHistory,
updateChatHistoryTitle,
deleteChatHistory,
checkProviderConnectivity
}
3 changes: 2 additions & 1 deletion ui-desktop/src/main/src/client/subscriptions/no-core.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ const listeners = {
"update-chat-history-title": handlers.updateChatHistoryTitle,
// Failover
"get-failover-setting": handlers.isFailoverEnabled,
"set-failover-setting": handlers.setFailoverSetting
"set-failover-setting": handlers.setFailoverSetting,
"check-provider-connectivity": handlers.checkProviderConnectivity
}

// Subscribe to messages where no core has to react
Expand Down
1 change: 1 addition & 0 deletions ui-desktop/src/renderer/src/client/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ const createClient = function (createStore) {
// Failover
getFailoverSetting: utils.forwardToMainProcess('get-failover-setting', 750000),
setFailoverSetting: utils.forwardToMainProcess('set-failover-setting', 750000),
checkProviderConnectivity: utils.forwardToMainProcess('check-provider-connectivity', 750000)
}

const api = {
Expand Down
112 changes: 90 additions & 22 deletions ui-desktop/src/renderer/src/components/chat/Chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,14 @@ const userMessage = { user: 'Me', role: "user", icon: "M", color: "#20dc8e" };

const Chat = (props) => {
const chatBlockRef = useRef<null | HTMLDivElement>(null);
const bidsSpinWaitClosed = useRef(false);

const [value, setValue] = useState("");
const [isLoading, setIsLoading] = useState(true);
const [messages, setMessages] = useState<any>([]);
const [isOpen, setIsOpen] = useState(false);
const [sessions, setSessions] = useState<any>();
const [providersAvailability, setProvidersAvailability] = useState<any[]>([]);

const [isSpinning, setIsSpinning] = useState(false);
const [meta, setMeta] = useState({ budget: 0, supply: 0 });
Expand Down Expand Up @@ -81,14 +83,14 @@ const Chat = (props) => {

useEffect(() => {
(async () => {
const [meta, chainData, chats, userBalances] = await Promise.all([
props.getMetaInfo(),
console.time("LOAD")
const [chainData, userSessions, chats] = await Promise.all([
props.getModelsData(),
props.client.getChatHistoryTitles() as Promise<ChatTitle[]>,
props.getBalances()]);
props.getSessionsByUser(props.address),
props.client.getChatHistoryTitles() as Promise<ChatTitle[]>]);

setBalances(userBalances)
setMeta(meta);
setBalances(chainData.userBalances)
setMeta(chainData.meta);
setChainData(chainData)

const mappedChatData = chats.reduce((res, item) => {
Expand All @@ -106,7 +108,15 @@ const Chat = (props) => {
}, [] as ChatData[])
setChatsData(mappedChatData);

const sessions = await refreshSessions(chainData?.models);
const sessions = userSessions.reduce((res, item) => {
const sessionModel = chainData.models.find(x => x.Id == item.ModelAgentId);
if (sessionModel) {
item.ModelName = sessionModel.Name;
res.push(item);
}
return res;
}, []);
setSessions(sessions);

const openSessions = sessions.filter(s => !isClosed(s));

Expand All @@ -120,6 +130,7 @@ const Chat = (props) => {

if (!openSessions.length) {
useLocalModelChat();
console.timeEnd("LOAD")
return;
}

Expand All @@ -128,10 +139,11 @@ const Chat = (props) => {

if (!latestSessionModel) {
useLocalModelChat();
console.timeEnd("LOAD")
return;
}

const openBid = latestSessionModel?.bids?.find(b => b.Id == latestSession.BidID);
const openBid = await props.getBidInfo(latestSession.BidID)

if (!openBid) {
useLocalModelChat();
Expand All @@ -141,12 +153,66 @@ const Chat = (props) => {
setSelectedBid(openBid);
setActiveSession(latestSession);
setChat({ id: generateHashId(), createdAt: new Date(), modelId: latestSessionModel.ModelAgentId });
})().then(() => {
console.timeEnd("LOAD")
})()
.then(() => {
setIsLoading(false);
})
}, [])

useEffect(() => {
if(!chainData)
return;

(async () => {
const providersMap = chainData.providers.reduce((a, b) => ({ ...a, [b.Address.toLowerCase()]: b }), {});
const modelsWithBids= (await Promise.all(
chainData.models.map(async m => {
const id = m.Id;
if(m.isLocal){
return { id }
}
const bids = (await props.getBidsByModelId(id))
.map(b => ({ ...b, ProviderData: providersMap[b.Provider.toLowerCase()], Model: m }))
.filter(b => b.ProviderData);

if(!bids.length){
return null;
}

return { id, bids }
})
)).reduce((acc, next) => {
if(!next) {
return acc;
}
const model = chainData.models.find(m => m.Id == next.id);
return [...acc, { ...model, bids: next.bids}]
}, []);

setChainData({...chainData, models: modelsWithBids})
bidsSpinWaitClosed.current = true;
})();

(async () => {
const availabilityResults = await props.getProvidersAvailability(chainData.providers);
setProvidersAvailability(availabilityResults);
})();

}, chainData)

const spinWaitForBids = async () => {
if(bidsSpinWaitClosed.current)
return;
setIsLoading(true);
while(!bidsSpinWaitClosed.current) {
await new Promise(resolve => setTimeout(resolve, 300));
}
setIsLoading(false);
}

const toggleDrawer = async () => {
spinWaitForBids();
setIsOpen((prevState) => !prevState)
}

Expand Down Expand Up @@ -228,9 +294,9 @@ const Chat = (props) => {
}
}

const refreshSessions = async (models = null) => {
const refreshSessions = async () => {
const sessions = (await props.getSessionsByUser(props.address)).reduce((res, item) => {
const sessionModel = (models || chainData.models).find(x => x.Id == item.ModelAgentId);
const sessionModel = chainData.models.find(x => x.Id == item.ModelAgentId);
if (sessionModel) {
item.ModelName = sessionModel.Name;
res.push(item);
Expand Down Expand Up @@ -272,8 +338,6 @@ const Chat = (props) => {
setSelectedModel(selectedModel);
setIsReadonly(false);

// toggleDrawer();

setChat({ ...chatData })

if (chatData.isLocal) {
Expand Down Expand Up @@ -301,6 +365,7 @@ const Chat = (props) => {
}

const handleReopen = async () => {
spinWaitForBids();
setIsLoading(true);
const newSessionId = await onOpenSession(true);
setIsReadonly(false);
Expand Down Expand Up @@ -428,11 +493,9 @@ const Chat = (props) => {
const otherMessages = memoState.filter(m => m.id != part.id);
if (imageContent) {
result = [...otherMessages, { id: part.job, user: modelName, role: "assistant", text: imageContent, isImageContent: true, ...iconProps }];
}
if (videoRawContent) {
} else if (videoRawContent) {
result = [...otherMessages, { id: part.job, user: modelName, role: "assistant", text: videoRawContent, isVideoRawContent: true, ...iconProps }];
}
else {
} else {
const text = `${message?.text || ''}${part?.choices[0]?.delta?.content || ''}`.replace("<|im_start|>", "").replace("<|im_end|>", "");
result = [...otherMessages, { id: part.id, user: modelName, role: "assistant", text: text, ...iconProps }];
}
Expand Down Expand Up @@ -521,7 +584,10 @@ const Chat = (props) => {
setIsReadonly(false);
setChat({ id: generateHashId(), createdAt: new Date(), modelId, isLocal });

const selectedModel = isLocal ? chainData.models.find((m: any) => m.Id == modelId) : chainData.models.find((m: any) => m.Id == modelId && m.bids);
const selectedModel = isLocal
? chainData.models.find((m: any) => m.Id == modelId)
: chainData.models.find((m: any) => m.Id == modelId && m.bids);

setSelectedModel(selectedModel);

if (isLocal) {
Expand All @@ -535,9 +601,7 @@ const Chat = (props) => {

if (openModelSession) {
const selectedBid = selectedModel.bids.find(b => b.Id == openModelSession.BidID && b.bids);
if (selectedBid) {
setSelectedBid(selectedBid);
}
setSelectedBid(selectedBid);
setActiveSession(openModelSession)
return;
}
Expand Down Expand Up @@ -616,7 +680,10 @@ const Chat = (props) => {
</div>
<BtnAccent
className='change-modal'
onClick={() => setOpenChangeModal(true)}>
onClick={async () => {
await spinWaitForBids();
setOpenChangeModal(true);
} }>
<IconMessagePlus></IconMessagePlus> New chat
</BtnAccent>
</div>
Expand Down Expand Up @@ -717,6 +784,7 @@ const Chat = (props) => {
models={(chainData as any)?.models}
isActive={openChangeModal}
symbol={props.symbol}
providersAvailability={providersAvailability}
onChangeModel={(eventData) => {
onCreateNewChat(eventData);
}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,7 @@ function ModelRow(props) {
const bids = props?.model?.bids || [];
const modelId = props?.model?.Id || '';
const isLocal = props?.model?.isLocal;
const lastAvailabilityCheck: Date = (() => {
if(!bids?.length) {
return new Date();
}
return bids.map(b => new Date(b.ProviderData?.availabilityUpdatedAt ?? new Date()))[0];
})();
const lastAvailabilityCheck: Date = new Date(props?.model?.lastCheck ?? new Date());

const [selected, changeSelected] = useState<any>();
const [useSelect, setUseSelect] = useState<boolean>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const RVContainer = styled(RVList)`
overflow: visible !important;
}`

const ModelSelectionModal = ({ isActive, handleClose, models, onChangeModel, symbol }) => {
const ModelSelectionModal = ({ isActive, handleClose, models, onChangeModel, symbol, providersAvailability }) => {
const [search, setSearch] = useState<string | undefined>();

if (!isActive) {
Expand All @@ -49,7 +49,30 @@ const ModelSelectionModal = ({ isActive, handleClose, models, onChangeModel, sym
}

const sortedModels = models
.map(m => ({ ...m, isOnline: m.isLocal || m.bids.some(b => b.ProviderData?.availabilityStatus != "disconnected") }))
.map(m => {
if(m.isLocal || !providersAvailability){
return ({...m, isOnline: true })
}

const info = m.bids.reduce((acc, next) => {
const entry = providersAvailability.find(pa => pa.id == next.Provider);
if(!entry) {
return acc;
}

if(entry.isOnline) {
return acc;
}

const isOnline = entry.status != "disconnected";

return {
isOnline,
lastCheck: !isOnline ? entry.time : undefined
}
}, {});
return ({ ...m, ...info })
} )
.sort((a, b) => b.isOnline - a.isOnline);

const filterdModels = search ? sortedModels.filter(m => m.Name.toLowerCase().includes(search.toLowerCase())) : sortedModels;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ const Dashboard = ({
ethCoinPrice,
loadTransactions,
getStakedFunds,
explorerUrl,
...props
}) => {
const [activeModal, setActiveModal] = useState(null)
Expand Down Expand Up @@ -174,7 +175,7 @@ const Dashboard = ({
</StakingWidjet>
<WidjetItem>
<CustomBtn
onClick={() => window.openLink(`https://sepolia.arbiscan.io/address/${address}`)}
onClick={() => window.openLink(explorerUrl)}
block
>
Transaction Explorer
Expand Down
Loading

0 comments on commit 6dab567

Please sign in to comment.