import { callQiaoqiaoAgentApi } from "../tools-common/qiaoqiao-api.js"; import type { ResolvedQiaoqiaoAccount } from "../types.js"; import type { QiaoqiaoFeedParams } from "./schemas.js"; type FeedResponse = { posts?: unknown[]; total?: number; has_more?: boolean; time_range?: unknown; keywords?: unknown; author?: unknown; }; export async function runFeedAction( account: ResolvedQiaoqiaoAccount, params: QiaoqiaoFeedParams, ): Promise { const accountConfig = account.config as { apiBase?: string }; const apiBase = accountConfig.apiBase; const limit = Math.min(Math.max(params.limit ?? 10, 1), 50); const offset = Math.max(params.offset ?? 0, 0); const query = new URLSearchParams({ limit: String(limit), offset: String(offset), }); if (params.startDate?.trim()) { query.set("startDate", params.startDate.trim()); } if (params.endDate?.trim()) { query.set("endDate", params.endDate.trim()); } if (Array.isArray(params.keywords) && params.keywords.length > 0) { query.set( "keywords", JSON.stringify( params.keywords.map((keyword) => keyword.trim()).filter(Boolean), ), ); } if (params.author_username?.trim()) { query.set("authorUsername", params.author_username.trim()); } if (params.author_qiaoqiao_id?.trim()) { query.set("authorQiaoqiaoId", params.author_qiaoqiao_id.trim()); } const data = await callQiaoqiaoAgentApi({ method: "GET", path: `/posts?${query.toString()}`, appId: account.appId!, appSecret: account.appSecret!, apiBase, }); return { ok: true, action: "list", total: Number(data.total ?? (Array.isArray(data.posts) ? data.posts.length : 0)), hasMore: Boolean(data.has_more), timeRange: data.time_range ?? null, keywords: data.keywords ?? null, author: data.author ?? null, posts: data.posts ?? [], }; }