@replicer/sdk 0.1.1 → 0.3.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/README.md +81 -17
- package/dist/chunk-CIXVIMX2.js +54 -0
- package/dist/index.cjs +136 -42
- package/dist/index.d.cts +499 -548
- package/dist/index.d.ts +499 -548
- package/dist/index.js +136 -42
- package/dist/tools.cjs +179 -0
- package/dist/tools.d.cts +79 -0
- package/dist/tools.d.ts +79 -0
- package/dist/tools.js +101 -0
- package/dist/types-DXQsrvOU.d.cts +1251 -0
- package/dist/types-DXQsrvOU.d.ts +1251 -0
- package/dist/webhooks.cjs +98 -14
- package/dist/webhooks.d.cts +36 -17
- package/dist/webhooks.d.ts +36 -17
- package/dist/webhooks.js +49 -14
- package/package.json +16 -2
package/dist/tools.js
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import {
|
|
2
|
+
DEFAULT_TOLERANCE_SECONDS,
|
|
3
|
+
SIGNATURE_HEADER,
|
|
4
|
+
verifySignatureHeader
|
|
5
|
+
} from "./chunk-CIXVIMX2.js";
|
|
6
|
+
|
|
7
|
+
// src/tools.ts
|
|
8
|
+
var TOOL_SIGNATURE_HEADER = SIGNATURE_HEADER;
|
|
9
|
+
var TOOL_CALL_ID_HEADER = "Replicer-Tool-Call-Id";
|
|
10
|
+
var TOOL_NAME_HEADER = "Replicer-Tool-Name";
|
|
11
|
+
var DEFAULT_TOOL_TOLERANCE_SECONDS = DEFAULT_TOLERANCE_SECONDS;
|
|
12
|
+
var ToolSignatureError = class extends Error {
|
|
13
|
+
constructor(message) {
|
|
14
|
+
super(message);
|
|
15
|
+
this.name = "ToolSignatureError";
|
|
16
|
+
}
|
|
17
|
+
};
|
|
18
|
+
function verifyToolCall(input) {
|
|
19
|
+
if (!verifySignatureHeader(input)) {
|
|
20
|
+
throw new ToolSignatureError("Invalid tool call signature");
|
|
21
|
+
}
|
|
22
|
+
const text = typeof input.payload === "string" ? input.payload : new TextDecoder().decode(input.payload);
|
|
23
|
+
let parsed;
|
|
24
|
+
try {
|
|
25
|
+
parsed = JSON.parse(text);
|
|
26
|
+
} catch {
|
|
27
|
+
throw new ToolSignatureError("Tool call payload is not valid JSON");
|
|
28
|
+
}
|
|
29
|
+
if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) {
|
|
30
|
+
throw new ToolSignatureError("Tool call payload is not a JSON object");
|
|
31
|
+
}
|
|
32
|
+
const call = parsed;
|
|
33
|
+
if (call.object !== "tool_call" || typeof call.tool?.name !== "string") {
|
|
34
|
+
throw new ToolSignatureError("Payload is not a tool call");
|
|
35
|
+
}
|
|
36
|
+
return call;
|
|
37
|
+
}
|
|
38
|
+
function readHeader(headers, name) {
|
|
39
|
+
if (typeof headers.get === "function") {
|
|
40
|
+
return headers.get(name) ?? void 0;
|
|
41
|
+
}
|
|
42
|
+
const wanted = name.toLowerCase();
|
|
43
|
+
for (const [key, value] of Object.entries(headers)) {
|
|
44
|
+
if (key.toLowerCase() === wanted) return Array.isArray(value) ? value.join(",") : value;
|
|
45
|
+
}
|
|
46
|
+
return void 0;
|
|
47
|
+
}
|
|
48
|
+
function createToolCallProcessor(options) {
|
|
49
|
+
return async ({ rawBody, headers, now }) => {
|
|
50
|
+
let call;
|
|
51
|
+
try {
|
|
52
|
+
call = verifyToolCall({
|
|
53
|
+
payload: rawBody,
|
|
54
|
+
header: readHeader(headers, SIGNATURE_HEADER),
|
|
55
|
+
secret: options.secret,
|
|
56
|
+
secrets: options.secrets,
|
|
57
|
+
toleranceSeconds: options.toleranceSeconds,
|
|
58
|
+
now
|
|
59
|
+
});
|
|
60
|
+
} catch {
|
|
61
|
+
return { status: 401, body: { error: "Invalid signature", code: "invalid_signature" } };
|
|
62
|
+
}
|
|
63
|
+
const fn = Object.prototype.hasOwnProperty.call(options.tools, call.tool.name) ? options.tools[call.tool.name] : void 0;
|
|
64
|
+
if (!fn) {
|
|
65
|
+
return { status: 404, body: { error: "Unknown tool", code: "unknown_tool" } };
|
|
66
|
+
}
|
|
67
|
+
try {
|
|
68
|
+
const result = await fn(call.arguments, call);
|
|
69
|
+
return { status: 200, body: { result: result === void 0 ? null : result } };
|
|
70
|
+
} catch (error) {
|
|
71
|
+
try {
|
|
72
|
+
options.onError?.(error, call);
|
|
73
|
+
} catch {
|
|
74
|
+
}
|
|
75
|
+
return { status: 500, body: { error: "Tool failed", code: "tool_error" } };
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
function handleToolCall(input) {
|
|
80
|
+
const { rawBody, headers, now, ...options } = input;
|
|
81
|
+
return createToolCallProcessor(options)({ rawBody, headers, now });
|
|
82
|
+
}
|
|
83
|
+
function createToolHandler(options) {
|
|
84
|
+
const process = createToolCallProcessor(options);
|
|
85
|
+
return async (request) => {
|
|
86
|
+
const rawBody = new Uint8Array(await request.arrayBuffer());
|
|
87
|
+
const { status, body } = await process({ rawBody, headers: request.headers });
|
|
88
|
+
return new Response(JSON.stringify(body), { status, headers: { "Content-Type": "application/json" } });
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
export {
|
|
92
|
+
DEFAULT_TOOL_TOLERANCE_SECONDS,
|
|
93
|
+
TOOL_CALL_ID_HEADER,
|
|
94
|
+
TOOL_NAME_HEADER,
|
|
95
|
+
TOOL_SIGNATURE_HEADER,
|
|
96
|
+
ToolSignatureError,
|
|
97
|
+
createToolCallProcessor,
|
|
98
|
+
createToolHandler,
|
|
99
|
+
handleToolCall,
|
|
100
|
+
verifyToolCall
|
|
101
|
+
};
|