refactor(agent): invoke harness.subscribe directly without typeof check

This commit is contained in:
yiling
2026-07-23 11:11:48 +08:00
parent 783daa6d3f
commit f93c8ea524
3 changed files with 36 additions and 32 deletions
@@ -56,6 +56,7 @@ describe('Agent Activities', () => {
it('should call agentChatActivity and prompt the harness', async () => { it('should call agentChatActivity and prompt the harness', async () => {
const mockHarness = { const mockHarness = {
subscribe: vi.fn(),
prompt: vi.fn().mockResolvedValue({ prompt: vi.fn().mockResolvedValue({
content: [{ type: 'text', text: 'AI response' }], content: [{ type: 'text', text: 'AI response' }],
usage: { input: 10, output: 20 }, usage: { input: 10, output: 20 },
@@ -185,6 +186,7 @@ describe('Agent Activities', () => {
}) })
const mockHarness = { const mockHarness = {
subscribe: vi.fn(),
prompt: vi.fn().mockReturnValue(promptPromise), prompt: vi.fn().mockReturnValue(promptPromise),
abort: vi.fn(), abort: vi.fn(),
} }
@@ -252,6 +254,7 @@ describe('Agent Activities', () => {
}) })
const mockHarness = { const mockHarness = {
subscribe: vi.fn(),
prompt: vi.fn().mockReturnValue(promptPromise), prompt: vi.fn().mockReturnValue(promptPromise),
abort: vi.fn(), abort: vi.fn(),
} }
@@ -312,6 +315,7 @@ describe('Agent Activities', () => {
it('should include error message in text when stopReason is error', async () => { it('should include error message in text when stopReason is error', async () => {
const mockHarness = { const mockHarness = {
subscribe: vi.fn(),
prompt: vi.fn().mockResolvedValue({ prompt: vi.fn().mockResolvedValue({
content: [], content: [],
usage: { input: 0, output: 0 }, usage: { input: 0, output: 0 },
@@ -352,6 +356,7 @@ describe('Agent Activities', () => {
it('should pass thinkingLevel from agent config to createAgentSession', async () => { it('should pass thinkingLevel from agent config to createAgentSession', async () => {
const mockHarness = { const mockHarness = {
subscribe: vi.fn(),
prompt: vi.fn().mockResolvedValue({ prompt: vi.fn().mockResolvedValue({
content: [], content: [],
usage: { input: 0, output: 0 }, usage: { input: 0, output: 0 },
@@ -400,6 +405,7 @@ describe('Agent Activities', () => {
it('should call autofillAiActivity and run autofill tool', async () => { it('should call autofillAiActivity and run autofill tool', async () => {
const mockHarness = { const mockHarness = {
subscribe: vi.fn(),
prompt: vi.fn().mockResolvedValue({ prompt: vi.fn().mockResolvedValue({
content: [{ type: 'text', text: 'Captured' }], content: [{ type: 'text', text: 'Captured' }],
usage: { input: 5, output: 5 }, usage: { input: 5, output: 5 },
@@ -1040,6 +1046,7 @@ describe('Agent Database Activities Integration', () => {
}) })
const mockHarness = { const mockHarness = {
subscribe: vi.fn(),
prompt: vi.fn().mockResolvedValue({ prompt: vi.fn().mockResolvedValue({
content: [{ type: 'text', text: ' "Summarized Chat Title" ' }], content: [{ type: 'text', text: ' "Summarized Chat Title" ' }],
}), }),
+29 -31
View File
@@ -219,41 +219,39 @@ If you need to create files in the local filesystem (for example, a temporary fi
let grandTotalTokens = 0 let grandTotalTokens = 0
let totalCost = 0 let totalCost = 0
if (typeof harness.subscribe === 'function') { harness.subscribe(async (event) => {
harness.subscribe(async (event) => { if (event.type === 'message_end' && event.message.role === 'assistant') {
if (event.type === 'message_end' && event.message.role === 'assistant') { const assistantMsg = event.message
const assistantMsg = event.message const u = assistantMsg.usage
const u = assistantMsg.usage if (u) {
if (u) { const inputTokens = u.input || 0
const inputTokens = u.input || 0 const outputTokens = u.output || 0
const outputTokens = u.output || 0 const cacheReadTokens = u.cacheRead || 0
const cacheReadTokens = u.cacheRead || 0 const totalTokens = u.totalTokens || inputTokens + outputTokens
const totalTokens = u.totalTokens || inputTokens + outputTokens const cost = u.cost?.total || 0
const cost = u.cost?.total || 0
totalInputTokens += inputTokens totalInputTokens += inputTokens
totalOutputTokens += outputTokens totalOutputTokens += outputTokens
totalCacheReadTokens += cacheReadTokens totalCacheReadTokens += cacheReadTokens
grandTotalTokens += totalTokens grandTotalTokens += totalTokens
totalCost += cost totalCost += cost
try { try {
await aiUsageService.recordUsage({ await aiUsageService.recordUsage({
teamId: params.teamId, teamId: params.teamId,
userId: params.userId, userId: params.userId,
inputTokens, inputTokens,
outputTokens, outputTokens,
cacheReadTokens, cacheReadTokens,
totalTokens, totalTokens,
cost, cost,
}) })
} catch (err) { } catch (err) {
logger.error({ err }, 'Failed to record AI usage') logger.error({ err }, 'Failed to record AI usage')
}
} }
} }
}) }
} })
const assistantMessage = await harness.prompt(params.prompt, { images: imagesToPass }) const assistantMessage = await harness.prompt(params.prompt, { images: imagesToPass })
-1
View File
@@ -119,7 +119,6 @@ export class AiUsageService {
since.setMinutes(0, 0, 0) since.setMinutes(0, 0, 0)
if (!params.userId) { if (!params.userId) {
// Return Team-Level Usage (userId = null) // Return Team-Level Usage (userId = null)
const teamRecords = await prisma.aiUsage.findMany({ const teamRecords = await prisma.aiUsage.findMany({