@proposit/proposit-core 1.0.2 → 1.1.1

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.
Files changed (92) hide show
  1. package/dist/cli/commands/parse.d.ts.map +1 -1
  2. package/dist/cli/commands/parse.js +62 -27
  3. package/dist/cli/commands/parse.js.map +1 -1
  4. package/dist/cli/llm/index.d.ts +5 -4
  5. package/dist/cli/llm/index.d.ts.map +1 -1
  6. package/dist/cli/llm/index.js +15 -3
  7. package/dist/cli/llm/index.js.map +1 -1
  8. package/dist/extensions/argument-ingestion/index.d.ts +9 -0
  9. package/dist/extensions/argument-ingestion/index.d.ts.map +1 -0
  10. package/dist/extensions/argument-ingestion/index.js +11 -0
  11. package/dist/extensions/argument-ingestion/index.js.map +1 -0
  12. package/dist/extensions/argument-ingestion/shared/basics-extension.d.ts +9 -0
  13. package/dist/extensions/argument-ingestion/shared/basics-extension.d.ts.map +1 -0
  14. package/dist/extensions/argument-ingestion/shared/basics-extension.js +25 -0
  15. package/dist/extensions/argument-ingestion/shared/basics-extension.js.map +1 -0
  16. package/dist/extensions/argument-ingestion/shared/finalize-response.d.ts +21 -0
  17. package/dist/extensions/argument-ingestion/shared/finalize-response.d.ts.map +1 -0
  18. package/dist/extensions/argument-ingestion/shared/finalize-response.js +33 -0
  19. package/dist/extensions/argument-ingestion/shared/finalize-response.js.map +1 -0
  20. package/dist/extensions/argument-ingestion/shared/role-derivation.d.ts +11 -0
  21. package/dist/extensions/argument-ingestion/shared/role-derivation.d.ts.map +1 -0
  22. package/dist/extensions/argument-ingestion/shared/role-derivation.js +26 -0
  23. package/dist/extensions/argument-ingestion/shared/role-derivation.js.map +1 -0
  24. package/dist/extensions/argument-ingestion/shared/types.d.ts +34 -0
  25. package/dist/extensions/argument-ingestion/shared/types.d.ts.map +1 -0
  26. package/dist/extensions/argument-ingestion/shared/types.js +16 -0
  27. package/dist/extensions/argument-ingestion/shared/types.js.map +1 -0
  28. package/dist/extensions/argument-ingestion/v1-single-shot.d.ts +18 -0
  29. package/dist/extensions/argument-ingestion/v1-single-shot.d.ts.map +1 -0
  30. package/dist/extensions/argument-ingestion/v1-single-shot.js +105 -0
  31. package/dist/extensions/argument-ingestion/v1-single-shot.js.map +1 -0
  32. package/dist/extensions/basics/schemata.d.ts +39 -0
  33. package/dist/extensions/basics/schemata.d.ts.map +1 -1
  34. package/dist/extensions/basics/schemata.js +21 -7
  35. package/dist/extensions/basics/schemata.js.map +1 -1
  36. package/dist/extensions/openai/errors.d.ts +49 -0
  37. package/dist/extensions/openai/errors.d.ts.map +1 -0
  38. package/dist/extensions/openai/errors.js +85 -0
  39. package/dist/extensions/openai/errors.js.map +1 -0
  40. package/dist/extensions/openai/index.d.ts +7 -0
  41. package/dist/extensions/openai/index.d.ts.map +1 -0
  42. package/dist/extensions/openai/index.js +10 -0
  43. package/dist/extensions/openai/index.js.map +1 -0
  44. package/dist/extensions/openai/provider.d.ts +22 -0
  45. package/dist/extensions/openai/provider.d.ts.map +1 -0
  46. package/dist/extensions/openai/provider.js +385 -0
  47. package/dist/extensions/openai/provider.js.map +1 -0
  48. package/dist/extensions/openai/structured-output.d.ts +18 -0
  49. package/dist/extensions/openai/structured-output.d.ts.map +1 -0
  50. package/dist/extensions/openai/structured-output.js +201 -0
  51. package/dist/extensions/openai/structured-output.js.map +1 -0
  52. package/dist/extensions/openai/types.d.ts +81 -0
  53. package/dist/extensions/openai/types.d.ts.map +1 -0
  54. package/dist/extensions/openai/types.js +21 -0
  55. package/dist/extensions/openai/types.js.map +1 -0
  56. package/dist/lib/index.d.ts +8 -0
  57. package/dist/lib/index.d.ts.map +1 -1
  58. package/dist/lib/index.js +15 -0
  59. package/dist/lib/index.js.map +1 -1
  60. package/dist/lib/llm/index.d.ts +2 -0
  61. package/dist/lib/llm/index.d.ts.map +1 -0
  62. package/dist/lib/llm/index.js +3 -0
  63. package/dist/lib/llm/index.js.map +1 -0
  64. package/dist/lib/llm/types.d.ts +49 -0
  65. package/dist/lib/llm/types.d.ts.map +1 -0
  66. package/dist/lib/llm/types.js +15 -0
  67. package/dist/lib/llm/types.js.map +1 -0
  68. package/dist/lib/pipelines/execute.d.ts +23 -0
  69. package/dist/lib/pipelines/execute.d.ts.map +1 -0
  70. package/dist/lib/pipelines/execute.js +541 -0
  71. package/dist/lib/pipelines/execute.js.map +1 -0
  72. package/dist/lib/pipelines/index.d.ts +7 -0
  73. package/dist/lib/pipelines/index.d.ts.map +1 -0
  74. package/dist/lib/pipelines/index.js +5 -0
  75. package/dist/lib/pipelines/index.js.map +1 -0
  76. package/dist/lib/pipelines/stage-helpers.d.ts +94 -0
  77. package/dist/lib/pipelines/stage-helpers.d.ts.map +1 -0
  78. package/dist/lib/pipelines/stage-helpers.js +354 -0
  79. package/dist/lib/pipelines/stage-helpers.js.map +1 -0
  80. package/dist/lib/pipelines/types.d.ts +88 -0
  81. package/dist/lib/pipelines/types.d.ts.map +1 -0
  82. package/dist/lib/pipelines/types.js +36 -0
  83. package/dist/lib/pipelines/types.js.map +1 -0
  84. package/package.json +14 -1
  85. package/dist/cli/llm/openai.d.ts +0 -4
  86. package/dist/cli/llm/openai.d.ts.map +0 -1
  87. package/dist/cli/llm/openai.js +0 -44
  88. package/dist/cli/llm/openai.js.map +0 -1
  89. package/dist/cli/llm/types.d.ts +0 -14
  90. package/dist/cli/llm/types.d.ts.map +0 -1
  91. package/dist/cli/llm/types.js +0 -2
  92. package/dist/cli/llm/types.js.map +0 -1
