@journeyrewards/hive-vercel 1.0.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,141 @@
1
+ // src/server.ts
2
+ function verifyApiKey(key) {
3
+ return typeof key === "string" && (key.startsWith("jh_live_") || key.startsWith("jh_test_")) && key.length > 16;
4
+ }
5
+ function createJourneyHiveHandler(config) {
6
+ const baseUrl = config.baseUrl || "https://journey-hive.replit.app";
7
+ const headers = {
8
+ "Content-Type": "application/json",
9
+ Authorization: `Bearer ${config.apiKey}`
10
+ };
11
+ return async function handler(req, res) {
12
+ if (req.method !== "POST") {
13
+ res.status(405).json({ error: { message: "Method not allowed" } });
14
+ return;
15
+ }
16
+ const { action, ...params } = req.body;
17
+ if (config.allowedAgentIds && params.agent_id) {
18
+ if (!config.allowedAgentIds.includes(params.agent_id)) {
19
+ res.status(403).json({ error: { message: "Agent not allowed" } });
20
+ return;
21
+ }
22
+ }
23
+ let path;
24
+ let method = "POST";
25
+ switch (action) {
26
+ case "create_response":
27
+ path = "/v1/responses";
28
+ break;
29
+ case "list_agents":
30
+ path = "/v1/agents";
31
+ method = "GET";
32
+ break;
33
+ case "get_agent":
34
+ path = `/v1/agents/${params.agent_id}`;
35
+ method = "GET";
36
+ break;
37
+ case "create_conversation":
38
+ path = "/v1/conversations";
39
+ break;
40
+ case "get_conversation":
41
+ path = `/v1/conversations/${params.conversation_id}`;
42
+ method = "GET";
43
+ break;
44
+ case "get_messages":
45
+ path = `/v1/conversations/${params.conversation_id}/messages`;
46
+ method = "GET";
47
+ break;
48
+ default:
49
+ res.status(400).json({ error: { message: `Unknown action: ${action}` } });
50
+ return;
51
+ }
52
+ try {
53
+ const fetchOptions = {
54
+ method,
55
+ headers
56
+ };
57
+ if (method === "POST") {
58
+ const { action: _action, ...bodyParams } = params;
59
+ fetchOptions.body = JSON.stringify(bodyParams);
60
+ }
61
+ const response = await fetch(`${baseUrl}${path}`, fetchOptions);
62
+ if (params.stream && action === "create_response") {
63
+ res.setHeader("Content-Type", "text/event-stream");
64
+ res.setHeader("Cache-Control", "no-cache");
65
+ res.setHeader("Connection", "keep-alive");
66
+ const reader = response.body?.getReader();
67
+ if (!reader) {
68
+ res.status(500).json({ error: { message: "No response body" } });
69
+ return;
70
+ }
71
+ const decoder = new TextDecoder();
72
+ try {
73
+ while (true) {
74
+ const { done, value } = await reader.read();
75
+ if (done) break;
76
+ res.write(decoder.decode(value, { stream: true }));
77
+ }
78
+ } finally {
79
+ res.end();
80
+ }
81
+ return;
82
+ }
83
+ const data = await response.json();
84
+ res.status(response.status).json(data);
85
+ } catch (err) {
86
+ const message = err instanceof Error ? err.message : "Internal server error";
87
+ res.status(500).json({ error: { message } });
88
+ }
89
+ };
90
+ }
91
+ var JourneyHiveServerClient = class {
92
+ constructor(config) {
93
+ this.baseUrl = config.baseUrl || "https://journey-hive.replit.app";
94
+ this.headers = {
95
+ "Content-Type": "application/json",
96
+ Authorization: `Bearer ${config.apiKey}`
97
+ };
98
+ }
99
+ async request(method, path, body) {
100
+ const res = await fetch(`${this.baseUrl}${path}`, {
101
+ method,
102
+ headers: this.headers,
103
+ body: body ? JSON.stringify(body) : void 0
104
+ });
105
+ if (!res.ok) {
106
+ const err = await res.json().catch(() => ({}));
107
+ throw new Error(err?.error?.message || `Request failed with status ${res.status}`);
108
+ }
109
+ return res.json();
110
+ }
111
+ async createResponse(params) {
112
+ return this.request("POST", "/v1/responses", params);
113
+ }
114
+ async getAgent(agentId) {
115
+ return this.request("GET", `/v1/agents/${agentId}`);
116
+ }
117
+ async listAgents() {
118
+ return this.request("GET", "/v1/agents");
119
+ }
120
+ async updateAgent(agentId, params) {
121
+ return this.request("PATCH", `/v1/agents/${agentId}`, params);
122
+ }
123
+ async createConversation(params) {
124
+ return this.request("POST", "/v1/conversations", params);
125
+ }
126
+ async getConversation(conversationId) {
127
+ return this.request("GET", `/v1/conversations/${conversationId}`);
128
+ }
129
+ async getMessages(conversationId) {
130
+ return this.request("GET", `/v1/conversations/${conversationId}/messages`);
131
+ }
132
+ async getUsage(days) {
133
+ const query = days ? `?days=${days}` : "";
134
+ return this.request("GET", `/v1/usage${query}`);
135
+ }
136
+ };
137
+ export {
138
+ JourneyHiveServerClient,
139
+ createJourneyHiveHandler,
140
+ verifyApiKey
141
+ };
package/package.json ADDED
@@ -0,0 +1,55 @@
1
+ {
2
+ "name": "@journeyrewards/hive-vercel",
3
+ "version": "1.0.0",
4
+ "description": "Vercel/Next.js SDK for Journey Hive Agent Orchestration",
5
+ "main": "dist/index.js",
6
+ "module": "dist/index.mjs",
7
+ "types": "dist/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "import": "./dist/index.mjs",
11
+ "require": "./dist/index.js",
12
+ "types": "./dist/index.d.ts"
13
+ },
14
+ "./server": {
15
+ "import": "./dist/server.mjs",
16
+ "require": "./dist/server.js",
17
+ "types": "./dist/server.d.ts"
18
+ }
19
+ },
20
+ "files": [
21
+ "dist",
22
+ "README.md"
23
+ ],
24
+ "scripts": {
25
+ "build": "tsup src/index.ts src/server.ts --format cjs,esm --dts --clean --external react --external next --external @journeyrewards/hive-sdk",
26
+ "prepublishOnly": "npm run build"
27
+ },
28
+ "repository": {
29
+ "type": "git",
30
+ "url": "https://github.com/journeyrewards/hive-vercel"
31
+ },
32
+ "homepage": "https://journey-hive.replit.app/api-guides",
33
+ "license": "MIT",
34
+ "keywords": [
35
+ "journeyrewards",
36
+ "journey-hive",
37
+ "ai-agents",
38
+ "vercel",
39
+ "nextjs",
40
+ "sdk"
41
+ ],
42
+ "engines": {
43
+ "node": ">=18"
44
+ },
45
+ "peerDependencies": {
46
+ "react": ">=18",
47
+ "next": ">=14",
48
+ "@journeyrewards/hive-sdk": "^1.0.0"
49
+ },
50
+ "devDependencies": {
51
+ "tsup": "^8.0.0",
52
+ "typescript": "^5.4.0",
53
+ "@types/react": "^18.0.0"
54
+ }
55
+ }