Skip to content

An open-source, faster Devin alternative

TypeScript

Work API: the Computer object's work.context op (session briefs, people, the caller's emails)

Igor Zalutski via Computerb8a20f19ead2
diff --git a/services/managed-agents/src/computer/api-http.ts b/services/managed-agents/src/computer/api-http.ts
index 1321885b..a0ebe3b0 100644
--- a/services/managed-agents/src/computer/api-http.ts
+++ b/services/managed-agents/src/computer/api-http.ts
@@ -25,11 +25,12 @@ import type { LogReadRefusal, SessionLogRpc } from "./session-log-contract.js";
import { isSessionId, memberId } from "./session-ids.js";
import type { ApiCaller, ApiOp, ApiReply, WriteMethod } from "./sessions-api.js";
import type { SecretsApiOp } from "./secrets-store.js";
+import type { WorkContextOp } from "./work/briefs.js";
import { gatewayTelemetry } from "./telemetry/gateway.js";
/** What api-http needs of the Computer DO stub. */
export interface ComputerApiStub {
- computerApi(accountId: string, caller: ApiCaller, op: ApiOp | SecretsApiOp): Promise<ApiReply>;
+ computerApi(accountId: string, caller: ApiCaller, op: ApiOp | SecretsApiOp | WorkContextOp): Promise<ApiReply>;
}
const BODY_MAX_BYTES = 512 * 1024;
diff --git a/services/managed-agents/src/computer/durable-object.ts b/services/managed-agents/src/computer/durable-object.ts
index b9626883..f1a49501 100644
--- a/services/managed-agents/src/computer/durable-object.ts
+++ b/services/managed-agents/src/computer/durable-object.ts
@@ -146,6 +146,7 @@ import {
import { type SubmittedView, parseSecretCommand } from "./secrets.js";
import { parseVariableCommand } from "./variables.js";
import { type SecretsApiOp, SecretsStore } from "./secrets-store.js";
+import { type WorkContextOp, workContext } from "./work/briefs.js";
import { AccessStore } from "./access-store.js";
import { AccessFlow } from "./access-flow.js";
import { AccessUi } from "./access-ui.js";
@@ -1625,10 +1626,12 @@ export class ComputerDurableObject extends DurableObject<ComputerEdgeEnv> {
// ------------------------------------------------------------------ session API (work 084)
/** One session API operation (api-http.ts resolved the caller); never throws for a refusal. */
- async computerApi(accountId: string, caller: ApiCaller, op: ApiOp | SecretsApiOp): Promise<ApiReply> {
+ async computerApi(accountId: string, caller: ApiCaller, op: ApiOp | SecretsApiOp | WorkContextOp): Promise<ApiReply> {
this.bind(accountId);
// Organization secrets (work 095) answer whether or not the sessions API is enabled for the organization.
if (op.op.startsWith("secrets.")) return this.secrets.api(caller, op as SecretsApiOp);
+ // The Work API's sessions and people (work tracking 3.10), likewise.
+ if (op.op === "work.context") return workContext({ sql: this.ctx.storage.sql, api: this.api }, caller, op as WorkContextOp);
return this.api.handle(caller, op as ApiOp);
}
diff --git a/services/managed-agents/src/computer/sessions-api.ts b/services/managed-agents/src/computer/sessions-api.ts
index d6b2bd35..1c2534af 100644
--- a/services/managed-agents/src/computer/sessions-api.ts
+++ b/services/managed-agents/src/computer/sessions-api.ts
@@ -273,12 +273,12 @@ export class ComputerSessionsApi {
return [...this.deps.sql.exec<Record<string, SqlStorageValue>>(`${ROW_SELECT} WHERE session_id = ?`, sessionId)][0] as unknown as ApiRow | undefined;
}
- private rowByKey(key: string): ApiRow | undefined {
+ rowByKey(key: string): ApiRow | undefined {
return [...this.deps.sql.exec<Record<string, SqlStorageValue>>(`${ROW_SELECT} WHERE runner_key = ?`, key)][0] as unknown as ApiRow | undefined;
}
/** A session the caller may see: every API session; a Slack one only by its linked owner. */
- private visible(caller: ApiCaller, sessionId: string): ApiRow | undefined {
+ visible(caller: ApiCaller, sessionId: string): ApiRow | undefined {
if (!isSessionId(sessionId)) return undefined;
const row = this.row(sessionId);
if (!row) return undefined;
diff --git a/services/managed-agents/src/computer/work/briefs.ts b/services/managed-agents/src/computer/work/briefs.ts
new file mode 100644
index 00000000..f05ccdb3
--- /dev/null
+++ b/services/managed-agents/src/computer/work/briefs.ts
@@ -0,0 +1,114 @@
+// The Computer object's half of the Work API's composition (work tracking 3.10): for the sessions
+// linked to the items on a page, a brief of each the caller may see; the members behind owner and
+// author emails; and the caller's own emails, for `mine`. The Worker joins this with the Work
+// index's items; neither object calls the other.
+//
+// One call (`work.context`) where the design names two (`briefs`, `people`): the page needs both,
+// and one round trip to the Computer object is cheaper than two.
+
+import type { Session, SessionStatus } from "../api-types.js";
+import type { ApiCaller, ApiReply, ApiRow } from "../sessions-api.js";
+
+/** At most this many linked sessions and emails per call. */
+export const CONTEXT_MAX = 2000;
+
+export interface WorkContextOp {
+ op: "work.context";
+ /** The ids in `Session:` trailers: a session id, or a Slack session's key. */
+ sessionIds: string[];
+ /** Owner and author emails to resolve to members. */
+ emails: string[];
+}
+
+export interface SessionBrief {
+ /** The session's public id. */
+ id: string;
+ title: string | null;
+ ownerId: string | null;
+ ownerName: string | null;
+ status: SessionStatus;
+ /** The oldest open ask's time; null when nothing is asked. */
+ openAskAt: string | null;
+ lastActiveAt: string;
+}
+
+export interface WorkContext {
+ /** By the id the trailer named; sessions the caller may not see are absent. */
+ sessions: Record<string, SessionBrief>;
+ /** By lowercased email: the member it belongs to, when one is known. */
+ people: Record<string, { id: string | null; name: string | null }>;
+ /** The caller's emails, lowercased: the one the edge asserted and the git emails of their sessions. */
+ callerEmails: string[];
+}
+
+/** What `workContext` needs of the Computer object. */
+export interface WorkContextDeps {
+ sql: SqlStorage;
+ api: {
+ row(sessionId: string): ApiRow | undefined;
+ rowByKey(key: string): ApiRow | undefined;
+ visible(caller: ApiCaller, sessionId: string): ApiRow | undefined;
+ compose(row: ApiRow): Session;
+ };
+}
+
+const NO_REPLY = /noreply/i;
+
+export function workContext(deps: WorkContextDeps, caller: ApiCaller, op: WorkContextOp): ApiReply {
+ const sessions: Record<string, SessionBrief> = {};
+ for (const ref of op.sessionIds.slice(0, CONTEXT_MAX)) {
+ const row = deps.api.row(ref) ?? deps.api.rowByKey(ref);
+ if (!row?.sessionId || !deps.api.visible(caller, row.sessionId)) continue;
+ const s = deps.api.compose(row);
+ const open = s.asks.filter((a) => a.status === "open").map((a) => a.createdAt).sort();
+ sessions[ref] = {
+ id: s.id,
+ title: s.title || null,
+ ownerId: row.ownerUserId,
+ ownerName: row.ownerName,
+ status: s.status,
+ openAskAt: open[0] ?? null,
+ lastActiveAt: s.lastActiveAt,
+ };
+ }
+
+ const people: WorkContext["people"] = {};
+ for (const raw of op.emails.slice(0, CONTEXT_MAX)) {
+ const email = raw.toLowerCase();
+ if (people[email]) continue;
+ const owner = deps.sql
+ .exec<{ id: string | null; name: string | null }>(
+ "SELECT owner_user_id AS id, owner_name AS name FROM sessions WHERE lower(owner_email) = ? AND owner_user_id IS NOT NULL ORDER BY updated_at DESC LIMIT 1",
+ email,
+ )
+ .toArray()[0];
+ if (owner) {
+ people[email] = { id: owner.id, name: owner.name };
+ continue;
+ }
+ const person = deps.sql
+ .exec<{ name: string; id: string | null }>(
+ `SELECT p.git_name AS name, (SELECT s.owner_user_id FROM sessions s WHERE s.owner_id = p.slack_user_id AND s.owner_user_id IS NOT NULL LIMIT 1) AS id
+ FROM people p WHERE lower(p.git_email) = ? LIMIT 1`,
+ email,
+ )
+ .toArray()[0];
+ if (person) people[email] = { id: person.id, name: person.name };
+ }
+
+ const mine = new Set<string>();
+ if (caller.email) mine.add(caller.email.toLowerCase());
+ for (const r of deps.sql
+ .exec<{ email: string }>(
+ `SELECT owner_email AS email FROM sessions WHERE owner_user_id = ? AND owner_email IS NOT NULL
+ UNION SELECT git_email AS email FROM people WHERE slack_user_id IN (SELECT owner_id FROM sessions WHERE owner_user_id = ?)`,
+ caller.member,
+ caller.member,
+ )
+ .toArray()) {
+ if (r.email && !NO_REPLY.test(r.email)) mine.add(r.email.toLowerCase());
+ }
+
+ const body: WorkContext = { sessions, people, callerEmails: [...mine] };
+ return { status: 200, body };
+}