Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: enhance multiple platform #64

Merged
merged 11 commits into from
Feb 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions common/changes/@coze/api/feat-taro-multi_2025-02-06-01-53.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"changes": [
{
"packageName": "@coze/api",
"comment": "fix version",
"type": "patch"
}
],
"packageName": "@coze/api",
"email": "[email protected]"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"changes": [
{
"packageName": "@coze/taro-api",
"comment": "remove multiplatform",
"type": "patch"
}
],
"packageName": "@coze/taro-api",
"email": "[email protected]"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"changes": [
{
"packageName": "@coze/taro-api",
"comment": "example",
"type": "patch"
}
],
"packageName": "@coze/taro-api",
"email": "[email protected]"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"changes": [
{
"packageName": "@coze/taro-api",
"comment": "modify api",
"type": "patch"
}
],
"packageName": "@coze/taro-api",
"email": "[email protected]"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"changes": [
{
"packageName": "@coze/taro-api",
"comment": "weapp",
"type": "patch"
}
],
"packageName": "@coze/taro-api",
"email": "[email protected]"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"changes": [
{
"packageName": "@coze/taro-api",
"comment": "expose response error",
"type": "minor"
}
],
"packageName": "@coze/taro-api",
"email": "[email protected]"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"changes": [
{
"packageName": "@coze/taro-api",
"comment": "fix testcase",
"type": "patch"
}
],
"packageName": "@coze/taro-api",
"email": "[email protected]"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"changes": [
{
"packageName": "@coze/taro-api",
"comment": "update version",
"type": "patch"
}
],
"packageName": "@coze/taro-api",
"email": "[email protected]"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"changes": [
{
"packageName": "@coze/taro-api",
"comment": "fix version",
"type": "patch"
}
],
"packageName": "@coze/taro-api",
"email": "[email protected]"
}
68 changes: 61 additions & 7 deletions examples/coze-js-taro/src/pages/index/index.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import { useState, useRef } from 'react';

import { useLoad } from '@tarojs/taro';
import { View, Text, Button } from '@tarojs/components';
import { View, Text, Button, Switch } from '@tarojs/components';
import { CozeAPI, AbortController } from '@coze/taro-api';
import { RoleType, ChatEventType } from '@coze/api';
import './index.css';

export default function Index() {
const [message, setMessage] = useState('');
const [streaming, setStreaming] = useState(true);
const [streamingMessage, setStreamingMessage] = useState('');
const [pollingMessage, setPollingMessage] = useState('');
const clientRef = useRef<CozeAPI | null>(null);

useLoad(() => {
Expand All @@ -18,9 +20,9 @@ export default function Index() {
});
});

