Avoid redundant transactional check before inserting dynamic tools (#10521)

Summary
- remove the extra transaction guard that checked for existing dynamic
tools per thread before inserting new ones
- insert each tool record with `ON CONFLICT(thread_id, position) DO
NOTHING` to ignore duplicates instead of pre-querying
- simplify execution to use the shared pool directly and avoid unneeded
commits

Testing
- Not run (not requested)
This commit is contained in:
jif-oai authored and GitHub committed 2026-02-03 15:34:28 +00:00
1 parent 944541e936
commit ed778f9017
1 file changed
+2 -12
+2 -12
View File
@@ -418,17 +418,7 @@ ON CONFLICT(id) DO UPDATE SET
if tools.is_empty() { if tools.is_empty() {
return Ok(()); return Ok(());
} }
let mut tx = self.pool.begin().await?;
let thread_id = thread_id.to_string(); let thread_id = thread_id.to_string();
let existing: Option<i64> =
sqlx::query_scalar("SELECT 1 FROM thread_dynamic_tools WHERE thread_id = ? LIMIT 1")
.bind(thread_id.as_str())
.fetch_optional(&mut *tx)
.await?;
if existing.is_some() {
tx.commit().await?;
return Ok(());
}
for (idx, tool) in tools.iter().enumerate() { for (idx, tool) in tools.iter().enumerate() {
let position = i64::try_from(idx).unwrap_or(i64::MAX); let position = i64::try_from(idx).unwrap_or(i64::MAX);
let input_schema = serde_json::to_string(&tool.input_schema)?; let input_schema = serde_json::to_string(&tool.input_schema)?;
@@ -441,6 +431,7 @@ INSERT INTO thread_dynamic_tools (
description, description,
input_schema input_schema
) VALUES (?, ?, ?, ?, ?) ) VALUES (?, ?, ?, ?, ?)
ON CONFLICT(thread_id, position) DO NOTHING
"#, "#,
) )
.bind(thread_id.as_str()) .bind(thread_id.as_str())
@@ -448,10 +439,9 @@ INSERT INTO thread_dynamic_tools (
.bind(tool.name.as_str()) .bind(tool.name.as_str())
.bind(tool.description.as_str()) .bind(tool.description.as_str())
.bind(input_schema) .bind(input_schema)
.execute(&mut *tx) .execute(self.pool.as_ref())
.await?; .await?;
} }
tx.commit().await?;
Ok(()) Ok(())
} }