@modular-prompt/driver 0.13.1 → 0.13.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/cache-controller.d.ts +4 -1
- package/dist/cache-controller.d.ts.map +1 -1
- package/dist/echo-driver.d.ts.map +1 -1
- package/dist/echo-driver.js +9 -39
- package/dist/echo-driver.js.map +1 -1
- package/dist/formatter/completion-formatter.d.ts.map +1 -1
- package/dist/formatter/completion-formatter.js +30 -24
- package/dist/formatter/completion-formatter.js.map +1 -1
- package/dist/formatter/converter.d.ts.map +1 -1
- package/dist/formatter/converter.js +18 -13
- package/dist/formatter/converter.js.map +1 -1
- package/dist/formatter/types.d.ts +7 -0
- package/dist/formatter/types.d.ts.map +1 -1
- package/dist/google-genai/google-genai-cache-controller.d.ts +1 -1
- package/dist/google-genai/google-genai-cache-controller.d.ts.map +1 -1
- package/dist/google-genai/google-genai-cache-controller.js +4 -4
- package/dist/google-genai/google-genai-cache-controller.js.map +1 -1
- package/dist/mlx-ml/mlx-cache-controller.d.ts +3 -4
- package/dist/mlx-ml/mlx-cache-controller.d.ts.map +1 -1
- package/dist/mlx-ml/mlx-cache-controller.js +46 -22
- package/dist/mlx-ml/mlx-cache-controller.js.map +1 -1
- package/dist/mlx-ml/mlx-driver.js +4 -4
- package/dist/mlx-ml/mlx-driver.js.map +1 -1
- package/dist/mlx-ml/process/response-processor.js +1 -1
- package/dist/mlx-ml/process/response-processor.js.map +1 -1
- package/dist/mlx-ml/tool-call-parser/content-parsers.d.ts +9 -0
- package/dist/mlx-ml/tool-call-parser/content-parsers.d.ts.map +1 -0
- package/dist/mlx-ml/tool-call-parser/content-parsers.js +223 -0
- package/dist/mlx-ml/tool-call-parser/content-parsers.js.map +1 -0
- package/dist/mlx-ml/tool-call-parser/detector.d.ts +16 -0
- package/dist/mlx-ml/tool-call-parser/detector.d.ts.map +1 -0
- package/dist/mlx-ml/tool-call-parser/detector.js +58 -0
- package/dist/mlx-ml/tool-call-parser/detector.js.map +1 -0
- package/dist/mlx-ml/tool-call-parser/index.d.ts +7 -0
- package/dist/mlx-ml/tool-call-parser/index.d.ts.map +1 -0
- package/dist/mlx-ml/tool-call-parser/index.js +136 -0
- package/dist/mlx-ml/tool-call-parser/index.js.map +1 -0
- package/dist/mlx-ml/tool-call-parser/tool-formatter.d.ts +8 -0
- package/dist/mlx-ml/tool-call-parser/tool-formatter.d.ts.map +1 -0
- package/dist/mlx-ml/tool-call-parser/tool-formatter.js +88 -0
- package/dist/mlx-ml/tool-call-parser/tool-formatter.js.map +1 -0
- package/dist/mlx-ml/tool-call-parser/types.d.ts +18 -0
- package/dist/mlx-ml/tool-call-parser/types.d.ts.map +1 -0
- package/dist/mlx-ml/tool-call-parser/types.js +2 -0
- package/dist/mlx-ml/tool-call-parser/types.js.map +1 -0
- package/dist/mlx-ml/tool-call-parser/utils.d.ts +5 -0
- package/dist/mlx-ml/tool-call-parser/utils.d.ts.map +1 -0
- package/dist/mlx-ml/tool-call-parser/utils.js +77 -0
- package/dist/mlx-ml/tool-call-parser/utils.js.map +1 -0
- package/package.json +3 -1
- package/src/mlx-ml/python/utils/token_utils.py +8 -38
- package/dist/mlx-ml/tool-call-parser.d.ts +0 -30
- package/dist/mlx-ml/tool-call-parser.d.ts.map +0 -1
- package/dist/mlx-ml/tool-call-parser.js +0 -642
- package/dist/mlx-ml/tool-call-parser.js.map +0 -1
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
import { detect } from './detector.js';
|
|
2
|
+
import { tryParsers, jsonParser, normalizeJsonToolCall } from './content-parsers.js';
|
|
3
|
+
import { escapeRegExp, extractJsonObject } from './utils.js';
|
|
4
|
+
export { formatToolDefinitionsAsText } from './tool-formatter.js';
|
|
5
|
+
export function parseToolCalls(text, runtimeInfo) {
|
|
6
|
+
const { delimiters, marker } = detect(runtimeInfo);
|
|
7
|
+
// 1. デリミタベースの検出
|
|
8
|
+
for (const { pair } of delimiters) {
|
|
9
|
+
const result = parseWithDelimiters(text, pair.start, pair.end);
|
|
10
|
+
if (result.toolCalls.length > 0)
|
|
11
|
+
return result;
|
|
12
|
+
}
|
|
13
|
+
// 2. マーカートークン(Mistral型)
|
|
14
|
+
if (marker) {
|
|
15
|
+
const result = parseMistralStyle(text, marker.text);
|
|
16
|
+
if (result.toolCalls.length > 0)
|
|
17
|
+
return result;
|
|
18
|
+
}
|
|
19
|
+
// 3. コードブロック
|
|
20
|
+
const codeBlockResult = parseCodeBlocks(text);
|
|
21
|
+
if (codeBlockResult.toolCalls.length > 0)
|
|
22
|
+
return codeBlockResult;
|
|
23
|
+
// 4. 汎用フォールバック
|
|
24
|
+
return parseGeneric(text);
|
|
25
|
+
}
|
|
26
|
+
function parseWithDelimiters(text, startDelimiter, endDelimiter) {
|
|
27
|
+
const toolCalls = [];
|
|
28
|
+
let content = text;
|
|
29
|
+
let callIndex = 0;
|
|
30
|
+
const regex = new RegExp(escapeRegExp(startDelimiter) + '([\\s\\S]*?)' + escapeRegExp(endDelimiter), 'g');
|
|
31
|
+
let match;
|
|
32
|
+
while ((match = regex.exec(text)) !== null) {
|
|
33
|
+
const innerContent = match[1].trim();
|
|
34
|
+
const parsed = tryParsers(innerContent);
|
|
35
|
+
if (parsed) {
|
|
36
|
+
const calls = Array.isArray(parsed) ? parsed : [parsed];
|
|
37
|
+
for (const call of calls) {
|
|
38
|
+
toolCalls.push({
|
|
39
|
+
id: `call_${callIndex++}`,
|
|
40
|
+
name: call.name,
|
|
41
|
+
arguments: call.arguments || {},
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
if (toolCalls.length > 0) {
|
|
47
|
+
content = text.replace(regex, '').trim();
|
|
48
|
+
}
|
|
49
|
+
return { content, toolCalls };
|
|
50
|
+
}
|
|
51
|
+
function parseMistralStyle(text, markerText) {
|
|
52
|
+
const markerIndex = text.indexOf(markerText);
|
|
53
|
+
if (markerIndex === -1)
|
|
54
|
+
return { content: text, toolCalls: [] };
|
|
55
|
+
const content = text.substring(0, markerIndex).trim();
|
|
56
|
+
const callText = text.substring(markerIndex + markerText.length).trim();
|
|
57
|
+
const toolCalls = [];
|
|
58
|
+
let callIndex = 0;
|
|
59
|
+
try {
|
|
60
|
+
const parsed = JSON.parse(callText);
|
|
61
|
+
const items = Array.isArray(parsed) ? parsed : [parsed];
|
|
62
|
+
for (const item of items) {
|
|
63
|
+
if (item.name) {
|
|
64
|
+
toolCalls.push({
|
|
65
|
+
id: `call_${callIndex++}`,
|
|
66
|
+
name: item.name,
|
|
67
|
+
arguments: item.arguments || item.parameters || {},
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
catch {
|
|
73
|
+
// skip
|
|
74
|
+
}
|
|
75
|
+
return { content, toolCalls };
|
|
76
|
+
}
|
|
77
|
+
function parseCodeBlocks(text) {
|
|
78
|
+
const toolCalls = [];
|
|
79
|
+
let content = text;
|
|
80
|
+
let callIndex = 0;
|
|
81
|
+
const regex = /```json:toolCall\s*\n([\s\S]*?)```/g;
|
|
82
|
+
let match;
|
|
83
|
+
while ((match = regex.exec(text)) !== null) {
|
|
84
|
+
const parsed = jsonParser.parse(match[1].trim());
|
|
85
|
+
if (parsed) {
|
|
86
|
+
const calls = Array.isArray(parsed) ? parsed : [parsed];
|
|
87
|
+
for (const call of calls) {
|
|
88
|
+
toolCalls.push({
|
|
89
|
+
id: `call_${callIndex++}`,
|
|
90
|
+
name: call.name,
|
|
91
|
+
arguments: call.arguments || {},
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
if (toolCalls.length > 0) {
|
|
97
|
+
content = text.replace(regex, '').trim();
|
|
98
|
+
}
|
|
99
|
+
return { content, toolCalls };
|
|
100
|
+
}
|
|
101
|
+
function parseGeneric(text) {
|
|
102
|
+
const toolCalls = [];
|
|
103
|
+
let content = text;
|
|
104
|
+
let callIndex = 0;
|
|
105
|
+
const matched = [];
|
|
106
|
+
for (let i = 0; i < text.length; i++) {
|
|
107
|
+
if (text[i] !== '{')
|
|
108
|
+
continue;
|
|
109
|
+
const jsonStr = extractJsonObject(text, i);
|
|
110
|
+
if (!jsonStr)
|
|
111
|
+
continue;
|
|
112
|
+
try {
|
|
113
|
+
const parsed = JSON.parse(jsonStr);
|
|
114
|
+
const normalized = normalizeJsonToolCall(parsed);
|
|
115
|
+
if (normalized) {
|
|
116
|
+
toolCalls.push({
|
|
117
|
+
id: `call_${callIndex++}`,
|
|
118
|
+
name: normalized.name,
|
|
119
|
+
arguments: normalized.arguments || {},
|
|
120
|
+
});
|
|
121
|
+
matched.push(jsonStr);
|
|
122
|
+
i += jsonStr.length - 1;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
catch {
|
|
126
|
+
// skip
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
if (matched.length > 0) {
|
|
130
|
+
for (const m of matched) {
|
|
131
|
+
content = content.replace(m, '').trim();
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
return { content, toolCalls };
|
|
135
|
+
}
|
|
136
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/mlx-ml/tool-call-parser/index.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,MAAM,EAAE,MAAM,eAAe,CAAC;AACvC,OAAO,EAAE,UAAU,EAAE,UAAU,EAAE,qBAAqB,EAAE,MAAM,sBAAsB,CAAC;AACrF,OAAO,EAAE,YAAY,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAC;AAI7D,OAAO,EAAE,2BAA2B,EAAE,MAAM,qBAAqB,CAAC;AAElE,MAAM,UAAU,cAAc,CAC5B,IAAY,EACZ,WAAkC;IAElC,MAAM,EAAE,UAAU,EAAE,MAAM,EAAE,GAAG,MAAM,CAAC,WAAW,CAAC,CAAC;IAEnD,gBAAgB;IAChB,KAAK,MAAM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AAC,MAAM,CAAC;YACP,OAAO;QACT,CAAC;IACH,CAAC;IAED,IAAI,OAAO,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;QACvB,KAAK,MAAM,CAAC,IAAI,OAAO,EAAE,CAAC;YACxB,OAAO,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,IAAI,EAAE,CAAC;QAC1C,CAAC;IACH,CAAC;IAED,OAAO,EAAE,OAAO,EAAE,SAAS,EAAE,CAAC;AAChC,CAAC"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { ToolDefinition } from '../../types.js';
|
|
2
|
+
import type { SpecialToken, SpecialTokenPair } from '../../formatter/types.js';
|
|
3
|
+
export declare function formatToolDefinitionsAsText(tools: ToolDefinition[], specialTokens?: Record<string, SpecialToken | SpecialTokenPair>, toolCallFormat?: {
|
|
4
|
+
call_start?: string;
|
|
5
|
+
call_end?: string;
|
|
6
|
+
tool_parser_type?: string;
|
|
7
|
+
}): string;
|
|
8
|
+
//# sourceMappingURL=tool-formatter.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tool-formatter.d.ts","sourceRoot":"","sources":["../../../src/mlx-ml/tool-call-parser/tool-formatter.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAC;AACrD,OAAO,KAAK,EAAE,YAAY,EAAE,gBAAgB,EAAE,MAAM,0BAA0B,CAAC;AA+B/E,wBAAgB,2BAA2B,CACzC,KAAK,EAAE,cAAc,EAAE,EACvB,aAAa,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,YAAY,GAAG,gBAAgB,CAAC,EAC/D,cAAc,CAAC,EAAE;IAAE,UAAU,CAAC,EAAE,MAAM,CAAC;IAAC,QAAQ,CAAC,EAAE,MAAM,CAAC;IAAC,gBAAgB,CAAC,EAAE,MAAM,CAAA;CAAE,GACrF,MAAM,CAoER"}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
function formatProperties(lines, properties, required, depth = 1) {
|
|
2
|
+
const indent = ' '.repeat(depth);
|
|
3
|
+
for (const [name, schema] of Object.entries(properties)) {
|
|
4
|
+
const schemaObj = schema;
|
|
5
|
+
const req = required?.includes(name) ? ' (required)' : '';
|
|
6
|
+
const desc = schemaObj.description ? `: ${schemaObj.description}` : '';
|
|
7
|
+
const type = schemaObj.type || 'any';
|
|
8
|
+
if (type === 'array' && schemaObj.items) {
|
|
9
|
+
lines.push(`${indent}- ${name}: array${req}${desc}`);
|
|
10
|
+
const items = schemaObj.items;
|
|
11
|
+
if (items.properties) {
|
|
12
|
+
lines.push(`${indent} Each item:`);
|
|
13
|
+
formatProperties(lines, items.properties, items.required, depth + 2);
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
else if (type === 'object' && schemaObj.properties) {
|
|
17
|
+
lines.push(`${indent}- ${name}: object${req}${desc}`);
|
|
18
|
+
formatProperties(lines, schemaObj.properties, schemaObj.required, depth + 1);
|
|
19
|
+
}
|
|
20
|
+
else {
|
|
21
|
+
lines.push(`${indent}- ${name}: ${type}${req}${desc}`);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
export function formatToolDefinitionsAsText(tools, specialTokens, toolCallFormat) {
|
|
26
|
+
const lines = ['## Available Tools', ''];
|
|
27
|
+
lines.push('Instead of generating a text response, you can respond with tool call data.');
|
|
28
|
+
lines.push('The result of the tool execution will be provided as context in your next generation.');
|
|
29
|
+
lines.push('Choose a tool from the available list below, determine the appropriate arguments, and output a JSON string in the specified format.');
|
|
30
|
+
lines.push('If a required tool is not in the available list, report this to the user.');
|
|
31
|
+
lines.push('');
|
|
32
|
+
let exampleToolName;
|
|
33
|
+
let exampleArgs;
|
|
34
|
+
for (const tool of tools) {
|
|
35
|
+
lines.push(`### ${tool.name}`);
|
|
36
|
+
if (tool.description) {
|
|
37
|
+
lines.push(tool.description);
|
|
38
|
+
}
|
|
39
|
+
if (tool.parameters) {
|
|
40
|
+
const params = tool.parameters;
|
|
41
|
+
if (params.properties) {
|
|
42
|
+
lines.push('Parameters:');
|
|
43
|
+
formatProperties(lines, params.properties, params.required, 1);
|
|
44
|
+
if (!exampleToolName) {
|
|
45
|
+
exampleToolName = tool.name;
|
|
46
|
+
const argEntries = [];
|
|
47
|
+
for (const [name, schema] of Object.entries(params.properties)) {
|
|
48
|
+
const schemaObj = schema;
|
|
49
|
+
const val = schemaObj.type === 'number' ? '0' : schemaObj.type === 'boolean' ? 'true' : `"..."`;
|
|
50
|
+
argEntries.push(`"${name}": ${val}`);
|
|
51
|
+
}
|
|
52
|
+
exampleArgs = `{${argEntries.join(', ')}}`;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
else {
|
|
56
|
+
lines.push(`Parameters: ${JSON.stringify(tool.parameters)}`);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
lines.push('');
|
|
60
|
+
}
|
|
61
|
+
const concreteExample = exampleToolName
|
|
62
|
+
? `{"name": "${exampleToolName}", "arguments": ${exampleArgs}}`
|
|
63
|
+
: '{"name": "tool_name", "arguments": {"key": "value"}}';
|
|
64
|
+
if (toolCallFormat?.call_start && toolCallFormat?.call_end) {
|
|
65
|
+
lines.push('To call a tool, respond ONLY with:');
|
|
66
|
+
lines.push(toolCallFormat.call_start);
|
|
67
|
+
lines.push(concreteExample);
|
|
68
|
+
lines.push(toolCallFormat.call_end);
|
|
69
|
+
}
|
|
70
|
+
else {
|
|
71
|
+
const toolCallToken = specialTokens?.tool_call;
|
|
72
|
+
if (toolCallToken && 'start' in toolCallToken && 'end' in toolCallToken) {
|
|
73
|
+
const pair = toolCallToken;
|
|
74
|
+
lines.push('To call a tool, respond ONLY with:');
|
|
75
|
+
lines.push(`${pair.start.text}`);
|
|
76
|
+
lines.push(concreteExample);
|
|
77
|
+
lines.push(`${pair.end.text}`);
|
|
78
|
+
}
|
|
79
|
+
else {
|
|
80
|
+
lines.push('To call a tool, respond ONLY with the following format. Do not include any other text before or after the tool call block:');
|
|
81
|
+
lines.push('```json:toolCall');
|
|
82
|
+
lines.push(concreteExample);
|
|
83
|
+
lines.push('```');
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
return lines.join('\n');
|
|
87
|
+
}
|
|
88
|
+
//# sourceMappingURL=tool-formatter.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tool-formatter.js","sourceRoot":"","sources":["../../../src/mlx-ml/tool-call-parser/tool-formatter.ts"],"names":[],"mappings":"AAGA,SAAS,gBAAgB,CACvB,KAAe,EACf,UAAmC,EACnC,QAAmB,EACnB,QAAgB,CAAC;IAEjB,MAAM,MAAM,GAAG,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;IAClC,KAAK,MAAM,CAAC,IAAI,EAAE,MAAM,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,UAAU,CAAC,EAAE,CAAC;QACxD,MAAM,SAAS,GAAG,MAAiC,CAAC;QACpD,MAAM,GAAG,GAAG,QAAQ,EAAE,QAAQ,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,aAAa,CAAC,CAAC,CAAC,EAAE,CAAC;QAC1D,MAAM,IAAI,GAAG,SAAS,CAAC,WAAW,CAAC,CAAC,CAAC,KAAK,SAAS,CAAC,WAAW,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;QACvE,MAAM,IAAI,GAAG,SAAS,CAAC,IAAI,IAAI,KAAK,CAAC;QAErC,IAAI,IAAI,KAAK,OAAO,IAAI,SAAS,CAAC,KAAK,EAAE,CAAC;YACxC,KAAK,CAAC,IAAI,CAAC,GAAG,MAAM,KAAK,IAAI,UAAU,GAAG,GAAG,IAAI,EAAE,CAAC,CAAC;YACrD,MAAM,KAAK,GAAG,SAAS,CAAC,KAAgC,CAAC;YACzD,IAAI,KAAK,CAAC,UAAU,EAAE,CAAC;gBACrB,KAAK,CAAC,IAAI,CAAC,GAAG,MAAM,cAAc,CAAC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[];
|
|
5
|
+
}
|
|
6
|
+
export interface ParsedToolCall {
|
|
7
|
+
name: string;
|
|
8
|
+
arguments: Record<string, unknown>;
|
|
9
|
+
}
|
|
10
|
+
export interface ContentParser {
|
|
11
|
+
readonly name: string;
|
|
12
|
+
parse(content: string): ParsedToolCall | ParsedToolCall[] | null;
|
|
13
|
+
}
|
|
14
|
+
export interface DelimiterPair {
|
|
15
|
+
start: string;
|
|
16
|
+
end: string;
|
|
17
|
+
}
|
|
18
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../src/mlx-ml/tool-call-parser/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,sBAAsB,CAAC;AAErD,MAAM,WAAW,mBAAmB;IAClC,OAAO,EAAE,MAAM,CAAC;IAChB,SAAS,EAAE,QAAQ,EAAE,CAAC;CACvB;AAED,MAAM,WAAW,cAAc;IAC7B,IAAI,EAAE,MAAM,CAAC;IACb,SAAS,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CACpC;AAED,MAAM,WAAW,aAAa;IAC5B,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,KAAK,CAAC,OAAO,EAAE,MAAM,GAAG,cAAc,GAAG,cAAc,EAAE,GAAG,IAAI,CAAC;CAClE;AAED,MAAM,WAAW,aAAa;IAC5B,KAAK,EAAE,MAAM,CAAC;IACd,GAAG,EAAE,MAAM,CAAC;CACb"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../../src/mlx-ml/tool-call-parser/types.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export declare function coerceValue(value: string | undefined): unknown;
|
|
2
|
+
export declare function extractBracketedValue(text: string, start: number): string | null;
|
|
3
|
+
export declare function extractJsonObject(text: string, start: number): string | null;
|
|
4
|
+
export declare function escapeRegExp(str: string): string;
|
|
5
|
+
//# sourceMappingURL=utils.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../../src/mlx-ml/tool-call-parser/utils.ts"],"names":[],"mappings":"AAAA,wBAAgB,WAAW,CAAC,KAAK,EAAE,MAAM,GAAG,SAAS,GAAG,OAAO,CAU9D;AAED,wBAAgB,qBAAqB,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAsBhF;AAED,wBAAgB,iBAAiB,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAwB5E;AAED,wBAAgB,YAAY,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAEhD"}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
export function coerceValue(value) {
|
|
2
|
+
if (value === undefined)
|
|
3
|
+
return null;
|
|
4
|
+
const normalized = value === 'None' ? 'null' : value === 'True' ? 'true' : value === 'False' ? 'false' : value;
|
|
5
|
+
try {
|
|
6
|
+
return JSON.parse(normalized);
|
|
7
|
+
}
|
|
8
|
+
catch {
|
|
9
|
+
return value;
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
export function extractBracketedValue(text, start) {
|
|
13
|
+
let depth = 0;
|
|
14
|
+
let inString = false;
|
|
15
|
+
let escape = false;
|
|
16
|
+
for (let i = start; i < text.length; i++) {
|
|
17
|
+
const ch = text[i];
|
|
18
|
+
if (escape) {
|
|
19
|
+
escape = false;
|
|
20
|
+
continue;
|
|
21
|
+
}
|
|
22
|
+
if (ch === '\\' && inString) {
|
|
23
|
+
escape = true;
|
|
24
|
+
continue;
|
|
25
|
+
}
|
|
26
|
+
if (ch === '"') {
|
|
27
|
+
inString = !inString;
|
|
28
|
+
continue;
|
|
29
|
+
}
|
|
30
|
+
if (inString)
|
|
31
|
+
continue;
|
|
32
|
+
if (ch === '[' || ch === '{')
|
|
33
|
+
depth++;
|
|
34
|
+
else if (ch === ']' || ch === '}')
|
|
35
|
+
depth--;
|
|
36
|
+
if (depth === 0) {
|
|
37
|
+
return text.slice(start, i + 1);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
return null;
|
|
41
|
+
}
|
|
42
|
+
export function extractJsonObject(text, start) {
|
|
43
|
+
if (text[start] !== '{')
|
|
44
|
+
return null;
|
|
45
|
+
let depth = 0;
|
|
46
|
+
let inString = false;
|
|
47
|
+
let escape = false;
|
|
48
|
+
for (let i = start; i < text.length; i++) {
|
|
49
|
+
const ch = text[i];
|
|
50
|
+
if (escape) {
|
|
51
|
+
escape = false;
|
|
52
|
+
continue;
|
|
53
|
+
}
|
|
54
|
+
if (ch === '\\' && inString) {
|
|
55
|
+
escape = true;
|
|
56
|
+
continue;
|
|
57
|
+
}
|
|
58
|
+
if (ch === '"') {
|
|
59
|
+
inString = !inString;
|
|
60
|
+
continue;
|
|
61
|
+
}
|
|
62
|
+
if (inString)
|
|
63
|
+
continue;
|
|
64
|
+
if (ch === '{' || ch === '[')
|
|
65
|
+
depth++;
|
|
66
|
+
else if (ch === '}' || ch === ']')
|
|
67
|
+
depth--;
|
|
68
|
+
if (depth === 0) {
|
|
69
|
+
return text.slice(start, i + 1);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
return null;
|
|
73
|
+
}
|
|
74
|
+
export function escapeRegExp(str) {
|
|
75
|
+
return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
76
|
+
}
|
|
77
|
+
//# sourceMappingURL=utils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../../../src/mlx-ml/tool-call-parser/utils.ts"],"names":[],"mappings":"AAAA,MAAM,UAAU,WAAW,CAAC,KAAyB;IACnD,IAAI,KAAK,KAAK,SAAS;QAAE,OAAO,IAAI,CAAC;IAEr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AAC,qBAAqB,EAAE,MAAM,CAAC,CAAC;AACpD,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@modular-prompt/driver",
|
|
3
|
-
"version": "0.13.
|
|
3
|
+
"version": "0.13.3",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"types": "./dist/index.d.ts",
|
|
@@ -25,12 +25,14 @@
|
|
|
25
25
|
"google-auth-library": "9.15.1",
|
|
26
26
|
"js-yaml": "4.1.1",
|
|
27
27
|
"openai": "5.23.2",
|
|
28
|
+
"proper-lockfile": "4.1.2",
|
|
28
29
|
"@modular-prompt/core": "0.3.0",
|
|
29
30
|
"@modular-prompt/utils": "0.3.5"
|
|
30
31
|
},
|
|
31
32
|
"devDependencies": {
|
|
32
33
|
"@eslint/js": "9.39.2",
|
|
33
34
|
"@types/node": "20.19.27",
|
|
35
|
+
"@types/proper-lockfile": "4.1.4",
|
|
34
36
|
"@typescript-eslint/eslint-plugin": "8.50.0",
|
|
35
37
|
"@typescript-eslint/parser": "8.50.0",
|
|
36
38
|
"eslint": "9.39.2",
|
|
@@ -187,50 +187,32 @@ def get_special_tokens(tokenizer):
|
|
|
187
187
|
def detect_tool_call_format(tokenizer):
|
|
188
188
|
"""tokenizer設定からtool call/resultのデリミタを検出する
|
|
189
189
|
|
|
190
|
-
|
|
191
|
-
- tool_parser_type:
|
|
192
|
-
- chat_template
|
|
190
|
+
tokenizerアクセスが必要な情報のみを抽出する:
|
|
191
|
+
- tool_parser_type: tokenizer_config由来のパーサー種別文字列
|
|
192
|
+
- chat_templateテキストからのデリミタパターン検出
|
|
193
|
+
|
|
194
|
+
パーサー種別→デリミタのマッピングはTS側(detector.ts)に一元化。
|
|
195
|
+
Python側はtokenizerから生の情報を抽出して渡す役割に専念する。
|
|
193
196
|
|
|
194
197
|
Returns:
|
|
195
198
|
dict or None: {
|
|
196
199
|
"tool_parser_type": str, # tokenizer_configのtool_parser_type
|
|
197
|
-
"call_start": str, #
|
|
198
|
-
"call_end": str, #
|
|
200
|
+
"call_start": str, # chat_templateから検出した開始デリミタ
|
|
201
|
+
"call_end": str, # chat_templateから検出した終了デリミタ
|
|
199
202
|
"response_start": str, # tool responseの開始デリミタ(検出時)
|
|
200
203
|
"response_end": str, # tool responseの終了デリミタ(検出時)
|
|
201
204
|
} or None
|
|
202
205
|
"""
|
|
203
206
|
import re
|
|
204
207
|
|
|
205
|
-
# tool_parser_type を取得
|
|
206
208
|
tool_parser_type = None
|
|
207
209
|
if hasattr(tokenizer, 'init_kwargs'):
|
|
208
210
|
tool_parser_type = tokenizer.init_kwargs.get('tool_parser_type')
|
|
209
211
|
|
|
210
|
-
# 既知パーサーからの逆引き(最優先)
|
|
211
|
-
KNOWN_TOOL_PARSERS = {
|
|
212
|
-
"json_tools": {"call_start": "<tool_call>", "call_end": "</tool_call>"},
|
|
213
|
-
"pythonic": {"call_start": "<|tool_call_start|>", "call_end": "<|tool_call_end|>"},
|
|
214
|
-
"function_gemma": {"call_start": "<start_function_call>", "call_end": "<end_function_call>"},
|
|
215
|
-
"mistral": {"call_start": "[TOOL_CALLS]", "call_end": ""},
|
|
216
|
-
"kimi_k2": {"call_start": "<|tool_calls_section_begin|>", "call_end": "<|tool_calls_section_end|>"},
|
|
217
|
-
"longcat": {"call_start": "<longcat_tool_call>", "call_end": "</longcat_tool_call>"},
|
|
218
|
-
"glm47": {"call_start": "<tool_call>", "call_end": "</tool_call>"},
|
|
219
|
-
"qwen3_coder": {"call_start": "<tool_call>", "call_end": "</tool_call>"},
|
|
220
|
-
"minimax_m2": {"call_start": "<minimax:tool_call>", "call_end": "</minimax:tool_call>"},
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
if tool_parser_type and tool_parser_type in KNOWN_TOOL_PARSERS:
|
|
224
|
-
result = {"tool_parser_type": tool_parser_type}
|
|
225
|
-
result.update(KNOWN_TOOL_PARSERS[tool_parser_type])
|
|
226
|
-
return result
|
|
227
|
-
|
|
228
|
-
# chat_template テキストを取得
|
|
229
212
|
template = getattr(tokenizer, 'chat_template', None)
|
|
230
213
|
if not template and hasattr(tokenizer, 'init_kwargs'):
|
|
231
214
|
template = tokenizer.init_kwargs.get('chat_template', '')
|
|
232
215
|
|
|
233
|
-
# tool_parser_type もテンプレートもなければ非対応
|
|
234
216
|
if not tool_parser_type and not template:
|
|
235
217
|
return None
|
|
236
218
|
|
|
@@ -238,21 +220,13 @@ def detect_tool_call_format(tokenizer):
|
|
|
238
220
|
if tool_parser_type:
|
|
239
221
|
result["tool_parser_type"] = tool_parser_type
|
|
240
222
|
|
|
241
|
-
# テンプレートテキストからデリミタを抽出
|
|
242
223
|
if template:
|
|
243
|
-
# 複数のtool_call関連パターンを順に試行
|
|
244
224
|
tool_call_patterns = [
|
|
245
|
-
# </tool_call>, <|/tool_call|>, <tool_call|> (終了タグ専用)
|
|
246
225
|
(r'<\|?tool_call\|?>', r'</tool_call>|<\|/tool_call\|>|<tool_call\|>'),
|
|
247
|
-
# <|tool_call_start|>...<|tool_call_end|>
|
|
248
226
|
(r'<\|tool_call_start\|>', r'<\|tool_call_end\|>'),
|
|
249
|
-
# <start_function_call>...<end_function_call>
|
|
250
227
|
(r'<start_function_call>', r'<end_function_call>'),
|
|
251
|
-
# <|tool_calls_section_begin|>...<|tool_calls_section_end|>
|
|
252
228
|
(r'<\|tool_calls_section_begin\|>', r'<\|tool_calls_section_end\|>'),
|
|
253
|
-
# <longcat_tool_call>...</longcat_tool_call>
|
|
254
229
|
(r'<longcat_tool_call>', r'</longcat_tool_call>'),
|
|
255
|
-
# <minimax:tool_call>...</minimax:tool_call>
|
|
256
230
|
(r'<minimax:tool_call>', r'</minimax:tool_call>'),
|
|
257
231
|
]
|
|
258
232
|
|
|
@@ -264,8 +238,6 @@ def detect_tool_call_format(tokenizer):
|
|
|
264
238
|
result["call_end"] = end_match.group(0)
|
|
265
239
|
break
|
|
266
240
|
|
|
267
|
-
# Harmony形式の専用検出
|
|
268
|
-
# テンプレート内で "functions." と <|call|> が共存する場合
|
|
269
241
|
if "call_start" not in result:
|
|
270
242
|
has_functions = re.search(r'"functions\."', template)
|
|
271
243
|
has_call = re.search(r'<\|call\|>', template)
|
|
@@ -274,14 +246,12 @@ def detect_tool_call_format(tokenizer):
|
|
|
274
246
|
result["call_start"] = "to=functions."
|
|
275
247
|
result["call_end"] = "<|call|>"
|
|
276
248
|
|
|
277
|
-
# Mistral特殊ケース
|
|
278
249
|
if "call_start" not in result:
|
|
279
250
|
mistral_match = re.search(r'\[TOOL_CALLS\]', template)
|
|
280
251
|
if mistral_match:
|
|
281
252
|
result["call_start"] = "[TOOL_CALLS]"
|
|
282
253
|
result["call_end"] = ""
|
|
283
254
|
|
|
284
|
-
# tool_response タグの検出
|
|
285
255
|
resp_tags = re.findall(r'<[|/]?tool_response[|]?>', template)
|
|
286
256
|
if len(resp_tags) >= 2:
|
|
287
257
|
open_tags = [t for t in resp_tags if '/' not in t]
|
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
import type { ToolCall } from '@modular-prompt/core';
|
|
2
|
-
import type { MlxRuntimeInfo } from './process/types.js';
|
|
3
|
-
import type { ToolDefinition } from '../types.js';
|
|
4
|
-
import type { SpecialToken, SpecialTokenPair } from '../formatter/types.js';
|
|
5
|
-
export interface ToolCallParseResult {
|
|
6
|
-
/** tool callを除いたテキスト */
|
|
7
|
-
content: string;
|
|
8
|
-
/** 検出されたtool calls */
|
|
9
|
-
toolCalls: ToolCall[];
|
|
10
|
-
}
|
|
11
|
-
/**
|
|
12
|
-
* モデル出力からtool callを検出・パース
|
|
13
|
-
*
|
|
14
|
-
* 検出戦略:
|
|
15
|
-
* 1. runtimeInfo.features.chat_template.tool_call_format が存在 → テンプレート由来のデリミタで検出
|
|
16
|
-
* 2. runtimeInfo.special_tokens.tool_call が存在 → 特殊トークンによるデリミタで検出
|
|
17
|
-
* 3. ```json:toolCall コードブロック → ラベル付きコードブロック検出
|
|
18
|
-
* 4. 汎用フォールバック → JSON形式のtool call検出
|
|
19
|
-
*/
|
|
20
|
-
export declare function parseToolCalls(text: string, runtimeInfo: MlxRuntimeInfo | null): ToolCallParseResult;
|
|
21
|
-
/**
|
|
22
|
-
* tool定義をテキスト形式にフォーマット
|
|
23
|
-
* tool_call_formatまたは特殊トークンがある場合はそのフォーマットに合わせた指示を生成
|
|
24
|
-
*/
|
|
25
|
-
export declare function formatToolDefinitionsAsText(tools: ToolDefinition[], specialTokens?: Record<string, SpecialToken | SpecialTokenPair>, toolCallFormat?: {
|
|
26
|
-
call_start?: string;
|
|
27
|
-
call_end?: string;
|
|
28
|
-
tool_parser_type?: string;
|
|
29
|
-
}): string;
|
|
30
|
-
//# sourceMappingURL=tool-call-parser.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"tool-call-parser.d.ts","sourceRoot":"","sources":["../../src/mlx-ml/tool-call-parser.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,sBAAsB,CAAC;AACrD,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAC;AACzD,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AAClD,OAAO,KAAK,EAAE,YAAY,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAC;AAuB5E,MAAM,WAAW,mBAAmB;IAClC,wBAAwB;IACxB,OAAO,EAAE,MAAM,CAAC;IAChB,sBAAsB;IACtB,SAAS,EAAE,QAAQ,EAAE,CAAC;CACvB;AAED;;;;;;;;GAQG;AACH,wBAAgB,cAAc,CAC5B,IAAI,EAAE,MAAM,EACZ,WAAW,EAAE,cAAc,GAAG,IAAI,GACjC,mBAAmB,CAoDrB;AAuiBD;;;GAGG;AACH,wBAAgB,2BAA2B,CACzC,KAAK,EAAE,cAAc,EAAE,EACvB,aAAa,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,YAAY,GAAG,gBAAgB,CAAC,EAC/D,cAAc,CAAC,EAAE;IAAE,UAAU,CAAC,EAAE,MAAM,CAAC;IAAC,QAAQ,CAAC,EAAE,MAAM,CAAC;IAAC,gBAAgB,CAAC,EAAE,MAAM,CAAA;CAAE,GACrF,MAAM,CAwER"}
|