@howaboua/pi-agent-comms 0.0.1-dev.13.1.gef1ce49691d6 → 0.0.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.
@@ -1,7 +1,17 @@
1
- import { StringEnum } from "@earendil-works/pi-ai";
2
1
  import type { AgentToolResult } from "@earendil-works/pi-coding-agent";
3
- import { Type } from "typebox";
4
- import { Check } from "typebox/value";
2
+ import {
3
+ bool,
4
+ compile,
5
+ int,
6
+ nul,
7
+ obj,
8
+ oneOf,
9
+ opt,
10
+ type Schema,
11
+ str,
12
+ union,
13
+ type Validator,
14
+ } from "@earendil-works/pi-schema";
5
15
  import type { LookupQuery } from "./bridge.js";
6
16
  import { noteResult } from "./note-result.js";
7
17
  import { internalQueryArguments, normalizeQueryInput } from "./query-input.js";
@@ -29,14 +39,14 @@ export const NOTES_ROUTING_GUIDELINE =
29
39
  export const NEW_CONTEXT_DESCRIPTION =
30
40
  "Start fresh context window. MUST save notes before calling";
31
41
  export const GPT_NEW_CONTEXT_DESCRIPTION = "Save notes→fresh context";
32
- const STRING = Type.Optional(Type.String());
33
- const INTEGER = Type.Optional(Type.Integer({ minimum: 1 }));
34
- const LINE = Type.Optional(Type.Union([Type.Integer(), Type.Null()]));
35
- export const NOTES_SCHEMA = Type.Object(
42
+ const STRING = opt(str());
43
+ const INTEGER = opt(int({ minimum: 1 }));
44
+ const LINE = opt(union([int(), nul()]));
45
+ export const NOTES_SCHEMA = obj(
36
46
  {
37
- action: Type.Optional(StringEnum(NOTES_ACTIONS)),
38
- path: Type.Optional(
39
- Type.Union([Type.String(), Type.Null()], {
47
+ action: opt({ ...oneOf(NOTES_ACTIONS), type: "string" }),
48
+ path: opt(
49
+ union([str(), nul()], {
40
50
  description:
41
51
  "File for read/write/append; prefix for list/search; virtual filesystem, not files in the project",
42
52
  }),
@@ -47,49 +57,52 @@ export const NOTES_SCHEMA = Type.Object(
47
57
  stop_line: LINE,
48
58
  limit: INTEGER,
49
59
  max_matches_per_file: INTEGER,
50
- file_order: Type.Optional(StringEnum(["ascending", "descending"])),
51
- file_order_by: Type.Optional(
52
- StringEnum(["name", "created_at", "updated_at"]),
53
- ),
54
- recent_file_first: Type.Optional(Type.Boolean()),
60
+ file_order: opt({ ...oneOf(["ascending", "descending"]), type: "string" }),
61
+ file_order_by: opt({
62
+ ...oneOf(["name", "created_at", "updated_at"]),
63
+ type: "string",
64
+ }),
65
+ recent_file_first: opt(bool()),
55
66
  },
56
- { additionalProperties: false },
67
+ { open: false },
57
68
  );
58
- const NEW_CONTEXT_SCHEMA = Type.Object(
69
+ const NEW_CONTEXT_SCHEMA = obj(
59
70
  {
60
- role: Type.Optional(
61
- Type.String({
71
+ role: opt(
72
+ str({
62
73
  minLength: 1,
63
74
  description:
64
75
  "Configured model-role ID, not an agent name or handoff text",
65
76
  }),
66
77
  ),
67
78
  },
68
- { additionalProperties: false },
79
+ { open: false },
69
80
  );
70
81
 
82
+ const newContextSchemaValidator = compile(NEW_CONTEXT_SCHEMA);
83
+
71
84
  export function newContextSchema(roleIds: string[]) {
72
- return Type.Object(
73
- roleIds.length ? { role: Type.Optional(StringEnum(roleIds)) } : {},
74
- { additionalProperties: false },
85
+ return obj(
86
+ roleIds.length ? { role: opt({ ...oneOf(roleIds), type: "string" }) } : {},
87
+ { open: false },
75
88
  );
76
89
  }
77
90
 
78
91
  export function prepareNewContextArguments(input: unknown) {
79
92
  const args = input ?? {};
80
- if (!Check(NEW_CONTEXT_SCHEMA, args))
93
+ if (!newContextSchemaValidator.check(args))
81
94
  throw new Error(
82
95
  'Use {} to keep the current model, or {role: "name"} for a configured role.',
83
96
  );
84
97
  return args;
85
98
  }
86
- export const HISTORY_SCHEMA = Type.Object(
99
+ export const HISTORY_SCHEMA = obj(
87
100
  {
88
- action: Type.Optional(StringEnum(HISTORY_ACTIONS)),
89
- agent_name: Type.Optional(Type.Union([Type.String(), Type.Null()])),
90
- item_id: Type.Optional(Type.String()),
91
- window_id: Type.Optional(
92
- Type.Union([Type.String(), Type.Null()], {
101
+ action: opt({ ...oneOf(HISTORY_ACTIONS), type: "string" }),
102
+ agent_name: opt(union([str(), nul()])),
103
+ item_id: opt(str()),
104
+ window_id: opt(
105
+ union([str(), nul()], {
93
106
  description: "Required with item_id for read",
94
107
  }),
95
108
  ),
@@ -97,18 +110,21 @@ export const HISTORY_SCHEMA = Type.Object(
97
110
  limit: INTEGER,
98
111
  limit_chars: INTEGER,
99
112
  max_chars_per_item: INTEGER,
100
- offset_chars: Type.Optional(Type.Integer({ minimum: 0 })),
101
- recent_first: Type.Optional(Type.Boolean()),
102
- role: Type.Optional(
103
- Type.Union([
104
- StringEnum(["user", "assistant", "tool", "system", "developer"]),
105
- Type.Null(),
113
+ offset_chars: opt(int({ minimum: 0 })),
114
+ recent_first: opt(bool()),
115
+ role: opt(
116
+ union([
117
+ {
118
+ ...oneOf(["user", "assistant", "tool", "system", "developer"]),
119
+ type: "string",
120
+ },
121
+ nul(),
106
122
  ]),
107
123
  ),
108
- tool_name: Type.Optional(Type.Union([Type.String(), Type.Null()])),
109
- tool_namespace: Type.Optional(Type.Union([Type.String(), Type.Null()])),
124
+ tool_name: opt(union([str(), nul()])),
125
+ tool_namespace: opt(union([str(), nul()])),
110
126
  },
111
- { additionalProperties: false },
127
+ { open: false },
112
128
  );
113
129
 
114
130
  const FIELDS: Record<string, readonly string[]> = {
@@ -272,25 +288,36 @@ export function queryHelp(
272
288
 
273
289
  export function prepareNotesArguments(args: unknown) {
274
290
  const params = normalizeQueryInput("notes", args);
275
- validateArguments("notes", params, NOTES_SCHEMA);
291
+ validateArguments("notes", params, NOTES_FIELD_VALIDATORS);
276
292
  return params;
277
293
  }
278
294
 
279
295
  export function prepareHistoryArguments(args: unknown) {
280
296
  const params = normalizeQueryInput("history", args);
281
- validateArguments("history", params, HISTORY_SCHEMA);
297
+ validateArguments("history", params, HISTORY_FIELD_VALIDATORS);
282
298
  return params;
283
299
  }
284
300
 
301
+ const NOTES_FIELD_VALIDATORS = fieldValidators(NOTES_SCHEMA);
302
+ const HISTORY_FIELD_VALIDATORS = fieldValidators(HISTORY_SCHEMA);
303
+
304
+ function fieldValidators(schema: Schema): Record<string, Validator> {
305
+ return Object.fromEntries(
306
+ Object.entries(schema["properties"] as Record<string, Schema>).map(
307
+ ([field, property]) => [field, compile(property)],
308
+ ),
309
+ );
310
+ }
311
+
285
312
  function validateArguments(
286
313
  namespace: LookupQuery["namespace"],
287
314
  params: Record<string, unknown>,
288
- schema: import("typebox").TObject,
315
+ validators: Record<string, Validator>,
289
316
  ) {
290
317
  if (!queryHelp(namespace, params)) queryArguments(namespace, params);
291
318
  for (const [field, value] of Object.entries(params)) {
292
- const property = schema.properties[field];
293
- if (!property || !Check(property, value))
319
+ const validator = validators[field];
320
+ if (!validator || !validator.check(value))
294
321
  throw new Error(
295
322
  `Invalid ${namespace} ${field}; use {} for exact arguments`,
296
323
  );
@@ -4,7 +4,7 @@ import type {
4
4
  ExtensionContext,
5
5
  ToolDefinition,
6
6
  } from "@earendil-works/pi-coding-agent";
7
- import { Type } from "typebox";
7
+ import { obj } from "@earendil-works/pi-schema";
8
8
  import type { LookupQuery, SharedNotesDetails } from "./bridge.js";
9
9
  import type { HistorySearch } from "./history-search.js";
10
10
  import type { NotesLifecycle } from "./lifecycle.js";
@@ -123,7 +123,7 @@ export function createTools(
123
123
  name: "get_context_remaining",
124
124
  label: "get_context_remaining",
125
125
  description: "Remaining context tokens",
126
- parameters: Type.Object({}, { additionalProperties: false }),
126
+ parameters: obj({}, { open: false }),
127
127
  async execute(_id, _input, _signal, _update, ctx) {
128
128
  assertActive();
129
129
  const remaining = lifecycle.windows.remaining(ctx);
@@ -4,45 +4,60 @@ import {
4
4
  buildSessionProjection,
5
5
  type SessionEntry,
6
6
  } from "@earendil-works/pi-coding-agent";
7
- import { Type } from "typebox";
8
- import { Check } from "typebox/value";
7
+ import {
8
+ arr,
9
+ compile,
10
+ lit,
11
+ num,
12
+ obj,
13
+ opt,
14
+ str,
15
+ union,
16
+ unknown,
17
+ unsafe,
18
+ } from "@earendil-works/pi-schema";
9
19
 
10
20
  const READ_KEY = "codexContextNoteRead";
11
- const READ_SCHEMA = Type.Object(
21
+ const READ_SCHEMA = obj(
12
22
  {
13
- protocol: Type.Literal(1),
14
- origin: Type.Literal("host"),
15
- id: Type.String({ pattern: "^[a-f0-9-]{36}$" }),
16
- path: Type.String({ minLength: 1 }),
17
- namespace: Type.Optional(Type.Literal("notes")),
18
- api: Type.String({ minLength: 1 }),
19
- provider: Type.String({ minLength: 1 }),
20
- model: Type.String({ minLength: 1 }),
21
- timestamp: Type.Number({ minimum: 0 }),
22
- content: Type.Array(
23
- Type.Union([
24
- Type.Object({ type: Type.Literal("text"), text: Type.String() }),
25
- Type.Object({
26
- type: Type.Literal("image"),
27
- data: Type.String(),
28
- mimeType: Type.String(),
29
- detail: Type.Optional(
30
- Type.Union([
31
- Type.Literal("auto"),
32
- Type.Literal("high"),
33
- Type.Literal("original"),
34
- ]),
35
- ),
36
- }),
23
+ protocol: lit(1),
24
+ origin: lit("host"),
25
+ id: str({ pattern: "^[a-f0-9-]{36}$" }),
26
+ path: str({ minLength: 1 }),
27
+ namespace: opt(lit("notes")),
28
+ api: str({ minLength: 1 }),
29
+ provider: str({ minLength: 1 }),
30
+ model: str({ minLength: 1 }),
31
+ timestamp: num({ minimum: 0 }),
32
+ content: arr(
33
+ union([
34
+ obj({ type: lit("text"), text: str() }, { open: true }),
35
+ obj(
36
+ {
37
+ type: lit("image"),
38
+ data: str(),
39
+ mimeType: str(),
40
+ detail: opt(union([lit("auto"), lit("high"), lit("original")])),
41
+ },
42
+ { open: true },
43
+ ),
37
44
  ]),
38
45
  ),
39
- details: Type.Object({
40
- codexHistoryNotes: Type.Record(Type.String(), Type.Unknown()),
41
- }),
46
+ details: obj(
47
+ {
48
+ codexHistoryNotes: unsafe<Record<string, unknown>>({
49
+ type: "object",
50
+ patternProperties: { "^.*$": unknown() },
51
+ }),
52
+ },
53
+ { open: true },
54
+ ),
42
55
  },
43
- { additionalProperties: false },
56
+ { open: false },
44
57
  );
45
58
 
59
+ const readSchemaValidator = compile(READ_SCHEMA);
60
+
46
61
  function callId(id: string): string {
47
62
  const name = "tree_handoff_" + id.replaceAll("-", "");
48
63
  return `${name}|fc_${name}`;
@@ -95,7 +110,7 @@ export function projectTreeHandoffReads(
95
110
  if (!details || typeof details !== "object" || !(READ_KEY in details))
96
111
  return [message];
97
112
  const read = details[READ_KEY];
98
- if (!Check(READ_SCHEMA, read))
113
+ if (!readSchemaValidator.check(read))
99
114
  throw new Error("Invalid persisted tree handoff read");
100
115
  const resultDetails = read.details;
101
116
  if (!isJson(resultDetails))