Skip to content

An open-source, faster Devin alternative

TypeScript

sdk: sessions.questions.dismiss; held-input and delivery.failed events; changelog 2.2.0 and 2.3.0

Igor Zalutski16a0fbb277a6
diff --git a/sdks/typescript/CHANGELOG.md b/sdks/typescript/CHANGELOG.md
index b1346e6e..2780992e 100644
--- a/sdks/typescript/CHANGELOG.md
+++ b/sdks/typescript/CHANGELOG.md
@@ -1,5 +1,41 @@
# Changelog
+## 2.3.0
+
+- `@opencomputer/sdk/agents`: questions. `Session.question` is the question
+ the agent asked with `ask`, or `null` (`SessionQuestion`); a turn that
+ ended by asking completes with `outcome: "question"` (`TurnOutcome`).
+ `turns.send(id, { input, answers: questionId })` answers it; naming a
+ question that is not the open one is `409 question_stale`.
+ `sessions.questions.dismiss(id, questionId)` closes it without an answer.
+- `TurnReceipt` is now a union: `AdmittedTurnReceipt` (`turnId`, `status`,
+ `duplicate`) or `HeldTurnReceipt` (`status: "held" | "discarded"`,
+ `questionId`, `duplicate`, no `turnId`) for input sent while a question is
+ open without answering it. Code that read `receipt.turnId` as a string
+ narrows on it first (`if (receipt.turnId !== undefined)`); a session that
+ never asks returns only admitted receipts. The React hook's matching
+ change is `@opencomputer/react` 0.4.0, where `SendReceipt.turnId` becomes
+ optional and `questionId` is set on a held receipt.
+- A turn queued when a question was asked settles as `cancelled` with
+ `reason: "held"` and `questionId`, on the turn and on its `turn.cancelled`
+ event (`discarded: true` when a stop dropped its input).
+- Events: `question.asked`, `question.answered`, `question.closed` with
+ `reason` `stopped | dismissed | ended | undeliverable`
+ (`QuestionClosedReason`); `message.held`, then `message.delivered` or
+ `message.discarded`, for each held input; `delivery.failed` for a channel
+ activity the platform could not post.
+
+## 2.2.0
+
+- `@opencomputer/sdk/agents`: `sessions.list` takes exact filters
+ (`SessionFilters`: `projectId`, `environment`, `agentId`, `status`, `deploymentId`,
+ `externalReference`, `createdAfter`, `createdBefore`, `updatedAfter`,
+ `labels`) and a `cursor`, and returns `nextCursor` (`null` on the last
+ page); `sessions.iterate` walks every page. `externalReference` on
+ `sessions.create` is your opaque reference to the session, returned on
+ the session, its list row and its `session.*` events, and part of the
+ creation identity.
+
## 2.1.2
- `@opencomputer/sdk/agents`: the transport called `fetch` as a method of the client, which a native fetch refuses with `Illegal invocation` in workerd; it is now called as a plain function. Found by the Development proof of a Worker without Node compatibility. No API change.
diff --git a/sdks/typescript/src/agents/client.test.ts b/sdks/typescript/src/agents/client.test.ts
index fcb28217..6229e55b 100644
--- a/sdks/typescript/src/agents/client.test.ts
+++ b/sdks/typescript/src/agents/client.test.ts
@@ -1,6 +1,7 @@
import { describe, expect, expectTypeOf, it } from "vitest";
import { OpenComputer } from "./client.js";
import { OpenComputerError } from "./errors.js";
+import type { SessionEvent } from "./types.js";
interface Call { method: string; path: string; headers: Record<string, string>; body?: unknown }
@@ -231,6 +232,57 @@ describe("OpenComputer client", () => {
await expect(client.sessions.turns.send("ses_1", { input: "again" })).rejects.toThrow();
});
+ it("dismisses the open question on its route and answers with the session", async () => {
+ const session = { id: "ses_1", agentId: "worker", deploymentId: "dep_1", status: "idle", source: "api", turns: [], question: null, createdAt: "t", updatedAt: "t" };
+ let stale = false;
+ const api = fakeApi({
+ "POST /api/managed-agents/sessions/ses_1/questions/q%201/dismiss": () =>
+ stale
+ ? Response.json({ error: { code: "question_stale", message: "That question is no longer open." } }, { status: 409 })
+ : Response.json(session),
+ });
+ const client = oc(api);
+ const dismissed = await client.sessions.questions.dismiss("ses_1", "q 1");
+ expect(dismissed.question).toBeNull();
+ expect(api.last()).toMatchObject({ method: "POST", path: "/api/managed-agents/sessions/ses_1/questions/q%201/dismiss" });
+ expect(api.last().body).toBeUndefined();
+ stale = true;
+ const failure = await client.sessions.questions.dismiss("ses_1", "q 1").catch((cause: unknown) => cause);
+ expect(failure).toBeInstanceOf(OpenComputerError);
+ expect(failure).toMatchObject({ code: "question_stale", status: 409 });
+ });
+
+ it("reads the held-input events, delivery failures and a held turn's cancellation", async () => {
+ const base = { sessionId: "ses_1", timestamp: "t" };
+ const events = [
+ { ...base, id: "e1", seq: 1, type: "message.held", data: { questionId: "q_1", heldId: "h_1", input: "Keep the old endpoint." } },
+ { ...base, id: "e2", seq: 2, turnId: "turn_2", type: "turn.cancelled", data: { reason: "held", questionId: "q_1", discarded: false } },
+ { ...base, id: "e3", seq: 3, type: "question.closed", data: { questionId: "q_1", reason: "dismissed" } },
+ { ...base, id: "e4", seq: 4, type: "message.delivered", data: { questionId: "q_1", heldId: "h_1", answerTurnId: "turn_3" } },
+ { ...base, id: "e5", seq: 5, type: "message.discarded", data: { questionId: "q_2", heldId: "h_2", reason: "stopped" } },
+ {
+ ...base,
+ id: "e6",
+ seq: 6,
+ type: "delivery.failed",
+ data: { channel: "linear", activityId: "a_1", agentSessionId: "as_1", kind: "terminal", contentType: "response", state: "failed", attempts: 1, error: "400", questionId: "q_1" },
+ },
+ ];
+ const api = fakeApi({ "GET /api/managed-agents/sessions/ses_1/events": () => Response.json({ events }) });
+ const read = await oc(api).sessions.events.list("ses_1");
+ expect(read).toEqual(events);
+ // The catch-all member keeps unknown types readable, so check the named members by type.
+ type Data<T extends string> = Extract<SessionEvent, { type: T; data: { questionId?: unknown } }>["data"];
+ expectTypeOf<Data<"message.held">["input"]>().toEqualTypeOf<string>();
+ expectTypeOf<Data<"message.delivered">["answerTurnId"]>().toEqualTypeOf<string>();
+ expectTypeOf<Data<"message.discarded">["reason"]>().toEqualTypeOf<string>();
+ expectTypeOf<Data<"delivery.failed">["activityId"]>().toEqualTypeOf<string>();
+ expectTypeOf<Data<"turn.cancelled">["questionId"]>().toEqualTypeOf<string | undefined>();
+ expectTypeOf<Data<"turn.cancelled">["discarded"]>().toEqualTypeOf<boolean | undefined>();
+ expectTypeOf<"dismissed">().toMatchTypeOf<Data<"question.closed">["reason"]>();
+ expectTypeOf<"undeliverable">().toMatchTypeOf<Data<"question.closed">["reason"]>();
+ });
+
it("reads the event log from a cursor", async () => {
const events = [{ id: "e1", seq: 1, timestamp: "t", sessionId: "ses_1", type: "session.created", data: { agentId: "worker", deploymentId: "dep_1" } }];
const api = fakeApi({ "GET /api/managed-agents/sessions/ses_1/events": () => Response.json({ events }) });
diff --git a/sdks/typescript/src/agents/client.ts b/sdks/typescript/src/agents/client.ts
index 93e3888d..160f443d 100644
--- a/sdks/typescript/src/agents/client.ts
+++ b/sdks/typescript/src/agents/client.ts
@@ -136,13 +136,35 @@ export class Events {
}
}
+export class Questions {
+ constructor(private readonly http: Http) {}
+
+ /**
+ * `POST /sessions/<id>/questions/<questionId>/dismiss`: closes the open
+ * question without an answer (`question.closed` with reason `dismissed`);
+ * inputs held behind it run as ordinary turns, in order. Repeating it is
+ * harmless; naming a question that is not the open one is `409
+ * question_stale`. Answers with the session.
+ */
+ dismiss(sessionId: string, questionId: string, options: CallOptions = {}): Promise<Session> {
+ return this.http.request(
+ "POST",
+ `/sessions/${segment(sessionId)}/questions/${segment(questionId)}/dismiss`,
+ shapes.session,
+ { signal: options.signal },
+ );
+ }
+}
+
export class Sessions {
readonly turns: Turns;
readonly events: Events;
+ readonly questions: Questions;
constructor(private readonly http: Http) {
this.turns = new Turns(http);
this.events = new Events(http);
+ this.questions = new Questions(http);
}
/** `POST /sessions`: creates a session without a turn. `created` is false when the key had already created it. */
diff --git a/sdks/typescript/src/agents/index.ts b/sdks/typescript/src/agents/index.ts
index 4de8a5c3..a15c0fce 100644
--- a/sdks/typescript/src/agents/index.ts
+++ b/sdks/typescript/src/agents/index.ts
@@ -10,6 +10,7 @@ export {
Sessions,
Turns,
Events,
+ Questions,
Projects,
Memory,
MemoryDocuments,
diff --git a/sdks/typescript/src/agents/types.ts b/sdks/typescript/src/agents/types.ts
index 378b5898..a7b5f018 100644
--- a/sdks/typescript/src/agents/types.ts
+++ b/sdks/typescript/src/agents/types.ts
@@ -64,6 +64,15 @@ export interface SessionQuestion {
askedAt: string;
}
+/**
+ * Why a question closed without an answer: `stopped` (a stop in the channel;
+ * held inputs are discarded), `dismissed` (`sessions.questions.dismiss`; held
+ * inputs run as ordinary turns), `ended` (the session ended; nothing runs),
+ * `undeliverable` (the question could never be shown; held inputs run as
+ * ordinary turns).
+ */
+export type QuestionClosedReason = "stopped" | "dismissed" | "ended" | "undeliverable" | (string & {});
+
/** How an input answered a question; `value` when the text matched an option by value or label. */
export interface QuestionAnswer {
questionId: string;
@@ -376,12 +385,48 @@ export type SessionEvent =
})
| (EventBase & { type: "question.asked"; data: { questionId: string; text: string; options: QuestionOption[] } })
| (EventBase & { type: "question.answered"; data: { questionId: string; answer: QuestionAnswer } })
- | (EventBase & { type: "question.closed"; data: { questionId: string; reason: "stopped" | (string & {}) } })
+ | (EventBase & { type: "question.closed"; data: { questionId: string; reason: QuestionClosedReason } })
+ | (EventBase & {
+ type: "message.held";
+ /** An input sent while `questionId` was open, without answering it. */
+ data: { questionId: string; heldId?: string; input: string; payload?: DataValue };
+ })
+ | (EventBase & {
+ type: "message.delivered";
+ /** The held input reached the agent: as `steering` on the answer turn, or as its own turn. */
+ data: { questionId: string; heldId?: string; answerTurnId: string };
+ })
+ | (EventBase & {
+ type: "message.discarded";
+ /** The held input never reached the agent; `reason` is why (`stopped`, `ended`). */
+ data: { questionId: string; heldId?: string; reason: string };
+ })
+ | (EventBase & {
+ type: "delivery.failed";
+ /** A channel activity the platform could not post; the session proceeded past it. */
+ data: {
+ channel: string;
+ activityId: string;
+ kind: string;
+ contentType: string;
+ state: string;
+ attempts: number;
+ error: string;
+ agentSessionId?: string;
+ questionId?: string;
+ retrying?: boolean;
+ };
+ })
| (EventBase & { type: "turn.failed"; data: Failure })
| (EventBase & {
type: "turn.cancelled";
data: {
- reason: "interrupted" | (string & {});
+ /** `held`: the turn was queued when a question was asked; its input waits on `questionId`. */
+ reason: "interrupted" | "held" | (string & {});
+ /** Set with `reason: "held"`: the question the turn's input waits on. */
+ questionId?: string;
+ /** `true` when the held input was dropped (a stop closed the question) and never reaches the agent. */
+ discarded?: boolean;
replacementTurnId?: string;
/** How long after the interrupt the turn settled, once the commands it had started were stopped. */
settledAfterMs?: number;