@@ -0,0 +1,94 @@
1
+ import type { TSchema } from "typebox";
2
+ import type { TDepSpec, TPipeline, TStage, TStageContext } from "./types.js";
3
+ import type { TReasoningEffort, TToolSpec } from "../llm/types.js";
4
+ export declare function deterministicStage<TOutput>(config: {
5
+ id: string;
6
+ dependsOn: readonly TDepSpec[];
7
+ outputSchema: TSchema;
8
+ fn: (ctx: TStageContext) => Promise<TOutput> | TOutput;
9
+ }): TStage<TOutput>;
10
+ export type TRetryReason = "schema_validation" | "transient" | "rate_limit";
11
+ export type TRetryPolicy = {
12
+ maxAttempts: number;
13
+ backoffMs: number;
14
+ retryOn: readonly TRetryReason[];
15
+ /** Bound on the per-retry appended validation-error fragment. */
16
+ maxAppendedErrorBytes?: number;
17
+ };
18
+ export declare const DEFAULT_RETRY_POLICY: TRetryPolicy;
19
+ /**
20
+ * Thrown by any stage when an `AbortSignal` cancels execution
21
+ * mid-flight. The executor recognizes this class and marks the stage
22
+ * as `skipped` (not `failed`); no `ProcessingFailure` is recorded
23
+ * because a caller-driven cancellation is not a stage failure to
24
+ * report. Distinguishing abort from a genuine provider error matters
25
+ * for server-side cancellation observability (slice 2C's SSE bridge
26
+ * routes these differently).
27
+ */
28
+ export declare class StageAbortedError extends Error {
29
+ readonly stageId: string;
30
+ constructor(args: {
31
+ stageId: string;
32
+ message?: string;
33
+ });
34
+ }
35
+ /**
36
+ * Thrown by `subPipelineStage`'s wrapper when the nested pipeline
37
+ * returns `output: null` (any required dep of its finalize was
38
+ * skipped/failed, or its finalize itself returned null). The wrapper
39
+ * surfaces this as a stage failure on the outer pipeline so the
40
+ * caller sees a clean per-stage failure rather than an LLM-flavored
41
+ * misnomer. Kept separate from `LlmStageRetryExhaustedError` because
42
+ * no LLM call and no retry are involved.
43
+ */
44
+ export declare class SubPipelineFailedError extends Error {
45
+ readonly stageId: string;
46
+ readonly code: string;
47
+ readonly failureContext: Record<string, unknown> | undefined;
48
+ constructor(args: {
49
+ stageId: string;
50
+ code: string;
51
+ message: string;
52
+ context?: Record<string, unknown>;
53
+ });
54
+ }
55
+ /**
56
+ * Thrown internally by `llmStage` after retry exhaustion. The
57
+ * executor catches it and converts it into a `ProcessingFailure`.
58
+ */
59
+ export declare class LlmStageRetryExhaustedError extends Error {
60
+ readonly reason: TRetryReason;
61
+ readonly code: string;
62
+ readonly attempts: number;
63
+ readonly stageId: string;
64
+ readonly failureContext: Record<string, unknown> | undefined;
65
+ constructor(args: {
66
+ stageId: string;
67
+ reason: TRetryReason;
68
+ code: string;
69
+ attempts: number;
70
+ message: string;
71
+ context?: Record<string, unknown>;
72
+ });
73
+ }
74
+ export declare function llmStage<TOutput>(config: {
75
+ id: string;
76
+ dependsOn: readonly TDepSpec[];
77
+ outputSchema: TSchema;
78
+ model: string;
79
+ reasoningEffort?: TReasoningEffort;
80
+ buildPrompt: (ctx: TStageContext) => {
81
+ system: string;
82
+ user: string;
83
+ };
84
+ tools?: readonly TToolSpec[];
85
+ retry?: Partial<TRetryPolicy>;
86
+ maxOutputTokens?: number;
87
+ }): TStage<TOutput>;
88
+ export declare function readStashedTokenUsage(ctx: TStageContext, stageId: string): import("../llm/types.js").TLlmTokenUsage | undefined;
89
+ export declare function subPipelineStage<TOutput>(config: {
90
+ id: string;
91
+ dependsOn: readonly TDepSpec[];
92
+ pipeline: TPipeline<unknown, TOutput>;
93
+ }): TStage<TOutput>;
94
+ //# sourceMappingURL=stage-helpers.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"stage-helpers.d.ts","sourceRoot":"","sources":["../../../src/lib/pipelines/stage-helpers.ts"],"names":[],"mappings":"AASA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,SAAS,CAAA;AAEtC,OAAO,KAAK,EACR,QAAQ,EACR,SAAS,EAET,MAAM,EACN,aAAa,EAChB,MAAM,YAAY,CAAA;AACnB,OAAO,KAAK,EAAe,gBAAgB,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAA;AAK/E,wBAAgB,kBAAkB,CAAC,OAAO,EAAE,MAAM,EAAE;IAChD,EAAE,EAAE,MAAM,CAAA;IACV,SAAS,EAAE,SAAS,QAAQ,EAAE,CAAA;IAC9B,YAAY,EAAE,OAAO,CAAA;IACrB,EAAE,EAAE,CAAC,GAAG,EAAE,aAAa,KAAK,OAAO,CAAC,OAAO,CAAC,GAAG,OAAO,CAAA;CACzD,GAAG,MAAM,CAAC,OAAO,CAAC,CAOlB;AAID,MAAM,MAAM,YAAY,GAAG,mBAAmB,GAAG,WAAW,GAAG,YAAY,CAAA;AA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AAE,MAAM,CAAA;QAChB,OAAO,EAAE,MAAM,CAAA;QACf,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;KACpC;CASJ;AAsDD,wBAAgB,QAAQ,CAAC,OAAO,EAAE,MAAM,EAAE;IACtC,EAAE,EAAE,MAAM,CAAA;IACV,SAAS,EAAE,SAAS,QAAQ,EAAE,CAAA;IAC9B,YAAY,EAAE,OAAO,CAAA;IACrB,KAAK,EAAE,MAAM,CAAA;IACb,eAAe,CAAC,EAAE,gBAAgB,CAAA;IAClC,WAAW,EAAE,CAAC,GAAG,EAAE,aAAa,KAAK;QAAE,MAAM,EAAE,MAAM,CAAC;QAAC,IAAI,EAAE,MAAM,CAAA;KAAE,CAAA;IACrE,KAAK,CAAC,EAAE,SAAS,SAAS,EAAE,CAAA;IAC5B,KAAK,CAAC,EAAE,OAAO,CAAC,YAAY,CAAC,CAAA;IAC7B,eAAe,CAAC,EAAE,MAAM,CAAA;CAC3B,GAAG,MAAM,CAAC,OAAO,CAAC,CA2JlB;AA2BD,wBAAgB,qBAAqB,CACjC,GAAG,EAAE,aAAa,EAClB,OAAO,EAAE,MAAM,GAChB,OAAO,iBAAiB,EAAE,cAAc,GAAG,SAAS,CAEtD;AAID,wBAAgB,gBAAgB,CAAC,OAAO,EAAE,MAAM,EAAE;IAC9C,EAAE,EAAE,MAAM,CAAA;IACV,SAAS,EAAE,SAAS,QAAQ,EAAE,CAAA;IAC9B,QAAQ,EAAE,SAAS,CAAC,OAAO,EAAE,OAAO,CAAC,CAAA;CACxC,GAAG,MAAM,CAAC,OAAO,CAAC,CA0ClB"}
@@ -0,0 +1,354 @@
1
+ // Helpers that compose stages with sensible defaults.
2
+ //
3
+ // - `deterministicStage` — pure (or async-pure) compute, no LLM call.
4
+ // - `llmStage` — wraps an LLM call with structured-output validation
5
+ // and the framework retry policy (schema-validation + transient).
6
+ // - `subPipelineStage` — recursively executes a nested pipeline as a
7
+ // single stage in the outer pipeline. Reserved for future
8
+ // composition; implemented + tested but unused by Phase 1 pipelines.
9
+ import { Value } from "typebox/value";
10
+ import { executePipeline } from "./execute.js";
11
+ // -- Deterministic --
12
+ export function deterministicStage(config) {
13
+ return {
14
+ id: config.id,
15
+ dependsOn: config.dependsOn,
16
+ outputSchema: config.outputSchema,
17
+ run: async (ctx) => config.fn(ctx),
18
+ };
19
+ }
20
+ export const DEFAULT_RETRY_POLICY = {
21
+ maxAttempts: 2,
22
+ backoffMs: 500,
23
+ retryOn: ["schema_validation", "transient"],
24
+ maxAppendedErrorBytes: 2048,
25
+ };
26
+ const TRUNCATION_SUFFIX = "…<truncated>";
27
+ function truncateValidationError(error, capBytes) {
28
+ // NOTE: `capBytes` is measured in JavaScript `string.length` —
29
+ // UTF-16 code units, not UTF-8 bytes. The spec (§6.3) phrases the
30
+ // cap as "bytes"; for ASCII-heavy validation errors the two are
31
+ // equal, and for non-ASCII paths a 2048 cap is roughly 2–4 KB of
32
+ // UTF-8 depending on character distribution. We accept this drift
33
+ // for V1: realistic TypeBox validation errors are short and
34
+ // English; a future polish pass can switch to
35
+ // `new TextEncoder().encode(error).length` if a real workload
36
+ // shows the distinction matters.
37
+ if (error.length <= capBytes) {
38
+ return error;
39
+ }
40
+ const head = Math.max(0, capBytes - TRUNCATION_SUFFIX.length);
41
+ return error.slice(0, head) + TRUNCATION_SUFFIX;
42
+ }
43
+ /**
44
+ * Thrown by any stage when an `AbortSignal` cancels execution
45
+ * mid-flight. The executor recognizes this class and marks the stage
46
+ * as `skipped` (not `failed`); no `ProcessingFailure` is recorded
47
+ * because a caller-driven cancellation is not a stage failure to
48
+ * report. Distinguishing abort from a genuine provider error matters
49
+ * for server-side cancellation observability (slice 2C's SSE bridge
50
+ * routes these differently).
51
+ */
52
+ export class StageAbortedError extends Error {
53
+ stageId;
54
+ constructor(args) {
55
+ super(args.message ?? "aborted");
56
+ this.name = "StageAbortedError";
57
+ this.stageId = args.stageId;
58
+ }
59
+ }
60
+ /**
61
+ * Thrown by `subPipelineStage`'s wrapper when the nested pipeline
62
+ * returns `output: null` (any required dep of its finalize was
63
+ * skipped/failed, or its finalize itself returned null). The wrapper
64
+ * surfaces this as a stage failure on the outer pipeline so the
65
+ * caller sees a clean per-stage failure rather than an LLM-flavored
66
+ * misnomer. Kept separate from `LlmStageRetryExhaustedError` because
67
+ * no LLM call and no retry are involved.
68
+ */
69
+ export class SubPipelineFailedError extends Error {
70
+ stageId;
71
+ code;
72
+ failureContext;
73
+ constructor(args) {
74
+ super(args.message);
75
+ this.name = "SubPipelineFailedError";
76
+ this.stageId = args.stageId;
77
+ this.code = args.code;
78
+ this.failureContext = args.context;
79
+ }
80
+ }
81
+ /**
82
+ * Thrown internally by `llmStage` after retry exhaustion. The
83
+ * executor catches it and converts it into a `ProcessingFailure`.
84
+ */
85
+ export class LlmStageRetryExhaustedError extends Error {
86
+ reason;
87
+ code;
88
+ attempts;
89
+ stageId;
90
+ failureContext;
91
+ constructor(args) {
92
+ super(args.message);
93
+ this.name = "LlmStageRetryExhaustedError";
94
+ this.stageId = args.stageId;
95
+ this.reason = args.reason;
96
+ this.code = args.code;
97
+ this.attempts = args.attempts;
98
+ this.failureContext = args.context;
99
+ }
100
+ }
101
+ function classifyError(err) {
102
+ if (typeof err !== "object" || err === null) {
103
+ return "non_retryable";
104
+ }
105
+ const tag = err.retryReason;
106
+ if (tag === "transient" || tag === "rate_limit") {
107
+ return tag;
108
+ }
109
+ return "non_retryable";
110
+ }
111
+ async function sleep(ms, signal) {
112
+ if (ms <= 0)
113
+ return;
114
+ if (signal.aborted)
115
+ return;
116
+ await new Promise((resolve) => {
117
+ const timer = setTimeout(() => {
118
+ signal.removeEventListener("abort", onAbort);
119
+ resolve();
120
+ }, ms);
121
+ const onAbort = () => {
122
+ clearTimeout(timer);
123
+ signal.removeEventListener("abort", onAbort);
124
+ resolve();
125
+ };
126
+ signal.addEventListener("abort", onAbort, { once: true });
127
+ });
128
+ }
129
+ function emitRetry(ctx, stageId, attempt, reason) {
130
+ const event = {
131
+ kind: "stage:retry",
132
+ stageId,
133
+ attempt,
134
+ reason,
135
+ at: now(),
136
+ };
137
+ ctx.emit(event);
138
+ }
139
+ function now() {
140
+ return typeof performance !== "undefined" && performance.now
141
+ ? performance.now()
142
+ : Date.now();
143
+ }
144
+ // -- LLM stage --
145
+ export function llmStage(config) {
146
+ const policy = {
147
+ ...DEFAULT_RETRY_POLICY,
148
+ ...config.retry,
149
+ retryOn: config.retry?.retryOn ?? DEFAULT_RETRY_POLICY.retryOn,
150
+ };
151
+ const errorCap = policy.maxAppendedErrorBytes ??
152
+ DEFAULT_RETRY_POLICY.maxAppendedErrorBytes ??
153
+ 2048;
154
+ return {
155
+ id: config.id,
156
+ dependsOn: config.dependsOn,
157
+ outputSchema: config.outputSchema,
158
+ run: async (ctx) => {
159
+ const prompt = config.buildPrompt(ctx);
160
+ let userMessage = prompt.user;
161
+ let attempt = 0;
162
+ let lastError = null;
163
+ while (attempt < policy.maxAttempts) {
164
+ attempt += 1;
165
+ if (ctx.signal.aborted) {
166
+ // Loop-top abort: caller cancelled before (or
167
+ // between) attempts. Surface as a `skipped` stage
168
+ // rather than a failure.
169
+ throw new StageAbortedError({ stageId: config.id });
170
+ }
171
+ const req = {
172
+ model: config.model,
173
+ reasoningEffort: config.reasoningEffort,
174
+ systemPrompt: prompt.system,
175
+ userMessage,
176
+ outputSchema: config.outputSchema,
177
+ tools: config.tools,
178
+ maxOutputTokens: config.maxOutputTokens,
179
+ signal: ctx.signal,
180
+ };
181
+ try {
182
+ const response = await ctx.llm.respond(req);
183
+ if (!Value.Check(config.outputSchema, response.output)) {
184
+ const errors = [
185
+ ...Value.Errors(config.outputSchema, response.output),
186
+ ];
187
+ const validationMessage = errors
188
+ .map((e) => `${e.instancePath}: ${e.message}`)
189
+ .join("; ");
190
+ lastError = {
191
+ reason: "schema_validation",
192
+ code: "OUTPUT_SCHEMA_INVALID",
193
+ message: validationMessage,
194
+ };
195
+ const retryable = policy.retryOn.includes("schema_validation");
196
+ if (!retryable || attempt >= policy.maxAttempts) {
197
+ break;
198
+ }
199
+ emitRetry(ctx, config.id, attempt, "schema_validation");
200
+ const truncated = truncateValidationError(validationMessage, errorCap);
201
+ userMessage =
202
+ prompt.user +
203
+ "\n\nYour previous response failed schema validation: " +
204
+ truncated +
205
+ ". Please retry conforming to the schema.";
206
+ await sleep(policy.backoffMs, ctx.signal);
207
+ continue;
208
+ }
209
+ // Token usage emission is handled by the executor at
210
+ // stage:end time; we return the response output and
211
+ // attach the usage onto a side channel via failure-free
212
+ // path.
213
+ // We stash token usage on a per-stage well-known key
214
+ // recognized by the executor.
215
+ stashTokenUsage(ctx, config.id, response.tokenUsage);
216
+ return response.output;
217
+ }
218
+ catch (err) {
219
+ if (err instanceof LlmStageRetryExhaustedError &&
220
+ err.stageId === config.id) {
221
+ // Re-throw our own marker; the catch below shouldn't
222
+ // see it. Defensive.
223
+ throw err;
224
+ }
225
+ if (err instanceof StageAbortedError) {
226
+ throw err;
227
+ }
228
+ // Mid-flight abort surfaces here when the provider
229
+ // honored the signal and threw. Recognize it before
230
+ // classifying as a generic non-retryable failure so
231
+ // the executor can mark this stage `skipped`, not
232
+ // `failed` with `LLM_NON_RETRYABLE_ERROR`.
233
+ if (ctx.signal.aborted) {
234
+ throw new StageAbortedError({ stageId: config.id });
235
+ }
236
+ const reason = classifyError(err);
237
+ const message = err instanceof Error ? err.message : String(err);
238
+ if (reason === "non_retryable") {
239
+ lastError = {
240
+ reason: "transient",
241
+ code: "LLM_NON_RETRYABLE_ERROR",
242
+ message,
243
+ };
244
+ break;
245
+ }
246
+ lastError = {
247
+ reason,
248
+ code: reason === "rate_limit"
249
+ ? "LLM_RATE_LIMITED"
250
+ : "LLM_TRANSIENT_ERROR",
251
+ message,
252
+ };
253
+ if (!policy.retryOn.includes(reason)) {
254
+ break;
255
+ }
256
+ if (attempt >= policy.maxAttempts) {
257
+ break;
258
+ }
259
+ emitRetry(ctx, config.id, attempt, reason);
260
+ await sleep(policy.backoffMs, ctx.signal);
261
+ }
262
+ }
263
+ const failure = lastError ?? {
264
+ reason: "transient",
265
+ code: "LLM_UNKNOWN_ERROR",
266
+ message: "llmStage retry loop exited without error context",
267
+ };
268
+ throw new LlmStageRetryExhaustedError({
269
+ stageId: config.id,
270
+ reason: failure.reason,
271
+ code: failure.code,
272
+ attempts: attempt,
273
+ message: failure.message,
274
+ context: failure.context,
275
+ });
276
+ },
277
+ };
278
+ }
279
+ // -- Token-usage side channel ---------------------------------------------
280
+ //
281
+ // llmStage and the executor share a tiny side-channel so the executor
282
+ // can attach token usage to the matching `stage:end` event without
283
+ // changing the stage `run` return shape. The side channel is a per-
284
+ // pipeline-run weak map keyed by the StageContext object.
285
+ const TOKEN_USAGE_CHANNELS = new WeakMap();
286
+ function stashTokenUsage(ctx, stageId, usage) {
287
+ let bucket = TOKEN_USAGE_CHANNELS.get(ctx);
288
+ if (!bucket) {
289
+ bucket = new Map();
290
+ TOKEN_USAGE_CHANNELS.set(ctx, bucket);
291
+ }
292
+ bucket.set(stageId, usage);
293
+ }
294
+ export function readStashedTokenUsage(ctx, stageId) {
295
+ return TOKEN_USAGE_CHANNELS.get(ctx)?.get(stageId);
296
+ }
297
+ // -- Sub-pipeline --
298
+ export function subPipelineStage(config) {
299
+ return {
300
+ id: config.id,
301
+ dependsOn: config.dependsOn,
302
+ outputSchema: config.pipeline.outputSchema,
303
+ run: async (ctx) => {
304
+ const prefix = `${config.id}::`;
305
+ const forwarded = (event) => {
306
+ ctx.emit(prefixSubPipelineEvent(prefix, event));
307
+ };
308
+ const result = await executePipeline(config.pipeline, ctx.input, {
309
+ llm: ctx.llm,
310
+ generateId: ctx.generateId,
311
+ signal: ctx.signal,
312
+ onEvent: forwarded,
313
+ });
314
+ for (const failure of result.failures) {
315
+ ctx.addFailure({
316
+ code: failure.code,
317
+ message: failure.message,
318
+ severity: failure.severity,
319
+ context: {
320
+ ...(failure.context ?? {}),
321
+ subPipelineStageId: config.id,
322
+ subPipelineStage: failure.stage,
323
+ },
324
+ });
325
+ }
326
+ if (result.output === null) {
327
+ throw new SubPipelineFailedError({
328
+ stageId: config.id,
329
+ code: "SUB_PIPELINE_NULL_OUTPUT",
330
+ message: "Nested pipeline returned null output; the outer stage cannot complete.",
331
+ context: {
332
+ subPipelineId: config.pipeline.id,
333
+ },
334
+ });
335
+ }
336
+ return result.output;
337
+ },
338
+ };
339
+ }
340
+ function prefixSubPipelineEvent(prefix, event) {
341
+ switch (event.kind) {
342
+ case "pipeline:start":
343
+ return { ...event, pipelineId: prefix + event.pipelineId };
344
+ case "pipeline:end":
345
+ return event;
346
+ case "stage:start":
347
+ return { ...event, stageId: prefix + event.stageId };
348
+ case "stage:end":
349
+ return { ...event, stageId: prefix + event.stageId };
350
+ case "stage:retry":
351
+ return { ...event, stageId: prefix + event.stageId };
352
+ }
353
+ }
354
+ //# sourceMappingURL=stage-helpers.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"stage-helpers.js","sourceRoot":"","sources":["../../../src/lib/pipelines/stage-helpers.ts"],"names":[],"mappings":"AAAA,sDAAsD;AACtD,EAAE;AACF,sEAAsE;AACtE,qEAAqE;AACrE,oEAAoE;AACpE,qEAAqE;AACrE,4DAA4D;AAC5D,uEAAuE;AAGvE,OAAO,EAAE,KAAK,EAAE,MAAM,eAAe,CAAA;AASrC,OAAO,EAAE,eAAe,EAAE,MAAM,cAAc,CAAA;AAE9C,sBAAsB;AAEtB,MAAM,UAAU,kBAAkB,CAAU,MAK3C;IACG,OAAO;QACH,EAAE,EAAE,MAAM,CAAC,EAAE;QACb,SAAS,EAAE,MAAM,CAAC,SAAS;QAC3B,YAAY,EAAE,MAAM,CAAC,YAAY;QACjC,GAAG,EAAE,KAAK,EAAE,GAAG,EAAE,EAAE,CAAC,MAAM,CAAC,EAAE,CAAC,GAAG,CAAC;KACrC,CAAA;AACL,CAAC;AAcD,MAAM,CAAC,MAAM,oBAAoB,GAAiB;IAC9C,WAAW,EAAE,CAAC;IACd,SAAS,EAAE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B,QAAQ,KAAK,CAAC,IAAI,EAAE,CAAC;QACjB,KAAK,gBAAgB;YACjB,OAAO,EAAE,GAAG,KAAK,EAAE,UAAU,EAAE,MAAM,GAAG,KAAK,CAAC,UAAU,EAAE,CAAA;QAC9D,KAAK,cAAc;YACf,OAAO,KAAK,CAAA;QAChB,KAAK,aAAa;YACd,OAAO,EAAE,GAAG,KAAK,EAAE,OAAO,EAAE,MAAM,GAAG,KAAK,CAAC,OAAO,EAAE,CAAA;QACxD,KAAK,WAAW;YACZ,OAAO,EAAE,GAAG,KAAK,EAAE,OAAO,EAAE,MAAM,GAAG,KAAK,CAAC,OAAO,EAAE,CAAA;QACxD,KAAK,aAAa;YACd,OAAO,EAAE,GAAG,KAAK,EAAE,OAAO,EAAE,MAAM,GAAG,KAAK,CAAC,OAAO,EAAE,CAAA;IAC5D,CAAC;AACL,CAAC"}
@@ -0,0 +1,88 @@
1
+ import type { TSchema } from "typebox";
2
+ import type { TLlmProvider, TLlmTokenUsage } from "../llm/types.js";
3
+ export type TOptionalDep = {
4
+ readonly __optional: true;
5
+ readonly id: string;
6
+ };
7
+ export type TDepSpec = string | TOptionalDep;
8
+ /**
9
+ * Wrap a stage id as an optional dependency. Optional upstream
10
+ * outcomes never propagate as skips: the depending stage runs even
11
+ * when the upstream skipped or failed, but `ctx.get` returns
12
+ * `undefined` for that id.
13
+ */
14
+ export declare function optional(id: string): TOptionalDep;
15
+ export declare function isOptionalDep(dep: TDepSpec): dep is TOptionalDep;
16
+ export declare function depId(dep: TDepSpec): string;
17
+ export type TStageStatus = "completed" | "skipped" | "failed";
18
+ export type TStageContext = {
19
+ input: unknown;
20
+ get<T>(stageId: string): T | undefined;
21
+ stageStatus(stageId: string): TStageStatus;
22
+ llm: TLlmProvider;
23
+ generateId: () => string;
24
+ signal: AbortSignal;
25
+ emit: (event: TPipelineEvent) => void;
26
+ addFailure: (failure: Omit<TProcessingFailure, "stage">) => void;
27
+ };
28
+ export type TStage<TOutput> = {
29
+ id: string;
30
+ dependsOn: readonly TDepSpec[];
31
+ outputSchema: TSchema;
32
+ run: (ctx: TStageContext) => Promise<TOutput>;
33
+ };
34
+ export type TPipelineFinalize<TOutput> = {
35
+ dependsOn: readonly TDepSpec[];
36
+ run: (ctx: TStageContext) => TOutput;
37
+ };
38
+ export type TPipeline<TInput, TOutput> = {
39
+ id: string;
40
+ version: string;
41
+ inputSchema: TSchema;
42
+ outputSchema: TSchema;
43
+ stages: readonly TStage<unknown>[];
44
+ finalize: TPipelineFinalize<TOutput>;
45
+ /** Phantom field so `TInput` participates in inference. */
46
+ _inputTypeMarker?: TInput;
47
+ };
48
+ export type TProcessingFailure = {
49
+ stage: string;
50
+ code: string;
51
+ message: string;
52
+ severity: "warning" | "error";
53
+ context?: Record<string, unknown>;
54
+ };
55
+ export type TPipelineResult<TOutput> = {
56
+ output: TOutput | null;
57
+ failures: TProcessingFailure[];
58
+ stageOutcomes: Record<string, TStageStatus>;
59
+ tokenUsage?: TLlmTokenUsage;
60
+ };
61
+ export type TPipelineEvent = {
62
+ kind: "pipeline:start";
63
+ pipelineId: string;
64
+ pipelineVersion: string;
65
+ at: number;
66
+ } | {
67
+ kind: "pipeline:end";
68
+ status: "completed" | "failed";
69
+ output: "present" | "null";
70
+ at: number;
71
+ } | {
72
+ kind: "stage:start";
73
+ stageId: string;
74
+ at: number;
75
+ } | {
76
+ kind: "stage:end";
77
+ stageId: string;
78
+ status: TStageStatus;
79
+ tokenUsage?: TLlmTokenUsage;
80
+ at: number;
81
+ } | {
82
+ kind: "stage:retry";
83
+ stageId: string;
84
+ attempt: number;
85
+ reason: string;
86
+ at: number;
87
+ };
88
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../src/lib/pipelines/types.ts"],"names":[],"mappings":"AAqBA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,SAAS,CAAA;AACtC,OAAO,KAAK,EAAE,YAAY,EAAE,cAAc,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E,EAAE,MAAM,CAAA;CACb,GACD;IAAE,IAAI,EAAE,aAAa,CAAC;IAAC,OAAO,EAAE,MAAM,CAAC;IAAC,EAAE,EAAE,MAAM,CAAA;CAAE,GACpD;IACI,IAAI,EAAE,WAAW,CAAA;IACjB,OAAO,EAAE,MAAM,CAAA;IACf,MAAM,EAAE,YAAY,CAAA;IACpB,UAAU,CAAC,EAAE,cAAc,CAAA;IAC3B,EAAE,EAAE,MAAM,CAAA;CACb,GACD;IACI,IAAI,EAAE,aAAa,CAAA;IACnB,OAAO,EAAE,MAAM,CAAA;IACf,OAAO,EAAE,MAAM,CAAA;IACf,MAAM,EAAE,MAAM,CAAA;IACd,EAAE,EAAE,MAAM,CAAA;CACb,CAAA"}
@@ -0,0 +1,36 @@
1
+ // Pipeline framework primitives — shared types for the executor,
2
+ // stage helpers, and consumers.
3
+ //
4
+ // Type aliases use the repo's T-prefixed PascalCase convention
5
+ // (enforced by `@typescript-eslint/naming-convention`); the public
6
+ // vocabulary maps to the spec's unprefixed names as follows:
7
+ //
8
+ // spec name ↔ repo type
9
+ // --------------- -------------------
10
+ // Stage<TOutput> TStage<TOutput>
11
+ // Pipeline<TIn,TOut> TPipeline<TInput, TOutput>
12
+ // StageContext TStageContext
13
+ // ProcessingFailure TProcessingFailure
14
+ // PipelineResult<T> TPipelineResult<TOutput>
15
+ // PipelineEvent TPipelineEvent
16
+ // DepSpec TDepSpec
17
+ // OptionalDep TOptionalDep
18
+ //
19
+ // Helper functions (`optional`, `executePipeline`, …) keep their
20
+ // spec-spelled camelCase names since they are values, not types.
21
+ /**
22
+ * Wrap a stage id as an optional dependency. Optional upstream
23
+ * outcomes never propagate as skips: the depending stage runs even
24
+ * when the upstream skipped or failed, but `ctx.get` returns
25
+ * `undefined` for that id.
26
+ */
27
+ export function optional(id) {
28
+ return { __optional: true, id };
29
+ }
30
+ export function isOptionalDep(dep) {
31
+ return typeof dep !== "string" && dep.__optional === true;
32
+ }
33
+ export function depId(dep) {
34
+ return typeof dep === "string" ? dep : dep.id;
35
+ }
36
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../../../src/lib/pipelines/types.ts"],"names":[],"mappings":"AAAA,iEAAiE;AACjE,gCAAgC;AAChC,EAAE;AACF,+DAA+D;AAC/D,mEAAmE;AACnE,6DAA6D;AAC7D,EAAE;AACF,kCAAkC;AAClC,4CAA4C;AAC5C,wCAAwC;AACxC,mDAAmD;AACnD,sCAAsC;AACtC,2CAA2C;AAC3C,iDAAiD;AACjD,uCAAuC;AACvC,iCAAiC;AACjC,qCAAqC;AACrC,EAAE;AACF,iEAAiE;AACjE,iEAAiE;AAmBjE;;;;;GAKG;AACH,MAAM,UAAU,QAAQ,CAAC,EAAU;IAC/B,OAAO,EAAE,UAAU,EAAE,IAAI,EAAE,EAAE,EAAE,CAAA;AACnC,CAAC;AAED,MAAM,UAAU,aAAa,CAAC,GAAa;IACvC,OAAO,OAAO,GAAG,KAAK,QAAQ,IAAI,GAAG,CAAC,UAAU,KAAK,IAAI,CAAA;AAC7D,CAAC;AAED,MAAM,UAAU,KAAK,CAAC,GAAa;IAC/B,OAAO,OAAO,GAAG,KAAK,QAAQ,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,GAAG,CAAC,EAAE,CAAA;AACjD,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@proposit/proposit-core",
3
- "version": "1.0.2",
3
+ "version": "1.1.1",
4
4
  "description": "Core engine for building and manipulating propositional logic arguments.",
