@bili-agent-kit/core 0.2.0 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.mjs CHANGED
@@ -398,7 +398,6 @@ const RISK_DEFAULTS = {
398
398
  readDaily: 3e3,
399
399
  writeHourly: 12,
400
400
  writeDaily: 30,
401
- writeGapMs: 12e4,
402
401
  dmGapMs: 6e4
403
402
  };
404
403
  function checkPaused(state, account, now) {
@@ -484,7 +483,7 @@ var Traffic = class {
484
483
  a.writeDay = 0;
485
484
  }
486
485
  if (writing) {
487
- const gap = url.pathname.endsWith("/send_msg") ? RISK_DEFAULTS.dmGapMs : RISK_DEFAULTS.writeGapMs;
486
+ const gap = url.pathname.endsWith("/send_msg") ? RISK_DEFAULTS.dmGapMs : 0;
488
487
  const sameGroup = !!bodyOptions?.writeGroup && a.lastWriteGroup === bodyOptions.writeGroup;
489
488
  if (!sameGroup && a.writeHour >= RISK_DEFAULTS.writeHourly || !sameGroup && a.writeDay >= RISK_DEFAULTS.writeDaily || now - a.lastWrite < gap && (!bodyOptions?.writeGroup || a.lastWriteGroup !== bodyOptions.writeGroup)) throw new KitError("RATE_LIMITED", "Write budget or cooldown reached.");
490
489
  if (!sameGroup) {
@@ -976,7 +975,10 @@ var BiliClient = class {
976
975
  img_url: z.string(),
977
976
  sub_url: z.string()
978
977
  }), nav.wbi_img);
979
- url.search = signWbi({ csrf: session.credentials?.bili_jct ?? "" }, images, this.traffic.clock.now());
978
+ url.search = signWbi({
979
+ ...Object.fromEntries(url.searchParams),
980
+ csrf: session.credentials?.bili_jct ?? ""
981
+ }, images, this.traffic.clock.now());
980
982
  }
981
983
  const form = body instanceof FormData;
982
984
  return scrub(await this.traffic.request(url, {
@@ -1547,23 +1549,29 @@ async function executeExtended(client, action, s, draftId, step, existingPics, r
1547
1549
  });
1548
1550
  }
1549
1551
  const dyn_req = {
1550
- scene: pics.length ? 2 : 1,
1552
+ scene: 2,
1551
1553
  content: { contents: [{
1552
1554
  raw_text: action.text,
1553
1555
  type: 1,
1554
1556
  biz_id: ""
1555
1557
  }] },
1556
- pics,
1558
+ ...pics.length ? { pics } : {},
1557
1559
  meta: { app_meta: {
1558
1560
  from: "create.dynamic.web",
1559
1561
  mobi_app: "web"
1560
1562
  } }
1561
1563
  };
1562
- return step("send", () => action.kind === "dynamic.create" ? client.postBody("/x/dynamic/feed/create/dyn", { dyn_req }, s, draftId, true) : client.postBody("/x/dynamic/feed/edit/dyn?platform=web", {
1564
+ const editing = action.kind === "dynamic.edit";
1565
+ if (editing) await step("submit_check", () => client.postBody(`/x/dynamic/feed/create/submit_check?csrf=${encodeURIComponent(csrf)}`, {
1563
1566
  dyn_id_str: action.dynamicId,
1564
- dyn_req,
1565
- csrf
1567
+ content: dyn_req.content,
1568
+ scene: dyn_req.scene,
1569
+ ...pics.length ? { pics } : {}
1566
1570
  }, s, draftId));
1571
+ return step("send", () => client.postBody(`/x/dynamic/feed/${editing ? "edit" : "create"}/dyn?platform=web`, {
1572
+ dyn_req,
1573
+ ...editing ? { dyn_id_str: action.dynamicId } : {}
1574
+ }, s, draftId, true));
1567
1575
  }
1568
1576
  }
1569
1577
  }
@@ -2326,8 +2334,10 @@ var ContentReads = class {
2326
2334
  });
2327
2335
  case "user_dynamics": return get("/x/polymer/web-dynamic/v1/feed/space", {
2328
2336
  host_mid: uid,
2329
- offset: String(a.offset)
2330
- });
2337
+ offset: String(a.offset),
2338
+ platform: "web",
2339
+ timezone_offset: new Date(this.client.traffic.clock.now()).getTimezoneOffset()
2340
+ }, true);
2331
2341
  case "dynamic": return get("/x/polymer/web-dynamic/v1/detail", { id: String(a.dynamicId) });
2332
2342
  case "articles": return get("/x/space/article", {
2333
2343
  mid: uid,
@@ -2567,6 +2577,26 @@ function cdnUrl(value) {
2567
2577
  ].some((host) => url.hostname === host || url.hostname.endsWith(`.${host}`))) throw new KitError("FORBIDDEN", "Media URL is outside the allowed Bilibili CDN.");
2568
2578
  return url;
2569
2579
  }
2580
+ function streamUrl(stream) {
2581
+ const s = object(stream);
2582
+ const candidates = [
2583
+ s.baseUrl,
2584
+ s.base_url,
2585
+ s.url,
2586
+ ...Array.isArray(s.backupUrl) ? s.backupUrl : [],
2587
+ ...Array.isArray(s.backup_url) ? s.backup_url : []
2588
+ ];
2589
+ let firstError;
2590
+ for (const candidate of candidates) {
2591
+ if (typeof candidate !== "string") continue;
2592
+ try {
2593
+ return cdnUrl(candidate).href;
2594
+ } catch (error) {
2595
+ firstError ??= error;
2596
+ }
2597
+ }
2598
+ throw firstError ?? new KitError("UPSTREAM", "Missing stream URL.");
2599
+ }
2570
2600
  async function mediaImages(root, files) {
2571
2601
  const images = [];
2572
2602
  for (const file of files.slice(0, 12)) {
@@ -2659,9 +2689,7 @@ var Media = class {
2659
2689
  const limit = options.maxBytes ?? 134217728;
2660
2690
  let bytes = 0;
2661
2691
  const save = async (stream, name) => {
2662
- const s = object(stream), url = s.baseUrl ?? s.base_url ?? s.url;
2663
- if (typeof url !== "string") throw new KitError("UPSTREAM", "Missing stream URL.");
2664
- const body = await this.fetchBytes(url, limit - bytes);
2692
+ const body = await this.fetchBytes(streamUrl(stream), limit - bytes);
2665
2693
  bytes += body.length;
2666
2694
  const file = join(dir, name);
2667
2695
  await writeFile(file, body, {