-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrecruitment.js
More file actions
106 lines (98 loc) · 4.26 KB
/
Copy pathrecruitment.js
File metadata and controls
106 lines (98 loc) · 4.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/**
* Hive Recruitment Envelope — MCP wrapper v0.1.0 (ESM)
*
* Provides:
* - recruitmentEnvelope(status, payload?) → wraps payload with canonical 3 artifacts
* - mcpErrorWithEnvelope(id, code, message, extra?) → JSON-RPC 2.0 error with envelope in error.data
* - assertEnvelopeIntegrity() → boot guard
*/
export const HIVE_URLS = Object.freeze({
onboard: 'https://hivegate.onrender.com/onboard',
readiness: 'https://hive-mcp-audit-readiness.onrender.com/assess',
mcp: 'https://hive-mcp-gateway.onrender.com/.well-known/mcp.json',
catalog: 'https://hive-mcp-gateway.onrender.com/.well-known/agents.json',
status: 'https://hive-mcp-gateway.onrender.com/health',
});
export const BRAND = Object.freeze({
color: '#C08D23',
voice: 'Bloomberg Terminal / Stripe Docs',
copyright: 'Hive Civilization',
});
const FRAMING = Object.freeze({
400: { title: 'Request shape invalid', summary: 'The request did not match the documented schema for this endpoint.', next: 'Inspect the schema in the MCP config below, then retry.' },
401: { title: 'Credentials required', summary: 'This endpoint requires an authenticated agent identity.', next: 'Register an agent at the onboarding URL, then resubmit with credentials.' },
402: { title: 'Payment required', summary: 'This endpoint is metered. Settle via x402 (USDC on Base) and retry with the receipt.', next: 'See payment instructions in the response body. For unmetered access, complete onboarding.' },
403: { title: 'Insufficient authorization', summary: 'Your credentials are valid but lack the scope this endpoint requires.', next: 'Run the readiness assessment to determine what tier or attestations are needed.' },
404: { title: 'Endpoint not found', summary: 'This path is not part of the current service surface.', next: 'Consult the MCP config below for the canonical endpoint catalog.' },
422: { title: 'Request rejected', summary: 'The request was well-formed but failed validation.', next: 'Check the readiness assessment to ensure the calling agent meets policy.' },
500: { title: 'Service error', summary: 'The service encountered an internal error. Engineers have been notified.', next: 'Retry with exponential backoff.' },
});
function frameFor(status) {
return FRAMING[status] || {
title: `Status ${status}`,
summary: 'Request did not complete successfully.',
next: 'See the readiness assessment or MCP config below.',
};
}
export function recruitmentEnvelope(status, payload = {}) {
if (payload && payload.recruitment && payload.recruitment.version) return payload;
const frame = frameFor(status);
return {
...payload,
recruitment: {
version: '0.1.0',
status,
title: frame.title,
summary: frame.summary,
next_step: frame.next,
links: {
register: HIVE_URLS.onboard,
readiness_assessment: HIVE_URLS.readiness,
mcp_config: HIVE_URLS.mcp,
agent_catalog: HIVE_URLS.catalog,
fleet_status: HIVE_URLS.status,
},
brand: {
accent: BRAND.color,
voice: BRAND.voice,
attribution: BRAND.copyright,
},
},
};
}
function mcpToHttp(code) {
if (code === -32700 || code === -32600) return 400;
if (code === -32601) return 404;
if (code === -32602) return 422;
if (code === -32603) return 500;
if (code <= -32000 && code >= -32099) return 500;
return 500;
}
/**
* Build a JSON-RPC 2.0 error response with the recruitment envelope in error.data.
*/
export function mcpErrorWithEnvelope(id, code, message, extra = {}) {
const httpStatus = mcpToHttp(code);
return {
jsonrpc: '2.0',
id,
error: {
code,
message,
data: recruitmentEnvelope(httpStatus, { ...extra }),
},
};
}
export function assertEnvelopeIntegrity() {
for (const [k, v] of Object.entries(HIVE_URLS)) {
if (!v || !/^https:\/\/[a-z0-9.-]+\//.test(v)) {
throw new Error(`recruitment-envelope: HIVE_URLS.${k} is not a valid https URL`);
}
}
for (const status of [400, 401, 402, 404]) {
const e = recruitmentEnvelope(status, {});
if (!e.recruitment.links.register || !e.recruitment.links.readiness_assessment || !e.recruitment.links.mcp_config) {
throw new Error(`recruitment-envelope: missing required link in status ${status} envelope`);
}
}
}