oocana 0.16.8__py3-none-any.whl → 0.16.10__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
oocana/__init__.py CHANGED
@@ -3,5 +3,6 @@ from .context import * # noqa: F403
3
3
  from .service import * # noqa: F403
4
4
  from .handle_data import * # noqa: F403
5
5
  from .preview import * # noqa: F403
6
+ from .extra import * # noqa: F403
6
7
  from .schema import * # noqa: F403
7
- from .mainframe import Mainframe as Mainframe # noqa: F403
8
+ from .mainframe import Mainframe as Mainframe # noqa: F403
oocana/context.py CHANGED
@@ -36,7 +36,6 @@ class Context:
36
36
  __block_info: BlockInfo
37
37
  __outputs_def: Dict[str, HandleDef]
38
38
  __store: Any
39
- __is_done: bool = False
40
39
  __keep_alive: OnlyEqualSelf = OnlyEqualSelf()
41
40
  __session_dir: str
42
41
  __tmp_dir: str
@@ -156,10 +155,6 @@ class Context:
156
155
  """
157
156
  return os.getenv("OO_HOST_ENDPOINT", None)
158
157
 
159
- @property
160
- def is_done(self) -> bool:
161
- return self.__is_done
162
-
163
158
  def __store_ref(self, handle: str):
164
159
  return StoreKey(
165
160
  executor=EXECUTOR_NAME,
@@ -170,8 +165,67 @@ class Context:
170
165
 
171
166
  def __is_basic_type(self, value: Any) -> bool:
172
167
  return isinstance(value, (int, float, str, bool))
168
+
169
+ def __wrap_output_value(self, handle: str, value: Any):
170
+ """
171
+ wrap the output value:
172
+ if the value is a var handle, store it in the store and return the reference.
173
+ if the value is a bin handle, store it in the store and return the reference.
174
+ if the handle is not defined in the block outputs schema, raise an ValueError.
175
+ otherwise, return the value.
176
+ :param handle: the handle of the output
177
+ :param value: the value of the output
178
+ :return: the wrapped value
179
+ """
180
+ # __outputs_def should never be None
181
+ if self.__outputs_def is None:
182
+ return value
183
+
184
+ output_def = self.__outputs_def.get(handle)
185
+ if output_def is None:
186
+ raise ValueError(
187
+ f"Output handle key: [{handle}] is not defined in Block outputs schema."
188
+ )
189
+
190
+ if output_def.is_var_handle() and not self.__is_basic_type(value):
191
+ ref = self.__store_ref(handle)
192
+ self.__store[ref] = value
193
+ var: VarValueDict = {
194
+ "__OOMOL_TYPE__": "oomol/var",
195
+ "value": asdict(ref)
196
+ }
197
+ return var
198
+
199
+ if output_def.is_bin_handle():
200
+ if not isinstance(value, bytes):
201
+ self.send_warning(
202
+ f"Output handle key: [{handle}] is defined as binary, but the value is not bytes."
203
+ )
204
+ return value
205
+
206
+ bin_file = f"{self.session_dir}/binary/{self.session_id}/{self.job_id}/{handle}"
207
+ os.makedirs(os.path.dirname(bin_file), exist_ok=True)
208
+ try:
209
+ with open(bin_file, "wb") as f:
210
+ f.write(value)
211
+ except IOError as e:
212
+ raise IOError(
213
+ f"Output handle key: [{handle}] is defined as binary, but an error occurred while writing the file: {e}"
214
+ )
215
+
216
+ if os.path.exists(bin_file):
217
+ bin_value: BinValueDict = {
218
+ "__OOMOL_TYPE__": "oomol/bin",
219
+ "value": bin_file,
220
+ }
221
+ return bin_value
222
+ else:
223
+ raise IOError(
224
+ f"Output handle key: [{handle}] is defined as binary, but the file is not written."
225
+ )
226
+ return value
173
227
 
174
- def output(self, key: str, value: Any, done: bool = False):
228
+ def output(self, key: str, value: Any):
175
229
  """
176
230
  output the value to the next block
177
231
 
@@ -179,81 +233,85 @@ class Context:
179
233
  value: Any, the value of the output
