Skip to content

Commit

Permalink
refactor: ♻️ improve handling of options and imports
Browse files Browse the repository at this point in the history
  • Loading branch information
pelikhan committed Dec 12, 2024
1 parent 014e886 commit 4057f6f
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 8 deletions.
6 changes: 5 additions & 1 deletion packages/cli/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ export async function run(
result?: GenerationResult
}> {
if (!scriptId) throw new Error("scriptId is required")
if (typeof files === "string") files = [files]
if (typeof options?.vars === "object")
options.vars = Object.entries(options.vars).map(
([key, value]) => `${key}=${value}`
)

const { env, signal, ...rest } = options || {}
const workerData = {
Expand All @@ -47,7 +52,6 @@ export async function run(
files: files || [],
options: rest,
}
if (typeof files === "string") files = [files]
const filename =
typeof __filename === "undefined"
? join(dirname(fileURLToPath(import.meta.url)), "genaiscript.cjs") // ignore esbuild warning
Expand Down
4 changes: 0 additions & 4 deletions packages/cli/src/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,10 +230,6 @@ export async function runScriptInternal(
}

logInfo(`genaiscript: ${scriptId}`)
if (!isQuiet)
Object.entries(runtimeHost.modelAliases).forEach(([key, value]) =>
logVerbose(` ${key}: ${value.model}`)
)

if (out) {
if (removeOut) await emptyDir(out)
Expand Down
26 changes: 23 additions & 3 deletions packages/sample/src/api.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,27 @@
console.log(`loading cli`)
const api = await import("../../cli/built/genaiscript.cjs")
const api = await import("genaiscript/api")

console.log(api)
const res = await api.run("poem")
{
const res = await api.run("poem")
if (res.error) throw new Error(res.error)
await api.run("summarize", "packages/sample/src/rag/markdown.md")
await api.run("parameters", [], {
vars: {
string: "abc",
number: 123,
boolean: true,
stringSchema: "efg",
numberSchema: 456,
booleanSchema: true,
},
})
}

console.log(res)
process.chdir("..")
{
await api.run(
"summarize",
"genaiscript/packages/sample/src/rag/markdown.md"
)
}

0 comments on commit 4057f6f

Please sign in to comment.