70 Commits

Author SHA1 Message Date
can1357 a5673c90f8 feat(ai): removed legacy Google interactions routing from AI providers
- Removed the Google Interactions transport and deleted interaction-specific request options from the shared AI stream typing/API surface.
- Simplified Google provider routing to eliminate interactions auto-selection logic and keep `streamGoogle` on the `:streamGenerateContent` path.
- Updated Vertex request handling to use resolved stream hosts without `/interactions`/`Api-Revision` and removed related interaction constants.
- Deleted obsolete Interactions tests and updated remaining Google stream tests to no longer reference `useInteractionsApi`/`storeInteraction`/`previousInteractionId`.
2026-07-13 18:43:52 +02:00
can1357 d01bf079ef feat(ai): added google interactions api transport
- Added `google-interactions.ts` streaming the Gemini Interactions API (model-mode steps, thought/tool/text deltas, usage, `previous_interaction_id` lineage) shared by the direct Google and Vertex providers.
- Routed `streamGoogle` and `streamGoogleVertex` through `resolveInteractionDispatch`, defaulting Gemini 3+ onto Interactions (official endpoint for direct Google, bearer/ADC for Vertex) with transparent `:streamGenerateContent` fallback and a `useInteractionsApi: false` opt-out.
- Added explicit Vertex bearer support in `google-auth.ts` via `GOOGLE_CLOUD_ACCESS_TOKEN`/`CLOUDSDK_AUTH_ACCESS_TOKEN` plus a `hasVertexBearerCredentialsHint` probe gating the auto-default.
- Added `useInteractionsApi`/`storeInteraction`/`previousInteractionId` to `StreamOptions` and `GoogleSharedStreamOptions`, threading them through `mapOptionsForApi`.
- Added `google-interactions` coverage and pinned the existing generateContent assertions with `useInteractionsApi: false`.
2026-06-30 06:37:04 +02:00
can1357 c3f7e849e5 refactor: centralized AI error handling into a dedicated module
- Migrated 288 lines of scattered error classification logic from `utils/error-id.ts` into a cohesive `packages/ai/src/error/` module with 13 specialized submodules covering flags, classes, OAuth, providers, rate-limiting, and finalization.
- Replaced 100+ generic `Error` throws across 60+ provider and registry files with semantic `AIError.*` classes (e.g., `AIError.MissingApiKeyError`, `AIError.OAuthError`, `AIError.ProviderResponseError`), improving error diagnostics and retry logic.
- Consolidated error utility imports from `pi-utils` and scattered classification functions into a single `AIError` namespace, reducing coupling and simplifying error handling across all packages.
2026-06-27 10:44:13 +02:00
can1357 4e24a79002 feat(ai): added AWS auth and Bedrock stream decoding with SigV4
- Removed deprecated AWS/Google client and proxy dependencies from root and AI package manifests.
- Added AWS credential chaining, SigV4 signing, and Bedrock stream decoding with region fallback and CRC checks.
- Added local Google type mirrors and migrated providers to request plans with SSE fetch and token-based auth.
- Updated AI changelog with auth/stream fixes, and added fetch-stub and SigV4/event-stream tests.
2026-05-17 02:36:43 +02:00
can1357 1b47cd3042 feat(ai): fetch overrides
- Introduced `FetchImpl` type with optional `preconnect` to accept non-Bun fetch implementations without type errors.
- Applied the new type across all providers and `StreamOptions.fetch`.
- Added tests verifying fetch override routing for openai-completions, openai-responses, and fetchWithRetry.
2026-05-15 09:16:38 +02:00
can1357 1b76b60b17 feat(ai/providers): added configurable fetch overrides to AI provider request paths
- Introduced a `fetch` option on `StreamOptions` and threaded it through providers to let callers supply a custom request transport.
- Updated provider clients and direct HTTP calls across Anthropic, OpenAI, Azure, Google, GitLab Duo, Gemini CLI, Ollama, and Codex flows to use the injected fetch implementation.
- Extended retry helper options to accept a fetch override and preserved preconnect support from the selected fetch function.
2026-05-15 09:07:34 +02:00
can1357 f1f6516056 refactor: reorganized exports and removed obsolete helper branches
- Removed export leakage by demoting many helper and const symbols to module-local scope.
- Renamed underscore-prefixed internals and cache fields, then updated related references and `satisfies never` checks.
- Deleted obsolete logic branches and helpers, including harmony-stream interruption flow and unused benchmark runtime helpers.
- Updated Biome config and manifests by broadening lint coverage and removing an unused `@napi-rs/cli` dev dependency.
- Adjusted tests and utilities to use renamed test helpers and remove redundant private test-only helpers/locals.
2026-05-14 04:36:19 +02:00
can1357 4388bb9845 fix(ai/providers): used provider error messages for aborted stream terminations
- Anthropic streaming now populated output.errorMessage from refusal stop_details, including category and explanation when available.
- Stream handlers for Bedrock, Azure OpenAI, Google, Google Vertex, Google Gemini CLI, and OpenAI now threw output.errorMessage when stopReason indicated aborted or error instead of a generic unknown message.
2026-05-10 05:31:25 +02:00
can1357 8c323666be feat: added ordered systemPrompt arrays and normalized context prompts
- Converted systemPrompt APIs and state types to ordered `string[]` across agent, AI, and coding-agent surfaces.
- Added `normalizeSystemPrompts` and applied it to context normalization before building provider request payloads.
- Updated AI providers to emit separate normalized prompt blocks/messages instead of a single merged system prompt.
- Removed dedicated `projectPrompt` state and remapped that context into system-context buckets in session, dump, and token accounting.
- Aligned tests and changelogs to pass and assert `systemPrompt` as arrays with ordered prompt semantics.
2026-05-04 15:20:26 +02:00
can1357 aa6fdc2262 feat(ai): added richer ai usage parsing for reasoning and token counters
- Extended `Usage` typing with `reasoningTokens`, `cttl`, and `server` fields for richer token accounting.
- Added conditional `reasoningTokens` output across OpenAI and Google providers when token counts are positive.
- Updated `parseChunkUsage` and OpenAI usage parsing to prevent `reasoning_tokens` double-counting.
- Added Anthropic usage extras mapping to emit TTL and server tool counters only when non-zero.
- Preserved Anthropic cache TTL and server counters when later usage events omit those fields.
- Added usage-attribution tests for parse helpers, missing fields, and zero-value merge scenarios.
2026-04-30 02:13:37 +02:00
Muness Castle d8a4b6ba10 fix(ai): don't pass empty string apiKey to Google GenAI SDK (#362)
When no GEMINI_API_KEY is set, the fallback `|| ""` passes an empty
string to `new GoogleGenAI({ apiKey: "" })`. The SDK treats this as a
user-provided API key and warns that it will take precedence over
Vertex AI project/location auth, breaking Vertex users.

