SDK
Dropstone 服务器的类型安全 JS 客户端。
Dropstone JS/TS SDK 为与本地 Dropstone 代理交互提供了一个类型安全的客户端。它将 dropstone serve 作为子进程生成,并为您提供一个指向它的类型化 HTTP 客户端。
需要在 CI 中进行无头 API 访问?:
对于纯编程使用(CI 管道、自动化、无服务器),请优先使用带有 DROPSTONE_API_KEY 的 HTTP API。本页面的 SDK 旨在将交互式代理嵌入到安装了 CLI 二进制文件的 Node 进程中。
有关底层 HTTP API 如何工作的信息,请参阅 Server 页面。
安装
从 npm 安装 SDK:
npm install @blankline/dropstone-sdk
创建客户端
创建 dropstone 实例:
import { createDropstone } from "@blankline/dropstone-sdk"
const { client } = await createDropstone()
这会启动服务器和客户端
选项
| 选项 | 类型 | 描述 | 默认值 |
|---|---|---|---|
hostname | string | 服务器主机名 | 127.0.0.1 |
port | number | 服务器端口 | 4096 |
signal | AbortSignal | 用于取消的中止信号 | undefined |
timeout | number | 服务器启动超时(毫秒) | 5000 |
config | Config | 配置对象 | {} |
配置
您可以传递一个配置对象来自定义行为。该实例仍会读取您的 dropstone.json,但您可以内联覆盖或添加配置:
import { createDropstone } from "@blankline/dropstone-sdk"
const dropstone = await createDropstone({
hostname: "127.0.0.1",
port: 4096,
config: {
model: "dropstone/dropstone-pro",
},
})
console.log(`Server running at ${dropstone.server.url}`)
dropstone.server.close()
仅客户端
如果您已经有一个运行中的 dropstone 实例,可以创建一个客户端实例来连接它:
import { createDropstoneClient } from "@blankline/dropstone-sdk"
const client = createDropstoneClient({
baseUrl: "http://localhost:4096",
})
选项
| 选项 | 类型 | 描述 | 默认值 |
|---|---|---|---|
baseUrl | string | 服务器的 URL | http://localhost:4096 |
fetch | function | 自定义 fetch 实现 | globalThis.fetch |
parseAs | string | 响应解析方法 | auto |
responseStyle | string | 返回样式:data 或 fields | fields |
throwOnError | boolean | 抛出错误而不是返回 | false |
类型
SDK 包含所有 API 类型的 TypeScript 定义。直接导入它们:
import type { Session, Message, Part } from "@blankline/dropstone-sdk"
所有类型都是从服务器的 OpenAPI 规范生成的,因此您在 TypeScript 中看到的名称与 server 请求和响应形状一一对应。
错误
SDK 可以抛出您可以捕获和处理的错误:
try {
await client.session.get({ path: { id: "invalid-id" } })
} catch (error) {
console.error("Failed to get session:", (error as Error).message)
}
结构化输出
您可以通过指定带有 JSON 模式的 format 来请求模型的结构化 JSON 输出。模型将使用 StructuredOutput 工具返回与您的模式匹配的验证 JSON。
基本用法
const result = await client.session.prompt({
path: { id: sessionId },
body: {
parts: [{ type: "text", text: "Research Dropstone and provide company info" }],
format: {
type: "json_schema",
schema: {
type: "object",
properties: {
company: { type: "string", description: "Company name" },
founded: { type: "number", description: "Year founded" },
products: {
type: "array",
items: { type: "string" },
description: "Main products",
},
},
required: ["company", "founded"],
},
},
},
})
// 访问结构化输出
console.log(result.data.info.structured_output)
// { company: "Dropstone", founded: 2024, products: ["Dropstone CLI"] }
输出格式类型
| 类型 | 描述 |
|---|---|
text | 默认。标准文本响应(无结构化输出) |
json_schema | 返回与提供的模式匹配的验证 JSON |
JSON 模式格式
使用 type: 'json_schema' 时,提供:
| 字段 | 类型 | 描述 |
|---|---|---|
type | 'json_schema' | 必需。指定 JSON 模式模式 |
schema | object | 必需。定义输出结构的 JSON 模式对象 |
retryCount | number | 可选。验证重试次数(默认值:2) |
错误处理
如果模型在所有重试后未能生成有效的结构化输出,响应将包含 StructuredOutputError:
if (result.data.info.error?.name === "StructuredOutputError") {
console.error("Failed to produce structured output:", result.data.info.error.message)
console.error("Attempts:", result.data.info.error.retries)
}
最佳实践
- 在模式属性中提供清晰的描述,以帮助模型理解要提取的数据
- 使用
required指定哪些字段必须存在 - 保持模式专注 - 复杂的嵌套模式可能更难让模型正确填充
- 设置适当的
retryCount- 对于复杂模式增加,对于简单模式减少
API
SDK 通过类型安全的客户端公开所有服务器 API。
全局
| 方法 | 描述 | 响应 |
|---|---|---|
global.health() | 检查服务器健康和版本 | { healthy: true, version: string } |
示例
const health = await client.global.health()
console.log(health.data.version)
应用
| 方法 | 描述 | 响应 |
|---|---|---|
app.log() | 写入日志条目 | boolean |
app.agents() | 列出所有可用代理 | Agent[] |
示例
// 写入日志条目
await client.app.log({
body: {
service: "my-app",
level: "info",
message: "Operation completed",
},
})
// 列出可用代理
const agents = await client.app.agents()
项目
| 方法 | 描述 | 响应 |
|---|---|---|
project.list() | 列出所有项目 | Project[] |
project.current() | 获取当前项目 | Project |
示例
// 列出所有项目
const projects = await client.project.list()
// 获取当前项目
const currentProject = await client.project.current()
路径
| 方法 | 描述 | 响应 |
|---|---|---|
path.get() | 获取当前路径 | Path |
示例
// 获取当前路径信息
const pathInfo = await client.path.get()
配置
| 方法 | 描述 | 响应 |
|---|---|---|
config.get() | 获取配置信息 | Config |
示例
const config = await client.config.get()
会话
| 方法 | 描述 | 注释 |
|---|---|---|
session.list() | 列出会话 | 返回 Session[] |
session.get({ path }) | 获取会话 | 返回 Session |
session.children({ path }) | 列出子会话 | 返回 Session[] |
session.create({ body }) | 创建会话 | 返回 Session |
session.delete({ path }) | 删除会话 | 返回 boolean |
session.update({ path, body }) | 更新会话属性 | 返回 Session |
session.init({ path, body }) | 分析应用并创建 AGENTS.md | 返回 boolean |
session.abort({ path }) | 中止运行中的会话 | 返回 boolean |
session.summarize({ path, body }) | 总结会话 | 返回 boolean |
session.messages({ path }) | 列出会话中的消息 | 返回 { info: Message, parts: Part[]}[] |
session.message({ path }) | 获取消息详情 | 返回 { info: Message, parts: Part[]} |
session.prompt({ path, body }) | 发送提示消息 | body.noReply: true 返回 UserMessage(仅上下文)。默认返回带有 AI 响应的 AssistantMessage。支持 body.outputFormat 用于 结构化输出 |
session.command({ path, body }) | 向会话发送命令 | 返回 { info: AssistantMessage, parts: Part[]} |
session.shell({ path, body }) | 运行 shell 命令 | 返回 AssistantMessage |
session.revert({ path, body }) | 还原消息 | 返回 Session |
session.unrevert({ path }) | 恢复已还原的消息 | 返回 Session |
postSessionByIdPermissionsByPermissionId({ path, body }) | 响应权限请求 | 返回 boolean |
示例
// 创建和管理会话
const session = await client.session.create({
body: { title: "My session" },
})
const sessions = await client.session.list()
// 发送提示消息
const result = await client.session.prompt({
path: { id: session.id },
body: {
model: { providerID: "dropstone", modelID: "dropstone-pro" },
parts: [{ type: "text", text: "Hello!" }],
},
})
// 注入上下文而不触发 AI 响应(对插件很有用)
await client.session.prompt({
path: { id: session.id },
body: {
noReply: true,
parts: [{ type: "text", text: "You are a helpful assistant." }],
},
})
文件
| 方法 | 描述 | 响应 |
|---|---|---|
find.text({ query }) | 在文件中搜索文本 | 包含 path、lines、line_number、absolute_offset、submatches 的匹配对象数组 |
find.files({ query }) | 按名称查找文件和目录 | string[](路径) |
find.symbols({ query }) | 查找工作区符号 | Symbol[] |
file.read({ query }) | 读取文件 | { type: "raw" | "patch", content: string } |
file.status({ query? }) | 获取跟踪文件的状态 | File[] |
find.files 支持一些可选的查询字段:
type:"file"或"directory"directory:覆盖搜索的项目根目录limit:最大结果数(1–200)
示例
// 搜索和读取文件
const textResults = await client.find.text({
query: { pattern: "function.*dropstone" },
})
const files = await client.find.files({
query: { query: "*.ts", type: "file" },
})
const directories = await client.find.files({
query: { query: "packages", type: "directory", limit: 20 },
})
const content = await client.file.read({
query: { path: "src/index.ts" },
})
认证
| 方法 | 描述 | 响应 |
|---|---|---|
auth.set({ ... }) | 设置认证凭证 | boolean |
示例
await client.auth.set({
path: { id: "dropstone" },
body: { type: "api", key: "your-dropstone-api-key" },
})
事件
| 方法 | 描述 | 响应 |
|---|---|---|
event.subscribe() | 服务器发送事件流 | 服务器发送事件流 |
示例
// 监听实时事件
const events = await client.event.subscribe()
for await (const event of events.stream) {
console.log("Event:", event.type, event.properties)
}