diff --git a/packages/generate-text/src/index.ts b/packages/generate-text/src/index.ts index 05a8fa4..3f5a4aa 100644 --- a/packages/generate-text/src/index.ts +++ b/packages/generate-text/src/index.ts @@ -5,6 +5,7 @@ import { type FinishReason, type Message, type Tool, + type Usage, } from '@xsai/shared-chat' export interface GenerateTextOptions extends ChatOptions { @@ -28,13 +29,7 @@ export interface GenerateTextResponse { model: string object: 'chat.completion' system_fingerprint: string - usage: GenerateTextResponseUsage -} - -export interface GenerateTextResponseUsage { - completion_tokens: number - prompt_tokens: number - total_tokens: number + usage: Usage } export interface ToolCall { @@ -57,7 +52,7 @@ export interface GenerateTextResult { text?: string toolCalls: ToolCall[] toolResults: ToolResult[] - usage: GenerateTextResponseUsage + usage: Usage } export interface StepResult { @@ -66,7 +61,7 @@ export interface StepResult { // type: 'continue' | 'initial' | 'tool-result' toolCalls: ToolCall[] toolResults: ToolResult[] - usage: GenerateTextResponseUsage + usage: Usage } /** @internal */ diff --git a/packages/shared-chat/src/types/index.ts b/packages/shared-chat/src/types/index.ts index 33aa846..ba56499 100644 --- a/packages/shared-chat/src/types/index.ts +++ b/packages/shared-chat/src/types/index.ts @@ -4,3 +4,4 @@ export type * from './message' export type * from './message-part' export type * from './tool' export type * from './tool-choice' +export type * from './usage' diff --git a/packages/shared-chat/src/types/usage.ts b/packages/shared-chat/src/types/usage.ts new file mode 100644 index 0000000..0a9287c --- /dev/null +++ b/packages/shared-chat/src/types/usage.ts @@ -0,0 +1,5 @@ +export interface Usage { + completion_tokens: number + prompt_tokens: number + total_tokens: number +} diff --git a/packages/stream-text/src/index.ts b/packages/stream-text/src/index.ts index 794317c..9c09e31 100644 --- a/packages/stream-text/src/index.ts +++ b/packages/stream-text/src/index.ts @@ -2,10 +2,11 @@ import { chat, type ChatOptions, type FinishReason, + type Usage, } from '@xsai/shared-chat' export interface StreamTextOptions extends ChatOptions { - onChunk?: (chunk: StreamTextResponse) => Promise | void + onChunk?: (chunk: ChunkResult) => Promise | void /** if you want to disable stream, use `@xsai/generate-{text,object}` */ stream?: never streamOptions?: { @@ -18,21 +19,15 @@ export interface StreamTextOptions extends ChatOptions { } } -export interface StreamTextResponseUsage { - completion_tokens: number - prompt_tokens: number - total_tokens: number -} - export interface StreamTextResult { - chunkStream: ReadableStream + chunkStream: ReadableStream finishReason?: FinishReason textStream: ReadableStream - usage?: StreamTextResponseUsage + usage?: Usage } // TODO: improve chunk type -export interface StreamTextResponse { +export interface ChunkResult { choices: { delta: { content: string @@ -46,7 +41,7 @@ export interface StreamTextResponse { model: string object: 'chat.completion.chunk' system_fingerprint: string - usage?: StreamTextResponseUsage + usage?: Usage } const chunkHeaderPrefix = 'data: ' @@ -62,7 +57,7 @@ export const streamText = async (options: StreamTextOptions): Promise