This repository was archived by the owner on Mar 23, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 774
Expand file tree
/
Copy pathplugin.py
More file actions
304 lines (254 loc) · 10.2 KB
/
plugin.py
File metadata and controls
304 lines (254 loc) · 10.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
import os
from dataclasses import dataclass, field
from datetime import timedelta
from typing import Any, Dict, List, Optional, Tuple
from injector import Module, provider
from taskweaver.config.module_config import ModuleConfig
from taskweaver.misc.component_registry import ComponentDisabledException, ComponentRegistry
from taskweaver.utils import read_yaml, validate_yaml
@dataclass
class PluginMetaData:
name: str
embedding: List[float] = field(default_factory=list)
embedding_model: Optional[str] = None
path: Optional[str] = None
md5hash: Optional[str] = None
@staticmethod
def from_dict(d: Dict[str, Any]):
return PluginMetaData(
name=d["name"],
embedding=d["embedding"] if "embedding" in d else [],
embedding_model=d["embedding_model"] if "embedding_model" in d else None,
path=d["path"] if "path" in d else None,
md5hash=d["md5hash"] if "md5hash" in d else None,
)
def to_dict(self):
return {
"name": self.name,
"embedding": self.embedding,
"embedding_model": self.embedding_model,
"path": self.path,
"md5hash": self.md5hash,
}
@dataclass
class PluginParameter:
"""PluginParameter is the data structure for plugin parameters (including arguments and return values.)"""
name: str = ""
type: str = "None"
required: bool = False
description: Optional[str] = None
@staticmethod
def from_dict(d: Dict[str, Any]):
return PluginParameter(
name=d["name"],
description=d["description"],
required=d["required"] if "required" in d else False,
type=d["type"] if "type" in d else "Any",
)
def format_prompt(self, indent: int = 0) -> str:
lines: List[str] = []
def line(cnt: str):
lines.append(" " * indent + cnt)
line(f"- name: {self.name}")
line(f" type: {self.type}")
line(f" required: {self.required}")
line(f" description: {self.description}")
return "\n".join(lines)
def to_dict(self):
return {
"name": self.name,
"type": self.type,
"required": self.required,
"description": self.description,
}
@dataclass
class PluginSpec:
"""PluginSpec is the data structure for plugin specification defined in the yaml files."""
name: str = ""
description: str = ""
examples: str = ""
args: List[PluginParameter] = field(default_factory=list)
returns: List[PluginParameter] = field(default_factory=list)
@staticmethod
def from_dict(d: Dict[str, Any]):
return PluginSpec(
name=d["name"],
description=d["description"],
examples=d.get("examples", ""),
args=[PluginParameter.from_dict(p) for p in d["parameters"]],
returns=[PluginParameter.from_dict(p) for p in d["returns"]],
)
def to_dict(self):
return {
"name": self.name,
"description": self.description,
"parameters": [p.to_dict() for p in self.args],
"returns": [p.to_dict() for p in self.returns],
}
def plugin_description(self) -> str:
plugin_description = f"- {self.name}: {self.description}"
required_args = [f"{arg.name}: {arg.type}" for arg in self.args if arg.required]
if required_args:
plugin_description += f" Arguments required: {', '.join(required_args)}\n"
return plugin_description
def format_prompt(self) -> str:
def normalize_type(t: str) -> str:
if t.lower() == "string":
return "str"
if t.lower() == "integer":
return "int"
return t
def normalize_description(d: str) -> str:
d = d.strip().replace("\n", "\n# ")
return d
def normalize_value(v: PluginParameter) -> PluginParameter:
return PluginParameter(
name=v.name,
type=normalize_type(v.type),
required=v.required,
description=normalize_description(v.description or ""),
)
def format_arg_val(val: PluginParameter) -> str:
val = normalize_value(val)
type_val = f"Optional[{val.type}]" if val.type != "Any" and not val.required else "Any"
if val.description is not None:
return f"\n# {val.description}\n{val.name}: {type_val}"
return f"{val.name}: {type_val}"
def format_examples(examples: str) -> str:
return examples.strip().replace("\n", "\n# ")
examples = format_examples(self.examples)
examples = f"# Examples:\n# {examples}\n" if examples else ""
param_list = ",".join([format_arg_val(p) for p in self.args])
return_type = ""
if len(self.returns) > 1:
def format_return_val(val: PluginParameter) -> str:
val = normalize_value(val)
if val.description is not None:
return f"\n# {val.name}: {val.description}\n{val.type}"
return val.type
return_type = f"Tuple[{','.join([format_return_val(r) for r in self.returns])}]"
elif len(self.returns) == 1:
rv = normalize_value(self.returns[0])
if rv.description is not None:
return_type = f"\\\n# {rv.name}: {rv.description}\n{rv.type}"
return_type = rv.type
else:
return_type = "None"
return f"# {self.description}\n{examples}" f"def {self.name}({param_list}) -> {return_type}:...\n"
@dataclass
class PluginEntry:
name: str
plugin_only: bool
impl: str
spec: PluginSpec
config: Dict[str, Any]
required: bool
enabled: bool = True
meta_data: Optional[PluginMetaData] = None
@staticmethod
def from_yaml_file(path: str) -> Optional["PluginEntry"]:
content = read_yaml(path)
yaml_file_name = os.path.basename(path)
meta_file_path = os.path.join(os.path.dirname(path), ".meta", f"meta_{yaml_file_name}")
if os.path.exists(meta_file_path):
meta_data = PluginMetaData.from_dict(read_yaml(meta_file_path))
meta_data.path = meta_file_path
else:
meta_data = PluginMetaData(name=os.path.splitext(yaml_file_name)[0], path=meta_file_path)
return PluginEntry.from_yaml_content(content, meta_data)
@staticmethod
def from_yaml_content(content: Dict, meta_data: Optional[PluginMetaData] = None) -> Optional["PluginEntry"]:
do_validate = False
valid_state = False
if do_validate:
valid_state = validate_yaml(content, schema="plugin_schema")
if not do_validate or valid_state:
spec: PluginSpec = PluginSpec.from_dict(content)
return PluginEntry(
name=spec.name,
impl=content.get("code", spec.name),
spec=spec,
config=content.get("configurations", {}),
required=content.get("required", False),
enabled=content.get("enabled", True),
plugin_only=content.get("plugin_only", False),
meta_data=meta_data,
)
return None
def format_prompt(self) -> str:
return self.spec.format_prompt()
def to_dict(self):
return {
"name": self.name,
"impl": self.impl,
"spec": self.spec,
"config": self.config,
"required": self.required,
"enabled": self.enabled,
"plugin_only": self.plugin_only,
}
def format_function_calling(self) -> Dict[str, Any]:
assert self.plugin_only is True, "Only `plugin_only` plugins can be called in this way."
def map_type(t: str) -> str:
if t.lower() == "string" or t.lower() == "str" or t.lower() == "text":
return "string"
if t.lower() == "integer" or t.lower() == "int":
return "integer"
if t.lower() == "float" or t.lower() == "double" or t.lower() == "number":
return "number"
if t.lower() == "boolean" or t.lower() == "bool":
return "boolean"
if t.lower() == "null" or t.lower() == "none":
return "null"
raise Exception(f"unknown type {t}")
function: Dict[str, Any] = {"type": "function", "function": {}}
required_params: List[str] = []
function["function"]["name"] = self.name
function["function"]["description"] = self.spec.description
function["function"]["parameters"] = {"type": "object", "properties": {}}
for arg in self.spec.args:
function["function"]["parameters"]["properties"][arg.name] = {
"type": map_type(arg.type),
"description": arg.description,
}
if arg.required:
required_params.append(arg.name)
function["function"]["parameters"]["required"] = required_params
return function
class PluginRegistry(ComponentRegistry[PluginEntry]):
def __init__(
self,
file_glob: str,
ttl: Optional[timedelta] = None,
) -> None:
super().__init__(file_glob, ttl)
def _load_component(self, path: str) -> Tuple[str, PluginEntry]:
entry: Optional[PluginEntry] = PluginEntry.from_yaml_file(path)
if entry is None:
raise Exception(f"failed to loading plugin from {path}")
if not entry.enabled:
raise ComponentDisabledException(f"plugin {entry.name} is disabled")
return entry.name, entry
class PluginModuleConfig(ModuleConfig):
def _configure(self) -> None:
self._set_name("plugin")
app_dir = self.src.app_base_path
self.base_path = self._get_path(
"base_path",
os.path.join(
app_dir,
"plugins",
),
)
class PluginModule(Module):
@provider
def provide_plugin_registry(
self,
config: PluginModuleConfig,
) -> PluginRegistry:
import os
file_glob = os.path.join(config.base_path, '**',"*.yaml")
return PluginRegistry(
file_glob=file_glob,
ttl=timedelta(minutes=10),
)