Skip to content

feat: 全局env管理器(PI v2.5 新增env)#1592

Merged
MistEO merged 2 commits intov2from
feat/env
Mar 25, 2026
Merged

feat: 全局env管理器(PI v2.5 新增env)#1592
MistEO merged 2 commits intov2from
feat/env

Conversation

@MistEO
Copy link
Copy Markdown
Contributor

@MistEO MistEO commented Mar 25, 2026

Summary by Sourcery

引入集中式的 PI 环境管理器,并将其集成到 agent 服务的初始化流程中。

新功能:

  • 新增全局 PI 环境管理器,用于解析并暴露 PI v2.5.0 的环境变量,包括 controller 和 resource 元数据。
  • 提供便捷的访问方法,用于获取关键的 PI 客户端和项目属性,例如语言、版本、controller 和 resource 信息。

增强点:

  • 更新 i18n 初始化逻辑,从新的 PI 环境管理器中读取客户端语言,而不是直接从环境变量中读取。
  • 在服务启动时初始化 PI 环境管理器,使 PI 上下文在各模块间可用。
Original summary in English

Summary by Sourcery

Introduce a centralized PI environment manager and integrate it into the agent service initialization.

New Features:

  • Add a global PI environment manager that parses and exposes PI v2.5.0 environment variables, including controller and resource metadata.
  • Provide convenience accessors for key PI client and project attributes, such as language, versions, controller, and resource info.

Enhancements:

  • Update i18n initialization to read the client language from the new PI environment manager instead of directly from the environment.
  • Initialize the PI environment manager at service startup to make PI context available across modules.

Copy link
Copy Markdown
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - 我发现了 1 个问题

Prompt for AI Agents
Please address the comments from this code review:

## Individual Comments

### Comment 1
<location path="agent/go-service/pkg/pienv/pienv.go" line_range="161-162" />
<code_context>
+	})
+}
+
+// Get returns the global Env. Returns nil if Init has not been called.
+func Get() *Env {
+	return global
+}
</code_context>
<issue_to_address>
**suggestion (bug_risk):** 当没有调用 `Init` 时,`Get` 和相关访问器会静默返回零值,这可能会掩盖配置问题。

因为 `Get()` 可能返回 `nil`,而辅助函数会默认返回空字符串,所以在 `Init` 调用之前使用它们会静默失败,并且会与之前使用 `os.Getenv` 的行为不同(例如,如果漏掉了 `pienv.Init``i18n.Init` 将总是回退到 `DefaultLang`)。为避免这种情况,可以让 `Get()`/这些辅助函数调用 `once.Do(Init)`,从而在没有显式初始化的情况下仍然安全;或者在检测到未初始化的访问时进行日志记录或触发 panic,以便及早暴露配置错误。

建议实现:

```golang
 // Get returns the global Env.
 // It panics if Init has not been called to surface configuration issues early.
func Get() *Env {
	if global == nil {
		panic("pienv.Get called before pienv.Init; ensure Init is invoked during service startup")
	}
	return global
}

```

如果存在类似 `String()``Int()` 等辅助函数,之前是基于 `Get()` 可能返回 `nil` 的假设,那么现在它们依赖这种 panic 行为来保护未初始化访问,因此不需要进一步修改。不过,如果有任何调用点之前防御性地写了 `if env := Get(); env != nil { ... }` 这样的判断,这些检查现在已经多余,可以简化掉,因为在此改动后 `Get()` 将不会再返回 `nil`。
</issue_to_address>

Sourcery 对开源项目免费——如果你觉得我们的 review 有帮助,欢迎分享 ✨
帮我变得更有用!请对每条评论点击 👍 或 👎,我会根据你的反馈改进后续的 review。
Original comment in English

Hey - I've found 1 issue

Prompt for AI Agents
Please address the comments from this code review:

## Individual Comments

### Comment 1
<location path="agent/go-service/pkg/pienv/pienv.go" line_range="161-162" />
<code_context>
+	})
+}
+
+// Get returns the global Env. Returns nil if Init has not been called.
+func Get() *Env {
+	return global
+}
</code_context>
<issue_to_address>
**suggestion (bug_risk):** `Get` and the accessors silently return zero values when `Init` was not called, which can mask configuration issues.

Because `Get()` may return `nil` and the helpers default to empty strings, calling them before `Init` will fail silently and can change behavior compared to the previous `os.Getenv` usage (e.g. `i18n.Init` will always fall back to `DefaultLang` if `pienv.Init` is missed). To avoid this, either make `Get()`/the helpers call `once.Do(Init)` so they’re safe without explicit initialization, or detect uninitialized access (log or panic) to surface configuration bugs early.

Suggested implementation:

```golang
 // Get returns the global Env.
 // It panics if Init has not been called to surface configuration issues early.
func Get() *Env {
	if global == nil {
		panic("pienv.Get called before pienv.Init; ensure Init is invoked during service startup")
	}
	return global
}

```

If there are helper functions like `String()`, `Int()`, etc. that previously assumed `Get()` could return `nil`, they now rely on this panic behavior to protect against uninitialized access and don’t need further modification. However, if any call sites were defensively checking `if env := Get(); env != nil { ... }`, those checks are now redundant and can be simplified, since `Get()` will never return `nil` after this change.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@MistEO MistEO requested review from isHarryh and zmdyy0318 March 25, 2026 08:41
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

该 PR 为 go-service 引入一个全局 PI v2.5 环境变量管理器,用于集中读取/解析 PI_* 环境变量,并让 i18n 语言选择改为基于该管理器的结果,统一入口避免各处散落的 os.Getenv

Changes:

  • 新增 pkg/pienv:解析并缓存 PI v2.5 的 PI_* 环境变量(含 PI_CONTROLLER/PI_RESOURCE JSON)。
  • i18n.Init() 改为通过 pienv.ClientLanguage() 获取语言,而非直接读取环境变量。
  • main() 启动时增加 pienv.Init(),保证 i18n 初始化前已读取环境上下文。

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 6 comments.

File Description
agent/go-service/pkg/pienv/pienv.go 新增 PI 环境变量解析与全局访问器(含 controller/resource JSON 解析与初始化日志)。
agent/go-service/pkg/i18n/i18n.go i18n 语言来源切换到 pienv,减少直接读环境变量的散落逻辑。
agent/go-service/main.go 启动流程中加入 pienv 初始化,确保 i18n 读取到 PI 语言信息。

@zmdyy0318
Copy link
Copy Markdown
Contributor

干啥的

@MistEO
Copy link
Copy Markdown
Contributor Author

MistEO commented Mar 25, 2026

干啥的

UI 的语言、选择的控制器、资源、版本号 之类的

MaaXYZ/MaaFramework@965647b

@MistEO MistEO merged commit d5a8569 into v2 Mar 25, 2026
16 checks passed
@MistEO MistEO deleted the feat/env branch March 25, 2026 09:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants