Skip to content

feat(models): support Pi Coding Agent - #918

Merged
jeff-r2026 merged 1 commit into
Tencent:mainfrom
daoiqi:worktree-pi-model-distribution
Sep 30, 2026
Merged

jeff-r2026 merged 1 commit into
Tencent:mainfrom
daoiqi:worktree-pi-model-distribution

Conversation

@daoiqi

@daoiqi daoiqi commented Sep 30, 2026 •

Copy link
Copy Markdown
Collaborator

Pi becomes the sixth agent teamai models switch can point at a team gateway. TeamAI writes one provider into ~/.pi/agent/models.json, holding every catalog model; a member's other providers in that file are never touched.

  • Key is the profile ref (team:<id> / local:<id>), not the bare id — an id is unique only within one catalog file, so a root and a namespace profile may both be tokenhub, and a bare id would make the second switch silently overwrite the first gateway. Pi shows the profile's name.
  • One provider covers all three protocols. Pi resolves the api and URL per model: the first in Chat Completions, Responses, Anthropic order becomes the provider's own, and a model reached through another carries api and baseUrl itself. A model served both ways is registered once, preferring an OpenAI one; an anthropic-only group pins a Claude model to Anthropic Messages.
  • An environment key is written as $VAR, and settings.json is left alone, so the default model stays the member's choice. PI_CODING_AGENT_DIR is honored the way CODEX_HOME is.
  • Restore removes the key the last switch created, even after the catalog re-points the profile at a different ref. Restoring without --agent now covers Pi too.

Verification: tsc --noEmit and lint clean; full suite 5379 passed (three pre-existing flaky push-env cases fail identically on the base); full e2e 384 passed, with 4 new cases driving the built CLI against a scratch HOME; real pi 0.87.1 lists all three models under local:tokenhub with the member's own provider untouched and restore returning the file exactly.

Summary

Type of Change

  • Bug fix (non-breaking change that fixes an issue)
  • New feature (non-breaking change that adds functionality)
  • Breaking change (fix or feature causing existing behavior to change)
  • Documentation only
  • Refactor / internal cleanup

Test Plan

  • npx tsc --noEmit passes
  • npm run lint passes
  • npx vitest run passes
  • Added/updated tests for the change

Related Issues

Notes for Reviewers

Pi becomes the sixth agent `teamai models switch` can point at a team
gateway. TeamAI writes one provider into `~/.pi/agent/models.json`, holding
every catalog model; a member's other providers in that file are never
touched.

- **Key is the profile ref** (`team:<id>` / `local:<id>`), not the bare `id`
  — an `id` is unique only within one catalog file, so a root and a namespace
  profile may both be `tokenhub`, and a bare id would make the second switch
  silently overwrite the first gateway. Pi shows the profile's `name`.
- **One provider covers all three protocols.** Pi resolves the api and URL
  per model: the first in Chat Completions, Responses, Anthropic order
  becomes the provider's own, and a model reached through another carries
  `api` and `baseUrl` itself. A model served both ways is registered once,
  preferring an OpenAI one; an `anthropic`-only group pins a Claude model to
  Anthropic Messages.
- **An environment key is written as `$VAR`**, and `settings.json` is left
  alone, so the default model stays the member's choice.
  `PI_CODING_AGENT_DIR` is honored the way `CODEX_HOME` is.
- **Restore** removes the key the last switch created, even after the catalog
  re-points the profile at a different ref. Restoring without `--agent` now
  covers Pi too.

Rebased onto current `origin/main` (3a9a24a). The only conflict was the
`./profile.js` import in `src/models/switch.ts`, where this change adds
`ModelProtocol` and Tencent#894 adds `LocalConfig` / `sameTeamIdentity`; resolved as
the union of both, and `git range-diff` confirms that line is the sole
difference from the original commit.

Verification on the rebased tree: `tsc --noEmit` and `lint` clean. Unit
suite 5501 passed (the same three `push-env` cases fail on clean
`origin/main`, verified in a separate worktree). Pi e2e suite 4 passed. Real
built CLI against a scratch `HOME`: `models add` -> `models switch --agent pi`
writes one provider keyed `local:tokenhub` with `settings.json` untouched;
`models list` reports Pi active; `models restore --agent pi` returns the file
to `{}`; a pre-existing user provider under the same ref is refused with
`pi already has a user-owned provider named local:tokenhub` and left byte
for byte intact; `PI_CODING_AGENT_DIR` redirects the write to a custom
directory.
@jeff-r2026 jeff-r2026 self-assigned this Sep 30, 2026
@github-actions

