@robota-sdk/agent-transport-http 3.0.0-beta.43

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Robota Contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,62 @@
1
+ # @robota-sdk/agent-transport-http
2
+
3
+ Hono-based HTTP transport adapter for exposing `InteractiveSession` over REST API. Compatible with Cloudflare Workers, Node.js, and AWS Lambda.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ pnpm add @robota-sdk/agent-transport-http
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ ```typescript
14
+ import { createAgentRoutes } from '@robota-sdk/agent-transport-http';
15
+ import { InteractiveSession, SystemCommandExecutor } from '@robota-sdk/agent-sdk';
16
+ import { Hono } from 'hono';
17
+
18
+ const app = new Hono();
19
+
20
+ const routes = createAgentRoutes({
21
+ sessionFactory: (_req) => interactiveSession, // resolve per-request
22
+ commandExecutor,
23
+ });
24
+
25
+ // Mount under a prefix
26
+ app.route('/agent', routes);
27
+
28
+ // Or export directly for Cloudflare Workers
29
+ export default routes;
30
+ ```
31
+
32
+ The `sessionFactory` callback receives the Hono request context and returns an `InteractiveSession`. Use this to resolve sessions by auth token, session ID, or any request-scoped data.
33
+
34
+ ## Endpoints
35
+
36
+ | Method | Path | Request Body | Response | Description |
37
+ | ------ | ------------- | -------------------------------- | -------------------------------- | ---------------------------- |
38
+ | POST | /submit | `{ prompt: string }` | SSE stream | Submit prompt, stream events |
39
+ | POST | /command | `{ name: string, args: string }` | `ICommandResult` JSON | Execute system command |
40
+ | POST | /abort | — | `{ ok: true }` | Abort current execution |
41
+ | POST | /cancel-queue | — | `{ ok: true }` | Cancel queued prompt |
42
+ | GET | /messages | — | `TUniversalMessage[]` JSON | Get message history |
43
+ | GET | /context | — | `IContextWindowState` JSON | Get context window state |
44
+ | GET | /executing | — | `{ executing: boolean }` JSON | Check if currently executing |
45
+ | GET | /pending | — | `{ pending: string\|null }` JSON | Get queued prompt |
46
+
47
+ ### SSE Event Types (POST /submit)
48
+
49
+ | Event | Data | Description |
50
+ | ------------- | ------------------------- | ----------------------- |
51
+ | `text_delta` | `{ delta: string }` | Streaming text chunk |
52
+ | `tool_start` | `IToolState` | Tool execution began |
53
+ | `tool_end` | `IToolState` | Tool execution finished |
54
+ | `thinking` | `{ isThinking: boolean }` | Execution state changed |
55
+ | `complete` | `IExecutionResult` | Prompt completed |
56
+ | `interrupted` | `IExecutionResult` | Execution was aborted |
57
+ | `error` | `{ message: string }` | Execution error |
58
+
59
+ ## Dependencies
60
+
61
+ - `@robota-sdk/agent-sdk` — `InteractiveSession`, `SystemCommandExecutor`
62
+ - `hono` — HTTP framework
@@ -0,0 +1,123 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var index_exports = {};
22
+ __export(index_exports, {
23
+ createAgentRoutes: () => createAgentRoutes
24
+ });
25
+ module.exports = __toCommonJS(index_exports);
26
+
27
+ // src/routes.ts
28
+ var import_hono = require("hono");
29
+ var import_streaming = require("hono/streaming");
30
+ function createAgentRoutes(options) {
31
+ const { sessionFactory, commandExecutor } = options;
32
+ const app = new import_hono.Hono();
33
+ app.post("/submit", async (c) => {
34
+ const session = await sessionFactory(c);
35
+ const body = await c.req.json();
36
+ if (!body.prompt || typeof body.prompt !== "string") {
37
+ return c.json({ error: "prompt is required" }, 400);
38
+ }
39
+ return (0, import_streaming.streamSSE)(c, async (stream) => {
40
+ const cleanup = [];
41
+ const subscribe = (event, handler) => {
42
+ session.on(event, handler);
43
+ cleanup.push(() => session.off(event, handler));
44
+ };
45
+ let completed = false;
46
+ const done = new Promise((resolve) => {
47
+ subscribe("text_delta", (delta) => {
48
+ stream.writeSSE({ event: "text_delta", data: JSON.stringify({ delta }) });
49
+ });
50
+ subscribe("tool_start", (state) => {
51
+ stream.writeSSE({ event: "tool_start", data: JSON.stringify(state) });
52
+ });
53
+ subscribe("tool_end", (state) => {
54
+ stream.writeSSE({ event: "tool_end", data: JSON.stringify(state) });
55
+ });
56
+ subscribe("thinking", (isThinking) => {
57
+ stream.writeSSE({ event: "thinking", data: JSON.stringify({ isThinking }) });
58
+ if (!isThinking && completed) {
59
+ resolve();
60
+ }
61
+ });
62
+ subscribe("complete", (result) => {
63
+ completed = true;
64
+ stream.writeSSE({ event: "complete", data: JSON.stringify(result) });
65
+ });
66
+ subscribe("interrupted", (result) => {
67
+ completed = true;
68
+ stream.writeSSE({ event: "interrupted", data: JSON.stringify(result) });
69
+ });
70
+ subscribe("error", (error) => {
71
+ completed = true;
72
+ stream.writeSSE({ event: "error", data: JSON.stringify({ message: error.message }) });
73
+ });
74
+ });
75
+ await session.submit(body.prompt);
76
+ await done;
77
+ for (const fn of cleanup) fn();
78
+ });
79
+ });
80
+ app.post("/command", async (c) => {
81
+ const session = await sessionFactory(c);
82
+ const body = await c.req.json();
83
+ if (!body.name || typeof body.name !== "string") {
84
+ return c.json({ error: "name is required" }, 400);
85
+ }
86
+ const result = await commandExecutor.execute(body.name, session, body.args ?? "");
87
+ if (!result) {
88
+ return c.json({ error: `Unknown command: ${body.name}` }, 404);
89
+ }
90
+ return c.json(result);
91
+ });
92
+ app.post("/abort", async (c) => {
93
+ const session = await sessionFactory(c);
94
+ session.abort();
95
+ return c.json({ ok: true });
96
+ });
97
+ app.post("/cancel-queue", async (c) => {
98
+ const session = await sessionFactory(c);
99
+ session.cancelQueue();
100
+ return c.json({ ok: true });
101
+ });
102
+ app.get("/messages", async (c) => {
103
+ const session = await sessionFactory(c);
104
+ return c.json(session.getMessages());
105
+ });
106
+ app.get("/context", async (c) => {
107
+ const session = await sessionFactory(c);
108
+ return c.json(session.getContextState());
109
+ });
110
+ app.get("/executing", async (c) => {
111
+ const session = await sessionFactory(c);
112
+ return c.json({ executing: session.isExecuting() });
113
+ });
114
+ app.get("/pending", async (c) => {
115
+ const session = await sessionFactory(c);
116
+ return c.json({ pending: session.getPendingPrompt() });
117
+ });
118
+ return app;
119
+ }
120
+ // Annotate the CommonJS export names for ESM import in node:
121
+ 0 && (module.exports = {
122
+ createAgentRoutes
123
+ });
@@ -0,0 +1,31 @@
1
+ import { Context, Hono } from 'hono';
2
+ import { InteractiveSession, SystemCommandExecutor } from '@robota-sdk/agent-sdk';
3
+
4
+ /**
5
+ * HTTP transport adapter — exposes InteractiveSession over REST API.
6
+ *
7
+ * Built on Hono for Cloudflare Workers + Node.js + AWS Lambda compatibility.
8
+ * Each endpoint maps 1:1 to an InteractiveSession or SystemCommandExecutor API.
9
+ */
10
+
11
+ /** Callback that resolves an InteractiveSession from the request context. */
12
+ type ISessionFactory = (c: Context) => InteractiveSession | Promise<InteractiveSession>;
13
+ interface IAgentRoutesOptions {
14
+ /** Resolve an InteractiveSession per request (e.g., by auth token, session ID). */
15
+ sessionFactory: ISessionFactory;
16
+ /** System command executor. */
17
+ commandExecutor: SystemCommandExecutor;
18
+ }
19
+ /**
20
+ * Create a Hono router with all agent HTTP endpoints.
21
+ *
22
+ * Usage:
23
+ * ```typescript
24
+ * const routes = createAgentRoutes({ sessionFactory, commandExecutor });
25
+ * app.route('/agent', routes); // mount on existing app
26
+ * export default routes; // or use standalone (CF Workers)
27
+ * ```
28
+ */
29
+ declare function createAgentRoutes(options: IAgentRoutesOptions): Hono;
30
+
31
+ export { type IAgentRoutesOptions, type ISessionFactory, createAgentRoutes };
@@ -0,0 +1,31 @@
1
+ import { Context, Hono } from 'hono';
2
+ import { InteractiveSession, SystemCommandExecutor } from '@robota-sdk/agent-sdk';
3
+
4
+ /**
5
+ * HTTP transport adapter — exposes InteractiveSession over REST API.
6
+ *
7
+ * Built on Hono for Cloudflare Workers + Node.js + AWS Lambda compatibility.
8
+ * Each endpoint maps 1:1 to an InteractiveSession or SystemCommandExecutor API.
9
+ */
10
+
11
+ /** Callback that resolves an InteractiveSession from the request context. */
12
+ type ISessionFactory = (c: Context) => InteractiveSession | Promise<InteractiveSession>;
13
+ interface IAgentRoutesOptions {
14
+ /** Resolve an InteractiveSession per request (e.g., by auth token, session ID). */
15
+ sessionFactory: ISessionFactory;
16
+ /** System command executor. */
17
+ commandExecutor: SystemCommandExecutor;
18
+ }
19
+ /**
20
+ * Create a Hono router with all agent HTTP endpoints.
21
+ *
22
+ * Usage:
23
+ * ```typescript
24
+ * const routes = createAgentRoutes({ sessionFactory, commandExecutor });
25
+ * app.route('/agent', routes); // mount on existing app
26
+ * export default routes; // or use standalone (CF Workers)
27
+ * ```
28
+ */
29
+ declare function createAgentRoutes(options: IAgentRoutesOptions): Hono;
30
+
31
+ export { type IAgentRoutesOptions, type ISessionFactory, createAgentRoutes };
@@ -0,0 +1,96 @@
1
+ // src/routes.ts
2
+ import { Hono } from "hono";
3
+ import { streamSSE } from "hono/streaming";
4
+ function createAgentRoutes(options) {
5
+ const { sessionFactory, commandExecutor } = options;
6
+ const app = new Hono();
7
+ app.post("/submit", async (c) => {
8
+ const session = await sessionFactory(c);
9
+ const body = await c.req.json();
10
+ if (!body.prompt || typeof body.prompt !== "string") {
11
+ return c.json({ error: "prompt is required" }, 400);
12
+ }
13
+ return streamSSE(c, async (stream) => {
14
+ const cleanup = [];
15
+ const subscribe = (event, handler) => {
16
+ session.on(event, handler);
17
+ cleanup.push(() => session.off(event, handler));
18
+ };
19
+ let completed = false;
20
+ const done = new Promise((resolve) => {
21
+ subscribe("text_delta", (delta) => {
22
+ stream.writeSSE({ event: "text_delta", data: JSON.stringify({ delta }) });
23
+ });
24
+ subscribe("tool_start", (state) => {
25
+ stream.writeSSE({ event: "tool_start", data: JSON.stringify(state) });
26
+ });
27
+ subscribe("tool_end", (state) => {
28
+ stream.writeSSE({ event: "tool_end", data: JSON.stringify(state) });
29
+ });
30
+ subscribe("thinking", (isThinking) => {
31
+ stream.writeSSE({ event: "thinking", data: JSON.stringify({ isThinking }) });
32
+ if (!isThinking && completed) {
33
+ resolve();
34
+ }
35
+ });
36
+ subscribe("complete", (result) => {
37
+ completed = true;
38
+ stream.writeSSE({ event: "complete", data: JSON.stringify(result) });
39
+ });
40
+ subscribe("interrupted", (result) => {
41
+ completed = true;
42
+ stream.writeSSE({ event: "interrupted", data: JSON.stringify(result) });
43
+ });
44
+ subscribe("error", (error) => {
45
+ completed = true;
46
+ stream.writeSSE({ event: "error", data: JSON.stringify({ message: error.message }) });
47
+ });
48
+ });
49
+ await session.submit(body.prompt);
50
+ await done;
51
+ for (const fn of cleanup) fn();
52
+ });
53
+ });
54
+ app.post("/command", async (c) => {
55
+ const session = await sessionFactory(c);
56
+ const body = await c.req.json();
57
+ if (!body.name || typeof body.name !== "string") {
58
+ return c.json({ error: "name is required" }, 400);
59
+ }
60
+ const result = await commandExecutor.execute(body.name, session, body.args ?? "");
61
+ if (!result) {
62
+ return c.json({ error: `Unknown command: ${body.name}` }, 404);
63
+ }
64
+ return c.json(result);
65
+ });
66
+ app.post("/abort", async (c) => {
67
+ const session = await sessionFactory(c);
68
+ session.abort();
69
+ return c.json({ ok: true });
70
+ });
71
+ app.post("/cancel-queue", async (c) => {
72
+ const session = await sessionFactory(c);
73
+ session.cancelQueue();
74
+ return c.json({ ok: true });
75
+ });
76
+ app.get("/messages", async (c) => {
77
+ const session = await sessionFactory(c);
78
+ return c.json(session.getMessages());
79
+ });
80
+ app.get("/context", async (c) => {
81
+ const session = await sessionFactory(c);
82
+ return c.json(session.getContextState());
83
+ });
84
+ app.get("/executing", async (c) => {
85
+ const session = await sessionFactory(c);
86
+ return c.json({ executing: session.isExecuting() });
87
+ });
88
+ app.get("/pending", async (c) => {
89
+ const session = await sessionFactory(c);
90
+ return c.json({ pending: session.getPendingPrompt() });
91
+ });
92
+ return app;
93
+ }
94
+ export {
95
+ createAgentRoutes
96
+ };
package/package.json ADDED
@@ -0,0 +1,45 @@
1
+ {
2
+ "name": "@robota-sdk/agent-transport-http",
3
+ "version": "3.0.0-beta.43",
4
+ "description": "HTTP transport adapter for exposing InteractiveSession over REST API",
5
+ "type": "module",
6
+ "main": "dist/node/index.js",
7
+ "types": "dist/node/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./dist/node/index.d.ts",
11
+ "node": {
12
+ "import": "./dist/node/index.js",
13
+ "require": "./dist/node/index.cjs"
14
+ },
15
+ "default": {
16
+ "import": "./dist/node/index.js",
17
+ "require": "./dist/node/index.cjs"
18
+ }
19
+ }
20
+ },
21
+ "files": [
22
+ "dist"
23
+ ],
24
+ "dependencies": {
25
+ "hono": "^4.7.0",
26
+ "@robota-sdk/agent-sdk": "3.0.0-beta.43"
27
+ },
28
+ "devDependencies": {
29
+ "rimraf": "^5.0.5",
30
+ "tsup": "^8.0.1",
31
+ "typescript": "^5.3.3",
32
+ "vitest": "^1.6.1"
33
+ },
34
+ "license": "MIT",
35
+ "publishConfig": {
36
+ "access": "public"
37
+ },
38
+ "scripts": {
39
+ "build": "tsup src/index.ts --format esm,cjs --dts --out-dir dist/node --clean",
40
+ "test": "vitest run --passWithNoTests",
41
+ "typecheck": "tsc --noEmit",
42
+ "lint": "eslint src/ --ext .ts",
43
+ "clean": "rimraf dist"
44
+ }
45
+ }