qwen.js 0.1.1 → 0.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/client.d.ts CHANGED
@@ -19,4 +19,5 @@ export declare class QwenClient {
19
19
  ask(prompt: string, options?: ChatOptions): Promise<string>;
20
20
  setModel(model: string): void;
21
21
  getModel(): string;
22
+ createToolResult(toolCallId: string, content: string): ChatMessage;
22
23
  }
package/dist/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  export { QwenClient } from "./client";
2
2
  export { generatePKCE, requestDeviceCode, pollForToken, refreshAccessToken, isTokenExpired, } from "./auth";
3
- export type { QwenConfig, ChatMessage, ChatOptions, ChatResponse, ChatSession, StreamChunk, TokenState, DeviceCodeResponse, TokenResponse, PKCEPair, } from "./types";
3
+ export type { QwenConfig, ChatMessage, ChatOptions, ChatResponse, ChatSession, StreamChunk, TokenState, DeviceCodeResponse, TokenResponse, PKCEPair, Tool, ToolCall, ToolResult, } from "./types";
4
4
  import { QwenClient } from "./client";
5
5
  export declare function createQwen(config?: {
6
6
  accessToken?: string;
package/dist/index.js CHANGED
@@ -30,8 +30,9 @@ async function requestDeviceCode(challenge) {
30
30
  return response.json();
31
31
  }
32
32
  async function pollForToken(deviceCode, verifier, interval) {
33
+ let pollInterval = Number.isFinite(interval) && interval > 0 ? interval : 5;
33
34
  while (true) {
34
- await new Promise((r) => setTimeout(r, interval * 1000));
35
+ await new Promise((r) => setTimeout(r, pollInterval * 1000));
35
36
  const response = await fetch(`${AUTH_BASE}/token`, {
36
37
  method: "POST",
37
38
  headers: { "Content-Type": "application/x-www-form-urlencoded" },
@@ -50,7 +51,7 @@ async function pollForToken(deviceCode, verifier, interval) {
50
51
  continue;
51
52
  }
52
53
  if (data.error === "slow_down") {
53
- interval += 1;
54
+ pollInterval += 1;
54
55
  continue;
55
56
  }
56
57
  throw new Error(`Token poll failed: ${data.error_description || data.error}`);
@@ -175,22 +176,36 @@ ${url}
175
176
  return response;
176
177
  }
177
178
  async chat(messages, options) {
178
- const response = await this.request("/chat/completions", {
179
+ const body = {
179
180
  model: options?.model ?? this.model,
180
181
  messages,
181
182
  temperature: options?.temperature,
182
183
  stream: false
183
- });
184
+ };
185
+ if (options?.tools) {
186
+ body.tools = options.tools;
187
+ }
188
+ if (options?.tool_choice) {
189
+ body.tool_choice = options.tool_choice;
190
+ }
191
+ const response = await this.request("/chat/completions", body);
184
192
  return response.json();
185
193
  }
186
194
  async* chatStream(messages, options) {
187
195
  const msgs = typeof messages === "string" ? [{ role: "user", content: messages }] : messages;
188
- const response = await this.request("/chat/completions", {
196
+ const body = {
189
197
  model: options?.model ?? this.model,
190
198
  messages: msgs,
191
199
  temperature: options?.temperature,
192
200
  stream: true
193
- });
201
+ };
202
+ if (options?.tools) {
203
+ body.tools = options.tools;
204
+ }
205
+ if (options?.tool_choice) {
206
+ body.tool_choice = options.tool_choice;
207
+ }
208
+ const response = await this.request("/chat/completions", body);
194
209
  const reader = response.body?.getReader();
195
210
  if (!reader)
196
211
  throw new Error("No response body");
@@ -234,6 +249,13 @@ ${url}
234
249
  getModel() {
235
250
  return this.model;
236
251
  }
252
+ createToolResult(toolCallId, content) {
253
+ return {
254
+ role: "tool",
255
+ content,
256
+ tool_call_id: toolCallId
257
+ };
258
+ }
237
259
  }
238
260
  // src/index.ts
239
261
  function createQwen(config) {
@@ -0,0 +1 @@
1
+ export {};
package/dist/types.d.ts CHANGED
@@ -4,13 +4,22 @@ export interface QwenConfig {
4
4
  model?: string;
5
5
  }
6
6
  export interface ChatMessage {
7
- role: "system" | "user" | "assistant";
8
- content: string;
7
+ role: "system" | "user" | "assistant" | "tool";
8
+ content: string | null;
9
+ tool_calls?: ToolCall[];
10
+ tool_call_id?: string;
9
11
  }
10
12
  export interface ChatOptions {
11
13
  model?: string;
12
14
  temperature?: number;
13
15
  stream?: boolean;
16
+ tools?: Tool[];
17
+ tool_choice?: "none" | "auto" | {
18
+ type: "function";
19
+ function: {
20
+ name: string;
21
+ };
22
+ };
14
23
  }
15
24
  export interface DeviceCodeResponse {
16
25
  device_code: string;
@@ -51,6 +60,26 @@ export interface ChatResponse {
51
60
  total_tokens: number;
52
61
  };
53
62
  }
63
+ export interface Tool {
64
+ type: "function";
65
+ function: {
66
+ name: string;
67
+ description: string;
68
+ parameters: Record<string, any>;
69
+ };
70
+ }
71
+ export interface ToolCall {
72
+ id: string;
73
+ type: "function";
74
+ function: {
75
+ name: string;
76
+ arguments: string;
77
+ };
78
+ }
79
+ export interface ToolResult {
80
+ tool_call_id: string;
81
+ content: string;
82
+ }
54
83
  export interface StreamChunk {
55
84
  id?: string;
56
85
  object?: string;
@@ -61,6 +90,7 @@ export interface StreamChunk {
61
90
  delta: {
62
91
  role?: string;
63
92
  content?: string;
93
+ tool_calls?: ToolCall[];
64
94
  };
65
95
  finish_reason?: string | null;
66
96
  }[];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "qwen.js",
3
- "version": "0.1.1",
3
+ "version": "0.1.4",
4
4
  "description": "Elegant TypeScript SDK for Qwen AI API with OAuth/PKCE authentication",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",