@lvce-editor/chat-tool-worker 2.1.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/LICENSE +21 -0
- package/README.md +3 -0
- package/dist/chatToolWorkerMain.js +158 -0
- package/package.json +13 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Lvce Editor
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
import { MessagePortRpcClient, WebWorkerRpcClient } from '@lvce-editor/rpc';
|
|
2
|
+
|
|
3
|
+
const getToolErrorResponse = (toolCallId, error) => {
|
|
4
|
+
return {
|
|
5
|
+
content: JSON.stringify({
|
|
6
|
+
error,
|
|
7
|
+
type: 'error'
|
|
8
|
+
}),
|
|
9
|
+
role: 'tool',
|
|
10
|
+
tool_call_id: toolCallId
|
|
11
|
+
};
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
const getToolSuccessResponse = (toolCallId, result) => {
|
|
15
|
+
return {
|
|
16
|
+
content: JSON.stringify({
|
|
17
|
+
result,
|
|
18
|
+
type: 'success'
|
|
19
|
+
}),
|
|
20
|
+
role: 'tool',
|
|
21
|
+
tool_call_id: toolCallId
|
|
22
|
+
};
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
const parseToolCallArguments = rawArguments => {
|
|
26
|
+
if (typeof rawArguments !== 'string') {
|
|
27
|
+
return {
|
|
28
|
+
message: 'Tool arguments must be a JSON string.',
|
|
29
|
+
type: 'error'
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
try {
|
|
33
|
+
const parsed = JSON.parse(rawArguments);
|
|
34
|
+
if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) {
|
|
35
|
+
return {
|
|
36
|
+
message: 'Tool arguments must decode to a JSON object.',
|
|
37
|
+
type: 'error'
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
return {
|
|
41
|
+
type: 'success',
|
|
42
|
+
value: parsed
|
|
43
|
+
};
|
|
44
|
+
} catch {
|
|
45
|
+
return {
|
|
46
|
+
message: 'Tool arguments are not valid JSON.',
|
|
47
|
+
type: 'error'
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
const toolArgumentSchemas = {
|
|
53
|
+
list_files: {
|
|
54
|
+
additionalProperties: false,
|
|
55
|
+
properties: {
|
|
56
|
+
path: 'string'
|
|
57
|
+
},
|
|
58
|
+
required: []
|
|
59
|
+
},
|
|
60
|
+
read_file: {
|
|
61
|
+
additionalProperties: false,
|
|
62
|
+
properties: {
|
|
63
|
+
path: 'string'
|
|
64
|
+
},
|
|
65
|
+
required: ['path']
|
|
66
|
+
},
|
|
67
|
+
write_file: {
|
|
68
|
+
additionalProperties: false,
|
|
69
|
+
properties: {
|
|
70
|
+
content: 'string',
|
|
71
|
+
path: 'string'
|
|
72
|
+
},
|
|
73
|
+
required: ['path', 'content']
|
|
74
|
+
}
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
const verifyToolCallArguments = (name, args) => {
|
|
78
|
+
const schema = toolArgumentSchemas[name];
|
|
79
|
+
if (!schema) {
|
|
80
|
+
return {
|
|
81
|
+
message: `Unknown tool: ${name}`,
|
|
82
|
+
type: 'error'
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
for (const requiredKey of schema.required) {
|
|
86
|
+
if (!(requiredKey in args)) {
|
|
87
|
+
return {
|
|
88
|
+
message: `Missing required argument: ${requiredKey}`,
|
|
89
|
+
type: 'error'
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
for (const [key, value] of Object.entries(args)) {
|
|
94
|
+
const expectedType = schema.properties[key];
|
|
95
|
+
if (!expectedType) {
|
|
96
|
+
if (schema.additionalProperties) {
|
|
97
|
+
continue;
|
|
98
|
+
}
|
|
99
|
+
return {
|
|
100
|
+
message: `Unexpected argument: ${key}`,
|
|
101
|
+
type: 'error'
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
if (typeof value !== expectedType) {
|
|
105
|
+
return {
|
|
106
|
+
message: `Invalid argument type for ${key}: expected ${expectedType}`,
|
|
107
|
+
type: 'error'
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
return {
|
|
112
|
+
type: 'success',
|
|
113
|
+
value: args
|
|
114
|
+
};
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
const handleToolCall = async (toolCallId, name, rawArguments) => {
|
|
118
|
+
const parsed = parseToolCallArguments(rawArguments);
|
|
119
|
+
if (parsed.type === 'error') {
|
|
120
|
+
return getToolErrorResponse(toolCallId, parsed.message);
|
|
121
|
+
}
|
|
122
|
+
const verified = verifyToolCallArguments(name, parsed.value);
|
|
123
|
+
if (verified.type === 'error') {
|
|
124
|
+
return getToolErrorResponse(toolCallId, verified.message);
|
|
125
|
+
}
|
|
126
|
+
return getToolSuccessResponse(toolCallId, {
|
|
127
|
+
arguments: verified.value,
|
|
128
|
+
name
|
|
129
|
+
});
|
|
130
|
+
};
|
|
131
|
+
|
|
132
|
+
const toolCommandMap = {
|
|
133
|
+
'ChatTool.handleToolCall': handleToolCall
|
|
134
|
+
};
|
|
135
|
+
|
|
136
|
+
const handleMessagePort = async port => {
|
|
137
|
+
await MessagePortRpcClient.create({
|
|
138
|
+
commandMap: toolCommandMap,
|
|
139
|
+
messagePort: port
|
|
140
|
+
});
|
|
141
|
+
};
|
|
142
|
+
|
|
143
|
+
const commandMap = {
|
|
144
|
+
...toolCommandMap,
|
|
145
|
+
'HandleMessagePort.handleMessagePort': handleMessagePort
|
|
146
|
+
};
|
|
147
|
+
|
|
148
|
+
const listen = async () => {
|
|
149
|
+
await WebWorkerRpcClient.create({
|
|
150
|
+
commandMap: commandMap
|
|
151
|
+
});
|
|
152
|
+
};
|
|
153
|
+
|
|
154
|
+
const main = async () => {
|
|
155
|
+
await listen();
|
|
156
|
+
};
|
|
157
|
+
|
|
158
|
+
main();
|
package/package.json
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@lvce-editor/chat-tool-worker",
|
|
3
|
+
"version": "2.1.0",
|
|
4
|
+
"description": "Chat Tool Worker",
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "git+https://github.com/lvce-editor/chat-view.git"
|
|
8
|
+
},
|
|
9
|
+
"license": "MIT",
|
|
10
|
+
"author": "Lvce Editor",
|
|
11
|
+
"type": "module",
|
|
12
|
+
"main": "dist/chatToolWorkerMain.js"
|
|
13
|
+
}
|