@xsai/generate-text 0.0.28 → 0.0.30

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/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { ChatOptions, FinishReason, AssistantMessageResponse, Usage } from '@xsai/shared-chat';
1
+ import { ChatOptions, FinishReason, AssistantMessageResponse, Usage, Message } from '@xsai/shared-chat';
2
2
 
3
3
  interface GenerateTextOptions extends ChatOptions {
4
4
  /** @default 1 */
@@ -22,20 +22,9 @@ interface GenerateTextResponse {
22
22
  system_fingerprint: string;
23
23
  usage: Usage;
24
24
  }
25
- interface ToolCall {
26
- args: string;
27
- toolCallId: string;
28
- toolCallType: 'function';
29
- toolName: string;
30
- }
31
- interface ToolResult {
32
- args: Record<string, unknown>;
33
- result: string;
34
- toolCallId: string;
35
- toolName: string;
36
- }
37
25
  interface GenerateTextResult {
38
26
  finishReason: FinishReason;
27
+ messages: Message[];
39
28
  steps: StepResult[];
40
29
  text?: string;
41
30
  toolCalls: ToolCall[];
@@ -43,11 +32,25 @@ interface GenerateTextResult {
43
32
  usage: Usage;
44
33
  }
45
34
  interface StepResult {
35
+ finishReason: FinishReason;
36
+ stepType: 'continue' | 'initial' | 'tool-result';
46
37
  text?: string;
47
38
  toolCalls: ToolCall[];
48
39
  toolResults: ToolResult[];
49
40
  usage: Usage;
50
41
  }
42
+ interface ToolCall {
43
+ args: string;
44
+ toolCallId: string;
45
+ toolCallType: 'function';
46
+ toolName: string;
47
+ }
48
+ interface ToolResult {
49
+ args: Record<string, unknown>;
50
+ result: string;
51
+ toolCallId: string;
52
+ toolName: string;
53
+ }
51
54
  declare const generateText: (options: GenerateTextOptions) => Promise<GenerateTextResult>;
52
55
 
53
56
  export { type GenerateTextOptions, type GenerateTextResponse, type GenerateTextResult, type StepResult, type ToolCall, type ToolResult, generateText as default, generateText };
package/dist/index.js CHANGED
@@ -1,19 +1,23 @@
1
1
  import { chat } from '@xsai/shared-chat';
2
2
 
3
- const rawGenerateText = async (options) => await chat({
3
+ const rawGenerateText = async (options) => chat({
4
4
  ...options,
5
- maxSteps: void 0,
5
+ maxSteps: undefined,
6
6
  messages: options.messages,
7
- steps: void 0,
7
+ steps: undefined,
8
8
  stream: false
9
- }).then((res) => res.json()).then(async ({ choices, usage }) => {
9
+ }).then(async (res) => res.json()).then(async ({ choices, usage }) => {
10
10
  const messages = options.messages;
11
11
  const steps = options.steps ?? [];
12
12
  const toolCalls = [];
13
13
  const toolResults = [];
14
14
  const { finish_reason: finishReason, message } = choices[0];
15
- if (message.content || !message.tool_calls || steps.length >= (options.maxSteps ?? 1)) {
15
+ messages.push({ ...message, content: message.content });
16
+ const stepType = steps.length === 0 ? "initial" : steps.at(-1)?.finishReason === "tool-calls" ? "tool-result" : "continue";
17
+ if (message.content !== undefined || !message.tool_calls || steps.length >= (options.maxSteps ?? 1)) {
16
18
  const step2 = {
19
+ finishReason,
20
+ stepType,
17
21
  text: message.content,
18
22
  toolCalls,
19
23
  toolResults,
@@ -24,11 +28,14 @@ const rawGenerateText = async (options) => await chat({
24
28
  await options.onStepFinish(step2);
25
29
  return {
26
30
  finishReason,
31
+ messages,
27
32
  steps,
28
- ...step2
33
+ text: message.content,
34
+ toolCalls,
35
+ toolResults,
36
+ usage
29
37
  };
30
38
  }
31
- messages.push({ ...message, content: message.content });
32
39
  for (const {
33
40
  function: { arguments: toolArgs, name: toolName },
34
41
  id: toolCallId,
@@ -36,7 +43,7 @@ const rawGenerateText = async (options) => await chat({
36
43
  } of message.tool_calls) {
37
44
  const tool = options.tools.find((tool2) => tool2.function.name === toolName);
38
45
  const parsedArgs = JSON.parse(toolArgs);
39
- const result = await tool.execute(parsedArgs);
46
+ const result = await tool.execute(parsedArgs, { abortSignal: options.abortSignal, messages, toolCallId });
40
47
  toolCalls.push({
41
48
  args: toolArgs,
42
49
  toolCallId,
@@ -56,6 +63,8 @@ const rawGenerateText = async (options) => await chat({
56
63
  });
57
64
  }
58
65
  const step = {
66
+ finishReason,
67
+ stepType,
59
68
  text: message.content,
60
69
  toolCalls,
61
70
  toolResults,
@@ -64,7 +73,7 @@ const rawGenerateText = async (options) => await chat({
64
73
  steps.push(step);
65
74
  if (options.onStepFinish)
66
75
  await options.onStepFinish(step);
67
- return async () => await rawGenerateText({
76
+ return async () => rawGenerateText({
68
77
  ...options,
69
78
  messages,
70
79
  steps
@@ -72,7 +81,7 @@ const rawGenerateText = async (options) => await chat({
72
81
  });
73
82
  const generateText = async (options) => {
74
83
  let result = await rawGenerateText(options);
75
- while (result instanceof Function)
84
+ while (typeof result === "function")
76
85
  result = await result();
77
86
  return result;
78
87
  };
package/package.json CHANGED
@@ -1,31 +1,31 @@
1
1
  {
2
2
  "name": "@xsai/generate-text",
3
- "version": "0.0.28",
4
3
  "type": "module",
4
+ "version": "0.0.30",
5
+ "description": "extra-small AI SDK for Browser, Node.js, Deno, Bun or Edge Runtime.",
5
6
  "author": "Moeru AI",
6
7
  "license": "MIT",
7
8
  "homepage": "https://xsai.js.org",
8
- "description": "extra-small AI SDK for Browser, Node.js, Deno, Bun or Edge Runtime.",
9
- "keywords": [
10
- "xsai",
11
- "openai",
12
- "ai"
13
- ],
14
9
  "repository": {
15
10
  "type": "git",
16
11
  "url": "git+https://github.com/moeru-ai/xsai.git",
17
12
  "directory": "packages/generate-text"
18
13
  },
19
14
  "bugs": "https://github.com/moeru-ai/xsai/issues",
15
+ "keywords": [
16
+ "xsai",
17
+ "openai",
18
+ "ai"
19
+ ],
20
20
  "sideEffects": false,
21
- "main": "./dist/index.js",
22
- "types": "./dist/index.d.ts",
23
21
  "exports": {
24
22
  ".": {
25
23
  "types": "./dist/index.d.ts",
26
24
  "default": "./dist/index.js"
27
25
  }
28
26
  },
27
+ "main": "./dist/index.js",
28
+ "types": "./dist/index.d.ts",
29
29
  "files": [
30
30
  "dist"
31
31
  ],