@xsai/generate-text 0.5.0-beta.3 → 0.5.0-beta.5

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,15 +1,19 @@
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, PostToolCall, PrepareStep, PreToolCall, 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;
6
+ postToolCall?: PostToolCall;
6
7
  prepareStep?: PrepareStep;
8
+ preToolCall?: PreToolCall;
7
9
  /** @internal */
8
10
  steps?: CompletionStep<true>[];
9
11
  /** @default `stepCountAtLeast(1)` */
10
12
  stopWhen?: StopCondition<Message>;
11
13
  /** if you want to enable stream, use `@xsai/stream-{text,object}` */
12
14
  stream?: never;
15
+ /** @internal */
16
+ totalUsage?: Usage;
13
17
  }
14
18
  interface GenerateTextResponse {
15
19
  choices: {
@@ -33,6 +37,7 @@ interface GenerateTextResult {
33
37
  text?: string;
34
38
  toolCalls: CompletionToolCall[];
35
39
  toolResults: CompletionToolResult[];
40
+ totalUsage: Usage;
36
41
  usage: Usage;
37
42
  }
38
43
  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}`, {
@@ -42,6 +42,8 @@ const rawGenerateText = async (options) => {
42
42
  msgToolCalls.map(async (toolCall) => executeTool({
43
43
  abortSignal: options.abortSignal,
44
44
  messages,
45
+ postToolCall: options.postToolCall,
46
+ preToolCall: options.preToolCall,
45
47
  toolCall,
46
48
  tools: options.tools
47
49
  }))
@@ -81,13 +83,15 @@ const rawGenerateText = async (options) => {
81
83
  text: step.text,
82
84
  toolCalls: step.toolCalls,
83
85
  toolResults: step.toolResults,
86
+ totalUsage,
84
87
  usage: step.usage
85
88
  };
86
89
  } else {
87
90
  return async () => rawGenerateText({
88
91
  ...options,
89
92
  messages,
90
- steps
93
+ steps,
94
+ totalUsage
91
95
  });
92
96
  }
93
97
  });
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.5",
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.5",
37
+ "@xsai/shared-chat": "0.5.0-beta.5"
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.5"
42
42
  },
43
43
  "scripts": {
44
44
  "build": "pkgroll",