mirror of
https://github.com/storytold/artcraft.git
synced 2026-11-04 13:28:06 -05:00
Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2bf5611391 |
@@ -1,421 +0,0 @@
|
||||
use chrono::{DateTime, Utc};
|
||||
use log::info;
|
||||
use serde::Deserialize;
|
||||
use wreq::Client;
|
||||
use wreq_util::Emulation;
|
||||
|
||||
use crate::creds::seedance2pro_session::Seedance2ProSession;
|
||||
use crate::error::seedance2pro_client_error::Seedance2ProClientError;
|
||||
use crate::error::seedance2pro_error::Seedance2ProError;
|
||||
use crate::error::seedance2pro_generic_api_error::Seedance2ProGenericApiError;
|
||||
use crate::requests::kinovi_host::{KinoviHost, resolve_host};
|
||||
use crate::utils::common_headers::FIREFOX_USER_AGENT;
|
||||
use crate::utils::number_coercion::de_u64_int_or_float;
|
||||
|
||||
/// Page size the kinovi.ai billing dashboard uses.
|
||||
pub const DEFAULT_BILLING_PAGE_LIMIT: u32 = 20;
|
||||
|
||||
// --- Args & response ---
|
||||
|
||||
pub struct GetBillingPaymentsHistoryArgs<'a> {
|
||||
pub session: &'a Seedance2ProSession,
|
||||
|
||||
/// Page size. The web dashboard uses [`DEFAULT_BILLING_PAGE_LIMIT`].
|
||||
pub limit: u32,
|
||||
|
||||
/// Zero-based row offset. Pagination is offset-based: advance by the
|
||||
/// number of payments actually returned until a page comes back empty.
|
||||
pub offset: u64,
|
||||
|
||||
/// Override the default host (kinovi.ai).
|
||||
pub host_override: Option<KinoviHost>,
|
||||
}
|
||||
|
||||
pub struct BillingPaymentsHistoryPage {
|
||||
/// Payments on this page, newest first.
|
||||
pub payments: Vec<BillingPayment>,
|
||||
|
||||
/// Cursor of the row after this page (`None` on the last page). The
|
||||
/// dashboard paginates by limit/offset; this is informational.
|
||||
pub maybe_next_cursor: Option<String>,
|
||||
|
||||
/// Total payment rows on the account, per the server.
|
||||
pub total: u64,
|
||||
}
|
||||
|
||||
// --- Public types ---
|
||||
|
||||
/// One payment on the Kinovi billing dashboard (a credit-package purchase).
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct BillingPayment {
|
||||
pub id: String,
|
||||
|
||||
/// Payment amount in US dollars (the API returns dollars: `2159`, `99.99`).
|
||||
pub amount_usd: f64,
|
||||
|
||||
pub status: BillingPaymentStatus,
|
||||
|
||||
pub payment_type: BillingPaymentType,
|
||||
|
||||
/// Kinovi credits granted by this payment (e.g. 525,000).
|
||||
pub credits_earned: u64,
|
||||
|
||||
/// e.g. "Credits Package"
|
||||
pub product_name: String,
|
||||
|
||||
pub transaction_id: String,
|
||||
pub payment_id: String,
|
||||
pub product_id: String,
|
||||
|
||||
/// ISO 8601 creation timestamp (e.g. `"2026-07-13T20:40:49.405Z"`).
|
||||
pub created_at: String,
|
||||
|
||||
/// Parsed `created_at`. `None` if the raw string could not be parsed.
|
||||
pub created_at_utc: Option<DateTime<Utc>>,
|
||||
|
||||
pub updated_at: String,
|
||||
}
|
||||
|
||||
/// Payment status. Only `PAID` has been observed; unrecognised values are
|
||||
/// preserved in `Unknown` so future statuses (refunds, chargebacks, pending)
|
||||
/// don't break deserialization.
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub enum BillingPaymentStatus {
|
||||
Paid,
|
||||
Unknown(String),
|
||||
}
|
||||
|
||||
impl BillingPaymentStatus {
|
||||
fn from_str(s: &str) -> Self {
|
||||
match s {
|
||||
"PAID" => Self::Paid,
|
||||
other => Self::Unknown(other.to_string()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Payment type. Only `ONE_TIME` has been observed.
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub enum BillingPaymentType {
|
||||
OneTime,
|
||||
Unknown(String),
|
||||
}
|
||||
|
||||
impl BillingPaymentType {
|
||||
fn from_str(s: &str) -> Self {
|
||||
match s {
|
||||
"ONE_TIME" => Self::OneTime,
|
||||
other => Self::Unknown(other.to_string()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// --- Implementation ---
|
||||
|
||||
pub async fn get_billing_payments_history(
|
||||
args: GetBillingPaymentsHistoryArgs<'_>,
|
||||
) -> Result<BillingPaymentsHistoryPage, Seedance2ProError> {
|
||||
let host = resolve_host(args.host_override.as_ref());
|
||||
let base_url = host.api_base_url();
|
||||
let url = format!("{}/api/trpc/billing.getPayments", base_url);
|
||||
|
||||
info!("Fetching billing payments (limit: {}, offset: {})...", args.limit, args.offset);
|
||||
|
||||
let input_json = build_input_json(args.limit, args.offset);
|
||||
|
||||
let client = Client::builder()
|
||||
.emulation(Emulation::Firefox143)
|
||||
.build()
|
||||
.map_err(|err| Seedance2ProClientError::WreqClientError(err))?;
|
||||
|
||||
let cookie = args.session.cookies.as_str();
|
||||
let referer = format!("{}/app/dashboard/billing", base_url);
|
||||
|
||||
let request = client.get(&url)
|
||||
.query(&[("batch", "1"), ("input", input_json.as_str())])
|
||||
.header("User-Agent", FIREFOX_USER_AGENT)
|
||||
.header("Accept", "*/*")
|
||||
.header("Accept-Language", "en-US,en;q=0.9")
|
||||
.header("Accept-Encoding", "gzip, deflate, br, zstd")
|
||||
.header("Referer", &referer)
|
||||
.header("content-type", "application/json")
|
||||
.header("x-trpc-source", "client")
|
||||
.header("Connection", "keep-alive")
|
||||
.header("Cookie", cookie)
|
||||
.header("Sec-Fetch-Dest", "empty")
|
||||
.header("Sec-Fetch-Mode", "cors")
|
||||
.header("Sec-Fetch-Site", "same-origin")
|
||||
.header("Priority", "u=4")
|
||||
.header("TE", "trailers")
|
||||
.build()
|
||||
.map_err(|err| Seedance2ProClientError::WreqClientError(err))?;
|
||||
|
||||
let response = client.execute(request)
|
||||
.await
|
||||
.map_err(|err| Seedance2ProGenericApiError::WreqError(err))?;
|
||||
|
||||
let status = response.status();
|
||||
let response_body = response.text()
|
||||
.await
|
||||
.map_err(|err| Seedance2ProGenericApiError::WreqError(err))?;
|
||||
|
||||
info!("Billing payments response status: {}", status);
|
||||
|
||||
if !status.is_success() {
|
||||
return Err(Seedance2ProGenericApiError::UncategorizedBadResponseWithStatusAndBody {
|
||||
status_code: status,
|
||||
body: response_body,
|
||||
}.into());
|
||||
}
|
||||
|
||||
parse_response_body(&response_body)
|
||||
}
|
||||
|
||||
fn parse_response_body(response_body: &str) -> Result<BillingPaymentsHistoryPage, Seedance2ProError> {
|
||||
let batch_response: Vec<BatchResponseItem> = serde_json::from_str(response_body)
|
||||
.map_err(|err| Seedance2ProGenericApiError::SerdeResponseParseErrorWithBody(err, response_body.to_string()))?;
|
||||
|
||||
let json = batch_response
|
||||
.into_iter()
|
||||
.next()
|
||||
.ok_or_else(|| Seedance2ProGenericApiError::UnexpectedResponseShape {
|
||||
explanation: "Empty batch response array".to_string(),
|
||||
raw_body: response_body.to_string(),
|
||||
})?
|
||||
.result
|
||||
.data
|
||||
.json;
|
||||
|
||||
let payments = json.payments
|
||||
.into_iter()
|
||||
.map(|p| {
|
||||
let created_at_utc = DateTime::parse_from_rfc3339(&p.created_at)
|
||||
.map(|dt| dt.with_timezone(&Utc))
|
||||
.ok();
|
||||
BillingPayment {
|
||||
id: p.id,
|
||||
amount_usd: p.amount,
|
||||
status: BillingPaymentStatus::from_str(&p.payment_status),
|
||||
payment_type: BillingPaymentType::from_str(&p.payment_type),
|
||||
credits_earned: p.credits_earned,
|
||||
product_name: p.product_name,
|
||||
transaction_id: p.transaction_id,
|
||||
payment_id: p.payment_id,
|
||||
product_id: p.product_id,
|
||||
created_at: p.created_at,
|
||||
created_at_utc,
|
||||
updated_at: p.updated_at,
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
|
||||
Ok(BillingPaymentsHistoryPage {
|
||||
payments,
|
||||
maybe_next_cursor: json.next_cursor,
|
||||
total: json.total,
|
||||
})
|
||||
}
|
||||
|
||||
/// Builds the tRPC `input` JSON for the billing.getPayments endpoint.
|
||||
fn build_input_json(limit: u32, offset: u64) -> String {
|
||||
format!(r#"{{"0":{{"json":{{"limit":{limit},"offset":{offset}}}}}}}"#)
|
||||
}
|
||||
|
||||
// --- Raw response types ---
|
||||
|
||||
#[derive(Deserialize, Debug)]
|
||||
struct BatchResponseItem {
|
||||
result: RawResult,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Debug)]
|
||||
struct RawResult {
|
||||
data: RawData,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Debug)]
|
||||
struct RawData {
|
||||
json: RawJson,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Debug)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
struct RawJson {
|
||||
payments: Vec<RawPayment>,
|
||||
#[serde(default)]
|
||||
next_cursor: Option<String>,
|
||||
#[serde(deserialize_with = "de_u64_int_or_float")]
|
||||
total: u64,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Debug)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
struct RawPayment {
|
||||
id: String,
|
||||
amount: f64,
|
||||
payment_status: String,
|
||||
#[serde(default)]
|
||||
transaction_id: String,
|
||||
#[serde(default)]
|
||||
payment_id: String,
|
||||
#[serde(default)]
|
||||
product_id: String,
|
||||
#[serde(rename = "type")]
|
||||
payment_type: String,
|
||||
#[serde(deserialize_with = "de_u64_int_or_float")]
|
||||
credits_earned: u64,
|
||||
#[serde(default)]
|
||||
product_name: String,
|
||||
created_at: String,
|
||||
#[serde(default)]
|
||||
updated_at: String,
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::creds::seedance2pro_session::Seedance2ProSession;
|
||||
use crate::test_utils::get_test_cookies::get_test_cookies;
|
||||
use crate::test_utils::setup_test_logging::setup_test_logging;
|
||||
use errors::AnyhowResult;
|
||||
use log::LevelFilter;
|
||||
|
||||
mod offline_parsing {
|
||||
use super::*;
|
||||
|
||||
/// Trimmed verbatim from a captured billing.getPayments response
|
||||
/// (external/requests/sites/kinovi.ai/2026-07-13-billing/).
|
||||
const CAPTURED_PAGE: &str = r#"[{"result":{"data":{"json":{"payments":[
|
||||
{"id":"31722","amount":2159,"paymentStatus":"PAID","transactionId":"","paymentId":"","createdAt":"2026-07-13T20:40:49.405Z","updatedAt":"2026-07-13T20:40:49.405Z","productId":"","type":"ONE_TIME","creditsEarned":525000,"productName":"Credits Package"},
|
||||
{"id":"11491","amount":99.99,"paymentStatus":"PAID","transactionId":"202603141400154949180","paymentId":"202603141400154949180","createdAt":"2026-03-14T14:00:15.782Z","updatedAt":"2026-03-14T14:01:02.630Z","productId":"","type":"ONE_TIME","creditsEarned":22000,"productName":"Credits Package"}
|
||||
],"nextCursor":"29564","total":997}}}}]"#;
|
||||
|
||||
#[test]
|
||||
fn parses_captured_page() {
|
||||
let page = parse_response_body(CAPTURED_PAGE).expect("parse");
|
||||
assert_eq!(page.payments.len(), 2);
|
||||
assert_eq!(page.maybe_next_cursor.as_deref(), Some("29564"));
|
||||
assert_eq!(page.total, 997);
|
||||
|
||||
let first = &page.payments[0];
|
||||
assert_eq!(first.id, "31722");
|
||||
assert_eq!(first.amount_usd, 2159.0);
|
||||
assert_eq!(first.status, BillingPaymentStatus::Paid);
|
||||
assert_eq!(first.payment_type, BillingPaymentType::OneTime);
|
||||
assert_eq!(first.credits_earned, 525_000);
|
||||
assert!(first.created_at_utc.is_some());
|
||||
|
||||
let second = &page.payments[1];
|
||||
assert_eq!(second.amount_usd, 99.99);
|
||||
assert_eq!(second.transaction_id, "202603141400154949180");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn last_page_has_no_next_cursor() {
|
||||
let body = r#"[{"result":{"data":{"json":{"payments":[],"nextCursor":null,"total":997}}}}]"#;
|
||||
let page = parse_response_body(body).expect("parse");
|
||||
assert!(page.payments.is_empty());
|
||||
assert_eq!(page.maybe_next_cursor, None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unknown_status_and_type_are_preserved() {
|
||||
let body = r#"[{"result":{"data":{"json":{"payments":[
|
||||
{"id":"1","amount":5,"paymentStatus":"REFUNDED","type":"SUBSCRIPTION","creditsEarned":0,"createdAt":"2026-07-13T20:40:49.405Z"}
|
||||
],"nextCursor":null,"total":1}}}}]"#;
|
||||
let page = parse_response_body(body).expect("parse");
|
||||
assert_eq!(page.payments[0].status, BillingPaymentStatus::Unknown("REFUNDED".to_string()));
|
||||
assert_eq!(page.payments[0].payment_type, BillingPaymentType::Unknown("SUBSCRIPTION".to_string()));
|
||||
}
|
||||
}
|
||||
|
||||
mod live {
|
||||
use super::*;
|
||||
|
||||
#[tokio::test]
|
||||
#[ignore] // manual test — requires real cookies
|
||||
async fn live_fetch_first_payments_page() -> AnyhowResult<()> {
|
||||
setup_test_logging(LevelFilter::Info);
|
||||
let session = test_session()?;
|
||||
|
||||
let page = get_billing_payments_history(GetBillingPaymentsHistoryArgs {
|
||||
session: &session,
|
||||
limit: DEFAULT_BILLING_PAGE_LIMIT,
|
||||
offset: 0,
|
||||
host_override: None,
|
||||
}).await?;
|
||||
|
||||
println!("payments: {}, total: {}, next_cursor: {:?}",
|
||||
page.payments.len(), page.total, page.maybe_next_cursor);
|
||||
for payment in &page.payments {
|
||||
println!(" {} | {} | ${:.2} | {} credits | {:?} | {:?}",
|
||||
payment.id, payment.created_at, payment.amount_usd,
|
||||
payment.credits_earned, payment.status, payment.payment_type);
|
||||
}
|
||||
assert!(!page.payments.is_empty());
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Pages through the ENTIRE payments history with throttling and prints
|
||||
/// lifetime totals. ~1,000 rows / 50 pages as of 2026-07.
|
||||
/// cargo test -p seedance2pro_client live_paginate_entire_payments_history -- --ignored --nocapture
|
||||
#[tokio::test]
|
||||
#[ignore] // manual test — requires real cookies; fetches every page
|
||||
async fn live_paginate_entire_payments_history() -> AnyhowResult<()> {
|
||||
setup_test_logging(LevelFilter::Info);
|
||||
let session = test_session()?;
|
||||
|
||||
const PAGE_DELAY_MS: u64 = 250;
|
||||
const MAX_PAGES: usize = 2_000; // runaway guard
|
||||
|
||||
let mut offset: u64 = 0;
|
||||
let mut pages = 0usize;
|
||||
let mut total_rows = 0u64;
|
||||
let mut total_amount_usd = 0f64;
|
||||
let mut total_credits = 0u64;
|
||||
let mut last_reported_total = 0u64;
|
||||
|
||||
for _ in 0..MAX_PAGES {
|
||||
let page = get_billing_payments_history(GetBillingPaymentsHistoryArgs {
|
||||
session: &session,
|
||||
limit: DEFAULT_BILLING_PAGE_LIMIT,
|
||||
offset,
|
||||
host_override: None,
|
||||
}).await?;
|
||||
|
||||
pages += 1;
|
||||
last_reported_total = page.total;
|
||||
if page.payments.is_empty() {
|
||||
break;
|
||||
}
|
||||
|
||||
for payment in &page.payments {
|
||||
total_rows += 1;
|
||||
total_amount_usd += payment.amount_usd;
|
||||
total_credits += payment.credits_earned;
|
||||
}
|
||||
|
||||
println!("page {} (offset {}): {} payments, running total ${:.2}",
|
||||
pages, offset, page.payments.len(), total_amount_usd);
|
||||
|
||||
// Advance by what was actually returned so a server-side limit cap
|
||||
// can never skip rows.
|
||||
offset += page.payments.len() as u64;
|
||||
tokio::time::sleep(std::time::Duration::from_millis(PAGE_DELAY_MS)).await;
|
||||
}
|
||||
|
||||
println!("── payments history totals ──");
|
||||
println!("pages: {}", pages);
|
||||
println!("payments: {} (server reports total {})", total_rows, last_reported_total);
|
||||
println!("lifetime spend: ${:.2}", total_amount_usd);
|
||||
println!("lifetime credits: {}", total_credits);
|
||||
assert!(total_rows > 0);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn test_session() -> AnyhowResult<Seedance2ProSession> {
|
||||
let cookies = get_test_cookies()?;
|
||||
Ok(Seedance2ProSession::from_cookies_string(cookies))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,415 +0,0 @@
|
||||
use chrono::{DateTime, Utc};
|
||||
use log::info;
|
||||
use serde::Deserialize;
|
||||
use wreq::Client;
|
||||
use wreq_util::Emulation;
|
||||
|
||||
use crate::creds::seedance2pro_session::Seedance2ProSession;
|
||||
use crate::error::seedance2pro_client_error::Seedance2ProClientError;
|
||||
use crate::error::seedance2pro_error::Seedance2ProError;
|
||||
use crate::error::seedance2pro_generic_api_error::Seedance2ProGenericApiError;
|
||||
use crate::requests::kinovi_host::{KinoviHost, resolve_host};
|
||||
use crate::utils::common_headers::FIREFOX_USER_AGENT;
|
||||
use crate::utils::number_coercion::{de_i64_int_or_float, de_u64_int_or_float};
|
||||
|
||||
/// Page size the kinovi.ai billing dashboard uses.
|
||||
pub const DEFAULT_CREDITS_PAGE_LIMIT: u32 = 20;
|
||||
|
||||
// --- Args & response ---
|
||||
|
||||
pub struct GetCreditsHistoryArgs<'a> {
|
||||
pub session: &'a Seedance2ProSession,
|
||||
|
||||
/// Page size. The web dashboard uses [`DEFAULT_CREDITS_PAGE_LIMIT`].
|
||||
pub limit: u32,
|
||||
|
||||
/// Zero-based row offset. Pagination is offset-based: advance by the
|
||||
/// number of entries actually returned until a page comes back empty.
|
||||
/// NB: the ledger grows while you page (offsets shift under live traffic),
|
||||
/// so dedupe by entry `id` when sweeping the full history.
|
||||
pub offset: u64,
|
||||
|
||||
/// Override the default host (kinovi.ai).
|
||||
pub host_override: Option<KinoviHost>,
|
||||
}
|
||||
|
||||
pub struct CreditsHistoryPage {
|
||||
/// Ledger entries on this page, newest first.
|
||||
pub entries: Vec<CreditHistoryEntry>,
|
||||
|
||||
/// Total ledger rows on the account, per the server.
|
||||
pub total: u64,
|
||||
}
|
||||
|
||||
// --- Public types ---
|
||||
|
||||
/// One row of the Kinovi credits ledger: a signed credit delta attributed to
|
||||
/// a generation order (consumption) or a grant (purchase, registration bonus).
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct CreditHistoryEntry {
|
||||
pub id: u64,
|
||||
|
||||
pub entry_type: CreditHistoryEntryType,
|
||||
|
||||
/// Signed credit delta: negative for consumption, positive for grants.
|
||||
pub credit_delta: i64,
|
||||
|
||||
/// The generation order this entry bills, when applicable (`ord_...`).
|
||||
/// `None` for grants (purchases, registration bonuses).
|
||||
pub maybe_order_id: Option<String>,
|
||||
|
||||
/// e.g. "Seedance 2.0 Video", "Credit Package - xl (25000 credits)"
|
||||
pub product_name: String,
|
||||
|
||||
/// ISO 8601 creation timestamp (e.g. `"2026-07-14T00:31:12.820Z"`).
|
||||
pub created_at: String,
|
||||
|
||||
/// Parsed `created_at`. `None` if the raw string could not be parsed.
|
||||
pub created_at_utc: Option<DateTime<Utc>>,
|
||||
}
|
||||
|
||||
/// Ledger entry type. Observed values are mapped; anything new (refunds,
|
||||
/// failure credits, expirations, ...) lands in `Unknown` so deserialization
|
||||
/// never breaks.
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
||||
pub enum CreditHistoryEntryType {
|
||||
/// Credits consumed by a successful generation (negative delta).
|
||||
ConsumptionSuccess,
|
||||
/// Credits granted by a package purchase (positive delta).
|
||||
PurchasedCredits,
|
||||
/// Sign-up bonus credits (positive delta).
|
||||
NewUserRegistration,
|
||||
Unknown(String),
|
||||
}
|
||||
|
||||
impl CreditHistoryEntryType {
|
||||
pub fn as_str(&self) -> &str {
|
||||
match self {
|
||||
Self::ConsumptionSuccess => "CONSUMPTION_SUCCESS",
|
||||
Self::PurchasedCredits => "PURCHASED_CREDITS",
|
||||
Self::NewUserRegistration => "NEW_USER_REGISTRATION",
|
||||
Self::Unknown(other) => other.as_str(),
|
||||
}
|
||||
}
|
||||
|
||||
fn from_str(s: &str) -> Self {
|
||||
match s {
|
||||
"CONSUMPTION_SUCCESS" => Self::ConsumptionSuccess,
|
||||
"PURCHASED_CREDITS" => Self::PurchasedCredits,
|
||||
"NEW_USER_REGISTRATION" => Self::NewUserRegistration,
|
||||
other => Self::Unknown(other.to_string()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// --- Implementation ---
|
||||
|
||||
pub async fn get_credits_history(
|
||||
args: GetCreditsHistoryArgs<'_>,
|
||||
) -> Result<CreditsHistoryPage, Seedance2ProError> {
|
||||
let host = resolve_host(args.host_override.as_ref());
|
||||
let base_url = host.api_base_url();
|
||||
let url = format!("{}/api/trpc/credits.getCreditHistory", base_url);
|
||||
|
||||
info!("Fetching credits history (limit: {}, offset: {})...", args.limit, args.offset);
|
||||
|
||||
let input_json = build_input_json(args.limit, args.offset);
|
||||
|
||||
let client = Client::builder()
|
||||
.emulation(Emulation::Firefox143)
|
||||
.build()
|
||||
.map_err(|err| Seedance2ProClientError::WreqClientError(err))?;
|
||||
|
||||
let cookie = args.session.cookies.as_str();
|
||||
let referer = format!("{}/app/dashboard/billing", base_url);
|
||||
|
||||
let request = client.get(&url)
|
||||
.query(&[("batch", "1"), ("input", input_json.as_str())])
|
||||
.header("User-Agent", FIREFOX_USER_AGENT)
|
||||
.header("Accept", "*/*")
|
||||
.header("Accept-Language", "en-US,en;q=0.9")
|
||||
.header("Accept-Encoding", "gzip, deflate, br, zstd")
|
||||
.header("Referer", &referer)
|
||||
.header("content-type", "application/json")
|
||||
.header("x-trpc-source", "client")
|
||||
.header("Connection", "keep-alive")
|
||||
.header("Cookie", cookie)
|
||||
.header("Sec-Fetch-Dest", "empty")
|
||||
.header("Sec-Fetch-Mode", "cors")
|
||||
.header("Sec-Fetch-Site", "same-origin")
|
||||
.header("Priority", "u=4")
|
||||
.header("TE", "trailers")
|
||||
.build()
|
||||
.map_err(|err| Seedance2ProClientError::WreqClientError(err))?;
|
||||
|
||||
let response = client.execute(request)
|
||||
.await
|
||||
.map_err(|err| Seedance2ProGenericApiError::WreqError(err))?;
|
||||
|
||||
let status = response.status();
|
||||
let response_body = response.text()
|
||||
.await
|
||||
.map_err(|err| Seedance2ProGenericApiError::WreqError(err))?;
|
||||
|
||||
info!("Credits history response status: {}", status);
|
||||
|
||||
if !status.is_success() {
|
||||
return Err(Seedance2ProGenericApiError::UncategorizedBadResponseWithStatusAndBody {
|
||||
status_code: status,
|
||||
body: response_body,
|
||||
}.into());
|
||||
}
|
||||
|
||||
parse_response_body(&response_body)
|
||||
}
|
||||
|
||||
fn parse_response_body(response_body: &str) -> Result<CreditsHistoryPage, Seedance2ProError> {
|
||||
let batch_response: Vec<BatchResponseItem> = serde_json::from_str(response_body)
|
||||
.map_err(|err| Seedance2ProGenericApiError::SerdeResponseParseErrorWithBody(err, response_body.to_string()))?;
|
||||
|
||||
let json = batch_response
|
||||
.into_iter()
|
||||
.next()
|
||||
.ok_or_else(|| Seedance2ProGenericApiError::UnexpectedResponseShape {
|
||||
explanation: "Empty batch response array".to_string(),
|
||||
raw_body: response_body.to_string(),
|
||||
})?
|
||||
.result
|
||||
.data
|
||||
.json;
|
||||
|
||||
let entries = json.credits
|
||||
.into_iter()
|
||||
.map(|entry| {
|
||||
let created_at_utc = DateTime::parse_from_rfc3339(&entry.created_at)
|
||||
.map(|dt| dt.with_timezone(&Utc))
|
||||
.ok();
|
||||
CreditHistoryEntry {
|
||||
id: entry.id,
|
||||
entry_type: CreditHistoryEntryType::from_str(&entry.entry_type),
|
||||
credit_delta: entry.credit,
|
||||
maybe_order_id: entry.order_id,
|
||||
product_name: entry.product_name,
|
||||
created_at: entry.created_at,
|
||||
created_at_utc,
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
|
||||
Ok(CreditsHistoryPage {
|
||||
entries,
|
||||
total: json.total,
|
||||
})
|
||||
}
|
||||
|
||||
/// Builds the tRPC `input` JSON for the credits.getCreditHistory endpoint.
|
||||
fn build_input_json(limit: u32, offset: u64) -> String {
|
||||
format!(r#"{{"0":{{"json":{{"limit":{limit},"offset":{offset}}}}}}}"#)
|
||||
}
|
||||
|
||||
// --- Raw response types ---
|
||||
|
||||
#[derive(Deserialize, Debug)]
|
||||
struct BatchResponseItem {
|
||||
result: RawResult,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Debug)]
|
||||
struct RawResult {
|
||||
data: RawData,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Debug)]
|
||||
struct RawData {
|
||||
json: RawJson,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Debug)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
struct RawJson {
|
||||
credits: Vec<RawCreditEntry>,
|
||||
#[serde(deserialize_with = "de_u64_int_or_float")]
|
||||
total: u64,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Debug)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
struct RawCreditEntry {
|
||||
#[serde(deserialize_with = "de_u64_int_or_float")]
|
||||
id: u64,
|
||||
#[serde(rename = "type")]
|
||||
entry_type: String,
|
||||
#[serde(deserialize_with = "de_i64_int_or_float")]
|
||||
credit: i64,
|
||||
#[serde(default)]
|
||||
order_id: Option<String>,
|
||||
created_at: String,
|
||||
#[serde(default)]
|
||||
product_name: String,
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::creds::seedance2pro_session::Seedance2ProSession;
|
||||
use crate::test_utils::get_test_cookies::get_test_cookies;
|
||||
use crate::test_utils::setup_test_logging::setup_test_logging;
|
||||
use errors::AnyhowResult;
|
||||
use log::LevelFilter;
|
||||
|
||||
mod offline_parsing {
|
||||
use super::*;
|
||||
|
||||
/// Trimmed verbatim from a captured credits.getCreditHistory response
|
||||
/// (external/requests/sites/kinovi.ai/2026-07-13-billing/).
|
||||
const CAPTURED_PAGE: &str = r#"[{"result":{"data":{"json":{"credits":[
|
||||
{"id":2750659,"type":"CONSUMPTION_SUCCESS","credit":-454,"orderId":"ord_mff2cjk2feczmzqweizcdal2","createdAt":"2026-07-14T00:31:12.820Z","productName":"Seedance 2.0 Video"},
|
||||
{"id":591799,"type":"PURCHASED_CREDITS","credit":25000,"orderId":null,"createdAt":"2026-02-19T08:16:58.524Z","productName":"Credit Package - xl (25000 credits)"},
|
||||
{"id":563856,"type":"NEW_USER_REGISTRATION","credit":200,"orderId":null,"createdAt":"2026-02-18T01:02:52.747Z","productName":"unknown"}
|
||||
],"total":304617}}}}]"#;
|
||||
|
||||
#[test]
|
||||
fn parses_captured_page() {
|
||||
let page = parse_response_body(CAPTURED_PAGE).expect("parse");
|
||||
assert_eq!(page.entries.len(), 3);
|
||||
assert_eq!(page.total, 304_617);
|
||||
|
||||
let consumption = &page.entries[0];
|
||||
assert_eq!(consumption.id, 2_750_659);
|
||||
assert_eq!(consumption.entry_type, CreditHistoryEntryType::ConsumptionSuccess);
|
||||
assert_eq!(consumption.credit_delta, -454);
|
||||
assert_eq!(consumption.maybe_order_id.as_deref(), Some("ord_mff2cjk2feczmzqweizcdal2"));
|
||||
assert!(consumption.created_at_utc.is_some());
|
||||
|
||||
let purchase = &page.entries[1];
|
||||
assert_eq!(purchase.entry_type, CreditHistoryEntryType::PurchasedCredits);
|
||||
assert_eq!(purchase.credit_delta, 25_000);
|
||||
assert_eq!(purchase.maybe_order_id, None);
|
||||
|
||||
let registration = &page.entries[2];
|
||||
assert_eq!(registration.entry_type, CreditHistoryEntryType::NewUserRegistration);
|
||||
assert_eq!(registration.credit_delta, 200);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unknown_entry_type_is_preserved() {
|
||||
let body = r#"[{"result":{"data":{"json":{"credits":[
|
||||
{"id":1,"type":"CONSUMPTION_REFUND","credit":454,"orderId":"ord_x","createdAt":"2026-07-14T00:31:12.820Z","productName":"Seedance 2.0 Video"}
|
||||
],"total":1}}}}]"#;
|
||||
let page = parse_response_body(body).expect("parse");
|
||||
assert_eq!(
|
||||
page.entries[0].entry_type,
|
||||
CreditHistoryEntryType::Unknown("CONSUMPTION_REFUND".to_string()),
|
||||
);
|
||||
assert_eq!(page.entries[0].credit_delta, 454);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn entry_type_round_trips_as_str() {
|
||||
for raw in ["CONSUMPTION_SUCCESS", "PURCHASED_CREDITS", "NEW_USER_REGISTRATION", "SOMETHING_NEW"] {
|
||||
assert_eq!(CreditHistoryEntryType::from_str(raw).as_str(), raw);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
mod live {
|
||||
use super::*;
|
||||
use std::collections::HashMap;
|
||||
use std::collections::HashSet;
|
||||
|
||||
#[tokio::test]
|
||||
#[ignore] // manual test — requires real cookies
|
||||
async fn live_fetch_first_credits_page() -> AnyhowResult<()> {
|
||||
setup_test_logging(LevelFilter::Info);
|
||||
let session = test_session()?;
|
||||
|
||||
let page = get_credits_history(GetCreditsHistoryArgs {
|
||||
session: &session,
|
||||
limit: DEFAULT_CREDITS_PAGE_LIMIT,
|
||||
offset: 0,
|
||||
host_override: None,
|
||||
}).await?;
|
||||
|
||||
println!("entries: {}, total: {}", page.entries.len(), page.total);
|
||||
for entry in &page.entries {
|
||||
println!(" {} | {} | {:?} | {} | {:?}",
|
||||
entry.id, entry.created_at, entry.entry_type, entry.credit_delta,
|
||||
entry.maybe_order_id);
|
||||
}
|
||||
assert!(!page.entries.is_empty());
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Pages through the ENTIRE credits ledger with throttling and prints
|
||||
/// per-type tallies (this is where any refund-like entry types will
|
||||
/// surface). ~300k rows as of 2026-07 — expect a long run; the ledger
|
||||
/// also grows while paging, so entries are deduped by id.
|
||||
/// cargo test -p seedance2pro_client live_paginate_entire_credits_history -- --ignored --nocapture
|
||||
#[tokio::test]
|
||||
#[ignore] // manual test — requires real cookies; fetches every page
|
||||
async fn live_paginate_entire_credits_history() -> AnyhowResult<()> {
|
||||
setup_test_logging(LevelFilter::Info);
|
||||
let session = test_session()?;
|
||||
|
||||
const PAGE_LIMIT: u32 = 100;
|
||||
const PAGE_DELAY_MS: u64 = 250;
|
||||
const MAX_PAGES: usize = 50_000; // runaway guard
|
||||
|
||||
let mut offset: u64 = 0;
|
||||
let mut pages = 0usize;
|
||||
let mut seen_ids: HashSet<u64> = HashSet::new();
|
||||
let mut type_counts: HashMap<CreditHistoryEntryType, (u64, i64)> = HashMap::new();
|
||||
let mut last_reported_total = 0u64;
|
||||
|
||||
for _ in 0..MAX_PAGES {
|
||||
let page = get_credits_history(GetCreditsHistoryArgs {
|
||||
session: &session,
|
||||
limit: PAGE_LIMIT,
|
||||
offset,
|
||||
host_override: None,
|
||||
}).await?;
|
||||
|
||||
pages += 1;
|
||||
last_reported_total = page.total;
|
||||
if page.entries.is_empty() {
|
||||
break;
|
||||
}
|
||||
|
||||
for entry in &page.entries {
|
||||
if !seen_ids.insert(entry.id) {
|
||||
continue; // offset shifted under live traffic; already counted
|
||||
}
|
||||
let tally = type_counts.entry(entry.entry_type.clone()).or_insert((0, 0));
|
||||
tally.0 += 1;
|
||||
tally.1 += entry.credit_delta;
|
||||
}
|
||||
|
||||
if pages % 25 == 0 {
|
||||
println!("page {} (offset {}): {} unique entries so far (server total {})",
|
||||
pages, offset, seen_ids.len(), page.total);
|
||||
}
|
||||
|
||||
// Advance by what was actually returned so a server-side limit cap
|
||||
// can never skip rows.
|
||||
offset += page.entries.len() as u64;
|
||||
tokio::time::sleep(std::time::Duration::from_millis(PAGE_DELAY_MS)).await;
|
||||
}
|
||||
|
||||
println!("── credits ledger totals ──");
|
||||
println!("pages: {}", pages);
|
||||
println!("unique entries: {} (server reports total {})", seen_ids.len(), last_reported_total);
|
||||
let mut types: Vec<_> = type_counts.iter().collect();
|
||||
types.sort_by_key(|(_, (count, _))| std::cmp::Reverse(*count));
|
||||
for (entry_type, (count, delta_sum)) in types {
|
||||
println!(" {:<28} n={:<8} credit_sum={}", entry_type.as_str(), count, delta_sum);
|
||||
}
|
||||
assert!(!seen_ids.is_empty());
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn test_session() -> AnyhowResult<Seedance2ProSession> {
|
||||
let cookies = get_test_cookies()?;
|
||||
Ok(Seedance2ProSession::from_cookies_string(cookies))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,2 +0,0 @@
|
||||
pub mod get_billing_payments_history;
|
||||
pub mod get_credits_history;
|
||||
@@ -2,7 +2,6 @@
|
||||
pub (crate) mod test_utils;
|
||||
|
||||
// User API
|
||||
pub mod billing;
|
||||
pub mod cost;
|
||||
pub mod creds;
|
||||
pub mod error;
|
||||
|
||||
@@ -19,9 +19,7 @@ pub fn setup_test_logging(level: LevelFilter) {
|
||||
|
||||
println!("Log level: {:?}", env::var(RUST_LOG));
|
||||
|
||||
// try_init: several #[ignore] live tests may run in one process, and only
|
||||
// the first can install the logger.
|
||||
let _ = Builder::new()
|
||||
Builder::new()
|
||||
.is_test(true)
|
||||
.format(|buf, record| {
|
||||
writeln!(buf,
|
||||
@@ -33,7 +31,7 @@ pub fn setup_test_logging(level: LevelFilter) {
|
||||
})
|
||||
.filter(None, level)
|
||||
.filter_level(level)
|
||||
.try_init();
|
||||
.init();
|
||||
|
||||
trace!("Test trace log");
|
||||
debug!("Test debug log");
|
||||
|
||||
@@ -28,20 +28,6 @@ fn coerce_to_u32(number: &Number) -> u32 {
|
||||
coerce_to_u64(number).min(u32::MAX as u64) as u32
|
||||
}
|
||||
|
||||
/// Coerce a parsed JSON number to `i64`. Exact integers pass through; floats
|
||||
/// are rounded; non-finite values become `0`.
|
||||
fn coerce_to_i64(number: &Number) -> i64 {
|
||||
if let Some(signed) = number.as_i64() {
|
||||
return signed;
|
||||
}
|
||||
if let Some(float) = number.as_f64() {
|
||||
if float.is_finite() {
|
||||
return float.round() as i64;
|
||||
}
|
||||
}
|
||||
0
|
||||
}
|
||||
|
||||
/// `#[serde(deserialize_with = "...")]` for a required `u64` field that may
|
||||
/// arrive as an integer or a float.
|
||||
pub fn de_u64_int_or_float<'de, D>(deserializer: D) -> Result<u64, D::Error>
|
||||
@@ -52,16 +38,6 @@ where
|
||||
Ok(coerce_to_u64(&number))
|
||||
}
|
||||
|
||||
/// `#[serde(deserialize_with = "...")]` for a required `i64` field that may
|
||||
/// arrive as an integer or a float (e.g. signed credit deltas).
|
||||
pub fn de_i64_int_or_float<'de, D>(deserializer: D) -> Result<i64, D::Error>
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
let number = Number::deserialize(deserializer)?;
|
||||
Ok(coerce_to_i64(&number))
|
||||
}
|
||||
|
||||
/// `#[serde(deserialize_with = "...")]` for an optional `u64` field (handles
|
||||
/// `null` / missing as `None`).
|
||||
pub fn de_opt_u64_int_or_float<'de, D>(deserializer: D) -> Result<Option<u64>, D::Error>
|
||||
@@ -114,12 +90,6 @@ mod tests {
|
||||
value: Option<u32>,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct I64Holder {
|
||||
#[serde(deserialize_with = "de_i64_int_or_float")]
|
||||
value: i64,
|
||||
}
|
||||
|
||||
fn parse_u32(json: &str) -> u32 {
|
||||
serde_json::from_str::<U32Holder>(json).unwrap().value
|
||||
}
|
||||
@@ -128,10 +98,6 @@ mod tests {
|
||||
serde_json::from_str::<U64Holder>(json).unwrap().value
|
||||
}
|
||||
|
||||
fn parse_i64(json: &str) -> i64 {
|
||||
serde_json::from_str::<I64Holder>(json).unwrap().value
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn u32_accepts_integer() {
|
||||
assert_eq!(parse_u32(r#"{"value": 568}"#), 568);
|
||||
@@ -160,13 +126,6 @@ mod tests {
|
||||
assert_eq!(parse_u64(r#"{"value": -5.5}"#), 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn i64_accepts_negative_integers_and_floats() {
|
||||
assert_eq!(parse_i64(r#"{"value": -454}"#), -454);
|
||||
assert_eq!(parse_i64(r#"{"value": -454.6}"#), -455);
|
||||
assert_eq!(parse_i64(r#"{"value": 25000}"#), 25000);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn optional_handles_null_missing_and_values() {
|
||||
assert_eq!(serde_json::from_str::<OptU32Holder>(r#"{"value": null}"#).unwrap().value, None);
|
||||
|
||||
@@ -7,10 +7,6 @@ use super::subcommands;
|
||||
/// All canonical subcommand names for this module.
|
||||
/// Used by the underscore-insensitive arg normalizer.
|
||||
pub const SUBCOMMAND_NAMES: &[&str] = &[
|
||||
"account_info",
|
||||
"audit_credits",
|
||||
"audit_orders",
|
||||
"audit_payments",
|
||||
"failed_job_histogram",
|
||||
"find_job",
|
||||
"generate_video",
|
||||
@@ -19,21 +15,6 @@ pub const SUBCOMMAND_NAMES: &[&str] = &[
|
||||
#[derive(Subcommand)]
|
||||
#[command(rename_all = "snake_case")]
|
||||
pub enum Seedance2proCommand {
|
||||
/// Print the account's current credit balances as one CSV line
|
||||
AccountInfo(subcommands::account_info::AccountInfoArgs),
|
||||
|
||||
/// Dump the account's credits ledger (credits.getCreditHistory) to CSV
|
||||
/// back to a start date, for refund-entry auditing
|
||||
AuditCredits(subcommands::audit_credits::AuditCreditsArgs),
|
||||
|
||||
/// Dump Kinovi's per-order billing records (totalCredits) to CSV for a
|
||||
/// date window, to audit whether failed orders are charged
|
||||
AuditOrders(subcommands::audit_orders::AuditOrdersArgs),
|
||||
|
||||
/// Dump the account's billing payments history (credit-package
|
||||
/// purchases) to CSV, for reconciling refills against invoices
|
||||
AuditPayments(subcommands::audit_payments::AuditPaymentsArgs),
|
||||
|
||||
/// Find a job by its order ID across all pages
|
||||
FindJob(subcommands::find_job::FindJobArgs),
|
||||
|
||||
@@ -51,10 +32,6 @@ pub async fn run(command: Seedance2proCommand) -> anyhow::Result<()> {
|
||||
let state = Seedance2ProState { cookies };
|
||||
|
||||
match command {
|
||||
Seedance2proCommand::AccountInfo(args) => subcommands::account_info::run(&state, args).await,
|
||||
Seedance2proCommand::AuditCredits(args) => subcommands::audit_credits::run(&state, args).await,
|
||||
Seedance2proCommand::AuditOrders(args) => subcommands::audit_orders::run(&state, args).await,
|
||||
Seedance2proCommand::AuditPayments(args) => subcommands::audit_payments::run(&state, args).await,
|
||||
Seedance2proCommand::FindJob(args) => subcommands::find_job::run(&state, args).await,
|
||||
Seedance2proCommand::FailedJobHistogram => subcommands::failed_job_histogram::run(&state).await,
|
||||
Seedance2proCommand::GenerateVideo(args) => subcommands::generate_video::run(&state, args).await,
|
||||
|
||||
-46
@@ -1,46 +0,0 @@
|
||||
use anyhow::anyhow;
|
||||
use clap::Args;
|
||||
use seedance2pro_client::creds::seedance2pro_session::Seedance2ProSession;
|
||||
use seedance2pro_client::requests::get_user_auth_details::get_user_auth_details::{
|
||||
get_user_auth_details, GetUserAuthDetailsArgs,
|
||||
};
|
||||
|
||||
use super::super::state::Seedance2ProState;
|
||||
|
||||
/// Print the account's current credit balances (auth.user endpoint) as a
|
||||
/// single CSV line: account,email,credits,available_credits.
|
||||
#[derive(Args)]
|
||||
pub struct AccountInfoArgs {
|
||||
/// Read session cookies from this env var instead of SEEDANCE2PRO_COOKIES.
|
||||
#[arg(long)]
|
||||
pub cookies_env: Option<String>,
|
||||
|
||||
/// Account label for the output line. Defaults to the cookies env var name.
|
||||
#[arg(long)]
|
||||
pub account: Option<String>,
|
||||
}
|
||||
|
||||
pub async fn run(state: &Seedance2ProState, args: AccountInfoArgs) -> anyhow::Result<()> {
|
||||
let (cookies, account_label) = match &args.cookies_env {
|
||||
Some(var) => (
|
||||
easyenv::get_env_string_required(var)
|
||||
.map_err(|err| anyhow!("Missing {} env var: {:?}", var, err))?,
|
||||
args.account.clone().unwrap_or_else(|| var.clone()),
|
||||
),
|
||||
None => (
|
||||
state.cookies.clone(),
|
||||
args.account.clone().unwrap_or_else(|| "SEEDANCE2PRO_COOKIES".to_string()),
|
||||
),
|
||||
};
|
||||
let session = Seedance2ProSession::from_cookies_string(cookies);
|
||||
|
||||
let details = get_user_auth_details(GetUserAuthDetailsArgs {
|
||||
session: &session,
|
||||
host_override: None,
|
||||
}).await
|
||||
.map_err(|err| anyhow!("Error fetching auth details: {:?}", err))?;
|
||||
|
||||
println!("{},{},{},{}",
|
||||
account_label, details.email, details.credits, details.available_credits);
|
||||
Ok(())
|
||||
}
|
||||
-167
@@ -1,167 +0,0 @@
|
||||
use std::collections::HashMap;
|
||||
use std::fs::OpenOptions;
|
||||
use std::io::Write;
|
||||
use std::time::Duration;
|
||||
|
||||
use anyhow::anyhow;
|
||||
use clap::Args;
|
||||
use log::info;
|
||||
use seedance2pro_client::billing::get_credits_history::{
|
||||
get_credits_history, CreditHistoryEntry, GetCreditsHistoryArgs,
|
||||
};
|
||||
use seedance2pro_client::creds::seedance2pro_session::Seedance2ProSession;
|
||||
|
||||
use super::super::state::Seedance2ProState;
|
||||
|
||||
/// Dump the account's credits ledger (credits.getCreditHistory) to CSV,
|
||||
/// newest-first, back to a start date. The ledger is offset-paginated and
|
||||
/// grows while paging (new rows push older rows to higher offsets), which
|
||||
/// causes re-reads rather than gaps - consumers should dedupe by entry id.
|
||||
#[derive(Args)]
|
||||
pub struct AuditCreditsArgs {
|
||||
/// Output CSV path. Appends if the file exists.
|
||||
#[arg(long)]
|
||||
pub out: String,
|
||||
|
||||
/// Stop paging once a whole page is older than this UTC date (YYYY-MM-DD).
|
||||
#[arg(long)]
|
||||
pub start_date: Option<String>,
|
||||
|
||||
/// Page size to request (the server may cap it; paging advances by rows
|
||||
/// actually returned, so a cap cannot skip rows).
|
||||
#[arg(long, default_value_t = 100)]
|
||||
pub limit: u32,
|
||||
|
||||
/// Maximum pages to fetch this run.
|
||||
#[arg(long, default_value_t = 10_000)]
|
||||
pub max_pages: usize,
|
||||
|
||||
/// Resume from a row offset (e.g. after an interrupted run).
|
||||
#[arg(long, default_value_t = 0)]
|
||||
pub offset: u64,
|
||||
|
||||
/// Delay between page fetches, in milliseconds.
|
||||
#[arg(long, default_value_t = 250)]
|
||||
pub delay_ms: u64,
|
||||
|
||||
/// Read session cookies from this env var instead of SEEDANCE2PRO_COOKIES.
|
||||
#[arg(long)]
|
||||
pub cookies_env: Option<String>,
|
||||
|
||||
/// Account label written into each CSV row. Defaults to the cookies env
|
||||
/// var name.
|
||||
#[arg(long)]
|
||||
pub account: Option<String>,
|
||||
}
|
||||
|
||||
pub async fn run(state: &Seedance2ProState, args: AuditCreditsArgs) -> anyhow::Result<()> {
|
||||
let (cookies, account_label) = match &args.cookies_env {
|
||||
Some(var) => (
|
||||
easyenv::get_env_string_required(var)
|
||||
.map_err(|err| anyhow!("Missing {} env var: {:?}", var, err))?,
|
||||
args.account.clone().unwrap_or_else(|| var.clone()),
|
||||
),
|
||||
None => (
|
||||
state.cookies.clone(),
|
||||
args.account.clone().unwrap_or_else(|| "SEEDANCE2PRO_COOKIES".to_string()),
|
||||
),
|
||||
};
|
||||
let session = Seedance2ProSession::from_cookies_string(cookies);
|
||||
|
||||
let file_is_new = std::fs::metadata(&args.out).map(|m| m.len() == 0).unwrap_or(true);
|
||||
let mut out = OpenOptions::new().create(true).append(true).open(&args.out)
|
||||
.map_err(|err| anyhow!("Cannot open {}: {}", args.out, err))?;
|
||||
if file_is_new {
|
||||
writeln!(out, "account,entry_id,created_at,entry_type,credit_delta,order_id,product_name")?;
|
||||
}
|
||||
|
||||
let mut offset = args.offset;
|
||||
let mut rows = 0u64;
|
||||
let mut type_tallies: HashMap<String, (u64, i64)> = HashMap::new();
|
||||
let mut last_reported_total = 0u64;
|
||||
|
||||
for page_number in 1..=args.max_pages {
|
||||
let page = get_credits_history(GetCreditsHistoryArgs {
|
||||
session: &session,
|
||||
limit: args.limit,
|
||||
offset,
|
||||
host_override: None,
|
||||
}).await
|
||||
.map_err(|err| anyhow!(
|
||||
"Error fetching credits page {} (resume with --offset {}): {:?}",
|
||||
page_number, offset, err))?;
|
||||
|
||||
last_reported_total = page.total;
|
||||
if page.entries.is_empty() {
|
||||
info!("Page {} returned no entries; reached the end of the ledger.", page_number);
|
||||
break;
|
||||
}
|
||||
|
||||
let newest_day = page.entries.first().map(|e| day_of(e)).unwrap_or_default();
|
||||
let oldest_day = page.entries.last().map(|e| day_of(e)).unwrap_or_default();
|
||||
|
||||
for entry in &page.entries {
|
||||
write_entry_row(&mut out, &account_label, entry)?;
|
||||
rows += 1;
|
||||
let tally = type_tallies.entry(entry.entry_type.as_str().to_string()).or_insert((0, 0));
|
||||
tally.0 += 1;
|
||||
tally.1 += entry.credit_delta;
|
||||
}
|
||||
out.flush()?;
|
||||
|
||||
if page_number % 25 == 0 || page_number == 1 {
|
||||
info!(
|
||||
"Page {} (offset {}): {} entries ({} .. {}), written {} (server total {})",
|
||||
page_number, offset, page.entries.len(), newest_day, oldest_day, rows, page.total,
|
||||
);
|
||||
}
|
||||
|
||||
// Newest-first: once a whole page predates the window, everything
|
||||
// after it does too.
|
||||
if args.start_date.as_deref().is_some_and(|start| oldest_day.as_str() < start) {
|
||||
info!("Reached entries older than {}; done.", args.start_date.as_deref().unwrap());
|
||||
break;
|
||||
}
|
||||
|
||||
offset += page.entries.len() as u64;
|
||||
tokio::time::sleep(Duration::from_millis(args.delay_ms)).await;
|
||||
}
|
||||
|
||||
println!("── audit_credits summary [{}] ──", account_label);
|
||||
println!("entries written: {} (server reports ledger total {}; duplicates possible", rows, last_reported_total);
|
||||
println!(" under live growth - dedupe by entry_id downstream)");
|
||||
let mut tallies: Vec<_> = type_tallies.iter().collect();
|
||||
tallies.sort_by_key(|(_, (count, _))| std::cmp::Reverse(*count));
|
||||
for (entry_type, (count, delta_sum)) in tallies {
|
||||
println!(" {:<28} n={:<9} credit_sum={}", entry_type, count, delta_sum);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// UTC day prefix ("YYYY-MM-DD"); ISO strings compare lexicographically.
|
||||
fn day_of(entry: &CreditHistoryEntry) -> String {
|
||||
entry.created_at.get(..10).unwrap_or("").to_string()
|
||||
}
|
||||
|
||||
fn write_entry_row(out: &mut impl Write, account: &str, entry: &CreditHistoryEntry) -> anyhow::Result<()> {
|
||||
writeln!(
|
||||
out,
|
||||
"{},{},{},{},{},{},{}",
|
||||
account,
|
||||
entry.id,
|
||||
entry.created_at,
|
||||
entry.entry_type.as_str(),
|
||||
entry.credit_delta,
|
||||
entry.maybe_order_id.as_deref().unwrap_or(""),
|
||||
csv_quote(&entry.product_name),
|
||||
)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Minimal CSV quoting for the one free-text field.
|
||||
fn csv_quote(field: &str) -> String {
|
||||
if field.is_empty() {
|
||||
return String::new();
|
||||
}
|
||||
format!("\"{}\"", field.replace('"', "\"\"").replace('\n', " "))
|
||||
}
|
||||
-243
@@ -1,243 +0,0 @@
|
||||
use std::fs::OpenOptions;
|
||||
use std::io::Write;
|
||||
use std::time::Duration;
|
||||
|
||||
use anyhow::anyhow;
|
||||
use clap::Args;
|
||||
use log::{info, warn};
|
||||
use seedance2pro_client::creds::seedance2pro_session::Seedance2ProSession;
|
||||
use seedance2pro_client::requests::poll_orders::poll_orders::{
|
||||
poll_orders, OrderStatus, PollOrdersArgs, TaskStatus,
|
||||
};
|
||||
|
||||
use super::super::state::Seedance2ProState;
|
||||
|
||||
/// Dump Kinovi's own per-order billing records (`totalCredits`) to CSV for a
|
||||
/// date window, by paging the getOrders API newest-to-oldest. Used to audit
|
||||
/// whether failed orders carry charges.
|
||||
#[derive(Args)]
|
||||
pub struct AuditOrdersArgs {
|
||||
/// Output CSV path. Appends if the file exists (resume-friendly).
|
||||
#[arg(long)]
|
||||
pub out: String,
|
||||
|
||||
/// Stop paging once a whole page is older than this UTC date (YYYY-MM-DD).
|
||||
/// Without it, paging stops at --max_pages.
|
||||
#[arg(long)]
|
||||
pub start_date: Option<String>,
|
||||
|
||||
/// Skip (do not write) orders on days after this UTC date (YYYY-MM-DD,
|
||||
/// inclusive). Pages still have to be fetched to reach older orders.
|
||||
#[arg(long)]
|
||||
pub end_date: Option<String>,
|
||||
|
||||
/// Maximum pages to fetch this run (30 orders per page).
|
||||
#[arg(long, default_value_t = 200)]
|
||||
pub max_pages: usize,
|
||||
|
||||
/// Resume from a cursor logged by a previous run.
|
||||
#[arg(long)]
|
||||
pub cursor: Option<u64>,
|
||||
|
||||
/// Delay between page fetches, in milliseconds.
|
||||
#[arg(long, default_value_t = 250)]
|
||||
pub delay_ms: u64,
|
||||
|
||||
/// Read session cookies from this env var instead of SEEDANCE2PRO_COOKIES
|
||||
/// (e.g. SEEDANCE2PRO_VOLCENGINE_COOKIES / SEEDANCE2PRO_BYTEPLUS_COOKIES /
|
||||
/// SEEDANCE2PRO_BYTEPLUS_ULTRA_COOKIES to audit each Kinovi account).
|
||||
#[arg(long)]
|
||||
pub cookies_env: Option<String>,
|
||||
|
||||
/// Account label written into each CSV row. Defaults to the cookies env
|
||||
/// var name (or "SEEDANCE2PRO_COOKIES").
|
||||
#[arg(long)]
|
||||
pub account: Option<String>,
|
||||
}
|
||||
|
||||
pub async fn run(state: &Seedance2ProState, args: AuditOrdersArgs) -> anyhow::Result<()> {
|
||||
let (cookies, account_label) = match &args.cookies_env {
|
||||
Some(var) => (
|
||||
easyenv::get_env_string_required(var)
|
||||
.map_err(|err| anyhow!("Missing {} env var: {:?}", var, err))?,
|
||||
args.account.clone().unwrap_or_else(|| var.clone()),
|
||||
),
|
||||
None => (
|
||||
state.cookies.clone(),
|
||||
args.account.clone().unwrap_or_else(|| "SEEDANCE2PRO_COOKIES".to_string()),
|
||||
),
|
||||
};
|
||||
let session = Seedance2ProSession::from_cookies_string(cookies);
|
||||
|
||||
let file_is_new = std::fs::metadata(&args.out).map(|m| m.len() == 0).unwrap_or(true);
|
||||
let mut out = OpenOptions::new().create(true).append(true).open(&args.out)
|
||||
.map_err(|err| anyhow!("Cannot open {}: {}", args.out, err))?;
|
||||
if file_is_new {
|
||||
writeln!(out, "account,order_id,created_at,task_status,media_type,total_credits,result_count,fail_reason")?;
|
||||
}
|
||||
|
||||
let mut cursor = args.cursor;
|
||||
let mut stats = AuditStats::default();
|
||||
|
||||
for page in 1..=args.max_pages {
|
||||
let result = poll_orders(PollOrdersArgs {
|
||||
session: &session,
|
||||
cursor,
|
||||
host_override: None,
|
||||
}).await
|
||||
.map_err(|err| anyhow!(
|
||||
"Error polling page {} (resume with --cursor {:?}): {:?}", page, cursor, err))?;
|
||||
|
||||
if result.orders.is_empty() {
|
||||
info!("Page {} returned no orders; stopping.", page);
|
||||
break;
|
||||
}
|
||||
|
||||
let newest = order_day(result.orders.first().unwrap());
|
||||
let oldest = order_day(result.orders.last().unwrap());
|
||||
|
||||
for order in &result.orders {
|
||||
let day = order_day(order);
|
||||
if args.end_date.as_deref().is_some_and(|end| day > end) {
|
||||
stats.skipped_after_window += 1;
|
||||
continue;
|
||||
}
|
||||
if args.start_date.as_deref().is_some_and(|start| day < start) {
|
||||
stats.skipped_before_window += 1;
|
||||
continue;
|
||||
}
|
||||
write_order_row(&mut out, &account_label, order)?;
|
||||
stats.record(order);
|
||||
}
|
||||
out.flush()?;
|
||||
|
||||
info!(
|
||||
"Page {}: {} orders ({} .. {}), written so far: {}, next cursor: {:?}",
|
||||
page, result.orders.len(), newest, oldest, stats.written, result.next_cursor,
|
||||
);
|
||||
|
||||
// The API pages newest-to-oldest: once the whole page predates the
|
||||
// window, everything after it will too.
|
||||
if args.start_date.as_deref().is_some_and(|start| oldest < start) {
|
||||
info!("Reached orders older than {}; done.", args.start_date.as_deref().unwrap());
|
||||
cursor = result.next_cursor;
|
||||
break;
|
||||
}
|
||||
|
||||
cursor = result.next_cursor;
|
||||
if cursor.is_none() {
|
||||
info!("No further pages; reached the end of the account's history.");
|
||||
break;
|
||||
}
|
||||
|
||||
tokio::time::sleep(Duration::from_millis(args.delay_ms)).await;
|
||||
}
|
||||
|
||||
stats.print_summary(&account_label, cursor);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
struct AuditStats {
|
||||
written: u64,
|
||||
skipped_after_window: u64,
|
||||
skipped_before_window: u64,
|
||||
completed: u64,
|
||||
completed_credits: u64,
|
||||
completed_missing_credits: u64,
|
||||
failed: u64,
|
||||
failed_credits: u64,
|
||||
failed_with_nonzero_credits: u64,
|
||||
in_flight: u64,
|
||||
in_flight_credits: u64,
|
||||
}
|
||||
|
||||
impl AuditStats {
|
||||
fn record(&mut self, order: &OrderStatus) {
|
||||
self.written += 1;
|
||||
let credits = u64::from(order.total_credits.unwrap_or(0));
|
||||
match order.task_status {
|
||||
TaskStatus::Completed => {
|
||||
self.completed += 1;
|
||||
self.completed_credits += credits;
|
||||
if order.total_credits.is_none() {
|
||||
self.completed_missing_credits += 1;
|
||||
}
|
||||
}
|
||||
TaskStatus::Failed => {
|
||||
self.failed += 1;
|
||||
self.failed_credits += credits;
|
||||
if credits > 0 {
|
||||
self.failed_with_nonzero_credits += 1;
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
self.in_flight += 1;
|
||||
self.in_flight_credits += credits;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn print_summary(&self, account: &str, resume_cursor: Option<u64>) {
|
||||
// Final results are intentionally println: they ARE the tool's output.
|
||||
println!("── audit_orders summary [{}] ──", account);
|
||||
println!("orders written: {}", self.written);
|
||||
println!(" completed: {} ({} credits; {} missing totalCredits)",
|
||||
self.completed, self.completed_credits, self.completed_missing_credits);
|
||||
println!(" failed: {} ({} credits; {} orders with credits > 0)",
|
||||
self.failed, self.failed_credits, self.failed_with_nonzero_credits);
|
||||
println!(" pending/processing: {} ({} credits)", self.in_flight, self.in_flight_credits);
|
||||
println!("skipped (newer than window): {}", self.skipped_after_window);
|
||||
println!("skipped (older than window): {}", self.skipped_before_window);
|
||||
match resume_cursor {
|
||||
Some(c) => println!("resume cursor: {}", c),
|
||||
None => println!("resume cursor: (none - history exhausted or window done)"),
|
||||
}
|
||||
if self.failed_with_nonzero_credits > 0 {
|
||||
warn!(
|
||||
"{} FAILED orders carry non-zero totalCredits ({} credits total) — \
|
||||
Kinovi's own records say failures were charged.",
|
||||
self.failed_with_nonzero_credits, self.failed_credits,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// UTC day prefix ("YYYY-MM-DD") of the order's ISO 8601 created_at.
|
||||
/// ISO strings compare lexicographically, so no date parsing is needed.
|
||||
fn order_day(order: &OrderStatus) -> &str {
|
||||
order.created_at.get(..10).unwrap_or("")
|
||||
}
|
||||
|
||||
fn write_order_row(out: &mut impl Write, account: &str, order: &OrderStatus) -> anyhow::Result<()> {
|
||||
let media_type = order.media_type.as_ref()
|
||||
.map(|m| format!("{:?}", m))
|
||||
.unwrap_or_default();
|
||||
let total_credits = order.total_credits
|
||||
.map(|c| c.to_string())
|
||||
.unwrap_or_default();
|
||||
let fail_reason = order.fail_reason.as_ref()
|
||||
.map(|fr| fr.reason.as_str())
|
||||
.unwrap_or("");
|
||||
writeln!(
|
||||
out,
|
||||
"{},{},{},{:?},{},{},{},{}",
|
||||
account,
|
||||
order.order_id,
|
||||
order.created_at,
|
||||
order.task_status,
|
||||
media_type,
|
||||
total_credits,
|
||||
order.results.len(),
|
||||
csv_quote(fail_reason),
|
||||
)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Minimal CSV quoting for the one free-text field.
|
||||
fn csv_quote(field: &str) -> String {
|
||||
if field.is_empty() {
|
||||
return String::new();
|
||||
}
|
||||
format!("\"{}\"", field.replace('"', "\"\"").replace('\n', " "))
|
||||
}
|
||||
-131
@@ -1,131 +0,0 @@
|
||||
use std::fs::OpenOptions;
|
||||
use std::io::Write;
|
||||
use std::time::Duration;
|
||||
|
||||
use anyhow::anyhow;
|
||||
use clap::Args;
|
||||
use log::info;
|
||||
use seedance2pro_client::billing::get_billing_payments_history::{
|
||||
get_billing_payments_history, BillingPayment, GetBillingPaymentsHistoryArgs,
|
||||
DEFAULT_BILLING_PAGE_LIMIT,
|
||||
};
|
||||
use seedance2pro_client::creds::seedance2pro_session::Seedance2ProSession;
|
||||
|
||||
use super::super::state::Seedance2ProState;
|
||||
|
||||
/// Dump the account's full billing payments history (credit-package
|
||||
/// purchases) to CSV, for reconciling refill charges against invoices.
|
||||
#[derive(Args)]
|
||||
pub struct AuditPaymentsArgs {
|
||||
/// Output CSV path. Appends if the file exists.
|
||||
#[arg(long)]
|
||||
pub out: String,
|
||||
|
||||
/// Maximum pages to fetch (20 payments per page).
|
||||
#[arg(long, default_value_t = 500)]
|
||||
pub max_pages: usize,
|
||||
|
||||
/// Delay between page fetches, in milliseconds.
|
||||
#[arg(long, default_value_t = 250)]
|
||||
pub delay_ms: u64,
|
||||
|
||||
/// Read session cookies from this env var instead of SEEDANCE2PRO_COOKIES.
|
||||
#[arg(long)]
|
||||
pub cookies_env: Option<String>,
|
||||
|
||||
/// Account label written into each CSV row. Defaults to the cookies env
|
||||
/// var name.
|
||||
#[arg(long)]
|
||||
pub account: Option<String>,
|
||||
}
|
||||
|
||||
pub async fn run(state: &Seedance2ProState, args: AuditPaymentsArgs) -> anyhow::Result<()> {
|
||||
let (cookies, account_label) = match &args.cookies_env {
|
||||
Some(var) => (
|
||||
easyenv::get_env_string_required(var)
|
||||
.map_err(|err| anyhow!("Missing {} env var: {:?}", var, err))?,
|
||||
args.account.clone().unwrap_or_else(|| var.clone()),
|
||||
),
|
||||
None => (
|
||||
state.cookies.clone(),
|
||||
args.account.clone().unwrap_or_else(|| "SEEDANCE2PRO_COOKIES".to_string()),
|
||||
),
|
||||
};
|
||||
let session = Seedance2ProSession::from_cookies_string(cookies);
|
||||
|
||||
let file_is_new = std::fs::metadata(&args.out).map(|m| m.len() == 0).unwrap_or(true);
|
||||
let mut out = OpenOptions::new().create(true).append(true).open(&args.out)
|
||||
.map_err(|err| anyhow!("Cannot open {}: {}", args.out, err))?;
|
||||
if file_is_new {
|
||||
writeln!(out, "account,payment_id,created_at,amount_usd,credits_earned,status,payment_type,product_name,transaction_id")?;
|
||||
}
|
||||
|
||||
let mut offset: u64 = 0;
|
||||
let mut rows = 0u64;
|
||||
let mut total_amount = 0f64;
|
||||
let mut total_credits = 0u64;
|
||||
let mut last_reported_total = 0u64;
|
||||
|
||||
for page_number in 1..=args.max_pages {
|
||||
let page = get_billing_payments_history(GetBillingPaymentsHistoryArgs {
|
||||
session: &session,
|
||||
limit: DEFAULT_BILLING_PAGE_LIMIT,
|
||||
offset,
|
||||
host_override: None,
|
||||
}).await
|
||||
.map_err(|err| anyhow!(
|
||||
"Error fetching payments page {} (offset {}): {:?}", page_number, offset, err))?;
|
||||
|
||||
last_reported_total = page.total;
|
||||
if page.payments.is_empty() {
|
||||
break;
|
||||
}
|
||||
|
||||
for payment in &page.payments {
|
||||
write_payment_row(&mut out, &account_label, payment)?;
|
||||
rows += 1;
|
||||
total_amount += payment.amount_usd;
|
||||
total_credits += payment.credits_earned;
|
||||
}
|
||||
out.flush()?;
|
||||
|
||||
info!(
|
||||
"Page {} (offset {}): {} payments; running ${:.2} / {} credits (server total {})",
|
||||
page_number, offset, page.payments.len(), total_amount, total_credits, page.total,
|
||||
);
|
||||
|
||||
offset += page.payments.len() as u64;
|
||||
tokio::time::sleep(Duration::from_millis(args.delay_ms)).await;
|
||||
}
|
||||
|
||||
println!("── audit_payments summary [{}] ──", account_label);
|
||||
println!("payments written: {} (server reports total {})", rows, last_reported_total);
|
||||
println!("lifetime charged: ${:.2}", total_amount);
|
||||
println!("lifetime credits: {}", total_credits);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn write_payment_row(out: &mut impl Write, account: &str, payment: &BillingPayment) -> anyhow::Result<()> {
|
||||
writeln!(
|
||||
out,
|
||||
"{},{},{},{},{},{:?},{:?},{},{}",
|
||||
account,
|
||||
payment.id,
|
||||
payment.created_at,
|
||||
payment.amount_usd,
|
||||
payment.credits_earned,
|
||||
payment.status,
|
||||
payment.payment_type,
|
||||
csv_quote(&payment.product_name),
|
||||
payment.transaction_id,
|
||||
)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Minimal CSV quoting for the one free-text field.
|
||||
fn csv_quote(field: &str) -> String {
|
||||
if field.is_empty() {
|
||||
return String::new();
|
||||
}
|
||||
format!("\"{}\"", field.replace('"', "\"\"").replace('\n', " "))
|
||||
}
|
||||
@@ -1,7 +1,3 @@
|
||||
pub mod account_info;
|
||||
pub mod audit_credits;
|
||||
pub mod audit_orders;
|
||||
pub mod audit_payments;
|
||||
pub mod failed_job_histogram;
|
||||
pub mod find_job;
|
||||
pub mod generate_video;
|
||||
|
||||
@@ -169,60 +169,6 @@ export class UsersApi extends ApiManager {
|
||||
}));
|
||||
}
|
||||
|
||||
public async RequestPasswordReset({
|
||||
email,
|
||||
}: {
|
||||
email: string;
|
||||
}): Promise<ApiResponse<null>> {
|
||||
const endpoint = `${this.getApiSchemeAndHost()}/v1/request_password_reset`;
|
||||
return await this.post<
|
||||
{ email: string },
|
||||
{ success: boolean; error_message?: string }
|
||||
>({
|
||||
endpoint,
|
||||
body: { email },
|
||||
})
|
||||
.then((response) => ({
|
||||
success: response.success,
|
||||
errorMessage: response.error_message,
|
||||
}))
|
||||
.catch((err) => ({
|
||||
success: false,
|
||||
errorMessage: err.message,
|
||||
}));
|
||||
}
|
||||
|
||||
public async ResetPassword({
|
||||
token,
|
||||
password,
|
||||
passwordConfirmation,
|
||||
}: {
|
||||
token: string;
|
||||
password: string;
|
||||
passwordConfirmation: string;
|
||||
}): Promise<ApiResponse<null>> {
|
||||
const endpoint = `${this.getApiSchemeAndHost()}/v1/reset_password`;
|
||||
return await this.post<
|
||||
{ token: string; password: string; password_confirmation: string },
|
||||
{ success: boolean; error_message?: string }
|
||||
>({
|
||||
endpoint,
|
||||
body: {
|
||||
token,
|
||||
password,
|
||||
password_confirmation: passwordConfirmation,
|
||||
},
|
||||
})
|
||||
.then((response) => ({
|
||||
success: response.success,
|
||||
errorMessage: response.error_message,
|
||||
}))
|
||||
.catch((err) => ({
|
||||
success: false,
|
||||
errorMessage: err.message,
|
||||
}));
|
||||
}
|
||||
|
||||
public async ChangePassword({
|
||||
password,
|
||||
passwordConfirmation,
|
||||
|
||||
@@ -1,38 +0,0 @@
|
||||
import { ApiManager, ApiResponse } from "./ApiManager";
|
||||
|
||||
export class AnalyticsApi extends ApiManager {
|
||||
public PostAnalytics({
|
||||
maybeLastAction,
|
||||
maybeLogToken,
|
||||
}: {
|
||||
maybeLastAction: string;
|
||||
maybeLogToken: string;
|
||||
}): Promise<ApiResponse<string>> {
|
||||
const endpoint = `${this.getApiSchemeAndHost()}/v1/analytics/log_session`;
|
||||
|
||||
const body = {
|
||||
maybe_last_action: maybeLastAction,
|
||||
maybe_log_token: maybeLogToken,
|
||||
};
|
||||
|
||||
return this.post<
|
||||
{
|
||||
maybe_last_action: string;
|
||||
maybe_log_token: string;
|
||||
},
|
||||
{
|
||||
success?: boolean;
|
||||
log_token?: string;
|
||||
BadInput?: string;
|
||||
}
|
||||
>({ endpoint, body })
|
||||
.then((response) => ({
|
||||
success: response.success ?? false,
|
||||
data: response.log_token,
|
||||
errorMessage: response.BadInput,
|
||||
}))
|
||||
.catch((err) => {
|
||||
return { success: false, errorMessage: err.message };
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -86,128 +86,4 @@ export class EngineApi extends ApiManager {
|
||||
return result;
|
||||
}
|
||||
|
||||
public async enqueueImageGeneration({
|
||||
disableSystemPrompt,
|
||||
prompt,
|
||||
snapshotMediaToken,
|
||||
additionalImages,
|
||||
}: {
|
||||
disableSystemPrompt: boolean;
|
||||
prompt: string;
|
||||
snapshotMediaToken: string;
|
||||
additionalImages?: string[];
|
||||
}): Promise<ApiResponse<string>> {
|
||||
const endpoint = `${this.getApiSchemeAndHost()}/v1/image_studio/prompt`;
|
||||
|
||||
const uuidIdempotencyToken = crypto.randomUUID(); // Generate a new UUID
|
||||
const body = {
|
||||
uuid_idempotency_token: uuidIdempotencyToken,
|
||||
disable_system_prompt: disableSystemPrompt,
|
||||
prompt,
|
||||
snapshot_media_token: snapshotMediaToken, // Changed from scene_media_token to snapshot_media_token
|
||||
additional_images: additionalImages,
|
||||
};
|
||||
|
||||
const postResponse = await this.post<
|
||||
{
|
||||
uuid_idempotency_token: string;
|
||||
disable_system_prompt: boolean;
|
||||
prompt: string;
|
||||
snapshot_media_token: string;
|
||||
additional_images?: string[];
|
||||
},
|
||||
{ success?: boolean; job_token?: string; error_reason?: string, failure_reason?: string}
|
||||
>({
|
||||
endpoint,
|
||||
body,
|
||||
});
|
||||
console.log("postResponse", postResponse);
|
||||
// Check if the response is successful
|
||||
const isSuccess = postResponse.success ?? false;
|
||||
|
||||
// Prepare the result object
|
||||
const result = {
|
||||
success: isSuccess,
|
||||
data: isSuccess ? postResponse.job_token : undefined,
|
||||
errorMessage: isSuccess ? undefined : (postResponse.error_reason || postResponse.failure_reason),
|
||||
};
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public async pollJobSession(jobToken: string): Promise<
|
||||
ApiResponse<{
|
||||
status: string;
|
||||
result: {
|
||||
generated_images?: string[];
|
||||
error?: string;
|
||||
};
|
||||
}>
|
||||
> {
|
||||
const endpoint = `${this.getApiSchemeAndHost()}/v1/jobs/session/${jobToken}`;
|
||||
|
||||
const response = await this.get<{
|
||||
success?: boolean;
|
||||
status?: string;
|
||||
result?: {
|
||||
generated_images?: string[];
|
||||
error?: string;
|
||||
};
|
||||
BadInput?: string;
|
||||
}>({
|
||||
endpoint,
|
||||
});
|
||||
|
||||
const success = response.success ?? false;
|
||||
const status = response.status ?? "";
|
||||
const result = response.result ?? {
|
||||
generated_images: [],
|
||||
error: undefined,
|
||||
};
|
||||
const errorMessage = response.BadInput;
|
||||
|
||||
return {
|
||||
success,
|
||||
data: { status, result },
|
||||
errorMessage,
|
||||
};
|
||||
}
|
||||
|
||||
public async pollStudioSessionJobs(jobToken: string): Promise<
|
||||
ApiResponse<{
|
||||
status: string;
|
||||
result: {
|
||||
generated_images?: string[];
|
||||
error?: string;
|
||||
};
|
||||
}>
|
||||
> {
|
||||
const endpoint = `${this.getApiSchemeAndHost()}/v1/image_studio/session_jobs/${jobToken}`;
|
||||
|
||||
const response = await this.get<{
|
||||
success?: boolean;
|
||||
status?: string;
|
||||
result?: {
|
||||
generated_images?: string[];
|
||||
error?: string;
|
||||
};
|
||||
BadInput?: string;
|
||||
}>({
|
||||
endpoint,
|
||||
});
|
||||
|
||||
const success = response.success ?? false;
|
||||
const status = response.status ?? "";
|
||||
const result = response.result ?? {
|
||||
generated_images: [],
|
||||
error: undefined,
|
||||
};
|
||||
const errorMessage = response.BadInput;
|
||||
|
||||
return {
|
||||
success,
|
||||
data: { status, result },
|
||||
errorMessage,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,53 +1,5 @@
|
||||
import { ApiManager, ApiResponse } from "./ApiManager";
|
||||
import type {
|
||||
Pagination,
|
||||
UserBookmarkBatch,
|
||||
UserBookmarkByEntity,
|
||||
UserBookmarkByUser,
|
||||
} from "@storyteller/ui-pagescene";
|
||||
import { authentication } from "~/signals";
|
||||
|
||||
export enum ScopedEntityTypes {
|
||||
USER = "user",
|
||||
TTS_MODEL = "tts-model",
|
||||
TTS_RESULT = "tts-result",
|
||||
W2L_TEMPLATE = "w2l-template",
|
||||
W2L_RESULT = "w2l-result",
|
||||
MEDIA_FILE = "media_file",
|
||||
MODEL_WEIGHT = "model_weight",
|
||||
VOICE_CONVERSION_MODEL = "voice_conversion_model",
|
||||
ZS_VOICE = "z_voice",
|
||||
}
|
||||
|
||||
export enum ScopedWeightTypes {
|
||||
HIFIGAN_TTl2 = "hifigan_tt2",
|
||||
RVC_V2 = "rvc_v2",
|
||||
SD_1_5 = "sd_1.5",
|
||||
SDXL = "sdxl",
|
||||
SO_VITS_SVC = "so_vits_svc",
|
||||
TT2 = "tt2",
|
||||
LORA = "loRA",
|
||||
VALL_E = "vall_e",
|
||||
COMFY_UI = "comfy_ui",
|
||||
}
|
||||
|
||||
export enum ScopedMediaFileType {
|
||||
IMAGE_GENERATION = "image_generation",
|
||||
TEXT_TO_SPEECH = "text_to_speech",
|
||||
VOCODER = "vocoder",
|
||||
VOICE_CONVERSION = "voice_conversion",
|
||||
WORKFLOW_CONFIG = "workflow_config",
|
||||
}
|
||||
|
||||
interface ListUserBookmarksByUserRequest {
|
||||
username?: string;
|
||||
sort_ascending?: boolean;
|
||||
page_size?: number;
|
||||
page_index?: number;
|
||||
maybe_scoped_entity_type?: ScopedEntityTypes[];
|
||||
maybe_scoped_weight_type?: ScopedWeightTypes[];
|
||||
maybe_scoped_media_file_type?: ScopedMediaFileType[];
|
||||
}
|
||||
import type { UserBookmarkBatch } from "@storyteller/ui-pagescene";
|
||||
|
||||
export class UserBookmarksApi extends ApiManager {
|
||||
public CreateUserBookmark({
|
||||
@@ -134,69 +86,4 @@ export class UserBookmarksApi extends ApiManager {
|
||||
});
|
||||
}
|
||||
|
||||
public ListUserBookmarksByUser({
|
||||
username,
|
||||
sort_ascending,
|
||||
page_size,
|
||||
page_index,
|
||||
maybe_scoped_entity_type,
|
||||
maybe_scoped_weight_type,
|
||||
maybe_scoped_media_file_type,
|
||||
}: ListUserBookmarksByUserRequest): Promise<
|
||||
ApiResponse<UserBookmarkByUser[], Pagination>
|
||||
> {
|
||||
const user = username ?? authentication.userInfo.value?.username;
|
||||
const endpoint = `${this.getApiSchemeAndHost()}/v1/user_bookmarks/list/${user}`;
|
||||
|
||||
const query = {
|
||||
sort_ascending,
|
||||
page_size,
|
||||
page_index,
|
||||
maybe_scoped_entity_type: maybe_scoped_entity_type
|
||||
? maybe_scoped_entity_type.join(",")
|
||||
: undefined,
|
||||
maybe_scoped_weight_type: maybe_scoped_weight_type
|
||||
? maybe_scoped_weight_type.join(",")
|
||||
: undefined,
|
||||
maybe_scoped_media_file_type: maybe_scoped_media_file_type
|
||||
? maybe_scoped_media_file_type.join(",")
|
||||
: undefined,
|
||||
};
|
||||
|
||||
return this.get<{
|
||||
success?: boolean;
|
||||
pagination?: Pagination;
|
||||
results: UserBookmarkByUser[];
|
||||
}>({ endpoint, query })
|
||||
.then((response) => ({
|
||||
success: response.success ?? false,
|
||||
data: response.results,
|
||||
pagination: response.pagination,
|
||||
}))
|
||||
.catch((err) => {
|
||||
return { success: false, errorMessage: err.message };
|
||||
});
|
||||
}
|
||||
|
||||
public ListUserBookmarksByEntity({
|
||||
entityType,
|
||||
entityToken,
|
||||
}: {
|
||||
entityType: string;
|
||||
entityToken: string;
|
||||
}): Promise<ApiResponse<UserBookmarkByEntity[]>> {
|
||||
const endpoint = `${this.getApiSchemeAndHost()}/v1/user_bookmarks/list/${entityType}/${entityToken}`;
|
||||
|
||||
return this.get<{
|
||||
success: boolean;
|
||||
user_bookmarks: UserBookmarkByEntity[];
|
||||
}>({ endpoint })
|
||||
.then((response) => ({
|
||||
success: true,
|
||||
data: response.user_bookmarks,
|
||||
}))
|
||||
.catch((err) => {
|
||||
return { success: false, errorMessage: err.message };
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,53 +0,0 @@
|
||||
import { ApiManager, ApiResponse } from "./ApiManager";
|
||||
|
||||
interface VoiceConversionParams {
|
||||
autoPredictF0?: boolean;
|
||||
creatorSetVisibility?: string;
|
||||
isStorytellerDemo?: boolean;
|
||||
overrideF0Method?: string;
|
||||
sourceMediaUploadToken: string;
|
||||
transpose?: number;
|
||||
uuidIdempotencyToken: string;
|
||||
voiceConversionModelToken: string;
|
||||
}
|
||||
|
||||
interface VoiceConversionRequest {
|
||||
auto_predict_f0: boolean;
|
||||
creator_set_visibility: string;
|
||||
is_storyteller_demo: boolean;
|
||||
override_f0_method: string;
|
||||
source_media_upload_token: string;
|
||||
transpose: number;
|
||||
uuid_idempotency_token: string;
|
||||
voice_conversion_model_token: string;
|
||||
}
|
||||
|
||||
export class VoiceConversionApi extends ApiManager {
|
||||
public ConvertVoice(
|
||||
params: VoiceConversionParams,
|
||||
): Promise<ApiResponse<string>> {
|
||||
const endpoint = `${this.getApiSchemeAndHost()}/v1/voice_conversion/inference`;
|
||||
|
||||
const body = this.parseBodyValues<
|
||||
VoiceConversionParams,
|
||||
VoiceConversionRequest
|
||||
>(params);
|
||||
|
||||
return this.post<
|
||||
VoiceConversionRequest,
|
||||
{
|
||||
success?: boolean;
|
||||
inference_job_token?: string;
|
||||
BadInput?: string;
|
||||
}
|
||||
>({ endpoint, body })
|
||||
.then((response) => ({
|
||||
success: response.success ?? false,
|
||||
data: response.inference_job_token,
|
||||
errorMessage: response.BadInput,
|
||||
}))
|
||||
.catch((err) => {
|
||||
return { success: false, errorMessage: err.message };
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -1,78 +0,0 @@
|
||||
import { ApiManager, ApiResponse } from "./ApiManager";
|
||||
import type { Pagination } from "@storyteller/ui-pagescene";
|
||||
import { authentication } from "~/signals";
|
||||
import { ZsDataset } from "~/models/Dataset";
|
||||
|
||||
interface VoiceDesignerParams {
|
||||
text: string;
|
||||
uuidIdempotencyToken: string;
|
||||
voiceToken: string;
|
||||
}
|
||||
|
||||
interface VoiceDesignerRequest {
|
||||
text: string;
|
||||
uuid_idempotency_token: string;
|
||||
voice_token: string;
|
||||
}
|
||||
|
||||
export class VoiceDesignerApi extends ApiManager {
|
||||
public EnqueueTts(
|
||||
params: VoiceDesignerParams,
|
||||
): Promise<
|
||||
ApiResponse<{ inference_job_token?: string; voice_token?: string }>
|
||||
> {
|
||||
const endpoint = `${this.getApiSchemeAndHost()}/v1/voice_designer/enqueue_tts`;
|
||||
|
||||
const body = this.parseBodyValues<
|
||||
VoiceDesignerParams,
|
||||
VoiceDesignerRequest
|
||||
>(params);
|
||||
|
||||
return this.post<
|
||||
VoiceDesignerRequest,
|
||||
{
|
||||
success?: boolean;
|
||||
inference_job_token?: string;
|
||||
voice_token: string;
|
||||
BadInput?: string;
|
||||
}
|
||||
>({ endpoint, body })
|
||||
.then((response) => ({
|
||||
success: response.success ?? false,
|
||||
...(response.success
|
||||
? {
|
||||
data: {
|
||||
inference_job_token: response.inference_job_token,
|
||||
voice_token: response.voice_token,
|
||||
},
|
||||
}
|
||||
: {}),
|
||||
errorMessage: response.BadInput,
|
||||
}))
|
||||
.catch((err) => {
|
||||
return { success: false, errorMessage: err.message };
|
||||
});
|
||||
}
|
||||
|
||||
public ListDatasetsByUser({
|
||||
username,
|
||||
}: {
|
||||
username?: string;
|
||||
}): Promise<ApiResponse<ZsDataset[]>> {
|
||||
const user = username ?? authentication.userInfo.value?.username;
|
||||
const endpoint = `${this.getApiSchemeAndHost()}/v1/voice_designer/user/${user}/list`;
|
||||
|
||||
return this.get<{
|
||||
success: boolean;
|
||||
datasets: ZsDataset[];
|
||||
pagination: Pagination;
|
||||
}>({ endpoint })
|
||||
.then((response) => ({
|
||||
success: true,
|
||||
data: response.datasets,
|
||||
}))
|
||||
.catch((err) => {
|
||||
return { success: false, errorMessage: err.message };
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -275,7 +275,7 @@ export class WeightsApi extends ApiManager {
|
||||
weightToken: string;
|
||||
coverImageMediaFileToken: string;
|
||||
}): Promise<ApiResponse<undefined>> {
|
||||
const endpoint = `${this.getApiSchemeAndHost()}/v1/weights/${weightToken}/cover_image`;
|
||||
const endpoint = `${this.getApiSchemeAndHost()}/v1/weights/weight/${weightToken}/cover_image`;
|
||||
|
||||
const body = {
|
||||
cover_image_media_file_token: coverImageMediaFileToken,
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
// https://storyteller-docs.netlify.app/#/
|
||||
export * from "./AnalyticsApi";
|
||||
export * from "./BetaKeysApi";
|
||||
export * from "./BillingApi";
|
||||
export * from "./CommentsApi";
|
||||
|
||||
@@ -1,39 +0,0 @@
|
||||
import { authentication } from "~/signals";
|
||||
import { UserInfo } from "~/models";
|
||||
import EnvironmentVariables from "~/Classes/EnvironmentVariables";
|
||||
import { AnalyticsApi } from "~/Classes/ApiManager/AnalyticsApi";
|
||||
import testUpdateDeleteEndpoints from "./utils/testUpdateDeleteEndpoints";
|
||||
|
||||
EnvironmentVariables.initialize({ BASE_API: "http://localhost:3000" });
|
||||
|
||||
describe("AnalyticsApi", () => {
|
||||
beforeAll(() => {
|
||||
authentication.userInfo.value = {
|
||||
user_token: "un1",
|
||||
username: "un1",
|
||||
} as UserInfo;
|
||||
});
|
||||
describe("run tests", () => {
|
||||
const api = new AnalyticsApi();
|
||||
[
|
||||
{
|
||||
name: "PostAnalytics",
|
||||
function: api.PostAnalytics.bind(api),
|
||||
endpoint: "http://localhost:3000/v1/analytics/log_session",
|
||||
method: "POST",
|
||||
paramsIn: {
|
||||
maybeLastAction: "mla1",
|
||||
maybeLogToken: "mlt1",
|
||||
},
|
||||
paramsTest: {
|
||||
maybe_last_action: "mla1",
|
||||
maybe_log_token: "mlt1",
|
||||
},
|
||||
response: { success: true, log_token: "lt1" },
|
||||
data: "lt1",
|
||||
},
|
||||
].forEach((testMethod) => {
|
||||
testUpdateDeleteEndpoints(api, testMethod);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -1,58 +1,7 @@
|
||||
import { authentication } from "~/signals";
|
||||
import { UserInfo } from "~/models";
|
||||
import EnvironmentVariables from "~/Classes/EnvironmentVariables";
|
||||
import {
|
||||
ScopedEntityTypes,
|
||||
ScopedMediaFileType,
|
||||
ScopedWeightTypes,
|
||||
UserBookmarksApi,
|
||||
} from "~/Classes/ApiManager/UserBookmarksApi";
|
||||
|
||||
const mockBookmark = {
|
||||
created_at: "2024-06-15T11:15:26.997Z",
|
||||
details: {
|
||||
entity_token: "string",
|
||||
entity_type: "user",
|
||||
maybe_media_file_data: {
|
||||
maybe_creator: {
|
||||
default_avatar: {
|
||||
color_index: 0,
|
||||
image_index: 0,
|
||||
},
|
||||
display_name: "string",
|
||||
gravatar_hash: "string",
|
||||
user_token: "string",
|
||||
username: "string",
|
||||
},
|
||||
media_type: "audio",
|
||||
public_bucket_path: "string",
|
||||
},
|
||||
maybe_summary_text: "string",
|
||||
maybe_thumbnail_url: "string",
|
||||
maybe_weight_data: {
|
||||
maybe_cover_image_public_bucket_path: "string",
|
||||
maybe_creator: {
|
||||
default_avatar: {
|
||||
color_index: 0,
|
||||
image_index: 0,
|
||||
},
|
||||
display_name: "string",
|
||||
gravatar_hash: "string",
|
||||
user_token: "string",
|
||||
username: "string",
|
||||
},
|
||||
title: "string",
|
||||
weight_category: "image_generation",
|
||||
weight_type: "hifigan_tt2",
|
||||
},
|
||||
stats: {
|
||||
bookmark_count: 0,
|
||||
positive_rating_count: 0,
|
||||
},
|
||||
},
|
||||
token: "string",
|
||||
updated_at: "2024-06-15T11:15:26.997Z",
|
||||
};
|
||||
import { UserBookmarksApi } from "~/Classes/ApiManager/UserBookmarksApi";
|
||||
|
||||
describe("UserBookmarksApi", () => {
|
||||
beforeAll(() => {
|
||||
@@ -286,192 +235,4 @@ describe("UserBookmarksApi", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("ListUserBookmarksByUser", () => {
|
||||
it("no params", async () => {
|
||||
const api = new UserBookmarksApi();
|
||||
jest.spyOn(api, "fetch").mockResolvedValue({
|
||||
pagination: {
|
||||
current: 0,
|
||||
total_page_count: 0,
|
||||
},
|
||||
results: [mockBookmark],
|
||||
success: true,
|
||||
});
|
||||
const response = await api.ListUserBookmarksByUser({});
|
||||
expect(api.fetch).toHaveBeenCalledWith(
|
||||
"http://localhost:3000/v1/user_bookmarks/list/un1",
|
||||
{
|
||||
method: "GET",
|
||||
query: {
|
||||
maybe_scoped_entity_type: undefined,
|
||||
maybe_scoped_media_file_type: undefined,
|
||||
maybe_scoped_weight_type: undefined,
|
||||
page_index: undefined,
|
||||
page_size: undefined,
|
||||
sort_ascending: undefined,
|
||||
},
|
||||
},
|
||||
);
|
||||
expect(response).toEqual({
|
||||
success: true,
|
||||
data: [mockBookmark],
|
||||
pagination: {
|
||||
current: 0,
|
||||
total_page_count: 0,
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
it("all params", async () => {
|
||||
const api = new UserBookmarksApi();
|
||||
jest.spyOn(api, "fetch").mockResolvedValue({
|
||||
pagination: {
|
||||
current: 0,
|
||||
total_page_count: 0,
|
||||
},
|
||||
results: [mockBookmark],
|
||||
success: true,
|
||||
});
|
||||
const response = await api.ListUserBookmarksByUser({
|
||||
username: "user1",
|
||||
sort_ascending: true,
|
||||
page_size: 76,
|
||||
page_index: 2,
|
||||
maybe_scoped_entity_type: [ScopedEntityTypes.MEDIA_FILE],
|
||||
maybe_scoped_weight_type: [
|
||||
ScopedWeightTypes.COMFY_UI,
|
||||
ScopedWeightTypes.SDXL,
|
||||
],
|
||||
maybe_scoped_media_file_type: [
|
||||
ScopedMediaFileType.IMAGE_GENERATION,
|
||||
ScopedMediaFileType.TEXT_TO_SPEECH,
|
||||
ScopedMediaFileType.WORKFLOW_CONFIG,
|
||||
],
|
||||
});
|
||||
expect(api.fetch).toHaveBeenCalledWith(
|
||||
"http://localhost:3000/v1/user_bookmarks/list/user1",
|
||||
{
|
||||
method: "GET",
|
||||
query: {
|
||||
maybe_scoped_entity_type: "media_file",
|
||||
maybe_scoped_media_file_type:
|
||||
"image_generation,text_to_speech,workflow_config",
|
||||
maybe_scoped_weight_type: "comfy_ui,sdxl",
|
||||
page_index: 2,
|
||||
page_size: 76,
|
||||
sort_ascending: true,
|
||||
},
|
||||
},
|
||||
);
|
||||
expect(response).toEqual({
|
||||
success: true,
|
||||
data: [mockBookmark],
|
||||
pagination: {
|
||||
current: 0,
|
||||
total_page_count: 0,
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
it("exception", async () => {
|
||||
const api = new UserBookmarksApi();
|
||||
jest.spyOn(api, "fetch").mockRejectedValue(new Error("server error"));
|
||||
const response = await api.ListUserBookmarksByUser({});
|
||||
expect(api.fetch).toHaveBeenCalledWith(
|
||||
"http://localhost:3000/v1/user_bookmarks/list/un1",
|
||||
{
|
||||
method: "GET",
|
||||
query: {
|
||||
maybe_scoped_entity_type: undefined,
|
||||
maybe_scoped_media_file_type: undefined,
|
||||
maybe_scoped_weight_type: undefined,
|
||||
page_index: undefined,
|
||||
page_size: undefined,
|
||||
sort_ascending: undefined,
|
||||
},
|
||||
},
|
||||
);
|
||||
expect(response).toEqual({
|
||||
success: false,
|
||||
errorMessage: "server error",
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("ListUserBookmarksByEntity", () => {
|
||||
it("success", async () => {
|
||||
const api = new UserBookmarksApi();
|
||||
jest.spyOn(api, "fetch").mockResolvedValue({
|
||||
success: true,
|
||||
user_bookmarks: [
|
||||
{
|
||||
created_at: "2024-06-15T11:34:56.678Z",
|
||||
token: "string",
|
||||
updated_at: "2024-06-15T11:34:56.678Z",
|
||||
user: {
|
||||
default_avatar: {
|
||||
color_index: 0,
|
||||
image_index: 0,
|
||||
},
|
||||
display_name: "string",
|
||||
gravatar_hash: "string",
|
||||
user_token: "string",
|
||||
username: "string",
|
||||
},
|
||||
},
|
||||
],
|
||||
});
|
||||
const response = await api.ListUserBookmarksByEntity({
|
||||
entityType: "et1",
|
||||
entityToken: "tok1",
|
||||
});
|
||||
expect(api.fetch).toHaveBeenCalledWith(
|
||||
"http://localhost:3000/v1/user_bookmarks/list/et1/tok1",
|
||||
{
|
||||
method: "GET",
|
||||
query: undefined,
|
||||
},
|
||||
);
|
||||
expect(response).toEqual({
|
||||
success: true,
|
||||
data: [
|
||||
{
|
||||
created_at: "2024-06-15T11:34:56.678Z",
|
||||
token: "string",
|
||||
updated_at: "2024-06-15T11:34:56.678Z",
|
||||
user: {
|
||||
default_avatar: {
|
||||
color_index: 0,
|
||||
image_index: 0,
|
||||
},
|
||||
display_name: "string",
|
||||
gravatar_hash: "string",
|
||||
user_token: "string",
|
||||
username: "string",
|
||||
},
|
||||
},
|
||||
],
|
||||
});
|
||||
});
|
||||
|
||||
it("exception", async () => {
|
||||
const api = new UserBookmarksApi();
|
||||
jest.spyOn(api, "fetch").mockRejectedValue(new Error("server error"));
|
||||
const response = await api.ListUserBookmarksByEntity({
|
||||
entityType: "et1",
|
||||
entityToken: "tok1",
|
||||
});
|
||||
expect(api.fetch).toHaveBeenCalledWith(
|
||||
"http://localhost:3000/v1/user_bookmarks/list/et1/tok1",
|
||||
{
|
||||
method: "GET",
|
||||
query: undefined,
|
||||
},
|
||||
);
|
||||
expect(response).toEqual({
|
||||
success: false,
|
||||
errorMessage: "server error",
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,52 +0,0 @@
|
||||
import { authentication } from "~/signals";
|
||||
import { UserInfo } from "~/models";
|
||||
import EnvironmentVariables from "~/Classes/EnvironmentVariables";
|
||||
import testUpdateDeleteEndpoints from "./utils/testUpdateDeleteEndpoints";
|
||||
import { VoiceConversionApi } from "~/Classes/ApiManager/VoiceConversionApi";
|
||||
|
||||
EnvironmentVariables.initialize({ BASE_API: "http://localhost:3000" });
|
||||
|
||||
describe("VoiceConversionApi", () => {
|
||||
beforeAll(() => {
|
||||
authentication.userInfo.value = {
|
||||
user_token: "un1",
|
||||
username: "un1",
|
||||
} as UserInfo;
|
||||
});
|
||||
describe("run tests", () => {
|
||||
const api = new VoiceConversionApi();
|
||||
[
|
||||
{
|
||||
name: "PostAnalytics",
|
||||
function: api.ConvertVoice.bind(api),
|
||||
endpoint: "http://localhost:3000/v1/voice_conversion/inference",
|
||||
method: "POST",
|
||||
hasFailure: true,
|
||||
paramsIn: {
|
||||
autoPredictF0: true,
|
||||
creatorSetVisibility: "public",
|
||||
isStorytellerDemo: true,
|
||||
overrideF0Method: "rmvpe",
|
||||
sourceMediaUploadToken: "sourceToken123",
|
||||
transpose: 2,
|
||||
uuidIdempotencyToken: "uuid-1234-5678",
|
||||
voiceConversionModelToken: "voiceModelToken123",
|
||||
},
|
||||
paramsTest: {
|
||||
auto_predict_f0: true,
|
||||
creator_set_visibility: "public",
|
||||
is_storyteller_demo: true,
|
||||
override_f0_method: "rmvpe",
|
||||
source_media_upload_token: "sourceToken123",
|
||||
transpose: 2,
|
||||
uuid_idempotency_token: "uuid-1234-5678",
|
||||
voice_conversion_model_token: "voiceModelToken123",
|
||||
},
|
||||
response: { inference_job_token: "ijt1" },
|
||||
data: "ijt1",
|
||||
},
|
||||
].forEach((testMethod) => {
|
||||
testUpdateDeleteEndpoints(api, testMethod);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -1,93 +0,0 @@
|
||||
import { authentication } from "~/signals";
|
||||
import { UserInfo } from "~/models";
|
||||
import EnvironmentVariables from "~/Classes/EnvironmentVariables";
|
||||
import testUpdateDeleteEndpoints from "./utils/testUpdateDeleteEndpoints";
|
||||
import { VoiceDesignerApi } from "~/Classes/ApiManager/VoiceDesignerApi";
|
||||
import testListEndpoints from "./utils/testListEndpoints";
|
||||
|
||||
const mockDataset = {
|
||||
created_at: "2024-06-18T11:11:03.533Z",
|
||||
creator: {
|
||||
default_avatar: {
|
||||
color_index: 3,
|
||||
image_index: 1,
|
||||
},
|
||||
display_name: "JaneDoe",
|
||||
gravatar_hash: "b1c967db4babc4eebc51abfa7a3e74d5",
|
||||
user_token: "user-token-9876",
|
||||
username: "janedoe",
|
||||
},
|
||||
creator_set_visibility: "private",
|
||||
dataset_token: "dataset-token-456",
|
||||
ietf_language_tag: "fr-FR",
|
||||
ietf_primary_language_subtag: "fr",
|
||||
title: "French Language Dataset",
|
||||
updated_at: "2024-06-18T12:15:30.000Z",
|
||||
};
|
||||
|
||||
EnvironmentVariables.initialize({ BASE_API: "http://localhost:3000" });
|
||||
|
||||
describe("VoiceDesignerApi", () => {
|
||||
beforeAll(() => {
|
||||
authentication.userInfo.value = {
|
||||
user_token: "un1",
|
||||
username: "un1",
|
||||
} as UserInfo;
|
||||
});
|
||||
describe("run tests", () => {
|
||||
const api = new VoiceDesignerApi();
|
||||
[
|
||||
{
|
||||
name: "PostAnalytics",
|
||||
function: api.EnqueueTts.bind(api),
|
||||
endpoint: "http://localhost:3000/v1/voice_designer/enqueue_tts",
|
||||
method: "POST",
|
||||
hasFailure: true,
|
||||
paramsIn: {
|
||||
text: "Sample text 1",
|
||||
uuidIdempotencyToken: "uuid-1234-5678-abcdef",
|
||||
voiceToken: "voice-token-1",
|
||||
},
|
||||
paramsTest: {
|
||||
text: "Sample text 1",
|
||||
uuid_idempotency_token: "uuid-1234-5678-abcdef",
|
||||
voice_token: "voice-token-1",
|
||||
},
|
||||
response: { inference_job_token: "ijt1", voice_token: "vt1" },
|
||||
data: { inference_job_token: "ijt1", voice_token: "vt1" },
|
||||
},
|
||||
].forEach((testMethod) => {
|
||||
testUpdateDeleteEndpoints(api, testMethod);
|
||||
});
|
||||
});
|
||||
|
||||
describe("run list tests", () => {
|
||||
const api = new VoiceDesignerApi();
|
||||
[
|
||||
{
|
||||
name: "ListWeightsByUser",
|
||||
function: api.ListDatasetsByUser.bind(api),
|
||||
tests: [
|
||||
{
|
||||
name: "no user",
|
||||
endpoint: "http://localhost:3000/v1/voice_designer/user/un1/list",
|
||||
query: {},
|
||||
fetchQuery: undefined,
|
||||
response: { datasets: [mockDataset] },
|
||||
data: [mockDataset],
|
||||
},
|
||||
{
|
||||
name: "user",
|
||||
endpoint: "http://localhost:3000/v1/voice_designer/user/user1/list",
|
||||
query: { username: "user1" },
|
||||
fetchQuery: undefined,
|
||||
response: { datasets: [mockDataset] },
|
||||
data: [mockDataset],
|
||||
},
|
||||
],
|
||||
},
|
||||
].forEach((testMethod) => {
|
||||
testListEndpoints(api, testMethod);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -3,7 +3,6 @@ export * from "./lib/enums/Job.js";
|
||||
export * from "./lib/models/Users.js";
|
||||
export * from "./lib/models/Job.js";
|
||||
export * from "./lib/models/Prompts.js";
|
||||
export * from "./lib/AnalyticsApi.js";
|
||||
export * from "./lib/BillingApi.js";
|
||||
export * from "./lib/CreditsApi.js";
|
||||
export * from "./lib/GenerationApi.js";
|
||||
|
||||
@@ -1,38 +0,0 @@
|
||||
import { ApiManager, ApiResponse } from "./ApiManager.js";
|
||||
|
||||
export class AnalyticsApi extends ApiManager {
|
||||
public PostAnalytics({
|
||||
maybeLastAction,
|
||||
maybeLogToken,
|
||||
}: {
|
||||
maybeLastAction: string;
|
||||
maybeLogToken: string;
|
||||
}): Promise<ApiResponse<string>> {
|
||||
const endpoint = `${this.getApiSchemeAndHost()}/v1/analytics/log_session`;
|
||||
|
||||
const body = {
|
||||
maybe_last_action: maybeLastAction,
|
||||
maybe_log_token: maybeLogToken,
|
||||
};
|
||||
|
||||
return this.post<
|
||||
{
|
||||
maybe_last_action: string;
|
||||
maybe_log_token: string;
|
||||
},
|
||||
{
|
||||
success?: boolean;
|
||||
log_token?: string;
|
||||
BadInput?: string;
|
||||
}
|
||||
>({ endpoint, body })
|
||||
.then((response) => ({
|
||||
success: response.success ?? false,
|
||||
data: response.log_token,
|
||||
errorMessage: response.BadInput,
|
||||
}))
|
||||
.catch((err) => {
|
||||
return { success: false, errorMessage: err.message };
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -86,128 +86,4 @@ export class EngineApi extends ApiManager {
|
||||
return result;
|
||||
}
|
||||
|
||||
public async enqueueImageGeneration({
|
||||
disableSystemPrompt,
|
||||
prompt,
|
||||
snapshotMediaToken,
|
||||
additionalImages,
|
||||
}: {
|
||||
disableSystemPrompt: boolean;
|
||||
prompt: string;
|
||||
snapshotMediaToken: string;
|
||||
additionalImages?: string[];
|
||||
}): Promise<ApiResponse<string>> {
|
||||
const endpoint = `${this.getApiSchemeAndHost()}/v1/image_studio/prompt`;
|
||||
|
||||
const uuidIdempotencyToken = crypto.randomUUID(); // Generate a new UUID
|
||||
const body = {
|
||||
uuid_idempotency_token: uuidIdempotencyToken,
|
||||
disable_system_prompt: disableSystemPrompt,
|
||||
prompt,
|
||||
snapshot_media_token: snapshotMediaToken, // Changed from scene_media_token to snapshot_media_token
|
||||
additional_images: additionalImages,
|
||||
};
|
||||
|
||||
const postResponse = await this.post<
|
||||
{
|
||||
uuid_idempotency_token: string;
|
||||
disable_system_prompt: boolean;
|
||||
prompt: string;
|
||||
snapshot_media_token: string;
|
||||
additional_images?: string[];
|
||||
},
|
||||
{ success?: boolean; job_token?: string; BadInput?: string }
|
||||
>({
|
||||
endpoint,
|
||||
body,
|
||||
});
|
||||
|
||||
// Check if the response is successful
|
||||
const isSuccess = postResponse.success ?? false;
|
||||
|
||||
// Prepare the result object
|
||||
const result = {
|
||||
success: isSuccess,
|
||||
data: isSuccess ? postResponse.job_token : undefined,
|
||||
errorMessage: isSuccess ? undefined : postResponse.BadInput,
|
||||
};
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public async pollJobSession(jobToken: string): Promise<
|
||||
ApiResponse<{
|
||||
status: string;
|
||||
result: {
|
||||
generated_images?: string[];
|
||||
error?: string;
|
||||
};
|
||||
}>
|
||||
> {
|
||||
const endpoint = `${this.getApiSchemeAndHost()}/v1/jobs/session/${jobToken}`;
|
||||
|
||||
const response = await this.get<{
|
||||
success?: boolean;
|
||||
status?: string;
|
||||
result?: {
|
||||
generated_images?: string[];
|
||||
error?: string;
|
||||
};
|
||||
BadInput?: string;
|
||||
}>({
|
||||
endpoint,
|
||||
});
|
||||
|
||||
const success = response.success ?? false;
|
||||
const status = response.status ?? "";
|
||||
const result = response.result ?? {
|
||||
generated_images: [],
|
||||
error: undefined,
|
||||
};
|
||||
const errorMessage = response.BadInput;
|
||||
|
||||
return {
|
||||
success,
|
||||
data: { status, result },
|
||||
errorMessage,
|
||||
};
|
||||
}
|
||||
|
||||
public async pollStudioSessionJobs(jobToken: string): Promise<
|
||||
ApiResponse<{
|
||||
status: string;
|
||||
result: {
|
||||
generated_images?: string[];
|
||||
error?: string;
|
||||
};
|
||||
}>
|
||||
> {
|
||||
const endpoint = `${this.getApiSchemeAndHost()}/v1/image_studio/session_jobs/${jobToken}`;
|
||||
|
||||
const response = await this.get<{
|
||||
success?: boolean;
|
||||
status?: string;
|
||||
result?: {
|
||||
generated_images?: string[];
|
||||
error?: string;
|
||||
};
|
||||
BadInput?: string;
|
||||
}>({
|
||||
endpoint,
|
||||
});
|
||||
|
||||
const success = response.success ?? false;
|
||||
const status = response.status ?? "";
|
||||
const result = response.result ?? {
|
||||
generated_images: [],
|
||||
error: undefined,
|
||||
};
|
||||
const errorMessage = response.BadInput;
|
||||
|
||||
return {
|
||||
success,
|
||||
data: { status, result },
|
||||
errorMessage,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,57 +3,6 @@ import { Prompts } from "./models/Prompts.js";
|
||||
import { FetchProxy as fetch } from "@storyteller/tauri-utils";
|
||||
|
||||
export class PromptsApi extends ApiManager {
|
||||
public async enqueueImageGeneration({
|
||||
disableSystemPrompt,
|
||||
prompt,
|
||||
snapshotMediaToken,
|
||||
additionalImages,
|
||||
}: {
|
||||
disableSystemPrompt: boolean;
|
||||
prompt: string;
|
||||
snapshotMediaToken: string;
|
||||
additionalImages?: string[];
|
||||
}): Promise<ApiResponse<string>> {
|
||||
const endpoint = `${this.getApiSchemeAndHost()}/v1/image_studio/prompt`;
|
||||
|
||||
const uuidIdempotencyToken = crypto.randomUUID(); // Generate a new UUID
|
||||
const body = {
|
||||
uuid_idempotency_token: uuidIdempotencyToken,
|
||||
disable_system_prompt: disableSystemPrompt,
|
||||
prompt,
|
||||
snapshot_media_token: snapshotMediaToken, // Changed from scene_media_token to snapshot_media_token
|
||||
additional_images: additionalImages,
|
||||
};
|
||||
|
||||
const postResponse = await this.post<
|
||||
{
|
||||
uuid_idempotency_token: string;
|
||||
disable_system_prompt: boolean;
|
||||
prompt: string;
|
||||
snapshot_media_token: string;
|
||||
additional_images?: string[];
|
||||
},
|
||||
{ success?: boolean; job_token?: string; BadInput?: string }
|
||||
>({
|
||||
endpoint,
|
||||
body,
|
||||
});
|
||||
|
||||
// Check if the response is successful
|
||||
const isSuccess = postResponse.success ?? false;
|
||||
// Prepare the result object
|
||||
console.log("postResponse FOR ENQUEUE IMAGE GENERATION");
|
||||
console.log(postResponse);
|
||||
|
||||
const result = {
|
||||
success: isSuccess,
|
||||
data: isSuccess ? postResponse.job_token : undefined,
|
||||
errorMessage: isSuccess ? undefined : postResponse.BadInput,
|
||||
};
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public async pollJobSession(
|
||||
jobToken: string,
|
||||
thumbnailWidth = 256
|
||||
@@ -130,44 +79,6 @@ export class PromptsApi extends ApiManager {
|
||||
};
|
||||
}
|
||||
|
||||
public async pollStudioSessionJobs(jobToken: string): Promise<
|
||||
ApiResponse<{
|
||||
status: string;
|
||||
result: {
|
||||
generated_images?: string[];
|
||||
error?: string;
|
||||
};
|
||||
}>
|
||||
> {
|
||||
const endpoint = `${this.getApiSchemeAndHost()}/v1/image_studio/session_jobs/${jobToken}`;
|
||||
|
||||
const response = await this.get<{
|
||||
success?: boolean;
|
||||
status?: string;
|
||||
result?: {
|
||||
generated_images?: string[];
|
||||
error?: string;
|
||||
};
|
||||
BadInput?: string;
|
||||
}>({
|
||||
endpoint,
|
||||
});
|
||||
|
||||
const success = response.success ?? false;
|
||||
const status = response.status ?? "";
|
||||
const result = response.result ?? {
|
||||
generated_images: [],
|
||||
error: undefined,
|
||||
};
|
||||
const errorMessage = response.BadInput;
|
||||
|
||||
return {
|
||||
success,
|
||||
data: { status, result },
|
||||
errorMessage,
|
||||
};
|
||||
}
|
||||
|
||||
public GetPromptsByToken({
|
||||
token,
|
||||
}: {
|
||||
|
||||
@@ -1,52 +1,5 @@
|
||||
import { ApiManager, ApiResponse } from "./ApiManager.js";
|
||||
import {
|
||||
UserBookmarkBatch,
|
||||
UserBookmarkByEntity,
|
||||
UserBookmarkByUser,
|
||||
} from "./models/UserBookmark.js";
|
||||
import { Pagination } from "./models/Pagination.js";
|
||||
|
||||
export enum ScopedEntityTypes {
|
||||
USER = "user",
|
||||
TTS_MODEL = "tts-model",
|
||||
TTS_RESULT = "tts-result",
|
||||
W2L_TEMPLATE = "w2l-template",
|
||||
W2L_RESULT = "w2l-result",
|
||||
MEDIA_FILE = "media_file",
|
||||
MODEL_WEIGHT = "model_weight",
|
||||
VOICE_CONVERSION_MODEL = "voice_conversion_model",
|
||||
ZS_VOICE = "z_voice",
|
||||
}
|
||||
|
||||
export enum ScopedWeightTypes {
|
||||
HIFIGAN_TTl2 = "hifigan_tt2",
|
||||
RVC_V2 = "rvc_v2",
|
||||
SD_1_5 = "sd_1.5",
|
||||
SDXL = "sdxl",
|
||||
SO_VITS_SVC = "so_vits_svc",
|
||||
TT2 = "tt2",
|
||||
LORA = "loRA",
|
||||
VALL_E = "vall_e",
|
||||
COMFY_UI = "comfy_ui",
|
||||
}
|
||||
|
||||
export enum ScopedMediaFileType {
|
||||
IMAGE_GENERATION = "image_generation",
|
||||
TEXT_TO_SPEECH = "text_to_speech",
|
||||
VOCODER = "vocoder",
|
||||
VOICE_CONVERSION = "voice_conversion",
|
||||
WORKFLOW_CONFIG = "workflow_config",
|
||||
}
|
||||
|
||||
interface ListUserBookmarksByUserRequest {
|
||||
username: string;
|
||||
sort_ascending?: boolean;
|
||||
page_size?: number;
|
||||
page_index?: number;
|
||||
maybe_scoped_entity_type?: ScopedEntityTypes[];
|
||||
maybe_scoped_weight_type?: ScopedWeightTypes[];
|
||||
maybe_scoped_media_file_type?: ScopedMediaFileType[];
|
||||
}
|
||||
import { UserBookmarkBatch } from "./models/UserBookmark.js";
|
||||
|
||||
export class UserBookmarksApi extends ApiManager {
|
||||
public CreateUserBookmark({
|
||||
@@ -133,69 +86,4 @@ export class UserBookmarksApi extends ApiManager {
|
||||
});
|
||||
}
|
||||
|
||||
public ListUserBookmarksByUser({
|
||||
username,
|
||||
sort_ascending,
|
||||
page_size,
|
||||
page_index,
|
||||
maybe_scoped_entity_type,
|
||||
maybe_scoped_weight_type,
|
||||
maybe_scoped_media_file_type,
|
||||
}: ListUserBookmarksByUserRequest): Promise<
|
||||
ApiResponse<UserBookmarkByUser[], Pagination>
|
||||
> {
|
||||
const user = username;
|
||||
const endpoint = `${this.getApiSchemeAndHost()}/v1/user_bookmarks/list/${user}`;
|
||||
|
||||
const query = {
|
||||
sort_ascending,
|
||||
page_size,
|
||||
page_index,
|
||||
maybe_scoped_entity_type: maybe_scoped_entity_type
|
||||
? maybe_scoped_entity_type.join(",")
|
||||
: undefined,
|
||||
maybe_scoped_weight_type: maybe_scoped_weight_type
|
||||
? maybe_scoped_weight_type.join(",")
|
||||
: undefined,
|
||||
maybe_scoped_media_file_type: maybe_scoped_media_file_type
|
||||
? maybe_scoped_media_file_type.join(",")
|
||||
: undefined,
|
||||
};
|
||||
|
||||
return this.get<{
|
||||
success?: boolean;
|
||||
pagination?: Pagination;
|
||||
results: UserBookmarkByUser[];
|
||||
}>({ endpoint, query })
|
||||
.then((response) => ({
|
||||
success: response.success ?? false,
|
||||
data: response.results,
|
||||
pagination: response.pagination,
|
||||
}))
|
||||
.catch((err) => {
|
||||
return { success: false, errorMessage: err.message };
|
||||
});
|
||||
}
|
||||
|
||||
public ListUserBookmarksByEntity({
|
||||
entityType,
|
||||
entityToken,
|
||||
}: {
|
||||
entityType: string;
|
||||
entityToken: string;
|
||||
}): Promise<ApiResponse<UserBookmarkByEntity[]>> {
|
||||
const endpoint = `${this.getApiSchemeAndHost()}/v1/user_bookmarks/list/${entityType}/${entityToken}`;
|
||||
|
||||
return this.get<{
|
||||
success: boolean;
|
||||
user_bookmarks: UserBookmarkByEntity[];
|
||||
}>({ endpoint })
|
||||
.then((response) => ({
|
||||
success: true,
|
||||
data: response.user_bookmarks,
|
||||
}))
|
||||
.catch((err) => {
|
||||
return { success: false, errorMessage: err.message };
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,53 +0,0 @@
|
||||
import { ApiManager, ApiResponse } from "./ApiManager.js";
|
||||
|
||||
interface VoiceConversionParams {
|
||||
autoPredictF0?: boolean;
|
||||
creatorSetVisibility?: string;
|
||||
isStorytellerDemo?: boolean;
|
||||
overrideF0Method?: string;
|
||||
sourceMediaUploadToken: string;
|
||||
transpose?: number;
|
||||
uuidIdempotencyToken: string;
|
||||
voiceConversionModelToken: string;
|
||||
}
|
||||
|
||||
interface VoiceConversionRequest {
|
||||
auto_predict_f0: boolean;
|
||||
creator_set_visibility: string;
|
||||
is_storyteller_demo: boolean;
|
||||
override_f0_method: string;
|
||||
source_media_upload_token: string;
|
||||
transpose: number;
|
||||
uuid_idempotency_token: string;
|
||||
voice_conversion_model_token: string;
|
||||
}
|
||||
|
||||
export class VoiceConversionApi extends ApiManager {
|
||||
public ConvertVoice(
|
||||
params: VoiceConversionParams,
|
||||
): Promise<ApiResponse<string>> {
|
||||
const endpoint = `${this.getApiSchemeAndHost()}/v1/voice_conversion/inference`;
|
||||
|
||||
const body = this.parseBodyValues<
|
||||
VoiceConversionParams,
|
||||
VoiceConversionRequest
|
||||
>(params);
|
||||
|
||||
return this.post<
|
||||
VoiceConversionRequest,
|
||||
{
|
||||
success?: boolean;
|
||||
inference_job_token?: string;
|
||||
BadInput?: string;
|
||||
}
|
||||
>({ endpoint, body })
|
||||
.then((response) => ({
|
||||
success: response.success ?? false,
|
||||
data: response.inference_job_token,
|
||||
errorMessage: response.BadInput,
|
||||
}))
|
||||
.catch((err) => {
|
||||
return { success: false, errorMessage: err.message };
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -1,77 +0,0 @@
|
||||
import { ApiManager, ApiResponse } from "./ApiManager.js";
|
||||
import { Pagination } from "./models/Pagination.js";
|
||||
import { ZsDataset } from "./models/Dataset.js";
|
||||
|
||||
interface VoiceDesignerParams {
|
||||
text: string;
|
||||
uuidIdempotencyToken: string;
|
||||
voiceToken: string;
|
||||
}
|
||||
|
||||
interface VoiceDesignerRequest {
|
||||
text: string;
|
||||
uuid_idempotency_token: string;
|
||||
voice_token: string;
|
||||
}
|
||||
|
||||
export class VoiceDesignerApi extends ApiManager {
|
||||
public EnqueueTts(
|
||||
params: VoiceDesignerParams,
|
||||
): Promise<
|
||||
ApiResponse<{ inference_job_token?: string; voice_token?: string }>
|
||||
> {
|
||||
const endpoint = `${this.getApiSchemeAndHost()}/v1/voice_designer/enqueue_tts`;
|
||||
|
||||
const body = this.parseBodyValues<
|
||||
VoiceDesignerParams,
|
||||
VoiceDesignerRequest
|
||||
>(params);
|
||||
|
||||
return this.post<
|
||||
VoiceDesignerRequest,
|
||||
{
|
||||
success?: boolean;
|
||||
inference_job_token?: string;
|
||||
voice_token: string;
|
||||
BadInput?: string;
|
||||
}
|
||||
>({ endpoint, body })
|
||||
.then((response) => ({
|
||||
success: response.success ?? false,
|
||||
...(response.success
|
||||
? {
|
||||
data: {
|
||||
inference_job_token: response.inference_job_token,
|
||||
voice_token: response.voice_token,
|
||||
},
|
||||
}
|
||||
: {}),
|
||||
errorMessage: response.BadInput,
|
||||
}))
|
||||
.catch((err) => {
|
||||
return { success: false, errorMessage: err.message };
|
||||
});
|
||||
}
|
||||
|
||||
public ListDatasetsByUser({
|
||||
username,
|
||||
}: {
|
||||
username: string;
|
||||
}): Promise<ApiResponse<ZsDataset[]>> {
|
||||
const user = username;
|
||||
const endpoint = `${this.getApiSchemeAndHost()}/v1/voice_designer/user/${user}/list`;
|
||||
|
||||
return this.get<{
|
||||
success: boolean;
|
||||
datasets: ZsDataset[];
|
||||
pagination: Pagination;
|
||||
}>({ endpoint })
|
||||
.then((response) => ({
|
||||
success: true,
|
||||
data: response.datasets,
|
||||
}))
|
||||
.catch((err) => {
|
||||
return { success: false, errorMessage: err.message };
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -274,7 +274,7 @@ export class WeightsApi extends ApiManager {
|
||||
weightToken: string;
|
||||
coverImageMediaFileToken: string;
|
||||
}): Promise<ApiResponse<undefined>> {
|
||||
const endpoint = `${this.getApiSchemeAndHost()}/v1/weights/${weightToken}/cover_image`;
|
||||
const endpoint = `${this.getApiSchemeAndHost()}/v1/weights/weight/${weightToken}/cover_image`;
|
||||
|
||||
const body = {
|
||||
cover_image_media_file_token: coverImageMediaFileToken,
|
||||
|
||||
@@ -4,72 +4,3 @@ export interface UserBookmarkBatch {
|
||||
is_bookmarked: boolean;
|
||||
maybe_bookmark_token: string;
|
||||
}
|
||||
|
||||
interface DefaultAvatar {
|
||||
color_index: number;
|
||||
image_index: number;
|
||||
}
|
||||
|
||||
interface User {
|
||||
default_avatar: DefaultAvatar;
|
||||
display_name: string;
|
||||
gravatar_hash: string;
|
||||
user_token: string;
|
||||
username: string;
|
||||
}
|
||||
|
||||
export interface UserBookmarkByEntity {
|
||||
created_at: string;
|
||||
token: string;
|
||||
updated_at: string;
|
||||
user: User;
|
||||
}
|
||||
|
||||
interface DefaultAvatar {
|
||||
color_index: number;
|
||||
image_index: number;
|
||||
}
|
||||
|
||||
interface Creator {
|
||||
default_avatar: DefaultAvatar;
|
||||
display_name: string;
|
||||
gravatar_hash: string;
|
||||
user_token: string;
|
||||
username: string;
|
||||
}
|
||||
|
||||
interface MediaFileData {
|
||||
maybe_creator: Creator;
|
||||
media_type: string; // Adjust type as per your specific schema
|
||||
public_bucket_path: string;
|
||||
}
|
||||
|
||||
interface WeightData {
|
||||
maybe_cover_image_public_bucket_path: string;
|
||||
maybe_creator: Creator;
|
||||
title: string;
|
||||
weight_category: string; // Adjust type as per your specific schema
|
||||
weight_type: string; // Adjust type as per your specific schema
|
||||
}
|
||||
|
||||
interface Stats {
|
||||
bookmark_count: number;
|
||||
positive_rating_count: number;
|
||||
}
|
||||
|
||||
interface Details {
|
||||
entity_token: string;
|
||||
entity_type: string; // Adjust type as per your specific schema
|
||||
maybe_media_file_data?: MediaFileData;
|
||||
maybe_summary_text?: string;
|
||||
maybe_thumbnail_url?: string;
|
||||
maybe_weight_data?: WeightData;
|
||||
stats: Stats;
|
||||
}
|
||||
|
||||
export interface UserBookmarkByUser {
|
||||
created_at: string;
|
||||
details: Details;
|
||||
token: string;
|
||||
updated_at: string;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user