@gaunt-sloth/tools 0.0.1
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/README.md +25 -0
- package/dist/builtInToolsConfig.d.ts +22 -0
- package/dist/builtInToolsConfig.js +134 -0
- package/dist/builtInToolsConfig.js.map +1 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +3 -0
- package/dist/index.js.map +1 -0
- package/dist/middleware/binaryContentInjectionMiddleware.d.ts +22 -0
- package/dist/middleware/binaryContentInjectionMiddleware.js +125 -0
- package/dist/middleware/binaryContentInjectionMiddleware.js.map +1 -0
- package/dist/middleware/registry.d.ts +34 -0
- package/dist/middleware/registry.js +124 -0
- package/dist/middleware/registry.js.map +1 -0
- package/dist/middleware/types.d.ts +89 -0
- package/dist/middleware/types.js +9 -0
- package/dist/middleware/types.js.map +1 -0
- package/dist/tools/GthCustomToolkit.d.ts +41 -0
- package/dist/tools/GthCustomToolkit.js +280 -0
- package/dist/tools/GthCustomToolkit.js.map +1 -0
- package/dist/tools/GthDevToolkit.d.ts +24 -0
- package/dist/tools/GthDevToolkit.js +189 -0
- package/dist/tools/GthDevToolkit.js.map +1 -0
- package/dist/tools/GthFileSystemToolkit.d.ts +36 -0
- package/dist/tools/GthFileSystemToolkit.js +775 -0
- package/dist/tools/GthFileSystemToolkit.js.map +1 -0
- package/dist/tools/binaryUtils.d.ts +13 -0
- package/dist/tools/binaryUtils.js +55 -0
- package/dist/tools/binaryUtils.js.map +1 -0
- package/dist/tools/gthStatusUpdateTool.d.ts +2 -0
- package/dist/tools/gthStatusUpdateTool.js +15 -0
- package/dist/tools/gthStatusUpdateTool.js.map +1 -0
- package/dist/tools/gthWebFetchTool.d.ts +2 -0
- package/dist/tools/gthWebFetchTool.js +47 -0
- package/dist/tools/gthWebFetchTool.js.map +1 -0
- package/dist/utils/aiignoreUtils.d.ts +29 -0
- package/dist/utils/aiignoreUtils.js +82 -0
- package/dist/utils/aiignoreUtils.js.map +1 -0
- package/package.json +28 -0
package/README.md
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# @gaunt-sloth/tools
|
|
2
|
+
|
|
3
|
+
Tools and middleware for Gaunt Sloth.
|
|
4
|
+
|
|
5
|
+
## Contents
|
|
6
|
+
|
|
7
|
+
- Built-in tools configuration (`builtInToolsConfig`)
|
|
8
|
+
- Filesystem toolkit (`GthFileSystemToolkit`) — provides read/write/glob/grep tools controlled by `.aiignore`
|
|
9
|
+
- Custom tools (`GthCustomToolkit`) — executes user-defined shell commands from config
|
|
10
|
+
- Dev tools (`GthDevToolkit`) — tools for development and coding sessions
|
|
11
|
+
- Status update tool
|
|
12
|
+
- Web fetch tool
|
|
13
|
+
- Binary content injection middleware
|
|
14
|
+
- Middleware registry (`resolveMiddleware`) and types
|
|
15
|
+
|
|
16
|
+
## Dependencies
|
|
17
|
+
|
|
18
|
+
- `@gaunt-sloth/core`
|
|
19
|
+
|
|
20
|
+
## Exports
|
|
21
|
+
|
|
22
|
+
```js
|
|
23
|
+
import { builtInToolsConfig } from '@gaunt-sloth/tools/builtInToolsConfig.js';
|
|
24
|
+
import { resolveMiddleware } from '@gaunt-sloth/tools/middleware/registry.js';
|
|
25
|
+
```
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { StructuredToolInterface } from '@langchain/core/tools';
|
|
2
|
+
import { GthConfig } from '@gaunt-sloth/core/config.js';
|
|
3
|
+
import { GthCommand } from '@gaunt-sloth/core/core/types.js';
|
|
4
|
+
/**
|
|
5
|
+
* Available built-in tools may be configured in JSON config, see `builtInTools` of {@link GthConfig}.
|
|
6
|
+
*
|
|
7
|
+
* Does not include `filesystem`, because filesystem has its own config in {@link GthConfig}.
|
|
8
|
+
*/
|
|
9
|
+
export declare const AVAILABLE_BUILT_IN_TOOLS: {
|
|
10
|
+
/**
|
|
11
|
+
* Reference tool. Simply prints provided argument to the screen.
|
|
12
|
+
*/
|
|
13
|
+
readonly gth_status_update: "#src/tools/gthStatusUpdateTool.js";
|
|
14
|
+
/**
|
|
15
|
+
* Web fetch tool.
|
|
16
|
+
*/
|
|
17
|
+
readonly gth_web_fetch: "#src/tools/gthWebFetchTool.js";
|
|
18
|
+
};
|
|
19
|
+
/**
|
|
20
|
+
* Get default tools based on filesystem and built-in tools configuration
|
|
21
|
+
*/
|
|
22
|
+
export declare function getDefaultTools(config: GthConfig, command?: GthCommand): Promise<StructuredToolInterface[]>;
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @packageDocumentation
|
|
3
|
+
* Built-in tools config. {@link AVAILABLE_BUILT_IN_TOOLS} has a list of available tools.
|
|
4
|
+
*/
|
|
5
|
+
import GthFileSystemToolkit from '#src/tools/GthFileSystemToolkit.js';
|
|
6
|
+
import { displayWarning } from '@gaunt-sloth/core/utils/consoleUtils.js';
|
|
7
|
+
import { getCurrentWorkDir } from '@gaunt-sloth/core/utils/systemUtils.js';
|
|
8
|
+
import GthDevToolkit from '#src/tools/GthDevToolkit.js';
|
|
9
|
+
import GthCustomToolkit from '#src/tools/GthCustomToolkit.js';
|
|
10
|
+
/**
|
|
11
|
+
* Available built-in tools may be configured in JSON config, see `builtInTools` of {@link GthConfig}.
|
|
12
|
+
*
|
|
13
|
+
* Does not include `filesystem`, because filesystem has its own config in {@link GthConfig}.
|
|
14
|
+
*/
|
|
15
|
+
export const AVAILABLE_BUILT_IN_TOOLS = {
|
|
16
|
+
/**
|
|
17
|
+
* Reference tool. Simply prints provided argument to the screen.
|
|
18
|
+
*/
|
|
19
|
+
gth_status_update: '#src/tools/gthStatusUpdateTool.js',
|
|
20
|
+
/**
|
|
21
|
+
* Web fetch tool.
|
|
22
|
+
*/
|
|
23
|
+
gth_web_fetch: '#src/tools/gthWebFetchTool.js',
|
|
24
|
+
};
|
|
25
|
+
/**
|
|
26
|
+
* Get default tools based on filesystem and built-in tools configuration
|
|
27
|
+
*/
|
|
28
|
+
export async function getDefaultTools(config, command) {
|
|
29
|
+
const filesystemTools = filterFilesystemTools(config.filesystem, config.aiignore, config.binaryFormats);
|
|
30
|
+
const builtInTools = await getBuiltInTools(config);
|
|
31
|
+
const devTools = await filterDevTools(command, config.commands?.code?.devTools);
|
|
32
|
+
const customTools = getCustomTools(config, command);
|
|
33
|
+
return [...filesystemTools, ...devTools, ...customTools, ...builtInTools];
|
|
34
|
+
}
|
|
35
|
+
async function filterDevTools(command, devToolConfig) {
|
|
36
|
+
if (command !== 'code' || !devToolConfig) {
|
|
37
|
+
return [];
|
|
38
|
+
}
|
|
39
|
+
const toolkit = new GthDevToolkit(devToolConfig);
|
|
40
|
+
return [...toolkit.getTools()];
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Get custom tools based on configuration.
|
|
44
|
+
* Supports global customTools and per-command overrides.
|
|
45
|
+
*/
|
|
46
|
+
function getCustomTools(config, command) {
|
|
47
|
+
// Determine which custom tools to use
|
|
48
|
+
let toolsConfig;
|
|
49
|
+
if (command && config.commands?.[command]) {
|
|
50
|
+
const cmdConfig = config.commands[command];
|
|
51
|
+
// Check if command has customTools override
|
|
52
|
+
if ('customTools' in cmdConfig) {
|
|
53
|
+
toolsConfig = cmdConfig.customTools;
|
|
54
|
+
}
|
|
55
|
+
else {
|
|
56
|
+
// No override, use root-level
|
|
57
|
+
toolsConfig = config.customTools;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
else {
|
|
61
|
+
// No command specified or no command config, use root-level
|
|
62
|
+
toolsConfig = config.customTools;
|
|
63
|
+
}
|
|
64
|
+
// If explicitly disabled or empty, return no tools
|
|
65
|
+
if (toolsConfig === false || !toolsConfig || Object.keys(toolsConfig).length === 0) {
|
|
66
|
+
return [];
|
|
67
|
+
}
|
|
68
|
+
// Create toolkit with the determined config
|
|
69
|
+
const toolkit = new GthCustomToolkit(toolsConfig);
|
|
70
|
+
return toolkit.getTools();
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Filter filesystem tools based on configuration
|
|
74
|
+
*/
|
|
75
|
+
function filterFilesystemTools(filesystemConfig, aiignoreConfig, binaryFormats) {
|
|
76
|
+
const toolkit = new GthFileSystemToolkit({
|
|
77
|
+
allowedDirectories: [getCurrentWorkDir()],
|
|
78
|
+
aiignoreConfig,
|
|
79
|
+
binaryFormats,
|
|
80
|
+
});
|
|
81
|
+
if (filesystemConfig === 'all') {
|
|
82
|
+
return toolkit.getTools();
|
|
83
|
+
}
|
|
84
|
+
if (filesystemConfig === 'none') {
|
|
85
|
+
return [];
|
|
86
|
+
}
|
|
87
|
+
if (filesystemConfig === 'read') {
|
|
88
|
+
// Read-only: only allow read operations
|
|
89
|
+
return toolkit.getFilteredTools(['read']);
|
|
90
|
+
}
|
|
91
|
+
if (!Array.isArray(filesystemConfig)) {
|
|
92
|
+
return toolkit.getTools();
|
|
93
|
+
}
|
|
94
|
+
if (filesystemConfig.includes('all')) {
|
|
95
|
+
return toolkit.getTools();
|
|
96
|
+
}
|
|
97
|
+
// Handle an array of specific tool names or 'read'/'all'
|
|
98
|
+
const allowedTools = filesystemConfig.includes('read')
|
|
99
|
+
? toolkit.getFilteredTools(['read'])
|
|
100
|
+
: [];
|
|
101
|
+
// Also allow specific tool names
|
|
102
|
+
const allowedToolNames = new Set(filesystemConfig.filter((name) => name !== 'read' && name !== 'all'));
|
|
103
|
+
const specificNamedTools = toolkit.getTools().filter((tool) => {
|
|
104
|
+
return tool.name && allowedToolNames.has(tool.name);
|
|
105
|
+
});
|
|
106
|
+
// Combine and deduplicate
|
|
107
|
+
const allAllowedTools = [...allowedTools, ...specificNamedTools];
|
|
108
|
+
return allAllowedTools.filter((tool, index, arr) => arr.findIndex((t) => t.name === tool.name) === index);
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Get built-in tools based on configuration
|
|
112
|
+
*/
|
|
113
|
+
async function getBuiltInTools(config) {
|
|
114
|
+
const tools = [];
|
|
115
|
+
if (!config.builtInTools) {
|
|
116
|
+
return tools;
|
|
117
|
+
}
|
|
118
|
+
for (const toolName of config.builtInTools) {
|
|
119
|
+
if (toolName in AVAILABLE_BUILT_IN_TOOLS) {
|
|
120
|
+
try {
|
|
121
|
+
const tool = await import(AVAILABLE_BUILT_IN_TOOLS[toolName]);
|
|
122
|
+
tools.push(tool.get(config));
|
|
123
|
+
}
|
|
124
|
+
catch (error) {
|
|
125
|
+
displayWarning(`Failed to load built-in tool '${toolName}': ${error}`);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
else {
|
|
129
|
+
displayWarning(`Unknown built-in tool: ${toolName}`);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
return tools;
|
|
133
|
+
}
|
|
134
|
+
//# sourceMappingURL=builtInToolsConfig.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"builtInToolsConfig.js","sourceRoot":"","sources":["../src/builtInToolsConfig.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,OAAO,oBAAoB,MAAM,oCAAoC,CAAC;AAGtE,OAAO,EAAE,cAAc,EAAE,MAAM,yCAAyC,CAAC;AACzE,OAAO,EAAE,iBAAiB,EAAE,MAAM,wCAAwC,CAAC;AAE3E,OAAO,aAAa,MAAM,6BAA6B,CAAC;AACxD,OAAO,gBAAgB,MAAM,gCAAgC,CAAC;AAE9D;;;;GAIG;AACH,MAAM,CAAC,MAAM,wBAAwB,GAAG;IACtC;;OAEG;IACH,iBAAiB,EAAE,mCAAmC;IACtD;;OAEG;IACH,aAAa,EAAE,+BAA+B;CACtC,CAAC;AAEX;;GAEG;AACH,MAAM,CAAC,KAAK,UAAU,eAAe,CACnC,MAAiB,EACjB,OAAoB;IAEpB,MAAM,eAAe,GAAG,qBAAqB,CAC3C,MAAM,CAAC,UAAU,EACjB,MAAM,CAAC,QAAQ,EACf,MAAM,CAAC,aAAa,CACrB,CAAC;IACF,MAAM,YAAY,GAAG,MAAM,eAAe,CAAC,MAAM,CAAC,CAAC;IACnD,MAAM,QAAQ,GAAG,MAAM,cAAc,CAA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C;IAED,OAAO,KAAK,CAAC;AACf,CAAC"}
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,4BAA4B,CAAC;AAC3C,OAAO,EAAE,iBAAiB,EAAE,MAAM,6BAA6B,CAAC"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @packageDocumentation
|
|
3
|
+
* Middleware to inject binary content (images, PDFs, audio) as HumanMessage.
|
|
4
|
+
*
|
|
5
|
+
* The gth_read_binary tool returns binary data as a special string format:
|
|
6
|
+
* gth_read_binary;type:${type};path:${encodedPath};data:${media_type};base64,${data}
|
|
7
|
+
* where path is URL-encoded to handle special characters like semicolons.
|
|
8
|
+
*
|
|
9
|
+
* This middleware:
|
|
10
|
+
* 1. Detects the gth_read_binary tool calls
|
|
11
|
+
* 2. Parses the special string format from ToolMessage content
|
|
12
|
+
* 3. Injects HumanMessage with binary content blocks before next model call
|
|
13
|
+
*
|
|
14
|
+
* This works around LangChain's limitation where ToolMessage doesn't properly
|
|
15
|
+
* support binary content blocks for most providers.
|
|
16
|
+
*/
|
|
17
|
+
import { type AgentMiddleware } from 'langchain';
|
|
18
|
+
import type { GthConfig } from '@gaunt-sloth/core/config.js';
|
|
19
|
+
export interface BinaryContentInjectionMiddlewareSettings {
|
|
20
|
+
name?: 'binary-content-injection';
|
|
21
|
+
}
|
|
22
|
+
export declare function createBinaryContentInjectionMiddleware(_settings: BinaryContentInjectionMiddlewareSettings, _gthConfig: GthConfig): Promise<AgentMiddleware>;
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @packageDocumentation
|
|
3
|
+
* Middleware to inject binary content (images, PDFs, audio) as HumanMessage.
|
|
4
|
+
*
|
|
5
|
+
* The gth_read_binary tool returns binary data as a special string format:
|
|
6
|
+
* gth_read_binary;type:${type};path:${encodedPath};data:${media_type};base64,${data}
|
|
7
|
+
* where path is URL-encoded to handle special characters like semicolons.
|
|
8
|
+
*
|
|
9
|
+
* This middleware:
|
|
10
|
+
* 1. Detects the gth_read_binary tool calls
|
|
11
|
+
* 2. Parses the special string format from ToolMessage content
|
|
12
|
+
* 3. Injects HumanMessage with binary content blocks before next model call
|
|
13
|
+
*
|
|
14
|
+
* This works around LangChain's limitation where ToolMessage doesn't properly
|
|
15
|
+
* support binary content blocks for most providers.
|
|
16
|
+
*/
|
|
17
|
+
import { createMiddleware } from 'langchain';
|
|
18
|
+
import path from 'node:path';
|
|
19
|
+
import { debugLog } from '@gaunt-sloth/core/utils/debugUtils.js';
|
|
20
|
+
import { ToolMessage, HumanMessage } from '@langchain/core/messages';
|
|
21
|
+
/**
|
|
22
|
+
* Parse the special binary format string returned by gth_read_binary tool.
|
|
23
|
+
* Format: gth_read_binary;type:${type};path:${encodedPath};data:${media_type};base64,${data}
|
|
24
|
+
* Path is URL-encoded to handle special characters.
|
|
25
|
+
*/
|
|
26
|
+
function parseBinaryContent(content) {
|
|
27
|
+
if (!content.startsWith('gth_read_binary;')) {
|
|
28
|
+
return null;
|
|
29
|
+
}
|
|
30
|
+
try {
|
|
31
|
+
const parts = content.split(';');
|
|
32
|
+
if (parts.length < 4) {
|
|
33
|
+
return null;
|
|
34
|
+
}
|
|
35
|
+
const typeMatch = parts[1]?.match(/^type:(.+)$/);
|
|
36
|
+
const pathMatch = parts[2]?.match(/^path:(.+)$/);
|
|
37
|
+
const dataMatch = parts[3]?.match(/^data:(.+)$/);
|
|
38
|
+
const base64Match = content.match(/;base64,(.+)$/);
|
|
39
|
+
if (!typeMatch || !pathMatch || !dataMatch || !base64Match) {
|
|
40
|
+
return null;
|
|
41
|
+
}
|
|
42
|
+
// Decode the URL-encoded path
|
|
43
|
+
const decodedPath = decodeURIComponent(pathMatch[1]);
|
|
44
|
+
return {
|
|
45
|
+
formatType: typeMatch[1],
|
|
46
|
+
path: decodedPath,
|
|
47
|
+
media_type: dataMatch[1],
|
|
48
|
+
data: base64Match[1],
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
catch {
|
|
52
|
+
return null;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
function createContentBlock(binaryData) {
|
|
56
|
+
const { formatType, media_type, data } = binaryData;
|
|
57
|
+
return {
|
|
58
|
+
type: formatType,
|
|
59
|
+
source_type: 'base64',
|
|
60
|
+
mime_type: media_type,
|
|
61
|
+
data,
|
|
62
|
+
metadata: {
|
|
63
|
+
filename: path.basename(binaryData.path),
|
|
64
|
+
},
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
function getFormatLabel(formatType) {
|
|
68
|
+
const labels = {
|
|
69
|
+
image: 'image',
|
|
70
|
+
video: 'video',
|
|
71
|
+
audio: 'audio',
|
|
72
|
+
file: 'file',
|
|
73
|
+
};
|
|
74
|
+
return labels[formatType] || 'file';
|
|
75
|
+
}
|
|
76
|
+
export function createBinaryContentInjectionMiddleware(_settings, _gthConfig) {
|
|
77
|
+
debugLog('Creating binary content injection middleware');
|
|
78
|
+
return Promise.resolve(createMiddleware({
|
|
79
|
+
name: 'binary-content-injection',
|
|
80
|
+
// Before next model call, detect and inject HumanMessage with binary content
|
|
81
|
+
beforeModel: async (state) => {
|
|
82
|
+
const messages = state.messages || [];
|
|
83
|
+
// Find recent ToolMessages from gth_read_binary
|
|
84
|
+
const binaryMessages = [];
|
|
85
|
+
// Check last few messages (usually just need to check the most recent)
|
|
86
|
+
for (let i = messages.length - 1; i >= Math.max(0, messages.length - 5); i--) {
|
|
87
|
+
const msg = messages[i];
|
|
88
|
+
if (msg instanceof ToolMessage &&
|
|
89
|
+
msg.name === 'gth_read_binary' &&
|
|
90
|
+
typeof msg.content === 'string') {
|
|
91
|
+
const parsedContent = parseBinaryContent(msg.content);
|
|
92
|
+
if (parsedContent) {
|
|
93
|
+
binaryMessages.push({ message: msg, binaryData: parsedContent });
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
// If we found binary content, inject HumanMessage(s)
|
|
98
|
+
if (binaryMessages.length > 0) {
|
|
99
|
+
debugLog(`Injecting ${binaryMessages.length} HumanMessage(s) with binary content`);
|
|
100
|
+
const newMessages = [...messages];
|
|
101
|
+
for (const { binaryData } of binaryMessages) {
|
|
102
|
+
const formatLabel = getFormatLabel(binaryData.formatType);
|
|
103
|
+
const contentBlock = createContentBlock(binaryData);
|
|
104
|
+
const humanMessage = new HumanMessage({
|
|
105
|
+
content: [
|
|
106
|
+
{
|
|
107
|
+
type: 'text',
|
|
108
|
+
text: `Here is the ${formatLabel} content from the file:`,
|
|
109
|
+
},
|
|
110
|
+
contentBlock,
|
|
111
|
+
],
|
|
112
|
+
});
|
|
113
|
+
newMessages.push(humanMessage);
|
|
114
|
+
}
|
|
115
|
+
// Return the modified state with new messages
|
|
116
|
+
return {
|
|
117
|
+
messages: newMessages,
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
// No binary content, pass through
|
|
121
|
+
return undefined;
|
|
122
|
+
},
|
|
123
|
+
}));
|
|
124
|
+
}
|
|
125
|
+
//# sourceMappingURL=binaryContentInjectionMiddleware.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"binaryContentInjectionMiddleware.js","sourceRoot":"","sources":["../../src/middleware/binaryContentInjectionMiddleware.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AAEH,OAAO,EAAE,gBAAgB,EAAwB,MAAM,WAAW,CAAC;AACnE,OAAO,IAAI,MAAM,WAAW,CAAC;AAE7B,OAAO,EAAE,QAAQ,EAAE,MAAM,uCAAuC,CAAC;AACjE,OAAO,EAAE,WAAW,EAAE,YAAY,EAAE,MAAM,0BAA0B,CAAC;AAcrE;;;;GAIG;AACH,SAAS,kBAAkB,CAAC,OAAe;IACzC,IAAI,CAAC,OAAO,CAAC,UAAU,CAAC,kBAAkB,CAAC,EAAE,CAAC;QAC5C,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CAAC;gBACH,CAAC;YACH,CAAC;YAED,qDAAqD;YACrD,IAAI,cAAc,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;gBAC9B,QAAQ,CAAC,aAAa,cAAc,CAAC,MAAM,sCAAsC,CAAC,CAAC;gBAEnF,MAAM,WAAW,GAAG,CAAC,GAAG,QAAQ,CAAC,CAAC;gBAElC,KAAK,MAAM,EAAE,UAAU,EAAE,IAAI,cAAc,EAAE,CAAC;oBAC5C,MAAM,WAAW,GAAG,cAAc,CAAC,UAAU,CAAC,UAAU,CAAC,CAAC;oBAC1D,MAAM,YAAY,GAAG,kBAAkB,CAAC,UAAU,CAAC,CAAC;oBAEpD,MAAM,YAAY,GAAG,IAAI,YAAY,CAAC;wBACpC,OAAO,EAAE;4BACP;gCACE,IAAI,EAAE,MAAM;gCACZ,IAAI,EAAE,eAAe,WAAW,yBAAyB;6BAC1D;4BACD,YAAY;yBACK;qBACpB,CAAC,CAAC;oBAEH,WAAW,CAAC,IAAI,CAAC,YAAY,CAAC,CAAC;gBACjC,CAAC;gBAED,8CAA8C;gBAC9C,OAAO;oBACL,QAAQ,EAAE,WAAW;iBACtB,CAAC;YACJ,CAAC;YAED,kCAAkC;YAClC,OAAO,SAAS,CAAC;QACnB,CAAC;KACF,CAAC,CACH,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @packageDocumentation
|
|
3
|
+
* Middleware registry for Gaunt Sloth Assistant.
|
|
4
|
+
*
|
|
5
|
+
* This module provides factory functions for creating predefined middleware instances
|
|
6
|
+
* and a resolver to convert middleware configurations into middleware objects.
|
|
7
|
+
*/
|
|
8
|
+
import type { GthConfig } from '@gaunt-sloth/core/config.js';
|
|
9
|
+
import type { AnthropicPromptCachingConfig, MiddlewareConfig, SummarizationConfig } from '#src/middleware/types.js';
|
|
10
|
+
import { type AgentMiddleware } from 'langchain';
|
|
11
|
+
/**
|
|
12
|
+
* Create Anthropic prompt caching middleware.
|
|
13
|
+
* This middleware adds cache control headers to reduce API costs.
|
|
14
|
+
*/
|
|
15
|
+
export declare function createAnthropicPromptCachingMiddleware(config: AnthropicPromptCachingConfig, _: GthConfig): Promise<AgentMiddleware>;
|
|
16
|
+
/**
|
|
17
|
+
* Create summarization middleware.
|
|
18
|
+
* This middleware automatically condenses conversation history when approaching token limits.
|
|
19
|
+
*
|
|
20
|
+
* @param config - Configuration for the middleware
|
|
21
|
+
* @param gthConfig - Full Gaunt Sloth configuration
|
|
22
|
+
* @returns Middleware object
|
|
23
|
+
*/
|
|
24
|
+
export declare function createSummarizationMiddleware(config: SummarizationConfig, gthConfig: GthConfig): Promise<AgentMiddleware>;
|
|
25
|
+
/**
|
|
26
|
+
* Resolve middleware configuration into middleware instances.
|
|
27
|
+
* Converts string identifiers and config objects into actual middleware.
|
|
28
|
+
* Automatically injects binary-content-injection middleware if binaryFormats is enabled.
|
|
29
|
+
*
|
|
30
|
+
* @param configs - Array of middleware configurations
|
|
31
|
+
* @param gthConfig - Full Gaunt Sloth configuration
|
|
32
|
+
* @returns Array of middleware instances
|
|
33
|
+
*/
|
|
34
|
+
export declare function resolveMiddleware(configs: MiddlewareConfig[] | undefined, gthConfig: GthConfig): Promise<AgentMiddleware[]>;
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @packageDocumentation
|
|
3
|
+
* Middleware registry for Gaunt Sloth Assistant.
|
|
4
|
+
*
|
|
5
|
+
* This module provides factory functions for creating predefined middleware instances
|
|
6
|
+
* and a resolver to convert middleware configurations into middleware objects.
|
|
7
|
+
*/
|
|
8
|
+
import { displayWarning } from '@gaunt-sloth/core/utils/consoleUtils.js';
|
|
9
|
+
import { debugLog } from '@gaunt-sloth/core/utils/debugUtils.js';
|
|
10
|
+
import { anthropicPromptCachingMiddleware, summarizationMiddleware, } from 'langchain';
|
|
11
|
+
import { createBinaryContentInjectionMiddleware, } from '#src/middleware/binaryContentInjectionMiddleware.js';
|
|
12
|
+
const predefinedMiddlewareFactories = {
|
|
13
|
+
/**
|
|
14
|
+
* Anthropic prompt caching middleware. see https://docs.langchain.com/oss/javascript/langchain/middleware#anthropic-prompt-caching
|
|
15
|
+
*/
|
|
16
|
+
'anthropic-prompt-caching': (settings, gthConfig) => createAnthropicPromptCachingMiddleware(settings, gthConfig),
|
|
17
|
+
/**
|
|
18
|
+
* Summarization middleware. see https://docs.langchain.com/oss/javascript/langchain/middleware#summarization
|
|
19
|
+
*/
|
|
20
|
+
summarization: (settings, gthConfig) => createSummarizationMiddleware(settings, gthConfig),
|
|
21
|
+
/**
|
|
22
|
+
* Binary content injection middleware.
|
|
23
|
+
* Intercepts tool results containing binary data (images, PDFs, audio) and injects them
|
|
24
|
+
* as HumanMessage content blocks before the next model call.
|
|
25
|
+
* This works around LangChain's limitation where ToolMessage doesn't support binary content.
|
|
26
|
+
*/
|
|
27
|
+
'binary-content-injection': (settings, gthConfig) => createBinaryContentInjectionMiddleware(settings, gthConfig),
|
|
28
|
+
};
|
|
29
|
+
function isPredefinedMiddlewareName(name) {
|
|
30
|
+
return name in predefinedMiddlewareFactories;
|
|
31
|
+
}
|
|
32
|
+
function isPredefinedMiddlewareObject(config) {
|
|
33
|
+
return (typeof config === 'object' &&
|
|
34
|
+
config !== null &&
|
|
35
|
+
'name' in config &&
|
|
36
|
+
typeof config.name === 'string' &&
|
|
37
|
+
isPredefinedMiddlewareName(config.name));
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Create Anthropic prompt caching middleware.
|
|
41
|
+
* This middleware adds cache control headers to reduce API costs.
|
|
42
|
+
*/
|
|
43
|
+
export async function createAnthropicPromptCachingMiddleware(config, _) {
|
|
44
|
+
debugLog(`Creating Anthropic prompt caching middleware with TTL: ${config.ttl || 'default'}`);
|
|
45
|
+
// Dynamic import for async initialization
|
|
46
|
+
return Promise.resolve(anthropicPromptCachingMiddleware({ ttl: config.ttl }));
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Create summarization middleware.
|
|
50
|
+
* This middleware automatically condenses conversation history when approaching token limits.
|
|
51
|
+
*
|
|
52
|
+
* @param config - Configuration for the middleware
|
|
53
|
+
* @param gthConfig - Full Gaunt Sloth configuration
|
|
54
|
+
* @returns Middleware object
|
|
55
|
+
*/
|
|
56
|
+
export async function createSummarizationMiddleware(config, gthConfig) {
|
|
57
|
+
debugLog('Creating summarization middleware');
|
|
58
|
+
return Promise.resolve(summarizationMiddleware({
|
|
59
|
+
model: config.model || gthConfig.llm,
|
|
60
|
+
...config,
|
|
61
|
+
}));
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Resolve middleware configuration into middleware instances.
|
|
65
|
+
* Converts string identifiers and config objects into actual middleware.
|
|
66
|
+
* Automatically injects binary-content-injection middleware if binaryFormats is enabled.
|
|
67
|
+
*
|
|
68
|
+
* @param configs - Array of middleware configurations
|
|
69
|
+
* @param gthConfig - Full Gaunt Sloth configuration
|
|
70
|
+
* @returns Array of middleware instances
|
|
71
|
+
*/
|
|
72
|
+
export async function resolveMiddleware(configs, gthConfig) {
|
|
73
|
+
const configsToResolve = configs || [];
|
|
74
|
+
const middleware = [];
|
|
75
|
+
// Auto-inject binary-content-injection middleware if binaryFormats is configured
|
|
76
|
+
// It is only auto-injected if binaryFormats is enabled.
|
|
77
|
+
const hasBinaryFormats = gthConfig.binaryFormats !== undefined &&
|
|
78
|
+
gthConfig.binaryFormats !== false &&
|
|
79
|
+
gthConfig.binaryFormats.length > 0;
|
|
80
|
+
const hasBinaryMiddleware = configsToResolve.some((c) => c === 'binary-content-injection' ||
|
|
81
|
+
(typeof c === 'object' && 'name' in c && c.name === 'binary-content-injection'));
|
|
82
|
+
if (hasBinaryFormats && !hasBinaryMiddleware) {
|
|
83
|
+
debugLog('Auto-injecting binary-content-injection middleware (binaryFormats is enabled)');
|
|
84
|
+
middleware.push(await createPredefinedMiddleware('binary-content-injection', {}, gthConfig));
|
|
85
|
+
}
|
|
86
|
+
for (const config of configsToResolve) {
|
|
87
|
+
try {
|
|
88
|
+
// Handle string configuration (predefined middleware with defaults)
|
|
89
|
+
if (typeof config === 'string') {
|
|
90
|
+
middleware.push(await createPredefinedMiddleware(config, {}, gthConfig));
|
|
91
|
+
}
|
|
92
|
+
// Handle predefined middleware with custom settings
|
|
93
|
+
else if (isPredefinedMiddlewareObject(config)) {
|
|
94
|
+
const { name, ...settings } = config;
|
|
95
|
+
middleware.push(await createPredefinedMiddleware(name, settings, gthConfig));
|
|
96
|
+
}
|
|
97
|
+
// Handle custom middleware object (JS config only)
|
|
98
|
+
else {
|
|
99
|
+
debugLog('Adding custom middleware');
|
|
100
|
+
middleware.push(config);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
catch (error) {
|
|
104
|
+
displayWarning(`Failed to create middleware: ${error instanceof Error ? error.message : String(error)}`);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
return middleware;
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Create a predefined middleware instance by name.
|
|
111
|
+
*
|
|
112
|
+
* @param name - Name of the predefined middleware
|
|
113
|
+
* @param settings - Configuration settings for the middleware
|
|
114
|
+
* @param gthConfig - Full Gaunt Sloth configuration
|
|
115
|
+
* @returns Middleware instance
|
|
116
|
+
*/
|
|
117
|
+
async function createPredefinedMiddleware(name, settings, gthConfig) {
|
|
118
|
+
if (!isPredefinedMiddlewareName(name)) {
|
|
119
|
+
throw new Error(`Unknown predefined middleware: ${name}`);
|
|
120
|
+
}
|
|
121
|
+
const factory = predefinedMiddlewareFactories[name];
|
|
122
|
+
return factory(settings, gthConfig);
|
|
123
|
+
}
|
|
124
|
+
//# sourceMappingURL=registry.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"registry.js","sourceRoot":"","sources":["../../src/middleware/registry.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AASH,OAAO,EAAE,cAAc,EAAE,MAAM,yCAAyC,CAAC;AACzE,OAAO,EAAE,QA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E,CAAC;YACD,oDAAoD;iBAC/C,IAAI,4BAA4B,CAAC,MAAM,CAAC,EAAE,CAAC;gBAC9C,MAAM,EAAE,IAAI,EAAE,GAAG,QAAQ,EAAE,GAAG,MAAM,CAAC;gBACrC,UAAU,CAAC,IAAI,CAAC,MAAM,0BAA0B,CAAC,IAAI,EAAE,QAAQ,EAAE,SAAS,CAAC,CAAC,CAAC;YAC/E,CAAC;YACD,mDAAmD;iBAC9C,CAAC;gBACJ,QAAQ,CAAC,0BAA0B,CAAC,CAAC;gBACrC,UAAU,CAAC,IAAI,CAAC,MAAyB,CAAC,CAAC;YAC7C,CAAC;QACH,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,cAAc,CACZ,gCAAgC,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,EAAE,CACzF,CAAC;QACJ,CAAC;IACH,CAAC;IAED,OAAO,UAAU,CAAC;AACpB,CAAC;AAED;;;;;;;GAOG;AACH,KAAK,UAAU,0BAA0B,CACvC,IAAY,EACZ,QAAiC,EACjC,SAAoB;IAEpB,IAAI,CAAC,0BAA0B,CAAC,IAAI,CAAC,EAAE,CAAC;QACtC,MAAM,IAAI,KAAK,CAAC,kCAAkC,IAAI,EAAE,CAAC,CAAC;IAC5D,CAAC;IAED,MAAM,OAAO,GAAG,6BAA6B,CAAC,IAAI,CAAC,CAAC;IACpD,OAAO,OAAO,CAAC,QAAQ,EAAE,SAAS,CAAC,CAAC;AACtC,CAAC"}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @packageDocumentation
|
|
3
|
+
* Type definitions for middleware configuration in Gaunt Sloth Assistant.
|
|
4
|
+
*
|
|
5
|
+
* Middleware provides hooks to intercept and control agent execution at critical points.
|
|
6
|
+
* This module defines the configuration interfaces for both predefined and custom middleware.
|
|
7
|
+
*/
|
|
8
|
+
import type { BaseChatModel } from '@langchain/core/language_models/chat_models';
|
|
9
|
+
import { AgentMiddleware } from 'langchain';
|
|
10
|
+
/**
|
|
11
|
+
* Predefined middleware types that can be configured via JSON config.
|
|
12
|
+
*/
|
|
13
|
+
export type PredefinedMiddlewareName = 'anthropic-prompt-caching' | 'summarization' | 'binary-content-injection';
|
|
14
|
+
/**
|
|
15
|
+
* Configuration for Anthropic prompt caching middleware.
|
|
16
|
+
*/
|
|
17
|
+
export interface AnthropicPromptCachingConfig {
|
|
18
|
+
/**
|
|
19
|
+
* Cache TTL (time to live).
|
|
20
|
+
* Examples: "5m" for 5 minutes, "1h" for 1 hour
|
|
21
|
+
*/
|
|
22
|
+
ttl?: '5m' | '1h';
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Configuration for summarization middleware.
|
|
26
|
+
*/
|
|
27
|
+
export interface SummarizationConfig {
|
|
28
|
+
/**
|
|
29
|
+
* Model to use for summarization.
|
|
30
|
+
* If not provided, uses the main LLM from config.
|
|
31
|
+
*/
|
|
32
|
+
model?: BaseChatModel;
|
|
33
|
+
/**
|
|
34
|
+
* Maximum tokens before triggering summarization.
|
|
35
|
+
* Set one of:
|
|
36
|
+
*/
|
|
37
|
+
trigger?: {
|
|
38
|
+
fraction?: number;
|
|
39
|
+
tokens?: number;
|
|
40
|
+
messages?: number;
|
|
41
|
+
};
|
|
42
|
+
/**
|
|
43
|
+
* How many tokens, messages, or a fraction of context to keep.
|
|
44
|
+
* Set one of:
|
|
45
|
+
*/
|
|
46
|
+
keep?: {
|
|
47
|
+
fraction?: number;
|
|
48
|
+
tokens?: number;
|
|
49
|
+
messages?: number;
|
|
50
|
+
};
|
|
51
|
+
/**
|
|
52
|
+
* Custom prompt template for summarization.
|
|
53
|
+
*/
|
|
54
|
+
summaryPrompt?: string;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Configuration for image format transformation middleware.
|
|
58
|
+
*/
|
|
59
|
+
export interface ImageFormatTransformConfig {
|
|
60
|
+
/**
|
|
61
|
+
* Detail level for image processing.
|
|
62
|
+
* Examples: "low", "high", "auto"
|
|
63
|
+
*/
|
|
64
|
+
detail?: 'low' | 'high' | 'auto';
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Configuration for binary content injection middleware.
|
|
68
|
+
* This middleware intercepts tool results containing binary data (images, PDFs, audio)
|
|
69
|
+
* and injects them as HumanMessage content blocks.
|
|
70
|
+
*/
|
|
71
|
+
export interface BinaryContentInjectionConfig {
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Union type of all predefined middleware configurations.
|
|
75
|
+
*/
|
|
76
|
+
export type PredefinedMiddlewareConfig = ({
|
|
77
|
+
name: 'anthropic-prompt-caching';
|
|
78
|
+
} & AnthropicPromptCachingConfig) | ({
|
|
79
|
+
name: 'summarization';
|
|
80
|
+
} & SummarizationConfig) | ({
|
|
81
|
+
name: 'binary-content-injection';
|
|
82
|
+
} & BinaryContentInjectionConfig);
|
|
83
|
+
/**
|
|
84
|
+
* Middleware configuration that can be specified in JSON or JS config.
|
|
85
|
+
* - String: Name of predefined middleware with default settings
|
|
86
|
+
* - PredefinedMiddlewareConfig: Predefined middleware with custom settings (JSON compatible)
|
|
87
|
+
* - CustomMiddleware: Custom middleware object (JS config only)
|
|
88
|
+
*/
|
|
89
|
+
export type MiddlewareConfig = string | PredefinedMiddlewareConfig | AgentMiddleware;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @packageDocumentation
|
|
3
|
+
* Type definitions for middleware configuration in Gaunt Sloth Assistant.
|
|
4
|
+
*
|
|
5
|
+
* Middleware provides hooks to intercept and control agent execution at critical points.
|
|
6
|
+
* This module defines the configuration interfaces for both predefined and custom middleware.
|
|
7
|
+
*/
|
|
8
|
+
export {};
|
|
9
|
+
//# sourceMappingURL=types.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/middleware/types.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG"}
|