@sixb/server 0.1.26 → 0.2.0
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.
- package/README.md +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2495 -1845
- package/dist/index.js.map +29 -27
- package/dist/routes/actions.d.ts +1 -1
- package/dist/routes/agent-approvals.d.ts +11 -5
- package/dist/routes/agent-approvals.d.ts.map +1 -1
- package/dist/routes/agent-children.d.ts +2 -2
- package/dist/routes/agent-computers.d.ts +214 -10
- package/dist/routes/agent-computers.d.ts.map +1 -1
- package/dist/routes/agent-runners.d.ts +1 -1
- package/dist/routes/agent-runners.d.ts.map +1 -1
- package/dist/routes/agents.d.ts +230 -24
- package/dist/routes/agents.d.ts.map +1 -1
- package/dist/routes/ai-accounting.d.ts.map +1 -1
- package/dist/routes/auth.d.ts +9 -0
- package/dist/routes/auth.d.ts.map +1 -1
- package/dist/routes/datasets.d.ts +93 -17
- package/dist/routes/datasets.d.ts.map +1 -1
- package/dist/routes/events.d.ts +1 -1
- package/dist/routes/links.d.ts +121 -81
- package/dist/routes/links.d.ts.map +1 -1
- package/dist/routes/logs.d.ts +3 -3
- package/dist/routes/objects.d.ts +149 -58
- package/dist/routes/objects.d.ts.map +1 -1
- package/dist/routes/observed-agent-threads.d.ts +23 -5
- package/dist/routes/observed-agent-threads.d.ts.map +1 -1
- package/dist/routes/pipelines.d.ts +2 -2
- package/dist/routes/projections.d.ts +122 -122
- package/dist/routes/rules.d.ts +2 -2
- package/dist/routes/telemetry.d.ts +0 -50
- package/dist/routes/telemetry.d.ts.map +1 -1
- package/dist/routes/webhook-runs.d.ts +1 -1
- package/dist/routes/workflows.d.ts +21 -10
- package/dist/routes/workflows.d.ts.map +1 -1
- package/dist/routes/ws/agents.d.ts +4 -1
- package/dist/routes/ws/agents.d.ts.map +1 -1
- package/dist/routes/ws/logs.d.ts +10 -10
- package/dist/schemas/actions.d.ts +3 -3
- package/dist/schemas/activity.d.ts +4 -4
- package/dist/schemas/agent-approvals.d.ts +21 -21
- package/dist/schemas/agents.d.ts +760 -107
- package/dist/schemas/agents.d.ts.map +1 -1
- package/dist/schemas/ai-accounting.d.ts +1114 -6
- package/dist/schemas/ai-accounting.d.ts.map +1 -1
- package/dist/schemas/auth.d.ts +47 -0
- package/dist/schemas/auth.d.ts.map +1 -1
- package/dist/schemas/datasets.d.ts +303 -60
- package/dist/schemas/datasets.d.ts.map +1 -1
- package/dist/schemas/event-payloads.d.ts +6 -6
- package/dist/schemas/events.d.ts +8 -8
- package/dist/schemas/lineage.d.ts +1272 -0
- package/dist/schemas/lineage.d.ts.map +1 -0
- package/dist/schemas/links.d.ts +4 -27
- package/dist/schemas/links.d.ts.map +1 -1
- package/dist/schemas/logs.d.ts +22 -22
- package/dist/schemas/objects.d.ts +7 -26
- package/dist/schemas/objects.d.ts.map +1 -1
- package/dist/schemas/pipelines.d.ts +12 -12
- package/dist/schemas/projections.d.ts +733 -733
- package/dist/schemas/rules.d.ts +4 -4
- package/dist/schemas/telemetry.d.ts +0 -13
- package/dist/schemas/telemetry.d.ts.map +1 -1
- package/dist/schemas/webhook-runs.d.ts +2 -2
- package/dist/schemas/workflows.d.ts +133 -44
- package/dist/schemas/workflows.d.ts.map +1 -1
- package/dist/server.d.ts +4 -0
- package/dist/server.d.ts.map +1 -1
- package/dist/utils/request-body.d.ts +5 -0
- package/dist/utils/request-body.d.ts.map +1 -1
- package/dist/utils/request-timing.d.ts +18 -0
- package/dist/utils/request-timing.d.ts.map +1 -0
- package/package.json +7 -7
- package/src/index.ts +1 -0
- package/src/routes/agent-approvals.ts +2 -2
- package/src/routes/agent-computers.ts +309 -87
- package/src/routes/agent-runners.ts +30 -6
- package/src/routes/agents.ts +187 -25
- package/src/routes/ai-accounting.ts +149 -12
- package/src/routes/auth.ts +24 -2
- package/src/routes/datasets.ts +55 -5
- package/src/routes/links.ts +43 -76
- package/src/routes/objects.ts +49 -42
- package/src/routes/observed-agent-threads.ts +2 -2
- package/src/routes/telemetry.ts +1 -42
- package/src/routes/ws/agents.ts +10 -1
- package/src/schemas/agents.ts +121 -6
- package/src/schemas/ai-accounting.ts +78 -15
- package/src/schemas/auth.ts +19 -0
- package/src/schemas/datasets.ts +36 -0
- package/src/schemas/lineage.ts +115 -0
- package/src/schemas/links.ts +3 -11
- package/src/schemas/objects.ts +6 -10
- package/src/schemas/telemetry.ts +0 -6
- package/src/server.ts +18 -2
- package/src/utils/request-body.ts +10 -4
- package/src/utils/request-timing.ts +136 -0
package/dist/server.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"server.d.ts","sourceRoot":"","sources":["../src/server.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAA6B,YAAY,EAAE,MAAM,YAAY,CAAA;AAQzE,OAAO,EAAE,MAAM,EAAc,MAAM,QAAQ,CAAA;AAE3C,OAAO,EACL,KAAK,6BAA6B,EAClC,KAAK,2BAA2B,EAIhC,KAAK,kBAAkB,EACvB,KAAK,0BAA0B,EAC/B,KAAK,4BAA4B,EAKjC,KAAK,oBAAoB,EAC1B,MAAM,uBAAuB,CAAA;AAO9B,OAAO,KAAK,EAAE,yBAAyB,EAAE,MAAM,mBAAmB,CAAA;AAKlE,OAAO,EAGL,KAAK,uBAAuB,EAC7B,MAAM,sBAAsB,CAAA;
|
|
1
|
+
{"version":3,"file":"server.d.ts","sourceRoot":"","sources":["../src/server.ts"],"names":[],"mappings":"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"}
|
|
@@ -19,6 +19,11 @@ export declare class RequestBodyTooLargeError extends Error {
|
|
|
19
19
|
* cap fails with `RequestBodyTooLargeError`, which Elysia surfaces as a parse error.
|
|
20
20
|
*/
|
|
21
21
|
export declare function limitRequestBody(request: Request): Request;
|
|
22
|
+
/**
|
|
23
|
+
* Lifts the server's cap for a request whose handler reads the body itself, within its own limit,
|
|
24
|
+
* once it has authenticated the caller: a runner's run session (see serveAgentRunSession).
|
|
25
|
+
*/
|
|
26
|
+
export declare function liftRequestBodyCap(request: Request): void;
|
|
22
27
|
/**
|
|
23
28
|
* Reads a request body fully into memory, failing once it passes `limitBytes`. That limit
|
|
24
29
|
* replaces the server's default cap for this request, so a route reading more than the default
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"request-body.d.ts","sourceRoot":"","sources":["../../src/utils/request-body.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,+EAA+E;AAC/E,eAAO,MAAM,gCAAgC,QAAc,CAAA;AAE3D,qBAAa,wBAAyB,SAAQ,KAAK;IAI/C,QAAQ,CAAC,UAAU,EAAE,MAAM;IAH7B,QAAQ,CAAC,IAAI,8BAA6B;gBAG/B,UAAU,EAAE,MAAM,EAC3B,OAAO,SAAuD;CAIjE;AAID;;;GAGG;AACH,wBAAgB,gBAAgB,CAAC,OAAO,EAAE,OAAO,GAAG,OAAO,CAwB1D;AAED;;;;;GAKG;AACH,wBAAsB,wBAAwB,CAC5C,OAAO,EAAE,OAAO,EAChB,UAAU,EAAE,MAAM,EAClB,eAAe,CAAC,EAAE,MAAM,GACvB,OAAO,CAAC,UAAU,CAAC,WAAW,CAAC,CAAC,
|
|
1
|
+
{"version":3,"file":"request-body.d.ts","sourceRoot":"","sources":["../../src/utils/request-body.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,+EAA+E;AAC/E,eAAO,MAAM,gCAAgC,QAAc,CAAA;AAE3D,qBAAa,wBAAyB,SAAQ,KAAK;IAI/C,QAAQ,CAAC,UAAU,EAAE,MAAM;IAH7B,QAAQ,CAAC,IAAI,8BAA6B;gBAG/B,UAAU,EAAE,MAAM,EAC3B,OAAO,SAAuD;CAIjE;AAID;;;GAGG;AACH,wBAAgB,gBAAgB,CAAC,OAAO,EAAE,OAAO,GAAG,OAAO,CAwB1D;AAED;;;GAGG;AACH,wBAAgB,kBAAkB,CAAC,OAAO,EAAE,OAAO,GAAG,IAAI,CAGzD;AAED;;;;;GAKG;AACH,wBAAsB,wBAAwB,CAC5C,OAAO,EAAE,OAAO,EAChB,UAAU,EAAE,MAAM,EAClB,eAAe,CAAC,EAAE,MAAM,GACvB,OAAO,CAAC,UAAU,CAAC,WAAW,CAAC,CAAC,CAyClC"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export interface SixbRequestTimingOptions {
|
|
2
|
+
/**
|
|
3
|
+
* Send a `Server-Timing` header on responses to signed-in requests, which browsers show under
|
|
4
|
+
* a request's Timing tab. Off by default, since timings reveal how the server spends its time.
|
|
5
|
+
*/
|
|
6
|
+
readonly header?: boolean;
|
|
7
|
+
/** Log requests that take at least this long. Defaults to 1000; 0 turns the log off. */
|
|
8
|
+
readonly slowRequestMillis?: number;
|
|
9
|
+
}
|
|
10
|
+
/** Read `SIXB_SERVER_TIMING` and `SIXB_SLOW_REQUEST_MS`. */
|
|
11
|
+
export declare function requestTimingOptionsFromEnv(env?: Readonly<Record<string, string | undefined>>): SixbRequestTimingOptions;
|
|
12
|
+
/** The request has been authenticated and is about to run its route. */
|
|
13
|
+
export declare function markRequestAuthorized(route: string, showTimings: boolean): void;
|
|
14
|
+
/** Name what a shared route is working on, so a slow request can be told from its neighbors. */
|
|
15
|
+
export declare function noteRequestSubject(subject: string | undefined): void;
|
|
16
|
+
/** Run one request, then say where its time went. Time after the response starts is not counted. */
|
|
17
|
+
export declare function timeRequest<TResult>(request: Request, options: SixbRequestTimingOptions, handle: () => TResult | Promise<TResult>): Promise<TResult | Response>;
|
|
18
|
+
//# sourceMappingURL=request-timing.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"request-timing.d.ts","sourceRoot":"","sources":["../../src/utils/request-timing.ts"],"names":[],"mappings":"AAKA,MAAM,WAAW,wBAAwB;IACvC;;;OAGG;IACH,QAAQ,CAAC,MAAM,CAAC,EAAE,OAAO,CAAA;IACzB,wFAAwF;IACxF,QAAQ,CAAC,iBAAiB,CAAC,EAAE,MAAM,CAAA;CACpC;AAED,4DAA4D;AAC5D,wBAAgB,2BAA2B,CACzC,GAAG,GAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,CAAC,CAAe,GAC9D,wBAAwB,CAM1B;AAiBD,wEAAwE;AACxE,wBAAgB,qBAAqB,CAAC,KAAK,EAAE,MAAM,EAAE,WAAW,EAAE,OAAO,GAAG,IAAI,CAU/E;AAED,gGAAgG;AAChG,wBAAgB,kBAAkB,CAAC,OAAO,EAAE,MAAM,GAAG,SAAS,GAAG,IAAI,CAGpE;AAiDD,oGAAoG;AACpG,wBAAsB,WAAW,CAAC,OAAO,EACvC,OAAO,EAAE,OAAO,EAChB,OAAO,EAAE,wBAAwB,EACjC,MAAM,EAAE,MAAM,OAAO,GAAG,OAAO,CAAC,OAAO,CAAC,GACvC,OAAO,CAAC,OAAO,GAAG,QAAQ,CAAC,CAuB7B"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sixb/server",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "HTTP and WebSocket API server for Sixb",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"sixb",
|
|
@@ -38,14 +38,14 @@
|
|
|
38
38
|
"zod-to-json-schema": "^3.24.0"
|
|
39
39
|
},
|
|
40
40
|
"peerDependencies": {
|
|
41
|
-
"@sixb/core": "0.
|
|
41
|
+
"@sixb/core": "0.2.0"
|
|
42
42
|
},
|
|
43
43
|
"devDependencies": {
|
|
44
|
-
"@sixb/auth-magic-link": "0.
|
|
45
|
-
"@sixb/auth-oidc": "0.
|
|
46
|
-
"@sixb/cli-core": "0.
|
|
47
|
-
"@sixb/core": "0.
|
|
48
|
-
"@sixb/sqlite": "0.
|
|
44
|
+
"@sixb/auth-magic-link": "0.2.0",
|
|
45
|
+
"@sixb/auth-oidc": "0.2.0",
|
|
46
|
+
"@sixb/cli-core": "0.2.0",
|
|
47
|
+
"@sixb/core": "0.2.0",
|
|
48
|
+
"@sixb/sqlite": "0.2.0",
|
|
49
49
|
"@types/bun": "latest",
|
|
50
50
|
"typescript": "^5.7.0"
|
|
51
51
|
},
|
package/src/index.ts
CHANGED
|
@@ -2,3 +2,4 @@ export type { SixbApiBrowserPolicy } from "./auth/browser-origin"
|
|
|
2
2
|
export type { SixbAuthExperienceOptions } from "./auth/experience"
|
|
3
3
|
export type { SixbSharedAccessOptions } from "./auth/shared-access"
|
|
4
4
|
export { createSixbServer, SixbServer, type SixbServerOptions } from "./server"
|
|
5
|
+
export { requestTimingOptionsFromEnv, type SixbRequestTimingOptions } from "./utils/request-timing"
|
|
@@ -2,7 +2,7 @@ import type { AgentApprovalRecord, SixbHostView } from "@sixb/core"
|
|
|
2
2
|
import { agentApprovalCallers } from "@sixb/core/internal/agents"
|
|
3
3
|
import type { AgentStorage } from "@sixb/core/storage"
|
|
4
4
|
import type { Elysia } from "elysia"
|
|
5
|
-
import { requireRequestSixb } from "../auth/scope"
|
|
5
|
+
import { requestActor, requireRequestSixb } from "../auth/scope"
|
|
6
6
|
import { SIXB_SESSION_MUTATION_SECURITY_REQUIREMENT } from "../openapi/security"
|
|
7
7
|
import { OPENAPI_TAGS } from "../openapi/tags"
|
|
8
8
|
import {
|
|
@@ -146,7 +146,7 @@ export function registerAgentApprovalRoutes(app: Elysia, host: SixbHostView) {
|
|
|
146
146
|
return { error: "Agent thread not found" }
|
|
147
147
|
}
|
|
148
148
|
|
|
149
|
-
return { ...(await serializeThread(host, thread)), ...approvalMode }
|
|
149
|
+
return { ...(await serializeThread(host, thread, requestActor(sixb))), ...approvalMode }
|
|
150
150
|
} catch (error) {
|
|
151
151
|
return handleAgentRouteError(error, set)
|
|
152
152
|
}
|
|
@@ -1,126 +1,348 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import type { SixbHostView } from "@sixb/core"
|
|
2
|
+
import { sameComputerPlace } from "@sixb/core/internal/agent-run-storage-provider"
|
|
2
3
|
import type { AgentRunSessionHost } from "@sixb/core/internal/agents"
|
|
3
|
-
import
|
|
4
|
+
import { publishAgentComputerActivity } from "@sixb/core/internal/agents/streams"
|
|
5
|
+
import type {
|
|
6
|
+
AgentComputerSessionRecord,
|
|
7
|
+
AgentStorage,
|
|
8
|
+
AgentThreadComputer,
|
|
9
|
+
ConversationAgentRunRecord,
|
|
10
|
+
Principal,
|
|
11
|
+
} from "@sixb/core/storage"
|
|
4
12
|
import type { Elysia } from "elysia"
|
|
13
|
+
import { z } from "zod"
|
|
5
14
|
import { requestActor, requireRequestSixb } from "../auth/scope"
|
|
6
15
|
import { SIXB_SESSION_MUTATION_SECURITY_REQUIREMENT } from "../openapi/security"
|
|
7
16
|
import { OPENAPI_TAGS } from "../openapi/tags"
|
|
8
17
|
import {
|
|
18
|
+
AgentComputerSessionParamsSchema,
|
|
9
19
|
AgentRequestErrorResponseSchema,
|
|
20
|
+
AgentThreadComputerInputSchema,
|
|
10
21
|
AgentThreadParamsSchema,
|
|
11
22
|
AgentThreadSchema,
|
|
23
|
+
HoldAgentComputerSessionBodySchema,
|
|
24
|
+
HoldAgentComputerSessionResponseSchema,
|
|
12
25
|
MoveAgentThreadToComputerBodySchema,
|
|
13
26
|
} from "../schemas/agents"
|
|
14
27
|
import { ErrorResponseSchema } from "../schemas/common"
|
|
15
28
|
import { unconfiguredStorageResponse } from "../utils/http"
|
|
16
|
-
import { handleAgentRouteError,
|
|
29
|
+
import { handleAgentRouteError, principalKey, serializeThreadRead } from "./agents"
|
|
17
30
|
|
|
18
|
-
// A computer's thread runs on one of its owner's computers
|
|
19
|
-
//
|
|
20
|
-
//
|
|
31
|
+
// A computer's thread runs on one of its owner's computers, in one folder there: only `sixb` open
|
|
32
|
+
// in that folder claims its turns. Each open `sixb` holds a session, a lease it renews, so the
|
|
33
|
+
// thread reads as online while one is live, and its owner can send from anywhere. Moving it to
|
|
34
|
+
// another folder, or to the cloud, is its owner's call.
|
|
35
|
+
|
|
36
|
+
/** How long a session stays live without a renewal; `sixb` renews it three times as often. */
|
|
37
|
+
const SESSION_MS = 60_000
|
|
38
|
+
/** How many sessions one person holds at once: one per open `sixb`, a terminal each. */
|
|
39
|
+
const MAX_SESSIONS = 20
|
|
40
|
+
|
|
41
|
+
type ComputerClaim =
|
|
42
|
+
| { readonly computer: AgentThreadComputer }
|
|
43
|
+
| {
|
|
44
|
+
readonly refused: {
|
|
45
|
+
readonly status: 400 | 403
|
|
46
|
+
readonly error: Record<string, string>
|
|
47
|
+
}
|
|
48
|
+
}
|
|
21
49
|
|
|
22
50
|
/**
|
|
23
|
-
*
|
|
24
|
-
* runs
|
|
51
|
+
* The computer and folder claiming a computer's turn, or why the turn is not theirs: its thread
|
|
52
|
+
* runs on the claiming computer, in the folder it names. It reads as of now, for a refusal that
|
|
53
|
+
* says why; the run's start is what decides, should the thread move right after.
|
|
25
54
|
*/
|
|
26
|
-
export async function
|
|
55
|
+
export async function computerClaiming(
|
|
27
56
|
sessions: AgentRunSessionHost,
|
|
28
57
|
run: ConversationAgentRunRecord,
|
|
29
58
|
body: unknown
|
|
30
|
-
): Promise<
|
|
31
|
-
const
|
|
32
|
-
|
|
59
|
+
): Promise<ComputerClaim> {
|
|
60
|
+
const claimed = AgentThreadComputerInputSchema.safeParse(
|
|
61
|
+
(body as { readonly computer?: unknown } | null)?.computer
|
|
62
|
+
)
|
|
63
|
+
if (!claimed.success) {
|
|
33
64
|
return {
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
65
|
+
refused: {
|
|
66
|
+
status: 400,
|
|
67
|
+
error: {
|
|
68
|
+
kind: "Error",
|
|
69
|
+
message:
|
|
70
|
+
"[SixbServer] A claim of a computer's turn names the computer and folder it comes from.",
|
|
71
|
+
},
|
|
38
72
|
},
|
|
39
73
|
}
|
|
40
74
|
}
|
|
41
75
|
|
|
42
|
-
const
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
projectId: run.projectId,
|
|
49
|
-
id: run.threadId,
|
|
50
|
-
computerId,
|
|
51
|
-
ifUnassigned: true,
|
|
52
|
-
})
|
|
53
|
-
).computerId
|
|
54
|
-
if (assigned === computerId) return null
|
|
55
|
-
|
|
76
|
+
const thread = await sessions.storage.agents.threads.getById({
|
|
77
|
+
projectId: run.projectId,
|
|
78
|
+
id: run.threadId,
|
|
79
|
+
})
|
|
80
|
+
const place = thread?.computer
|
|
81
|
+
if (place && sameComputerPlace(place, claimed.data)) return { computer: claimed.data }
|
|
56
82
|
return {
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
83
|
+
refused: {
|
|
84
|
+
status: 403,
|
|
85
|
+
error: {
|
|
86
|
+
kind: "AgentRunRefusedError",
|
|
87
|
+
message: !place
|
|
88
|
+
? `[SixbServer] Agent thread '${run.threadId}' has no folder yet: move it to this one to run its turns here.`
|
|
89
|
+
: place.id === claimed.data.id
|
|
90
|
+
? `[SixbServer] Agent thread '${run.threadId}' works in ${place.folder}: open sixb there, or move it to this folder.`
|
|
91
|
+
: `[SixbServer] Agent thread '${run.threadId}' runs on ${place.name}: move it to this computer to run its turns here.`,
|
|
92
|
+
},
|
|
61
93
|
},
|
|
62
94
|
}
|
|
63
95
|
}
|
|
64
96
|
|
|
97
|
+
/** End the sessions no `sixb` renewed, and say so for each folder left with none. */
|
|
98
|
+
export async function endLapsedComputerSessions(host: SixbHostView): Promise<void> {
|
|
99
|
+
const agents = host.storage.agents
|
|
100
|
+
if (!agents) return
|
|
101
|
+
const lapsed = await agents.computers.removeLapsed({ projectId: host.id, now: new Date() })
|
|
102
|
+
await announceOffline(host, agents, lapsed)
|
|
103
|
+
}
|
|
104
|
+
|
|
65
105
|
export function registerAgentComputerRoutes(app: Elysia, host: SixbHostView) {
|
|
66
|
-
return app
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
106
|
+
return app
|
|
107
|
+
.post(
|
|
108
|
+
"/api/agent-threads/:threadId/computer",
|
|
109
|
+
async (context) => {
|
|
110
|
+
const { params, body, set } = context
|
|
111
|
+
const sixb = requireRequestSixb(context)
|
|
112
|
+
try {
|
|
113
|
+
if (!host.storage.agents) return unconfiguredStorageResponse(set, "Agent storage")
|
|
74
114
|
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
`[SixbServer] Agent thread '${params.threadId}' not found.`
|
|
81
|
-
)
|
|
82
|
-
}
|
|
83
|
-
if (!principalsEqual(thread.ownerPrincipal, requestActor(sixb))) {
|
|
84
|
-
throw new AgentRequestError(
|
|
85
|
-
"not_thread_owner",
|
|
86
|
-
`[SixbServer] Only the owner of Agent thread '${params.threadId}' moves it to a computer.`
|
|
87
|
-
)
|
|
88
|
-
}
|
|
89
|
-
if (thread.runsOn !== "computer") {
|
|
90
|
-
throw new AgentRequestError(
|
|
91
|
-
"invalid_message",
|
|
92
|
-
`[SixbServer] Agent thread '${params.threadId}' runs in the cloud, on no computer.`
|
|
93
|
-
)
|
|
115
|
+
const { computer } = MoveAgentThreadToComputerBodySchema.parse(body)
|
|
116
|
+
const moved = await sixb.agent.threads.moveToComputer(params.threadId, computer)
|
|
117
|
+
return await serializeThreadRead(host, sixb, moved)
|
|
118
|
+
} catch (error) {
|
|
119
|
+
return handleAgentRouteError(error, set)
|
|
94
120
|
}
|
|
121
|
+
},
|
|
122
|
+
{
|
|
123
|
+
params: AgentThreadParamsSchema,
|
|
124
|
+
body: MoveAgentThreadToComputerBodySchema,
|
|
125
|
+
response: {
|
|
126
|
+
200: AgentThreadSchema,
|
|
127
|
+
400: AgentRequestErrorResponseSchema,
|
|
128
|
+
403: AgentRequestErrorResponseSchema,
|
|
129
|
+
404: AgentRequestErrorResponseSchema,
|
|
130
|
+
409: AgentRequestErrorResponseSchema,
|
|
131
|
+
501: ErrorResponseSchema,
|
|
132
|
+
},
|
|
133
|
+
detail: {
|
|
134
|
+
summary: "Move a thread to one of your computers, in a folder there",
|
|
135
|
+
description:
|
|
136
|
+
"A thread that runs on its owner's computer runs there alone, in one folder: only `sixb` open in that folder claims its turns. Its owner moves it to another folder or computer. Refused while a turn runs or waits on approvals.",
|
|
137
|
+
tags: [OPENAPI_TAGS.agentThreads.name],
|
|
138
|
+
operationId: "moveAgentThreadToComputer",
|
|
139
|
+
security: SIXB_SESSION_MUTATION_SECURITY_REQUIREMENT,
|
|
140
|
+
},
|
|
141
|
+
}
|
|
142
|
+
)
|
|
143
|
+
.delete(
|
|
144
|
+
"/api/agent-threads/:threadId/computer",
|
|
145
|
+
async (context) => {
|
|
146
|
+
const { params, set } = context
|
|
147
|
+
const sixb = requireRequestSixb(context)
|
|
148
|
+
try {
|
|
149
|
+
if (!host.storage.agents) return unconfiguredStorageResponse(set, "Agent storage")
|
|
95
150
|
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
151
|
+
const moved = await sixb.agent.threads.moveToCloud(params.threadId)
|
|
152
|
+
return await serializeThreadRead(host, sixb, moved)
|
|
153
|
+
} catch (error) {
|
|
154
|
+
return handleAgentRouteError(error, set)
|
|
155
|
+
}
|
|
156
|
+
},
|
|
157
|
+
{
|
|
158
|
+
params: AgentThreadParamsSchema,
|
|
159
|
+
response: {
|
|
160
|
+
200: AgentThreadSchema,
|
|
161
|
+
400: AgentRequestErrorResponseSchema,
|
|
162
|
+
403: AgentRequestErrorResponseSchema,
|
|
163
|
+
404: AgentRequestErrorResponseSchema,
|
|
164
|
+
409: AgentRequestErrorResponseSchema,
|
|
165
|
+
501: ErrorResponseSchema,
|
|
166
|
+
},
|
|
167
|
+
detail: {
|
|
168
|
+
summary: "Move a thread from your computer to the cloud",
|
|
169
|
+
description:
|
|
170
|
+
"Its turns run in the cloud from now on, starting with the one waiting for the computer, if any. The files it worked on stay on the computer. Refused while a turn runs there or waits on approvals.",
|
|
171
|
+
tags: [OPENAPI_TAGS.agentThreads.name],
|
|
172
|
+
operationId: "moveAgentThreadToCloud",
|
|
173
|
+
security: SIXB_SESSION_MUTATION_SECURITY_REQUIREMENT,
|
|
174
|
+
},
|
|
104
175
|
}
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
176
|
+
)
|
|
177
|
+
.put(
|
|
178
|
+
"/api/agent-computers/sessions/:sessionId",
|
|
179
|
+
async (context) => {
|
|
180
|
+
const { params, body, set } = context
|
|
181
|
+
const sixb = requireRequestSixb(context)
|
|
182
|
+
try {
|
|
183
|
+
const agents = host.storage.agents
|
|
184
|
+
if (!agents) return unconfiguredStorageResponse(set, "Agent storage")
|
|
185
|
+
|
|
186
|
+
const { computer } = HoldAgentComputerSessionBodySchema.parse(body)
|
|
187
|
+
const ownerPrincipal = requestActor(sixb)
|
|
188
|
+
const now = new Date()
|
|
189
|
+
const before = await agents.computers.listLive({
|
|
190
|
+
projectId: host.id,
|
|
191
|
+
ownerPrincipal,
|
|
192
|
+
now,
|
|
193
|
+
})
|
|
194
|
+
if (
|
|
195
|
+
before.length >= MAX_SESSIONS &&
|
|
196
|
+
!before.some((session) => session.id === params.sessionId)
|
|
197
|
+
) {
|
|
198
|
+
set.status = 429
|
|
199
|
+
return {
|
|
200
|
+
error: `[SixbServer] You have ${MAX_SESSIONS} sixb sessions open: quit one to open another.`,
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
const expiresAt = new Date(now.getTime() + SESSION_MS)
|
|
204
|
+
await agents.computers.hold({
|
|
205
|
+
projectId: host.id,
|
|
206
|
+
id: params.sessionId,
|
|
207
|
+
ownerPrincipal,
|
|
208
|
+
computer,
|
|
209
|
+
expiresAt,
|
|
210
|
+
})
|
|
211
|
+
if (!before.some((session) => sameComputerPlace(session.computer, computer))) {
|
|
212
|
+
await announce(host, ownerPrincipal, computer, true)
|
|
213
|
+
}
|
|
214
|
+
return HoldAgentComputerSessionResponseSchema.parse({
|
|
215
|
+
expiresAt: expiresAt.toISOString(),
|
|
216
|
+
waiting: await waitingTurns(host, agents, ownerPrincipal, computer),
|
|
217
|
+
})
|
|
218
|
+
} catch (error) {
|
|
219
|
+
return handleAgentRouteError(error, set)
|
|
220
|
+
}
|
|
115
221
|
},
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
222
|
+
{
|
|
223
|
+
params: AgentComputerSessionParamsSchema,
|
|
224
|
+
body: HoldAgentComputerSessionBodySchema,
|
|
225
|
+
response: {
|
|
226
|
+
200: HoldAgentComputerSessionResponseSchema,
|
|
227
|
+
400: AgentRequestErrorResponseSchema,
|
|
228
|
+
429: ErrorResponseSchema,
|
|
229
|
+
501: ErrorResponseSchema,
|
|
230
|
+
},
|
|
231
|
+
detail: {
|
|
232
|
+
summary: "Open or renew a computer session",
|
|
233
|
+
description:
|
|
234
|
+
"`sixb` holds one while it is open: your threads on this computer, in this folder, read as online, and you can continue them from anywhere. It lapses a minute after its last renewal. The answer lists the turns waiting for it.",
|
|
235
|
+
tags: [OPENAPI_TAGS.agentThreads.name],
|
|
236
|
+
operationId: "holdAgentComputerSession",
|
|
237
|
+
security: SIXB_SESSION_MUTATION_SECURITY_REQUIREMENT,
|
|
238
|
+
},
|
|
239
|
+
}
|
|
240
|
+
)
|
|
241
|
+
.delete(
|
|
242
|
+
"/api/agent-computers/sessions/:sessionId",
|
|
243
|
+
async (context) => {
|
|
244
|
+
const { params, set } = context
|
|
245
|
+
const sixb = requireRequestSixb(context)
|
|
246
|
+
try {
|
|
247
|
+
const agents = host.storage.agents
|
|
248
|
+
if (!agents) return unconfiguredStorageResponse(set, "Agent storage")
|
|
249
|
+
|
|
250
|
+
const released = await agents.computers.release({
|
|
251
|
+
projectId: host.id,
|
|
252
|
+
id: params.sessionId,
|
|
253
|
+
ownerPrincipal: requestActor(sixb),
|
|
254
|
+
})
|
|
255
|
+
if (released) await announceOffline(host, agents, [released])
|
|
256
|
+
set.status = 204
|
|
257
|
+
return
|
|
258
|
+
} catch (error) {
|
|
259
|
+
return handleAgentRouteError(error, set)
|
|
260
|
+
}
|
|
123
261
|
},
|
|
262
|
+
{
|
|
263
|
+
params: AgentComputerSessionParamsSchema,
|
|
264
|
+
response: {
|
|
265
|
+
204: z.void(),
|
|
266
|
+
501: ErrorResponseSchema,
|
|
267
|
+
},
|
|
268
|
+
detail: {
|
|
269
|
+
summary: "Close a computer session",
|
|
270
|
+
description: "`sixb` closes its session when it quits.",
|
|
271
|
+
tags: [OPENAPI_TAGS.agentThreads.name],
|
|
272
|
+
operationId: "releaseAgentComputerSession",
|
|
273
|
+
security: SIXB_SESSION_MUTATION_SECURITY_REQUIREMENT,
|
|
274
|
+
},
|
|
275
|
+
}
|
|
276
|
+
)
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
/** The owner's queued turns that wait for this computer, in this folder. */
|
|
280
|
+
async function waitingTurns(
|
|
281
|
+
host: SixbHostView,
|
|
282
|
+
agents: AgentStorage,
|
|
283
|
+
ownerPrincipal: Principal,
|
|
284
|
+
computer: AgentThreadComputer
|
|
285
|
+
): Promise<string[]> {
|
|
286
|
+
// Their own threads there with a turn active: nobody else's turns, however many, crowd them out.
|
|
287
|
+
const { threads } = await agents.threads.list({
|
|
288
|
+
projectId: host.id,
|
|
289
|
+
ownerPrincipal,
|
|
290
|
+
activeOnComputer: computer,
|
|
291
|
+
limit: 100,
|
|
292
|
+
})
|
|
293
|
+
const active = threads.flatMap((thread) => (thread.activeRunId ? [thread.activeRunId] : []))
|
|
294
|
+
if (active.length === 0) return []
|
|
295
|
+
const runs = await agents.runs.getByIds({ projectId: host.id, ids: active })
|
|
296
|
+
return runs
|
|
297
|
+
.filter(
|
|
298
|
+
(run) =>
|
|
299
|
+
run.kind === "conversation" && run.status === "queued" && run.spec?.runsOn === "computer"
|
|
300
|
+
)
|
|
301
|
+
.map((run) => run.id)
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
/** For each folder these sessions held, say it went offline when no other session holds it. */
|
|
305
|
+
async function announceOffline(
|
|
306
|
+
host: SixbHostView,
|
|
307
|
+
agents: AgentStorage,
|
|
308
|
+
ended: readonly AgentComputerSessionRecord[]
|
|
309
|
+
): Promise<void> {
|
|
310
|
+
const now = new Date()
|
|
311
|
+
const byOwner = new Map<string, AgentComputerSessionRecord[]>()
|
|
312
|
+
for (const session of ended) {
|
|
313
|
+
const key = principalKey(session.ownerPrincipal)
|
|
314
|
+
const sessions = byOwner.get(key)
|
|
315
|
+
if (sessions) sessions.push(session)
|
|
316
|
+
else byOwner.set(key, [session])
|
|
317
|
+
}
|
|
318
|
+
for (const [first, ...rest] of byOwner.values()) {
|
|
319
|
+
if (!first) continue
|
|
320
|
+
const { ownerPrincipal } = first
|
|
321
|
+
const live = await agents.computers.listLive({ projectId: host.id, ownerPrincipal, now })
|
|
322
|
+
const announced: AgentThreadComputer[] = []
|
|
323
|
+
for (const { computer } of [first, ...rest]) {
|
|
324
|
+
const stillHeld = [...live.map((other) => other.computer), ...announced]
|
|
325
|
+
if (stillHeld.some((other) => sameComputerPlace(other, computer))) continue
|
|
326
|
+
announced.push(computer)
|
|
327
|
+
await announce(host, ownerPrincipal, computer, false)
|
|
124
328
|
}
|
|
125
|
-
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
/** Presence is a hint the web follows: a lost announcement only delays its badge. */
|
|
333
|
+
async function announce(
|
|
334
|
+
host: SixbHostView,
|
|
335
|
+
ownerPrincipal: Principal,
|
|
336
|
+
computer: AgentThreadComputer,
|
|
337
|
+
online: boolean
|
|
338
|
+
): Promise<void> {
|
|
339
|
+
await publishAgentComputerActivity(host.broker, {
|
|
340
|
+
projectId: host.id,
|
|
341
|
+
ownerPrincipal,
|
|
342
|
+
computerId: computer.id,
|
|
343
|
+
folder: computer.folder,
|
|
344
|
+
online,
|
|
345
|
+
}).catch((error: unknown) => {
|
|
346
|
+
console.error("[SixbServer] Could not announce a computer session.", error)
|
|
347
|
+
})
|
|
126
348
|
}
|
|
@@ -16,7 +16,8 @@ import {
|
|
|
16
16
|
import { enqueueAiModelCallRecovery } from "@sixb/core/internal/model-execution"
|
|
17
17
|
import type { Elysia } from "elysia"
|
|
18
18
|
import { requestActor, requireRequestSixb } from "../auth/scope"
|
|
19
|
-
import {
|
|
19
|
+
import { liftRequestBodyCap } from "../utils/request-body"
|
|
20
|
+
import { computerClaiming, endLapsedComputerSessions } from "./agent-computers"
|
|
20
21
|
|
|
21
22
|
// A runner outside the worker, such as one on a person's computer, runs a turn through the API:
|
|
22
23
|
// it claims the turn as the person who asked for it, then reaches the turn's session with the
|
|
@@ -56,15 +57,26 @@ const SWEEP_CLAIM_MS = 60_000
|
|
|
56
57
|
/**
|
|
57
58
|
* End the runs whose runner stopped renewing them, as the worker's repair pass does: a runner on
|
|
58
59
|
* a computer has no worker behind it. Several API servers may sweep at once: each run is ended
|
|
59
|
-
* once, by whichever takes it over. Returns the stop.
|
|
60
|
+
* once, by whichever takes it over. Computer sessions that lapsed end too. Returns the stop.
|
|
60
61
|
*/
|
|
61
62
|
export function startLapsedRunSweeper(host: SixbHostView): () => void {
|
|
62
63
|
const sessions = agentRunSessionHost(host)
|
|
63
64
|
if (!sessions) return () => {}
|
|
65
|
+
// One sweep of sessions at a time: a slow one is not joined by the next.
|
|
66
|
+
let sweepingSessions = false
|
|
64
67
|
const sweep = () => {
|
|
65
68
|
void endLapsedAgentRuns(sessions, { leaseMs: SWEEP_CLAIM_MS }).catch((error: unknown) => {
|
|
66
69
|
console.error("[SixbServer] Could not end lapsed agent runs; retrying.", error)
|
|
67
70
|
})
|
|
71
|
+
if (sweepingSessions) return
|
|
72
|
+
sweepingSessions = true
|
|
73
|
+
void endLapsedComputerSessions(host)
|
|
74
|
+
.catch((error: unknown) => {
|
|
75
|
+
console.error("[SixbServer] Could not end lapsed computer sessions; retrying.", error)
|
|
76
|
+
})
|
|
77
|
+
.finally(() => {
|
|
78
|
+
sweepingSessions = false
|
|
79
|
+
})
|
|
68
80
|
}
|
|
69
81
|
sweep()
|
|
70
82
|
const timer = setInterval(sweep, SWEEP_INTERVAL_MS)
|
|
@@ -81,7 +93,12 @@ export function agentRunSessionHandler(
|
|
|
81
93
|
let sessions: AgentRunSessionHost | null | undefined
|
|
82
94
|
return async (request) => {
|
|
83
95
|
sessions ??= agentRunSessionHost(host)
|
|
84
|
-
|
|
96
|
+
// The session reads its runner's body within its own limit, past the server's default cap.
|
|
97
|
+
const served = sessions
|
|
98
|
+
? await serveAgentRunSession(sessions, request, {
|
|
99
|
+
admitted: () => liftRequestBodyCap(request),
|
|
100
|
+
})
|
|
101
|
+
: null
|
|
85
102
|
if (served) return served
|
|
86
103
|
|
|
87
104
|
// It carries the runner's authority over its run, not anyone's over the project.
|
|
@@ -151,9 +168,16 @@ export function registerAgentRunnerRoutes(app: Elysia, host: SixbHostView) {
|
|
|
151
168
|
403
|
|
152
169
|
)
|
|
153
170
|
}
|
|
154
|
-
const
|
|
155
|
-
if (
|
|
156
|
-
|
|
171
|
+
const claimed = await computerClaiming(sessions, run, context.body)
|
|
172
|
+
if ("refused" in claimed)
|
|
173
|
+
return answer({ error: claimed.refused.error }, claimed.refused.status)
|
|
174
|
+
return answer({
|
|
175
|
+
result: await claimAgentRunForRunner(sessions, {
|
|
176
|
+
run,
|
|
177
|
+
leaseExpiresAt,
|
|
178
|
+
onComputer: claimed.computer,
|
|
179
|
+
}),
|
|
180
|
+
})
|
|
157
181
|
} catch (error) {
|
|
158
182
|
// A turn whose runner stopped ends interrupted rather than being claimed again.
|
|
159
183
|
return answer(
|