| 1551 | } |
| 1552 | |
| 1553 | void ProxyRunner::finish_request(const td::Bits256 &worker_request_id, const td::Bits256 &client_request_id, |
| 1554 | std::shared_ptr<ProxyClientInfo> client, TcpClient::ConnectionId client_connection_id, |
| 1555 | std::shared_ptr<ProxyWorkerInfo> worker, |
| 1556 | std::shared_ptr<ProxyWorkerConnectionInfo> worker_connection, |
| 1557 | ton::tl_object_ptr<cocoon_api::tokensUsed> tokens_used, td::int64 to_unlock, |
| 1558 | bool is_success, double work_time, double worker_run_time) { |
| 1559 | auto to_deduct = tokens_used->total_tokens_used_; |
| 1560 | worker->adjust_balance(to_deduct); |
| 1561 | client->deduct(to_deduct); |
| 1562 | client->release_reserve(to_unlock); |
| 1563 | if (is_success) { |
| 1564 | worker->forwarded_query_success(work_time); |
| 1565 | worker_connection->forwarded_query_success(work_time, worker_run_time); |
| 1566 | client->stop_query(); |
| 1567 | } else { |
| 1568 | worker->forwarded_query_failed(work_time); |
| 1569 | worker_connection->forwarded_query_failed(work_time, worker_run_time); |
| 1570 | client->stop_query(); |
| 1571 | } |
| 1572 | CHECK(running_queries_.erase(worker_request_id)); |
| 1573 | |
| 1574 | stats_->total_adjusted_tokens_used += (double)tokens_used->total_tokens_used_; |
| 1575 | stats_->prompt_adjusted_tokens_used += (double)tokens_used->prompt_tokens_used_; |
| 1576 | stats_->cached_adjusted_tokens_used += (double)tokens_used->cached_tokens_used_; |
| 1577 | stats_->completion_adjusted_tokens_used += (double)tokens_used->completion_tokens_used_; |
| 1578 | stats_->reasoning_adjusted_tokens_used += (double)tokens_used->reasoning_tokens_used_; |
| 1579 | |
| 1580 | sign_worker_payment(*worker); |
| 1581 | //proxy.workerRequestPayment signed_payment:proxy.SignedPayment db_tokens:long max_tokens:long last_request_tokens:long last_request_client:string = |
| 1582 | //proxy.WorkerRequestPayment; |
| 1583 | auto req = cocoon::create_serialize_tl_object<cocoon_api::proxy_workerRequestPayment>( |
| 1584 | worker_request_id, worker->signed_payment(), worker->tokens_committed_to_db(), worker->tokens_max(), to_deduct, |
| 1585 | client->client_owner_address().rserialize(true)); |
| 1586 | send_message_to_connection(worker_connection->connection_id, std::move(req)); |
| 1587 | |
| 1588 | sign_client_payment(*client); |
| 1589 | auto req2 = cocoon::create_serialize_tl_object<cocoon_api::proxy_clientRequestPayment>( |
| 1590 | client_request_id, client->signed_payment(), client->tokens_committed_to_db(), client->tokens_max(), to_deduct); |
| 1591 | send_message_to_connection(client_connection_id, std::move(req2)); |
| 1592 | } |
| 1593 | |
| 1594 | /* |
| 1595 | * |
nothing calls this directly
no test coverage detected