Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
119 changes: 117 additions & 2 deletions components/ambient-ui/src/adapters/__tests__/mappers.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { describe, it, expect } from 'vitest'
import { mapSdkSessionToDomain, mapSdkProjectToDomain, mapSessionMessageToDomain } from '../mappers'
import { mapSdkSessionToDomain, mapSdkProjectToDomain, mapSessionMessageToDomain, mapSdkAgentToDomain } from '../mappers'
import type { SdkSessionMessageShape } from '../mappers'
import type { Session, Project } from 'ambient-sdk'
import type { Session, Project, Agent } from 'ambient-sdk'

function makeSdkSession(overrides: Partial<Session> = {}): Session {
return {
Expand Down Expand Up @@ -454,3 +454,118 @@ describe('mapSessionMessageToDomain', () => {
expect(domain.payload).toBe(complexPayload)
})
})

function makeSdkAgent(overrides: Partial<Agent> = {}): Agent {
return {
id: 'agent-001',
kind: 'Agent',
href: '/api/ambient/v1/agents/agent-001',
created_at: '2026-02-01T09:00:00Z',
updated_at: '2026-02-01T10:00:00Z',
annotations: '{}',
bot_account_name: '',
current_session_id: '',
description: 'A test agent',
display_name: 'Test Agent',
environment_variables: '',
labels: '',
llm_max_tokens: 4096,
llm_model: 'claude-sonnet-4-20250514',
llm_temperature: 0.7,
name: 'test-agent',
owner_user_id: 'user-42',
parent_agent_id: '',
project_id: 'proj-123',
prompt: 'You are a helpful agent.',
repo_url: 'https://github.com/org/repo',
resource_overrides: '',
workflow_id: 'wf-1',
...overrides,
}
}

describe('mapSdkAgentToDomain', () => {
it('maps snake_case fields to camelCase', () => {
const sdk = makeSdkAgent()
const domain = mapSdkAgentToDomain(sdk)

expect(domain.id).toBe('agent-001')
expect(domain.name).toBe('test-agent')
expect(domain.displayName).toBe('Test Agent')
expect(domain.description).toBe('A test agent')
expect(domain.model).toBe('claude-sonnet-4-20250514')
expect(domain.ownerUserId).toBe('user-42')
expect(domain.projectId).toBe('proj-123')
expect(domain.prompt).toBe('You are a helpful agent.')
expect(domain.repoUrl).toBe('https://github.com/org/repo')
expect(domain.workflowId).toBe('wf-1')
expect(domain.createdAt).toBe('2026-02-01T09:00:00Z')
expect(domain.updatedAt).toBe('2026-02-01T10:00:00Z')
})

it('maps empty string fields to null', () => {
const sdk = makeSdkAgent({
display_name: '',
description: '',
llm_model: '',
owner_user_id: '',
current_session_id: '',
project_id: '',
prompt: '',
repo_url: '',
workflow_id: '',
})
const domain = mapSdkAgentToDomain(sdk)

expect(domain.displayName).toBeNull()
expect(domain.description).toBeNull()
expect(domain.model).toBeNull()
expect(domain.ownerUserId).toBeNull()
expect(domain.currentSessionId).toBeNull()
expect(domain.projectId).toBeNull()
expect(domain.prompt).toBeNull()
expect(domain.repoUrl).toBeNull()
expect(domain.workflowId).toBeNull()
})

it('parses valid annotations JSON to Record', () => {
const annotations = JSON.stringify({ team: 'platform', tier: 'production' })
const sdk = makeSdkAgent({ annotations })
const domain = mapSdkAgentToDomain(sdk)

expect(domain.annotations).toEqual({ team: 'platform', tier: 'production' })
})

it('returns empty Record for invalid annotations', () => {
const sdk = makeSdkAgent({ annotations: 'broken{' })
const domain = mapSdkAgentToDomain(sdk)
expect(domain.annotations).toEqual({})
})

it('parses valid labels JSON to Record', () => {
const labels = JSON.stringify({ env: 'dev', app: 'backend' })
const sdk = makeSdkAgent({ labels })
const domain = mapSdkAgentToDomain(sdk)

expect(domain.labels).toEqual({ env: 'dev', app: 'backend' })
})

it('returns empty Record for invalid labels', () => {
const sdk = makeSdkAgent({ labels: '["a"]' })
const domain = mapSdkAgentToDomain(sdk)
expect(domain.labels).toEqual({})
})

it('handles null created_at and updated_at', () => {
const sdk = makeSdkAgent({ created_at: null, updated_at: null })
const domain = mapSdkAgentToDomain(sdk)
expect(domain.createdAt).toBe('')
expect(domain.updatedAt).toBe('')
})

it('maps current_session_id when present', () => {
const sdk = makeSdkAgent({ current_session_id: 'sess-abc' })
const domain = mapSdkAgentToDomain(sdk)
expect(domain.currentSessionId).toBe('sess-abc')
})
})
24 changes: 22 additions & 2 deletions components/ambient-ui/src/adapters/mappers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { Session, Project } from 'ambient-sdk'
import type { Session, Project, Agent } from 'ambient-sdk'
import type {
DomainSession, DomainProject, DomainSessionMessage, SessionPhase, SessionEventType,
DomainSession, DomainProject, DomainSessionMessage, DomainAgent, SessionPhase, SessionEventType,
DomainRepo, DomainReconciledRepo, DomainCondition, ReconciledRepoStatus, ConditionStatus,
} from '@/domain/types'

