@usepipr/sdk 0.4.3 → 0.5.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/dist/index.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { a as configFactoryBrand, i as builtinReadOnlyToolBrand, o as assertSupportedCommandRestCapture, r as renderPromptValue } from "./config-Ct0Qfa_b.mjs";
1
+ import { a as configFactoryBrand, i as serializePromptJson, o as assertSupportedCommandRestCapture, r as renderPromptValue } from "./config-STm6gTIf.mjs";
2
2
  import { z, z as z$1 } from "zod";
3
3
  //#region src/prompt.ts
4
4
  /** Creates trimmed Markdown from a template literal with common indentation removed. */
@@ -18,6 +18,110 @@ function stripCommonIndent(value) {
18
18
  return lines.map((line) => line.slice(indent)).join("\n");
19
19
  }
20
20
  //#endregion
21
+ //#region src/runtime-handles.ts
22
+ const taskRecords = /* @__PURE__ */ new WeakMap();
23
+ const agentRecords = /* @__PURE__ */ new WeakMap();
24
+ const toolRecords = /* @__PURE__ */ new WeakMap();
25
+ function createTaskHandle(definition) {
26
+ const handle = {
27
+ kind: "pipr.task",
28
+ name: definition.name
29
+ };
30
+ const record = {
31
+ name: definition.name,
32
+ check: definition.check,
33
+ ...definition.local === false ? { local: false } : {},
34
+ handler: definition.run
35
+ };
36
+ taskRecords.set(handle, record);
37
+ return {
38
+ handle,
39
+ record
40
+ };
41
+ }
42
+ function runtimeTaskForHandle(task) {
43
+ const record = taskRecords.get(task);
44
+ if (!record) throw new Error("Expected a task handle created by pipr.task");
45
+ return record;
46
+ }
47
+ function createAgentHandle(definition) {
48
+ const handle = {
49
+ kind: "pipr.agent",
50
+ name: definition.name,
51
+ extend(patch) {
52
+ return createAgentHandle({
53
+ ...definition,
54
+ ...patch,
55
+ instructions: patch.instructions === void 0 ? definition.instructions : {
56
+ kind: "pipr.prompt",
57
+ value: `${renderPromptValue(definition.instructions)}\n\n${renderPromptValue(patch.instructions)}`.trim()
58
+ }
59
+ }).handle;
60
+ }
61
+ };
62
+ const record = {
63
+ name: definition.name,
64
+ definition: runtimeAgentDefinition(definition)
65
+ };
66
+ agentRecords.set(handle, record);
67
+ return {
68
+ handle,
69
+ record
70
+ };
71
+ }
72
+ function runtimeAgentForHandle(agent) {
73
+ const record = agentRecords.get(agent);
74
+ if (!record) throw new Error("Expected an agent handle created by pipr.agent");
75
+ return record;
76
+ }
77
+ function createToolHandle(definition) {
78
+ const handle = {
79
+ kind: "pipr.tool",
80
+ name: definition.name
81
+ };
82
+ const record = {
83
+ name: definition.name,
84
+ description: definition.description,
85
+ input: definition.input,
86
+ output: definition.output,
87
+ run: definition.run,
88
+ toModelOutput: definition.toModelOutput
89
+ };
90
+ toolRecords.set(handle, record);
91
+ return {
92
+ handle,
93
+ record
94
+ };
95
+ }
96
+ function createBuiltinReadOnlyToolHandle() {
97
+ const handle = {
98
+ kind: "pipr.tool",
99
+ name: "readOnly"
100
+ };
101
+ const record = {
102
+ name: "readOnly",
103
+ builtinReadOnly: true
104
+ };
105
+ toolRecords.set(handle, record);
106
+ return {
107
+ handle,
108
+ record
109
+ };
110
+ }
111
+ function runtimeToolForHandle(tool) {
112
+ const record = toolRecords.get(tool);
113
+ if (!record) throw new Error("Expected a tool handle created by pipr.tool");
114
+ return record;
115
+ }
116
+ function runtimeAgentDefinition(definition) {
117
+ return {
118
+ ...definition,
119
+ prompt: definition.prompt,
120
+ output: definition.output,
121
+ tools: definition.tools?.map(runtimeToolForHandle)
122
+ };
123
+ }
124
+ //#endregion
21
125
  //#region src/review-contract.ts
22
126
  const nonEmptyStringSchema = z$1.string().min(1);
23
127
  const positiveIntegerSchema = z$1.number().int().positive();
