Skip to content
Merged
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
19 changes: 19 additions & 0 deletions changes/feat-scheduled-actions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
### Added

- **Cancel, edit and send now a scheduled message: `messages scheduled cancel <chat> <scheduled-id...>`,
`messages scheduled edit <chat> <scheduled-id> [text] [--at-time <time>]` and
`messages scheduled send <chat> <scheduled-id...>`.** Before, a scheduled message could only be listed, and the
owner cancelled one in the app. A scheduled message has an id of its own: `messages edit` on it failed, and
`messages delete` with the same number would have deleted an old, sent message. Each command takes only the ids
`messages scheduled <chat>` lists and checks them against that list before anything changes; an unknown id is
`not_found` and points at the list. Cancelling counts and asks as a delete does (`--allow-dangerous` at an `ask`
level), sending now as a send. An edit with only `--at-time` keeps the text, and one with only the text keeps the
time. The keys are `messages.scheduled.cancel`, `.edit` and `.send`; the answers are the entities
`scheduled_cancellation`, `message_write` and `scheduled_send`. An adapter adds them through the optional port
group `ScheduledChanges` (`cancelScheduled`, `editScheduled`, `sendScheduled`); a messenger without it answers
"this messenger cannot …". Needs cli-base with `scheduled` as a resource word, for `cli-dev lint`.

### Fixed

- **The MCP command search ranks the shorter name first on a tie**, so "send message" finds `messages send` before
`messages scheduled send`.
5 changes: 4 additions & 1 deletion docs/dev/STANDARD.md
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,7 @@ table. "Not" lists the words a term replaces. How `cli-dev lint` runs over a CLI
| `privacy` | singular | who may find, see, call or add the account | |
| `metadata` | singular | a profile's cached source attributes | |
| `auth` | singular | the credentials a bot profile uses: its token and the secrets beside it | |
| `scheduled` | singular | a chat's messages the messenger will send later; their ids are their own, never the ids of sent messages | |
| `schedule` | singular | a profile's timers the owner saved, each running a command of this tool or an agent; the tool's own background work is jobs | |

<!-- resources: end -->
Expand Down Expand Up @@ -499,9 +500,11 @@ table. "Not" lists the words a term replaces. How `cli-dev lint` runs over a CLI
| `--allow-delete` | | switch | permit a call that deletes for good | |
| `--allow-remote` | | switch | consent to sending the stored data to the configured remote model | |
| `--answerer` | `<person>` | string | a person whose reply counts as an answer; repeat it for more | |
| `--browser` | `<program>` | string | the browser program to open for sign-in | |
| `--browser` | `<program>` | string | the browser to open for sign-in | |
| `--credential-storage` | `<storage>` | choice | where a sign-in is kept: keyring, which falls back to a private file by itself, or file (WC-SECR-4) | |
| `--checkpoint` | `<file>` | path | keep resume progress in this file | |
| `--site` | `<address>` | string | the owner's site a sign-in is for | |
| `--login` | `<name>` | string | the user name a password is for | |
| `--expected-event-id` | `<id>` | id | refuse unless the linked event is still this one, or none | |
| `--expected-person` | `<uid>` | id | refuse unless the linked person is still this one, or none | |
| `--html` | | switch | the text is HTML | |
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@
"@types/pngjs": "6.0.5",
"@types/ssh2": "1.15.5",
"@vitest/coverage-v8": "^5.0.5",
"@wirecat/cli-base": "0.120.0",
"@wirecat/cli-base": "0.122.0",
"@wirecat/cli-core-legacy-tags": "npm:@wirecat/cli-core@0.414.0",
"ajv": "8.17.1",
"commander": "15.0.0",
Expand Down
16 changes: 8 additions & 8 deletions pnpm-lock.yaml

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

1 change: 1 addition & 0 deletions src/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ export type {
PushedHistory,
ReadState,
RemoteFile,
ScheduledChanges,
ScheduledMessages,
SenderIdentities,
SenderSearch,
Expand Down
3 changes: 3 additions & 0 deletions src/cli/messenger/lacks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ const argvOf = (root: string): Record<string, string[]> => {
"messages comments": [chat, message],
"messages pin": [chat, message],
"messages unpin": [chat, message],
"messages scheduled cancel": [chat, message],
"messages scheduled edit": [chat, message, "new text"],
"messages scheduled send": [chat, message],
}
}

