diff --git a/packages-ext/telemetry/src/utils/stream-text-internal.ts b/packages-ext/telemetry/src/utils/stream-text-internal.ts index 8226c16..4369672 100644 --- a/packages-ext/telemetry/src/utils/stream-text-internal.ts +++ b/packages-ext/telemetry/src/utils/stream-text-internal.ts @@ -76,48 +76,3 @@ export const transformChunk = () => { }, }) } - -export class DelayedPromise { - get promise(): Promise { - if (this._promise == null) { - this._promise = new Promise((resolve, reject) => { - if (this.status.type === 'resolved') { - resolve(this.status.value) - } - else if (this.status.type === 'rejected') { - reject(this.status.error) - } - - this._resolve = resolve - this._reject = reject - }) - } - - return this._promise - } - - private _promise: Promise | undefined - private _reject: ((error: unknown) => void) | undefined - private _resolve: ((value: T) => void) | undefined - - private status: - | { error: unknown, type: 'rejected' } - | { type: 'pending' } - | { type: 'resolved', value: T } = { type: 'pending' } - - reject(error: unknown): void { - this.status = { error, type: 'rejected' } - - if (this._promise) { - this._reject?.(error) - } - } - - resolve(value: T): void { - this.status = { type: 'resolved', value } - - if (this._promise) { - this._resolve?.(value) - } - } -} diff --git a/packages-ext/telemetry/src/utils/stream-text.ts b/packages-ext/telemetry/src/utils/stream-text.ts index 8ffaf07..adf030e 100644 --- a/packages-ext/telemetry/src/utils/stream-text.ts +++ b/packages-ext/telemetry/src/utils/stream-text.ts @@ -1,6 +1,6 @@ import type { CompletionStep, CompletionToolCall, CompletionToolResult, FinishReason, Message, StreamTextEvent, StreamTextOptions, StreamTextResult, ToolCall, Usage, WithUnknown } from 'xsai' -import { chat, determineStepType, executeTool, objCamelToSnake, trampoline } from 'xsai' +import { chat, DelayedPromise, determineStepType, executeTool, objCamelToSnake, trampoline } from 'xsai' import type { WithTelemetry } from '../types/options' @@ -8,7 +8,7 @@ import { commonAttributes, idAttributes, metadataAttributes } from './attributes import { getTracer } from './get-tracer' import { now } from './now' import { recordSpan, recordSpanSync } from './record-span' -import { DelayedPromise, transformChunk } from './stream-text-internal' +import { transformChunk } from './stream-text-internal' import { stringifyTool } from './stringify-tool' import { wrapTool } from './wrap-tool' diff --git a/packages/stream-text/src/index.ts b/packages/stream-text/src/index.ts index 959c7ca..27a2925 100644 --- a/packages/stream-text/src/index.ts +++ b/packages/stream-text/src/index.ts @@ -1,12 +1,11 @@ import type { WithUnknown } from '@xsai/shared' import type { ChatOptions, CompletionStep, CompletionToolCall, CompletionToolResult, FinishReason, Message, ToolCall, Usage } from '@xsai/shared-chat' -import { objCamelToSnake, trampoline } from '@xsai/shared' +import { DelayedPromise, objCamelToSnake, trampoline } from '@xsai/shared' import { chat, determineStepType, executeTool } from '@xsai/shared-chat' import type { StreamTextEvent } from './types/event' -import { DelayedPromise } from './internal/_delayed-promise' import { transformChunk } from './internal/_transform-chunk' export type * from './types/event' diff --git a/packages/stream-text/src/internal/_delayed-promise.ts b/packages/stream-text/src/internal/_delayed-promise.ts deleted file mode 100644 index 55e3804..0000000 --- a/packages/stream-text/src/internal/_delayed-promise.ts +++ /dev/null @@ -1,44 +0,0 @@ -export class DelayedPromise { - get promise(): Promise { - if (this._promise == null) { - this._promise = new Promise((resolve, reject) => { - if (this.status.type === 'resolved') { - resolve(this.status.value) - } - else if (this.status.type === 'rejected') { - reject(this.status.error) - } - - this._resolve = resolve - this._reject = reject - }) - } - - return this._promise - } - - private _promise: Promise | undefined - private _reject: ((error: unknown) => void) | undefined - private _resolve: ((value: T) => void) | undefined - - private status: - | { error: unknown, type: 'rejected' } - | { type: 'pending' } - | { type: 'resolved', value: T } = { type: 'pending' } - - reject(error: unknown): void { - this.status = { error, type: 'rejected' } - - if (this._promise) { - this._reject?.(error) - } - } - - resolve(value: T): void { - this.status = { type: 'resolved', value } - - if (this._promise) { - this._resolve?.(value) - } - } -}