hereco 0.2.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.
Files changed (3) hide show
  1. package/index.d.ts +48 -0
  2. package/index.js +149 -0
  3. package/package.json +17 -0
package/index.d.ts ADDED
@@ -0,0 +1,48 @@
1
+ declare module 'hereco' {
2
+ export interface HerecoOptions {
3
+ apiKey?: string;
4
+ baseUrl?: string;
5
+ timeout?: number;
6
+ }
7
+
8
+ export interface ChatOptions {
9
+ message: string;
10
+ model?: string;
11
+ system?: string;
12
+ temperature?: number;
13
+ maxTokens?: number;
14
+ }
15
+
16
+ export interface ChatResponse {
17
+ outputText: string;
18
+ model: string;
19
+ usage: Record<string, number>;
20
+ toString(): string;
21
+ }
22
+
23
+ export interface KeyValidation {
24
+ valid: boolean;
25
+ keyId?: string;
26
+ permissions?: string[];
27
+ credits?: { balance: number; balanceFormatted: string };
28
+ rateLimit?: { limit: number; remaining: number };
29
+ error?: string;
30
+ }
31
+
32
+ export class Chat {
33
+ create(options: string | ChatOptions): Promise<ChatResponse>;
34
+ }
35
+
36
+ export class Hereco {
37
+ constructor(options?: HerecoOptions);
38
+ chat: Chat;
39
+ testKey(): Promise<KeyValidation>;
40
+ ask(message: string, options?: Omit<ChatOptions, 'message'>): Promise<string>;
41
+ }
42
+
43
+ export class HerecoBotError extends Error {
44
+ status: number;
45
+ }
46
+
47
+ export default Hereco;
48
+ }
package/index.js ADDED
@@ -0,0 +1,149 @@
1
+ /**
2
+ * Hereco AI SDK for JavaScript/Node.js
3
+ * @module hereco
4
+ */
5
+
6
+ class Hereco {
7
+ /**
8
+ * Create a Hereco client
9
+ * @param {Object} options
10
+ * @param {string} options.apiKey - Your Hereco API key
11
+ * @param {string} [options.baseUrl] - API base URL (default: https://hereco.xyz)
12
+ * @param {number} [options.timeout] - Request timeout in ms (default: 30000)
13
+ */
14
+ constructor(options = {}) {
15
+ this.apiKey = options.apiKey || process.env.HERECO_API_KEY;
16
+ this.baseUrl = (options.baseUrl || process.env.HERECO_API_BASE || 'https://hereco.xyz').replace(/\/$/, '');
17
+ this.timeout = options.timeout || 30000;
18
+
19
+ if (!this.apiKey) {
20
+ throw new Error('API key is required. Set HERECO_API_KEY or pass apiKey option.');
21
+ }
22
+
23
+ this.chat = new Chat(this);
24
+ }
25
+
26
+ async _request(endpoint, options = {}) {
27
+ const url = `${this.baseUrl}${endpoint}`;
28
+ const controller = new AbortController();
29
+ const timeoutId = setTimeout(() => controller.abort(), this.timeout);
30
+
31
+ try {
32
+ const response = await fetch(url, {
33
+ ...options,
34
+ headers: {
35
+ 'Authorization': `Bearer ${this.apiKey}`,
36
+ 'Content-Type': 'application/json',
37
+ ...options.headers,
38
+ },
39
+ signal: controller.signal,
40
+ });
41
+
42
+ clearTimeout(timeoutId);
43
+
44
+ if (!response.ok) {
45
+ const error = await response.json().catch(() => ({}));
46
+ throw new HerecoBotError(error.error || `HTTP ${response.status}`, response.status);
47
+ }
48
+
49
+ return response.json();
50
+ } catch (err) {
51
+ clearTimeout(timeoutId);
52
+ if (err.name === 'AbortError') {
53
+ throw new HerecoBotError('Request timeout', 408);
54
+ }
55
+ throw err;
56
+ }
57
+ }
58
+
59
+ /**
60
+ * Test if API key is valid
61
+ * @returns {Promise<Object>} Validation result
62
+ */
63
+ async testKey() {
64
+ try {
65
+ const result = await this._request('/api/v1/validate');
66
+ return {
67
+ valid: true,
68
+ keyId: result.keyId,
69
+ permissions: result.permissions,
70
+ credits: result.credits,
71
+ rateLimit: result.rateLimit,
72
+ };
73
+ } catch (err) {
74
+ return {
75
+ valid: false,
76
+ error: err.message,
77
+ };
78
+ }
79
+ }
80
+
81
+ /**
82
+ * Simple one-liner to ask a question
83
+ * @param {string} message - Question to ask
84
+ * @param {Object} [options] - Additional options
85
+ * @returns {Promise<string>} Response text
86
+ */
87
+ async ask(message, options = {}) {
88
+ const response = await this.chat.create({ message, ...options });
89
+ return response.outputText;
90
+ }
91
+ }
92
+
93
+ class Chat {
94
+ constructor(client) {
95
+ this._client = client;
96
+ }
97
+
98
+ /**
99
+ * Create a chat completion
100
+ * @param {Object} options
101
+ * @param {string} options.message - User message
102
+ * @param {string} [options.model] - Model name (default: 'default')
103
+ * @param {string} [options.system] - System prompt
104
+ * @param {number} [options.temperature] - Temperature (0-1)
105
+ * @param {number} [options.maxTokens] - Max tokens
106
+ * @returns {Promise<ChatResponse>}
107
+ */
108
+ async create(options) {
109
+ const { message, model = 'default', system, temperature = 0.7, maxTokens = 512 } =
110
+ typeof options === 'string' ? { message: options } : options;
111
+
112
+ const result = await this._client._request('/api/v1/chat', {
113
+ method: 'POST',
114
+ body: JSON.stringify({
115
+ message,
116
+ model,
117
+ system,
118
+ temperature,
119
+ max_tokens: maxTokens,
120
+ }),
121
+ });
122
+
123
+ return new ChatResponse(result);
124
+ }
125
+ }
126
+
127
+ class ChatResponse {
128
+ constructor(data) {
129
+ this.outputText = data.output_text || data.response || '';
130
+ this.model = data.model || 'default';
131
+ this.usage = data.usage || {};
132
+ }
133
+
134
+ toString() {
135
+ return this.outputText;
136
+ }
137
+ }
138
+
139
+ class HerecoBotError extends Error {
140
+ constructor(message, status) {
141
+ super(message);
142
+ this.name = 'HerecoBotError';
143
+ this.status = status;
144
+ }
145
+ }
146
+
147
+ // CommonJS export
148
+ module.exports = { Hereco, HerecoBotError, ChatResponse };
149
+ module.exports.default = Hereco;
package/package.json ADDED
@@ -0,0 +1,17 @@
1
+ {
2
+ "name": "hereco",
3
+ "version": "0.2.0",
4
+ "description": "Official Hereco AI SDK for JavaScript/Node.js",
5
+ "main": "index.js",
6
+ "types": "index.d.ts",
7
+ "keywords": ["hereco", "ai", "chatbot", "sdk", "api"],
8
+ "author": "Hereco",
9
+ "license": "MIT",
10
+ "repository": {
11
+ "type": "git",
12
+ "url": "https://github.com/hereco/hereco-node"
13
+ },
14
+ "engines": {
15
+ "node": ">=18.0.0"
16
+ }
17
+ }