Skip to content

integrations(agents): align to the cross-language agent-tool contract#66

Merged
wudidapaopao merged 2 commits into
chdb-io:mainfrom
ShawnChen-Sirius:feat/agents-contract
Jul 2, 2026
Merged

integrations(agents): align to the cross-language agent-tool contract#66
wudidapaopao merged 2 commits into
chdb-io:mainfrom
ShawnChen-Sirius:feat/agents-contract

Conversation

@ShawnChen-Sirius

@ShawnChen-Sirius ShawnChen-Sirius commented Jul 2, 2026

Copy link
Copy Markdown
Contributor

Brings chdb-node's agent tools into conformance with the cross-language agent-tool contract introduced in chdb-io/chdb (chdb/agents/CONTRACT.md, see chdb-io/chdb#597). The Python chdb.agents.ChDBTool is the reference; this makes the TypeScript binding implement the same methods with the same semantics, verified against the same fixture.

Why

The existing chdb/ai-sdk and chdb/mastra tools predated the contract: only 3 methods (chdbQuery/chdbListTables/chdbDescribeSource), different names, no parameter binding, no shared error classification / 64-bit policy / resource controls, and no conformance runner. That's a parallel implementation that can drift from Python — exactly what the contract exists to prevent.

The aligned base (integrations/agents/, mirrors chdb.agents)

  • tool.mjsChDBTool with the full canonical method set: run_select_query, list_databases, list_tables, describe_table, get_sample_data, list_functions, attach_file, plus toolSpecs() / call().
  • P1 read-only enforced by the engine (SET readonly=2, immutable, opt out with readOnly:false).
  • P2 values bound as {name:Type} params (never concatenated); identifiers via quoteIdent (backtick-double-escape, reject NUL).
  • P3 maxRows + maxBytes guard with a truncated flag (never silent).
  • P4 call() returns the { ok, result } | { ok, error } envelope; direct methods reject with typed errors. errors.mjs uses the same regex + code map as the reference (164→READONLY, 62→SYNTAX, 46/47/60/81/115→UNKNOWN_*).
  • P5 max_execution_time, file_allowlist, attachments + attach_file.
  • 64-bit ints returned exact (output_format_json_quote_64bit_integers=1).
  • The Python-only dataframe_query (in-process pandas via Python()) is intentionally omitted — a language-specific capability outside the cross-language base, per the contract.

Frameworks are now thin shims

ai-sdk.mjs and mastra.mjs build the identical canonical tool set from one shared descriptor list and delegate every call to ChDBTool.call(), so both frameworks (and Python) behave identically. Removed chdb-tool-core.mjs. New framework-agnostic entry point: import { ChDBTool } from 'chdb/agents'.

Contract + conformance vendored into chdb-node

  • integrations/agents/CONTRACT.md and conformance/{cases.jsonl,fixtures,README} are synced mirrors of the canonical files upstream (provenance noted at the top of each).
  • test/v3/integrations/agents-conformance.test.ts is the TypeScript twin of the Python reference runner, over the same cases.jsonl.

Verification

  • Agents conformance: 16/16 cases pass (P1–P5, introspection, safety, catalog).
  • Full integration suite: 46/46 (agents-conformance, ai-sdk, mastra, chdb-vector, chdb-store, hypequery).
  • No dependency changes (lockfile untouched); the base uses only the existing engine API, adapters use the existing ai/@mastra/core/zod optional peers.

Note: the pre-existing layer3/{arrow-input,stream} local failures are environmental (they fail identically on a clean main here) and unrelated to this change.

🤖 Generated with Claude Code

Note

