polos-sdk 0.1.0__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.
Files changed (55) hide show
  1. polos/__init__.py +105 -0
  2. polos/agents/__init__.py +7 -0
  3. polos/agents/agent.py +746 -0
  4. polos/agents/conversation_history.py +121 -0
  5. polos/agents/stop_conditions.py +280 -0
  6. polos/agents/stream.py +635 -0
  7. polos/core/__init__.py +0 -0
  8. polos/core/context.py +143 -0
  9. polos/core/state.py +26 -0
  10. polos/core/step.py +1380 -0
  11. polos/core/workflow.py +1192 -0
  12. polos/features/__init__.py +0 -0
  13. polos/features/events.py +456 -0
  14. polos/features/schedules.py +110 -0
  15. polos/features/tracing.py +605 -0
  16. polos/features/wait.py +82 -0
  17. polos/llm/__init__.py +9 -0
  18. polos/llm/generate.py +152 -0
  19. polos/llm/providers/__init__.py +5 -0
  20. polos/llm/providers/anthropic.py +615 -0
  21. polos/llm/providers/azure.py +42 -0
  22. polos/llm/providers/base.py +196 -0
  23. polos/llm/providers/fireworks.py +41 -0
  24. polos/llm/providers/gemini.py +40 -0
  25. polos/llm/providers/groq.py +40 -0
  26. polos/llm/providers/openai.py +1021 -0
  27. polos/llm/providers/together.py +40 -0
  28. polos/llm/stream.py +183 -0
  29. polos/middleware/__init__.py +0 -0
  30. polos/middleware/guardrail.py +148 -0
  31. polos/middleware/guardrail_executor.py +253 -0
  32. polos/middleware/hook.py +164 -0
  33. polos/middleware/hook_executor.py +104 -0
  34. polos/runtime/__init__.py +0 -0
  35. polos/runtime/batch.py +87 -0
  36. polos/runtime/client.py +841 -0
  37. polos/runtime/queue.py +42 -0
  38. polos/runtime/worker.py +1365 -0
  39. polos/runtime/worker_server.py +249 -0
  40. polos/tools/__init__.py +0 -0
  41. polos/tools/tool.py +587 -0
  42. polos/types/__init__.py +23 -0
  43. polos/types/types.py +116 -0
  44. polos/utils/__init__.py +27 -0
  45. polos/utils/agent.py +27 -0
  46. polos/utils/client_context.py +41 -0
  47. polos/utils/config.py +12 -0
  48. polos/utils/output_schema.py +311 -0
  49. polos/utils/retry.py +47 -0
  50. polos/utils/serializer.py +167 -0
  51. polos/utils/tracing.py +27 -0
  52. polos/utils/worker_singleton.py +40 -0
  53. polos_sdk-0.1.0.dist-info/METADATA +650 -0
  54. polos_sdk-0.1.0.dist-info/RECORD +55 -0
  55. polos_sdk-0.1.0.dist-info/WHEEL +4 -0
