-
Notifications
You must be signed in to change notification settings - Fork 1
Improve Performance: CoPilot: users experience #110
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
quge009
wants to merge
34
commits into
dev
Choose a base branch
from
lequ/copilot/improve-perf
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
34 commits
Select commit
Hold shift + click to select a range
d79b3c5
add: stream output
quge009 4278f23
change: use frontend to assign turnId
quge009 7e01649
modify: state update help message
quge009 434d8ec
improve: auto scroll
quge009 b68c194
adding more status update message
quge009 1adf126
minor: add new status update message
quge009 92d2869
Add feature: multi user concurrency
quge009 a0c50a0
fix: minor bug for non-streaming api
quge009 f4c3f1b
improve user experience: post unauthorized access information to unau…
quge009 7776476
code cleanup: remove unnecessary llmsession instances
quge009 1e1285c
code refactor: stage 0
quge009 99c280a
code refactor: stage 1
quge009 7f989e7
code refactor: stage 2
quge009 856f861
code refactor: stage 3, use the same llmsession for each conversation…
quge009 9f8a8b0
code refactor: stage 4, change ltp into a class for easier state mana…
quge009 798d738
code refactor: stage 5, fix chuck accumulation bug
quge009 173a9b2
code refactor: stage 6, smart help into a class
quge009 01384ec
minor bug fix
quge009 16f4c63
improve: response latency by merge small llm chat calls into one clas…
quge009 27d8f64
improve: response latency, by reuse llmsession for requests from the …
quge009 138ba06
fix bug: missing import, changed prompt file name
quge009 7ba1374
debugging: log the question parsing output into the dev log
quge009 b9f3d52
resolve review comment: add appropriate null handling
quge009 e42bea9
update: nginx configuration to add the new /copilot/api/stream endpoi…
quge009 372cfe8
Update src/copilot-chat/src/copilot_agent/copilot_conversation.py
quge009 3c41e32
remove unnecessary comment
quge009 32a2906
Update src/copilot-chat/src/copilot_agent/copilot_turn.py
quge009 4019f8a
resolve review comment: remove consle log
quge009 f8a5211
Update src/copilot-chat/src/copilot_agent/copilot_service.py
quge009 66d3a90
Update src/copilot-chat/src/copilot_agent/ltp/ltp.py
quge009 735ec38
Update src/copilot-chat/src/copilot_agent/copilot_conversation.py
quge009 53ddf4d
update: remove unused function
quge009 34b95ae
improve: robustness, gracefully handling if classification fail
quge009 cb34b54
change classifier version for deployment
quge009 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,8 +21,8 @@ export default function ChatBox() { | |
| // Use local backend when running the dev server (npm start), | ||
| // and use the relative path for production builds (npm run build). | ||
| const REMOTE_SERVER_URL = process.env.NODE_ENV === 'development' | ||
| ? 'http://127.0.0.1:60000/copilot/api/operation' | ||
| : '/copilot/api/operation'; | ||
| ? 'http://127.0.0.1:60000/copilot/api/stream' | ||
| : '/copilot/api/stream'; | ||
|
|
||
| const makeChatRequest = async (e: React.FormEvent) => { | ||
| e.preventDefault(); | ||
|
|
@@ -38,6 +38,17 @@ export default function ChatBox() { | |
| setPrompt(""); | ||
| setLoading(true); | ||
| try { | ||
| // create a stable turnId and include it in the payload so server will echo/use it | ||
| const turnId = uuidv4(); | ||
| const messageInfo = { | ||
| userId: paiuser, | ||
| convId: currentConversationId, | ||
| turnId: turnId, | ||
| timestamp: Math.floor(Date.now()), | ||
| timestampUnit: "ms", | ||
| type: "question", | ||
| }; | ||
|
|
||
| const payload = { | ||
| async_: false, | ||
| stream: false, | ||
|
|
@@ -48,18 +59,14 @@ export default function ChatBox() { | |
| username: paiuser, | ||
| restToken: restServerToken, | ||
| jobToken: jobServerToken, | ||
| currentJob: null // currentJob ? { id: currentJob.id, name: currentJob.name, username: currentJob.username, status: currentJob.status, ip: currentJob.ip, port: currentJob.port } : null | ||
| currentJob: null | ||
| }, | ||
| messageInfo: { | ||
| userId: paiuser, | ||
| convId: currentConversationId, | ||
| turnId: uuidv4(), | ||
| timestamp: Math.floor(Date.now()), | ||
| timestampUnit: "ms", | ||
| type: "question", | ||
| } | ||
| messageInfo: messageInfo | ||
| } | ||
| }; | ||
|
|
||
| // Create assistant placeholder and attach the same messageInfo (turnId) so feedback maps to this response | ||
| useChatStore.getState().addChat({ role: "assistant", message: "", timestamp: new Date(), messageInfo }); | ||
| const response = await fetch(REMOTE_SERVER_URL, { | ||
| method: "POST", | ||
| headers: { | ||
|
|
@@ -69,15 +76,93 @@ export default function ChatBox() { | |
| body: JSON.stringify(payload), | ||
| }); | ||
| if (!response.ok) throw new Error("Remote server error"); | ||
| const data = await response.json(); | ||
| if (data?.data?.answer !== "skip") { | ||
| useChatStore.getState().addChat({ | ||
| role: "assistant", | ||
| message: data?.data?.answer ?? "No answer found", | ||
| timestamp: new Date(), | ||
| messageInfo: data?.data?.message_info, // Store the message_info from response | ||
| }); | ||
|
|
||
| const reader = response.body?.getReader(); | ||
| if (!reader) throw new Error('No response body for streaming'); | ||
| const decoder = new TextDecoder(); | ||
| // Buffer incoming bytes and parse SSE-style messages (separated by '\n\n') | ||
| let buffer = ''; | ||
| while (true) { | ||
| const { value, done: readerDone } = await reader.read(); | ||
| if (value) { | ||
| buffer += decoder.decode(value, { stream: true }); | ||
| } | ||
|
|
||
| // Process all complete SSE messages in buffer | ||
| let sepIndex; | ||
| while ((sepIndex = buffer.indexOf('\n\n')) !== -1) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am not sure if this will cause infinite loop here when buffer.indexOf('\n\n') !== -1, please make sure that the loop can be breaked from the loop no matter which parts will be executed |
||
| const rawEvent = buffer.slice(0, sepIndex); | ||
| buffer = buffer.slice(sepIndex + 2); | ||
|
|
||
| // Extract data: lines and join with newline to preserve original formatting | ||
| const lines = rawEvent.split(/\n/); | ||
| const dataParts: string[] = []; | ||
| let isDoneEvent = false; | ||
| for (const line of lines) { | ||
| if (line.startsWith('data:')) { | ||
| dataParts.push(line.slice(5)); | ||
| } else if (line.startsWith('event:')) { | ||
| const ev = line.slice(6).trim(); | ||
| if (ev === 'done') isDoneEvent = true; | ||
| } | ||
| } | ||
|
|
||
| if (dataParts.length > 0) { | ||
| const dataStr = dataParts.join('\n'); | ||
| // If the server sent a JSON 'append' event, append to last assistant message | ||
| let handled = false; | ||
| const trimmed = dataStr.trim(); | ||
| if (trimmed.startsWith('{')) { | ||
| try { | ||
| const parsed = JSON.parse(trimmed); | ||
| if (parsed && parsed.type === 'append' && typeof parsed.text === 'string') { | ||
| useChatStore.getState().appendToLastAssistant(parsed.text); | ||
| handled = true; | ||
| } | ||
| else if (parsed && parsed.type === 'meta' && parsed.messageInfo) { | ||
| // attach backend-generated messageInfo (turnId etc.) to the last assistant message | ||
| useChatStore.getState().setLastAssistantMessageInfo(parsed.messageInfo); | ||
| handled = true; | ||
| } | ||
| } catch (e) { | ||
| // not JSON, fall through to full replace | ||
| } | ||
| } | ||
|
|
||
| if (!handled) { | ||
| // If server sent a full snapshot repeatedly (common when backend doesn't send structured append events), | ||
| // detect the already-displayed prefix and append only the new suffix. This avoids blinking and missing lines | ||
| // during rapid streaming of many list items. | ||
| const store = useChatStore.getState(); | ||
| const msgs = store.chatMsgs; | ||
| let lastAssistant = ""; | ||
| for (let i = msgs.length - 1; i >= 0; i--) { | ||
| if (msgs[i].role === 'assistant') { | ||
| lastAssistant = msgs[i].message || ''; | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| if (lastAssistant && dataStr.startsWith(lastAssistant)) { | ||
| const suffix = dataStr.slice(lastAssistant.length); | ||
| if (suffix.length > 0) store.appendToLastAssistant(suffix); | ||
| } else { | ||
| // Fallback: replace the last assistant message with the full reconstructed text | ||
| store.replaceLastAssistant(dataStr); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if (isDoneEvent) { | ||
| // stream finished | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| if (readerDone) break; | ||
| } | ||
|
|
||
| // After the streaming loop, do not alter the assembled markdown so newlines are preserved | ||
| } catch (err) { | ||
| toast.error("Failed to get response from remote server"); | ||
| } | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,19 +1,24 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| import React from "react"; | ||
| import { cn } from "../libs/utils"; | ||
|
|
||
| interface PaneProps extends React.HTMLAttributes<HTMLDivElement> {} | ||
|
|
||
| export const Pane: React.FC<PaneProps> = ({ children, className }) => { | ||
| return ( | ||
| <div | ||
| className={cn( | ||
| "bg-background flex-1 p-4 border-2 border-gray-300 rounded-md overflow-y-auto flex flex-col", | ||
| className | ||
| )} | ||
| > | ||
| {children} | ||
| </div> | ||
| ); | ||
| }; | ||
| export const Pane = React.forwardRef<HTMLDivElement, PaneProps>( | ||
| ({ children, className }, ref) => { | ||
| return ( | ||
| <div | ||
| ref={ref} | ||
| className={cn( | ||
| "bg-background flex-1 p-4 border-2 border-gray-300 rounded-md overflow-y-auto flex flex-col", | ||
| className | ||
| )} | ||
| > | ||
| {children} | ||
| </div> | ||
| ); | ||
| } | ||
| ); | ||
| Pane.displayName = "Pane"; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.