typeclaw 0.38.0 → 0.39.0

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 (63) hide show
  1. package/README.md +4 -0
  2. package/auth.schema.json +109 -0
  3. package/package.json +2 -2
  4. package/secrets.schema.json +109 -0
  5. package/src/agent/attention-escalation.ts +4 -3
  6. package/src/agent/auth.ts +2 -2
  7. package/src/agent/index.ts +25 -2
  8. package/src/agent/restart/index.ts +17 -8
  9. package/src/agent/session-origin.ts +15 -2
  10. package/src/agent/subagents.ts +29 -1
  11. package/src/agent/system-prompt.ts +18 -5
  12. package/src/agent/tools/restart.ts +0 -3
  13. package/src/agent/tools/spawn-subagent.ts +7 -0
  14. package/src/bundled-plugins/memory/README.md +12 -1
  15. package/src/bundled-plugins/memory/index.ts +63 -8
  16. package/src/bundled-plugins/memory/memory-logger.ts +14 -10
  17. package/src/bundled-plugins/operator/operator.ts +1 -0
  18. package/src/channels/adapters/kakaotalk.ts +1 -1
  19. package/src/channels/adapters/webex-bot-classify.ts +20 -14
  20. package/src/channels/adapters/webex-bot-reference.ts +8 -6
  21. package/src/channels/adapters/webex-bot.ts +106 -39
  22. package/src/channels/adapters/webex-channel-resolver.ts +29 -0
  23. package/src/channels/adapters/webex-classify.ts +104 -0
  24. package/src/channels/adapters/webex-format.ts +53 -0
  25. package/src/channels/adapters/webex-id-ref.ts +32 -0
  26. package/src/channels/adapters/webex-reference.ts +49 -0
  27. package/src/channels/adapters/webex.ts +493 -0
  28. package/src/channels/manager.ts +63 -7
  29. package/src/channels/persistence.ts +67 -5
  30. package/src/channels/router.ts +8 -0
  31. package/src/channels/schema.ts +2 -0
  32. package/src/cli/channel.ts +118 -7
  33. package/src/cli/init.ts +70 -1
  34. package/src/cli/model.ts +143 -3
  35. package/src/config/channels-mutation.ts +2 -1
  36. package/src/config/config.ts +94 -31
  37. package/src/config/index.ts +3 -0
  38. package/src/config/models-mutation.ts +105 -15
  39. package/src/config/providers-mutation.ts +4 -4
  40. package/src/config/providers.ts +6 -14
  41. package/src/container/shared.ts +21 -0
  42. package/src/container/start.ts +25 -7
  43. package/src/doctor/channel-checks.ts +43 -0
  44. package/src/hostd/daemon.ts +7 -5
  45. package/src/hostd/index.ts +1 -11
  46. package/src/hostd/paths.ts +0 -16
  47. package/src/hostd/protocol.ts +2 -2
  48. package/src/init/checkpoint.ts +1 -1
  49. package/src/init/index.ts +60 -3
  50. package/src/init/run-owner-claim.ts +1 -0
  51. package/src/init/webex-auth.ts +68 -0
  52. package/src/inspect/label.ts +13 -2
  53. package/src/inspect/render.ts +5 -3
  54. package/src/permissions/resolve.ts +38 -1
  55. package/src/role-claim/match-rule.ts +5 -1
  56. package/src/run/index.ts +4 -1
  57. package/src/secrets/codex-auth-json.ts +52 -17
  58. package/src/secrets/encryption.ts +5 -1
  59. package/src/secrets/export-codex-auth-file.ts +26 -3
  60. package/src/secrets/schema.ts +35 -0
  61. package/src/secrets/webex-store.ts +113 -0
  62. package/src/skills/typeclaw-config/SKILL.md +18 -16
  63. package/typeclaw.schema.json +326 -0
