Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions openless-all/app/crates/openless-core/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2542,6 +2542,7 @@ impl OpenLessBackend {
&self.deps.task_spawner,
Arc::clone(&resources),
Arc::clone(&self.deps.dictation_engine).start_transcription_with_progress(
Arc::clone(&self.deps.task_spawner),
session_id,
Arc::clone(&context),
Arc::clone(&partials) as Arc<dyn TextStreamSink>,
Expand Down
36 changes: 22 additions & 14 deletions openless-all/app/crates/openless-core/src/asr/bailian.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,15 @@ const PER_ADDR_TCP_TIMEOUT: Duration = Duration::from_millis(1500);

fn default_port_for_request(
request: &tokio_tungstenite::tungstenite::handshake::client::Request,
) -> Result<u16, WsError> {
) -> Result<u16, Box<WsError>> {
let default_port = match request.uri().scheme_str() {
Some("ws") => 80,
Some("wss") => 443,
_ => {
return Err(WsError::Url(
tokio_tungstenite::tungstenite::error::UrlError::UnsupportedUrlScheme,
))
)
.into())
}
};
Ok(request.uri().port_u16().unwrap_or(default_port))
Expand All @@ -72,13 +73,14 @@ async fn connect_ws_to_addrs(
WsStream,
tokio_tungstenite::tungstenite::handshake::client::Response,
),
WsError,
Box<WsError>,
> {
if addrs.is_empty() {
return Err(WsError::Io(std::io::Error::new(
std::io::ErrorKind::NotFound,
"no addresses for websocket endpoint",
)));
))
.into());
}

let mut last_err = None;
Expand All @@ -101,12 +103,14 @@ async fn connect_ws_to_addrs(
}
}

Err(last_err.unwrap_or_else(|| {
WsError::Io(std::io::Error::new(
std::io::ErrorKind::NotConnected,
"no tcp candidate",
))
}))
Err(last_err
.unwrap_or_else(|| {
WsError::Io(std::io::Error::new(
std::io::ErrorKind::NotConnected,
"no tcp candidate",
))
})
.into())
}

