@clearideas/agent-runtime-contracts 0.7.0 → 0.8.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.d.ts +167 -1
- package/dist/index.js +257 -2
- package/package.json +2 -2
package/dist/index.d.ts
CHANGED
|
@@ -99,6 +99,130 @@ export interface PromptMessage {
|
|
|
99
99
|
/** Provider-specific replay information forwarded by compatible model adapters. */
|
|
100
100
|
providerOptions?: JsonObject;
|
|
101
101
|
}
|
|
102
|
+
/**
|
|
103
|
+
* The special variable through which steps read a run's conversation history.
|
|
104
|
+
* It is reserved only in manifests that declare `messages`.
|
|
105
|
+
*/
|
|
106
|
+
export declare const MESSAGES_VARIABLE: "messages";
|
|
107
|
+
export declare const MESSAGES_SIGNATURE_VERSION: "1";
|
|
108
|
+
export declare const MESSAGES_SIGNATURE_ALGORITHM: "HMAC-SHA256";
|
|
109
|
+
/** Content a conversation message may carry. Provider options are never accepted. */
|
|
110
|
+
export type ConversationContentPart = ({
|
|
111
|
+
type: "text";
|
|
112
|
+
text: string;
|
|
113
|
+
} | {
|
|
114
|
+
type: "json";
|
|
115
|
+
value: JsonValue;
|
|
116
|
+
} | {
|
|
117
|
+
type: "image";
|
|
118
|
+
artifact?: ArtifactRef;
|
|
119
|
+
url?: string;
|
|
120
|
+
/** Base64-encoded bytes. Stored as an artifact when the run is created. */
|
|
121
|
+
data?: string;
|
|
122
|
+
mediaType?: string;
|
|
123
|
+
} | {
|
|
124
|
+
type: "file";
|
|
125
|
+
artifact?: ArtifactRef;
|
|
126
|
+
url?: string;
|
|
127
|
+
/** Base64-encoded bytes. Stored as an artifact when the run is created. */
|
|
128
|
+
data?: string;
|
|
129
|
+
name?: string;
|
|
130
|
+
mediaType?: string;
|
|
131
|
+
}) & {
|
|
132
|
+
/** Opaque application metadata. Persisted and signed, never sent to a model. */
|
|
133
|
+
metadata?: JsonObject;
|
|
134
|
+
};
|
|
135
|
+
/** One message of a conversation history supplied to, or returned by, a run. */
|
|
136
|
+
export interface ConversationMessage {
|
|
137
|
+
role: "user" | "assistant";
|
|
138
|
+
content: ConversationContentPart[];
|
|
139
|
+
/**
|
|
140
|
+
* Opaque application metadata. `metadata.agentRuntime` is reserved for the
|
|
141
|
+
* runtime's leading summary or note marker.
|
|
142
|
+
*/
|
|
143
|
+
metadata?: JsonObject;
|
|
144
|
+
}
|
|
145
|
+
/** A conversation message as supplied: `content` may be a string shorthand for one text part. */
|
|
146
|
+
export interface ConversationMessageInput {
|
|
147
|
+
role: "user" | "assistant";
|
|
148
|
+
content: string | ConversationContentPart[];
|
|
149
|
+
metadata?: JsonObject;
|
|
150
|
+
}
|
|
151
|
+
/** An HMAC over an exact prefix of a history and the host's scope. */
|
|
152
|
+
export interface MessagesSignature {
|
|
153
|
+
version: typeof MESSAGES_SIGNATURE_VERSION;
|
|
154
|
+
algorithm: typeof MESSAGES_SIGNATURE_ALGORITHM;
|
|
155
|
+
/** Signing key identifier, for rotation. */
|
|
156
|
+
keyId: string;
|
|
157
|
+
/** Number of leading messages covered. */
|
|
158
|
+
count: number;
|
|
159
|
+
/** base64url MAC. */
|
|
160
|
+
value: string;
|
|
161
|
+
}
|
|
162
|
+
/** What a leading runtime marker message stands for. */
|
|
163
|
+
export interface MessagesMarker {
|
|
164
|
+
kind: "summary" | "note";
|
|
165
|
+
/** Earlier messages it replaces, cumulative across compactions. */
|
|
166
|
+
replaces: number;
|
|
167
|
+
/**
|
|
168
|
+
* Completed turns it covers, cumulative across compactions. A turn is one
|
|
169
|
+
* exchange: the user's messages and the reply that completes them. Turn N
|
|
170
|
+
* ends with the N-th assistant reply of the conversation.
|
|
171
|
+
*/
|
|
172
|
+
turns?: number;
|
|
173
|
+
/** Compactions in the conversation so far. */
|
|
174
|
+
sequence: number;
|
|
175
|
+
strategy: "summarize" | "prune";
|
|
176
|
+
}
|
|
177
|
+
/** The agent accepts a conversation history. Absent: no history is accepted. */
|
|
178
|
+
export interface AgentMessagesDeclaration {
|
|
179
|
+
/** A run must supply a history. Default false. */
|
|
180
|
+
required?: boolean;
|
|
181
|
+
description?: string;
|
|
182
|
+
/** Compaction trigger: message count, including a leading marker. */
|
|
183
|
+
maxMessages?: number;
|
|
184
|
+
/** Compaction trigger: UTF-8 bytes of the history JSON (media as references). */
|
|
185
|
+
maxBytes?: number;
|
|
186
|
+
/** Every message before the new turn must be covered by a valid signature. */
|
|
187
|
+
requireSignature?: boolean;
|
|
188
|
+
/** History-only overrides merged over the manifest's `policies.context`. */
|
|
189
|
+
context?: ContextPolicy;
|
|
190
|
+
/**
|
|
191
|
+
* Wording of the leading marker a compaction writes. Templates over
|
|
192
|
+
* `context.firstTurn`, `context.lastTurn`, `context.turns` (for example
|
|
193
|
+
* "1–10"), and `context.messages` (messages replaced).
|
|
194
|
+
*/
|
|
195
|
+
markers?: {
|
|
196
|
+
/** Heading of a summary. */
|
|
197
|
+
summary?: string;
|
|
198
|
+
/** Text of a note that records removed messages. */
|
|
199
|
+
note?: string;
|
|
200
|
+
};
|
|
201
|
+
metadata?: JsonObject;
|
|
202
|
+
}
|
|
203
|
+
/** How one step sees the history. */
|
|
204
|
+
export type StepHistory = "all" | {
|
|
205
|
+
/** Newest N messages. Absent: every message. */
|
|
206
|
+
last?: number;
|
|
207
|
+
/** Include a leading summary or note. Default: true without `last`, false with it. */
|
|
208
|
+
summary?: boolean;
|
|
209
|
+
/** Prompt steps: insert the selection as model turns. Default true. */
|
|
210
|
+
insert?: boolean;
|
|
211
|
+
/** Media handling for this step. Default include. */
|
|
212
|
+
media?: "include" | "latest" | "omit";
|
|
213
|
+
};
|
|
214
|
+
/** The effective history of a run, carried by every checkpoint. */
|
|
215
|
+
export interface RunMessagesState {
|
|
216
|
+
history: ConversationMessage[];
|
|
217
|
+
/** `pending` until run-level compaction has finished; it never runs again after `complete`. */
|
|
218
|
+
compaction: "pending" | "complete";
|
|
219
|
+
/** Leading messages covered by a verified signature when the run was created. */
|
|
220
|
+
signedCount: number;
|
|
221
|
+
/** Host scope bound into signatures. */
|
|
222
|
+
scope?: string;
|
|
223
|
+
/** Completed summarizer calls of an unfinished compaction. */
|
|
224
|
+
journal?: JsonObject;
|
|
225
|
+
}
|
|
102
226
|
export interface TranscriptItem {
|
|
103
227
|
id: string;
|
|
104
228
|
type: "message" | "reasoning" | "tool-call" | "tool-result" | "artifact" | "error";
|
|
@@ -224,7 +348,7 @@ export declare const CONTEXT_STRATEGIES: readonly ["summarize", "prune", "summar
|
|
|
224
348
|
export type ContextStrategy = (typeof CONTEXT_STRATEGIES)[number];
|
|
225
349
|
/** The model and instructions used when a context strategy summarizes. */
|
|
226
350
|
/** What the summarizer condenses. */
|
|
227
|
-
export declare const SUMMARY_KINDS: readonly ["input", "transcript", "tool-result"];
|
|
351
|
+
export declare const SUMMARY_KINDS: readonly ["input", "transcript", "tool-result", "messages"];
|
|
228
352
|
export type SummaryKind = (typeof SUMMARY_KINDS)[number];
|
|
229
353
|
export interface ContextSummarizer {
|
|
230
354
|
/** Defaults to the step model, then the manifest model. */
|
|
@@ -238,6 +362,8 @@ export interface ContextSummarizer {
|
|
|
238
362
|
input?: string;
|
|
239
363
|
transcript?: string;
|
|
240
364
|
toolResult?: string;
|
|
365
|
+
/** Conversation history compaction. */
|
|
366
|
+
messages?: string;
|
|
241
367
|
};
|
|
242
368
|
/** Output-token limit for each summarizer call. Defaults from the target size. */
|
|
243
369
|
maxOutputTokens?: number;
|
|
@@ -345,6 +471,12 @@ export interface AgentStepBase {
|
|
|
345
471
|
name?: string;
|
|
346
472
|
description?: string;
|
|
347
473
|
when?: string;
|
|
474
|
+
/**
|
|
475
|
+
* Which conversation messages this step sees. Requires a manifest
|
|
476
|
+
* `messages` declaration. Narrows both the `messages` variable and, for
|
|
477
|
+
* prompt steps, the turns inserted into the model input.
|
|
478
|
+
*/
|
|
479
|
+
history?: StepHistory;
|
|
348
480
|
outputVariable?: string;
|
|
349
481
|
includeInFinalOutput?: boolean;
|
|
350
482
|
policies?: ExecutionPolicies;
|
|
@@ -374,6 +506,12 @@ export type PromptStep = PromptStepDefinition & ({
|
|
|
374
506
|
messages: PromptMessage[];
|
|
375
507
|
prompt?: never;
|
|
376
508
|
systemPrompt?: never;
|
|
509
|
+
} | {
|
|
510
|
+
/** System prompt plus the conversation history, with no extra user prompt. */
|
|
511
|
+
history: StepHistory;
|
|
512
|
+
systemPrompt?: string;
|
|
513
|
+
prompt?: never;
|
|
514
|
+
messages?: never;
|
|
377
515
|
});
|
|
378
516
|
export interface LoopDefinition {
|
|
379
517
|
source?: string;
|
|
@@ -435,6 +573,7 @@ export declare const contextPolicySchema: z.ZodType<ContextPolicy>;
|
|
|
435
573
|
export declare const executionPoliciesSchema: z.ZodType<ExecutionPolicies>;
|
|
436
574
|
/** Policy fields that only a prompt step can use. */
|
|
437
575
|
export declare const PROMPT_ONLY_POLICY_FIELDS: readonly ["context", "toolCallLimit", "modelRetry", "outputRepair"];
|
|
576
|
+
export declare const stepHistorySchema: z.ZodType<StepHistory>;
|
|
438
577
|
export declare const promptMessageSchema: z.ZodType<PromptMessage>;
|
|
439
578
|
export declare const agentWorkspaceBindingSchema: z.ZodType<AgentWorkspaceBinding>;
|
|
440
579
|
export declare const agentStepSchema: z.ZodType<AgentStep>;
|
|
@@ -448,6 +587,8 @@ export interface AgentManifest {
|
|
|
448
587
|
connections?: AgentConnectionBinding[];
|
|
449
588
|
/** Host-defined file collections available to workspace tools and code steps. */
|
|
450
589
|
workspaces?: AgentWorkspaceBinding[];
|
|
590
|
+
/** The agent accepts a conversation history from its runs. */
|
|
591
|
+
messages?: AgentMessagesDeclaration;
|
|
451
592
|
steps: AgentStep[];
|
|
452
593
|
/** Manifest-wide execution policy defaults; steps may override them. */
|
|
453
594
|
policies?: ExecutionPolicies;
|
|
@@ -463,6 +604,7 @@ export interface AgentManifest {
|
|
|
463
604
|
metadata?: JsonObject;
|
|
464
605
|
extensions?: JsonObject;
|
|
465
606
|
}
|
|
607
|
+
export declare const agentMessagesDeclarationSchema: z.ZodType<AgentMessagesDeclaration>;
|
|
466
608
|
export declare const agentManifestSchema: z.ZodType<AgentManifest>;
|
|
467
609
|
export interface AgentRunManifest {
|
|
468
610
|
schemaVersion: typeof AGENT_RUN_MANIFEST_SCHEMA_VERSION;
|
|
@@ -473,6 +615,10 @@ export interface AgentRunManifest {
|
|
|
473
615
|
variables?: AgentVariableOverride[];
|
|
474
616
|
execution?: AgentRunExecution;
|
|
475
617
|
budget?: AgentRunBudget;
|
|
618
|
+
/** Conversation history for an agent that declares `messages`. */
|
|
619
|
+
messages?: ConversationMessageInput[];
|
|
620
|
+
/** The runtime's signature over a leading prefix of `messages`. */
|
|
621
|
+
messagesSignature?: MessagesSignature;
|
|
476
622
|
}
|
|
477
623
|
export interface AgentRunBudget {
|
|
478
624
|
/** Cumulative reported model tokens allowed across all run attempts. */
|
|
@@ -531,6 +677,8 @@ export interface RunCheckpoint {
|
|
|
531
677
|
continuation?: JsonObject;
|
|
532
678
|
/** Cumulative model-token usage and the effective limit for this run. */
|
|
533
679
|
budget?: RunBudgetState;
|
|
680
|
+
/** The run's effective conversation history, when the manifest declares messages. */
|
|
681
|
+
messages?: RunMessagesState;
|
|
534
682
|
createdAt: string;
|
|
535
683
|
metadata?: JsonObject;
|
|
536
684
|
}
|
|
@@ -567,6 +715,16 @@ export declare const runErrorSchema: z.ZodObject<{
|
|
|
567
715
|
details: z.ZodOptional<z.ZodType<JsonObject, unknown, z.core.$ZodTypeInternals<JsonObject, unknown>>>;
|
|
568
716
|
stack: z.ZodOptional<z.ZodString>;
|
|
569
717
|
}, z.core.$strict>;
|
|
718
|
+
/** Reads a leading marker from message metadata; undefined when there is none. */
|
|
719
|
+
export declare const messagesMarkerOf: (message: ConversationMessage | undefined) => MessagesMarker | undefined;
|
|
720
|
+
/** A normalized conversation history: full content-part form only. */
|
|
721
|
+
export declare const conversationMessagesSchema: z.ZodType<ConversationMessage[]>;
|
|
722
|
+
/** A supplied conversation history; string content is normalized to one text part. */
|
|
723
|
+
export declare const conversationMessagesInputSchema: z.ZodType<ConversationMessage[]>;
|
|
724
|
+
export declare const messagesSignatureSchema: z.ZodType<MessagesSignature>;
|
|
725
|
+
export declare const runMessagesStateSchema: z.ZodType<RunMessagesState>;
|
|
726
|
+
/** Parse and normalize a supplied conversation history. */
|
|
727
|
+
export declare const parseConversationMessages: (input: unknown) => ConversationMessage[];
|
|
570
728
|
export declare const transcriptItemSchema: z.ZodObject<{
|
|
571
729
|
id: z.ZodString;
|
|
572
730
|
type: z.ZodEnum<{
|
|
@@ -1254,6 +1412,7 @@ export declare const runCheckpointSchema: z.ZodObject<{
|
|
|
1254
1412
|
}, z.core.$strict>>>;
|
|
1255
1413
|
continuation: z.ZodOptional<z.ZodType<JsonObject, unknown, z.core.$ZodTypeInternals<JsonObject, unknown>>>;
|
|
1256
1414
|
budget: z.ZodOptional<z.ZodType<RunBudgetState, unknown, z.core.$ZodTypeInternals<RunBudgetState, unknown>>>;
|
|
1415
|
+
messages: z.ZodOptional<z.ZodLazy<z.ZodType<RunMessagesState, unknown, z.core.$ZodTypeInternals<RunMessagesState, unknown>>>>;
|
|
1257
1416
|
createdAt: z.ZodISODateTime;
|
|
1258
1417
|
metadata: z.ZodOptional<z.ZodType<JsonObject, unknown, z.core.$ZodTypeInternals<JsonObject, unknown>>>;
|
|
1259
1418
|
}, z.core.$strict>;
|
|
@@ -1280,6 +1439,13 @@ export interface RunResult {
|
|
|
1280
1439
|
transcript: TranscriptItem[];
|
|
1281
1440
|
artifacts: ArtifactRef[];
|
|
1282
1441
|
usage?: ModelUsage;
|
|
1442
|
+
/**
|
|
1443
|
+
* The effective conversation history plus one assistant message containing
|
|
1444
|
+
* the final output. Present when the manifest declares messages.
|
|
1445
|
+
*/
|
|
1446
|
+
messages?: ConversationMessage[];
|
|
1447
|
+
/** Present when the host configured a signer. Covers every message. */
|
|
1448
|
+
messagesSignature?: MessagesSignature;
|
|
1283
1449
|
startedAt: string;
|
|
1284
1450
|
completedAt: string;
|
|
1285
1451
|
metadata?: JsonObject;
|
package/dist/index.js
CHANGED
|
@@ -41,6 +41,14 @@ export const agentVariableKeySchema = z
|
|
|
41
41
|
.max(100)
|
|
42
42
|
.refine((value) => !value.includes("."), "Variable keys cannot contain dots; use dot notation only to read properties inside object values.")
|
|
43
43
|
.refine((value) => !RESERVED_VARIABLE_PATH_SEGMENTS.has(value.toLowerCase()), "Variable keys cannot use prototype-sensitive names.");
|
|
44
|
+
// ─── Conversation messages ──────────────────────────────────────────────────
|
|
45
|
+
/**
|
|
46
|
+
* The special variable through which steps read a run's conversation history.
|
|
47
|
+
* It is reserved only in manifests that declare `messages`.
|
|
48
|
+
*/
|
|
49
|
+
export const MESSAGES_VARIABLE = "messages";
|
|
50
|
+
export const MESSAGES_SIGNATURE_VERSION = "1";
|
|
51
|
+
export const MESSAGES_SIGNATURE_ALGORITHM = "HMAC-SHA256";
|
|
44
52
|
export const WORKSPACE_MODES = ["read", "read_write"];
|
|
45
53
|
export const valueMatchesAgentVariableType = (value, type) => {
|
|
46
54
|
if (type === "array")
|
|
@@ -107,7 +115,12 @@ export const CONTEXT_STRATEGIES = [
|
|
|
107
115
|
];
|
|
108
116
|
/** The model and instructions used when a context strategy summarizes. */
|
|
109
117
|
/** What the summarizer condenses. */
|
|
110
|
-
export const SUMMARY_KINDS = [
|
|
118
|
+
export const SUMMARY_KINDS = [
|
|
119
|
+
"input",
|
|
120
|
+
"transcript",
|
|
121
|
+
"tool-result",
|
|
122
|
+
"messages",
|
|
123
|
+
];
|
|
111
124
|
const explicitModelReferenceSchema = z.strictObject({
|
|
112
125
|
provider: z.string().min(1),
|
|
113
126
|
model: z.string().min(1),
|
|
@@ -134,6 +147,7 @@ export const contextPolicySchema = z.strictObject({
|
|
|
134
147
|
input: z.string().min(1).optional(),
|
|
135
148
|
transcript: z.string().min(1).optional(),
|
|
136
149
|
toolResult: z.string().min(1).optional(),
|
|
150
|
+
messages: z.string().min(1).optional(),
|
|
137
151
|
})
|
|
138
152
|
.optional(),
|
|
139
153
|
maxOutputTokens: z.number().int().positive().optional(),
|
|
@@ -199,11 +213,21 @@ export const PROMPT_ONLY_POLICY_FIELDS = [
|
|
|
199
213
|
"modelRetry",
|
|
200
214
|
"outputRepair",
|
|
201
215
|
];
|
|
216
|
+
export const stepHistorySchema = z.union([
|
|
217
|
+
z.literal("all"),
|
|
218
|
+
z.strictObject({
|
|
219
|
+
last: z.number().int().positive().optional(),
|
|
220
|
+
summary: z.boolean().optional(),
|
|
221
|
+
insert: z.boolean().optional(),
|
|
222
|
+
media: z.enum(["include", "latest", "omit"]).optional(),
|
|
223
|
+
}),
|
|
224
|
+
]);
|
|
202
225
|
const stepBaseShape = {
|
|
203
226
|
id: z.string().min(1),
|
|
204
227
|
name: z.string().min(1).optional(),
|
|
205
228
|
description: z.string().optional(),
|
|
206
229
|
when: z.string().optional(),
|
|
230
|
+
history: stepHistorySchema.optional(),
|
|
207
231
|
outputVariable: agentVariableKeySchema.optional(),
|
|
208
232
|
includeInFinalOutput: z.boolean().optional(),
|
|
209
233
|
policies: executionPoliciesSchema.optional(),
|
|
@@ -317,12 +341,20 @@ export const agentStepSchema = z.lazy(() => z.discriminatedUnion("type", [
|
|
|
317
341
|
.superRefine((step, context) => {
|
|
318
342
|
const usesLegacyPrompt = step.prompt !== undefined;
|
|
319
343
|
const usesMessages = step.messages !== undefined;
|
|
320
|
-
|
|
344
|
+
const insertsHistory = step.history !== undefined &&
|
|
345
|
+
(step.history === "all" || step.history.insert !== false);
|
|
346
|
+
if (usesLegacyPrompt && usesMessages) {
|
|
321
347
|
context.addIssue({
|
|
322
348
|
code: "custom",
|
|
323
349
|
message: "Prompt steps require exactly one of prompt or messages.",
|
|
324
350
|
});
|
|
325
351
|
}
|
|
352
|
+
else if (!usesLegacyPrompt && !usesMessages && !insertsHistory) {
|
|
353
|
+
context.addIssue({
|
|
354
|
+
code: "custom",
|
|
355
|
+
message: "Prompt steps require prompt, messages, or history that inserts conversation turns.",
|
|
356
|
+
});
|
|
357
|
+
}
|
|
326
358
|
if (usesMessages && step.systemPrompt !== undefined) {
|
|
327
359
|
context.addIssue({
|
|
328
360
|
code: "custom",
|
|
@@ -528,6 +560,79 @@ const validateStepPolicies = (steps, context, path) => {
|
|
|
528
560
|
}
|
|
529
561
|
});
|
|
530
562
|
};
|
|
563
|
+
export const agentMessagesDeclarationSchema = z.strictObject({
|
|
564
|
+
required: z.boolean().optional(),
|
|
565
|
+
description: z.string().optional(),
|
|
566
|
+
maxMessages: z.number().int().min(2).optional(),
|
|
567
|
+
maxBytes: z.number().int().min(1024).optional(),
|
|
568
|
+
requireSignature: z.boolean().optional(),
|
|
569
|
+
context: contextPolicySchema
|
|
570
|
+
.refine((policy) => policy.trigger === undefined, "messages.context cannot set trigger; maxMessages and maxBytes are the history's compaction triggers.")
|
|
571
|
+
.optional(),
|
|
572
|
+
markers: z
|
|
573
|
+
.strictObject({
|
|
574
|
+
summary: z.string().min(1).optional(),
|
|
575
|
+
note: z.string().min(1).optional(),
|
|
576
|
+
})
|
|
577
|
+
.optional(),
|
|
578
|
+
metadata: jsonObjectSchema.optional(),
|
|
579
|
+
});
|
|
580
|
+
const isReservedMessagesName = (value) => value != null &&
|
|
581
|
+
(value.split(".")[0] ?? "").trim().toLowerCase() === MESSAGES_VARIABLE;
|
|
582
|
+
/**
|
|
583
|
+
* `history` requires a `messages` declaration, `history.insert` applies only
|
|
584
|
+
* to prompt steps, and a declaring manifest reserves the `messages` name.
|
|
585
|
+
*/
|
|
586
|
+
const validateStepHistory = (steps, declared, context, path) => {
|
|
587
|
+
steps.forEach((step, index) => {
|
|
588
|
+
const stepPath = [...path, index];
|
|
589
|
+
if (step.history !== undefined && !declared) {
|
|
590
|
+
context.addIssue({
|
|
591
|
+
code: "custom",
|
|
592
|
+
message: `Step ${step.id} sets history, but the manifest does not declare messages.`,
|
|
593
|
+
path: [...stepPath, "history"],
|
|
594
|
+
});
|
|
595
|
+
}
|
|
596
|
+
if (step.history !== undefined &&
|
|
597
|
+
step.history !== "all" &&
|
|
598
|
+
step.history.insert !== undefined &&
|
|
599
|
+
step.type !== "prompt") {
|
|
600
|
+
context.addIssue({
|
|
601
|
+
code: "custom",
|
|
602
|
+
message: `history.insert applies only to prompt steps; ${step.type} step ${step.id} cannot set it.`,
|
|
603
|
+
path: [...stepPath, "history", "insert"],
|
|
604
|
+
});
|
|
605
|
+
}
|
|
606
|
+
if (declared && isReservedMessagesName(step.outputVariable)) {
|
|
607
|
+
context.addIssue({
|
|
608
|
+
code: "custom",
|
|
609
|
+
message: `Step ${step.id} cannot write the reserved messages variable.`,
|
|
610
|
+
path: [...stepPath, "outputVariable"],
|
|
611
|
+
});
|
|
612
|
+
}
|
|
613
|
+
if (step.type === "loop") {
|
|
614
|
+
if (declared) {
|
|
615
|
+
for (const field of [
|
|
616
|
+
"itemVariable",
|
|
617
|
+
"indexVariable",
|
|
618
|
+
"resultVariable",
|
|
619
|
+
]) {
|
|
620
|
+
if (isReservedMessagesName(step.loop[field])) {
|
|
621
|
+
context.addIssue({
|
|
622
|
+
code: "custom",
|
|
623
|
+
message: `Loop step ${step.id} cannot write the reserved messages variable.`,
|
|
624
|
+
path: [...stepPath, "loop", field],
|
|
625
|
+
});
|
|
626
|
+
}
|
|
627
|
+
}
|
|
628
|
+
}
|
|
629
|
+
validateStepHistory(step.steps, declared, context, [
|
|
630
|
+
...stepPath,
|
|
631
|
+
"steps",
|
|
632
|
+
]);
|
|
633
|
+
}
|
|
634
|
+
});
|
|
635
|
+
};
|
|
531
636
|
const MAXIMUM_AGENT_MANIFEST_STRUCTURE_DEPTH = 128;
|
|
532
637
|
const MAXIMUM_AGENT_MANIFEST_STRUCTURE_NODES = 100_000;
|
|
533
638
|
const agentManifestStructureIssue = (input) => {
|
|
@@ -580,6 +685,7 @@ const agentManifestObjectSchema = z
|
|
|
580
685
|
variables: z.array(agentVariableDefinitionSchema).optional(),
|
|
581
686
|
connections: z.array(agentConnectionBindingSchema).optional(),
|
|
582
687
|
workspaces: z.array(agentWorkspaceBindingSchema).optional(),
|
|
688
|
+
messages: agentMessagesDeclarationSchema.optional(),
|
|
583
689
|
steps: z.array(agentStepSchema),
|
|
584
690
|
policies: executionPoliciesSchema.optional(),
|
|
585
691
|
limits: z
|
|
@@ -634,9 +740,19 @@ const agentManifestObjectSchema = z
|
|
|
634
740
|
validateCodeStepWorkspaces(manifest.steps, workspaceAliases, context, [
|
|
635
741
|
"steps",
|
|
636
742
|
]);
|
|
743
|
+
validateStepHistory(manifest.steps, manifest.messages != null, context, [
|
|
744
|
+
"steps",
|
|
745
|
+
]);
|
|
637
746
|
const variableKeys = new Set();
|
|
638
747
|
manifest.variables?.forEach((definition, index) => {
|
|
639
748
|
const normalizedKey = definition.key.toLowerCase();
|
|
749
|
+
if (manifest.messages != null && normalizedKey === MESSAGES_VARIABLE) {
|
|
750
|
+
context.addIssue({
|
|
751
|
+
code: "custom",
|
|
752
|
+
message: "The messages variable is reserved in a manifest that declares messages.",
|
|
753
|
+
path: ["variables", index, "key"],
|
|
754
|
+
});
|
|
755
|
+
}
|
|
640
756
|
if (variableKeys.has(normalizedKey)) {
|
|
641
757
|
context.addIssue({
|
|
642
758
|
code: "custom",
|
|
@@ -675,6 +791,8 @@ export const agentRunManifestSchema = z.strictObject({
|
|
|
675
791
|
variables: agentVariableOverridesSchema.optional(),
|
|
676
792
|
execution: agentRunExecutionSchema.optional(),
|
|
677
793
|
budget: agentRunBudgetSchema.optional(),
|
|
794
|
+
messages: z.lazy(() => conversationMessagesInputSchema).optional(),
|
|
795
|
+
messagesSignature: z.lazy(() => messagesSignatureSchema).optional(),
|
|
678
796
|
});
|
|
679
797
|
export const artifactRefSchema = z.strictObject({
|
|
680
798
|
id: z.string().min(1),
|
|
@@ -768,6 +886,142 @@ const contentPartSchema = z.discriminatedUnion("type", [
|
|
|
768
886
|
...contentPartMetadataShape,
|
|
769
887
|
}),
|
|
770
888
|
]);
|
|
889
|
+
const conversationContentPartSchema = z.discriminatedUnion("type", [
|
|
890
|
+
z.strictObject({
|
|
891
|
+
type: z.literal("text"),
|
|
892
|
+
text: z.string(),
|
|
893
|
+
metadata: jsonObjectSchema.optional(),
|
|
894
|
+
}),
|
|
895
|
+
z.strictObject({
|
|
896
|
+
type: z.literal("json"),
|
|
897
|
+
value: jsonValueSchema,
|
|
898
|
+
metadata: jsonObjectSchema.optional(),
|
|
899
|
+
}),
|
|
900
|
+
z.strictObject({
|
|
901
|
+
type: z.literal("image"),
|
|
902
|
+
...mediaSourceSchema,
|
|
903
|
+
mediaType: z.string().min(1).optional(),
|
|
904
|
+
metadata: jsonObjectSchema.optional(),
|
|
905
|
+
}),
|
|
906
|
+
z.strictObject({
|
|
907
|
+
type: z.literal("file"),
|
|
908
|
+
...mediaSourceSchema,
|
|
909
|
+
name: z.string().min(1).optional(),
|
|
910
|
+
mediaType: z.string().min(1).optional(),
|
|
911
|
+
metadata: jsonObjectSchema.optional(),
|
|
912
|
+
}),
|
|
913
|
+
]);
|
|
914
|
+
const messagesMarkerSchema = z.strictObject({
|
|
915
|
+
kind: z.enum(["summary", "note"]),
|
|
916
|
+
replaces: z.number().int().nonnegative().safe(),
|
|
917
|
+
turns: z.number().int().nonnegative().safe().optional(),
|
|
918
|
+
sequence: z.number().int().positive().safe(),
|
|
919
|
+
strategy: z.enum(["summarize", "prune"]),
|
|
920
|
+
});
|
|
921
|
+
/** Reads a leading marker from message metadata; undefined when there is none. */
|
|
922
|
+
export const messagesMarkerOf = (message) => {
|
|
923
|
+
const runtime = message?.metadata?.agentRuntime;
|
|
924
|
+
if (runtime == null || typeof runtime !== "object" || Array.isArray(runtime))
|
|
925
|
+
return undefined;
|
|
926
|
+
const parsed = messagesMarkerSchema.safeParse(runtime.messages);
|
|
927
|
+
return parsed.success ? parsed.data : undefined;
|
|
928
|
+
};
|
|
929
|
+
const conversationMessageObjectSchema = z.strictObject({
|
|
930
|
+
role: z.enum(["user", "assistant"]),
|
|
931
|
+
content: z.array(conversationContentPartSchema).min(1),
|
|
932
|
+
metadata: jsonObjectSchema.optional(),
|
|
933
|
+
});
|
|
934
|
+
const normalizeConversationMessage = (input) => input != null &&
|
|
935
|
+
typeof input === "object" &&
|
|
936
|
+
!Array.isArray(input) &&
|
|
937
|
+
typeof input.content === "string"
|
|
938
|
+
? {
|
|
939
|
+
...input,
|
|
940
|
+
content: [
|
|
941
|
+
{ type: "text", text: input.content },
|
|
942
|
+
],
|
|
943
|
+
}
|
|
944
|
+
: input;
|
|
945
|
+
const validateConversationMessages = (messages, context) => {
|
|
946
|
+
messages.forEach((message, messageIndex) => {
|
|
947
|
+
const runtime = message.metadata?.agentRuntime;
|
|
948
|
+
if (runtime !== undefined) {
|
|
949
|
+
if (messageIndex > 0) {
|
|
950
|
+
context.addIssue({
|
|
951
|
+
code: "custom",
|
|
952
|
+
path: [messageIndex, "metadata", "agentRuntime"],
|
|
953
|
+
message: "metadata.agentRuntime is reserved for the runtime marker, which may only be the first message.",
|
|
954
|
+
});
|
|
955
|
+
}
|
|
956
|
+
else if (runtime == null ||
|
|
957
|
+
typeof runtime !== "object" ||
|
|
958
|
+
Array.isArray(runtime) ||
|
|
959
|
+
Object.keys(runtime).some((key) => key !== "messages") ||
|
|
960
|
+
!messagesMarkerSchema.safeParse(runtime.messages).success) {
|
|
961
|
+
context.addIssue({
|
|
962
|
+
code: "custom",
|
|
963
|
+
path: [messageIndex, "metadata", "agentRuntime"],
|
|
964
|
+
message: "metadata.agentRuntime is not a valid runtime marker.",
|
|
965
|
+
});
|
|
966
|
+
}
|
|
967
|
+
}
|
|
968
|
+
message.content.forEach((part, partIndex) => {
|
|
969
|
+
if (part.type !== "image" && part.type !== "file")
|
|
970
|
+
return;
|
|
971
|
+
const sourceCount = [part.artifact, part.url, part.data].filter((value) => value !== undefined).length;
|
|
972
|
+
if (sourceCount !== 1) {
|
|
973
|
+
context.addIssue({
|
|
974
|
+
code: "custom",
|
|
975
|
+
path: [messageIndex, "content", partIndex],
|
|
976
|
+
message: "Message media parts require exactly one of artifact, url, or data.",
|
|
977
|
+
});
|
|
978
|
+
}
|
|
979
|
+
if (part.type === "file" &&
|
|
980
|
+
part.artifact == null &&
|
|
981
|
+
part.mediaType == null) {
|
|
982
|
+
context.addIssue({
|
|
983
|
+
code: "custom",
|
|
984
|
+
path: [messageIndex, "content", partIndex, "mediaType"],
|
|
985
|
+
message: "Message file parts without an artifact reference require mediaType.",
|
|
986
|
+
});
|
|
987
|
+
}
|
|
988
|
+
if (part.data !== undefined && part.mediaType == null) {
|
|
989
|
+
context.addIssue({
|
|
990
|
+
code: "custom",
|
|
991
|
+
path: [messageIndex, "content", partIndex, "mediaType"],
|
|
992
|
+
message: "Message media parts with inline data require mediaType.",
|
|
993
|
+
});
|
|
994
|
+
}
|
|
995
|
+
});
|
|
996
|
+
});
|
|
997
|
+
};
|
|
998
|
+
/** A normalized conversation history: full content-part form only. */
|
|
999
|
+
export const conversationMessagesSchema = z
|
|
1000
|
+
.array(conversationMessageObjectSchema)
|
|
1001
|
+
.superRefine(validateConversationMessages);
|
|
1002
|
+
/** A supplied conversation history; string content is normalized to one text part. */
|
|
1003
|
+
export const conversationMessagesInputSchema = z
|
|
1004
|
+
.array(z.preprocess(normalizeConversationMessage, conversationMessageObjectSchema))
|
|
1005
|
+
.superRefine(validateConversationMessages);
|
|
1006
|
+
export const messagesSignatureSchema = z.strictObject({
|
|
1007
|
+
version: z.literal(MESSAGES_SIGNATURE_VERSION),
|
|
1008
|
+
algorithm: z.literal(MESSAGES_SIGNATURE_ALGORITHM),
|
|
1009
|
+
keyId: z.string().min(1).max(200),
|
|
1010
|
+
count: z.number().int().nonnegative().safe(),
|
|
1011
|
+
value: z
|
|
1012
|
+
.string()
|
|
1013
|
+
.min(1)
|
|
1014
|
+
.regex(/^[A-Za-z0-9_-]+$/u, "Signature value must be base64url."),
|
|
1015
|
+
});
|
|
1016
|
+
export const runMessagesStateSchema = z.strictObject({
|
|
1017
|
+
history: conversationMessagesSchema,
|
|
1018
|
+
compaction: z.enum(["pending", "complete"]),
|
|
1019
|
+
signedCount: z.number().int().nonnegative().safe(),
|
|
1020
|
+
scope: z.string().optional(),
|
|
1021
|
+
journal: jsonObjectSchema.optional(),
|
|
1022
|
+
});
|
|
1023
|
+
/** Parse and normalize a supplied conversation history. */
|
|
1024
|
+
export const parseConversationMessages = (input) => conversationMessagesInputSchema.parse(input);
|
|
771
1025
|
export const transcriptItemSchema = z.strictObject({
|
|
772
1026
|
id: z.string().min(1),
|
|
773
1027
|
type: z.enum([
|
|
@@ -822,6 +1076,7 @@ export const runCheckpointSchema = z.strictObject({
|
|
|
822
1076
|
activeStepArtifacts: z.array(artifactRefSchema).optional(),
|
|
823
1077
|
continuation: jsonObjectSchema.optional(),
|
|
824
1078
|
budget: runBudgetStateSchema.optional(),
|
|
1079
|
+
messages: z.lazy(() => runMessagesStateSchema).optional(),
|
|
825
1080
|
createdAt: z.iso.datetime({ offset: true }),
|
|
826
1081
|
metadata: jsonObjectSchema.optional(),
|
|
827
1082
|
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@clearideas/agent-runtime-contracts",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.8.0",
|
|
4
4
|
"description": "Versioned, persistence-independent contracts for Clear Ideas Agent Runtime.",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"author": "Clear Ideas Incorporated",
|
|
@@ -43,7 +43,7 @@
|
|
|
43
43
|
"vitest": "^5.1.0"
|
|
44
44
|
},
|
|
45
45
|
"engines": {
|
|
46
|
-
"node": ">=
|
|
46
|
+
"node": ">=22.20.0"
|
|
47
47
|
},
|
|
48
48
|
"homepage": "https://github.com/clearideas/agent-runtime#readme",
|
|
49
49
|
"bugs": {
|