@@ -0,0 +1,104 @@
1
+ // WebexListenerEventMap requires agent-messenger/webex (PR #239); resolves via linked upstream.
2
+ import type { WebexListenerEventMap } from 'agent-messenger/webex'
3
+
4
+ import { matchesAnyAlias } from '@/channels/engagement'
5
+ import type { ChannelAdapterConfig } from '@/channels/schema'
6
+ import type { InboundAttachment, InboundMessage } from '@/channels/types'
7
+
8
+ export type WebexInboundMessage = WebexListenerEventMap['message_created'][0]
9
+
10
+ export type InboundDropReason = 'self_author' | 'empty_content' | 'pre_connect'
11
+
12
+ export type InboundClassification =
13
+ | { kind: 'drop'; reason: InboundDropReason }
14
+ | { kind: 'route'; payload: InboundMessage }
15
+
16
+ // `botPersonRef` is the bot's decoded UUID ref (from WebexPerson.ref), matched
17
+ // against the event's *Ref fields so identity comparison happens entirely in
18
+ // ref-space — the same currency typeclaw now stores as ChannelKey/authorId.
19
+ export function classifyInbound(
20
+ event: WebexInboundMessage,
21
+ _config: ChannelAdapterConfig,
22
+ botPersonRef: string | null,
23
+ selfAliases: readonly string[] = [],
24
+ ): InboundClassification {
25
+ if (botPersonRef !== null && event.personRef === botPersonRef) {
26
+ return { kind: 'drop', reason: 'self_author' }
27
+ }
28
+
29
+ const { text, attachments } = splitInbound(event)
30
+ if (text === '') return { kind: 'drop', reason: 'empty_content' }
31
+
32
+ if (botPersonRef === null) {
33
+ return { kind: 'drop', reason: 'pre_connect' }
34
+ }
35
+
36
+ const isDm = event.roomType === 'direct'
37
+ const structuredBotMention = event.mentionedPeopleRefs.includes(botPersonRef) || event.mentionedGroups.includes('all')
38
+ const aliasMatched = !structuredBotMention && matchesAnyAlias(text, selfAliases)
39
+ const isBotMention = structuredBotMention || aliasMatched
40
+ const mentionsOthers = event.mentionedPeopleRefs.length > 0 && !event.mentionedPeopleRefs.includes(botPersonRef)
41
+ const ts = Date.parse(event.created)
42
+
43
+ return {
44
+ kind: 'route',
45
+ payload: {
46
+ adapter: 'webex',
47
+ // Webex message events do not include an org/team id; the room ref is the
48
+ // stable permission bucket for group spaces while DMs use the shared key.
49
+ // The decoded UUID ref is stored (not the base64 REST id) so the persisted
50
+ // key is human-readable; the SDK re-encodes it for outbound calls.
51
+ workspace: isDm ? '@dm' : event.roomRef,
52
+ chat: event.roomRef,
53
+ thread: null,
54
+ text,
55
+ ...(attachments.length > 0 ? { attachments } : {}),
56
+ externalMessageId: event.ref,
57
+ authorId: event.personRef,
58
+ authorName: event.personEmail,
59
+ authorIsBot: false,
60
+ isBotMention,
61
+ // Webex Mercury only exposes the parent ref inline, not the parent author.
62
+ // When the reply has a structured bot mention we can identify it as
63
+ // bot-directed; otherwise leave the parent unattributed instead of guessing.
64
+ // Alias matches are mention-equivalent for engagement, but they do not prove
65
+ // the parent is bot-authored; enrichment fetches the parent and attributes it.
66
+ replyToBotMessageId: event.parentRef !== undefined && structuredBotMention ? event.parentRef : null,
67
+ mentionsOthers,
68
+ replyToOtherMessageId: null,
69
+ isDm,
70
+ ts: Number.isFinite(ts) ? ts : 0,
71
+ },
72
+ }
73
+ }
74
+
75
+ type SplitInbound = { text: string; attachments: InboundAttachment[] }
76
+
77
+ function splitInbound(event: WebexInboundMessage): SplitInbound {
78
+ const attachments = event.files.map((ref: string, index: number) => ({
79
+ id: index + 1,
80
+ kind: 'file' as const,
81
+ ref,
82
+ filename: filenameFromUrl(ref) ?? `webex-file-${index + 1}`,
83
+ }))
84
+ if (attachments.length === 0) return { text: event.text, attachments: [] }
85
+ const summary = attachments.map(renderPlaceholder).join('\n')
86
+ const text = event.text === '' ? summary : `${event.text}\n${summary}`
87
+ return { text, attachments }
88
+ }
89
+
90
+ function filenameFromUrl(ref: string): string | null {
91
+ try {
92
+ const url = new URL(ref)
93
+ const name = url.pathname.split('/').filter(Boolean).pop()
94
+ return name === undefined || name === '' ? null : name
95
+ } catch {
96
+ return null
97
+ }
98
+ }
99
+
100
+ function renderPlaceholder(attachment: InboundAttachment): string {
101
+ const parts: string[] = [`Webex attachment #${attachment.id}: ${attachment.kind}`]
102
+ if (attachment.filename !== undefined) parts.push(`name=${attachment.filename}`)
103
+ return `[${parts.join(' ')}]`
104
+ }
@@ -0,0 +1,53 @@
1
+ import type { WebexMessage } from 'agent-messenger/webex'
2
+
3
+ // Webex's E2E (internal conversation) path renders agent markdown as an HTML
4
+ // `content` field that some clients display verbatim, so messages sent with
5
+ // `markdown: true` leak literal `<br/>` and `&apos;`. typeclaw sends plain text
6
+ // outbound; inbound we still read the HTML `html` field as a fallback, so this
7
+ // undoes that HTML. Protocol HTML (not natural language) -> ASCII literal set.
8
+
9
+ // Normalize ONLY the HTML `html` fallback: `text`/`markdown` are author-clean and
10
+ // must stay raw, or literal `&`/`<` the user typed would be corrupted.
11
+ export function resolveWebexBodyText(msg: Pick<WebexMessage, 'text' | 'markdown' | 'html'>): string {
12
+ if (msg.text !== undefined && msg.text !== '') return msg.text
13
+ if (msg.markdown !== undefined && msg.markdown !== '') return msg.markdown
14
+ if (msg.html !== undefined && msg.html !== '') return normalizeWebexHtmlFallbackText(msg.html)
15
+ return ''
16
+ }
17
+
18
+ export function normalizeWebexHtmlFallbackText(value: string): string {
19
+ const withBreaks = value
20
+ .replace(/<br\s*\/?>/gi, '\n')
21
+ .replace(/<\/p>\s*<p[^>]*>/gi, '\n\n')
22
+ .replace(/<\/?[^>]+>/g, '')
23
+ return decodeHtmlEntities(withBreaks)
24
+ }
25
+
26
+ function decodeHtmlEntities(value: string): string {
27
+ return value.replace(/&(#x[0-9a-f]+|#\d+|amp|lt|gt|quot|apos|nbsp);/gi, (match, entity: string) => {
28
+ const lower = entity.toLowerCase()
29
+ if (lower.startsWith('#x')) return codePoint(Number.parseInt(lower.slice(2), 16))
30
+ if (lower.startsWith('#')) return codePoint(Number.parseInt(lower.slice(1), 10))
31
+ switch (lower) {
32
+ case 'amp':
33
+ return '&'
34
+ case 'lt':
35
+ return '<'
36
+ case 'gt':
37
+ return '>'
38
+ case 'quot':
39
+ return '"'
40
+ case 'apos':
41
+ return "'"
42
+ case 'nbsp':
43
+ return ' '
44
+ default:
45
+ return match
46
+ }
47
+ })
48
+ }
49
+
50
+ function codePoint(value: number): string {
51
+ if (!Number.isFinite(value) || value < 0 || value > 0x10ffff) return ''
52
+ return String.fromCodePoint(value)
53
+ }
@@ -0,0 +1,32 @@
1
+ import { decodeWebexId } from 'agent-messenger/webex'
2
+ import type { WebexRestIdType } from 'agent-messenger/webex'
3
+
4
+ // Webex REST ids are base64url of `ciscospark://<cluster>/<TYPE>/<uuid>`. The
5
+ // opaque blob is unreadable in `inspect` and — worst — in hand-authored
6
+ // permission rules (`webex:* author:Y2lzY29zcGFyazov...`). A `ref` is the
7
+ // decoded trailing value: a UUID for ROOM/MESSAGE, a UUID OR an email for
8
+ // PEOPLE (legacy "Hydra" accounts encode the email).
9
+ //
10
+ // `decodeWebexId` comes from agent-messenger and is null-safe on non-ids.
11
+ // `toRef` and `isWebexIdOfType` are local wrappers: upstream's own `toRef`
12
+ // THROWS on a value that is not a valid REST id (e.g. a bare uuid already in
13
+ // ref form), but every typeclaw call site feeds it values that may already be
14
+ // refs or non-webex tokens, so we need the fall-open contract instead.
15
+ //
16
+ // `ref` is for human-facing surfaces only; the canonical base64 id stays the
17
+ // wire/session currency.
18
+
19
+ export { decodeWebexId } from 'agent-messenger/webex'
20
+ export type { DecodedWebexId, WebexRestIdType } from 'agent-messenger/webex'
21
+
22
+ export function toRef(id: string): string {
23
+ const decoded = decodeWebexId(id)
24
+ return decoded === null ? id : decoded.uuid
25
+ }
26
+
27
+ // Refuses cross-type collisions: a decoded ROOM uuid must never satisfy an
28
+ // `author:` (PEOPLE) rule even if the trailing uuid strings happen to match.
29
+ export function isWebexIdOfType(id: string, type: WebexRestIdType): boolean {
30
+ const decoded = decodeWebexId(id)
31
+ return decoded !== null && decoded.type === type
32
+ }
@@ -0,0 +1,49 @@
1
+ import type { WebexClient } from 'agent-messenger/webex'
2
+
3
+ import type { InboundMessage, QuoteAnchorSource } from '@/channels/types'
4
+
5
+ import { resolveWebexBodyText } from './webex-format'
6
+
7
+ // Webex Mercury delivers only the parent message id inline, not its author,
8
+ // so the classifier cannot tell whether a reply targets the bot. We fetch the
9
+ // parent here (already needed for the quote anchor) and use its author to set
10
+ // the reply-target fields the engagement layer reads: a non-mention reply to a
11
+ // bot-authored parent must engage via the `reply` trigger, and a reply to a
12
+ // different author must suppress the solo-human fallback. Both are no-ops if
13
+ // the classifier already attributed the reply (it sets replyToBotMessageId
14
+ // when the reply also @-mentions the bot).
15
+ export async function enrichWebexMessageReference(args: {
16
+ client: Pick<WebexClient, 'getMessage'>
17
+ inbound: InboundMessage
18
+ parentRef?: string
19
+ botPersonId: string | null
20
+ }): Promise<InboundMessage> {
21
+ if (args.parentRef === undefined || args.parentRef === '') return args.inbound
22
+ try {
23
+ const parent = await args.client.getMessage(args.parentRef)
24
+ const attributed = attributeReply(args.inbound, parent.ref, parent.personRef, args.botPersonId)
25
+ const source: QuoteAnchorSource = {
26
+ adapter: 'webex',
27
+ authorId: parent.personRef,
28
+ authorName: parent.personEmail,
29
+ text: resolveWebexBodyText(parent),
30
+ }
31
+ if (source.text.trim() === '') return attributed
32
+ return { ...attributed, referenceContext: { kind: 'reply', sources: [source] } }
33
+ } catch {
34
+ return args.inbound
35
+ }
36
+ }
37
+
38
+ function attributeReply(
39
+ inbound: InboundMessage,
40
+ parentId: string,
41
+ parentPersonId: string,
42
+ botPersonId: string | null,
43
+ ): InboundMessage {
44
+ if (inbound.replyToBotMessageId !== null || inbound.replyToOtherMessageId !== null) return inbound
45
+ if (botPersonId !== null && parentPersonId === botPersonId) {
46
+ return { ...inbound, replyToBotMessageId: parentId }
47
+ }
48
+ return { ...inbound, replyToOtherMessageId: parentId }
49
+ }