@zvada/agent-server 0.3.20 → 0.3.22

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 (77) hide show
  1. package/CHANGELOG.md +82 -0
  2. package/README.md +2 -1
  3. package/dist/types/client/connection.d.ts +2 -0
  4. package/dist/types/client/connection.d.ts.map +1 -1
  5. package/dist/types/client/logins.d.ts +2 -2
  6. package/dist/types/client/logins.d.ts.map +1 -1
  7. package/dist/types/core/agents/claude-code/approvals.d.ts +16 -5
  8. package/dist/types/core/agents/claude-code/approvals.d.ts.map +1 -1
  9. package/dist/types/core/agents/claude-code/options.d.ts +5 -3
  10. package/dist/types/core/agents/claude-code/options.d.ts.map +1 -1
  11. package/dist/types/core/agents/claude-code/session.d.ts.map +1 -1
  12. package/dist/types/core/agents/codex-app-server/client.d.ts +9 -0
  13. package/dist/types/core/agents/codex-app-server/client.d.ts.map +1 -1
  14. package/dist/types/core/agents/codex-app-server/codex-agent.d.ts +3 -1
  15. package/dist/types/core/agents/codex-app-server/codex-agent.d.ts.map +1 -1
  16. package/dist/types/core/agents/codex-app-server/homes.d.ts +7 -5
  17. package/dist/types/core/agents/codex-app-server/homes.d.ts.map +1 -1
  18. package/dist/types/core/agents/codex-app-server/mcp.d.ts +48 -8
  19. package/dist/types/core/agents/codex-app-server/mcp.d.ts.map +1 -1
  20. package/dist/types/core/agents/codex-app-server/options.d.ts +6 -2
  21. package/dist/types/core/agents/codex-app-server/options.d.ts.map +1 -1
  22. package/dist/types/core/agents/codex-app-server/session.d.ts +10 -1
  23. package/dist/types/core/agents/codex-app-server/session.d.ts.map +1 -1
  24. package/dist/types/core/agents/codex-app-server/turn.d.ts +6 -0
  25. package/dist/types/core/agents/codex-app-server/turn.d.ts.map +1 -1
  26. package/dist/types/core/agents/plan.d.ts +2 -13
  27. package/dist/types/core/agents/plan.d.ts.map +1 -1
  28. package/dist/types/core/index.d.ts +1 -1
  29. package/dist/types/core/index.d.ts.map +1 -1
  30. package/dist/types/core/runtime/agent-runtime.d.ts +2 -1
  31. package/dist/types/core/runtime/agent-runtime.d.ts.map +1 -1
  32. package/dist/types/core/runtime/turn.d.ts +2 -0
  33. package/dist/types/core/runtime/turn.d.ts.map +1 -1
  34. package/dist/types/core/utils/errors.d.ts +8 -0
  35. package/dist/types/core/utils/errors.d.ts.map +1 -1
  36. package/dist/types/operator/index.d.ts +11 -0
  37. package/dist/types/operator/index.d.ts.map +1 -0
  38. package/dist/types/operator/mcp.d.ts +33 -0
  39. package/dist/types/operator/mcp.d.ts.map +1 -0
  40. package/dist/types/operator/operator.d.ts +206 -0
  41. package/dist/types/operator/operator.d.ts.map +1 -0
  42. package/dist/types/operator/render.d.ts +87 -0
  43. package/dist/types/operator/render.d.ts.map +1 -0
  44. package/dist/types/operator/view.d.ts +44 -0
  45. package/dist/types/operator/view.d.ts.map +1 -0
  46. package/dist/types/protocol/plan.d.ts +20 -0
  47. package/dist/types/protocol/plan.d.ts.map +1 -0
  48. package/dist/types/server/acp/binding.d.ts.map +1 -1
  49. package/dist/types/sync/worker.d.ts.map +1 -1
  50. package/docs/consuming.md +188 -14
  51. package/docs/harnesses.md +13 -8
  52. package/package.json +6 -1
  53. package/src/client/connection.ts +10 -0
  54. package/src/client/logins.ts +23 -14
  55. package/src/core/agents/claude-code/approvals.ts +33 -6
  56. package/src/core/agents/claude-code/options.ts +5 -3
  57. package/src/core/agents/claude-code/session.ts +4 -2
  58. package/src/core/agents/codex-app-server/client.ts +32 -2
  59. package/src/core/agents/codex-app-server/codex-agent.ts +26 -5
  60. package/src/core/agents/codex-app-server/homes.ts +16 -9
  61. package/src/core/agents/codex-app-server/mcp.ts +122 -21
  62. package/src/core/agents/codex-app-server/options.ts +10 -1
  63. package/src/core/agents/codex-app-server/session.ts +46 -9
  64. package/src/core/agents/codex-app-server/turn.ts +26 -12
  65. package/src/core/agents/plan.ts +3 -19
  66. package/src/core/index.ts +1 -0
  67. package/src/core/runtime/agent-runtime.ts +22 -16
  68. package/src/core/runtime/turn.ts +2 -0
  69. package/src/core/utils/errors.ts +13 -0
  70. package/src/operator/index.ts +40 -0
  71. package/src/operator/mcp.ts +171 -0
  72. package/src/operator/operator.ts +778 -0
  73. package/src/operator/render.ts +435 -0
  74. package/src/operator/view.ts +82 -0
  75. package/src/protocol/plan.ts +35 -0
  76. package/src/server/acp/binding.ts +3 -2
  77. package/src/sync/worker.ts +4 -0
