Skip to content

ZJU-REAL/Easel / skills/builtin/skill-wechat-publisher

skill-wechat-publisher

Verified

微信公众号文章自动创作与发布工具。给定参考文章、文字或文档,自动搜索整理全网相关信息、生成图文并茂的公众号文章,并发布到微信公众号草稿箱。特别强调反 AI 检测写作。 触发场景(沾边就用):用户提到"公众号 / 微信文章 / 推文 / 公号 / 发文 / mp / 草稿箱 / 群发",或要求把话题/文档/笔记写成公众号文章并发布到微信。

FileKindSize
EASEL-META.mdmarkdown2.6 KB
SKILL.mdskill md11.4 KB
assets/image-styles/README.mdmarkdown29.5 KB
assets/image-styles/blue.jsonconfig3.0 KB
assets/image-styles/data-chart.jsonconfig2.4 KB
assets/image-styles/flat-editorial.jsonconfig4.9 KB
assets/image-styles/hand-drawn-blue.jsonconfig2.3 KB
assets/image-styles/illustrated-warm.jsonconfig3.3 KB
assets/image-styles/infographic-blue.jsonconfig5.2 KB
assets/image-styles/infographic-dark.jsonconfig5.4 KB
assets/image-styles/infographic-mint.jsonconfig5.2 KB
assets/image-styles/infographic-warm.jsonconfig5.4 KB
assets/image-styles/knowledge-card.jsonconfig2.3 KB
assets/image-styles/magazine-editorial.jsonconfig2.3 KB
assets/image-styles/marker-coral.jsonconfig6.4 KB
assets/image-styles/marker-lime.jsonconfig6.6 KB
assets/image-styles/marker-pink.jsonconfig6.4 KB
assets/image-styles/marker-sky.jsonconfig6.4 KB
assets/image-styles/marker-violet.jsonconfig6.4 KB
assets/image-styles/meme-illustration.jsonconfig2.3 KB
assets/image-styles/mint.jsonconfig3.1 KB
assets/image-styles/morandi.jsonconfig3.4 KB
assets/image-styles/previews/blue.webpbinary104.0 KB
assets/image-styles/previews/data-chart.webpbinary16.4 KB
assets/image-styles/previews/flat-editorial.webpbinary109.7 KB
assets/image-styles/previews/hand-drawn-blue.webpbinary17.9 KB
assets/image-styles/previews/illustrated-warm.webpbinary31.2 KB
assets/image-styles/previews/infographic-blue.webpbinary160.4 KB
assets/image-styles/previews/infographic-dark.webpbinary142.1 KB
assets/image-styles/previews/infographic-mint.webpbinary150.2 KB
assets/image-styles/previews/infographic-warm.webpbinary162.5 KB
assets/image-styles/previews/knowledge-card.webpbinary22.4 KB
assets/image-styles/previews/magazine-editorial.webpbinary14.0 KB
assets/image-styles/previews/marker-coral.webpbinary41.2 KB
assets/image-styles/previews/marker-lime-template.webpbinary34.3 KB
assets/image-styles/previews/marker-lime.webpbinary39.3 KB
assets/image-styles/previews/marker-pink.webpbinary37.5 KB
assets/image-styles/previews/marker-sky.webpbinary47.9 KB
assets/image-styles/previews/marker-violet.webpbinary42.8 KB
assets/image-styles/previews/meme-illustration.webpbinary35.5 KB
assets/image-styles/previews/mint.webpbinary95.7 KB
assets/image-styles/previews/morandi.webpbinary82.0 KB
assets/image-styles/previews/slice-of-life.webpbinary46.3 KB
assets/image-styles/previews/warm-handdrawn.webpbinary79.0 KB
assets/image-styles/previews/xiaohongshu-colorful.webpbinary24.3 KB
assets/image-styles/slice-of-life.jsonconfig4.9 KB
assets/image-styles/warm-handdrawn.jsonconfig4.4 KB
assets/image-styles/xiaohongshu-colorful.jsonconfig2.3 KB
assets/theme-previews/README.mdmarkdown1.7 KB
assets/theme-previews/academic-paper.htmlother13.5 KB
assets/theme-previews/business-navy.htmlother13.6 KB
assets/theme-previews/elegant-ink.htmlother13.0 KB
assets/theme-previews/girly-pink.htmlother13.2 KB
assets/theme-previews/index.htmlother26.6 KB
assets/theme-previews/ink-wash.htmlother13.5 KB
assets/theme-previews/magazine-grid.htmlother13.0 KB
assets/theme-previews/minimal-bw.htmlother13.6 KB
assets/theme-previews/minimal-mono.htmlother12.7 KB
assets/theme-previews/mint-fresh.htmlother14.0 KB
assets/theme-previews/news-bold.htmlother13.3 KB
assets/theme-previews/refined-blue.htmlother15.1 KB
assets/theme-previews/sage-premium.htmlother13.6 KB
assets/theme-previews/sample.mdmarkdown2.6 KB
assets/theme-previews/screenshots/ink-wash.webpbinary27.8 KB
assets/theme-previews/screenshots/minimal-mono.webpbinary34.8 KB
assets/theme-previews/screenshots/news-bold.webpbinary32.5 KB
assets/theme-previews/screenshots/refined-blue.webpbinary34.3 KB
assets/theme-previews/screenshots/theme-overview.webpbinary229.8 KB
assets/theme-previews/sunset-coral.htmlother14.8 KB
assets/theme-previews/warm-editorial.htmlother12.8 KB
assets/theme-previews/warm-orange.htmlother12.3 KB
assets/themes/academic-paper.jsonconfig5.2 KB
assets/themes/business-navy.jsonconfig5.2 KB
assets/themes/elegant-ink.jsonconfig5.1 KB
assets/themes/girly-pink.jsonconfig5.3 KB
assets/themes/ink-wash.jsonconfig5.1 KB
assets/themes/magazine-grid.jsonconfig5.1 KB
assets/themes/minimal-bw.jsonconfig5.2 KB
assets/themes/minimal-mono.jsonconfig5.0 KB
assets/themes/mint-fresh.jsonconfig5.3 KB
assets/themes/news-bold.jsonconfig5.1 KB
assets/themes/refined-blue.jsonconfig5.6 KB
assets/themes/sage-premium.jsonconfig5.3 KB
assets/themes/sunset-coral.jsonconfig5.5 KB
assets/themes/warm-editorial.jsonconfig4.9 KB
assets/themes/warm-orange.jsonconfig5.0 KB
assets/typeset-card/template.htmlother3.5 KB
brief.md.exampleother2.3 KB
references/anti-ai-checklist.mdmarkdown3.4 KB
references/api_reference.mdmarkdown3.2 KB
references/article-structures.mdmarkdown3.2 KB
references/errors.mdmarkdown1.1 KB
references/image-styles-guide.mdmarkdown3.5 KB
references/inline-markup.mdmarkdown1.7 KB
references/multi-platform-sync.mdmarkdown2.6 KB
references/newspic-mode.mdmarkdown5.7 KB
references/themes.mdmarkdown1.8 KB
references/typeset-card.mdmarkdown4.3 KB
scripts/ai_score.pyexecutablescript15.2 KB
scripts/api.pyexecutablescript13.3 KB
scripts/baoyu_image_gen.tsexecutablescript5.5 KB
scripts/baoyu_image_gen_core.tsscript11.5 KB
scripts/config.pyexecutablescript11.7 KB
scripts/generate_image.pyexecutablescript4.3 KB
scripts/html_converter.pyexecutablescript23.3 KB
scripts/image_handler.pyexecutablescript17.5 KB
scripts/multi_publish.pyexecutablescript14.2 KB
scripts/newspic_build.pyexecutablescript14.0 KB
scripts/publish.pyexecutablescript35.7 KB
scripts/sync_pages.shexecutablescript994 B
scripts/wechat_api.pyexecutablescript6.0 KB
scripts/wechat_token.pyexecutablescript5.1 KB
tests/__init__.pyscript0 B
tests/conftest.pyscript4.1 KB
tests/test_ai_score.pyscript8.1 KB
tests/test_config.pyscript7.4 KB
tests/test_config_integration.pyscript9.7 KB
tests/test_draft_submission.pyscript3.1 KB
tests/test_html_converter.pyscript10.2 KB
wechat-publisher.yaml.exampleother1.8 KB

