query_headhaul_document_list.py 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. from tools.headhaul_document_common import (
  2. NUMBER_FIELDS,
  3. bounded_integer,
  4. number_list,
  5. number_properties,
  6. )
  7. class QueryHeadhaulDocumentListTool:
  8. name = 'query_headhaul_document_list'
  9. route_path = '/mcp/tools/queryHeadhaulDocumentList'
  10. def __init__(self, api_client=None):
  11. self.api_client = api_client
  12. def metadata(self):
  13. properties = {
  14. 'document_type_id': {
  15. 'type': 'integer',
  16. 'minimum': 1,
  17. 'description': (
  18. '文档类型。必须先调用list_headhaul_document_filter_options'
  19. '并选择“查询文档类型”取得可传值。'
  20. ),
  21. },
  22. 'original_name': {
  23. 'type': 'string',
  24. 'minLength': 1,
  25. 'maxLength': 255,
  26. 'description': '完整原始文件名,精确匹配,不是模糊搜索。',
  27. },
  28. }
  29. properties.update(number_properties())
  30. properties['page'] = {
  31. 'type': 'integer', 'minimum': 1, 'maximum': 100, 'default': 1,
  32. }
  33. properties['limit'] = {
  34. 'type': 'integer', 'minimum': 1, 'maximum': 100, 'default': 20,
  35. }
  36. return {
  37. 'name': self.name,
  38. 'description': (
  39. '使用场景:用户要看头程文档列表时,必须先有文档类型、完整文件名'
  40. '或已经标明种类的录入单号之一,再查询当前页。对照后台'
  41. 'admin/document/index,一行一份文件或外链。号码类型不明确时必须先询问用户,'
  42. '不得根据号码格式猜测,不得跨字段或跨工具试查,也不得改走'
  43. 'query_customs_declaration_files或订单附件。'
  44. '禁止使用:空扫全表、混查单号框、行内编辑、把文件放进工具参数。'
  45. '参数只用于工具内部调用;最终回答只能展示中文业务名称;'
  46. '描述筛选条件时不得展示筛选字段的英文参数名。'
  47. ),
  48. 'input_schema': {
  49. 'type': 'object',
  50. 'properties': properties,
  51. 'additionalProperties': False,
  52. },
  53. }
  54. def call(
  55. self,
  56. document_type_id=None,
  57. original_name=None,
  58. page=1,
  59. limit=20,
  60. request_id='rq_query_headhaul_document_list',
  61. **numbers
  62. ):
  63. if self.api_client is None:
  64. raise RuntimeError(
  65. 'api client is required for query_headhaul_document_list'
  66. )
  67. payload = {}
  68. if document_type_id is not None:
  69. if isinstance(document_type_id, bool) or not isinstance(
  70. document_type_id, int
  71. ) or document_type_id < 1:
  72. raise ValueError('document_type_id is invalid')
  73. payload['document_type_id'] = document_type_id
  74. if original_name is not None:
  75. name = str(original_name).strip()
  76. if not name or len(name) > 255:
  77. raise ValueError('original_name is invalid')
  78. payload['original_name'] = name
  79. for field in NUMBER_FIELDS:
  80. if field in numbers and numbers[field] is not None:
  81. payload[field] = number_list(numbers[field], field)
  82. if (
  83. 'document_type_id' not in payload
  84. and 'original_name' not in payload
  85. and not any(field in payload for field in NUMBER_FIELDS)
  86. ):
  87. raise ValueError('locator is required')
  88. payload['page'] = bounded_integer(page, 'page')
  89. payload['limit'] = bounded_integer(limit, 'limit')
  90. return self.api_client.call_tool(
  91. self.name, self.route_path, payload, request_id,
  92. )