Copy link
Copy Markdown

Findings

  • [P1 blocking] Preserve an existing target provider when switching profiles — src/models/switch.ts:994. Once Pi has any managed state, collision detection is skipped. If TeamAI manages team:a and the user already owns local:b, switching to local:b overwrites that provider, then restore deletes it permanently. Pi must check collisions whenever the desired key differs from the currently managed key.
  • [P2 non-blocking] Make repeated Pi switches idempotent — src/models/switch.ts:777. The desired snapshot contains the synthetic id, while the snapshot read from disk never does, so equal(current, desired) can never report unchanged. Reapplying the same profile unnecessarily rewrites models.json and reports switched; compare using Pi’s managed-field normalization instead.

The PR description’s testing record is sufficient, including representative built-CLI and real-Pi verification.

@daoiqi
daoiqi force-pushed the worktree-pi-model-distribution branch from eca96df to 6ee0efb Compare September 30, 2026 02:42
@github-actions

Copy link
Copy Markdown

Findings

  • [P1 blocking] Preserve an existing target provider when switching profiles — src/models/switch.ts:1031. Collision checks are still skipped whenever Pi already has managed state. Switching from managed key team:a to a user-owned local:b overwrites local:b; restore then deletes it permanently.
  • [P2 non-blocking] Make repeated Pi switches idempotent — src/models/switch.ts:1022. The disk snapshot omits synthetic id, while desired includes it, so exact equality never succeeds. Reapplying an unchanged profile rewrites models.json and reports switched.
  • [P2 non-blocking] Preserve Pi’s key in interrupted-switch recovery state — src/models/switch.ts:1045. On a second switch, pending.before receives the disk snapshot without id; if interrupted before the file write, recoverPending cannot locate the old provider from that snapshot and permanently skips future switch/restore attempts as externally modified.

The PR description includes sufficient representative real-CLI and real-Pi verification.

@jeff-r2026
jeff-r2026 merged commit a8ab138 into Tencent:main Sep 30, 2026
13 checks passed
Walternater pushed a commit to Walternater/teamai-cli that referenced this pull request Nov 2, 2026
Pi becomes the sixth agent `teamai models switch` can point at a team
gateway. TeamAI writes one provider into `~/.pi/agent/models.json`, holding
every catalog model; a member's other providers in that file are never
touched.

- **Key is the profile ref** (`team:<id>` / `local:<id>`), not the bare `id`
  — an `id` is unique only within one catalog file, so a root and a namespace
  profile may both be `tokenhub`, and a bare id would make the second switch
  silently overwrite the first gateway. Pi shows the profile's `name`.
- **One provider covers all three protocols.** Pi resolves the api and URL
  per model: the first in Chat Completions, Responses, Anthropic order
  becomes the provider's own, and a model reached through another carries
  `api` and `baseUrl` itself. A model served both ways is registered once,
  preferring an OpenAI one; an `anthropic`-only group pins a Claude model to
  Anthropic Messages.
- **An environment key is written as `$VAR`**, and `settings.json` is left
  alone, so the default model stays the member's choice.
  `PI_CODING_AGENT_DIR` is honored the way `CODEX_HOME` is.
- **Restore** removes the key the last switch created, even after the catalog
  re-points the profile at a different ref. Restoring without `--agent` now
  covers Pi too.

Rebased onto current `origin/main` (3a9a24a). The only conflict was the
`./profile.js` import in `src/models/switch.ts`, where this change adds
`ModelProtocol` and Tencent#894 adds `LocalConfig` / `sameTeamIdentity`; resolved as
the union of both, and `git range-diff` confirms that line is the sole
difference from the original commit.

Verification on the rebased tree: `tsc --noEmit` and `lint` clean. Unit
suite 5501 passed (the same three `push-env` cases fail on clean
`origin/main`, verified in a separate worktree). Pi e2e suite 4 passed. Real
built CLI against a scratch `HOME`: `models add` -> `models switch --agent pi`
writes one provider keyed `local:tokenhub` with `settings.json` untouched;
`models list` reports Pi active; `models restore --agent pi` returns the file
to `{}`; a pre-existing user provider under the same ref is refused with
`pi already has a user-owned provider named local:tokenhub` and left byte
for byte intact; `PI_CODING_AGENT_DIR` redirects the write to a custom
directory.
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.

2 participants