-
Notifications
You must be signed in to change notification settings - Fork 19
添加agent studio的cli能力 #116
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
chenanran555
wants to merge
7
commits into
main
Choose a base branch
from
feat/cma
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+3,106
−149
Open
添加agent studio的cli能力 #116
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
6329427
feat(agent): add agent command group with session and state management
chenanran555 d6bd38a
feat(agent): agent相关cli命令的client层功能,对齐cli client的基础能力
chenanran555 1c9dac2
feat(cma): login时初始化agent相关的baseUrl
chenanran555 7cbd61d
Merge remote-tracking branch 'origin/main' into feat/cma
chenanran555 9e59b01
feat: update openagentpack sdk
chenanran555 1da3367
feat: fix ci
chenanran555 64335a6
feat(agent): rename cli command to managed-agent
chenanran555 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,4 +2,7 @@ node_modules | |
| dist | ||
| *.log | ||
| .DS_Store | ||
| outputs/ | ||
| outputs/ | ||
| # agents | ||
| agents.state.json | ||
| .env | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| version: "1" | ||
|
|
||
| providers: | ||
| bailian: | ||
| # bl auth login --api-key <key> sets DASHSCOPE_API_KEY; --agentstudio-base-url <url> sets BAILIAN_BASE_URL | ||
| api_key: ${DASHSCOPE_API_KEY} | ||
| base_url: ${BAILIAN_BASE_URL} | ||
|
|
||
| defaults: | ||
| provider: bailian | ||
|
|
||
| environments: | ||
| dev: | ||
| config: | ||
| type: cloud | ||
| networking: | ||
| type: unrestricted | ||
|
|
||
| agents: | ||
| assistant: | ||
| description: "General-purpose assistant" | ||
| model: qwen3.7-max | ||
| instructions: | | ||
| You are a helpful assistant. | ||
| environment: dev | ||
| tools: | ||
| builtin: [bash, read, glob, grep] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
packages/commands/src/commands/managed-agent/_engine/address-utils.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| import type { ResourceAddress } from "@openagentpack/sdk"; | ||
|
|
||
| /** Full state address: provider.type.name */ | ||
| export function formatResourceAddress(address: ResourceAddress): string { | ||
| return `${address.provider}.${address.type}.${address.name}`; | ||
| } | ||
|
|
||
| /** CLI display short label: type.name (provider) */ | ||
| export function formatResourceLabel(address: ResourceAddress): string { | ||
| return `${address.type}.${address.name} (${address.provider})`; | ||
| } |
54 changes: 54 additions & 0 deletions
54
packages/commands/src/commands/managed-agent/_engine/config-loader.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| import { | ||
| createProjectRuntime, | ||
| type ProjectRuntimeContext, | ||
| resolveProjectConfig, | ||
| UserError, | ||
| } from "@openagentpack/sdk"; | ||
| import { ensureCredentials } from "./credentials.ts"; | ||
| import { loadFileState } from "./file-state-manager.ts"; | ||
| import { type HostContext, installSdkTransport } from "./transport.ts"; | ||
|
|
||
| export { CREDENTIALS_NOTE } from "./credentials.ts"; | ||
|
|
||
| /** | ||
| * Build a full ProjectRuntimeContext from a config file path — the standard | ||
| * entry point for agent commands that need the SDK engine. Mirrors OpenAgentPack | ||
| * CLI's buildCliRuntime: resolve config → load local state → assemble runtime. | ||
| * Takes the host context first so every SDK-engine command wires the | ||
| * instrumented transport (UA / tracking headers / verbose) by construction. | ||
| */ | ||
| export async function buildAgentRuntime( | ||
| host: HostContext, | ||
| filePath: string, | ||
| options: { | ||
| resolveEnv?: boolean; | ||
| projectName?: string; | ||
| statePath?: string; | ||
| } = {}, | ||
| ): Promise<ProjectRuntimeContext & { configPath: string }> { | ||
| installSdkTransport(host); | ||
| ensureCredentials(); | ||
| const { config, configPath, projectName } = await resolveProjectConfig(filePath, options); | ||
| const state = await loadFileState(configPath, options.statePath, projectName); | ||
| const ctx = createProjectRuntime({ | ||
| projectName, | ||
| config, | ||
| state, | ||
| configPath, | ||
| providers: config.providers, | ||
| }); | ||
| return { ...ctx, configPath }; | ||
| } | ||
|
|
||
| /** Ensure a user-supplied --provider value is actually configured in agents.yaml. */ | ||
| export function assertProviderConfigured( | ||
| ctx: ProjectRuntimeContext, | ||
| provider: string | undefined, | ||
| ): void { | ||
| if (!provider || provider === "all") return; | ||
| if (ctx.providers.has(provider)) return; | ||
| const available = Array.from(ctx.providers.keys()).join(", ") || "none"; | ||
| throw new UserError( | ||
| `Provider '${provider}' is not configured. Available providers: ${available}.`, | ||
| ); | ||
| } |
23 changes: 23 additions & 0 deletions
23
packages/commands/src/commands/managed-agent/_engine/console-capture.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| /** | ||
| * Redirect `console.log` / `console.info` to stderr while `fn` runs. | ||
| * | ||
| * The OpenAgentPack SDK's provider adapters emit progress/debug logging via | ||
| * `console.log` (e.g. `[skill-upload]`), which would corrupt bl's stdout data | ||
| * channel in `--output json` mode. Wrapping SDK calls that may log keeps stdout | ||
| * a clean data channel. Restores the originals on completion. | ||
| */ | ||
| export async function withStdoutProtected<T>(fn: () => Promise<T>): Promise<T> { | ||
| const originalLog = console.log; | ||
| const originalInfo = console.info; | ||
| const toStderr = (...args: unknown[]): void => { | ||
| process.stderr.write(`${args.map((arg) => String(arg)).join(" ")}\n`); | ||
| }; | ||
| console.log = toStderr; | ||
| console.info = toStderr; | ||
| try { | ||
| return await fn(); | ||
| } finally { | ||
| console.log = originalLog; | ||
| console.info = originalInfo; | ||
| } | ||
| } |
63 changes: 63 additions & 0 deletions
63
packages/commands/src/commands/managed-agent/_engine/credentials.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| import { bootstrapRuntimeCredentialsSync } from "@openagentpack/sdk"; | ||
| import { readConfigFile } from "bailian-cli-core"; | ||
|
|
||
| let bootstrapped = false; | ||
|
|
||
| /** | ||
| * Shared `--help` note documenting where agent commands get provider | ||
| * credentials. Mirrors the credential-source hint bl's native commands surface | ||
| * (knowledge / usage / token-plan), adapted for the SDK's env-based resolution | ||
| * and the {@link bridgeBailianCredentials} fallback. Attach to every command | ||
| * that loads agents.yaml. `bl` prefix is safe: agent commands ship on `bl` only. | ||
| */ | ||
| export const CREDENTIALS_NOTE = [ | ||
| "Credentials come from the env vars referenced in agents.yaml (e.g. ${DASHSCOPE_API_KEY}, ${BAILIAN_BASE_URL}).", | ||
| "For the bailian provider, bl fills these from your login as a fallback: `bl auth login --api-key <key> --agentstudio-base-url <url>`.", | ||
| ]; | ||
|
|
||
| /** | ||
| * Bridge bl's own login state into the env vars the OpenAgentPack SDK reads for | ||
| * the bailian provider. bl persists `api_key` / `agentstudio_base_url` in | ||
| * `~/.bailian/config.json` (via `bl auth login`); mirror them onto | ||
| * `DASHSCOPE_API_KEY` / `BAILIAN_BASE_URL` so users don't have to re-declare the | ||
| * same credentials for `bl managed-agent *`. `workspace_id` is still bridged for configs | ||
| * that predate the base_url flow (the SDK accepts either). | ||
| * | ||
| * Lowest priority: only fills a var that is still unset, so anything already in | ||
| * the environment (shell export, `.env`, or `~/.agents/config.json` — which the | ||
| * SDK bootstrap has already applied) wins. Missing values are left alone; the | ||
| * SDK surfaces its own error when interpolation can't resolve, and non-bailian | ||
| * providers (claude/qoder/ark) don't need DashScope credentials at all. | ||
| */ | ||
| export function bridgeBailianCredentials(): void { | ||
| const file = readConfigFile(); | ||
| if (!process.env.DASHSCOPE_API_KEY?.trim() && file.api_key) { | ||
| process.env.DASHSCOPE_API_KEY = file.api_key; | ||
| } | ||
| if (!process.env.BAILIAN_BASE_URL?.trim() && file.agentstudio_base_url) { | ||
| process.env.BAILIAN_BASE_URL = file.agentstudio_base_url; | ||
| } | ||
| if (!process.env.BAILIAN_WORKSPACE_ID?.trim() && file.workspace_id) { | ||
| process.env.BAILIAN_WORKSPACE_ID = file.workspace_id; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Lazily load `.env` and `~/.agents/config.json` into `process.env` so the | ||
| * OpenAgentPack SDK can resolve provider credentials (e.g. DASHSCOPE_API_KEY, | ||
| * BAILIAN_WORKSPACE_ID), then bridge bl's own config as a fallback. Safe to call | ||
| * repeatedly — only the first call does I/O. | ||
| * | ||
| * Effective precedence for bailian provider fields: | ||
| * ~/.agents/config.json > shell env > .env > ~/.bailian/config.json | ||
| * | ||
| * NOTE: agent commands declare `auth: "none"` and let the SDK own credential | ||
| * resolution. The bl-config bridge is a best-effort fallback; it never overrides | ||
| * a value the SDK bootstrap already resolved. | ||
| */ | ||
| export function ensureCredentials(): void { | ||
| if (bootstrapped) return; | ||
| bootstrapped = true; | ||
| bootstrapRuntimeCredentialsSync(); | ||
| bridgeBailianCredentials(); | ||
| } |
57 changes: 57 additions & 0 deletions
57
packages/commands/src/commands/managed-agent/_engine/errors.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| import { UserError } from "@openagentpack/sdk"; | ||
| import { type ApiErrorBody, BailianError, ExitCode, mapApiError } from "bailian-cli-core"; | ||
|
|
||
| /** | ||
| * Structural shape of the SDK's `ApiError` (thrown by provider clients on HTTP | ||
| * 4xx/5xx). Matched on fields instead of `instanceof` because the installed SDK | ||
| * version does not export the class yet, and structural matching keeps this | ||
| * check stable across SDK versions either way. | ||
| */ | ||
| interface SdkApiErrorLike extends Error { | ||
| statusCode: number; | ||
| responseBody: string; | ||
| } | ||
|
|
||
| function isSdkApiError(error: Error): error is SdkApiErrorLike { | ||
| const candidate = error as Partial<SdkApiErrorLike>; | ||
| return typeof candidate.statusCode === "number" && typeof candidate.responseBody === "string"; | ||
| } | ||
|
|
||
| /** | ||
| * The SDK embeds the raw response body in its error message; recover the | ||
| * structured fields (message / code / request_id) when the body is JSON so | ||
| * `mapApiError` surfaces a clean server message plus api metadata. Non-JSON | ||
| * bodies pass through verbatim as the message. | ||
| */ | ||
| function parseSdkResponseBody(raw: string): ApiErrorBody { | ||
| try { | ||
| const parsed: unknown = JSON.parse(raw); | ||
| if (parsed && typeof parsed === "object") return parsed as ApiErrorBody; | ||
| } catch { | ||
| /* non-JSON body */ | ||
| } | ||
| return { message: raw.trim() || undefined }; | ||
| } | ||
|
|
||
| /** | ||
| * Run an SDK-backed operation, translating SDK error types into BailianError so | ||
| * bl's error handler produces the right exit code and hint formatting. | ||
| * SDK `UserError` → USAGE; SDK `ApiError` (server HTTP error) → GENERAL via | ||
| * `mapApiError` (server message passed through verbatim, with | ||
| * httpStatus/apiCode/requestId metadata for --output json); any other Error → | ||
| * GENERAL (message passed through, per bl's "don't translate server errors" | ||
| * boundary). | ||
| */ | ||
| export async function withAgentErrors<T>(fn: () => Promise<T>): Promise<T> { | ||
| try { | ||
| return await fn(); | ||
| } catch (error) { | ||
| if (error instanceof BailianError) throw error; | ||
| if (error instanceof UserError) throw new BailianError(error.message, ExitCode.USAGE); | ||
| if (error instanceof Error && isSdkApiError(error)) { | ||
| throw mapApiError(error.statusCode, parseSdkResponseBody(error.responseBody)); | ||
| } | ||
| if (error instanceof Error) throw new BailianError(error.message, ExitCode.GENERAL); | ||
| throw error; | ||
| } | ||
| } |
10 changes: 10 additions & 0 deletions
10
packages/commands/src/commands/managed-agent/_engine/feedback.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| import type { RuntimeFeedbackEvent } from "@openagentpack/sdk"; | ||
|
|
||
| /** | ||
| * Render SDK runtime feedback to stderr, keeping stdout a clean data channel. | ||
| * Used as the `onFeedback` sink for plan/apply so progress messages don't mix | ||
| * with structured output. | ||
| */ | ||
| export function renderAgentFeedback(event: RuntimeFeedbackEvent): void { | ||
| process.stderr.write(`${event.message}\n`); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[P1] SDK 的 Node 版本破坏现有发布契约
当前引入的
@openagentpack/sdk声明engines.node >=22,但bailian-cli、bailian-cli-commands和 README 仍承诺 Node >=18.17。Agent commands 又通过 commands index 静态导出,SDK 没有被安全隔离到bl cma执行时才加载,因此 Node 18/20 下可能影响整个bl的启动。请先统一运行时契约:要么让 SDK 支持并验证现有 Node 范围,要么同步提升所有发布包、README、CI 和发布检查的 Node 下限。需要增加最低支持 Node 版本下的安装及
bl --helpsmoke。