dex-connector-openai 0.1.0__tar.gz
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.
- dex_connector_openai-0.1.0/.gitignore +6 -0
- dex_connector_openai-0.1.0/LICENSE +21 -0
- dex_connector_openai-0.1.0/PKG-INFO +86 -0
- dex_connector_openai-0.1.0/README.md +74 -0
- dex_connector_openai-0.1.0/examples/summarize_text/__init__.py +3 -0
- dex_connector_openai-0.1.0/examples/summarize_text/flow.py +156 -0
- dex_connector_openai-0.1.0/examples/summarize_text/worker.py +76 -0
- dex_connector_openai-0.1.0/integration/test_summarize_text.py +129 -0
- dex_connector_openai-0.1.0/pyproject.toml +37 -0
- dex_connector_openai-0.1.0/src/dex_connectors/openai/__init__.py +59 -0
- dex_connector_openai-0.1.0/src/dex_connectors/openai/_generated/__init__.py +2 -0
- dex_connector_openai-0.1.0/src/dex_connectors/openai/_generated/config.py +26 -0
- dex_connector_openai-0.1.0/src/dex_connectors/openai/_generated/connection.py +142 -0
- dex_connector_openai-0.1.0/src/dex_connectors/openai/_generated/definitions.py +60 -0
- dex_connector_openai-0.1.0/src/dex_connectors/openai/_generated/models.py +83 -0
- dex_connector_openai-0.1.0/src/dex_connectors/openai/client.py +851 -0
- dex_connector_openai-0.1.0/src/dex_connectors/openai/connection.py +46 -0
- dex_connector_openai-0.1.0/src/dex_connectors/openai/dex-connector.json +87 -0
- dex_connector_openai-0.1.0/src/dex_connectors/openai/mock_checks.py +58 -0
- dex_connector_openai-0.1.0/src/dex_connectors/openai/models.py +41 -0
- dex_connector_openai-0.1.0/src/dex_connectors/openai/openaimock/__init__.py +157 -0
- dex_connector_openai-0.1.0/src/dex_connectors/openai/py.typed +0 -0
- dex_connector_openai-0.1.0/src/dex_connectors/openai/steps.py +137 -0
- dex_connector_openai-0.1.0/src/dex_connectors/openai/stream.py +163 -0
- dex_connector_openai-0.1.0/src/dex_connectors/openai/wire.py +92 -0
- dex_connector_openai-0.1.0/tests/test_conformance.py +81 -0
- dex_connector_openai-0.1.0/tests/test_create_response.py +502 -0
- dex_connector_openai-0.1.0/tests/test_manifest_contract.py +96 -0
- dex_connector_openai-0.1.0/tests/test_mock_checks.py +45 -0
- dex_connector_openai-0.1.0/tests/test_retrieve_response.py +122 -0
- dex_connector_openai-0.1.0/uv.lock +932 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Super Durable
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: dex-connector-openai
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: OpenAI connector for Dex Flows
|
|
5
|
+
License-Expression: MIT
|
|
6
|
+
License-File: LICENSE
|
|
7
|
+
Requires-Python: >=3.11
|
|
8
|
+
Requires-Dist: dex-connectors-sdk==0.2.0
|
|
9
|
+
Requires-Dist: dex-python-sdk>=1.28.0
|
|
10
|
+
Requires-Dist: httpx>=0.27
|
|
11
|
+
Description-Content-Type: text/markdown
|
|
12
|
+
|
|
13
|
+
# OpenAI connector for Python
|
|
14
|
+
|
|
15
|
+
Install with `pip install dex-connector-openai==0.1.0`. The package requires
|
|
16
|
+
`dex-connectors-sdk==0.2.0` and `dex-python-sdk>=1.28.0`, and implements the
|
|
17
|
+
manifest operations `createResponse` (Mutation) and `retrieveResponse` (Query)
|
|
18
|
+
with the same branches, defaults, and failure classification as the Go module.
|
|
19
|
+
|
|
20
|
+
## Operations
|
|
21
|
+
|
|
22
|
+
| Operation | Factory | Happy branch | Optional branches |
|
|
23
|
+
| --- | --- | --- | --- |
|
|
24
|
+
| `createResponse` | `new_create_response_step` | `completed` | `failed`, `providerRejected`, `uncertain`, `defect` |
|
|
25
|
+
| `retrieveResponse` | `new_retrieve_response_step` | `found` | `notFound`, `providerRejected`, `invalidResponse`, `defect` |
|
|
26
|
+
|
|
27
|
+
## createResponse retries
|
|
28
|
+
|
|
29
|
+
The Responses API does not deduplicate create requests, so `createResponse`
|
|
30
|
+
fences every retry with its own Dex heartbeat checkpoint. Keep the Step's
|
|
31
|
+
manifest sync Execute durability, and record no other heartbeats in that Step.
|
|
32
|
+
|
|
33
|
+
- An attempt that observed a response ID reads that response back instead of
|
|
34
|
+
creating another one, polling `queued` and `in_progress` until five seconds
|
|
35
|
+
before the Execute deadline.
|
|
36
|
+
- Only an attempt that provably created nothing (503, 429, a connection
|
|
37
|
+
refused before sending, or a `cancelled` response) lets the next attempt
|
|
38
|
+
resend.
|
|
39
|
+
- Every other unconfirmed outcome selects `uncertain`: 408, 409, a 5xx other
|
|
40
|
+
than 503, an ambiguous transport failure, a streamed error before a response
|
|
41
|
+
ID, an unreadable body, or a read-back that cannot confirm the response.
|
|
42
|
+
|
|
43
|
+
The manifest retry policy allows three attempts within five minutes because a
|
|
44
|
+
duplicate create is billed.
|
|
45
|
+
|
|
46
|
+
## Streaming and structured output
|
|
47
|
+
|
|
48
|
+
Passing `progress_stream` or `text_stream` to `new_create_response_step` makes
|
|
49
|
+
OpenAI stream the response. Lifecycle events become progress, text deltas are
|
|
50
|
+
appended to the text Stream best effort, and the terminal event selects the
|
|
51
|
+
branch. `maxResponseBytes` bounds the whole stream and `maxSseEventBytes` one
|
|
52
|
+
event; after either limit the connector drains the stream and reads the stored
|
|
53
|
+
response back when its ID was observed.
|
|
54
|
+
|
|
55
|
+
`StructuredOutput` sends a JSON Schema `text.format`; the completed
|
|
56
|
+
`output_text` is then the JSON value.
|
|
57
|
+
|
|
58
|
+
## Authorization and configuration
|
|
59
|
+
|
|
60
|
+
Create a Dex Web connection as described in the
|
|
61
|
+
[connector README](../README.md). The API key travels only in the
|
|
62
|
+
`Authorization` header. The `modelPicker` configuration UI unit
|
|
63
|
+
(`UI_UNIT_MODEL_PICKER`, port `UI_MODEL_PICKER_PORT_MODEL`) lets Dex Web store
|
|
64
|
+
a per-Step model; a blank model uses the connection's model.
|
|
65
|
+
|
|
66
|
+
## Example
|
|
67
|
+
|
|
68
|
+
[`examples/summarize_text`](examples/summarize_text/flow.py) records the
|
|
69
|
+
request, creates a summary with streamed text and a model picker, and records
|
|
70
|
+
the outcome. Run the Worker with
|
|
71
|
+
`uv run python -m examples.summarize_text.worker` after `dexcli dev` has loaded
|
|
72
|
+
a project containing the `openai-api` connection.
|
|
73
|
+
|
|
74
|
+
## Tests
|
|
75
|
+
|
|
76
|
+
`openaimock.Mock` scripts both operations and defaults to the manifest's
|
|
77
|
+
default answers. Run `uv run pytest tests` for fencing, streaming, read-back,
|
|
78
|
+
and manifest contract checks. With `dexcli dev` running, run
|
|
79
|
+
`DEX_FLOW_SERVICE_ADDRESS=127.0.0.1:34701 uv run pytest integration` to drive the
|
|
80
|
+
example through a real Dex Worker with a scripted provider transport. The
|
|
81
|
+
Python mock checks create requests and strict structured answers with the SDK
|
|
82
|
+
text generation validator, matching the Go connector's mock contract.
|
|
83
|
+
|
|
84
|
+
`uv run pytest tests/test_conformance.py` replays the Go-recorded provider
|
|
85
|
+
scenarios for both operations. Live OpenAI authorization and provider behavior
|
|
86
|
+
have not been verified.
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
# OpenAI connector for Python
|
|
2
|
+
|
|
3
|
+
Install with `pip install dex-connector-openai==0.1.0`. The package requires
|
|
4
|
+
`dex-connectors-sdk==0.2.0` and `dex-python-sdk>=1.28.0`, and implements the
|
|
5
|
+
manifest operations `createResponse` (Mutation) and `retrieveResponse` (Query)
|
|
6
|
+
with the same branches, defaults, and failure classification as the Go module.
|
|
7
|
+
|
|
8
|
+
## Operations
|
|
9
|
+
|
|
10
|
+
| Operation | Factory | Happy branch | Optional branches |
|
|
11
|
+
| --- | --- | --- | --- |
|
|
12
|
+
| `createResponse` | `new_create_response_step` | `completed` | `failed`, `providerRejected`, `uncertain`, `defect` |
|
|
13
|
+
| `retrieveResponse` | `new_retrieve_response_step` | `found` | `notFound`, `providerRejected`, `invalidResponse`, `defect` |
|
|
14
|
+
|
|
15
|
+
## createResponse retries
|
|
16
|
+
|
|
17
|
+
The Responses API does not deduplicate create requests, so `createResponse`
|
|
18
|
+
fences every retry with its own Dex heartbeat checkpoint. Keep the Step's
|
|
19
|
+
manifest sync Execute durability, and record no other heartbeats in that Step.
|
|
20
|
+
|
|
21
|
+
- An attempt that observed a response ID reads that response back instead of
|
|
22
|
+
creating another one, polling `queued` and `in_progress` until five seconds
|
|
23
|
+
before the Execute deadline.
|
|
24
|
+
- Only an attempt that provably created nothing (503, 429, a connection
|
|
25
|
+
refused before sending, or a `cancelled` response) lets the next attempt
|
|
26
|
+
resend.
|
|
27
|
+
- Every other unconfirmed outcome selects `uncertain`: 408, 409, a 5xx other
|
|
28
|
+
than 503, an ambiguous transport failure, a streamed error before a response
|
|
29
|
+
ID, an unreadable body, or a read-back that cannot confirm the response.
|
|
30
|
+
|
|
31
|
+
The manifest retry policy allows three attempts within five minutes because a
|
|
32
|
+
duplicate create is billed.
|
|
33
|
+
|
|
34
|
+
## Streaming and structured output
|
|
35
|
+
|
|
36
|
+
Passing `progress_stream` or `text_stream` to `new_create_response_step` makes
|
|
37
|
+
OpenAI stream the response. Lifecycle events become progress, text deltas are
|
|
38
|
+
appended to the text Stream best effort, and the terminal event selects the
|
|
39
|
+
branch. `maxResponseBytes` bounds the whole stream and `maxSseEventBytes` one
|
|
40
|
+
event; after either limit the connector drains the stream and reads the stored
|
|
41
|
+
response back when its ID was observed.
|
|
42
|
+
|
|
43
|
+
`StructuredOutput` sends a JSON Schema `text.format`; the completed
|
|
44
|
+
`output_text` is then the JSON value.
|
|
45
|
+
|
|
46
|
+
## Authorization and configuration
|
|
47
|
+
|
|
48
|
+
Create a Dex Web connection as described in the
|
|
49
|
+
[connector README](../README.md). The API key travels only in the
|
|
50
|
+
`Authorization` header. The `modelPicker` configuration UI unit
|
|
51
|
+
(`UI_UNIT_MODEL_PICKER`, port `UI_MODEL_PICKER_PORT_MODEL`) lets Dex Web store
|
|
52
|
+
a per-Step model; a blank model uses the connection's model.
|
|
53
|
+
|
|
54
|
+
## Example
|
|
55
|
+
|
|
56
|
+
[`examples/summarize_text`](examples/summarize_text/flow.py) records the
|
|
57
|
+
request, creates a summary with streamed text and a model picker, and records
|
|
58
|
+
the outcome. Run the Worker with
|
|
59
|
+
`uv run python -m examples.summarize_text.worker` after `dexcli dev` has loaded
|
|
60
|
+
a project containing the `openai-api` connection.
|
|
61
|
+
|
|
62
|
+
## Tests
|
|
63
|
+
|
|
64
|
+
`openaimock.Mock` scripts both operations and defaults to the manifest's
|
|
65
|
+
default answers. Run `uv run pytest tests` for fencing, streaming, read-back,
|
|
66
|
+
and manifest contract checks. With `dexcli dev` running, run
|
|
67
|
+
`DEX_FLOW_SERVICE_ADDRESS=127.0.0.1:34701 uv run pytest integration` to drive the
|
|
68
|
+
example through a real Dex Worker with a scripted provider transport. The
|
|
69
|
+
Python mock checks create requests and strict structured answers with the SDK
|
|
70
|
+
text generation validator, matching the Go connector's mock contract.
|
|
71
|
+
|
|
72
|
+
`uv run pytest tests/test_conformance.py` replays the Go-recorded provider
|
|
73
|
+
scenarios for both operations. Live OpenAI authorization and provider behavior
|
|
74
|
+
have not been verified.
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
# Copyright (c) 2026 Super Durable
|
|
2
|
+
# SPDX-License-Identifier: MIT
|
|
3
|
+
"""Runnable summary Flow using the OpenAI createResponse Mutation with streamed text."""
|
|
4
|
+
|
|
5
|
+
from dataclasses import dataclass, field
|
|
6
|
+
|
|
7
|
+
import dex
|
|
8
|
+
from dex_connectors.sdk import (
|
|
9
|
+
ConnectorConfigurationUI,
|
|
10
|
+
ConnectorUIBinding,
|
|
11
|
+
ConnectorUIUnit,
|
|
12
|
+
MutationResult,
|
|
13
|
+
)
|
|
14
|
+
|
|
15
|
+
from dex_connectors.openai import (
|
|
16
|
+
CREATE_RESPONSE_DEFINITION,
|
|
17
|
+
UI_MODEL_PICKER_PORT_MODEL,
|
|
18
|
+
UI_UNIT_MODEL_PICKER,
|
|
19
|
+
Connection,
|
|
20
|
+
CreateRequest,
|
|
21
|
+
Response,
|
|
22
|
+
Usage,
|
|
23
|
+
new_create_response_step,
|
|
24
|
+
)
|
|
25
|
+
|
|
26
|
+
FLOW_TYPE = "OpenAISummarizeText"
|
|
27
|
+
CONNECTION_NAME = "openai-api"
|
|
28
|
+
SUMMARIZE_TEXT_STEP_TYPE = "SummarizeText"
|
|
29
|
+
SUMMARY_OPERATION_ID = CREATE_RESPONSE_DEFINITION.operation.operation_id
|
|
30
|
+
_MAX_TEXT_BYTES = 64 << 10
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
@dataclass(frozen=True, slots=True)
|
|
34
|
+
class SummaryRequest:
|
|
35
|
+
"""Text to summarize; at most 64 KiB."""
|
|
36
|
+
|
|
37
|
+
text: str
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
@dataclass(frozen=True, slots=True)
|
|
41
|
+
class SummaryOutcome:
|
|
42
|
+
"""Selected branch and the stored response's summary, identity, and usage."""
|
|
43
|
+
|
|
44
|
+
branch: str
|
|
45
|
+
summary: str
|
|
46
|
+
response_id: str = field(metadata={"dex_json_name": "responseId"})
|
|
47
|
+
status: str
|
|
48
|
+
usage: Usage
|
|
49
|
+
model: str = field(default="", metadata={"dex_json_omit_default": True})
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
@dataclass(frozen=True, slots=True)
|
|
53
|
+
class SummaryModelConfiguration:
|
|
54
|
+
"""Model chosen in Dex Web's model picker; blank uses the connection's model."""
|
|
55
|
+
|
|
56
|
+
model: str = ""
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
_REQUEST = dex.Attribute("openai-summary-request", SummaryRequest)
|
|
60
|
+
_OUTCOME = dex.Attribute("openai-summary-outcome", SummaryOutcome)
|
|
61
|
+
SUMMARY_TEXT_STREAM = dex.Stream("openai-summary-text", str, 1 << 20)
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
# dex:group group-id:summary group-label:"Summary"
|
|
65
|
+
# dex:explanation text:"Check and store the submitted text before asking OpenAI for a summary."
|
|
66
|
+
class _RecordSummaryRequest(dex.Step[SummaryRequest]):
|
|
67
|
+
def get_step_type(self) -> str:
|
|
68
|
+
return "RecordSummaryRequest"
|
|
69
|
+
|
|
70
|
+
def execute(self, context: dex.Context, input: SummaryRequest) -> dex.StepDecision:
|
|
71
|
+
if not input.text.strip() or len(input.text.encode()) > _MAX_TEXT_BYTES:
|
|
72
|
+
return dex.force_fail("summary request requires non-empty text of at most 64 KiB")
|
|
73
|
+
_REQUEST.set(context, input)
|
|
74
|
+
return dex.go_to(dex.StepRef(SUMMARIZE_TEXT_STEP_TYPE), input)
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
# dex:group group-id:summary group-label:"Summary"
|
|
78
|
+
# dex:explanation text:"Record the completed summary, its response ID, and token usage."
|
|
79
|
+
class _RecordSummaryOutcome(dex.Step[MutationResult[Response]]):
|
|
80
|
+
def get_step_type(self) -> str:
|
|
81
|
+
return "RecordSummaryOutcome"
|
|
82
|
+
|
|
83
|
+
def execute(self, context: dex.Context, input: MutationResult[Response]) -> dex.StepDecision:
|
|
84
|
+
outcome = SummaryOutcome(
|
|
85
|
+
branch=input.branch,
|
|
86
|
+
summary=input.value.output_text,
|
|
87
|
+
response_id=input.value.id,
|
|
88
|
+
status=input.value.status,
|
|
89
|
+
usage=input.value.usage,
|
|
90
|
+
model=input.value.model,
|
|
91
|
+
)
|
|
92
|
+
_OUTCOME.set(context, outcome)
|
|
93
|
+
return dex.graceful_complete(outcome)
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
class SummarizeTextFlow(dex.Flow[SummaryRequest]):
|
|
97
|
+
"""Store an OpenAI response that summarizes text, streaming the summary while it is written."""
|
|
98
|
+
|
|
99
|
+
def __init__(self, connection: Connection, summary_model: SummaryModelConfiguration) -> None:
|
|
100
|
+
if connection.reference.name != CONNECTION_NAME:
|
|
101
|
+
raise ValueError("summarize text requires the static openai-api connection")
|
|
102
|
+
self._summary_model = summary_model.model.strip()
|
|
103
|
+
self._record_request = _RecordSummaryRequest()
|
|
104
|
+
self._record_outcome = _RecordSummaryOutcome()
|
|
105
|
+
self._summarize = new_create_response_step(
|
|
106
|
+
step_type=SUMMARIZE_TEXT_STEP_TYPE,
|
|
107
|
+
step_input_type=SummaryRequest,
|
|
108
|
+
annotations={
|
|
109
|
+
"groupId": "summary",
|
|
110
|
+
"groupLabel": "Summary",
|
|
111
|
+
"explanation": "Store an OpenAI Response that summarizes the submitted text, streaming the "
|
|
112
|
+
"summary while it is written.",
|
|
113
|
+
},
|
|
114
|
+
configuration_ui=ConnectorConfigurationUI(
|
|
115
|
+
(
|
|
116
|
+
ConnectorUIUnit(
|
|
117
|
+
"summaryModel",
|
|
118
|
+
UI_UNIT_MODEL_PICKER,
|
|
119
|
+
"Summary model",
|
|
120
|
+
"Choose the OpenAI model that writes the summary from OpenAI's live model list, "
|
|
121
|
+
"or keep the connection's model, which an empty pick uses.",
|
|
122
|
+
False,
|
|
123
|
+
(ConnectorUIBinding(UI_MODEL_PICKER_PORT_MODEL, "/model"),),
|
|
124
|
+
),
|
|
125
|
+
)
|
|
126
|
+
),
|
|
127
|
+
connection=connection,
|
|
128
|
+
connection_name=CONNECTION_NAME,
|
|
129
|
+
map_to_operation_input=self.map_to_create_request,
|
|
130
|
+
text_stream=SUMMARY_TEXT_STREAM,
|
|
131
|
+
completed=self._record_outcome,
|
|
132
|
+
)
|
|
133
|
+
|
|
134
|
+
def map_to_create_request(self, request: SummaryRequest) -> CreateRequest:
|
|
135
|
+
"""Ask for a short, faithful summary with the configured model."""
|
|
136
|
+
return CreateRequest(
|
|
137
|
+
input=request.text,
|
|
138
|
+
model=self._summary_model,
|
|
139
|
+
instructions=(
|
|
140
|
+
"Summarize the user's text in at most three sentences. Use only facts stated in the text."
|
|
141
|
+
),
|
|
142
|
+
)
|
|
143
|
+
|
|
144
|
+
def get_flow_type(self) -> str:
|
|
145
|
+
"""Return the stable Flow identity used in Dex Web."""
|
|
146
|
+
return FLOW_TYPE
|
|
147
|
+
|
|
148
|
+
def get_steps(self) -> dex.StepList[SummaryRequest]:
|
|
149
|
+
"""Register request recording, the OpenAI create Step, and outcome recording."""
|
|
150
|
+
return dex.StepList.start_step(self._record_request).other_steps(
|
|
151
|
+
self._summarize, self._record_outcome
|
|
152
|
+
)
|
|
153
|
+
|
|
154
|
+
def get_persistence_schema(self) -> dex.PersistenceSchema:
|
|
155
|
+
"""Persist the request, outcome, and streamed summary text."""
|
|
156
|
+
return dex.PersistenceSchema.of(_REQUEST, _OUTCOME, SUMMARY_TEXT_STREAM)
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
# Copyright (c) 2026 Super Durable
|
|
2
|
+
# SPDX-License-Identifier: MIT
|
|
3
|
+
"""Worker entrypoint for the OpenAI summarize text Flow."""
|
|
4
|
+
|
|
5
|
+
import asyncio
|
|
6
|
+
import os
|
|
7
|
+
import tempfile
|
|
8
|
+
from pathlib import Path
|
|
9
|
+
|
|
10
|
+
import dex
|
|
11
|
+
from dex_connectors.sdk.projectconfig import LoadedProject, load_from_environment
|
|
12
|
+
|
|
13
|
+
from dex_connectors.openai import CONNECTOR_ID, new_project_connection
|
|
14
|
+
|
|
15
|
+
from .flow import (
|
|
16
|
+
CONNECTION_NAME,
|
|
17
|
+
FLOW_TYPE,
|
|
18
|
+
SUMMARIZE_TEXT_STEP_TYPE,
|
|
19
|
+
SUMMARY_OPERATION_ID,
|
|
20
|
+
SummarizeTextFlow,
|
|
21
|
+
SummaryModelConfiguration,
|
|
22
|
+
)
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def load_summary_model_configuration(project: LoadedProject) -> SummaryModelConfiguration:
|
|
26
|
+
"""Read the summary Step's model pick; no saved pick uses the connection's model."""
|
|
27
|
+
for entry in project.configuration.operation_configurations:
|
|
28
|
+
if (
|
|
29
|
+
entry.get("connectorId") == CONNECTOR_ID
|
|
30
|
+
and entry.get("connectionName") == CONNECTION_NAME
|
|
31
|
+
and entry.get("operationId") == SUMMARY_OPERATION_ID
|
|
32
|
+
and entry.get("flowType") == FLOW_TYPE
|
|
33
|
+
and entry.get("stepType") == SUMMARIZE_TEXT_STEP_TYPE
|
|
34
|
+
):
|
|
35
|
+
configuration = entry["configuration"]
|
|
36
|
+
if set(configuration) - {"model"} or not isinstance(configuration.get("model", ""), str):
|
|
37
|
+
raise ValueError("summary model configuration accepts only a model string")
|
|
38
|
+
return SummaryModelConfiguration(configuration.get("model", ""))
|
|
39
|
+
return SummaryModelConfiguration()
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
async def run_worker() -> None:
|
|
43
|
+
"""Load the named Dex Web connection and serve text summaries."""
|
|
44
|
+
project = await load_from_environment()
|
|
45
|
+
connection = None
|
|
46
|
+
cache = None
|
|
47
|
+
try:
|
|
48
|
+
connection = new_project_connection(project, CONNECTION_NAME)
|
|
49
|
+
flow = SummarizeTextFlow(connection, load_summary_model_configuration(project))
|
|
50
|
+
registry = dex.Registry([flow], allow_async_handlers=True)
|
|
51
|
+
cache_directory = os.environ.get(
|
|
52
|
+
"DEX_BLOB_CACHE_DIR", str(Path(tempfile.gettempdir()) / "dex-openai-summarize-text-blobs")
|
|
53
|
+
)
|
|
54
|
+
cache = dex.open_blob_cache(dex.BlobCacheConfig(cache_directory, 1 << 30))
|
|
55
|
+
worker = dex.AsyncWorker(
|
|
56
|
+
registry,
|
|
57
|
+
cache,
|
|
58
|
+
dex.WorkerOptions(server_address=os.environ.get("DEX_FLOW_SERVICE_ADDRESS", "localhost:8801")),
|
|
59
|
+
)
|
|
60
|
+
async with worker:
|
|
61
|
+
await worker.start()
|
|
62
|
+
finally:
|
|
63
|
+
if connection is not None:
|
|
64
|
+
await connection.aclose()
|
|
65
|
+
if cache is not None:
|
|
66
|
+
cache.close()
|
|
67
|
+
await project.close()
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
def main() -> None:
|
|
71
|
+
"""Run until the Worker stops or the process receives an interrupt."""
|
|
72
|
+
asyncio.run(run_worker())
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
if __name__ == "__main__":
|
|
76
|
+
main()
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
# Copyright (c) 2026 Super Durable
|
|
2
|
+
# SPDX-License-Identifier: MIT
|
|
3
|
+
"""Run the summarize text Flow with the real createResponse client against a local Dex Server."""
|
|
4
|
+
|
|
5
|
+
import asyncio
|
|
6
|
+
import json
|
|
7
|
+
import os
|
|
8
|
+
from datetime import timedelta
|
|
9
|
+
from pathlib import Path
|
|
10
|
+
from uuid import uuid4
|
|
11
|
+
|
|
12
|
+
import dex
|
|
13
|
+
import httpx
|
|
14
|
+
import pytest
|
|
15
|
+
from dex_connectors.sdk import ConnectionRef, SecretString, StaticCredentialProvider
|
|
16
|
+
|
|
17
|
+
from dex_connectors.openai import CONNECTOR_ID, Client, Config, Credentials, new_connection
|
|
18
|
+
from examples.summarize_text.flow import (
|
|
19
|
+
CONNECTION_NAME,
|
|
20
|
+
SummarizeTextFlow,
|
|
21
|
+
SummaryModelConfiguration,
|
|
22
|
+
SummaryOutcome,
|
|
23
|
+
SummaryRequest,
|
|
24
|
+
)
|
|
25
|
+
|
|
26
|
+
RESPONSE_ID = "resp_integration001"
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def completed_stream() -> bytes:
|
|
30
|
+
events = [
|
|
31
|
+
{
|
|
32
|
+
"type": "response.created",
|
|
33
|
+
"sequence_number": 0,
|
|
34
|
+
"response": {"id": RESPONSE_ID, "status": "queued"},
|
|
35
|
+
},
|
|
36
|
+
{"type": "response.output_text.delta", "delta": "Shipped "},
|
|
37
|
+
{"type": "response.output_text.delta", "delta": "today."},
|
|
38
|
+
{
|
|
39
|
+
"type": "response.completed",
|
|
40
|
+
"response": {
|
|
41
|
+
"id": RESPONSE_ID,
|
|
42
|
+
"model": "gpt-6-sol",
|
|
43
|
+
"status": "completed",
|
|
44
|
+
"output": [
|
|
45
|
+
{"type": "message", "content": [{"type": "output_text", "text": "Shipped today."}]}
|
|
46
|
+
],
|
|
47
|
+
"usage": {"input_tokens": 9, "output_tokens": 3, "total_tokens": 12},
|
|
48
|
+
},
|
|
49
|
+
},
|
|
50
|
+
]
|
|
51
|
+
return "".join(f"data: {json.dumps(event)}\n\n" for event in events).encode()
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
class ScriptedOpenAI:
|
|
55
|
+
"""Answer POST /responses per Flow from a list of statuses, recording every request."""
|
|
56
|
+
|
|
57
|
+
def __init__(self) -> None:
|
|
58
|
+
self.statuses_by_input: dict[str, list[int]] = {}
|
|
59
|
+
self.requests: list[httpx.Request] = []
|
|
60
|
+
|
|
61
|
+
def handle(self, request: httpx.Request) -> httpx.Response:
|
|
62
|
+
self.requests.append(request)
|
|
63
|
+
statuses = self.statuses_by_input.get(json.loads(request.content)["input"], [])
|
|
64
|
+
status = statuses.pop(0) if statuses else 200
|
|
65
|
+
if status != 200:
|
|
66
|
+
return httpx.Response(status)
|
|
67
|
+
return httpx.Response(200, content=completed_stream(), headers={"Content-Type": "text/event-stream"})
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
@pytest.mark.asyncio
|
|
71
|
+
async def test_summarize_text_real_dex_worker(tmp_path: Path) -> None:
|
|
72
|
+
"""Exercise streamed completion, a fenced 503 resend, and an unresent uncertain 500."""
|
|
73
|
+
server_address = os.environ["DEX_FLOW_SERVICE_ADDRESS"]
|
|
74
|
+
provider = ScriptedOpenAI()
|
|
75
|
+
reference = ConnectionRef(CONNECTOR_ID, CONNECTION_NAME)
|
|
76
|
+
openai_client = Client(
|
|
77
|
+
Config(endpoint="http://127.0.0.1:34788/v1"),
|
|
78
|
+
StaticCredentialProvider({reference: Credentials(SecretString("sk-integration"))}),
|
|
79
|
+
httpx.AsyncClient(transport=httpx.MockTransport(provider.handle)),
|
|
80
|
+
)
|
|
81
|
+
flow = SummarizeTextFlow(new_connection(openai_client, reference), SummaryModelConfiguration())
|
|
82
|
+
registry = dex.Registry([flow], allow_async_handlers=True)
|
|
83
|
+
cache = dex.open_blob_cache(dex.BlobCacheConfig(str(tmp_path / "blobs"), 64 * 1024 * 1024))
|
|
84
|
+
worker = dex.AsyncWorker(registry, cache, dex.WorkerOptions(server_address=server_address))
|
|
85
|
+
worker_task = asyncio.create_task(worker.start())
|
|
86
|
+
try:
|
|
87
|
+
await worker.wait_pull_registered(timeout=30)
|
|
88
|
+
async with dex.AsyncClient(
|
|
89
|
+
registry, cache, dex.ClientOptions(server_address=server_address)
|
|
90
|
+
) as client:
|
|
91
|
+
for text, statuses, expected_posts in (
|
|
92
|
+
(f"streamed {uuid4()}", [], 1),
|
|
93
|
+
(f"resend after 503 {uuid4()}", [503], 2),
|
|
94
|
+
):
|
|
95
|
+
provider.statuses_by_input[text] = list(statuses)
|
|
96
|
+
flow_id = f"openai-python-{uuid4()}"
|
|
97
|
+
await client.start_flow(flow, flow_id, SummaryRequest(text))
|
|
98
|
+
result = await client.wait_for_flow(flow_id, timedelta(seconds=60))
|
|
99
|
+
assert result.status == dex.FlowStatus.COMPLETED, result.error_message
|
|
100
|
+
outcomes = [
|
|
101
|
+
item.decode(SummaryOutcome)
|
|
102
|
+
for item in result.completions
|
|
103
|
+
if item.step_type == "RecordSummaryOutcome"
|
|
104
|
+
]
|
|
105
|
+
assert len(outcomes) == 1
|
|
106
|
+
assert outcomes[0].branch == "completed"
|
|
107
|
+
assert outcomes[0].summary == "Shipped today."
|
|
108
|
+
assert outcomes[0].response_id == RESPONSE_ID
|
|
109
|
+
posts = [
|
|
110
|
+
request for request in provider.requests if json.loads(request.content)["input"] == text
|
|
111
|
+
]
|
|
112
|
+
assert len(posts) == expected_posts
|
|
113
|
+
assert len({request.headers["X-Client-Request-Id"] for request in posts}) == 1
|
|
114
|
+
|
|
115
|
+
uncertain_text = f"uncertain after 500 {uuid4()}"
|
|
116
|
+
provider.statuses_by_input[uncertain_text] = [500, 500]
|
|
117
|
+
uncertain_id = f"openai-python-uncertain-{uuid4()}"
|
|
118
|
+
await client.start_flow(flow, uncertain_id, SummaryRequest(uncertain_text))
|
|
119
|
+
uncertain = await client.wait_for_flow(uncertain_id, timedelta(seconds=60))
|
|
120
|
+
assert uncertain.status == dex.FlowStatus.FAILED
|
|
121
|
+
assert uncertain.error_message is not None and "uncertain" in uncertain.error_message
|
|
122
|
+
assert [json.loads(request.content)["input"] for request in provider.requests].count(
|
|
123
|
+
uncertain_text
|
|
124
|
+
) == 1
|
|
125
|
+
finally:
|
|
126
|
+
await worker.stop()
|
|
127
|
+
await worker_task
|
|
128
|
+
cache.close()
|
|
129
|
+
await openai_client.aclose()
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["hatchling>=1.27"]
|
|
3
|
+
build-backend = "hatchling.build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "dex-connector-openai"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "OpenAI connector for Dex Flows"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.11"
|
|
11
|
+
license = "MIT"
|
|
12
|
+
dependencies = ["dex-connectors-sdk==0.2.0", "dex-python-sdk>=1.28.0", "httpx>=0.27"]
|
|
13
|
+
|
|
14
|
+
[dependency-groups]
|
|
15
|
+
dev = ["pytest>=8", "pytest-asyncio>=0.24", "mypy>=1.13", "ruff>=0.7", "pyyaml>=6", "types-PyYAML>=6"]
|
|
16
|
+
|
|
17
|
+
[tool.hatch.build.targets.wheel]
|
|
18
|
+
packages = ["src/dex_connectors"]
|
|
19
|
+
|
|
20
|
+
[tool.pytest.ini_options]
|
|
21
|
+
testpaths = ["tests"]
|
|
22
|
+
pythonpath = [".", "src"]
|
|
23
|
+
asyncio_mode = "auto"
|
|
24
|
+
asyncio_default_fixture_loop_scope = "function"
|
|
25
|
+
|
|
26
|
+
[tool.mypy]
|
|
27
|
+
python_version = "3.11"
|
|
28
|
+
strict = true
|
|
29
|
+
mypy_path = "src"
|
|
30
|
+
explicit_package_bases = true
|
|
31
|
+
|
|
32
|
+
[tool.ruff]
|
|
33
|
+
target-version = "py311"
|
|
34
|
+
line-length = 110
|
|
35
|
+
|
|
36
|
+
[tool.ruff.lint]
|
|
37
|
+
select = ["E", "F", "I", "UP", "B"]
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
# Copyright (c) 2026 Super Durable
|
|
2
|
+
# SPDX-License-Identifier: MIT
|
|
3
|
+
"""OpenAI Responses operations for Dex connector Steps."""
|
|
4
|
+
|
|
5
|
+
from .client import (
|
|
6
|
+
COMPLETED,
|
|
7
|
+
CONNECTOR_ID,
|
|
8
|
+
CREATE_RESPONSE_DEFINITION,
|
|
9
|
+
DEFECT,
|
|
10
|
+
FAILED,
|
|
11
|
+
FOUND,
|
|
12
|
+
INVALID_RESPONSE,
|
|
13
|
+
NOT_FOUND,
|
|
14
|
+
PROVIDER_REJECTED,
|
|
15
|
+
RETRIEVE_RESPONSE_DEFINITION,
|
|
16
|
+
UI_MODEL_PICKER_PORT_MODEL,
|
|
17
|
+
UI_UNIT_MODEL_PICKER,
|
|
18
|
+
UNCERTAIN,
|
|
19
|
+
Client,
|
|
20
|
+
Config,
|
|
21
|
+
CreateResponseOperation,
|
|
22
|
+
Credentials,
|
|
23
|
+
RetrieveResponseOperation,
|
|
24
|
+
)
|
|
25
|
+
from .connection import Connection, new_connection, new_connection_with_operations, new_project_connection
|
|
26
|
+
from .models import CreateRequest, Response, RetrieveRequest, StructuredOutput, Usage
|
|
27
|
+
from .steps import new_create_response_step, new_retrieve_response_step
|
|
28
|
+
|
|
29
|
+
__all__ = [
|
|
30
|
+
"Client",
|
|
31
|
+
"Config",
|
|
32
|
+
"Connection",
|
|
33
|
+
"CreateRequest",
|
|
34
|
+
"CreateResponseOperation",
|
|
35
|
+
"Credentials",
|
|
36
|
+
"Response",
|
|
37
|
+
"RetrieveRequest",
|
|
38
|
+
"RetrieveResponseOperation",
|
|
39
|
+
"StructuredOutput",
|
|
40
|
+
"Usage",
|
|
41
|
+
"new_connection",
|
|
42
|
+
"new_connection_with_operations",
|
|
43
|
+
"new_create_response_step",
|
|
44
|
+
"new_project_connection",
|
|
45
|
+
"new_retrieve_response_step",
|
|
46
|
+
"COMPLETED",
|
|
47
|
+
"CONNECTOR_ID",
|
|
48
|
+
"CREATE_RESPONSE_DEFINITION",
|
|
49
|
+
"DEFECT",
|
|
50
|
+
"FAILED",
|
|
51
|
+
"FOUND",
|
|
52
|
+
"INVALID_RESPONSE",
|
|
53
|
+
"NOT_FOUND",
|
|
54
|
+
"PROVIDER_REJECTED",
|
|
55
|
+
"RETRIEVE_RESPONSE_DEFINITION",
|
|
56
|
+
"UI_MODEL_PICKER_PORT_MODEL",
|
|
57
|
+
"UI_UNIT_MODEL_PICKER",
|
|
58
|
+
"UNCERTAIN",
|
|
59
|
+
]
|