Skip to content
This repository was archived by the owner on Apr 14, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions dagger-programmer/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/sdk/** linguist-generated
3 changes: 3 additions & 0 deletions dagger-programmer/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/sdk
/.venv
/**/__pycache__
13 changes: 13 additions & 0 deletions dagger-programmer/dagger.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "dagger-programmer",
"engineVersion": "v0.17.0-llm.2",
"sdk": {
"source": "python"
},
"dependencies": [
{
"name": "moduleWorkspace",
"source": "moduleWorkspace"
}
]
}
1 change: 1 addition & 0 deletions dagger-programmer/moduleWorkspace/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/sdk/** linguist-generated
3 changes: 3 additions & 0 deletions dagger-programmer/moduleWorkspace/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/sdk
/**/node_modules/**
/**/.pnpm-store/**
7 changes: 7 additions & 0 deletions dagger-programmer/moduleWorkspace/dagger.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "moduleWorkspace",
"engineVersion": "v0.17.0-llm.2",
"sdk": {
"source": "typescript"
}
}
8 changes: 8 additions & 0 deletions dagger-programmer/moduleWorkspace/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"type": "module",
"dependencies": {
"typescript": "^5.5.4",
"@dagger.io/dagger": "./sdk"
},
"packageManager": "yarn@1.22.22+sha512.a6b2f7906b721bba3d67d4aff083df04dad64c399707841b7acf00f6b133b7ac24255f2652fa22ae3534329dc6180534e98d17432037ff6fd140556e2bb3137e"
}
158 changes: 158 additions & 0 deletions dagger-programmer/moduleWorkspace/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
/**
* A generated module for DagWorkspace functions
*/
import {
Container,
dag,
Directory,
object,
func,
ReturnType,
} from "@dagger.io/dagger";

@object()
export class ModuleWorkspace {
/*
* The workspace directory
*/
@func()
workspace: Directory;
sdk: string;
name: string;
daggerVersion: string;

constructor(
/*
* module SDK
*/
sdk: string,
/*
* module name
*/
name: string,
/*
* Dagger Version
*/
daggerVersion = "latest",
/*
* Workspace directory
*/
workspace?: Directory,
) {
this.sdk = sdk;
this.name = name;
this.daggerVersion = daggerVersion;
this.workspace =
workspace ??
dag.directory().withNewFile(this.sdkFilePath(), "EMPTY FILE");
}
/**
* Writes the module code to the workspace
* @param content The updated module code
*/
@func()
write(content: string): ModuleWorkspace {
// NOTE: constraining this to the default file path for now to keep things simple
// To develop module examples or more complicated modules, this will need to be unlocked
this.workspace = this.workspace.withNewFile(this.sdkFilePath(), content);
return this;
}

/**
* Reads the module code from the workspace
*/
@func()
async read(): Promise<string> {
return await this.workspace.file(this.sdkFilePath()).contents();
}

/**
* Test if a module works by outputting the module functions
*/
@func()
async test(): Promise<string> {
const dagger = this.daggerCommand();
const functions = dagger.withExec(["/usr/local/bin/dagger", "functions"], {
experimentalPrivilegedNesting: true,
expect: ReturnType.Any,
});
if ((await functions.exitCode()) != 0) {
return await functions.stderr();
}
return await functions.stdout();
}

/**
* Get reference information for developing with a Dagger SDK
* @param sdk Dagger SDK language to get reference for
*/
@func()
async getReference(sdk: string): Promise<string> {
const daggerDocs = dag
.git("https://github.com/dagger/dagger")
.head()
.tree()
.directory("docs/current_docs/");

// Messing around with changing which snippets are included. Dont want to overload the LLM with too much info
const snippets: { [key: string]: string } = {
// "multi stage build": "/cookbook/snippets/builds/multi-stage-build",
// "cache volumes": "/cookbook/snippets/builds/cache",
// "secret variables": "/cookbook/snippets/secret-variable",
"bind services to containers": "/api/snippets/services/bind-services",
// "expose services to the host": "/api/snippets/services/expose-dagger-services-to-host",
};

// FIXME: ideally cookbook snippets would follow module structure too
const snippetSdkPaths: { [key: string]: string } = {
go: "main.go",
python: "main.py",
typescript: "index.ts",
php: "src/MyModule.php",
java: "src/main/java/io/dagger/modules/mymodule/MyModule.java",
};

// TODO: also include full sdk reference?
let reference = `Reference for using Dagger with the ${sdk} SDK\n`;
reference += `The relevant code for a ${sdk} SDK module is at "${this.sdkFilePath(sdk)}"\n`;
for (const snippet in snippets) {
const code = await daggerDocs
.file(`${snippets[snippet]}/${sdk}/${snippetSdkPaths[sdk]}`)
.contents();
reference += `\n${snippet}:\n<code>\n${code}\n</code>\n`;
}
return reference;
}

// Helper to get the correct code path for a given SDK
sdkFilePath(sdk?: string): string {
sdk = sdk ?? this.sdk;
const defaultFilePaths: { [key: string]: string } = {
go: "main.go",
python: `src/${this.name}/main.py`,
typescript: "src/index.ts",
php: `src/${this.name}.php`,
java: `src/main/java/io/dagger/modules/${this.name.toLowerCase()}/${this.name}.java`,
};
return defaultFilePaths[this.sdk];
}

// Helper to get a container that can execute dagger commands on our module
daggerCommand(): Container {
return dag
.container()
.from("alpine")
.withExec(["apk", "add", "curl"])
.withEnvVariable("DAGGER_VERSION", this.daggerVersion)
.withExec([
"sh",
"-c",
"curl -fsSL https://dl.dagger.io/dagger/install.sh | BIN_DIR=/usr/local/bin sh",
])
.withWorkdir("/mod")
.withExec(["dagger", "init", "--name", this.name, "--sdk", this.sdk], {
experimentalPrivilegedNesting: true,
})
.withFile(this.sdkFilePath(), this.workspace.file(this.sdkFilePath()));
}
}
11 changes: 11 additions & 0 deletions dagger-programmer/moduleWorkspace/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"compilerOptions": {
"target": "ES2022",
"moduleResolution": "Node",
"experimentalDecorators": true,
"paths": {
"@dagger.io/dagger": ["./sdk/src"],
"@dagger.io/dagger/telemetry": ["./sdk/src/telemetry"]
}
}
}
Loading