Skip to content

Commit bb7748c

Browse files
committed
Move DynamicAgentService and AgentTemplate to common
1 parent 772719e commit bb7748c

File tree

7 files changed

+81
-73
lines changed

7 files changed

+81
-73
lines changed

backend/src/templates/agent-registry.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import type { DynamicAgentValidationError } from './dynamic-agent-service'
2-
import type { AgentTemplate } from './types'
1+
import type { DynamicAgentValidationError } from '@codebuff/common/templates/dynamic-agent-service'
2+
import { dynamicAgentService } from '@codebuff/common/templates/dynamic-agent-service'
3+
import type { AgentTemplate } from '@codebuff/common/types/agent-template'
34

45
import { ProjectFileContext } from '@codebuff/common/util/file'
56
import { logger } from '../util/logger'
67
import { agentTemplates as staticTemplates } from './agent-list'
7-
import { dynamicAgentService } from './dynamic-agent-service'
88

99
export type AgentRegistry = Record<string, AgentTemplate>
1010

backend/src/templates/types.ts

Lines changed: 8 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,15 @@
1-
import type { Model } from '@codebuff/common/constants'
2-
import type { ToolName } from '@codebuff/common/tools/constants'
31
import type {
4-
AgentState,
5-
AgentTemplateType,
6-
ToolResult,
7-
} from '@codebuff/common/types/session-state'
8-
import type { z } from 'zod/v4'
9-
import type { CodebuffToolCall } from '../tools/constants'
2+
AgentTemplate,
3+
StepGenerator,
4+
StepHandler,
5+
} from '@codebuff/common/types/agent-template'
6+
import type { AgentTemplateType } from '@codebuff/common/types/session-state'
7+
import type { ToolName } from '@codebuff/common/tools/constants'
108

119
import { AgentTemplateTypes } from '@codebuff/common/types/session-state'
1210

13-
export type AgentTemplate<
14-
P = string | undefined,
15-
T = Record<string, any> | undefined,
16-
> = {
17-
id: AgentTemplateType
18-
displayName: string
19-
model: Model
20-
21-
toolNames: ToolName[]
22-
subagents: AgentTemplateType[]
23-
24-
parentPrompt?: string
25-
systemPrompt: string
26-
instructionsPrompt: string
27-
stepPrompt: string
28-
parentInstructions?: Record<string, string>
29-
30-
// Required parameters for spawning this agent.
31-
inputSchema: {
32-
prompt?: z.ZodSchema<P>
33-
params?: z.ZodSchema<T>
34-
}
35-
includeMessageHistory: boolean
36-
outputMode: 'last_message' | 'all_messages' | 'json'
37-
outputSchema?: z.ZodSchema<any>
38-
39-
handleSteps?: StepHandler<P, T> | string // Function or string of the generator code for running in a sandbox
40-
}
41-
42-
export type StepGenerator = Generator<
43-
Omit<CodebuffToolCall, 'toolCallId'> | 'STEP' | 'STEP_ALL',
44-
void,
45-
{ agentState: AgentState; toolResult: ToolResult | undefined }
46-
>
47-
48-
export type StepHandler<
49-
P = string | undefined,
50-
T = Record<string, any> | undefined,
51-
> = (params: { agentState: AgentState; prompt: P; params: T }) => StepGenerator
11+
// Re-export for backward compatibility
12+
export type { AgentTemplate, StepGenerator, StepHandler }
5213

5314
const placeholderNames = [
5415
'AGENT_NAME',

backend/src/__tests__/dynamic-agent-loader.test.ts renamed to common/src/__tests__/dynamic-agent-loader.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import {
22
clearMockedModules,
33
mockModule,
4-
} from '@codebuff/common/testing/mock-modules'
5-
import { ProjectFileContext } from '@codebuff/common/util/file'
4+
} from '../testing/mock-modules'
5+
import { ProjectFileContext } from '../util/file'
66
import { afterAll, beforeAll, describe, expect, it } from 'bun:test'
77
import {
88
dynamicAgentService,
@@ -316,4 +316,4 @@ describe('Dynamic Agent Loader', () => {
316316
'git-committer' // Normalized without prefix
317317
)
318318
})
319-
})
319+
})

backend/src/__tests__/dynamic-agent-schema-validation.test.ts renamed to common/src/__tests__/dynamic-agent-schema-validation.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import {
22
clearMockedModules,
33
mockModule,
4-
} from '@codebuff/common/testing/mock-modules'
4+
} from '../testing/mock-modules'
55
import {
66
getStubProjectFileContext,
77
ProjectFileContext,
8-
} from '@codebuff/common/util/file'
8+
} from '../util/file'
99
import { afterAll, beforeAll, beforeEach, describe, expect, it } from 'bun:test'
1010
import { DynamicAgentService } from '../templates/dynamic-agent-service'
1111

