Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

files accept hint #984

Merged
merged 3 commits into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
11 changes: 11 additions & 0 deletions packages/cli/src/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,17 @@ export async function runScriptInternal(
resolve(t.filename) === resolve(scriptId))
)
if (!script) throw new Error(`script ${scriptId} not found`)

if (script.accept) {
const exts = script.accept
.split(",")
.map((s) => s.trim())
.filter((s) => !!s)
for (const rf of resolvedFiles) {
if (!exts.some((ext) => rf.endsWith(ext))) resolvedFiles.delete(rf)
}
}

const fragment: Fragment = {
files: Array.from(resolvedFiles),
workspaceFiles,
Expand Down
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
"mammoth": "^1.9.0",
"mathjs": "^14.0.1",
"merge-descriptors": "^2.0.0",
"mime-types": "^2.1.35",
"mime": "^4.0.6",
"minimatch": "^10.0.1",
"minisearch": "^7.1.1",
"mustache": "^4.2.0",
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/mime.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Import the 'lookup' function from the 'mime-types' library and rename it to 'mimeTypesLookup'
import { lookup as mimeTypesLookup } from "mime-types"
import mime from "mime"

// Define constant MIME types for specific programming languages
export const TYPESCRIPT_MIME_TYPE = "text/x-typescript"
Expand Down Expand Up @@ -32,5 +32,5 @@
if (/\.astro$/i.test(filename)) return "text/x-astro"

// Default to lookup from 'mime-types' or return empty string
return mimeTypesLookup(filename) || ""
return mime.getType(filename) || ""

Check failure on line 35 in packages/core/src/mime.ts

View workflow job for this annotation

GitHub Actions / build

Consider using a more specific MIME type lookup function to avoid potential issues with file extensions.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider using a more specific MIME type lookup function to avoid potential issues with file extensions.

AI-generated content by pr-review-commit mime_type_lookup may be incorrect

}
1 change: 1 addition & 0 deletions packages/core/src/template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,7 @@ async function parsePromptTemplateCore(
checker.checkString("embeddingsModel")
checker.checkString("provider")
checker.checkString("responseType")
checker.checkString("accept")
checker.checkJSONSchema("responseSchema")

checker.checkString("embeddingsModel")
Expand Down
5 changes: 5 additions & 0 deletions packages/core/src/types/prompt_template.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,11 @@ interface PromptScript
*/
files?: string | string[]

/**
* A comma separated list of file extensions to accept.
*/
accept?: string

/**
* Extra variable values that can be used to configure system prompts.
*/
Expand Down
8 changes: 8 additions & 0 deletions packages/sample/genaisrc/parameters.genai.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
script({
accept: ".txt,.md,.pdf",
files: "src/*",
model: "small",
parameters: {
string: "abc",
number: 123,
Expand All @@ -20,6 +23,7 @@ script({
},
},
tests: {
files: "src/*",
vars: {
string: "abc",
number: 123,
Expand Down Expand Up @@ -67,3 +71,7 @@ if (env.vars["booleanSchema"] !== true)
throw new Error("booleanSchema parameter not set")
if (env.vars["boolean-schema"] !== true)
throw new Error("booleanSchema parameter not set")

console.log({ files: env.files.map((f) => f.filename) })
if (env.files.some((f) => f.filename.endsWith(".ts")))
throw new Error("accept not working")
24 changes: 22 additions & 2 deletions packages/web/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ import { stringify as YAMLStringify } from "yaml"
import { fenceMD } from "../../core/src/mkmd"
import { isBinaryMimeType } from "../../core/src/binary"
import { toBase64 } from "../../core/src/base64"
import { underscore } from "inflection"
import { lookupMime } from "../../core/src/mime"

const urlParams = new URLSearchParams(window.location.hash)
const apiKey = urlParams.get("api-key")
Expand Down Expand Up @@ -593,7 +595,9 @@ function JSONSchemaObjectForm(props: {
<VscodeFormContainer>
{Object.entries(properties).map(([fieldName, field]) => (
<VscodeFormGroup key={fieldName}>
<VscodeLabel>{fieldName}</VscodeLabel>
<VscodeLabel>
{underscore(fieldName).replaceAll("_", " ")}
</VscodeLabel>
<JSONSchemaSimpleTypeFormField
field={field}
value={value[fieldName]}
Expand Down Expand Up @@ -908,9 +912,25 @@ function toStringList(...token: (string | undefined | null)[]) {
return md
}

function acceptToAccept(accept: string | undefined) {
if (!accept) return undefined
const res: Record<string, string[]> = {}
const extensions = accept.split(",")
for (const ext of extensions) {
const mime = lookupMime(ext)
if (mime) {
const exts = res[mime] || (res[mime] = [])
if (!exts.includes(ext)) exts.push(ext)
}
}
return res
}

function FilesDropZone() {
const script = useScript()
const { accept } = script || {}
const { acceptedFiles, isDragActive, getRootProps, getInputProps } =
useDropzone()
useDropzone({ multiple: true, accept: acceptToAccept(accept) })
const { setImportedFiles } = useApi()

useEffect(() => setImportedFiles(acceptedFiles.slice()), [acceptedFiles])
Expand Down
7 changes: 6 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7780,7 +7780,7 @@ [email protected]:
resolved "https://registry.yarnpkg.com/mime-db/-/mime-db-1.52.0.tgz#bbabcdc02859f4987301c856e3387ce5ec43bf70"
integrity sha512-sPU4uV7dYlvtWJxwwxHD0PuihVNiE7TyAbQ5SWxDCB9mUYvOgroQOwYQQOKPJ8CIbE+1ETVlOoK1UC2nU3gYvg==

mime-types@^2.1.12, mime-types@^2.1.35, mime-types@~2.1.19:
mime-types@^2.1.12, mime-types@~2.1.19:
version "2.1.35"
resolved "https://registry.yarnpkg.com/mime-types/-/mime-types-2.1.35.tgz#381a871b62a734450660ae3deee44813f70d959a"
integrity sha512-ZDY+bPm5zTTF+YpCrAU9nK0UgICYPT0QtT1NZWFv4s++TNkcgVaT0g6+4R2uI4MjQjzysHB1zxuWL50hzaeXiw==
Expand All @@ -7797,6 +7797,11 @@ mime@^1.3.4:
resolved "https://registry.yarnpkg.com/mime/-/mime-1.6.0.tgz#32cd9e5c64553bd58d19a568af452acff04981b1"
integrity sha512-x0Vn8spI+wuJ1O6S7gnbaQg8Pxh4NNHb7KSINmEWKiPE4RKOplvijn+NkmYmmRgP68mc70j2EbeTFRsrswaQeg==

mime@^4.0.6:
version "4.0.6"
resolved "https://registry.yarnpkg.com/mime/-/mime-4.0.6.tgz#ca83bec0bcf2a02353d0e02da99be05603d04839"
integrity sha512-4rGt7rvQHBbaSOF9POGkk1ocRP16Md1x36Xma8sz8h8/vfCUI2OtEIeCqe4Ofes853x4xDoPiFLIT47J5fI/7A==

mimic-response@^3.1.0:
version "3.1.0"
resolved "https://registry.yarnpkg.com/mimic-response/-/mimic-response-3.1.0.tgz#2d1d59af9c1b129815accc2c46a022a5ce1fa3c9"
Expand Down
Loading