const handleClick = async () => {
const handleStreamingChat = async () => {
if (clientRef.current) {
setMessage('');
setStreamingMessage('');
try {
const controller = new AbortController();
// setTimeout(() => {
Expand All @@ -41,7 +43,7 @@ export default function Index() {
);
for await (const chunk of res) {
if (chunk.event === ChatEventType.CONVERSATION_MESSAGE_DELTA) {
setMessage(msg => msg + chunk.data.content);
setStreamingMessage(msg => msg + chunk.data.content);
}
console.log(chunk);
}
Expand All @@ -51,10 +53,62 @@ export default function Index() {
}
};

const handlePollingChat = async () => {
if (clientRef.current) {
setPollingMessage('');
try {
const controller = new AbortController();
// setTimeout(() => {
// controller.abort();
// }, 10);

const { messages = [] } = await clientRef.current.chat.createAndPoll(
{
bot_id: process.env.TARO_APP_COZE_BOT_ID ?? '',
user_id: 'abc',
additional_messages: [
{ role: RoleType.User, content: 'hello', content_type: 'text' },
],
},
{
signal: controller.signal,
},
);
setPollingMessage(
(messages || []).reduce((acc, cur) => {
if (cur.type === 'answer') {
acc += cur.content;
}
return acc;
}, ''),
);
console.log('messages: ', messages);
} catch (e) {
console.log('failed: ', e);
}
}
};

return (
<View className="index">
<Button onClick={handleClick}>streaming chat</Button>
<Text>{message}</Text>
<View>
<Switch
checked={streaming}
onChange={evt => setStreaming(evt.detail.value)}
/>
<Text>{streaming ? 'streaming' : 'polling'}</Text>
</View>
{streaming ? (
<>
<Button onClick={handleStreamingChat}>streaming chat</Button>
<Text>{streamingMessage}</Text>
</>
) : (
<>
<Button onClick={handlePollingChat}>polling chat</Button>
<Text>{pollingMessage}</Text>
</>
)}
</View>
);
}
18 changes: 1 addition & 17 deletions examples/coze-js-taro3/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,7 @@ export default defineConfig(async merge => {
options: {},
},
framework: 'vue3',
compiler: {
type: 'webpack5',
prebundle: {
// 1. Don‘t prebundle '@coze/taro-api'
exclude: ['@coze/taro-api'],
},
},
compiler: 'webpack5',
cache: {
enable: false,
},
Expand All @@ -56,11 +50,6 @@ export default defineConfig(async merge => {
},
},
webpackChain(chain) {
// 2. Enable multi-platform support for '@coze/taro-api'
chain.resolve.plugin('MultiPlatformPlugin').tap(args => {
args[2]['include'] = ['@coze/taro-api'];
return args;
});
chain.resolve.plugin('tsconfig-paths').use(TsconfigPathsPlugin);
chain.module
.rule('taroApiBabel')
Expand Down Expand Up @@ -100,11 +89,6 @@ export default defineConfig(async merge => {
},
},
webpackChain(chain) {
// 2. Enable multi-platform support for '@coze/taro-api'
chain.resolve.plugin('MultiPlatformPlugin').tap(args => {
args[2]['include'] = ['@coze/taro-api'];
return args;
});
chain.resolve.plugin('tsconfig-paths').use(TsconfigPathsPlugin);
chain.module
.rule('taroApiBabel')
Expand Down
70 changes: 61 additions & 9 deletions examples/coze-js-taro3/src/pages/index/index.vue
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
<template>
<View class="index">
<Button @tap="handleClick">streaming chat</Button>
<Text>{{ msg }}</Text>
<Switch :checked="streaming" @change="handleChangeMode"></Switch>
<Text v-if="streaming">streaming chat</Text>
<Text v-else>polling chat</Text>
<template v-if="streaming">
<Button @tap="handleStreamingChat">streaming chat</Button>
<Text>{{ streamingMessage }}</Text>
</template>
<template v-else>
<Button @tap="handlePollingChat">polling chat</Button>
<Text>{{ pollingMessage }}</Text>
</template>
</View>
</template>

<script>
import { View, Text, Button } from '@tarojs/components';
import { View, Text, Button, Switch } from '@tarojs/components';
import { CozeAPI, AbortController } from '@coze/taro-api';
import { RoleType, ChatEventType } from '@coze/api';
import { ref } from 'vue';
Expand All @@ -17,18 +26,25 @@ export default {
View,
Text,
Button,
Switch,
},

setup() {
const msg = ref('');
const streaming = ref(true);
const streamingMessage = ref('');
const pollingMessage = ref('');
const client = new CozeAPI({
baseURL: process.env.TARO_APP_COZE_BASE_URL,
token: process.env.TARO_APP_COZE_PAT ?? '',
allowPersonalAccessTokenInBrowser: true, // only for test
});

async function handleClick() {
msg.value = '';
function handleChangeMode(evt) {
streaming.value = evt.detail.value;
}

async function handleStreamingChat() {
streamingMessage.value = '';
try {
const controller = new AbortController();
// setTimeout(() => {
Expand All @@ -49,7 +65,7 @@ export default {
);
for await (const chunk of res) {
if (chunk.event === ChatEventType.CONVERSATION_MESSAGE_DELTA) {
msg.value += chunk.data.content;
streamingMessage.value += chunk.data.content;
}
console.log(chunk);
}
Expand All @@ -58,9 +74,45 @@ export default {
}
}

async function handlePollingChat() {
pollingMessage.value = '';
try {
const controller = new AbortController();
// setTimeout(() => {
// controller.abort();
// }, 10);

const { messages } = await client.chat.createAndPoll(
{
bot_id: process.env.TARO_APP_COZE_BOT_ID ?? '',
user_id: 'abc',
additional_messages: [
{ role: RoleType.User, content: 'hello', content_type: 'text' },
],
},
{
signal: controller.signal,
},
);
pollingMessage.value = (messages || []).reduce((acc, cur) => {
if (cur.type === 'answer') {
acc += cur.content;
}
return acc;
}, '');
console.log('messages: ', messages);
} catch (e) {
console.log('failed: ', e);
}
}

return {
msg,
handleClick,
streaming,
streamingMessage,
pollingMessage,
handleChangeMode,
handleStreamingChat,
handlePollingChat,
};
},
};
Expand Down
2 changes: 1 addition & 1 deletion packages/coze-js/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@coze/api",
"version": "1.0.16",
"version": "1.0.17",
"description": "Official Coze Node.js SDK for seamless AI integration into your applications | 扣子官方 Node.js SDK,助您轻松集成 AI 能力到应用中",
"keywords": [
"coze",
Expand Down
Loading
Loading