SDK
用於 dropstone 伺服器的型別安全 JS 客戶端。
Dropstone JS/TS SDK 提供了一個型別安全的客戶端,用於與本地 Dropstone 代理互動。它將 dropstone serve 作為子程序生成,並為您提供指向它的型別化 HTTP 客戶端。
需要在 CI 中進行無頭 API 存取?:
對於純程式化使用(CI 管道、自動化、無伺服器),建議使用帶有 DROPSTONE_API_KEY 的 HTTP API。本頁面上的 SDK 適用於在安裝了 CLI 二進位檔案的 Node 程序中嵌入互動式代理。
請參閱 Server 頁面以了解基礎 HTTP API 的工作原理。
安裝
從 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"],
},
},
},
})
// Access the structured output
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[] |
範例
// Write a log entry
await client.app.log({
body: {
service: "my-app",
level: "info",
message: "Operation completed",
},
})
// List available agents
const agents = await client.app.agents()
專案
| 方法 | 描述 | 回應 |
|---|---|---|
project.list() | 列出所有專案 | Project[] |
project.current() | 取得目前專案 | Project |
範例
// List all projects
const projects = await client.project.list()
// Get current project
const currentProject = await client.project.current()
路徑
| 方法 | 描述 | 回應 |
|---|---|---|
path.get() | 取得目前路徑 | Path |
範例
// Get current path information
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(僅限上下文)。預設回傳 AssistantMessage 與 AI 回應。支援 body.outputFormat 用於 structured output |
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 |
範例
// Create and manage sessions
const session = await client.session.create({
body: { title: "My session" },
})
const sessions = await client.session.list()
// Send a prompt message
const result = await client.session.prompt({
path: { id: session.id },
body: {
model: { providerID: "dropstone", modelID: "dropstone-pro" },
parts: [{ type: "text", text: "Hello!" }],
},
})
// Inject context without triggering AI response (useful for plugins)
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)
範例
// Search and read files
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() | 伺服器傳送事件串流 | 伺服器傳送事件串流 |
範例
// Listen to real-time events
const events = await client.event.subscribe()
for await (const event of events.stream) {
console.log("Event:", event.type, event.properties)
}