@fastpaca/cria 1.4.0 → 1.6.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.
Files changed (129) hide show
  1. package/README.md +37 -27
  2. package/dist/dsl/builder.d.ts +275 -0
  3. package/dist/dsl/builder.d.ts.map +1 -0
  4. package/dist/dsl/builder.js +569 -0
  5. package/dist/dsl/builder.js.map +1 -0
  6. package/dist/dsl/index.d.ts +78 -0
  7. package/dist/dsl/index.d.ts.map +1 -0
  8. package/dist/dsl/index.js +92 -0
  9. package/dist/dsl/index.js.map +1 -0
  10. package/dist/dsl/strategies.d.ts +29 -0
  11. package/dist/dsl/strategies.d.ts.map +1 -0
  12. package/dist/dsl/strategies.js +62 -0
  13. package/dist/dsl/strategies.js.map +1 -0
  14. package/dist/dsl/summary.d.ts +51 -0
  15. package/dist/dsl/summary.d.ts.map +1 -0
  16. package/dist/dsl/summary.js +56 -0
  17. package/dist/dsl/summary.js.map +1 -0
  18. package/dist/dsl/templating.d.ts +31 -0
  19. package/dist/dsl/templating.d.ts.map +1 -0
  20. package/dist/dsl/templating.js +83 -0
  21. package/dist/dsl/templating.js.map +1 -0
  22. package/dist/{components → dsl}/vector-search.d.ts +11 -34
  23. package/dist/dsl/vector-search.d.ts.map +1 -0
  24. package/dist/{components → dsl}/vector-search.js +12 -34
  25. package/dist/dsl/vector-search.js.map +1 -0
  26. package/dist/eval/index.d.ts +16 -2
  27. package/dist/eval/index.d.ts.map +1 -1
  28. package/dist/eval/judge.d.ts +7 -5
  29. package/dist/eval/judge.d.ts.map +1 -1
  30. package/dist/eval/judge.js +18 -5
  31. package/dist/eval/judge.js.map +1 -1
  32. package/dist/index.d.ts +12 -11
  33. package/dist/index.d.ts.map +1 -1
  34. package/dist/index.js +8 -8
  35. package/dist/index.js.map +1 -1
  36. package/dist/instrumentation/otel.js +5 -14
  37. package/dist/instrumentation/otel.js.map +1 -1
  38. package/dist/memory/chroma/index.js +4 -4
  39. package/dist/protocols/chat-completions.d.ts +38 -0
  40. package/dist/protocols/chat-completions.d.ts.map +1 -0
  41. package/dist/protocols/chat-completions.js +94 -0
  42. package/dist/protocols/chat-completions.js.map +1 -0
  43. package/dist/protocols/responses.d.ts +80 -0
  44. package/dist/protocols/responses.d.ts.map +1 -0
  45. package/dist/protocols/responses.js +148 -0
  46. package/dist/protocols/responses.js.map +1 -0
  47. package/dist/provider.d.ts +94 -0
  48. package/dist/provider.d.ts.map +1 -0
  49. package/dist/provider.js +60 -0
  50. package/dist/provider.js.map +1 -0
  51. package/dist/providers/ai-sdk.d.ts +44 -0
  52. package/dist/providers/ai-sdk.d.ts.map +1 -0
  53. package/dist/providers/ai-sdk.js +144 -0
  54. package/dist/providers/ai-sdk.js.map +1 -0
  55. package/dist/providers/anthropic.d.ts +48 -0
  56. package/dist/providers/anthropic.d.ts.map +1 -0
  57. package/dist/providers/anthropic.js +279 -0
  58. package/dist/providers/anthropic.js.map +1 -0
  59. package/dist/providers/openai.d.ts +64 -0
  60. package/dist/providers/openai.d.ts.map +1 -0
  61. package/dist/providers/openai.js +411 -0
  62. package/dist/providers/openai.js.map +1 -0
  63. package/dist/render.d.ts +25 -22
  64. package/dist/render.d.ts.map +1 -1
  65. package/dist/render.js +194 -63
  66. package/dist/render.js.map +1 -1
  67. package/dist/testing/plaintext.d.ts +29 -0
  68. package/dist/testing/plaintext.d.ts.map +1 -0
  69. package/dist/testing/plaintext.js +106 -0
  70. package/dist/testing/plaintext.js.map +1 -0
  71. package/dist/types.d.ts +186 -174
  72. package/dist/types.d.ts.map +1 -1
  73. package/dist/types.js +22 -104
  74. package/dist/types.js.map +1 -1
  75. package/package.json +25 -49
  76. package/dist/components/index.d.ts +0 -147
  77. package/dist/components/index.d.ts.map +0 -1
  78. package/dist/components/index.js +0 -181
  79. package/dist/components/index.js.map +0 -1
  80. package/dist/components/summary.d.ts +0 -89
  81. package/dist/components/summary.d.ts.map +0 -1
  82. package/dist/components/summary.js +0 -118
  83. package/dist/components/summary.js.map +0 -1
  84. package/dist/components/vector-search.d.ts.map +0 -1
  85. package/dist/components/vector-search.js.map +0 -1
  86. package/dist/dsl.d.ts +0 -233
  87. package/dist/dsl.d.ts.map +0 -1
  88. package/dist/dsl.js +0 -428
  89. package/dist/dsl.js.map +0 -1
  90. package/dist/integrations/ai-sdk.d.ts +0 -22
  91. package/dist/integrations/ai-sdk.d.ts.map +0 -1
  92. package/dist/integrations/ai-sdk.js +0 -172
  93. package/dist/integrations/ai-sdk.js.map +0 -1
  94. package/dist/integrations/anthropic.d.ts +0 -50
  95. package/dist/integrations/anthropic.d.ts.map +0 -1
  96. package/dist/integrations/anthropic.js +0 -220
  97. package/dist/integrations/anthropic.js.map +0 -1
  98. package/dist/integrations/openai.d.ts +0 -48
  99. package/dist/integrations/openai.d.ts.map +0 -1
  100. package/dist/integrations/openai.js +0 -265
  101. package/dist/integrations/openai.js.map +0 -1
  102. package/dist/jsx/index.d.ts +0 -6
  103. package/dist/jsx/index.d.ts.map +0 -1
  104. package/dist/jsx/index.js +0 -7
  105. package/dist/jsx/index.js.map +0 -1
  106. package/dist/jsx/jsx-dev-runtime.d.ts +0 -2
  107. package/dist/jsx/jsx-dev-runtime.d.ts.map +0 -1
  108. package/dist/jsx/jsx-dev-runtime.js +0 -3
  109. package/dist/jsx/jsx-dev-runtime.js.map +0 -1
  110. package/dist/jsx/jsx-runtime.d.ts +0 -40
  111. package/dist/jsx/jsx-runtime.d.ts.map +0 -1
  112. package/dist/jsx/jsx-runtime.js +0 -40
  113. package/dist/jsx/jsx-runtime.js.map +0 -1
  114. package/dist/renderers/markdown.d.ts +0 -3
  115. package/dist/renderers/markdown.d.ts.map +0 -1
  116. package/dist/renderers/markdown.js +0 -43
  117. package/dist/renderers/markdown.js.map +0 -1
  118. package/dist/renderers/shared.d.ts +0 -82
  119. package/dist/renderers/shared.d.ts.map +0 -1
  120. package/dist/renderers/shared.js +0 -156
  121. package/dist/renderers/shared.js.map +0 -1
  122. package/dist/snapshot.d.ts +0 -47
  123. package/dist/snapshot.d.ts.map +0 -1
  124. package/dist/snapshot.js +0 -144
  125. package/dist/snapshot.js.map +0 -1
  126. package/dist/tokenizers.d.ts +0 -14
  127. package/dist/tokenizers.d.ts.map +0 -1
  128. package/dist/tokenizers.js +0 -45
  129. package/dist/tokenizers.js.map +0 -1
