@pstdio/sdk 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +357 -0
- package/package.json +1 -1
package/README.md
ADDED
|
@@ -0,0 +1,357 @@
|
|
|
1
|
+
# @pstdio/sdk
|
|
2
|
+
|
|
3
|
+
TypeScript SDK for Prompt Studio.
|
|
4
|
+
|
|
5
|
+
This package is the public integration surface for:
|
|
6
|
+
|
|
7
|
+
- calling the Prompt Studio HTTP API
|
|
8
|
+
- importing shared request and resource types
|
|
9
|
+
- rendering prompt templates
|
|
10
|
+
- authoring Prompt Studio plugins, actions, and lifecycle hooks
|
|
11
|
+
|
|
12
|
+
The package is ESM-only and is published through subpath exports. Import from the entrypoint you need, not from `@pstdio/sdk` directly.
|
|
13
|
+
|
|
14
|
+
## Install
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
bun add @pstdio/sdk
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## Entry Points
|
|
21
|
+
|
|
22
|
+
| Import path | Purpose |
|
|
23
|
+
| ----------------------- | --------------------------------------------------------- |
|
|
24
|
+
| `@pstdio/sdk/client` | Runtime HTTP client for Prompt Studio |
|
|
25
|
+
| `@pstdio/sdk/api` | Request and response payload types |
|
|
26
|
+
| `@pstdio/sdk/resources` | Shared resource/entity types |
|
|
27
|
+
| `@pstdio/sdk/plugins` | Plugin definition types, hook types, and helper utilities |
|
|
28
|
+
| `@pstdio/sdk/prompts` | Prompt rendering helpers |
|
|
29
|
+
| `@pstdio/sdk/hooks` | Hook context and hook client types |
|
|
30
|
+
|
|
31
|
+
Example:
|
|
32
|
+
|
|
33
|
+
```ts
|
|
34
|
+
import { createClient, PstdioApiError } from "@pstdio/sdk/client";
|
|
35
|
+
import type { CreateTicketInput } from "@pstdio/sdk/api";
|
|
36
|
+
import type { TicketDetail } from "@pstdio/sdk/resources";
|
|
37
|
+
import { createSession, definePlugin } from "@pstdio/sdk/plugins";
|
|
38
|
+
import { renderPrompt } from "@pstdio/sdk/prompts";
|
|
39
|
+
import type { AttemptStatusChangeContext } from "@pstdio/sdk/hooks";
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
## HTTP Client
|
|
43
|
+
|
|
44
|
+
Create a client with `createClient()`:
|
|
45
|
+
|
|
46
|
+
```ts
|
|
47
|
+
import { createClient } from "@pstdio/sdk/client";
|
|
48
|
+
|
|
49
|
+
const client = createClient({
|
|
50
|
+
baseUrl: process.env.PSTDIO_API_URL,
|
|
51
|
+
token: process.env.PSTDIO_API_TOKEN,
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
const tickets = await client.tickets.list("proj_123", {
|
|
55
|
+
status: "wip",
|
|
56
|
+
tag: ["backend", "bug"],
|
|
57
|
+
});
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
### Client options
|
|
61
|
+
|
|
62
|
+
- `baseUrl`: API base URL. Defaults to `process.env.PSTDIO_API_URL ?? "http://localhost:19840"`.
|
|
63
|
+
- `token`: Optional bearer token. Sent as `Authorization: Bearer <token>`.
|
|
64
|
+
- `fetch`: Optional `fetch` implementation override for tests or custom runtimes.
|
|
65
|
+
|
|
66
|
+
The client expects a runtime with `fetch` available, or an explicit `fetch` passed in through options.
|
|
67
|
+
|
|
68
|
+
### Error handling
|
|
69
|
+
|
|
70
|
+
The request layer throws `PstdioApiError` for non-2xx responses.
|
|
71
|
+
|
|
72
|
+
- `error.message`: API error message
|
|
73
|
+
- `error.status`: HTTP status code
|
|
74
|
+
- when the API returns `hook_output`, it is appended to `message`
|
|
75
|
+
|
|
76
|
+
```ts
|
|
77
|
+
import { PstdioApiError, createClient } from "@pstdio/sdk/client";
|
|
78
|
+
|
|
79
|
+
const client = createClient();
|
|
80
|
+
|
|
81
|
+
try {
|
|
82
|
+
await client.workspaces.updateAttemptStatus("ws_123", {
|
|
83
|
+
status: "review-ready",
|
|
84
|
+
});
|
|
85
|
+
} catch (error) {
|
|
86
|
+
if (error instanceof PstdioApiError) {
|
|
87
|
+
console.error(error.status, error.message);
|
|
88
|
+
}
|
|
89
|
+
throw error;
|
|
90
|
+
}
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
### Client groups
|
|
94
|
+
|
|
95
|
+
`createClient()` returns a grouped client with these domains:
|
|
96
|
+
|
|
97
|
+
| Group | Methods |
|
|
98
|
+
| ------------ | -------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
99
|
+
| `projects` | `list`, `get`, `create`, `delete`, `listRepos`, `registerRepo`, `removeRepo` |
|
|
100
|
+
| `tickets` | `list`, `get`, `create`, `update`, `delete`, `createAttempt`, `updateWhenAttemptStatus`, `listFiles`, `getFileContent`, `uploadFile`, `deleteFile` |
|
|
101
|
+
| `workspaces` | `list`, `getByShorthand`, `create`, `updateAttemptStatus`, `removeWorktree`, `delete` |
|
|
102
|
+
| `sessions` | `list`, `get`, `create`, `archive`, `followUp`, `approve`, `getConversation`, `resolveSessionId`, `updateStatus` |
|
|
103
|
+
| `statuses` | `list`, `create`, `update`, `setDefault`, `delete`, `listAttemptStatuses`, `createAttemptStatus`, `updateAttemptStatus`, `deleteAttemptStatus` |
|
|
104
|
+
| `tags` | `list`, `create`, `update`, `delete`, `createOption`, `updateOption`, `deleteOption` |
|
|
105
|
+
| `templates` | `list`, `get`, `create`, `update`, `delete` |
|
|
106
|
+
| `skills` | `list`, `get`, `update` |
|
|
107
|
+
| `agents` | `list`, `info`, `models`, `setup`, `setupAvailable`, `update`, `delete` |
|
|
108
|
+
| `actions` | `list`, `execute` |
|
|
109
|
+
|
|
110
|
+
Notes:
|
|
111
|
+
|
|
112
|
+
- `tickets.list(projectId, filters)` supports `status`, `tag`, `archived`, `draft`, `parent_id`, `shorthand`, and `search`.
|
|
113
|
+
- `tickets.getFileContent(ticketId, fileId)` returns `Uint8Array`, not JSON.
|
|
114
|
+
- `createRequest()` is also exported if you want the lower-level request function without the grouped client.
|
|
115
|
+
|
|
116
|
+
## API Types
|
|
117
|
+
|
|
118
|
+
`@pstdio/sdk/api` re-exports the public request and response types used by the HTTP client.
|
|
119
|
+
|
|
120
|
+
```ts
|
|
121
|
+
import type {
|
|
122
|
+
CreateSessionInput,
|
|
123
|
+
CreateTicketInput,
|
|
124
|
+
UpdateTicketInput,
|
|
125
|
+
} from "@pstdio/sdk/api";
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
Most of these types come from `pstdio-api-contracts`. The SDK also defines a few client-facing types:
|
|
129
|
+
|
|
130
|
+
- `ListTicketsInput`
|
|
131
|
+
- `TicketAttemptResponse`
|
|
132
|
+
- `ActionResult`
|
|
133
|
+
- `ExecuteActionInput`
|
|
134
|
+
|
|
135
|
+
Use `import type` for this entrypoint. It does not expose runtime helpers.
|
|
136
|
+
|
|
137
|
+
## Resource Types
|
|
138
|
+
|
|
139
|
+
`@pstdio/sdk/resources` re-exports the shared Prompt Studio entities used across the API and plugin system.
|
|
140
|
+
|
|
141
|
+
Common exports include `Project`, `Repo`, `Ticket`, `TicketDetail`, `TicketListItem`, `TicketFile`, `Workspace`,
|
|
142
|
+
`WorkspaceListItem`, `Session`, `SessionStatus`, `Status`, `AttemptStatus`, `Tag`, `TagOption`, `Template`,
|
|
143
|
+
`TemplateWithContent`, `TemplateType`, `Skill`, `SkillWithContent`, `AgentConfig`, `AgentInfo`, `AgentModel`,
|
|
144
|
+
`AgentAvailabilityType`, and `FileRecord`.
|
|
145
|
+
|
|
146
|
+
```ts
|
|
147
|
+
import type {
|
|
148
|
+
Session,
|
|
149
|
+
TicketDetail,
|
|
150
|
+
WorkspaceListItem,
|
|
151
|
+
} from "@pstdio/sdk/resources";
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
## Prompt Rendering
|
|
155
|
+
|
|
156
|
+
`@pstdio/sdk/prompts` exposes `renderPrompt(template, data)`, a small wrapper around Mustache.
|
|
157
|
+
|
|
158
|
+
```ts
|
|
159
|
+
import { renderPrompt } from "@pstdio/sdk/prompts";
|
|
160
|
+
|
|
161
|
+
const prompt = renderPrompt("Implement ticket {{ticket}}", {
|
|
162
|
+
ticket: "PS-42",
|
|
163
|
+
});
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
Use this when a session prompt or action prompt is stored as a reusable template with variables.
|
|
167
|
+
|
|
168
|
+
## Plugins
|
|
169
|
+
|
|
170
|
+
Prompt Studio plugins export a default `definePlugin(...)` result from a TypeScript or JavaScript module.
|
|
171
|
+
|
|
172
|
+
Plugins can provide:
|
|
173
|
+
|
|
174
|
+
- `actions`: user-triggered actions attached to tickets, workspaces, or sessions
|
|
175
|
+
- `hooks`: lifecycle handlers that run before or after Prompt Studio events
|
|
176
|
+
|
|
177
|
+
### Action example
|
|
178
|
+
|
|
179
|
+
```ts
|
|
180
|
+
import { createSession, definePlugin } from "@pstdio/sdk/plugins";
|
|
181
|
+
|
|
182
|
+
export default definePlugin({
|
|
183
|
+
actions: [
|
|
184
|
+
{
|
|
185
|
+
key: "refine-ticket",
|
|
186
|
+
label: "Refine ticket",
|
|
187
|
+
targetType: "ticket",
|
|
188
|
+
placement: "overflow",
|
|
189
|
+
params: [
|
|
190
|
+
{
|
|
191
|
+
key: "context",
|
|
192
|
+
label: "Additional context",
|
|
193
|
+
type: "longtext",
|
|
194
|
+
required: false,
|
|
195
|
+
},
|
|
196
|
+
],
|
|
197
|
+
async trigger(ctx) {
|
|
198
|
+
const context = ctx.params.context as string | undefined;
|
|
199
|
+
|
|
200
|
+
const parts = [`Refine ticket: ${ctx.target.shorthand}`];
|
|
201
|
+
if (context) parts.push(`Additional context:\n${context}`);
|
|
202
|
+
|
|
203
|
+
await createSession(ctx, {
|
|
204
|
+
title: `Refine ticket: ${ctx.target.shorthand}`,
|
|
205
|
+
prompt: parts.join("\n\n"),
|
|
206
|
+
});
|
|
207
|
+
},
|
|
208
|
+
},
|
|
209
|
+
],
|
|
210
|
+
});
|
|
211
|
+
```
|
|
212
|
+
|
|
213
|
+
### Hook example
|
|
214
|
+
|
|
215
|
+
```ts
|
|
216
|
+
import { definePlugin, runCommand } from "@pstdio/sdk/plugins";
|
|
217
|
+
|
|
218
|
+
export default definePlugin({
|
|
219
|
+
hooks: {
|
|
220
|
+
async preAttemptStatusChange(ctx) {
|
|
221
|
+
if (ctx.toStatus !== "review-ready") return;
|
|
222
|
+
if (!ctx.worktreePath) return;
|
|
223
|
+
|
|
224
|
+
const validation = await runCommand(ctx.worktreePath, [
|
|
225
|
+
"bun",
|
|
226
|
+
"run",
|
|
227
|
+
"validate",
|
|
228
|
+
]);
|
|
229
|
+
if (validation.exitCode === 0) return;
|
|
230
|
+
|
|
231
|
+
const output = [validation.stdout, validation.stderr]
|
|
232
|
+
.filter(Boolean)
|
|
233
|
+
.join("\n\n");
|
|
234
|
+
|
|
235
|
+
return {
|
|
236
|
+
reject: true,
|
|
237
|
+
reason: output || "bun run validate failed",
|
|
238
|
+
};
|
|
239
|
+
},
|
|
240
|
+
},
|
|
241
|
+
});
|
|
242
|
+
```
|
|
243
|
+
|
|
244
|
+
### Plugin types
|
|
245
|
+
|
|
246
|
+
The plugin entrypoint exports the core types used by plugin authors, including `PluginDefinition`, `PluginHooks`,
|
|
247
|
+
`PrePluginHooks`, `PostPluginHooks`, `HookResponse`, `PreHookReturn`, `PostHookReturn`, `ActionDefinition`,
|
|
248
|
+
`ActionDescriptor`, `ActionInput`, `ActionTriggerContext`, `ActionParamDef`, `TargetType`, and `ActionPlacement`.
|
|
249
|
+
|
|
250
|
+
Action parameter definitions support `text`, `longtext`, `select`, `template-select`, `agent`, and `repo`.
|
|
251
|
+
Action targets support `ticket`, `workspace`, and `session`. Action placement supports `primary`, `secondary`,
|
|
252
|
+
and `overflow`.
|
|
253
|
+
|
|
254
|
+
`definePlugin()` is intentionally small. Today it mainly validates that every declared action has a `trigger(ctx)` function and then returns the plugin definition unchanged.
|
|
255
|
+
|
|
256
|
+
### Lifecycle hooks
|
|
257
|
+
|
|
258
|
+
Pre hooks may return `{ reject, reason, data }` to stop an operation. Post hooks return `void`.
|
|
259
|
+
|
|
260
|
+
Available pre hooks:
|
|
261
|
+
|
|
262
|
+
- `preTicketCreation`
|
|
263
|
+
- `preTicketStatusChange`
|
|
264
|
+
- `preTicketArchive`
|
|
265
|
+
- `preTicketDeletion`
|
|
266
|
+
- `preWorktreeCreate`
|
|
267
|
+
- `preWorktreeRemove`
|
|
268
|
+
- `preCommit`
|
|
269
|
+
- `preRebase`
|
|
270
|
+
- `preMerge`
|
|
271
|
+
- `preAttemptStatusChange`
|
|
272
|
+
|
|
273
|
+
Available post hooks:
|
|
274
|
+
|
|
275
|
+
- `postTicketCreation`
|
|
276
|
+
- `postTicketStatusChange`
|
|
277
|
+
- `postTicketArchive`
|
|
278
|
+
- `postTicketDeletion`
|
|
279
|
+
- `postSessionStart`
|
|
280
|
+
- `postSessionSuccess`
|
|
281
|
+
- `postSessionFail`
|
|
282
|
+
- `postSessionResume`
|
|
283
|
+
- `postSessionAwaitInput`
|
|
284
|
+
- `postWorktreeCreate`
|
|
285
|
+
- `postWorktreeRemove`
|
|
286
|
+
- `postCommit`
|
|
287
|
+
- `postRebase`
|
|
288
|
+
- `postMerge`
|
|
289
|
+
- `onConflict`
|
|
290
|
+
- `postAttemptStatusChange`
|
|
291
|
+
|
|
292
|
+
### Plugin helpers
|
|
293
|
+
|
|
294
|
+
`@pstdio/sdk/plugins` also exports helper functions for common workflow automation:
|
|
295
|
+
|
|
296
|
+
| Helper | Purpose |
|
|
297
|
+
| ---------------------------------- | -------------------------------------------------------------------------------------------- |
|
|
298
|
+
| `createAttempt` | Create a ticket attempt and start a session |
|
|
299
|
+
| `createWorkspace` | Create a ticket attempt without starting a session |
|
|
300
|
+
| `createSession` | Create a session using `ctx.projectId` automatically |
|
|
301
|
+
| `followupSession` | Send a follow-up message using an explicit or contextual session id |
|
|
302
|
+
| `findTicketByRef` | Resolve a ticket by id or shorthand |
|
|
303
|
+
| `findWorkspaceByRef` | Resolve a workspace by id or shorthand |
|
|
304
|
+
| `getAttemptsForTicket` | List workspaces for a ticket |
|
|
305
|
+
| `workspacesForTicket` | List workspaces for a ticket |
|
|
306
|
+
| `setTicketStatus` | Resolve a status by name and update the ticket |
|
|
307
|
+
| `setWorkspaceAttemptStatus` | Update a workspace attempt status by name |
|
|
308
|
+
| `updateTicketWhenAllAttemptsMatch` | Update a ticket when all attempts share a target attempt status |
|
|
309
|
+
| `removeAllWorktreesForTicket` | Remove every worktree currently attached to a ticket |
|
|
310
|
+
| `bootstrapWorktree` | Copy Prompt Studio and agent metadata into a worktree and optionally pull the ticket locally |
|
|
311
|
+
| `pullTickets` | Write ticket markdown and attachments into `.pstdio/tickets/...` |
|
|
312
|
+
| `runCommand` | Run a command array in a working directory and capture `stdout`, `stderr`, and `exitCode` |
|
|
313
|
+
|
|
314
|
+
These helpers accept action or hook context and resolve project-scoped ids for you where possible.
|
|
315
|
+
|
|
316
|
+
## Hook Context Types
|
|
317
|
+
|
|
318
|
+
`@pstdio/sdk/hooks` exposes the shared context types used by hooks and hook runtimes:
|
|
319
|
+
|
|
320
|
+
`BaseHookContext`, `HookClient`, `HookPayload`, `SessionFollowupInput`, `AttemptStatusChangeContext`,
|
|
321
|
+
`SessionHookContext`, `TicketContext`, `TicketCreationContext`, `TicketStatusChangeContext`, `WorktreeContext`,
|
|
322
|
+
and `WorktreeCreateContext`.
|
|
323
|
+
|
|
324
|
+
Import from this entrypoint when you want to annotate hook code explicitly:
|
|
325
|
+
|
|
326
|
+
```ts
|
|
327
|
+
import type {
|
|
328
|
+
SessionHookContext,
|
|
329
|
+
TicketStatusChangeContext,
|
|
330
|
+
} from "@pstdio/sdk/hooks";
|
|
331
|
+
|
|
332
|
+
const logStatusChange = (ctx: TicketStatusChangeContext) => {
|
|
333
|
+
console.log(ctx.shorthand, ctx.fromStatus, ctx.toStatus);
|
|
334
|
+
};
|
|
335
|
+
|
|
336
|
+
const onSessionStart = async (ctx: SessionHookContext) => {
|
|
337
|
+
console.log(ctx.sessionId, ctx.sessionStatus);
|
|
338
|
+
};
|
|
339
|
+
```
|
|
340
|
+
|
|
341
|
+
## Package Development
|
|
342
|
+
|
|
343
|
+
From the repo root:
|
|
344
|
+
|
|
345
|
+
```bash
|
|
346
|
+
bun run --cwd packages/sdk build
|
|
347
|
+
bun run --cwd packages/sdk lint
|
|
348
|
+
bun run --cwd packages/sdk test
|
|
349
|
+
```
|
|
350
|
+
|
|
351
|
+
From `packages/sdk`:
|
|
352
|
+
|
|
353
|
+
```bash
|
|
354
|
+
bun run build
|
|
355
|
+
bun run lint
|
|
356
|
+
bun run test
|
|
357
|
+
```
|