Skip to content

Commit b86d804

Browse files
authored
Fix issues with openai key, add npm prompt (#19)
* Fix issues with openai key, add npm prompt * Switch to environment var for openai secret * Update entrypoint for `npm` extractor
1 parent 942593c commit b86d804

File tree

8 files changed

+116
-12
lines changed

8 files changed

+116
-12
lines changed

extractors/npm/Dockerfile

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
FROM alpine:latest
2+
3+
RUN apk add --no-cache jq bash fd
4+
5+
VOLUME [ "/project" ]
6+
7+
WORKDIR /project
8+
9+
COPY entrypoint.sh /app/entrypoint.sh
10+
RUN chmod +x /app/entrypoint.sh
11+
12+
ENTRYPOINT ["/app/entrypoint.sh"]
13+

extractors/npm/entrypoint.sh

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#!/bin/bash
2+
3+
OUTPUT_JSON="{}"
4+
5+
PACKAGE_JSONS=$(fd -e package.json)
6+
7+
TOTAL_PACKAGE_JSONS=$(echo "$PACKAGE_JSONS" | wc -l)
8+
9+
OUTPUT_JSON=$(echo "$OUTPUT_JSON" | jq -c ".total_package_json_files = $TOTAL_PACKAGE_JSONS")
10+
# If no package.json files are found, exit
11+
if [ "$TOTAL_PACKAGE_JSONS" -eq 0 ]; then
12+
echo "No package.json files found in the current directory"
13+
exit 1
14+
fi
15+
16+
# If root level package.json
17+
if [ -f "package.json" ]; then
18+
OUTPUT_JSON=$(echo "$OUTPUT_JSON" | jq -c ".root_package_json = $(cat package.json)")
19+
20+
fi
21+
22+
# If has node_modules
23+
if [ -d "node_modules" ]; then
24+
ARG="$(ls node_modules | wc -l) directories"
25+
OUTPUT_JSON=$(echo "$OUTPUT_JSON" | jq --arg ARG "$ARG" '.node_modules = $ARG')
26+
fi
27+
28+
# If has package-lock.json
29+
if [ -f "package-lock.json" ]; then
30+
OUTPUT_JSON=$(echo "$OUTPUT_JSON" | jq -c ".package_lock = true")
31+
fi
32+
33+
# If has yarn.lock
34+
if [ -f "yarn.lock" ]; then
35+
OUTPUT_JSON=$(echo "$OUTPUT_JSON" | jq -c ".yarn_lock = true")
36+
fi
37+
38+
# If has pnpm-lock.yaml
39+
if [ -f "pnpm-lock.yaml" ]; then
40+
OUTPUT_JSON=$(echo "$OUTPUT_JSON" | jq -c ".pnpm_lock = true")
41+
fi
42+
43+
echo -e "{\"npm\": $OUTPUT_JSON }"

extractors/registry.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,7 @@ registry:
1313
command:
1414
- -json
1515
output-handler: linguist
16+
- name: npm
17+
image: vonwig/npm:latest
1618

1719

prompts/npm/1_system_prompt.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
You are and assistant that is an expert at npm projects.
2+
3+
An NPM project will be described to you. It will include information about the project, the package.json files, the node_modules directory, and the package lock files.
4+
5+
Inside of a package.json file, the source of truth for package manager should be the key `packageManager` but it is rarely defined.
6+
7+
If there are multiple lockfiles, you need to respond warning that there are multiple lockfiles. Recommend running the prompt `github.com:docker/labs-ai-tools-for-devs?path=prompts/choose-package-manager` to choose the correct package manager.
8+
9+
If there is one lockfile, tell them what package manager is used for the project.
10+
11+
Otherwise, simply summarize the project. Be brief, but consider the package.json provided, and what the typical developer would want to know about the project.

prompts/npm/2_user_prompt.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
The project has {{npm.total_package_json_files}} package.json files.
2+
3+
The root package.json file is:
4+
5+
```json
6+
{{npm.root_package_json}}
7+
```
8+
9+
There are {{npm.node_modules}} currently installed in the project.
10+
11+
The package lock files are:
12+
13+
```json
14+
{{npm.package_lock}}
15+
```
16+
17+
```json
18+
{{npm.yarn_lock}}
19+
```
20+
21+
```json
22+
{{npm.pnpm_lock}}
23+
```

prompts/npm/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
extractors:
3+
- image: vonwig/npm:latest
4+
---
5+
6+
Responds with a summary of the NPM project.

src/extension/ui/src/App.tsx

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import Projects from './components/Projects';
77
import Prompts from './components/Prompts';
88
import RunOutput from './components/RunOutput';
99
import Runner from './components/Runner'; // Added this import
10+
import { run } from 'node:test';
11+
import { ExecResult } from '@docker/extension-api-client-types/dist/v0';
1012

1113
const client = createDockerDesktopClient();
1214

@@ -62,6 +64,8 @@ const debounce = (fn: Function, ms: number) => {
6264

6365
const debouncedToastSuccess = debounce(client.desktopUI.toast.success, 1000)
6466

67+
let pullImagePromise: Promise<ExecResult> | undefined;
68+
6569
export function App() {
6670
const [projects, setProjects] = React.useState<string[]>(localStorage.getItem('projects') ? JSON.parse(localStorage.getItem('projects')!) : []);
6771
const [selectedProject, setSelectedProject] = React.useState<string | null>(localStorage.getItem('selectedProject') || null);
@@ -84,15 +88,7 @@ export function App() {
8488
useEffect(() => {
8589

8690
try {
87-
client.docker.cli.exec("pull", ["vonwig/function_write_files"]).then(() => {
88-
client.docker.cli.exec("run", [
89-
"-v",
90-
"openai_key:/root",
91-
"--workdir", "/root",
92-
"vonwig/function_write_files",
93-
`'` + JSON.stringify({ files: [{ path: ".openai-api-key", content: openAIKey, executable: false }] }) + `'`
94-
]);
95-
});
91+
pullImagePromise = client.docker.cli.exec("pull", ["vonwig/function_write_files"])
9692
client.docker.cli.exec("pull", ["vonwig/prompts"], {
9793
stream: {
9894
onOutput: ({ stdout, stderr }) => {
@@ -168,7 +164,16 @@ export function App() {
168164

169165
const startPrompt = async () => {
170166
track('DockerPromptsStartPrompt');
171-
runOutput.updateOutput({ method: 'message', params: { debug: 'Pulling images' } })
167+
168+
await pullImagePromise
169+
170+
client.docker.cli.exec("run", [
171+
"-v",
172+
"openai_key:/secret",
173+
"--workdir", "/secret",
174+
"vonwig/function_write_files",
175+
`'` + JSON.stringify({ files: [{ path: ".openai-api-key", content: openAIKey, executable: false }] }) + `'`
176+
]);
172177

173178
runOutput.updateOutput({ method: 'message', params: { debug: 'Running prompts...' } })
174179
const args = getRunArgs(selectedPrompt!, selectedProject!, "", client.host.platform)

src/extension/ui/src/args.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@ export const getRunArgs = (promptRef: string, projectDir: string, username: stri
1414
const baseArgs: string[] = [
1515
'--rm',
1616
'-v', '/var/run/docker.sock:/var/run/docker.sock',
17-
'-v', 'openai_key:/root',
18-
'--mount', 'type=volume,source=docker-prompts,target=/prompts'
17+
'-v', 'openai_key:/secret',
18+
'--mount', 'type=volume,source=docker-prompts,target=/prompts',
19+
'-e', 'OPENAI_API_KEY_LOCATION=/secret'
1920
];
2021

2122
const runArgs: string[] = render ? [] : [

0 commit comments

Comments
 (0)