@midsummer-ai/sdk 0.1.0-staging.5404 → 0.1.0-staging.5411

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,35 @@ 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
+ * Asks a person on the team to approve before the code goes on, and returns their answer: `approved`, who, and their
51
+ * note. While it waits the run holds nothing open, for as long as the version's time limit allows; it is shown in
52
+ * the team's approvals, with a notification, and on the merge request it names. Decide what a no means in your code.
53
+ *
54
+ * const { approved } = await agent.approve({ title: "Email the pricing summary to the team?", details: summary })
55
+ */
56
+ approve(approval: Approval): Promise<Decision>;
57
+ }
58
+ /** What `agent.approve` asks a person. */
59
+ interface Approval {
60
+ /** The question, in a line, such as "Email the pricing summary to the team?": 1 to 200 characters. */
61
+ readonly title: string;
62
+ /** What they need to decide, at most 4,000 characters. */
63
+ readonly details?: string;
64
+ /** The merge request it's about, as `team/repository#number`, where it is asked too. */
65
+ readonly mergeRequest?: string;
66
+ /**
67
+ * Who to ask, by handle, such as "ada": a person on the team who can use its agents. Left out, whoever started the
68
+ * run is asked if they still can, else whoever made the agent, else one of the team's owners.
69
+ */
70
+ readonly from?: string;
71
+ }
72
+ /** A person's answer to `agent.approve`: yes or no, by whom (their handle), their note, and when. */
73
+ interface Decision {
74
+ readonly approved: boolean;
75
+ readonly by: string;
76
+ readonly note?: string;
77
+ readonly at: number;
49
78
  }
50
79
  /** A JSON Schema, such as `{ type: "object", properties: { price: { type: "number" } }, required: ["price"] }`. */
51
80
  type JsonSchema = Readonly<Record<string, unknown>>;
@@ -157,7 +186,9 @@ type AgentCall = {
157
186
  readonly prompt?: string;
158
187
  } & Omit<Question, "prompt">) | ({
159
188
  readonly kind: "loop";
160
- } & Loop);
189
+ } & Loop) | ({
190
+ readonly kind: "approve";
191
+ } & Approval);
161
192
  type RunStatus = "queued" | "running" | "completed" | "failed" | "cancelled";
162
193
  /** What a run is when it begins. */
163
194
  interface RunStart {
@@ -194,7 +225,7 @@ declare class AgentError extends Error {
194
225
  readonly code: AgentErrorCode;
195
226
  constructor(code: AgentErrorCode, message: string);
196
227
  }
197
- /** How a run ended, or, `paused`, that its code waits on loops still working and is run again once they end. */
228
+ /** 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
229
  interface RunEnd {
199
230
  readonly status: RunStatus;
200
231
  readonly output?: unknown;
@@ -212,4 +243,4 @@ declare function httpWire(url: string, token: string, fetcher?: typeof fetch): R
212
243
  /** 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
244
  declare function executeFromEnvironment(agent: Agent, env: Readonly<Record<string, string | undefined>>): Promise<RunEnd>;
214
245
  //#endregion
215
- export { Agent, AgentCall, AgentContext, AgentError, AgentErrorCode, Answer, JsonSchema, Loop, Message, Picture, Question, RunEnd, RunStart, RunStatus, RunWire, Tool, ToolCall, execute, executeFromEnvironment, httpWire };
246
+ 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,8 @@ async function execute(agent, wire, env = {}) {
192
207
  });
193
208
  },
194
209
  ask,
195
- loop
210
+ loop,
211
+ approve
196
212
  };
197
213
  let output;
198
214
  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,7 @@ 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;
34
35
  }): AgentStandIn;
35
36
  //#endregion
36
37
  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,14 @@ function agentStandIn(options = {}) {
120
120
  toolCalls: []
121
121
  } : said, { prompt: call.prompt ?? "" });
122
122
  }
123
+ case "approve": {
124
+ if (!options.approve) throw new AgentError("invalid", "Approvals are not set up in this Stand-in: give it `approve`.");
125
+ const { kind: _kind, ...asked } = call;
126
+ return keep("step", {
127
+ done: true,
128
+ value: await options.approve(asked)
129
+ }, { title: call.title });
130
+ }
123
131
  case "loop": {
124
132
  if (!options.loop) throw new AgentError("invalid", "Loops are not set up in this Stand-in: give it `loop`.");
125
133
  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.5404";
24
+ const SDK_VERSION = "0.1.0-staging.5411";
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.5404",
3
+ "version": "0.1.0-staging.5411",
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.",