@dual-os/cli 0.0.0-stage → 0.11.0-next.0

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.
Files changed (254) hide show
  1. package/LICENSE +4 -0
  2. package/README.md +15 -2
  3. package/dist/_virtual/_rolldown/runtime.js +13 -0
  4. package/dist/auth/credentials.d.ts +17 -0
  5. package/dist/auth/credentials.d.ts.map +1 -0
  6. package/dist/auth/credentials.js +57 -0
  7. package/dist/auth/credentials.js.map +1 -0
  8. package/dist/auth/destination.d.ts +22 -0
  9. package/dist/auth/destination.d.ts.map +1 -0
  10. package/dist/auth/destination.js +49 -0
  11. package/dist/auth/destination.js.map +1 -0
  12. package/dist/auth/http.d.ts +12 -0
  13. package/dist/auth/http.d.ts.map +1 -0
  14. package/dist/auth/http.js +34 -0
  15. package/dist/auth/http.js.map +1 -0
  16. package/dist/auth/login.d.ts +22 -0
  17. package/dist/auth/login.d.ts.map +1 -0
  18. package/dist/auth/login.js +98 -0
  19. package/dist/auth/login.js.map +1 -0
  20. package/dist/auth/protocol.d.ts +26 -0
  21. package/dist/auth/protocol.d.ts.map +1 -0
  22. package/dist/auth/protocol.js +31 -0
  23. package/dist/auth/protocol.js.map +1 -0
  24. package/dist/baseline.d.ts +18 -0
  25. package/dist/baseline.d.ts.map +1 -0
  26. package/dist/baseline.js +22 -0
  27. package/dist/baseline.js.map +1 -0
  28. package/dist/bin.d.ts +1 -0
  29. package/dist/bin.js +7 -0
  30. package/dist/bin.js.map +1 -0
  31. package/dist/cli.d.ts +19 -0
  32. package/dist/cli.d.ts.map +1 -0
  33. package/dist/cli.js +492 -0
  34. package/dist/cli.js.map +1 -0
  35. package/dist/create/choices.d.ts +58 -0
  36. package/dist/create/choices.d.ts.map +1 -0
  37. package/dist/create/choices.js +121 -0
  38. package/dist/create/choices.js.map +1 -0
  39. package/dist/create/deployment.d.ts +48 -0
  40. package/dist/create/deployment.d.ts.map +1 -0
  41. package/dist/create/deployment.js +66 -0
  42. package/dist/create/deployment.js.map +1 -0
  43. package/dist/create/scaffold.d.ts +57 -0
  44. package/dist/create/scaffold.d.ts.map +1 -0
  45. package/dist/create/scaffold.js +198 -0
  46. package/dist/create/scaffold.js.map +1 -0
  47. package/dist/create/source-reference.d.ts +39 -0
  48. package/dist/create/source-reference.d.ts.map +1 -0
  49. package/dist/create/source-reference.js +185 -0
  50. package/dist/create/source-reference.js.map +1 -0
  51. package/dist/create/template.d.ts +116 -0
  52. package/dist/create/template.d.ts.map +1 -0
  53. package/dist/create/template.js +211 -0
  54. package/dist/create/template.js.map +1 -0
  55. package/dist/diagnostics.d.ts +6 -0
  56. package/dist/diagnostics.d.ts.map +1 -0
  57. package/dist/diagnostics.js +18 -0
  58. package/dist/diagnostics.js.map +1 -0
  59. package/dist/help.d.ts +16 -0
  60. package/dist/help.d.ts.map +1 -0
  61. package/dist/help.js +73 -0
  62. package/dist/help.js.map +1 -0
  63. package/dist/openapi/bundle.d.ts +18 -0
  64. package/dist/openapi/bundle.d.ts.map +1 -0
  65. package/dist/openapi/bundle.js +201 -0
  66. package/dist/openapi/bundle.js.map +1 -0
  67. package/dist/openapi/discovery.d.ts +30 -0
  68. package/dist/openapi/discovery.d.ts.map +1 -0
  69. package/dist/openapi/discovery.js +325 -0
  70. package/dist/openapi/discovery.js.map +1 -0
  71. package/dist/openapi/emit.d.ts +52 -0
  72. package/dist/openapi/emit.d.ts.map +1 -0
  73. package/dist/openapi/emit.js +596 -0
  74. package/dist/openapi/emit.js.map +1 -0
  75. package/dist/openapi/graphql.d.ts +102 -0
  76. package/dist/openapi/graphql.d.ts.map +1 -0
  77. package/dist/openapi/graphql.js +294 -0
  78. package/dist/openapi/graphql.js.map +1 -0
  79. package/dist/openapi/identity.d.ts +88 -0
  80. package/dist/openapi/identity.d.ts.map +1 -0
  81. package/dist/openapi/identity.js +98 -0
  82. package/dist/openapi/identity.js.map +1 -0
  83. package/dist/openapi/mcp.d.ts +21 -0
  84. package/dist/openapi/mcp.d.ts.map +1 -0
  85. package/dist/openapi/mcp.js +133 -0
  86. package/dist/openapi/mcp.js.map +1 -0
  87. package/dist/openapi/operations.d.ts +165 -0
  88. package/dist/openapi/operations.d.ts.map +1 -0
  89. package/dist/openapi/operations.js +361 -0
  90. package/dist/openapi/operations.js.map +1 -0
  91. package/dist/openapi/overlay.d.ts +23 -0
  92. package/dist/openapi/overlay.d.ts.map +1 -0
  93. package/dist/openapi/overlay.js +115 -0
  94. package/dist/openapi/overlay.js.map +1 -0
  95. package/dist/openapi/plugin.d.ts +83 -0
  96. package/dist/openapi/plugin.d.ts.map +1 -0
  97. package/dist/openapi/plugin.js +468 -0
  98. package/dist/openapi/plugin.js.map +1 -0
  99. package/dist/openapi/polymorphism.d.ts +21 -0
  100. package/dist/openapi/polymorphism.d.ts.map +1 -0
  101. package/dist/openapi/polymorphism.js +96 -0
  102. package/dist/openapi/polymorphism.js.map +1 -0
  103. package/dist/openapi/postman.d.ts +21 -0
  104. package/dist/openapi/postman.d.ts.map +1 -0
  105. package/dist/openapi/postman.js +286 -0
  106. package/dist/openapi/postman.js.map +1 -0
  107. package/dist/openapi/schema.d.ts +178 -0
  108. package/dist/openapi/schema.d.ts.map +1 -0
  109. package/dist/openapi/schema.js +1234 -0
  110. package/dist/openapi/schema.js.map +1 -0
  111. package/dist/openapi/select.d.ts +22 -0
  112. package/dist/openapi/select.d.ts.map +1 -0
  113. package/dist/openapi/select.js +79 -0
  114. package/dist/openapi/select.js.map +1 -0
  115. package/dist/openapi/spec.d.ts +147 -0
  116. package/dist/openapi/spec.d.ts.map +1 -0
  117. package/dist/openapi/spec.js +263 -0
  118. package/dist/openapi/spec.js.map +1 -0
  119. package/dist/openapi/swagger.d.ts +166 -0
  120. package/dist/openapi/swagger.d.ts.map +1 -0
  121. package/dist/openapi/swagger.js +298 -0
  122. package/dist/openapi/swagger.js.map +1 -0
  123. package/dist/openapi/versions.d.ts +52 -0
  124. package/dist/openapi/versions.d.ts.map +1 -0
  125. package/dist/openapi/versions.js +120 -0
  126. package/dist/openapi/versions.js.map +1 -0
  127. package/dist/plugin/archive.d.ts +27 -0
  128. package/dist/plugin/archive.d.ts.map +1 -0
  129. package/dist/plugin/archive.js +170 -0
  130. package/dist/plugin/archive.js.map +1 -0
  131. package/dist/plugin/bundle.d.ts +27 -0
  132. package/dist/plugin/bundle.d.ts.map +1 -0
  133. package/dist/plugin/bundle.js +328 -0
  134. package/dist/plugin/bundle.js.map +1 -0
  135. package/dist/plugin/init.d.ts +18 -0
  136. package/dist/plugin/init.d.ts.map +1 -0
  137. package/dist/plugin/init.js +121 -0
  138. package/dist/plugin/init.js.map +1 -0
  139. package/dist/plugin/inspection.d.ts +24 -0
  140. package/dist/plugin/inspection.d.ts.map +1 -0
  141. package/dist/plugin/inspection.js +24 -0
  142. package/dist/plugin/inspection.js.map +1 -0
  143. package/dist/plugin/package.d.ts +26 -0
  144. package/dist/plugin/package.d.ts.map +1 -0
  145. package/dist/plugin/package.js +73 -0
  146. package/dist/plugin/package.js.map +1 -0
  147. package/dist/plugin/publication.d.ts +29 -0
  148. package/dist/plugin/publication.d.ts.map +1 -0
  149. package/dist/plugin/publication.js +65 -0
  150. package/dist/plugin/publication.js.map +1 -0
  151. package/dist/plugin/validation.d.ts +15 -0
  152. package/dist/plugin/validation.d.ts.map +1 -0
  153. package/dist/plugin/validation.js +119 -0
  154. package/dist/plugin/validation.js.map +1 -0
  155. package/dist/release/archives.d.ts +33 -0
  156. package/dist/release/archives.d.ts.map +1 -0
  157. package/dist/release/archives.js +81 -0
  158. package/dist/release/archives.js.map +1 -0
  159. package/dist/release/release-manifest.d.ts +182 -0
  160. package/dist/release/release-manifest.d.ts.map +1 -0
  161. package/dist/release/release-manifest.js +79 -0
  162. package/dist/release/release-manifest.js.map +1 -0
  163. package/dist/release/releases.d.ts +36 -0
  164. package/dist/release/releases.d.ts.map +1 -0
  165. package/dist/release/releases.js +122 -0
  166. package/dist/release/releases.js.map +1 -0
  167. package/dist/skills.d.ts +35 -0
  168. package/dist/skills.d.ts.map +1 -0
  169. package/dist/skills.js +257 -0
  170. package/dist/skills.js.map +1 -0
  171. package/package.json +73 -4
  172. package/skills/dual-fde/SKILL.md +38 -0
  173. package/skills/dual-fde/references/access.md +62 -0
  174. package/skills/dual-fde/references/author-plugins.md +54 -0
  175. package/skills/dual-fde/references/cli.md +97 -0
  176. package/skills/dual-fde/references/manage-plugins.md +78 -0
  177. package/skills/dual-fde/references/migrations-deployment.md +84 -0
  178. package/skills/dual-fde/references/openapi-plugins.md +47 -0
  179. package/skills/dual-fde/references/sdk-docs.md +56 -0
  180. package/skills/dual-fde/references/upgrades.md +115 -0
  181. package/skills/dual-fde/references/working-context.md +69 -0
  182. package/src/auth/credentials.ts +84 -0
  183. package/src/auth/destination.ts +83 -0
  184. package/src/auth/http.ts +39 -0
  185. package/src/auth/login.ts +177 -0
  186. package/src/auth/protocol.ts +37 -0
  187. package/src/baseline.ts +15 -0
  188. package/src/bin.ts +4 -0
  189. package/src/cli.ts +969 -0
  190. package/src/create/choices.ts +177 -0
  191. package/src/create/deployment.ts +96 -0
  192. package/src/create/scaffold.ts +334 -0
  193. package/src/create/source-reference.ts +212 -0
  194. package/src/create/template.ts +300 -0
  195. package/src/diagnostics.ts +8 -0
  196. package/src/help.ts +142 -0
  197. package/src/openapi/bundle.ts +290 -0
  198. package/src/openapi/discovery.ts +438 -0
  199. package/src/openapi/emit.ts +1031 -0
  200. package/src/openapi/graphql.ts +375 -0
  201. package/src/openapi/identity.ts +153 -0
  202. package/src/openapi/mcp.ts +165 -0
  203. package/src/openapi/operations.ts +781 -0
  204. package/src/openapi/overlay.ts +195 -0
  205. package/src/openapi/plugin.ts +856 -0
  206. package/src/openapi/polymorphism.ts +207 -0
  207. package/src/openapi/postman.ts +403 -0
  208. package/src/openapi/schema.ts +2138 -0
  209. package/src/openapi/select.ts +113 -0
  210. package/src/openapi/spec.ts +401 -0
  211. package/src/openapi/swagger.ts +422 -0
  212. package/src/openapi/versions.ts +174 -0
  213. package/src/plugin/archive.ts +244 -0
  214. package/src/plugin/bundle.ts +572 -0
  215. package/src/plugin/init.ts +178 -0
  216. package/src/plugin/inspection.ts +19 -0
  217. package/src/plugin/package.ts +140 -0
  218. package/src/plugin/publication.ts +128 -0
  219. package/src/plugin/validation.ts +213 -0
  220. package/src/release/archives.ts +133 -0
  221. package/src/release/release-manifest.ts +79 -0
  222. package/src/release/releases.ts +210 -0
  223. package/src/skills.ts +434 -0
  224. package/templates/compose/.env.compose.example.hbs +3 -0
  225. package/templates/compose/.env.host.example.hbs +4 -0
  226. package/templates/compose/Dockerfile.hbs +12 -0
  227. package/templates/compose/OPERATIONS.md.hbs +159 -0
  228. package/templates/compose/_dockerignore +14 -0
  229. package/templates/compose/compose.external.yaml +7 -0
  230. package/templates/compose/compose.host-linux.yaml +5 -0
  231. package/templates/compose/compose.host.yaml.hbs +15 -0
  232. package/templates/compose/compose.yaml.hbs +85 -0
  233. package/templates/compose/release.env.hbs +2 -0
  234. package/templates/default/.env.example.hbs +79 -0
  235. package/templates/default/README.md.hbs +150 -0
  236. package/templates/default/_gitignore +11 -0
  237. package/templates/default/packages/customer-plugin/src/Customer.ts.hbs +17 -0
  238. package/templates/default/packages/customer-plugin/tsconfig.json +15 -0
  239. package/templates/default/packages/execution-layer/src/App.ts.hbs +7 -0
  240. package/templates/default/packages/execution-layer/src/Host.ts.hbs +31 -0
  241. package/templates/default/packages/execution-layer/src/Telemetry.ts.hbs +23 -0
  242. package/templates/default/packages/execution-layer/src/api.ts.hbs +21 -0
  243. package/templates/default/packages/execution-layer/src/bootstrap.ts +22 -0
  244. package/templates/default/packages/execution-layer/src/migrate.ts +11 -0
  245. package/templates/default/packages/execution-layer/src/registry-check.ts +29 -0
  246. package/templates/default/packages/execution-layer/src/registry-sync.ts +14 -0
  247. package/templates/default/packages/execution-layer/src/worker.ts.hbs +16 -0
  248. package/templates/default/packages/execution-layer/test/app.host-test.ts.hbs +58 -0
  249. package/templates/default/packages/execution-layer/test/app.test.ts.hbs +16 -0
  250. package/templates/default/tsconfig.json +5 -0
  251. package/templates/default/vitest.config.ts +5 -0
  252. package/templates/default/vitest.host.config.ts +6 -0
  253. package/templates/opensandbox/OPEN-SANDBOX.md +25 -0
  254. package/templates/opensandbox/docker/agent-sandbox.Dockerfile +41 -0
