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 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: ChatCompletionMessageParam[];
18
- tools?: ChatCompletionTool[];
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: ChatCompletionMessageParam[] = [];
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: ChatCompletionMessageParam = { role: "assistant", content: "" };
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: ChatCompletionTool[] = [];
53
- const followupMessages: ChatCompletionMessageParam[] = [];
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
- type: "function",
59
- function: {
60
- name: definition.name,
61
- description: definition.tool.description,
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
- } as ChatCompletionMessageParam;
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: ChatCompletionMessageParam[];
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
  }