Implement cross-language agent-tool contract for chDB with typed errors, safety helpers, and seven canonical tools

  • Adds a new ./agents subpath export (agents/) implementing the full chDB agent-tool contract: ChDBTool with engine-enforced read-only mode, parameter binding, result capping with truncation signaling, and structured error envelopes.
  • Introduces typed error classes (ChDBError, ChDBReadOnlyError, ChDBSyntaxError, ChDBUnknownObjectError) with a parseError() function that maps engine exception codes and types to the correct subclass.
  • Adds safety utilities (quoteIdent, quoteString, pathAllowed, scanFilePaths) for identifier quoting, string literal escaping, and file path allowlist enforcement.
  • Reworks the Vercel AI SDK (ai-sdk.mjs) and Mastra (mastra.mjs) adapters to expose seven canonical contract tools (run_select_query, list_databases, list_tables, describe_table, get_sample_data, list_functions, attach_file) with ToolEnvelope semantics and a close() method.
  • Removes the previous chdb-tool-core.mjs three-tool executor; adapters now delegate entirely to ChDBTool.call().
  • Risk: Behavioral change — adapter tool names and result shapes are now contract-defined; callers relying on previous bespoke tool keys or result types will break.

Macroscope summarized d97d2df.

chdb-node's agent tools predated chdb's cross-language agent-tool contract
(chdb-io/chdb chdb/agents/CONTRACT.md) and only implemented a 3-method
subset with different names, no param binding, and no shared error/64-bit/
resource semantics. This ports the contract's base faithfully so the TS
binding is contract-conformant and the Python/TS surfaces stay in lock-step.

Base (integrations/agents/, mirrors chdb.agents):
- tool.mjs: ChDBTool with the full canonical method set — run_select_query,
  list_databases, list_tables, describe_table, get_sample_data,
  list_functions, attach_file — plus toolSpecs()/call().
- P1 read-only via SET readonly=2 (immutable, opt out with readOnly:false);
  P2 values bound as {name:Type} params, identifiers via quoteIdent; P3
  maxRows + maxBytes with a truncated flag; P4 call() returns the
  {ok,result}|{ok,error} envelope and query() rejects with typed errors.
- errors.mjs: parseError with the same regex + code map as the reference
  (164->READONLY, 62->SYNTAX, 46/47/60/81/115->UNKNOWN_*).
- safety.mjs: quoteIdent/quoteString/pathAllowed/scanFilePaths.
- 64-bit ints returned exact (output_format_json_quote_64bit_integers=1);
  P5 max_execution_time / file_allowlist / attachments+attach_file.
- The Python-only dataframe_query is intentionally omitted (language-specific,
  outside the cross-language base).

Framework adapters are now thin shims over the base:
- ai-sdk.mjs and mastra.mjs build the same canonical tool set from a shared
  descriptor list and delegate every call to ChDBTool.call(), so both
  frameworks expose identical behavior. Removed chdb-tool-core.mjs.
- New framework-agnostic entry point: import { ChDBTool } from 'chdb/agents'.

Contract + conformance vendored:
- integrations/agents/CONTRACT.md and conformance/{cases.jsonl,fixtures,README}
  are synced mirrors of the canonical files in chdb-io/chdb.
- test/v3/integrations/agents-conformance.test.ts is the TS twin of the Python
  reference runner over the same fixture: 16/16 cases pass. Full integration
  suite 46/46.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@ShawnChen-Sirius ShawnChen-Sirius requested a review from Copilot July 2, 2026 00:49
@ShawnChen-Sirius

Copy link
Copy Markdown
Contributor Author

@chibugai, review it.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Aligns chdb-node’s agent integrations (AI SDK + Mastra) to the cross-language ChDBTool contract used by chdb-io/chdb, including a shared conformance fixture and a new framework-agnostic chdb/agents entry point.

Changes:

  • Introduces integrations/agents/ChDBTool implementing the canonical contract tool surface (P1–P5), plus shared error parsing and safety helpers.
  • Refactors integrations/ai-sdk and integrations/mastra adapters into thin shims over ChDBTool.call() using a shared descriptor list.
  • Vendors the contract + conformance fixture and adds a TypeScript conformance runner test.

Reviewed changes

