mirror of
https://github.com/geoffsee/open-gsio.git
synced 2025-09-08 22:56:46 +00:00
Refactor: Relocate SDK files to lib
and update imports
Moved all SDK files from the `sdk` directory to the `lib` directory to better align with project structure. Updated all associated import paths across the codebase to reflect this change. Removed unused or commented-out code in `SiteCoordinator.js` for better clarity and maintainability.
This commit is contained in:

committed by
Geoff Seemueller

parent
fc22278b58
commit
335e8eff11
104
workers/site/lib/handleStreamData.ts
Normal file
104
workers/site/lib/handleStreamData.ts
Normal file
@@ -0,0 +1,104 @@
|
||||
interface StreamChoice {
|
||||
index?: number;
|
||||
delta: {
|
||||
content: string;
|
||||
};
|
||||
logprobs: null;
|
||||
finish_reason: string | null;
|
||||
}
|
||||
|
||||
interface StreamResponse {
|
||||
type: string;
|
||||
data: {
|
||||
choices?: StreamChoice[];
|
||||
delta?: {
|
||||
text?: string;
|
||||
};
|
||||
type?: string;
|
||||
content_block?: {
|
||||
type: string;
|
||||
text: string;
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
const handleStreamData = (
|
||||
controller: ReadableStreamDefaultController,
|
||||
encoder: TextEncoder,
|
||||
) => {
|
||||
return (
|
||||
data: StreamResponse,
|
||||
transformFn?: (data: StreamResponse) => StreamResponse,
|
||||
) => {
|
||||
if (!data?.type || data.type !== "chat") {
|
||||
return;
|
||||
}
|
||||
|
||||
let transformedData: StreamResponse;
|
||||
|
||||
if (transformFn) {
|
||||
transformedData = transformFn(data);
|
||||
} else {
|
||||
if (
|
||||
data.data.type === "content_block_start" &&
|
||||
data.data.content_block?.type === "text"
|
||||
) {
|
||||
transformedData = {
|
||||
type: "chat",
|
||||
data: {
|
||||
choices: [
|
||||
{
|
||||
delta: {
|
||||
content: data.data.content_block.text || "",
|
||||
},
|
||||
logprobs: null,
|
||||
finish_reason: null,
|
||||
},
|
||||
],
|
||||
},
|
||||
};
|
||||
} else if (data.data.delta?.text) {
|
||||
transformedData = {
|
||||
type: "chat",
|
||||
data: {
|
||||
choices: [
|
||||
{
|
||||
delta: {
|
||||
content: data.data.delta.text,
|
||||
},
|
||||
logprobs: null,
|
||||
finish_reason: null,
|
||||
},
|
||||
],
|
||||
},
|
||||
};
|
||||
} else if (data.data.choices?.[0]?.delta?.content) {
|
||||
transformedData = {
|
||||
type: "chat",
|
||||
data: {
|
||||
choices: [
|
||||
{
|
||||
index: data.data.choices[0].index,
|
||||
delta: {
|
||||
content: data.data.choices[0].delta.content,
|
||||
},
|
||||
logprobs: null,
|
||||
finish_reason: data.data.choices[0].finish_reason,
|
||||
},
|
||||
],
|
||||
},
|
||||
};
|
||||
} else if (data.data.choices) {
|
||||
transformedData = data;
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
controller.enqueue(
|
||||
encoder.encode(`data: ${JSON.stringify(transformedData)}\n\n`),
|
||||
);
|
||||
};
|
||||
};
|
||||
|
||||
export default handleStreamData;
|
Reference in New Issue
Block a user