@sixb/server 0.1.20 → 0.1.22
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/dist/artifacts/frame.d.ts +18 -0
- package/dist/artifacts/frame.d.ts.map +1 -0
- package/dist/auth/browser-origin.d.ts +10 -1
- package/dist/auth/browser-origin.d.ts.map +1 -1
- package/dist/auth/experience.d.ts +1 -0
- package/dist/auth/experience.d.ts.map +1 -1
- package/dist/auth/public-routes.d.ts.map +1 -1
- package/dist/auth/responses.d.ts +1 -1
- package/dist/auth/responses.d.ts.map +1 -1
- package/dist/auth/scope.d.ts +19 -3
- package/dist/auth/scope.d.ts.map +1 -1
- package/dist/files/content.d.ts +2 -3
- package/dist/files/content.d.ts.map +1 -1
- package/dist/index.js +10636 -7071
- package/dist/index.js.map +56 -38
- package/dist/models/display.d.ts +27 -0
- package/dist/models/display.d.ts.map +1 -1
- package/dist/openapi/components.d.ts +14 -0
- package/dist/openapi/components.d.ts.map +1 -0
- package/dist/openapi/tags.d.ts +22 -2
- package/dist/openapi/tags.d.ts.map +1 -1
- package/dist/registerRoutes.d.ts +3 -0
- package/dist/registerRoutes.d.ts.map +1 -1
- package/dist/routes/actions.d.ts +5 -5
- package/dist/routes/activity.d.ts +35 -0
- package/dist/routes/activity.d.ts.map +1 -0
- package/dist/routes/agent-api-gateway.d.ts.map +1 -1
- package/dist/routes/agent-approval-gate.d.ts +27 -0
- package/dist/routes/agent-approval-gate.d.ts.map +1 -0
- package/dist/routes/agent-approvals.d.ts +599 -0
- package/dist/routes/agent-approvals.d.ts.map +1 -0
- package/dist/routes/agent-children.d.ts +318 -0
- package/dist/routes/agent-children.d.ts.map +1 -0
- package/dist/routes/agent-memory.d.ts +267 -0
- package/dist/routes/agent-memory.d.ts.map +1 -0
- package/dist/routes/agents.d.ts +1008 -98
- package/dist/routes/agents.d.ts.map +1 -1
- package/dist/routes/ai-accounting.d.ts.map +1 -1
- package/dist/routes/artifact-frames.d.ts +98 -0
- package/dist/routes/artifact-frames.d.ts.map +1 -0
- package/dist/routes/artifacts.d.ts +1508 -0
- package/dist/routes/artifacts.d.ts.map +1 -0
- package/dist/routes/auth.d.ts +117 -11
- package/dist/routes/auth.d.ts.map +1 -1
- package/dist/routes/connector-connection-runs.d.ts +84 -84
- package/dist/routes/connector-connections.d.ts +2 -2
- package/dist/routes/connectors.d.ts +2 -2
- package/dist/routes/datasets.d.ts +27 -27
- package/dist/routes/events.d.ts +5 -5
- package/dist/routes/files.d.ts +14 -14
- package/dist/routes/files.d.ts.map +1 -1
- package/dist/routes/links.d.ts +2 -2
- package/dist/routes/logs.d.ts +5 -5
- package/dist/routes/model-calls.d.ts +155 -0
- package/dist/routes/model-calls.d.ts.map +1 -0
- package/dist/routes/model-generations.d.ts +400 -0
- package/dist/routes/model-generations.d.ts.map +1 -0
- package/dist/routes/models.d.ts +434 -1
- package/dist/routes/models.d.ts.map +1 -1
- package/dist/routes/objects.d.ts +71 -40
- package/dist/routes/objects.d.ts.map +1 -1
- package/dist/routes/observed-agent-threads.d.ts +682 -0
- package/dist/routes/observed-agent-threads.d.ts.map +1 -0
- package/dist/routes/ontology.d.ts +66 -62
- package/dist/routes/ontology.d.ts.map +1 -1
- package/dist/routes/pipelines.d.ts +53 -53
- package/dist/routes/projections.d.ts +63 -63
- package/dist/routes/rules.d.ts +2 -2
- package/dist/routes/share-grants.d.ts +4 -4
- package/dist/routes/syncs.d.ts +18 -18
- package/dist/routes/users.d.ts +117 -0
- package/dist/routes/users.d.ts.map +1 -0
- package/dist/routes/webhook-runs.d.ts +1 -1
- package/dist/routes/workflows.d.ts +142 -76
- package/dist/routes/workflows.d.ts.map +1 -1
- package/dist/routes/ws/agents.d.ts +7 -2
- package/dist/routes/ws/agents.d.ts.map +1 -1
- package/dist/routes/ws/events.d.ts +6 -6
- package/dist/routes/ws/events.d.ts.map +1 -1
- package/dist/routes/ws/logs.d.ts +14 -14
- package/dist/routes/ws/subscription-hub.d.ts +8 -3
- package/dist/routes/ws/subscription-hub.d.ts.map +1 -1
- package/dist/schemas/actions.d.ts +15 -15
- package/dist/schemas/activity.d.ts +1298 -0
- package/dist/schemas/activity.d.ts.map +1 -0
- package/dist/schemas/agent-approvals.d.ts +1091 -0
- package/dist/schemas/agent-approvals.d.ts.map +1 -0
- package/dist/schemas/agent-memory.d.ts +250 -0
- package/dist/schemas/agent-memory.d.ts.map +1 -0
- package/dist/schemas/agents.d.ts +5485 -460
- package/dist/schemas/agents.d.ts.map +1 -1
- package/dist/schemas/ai-accounting.d.ts +1315 -436
- package/dist/schemas/ai-accounting.d.ts.map +1 -1
- package/dist/schemas/ai-limits.d.ts +36 -36
- package/dist/schemas/ai-usage.d.ts +12 -0
- package/dist/schemas/ai-usage.d.ts.map +1 -1
- package/dist/schemas/artifacts.d.ts +1857 -0
- package/dist/schemas/artifacts.d.ts.map +1 -0
- package/dist/schemas/auth.d.ts +125 -52
- package/dist/schemas/auth.d.ts.map +1 -1
- package/dist/schemas/connectors.d.ts +42 -42
- package/dist/schemas/datasets.d.ts +108 -108
- package/dist/schemas/event-payloads.d.ts +6 -6
- package/dist/schemas/events.d.ts +32 -32
- package/dist/schemas/events.d.ts.map +1 -1
- package/dist/schemas/files.d.ts +24 -24
- package/dist/schemas/links.d.ts +2 -2
- package/dist/schemas/logs.d.ts +32 -32
- package/dist/schemas/models.d.ts +8889 -10
- package/dist/schemas/models.d.ts.map +1 -1
- package/dist/schemas/objects.d.ts +48 -12
- package/dist/schemas/objects.d.ts.map +1 -1
- package/dist/schemas/ontology.d.ts +127 -104
- package/dist/schemas/ontology.d.ts.map +1 -1
- package/dist/schemas/pipelines.d.ts +166 -166
- package/dist/schemas/projections.d.ts +346 -346
- package/dist/schemas/rules.d.ts +4 -4
- package/dist/schemas/share-grants.d.ts +14 -14
- package/dist/schemas/syncs.d.ts +40 -40
- package/dist/schemas/users.d.ts +72 -0
- package/dist/schemas/users.d.ts.map +1 -0
- package/dist/schemas/webhook-runs.d.ts +6 -6
- package/dist/schemas/workflows.d.ts +769 -254
- package/dist/schemas/workflows.d.ts.map +1 -1
- package/dist/server.d.ts +1 -0
- package/dist/server.d.ts.map +1 -1
- package/dist/utils/http.d.ts.map +1 -1
- package/package.json +7 -7
- package/src/artifacts/frame.ts +51 -0
- package/src/auth/browser-origin.ts +20 -5
- package/src/auth/experience.ts +1 -1
- package/src/auth/guard.ts +2 -2
- package/src/auth/public-routes.ts +16 -0
- package/src/auth/responses.ts +7 -6
- package/src/auth/scope.ts +32 -3
- package/src/files/content.ts +9 -13
- package/src/models/display.ts +61 -16
- package/src/openapi/components.ts +54 -0
- package/src/openapi/tags.ts +20 -0
- package/src/registerRoutes.ts +23 -0
- package/src/routes/activity.ts +327 -0
- package/src/routes/agent-api-gateway.ts +115 -91
- package/src/routes/agent-approval-gate.ts +213 -0
- package/src/routes/agent-approvals.ts +299 -0
- package/src/routes/agent-children.ts +209 -0
- package/src/routes/agent-memory.ts +210 -0
- package/src/routes/agents.ts +364 -143
- package/src/routes/ai-accounting.ts +4 -0
- package/src/routes/artifact-frames.ts +144 -0
- package/src/routes/artifacts.ts +717 -0
- package/src/routes/auth.ts +306 -13
- package/src/routes/file-downloads.ts +1 -3
- package/src/routes/files.ts +6 -7
- package/src/routes/model-calls.ts +120 -0
- package/src/routes/model-generations.ts +245 -0
- package/src/routes/models.ts +116 -9
- package/src/routes/objects.ts +55 -61
- package/src/routes/observed-agent-threads.ts +253 -0
- package/src/routes/ontology.ts +1 -0
- package/src/routes/users.ts +73 -0
- package/src/routes/workflows.ts +6 -9
- package/src/routes/ws/agents.ts +10 -5
- package/src/routes/ws/events.ts +3 -2
- package/src/routes/ws/subscription-hub.ts +53 -20
- package/src/schemas/activity.ts +287 -0
- package/src/schemas/agent-approvals.ts +129 -0
- package/src/schemas/agent-memory.ts +96 -0
- package/src/schemas/agents.ts +240 -2
- package/src/schemas/ai-accounting.ts +40 -4
- package/src/schemas/ai-usage.ts +10 -0
- package/src/schemas/artifacts.ts +246 -0
- package/src/schemas/auth.ts +25 -0
- package/src/schemas/models.ts +197 -1
- package/src/schemas/objects.ts +181 -0
- package/src/schemas/ontology.ts +1 -0
- package/src/schemas/users.ts +24 -0
- package/src/schemas/workflows.ts +2 -0
- package/src/server.ts +34 -19
- package/src/utils/http.ts +2 -0
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
import type { AgentChildRunView, AgentChildView, SixbHostView } from "@sixb/core"
|
|
2
|
+
import { agentRunStreamId } from "@sixb/core/internal/agents/streams"
|
|
3
|
+
import type { Elysia } from "elysia"
|
|
4
|
+
import { z } from "zod"
|
|
5
|
+
import { accessTokenSecurityRequirement } from "../auth/access-token-boundary"
|
|
6
|
+
import { requireRequestSixb } from "../auth/scope"
|
|
7
|
+
import {
|
|
8
|
+
createContextualFileContentResponse,
|
|
9
|
+
fileContentGetResponses,
|
|
10
|
+
fileContentHeadResponses,
|
|
11
|
+
handleFileContentQueryValidationError,
|
|
12
|
+
} from "../files/content"
|
|
13
|
+
import { OPENAPI_TAGS } from "../openapi/tags"
|
|
14
|
+
import {
|
|
15
|
+
AgentChildListQuerySchema,
|
|
16
|
+
AgentChildListResponseSchema,
|
|
17
|
+
AgentChildSchema,
|
|
18
|
+
AgentThreadParamsSchema,
|
|
19
|
+
} from "../schemas/agents"
|
|
20
|
+
import { ErrorResponseSchema } from "../schemas/common"
|
|
21
|
+
import { FileContentQuerySchema } from "../schemas/files"
|
|
22
|
+
import { toIsoString, unconfiguredStorageResponse } from "../utils/http"
|
|
23
|
+
import { handleAgentRouteError } from "./agents"
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* A file of one run of a sub-agent: one its parent handed it (`/handedFiles/<n>/fileRef`), or one
|
|
27
|
+
* the run returned from `out/` (`/files/<n>/fileRef`). What a tool showed it is a file of its
|
|
28
|
+
* thread's messages.
|
|
29
|
+
*/
|
|
30
|
+
export const AgentChildFileContentQuerySchema = FileContentQuerySchema.extend({
|
|
31
|
+
run: z.string().min(1),
|
|
32
|
+
path: z
|
|
33
|
+
.string()
|
|
34
|
+
.min(1)
|
|
35
|
+
.regex(
|
|
36
|
+
/^\/(?:handedFiles|files)(?:\/|$)/,
|
|
37
|
+
"Sub-agent file content paths must start with /handedFiles/ or /files/"
|
|
38
|
+
),
|
|
39
|
+
})
|
|
40
|
+
|
|
41
|
+
const VISIBILITY =
|
|
42
|
+
"A sub-agent is visible exactly when its conversation is: to the thread's owner and participants; observers read it through the observed routes. What it did is its thread's messages, at `GET /api/agent-threads/:threadId/messages` with its `threadId`."
|
|
43
|
+
|
|
44
|
+
/** A sub-agent as the lists return it. */
|
|
45
|
+
export function serializeAgentChild(
|
|
46
|
+
child: AgentChildView
|
|
47
|
+
): ReturnType<typeof AgentChildSchema.parse> {
|
|
48
|
+
return AgentChildSchema.parse({
|
|
49
|
+
id: child.id,
|
|
50
|
+
threadId: child.threadId,
|
|
51
|
+
projectId: child.projectId,
|
|
52
|
+
spawnedBy: child.spawnedBy,
|
|
53
|
+
key: child.key,
|
|
54
|
+
task: child.task,
|
|
55
|
+
model: child.model,
|
|
56
|
+
tier: child.tier,
|
|
57
|
+
reasoning: child.reasoning,
|
|
58
|
+
handedFiles: child.handedFiles,
|
|
59
|
+
status: child.status,
|
|
60
|
+
finishReason: child.finishReason,
|
|
61
|
+
error: child.error,
|
|
62
|
+
awaiting: child.awaiting,
|
|
63
|
+
ended: child.ended,
|
|
64
|
+
activeRunId: child.activeRunId,
|
|
65
|
+
createdAt: toIsoString(child.createdAt),
|
|
66
|
+
completedAt: child.completedAt ? toIsoString(child.completedAt) : undefined,
|
|
67
|
+
usage: child.usage,
|
|
68
|
+
cost: child.cost,
|
|
69
|
+
runs: child.runs.map(serializeAgentChildRun),
|
|
70
|
+
})
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function serializeAgentChildRun(run: AgentChildRunView) {
|
|
74
|
+
return {
|
|
75
|
+
id: run.id,
|
|
76
|
+
parentRunId: run.parentRunId,
|
|
77
|
+
status: run.status,
|
|
78
|
+
finishReason: run.finishReason,
|
|
79
|
+
error: run.error,
|
|
80
|
+
modelId: run.modelId,
|
|
81
|
+
attempt: run.attempt,
|
|
82
|
+
streamId: agentRunStreamId(run.id),
|
|
83
|
+
createdAt: toIsoString(run.createdAt),
|
|
84
|
+
startedAt: run.startedAt ? toIsoString(run.startedAt) : undefined,
|
|
85
|
+
completedAt: run.completedAt ? toIsoString(run.completedAt) : undefined,
|
|
86
|
+
result: run.result,
|
|
87
|
+
usage: run.usage,
|
|
88
|
+
cost: run.cost,
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/** A file of a sub-agent's run, read through whoever may read the sub-agent. */
|
|
93
|
+
export function agentChildFileContentResponse(
|
|
94
|
+
host: SixbHostView,
|
|
95
|
+
context: {
|
|
96
|
+
readonly query: unknown
|
|
97
|
+
readonly request: Request
|
|
98
|
+
readonly set: { status?: number | string }
|
|
99
|
+
},
|
|
100
|
+
readChild: () => Promise<AgentChildView | null>,
|
|
101
|
+
options: { readonly head?: boolean } = {}
|
|
102
|
+
) {
|
|
103
|
+
if (!host.storage.agents) {
|
|
104
|
+
return unconfiguredStorageResponse(context.set, "Agent storage")
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
return createContextualFileContentResponse({
|
|
108
|
+
blobStorage: host.blobStorage,
|
|
109
|
+
query: context.query,
|
|
110
|
+
querySchema: AgentChildFileContentQuerySchema,
|
|
111
|
+
request: context.request,
|
|
112
|
+
set: context.set,
|
|
113
|
+
head: options.head,
|
|
114
|
+
resolveRoot: async (query) => {
|
|
115
|
+
const child = await readChild()
|
|
116
|
+
const run = child?.runs.find((candidate) => candidate.id === query.run)
|
|
117
|
+
return child && run
|
|
118
|
+
? { handedFiles: child.handedFiles, files: run.result?.files ?? [] }
|
|
119
|
+
: null
|
|
120
|
+
},
|
|
121
|
+
})
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* A conversation's sub-agents, and their files. What one did is its thread's: the thread routes
|
|
126
|
+
* read it, and `/api/agent-runs/:runId` its runs.
|
|
127
|
+
*/
|
|
128
|
+
export function registerAgentChildRoutes(app: Elysia, host: SixbHostView) {
|
|
129
|
+
const readChild = (context: { readonly params: { readonly threadId: string } }) => () =>
|
|
130
|
+
requireRequestSixb(context).agent.threads.getChild(context.params.threadId)
|
|
131
|
+
|
|
132
|
+
return app
|
|
133
|
+
.get(
|
|
134
|
+
"/api/agent-threads/:threadId/children",
|
|
135
|
+
async (context) => {
|
|
136
|
+
const { params, set } = context
|
|
137
|
+
try {
|
|
138
|
+
if (!host.storage.agents) {
|
|
139
|
+
return unconfiguredStorageResponse(set, "Agent storage")
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
const query = AgentChildListQuerySchema.parse(context.query)
|
|
143
|
+
const result = await requireRequestSixb(context).agent.threads.listChildren(
|
|
144
|
+
params.threadId,
|
|
145
|
+
query.runId === undefined ? {} : { runId: query.runId }
|
|
146
|
+
)
|
|
147
|
+
if (!result) {
|
|
148
|
+
set.status = 404
|
|
149
|
+
return { error: "Agent thread not found" }
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
return AgentChildListResponseSchema.parse({
|
|
153
|
+
children: result.children.map(serializeAgentChild),
|
|
154
|
+
})
|
|
155
|
+
} catch (error) {
|
|
156
|
+
return handleAgentRouteError(error, set)
|
|
157
|
+
}
|
|
158
|
+
},
|
|
159
|
+
{
|
|
160
|
+
params: AgentThreadParamsSchema,
|
|
161
|
+
query: AgentChildListQuerySchema,
|
|
162
|
+
response: {
|
|
163
|
+
200: AgentChildListResponseSchema,
|
|
164
|
+
400: ErrorResponseSchema,
|
|
165
|
+
404: ErrorResponseSchema,
|
|
166
|
+
501: ErrorResponseSchema,
|
|
167
|
+
},
|
|
168
|
+
detail: {
|
|
169
|
+
summary: "List the sub-agents of an agent conversation",
|
|
170
|
+
description: `${VISIBILITY} \`runId\` keeps those one turn spawned. Each lists every run it took, the one at work by \`activeRunId\`.`,
|
|
171
|
+
tags: [OPENAPI_TAGS.agentThreads.name],
|
|
172
|
+
operationId: "listAgentThreadChildren",
|
|
173
|
+
},
|
|
174
|
+
}
|
|
175
|
+
)
|
|
176
|
+
.get(
|
|
177
|
+
"/api/agent-threads/:threadId/child-files/content",
|
|
178
|
+
(context) => agentChildFileContentResponse(host, context, readChild(context)),
|
|
179
|
+
{
|
|
180
|
+
params: AgentThreadParamsSchema,
|
|
181
|
+
query: AgentChildFileContentQuerySchema,
|
|
182
|
+
error: handleFileContentQueryValidationError,
|
|
183
|
+
detail: {
|
|
184
|
+
summary: "Get a file of a sub-agent",
|
|
185
|
+
description: `${VISIBILITY} \`threadId\` is the sub-agent's thread, \`run\` one of its runs, and \`path\` points into it: \`/handedFiles/0/fileRef\`, or \`/files/0/fileRef\` for what the run returned.`,
|
|
186
|
+
tags: [OPENAPI_TAGS.agentThreads.name],
|
|
187
|
+
operationId: "getAgentChildFileContent",
|
|
188
|
+
security: accessTokenSecurityRequirement("getAgentChildFileContent"),
|
|
189
|
+
responses: fileContentGetResponses({ optionalStorage: true }),
|
|
190
|
+
},
|
|
191
|
+
}
|
|
192
|
+
)
|
|
193
|
+
.head(
|
|
194
|
+
"/api/agent-threads/:threadId/child-files/content",
|
|
195
|
+
(context) => agentChildFileContentResponse(host, context, readChild(context), { head: true }),
|
|
196
|
+
{
|
|
197
|
+
params: AgentThreadParamsSchema,
|
|
198
|
+
query: AgentChildFileContentQuerySchema,
|
|
199
|
+
error: handleFileContentQueryValidationError,
|
|
200
|
+
detail: {
|
|
201
|
+
summary: "Head a file of a sub-agent",
|
|
202
|
+
tags: [OPENAPI_TAGS.agentThreads.name],
|
|
203
|
+
operationId: "headAgentChildFileContent",
|
|
204
|
+
security: accessTokenSecurityRequirement("headAgentChildFileContent"),
|
|
205
|
+
responses: fileContentHeadResponses({ optionalStorage: true }),
|
|
206
|
+
},
|
|
207
|
+
}
|
|
208
|
+
)
|
|
209
|
+
}
|
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
import { AgentMemoryError, type AgentMemoryRepoName } from "@sixb/core"
|
|
2
|
+
import type { Elysia } from "elysia"
|
|
3
|
+
import { ZodError } from "zod"
|
|
4
|
+
import { accessTokenSecurityRequirement } from "../auth/access-token-boundary"
|
|
5
|
+
import { requestAuthState, requireRequestSixb } from "../auth/scope"
|
|
6
|
+
import { OPENAPI_TAGS } from "../openapi/tags"
|
|
7
|
+
import {
|
|
8
|
+
AgentMemoryConflictResponseSchema,
|
|
9
|
+
AgentMemoryErrorResponseSchema,
|
|
10
|
+
AgentMemoryParamsSchema,
|
|
11
|
+
AgentMemoryRepoQuerySchema,
|
|
12
|
+
AgentMemoryRepoResponseSchema,
|
|
13
|
+
AgentMemoryRevisionListQuerySchema,
|
|
14
|
+
AgentMemoryRevisionListResponseSchema,
|
|
15
|
+
DeleteAgentMemoryResponseSchema,
|
|
16
|
+
SaveAgentMemoryBodySchema,
|
|
17
|
+
SavedAgentMemoryResponseSchema,
|
|
18
|
+
} from "../schemas/agent-memory"
|
|
19
|
+
import { handleRouteError, parseOptionalInt, toIsoString } from "../utils/http"
|
|
20
|
+
|
|
21
|
+
type RouteSet = { status?: number | string }
|
|
22
|
+
|
|
23
|
+
function handleAgentMemoryRouteError(error: unknown, set: RouteSet) {
|
|
24
|
+
if (error instanceof AgentMemoryError) {
|
|
25
|
+
switch (error.code) {
|
|
26
|
+
case "invalid_repo":
|
|
27
|
+
case "invalid_save":
|
|
28
|
+
set.status = 400
|
|
29
|
+
break
|
|
30
|
+
case "not_allowed":
|
|
31
|
+
set.status = 403
|
|
32
|
+
break
|
|
33
|
+
case "storage_unavailable":
|
|
34
|
+
set.status = 501
|
|
35
|
+
break
|
|
36
|
+
}
|
|
37
|
+
// The message says what to change, so the Agent can act on it as it is.
|
|
38
|
+
return { error: error.message, code: error.code }
|
|
39
|
+
}
|
|
40
|
+
if (error instanceof ZodError) {
|
|
41
|
+
set.status = 400
|
|
42
|
+
return { error: "Invalid Agent memory request" }
|
|
43
|
+
}
|
|
44
|
+
return handleRouteError(error, set)
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function repoParam(params: unknown): AgentMemoryRepoName {
|
|
48
|
+
return AgentMemoryParamsSchema.parse(params).repo
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export function registerAgentMemoryRoutes(app: Elysia) {
|
|
52
|
+
return app
|
|
53
|
+
.get(
|
|
54
|
+
"/api/agent-memory/:repo",
|
|
55
|
+
async (context) => {
|
|
56
|
+
try {
|
|
57
|
+
const revision = parseOptionalInt(
|
|
58
|
+
AgentMemoryRepoQuerySchema.parse(context.query).revision
|
|
59
|
+
)
|
|
60
|
+
const repo = await requireRequestSixb(context).agent.memory.get(
|
|
61
|
+
repoParam(context.params),
|
|
62
|
+
revision === undefined ? {} : { revision }
|
|
63
|
+
)
|
|
64
|
+
return AgentMemoryRepoResponseSchema.parse(repo)
|
|
65
|
+
} catch (error) {
|
|
66
|
+
return handleAgentMemoryRouteError(error, context.set)
|
|
67
|
+
}
|
|
68
|
+
},
|
|
69
|
+
{
|
|
70
|
+
params: AgentMemoryParamsSchema,
|
|
71
|
+
query: AgentMemoryRepoQuerySchema,
|
|
72
|
+
response: {
|
|
73
|
+
200: AgentMemoryRepoResponseSchema,
|
|
74
|
+
400: AgentMemoryErrorResponseSchema,
|
|
75
|
+
403: AgentMemoryErrorResponseSchema,
|
|
76
|
+
501: AgentMemoryErrorResponseSchema,
|
|
77
|
+
},
|
|
78
|
+
detail: {
|
|
79
|
+
summary: "Get an Agent memory repo",
|
|
80
|
+
description:
|
|
81
|
+
"Every file of the repo as of its latest revision, or as an earlier revision left it.",
|
|
82
|
+
tags: [OPENAPI_TAGS.agentMemory.name],
|
|
83
|
+
operationId: "getAgentMemory",
|
|
84
|
+
security: accessTokenSecurityRequirement("getAgentMemory"),
|
|
85
|
+
},
|
|
86
|
+
}
|
|
87
|
+
)
|
|
88
|
+
.get(
|
|
89
|
+
"/api/agent-memory/:repo/revisions",
|
|
90
|
+
async (context) => {
|
|
91
|
+
try {
|
|
92
|
+
const query = AgentMemoryRevisionListQuerySchema.parse(context.query)
|
|
93
|
+
const limit = parseOptionalInt(query.limit)
|
|
94
|
+
const offset = parseOptionalInt(query.offset)
|
|
95
|
+
const result = await requireRequestSixb(context).agent.memory.listRevisions(
|
|
96
|
+
repoParam(context.params),
|
|
97
|
+
{
|
|
98
|
+
...(limit === undefined ? {} : { limit }),
|
|
99
|
+
...(offset === undefined ? {} : { offset }),
|
|
100
|
+
}
|
|
101
|
+
)
|
|
102
|
+
return AgentMemoryRevisionListResponseSchema.parse({
|
|
103
|
+
...result,
|
|
104
|
+
revisions: result.revisions.map((revision) => ({
|
|
105
|
+
...revision,
|
|
106
|
+
createdAt: toIsoString(revision.createdAt),
|
|
107
|
+
})),
|
|
108
|
+
})
|
|
109
|
+
} catch (error) {
|
|
110
|
+
return handleAgentMemoryRouteError(error, context.set)
|
|
111
|
+
}
|
|
112
|
+
},
|
|
113
|
+
{
|
|
114
|
+
params: AgentMemoryParamsSchema,
|
|
115
|
+
query: AgentMemoryRevisionListQuerySchema,
|
|
116
|
+
response: {
|
|
117
|
+
200: AgentMemoryRevisionListResponseSchema,
|
|
118
|
+
400: AgentMemoryErrorResponseSchema,
|
|
119
|
+
403: AgentMemoryErrorResponseSchema,
|
|
120
|
+
501: AgentMemoryErrorResponseSchema,
|
|
121
|
+
},
|
|
122
|
+
detail: {
|
|
123
|
+
summary: "List an Agent memory repo's revisions",
|
|
124
|
+
description: "Each save is one revision, newest first.",
|
|
125
|
+
tags: [OPENAPI_TAGS.agentMemory.name],
|
|
126
|
+
operationId: "listAgentMemoryRevisions",
|
|
127
|
+
security: accessTokenSecurityRequirement("listAgentMemoryRevisions"),
|
|
128
|
+
},
|
|
129
|
+
}
|
|
130
|
+
)
|
|
131
|
+
.post(
|
|
132
|
+
"/api/agent-memory/:repo/revisions",
|
|
133
|
+
async (context) => {
|
|
134
|
+
try {
|
|
135
|
+
const repo = repoParam(context.params)
|
|
136
|
+
const body = SaveAgentMemoryBodySchema.parse(context.body)
|
|
137
|
+
// The conversation is the run's own, never a value the request supplies.
|
|
138
|
+
const { agentRun } = requestAuthState(context)
|
|
139
|
+
const result = await requireRequestSixb(context).agent.memory.save(repo, {
|
|
140
|
+
...body,
|
|
141
|
+
...(agentRun ? { source: `/agents/${agentRun.threadId}` } : {}),
|
|
142
|
+
})
|
|
143
|
+
if (!result.saved) {
|
|
144
|
+
context.set.status = 409
|
|
145
|
+
return AgentMemoryConflictResponseSchema.parse({
|
|
146
|
+
error:
|
|
147
|
+
"Another save changed these files since they were loaded. Merge their contents into yours and save again.",
|
|
148
|
+
code: "conflict",
|
|
149
|
+
revision: result.revision,
|
|
150
|
+
conflicts: result.conflicts,
|
|
151
|
+
})
|
|
152
|
+
}
|
|
153
|
+
context.set.status = 201
|
|
154
|
+
return SavedAgentMemoryResponseSchema.parse({
|
|
155
|
+
repo,
|
|
156
|
+
revision: result.revision,
|
|
157
|
+
warnings: result.warnings,
|
|
158
|
+
})
|
|
159
|
+
} catch (error) {
|
|
160
|
+
return handleAgentMemoryRouteError(error, context.set)
|
|
161
|
+
}
|
|
162
|
+
},
|
|
163
|
+
{
|
|
164
|
+
params: AgentMemoryParamsSchema,
|
|
165
|
+
body: SaveAgentMemoryBodySchema,
|
|
166
|
+
response: {
|
|
167
|
+
201: SavedAgentMemoryResponseSchema,
|
|
168
|
+
400: AgentMemoryErrorResponseSchema,
|
|
169
|
+
403: AgentMemoryErrorResponseSchema,
|
|
170
|
+
409: AgentMemoryConflictResponseSchema,
|
|
171
|
+
501: AgentMemoryErrorResponseSchema,
|
|
172
|
+
},
|
|
173
|
+
detail: {
|
|
174
|
+
summary: "Save changes to an Agent memory repo",
|
|
175
|
+
description:
|
|
176
|
+
"Stores the changes as the repo's next revision. When another save changed one of the files after baseRevision, nothing is saved and the response holds those files as they are now.",
|
|
177
|
+
tags: [OPENAPI_TAGS.agentMemory.name],
|
|
178
|
+
operationId: "saveAgentMemory",
|
|
179
|
+
security: accessTokenSecurityRequirement("saveAgentMemory"),
|
|
180
|
+
},
|
|
181
|
+
}
|
|
182
|
+
)
|
|
183
|
+
.delete(
|
|
184
|
+
"/api/agent-memory/:repo",
|
|
185
|
+
async (context) => {
|
|
186
|
+
try {
|
|
187
|
+
await requireRequestSixb(context).agent.memory.delete(repoParam(context.params))
|
|
188
|
+
return DeleteAgentMemoryResponseSchema.parse({ deleted: true })
|
|
189
|
+
} catch (error) {
|
|
190
|
+
return handleAgentMemoryRouteError(error, context.set)
|
|
191
|
+
}
|
|
192
|
+
},
|
|
193
|
+
{
|
|
194
|
+
params: AgentMemoryParamsSchema,
|
|
195
|
+
response: {
|
|
196
|
+
200: DeleteAgentMemoryResponseSchema,
|
|
197
|
+
400: AgentMemoryErrorResponseSchema,
|
|
198
|
+
403: AgentMemoryErrorResponseSchema,
|
|
199
|
+
501: AgentMemoryErrorResponseSchema,
|
|
200
|
+
},
|
|
201
|
+
detail: {
|
|
202
|
+
summary: "Delete an Agent memory repo",
|
|
203
|
+
description: "Deletes the repo with its history.",
|
|
204
|
+
tags: [OPENAPI_TAGS.agentMemory.name],
|
|
205
|
+
operationId: "deleteAgentMemory",
|
|
206
|
+
security: accessTokenSecurityRequirement("deleteAgentMemory"),
|
|
207
|
+
},
|
|
208
|
+
}
|
|
209
|
+
)
|
|
210
|
+
}
|