@zhin.js/adapter-kook 1.1.25 → 1.1.27

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.
Files changed (45) hide show
  1. package/CHANGELOG.md +173 -0
  2. package/LICENSE +21 -0
  3. package/PERMITS.md +20 -14
  4. package/README.md +6 -2
  5. package/adapters/kook/index.js +3 -0
  6. package/adapters/kook/index.ts +3 -0
  7. package/agents/kook/conversation.js +88 -0
  8. package/agents/kook/conversation.ts +99 -0
  9. package/agents/kook/messages.js +16 -0
  10. package/agents/kook/messages.ts +16 -0
  11. package/agents/kook/skills/kook/tools/blacklist/index.js +9 -6
  12. package/agents/kook/skills/kook/tools/blacklist/index.ts +9 -7
  13. package/agents/kook/skills/kook/tools/create_role/index.js +5 -6
  14. package/agents/kook/skills/kook/tools/create_role/index.ts +6 -7
  15. package/agents/kook/skills/kook/tools/delete_role/index.js +5 -6
  16. package/agents/kook/skills/kook/tools/delete_role/index.ts +6 -7
  17. package/agents/kook/skills/kook/tools/grant_role/index.js +9 -7
  18. package/agents/kook/skills/kook/tools/grant_role/index.ts +9 -8
  19. package/agents/kook/skills/kook/tools/list_roles/index.js +5 -7
  20. package/agents/kook/skills/kook/tools/list_roles/index.ts +6 -8
  21. package/agents/kook/skills/kook/tools/revoke_role/index.js +9 -7
  22. package/agents/kook/skills/kook/tools/revoke_role/index.ts +9 -8
  23. package/lib/console-spec.js +148 -13
  24. package/lib/endpoint.d.ts +2 -1
  25. package/lib/endpoint.js +27 -12
  26. package/lib/index.d.ts +1 -1
  27. package/lib/index.js +1 -1
  28. package/lib/platform-permit.d.ts +4 -19
  29. package/lib/platform-permit.js +7 -46
  30. package/lib/protocol.d.ts +1 -5
  31. package/lib/protocol.js +3 -28
  32. package/lib/sender-roles.d.ts +6 -6
  33. package/lib/sender-roles.js +55 -21
  34. package/lib/webhook.d.ts +1 -2
  35. package/lib/webhook.js +13 -33
  36. package/package.json +23 -26
  37. package/plugin.js +0 -6
  38. package/schema.json +0 -56
  39. package/src/console-spec.ts +148 -13
  40. package/src/endpoint.ts +27 -13
  41. package/src/index.ts +1 -7
  42. package/src/platform-permit.ts +5 -70
  43. package/src/protocol.ts +4 -33
  44. package/src/sender-roles.ts +59 -27
  45. package/src/webhook.ts +14 -36
package/src/webhook.ts CHANGED
@@ -2,7 +2,6 @@ import { endpointSpec, type Endpoint } from 'zhin.js/adapter';
2
2
  /**
3
3
  * KOOK webhook HTTP: verify → parse → admit.
4
4
  */
5
- import type { IncomingMessage, ServerResponse } from 'node:http';
6
5
  import type { HttpHost, HttpRouteRegistration } from '@zhin.js/host-http';
7
6
  import { formatCompact, getLogger } from '@zhin.js/logger';
8
7
  import {
@@ -34,17 +33,14 @@ export function registerKookWebhookRoutes(
34
33
  ): HttpRouteRegistration[] {
35
34
  const path = handler.endpointConfig.webhookPath;
36
35
  return [
37
- http.route('POST', path, async (request, response) => {
38
- await handleKookWebhookRequest(request, response, handler);
39
- }, { summary: 'KOOK webhook callback', tags: ['kook'] }),
36
+ http.route('POST', path, (request) => handleKookWebhookRequest(request, handler), { summary: 'KOOK webhook callback', tags: ['kook'] }),
40
37
  ];
41
38
  }
42
39
 
43
40
  export async function handleKookWebhookRequest(
44
- request: IncomingMessage,
45
- response: ServerResponse,
41
+ request: Request,
46
42
  handler: KookWebhookHandler,
47
- ): Promise<void> {
43
+ ): Promise<Response> {
48
44
  try {
49
45
  const rawBody = await readRequestBody(request);
50
46
  let frame: KookWebhookFrame;
@@ -56,46 +52,32 @@ export async function handleKookWebhookRequest(
56
52
  ok: false,
57
53
  error: error instanceof Error ? error.message : String(error),
58
54
  }));
59
- response.writeHead(400, { 'Content-Type': 'application/json' });
60
- response.end(JSON.stringify({ message: 'Invalid webhook payload' }));
61
- return;
55
+ return new Response(JSON.stringify({ message: 'Invalid webhook payload' }), { status: 400, headers: { 'Content-Type': 'application/json' } });
62
56
  }
