@zvada/agent-server 0.3.26 → 0.3.27

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 (124) hide show
  1. package/CHANGELOG.md +33 -0
  2. package/README.md +1 -1
  3. package/dist/client/client.js +27 -11
  4. package/dist/client/connection.js +35 -3
  5. package/dist/core/agents/claude-code/approvals.js +1 -1
  6. package/dist/core/agents/codex-app-server/plan-approval.js +1 -1
  7. package/dist/core/agents/scripted/scripted-agent.js +30 -3
  8. package/dist/core/presets.js +4 -1
  9. package/dist/core/runtime/agent-runtime.js +15 -5
  10. package/dist/core/runtime/event-processor.js +13 -8
  11. package/dist/core/runtime/origin-line.js +42 -0
  12. package/dist/core/runtime/request-broker.js +2 -1
  13. package/dist/core/runtime/steering.js +4 -3
  14. package/dist/core/runtime/turn.js +6 -4
  15. package/dist/operator/errors.js +97 -0
  16. package/dist/operator/index.js +10 -5
  17. package/dist/operator/mcp.js +7 -12
  18. package/dist/operator/operator.js +1336 -380
  19. package/dist/operator/refs.js +59 -0
  20. package/dist/operator/render.js +171 -74
  21. package/dist/operator/session-tools.js +87 -0
  22. package/dist/operator/types.js +30 -0
  23. package/dist/operator/view.js +82 -27
  24. package/dist/protocol/config.js +12 -5
  25. package/dist/protocol/conversation.js +3 -0
  26. package/dist/protocol/history.js +2 -1
  27. package/dist/protocol/index.js +1 -0
  28. package/dist/protocol/lifecycle.js +19 -0
  29. package/dist/protocol/origin.js +39 -0
  30. package/dist/protocol/reduce.js +2 -0
  31. package/dist/protocol/wire.js +17 -0
  32. package/dist/server/routes.js +15 -5
  33. package/dist/types/client/client.d.ts.map +1 -1
  34. package/dist/types/client/connection.d.ts +14 -2
  35. package/dist/types/client/connection.d.ts.map +1 -1
  36. package/dist/types/client/turns.d.ts +2 -2
  37. package/dist/types/client/turns.d.ts.map +1 -1
  38. package/dist/types/core/agents/base.d.ts +2 -0
  39. package/dist/types/core/agents/base.d.ts.map +1 -1
  40. package/dist/types/core/agents/scripted/scripted-agent.d.ts +14 -1
  41. package/dist/types/core/agents/scripted/scripted-agent.d.ts.map +1 -1
  42. package/dist/types/core/presets.d.ts.map +1 -1
  43. package/dist/types/core/runtime/agent-runtime.d.ts +4 -2
  44. package/dist/types/core/runtime/agent-runtime.d.ts.map +1 -1
  45. package/dist/types/core/runtime/event-processor.d.ts +2 -2
  46. package/dist/types/core/runtime/event-processor.d.ts.map +1 -1
  47. package/dist/types/core/runtime/origin-line.d.ts +15 -0
  48. package/dist/types/core/runtime/origin-line.d.ts.map +1 -0
  49. package/dist/types/core/runtime/request-broker.d.ts +1 -1
  50. package/dist/types/core/runtime/request-broker.d.ts.map +1 -1
  51. package/dist/types/core/runtime/steering.d.ts +7 -4
  52. package/dist/types/core/runtime/steering.d.ts.map +1 -1
  53. package/dist/types/core/runtime/turn.d.ts.map +1 -1
  54. package/dist/types/operator/errors.d.ts +83 -0
  55. package/dist/types/operator/errors.d.ts.map +1 -0
  56. package/dist/types/operator/index.d.ts +10 -5
  57. package/dist/types/operator/index.d.ts.map +1 -1
  58. package/dist/types/operator/mcp.d.ts +1 -1
  59. package/dist/types/operator/mcp.d.ts.map +1 -1
  60. package/dist/types/operator/operator.d.ts +15 -197
  61. package/dist/types/operator/operator.d.ts.map +1 -1
  62. package/dist/types/operator/refs.d.ts +25 -0
  63. package/dist/types/operator/refs.d.ts.map +1 -0
  64. package/dist/types/operator/render.d.ts +59 -19
  65. package/dist/types/operator/render.d.ts.map +1 -1
  66. package/dist/types/operator/session-tools.d.ts +61 -0
  67. package/dist/types/operator/session-tools.d.ts.map +1 -0
  68. package/dist/types/operator/types.d.ts +441 -0
  69. package/dist/types/operator/types.d.ts.map +1 -0
  70. package/dist/types/operator/view.d.ts +19 -29
  71. package/dist/types/operator/view.d.ts.map +1 -1
  72. package/dist/types/protocol/config.d.ts +20 -5
  73. package/dist/types/protocol/config.d.ts.map +1 -1
  74. package/dist/types/protocol/conversation.d.ts +44 -3
  75. package/dist/types/protocol/conversation.d.ts.map +1 -1
  76. package/dist/types/protocol/history.d.ts +1 -1
  77. package/dist/types/protocol/history.d.ts.map +1 -1
  78. package/dist/types/protocol/index.d.ts +1 -0
  79. package/dist/types/protocol/index.d.ts.map +1 -1
  80. package/dist/types/protocol/lifecycle.d.ts +40 -2
  81. package/dist/types/protocol/lifecycle.d.ts.map +1 -1
  82. package/dist/types/protocol/origin.d.ts +49 -0
  83. package/dist/types/protocol/origin.d.ts.map +1 -0
  84. package/dist/types/protocol/reduce.d.ts.map +1 -1
  85. package/dist/types/protocol/wire-api.d.ts +39 -1
  86. package/dist/types/protocol/wire-api.d.ts.map +1 -1
  87. package/dist/types/protocol/wire.d.ts +65 -2
  88. package/dist/types/protocol/wire.d.ts.map +1 -1
  89. package/dist/types/server/routes.d.ts.map +1 -1
  90. package/docs/consuming.md +56 -121
  91. package/docs/sync-history.md +2 -1
  92. package/package.json +1 -1
  93. package/src/client/client.ts +36 -9
  94. package/src/client/connection.ts +31 -1
  95. package/src/client/turns.ts +2 -1
  96. package/src/core/agents/base.ts +2 -0
  97. package/src/core/agents/claude-code/approvals.ts +2 -2
  98. package/src/core/agents/codex-app-server/plan-approval.ts +1 -1
  99. package/src/core/agents/scripted/scripted-agent.ts +42 -3
  100. package/src/core/presets.ts +4 -1
  101. package/src/core/runtime/agent-runtime.ts +31 -7
  102. package/src/core/runtime/event-processor.ts +13 -6
  103. package/src/core/runtime/origin-line.ts +43 -0
  104. package/src/core/runtime/request-broker.ts +2 -0
  105. package/src/core/runtime/steering.ts +8 -4
  106. package/src/core/runtime/turn.ts +11 -4
  107. package/src/operator/errors.ts +218 -0
  108. package/src/operator/index.ts +18 -23
  109. package/src/operator/mcp.ts +7 -16
  110. package/src/operator/operator.ts +1583 -613
  111. package/src/operator/refs.ts +75 -0
  112. package/src/operator/render.ts +208 -98
  113. package/src/operator/session-tools.ts +126 -0
  114. package/src/operator/types.ts +505 -0
  115. package/src/operator/view.ts +93 -42
  116. package/src/protocol/config.ts +14 -6
  117. package/src/protocol/conversation.ts +3 -0
  118. package/src/protocol/history.ts +2 -1
  119. package/src/protocol/index.ts +1 -0
  120. package/src/protocol/lifecycle.ts +21 -0
  121. package/src/protocol/origin.ts +47 -0
  122. package/src/protocol/reduce.ts +4 -0
  123. package/src/protocol/wire.ts +18 -0
  124. package/src/server/routes.ts +16 -5