5
5
  "license": "MIT",
6
6
  "keywords": [
@@ -32,6 +32,11 @@
32
32
  "types": "./dist/extensions/basics/index.d.ts",
33
33
  "import": "./dist/extensions/basics/index.js",
34
34
  "default": "./dist/extensions/basics/index.js"
35
+ },
36
+ "./extensions/openai": {
37
+ "types": "./dist/extensions/openai/index.d.ts",
38
+ "import": "./dist/extensions/openai/index.js",
39
+ "default": "./dist/extensions/openai/index.js"
35
40
  }
36
41
  },
37
42
  "bin": {
@@ -66,6 +71,14 @@
66
71
  "typebox": "^1.1.14",
67
72
  "yaml": "^2.8.3"
68
73
  },
74
+ "peerDependencies": {
75
+ "openai": ">=4.0.0"
76
+ },
77
+ "peerDependenciesMeta": {
78
+ "openai": {
79
+ "optional": true
80
+ }
81
+ },
69
82
  "scripts": {
70
83
  "generate:parser": "peggy --format es -o src/lib/core/parser/formula-gen.js src/lib/core/parser/formula.peggy",
71
84
  "build": "rm -rf dist tsconfig.build.tsbuildinfo && pnpm run generate:parser && pnpm tsc -p tsconfig.build.json && cp src/lib/core/parser/formula-gen.js dist/lib/core/parser/formula-gen.js && pnpm run docs",
@@ -1,4 +0,0 @@
1
- import type { TLlmProvider, TLlmProviderOptions } from "./types.js";
2
- export declare const OPENAI_API_KEY_ENV = "OPENAI_API_KEY";
3
- export declare function createOpenAiProvider(options: TLlmProviderOptions): TLlmProvider;
4
- //# sourceMappingURL=openai.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"openai.d.ts","sourceRoot":"","sources":["../../../src/cli/llm/openai.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,YAAY,EAAE,mBAAmB,EAAE,MAAM,YAAY,CAAA;AAInE,eAAO,MAAM,kBAAkB,mBAAmB,CAAA;AAElD,wBAAgB,oBAAoB,CAChC,OAAO,EAAE,mBAAmB,GAC7B,YAAY,CAmDd"}