Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

2.8 KB
Raw Blame
"""The two hint mechanics a handler cannot do inline: the next page of a list, and hints on one SSE event kind."""

import json
from collections.abc import Iterator, Mapping

from pydantic import BaseModel

from controllers.openapi._hints import attach_stream_hints, next_page_hint, page_after
from controllers.openapi._models import Hint, PageQuery


class _Query(PageQuery):
    name: str | None = None


def test_next_page_hint_copies_path_and_query_and_bumps_page() -> None:
    [hint] = next_page_hint(
        op="thing.list",
        path_args={"workspace_id": "ws-1"},
        query=_Query(page=2, name="x"),
        page=page_after(page=2, limit=20, has_more=True),
    )
    assert hint == Hint(
        summary="Next page", op="thing.list", input={"workspace_id": "ws-1", "name": "x", "page": 3, "limit": 20}
    )
    last = page_after(page=5, limit=20, has_more=False)
    assert next_page_hint(op="thing.list", path_args={}, query=None, page=last) == []


def _sse(event: Mapping[str, object]) -> str:
    return f"data: {json.dumps(event)}\n\n"


def _build(event: Mapping[str, object]) -> list[Hint]:
    return [Hint(summary="Go on", op="thing.resume", input={"run_id": event["run_id"]})]


def test_attach_stream_hints_decorates_only_the_wanted_event() -> None:
    passthrough = [
        "event: ping\n\n",
        _sse({"event": "message", "answer": "paused"}),
    ]
    wanted = {"event": "paused", "run_id": "r1", "data": {"x": 1}}
    out = list(attach_stream_hints(iter([*passthrough, _sse(wanted)]), event="paused", build=_build))
    assert out[:-1] == passthrough
    assert json.loads(out[-1][len("data: ") :]) == {
        **wanted,
        "hints": [{"summary": "Go on", "op": "thing.resume", "input": {"run_id": "r1"}}],
    }


def test_attach_stream_hints_passes_through_what_it_cannot_read() -> None:
    """A frame that only mentions the event name, a non-object frame, and an event the
    builder cannot read all reach the wire unchanged instead of ending the stream."""

    class _Fields(BaseModel):
        run_id: str

    def build(event: Mapping[str, object]) -> list[Hint]:
        return _build(_Fields.model_validate(event).model_dump())

    chunks = [
        'data: {"answer": "the paused word", "event": "message"}\n\n',
        "data: not json but paused\n\n",
        'data: ["paused"]\n\n',
        _sse({"event": "paused", "data": {"x": 1}}),
    ]
    assert list(attach_stream_hints(iter(chunks), event="paused", build=build)) == chunks


def test_attach_stream_hints_closes_the_source() -> None:
    class Source:
        closed = False

        def __iter__(self) -> Iterator[str]:
            yield _sse({"event": "message"})

        def close(self) -> None:
            self.closed = True

    source = Source()
    gen = attach_stream_hints(source, event="paused", build=_build)
    next(gen)
    gen.close()
    assert source.closed