@yuhere/js-jsonrpc2 2.1.4 → 2.1.6
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/core/llm-tools.d.ts +34 -0
- package/dist/core/llm-tools.d.ts.map +1 -0
- package/dist/core/llm-tools.js +179 -0
- package/dist/core/llm-tools.js.map +1 -0
- package/dist/core/permission.d.ts +1 -0
- package/dist/core/permission.d.ts.map +1 -1
- package/dist/core/permission.js +12 -1
- package/dist/core/permission.js.map +1 -1
- package/dist/core/system-methods.d.ts.map +1 -1
- package/dist/core/system-methods.js +29 -14
- package/dist/core/system-methods.js.map +1 -1
- package/package.json +2 -2
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import type { Schema } from '../schema/json/primitives.js';
|
|
2
|
+
import type { Repository } from './Repository.js';
|
|
3
|
+
export interface JSONSchema {
|
|
4
|
+
type?: string | string[];
|
|
5
|
+
description?: string;
|
|
6
|
+
enum?: unknown[];
|
|
7
|
+
const?: unknown;
|
|
8
|
+
minimum?: number;
|
|
9
|
+
maximum?: number;
|
|
10
|
+
minLength?: number;
|
|
11
|
+
maxLength?: number;
|
|
12
|
+
minItems?: number;
|
|
13
|
+
maxItems?: number;
|
|
14
|
+
minProperties?: number;
|
|
15
|
+
maxProperties?: number;
|
|
16
|
+
items?: JSONSchema | JSONSchema[];
|
|
17
|
+
properties?: Record<string, JSONSchema>;
|
|
18
|
+
required?: string[];
|
|
19
|
+
additionalProperties?: boolean | JSONSchema;
|
|
20
|
+
anyOf?: JSONSchema[];
|
|
21
|
+
nullable?: boolean;
|
|
22
|
+
}
|
|
23
|
+
export declare function schemaToJSONSchema(schema: Schema<unknown>): JSONSchema;
|
|
24
|
+
type Tools = Array<{
|
|
25
|
+
type: 'function';
|
|
26
|
+
function: {
|
|
27
|
+
name: string;
|
|
28
|
+
description: string;
|
|
29
|
+
parameters: Record<string, unknown>;
|
|
30
|
+
};
|
|
31
|
+
}>;
|
|
32
|
+
export declare function generateLlmTools(repository: Repository, permCheck: unknown, rawOptions: unknown): Promise<Tools>;
|
|
33
|
+
export {};
|
|
34
|
+
//# sourceMappingURL=llm-tools.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"llm-tools.d.ts","sourceRoot":"","sources":["../../src/core/llm-tools.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAqB,MAAM,2BAA2B,CAAC;AAI3E,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAG/C,MAAM,WAAW,UAAU;IACvB,IAAI,CAAC,EAAE,MAAM,GAAG,MAAM,EAAE,CAAC;IACzB,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,IAAI,CAAC,EAAE,OAAO,EAAE,CAAC;IACjB,KAAK,CAAC,EAAE,OAAO,CAAC;IAChB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,KAAK,CAAC,EAAE,UAAU,GAAG,UAAU,EAAE,CAAC;IAClC,UAAU,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,UAAU,CAAC,CAAC;IACxC,QAAQ,CAAC,EAAE,MAAM,EAAE,CAAC;IACpB,oBAAoB,CAAC,EAAE,OAAO,GAAG,UAAU,CAAC;IAC5C,KAAK,CAAC,EAAE,UAAU,EAAE,CAAC;IACrB,QAAQ,CAAC,EAAE,OAAO,CAAC;CACtB;AAED,wBAAgB,kBAAkB,CAAC,MAAM,EAAE,MAAM,CAAC,OAAO,CAAC,GAAG,UAAU,CAEtE;AAsGD,KAAK,KAAK,GAAG,KAAK,CAAC;IACf,IAAI,EAAE,UAAU,CAAC;IACjB,QAAQ,EAAE;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,WAAW,EAAE,MAAM,CAAC;QAAC,UAAU,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;KAAE,CAAC;CACxF,CAAC,CAAC;AAEH,wBAAsB,gBAAgB,CAClC,UAAU,EAAE,UAAU,EACtB,SAAS,EAAE,OAAO,EAClB,UAAU,EAAE,OAAO,GACpB,OAAO,CAAC,KAAK,CAAC,CAuEhB"}
|
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
import { RPCError } from '../errors/RPCError.js';
|
|
2
|
+
import { METHOD_NOT_FOUND } from '../errors/codes.js';
|
|
3
|
+
import { assertAccessible } from './permission.js';
|
|
4
|
+
import { InjectableMarker } from "../schema/json/index.js";
|
|
5
|
+
export function schemaToJSONSchema(schema) {
|
|
6
|
+
return convert(schema.toJSON());
|
|
7
|
+
}
|
|
8
|
+
function convert(desc) {
|
|
9
|
+
const out = {};
|
|
10
|
+
if (desc.description)
|
|
11
|
+
out.description = desc.description;
|
|
12
|
+
switch (desc.kind) {
|
|
13
|
+
case 'string': {
|
|
14
|
+
out.type = 'string';
|
|
15
|
+
const c = (desc.constraints ?? {});
|
|
16
|
+
if (c.min !== undefined)
|
|
17
|
+
out.minLength = c.min;
|
|
18
|
+
if (c.max !== undefined)
|
|
19
|
+
out.maxLength = c.max;
|
|
20
|
+
break;
|
|
21
|
+
}
|
|
22
|
+
case 'number': {
|
|
23
|
+
out.type = 'number';
|
|
24
|
+
const c = (desc.constraints ?? {});
|
|
25
|
+
if (c.min !== undefined)
|
|
26
|
+
out.minimum = c.min;
|
|
27
|
+
if (c.max !== undefined)
|
|
28
|
+
out.maximum = c.max;
|
|
29
|
+
break;
|
|
30
|
+
}
|
|
31
|
+
case 'boolean':
|
|
32
|
+
out.type = 'boolean';
|
|
33
|
+
break;
|
|
34
|
+
case 'literal': {
|
|
35
|
+
const value = desc.constraints.value;
|
|
36
|
+
out.const = value;
|
|
37
|
+
out.type = value === null ? 'null' : typeof value;
|
|
38
|
+
break;
|
|
39
|
+
}
|
|
40
|
+
case 'enum': {
|
|
41
|
+
out.type = 'string';
|
|
42
|
+
out.enum = desc.constraints.values;
|
|
43
|
+
break;
|
|
44
|
+
}
|
|
45
|
+
case 'array': {
|
|
46
|
+
out.type = 'array';
|
|
47
|
+
const c = (desc.constraints ?? {});
|
|
48
|
+
if (c.min !== undefined)
|
|
49
|
+
out.minItems = c.min;
|
|
50
|
+
if (c.max !== undefined)
|
|
51
|
+
out.maxItems = c.max;
|
|
52
|
+
out.items = convert(desc.children.items);
|
|
53
|
+
break;
|
|
54
|
+
}
|
|
55
|
+
case 'tuple': {
|
|
56
|
+
out.type = 'array';
|
|
57
|
+
const items = desc.children.items;
|
|
58
|
+
out.items = items.map(convert);
|
|
59
|
+
out.minItems = items.length;
|
|
60
|
+
out.maxItems = items.length;
|
|
61
|
+
break;
|
|
62
|
+
}
|
|
63
|
+
case 'object': {
|
|
64
|
+
out.type = 'object';
|
|
65
|
+
const strict = Boolean(desc.constraints.strict);
|
|
66
|
+
const props = desc.children
|
|
67
|
+
.properties;
|
|
68
|
+
out.properties = {};
|
|
69
|
+
for (const [k, v] of Object.entries(props))
|
|
70
|
+
out.properties[k] = convert(v);
|
|
71
|
+
if (strict) {
|
|
72
|
+
out.required = Object.keys(props);
|
|
73
|
+
out.additionalProperties = false;
|
|
74
|
+
}
|
|
75
|
+
else {
|
|
76
|
+
out.additionalProperties = true;
|
|
77
|
+
}
|
|
78
|
+
break;
|
|
79
|
+
}
|
|
80
|
+
case 'record': {
|
|
81
|
+
out.type = 'object';
|
|
82
|
+
const c = (desc.constraints ?? {});
|
|
83
|
+
if (c.min !== undefined)
|
|
84
|
+
out.minProperties = c.min;
|
|
85
|
+
if (c.max !== undefined)
|
|
86
|
+
out.maxProperties = c.max;
|
|
87
|
+
out.additionalProperties = convert(desc.children.values);
|
|
88
|
+
break;
|
|
89
|
+
}
|
|
90
|
+
case 'union': {
|
|
91
|
+
const options = desc.children.options;
|
|
92
|
+
out.anyOf = options.map(convert);
|
|
93
|
+
break;
|
|
94
|
+
}
|
|
95
|
+
case 'nullable': {
|
|
96
|
+
const inner = convert(desc.children.inner);
|
|
97
|
+
if (typeof inner.type === 'string') {
|
|
98
|
+
inner.type = [inner.type, 'null'];
|
|
99
|
+
}
|
|
100
|
+
else if (Array.isArray(inner.type)) {
|
|
101
|
+
inner.type = [...inner.type, 'null'];
|
|
102
|
+
}
|
|
103
|
+
else {
|
|
104
|
+
inner.nullable = true;
|
|
105
|
+
}
|
|
106
|
+
return inner;
|
|
107
|
+
}
|
|
108
|
+
case 'unknown': {
|
|
109
|
+
out.type = ['object', 'array', 'string', 'number', 'boolean', 'null'];
|
|
110
|
+
break;
|
|
111
|
+
}
|
|
112
|
+
default:
|
|
113
|
+
throw new Error(`Unsupported schema kind for JSON Schema: ${desc.kind}`);
|
|
114
|
+
}
|
|
115
|
+
return out;
|
|
116
|
+
}
|
|
117
|
+
export async function generateLlmTools(repository, permCheck, rawOptions) {
|
|
118
|
+
const opts = (rawOptions ?? {});
|
|
119
|
+
const { includeStreaming = false, namePrefix = '', prefix } = opts;
|
|
120
|
+
const tools = [];
|
|
121
|
+
for (const name of repository.list()) {
|
|
122
|
+
if (prefix && !name.startsWith(prefix))
|
|
123
|
+
continue;
|
|
124
|
+
const descriptor = repository.get(name);
|
|
125
|
+
if (!descriptor)
|
|
126
|
+
continue;
|
|
127
|
+
// 权限过滤:不可见的方法直接跳过(与 listMethods 语义一致)
|
|
128
|
+
try {
|
|
129
|
+
await assertAccessible(descriptor, permCheck);
|
|
130
|
+
}
|
|
131
|
+
catch (error) {
|
|
132
|
+
if (error instanceof RPCError && error.code === METHOD_NOT_FOUND)
|
|
133
|
+
continue;
|
|
134
|
+
throw error;
|
|
135
|
+
}
|
|
136
|
+
const isStream = descriptor.shouldStreamReturn;
|
|
137
|
+
if (isStream && !includeStreaming)
|
|
138
|
+
continue;
|
|
139
|
+
// 收集客户端参数,跳过 injectable
|
|
140
|
+
const properties = {};
|
|
141
|
+
const required = [];
|
|
142
|
+
let paramIndex = 0;
|
|
143
|
+
for (let i = 1; i < descriptor.signature.length; i++) {
|
|
144
|
+
const slot = descriptor.signature[i];
|
|
145
|
+
if (slot instanceof InjectableMarker)
|
|
146
|
+
continue;
|
|
147
|
+
const key = `param${paramIndex}`;
|
|
148
|
+
properties[key] = schemaToJSONSchema(slot);
|
|
149
|
+
required.push(key);
|
|
150
|
+
paramIndex++;
|
|
151
|
+
}
|
|
152
|
+
// description:document + 流式说明 + 无参说明
|
|
153
|
+
const parts = [];
|
|
154
|
+
const doc = descriptor.document;
|
|
155
|
+
const docText = typeof doc === 'function' ? ((await doc(descriptor)) ?? '') : (doc ?? '');
|
|
156
|
+
if (docText)
|
|
157
|
+
parts.push(docText);
|
|
158
|
+
if (isStream) {
|
|
159
|
+
parts.push('(Returns a stream of results; the host application consumes it incrementally and the LLM only receives the aggregated result.)');
|
|
160
|
+
}
|
|
161
|
+
if (paramIndex === 0)
|
|
162
|
+
parts.push('(This method takes no parameters.)');
|
|
163
|
+
tools.push({
|
|
164
|
+
type: 'function',
|
|
165
|
+
function: {
|
|
166
|
+
name: `${namePrefix}${name}`,
|
|
167
|
+
description: parts.join('\n\n') || name,
|
|
168
|
+
parameters: {
|
|
169
|
+
type: 'object',
|
|
170
|
+
properties,
|
|
171
|
+
required,
|
|
172
|
+
additionalProperties: false,
|
|
173
|
+
},
|
|
174
|
+
},
|
|
175
|
+
});
|
|
176
|
+
}
|
|
177
|
+
return tools;
|
|
178
|
+
}
|
|
179
|
+
//# sourceMappingURL=llm-tools.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"llm-tools.js","sourceRoot":"","sources":["../../src/core/llm-tools.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,QAAQ,EAAE,MAAM,oBAAoB,CAAC;AAC9C,OAAO,EAAE,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QAChC,MAAM,OAAO,GACT,OAAO,GAAG,KAAK,UAAU,CAAC,CAAC,CAAC,CAAC,CAAC,MAAM,GAAG,CAAC,UAAmB,CAAC,CAAC,IAAI,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,IAAI,EAAE,CAAC,CAAC;QACvF,IAAI,OAAO;YAAE,KAAK,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;QACjC,IAAI,QAAQ,EAAE,CAAC;YACX,KAAK,CAAC,IAAI,CACN,gIAAgI,CACnI,CAAC;QACN,CAAC;QACD,IAAI,UAAU,KAAK,CAAC;YAAE,KAAK,CAAC,IAAI,CAAC,oCAAoC,CAAC,CAAC;QAEvE,KAAK,CAAC,IAAI,CAAC;YACP,IAAI,EAAE,UAAU;YAChB,QAAQ,EAAE;gBACN,IAAI,EAAE,GAAG,UAAU,GAAG,IAAI,EAAE;gBAC5B,WAAW,EAAE,KAAK,CAAC,IAAI,CAAC,MAAM,CAAC,IAAI,IAAI;gBACvC,UAAU,EAAE;oBACR,IAAI,EAAE,QAAQ;oBACd,UAAU;oBACV,QAAQ;oBACR,oBAAoB,EAAE,KAAK;iBAC9B;aACJ;SACJ,CAAC,CAAC;IACP,CAAC;IAED,OAAO,KAAK,CAAC;AACjB,CAAC"}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { MethodDescriptor } from './descriptor.js';
|
|
2
2
|
import type { Injectables } from '../types.js';
|
|
3
3
|
export declare function checkPermission(descriptor: MethodDescriptor, injections: Injectables): Promise<void>;
|
|
4
|
+
export declare function assertAccessible(descriptor: MethodDescriptor, permCheck: unknown): Promise<void>;
|
|
4
5
|
//# sourceMappingURL=permission.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"permission.d.ts","sourceRoot":"","sources":["../../src/core/permission.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAC;AACrD,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,UAAU,CAAC;
|
|
1
|
+
{"version":3,"file":"permission.d.ts","sourceRoot":"","sources":["../../src/core/permission.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAC;AACrD,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,UAAU,CAAC;AAE5C,wBAAsB,eAAe,CAAC,UAAU,EAAE,gBAAgB,EAAE,UAAU,EAAE,WAAW,GAAG,OAAO,CAAC,IAAI,CAAC,CAoB1G;AAED,wBAAsB,gBAAgB,CAAC,UAAU,EAAE,gBAAgB,EAAE,SAAS,EAAE,OAAO,GAAG,OAAO,CAAC,IAAI,CAAC,CAStG"}
|
package/dist/core/permission.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { RPCError } from '../errors/RPCError.js';
|
|
2
|
-
import { PERMISSION_DENIED } from '../errors/codes.js';
|
|
2
|
+
import { METHOD_NOT_FOUND, PERMISSION_DENIED } from '../errors/codes.js';
|
|
3
3
|
export async function checkPermission(descriptor, injections) {
|
|
4
4
|
const grantTo = descriptor.grantTo;
|
|
5
5
|
if (!grantTo || grantTo.length === 0)
|
|
@@ -15,4 +15,15 @@ export async function checkPermission(descriptor, injections) {
|
|
|
15
15
|
}
|
|
16
16
|
throw new RPCError(PERMISSION_DENIED, 'Permission denied', `one of ${JSON.stringify(grantTo)} is necessary for [${descriptor.namespace}].`);
|
|
17
17
|
}
|
|
18
|
+
export async function assertAccessible(descriptor, permCheck) {
|
|
19
|
+
try {
|
|
20
|
+
await checkPermission(descriptor, { perm_check: permCheck });
|
|
21
|
+
}
|
|
22
|
+
catch (error) {
|
|
23
|
+
if (error instanceof RPCError && error.code === PERMISSION_DENIED) {
|
|
24
|
+
throw new RPCError(METHOD_NOT_FOUND, 'Method not found', `Method not found.[${descriptor.namespace}]`);
|
|
25
|
+
}
|
|
26
|
+
throw error;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
18
29
|
//# sourceMappingURL=permission.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"permission.js","sourceRoot":"","sources":["../../src/core/permission.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,oBAAoB,CAAC;AAC9C,OAAO,EAAE,iBAAiB,EAAE,MAAM,iBAAiB,CAAC;
|
|
1
|
+
{"version":3,"file":"permission.js","sourceRoot":"","sources":["../../src/core/permission.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,oBAAoB,CAAC;AAC9C,OAAO,EAAE,gBAAgB,EAAE,iBAAiB,EAAE,MAAM,iBAAiB,CAAC;AAItE,MAAM,CAAC,KAAK,UAAU,eAAe,CAAC,UAA4B,EAAE,UAAuB;IACzF,MAAM,OAAO,GAAG,UAAU,CAAC,OAAO,CAAC;IACnC,IAAI,CAAC,OAAO,IAAI,OAAO,CAAC,MAAM,KAAK,CAAC;QAAE,OAAO;IAE7C,MAAM,SAAS,GAAG,UAAU,CAAC,YAAY,CAAC,CAAC;IAC3C,IAAI,OAAO,SAAS,KAAK,UAAU;QAAE,OAAO;IAE5C,MAAM,MAAM,GAAG,MAAO,SAAmG,CACvH,OAAO,EACP,UAAU,EACV,UAAU,CACX,CAAC;IAEF,IAAI,MAAM,KAAK,IAAI;QAAE,OAAO;IAE5B,IAAI,MAAM,YAAY,QAAQ,EAAE,CAAC;QAC/B,MAAM,MAAM,CAAC;IACf,CAAC;IAED,MAAM,IAAI,QAAQ,CAAC,iBAAiB,EAAE,mBAAmB,EAAE,UAAU,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,sBAAsB,UAAU,CAAC,SAAS,IAAI,CAAC,CAAC;AAC9I,CAAC;AAED,MAAM,CAAC,KAAK,UAAU,gBAAgB,CAAC,UAA4B,EAAE,SAAkB;IACrF,IAAI,CAAC;QACH,MAAM,eAAe,CAAC,UAAU,EAAE,EAAE,UAAU,EAAE,SAAS,EAAE,CAAC,CAAC;IAC/D,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,IAAI,KAAK,YAAY,QAAQ,IAAI,KAAK,CAAC,IAAI,KAAK,iBAAiB,EAAE,CAAC;YAClE,MAAM,IAAI,QAAQ,CAAC,gBAAgB,EAAE,kBAAkB,EAAE,qBAAqB,UAAU,CAAC,SAAS,GAAG,CAAC,CAAC;QACzG,CAAC;QACD,MAAM,KAAK,CAAC;IACd,CAAC;AACH,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"system-methods.d.ts","sourceRoot":"","sources":["../../src/core/system-methods.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"system-methods.d.ts","sourceRoot":"","sources":["../../src/core/system-methods.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAO/C,wBAAgB,qBAAqB,CAAC,UAAU,EAAE,UAAU,GAAG,IAAI,CAiGlE"}
|
|
@@ -1,22 +1,11 @@
|
|
|
1
|
-
// src/core/system-methods.ts
|
|
2
1
|
import { RPCError } from '../errors/RPCError.js';
|
|
3
|
-
import { METHOD_NOT_FOUND
|
|
4
|
-
import {
|
|
2
|
+
import { METHOD_NOT_FOUND } from '../errors/codes.js';
|
|
3
|
+
import { assertAccessible } from './permission.js';
|
|
5
4
|
import { s } from '../schema/json/index.js';
|
|
5
|
+
import { generateLlmTools } from "./llm-tools.js";
|
|
6
6
|
function notFound(namespace) {
|
|
7
7
|
return new RPCError(METHOD_NOT_FOUND, 'Method not found', `Method not found.[${namespace}]`);
|
|
8
8
|
}
|
|
9
|
-
async function assertAccessible(descriptor, permCheck) {
|
|
10
|
-
try {
|
|
11
|
-
await checkPermission(descriptor, { perm_check: permCheck });
|
|
12
|
-
}
|
|
13
|
-
catch (error) {
|
|
14
|
-
if (error instanceof RPCError && error.code === PERMISSION_DENIED) {
|
|
15
|
-
throw notFound(descriptor.namespace);
|
|
16
|
-
}
|
|
17
|
-
throw error;
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
9
|
export function registerSystemMethods(repository) {
|
|
21
10
|
repository.register({
|
|
22
11
|
namespace: 'system.listMethods',
|
|
@@ -72,5 +61,31 @@ export function registerSystemMethods(repository) {
|
|
|
72
61
|
}
|
|
73
62
|
return document === undefined ? null : document;
|
|
74
63
|
});
|
|
64
|
+
repository.register({
|
|
65
|
+
namespace: 'llm.tools',
|
|
66
|
+
document: 'Generate OpenAI-style tool/function definitions for all methods the caller can invoke.\n\n' +
|
|
67
|
+
'Each entry is `{ type: "function", function: { name, description, parameters } }`.\n' +
|
|
68
|
+
'Injectable parameters are omitted (they are provided by the server).\n' +
|
|
69
|
+
'Streaming methods are excluded by default; pass `{ includeStreaming: true }` to include them\n' +
|
|
70
|
+
'(their description will note that they return a stream).\n\n' +
|
|
71
|
+
'Optional params: `{ includeStreaming?: boolean, namePrefix?: string, prefix?: string }`.',
|
|
72
|
+
signature: [
|
|
73
|
+
s.array(s.object({
|
|
74
|
+
type: s.literal('function'),
|
|
75
|
+
function: s.object({
|
|
76
|
+
name: s.string(),
|
|
77
|
+
description: s.string(),
|
|
78
|
+
parameters: s.record(s.unknown()),
|
|
79
|
+
}),
|
|
80
|
+
})),
|
|
81
|
+
s.injectable("repository"),
|
|
82
|
+
s.injectable('perm_check'),
|
|
83
|
+
s.object({
|
|
84
|
+
includeStreaming: s.boolean().describe('Include stream-returning methods (default false)'),
|
|
85
|
+
namePrefix: s.string().describe('Prefix added to every tool name'),
|
|
86
|
+
prefix: s.string().describe('Only include methods whose namespace starts with this'),
|
|
87
|
+
}).describe('Options').nullable()
|
|
88
|
+
],
|
|
89
|
+
}, generateLlmTools);
|
|
75
90
|
}
|
|
76
91
|
//# sourceMappingURL=system-methods.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"system-methods.js","sourceRoot":"","sources":["../../src/core/system-methods.ts"],"names":[],"mappings":"AAAA,
|
|
1
|
+
{"version":3,"file":"system-methods.js","sourceRoot":"","sources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@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@yuhere/js-jsonrpc2",
|
|
3
|
-
"version": "2.1.
|
|
3
|
+
"version": "2.1.6",
|
|
4
4
|
"description": "A TypeScript JSON-RPC 2.0 server library with schema validation, authorization, dependency injection, and multiple transports.",
|
|
5
5
|
"homepage": "https://github.com/yuhere/js-jsonrpc2#readme",
|
|
6
6
|
"license": "MIT",
|
|
@@ -40,7 +40,7 @@
|
|
|
40
40
|
"js-jsonrpc2": "./dist/cli.js"
|
|
41
41
|
},
|
|
42
42
|
"scripts": {
|
|
43
|
-
"build": "clean && npm run build:lib && npm run build:webui",
|
|
43
|
+
"build": "npm run clean && npm run build:lib && npm run build:webui",
|
|
44
44
|
"build:lib": "tsc -p tsconfig.build.json && tsc-esm-fix",
|
|
45
45
|
"build:webui": "tsc -p webui//tsconfig.json && vite build --config webui/vite.config.ts",
|
|
46
46
|
"dev": "run-p dev:lib dev:webui",
|