@sea-ecommerce/admin-mcp-server 0.1.3 → 0.1.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/derlux-admin-mcp.js +209 -36
- package/package.json +1 -1
- package/skill/SKILL.md +25 -7
package/dist/derlux-admin-mcp.js
CHANGED
|
@@ -21567,6 +21567,23 @@ function loadConfig() {
|
|
|
21567
21567
|
requiredLocales
|
|
21568
21568
|
};
|
|
21569
21569
|
}
|
|
21570
|
+
var LOCALE_CANON = {
|
|
21571
|
+
en: "en-US",
|
|
21572
|
+
"en-us": "en-US",
|
|
21573
|
+
"en-US": "en-US",
|
|
21574
|
+
zh: "zh-CN",
|
|
21575
|
+
"zh-cn": "zh-CN",
|
|
21576
|
+
"zh-CN": "zh-CN",
|
|
21577
|
+
ms: "ms-MY",
|
|
21578
|
+
"ms-my": "ms-MY",
|
|
21579
|
+
"ms-MY": "ms-MY",
|
|
21580
|
+
id: "id-ID",
|
|
21581
|
+
"id-id": "id-ID",
|
|
21582
|
+
"id-ID": "id-ID"
|
|
21583
|
+
};
|
|
21584
|
+
function normalizeLocale(input) {
|
|
21585
|
+
return LOCALE_CANON[input] ?? LOCALE_CANON[input.trim().toLowerCase()] ?? null;
|
|
21586
|
+
}
|
|
21570
21587
|
|
|
21571
21588
|
// src/auth.ts
|
|
21572
21589
|
import { readFileSync, writeFileSync, mkdirSync, existsSync } from "node:fs";
|
|
@@ -21691,8 +21708,31 @@ var MedusaAdminClient = class {
|
|
|
21691
21708
|
listProducts(params = {}) {
|
|
21692
21709
|
return this.request("GET", `/admin/products${buildQuery(params)}`);
|
|
21693
21710
|
}
|
|
21694
|
-
|
|
21695
|
-
|
|
21711
|
+
/**
|
|
21712
|
+
* 把「商品 id 或 handle」解析为商品 id。
|
|
21713
|
+
* ⚠️ Medusa v2 的 GET /admin/products/{id} **只认 id**:传 handle 会 404(Product not found)。
|
|
21714
|
+
* 因此 handle 必须先走列表端点 `?handle=<h>`(精确匹配)解析成 id,再由调用方按 id 取详情。
|
|
21715
|
+
* 解析不到(id 不存在 / handle 无匹配)返回 null。
|
|
21716
|
+
*/
|
|
21717
|
+
async resolveProductId(idOrHandle) {
|
|
21718
|
+
const s = idOrHandle.trim();
|
|
21719
|
+
if (/^prod_[a-z0-9]+$/i.test(s)) return s;
|
|
21720
|
+
try {
|
|
21721
|
+
const data = await this.request("GET", `/admin/products${buildQuery({ handle: s, limit: 10 })}`);
|
|
21722
|
+
const list = data?.products || [];
|
|
21723
|
+
const exact = list.find((p) => p.handle === s);
|
|
21724
|
+
return (exact ?? list[0])?.id || null;
|
|
21725
|
+
} catch {
|
|
21726
|
+
return null;
|
|
21727
|
+
}
|
|
21728
|
+
}
|
|
21729
|
+
/** 按商品 id 或 handle 获取商品详情(handle 自动解析为 id,见 resolveProductId)。 */
|
|
21730
|
+
async getProduct(idOrHandle) {
|
|
21731
|
+
const id = await this.resolveProductId(idOrHandle);
|
|
21732
|
+
if (id) {
|
|
21733
|
+
return this.request("GET", `/admin/products/${encodeURIComponent(id)}`);
|
|
21734
|
+
}
|
|
21735
|
+
return this.request("GET", `/admin/products/${encodeURIComponent(idOrHandle.trim())}`);
|
|
21696
21736
|
}
|
|
21697
21737
|
/** 列出销售渠道(用于默认渠道解析) */
|
|
21698
21738
|
listSalesChannels(params = {}) {
|
|
@@ -22110,7 +22150,105 @@ location_id \u53EF\u9009\uFF1A\u4F20\u4E86\u5219\u53EA\u56DE\u8BE5\u4ED3\u5E93\u
|
|
|
22110
22150
|
}
|
|
22111
22151
|
];
|
|
22112
22152
|
|
|
22153
|
+
// src/tools/translations.ts
|
|
22154
|
+
function canonLocaleKey(locale) {
|
|
22155
|
+
return normalizeLocale(locale) ?? locale;
|
|
22156
|
+
}
|
|
22157
|
+
async function planTranslationUpsert(client2, reference, referenceId, entries) {
|
|
22158
|
+
const data = await client2.listTranslations({ reference, reference_id: referenceId, limit: 100 });
|
|
22159
|
+
const existing = data?.translations || [];
|
|
22160
|
+
const byLocale = new Map(existing.map((r) => [r.locale_code, r]));
|
|
22161
|
+
const create = [];
|
|
22162
|
+
const update = [];
|
|
22163
|
+
const matched = [];
|
|
22164
|
+
for (const e of entries) {
|
|
22165
|
+
const target = canonLocaleKey(e.locale_code);
|
|
22166
|
+
let row = byLocale.get(e.locale_code) || byLocale.get(target);
|
|
22167
|
+
if (!row) {
|
|
22168
|
+
row = existing.find((r) => canonLocaleKey(r.locale_code) === target) || null;
|
|
22169
|
+
}
|
|
22170
|
+
if (row) {
|
|
22171
|
+
const merged = { ...row.translations || {} };
|
|
22172
|
+
for (const [k, v] of Object.entries(e.translations)) {
|
|
22173
|
+
if (v !== void 0 && v !== null) merged[k] = v;
|
|
22174
|
+
}
|
|
22175
|
+
const upd = { id: row.id, translations: merged };
|
|
22176
|
+
if (row.locale_code !== target) upd.locale_code = target;
|
|
22177
|
+
update.push(upd);
|
|
22178
|
+
matched.push({ id: row.id, from_locale: row.locale_code, to_locale: target });
|
|
22179
|
+
} else {
|
|
22180
|
+
const clean = {};
|
|
22181
|
+
for (const [k, v] of Object.entries(e.translations)) {
|
|
22182
|
+
if (v !== void 0 && v !== null) clean[k] = v;
|
|
22183
|
+
}
|
|
22184
|
+
create.push({ reference, reference_id: referenceId, locale_code: target, translations: clean });
|
|
22185
|
+
}
|
|
22186
|
+
}
|
|
22187
|
+
return { create, update, matched };
|
|
22188
|
+
}
|
|
22189
|
+
async function upsertTranslations(client2, reference, referenceId, entries) {
|
|
22190
|
+
const plan = await planTranslationUpsert(client2, reference, referenceId, entries);
|
|
22191
|
+
const result = await client2.batchTranslations(plan.create, plan.update);
|
|
22192
|
+
return { created: plan.create.length, updated: plan.update.length, result };
|
|
22193
|
+
}
|
|
22194
|
+
var translationTools = [
|
|
22195
|
+
{
|
|
22196
|
+
name: "set_translations",
|
|
22197
|
+
description: `\u4E3A\u5B9E\u4F53\uFF08\u5546\u54C1\u7B49\uFF09**\u521B\u5EFA\u6216\u66F4\u65B0\uFF08upsert\uFF09**\u591A\u8BED\u8A00\u7FFB\u8BD1\uFF08title/subtitle/description\uFF09\uFF0C\u8D70 /admin/translations/batch\u3002
|
|
22198
|
+
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
|
|
22199
|
+
\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
|
|
22200
|
+
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
|
|
22201
|
+
\u7528\u9014\uFF1A\u5546\u54C1\u521B\u5EFA\u540E\u8865\u7FFB\u8BD1/\u6539\u7FFB\u8BD1/\u4FEE\u590D\u591A\u8BED\u8A00\u3002\u9ED8\u8BA4 dry_run\uFF08confirm=false\uFF09\u9884\u89C8\u5C06\u521B\u5EFA/\u66F4\u65B0\u54EA\u4E9B\u884C\u3002`,
|
|
22202
|
+
schema: {
|
|
22203
|
+
reference: external_exports.string().default("product").describe("\u5B9E\u4F53\u7C7B\u578B\uFF0C\u5546\u54C1\u7528 product"),
|
|
22204
|
+
reference_id: external_exports.string().describe("\u5B9E\u4F53 id\uFF1B\u5546\u54C1\u53EF\u4F20 handle\uFF08\u81EA\u52A8\u89E3\u6790\u4E3A id\uFF09"),
|
|
22205
|
+
translations: external_exports.record(external_exports.string(), external_exports.object({
|
|
22206
|
+
title: external_exports.string().optional(),
|
|
22207
|
+
subtitle: external_exports.string().optional(),
|
|
22208
|
+
description: external_exports.string().optional()
|
|
22209
|
+
})).describe('locale \u2192 \u7FFB\u8BD1\u5B57\u6BB5\uFF0C\u5982 {"zh-CN":{"title":"...","description":"..."}}\uFF1B\u77ED\u7801/\u533A\u57DF\u7801\u5747\u53EF'),
|
|
22210
|
+
confirm: external_exports.boolean().default(false)
|
|
22211
|
+
},
|
|
22212
|
+
async run(input, { client: client2 }) {
|
|
22213
|
+
let refId = String(input.reference_id).trim();
|
|
22214
|
+
if (input.reference === "product" && !/^prod_[a-z0-9]+$/i.test(refId)) {
|
|
22215
|
+
const id = await client2.resolveProductId(refId);
|
|
22216
|
+
if (!id) return `\u9519\u8BEF\uFF1A\u627E\u4E0D\u5230\u5546\u54C1 "${refId}"\uFF08\u8BF7\u4F20\u5546\u54C1 id \u6216\u6709\u6548 handle\uFF09`;
|
|
22217
|
+
refId = id;
|
|
22218
|
+
}
|
|
22219
|
+
const entries = Object.entries(input.translations).map(([locale, t]) => ({
|
|
22220
|
+
locale_code: canonLocaleKey(locale),
|
|
22221
|
+
translations: Object.fromEntries(
|
|
22222
|
+
Object.entries(t ?? {}).filter(([, v]) => v !== void 0 && v !== null)
|
|
22223
|
+
)
|
|
22224
|
+
}));
|
|
22225
|
+
if (!entries.length) return "\u9519\u8BEF\uFF1Atranslations \u4E3A\u7A7A\uFF0C\u65E0\u4E8B\u53EF\u505A\u3002";
|
|
22226
|
+
const plan = await planTranslationUpsert(client2, input.reference, refId, entries);
|
|
22227
|
+
if (!input.confirm) {
|
|
22228
|
+
return {
|
|
22229
|
+
dry_run: true,
|
|
22230
|
+
reference: input.reference,
|
|
22231
|
+
reference_id: refId,
|
|
22232
|
+
will_create: plan.create,
|
|
22233
|
+
will_update: plan.update,
|
|
22234
|
+
matched_existing: plan.matched,
|
|
22235
|
+
note: "\u672A confirm\uFF0C\u4EC5\u9884\u89C8\u3002\u786E\u8BA4\u8BF7\u8BBE confirm=true\u3002"
|
|
22236
|
+
};
|
|
22237
|
+
}
|
|
22238
|
+
const { created, updated, result } = await upsertTranslations(client2, input.reference, refId, entries);
|
|
22239
|
+
return { created, updated, result };
|
|
22240
|
+
}
|
|
22241
|
+
}
|
|
22242
|
+
];
|
|
22243
|
+
|
|
22113
22244
|
// src/tools/products.ts
|
|
22245
|
+
function canonicalizeTranslations(translations) {
|
|
22246
|
+
const out = {};
|
|
22247
|
+
for (const [key, val] of Object.entries(translations ?? {})) {
|
|
22248
|
+
out[normalizeLocale(key) ?? key] = val;
|
|
22249
|
+
}
|
|
22250
|
+
return out;
|
|
22251
|
+
}
|
|
22114
22252
|
var PRODUCT_STATUS = [
|
|
22115
22253
|
"draft",
|
|
22116
22254
|
"published",
|
|
@@ -22265,10 +22403,10 @@ SKU / \u89C4\u683C\uFF1Asku \u5199\u5165\u9ED8\u8BA4\u53D8\u4F53\uFF1Bmetadata \
|
|
|
22265
22403
|
if (!HANDLE_RE.test(handle)) {
|
|
22266
22404
|
return `\u9519\u8BEF\uFF1A\u5F52\u4E00\u5316\u540E\u7684 Handle "${handle}" \u4ECD\u975E\u6CD5\uFF08\u53EA\u80FD\u5C0F\u5199\u5B57\u6BCD/\u6570\u5B57/\u8FDE\u5B57\u7B26\uFF09\u3002\u8BF7\u68C0\u67E5\u8F93\u5165\u3002`;
|
|
22267
22405
|
}
|
|
22268
|
-
const
|
|
22269
|
-
const
|
|
22270
|
-
|
|
22271
|
-
);
|
|
22406
|
+
const canon = (l) => normalizeLocale(l) ?? l;
|
|
22407
|
+
const normTranslations = canonicalizeTranslations(input.translations);
|
|
22408
|
+
const provided = Object.keys(normTranslations);
|
|
22409
|
+
const missing = config3.requiredLocales.map(canon).filter((l) => !provided.includes(l) || !normTranslations[l]?.title);
|
|
22272
22410
|
let channelIds = [];
|
|
22273
22411
|
if (input.sales_channels?.length) {
|
|
22274
22412
|
const ch = await client2.listSalesChannels({ limit: 100 });
|
|
@@ -22286,7 +22424,22 @@ SKU / \u89C4\u683C\uFF1Asku \u5199\u5165\u9ED8\u8BA4\u53D8\u4F53\uFF1Bmetadata \
|
|
|
22286
22424
|
if (!hit) return `\u9519\u8BEF\uFF1A\u627E\u4E0D\u5230\u96C6\u5408 "${collectionId}"\uFF08\u7528 list_collections \u67E5\u770B\u53EF\u7528\u96C6\u5408\uFF09`;
|
|
22287
22425
|
collectionId = hit.id;
|
|
22288
22426
|
}
|
|
22427
|
+
let handleCollision = null;
|
|
22428
|
+
try {
|
|
22429
|
+
const existing = await client2.listProducts({ handle, limit: 5 });
|
|
22430
|
+
const dup = (existing?.products || []).find((p) => p.handle === handle);
|
|
22431
|
+
if (dup) handleCollision = { id: dup.id, title: dup.title, status: dup.status };
|
|
22432
|
+
} catch {
|
|
22433
|
+
}
|
|
22289
22434
|
const warnings = [];
|
|
22435
|
+
if (handleCollision) {
|
|
22436
|
+
warnings.push(
|
|
22437
|
+
`handle \u51B2\u7A81\uFF1A\u5DF2\u5B58\u5728\u540C handle \u5546\u54C1\u300C${handleCollision.title}\u300D(id: ${handleCollision.id}, status: ${handleCollision.status})\u3002\u82E5\u662F\u5728\u91CD\u65B0\u5F55\u5165\u540C\u4E00\u5546\u54C1\uFF0C\u8BF7\u6539\u7528 update_product \u4FEE\u6539\u8BE5\u5546\u54C1\uFF0C\u800C\u975E create_product\u3002`
|
|
22438
|
+
);
|
|
22439
|
+
if (input.confirm) {
|
|
22440
|
+
return `\u9519\u8BEF\uFF1Ahandle "${handle}" \u5DF2\u88AB\u5546\u54C1 ${handleCollision.id}\uFF08\u300C${handleCollision.title}\u300D\uFF09\u5360\u7528\uFF0C\u65E0\u6CD5\u91CD\u590D\u521B\u5EFA\u3002\u82E5\u8FD9\u662F\u540C\u4E00\u5546\u54C1\u7684\u91CD\u65B0\u5F55\u5165\uFF0C\u8BF7\u7528 update_product(id=${handleCollision.id}) \u66F4\u65B0\uFF1B\u82E5\u662F\u4E0D\u540C\u5546\u54C1\uFF0C\u8BF7\u6362\u4E00\u4E2A handle \u6216\u6807\u9898\u3002`;
|
|
22441
|
+
}
|
|
22442
|
+
}
|
|
22290
22443
|
if (input.manage_inventory !== false && !input.initial_stock) {
|
|
22291
22444
|
warnings.push(
|
|
22292
22445
|
"manage_inventory=true \u4F46\u672A\u7ED9 initial_stock\uFF1A\u521B\u5EFA\u540E\u4E3A 0 \u5E93\u5B58\uFF0C\u524D\u7AEF\u5C06\u663E\u793A\u7F3A\u8D27\u3002\u6B63\u5F0F\u4E0A\u67B6\u524D\u5FC5\u987B\u8C03\u7528 set_product_stock \u5EFA\u4ED3\u3002"
|
|
@@ -22310,9 +22463,12 @@ SKU / \u89C4\u683C\uFF1Asku \u5199\u5165\u9ED8\u8BA4\u53D8\u4F53\uFF1Bmetadata \
|
|
|
22310
22463
|
weight: input.weight,
|
|
22311
22464
|
manage_inventory: input.manage_inventory,
|
|
22312
22465
|
initial_stock: input.initial_stock,
|
|
22466
|
+
// 实际将写入翻译表的 locale_code(已归一化为 BCP-47,见 canonicalizeTranslations)
|
|
22467
|
+
translation_locales: provided,
|
|
22313
22468
|
missing_translations: missing
|
|
22314
22469
|
},
|
|
22315
22470
|
warnings,
|
|
22471
|
+
handle_collision: handleCollision,
|
|
22316
22472
|
note: "\u672A confirm\uFF0C\u4EC5\u9884\u89C8\u3002\u786E\u8BA4\u8BF7\u8BBE confirm=true\u3002"
|
|
22317
22473
|
};
|
|
22318
22474
|
}
|
|
@@ -22359,15 +22515,17 @@ SKU / \u89C4\u683C\uFF1Asku \u5199\u5165\u9ED8\u8BA4\u53D8\u4F53\uFF1Bmetadata \
|
|
|
22359
22515
|
const data = await client2.createProduct(payload);
|
|
22360
22516
|
const productId = data?.product?.id || data?.id;
|
|
22361
22517
|
let translationResult = null;
|
|
22362
|
-
if (
|
|
22363
|
-
const create = Object.entries(input.translations).map(([locale, t]) => ({
|
|
22364
|
-
reference: "product",
|
|
22365
|
-
reference_id: productId,
|
|
22366
|
-
locale_code: locale,
|
|
22367
|
-
translations: { title: t?.title, description: t?.description }
|
|
22368
|
-
}));
|
|
22518
|
+
if (Object.keys(normTranslations).length) {
|
|
22369
22519
|
try {
|
|
22370
|
-
translationResult = await
|
|
22520
|
+
translationResult = await upsertTranslations(
|
|
22521
|
+
client2,
|
|
22522
|
+
"product",
|
|
22523
|
+
productId,
|
|
22524
|
+
Object.entries(normTranslations).map(([locale, t]) => ({
|
|
22525
|
+
locale_code: locale,
|
|
22526
|
+
translations: { title: t?.title, description: t?.description }
|
|
22527
|
+
}))
|
|
22528
|
+
);
|
|
22371
22529
|
} catch (e) {
|
|
22372
22530
|
return { created: data, warning_translations: `\u7FFB\u8BD1\u5199\u5165\u5931\u8D25 \u2014\u2014 ${e?.message || e}` };
|
|
22373
22531
|
}
|
|
@@ -22418,6 +22576,7 @@ metadata \u4E3A**\u5408\u5E76**\u8BED\u4E49\uFF1A\u65B0\u952E\u8986\u76D6\u540C\
|
|
|
22418
22576
|
confirm: external_exports.boolean().default(false)
|
|
22419
22577
|
},
|
|
22420
22578
|
async run(input, { client: client2 }) {
|
|
22579
|
+
const productId = await client2.resolveProductId(input.id) || String(input.id).trim();
|
|
22421
22580
|
const patch = {};
|
|
22422
22581
|
if (input.title) patch.title = input.title;
|
|
22423
22582
|
if (input.handle) {
|
|
@@ -22445,22 +22604,25 @@ metadata \u4E3A**\u5408\u5E76**\u8BED\u4E49\uFF1A\u65B0\u952E\u8986\u76D6\u540C\
|
|
|
22445
22604
|
if (input[k]) patch[k] = input[k];
|
|
22446
22605
|
}
|
|
22447
22606
|
if (input.metadata && Object.keys(input.metadata).length) {
|
|
22448
|
-
const cur = await client2.getProduct(
|
|
22607
|
+
const cur = await client2.getProduct(productId).catch(() => null);
|
|
22449
22608
|
const existingMeta = (cur?.product ?? cur)?.metadata || {};
|
|
22450
22609
|
patch.metadata = { ...existingMeta, ...input.metadata };
|
|
22451
22610
|
}
|
|
22452
|
-
|
|
22453
|
-
|
|
22611
|
+
const normTranslations = canonicalizeTranslations(input.translations);
|
|
22612
|
+
if (!input.confirm) return { dry_run: true, will_update: { ...patch, translation_locales: Object.keys(normTranslations) } };
|
|
22613
|
+
const data = await client2.updateProduct(productId, patch);
|
|
22454
22614
|
let translationResult = null;
|
|
22455
|
-
if (
|
|
22456
|
-
const create = Object.entries(input.translations).map(([locale, t]) => ({
|
|
22457
|
-
reference: "product",
|
|
22458
|
-
reference_id: input.id,
|
|
22459
|
-
locale_code: locale,
|
|
22460
|
-
translations: { title: t?.title, description: t?.description }
|
|
22461
|
-
}));
|
|
22615
|
+
if (Object.keys(normTranslations).length) {
|
|
22462
22616
|
try {
|
|
22463
|
-
translationResult = await
|
|
22617
|
+
translationResult = await upsertTranslations(
|
|
22618
|
+
client2,
|
|
22619
|
+
"product",
|
|
22620
|
+
productId,
|
|
22621
|
+
Object.entries(normTranslations).map(([locale, t]) => ({
|
|
22622
|
+
locale_code: locale,
|
|
22623
|
+
translations: { title: t?.title, description: t?.description }
|
|
22624
|
+
}))
|
|
22625
|
+
);
|
|
22464
22626
|
} catch (e) {
|
|
22465
22627
|
return { updated: data, warning_translations: `\u7FFB\u8BD1\u5199\u5165\u5931\u8D25 \u2014\u2014 ${e?.message || e}` };
|
|
22466
22628
|
}
|
|
@@ -22488,7 +22650,8 @@ metadata \u4E3A**\u5408\u5E76**\u8BED\u4E49\uFF1A\u65B0\u952E\u8986\u76D6\u540C\
|
|
|
22488
22650
|
variant_id: input.variant_id || "(\u7B2C\u4E00\u4E2A\u53D8\u4F53)"
|
|
22489
22651
|
};
|
|
22490
22652
|
}
|
|
22491
|
-
const
|
|
22653
|
+
const productId = await client2.resolveProductId(input.product_id) || String(input.product_id).trim();
|
|
22654
|
+
const prod = await client2.getProduct(productId).catch(() => null);
|
|
22492
22655
|
const product = prod?.product ?? prod;
|
|
22493
22656
|
if (!product) return `\u9519\u8BEF\uFF1A\u627E\u4E0D\u5230\u5546\u54C1 ${input.product_id}`;
|
|
22494
22657
|
const variants = product.variants || [];
|
|
@@ -22496,7 +22659,7 @@ metadata \u4E3A**\u5408\u5E76**\u8BED\u4E49\uFF1A\u65B0\u952E\u8986\u76D6\u540C\
|
|
|
22496
22659
|
const target = input.variant_id ? variants.find((v) => v.id === input.variant_id) : variants[0];
|
|
22497
22660
|
if (!target) return `\u9519\u8BEF\uFF1A\u627E\u4E0D\u5230\u76EE\u6807\u53D8\u4F53\uFF08\u6307\u5B9A ${input.variant_id} \u6216\u7B2C\u4E00\u4E2A\u53D8\u4F53\uFF09\u3002`;
|
|
22498
22661
|
const data = await client2.updateProductPrices(
|
|
22499
|
-
|
|
22662
|
+
productId,
|
|
22500
22663
|
target.id,
|
|
22501
22664
|
input.prices.map((p) => ({ amount: p.amount, currency_code: p.currency_code }))
|
|
22502
22665
|
);
|
|
@@ -22512,7 +22675,8 @@ metadata \u4E3A**\u5408\u5E76**\u8BED\u4E49\uFF1A\u65B0\u952E\u8986\u76D6\u540C\
|
|
|
22512
22675
|
},
|
|
22513
22676
|
async run(input, { client: client2 }) {
|
|
22514
22677
|
if (!input.confirm) return { dry_run: true, will_set_status: "archived" };
|
|
22515
|
-
|
|
22678
|
+
const productId = await client2.resolveProductId(input.id) || String(input.id).trim();
|
|
22679
|
+
return client2.updateProduct(productId, { status: "archived" });
|
|
22516
22680
|
}
|
|
22517
22681
|
},
|
|
22518
22682
|
{
|
|
@@ -22573,15 +22737,19 @@ metadata \u4E3A**\u5408\u5E76**\u8BED\u4E49\uFF1A\u65B0\u952E\u8986\u76D6\u540C\
|
|
|
22573
22737
|
{
|
|
22574
22738
|
name: "get_product_content",
|
|
22575
22739
|
description: `\u8BFB\u53D6\u5546\u54C1\u8BE6\u60C5\u9875\u5BCC\u5185\u5BB9\u5757\uFF08GET /admin/product-contents\uFF09\u3002
|
|
22576
|
-
locale \
|
|
22740
|
+
locale \u63A5\u53D7\u533A\u57DF\u7801 en-US / zh-CN / ms-MY / id-ID\uFF08\u63A8\u8350\uFF0C\u4E0E\u524D\u7AEF\u4E00\u81F4\uFF09\u6216\u77ED\u7801 en / zh / ms / id\uFF08\u81EA\u52A8\u5F52\u4E00\u5316\u4E3A\u533A\u57DF\u7801\uFF09\u3002
|
|
22577
22741
|
\u8FD4\u56DE { product_content: { id, product_id, locale, status, blocks(\u5DF2\u89E3\u6790\u6570\u7EC4) } | null }\u3002`,
|
|
22578
22742
|
schema: {
|
|
22579
22743
|
product_id: external_exports.string(),
|
|
22580
|
-
locale: external_exports.
|
|
22744
|
+
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"),
|
|
22581
22745
|
status: external_exports.enum(["draft", "published"]).default("published")
|
|
22582
22746
|
},
|
|
22583
22747
|
async run(input, { client: client2 }) {
|
|
22584
|
-
const
|
|
22748
|
+
const locale = normalizeLocale(input.locale);
|
|
22749
|
+
if (!locale) {
|
|
22750
|
+
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`;
|
|
22751
|
+
}
|
|
22752
|
+
const data = await client2.getProductContent(input.product_id, locale, input.status);
|
|
22585
22753
|
const pc = data?.product_content;
|
|
22586
22754
|
if (!pc) return { product_content: null };
|
|
22587
22755
|
let blocks = [];
|
|
@@ -22604,11 +22772,11 @@ storefront \u8BE6\u60C5\u9875\u300C\u5BCC\u5185\u5BB9\u300D\u533A\u7531\u8FD9\u4
|
|
|
22604
22772
|
- video: data.{ url, poster? }\uFF08\u652F\u6301 YouTube\uFF09
|
|
22605
22773
|
- faq: data.{ items: {question,answer}[] }
|
|
22606
22774
|
- specs: data.{ rows: {label,value}[] }
|
|
22607
|
-
\u26A0\uFE0F locale \
|
|
22775
|
+
\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
|
|
22608
22776
|
\u9ED8\u8BA4 dry_run\uFF08confirm=false\uFF09\u4EC5\u9884\u89C8\u3002`,
|
|
22609
22777
|
schema: {
|
|
22610
22778
|
product_id: external_exports.string(),
|
|
22611
|
-
locale: external_exports.
|
|
22779
|
+
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"),
|
|
22612
22780
|
status: external_exports.enum(["draft", "published"]).default("published"),
|
|
22613
22781
|
blocks: external_exports.array(
|
|
22614
22782
|
external_exports.object({
|
|
@@ -22620,12 +22788,16 @@ storefront \u8BE6\u60C5\u9875\u300C\u5BCC\u5185\u5BB9\u300D\u533A\u7531\u8FD9\u4
|
|
|
22620
22788
|
confirm: external_exports.boolean().default(false)
|
|
22621
22789
|
},
|
|
22622
22790
|
async run(input, { client: client2 }) {
|
|
22791
|
+
const locale = normalizeLocale(input.locale);
|
|
22792
|
+
if (!locale) {
|
|
22793
|
+
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`;
|
|
22794
|
+
}
|
|
22623
22795
|
if (!input.confirm) {
|
|
22624
22796
|
return {
|
|
22625
22797
|
dry_run: true,
|
|
22626
22798
|
will_create: {
|
|
22627
22799
|
product_id: input.product_id,
|
|
22628
|
-
locale
|
|
22800
|
+
locale,
|
|
22629
22801
|
status: input.status,
|
|
22630
22802
|
block_count: input.blocks.length,
|
|
22631
22803
|
types: input.blocks.map((b) => b.type)
|
|
@@ -22634,7 +22806,7 @@ storefront \u8BE6\u60C5\u9875\u300C\u5BCC\u5185\u5BB9\u300D\u533A\u7531\u8FD9\u4
|
|
|
22634
22806
|
}
|
|
22635
22807
|
const data = await client2.createProductContent({
|
|
22636
22808
|
product_id: input.product_id,
|
|
22637
|
-
locale
|
|
22809
|
+
locale,
|
|
22638
22810
|
status: input.status,
|
|
22639
22811
|
blocks: input.blocks
|
|
22640
22812
|
});
|
|
@@ -22799,6 +22971,7 @@ ${r.snippet}`).join("\n\n");
|
|
|
22799
22971
|
// src/tools/index.ts
|
|
22800
22972
|
var toolDefs = [
|
|
22801
22973
|
...productTools,
|
|
22974
|
+
...translationTools,
|
|
22802
22975
|
...stockTools,
|
|
22803
22976
|
...opsTools,
|
|
22804
22977
|
...docTools
|
|
@@ -22822,7 +22995,7 @@ var config2 = loadConfig();
|
|
|
22822
22995
|
var auth = new AuthManager(config2);
|
|
22823
22996
|
var client = new MedusaAdminClient(config2, auth);
|
|
22824
22997
|
var ctx = { client, config: config2 };
|
|
22825
|
-
var server = new McpServer({ name: "derlux-admin", version: "0.1.
|
|
22998
|
+
var server = new McpServer({ name: "derlux-admin", version: "0.1.5" });
|
|
22826
22999
|
for (const def of toolDefs) {
|
|
22827
23000
|
server.tool(def.name, def.description, def.schema, async (args) => {
|
|
22828
23001
|
try {
|
package/package.json
CHANGED
package/skill/SKILL.md
CHANGED
|
@@ -29,10 +29,13 @@ description: 运营使用本地 AI agent(derlux-admin MCP server)操作 DERL
|
|
|
29
29
|
6. **商品必须挂销售渠道才在前端可见**:新建商品默认**不在任何销售渠道**,storefront(按 publishable key → 渠道过滤)拉不到它,详情页表现成「空/404」。录入后用 `add_product_to_sales_channel`(`confirm=true`)把它加入渠道(默认第一个启用渠道;马来西亚站用 `sc_01M4K4F0D6HBHZ0NH6WB7H5KPT`「MY Storefront」)。
|
|
30
30
|
7. **两套 locale 码,别混**:
|
|
31
31
|
- 商品标题/描述的**翻译**(`create_product` 的 `translations`、后端 `/admin/translations/batch`):用**简码** `en` / `zh` / `ms` / `id`。store `/store/products?locale=zh` 即回退到 zh 翻译。
|
|
32
|
-
- 商品详情页**富内容块**(`create_product_content` 的 `locale
|
|
32
|
+
- 商品详情页**富内容块**(`create_product_content` / `get_product_content` 的 `locale`):推荐用**区域码** `en-US` / `zh-CN` / `ms-MY` / `id-ID`(与前端 `useProductContent` 的 localeMap 一致);**简码 `en`/`zh`/`ms`/`id` 也会被工具自动归一化为区域码**,两种写法都能命中,但请统一用区域码避免歧义。
|
|
33
33
|
8. **详情页「富内容」区靠 `create_product_content`,不是 `description`**:storefront 详情页下方大块内容由 `product_content` 模块渲染(`richtext`/`specs`/`image_gallery` 等)。只填 `product.description` 不会显示在那里——录入完整商品时必须再建一份 `product_content`(建议每语言一份)。`get_product` 默认不含该字段,核对请用 `get_product_content`。
|
|
34
|
+
- ⚠️ **storefront 对富内容按 `locale` 精确匹配、无回退(实测)**:`/store/product-contents?handle=&locale=zh-CN` 只取 `locale_code===zh-CN` 且 `status=published` 的那一行,**缺该语言行即返回 `null`=该语言详情页空白,不会回退到英文**。故四语言(`en-US/zh-CN/ms-MY/id-ID`)都要各建一份 `product_content`,漏任一语言则该语言详情页空。
|
|
35
|
+
- ⚠️ **富内容块字段名易错**:`richtext` 用 `data.html`(**不是** `data.content`)、`specs` 用 `data.rows`(**不是** `data.specs`);`image_gallery` 用 `data.images`。写错字段名块会渲染为空。
|
|
34
36
|
9. **不建库存 = 前端缺货**:变体 `manage_inventory` 默认 `true`,但新商品默认 0 库存——`create_product` 时必须给 `initial_stock`(如 `{quantity:100}`,默认写 Main Warehouse),或对已有商品调 `set_product_stock`,否则前台显示缺货、无法下单。
|
|
35
37
|
10. **多语言核对用 `get_translations`**:`get_product` 默认字段**不含** translations,不要凭它为空就判断没翻译。用 `get_translations`(reference_id=商品id或handle) 读取实际写入的各语言 title/description。
|
|
38
|
+
11. **创建前先查重 + 警惕 handle 归一化产生不同结果**:运营给的名字经归一化(小写、`空格/下划线/点→连字符`)后,**不同写法可能落到不同 handle**(例:`DR CYJ X`→`dr-cyj-x`、`DR.CYJ X`→`drcyj-x`),从而**静默建出一个近乎重复的空商品**(本包实际踩过:`dr-cyj-hair-filler-hair-grow` 与 `drcyj-hair-filler-hair-grow` 是两个商品)。故 `create_product` 前必先用 `list_products`/`get_product`(按相近名或 handle 搜一遍)确认无同款;已存在则转「补齐/修改」流程(`update_product` / `set_translations` / `create_product_content` / `set_product_stock`),不要重复 `create_product`。
|
|
36
39
|
|
|
37
40
|
## 工具速查表
|
|
38
41
|
|
|
@@ -43,6 +46,7 @@ description: 运营使用本地 AI agent(derlux-admin MCP server)操作 DERL
|
|
|
43
46
|
| `list_sales_channels` | 读 | 否 | 直接返回(渠道 id/名称,建商品前确认用)|
|
|
44
47
|
| `list_collections` | 读 | 否 | 直接返回 |
|
|
45
48
|
| `get_translations` | 读 | 否 | 直接返回(某商品各语言的 title/description,多语言核对唯一可靠途径)|
|
|
49
|
+
| `set_translations` | 写 | 是(`confirm=true` 才写)| **upsert** 多语言翻译(title/subtitle/description):同语言已存在→更新(字段合并、绝不重复行),否则新建;短码/区域码统一归一化为 BCP-47 落库,并**改码修复**历史短码行;`confirm=false` 仅预览 |
|
|
46
50
|
| `list_stock_locations` | 读 | 否 | 直接返回 |
|
|
47
51
|
| `get_inventory_levels` | 读 | 否 | 直接返回(location_id 可选:按仓库列库存条目与水平;q 按 sku/title 过滤)|
|
|
48
52
|
| `set_product_stock` | 写 | 是(`confirm=true` 才写)| 幂等建仓:自动找/建库存条目 + 写仓库数量 + manage_inventory;`confirm=false` 仅预览 |
|
|
@@ -124,6 +128,12 @@ description: 运营使用本地 AI agent(derlux-admin MCP server)操作 DERL
|
|
|
124
128
|
- `faq`:`data.{ items: {question,answer}[] }`
|
|
125
129
|
- `specs`:`data.{ rows: {label,value}[] }`
|
|
126
130
|
- `confirm=false` 返回预览(含块数量与类型);`confirm=true` 才写入。
|
|
131
|
+
- **`set_translations`**:**创建或更新(upsert)**某实体的多语言翻译(title/subtitle/description),走 `/admin/translations/batch`。用于商品创建后补/改翻译、修复多语言。
|
|
132
|
+
- `reference`(string,默认 `product`);`reference_id`(string,**必填**,商品可传 handle,自动解析为 id);`translations`(`record<locale,{title?,subtitle?,description?}>`,**必填**,如 `{"zh-CN":{"title":"…","description":"…"},"ms-MY":{…},"id-ID":{…}}`);`confirm`(默认 false)。
|
|
133
|
+
- **upsert 语义**:目标语言已有翻译 → 更新(字段**合并**,调用里没给的旧字段保留,**绝不产生重复行**);没有 → 新建。
|
|
134
|
+
- **locale 归一化**:key 接受短码(`en`/`zh`/`ms`/`id`)或 BCP-47 区域码(`en-US`/`zh-CN`/`ms-MY`/`id-ID`),工具统一归一化为 **BCP-47** 落库。storefront 对翻译接口一律请求 BCP-47、与库中 `locale_code` 精确匹配——历史按短码(如 `zh`)落库的旧行会被本次调用**改码修复**(update 时把 `locale_code` 顺带改成区域码,见 `matched_existing`)。请统一用区域码避免歧义。
|
|
135
|
+
- `confirm=false` 返回 `{dry_run:true, will_create, will_update, matched_existing, note}` 预览;`confirm=true` 才写入并返回 `{created, updated, result}`。
|
|
136
|
+
- 与 `create_product`/`update_product` 内嵌的 `translations` 区别:本工具对**已存在商品**逐语言 upsert 更精准(合并而非整表覆盖、不重复行),补翻译/改翻译优先用它。
|
|
127
137
|
|
|
128
138
|
### 写 —— 仓库与库存
|
|
129
139
|
- **`create_stock_location`**:新增仓库(StockLocation)。
|
|
@@ -138,12 +148,18 @@ description: 运营使用本地 AI agent(derlux-admin MCP server)操作 DERL
|
|
|
138
148
|
|
|
139
149
|
## 推荐操作流程(SOP)
|
|
140
150
|
|
|
141
|
-
|
|
142
|
-
1.
|
|
143
|
-
2.
|
|
144
|
-
3.
|
|
145
|
-
4. **`create_product_content`(`confirm=true`,逐语言 `en-US/zh-CN/ms-MY/id-ID
|
|
146
|
-
5.
|
|
151
|
+
**创建商品(稳妥 6 步,含查重与上线核对)**
|
|
152
|
+
1. **查重 + 收料**:先 `list_products`/`get_product` 按相近名或 handle 搜一遍,确认无已存在同款(handle 归一化后不同写法可能产生**不同** handle → 静默重复商品,见硬规则 11);若已存在,转「修改/补齐」流程而非新建。同时收集:标题、价格(主单位)、四语言标题/描述、图片、规格、重量、初始库存、目标渠道、各语言富文本。
|
|
153
|
+
2. **预览**:`create_product`(`confirm=false`)→ 把「缺语言 / 缺货 / 是否疑似重复」读给运营。
|
|
154
|
+
3. 补全缺失语言与 `initial_stock`;运营确认后 `create_product`(`confirm=true`,带 `sales_channels`、`initial_stock`,正式上架加 `status: "published"`,必要时 `acknowledge_missing_languages=true`)。此步即完成:图片上传、SKU、四语言翻译、渠道挂载、建库存。
|
|
155
|
+
4. **`create_product_content`(`confirm=true`,逐语言 `en-US/zh-CN/ms-MY/id-ID` 各建一份)** → 写详情页富内容(`richtext` + `specs` + `image_gallery`)。storefront 对富内容**按语言精确匹配、无回退**,漏任一语言则该语言详情页空白(见硬规则 8)。
|
|
156
|
+
5. **(可选)** 仅补/改已存在语言的 title/description 时用 `set_translations`(upsert/合并不重复、自动把历史短码改码为区域码,见硬规则 7 / 该工具说明)。
|
|
157
|
+
6. **上线核对(go-live checklist)——全绿才算交付完成**:
|
|
158
|
+
- **前台可见**:`get_product` 看 `status=published` 且已挂销售渠道;
|
|
159
|
+
- **多语言齐全**:`get_translations` 看 4 条 locale 都有 title/description;
|
|
160
|
+
- **各语言详情页富内容齐全**:`get_product_content` 逐语言各取一次,4 语言均非空(无回退,见硬规则 8);
|
|
161
|
+
- **有货可下单**:`get_inventory_levels`(目标仓)quantity>0。
|
|
162
|
+
全绿后把新商品 id/handle 告知运营。
|
|
147
163
|
|
|
148
164
|
**修改 / 下架商品**
|
|
149
165
|
1. `get_product` 或 `list_products` 确认目标 id。
|
|
@@ -160,12 +176,14 @@ description: 运营使用本地 AI agent(derlux-admin MCP server)操作 DERL
|
|
|
160
176
|
## 常见任务示例(运营怎么说 / 你怎么调)
|
|
161
177
|
|
|
162
178
|
- 「下架 handle 为 `summer-serum` 的商品」→ `get_product`(id_or_handle=`summer-serum`) 取 id → `deactivate_product`(id, confirm=false 预览 → 确认后 confirm=true)。
|
|
179
|
+
- 「录入 DR.CYJ Hair Filler,价格 420,4 图,补全多语言」→ **先** `list_products`(q=`hair filler`) / `get_product`(handle=候选) 查重;若已存在同款 → 走 `update_product`/`set_translations`/`create_product_content`/`set_product_stock` 补齐,**不要新建**。确认无同款后再按「创建商品 6 步」执行。
|
|
163
180
|
- 「给 A 商品补 100 件库存」→ `set_product_stock`(product_id=A 的 id, quantity=100, confirm=false 预览 → 确认后 confirm=true;默认写 Main Warehouse,指定仓传 location_id)。
|
|
164
181
|
- 「查一下最近 10 个待发货订单」→ `list_orders`(status=`pending`, limit=10)。
|
|
165
182
|
- 「后台怎么新建优惠券」→ `ask_usage`(question=`如何新建优惠券`)。
|
|
166
183
|
- 「列出所有退货请求」→ `list_return_requests`()。
|
|
167
184
|
- 「把这份抓取的 Lazada 商品录入:标题 DR.CYJ Hair Filler、价格 420 MYR、4 张本地图、SKU `4820...`、规格存 metadata、补全中/马来/印尼语翻译」→ 先整理数据,`create_product`(`confirm=false` 看预览含 images/sku/metadata/缺语言/缺货告警 → 确认后 `confirm=true`,带 `sales_channels: ["MY Storefront"]`、`initial_stock: {quantity: 100}`、`weight: 150`,必要时 `acknowledge_missing_languages=true`)。工具会自动上传图片、写 SKU/metadata/物理字段、四语言翻译、渠道挂载并建库存。再 `create_product_content`(`locale=en-US/zh-CN/ms-MY/id-ID`, `confirm=true`) 逐语言写富内容块(richtext 描述 + specs 规格 + image_gallery 图集),最后 `get_translations` + `get_product_content` 核对。
|
|
168
185
|
- 「核对某商品多语言是否齐全」→ `get_translations`(reference_id=商品 id 或 handle),看 4 条 locale 是否都有 title/description。
|
|
186
|
+
- 「给某商品补全 / 更新中·马来·印尼语标题描述」→ `set_translations`(reference_id=商品 id 或 handle, `translations={"zh-CN":{title,description},"ms-MY":{…},"id-ID":{…}}`, confirm=false 预览 → confirm=true)。同语言已存在则合并更新、不重复行;旧短码行会被自动改码为区域码。
|
|
169
187
|
- 「让这个商品在前端能搜到」→ `get_product` 取 id → `add_product_to_sales_channel`(`product_ids=[id]`, `confirm=true`)。
|
|
170
188
|
- 「补全某商品中文详情页内容」→ `create_product_content`(`product_id`, `locale=zh-CN`, `blocks=[{type:'richtext',data:{html:'...'}},{type:'specs',data:{rows:[...]}}]`, `confirm=true`)。可用 `get_product_content` 先核对现状。
|
|
171
189
|
|