chore(a2a-server): if a2a task creation fails return error to user (#8106)

Co-authored-by: cornmander <shikhman@google.com>
This commit is contained in:
Adam Weidman
2025-09-10 12:08:58 -04:00
committed by GitHub
parent 50e7c88aa4
commit bd0f085ae8
4 changed files with 91 additions and 15 deletions

View File

@@ -36,6 +36,7 @@ import { loadSettings } from '../config/settings.js';
import { loadExtensions } from '../config/extension.js';
import { Task } from './task.js';
import { requestStorage } from '../http/requestStorage.js';
import { pushTaskStateFailed } from '../utils/executor_utils.js';
/**
* Provides a wrapper for Task. Passes data from Task to SDKTask.
@@ -116,8 +117,8 @@ export class CoderAgentExecutor implements AgentExecutor {
const agentSettings = persistedState._agentSettings;
const config = await this.getConfig(agentSettings, sdkTask.id);
const contextId =
(metadata['_contextId'] as string) || (sdkTask.contextId as string);
const contextId: string =
(metadata['_contextId'] as string) || sdkTask.contextId;
const runtimeTask = await Task.create(
sdkTask.id,
contextId,
@@ -280,10 +281,10 @@ export class CoderAgentExecutor implements AgentExecutor {
const sdkTask = requestContext.task as SDKTask | undefined;
const taskId = sdkTask?.id || userMessage.taskId || uuidv4();
const contextId =
const contextId: string =
userMessage.contextId ||
sdkTask?.contextId ||
sdkTask?.metadata?.['_contextId'] ||
(sdkTask?.metadata?.['_contextId'] as string) ||
uuidv4();
logger.info(
@@ -381,12 +382,21 @@ export class CoderAgentExecutor implements AgentExecutor {
const agentSettings = userMessage.metadata?.[
'coderAgent'
] as AgentSettings;
wrapper = await this.createTask(
taskId,
contextId as string,
agentSettings,
eventBus,
);
try {
wrapper = await this.createTask(
taskId,
contextId,
agentSettings,
eventBus,
);
} catch (error) {
logger.error(
`[CoderAgentExecutor] Error creating task ${taskId}:`,
error,
);
pushTaskStateFailed(error, eventBus, taskId, contextId);
return;
}
const newTaskSDK = wrapper.toSDKTask();
eventBus.publish({
...newTaskSDK,

View File

@@ -108,9 +108,10 @@ export async function loadConfig(
logger.info('[Config] Using Gemini API Key');
await config.refreshAuth(AuthType.USE_GEMINI);
} else {
logger.error(
`[Config] Unable to set GeneratorConfig. Please provide a GEMINI_API_KEY or set USE_CCPA.`,
);
const errorMessage =
'[Config] Unable to set GeneratorConfig. Please provide a GEMINI_API_KEY or set USE_CCPA.';
logger.error(errorMessage);
throw new Error(errorMessage);
}
return config;

View File

@@ -7,14 +7,17 @@
import { describe, it, expect, beforeAll, afterAll, vi } from 'vitest';
import request from 'supertest';
import type express from 'express';
import { createApp, updateCoderAgentCardUrl } from './app.js';
import * as fs from 'node:fs';
import * as path from 'node:path';
import * as os from 'node:os';
import type { Server } from 'node:http';
import type { TaskMetadata } from '../types.js';
import type { AddressInfo } from 'node:net';
import { createApp, updateCoderAgentCardUrl } from './app.js';
import type { TaskMetadata } from '../types.js';
import { createMockConfig } from '../utils/testing_utils.js';
import type { Config } from '@google/gemini-cli-core';
// Mock the logger to avoid polluting test output
// Comment out to help debug
vi.mock('../utils/logger.js', () => ({
@@ -56,6 +59,16 @@ vi.mock('../agent/task.js', () => {
return { Task: MockTask };
});
vi.mock('../config/config.js', async () => {
const actual = await vi.importActual('../config/config.js');
return {
...actual,
loadConfig: vi
.fn()
.mockImplementation(async () => createMockConfig({}) as Config),
};
});
describe('Agent Server Endpoints', () => {
let app: express.Express;
let server: Server;

View File

@@ -0,0 +1,52 @@
/**
* @license
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import type { Message } from '@a2a-js/sdk';
import type { ExecutionEventBus } from '@a2a-js/sdk/server';
import { v4 as uuidv4 } from 'uuid';
import { CoderAgentEvent } from '../types.js';
import type { StateChange } from '../types.js';
export async function pushTaskStateFailed(
error: unknown,
eventBus: ExecutionEventBus,
taskId: string,
contextId: string,
) {
const errorMessage =
error instanceof Error ? error.message : 'Agent execution error';
const stateChange: StateChange = {
kind: CoderAgentEvent.StateChangeEvent,
};
eventBus.publish({
kind: 'status-update',
taskId,
contextId,
status: {
state: 'failed',
message: {
kind: 'message',
role: 'agent',
parts: [
{
kind: 'text',
text: errorMessage,
},
],
messageId: uuidv4(),
taskId,
contextId,
} as Message,
},
final: true,
metadata: {
coderAgent: stateChange,
model: 'unknown',
error: errorMessage,
},
});
}