|
| 1 | +--- |
| 2 | +name: coze-backend-go |
| 3 | +description: "Documentation of Coze Studio backend Go + DDD conventions. Use when editing `backend/`, implementing APIs (IDL-first), working with Domain/Application/Repository/DAL boundaries, generating DAL via gorm/gen, handling errno and layered errors, crossdomain calls, sessions/middleware, transactions, mocks/tests, and ID generation." |
| 4 | +--- |
| 5 | + |
| 6 | +# Coze Studio Backend Go + DDD |
| 7 | + |
| 8 | +This skill documents the Coze Studio backend conventions as **actionable AI-coding guidelines**: **Go practices**, **DDD layering**, **IDL-driven development**, DB reverse generation via `gorm/gen`, and Coze-specific patterns such as **domain-scoped error codes**, **space/session isolation**, and **workflow-domain conventions**. |
| 9 | + |
| 10 | +> This skill is aligned with `coze-studio` backend **v0.5.1**. If the repository evolves (structure/codegen commands), treat the repo as the source of truth and refresh this skill accordingly. |
| 11 | +
|
| 12 | +## Emphasis |
| 13 | + |
| 14 | +- **Go**: interface vs implementation separation, Functional Options, error-as-value, `internal` packages, `mockgen`, context propagation, and chunked batch queries. |
| 15 | +- **DDD**: business invariants live in the Domain layer; API/Application layers orchestrate and map models only. |
| 16 | +- **Coze conventions**: IDL-first, unified `{code,msg,data}` responses, errno ranges per domain, crossdomain interfaces, and workflow-specific error handling. |
| 17 | +- **Multi-space (tenant-like)**: most resources are scoped by `SpaceID` and must enforce access control (user ↔ space membership) at the Application boundary; never query/update cross-space data without explicit checks. |
| 18 | + |
| 19 | +--- |
| 20 | + |
| 21 | +## Skill 0: Architecture and layering overview |
| 22 | + |
| 23 | +``` |
| 24 | +idl/ ← (1) IDL definitions (Thrift) |
| 25 | +backend/ |
| 26 | + api/ |
| 27 | + handler/coze/ ← (2) HTTP handlers (generated stubs + manual bodies) |
| 28 | + router/coze/ ← (3) Route registration (generated) |
| 29 | + model/ ← (4) API request/response structs (generated) |
| 30 | + middleware/ ← (5) Middleware (session/auth/log/etc.) |
| 31 | + application/{domain}/ ← (6) Application services (orchestration only) |
| 32 | + domain/{domain}/ |
| 33 | + entity/ ← (7) Domain entities |
| 34 | + service/ ← (8) Domain service interfaces + implementations |
| 35 | + repository/ ← (9) Repository interfaces + implementations |
| 36 | + internal/dal/ |
| 37 | + model/ ← (10) gorm/gen models (*.gen.go) |
| 38 | + query/ ← (11) gorm/gen query APIs (*.gen.go) |
| 39 | + dto/ ← (12) Domain DTOs |
| 40 | + crossdomain/ ← (13) Cross-domain per-domain packages (ACL) |
| 41 | + {domain}/ |
| 42 | + contract.go ← cross-domain interface definitions (anti-corruption layer) |
| 43 | + impl/ ← concrete implementations |
| 44 | + model/ ← cross-domain models (stable, avoid domain entities) |
| 45 | + *mock/ ← mocks (generated) |
| 46 | + infra/ ← (14) Infrastructure capabilities (interface + impl/) |
| 47 | + {capability}/ |
| 48 | + *.go ← capability interfaces/types (acts as "contract") |
| 49 | + entity/ ← (optional) infra-level entities/VOs |
| 50 | + impl/ ← implementations (mysql/redis/nsq/...) |
| 51 | + types/ |
| 52 | + ddl/ ← (15) DB reverse-generation scripts (gen_orm_query.go) |
| 53 | + errno/ ← (16) Global/domain error codes |
| 54 | + consts/ ← (17) Global constants |
| 55 | +``` |
| 56 | + |
| 57 | +Boot entry: `backend/main.go` → `application.Init(ctx)` → `router.GeneratedRegister(s)` |
| 58 | + |
| 59 | +Cross-domain calls must go through the `crossdomain/{domain}/contract.go` interfaces instead of directly depending on other domains’ `service` packages. |
| 60 | + |
| 61 | +--- |
| 62 | + |
| 63 | +## Documents index |
| 64 | + |
| 65 | +| Topic | File | |
| 66 | +|------|------| |
| 67 | +| End-to-end “add a module” SOP | [checklist.md](checklist.md) | |
| 68 | +| Notes and caveats | [notes.md](notes.md) | |
| 69 | +| Code pointers (paths + line ranges) | [citations.md](citations.md) | |
| 70 | +| Skill 1: IDL-first API design | [skill-01-idl.md](skill-01-idl.md) | |
| 71 | +| Skill 2: DB DDL → gorm/gen DAL generation | [skill-02-dal.md](skill-02-dal.md) | |
| 72 | +| Skill 3: Domain module structure (DDD) | [skill-03-domain.md](skill-03-domain.md) | |
| 73 | +| Skill 4: Application layer conventions | [skill-04-application.md](skill-04-application.md) | |
| 74 | +| Skill 5: Transaction patterns | [skill-05-transaction.md](skill-05-transaction.md) | |
| 75 | +| Skill 6: Errno + layered validation/error handling | [skill-06-errno.md](skill-06-errno.md) | |
| 76 | +| Skill 7: Handler conventions | [skill-07-handler.md](skill-07-handler.md) | |
| 77 | +| Skill 8: Middleware & session conventions | [skill-08-middleware.md](skill-08-middleware.md) | |
| 78 | +| Skill 9: crossdomain conventions | [skill-09-crossdomain.md](skill-09-crossdomain.md) | |
| 79 | +| Skill 10: DTO conventions | [skill-10-dto.md](skill-10-dto.md) | |
| 80 | +| Skill 11: Mocks & tests | [skill-11-mock.md](skill-11-mock.md) | |
| 81 | +| Skill 12: ID generation rules | [skill-12-id.md](skill-12-id.md) | |
| 82 | +| Skill 13: Mermaid flowcharts & architecture diagrams | [skill-13-flowchart-generator.md](skill-13-flowchart-generator.md) | |
| 83 | +| Skill 14: Eino conventions | [skill-14-eino.md](skill-14-eino.md) | |
| 84 | + |
0 commit comments