Expand Down
3 changes: 3 additions & 0 deletions src/cli/messenger/lacks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ export const COMMAND_NEEDS: Readonly<Record<string, Need>> = {
"messages comments": ["comments", "read comments on a channel post"],
"messages pin": ["pin", "pin a message"],
"messages unpin": ["unpin", "unpin a message"],
"messages scheduled cancel": ["cancelScheduled", "cancel a scheduled message"],
"messages scheduled edit": ["editScheduled", "edit a scheduled message"],
"messages scheduled send": ["sendScheduled", "send a scheduled message now"],
}

/** The commands the store answers for a messenger whose history is pushed to it, so the method is not needed there. */
Expand Down
11 changes: 9 additions & 2 deletions src/cli/messenger/messages-delete-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import { type Messenger, messengerContext } from "./context.js"
* question, and `--allow-dangerous` is the yes. For everyone only
* with `--for-everyone` (max-cli `NEED-239`), and never for an agent (NEED-1423).
*/
export const deleteCommand = (messenger: Messenger): Command =>
annotate(new Command("delete"), {
export const deleteCommand = (messenger: Messenger): Command => {
const command = annotate(new Command("delete"), {
examples: [`${messenger.app.command} personal messages delete "Book club" 101`],
effects: REMOTE_WRITE,
output: { entity: "message_deletion" },
Expand Down Expand Up @@ -49,3 +49,10 @@ export const deleteCommand = (messenger: Messenger): Command =>
)
renderAnswer(this, context, itemOf(snakeKeys(deleted)))
})
if (messenger.scheduledSends !== false)
command.addHelpText(
"after",
"\nA message waiting to be sent has an id of its own; cancel it with `messages scheduled cancel`.",
)
return command
}
5 changes: 5 additions & 0 deletions src/cli/messenger/messages-edit-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,10 @@ export const editCommand = (messenger: Messenger): Command => {
await afterWrite(edited, () => renderAnswer(this, context, writtenAnswer(edited, reader)))
})
if (messenger.html) edit.option("--html", HTML_HELP)
if (messenger.scheduledSends !== false)
edit.addHelpText(
"after",
"\nA message waiting to be sent has an id of its own; change it with `messages scheduled edit`.",
)
return edit
}
134 changes: 130 additions & 4 deletions src/cli/messenger/messages-scheduled-command.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,47 @@
import { CliError } from "@wirecat/cli-base"
import { annotate } from "@wirecat/cli-base/commands"
import { Command } from "commander"
import { sendTime } from "../../domain/send-time.js"
import { capability } from "../../ports/messenger.js"
import { renderMessages } from "../../render/messages.js"
import { REMOTE_READ } from "../effects.js"
import { listOf, renderAnswer } from "../envelope.js"
import { DELETE_AT_ONCE } from "../../services/index.js"
import { ownerOnlyOption, REMOTE_READ, REMOTE_WRITE } from "../effects.js"
import { itemOf, listOf, renderAnswer, snakeKeys } from "../envelope.js"
import { type Messenger, messengerContext } from "./context.js"
import { messageItems, selfOf, viewOf } from "./message-items.js"
import { messageItem, messageItems, selfOf, viewOf } from "./message-items.js"
import { afterWrite, writtenAnswer } from "./message-writes.js"
import { HTML_HELP } from "./messages-send-command.js"
import { readAll } from "./stdin.js"

const SCHEDULED_IDS =
"the scheduled ids `messages scheduled <chat>` lists, by spaces or commas, " +
`at most ${DELETE_AT_ONCE}; never the id of a sent message`

const SAME_NUMBER =
"A scheduled message has an id of its own: the same number in the chat's history is another, sent message. " +
"Each id is checked against the scheduled list before anything changes."

const idsOf = (typed: string[]): string[] => {
const ids = typed
.flatMap((one) => one.split(","))
.map((one) => one.trim())
.filter(Boolean)
if (ids.length === 0)
throw new CliError("validation_error", "no scheduled id given — `messages scheduled <chat>` lists them", {
sent: false,
})
return ids
}

