mirror of
https://ghproxy.cflmy.top/https://github.com/deepseek-ai/deepseek-harness.git
synced 2026-11-29 08:39:35 +08:00
im: accept an explicit reply referrer on ImSendOptions; it replaces the quote's or thread's referrer on the target
This commit is contained in:
1 parent
5b410d2546
commit
785652c0fc
5 files changed
+37
-6
No files matched your search
@@ -95,7 +95,7 @@ Each `ImBinding` grants access to one `chat`, with `writable` controlling publis
|
||||
|
||||
### Outbound publication, formatting, and history
|
||||
|
||||
All outbound messages go through `ctx.im.send` (single message) or `ctx.im.stream` (streaming updates). Every publication also re-verifies the target chat against the current `config.sources` rule named on the stored chat record (throwing `ImError('access')` if the rule was removed or narrowed so the chat is no longer admitted, and self-healing on the next inbound frame if another configured rule now admits it). When `options.session` is provided, that session or one of its hosted ancestors must have a writable binding for the target chat. Passing `options.quote` must name a message stored in `im_messages` for the target chat (keys from another bot, another chat, or unstored/expired keys are rejected with `ImError('access')`); it prepends a `<quote>` element to the message content and attaches the quoted message's stored `referrer` to the target, enabling platform thread placement and passive replies. Passing `options.thread` targets a specific thread id, or `true` to reply in the quoted message's thread. An explicit `thread` string must belong to the target chat: the sender reuses a matching stored message's `referrer` when available, otherwise the adapter resolves and verifies the anchor against the platform (`published: 'no'` if resolution or chat ownership fails). When every writable binding of `options.session` for the chat is confined to `thread`, `options.thread` is omitted, and `options.quote` names a thread root message (one with no `thread` field of its own), `send` and `stream` forward that root message id as `Target.thread` so the reply stays in the thread. A publication that names neither a thread nor a message stays in the thread its session answered last (`ImSessions.trigger`): that is where the session's own answers go, because output renderers quote the trigger and inherit its thread, so the chat's own timeline is not where a task's work belongs. A session confined to `thread` therefore publishes into the answered thread without naming it, and a bare publication is refused only when the answered message belongs to another thread.
|
||||
All outbound messages go through `ctx.im.send` (single message) or `ctx.im.stream` (streaming updates). Every publication also re-verifies the target chat against the current `config.sources` rule named on the stored chat record (throwing `ImError('access')` if the rule was removed or narrowed so the chat is no longer admitted, and self-healing on the next inbound frame if another configured rule now admits it). When `options.session` is provided, that session or one of its hosted ancestors must have a writable binding for the target chat. Passing `options.quote` must name a message stored in `im_messages` for the target chat (keys from another bot, another chat, or unstored/expired keys are rejected with `ImError('access')`); it prepends a `<quote>` element to the message content and attaches the quoted message's stored `referrer` to the target, enabling platform thread placement and passive replies. Passing `options.referrer` hands the adapter an explicit reply context — the `referrer` of an interaction the publication answers, such as a QQ button click, so the answer is a passive reply to that click; it replaces the referrer a quote or thread would supply, grants no access, and is not part of the receipt digest. Passing `options.thread` targets a specific thread id, or `true` to reply in the quoted message's thread. An explicit `thread` string must belong to the target chat: the sender reuses a matching stored message's `referrer` when available, otherwise the adapter resolves and verifies the anchor against the platform (`published: 'no'` if resolution or chat ownership fails). When every writable binding of `options.session` for the chat is confined to `thread`, `options.thread` is omitted, and `options.quote` names a thread root message (one with no `thread` field of its own), `send` and `stream` forward that root message id as `Target.thread` so the reply stays in the thread. A publication that names neither a thread nor a message stays in the thread its session answered last (`ImSessions.trigger`): that is where the session's own answers go, because output renderers quote the trigger and inherit its thread, so the chat's own timeline is not where a task's work belongs. A session confined to `thread` therefore publishes into the answered thread without naming it, and a bare publication is refused only when the answered message belongs to another thread.
|
||||
|
||||
When `options.operationId` is provided, the sender writes a `pending` receipt to `im_receipts` before calling the platform, then updates it to `confirmed`, `failed` (when the platform explicitly rejected the send before publishing), `partial` (when a multi-part send or an opened stream failed after publishing at least one part or frame that remains in the chat), or `uncertain` (on network failure, timeout, cancellation after dispatch, or an unrecorded crash state). Replaying the same `operationId` with the same target and content returns the stored receipt without resending; reusing an `operationId` with different parameters throws a `conflict` error.
|
||||
|
||||
|
||||
@@ -95,7 +95,7 @@ kind: "package-reference"
|
||||
|
||||
### 消息发送、格式化与历史
|
||||
|
||||
所有出站消息均通过 `ctx.im.send`(单条消息)或 `ctx.im.stream`(流式更新消息)发送。每次发送还会对照聊天记录上保存的规则 id 重新核验当前 `config.sources` 准入(若规则已被删除或收窄导致该聊天不再被接纳,则抛出 `ImError('access')`;若另一条当前规则仍接纳该聊天,则在下一条入站消息刷新 `chat.source` 后自愈)。传入 `options.session` 时,该会话或其托管祖先必须在目标聊天上拥有 `writable: true` 的绑定。传入 `options.quote` 必须指向目标聊天在 `im_messages` 中已存的消息键(来自其他机器人、其他聊天或未入库/已过期的键会以 `ImError('access')` 拒绝),它会在消息内容最前面插入 `<quote>` 元素,并将被引用消息保存的适配器 `referrer` 附加到发送目标上,从而支持被动回复与话题内回复。传入 `options.thread` 可指定目标话题线程 id,设为 `true` 则跟随被引用消息所在的话题线程。显式指定 `thread` 字符串时,该线程必须属于目标聊天:发送器优先复用同聊天已存消息的匹配 `referrer`,未命中时由适配器向平台解析并校验归属(解析或归属校验失败时以 `published: 'no'` 拒绝发布)。当 `options.session` 在该聊天上的全部可写绑定都受 `thread` 约束、未传入显式 `options.thread`、且 `options.quote` 指向话题根消息(自身无 `thread` 字段)时,`send` 与 `stream` 会将该根消息 id 作为 `Target.thread` 转发给适配器,使回复留在话题内。既未指定话题线程、也未引用消息的发送,落在该会话最后回复的消息所在的话题线程里(`ImSessions.trigger`):那正是该会话自身答案的落点,因为输出渲染器会引用这条触发消息并继承其话题线程,任务的工作不属于聊天主时间线。因此受 `thread` 约束的会话不必指定话题就能发到它正在回复的那条线程;只有当这条消息属于另一个话题线程时,未指定话题的发送才会被拒绝。
|
||||
所有出站消息均通过 `ctx.im.send`(单条消息)或 `ctx.im.stream`(流式更新消息)发送。每次发送还会对照聊天记录上保存的规则 id 重新核验当前 `config.sources` 准入(若规则已被删除或收窄导致该聊天不再被接纳,则抛出 `ImError('access')`;若另一条当前规则仍接纳该聊天,则在下一条入站消息刷新 `chat.source` 后自愈)。传入 `options.session` 时,该会话或其托管祖先必须在目标聊天上拥有 `writable: true` 的绑定。传入 `options.quote` 必须指向目标聊天在 `im_messages` 中已存的消息键(来自其他机器人、其他聊天或未入库/已过期的键会以 `ImError('access')` 拒绝),它会在消息内容最前面插入 `<quote>` 元素,并将被引用消息保存的适配器 `referrer` 附加到发送目标上,从而支持被动回复与话题内回复。传入 `options.referrer` 可显式指定适配器的回复上下文——即本次发送所回应的交互的 `referrer`(例如 QQ 按钮点击),使回复成为对该点击的被动回复;它会取代引用或话题线程本会提供的 `referrer`,不授予任何权限,也不计入回执摘要。传入 `options.thread` 可指定目标话题线程 id,设为 `true` 则跟随被引用消息所在的话题线程。显式指定 `thread` 字符串时,该线程必须属于目标聊天:发送器优先复用同聊天已存消息的匹配 `referrer`,未命中时由适配器向平台解析并校验归属(解析或归属校验失败时以 `published: 'no'` 拒绝发布)。当 `options.session` 在该聊天上的全部可写绑定都受 `thread` 约束、未传入显式 `options.thread`、且 `options.quote` 指向话题根消息(自身无 `thread` 字段)时,`send` 与 `stream` 会将该根消息 id 作为 `Target.thread` 转发给适配器,使回复留在话题内。既未指定话题线程、也未引用消息的发送,落在该会话最后回复的消息所在的话题线程里(`ImSessions.trigger`):那正是该会话自身答案的落点,因为输出渲染器会引用这条触发消息并继承其话题线程,任务的工作不属于聊天主时间线。因此受 `thread` 约束的会话不必指定话题就能发到它正在回复的那条线程;只有当这条消息属于另一个话题线程时,未指定话题的发送才会被拒绝。
|
||||
|
||||
传入 `options.operationId` 时,发送器会在调用平台接口前向 `im_receipts` 写入 `pending` 回执,并在调用结束后更新为观测到的结果:`confirmed`、`failed`(平台明确拒绝且未发布任何内容)、`partial`(多段消息或已开流的流式消息在部分内容已留在聊天中后失败)或 `uncertain`(网络故障、超时、发出后取消或崩溃后无结果记录)。使用相同的 `operationId`、目标与内容重放会直接返回已存回执而不会重复发送;相同 `operationId` 搭配不同参数会抛出 `conflict` 错误。
|
||||
|
||||
|
||||
@@ -581,6 +581,12 @@ export interface ImSendOptions extends SendOptions {
|
||||
* answered last in memory (falling back to the chat's main timeline when none exists or after a restart).
|
||||
*/
|
||||
thread?: string | true
|
||||
/**
|
||||
* Adapter-opaque reply context of the interaction this publication answers (`Target.referrer`), e.g. the referrer of
|
||||
* a QQ button click so the answer is a passive reply to that click. Replaces the referrer `quote` or `thread` would
|
||||
* supply; grants no access of its own.
|
||||
*/
|
||||
referrer?: unknown
|
||||
/** For grants check + attribution. */
|
||||
session?: SessionId
|
||||
}
|
||||
|
||||
@@ -12,7 +12,10 @@
|
||||
* `config.sources` still exists and admits it (else `ImError('access')`), and applies the caller's thread wish; `quote`
|
||||
* becomes the content's `<quote id>` envelope element, so the platform reply rides the normal transform/fallback path,
|
||||
* and the quoted stored message's adapter referrer becomes `Target.referrer` (passive replies, thread placement) — for
|
||||
* a stream on every state, because the target is resolved once.
|
||||
* a stream on every state, because the target is resolved once. An explicit `options.referrer` (the adapter-opaque
|
||||
* reply context of an interaction the caller answers, such as a QQ button click's `event_id`) replaces the one the
|
||||
* quote or thread would supply: the caller names the event it answers, which a stored message cannot. It carries no
|
||||
* grant of its own and is not part of a receipt's digest.
|
||||
* - **Receipts.** A publication with `options.operationId` records `pending` *before* it reaches the platform and the
|
||||
* observed outcome after it: `confirmed`, `failed` (the platform answered and published nothing —
|
||||
* `PlatformError.published === 'no'`, `StrictRenderError`, or a stream whose message was never opened or was
|
||||
@@ -317,12 +320,13 @@ export class ImSender {
|
||||
* current `config.sources`), the caller's thread wish and — when the publication answers a stored message or stored
|
||||
* thread of this chat — that message's adapter referrer (see {@link referrerOf}).
|
||||
* @param chat - chat key.
|
||||
* @param options - `thread` and `quote`; a `quote` is applied to the content (see {@link send}).
|
||||
* @param options - `thread`, `quote` and `referrer`; a `quote` is applied to the content (see {@link send}), an
|
||||
* explicit `referrer` replaces the quote's or thread's.
|
||||
* @returns the target handed to `bot.send` / `bot.stream`.
|
||||
* @throws ImError `access` when `chat` was never admitted or its stored `source` rule was removed or narrowed so it
|
||||
* no longer admits `chat`.
|
||||
*/
|
||||
target(chat: ImChatKey, options: Pick<ImSendOptions, 'thread' | 'quote'> = {}): Target {
|
||||
target(chat: ImChatKey, options: Pick<ImSendOptions, 'thread' | 'quote' | 'referrer'> = {}): Target {
|
||||
const known = this.deps.store.chat(chat)
|
||||
if (known === undefined) throw new ImError('access', `Chat ${chat} is not admitted to IM.`)
|
||||
const source = this.config.sources.find(candidate => candidate.id === known.source)
|
||||
@@ -330,7 +334,8 @@ export class ImSender {
|
||||
throw new ImError('access', `Chat ${chat} is no longer admitted to IM.`)
|
||||
}
|
||||
const thread = options.thread
|
||||
const referrer = this.referrerOf(chat, options.quote, thread)
|
||||
// An explicit referrer names the event being answered; it wins over the one a quote or thread would supply.
|
||||
const referrer = options.referrer !== undefined ? options.referrer : this.referrerOf(chat, options.quote, thread)
|
||||
return {
|
||||
chat: { ...known.ref },
|
||||
...thread === undefined ? {} : { thread },
|
||||
|
||||
@@ -911,6 +911,26 @@ describe('ImSender referrers', () => {
|
||||
expect(bot.operations).toHaveLength(1)
|
||||
})
|
||||
|
||||
it('an explicit referrer reaches the target unquoted, and replaces the one a quote supplies', async () => {
|
||||
const { bot, sender, store } = await boot()
|
||||
const CLICK = { event_id: 'evt-click', receivedAt: 2000, chatType: 'group', chatId: 'chat' }
|
||||
await sender.send(CHAT, h.text('answer to a click'), { referrer: CLICK })
|
||||
expect(bot.operations.at(-1)?.target?.referrer).toEqual(CLICK)
|
||||
|
||||
// With a quote the envelope stays, but the click is what the publication answers.
|
||||
const quoted = await store.put(inboundDraft(ImMessageKey('fake:bot', 'm7'), REFERRER), 'none')
|
||||
await sender.send(CHAT, h.text('quoted answer'), { quote: quoted.key, referrer: CLICK })
|
||||
expect(bot.operations.at(-1)?.target?.referrer).toEqual(CLICK)
|
||||
expect(bot.operations.at(-1)?.payload).toMatchObject({ quote: 'm7' })
|
||||
|
||||
// The referrer is not part of the request's identity: a replay with another one returns the recorded outcome.
|
||||
const first = await sender.send(CHAT, h.text('once'), { operationId: 'op-ref', referrer: CLICK })
|
||||
const sent = bot.operations.length
|
||||
const again = await sender.send(CHAT, h.text('once'), { operationId: 'op-ref', referrer: { event_id: 'evt-2' } })
|
||||
expect(again.status).toBe(first.status)
|
||||
expect(bot.operations).toHaveLength(sent)
|
||||
})
|
||||
|
||||
it('leaves the target without a referrer when unquoted or quoting a message without one', async () => {
|
||||
const { bot, sender, store } = await boot()
|
||||
await sender.send(CHAT, h.text('unquoted'))
|
||||
|
||||
Reference in new issue
Block a user