@unispark.ai/agent-hub 0.1.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 @@
1
+ export {};
@@ -0,0 +1,69 @@
1
+ #!/usr/bin/env node
2
+ import { n as SdkError, t as AgentHubSDK } from "../sdk-DsYThTRp.js";
3
+ import { Command } from "commander";
4
+ //#region src/cli/output.ts
5
+ /** Write a success JSON envelope to stdout. */
6
+ function success(data) {
7
+ process.stdout.write(`${JSON.stringify({
8
+ ok: true,
9
+ data
10
+ })}\n`);
11
+ }
12
+ /** Write an error JSON envelope to stderr and exit with the given code. */
13
+ function fail(code, message, exitCode = 1) {
14
+ process.stderr.write(`${JSON.stringify({
15
+ ok: false,
16
+ error: {
17
+ code,
18
+ message
19
+ }
20
+ })}\n`);
21
+ process.exit(exitCode);
22
+ }
23
+ //#endregion
24
+ //#region src/cli/index.ts
25
+ function resolveConfig() {
26
+ const token = process.env.AGENT_HUB_TOKEN;
27
+ if (!token) fail("MISSING_TOKEN", "AGENT_HUB_TOKEN environment variable is required.", 2);
28
+ return {
29
+ serverUrl: process.env.AGENT_HUB_SERVER ?? "http://localhost:8000",
30
+ token
31
+ };
32
+ }
33
+ function createSdk() {
34
+ return new AgentHubSDK(resolveConfig());
35
+ }
36
+ function handleError(error) {
37
+ if (error instanceof SdkError) {
38
+ const exitCode = error.statusCode === 401 ? 3 : 1;
39
+ fail(`HTTP_${error.statusCode}`, error.message, exitCode);
40
+ }
41
+ if (error instanceof TypeError && "cause" in error) fail("CONNECTION_ERROR", `Cannot connect to server: ${error.message}`, 6);
42
+ fail("UNKNOWN_ERROR", error instanceof Error ? error.message : String(error), 1);
43
+ }
44
+ const program = new Command();
45
+ program.name("agent-hub").description("Agent Hub CLI — agent-facing entry point").version("0.1.0");
46
+ program.command("register").description("Register this agent and return identity info").action(async () => {
47
+ try {
48
+ success(await createSdk().register());
49
+ } catch (error) {
50
+ handleError(error);
51
+ }
52
+ });
53
+ program.command("pull").description("Pull pending messages from inbox").option("-l, --limit <number>", "Maximum entries to return", "10").option("-a, --ack", "Automatically ACK entries after pulling").action(async (options) => {
54
+ try {
55
+ const sdk = createSdk();
56
+ const limit = Number.parseInt(options.limit, 10);
57
+ if (Number.isNaN(limit) || limit < 1 || limit > 50) fail("INVALID_LIMIT", "Limit must be between 1 and 50.", 2);
58
+ const result = await sdk.pull(limit);
59
+ if (options.ack && result.entries.length > 0) await Promise.all(result.entries.map((entry) => sdk.ack(entry.id)));
60
+ success(result);
61
+ } catch (error) {
62
+ handleError(error);
63
+ }
64
+ });
65
+ program.parse();
66
+ //#endregion
67
+ export {};
68
+
69
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","names":[],"sources":["../../src/cli/output.ts","../../src/cli/index.ts"],"sourcesContent":["/** Write a success JSON envelope to stdout. */\nexport function success(data: unknown): void {\n process.stdout.write(`${JSON.stringify({ ok: true, data })}\\n`);\n}\n\n/** Write an error JSON envelope to stderr and exit with the given code. */\nexport function fail(code: string, message: string, exitCode = 1): never {\n process.stderr.write(`${JSON.stringify({ ok: false, error: { code, message } })}\\n`);\n process.exit(exitCode);\n}\n","#!/usr/bin/env node\n\nimport { Command } from \"commander\";\nimport { AgentHubSDK, SdkError } from \"../sdk.js\";\nimport { fail, success } from \"./output.js\";\n\nfunction resolveConfig(): { serverUrl: string; token: string } {\n const token = process.env.AGENT_HUB_TOKEN;\n if (!token) {\n fail(\"MISSING_TOKEN\", \"AGENT_HUB_TOKEN environment variable is required.\", 2);\n }\n const serverUrl = process.env.AGENT_HUB_SERVER ?? \"http://localhost:8000\";\n return { serverUrl, token };\n}\n\nfunction createSdk(): AgentHubSDK {\n const config = resolveConfig();\n return new AgentHubSDK(config);\n}\n\nfunction handleError(error: unknown): never {\n if (error instanceof SdkError) {\n const exitCode = error.statusCode === 401 ? 3 : 1;\n fail(`HTTP_${error.statusCode}`, error.message, exitCode);\n }\n if (error instanceof TypeError && \"cause\" in error) {\n fail(\"CONNECTION_ERROR\", `Cannot connect to server: ${error.message}`, 6);\n }\n const msg = error instanceof Error ? error.message : String(error);\n fail(\"UNKNOWN_ERROR\", msg, 1);\n}\n\nconst program = new Command();\n\nprogram.name(\"agent-hub\").description(\"Agent Hub CLI — agent-facing entry point\").version(\"0.1.0\");\n\nprogram\n .command(\"register\")\n .description(\"Register this agent and return identity info\")\n .action(async () => {\n try {\n const sdk = createSdk();\n const result = await sdk.register();\n success(result);\n } catch (error) {\n handleError(error);\n }\n });\n\nprogram\n .command(\"pull\")\n .description(\"Pull pending messages from inbox\")\n .option(\"-l, --limit <number>\", \"Maximum entries to return\", \"10\")\n .option(\"-a, --ack\", \"Automatically ACK entries after pulling\")\n .action(async (options: { limit: string; ack?: boolean }) => {\n try {\n const sdk = createSdk();\n const limit = Number.parseInt(options.limit, 10);\n if (Number.isNaN(limit) || limit < 1 || limit > 50) {\n fail(\"INVALID_LIMIT\", \"Limit must be between 1 and 50.\", 2);\n }\n const result = await sdk.pull(limit);\n\n if (options.ack && result.entries.length > 0) {\n await Promise.all(result.entries.map((entry) => sdk.ack(entry.id)));\n }\n\n success(result);\n } catch (error) {\n handleError(error);\n }\n });\n\nprogram.parse();\n"],"mappings":";;;;;AACA,SAAgB,QAAQ,MAAqB;AAC3C,SAAQ,OAAO,MAAM,GAAG,KAAK,UAAU;EAAE,IAAI;EAAM;EAAM,CAAC,CAAC,IAAI;;;AAIjE,SAAgB,KAAK,MAAc,SAAiB,WAAW,GAAU;AACvE,SAAQ,OAAO,MAAM,GAAG,KAAK,UAAU;EAAE,IAAI;EAAO,OAAO;GAAE;GAAM;GAAS;EAAE,CAAC,CAAC,IAAI;AACpF,SAAQ,KAAK,SAAS;;;;ACFxB,SAAS,gBAAsD;CAC7D,MAAM,QAAQ,QAAQ,IAAI;AAC1B,KAAI,CAAC,MACH,MAAK,iBAAiB,qDAAqD,EAAE;AAG/E,QAAO;EAAE,WADS,QAAQ,IAAI,oBAAo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
2
+ type SdkConfig = {
3
+ serverUrl: string;
4
+ token: string;
5
+ };
6
+ type RegisterResult = {
7
+ agentId: string;
8
+ inboxId: string;
9
+ status: string;
10
+ displayName: string | null;
11
+ };
12
+ type PullResult = {
13
+ entries: InboxEntryWithMessage[];
14
+ };
15
+ declare class AgentHubSDK {
16
+ private readonly baseUrl;
17
+ private readonly token;
18
+ constructor(config: SdkConfig);
19
+ /** Validate token, return agent identity. */
20
+ register(): Promise<RegisterResult>;
21
+ /** Fetch pending inbox entries. */
22
+ pull(limit?: number): Promise<PullResult>;
23
+ /** Acknowledge an inbox entry. */
24
+ ack(entryId: number): Promise<void>;
25
+ /** Renew lease on an inbox entry. */
26
+ renew(entryId: number): Promise<void>;
27
+ private requestVoid;
28
+ private requestJson;
29
+ private doFetch;
30
+ private toSdkError;
31
+ }
32
+ declare class SdkError extends Error {
33
+ readonly statusCode: number;
34
+ constructor(statusCode: number, message: string);
35
+ }
36
+ //# sourceMappingURL=sdk.d.ts.map
37
+ //#endregion
38
+ export { AgentHubSDK, type PullResult, type RegisterResult, type SdkConfig, SdkError };
39
+ //# sourceMappingURL=index-BKH8sT1-.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index-BKH8sT1-.d.ts","names":[],"sources":["../src/sdk.ts"],"sourcesContent":[],"mappings":";AAEY,KAAA,SAAA,GAAS;EAKT,SAAA,EAAA,MAAc;EAOd,KAAA,EAAA,MAAU;AAItB,CAAA;AAAwB,KAXZ,cAAA,GAWY;SAIF,EAAA,MAAA;SAOM,EAAA,MAAA;QAAR,EAAA,MAAA;aAWc,EAAA,MAAA,GAAA,IAAA;;AAMJ,KAhClB,UAAA,GAgCkB;SAKE,EApCrB,qBAoCqB,EAAA;CAAO;AA4C1B,cA7EA,WAAA,CA6EsB;;;sBAzEb;;cAOF,QAAQ;;wBAWF,QAAQ;;wBAMJ;;0BAKE;;;;;;cA4CnB,QAAA,SAAiB,KAAA"}
package/dist/index.js ADDED
@@ -0,0 +1,2 @@
1
+ import { n as SdkError, t as AgentHubSDK } from "./sdk-DsYThTRp.js";
2
+ export { AgentHubSDK, SdkError };
@@ -0,0 +1,70 @@
1
+ //#region src/sdk.ts
2
+ var AgentHubSDK = class {
3
+ baseUrl;
4
+ token;
5
+ constructor(config) {
6
+ this.baseUrl = config.serverUrl.replace(/\/+$/, "");
7
+ this.token = config.token;
8
+ }
9
+ /** Validate token, return agent identity. */
10
+ async register() {
11
+ const agent = await this.requestJson("/api/v1/agent/me");
12
+ return {
13
+ agentId: agent.id,
14
+ inboxId: agent.inboxId,
15
+ status: agent.status,
16
+ displayName: agent.displayName
17
+ };
18
+ }
19
+ /** Fetch pending inbox entries. */
20
+ async pull(limit = 10) {
21
+ return { entries: await this.requestJson(`/api/v1/agent/inbox?limit=${limit}`) };
22
+ }
23
+ /** Acknowledge an inbox entry. */
24
+ async ack(entryId) {
25
+ await this.requestVoid(`/api/v1/agent/inbox/${entryId}/ack`, { method: "POST" });
26
+ }
27
+ /** Renew lease on an inbox entry. */
28
+ async renew(entryId) {
29
+ await this.requestVoid(`/api/v1/agent/inbox/${entryId}/renew`, { method: "POST" });
30
+ }
31
+ async requestVoid(path, init) {
32
+ const response = await this.doFetch(path, init);
33
+ if (!response.ok) throw await this.toSdkError(response);
34
+ }
35
+ async requestJson(path, init) {
36
+ const response = await this.doFetch(path, init);
37
+ if (!response.ok) throw await this.toSdkError(response);
38
+ return await response.json();
39
+ }
40
+ async doFetch(path, init) {
41
+ const url = `${this.baseUrl}${path}`;
42
+ const headers = { Authorization: `Bearer ${this.token}` };
43
+ if (init?.body) headers["Content-Type"] = "application/json";
44
+ return fetch(url, {
45
+ ...init,
46
+ headers
47
+ });
48
+ }
49
+ async toSdkError(response) {
50
+ const body = await response.text();
51
+ let message;
52
+ try {
53
+ message = JSON.parse(body).error ?? body;
54
+ } catch {
55
+ message = body;
56
+ }
57
+ return new SdkError(response.status, message);
58
+ }
59
+ };
60
+ var SdkError = class extends Error {
61
+ constructor(statusCode, message) {
62
+ super(message);
63
+ this.statusCode = statusCode;
64
+ this.name = "SdkError";
65
+ }
66
+ };
67
+ //#endregion
68
+ export { SdkError as n, AgentHubSDK as t };
69
+
70
+ //# sourceMappingURL=sdk-DsYThTRp.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sdk-DsYThTRp.js","names":[],"sources":["../src/sdk.ts"],"sourcesContent":["import type { Agent, InboxEntryWithMessage } from \"@agent-hub/shared\";\n\nexport type SdkConfig = {\n serverUrl: string;\n token: string;\n};\n\nexport type RegisterResult = {\n agentId: string;\n inboxId: string;\n status: string;\n displayName: string | null;\n};\n\nexport type PullResult = {\n entries: InboxEntryWithMessage[];\n};\n\nexport class AgentHubSDK {\n private readonly baseUrl: string;\n private readonly token: string;\n\n constructor(config: SdkConfig) {\n // Strip trailing slash\n this.baseUrl = config.serverUrl.replace(/\\/+$/, \"\");\n this.token = config.token;\n }\n\n /** Validate token, return agent identity. */\n async register(): Promise<RegisterResult> {\n const agent = await this.requestJson<Agent>(\"/api/v1/agent/me\");\n return {\n agentId: agent.id,\n inboxId: agent.inboxId,\n status: agent.status,\n displayName: agent.displayName,\n };\n }\n\n /** Fetch pending inbox entries. */\n async pull(limit = 10): Promise<PullResult> {\n const entries = await this.requestJson<InboxEntryWithMessage[]>(`/api/v1/agent/inbox?limit=${limit}`);\n return { entries };\n }\n\n /** Acknowledge an inbox entry. */\n async ack(entryId: number): Promise<void> {\n await this.requestVoid(`/api/v1/agent/inbox/${entryId}/ack`, { method: \"POST\" });\n }\n\n /** Renew lease on an inbox entry. */\n async renew(entryId: number): Promise<void> {\n await this.requestVoid(`/api/v1/agent/inbox/${entryId}/renew`, { method: \"POST\" });\n }\n\n private async requestVoid(path: string, init?: RequestInit): Promise<void> {\n const response = await this.doFetch(path, init);\n if (!response.ok) {\n throw await this.toSdkError(response);\n }\n }\n\n private async requestJson<T>(path: string, init?: RequestInit): Promise<T> {\n const response = await this.doFetch(path, init);\n if (!response.ok) {\n throw await this.toSdkError(response);\n }\n return (await response.json()) as T;\n }\n\n private async doFetch(path: string, init?: RequestInit): Promise<Response> {\n const url = `${this.baseUrl}${path}`;\n const headers: Record<string, string> = {\n Authorization: `Bearer ${this.token}`,\n };\n // Only set Content-Type for requests with a body — Fastify rejects empty JSON bodies\n if (init?.body) {\n headers[\"Content-Type\"] = \"application/json\";\n }\n return fetch(url, { ...init, headers });\n }\n\n private async toSdkError(response: Response): Promise<SdkError> {\n const body = await response.text();\n let message: string;\n try {\n const json = JSON.parse(body) as { error?: string };\n message = json.error ?? body;\n } catch {\n message = body;\n }\n return new SdkError(response.status, message);\n }\n}\n\nexport class SdkError extends Error {\n constructor(\n public readonly statusCode: number,\n message: string,\n ) {\n super(message);\n this.name = \"SdkError\";\n }\n}\n"],"mappings":";AAkBA,IAAa,cAAb,MAAyB;CACvB;CACA;CAEA,YAAY,QAAmB;AAE7B,OAAK,UAAU,OAAO,UAAU,QAAQ,QAAQ,GAAG;AACnD,OAAK,QAAQ,OAAO;;;CAItB,MAAM,WAAoC;EACxC,MAAM,QAAQ,MAAM,KAAK,YAAmB,mBAAmB;AAC/D,SAAO;GACL,SAAS,MAAM;GACf,SAAS,MAAM;GACf,QAAQ,MAAM;GACd,aAAa,MAAM;GACpB;;;CAIH,MAAM,KAAK,QAAQ,IAAyB;AAE1C,SAAO,EAAE,SADO,MAAM,KAAK,YAAqC,6BAA6B,QAAQ,EACnF;;;CAIpB,MAAM,IAAI,SAAgC;AACxC,QAAM,KAAK,YAAY,uBAAuB,QAAQ,OAAO,EAAE,QAAQ,QAAQ,CAAC;;;CAIlF,MAAM,MAAM,SAAgC;AAC1C,QAAM,KAAK,YAAY,uBAAuB,QAAQ,SAAS,EAAE,QAAQ,QAAQ,CAAC;;CAGpF,MAAc,YAAY,MAAc,MAAmC;EACzE,MAAM,WAAW,MAAM,KAAK,QAAQ,MAAM,KAAK;AAC/C,MAAI,CAAC,SAAS,GACZ,OAAM,MAAM,KAAK,WAAW,SAAS;;CAIzC,MAAc,YAAe,MAAc,MAAgC;EACzE,MAAM,WAAW,MAAM,KAAK,QAAQ,MAAM,KAAK;AAC/C,MAAI,CAAC,SAAS,GACZ,OAAM,MAAM,KAAK,WAAW,SAAS;AAEvC,SAAQ,MAAM,SAAS,MAAM;;CAG/B,MAAc,QAAQ,MAAc,MAAuC;EACzE,MAAM,MAAM,GAAG,KAAK,UAAU;EAC9B,MAAM,UAAkC,EACtC,eAAe,UAAU,KAAK,SAC/B;AAED,MAAI,MAAM,KACR,SAAQ,kBAAkB;AAE5B,SAAO,MAAM,KAAK;GAAE,GAAG;GAAM;GAAS,CAAC;;CAGzC,MAAc,WAAW,UAAuC;EAC9D,MAAM,OAAO,MAAM,SAAS,MAAM;EAClC,IAAI;AACJ,MAAI;AAEF,aADa,KAAK,MAAM,KAAK,CACd,SAAS;UAClB;AACN,aAAU;;AAEZ,SAAO,IAAI,SAAS,SAAS,QAAQ,QAAQ;;;AAIjD,IAAa,WAAb,cAA8B,MAAM;CAClC,YACE,YACA,SACA;AACA,QAAM,QAAQ;AAHE,OAAA,aAAA;AAIhB,OAAK,OAAO"}
package/package.json ADDED
@@ -0,0 +1,36 @@
1
+ {
2
+ "name": "@unispark.ai/agent-hub",
3
+ "version": "0.1.0",
4
+ "type": "module",
5
+ "description": "Agent Hub CLI and SDK — agent-facing client for Agent Hub server",
6
+ "exports": {
7
+ ".": {
8
+ "import": "./dist/index.js",
9
+ "types": "./dist/index.d.ts"
10
+ }
11
+ },
12
+ "bin": {
13
+ "agent-hub": "./dist/cli/index.js"
14
+ },
15
+ "files": [
16
+ "dist"
17
+ ],
18
+ "publishConfig": {
19
+ "access": "public"
20
+ },
21
+ "dependencies": {
22
+ "commander": "^13.1.0",
23
+ "zod": "^3.25.0"
24
+ },
25
+ "devDependencies": {
26
+ "@types/node": "^22.16.0",
27
+ "tsdown": "^0.12.0",
28
+ "vitest": "^3.2.0",
29
+ "@agent-hub/shared": "0.1.0"
30
+ },
31
+ "scripts": {
32
+ "build": "tsdown src/index.ts src/cli/index.ts --format esm --dts",
33
+ "typecheck": "tsc --noEmit",
34
+ "test": "vitest run --passWithNoTests"
35
+ }
36
+ }