scripts/baoyu_image_gen_core.ts

View on GitHub
import path from "node:path";
import { existsSync } from "node:fs";
import { readFile } from "node:fs/promises";
import { fileURLToPath } from "node:url";

export type Provider = "openai" | "gemini-proxy";
export type Quality = "normal" | "2k";

export type CliArgs = {
  prompt: string | null;
  promptFiles: string[];
  imagePath: string | null;
  provider: Provider | null;
  model: string | null;
  aspectRatio: string | null;
  size: string | null;
  quality: Quality;
  referenceImages: string[];
  n: number;
  json: boolean;
  help: boolean;
};

type OpenAIImageResult = {
  data?: Array<{ url?: string; b64_json?: string }>;
};

type ChatCompletionResult = {
  choices?: Array<{
    message?: {
      content?: unknown;
      images?: Array<{ url?: string; b64_json?: string; b64?: string }>;
      image_url?: { url?: string };
    };
  }>;
  candidates?: Array<{
    content?: {
      parts?: Array<{
        inlineData?: { data?: string };
        text?: string;
      }>;
    };
  }>;
};

const SCRIPT_DIR = path.dirname(fileURLToPath(import.meta.url));
const SKILL_ROOT = path.resolve(SCRIPT_DIR, "..");
const CONFIG_PATH = path.join(SKILL_ROOT, "wechat-publisher.yaml");
const CONFIG_EXPORT_KEYS = [
  "WECHAT_PUBLISHER_IMAGE_GENERATOR",
  "OPENAI_API_KEY",
  "OPENAI_BASE_URL",
  "OPENAI_IMAGE_MODEL",
  "GEMINI_PROXY_API_KEY",
  "GEMINI_PROXY_BASE_URL",
  "GEMINI_PROXY_IMAGE_MODEL",
  "GEMINI_WEB_DATA_DIR",
  "GEMINI_WEB_COOKIE_PATH",
  "GEMINI_WEB_CHROME_PROFILE_DIR",
  "GEMINI_WEB_CHROME_PATH",
  "WECHATSYNC_MCP_TOKEN",
] as const;

