Skip to content

Commit 01bdf36

Browse files
committed
update
1 parent efd264a commit 01bdf36

File tree

3 files changed

+33
-25
lines changed

3 files changed

+33
-25
lines changed

src/api/main.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ async def lifespan(app: fastapi.FastAPI):
4747
)
4848

4949
# TODO: add more files are not supported for citation at the moment
50+
"""
5051
file_names = ["product_info_1.md", "product_info_2.md"]
5152
files: Dict[str, str] = {}
5253
for file_name in file_names:
@@ -63,7 +64,7 @@ async def lifespan(app: fastapi.FastAPI):
6364
tool_set.add(file_search_tool)
6465
6566
print(f"ToolResource: {tool_set.resources}")
66-
67+
"""
6768
agent = await ai_client.agents.create_agent(
6869
model="gpt-4o-mini", name="my-assistant", instructions="You are helpful assistant", tools = tool_set.definitions, tool_resources=tool_set.resources
6970
)
@@ -72,8 +73,8 @@ async def lifespan(app: fastapi.FastAPI):
7273

7374
bp.ai_client = ai_client
7475
bp.agent = agent
75-
bp.vector_store = vector_store
76-
bp.files = files
76+
#bp.vector_store = vector_store
77+
#bp.files = files
7778

7879
yield
7980

@@ -85,8 +86,8 @@ async def stop_server():
8586
await bp.ai_client.agents.delete_file(file_id)
8687
print(f"Deleted file {file_id}")
8788

88-
await bp.ai_client.agents.delete_vector_store(bp.vector_store.id)
89-
print(f"Deleted vector store {bp.vector_store.id}")
89+
#await bp.ai_client.agents.delete_vector_store(bp.vector_store.id)
90+
#print(f"Deleted vector store {bp.vector_store.id}")
9091

9192
await bp.ai_client.agents.delete_agent(bp.agent.id)
9293

src/api/static/ChatClient.js

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -31,46 +31,45 @@ class ChatClient {
3131
console.error('Fetch failed:', error);
3232
}
3333
}
34-
34+
3535
handleMessages(stream) {
3636
let messageDiv = null;
3737
let accumulatedContent = '';
3838
let isStreaming = true;
3939
let buffer = '';
4040
let annotations = [];
41-
41+
4242
const reader = stream.getReader();
4343
const decoder = new TextDecoder();
44-
44+
4545
const readStream = async () => {
4646
while (true) {
4747
const { done, value } = await reader.read();
4848
if (done) {
49-
break;
49+
break; // The stream finished normally.
5050
}
51-
51+
5252
buffer += decoder.decode(value, { stream: true });
53-
5453
let boundary = buffer.indexOf('\n');
54+
55+
5556
while (boundary !== -1) {
5657
const chunk = buffer.slice(0, boundary).trim();
5758
buffer = buffer.slice(boundary + 1);
58-
59+
5960
if (chunk.startsWith('data: ')) {
6061
const data = JSON.parse(chunk.slice(6));
61-
62+
6263
if (data.type === "stream_end") {
63-
reader.releaseLock();
64+
// Just break out of the loop, no releaseLock().
6465
messageDiv = null;
6566
accumulatedContent = '';
67+
break;
6668
} else {
6769
if (!messageDiv) {
6870
messageDiv = this.ui.createAssistantMessageDiv();
69-
if (!messageDiv) {
70-
console.error("Failed to create message div.");
71-
}
7271
}
73-
72+
7473
if (data.type === "completed_message") {
7574
this.ui.clearAssistantMessage(messageDiv);
7675
accumulatedContent = data.content;
@@ -79,19 +78,26 @@ class ChatClient {
7978
} else {
8079
accumulatedContent += data.content;
8180
}
82-
83-
this.ui.appendAssistantMessage(messageDiv, accumulatedContent, isStreaming, annotations);
81+
82+
this.ui.appendAssistantMessage(
83+
messageDiv,
84+
accumulatedContent,
85+
isStreaming,
86+
annotations
87+
);
8488
}
8589
}
86-
90+
8791
boundary = buffer.indexOf('\n');
8892
}
8993
}
9094
};
91-
92-
readStream().catch(error => {
93-
console.error('Stream reading failed:', error);
94-
});
95+
96+
// Ensure errors surface nicely:
97+
readStream()
98+
.catch(error => {
99+
console.error('Stream reading failed:', error);
100+
});
95101
}
96102

97103
}

src/files/product_info_3.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Need to stop

0 commit comments

Comments
 (0)