@@ -15,7 +15,7 @@ describe('Dynamic Agent Schema Validation', () => {
1515

1616
beforeAll(() => {
1717
// Mock logger to avoid console output during tests
18-
mockModule('@codebuff/backend/util/logger', () => ({
18+
mockModule('../util/logger', () => ({
1919
logger: {
2020
debug: () => {},
2121
warn: () => {},
@@ -382,4 +382,4 @@ describe('Dynamic Agent Schema Validation', () => {
382382
).toBeUndefined()
383383
})
384384
})
385-
})
385+
})

backend/src/__tests__/handlesteps-parsing.test.ts renamed to common/src/__tests__/handlesteps-parsing.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import {
22
DynamicAgentConfigSchema,
33
DynamicAgentTemplate,
4-
} from '@codebuff/common/types/dynamic-agent-template'
5-
import { AgentState } from '@codebuff/common/types/session-state'
6-
import { ProjectFileContext } from '@codebuff/common/util/file'
4+
} from '../types/dynamic-agent-template'
5+
import { AgentState } from '../types/session-state'
6+
import { ProjectFileContext } from '../util/file'
77
import { afterEach, beforeEach, describe, expect, test } from 'bun:test'
88
import { dynamicAgentService } from '../templates/dynamic-agent-service'
99

@@ -135,7 +135,7 @@ describe('handleSteps Parsing Tests', () => {
135135
test('should require set_output tool for handleSteps with json output mode', () => {
136136
const {
137137
DynamicAgentTemplateSchema,
138-
} = require('@codebuff/common/types/dynamic-agent-template')
138+
} = require('../types/dynamic-agent-template')
139139

140140
const agentConfig = {
141141
id: 'test-agent',
@@ -226,4 +226,4 @@ describe('handleSteps Parsing Tests', () => {
226226
expect(result.templates['test-agent'].handleSteps).toBe(expectedStringified)
227227
expect(typeof result.templates['test-agent'].handleSteps).toBe('string')
228228
})
229-
})
229+
})

backend/src/templates/dynamic-agent-service.ts renamed to common/src/templates/dynamic-agent-service.ts

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,20 @@
11
import * as path from 'path'
22

3-
import { ToolName } from '@codebuff/common/tools/constants'
4-
import { DynamicAgentTemplate } from '@codebuff/common/types/dynamic-agent-template'
5-
import { AgentTemplateType } from '@codebuff/common/types/session-state'
6-
import { filterValidAgentTemplates } from '@codebuff/common/util/agent-file-utils'
7-
import { normalizeAgentNames } from '@codebuff/common/util/agent-name-normalization'
3+
import { ToolName } from '../tools/constants'
4+
import { AgentTemplate } from '../types/agent-template'
5+
import { DynamicAgentTemplate } from '../types/dynamic-agent-template'
6+
import { AgentTemplateType } from '../types/session-state'
7+
import { filterValidAgentTemplates } from '../util/agent-file-utils'
8+
import { normalizeAgentNames } from '../util/agent-name-normalization'
89
import {
910
formatParentInstructionsError,
1011
formatSubagentError,
1112
validateParentInstructions,
1213
validateSubagents,
13-
} from '@codebuff/common/util/agent-template-validation'
14-
import { ProjectFileContext } from '@codebuff/common/util/file'
15-
import { convertJsonSchemaToZod } from 'zod-from-json-schema'
16-
14+
} from '../util/agent-template-validation'
15+
import { ProjectFileContext } from '../util/file'
1716
import { logger } from '../util/logger'
18-
import { AgentTemplate } from './types'
17+
import { convertJsonSchemaToZod } from 'zod-from-json-schema'
1918

2019
export interface DynamicAgentValidationError {
2120
filePath: string
@@ -411,4 +410,4 @@ export class DynamicAgentService {
411410
}
412411

413412
// Export a singleton instance
414-
export const dynamicAgentService = new DynamicAgentService()
413+
export const dynamicAgentService = new DynamicAgentService()

common/src/types/agent-template.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import type { Model } from '../constants'
2+
import type { ToolName } from '../tools/constants'
3+
import type {
4+
AgentState,
5+
AgentTemplateType,
6+
ToolResult,
7+
} from './session-state'
8+
import type { z } from 'zod/v4'
9+
10+
export type AgentTemplate<
11+
P = string | undefined,
12+
T = Record<string, any> | undefined,
13+
> = {
14+
id: AgentTemplateType
15+
displayName: string
16+
model: Model
17+
18+
toolNames: ToolName[]
19+
subagents: AgentTemplateType[]
20+
21+
parentPrompt?: string
22+
systemPrompt: string
23+
instructionsPrompt: string
24+
stepPrompt: string
25+
parentInstructions?: Record<string, string>
26+
27+
// Required parameters for spawning this agent.
28+
inputSchema: {
29+
prompt?: z.ZodSchema<P>
30+
params?: z.ZodSchema<T>
31+
}
32+
includeMessageHistory: boolean
33+
outputMode: 'last_message' | 'all_messages' | 'json'
34+
outputSchema?: z.ZodSchema<any>
35+
36+
handleSteps?: StepHandler<P, T> | string // Function or string of the generator code for running in a sandbox
37+
}
38+
39+
export type StepGenerator = Generator<
40+
Omit<any, 'toolCallId'> | 'STEP' | 'STEP_ALL', // Generic tool call type
41+
void,
42+
{ agentState: AgentState; toolResult: ToolResult | undefined }
43+
>
44+
45+
export type StepHandler<
46+
P = string | undefined,
47+
T = Record<string, any> | undefined,
48+
> = (params: { agentState: AgentState; prompt: P; params: T }) => StepGenerator

0 commit comments

Comments
 (0)