1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
//! Shared single-text model completion boundary.
//!
//! Permission review and context compaction both ask a
//! model for exactly one text answer and must treat stream surprises as typed
//! failures instead of guessing. This module owns that one contract so each
//! role does not re-derive it:
//!
//! - cooperative cancellation is classified once, from the caller token, the
//! provider error kind, and the model's own finish reason;
//! - provider setup failures and stream failures stay distinguishable;
//! - tool calls, non-stop finishes, multi-output responses, and truncated
//! streams are separate failures rather than one opaque "invalid response".
//!
//! The helper is policy-free: it does not retry, record events, or decide
//! whether a failure is acceptable. Callers map [`ModelCompletionError`] into
//! their own domain error and keep their own retry policy, and they own the
//! [`ModelStreamContext`] so provider-visible cache keys stay caller-controlled.
use futures_util::StreamExt;
use merry_llm::{
FinishDetail, FinishReason, ModelError, ModelEvent, ModelOutput, ModelProvider, ModelRequest,
ModelStreamContext, ProviderErrorKind,
};
use tokio_util::sync::CancellationToken;
/// Returns whether a provider error must be treated as cooperative cancellation.
///
/// Both `ModelError::Cancelled` and a provider-reported cancellation carry
/// [`ProviderErrorKind::Cancelled`], so the normalized kind is sufficient.
pub(crate) fn is_cancelled_model_error(error: &ModelError) -> bool {
error.kind() == ProviderErrorKind::Cancelled
}
/// Why one single-text completion did not produce an accepted answer.
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) enum ModelCompletionError {
/// The caller token, the provider, or the model cancelled the request.
Cancelled,
/// The provider failed before the stream was established.
Setup {
/// Provider-neutral error category.
kind: ProviderErrorKind,
/// Actionable provider message.
message: String,
},
/// The provider failed while the stream was being read.
Stream {
/// Provider-neutral error category.
kind: ProviderErrorKind,
/// Actionable provider message.
message: String,
},
/// The model requested a tool call, which a single-text answer must not do.
ToolCallRequested,
/// The model stopped for a reason other than a normal stop.
NonStopFinish {
/// Finish reason reported by the provider.
finish_reason: FinishReason,
/// Optional provider-neutral detail, such as an exhausted output budget.
finish_detail: Option<FinishDetail>,
},
/// The answer was not exactly one text item.
NotSingleText,
/// The stream ended before a completion event arrived.
EndedBeforeCompletion,
}
/// Drives one provider stream and returns its single text answer.
///
/// The caller supplies the stream context so provider-visible cache keys and
/// continuation state stay caller-owned. Cancellation is checked before setup
/// and while reading, so a cancelled token never opens a provider request.
pub(crate) async fn complete_single_text(
provider: &dyn ModelProvider,
request: ModelRequest,
stream_context: ModelStreamContext,
token: &CancellationToken,
) -> Result<String, ModelCompletionError> {
if token.is_cancelled() {
return Err(ModelCompletionError::Cancelled);
}
let stream_result = tokio::select! {
biased;
() = token.cancelled() => return Err(ModelCompletionError::Cancelled),
result = provider.stream_model(request, stream_context) => result,
};
let mut stream = match stream_result {
Ok(stream) => stream,
Err(error) if is_cancelled_model_error(&error) => {
return Err(ModelCompletionError::Cancelled);
}
Err(error) => {
return Err(ModelCompletionError::Setup {
kind: error.kind(),
message: error.message().to_owned(),
});
}
};
loop {
let item = tokio::select! {
biased;
() = token.cancelled() => return Err(ModelCompletionError::Cancelled),
item = stream.next() => item,
};
match item {
Some(Ok(
ModelEvent::Started
| ModelEvent::OutputTextDelta { .. }
| ModelEvent::ReasoningTextDelta { .. }
| ModelEvent::OutputProgress { .. },
)) => {}
Some(Ok(ModelEvent::ToolCallRequested { .. })) => {
return Err(ModelCompletionError::ToolCallRequested);
}
Some(Ok(ModelEvent::Completed { response })) => {
if response.finish_reason() == FinishReason::Cancelled {
return Err(ModelCompletionError::Cancelled);
}
if response.finish_reason() != FinishReason::Stop {
return Err(ModelCompletionError::NonStopFinish {
finish_reason: response.finish_reason(),
finish_detail: response.finish_detail(),
});
}
let [ModelOutput::Text { text }] = response.outputs() else {
return Err(ModelCompletionError::NotSingleText);
};
return Ok(text.clone());
}
Some(Err(error)) => {
if is_cancelled_model_error(&error) {
return Err(ModelCompletionError::Cancelled);
}
return Err(ModelCompletionError::Stream {
kind: error.kind(),
message: error.message().to_owned(),
});
}
None => return Err(ModelCompletionError::EndedBeforeCompletion),
}
}
}