function hasConfiguredProviderEnv(): boolean {
  return Boolean(process.env.GEMINI_PROXY_API_KEY || process.env.OPENAI_API_KEY);
}

function hasCanonicalConfigFile(): boolean {
  return existsSync(CONFIG_PATH);
}

function hasLoadedProviderEnv(
  loaded: Partial<Record<(typeof CONFIG_EXPORT_KEYS)[number], string | null>>
): boolean {
  return Boolean(loaded.OPENAI_API_KEY || loaded.GEMINI_PROXY_API_KEY);
}

export async function loadEnv(): Promise<void> {
  if (typeof Bun === "undefined") return;

  const python = `
import json
import os
import sys
from pathlib import Path

keys = ${JSON.stringify([...CONFIG_EXPORT_KEYS])}
sys.path.insert(0, str(Path.cwd() / "scripts"))

for key in keys:
    os.environ.pop(key, None)

import config  # noqa: E402

config.load_env()
print(json.dumps({key: os.environ.get(key) for key in keys}))
`.trim();

  const result = Bun.spawnSync({
    cmd: ["python3", "-c", python],
    cwd: SKILL_ROOT,
    env: process.env,
    stdout: "pipe",
    stderr: "pipe",
  });

  if (result.exitCode !== 0) {
    const stderr = Buffer.from(result.stderr).toString("utf8").trim();
    if (!hasCanonicalConfigFile() && hasConfiguredProviderEnv()) return;
    throw new Error(stderr || "Failed to load wechat-publisher.yaml via scripts/config.py");
  }

  const stderr = Buffer.from(result.stderr).toString("utf8").trim();
  if (stderr) {
    throw new Error(stderr);
  }

  const output = Buffer.from(result.stdout).toString("utf8").trim();
  if (!output) {
    if (!hasCanonicalConfigFile() && hasConfiguredProviderEnv()) return;
    throw new Error("wechat-publisher.yaml did not export any image provider settings");
  }

  try {
    const loaded = JSON.parse(output) as Partial<Record<(typeof CONFIG_EXPORT_KEYS)[number], string | null>>;
    const hasCanonicalConfig = hasCanonicalConfigFile();
    if (hasCanonicalConfig && !hasLoadedProviderEnv(loaded)) {
      throw new Error(
        "wechat-publisher.yaml does not configure OPENAI_API_KEY or GEMINI_PROXY_API_KEY"
      );
    }

    for (const key of CONFIG_EXPORT_KEYS) {
      const value = loaded[key];
      if (hasCanonicalConfig) {
        if (value) process.env[key] = value;
        else delete process.env[key];
        continue;
      }
      if (value && !process.env[key]) {
        process.env[key] = value;
      }
    }
    return;
  }
  catch (error) {
    if (!hasCanonicalConfigFile() && hasConfiguredProviderEnv()) return;
    throw new Error(
      error instanceof Error
        ? `Failed to parse config exports: ${error.message}`
        : "Failed to parse config exports"
    );
  }
}

export function detectProvider(args: CliArgs): Provider {
  if (args.provider) return args.provider;
  if (process.env.GEMINI_PROXY_API_KEY) return "gemini-proxy";
  if (process.env.OPENAI_API_KEY) return "openai";
  throw new Error(
    "No image provider configured. Set GEMINI_PROXY_API_KEY or OPENAI_API_KEY."
  );
}

