xai_video_tools.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #!/usr/bin/env python3
  2. """xAI-specific Imagine video edit and extend tools."""
  3. from __future__ import annotations
  4. import json
  5. from typing import Any, Dict, Optional
  6. from hermes_cli.config import load_config
  7. from plugins.video_gen.xai import has_xai_video_credentials, run_xai_video_edit, run_xai_video_extend
  8. from tools.registry import registry, tool_error
  9. def _configured_for_xai_video() -> bool:
  10. try:
  11. section = load_config().get("video_gen")
  12. except Exception:
  13. return False
  14. return isinstance(section, dict) and section.get("provider") == "xai"
  15. def _check_xai_video_requirements() -> bool:
  16. return _configured_for_xai_video() and has_xai_video_credentials()
  17. def _clean_string(value: Any) -> Optional[str]:
  18. return value.strip() if isinstance(value, str) and value.strip() else None
  19. def _coerce_int(value: Any) -> Optional[int]:
  20. # bool is rejected (unlike video_generation_tool._coerce_int) so duration=true never becomes 1.
  21. if value is None or isinstance(value, bool):
  22. return None
  23. try:
  24. return int(value)
  25. except (TypeError, ValueError):
  26. return None
  27. _VIDEO_URL_PARAM = {
  28. "type": "string",
  29. "description": (
  30. "Public HTTPS MP4 URL of the source video — the `video` or "
  31. "`public_url` from a prior xAI Imagine result."
  32. ),
  33. }
  34. def _xai_video_schema(name: str, verb: str, noun: str, prompt_verb: str, extra: dict[str, Any]) -> dict[str, Any]:
  35. return {
  36. "name": name,
  37. "description": (
  38. f"{verb} an existing video with xAI Imagine. This is separate from "
  39. f"`video_generate` because video {noun} is provider-specific. "
  40. "`video_url` must be the public HTTPS MP4 URL from a prior Imagine "
  41. "result (`video` or `public_url` on files-cdn)."
  42. ),
  43. "parameters": {
  44. "type": "object",
  45. "properties": {
  46. "prompt": {
  47. "type": "string",
  48. "description": f"Instruction for how xAI should {prompt_verb} the source video.",
  49. },
  50. "video_url": _VIDEO_URL_PARAM,
  51. **extra,
  52. },
  53. "required": ["prompt", "video_url"],
  54. },
  55. }
  56. XAI_VIDEO_EDIT_SCHEMA: dict[str, Any] = _xai_video_schema("xai_video_edit", "Edit", "editing", "modify", {})
  57. XAI_VIDEO_EXTEND_SCHEMA: dict[str, Any] = _xai_video_schema(
  58. "xai_video_extend", "Extend", "extension", "continue", {
  59. "duration": {
  60. "type": "integer",
  61. "description": (
  62. "Desired extension duration in seconds. xAI clamps this "
  63. "to its supported range."
  64. ),
  65. },
  66. },
  67. )
  68. def _run_xai_video_tool(args: dict[str, Any], op: str, run, **extra: Any) -> str:
  69. prompt, video_url = _clean_string(args.get("prompt")), _clean_string(args.get("video_url"))
  70. if not prompt:
  71. return tool_error(f"prompt is required for xAI video {op}")
  72. if not (video_url and video_url.lower().startswith(("http://", "https://"))): # public URL only
  73. return tool_error(
  74. "video_url must be a public HTTPS MP4 URL (the `video`/`public_url` "
  75. "from a prior Imagine result)"
  76. )
  77. if not _configured_for_xai_video():
  78. return json.dumps({
  79. "success": False,
  80. "error": (
  81. "xAI video edit/extend tools require `video_gen.provider` to be "
  82. "configured as `xai` via `hermes tools` -> Video Generation."
  83. ),
  84. "error_type": "provider_not_configured",
  85. "provider": "xai",
  86. })
  87. return json.dumps(run(prompt=prompt, video_url=video_url, **extra))
  88. def _handle_xai_video_edit(args: dict[str, Any], **_kw: Any) -> str:
  89. return _run_xai_video_tool(args, "edit", run_xai_video_edit)
  90. def _handle_xai_video_extend(args: dict[str, Any], **_kw: Any) -> str:
  91. return _run_xai_video_tool(args, "extend", run_xai_video_extend, duration=_coerce_int(args.get("duration")))
  92. for _name, _schema, _handler in (
  93. ("xai_video_edit", XAI_VIDEO_EDIT_SCHEMA, _handle_xai_video_edit),
  94. ("xai_video_extend", XAI_VIDEO_EXTEND_SCHEMA, _handle_xai_video_extend),
  95. ):
  96. registry.register(
  97. name=_name, toolset="video_gen", schema=_schema, handler=_handler,
  98. check_fn=_check_xai_video_requirements, requires_env=[], is_async=False, emoji="video",
  99. )