Skip to content

Commit

Permalink
add option to open view in vscode
Browse files Browse the repository at this point in the history
  • Loading branch information
pelikhan committed Jan 14, 2025
1 parent 1acf870 commit b1a22b9
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 21 deletions.
43 changes: 31 additions & 12 deletions packages/cli/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ export async function startServer(options: {
const wss = new WebSocketServer({ noServer: true })

// Stores active script runs with their cancellation controllers and traces.
let lastRunResult: PromptScriptEndResponseEvent & {
trace: string
} = undefined
const runs: Record<
string,
{
Expand Down Expand Up @@ -284,25 +287,38 @@ export async function startServer(options: {
}

// send traces of in-flight runs
for (const [runId, run] of Object.entries(runs)) {
chunkString(run.outputTrace.content).forEach((c) =>
const activeRuns = Object.entries(runs)
if (activeRuns.length) {
for (const [runId, run] of activeRuns) {
ws.send(
JSON.stringify({
type: "script.progress",
runId,
trace: c,
output: run.outputTrace.content,
} satisfies PromptScriptProgressResponseEvent)
)
)
chunkString(run.trace.content).forEach((c) =>
chunkString(run.trace.content).forEach((c) =>
ws.send(
JSON.stringify({
type: "script.progress",
runId,
trace: c,
} satisfies PromptScriptProgressResponseEvent)
)
)
}
} else if (lastRunResult) {
const { trace, ...restResult } = lastRunResult
chunkString(trace).forEach((c) =>
ws.send(
JSON.stringify({
type: "script.progress",
runId,
runId: lastRunResult.runId,
trace: c,
} satisfies PromptScriptProgressResponseEvent)
)
)
ws.send(JSON.stringify(restResult))
}

// Handle incoming messages based on their type.
Expand Down Expand Up @@ -420,12 +436,15 @@ export async function startServer(options: {
logVerbose(
`\nrun ${runId}: completed with ${exitCode}`
)
send({
type: "script.end",
runId,
exitCode,
result,
} satisfies PromptScriptEndResponseEvent)
send(
(lastRunResult = {
type: "script.end",
runId,
exitCode,
result,
trace: trace.content,
})
)
})
.catch((e) => {
if (canceller.controller.signal.aborted) return
Expand Down
4 changes: 2 additions & 2 deletions packages/sample/genaiscript.config.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
modelAliases:
large: google:gemini-exp-1206
vision: google:gemini-exp-1206
gemini: google:gemini-exp-1206
video: google:gemini-exp-1206
5 changes: 5 additions & 0 deletions packages/vscode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,11 @@
"title": "Open request or response",
"category": "GenAIScript"
},
{
"command": "genaiscript.request.open.view",
"title": "View request",
"category": "GenAIScript"
},
{
"command": "genaiscript.request.open.trace",
"title": "Open Trace",
Expand Down
2 changes: 2 additions & 0 deletions packages/vscode/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { activateConnectionInfoTree } from "./connectioninfotree"
import { activeTaskProvider } from "./taskprovider"
import { activateSamplesCommands } from "./samplescommands"
import { activateChatParticipant } from "./chatparticipant"
import { activeWebview } from "./webview"

export async function activate(context: ExtensionContext) {
const state = new ExtensionState(context)
Expand All @@ -39,6 +40,7 @@ export async function activate(context: ExtensionContext) {
activateDocsNotebook(state)
activeTaskProvider(state)
activateChatParticipant(state)
activeWebview(state)

context.subscriptions.push(
registerCommand("genaiscript.request.abort", async () => {
Expand Down
2 changes: 2 additions & 0 deletions packages/vscode/src/fragmentcommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,8 @@ export function activateFragmentCommands(state: ExtensionState) {
)
}



subscriptions.push(
registerCommand("genaiscript.fragment.prompt", fragmentPrompt),
registerCommand("genaiscript.fragment.debug", fragmentDebug)
Expand Down
2 changes: 0 additions & 2 deletions packages/vscode/src/markdowndocumentprovider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import {
topLogprobsToMarkdown,
} from "../../core/src/logprob"
import { fenceMD } from "../../core/src/mkmd"
import { createWebview } from "./webview"

const SCHEME = "genaiscript"

Expand Down Expand Up @@ -233,7 +232,6 @@ export function activateMarkdownTextDocumentContentProvider(
)
),
registerCommand("genaiscript.request.open.output", async () => {
//await createWebview(state)
return vscode.commands.executeCommand(
"genaiscript.request.open",
REQUEST_OUTPUT_FILENAME
Expand Down
6 changes: 6 additions & 0 deletions packages/vscode/src/promptcommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,17 @@ export function commandButtons(state: ExtensionState) {
const request = state.aiRequest
const { computing } = request || {}
const abort = "Abort"
const view = "View"
const output = "Output"
const trace = "Trace"
const cmds: { label: string; description?: string; cmd: string }[] = []
if (computing) cmds.push({ label: abort, cmd: "genaiscript.request.abort" })
if (request) {
cmds.push({
label: view,
description: "View GenAIScript request.",
cmd: "genaiscript.request.open.view",
})
cmds.push({
label: output,
description: "Preview AI response.",
Expand Down
16 changes: 13 additions & 3 deletions packages/vscode/src/webview.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import * as vscode from "vscode"
import { TOOL_ID, TOOL_NAME } from "../../core/src/constants"
import { ExtensionState } from "./state"
import { registerCommand } from "./commands"

export async function createWebview(
state: ExtensionState
): Promise<vscode.Webview> {
async function createWebview(state: ExtensionState): Promise<vscode.Webview> {
const { host, sessionApiKey, context } = state
await host.server.client()

Expand All @@ -16,6 +15,7 @@ export async function createWebview(
{
localResourceRoots: [vscode.Uri.parse(authority)],
enableScripts: true,
retainContextWhenHidden: true,
}
)
context.subscriptions.push(webview)
Expand All @@ -39,3 +39,13 @@ export async function createWebview(
`
return webview.webview
}

export function activeWebview(state: ExtensionState) {
const { context } = state
const { subscriptions } = context
subscriptions.push(
registerCommand("genaiscript.request.open.view", async () => {
await createWebview(state)
})
)
}
6 changes: 4 additions & 2 deletions packages/web/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -134,12 +134,14 @@ class RunClient extends WebSocketClient {
console.log(`script: end`, data.result)
this.updateRunId(data)
this.result = cleanedClone(data.result)
this.output = data.result.text
this.dispatchEvent(
new CustomEvent(RunClient.SCRIPT_END_EVENT, {
detail: this.result,
})
)
this.dispatchEvent(new Event(RunClient.RESULT_EVENT))
this.dispatchEvent(new Event(RunClient.PROGRESS_EVENT))
break
}
case "script.start":
Expand Down Expand Up @@ -1281,8 +1283,8 @@ function ResultsTabs() {
onVscTabsSelect={(e) => setSelected(e.detail.selectedIndex)}
panel
>
<OutputTraceTabPanel selected={selected === 0} />
<TraceTabPanel selected={selected === 1} />
<TraceTabPanel selected={selected === 0} />
<OutputTraceTabPanel selected={selected === 1} />
<MessagesTabPanel />
<ProblemsTabPanel />
<LogProbsTabPanel />
Expand Down

0 comments on commit b1a22b9

Please sign in to comment.