async fn connect_ws_prefer_ipv4(
Expand All @@ -116,7 +120,7 @@ async fn connect_ws_prefer_ipv4(
WsStream,
tokio_tungstenite::tungstenite::handshake::client::Response,
),
WsError,
Box<WsError>,
> {
let port = default_port_for_request(&request)?;
let host = request.uri().host().unwrap_or("").to_string();
Expand Down Expand Up @@ -869,13 +873,17 @@ mod tests {
let request = "https://localhost/path".into_client_request().unwrap();
let explicit_port = "https://localhost:443/path".into_client_request().unwrap();
assert!(matches!(
default_port_for_request(&request),
default_port_for_request(&request)
.as_ref()
.map_err(|error| error.as_ref()),
Err(WsError::Url(
tokio_tungstenite::tungstenite::error::UrlError::UnsupportedUrlScheme
))
));
assert!(matches!(
default_port_for_request(&explicit_port),
default_port_for_request(&explicit_port)
.as_ref()
.map_err(|error| error.as_ref()),
Err(WsError::Url(
tokio_tungstenite::tungstenite::error::UrlError::UnsupportedUrlScheme
))
Expand Down Expand Up @@ -943,7 +951,7 @@ mod tests {
let request = "ws://localhost/path".into_client_request().unwrap();
let result = connect_ws_to_addrs(request, Vec::new()).await;
assert!(
matches!(result, Err(WsError::Io(error)) if error.kind() == std::io::ErrorKind::NotFound)
matches!(result.as_ref().map_err(|error| error.as_ref()), Err(WsError::Io(error)) if error.kind() == std::io::ErrorKind::NotFound)
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ pub struct DashScopeMultimodalASR {
api_key: String,
base_url: String,
model: String,
protocol: crate::provider_rules::BailianProtocol,
buffer: Mutex<Vec<u8>>,
}

Expand All @@ -56,16 +57,24 @@ impl DashScopeMultimodalASR {
api_key,
base_url,
model,
protocol: crate::provider_rules::BailianProtocol::Auto,
buffer: Mutex::new(Vec::new()),
}
}

pub fn with_protocol(mut self, protocol: crate::provider_rules::BailianProtocol) -> Self {
self.protocol = protocol;
self
}

pub fn buffer_duration_ms(&self) -> u64 {
crate::asr::pcm::pcm_duration_ms(&self.buffer.lock())
}

pub fn transcribe_timeout(&self, audio_secs: f64) -> Duration {
if protocol_for_model(&self.model) == Some(DashScopeBatchProtocol::AsyncTranscription) {
if self.protocol.batch_protocol(&self.model)
== Some(DashScopeBatchProtocol::AsyncTranscription)
{
let pcm_bytes = (audio_secs.max(0.0) * 32_000.0).ceil() as u64;
return async_upload_timeout(pcm_bytes.saturating_add(44))
+ Duration::from_secs(ASYNC_TASK_POLL_TIMEOUT_SECS + ASYNC_WORKFLOW_OVERHEAD_SECS);
Expand Down Expand Up @@ -98,7 +107,9 @@ impl DashScopeMultimodalASR {
}

let duration_ms = crate::asr::pcm::pcm_duration_ms(pcm);
if protocol_for_model(&self.model) == Some(DashScopeBatchProtocol::AsyncTranscription) {
if self.protocol.batch_protocol(&self.model)
== Some(DashScopeBatchProtocol::AsyncTranscription)
{
let samples: Vec<i16> = pcm
.as_chunks::<2>()
.0
Expand Down Expand Up @@ -129,7 +140,11 @@ impl DashScopeMultimodalASR {
.map(|chunk| i16::from_le_bytes([chunk[0], chunk[1]]))
.collect();
let wav = encode_wav_16k_mono(&samples);
let body = dashscope_multimodal_body(&self.model, &wav);
let audio_data = format!(
"data:audio/wav;base64,{}",
base64::engine::general_purpose::STANDARD.encode(&wav)
);
let body = dashscope_multimodal_body_with_protocol(&self.model, &audio_data, self.protocol);
let url = generation_url(&self.base_url)?;
let request_timeout =
self.transcribe_timeout(crate::asr::pcm::pcm_duration_ms(pcm) as f64 / 1000.0);
Expand Down Expand Up @@ -471,7 +486,19 @@ pub fn dashscope_multimodal_body(model: &str, wav: &[u8]) -> Value {
}

pub fn dashscope_multimodal_body_from_uri(model: &str, audio_uri: &str) -> Value {
if crate::provider_rules::dashscope_uses_qwen_sync_envelope(model) {
dashscope_multimodal_body_with_protocol(
model,
audio_uri,
crate::provider_rules::BailianProtocol::Auto,
)
}

pub fn dashscope_multimodal_body_with_protocol(
model: &str,
audio_uri: &str,
protocol: crate::provider_rules::BailianProtocol,
) -> Value {
if protocol.uses_qwen_envelope(model) {
return serde_json::json!({
"model": model,
"input": {
Expand Down Expand Up @@ -623,6 +650,42 @@ pub fn extract_dashscope_text(json: &Value) -> String {

#[cfg(test)]
mod tests {
#[test]
fn manual_protocol_controls_envelope_and_timeout_without_model_inference() {
use super::*;
use crate::provider_rules::BailianProtocol;
let uri = "data:audio/wav;base64,AAAA";
let standard = dashscope_multimodal_body_with_protocol(
"qwen3-asr-flash",
uri,
BailianProtocol::Multimodal,
);
assert_eq!(
standard["input"]["messages"][0]["content"][0]["input_audio"]["data"],
uri
);
let qwen = dashscope_multimodal_body_with_protocol(
"unknown-model",
uri,
BailianProtocol::QwenMultimodal,
);
assert_eq!(qwen["input"]["messages"][0]["content"][0]["audio"], uri);
assert!(qwen.get("parameters").is_none());
let asynchronous = DashScopeMultimodalASR::new(
"key".into(),
"https://example.com".into(),
"unknown-model".into(),
)
.with_protocol(BailianProtocol::AsyncTranscription);
assert!(asynchronous.transcribe_timeout(1.0) > Duration::from_secs(600));
let synchronous = DashScopeMultimodalASR::new(
"key".into(),
"https://example.com".into(),
"fun-asr".into(),
)
.with_protocol(BailianProtocol::Multimodal);
assert_eq!(synchronous.transcribe_timeout(1.0), Duration::from_secs(30));
}
use super::*;
use crate::asr::AudioConsumer;
use std::io::{Read, Write};
Expand Down
93 changes: 78 additions & 15 deletions openless-all/app/crates/openless-core/src/cloud_providers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ struct CloudTranscriptionPreparation {
context: Arc<DictationContext>,
provider_type: String,
effective_provider: String,
bailian_protocol: crate::provider_rules::BailianProtocol,
kind: crate::provider_rules::ActiveAsrProviderKind,
model: String,
api_key: String,
Expand Down Expand Up @@ -401,9 +402,14 @@ async fn prepare_cloud_transcription(
.clone()
.or_else(|| value(ASR_MODEL_ACCOUNT))
.unwrap_or_default();
let effective_provider =
crate::provider_rules::resolve_effective_asr_provider(&provider_type, &model)
.map_err(|message| BackendError::new(BackendErrorCode::InvalidArgument, message))?;
let bailian_protocol = crate::provider_rules::BailianProtocol::from_config(
&provider_type,
value(ASR_ADVANCED_CONFIG_ACCOUNT).as_deref(),
)
.map_err(|message| BackendError::new(BackendErrorCode::InvalidArgument, message))?;
let effective_provider = bailian_protocol
.resolve_provider(&provider_type, &model)
.map_err(|message| BackendError::new(BackendErrorCode::InvalidArgument, message))?;
let advanced_config = crate::provider_rules::advanced_asr_config_for(
&provider_type,
value(ASR_ADVANCED_CONFIG_ACCOUNT).as_deref(),
Expand All @@ -413,6 +419,7 @@ async fn prepare_cloud_transcription(
provider_type,
kind: crate::provider_rules::active_asr_provider_kind(&effective_provider),
effective_provider,
bailian_protocol,
api_key: value(ASR_API_KEY_ACCOUNT).unwrap_or_default(),
endpoint: value(ASR_ENDPOINT_ACCOUNT).unwrap_or_default(),
advanced_config,
Expand Down Expand Up @@ -622,24 +629,22 @@ async fn build_cloud_transcription_session(
let stored_endpoint = non_blank_owned(endpoint)
.unwrap_or_else(|| crate::asr::dashscope_multimodal::DEFAULT_ENDPOINT.to_string());
let endpoint = if provider_type == crate::asr::bailian::PROVIDER_ID {
let protocol =
match crate::provider_rules::dashscope_batch_protocol_for_model(&model) {
Some(crate::provider_rules::DashScopeBatchProtocol::AsyncTranscription) => {
BailianEndpointProtocol::AsyncTranscription
}
_ => BailianEndpointProtocol::Multimodal,
};
let protocol = match preparation.bailian_protocol.batch_protocol(&model) {
Some(crate::provider_rules::DashScopeBatchProtocol::AsyncTranscription) => {
BailianEndpointProtocol::AsyncTranscription
}
_ => BailianEndpointProtocol::Multimodal,
};
crate::provider_rules::derive_bailian_endpoint(&stored_endpoint, protocol)
.unwrap_or(stored_endpoint)
} else {
stored_endpoint
};
(
CloudTranscriptionSessionKind::DashScope(Arc::new(DashScopeMultimodalASR::new(
api_key,
endpoint,
model.clone(),
))),
CloudTranscriptionSessionKind::DashScope(Arc::new(
DashScopeMultimodalASR::new(api_key, endpoint, model.clone())
.with_protocol(preparation.bailian_protocol),
)),
Some(model),
)
}
Expand Down Expand Up @@ -2129,6 +2134,64 @@ fn build_omni_prompt(context: &DictationContext) -> String {

#[cfg(test)]
mod tests {
#[tokio::test]
async fn manual_bailian_protocol_is_snapshotted_for_recording() {
use super::*;
use crate::provider_rules::BailianProtocol;
let credentials = crate::InMemoryCredentialStore::default();
for (name, expected) in [
("dashscope-realtime", "bailian"),
("qwen-realtime", "bailian-qwen3-realtime"),
("multimodal", "bailian-fun-asr-flash"),
("qwen-multimodal", "bailian-fun-asr-flash"),
("async-transcription", "bailian-fun-asr-flash"),
] {
let raw = format!(r#"{{"bailianProtocol":"{name}"}}"#);
for (account, value) in [
(ASR_API_KEY_ACCOUNT, "fixture-key"),
(ASR_ADVANCED_CONFIG_ACCOUNT, raw.as_str()),
] {
credentials
.write(
CredentialKey::new(
CredentialNamespace::Asr,
Some(name.to_string()),
account,
)
.unwrap(),
crate::SecretValue::new(value),
)
.await
.unwrap();
}
let mut context = DictationContext::default();
context.asr.provider_id = name.to_string();
context.asr.provider_type = "bailian".into();
context.asr.model = Some("unknown-model".into());
let prepared = prepare_cloud_transcription(&credentials, Arc::new(context))
.await
.unwrap();
assert_eq!(prepared.effective_provider, expected);
assert_eq!(prepared.model, "unknown-model");
// A later settings change must not mutate an already prepared recording.
credentials
.write(
CredentialKey::new(
CredentialNamespace::Asr,
Some(name.to_string()),
ASR_ADVANCED_CONFIG_ACCOUNT,
)
.unwrap(),
crate::SecretValue::new("{}"),
)
.await
.unwrap();
assert_eq!(
prepared.bailian_protocol,
BailianProtocol::from_config("bailian", Some(&raw)).unwrap()
);
}
}
use super::*;
use crate::{InMemoryCredentialStore, ProviderInvocation, SecretValue};

Expand Down
2 changes: 1 addition & 1 deletion openless-all/app/crates/openless-core/src/cloud_sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ impl CloudSyncService {
.parse::<std::net::IpAddr>()
.is_ok_and(|ip| ip.is_loopback())
});
if (!matches!(url.scheme(), "https") && !(url.scheme() == "http" && loopback))
if !(matches!(url.scheme(), "https") || url.scheme() == "http" && loopback)
|| !url.username().is_empty()
|| url.password().is_some()
{
Expand Down
Loading
Loading