@alexlikevibe/lark-channel-bridge 0.1.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.
@@ -0,0 +1,161 @@
1
+ type AgentEvent = {
2
+ type: 'system';
3
+ sessionId?: string;
4
+ threadId?: string;
5
+ cwd?: string;
6
+ model?: string;
7
+ } | {
8
+ type: 'text';
9
+ delta: string;
10
+ } | {
11
+ type: 'thinking';
12
+ delta: string;
13
+ } | {
14
+ type: 'tool_use';
15
+ id: string;
16
+ name: string;
17
+ input: unknown;
18
+ } | {
19
+ type: 'tool_result';
20
+ id: string;
21
+ output: string;
22
+ isError: boolean;
23
+ } | {
24
+ type: 'usage';
25
+ inputTokens?: number;
26
+ outputTokens?: number;
27
+ cachedInputTokens?: number;
28
+ reasoningOutputTokens?: number;
29
+ costUsd?: number;
30
+ } | {
31
+ type: 'done';
32
+ sessionId?: string;
33
+ threadId?: string;
34
+ terminationReason: 'normal' | 'interrupted' | 'timeout';
35
+ } | {
36
+ type: 'error';
37
+ message: string;
38
+ terminationReason: 'failed' | 'interrupted' | 'timeout';
39
+ };
40
+
41
+ type ToolStatus = 'running' | 'done' | 'error';
42
+ interface ToolEntry {
43
+ id: string;
44
+ name: string;
45
+ input: unknown;
46
+ status: ToolStatus;
47
+ output?: string;
48
+ }
49
+ type Block = {
50
+ kind: 'text';
51
+ content: string;
52
+ streaming: boolean;
53
+ } | {
54
+ kind: 'tool';
55
+ tool: ToolEntry;
56
+ };
57
+ type FooterStatus = 'thinking' | 'tool_running' | 'streaming' | null;
58
+ type Terminal = 'running' | 'done' | 'interrupted' | 'error' | 'idle_timeout';
59
+ interface RunState {
60
+ blocks: Block[];
61
+ reasoning: {
62
+ content: string;
63
+ active: boolean;
64
+ };
65
+ footer: FooterStatus;
66
+ terminal: Terminal;
67
+ errorMsg?: string;
68
+ /** Set when terminal === 'idle_timeout' — how long claude was idle before
69
+ * the watchdog gave up (so the message can say "N 分钟无响应"). */
70
+ idleTimeoutMinutes?: number;
71
+ }
72
+ declare const initialState: RunState;
73
+ declare function reduce(state: RunState, evt: AgentEvent): RunState;
74
+ declare function markInterrupted(state: RunState): RunState;
75
+ declare function finalizeIfRunning(state: RunState): RunState;
76
+
77
+ interface RunCardRenderOptions {
78
+ signCallback?: (action: string) => string;
79
+ }
80
+ declare function renderCard(state: RunState, options?: RunCardRenderOptions): object;
81
+
82
+ /**
83
+ * Render `RunState` as plain markdown text — used in `messageReply: 'text'`
84
+ * mode where we stream a markdown message instead of a card.
85
+ *
86
+ * Differences vs `renderCard`:
87
+ * - No collapsible panels, no buttons (markdown messages have neither)
88
+ * - Tool calls collapse to a single short line each (no body)
89
+ * - No reasoning / thinking output (no place to fold it; would be noise)
90
+ * - Footer is appended inline at the bottom while running
91
+ */
92
+ declare function renderText(state: RunState): string;
93
+
94
+ /**
95
+ * Structured logger.
96
+ *
97
+ * Two destinations on every call:
98
+ * 1. JSON line into the active profile logs directory — the durable
99
+ * record `/doctor` greps over.
100
+ * 2. Compact human-readable line on stdout/stderr — for live tailing in dev.
101
+ *
102
+ * Per-message context (traceId, chatId, msgId) is propagated automatically
103
+ * via AsyncLocalStorage; call `withTrace()` once at the entry point and any
104
+ * downstream `log.*` calls pick up the same fields.
105
+ */
106
+ interface LogContext {
107
+ traceId?: string;
108
+ chatId?: string;
109
+ msgId?: string;
110
+ }
111
+ type LogFields = Record<string, unknown>;
112
+ declare function reportMetric(name: string, value: number, tags?: Record<string, string>): void;
113
+ declare function reportError(err: unknown, ctx?: Record<string, unknown>): void;
114
+
115
+ /**
116
+ * Optional telemetry hook.
117
+ *
118
+ * The bridge itself ships **no** telemetry: by default this module is inert
119
+ * (a noop adapter), pulls in zero dependencies, and makes zero network calls.
120
+ *
121
+ * An operator who wants monitoring points `LARK_CHANNEL_TELEMETRY_MODULE` at a
122
+ * package that default-exports (or exposes `createAdapter`) an `AdapterFactory`.
123
+ * That package — not this repo — owns the vendor SDK, endpoints, and keys.
124
+ * See README "Optional telemetry".
125
+ */
126
+ /** A single structured event — mirrors what `logger.emit` produces. */
127
+ interface TelemetryEvent {
128
+ level: 'info' | 'warn' | 'error';
129
+ phase: string;
130
+ event: string;
131
+ fields: LogFields;
132
+ ctx: LogContext;
133
+ /** ISO-8601 timestamp, same value written to the JSON log line. */
134
+ ts: string;
135
+ }
136
+ /** Sink an external package provides to receive bridge telemetry. */
137
+ interface TelemetryAdapter {
138
+ /** Called for every `log.*` call (info / warn / error). */
139
+ emit(event: TelemetryEvent): void;
140
+ /** Capture an error/exception with its stack. */
141
+ recordError(err: unknown, ctx?: Record<string, unknown>): void;
142
+ /** Record a numeric metric with optional string tags. */
143
+ recordMetric(name: string, value: number, tags?: Record<string, string>): void;
144
+ /** Flush buffered events; `timeoutMs` bounds the wait. Optional. */
145
+ flush?(timeoutMs?: number): Promise<void> | void;
146
+ /** Release resources on shutdown. Optional. */
147
+ close?(): Promise<void> | void;
148
+ }
149
+ /** Runtime metadata handed to the factory when the adapter is loaded. */
150
+ interface AdapterMeta {
151
+ version: string;
152
+ appId?: string;
153
+ tenant?: string;
154
+ /** Host machine identifier (e.g. `os.hostname()`). Useful as a stable
155
+ * `deviceId` for the telemetry sink — survives process restarts. */
156
+ hostname?: string;
157
+ }
158
+ /** The shape an external module must default-export (or expose as `createAdapter`). */
159
+ type AdapterFactory = (meta: AdapterMeta) => TelemetryAdapter;
160
+
161
+ export { type AdapterFactory, type AdapterMeta, type Block, type FooterStatus, type RunState, type TelemetryAdapter, type TelemetryEvent, type Terminal, type ToolEntry, type ToolStatus, finalizeIfRunning, initialState, markInterrupted, reduce, renderCard, renderText, reportError, reportMetric };