@midsummer-ai/sdk 0.1.0-staging.5407 → 0.1.0-staging.5414

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.
@@ -46,6 +46,44 @@ interface AgentContext {
46
46
  * as JSON fitting that schema; without, as the engine's words. A loop that couldn't finish throws its reason.
47
47
  */
48
48
  loop(loop: Loop): Promise<unknown>;
49
+ /**
50
+ * Runs another agent of the project, by name, on what it runs in this run's environment, and returns its output.
51
+ * It's a durable call: asked again on resume, it's the same run. While it works, this run waits without holding
52
+ * anything open, as with a loop; `Promise.all` over calls runs them all at once. It spends from this run's budget.
53
+ * An agent that couldn't finish throws its reason.
54
+ *
55
+ * const [a, b] = await Promise.all([agent.call("researcher", { site: "a.example" }), agent.call("researcher", { site: "b.example" })])
56
+ */
57
+ call(name: string, input?: unknown): Promise<unknown>;
58
+ /**
59
+ * Asks a person on the team to approve before the code goes on, and returns their answer: `approved`, who, and their
60
+ * note. While it waits the run holds nothing open, for as long as the version's time limit allows; it is shown in
61
+ * the team's approvals, with a notification, and on the merge request it names. Decide what a no means in your code.
62
+ *
63
+ * const { approved } = await agent.approve({ title: "Email the pricing summary to the team?", details: summary })
64
+ */
65
+ approve(approval: Approval): Promise<Decision>;
66
+ }
67
+ /** What `agent.approve` asks a person. */
68
+ interface Approval {
69
+ /** The question, in a line, such as "Email the pricing summary to the team?": 1 to 200 characters. */
70
+ readonly title: string;
71
+ /** What they need to decide, at most 4,000 characters. */
72
+ readonly details?: string;
73
+ /** The merge request it's about, as `team/repository#number`, where it is asked too. */
74
+ readonly mergeRequest?: string;
75
+ /**
76
+ * Who to ask, by handle, such as "ada": a person on the team who can use its agents. Left out, whoever started the
77
+ * run is asked if they still can, else whoever made the agent, else one of the team's owners.
78
+ */
79
+ readonly from?: string;
80
+ }
81
+ /** A person's answer to `agent.approve`: yes or no, by whom (their handle), their note, and when. */
82
+ interface Decision {
83
+ readonly approved: boolean;
84
+ readonly by: string;
85
+ readonly note?: string;
86
+ readonly at: number;
49
87
  }
50
88
  /** A JSON Schema, such as `{ type: "object", properties: { price: { type: "number" } }, required: ["price"] }`. */
51
89
  type JsonSchema = Readonly<Record<string, unknown>>;
@@ -157,7 +195,13 @@ type AgentCall = {
157
195
  readonly prompt?: string;
158
196
  } & Omit<Question, "prompt">) | ({
159
197
  readonly kind: "loop";
160
- } & Loop);
198
+ } & Loop) | {
199
+ readonly kind: "agent";
200
+ readonly name: string;
201
+ readonly input?: unknown;
202
+ } | ({
203
+ readonly kind: "approve";
204
+ } & Approval);
161
205
  type RunStatus = "queued" | "running" | "completed" | "failed" | "cancelled";
162
206
  /** What a run is when it begins. */
163
207
  interface RunStart {
@@ -194,7 +238,7 @@ declare class AgentError extends Error {
194
238
  readonly code: AgentErrorCode;
195
239
  constructor(code: AgentErrorCode, message: string);
196
240
  }
197
- /** How a run ended, or, `paused`, that its code waits on loops still working and is run again once they end. */
241
+ /** How a run ended, or, `paused`, that its code waits on loops still working or a person's answer, and is run again once one comes. */
198
242
  interface RunEnd {
199
243
  readonly status: RunStatus;
200
244
  readonly output?: unknown;
@@ -212,4 +256,4 @@ declare function httpWire(url: string, token: string, fetcher?: typeof fetch): R
212
256
  /** Runs the run the environment names, as the platform's host does: `MIDSUMMER_AGENT_WIRE` and `MIDSUMMER_AGENT_TOKEN`; the rest is the run's `env`. */
213
257
  declare function executeFromEnvironment(agent: Agent, env: Readonly<Record<string, string | undefined>>): Promise<RunEnd>;
214
258
  //#endregion
215
- export { Agent, AgentCall, AgentContext, AgentError, AgentErrorCode, Answer, JsonSchema, Loop, Message, Picture, Question, RunEnd, RunStart, RunStatus, RunWire, Tool, ToolCall, execute, executeFromEnvironment, httpWire };
259
+ export { Agent, AgentCall, AgentContext, AgentError, AgentErrorCode, Answer, Approval, Decision, JsonSchema, Loop, Message, Picture, Question, RunEnd, RunStart, RunStatus, RunWire, Tool, ToolCall, execute, executeFromEnvironment, httpWire };
package/agents/index.mjs CHANGED
@@ -14,6 +14,8 @@ import { AsyncLocalStorage } from "node:async_hooks";
14
14
  * },
15
15
  * } satisfies Agent
16
16
  *
17
+ * `agent.approve({ title })` waits for a person's yes, holding nothing open, and returns their answer.
18
+ *
17
19
  * `agent.loop({ goal })` lets the model drive: Midsummer's engine works on the goal, step by step, as the agent's team
18
20
  * agent, and the call returns its answer (or, with `output`, that answer as JSON fitting the schema). While a loop
19
21
  * works, the run waits without holding anything open, and it goes on from there once the loop ends; loops started
