@journeyrewards/hive-sdk 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.
package/README.md ADDED
@@ -0,0 +1,202 @@
1
+ # @journeyrewards/hive-sdk
2
+
3
+ TypeScript SDK for the [Journey Hive](https://journey-hive.replit.app) Agent Orchestration API.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install @journeyrewards/hive-sdk
9
+ ```
10
+
11
+ ## Quick Start
12
+
13
+ ```typescript
14
+ import { JourneyHiveClient } from "@journeyrewards/hive-sdk";
15
+
16
+ const client = new JourneyHiveClient({
17
+ apiKey: "jh_live_...",
18
+ });
19
+
20
+ // Send a message (uses default controller agent if configured)
21
+ const response = await client.responses.create({
22
+ input: "What rooms are available tonight?",
23
+ });
24
+
25
+ console.log(response.output[0].content[0]);
26
+ ```
27
+
28
+ ## Configuration
29
+
30
+ ```typescript
31
+ const client = new JourneyHiveClient({
32
+ apiKey: "jh_live_...",
33
+ baseUrl: "https://your-instance.replit.app", // optional
34
+ timeout: 30000, // optional, ms
35
+ defaultAgentId: "uuid-of-your-agent", // optional
36
+ debug: true, // optional, logs requests
37
+ });
38
+ ```
39
+
40
+ | Option | Type | Default | Description |
41
+ |--------|------|---------|-------------|
42
+ | `apiKey` | `string` | required | Your API key (starts with `jh_live_` or `jh_test_`) |
43
+ | `baseUrl` | `string` | `https://journey-hive.replit.app` | API base URL |
44
+ | `timeout` | `number` | `30000` | Request timeout in milliseconds |
45
+ | `defaultAgentId` | `string` | `undefined` | Default agent ID for requests without `agent_id` |
46
+ | `debug` | `boolean` | `false` | Enable request/response logging |
47
+
48
+ ## Sending Messages
49
+
50
+ ### Synchronous
51
+
52
+ ```typescript
53
+ const response = await client.responses.create({
54
+ input: "Check me in to room 304",
55
+ agent_id: "optional-if-default-configured",
56
+ });
57
+
58
+ console.log(response.output[0].content[0].text);
59
+ ```
60
+
61
+ ### Streaming (SSE)
62
+
63
+ ```typescript
64
+ const stream = await client.responses.create({
65
+ input: "Tell me about today's arrivals",
66
+ stream: true,
67
+ });
68
+
69
+ for await (const event of stream) {
70
+ if (event.type === "response.output_text.delta") {
71
+ process.stdout.write(event.delta);
72
+ }
73
+ if (event.type === "response.completed") {
74
+ console.log("\n\nTokens used:", event.usage.total_tokens);
75
+ }
76
+ }
77
+ ```
78
+
79
+ ### With Conversation Context
80
+
81
+ ```typescript
82
+ const conv = await client.conversations.create({});
83
+
84
+ const r1 = await client.responses.create({
85
+ input: "I need to check in a guest",
86
+ conversation_id: conv.id,
87
+ });
88
+
89
+ const r2 = await client.responses.create({
90
+ input: "The guest name is John Smith, confirmation #12345",
91
+ conversation_id: conv.id,
92
+ });
93
+ ```
94
+
95
+ ### Idempotency
96
+
97
+ ```typescript
98
+ const response = await client.responses.create({
99
+ input: "Process payment for room 304",
100
+ idempotency_key: "payment-304-2024-01-15",
101
+ });
102
+ ```
103
+
104
+ ## Conversations
105
+
106
+ ```typescript
107
+ // List conversations with cursor pagination
108
+ const list = await client.conversations.list({ limit: 20 });
109
+
110
+ if (list.has_more && list.next_cursor) {
111
+ const next = await client.conversations.list({
112
+ limit: 20,
113
+ cursor: list.next_cursor,
114
+ });
115
+ }
116
+
117
+ // Get conversation messages
118
+ const messages = await client.conversations.messages(conv.id);
119
+
120
+ // Close a conversation
121
+ await client.conversations.close(conv.id);
122
+ ```
123
+
124
+ ## Agents
125
+
126
+ ```typescript
127
+ const agents = await client.agents.list();
128
+ const agent = await client.agents.get("agent-uuid");
129
+ ```
130
+
131
+ ## Usage & Analytics
132
+
133
+ ```typescript
134
+ const usage = await client.usage.get(7);
135
+ console.log(`Total requests: ${usage.total_requests}`);
136
+ console.log(`Total tokens: ${usage.total_tokens}`);
137
+ ```
138
+
139
+ ## Error Handling
140
+
141
+ ```typescript
142
+ import { JourneyHiveError, AuthenticationError, RateLimitError } from "@journeyrewards/hive-sdk";
143
+
144
+ try {
145
+ await client.responses.create({ input: "Hello" });
146
+ } catch (err) {
147
+ if (err instanceof RateLimitError) {
148
+ console.log("Rate limited, retrying...");
149
+ } else if (err instanceof AuthenticationError) {
150
+ console.log("Invalid API key");
151
+ } else if (err instanceof JourneyHiveError) {
152
+ console.log(`API Error: ${err.message} (${err.code})`);
153
+ }
154
+ }
155
+ ```
156
+
157
+ ## Debug Mode
158
+
159
+ ```typescript
160
+ const client = new JourneyHiveClient({
161
+ apiKey: "jh_live_...",
162
+ debug: true,
163
+ });
164
+
165
+ // Logs:
166
+ // [JourneyHive >>>] 2024-01-15T10:30:00.000Z POST /v1/responses { ... }
167
+ // [JourneyHive <<<] 200 /v1/responses [req_id: abc123]
168
+ ```
169
+
170
+ ## API Reference
171
+
172
+ | Method | HTTP | Description |
173
+ |--------|------|-------------|
174
+ | `responses.create(params)` | `POST /v1/responses` | Create a response (stream or sync) |
175
+ | `responses.get(id)` | `GET /v1/responses/:id` | Retrieve a response |
176
+ | `conversations.create(params)` | `POST /v1/conversations` | Create a conversation |
177
+ | `conversations.get(id)` | `GET /v1/conversations/:id` | Get conversation details |
178
+ | `conversations.list(params?)` | `GET /v1/conversations` | List conversations |
179
+ | `conversations.messages(id)` | `GET /v1/conversations/:id/messages` | Get conversation messages |
180
+ | `conversations.close(id)` | `DELETE /v1/conversations/:id` | Close a conversation |
181
+ | `agents.list()` | `GET /v1/agents` | List available agents |
182
+ | `agents.get(id)` | `GET /v1/agents/:id` | Get agent details |
183
+ | `agents.update(id, params)` | `PATCH /v1/agents/:id` | Update agent configuration |
184
+ | `usage.get(days?)` | `GET /v1/usage` | Get usage statistics |
185
+
186
+ ## Stream Event Types
187
+
188
+ | Event Type | Description |
189
+ |------------|-------------|
190
+ | `response.created` | Response object created |
191
+ | `response.in_progress` | Agent is processing |
192
+ | `response.output_item.added` | New output item started |
193
+ | `response.output_text.delta` | Incremental text chunk |
194
+ | `response.output_text.done` | Full text output complete |
195
+ | `response.content_part.done` | Content part finalized |
196
+ | `response.output_item.done` | Output item finalized |
197
+ | `response.completed` | Response fully complete with usage |
198
+ | `response.failed` | Response generation failed |
199
+
200
+ ## License
201
+
202
+ MIT
@@ -0,0 +1,273 @@
1
+ interface JourneyHiveConfig {
2
+ apiKey: string;
3
+ baseUrl?: string;
4
+ timeout?: number;
5
+ defaultAgentId?: string;
6
+ debug?: boolean;
7
+ }
8
+ interface Usage {
9
+ input_tokens: number;
10
+ output_tokens: number;
11
+ total_tokens: number;
12
+ }
13
+ type ContentPart = {
14
+ type: "output_text";
15
+ text: string;
16
+ } | {
17
+ type: "tool_use";
18
+ name: string;
19
+ arguments: Record<string, unknown>;
20
+ };
21
+ interface OutputItem {
22
+ id: string;
23
+ type: "message";
24
+ role: string;
25
+ content: ContentPart[];
26
+ }
27
+ interface Response {
28
+ id: string;
29
+ object: "response";
30
+ status: string;
31
+ agent_id: string;
32
+ conversation_id: string;
33
+ output: OutputItem[];
34
+ usage: Usage;
35
+ created_at: number;
36
+ metadata?: Record<string, unknown>;
37
+ }
38
+ interface Conversation {
39
+ id: string;
40
+ object: "conversation";
41
+ agent_id: string;
42
+ status: string;
43
+ channel: string;
44
+ external_user_id: string | null;
45
+ created_at: number;
46
+ updated_at: number;
47
+ metadata?: Record<string, unknown>;
48
+ }
49
+ interface Message {
50
+ id: string;
51
+ object: "message";
52
+ role: string;
53
+ content: ContentPart[];
54
+ created_at: number;
55
+ metadata?: Record<string, unknown>;
56
+ }
57
+ interface Agent {
58
+ id: string;
59
+ object: "agent";
60
+ name: string;
61
+ slug: string;
62
+ type: string;
63
+ status: string;
64
+ external_access: string;
65
+ model: string;
66
+ created_at: number;
67
+ metadata?: Record<string, unknown>;
68
+ }
69
+ interface ListResponse<T> {
70
+ object: "list";
71
+ data: T[];
72
+ has_more: boolean;
73
+ next_cursor?: string | null;
74
+ }
75
+ interface PaginationParams {
76
+ cursor?: string;
77
+ limit?: number;
78
+ }
79
+ interface InputItem {
80
+ role?: string;
81
+ content?: string;
82
+ text?: string;
83
+ }
84
+ interface CreateResponseParams {
85
+ agent_id?: string;
86
+ input: string | InputItem[];
87
+ conversation_id?: string;
88
+ stream?: boolean;
89
+ instructions?: string;
90
+ metadata?: Record<string, unknown>;
91
+ idempotency_key?: string;
92
+ }
93
+ interface CreateConversationParams {
94
+ agent_id?: string;
95
+ external_user_id?: string;
96
+ metadata?: Record<string, unknown>;
97
+ idempotency_key?: string;
98
+ }
99
+ interface UpdateAgentParams {
100
+ soul_template?: string;
101
+ personality_notes?: string;
102
+ metadata?: Record<string, unknown>;
103
+ }
104
+ interface ListConversationsParams extends PaginationParams {
105
+ agent_id?: string;
106
+ status?: string;
107
+ }
108
+ interface ListAgentsParams extends PaginationParams {
109
+ }
110
+ interface ListMessagesParams extends PaginationParams {
111
+ }
112
+ interface UsageRequest {
113
+ endpoint: string;
114
+ method: string;
115
+ status_code: number;
116
+ tokens_used: number;
117
+ latency_ms: number;
118
+ created_at: number;
119
+ }
120
+ interface UsageSummary {
121
+ object: string;
122
+ period_days: number;
123
+ total_requests: number;
124
+ total_tokens: number;
125
+ avg_latency_ms: number;
126
+ recent_requests: UsageRequest[];
127
+ }
128
+ interface ResponseCreatedEvent {
129
+ type: "response.created";
130
+ id: string;
131
+ object: "response";
132
+ status: string;
133
+ agent_id: string;
134
+ conversation_id: string;
135
+ created_at: number;
136
+ output: OutputItem[];
137
+ usage: Usage | null;
138
+ }
139
+ interface ResponseInProgressEvent {
140
+ type: "response.in_progress";
141
+ message: string;
142
+ }
143
+ interface OutputItemAddedEvent {
144
+ type: "response.output_item.added";
145
+ output_index: number;
146
+ item: OutputItem;
147
+ }
148
+ interface OutputTextDeltaEvent {
149
+ type: "response.output_text.delta";
150
+ output_index: number;
151
+ content_index: number;
152
+ delta: string;
153
+ }
154
+ interface OutputTextDoneEvent {
155
+ type: "response.output_text.done";
156
+ output_index: number;
157
+ content_index: number;
158
+ text: string;
159
+ }
160
+ interface ContentPartDoneEvent {
161
+ type: "response.content_part.done";
162
+ output_index: number;
163
+ content_index: number;
164
+ part: ContentPart;
165
+ }
166
+ interface OutputItemDoneEvent {
167
+ type: "response.output_item.done";
168
+ output_index: number;
169
+ item: OutputItem;
170
+ }
171
+ interface ResponseCompletedEvent {
172
+ type: "response.completed";
173
+ id: string;
174
+ object: "response";
175
+ status: string;
176
+ agent_id: string;
177
+ conversation_id: string;
178
+ output: OutputItem[];
179
+ usage: Usage;
180
+ created_at: number;
181
+ metadata?: Record<string, unknown>;
182
+ }
183
+ interface ResponseFailedEvent {
184
+ type: "response.failed";
185
+ error: {
186
+ message: string;
187
+ type: string;
188
+ code: string;
189
+ };
190
+ }
191
+ type StreamEvent = ResponseCreatedEvent | ResponseInProgressEvent | OutputItemAddedEvent | OutputTextDeltaEvent | OutputTextDoneEvent | ContentPartDoneEvent | OutputItemDoneEvent | ResponseCompletedEvent | ResponseFailedEvent;
192
+
193
+ declare class JourneyHiveClient {
194
+ private apiKey;
195
+ private baseUrl;
196
+ private timeout;
197
+ private defaultAgentId?;
198
+ private debug;
199
+ responses: ResponsesAPI;
200
+ conversations: ConversationsAPI;
201
+ agents: AgentsAPI;
202
+ usage: UsageAPI;
203
+ constructor(config: JourneyHiveConfig);
204
+ getDefaultAgentId(): string | undefined;
205
+ private log;
206
+ _fetch(path: string, options?: RequestInit & {
207
+ retries?: number;
208
+ idempotencyKey?: string;
209
+ }): Promise<globalThis.Response>;
210
+ }
211
+ declare class ResponsesAPI {
212
+ private client;
213
+ constructor(client: JourneyHiveClient);
214
+ private resolveAgentId;
215
+ create(params: CreateResponseParams & {
216
+ stream: true;
217
+ }): Promise<AsyncGenerator<StreamEvent>>;
218
+ create(params: CreateResponseParams & {
219
+ stream?: false;
220
+ }): Promise<Response>;
221
+ create(params: CreateResponseParams): Promise<Response | AsyncGenerator<StreamEvent>>;
222
+ get(id: string): Promise<Response>;
223
+ }
224
+ declare class ConversationsAPI {
225
+ private client;
226
+ constructor(client: JourneyHiveClient);
227
+ private resolveAgentId;
228
+ create(params: CreateConversationParams): Promise<Conversation>;
229
+ get(id: string): Promise<Conversation>;
230
+ list(params?: ListConversationsParams): Promise<ListResponse<Conversation>>;
231
+ messages(id: string, params?: ListMessagesParams): Promise<ListResponse<Message>>;
232
+ close(id: string): Promise<void>;
233
+ }
234
+ declare class AgentsAPI {
235
+ private client;
236
+ constructor(client: JourneyHiveClient);
237
+ list(params?: ListAgentsParams): Promise<ListResponse<Agent>>;
238
+ get(id: string): Promise<Agent>;
239
+ update(id: string, params: UpdateAgentParams): Promise<Agent>;
240
+ }
241
+ declare class UsageAPI {
242
+ private client;
243
+ constructor(client: JourneyHiveClient);
244
+ get(days?: number): Promise<UsageSummary>;
245
+ }
246
+
247
+ declare class JourneyHiveError extends Error {
248
+ status: number;
249
+ type: string;
250
+ code: string;
251
+ constructor(status: number, type: string, code: string, message: string);
252
+ }
253
+ declare class AuthenticationError extends JourneyHiveError {
254
+ constructor(code: string, message: string);
255
+ }
256
+ declare class RateLimitError extends JourneyHiveError {
257
+ constructor(code: string, message: string);
258
+ }
259
+ declare class NotFoundError extends JourneyHiveError {
260
+ constructor(code: string, message: string);
261
+ }
262
+ declare class PermissionError extends JourneyHiveError {
263
+ constructor(code: string, message: string);
264
+ }
265
+ declare function parseErrorResponse(status: number, body: {
266
+ error?: {
267
+ message?: string;
268
+ type?: string;
269
+ code?: string;
270
+ };
271
+ }): JourneyHiveError;
272
+
273
+ export { type Agent, AuthenticationError, type ContentPart, type ContentPartDoneEvent, type Conversation, type CreateConversationParams, type CreateResponseParams, type InputItem, JourneyHiveClient, type JourneyHiveConfig, JourneyHiveError, type ListAgentsParams, type ListConversationsParams, type ListMessagesParams, type ListResponse, type Message, NotFoundError, type OutputItem, type OutputItemAddedEvent, type OutputItemDoneEvent, type OutputTextDeltaEvent, type OutputTextDoneEvent, type PaginationParams, PermissionError, RateLimitError, type Response, type ResponseCompletedEvent, type ResponseCreatedEvent, type ResponseFailedEvent, type ResponseInProgressEvent, type StreamEvent, type UpdateAgentParams, type Usage, type UsageRequest, type UsageSummary, parseErrorResponse };