@sea-ecommerce/admin-mcp-server 0.2.2 → 0.3.0
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/README.md +22 -3
- package/dist/derlux-admin-mcp.js +1084 -52
- package/package.json +1 -1
- package/skill/SKILL.md +84 -9
- package/skill//345/267/245/345/205/267/345/217/202/346/225/260/345/217/202/350/200/203.md +128 -6
- package/skill//350/277/220/350/220/245/344/272/272/345/221/230/346/211/213/345/206/214/03-/345/225/206/345/223/201/347/256/241/347/220/206.md +99 -12
- package/skill//350/277/220/350/220/245/344/272/272/345/221/230/346/211/213/345/206/214/05-/350/256/242/345/215/225/344/270/216/345/224/256/345/220/216.md +19 -2
- package/skill//350/277/220/350/220/245/344/272/272/345/221/230/346/211/213/345/206/214/06-/345/261/245/347/272/246/344/270/216/345/217/221/350/264/247.md +1 -1
- package/skill//350/277/220/350/220/245/344/272/272/345/221/230/346/211/213/345/206/214/08-/350/220/245/351/224/200/344/270/216/344/277/203/351/224/200.md +37 -5
- package/skill//350/277/220/350/220/245/344/272/272/345/221/230/346/211/213/345/206/214/09-/347/253/231/347/202/271/344/270/216/351/205/215/351/200/201/350/256/276/347/275/256.md +2 -0
- package/skill//350/277/220/350/220/245/344/272/272/345/221/230/346/211/213/345/206/214/11-/345/234/250/347/272/277/345/256/242/346/234/215.md +22 -0
- package/skill//350/277/220/350/220/245/344/272/272/345/221/230/346/211/213/345/206/214/README.md +3 -0
- package/skill//350/277/220/350/220/245/345/217/202/350/200/203.md +9 -5
package/dist/derlux-admin-mcp.js
CHANGED
|
@@ -21947,6 +21947,88 @@ var MedusaAdminClient = class {
|
|
|
21947
21947
|
listCollections(params = {}) {
|
|
21948
21948
|
return this.request("GET", `/admin/collections${buildQuery(params)}`);
|
|
21949
21949
|
}
|
|
21950
|
+
// ─── 产品分类(Product Categories)───
|
|
21951
|
+
// ⚠️ Medusa v2 契约:列表响应键为 product_categories,单条为 product_category;
|
|
21952
|
+
// 更新是 POST /admin/product-categories/:id(非 PUT/PATCH);父级字段名为 parent_category_id(非 parent_id)。
|
|
21953
|
+
listProductCategories(params = {}) {
|
|
21954
|
+
return this.request("GET", `/admin/product-categories${buildQuery(params)}`);
|
|
21955
|
+
}
|
|
21956
|
+
getProductCategory(id) {
|
|
21957
|
+
return this.request("GET", `/admin/product-categories/${encodeURIComponent(id)}`);
|
|
21958
|
+
}
|
|
21959
|
+
createProductCategory(body) {
|
|
21960
|
+
return this.request("POST", "/admin/product-categories", body);
|
|
21961
|
+
}
|
|
21962
|
+
updateProductCategory(id, body) {
|
|
21963
|
+
return this.request("POST", `/admin/product-categories/${encodeURIComponent(id)}`, body);
|
|
21964
|
+
}
|
|
21965
|
+
deleteProductCategory(id) {
|
|
21966
|
+
return this.request("DELETE", `/admin/product-categories/${encodeURIComponent(id)}`);
|
|
21967
|
+
}
|
|
21968
|
+
/**
|
|
21969
|
+
* 把「分类 id / handle / name」解析为分类 id。
|
|
21970
|
+
* ⚠️ GET /admin/product-categories/:id 只认 id;handle/name 必须走列表端点解析。
|
|
21971
|
+
* id 直接返回;否则先按 handle 精确匹配(?handle=),再按 q 查并在结果里精确比 name。
|
|
21972
|
+
*/
|
|
21973
|
+
async resolveProductCategoryId(idOrHandleOrName) {
|
|
21974
|
+
const s = idOrHandleOrName.trim();
|
|
21975
|
+
if (/^pcat_[a-z0-9]+$/i.test(s)) return s;
|
|
21976
|
+
try {
|
|
21977
|
+
const byHandle = await this.request(
|
|
21978
|
+
"GET",
|
|
21979
|
+
`/admin/product-categories${buildQuery({ handle: s, limit: 10 })}`
|
|
21980
|
+
);
|
|
21981
|
+
const handleList = byHandle?.product_categories || [];
|
|
21982
|
+
const exactHandle = handleList.find((c) => c.handle === s);
|
|
21983
|
+
if (exactHandle) return exactHandle.id;
|
|
21984
|
+
const byName = await this.request(
|
|
21985
|
+
"GET",
|
|
21986
|
+
`/admin/product-categories${buildQuery({ q: s, limit: 100 })}`
|
|
21987
|
+
);
|
|
21988
|
+
const nameList = byName?.product_categories || [];
|
|
21989
|
+
const exactName = nameList.find((c) => c.name === s);
|
|
21990
|
+
return (exactName ?? nameList[0])?.id || null;
|
|
21991
|
+
} catch {
|
|
21992
|
+
return null;
|
|
21993
|
+
}
|
|
21994
|
+
}
|
|
21995
|
+
// ─── 产品系列 / 集合(Collections)───
|
|
21996
|
+
// ⚠️ 更新是 POST /admin/collections/:id;官方 validator 字段仅 title/handle/external_id/metadata(无 description)。
|
|
21997
|
+
getCollection(id) {
|
|
21998
|
+
return this.request("GET", `/admin/collections/${encodeURIComponent(id)}`);
|
|
21999
|
+
}
|
|
22000
|
+
createCollection(body) {
|
|
22001
|
+
return this.request("POST", "/admin/collections", body);
|
|
22002
|
+
}
|
|
22003
|
+
updateCollection(id, body) {
|
|
22004
|
+
return this.request("POST", `/admin/collections/${encodeURIComponent(id)}`, body);
|
|
22005
|
+
}
|
|
22006
|
+
deleteCollection(id) {
|
|
22007
|
+
return this.request("DELETE", `/admin/collections/${encodeURIComponent(id)}`);
|
|
22008
|
+
}
|
|
22009
|
+
/**
|
|
22010
|
+
* 把「系列 id / handle / title」解析为系列 id。
|
|
22011
|
+
* ⚠️ GET /admin/collections/:id 只认 id;handle/title 必须走列表端点解析。
|
|
22012
|
+
*/
|
|
22013
|
+
async resolveCollectionId(idOrHandleOrTitle) {
|
|
22014
|
+
const s = idOrHandleOrTitle.trim();
|
|
22015
|
+
if (/^pcol_[a-z0-9]+$/i.test(s)) return s;
|
|
22016
|
+
try {
|
|
22017
|
+
const byHandle = await this.request(
|
|
22018
|
+
"GET",
|
|
22019
|
+
`/admin/collections${buildQuery({ handle: s, limit: 10 })}`
|
|
22020
|
+
);
|
|
22021
|
+
const handleList = byHandle?.collections || [];
|
|
22022
|
+
const exactHandle = handleList.find((c) => c.handle === s);
|
|
22023
|
+
if (exactHandle) return exactHandle.id;
|
|
22024
|
+
const byTitle = await this.request("GET", `/admin/collections${buildQuery({ q: s, limit: 100 })}`);
|
|
22025
|
+
const titleList = byTitle?.collections || [];
|
|
22026
|
+
const exactTitle = titleList.find((c) => c.title === s);
|
|
22027
|
+
return (exactTitle ?? titleList[0])?.id || null;
|
|
22028
|
+
} catch {
|
|
22029
|
+
return null;
|
|
22030
|
+
}
|
|
22031
|
+
}
|
|
21950
22032
|
/**
|
|
21951
22033
|
* 把一组商品加入销售渠道(POST /admin/sales-channels/{id}/products)。
|
|
21952
22034
|
* ⚠️ 注意:Medusa v2 该端点挂在「渠道」下,不是商品下;body 为 { add:[productIds], remove:[] }。
|
|
@@ -21958,6 +22040,67 @@ var MedusaAdminClient = class {
|
|
|
21958
22040
|
remove: []
|
|
21959
22041
|
});
|
|
21960
22042
|
}
|
|
22043
|
+
/**
|
|
22044
|
+
* 原生 CSV 导入(Medusa 后台 Products → Import 的等效自动化):
|
|
22045
|
+
* 把本地 CSV 以 multipart 上传到 POST /admin/products/import,返回 transaction_id 与 summary(toCreate/toUpdate)。
|
|
22046
|
+
* ⚠️ 该端点只「登记」导入事务,需再调用 confirmProductImport 才会真正在后台批量建/更商品;
|
|
22047
|
+
* 仅上传不 confirm 时零副作用(不会创建任何商品),可安全用于预检 CSV 是否合法、将建/更多少款。
|
|
22048
|
+
*/
|
|
22049
|
+
async importProductsCsv(fileBuffer, filename) {
|
|
22050
|
+
const form = new FormData();
|
|
22051
|
+
form.append("file", new Blob([new Uint8Array(fileBuffer)], { type: "text/csv" }), filename);
|
|
22052
|
+
const doPost = async () => {
|
|
22053
|
+
const res = await fetch(`${this.config.backendUrl}/admin/products/import`, {
|
|
22054
|
+
method: "POST",
|
|
22055
|
+
headers: this.auth.headers(),
|
|
22056
|
+
body: form
|
|
22057
|
+
});
|
|
22058
|
+
if (res.status === 401) {
|
|
22059
|
+
this.auth.clear();
|
|
22060
|
+
await this.auth.login();
|
|
22061
|
+
return doPost();
|
|
22062
|
+
}
|
|
22063
|
+
if (!res.ok) {
|
|
22064
|
+
const text = await res.text();
|
|
22065
|
+
throw new MedusaAdminError(res.status, text || res.statusText);
|
|
22066
|
+
}
|
|
22067
|
+
return await res.json();
|
|
22068
|
+
};
|
|
22069
|
+
return doPost();
|
|
22070
|
+
}
|
|
22071
|
+
/**
|
|
22072
|
+
* 确认 CSV 导入:触发后台批量建/更商品(POST /admin/products/import/{transaction_id}/confirm)。
|
|
22073
|
+
* Medusa 的导入工作流默认「等待确认」才执行,所以上传后必须显式 confirm。
|
|
22074
|
+
*/
|
|
22075
|
+
async confirmProductImport(transactionId) {
|
|
22076
|
+
await this.request("POST", `/admin/products/import/${encodeURIComponent(transactionId)}/confirm`, {});
|
|
22077
|
+
}
|
|
22078
|
+
/**
|
|
22079
|
+
* 查询导入工作流执行状态。
|
|
22080
|
+
* ⚠️ Medusa 2.21 **没有** `/admin/workflows/executions/{id}` 这个路径:只能走列表端点
|
|
22081
|
+
* `GET /admin/workflows-executions?transaction_id=<id>&workflow_id=import-products`,
|
|
22082
|
+
* 返回 `{ workflow_executions: [{ state, execution, ... }] }`——状态字段是 `state` 而不是 `status`。
|
|
22083
|
+
* 查不到记录时返回 null,由调用方兜底(不抛错)。
|
|
22084
|
+
*/
|
|
22085
|
+
async getWorkflowExecution(transactionId, workflowId = "import-products") {
|
|
22086
|
+
const data = await this.request(
|
|
22087
|
+
"GET",
|
|
22088
|
+
`/admin/workflows-executions${buildQuery({ transaction_id: transactionId, workflow_id: workflowId })}`
|
|
22089
|
+
);
|
|
22090
|
+
const row = (data?.workflow_executions || [])[0];
|
|
22091
|
+
if (!row) return null;
|
|
22092
|
+
return { state: String(row.state || "") };
|
|
22093
|
+
}
|
|
22094
|
+
/**
|
|
22095
|
+
* 为已有商品的选项补取值(POST /admin/products/{id}/options/batch 的 update 段,Medusa ≥2.17)。
|
|
22096
|
+
* 更新商品(POST /admin/products/{id})自 2.16 起禁止传 `options`,新增选项/取值只能走本端点;
|
|
22097
|
+
* 新建变体前必须先确保其引用的选项取值已存在,否则商品 update 会因找不到选项值而失败。
|
|
22098
|
+
*/
|
|
22099
|
+
addProductOptionValues(productId, update) {
|
|
22100
|
+
return this.request("POST", `/admin/products/${encodeURIComponent(productId)}/options/batch`, {
|
|
22101
|
+
update
|
|
22102
|
+
});
|
|
22103
|
+
}
|
|
21961
22104
|
// ─── 商品详情页富内容(product_content 模块)───
|
|
21962
22105
|
/**
|
|
21963
22106
|
* 读取某商品某语言的富内容块(GET /admin/product-contents)。
|
|
@@ -22352,6 +22495,11 @@ var contextTools = [
|
|
|
22352
22495
|
}
|
|
22353
22496
|
];
|
|
22354
22497
|
|
|
22498
|
+
// src/tools/products.ts
|
|
22499
|
+
import { mkdirSync as mkdirSync2, writeFileSync as writeFileSync2 } from "node:fs";
|
|
22500
|
+
import { homedir as homedir2 } from "node:os";
|
|
22501
|
+
import { join as join2 } from "node:path";
|
|
22502
|
+
|
|
22355
22503
|
// src/tools/stock.ts
|
|
22356
22504
|
async function applyProductStock(ctx2, opts) {
|
|
22357
22505
|
const { client: client2 } = ctx2;
|
|
@@ -22557,6 +22705,18 @@ location_id \u53EF\u9009\uFF1A\u4F20\u4E86\u5219\u53EA\u56DE\u8BE5\u4ED3\u5E93\u
|
|
|
22557
22705
|
];
|
|
22558
22706
|
|
|
22559
22707
|
// src/tools/translations.ts
|
|
22708
|
+
var TRANSLATION_FIELDS = {
|
|
22709
|
+
product: ["title", "subtitle", "description"],
|
|
22710
|
+
product_option: ["title"],
|
|
22711
|
+
product_option_value: ["value"],
|
|
22712
|
+
product_category: ["name", "description"],
|
|
22713
|
+
product_collection: ["title"],
|
|
22714
|
+
shipping_option: ["name"]
|
|
22715
|
+
};
|
|
22716
|
+
var DEFAULT_TRANSLATION_FIELDS = ["title", "subtitle", "description"];
|
|
22717
|
+
function allowedTranslationFields(reference) {
|
|
22718
|
+
return TRANSLATION_FIELDS[reference] ?? DEFAULT_TRANSLATION_FIELDS;
|
|
22719
|
+
}
|
|
22560
22720
|
function canonLocaleKey(locale) {
|
|
22561
22721
|
return normalizeLocale(locale) ?? locale;
|
|
22562
22722
|
}
|
|
@@ -22600,23 +22760,37 @@ async function upsertTranslations(client2, reference, referenceId, entries) {
|
|
|
22600
22760
|
var translationTools = [
|
|
22601
22761
|
{
|
|
22602
22762
|
name: "set_translations",
|
|
22603
|
-
description: `\u4E3A\u5B9E\u4F53\uFF08\u5546\u54C1\
|
|
22763
|
+
description: `\u4E3A\u5B9E\u4F53\uFF08\u5546\u54C1\u3001\u9009\u9879\u540D\u3001\u9009\u9879\u53D6\u503C\u3001\u5206\u7C7B\u3001\u7CFB\u5217\u3001\u914D\u9001\u65B9\u5F0F\uFF09**\u521B\u5EFA\u6216\u66F4\u65B0\uFF08upsert\uFF09**\u591A\u8BED\u8A00\u7FFB\u8BD1\uFF0C\u8D70 /admin/translations/batch\u3002
|
|
22604
22764
|
upsert \u8BED\u4E49\uFF1A\u540C\u8BED\u8A00\u5DF2\u6709\u7FFB\u8BD1 \u2192 \u66F4\u65B0\uFF08\u5B57\u6BB5\u5408\u5E76\uFF0C\u672A\u7ED9\u7684\u65E7\u5B57\u6BB5\u4FDD\u7559\uFF0C\u7EDD\u4E0D\u4EA7\u751F\u91CD\u590D\u884C\uFF09\uFF1B\u6CA1\u6709 \u2192 \u65B0\u5EFA\u3002
|
|
22765
|
+
\u26A0\uFE0F \u5199\u5165\u5B57\u6BB5\u6309 reference \u767D\u540D\u5355\u6821\u9A8C\uFF0C\u5199\u9519\u5B57\u6BB5\u4F1A**\u76F4\u63A5\u62A5\u9519**\u800C\u4E0D\u662F\u9759\u9ED8\u5B58\u6210\u65E0\u4EBA\u8BFB\u53D6\u7684\u952E\uFF1A
|
|
22766
|
+
- product \u2192 title / subtitle / description\uFF1B
|
|
22767
|
+
- product_option\uFF08\u9009\u9879\u540D\uFF09\u2192 title\uFF1B
|
|
22768
|
+
- product_option_value\uFF08\u9009\u9879\u53D6\u503C\uFF0C\u5982 ALC / Penhold\uFF09\u2192 **value**\uFF08\u4E0D\u662F title\uFF01\uFF09\uFF1B
|
|
22769
|
+
- product_category \u2192 name / description\uFF1Bproduct_collection \u2192 title\uFF1Bshipping_option \u2192 name\u3002
|
|
22605
22770
|
\u26A0\uFE0F locale \u952E\u63A5\u53D7\u77ED\u7801\uFF08en/zh/ms/id\uFF09\u6216 BCP-47 \u533A\u57DF\u7801\uFF08en-US/zh-CN/ms-MY/id-ID\uFF09\uFF0C\u5DE5\u5177\u7EDF\u4E00\u5F52\u4E00\u5316\u4E3A BCP-47 \u843D\u5E93\u2014\u2014
|
|
22606
22771
|
storefront \u5BF9\u7FFB\u8BD1\u63A5\u53E3\u4E00\u5F8B\u8BF7\u6C42 BCP-47\u3001\u4E0E\u5E93\u4E2D locale_code \u7CBE\u786E\u5339\u914D\uFF1B\u5386\u53F2\u6309\u77ED\u7801\u843D\u5E93\u7684\u884C\u4F1A\u88AB\u672C\u6B21\u8C03\u7528**\u6539\u7801\u4FEE\u590D**\u3002
|
|
22607
|
-
\
|
|
22772
|
+
\u5B57\u6BB5\u503C\u4F20\u7A7A\u5B57\u7B26\u4E32 = \u628A\u8BE5\u5B57\u6BB5\u8BD1\u6587\u7F6E\u7A7A\uFF08\u5199\u5165\u540E\u8BF7\u7528 get_translations \u56DE\u8BFB\u786E\u8BA4\uFF0C\u5220\u9664\u6574\u6761\u8BD1\u6587\u8BF7\u5230\u540E\u53F0\u64CD\u4F5C\uFF09\u3002
|
|
22773
|
+
\u7528\u9014\uFF1A\u5546\u54C1\u521B\u5EFA\u540E\u8865\u7FFB\u8BD1/\u6539\u7FFB\u8BD1/\u4FEE\u590D\u591A\u8BED\u8A00\uFF1B\u628A\u82F1\u6587\u7A3F\u7FFB\u597D\u540E\u540C\u6B65\u5230\u5176\u4ED6\u8BED\u8A00\u3002
|
|
22774
|
+
\u9ED8\u8BA4 dry_run\uFF08confirm=false\uFF09\u9884\u89C8\u5C06\u521B\u5EFA/\u66F4\u65B0\u54EA\u4E9B\u884C\u3002`,
|
|
22608
22775
|
schema: {
|
|
22609
|
-
reference: external_exports.string().default("product").describe("\u5B9E\u4F53\u7C7B\u578B\
|
|
22610
|
-
reference_id: external_exports.string().describe("\u5B9E\u4F53 id\uFF1B\u5546\u54C1\u53EF\u4F20 handle\uFF08\u81EA\u52A8\u89E3\u6790\u4E3A id\uFF09"),
|
|
22611
|
-
translations: external_exports.record(external_exports.string(), external_exports.
|
|
22612
|
-
title: external_exports.string().optional(),
|
|
22613
|
-
subtitle: external_exports.string().optional(),
|
|
22614
|
-
description: external_exports.string().optional()
|
|
22615
|
-
})).describe('locale \u2192 \u7FFB\u8BD1\u5B57\u6BB5\uFF0C\u5982 {"zh-CN":{"title":"...","description":"..."}}\uFF1B\u77ED\u7801/\u533A\u57DF\u7801\u5747\u53EF'),
|
|
22776
|
+
reference: external_exports.string().default("product").describe("\u5B9E\u4F53\u7C7B\u578B\uFF1Aproduct / product_option / product_option_value / product_category / product_collection / shipping_option"),
|
|
22777
|
+
reference_id: external_exports.string().describe("\u5B9E\u4F53 id\uFF1B\u5546\u54C1\u53EF\u4F20 handle\uFF08\u81EA\u52A8\u89E3\u6790\u4E3A id\uFF09\uFF1B\u9009\u9879\u4E0E\u9009\u9879\u53D6\u503C\u987B\u4F20\u771F\u5B9E id"),
|
|
22778
|
+
translations: external_exports.record(external_exports.string(), external_exports.record(external_exports.string(), external_exports.string())).describe('locale \u2192 \u7FFB\u8BD1\u5B57\u6BB5\uFF0C\u5982 {"zh-CN":{"title":"...","description":"..."}}\uFF1B\u5B57\u6BB5\u540D\u987B\u5339\u914D reference\uFF08\u9009\u9879\u53D6\u503C\u7528 value\uFF09\u3002\u77ED\u7801/\u533A\u57DF\u7801\u5747\u53EF'),
|
|
22616
22779
|
confirm: external_exports.boolean().default(false)
|
|
22617
22780
|
},
|
|
22618
22781
|
async run(input, { client: client2 }) {
|
|
22619
22782
|
let refId = String(input.reference_id).trim();
|
|
22783
|
+
const allowed = allowedTranslationFields(input.reference);
|
|
22784
|
+
const badFields = Array.from(
|
|
22785
|
+
new Set(
|
|
22786
|
+
Object.values(input.translations ?? {}).flatMap(
|
|
22787
|
+
(t) => Object.keys(t ?? {}).filter((k) => !allowed.includes(k))
|
|
22788
|
+
)
|
|
22789
|
+
)
|
|
22790
|
+
);
|
|
22791
|
+
if (badFields.length) {
|
|
22792
|
+
return `\u9519\u8BEF\uFF1Areference "${input.reference}" \u4E0D\u652F\u6301\u7FFB\u8BD1\u5B57\u6BB5 ${badFields.join(" / ")}\uFF1B\u5141\u8BB8\u7684\u5B57\u6BB5\u662F ${allowed.join(" / ")}\u3002${input.reference === "product_option_value" ? "\u9009\u9879\u53D6\u503C\uFF08\u5982 ALC / Penhold\uFF09\u7684\u8BD1\u6587\u5217\u540D\u662F value \u800C\u4E0D\u662F title\u3002" : ""}`;
|
|
22793
|
+
}
|
|
22620
22794
|
if (input.reference === "product" && !/^prod_[a-z0-9]+$/i.test(refId)) {
|
|
22621
22795
|
const id = await client2.resolveProductId(refId);
|
|
22622
22796
|
if (!id) return `\u9519\u8BEF\uFF1A\u627E\u4E0D\u5230\u5546\u54C1 "${refId}"\uFF08\u8BF7\u4F20\u5546\u54C1 id \u6216\u6709\u6548 handle\uFF09`;
|
|
@@ -22629,6 +22803,10 @@ storefront \u5BF9\u7FFB\u8BD1\u63A5\u53E3\u4E00\u5F8B\u8BF7\u6C42 BCP-47\u3001\u
|
|
|
22629
22803
|
)
|
|
22630
22804
|
}));
|
|
22631
22805
|
if (!entries.length) return "\u9519\u8BEF\uFF1Atranslations \u4E3A\u7A7A\uFF0C\u65E0\u4E8B\u53EF\u505A\u3002";
|
|
22806
|
+
const emptyLocales = entries.filter((e) => Object.keys(e.translations).length === 0).map((e) => e.locale_code);
|
|
22807
|
+
if (emptyLocales.length) {
|
|
22808
|
+
return `\u9519\u8BEF\uFF1Alocale ${emptyLocales.join(" / ")} \u6CA1\u6709\u4EFB\u4F55\u7FFB\u8BD1\u5B57\u6BB5\uFF08\u53EF\u80FD\u5B57\u6BB5\u540D\u5199\u9519\u88AB\u8FC7\u6EE4\uFF09\uFF0C\u5DF2\u4E2D\u6B62\u4EE5\u514D\u5199\u5165\u7A7A\u8BD1\u6587\u884C\u3002\u5141\u8BB8\u7684\u5B57\u6BB5\uFF1A${allowed.join(" / ")}\u3002`;
|
|
22809
|
+
}
|
|
22632
22810
|
const plan = await planTranslationUpsert(client2, input.reference, refId, entries);
|
|
22633
22811
|
if (!input.confirm) {
|
|
22634
22812
|
return {
|
|
@@ -22670,6 +22848,49 @@ var translationShape = external_exports.object({
|
|
|
22670
22848
|
title: external_exports.string().optional(),
|
|
22671
22849
|
description: external_exports.string().optional()
|
|
22672
22850
|
});
|
|
22851
|
+
function summarizeBlocks(blocks) {
|
|
22852
|
+
return { count: blocks.length, types: blocks.map((b) => String(b?.type ?? "unknown")) };
|
|
22853
|
+
}
|
|
22854
|
+
function diffBlockStructure(prev, next) {
|
|
22855
|
+
const tally = (arr) => arr.reduce((m, t) => (m[t] = (m[t] || 0) + 1, m), {});
|
|
22856
|
+
const a = tally(prev);
|
|
22857
|
+
const b = tally(next);
|
|
22858
|
+
const added = [];
|
|
22859
|
+
const removed = [];
|
|
22860
|
+
for (const k of Object.keys(b)) {
|
|
22861
|
+
const d = (b[k] || 0) - (a[k] || 0);
|
|
22862
|
+
if (d > 0) added.push(d > 1 ? `${k}\xD7${d}` : k);
|
|
22863
|
+
}
|
|
22864
|
+
for (const k of Object.keys(a)) {
|
|
22865
|
+
const d = (a[k] || 0) - (b[k] || 0);
|
|
22866
|
+
if (d > 0) removed.push(d > 1 ? `${k}\xD7${d}` : k);
|
|
22867
|
+
}
|
|
22868
|
+
return {
|
|
22869
|
+
types_added: added,
|
|
22870
|
+
types_removed: removed,
|
|
22871
|
+
same_structure: JSON.stringify(prev) === JSON.stringify(next)
|
|
22872
|
+
};
|
|
22873
|
+
}
|
|
22874
|
+
function backupProductContent(productId, locale, status, blocks) {
|
|
22875
|
+
try {
|
|
22876
|
+
const dir = join2(homedir2(), ".derlux", "content-backups");
|
|
22877
|
+
mkdirSync2(dir, { recursive: true });
|
|
22878
|
+
const stamp = (/* @__PURE__ */ new Date()).toISOString().replace(/[:.]/g, "-");
|
|
22879
|
+
const file = join2(dir, `${productId}-${locale}-${status}-${stamp}.json`);
|
|
22880
|
+
writeFileSync2(
|
|
22881
|
+
file,
|
|
22882
|
+
JSON.stringify(
|
|
22883
|
+
{ product_id: productId, locale, status, saved_at: (/* @__PURE__ */ new Date()).toISOString(), blocks },
|
|
22884
|
+
null,
|
|
22885
|
+
2
|
|
22886
|
+
),
|
|
22887
|
+
"utf8"
|
|
22888
|
+
);
|
|
22889
|
+
return file;
|
|
22890
|
+
} catch {
|
|
22891
|
+
return null;
|
|
22892
|
+
}
|
|
22893
|
+
}
|
|
22673
22894
|
var productTools = [
|
|
22674
22895
|
{
|
|
22675
22896
|
name: "list_products",
|
|
@@ -22723,11 +22944,14 @@ var productTools = [
|
|
|
22723
22944
|
name: "get_translations",
|
|
22724
22945
|
description: `\u8BFB\u53D6\u67D0\u5B9E\u4F53\u7684\u591A\u8BED\u8A00\u7FFB\u8BD1\uFF08GET /admin/translations\uFF09\u2014\u2014\u6838\u5BF9\u5546\u54C1\u56DB\u8BED\u8A00\u662F\u5426\u5199\u5165\u6210\u529F\u7684\u552F\u4E00\u53EF\u9760\u9014\u5F84
|
|
22725
22946
|
\uFF08get_product \u9ED8\u8BA4\u5B57\u6BB5\u4E0D\u542B translations\uFF09\u3002reference \u4E00\u822C\u4E3A product\uFF0Creference_id \u4F20\u5546\u54C1 id \u6216 handle\u3002
|
|
22726
|
-
\
|
|
22947
|
+
\u4E5F\u652F\u6301 product_option\uFF08\u9009\u9879\u540D\uFF09/ product_option_value\uFF08\u9009\u9879\u53D6\u503C\uFF09/ product_category / product_collection / shipping_option\u3002
|
|
22948
|
+
\u8FD4\u56DE\u6BCF\u6761 { locale_code, field_keys, ...\u539F\u6837\u5B57\u6BB5 }\uFF1A\u5546\u54C1\u662F title/subtitle/description\uFF0C\u9009\u9879\u53D6\u503C\u662F value\uFF0C
|
|
22949
|
+
**\u9ED8\u8BA4\u4E0D\u622A\u65AD**\uFF08\u9700\u8981\u63A7\u5236\u4E0A\u4E0B\u6587\u65F6\u7528 max_chars\uFF0C\u907F\u514D"\u770B\u4E0D\u5230\u5168\u6587\u5C31\u6CA1\u6CD5\u6838\u5BF9"\uFF09\u3002`,
|
|
22727
22950
|
schema: {
|
|
22728
22951
|
reference: external_exports.string().default("product"),
|
|
22729
|
-
reference_id: external_exports.string().describe("\u5546\u54C1 id \u6216 handle"),
|
|
22952
|
+
reference_id: external_exports.string().describe("\u5546\u54C1 id \u6216 handle\uFF1B\u5176\u5B83\u5B9E\u4F53\u4F20\u771F\u5B9E id"),
|
|
22730
22953
|
locale_code: external_exports.string().optional().describe("\u53EA\u53D6\u67D0\u4E00\u8BED\u8A00\uFF0C\u5982 zh"),
|
|
22954
|
+
max_chars: external_exports.number().int().nonnegative().default(0).describe("\u5355\u5B57\u6BB5\u6700\u591A\u8FD4\u56DE\u591A\u5C11\u5B57\uFF0C0=\u4E0D\u622A\u65AD\uFF08\u9ED8\u8BA4\uFF09\u3002\u8D85\u957F\u65F6\u5C3E\u90E8\u6807\u51FA\u539F\u59CB\u5B57\u6570"),
|
|
22731
22955
|
limit: external_exports.number().int().positive().max(100).default(50)
|
|
22732
22956
|
},
|
|
22733
22957
|
async run(input, { client: client2 }) {
|
|
@@ -22745,14 +22969,12 @@ var productTools = [
|
|
|
22745
22969
|
};
|
|
22746
22970
|
if (input.locale_code) params.locale_code = input.locale_code;
|
|
22747
22971
|
const data = await client2.listTranslations(params);
|
|
22972
|
+
const maxChars = input.max_chars ?? 0;
|
|
22973
|
+
const clip = (v) => typeof v === "string" && maxChars > 0 && v.length > maxChars ? `${v.slice(0, maxChars)}\u2026[\u5171 ${v.length} \u5B57]` : v;
|
|
22748
22974
|
const rows = (data?.translations || []).map((t) => {
|
|
22749
22975
|
const tr = t.translations || {};
|
|
22750
|
-
|
|
22751
|
-
|
|
22752
|
-
title: tr.title ?? null,
|
|
22753
|
-
description: tr.description ? tr.description.slice(0, 120) + "\u2026" : null,
|
|
22754
|
-
has_full_description: Boolean(tr.description)
|
|
22755
|
-
};
|
|
22976
|
+
const fields = Object.fromEntries(Object.entries(tr).map(([k, v]) => [k, clip(v)]));
|
|
22977
|
+
return { locale_code: t.locale_code, field_keys: Object.keys(tr), ...fields };
|
|
22756
22978
|
});
|
|
22757
22979
|
return { reference: input.reference, reference_id: input.reference_id, translations: rows, count: rows.length };
|
|
22758
22980
|
}
|
|
@@ -23184,12 +23406,16 @@ storefront \u8BE6\u60C5\u9875\u300C\u5BCC\u5185\u5BB9\u300D\u533A\u7531\u8FD9\u4
|
|
|
23184
23406
|
- video: data.{ url, poster? }\uFF08\u652F\u6301 YouTube\uFF09
|
|
23185
23407
|
- faq: data.{ items: {question,answer}[] }
|
|
23186
23408
|
- specs: data.{ rows: {label,value}[] }
|
|
23409
|
+
\u26A0\uFE0F \u8BE5\u63A5\u53E3\u662F**\u6574\u5757\u66FF\u6362**\uFF08\u4E0D\u5408\u5E76\uFF09\uFF1A\u76EE\u6807\u8BED\u8A00\u5DF2\u6709\u5185\u5BB9\u65F6\u4F1A\u88AB\u6574\u7EC4\u8986\u76D6\uFF0C\u540E\u7AEF\u65E0\u7248\u672C\u5386\u53F2\u3002
|
|
23410
|
+
\u56E0\u6B64\u672C\u5DE5\u5177\u9ED8\u8BA4\u62E6\u4E00\u9053\uFF1A\u76EE\u6807\u8BED\u8A00\u5DF2\u6709\u5757 \u2192 dry_run \u8FD4\u56DE existing + diff\uFF0Cconfirm \u5FC5\u987B\u663E\u5F0F\u5E26 overwrite=true\uFF0C
|
|
23411
|
+
\u5E76\u5728\u8986\u76D6\u524D\u628A\u65E7\u5757\u5907\u4EFD\u5230 ~/.derlux/content-backups/\uFF08\u54CD\u5E94\u91CC\u7684 backup_path\uFF09\u3002
|
|
23412
|
+
\u53EA\u60F3\u6539\u4E2A\u522B\u5757 \u2192 \u5148 get_product_content \u53D6\u56DE\u65E7\u5757\uFF0C\u5728\u65E7\u5757\u4E0A\u6539\u5199\u540E\u6574\u7EC4\u63D0\u4EA4\uFF0C\u4E0D\u8981\u51ED\u8BB0\u5FC6\u91CD\u5EFA\uFF08\u4F1A\u4E22\u5757/\u4E22\u56FE\uFF09\u3002
|
|
23187
23413
|
\u26A0\uFE0F locale \u7528\u533A\u57DF\u7801 en-US / zh-CN / ms-MY / id-ID\uFF08\u4E0E\u524D\u7AEF\u4E00\u81F4\uFF0C\u63A8\u8350\uFF09\uFF1B\u4E5F\u63A5\u53D7\u77ED\u7801 en/zh/ms/id\uFF08\u81EA\u52A8\u5F52\u4E00\u5316\u4E3A\u533A\u57DF\u7801\uFF09\u3002\u5546\u54C1\u6807\u9898/\u63CF\u8FF0\u7684\u7FFB\u8BD1\u7528\u77ED\u7801\uFF0C\u4E8C\u8005\u4E0D\u540C\u3002
|
|
23188
23414
|
\u9ED8\u8BA4 dry_run\uFF08confirm=false\uFF09\u4EC5\u9884\u89C8\u3002`,
|
|
23189
23415
|
schema: {
|
|
23190
23416
|
product_id: external_exports.string(),
|
|
23191
23417
|
locale: external_exports.string().default("en-US").describe("\u533A\u57DF\u7801 en-US/zh-CN/ms-MY/id-ID\uFF0C\u6216\u77ED\u7801 en/zh/ms/id"),
|
|
23192
|
-
status: external_exports.enum(["draft", "published"]).default("published"),
|
|
23418
|
+
status: external_exports.enum(["draft", "published"]).default("published").describe("\u5199\u54EA\u4E00\u7248\uFF1Adraft=\u8349\u7A3F\uFF08\u540E\u53F0\u9884\u89C8\u7528\uFF09\uFF0Cpublished=\u987E\u5BA2\u53EF\u89C1\uFF1B\u4E24\u7248\u72EC\u7ACB\u5B58\u50A8"),
|
|
23193
23419
|
blocks: external_exports.array(
|
|
23194
23420
|
external_exports.object({
|
|
23195
23421
|
id: external_exports.string().optional(),
|
|
@@ -23197,6 +23423,8 @@ storefront \u8BE6\u60C5\u9875\u300C\u5BCC\u5185\u5BB9\u300D\u533A\u7531\u8FD9\u4
|
|
|
23197
23423
|
data: external_exports.record(external_exports.string(), external_exports.any())
|
|
23198
23424
|
})
|
|
23199
23425
|
).describe("ContentBlock[] \u6570\u7EC4"),
|
|
23426
|
+
overwrite: external_exports.boolean().default(false).describe("\u76EE\u6807\u8BED\u8A00\u5DF2\u6709\u5185\u5BB9\u65F6\uFF0C\u5FC5\u987B\u663E\u5F0F\u8BBE true \u624D\u5141\u8BB8\u6574\u5757\u8986\u76D6\uFF08\u9632\u9759\u9ED8\u4E22\u7A3F\uFF09"),
|
|
23427
|
+
backup: external_exports.boolean().default(true).describe("\u8986\u76D6\u524D\u662F\u5426\u628A\u65E7\u5757\u5907\u4EFD\u5230 ~/.derlux/content-backups/\uFF08\u9ED8\u8BA4\u5F00\u542F\uFF09"),
|
|
23200
23428
|
confirm: external_exports.boolean().default(false)
|
|
23201
23429
|
},
|
|
23202
23430
|
async run(input, { client: client2 }) {
|
|
@@ -23204,6 +23432,32 @@ storefront \u8BE6\u60C5\u9875\u300C\u5BCC\u5185\u5BB9\u300D\u533A\u7531\u8FD9\u4
|
|
|
23204
23432
|
if (!locale) {
|
|
23205
23433
|
return `\u9519\u8BEF\uFF1Alocale "${input.locale}" \u4E0D\u53D7\u652F\u6301\uFF0C\u8BF7\u7528 en-US / zh-CN / ms-MY / id-ID\uFF08\u6216\u77ED\u7801 en/zh/ms/id\uFF09\u3002`;
|
|
23206
23434
|
}
|
|
23435
|
+
let existing = null;
|
|
23436
|
+
try {
|
|
23437
|
+
const data2 = await client2.getProductContent(input.product_id, locale, input.status);
|
|
23438
|
+
const pc = data2?.product_content;
|
|
23439
|
+
if (pc) {
|
|
23440
|
+
let blocks = [];
|
|
23441
|
+
try {
|
|
23442
|
+
blocks = pc.blocks ? JSON.parse(pc.blocks) : [];
|
|
23443
|
+
} catch {
|
|
23444
|
+
blocks = [];
|
|
23445
|
+
}
|
|
23446
|
+
existing = { id: pc.id, blocks };
|
|
23447
|
+
}
|
|
23448
|
+
} catch (err) {
|
|
23449
|
+
const status = err instanceof MedusaAdminError ? err.status : 0;
|
|
23450
|
+
if (status !== 404 && status !== 400) {
|
|
23451
|
+
return {
|
|
23452
|
+
error: `\u65E0\u6CD5\u786E\u8BA4\u76EE\u6807\u8BED\u8A00\u662F\u5426\u5DF2\u6709\u5185\u5BB9\uFF08${locale}/${input.status}\uFF09${err instanceof Error ? `\uFF1A${err.message}` : ""}\u3002\u4E3A\u907F\u514D\u9759\u9ED8\u8986\u76D6\u8FD0\u8425\u7A3F\u4EF6\u5DF2\u4E2D\u6B62\uFF0C\u8BF7\u91CD\u8BD5\u6216\u5148\u624B\u52A8\u6838\u5BF9\u3002`
|
|
23453
|
+
};
|
|
23454
|
+
}
|
|
23455
|
+
}
|
|
23456
|
+
const prevBlocks = existing?.blocks ?? [];
|
|
23457
|
+
const hasPrev = prevBlocks.length > 0;
|
|
23458
|
+
const prevSummary = summarizeBlocks(prevBlocks);
|
|
23459
|
+
const nextSummary = summarizeBlocks(input.blocks);
|
|
23460
|
+
const diff = hasPrev ? diffBlockStructure(prevSummary.types, nextSummary.types) : { types_added: nextSummary.types, types_removed: [], same_structure: false };
|
|
23207
23461
|
if (!input.confirm) {
|
|
23208
23462
|
return {
|
|
23209
23463
|
dry_run: true,
|
|
@@ -23211,18 +23465,401 @@ storefront \u8BE6\u60C5\u9875\u300C\u5BCC\u5185\u5BB9\u300D\u533A\u7531\u8FD9\u4
|
|
|
23211
23465
|
product_id: input.product_id,
|
|
23212
23466
|
locale,
|
|
23213
23467
|
status: input.status,
|
|
23214
|
-
block_count:
|
|
23215
|
-
types:
|
|
23216
|
-
}
|
|
23468
|
+
block_count: nextSummary.count,
|
|
23469
|
+
types: nextSummary.types
|
|
23470
|
+
},
|
|
23471
|
+
existing: hasPrev ? { id: existing.id, ...prevSummary } : null,
|
|
23472
|
+
will_replace: hasPrev,
|
|
23473
|
+
overwrite_required: hasPrev,
|
|
23474
|
+
diff,
|
|
23475
|
+
note: hasPrev ? `\u76EE\u6807\u8BED\u8A00 ${locale}\uFF08${input.status}\uFF09\u5DF2\u6709 ${prevSummary.count} \u4E2A\u5757\uFF0Cconfirm \u65F6\u5FC5\u987B\u5E26 overwrite=true\uFF1B\u8986\u76D6\u524D\u4F1A\u81EA\u52A8\u5907\u4EFD\u65E7\u5757\u3002` : "\u76EE\u6807\u8BED\u8A00\u6682\u65E0\u5185\u5BB9\uFF0C\u53EF\u76F4\u63A5 confirm \u5199\u5165\u3002"
|
|
23217
23476
|
};
|
|
23218
23477
|
}
|
|
23478
|
+
if (hasPrev && !input.overwrite) {
|
|
23479
|
+
return {
|
|
23480
|
+
error: `\u76EE\u6807\u8BED\u8A00 ${locale}\uFF08${input.status}\uFF09\u5DF2\u6709 ${prevSummary.count} \u4E2A\u5185\u5BB9\u5757\uFF08${prevSummary.types.join(" / ")}\uFF09\uFF0C\u672C\u6B21\u63D0\u4EA4\u4F1A\u6574\u5757\u8986\u76D6\u3002`,
|
|
23481
|
+
overwrite_required: true,
|
|
23482
|
+
existing: { id: existing.id, ...prevSummary },
|
|
23483
|
+
diff,
|
|
23484
|
+
hint: `\u786E\u8BA4\u66FF\u6362\u8BF7\u5E26 overwrite=true \u91CD\u8DD1\uFF1B\u53EA\u60F3\u6539\u4E2A\u522B\u5757\u8BF7\u5148 get_product_content \u53D6\u56DE\u65E7\u5757\uFF0C\u5728\u65E7\u5757\u4E0A\u6539\u5199\u540E\u6574\u7EC4\u63D0\u4EA4\uFF08\u76F4\u63A5\u91CD\u5EFA\u4F1A\u4E22\u5757\uFF09\u3002\u4E5F\u8981\u786E\u8BA4 status\uFF1Adraft \u4E0E published \u662F\u4E24\u4EFD\u72EC\u7ACB\u5185\u5BB9\uFF0C\u5F53\u524D\u5199\u7684\u662F ${input.status}\u3002`
|
|
23485
|
+
};
|
|
23486
|
+
}
|
|
23487
|
+
const backupPath = hasPrev && input.backup !== false ? backupProductContent(input.product_id, locale, input.status, prevBlocks) : null;
|
|
23219
23488
|
const data = await client2.createProductContent({
|
|
23220
23489
|
product_id: input.product_id,
|
|
23221
23490
|
locale,
|
|
23222
23491
|
status: input.status,
|
|
23223
23492
|
blocks: input.blocks
|
|
23224
23493
|
});
|
|
23225
|
-
return {
|
|
23494
|
+
return {
|
|
23495
|
+
created: data,
|
|
23496
|
+
locale,
|
|
23497
|
+
status: input.status,
|
|
23498
|
+
block_count: nextSummary.count,
|
|
23499
|
+
replaced: hasPrev,
|
|
23500
|
+
diff,
|
|
23501
|
+
backup_path: hasPrev ? backupPath ?? "\u5907\u4EFD\u5931\u8D25\uFF08\u672A\u963B\u65AD\u5199\u5165\uFF09\u2014\u2014\u65E7\u7A3F\u5DF2\u4E22\u5931\uFF0C\u8BF7\u786E\u8BA4\u662F\u5426\u8BEF\u8986\u76D6" : null
|
|
23502
|
+
};
|
|
23503
|
+
}
|
|
23504
|
+
}
|
|
23505
|
+
];
|
|
23506
|
+
|
|
23507
|
+
// src/tools/categories.ts
|
|
23508
|
+
var categoryTranslationShape = external_exports.object({
|
|
23509
|
+
name: external_exports.string().optional(),
|
|
23510
|
+
description: external_exports.string().optional()
|
|
23511
|
+
});
|
|
23512
|
+
function toCategoryTranslations(raw) {
|
|
23513
|
+
return Object.entries(raw ?? {}).map(([locale, t]) => ({
|
|
23514
|
+
locale_code: canonLocaleKey(locale),
|
|
23515
|
+
translations: Object.fromEntries(
|
|
23516
|
+
Object.entries(t ?? {}).filter(([, v]) => v !== void 0 && v !== null)
|
|
23517
|
+
)
|
|
23518
|
+
}));
|
|
23519
|
+
}
|
|
23520
|
+
var categoryTools = [
|
|
23521
|
+
{
|
|
23522
|
+
name: "list_product_categories",
|
|
23523
|
+
description: "\u67E5\u8BE2\u540E\u53F0\u4EA7\u54C1\u5206\u7C7B\u5217\u8868\uFF08GET /admin/product-categories\uFF09\u3002\u652F\u6301 q/handle/parent_category_id/is_internal/is_active \u8FC7\u6EE4\u3002\u5206\u7C7B\u5168\u7AD9\u5171\u7528\uFF0C\u4E0D\u6309\u56FD\u5BB6\u7AD9\u70B9\u533A\u5206\u3002",
|
|
23524
|
+
schema: {
|
|
23525
|
+
q: external_exports.string().optional().describe("\u5173\u952E\u8BCD\uFF08\u5339\u914D name/handle\uFF09"),
|
|
23526
|
+
handle: external_exports.string().optional(),
|
|
23527
|
+
parent_category_id: external_exports.string().optional().describe("\u53EA\u53D6\u67D0\u7236\u5206\u7C7B\u7684\u76F4\u63A5\u5B50\u7EA7"),
|
|
23528
|
+
is_internal: external_exports.boolean().optional().describe("\u662F\u5426\u4EC5\u5185\u90E8\u53EF\u89C1"),
|
|
23529
|
+
is_active: external_exports.boolean().optional(),
|
|
23530
|
+
limit: external_exports.number().int().positive().max(100).default(50),
|
|
23531
|
+
offset: external_exports.number().int().nonnegative().default(0)
|
|
23532
|
+
},
|
|
23533
|
+
async run(input, { client: client2 }) {
|
|
23534
|
+
return client2.listProductCategories({
|
|
23535
|
+
q: input.q,
|
|
23536
|
+
handle: input.handle,
|
|
23537
|
+
parent_category_id: input.parent_category_id,
|
|
23538
|
+
is_internal: input.is_internal,
|
|
23539
|
+
is_active: input.is_active,
|
|
23540
|
+
limit: input.limit,
|
|
23541
|
+
offset: input.offset
|
|
23542
|
+
});
|
|
23543
|
+
}
|
|
23544
|
+
},
|
|
23545
|
+
{
|
|
23546
|
+
name: "get_product_category",
|
|
23547
|
+
description: "\u6309\u5206\u7C7B id\u3001handle \u6216\u540D\u79F0\u83B7\u53D6\u5206\u7C7B\u8BE6\u60C5\uFF08GET /admin/product-categories/:id\uFF09\u3002\u4F20 handle/name \u4F1A\u5148\u89E3\u6790\u4E3A id\u3002",
|
|
23548
|
+
schema: { id_or_handle_or_name: external_exports.string() },
|
|
23549
|
+
async run(input, { client: client2 }) {
|
|
23550
|
+
const id = await client2.resolveProductCategoryId(input.id_or_handle_or_name);
|
|
23551
|
+
if (!id) return `\u9519\u8BEF\uFF1A\u627E\u4E0D\u5230\u5206\u7C7B "${input.id_or_handle_or_name}"\uFF08\u7528 list_product_categories \u67E5\u770B\u53EF\u7528\u5206\u7C7B\uFF09`;
|
|
23552
|
+
return client2.getProductCategory(id);
|
|
23553
|
+
}
|
|
23554
|
+
},
|
|
23555
|
+
{
|
|
23556
|
+
name: "create_product_category",
|
|
23557
|
+
description: `\u65B0\u5EFA\u4E00\u4E2A\u4EA7\u54C1\u5206\u7C7B\u3002
|
|
23558
|
+
handle \u4EC5\u542B\u5C0F\u5199\u5B57\u6BCD/\u6570\u5B57/\u8FDE\u5B57\u7B26\uFF0C\u5DE5\u5177\u81EA\u52A8\u5F52\u4E00\u5316\uFF1B\u4E0D\u586B\u5219\u7531 name \u751F\u6210\u3002
|
|
23559
|
+
parent_category_id \u53EF\u4F20\u7236\u5206\u7C7B id \u6216\u5176\u540D\u79F0/handle\uFF08\u81EA\u52A8\u89E3\u6790\uFF09\u3002is_internal \u9ED8\u8BA4 false\uFF0Cis_active \u9ED8\u8BA4 true\u3002
|
|
23560
|
+
\u591A\u8BED\u8A00\uFF1Atranslations \u53EF\u4F20 { "zh-CN": {"name":"...","description":"..."}, ... }\uFF08\u77ED\u7801/\u533A\u57DF\u7801\u5747\u53EF\uFF0C\u7EDF\u4E00\u5F52\u4E00\u5316\u4E3A BCP-47 \u843D\u5E93\uFF09\uFF0C\u8D70\u72EC\u7ACB\u7684\u7FFB\u8BD1\u7AEF\u70B9\uFF0C\u5931\u8D25\u4E0D\u963B\u65AD\u5206\u7C7B\u521B\u5EFA\uFF08\u4F1A\u8FD4\u56DE warning\uFF09\u3002
|
|
23561
|
+
\u9ED8\u8BA4 dry_run\uFF08confirm=false\uFF09\u4EC5\u9884\u89C8\uFF0Cconfirm=true \u624D\u771F\u6B63\u521B\u5EFA\u3002`,
|
|
23562
|
+
schema: {
|
|
23563
|
+
name: external_exports.string().describe("\u5206\u7C7B\u540D\u79F0\uFF08\u5FC5\u586B\uFF09"),
|
|
23564
|
+
handle: external_exports.string().optional().describe("\u4E0D\u586B\u5219\u7531 name \u5F52\u4E00\u5316\u751F\u6210"),
|
|
23565
|
+
description: external_exports.string().optional(),
|
|
23566
|
+
parent_category_id: external_exports.string().optional().describe("\u7236\u5206\u7C7B id / \u540D\u79F0 / handle"),
|
|
23567
|
+
is_internal: external_exports.boolean().default(false).describe("\u662F\u5426\u4EC5\u5185\u90E8\u53EF\u89C1\uFF0C\u9ED8\u8BA4 false"),
|
|
23568
|
+
is_active: external_exports.boolean().default(true).describe("\u662F\u5426\u542F\u7528\uFF0C\u9ED8\u8BA4 true"),
|
|
23569
|
+
external_id: external_exports.string().optional(),
|
|
23570
|
+
metadata: external_exports.record(external_exports.string(), external_exports.any()).optional().describe("\u4EFB\u610F\u952E\u503C\u5143\u6570\u636E"),
|
|
23571
|
+
rank: external_exports.number().int().optional().describe("\u540C\u7EA7\u6392\u5E8F\u503C"),
|
|
23572
|
+
translations: external_exports.record(external_exports.string(), categoryTranslationShape).optional(),
|
|
23573
|
+
confirm: external_exports.boolean().default(false)
|
|
23574
|
+
},
|
|
23575
|
+
async run(input, { client: client2 }) {
|
|
23576
|
+
const handle = normalizeHandle(input.handle || input.name);
|
|
23577
|
+
if (!HANDLE_RE.test(handle)) {
|
|
23578
|
+
return `\u9519\u8BEF\uFF1A\u5F52\u4E00\u5316\u540E\u7684 handle "${handle}" \u975E\u6CD5\uFF08\u53EA\u80FD\u5C0F\u5199\u5B57\u6BCD/\u6570\u5B57/\u8FDE\u5B57\u7B26\uFF09\u3002\u8BF7\u68C0\u67E5\u8F93\u5165\u3002`;
|
|
23579
|
+
}
|
|
23580
|
+
let parentId = input.parent_category_id;
|
|
23581
|
+
if (parentId && !parentId.startsWith("pcat_")) {
|
|
23582
|
+
const resolved = await client2.resolveProductCategoryId(parentId);
|
|
23583
|
+
if (!resolved) return `\u9519\u8BEF\uFF1A\u627E\u4E0D\u5230\u7236\u5206\u7C7B "${parentId}"\uFF08\u7528 list_product_categories \u67E5\u770B\uFF09`;
|
|
23584
|
+
parentId = resolved;
|
|
23585
|
+
}
|
|
23586
|
+
let collision = null;
|
|
23587
|
+
try {
|
|
23588
|
+
const existing = await client2.listProductCategories({ handle, limit: 5 });
|
|
23589
|
+
const dup = (existing?.product_categories || []).find((c) => c.handle === handle);
|
|
23590
|
+
if (dup) collision = { id: dup.id, name: dup.name };
|
|
23591
|
+
} catch {
|
|
23592
|
+
}
|
|
23593
|
+
const warnings = [];
|
|
23594
|
+
if (collision) {
|
|
23595
|
+
warnings.push(`handle "${handle}" \u5DF2\u88AB\u5206\u7C7B\u300C${collision.name}\u300D(${collision.id}) \u5360\u7528`);
|
|
23596
|
+
if (input.confirm) {
|
|
23597
|
+
return `\u9519\u8BEF\uFF1Ahandle "${handle}" \u5DF2\u88AB\u5206\u7C7B ${collision.id}\uFF08\u300C${collision.name}\u300D\uFF09\u5360\u7528\uFF0C\u65E0\u6CD5\u91CD\u590D\u521B\u5EFA\u3002\u82E5\u662F\u8981\u4FEE\u6539\u8BE5\u5206\u7C7B\u8BF7\u7528 update_product_category\u3002`;
|
|
23598
|
+
}
|
|
23599
|
+
}
|
|
23600
|
+
const entries = toCategoryTranslations(input.translations);
|
|
23601
|
+
const payload = {
|
|
23602
|
+
name: input.name,
|
|
23603
|
+
handle,
|
|
23604
|
+
is_internal: input.is_internal,
|
|
23605
|
+
is_active: input.is_active
|
|
23606
|
+
};
|
|
23607
|
+
if (input.description) payload.description = input.description;
|
|
23608
|
+
if (parentId) payload.parent_category_id = parentId;
|
|
23609
|
+
if (input.external_id) payload.external_id = input.external_id;
|
|
23610
|
+
if (input.metadata) payload.metadata = input.metadata;
|
|
23611
|
+
if (input.rank != null) payload.rank = input.rank;
|
|
23612
|
+
if (!input.confirm) {
|
|
23613
|
+
return {
|
|
23614
|
+
dry_run: true,
|
|
23615
|
+
will_create: payload,
|
|
23616
|
+
translation_locales: entries.map((e) => e.locale_code),
|
|
23617
|
+
warnings,
|
|
23618
|
+
handle_collision: collision,
|
|
23619
|
+
note: "\u672A confirm\uFF0C\u4EC5\u9884\u89C8\u3002\u786E\u8BA4\u8BF7\u8BBE confirm=true\u3002"
|
|
23620
|
+
};
|
|
23621
|
+
}
|
|
23622
|
+
const data = await client2.createProductCategory(payload);
|
|
23623
|
+
const categoryId = data?.product_category?.id || data?.id;
|
|
23624
|
+
let translationResult = null;
|
|
23625
|
+
if (entries.length && categoryId) {
|
|
23626
|
+
try {
|
|
23627
|
+
translationResult = await upsertTranslations(client2, "product_category", categoryId, entries);
|
|
23628
|
+
} catch (e) {
|
|
23629
|
+
warnings.push(`\u7FFB\u8BD1\u5199\u5165\u5931\u8D25\uFF08\u5206\u7C7B\u5DF2\u521B\u5EFA\uFF0C\u53EF\u8865\u8C03 set_translations reference=product_category\uFF09\u2014\u2014 ${e?.message || e}`);
|
|
23630
|
+
}
|
|
23631
|
+
}
|
|
23632
|
+
return { created: data, translations: translationResult, warnings };
|
|
23633
|
+
}
|
|
23634
|
+
},
|
|
23635
|
+
{
|
|
23636
|
+
name: "update_product_category",
|
|
23637
|
+
description: `\u4FEE\u6539\u4EA7\u54C1\u5206\u7C7B\u5B57\u6BB5\uFF08\u540D\u79F0/handle/\u63CF\u8FF0/\u7236\u7EA7/\u542F\u7528\u72B6\u6001/\u5185\u90E8\u53EF\u89C1/metadata/\u6392\u5E8F/\u591A\u8BED\u8A00\uFF09\u3002
|
|
23638
|
+
id \u53EF\u4F20\u5206\u7C7B id\u3001handle \u6216\u540D\u79F0\u3002handle \u81EA\u52A8\u5F52\u4E00\u5316\uFF1Bmetadata \u4E3A\u5408\u5E76\u8BED\u4E49\uFF08\u65B0\u952E\u8986\u76D6\u540C\u540D\u952E\uFF0C\u672A\u63D0\u53CA\u7684\u65E7\u952E\u4FDD\u7559\uFF09\u3002
|
|
23639
|
+
parent_category_id \u4F20\u7A7A\u5B57\u7B26\u4E32\u53EF\u89E3\u7ED1\u7236\u7EA7\u3002translations \u4E3A upsert\uFF08\u540C\u8BED\u8A00\u5408\u5E76\u66F4\u65B0\uFF0C\u4E0D\u4EA7\u751F\u91CD\u590D\u884C\uFF09\u3002\u9ED8\u8BA4 dry_run\u3002`,
|
|
23640
|
+
schema: {
|
|
23641
|
+
id: external_exports.string().describe("\u5206\u7C7B id / handle / \u540D\u79F0"),
|
|
23642
|
+
name: external_exports.string().optional(),
|
|
23643
|
+
handle: external_exports.string().optional(),
|
|
23644
|
+
description: external_exports.string().optional(),
|
|
23645
|
+
parent_category_id: external_exports.string().optional().describe("\u7236\u5206\u7C7B id / \u540D\u79F0 / handle\uFF1B\u4F20\u7A7A\u5B57\u7B26\u4E32\u89E3\u7ED1\u7236\u7EA7"),
|
|
23646
|
+
is_internal: external_exports.boolean().optional(),
|
|
23647
|
+
is_active: external_exports.boolean().optional(),
|
|
23648
|
+
external_id: external_exports.string().optional(),
|
|
23649
|
+
metadata: external_exports.record(external_exports.string(), external_exports.any()).optional().describe("\u5408\u5E76\u8FDB\u73B0\u6709 metadata\uFF08\u4E0D\u6574\u4F53\u8986\u76D6\uFF09"),
|
|
23650
|
+
rank: external_exports.number().int().optional(),
|
|
23651
|
+
translations: external_exports.record(external_exports.string(), categoryTranslationShape).optional(),
|
|
23652
|
+
confirm: external_exports.boolean().default(false)
|
|
23653
|
+
},
|
|
23654
|
+
async run(input, { client: client2 }) {
|
|
23655
|
+
const categoryId = await client2.resolveProductCategoryId(input.id);
|
|
23656
|
+
if (!categoryId) return `\u9519\u8BEF\uFF1A\u627E\u4E0D\u5230\u5206\u7C7B "${input.id}"\uFF08\u7528 list_product_categories \u67E5\u770B\uFF09`;
|
|
23657
|
+
const patch = {};
|
|
23658
|
+
if (input.name) patch.name = input.name;
|
|
23659
|
+
if (input.handle) {
|
|
23660
|
+
const h = normalizeHandle(input.handle);
|
|
23661
|
+
if (!HANDLE_RE.test(h)) return `\u9519\u8BEF\uFF1A\u5F52\u4E00\u5316 handle "${h}" \u975E\u6CD5\u3002`;
|
|
23662
|
+
patch.handle = h;
|
|
23663
|
+
}
|
|
23664
|
+
if (input.description) patch.description = input.description;
|
|
23665
|
+
if (input.parent_category_id !== void 0) {
|
|
23666
|
+
if (input.parent_category_id === "") {
|
|
23667
|
+
patch.parent_category_id = null;
|
|
23668
|
+
} else if (!input.parent_category_id.startsWith("pcat_")) {
|
|
23669
|
+
const resolved = await client2.resolveProductCategoryId(input.parent_category_id);
|
|
23670
|
+
if (!resolved) return `\u9519\u8BEF\uFF1A\u627E\u4E0D\u5230\u7236\u5206\u7C7B "${input.parent_category_id}"`;
|
|
23671
|
+
patch.parent_category_id = resolved;
|
|
23672
|
+
} else {
|
|
23673
|
+
patch.parent_category_id = input.parent_category_id;
|
|
23674
|
+
}
|
|
23675
|
+
}
|
|
23676
|
+
if (input.is_internal !== void 0) patch.is_internal = input.is_internal;
|
|
23677
|
+
if (input.is_active !== void 0) patch.is_active = input.is_active;
|
|
23678
|
+
if (input.external_id) patch.external_id = input.external_id;
|
|
23679
|
+
if (input.rank != null) patch.rank = input.rank;
|
|
23680
|
+
if (input.metadata && Object.keys(input.metadata).length) {
|
|
23681
|
+
const cur = await client2.getProductCategory(categoryId).catch(() => null);
|
|
23682
|
+
const existingMeta = (cur?.product_category ?? cur)?.metadata || {};
|
|
23683
|
+
patch.metadata = { ...existingMeta, ...input.metadata };
|
|
23684
|
+
}
|
|
23685
|
+
const entries = toCategoryTranslations(input.translations);
|
|
23686
|
+
if (!input.confirm) {
|
|
23687
|
+
return { dry_run: true, will_update: { id: categoryId, ...patch }, translation_locales: entries.map((e) => e.locale_code) };
|
|
23688
|
+
}
|
|
23689
|
+
const data = await client2.updateProductCategory(categoryId, patch);
|
|
23690
|
+
let translationResult = null;
|
|
23691
|
+
if (entries.length) {
|
|
23692
|
+
try {
|
|
23693
|
+
translationResult = await upsertTranslations(client2, "product_category", categoryId, entries);
|
|
23694
|
+
} catch (e) {
|
|
23695
|
+
return { updated: data, warning_translations: `\u7FFB\u8BD1\u5199\u5165\u5931\u8D25 \u2014\u2014 ${e?.message || e}` };
|
|
23696
|
+
}
|
|
23697
|
+
}
|
|
23698
|
+
return { updated: data, translations: translationResult };
|
|
23699
|
+
}
|
|
23700
|
+
},
|
|
23701
|
+
{
|
|
23702
|
+
name: "delete_product_category",
|
|
23703
|
+
description: "\u5220\u9664\u4E00\u4E2A\u4EA7\u54C1\u5206\u7C7B\uFF08DELETE /admin/product-categories/:id\uFF09\u3002id \u53EF\u4F20\u5206\u7C7B id\u3001handle \u6216\u540D\u79F0\u3002\u26A0\uFE0F \u6709\u5B50\u5206\u7C7B\u6216\u5DF2\u6302\u5546\u54C1\u7684\u5206\u7C7B\u5220\u9664\u53EF\u80FD\u5931\u8D25\uFF0C\u9700\u5148\u8C03\u6574\u3002\u9ED8\u8BA4 dry_run\u3002",
|
|
23704
|
+
schema: {
|
|
23705
|
+
id: external_exports.string().describe("\u5206\u7C7B id / handle / \u540D\u79F0"),
|
|
23706
|
+
confirm: external_exports.boolean().default(false)
|
|
23707
|
+
},
|
|
23708
|
+
async run(input, { client: client2 }) {
|
|
23709
|
+
const categoryId = await client2.resolveProductCategoryId(input.id);
|
|
23710
|
+
if (!categoryId) return `\u9519\u8BEF\uFF1A\u627E\u4E0D\u5230\u5206\u7C7B "${input.id}"\uFF08\u7528 list_product_categories \u67E5\u770B\uFF09`;
|
|
23711
|
+
if (!input.confirm) {
|
|
23712
|
+
return { dry_run: true, will_delete: { id: categoryId, input: input.id }, note: "\u672A confirm\uFF0C\u4EC5\u9884\u89C8\u3002\u786E\u8BA4\u8BF7\u8BBE confirm=true\u3002" };
|
|
23713
|
+
}
|
|
23714
|
+
return client2.deleteProductCategory(categoryId);
|
|
23715
|
+
}
|
|
23716
|
+
}
|
|
23717
|
+
];
|
|
23718
|
+
|
|
23719
|
+
// src/tools/collections.ts
|
|
23720
|
+
var collectionTranslationShape = external_exports.object({
|
|
23721
|
+
title: external_exports.string().optional()
|
|
23722
|
+
});
|
|
23723
|
+
function toCollectionTranslations(raw) {
|
|
23724
|
+
return Object.entries(raw ?? {}).map(([locale, t]) => ({
|
|
23725
|
+
locale_code: canonLocaleKey(locale),
|
|
23726
|
+
translations: Object.fromEntries(
|
|
23727
|
+
Object.entries(t ?? {}).filter(([, v]) => v !== void 0 && v !== null)
|
|
23728
|
+
)
|
|
23729
|
+
}));
|
|
23730
|
+
}
|
|
23731
|
+
var collectionTools = [
|
|
23732
|
+
{
|
|
23733
|
+
name: "get_collection",
|
|
23734
|
+
description: "\u6309\u7CFB\u5217 id\u3001handle \u6216\u6807\u9898\u83B7\u53D6\u7CFB\u5217\u8BE6\u60C5\uFF08GET /admin/collections/:id\uFF09\u3002\u4F20 handle/title \u4F1A\u5148\u89E3\u6790\u4E3A id\u3002",
|
|
23735
|
+
schema: { id_or_handle_or_title: external_exports.string() },
|
|
23736
|
+
async run(input, { client: client2 }) {
|
|
23737
|
+
const id = await client2.resolveCollectionId(input.id_or_handle_or_title);
|
|
23738
|
+
if (!id) return `\u9519\u8BEF\uFF1A\u627E\u4E0D\u5230\u7CFB\u5217 "${input.id_or_handle_or_title}"\uFF08\u7528 list_collections \u67E5\u770B\u53EF\u7528\u7CFB\u5217\uFF09`;
|
|
23739
|
+
return client2.getCollection(id);
|
|
23740
|
+
}
|
|
23741
|
+
},
|
|
23742
|
+
{
|
|
23743
|
+
name: "create_collection",
|
|
23744
|
+
description: `\u65B0\u5EFA\u4E00\u4E2A\u4EA7\u54C1\u7CFB\u5217 / \u96C6\u5408\uFF08\u5982\u6D3B\u52A8\u96C6\u5408\u3001\u9996\u9875\u7CBE\u9009\u96C6\u5408 featured\uFF09\u3002
|
|
23745
|
+
handle \u4EC5\u542B\u5C0F\u5199\u5B57\u6BCD/\u6570\u5B57/\u8FDE\u5B57\u7B26\uFF0C\u5DE5\u5177\u81EA\u52A8\u5F52\u4E00\u5316\uFF1B\u4E0D\u586B\u5219\u7531 title \u751F\u6210\u3002
|
|
23746
|
+
\u26A0\uFE0F \u5B98\u65B9 collection validator \u6CA1\u6709 description \u5B57\u6BB5\uFF0C\u5982\u9700\u63CF\u8FF0\u8BF7\u653E metadata\uFF08\u5982 {"description":"..."}\uFF09\u3002
|
|
23747
|
+
\u591A\u8BED\u8A00\uFF1Atranslations \u53EF\u4F20 { "zh-CN": {"title":"..."}, ... }\uFF08\u77ED\u7801/\u533A\u57DF\u7801\u5747\u53EF\uFF0C\u7EDF\u4E00\u5F52\u4E00\u5316\u4E3A BCP-47 \u843D\u5E93\uFF09\uFF0C\u8D70\u72EC\u7ACB\u7FFB\u8BD1\u7AEF\u70B9\uFF0C\u5931\u8D25\u4E0D\u963B\u65AD\u521B\u5EFA\u3002
|
|
23748
|
+
\u9ED8\u8BA4 dry_run\uFF08confirm=false\uFF09\u4EC5\u9884\u89C8\uFF0Cconfirm=true \u624D\u771F\u6B63\u521B\u5EFA\u3002`,
|
|
23749
|
+
schema: {
|
|
23750
|
+
title: external_exports.string().describe("\u7CFB\u5217\u6807\u9898\uFF08\u5FC5\u586B\uFF09"),
|
|
23751
|
+
handle: external_exports.string().optional().describe("\u4E0D\u586B\u5219\u7531 title \u5F52\u4E00\u5316\u751F\u6210"),
|
|
23752
|
+
external_id: external_exports.string().optional(),
|
|
23753
|
+
metadata: external_exports.record(external_exports.string(), external_exports.any()).optional().describe('\u4EFB\u610F\u952E\u503C\u5143\u6570\u636E\uFF0C\u5982 {"is_navigable":true,"description":"..."}'),
|
|
23754
|
+
translations: external_exports.record(external_exports.string(), collectionTranslationShape).optional(),
|
|
23755
|
+
confirm: external_exports.boolean().default(false)
|
|
23756
|
+
},
|
|
23757
|
+
async run(input, { client: client2 }) {
|
|
23758
|
+
const handle = normalizeHandle(input.handle || input.title);
|
|
23759
|
+
if (!HANDLE_RE.test(handle)) {
|
|
23760
|
+
return `\u9519\u8BEF\uFF1A\u5F52\u4E00\u5316\u540E\u7684 handle "${handle}" \u975E\u6CD5\uFF08\u53EA\u80FD\u5C0F\u5199\u5B57\u6BCD/\u6570\u5B57/\u8FDE\u5B57\u7B26\uFF09\u3002\u8BF7\u68C0\u67E5\u8F93\u5165\u3002`;
|
|
23761
|
+
}
|
|
23762
|
+
let collision = null;
|
|
23763
|
+
try {
|
|
23764
|
+
const existing = await client2.listCollections({ handle, limit: 5 });
|
|
23765
|
+
const dup = (existing?.collections || []).find((c) => c.handle === handle);
|
|
23766
|
+
if (dup) collision = { id: dup.id, title: dup.title };
|
|
23767
|
+
} catch {
|
|
23768
|
+
}
|
|
23769
|
+
const warnings = [];
|
|
23770
|
+
if (collision) {
|
|
23771
|
+
warnings.push(`handle "${handle}" \u5DF2\u88AB\u7CFB\u5217\u300C${collision.title}\u300D(${collision.id}) \u5360\u7528`);
|
|
23772
|
+
if (input.confirm) {
|
|
23773
|
+
return `\u9519\u8BEF\uFF1Ahandle "${handle}" \u5DF2\u88AB\u7CFB\u5217 ${collision.id}\uFF08\u300C${collision.title}\u300D\uFF09\u5360\u7528\uFF0C\u65E0\u6CD5\u91CD\u590D\u521B\u5EFA\u3002\u82E5\u662F\u8981\u4FEE\u6539\u8BE5\u7CFB\u5217\u8BF7\u7528 update_collection\u3002`;
|
|
23774
|
+
}
|
|
23775
|
+
}
|
|
23776
|
+
const entries = toCollectionTranslations(input.translations);
|
|
23777
|
+
const payload = { title: input.title, handle };
|
|
23778
|
+
if (input.external_id) payload.external_id = input.external_id;
|
|
23779
|
+
if (input.metadata) payload.metadata = input.metadata;
|
|
23780
|
+
if (!input.confirm) {
|
|
23781
|
+
return {
|
|
23782
|
+
dry_run: true,
|
|
23783
|
+
will_create: payload,
|
|
23784
|
+
translation_locales: entries.map((e) => e.locale_code),
|
|
23785
|
+
warnings,
|
|
23786
|
+
handle_collision: collision,
|
|
23787
|
+
note: "\u672A confirm\uFF0C\u4EC5\u9884\u89C8\u3002\u786E\u8BA4\u8BF7\u8BBE confirm=true\u3002"
|
|
23788
|
+
};
|
|
23789
|
+
}
|
|
23790
|
+
const data = await client2.createCollection(payload);
|
|
23791
|
+
const collectionId = data?.collection?.id || data?.id;
|
|
23792
|
+
let translationResult = null;
|
|
23793
|
+
if (entries.length && collectionId) {
|
|
23794
|
+
try {
|
|
23795
|
+
translationResult = await upsertTranslations(client2, "product_collection", collectionId, entries);
|
|
23796
|
+
} catch (e) {
|
|
23797
|
+
warnings.push(`\u7FFB\u8BD1\u5199\u5165\u5931\u8D25\uFF08\u7CFB\u5217\u5DF2\u521B\u5EFA\uFF0C\u53EF\u8865\u8C03 set_translations reference=product_collection\uFF09\u2014\u2014 ${e?.message || e}`);
|
|
23798
|
+
}
|
|
23799
|
+
}
|
|
23800
|
+
return { created: data, translations: translationResult, warnings };
|
|
23801
|
+
}
|
|
23802
|
+
},
|
|
23803
|
+
{
|
|
23804
|
+
name: "update_collection",
|
|
23805
|
+
description: `\u4FEE\u6539\u4EA7\u54C1\u7CFB\u5217\u5B57\u6BB5\uFF08\u6807\u9898/handle/external_id/metadata/\u591A\u8BED\u8A00\uFF09\u3002id \u53EF\u4F20\u7CFB\u5217 id\u3001handle \u6216\u6807\u9898\u3002
|
|
23806
|
+
handle \u81EA\u52A8\u5F52\u4E00\u5316\uFF1Bmetadata \u4E3A\u5408\u5E76\u8BED\u4E49\uFF08\u65B0\u952E\u8986\u76D6\u540C\u540D\u952E\uFF0C\u672A\u63D0\u53CA\u7684\u65E7\u952E\u4FDD\u7559\uFF09\u2014\u2014\u672C\u9879\u76EE\u540E\u53F0\u300C\u5BFC\u822A\u663E\u793A\u300D\u5F00\u5173\u5C31\u5B58\u5728 metadata.is_navigable\u3002
|
|
23807
|
+
translations \u4E3A upsert\uFF08\u540C\u8BED\u8A00\u5408\u5E76\u66F4\u65B0\uFF0C\u4E0D\u4EA7\u751F\u91CD\u590D\u884C\uFF09\u3002\u9ED8\u8BA4 dry_run\u3002`,
|
|
23808
|
+
schema: {
|
|
23809
|
+
id: external_exports.string().describe("\u7CFB\u5217 id / handle / \u6807\u9898"),
|
|
23810
|
+
title: external_exports.string().optional(),
|
|
23811
|
+
handle: external_exports.string().optional(),
|
|
23812
|
+
external_id: external_exports.string().optional(),
|
|
23813
|
+
metadata: external_exports.record(external_exports.string(), external_exports.any()).optional().describe("\u5408\u5E76\u8FDB\u73B0\u6709 metadata\uFF08\u4E0D\u6574\u4F53\u8986\u76D6\uFF09"),
|
|
23814
|
+
translations: external_exports.record(external_exports.string(), collectionTranslationShape).optional(),
|
|
23815
|
+
confirm: external_exports.boolean().default(false)
|
|
23816
|
+
},
|
|
23817
|
+
async run(input, { client: client2 }) {
|
|
23818
|
+
const collectionId = await client2.resolveCollectionId(input.id);
|
|
23819
|
+
if (!collectionId) return `\u9519\u8BEF\uFF1A\u627E\u4E0D\u5230\u7CFB\u5217 "${input.id}"\uFF08\u7528 list_collections \u67E5\u770B\uFF09`;
|
|
23820
|
+
const patch = {};
|
|
23821
|
+
if (input.title) patch.title = input.title;
|
|
23822
|
+
if (input.handle) {
|
|
23823
|
+
const h = normalizeHandle(input.handle);
|
|
23824
|
+
if (!HANDLE_RE.test(h)) return `\u9519\u8BEF\uFF1A\u5F52\u4E00\u5316 handle "${h}" \u975E\u6CD5\u3002`;
|
|
23825
|
+
patch.handle = h;
|
|
23826
|
+
}
|
|
23827
|
+
if (input.external_id) patch.external_id = input.external_id;
|
|
23828
|
+
if (input.metadata && Object.keys(input.metadata).length) {
|
|
23829
|
+
const cur = await client2.getCollection(collectionId).catch(() => null);
|
|
23830
|
+
const existingMeta = (cur?.collection ?? cur)?.metadata || {};
|
|
23831
|
+
patch.metadata = { ...existingMeta, ...input.metadata };
|
|
23832
|
+
}
|
|
23833
|
+
const entries = toCollectionTranslations(input.translations);
|
|
23834
|
+
if (!input.confirm) {
|
|
23835
|
+
return { dry_run: true, will_update: { id: collectionId, ...patch }, translation_locales: entries.map((e) => e.locale_code) };
|
|
23836
|
+
}
|
|
23837
|
+
const data = await client2.updateCollection(collectionId, patch);
|
|
23838
|
+
let translationResult = null;
|
|
23839
|
+
if (entries.length) {
|
|
23840
|
+
try {
|
|
23841
|
+
translationResult = await upsertTranslations(client2, "product_collection", collectionId, entries);
|
|
23842
|
+
} catch (e) {
|
|
23843
|
+
return { updated: data, warning_translations: `\u7FFB\u8BD1\u5199\u5165\u5931\u8D25 \u2014\u2014 ${e?.message || e}` };
|
|
23844
|
+
}
|
|
23845
|
+
}
|
|
23846
|
+
return { updated: data, translations: translationResult };
|
|
23847
|
+
}
|
|
23848
|
+
},
|
|
23849
|
+
{
|
|
23850
|
+
name: "delete_collection",
|
|
23851
|
+
description: "\u5220\u9664\u4E00\u4E2A\u4EA7\u54C1\u7CFB\u5217 / \u96C6\u5408\uFF08DELETE /admin/collections/:id\uFF09\u3002id \u53EF\u4F20\u7CFB\u5217 id\u3001handle \u6216\u6807\u9898\u3002\u26A0\uFE0F \u5DF2\u6302\u5546\u54C1\u7684\u7CFB\u5217\u5220\u9664\u884C\u4E3A\u53D6\u51B3\u4E8E\u540E\u7AEF\u5DE5\u4F5C\u6D41\uFF0C\u9700\u5148\u786E\u8BA4\u3002\u9ED8\u8BA4 dry_run\u3002",
|
|
23852
|
+
schema: {
|
|
23853
|
+
id: external_exports.string().describe("\u7CFB\u5217 id / handle / \u6807\u9898"),
|
|
23854
|
+
confirm: external_exports.boolean().default(false)
|
|
23855
|
+
},
|
|
23856
|
+
async run(input, { client: client2 }) {
|
|
23857
|
+
const collectionId = await client2.resolveCollectionId(input.id);
|
|
23858
|
+
if (!collectionId) return `\u9519\u8BEF\uFF1A\u627E\u4E0D\u5230\u7CFB\u5217 "${input.id}"\uFF08\u7528 list_collections \u67E5\u770B\uFF09`;
|
|
23859
|
+
if (!input.confirm) {
|
|
23860
|
+
return { dry_run: true, will_delete: { id: collectionId, input: input.id }, note: "\u672A confirm\uFF0C\u4EC5\u9884\u89C8\u3002\u786E\u8BA4\u8BF7\u8BBE confirm=true\u3002" };
|
|
23861
|
+
}
|
|
23862
|
+
return client2.deleteCollection(collectionId);
|
|
23226
23863
|
}
|
|
23227
23864
|
}
|
|
23228
23865
|
];
|
|
@@ -23280,12 +23917,12 @@ var opsTools = [
|
|
|
23280
23917
|
|
|
23281
23918
|
// src/resources/docs.ts
|
|
23282
23919
|
import { readdirSync, readFileSync as readFileSync3, existsSync as existsSync3, statSync } from "node:fs";
|
|
23283
|
-
import { join as
|
|
23920
|
+
import { join as join3 } from "node:path";
|
|
23284
23921
|
var cache = null;
|
|
23285
23922
|
function walk(dir, out = []) {
|
|
23286
23923
|
if (!existsSync3(dir)) return out;
|
|
23287
23924
|
for (const entry of readdirSync(dir)) {
|
|
23288
|
-
const full =
|
|
23925
|
+
const full = join3(dir, entry);
|
|
23289
23926
|
try {
|
|
23290
23927
|
if (statSync(full).isDirectory()) walk(full, out);
|
|
23291
23928
|
else if (full.endsWith(".md")) out.push(full);
|
|
@@ -23438,6 +24075,20 @@ var labelsShape = external_exports.object({
|
|
|
23438
24075
|
sku: external_exports.string(),
|
|
23439
24076
|
series: external_exports.string()
|
|
23440
24077
|
});
|
|
24078
|
+
var optionShape = external_exports.object({
|
|
24079
|
+
title: external_exports.string().describe('\u9009\u9879\u6807\u9898\uFF0C\u5982 "Fiber Type" / "Handle Type"'),
|
|
24080
|
+
values: external_exports.array(external_exports.string().min(1)).min(1).describe("\u8BE5\u9009\u9879\u4E0B\u7684\u5168\u90E8\u53D6\u503C\uFF08\u53BB\u91CD\u540E\u5199\u5165\uFF09")
|
|
24081
|
+
});
|
|
24082
|
+
var variantSpecShape = external_exports.object({
|
|
24083
|
+
sku: external_exports.string().optional().describe("\u53D8\u4F53 SKU\uFF08\u5E93\u5B58/\u5C65\u7EA6\u5173\u952E\u6807\u8BC6\uFF0C\u5EFA\u8BAE\u5FC5\u586B\uFF09"),
|
|
24084
|
+
title: external_exports.string().optional().describe('\u53D8\u4F53\u6807\u9898\uFF0C\u7701\u7565\u65F6\u7531 options \u53D6\u503C\u62FC\u63A5\uFF08\u5982 "ALC / Penhold"\uFF09'),
|
|
24085
|
+
options: external_exports.record(external_exports.string(), external_exports.string()).describe('\u9009\u9879\u6807\u9898\u2192\u53D6\u503C\uFF0C\u5982 {"Fiber Type":"ALC","Handle Type":"Penhold"}'),
|
|
24086
|
+
price: external_exports.number().nonnegative().optional(),
|
|
24087
|
+
currency_code: external_exports.string().optional(),
|
|
24088
|
+
prices: external_exports.array(priceShape).optional(),
|
|
24089
|
+
manage_inventory: external_exports.boolean().optional(),
|
|
24090
|
+
initial_stock: stockShape.optional()
|
|
24091
|
+
});
|
|
23441
24092
|
var productSpecShape = external_exports.object({
|
|
23442
24093
|
handle: external_exports.string(),
|
|
23443
24094
|
title: external_exports.string(),
|
|
@@ -23463,7 +24114,15 @@ var productSpecShape = external_exports.object({
|
|
|
23463
24114
|
material: external_exports.string().optional(),
|
|
23464
24115
|
manage_inventory: external_exports.boolean().optional(),
|
|
23465
24116
|
initial_stock: stockShape.optional(),
|
|
23466
|
-
content: external_exports.record(external_exports.string(), external_exports.array(blockShape)).optional()
|
|
24117
|
+
content: external_exports.record(external_exports.string(), external_exports.array(blockShape)).optional(),
|
|
24118
|
+
/**
|
|
24119
|
+
* 商品选项(多变体时必填/可推导)。与 variants 配合:每款商品可声明若干选项,
|
|
24120
|
+
* 每个变体用 options 映射指定其在各选项下的取值。省略时由 variants 自动推导。
|
|
24121
|
+
* ⚠️ 仅声明 options 而无 variants 不支持(单 SKU 商品无需选项)。
|
|
24122
|
+
*/
|
|
24123
|
+
options: external_exports.array(optionShape).optional(),
|
|
24124
|
+
/** 多变体:每项为一个变体(含 options 映射 + 自身价格/SKU/库存)。与 options 配合。 */
|
|
24125
|
+
variants: external_exports.array(variantSpecShape).optional()
|
|
23467
24126
|
});
|
|
23468
24127
|
var optionsShape = external_exports.object({
|
|
23469
24128
|
on_duplicate: external_exports.enum(["error", "skip", "update", "archive"]).default("update"),
|
|
@@ -23574,6 +24233,40 @@ function buildPrices(p) {
|
|
|
23574
24233
|
if (p.price != null) return [{ amount: p.price, currency_code: p.currency_code || "MYR" }];
|
|
23575
24234
|
return [];
|
|
23576
24235
|
}
|
|
24236
|
+
function deriveOptions(variants) {
|
|
24237
|
+
const titleToValues = /* @__PURE__ */ new Map();
|
|
24238
|
+
for (const v of variants) {
|
|
24239
|
+
for (const [title, value] of Object.entries(v.options || {})) {
|
|
24240
|
+
const arr = titleToValues.get(title) || [];
|
|
24241
|
+
if (!arr.includes(value)) arr.push(value);
|
|
24242
|
+
titleToValues.set(title, arr);
|
|
24243
|
+
}
|
|
24244
|
+
}
|
|
24245
|
+
return [...titleToValues.entries()].map(([title, values]) => ({ title, values }));
|
|
24246
|
+
}
|
|
24247
|
+
function optionSignature(v) {
|
|
24248
|
+
const entries = Object.entries(v.options || {}).sort((a, b) => a[0].localeCompare(b[0]));
|
|
24249
|
+
return entries.map(([k, val]) => `${k}=${val}`).join("|");
|
|
24250
|
+
}
|
|
24251
|
+
function optionSignatureFromApi(variant, optionTitleById) {
|
|
24252
|
+
const rec = {};
|
|
24253
|
+
for (const o of variant.options || []) {
|
|
24254
|
+
const title = o?.option_id ? optionTitleById.get(String(o.option_id)) : void 0;
|
|
24255
|
+
if (title && o?.value != null) rec[title] = String(o.value);
|
|
24256
|
+
}
|
|
24257
|
+
return optionSignature(rec);
|
|
24258
|
+
}
|
|
24259
|
+
function collectMissingOptionValues(currentOptions, specOptions) {
|
|
24260
|
+
const out = [];
|
|
24261
|
+
for (const so of specOptions) {
|
|
24262
|
+
const cur = (currentOptions || []).find((o) => String(o?.title) === so.title);
|
|
24263
|
+
if (!cur?.id) continue;
|
|
24264
|
+
const existing = new Set((cur.values || []).map((v) => String(v?.value)));
|
|
24265
|
+
const missing = [...new Set(so.values)].filter((v) => !existing.has(v));
|
|
24266
|
+
if (missing.length) out.push({ title: so.title, product_option_id: String(cur.id), values: missing });
|
|
24267
|
+
}
|
|
24268
|
+
return out;
|
|
24269
|
+
}
|
|
23577
24270
|
function buildAutoContent(p, opts, imageUrl) {
|
|
23578
24271
|
const labelsFor = (locale) => {
|
|
23579
24272
|
const short = SHORT_OF[locale] || locale.split("-")[0];
|
|
@@ -23635,9 +24328,10 @@ async function buildPlan(ctx2, spec) {
|
|
|
23635
24328
|
for (const p of spec.products) {
|
|
23636
24329
|
const item = {
|
|
23637
24330
|
handle: p.handle,
|
|
23638
|
-
sku: p.sku,
|
|
24331
|
+
sku: p.sku || p.variants?.[0]?.sku,
|
|
23639
24332
|
title: p.title,
|
|
23640
24333
|
action: "create",
|
|
24334
|
+
variant_count: p.variants?.length || void 0,
|
|
23641
24335
|
warnings: []
|
|
23642
24336
|
};
|
|
23643
24337
|
const block = (reason) => {
|
|
@@ -23647,6 +24341,23 @@ async function buildPlan(ctx2, spec) {
|
|
|
23647
24341
|
if (!HANDLE_RE2.test(p.handle)) {
|
|
23648
24342
|
block(`handle "${p.handle}" \u5F52\u4E00\u5316\u540E\u975E\u6CD5\uFF0C\u8DF3\u8FC7\u8BE5\u6B3E`);
|
|
23649
24343
|
}
|
|
24344
|
+
if (p.variants?.length) {
|
|
24345
|
+
const optTitles = new Set((p.options || deriveOptions(p.variants)).map((o) => o.title));
|
|
24346
|
+
for (const v of p.variants) {
|
|
24347
|
+
if (!v.options || !Object.keys(v.options).length) {
|
|
24348
|
+
block(`\u53D8\u4F53 ${v.sku || "(\u65E0SKU)"} \u7F3A\u5C11 options \u6620\u5C04\uFF0C\u8DF3\u8FC7\u8BE5\u6B3E`);
|
|
24349
|
+
break;
|
|
24350
|
+
}
|
|
24351
|
+
const keys = Object.keys(v.options).sort().join(",");
|
|
24352
|
+
const expected = [...optTitles].sort().join(",");
|
|
24353
|
+
if (keys !== expected) {
|
|
24354
|
+
block(`\u53D8\u4F53 ${v.sku || "(\u65E0SKU)"} \u7684 options \u952E [${keys}] \u4E0E\u5546\u54C1\u9009\u9879 [${expected}] \u4E0D\u4E00\u81F4\uFF0C\u8DF3\u8FC7\u8BE5\u6B3E`);
|
|
24355
|
+
break;
|
|
24356
|
+
}
|
|
24357
|
+
}
|
|
24358
|
+
} else if (p.options?.length) {
|
|
24359
|
+
block("\u4EC5\u58F0\u660E options \u4F46\u65E0 variants\uFF1A\u5355 SKU \u5546\u54C1\u65E0\u9700\u9009\u9879\uFF0C\u591A\u53D8\u4F53\u5FC5\u987B\u540C\u65F6\u7ED9 variants");
|
|
24360
|
+
}
|
|
23650
24361
|
const missing = required2.filter((l) => !p.translations?.[l]?.title);
|
|
23651
24362
|
if (missing.length) item.warnings.push(`\u7F3A\u8BED\u8A00 title: ${missing.join(", ")}`);
|
|
23652
24363
|
for (const img of p.images || []) {
|
|
@@ -23654,7 +24365,7 @@ async function buildPlan(ctx2, spec) {
|
|
|
23654
24365
|
block(`\u56FE\u7247\u4E0D\u5B58\u5728: ${img}\uFF0C\u8DF3\u8FC7\u8BE5\u6B3E`);
|
|
23655
24366
|
}
|
|
23656
24367
|
}
|
|
23657
|
-
if (!buildPrices(p).length) item.warnings.push("\u672A\u7ED9\u4EF7\u683C");
|
|
24368
|
+
if (!p.variants?.length && !buildPrices(p).length) item.warnings.push("\u672A\u7ED9\u4EF7\u683C");
|
|
23658
24369
|
const unknownChannels = (p.sales_channels || []).filter((n) => !channelMap.has(n));
|
|
23659
24370
|
if (unknownChannels.length) {
|
|
23660
24371
|
block(`\u627E\u4E0D\u5230\u6E20\u9053: ${unknownChannels.join(", ")}\uFF0C\u8DF3\u8FC7\u8BE5\u6B3E`);
|
|
@@ -23669,15 +24380,36 @@ async function buildPlan(ctx2, spec) {
|
|
|
23669
24380
|
} catch (e) {
|
|
23670
24381
|
item.warnings.push(`handle \u67E5\u91CD\u5931\u8D25\uFF1A${(e instanceof Error ? e.message : String(e)).slice(0, 100)}`);
|
|
23671
24382
|
}
|
|
23672
|
-
if (!existing &&
|
|
24383
|
+
if (!existing && item.sku) {
|
|
23673
24384
|
try {
|
|
23674
|
-
const d = await client2.listProducts({ q:
|
|
24385
|
+
const d = await client2.listProducts({ q: item.sku, limit: 10 });
|
|
23675
24386
|
existing = (d?.products || []).find(
|
|
23676
|
-
(x) => (x.variants || []).some((v) => v.sku ===
|
|
24387
|
+
(x) => (x.variants || []).some((v) => v.sku === item.sku)
|
|
23677
24388
|
) || null;
|
|
23678
24389
|
} catch {
|
|
23679
24390
|
}
|
|
23680
24391
|
}
|
|
24392
|
+
if (existing && p.variants?.length && item.action !== "blocked") {
|
|
24393
|
+
const specOptions = p.options && p.options.length ? p.options : deriveOptions(p.variants);
|
|
24394
|
+
const specTitles = [...new Set(specOptions.map((o) => o.title))].sort();
|
|
24395
|
+
const curTitles = [
|
|
24396
|
+
...new Set(
|
|
24397
|
+
(existing.options || []).map((o) => String(o?.title || "")).filter(Boolean)
|
|
24398
|
+
)
|
|
24399
|
+
].sort();
|
|
24400
|
+
if (curTitles.join("|") !== specTitles.join("|")) {
|
|
24401
|
+
block(
|
|
24402
|
+
`\u5DF2\u5B58\u5728\u5546\u54C1 ${existing.id} \u7684\u9009\u9879\u7ED3\u6784 [${curTitles.join(", ") || "\u65E0"}] \u4E0E spec [${specTitles.join(", ")}] \u4E0D\u4E00\u81F4\uFF0C\u4E0D\u652F\u6301\u539F\u5730\u6539\u9020\uFF1B\u8BF7\u5148\u4EBA\u5DE5\u8C03\u6574\u9009\u9879\uFF0C\u6216\u7528 on_duplicate=archive \u4E0B\u67B6\u65E7\u6B3E\u540E\u65B0\u5EFA`
|
|
24403
|
+
);
|
|
24404
|
+
} else if (existing.options) {
|
|
24405
|
+
const missingValues = collectMissingOptionValues(existing.options, specOptions);
|
|
24406
|
+
if (missingValues.length) {
|
|
24407
|
+
item.warnings.push(
|
|
24408
|
+
`\u5C06\u65B0\u589E\u9009\u9879\u53D6\u503C\uFF1A${missingValues.map((m) => `${m.title}=${m.values.join("/")}`).join("; ")}`
|
|
24409
|
+
);
|
|
24410
|
+
}
|
|
24411
|
+
}
|
|
24412
|
+
}
|
|
23681
24413
|
const strategy = opts?.on_duplicate ?? "update";
|
|
23682
24414
|
if (existing) {
|
|
23683
24415
|
item.existing_id = existing.id;
|
|
@@ -23692,7 +24424,8 @@ async function buildPlan(ctx2, spec) {
|
|
|
23692
24424
|
else if (strategy === "archive") item.action = "archive+create";
|
|
23693
24425
|
else item.action = "update";
|
|
23694
24426
|
}
|
|
23695
|
-
|
|
24427
|
+
const anyVariantStock = !!p.variants?.some((v) => v.initial_stock);
|
|
24428
|
+
if ((item.action === "create" || item.action === "archive+create") && (p.manage_inventory ?? true) && !(p.initial_stock ?? opts?.default_initial_stock) && !anyVariantStock) {
|
|
23696
24429
|
item.warnings.push("\u672A\u7ED9 initial_stock\uFF0C\u521B\u5EFA\u540E 0 \u5E93\u5B58=\u524D\u53F0\u7F3A\u8D27");
|
|
23697
24430
|
}
|
|
23698
24431
|
items.push(item);
|
|
@@ -23701,11 +24434,12 @@ async function buildPlan(ctx2, spec) {
|
|
|
23701
24434
|
}
|
|
23702
24435
|
async function executeItem(ctx2, p, item, plan, opts) {
|
|
23703
24436
|
const { client: client2 } = ctx2;
|
|
24437
|
+
const warnings = [...item.warnings];
|
|
23704
24438
|
const summary = {
|
|
23705
24439
|
handle: p.handle,
|
|
23706
24440
|
sku: p.sku || null,
|
|
23707
24441
|
action: item.action,
|
|
23708
|
-
warnings
|
|
24442
|
+
warnings
|
|
23709
24443
|
};
|
|
23710
24444
|
if (item.action === "skip" || item.action === "blocked") {
|
|
23711
24445
|
summary.skipped_reason = item.action === "skip" ? "on_duplicate=skip" : "plan blocked";
|
|
@@ -23750,10 +24484,62 @@ async function executeItem(ctx2, p, item, plan, opts) {
|
|
|
23750
24484
|
patch.images = Array.from(/* @__PURE__ */ new Set([...existingUrls, ...imageUrls])).map((u) => ({ url: u }));
|
|
23751
24485
|
}
|
|
23752
24486
|
await client2.updateProduct(productId, patch);
|
|
23753
|
-
if (
|
|
24487
|
+
if (p.variants?.length) {
|
|
24488
|
+
const curVariants = current.variants || [];
|
|
24489
|
+
const optionTitleById = /* @__PURE__ */ new Map();
|
|
24490
|
+
for (const o of current.options || []) {
|
|
24491
|
+
if (o?.id && o?.title) optionTitleById.set(String(o.id), String(o.title));
|
|
24492
|
+
}
|
|
24493
|
+
const specOptions = p.options && p.options.length ? p.options : deriveOptions(p.variants);
|
|
24494
|
+
const missingValues = collectMissingOptionValues(current.options, specOptions);
|
|
24495
|
+
if (missingValues.length) {
|
|
24496
|
+
await client2.addProductOptionValues(
|
|
24497
|
+
productId,
|
|
24498
|
+
missingValues.map((m) => ({
|
|
24499
|
+
product_option_id: m.product_option_id,
|
|
24500
|
+
add: m.values.map((value) => ({ value }))
|
|
24501
|
+
}))
|
|
24502
|
+
);
|
|
24503
|
+
warnings.push(
|
|
24504
|
+
`\u5DF2\u65B0\u589E\u9009\u9879\u53D6\u503C\uFF1A${missingValues.map((m) => `${m.title}=${m.values.join("/")}`).join("; ")}`
|
|
24505
|
+
);
|
|
24506
|
+
}
|
|
24507
|
+
const createdSignatures = /* @__PURE__ */ new Set();
|
|
24508
|
+
for (const v of p.variants) {
|
|
24509
|
+
const vPrices = buildPrices(v).length ? buildPrices(v) : prices;
|
|
24510
|
+
if (!vPrices.length) continue;
|
|
24511
|
+
const sig = optionSignature(v);
|
|
24512
|
+
const match = v.sku ? curVariants.find((x) => x.sku === v.sku) : sig ? curVariants.find((x) => optionSignatureFromApi(x, optionTitleById) === sig) : void 0;
|
|
24513
|
+
if (match) {
|
|
24514
|
+
await client2.updateProductPrices(productId, match.id, vPrices);
|
|
24515
|
+
} else {
|
|
24516
|
+
if (!v.sku) {
|
|
24517
|
+
if (!sig) {
|
|
24518
|
+
warnings.push("\u53D8\u4F53\u65E2\u65E0 SKU \u4E5F\u65E0\u6CD5\u6309\u9009\u9879\u5339\u914D\uFF0C\u8DF3\u8FC7\u8865\u5EFA");
|
|
24519
|
+
continue;
|
|
24520
|
+
}
|
|
24521
|
+
if (createdSignatures.has(sig) || curVariants.some((x) => optionSignatureFromApi(x, optionTitleById) === sig)) {
|
|
24522
|
+
continue;
|
|
24523
|
+
}
|
|
24524
|
+
createdSignatures.add(sig);
|
|
24525
|
+
}
|
|
24526
|
+
await client2.updateProduct(productId, {
|
|
24527
|
+
variants: [
|
|
24528
|
+
{
|
|
24529
|
+
title: v.title || Object.values(v.options).join(" / "),
|
|
24530
|
+
options: v.options,
|
|
24531
|
+
prices: vPrices,
|
|
24532
|
+
manage_inventory: v.manage_inventory ?? p.manage_inventory ?? true,
|
|
24533
|
+
...v.sku ? { sku: v.sku } : {}
|
|
24534
|
+
}
|
|
24535
|
+
]
|
|
24536
|
+
});
|
|
24537
|
+
}
|
|
24538
|
+
}
|
|
24539
|
+
} else if (prices.length) {
|
|
23754
24540
|
const v = (current.variants || [])[0];
|
|
23755
24541
|
if (v) await client2.updateProductPrices(productId, v.id, prices);
|
|
23756
|
-
else
|
|
24542
|
+
else warnings.push("\u5546\u54C1\u6CA1\u6709\u53D8\u4F53\uFF0C\u4EF7\u683C\u672A\u66F4\u65B0");
|
|
23757
24543
|
}
|
|
23758
24544
|
for (const name of p.sales_channels || []) {
|
|
23759
24545
|
const cid = plan.channelMap.get(name);
|
|
@@ -23764,13 +24550,13 @@ async function executeItem(ctx2, p, item, plan, opts) {
|
|
|
23764
24550
|
}
|
|
23765
24551
|
}
|
|
23766
24552
|
} else {
|
|
24553
|
+
const isMulti = !!(p.variants && p.variants.length);
|
|
23767
24554
|
const payload = {
|
|
23768
24555
|
title: p.title,
|
|
23769
24556
|
handle: p.handle,
|
|
23770
24557
|
status: p.status ?? opts?.default_status ?? "draft",
|
|
23771
24558
|
subtitle: p.subtitle,
|
|
23772
24559
|
description: p.description,
|
|
23773
|
-
options: [{ title: "Default", values: ["Default"] }],
|
|
23774
24560
|
...physical
|
|
23775
24561
|
};
|
|
23776
24562
|
if (p.metadata) payload.metadata = p.metadata;
|
|
@@ -23778,16 +24564,35 @@ async function executeItem(ctx2, p, item, plan, opts) {
|
|
|
23778
24564
|
const channelIds = (p.sales_channels || []).map((n) => plan.channelMap.get(n)).filter(Boolean);
|
|
23779
24565
|
if (channelIds.length) payload.sales_channels = channelIds.map((id) => ({ id }));
|
|
23780
24566
|
if (collectionId) payload.collection_id = collectionId;
|
|
23781
|
-
if (
|
|
23782
|
-
|
|
23783
|
-
|
|
23784
|
-
|
|
23785
|
-
|
|
23786
|
-
|
|
23787
|
-
|
|
23788
|
-
|
|
23789
|
-
|
|
23790
|
-
|
|
24567
|
+
if (isMulti) {
|
|
24568
|
+
const options = (p.options && p.options.length ? p.options : deriveOptions(p.variants)).map((o) => ({
|
|
24569
|
+
title: o.title,
|
|
24570
|
+
values: [...new Set(o.values)]
|
|
24571
|
+
}));
|
|
24572
|
+
payload.options = options;
|
|
24573
|
+
payload.variants = p.variants.map((v) => {
|
|
24574
|
+
const vPrices = buildPrices(v).length ? buildPrices(v) : prices;
|
|
24575
|
+
return {
|
|
24576
|
+
title: v.title || Object.values(v.options).join(" / "),
|
|
24577
|
+
options: v.options,
|
|
24578
|
+
...vPrices.length || v.sku ? { prices: vPrices } : {},
|
|
24579
|
+
manage_inventory: v.manage_inventory ?? p.manage_inventory ?? true,
|
|
24580
|
+
...v.sku ? { sku: v.sku } : {}
|
|
24581
|
+
};
|
|
24582
|
+
});
|
|
24583
|
+
} else {
|
|
24584
|
+
payload.options = [{ title: "Default", values: ["Default"] }];
|
|
24585
|
+
if (prices.length || p.sku) {
|
|
24586
|
+
payload.variants = [
|
|
24587
|
+
{
|
|
24588
|
+
title: "Default",
|
|
24589
|
+
options: { Default: "Default" },
|
|
24590
|
+
prices,
|
|
24591
|
+
manage_inventory: p.manage_inventory ?? true,
|
|
24592
|
+
...p.sku ? { sku: p.sku } : {}
|
|
24593
|
+
}
|
|
24594
|
+
];
|
|
24595
|
+
}
|
|
23791
24596
|
}
|
|
23792
24597
|
const data = await client2.createProduct(payload);
|
|
23793
24598
|
productId = data?.product?.id || data?.id;
|
|
@@ -23811,7 +24616,22 @@ async function executeItem(ctx2, p, item, plan, opts) {
|
|
|
23811
24616
|
summary.translations = `${r.created} created / ${r.updated} updated`;
|
|
23812
24617
|
}
|
|
23813
24618
|
const effectiveStock = item.action === "update" ? p.initial_stock : p.initial_stock ?? opts?.default_initial_stock;
|
|
23814
|
-
if (
|
|
24619
|
+
if (p.variants?.length) {
|
|
24620
|
+
let stocked = 0;
|
|
24621
|
+
for (const v of p.variants) {
|
|
24622
|
+
const vStock = v.initial_stock ?? effectiveStock;
|
|
24623
|
+
if (!vStock || !v.sku) continue;
|
|
24624
|
+
await applyProductStock(ctx2, {
|
|
24625
|
+
productId,
|
|
24626
|
+
sku: v.sku,
|
|
24627
|
+
locationId: vStock.location_id,
|
|
24628
|
+
quantity: vStock.quantity,
|
|
24629
|
+
manageInventory: v.manage_inventory ?? p.manage_inventory ?? true
|
|
24630
|
+
});
|
|
24631
|
+
stocked++;
|
|
24632
|
+
}
|
|
24633
|
+
if (stocked) summary.stock = `${stocked}/${p.variants.length} \u53D8\u4F53\u5DF2\u5EFA\u4ED3`;
|
|
24634
|
+
} else if (effectiveStock) {
|
|
23815
24635
|
await applyProductStock(ctx2, {
|
|
23816
24636
|
productId,
|
|
23817
24637
|
sku: p.sku,
|
|
@@ -23845,7 +24665,14 @@ async function executeItem(ctx2, p, item, plan, opts) {
|
|
|
23845
24665
|
}
|
|
23846
24666
|
summary.content_locales = contentLocales;
|
|
23847
24667
|
summary.images = imageUrls.length;
|
|
23848
|
-
|
|
24668
|
+
if (p.variants?.length) {
|
|
24669
|
+
summary.variants = p.variants.map((v) => ({
|
|
24670
|
+
sku: v.sku ?? null,
|
|
24671
|
+
prices: buildPrices(v).map((x) => `${x.currency_code}:${x.amount}`)
|
|
24672
|
+
}));
|
|
24673
|
+
} else {
|
|
24674
|
+
summary.prices = prices.map((x) => `${x.currency_code}:${x.amount}`);
|
|
24675
|
+
}
|
|
23849
24676
|
return summary;
|
|
23850
24677
|
}
|
|
23851
24678
|
async function runBatchImport(ctx2, rawSpec, runOpts = {}) {
|
|
@@ -23872,6 +24699,7 @@ async function runBatchImport(ctx2, rawSpec, runOpts = {}) {
|
|
|
23872
24699
|
handle: i.handle,
|
|
23873
24700
|
sku: i.sku || null,
|
|
23874
24701
|
action: i.action,
|
|
24702
|
+
variants: i.variant_count ?? 1,
|
|
23875
24703
|
warnings: i.warnings
|
|
23876
24704
|
})),
|
|
23877
24705
|
blocked: blockedItems.length,
|
|
@@ -23928,6 +24756,8 @@ var batchTools = [
|
|
|
23928
24756
|
name: "batch_import_products",
|
|
23929
24757
|
description: `\u3010\u6279\u91CF\u5F55\u5165\u9996\u9009\u3011\u4E00\u6B21\u5BFC\u5165\u591A\u6B3E\u5B8C\u6574\u5546\u54C1\uFF1A\u67E5\u91CD \u2192 \u56FE\u7247\u4E0A\u4F20 \u2192 \u5546\u54C1\u4E0E\u591A\u5E01\u79CD\u4EF7\u683C \u2192 \u56DB\u8BED\u8A00\u7FFB\u8BD1 \u2192 \u5E93\u5B58 \u2192 \u9500\u552E\u6E20\u9053 \u2192 \u5404\u8BED\u8A00\u8BE6\u60C5\u9875\u5BCC\u5185\u5BB9 \u2192 \u6458\u8981\u56DE\u8BFB\u3002
|
|
23930
24758
|
spec \u4E3A\u58F0\u660E\u5F0F JSON\uFF08\u5BF9\u8C61\u3001JSON \u5B57\u7B26\u4E32\u6216 spec_path \u6587\u4EF6\u8DEF\u5F84\uFF09\uFF0C\u5B57\u6BB5\u4E0E create_product \u57FA\u672C\u4E00\u81F4\uFF0C\u53E6\u53EF\u5E26 content(locale\u2192blocks) \u6216 options.auto_content=true \u81EA\u52A8\u751F\u6210\u5BCC\u5185\u5BB9\u3002
|
|
24759
|
+
\u26A0\uFE0F \u652F\u6301\u591A\u53D8\u4F53\uFF1A\u6BCF\u6B3E\u53EF\u5E26 options\uFF08\u5546\u54C1\u9009\u9879\uFF0C\u5982 "Fiber Type"/"Handle Type"\uFF09\u4E0E variants\uFF08\u6BCF\u4E2A\u53D8\u4F53\u542B options \u6620\u5C04 + \u5404\u81EA SKU/\u4EF7\u683C/\u5E93\u5B58\uFF09\uFF0C\u53EF\u5EFA\u6A21\u300CALC\xD7CS\u300D\u8FD9\u7C7B\u591A\u89C4\u683C\u7EC4\u5408\uFF1B\u7701\u7565 options/variants \u65F6\u9000\u5316\u4E3A\u5355\u53D8\u4F53\uFF08\u517C\u5BB9\u65E7\u884C\u4E3A\uFF09\u3002variants \u7684 options \u952E\u987B\u4E0E\u5546\u54C1 options \u6807\u9898\u4E00\u81F4\u3002
|
|
24760
|
+
\u5BF9\u5DF2\u6709\u5546\u54C1\u539F\u5730\u8865\u53D8\u4F53\u8981\u6C42\u5176\u9009\u9879\u6807\u9898\u96C6\u5408\u4E0E spec \u5B8C\u5168\u4E00\u81F4\uFF08\u7F3A\u7684\u53D6\u503C\u4F1A\u5148\u8865\uFF09\uFF0C\u5426\u5219\u9884\u68C0\u963B\u65AD\uFF08\u5EFA\u8BAE on_duplicate=archive \u4E0B\u67B6\u540E\u65B0\u5EFA\uFF09\u3002
|
|
23931
24761
|
options.on_duplicate: update(\u9ED8\u8BA4\uFF0C\u6309 handle/SKU \u5E42\u7B49 upsert) | skip | error | archive(\u65E7\u6B3E\u4E0B\u67B6\u540E\u65B0\u5EFA)\u3002
|
|
23932
24762
|
\u9ED8\u8BA4 dry_run\uFF1A\u53EA\u56DE\u6BCF\u6B3E\u4E00\u884C\u6458\u8981 + warnings + confirm_token\uFF0C\u4E0D\u5199\u5E93\uFF1B\u786E\u8BA4\u540E\u7528 confirm=true \u5E76\u539F\u6837\u5E26\u56DE confirm_token \u624D\u6267\u884C\u3002
|
|
23933
24763
|
\u65B0\u5EFA\u5546\u54C1\u9ED8\u8BA4 status=draft\uFF08skill \u786C\u89C4\u5219\uFF1A\u4E0D\u76F4\u63A5\u4E0A\u67B6\uFF09\uFF0C\u662F\u5426\u4E0A\u67B6\u7531 spec \u663E\u5F0F\u58F0\u660E\u3002
|
|
@@ -23956,23 +24786,195 @@ options.default_initial_stock \u4E5F\u53EA\u4F5C\u7528\u4E8E\u65B0\u5EFA\uFF0C\u
|
|
|
23956
24786
|
}
|
|
23957
24787
|
];
|
|
23958
24788
|
|
|
24789
|
+
// src/tools/csv-import.ts
|
|
24790
|
+
import { readFileSync as readFileSync5, existsSync as existsSync5 } from "node:fs";
|
|
24791
|
+
var stripBom2 = (s) => s.replace(/^\uFEFF/, "");
|
|
24792
|
+
function parseCsv(text) {
|
|
24793
|
+
const rows = [];
|
|
24794
|
+
let row = [];
|
|
24795
|
+
let field = "";
|
|
24796
|
+
let inQuotes = false;
|
|
24797
|
+
for (let i = 0; i < text.length; i++) {
|
|
24798
|
+
const c = text[i];
|
|
24799
|
+
if (inQuotes) {
|
|
24800
|
+
if (c === '"') {
|
|
24801
|
+
if (text[i + 1] === '"') {
|
|
24802
|
+
field += '"';
|
|
24803
|
+
i++;
|
|
24804
|
+
} else {
|
|
24805
|
+
inQuotes = false;
|
|
24806
|
+
}
|
|
24807
|
+
} else {
|
|
24808
|
+
field += c;
|
|
24809
|
+
}
|
|
24810
|
+
} else {
|
|
24811
|
+
if (c === '"') {
|
|
24812
|
+
inQuotes = true;
|
|
24813
|
+
} else if (c === ",") {
|
|
24814
|
+
row.push(field);
|
|
24815
|
+
field = "";
|
|
24816
|
+
} else if (c === "\n" || c === "\r") {
|
|
24817
|
+
if (c === "\r" && text[i + 1] === "\n") i++;
|
|
24818
|
+
row.push(field);
|
|
24819
|
+
rows.push(row);
|
|
24820
|
+
row = [];
|
|
24821
|
+
field = "";
|
|
24822
|
+
} else {
|
|
24823
|
+
field += c;
|
|
24824
|
+
}
|
|
24825
|
+
}
|
|
24826
|
+
}
|
|
24827
|
+
if (field.length > 0 || row.length > 0) {
|
|
24828
|
+
row.push(field);
|
|
24829
|
+
rows.push(row);
|
|
24830
|
+
}
|
|
24831
|
+
return rows.filter((r) => r.length > 1 || r.length === 1 && r[0].trim() !== "");
|
|
24832
|
+
}
|
|
24833
|
+
function lintCsv(text, filename) {
|
|
24834
|
+
const warnings = [];
|
|
24835
|
+
if (text.charCodeAt(0) === 65279) warnings.push("\u6587\u4EF6\u5E26 UTF-8 BOM\uFF0CMedusa \u89E3\u6790\u53EF\u80FD\u62A5\u9519\uFF0C\u5EFA\u8BAE\u53BB BOM \u540E\u91CD\u5B58");
|
|
24836
|
+
const rows = parseCsv(text);
|
|
24837
|
+
if (rows.length < 2) {
|
|
24838
|
+
warnings.push("CSV \u884C\u6570 < 2\uFF08\u81F3\u5C11\u5E94\u6709\u8868\u5934 + 1 \u884C\u6570\u636E\uFF09");
|
|
24839
|
+
return warnings;
|
|
24840
|
+
}
|
|
24841
|
+
const header = rows[0].map((h) => h.trim());
|
|
24842
|
+
const expected = ["Product Handle", "Variant SKU", "Product Status", "Variant Price MYR", "Variant Price IDR"];
|
|
24843
|
+
const missing = expected.filter((c) => !header.includes(c));
|
|
24844
|
+
if (missing.length) warnings.push(`\u7F3A\u5C11\u5FC5\u586B\u5217\uFF1A${missing.join(", ")}`);
|
|
24845
|
+
const ncol = header.length;
|
|
24846
|
+
const ragged = rows.slice(1).filter((r) => r.length !== ncol).length;
|
|
24847
|
+
if (ragged > 0) warnings.push(`\u6709 ${ragged} \u884C\u6570\u636E\u5217\u6570(${ncol === 0 ? "?" : ""}) \u4E0E\u8868\u5934(${ncol}) \u4E0D\u4E00\u81F4\uFF08\u53EF\u80FD\u591A\u4F59/\u7F3A\u5931\u5217\uFF0CMedusa \u4F1A\u9519\u4F4D\u6216\u62A5\u9519\uFF09`);
|
|
24848
|
+
if (!header.includes("Product Handle")) {
|
|
24849
|
+
} else {
|
|
24850
|
+
const hi = header.indexOf("Product Handle");
|
|
24851
|
+
const ti = header.indexOf("Product Title");
|
|
24852
|
+
const byHandle = /* @__PURE__ */ new Map();
|
|
24853
|
+
for (const r of rows.slice(1)) {
|
|
24854
|
+
const hd = (r[hi] || "").trim();
|
|
24855
|
+
const tt = (r[ti] || "").trim();
|
|
24856
|
+
if (!hd || !tt) continue;
|
|
24857
|
+
const set = byHandle.get(hd) || /* @__PURE__ */ new Set();
|
|
24858
|
+
set.add(tt);
|
|
24859
|
+
byHandle.set(hd, set);
|
|
24860
|
+
}
|
|
24861
|
+
const conflicting = [...byHandle.entries()].filter(([, titles]) => titles.size > 1);
|
|
24862
|
+
if (conflicting.length) {
|
|
24863
|
+
warnings.push(`\u540C\u4E00 Handle \u5BF9\u5E94\u591A\u4E2A Product Title\uFF1A${conflicting.slice(0, 3).map(([h, t]) => `${h}(${[...t].join(" / ")})`).join("; ")}`);
|
|
24864
|
+
}
|
|
24865
|
+
}
|
|
24866
|
+
const numericCols = header.filter((h) => /^Variant Price |^Product (Weight|Length|Width|Height)$|^Variant (Weight|Length|Width|Height)$/.test(h));
|
|
24867
|
+
const boolCols = header.filter((h) => /Discountable|^Variant (Manage Inventory|Allow Backorder)$/.test(h));
|
|
24868
|
+
for (const r of rows.slice(1)) {
|
|
24869
|
+
for (const c of numericCols) {
|
|
24870
|
+
const v = (r[header.indexOf(c)] || "").trim();
|
|
24871
|
+
if (v && Number.isNaN(Number(v))) {
|
|
24872
|
+
warnings.push(`\u5217 "${c}" \u51FA\u73B0\u975E\u6570\u5B57\u503C "${v.slice(0, 40)}"\uFF08\u7591\u4F3C\u5217\u9519\u4F4D/\u503C\u4E32\u4F4D\uFF09`);
|
|
24873
|
+
}
|
|
24874
|
+
}
|
|
24875
|
+
for (const c of boolCols) {
|
|
24876
|
+
const v = (r[header.indexOf(c)] || "").trim().toUpperCase();
|
|
24877
|
+
if (v && v !== "TRUE" && v !== "FALSE") {
|
|
24878
|
+
warnings.push(`\u5217 "${c}" \u51FA\u73B0\u975E\u5E03\u5C14\u503C "${v.slice(0, 40)}"\uFF08\u7591\u4F3C\u5217\u9519\u4F4D/\u503C\u4E32\u4F4D\uFF09`);
|
|
24879
|
+
}
|
|
24880
|
+
}
|
|
24881
|
+
}
|
|
24882
|
+
const stringNumericCols = header.filter((h) => /HS Code|Barcode|MID Code/i.test(h));
|
|
24883
|
+
for (const r of rows.slice(1)) {
|
|
24884
|
+
for (const c of stringNumericCols) {
|
|
24885
|
+
const v = (r[header.indexOf(c)] || "").trim();
|
|
24886
|
+
if (v && /^[\d.]+$/.test(v)) {
|
|
24887
|
+
warnings.push(`\u5217 "${c}" \u7684\u7EAF\u6570\u5B57\u503C "${v}" \u4F1A\u88AB Medusa \u89E3\u6790\u4E3A number \u800C\u62A5\u9519\uFF08schema \u8981 string\uFF09\uFF1B\u5EFA\u8BAE\u8BE5\u5217\u7559\u7A7A\uFF0C\u5BFC\u5165\u540E\u7528 JSON API \u8865\u503C`);
|
|
24888
|
+
}
|
|
24889
|
+
}
|
|
24890
|
+
}
|
|
24891
|
+
return warnings;
|
|
24892
|
+
}
|
|
24893
|
+
var sleep = (ms) => new Promise((r) => setTimeout(r, ms));
|
|
24894
|
+
async function runTriggerCsvImport(ctx2, csvPath, confirm, pollTimeoutMs) {
|
|
24895
|
+
const { client: client2 } = ctx2;
|
|
24896
|
+
if (!existsSync5(csvPath)) return { error: `CSV \u6587\u4EF6\u4E0D\u5B58\u5728: ${csvPath}` };
|
|
24897
|
+
const raw = readFileSync5(csvPath);
|
|
24898
|
+
const text = stripBom2(raw.toString("utf8"));
|
|
24899
|
+
const lint = lintCsv(text, csvPath);
|
|
24900
|
+
const uploaded = await client2.importProductsCsv(raw, csvPath.split("/").pop() || "products.csv");
|
|
24901
|
+
const out = {
|
|
24902
|
+
uploaded: true,
|
|
24903
|
+
lint,
|
|
24904
|
+
transaction_id: uploaded.transaction_id,
|
|
24905
|
+
summary: uploaded.summary,
|
|
24906
|
+
dry_run: !confirm,
|
|
24907
|
+
note: confirm ? "\u5DF2\u786E\u8BA4\uFF0C\u540E\u53F0\u6B63\u5728\u6279\u91CF\u5BFC\u5165\uFF1B\u4E0B\u9762\u8F6E\u8BE2\u6267\u884C\u72B6\u6001\u3002" : "\u672A confirm\uFF1ACSV \u5DF2\u4E0A\u4F20\u5E76\u6821\u9A8C\uFF0C\u4F46\u5C1A\u672A\u5199\u5165\u4EFB\u4F55\u5546\u54C1\u3002\u786E\u8BA4\u8BF7\u7528 confirm=true \u5E26\u540C\u4E00 csv_path\u3002"
|
|
24908
|
+
};
|
|
24909
|
+
if (!confirm) return out;
|
|
24910
|
+
await client2.confirmProductImport(uploaded.transaction_id);
|
|
24911
|
+
out.confirmed = true;
|
|
24912
|
+
const TERMINAL_STATES = /* @__PURE__ */ new Set(["done", "failed", "reverted"]);
|
|
24913
|
+
let status = "submitted";
|
|
24914
|
+
const deadline = Date.now() + pollTimeoutMs;
|
|
24915
|
+
while (Date.now() < deadline) {
|
|
24916
|
+
await sleep(3e3);
|
|
24917
|
+
try {
|
|
24918
|
+
const ex = await client2.getWorkflowExecution(uploaded.transaction_id);
|
|
24919
|
+
if (ex?.state) {
|
|
24920
|
+
status = ex.state;
|
|
24921
|
+
if (TERMINAL_STATES.has(status)) break;
|
|
24922
|
+
}
|
|
24923
|
+
} catch {
|
|
24924
|
+
break;
|
|
24925
|
+
}
|
|
24926
|
+
}
|
|
24927
|
+
out.status = status;
|
|
24928
|
+
if (status === "failed" || status === "reverted") {
|
|
24929
|
+
out.error = `\u5BFC\u5165\u5DE5\u4F5C\u6D41\u72B6\u6001\u4E3A ${status}\uFF0C\u8BF7\u5230\u540E\u53F0 Products \u2192 Import \u67E5\u770B\u8BE5\u6279\u6B21\u9519\u8BEF\u8BE6\u60C5`;
|
|
24930
|
+
}
|
|
24931
|
+
out.note = status === "done" ? "\u5BFC\u5165\u5DE5\u4F5C\u6D41\u5DF2\u5B8C\u6210\uFF1B\u8BF7\u5230\u540E\u53F0\u6838\u5BF9\u5546\u54C1\uFF08\u6CE8\u610F\u539F\u751F\u5BFC\u5165\u4E0D\u542B\u56DB\u8BED/\u5BCC\u5185\u5BB9/\u5E93\u5B58\uFF0C\u9700\u53E6\u884C\u8865\u9F50\uFF09\u3002" : status === "failed" || status === "reverted" ? "\u5BFC\u5165\u5DE5\u4F5C\u6D41\u5931\u8D25/\u5DF2\u56DE\u6EDA\uFF0C\u672A\u5199\u5165\u6216\u5DF2\u64A4\u9500\u90E8\u5206\u5546\u54C1\uFF1B\u8BF7\u5230\u540E\u53F0\u6838\u5BF9\u5E76\u4FEE\u6B63 CSV \u540E\u91CD\u8BD5\u3002" : "\u5DF2\u63D0\u4EA4\u786E\u8BA4\uFF0C\u5BFC\u5165\u4ECD\u5728\u540E\u53F0\u6267\u884C\uFF08\u8F6E\u8BE2\u8D85\u65F6\u6216\u72B6\u6001\u7AEF\u70B9\u6682\u4E0D\u53EF\u7528\uFF09\uFF1B\u8BF7\u7A0D\u540E\u5230\u540E\u53F0 Products \u6838\u5BF9\u7ED3\u679C\u3002";
|
|
24932
|
+
return out;
|
|
24933
|
+
}
|
|
24934
|
+
var csvImportTools = [
|
|
24935
|
+
{
|
|
24936
|
+
name: "trigger_native_csv_import",
|
|
24937
|
+
description: `\u3010\u539F\u751F CSV \u5BFC\u5165\u81EA\u52A8\u5316\u3011\u628A\u540E\u53F0 Products \u2192 Import \u7684 CSV \u901A\u9053\u5B8C\u5168\u81EA\u52A8\u5316\uFF0C\u56E0\u6B64**\u539F\u751F\u652F\u6301\u591A\u53D8\u4F53/\u591A\u9009\u9879**\uFF08CSV \u6A21\u677F\u7684 Variant Option 1/2/3 \u5217\uFF09\u3002
|
|
24938
|
+
\u8F93\u5165\u672C\u5730 CSV \u7EDD\u5BF9\u8DEF\u5F84\uFF08\u4E0E product-import-template.csv \u540C\u5217\u540D\uFF09\u3002
|
|
24939
|
+
\u4E24\u6B65\uFF1A\u2460 \u4E0A\u4F20 CSV \u5230 /admin/products/import \u62FF transaction_id + summary(\u5C06\u5EFA/\u66F4\u591A\u5C11\u6B3E)\uFF1B\u2461 \u663E\u5F0F confirm=true \u624D\u771F\u6B63\u5728\u540E\u53F0\u6279\u91CF\u5EFA/\u66F4\u5546\u54C1\uFF08Medusa \u5BFC\u5165\u5DE5\u4F5C\u6D41\u9ED8\u8BA4\u7B49\u5F85\u786E\u8BA4\uFF09\u3002
|
|
24940
|
+
\u9ED8\u8BA4 confirm=false\uFF1A\u4EC5\u4E0A\u4F20 + \u8F7B\u91CF lint + \u8FD4\u56DE summary\uFF0C**\u96F6\u526F\u4F5C\u7528**\uFF08\u4E0D\u521B\u5EFA\u4EFB\u4F55\u5546\u54C1\uFF09\uFF0C\u53EF\u5B89\u5168\u9884\u68C0 CSV\u3002
|
|
24941
|
+
\u26A0\uFE0F \u4E0E batch_import_products\uFF08MCP \u58F0\u660E\u5F0F JSON\uFF09\u662F\u4E24\u5957\u673A\u5236\uFF1B\u539F\u751F CSV \u5BFC\u5165**\u4E0D\u542B\u56DB\u8BED\u7FFB\u8BD1/\u5BCC\u5185\u5BB9/\u5E93\u5B58**\uFF0C\u5BFC\u5165\u540E\u9700\u5230\u7CFB\u7EDF\u5185\u6216\u518D\u7528\u5176\u5B83\u5DE5\u5177\u8865\u9F50\u3002
|
|
24942
|
+
\u26A0\uFE0F \u5199\u5165\u524D\u52A1\u5FC5\u786E\u8BA4 CSV \u6B63\u786E\uFF1Alint \u4F1A\u68C0\u67E5\u5217\u5BF9\u9F50/ BOM / \u5FC5\u586B\u5217 / \u91CD\u590D Handle\u3002`,
|
|
24943
|
+
schema: {
|
|
24944
|
+
csv_path: external_exports.string().describe("\u672C\u5730 CSV \u6587\u4EF6\u7EDD\u5BF9\u8DEF\u5F84\uFF08\u5217\u540D\u4E0E\u540E\u53F0 Products \u2192 Import \u6A21\u677F\u4E00\u81F4\uFF09"),
|
|
24945
|
+
confirm: external_exports.boolean().default(false).describe("true \u624D\u771F\u6B63\u89E6\u53D1\u540E\u53F0\u6279\u91CF\u5BFC\u5165\uFF1Bfalse \u4EC5\u4E0A\u4F20+\u6821\u9A8C\uFF08\u96F6\u526F\u4F5C\u7528\uFF09"),
|
|
24946
|
+
poll_timeout_ms: external_exports.number().int().positive().max(12e4).default(6e4).describe("confirm \u540E\u8F6E\u8BE2\u6267\u884C\u72B6\u6001\u7684\u6700\u957F\u7B49\u5F85\uFF08\u6BEB\u79D2\uFF09")
|
|
24947
|
+
},
|
|
24948
|
+
async run(input, ctx2) {
|
|
24949
|
+
try {
|
|
24950
|
+
return await runTriggerCsvImport(ctx2, input.csv_path, Boolean(input.confirm), input.poll_timeout_ms ?? 6e4);
|
|
24951
|
+
} catch (e) {
|
|
24952
|
+
return { error: `\u539F\u751F CSV \u5BFC\u5165\u5931\u8D25\uFF1A${e instanceof Error ? e.message : String(e)}` };
|
|
24953
|
+
}
|
|
24954
|
+
}
|
|
24955
|
+
}
|
|
24956
|
+
];
|
|
24957
|
+
|
|
23959
24958
|
// src/tools/index.ts
|
|
23960
24959
|
var toolDefs = [
|
|
23961
24960
|
...contextTools,
|
|
23962
24961
|
...productTools,
|
|
24962
|
+
...categoryTools,
|
|
24963
|
+
...collectionTools,
|
|
23963
24964
|
...translationTools,
|
|
23964
24965
|
...stockTools,
|
|
23965
24966
|
...opsTools,
|
|
23966
24967
|
...batchTools,
|
|
24968
|
+
...csvImportTools,
|
|
23967
24969
|
...docTools
|
|
23968
24970
|
];
|
|
23969
24971
|
|
|
23970
24972
|
// src/audit.ts
|
|
23971
|
-
import { appendFileSync, mkdirSync as
|
|
24973
|
+
import { appendFileSync, mkdirSync as mkdirSync3 } from "node:fs";
|
|
23972
24974
|
import { dirname as dirname2 } from "node:path";
|
|
23973
24975
|
function audit(auditFile, name, args, ok) {
|
|
23974
24976
|
try {
|
|
23975
|
-
|
|
24977
|
+
mkdirSync3(dirname2(auditFile), { recursive: true });
|
|
23976
24978
|
const line = `${(/* @__PURE__ */ new Date()).toISOString()} | tool=${name} | ok=${ok} | args=${JSON.stringify(args)}
|
|
23977
24979
|
`;
|
|
23978
24980
|
appendFileSync(auditFile, line);
|
|
@@ -23982,7 +24984,7 @@ function audit(auditFile, name, args, ok) {
|
|
|
23982
24984
|
|
|
23983
24985
|
// src/cli.ts
|
|
23984
24986
|
function isCliInvocation(argv2) {
|
|
23985
|
-
return argv2[0] === "import" || argv2.some((a) => a.startsWith("--"));
|
|
24987
|
+
return argv2[0] === "import" || argv2[0] === "import-csv" || argv2.some((a) => a.startsWith("--"));
|
|
23986
24988
|
}
|
|
23987
24989
|
var USAGE = `derlux-admin-mcp CLI
|
|
23988
24990
|
|
|
@@ -23990,6 +24992,9 @@ var USAGE = `derlux-admin-mcp CLI
|
|
|
23990
24992
|
import --spec <path> --dry-run \u9884\u89C8\u6574\u6279\u5BFC\u5165\uFF0C\u8F93\u51FA\u6458\u8981\u4E0E confirm_token\uFF08\u4E0D\u5199\u5E93\uFF09
|
|
23991
24993
|
import --spec <path> --commit --token <confirm_token> [--verbose]
|
|
23992
24994
|
\u6309 dry-run \u7684 spec \u6B63\u5F0F\u5199\u5165
|
|
24995
|
+
import-csv --path <csv> \u4E0A\u4F20\u539F\u751F CSV \u5230 /admin/products/import \u5E76\u6821\u9A8C\uFF08\u96F6\u526F\u4F5C\u7528\uFF0C\u4E0D\u5199\u5E93\uFF09
|
|
24996
|
+
import-csv --path <csv> --confirm [--timeout <ms>]
|
|
24997
|
+
\u4E0A\u4F20\u5E76\u786E\u8BA4\u89E6\u53D1\u540E\u53F0\u6279\u91CF\u5BFC\u5165\uFF08\u4F1A\u771F\u6B63\u5EFA/\u66F4\u5546\u54C1\uFF09
|
|
23993
24998
|
--help \u663E\u793A\u672C\u5E2E\u52A9
|
|
23994
24999
|
|
|
23995
25000
|
\u793A\u4F8B\uFF1A
|
|
@@ -24015,6 +25020,33 @@ async function runCli(argv2, ctx2) {
|
|
|
24015
25020
|
`);
|
|
24016
25021
|
return out.ok ? 0 : 1;
|
|
24017
25022
|
}
|
|
25023
|
+
if (argv2[0] === "import-csv") {
|
|
25024
|
+
const args2 = argv2.slice(1);
|
|
25025
|
+
const path = argValue(args2, "--path");
|
|
25026
|
+
if (!path) {
|
|
25027
|
+
process.stderr.write("\u7F3A\u5C11 --path <csv>\n");
|
|
25028
|
+
return 1;
|
|
25029
|
+
}
|
|
25030
|
+
const confirm = args2.includes("--confirm");
|
|
25031
|
+
const timeout = (() => {
|
|
25032
|
+
const t = argValue(args2, "--timeout");
|
|
25033
|
+
const n = t ? Number(t) : NaN;
|
|
25034
|
+
return Number.isFinite(n) && n > 0 ? Math.min(n, 12e4) : 6e4;
|
|
25035
|
+
})();
|
|
25036
|
+
try {
|
|
25037
|
+
const out = await runTriggerCsvImport(ctx2, path, confirm, timeout);
|
|
25038
|
+
process.stdout.write(`${JSON.stringify(out, null, 2)}
|
|
25039
|
+
`);
|
|
25040
|
+
if (out.error) return 1;
|
|
25041
|
+
const status = out.status;
|
|
25042
|
+
if (confirm && (status === "failed" || status === "reverted")) return 1;
|
|
25043
|
+
return 0;
|
|
25044
|
+
} catch (e) {
|
|
25045
|
+
process.stderr.write(`CSV \u5BFC\u5165\u5931\u8D25\uFF1A${e instanceof Error ? e.message : String(e)}
|
|
25046
|
+
`);
|
|
25047
|
+
return 1;
|
|
25048
|
+
}
|
|
25049
|
+
}
|
|
24018
25050
|
if (argv2[0] !== "import") {
|
|
24019
25051
|
process.stderr.write(`\u672A\u77E5\u547D\u4EE4/\u53C2\u6570: ${argv2.join(" ")}
|
|
24020
25052
|
|
|
@@ -24070,10 +25102,10 @@ if (isCliInvocation(argv)) {
|
|
|
24070
25102
|
process.exit(code);
|
|
24071
25103
|
}
|
|
24072
25104
|
process.stderr.write(
|
|
24073
|
-
`[derlux-admin] v0.
|
|
25105
|
+
`[derlux-admin] v0.3.0 backend=${config2.backendUrl} account=${config2.adminEmail || "(\u672A\u914D\u7F6E)"} session_scope=${config2.sessionScope} tools=${toolDefs.length}
|
|
24074
25106
|
`
|
|
24075
25107
|
);
|
|
24076
|
-
var server = new McpServer({ name: "derlux-admin", version: "0.
|
|
25108
|
+
var server = new McpServer({ name: "derlux-admin", version: "0.3.0" });
|
|
24077
25109
|
function isToolSuccess(result) {
|
|
24078
25110
|
if (!result || typeof result !== "object") return true;
|
|
24079
25111
|
const r = result;
|