swiftpy-framework 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.
- swiftpy/.gitignore +0 -0
- swiftpy/__init__.py +572 -0
- swiftpy/admin/__init__.py +50 -0
- swiftpy/admin/auth.py +522 -0
- swiftpy/admin/registry.py +567 -0
- swiftpy/admin/views.py +1583 -0
- swiftpy/ai/__init__.py +199 -0
- swiftpy/ai/_deterministic.py +40 -0
- swiftpy/ai/_fake.py +604 -0
- swiftpy/ai/agents/__init__.py +30 -0
- swiftpy/ai/agents/base_agent.py +506 -0
- swiftpy/ai/agents/memory.py +864 -0
- swiftpy/ai/client.py +1011 -0
- swiftpy/ai/conversation.py +171 -0
- swiftpy/ai/embeddings.py +321 -0
- swiftpy/ai/exceptions.py +75 -0
- swiftpy/ai/history_strategies.py +157 -0
- swiftpy/ai/middleware.py +918 -0
- swiftpy/ai/observability/__init__.py +28 -0
- swiftpy/ai/observability/drivers.py +763 -0
- swiftpy/ai/observability/observer.py +364 -0
- swiftpy/ai/pool.py +102 -0
- swiftpy/ai/pricing.py +101 -0
- swiftpy/ai/prompts/__init__.py +6 -0
- swiftpy/ai/prompts/loader.py +343 -0
- swiftpy/ai/providers/__init__.py +22 -0
- swiftpy/ai/providers/anthropic_provider.py +353 -0
- swiftpy/ai/providers/base.py +273 -0
- swiftpy/ai/providers/openai_provider.py +275 -0
- swiftpy/ai/rag/__init__.py +53 -0
- swiftpy/ai/rag/context_formatter.py +263 -0
- swiftpy/ai/rag/pipeline_builder.py +544 -0
- swiftpy/ai/rag/reranker.py +484 -0
- swiftpy/ai/rag/result.py +78 -0
- swiftpy/ai/rag/retriever.py +315 -0
- swiftpy/ai/redis_store.py +227 -0
- swiftpy/ai/streaming.py +115 -0
- swiftpy/ai/tools/__init__.py +22 -0
- swiftpy/ai/tools/base_tool.py +489 -0
- swiftpy/ai/tools/first_party.py +384 -0
- swiftpy/ai/tools/tool_loop.py +417 -0
- swiftpy/ai/vectorstore/__init__.py +46 -0
- swiftpy/ai/vectorstore/base.py +542 -0
- swiftpy/ai/vectorstore/chroma_adapter.py +435 -0
- swiftpy/ai/vectorstore/in_memory_adapter.py +321 -0
- swiftpy/ai/vectorstore/lancedb_adapter.py +370 -0
- swiftpy/ai/vectorstore/pgvector_adapter.py +496 -0
- swiftpy/ai/vectorstore/pinecone_adapter.py +445 -0
- swiftpy/ai/vectorstore/qdrant_adapter.py +418 -0
- swiftpy/auth/__init__.py +50 -0
- swiftpy/auth/api_key_guard.py +544 -0
- swiftpy/auth/auth_manager.py +259 -0
- swiftpy/auth/exceptions.py +71 -0
- swiftpy/auth/jwt_guard.py +230 -0
- swiftpy/auth/jwt_issuer.py +658 -0
- swiftpy/auth/token_blacklist.py +300 -0
- swiftpy/cache/__init__.py +14 -0
- swiftpy/cache/cache.py +251 -0
- swiftpy/cache/driver.py +216 -0
- swiftpy/cache/drivers/memory.py +199 -0
- swiftpy/cache/drivers/redis.py +421 -0
- swiftpy/console/__init__.py +0 -0
- swiftpy/console/cli.py +68 -0
- swiftpy/console/commands/__init__.py +0 -0
- swiftpy/console/commands/cache.py +164 -0
- swiftpy/console/commands/make.py +935 -0
- swiftpy/console/commands/migrate.py +280 -0
- swiftpy/console/commands/new.py +1806 -0
- swiftpy/console/commands/queue.py +306 -0
- swiftpy/console/commands/seed.py +252 -0
- swiftpy/console/commands/start.py +171 -0
- swiftpy/console/commands/storage.py +104 -0
- swiftpy/console/loader.py +21 -0
- swiftpy/console/stubs/__init__.py +26 -0
- swiftpy/console/stubs/controller.stub +14 -0
- swiftpy/console/stubs/event.stub +14 -0
- swiftpy/console/stubs/job.stub +22 -0
- swiftpy/console/stubs/listener.stub +14 -0
- swiftpy/console/stubs/mail.stub +20 -0
- swiftpy/console/stubs/middleware.stub +16 -0
- swiftpy/console/stubs/migration.alter.stub +17 -0
- swiftpy/console/stubs/migration.api_keys.stub +22 -0
- swiftpy/console/stubs/migration.blank.stub +11 -0
- swiftpy/console/stubs/migration.create.stub +15 -0
- swiftpy/console/stubs/migration.stub +11 -0
- swiftpy/console/stubs/migration.update.stub +17 -0
- swiftpy/console/stubs/model.stub +17 -0
- swiftpy/console/stubs/provider.stub +18 -0
- swiftpy/console/stubs/repository.stub +10 -0
- swiftpy/console/stubs/request.stub +16 -0
- swiftpy/console/stubs/resource.stub +21 -0
- swiftpy/console/stubs/schema.stub +12 -0
- swiftpy/console/stubs/seeder.stub +14 -0
- swiftpy/console/stubs/service.stub +10 -0
- swiftpy/core/__init__.py +0 -0
- swiftpy/core/application.py +1306 -0
- swiftpy/core/bootstrap.py +30 -0
- swiftpy/core/config.py +432 -0
- swiftpy/core/container.py +596 -0
- swiftpy/core/container_exceptions.py +18 -0
- swiftpy/core/context.py +148 -0
- swiftpy/core/exceptions.py +352 -0
- swiftpy/core/providers/base.py +35 -0
- swiftpy/database/__init__.py +41 -0
- swiftpy/database/exceptions.py +42 -0
- swiftpy/database/identifiers.py +105 -0
- swiftpy/database/migrations/__init__.py +36 -0
- swiftpy/database/migrations/blueprint.py +431 -0
- swiftpy/database/migrations/lock.py +104 -0
- swiftpy/database/migrations/migration.py +22 -0
- swiftpy/database/migrations/migrator.py +507 -0
- swiftpy/database/migrations/schema.py +435 -0
- swiftpy/database/orm/__init__.py +33 -0
- swiftpy/database/orm/fields.py +92 -0
- swiftpy/database/orm/model.py +2034 -0
- swiftpy/database/orm/registry.py +33 -0
- swiftpy/database/orm/relationships.py +836 -0
- swiftpy/database/pagination/__init__.py +16 -0
- swiftpy/database/pagination/contracts.py +60 -0
- swiftpy/database/pagination/paginator.py +112 -0
- swiftpy/database/pagination/simple_paginator.py +97 -0
- swiftpy/database/pool.py +218 -0
- swiftpy/database/query_builder.py +1803 -0
- swiftpy/database/seeder.py +39 -0
- swiftpy/database/transaction.py +276 -0
- swiftpy/events/__init__.py +10 -0
- swiftpy/events/dispatcher.py +308 -0
- swiftpy/events/redis_bus.py +552 -0
- swiftpy/filesystem/__init__.py +227 -0
- swiftpy/filesystem/disk.py +378 -0
- swiftpy/filesystem/drivers/__init__.py +11 -0
- swiftpy/filesystem/drivers/base.py +156 -0
- swiftpy/filesystem/drivers/local.py +559 -0
- swiftpy/filesystem/drivers/s3.py +655 -0
- swiftpy/filesystem/events.py +37 -0
- swiftpy/filesystem/exceptions.py +127 -0
- swiftpy/filesystem/manager.py +138 -0
- swiftpy/filesystem/metadata.py +22 -0
- swiftpy/filesystem/paths.py +99 -0
- swiftpy/filesystem/service_provider.py +17 -0
- swiftpy/filesystem/uploads/__init__.py +13 -0
- swiftpy/filesystem/uploads/handlers.py +362 -0
- swiftpy/filesystem/uploads/uploaded_file.py +222 -0
- swiftpy/filesystem/uploads/validators.py +186 -0
- swiftpy/filesystem/visibility.py +22 -0
- swiftpy/helpers/__init__.py +42 -0
- swiftpy/helpers/helper.py +180 -0
- swiftpy/http/exceptions.py +41 -0
- swiftpy/http/middleware.py +307 -0
- swiftpy/http/request.py +375 -0
- swiftpy/http/response.py +442 -0
- swiftpy/http/router.py +1050 -0
- swiftpy/mail/__init__.py +58 -0
- swiftpy/mail/attachments.py +255 -0
- swiftpy/mail/config.py +71 -0
- swiftpy/mail/envelope.py +183 -0
- swiftpy/mail/exceptions.py +90 -0
- swiftpy/mail/facade.py +312 -0
- swiftpy/mail/mailable.py +53 -0
- swiftpy/mail/manager.py +240 -0
- swiftpy/mail/message.py +186 -0
- swiftpy/mail/models.py +19 -0
- swiftpy/mail/queue/__init__.py +5 -0
- swiftpy/mail/queue/job.py +279 -0
- swiftpy/mail/service_provider.py +19 -0
- swiftpy/mail/templates/__init__.py +5 -0
- swiftpy/mail/templates/renderer.py +173 -0
- swiftpy/mail/transports/__init__.py +21 -0
- swiftpy/mail/transports/api/__init__.py +13 -0
- swiftpy/mail/transports/api/base.py +194 -0
- swiftpy/mail/transports/api/mailgun.py +158 -0
- swiftpy/mail/transports/api/resend.py +156 -0
- swiftpy/mail/transports/api/sendgrid.py +172 -0
- swiftpy/mail/transports/base.py +24 -0
- swiftpy/mail/transports/console.py +91 -0
- swiftpy/mail/transports/sendmail.py +128 -0
- swiftpy/mail/transports/smtp.py +466 -0
- swiftpy/queue/__init__.py +13 -0
- swiftpy/queue/dispatcher.py +813 -0
- swiftpy/queue/job.py +541 -0
- swiftpy/queue/worker.py +556 -0
- swiftpy/support/__init__.py +12 -0
- swiftpy/support/keys.py +108 -0
- swiftpy/support/redis_config.py +223 -0
- swiftpy/support/strings.py +114 -0
- swiftpy/testing/__init__.py +20 -0
- swiftpy/testing/fakes/__init__.py +11 -0
- swiftpy/testing/fakes/ai_fake.py +21 -0
- swiftpy/testing/fakes/vectorstore_fake.py +194 -0
- swiftpy/testing/fixtures.py +124 -0
- swiftpy/testing/http_client.py +730 -0
- swiftpy/testing/test_case.py +126 -0
- swiftpy/validation/__init__.py +97 -0
- swiftpy/validation/exceptions.py +26 -0
- swiftpy/validation/form_request.py +302 -0
- swiftpy/validation/protocols.py +33 -0
- swiftpy/validation/rules/__init__.py +89 -0
- swiftpy/validation/rules/builtin.py +391 -0
- swiftpy/validation/rules/exists.py +86 -0
- swiftpy/validation/rules/registry.py +290 -0
- swiftpy/validation/rules/required.py +55 -0
- swiftpy/validation/rules/unique.py +138 -0
- swiftpy/validation/validator.py +612 -0
- swiftpy/websockets/__init__.py +19 -0
- swiftpy/websockets/connection.py +317 -0
- swiftpy/websockets/manager.py +271 -0
- swiftpy_framework-0.1.0.dist-info/METADATA +603 -0
- swiftpy_framework-0.1.0.dist-info/RECORD +211 -0
- swiftpy_framework-0.1.0.dist-info/WHEEL +4 -0
- swiftpy_framework-0.1.0.dist-info/entry_points.txt +2 -0
- swiftpy_framework-0.1.0.dist-info/licenses/LICENSE +21 -0
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import json
|
|
4
|
+
from abc import ABC, abstractmethod
|
|
5
|
+
from collections.abc import Callable
|
|
6
|
+
|
|
7
|
+
from swiftpy.ai.conversation import Conversation
|
|
8
|
+
from swiftpy.ai.providers.base import Message
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def _trim_orphaned_leading_turns(
|
|
12
|
+
dialogue: list[Message],
|
|
13
|
+
*,
|
|
14
|
+
min_remaining: int = 0,
|
|
15
|
+
) -> list[Message]:
|
|
16
|
+
"""Remove leading orphaned assistant/tool messages so dialogue starts cleanly with user."""
|
|
17
|
+
while len(dialogue) > min_remaining and dialogue[0].role in ("assistant", "tool"):
|
|
18
|
+
dialogue.pop(0)
|
|
19
|
+
return dialogue
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def default_token_estimator(text: str) -> int:
|
|
23
|
+
"""
|
|
24
|
+
Heuristic token estimator (~4 characters per token).
|
|
25
|
+
Provides consistent, fast token estimation without external dependencies.
|
|
26
|
+
"""
|
|
27
|
+
if not text:
|
|
28
|
+
return 0
|
|
29
|
+
return max(1, len(text) // 4 + 1)
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
class HistoryStrategy(ABC):
|
|
33
|
+
"""Abstract base class for conversation history pruning strategies."""
|
|
34
|
+
|
|
35
|
+
@abstractmethod
|
|
36
|
+
def apply(self, conversation: Conversation) -> Conversation:
|
|
37
|
+
"""Apply the pruning strategy to a conversation, returning a new trimmed Conversation."""
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
class SlidingWindowStrategy(HistoryStrategy):
|
|
41
|
+
"""
|
|
42
|
+
Sliding window history strategy retaining the last N dialogue messages.
|
|
43
|
+
|
|
44
|
+
INVARIANTS:
|
|
45
|
+
1. All system prompt messages are strictly preserved at the start.
|
|
46
|
+
2. Pruning maintains turn integrity: if pruning leaves an orphaned
|
|
47
|
+
assistant response at the beginning of dialogue, it is removed so
|
|
48
|
+
the retained dialogue cleanly begins with a user turn.
|
|
49
|
+
3. The input conversation is never mutated.
|
|
50
|
+
"""
|
|
51
|
+
|
|
52
|
+
def __init__(self, max_messages: int = 10) -> None:
|
|
53
|
+
if max_messages <= 0:
|
|
54
|
+
raise ValueError(f"max_messages must be a positive integer, got {max_messages}")
|
|
55
|
+
self.max_messages = max_messages
|
|
56
|
+
|
|
57
|
+
def apply(self, conversation: Conversation) -> Conversation:
|
|
58
|
+
all_msgs = conversation.messages
|
|
59
|
+
if len(all_msgs) <= self.max_messages:
|
|
60
|
+
return conversation.clone()
|
|
61
|
+
|
|
62
|
+
system_msgs = conversation.system_messages
|
|
63
|
+
dialogue_msgs = conversation.dialogue_messages
|
|
64
|
+
|
|
65
|
+
allowed_dialogue = max(0, self.max_messages - len(system_msgs))
|
|
66
|
+
trimmed_dialogue = dialogue_msgs[-allowed_dialogue:] if allowed_dialogue > 0 else []
|
|
67
|
+
|
|
68
|
+
# Turn integrity: do not start dialogue with orphaned assistant or tool messages
|
|
69
|
+
trimmed_dialogue = _trim_orphaned_leading_turns(trimmed_dialogue, min_remaining=0)
|
|
70
|
+
|
|
71
|
+
retained: list[Message] = []
|
|
72
|
+
retained.extend(system_msgs)
|
|
73
|
+
retained.extend(trimmed_dialogue)
|
|
74
|
+
|
|
75
|
+
return Conversation(messages=retained)
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
class TokenBudgetStrategy(HistoryStrategy):
|
|
79
|
+
"""
|
|
80
|
+
Token budget history strategy strictly enforcing a maximum token capacity.
|
|
81
|
+
|
|
82
|
+
INVARIANTS:
|
|
83
|
+
1. All system prompt messages are strictly preserved.
|
|
84
|
+
2. If system prompt(s) alone exceed max_tokens, raises ValueError.
|
|
85
|
+
3. If the newest message alone exceeds the available budget, raises ValueError.
|
|
86
|
+
4. Dialogue turns are pruned from oldest to newest.
|
|
87
|
+
5. Returned conversation total tokens NEVER exceed max_tokens.
|
|
88
|
+
6. The input conversation is never mutated.
|
|
89
|
+
"""
|
|
90
|
+
|
|
91
|
+
def __init__(
|
|
92
|
+
self,
|
|
93
|
+
max_tokens: int = 2000,
|
|
94
|
+
estimator: Callable[[str], int] | None = None,
|
|
95
|
+
) -> None:
|
|
96
|
+
if max_tokens <= 0:
|
|
97
|
+
raise ValueError(f"max_tokens must be a positive integer, got {max_tokens}")
|
|
98
|
+
self.max_tokens = max_tokens
|
|
99
|
+
self.estimator = estimator or default_token_estimator
|
|
100
|
+
|
|
101
|
+
def estimate_message(self, message: Message) -> int:
|
|
102
|
+
"""Estimate token consumption of a single message including framing overhead."""
|
|
103
|
+
base = self.estimator(message.content) + 4
|
|
104
|
+
if message.tool_calls:
|
|
105
|
+
base += self.estimator(json.dumps([tc.to_dict() for tc in message.tool_calls]))
|
|
106
|
+
return base
|
|
107
|
+
|
|
108
|
+
def estimate_conversation(self, conversation: Conversation) -> int:
|
|
109
|
+
"""Calculate total estimated token consumption of a conversation."""
|
|
110
|
+
return sum(self.estimate_message(m) for m in conversation.messages)
|
|
111
|
+
|
|
112
|
+
def apply(self, conversation: Conversation) -> Conversation:
|
|
113
|
+
system_msgs = conversation.system_messages
|
|
114
|
+
dialogue_msgs = conversation.dialogue_messages
|
|
115
|
+
|
|
116
|
+
system_tokens = sum(self.estimate_message(m) for m in system_msgs)
|
|
117
|
+
if system_tokens > self.max_tokens:
|
|
118
|
+
raise ValueError(
|
|
119
|
+
f"System prompt consumes {system_tokens} tokens, exceeding total budget of {self.max_tokens} tokens."
|
|
120
|
+
)
|
|
121
|
+
|
|
122
|
+
remaining_budget = self.max_tokens - system_tokens
|
|
123
|
+
|
|
124
|
+
if not dialogue_msgs:
|
|
125
|
+
return Conversation(messages=system_msgs)
|
|
126
|
+
|
|
127
|
+
newest_msg = dialogue_msgs[-1]
|
|
128
|
+
newest_tokens = self.estimate_message(newest_msg)
|
|
129
|
+
if newest_tokens > remaining_budget:
|
|
130
|
+
raise ValueError(
|
|
131
|
+
f"Newest message ({newest_tokens} tokens) exceeds remaining budget of {remaining_budget} tokens."
|
|
132
|
+
)
|
|
133
|
+
|
|
134
|
+
# Select dialogue messages from most recent backward
|
|
135
|
+
retained_dialogue_reversed: list[Message] = []
|
|
136
|
+
accumulated_tokens = 0
|
|
137
|
+
|
|
138
|
+
for msg in reversed(dialogue_msgs):
|
|
139
|
+
msg_tokens = self.estimate_message(msg)
|
|
140
|
+
if accumulated_tokens + msg_tokens <= remaining_budget:
|
|
141
|
+
retained_dialogue_reversed.append(msg)
|
|
142
|
+
accumulated_tokens += msg_tokens
|
|
143
|
+
else:
|
|
144
|
+
break
|
|
145
|
+
|
|
146
|
+
retained_dialogue = list(reversed(retained_dialogue_reversed))
|
|
147
|
+
|
|
148
|
+
# Turn integrity: ensure dialogue begins cleanly with user turn (drop orphaned assistant/tool)
|
|
149
|
+
retained_dialogue = _trim_orphaned_leading_turns(retained_dialogue, min_remaining=1)
|
|
150
|
+
|
|
151
|
+
retained: list[Message] = []
|
|
152
|
+
retained.extend(system_msgs)
|
|
153
|
+
retained.extend(retained_dialogue)
|
|
154
|
+
|
|
155
|
+
result = Conversation(messages=retained)
|
|
156
|
+
assert self.estimate_conversation(result) <= self.max_tokens, "Budget violated"
|
|
157
|
+
return result
|