Fix paste ownership, input bounds, and IPC validation
Supersedes #5745, #5746, and #5747.
This commit is contained in:
@@ -104,6 +104,27 @@ function uniqueModels(models: CommitMessageModel[]): CommitMessageModel[] {
|
||||
})
|
||||
}
|
||||
|
||||
function* iterateModelOutputLines(output: string): Generator<string> {
|
||||
let lineStart = 0
|
||||
|
||||
for (let index = 0; index < output.length; index++) {
|
||||
const code = output.charCodeAt(index)
|
||||
if (code !== 10 && code !== 13) {
|
||||
continue
|
||||
}
|
||||
|
||||
yield output.slice(lineStart, index)
|
||||
if (code === 13 && output.charCodeAt(index + 1) === 10) {
|
||||
index++
|
||||
}
|
||||
lineStart = index + 1
|
||||
}
|
||||
|
||||
if (lineStart <= output.length) {
|
||||
yield output.slice(lineStart)
|
||||
}
|
||||
}
|
||||
|
||||
function withOpenAiThinking(
|
||||
id: string
|
||||
): Pick<CommitMessageModel, 'thinkingLevels' | 'defaultThinkingLevel'> {
|
||||
@@ -148,74 +169,121 @@ export function parseCodexModels(stdout: string): CommitMessageModel[] {
|
||||
}
|
||||
|
||||
export function parseLineModels(stdout: string): CommitMessageModel[] {
|
||||
return uniqueModels(
|
||||
stdout
|
||||
.split(/\r?\n/)
|
||||
.map((line) => line.trim())
|
||||
.filter((line) => line.length > 0 && !line.includes(' '))
|
||||
.map((id) => ({
|
||||
id,
|
||||
label: labelFromModelId(id),
|
||||
...withOpenAiThinking(id)
|
||||
}))
|
||||
)
|
||||
const models: CommitMessageModel[] = []
|
||||
for (const rawLine of iterateModelOutputLines(stdout)) {
|
||||
const id = rawLine.trim()
|
||||
if (id.length === 0 || id.includes(' ')) {
|
||||
continue
|
||||
}
|
||||
models.push({
|
||||
id,
|
||||
label: labelFromModelId(id),
|
||||
...withOpenAiThinking(id)
|
||||
})
|
||||
}
|
||||
return uniqueModels(models)
|
||||
}
|
||||
|
||||
export function parsePiModels(stdout: string): CommitMessageModel[] {
|
||||
return uniqueModels(
|
||||
stdout
|
||||
.split(/\r?\n/)
|
||||
.map((line) => line.trim().split(/\s+/))
|
||||
.filter((parts) => parts.length >= 6 && parts[0] !== 'provider')
|
||||
.map((parts) => {
|
||||
const [provider, model, , , thinking] = parts
|
||||
const id = `${provider}/${model}`
|
||||
return {
|
||||
id,
|
||||
label: `${labelFromModelId(provider)} ${labelFromModelId(model)}`,
|
||||
...(thinking === 'yes'
|
||||
? {
|
||||
thinkingLevels: [
|
||||
{ id: 'off', label: 'Off' },
|
||||
{ id: 'low', label: 'Low' },
|
||||
{ id: 'medium', label: 'Medium' },
|
||||
{ id: 'high', label: 'High' },
|
||||
{ id: 'xhigh', label: 'Extra High' }
|
||||
],
|
||||
defaultThinkingLevel: 'low'
|
||||
}
|
||||
: {})
|
||||
}
|
||||
})
|
||||
const models: CommitMessageModel[] = []
|
||||
for (const rawLine of iterateModelOutputLines(stdout)) {
|
||||
const parts = getPiModelTableFields(rawLine, 6)
|
||||
if (parts.length < 6 || parts[0] === 'provider') {
|
||||
continue
|
||||
}
|
||||
|
||||
const [provider, model, , , thinking] = parts
|
||||
const id = `${provider}/${model}`
|
||||
models.push({
|
||||
id,
|
||||
label: `${labelFromModelId(provider)} ${labelFromModelId(model)}`,
|
||||
...(thinking === 'yes'
|
||||
? {
|
||||
thinkingLevels: [
|
||||
{ id: 'off', label: 'Off' },
|
||||
{ id: 'low', label: 'Low' },
|
||||
{ id: 'medium', label: 'Medium' },
|
||||
{ id: 'high', label: 'High' },
|
||||
{ id: 'xhigh', label: 'Extra High' }
|
||||
],
|
||||
defaultThinkingLevel: 'low'
|
||||
}
|
||||
: {})
|
||||
})
|
||||
}
|
||||
return uniqueModels(models)
|
||||
}
|
||||
|
||||
// Why: model discovery output can include paste-sized noisy lines; only the first fields matter.
|
||||
function getPiModelTableFields(line: string, maxFields: number): string[] {
|
||||
const fields: string[] = []
|
||||
let tokenStart = -1
|
||||
|
||||
for (let index = 0; index <= line.length; index += 1) {
|
||||
const isEnd = index === line.length
|
||||
if (!isEnd && !isPiModelTableWhitespace(line.charCodeAt(index))) {
|
||||
if (tokenStart === -1) {
|
||||
tokenStart = index
|
||||
}
|
||||
continue
|
||||
}
|
||||
if (tokenStart !== -1) {
|
||||
fields.push(line.slice(tokenStart, index))
|
||||
tokenStart = -1
|
||||
if (fields.length >= maxFields) {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return fields
|
||||
}
|
||||
|
||||
function isPiModelTableWhitespace(code: number): boolean {
|
||||
return (
|
||||
code === 32 ||
|
||||
(code >= 9 && code <= 13) ||
|
||||
code === 160 ||
|
||||
code === 5760 ||
|
||||
(code >= 8192 && code <= 8202) ||
|
||||
code === 8232 ||
|
||||
code === 8233 ||
|
||||
code === 8239 ||
|
||||
code === 8287 ||
|
||||
code === 12288 ||
|
||||
code === 65279
|
||||
)
|
||||
}
|
||||
|
||||
export function parseCursorModels(stdout: string): CommitMessageModel[] {
|
||||
return uniqueModels(
|
||||
stdout
|
||||
.split(/\r?\n/)
|
||||
.map((line) => line.trim())
|
||||
.map((line) => /^([^\s]+)\s+-\s+(.+)$/.exec(line))
|
||||
.filter((match): match is RegExpExecArray => Boolean(match))
|
||||
.map((match) => ({
|
||||
id: match[1],
|
||||
label: match[2].replace(/\s+\((?:default|current)\)$/i, ''),
|
||||
...withOpenAiThinking(match[1])
|
||||
}))
|
||||
)
|
||||
const models: CommitMessageModel[] = []
|
||||
for (const rawLine of iterateModelOutputLines(stdout)) {
|
||||
const match = /^([^\s]+)\s+-\s+(.+)$/.exec(rawLine.trim())
|
||||
if (!match) {
|
||||
continue
|
||||
}
|
||||
models.push({
|
||||
id: match[1],
|
||||
label: match[2].replace(/\s+\((?:default|current)\)$/i, ''),
|
||||
...withOpenAiThinking(match[1])
|
||||
})
|
||||
}
|
||||
return uniqueModels(models)
|
||||
}
|
||||
|
||||
export function parseAntigravityModels(stdout: string): CommitMessageModel[] {
|
||||
return uniqueModels(
|
||||
stdout
|
||||
.split(/\r?\n/)
|
||||
.map((line) => line.trim())
|
||||
.filter((line) => line.length > 0)
|
||||
.map((id) => ({
|
||||
id,
|
||||
label: id
|
||||
}))
|
||||
)
|
||||
const models: CommitMessageModel[] = []
|
||||
for (const rawLine of iterateModelOutputLines(stdout)) {
|
||||
const id = rawLine.trim()
|
||||
if (id.length === 0) {
|
||||
continue
|
||||
}
|
||||
models.push({
|
||||
id,
|
||||
label: id
|
||||
})
|
||||
}
|
||||
return uniqueModels(models)
|
||||
}
|
||||
|
||||
export const COMMIT_MESSAGE_AGENT_SPECS: Partial<Record<TuiAgent, CommitMessageAgentSpec>> = {
|
||||
|
||||
Reference in New Issue
Block a user