@teable/agent-gateway 1.0.0 → 1.0.2

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 CHANGED
@@ -31,6 +31,10 @@ Apps are checked out in `~/apps/<appId>` with `teable-next clone`
31
31
  gateway keeps its own files in `~/.teable-agent-gateway`. Its agent today is
32
32
  grok-build (`grok agent --always-approve stdio`), configured to reach nothing
33
33
  but Teable's AI proxy, with a model token per session that the gateway renews.
34
+ It acts without asking, but when it asks the person a question, or to approve
35
+ its plan, the gateway puts that to them in Teable's builder and gives the
36
+ answer back. A message the person sends while it works goes into what it is
37
+ doing, and its background tasks show in the builder as they run and end.
34
38
 
35
39
  `@teable/agent-gateway/protocol` holds the types of what travels between the
36
40
  gateway, the hub and the builder.
package/dist/agent.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import { type AcpMessage, type JsonRpcId } from "./protocol.js";
2
+ import type { AskShim, NoticeShim } from "./shims.js";
2
3
  interface AgentStart {
3
4
  command: string;
4
5
  args: string[];
@@ -9,6 +10,10 @@ interface AgentStart {
9
10
  /** The process ended, and why. */
10
11
  onExit(message: string): void;
11
12
  log(line: string): void;
13
+ /** The vendor requests with which the agent asks the person, by method (shims.ts). */
14
+ asks?: Readonly<Record<string, AskShim>>;
15
+ /** The vendor notifications that say what the protocol has words for, by method (shims.ts). */
16
+ notices?: Readonly<Record<string, NoticeShim>>;
12
17
  }
13
18
  export declare class AgentProcess {
14
19
  #private;
@@ -16,11 +21,18 @@ export declare class AgentProcess {
16
21
  get running(): boolean;
17
22
  /** The hub's prompts the agent is working on. */
18
23
  get prompts(): JsonRpcId[];
24
+ /** How many of its background tasks run. */
25
+ get tasks(): number;
19
26
  /** A request of the gateway's own; rejects with the agent's error, or when it takes too long. */
20
27
  request(method: string, params: unknown, timeoutMs: number): Promise<unknown>;
21
28
  /** A request whose replayed updates the hub must not see again: `session/load`. */
22
29
  load(params: unknown, timeoutMs: number): Promise<unknown>;
23
- /** Relay the hub's message to the agent, unchanged. */
30
+ /**
31
+ * Send a message into the running turn, as the agent takes one: a second
32
+ * `session/prompt`, under the hub's id.
33
+ */
34
+ steer(request: string, params: unknown): void;
35
+ /** Relay the hub's message to the agent: unchanged, but an answer to what a shim asked, in the agent's words. */
24
36
  send(message: AcpMessage): void;
25
37
  /** Stop reading what the agent says (its writes then wait), and read on. */
26
38
  pause(): void;
package/dist/agents.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import type { ModelKey } from "./platform.js";
2
2
  import type { AgentCapabilities, AgentName } from "./protocol.js";
3
+ import { type AskShim, type NoticeShim } from "./shims.js";
3
4
  /** What a session's agent is prepared with. */
4
5
  interface AgentContext {
5
6
  /** The gateway's own directory: agents keep their state under it. */
@@ -14,6 +15,10 @@ interface AgentDefinition {
14
15
  args: string[];
15
16
  /** Write the agent's configuration for a session; the environment to start it with. */
16
17
  prepare(context: AgentContext): Record<string, string>;
18
+ /** Its vendor requests that ask the person, by method, and the shims that relay them (shims.ts). */
19
+ asks?: Readonly<Record<string, AskShim>>;
20
+ /** Its vendor notifications that say what the protocol has words for, by method (shims.ts). */
21
+ notices?: Readonly<Record<string, NoticeShim>>;
17
22
  }
18
23
  /** Write a file whole or not at all: an agent starting meanwhile reads the old one or the new one. */
19
24
  export declare function writeAtomically(path: string, text: string, mode?: number): void;
@@ -0,0 +1,112 @@
1
+ // src/protocol.ts
2
+ var GATEWAY_PATH = "/api/v4/agent/gateway";
3
+ var AGENT_PROTOCOL = 2;
4
+ var PING = "ping";
5
+ var PONG = "pong";
6
+ var GATEWAY_CLOSE = { replaced: 4e3, expired: 4001, unsupported: 4002 };
7
+ var GATEWAY_TOO_FAST = 4429;
8
+ var MAX_FRAME = 256 * 1024;
9
+ function shrink(message, max) {
10
+ const whole = JSON.stringify(message).length;
11
+ if (whole <= max) return { message, shrunk: 0 };
12
+ const lengths = [];
13
+ const collect = (value) => {
14
+ if (typeof value === "string") lengths.push(value.length);
15
+ else if (Array.isArray(value)) value.forEach(collect);
16
+ else if (typeof value === "object" && value !== null) Object.values(value).forEach(collect);
17
+ };
18
+ collect(message);
19
+ lengths.sort((a, b) => b - a);
20
+ const cut = (value, cap2) => {
21
+ if (typeof value === "string") return value.length > cap2 ? `${value.slice(0, cap2)}\u2026` : value;
22
+ if (Array.isArray(value)) return value.map((each) => cut(each, cap2));
23
+ if (typeof value === "object" && value !== null) {
24
+ return Object.fromEntries(Object.entries(value).map(([key, each]) => [key, cut(each, cap2)]));
25
+ }
26
+ return value;
27
+ };
28
+ let excess = whole - max;
29
+ let cap = lengths[0] ?? 0;
30
+ for (let index = 0; index < lengths.length && excess > 0; index++) {
31
+ const next = lengths[index + 1] ?? 0;
32
+ const saved = (cap - next) * (index + 1);
33
+ if (saved >= excess) {
34
+ cap -= Math.ceil(excess / (index + 1));
35
+ excess = 0;
36
+ } else {
37
+ excess -= saved;
38
+ cap = next;
39
+ }
40
+ }
41
+ for (cap = Math.max(cap - 16, 0); ; cap = Math.floor(cap / 2)) {
42
+ const shrunk = cut(message, cap);
43
+ const length = JSON.stringify(shrunk).length;
44
+ if (length <= max) return { message: shrunk, shrunk: whole - length };
45
+ if (cap === 0) break;
46
+ }
47
+ const { jsonrpc, id, method } = message;
48
+ const named = method === void 0 ? { jsonrpc, id, result: null } : { jsonrpc, ...id === void 0 ? {} : { id }, method, params: null };
49
+ return { message: named, shrunk: whole - JSON.stringify(named).length };
50
+ }
51
+ var TAB_CLOSE = {
52
+ expired: 4e3,
53
+ badInput: 4400,
54
+ unauthenticated: 4401,
55
+ forbidden: 4403,
56
+ notFound: 4404,
57
+ rateLimited: 4429
58
+ };
59
+ var SESSION_ID = /^ses[0-9A-Za-z]{16}$/;
60
+ var ACP = {
61
+ initialize: "initialize",
62
+ newSession: "session/new",
63
+ loadSession: "session/load",
64
+ prompt: "session/prompt",
65
+ cancel: "session/cancel",
66
+ update: "session/update",
67
+ requestPermission: "session/request_permission"
68
+ };
69
+ var ASK = {
70
+ /** Questions for the person (AgentQuestionRequest), answered with an AgentQuestionOutcome. */
71
+ question: "_teable/question",
72
+ /** A plan for the person to approve before the agent acts on it (AgentPlanRequest), answered with an AgentPlanOutcome. */
73
+ plan: "_teable/plan"
74
+ };
75
+ var NOTICE = {
76
+ /** The agent took a message sent while its turn ran (a `steer`) into that turn (SteeredNotice). */
77
+ steered: "_teable/steered",
78
+ /** A background task of the agent's started, or ended (TaskNotice). */
79
+ task: "_teable/task"
80
+ };
81
+ var TASK_OUTPUT = 8e3;
82
+ function isAcpRequest(message) {
83
+ return "method" in message && "id" in message;
84
+ }
85
+ function isAcpNotification(message) {
86
+ return "method" in message && !("id" in message);
87
+ }
88
+ function isAcpResponse(message) {
89
+ return !("method" in message) && "id" in message;
90
+ }
91
+ var AGENT_NAMES = ["grok"];
92
+
93
+ export {
94
+ GATEWAY_PATH,
95
+ AGENT_PROTOCOL,
96
+ PING,
97
+ PONG,
98
+ GATEWAY_CLOSE,
99
+ GATEWAY_TOO_FAST,
100
+ MAX_FRAME,
101
+ shrink,
102
+ TAB_CLOSE,
103
+ SESSION_ID,
104
+ ACP,
105
+ ASK,
106
+ NOTICE,
107
+ TASK_OUTPUT,
108
+ isAcpRequest,
109
+ isAcpNotification,
110
+ isAcpResponse,
111
+ AGENT_NAMES
112
+ };