Skip to content

Commit

Permalink
chore: 提供兼容vercel/ai的message封装,能够不引入ai包编译
Browse files Browse the repository at this point in the history
  • Loading branch information
TBXark committed Nov 13, 2024
1 parent f8ade3c commit 62c4fff
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 17 deletions.
12 changes: 7 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "chatgpt-telegram-workers",
"type": "module",
"version": "1.10.0",
"version": "1.10.2",
"description": "The easiest and quickest way to deploy your own ChatGPT Telegram bot is to use a single file and simply copy and paste it. There is no need for any dependencies, local development environment configuration, domain names, or servers.",
"author": "tbxark <[email protected]>",
"license": "MIT",
Expand All @@ -20,14 +20,14 @@
"dist"
],
"scripts": {
"lint": "eslint --fix *.js *.ts src plugins scripts",
"lint": "eslint --fix *.js *.ts src plugins",
"version": "tsx src/vite/version/main.ts",
"build": "vite build",
"build:local": "ENTRY=src/entry/local/index.ts vite build",
"build:docker": "docker build -t chatgpt-telegram-workers:latest .",
"build:dockerx": "docker build --platform linux/amd64,linux/arm64 -t tbxark/chatgpt-telegram-workers:latest --push .",
"build:vercel": "ENTRY=src/entry/vercel/index.ts vite build",
"build:pack": "TYPES=true FORMATS=es,cjs vite build",
"build:pack": "TYPES=true FORMATS=es,cjs ENTRY=src/entry/pack/index.ts vite build",
"deploy:dist": "wrangler deploy",
"deploy:build": "npm run build && wrangler deploy",
"deploy:vercel": "vercel deploy --prod",
Expand All @@ -40,14 +40,16 @@
"test": "tsx ./src/agent/index.test.ts"
},
"dependencies": {
"cloudflare-worker-adapter": "^1.3.3"
},
"optionalDependencies": {
"@ai-sdk/anthropic": "^0.0.56",
"@ai-sdk/azure": "^0.0.52",
"@ai-sdk/cohere": "^0.0.28",
"@ai-sdk/google": "^0.0.55",
"@ai-sdk/mistral": "^0.0.46",
"@ai-sdk/openai": "^0.0.72",
"ai": "^3.4.33",
"cloudflare-worker-adapter": "^1.3.3"
"ai": "^3.4.33"
},
"devDependencies": {
"@antfu/eslint-config": "^3.8.0",
Expand Down
55 changes: 55 additions & 0 deletions src/agent/message.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
export type DataContent = string | Uint8Array | ArrayBuffer | Buffer;

interface TextPart {
type: 'text';
text: string;
}

interface ImagePart {
type: 'image';
image: DataContent | URL;
mimeType?: string;
}

interface ToolCallPart {
type: 'tool-call';
toolCallId: string;
toolName: string;
args: unknown;
}

interface FilePart {
type: 'file';
data: DataContent | URL;
}

interface ToolResultPart {
type: 'tool-result';
toolCallId: string;
toolName: string;
result: unknown;
}

type AssistantContent = string | Array<TextPart | ToolCallPart>;
type UserContent = string | Array<TextPart | ImagePart | FilePart>;
type ToolContent = Array<ToolResultPart>;

export interface CoreSystemMessage {
role: 'system';
content: string;
}

export interface CoreAssistantMessage {
role: 'assistant';
content: AssistantContent;
}

export interface CoreUserMessage {
role: 'user';
content: UserContent;
}

export interface CoreToolMessage {
role: 'tool';
content: ToolContent;
}
6 changes: 3 additions & 3 deletions src/agent/next/next.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { ProviderV1 } from '@ai-sdk/provider';

Check failure on line 1 in src/agent/next/next.ts

View workflow job for this annotation

GitHub Actions / Deploy

Cannot find module '@ai-sdk/provider' or its corresponding type declarations.
import type { LanguageModelV1 } from 'ai';
import type { CoreMessage, LanguageModelV1 } from 'ai';

Check failure on line 2 in src/agent/next/next.ts

View workflow job for this annotation

GitHub Actions / Deploy

Cannot find module 'ai' or its corresponding type declarations.
import type { AgentUserConfig } from '../../config/env';
import type { ChatAgent, ChatAgentResponse, ChatStreamTextHandler, HistoryItem, LLMChatParams } from '../types';
import { createAnthropic } from '@ai-sdk/anthropic';

Check failure on line 5 in src/agent/next/next.ts

View workflow job for this annotation

GitHub Actions / Deploy

Cannot find module '@ai-sdk/anthropic' or its corresponding type declarations.
Expand All @@ -16,7 +16,7 @@ async function requestChatCompletionsV2(params: { model: LanguageModelV1; prompt
const stream = await streamText({
model: params.model,
prompt: params.prompt,
messages: params.messages,
messages: params.messages as Array<CoreMessage>,
});
await streamHandler(stream.textStream, t => t, onStream);

Check failure on line 21 in src/agent/next/next.ts

View workflow job for this annotation

GitHub Actions / Deploy

Type 'unknown' is not assignable to type 'string | null'.
return {
Expand All @@ -27,7 +27,7 @@ async function requestChatCompletionsV2(params: { model: LanguageModelV1; prompt
const result = await generateText({
model: params.model,
prompt: params.prompt,
messages: params.messages,
messages: params.messages as Array<CoreMessage>,
});
return {
text: result.text,
Expand Down
10 changes: 7 additions & 3 deletions src/agent/types.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
import type { CoreAssistantMessage, CoreSystemMessage, CoreToolMessage, CoreUserMessage } from 'ai';
import type { AgentUserConfig } from '../config/env';
import type { CoreAssistantMessage, CoreSystemMessage, CoreToolMessage, CoreUserMessage, DataContent } from './message';
// 当使用 `ai` 包时,取消注释以下行并注释掉上一行
// import type { CoreAssistantMessage, CoreSystemMessage, CoreToolMessage, CoreUserMessage, DataContent } from 'ai';

export type DataItemContent = DataContent;

export type ImageContent = string | Uint8Array | ArrayBuffer | Buffer | URL;
export type SystemMessageItem = CoreSystemMessage;
export type UserMessageItem = CoreUserMessage;
export type AssistantMessageItem = CoreAssistantMessage;
export type ToolMessageItem = CoreToolMessage;

export type ResponseMessage = AssistantMessageItem | ToolMessageItem;
export type HistoryItem = SystemMessageItem | UserMessageItem | AssistantMessageItem | ToolMessageItem;

export interface HistoryModifierResult {
history: HistoryItem[];
message: UserMessageItem;
message: CoreUserMessage;
}

export interface LLMChatParams {
Expand Down
4 changes: 2 additions & 2 deletions src/agent/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { ChatAgentResponse, HistoryItem, ImageContent } from './types';
import type { ChatAgentResponse, DataItemContent, HistoryItem } from './types';

export interface ImageRealContent {
url?: string;
Expand All @@ -20,7 +20,7 @@ export function extractTextContent(history: HistoryItem): string {
return '';
}

export function extractImageContent(imageData: ImageContent): ImageRealContent {
export function extractImageContent(imageData: DataItemContent | URL): ImageRealContent {
if (imageData instanceof URL) {
return { url: imageData.href };
}
Expand Down
4 changes: 2 additions & 2 deletions src/config/version.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export const BUILD_TIMESTAMP = 1731468421;
export const BUILD_VERSION = '9e1cda2';
export const BUILD_TIMESTAMP = 1731482441;
export const BUILD_VERSION = 'f8ade3c';
4 changes: 4 additions & 0 deletions src/entry/pack/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export * from '../../agent';
export * from '../../config/env';
export * from '../../route/index';
export * from '../../telegram/handler';
11 changes: 9 additions & 2 deletions vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { createVersionPlugin } from './src/vite/version';

const {
TYPES = 'false',
KEEP_VERSION = 'false',
FORMATS = 'es',
ENTRY = 'src/entry/core/index.ts',
} = process.env;
Expand All @@ -27,12 +28,18 @@ const plugins: Plugin[] = [
typescript: true,
}),
nodeExternals(),
createVersionPlugin(path.resolve(__dirname)),
];

if (TYPES === 'true') {
plugins.push(
dts(),
dts({
rollupTypes: true,
}),
);
}
if (KEEP_VERSION === 'false') {
plugins.push(
createVersionPlugin(path.resolve(__dirname)),
);
}

Expand Down

0 comments on commit 62c4fff

Please sign in to comment.