/** Where to look after a scheduled send ended without an answer: repeating it would schedule a second one. */
export const scheduledCommand = (messenger: Messenger): Command => {
const command = annotate(new Command("scheduled"), {
examples: [`${messenger.app.command} personal messages scheduled "Book club"`],
effects: REMOTE_READ,
})
.description("messages waiting to be sent later in a chat, soonest first; cancel one in the app")
.description("messages waiting to be sent later in a chat, soonest first, with their scheduled ids")
.argument("<chat>", messenger.chatArgument)
.addHelpText("after", `\n${SAME_NUMBER}`)
.action(async function (this: Command, chat: string) {
const context = messengerContext(this, messenger)
const { items, reader } = await context.withMessenger(async (connection) => ({
Expand All @@ -37,5 +64,104 @@ export const scheduledCommand = (messenger: Messenger): Command => {
),
)
})
command.addCommand(cancelCommand(messenger))
command.addCommand(editCommand(messenger))
command.addCommand(sendNowCommand(messenger))
return annotate(command, { output: { entity: "message" } })
}

/** Nobody gets a cancelled message back, so it asks as a deletion does where the profile's level is `ask`. */
const cancelCommand = (messenger: Messenger): Command =>
annotate(new Command("cancel"), {
examples: [`${messenger.app.command} personal messages scheduled cancel "Book club" 3`],
effects: REMOTE_WRITE,
output: { entity: "scheduled_cancellation" },
})
.description("cancel scheduled messages before they are sent; they cannot be got back")
.argument("<chat>", messenger.chatArgument)
.argument("<scheduled-id...>", SCHEDULED_IDS)
.addOption(
ownerOnlyOption("--allow-dangerous", "go ahead without the question an ask level puts before a cancellation"),
)
.addHelpText("after", `\n${SAME_NUMBER}`)
.action(async function (this: Command, chat: string, typed: string[]) {
const ids = idsOf(typed)
const context = messengerContext(this, messenger)
const cancelled = await context.withServices((services) => services.messages.cancelScheduled({ chat, ids }))
renderAnswer(this, context, itemOf(snakeKeys(cancelled)))
})

const editCommand = (messenger: Messenger): Command => {
const edit = annotate(new Command("edit"), {
literal_after_dashes: true,
examples: [
`${messenger.app.command} personal messages scheduled edit "Book club" 3 "The meeting starts at 11."`,
`${messenger.app.command} personal messages scheduled edit "Book club" 3 --at-time 2026-12-01T09:00`,
],
effects: REMOTE_WRITE,
output: { entity: "message_write" },
})
.description("change the text or the time of a scheduled message before it is sent")
.argument("<chat>", messenger.chatArgument)
.argument("<scheduled-id>", "the scheduled id `messages scheduled <chat>` lists; never the id of a sent message")
.argument("[text]", "the new text; without it and without --at-time, read from stdin")
.option("--at-time <time>", "send it at this time instead: 2026-09-25T09:00 (local time), or 30m, 2h, 1d from now")
.option("--md", "read this messenger's Markdown; see its formatting guide for supported syntax")
.addHelpText("after", `\n${SAME_NUMBER}`)
.action(async function (this: Command, chat: string, id: string, text: string | undefined) {
const context = messengerContext(this, messenger)
const { md: markdown, html, atTime } = this.opts<{ md?: boolean; html?: boolean; atTime?: string }>()
const at = atTime === undefined ? undefined : sendTime(atTime)
const body = text ?? (at === undefined ? await readAll(context.stdin) : undefined)
if (body !== undefined && body.trim() === "")
throw new CliError(
"validation_error",
"no new text — give it, pipe it in, or change only the time with --at-time",
)
const { edited, reader } = await context.withServices(async (services) => {
const edited = await services.messages.editScheduled({
chat,
id: id.trim(),
...(body === undefined ? {} : { text: body }),
...(markdown === true ? { markdown } : {}),
...(html === true ? { html } : {}),
...(at === undefined ? {} : { at }),
})
return afterWrite(edited, async () => ({ edited, reader: await services.messages.reader() }))
})
await afterWrite(edited, () => renderAnswer(this, context, writtenAnswer(edited, reader)))
})
if (messenger.html) edit.option("--html", HTML_HELP)
return edit
}

const sendNowCommand = (messenger: Messenger): Command =>
annotate(new Command("send"), {
examples: [`${messenger.app.command} personal messages scheduled send "Book club" 3`],
effects: REMOTE_WRITE,
output: { entity: "scheduled_send" },
})
.description("deliver what is scheduled right away instead of at its time; each gets a new id")
.argument("<chat>", messenger.chatArgument)
.argument("<scheduled-id...>", SCHEDULED_IDS)
.addHelpText("after", `\n${SAME_NUMBER}`)
.action(async function (this: Command, chat: string, typed: string[]) {
const ids = idsOf(typed)
const context = messengerContext(this, messenger)
const { done, reader } = await context.withServices(async (services) => {
const done = await services.messages.sendScheduled({ chat, ids })
return afterWrite(done, async () => ({ done, reader: await services.messages.reader() }))
})
await afterWrite(done, () =>
renderAnswer(
this,
context,
itemOf({
operation_id: done.operationId,
chat_id: done.chatId,
sent: done.sent,
messages: done.messages.map((one) => messageItem(one, reader)),
}),
),
)
})
Loading
Loading