Skip to content

An open-source, faster Devin alternative

TypeScript

sdk, react, api-edge, docs: match blue's final question shapes

Igor Zalutski69e00c268b8f
diff --git a/cloudflare-workers/api-edge/src/managed_agents.test.ts b/cloudflare-workers/api-edge/src/managed_agents.test.ts
index 3fffab78..7aa31425 100644
--- a/cloudflare-workers/api-edge/src/managed_agents.test.ts
+++ b/cloudflare-workers/api-edge/src/managed_agents.test.ts
@@ -4225,45 +4225,12 @@ describe("managed agents proxy", () => {
expect(fetchSpy).toHaveBeenCalledTimes(1);
});
- it("words held_inputs_full for the caller", async () => {
- vi.stubGlobal(
- "fetch",
- vi.fn(async () =>
- Response.json(
- { error: { code: "held_inputs_full", message: "held inputs exceed 65536 bytes for q_1" } },
- { status: 409 },
- ),
- ),
- );
- const response = await proxyManagedAgents(
- new Request(
- "https://app.opencomputer.dev/api/managed-agents/sessions/session-1/turns",
- {
- method: "POST",
- headers: { "content-type": "application/json" },
- body: JSON.stringify({ input: "One more constraint" }),
- },
- ),
- env,
- caller,
- "/api/managed-agents",
- );
- expect(response.status).toBe(409);
- expect(await response.json()).toEqual({
- error: {
- code: "held_inputs_full",
- message:
- "Too many messages are waiting on the agent's question. Answer or dismiss the question, then send again.",
- },
- });
- });
-
it("passes a held receipt through with its question and no turn", async () => {
vi.stubGlobal(
"fetch",
vi.fn(async () =>
Response.json(
- { status: "held", questionId: question.id, duplicate: false, accountId: "acct_private" },
+ { status: "held", questionId: question.id, heldId: "held_1", duplicate: false, accountId: "acct_private" },
{ status: 202 },
),
),
@@ -4285,6 +4252,7 @@ describe("managed agents proxy", () => {
expect(await response.json()).toEqual({
status: "held",
questionId: question.id,
+ heldId: "held_1",
duplicate: false,
});
});
diff --git a/cloudflare-workers/api-edge/src/managed_agents.ts b/cloudflare-workers/api-edge/src/managed_agents.ts
index fa39bf32..c1da8fe7 100644
--- a/cloudflare-workers/api-edge/src/managed_agents.ts
+++ b/cloudflare-workers/api-edge/src/managed_agents.ts
@@ -262,9 +262,6 @@ async function publicErrorResponse(
: undefined;
if (slackSetupMessage) {
message = slackSetupMessage;
- } else if (backendCode === "held_inputs_full") {
- message =
- "Too many messages are waiting on the agent's question. Answer or dismiss the question, then send again.";
} else if (linearMessage) {
message = linearMessage;
} else if (missingTemplateManifest) {
@@ -2134,6 +2131,7 @@ function publicSuccessBody(
status: body.status,
duplicate: body.duplicate,
...(typeof body.questionId === "string" ? { questionId: body.questionId } : {}),
+ ...(typeof body.heldId === "string" ? { heldId: body.heldId } : {}),
};
}
if (method === "GET" && suffix === "/billing/sessions") {
diff --git a/docs/agents/api.mdx b/docs/agents/api.mdx
index 7da90628..1338ef58 100644
--- a/docs/agents/api.mdx
+++ b/docs/agents/api.mdx
@@ -344,7 +344,7 @@ another request's key.
Codes: `400 invalid_turn`, `400 invalid_payload` (over 32 KB),
`402 insufficient_credits`, `409 idempotency_conflict`,
-`409 question_stale`, `409 held_inputs_full` (answer or dismiss first),
+`409 question_stale`,
`409 memory_admission_pending` (retry the session creation with its key
first), `409 memory_admission_rejected` (the session is ended; create a new
one).
diff --git a/react/src/index.ts b/react/src/index.ts
index 56670f86..27d8a224 100644
--- a/react/src/index.ts
+++ b/react/src/index.ts
@@ -94,6 +94,8 @@ export interface SendReceipt {
turnId?: string;
/** Set when the input was held: the question it waits on. */
questionId?: string;
+ /** Set when the input was held: its id, the same one its `message.held` event carries. */
+ heldId?: string;
/**
* The turn's persisted status: `queued` behind earlier turns or `running`
* at once for a new turn; for a retried key, whatever the existing turn
@@ -232,8 +234,8 @@ function asSendError(cause: unknown): SendError {
/** The documented admission reply, checked before it becomes a receipt. */
function turnAdmission(
body: unknown,
-): { turnId?: string; questionId?: string; status: TurnStatus; duplicate: boolean } {
- const reply = body as { turnId?: unknown; questionId?: unknown; status?: unknown; duplicate?: unknown } | null;
+): { turnId?: string; questionId?: string; heldId?: string; status: TurnStatus; duplicate: boolean } {
+ const reply = body as { turnId?: unknown; questionId?: unknown; heldId?: unknown; status?: unknown; duplicate?: unknown } | null;
if (!reply || typeof reply.status !== "string" || !reply.status) {
throw new SendError("The admission reply was not a turn receipt.", "invalid_response");
}
@@ -247,7 +249,12 @@ function turnAdmission(
typeof reply.questionId === "string" &&
reply.questionId
) {
- return { questionId: reply.questionId, status: reply.status as TurnStatus, duplicate };
+ return {
+ questionId: reply.questionId,
+ ...(typeof reply.heldId === "string" && reply.heldId ? { heldId: reply.heldId } : {}),
+ status: reply.status as TurnStatus,
+ duplicate,
+ };
}
throw new SendError("The admission reply was not a turn receipt.", "invalid_response");
}
diff --git a/sdks/typescript/src/agents/client.test.ts b/sdks/typescript/src/agents/client.test.ts
index 6229e55b..8504d4d4 100644
--- a/sdks/typescript/src/agents/client.test.ts
+++ b/sdks/typescript/src/agents/client.test.ts
@@ -218,7 +218,7 @@ describe("OpenComputer client", () => {
});
it("returns a held receipt, without a turn, for input sent while a question is open", async () => {
- let body: Record<string, unknown> = { status: "held", questionId: "q_1", duplicate: false };
+ let body: Record<string, unknown> = { status: "held", questionId: "q_1", heldId: "held_1", duplicate: false };
const api = fakeApi({
"POST /api/managed-agents/sessions/ses_1/turns": () => Response.json(body, { status: 202 }),
});
@@ -226,9 +226,10 @@ describe("OpenComputer client", () => {
expect(await client.sessions.turns.send("ses_1", { input: "Keep the old endpoint." })).toEqual({
status: "held",
questionId: "q_1",
+ heldId: "held_1",
duplicate: false,
});
- body = { status: "held", duplicate: false };
+ body = { status: "held", questionId: "q_1", duplicate: false };
await expect(client.sessions.turns.send("ses_1", { input: "again" })).rejects.toThrow();
});
@@ -275,7 +276,8 @@ describe("OpenComputer client", () => {
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<"ended">().toMatchTypeOf<Data<"message.discarded">["reason"]>();
+ expectTypeOf<Data<"message.delivered">["as"]>().toEqualTypeOf<"steering" | "answer" | "turn">();
expectTypeOf<Data<"delivery.failed">["activityId"]>().toEqualTypeOf<string>();
expectTypeOf<Data<"turn.cancelled">["questionId"]>().toEqualTypeOf<string | undefined>();
expectTypeOf<Data<"turn.cancelled">["discarded"]>().toEqualTypeOf<boolean | undefined>();
diff --git a/sdks/typescript/src/agents/client.ts b/sdks/typescript/src/agents/client.ts
index 160f443d..580521de 100644
--- a/sdks/typescript/src/agents/client.ts
+++ b/sdks/typescript/src/agents/client.ts
@@ -115,6 +115,7 @@ export class Turns {
return {
status: answer.body.status as "held" | "discarded",
questionId: answer.body.questionId as string,
+ heldId: answer.body.heldId as string,
duplicate,
};
}
diff --git a/sdks/typescript/src/agents/shapes.ts b/sdks/typescript/src/agents/shapes.ts
index ceea8523..41307210 100644
--- a/sdks/typescript/src/agents/shapes.ts
+++ b/sdks/typescript/src/agents/shapes.ts
@@ -321,12 +321,14 @@ const turnReceiptFields = object({
status: nonEmptyString,
duplicate: optional(boolean),
questionId: optional(string),
+ heldId: optional(string),
});
/** A turn, or an input held behind an open question (`held` / `discarded` with `questionId`, no turn). */
export const turnReceipt: Shape<ReturnType<typeof turnReceiptFields>> = (value, path) => {
const receipt = turnReceiptFields(value, path);
- const held = (receipt.status === "held" || receipt.status === "discarded") && !!receipt.questionId;
+ const held =
+ (receipt.status === "held" || receipt.status === "discarded") && !!receipt.questionId && !!receipt.heldId;
if (receipt.turnId === undefined && !held) {
throw new ShapeError(at(path, "turnId"), "a string, or a held receipt with questionId");
}
diff --git a/sdks/typescript/src/agents/types.ts b/sdks/typescript/src/agents/types.ts
index a7b5f018..4e7a6a6c 100644
--- a/sdks/typescript/src/agents/types.ts
+++ b/sdks/typescript/src/agents/types.ts
@@ -209,6 +209,8 @@ export interface HeldTurnReceipt {
status: "held" | "discarded";
/** The question the input is waiting on. */
questionId: string;
+ /** The held input's id; its `message.held` / `message.delivered` events carry the same one. */
+ heldId: string;
/** `true` when the `idempotencyKey` had already been received. */
duplicate: boolean;
turnId?: undefined;
@@ -389,17 +391,34 @@ export type SessionEvent =
| (EventBase & {
type: "message.held";
/** An input sent while `questionId` was open, without answering it. */
- data: { questionId: string; heldId?: string; input: string; payload?: DataValue };
+ data: {
+ questionId: string;
+ heldId: string;
+ input: string;
+ payload?: DataValue;
+ receivedAt: string;
+ /** When the provider says it was written (Linear's prompt time). */
+ providerTime?: string;
+ /** Set when a turn already queued behind the asking turn was held. */
+ turnId?: string;
+ };
})
| (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 };
+ data: {
+ questionId: string;
+ heldId: string;
+ /** The turn that carried it: the answer turn for steering or the answer, its own turn otherwise. */
+ answerTurnId: string;
+ turnId: string;
+ as: "steering" | "answer" | "turn";
+ };
})
| (EventBase & {
type: "message.discarded";
/** The held input never reached the agent; `reason` is why (`stopped`, `ended`). */
- data: { questionId: string; heldId?: string; reason: string };
+ data: { questionId: string; heldId: string; reason: "stopped" | "ended" | (string & {}) };
})
| (EventBase & {
type: "delivery.failed";
@@ -409,9 +428,19 @@ export type SessionEvent =
activityId: string;
kind: string;
contentType: string;
- state: string;
+ state: "failed_transient" | "failed_permanent" | "invalidated" | "dropped" | (string & {});
attempts: number;
- error: string;
+ /** A fixed code, never the provider's message. */
+ error:
+ | "provider_unavailable"
+ | "provider_rejected"
+ | "connection_not_found"
+ | "connection_pending"
+ | "connection_disconnected"
+ | "connection_revoked"
+ | "session_superseded"
+ | (string & {});
+ turnId?: string;
agentSessionId?: string;
questionId?: string;
retrying?: boolean;