mirror of
https://github.com/geoffsee/open-gsio.git
synced 2025-09-08 22:56:46 +00:00

- Moved `providers`, `services`, `models`, `lib`, and related files to `src` directory within `server` package. - Adjusted imports across the codebase to reflect the new paths. - Renamed several `.ts` files for consistency. - Introduced an `index.ts` in the `ai/providers` package to export all providers. This improves maintainability and aligns with the project's updated directory structure.
94 lines
2.2 KiB
TypeScript
94 lines
2.2 KiB
TypeScript
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;
|