mioku-service-ai 2.2.2 → 3.0.0
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/core/tool-loop.ts +21 -25
- package/index.ts +13 -663
- package/instance/index.ts +459 -0
- package/package.json +4 -2
- package/providers/anthropic.ts +305 -0
- package/providers/base.ts +135 -0
- package/providers/gemini.ts +324 -0
- package/providers/index.ts +36 -0
- package/providers/openai-chat.ts +332 -0
- package/providers/openai-response.ts +280 -0
- package/providers-registry.ts +348 -0
- package/service.ts +466 -0
- package/types.ts +162 -14
- package/usage/tracker.ts +7 -11
package/core/tool-loop.ts
CHANGED
|
@@ -1,7 +1,3 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
ChatCompletionMessageParam,
|
|
3
|
-
ChatCompletionTool,
|
|
4
|
-
} from "openai/resources/chat/completions";
|
|
5
1
|
import type { AITool, ToolCallRecord, ToolResultFollowup } from "mioku";
|
|
6
2
|
import { TOOL_RESULT_FOLLOWUP_KEY } from "mioku";
|
|
7
3
|
import { logger } from "mioki";
|
|
@@ -9,17 +5,20 @@ import type {
|
|
|
9
5
|
AssistantMessageResult,
|
|
10
6
|
CompleteOptions,
|
|
11
7
|
CompleteResponse,
|
|
8
|
+
UnifiedMessage,
|
|
9
|
+
UnifiedToolDefinition,
|
|
12
10
|
} from "../types";
|
|
13
11
|
import type { UsageTracker } from "../usage/tracker";
|
|
14
12
|
|
|
15
13
|
export interface AssistantRequestArgs {
|
|
16
14
|
model: string;
|
|
17
|
-
messages:
|
|
18
|
-
tools?:
|
|
15
|
+
messages: UnifiedMessage[] | any[];
|
|
16
|
+
tools?: UnifiedToolDefinition[] | any[];
|
|
19
17
|
temperature: number;
|
|
20
18
|
max_tokens?: number;
|
|
21
19
|
stream?: boolean;
|
|
22
20
|
onTextDelta?: (delta: string) => void | Promise<void>;
|
|
21
|
+
cachePreference?: "prefer" | "none";
|
|
23
22
|
}
|
|
24
23
|
|
|
25
24
|
export interface ToolLoopDeps {
|
|
@@ -36,12 +35,12 @@ export async function runToolLoop(
|
|
|
36
35
|
const maxIterations = options.maxIterations ?? 40;
|
|
37
36
|
const allToolCalls: ToolCallRecord[] = [];
|
|
38
37
|
const failedToolCallKeys = new Set<string>();
|
|
39
|
-
const sessionMessages = [...options.messages];
|
|
40
|
-
const turnMessages:
|
|
38
|
+
const sessionMessages: any[] = [...options.messages];
|
|
39
|
+
const turnMessages: any[] = [];
|
|
41
40
|
let iterations = 0;
|
|
42
41
|
let content = "";
|
|
43
42
|
let reasoning: string | null = null;
|
|
44
|
-
let raw:
|
|
43
|
+
let raw: any = { role: "assistant", content: "" };
|
|
45
44
|
|
|
46
45
|
while (iterations < maxIterations) {
|
|
47
46
|
iterations++;
|
|
@@ -49,21 +48,19 @@ export async function runToolLoop(
|
|
|
49
48
|
? options.executableToolsProvider()
|
|
50
49
|
: (options.executableTools ?? []);
|
|
51
50
|
const toolMap = new Map<string, AITool>();
|
|
52
|
-
const tools:
|
|
53
|
-
const followupMessages:
|
|
51
|
+
const tools: UnifiedToolDefinition[] = [];
|
|
52
|
+
const followupMessages: any[] = [];
|
|
54
53
|
|
|
55
54
|
for (const definition of currentDefinitions) {
|
|
56
55
|
toolMap.set(definition.name, definition.tool);
|
|
57
56
|
tools.push({
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
parameters: definition.tool.parameters,
|
|
63
|
-
},
|
|
57
|
+
name: definition.name,
|
|
58
|
+
description: definition.tool.description,
|
|
59
|
+
parameters: definition.tool.parameters,
|
|
60
|
+
cacheable: true,
|
|
64
61
|
});
|
|
65
62
|
}
|
|
66
|
-
tracker.recordToolDefinitions(tools);
|
|
63
|
+
tracker.recordToolDefinitions(tools as any);
|
|
67
64
|
|
|
68
65
|
const assistant = await deps.requestAssistantMessage({
|
|
69
66
|
model,
|
|
@@ -73,6 +70,7 @@ export async function runToolLoop(
|
|
|
73
70
|
max_tokens: options.max_tokens,
|
|
74
71
|
stream: options.stream,
|
|
75
72
|
onTextDelta: options.onTextDelta,
|
|
73
|
+
cachePreference: options.cachePreference ?? "prefer",
|
|
76
74
|
});
|
|
77
75
|
tracker.recordAssistant(assistant);
|
|
78
76
|
if (assistant.usage) tracker.recordMeasuredTokens(assistant.usage);
|
|
@@ -138,10 +136,10 @@ export async function runToolLoop(
|
|
|
138
136
|
role: "tool",
|
|
139
137
|
content: JSON.stringify(normalized.visibleResult),
|
|
140
138
|
tool_call_id: toolCall.id,
|
|
141
|
-
}
|
|
139
|
+
};
|
|
142
140
|
sessionMessages.push(toolMessage);
|
|
143
141
|
turnMessages.push(toolMessage);
|
|
144
|
-
tracker.recordMessage(toolMessage);
|
|
142
|
+
tracker.recordMessage(toolMessage as any);
|
|
145
143
|
followupMessages.push(...normalized.followupMessages);
|
|
146
144
|
}
|
|
147
145
|
|
|
@@ -149,7 +147,7 @@ export async function runToolLoop(
|
|
|
149
147
|
sessionMessages.push(...followupMessages);
|
|
150
148
|
turnMessages.push(...followupMessages);
|
|
151
149
|
for (const followupMessage of followupMessages) {
|
|
152
|
-
tracker.recordMessage(followupMessage);
|
|
150
|
+
tracker.recordMessage(followupMessage as any);
|
|
153
151
|
}
|
|
154
152
|
}
|
|
155
153
|
}
|
|
@@ -201,7 +199,7 @@ function isToolErrorResult(result: any): boolean {
|
|
|
201
199
|
|
|
202
200
|
interface NormalizedToolResult {
|
|
203
201
|
visibleResult: any;
|
|
204
|
-
followupMessages:
|
|
202
|
+
followupMessages: any[];
|
|
205
203
|
}
|
|
206
204
|
|
|
207
205
|
export function normalizeToolResult(result: any): NormalizedToolResult {
|
|
@@ -236,8 +234,6 @@ export function normalizeToolResult(result: any): NormalizedToolResult {
|
|
|
236
234
|
|
|
237
235
|
return {
|
|
238
236
|
visibleResult,
|
|
239
|
-
followupMessages: [
|
|
240
|
-
{ role: "user", content } as ChatCompletionMessageParam,
|
|
241
|
-
],
|
|
237
|
+
followupMessages: [{ role: "user", content }],
|
|
242
238
|
};
|
|
243
239
|
}
|