| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390 |
- import { mkdir, readFile, writeFile } from "node:fs/promises";
- import { basename, join } from "node:path";
- import {
- createAnthropicRequestBody,
- deriveTrajectoriesFromEntries,
- type DerivedTrajectoryResult,
- } from "./derive.js";
- import { isAppendOnly, isTrajectoryMessageEqual } from "./message-continuity.js";
- import type { JsonObject, OpenAiMessage, TrajectoryJsonlEntry } from "./types.js";
- const DEFAULT_QUERY_SOURCE = "main_turn";
- const SESSION_TITLE_PROMPT_PREFIX = "Generate a concise title for this coding session.";
- export interface ModelIoJsonlEntry extends JsonObject {
- querySource?: string;
- request?: ModelIoRequest;
- response?: ModelIoResponse;
- type?: string;
- }
- interface ModelIoRequest extends JsonObject {
- body?: JsonObject;
- bodyMessageOffset?: number;
- bodyMessagesKind?: string;
- messageOffset?: number;
- messages?: unknown[];
- messagesKind?: string;
- sdkMessageOffset?: number;
- sdkMessages?: unknown[];
- sdkMessagesKind?: string;
- }
- interface ModelIoResponse extends JsonObject {
- text?: string;
- toolCalls?: unknown[];
- }
- interface ModelIoToolCall {
- id?: string;
- input?: unknown;
- name?: string;
- }
- export interface ModelIoTrajectoryOptions {
- querySource?: string;
- }
- export async function writeModelIoAnthropicTrajectory(input: {
- inputPath: string;
- outDir: string;
- querySource?: string;
- }): Promise<DerivedTrajectoryResult> {
- const result = deriveTrajectoriesFromModelIoEntries(await readModelIoJsonl(input.inputPath), {
- querySource: input.querySource,
- });
- if (result.trajectories.length === 0) {
- throw new Error(`No model-io records matched querySource ${input.querySource ?? DEFAULT_QUERY_SOURCE}`);
- }
- const trajectoriesDir = join(input.outDir, "trajectories");
- await mkdir(trajectoriesDir, { recursive: true });
- for (const trajectory of result.trajectories) {
- await writeFile(
- join(trajectoriesDir, `${trajectory.id}.openai_request_body.json`),
- `${JSON.stringify(trajectory.requestBody, null, 2)}\n`,
- "utf8",
- );
- await writeFile(
- join(trajectoriesDir, `${trajectory.id}.anthropic_request_body.json`),
- `${JSON.stringify(createAnthropicRequestBody(trajectory), null, 2)}\n`,
- "utf8",
- );
- }
- await writeFile(
- join(input.outDir, "anthropic_trajectory.json"),
- `${JSON.stringify(createAnthropicRequestBody(result.trajectories[0]!), null, 2)}\n`,
- "utf8",
- );
- await writeFile(
- join(input.outDir, "manifest.json"),
- `${JSON.stringify(result.manifest, null, 2)}\n`,
- "utf8",
- );
- return result;
- }
- export function deriveTrajectoriesFromModelIoEntries(
- entries: readonly ModelIoJsonlEntry[],
- options: ModelIoTrajectoryOptions = {},
- ): DerivedTrajectoryResult {
- const querySource = options.querySource ?? DEFAULT_QUERY_SOURCE;
- const expandedRecords = expandModelIoRecords(
- entries.filter((entry) => entry.type === undefined || entry.type === "model_io"),
- );
- const trajectoryEntries = modelIoRecordsToTrajectoryEntries(
- expandedRecords.filter((record) => modelIoQuerySource(record) === querySource),
- );
- return deriveTrajectoriesFromEntries(trajectoryEntries);
- }
- async function readModelIoJsonl(inputPath: string): Promise<ModelIoJsonlEntry[]> {
- const content = await readFile(inputPath, "utf8");
- return content
- .split(/\r?\n/u)
- .filter((line) => line.trim().length > 0)
- .map((line, index) => {
- try {
- return JSON.parse(line) as ModelIoJsonlEntry;
- } catch (error) {
- throw new Error(`Invalid model-io JSONL at ${basename(inputPath)}:${index + 1}`, {
- cause: error,
- });
- }
- });
- }
- function expandModelIoRecords(entries: readonly ModelIoJsonlEntry[]): ModelIoJsonlEntry[] {
- const expanded: ModelIoJsonlEntry[] = [];
- let previous: ModelIoJsonlEntry | undefined;
- for (const entry of entries) {
- const next = expandModelIoRecord(entry, previous);
- expanded.push(next);
- previous = next;
- }
- return expanded;
- }
- function expandModelIoRecord(
- entry: ModelIoJsonlEntry,
- previousEntry?: ModelIoJsonlEntry,
- ): ModelIoJsonlEntry {
- const request = asRecord(entry.request);
- if (!request) {
- return cloneJsonObject(entry) as ModelIoJsonlEntry;
- }
- return {
- ...cloneJsonObject(entry),
- request: expandModelIoRequest(request, asRecord(previousEntry?.request)),
- };
- }
- function expandModelIoRequest(
- request: Record<string, unknown>,
- previousRequest?: Record<string, unknown>,
- ): ModelIoRequest {
- const next = { ...cloneJsonObject(request) } as ModelIoRequest;
- expandMessageCollection(next, previousRequest, {
- collectionKey: "messages",
- kindKey: "messagesKind",
- offsetKey: "messageOffset",
- });
- expandMessageCollection(next, previousRequest, {
- collectionKey: "sdkMessages",
- kindKey: "sdkMessagesKind",
- offsetKey: "sdkMessageOffset",
- });
- const body = asRecord(next.body);
- if (body) {
- const nextBody = { ...cloneJsonObject(body) } as JsonObject;
- expandMessageCollection(
- nextBody,
- asRecord(asRecord(previousRequest?.body)),
- {
- collectionKey: "messages",
- kindKey: "bodyMessagesKind",
- offsetKey: "bodyMessageOffset",
- },
- next,
- );
- next.body = nextBody;
- }
- return next;
- }
- function expandMessageCollection(
- target: JsonObject,
- previous: Record<string, unknown> | undefined,
- keys: {
- collectionKey: string;
- kindKey: string;
- offsetKey: string;
- },
- metadataSource: JsonObject = target,
- ): void {
- if (metadataSource[keys.kindKey] !== "delta") {
- return;
- }
- const deltaMessages = target[keys.collectionKey];
- const previousMessages = previous?.[keys.collectionKey];
- const offset = asNonNegativeInteger(metadataSource[keys.offsetKey]);
- if (!Array.isArray(deltaMessages) || !Array.isArray(previousMessages) || offset === undefined) {
- return;
- }
- target[keys.collectionKey] = [
- ...cloneArray(previousMessages.slice(0, offset)),
- ...cloneArray(deltaMessages),
- ];
- }
- function modelIoRecordsToTrajectoryEntries(
- records: readonly ModelIoJsonlEntry[],
- ): TrajectoryJsonlEntry[] {
- const entries: TrajectoryJsonlEntry[] = [];
- let requestIndex = 0;
- const requests = records.flatMap((record) => {
- const body = asRecord(record.request?.body);
- return body ? [{ body, response: record.response }] : [];
- });
- for (const [recordIndex, { body, response }] of requests.entries()) {
- const messages = body.messages;
- if (!Array.isArray(messages)) {
- throw new Error("model-io request.body must contain a messages array.");
- }
- requestIndex += 1;
- const { messages: _messages, ...bodyWithoutMessages } = cloneJsonObject(body);
- entries.push({
- bodyWithoutMessages,
- kind: "request",
- requestIndex,
- });
- for (let messageIndex = 0; messageIndex < messages.length; messageIndex += 1) {
- const message = messages[messageIndex];
- if (!isOpenAiMessage(message)) {
- throw new Error(`model-io request message ${messageIndex} is missing role.`);
- }
- entries.push({
- kind: "message",
- message: cloneJsonObject(message) as OpenAiMessage,
- messageIndex,
- requestIndex,
- });
- }
- const assistantMessage = assistantMessageFromModelIoResponse(response);
- if (assistantMessage) {
- entries.push({
- kind: "message",
- message: completeAssistantMessage(
- assistantMessage,
- messages as OpenAiMessage[],
- requests[recordIndex + 1]?.body.messages,
- ),
- requestIndex,
- });
- }
- }
- return entries;
- }
- function completeAssistantMessage(
- summary: OpenAiMessage,
- previous: readonly OpenAiMessage[],
- next: unknown,
- ): OpenAiMessage {
- if (!Array.isArray(next) || !next.every(isOpenAiMessage) || !isAppendOnly(previous, next)) {
- return summary;
- }
- const recorded = next[previous.length];
- if (recorded?.role !== "assistant" || !Array.isArray(recorded.content)) return summary;
- // response 摘要没有完整 thinking/签名,直接比较会误分段。
- // 只从前缀连续且正文/工具完全匹配的下一请求补全;已有历史的 thinking 仍严格比较。
- const comparable = {
- ...recorded,
- content: recorded.content.filter((block: unknown) => {
- const type = asRecord(block)?.type;
- return type !== "thinking" && type !== "redacted_thinking";
- }),
- };
- return isTrajectoryMessageEqual(summary, comparable)
- ? (cloneJsonObject(recorded) as OpenAiMessage)
- : summary;
- }
- function assistantMessageFromModelIoResponse(
- response: ModelIoResponse | undefined,
- ): OpenAiMessage | undefined {
- if (!response) {
- return undefined;
- }
- const content: unknown[] = [];
- if (typeof response.text === "string" && response.text.length > 0) {
- content.push({ type: "text", text: response.text });
- }
- for (const toolCall of normalizedToolCalls(response.toolCalls)) {
- content.push({
- type: "tool_use",
- id: toolCall.id,
- name: toolCall.name,
- input: cloneJsonValue(toolCall.input ?? {}),
- });
- }
- return content.length > 0 ? { role: "assistant", content } : undefined;
- }
- function normalizedToolCalls(value: unknown[] | undefined): ModelIoToolCall[] {
- if (!Array.isArray(value)) {
- return [];
- }
- return value.flatMap((raw) => {
- const toolCall = asRecord(raw);
- if (!toolCall) {
- return [];
- }
- const id = typeof toolCall?.id === "string" ? toolCall.id : undefined;
- const name = typeof toolCall?.name === "string" ? toolCall.name : undefined;
- if (!id || !name) {
- return [];
- }
- return [{ id, input: toolCall.input, name }];
- });
- }
- function modelIoQuerySource(record: ModelIoJsonlEntry): string {
- if (typeof record.querySource === "string") {
- return record.querySource;
- }
- const messages = asRecord(record.request?.body)?.messages;
- const firstMessage = Array.isArray(messages) ? asRecord(messages[0]) : undefined;
- if (firstMessage?.role === "system" || firstMessage?.role === "user") {
- const text = messageText(firstMessage.content);
- if (text.startsWith(SESSION_TITLE_PROMPT_PREFIX)) {
- return "session_title";
- }
- }
- return DEFAULT_QUERY_SOURCE;
- }
- function messageText(value: unknown): string {
- if (typeof value === "string") {
- return value;
- }
- if (!value || typeof value !== "object") {
- return "";
- }
- if (Array.isArray(value)) {
- return value.map(messageText).join("\n");
- }
- const record = value as Record<string, unknown>;
- return [record.text, record.content].map(messageText).join("\n");
- }
- function isOpenAiMessage(value: unknown): value is OpenAiMessage {
- return Boolean(
- value &&
- typeof value === "object" &&
- !Array.isArray(value) &&
- typeof (value as { role?: unknown }).role === "string",
- );
- }
- function asRecord(value: unknown): Record<string, unknown> | undefined {
- return typeof value === "object" && value !== null && !Array.isArray(value)
- ? (value as Record<string, unknown>)
- : undefined;
- }
- function asNonNegativeInteger(value: unknown): number | undefined {
- return typeof value === "number" && Number.isInteger(value) && value >= 0 ? value : undefined;
- }
- function cloneArray(value: readonly unknown[]): unknown[] {
- return JSON.parse(JSON.stringify(value)) as unknown[];
- }
- function cloneJsonObject(value: Record<string, unknown>): JsonObject {
- return JSON.parse(JSON.stringify(value)) as JsonObject;
- }
- function cloneJsonValue(value: unknown): unknown {
- return JSON.parse(JSON.stringify(value));
- }
|