@xsai/generate-text 0.5.0-beta.3 → 0.5.0-beta.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/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { WithUnknown } from '@xsai/shared';
2
- import { ChatOptions, CompletionStep, PrepareStep, StopCondition, Message, FinishReason, AssistantMessage, ChatCompletionUsage, CompletionToolCall, CompletionToolResult, Usage } from '@xsai/shared-chat';
2
+ import { ChatOptions, CompletionStep, PrepareStep, StopCondition, Message, Usage, FinishReason, AssistantMessage, ChatCompletionUsage, CompletionToolCall, CompletionToolResult } from '@xsai/shared-chat';
3
3
 
4
4
  interface GenerateTextOptions extends ChatOptions {
5
5
  onStepFinish?: (step: CompletionStep<true>) => Promise<unknown> | unknown;
@@ -10,6 +10,8 @@ interface GenerateTextOptions extends ChatOptions {
10
10
  stopWhen?: StopCondition<Message>;
11
11
  /** if you want to enable stream, use `@xsai/stream-{text,object}` */
12
12
  stream?: never;
13
+ /** @internal */
14
+ totalUsage?: Usage;
13
15
  }
14
16
  interface GenerateTextResponse {
15
17
  choices: {
@@ -33,6 +35,7 @@ interface GenerateTextResult {
33
35
  text?: string;
34
36
  toolCalls: CompletionToolCall[];
35
37
  toolResults: CompletionToolResult[];
38
+ totalUsage: Usage;
36
39
  usage: Usage;
37
40
  }
38
41
  declare const generateText: (options: WithUnknown<GenerateTextOptions>) => Promise<GenerateTextResult>;
package/dist/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { trampoline, responseJSON, InvalidResponseError } from '@xsai/shared';
2
- import { resolvePrepareStep, chat, normalizeChatCompletionUsage, stepCountAtLeast, executeTool, shouldStop } from '@xsai/shared-chat';
2
+ import { resolvePrepareStep, chat, normalizeChatCompletionUsage, computeTotalUsage, stepCountAtLeast, executeTool, shouldStop } from '@xsai/shared-chat';
3
3
 
4
4
  const rawGenerateText = async (options) => {
5
5
  const messages = options.steps == null ? structuredClone(options.messages) : options.messages;
@@ -14,16 +14,16 @@ const rawGenerateText = async (options) => {
14
14
  });
15
15
  return chat({
16
16
  ...options,
17
- maxSteps: void 0,
18
17
  messages: stepOptions.input,
19
18
  model: stepOptions.model,
20
19
  steps: void 0,
21
- stopWhen: void 0,
22
20
  stream: false,
23
- toolChoice: stepOptions.toolChoice
21
+ toolChoice: stepOptions.toolChoice,
22
+ totalUsage: void 0
24
23
  }).then(responseJSON).then(async (res) => {
25
24
  const { choices } = res;
26
25
  const usage = normalizeChatCompletionUsage(res.usage);
26
+ const totalUsage = computeTotalUsage(options.totalUsage, usage);
27
27
  if (!choices?.length) {
28
28
  const responseBody = JSON.stringify(res);
29
29
  throw new InvalidResponseError(`No choices returned, response body: ${responseBody}`, {
@@ -81,13 +81,15 @@ const rawGenerateText = async (options) => {
81
81
  text: step.text,
82
82
  toolCalls: step.toolCalls,
83
83
  toolResults: step.toolResults,
84
+ totalUsage,
84
85
  usage: step.usage
85
86
  };
86
87
  } else {
87
88
  return async () => rawGenerateText({
88
89
  ...options,
89
90
  messages,
90
- steps
91
+ steps,
92
+ totalUsage
91
93
  });
92
94
  }
93
95
  });
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@xsai/generate-text",
3
3
  "type": "module",
4
- "version": "0.5.0-beta.3",
4
+ "version": "0.5.0-beta.4",
5
5
  "description": "extra-small AI SDK.",
6
6
  "author": "Moeru AI",
7
7
  "license": "MIT",
@@ -33,12 +33,12 @@
33
33
  "dist"
34
34
  ],
35
35
  "dependencies": {
36
- "@xsai/shared": "0.5.0-beta.3",
37
- "@xsai/shared-chat": "0.5.0-beta.3"
36
+ "@xsai/shared": "0.5.0-beta.4",
37
+ "@xsai/shared-chat": "0.5.0-beta.4"
38
38
  },
39
39
  "devDependencies": {
40
40
  "valibot": "^1.0.0",
41
- "@xsai/tool": "0.5.0-beta.3"
41
+ "@xsai/tool": "0.5.0-beta.4"
42
42
  },
43
43
  "scripts": {
44
44
  "build": "pkgroll",