Skip to content

Commit c4a4bf5

Browse files
committed
add agent-runtime to sdk
1 parent 43cac13 commit c4a4bf5

File tree

7 files changed

+296
-174
lines changed

7 files changed

+296
-174
lines changed

common/src/types/contracts/database.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ type User = {
66
email: string
77
discord_id: string | null
88
}
9+
export const userColumns = ['id', 'email', 'discord_id'] as const
910
export type UserColumn = keyof User
1011
export type GetUserInfoFromApiKeyInput<T extends UserColumn> = {
1112
apiKey: string

sdk/src/impl/agent-runtime.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
import { trackEvent } from '@codebuff/common/analytics'
2-
import { success } from '@codebuff/common/util/error'
3-
41
import {
52
addAgentStep,
63
fetchAgentFromDatabase,
@@ -9,12 +6,14 @@ import {
96
startAgentRun,
107
} from './database'
118
import { promptAiSdk, promptAiSdkStream, promptAiSdkStructured } from './llm'
9+
import { trackEvent } from '../../../common/src/analytics'
10+
import { success } from '../../../common/src/util/error'
1211

1312
import type {
1413
AgentRuntimeDeps,
1514
AgentRuntimeScopedDeps,
16-
} from '@codebuff/common/types/contracts/agent-runtime'
17-
import type { Logger } from '@codebuff/common/types/contracts/logger'
15+
} from '../../../common/src/types/contracts/agent-runtime'
16+
import type { Logger } from '../../../common/src/types/contracts/logger'
1817

1918
export function getAgentRuntimeImpl(
2019
params: {

sdk/src/impl/database.ts

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { getErrorObject } from '@codebuff/common/util/error'
2-
1+
import { userColumns } from '../../../common/src/types/contracts/database'
2+
import { getErrorObject } from '../../../common/src/util/error'
33
import { WEBSITE_URL } from '../constants'
44

55
import type {
@@ -10,15 +10,35 @@ import type {
1010
GetUserInfoFromApiKeyOutput,
1111
StartAgentRunFn,
1212
UserColumn,
13-
} from '@codebuff/common/types/contracts/database'
14-
import type { ParamsOf } from '@codebuff/common/types/function-params'
13+
} from '../../../common/src/types/contracts/database'
14+
import type { ParamsOf } from '../../../common/src/types/function-params'
15+
16+
const userInfoCache: Record<
17+
string,
18+
Awaited<GetUserInfoFromApiKeyOutput<UserColumn>>
19+
> = {}
1520

1621
export async function getUserInfoFromApiKey<T extends UserColumn>(
1722
params: GetUserInfoFromApiKeyInput<T>,
1823
): GetUserInfoFromApiKeyOutput<T> {
1924
const { apiKey, fields, logger } = params
2025

21-
const urlParams = new URLSearchParams({ apiKey, fields: fields.join(',') })
26+
if (apiKey in userInfoCache) {
27+
const userInfo = userInfoCache[apiKey]
28+
if (userInfo === null) {
29+
return userInfo
30+
}
31+
return Object.fromEntries(
32+
fields.map((field) => [field, userInfo[field]]),
33+
) as {
34+
[K in (typeof fields)[number]]: (typeof userInfo)[K]
35+
}
36+
}
37+
38+
const urlParams = new URLSearchParams({
39+
apiKey,
40+
fields: userColumns.join(','),
41+
})
2242
const url = new URL(`/api/v1/me?${urlParams}`, WEBSITE_URL)
2343

2444
try {
@@ -36,14 +56,25 @@ export async function getUserInfoFromApiKey<T extends UserColumn>(
3656
)
3757
return null
3858
}
39-
return response.json()
59+
60+
userInfoCache[apiKey] = await response.json()
4061
} catch (error) {
4162
logger.error(
4263
{ error: getErrorObject(error), apiKey, fields },
4364
'getUserInfoFromApiKey error',
4465
)
4566
return null
4667
}
68+
69+
const userInfo = userInfoCache[apiKey]
70+
if (userInfo === null) {
71+
return userInfo
72+
}
73+
return Object.fromEntries(
74+
fields.map((field) => [field, userInfo[field]]),
75+
) as {
76+
[K in (typeof fields)[number]]: (typeof userInfo)[K]
77+
}
4778
}
4879

4980
export async function fetchAgentFromDatabase(

sdk/src/impl/llm.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
11
import { createOpenAICompatible } from '@ai-sdk/openai-compatible'
2+
import { streamText, APICallError, generateText, generateObject } from 'ai'
3+
4+
import { PROFIT_MARGIN } from '../../../common/src/old-constants'
5+
import { buildArray } from '../../../common/src/util/array'
6+
import { getErrorObject } from '../../../common/src/util/error'
7+
import { convertCbToModelMessages } from '../../../common/src/util/messages'
8+
import { StopSequenceHandler } from '../../../common/src/util/stop-sequence'
29
import {
310
checkLiveUserInput,
411
getLiveUserInputIds,
5-
} from '@codebuff/agent-runtime/live-user-inputs'
6-
import { PROFIT_MARGIN } from '@codebuff/common/old-constants'
7-
import { buildArray } from '@codebuff/common/util/array'
8-
import { getErrorObject } from '@codebuff/common/util/error'
9-
import { convertCbToModelMessages } from '@codebuff/common/util/messages'
10-
import { StopSequenceHandler } from '@codebuff/common/util/stop-sequence'
11-
import { streamText, APICallError, generateText, generateObject } from 'ai'
12-
12+
} from '../../../packages/agent-runtime/src/live-user-inputs'
1313
import { WEBSITE_URL } from '../constants'
1414

15-
import type { LanguageModelV2 } from '@ai-sdk/provider'
1615
import type {
1716
PromptAiSdkFn,
1817
PromptAiSdkStreamFn,
1918
PromptAiSdkStructuredInput,
2019
PromptAiSdkStructuredOutput,
21-
} from '@codebuff/common/types/contracts/llm'
22-
import type { ParamsOf } from '@codebuff/common/types/function-params'
20+
} from '../../../common/src/types/contracts/llm'
21+
import type { ParamsOf } from '../../../common/src/types/function-params'
22+
import type { LanguageModelV2 } from '@ai-sdk/provider'
2323
import type {
2424
OpenRouterProviderOptions,
2525
OpenRouterUsageAccounting,

sdk/src/run-state.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
import * as os from 'os'
22
import path from 'path'
33

4-
import { getFileTokenScores } from '@codebuff/code-map/parse'
54
import { cloneDeep } from 'lodash'
65

76
import {
87
getProjectFileTree,
98
getAllFilePaths,
109
} from '../../common/src/project-file-tree'
1110
import { getInitialSessionState } from '../../common/src/types/session-state'
11+
import { getFileTokenScores } from '../../packages/code-map/src/parse'
1212

1313
import type { CustomToolDefinition } from './custom-tool'
1414
import type { AgentDefinition } from '../../common/src/templates/initial-agents-dir/types/agent-definition'
15+
import type { CodebuffFileSystem } from '../../common/src/types/filesystem'
1516
import type { Message } from '../../common/src/types/messages/codebuff-message'
1617
import type {
1718
AgentOutput,
@@ -21,7 +22,6 @@ import type {
2122
CustomToolDefinitions,
2223
FileTreeNode,
2324
} from '../../common/src/util/file'
24-
import type { CodebuffFileSystem } from '@codebuff/common/types/filesystem'
2525

2626
export type RunState = {
2727
sessionState: SessionState
@@ -36,7 +36,7 @@ export type InitialSessionStateOptions = {
3636
customToolDefinitions?: CustomToolDefinition[]
3737
maxAgentSteps?: number
3838
fs?: CodebuffFileSystem
39-
};
39+
}
4040

4141
/**
4242
* Processes agent definitions array and converts handleSteps functions to strings
@@ -167,11 +167,11 @@ function deriveKnowledgeFiles(
167167

168168
export function initialSessionState(
169169
options: InitialSessionStateOptions,
170-
): Promise<SessionState>;
170+
): Promise<SessionState>
171171
export function initialSessionState(
172172
cwd: string,
173173
options?: Omit<InitialSessionStateOptions, 'cwd'>,
174-
): Promise<SessionState>;
174+
): Promise<SessionState>
175175
export async function initialSessionState(
176176
arg1: string | InitialSessionStateOptions,
177177
arg2?: Omit<InitialSessionStateOptions, 'cwd'>,

0 commit comments

Comments
 (0)