Copilot reviewed 22 out of 22 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
test/v3/integrations/mastra.test.ts Updates Mastra adapter tests to assert canonical tool names and envelope-shaped results.
test/v3/integrations/ai-sdk.test.ts Updates AI SDK adapter tests for canonical toolset, param binding, read-only envelope errors, and 64-bit JSON behavior.
test/v3/integrations/agents-conformance.test.ts Adds TS conformance runner that executes the vendored cases.jsonl against ChDBTool.
package.json Adds ./agents subpath export for the new framework-agnostic entry point.
integrations/mastra.mjs Replaces legacy 3-tool implementation with canonical toolset built from descriptors and delegated to ChDBTool.call().
integrations/mastra.d.mts Updates Mastra typings to expose canonical tool names and envelope return type; re-exports agent error/tool types.
integrations/chdb-tool-core.mjs Removes superseded legacy framework-agnostic executor core.
integrations/ai-sdk.mjs Replaces legacy 3-tool implementation with canonical toolset built from descriptors and delegated to ChDBTool.call().
integrations/ai-sdk.d.mts Updates AI SDK typings to expose canonical tool names and envelope return type; re-exports agent error/tool types.
integrations/agents/tool.mjs Adds the core ChDBTool implementation (query, introspection, attachments, call-envelope dispatch).
integrations/agents/tool.d.mts Adds TypeScript typings for ChDBTool, QueryResult, and the tool-dispatch envelope.
integrations/agents/safety.mjs Adds shared quoting + allowlist helpers (quoteIdent, quoteString, scanFilePaths, pathAllowed).
integrations/agents/safety.d.mts Adds typings for safety helpers.
integrations/agents/index.mjs Adds chdb/agents entry point exporting tool, errors, and safety helpers.
integrations/agents/index.d.mts Adds typings for chdb/agents entry point exports.
integrations/agents/framework.mjs Adds shared framework glue: resolveTool() and canonical descriptor list with schemas.
integrations/agents/errors.mjs Adds shared error classes + parseError() to classify engine errors into typed failures.
integrations/agents/errors.d.mts Adds typings for error objects and error classes.
integrations/agents/CONTRACT.md Vendors the upstream cross-language agent-tool contract doc.
integrations/agents/conformance/README.md Vendors upstream conformance fixture documentation.
integrations/agents/conformance/fixtures/sample.csv Adds vendored fixture data for conformance cases.
integrations/agents/conformance/cases.jsonl Adds vendored conformance cases executed by the TS runner.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread integrations/agents/tool.mjs
Comment thread integrations/agents/tool.mjs
Comment thread integrations/agents/tool.mjs
Comment thread integrations/ai-sdk.mjs
Comment thread integrations/mastra.mjs
Comment thread integrations/agents/safety.mjs
Comment thread integrations/agents/safety.mjs
Comment thread integrations/agents/tool.mjs
Comment thread integrations/agents/tool.mjs Outdated
…or paths

Addresses the resource-lifetime review findings on the agent tools (the
behavioral pillars already match the reference, so only the leaks are fixed):

- ChDBTool constructor: if setup (SET queries / attachment views / the
  read-only lock) throws after the owned Session is created, close that
  Session before re-throwing — the constructor never returns, so the caller
  otherwise has no handle to close() and the temp dir + native handle leak.
