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

Refactor fetchText into global function and remove from PromptContext #699

Merged
merged 1 commit into from
Sep 9, 2024
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
9 changes: 0 additions & 9 deletions docs/genaisrc/genaiscript.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 0 additions & 9 deletions genaisrc/genaiscript.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 0 additions & 9 deletions packages/core/src/genaisrc/genaiscript.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 44 additions & 0 deletions packages/core/src/globals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
import { JSONLStringify, JSONLTryParse } from "./jsonl"
import { HTMLTablesToJSON, HTMLToMarkdown, HTMLToText } from "./html"
import { CancelError } from "./error"
import { createFetch } from "./fetch"
import { readText } from "./fs"
import { logVerbose } from "./util"

export function resolveGlobal(): any {
if (typeof window !== "undefined")
Expand Down Expand Up @@ -66,4 +69,45 @@
glb.cancel = (reason?: string) => {
throw new CancelError(reason || "user cancelled")
}
glb.fetchText = async (
urlOrFile: string | WorkspaceFile,
fetchOptions?: FetchTextOptions
) => {
if (typeof urlOrFile === "string") {
urlOrFile = {
filename: urlOrFile,
content: "",
}
}
const url = urlOrFile.filename
let ok = false
let status = 404
let text: string
if (/^https?:\/\//i.test(url)) {
const fetch = await createFetch()
const resp = await fetch(url, fetchOptions)
ok = resp.ok
status = resp.status
if (ok) text = await resp.text()
} else {
try {
text = await readText("workspace://" + url)
ok = true
} catch (e) {
logVerbose(e)
ok = false
status = 404
}

Check failure on line 100 in packages/core/src/globals.ts

View workflow job for this annotation

GitHub Actions / build

The promise returned by `readText` function is not properly handled. If the promise is rejected, it will lead to an unhandled promise rejection. Consider adding error handling for this promise. 😊
pelikhan marked this conversation as resolved.
Show resolved Hide resolved
}
const file: WorkspaceFile = {
filename: urlOrFile.filename,
content: text,
}
return {
ok,
status,
text,
file,
}
}

Check failure on line 112 in packages/core/src/globals.ts

View workflow job for this annotation

GitHub Actions / build

The function `fetchText` does not handle errors that may occur during the fetch operation. Consider adding a try-catch block to handle potential errors and improve the robustness of your code. 😊
pelikhan marked this conversation as resolved.
Show resolved Hide resolved
}
48 changes: 1 addition & 47 deletions packages/core/src/promptcontext.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,13 @@
import {

Check failure on line 1 in packages/core/src/promptcontext.ts

View workflow job for this annotation

GitHub Actions / build

The `fetchText` function has been removed from the `PromptContext` but it's not clear if it has been replaced or if its removal could impact other parts of the codebase. Please ensure that this change does not affect the functionality of the application. 😊
executeChatSession,
mergeGenerationOptions,
tracePromptResult,
} from "./chat"
import { host } from "./host"
import {
HTMLEscape,
arrayify,
dotGenaiscriptPath,
logVerbose,
sha256string,
} from "./util"
import { HTMLEscape, arrayify, dotGenaiscriptPath, sha256string } from "./util"
import { runtimeHost } from "./host"
import { MarkdownTrace } from "./trace"
import { createParsers } from "./parsers"
import { readText } from "./fs"
import {
PromptNode,
appendChild,
Expand All @@ -30,7 +23,6 @@
createChatGenerationContext,
} from "./runpromptcontext"
import { isCancelError, NotSupportedError, serializeError } from "./error"
import { createFetch } from "./fetch"
import { GenerationOptions } from "./generation"
import { fuzzSearch } from "./fuzzsearch"
import { parseModelIdentifier } from "./models"
Expand Down Expand Up @@ -360,44 +352,6 @@
trace.endDetails()
}
},
fetchText: async (urlOrFile, fetchOptions) => {
if (typeof urlOrFile === "string") {
urlOrFile = {
filename: urlOrFile,
content: "",
}
}
const url = urlOrFile.filename
let ok = false
let status = 404
let text: string
if (/^https?:\/\//i.test(url)) {
const fetch = await createFetch({ cancellationToken })
const resp = await fetch(url, fetchOptions)
ok = resp.ok
status = resp.status
if (ok) text = await resp.text()
} else {
try {
text = await readText("workspace://" + url)
ok = true
} catch (e) {
logVerbose(e)
ok = false
status = 404
}
}
const file: WorkspaceFile = {
filename: urlOrFile.filename,
content: text,
}
return {
ok,
status,
text,
file,
}
},
}
pelikhan marked this conversation as resolved.
Show resolved Hide resolved
env.generator = ctx
ctx.env = Object.freeze(env)
Expand Down
9 changes: 0 additions & 9 deletions packages/core/src/types/prompt_template.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2267,15 +2267,6 @@ interface PromptContext extends ChatGenerationContext {
generator: string | PromptGenerator,
options?: PromptGeneratorOptions
): Promise<RunPromptResult>
fetchText(
urlOrFile: string | WorkspaceFile,
options?: FetchTextOptions
): Promise<{
ok: boolean
status: number
text?: string
file?: WorkspaceFile
}>
env: ExpansionVariables
path: Path
parsers: Parsers
Expand Down
9 changes: 0 additions & 9 deletions packages/sample/genaisrc/genaiscript.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 0 additions & 9 deletions packages/sample/genaisrc/node/genaiscript.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 0 additions & 9 deletions packages/sample/genaisrc/python/genaiscript.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 0 additions & 9 deletions packages/sample/genaisrc/style/genaiscript.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 0 additions & 9 deletions packages/sample/src/aici/genaiscript.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 0 additions & 9 deletions packages/sample/src/errors/genaiscript.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 0 additions & 9 deletions packages/sample/src/genaiscript.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 0 additions & 9 deletions packages/sample/src/makecode/genaiscript.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 0 additions & 9 deletions packages/sample/src/tla/genaiscript.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 0 additions & 9 deletions packages/sample/src/vision/genaiscript.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 0 additions & 9 deletions slides/genaisrc/genaiscript.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading