Skip to content

Commit b2aec1c

Browse files
gcfengfengguocai
and
fengguocai
authored
feat: add workflows app_id (#40)
* feat: add worksflow app_id * fix: improve type safety --------- Co-authored-by: fengguocai <[email protected]>
1 parent e98be85 commit b2aec1c

File tree

4 files changed

+42
-11
lines changed

4 files changed

+42
-11
lines changed

Diff for: packages/coze-js/src/core.ts

+17-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
/* eslint-disable max-params */
2-
import { type AxiosRequestConfig, type AxiosResponseHeaders } from 'axios';
2+
import {
3+
type AxiosRequestConfig,
4+
type AxiosResponseHeaders,
5+
type AxiosInstance,
6+
} from 'axios';
37

48
import { getNodeClientUserAgent, getUserAgent } from './version.js';
59
import { isBrowser, isPersonalAccessToken, mergeConfig } from './utils.js';
@@ -11,18 +15,21 @@ import { COZE_COM_BASE_URL } from './constant.js';
1115
export type RequestOptions = Omit<
1216
AxiosRequestConfig,
1317
'url' | 'method' | 'baseURL' | 'data' | 'responseType'
14-
>;
18+
> &
19+
Record<string, unknown>;
1520
export interface ClientOptions {
1621
/** baseURL, default is https://api.coze.com, Use https://api.coze.cn if you use https://coze.cn */
1722
baseURL?: string;
1823
/** Personal Access Token (PAT) or OAuth2.0 token */
1924
token: string;
2025
/** see https://github.com/axios/axios?tab=readme-ov-file#request-config */
2126
axiosOptions?: RequestOptions;
27+
/** Custom axios instance */
28+
axiosInstance?: AxiosInstance | unknown;
2229
/** Whether to enable debug mode */
2330
debug?: boolean;
2431
/** Custom headers */
25-
headers?: Headers | undefined;
32+
headers?: Headers | Record<string, unknown>;
2633
/** Whether Personal Access Tokens (PAT) are allowed in browser environments */
2734
allowPersonalAccessTokenInBrowser?: boolean;
2835
}
@@ -32,15 +39,17 @@ export class APIClient {
3239
baseURL: string;
3340
token: string;
3441
axiosOptions?: RequestOptions;
42+
axiosInstance?: AxiosInstance | unknown;
3543
debug: boolean;
3644
allowPersonalAccessTokenInBrowser: boolean;
37-
headers?: Headers;
45+
headers?: Headers | Record<string, unknown>;
3846

3947
constructor(config: ClientOptions) {
4048
this._config = config;
4149
this.baseURL = config.baseURL || COZE_COM_BASE_URL;
4250
this.token = config.token;
4351
this.axiosOptions = config.axiosOptions || {};
52+
this.axiosInstance = config.axiosInstance;
4453
this.debug = config.debug || false;
4554
this.allowPersonalAccessTokenInBrowser =
4655
config.allowPersonalAccessTokenInBrowser || false;
@@ -101,6 +110,7 @@ export class APIClient {
101110

102111
const fetchOptions = this.buildOptions(method, body, options);
103112
fetchOptions.isStreaming = isStream;
113+
fetchOptions.axiosInstance = this.axiosInstance;
104114

105115
this.debugLog(`--- request url: ${fullUrl}`);
106116
this.debugLog('--- request options:', fetchOptions);
@@ -110,7 +120,9 @@ export class APIClient {
110120
this.debugLog(`--- response status: ${response.status}`);
111121
this.debugLog('--- response headers: ', response.headers);
112122

113-
const contentType = response.headers['content-type'];
123+
// Taro use `header`
124+
const contentType = (response.headers ??
125+
(response as unknown as Record<string, string>).header)['content-type'];
114126

115127
if (isStream) {
116128
if (contentType && contentType.includes('application/json')) {

Diff for: packages/coze-js/src/fetcher.ts

+15-5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import axios, {
44
type AxiosResponseHeaders,
55
type AxiosRequestConfig,
66
type AxiosResponse,
7+
type AxiosInstance,
8+
type AxiosStatic,
79
} from 'axios';
810

911
import {
@@ -16,7 +18,7 @@ import {
1618

1719
export interface FetchAPIOptions extends AxiosRequestConfig {
1820
// Custom axios instance
19-
axiosInstance?: typeof axios;
21+
axiosInstance?: AxiosInstance | unknown;
2022
// Whether to use streaming mode
2123
isStreaming?: boolean;
2224
}
@@ -55,7 +57,7 @@ export async function fetchAPI<ResultType>(
5557
const axiosInstance = options.axiosInstance || axios;
5658

5759
// Add version check for streaming requests
58-
if (options.isStreaming) {
60+
if (options.isStreaming && isAxiosStatic(axiosInstance)) {
5961
const axiosVersion = axiosInstance.VERSION || axios.VERSION;
6062
if (!axiosVersion || compareVersions(axiosVersion, '1.7.1') < 0) {
6163
throw new CozeError(
@@ -64,7 +66,7 @@ export async function fetchAPI<ResultType>(
6466
}
6567
}
6668

67-
const response: AxiosResponse = await axiosInstance({
69+
const response: AxiosResponse = await (axiosInstance as AxiosInstance)({
6870
url,
6971
responseType: options.isStreaming ? 'stream' : 'json',
7072
adapter: options.isStreaming ? 'fetch' : undefined,
@@ -77,12 +79,16 @@ export async function fetchAPI<ResultType>(
7779
async *stream(): AsyncGenerator<ResultType> {
7880
try {
7981
const stream = response.data;
80-
const reader = stream[Symbol.asyncIterator]();
82+
const reader = stream[Symbol.asyncIterator]
83+
? stream[Symbol.asyncIterator]()
84+
: stream.getReader();
8185
const decoder = new TextDecoder();
8286
const fieldValues: Record<string, string> = {};
8387
let buffer = '';
8488
while (true) {
85-
const { done, value } = await reader.next();
89+
const { done, value } = await (reader.next
90+
? reader.next()
91+
: reader.read());
8692
if (done) {
8793
if (buffer) {
8894
// If the stream ends without a newline, it means an error occurred
@@ -135,3 +141,7 @@ function compareVersions(v1: string, v2: string): number {
135141
}
136142
return 0;
137143
}
144+
145+
function isAxiosStatic(instance: unknown): instance is AxiosStatic {
146+
return !!(instance as AxiosStatic)?.Axios;
147+
}

Diff for: packages/coze-js/src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export class CozeAPI extends APIClient {
1111
workspaces: API.WorkSpaces = new API.WorkSpaces(this);
1212
audio: API.Audio = new API.Audio(this);
1313
}
14+
export { type ClientOptions, type RequestOptions } from './core.js';
1415
export * from './auth.js';
1516
export * from './resources/index.js';
1617
export * from './fetcher.js';

Diff for: packages/coze-js/src/resources/workflows/runs/runs.ts

+9-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ export class Runs extends APIResource {
1111
* @param params.bot_id - Optional The ID of the bot associated with the workflow. | 可选 与工作流关联的机器人 ID。
1212
* @param params.parameters - Optional Parameters for the workflow execution. | 可选 工作流执行的参数。
1313
* @param params.ext - Optional Additional information for the workflow execution. | 可选 工作流执行的附加信息。
14+
* @param params.execute_mode - Optional The mode in which to execute the workflow. | 可选 工作流执行的模式。
15+
* @param params.connector_id - Optional The ID of the connector to use for the workflow. | 可选 用于工作流的连接器 ID。
16+
* @param params.app_id - Optional The ID of the app. | 可选 要进行会话聊天的 App ID
1417
* @returns RunWorkflowData | 工作流运行数据
1518
*/
1619
async create(params: RunWorkflowReq, options?: RequestOptions) {
@@ -31,6 +34,9 @@ export class Runs extends APIResource {
3134
* @param params.bot_id - Optional The ID of the bot associated with the workflow. | 可选 与工作流关联的机器人 ID。
3235
* @param params.parameters - Optional Parameters for the workflow execution. | 可选 工作流执行的参数。
3336
* @param params.ext - Optional Additional information for the workflow execution. | 可选 工作流执行的附加信息。
37+
* @param params.execute_mode - Optional The mode in which to execute the workflow. | 可选 工作流执行的模式。
38+
* @param params.connector_id - Optional The ID of the connector to use for the workflow. | 可选 用于工作流的连接器 ID。
39+
* @param params.app_id - Optional The ID of the app. | 可选 要进行会话聊天的 App ID
3440
* @returns Stream<WorkflowEvent, { id: string; event: string; data: string }> | 工作流事件流
3541
*/
3642
async *stream(params: RunWorkflowReq, options?: RequestOptions) {
@@ -68,7 +74,6 @@ export class Runs extends APIResource {
6874
* @param params.interrupt_type - Required The type of interruption to resume from. | 必选 要恢复的中断类型。
6975
* @returns { id: string; event: WorkflowEventType; data: WorkflowEventMessage | WorkflowEventInterrupt | WorkflowEventError | null } | 恢复的工作流事件数据
7076
*/
71-
7277
async resume(params: ResumeWorkflowReq, options?: RequestOptions) {
7378
const apiUrl = '/v1/workflow/stream_resume';
7479
const response = await this._client.post<
@@ -92,6 +97,9 @@ export interface RunWorkflowReq {
9297
bot_id?: string;
9398
parameters?: Record<string, unknown>;
9499
ext?: Record<string, string>;
100+
execute_mode?: string;
101+
connector_id?: string;
102+
app_id?: string;
95103
}
96104

97105
export interface RunWorkflowData {

0 commit comments

Comments
 (0)