@@ -0,0 +1,97 @@
1
+ /**
2
+ * The refusals: a closed set of codes (`OPERATOR_ERROR_CODES`), each with its
3
+ * sentence. A refusal reads `refused (<code>): <sentence>`, then the host's
4
+ * words, then the next call, which names only a tool that is installed.
5
+ */
6
+ // @ref LLP 0019#errors — one sentence per code, and a next call that exists
7
+ import { json } from "./render.js";
8
+ /**
9
+ * A host's refusal at its own commit (`start`, `archive`, `send`): the code
10
+ * and the sessions it is about, by id; the package words it as its own
11
+ * refusal of that code, naming the sessions as the caller sees them.
12
+ */
13
+ export class Refused extends Error {
14
+ refusal;
15
+ name = "Refused";
16
+ constructor(refusal) {
17
+ super(`refused (${refusal.code})`);
18
+ this.refusal = refusal;
19
+ }
20
+ }
21
+ const archiveHint = (hint, { sessions, workspaces }) => sessions.length || workspaces.length
22
+ ? hint("archive", {
23
+ ...(sessions.length ? { sessions } : {}),
24
+ ...(workspaces.length ? { workspaces } : {}),
25
+ })
26
+ : undefined;
27
+ /** A refusal a model can act on. Anything else a tool throws is `failed`. */
28
+ export class OperatorError extends Error {
29
+ code;
30
+ extra;
31
+ name = "OperatorError";
32
+ constructor(code, sentence, extra = {}) {
33
+ const done = extra.done ? `${extra.done} ` : "";
34
+ const detail = extra.detail ? ` ${extra.detail}` : "";
35
+ const next = extra.next ? `\n ${extra.next}` : "";
36
+ super(`refused (${code}): ${done}${sentence}${detail}${next}`);
37
+ this.code = code;
38
+ this.extra = extra;
39
+ }
40
+ get retryAfter() {
41
+ return this.extra.retryAfter;
42
+ }
43
+ /** A refusal whose words are already built (a batch whose every item was refused). */
44
+ static said(code, text) {
45
+ const error = new OperatorError(code, "");
46
+ error.message = text;
47
+ return error;
48
+ }
49
+ }
50
+ const NOTHING = "Nothing was done.";
51
+ /** Every value a refusal names: one JSON line, nothing raw (a repository's name, a setup's error). */
52
+ const q = (text) => json(text);
53
+ const list = (refs) => refs.map(q).join(", ") || "(none)";
54
+ /**
55
+ * Every refusal, by code. Each takes what its sentence names and the `hint`
56
+ * of the operator that refuses, so its next call is one the caller has.
57
+ */
58
+ export const refuse = {
59
+ invalidInput: (tool, field, problem) => new OperatorError("invalid_input", `${tool}: ${field}: ${problem}. ${NOTHING}`),
60
+ notFound: (hint, ref, known) => new OperatorError("not_found", `No session, workspace or project ${q(ref)}. Known: ${list(known)}. ${NOTHING}`, { next: hint("list_sessions") }),
61
+ ambiguous: (ref, refs) => new OperatorError("ambiguous", `${q(ref)} names several: ${list(refs)}. Use one of them. ${NOTHING}`),
62
+ archived: (hint, ref, project) => new OperatorError("archived", `${q(ref)} is archived, so nothing was done.`, {
63
+ next: project ? hint("start_session", { project, message: "<the task>" }) : undefined,
64
+ }),
65
+ self: (what) => new OperatorError("self", `That is your own ${what}: a session cannot send to, stop, answer or archive itself. ${NOTHING}`),
66
+ readOnly: () => new OperatorError("read_only", `You are in plan mode, so you may only list, read and wait. ${NOTHING} Put this step in your plan.`),
67
+ access: (detail) => new OperatorError("access", `You may not do this here. ${NOTHING}`, { detail }),
68
+ notYoursArchive: (ref, busy) => new OperatorError("not_yours", `${q(ref)} has sessions you did not start working or waiting (${list(busy)}). Nothing was archived. Ask the person.`),
69
+ notYoursAnswer: (ref) => new OperatorError("not_yours", `${q(ref)} was not started by you or a session you started, so its answer is the person's. Nothing was sent. Tell the person what it waits on.`),
70
+ notYoursPermission: () => new OperatorError("not_yours", "Only a session with full access answers a permission. Nothing was sent. Tell the person."),
71
+ hasChildren: (hint, ref, children, archive) => new OperatorError("has_children", `${q(ref)} started sessions that are not archived (${list(children)}). Nothing was archived. Archive those first.`, { next: archiveHint(hint, archive) }),
72
+ busy: (hint, ref, state, undone) => new OperatorError("busy", `${q(ref)} is ${state}, so nothing was ${undone}.`, {
73
+ next: state === "being set up"
74
+ ? hint("wait_for_sessions", { sessions: [ref] })
75
+ : hint("stop_sessions", { sessions: [ref] }),
76
+ }),
77
+ notWaiting: (hint, ref, status, current) => new OperatorError("not_waiting", `${q(ref)} is not waiting on that request (status: ${status}). Nothing was sent.${current ? ` It waits on request ${q(current)}.` : ""}`, {
78
+ next: current
79
+ ? hint("answer_request", { session: ref, request: current, answer: "<your answer>" })
80
+ : hint("send_message", { session: ref, message: "<your answer>" }),
81
+ }),
82
+ personOnly: (ref, why) => new OperatorError("person_only", `${q(ref)} waits on something only the person can answer, where the session runs (${why}). Nothing was sent. Tell the person.`),
83
+ setupFailed: (hint, ref, reason, done) => new OperatorError("setup_failed", `${q(ref)} could not be set up: ${q(reason)}. Nothing was sent.`, {
84
+ next: hint("archive", { workspaces: [ref] }),
85
+ done,
86
+ }),
87
+ limitDepth: (depth) => new OperatorError("limit_depth", `You were started by a chain of ${depth} sessions, the most allowed, so you cannot start another. Nothing was started. Do the work yourself, or tell the person.`),
88
+ limitSessions: (hint, count, settled) => new OperatorError("limit_sessions", `You have ${count} sessions you started that are not archived (the limit). Nothing was started. Archive the ones whose work is done.`, { next: archiveHint(hint, settled) }),
89
+ limitSettingUp: (retryAfter, detail) => new OperatorError("limit_setting_up", `As many workspaces as this host sets up at once are being set up. Nothing was started. Try again in about ${retryAfter}s.`, { detail, retryAfter }),
90
+ limitRate: (count, retryAfter) => new OperatorError("limit_rate", `You started ${count} sessions in the last minute (the limit). Nothing was started. Try again in ${retryAfter}s.`, { retryAfter }),
91
+ limitResources: (retryAfter, detail) => new OperatorError("limit_resources", `This computer cannot take another session now. Nothing was started.${retryAfter ? ` Try again in ${retryAfter}s.` : ""}`, { detail, retryAfter }),
92
+ staleCursor: (hint, ref) => new OperatorError("stale_cursor", `That cursor no longer points into ${q(ref)}. Nothing was read.`, {
93
+ next: hint("read_session", { session: ref }),
94
+ }),
95
+ unsupported: (detail) => new OperatorError("unsupported", `This cannot be done here. ${NOTHING}`, { detail }),
96
+ failed: () => new OperatorError("failed", "The tool failed; nothing more is known. Try again, or tell the person."),
97
+ };
@@ -1,10 +1,15 @@
1
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).
2
+ * `@zvada/agent-server/operator`: the session tools an outside agent, or a
3
+ * product's own session, operates a product's coding agents with, and their
4
+ * MCP binding. Imports only the protocol and zod; the product is the
5
+ * `OperatorHost` (LLP 0014, LLP 0019).
5
6
  */
