persist pasted text in history and align sdk command types
This commit is contained in:
@@ -1306,7 +1306,7 @@ function commandRequiresArguments(template?: string) {
|
|||||||
return /\$(?:\d+|ARGUMENTS)/.test(template)
|
return /\$(?:\d+|ARGUMENTS)/.test(template)
|
||||||
}
|
}
|
||||||
|
|
||||||
function promptForCommandArguments(command: SDKCommand.Info) {
|
function promptForCommandArguments(command: SDKCommand) {
|
||||||
if (!commandRequiresArguments(command.template)) {
|
if (!commandRequiresArguments(command.template)) {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
@@ -1322,7 +1322,7 @@ function formatCommandLabel(name: string) {
|
|||||||
return name.charAt(0).toUpperCase() + name.slice(1)
|
return name.charAt(0).toUpperCase() + name.slice(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
function buildCustomCommandEntries(instanceId: string, commands: SDKCommand.Info[]): Command[] {
|
function buildCustomCommandEntries(instanceId: string, commands: SDKCommand[]): Command[] {
|
||||||
return commands.map((cmd) => ({
|
return commands.map((cmd) => ({
|
||||||
id: `custom:${instanceId}:${cmd.name}`,
|
id: `custom:${instanceId}:${cmd.name}`,
|
||||||
label: formatCommandLabel(cmd.name),
|
label: formatCommandLabel(cmd.name),
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ import { createSignal, Show, onMount, For, onCleanup, createEffect, on, untrack
|
|||||||
import UnifiedPicker from "./unified-picker"
|
import UnifiedPicker from "./unified-picker"
|
||||||
import { addToHistory, getHistory } from "../stores/message-history"
|
import { addToHistory, getHistory } from "../stores/message-history"
|
||||||
import { getAttachments, addAttachment, clearAttachments, removeAttachment } from "../stores/attachments"
|
import { getAttachments, addAttachment, clearAttachments, removeAttachment } from "../stores/attachments"
|
||||||
|
import { resolvePastedPlaceholders } from "../lib/prompt-placeholders"
|
||||||
import { createFileAttachment, createTextAttachment, createAgentAttachment } from "../types/attachment"
|
import { createFileAttachment, createTextAttachment, createAgentAttachment } from "../types/attachment"
|
||||||
import type { Attachment } from "../types/attachment"
|
import type { Attachment } from "../types/attachment"
|
||||||
import type { Agent } from "../types/session"
|
import type { Agent } from "../types/session"
|
||||||
@@ -465,6 +466,8 @@ export default function PromptInput(props: PromptInputProps) {
|
|||||||
const currentAttachments = attachments()
|
const currentAttachments = attachments()
|
||||||
if (!text || props.disabled) return
|
if (!text || props.disabled) return
|
||||||
|
|
||||||
|
const resolvedPrompt = resolvePastedPlaceholders(text, currentAttachments)
|
||||||
|
|
||||||
clearPrompt()
|
clearPrompt()
|
||||||
clearAttachments(props.instanceId, props.sessionId)
|
clearAttachments(props.instanceId, props.sessionId)
|
||||||
setIgnoredAtPositions(new Set<number>())
|
setIgnoredAtPositions(new Set<number>())
|
||||||
@@ -473,7 +476,7 @@ export default function PromptInput(props: PromptInputProps) {
|
|||||||
setHistoryDraft(null)
|
setHistoryDraft(null)
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await addToHistory(props.instanceFolder, text)
|
await addToHistory(props.instanceFolder, resolvedPrompt)
|
||||||
const updated = await getHistory(props.instanceFolder)
|
const updated = await getHistory(props.instanceFolder)
|
||||||
setHistory(updated)
|
setHistory(updated)
|
||||||
setHistoryIndex(-1)
|
setHistoryIndex(-1)
|
||||||
|
|||||||
36
src/lib/prompt-placeholders.ts
Normal file
36
src/lib/prompt-placeholders.ts
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
import type { Attachment } from "../types/attachment"
|
||||||
|
|
||||||
|
export function resolvePastedPlaceholders(prompt: string, attachments: Attachment[] = []): string {
|
||||||
|
if (!prompt || !prompt.includes("[pasted #")) {
|
||||||
|
return prompt
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!attachments || attachments.length === 0) {
|
||||||
|
return prompt
|
||||||
|
}
|
||||||
|
|
||||||
|
const lookup = new Map<string, string>()
|
||||||
|
|
||||||
|
for (const attachment of attachments) {
|
||||||
|
const source = attachment?.source
|
||||||
|
if (!source || source.type !== "text") continue
|
||||||
|
const display = attachment?.display
|
||||||
|
const value = source.value
|
||||||
|
if (typeof display !== "string" || typeof value !== "string") continue
|
||||||
|
const match = display.match(/pasted #(\d+)/)
|
||||||
|
if (!match) continue
|
||||||
|
const placeholder = `[pasted #${match[1]}]`
|
||||||
|
if (!lookup.has(placeholder)) {
|
||||||
|
lookup.set(placeholder, value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (lookup.size === 0) {
|
||||||
|
return prompt
|
||||||
|
}
|
||||||
|
|
||||||
|
return prompt.replace(/\[pasted #(\d+)\]/g, (fullMatch) => {
|
||||||
|
const replacement = lookup.get(fullMatch)
|
||||||
|
return typeof replacement === "string" ? replacement : fullMatch
|
||||||
|
})
|
||||||
|
}
|
||||||
@@ -2,7 +2,7 @@ import { createSignal } from "solid-js"
|
|||||||
import type { Command as SDKCommand } from "@opencode-ai/sdk"
|
import type { Command as SDKCommand } from "@opencode-ai/sdk"
|
||||||
import type { OpencodeClient } from "@opencode-ai/sdk/client"
|
import type { OpencodeClient } from "@opencode-ai/sdk/client"
|
||||||
|
|
||||||
const [commandMap, setCommandMap] = createSignal<Map<string, SDKCommand.Info[]>>(new Map())
|
const [commandMap, setCommandMap] = createSignal<Map<string, SDKCommand[]>>(new Map())
|
||||||
|
|
||||||
export async function fetchCommands(instanceId: string, client: OpencodeClient): Promise<void> {
|
export async function fetchCommands(instanceId: string, client: OpencodeClient): Promise<void> {
|
||||||
const response = await client.command.list()
|
const response = await client.command.list()
|
||||||
@@ -14,7 +14,7 @@ export async function fetchCommands(instanceId: string, client: OpencodeClient):
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getCommands(instanceId: string): SDKCommand.Info[] {
|
export function getCommands(instanceId: string): SDKCommand[] {
|
||||||
return commandMap().get(instanceId) ?? []
|
return commandMap().get(instanceId) ?? []
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
import type { Message } from "../types/message"
|
import type { Message } from "../types/message"
|
||||||
|
|
||||||
|
import { resolvePastedPlaceholders } from "../lib/prompt-placeholders"
|
||||||
import { instances } from "./instances"
|
import { instances } from "./instances"
|
||||||
|
|
||||||
import {
|
import {
|
||||||
addRecentModelPreference,
|
addRecentModelPreference,
|
||||||
preferences,
|
preferences,
|
||||||
@@ -62,41 +64,6 @@ function createId(prefix: string): string {
|
|||||||
return `${prefix}_${hex}${random}`
|
return `${prefix}_${hex}${random}`
|
||||||
}
|
}
|
||||||
|
|
||||||
function resolvePastedPlaceholders(prompt: string, attachments: any[] = []): string {
|
|
||||||
if (!prompt || !prompt.includes("[pasted #")) {
|
|
||||||
return prompt
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!attachments || attachments.length === 0) {
|
|
||||||
return prompt
|
|
||||||
}
|
|
||||||
|
|
||||||
const lookup = new Map<string, string>()
|
|
||||||
|
|
||||||
for (const attachment of attachments) {
|
|
||||||
const source = attachment?.source
|
|
||||||
if (!source || source.type !== "text") continue
|
|
||||||
const display: string | undefined = attachment?.display
|
|
||||||
const value: unknown = source.value
|
|
||||||
if (typeof display !== "string" || typeof value !== "string") continue
|
|
||||||
const match = display.match(/pasted #(\d+)/)
|
|
||||||
if (!match) continue
|
|
||||||
const placeholder = `[pasted #${match[1]}]`
|
|
||||||
if (!lookup.has(placeholder)) {
|
|
||||||
lookup.set(placeholder, value)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (lookup.size === 0) {
|
|
||||||
return prompt
|
|
||||||
}
|
|
||||||
|
|
||||||
return prompt.replace(/\[pasted #(\d+)\]/g, (fullMatch) => {
|
|
||||||
const replacement = lookup.get(fullMatch)
|
|
||||||
return typeof replacement === "string" ? replacement : fullMatch
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
async function sendMessage(
|
async function sendMessage(
|
||||||
instanceId: string,
|
instanceId: string,
|
||||||
sessionId: string,
|
sessionId: string,
|
||||||
|
|||||||
Reference in New Issue
Block a user