Skip to content

Commit

Permalink
Fix issues with openai key, add npm prompt (#19)
Browse files Browse the repository at this point in the history
* Fix issues with openai key, add npm prompt

* Switch to environment var for openai secret

* Update entrypoint for `npm` extractor
  • Loading branch information
ColinMcNeil authored Sep 3, 2024
1 parent 942593c commit b86d804
Show file tree
Hide file tree
Showing 8 changed files with 116 additions and 12 deletions.
13 changes: 13 additions & 0 deletions extractors/npm/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
FROM alpine:latest

RUN apk add --no-cache jq bash fd

VOLUME [ "/project" ]

WORKDIR /project

COPY entrypoint.sh /app/entrypoint.sh
RUN chmod +x /app/entrypoint.sh

ENTRYPOINT ["/app/entrypoint.sh"]

43 changes: 43 additions & 0 deletions extractors/npm/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#!/bin/bash

OUTPUT_JSON="{}"

PACKAGE_JSONS=$(fd -e package.json)

TOTAL_PACKAGE_JSONS=$(echo "$PACKAGE_JSONS" | wc -l)

OUTPUT_JSON=$(echo "$OUTPUT_JSON" | jq -c ".total_package_json_files = $TOTAL_PACKAGE_JSONS")
# If no package.json files are found, exit
if [ "$TOTAL_PACKAGE_JSONS" -eq 0 ]; then
echo "No package.json files found in the current directory"
exit 1
fi

# If root level package.json
if [ -f "package.json" ]; then
OUTPUT_JSON=$(echo "$OUTPUT_JSON" | jq -c ".root_package_json = $(cat package.json)")

fi

# If has node_modules
if [ -d "node_modules" ]; then
ARG="$(ls node_modules | wc -l) directories"
OUTPUT_JSON=$(echo "$OUTPUT_JSON" | jq --arg ARG "$ARG" '.node_modules = $ARG')
fi

# If has package-lock.json
if [ -f "package-lock.json" ]; then
OUTPUT_JSON=$(echo "$OUTPUT_JSON" | jq -c ".package_lock = true")
fi

# If has yarn.lock
if [ -f "yarn.lock" ]; then
OUTPUT_JSON=$(echo "$OUTPUT_JSON" | jq -c ".yarn_lock = true")
fi

# If has pnpm-lock.yaml
if [ -f "pnpm-lock.yaml" ]; then
OUTPUT_JSON=$(echo "$OUTPUT_JSON" | jq -c ".pnpm_lock = true")
fi

echo -e "{\"npm\": $OUTPUT_JSON }"
2 changes: 2 additions & 0 deletions extractors/registry.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,7 @@ registry:
command:
- -json
output-handler: linguist
- name: npm
image: vonwig/npm:latest


11 changes: 11 additions & 0 deletions prompts/npm/1_system_prompt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
You are and assistant that is an expert at npm projects.

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.

Inside of a package.json file, the source of truth for package manager should be the key `packageManager` but it is rarely defined.

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.

If there is one lockfile, tell them what package manager is used for the project.

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.
23 changes: 23 additions & 0 deletions prompts/npm/2_user_prompt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
The project has {{npm.total_package_json_files}} package.json files.

The root package.json file is:

```json
{{npm.root_package_json}}
```

There are {{npm.node_modules}} currently installed in the project.

The package lock files are:

```json
{{npm.package_lock}}
```

```json
{{npm.yarn_lock}}
```

```json
{{npm.pnpm_lock}}
```
6 changes: 6 additions & 0 deletions prompts/npm/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
extractors:
- image: vonwig/npm:latest
---

Responds with a summary of the NPM project.
25 changes: 15 additions & 10 deletions src/extension/ui/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import Projects from './components/Projects';
import Prompts from './components/Prompts';
import RunOutput from './components/RunOutput';
import Runner from './components/Runner'; // Added this import
import { run } from 'node:test';
import { ExecResult } from '@docker/extension-api-client-types/dist/v0';

const client = createDockerDesktopClient();

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

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

let pullImagePromise: Promise<ExecResult> | undefined;

export function App() {
const [projects, setProjects] = React.useState<string[]>(localStorage.getItem('projects') ? JSON.parse(localStorage.getItem('projects')!) : []);
const [selectedProject, setSelectedProject] = React.useState<string | null>(localStorage.getItem('selectedProject') || null);
Expand All @@ -84,15 +88,7 @@ export function App() {
useEffect(() => {

try {
client.docker.cli.exec("pull", ["vonwig/function_write_files"]).then(() => {
client.docker.cli.exec("run", [
"-v",
"openai_key:/root",
"--workdir", "/root",
"vonwig/function_write_files",
`'` + JSON.stringify({ files: [{ path: ".openai-api-key", content: openAIKey, executable: false }] }) + `'`
]);
});
pullImagePromise = client.docker.cli.exec("pull", ["vonwig/function_write_files"])
client.docker.cli.exec("pull", ["vonwig/prompts"], {
stream: {
onOutput: ({ stdout, stderr }) => {
Expand Down Expand Up @@ -168,7 +164,16 @@ export function App() {

const startPrompt = async () => {
track('DockerPromptsStartPrompt');
runOutput.updateOutput({ method: 'message', params: { debug: 'Pulling images' } })

await pullImagePromise

client.docker.cli.exec("run", [
"-v",
"openai_key:/secret",
"--workdir", "/secret",
"vonwig/function_write_files",
`'` + JSON.stringify({ files: [{ path: ".openai-api-key", content: openAIKey, executable: false }] }) + `'`
]);

runOutput.updateOutput({ method: 'message', params: { debug: 'Running prompts...' } })
const args = getRunArgs(selectedPrompt!, selectedProject!, "", client.host.platform)
Expand Down
5 changes: 3 additions & 2 deletions src/extension/ui/src/args.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ export const getRunArgs = (promptRef: string, projectDir: string, username: stri
const baseArgs: string[] = [
'--rm',
'-v', '/var/run/docker.sock:/var/run/docker.sock',
'-v', 'openai_key:/root',
'--mount', 'type=volume,source=docker-prompts,target=/prompts'
'-v', 'openai_key:/secret',
'--mount', 'type=volume,source=docker-prompts,target=/prompts',
'-e', 'OPENAI_API_KEY_LOCATION=/secret'
];

const runArgs: string[] = render ? [] : [
Expand Down

0 comments on commit b86d804

Please sign in to comment.