Skip to content

Split the SDK into internal layer packages behind a dify.X facade - #1

Merged
r-hashi01 merged 6 commits into
mainfrom
refactor/clean-architecture
Sep 28, 2026
Merged

r-hashi01 merged 6 commits into
mainfrom
refactor/clean-architecture

Conversation

@r-hashi01

@r-hashi01 r-hashi01 commented Sep 28, 2026 •

Copy link
Copy Markdown
Collaborator

Callers still import one package and write dify.X. Inside, the code is split into layers under internal/, one package each, so the compiler enforces the dependency direction.

client.go            NewApp / OpenApp / NewKnowledge / Probe: wires infra to the port
dify.go              an alias or a forwarding func for every public name, with its doc comment
internal/usecase     the resources: verbs, params, which request each sends
internal/infra       Transport: retries, 429, error mapping, stream idle timeout; credentials
internal/port        Port, the interface resources send through; Request
internal/codec       *From decoders, SSE streams and the watcher, Page[T]
internal/entity      what calls return; Usage, Amount
internal/kernel      Object (lenient JSON reads), errors
tests/               tests that drive dify.X against a fake or a live Dify, with fixtures

Commits

  1. Put the resources behind a port. Resources depend on an interface instead of *transport.
  2. Split entities and decoding out of the resource files. Reads that were inline in verbs become decoders, with the same fallbacks in the same order.
  3. Check the layers with a test. This is the one-package version of the check; commit 6 replaces it.
  4. Describe the layers in AGENTS.md.
  5. Open seams where a layer set another's unexported fields. TransportError.timeout, APIError.isUpload, the stream wrappers, and App/Knowledge construction now go through constructors. That way the split does not widen the public API.
  6. Move the layers into internal packages. This was done with a go/types codemod: names are exported only inside internal/ and qualified across packages, and the facade is generated with its doc comments. tests/architecture_test.go checks which layer imports which, and that usecase never reads Dify's JSON itself.

Compatibility

  • Public API: unchanged. I dumped every exported name, its type, and its fields and methods with go/types, before and after, and compared the two. The only differences are in how types are printed.
  • Go floor: 1.23 → 1.24. Page[T] is re-exported as a generic type alias, which Go supports from 1.24. go.mod, CI and the README are updated.
  • Docs on pkg.go.dev: dify.App etc. show as aliases. The type doc is copied to the root; field and method docs live on the internal declaration.

Verification

  • gofmt -l . and go vet ./... are clean.
  • go test -race ./... passes on Go 1.24.0 and 1.26.4: 120 top-level tests, the same set as before, with the architecture test replaced.
  • To check that the architecture tests fail when they should, I added a resource that imports infra and reads a kernel.Object. Both were reported.
  • The live harness passes against a local Dify 1.17.1 (14 tests).

The console client (Management) builds on this and is in #2.

🤖 Generated with Claude Code

r-hashi01 and others added 6 commits September 28, 2026 14:10
Resources held *transport and could reach its fields, retries and raw
responses. They now hold a port interface that says which operation to
perform and nothing about how it is sent; streams come back as an open body
rather than an *http.Response. The constructors move to client.go, the one
place that knows a transport stands behind the port.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
What Dify answers is now read in one of three places: model_app.go and
model_knowledge.go hold the types a call returns and their behaviour,
decode_app.go and decode_knowledge.go turn Dify's JSON into them, and the
resource files keep only the verbs, their params, and which request each
sends. Reads that were inline in a verb (Info, Parameters, Site, the
envelope fallbacks for documents, segments and metadata) became decoders
of their own, with the same fallbacks in the same order.

results.go is now model_app.go.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
One package leaves the compiler nothing to enforce between layers, so
architecture_test.go type-checks the package and fails when a file uses a
declaration from an outer layer, when a resource reads fields out of Dify's
JSON instead of handing it to a decoder, or when a new file is in no layer.

It found two crossings, both fixed here: TransportError's message asked the
transport whether a method is idempotent, and WaitUntilSettled borrowed the
transport's sleep. Both now live in the kernel (errors.go, clock.go).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
TransportError.timeout and APIError.isUpload were set by the transport,
the use cases built MessageStream and WorkflowRunStream around the event
stream inside them, and the constructors filled App's and Knowledge's
unexported port field. Each now goes through a constructor in the layer
that owns the field, so the layers can become packages without widening
the public API.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Each layer is now a package under internal/ — kernel, entity, codec,
port, usecase, infra — so the compiler holds the dependency direction
instead of a test reading file names. The root keeps client.go and a
dify.go that re-declares every name callers use as an alias or a
forwarding func, with its doc comment, so callers still write dify.X
and the public API is the same: every exported name, field and method
compares equal under go/types.

Names one layer needs from another are exported inside internal/,
where no caller can reach them. The tests that drive the SDK through
dify.X move to tests/, with their fixtures in tests/testdata;
usage_test.go, which tests entity internals, sits in internal/entity.
tests/architecture_test.go checks which layer imports which, and that a
resource hands Dify's JSON to a codec decoder rather than reading it.

Page[T] is re-exported as a generic type alias, which needs Go 1.24,
so go.mod and the CI floor move from 1.23 to 1.24.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@r-hashi01 r-hashi01 changed the title Layer the package, and check the layers with a test Split the SDK into internal layer packages behind a dify.X facade Sep 28, 2026
@r-hashi01 r-hashi01 changed the title Split the SDK into internal layer packages behind a dify.X facade Layer the SDK into internal packages, and add Management (console API) Sep 28, 2026
@r-hashi01
r-hashi01 force-pushed the refactor/clean-architecture branch from 5073161 to 69ea08e Compare September 28, 2026 07:54
@r-hashi01 r-hashi01 changed the title Layer the SDK into internal packages, and add Management (console API) Split the SDK into internal layer packages behind a dify.X facade Sep 28, 2026
@r-hashi01
r-hashi01 merged commit 79d0b0b into main Sep 28, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant