hiloop-sdk 0.8.7 → 0.9.1

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/dist/client.d.ts CHANGED
@@ -17,8 +17,15 @@ export interface SendOptions {
17
17
  }
18
18
  export interface HiloopClientOptions {
19
19
  apiKey: string;
20
- /** Agent name. Auto-creates the agent if it doesn't exist (space API keys only). */
21
- agentName?: string;
20
+ /** Agent name or UUID. Sent as X-Hiloop header to identify the agent. */
21
+ agentName: string;
22
+ /**
23
+ * Space name. If provided, the SDK auto-creates the space on first use
24
+ * (idempotent — returns existing if slug matches). Requires a key with
25
+ * "owner" permission. The slug is derived from the name (lowercased,
26
+ * non-alphanumeric replaced with hyphens).
27
+ */
28
+ spaceName?: string;
22
29
  baseUrl?: string;
23
30
  /** Agent's X25519 secret key (base64). Required for E2E encryption. */
24
31
  secretKey?: string;
@@ -42,6 +49,8 @@ export declare class HiloopClient {
42
49
  private initialized;
43
50
  private initPromise;
44
51
  private options;
52
+ /** Cached spaceId after auto-creation resolves spaceName. */
53
+ private resolvedSpaceId;
45
54
  constructor(options: HiloopClientOptions);
46
55
  /**
47
56
  * Initialize encryption: derive keypair from API key, register public key,
package/dist/client.js CHANGED
@@ -14,11 +14,9 @@ export class HiloopClient {
14
14
  this.publicKeyB64 = "";
15
15
  this.initialized = false;
16
16
  this.initPromise = null;
17
+ /** Cached spaceId after auto-creation resolves spaceName. */
18
+ this.resolvedSpaceId = null;
17
19
  this.apiKey = options.apiKey;
18
- if (options.apiKey.startsWith("hlp_prov_")) {
19
- throw new Error("Provisioning keys (hlp_prov_*) cannot be used with HiloopClient. " +
20
- "Use the agent API key returned by POST /v1/provision/agents instead.");
21
- }
22
20
  this.baseUrl = (options.baseUrl ?? "https://api.hi-loop.com").replace(/\/$/, "");
23
21
  this.timeout = options.timeout ?? 30000;
24
22
  this.options = options;
@@ -38,11 +36,7 @@ export class HiloopClient {
38
36
  * Initialize encryption: derive keypair from API key, register public key,
39
37
  * and fetch space encryption info. Called automatically on first API call.
40
38
  */
41
- async ensureInitialized(agentName) {
42
- // If an agentName is provided and we don't have one yet, store it for init
43
- if (agentName && !this.options.agentName) {
44
- this.options = { ...this.options, agentName };
45
- }
39
+ async ensureInitialized() {
46
40
  if (this.initialized)
47
41
  return;
48
42
  if (this.initPromise)
@@ -51,6 +45,17 @@ export class HiloopClient {
51
45
  await this.initPromise;
52
46
  }
53
47
  async doInit() {
48
+ // 0. Auto-create space if spaceName is provided (idempotent)
49
+ if (this.options.spaceName && !this.resolvedSpaceId) {
50
+ const slug = this.options.spaceName.toLowerCase().replace(/[^a-z0-9]/g, "-").slice(0, 100);
51
+ try {
52
+ const space = await this.rawRequest("POST", "/org/spaces", { body: { name: this.options.spaceName, slug } });
53
+ this.resolvedSpaceId = space.id;
54
+ }
55
+ catch {
56
+ // Space creation failed — may not have owner permission, continue without
57
+ }
58
+ }
54
59
  // 1. Derive keypair from API key (or use provided keys)
55
60
  let secretKey = this.options.secretKey;
56
61
  let publicKey = this.options.publicKey;
@@ -96,14 +101,11 @@ export class HiloopClient {
96
101
  }
97
102
  /** Raw HTTP request without triggering auto-init (used during init itself). */
98
103
  buildHeaders(agentName) {
99
- const h = {
104
+ return {
100
105
  "Content-Type": "application/json",
101
106
  "X-API-Key": this.apiKey,
107
+ "X-Hiloop": agentName ?? this.options.agentName,
102
108
  };
103
- const agent = agentName ?? this.options.agentName;
104
- if (agent)
105
- h["X-Agent"] = agent;
106
- return h;
107
109
  }
108
110
  async rawRequest(method, path, options) {
109
111
  const url = `${this.baseUrl}/v1${path}`;
@@ -319,7 +321,7 @@ export class HiloopClient {
319
321
  // -- Unified Message API ----------------------------------------------------
320
322
  /** Options for send / sendText. */
321
323
  async sendComponents(sessionId, components, opts) {
322
- await this.ensureInitialized(opts?.agentName);
324
+ await this.ensureInitialized();
323
325
  const encryptedComponents = await this.crypto.encryptSessionMessage(JSON.stringify(components));
324
326
  const body = { encryptedComponents };
325
327
  // Extract metadata from components for server-side routing
package/dist/ws.d.ts CHANGED
@@ -3,7 +3,7 @@
3
3
  *
4
4
  * Usage:
5
5
  * const ws = new HiloopWsClient({
6
- * apiKey: "hlp_space_...",
6
+ * apiKey: "hlp_...",
7
7
  * agentName: "my-bot",
8
8
  * });
9
9
  * ws.on("session.message.new", (msg) => console.log(msg.content));
@@ -13,8 +13,8 @@
13
13
  */
14
14
  export interface HiloopWsClientOptions {
15
15
  apiKey: string;
16
- /** Agent name (required for space API keys, auto-creates if needed). */
17
- agentName?: string;
16
+ /** Agent name or UUID. Sent as X-Hiloop header and query param. */
17
+ agentName: string;
18
18
  baseUrl?: string;
19
19
  /** Auto-reconnect on disconnect (default: true). */
20
20
  autoReconnect?: boolean;
package/dist/ws.js CHANGED
@@ -3,7 +3,7 @@
3
3
  *
4
4
  * Usage:
5
5
  * const ws = new HiloopWsClient({
6
- * apiKey: "hlp_space_...",
6
+ * apiKey: "hlp_...",
7
7
  * agentName: "my-bot",
8
8
  * });
9
9
  * ws.on("session.message.new", (msg) => console.log(msg.content));
@@ -41,8 +41,7 @@ export class HiloopWsClient {
41
41
  "X-API-Key": this.options.apiKey,
42
42
  "Content-Type": "application/json",
43
43
  };
44
- if (this.options.agentName)
45
- headers["X-Agent"] = this.options.agentName;
44
+ headers["X-Hiloop"] = this.options.agentName;
46
45
  // Register public key
47
46
  const fingerprint = await computeFingerprint(kp.publicKey);
48
47
  try {
@@ -70,9 +69,7 @@ export class HiloopWsClient {
70
69
  return new Promise((resolve, reject) => {
71
70
  const baseUrl = (this.options.baseUrl ?? "https://api.hi-loop.com").replace(/\/$/, "");
72
71
  const wsUrl = baseUrl.replace(/^http/, "ws");
73
- const params = new URLSearchParams({ key: this.options.apiKey });
74
- if (this.options.agentName)
75
- params.set("agent", this.options.agentName);
72
+ const params = new URLSearchParams({ key: this.options.apiKey, agent: this.options.agentName });
76
73
  this.ws = new WebSocket(`${wsUrl}/v1/agent/ws?${params}`);
77
74
  this.ws.onopen = () => {
78
75
  this.reconnectAttempts = 0;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hiloop-sdk",
3
- "version": "0.8.7",
3
+ "version": "0.9.1",
4
4
  "description": "TypeScript SDK for Hiloop — zero-trust human-AI agent interaction platform",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",