@replicer/sdk 0.2.0 → 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 +32 -2
- package/dist/chunk-CIXVIMX2.js +54 -0
- package/dist/index.cjs +52 -4
- package/dist/index.d.cts +173 -6
- package/dist/index.d.ts +173 -6
- package/dist/index.js +52 -4
- 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-CmxwElVc.d.cts → types-DXQsrvOU.d.cts} +324 -5
- package/dist/{types-CmxwElVc.d.ts → types-DXQsrvOU.d.ts} +324 -5
- package/dist/webhooks.cjs +32 -20
- package/dist/webhooks.d.cts +5 -4
- package/dist/webhooks.d.ts +5 -4
- package/dist/webhooks.js +12 -45
- package/package.json +14 -1
package/dist/tools.d.cts
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { b6 as ToolCall } from './types-DXQsrvOU.cjs';
|
|
2
|
+
export { b7 as ToolCallResponse } from './types-DXQsrvOU.cjs';
|
|
3
|
+
|
|
4
|
+
/** Name of the signature header (same as webhooks). */
|
|
5
|
+
declare const TOOL_SIGNATURE_HEADER = "Replicer-Signature";
|
|
6
|
+
/** Header carrying the unique id of this invocation (`tcall_…`, same as the body `id`). */
|
|
7
|
+
declare const TOOL_CALL_ID_HEADER = "Replicer-Tool-Call-Id";
|
|
8
|
+
/** Header carrying the tool name. */
|
|
9
|
+
declare const TOOL_NAME_HEADER = "Replicer-Tool-Name";
|
|
10
|
+
/** Default accepted distance, in seconds, between the signed timestamp and now. */
|
|
11
|
+
declare const DEFAULT_TOOL_TOLERANCE_SECONDS = 300;
|
|
12
|
+
/** Thrown by `verifyToolCall` for a missing, invalid or expired signature, or a body that is not a tool call. */
|
|
13
|
+
declare class ToolSignatureError extends Error {
|
|
14
|
+
constructor(message: string);
|
|
15
|
+
}
|
|
16
|
+
interface VerifyToolCallInput {
|
|
17
|
+
/** The raw request body exactly as received (never re-serialized JSON). */
|
|
18
|
+
payload: string | Uint8Array;
|
|
19
|
+
/** Value of the `Replicer-Signature` header. */
|
|
20
|
+
header: string | null | undefined;
|
|
21
|
+
/** The signing secret of the tool's webhook endpoint (`whsec_…`). One of `secret` / `secrets` is required. */
|
|
22
|
+
secret?: string;
|
|
23
|
+
/** Several secrets, any of which may match (the new and the old one during a rotation). */
|
|
24
|
+
secrets?: string[];
|
|
25
|
+
/** Maximum distance between the signed timestamp and now (default 300). */
|
|
26
|
+
toleranceSeconds?: number;
|
|
27
|
+
/** Current time (default `new Date()`), for tests. */
|
|
28
|
+
now?: Date;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Verify the signature, then parse the body as a tool call. Throws `ToolSignatureError` when the
|
|
32
|
+
* signature is missing, invalid or stale, or the body is not a JSON tool call.
|
|
33
|
+
*/
|
|
34
|
+
declare function verifyToolCall<Args = Record<string, unknown>>(input: VerifyToolCallInput): ToolCall<Args>;
|
|
35
|
+
/** A tool implementation: gets the typed arguments and the full call, returns the `result` the agent reads. */
|
|
36
|
+
type ToolFunction<Args = Record<string, unknown>> = (args: Args, call: ToolCall<Args>) => unknown | Promise<unknown>;
|
|
37
|
+
/** Map of tool name to implementation. Type the arguments per tool: `{ book_table: async (a: { time: string }) => … }`. */
|
|
38
|
+
type ToolMap = Record<string, ToolFunction<any>>;
|
|
39
|
+
interface ToolHandlerOptions<Tools extends ToolMap = ToolMap> {
|
|
40
|
+
secret?: string;
|
|
41
|
+
/** Several secrets, any of which may match (rotation overlap). */
|
|
42
|
+
secrets?: string[];
|
|
43
|
+
toleranceSeconds?: number;
|
|
44
|
+
tools: Tools;
|
|
45
|
+
/** Called with the error a tool threw (the client always gets a generic 500). */
|
|
46
|
+
onError?: (error: unknown, call: ToolCall) => void;
|
|
47
|
+
}
|
|
48
|
+
interface HandleToolCallInput {
|
|
49
|
+
rawBody: string | Uint8Array;
|
|
50
|
+
/** Request headers: a `Headers`, or a plain Node object (`req.headers`). */
|
|
51
|
+
headers: Headers | Record<string, string | string[] | undefined>;
|
|
52
|
+
now?: Date;
|
|
53
|
+
}
|
|
54
|
+
interface ToolHandlerResult {
|
|
55
|
+
status: number;
|
|
56
|
+
body: {
|
|
57
|
+
result: unknown;
|
|
58
|
+
} | {
|
|
59
|
+
error: string;
|
|
60
|
+
code: string;
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Framework-agnostic handler for Express and plain Node: pass the raw body and the headers, send
|
|
65
|
+
* back `status` and `body` as JSON. 401 for a bad signature, 404 for an unknown tool, 500 (generic
|
|
66
|
+
* message) when the tool throws, else 200 `{ result }`.
|
|
67
|
+
*/
|
|
68
|
+
declare function createToolCallProcessor<Tools extends ToolMap>(options: ToolHandlerOptions<Tools>): (input: HandleToolCallInput) => Promise<ToolHandlerResult>;
|
|
69
|
+
/** One-shot variant of `createToolCallProcessor` for Express and plain Node. */
|
|
70
|
+
declare function handleToolCall<Tools extends ToolMap>(input: HandleToolCallInput & ToolHandlerOptions<Tools>): Promise<ToolHandlerResult>;
|
|
71
|
+
/**
|
|
72
|
+
* Fetch-API handler (Next.js route handlers, Hono, Cloudflare Workers, Deno, Bun):
|
|
73
|
+
* `export const POST = createToolHandler({ secret, tools: { book_table: async (args, call) => … } })`.
|
|
74
|
+
* It verifies the signature, routes on `tool.name` and answers `{ result }` (401 bad signature,
|
|
75
|
+
* 404 unknown tool, 500 when the tool throws, without leaking the error message).
|
|
76
|
+
*/
|
|
77
|
+
declare function createToolHandler<Tools extends ToolMap>(options: ToolHandlerOptions<Tools>): (request: Request) => Promise<Response>;
|
|
78
|
+
|
|
79
|
+
export { DEFAULT_TOOL_TOLERANCE_SECONDS, type HandleToolCallInput, TOOL_CALL_ID_HEADER, TOOL_NAME_HEADER, TOOL_SIGNATURE_HEADER, ToolCall, type ToolFunction, type ToolHandlerOptions, type ToolHandlerResult, type ToolMap, ToolSignatureError, type VerifyToolCallInput, createToolCallProcessor, createToolHandler, handleToolCall, verifyToolCall };
|
package/dist/tools.d.ts
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { b6 as ToolCall } from './types-DXQsrvOU.js';
|
|
2
|
+
export { b7 as ToolCallResponse } from './types-DXQsrvOU.js';
|
|
3
|
+
|
|
4
|
+
/** Name of the signature header (same as webhooks). */
|
|
5
|
+
declare const TOOL_SIGNATURE_HEADER = "Replicer-Signature";
|
|
6
|
+
/** Header carrying the unique id of this invocation (`tcall_…`, same as the body `id`). */
|
|
7
|
+
declare const TOOL_CALL_ID_HEADER = "Replicer-Tool-Call-Id";
|
|
8
|
+
/** Header carrying the tool name. */
|
|
9
|
+
declare const TOOL_NAME_HEADER = "Replicer-Tool-Name";
|
|
10
|
+
/** Default accepted distance, in seconds, between the signed timestamp and now. */
|
|
11
|
+
declare const DEFAULT_TOOL_TOLERANCE_SECONDS = 300;
|
|
12
|
+
/** Thrown by `verifyToolCall` for a missing, invalid or expired signature, or a body that is not a tool call. */
|
|
13
|
+
declare class ToolSignatureError extends Error {
|
|
14
|
+
constructor(message: string);
|
|
15
|
+
}
|
|
16
|
+
interface VerifyToolCallInput {
|
|
17
|
+
/** The raw request body exactly as received (never re-serialized JSON). */
|
|
18
|
+
payload: string | Uint8Array;
|
|
19
|
+
/** Value of the `Replicer-Signature` header. */
|
|
20
|
+
header: string | null | undefined;
|
|
21
|
+
/** The signing secret of the tool's webhook endpoint (`whsec_…`). One of `secret` / `secrets` is required. */
|
|
22
|
+
secret?: string;
|
|
23
|
+
/** Several secrets, any of which may match (the new and the old one during a rotation). */
|
|
24
|
+
secrets?: string[];
|
|
25
|
+
/** Maximum distance between the signed timestamp and now (default 300). */
|
|
26
|
+
toleranceSeconds?: number;
|
|
27
|
+
/** Current time (default `new Date()`), for tests. */
|
|
28
|
+
now?: Date;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Verify the signature, then parse the body as a tool call. Throws `ToolSignatureError` when the
|
|
32
|
+
* signature is missing, invalid or stale, or the body is not a JSON tool call.
|
|
33
|
+
*/
|
|
34
|
+
declare function verifyToolCall<Args = Record<string, unknown>>(input: VerifyToolCallInput): ToolCall<Args>;
|
|
35
|
+
/** A tool implementation: gets the typed arguments and the full call, returns the `result` the agent reads. */
|
|
36
|
+
type ToolFunction<Args = Record<string, unknown>> = (args: Args, call: ToolCall<Args>) => unknown | Promise<unknown>;
|
|
37
|
+
/** Map of tool name to implementation. Type the arguments per tool: `{ book_table: async (a: { time: string }) => … }`. */
|
|
38
|
+
type ToolMap = Record<string, ToolFunction<any>>;
|
|
39
|
+
interface ToolHandlerOptions<Tools extends ToolMap = ToolMap> {
|
|
40
|
+
secret?: string;
|
|
41
|
+
/** Several secrets, any of which may match (rotation overlap). */
|
|
42
|
+
secrets?: string[];
|
|
43
|
+
toleranceSeconds?: number;
|
|
44
|
+
tools: Tools;
|
|
45
|
+
/** Called with the error a tool threw (the client always gets a generic 500). */
|
|
46
|
+
onError?: (error: unknown, call: ToolCall) => void;
|
|
47
|
+
}
|
|
48
|
+
interface HandleToolCallInput {
|
|
49
|
+
rawBody: string | Uint8Array;
|
|
50
|
+
/** Request headers: a `Headers`, or a plain Node object (`req.headers`). */
|
|
51
|
+
headers: Headers | Record<string, string | string[] | undefined>;
|
|
52
|
+
now?: Date;
|
|
53
|
+
}
|
|
54
|
+
interface ToolHandlerResult {
|
|
55
|
+
status: number;
|
|
56
|
+
body: {
|
|
57
|
+
result: unknown;
|
|
58
|
+
} | {
|
|
59
|
+
error: string;
|
|
60
|
+
code: string;
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Framework-agnostic handler for Express and plain Node: pass the raw body and the headers, send
|
|
65
|
+
* back `status` and `body` as JSON. 401 for a bad signature, 404 for an unknown tool, 500 (generic
|
|
66
|
+
* message) when the tool throws, else 200 `{ result }`.
|
|
67
|
+
*/
|
|
68
|
+
declare function createToolCallProcessor<Tools extends ToolMap>(options: ToolHandlerOptions<Tools>): (input: HandleToolCallInput) => Promise<ToolHandlerResult>;
|
|
69
|
+
/** One-shot variant of `createToolCallProcessor` for Express and plain Node. */
|
|
70
|
+
declare function handleToolCall<Tools extends ToolMap>(input: HandleToolCallInput & ToolHandlerOptions<Tools>): Promise<ToolHandlerResult>;
|
|
71
|
+
/**
|
|
72
|
+
* Fetch-API handler (Next.js route handlers, Hono, Cloudflare Workers, Deno, Bun):
|
|
73
|
+
* `export const POST = createToolHandler({ secret, tools: { book_table: async (args, call) => … } })`.
|
|
74
|
+
* It verifies the signature, routes on `tool.name` and answers `{ result }` (401 bad signature,
|
|
75
|
+
* 404 unknown tool, 500 when the tool throws, without leaking the error message).
|
|
76
|
+
*/
|
|
77
|
+
declare function createToolHandler<Tools extends ToolMap>(options: ToolHandlerOptions<Tools>): (request: Request) => Promise<Response>;
|
|
78
|
+
|
|
79
|
+
export { DEFAULT_TOOL_TOLERANCE_SECONDS, type HandleToolCallInput, TOOL_CALL_ID_HEADER, TOOL_NAME_HEADER, TOOL_SIGNATURE_HEADER, ToolCall, type ToolFunction, type ToolHandlerOptions, type ToolHandlerResult, type ToolMap, ToolSignatureError, type VerifyToolCallInput, createToolCallProcessor, createToolHandler, handleToolCall, verifyToolCall };
|
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
|
+
};
|