Skip to content

Commit

Permalink
fix: lint errors
Browse files Browse the repository at this point in the history
  • Loading branch information
kwaa committed Jan 22, 2025
1 parent 4d60993 commit ebc5227
Show file tree
Hide file tree
Showing 22 changed files with 64 additions and 47 deletions.
4 changes: 3 additions & 1 deletion cspell.config.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version: '0.2'
version: "0.2"
ignorePaths: []
dictionaryDefinitions: []
dictionaries: []
Expand All @@ -13,6 +13,7 @@ words:
- gcornut
- importantimport
- koemotion
- masknet
- Moeru
- moeru-ai
- mxbai-embed-large
Expand All @@ -22,6 +23,7 @@ words:
- Ollama
- pkgroll
- qwen
- sonarjs
- twoslash
- typeschema
- unspeech
Expand Down
3 changes: 1 addition & 2 deletions packages/generate-object/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@
},
"dependencies": {
"@typeschema/main": "catalog:",
"@xsai/generate-text": "workspace:",
"@xsai/shared": "workspace:"
"@xsai/generate-text": "workspace:"
},
"devDependencies": {
"@gcornut/valibot-json-schema": "catalog:",
Expand Down
8 changes: 2 additions & 6 deletions packages/generate-object/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { type Infer, type Schema, toJSONSchema, validate } from '@typeschema/main'
import { generateText, type GenerateTextOptions, type GenerateTextResult } from '@xsai/generate-text'
import { clean } from '@xsai/shared'

export interface GenerateObjectOptions<T extends Schema> extends GenerateTextOptions {
schema: T
Expand All @@ -15,17 +14,14 @@ export interface GenerateObjectResult<T extends Schema> extends Omit<GenerateTex

/** @experimental WIP */
export const generateObject = async <T extends Schema>(options: GenerateObjectOptions<T>): Promise<GenerateObjectResult<T>> =>
// eslint-disable-next-line @masknet/no-then
generateText({
...options,
response_format: {
json_schema: {
description: options.schemaDescription,
name: options.schemaName ?? 'json_schema',
schema: await toJSONSchema(options.schema)
.then(json => clean({
...json,
$schema: undefined,
})),
schema: await toJSONSchema(options.schema),
strict: true,
},
type: 'json_schema',
Expand Down
1 change: 1 addition & 0 deletions packages/generate-object/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { describe, expect, it } from 'vitest'

import { generateObject } from '../src'

// eslint-disable-next-line @masknet/no-top-level
describe('@xsai/generate-object', () => {
it('basic', async () => {
const { object } = await generateObject({
Expand Down
2 changes: 2 additions & 0 deletions packages/generate-speech/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { describe, expect, it } from 'vitest'

import { generateSpeech } from '../src'

// eslint-disable-next-line @masknet/no-top-level
describe('@xsai/generate-speech', () => {
it('basic', async () => {
const speech = await generateSpeech({
Expand All @@ -17,6 +18,7 @@ describe('@xsai/generate-speech', () => {
it('chinese', async () => {
const speech = await generateSpeech({
baseURL: new URL('http://localhost:5050'),
// eslint-disable-next-line @masknet/unicode-specific-set
input: '我能吞下玻璃而不伤身体。',
model: 'tts-1',
voice: 'zh-CN-XiaoyiNeural',
Expand Down
5 changes: 3 additions & 2 deletions packages/generate-text/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ type RawGenerateTextTrampoline<T> = Promise<(() => RawGenerateTextTrampoline<T>)

/** @internal */
const rawGenerateText: RawGenerateText = async (options: GenerateTextOptions) =>
// eslint-disable-next-line @masknet/no-then
chat({
...options,
maxSteps: undefined,
Expand All @@ -88,7 +89,7 @@ const rawGenerateText: RawGenerateText = async (options: GenerateTextOptions) =>

const { finish_reason: finishReason, message } = choices[0]

if (message.content || !message.tool_calls || steps.length >= (options.maxSteps ?? 1)) {
if (message.content !== undefined || !message.tool_calls || steps.length >= (options.maxSteps ?? 1)) {
const step: StepResult = {
text: message.content,
toolCalls,
Expand Down Expand Up @@ -116,7 +117,7 @@ const rawGenerateText: RawGenerateText = async (options: GenerateTextOptions) =>
type: toolCallType,
} of message.tool_calls) {
const tool = (options.tools as Tool[]).find(tool => tool.function.name === toolName)!
const parsedArgs: Record<string, unknown> = JSON.parse(toolArgs)
const parsedArgs = JSON.parse(toolArgs) as Record<string, unknown>
const result = await tool.execute(parsedArgs)

toolCalls.push({
Expand Down
1 change: 1 addition & 0 deletions packages/generate-transcription/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export const generateTranscription = async (options: GenerateTranscriptionOption
body.append('model', options.model)
body.append('file', options.file, options.fileName)

// eslint-disable-next-line @masknet/no-then
return (options.fetch ?? globalThis.fetch)(requestURL('audio/transcriptions', options.baseURL), {
body,
headers: requestHeaders(options.headers, options.apiKey),
Expand Down
1 change: 1 addition & 0 deletions packages/generate-transcription/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { describe, expect, it } from 'vitest'

import { generateTranscription } from '../src'

// eslint-disable-next-line @masknet/no-top-level
describe('@xsai/generate-transcription', () => {
it('basic', async () => {
const { text } = await generateTranscription({
Expand Down
1 change: 1 addition & 0 deletions packages/model/src/utils/list-models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export interface ListModelsResponse {
}

export const listModels = async (options: ListModelsOptions): Promise<Model[]> =>
// eslint-disable-next-line @masknet/no-then
(options.fetch ?? globalThis.fetch)(requestURL('models', options.baseURL), {
headers: requestHeaders({
'Content-Type': 'application/json',
Expand Down
1 change: 1 addition & 0 deletions packages/model/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { describe, expect, it } from 'vitest'

import { listModels, retrieveModel } from '../src'

// eslint-disable-next-line @masknet/no-top-level
describe('@xsai/model', () => {
it('listModels', async () => {
const models = await listModels({
Expand Down
4 changes: 2 additions & 2 deletions packages/providers/src/providers/ollama.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { generateCRO } from '../utils/generate-cro'

export const createOllama = (userOptions?: ProviderOptions<false>):
/** @see {@link https://ollama.com/search} */
ChatProvider<'gemma2' | 'llama3.1' | 'llama3.2' | 'llama3.2-vision' | 'llama3.3' | 'qwen2.5' | 'qwen2.5-coder' | 'qwq' | (string & {})>
& EmbedProvider<'all-minilm' | 'mxbai-embed-large' | 'nomic-embed-text' | (string & {})>
ChatProvider<'gemma2' | 'llama3.1' | 'llama3.2' | 'llama3.2-vision' | 'llama3.3' | 'qwen2.5' | 'qwen2.5-coder' | 'qwq'>
& EmbedProvider<'all-minilm' | 'mxbai-embed-large' | 'nomic-embed-text'>
& ModelProvider => {
const options: ProviderResult = {
...userOptions,
Expand Down
8 changes: 4 additions & 4 deletions packages/providers/src/providers/unspeech/elevenlabs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import { generateCRO } from '../../utils/generate-cro'
/** @see {@link https://elevenlabs.io/docs/api-reference/text-to-speech/convert#request} */
export interface UnElevenLabsOptions {
/**
* This parameter controls text normalization with three modes: auto’, ‘on’, and off. When set to auto,
* This parameter controls text normalization with three modes: 'auto', 'on', and 'off'. When set to 'auto',
* the system will automatically decide whether to apply text normalization (e.g., spelling out numbers).
* With ‘on’, text normalization will always be applied, while with off, it will be skipped. Cannot be
* turned on for eleven_turbo_v2_5 model.
* With 'on', text normalization will always be applied, while with 'off', it will be skipped. Cannot be
* turned on for 'eleven_turbo_v2_5' model.
*/
applyTextNormalization?: 'auto' | 'off' | 'on'
/**
Expand Down Expand Up @@ -63,7 +63,7 @@ export interface UnElevenLabsOptions {
*/
seed?: number
/**
* Voice settings overriding stored setttings for the given voice. They are applied only on the given request.
* Voice settings overriding stored settings for the given voice. They are applied only on the given request.
*/
voiceSettings?: {
similarityBoost?: number
Expand Down
5 changes: 5 additions & 0 deletions packages/providers/src/types/provider.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import type { CommonRequestOptions } from '@xsai/shared'

export interface ChatProvider<T extends string = string> {
// eslint-disable-next-line sonarjs/no-useless-intersection
chat: (model: (string & {}) | T) => CommonRequestOptions
}

export interface EmbedProvider<T extends string = string> {
// eslint-disable-next-line sonarjs/no-useless-intersection
embed: (model: (string & {}) | T) => CommonRequestOptions
}

Expand All @@ -22,10 +24,13 @@ export type ProviderResult = Omit<CommonRequestOptions, 'model'> & Partial<Pick<

export interface SpeechProvider<T extends string = string, T2 = undefined> {
speech: T2 extends undefined
// eslint-disable-next-line sonarjs/no-useless-intersection
? (model: (string & {}) | T) => CommonRequestOptions
// eslint-disable-next-line sonarjs/no-useless-intersection
: (model: (string & {}) | T, extraOptions?: T2) => CommonRequestOptions & T2
}

export interface TranscriptionProvider<T extends string = string> {
// eslint-disable-next-line sonarjs/no-useless-intersection
transcription: (model: (string & {}) | T) => CommonRequestOptions
}
2 changes: 1 addition & 1 deletion packages/shared/src/utils/request-headers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ import type { CommonRequestOptions } from '../types'
import { clean } from './clean'

export const requestHeaders = (headers?: CommonRequestOptions['headers'], apiKey?: CommonRequestOptions['apiKey']) => clean({
Authorization: apiKey ? `Bearer ${apiKey}` : undefined,
Authorization: apiKey !== undefined ? `Bearer ${apiKey}` : undefined,
...headers,
})
1 change: 1 addition & 0 deletions packages/shared/src/utils/response-json.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { responseCatch } from './response-catch'

export const responseJSON = async <T>(res: Response) =>
// eslint-disable-next-line @masknet/no-then
responseCatch(res)
.then(async res => res.json() as Promise<T>)
1 change: 0 additions & 1 deletion packages/stream-object/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
},
"dependencies": {
"@typeschema/main": "catalog:",
"@xsai/shared": "workspace:",
"@xsai/stream-text": "workspace:"
},
"devDependencies": {
Expand Down
13 changes: 4 additions & 9 deletions packages/stream-object/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type { PartialDeep } from 'type-fest'

import { type Infer, type Schema, toJSONSchema } from '@typeschema/main'
import { clean } from '@xsai/shared'
import { streamText, type StreamTextOptions, type StreamTextResult } from '@xsai/stream-text'
import { parse } from 'best-effort-json-parser'

Expand All @@ -17,17 +16,14 @@ export interface StreamObjectResult<T extends Schema> extends StreamTextResult {

/** @experimental WIP */
export const streamObject = async <T extends Schema>(options: StreamObjectOptions<T>): Promise<StreamObjectResult<T>> =>
// eslint-disable-next-line @masknet/no-then
streamText({
...options,
response_format: {
json_schema: {
description: options.schemaDescription,
name: options.schemaName ?? 'json_schema',
schema: await toJSONSchema(options.schema)
.then(json => clean({
...json,
$schema: undefined,
})),
schema: await toJSONSchema(options.schema),
strict: true,
},
type: 'json_schema',
Expand All @@ -39,19 +35,18 @@ export const streamObject = async <T extends Schema>(options: StreamObjectOption
const [textStream, rawPartialObjectStream] = rawTextStream.tee()

let partialObjectData = ''
let partialObjectSnapshot = {}
let partialObjectSnapshot = {} as PartialDeep<Infer<T>>
const partialObjectStream = rawPartialObjectStream.pipeThrough(new TransformStream({
transform: (chunk, controller) => {
partialObjectData += chunk
try {
const data = parse(partialObjectData)
const data = parse(partialObjectData) as PartialDeep<Infer<T>>

if (JSON.stringify(partialObjectSnapshot) !== JSON.stringify(data)) {
partialObjectSnapshot = data
controller.enqueue(data)
}
}
// TODO: maybe handle
catch {}
},
}))
Expand Down
10 changes: 10 additions & 0 deletions packages/stream-object/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@ import { describe, expect, it } from 'vitest'

import { streamObject } from '../src'

// make TS happy
// https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream#browser_compatibility
declare global {
interface ReadableStream<R = any> {
// eslint-disable-next-line ts/method-signature-style
[Symbol.asyncIterator](): AsyncIterableIterator<R>
}
}

// eslint-disable-next-line @masknet/no-top-level
describe('@xsai/stream-object', () => {
it('basic', async () => {
const { partialObjectStream } = await streamObject({
Expand Down
12 changes: 7 additions & 5 deletions packages/stream-text/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,13 @@ export interface StreamTextResult {
usage?: Usage
}

// eslint-disable-next-line @masknet/string-no-data-url
const chunkHeaderPrefix = 'data:'

/**
* @experimental WIP, does not support function calling (tools).
*/
// eslint-disable-next-line @masknet/no-then
export const streamText = async (options: StreamTextOptions): Promise<StreamTextResult> => chat({
...options,
stream: true,
Expand Down Expand Up @@ -87,16 +89,16 @@ export const streamText = async (options: StreamTextOptions): Promise<StreamText
}

// Process normal chunk
const chunk: ChunkResult = JSON.parse(data)
const chunk = JSON.parse(data) as ChunkResult
controller.enqueue(chunk)

if (options.onChunk)
await options.onChunk(chunk)

if (chunk.choices[0].finish_reason) {
if (chunk.choices[0].finish_reason !== undefined) {
finishReason = chunk.choices[0].finish_reason
}
if (chunk.usage) {
if (chunk.usage !== undefined) {
usage = chunk.usage
}
}
Expand All @@ -105,11 +107,11 @@ export const streamText = async (options: StreamTextOptions): Promise<StreamText

// null body handled by import('@xsai/shared-chat').chat()
const rawChunkStream = res.body!.pipeThrough(new TransformStream({
transform: async (chunk, controller) => {
transform: async (chunk, controller: TransformStreamDefaultController<ChunkResult>) => {
const text = decoder.decode(chunk, { stream: true })
buffer += text
const lines = buffer.split('\n')
buffer = lines.pop() || ''
buffer = lines.pop() ?? ''

// Process complete lines
for (const line of lines) {
Expand Down
16 changes: 12 additions & 4 deletions packages/stream-text/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,18 @@ import { ollama } from '@xsai/providers'
import { clean } from '@xsai/shared'
import { describe, expect, it } from 'vitest'

import { streamText, type StreamTextResponse } from '../src'
import { type ChunkResult, streamText } from '../src'

// make TS happy
// https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream#browser_compatibility
declare global {
interface ReadableStream<R = any> {
// eslint-disable-next-line ts/method-signature-style
[Symbol.asyncIterator](): AsyncIterableIterator<R>
}
}

// eslint-disable-next-line @masknet/no-top-level
describe('@xsai/stream-text', () => {
it('basic', async () => {
const { textStream } = await streamText({
Expand Down Expand Up @@ -49,7 +59,7 @@ describe('@xsai/stream-text', () => {
onChunk: () => { onChunkCount++ },
})

const chunk: StreamTextResponse[] = []
const chunk: ChunkResult[] = []
const text: string[] = []

for await (const chunkPart of chunkStream) {
Expand All @@ -71,6 +81,4 @@ describe('@xsai/stream-text', () => {

expect(onChunkCount).toMatchSnapshot(chunkCount)
})

// TODO: error handling
})
2 changes: 0 additions & 2 deletions packages/tool/src/generate-text.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import type { Schema } from '@typeschema/main'
// eslint-disable-next-line unused-imports/no-unused-imports
import type { GenerateTextOptions as _GTO } from '@xsai/generate-text'

import type { ToolResult } from '.'

Expand Down
Loading

0 comments on commit ebc5227

Please sign in to comment.