-
-
Notifications
You must be signed in to change notification settings - Fork 120
Expand file tree
/
Copy pathopenai-responses.ts
More file actions
102 lines (85 loc) · 3.44 KB
/
openai-responses.ts
File metadata and controls
102 lines (85 loc) · 3.44 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
import type { FormatDescriptor, ToolOutput } from "../types"
import type { PluginState } from "../../state"
function isNudgeItem(item: any, nudgeText: string): boolean {
if (typeof item.content === 'string') {
return item.content === nudgeText
}
return false
}
function injectSynth(input: any[], instruction: string, nudgeText: string, systemReminder: string): boolean {
const fullInstruction = systemReminder + '\n\n' + instruction
for (let i = input.length - 1; i >= 0; i--) {
const item = input[i]
if (item.type === 'message' && item.role === 'user') {
if (isNudgeItem(item, nudgeText)) continue
if (typeof item.content === 'string') {
if (item.content.includes(instruction)) return false
item.content = item.content + '\n\n' + fullInstruction
} else if (Array.isArray(item.content)) {
const alreadyInjected = item.content.some(
(part: any) => part?.type === 'input_text' && typeof part.text === 'string' && part.text.includes(instruction)
)
if (alreadyInjected) return false
item.content.push({ type: 'input_text', text: fullInstruction })
}
return true
}
}
return false
}
function injectPrunableList(input: any[], injection: string): boolean {
if (!injection) return false
input.push({ type: 'message', role: 'user', content: injection })
return true
}
export const openaiResponsesFormat: FormatDescriptor = {
name: 'openai-responses',
detect(body: any): boolean {
return body.input && Array.isArray(body.input)
},
getDataArray(body: any): any[] | undefined {
return body.input
},
injectSynth(data: any[], instruction: string, nudgeText: string, systemReminder: string): boolean {
return injectSynth(data, instruction, nudgeText, systemReminder)
},
injectPrunableList(data: any[], injection: string): boolean {
return injectPrunableList(data, injection)
},
extractToolOutputs(data: any[], state: PluginState): ToolOutput[] {
const outputs: ToolOutput[] = []
for (const item of data) {
if (item.type === 'function_call_output' && item.call_id) {
const metadata = state.toolParameters.get(item.call_id.toLowerCase())
outputs.push({
id: item.call_id.toLowerCase(),
toolName: metadata?.tool ?? item.name
})
}
}
return outputs
},
replaceToolOutput(data: any[], toolId: string, prunedMessage: string, _state: PluginState): boolean {
const toolIdLower = toolId.toLowerCase()
let replaced = false
for (let i = 0; i < data.length; i++) {
const item = data[i]
if (item.type === 'function_call_output' && item.call_id?.toLowerCase() === toolIdLower) {
data[i] = { ...item, output: prunedMessage }
replaced = true
}
}
return replaced
},
hasToolOutputs(data: any[]): boolean {
return data.some((item: any) => item.type === 'function_call_output')
},
getLogMetadata(data: any[], replacedCount: number, inputUrl: string): Record<string, any> {
return {
url: inputUrl,
replacedCount,
totalItems: data.length,
format: 'openai-responses-api'
}
}
}