polos/core/context.py ADDED
@@ -0,0 +1,143 @@
1
+ """Context classes for workflow and agent execution."""
2
+
3
+ from datetime import datetime
4
+ from typing import Any
5
+
6
+ from pydantic import BaseModel
7
+
8
+
9
+ class WorkflowContext:
10
+ """Context available to all workflow functions.
11
+
12
+ Provides execution-related information that workflows can use for
13
+ checkpointing, triggering sub-workflows, and other operations.
14
+
15
+ Includes state managers for workflow-scoped, session-scoped, and user-scoped state.
16
+ Also provides a Step helper for durable execution.
17
+ """
18
+
19
+ def __init__(
20
+ self,
21
+ workflow_id: str,
22
+ execution_id: str,
23
+ deployment_id: str,
24
+ session_id: str,
25
+ user_id: str | None = None,
26
+ parent_execution_id: str | None = None,
27
+ root_execution_id: str | None = None,
28
+ retry_count: int = 0,
29
+ created_at: datetime | None = None,
30
+ workflow_type: str | None = "workflow",
31
+ otel_traceparent: str | None = None,
32
+ otel_span_id: str | None = None,
33
+ state_schema: type[BaseModel] | None = None,
34
+ initial_state: dict[str, Any] | None = None,
35
+ ):
36
+ self.workflow_id = workflow_id
37
+ self.execution_id = execution_id
38
+ self.deployment_id = deployment_id
39
+ self.parent_execution_id = parent_execution_id
40
+ self.root_execution_id = root_execution_id or execution_id
41
+ self.retry_count = retry_count
42
+ self.created_at = created_at
43
+ self.session_id = session_id
44
+ self.user_id = user_id
45
+ self.workflow_type = workflow_type
46
+ self.otel_traceparent = otel_traceparent
47
+ self.otel_span_id = otel_span_id
48
+
49
+ # Initialize typed workflow state if state_schema is provided
50
+ if state_schema:
51
+ if initial_state:
52
+ self.state = state_schema.model_validate(initial_state)
53
+ else:
54
+ self.state = state_schema() # Use defaults
55
+ else:
56
+ self.state = None
57
+
58
+ # Initialize step helper for durable execution
59
+ from .step import Step
60
+
61
+ self.step = Step(self)
62
+
63
+ def to_dict(self) -> dict[str, Any]:
64
+ """Convert context to dictionary for execution context cache."""
65
+ return {
66
+ "workflow_id": self.workflow_id,
67
+ "execution_id": self.execution_id,
68
+ "deployment_id": self.deployment_id,
69
+ "parent_execution_id": self.parent_execution_id,
70
+ "root_execution_id": self.root_execution_id,
71
+ "retry_count": self.retry_count,
72
+ "created_at": self.created_at.isoformat() if self.created_at else None,
73
+ "session_id": self.session_id,
74
+ "user_id": self.user_id,
75
+ }
76
+
77
+
78
+ class AgentContext(WorkflowContext):
79
+ """Context for agents - extends WorkflowContext with agent information."""
80
+
81
+ def __init__(
82
+ self,
83
+ agent_id: str,
84
+ execution_id: str,
85
+ deployment_id: str,
86
+ parent_execution_id: str | None = None,
87
+ root_execution_id: str | None = None,
88
+ retry_count: int = 0,
89
+ model: str = "gpt-4",
90
+ provider: str = "openai",
91
+ system_prompt: str | None = None,
92
+ tools: list[Any] | None = None,
93
+ temperature: float | None = None,
94
+ max_tokens: int | None = None,
95
+ session_id: str | None = None,
96
+ conversation_id: str | None = None,
97
+ user_id: str | None = None,
98
+ created_at: datetime | None = None,
99
+ otel_traceparent: str | None = None,
100
+ otel_span_id: str | None = None,
101
+ state_schema: type[BaseModel] | None = None,
102
+ initial_state: dict[str, Any] | None = None,
103
+ ):
104
+ # Call parent with required parameters
105
+ super().__init__(
106
+ workflow_id=agent_id,
107
+ execution_id=execution_id,
108
+ deployment_id=deployment_id,
109
+ session_id=session_id,
110
+ user_id=user_id,
111
+ parent_execution_id=parent_execution_id,
112
+ root_execution_id=root_execution_id,
113
+ retry_count=retry_count,
114
+ created_at=created_at,
115
+ workflow_type="agent",
116
+ otel_traceparent=otel_traceparent,
117
+ otel_span_id=otel_span_id,
118
+ state_schema=state_schema,
119
+ initial_state=initial_state,
120
+ )
121
+ self.agent_id = agent_id
122
+ self.model = model
123
+ self.provider = provider
124
+ self.system_prompt = system_prompt
125
+ self.tools = tools or []
126
+ self.temperature = temperature
127
+ self.max_tokens = max_tokens
128
+ self.conversation_id = conversation_id
129
+
130
+ def to_dict(self) -> dict[str, Any]:
131
+ """Convert context to dictionary, including agent-specific fields."""
132
+ base_dict = super().to_dict()
133
+ base_dict.update(
134
+ {
135
+ "model": self.model,
136
+ "provider": self.provider,
137
+ "system_prompt": self.system_prompt,
138
+ "tools": self.tools,
139
+ "temperature": self.temperature,
140
+ "max_tokens": self.max_tokens,
141
+ }
142
+ )
143
+ return base_dict
polos/core/state.py ADDED
@@ -0,0 +1,26 @@
1
+ """State management for sessions and users."""
2
+
3
+ from pydantic import BaseModel, ConfigDict
4
+
5
+
6
+ class WorkflowState(BaseModel):
7
+ """Base class for workflow state.
8
+
9
+ Workflow state is a Pydantic model that persists across workflow execution.
10
+ State is saved only when the workflow completes or fails.
11
+
12
+ Example:
13
+ from polos import WorkflowState, workflow, WorkflowContext
14
+
15
+ class MyState(WorkflowState):
16
+ counter: int = 0
17
+ items: list[str] = []
18
+
19
+ @workflow(state_schema=MyState)
20
+ async def my_workflow(ctx: WorkflowContext, payload: dict):
21
+ ctx.state.counter += 1
22
+ ctx.state.items.append("new")
23
+ return {"done": True}
24
+ """
25
+
26
+ model_config = ConfigDict(validate_assignment=True)