@@ -173,20 +277,17 @@ function createBuilder() {
173
277
  const commands = [];
174
278
  const tools = [];
175
279
  const publication = {};
280
+ const readOnlyTool = createBuiltinReadOnlyToolHandle();
176
281
  let checks;
177
282
  let limits;
178
283
  const api = {
179
- tools: { readOnly: [{
180
- kind: "pipr.tool",
181
- name: "readOnly",
182
- [builtinReadOnlyToolBrand]: true
183
- }] },
284
+ tools: { readOnly: [readOnlyTool.handle] },
184
285
  schemas,
185
286
  on: { changeRequest(options) {
186
287
  if (!Array.isArray(options.actions) || !options.task) throw new Error("pipr.on.changeRequest requires { actions, task }");
187
288
  changeRequestTriggers.push({
188
289
  actions: [...options.actions],
189
- task: options.task
290
+ task: runtimeTaskForHandle(options.task)
190
291
  });
191
292
  } },
192
293
  secret(options) {
@@ -212,21 +313,15 @@ function createBuilder() {
212
313
  return profile;
213
314
  },
214
315
  agent(definition) {
215
- const agent = createAgent(definition);
216
- agents.push(agent);
217
- return agent;
316
+ const agent = createAgentHandle(definition);
317
+ agents.push(agent.record);
318
+ return agent.handle;
218
319
  },
219
320
  task(definition) {
220
321
  if (!definition.name || typeof definition.run !== "function") throw new Error("pipr.task requires { name, run }");
221
- const task = {
222
- kind: "pipr.task",
223
- name: definition.name,
224
- check: definition.check,
225
- ...definition.local === false ? { local: false } : {},
226
- handler: definition.run
227
- };
228
- tasks.push(task);
229
- return task;
322
+ const task = createTaskHandle(definition);
323
+ tasks.push(task.record);
324
+ return task.handle;
230
325
  },
231
326
  reviewer(options) {
232
327
  return createReviewer(api, options);
@@ -253,7 +348,7 @@ function createBuilder() {
253
348
  permission: options.permission ?? "write",
254
349
  description: options.description,
255
350
  parse: options.parse,
256
- task: options.task
351
+ task: runtimeTaskForHandle(options.task)
257
352
  });
258
353
  },
259
354
  use(plugin) {
@@ -263,13 +358,12 @@ function createBuilder() {
263
358
  if (definition.name === "readOnly") throw new Error("Tool name 'readOnly' is reserved for pipr built-in tools");
264
359
  const run = definition.run;
265
360
  if (!run) throw new Error(`Tool '${definition.name}' must define run`);
266
- const tool = {
267
- kind: "pipr.tool",
361
+ const tool = createToolHandle({
268
362
  ...definition,
269
363
  run
270
- };
271
- tools.push(tool);
272
- return tool;
364
+ });
365
+ tools.push(tool.record);
366
+ return tool.handle;
273
367
  },
274
368
  schema,
275
369
  jsonSchema,
@@ -291,7 +385,7 @@ function createBuilder() {
291
385
  };
292
386
  },
293
387
  json(value, options) {
294
- const text = JSON.stringify(value, null, options?.pretty === false ? 0 : 2);
388
+ const text = serializePromptJson(value, options?.pretty !== false);
295
389
  if (options?.maxCharacters !== void 0 && text.length > options.maxCharacters) throw new Error(`JSON prompt value exceeded ${options.maxCharacters} characters`);
296
390
  return {
297
391
  kind: "pipr.prompt",
@@ -306,6 +400,7 @@ function createBuilder() {
306
400
  assertUnique(commands.map((command) => command.pattern), "command");
307
401
  assertModelIdentity(models);
308
402
  return {
403
+ resolveAgent: runtimeAgentForHandle,
309
404
  models,
310
405
  agents,
311
406
  tasks,
@@ -469,6 +564,7 @@ function createReviewRecipeTask(api, id, agent, options) {
469
564
  });
470
565
  const source = typeof options.comment === "function" ? await options.comment(result, {
471
566
  review: { id },
567
+ run: context.run,
472
568
  repository: context.repository,
473
569
  change: context.change,
474
570
  platform: context.platform
@@ -558,23 +654,6 @@ function assertDiffManifestLimitConflicts(current, next) {
558
654
  for (const [key, value] of Object.entries(next.diffManifest)) if (value !== void 0 && current.diffManifest[key] !== void 0 && current.diffManifest[key] !== value) throw new Error(`pipr.config limits.diffManifest.${key} conflicts with existing value`);
559
655
  }
560
656
  }
561
- function createAgent(definition) {
562
- return {
563
- kind: "pipr.agent",
564
- name: definition.name,
565
- definition,
566
- extend(patch) {
567
- return createAgent({
568
- ...definition,
569
- ...patch,
570
- instructions: patch.instructions === void 0 ? definition.instructions : {
571
- kind: "pipr.prompt",
572
- value: `${renderPromptValue(definition.instructions)}\n\n${renderPromptValue(patch.instructions)}`.trim()
573
- }
574
- });
575
- }
576
- };
577
- }
578
657
  function assertUnique(values, label) {
579
658
  const seen = /* @__PURE__ */ new Set();
580
659
  for (const value of values) {
@@ -629,6 +708,128 @@ function stableJsonValue(value) {
629
708
  return value;
630
709
  }
631
710
  //#endregion
632
- export { defaultReviewActions, defaultReviewEntrypoints, definePipr, definePlugin, jsonSchema, md, parseReviewFinding, parseReviewResult, parseReviewSummary, reviewFindingSchema, reviewResultSchema, reviewSchemaExample, reviewSummarySchema, schema, schemas, z };
711
+ //#region src/result.ts
712
+ const piprRunTriggers = [
713
+ "change-request",
714
+ "command",
715
+ "verifier",
716
+ "local"
717
+ ];
718
+ const text = z$1.string().min(1);
719
+ const count = z$1.number().int().nonnegative();
720
+ const header = { formatVersion: z$1.literal(2) };
721
+ const runSummarySchema = z$1.strictObject({
722
+ id: text.max(200),
723
+ trigger: z$1.enum(piprRunTriggers),
724
+ baseSha: text.max(200),
725
+ headSha: text.max(200),
726
+ tasks: z$1.array(text.max(200)).max(200),
727
+ durationMs: count,
728
+ models: z$1.array(text.max(200)).max(20),
729
+ agentRuns: count,
730
+ inputTokens: count,
731
+ outputTokens: count,
732
+ costUsd: z$1.number().nonnegative().finite(),
733
+ usageStatus: z$1.enum([
734
+ "complete",
735
+ "partial",
736
+ "unavailable"
737
+ ])
738
+ });
739
+ const inlineCountsSchema = z$1.strictObject({
740
+ posted: count,
741
+ skipped: count,
742
+ failed: count
743
+ });
744
+ const publicationCountsSchema = z$1.strictObject({
745
+ inlineComments: inlineCountsSchema,
746
+ inlinePublicationErrorCount: count,
747
+ inlineResolutionErrorCount: count
748
+ });
749
+ const reviewPublicationSchema = z$1.discriminatedUnion("state", [z$1.strictObject({ state: z$1.literal("disabled") }), z$1.strictObject({
750
+ state: z$1.literal("completed"),
751
+ mainComment: z$1.strictObject({ action: z$1.enum(["created", "updated"]) }),
752
+ ...publicationCountsSchema.shape
753
+ })]);
754
+ const schemas$1 = [
755
+ z$1.strictObject({
756
+ ...header,
757
+ kind: z$1.literal("review"),
758
+ run: runSummarySchema,
759
+ mainComment: z$1.string(),
760
+ inlineFindings: z$1.array(reviewFindingSchema),
761
+ droppedFindings: z$1.array(z$1.strictObject({
762
+ finding: reviewFindingSchema,
763
+ reason: text
764
+ })),
765
+ taskChecks: z$1.array(z$1.strictObject({
766
+ taskName: text,
767
+ conclusion: z$1.enum([
768
+ "success",
769
+ "failure",
770
+ "neutral"
771
+ ]),
772
+ summary: text.optional()
773
+ })),
774
+ repairAttempted: z$1.boolean(),
775
+ publication: reviewPublicationSchema
776
+ }),
777
+ z$1.strictObject({
778
+ ...header,
779
+ kind: z$1.literal("skipped"),
780
+ reason: text
781
+ }),
782
+ z$1.strictObject({
783
+ ...header,
784
+ kind: z$1.literal("ignored"),
785
+ reason: text
786
+ }),
787
+ z$1.strictObject({
788
+ ...header,
789
+ kind: z$1.literal("dry-run")
790
+ }),
791
+ z$1.strictObject({
792
+ ...header,
793
+ kind: z$1.literal("command-help"),
794
+ reason: text,
795
+ mainComment: z$1.string()
796
+ }),
797
+ z$1.strictObject({
798
+ ...header,
799
+ kind: z$1.literal("command-response"),
800
+ run: runSummarySchema,
801
+ mainComment: z$1.string(),
802
+ publication: z$1.strictObject({
803
+ state: z$1.literal("completed"),
804
+ action: z$1.enum(["created", "updated"])
805
+ })
806
+ }),
807
+ z$1.strictObject({
808
+ ...header,
809
+ kind: z$1.literal("verifier"),
810
+ run: runSummarySchema,
811
+ publication: z$1.strictObject({
812
+ state: z$1.literal("completed"),
813
+ inlineResolutionErrorCount: count
814
+ })
815
+ }),
816
+ z$1.strictObject({
817
+ ...header,
818
+ kind: z$1.literal("publication-error"),
819
+ message: text,
820
+ publication: publicationCountsSchema.optional()
821
+ }),
822
+ z$1.strictObject({
823
+ ...header,
824
+ kind: z$1.literal("error"),
825
+ message: text
826
+ })
827
+ ];
828
+ const piprResultSchema = z$1.discriminatedUnion("kind", schemas$1);
829
+ function parsePiprResult(value) {
830
+ return piprResultSchema.parse(value);
831
+ }
832
+ //#endregion
833
+ export { defaultReviewActions, defaultReviewEntrypoints, definePipr, definePlugin, jsonSchema, md, parsePiprResult, parseReviewFinding, parseReviewResult, parseReviewSummary, piprResultSchema, reviewFindingSchema, reviewResultSchema, reviewSchemaExample, reviewSummarySchema, schema, schemas, z };
633
834
 
634
835
  //# sourceMappingURL=index.mjs.map