6
7
  export { PLAN_OPTIONS, PLAN_TITLE } from "../protocol/plan.js";
8
+ export { OperatorError, Refused, refuse } from "./errors.js";
7
9
  export { createMcpHandler, } from "./mcp.js";
8
- export { OperatorError, createOperator, tool, } from "./operator.js";
10
+ export { DEFAULT_LIMITS, DEFAULT_WAITS, createOperator, tool } from "./operator.js";
11
+ export { refOf } from "./refs.js";
12
+ export { OPERATOR_GUIDANCE, operatorSessionTools, } from "./session-tools.js";
9
13
  export { callHint, oneLine, quote, quoted } from "./render.js";
10
- export { phaseOf, viewOf, } from "./view.js";
14
+ export * from "./types.js";
15
+ export { settledStatus, turnOf, viewOf, waitingOf, } from "./view.js";
@@ -9,8 +9,7 @@
9
9
  * never opens a stream of its own.
10
10
  */
11
11
  // @ref LLP 0014#the-mcp-binding — the engine answers MCP's tool methods; the route, its authentication and its caller are the host's
12
- import { OperatorError } from "./operator.js";
13
- import { quoted } from "./render.js";
12
+ import { OperatorError, refuse } from "./errors.js";
14
13
  /**
15
14
  * The MCP protocol versions this binding answers, newest first: the list the
16
15
  * official SDK (`@modelcontextprotocol/sdk` 1.32, `SUPPORTED_PROTOCOL_VERSIONS`
@@ -88,8 +87,9 @@ export function createMcpHandler(source, info) {
88
87
  })),
89
88
  });
90
89
  }
91
- catch (error) {
92
- return fail(id, -32603, messageOf(error));
90
+ catch {
91
+ // Its text may be anyone's: a fixed sentence instead.
92
+ return fail(id, -32603, "The tool list could not be read.");
93
93
  }
94
94
  }
95
95
  case "tools/call": {
@@ -101,11 +101,9 @@ export function createMcpHandler(source, info) {
101
101
  }
102
102
  catch (error) {
103
103
  // The model reads the reason and can act on it; a JSON-RPC error it would not see.
104
- // A refusal's words are meant for it; any other failure's may be an agent's or a
105
- // repository's, so it is one quoted line, never a line that reads as a hint.
106
- return ok(id, text(error instanceof OperatorError
107
- ? error.message
108
- : `The tool failed: ${quoted(messageOf(error))}`, true));
104
+ return ok(id,
105
+ // Any other failure's text may be an agent's or a repository's: never said.
106
+ text(error instanceof OperatorError ? error.message : refuse.failed().message, true));
109
107
  }
110
108
  }
111
109
  default:
@@ -134,6 +132,3 @@ function idOf(message) {
134
132
  const id = message.id;
135
133
  return typeof id === "string" || typeof id === "number" ? id : null;
136
134
  }
137
- function messageOf(error) {
138
- return error instanceof Error ? error.message : String(error);
139
- }