f2306a3d11
Lint and Test / lint_test (3.11) (push) Has been cancelled
Lint and Test / lint_test (3.10) (push) Has been cancelled
Lint and Test / lint_test (3.12) (push) Has been cancelled
Publish to PyPI and release / Build distribution (push) Has been cancelled
Codespell / Check for spelling errors (push) Has been cancelled
Publish to PyPI and release / Publish to PyPI (push) Has been cancelled
Publish to PyPI and release / Make release (push) Has been cancelled
48 lines
1.5 KiB
Python
48 lines
1.5 KiB
Python
import subprocess
|
|
from typing import Any, Dict
|
|
|
|
from pydantic import BaseModel, Field
|
|
|
|
|
|
class Function(BaseModel):
|
|
"""
|
|
Executes Apple Script on macOS and returns the output (result).
|
|
Can be used for actions like: draft (prepare) an email, show calendar events, create a note.
|
|
"""
|
|
|
|
apple_script: str = Field(
|
|
default=...,
|
|
example='tell application "Finder" to get the name of every disk',
|
|
description="Apple Script to execute.",
|
|
) # type: ignore
|
|
|
|
@classmethod
|
|
def execute(cls, apple_script):
|
|
script_command = ["osascript", "-e", apple_script]
|
|
try:
|
|
process = subprocess.Popen(
|
|
script_command, stdout=subprocess.PIPE, stderr=subprocess.PIPE
|
|
)
|
|
output, _ = process.communicate()
|
|
output = output.decode("utf-8").strip()
|
|
return f"Output: {output}"
|
|
except Exception as e:
|
|
return f"Error: {e}"
|
|
|
|
@classmethod
|
|
def openai_schema(cls) -> Dict[str, Any]:
|
|
"""Generate OpenAI function schema from Pydantic model."""
|
|
schema = cls.model_json_schema()
|
|
return {
|
|
"type": "function",
|
|
"function": {
|
|
"name": "execute_apple_script",
|
|
"description": cls.__doc__.strip() if cls.__doc__ else "",
|
|
"parameters": {
|
|
"type": "object",
|
|
"properties": schema.get("properties", {}),
|
|
"required": schema.get("required", []),
|
|
},
|
|
},
|
|
}
|