mirror of
https://github.com/claude-code-best/claude-code.git
synced 2026-06-23 00:35:51 +00:00
style: 完成所有文件的lint
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import { randomUUID } from "node:crypto";
|
||||
import { getEventBus } from "../transport/event-bus";
|
||||
import { randomUUID } from 'node:crypto'
|
||||
import { getEventBus } from '../transport/event-bus'
|
||||
|
||||
/**
|
||||
* Extract plain text from various message payload formats.
|
||||
@@ -9,75 +9,87 @@ import { getEventBus } from "../transport/event-bus";
|
||||
* { message: { content: [{type:"text",text:"..."}] } }
|
||||
*/
|
||||
function extractContent(payload: unknown): string {
|
||||
if (!payload || typeof payload !== "object") {
|
||||
return typeof payload === "string" ? payload : "";
|
||||
if (!payload || typeof payload !== 'object') {
|
||||
return typeof payload === 'string' ? payload : ''
|
||||
}
|
||||
|
||||
const p = payload as Record<string, unknown>;
|
||||
const p = payload as Record<string, unknown>
|
||||
|
||||
// Direct content field
|
||||
if (typeof p.content === "string" && p.content) return p.content;
|
||||
if (typeof p.content === 'string' && p.content) return p.content
|
||||
|
||||
// message.content (child process format)
|
||||
const msg = p.message;
|
||||
if (msg && typeof msg === "object") {
|
||||
const mc = (msg as Record<string, unknown>).content;
|
||||
if (typeof mc === "string") return mc;
|
||||
const msg = p.message
|
||||
if (msg && typeof msg === 'object') {
|
||||
const mc = (msg as Record<string, unknown>).content
|
||||
if (typeof mc === 'string') return mc
|
||||
if (Array.isArray(mc)) {
|
||||
return mc
|
||||
.filter((b: unknown) => typeof b === "object" && b !== null && (b as Record<string, unknown>).type === "text")
|
||||
.map((b: Record<string, unknown>) => (b as Record<string, unknown>).text || "")
|
||||
.join("");
|
||||
.filter(
|
||||
(b: unknown) =>
|
||||
typeof b === 'object' &&
|
||||
b !== null &&
|
||||
(b as Record<string, unknown>).type === 'text',
|
||||
)
|
||||
.map(
|
||||
(b: Record<string, unknown>) =>
|
||||
(b as Record<string, unknown>).text || '',
|
||||
)
|
||||
.join('')
|
||||
}
|
||||
}
|
||||
|
||||
return "";
|
||||
return ''
|
||||
}
|
||||
|
||||
/**
|
||||
* Normalize event payload into a flat structure with guaranteed `content` string.
|
||||
* Preserves original payload in `raw` field and keeps tool-specific fields.
|
||||
*/
|
||||
export function normalizePayload(type: string, payload: unknown): Record<string, unknown> {
|
||||
if (!payload || typeof payload !== "object") {
|
||||
return { content: typeof payload === "string" ? payload : "", raw: payload };
|
||||
export function normalizePayload(
|
||||
type: string,
|
||||
payload: unknown,
|
||||
): Record<string, unknown> {
|
||||
if (!payload || typeof payload !== 'object') {
|
||||
return { content: typeof payload === 'string' ? payload : '', raw: payload }
|
||||
}
|
||||
|
||||
const p = payload as Record<string, unknown>;
|
||||
const content = extractContent(payload);
|
||||
const p = payload as Record<string, unknown>
|
||||
const content = extractContent(payload)
|
||||
|
||||
const normalized: Record<string, unknown> = {
|
||||
content,
|
||||
raw: payload,
|
||||
};
|
||||
|
||||
if (typeof p.uuid === "string" && p.uuid) normalized.uuid = p.uuid;
|
||||
if (typeof p.isSynthetic === "boolean") normalized.isSynthetic = p.isSynthetic;
|
||||
if (typeof p.status === "string") normalized.status = p.status;
|
||||
if (typeof p.subtype === "string") normalized.subtype = p.subtype;
|
||||
|
||||
// Preserve tool fields
|
||||
if (p.tool_name) normalized.tool_name = p.tool_name;
|
||||
if (p.name) normalized.tool_name = p.name;
|
||||
if (p.tool_input) normalized.tool_input = p.tool_input;
|
||||
if (p.input) normalized.tool_input = p.input;
|
||||
|
||||
// Preserve permission fields
|
||||
if (p.request_id) normalized.request_id = p.request_id;
|
||||
if (p.request) normalized.request = p.request;
|
||||
if (p.approved !== undefined) normalized.approved = p.approved;
|
||||
if (p.updated_input) normalized.updated_input = p.updated_input;
|
||||
|
||||
// Preserve message field for backward compat
|
||||
if (p.message) normalized.message = p.message;
|
||||
|
||||
if (type === "task_state") {
|
||||
if (typeof p.task_list_id === "string") normalized.task_list_id = p.task_list_id;
|
||||
if (typeof p.taskListId === "string") normalized.taskListId = p.taskListId;
|
||||
if (Array.isArray(p.tasks)) normalized.tasks = p.tasks;
|
||||
}
|
||||
|
||||
return normalized;
|
||||
if (typeof p.uuid === 'string' && p.uuid) normalized.uuid = p.uuid
|
||||
if (typeof p.isSynthetic === 'boolean') normalized.isSynthetic = p.isSynthetic
|
||||
if (typeof p.status === 'string') normalized.status = p.status
|
||||
if (typeof p.subtype === 'string') normalized.subtype = p.subtype
|
||||
|
||||
// Preserve tool fields
|
||||
if (p.tool_name) normalized.tool_name = p.tool_name
|
||||
if (p.name) normalized.tool_name = p.name
|
||||
if (p.tool_input) normalized.tool_input = p.tool_input
|
||||
if (p.input) normalized.tool_input = p.input
|
||||
|
||||
// Preserve permission fields
|
||||
if (p.request_id) normalized.request_id = p.request_id
|
||||
if (p.request) normalized.request = p.request
|
||||
if (p.approved !== undefined) normalized.approved = p.approved
|
||||
if (p.updated_input) normalized.updated_input = p.updated_input
|
||||
|
||||
// Preserve message field for backward compat
|
||||
if (p.message) normalized.message = p.message
|
||||
|
||||
if (type === 'task_state') {
|
||||
if (typeof p.task_list_id === 'string')
|
||||
normalized.task_list_id = p.task_list_id
|
||||
if (typeof p.taskListId === 'string') normalized.taskListId = p.taskListId
|
||||
if (Array.isArray(p.tasks)) normalized.tasks = p.tasks
|
||||
}
|
||||
|
||||
return normalized
|
||||
}
|
||||
|
||||
/** Publish an event to a session's bus (in-memory only) */
|
||||
@@ -85,12 +97,12 @@ export function publishSessionEvent(
|
||||
sessionId: string,
|
||||
type: string,
|
||||
payload: unknown,
|
||||
direction: "inbound" | "outbound",
|
||||
direction: 'inbound' | 'outbound',
|
||||
) {
|
||||
const bus = getEventBus(sessionId);
|
||||
const eventId = randomUUID();
|
||||
const bus = getEventBus(sessionId)
|
||||
const eventId = randomUUID()
|
||||
|
||||
const normalized = normalizePayload(type, payload);
|
||||
const normalized = normalizePayload(type, payload)
|
||||
|
||||
const event = bus.publish({
|
||||
id: eventId,
|
||||
@@ -98,7 +110,7 @@ export function publishSessionEvent(
|
||||
type,
|
||||
payload: normalized,
|
||||
direction,
|
||||
});
|
||||
})
|
||||
|
||||
return event;
|
||||
return event
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user