- ai-sdk / mastra chdbTools(): expose a non-enumerable close() on the
  returned toolset (and on chdbQueryTool's tool) that closes the Session the
  toolset owns. Non-enumerable so the frameworks don't treat it as a tool; a
  no-op when the caller passed their own session/tool. Types + docs updated.

Adds test/v3/integrations/agents-tool.test.ts covering the constructor
error-path cleanup, close() idempotency, caller-owned sessions being left
open, and the non-enumerable toolset close(). Integration suite 50/50.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@wudidapaopao wudidapaopao merged commit 62febb2 into chdb-io:main Jul 2, 2026
18 checks passed
ShawnChen-Sirius added a commit that referenced this pull request Jul 7, 2026
…w integrations

Additive minor release on top of 3.1.0 (2026-06-24). No breaking changes;
existing 3.1.0 APIs continue to work unchanged. libchdb bits are unchanged
(chdb-core v26.5.1-rc.1 packaged as @chdb/lib-* 26.5.2 — no republish
needed, the subpackage-matrix job will idempotent-skip).

## What's new

### Layer 3 fluent query builder SDK (#55, #59)

First-class typed fluent query API — Kysely-/Drizzle-style:

    const db = connect({ session })
    const rows = await db
      .from('users')
      .where(r => r.age.gt(18))
      .select('id', 'name')
      .execute()

- `src/layer3/{builder,execute,compile,dialect,introspection,connect,types,codegen}/`
- `chdb-gen-types` CLI — generates row types from a schema (Drizzle /
  Prisma / raw SQL sources)
- `.stream()` terminal with server-side parameter binding
- Arrow C Data Interface support: register JS columnar data as
  `arrowstream()` tables
- 12 new test files under `test/v3/layer3/`

### Cross-language agent-tool contract + 2 framework adapters (#61, #66)

- `chdb/ai-sdk` — Vercel AI SDK tools (`generateText`/`streamText`)
- `chdb/mastra` — Mastra tools + `ChDBVector` (HNSW RAG store) + `ChDBStore`
- Both are thin wrappers over `ChDBTool.call()` from `integrations/agents/`,
  which implements the cross-language contract vendored from `chdb-io/chdb`
  (`chdb/agents/CONTRACT.md`, chdb-io/chdb#597).
- 7 canonical tools: `run_select_query` / `list_databases` / `list_tables` /
  `describe_table` / `get_sample_data` / `list_functions` / `attach_file`
- 5 pillars enforced: engine-level readonly=2 (P1), value-vs-identifier
  separation (P2), truncation flag (P3), error envelope for model
  self-correction (P4), resource/source controls (P5)
- Behavior verified against the shared `conformance/cases.jsonl` fixture

### `chdb/hypequery` DatabaseAdapter (#63, #65)

Lets `@hypequery/clickhouse` query builder run on embedded chDB instead
of a remote ClickHouse server. Uses hypequery's own `substituteParameters`
(imported, not copied) so rendered SQL stays byte-identical to hypequery's
built-in HTTP adapter.

### `chdb/connection` stabilization for @clickhouse/client 1.23.0

- `peerDependency: "@clickhouse/client: >=1.23.0"` (optional) — old client
  triggers a clear npm warning instead of a runtime TS error
- Parity runner switched to track `ClickHouse/clickhouse-js` release tag
  `client-1.23.0` (per the sync-policy table; #52 shipped the initial hook)
- Runner auto-detects upstream Vitest layout (pre-/post-clickhouse-js#931)
- Runner re-synced when clickhouse-js reorganized into a monorepo (#62)

### AI-discovery docs (#60)

- Top-level `AGENTS.md` + `llms-full.txt`
- Per-subpath `AGENTS.md` (`src/layer3/`, `src/connection/`, etc.)
- All packaged in `files` so agents crawling the installed tarball find them

## Fixes / infrastructure

- Bind `null` / `undefined` query params as SQL `\N` (contributor
  @dfrankland, #68) — matches `@clickhouse/client` behavior
- `close()`-mid-flight engine abort + ResultSet teardown race (#56)
- `ChDBVector` HNSW granularity / filter binding / dimension guard (#61)
- Release workflow: publish → `unverified` dist-tag → clean-install verify
  matrix → promote to `latest` (#64). A broken release now never reaches
  `latest` users.
- CI: cache deps + harden npm registry fetch against transient resets

## User-facing installation matrix

```
npm install chdb                                    # base + Layer 3 SDK
npm install chdb @clickhouse/client                 # + chdb/connection
npm install chdb @hypequery/clickhouse              # + chdb/hypequery
npm install chdb ai zod                             # + chdb/ai-sdk
npm install chdb @mastra/core zod                   # + chdb/mastra
```

Native libchdb: chdb-core v26.5.1-rc.1 (packaged as @chdb/lib-* 26.5.2),
unchanged from 3.1.0.

Generated with [Claude Code](https://claude.ai/code)
via [Happy](https://happy.engineering)

Co-Authored-By: Claude <noreply@anthropic.com>
Co-Authored-By: Happy <yesreply@happy.engineering>
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