chore: generate
This commit is contained in:
@@ -24,7 +24,11 @@ export function cortexFetch(upstream: FetchLike = fetch) {
|
|||||||
if (!response.ok && response.status === 400) {
|
if (!response.ok && response.status === 400) {
|
||||||
try {
|
try {
|
||||||
const errorData = (await response.clone().json()) as Record<string, unknown>
|
const errorData = (await response.clone().json()) as Record<string, unknown>
|
||||||
if (String(errorData.message || errorData.error || "").toLowerCase().includes("conversation complete")) {
|
if (
|
||||||
|
String(errorData.message || errorData.error || "")
|
||||||
|
.toLowerCase()
|
||||||
|
.includes("conversation complete")
|
||||||
|
) {
|
||||||
return new Response(
|
return new Response(
|
||||||
JSON.stringify({ choices: [{ finish_reason: "stop", message: { content: "", role: "assistant" } }] }),
|
JSON.stringify({ choices: [{ finish_reason: "stop", message: { content: "", role: "assistant" } }] }),
|
||||||
{ status: 200, headers: new Headers({ "content-type": "application/json" }) },
|
{ status: 200, headers: new Headers({ "content-type": "application/json" }) },
|
||||||
@@ -45,7 +49,9 @@ export function cortexFetch(upstream: FetchLike = fetch) {
|
|||||||
ctrl.close()
|
ctrl.close()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
ctrl.enqueue(encoder.encode(decoder.decode(value, { stream: true }).replace(/"role"\s*:\s*""/g, '"role":"assistant"')))
|
ctrl.enqueue(
|
||||||
|
encoder.encode(decoder.decode(value, { stream: true }).replace(/"role"\s*:\s*""/g, '"role":"assistant"')),
|
||||||
|
)
|
||||||
},
|
},
|
||||||
cancel() {
|
cancel() {
|
||||||
reader.cancel()
|
reader.cancel()
|
||||||
@@ -65,8 +71,7 @@ export const SnowflakeCortexPlugin = PluginV2.define({
|
|||||||
"aisdk.sdk": Effect.fn(function* (evt) {
|
"aisdk.sdk": Effect.fn(function* (evt) {
|
||||||
if (evt.model.providerID !== ProviderV2.ID.make("snowflake-cortex")) return
|
if (evt.model.providerID !== ProviderV2.ID.make("snowflake-cortex")) return
|
||||||
const pat =
|
const pat =
|
||||||
process.env.SNOWFLAKE_CORTEX_PAT ??
|
process.env.SNOWFLAKE_CORTEX_PAT ?? (typeof evt.options.apiKey === "string" ? evt.options.apiKey : undefined)
|
||||||
(typeof evt.options.apiKey === "string" ? evt.options.apiKey : undefined)
|
|
||||||
const upstream = typeof evt.options.fetch === "function" ? (evt.options.fetch as FetchLike) : undefined
|
const upstream = typeof evt.options.fetch === "function" ? (evt.options.fetch as FetchLike) : undefined
|
||||||
if (evt.options.includeUsage !== false) evt.options.includeUsage = true
|
if (evt.options.includeUsage !== false) evt.options.includeUsage = true
|
||||||
const mod = yield* Effect.promise(() => import("@ai-sdk/openai-compatible"))
|
const mod = yield* Effect.promise(() => import("@ai-sdk/openai-compatible"))
|
||||||
|
|||||||
@@ -173,10 +173,18 @@ describe("cortexFetch", () => {
|
|||||||
bun_it("rewrites role:'' to role:'assistant' in streaming SSE chunks", async () => {
|
bun_it("rewrites role:'' to role:'assistant' in streaming SSE chunks", async () => {
|
||||||
const chunk = `data: {"choices":[{"delta":{"role":"","content":"Hi"},"index":0}]}\n\n`
|
const chunk = `data: {"choices":[{"delta":{"role":"","content":"Hi"},"index":0}]}\n\n`
|
||||||
const upstream: FetchLike = async () =>
|
const upstream: FetchLike = async () =>
|
||||||
new Response(new ReadableStream({ start: (ctrl) => { ctrl.enqueue(new TextEncoder().encode(chunk)); ctrl.close() } }), {
|
new Response(
|
||||||
status: 200,
|
new ReadableStream({
|
||||||
headers: { "content-type": "text/event-stream" },
|
start: (ctrl) => {
|
||||||
})
|
ctrl.enqueue(new TextEncoder().encode(chunk))
|
||||||
|
ctrl.close()
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
{
|
||||||
|
status: 200,
|
||||||
|
headers: { "content-type": "text/event-stream" },
|
||||||
|
},
|
||||||
|
)
|
||||||
const response = await cortexFetch(upstream)("https://test", {})
|
const response = await cortexFetch(upstream)("https://test", {})
|
||||||
const text = await response.text()
|
const text = await response.text()
|
||||||
expect(text).toContain('"role":"assistant"')
|
expect(text).toContain('"role":"assistant"')
|
||||||
|
|||||||
@@ -860,10 +860,7 @@ function custom(dep: CustomDep): Record<string, CustomLoader> {
|
|||||||
(auth?.type === "api" ? auth.metadata?.account : undefined) ??
|
(auth?.type === "api" ? auth.metadata?.account : undefined) ??
|
||||||
input.options?.account
|
input.options?.account
|
||||||
|
|
||||||
const pat =
|
const pat = env["SNOWFLAKE_CORTEX_PAT"] ?? (auth?.type === "api" ? auth.key : undefined) ?? input.options?.apiKey
|
||||||
env["SNOWFLAKE_CORTEX_PAT"] ??
|
|
||||||
(auth?.type === "api" ? auth.key : undefined) ??
|
|
||||||
input.options?.apiKey
|
|
||||||
|
|
||||||
if (!account || !pat) {
|
if (!account || !pat) {
|
||||||
const missing = [!account && "SNOWFLAKE_ACCOUNT", !pat && "SNOWFLAKE_CORTEX_PAT"].filter(Boolean).join(", ")
|
const missing = [!account && "SNOWFLAKE_ACCOUNT", !pat && "SNOWFLAKE_CORTEX_PAT"].filter(Boolean).join(", ")
|
||||||
@@ -905,7 +902,9 @@ function custom(dep: CustomDep): Record<string, CustomLoader> {
|
|||||||
const errorMessage = String(errorData.message || errorData.error || "")
|
const errorMessage = String(errorData.message || errorData.error || "")
|
||||||
if (errorMessage.toLowerCase().includes("conversation complete")) {
|
if (errorMessage.toLowerCase().includes("conversation complete")) {
|
||||||
return new Response(
|
return new Response(
|
||||||
JSON.stringify({ choices: [{ finish_reason: "stop", message: { content: "", role: "assistant" } }] }),
|
JSON.stringify({
|
||||||
|
choices: [{ finish_reason: "stop", message: { content: "", role: "assistant" } }],
|
||||||
|
}),
|
||||||
{ status: 200, headers: new Headers({ "content-type": "application/json" }) },
|
{ status: 200, headers: new Headers({ "content-type": "application/json" }) },
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user