export function getDefaultModel(provider: Provider): string {
  if (provider === "gemini-proxy") {
    return process.env.GEMINI_PROXY_IMAGE_MODEL || "gemini-2.5-flash";
  }
  return process.env.OPENAI_IMAGE_MODEL || "gpt-image-1";
}

export async function buildPrompt(args: CliArgs): Promise<string> {
  if (args.prompt) return args.prompt;
  if (args.promptFiles.length > 0) {
    const parts: string[] = [];
    for (const file of args.promptFiles) {
      parts.push(await readFile(file, "utf8"));
    }
    return parts.join("\n\n").trim();
  }

  if (!process.stdin.isTTY) {
    try {
      const stdin = await Bun.stdin.text();
      if (stdin.trim()) return stdin.trim();
    } catch {}
  }

  throw new Error("Prompt is required.");
}

export function normalizeOutputImagePath(filePath: string): string {
  const full = path.resolve(filePath);
  return path.extname(full) ? full : `${full}.png`;
}

export async function generateImage(
  provider: Provider,
  model: string,
  prompt: string,
  args: CliArgs
): Promise<Uint8Array> {
  if (provider === "gemini-proxy") {
    return generateWithGeminiProxy(prompt, model, args);
  }
  return generateWithOpenAI(prompt, model, args);
}

async function generateWithOpenAI(
  prompt: string,
  model: string,
  args: CliArgs
): Promise<Uint8Array> {
  const baseURL = (process.env.OPENAI_BASE_URL || "https://api.openai.com/v1").replace(/\/$/, "");
  const apiKey = process.env.OPENAI_API_KEY;
  if (!apiKey) throw new Error("OPENAI_API_KEY is required");

  const body: Record<string, unknown> = {
    model,
    prompt: applyPromptHints(prompt, args),
    size: args.size || getOpenAISize(args.aspectRatio, args.quality),
    n: args.n,
  };

  const res = await fetch(`${baseURL}/images/generations`, {
    method: "POST",
    headers: {
      "Content-Type": "application/json",
      Authorization: `Bearer ${apiKey}`,
    },
    body: JSON.stringify(body),
  });

  if (!res.ok) {
    throw new Error(`OpenAI image API error: ${await res.text()}`);
  }

  return extractImageBytes(await res.json() as OpenAIImageResult);
}

async function generateWithGeminiProxy(
  prompt: string,
  model: string,
  args: CliArgs
): Promise<Uint8Array> {
  const baseURL = normalizeGeminiProxyBaseUrl(process.env.GEMINI_PROXY_BASE_URL);
  const apiKey = process.env.GEMINI_PROXY_API_KEY;
  if (!baseURL) throw new Error("GEMINI_PROXY_BASE_URL is required");
  if (!apiKey) throw new Error("GEMINI_PROXY_API_KEY is required");

  const content: Array<Record<string, unknown>> = [];
  for (const file of args.referenceImages) {
    const { dataUrl } = await readImageAsDataUrl(file);
    content.push({ type: "image_url", image_url: { url: dataUrl } });
  }
  content.push({
    type: "text",
    text: buildGeminiProxyPrompt(prompt, args),
  });

  const body = {
    model,
    messages: [{ role: "user", content }],
  };

  const res = await fetch(`${baseURL}/chat/completions`, {
    method: "POST",
    headers: {
      "Content-Type": "application/json",
      Authorization: `Bearer ${apiKey}`,
    },
    body: JSON.stringify(body),
  });

  if (!res.ok) {
    throw new Error(`Gemini proxy API error: ${await res.text()}`);
  }

  return extractImageBytes(await res.json() as ChatCompletionResult);
}

function normalizeGeminiProxyBaseUrl(raw: string | undefined): string | null {
  const trimmed = raw?.trim();
  if (!trimmed) return null;
  const withoutSlash = trimmed.replace(/\/+$/, "");
  return withoutSlash.endsWith("/v1") ? withoutSlash : `${withoutSlash}/v1`;
}

function buildGeminiProxyPrompt(prompt: string, args: CliArgs): string {
  const parts = [
    "Generate exactly one image.",
    "Return image data directly if the proxy supports it.",
    "Preferred response order: base64 image, image URL, then nothing else.",
    applyPromptHints(prompt, args),
  ];
  return parts.join("\n");
}

function applyPromptHints(prompt: string, args: CliArgs): string {
  let result = prompt.trim();
  if (args.aspectRatio) result += `\nAspect ratio: ${args.aspectRatio}.`;
  if (args.quality === "2k") result += "\nHigh resolution target: 2048px.";
  if (args.size) result += `\nTarget size: ${args.size}.`;
  return result;
}

function getOpenAISize(aspectRatio: string | null, quality: Quality): string {
  const base = quality === "2k" ? 2048 : 1024;
  if (!aspectRatio) return `${base}x${base}`;
  if (aspectRatio === "16:9") return quality === "2k" ? "2048x1024" : "1792x1024";
  if (aspectRatio === "9:16") return quality === "2k" ? "1024x2048" : "1024x1792";
  return `${base}x${base}`;
}

async function readImageAsDataUrl(filePath: string): Promise<{ dataUrl: string }> {
  const bytes = await readFile(filePath);
  const ext = path.extname(filePath).toLowerCase();
  const mime =
    ext === ".jpg" || ext === ".jpeg" ? "image/jpeg" :
    ext === ".webp" ? "image/webp" :
    ext === ".gif" ? "image/gif" :
    "image/png";
  return { dataUrl: `data:${mime};base64,${bytes.toString("base64")}` };
}

function extractImageBytes(payload: unknown): Promise<Uint8Array> {
  const direct = extractInlineImage(payload);
  if (direct) return Promise.resolve(direct);

  const url = extractImageUrl(payload);
  if (url) return downloadImage(url);

  throw new Error("Cannot extract image data from response.");
}

function extractInlineImage(payload: unknown): Uint8Array | null {
  const dataUrl = findString(payload, (value) => value.startsWith("data:image/"));
  const base64Value = dataUrl ?? findString(payload, (value) => isLikelyImageBase64(value));
  if (!base64Value) return null;
  const raw = base64Value.startsWith("data:image/")
    ? base64Value.slice(base64Value.indexOf(",") + 1)
    : base64Value;
  return Uint8Array.from(Buffer.from(raw, "base64"));
}

function extractImageUrl(payload: unknown): string | null {
  return findString(payload, (value) =>
    value.startsWith("https://") &&
    (value.includes(".png") || value.includes(".jpg") || value.includes(".jpeg") || value.includes(".webp") || value.includes("image"))
  );
}

function findString(value: unknown, match: (text: string) => boolean): string | null {
  if (typeof value === "string") return match(value) ? value : null;
  if (Array.isArray(value)) {
    for (const item of value) {
      const found = findString(item, match);
      if (found) return found;
    }
    return null;
  }
  if (value && typeof value === "object") {
    for (const child of Object.values(value as Record<string, unknown>)) {
      const found = findString(child, match);
      if (found) return found;
    }
  }
  return null;
}

function isLikelyImageBase64(value: string): boolean {
  if (value.length < 256) return false;
  if (value.length % 4 !== 0) return false;
  if (!/^[A-Za-z0-9+/=\r\n]+$/.test(value)) return false;
  const prefix = Buffer.from(value.slice(0, 64), "base64");
  return (
    prefix.subarray(0, 3).equals(Buffer.from([0xff, 0xd8, 0xff])) ||
    prefix.subarray(0, 8).equals(Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a])) ||
    prefix.subarray(0, 4).toString("ascii") === "RIFF"
  );
}

async function downloadImage(url: string): Promise<Uint8Array> {
  const res = await fetch(url);
  if (!res.ok) {
    throw new Error(`Failed to download image from ${url}`);
  }
  return new Uint8Array(await res.arrayBuffer());
}
reverse-engineer-anythingmorluto/reaReverse engineer native, managed, Electron/JavaScript, packaged, firmware, and browser targets with REA, and build agent environments (AgentEnv bundles) of web applications from a live instance or a capture, and of Linux desktop apps with a task from a request. Use shipped-artifact or requested runtime evidence to explain features, compare versions, decompile code, or guide a reconstruction. Skip REA for ordinary source-repository architecture analysis.Stars: 472kUpdated Claude CodeCodexVerifiedInstall
diagnosing-superpowersobra/superpowersUse when a superpowers session went wrong and your human partner wants to know why — repeated work, ignored plans, stumbles, poor results, a skill that didn't fire, "it took too long", "why is it so expensive", "what is it doing" — or wants to build a bug report for the superpowers maintainers, for the current session or a past one identified by id or path, on any harness.Stars: 322kUpdated Claude CodeCodexVerifiedInstall
dispatching-parallel-agentsobra/superpowersUse when facing 2+ independent tasks that can be worked on without shared state or sequential dependenciesStars: 322kUpdated Claude CodeCodexVerifiedInstall
executing-plansobra/superpowersUse when executing an implementation plan in the current session as the implementer yourself — your human partner chose inline execution, or no subagent tool is availableStars: 322kUpdated Claude CodeCodexVerifiedInstall