npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

@tcsk-ai/core

v0.0.2

Published

Core utilities and types for TCSK AI SDK

Downloads

4

Readme

@tcsk-ai/core

TCSK AI SDK 的核心包,提供与TCSK GPT API交互的基础功能。

安装

npm install @tcsk-ai/core

快速开始

基本使用

import { createDefaultTCSKClient, createUserMessage } from '@tcsk-ai/core'

// 创建客户端
const client = createDefaultTCSKClient(
  'your-username',
  'task-id-123',
  'azure',
  'gpt-35-turbo'
)

// 发送聊天请求
const response = await client.chat([
  createUserMessage('你好,请用幽默的语气回复')
])

console.log(response.result.content)

流式聊天

import { createTCSKClient, createUserMessage } from '@tcsk-ai/core'

const client = createTCSKClient({
  username: 'your-username',
  taskId: 'task-id-123',
  companyCode: 'azure',
  model: 'gpt-35-turbo',
  source: 'prompt',
  timeout: 60
})

// 流式聊天
const stream = await client.chatStream([
  createUserMessage('请写一首关于春天的诗')
])

for await (const chunk of stream) {
  if (chunk.delta.content) {
    process.stdout.write(chunk.delta.content)
  }

  if (chunk.done) {
    console.log('\n总Token数:', chunk.usage?.totalTokens)
  }
}

高级配置

import { createSystemMessage, createUserMessage, TCSKHttpClient } from '@tcsk-ai/core'

const client = new TCSKHttpClient({
  username: 'your-username',
  taskId: 'task-id-123',
  companyCode: 'azure',
  model: 'gpt-35-turbo',
  modelMethod: 'chat_completions',
  baseURL: 'https://gptapi.tcshuke.com', // 自定义API地址
  source: 'custom-source',
  timeout: 120,
  headers: {
    'X-Custom-Header': 'value'
  }
})

const response = await client.chat([
  createSystemMessage('你是一个专业的技术顾问'),
  createUserMessage('请解释什么是微服务架构')
], {
  temperature: 0.7,
  maxTokens: 2048,
  promptParams: {
    promptId: 'tech-consultant',
    source: 'knowledge-base',
    templateVersion: 'v1.0'
  }
})

API 参考

接口定义

TCSKModelConfig

interface TCSKModelConfig {
  username: string // 用户账户名
  source?: string // 来源(获取token时使用)
  baseURL?: string // API基础地址
  accessToken?: string // 访问令牌(可选,会自动获取)
  expirationTime?: number // 令牌过期时间
  taskId: string // 任务ID
  companyCode: string // 公司代码
  model: string // 模型名称
  modelMethod?: string // 模型方法
  timeout?: number // 请求超时时间(秒)
  headers?: Record<string, string> // 自定义请求头
}

TCSKChatMessage

interface TCSKChatMessage {
  role: 'system' | 'user' | 'assistant' // 消息角色
  content: string // 消息内容
}

TCSKChatOptions

interface TCSKChatOptions {
  temperature?: number // 温度参数,控制输出随机性
  maxTokens?: number // 最大输出令牌数
  stream?: boolean // 流式输出
  detailParams?: Record<string, any> // 详细参数
  promptParams?: { // 提示词参数
    promptId?: string
    source?: string
    templateVersion?: string
    returnDataFlag?: string
    dialogueId?: number
    questionId?: string
    logType?: string
    directBack?: Record<string, any>
  }
}

工具函数

消息创建

import {
  createAssistantMessage,
  createSystemMessage,
  createUserMessage
} from '@tcsk-ai/core'

const systemMsg = createSystemMessage('你是一个助手')
const userMsg = createUserMessage('你好')
const assistantMsg = createAssistantMessage('你好!有什么可以帮助你的吗?')

响应状态检查

import { isClientError, isServerError, isSuccessResponse } from '@tcsk-ai/core'

const response = await client.chat(messages)

if (isSuccessResponse(response.code)) {
  console.log('请求成功')
}
else if (isClientError(response.code)) {
  console.log('客户端错误')
}
else if (isServerError(response.code)) {
  console.log('服务端错误')
}

任务ID生成

import { generateTaskId } from '@tcsk-ai/core'

const taskId = generateTaskId() // 生成唯一的任务ID

错误处理

import { TCSKError } from '@tcsk-ai/core'

try {
  const response = await client.chat(messages)
}
catch (error) {
  if (error instanceof TCSKError) {
    console.log('错误代码:', error.code)
    console.log('HTTP状态码:', error.statusCode)
    console.log('是否是认证错误:', error.isAuthError)
    console.log('是否是网络错误:', error.isNetworkError)
  }
}

注意事项

  1. Token管理: 客户端会自动管理访问令牌的获取和刷新,无需手动处理。

  2. 错误重试: 当遇到token过期错误时,客户端会自动重新获取token并重试请求。

  3. 流式响应: 流式聊天支持实时接收AI生成的内容,适合长文本生成场景。

  4. 配置验证: 使用validateConfig函数可以在创建客户端前验证配置的完整性。

License

MIT