@xsai/generate-text 0.1.2 → 0.1.3
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 +2 -2
- package/dist/index.js +19 -28
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ChatOptions, Tool, FinishReason, AssistantMessageResponse, Usage, Message, CompletionToolCall, CompletionToolResult } from '@xsai/shared-chat';
|
|
1
|
+
import { ChatOptions, Tool, FinishReason, AssistantMessageResponse, Usage, Message, CompletionToolCall, CompletionToolResult, StepType } from '@xsai/shared-chat';
|
|
2
2
|
|
|
3
3
|
interface GenerateTextOptions extends ChatOptions {
|
|
4
4
|
/** @default 1 */
|
|
@@ -34,7 +34,7 @@ interface GenerateTextResult {
|
|
|
34
34
|
}
|
|
35
35
|
interface GenerateTextStepResult {
|
|
36
36
|
finishReason: FinishReason;
|
|
37
|
-
stepType:
|
|
37
|
+
stepType: StepType;
|
|
38
38
|
text?: string;
|
|
39
39
|
toolCalls: CompletionToolCall[];
|
|
40
40
|
toolResults: CompletionToolResult[];
|
package/dist/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
// src/index.ts
|
|
2
|
-
import { chat,
|
|
2
|
+
import { chat, determineStepType, executeTool } from "@xsai/shared-chat";
|
|
3
3
|
var rawGenerateText = async (options) => chat({
|
|
4
4
|
...options,
|
|
5
5
|
maxSteps: void 0,
|
|
@@ -12,9 +12,15 @@ var rawGenerateText = async (options) => chat({
|
|
|
12
12
|
const toolCalls = [];
|
|
13
13
|
const toolResults = [];
|
|
14
14
|
const { finish_reason: finishReason, message } = choices[0];
|
|
15
|
+
const msgToolCalls = message?.tool_calls ?? [];
|
|
16
|
+
const stepType = determineStepType({
|
|
17
|
+
finishReason,
|
|
18
|
+
maxSteps: options.maxSteps ?? 1,
|
|
19
|
+
stepsLength: steps.length,
|
|
20
|
+
toolCallsLength: msgToolCalls.length
|
|
21
|
+
});
|
|
15
22
|
messages.push({ ...message, content: message.content });
|
|
16
|
-
|
|
17
|
-
if (message.content !== void 0 && message.content.length > 0 || !message.tool_calls || steps.length >= (options.maxSteps ?? 1)) {
|
|
23
|
+
if (finishReason === "stop" || stepType === "done") {
|
|
18
24
|
const step2 = {
|
|
19
25
|
finishReason,
|
|
20
26
|
stepType,
|
|
@@ -36,35 +42,20 @@ var rawGenerateText = async (options) => chat({
|
|
|
36
42
|
usage
|
|
37
43
|
};
|
|
38
44
|
}
|
|
39
|
-
for (const {
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
if (!tool) {
|
|
46
|
-
const availableTools = options.tools?.map((tool2) => tool2.function.name);
|
|
47
|
-
const availableToolsErrorMsg = availableTools === void 0 ? "No tools are available." : `Available tools: ${availableTools.join(", ")}.`;
|
|
48
|
-
throw new Error(`Model tried to call unavailable tool '${toolName}. ${availableToolsErrorMsg}.`);
|
|
49
|
-
}
|
|
50
|
-
const parsedArgs = JSON.parse(toolArgs);
|
|
51
|
-
const result = wrapToolResult(await tool.execute(parsedArgs, { abortSignal: options.abortSignal, messages, toolCallId }));
|
|
52
|
-
toolCalls.push({
|
|
53
|
-
args: toolArgs,
|
|
54
|
-
toolCallId,
|
|
55
|
-
toolCallType,
|
|
56
|
-
toolName
|
|
57
|
-
});
|
|
58
|
-
toolResults.push({
|
|
59
|
-
args: parsedArgs,
|
|
60
|
-
result,
|
|
61
|
-
toolCallId,
|
|
62
|
-
toolName
|
|
45
|
+
for (const toolCall of msgToolCalls) {
|
|
46
|
+
const { parsedArgs, result, toolName } = await executeTool({
|
|
47
|
+
abortSignal: options.abortSignal,
|
|
48
|
+
messages,
|
|
49
|
+
toolCall,
|
|
50
|
+
tools: options.tools
|
|
63
51
|
});
|
|
52
|
+
const toolInfo = { toolCallId: toolCall.id, toolName };
|
|
53
|
+
toolCalls.push({ ...toolInfo, args: toolCall.function.arguments, toolCallType: toolCall.type });
|
|
54
|
+
toolResults.push({ ...toolInfo, args: parsedArgs, result });
|
|
64
55
|
messages.push({
|
|
65
56
|
content: result,
|
|
66
57
|
role: "tool",
|
|
67
|
-
tool_call_id:
|
|
58
|
+
tool_call_id: toolCall.id
|
|
68
59
|
});
|
|
69
60
|
}
|
|
70
61
|
const step = {
|