Expand Down Expand Up @@ -168,6 +168,26 @@ export function mapSdkProjectToDomain(sdk: Project): DomainProject {
}
}

export function mapSdkAgentToDomain(sdk: Agent): DomainAgent {
return {
id: sdk.id,
name: sdk.name,
displayName: emptyToNull(sdk.display_name),
description: emptyToNull(sdk.description),
model: emptyToNull(sdk.llm_model),
ownerUserId: emptyToNull(sdk.owner_user_id),
currentSessionId: emptyToNull(sdk.current_session_id),
projectId: emptyToNull(sdk.project_id),
prompt: emptyToNull(sdk.prompt),
repoUrl: emptyToNull(sdk.repo_url),
workflowId: emptyToNull(sdk.workflow_id),
annotations: parseAnnotations(sdk.annotations),
labels: parseJsonObject(sdk.labels),
createdAt: sdk.created_at ?? '',
updatedAt: sdk.updated_at ?? '',
}
}

export type SdkSessionMessageShape = {
id: string
session_id: string
Expand Down
49 changes: 49 additions & 0 deletions components/ambient-ui/src/adapters/sdk-agents.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { AgentAPI } from 'ambient-sdk'
import type { AgentsPort } from '@/ports/agents'
import type { DomainAgent, ListParams, PaginatedResult } from '@/domain/types'
import { mapSdkAgentToDomain } from './mappers'
import { getConfig } from './sdk-client'

function sanitizeSearch(value: string): string {
return value.replace(/['"%;\\]/g, '')
}

function getProjectScopedAPI(projectId: string): AgentAPI {
return new AgentAPI({ ...getConfig(), project: projectId })
}

function buildSdkListOptions(params?: ListParams) {
return {
page: params?.page ?? 1,
size: params?.size ?? 20,
search: params?.search
? `name like '%${sanitizeSearch(params.search)}%'`
: undefined,
orderBy: params?.orderBy,
}
}

export function createAgentsAdapter(): AgentsPort {
return {
async list(projectId: string, params?: ListParams): Promise<PaginatedResult<DomainAgent>> {
const api = getProjectScopedAPI(projectId)
const opts = buildSdkListOptions(params)
const result = await api.list(opts)
const page = opts.page
const size = opts.size
return {
items: result.items.map(mapSdkAgentToDomain),
total: result.total,
page,
size,
hasMore: page * size < result.total,
}
},

async get(agentId: string): Promise<DomainAgent> {
const api = new AgentAPI({ ...getConfig(), project: '_' })
const agent = await api.get(agentId)
return mapSdkAgentToDomain(agent)
},
}
}
1 change: 0 additions & 1 deletion components/ambient-ui/src/adapters/sdk-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ const bffConfig: AmbientClientConfig = {

let sessions: SessionAPI | null = null
let projects: ProjectAPI | null = null

export function getSessionAPI(): SessionAPI {
if (!sessions) {
sessions = new SessionAPI(bffConfig)
Expand Down
24 changes: 22 additions & 2 deletions components/ambient-ui/src/adapters/sdk-sessions.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { SessionAPI } from 'ambient-sdk'
import type { SessionAPI, SessionCreateRequest } from 'ambient-sdk'
import type { SessionsPort } from '@/ports/sessions'
import type { DomainSession, ListParams, PaginatedResult } from '@/domain/types'
import type { DomainSession, DomainSessionCreateRequest, ListParams, PaginatedResult } from '@/domain/types'
import { mapSdkSessionToDomain } from './mappers'
import { getSessionAPI } from './sdk-client'

Expand All @@ -21,6 +21,20 @@ function buildSdkListOptions(projectId: string, params?: ListParams) {
}
}

function mapDomainCreateToSdk(request: DomainSessionCreateRequest): SessionCreateRequest {
const sdkReq: SessionCreateRequest = {
name: request.name,
project_id: request.projectId,
}
if (request.agentId) sdkReq.agent_id = request.agentId
if (request.prompt) sdkReq.prompt = request.prompt
if (request.model) sdkReq.llm_model = request.model
if (request.temperature !== undefined) sdkReq.llm_temperature = request.temperature
if (request.maxTokens !== undefined) sdkReq.llm_max_tokens = request.maxTokens
if (request.timeout !== undefined) sdkReq.timeout = request.timeout
return sdkReq
}

function createSdkSessionsAdapter(api: SessionAPI): SessionsPort {
return {
async list(projectId: string, params?: ListParams): Promise<PaginatedResult<DomainSession>> {
Expand All @@ -43,6 +57,12 @@ function createSdkSessionsAdapter(api: SessionAPI): SessionsPort {
return mapSdkSessionToDomain(session)
},

async create(request: DomainSessionCreateRequest): Promise<DomainSession> {
const sdkReq = mapDomainCreateToSdk(request)
const session = await api.create(sdkReq)
return mapSdkSessionToDomain(session)
},

async stop(sessionId: string): Promise<void> {
await api.stop(sessionId)
},
Expand Down
Loading
Loading