Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 13 additions & 10 deletions pi-ask/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions pi-ask/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,13 @@
"pack:check": "npm pack --dry-run",
"prepublishOnly": "npm test && npm run typecheck"
},
"dependencies": {
"@earendil-works/pi-schema": "1.5.0"
},
"peerDependencies": {
"@earendil-works/pi-ai": "*",
"@earendil-works/pi-coding-agent": "*",
"@earendil-works/pi-tui": "*",
"typebox": "*"
"@earendil-works/pi-tui": "*"
},
"overrides": {
"undici": "8.12.2"
Expand All @@ -57,7 +59,6 @@
"@earendil-works/pi-coding-agent": "^1.4.0",
"@earendil-works/pi-tui": "^1.4.0",
"@types/node": "26.6.5",
"typebox": "^1.4.3",
"typescript": "7.0.2"
},
"pi": {
Expand Down
8 changes: 5 additions & 3 deletions pi-ask/src/answer-extraction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@ import {
type UserMessage,
} from "@earendil-works/pi-ai";
import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
import { Value } from "typebox/value";
import { compile } from "@earendil-works/pi-schema";
import type { AskConfig } from "./config/schema.ts";
import { AnswerExtractionParamsSchema } from "./schema.ts";
import { collectValidationIssues } from "./state/normalize.ts";
import type { AskParams } from "./types.ts";

const ANSWER_EXTRACTION_TOOL_NAME = "ask_user";

const validateAnswerExtractionParams = compile(AnswerExtractionParamsSchema);

const ANSWER_EXTRACTION_TOOL = {
name: ANSWER_EXTRACTION_TOOL_NAME,
description:
Expand Down Expand Up @@ -223,11 +225,11 @@ export function parseExtractionCandidate(
}

function parseExtractionValue(value: unknown): ParsedExtraction {
const [shapeIssue] = Value.Errors(AnswerExtractionParamsSchema, value);
const [shapeIssue] = validateAnswerExtractionParams.errors(value);
if (shapeIssue) {
return {
ok: false,
error: `${shapeIssue.instancePath || "root"} ${shapeIssue.message}`,
error: `${shapeIssue.path || "root"} ${shapeIssue.message}`,
};
}
const params = value as AskParams;
Expand Down
213 changes: 127 additions & 86 deletions pi-ask/src/config/schema.ts
Original file line number Diff line number Diff line change
@@ -1,94 +1,135 @@
import { type Static, Type } from "typebox";
import { Compile } from "typebox/compile";
import {
arr,
bool,
compile,
lit,
num,
obj,
opt,
str,
union,
type Static,
} from "@earendil-works/pi-schema";

const AskAnswerModelPreferenceSchema = Type.Object({
id: Type.String(),
provider: Type.String(),
});
const AskAnswerModelPreferenceSchema = obj(
{
id: str(),
provider: str(),
},
{ open: true }
);

const AskNotificationChannelSchema = Type.Union([
Type.Literal("bell"),
Type.Literal("osc9"),
Type.Literal("osc777"),
Type.Object({
command: Type.String(),
type: Type.Literal("command"),
}),
const AskNotificationChannelSchema = union([
lit("bell"),
lit("osc9"),
lit("osc777"),
obj(
{
command: str(),
type: lit("command"),
},
{ open: true }
),
]);

const AskKeyBindingSchema = Type.Union([
Type.String(),
Type.Array(Type.String()),
]);
const AskKeyBindingSchema = union([str(), arr(str())]);

const AskConfigKeymapsSchema = Type.Object({
global: Type.Object({
dismiss: Type.Optional(AskKeyBindingSchema),
settings: Type.Optional(AskKeyBindingSchema),
}),
main: Type.Object({
cancel: Type.Optional(AskKeyBindingSchema),
changeQuestionType: Type.Optional(AskKeyBindingSchema),
confirm: Type.Optional(AskKeyBindingSchema),
nextOption: Type.Optional(AskKeyBindingSchema),
nextTab: Type.Optional(AskKeyBindingSchema),
optionNote: Type.Optional(AskKeyBindingSchema),
previousOption: Type.Optional(AskKeyBindingSchema),
previousTab: Type.Optional(AskKeyBindingSchema),
questionNote: Type.Optional(AskKeyBindingSchema),
toggle: Type.Optional(AskKeyBindingSchema),
}),
editor: Type.Object({
close: Type.Optional(AskKeyBindingSchema),
nextOptionWhenEmpty: Type.Optional(AskKeyBindingSchema),
nextTabWhenEmpty: Type.Optional(AskKeyBindingSchema),
previousOptionWhenEmpty: Type.Optional(AskKeyBindingSchema),
previousTabWhenEmpty: Type.Optional(AskKeyBindingSchema),
submit: Type.Optional(AskKeyBindingSchema),
}),
noteEditor: Type.Object({
close: Type.Optional(AskKeyBindingSchema),
nextOptionWhenEmpty: Type.Optional(AskKeyBindingSchema),
nextTabWhenEmpty: Type.Optional(AskKeyBindingSchema),
previousOptionWhenEmpty: Type.Optional(AskKeyBindingSchema),
previousTabWhenEmpty: Type.Optional(AskKeyBindingSchema),
save: Type.Optional(AskKeyBindingSchema),
}),
settingsModal: Type.Object({
close: Type.Optional(AskKeyBindingSchema),
nextOption: Type.Optional(AskKeyBindingSchema),
previousOption: Type.Optional(AskKeyBindingSchema),
toggle: Type.Optional(AskKeyBindingSchema),
}),
});
const AskConfigKeymapsSchema = obj(
{
global: obj(
{
dismiss: opt(AskKeyBindingSchema),
settings: opt(AskKeyBindingSchema),
},
{ open: true }
),
main: obj(
{
cancel: opt(AskKeyBindingSchema),
changeQuestionType: opt(AskKeyBindingSchema),
confirm: opt(AskKeyBindingSchema),
nextOption: opt(AskKeyBindingSchema),
nextTab: opt(AskKeyBindingSchema),
optionNote: opt(AskKeyBindingSchema),
previousOption: opt(AskKeyBindingSchema),
previousTab: opt(AskKeyBindingSchema),
questionNote: opt(AskKeyBindingSchema),
toggle: opt(AskKeyBindingSchema),
},
{ open: true }
),
editor: obj(
{
close: opt(AskKeyBindingSchema),
nextOptionWhenEmpty: opt(AskKeyBindingSchema),
nextTabWhenEmpty: opt(AskKeyBindingSchema),
previousOptionWhenEmpty: opt(AskKeyBindingSchema),
previousTabWhenEmpty: opt(AskKeyBindingSchema),
submit: opt(AskKeyBindingSchema),
},
{ open: true }
),
noteEditor: obj(
{
close: opt(AskKeyBindingSchema),
nextOptionWhenEmpty: opt(AskKeyBindingSchema),
nextTabWhenEmpty: opt(AskKeyBindingSchema),
previousOptionWhenEmpty: opt(AskKeyBindingSchema),
previousTabWhenEmpty: opt(AskKeyBindingSchema),
save: opt(AskKeyBindingSchema),
},
{ open: true }
),
settingsModal: obj(
{
close: opt(AskKeyBindingSchema),
nextOption: opt(AskKeyBindingSchema),
previousOption: opt(AskKeyBindingSchema),
toggle: opt(AskKeyBindingSchema),
},
{ open: true }
),
},
{ open: true }
);

export const AskConfigFileSchema = Type.Object({
schemaVersion: Type.Optional(Type.Number()),
answer: Type.Optional(
Type.Object({
extractionModels: Type.Optional(
Type.Array(AskAnswerModelPreferenceSchema)
),
extractionRetries: Type.Optional(Type.Number()),
extractionTimeoutMs: Type.Optional(Type.Number()),
})
),
behaviour: Type.Optional(
Type.Object({
autoSubmitWhenAnsweredWithoutNotes: Type.Optional(Type.Boolean()),
confirmDismissWhenDirty: Type.Optional(Type.Boolean()),
doublePressReviewShortcuts: Type.Optional(Type.Boolean()),
showFooterHints: Type.Optional(Type.Boolean()),
})
),
keymaps: Type.Optional(AskConfigKeymapsSchema),
notifications: Type.Optional(
Type.Object({
channels: Type.Optional(Type.Array(AskNotificationChannelSchema)),
enabled: Type.Optional(Type.Boolean()),
})
),
});
export const AskConfigFileSchema = obj(
{
schemaVersion: opt(num()),
answer: opt(
obj(
{
extractionModels: opt(arr(AskAnswerModelPreferenceSchema)),
extractionRetries: opt(num()),
extractionTimeoutMs: opt(num()),
},
{ open: true }
)
),
behaviour: opt(
obj(
{
autoSubmitWhenAnsweredWithoutNotes: opt(bool()),
confirmDismissWhenDirty: opt(bool()),
doublePressReviewShortcuts: opt(bool()),
showFooterHints: opt(bool()),
},
{ open: true }
)
),
keymaps: opt(AskConfigKeymapsSchema),
notifications: opt(
obj(
{
channels: opt(arr(AskNotificationChannelSchema)),
enabled: opt(bool()),
},
{ open: true }
)
),
},
{ open: true }
);

export type AskConfigFile = Static<typeof AskConfigFileSchema>;
export type AskAnswerModelPreference = Static<
Expand Down Expand Up @@ -156,4 +197,4 @@ export interface AskConfig {
};
}

export const validateAskConfigFile = Compile(AskConfigFileSchema);
export const validateAskConfigFile = compile(AskConfigFileSchema);
2 changes: 1 addition & 1 deletion pi-ask/src/config/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export class AskConfigStore {
}

const parsed = parseJson(content);
if (!parsed.ok || !validateAskConfigFile.Check(parsed.value)) {
if (!parsed.ok || !validateAskConfigFile.check(parsed.value)) {
return this.loadDefaultsWithNotice(INVALID_CONFIG_NOTICE);
}

Expand Down
Loading
Loading