Skip to content

Commit 29cdc81

Browse files
committed
feat(compose): support AI models in Compose specification
1 parent 9fa98de commit 29cdc81

4 files changed

Lines changed: 257 additions & 0 deletions

File tree

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
import type { Compose, ComposeSpecification } from "@dokploy/server";
2+
import {
3+
addAppNameToPreventCollision,
4+
addSuffixToAllProperties,
5+
} from "@dokploy/server";
6+
import { addDomainToCompose } from "@dokploy/server/utils/docker/domain";
7+
import { execAsyncRemote } from "@dokploy/server/utils/process/execAsync";
8+
import { expect, test, vi } from "vitest";
9+
import { parse, stringify } from "yaml";
10+
11+
vi.mock("@dokploy/server/utils/process/execAsync", async (importOriginal) => ({
12+
...(await importOriginal<
13+
typeof import("@dokploy/server/utils/process/execAsync")
14+
>()),
15+
execAsyncRemote: vi.fn(),
16+
}));
17+
18+
const modelsComposeFile = `
19+
services:
20+
chat:
21+
image: my-chat-app
22+
models:
23+
- llm
24+
worker:
25+
image: my-worker
26+
models:
27+
embed:
28+
endpoint_var: EMBED_URL
29+
model_var: EMBED_MODEL
30+
models:
31+
llm:
32+
model: ai/smollm2
33+
context_size: 2048
34+
runtime_flags:
35+
- "--verbose"
36+
embed:
37+
model: ai/all-minilm
38+
name: embeddings
39+
`;
40+
41+
const expectedModels = {
42+
llm: {
43+
model: "ai/smollm2",
44+
context_size: 2048,
45+
runtime_flags: ["--verbose"],
46+
},
47+
embed: {
48+
model: "ai/all-minilm",
49+
name: "embeddings",
50+
},
51+
};
52+
53+
const expectedChatModels = ["llm"];
54+
const expectedWorkerModels = {
55+
embed: {
56+
endpoint_var: "EMBED_URL",
57+
model_var: "EMBED_MODEL",
58+
},
59+
};
60+
61+
const assertModelsPreserved = (
62+
spec: ComposeSpecification | null,
63+
serviceNames: { chat: string; worker: string } = {
64+
chat: "chat",
65+
worker: "worker",
66+
},
67+
) => {
68+
expect(spec?.models).toEqual(expectedModels);
69+
expect(spec?.services?.[serviceNames.chat]?.models).toEqual(
70+
expectedChatModels,
71+
);
72+
expect(spec?.services?.[serviceNames.worker]?.models).toEqual(
73+
expectedWorkerModels,
74+
);
75+
};
76+
77+
const rawCompose = (overrides?: Record<string, unknown>) =>
78+
({
79+
appName: "chat-app",
80+
composeFile: modelsComposeFile,
81+
composePath: "./docker-compose.yml",
82+
composeType: "docker-compose",
83+
isolatedDeployment: false,
84+
isolatedDeploymentsVolume: false,
85+
randomize: false,
86+
serverId: null,
87+
sourceType: "raw",
88+
suffix: "",
89+
...overrides,
90+
}) as unknown as Compose;
91+
92+
test("compose without models is unchanged besides existing suffix behavior", () => {
93+
const composeData = parse(`
94+
services:
95+
web:
96+
image: nginx:latest
97+
volumes:
98+
- web_data:/data
99+
volumes:
100+
web_data:
101+
`) as ComposeSpecification;
102+
const updated = addSuffixToAllProperties(composeData, "testhash");
103+
104+
expect(updated.models).toBeUndefined();
105+
expect(updated.services).toEqual({
106+
"web-testhash": {
107+
image: "nginx:latest",
108+
volumes: ["web_data-testhash:/data"],
109+
},
110+
});
111+
expect(updated.volumes).toEqual({
112+
"web_data-testhash": null,
113+
});
114+
});
115+
116+
test("suffixing does not rename model identifiers or drop model config", () => {
117+
const updated = addSuffixToAllProperties(
118+
parse(modelsComposeFile) as ComposeSpecification,
119+
"testhash",
120+
);
121+
122+
assertModelsPreserved(updated, {
123+
chat: "chat-testhash",
124+
worker: "worker-testhash",
125+
});
126+
expect(updated.services).not.toHaveProperty("chat");
127+
expect(updated.models).not.toHaveProperty("llm-testhash");
128+
});
129+
130+
test("isolated deployment preserves model identifiers", () => {
131+
assertModelsPreserved(
132+
addAppNameToPreventCollision(
133+
parse(modelsComposeFile) as ComposeSpecification,
134+
"chat-app",
135+
false,
136+
),
137+
);
138+
});
139+
140+
test("raw remote compose conversion preserves models", async () => {
141+
vi.mocked(execAsyncRemote).mockResolvedValue({
142+
stdout: "services:\n dropped:\n image: alpine:latest\n",
143+
stderr: "",
144+
});
145+
146+
const converted = await addDomainToCompose(
147+
rawCompose({ serverId: "remote-server" }),
148+
[],
149+
);
150+
151+
assertModelsPreserved(converted);
152+
expect(execAsyncRemote).not.toHaveBeenCalled();
153+
154+
const written = parse(
155+
stringify(converted, { lineWidth: 1000 }),
156+
) as ComposeSpecification;
157+
assertModelsPreserved(written);
158+
});
159+
160+
test("invalid YAML still fails at parse", async () => {
161+
await expect(
162+
addDomainToCompose(rawCompose({ composeFile: "services: [" }), []),
163+
).rejects.toThrow();
164+
});