63
57
 
64
58
  const event = frame.d;
65
59
  if (!event || typeof event !== 'object') {
66
- response.writeHead(200, { 'Content-Type': 'application/json' });
67
- response.end(JSON.stringify({ message: 'OK' }));
68
- return;
60
+ return new Response(JSON.stringify({ message: 'OK' }), { status: 200, headers: { 'Content-Type': 'application/json' } });
69
61
  }
70
62
 
71
63
  if (isKookWebhookChallenge(event)) {
72
64
  if (!verifyKookWebhookToken(handler.endpointConfig.verifyToken, event.verify_token)) {
73
- response.writeHead(403, { 'Content-Type': 'application/json' });
74
- response.end(JSON.stringify({ message: 'Invalid verify_token' }));
75
- return;
65
+ return new Response(JSON.stringify({ message: 'Invalid verify_token' }), { status: 403, headers: { 'Content-Type': 'application/json' } });
76
66
  }
77
- response.writeHead(200, { 'Content-Type': 'application/json' });
78
- response.end(JSON.stringify({ challenge: event.challenge ?? '' }));
79
- return;
67
+ return new Response(JSON.stringify({ challenge: event.challenge ?? '' }), { status: 200, headers: { 'Content-Type': 'application/json' } });
80
68
  }
81
69
 
82
70
  if (!verifyKookWebhookToken(handler.endpointConfig.verifyToken, event.verify_token)) {
83
- response.writeHead(403, { 'Content-Type': 'application/json' });
84
- response.end(JSON.stringify({ message: 'Invalid verify_token' }));
85
- return;
71
+ return new Response(JSON.stringify({ message: 'Invalid verify_token' }), { status: 403, headers: { 'Content-Type': 'application/json' } });
86
72
  }
87
73
 
88
74
  if (!handler.isOpen) {
89
- response.writeHead(503, { 'Content-Type': 'application/json' });
90
- response.end(JSON.stringify({ message: 'Endpoint unavailable' }));
91
- return;
75
+ return new Response(JSON.stringify({ message: 'Endpoint unavailable' }), { status: 503, headers: { 'Content-Type': 'application/json' } });
92
76
  }
93
- await handler[endpointSpec].events.run(async () => {
77
+ return await handler[endpointSpec].events.run(async () => {
94
78
  if (frame.sn != null) {
95
79
  if (!handler.checkAndRememberSn(frame.sn)) {
96
- response.writeHead(200, { 'Content-Type': 'application/json' });
97
- response.end(JSON.stringify({ message: 'OK' }));
98
- return;
80
+ return new Response(JSON.stringify({ message: 'OK' }), { status: 200, headers: { 'Content-Type': 'application/json' } });
99
81
  }
100
82
  }
101
83
 
@@ -103,17 +85,13 @@ export async function handleKookWebhookRequest(
103
85
  await handler.admitEvent(event);
104
86
  } catch {
105
87
  if (frame.sn != null) handler.forgetSn?.(frame.sn);
106
- response.writeHead(503, { 'Content-Type': 'application/json' });
107
- response.end(JSON.stringify({ message: 'Admission unavailable' }));
108
- return;
88
+ return new Response(JSON.stringify({ message: 'Admission unavailable' }), { status: 503, headers: { 'Content-Type': 'application/json' } });
109
89
  }
110
90
 
111
- response.writeHead(200, { 'Content-Type': 'application/json' });
112
- response.end(JSON.stringify({ message: 'OK' }));
91
+ return new Response(JSON.stringify({ message: 'OK' }), { status: 200, headers: { 'Content-Type': 'application/json' } });
113
92
  });
114
93
  } catch (error) {
115
94
  logger.error('KOOK webhook error:', error);
116
- response.writeHead(200, { 'Content-Type': 'application/json' });
117
- response.end(JSON.stringify({ message: 'OK' }));
95
+ return new Response(JSON.stringify({ message: 'OK' }), { status: 200, headers: { 'Content-Type': 'application/json' } });
118
96
  }
119
97
  }