🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.
TypeScript agentagent-collaborationagent-harnessloop-engineeringmcppersonal-assistantproactive-agentskillssuper-agenttask-management
import type OpenAI from 'openai';
import type { Stream } from 'openai/streaming';
import type { ChatStreamCallbacks } from '../../types';
import { convertOpenAIUsage } from '../usageConverters';
import {
createEventCallbacksTransformer,
createModelEventTransformer,
createSSEEncoder,
} from './events';
import type {
ChatPayloadForTransformStream,
StreamContext,
StreamProtocolChunk,
StreamProtocolToolCallChunk,
} from './protocol';
import { convertIterableToStream, generateToolCallId } from './protocol';
import { setOpenAIChatCompletionUsageMissingDiagnostics } from './usageMissingDiagnostics';
export function transformSparkResponseToStream(data: OpenAI.ChatCompletion) {
return new ReadableStream({
start(controller) {
const choices = data?.choices || [];
const chunk: OpenAI.ChatCompletionChunk = {
choices: choices.map((choice: OpenAI.ChatCompletion.Choice) => {
const toolCallsArray = choice.message.tool_calls
? Array.isArray(choice.message.tool_calls)
? choice.message.tool_calls
: [choice.message.tool_calls]
: []; // If not an array, wrap it in an array
return {
delta: {
content: choice.message.content,
role: choice.message.role,
tool_calls: toolCallsArray.map(
(tool, index): OpenAI.ChatCompletionChunk.Choice.Delta.ToolCall => {
// OpenAI SDK v6 made tool calls a function|custom union; lobehub only emits function calls.
const fnTool = tool as OpenAI.ChatCompletionMessageFunctionToolCall;
return {
function: fnTool.function,
id: fnTool.id,
index,
type: fnTool.type,
};
},
),
},
finish_reason: null,
index: choice.index,
logprobs: choice.logprobs,
};
}),
created: data.created,
id: data.id,
model: data.model,
object: 'chat.completion.chunk',
};
controller.enqueue(chunk);
if (data.usage) {
controller.enqueue({
choices: [],
created: data.created,
id: data.id,
model: data.model,
object: 'chat.completion.chunk',
usage: data.usage,
} as unknown as OpenAI.ChatCompletionChunk);
}
controller.enqueue({
choices: choices.map((choice: OpenAI.ChatCompletion.Choice) => ({
delta: {
content: null,
role: choice.message.role,
},
finish_reason: choice.finish_reason,
index: choice.index,
logprobs: choice.logprobs,
})),
created: data.created,
id: data.id,
model: data.model,
object: 'chat.completion.chunk',
system_fingerprint: data.system_fingerprint,
} as OpenAI.ChatCompletionChunk);
controller.close();
},
});
}
export const transformSparkStream = (
chunk: OpenAI.ChatCompletionChunk,
streamContext?: StreamContext,
payload?: ChatPayloadForTransformStream,
): StreamProtocolChunk | StreamProtocolChunk[] => {
if (streamContext) {
if (streamContext.chunkIndex === undefined) {
streamContext.chunkIndex = 0;
} else {
streamContext.chunkIndex++;
}
}
if (Array.isArray(chunk.choices) && chunk.choices.length === 0 && chunk.usage) {
if (streamContext) delete streamContext.usageMissingDiagnostics;
return { data: convertOpenAIUsage(chunk.usage, payload), id: chunk.id, type: 'usage' };
}
const item = chunk.choices[0];
if (!item) {
return { data: chunk, id: chunk.id, type: 'data' };
}
if (item.delta?.tool_calls) {
const toolCallsArray = Array.isArray(item.delta.tool_calls)
? item.delta.tool_calls
: [item.delta.tool_calls]; // If not an array, wrap it in an array
if (toolCallsArray.length > 0) {
return {
data: toolCallsArray.map((toolCall, index) => ({
function: toolCall.function,
id: toolCall.id || generateToolCallId(index, toolCall.function?.name),
index: typeof toolCall.index !== 'undefined' ? toolCall.index : index,
type: toolCall.type || 'function',
})),
id: chunk.id,
type: 'tool_calls',
} as StreamProtocolToolCallChunk;
}
}
if (item.finish_reason) {
const usageChunk: StreamProtocolChunk | undefined = chunk.usage
? { data: convertOpenAIUsage(chunk.usage, payload), id: chunk.id, type: 'usage' }
: undefined;
const appendUsageChunk = (
protocolChunk: StreamProtocolChunk | StreamProtocolChunk[],
): StreamProtocolChunk | StreamProtocolChunk[] => {
if (!usageChunk) return protocolChunk;
if (streamContext) delete streamContext.usageMissingDiagnostics;
return Array.isArray(protocolChunk)
? [...protocolChunk, usageChunk]
: [protocolChunk, usageChunk];
};
if (streamContext) {
if (usageChunk) {
delete streamContext.usageMissingDiagnostics;
} else {
setOpenAIChatCompletionUsageMissingDiagnostics(streamContext, payload, {
finishReason: item.finish_reason,
responseId: chunk.id,
});
}
}
// one-api's streaming interface can have both finish_reason and content
// {"id":"demo","model":"deepl-en","choices":[{"index":0,"delta":{"role":"assistant","content":"Introduce yourself."},"finish_reason":"stop"}]}
if (typeof item.delta?.content === 'string' && !!item.delta.content) {
return appendUsageChunk({ data: item.delta.content, id: chunk.id, type: 'text' });
}
if (usageChunk) return usageChunk;
return { data: item.finish_reason, id: chunk.id, type: 'stop' };
}
if (
item.delta &&
'reasoning_content' in item.delta &&
typeof item.delta.reasoning_content === 'string' &&
item.delta.reasoning_content !== ''
) {
return { data: item.delta.reasoning_content, id: chunk.id, type: 'reasoning' };
}
if (typeof item.delta?.content === 'string') {
/*
Handle v1 endpoint usage, mixed in the last content
{"code":0,"message":"Success","sid":"cha000d05ef@dx196553ae415b80a432","id":"cha000d05ef@dx196553ae415b80a432","created":1745186655,"choices":[{"delta":{"role":"assistant","content":"😊"},"index":0}],"usage":{"prompt_tokens":1,"completion_tokens":418,"total_tokens":419}}
*/
if (chunk.usage) {
if (streamContext) delete streamContext.usageMissingDiagnostics;
return [
{ data: item.delta.content, id: chunk.id, type: 'text' },
{ data: convertOpenAIUsage(chunk.usage, payload), id: chunk.id, type: 'usage' },
];
}
return { data: item.delta.content, id: chunk.id, type: 'text' };
}
if (item.delta?.content === null) {
return { data: item.delta, id: chunk.id, type: 'data' };
}
// Handle v2 endpoint usage
if (chunk.usage) {
if (streamContext) delete streamContext.usageMissingDiagnostics;
return { data: convertOpenAIUsage(chunk.usage, payload), id: chunk.id, type: 'usage' };
}
return {
data: { delta: item.delta, id: chunk.id, index: item.index },
id: chunk.id,
type: 'data',
};
};
export const SparkAIStream = (
stream: Stream<OpenAI.ChatCompletionChunk> | ReadableStream,
// TODO: preserve for RFC 097
{
callbacks,
payload,
}: {
callbacks?: ChatStreamCallbacks;
inputStartAt?: number;
payload?: ChatPayloadForTransformStream;
} = {},
) => {
const streamContext: StreamContext = { id: '' };
const readableStream =
stream instanceof ReadableStream ? stream : convertIterableToStream(stream);
const transformWithPayload = (chunk: OpenAI.ChatCompletionChunk, streamContext: StreamContext) =>
transformSparkStream(chunk, streamContext, payload);
return readableStream
.pipeThrough(createModelEventTransformer(transformWithPayload, streamContext))
.pipeThrough(createEventCallbacksTransformer(callbacks, { streamStack: streamContext }))
.pipeThrough(createSSEEncoder());
};