package/dist/types.d.ts CHANGED
@@ -1,231 +1,237 @@
1
- import { z } from "zod";
2
1
  /**
3
- * Message role used by semantic `kind: "message"` regions.
2
+ * Prompt pipeline mental model (authoritative + opinionated):
4
3
  *
5
- * This is intentionally compatible with common LLM SDKs (system/user/assistant/tool),
6
- * while still allowing custom roles for bespoke targets.
4
+ * Fluent DSL
5
+ * |
6
+ * v
7
+ * PromptTree (scopes + message leaves)
8
+ * |
9
+ * v
10
+ * PromptLayout (flat, role-shaped messages)
11
+ * |
12
+ * v
13
+ * RenderOut (provider payloads)
14
+ *
15
+ * Why this shape?
16
+ * - Scopes exist for compaction/strategy. They are structural only.
17
+ * - Messages are semantic boundaries. They are leaf nodes and only hold parts.
18
+ * - Parts are the smallest typed units (text/reasoning/tool-call/tool-result).
19
+ *
20
+ * PromptLayout intentionally normalizes message shapes so codecs do NOT
21
+ * re-interpret parts or re-check invariants. Some providers (AI SDK, Anthropic)
22
+ * require a parts array, so codecs re-expand assistant/tool data back into
23
+ * parts. That is a translation step for provider compatibility, not a loop in
24
+ * the core model.
25
+ */
26
+ import type { ModelProvider } from "./provider";
27
+ /**
28
+ * Message role used by semantic `kind: "message"` nodes.
29
+ *
30
+ * This is intentionally compatible with common LLM SDKs
31
+ * (system/developer/user/assistant/tool).
7
32
  */
8
- export declare const PromptRoleSchema: z.ZodString;
9
- export type PromptRole = z.infer<typeof PromptRoleSchema>;
33
+ export type PromptRole = "system" | "developer" | "user" | "assistant" | "tool";
10
34
  /**
11
- * A model provider that can generate completions.
35
+ * Provider-specific tool IO contract.
12
36
  *
13
- * This abstraction allows Cria components to call AI models without
14
- * being coupled to a specific SDK. Each provider specifies its own
15
- * rendered message type (e.g., AI SDK's ModelMessage[], OpenAI's
16
- * ChatCompletionMessageParam[]).
37
+ * Each provider pins the concrete types used for tool-call inputs and
38
+ * tool-result outputs. Those types flow through the prompt tree and layout so
39
+ * codecs can translate without defensive serialization later.
17
40
  */
18
- export interface ModelProvider<TRendered> {
19
- /** Provider identifier for debugging */
20
- name: string;
21
- /**
22
- * Tokenizer for this provider's model.
23
- *
24
- * Used for budget fitting when the caller doesn't pass a tokenizer directly.
25
- * Providers should supply an estimate that matches the chosen model; callers
26
- * can still override via render options.
27
- */
28
- tokenizer?: Tokenizer;
29
- /** Renderer that produces provider-specific prompt input. */
30
- renderer: PromptRenderer<TRendered>;
31
- /** Generate a text completion from rendered prompt input. */
32
- completion(rendered: TRendered): MaybePromise<string>;
33
- /**
34
- * Generate a structured object validated against the schema.
35
- *
36
- * Implementations should use native structured output when available
37
- * (e.g., AI SDK's generateObject, OpenAI's json_schema response_format),
38
- * falling back to completion + JSON.parse + schema.parse internally.
39
- */
40
- object<T>(rendered: TRendered, schema: z.ZodType<T>): MaybePromise<T>;
41
+ export interface ProviderToolIO {
42
+ callInput: unknown;
43
+ resultOutput: unknown;
41
44
  }
45
+ /**
46
+ * Tool IO for an unbound prompt.
47
+ *
48
+ * If a prompt has no provider, tool IO is "never" so tool parts cannot be
49
+ * constructed until a provider supplies the IO contract. This avoids runtime
50
+ * checks by pushing the constraint into the type system.
51
+ */
52
+ interface UnboundToolIO {
53
+ callInput: never;
54
+ resultOutput: never;
55
+ }
56
+ /**
57
+ * Resolve the tool IO contract for a given provider type.
58
+ *
59
+ * This is the bridge between a provider binding and the rest of the DSL:
60
+ * it extracts the provider's tool IO types so parts/messages/layouts carry
61
+ * the correct shapes end-to-end.
62
+ */
63
+ export type ToolIOForProvider<P> = P extends ModelProvider<unknown, infer TToolIO> ? TToolIO : UnboundToolIO;
64
+ type ToolCallInput<TToolIO extends ProviderToolIO> = TToolIO["callInput"];
65
+ type ToolResultOutput<TToolIO extends ProviderToolIO> = TToolIO["resultOutput"];
42
66
  /**
43
67
  * Context that can be provided through the component tree.
44
68
  *
45
- * Provider scopes inject context that child components can access during
46
- * rendering and strategy execution.
69
+ * Provider scopes inject context so children inherit the same provider binding
70
+ * and tool IO contract during rendering and strategy execution.
47
71
  */
48
72
  export interface CriaContext {
49
73
  /** Model provider for AI-powered operations */
50
- provider?: ModelProvider<unknown> | undefined;
74
+ provider?: ModelProvider<unknown, ProviderToolIO> | undefined;
51
75
  }
52
76
  export type MaybePromise<T> = T | Promise<T>;
53
77
  /**
54
- * A function that counts tokens in a string.
55
- * Cria doesn't bundle a tokenizer. You provide one.
78
+ * Content parts that appear as leaf nodes in message nodes.
56
79
  *
57
- * @example
58
- * ```typescript
59
- * import { encoding_for_model } from "tiktoken";
60
- *
61
- * const enc = encoding_for_model("gpt-4");
62
- * const tokenizer: Tokenizer = (text) => enc.encode(text).length;
63
- * ```
80
+ * Tool parts directly embed provider-native input/output shapes, so a bound
81
+ * provider determines their types without runtime validation.
64
82
  */
65
- export type Tokenizer = (text: string) => number;
66
- export declare const PromptKindSchema: z.ZodUnion<[z.ZodObject<{
67
- kind: z.ZodOptional<z.ZodUndefined>;
68
- }, "strip", z.ZodTypeAny, {
69
- kind?: undefined;
70
- }, {
71
- kind?: undefined;
72
- }>, z.ZodObject<{
73
- kind: z.ZodLiteral<"message">;
74
- role: z.ZodString;
75
- }, "strip", z.ZodTypeAny, {
76
- kind: "message";
77
- role: string;
78
- }, {
79
- kind: "message";
80
- role: string;
81
- }>, z.ZodObject<{
82
- kind: z.ZodLiteral<"tool-call">;
83
- toolCallId: z.ZodString;
84
- toolName: z.ZodString;
85
- input: z.ZodUnknown;
86
- }, "strip", z.ZodTypeAny, {
87
- kind: "tool-call";
83
+ export interface ToolCallPart<TToolIO extends ProviderToolIO = ProviderToolIO> {
84
+ type: "tool-call";
88
85
  toolCallId: string;
89
86
  toolName: string;
90
- input?: unknown;
91
- }, {
92
- kind: "tool-call";
93
- toolCallId: string;
94
- toolName: string;
95
- input?: unknown;
96
- }>, z.ZodObject<{
97
- kind: z.ZodLiteral<"tool-result">;
98
- toolCallId: z.ZodString;
99
- toolName: z.ZodString;
100
- output: z.ZodUnknown;
101
- }, "strip", z.ZodTypeAny, {
102
- kind: "tool-result";
103
- toolCallId: string;
104
- toolName: string;
105
- output?: unknown;
106
- }, {
107
- kind: "tool-result";
87
+ input: ToolCallInput<TToolIO>;
88
+ }
89
+ export interface ToolResultPart<TToolIO extends ProviderToolIO = ProviderToolIO> {
90
+ type: "tool-result";
108
91
  toolCallId: string;
109
92
  toolName: string;
110
- output?: unknown;
111
- }>, z.ZodObject<{
112
- kind: z.ZodLiteral<"reasoning">;
113
- text: z.ZodString;
114
- }, "strip", z.ZodTypeAny, {
115
- kind: "reasoning";
93
+ output: ToolResultOutput<TToolIO>;
94
+ }
95
+ /**
96
+ * The smallest typed units used inside messages.
97
+ *
98
+ * Keeping tool IO typed at the part level forces every higher-level structure
99
+ * (messages/layout/tree) to stay provider-consistent.
100
+ */
101
+ export type PromptPart<TToolIO extends ProviderToolIO = ProviderToolIO> = {
102
+ type: "text";
116
103
  text: string;
117
- }, {
118
- kind: "reasoning";
104
+ } | {
105
+ type: "reasoning";
119
106
  text: string;
120
- }>]>;
121
- export type PromptKind = z.infer<typeof PromptKindSchema>;
122
- export type PromptNodeKind = PromptKind["kind"];
123
- export interface PromptElementBase {
107
+ } | ToolCallPart<TToolIO> | ToolResultPart<TToolIO>;
108
+ /** Convenience type for reasoning parts in a typed prompt. */
109
+ export type ReasoningPart<TToolIO extends ProviderToolIO = ProviderToolIO> = Extract<PromptPart<TToolIO>, {
110
+ type: "reasoning";
111
+ }>;
112
+ /** Convenience type for text parts in a typed prompt. */
113
+ export type TextPart<TToolIO extends ProviderToolIO = ProviderToolIO> = Extract<PromptPart<TToolIO>, {
114
+ type: "text";
115
+ }>;
116
+ /**
117
+ * Structural scope node in the prompt tree.
118
+ *
119
+ * Scopes group messages for composition and compaction while keeping the tool
120
+ * IO types consistent across all descendants.
121
+ */
122
+ export interface PromptScope<TToolIO extends ProviderToolIO = ProviderToolIO> {
123
+ kind: "scope";
124
124
  priority: number;
125
125
  strategy?: Strategy | undefined;
126
126
  id?: string | undefined;
127
127
  context?: CriaContext | undefined;
128
- children: PromptChildren;
128
+ children: readonly PromptNode<TToolIO>[];
129
129
  }
130
- export type PromptElement = (PromptElementBase & {
131
- kind?: undefined;
132
- }) | (PromptElementBase & {
130
+ /**
131
+ * Message boundary node in the prompt tree.
132
+ *
133
+ * A message node is role-shaped and contains parts that already carry the
134
+ * provider-bound tool IO types.
135
+ */
136
+ export interface PromptMessageNode<TToolIO extends ProviderToolIO = ProviderToolIO> {
133
137
  kind: "message";
134
138
  role: PromptRole;
135
- }) | (PromptElementBase & {
136
- kind: "tool-call";
137
- toolCallId: string;
138
- toolName: string;
139
- input: unknown;
140
- }) | (PromptElementBase & {
141
- kind: "tool-result";
142
- toolCallId: string;
143
- toolName: string;
144
- output: unknown;
145
- }) | (PromptElementBase & {
146
- kind: "reasoning";
147
- text: string;
148
- });
149
- export type PromptChild = string | PromptElement;
150
- export type PromptChildren = PromptChild[];
151
- export declare const PromptElementSchema: z.ZodType<PromptElement>;
152
- export declare const PromptChildSchema: z.ZodType<PromptChild>;
153
- export declare const PromptChildrenSchema: z.ZodType<PromptChildren>;
139
+ id?: string | undefined;
140
+ children: readonly PromptPart<TToolIO>[];
141
+ }
154
142
  /**
155
- * A renderer that converts a fitted prompt tree into an output format.
143
+ * Nodes in the prompt tree.
156
144
  *
157
- * Renderers are used for two things:
158
- * - **Token accounting / fitting** via `tokenString` (a stable string projection)
159
- * - **Final output** via `render` (can be async, and can produce any type)
145
+ * Scopes provide structure and strategy boundaries. Message nodes are the
146
+ * semantic leaves that carry typed parts. The generic parameter ensures tool
147
+ * IO types stay consistent across the entire tree.
148
+ */
149
+ export type PromptNode<TToolIO extends ProviderToolIO = ProviderToolIO> = PromptScope<TToolIO> | PromptMessageNode<TToolIO>;
150
+ /**
151
+ * Root prompt tree type.
160
152
  *
161
- * @template TOutput - The produced output type (e.g. `string`, `ModelMessage[]`, etc.).
153
+ * The root is always a scope so strategies can operate at the top level while
154
+ * preserving the provider-bound tool IO types for all descendants.
162
155
  */
163
- export interface PromptRenderer<TOutput> {
164
- /** A short identifier for debugging/observability. */
165
- name: string;
166
- /**
167
- * A deterministic string projection of the prompt tree used for token counting.
168
- *
169
- * Important properties:
170
- * - **Pure / deterministic**: same tree => same string
171
- * - **Cheap**: called frequently during fitting
172
- * - **Representative**: should correlate with what `render()` produces (especially for string targets)
173
- *
174
- * For structured targets (e.g. AI SDK messages), this can be a markdown-ish projection
175
- * that approximates the effective prompt content for token budgeting.
176
- */
177
- tokenString: (element: PromptElement) => string;
178
- /**
179
- * Render the fitted prompt tree to the target output.
180
- *
181
- * May be async (e.g. when a renderer needs to fetch/resolve attachments, or when
182
- * strategies summarized content during fitting).
183
- */
184
- render: (element: PromptElement) => MaybePromise<TOutput>;
185
- /**
186
- * The “empty” value for this renderer.
187
- *
188
- * Used when the budget is <= 0, or when strategies remove the entire tree.
189
- */
190
- empty: () => TOutput;
156
+ export type PromptTree<TToolIO extends ProviderToolIO = ProviderToolIO> = PromptScope<TToolIO>;
157
+ /** Shorthand for scope children with a shared tool IO contract. */
158
+ export type ScopeChildren<TToolIO extends ProviderToolIO = ProviderToolIO> = readonly PromptNode<TToolIO>[];
159
+ /** Shorthand for message children with a shared tool IO contract. */
160
+ export type MessageChildren<TToolIO extends ProviderToolIO = ProviderToolIO> = readonly PromptPart<TToolIO>[];
161
+ /** System messages are plain text and carry no tool information. */
162
+ export interface SystemMessage {
163
+ role: "system";
164
+ text: string;
165
+ }
166
+ /** Developer messages are plain text and carry no tool information. */
167
+ export interface DeveloperMessage {
168
+ role: "developer";
169
+ text: string;
170
+ }
171
+ /** User messages are plain text and carry no tool information. */
172
+ export interface UserMessage {
173
+ role: "user";
174
+ text: string;
191
175
  }
192
176
  /**
193
- * Canonical normalized child node type stored in the IR.
177
+ * Assistant messages can include reasoning and tool calls in addition to text.
178
+ * Tool call inputs are typed by the provider binding.
179
+ */
180
+ export interface AssistantMessage<TToolIO extends ProviderToolIO = ProviderToolIO> {
181
+ role: "assistant";
182
+ text: string;
183
+ reasoning?: string | undefined;
184
+ toolCalls?: readonly ToolCallPart<TToolIO>[] | undefined;
185
+ }
186
+ /**
187
+ * Tool messages represent a single tool result with provider-typed output.
188
+ */
189
+ export interface ToolMessage<TToolIO extends ProviderToolIO = ProviderToolIO> {
190
+ role: "tool";
191
+ toolCallId: string;
192
+ toolName: string;
193
+ output: ToolResultOutput<TToolIO>;
194
+ }
195
+ /**
196
+ * PromptLayout is a list of fully-shaped messages. The union is deliberate:
197
+ * - Assistant messages can include reasoning/toolCalls.
198
+ * - Tool messages are singular tool results with call metadata.
199
+ * - System/User messages are text-only.
200
+ * This keeps invalid combinations out of the layout by construction.
201
+ *
202
+ * The layout is the normalized form that codecs consume. It is produced by
203
+ * flattening the prompt tree and should not require further validation.
204
+ */
205
+ export type PromptMessage<TToolIO extends ProviderToolIO = ProviderToolIO> = SystemMessage | DeveloperMessage | UserMessage | AssistantMessage<TToolIO> | ToolMessage<TToolIO>;
206
+ /** Flat, role-shaped message list used by codecs and token counting. */
207
+ export type PromptLayout<TToolIO extends ProviderToolIO = ProviderToolIO> = readonly PromptMessage<TToolIO>[];
208
+ /**
209
+ * Result of applying a strategy to a scope.
194
210
  *
195
- * This is the only type you’ll find inside `PromptElement.children` after child normalization.
211
+ * Returning null means "drop this scope entirely".
196
212
  */
197
- export type JsonValue = null | string | number | boolean | JsonValue[] | {
198
- [key: string]: JsonValue;
199
- };
200
- export declare const JsonValueSchema: z.ZodType<JsonValue>;
201
- export type StrategyResult = PromptElement | null;
213
+ export type StrategyResult<TToolIO extends ProviderToolIO = ProviderToolIO> = PromptScope<TToolIO> | null;
202
214
  /**
203
215
  * Context passed to strategy functions during the fit loop.
204
216
  *
205
- * @property target - The specific region to reduce
206
- * @property budget - The total token budget we're trying to fit within
207
- * @property tokenizer - Function to count tokens in a string
208
- * @property tokenString - Renderer-provided projection used for token counting
217
+ * @property target - The specific scope to reduce
209
218
  * @property totalTokens - Current total token count for the prompt
210
219
  * @property iteration - Which iteration of the fit loop (for debugging)
211
220
  * @property context - Inherited context from ancestor provider components
212
221
  */
213
- export interface StrategyInput {
214
- target: PromptElement;
215
- budget: number;
216
- tokenizer: Tokenizer;
217
- tokenString: (element: PromptElement) => string;
222
+ export interface StrategyInput<TToolIO extends ProviderToolIO = ProviderToolIO> {
223
+ target: PromptScope<TToolIO>;
218
224
  totalTokens: number;
219
225
  iteration: number;
220
226
  /** Context inherited from ancestor provider components */
221
227
  context: CriaContext;
222
228
  }
223
229
  /**
224
- * A Strategy function that rewrites a region subtree when the prompt is over budget.
230
+ * A Strategy function that rewrites a scope subtree when the prompt is over budget.
225
231
  *
226
232
  * Strategies are applied during fitting, starting from the least important
227
233
  * priority (highest number). Strategies run **bottom-up** (post-order) so nested
228
- * regions get a chance to shrink before their parents.
234
+ * scopes get a chance to shrink before their parents.
229
235
  *
230
236
  * Strategies must be:
231
237
  * - **Pure**: don't mutate the input element
@@ -234,11 +240,16 @@ export interface StrategyInput {
234
240
  *
235
241
  * Strategies have full ownership of their subtree: they can replace the element
236
242
  * and/or rewrite any children.
243
+ *
244
+ * The generic parameter ensures strategies preserve the tool IO contract
245
+ * instead of re-shaping it.
237
246
  */
238
- export type Strategy = (input: StrategyInput) => MaybePromise<StrategyResult>;
247
+ export type Strategy = <TToolIO extends ProviderToolIO>(input: StrategyInput<TToolIO>) => MaybePromise<StrategyResult<TToolIO>>;
239
248
  /**
240
249
  * Error thrown when the prompt cannot be fit within the budget.
241
250
  *
251
+ * This is a standard error that should be caught and handled by the caller.
252
+ *
242
253
  * This happens when:
243
254
  * - No strategies remain but still over budget
244
255
  * - Strategies made no progress (possible infinite loop)
@@ -261,4 +272,5 @@ export declare class FitError extends Error {
261
272
  totalTokens: number;
262
273
  });
263
274
  }
275
+ export {};
264
276
  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CACtB;AAED;;;;;;;;;;;;;;;;;GAiBG;AACH,MAAM,MAAM,QAAQ,GAAG,CAAC,OAAO,SAAS,cAAc,EACpD,KAAK,EAAE,aAAa,CAAC,OAAO,CAAC,KAC1B,YAAY,CAAC,cAAc,CAAC,OAAO,CAAC,CAAC,CAAC;AAE3C;;;;;;;;;;;;GAYG;AACH,qBAAa,QAAS,SAAQ,KAAK;IACjC,YAAY,EAAE,MAAM,CAAC;IACrB,QAAQ,EAAE,MAAM,CAAC;IACjB,SAAS,EAAE,MAAM,CAAC;IAClB,MAAM,EAAE,MAAM,CAAC;IACf,WAAW,EAAE,MAAM,CAAC;gBAER,MAAM,EAAE;QAClB,YAAY,EAAE,MAAM,CAAC;QACrB,QAAQ,EAAE,MAAM,CAAC;QACjB,SAAS,EAAE,MAAM,CAAC;QAClB,MAAM,EAAE,MAAM,CAAC;QACf,WAAW,EAAE,MAAM,CAAC;KACrB;CAYF"}
package/dist/types.js CHANGED
@@ -1,115 +1,33 @@
1
- import { z } from "zod";
2
1
  /**
3
- * Message role used by semantic `kind: "message"` regions.
2
+ * Prompt pipeline mental model (authoritative + opinionated):
4
3
  *
5
- * This is intentionally compatible with common LLM SDKs (system/user/assistant/tool),
6
- * while still allowing custom roles for bespoke targets.
7
- */
8
- export const PromptRoleSchema = z
9
- .string()
10
- .describe('Message role used by semantic `kind: "message"` regions (system/user/assistant/tool/custom).');
11
- /**
12
- * Semantic variants for a region node.
4
+ * Fluent DSL
5
+ * |
6
+ * v
7
+ * PromptTree (scopes + message leaves)
8
+ * |
9
+ * v
10
+ * PromptLayout (flat, role-shaped messages)
11
+ * |
12
+ * v
13
+ * RenderOut (provider payloads)
13
14
  *
14
- * Cria’s IR is “Regions all the way down” (like a DOM tree). `PromptKindSchema`
15
- * defines how we recognize prompt parts so renderers can emit structured targets
16
- * without parsing strings.
17
- */
18
- const PromptKindNoneSchema = z.object({ kind: z.undefined().optional() });
19
- const PromptKindMessageSchema = z.object({
20
- kind: z.literal("message"),
21
- role: PromptRoleSchema,
22
- });
23
- const PromptKindToolCallSchema = z.object({
24
- kind: z.literal("tool-call"),
25
- toolCallId: z.string(),
26
- toolName: z.string(),
27
- input: z.unknown(),
28
- });
29
- const PromptKindToolResultSchema = z.object({
30
- kind: z.literal("tool-result"),
31
- toolCallId: z.string(),
32
- toolName: z.string(),
33
- output: z.unknown(),
34
- });
35
- const PromptKindReasoningSchema = z.object({
36
- kind: z.literal("reasoning"),
37
- text: z.string(),
38
- });
39
- export const PromptKindSchema = z.union([
40
- PromptKindNoneSchema,
41
- PromptKindMessageSchema,
42
- PromptKindToolCallSchema,
43
- PromptKindToolResultSchema,
44
- PromptKindReasoningSchema,
45
- ]);
46
- /**
47
- * The core IR node type. All Cria components return a `PromptElement`.
15
+ * Why this shape?
16
+ * - Scopes exist for compaction/strategy. They are structural only.
17
+ * - Messages are semantic boundaries. They are leaf nodes and only hold parts.
18
+ * - Parts are the smallest typed units (text/reasoning/tool-call/tool-result).
48
19
  *
49
- * **Everything is a Region** (think: a DOM `<div>`): `priority`, `strategy`, and
50
- * `children` make up the structural prompt tree.
51
- *
52
- * If you attach a semantic `kind` (via `PromptKind`), the node becomes a recognized
53
- * prompt part (message/tool-call/tool-result/reasoning) and renderers can emit
54
- * structured targets without parsing strings.
55
- *
56
- * `PromptElementSchema` is the single source of truth for validation and type inference.
20
+ * PromptLayout intentionally normalizes message shapes so codecs do NOT
21
+ * re-interpret parts or re-check invariants. Some providers (AI SDK, Anthropic)
22
+ * require a parts array, so codecs re-expand assistant/tool data back into
23
+ * parts. That is a translation step for provider compatibility, not a loop in
24
+ * the core model.
57
25
  */
58
- const strategyValidator = (value) => typeof value === "function";
59
- const PromptBaseSchema = z
60
- .object({
61
- priority: z.number(),
62
- strategy: z.custom(strategyValidator).optional(),
63
- id: z.string().optional(),
64
- context: z.custom().optional(),
65
- })
66
- .strict();
67
- export const PromptElementSchema = z.lazy(() => z.union([
68
- PromptBaseSchema.extend({
69
- kind: z.undefined().optional(),
70
- children: z.array(z.union([z.string(), z.lazy(() => PromptElementSchema)])),
71
- }),
72
- PromptBaseSchema.extend({
73
- kind: z.literal("message"),
74
- role: PromptRoleSchema,
75
- children: z.array(z.union([z.string(), z.lazy(() => PromptElementSchema)])),
76
- }),
77
- PromptBaseSchema.extend({
78
- kind: z.literal("tool-call"),
79
- toolCallId: z.string(),
80
- toolName: z.string(),
81
- input: z.unknown(),
82
- children: z.array(z.union([z.string(), z.lazy(() => PromptElementSchema)])),
83
- }),
84
- PromptBaseSchema.extend({
85
- kind: z.literal("tool-result"),
86
- toolCallId: z.string(),
87
- toolName: z.string(),
88
- output: z.unknown(),
89
- children: z.array(z.union([z.string(), z.lazy(() => PromptElementSchema)])),
90
- }),
91
- PromptBaseSchema.extend({
92
- kind: z.literal("reasoning"),
93
- text: z.string(),
94
- children: z.array(z.union([z.string(), z.lazy(() => PromptElementSchema)])),
95
- }),
96
- ]));
97
- export const PromptChildSchema = z.union([
98
- z.string(),
99
- z.lazy(() => PromptElementSchema),
100
- ]);
101
- export const PromptChildrenSchema = z.array(PromptChildSchema);
102
- export const JsonValueSchema = z.lazy(() => z.union([
103
- z.null(),
104
- z.string(),
105
- z.number(),
106
- z.boolean(),
107
- z.array(JsonValueSchema),
108
- z.record(JsonValueSchema),
109
- ]));
110
26
  /**
111
27
  * Error thrown when the prompt cannot be fit within the budget.
112
28
  *
29
+ * This is a standard error that should be caught and handled by the caller.
30
+ *
113
31
  * This happens when:
114
32
  * - No strategies remain but still over budget
115
33
  * - Strategies made no progress (possible infinite loop)
package/dist/types.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB;;;;;GAKG;AACH,MAAM,CAAC,MAAM,gBAAgB,GAAG,CAAC;KAC9B,MAAM,EAAE;KACR,QAAQ,CACP,8FAA8F,CAC/F,CAAC;AAoEJ;;;;;;GAM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