@@ -143,6 +145,19 @@ async function execute(agent, wire, env = {}) {
143
145
  if (shaped.output === void 0) throw new AgentError("failed", `The loop's answer couldn't be put into its schema: ${shaped.unfit ?? "the model gave none."}`);
144
146
  return shaped.output;
145
147
  };
148
+ /** A person's answer once there is one; until then, a promise that never settles, which pauses the run as a loop does. */
149
+ const approve = async (asked) => {
150
+ const kept = await call(nextId(), {
151
+ kind: "approve",
152
+ ...asked
153
+ });
154
+ if (!kept.done) {
155
+ waiting++;
156
+ idle();
157
+ return new Promise(() => {});
158
+ }
159
+ return kept.value;
160
+ };
146
161
  const context = {
147
162
  runId: start.runId,
148
163
  agent: start.agent,
@@ -192,7 +207,22 @@ async function execute(agent, wire, env = {}) {
192
207
  });
193
208
  },
194
209
  ask,
195
- loop
210
+ loop,
211
+ call: async (name, input) => {
212
+ const kept = await call(nextId(), {
213
+ kind: "agent",
214
+ name,
215
+ ...input === void 0 ? {} : { input }
216
+ });
217
+ if (!kept.done) {
218
+ waiting++;
219
+ idle();
220
+ return new Promise(() => {});
221
+ }
222
+ if (kept.error !== void 0) throw new AgentError("failed", `${name} couldn't finish: ${kept.error}`);
223
+ return kept.value;
224
+ },
225
+ approve
196
226
  };
197
227
  let output;
198
228
  try {
@@ -1,4 +1,4 @@
1
- import { Agent, Answer, Loop, Question, RunEnd, RunStatus, RunWire } from "./index.mjs";
1
+ import { Agent, Answer, Approval, Decision, Loop, Question, RunEnd, RunStatus, RunWire } from "./index.mjs";
2
2
  //#region src/agents/stand-in.d.ts
3
3
  interface StandInEvent {
4
4
  readonly sequence: number;
@@ -31,6 +31,8 @@ declare function agentStandIn(options?: {
31
31
  readonly instructions?: string;
32
32
  readonly ask?: (prompt: string, question: Question) => Promise<string | Answer> | string | Answer;
33
33
  readonly loop?: (loop: Loop) => Promise<string> | string;
34
+ readonly approve?: (approval: Approval) => Promise<Decision> | Decision;
35
+ readonly agents?: Readonly<Record<string, (input: unknown) => Promise<unknown> | unknown>>;
34
36
  }): AgentStandIn;
35
37
  //#endregion
36
38
  export { AgentStandIn, StandInEvent, agentStandIn };
@@ -11,7 +11,7 @@ import { AgentError, execute } from "./index.mjs";
11
11
  * const { end, events } = await local.run(agent, { name: "Ada" })
12
12
  *
13
13
  * Model questions are replied to by `ask` when given, else refused as not set up; spend is not kept. A loop returns at
14
- * once with the words `loop` gives, else is refused the same way.
14
+ * once with the words `loop` gives, and an approval with the answer `approve` gives, else each is refused the same way.
15
15
  */
16
16
  const clone = (value) => value === void 0 ? value : JSON.parse(JSON.stringify(value));
17
17
  function agentStandIn(options = {}) {
@@ -120,6 +120,22 @@ function agentStandIn(options = {}) {
120
120
  toolCalls: []
121
121
  } : said, { prompt: call.prompt ?? "" });
122
122
  }
123
+ case "agent": {
124
+ const other = options.agents?.[call.name];
125
+ if (!other) throw new AgentError("not-found", `There is no agent ${call.name} in this Stand-in: give it under \`agents\`.`);
126
+ return keep("step", {
127
+ done: true,
128
+ value: clone(await other(clone(call.input))) ?? null
129
+ }, { agent: call.name });
130
+ }
131
+ case "approve": {
132
+ if (!options.approve) throw new AgentError("invalid", "Approvals are not set up in this Stand-in: give it `approve`.");
133
+ const { kind: _kind, ...asked } = call;
134
+ return keep("step", {
135
+ done: true,
136
+ value: await options.approve(asked)
137
+ }, { title: call.title });
138
+ }
123
139
  case "loop": {
124
140
  if (!options.loop) throw new AgentError("invalid", "Loops are not set up in this Stand-in: give it `loop`.");
125
141
  const { kind: _kind, ...loop } = call;
package/here-BO-wQDjz.mjs CHANGED
@@ -21,7 +21,7 @@ function processEnv() {
21
21
  * This package's version: `0.0.0-dev` here, and in a published package the version it was published at, which the
22
22
  * release writes in its place as it stages the package (`stampSdk` in `scripts/release/packages.ts`).
23
23
  */
24
- const SDK_VERSION = "0.1.0-staging.5407";
24
+ const SDK_VERSION = "0.1.0-staging.5414";
25
25
  /**
26
26
  * The header that tells Midsummer which client made each of this package's calls: `Midsummer-Client: sdk/<version>
27
27
  * <part>`, `part` the product whose client it is (`database`, `storage`, `queues`, ...), so the platform counts which
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@midsummer-ai/sdk",
3
- "version": "0.1.0-staging.5407",
3
+ "version": "0.1.0-staging.5414",
4
4
  "license": "Apache-2.0",
5
5
  "type": "module",
6
6
  "description": "Midsummer's handles for an app's code: its Database, Storage, Queues, Vector DB, AI, Email and Agents, with stand-ins for tests.",