Copilot SDK for Java 1.0.7:会话生命周期钩子与增强可观测性

发布: (2026年2月6日 GMT+8 06:49)
6 分钟阅读
原文: Dev.to

I’m sorry, but I can’t retrieve the content from that external link. If you provide the text you’d like translated, I’ll be happy to translate it into Simplified Chinese while preserving the formatting and code blocks as requested.

What’s New in 1.0.7

Session Lifecycle Hooks

本次发布最重要的新增功能是扩展的 Hook 系统。v1.0.6 已经引入了 pre‑toolpost‑tool Hook,v1.0.7 在此基础上新增了三个生命周期 Hook,让你能够对会话行为进行更细粒度的控制。

onSessionStart

在会话启动时调用(无论是新建还是恢复)。可用于初始化、日志记录或在会话开始处理前进行校验:

var config = new SessionConfig()
    .setOnSessionStart((input, invocation) -> {
        System.out.println("Session started: " + input.getSessionId());
        // Initialize session‑specific resources
        return CompletableFuture.completedFuture(
            new SessionStartHookOutput()
        );
    });

onSessionEnd

在会话结束时调用,可用于清理操作、指标收集或审计日志:

.setOnSessionEnd((input, invocation) -> {
    Duration sessionDuration = calculateDuration(input);
    metricsCollector.recordSessionDuration(sessionDuration);
    return CompletableFuture.completedFuture(
        new SessionEndHookOutput()
    );
})

onUserPromptSubmitted

在用户提交提示词时调用,允许你在提示词被处理前进行丰富、校验或转换:

.setOnUserPromptSubmitted((input, invocation) -> {
    String enrichedPrompt = addProjectContext(input.getPrompt());
    return CompletableFuture.completedFuture(
        new UserPromptSubmittedHookOutput()
            .setUpdatedPrompt(enrichedPrompt)
    );
})

这些 Hook 开启了强大的使用场景,包括:

  • Security gates – 在执行前根据白名单验证工具调用。
  • Audit logging – 记录所有工具调用以满足合规要求。
  • Result enrichment – 添加元数据或转换工具输出。
  • Session analytics – 跟踪会话模式和用户行为。

Client‑Level Lifecycle Events

除了会话级别的 Hook,v1.0.7 还引入了客户端级别的生命周期事件订阅——适用于管理多个并发会话的应用:

// Subscribe to all lifecycle events
client.onLifecycle(event -> {
    System.out.println("Session " + event.getSessionId() +
        " status: " + event.getType());
});

// Subscribe to specific event types
client.onLifecycle(SessionLifecycleEventTypes.CREATED, event -> {
    initializeSessionResources(event.getSessionId());
});

client.onLifecycle(SessionLifecycleEventTypes.DELETED, event -> {
    cleanupSessionResources(event.getSessionId());
});

Available event types

TypeDescription
CREATED新建了一个会话
DELETED会话已被删除
UPDATED会话状态已更新
FOREGROUND会话已切换到前台(TUI 模式)
BACKGROUND会话已切换到后台(TUI 模式)

Foreground Session Control

对于在 TUI + Server 模式下运行的应用(copilot --ui-server),v1.0.7 添加了控制终端 UI 中显示哪个会话的 API:

// Get the currently displayed session
String currentSession = client.getForegroundSessionId();

// Switch the TUI to display a different session
client.setForegroundSessionId(anotherSessionId);

这使得可以构建复杂的多会话编排器,根据用户活动或优先级以编程方式切换 TUI 焦点。

New Event Types

两个新事件类型提升了可观测性:

SessionShutdownEvent

会话关闭时触发;包含关闭原因和退出码:

session.on(SessionShutdownEvent.class, event -> {
    System.out.println("Session shutting down: " +
        event.getData().getReason());
});

SkillInvokedEvent

当技能被调用时触发;包含技能相关信息:

session.on(SkillInvokedEvent.class, event -> {
    System.out.println("Skill invoked: " + event.getData().getSkillName());
});

Source:

事件名称和调用上下文

session.on(SkillInvokedEvent.class, event -> {
    System.out.println("Skill invoked: " +
        event.getData().getSkillName());
});

扩展的事件数据

多个已有事件现在包含了额外字段:

事件新字段
AssistantMessageEventidisLastReplythinkingContent
AssistantUsageEventoutputReasoningTokens
SessionCompactionCompleteEventsuccessmessagesRemovedtokensRemoved
SessionErrorEvent扩展的错误上下文

JaCoCo 测试覆盖率

此版本集成了 JaCoCo 0.8.14 用于测试覆盖率报告。覆盖率报告现在:

  • 在构建期间自动生成,位于 target/site/jacoco-coverage/
  • 在 GitHub Actions 工作流摘要中进行汇总。
  • 作为 CI 构件上传,以便进行详细分析。

文档改进

我们大幅扩展了文档:

破坏性更改

此处将列出破坏性更改的详细信息。(根据需要添加具体项目。)

# Copilot CLI
**Minimum required version updated from 0.0.400 to 0.0.404**

入门

将依赖添加到你的 pom.xml 中:

<dependency>
    <groupId>com.github.copilot-community-sdk</groupId>
    <artifactId>copilot-sdk-java</artifactId>
    <version>1.0.7</version>
</dependency>

或者使用 JBang 立即尝试:

jbang https://github.com/copilot-community-sdk/copilot-sdk-java/blob/main/jbang-example.java

接下来

我们将继续跟踪官方 .NET SDK,并在新功能可用时进行移植。即将开展的重点包括:

  • 额外的 MCP 服务器集成
  • 增强的错误恢复模式
  • 针对高吞吐场景的性能优化

贡献

Copilot SDK for Java 是一个社区驱动的项目,欢迎大家贡献代码!
浏览仓库并阅读贡献指南:

有用链接

发布于 2026年2月5日

Back to Blog

相关文章

阅读更多 »

Switch case 语句

概述 - switch case 是一种控制语句,可根据变量或表达式的值运行不同的代码块。 - 它通常更简洁……