| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178 |
- import re
- class QueryPendingGoodsListTool:
- name = 'query_pending_goods_list'
- route_path = '/mcp/tools/queryPendingGoodsList'
- SHIPPING_METHODS = ('空运', '海运', '陆运')
- GOODS_ATTRS = ('普货', '带电', '带磁', '玩具', '敏感货', '食品')
- def __init__(self, api_client=None):
- self.api_client = api_client
- def metadata(self):
- return {
- 'name': self.name,
- 'description': (
- '使用场景:用户要看待审核商品时调用。只查商品审核页待审核页签,'
- '可以不带筛选直接看第一页。客户、起运国、目的国必须先调用'
- 'list_pending_goods_filter_options取得可传值;同名先让用户选定。'
- '第一列是商品编号。要审核时先用这个编号调用 open_goods_audit。'
- '禁止使用:在本工具里审核、驳回或导出,不得改走query_goods_info。'
- '参数只用于工具内部调用;最终回答只能展示中文业务名称;'
- '描述筛选条件时不得展示筛选字段的英文参数名。'
- ),
- 'input_schema': {
- 'type': 'object',
- 'properties': {
- 'skus': {
- 'type': 'array',
- 'maxItems': 50,
- 'items': {
- 'type': 'string',
- 'minLength': 1,
- 'maxLength': 200,
- },
- 'description': 'SKU,精确匹配,最多50个。多项表示命中任意一个。',
- },
- 'customer_id': {
- 'type': 'integer',
- 'minimum': 1,
- 'description': '客户ID。必须先调用筛选项选择「客户名称」取得value。',
- },
- 'departure_country': {
- 'type': 'string',
- 'pattern': '^[A-Za-z]{2,3}$',
- 'description': '起运国代码。必须先调用筛选项选择「起运国」取得value。',
- },
- 'destination_country': {
- 'type': 'string',
- 'pattern': '^[A-Za-z]{2,3}$',
- 'description': '目的国代码。必须先调用筛选项选择「目的国」取得value。',
- },
- 'shipping_method': {
- 'type': 'string',
- 'enum': list(self.SHIPPING_METHODS),
- 'description': '运输方式,只能是空运、海运、陆运。',
- },
- 'goods_attrs': {
- 'type': 'array',
- 'maxItems': 6,
- 'items': {
- 'type': 'string',
- 'enum': list(self.GOODS_ATTRS),
- },
- 'description': '商品属性。多项表示这条商品同时具备。普货是商品分类。',
- },
- 'page': {
- 'type': 'integer',
- 'minimum': 1,
- 'maximum': 100,
- 'default': 1,
- },
- 'limit': {
- 'type': 'integer',
- 'minimum': 1,
- 'maximum': 100,
- 'default': 20,
- },
- },
- 'additionalProperties': False,
- },
- }
- def call(
- self,
- skus=None,
- customer_id=None,
- departure_country=None,
- destination_country=None,
- shipping_method=None,
- goods_attrs=None,
- page=1,
- limit=20,
- request_id='rq_query_pending_goods_list',
- ):
- if self.api_client is None:
- raise RuntimeError(
- 'api client is required for query_pending_goods_list'
- )
- payload = {
- 'page': self._bounded_integer(page, 'page'),
- 'limit': self._bounded_integer(limit, 'limit'),
- }
- if skus is not None:
- payload['skus'] = self._skus(skus)
- if customer_id is not None:
- payload['customer_id'] = self._customer_id(customer_id)
- if departure_country is not None:
- payload['departure_country'] = self._country(
- departure_country, 'departure_country'
- )
- if destination_country is not None:
- payload['destination_country'] = self._country(
- destination_country, 'destination_country'
- )
- if shipping_method is not None:
- text = str(shipping_method).strip()
- if text not in self.SHIPPING_METHODS:
- raise ValueError('shipping_method is invalid')
- payload['shipping_method'] = text
- if goods_attrs is not None:
- payload['goods_attrs'] = self._attrs(goods_attrs)
- return self.api_client.call_tool(
- self.name, self.route_path, payload, request_id,
- )
- def _skus(self, values):
- if isinstance(values, str):
- values = re.split(r'[\r\n,,;;、]+', values)
- if not isinstance(values, list):
- raise ValueError('skus is invalid')
- result = []
- for item in values:
- for piece in re.split(r'[\r\n,,;;、]+', str(item)):
- sku = piece.strip()
- if not sku:
- continue
- if len(sku) > 200:
- raise ValueError('skus is invalid')
- if sku not in result:
- result.append(sku)
- if not result or len(result) > 50:
- raise ValueError('skus is invalid')
- return result
- @staticmethod
- def _customer_id(value):
- if isinstance(value, bool) or not isinstance(value, int) or value < 1:
- raise ValueError('customer_id is invalid')
- return value
- @staticmethod
- def _country(value, field):
- text = str(value or '').strip().upper()
- if not re.match(r'^[A-Z]{2,3}$', text):
- raise ValueError(field + ' is invalid')
- return text
- def _attrs(self, values):
- if not isinstance(values, list) or not values:
- raise ValueError('goods_attrs is invalid')
- result = []
- for item in values:
- name = str(item).strip()
- if name not in self.GOODS_ATTRS:
- raise ValueError('goods_attrs is invalid')
- if name not in result:
- result.append(name)
- return result
- @staticmethod
- def _bounded_integer(value, field):
- if isinstance(value, bool) or not isinstance(value, int):
- raise ValueError(field + ' is invalid')
- if value < 1 or value > 100:
- raise ValueError(field + ' is invalid')
- return value
|