-
Notifications
You must be signed in to change notification settings - Fork 519
Expand file tree
/
Copy pathagent-db.ts
More file actions
109 lines (98 loc) · 3.18 KB
/
agent-db.ts
File metadata and controls
109 lines (98 loc) · 3.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import { validateSingleAgent } from '@codebuff/common/templates/agent-validation'
import db from '@codebuff/internal/db'
import * as schema from '@codebuff/internal/db/schema'
import { and, desc, eq } from 'drizzle-orm'
import type { FetchAgentFromDatabaseFn } from '@codebuff/common/types/contracts/database'
import type { DynamicAgentTemplate } from '@codebuff/common/types/dynamic-agent-template'
import type { ParamsOf } from '@codebuff/common/types/function-params'
/**
* Fetch and validate an agent from the database by publisher/agent-id[@version] format
*/
export async function fetchAgentFromDatabase(
params: ParamsOf<FetchAgentFromDatabaseFn>,
): ReturnType<FetchAgentFromDatabaseFn> {
const { parsedAgentId, logger } = params
const { publisherId, agentId, version } = parsedAgentId
try {
let agentConfig
if (version && version !== 'latest') {
// Query for specific version
agentConfig = await db
.select()
.from(schema.agentConfig)
.where(
and(
eq(schema.agentConfig.id, agentId),
eq(schema.agentConfig.publisher_id, publisherId),
eq(schema.agentConfig.version, version),
),
)
.then((rows) => rows[0])
} else {
// Query for latest version
agentConfig = await db
.select()
.from(schema.agentConfig)
.where(
and(
eq(schema.agentConfig.id, agentId),
eq(schema.agentConfig.publisher_id, publisherId),
),
)
.orderBy(
desc(schema.agentConfig.major),
desc(schema.agentConfig.minor),
desc(schema.agentConfig.patch),
)
.limit(1)
.then((rows) => rows[0])
}
if (!agentConfig) {
logger.debug(
{ publisherId, agentId, version },
'fetchAgentFromDatabase: Agent not found in database',
)
return null
}
const rawAgentData = agentConfig.data as DynamicAgentTemplate
// Validate the raw agent data with the original agentId (not full identifier)
const validationResult = validateSingleAgent({
template: { ...rawAgentData, id: agentId, version: agentConfig.version },
filePath: `${publisherId}/${agentId}@${agentConfig.version}`,
})
if (!validationResult.success) {
logger.error(
{
publisherId,
agentId,
version: agentConfig.version,
error: validationResult.error,
},
'fetchAgentFromDatabase: Agent validation failed',
)
return null
}
// Set the correct full agent ID for the final template
const agentTemplate = {
...validationResult.agentTemplate!,
id: `${publisherId}/${agentId}@${agentConfig.version}`,
}
logger.debug(
{
publisherId,
agentId,
version: agentConfig.version,
fullAgentId: agentTemplate.id,
parsedAgentId,
},
'fetchAgentFromDatabase: Successfully loaded and validated agent from database',
)
return agentTemplate
} catch (error) {
logger.error(
{ publisherId, agentId, version, error },
'fetchAgentFromDatabase: Error fetching agent from database',
)
return null
}
}