@usenotra/geo
Version:
Request capture SDK for AI traffic attribution and agent feedback collection
179 lines (173 loc) • 5.88 kB
JavaScript
import { z } from 'zod';
// src/feedback/constants.ts
var DEFAULT_ENDPOINT = "https://api.usenotra.com";
var FEEDBACK_PATH = "/v1/feedback";
var SUBMIT_TIMEOUT_MS = 1e4;
var MISSING_URL_MESSAGE = "Provide the feedback URL from the Feedback page in your Notra dashboard as `url`.";
var DEFAULT_TOOL_NAME = "submit_feedback";
var FEEDBACK_KINDS = [
"bug",
"feature",
"praise",
"question",
"other"
];
var FEEDBACK_SENTIMENTS = ["negative", "neutral", "positive"];
var MESSAGE_MAX_LENGTH = 4e3;
var TITLE_MAX_LENGTH = 200;
// src/feedback/client.ts
var FeedbackSubmitError = class extends Error {
status;
constructor(status, message) {
super(message);
this.name = "FeedbackSubmitError";
this.status = status;
}
};
function feedbackUrl(options) {
if (options.url) {
return options.url;
}
if (!options.token) {
throw new Error(MISSING_URL_MESSAGE);
}
const value = options.endpoint ?? DEFAULT_ENDPOINT;
let end = value.length;
while (end > 0 && value[end - 1] === "/") {
end -= 1;
}
return `${value.slice(0, end)}${FEEDBACK_PATH}`;
}
function feedbackHeaders(options) {
const headers = {
"content-type": "application/json"
};
if (!options.url && options.token) {
headers.authorization = `Bearer ${options.token}`;
}
return headers;
}
async function readErrorMessage(response) {
try {
const body = await response.json();
if (typeof body === "object" && body !== null && "error" in body && typeof body.error === "string") {
return body.error;
}
} catch {
return response.statusText;
}
return response.statusText;
}
async function submitFeedback(input, options) {
const send = options.fetch ?? globalThis.fetch;
const response = await send(feedbackUrl(options), {
method: "POST",
headers: feedbackHeaders(options),
body: JSON.stringify(input),
signal: AbortSignal.timeout(options.timeoutMs ?? SUBMIT_TIMEOUT_MS)
});
if (!response.ok) {
throw new FeedbackSubmitError(
response.status,
await readErrorMessage(response)
);
}
const body = await response.json();
if (typeof body === "object" && body !== null && "feedback" in body && typeof body.feedback === "object" && body.feedback !== null && "id" in body.feedback && typeof body.feedback.id === "string") {
return {
id: body.feedback.id,
deduplicated: "deduplicated" in body && typeof body.deduplicated === "boolean" ? body.deduplicated : false
};
}
throw new FeedbackSubmitError(response.status, "Unexpected response");
}
// src/send.ts
function reportGeoError(onError, error) {
try {
onError?.(error);
} catch {
}
}
// src/feedback/mcp.ts
var feedbackToolInputSchema = {
message: z.string().trim().min(1).max(MESSAGE_MAX_LENGTH).describe("The feedback, bug report, feature request or question"),
title: z.string().trim().min(1).max(TITLE_MAX_LENGTH).optional().describe("Short one-line summary"),
kind: z.enum(FEEDBACK_KINDS).optional().describe("bug, feature, praise, question or other"),
sentiment: z.enum(FEEDBACK_SENTIMENTS).optional().describe("negative, neutral or positive"),
contextUrl: z.string().url().optional().describe("Page, docs URL or resource the feedback is about")
};
var feedbackToolArgsSchema = z.object(feedbackToolInputSchema);
var feedbackToolOutputSchema = {
id: z.string().describe("Identifier of the recorded feedback entry"),
deduplicated: z.boolean().describe("True when identical feedback was already recorded and reused")
};
function buildFeedbackToolDescription(productName) {
const subject = productName ? `the ${productName} team` : "the product team";
return `Send feedback about ${productName ?? "this product"} to ${subject}. Use it to report bugs, request features, ask questions or share what worked well. Include the exact steps or URL when reporting a problem.`;
}
function toErrorText(error) {
if (error instanceof FeedbackSubmitError) {
return `Feedback could not be submitted (${error.status}): ${error.message}`;
}
if (error instanceof Error) {
return `Feedback could not be submitted: ${error.message}`;
}
return "Feedback could not be submitted.";
}
function createFeedbackToolHandler(options) {
return async (args) => {
const parsed = feedbackToolArgsSchema.safeParse(args);
if (!parsed.success) {
return {
content: [
{
type: "text",
text: `Invalid feedback: ${parsed.error.issues[0]?.message ?? "check the arguments"}`
}
],
isError: true
};
}
const input = {
...options.defaults,
...parsed.data
};
try {
const result = await submitFeedback(input, options);
return {
content: [
{
type: "text",
text: result.deduplicated ? "This feedback was already recorded." : "Thanks, the feedback was sent to the team."
}
],
structuredContent: result
};
} catch (error) {
reportGeoError(options.onError, error);
return {
content: [{ type: "text", text: toErrorText(error) }],
isError: true
};
}
};
}
function registerFeedbackTool(server, options) {
return server.registerTool(
options.toolName ?? DEFAULT_TOOL_NAME,
{
description: options.description ?? buildFeedbackToolDescription(options.productName),
annotations: {
title: "Submit feedback",
readOnlyHint: false,
destructiveHint: false,
idempotentHint: false,
openWorldHint: true
},
inputSchema: feedbackToolInputSchema,
outputSchema: feedbackToolOutputSchema
},
createFeedbackToolHandler(options)
);
}
export { FeedbackSubmitError, buildFeedbackToolDescription, createFeedbackToolHandler, feedbackToolInputSchema, feedbackToolOutputSchema, registerFeedbackTool, submitFeedback };