@@ -0,0 +1,375 @@
1
+ import {
2
+ Array,
3
+ Effect,
4
+ MutableHashMap,
5
+ MutableHashSet,
6
+ Number,
7
+ Option,
8
+ Predicate,
9
+ Record,
10
+ Result,
11
+ Schema,
12
+ String,
13
+ pipe,
14
+ } from "effect";
15
+
16
+ import * as GraphQL from "graphql";
17
+
18
+ type JsonObject = Record.ReadonlyRecord<string, unknown>;
19
+
20
+ type Selected = { schema: JsonObject; selection: string };
21
+
22
+ const scalars: Record.ReadonlyRecord<string, JsonObject> = {
23
+ Date: { format: "date", type: "string" },
24
+ DateTime: { format: "date-time", type: "string" },
25
+ Decimal: { format: "decimal", type: "string" },
26
+ HTML: { type: "string" },
27
+ JSON: {},
28
+ JSONObject: { type: "object" },
29
+ Money: { format: "decimal", type: "string" },
30
+ TimelessDate: { format: "date", type: "string" },
31
+ URL: { format: "uri", type: "string" },
32
+ UUID: { format: "uuid", type: "string" },
33
+ UnsignedInt64: { format: "uint64", type: "string" },
34
+ };
35
+
36
+ type Kind = (typeof kinds)[number];
37
+ const kinds = ["query", "mutation"] as const;
38
+
39
+ type IntrospectionQuery = typeof IntrospectionQuery.Type;
40
+ const IntrospectionQuery = Schema.declare(
41
+ (value: unknown): value is GraphQL.IntrospectionQuery =>
42
+ Predicate.hasProperty(value, "__schema") && Predicate.isObject(value.__schema),
43
+ );
44
+
45
+ type Introspection = typeof Introspection.Type;
46
+ const Introspection = Schema.Union([
47
+ Schema.Struct({ data: IntrospectionQuery }),
48
+ IntrospectionQuery,
49
+ ]);
50
+
51
+ export const introspectionQuery = GraphQL.getIntrospectionQuery({ descriptions: true });
52
+
53
+ function described(description?: string | null) {
54
+ return Predicate.isString(description) && String.isNonEmpty(description) ? { description } : {};
55
+ }
56
+
57
+ function reference(name: string) {
58
+ return { $ref: `#/components/schemas/${name}` };
59
+ }
60
+
61
+ function isConnection(type: GraphQL.GraphQLNamedType): type is GraphQL.GraphQLObjectType {
62
+ if (!GraphQL.isObjectType(type) || !pipe(type.name, String.endsWith("Connection"))) return false;
63
+ const fields = type.getFields();
64
+ return fields["pageInfo"] !== undefined && (fields["nodes"] ?? fields["edges"]) !== undefined;
65
+ }
66
+
67
+ function isSelectable(field: GraphQL.GraphQLField<unknown, unknown>) {
68
+ return (
69
+ Predicate.isNullish(field.deprecationReason) &&
70
+ !Array.some(field.args, GraphQL.isRequiredArgument)
71
+ );
72
+ }
73
+
74
+ function argumentList(
75
+ args: GraphQL.GraphQLField<unknown, unknown>["args"],
76
+ format: (arg: GraphQL.GraphQLArgument) => string,
77
+ ) {
78
+ return Array.isReadonlyArrayEmpty(args) ? "" : `(${Array.join(Array.map(args, format), ", ")})`;
79
+ }
80
+
81
+ function toDocument(schema: GraphQL.GraphQLSchema, endpoint: string) {
82
+ const components = MutableHashMap.empty<string, JsonObject>();
83
+ const selections = MutableHashMap.empty<string, string>();
84
+ const building = MutableHashSet.empty<string>();
85
+
86
+ function componentName(type: GraphQL.GraphQLNamedType, depth: number) {
87
+ const suffixed = `${type.name}${Array.getUnsafe(["Summary", "", "Detail"], depth)}`;
88
+ return depth !== 1 && schema.getType(suffixed) !== undefined
89
+ ? `${suffixed}Selection`
90
+ : suffixed;
91
+ }
92
+
93
+ function leaf(type: GraphQL.GraphQLLeafType): JsonObject {
94
+ if (GraphQL.isEnumType(type)) {
95
+ MutableHashMap.set(components, type.name, {
96
+ ...described(type.description),
97
+ enum: Array.map(type.getValues(), value => value.name),
98
+ type: "string",
99
+ });
100
+ return reference(type.name);
101
+ }
102
+ if (type.name === "Int") return { type: "integer" };
103
+ if (type.name === "Float") return { type: "number" };
104
+ if (type.name === "Boolean") return { type: "boolean" };
105
+ if (type.name === "String" || type.name === "ID") return { type: "string" };
106
+ return Option.getOrElse(Record.get(scalars, type.name), () => ({
107
+ description: `GraphQL ${type.name} scalar`,
108
+ }));
109
+ }
110
+
111
+ function input(type: GraphQL.GraphQLInputType): JsonObject {
112
+ if (GraphQL.isNonNullType(type)) return given(type.ofType);
113
+ return { anyOf: [given(type), { type: "null" }] };
114
+ }
115
+
116
+ function given(type: GraphQL.GraphQLInputType): JsonObject {
117
+ if (GraphQL.isNonNullType(type)) return given(type.ofType);
118
+ if (GraphQL.isListType(type)) return { items: input(type.ofType), type: "array" };
119
+ if (GraphQL.isLeafType(type)) return leaf(type);
120
+ if (MutableHashMap.has(components, type.name)) return reference(type.name);
121
+
122
+ MutableHashMap.set(components, type.name, {});
123
+ const fields = Record.values(type.getFields());
124
+ MutableHashMap.set(components, type.name, {
125
+ ...described(type.description),
126
+ properties: Record.fromIterableWith(fields, field => [
127
+ field.name,
128
+ { ...input(field.type), ...described(field.description) },
129
+ ]),
130
+ required: Array.map(Array.filter(fields, GraphQL.isRequiredInputField), field => field.name),
131
+ type: "object",
132
+ });
133
+ return reference(type.name);
134
+ }
135
+
136
+ function output(type: GraphQL.GraphQLOutputType, depth: number, root = false): Selected {
137
+ if (GraphQL.isNonNullType(type)) return present(type.ofType, depth, root);
138
+ const selected = present(type, depth, root);
139
+ return {
140
+ schema: { anyOf: [selected.schema, { type: "null" }] },
141
+ selection: selected.selection,
142
+ };
143
+ }
144
+
145
+ function present(type: GraphQL.GraphQLType, depth: number, root: boolean): Selected {
146
+ if (GraphQL.isListType(type)) {
147
+ const item = output(GraphQL.assertOutputType(type.ofType), depth);
148
+ return { schema: { items: item.schema, type: "array" }, selection: item.selection };
149
+ }
150
+ const named = GraphQL.getNamedType(type);
151
+ if (GraphQL.isLeafType(named)) return { schema: leaf(named), selection: "" };
152
+ return composite(GraphQL.assertCompositeType(named), depth, root);
153
+ }
154
+
155
+ function fieldsOf(
156
+ type: GraphQL.GraphQLObjectType | GraphQL.GraphQLInterfaceType,
157
+ depth: number,
158
+ root: boolean,
159
+ omitted: string[] = [],
160
+ ) {
161
+ const fields = Record.values(type.getFields());
162
+ if (root && isConnection(type)) {
163
+ const callable = Array.filter(fields, isSelectable);
164
+ const listed = Array.some(callable, field => field.name === "nodes") ? "nodes" : "edges";
165
+ const chosen = Array.filter(
166
+ callable,
167
+ field => field.name === listed || field.name === "pageInfo",
168
+ );
169
+ const fallback = Array.isReadonlyArrayEmpty(chosen)
170
+ ? Array.filter(callable, field => GraphQL.isLeafType(GraphQL.getNamedType(field.type)))
171
+ : chosen;
172
+ return Array.map(fallback, field => {
173
+ const nodeDepth = field.name === "nodes" ? 1 : 0;
174
+ const selectedDepth = field.name === "edges" ? 2 : nodeDepth;
175
+ return { field, selected: output(field.type, selectedDepth) };
176
+ });
177
+ }
178
+ const selectable = Array.filter(
179
+ fields,
180
+ field => isSelectable(field) && !Array.contains(omitted, field.name),
181
+ );
182
+ const leaves = Array.filter(selectable, field =>
183
+ GraphQL.isLeafType(GraphQL.getNamedType(field.type)),
184
+ );
185
+ const linked = depth > 0 || Array.isReadonlyArrayEmpty(leaves);
186
+ const nested = Number.max(depth - 1, 0);
187
+ return Array.filterMap(selectable, field => {
188
+ const named = GraphQL.getNamedType(field.type);
189
+ if (GraphQL.isLeafType(named)) {
190
+ return Result.succeed({ field, selected: output(field.type, depth) });
191
+ }
192
+ if (
193
+ !linked ||
194
+ isConnection(named) ||
195
+ MutableHashSet.has(building, componentName(named, nested))
196
+ ) {
197
+ return Result.failVoid;
198
+ }
199
+ return Result.succeed({ field, selected: output(field.type, nested) });
200
+ });
201
+ }
202
+
203
+ function union(type: GraphQL.GraphQLUnionType, name: string) {
204
+ // A member can reach this union again; marking it stops the recursion like an object's.
205
+ MutableHashSet.add(building, name);
206
+ const shapes = Array.flatMap(type.getTypes(), member =>
207
+ Array.map(fieldsOf(member, 0, false), entry => ({
208
+ name: entry.field.name,
209
+ shape: `${entry.field.type.toString()} ${entry.selected.selection}`,
210
+ })),
211
+ );
212
+ const conflicting = Array.dedupe(
213
+ Array.filterMap(shapes, entry =>
214
+ Array.some(shapes, other => other.name === entry.name && other.shape !== entry.shape)
215
+ ? Result.succeed(entry.name)
216
+ : Result.failVoid,
217
+ ),
218
+ );
219
+ const members = Array.map(type.getTypes(), member => {
220
+ const omitted = Array.filter(conflicting, field => member.getFields()[field] !== undefined);
221
+ if (Array.isReadonlyArrayEmpty(omitted)) {
222
+ return { member, selected: composite(member, 0, false) };
223
+ }
224
+ const variant = `${type.name}${member.name}`;
225
+ const selection = object(member, variant, 0, false, omitted);
226
+ MutableHashMap.set(selections, variant, selection);
227
+ return { member, selected: { schema: reference(variant), selection } };
228
+ });
229
+ MutableHashSet.remove(building, name);
230
+ MutableHashMap.set(components, name, {
231
+ ...described(type.description),
232
+ anyOf: Array.map(members, entry => entry.selected.schema),
233
+ });
234
+ return `{ __typename ${Array.join(
235
+ Array.map(members, entry => `... on ${entry.member.name} ${entry.selected.selection}`),
236
+ " ",
237
+ )} }`;
238
+ }
239
+
240
+ function object(
241
+ type: GraphQL.GraphQLObjectType | GraphQL.GraphQLInterfaceType,
242
+ name: string,
243
+ depth: number,
244
+ root: boolean,
245
+ omitted: string[] = [],
246
+ ) {
247
+ MutableHashSet.add(building, name);
248
+ const fields = fieldsOf(type, depth, root, omitted);
249
+ MutableHashSet.remove(building, name);
250
+ const typename =
251
+ GraphQL.isInterfaceType(type) || Array.isReadonlyArrayEmpty(fields) ? ["__typename"] : [];
252
+ MutableHashMap.set(components, name, {
253
+ ...described(type.description),
254
+ properties: {
255
+ ...Record.fromIterableWith(typename, key => [key, { type: "string" }]),
256
+ ...Record.fromIterableWith(fields, entry => [
257
+ entry.field.name,
258
+ { ...entry.selected.schema, ...described(entry.field.description) },
259
+ ]),
260
+ },
261
+ required: [
262
+ ...typename,
263
+ ...Array.map(
264
+ Array.filter(fields, entry => GraphQL.isNonNullType(entry.field.type)),
265
+ entry => entry.field.name,
266
+ ),
267
+ ],
268
+ type: "object",
269
+ });
270
+ return `{ ${Array.join(
271
+ [
272
+ ...typename,
273
+ ...Array.map(fields, entry =>
274
+ String.isEmpty(entry.selected.selection)
275
+ ? entry.field.name
276
+ : `${entry.field.name} ${entry.selected.selection}`,
277
+ ),
278
+ ],
279
+ " ",
280
+ )} }`;
281
+ }
282
+
283
+ function composite(type: GraphQL.GraphQLCompositeType, depth: number, root: boolean): Selected {
284
+ const name = componentName(type, depth);
285
+ const selection = Option.getOrElse(MutableHashMap.get(selections, name), () =>
286
+ GraphQL.isUnionType(type) ? union(type, name) : object(type, name, depth, root),
287
+ );
288
+ MutableHashMap.set(selections, name, selection);
289
+ return { schema: reference(name), selection };
290
+ }
291
+
292
+ function operation(kind: Kind, field: GraphQL.GraphQLField<unknown, unknown>) {
293
+ const selected = output(field.type, kind === "mutation" ? 2 : 1, true);
294
+ const variables = argumentList(
295
+ field.args,
296
+ arg =>
297
+ `$${arg.name}: ${(arg.defaultValue !== undefined && GraphQL.isNonNullType(arg.type) ? arg.type.ofType : arg.type).toString()}`,
298
+ );
299
+ const call = argumentList(field.args, arg => `${arg.name}: $${arg.name}`);
300
+ const selection = String.isEmpty(selected.selection) ? "" : ` ${selected.selection}`;
301
+ return {
302
+ ...(Predicate.isNullish(field.deprecationReason) ? {} : { deprecated: true }),
303
+ ...described(field.description),
304
+ operationId: field.name,
305
+ ...(Array.isReadonlyArrayEmpty(field.args)
306
+ ? {}
307
+ : {
308
+ requestBody: {
309
+ content: {
310
+ "application/json": {
311
+ schema: {
312
+ properties: Record.fromIterableWith(field.args, arg => [
313
+ arg.name,
314
+ { ...input(arg.type), ...described(arg.description) },
315
+ ]),
316
+ required: Array.map(
317
+ Array.filter(field.args, GraphQL.isRequiredArgument),
318
+ arg => arg.name,
319
+ ),
320
+ type: "object",
321
+ },
322
+ },
323
+ },
324
+ required: Array.some(field.args, GraphQL.isRequiredArgument),
325
+ },
326
+ }),
327
+ responses: {
328
+ "200": {
329
+ content: { "application/json": { schema: selected.schema } },
330
+ description: `The ${field.name} ${kind} result`,
331
+ },
332
+ },
333
+ tags: [kind],
334
+ "x-dual-graphql": {
335
+ document: `${kind} ${String.capitalize(field.name)}${variables} { ${field.name}${call}${selection} }`,
336
+ endpoint,
337
+ field: field.name,
338
+ },
339
+ };
340
+ }
341
+
342
+ const paths = Array.flatMap(kinds, kind => {
343
+ const root = kind === "query" ? schema.getQueryType() : schema.getMutationType();
344
+ return Array.map(
345
+ Record.values(root?.getFields() ?? {}),
346
+ field => [`/${kind}/${field.name}`, { post: operation(kind, field) }] as const,
347
+ );
348
+ });
349
+ return {
350
+ components: { schemas: Record.fromEntries(components) },
351
+ info: { ...described(schema.description), title: "", version: "graphql" },
352
+ openapi: "3.1.0",
353
+ paths: Record.fromEntries(paths),
354
+ servers: [{ url: endpoint }],
355
+ };
356
+ }
357
+
358
+ export const toOpenApi = Effect.fn("OpenApiGraphql.toOpenApi")(function* (
359
+ text: string,
360
+ endpoint: string,
361
+ ) {
362
+ if (!pipe(text, String.trimStart, String.startsWith("{"))) {
363
+ return toDocument(
364
+ yield* Effect.try(() => GraphQL.buildSchema(text, { assumeValidSDL: true })),
365
+ endpoint,
366
+ );
367
+ }
368
+ const decoded = yield* Schema.decodeEffect(Schema.fromJsonString(Introspection))(text);
369
+ const built = yield* Effect.try(() =>
370
+ GraphQL.buildClientSchema("data" in decoded ? decoded.data : decoded, { assumeValid: true }),
371
+ );
372
+ return toDocument(built, endpoint);
373
+ });
374
+
375
+ export * as OpenApiGraphql from "./graphql.ts";
@@ -0,0 +1,153 @@
1
+ import { Array, HashSet, Option, Record, Result, Schema, String, pipe } from "effect";
2
+
3
+ import { OpenApiSchema } from "./schema.ts";
4
+
5
+ type Alias = typeof Alias.Type;
6
+ const Alias = Schema.Struct({
7
+ method: Schema.String,
8
+ operationId: Schema.String,
9
+ path: Schema.String,
10
+ });
11
+
12
+ type Entry = typeof Entry.Type;
13
+ const Entry = Schema.Struct({
14
+ aliases: Schema.NonEmptyArray(Alias),
15
+ newIdentityFor: Schema.optionalKey(Schema.String),
16
+ source: Schema.String,
17
+ });
18
+
19
+ export type Ledger = typeof Ledger.Type;
20
+ export const Ledger = Schema.Record(Schema.String, Entry);
21
+
22
+ export function resolve(
23
+ actions: readonly (OpenApiSchema.Action & { readonly source: string })[],
24
+ previous: Ledger,
25
+ mappings: Record<string, string>,
26
+ ) {
27
+ const newIdentities = pipe(
28
+ Record.toEntries(previous),
29
+ Array.flatMap(entry =>
30
+ entry[1].newIdentityFor === undefined ? [] : [[entry[1].newIdentityFor, entry[0]] as const],
31
+ ),
32
+ Record.fromEntries,
33
+ );
34
+ const aliases = Array.flatMap(Record.toEntries(previous), entry =>
35
+ Array.map(entry[1].aliases, alias => ({ alias, canonical: entry[0], source: entry[1].source })),
36
+ );
37
+ const endpoints = Array.groupBy(
38
+ aliases,
39
+ entry => `${entry.source} ${entry.alias.method} ${entry.alias.path}`,
40
+ );
41
+ const operationIds = Array.groupBy(
42
+ aliases,
43
+ entry => `${entry.source} ${entry.alias.operationId}`,
44
+ );
45
+ function matchesOf(action: OpenApiSchema.Action & { readonly source: string }) {
46
+ const operation = action.operation;
47
+ const source = `${action.provider}/${action.source}`;
48
+ const exact =
49
+ endpoints[`${source} ${String.toUpperCase(operation.method)} ${operation.path}`] ?? [];
50
+ const renamed = String.isNonEmpty(operation.operationId)
51
+ ? (operationIds[`${source} ${operation.operationId}`] ?? [])
52
+ : [];
53
+ return pipe(
54
+ Array.isReadonlyArrayNonEmpty(exact) ? exact : renamed,
55
+ Array.map(entry => entry.canonical),
56
+ Array.dedupe,
57
+ );
58
+ }
59
+ const claimed = HashSet.fromIterable(
60
+ Array.flatMap(actions, action => {
61
+ const matches = matchesOf(action);
62
+ return matches.length === 1 ? matches : [];
63
+ }),
64
+ );
65
+ const resolved = Result.all(
66
+ Array.map(actions, action => {
67
+ const operation = action.operation;
68
+ const source = `${action.provider}/${action.source}`;
69
+ const alias = {
70
+ method: String.toUpperCase(operation.method),
71
+ operationId: operation.operationId,
72
+ path: operation.path,
73
+ };
74
+ const key = `${source} ${alias.method} ${alias.path}` as const;
75
+ const mapped = mappings[key];
76
+ const matches = matchesOf(action);
77
+ if (mapped !== undefined && mapped !== "new" && previous[mapped]?.source !== source) {
78
+ return Result.fail(
79
+ `Identity mapping ${key} must name an existing action in ${source}, or new`,
80
+ );
81
+ }
82
+ if (mapped === undefined && matches.length > 1) {
83
+ return Result.fail(
84
+ `Ambiguous identity for ${key}; set its identityMappings entry to the intended published action`,
85
+ );
86
+ }
87
+ const existing =
88
+ mapped === "new"
89
+ ? newIdentities[key]
90
+ : (mapped ?? Option.getOrUndefined(Array.head(matches)));
91
+ const proposed = `${action.provider}/${operation.groupId}/${operation.actionId}`;
92
+ // An operation whose endpoint moved keeps the action it names when that action lost its endpoint.
93
+ const canonical =
94
+ existing ??
95
+ (Record.has(previous, proposed) && HashSet.has(claimed, proposed)
96
+ ? `${proposed}-${OpenApiSchema.hash(key)}`
97
+ : proposed);
98
+ const parts = pipe(canonical, String.split("/"));
99
+ return Result.succeed({
100
+ action: {
101
+ ...action,
102
+ operation: {
103
+ ...operation,
104
+ actionId: Array.getUnsafe(parts, 2),
105
+ groupId: Array.getUnsafe(parts, 1),
106
+ },
107
+ },
108
+ alias,
109
+ canonical,
110
+ explicitNew: mapped === "new",
111
+ key,
112
+ source,
113
+ });
114
+ }),
115
+ );
116
+ if (Result.isFailure(resolved)) return Result.fail(resolved.failure);
117
+ const entries = resolved.success;
118
+ const ids = HashSet.fromIterable(Array.map(entries, entry => entry.canonical));
119
+ if (HashSet.size(ids) !== entries.length) {
120
+ return Result.fail(
121
+ "Several source operations resolve to the same published action; supply distinct identityMappings",
122
+ );
123
+ }
124
+ const ledger = {
125
+ ...previous,
126
+ ...Record.fromEntries(
127
+ Array.map(entries, entry => {
128
+ const previousAliases = previous[entry.canonical]?.aliases ?? [];
129
+ return [
130
+ entry.canonical,
131
+ {
132
+ ...previous[entry.canonical],
133
+ ...(entry.explicitNew ? { newIdentityFor: entry.key } : {}),
134
+ aliases: [
135
+ entry.alias,
136
+ ...Array.filter(
137
+ previousAliases,
138
+ alias =>
139
+ alias.method !== entry.alias.method ||
140
+ alias.path !== entry.alias.path ||
141
+ alias.operationId !== entry.alias.operationId,
142
+ ),
143
+ ],
144
+ source: entry.source,
145
+ },
146
+ ] as const;
147
+ }),
148
+ ),
149
+ };
150
+ return Result.succeed({ actions: Array.map(entries, entry => entry.action), ledger });
151
+ }
152
+
153
+ export * as OpenApiIdentity from "./identity.ts";
@@ -0,0 +1,165 @@
1
+ import { Array, HashSet, Option, Predicate, Record, String, pipe } from "effect";
2
+
3
+ import type { Mcp } from "@dual-os/sdk/Mcp";
4
+
5
+ type JsonObject = Record.ReadonlyRecord<string, unknown>;
6
+
7
+ const content = {
8
+ properties: {
9
+ content: {
10
+ items: {
11
+ properties: {
12
+ data: {
13
+ contentEncoding: "base64",
14
+ description: "The image or audio of the block",
15
+ type: "string",
16
+ },
17
+ mimeType: { type: "string" },
18
+ text: { type: "string" },
19
+ type: { description: "text, image, audio, resource_link or resource", type: "string" },
20
+ uri: { type: "string" },
21
+ },
22
+ required: ["type"],
23
+ type: "object",
24
+ },
25
+ type: "array",
26
+ },
27
+ },
28
+ required: ["content"],
29
+ type: "object",
30
+ };
31
+
32
+ function refersToRoot(value: unknown): boolean {
33
+ if (Array.isArray(value)) return Array.some(value, refersToRoot);
34
+ if (!Predicate.isObject(value)) return false;
35
+ return value["$ref"] === "#" || Array.some(Record.values(value), refersToRoot);
36
+ }
37
+
38
+ /**
39
+ * Moves a tool schema's `$defs` into components under names no earlier tool took, and points its
40
+ * references there. A reference to the whole schema, `#`, makes the schema itself a component.
41
+ */
42
+ function hoisted(prefix: string, schema: JsonObject, taken: HashSet.HashSet<string>) {
43
+ const definitions = Predicate.isObject(schema["$defs"]) ? schema["$defs"] : {};
44
+ function unique(reserved: HashSet.HashSet<string>, name: string) {
45
+ return pipe(
46
+ Array.makeBy(HashSet.size(reserved) + 1, index => (index === 0 ? name : `${name}${index}`)),
47
+ Array.findFirst(candidate => !HashSet.has(reserved, candidate)),
48
+ Option.getOrElse(() => name),
49
+ );
50
+ }
51
+ // Each name is reserved as soon as it is given, so two definitions never share one.
52
+ const allocated = Array.reduce(
53
+ Record.keys(definitions),
54
+ { names: Record.empty<string, string>(), reserved: taken },
55
+ (done, name) => {
56
+ const unused = unique(done.reserved, `${prefix}${name}`);
57
+ return {
58
+ names: Record.set(done.names, name, unused),
59
+ reserved: HashSet.add(done.reserved, unused),
60
+ };
61
+ },
62
+ );
63
+ const names = allocated.names;
64
+ const rootName = unique(allocated.reserved, prefix);
65
+ function rewrite(value: unknown): unknown {
66
+ if (Array.isArray(value)) return Array.map(value, rewrite);
67
+ if (!Predicate.isObject(value)) return value;
68
+ return Record.map(value, (entry, key) => {
69
+ if (key !== "$ref" || !Predicate.isString(entry)) return rewrite(entry);
70
+ if (entry === "#") return `#/components/schemas/${rootName}`;
71
+ // A pointer into a definition keeps the segments after the definition's name.
72
+ const pointer = /^#\/\$defs\/([^/]+)(.*)$/u.exec(entry);
73
+ return pipe(
74
+ Option.fromNullishOr(pointer?.[1]),
75
+ Option.flatMap(name => Record.get(names, name)),
76
+ Option.match({
77
+ onNone: () => entry,
78
+ onSome: name => `#/components/schemas/${name}${pointer?.[2] ?? ""}`,
79
+ }),
80
+ );
81
+ });
82
+ }
83
+ const own = Record.remove(schema, "$defs");
84
+ const hoistedDefinitions = Record.fromEntries(
85
+ Array.map(Record.toEntries(definitions), entry => [
86
+ Option.getOrElse(Record.get(names, entry[0]), () => entry[0]),
87
+ rewrite(entry[1]),
88
+ ]),
89
+ );
90
+ if (!refersToRoot(schema)) return { definitions: hoistedDefinitions, schema: rewrite(own) };
91
+ return {
92
+ definitions: { ...hoistedDefinitions, [rootName]: rewrite(own) },
93
+ schema: { $ref: `#/components/schemas/${rootName}` },
94
+ };
95
+ }
96
+
97
+ type Converted = {
98
+ readonly definitions: Record.ReadonlyRecord<string, unknown>;
99
+ readonly path: readonly [string, unknown];
100
+ };
101
+
102
+ function convertTool(tool: Mcp.Tool, endpoint: string, taken: HashSet.HashSet<string>): Converted {
103
+ const name = String.capitalize(tool.name);
104
+ const input = hoisted(`${name}Input`, tool.inputSchema, taken);
105
+ const output =
106
+ tool.outputSchema === undefined
107
+ ? undefined
108
+ : hoisted(
109
+ `${name}Output`,
110
+ tool.outputSchema,
111
+ HashSet.union(taken, HashSet.fromIterable(Record.keys(input.definitions))),
112
+ );
113
+ const required = tool.inputSchema["required"];
114
+ return {
115
+ definitions: { ...input.definitions, ...output?.definitions },
116
+ path: [
117
+ `/tools/${tool.name}`,
118
+ {
119
+ post: {
120
+ ...(tool.description === undefined ? {} : { description: tool.description }),
121
+ operationId: tool.name,
122
+ requestBody: {
123
+ content: { "application/json": { schema: input.schema } },
124
+ required: Array.isArray(required) && Array.isReadonlyArrayNonEmpty(required),
125
+ },
126
+ responses: {
127
+ "200": {
128
+ content: { "application/json": { schema: output?.schema ?? content } },
129
+ description: `The ${tool.name} result`,
130
+ },
131
+ },
132
+ summary: tool.title ?? tool.annotations?.title ?? tool.name,
133
+ tags: ["tools"],
134
+ "x-dual-mcp": { endpoint, structured: tool.outputSchema !== undefined, tool: tool.name },
135
+ },
136
+ },
137
+ ],
138
+ };
139
+ }
140
+
141
+ export function toOpenApi(server: Mcp.Implementation, tools: Mcp.Tool[], endpoint: string) {
142
+ const converted = Array.reduce(tools, Array.empty<Converted>(), (done, tool) =>
143
+ Array.append(
144
+ done,
145
+ convertTool(
146
+ tool,
147
+ endpoint,
148
+ HashSet.fromIterable(Array.flatMap(done, entry => Record.keys(entry.definitions))),
149
+ ),
150
+ ),
151
+ );
152
+ return {
153
+ components: {
154
+ schemas: Record.fromEntries(
155
+ Array.flatMap(converted, entry => Record.toEntries(entry.definitions)),
156
+ ),
157
+ },
158
+ info: { title: server.title ?? server.name, version: server.version },
159
+ openapi: "3.1.0",
160
+ paths: Record.fromEntries(Array.map(converted, entry => entry.path)),
161
+ servers: [{ url: endpoint }],
162
+ };
163
+ }
164
+
165
+ export * as OpenApiMcp from "./mcp.ts";