mirror of
https://wget.la/https://github.com/leookun/cursor-byok
synced 2026-12-01 09:15:33 +08:00
fix: shell
This commit is contained in:
1 parent
df053c3720
commit
5450fc76e2
25 files changed
+1367
-101
No files matched your search
@@ -2,7 +2,12 @@ use std::sync::Arc;
|
||||
|
||||
use tokio_util::sync::CancellationToken;
|
||||
|
||||
use crate::{client::ClientPort, model::PreparedRun, provider::Provider, store::Store};
|
||||
use crate::{
|
||||
client::{ClientCommand, ClientPort},
|
||||
model::PreparedRun,
|
||||
provider::Provider,
|
||||
store::Store,
|
||||
};
|
||||
|
||||
use super::{RunEngine, RunOutcome, RunRegistry};
|
||||
|
||||
@@ -26,6 +31,7 @@ impl RunActor {
|
||||
&self,
|
||||
prepared: PreparedRun,
|
||||
client: ClientPort,
|
||||
commands: tokio::sync::mpsc::Sender<ClientCommand>,
|
||||
cancellation: CancellationToken,
|
||||
) -> tokio::task::JoinHandle<RunOutcome> {
|
||||
let run_id = prepared.run_id.clone();
|
||||
@@ -35,6 +41,7 @@ impl RunActor {
|
||||
conversation_id.clone(),
|
||||
run_id.clone(),
|
||||
cancellation.clone(),
|
||||
commands,
|
||||
)
|
||||
.await;
|
||||
let actor = self.clone();
|
||||
|
||||
+81
-13
@@ -4,7 +4,10 @@ use std::sync::Arc;
|
||||
use tokio_util::sync::CancellationToken;
|
||||
|
||||
use crate::{
|
||||
client::{ClientEvent, ClientPort, CommitBarrier, CommitCause, StateCommitted},
|
||||
client::{
|
||||
ClientCommand, ClientEvent, ClientPort, CommitBarrier, CommitCause, MessageInsertion,
|
||||
StateCommitted,
|
||||
},
|
||||
model::{
|
||||
CanonicalMessage, MessageContent, Origin, PreparedRun, Role, RunAction, ToolRoundAssistant,
|
||||
ToolRoundId, Usage,
|
||||
@@ -158,6 +161,7 @@ impl RunEngine {
|
||||
calls: round.calls.clone(),
|
||||
recovered_started_at_ms: Some(round.started_at_ms),
|
||||
},
|
||||
Vec::new(),
|
||||
)
|
||||
.await
|
||||
{
|
||||
@@ -242,21 +246,27 @@ impl RunEngine {
|
||||
&cycle_cancellation,
|
||||
);
|
||||
tokio::pin!(cycle);
|
||||
let cycle = tokio::select! {
|
||||
result = &mut cycle => result,
|
||||
let mut pending_insertions = Vec::new();
|
||||
let cycle = loop {
|
||||
tokio::select! {
|
||||
result = &mut cycle => break result,
|
||||
command = client.commands.recv() => {
|
||||
let message = match command {
|
||||
Some(crate::client::ClientCommand::RuntimeMessage(message)) => message,
|
||||
Some(crate::client::ClientCommand::RuntimeEvent(event)) => event.into_message(),
|
||||
Some(crate::client::ClientCommand::Cancel) => {
|
||||
Some(ClientCommand::InsertMessages(insertion)) => {
|
||||
pending_insertions.push(insertion);
|
||||
continue;
|
||||
}
|
||||
Some(ClientCommand::RuntimeMessage(message)) => message,
|
||||
Some(ClientCommand::RuntimeEvent(event)) => event.into_message(),
|
||||
Some(ClientCommand::Cancel) => {
|
||||
cycle_cancellation.cancel();
|
||||
return (RunOutcome::Cancelled, usage);
|
||||
}
|
||||
Some(crate::client::ClientCommand::ClientClosed { error }) => {
|
||||
Some(ClientCommand::ClientClosed { error }) => {
|
||||
cycle_cancellation.cancel();
|
||||
return (RunOutcome::Failed(RunFailure::Client(error)), usage);
|
||||
}
|
||||
Some(crate::client::ClientCommand::ToolResult(_)) => {
|
||||
Some(ClientCommand::ToolResult(_)) => {
|
||||
cycle_cancellation.cancel();
|
||||
return (
|
||||
RunOutcome::Failed(RunFailure::Protocol(
|
||||
@@ -284,6 +294,19 @@ impl RunEngine {
|
||||
}
|
||||
}
|
||||
}
|
||||
revision = match append_insertions(
|
||||
&self.store,
|
||||
prepared,
|
||||
client,
|
||||
cancellation,
|
||||
revision,
|
||||
std::mem::take(&mut pending_insertions),
|
||||
)
|
||||
.await
|
||||
{
|
||||
Ok((revision, _)) => revision,
|
||||
Err(outcome) => return (outcome, usage),
|
||||
};
|
||||
revision = match append_runtime_message(
|
||||
&self.store,
|
||||
prepared,
|
||||
@@ -294,11 +317,12 @@ impl RunEngine {
|
||||
)
|
||||
.await
|
||||
{
|
||||
Ok(revision) => revision,
|
||||
Ok((revision, _)) => revision,
|
||||
Err(outcome) => return (outcome, usage),
|
||||
};
|
||||
continue 'model;
|
||||
}
|
||||
}
|
||||
};
|
||||
let cycle = match cycle {
|
||||
Ok(cycle) => cycle,
|
||||
@@ -413,6 +437,27 @@ impl RunEngine {
|
||||
Ok(revision) => revision,
|
||||
Err(error) => return (RunOutcome::Failed(error.into()), usage),
|
||||
};
|
||||
if !pending_insertions.is_empty() {
|
||||
let inserted = match append_insertions(
|
||||
&self.store,
|
||||
prepared,
|
||||
client,
|
||||
cancellation,
|
||||
revision,
|
||||
pending_insertions,
|
||||
)
|
||||
.await
|
||||
{
|
||||
Ok((next, inserted)) => {
|
||||
revision = next;
|
||||
inserted
|
||||
}
|
||||
Err(outcome) => return (outcome, usage),
|
||||
};
|
||||
if inserted {
|
||||
continue 'model;
|
||||
}
|
||||
}
|
||||
let (barrier, ready) = CommitBarrier::before_continue();
|
||||
if emit(
|
||||
client,
|
||||
@@ -453,6 +498,7 @@ impl RunEngine {
|
||||
calls: cycle.calls,
|
||||
recovered_started_at_ms: None,
|
||||
},
|
||||
pending_insertions,
|
||||
)
|
||||
.await
|
||||
{
|
||||
@@ -683,14 +729,36 @@ fn fallback_summary(messages: &[CanonicalMessage]) -> String {
|
||||
)
|
||||
}
|
||||
|
||||
async fn append_runtime_message(
|
||||
pub(super) async fn append_insertions(
|
||||
store: &Store,
|
||||
prepared: &PreparedRun,
|
||||
client: &mut ClientPort,
|
||||
cancellation: &CancellationToken,
|
||||
mut revision: crate::model::RevisionId,
|
||||
insertions: Vec<MessageInsertion>,
|
||||
) -> std::result::Result<(crate::model::RevisionId, bool), RunOutcome> {
|
||||
let mut inserted_any = false;
|
||||
for insertion in insertions {
|
||||
for message in insertion.messages {
|
||||
let (next, inserted) =
|
||||
append_runtime_message(store, prepared, client, cancellation, revision, message)
|
||||
.await?;
|
||||
revision = next;
|
||||
inserted_any |= inserted;
|
||||
}
|
||||
let _ = insertion.delivered.send(());
|
||||
}
|
||||
Ok((revision, inserted_any))
|
||||
}
|
||||
|
||||
pub(super) async fn append_runtime_message(
|
||||
store: &Store,
|
||||
prepared: &PreparedRun,
|
||||
client: &mut ClientPort,
|
||||
cancellation: &CancellationToken,
|
||||
revision: crate::model::RevisionId,
|
||||
message: CanonicalMessage,
|
||||
) -> std::result::Result<crate::model::RevisionId, RunOutcome> {
|
||||
) -> std::result::Result<(crate::model::RevisionId, bool), RunOutcome> {
|
||||
let event_id = message.runtime_event_id.clone().ok_or_else(|| {
|
||||
RunOutcome::Failed(RunFailure::Protocol(
|
||||
"runtime message has no event identity".into(),
|
||||
@@ -706,7 +774,7 @@ async fn append_runtime_message(
|
||||
.await
|
||||
.map_err(|error| RunOutcome::Failed(error.into()))?;
|
||||
if !inserted {
|
||||
return Ok(revision);
|
||||
return Ok((revision, false));
|
||||
}
|
||||
let (barrier, ready) = CommitBarrier::before_continue();
|
||||
emit(
|
||||
@@ -721,7 +789,7 @@ async fn append_runtime_message(
|
||||
.await
|
||||
.map_err(|_| client_failure())?;
|
||||
wait_for_state_ready(ready, cancellation).await?;
|
||||
Ok(revision)
|
||||
Ok((revision, true))
|
||||
}
|
||||
|
||||
async fn hydrate_tool_images(
|
||||
|
||||
@@ -3,7 +3,10 @@ use std::{collections::HashMap, sync::Arc};
|
||||
use tokio::sync::Mutex;
|
||||
use tokio_util::sync::CancellationToken;
|
||||
|
||||
use crate::model::{ConversationId, RunId};
|
||||
use crate::{
|
||||
client::{ClientCommand, MessageInsertion},
|
||||
model::{CanonicalMessage, ConversationId, RunId},
|
||||
};
|
||||
|
||||
#[derive(Clone, Default)]
|
||||
pub struct RunRegistry {
|
||||
@@ -13,6 +16,7 @@ pub struct RunRegistry {
|
||||
struct ActiveRun {
|
||||
run_id: RunId,
|
||||
cancellation: CancellationToken,
|
||||
commands: tokio::sync::mpsc::Sender<ClientCommand>,
|
||||
}
|
||||
|
||||
impl RunRegistry {
|
||||
@@ -21,12 +25,14 @@ impl RunRegistry {
|
||||
conversation_id: ConversationId,
|
||||
run_id: RunId,
|
||||
cancellation: CancellationToken,
|
||||
commands: tokio::sync::mpsc::Sender<ClientCommand>,
|
||||
) {
|
||||
let previous = self.active.lock().await.insert(
|
||||
conversation_id,
|
||||
ActiveRun {
|
||||
run_id: run_id.clone(),
|
||||
cancellation,
|
||||
commands,
|
||||
},
|
||||
);
|
||||
if let Some(previous) = previous.filter(|previous| previous.run_id != run_id) {
|
||||
@@ -34,6 +40,37 @@ impl RunRegistry {
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn insert_messages(
|
||||
&self,
|
||||
conversation_id: &ConversationId,
|
||||
messages: Vec<CanonicalMessage>,
|
||||
) -> bool {
|
||||
if messages.is_empty() {
|
||||
return true;
|
||||
}
|
||||
let commands = self
|
||||
.active
|
||||
.lock()
|
||||
.await
|
||||
.get(conversation_id)
|
||||
.map(|run| run.commands.clone());
|
||||
let Some(commands) = commands else {
|
||||
return false;
|
||||
};
|
||||
let (delivered, delivery) = tokio::sync::oneshot::channel();
|
||||
if commands
|
||||
.send(ClientCommand::InsertMessages(MessageInsertion {
|
||||
messages,
|
||||
delivered,
|
||||
}))
|
||||
.await
|
||||
.is_err()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
delivery.await.is_ok()
|
||||
}
|
||||
|
||||
pub async fn release(&self, conversation_id: &ConversationId, run_id: &RunId) {
|
||||
let mut active = self.active.lock().await;
|
||||
if active
|
||||
|
||||
@@ -1,7 +1,10 @@
|
||||
use tokio_util::sync::CancellationToken;
|
||||
|
||||
use crate::{
|
||||
client::{ClientCommand, ClientEvent, ClientPort, CommitBarrier, CommitCause, StateCommitted},
|
||||
client::{
|
||||
ClientCommand, ClientEvent, ClientPort, CommitBarrier, CommitCause, MessageInsertion,
|
||||
StateCommitted,
|
||||
},
|
||||
model::{PreparedRun, RevisionId, ToolCall, ToolRoundAssistant, ToolRoundId},
|
||||
store::Store,
|
||||
};
|
||||
@@ -22,6 +25,7 @@ pub(super) async fn execute(
|
||||
cancellation: &CancellationToken,
|
||||
mut revision: RevisionId,
|
||||
round: ToolRound,
|
||||
insertions: Vec<MessageInsertion>,
|
||||
) -> std::result::Result<RevisionId, RunOutcome> {
|
||||
let ToolRound {
|
||||
id: round_id,
|
||||
@@ -66,7 +70,10 @@ pub(super) async fn execute(
|
||||
.await?;
|
||||
|
||||
let mut remaining = calls.len();
|
||||
let mut pending_runtime_messages = Vec::new();
|
||||
let mut pending_runtime_messages = insertions
|
||||
.into_iter()
|
||||
.map(PendingRuntimeMessage::Insertion)
|
||||
.collect::<Vec<_>>();
|
||||
while remaining > 0 {
|
||||
let command = tokio::select! {
|
||||
_ = cancellation.cancelled() => return Err(RunOutcome::Cancelled),
|
||||
@@ -116,10 +123,13 @@ pub(super) async fn execute(
|
||||
}
|
||||
}
|
||||
Some(ClientCommand::RuntimeEvent(event)) => {
|
||||
pending_runtime_messages.push(event.into_message());
|
||||
pending_runtime_messages.push(PendingRuntimeMessage::Message(event.into_message()));
|
||||
}
|
||||
Some(ClientCommand::RuntimeMessage(message)) => {
|
||||
pending_runtime_messages.push(message);
|
||||
pending_runtime_messages.push(PendingRuntimeMessage::Message(message));
|
||||
}
|
||||
Some(ClientCommand::InsertMessages(insertion)) => {
|
||||
pending_runtime_messages.push(PendingRuntimeMessage::Insertion(insertion))
|
||||
}
|
||||
Some(ClientCommand::Cancel) => return Err(RunOutcome::Cancelled),
|
||||
Some(ClientCommand::ClientClosed { error }) => {
|
||||
@@ -128,40 +138,42 @@ pub(super) async fn execute(
|
||||
None => return Err(client_failure()),
|
||||
}
|
||||
}
|
||||
for message in pending_runtime_messages {
|
||||
let event_id = message.runtime_event_id.clone().ok_or_else(|| {
|
||||
RunOutcome::Failed(RunFailure::Protocol(
|
||||
"runtime message has no event identity".into(),
|
||||
))
|
||||
})?;
|
||||
let (next, inserted) = store
|
||||
.append_message_once(
|
||||
&prepared.conversation_id,
|
||||
&prepared.run_id,
|
||||
revision,
|
||||
&message,
|
||||
)
|
||||
.await
|
||||
.map_err(failed)?;
|
||||
revision = next;
|
||||
if inserted {
|
||||
let (barrier, ready) = CommitBarrier::before_continue();
|
||||
send(
|
||||
client,
|
||||
ClientEvent::StateCommitted(StateCommitted {
|
||||
revision_id: revision,
|
||||
tool_round_version: 0,
|
||||
cause: CommitCause::RuntimeEvent { event_id },
|
||||
barrier,
|
||||
}),
|
||||
)
|
||||
.await?;
|
||||
super::engine::wait_for_state_ready(ready, cancellation).await?;
|
||||
for pending in pending_runtime_messages {
|
||||
match pending {
|
||||
PendingRuntimeMessage::Message(message) => {
|
||||
revision = super::engine::append_runtime_message(
|
||||
store,
|
||||
prepared,
|
||||
client,
|
||||
cancellation,
|
||||
revision,
|
||||
message,
|
||||
)
|
||||
.await?
|
||||
.0;
|
||||
}
|
||||
PendingRuntimeMessage::Insertion(insertion) => {
|
||||
revision = super::engine::append_insertions(
|
||||
store,
|
||||
prepared,
|
||||
client,
|
||||
cancellation,
|
||||
revision,
|
||||
vec![insertion],
|
||||
)
|
||||
.await?
|
||||
.0;
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(revision)
|
||||
}
|
||||
|
||||
enum PendingRuntimeMessage {
|
||||
Message(crate::model::CanonicalMessage),
|
||||
Insertion(MessageInsertion),
|
||||
}
|
||||
|
||||
async fn send(client: &ClientPort, event: ClientEvent) -> std::result::Result<(), RunOutcome> {
|
||||
client
|
||||
.events
|
||||
|
||||
Reference in new issue
Block a user