export_out_of_province_port_data.py 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. class ExportOutOfProvincePortDataTool:
  2. name = 'export_out_of_province_port_data'
  3. route_path = '/mcp/tools/exportOutOfProvincePortData'
  4. def __init__(self, api_client=None):
  5. self.api_client = api_client
  6. def metadata(self):
  7. number_items = {
  8. 'type': 'string',
  9. 'minLength': 1,
  10. 'maxLength': 100,
  11. }
  12. return {
  13. 'name': self.name,
  14. 'description': (
  15. '导出后台排舱单列表中的省外进港资料,并返回可下载文件 URL。'
  16. '只能在用户明确要求导出省外进港资料,并明确提供排舱单号、'
  17. '柜号、提单号或SO号中的一种,以及宁波、上海或美森资料类型时调用。'
  18. '用户没有明确号码类型时必须先询问:'
  19. '“请确认使用哪种单号导出:排舱单号、柜号、提单号还是SO号?”'
  20. '确认前不得调用。不得根据号码格式猜测,也不得在查询失败后'
  21. '切换号码类型重试。一次只能选择一种号码类型。'
  22. '一次只能选择一种资料类型;用户要求多个类型时应分别调用。'
  23. '参数名仅用于工具调用;向用户回答时只能使用中文业务名称,'
  24. '不得展示内部参数名。'
  25. ),
  26. 'input_schema': {
  27. 'type': 'object',
  28. 'properties': {
  29. 'outbound_numbers': {
  30. 'type': 'array',
  31. 'items': dict(number_items),
  32. 'minItems': 1,
  33. 'maxItems': 100,
  34. 'uniqueItems': True,
  35. 'description': (
  36. '要导出的排舱单号数组。排舱单号是后台排舱单列表中的'
  37. '业务单号,不是系统订单号、客户参考号、快递单号、柜号、'
  38. 'SO号或Shipment ID。只能使用用户明确提供并确认类型为'
  39. '排舱单号的值,不得根据号码格式猜测。'
  40. ),
  41. 'examples': [[
  42. 'PC202607140001',
  43. 'PC202607140002',
  44. ]],
  45. },
  46. 'container_codes': {
  47. 'type': 'array',
  48. 'items': dict(number_items),
  49. 'minItems': 1,
  50. 'maxItems': 100,
  51. 'uniqueItems': True,
  52. 'description': (
  53. '要导出的柜号或集装箱号数组。仅当用户明确说明号码类型'
  54. '为“柜号”或“集装箱号”时使用,不得放入排舱单号或提单号。'
  55. ),
  56. 'examples': [[
  57. 'MSCU1234567',
  58. 'TGHU7654321',
  59. ]],
  60. },
  61. 'bl_numbers': {
  62. 'type': 'array',
  63. 'items': dict(number_items),
  64. 'minItems': 1,
  65. 'maxItems': 100,
  66. 'uniqueItems': True,
  67. 'description': (
  68. '要导出的提单号数组,对应后台排舱单列表中的“提单号”。'
  69. '不是系统提单号;仅当用户明确说明号码类型为“提单号”时使用。'
  70. ),
  71. 'examples': [[
  72. 'BL202607150001',
  73. 'BL202607150002',
  74. ]],
  75. },
  76. 'so_numbers': {
  77. 'type': 'array',
  78. 'items': dict(number_items),
  79. 'minItems': 1,
  80. 'maxItems': 100,
  81. 'uniqueItems': True,
  82. 'description': (
  83. '要导出的SO号数组,对应后台排舱单列表中的“SO号”,'
  84. '数据字段为fms_booking_detail.so_number。仅当用户'
  85. '明确说明号码类型为“SO号”时使用。'
  86. ),
  87. 'examples': [[
  88. 'SO202607160001',
  89. 'SO202607160002',
  90. ]],
  91. },
  92. 'file_type': {
  93. 'type': 'string',
  94. 'enum': ['NB', 'SH', 'MS'],
  95. 'description': (
  96. '进港资料类型:NB=宁波进港资料,SH=上海进港资料,'
  97. 'MS=美森进港资料。必须根据用户明确选择传值;'
  98. '用户未说明时先询问,不得默认选择。'
  99. ),
  100. 'examples': ['NB'],
  101. },
  102. },
  103. 'required': ['file_type'],
  104. 'oneOf': [
  105. {'required': ['outbound_numbers']},
  106. {'required': ['container_codes']},
  107. {'required': ['bl_numbers']},
  108. {'required': ['so_numbers']},
  109. ],
  110. 'additionalProperties': False,
  111. },
  112. }
  113. def call(
  114. self,
  115. outbound_numbers=None,
  116. file_type=None,
  117. request_id='rq_export_out_of_province_port_data',
  118. container_codes=None,
  119. bl_numbers=None,
  120. so_numbers=None,
  121. ):
  122. if self.api_client is None:
  123. raise RuntimeError(
  124. 'api client is required for export_out_of_province_port_data'
  125. )
  126. number_fields = {
  127. 'outbound_numbers': outbound_numbers,
  128. 'container_codes': container_codes,
  129. 'bl_numbers': bl_numbers,
  130. 'so_numbers': so_numbers,
  131. }
  132. provided = [
  133. field for field, values in number_fields.items()
  134. if values is not None
  135. ]
  136. if len(provided) != 1:
  137. raise ValueError('provide exactly one number type')
  138. number_field = provided[0]
  139. numbers = self._normalize_numbers(number_fields[number_field], number_field)
  140. normalized_type = str(file_type or '').strip().upper()
  141. if normalized_type not in ('NB', 'SH', 'MS'):
  142. raise ValueError('file_type must be NB, SH, or MS')
  143. return self.api_client.call_tool(
  144. self.name,
  145. self.route_path,
  146. {
  147. number_field: numbers,
  148. 'file_type': normalized_type,
  149. },
  150. request_id,
  151. )
  152. @staticmethod
  153. def _normalize_numbers(values, field):
  154. if not isinstance(values, list):
  155. raise ValueError('{0} must be an array'.format(field))
  156. if len(values) > 100:
  157. raise ValueError('{0} must contain 1 to 100 values'.format(field))
  158. numbers = []
  159. for value in values:
  160. if not isinstance(value, str):
  161. raise ValueError('{0} must contain strings'.format(field))
  162. value = value.strip()
  163. if not value:
  164. continue
  165. if len(value) > 100:
  166. raise ValueError('{0} contains a number that is too long'.format(field))
  167. if value not in numbers:
  168. numbers.append(value)
  169. if not numbers or len(numbers) > 100:
  170. raise ValueError('{0} must contain 1 to 100 values'.format(field))
  171. return numbers