fix: chat agent integration, llama inference bug

This commit is contained in:
Shivering Ocelot committed 2026-06-05 00:05:50 -05:00
1 parent 17d3041a60
commit e3e910df2c
2 files changed
+13 -5

No files matched your search

+11 -3
View File
@@ -2,6 +2,7 @@ import re
import json import json
import inspect import inspect
import time import time
import asyncio
from temporary import generate_tmp_id from temporary import generate_tmp_id
from stream import Stream from stream import Stream
@@ -79,12 +80,13 @@ class Agent:
await self.run(run) await self.run(run)
async def on_output_event(data): async def on_output_event(stream):
print("[agent] run output " + repr(stream))
new_message = await self.app.send_chat_message( new_message = await self.app.send_chat_message(
sender=self.id, sender=self.id,
chat_id=message.chat_id, chat_id=message.chat_id,
thread_id=message.thread_id, thread_id=message.thread_id,
content=data.stream, content=stream,
) )
run.emit_event('chat-message', { run.emit_event('chat-message', {
'messageId': new_message['id'], 'messageId': new_message['id'],
@@ -92,7 +94,7 @@ class Agent:
'threadId': message.thread_id, 'threadId': message.thread_id,
}) })
run.on('output', on_output_event) await run.on_async('output', on_output_event)
return run return run
def _role_for_message(self, message): def _role_for_message(self, message):
@@ -180,6 +182,12 @@ class AgentRun:
for event in self.events: for event in self.events:
if event["name"] == event_name: if event["name"] == event_name:
callback(event["data"]) callback(event["data"])
async def on_async(self, event_name, callback):
event_loop = asyncio.get_event_loop()
def new_callback(data):
event_loop.create_task(callback(data))
self.on(event_name, new_callback)
def emit_event(self, event_name, data=None): def emit_event(self, event_name, data=None):
"""Record an event and notify existing listeners. """Record an event and notify existing listeners.
+2 -2
View File
@@ -71,7 +71,7 @@ def build_messages(system_prompt="", instruction=None, input=None, messages=None
raise Exception("Unsupported message type: " + repr(type(message))) raise Exception("Unsupported message type: " + repr(type(message)))
# shouldn't be necessary. maybe an assert instead? # shouldn't be necessary. maybe an assert instead?
messages = [normalize(msg) for msg in messages] messages = [normalize(msg) for msg in messages or []]
# default user message # default user message
prompt = input prompt = input
@@ -141,7 +141,7 @@ async def chat_completion(app,
# llama.cpp # llama.cpp
async def local_chat_completion(app, messages, model, tools=[], opts={}, agent_run=None): async def local_chat_completion(app, messages, model, tools=[], opts={}, agent_run=None):
if agent_run: if agent_run:
content_stream = agent_run.stream content_stream = agent_run.get_output_stream()
else: else:
content_stream = Stream(app) content_stream = Stream(app)