180
234
  """
181
235
 
182
- v = value
183
-
184
- if self.__outputs_def is not None:
185
- output_def = self.__outputs_def.get(key)
186
- if (
187
- output_def is not None and output_def.is_var_handle() and not self.__is_basic_type(value) # 基础类型即使是变量也不放进 store,直接作为 json 内容传递
188
- ):
189
- ref = self.__store_ref(key)
190
- self.__store[ref] = value
191
- d: VarValueDict = {
192
- "__OOMOL_TYPE__": "oomol/var",
193
- "value": asdict(ref)
194
- }
195
- v = d
196
- elif output_def is not None and output_def.is_bin_handle():
197
- if not isinstance(value, bytes):
198
- self.send_warning(
199
- f"Output handle key: [{key}] is defined as binary, but the value is not bytes."
200
- )
201
- return
202
-
203
- bin_file = f"{self.session_dir}/binary/{self.session_id}/{self.job_id}/{key}"
204
- os.makedirs(os.path.dirname(bin_file), exist_ok=True)
205
- try:
206
- with open(bin_file, "wb") as f:
207
- f.write(value)
208
- except IOError as e:
209
- self.send_warning(
210
- f"Output handle key: [{key}] is defined as binary, but an error occurred while writing the file: {e}"
211
- )
212
- return
213
-
214
- if os.path.exists(bin_file):
215
- bin_value: BinValueDict = {
216
- "__OOMOL_TYPE__": "oomol/bin",
217
- "value": bin_file,
218
- }
219
- v = bin_value
220
- else:
221
- self.send_warning(
222
- f"Output handle key: [{key}] is defined as binary, but the file is not written."
223
- )
224
- return
225
-
226
- # 如果传入 key 在输出定义中不存在,直接忽略,不发送数据。但是 done 仍然生效。
227
- if self.__outputs_def is not None and self.__outputs_def.get(key) is None:
236
+ try:
237
+ wrap_value = self.__wrap_output_value(key, value)
238
+ except ValueError as e:
228
239
  self.send_warning(
229
- f"Output handle key: [{key}] is not defined in Block outputs schema."
240
+ f"{e}"
241
+ )
242
+ return
243
+ except IOError as e:
244
+ self.send_warning(
245
+ f"{e}"
230
246
  )
231
- if done:
232
- self.done()
233
247
  return
234
248
 
235
249
  node_result = {
236
250
  "type": "BlockOutput",
237
251
  "handle": key,
238
- "output": v,
239
- "done": done,
252
+ "output": wrap_value,
240
253
  }
241
254
  self.__mainframe.send(self.job_info, node_result)
255
+
256
+ def outputs(self, outputs: Dict[str, Any]):
257
+ """
258
+ output the value to the next block
242
259
 
