-
Notifications
You must be signed in to change notification settings - Fork 298
Expand file tree
/
Copy pathparser.ts
More file actions
114 lines (97 loc) · 3.49 KB
/
parser.ts
File metadata and controls
114 lines (97 loc) · 3.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import type { Logger } from '../client';
import { parseAccumulatedFormat } from './core-parser';
import {
ContentBlock,
JSONOutputFormat,
Message,
OutputConfig,
TextBlock,
MessageCreateParams,
} from '../resources/messages/messages';
import { Simplify } from './type-utils';
type AutoParseableOutputConfig = Omit<OutputConfig, 'format'> & {
format?: JSONOutputFormat | AutoParseableOutputFormat<any> | null;
};
export type ParseableMessageCreateParams = Simplify<
Omit<MessageCreateParams, 'output_config'> & {
output_config?: AutoParseableOutputConfig | null;
}
>;
export type ExtractParsedContentFromParams<Params extends ParseableMessageCreateParams> =
Params['output_config'] extends { format: AutoParseableOutputFormat<infer P> } ? P : null;
export type AutoParseableOutputFormat<ParsedT> = JSONOutputFormat & {
parse(content: string): ParsedT;
};
export type ParsedMessage<ParsedT> = Message & {
content: Array<ParsedContentBlock<ParsedT>>;
parsed_output: ParsedT | null;
};
export type ParsedContentBlock<ParsedT> =
| (TextBlock & { parsed_output: ParsedT | null })
| Exclude<ContentBlock, TextBlock>;
function getOutputFormat(
params: ParseableMessageCreateParams | null,
): JSONOutputFormat | AutoParseableOutputFormat<any> | null | undefined {
return params?.output_config?.format;
}
export function maybeParseMessage<Params extends ParseableMessageCreateParams | null>(
message: Message,
params: Params,
opts: { logger: Logger },
): ParsedMessage<ExtractParsedContentFromParams<NonNullable<Params>>> {
const outputFormat = getOutputFormat(params);
if (!params || !('parse' in (outputFormat ?? {}))) {
return {
...message,
content: message.content.map((block) => {
if (block.type === 'text') {
const parsedBlock = Object.defineProperty({ ...block }, 'parsed_output', {
value: null,
enumerable: false,
}) as ParsedContentBlock<ExtractParsedContentFromParams<NonNullable<Params>>>;
return parsedBlock;
}
return block;
}),
parsed_output: null,
} as ParsedMessage<ExtractParsedContentFromParams<NonNullable<Params>>>;
}
return parseMessage(message, params, opts);
}
export function parseMessage<Params extends ParseableMessageCreateParams>(
message: Message,
params: Params,
opts: { logger: Logger },
): ParsedMessage<ExtractParsedContentFromParams<Params>> {
let firstParsedOutput: ReturnType<typeof parseOutputFormat<Params>> | null = null;
const content: Array<ParsedContentBlock<ExtractParsedContentFromParams<Params>>> = message.content.map(
(block) => {
if (block.type === 'text') {
const parsedOutput = parseOutputFormat(params, block.text);
if (firstParsedOutput === null) {
firstParsedOutput = parsedOutput;
}
const parsedBlock = Object.defineProperty({ ...block }, 'parsed_output', {
value: parsedOutput,
enumerable: false,
}) as ParsedContentBlock<ExtractParsedContentFromParams<Params>>;
return parsedBlock;
}
return block;
},
);
return {
...message,
content,
parsed_output: firstParsedOutput,
} as ParsedMessage<ExtractParsedContentFromParams<Params>>;
}
function parseOutputFormat<Params extends ParseableMessageCreateParams>(
params: Params,
content: string,
): ExtractParsedContentFromParams<Params> | null {
return parseAccumulatedFormat(
getOutputFormat(params),
content,
) as ExtractParsedContentFromParams<Params> | null;
}