@honu-office/mcp 0.0.0-stage → 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.
@@ -0,0 +1,146 @@
1
+ /**
2
+ * `remediate` — submit a document to be fixed, and track the request.
3
+ *
4
+ * Remediation is the one operation here that is not purely computational: a
5
+ * request can be priced, queued and worked on over hours or days. So this tool
6
+ * submits and reports, and never blocks waiting for a human process to finish.
7
+ *
8
+ * It accepts the three starting points the API offers — a local file, a URL, or
9
+ * an existing analysis report — because "remediate what you just analyzed"
10
+ * is the common follow-up and re-uploading the file for it is wasteful.
11
+ */
12
+
13
+ import { responseKeys } from './discover.js';
14
+ import { unwrap } from '../http.js';
15
+ import { readDocument } from '../files.js';
16
+
17
+ /**
18
+ * Submit or track a remediation request.
19
+ *
20
+ * @param {{client: import('../http.js').ApiClient}} context - Injected dependencies.
21
+ * @param {{action?: string, file?: string, url?: string, reportId?: string, requestId?: string, notes?: string, limit?: number}} input - Tool input.
22
+ * @returns {Promise<object>} A structured result.
23
+ */
24
+ export async function remediate(context, input) {
25
+ const { client } = context;
26
+ const action = (input.action || (input.requestId ? 'status' : 'submit')).toLowerCase();
27
+
28
+ switch (action) {
29
+ case 'submit': {
30
+ const sources = [input.file, input.url, input.reportId].filter(Boolean);
31
+ if (sources.length === 0) {
32
+ throw new Error('Give one of `file` (a path on this machine), `url`, or `reportId` from a previous analysis.');
33
+ }
34
+ if (sources.length > 1) {
35
+ throw new Error('Give exactly one of `file`, `url` or `reportId`.');
36
+ }
37
+
38
+ let created;
39
+ let subject;
40
+ if (input.file) {
41
+ const document = await readDocument(input.file);
42
+ subject = document.path;
43
+ created = unwrap(
44
+ await client.upload('/api/remediation', document, { notes: input.notes })
45
+ );
46
+ } else if (input.url) {
47
+ subject = input.url;
48
+ created = unwrap(
49
+ await client.request('/api/remediation/from-url', {
50
+ method: 'POST',
51
+ body: { url: input.url, notes: input.notes },
52
+ })
53
+ );
54
+ } else {
55
+ subject = `report ${input.reportId}`;
56
+ created = unwrap(
57
+ await client.request('/api/remediation/from-report', {
58
+ method: 'POST',
59
+ body: { reportId: input.reportId, notes: input.notes },
60
+ })
61
+ );
62
+ }
63
+
64
+ const requestId = created?.requestId ?? created?.id;
65
+ return {
66
+ status: 'submitted',
67
+ subject,
68
+ requestId,
69
+ requestStatus: created?.status,
70
+ quote: readQuote(created),
71
+ message: requestId
72
+ ? `Remediation request ${requestId} created. Check it with action "status".`
73
+ : 'Request created, but HonuOffice returned no request id.',
74
+ // Key names only, never the raw response (OA-497).
75
+ responseKeys: requestId ? undefined : responseKeys(created),
76
+ };
77
+ }
78
+
79
+ case 'status': {
80
+ if (!input.requestId) throw new Error('`requestId` is required for action "status".');
81
+ // Encode the model-supplied id: a bare value with dot segments would
82
+ // otherwise traverse out of the remediation path into any authenticated
83
+ // GET (OA-475). The schema also constrains the id, this is defence in
84
+ // depth.
85
+ const id = encodeURIComponent(input.requestId);
86
+ const request = unwrap(await client.request(`/api/remediation/${id}`));
87
+ return {
88
+ status: 'ok',
89
+ requestId: input.requestId,
90
+ requestStatus: request?.status,
91
+ quote: readQuote(request),
92
+ createdAt: request?.createdAt,
93
+ completedAt: request?.completedAt,
94
+ downloads:
95
+ String(request?.status).toLowerCase() === 'completed'
96
+ ? {
97
+ original: `/api/remediation/${id}/download/original`,
98
+ remediated: `/api/remediation/${id}/download/remediated`,
99
+ }
100
+ : undefined,
101
+ };
102
+ }
103
+
104
+ case 'list': {
105
+ const body = await client.request('/api/remediation/my-requests', {
106
+ query: { limit: input.limit ?? 20 },
107
+ });
108
+ const payload = unwrap(body);
109
+ const list = Array.isArray(payload) ? payload : payload?.items || payload?.requests || [];
110
+ return {
111
+ status: 'ok',
112
+ count: list.length,
113
+ requests: list.map((request) => ({
114
+ requestId: request.requestId ?? request.id,
115
+ filename: request.filename,
116
+ requestStatus: request.status,
117
+ createdAt: request.createdAt,
118
+ })),
119
+ };
120
+ }
121
+
122
+ default:
123
+ throw new Error(`Unknown action "${input.action}". Use submit, status or list.`);
124
+ }
125
+ }
126
+
127
+ /**
128
+ * Pull out pricing, which is the thing a user most wants to see before
129
+ * committing — and which has been wrong before: a missing rate card once made
130
+ * every quote read $0.00 (api#433). Reporting the figure as the API gives it,
131
+ * and saying nothing when there is none, beats implying a free job.
132
+ *
133
+ * @param {unknown} payload - A remediation request payload.
134
+ * @returns {object|undefined} The quote, when present.
135
+ */
136
+ function readQuote(payload) {
137
+ const quote = payload?.quote ?? payload?.pricing ?? payload;
138
+ const amount = quote?.totalAmount ?? quote?.total ?? quote?.amount;
139
+ if (amount === undefined || amount === null) return undefined;
140
+ return {
141
+ totalAmount: amount,
142
+ currency: quote?.currency,
143
+ unitLabel: quote?.unitLabel,
144
+ ratePerUnit: quote?.ratePerUnit,
145
+ };
146
+ }