Skip to content

An open-source, faster Devin alternative

TypeScript

One idempotency rule: the Idempotency-Key header on create and on turns

Igor Zalutskia9a3bce5c783
diff --git a/docs/agents/api.mdx b/docs/agents/api.mdx
index e7ad9b92..24a3153e 100644
--- a/docs/agents/api.mdx
+++ b/docs/agents/api.mdx
@@ -204,18 +204,25 @@ returned unchanged.
curl -X POST 'https://app.opencomputer.dev/api/managed-agents/sessions/<session-id>/turns' \
-H 'x-api-key: <api-key>' \
-H 'Content-Type: application/json' \
+ -H 'Idempotency-Key: message-42' \
-d '{
"input": "Plan the workshop.",
- "idempotencyKey": "message-42",
"mode": "queue",
"payload": { "workshopId": "ws_812", "audience": "backend" }
}'
```
+`Idempotency-Key` is the one rule on both routes: it identifies this request,
+a repeat returns the existing turn, and a different request under the same
+key is refused (below). Without one every request starts a turn. The body
+field `idempotencyKey` carries the same key for a send that a browser makes
+through your own server, where the header is the server's to set; when both
+are present they must be equal, else `400 invalid_turn`.
+
| Field | Meaning |
| --- | --- |
| `input` | The user text for this turn. Required, not empty. |
-| `idempotencyKey` | Optional. Identifies this request: a repeat returns the existing turn, a different request under the same key is refused (below). Without one every request starts a turn. |
+| `idempotencyKey` | Optional. The key in body form, for a proxied browser send; equal to the header when both are present. |
| `mode` | `queue` (default): run after earlier turns. `steer`: deliver into the running turn when the runtime supports it, otherwise queue. `interrupt`: cancel running turns and run next. |
| `payload` | Optional. A JSON value for the agent, at most 32 KB encoded. It reaches the agent as [`useInput().payload`](/agents/inputs) with `source: "user"`, next to the text, and is recorded on the turn's `message.received` event. The text stays the message; the payload is structured context the agent reads. |
@@ -251,7 +258,7 @@ acknowledged:
`Idempotency-Key: <submission-id>`, the `deploymentId` and `environment`.
A retry then carries the deployment the submission was composed against,
even if the alias has moved since.
-2. `POST /sessions/<id>/turns` with `idempotencyKey: <submission-id>/start`,
+2. `POST /sessions/<id>/turns` with `Idempotency-Key: <submission-id>/start`,
the text and the `payload`.
Keep the submission (its id, the pinned deployment, the text and the
diff --git a/docs/reference/typescript-sdk/managed-agents.mdx b/docs/reference/typescript-sdk/managed-agents.mdx
index 96603c53..18a432a5 100644
--- a/docs/reference/typescript-sdk/managed-agents.mdx
+++ b/docs/reference/typescript-sdk/managed-agents.mdx
@@ -103,7 +103,8 @@ validate.
`create` takes `agentId`, `deploymentId`, `environment`, `memory`, `source`
and `labels` as [documented](/agents/api#create). `turns.send` takes `input`,
-`idempotencyKey`, `mode` and `payload`. Its receipt carries the turn's
+`idempotencyKey`, `mode` and `payload`; both `create` and `turns.send` send
+the key as the `Idempotency-Key` header. The receipt carries the turn's
persisted `status`: `queued` or `running` for a new turn, and for a repeated
key the existing turn's status, `completed`, `failed` or `cancelled` once it
has settled, with `duplicate: true`. The client maps nothing, so a retry
diff --git a/sdks/typescript/src/managed-agents/client.test.ts b/sdks/typescript/src/managed-agents/client.test.ts
index 6994d66a..c6fbd24f 100644
--- a/sdks/typescript/src/managed-agents/client.test.ts
+++ b/sdks/typescript/src/managed-agents/client.test.ts
@@ -99,7 +99,7 @@ describe("OpenComputer client", () => {
expect(api.last().path).toBe("/api/managed-agents/sessions");
});
- it("sends a turn with its key, mode and payload, and reads the receipt from 202 and 200", async () => {
+ it("sends a turn with its key as the Idempotency-Key header, its mode and payload, and reads the receipt from 202 and 200", async () => {
let duplicate = false;
const api = fakeApi({
"POST /api/managed-agents/sessions/ses_1/turns": () =>
@@ -112,15 +112,17 @@ describe("OpenComputer client", () => {
payload: { repo: "acme/web", ref: "main" },
});
expect(receipt).toEqual({ turnId: "turn_1", status: "queued", duplicate: false });
+ // One rule for both routes: the key is the header, never the body.
+ expect(api.last().headers["idempotency-key"]).toBe("task_1/start");
expect(api.last().body).toEqual({
input: "Fix the login page.",
- idempotencyKey: "task_1/start",
payload: { repo: "acme/web", ref: "main" },
});
duplicate = true;
expect(await client.sessions.turns.send("ses_1", { input: "again", mode: "steer" })).toEqual({
turnId: "turn_1", status: "running", duplicate: true,
});
+ expect(api.last().headers["idempotency-key"]).toBeUndefined();
expect(api.last().body).toEqual({ input: "again", mode: "steer" });
});
diff --git a/sdks/typescript/src/managed-agents/client.ts b/sdks/typescript/src/managed-agents/client.ts
index d4736b63..77ba346d 100644
--- a/sdks/typescript/src/managed-agents/client.ts
+++ b/sdks/typescript/src/managed-agents/client.ts
@@ -86,14 +86,19 @@ export interface ListDocumentsOptions extends EnvironmentOptions {
export class Turns {
constructor(private readonly http: Http) {}
- /** `POST /sessions/<id>/turns`: admits a turn; follow it in the event log. */
+ /**
+ * `POST /sessions/<id>/turns`: admits a turn; follow it in the event log.
+ * The key travels as the `Idempotency-Key` header, the one rule for both
+ * create and send; the body field of the same name is the form a browser
+ * send proxied through the application's own server uses.
+ */
async send(sessionId: string, params: SendTurnParams, options: CallOptions = {}): Promise<TurnReceipt> {
const body: Record<string, unknown> = { input: params.input };
- if (params.idempotencyKey !== undefined) body.idempotencyKey = params.idempotencyKey;
if (params.mode !== undefined) body.mode = params.mode;
if (params.payload !== undefined) body.payload = params.payload;
const answer = await this.http.send("POST", `/sessions/${segment(sessionId)}/turns`, shapes.turnReceipt, {
body,
+ headers: params.idempotencyKey !== undefined ? { "idempotency-key": params.idempotencyKey } : undefined,
signal: options.signal,
});
// The receipt says what the platform persisted. A repeated key answers
diff --git a/sdks/typescript/src/managed-agents/types.ts b/sdks/typescript/src/managed-agents/types.ts
index 55415405..06d9fd1c 100644
--- a/sdks/typescript/src/managed-agents/types.ts
+++ b/sdks/typescript/src/managed-agents/types.ts
@@ -148,7 +148,10 @@ export interface TurnReceipt {
export interface SendTurnParams {
/** The user text. Required, not empty. */
input: string;
- /** The same key returns the existing turn; without one every request starts a turn. */
+ /**
+ * Sent as the `Idempotency-Key` header. The same key returns the existing
+ * turn; without one every request starts a turn.
+ */
idempotencyKey?: string;
/** `queue` (default), `steer` or `interrupt`. */
mode?: TurnMode;