243
- if done:
244
- self.done()
260
+ map: Dict[str, Any], the key of the output, should be defined in the block schema output defs, the field name is handle
261
+ """
245
262
 
246
- def done(self, error: str | None = None):
247
- if self.__is_done:
248
- self.send_warning("done has been called multiple times, will be ignored.")
249
- return
250
- self.__is_done = True
251
- if error is None:
252
- self.__mainframe.send(self.job_info, {"type": "BlockFinished"})
263
+ values = {}
264
+ for key, value in outputs.items():
265
+ try:
266
+ wrap_value = self.__wrap_output_value(key, value)
267
+ values[key] = wrap_value
268
+ except ValueError as e:
269
+ self.send_warning(
270
+ f"{e}"
271
+ )
272
+ except IOError as e:
273
+ self.send_warning(
274
+ f"{e}"
275
+ )
276
+ self.__mainframe.send(self.job_info, {
277
+ "type": "BlockOutputs",
278
+ "outputs": values,
279
+ })
280
+
281
+
282
+
283
+ def finish(self, *, result: Dict[str, Any] | None = None, error: str | None = None):
284
+ """
285
+ finish the block, and send the result to oocana.
286
+ if error is not None, the block will be finished with error.
287
+ then if result is not None, the block will be finished with result.
288
+ lastly, if both error and result are None, the block will be finished without any result.
289
+ """
290
+
291
+ if error is not None:
292
+ self.__mainframe.send(self.job_info, {"type": "BlockFinished", "error": error})
293
+ elif result is not None:
294
+ wrap_result = {}
295
+ if isinstance(result, dict):
296
+ for key, value in result.items():
297
+ try:
298
+ wrap_result[key] = self.__wrap_output_value(key, value)
299
+ except ValueError as e:
300
+ self.send_warning(
301
+ f"Output handle key: [{key}] is not defined in Block outputs schema. {e}"
302
+ )
303
+ except IOError as e:
304
+ self.send_warning(
305
+ f"Output handle key: [{key}] is not defined in Block outputs schema. {e}"
306
+ )
307
+
308
+ self.__mainframe.send(self.job_info, {"type": "BlockFinished", "result": wrap_result})
309
+ else:
310
+ raise ValueError(
311
+ f"result should be a dict, but got {type(result)}"
312
+ )
253
313
  else:
254
- self.__mainframe.send(
255
- self.job_info, {"type": "BlockFinished", "error": error}
256
- )
314
+ self.__mainframe.send(self.job_info, {"type": "BlockFinished"})
257
315
 
258
316
  def send_message(self, payload):
259
317
  self.__mainframe.report(
oocana/extra.py ADDED
@@ -0,0 +1,13 @@
1
+ from typing import Literal, TypedDict
2
+
3
+ __all__ = ["LLMModelOptions", "LLMMessage"]
4
+
5
+ class LLMModelOptions(TypedDict):
6
+ model: str
7
+ temperature: float
8
+ top_p: float
9
+ max_tokens: int
10
+
11
+ class LLMMessage(TypedDict):
12
+ role: Literal["system", "user", "assistant"]
13
+ content: str
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: oocana
3
- Version: 0.16.8
3
+ Version: 0.16.10
4
4
  Summary: python implement of oocana to give a context for oocana block
5
5
  License: MIT
6
6
  Requires-Python: >=3.9
@@ -0,0 +1,14 @@
1
+ oocana-0.16.10.dist-info/METADATA,sha256=lndcAbXQ-_47BH_yJHGfm3LAxf1qHid7OAfNEPDOrxo,222
2
+ oocana-0.16.10.dist-info/WHEEL,sha256=tSfRZzRHthuv7vxpI4aehrdN9scLjk-dCJkPLzkHxGg,90
3
+ oocana-0.16.10.dist-info/entry_points.txt,sha256=6OYgBcLyFCUgeqLgnvMyOJxPCWzgy7se4rLPKtNonMs,34
4
+ oocana/__init__.py,sha256=kuZa-aY4bqStH_15K4KL9hjOeO9d-M9mJYKs4fm3n08,317
5
+ oocana/context.py,sha256=XdaFrj9QeFjGn2wYtxmOft7m2AOsHn6nGZ1kQIM8nAc,16780
6
+ oocana/data.py,sha256=0KpJyckz_X5JbrncBRFbOaU00CxcDx2UYHk6xuH1Poc,2890
7
+ oocana/extra.py,sha256=1f7cu0QA3jnb6PJtEDXMsQOSGWtjUhsRz82DaPRyjKY,289
8
+ oocana/handle_data.py,sha256=p0iEvdsVV3BqcelM8rvq0a_VPI52SeahaGCszfhZ0TI,1927
9
+ oocana/mainframe.py,sha256=Jixk9PjvJh7PnYwPGqOR5viwcOHWfaPhEiQFNHxHCxw,5219
10
+ oocana/preview.py,sha256=qJEoBQWno0BkuU501vd16VwE0okTC3PydFc_jpkwc98,2096
11
+ oocana/schema.py,sha256=8LwMaW4eXa3EmKxR4kzyTOpZiClMRMMsMA6f9CXodW4,4332
12
+ oocana/service.py,sha256=dPCcScQfMBlEjIodFnKU17-HlbBzrQbQK6CNRw7SmOE,2442
13
+ oocana/throtter.py,sha256=xTldm2OZ5uYPiyC6Aatug4FJAoMylPjtXftxHvSr_50,1104
14
+ oocana-0.16.10.dist-info/RECORD,,
@@ -1,13 +0,0 @@
1
- oocana-0.16.8.dist-info/METADATA,sha256=xwQQfPU_78nZH0uFPbXC1tCV4090vdogSFtT1gQ3pWg,221
2
- oocana-0.16.8.dist-info/WHEEL,sha256=tSfRZzRHthuv7vxpI4aehrdN9scLjk-dCJkPLzkHxGg,90
3
- oocana-0.16.8.dist-info/entry_points.txt,sha256=6OYgBcLyFCUgeqLgnvMyOJxPCWzgy7se4rLPKtNonMs,34
4
- oocana/__init__.py,sha256=zMrQ1asZoRE91-BShf1v2ibdThaMc07YtZt5iNbbxsg,281
5
- oocana/context.py,sha256=5wWUZr4BjojizFH-9yeadWvIpiUt2v7wUN3b7y34CFc,14677
6
- oocana/data.py,sha256=0KpJyckz_X5JbrncBRFbOaU00CxcDx2UYHk6xuH1Poc,2890
7
- oocana/handle_data.py,sha256=p0iEvdsVV3BqcelM8rvq0a_VPI52SeahaGCszfhZ0TI,1927
8
- oocana/mainframe.py,sha256=Jixk9PjvJh7PnYwPGqOR5viwcOHWfaPhEiQFNHxHCxw,5219
9
- oocana/preview.py,sha256=qJEoBQWno0BkuU501vd16VwE0okTC3PydFc_jpkwc98,2096
10
- oocana/schema.py,sha256=8LwMaW4eXa3EmKxR4kzyTOpZiClMRMMsMA6f9CXodW4,4332
11
- oocana/service.py,sha256=dPCcScQfMBlEjIodFnKU17-HlbBzrQbQK6CNRw7SmOE,2442
12
- oocana/throtter.py,sha256=xTldm2OZ5uYPiyC6Aatug4FJAoMylPjtXftxHvSr_50,1104
13
- oocana-0.16.8.dist-info/RECORD,,