@hirey/hi-agent-delivery 0.1.4 → 0.1.5

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.
@@ -6,6 +6,16 @@ export type SharedAgentDeliveryEventEnvelope = {
6
6
  preview?: Record<string, unknown> | null;
7
7
  payload?: Record<string, unknown> | null;
8
8
  resource_ref?: Record<string, unknown> | null;
9
+ reply_route_snapshot?: {
10
+ installation_id: string;
11
+ session_key?: string | null;
12
+ delivery_context?: {
13
+ channel?: string | null;
14
+ to?: string | null;
15
+ account_id?: string | null;
16
+ thread_id?: string | null;
17
+ } | null;
18
+ } | null;
9
19
  };
10
20
  export declare function buildGenericEventWebhookPayload(event: SharedAgentDeliveryEventEnvelope): {
11
21
  profile: string;
@@ -1 +1 @@
1
- {"version":3,"file":"payloads.d.ts","sourceRoot":"","sources":["../src/payloads.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,gCAAgC,EAChC,2BAA2B,EAC5B,MAAM,2BAA2B,CAAC;AAGnC,MAAM,MAAM,gCAAgC,GAAG;IAC7C,QAAQ,EAAE,MAAM,CAAC;IACjB,eAAe,EAAE,MAAM,CAAC;IACxB,KAAK,EAAE,MAAM,CAAC;IACd,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,IAAI,CAAC;IACzC,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,IAAI,CAAC;IACzC,YAAY,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,IAAI,CAAC;CAC/C,CAAC;AAWF,wBAAgB,+BAA+B,CAAC,KAAK,EAAE,gCAAgC;;;;;;;EAUtF;AAED,wBAAgB,wBAAwB,CAAC,IAAI,EAAE;IAC7C,KAAK,EAAE,gCAAgC,CAAC;IACxC,MAAM,CAAC,EAAE,gCAAgC,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,IAAI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IACxC,MAAM,CAAC,EAAE,gCAAgC,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,IAAI,CAAC;CAC5E;;;;;;;;;;;;EA+BA;AAED,wBAAgB,yBAAyB,CAAC,IAAI,EAAE;IAC9C,KAAK,EAAE,gCAAgC,CAAC;IACxC,MAAM,EAAE,2BAA2B,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CAC/D;;;;;;EAiBA"}
package/dist/payloads.js CHANGED
@@ -1,12 +1,16 @@
1
1
  import { normalizeText } from '@hirey/hi-agent-contracts';
2
- function stringifyEventSummary(event) {
2
+ function buildOpenClawContinuationMessage(event, messagePrefix) {
3
+ const parts = [];
4
+ if (messagePrefix)
5
+ parts.push(messagePrefix);
3
6
  if (event.preview?.text)
4
- return String(event.preview.text);
5
- return JSON.stringify({
7
+ parts.push(String(event.preview.text));
8
+ parts.push(JSON.stringify({
6
9
  topic: event.topic,
7
10
  payload: event.payload || {},
8
11
  resource_ref: event.resource_ref || {},
9
- });
12
+ }));
13
+ return parts.filter(Boolean).join('\n\n');
10
14
  }
11
15
  export function buildGenericEventWebhookPayload(event) {
12
16
  // generic webhook 始终直接转发 canonical event envelope,不额外塞宿主私货。
@@ -21,22 +25,29 @@ export function buildGenericEventWebhookPayload(event) {
21
25
  }
22
26
  export function buildOpenClawHookPayload(args) {
23
27
  const config = (args.config || {});
24
- const messagePrefix = normalizeText(config.message_prefix);
25
- const eventSummary = stringifyEventSummary(args.event);
26
- const message = [messagePrefix, eventSummary].filter(Boolean).join('\n\n') || eventSummary;
28
+ const routeSnapshot = args.event.reply_route_snapshot && typeof args.event.reply_route_snapshot === 'object'
29
+ ? args.event.reply_route_snapshot
30
+ : {};
31
+ const deliveryContext = routeSnapshot.delivery_context && typeof routeSnapshot.delivery_context === 'object'
32
+ ? routeSnapshot.delivery_context
33
+ : {};
34
+ const messagePrefix = normalizeText(config.message_prefix) || null;
35
+ const explicitSessionKey = normalizeText(routeSnapshot.session_key || config.session_key) || null;
27
36
  // OpenClaw 默认禁用外部 /hooks/agent request 自带 sessionKey。
28
- // 普通用户路径只在显式声明 send_session_key=true 时才发送它,避免默认 400
29
- const shouldSendSessionKey = config.send_session_key === true;
37
+ // 普通用户路径只在显式声明 send_session_key=true 且没有显式 route truth 时才发送 legacy key
38
+ const shouldSendSessionKey = config.send_session_key === true && !explicitSessionKey;
30
39
  const sessionKeyPrefix = normalizeText(config.session_key_prefix) || 'hi';
31
40
  return {
32
- message,
41
+ message: buildOpenClawContinuationMessage(args.event, messagePrefix),
33
42
  name: normalizeText(config.name) || 'Hi',
34
43
  agentId: normalizeText(config.agent_id) || undefined,
35
- ...(shouldSendSessionKey ? { sessionKey: `${sessionKeyPrefix}:${args.event.event_id}` } : {}),
44
+ ...(explicitSessionKey
45
+ ? { sessionKey: explicitSessionKey }
46
+ : (shouldSendSessionKey ? { sessionKey: `${sessionKeyPrefix}:${args.event.event_id}` } : {})),
36
47
  wakeMode: normalizeText(config.wake_mode) || 'now',
37
48
  deliver: config.deliver !== false,
38
- channel: normalizeText(config.channel) || undefined,
39
- to: normalizeText(config.to) || undefined,
49
+ channel: normalizeText(deliveryContext.channel || config.channel) || undefined,
50
+ to: normalizeText(deliveryContext.to || config.to) || undefined,
40
51
  model: normalizeText(config.model) || undefined,
41
52
  thinking: normalizeText(config.thinking) || undefined,
42
53
  timeoutSeconds: Number(config.timeout_seconds || 0) || undefined,
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=payloads.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"payloads.test.d.ts","sourceRoot":"","sources":["../src/payloads.test.ts"],"names":[],"mappings":""}
@@ -0,0 +1,39 @@
1
+ import assert from 'node:assert/strict';
2
+ import test from 'node:test';
3
+ import { buildOpenClawHookPayload } from './payloads.js';
4
+ test('buildOpenClawHookPayload prefers explicit reply route over legacy generated session key', () => {
5
+ const payload = buildOpenClawHookPayload({
6
+ event: {
7
+ event_id: 'evt_123',
8
+ target_agent_id: 'agent_main',
9
+ topic: 'pairing.updated',
10
+ preview: { text: 'Timeline updated' },
11
+ payload: { status: 'contacted' },
12
+ resource_ref: { resource_type: 'pairing', resource_id: 'pair_123' },
13
+ reply_route_snapshot: {
14
+ installation_id: 'agit_123',
15
+ session_key: 'sess_visible_thread',
16
+ delivery_context: {
17
+ channel: 'imessage',
18
+ to: '+15550001111',
19
+ },
20
+ },
21
+ },
22
+ config: {
23
+ name: 'Hirey Hi',
24
+ agent_id: 'main',
25
+ send_session_key: true,
26
+ session_key_prefix: 'legacy',
27
+ message_prefix: '[Hi Event]',
28
+ channel: 'sms',
29
+ to: '+15550002222',
30
+ },
31
+ });
32
+ assert.equal(payload.sessionKey, 'sess_visible_thread');
33
+ assert.equal(payload.channel, 'imessage');
34
+ assert.equal(payload.to, '+15550001111');
35
+ assert.match(String(payload.message), /\[Hi Event\]/);
36
+ assert.match(String(payload.message), /Timeline updated/);
37
+ assert.match(String(payload.message), /"topic":"pairing\.updated"/);
38
+ assert.match(String(payload.message), /"resource_id":"pair_123"/);
39
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hirey/hi-agent-delivery",
3
- "version": "0.1.4",
3
+ "version": "0.1.5",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -22,19 +22,21 @@
22
22
  "scripts": {
23
23
  "clean": "node -e \"require('node:fs').rmSync('dist', { recursive: true, force: true })\"",
24
24
  "build": "npm run clean && tsc -p tsconfig.json",
25
+ "test": "node --test --import tsx src/**/*.test.ts",
25
26
  "prepack": "npm run build"
26
27
  },
27
28
  "publishConfig": {
28
29
  "access": "public"
29
30
  },
30
31
  "dependencies": {
31
- "@hirey/hi-agent-contracts": "^0.1.9",
32
+ "@hirey/hi-agent-contracts": "^0.1.12",
32
33
  "nanoid": "^5.0.7",
33
34
  "pg": "^8.11.3"
34
35
  },
35
36
  "devDependencies": {
36
37
  "@types/node": "^20.11.30",
37
38
  "@types/pg": "^8.6.6",
39
+ "tsx": "^4.21.0",
38
40
  "typescript": "^5.4.5"
39
41
  }
40
42
  }