@@ -65,6 +65,19 @@ export class TurnConflictError extends AgentServerError {
65
65
  }
66
66
  }
67
67
 
68
+ /**
69
+ * A turn id the session already ran, whose answer the runtime no longer keeps
70
+ * (a volatile session remembers its last 16 turns): a retry this late cannot
71
+ * converge, and is never run a second time. The wire's `turnConflict`.
72
+ */
73
+ export class TurnForgottenError extends TurnConflictError {
74
+ constructor(sessionId: string, turnId: string) {
75
+ super(sessionId, turnId);
76
+ this.message = `turn ${turnId} on session ${sessionId} already ran, and its answer is no longer kept: send new input as a new turn`;
77
+ this.name = "TurnForgottenError";
78
+ }
79
+ }
80
+
68
81
  /**
69
82
  * A turn's request that `RunRequestSchema` refuses: it never runs. The wire
70
83
  * refuses the same request as `invalidParams` before it reaches the engine.
@@ -0,0 +1,40 @@
1
+ /**
2
+ * `@zvada/agent-server/operator`: the session tools an outside agent operates a
3
+ * product's coding agents with, and their MCP binding. Imports only the
4
+ * protocol and zod; the product is the `OperatorHost` (LLP 0014).
5
+ */
6
+ export { PLAN_OPTIONS, PLAN_TITLE } from "../protocol/plan.ts";
7
+ export {
8
+ type McpHandler,
9
+ type McpServerInfo,
10
+ type ToolSource,
11
+ createMcpHandler,
12
+ } from "./mcp.ts";
13
+ export {
14
+ type Operator,
15
+ type OperatorClock,
16
+ type OperatorHost,
17
+ type OperatorOptions,
18
+ type SendOptions,
19
+ type SendResult,
20
+ type SessionEntry,
21
+ type StartInput,
22
+ type StartResult,
23
+ type Tool,
24
+ type ToolInfo,
25
+ type ViewOptions,
26
+ OperatorError,
27
+ createOperator,
28
+ tool,
29
+ } from "./operator.ts";
30
+ export { type Answer, callHint, oneLine, quote, quoted } from "./render.ts";
31
+ export {
32
+ type DecisionRequest,
33
+ type Phase,
34
+ type QuestionRequest,
35
+ type SessionView,
36
+ type ViewMessage,
37
+ type Waiting,
38
+ phaseOf,
39
+ viewOf,
40
+ } from "./view.ts";
@@ -0,0 +1,171 @@
1
+ /**
2
+ * The operator tools over MCP, with no dependency: one function answers one
3
+ * JSON-RPC message (or a batch) of the Model Context Protocol's tool surface.
4
+ *
5
+ * It serves stateless Streamable HTTP. A product's route parses the POST
6
+ * body, calls the handler with the caller it authenticated, and answers
7
+ * `200 application/json` with the result, or `202` with no body when the
8
+ * result is null (a notification). It answers GET with `405`: the server
9
+ * never opens a stream of its own.
10
+ */
11
+
12
+ // @ref LLP 0014#the-mcp-binding — the engine answers MCP's tool methods; the route, its authentication and its caller are the host's
13
+
14
+ import { OperatorError, type ToolInfo } from "./operator.ts";
15
+ import { quoted } from "./render.ts";
16
+
17
+ /** Anything that lists and runs tools: an operator, or a host's wrapper around one. */
18
+ export interface ToolSource<Caller> {
19
+ tools(caller: Caller): ToolInfo[];
20
+ call(name: string, args: unknown, caller: Caller): Promise<string>;
21
+ }
22
+
23
+ export interface McpServerInfo {
24
+ name: string;
25
+ version: string;
26
+ title?: string;
27
+ /** Words for the client's model about how to use the server. */
28
+ instructions?: string;
29
+ }
30
+
31
+ /**
32
+ * The MCP protocol versions this binding answers, newest first: the list the
33
+ * official SDK (`@modelcontextprotocol/sdk` 1.32, `SUPPORTED_PROTOCOL_VERSIONS`
34
+ * in its `types.js`) accepts. The tool methods used here are the same in each.
35
+ */
36
+ export const MCP_PROTOCOL_VERSIONS = [
37
+ "2025-11-25",
38
+ "2025-06-18",
39
+ "2025-03-26",
40
+ "2024-11-05",
41
+ "2024-10-07",
42
+ ] as const;
43
+
44
+ /** The most messages one batch may hold. */
45
+ const MAX_BATCH = 20;
46
+
47
+ export type McpHandler<Caller> = (message: unknown, caller: Caller) => Promise<unknown | null>;
48
+
49
+ type Id = string | number | null;
50
+ const ok = (id: Id, result: unknown) => ({ jsonrpc: "2.0" as const, id, result });
51
+ const fail = (id: Id, code: number, message: string) => ({
52
+ jsonrpc: "2.0" as const,
53
+ id,
54
+ error: { code, message },
55
+ });
56
+ const isRecord = (value: unknown): value is Record<string, unknown> =>
57
+ typeof value === "object" && value !== null && !Array.isArray(value);
58
+ const text = (value: string, isError?: boolean) => ({
59
+ content: [{ type: "text", text: value }],
60
+ ...(isError ? { isError: true } : {}),
61
+ });
62
+
63
+ /** Answer one MCP JSON-RPC message (or batch array). Returns the response, or null for a notification. */
64
+ export function createMcpHandler<Caller>(
65
+ source: ToolSource<Caller>,
66
+ info: McpServerInfo,
67
+ ): McpHandler<Caller> {
68
+ const serverInfo = {
69
+ name: info.name,
70
+ version: info.version,
71
+ ...(info.title ? { title: info.title } : {}),
72
+ };
73
+
74
+ async function one(message: unknown, caller: Caller): Promise<unknown | null> {
75
+ if (!isRecord(message) || message.jsonrpc !== "2.0") {
76
+ return fail(isRecord(message) ? idOf(message) : null, -32600, "Invalid Request");
77
+ }
78
+ // A response to a request this server never sent, or a notification: nothing to answer.
79
+ if (typeof message.method !== "string") {
80
+ return "id" in message && ("result" in message || "error" in message)
81
+ ? null
82
+ : fail(idOf(message), -32600, "Invalid Request");
83
+ }
84
+ if (!("id" in message)) return null;
85
+ const id = idOf(message);
86
+ const params = isRecord(message.params) ? message.params : {};
87
+ switch (message.method) {
88
+ case "initialize": {
89
+ const asked = params.protocolVersion;
90
+ const protocolVersion =
91
+ MCP_PROTOCOL_VERSIONS.find((v) => v === asked) ?? MCP_PROTOCOL_VERSIONS[0];
92
+ return ok(id, {
93
+ protocolVersion,
94
+ capabilities: { tools: { listChanged: false } },
95
+ serverInfo,
96
+ ...(info.instructions ? { instructions: info.instructions } : {}),
97
+ });
98
+ }
99
+ case "ping":
100
+ return ok(id, {});
101
+ case "tools/list": {
102
+ try {
103
+ const tools = source.tools(caller);
104
+ return ok(id, {
105
+ tools: tools.map((tool) => ({
106
+ name: tool.name,
107
+ title: tool.title,
108
+ description: tool.description,
109
+ inputSchema: tool.inputSchema,
110
+ annotations: {
111
+ title: tool.title,
112
+ readOnlyHint: tool.readOnly,
113
+ destructiveHint: tool.destructive === true,
114
+ openWorldHint: tool.openWorld === true,
115
+ },
116
+ })),
117
+ });
118
+ } catch (error) {
119
+ return fail(id, -32603, messageOf(error));
120
+ }
121
+ }
122
+ case "tools/call": {
123
+ if (typeof params.name !== "string" || !params.name) {
124
+ return fail(id, -32602, "tools/call needs the tool's name");
125
+ }
126
+ try {
127
+ return ok(id, text(await source.call(params.name, params.arguments ?? {}, caller)));
128
+ } catch (error) {
129
+ // The model reads the reason and can act on it; a JSON-RPC error it would not see.
130
+ // A refusal's words are meant for it; any other failure's may be an agent's or a
131
+ // repository's, so it is one quoted line, never a line that reads as a hint.
132
+ return ok(
133
+ id,
134
+ text(
135
+ error instanceof OperatorError
136
+ ? error.message
137
+ : `The tool failed: ${quoted(messageOf(error))}`,
138
+ true,
139
+ ),
140
+ );
141
+ }
142
+ }
143
+ default:
144
+ return fail(id, -32601, `Method not found: ${message.method}`);
145
+ }
146
+ }
147
+
148
+ return async (message, caller) => {
149
+ if (!Array.isArray(message)) return one(message, caller);
150
+ if (!message.length) return fail(null, -32600, "Invalid Request: empty batch");
151
+ if (message.length > MAX_BATCH) {
152
+ return fail(null, -32600, `Invalid Request: a batch holds at most ${MAX_BATCH} messages`);
153
+ }
154
+ // One after another: a batch is never more calls at once than one message.
155
+ const replies: unknown[] = [];
156
+ for (const each of message) {
157
+ const answer = await one(each, caller);
158
+ if (answer !== null) replies.push(answer);
159
+ }
160
+ return replies.length ? replies : null;
161
+ };
162
+ }
163
+
164
+ function idOf(message: Record<string, unknown>): Id {
165
+ const id = message.id;
166
+ return typeof id === "string" || typeof id === "number" ? id : null;
167
+ }
168
+
169
+ function messageOf(error: unknown): string {
170
+ return error instanceof Error ? error.message : String(error);
171
+ }