Skip to content

Commit

Permalink
refactor(stream-text): unexport types
Browse files Browse the repository at this point in the history
  • Loading branch information
kwaa committed Feb 15, 2025
1 parent 701b1b7 commit c0a07a2
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 59 deletions.
54 changes: 0 additions & 54 deletions packages/stream-text/src/const.ts

This file was deleted.

2 changes: 1 addition & 1 deletion packages/stream-text/src/helper.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { StreamTextChunkResult } from './const'
import type { StreamTextChunkResult } from '.'

/** @internal */
// eslint-disable-next-line @masknet/string-no-data-url
Expand Down
60 changes: 56 additions & 4 deletions packages/stream-text/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,28 @@
import type { ChatOptions, Tool, ToolMessage } from '@xsai/shared-chat'
import type { AssistantMessage, ChatOptions, FinishReason, Message, Tool, ToolCall, ToolMessage, Usage } from '@xsai/shared-chat'

import { XSAIError } from '@xsai/shared'
import { chat } from '@xsai/shared-chat'

import type { StreamTextChoice, StreamTextChoiceState, StreamTextChunkResult, StreamTextMessage, StreamTextStep, StreamTextToolCall } from './const'

import { parseChunk } from './helper'

export * from './const'
export interface StreamTextChunkResult {
choices: {
delta: {
content?: string
refusal?: string
role: 'assistant'
tool_calls?: ToolCall[]
}
finish_reason?: FinishReason
index: number
}[]
created: number
id: string
model: string
object: 'chat.completion.chunk'
system_fingerprint: string
usage?: Usage
}

/**
* Options for configuring the StreamText functionality.
Expand Down Expand Up @@ -64,8 +79,45 @@ export interface StreamTextResult {
textStream: ReadableStream<string>
}

export interface StreamTextStep {
choices: StreamTextChoice[]
messages: Message[]
usage?: Usage
}

/** @internal */
type RecursivePromise<T> = Promise<(() => RecursivePromise<T>) | T>

/** @internal */
interface StreamTextChoice {
finish_reason?: FinishReason | null
index: number
message: StreamTextMessage
}

/** @internal */
interface StreamTextChoiceState {
calledToolCallIDs: Set<string>
currentToolID: null | string
endedToolCallIDs: Set<string>
index: number
toolCallErrors: { [id: string]: Error }
toolCallResults: { [id: string]: string }
}

/** @internal */
interface StreamTextMessage extends Omit<AssistantMessage, 'tool_calls'> {
content?: string
tool_calls?: { [id: string]: StreamTextToolCall }
}

/** @internal */
interface StreamTextToolCall extends ToolCall {
function: ToolCall['function'] & {
parsed_arguments: Record<string, unknown>
}
}

export const streamText = async (options: StreamTextOptions): Promise<StreamTextResult> => {
// output
let chunkCtrl: ReadableStreamDefaultController<StreamTextChunkResult> | undefined
Expand Down

0 comments on commit c0a07a2

Please sign in to comment.