‎apps/dokploy/components/shared/code-editor.tsx‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ const dockerComposeServices = [
3030
{ label: "networks", type: "keyword", info: "Define networks" },
3131
{ label: "configs", type: "keyword", info: "Define configuration files" },
3232
{ label: "secrets", type: "keyword", info: "Define secrets" },
33+
{ label: "models", type: "keyword", info: "Define AI models" },
3334
].map((opt) => ({
3435
...opt,
3536
apply: (
@@ -83,6 +84,11 @@ const dockerComposeServiceOptions = [
8384
},
8485
{ label: "restart", type: "keyword", info: "Restart policy" },
8586
{ label: "networks", type: "keyword", info: "Networks to join" },
87+
{
88+
label: "models",
89+
type: "keyword",
90+
info: "AI models to use, referencing top-level models",
91+
},
8692
].map((opt) => ({
8793
...opt,
8894
apply: (

‎apps/dokploy/components/shared/compose-spec.json‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,16 @@
3535
"additionalProperties": false
3636
},
3737

38+
"models": {
39+
"id": "#/properties/models",
40+
"type": "object",
41+
"patternProperties": {
42+
"^[a-zA-Z0-9._-]+$": {
43+
"$ref": "#/definitions/model"
44+
}
45+
}
46+
},
47+
3848
"networks": {
3949
"id": "#/properties/networks",
4050
"type": "object",
@@ -335,6 +345,30 @@
335345
"mem_swappiness": { "type": ["integer", "string"] },
336346
"memswap_limit": { "type": ["number", "string"] },
337347
"network_mode": { "type": "string" },
348+
"models": {
349+
"oneOf": [
350+
{ "$ref": "#/definitions/list_of_strings" },
351+
{
352+
"type": "object",
353+
"patternProperties": {
354+
"^[a-zA-Z0-9._-]+$": {
355+
"oneOf": [
356+
{
357+
"type": "object",
358+
"properties": {
359+
"endpoint_var": { "type": "string" },
360+
"model_var": { "type": "string" }
361+
},
362+
"additionalProperties": false,
363+
"patternProperties": { "^x-": {} }
364+
},
365+
{ "type": "null" }
366+
]
367+
}
368+
}
369+
}
370+
]
371+
},
338372
"networks": {
339373
"oneOf": [
340374
{ "$ref": "#/definitions/list_of_strings" },
@@ -890,6 +924,23 @@
890924
"patternProperties": { "^x-": {} }
891925
},
892926

927+
"model": {
928+
"id": "#/definitions/model",
929+
"type": "object",
930+
"properties": {
931+
"name": { "type": "string" },
932+
"model": { "type": "string" },
933+
"context_size": { "type": "integer" },
934+
"runtime_flags": {
935+
"type": "array",
936+
"items": { "type": "string" }
937+
}
938+
},
939+
"required": ["model"],
940+
"additionalProperties": false,
941+
"patternProperties": { "^x-": {} }
942+
},
943+
893944
"command": {
894945
"oneOf": [
895946
{ "type": "null" },

‎packages/server/src/utils/docker/types.ts‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,7 @@ export interface ComposeSpecification {
497497
*/
498498
include?: DefinitionsInclude[];
499499
services?: PropertiesServices;
500+
models?: PropertiesModels;
500501
networks?: PropertiesNetworks;
501502
volumes?: PropertiesVolumes;
502503
secrets?: PropertiesSecrets;
@@ -648,6 +649,23 @@ export interface DefinitionsService {
648649
mem_swappiness?: number;
649650
memswap_limit?: number | string;
650651
network_mode?: string;
652+
models?:
653+
| ListOfStrings
654+
| {
655+
/**
656+
* This interface was referenced by `undefined`'s JSON-Schema definition
657+
* via the `patternProperty` "^[a-zA-Z0-9._-]+$".
658+
*/
659+
[k: string]: {
660+
endpoint_var?: string;
661+
model_var?: string;
662+
/**
663+
* This interface was referenced by `undefined`'s JSON-Schema definition
664+
* via the `patternProperty` "^x-".
665+
*/
666+
[k: string]: unknown;
667+
} | null;
668+
};
651669
networks?:
652670
| ListOfStrings
653671
| {
@@ -877,3 +895,21 @@ export interface DefinitionsConfig {
877895
*/
878896
[k: string]: unknown;
879897
}
898+
export interface PropertiesModels {
899+
[k: string]: DefinitionsModel;
900+
}
901+
/**
902+
* This interface was referenced by `PropertiesModels`'s JSON-Schema definition
903+
* via the `patternProperty` "^[a-zA-Z0-9._-]+$".
904+
*/
905+
export interface DefinitionsModel {
906+
name?: string;
907+
model: string;
908+
context_size?: number;
909+
runtime_flags?: string[];
910+
/**
911+
* This interface was referenced by `DefinitionsModel`'s JSON-Schema definition
912+
* via the `patternProperty` "^x-".
913+
*/
914+
[k: string]: unknown;
915+
}

0 commit comments

Comments
 (0)