@contractspec/example.agent-console 3.7.6 → 3.8.2
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/.turbo/turbo-build.log +126 -105
- package/AGENTS.md +52 -31
- package/CHANGELOG.md +29 -0
- package/README.md +112 -83
- package/dist/agent/agent.event.js +1 -1
- package/dist/agent/agent.handler.d.ts +3 -0
- package/dist/agent/agent.handler.js +730 -1
- package/dist/agent/agent.operation.js +1 -1
- package/dist/agent/index.d.ts +5 -5
- package/dist/agent/index.js +74 -73
- package/dist/agent.feature.js +179 -0
- package/dist/browser/agent/agent.event.js +1 -1
- package/dist/browser/agent/agent.handler.js +730 -1
- package/dist/browser/agent/agent.operation.js +1 -1
- package/dist/browser/agent/index.js +74 -73
- package/dist/browser/agent.feature.js +179 -0
- package/dist/browser/docs/agent-console.docblock.js +11 -8
- package/dist/browser/docs/index.js +11 -8
- package/dist/browser/example.js +2 -3
- package/dist/browser/handlers/agent.handlers.js +1883 -2
- package/dist/browser/handlers/index.js +2142 -8
- package/dist/browser/index.js +4075 -3161
- package/dist/browser/presentations/index.js +51 -51
- package/dist/browser/run/index.js +380 -374
- package/dist/browser/run/run.event.js +2 -2
- package/dist/browser/run/run.handler.js +666 -1
- package/dist/browser/run/run.presentation.js +2 -2
- package/dist/browser/shared/index.js +293 -1
- package/dist/browser/shared/mock-runs.js +5 -0
- package/dist/browser/tool/index.js +161 -161
- package/dist/browser/tool/tool.event.js +1 -1
- package/dist/browser/tool/tool.handler.js +479 -3
- package/dist/browser/tool/tool.presentation.js +2 -2
- package/dist/browser/ui/AgentDashboard.js +1816 -931
- package/dist/browser/ui/AgentDashboard.visualizations.js +217 -0
- package/dist/browser/ui/AgentRunList.js +360 -128
- package/dist/browser/ui/AgentToolRegistry.js +9 -9
- package/dist/browser/ui/hooks/index.js +611 -161
- package/dist/browser/ui/hooks/useAgentList.js +1 -1
- package/dist/browser/ui/hooks/useAgentMutations.js +444 -9
- package/dist/browser/ui/hooks/useRunList.js +26 -11
- package/dist/browser/ui/hooks/useToolList.js +1 -1
- package/dist/browser/ui/index.js +2161 -1258
- package/dist/browser/ui/modals/AgentActionsModal.js +13 -13
- package/dist/browser/ui/modals/CreateAgentModal.js +15 -15
- package/dist/browser/ui/modals/index.js +297 -297
- package/dist/browser/ui/renderers/agent-list.markdown.js +14 -5
- package/dist/browser/ui/renderers/agent-list.renderer.js +7 -7
- package/dist/browser/ui/renderers/dashboard.markdown.js +207 -36
- package/dist/browser/ui/renderers/index.js +359 -163
- package/dist/browser/ui/renderers/run-list.markdown.js +9 -4
- package/dist/browser/ui/renderers/tool-registry.markdown.js +15 -4
- package/dist/browser/ui/views/AgentListView.js +7 -7
- package/dist/browser/ui/views/RunDataTable.js +326 -0
- package/dist/browser/ui/views/RunListView.js +360 -128
- package/dist/browser/ui/views/ToolRegistryView.js +9 -9
- package/dist/browser/ui/views/index.js +478 -246
- package/dist/browser/ui/views/run-data-table.columns.js +271 -0
- package/dist/browser/ui/views/run-list.shared.js +177 -0
- package/dist/browser/visualizations/catalog.js +134 -0
- package/dist/browser/visualizations/index.js +187 -0
- package/dist/browser/visualizations/selectors.js +181 -0
- package/dist/docs/agent-console.docblock.js +11 -8
- package/dist/docs/index.js +11 -8
- package/dist/example.js +2 -3
- package/dist/example.test.d.ts +1 -0
- package/dist/handlers/agent.handlers.d.ts +2 -0
- package/dist/handlers/agent.handlers.js +1883 -2
- package/dist/handlers/index.d.ts +2 -4
- package/dist/handlers/index.js +2142 -8
- package/dist/handlers/mock-handlers.test.d.ts +1 -0
- package/dist/index.d.ts +6 -4
- package/dist/index.js +4075 -3161
- package/dist/node/agent/agent.event.js +1 -1
- package/dist/node/agent/agent.handler.js +730 -1
- package/dist/node/agent/agent.operation.js +1 -1
- package/dist/node/agent/index.js +74 -73
- package/dist/node/agent.feature.js +179 -0
- package/dist/node/docs/agent-console.docblock.js +11 -8
- package/dist/node/docs/index.js +11 -8
- package/dist/node/example.js +2 -3
- package/dist/node/handlers/agent.handlers.js +1883 -2
- package/dist/node/handlers/index.js +2142 -8
- package/dist/node/index.js +4075 -3161
- package/dist/node/presentations/index.js +51 -51
- package/dist/node/run/index.js +380 -374
- package/dist/node/run/run.event.js +2 -2
- package/dist/node/run/run.handler.js +666 -1
- package/dist/node/run/run.presentation.js +2 -2
- package/dist/node/shared/index.js +293 -1
- package/dist/node/shared/mock-runs.js +5 -0
- package/dist/node/tool/index.js +161 -161
- package/dist/node/tool/tool.event.js +1 -1
- package/dist/node/tool/tool.handler.js +479 -3
- package/dist/node/tool/tool.presentation.js +2 -2
- package/dist/node/ui/AgentDashboard.js +1816 -931
- package/dist/node/ui/AgentDashboard.visualizations.js +217 -0
- package/dist/node/ui/AgentRunList.js +360 -128
- package/dist/node/ui/AgentToolRegistry.js +9 -9
- package/dist/node/ui/hooks/index.js +611 -161
- package/dist/node/ui/hooks/useAgentList.js +1 -1
- package/dist/node/ui/hooks/useAgentMutations.js +444 -9
- package/dist/node/ui/hooks/useRunList.js +26 -11
- package/dist/node/ui/hooks/useToolList.js +1 -1
- package/dist/node/ui/index.js +2161 -1258
- package/dist/node/ui/modals/AgentActionsModal.js +13 -13
- package/dist/node/ui/modals/CreateAgentModal.js +15 -15
- package/dist/node/ui/modals/index.js +297 -297
- package/dist/node/ui/renderers/agent-list.markdown.js +14 -5
- package/dist/node/ui/renderers/agent-list.renderer.js +7 -7
- package/dist/node/ui/renderers/dashboard.markdown.js +207 -36
- package/dist/node/ui/renderers/index.js +359 -163
- package/dist/node/ui/renderers/run-list.markdown.js +9 -4
- package/dist/node/ui/renderers/tool-registry.markdown.js +15 -4
- package/dist/node/ui/views/AgentListView.js +7 -7
- package/dist/node/ui/views/RunDataTable.js +326 -0
- package/dist/node/ui/views/RunListView.js +360 -128
- package/dist/node/ui/views/ToolRegistryView.js +9 -9
- package/dist/node/ui/views/index.js +478 -246
- package/dist/node/ui/views/run-data-table.columns.js +271 -0
- package/dist/node/ui/views/run-list.shared.js +177 -0
- package/dist/node/visualizations/catalog.js +134 -0
- package/dist/node/visualizations/index.js +187 -0
- package/dist/node/visualizations/selectors.js +181 -0
- package/dist/presentations/index.d.ts +3 -5
- package/dist/presentations/index.js +51 -51
- package/dist/proof/index.d.ts +2 -0
- package/dist/proof/meetup-proof.d.ts +10 -0
- package/dist/proof/meetup-proof.runtime.d.ts +22 -0
- package/dist/proof/meetup-proof.scenario.d.ts +2 -0
- package/dist/proof/meetup-proof.suite.d.ts +1 -0
- package/dist/proof/meetup-proof.test.d.ts +1 -0
- package/dist/run/index.d.ts +7 -7
- package/dist/run/index.js +380 -374
- package/dist/run/run.event.js +2 -2
- package/dist/run/run.handler.d.ts +7 -0
- package/dist/run/run.handler.js +666 -1
- package/dist/run/run.presentation.js +2 -2
- package/dist/shared/demo-dashboard-data.d.ts +16 -0
- package/dist/shared/demo-runtime-seed.d.ts +17 -0
- package/dist/shared/demo-runtime.d.ts +8 -0
- package/dist/shared/demo-runtime.test.d.ts +1 -0
- package/dist/shared/index.d.ts +4 -1
- package/dist/shared/index.js +293 -1
- package/dist/shared/mock-runs.d.ts +4 -0
- package/dist/shared/mock-runs.js +5 -0
- package/dist/tool/index.d.ts +7 -7
- package/dist/tool/index.js +161 -161
- package/dist/tool/tool.event.js +1 -1
- package/dist/tool/tool.handler.d.ts +3 -0
- package/dist/tool/tool.handler.js +479 -3
- package/dist/tool/tool.presentation.js +2 -2
- package/dist/ui/AgentDashboard.js +1816 -931
- package/dist/ui/AgentDashboard.sandbox.test.d.ts +1 -0
- package/dist/ui/AgentDashboard.visualizations.d.ts +4 -0
- package/dist/ui/AgentDashboard.visualizations.js +218 -0
- package/dist/ui/AgentRunList.js +360 -128
- package/dist/ui/AgentToolRegistry.js +9 -9
- package/dist/ui/hooks/index.d.ts +4 -4
- package/dist/ui/hooks/index.js +611 -161
- package/dist/ui/hooks/useAgentList.d.ts +5 -0
- package/dist/ui/hooks/useAgentList.js +1 -1
- package/dist/ui/hooks/useAgentMutations.d.ts +9 -2
- package/dist/ui/hooks/useAgentMutations.js +444 -9
- package/dist/ui/hooks/useRunList.d.ts +13 -2
- package/dist/ui/hooks/useRunList.js +26 -11
- package/dist/ui/hooks/useToolList.d.ts +5 -0
- package/dist/ui/hooks/useToolList.js +1 -1
- package/dist/ui/index.d.ts +3 -3
- package/dist/ui/index.js +2161 -1258
- package/dist/ui/modals/AgentActionsModal.js +13 -13
- package/dist/ui/modals/CreateAgentModal.js +15 -15
- package/dist/ui/modals/index.d.ts +1 -1
- package/dist/ui/modals/index.js +297 -297
- package/dist/ui/renderers/agent-list.markdown.d.ts +5 -0
- package/dist/ui/renderers/agent-list.markdown.js +14 -5
- package/dist/ui/renderers/agent-list.renderer.js +7 -7
- package/dist/ui/renderers/dashboard.markdown.d.ts +5 -0
- package/dist/ui/renderers/dashboard.markdown.js +207 -36
- package/dist/ui/renderers/index.d.ts +2 -2
- package/dist/ui/renderers/index.js +359 -163
- package/dist/ui/renderers/run-list.markdown.d.ts +5 -0
- package/dist/ui/renderers/run-list.markdown.js +9 -4
- package/dist/ui/renderers/tool-registry.markdown.d.ts +6 -1
- package/dist/ui/renderers/tool-registry.markdown.js +15 -4
- package/dist/ui/views/AgentListView.js +7 -7
- package/dist/ui/views/RunDataTable.d.ts +18 -0
- package/dist/ui/views/RunDataTable.js +327 -0
- package/dist/ui/views/RunListView.js +360 -128
- package/dist/ui/views/ToolRegistryView.js +9 -9
- package/dist/ui/views/index.js +478 -246
- package/dist/ui/views/run-data-table.columns.d.ts +3 -0
- package/dist/ui/views/run-data-table.columns.js +272 -0
- package/dist/ui/views/run-list.shared.d.ts +14 -0
- package/dist/ui/views/run-list.shared.js +178 -0
- package/dist/visualizations/catalog.d.ts +10 -0
- package/dist/visualizations/catalog.js +135 -0
- package/dist/visualizations/index.d.ts +2 -0
- package/dist/visualizations/index.js +188 -0
- package/dist/visualizations/selectors.d.ts +3 -0
- package/dist/visualizations/selectors.js +182 -0
- package/dist/visualizations/selectors.test.d.ts +1 -0
- package/package.json +114 -12
- package/proofs/agent-console-meetup.replay.json +220 -0
- package/src/agent/agent.entity.ts +111 -111
- package/src/agent/agent.enum.ts +12 -12
- package/src/agent/agent.event.ts +91 -91
- package/src/agent/agent.handler.ts +144 -127
- package/src/agent/agent.operation.ts +400 -400
- package/src/agent/agent.presentation.ts +62 -62
- package/src/agent/agent.schema.ts +175 -175
- package/src/agent/agent.test-spec.ts +48 -48
- package/src/agent/index.ts +46 -51
- package/src/agent.capability.ts +11 -11
- package/src/agent.feature.ts +134 -131
- package/src/docs/agent-console.docblock.ts +52 -49
- package/src/example.test.ts +75 -0
- package/src/example.ts +34 -35
- package/src/handlers/agent.handlers.ts +576 -522
- package/src/handlers/index.ts +30 -14
- package/src/handlers/mock-handlers.test.ts +77 -0
- package/src/index.ts +10 -9
- package/src/presentations/index.ts +11 -13
- package/src/proof/index.ts +2 -0
- package/src/proof/meetup-proof.runtime.ts +196 -0
- package/src/proof/meetup-proof.scenario.ts +99 -0
- package/src/proof/meetup-proof.suite.ts +29 -0
- package/src/proof/meetup-proof.test.ts +28 -0
- package/src/proof/meetup-proof.ts +130 -0
- package/src/run/index.ts +49 -54
- package/src/run/run.entity.ts +137 -137
- package/src/run/run.enum.ts +18 -18
- package/src/run/run.event.ts +174 -174
- package/src/run/run.handler.ts +113 -96
- package/src/run/run.operation.ts +474 -474
- package/src/run/run.presentation.ts +42 -42
- package/src/run/run.schema.ts +126 -126
- package/src/run/run.test-spec.ts +48 -48
- package/src/seeders/index.ts +21 -21
- package/src/shared/demo-dashboard-data.ts +58 -0
- package/src/shared/demo-runtime-seed.ts +139 -0
- package/src/shared/demo-runtime.test.ts +169 -0
- package/src/shared/demo-runtime.ts +260 -0
- package/src/shared/index.ts +12 -1
- package/src/shared/mock-agents.ts +76 -76
- package/src/shared/mock-runs.ts +107 -102
- package/src/shared/mock-tools.ts +140 -140
- package/src/shared/overlay-types.ts +23 -23
- package/src/tool/index.ts +39 -44
- package/src/tool/tool.entity.ts +73 -73
- package/src/tool/tool.enum.ts +13 -13
- package/src/tool/tool.event.ts +80 -80
- package/src/tool/tool.handler.ts +124 -107
- package/src/tool/tool.operation.ts +328 -328
- package/src/tool/tool.presentation.ts +43 -43
- package/src/tool/tool.schema.ts +106 -106
- package/src/tool/tool.test-spec.ts +48 -48
- package/src/ui/AgentDashboard.sandbox.test.tsx +312 -0
- package/src/ui/AgentDashboard.tsx +351 -348
- package/src/ui/AgentDashboard.visualizations.tsx +35 -0
- package/src/ui/hooks/index.ts +7 -7
- package/src/ui/hooks/useAgentList.ts +57 -56
- package/src/ui/hooks/useAgentMutations.ts +168 -159
- package/src/ui/hooks/useRunList.ts +90 -57
- package/src/ui/hooks/useToolList.ts +102 -101
- package/src/ui/index.ts +6 -9
- package/src/ui/modals/AgentActionsModal.tsx +262 -262
- package/src/ui/modals/CreateAgentModal.tsx +232 -232
- package/src/ui/modals/index.ts +1 -1
- package/src/ui/overlays/demo-overlays.ts +52 -52
- package/src/ui/renderers/agent-list.markdown.ts +81 -61
- package/src/ui/renderers/agent-list.renderer.tsx +14 -14
- package/src/ui/renderers/dashboard.markdown.ts +135 -139
- package/src/ui/renderers/index.ts +3 -4
- package/src/ui/renderers/run-list.markdown.ts +56 -47
- package/src/ui/renderers/tool-registry.markdown.ts +79 -66
- package/src/ui/views/AgentListView.tsx +90 -90
- package/src/ui/views/RunDataTable.tsx +74 -0
- package/src/ui/views/RunListView.tsx +84 -158
- package/src/ui/views/ToolRegistryView.tsx +113 -113
- package/src/ui/views/run-data-table.columns.tsx +102 -0
- package/src/ui/views/run-list.shared.tsx +139 -0
- package/src/visualizations/catalog.ts +132 -0
- package/src/visualizations/index.ts +2 -0
- package/src/visualizations/selectors.test.ts +12 -0
- package/src/visualizations/selectors.ts +70 -0
- package/tsconfig.json +7 -8
- package/tsdown.config.js +24 -3
|
@@ -3,604 +3,658 @@
|
|
|
3
3
|
*
|
|
4
4
|
* Database-backed handlers for agent management and runs.
|
|
5
5
|
*/
|
|
6
|
+
|
|
6
7
|
import type { DatabasePort } from '@contractspec/lib.runtime-sandbox';
|
|
7
8
|
import { web } from '@contractspec/lib.runtime-sandbox';
|
|
9
|
+
import {
|
|
10
|
+
CreateAgentCommand,
|
|
11
|
+
GetAgentQuery,
|
|
12
|
+
ListAgentsQuery,
|
|
13
|
+
UpdateAgentCommand,
|
|
14
|
+
} from '../agent/agent.operation';
|
|
15
|
+
import {
|
|
16
|
+
ExecuteAgentCommand,
|
|
17
|
+
GetRunMetricsQuery,
|
|
18
|
+
ListRunsQuery,
|
|
19
|
+
} from '../run/run.operation';
|
|
20
|
+
import { ListToolsQuery } from '../tool/tool.operation';
|
|
21
|
+
|
|
8
22
|
const { generateId } = web;
|
|
23
|
+
const RUNTIME_AGENT_HANDLER_CONTRACTS = [
|
|
24
|
+
CreateAgentCommand,
|
|
25
|
+
GetAgentQuery,
|
|
26
|
+
ListAgentsQuery,
|
|
27
|
+
UpdateAgentCommand,
|
|
28
|
+
ExecuteAgentCommand,
|
|
29
|
+
ListToolsQuery,
|
|
30
|
+
ListRunsQuery,
|
|
31
|
+
GetRunMetricsQuery,
|
|
32
|
+
] as const;
|
|
33
|
+
void RUNTIME_AGENT_HANDLER_CONTRACTS;
|
|
9
34
|
|
|
10
35
|
// ============ Types ============
|
|
11
36
|
|
|
12
37
|
export interface Agent {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
38
|
+
id: string;
|
|
39
|
+
projectId: string;
|
|
40
|
+
organizationId: string;
|
|
41
|
+
name: string;
|
|
42
|
+
description?: string;
|
|
43
|
+
modelProvider: string;
|
|
44
|
+
modelName: string;
|
|
45
|
+
systemPrompt?: string;
|
|
46
|
+
temperature: number;
|
|
47
|
+
maxTokens: number;
|
|
48
|
+
status: 'DRAFT' | 'ACTIVE' | 'PAUSED' | 'ARCHIVED';
|
|
49
|
+
createdAt: Date;
|
|
50
|
+
updatedAt: Date;
|
|
26
51
|
}
|
|
27
52
|
|
|
28
53
|
export interface Tool {
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
54
|
+
id: string;
|
|
55
|
+
projectId: string;
|
|
56
|
+
organizationId: string;
|
|
57
|
+
name: string;
|
|
58
|
+
description?: string;
|
|
59
|
+
version: string;
|
|
60
|
+
category:
|
|
61
|
+
| 'RETRIEVAL'
|
|
62
|
+
| 'COMPUTATION'
|
|
63
|
+
| 'COMMUNICATION'
|
|
64
|
+
| 'INTEGRATION'
|
|
65
|
+
| 'UTILITY'
|
|
66
|
+
| 'CUSTOM';
|
|
67
|
+
status: 'ACTIVE' | 'DISABLED' | 'DEPRECATED' | 'DRAFT';
|
|
68
|
+
inputSchema?: string;
|
|
69
|
+
outputSchema?: string;
|
|
70
|
+
endpoint?: string;
|
|
71
|
+
createdAt: Date;
|
|
72
|
+
updatedAt: Date;
|
|
48
73
|
}
|
|
49
74
|
|
|
50
75
|
export interface Run {
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
76
|
+
id: string;
|
|
77
|
+
projectId: string;
|
|
78
|
+
agentId: string;
|
|
79
|
+
agentName?: string;
|
|
80
|
+
status: 'QUEUED' | 'RUNNING' | 'COMPLETED' | 'FAILED' | 'CANCELLED';
|
|
81
|
+
input?: string;
|
|
82
|
+
output?: string;
|
|
83
|
+
totalTokens: number;
|
|
84
|
+
promptTokens: number;
|
|
85
|
+
completionTokens: number;
|
|
86
|
+
estimatedCostUsd: number;
|
|
87
|
+
durationMs?: number;
|
|
88
|
+
errorMessage?: string;
|
|
89
|
+
queuedAt: Date;
|
|
90
|
+
startedAt?: Date;
|
|
91
|
+
completedAt?: Date;
|
|
67
92
|
}
|
|
68
93
|
|
|
69
94
|
export interface RunMetrics {
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
95
|
+
totalRuns: number;
|
|
96
|
+
successRate: number;
|
|
97
|
+
averageDurationMs: number;
|
|
98
|
+
totalTokens: number;
|
|
99
|
+
totalCostUsd: number;
|
|
75
100
|
}
|
|
76
101
|
|
|
77
102
|
export interface ListAgentsInput {
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
103
|
+
projectId: string;
|
|
104
|
+
organizationId?: string;
|
|
105
|
+
status?: Agent['status'] | 'all';
|
|
106
|
+
search?: string;
|
|
107
|
+
limit?: number;
|
|
108
|
+
offset?: number;
|
|
84
109
|
}
|
|
85
110
|
|
|
86
111
|
export interface ListAgentsOutput {
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
112
|
+
items: Agent[];
|
|
113
|
+
total: number;
|
|
114
|
+
hasMore: boolean;
|
|
90
115
|
}
|
|
91
116
|
|
|
92
117
|
export interface ListToolsInput {
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
118
|
+
projectId: string;
|
|
119
|
+
organizationId?: string;
|
|
120
|
+
category?: Tool['category'] | 'all';
|
|
121
|
+
status?: Tool['status'] | 'all';
|
|
122
|
+
search?: string;
|
|
123
|
+
limit?: number;
|
|
124
|
+
offset?: number;
|
|
100
125
|
}
|
|
101
126
|
|
|
102
127
|
export interface ListToolsOutput {
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
128
|
+
items: Tool[];
|
|
129
|
+
total: number;
|
|
130
|
+
hasMore: boolean;
|
|
106
131
|
}
|
|
107
132
|
|
|
108
133
|
export interface ListRunsInput {
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
134
|
+
projectId: string;
|
|
135
|
+
organizationId?: string;
|
|
136
|
+
agentId?: string;
|
|
137
|
+
status?: Run['status'] | 'all';
|
|
138
|
+
sortBy?:
|
|
139
|
+
| 'queuedAt'
|
|
140
|
+
| 'totalTokens'
|
|
141
|
+
| 'durationMs'
|
|
142
|
+
| 'estimatedCostUsd'
|
|
143
|
+
| 'status'
|
|
144
|
+
| 'agentName';
|
|
145
|
+
sortDirection?: 'asc' | 'desc';
|
|
146
|
+
limit?: number;
|
|
147
|
+
offset?: number;
|
|
115
148
|
}
|
|
116
149
|
|
|
117
150
|
export interface ListRunsOutput {
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
151
|
+
items: Run[];
|
|
152
|
+
total: number;
|
|
153
|
+
hasMore: boolean;
|
|
121
154
|
}
|
|
122
155
|
|
|
123
156
|
export interface CreateAgentInput {
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
157
|
+
name: string;
|
|
158
|
+
description?: string;
|
|
159
|
+
modelProvider?: string;
|
|
160
|
+
modelName?: string;
|
|
161
|
+
systemPrompt?: string;
|
|
162
|
+
temperature?: number;
|
|
163
|
+
maxTokens?: number;
|
|
131
164
|
}
|
|
132
165
|
|
|
133
166
|
export interface UpdateAgentInput {
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
167
|
+
id: string;
|
|
168
|
+
name?: string;
|
|
169
|
+
description?: string;
|
|
170
|
+
status?: Agent['status'];
|
|
138
171
|
}
|
|
139
172
|
|
|
140
173
|
// ============ Row Types ============
|
|
141
174
|
|
|
142
175
|
interface AgentRow extends Record<string, unknown> {
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
176
|
+
id: string;
|
|
177
|
+
projectId: string;
|
|
178
|
+
organizationId: string;
|
|
179
|
+
name: string;
|
|
180
|
+
description: string | null;
|
|
181
|
+
modelProvider: string;
|
|
182
|
+
modelName: string;
|
|
183
|
+
systemPrompt: string | null;
|
|
184
|
+
temperature: number;
|
|
185
|
+
maxTokens: number;
|
|
186
|
+
status: string;
|
|
187
|
+
createdAt: string;
|
|
188
|
+
updatedAt: string;
|
|
156
189
|
}
|
|
157
190
|
|
|
158
191
|
interface ToolRow extends Record<string, unknown> {
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
192
|
+
id: string;
|
|
193
|
+
projectId: string;
|
|
194
|
+
organizationId: string;
|
|
195
|
+
name: string;
|
|
196
|
+
description: string | null;
|
|
197
|
+
version: string;
|
|
198
|
+
category: string;
|
|
199
|
+
status: string;
|
|
200
|
+
inputSchema: string | null;
|
|
201
|
+
outputSchema: string | null;
|
|
202
|
+
endpoint: string | null;
|
|
203
|
+
createdAt: string;
|
|
204
|
+
updatedAt: string;
|
|
172
205
|
}
|
|
173
206
|
|
|
174
207
|
interface RunRow extends Record<string, unknown> {
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
208
|
+
id: string;
|
|
209
|
+
projectId: string;
|
|
210
|
+
agentId: string;
|
|
211
|
+
status: string;
|
|
212
|
+
input: string | null;
|
|
213
|
+
output: string | null;
|
|
214
|
+
totalTokens: number;
|
|
215
|
+
promptTokens: number;
|
|
216
|
+
completionTokens: number;
|
|
217
|
+
estimatedCostUsd: number;
|
|
218
|
+
durationMs: number | null;
|
|
219
|
+
errorMessage: string | null;
|
|
220
|
+
queuedAt: string;
|
|
221
|
+
startedAt: string | null;
|
|
222
|
+
completedAt: string | null;
|
|
190
223
|
}
|
|
191
224
|
|
|
225
|
+
const RUN_SORT_COLUMNS = {
|
|
226
|
+
queuedAt: 'r.queuedAt',
|
|
227
|
+
totalTokens: 'r.totalTokens',
|
|
228
|
+
durationMs: 'r.durationMs',
|
|
229
|
+
estimatedCostUsd: 'r.estimatedCostUsd',
|
|
230
|
+
status: 'r.status',
|
|
231
|
+
agentName: 'a.name',
|
|
232
|
+
} as const;
|
|
233
|
+
|
|
192
234
|
function rowToAgent(row: AgentRow): Agent {
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
235
|
+
return {
|
|
236
|
+
id: row.id,
|
|
237
|
+
projectId: row.projectId,
|
|
238
|
+
organizationId: row.organizationId,
|
|
239
|
+
name: row.name,
|
|
240
|
+
description: row.description ?? undefined,
|
|
241
|
+
modelProvider: row.modelProvider,
|
|
242
|
+
modelName: row.modelName,
|
|
243
|
+
systemPrompt: row.systemPrompt ?? undefined,
|
|
244
|
+
temperature: row.temperature,
|
|
245
|
+
maxTokens: row.maxTokens,
|
|
246
|
+
status: row.status as Agent['status'],
|
|
247
|
+
createdAt: new Date(row.createdAt),
|
|
248
|
+
updatedAt: new Date(row.updatedAt),
|
|
249
|
+
};
|
|
208
250
|
}
|
|
209
251
|
|
|
210
252
|
function rowToTool(row: ToolRow): Tool {
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
253
|
+
return {
|
|
254
|
+
id: row.id,
|
|
255
|
+
projectId: row.projectId,
|
|
256
|
+
organizationId: row.organizationId,
|
|
257
|
+
name: row.name,
|
|
258
|
+
description: row.description ?? undefined,
|
|
259
|
+
version: row.version,
|
|
260
|
+
category: row.category as Tool['category'],
|
|
261
|
+
status: row.status as Tool['status'],
|
|
262
|
+
inputSchema: row.inputSchema ?? undefined,
|
|
263
|
+
outputSchema: row.outputSchema ?? undefined,
|
|
264
|
+
endpoint: row.endpoint ?? undefined,
|
|
265
|
+
createdAt: new Date(row.createdAt),
|
|
266
|
+
updatedAt: new Date(row.updatedAt),
|
|
267
|
+
};
|
|
226
268
|
}
|
|
227
269
|
|
|
228
270
|
function rowToRun(row: RunRow, agentName?: string): Run {
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
271
|
+
return {
|
|
272
|
+
id: row.id,
|
|
273
|
+
projectId: row.projectId,
|
|
274
|
+
agentId: row.agentId,
|
|
275
|
+
agentName,
|
|
276
|
+
status: row.status as Run['status'],
|
|
277
|
+
input: row.input ?? undefined,
|
|
278
|
+
output: row.output ?? undefined,
|
|
279
|
+
totalTokens: row.totalTokens,
|
|
280
|
+
promptTokens: row.promptTokens,
|
|
281
|
+
completionTokens: row.completionTokens,
|
|
282
|
+
estimatedCostUsd: row.estimatedCostUsd,
|
|
283
|
+
durationMs: row.durationMs ?? undefined,
|
|
284
|
+
errorMessage: row.errorMessage ?? undefined,
|
|
285
|
+
queuedAt: new Date(row.queuedAt),
|
|
286
|
+
startedAt: row.startedAt ? new Date(row.startedAt) : undefined,
|
|
287
|
+
completedAt: row.completedAt ? new Date(row.completedAt) : undefined,
|
|
288
|
+
};
|
|
247
289
|
}
|
|
248
290
|
|
|
249
291
|
// ============ Handler Factory ============
|
|
250
292
|
|
|
251
293
|
export function createAgentHandlers(db: DatabasePort) {
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
294
|
+
/**
|
|
295
|
+
* List agents
|
|
296
|
+
*/
|
|
297
|
+
async function listAgents(input: ListAgentsInput): Promise<ListAgentsOutput> {
|
|
298
|
+
const {
|
|
299
|
+
projectId,
|
|
300
|
+
organizationId,
|
|
301
|
+
status,
|
|
302
|
+
search,
|
|
303
|
+
limit = 20,
|
|
304
|
+
offset = 0,
|
|
305
|
+
} = input;
|
|
306
|
+
|
|
307
|
+
let whereClause = 'WHERE projectId = ?';
|
|
308
|
+
const params: (string | number)[] = [projectId];
|
|
309
|
+
|
|
310
|
+
if (organizationId) {
|
|
311
|
+
whereClause += ' AND organizationId = ?';
|
|
312
|
+
params.push(organizationId);
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
if (status && status !== 'all') {
|
|
316
|
+
whereClause += ' AND status = ?';
|
|
317
|
+
params.push(status);
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
if (search) {
|
|
321
|
+
whereClause += ' AND (name LIKE ? OR description LIKE ?)';
|
|
322
|
+
params.push(`%${search}%`, `%${search}%`);
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
const countResult = (
|
|
326
|
+
await db.query(
|
|
327
|
+
`SELECT COUNT(*) as count FROM agent_definition ${whereClause}`,
|
|
328
|
+
params
|
|
329
|
+
)
|
|
330
|
+
).rows as unknown as { count: number }[];
|
|
331
|
+
const total = countResult[0]?.count ?? 0;
|
|
332
|
+
|
|
333
|
+
const rows = (
|
|
334
|
+
await db.query(
|
|
335
|
+
`SELECT * FROM agent_definition ${whereClause} ORDER BY createdAt DESC LIMIT ? OFFSET ?`,
|
|
336
|
+
[...params, limit, offset]
|
|
337
|
+
)
|
|
338
|
+
).rows as unknown as AgentRow[];
|
|
339
|
+
|
|
340
|
+
return {
|
|
341
|
+
items: rows.map(rowToAgent),
|
|
342
|
+
total,
|
|
343
|
+
hasMore: offset + rows.length < total,
|
|
344
|
+
};
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
/**
|
|
348
|
+
* Get a single agent
|
|
349
|
+
*/
|
|
350
|
+
async function getAgent(id: string): Promise<Agent | null> {
|
|
351
|
+
const rows = (
|
|
352
|
+
await db.query(`SELECT * FROM agent_definition WHERE id = ?`, [id])
|
|
353
|
+
).rows as unknown as AgentRow[];
|
|
354
|
+
return rows[0] ? rowToAgent(rows[0]) : null;
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
/**
|
|
358
|
+
* Create an agent
|
|
359
|
+
*/
|
|
360
|
+
async function createAgent(
|
|
361
|
+
input: CreateAgentInput,
|
|
362
|
+
context: { projectId: string; organizationId: string }
|
|
363
|
+
): Promise<Agent> {
|
|
364
|
+
const id = generateId('agent');
|
|
365
|
+
const now = new Date().toISOString();
|
|
366
|
+
|
|
367
|
+
await db.execute(
|
|
368
|
+
`INSERT INTO agent_definition (id, projectId, organizationId, name, description, modelProvider, modelName, systemPrompt, temperature, maxTokens, status, createdAt, updatedAt)
|
|
327
369
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
370
|
+
[
|
|
371
|
+
id,
|
|
372
|
+
context.projectId,
|
|
373
|
+
context.organizationId,
|
|
374
|
+
input.name,
|
|
375
|
+
input.description ?? null,
|
|
376
|
+
input.modelProvider ?? 'openai',
|
|
377
|
+
input.modelName ?? 'gpt-4',
|
|
378
|
+
input.systemPrompt ?? null,
|
|
379
|
+
input.temperature ?? 0.7,
|
|
380
|
+
input.maxTokens ?? 4096,
|
|
381
|
+
'DRAFT',
|
|
382
|
+
now,
|
|
383
|
+
now,
|
|
384
|
+
]
|
|
385
|
+
);
|
|
386
|
+
|
|
387
|
+
const rows = (
|
|
388
|
+
await db.query(`SELECT * FROM agent_definition WHERE id = ?`, [id])
|
|
389
|
+
).rows as unknown as AgentRow[];
|
|
390
|
+
|
|
391
|
+
const row = rows[0];
|
|
392
|
+
if (!row) {
|
|
393
|
+
throw new Error('Failed to retrieve created agent');
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
return rowToAgent(row);
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
/**
|
|
400
|
+
* Update an agent
|
|
401
|
+
*/
|
|
402
|
+
async function updateAgent(input: UpdateAgentInput): Promise<Agent> {
|
|
403
|
+
const now = new Date().toISOString();
|
|
404
|
+
const updates: string[] = ['updatedAt = ?'];
|
|
405
|
+
const params: (string | null)[] = [now];
|
|
406
|
+
|
|
407
|
+
if (input.name !== undefined) {
|
|
408
|
+
updates.push('name = ?');
|
|
409
|
+
params.push(input.name);
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
if (input.description !== undefined) {
|
|
413
|
+
updates.push('description = ?');
|
|
414
|
+
params.push(input.description);
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
if (input.status !== undefined) {
|
|
418
|
+
updates.push('status = ?');
|
|
419
|
+
params.push(input.status);
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
params.push(input.id);
|
|
423
|
+
|
|
424
|
+
await db.execute(
|
|
425
|
+
`UPDATE agent_definition SET ${updates.join(', ')} WHERE id = ?`,
|
|
426
|
+
params
|
|
427
|
+
);
|
|
428
|
+
|
|
429
|
+
const rows = (
|
|
430
|
+
await db.query(`SELECT * FROM agent_definition WHERE id = ?`, [input.id])
|
|
431
|
+
).rows as unknown as AgentRow[];
|
|
432
|
+
|
|
433
|
+
if (!rows[0]) {
|
|
434
|
+
throw new Error('NOT_FOUND');
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
return rowToAgent(rows[0]);
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
/**
|
|
441
|
+
* List tools
|
|
442
|
+
*/
|
|
443
|
+
async function listTools(input: ListToolsInput): Promise<ListToolsOutput> {
|
|
444
|
+
const {
|
|
445
|
+
projectId,
|
|
446
|
+
organizationId,
|
|
447
|
+
category,
|
|
448
|
+
status,
|
|
449
|
+
search,
|
|
450
|
+
limit = 50,
|
|
451
|
+
offset = 0,
|
|
452
|
+
} = input;
|
|
453
|
+
|
|
454
|
+
let whereClause = 'WHERE projectId = ?';
|
|
455
|
+
const params: (string | number)[] = [projectId];
|
|
456
|
+
|
|
457
|
+
if (organizationId) {
|
|
458
|
+
whereClause += ' AND organizationId = ?';
|
|
459
|
+
params.push(organizationId);
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
if (category && category !== 'all') {
|
|
463
|
+
whereClause += ' AND category = ?';
|
|
464
|
+
params.push(category);
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
if (status && status !== 'all') {
|
|
468
|
+
whereClause += ' AND status = ?';
|
|
469
|
+
params.push(status);
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
if (search) {
|
|
473
|
+
whereClause += ' AND (name LIKE ? OR description LIKE ?)';
|
|
474
|
+
params.push(`%${search}%`, `%${search}%`);
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
const countResult = (
|
|
478
|
+
await db.query(
|
|
479
|
+
`SELECT COUNT(*) as count FROM agent_tool ${whereClause}`,
|
|
480
|
+
params
|
|
481
|
+
)
|
|
482
|
+
).rows as unknown as { count: number }[];
|
|
483
|
+
const total = countResult[0]?.count ?? 0;
|
|
484
|
+
|
|
485
|
+
const rows = (
|
|
486
|
+
await db.query(
|
|
487
|
+
`SELECT * FROM agent_tool ${whereClause} ORDER BY name ASC LIMIT ? OFFSET ?`,
|
|
488
|
+
[...params, limit, offset]
|
|
489
|
+
)
|
|
490
|
+
).rows as unknown as ToolRow[];
|
|
491
|
+
|
|
492
|
+
return {
|
|
493
|
+
items: rows.map(rowToTool),
|
|
494
|
+
total,
|
|
495
|
+
hasMore: offset + rows.length < total,
|
|
496
|
+
};
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
/**
|
|
500
|
+
* List runs
|
|
501
|
+
*/
|
|
502
|
+
async function listRuns(input: ListRunsInput): Promise<ListRunsOutput> {
|
|
503
|
+
const {
|
|
504
|
+
projectId,
|
|
505
|
+
agentId,
|
|
506
|
+
status,
|
|
507
|
+
sortBy,
|
|
508
|
+
sortDirection = 'desc',
|
|
509
|
+
limit = 20,
|
|
510
|
+
offset = 0,
|
|
511
|
+
} = input;
|
|
512
|
+
|
|
513
|
+
let whereClause = 'WHERE r.projectId = ?';
|
|
514
|
+
const params: (string | number)[] = [projectId];
|
|
515
|
+
|
|
516
|
+
if (agentId) {
|
|
517
|
+
whereClause += ' AND r.agentId = ?';
|
|
518
|
+
params.push(agentId);
|
|
519
|
+
}
|
|
520
|
+
|
|
521
|
+
if (status && status !== 'all') {
|
|
522
|
+
whereClause += ' AND r.status = ?';
|
|
523
|
+
params.push(status);
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
const countResult = (
|
|
527
|
+
await db.query(
|
|
528
|
+
`SELECT COUNT(*) as count FROM agent_run r ${whereClause}`,
|
|
529
|
+
params
|
|
530
|
+
)
|
|
531
|
+
).rows as unknown as { count: number }[];
|
|
532
|
+
const total = countResult[0]?.count ?? 0;
|
|
533
|
+
const sortColumn = sortBy
|
|
534
|
+
? RUN_SORT_COLUMNS[sortBy]
|
|
535
|
+
: RUN_SORT_COLUMNS.queuedAt;
|
|
536
|
+
const direction = sortDirection === 'asc' ? 'ASC' : 'DESC';
|
|
537
|
+
|
|
538
|
+
const rows = (
|
|
539
|
+
await db.query(
|
|
540
|
+
`SELECT r.*, a.name as agentName
|
|
487
541
|
FROM agent_run r
|
|
488
542
|
LEFT JOIN agent_definition a ON r.agentId = a.id
|
|
489
543
|
${whereClause}
|
|
490
|
-
ORDER BY r.queuedAt DESC LIMIT ? OFFSET ?`,
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
544
|
+
ORDER BY ${sortColumn} ${direction}, r.queuedAt DESC LIMIT ? OFFSET ?`,
|
|
545
|
+
[...params, limit, offset]
|
|
546
|
+
)
|
|
547
|
+
).rows as unknown as (RunRow & { agentName: string })[];
|
|
548
|
+
|
|
549
|
+
return {
|
|
550
|
+
items: rows.map((r) => rowToRun(r, r.agentName)),
|
|
551
|
+
total,
|
|
552
|
+
hasMore: offset + rows.length < total,
|
|
553
|
+
};
|
|
554
|
+
}
|
|
555
|
+
|
|
556
|
+
/**
|
|
557
|
+
* Get run metrics
|
|
558
|
+
*/
|
|
559
|
+
async function getRunMetrics(input: {
|
|
560
|
+
projectId: string;
|
|
561
|
+
agentId?: string;
|
|
562
|
+
startDate?: Date;
|
|
563
|
+
endDate?: Date;
|
|
564
|
+
}): Promise<RunMetrics> {
|
|
565
|
+
let whereClause = 'WHERE projectId = ?';
|
|
566
|
+
const params: (string | number)[] = [input.projectId];
|
|
567
|
+
|
|
568
|
+
if (input.agentId) {
|
|
569
|
+
whereClause += ' AND agentId = ?';
|
|
570
|
+
params.push(input.agentId);
|
|
571
|
+
}
|
|
572
|
+
|
|
573
|
+
if (input.startDate) {
|
|
574
|
+
whereClause += ' AND queuedAt >= ?';
|
|
575
|
+
params.push(input.startDate.toISOString());
|
|
576
|
+
}
|
|
577
|
+
|
|
578
|
+
if (input.endDate) {
|
|
579
|
+
whereClause += ' AND queuedAt <= ?';
|
|
580
|
+
params.push(input.endDate.toISOString());
|
|
581
|
+
}
|
|
582
|
+
|
|
583
|
+
const result = (
|
|
584
|
+
await db.query(
|
|
585
|
+
`SELECT
|
|
532
586
|
COUNT(*) as totalRuns,
|
|
533
587
|
SUM(CASE WHEN status = 'COMPLETED' THEN 1 ELSE 0 END) as completedRuns,
|
|
534
588
|
AVG(CASE WHEN status = 'COMPLETED' THEN durationMs ELSE NULL END) as avgDuration,
|
|
535
589
|
COALESCE(SUM(totalTokens), 0) as totalTokens,
|
|
536
590
|
COALESCE(SUM(estimatedCostUsd), 0) as totalCost
|
|
537
591
|
FROM agent_run ${whereClause}`,
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
592
|
+
params
|
|
593
|
+
)
|
|
594
|
+
).rows as unknown as {
|
|
595
|
+
totalRuns: number;
|
|
596
|
+
completedRuns: number;
|
|
597
|
+
avgDuration: number | null;
|
|
598
|
+
totalTokens: number;
|
|
599
|
+
totalCost: number;
|
|
600
|
+
}[];
|
|
601
|
+
|
|
602
|
+
const data = result[0];
|
|
603
|
+
const totalRuns = data?.totalRuns ?? 0;
|
|
604
|
+
const completedRuns = data?.completedRuns ?? 0;
|
|
605
|
+
|
|
606
|
+
return {
|
|
607
|
+
totalRuns,
|
|
608
|
+
successRate: totalRuns > 0 ? completedRuns / totalRuns : 0,
|
|
609
|
+
averageDurationMs: data?.avgDuration ?? 0,
|
|
610
|
+
totalTokens: data?.totalTokens ?? 0,
|
|
611
|
+
totalCostUsd: data?.totalCost ?? 0,
|
|
612
|
+
};
|
|
613
|
+
}
|
|
614
|
+
|
|
615
|
+
/**
|
|
616
|
+
* Execute an agent (create a run and queue it).
|
|
617
|
+
*/
|
|
618
|
+
async function executeAgent(input: {
|
|
619
|
+
agentId: string;
|
|
620
|
+
message: string;
|
|
621
|
+
context?: { projectId: string; organizationId: string };
|
|
622
|
+
}): Promise<Run> {
|
|
623
|
+
const agent = await getAgent(input.agentId);
|
|
624
|
+
if (!agent) throw new Error('AGENT_NOT_FOUND');
|
|
625
|
+
if (agent.status !== 'ACTIVE') throw new Error('AGENT_NOT_ACTIVE');
|
|
626
|
+
|
|
627
|
+
const id = generateId('run');
|
|
628
|
+
const now = new Date().toISOString();
|
|
629
|
+
const pid = input.context?.projectId ?? agent.projectId;
|
|
630
|
+
|
|
631
|
+
await db.execute(
|
|
632
|
+
`INSERT INTO agent_run (id, projectId, agentId, status, input, totalTokens, promptTokens, completionTokens, estimatedCostUsd, queuedAt)
|
|
579
633
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
634
|
+
[id, pid, input.agentId, 'QUEUED', input.message, 0, 0, 0, 0, now]
|
|
635
|
+
);
|
|
636
|
+
|
|
637
|
+
const rows = (
|
|
638
|
+
await db.query(
|
|
639
|
+
`SELECT r.*, a.name as agentName FROM agent_run r LEFT JOIN agent_definition a ON r.agentId = a.id WHERE r.id = ?`,
|
|
640
|
+
[id]
|
|
641
|
+
)
|
|
642
|
+
).rows as unknown as (RunRow & { agentName: string })[];
|
|
643
|
+
|
|
644
|
+
if (!rows[0]) throw new Error('Failed to retrieve created run');
|
|
645
|
+
return rowToRun(rows[0], rows[0].agentName);
|
|
646
|
+
}
|
|
647
|
+
|
|
648
|
+
return {
|
|
649
|
+
listAgents,
|
|
650
|
+
getAgent,
|
|
651
|
+
createAgent,
|
|
652
|
+
updateAgent,
|
|
653
|
+
executeAgent,
|
|
654
|
+
listTools,
|
|
655
|
+
listRuns,
|
|
656
|
+
getRunMetrics,
|
|
657
|
+
};
|
|
604
658
|
}
|
|
605
659
|
|
|
606
660
|
export type AgentHandlers = ReturnType<typeof createAgentHandlers>;
|