Pass `undefined` instead so the SDK falls through to Vertex auth.

Co-authored-by: Muness Castle <munesscastle@artium.ai>
2026-03-11 00:59:09 +01:00
Miroslav Drbal [ApoC] a2223cef60 fix: correct context window percentage and provider token mapping (#306)
The context fullness gauge was driven by output token count, causing
erratic jumps between turns (e.g. 84% -> 64%) with no compaction.

Status bar and estimateContextTokens now use calculatePromptTokens()
which returns input + cacheRead + cacheWrite — the actual input context
size. Previously both used a formula that included the final output token
count, which fluctuates with response length and is not part of the
context window for the current request.

isContextOverflow's usage-based fallback (z.ai silent overflow) was
also missing cacheWrite (cache_creation_input_tokens). Per Anthropic
docs the threshold is input + cache_read + cache_creation — all three.
Ref: https://platform.claude.com/docs/en/about-claude/pricing#long-context-pricing

google.ts and google-vertex.ts were double-counting cached tokens.
Gemini's promptTokenCount already includes cachedContentTokenCount, so
assigning input = promptTokenCount and cacheRead = cachedContentTokenCount
overcounted by cachedContentTokenCount on every cached request. Fixed
by subtracting first, matching the OpenAI convention:
  input = promptTokenCount - cachedContentTokenCount
  cacheRead = cachedContentTokenCount
  => input + cacheRead = promptTokenCount (total prompt, no double-count)
Ref: https://ai.google.dev/api/generate-content#v1beta.GenerateContentResponse.UsageMetadata

All other providers validated: amazon-bedrock (inputTokens is uncached
by API contract), openai-completions/responses/azure (already subtract
cached), kimi/gitlab-duo (delegate to correct implementations), cursor
(API exposes output tokens only — input stays 0 by design).

Co-authored-by: Miroslav Drbal <miroslav.drbal@gendigital.com>
2026-03-07 23:11:15 +01:00
can1357 8e3e0ebf9e feat: introduced Effort enum and ThinkingConfig for model-aware reasoning
- Introduced Effort enum and ThinkingConfig metadata for per-model reasoning capabilities with min/max effort levels.
- Migrated thinking level API from string-based ThinkingLevel to structured Effort enum across agent and AI packages.
- Added model-thinking module with effort mapping, policy application, and semantic versioning utilities for provider-specific thinking modes.
- Removed supportsXhigh() function and replaced effort clamping with model-aware validation using ThinkingConfig metadata.
- Expanded models.json with thinking configuration objects for 50+ models including Claude, Gemini, and OpenAI variants.
- Added Python analysis scripts for edit tool usage patterns and tool invocation stream processing.
2026-03-06 12:35:01 +01:00
can1357 924a70d9b4 refactor(ai): migrated Unicode handling to native toWellFormed() API
- Replaced custom `sanitizeSurrogates()` utility with native `String.prototype.toWellFormed()` across all provider implementations.
- Removed `sanitize-unicode.ts` utility module and all imports of `sanitizeSurrogates` function from 10 provider files.
- Updated Unicode surrogate handling in message conversion, response processing, and system prompt handling to use built-in JavaScript API.
- Documented removal of `sanitizeSurrogates()` utility and migration to native `toWellFormed()` in CHANGELOG.
2026-02-28 21:17:58 +01:00
can1357 a44f8f1f48 refactor(ai): restructured schema utilities into modular utils/schema package with unified strict mode enforcement
- Extracted schema utilities from typebox-helpers and google-shared into new modular utils/schema package with 17 exported functions.
- Consolidated OpenAI strict mode schema enforcement across codex, completions, and responses providers using unified adaptSchemaForStrict() helper.
- Refactored credential ranking from hardcoded Codex-specific logic to pluggable CredentialRankingStrategy pattern with provider implementations.
- Migrated 500+ lines of Google schema sanitization and normalization logic from google-shared.ts to dedicated utils/schema modules with expanded functionality.
2026-02-28 18:38:29 +01:00
can1357 17a61f5f0a feat(ai): add sampling controls across providers
Fixes #176
2026-02-26 10:23:45 +01:00
can1357 abf8c1efc9 refactor: unslop common utilities 2026-02-22 11:01:11 +01:00
can1357 5d32f93c0d fix(ai): improved error handling and provider robustness
- Added raw HTTP request dumps to error messages for 400 status codes.
- Implemented client-side retry for Anthropic streaming on transient errors.
- Preserved context by converting aborted assistant tool calls to text.
- Optimized model registry refresh in coding agent sessions.
2026-02-20 22:22:04 +01:00
can1357 57a3650cc6 fix: backport fixes from pi-mono (3635e45f..9f3eef65f)
packages/ai:
- feat: HTTP proxy support via environment variables
- fix: correct provider error message typo
- fix: filter deprecated OpenCode models from generation

packages/tui:
- fix: scrollback overwrite when appending lines past viewport
- fix: keep overlays centered across resizes
- fix: handle multi-line text in insertTextAtCursor()

packages/coding-agent:
- fix: add /model instruction to no models warning
- fix: active path highlighting in HTML exports
2026-01-27 19:47:36 +01:00
can1357 7bb2e1aa58 feat(port): ported pi-mono improvements + worker & patch improvements
- added Azure OpenAI Responses support and OpenRouter routing compat
- improved patch applicator diagnostics and subagent context propagation
- updated editor cursor handling, keybindings, and working message API
- refreshed porting sync metadata
2026-01-25 13:50:08 +01:00
can1357 779ca4872b style(deps): migrated from Prettier to Biome and updated formatting rules
- Removed Prettier configuration files (.prettierignore and .prettierrc) and migrated formatting to Biome.
- Updated Biome configuration from version 2.3.11 to 2.3.12 and changed arrowParentheses rule from 'always' to 'asNeeded'.
- Pinned @biomejs/biome dependency to exact version 2.3.12 in package.json and bun.lock.
- Applied consistent arrow function formatting across 489 files by removing unnecessary parentheses around single parameters.
- Removed blank lines after comment blocks and reorganized imports for consistency across the codebase.
2026-01-24 04:20:19 +01:00
can1357 f66e5dba9b build(config): refactored build and TypeScript configuration with Bun loaders
- Removed WASM generation script; use Bun `wasm?raw` loader for imports.
- Added bunfig.toml with loaders for `.md`, `.py`, and `.wasm?raw` text imports.
- Added types/assets/index.d.ts for global TypeScript module declarations.
- Unified TypeScript configuration with tsgo-based checking across monorepo.
- Removed build and WASM steps from install and publish pipelines.
2026-01-24 00:03:52 +01:00
can1357 0fe761dc4b build(deps): refactored TypeScript and package configuration across monorepo
- Added tsconfig.publish.json files to all packages with optimized publish-time configuration.
- Updated all package.json scripts with prepublishOnly hooks for correct type checking during publish.
- Added @oh-my-pi/omp-stats path mappings to root tsconfig.json for consistent imports.
- Added WASM generation script for photon module and integrated into install:dev script.
2026-01-23 13:46:06 +01:00
can1357 7b5af2dd9f refactor(build): migrated imports to path aliases with per-package tsconfig support
- converted relative imports to path aliases ($c/*, $ai/*, $tui/*, etc.) across all packages
- added per-package tsconfig.json with complete path mappings for runtime resolution
- set importModuleSpecifier to non-relative for IDE auto-import preferences
- updated dev script to run from monorepo root for consistent path resolution
2026-01-23 11:56:32 +01:00
can1357 aca0f03f41 feat(ai/providers): added duration and TTFT metrics to AI providers
- Added duration and TTFT (time to first token) metrics to AssistantMessage interface.
- Implemented performance tracking across all AI providers for streaming responses.
- Added timing capture in both success and error code paths for comprehensive metrics.
- Updated changelog to document new performance tracking capabilities.
2026-01-21 10:20:27 +01:00
can1357 1e79ba6cdc feat(ai): added custom headers and payload hooks to AI providers
- Added headers option to all providers for custom request headers.
- Added onPayload hook to observe provider request payloads before sending.
- Added strictResponsesPairing option for Azure OpenAI Responses API compatibility.
- Added originator option to loginOpenAICodex for custom OAuth flow identification.
- Enhanced AWS credential detection to support ECS task roles and IRSA web identity tokens.
2026-01-21 04:44:24 +01:00
can1357 d0333f1377 fix(ai/providers): fixed tool schema sanitization to only apply Google-specific transforms for Gemini models
- Fixed tool parameter schema sanitization to only apply Google-specific transformations for Gemini models.
- Added model parameter to convertTools function to enable conditional schema sanitization.
- Preserved original tool schemas for non-Gemini models running through Google providers.
2026-01-12 00:20:31 +01:00
can1357 b4782538c3 feat(ai): improved Google provider schema sanitization and added schema validation tests
- Exported sanitizeSchemaForGoogle utility function for public use.
- Added filtering of unsupported JSON Schema fields ($schema, $ref, $defs, format, examples, etc.) in Google provider schema sanitization.
- Added handling to ignore unsupported additionalProperties: false in Google provider.
- Removed format: date-time from timestamp type conversion in JTD to JSON Schema transformation.
- Added schema validation tests to verify tool schemas don't contain problematic JSON Schema features.
- Reorganized system prompt to display context, environment, and tools sections before discipline guidelines.
2026-01-12 00:07:36 +01:00
can1357 6e7ca2fc14 feat(coding-agent): added retry logic with exponential backoff
- Added retry logic with exponential backoff and model fallback for auto-compaction failures.
- Added support for pi/<role> model aliases and automatic model inheritance for subtasks.
- Enhanced error messages with retry-after timing from rate limit headers across all providers.
- Fixed image attachments being dropped when steering messages are queued during streaming.
- Changed edit tool to merge call and result displays into single block.
- Changed model override behavior to persist in settings when explicitly set via CLI.
2026-01-10 00:43:56 +01:00
can1357 5a63120dd3 feat(ai): add @oh-my-pi/pi-ai package with tool name prefixing
- Port pi-ai from upstream with Bun-first approach (source files, no dist)
- Add cli_ prefix to tool names in OAuth mode to avoid collisions
- Improve beta header handling with proper deduplication
- Convert codex-instructions.md to Bun text embed
- Strip .js extensions from all imports
2026-01-09 12:10:03 +01:00
can1357 5919b0df94 refactor: switched to upstream @mariozechner/pi-ai and removed unused packages
- Replaced local @oh-my-pi/pi-ai with upstream @mariozechner/pi-ai@0.37.4
- Added sessionId support to agent for provider caching (OpenAI Codex)
- Deleted packages/ai (now using upstream)
- Deleted packages/mom (unused)
- Deleted packages/web-ui (unused)
2026-01-06 22:25:24 +00:00
can1357 50ab6b8b1b style: removed .js extension from imports and added node: prefix to builtins 2026-01-03 05:12:25 +01:00
Mario Zechner bb96312cc0 Define own GoogleThinkingLevel type instead of importing from @google/genai
- Add GoogleThinkingLevel type mirroring Google's ThinkingLevel enum
- Update GoogleGeminiCliOptions and GoogleOptions to use our type
- Cast to any when assigning to Google SDK's ThinkingConfig
2025-12-30 22:42:25 +01:00
Mario Zechner 1d089cd803 getApiKeyFromEnv -> getEnvApiKey 2025-12-25 02:38:10 +01:00
Mario Zechner 886859197f WIP: remove setApiKey, resolveApiKey 2025-12-24 23:34:23 +01:00
Mario Zechner 944df61b30 feat(ai): add Google Cloud Code Assist provider
- Add new API type 'google-cloud-code-assist' for Gemini CLI / Antigravity auth
- Extract shared Google utilities to google-shared.ts
- Implement streaming provider for Cloud Code Assist endpoint
- Add 7 models: gemini-3-pro-high/low, gemini-3-flash, claude-sonnet/opus, gpt-oss

Models use OAuth authentication and have sh cost (uses Google account quota).
OAuth flow will be implemented in coding-agent in a follow-up.
2025-12-20 10:20:30 +01:00
Cyril c52096d869 fix(ai): prevent double API version path in Google provider URL 2025-12-19 20:41:07 +00:00
Mario Zechner c4441a8893 Merge pull request #221 from theBucky/fix/google-provider-baseurl
fix(ai): pass baseUrl to Google GenAI SDK via httpOptions
2025-12-18 15:40:05 +01:00
theBucky e4277bab19 fix(ai): pass baseUrl to Google GenAI SDK via httpOptions
Previously, when using 'google-generative-ai' API with a custom baseUrl
in models.json, the baseUrl was ignored and requests always went to the
default Google endpoint.

Now the provider correctly passes model.baseUrl to the SDK's
httpOptions.baseUrl, enabling use of custom endpoints or API proxies.

Fixes #216
2025-12-18 22:03:43 +08:00
Mario Zechner 2704ac8ca7 fix(ai): correct Gemini tool result format and improve type safety
- Fix tool result format for Gemini 3 Flash Preview compatibility
  - Use 'output' key for successful results (not 'result')
  - Use 'error' key for error results (not 'isError')
  - Per Google SDK documentation for FunctionResponse.response

- Improve type safety in google.ts provider
  - Add ImageContent import and use proper type guards
  - Replace 'as any' casts with proper typing
  - Import and use Schema type for tool parameters
  - Add proper typing for index deletion in error handler

- Add comprehensive test for Gemini 3 Flash tool calling
  - Tests successful tool call and result handling
  - Tests error tool result handling
  - Verifies fix for issue #213

Fixes #213
2025-12-18 13:43:39 +00:00
Markus Ylisiurunen 6fdd429584 Fix Gemini 3 Flash Preview thinking levels (#212)
* use the correct Gemini 3 Flash Preview thinking levels

* fix a build error

* add changelog entry

* regenerate models

* make less assumptions about future models
2025-12-18 13:03:28 +01:00
Markus Ylisiurunen 61e8d4f94e Support thinking level configuration for Gemini 3 Pro models (#176)
* support Google thinking level configuration for Gemini 3 Pro models

* relax model ID check for gemini 3 pro
2025-12-13 02:09:54 +01:00
Mario Zechner 83f099f282 Remove provider-level tool validation, add validateToolCall helper 2025-12-08 18:04:33 +01:00
Markus Ylisiurunen 1f33526e79 add option to skip provider tool call validation 2025-12-07 17:24:06 +02:00
Mario Zechner 92c654a872 Add totalTokens field to Usage type
- Added totalTokens field to Usage interface in pi-ai
- Anthropic: computed as input + output + cacheRead + cacheWrite
- OpenAI/Google: uses native total_tokens/totalTokenCount
- Fixed openai-completions to compute totalTokens when reasoning tokens present
- Updated calculateContextTokens() to use totalTokens field
- Added comprehensive test covering 13 providers

fixes #130
2025-12-06 22:46:02 +01:00
Mario Zechner de39f1f493 Add custom headers support for models.json
Fixes #39

- Added headers field to Model type (provider and model level)
- Model headers override provider headers when merged
- Supported in all APIs:
  - Anthropic: defaultHeaders
  - OpenAI (completions/responses): defaultHeaders
  - Google: httpOptions.headers
- Enables bypassing Cloudflare bot detection for proxied endpoints
- Updated documentation with examples

Also fixed:
- Mistral/Chutes syntax error (iif -> if)
- process.env.ANTHROPIC_API_KEY bug (use delete instead of = undefined)
2025-11-20 17:05:31 +01:00
Mario Zechner a11c1aa4ff Release v0.7.17 2025-11-18 17:49:12 +01:00
Mario Zechner 00d8286523 Handle FinishReason.NO_IMAGE and fix optional chaining
- Add NO_IMAGE to error finish reasons in Google provider
- Fix non-null assertion after optional chaining in Anthropic provider
- Migrate biome config to 2.3.5
- Ignore Tailwind CSS file from biome checks
- Bump all packages to version 0.6.0
2025-11-12 10:58:03 +01:00
Mario Zechner 84dcab219b Add image support in tool results across all providers
Tool results now use content blocks and can include both text and images.
All providers (Anthropic, Google, OpenAI Completions, OpenAI Responses)
correctly pass images from tool results to LLMs.

- Update ToolResultMessage type to use content blocks
- Add placeholder text for image-only tool results in Google/Anthropic
- OpenAI providers send tool result + follow-up user message with images
- Fix Anthropic JSON parsing for empty tool arguments
- Add comprehensive tests for image-only and text+image tool results
- Update README with tool result content blocks API
2025-11-12 10:45:56 +01:00
Mario Zechner 55dc0b6e08 Add timestamp to messages 2025-10-26 00:43:43 +02:00