mirror of
https://github.com/simstudioai/sim.git
synced 2026-02-11 15:14:53 -05:00
Compare commits
1 Commits
feat/blogb
...
feat/strea
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
031866e07c |
@@ -38,7 +38,6 @@ export async function GET(request: NextRequest, { params }: { params: Promise<{
|
||||
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
||||
}
|
||||
|
||||
const isInternalCall = auth.authType === 'internal_jwt'
|
||||
const userId = auth.userId || null
|
||||
|
||||
let workflowData = await getWorkflowById(workflowId)
|
||||
@@ -48,32 +47,29 @@ export async function GET(request: NextRequest, { params }: { params: Promise<{
|
||||
return NextResponse.json({ error: 'Workflow not found' }, { status: 404 })
|
||||
}
|
||||
|
||||
if (isInternalCall && !userId) {
|
||||
// Internal system calls (e.g. workflow-in-workflow executor) may not carry a userId.
|
||||
// These are already authenticated via internal JWT; allow read access.
|
||||
logger.info(`[${requestId}] Internal API call for workflow ${workflowId}`)
|
||||
} else if (!userId) {
|
||||
// Check if user has access to this workflow
|
||||
if (!userId) {
|
||||
logger.warn(`[${requestId}] Unauthorized access attempt for workflow ${workflowId}`)
|
||||
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
||||
} else {
|
||||
const authorization = await authorizeWorkflowByWorkspacePermission({
|
||||
workflowId,
|
||||
userId,
|
||||
action: 'read',
|
||||
})
|
||||
if (!authorization.workflow) {
|
||||
logger.warn(`[${requestId}] Workflow ${workflowId} not found`)
|
||||
return NextResponse.json({ error: 'Workflow not found' }, { status: 404 })
|
||||
}
|
||||
}
|
||||
|
||||
workflowData = authorization.workflow
|
||||
if (!authorization.allowed) {
|
||||
logger.warn(`[${requestId}] User ${userId} denied access to workflow ${workflowId}`)
|
||||
return NextResponse.json(
|
||||
{ error: authorization.message || 'Access denied' },
|
||||
{ status: authorization.status }
|
||||
)
|
||||
}
|
||||
const authorization = await authorizeWorkflowByWorkspacePermission({
|
||||
workflowId,
|
||||
userId,
|
||||
action: 'read',
|
||||
})
|
||||
if (!authorization.workflow) {
|
||||
logger.warn(`[${requestId}] Workflow ${workflowId} not found`)
|
||||
return NextResponse.json({ error: 'Workflow not found' }, { status: 404 })
|
||||
}
|
||||
|
||||
workflowData = authorization.workflow
|
||||
if (!authorization.allowed) {
|
||||
logger.warn(`[${requestId}] User ${userId} denied access to workflow ${workflowId}`)
|
||||
return NextResponse.json(
|
||||
{ error: authorization.message || 'Access denied' },
|
||||
{ status: authorization.status }
|
||||
)
|
||||
}
|
||||
|
||||
logger.debug(`[${requestId}] Attempting to load workflow ${workflowId} from normalized tables`)
|
||||
|
||||
@@ -108,7 +108,7 @@ const SmoothThinkingText = memo(
|
||||
return (
|
||||
<div
|
||||
ref={textRef}
|
||||
className='[&_*]:!text-[var(--text-muted)] [&_*]:!text-[12px] [&_*]:!leading-[1.4] [&_p]:!m-0 [&_p]:!mb-1 [&_h1]:!text-[12px] [&_h1]:!font-semibold [&_h1]:!m-0 [&_h1]:!mb-1 [&_h2]:!text-[12px] [&_h2]:!font-semibold [&_h2]:!m-0 [&_h2]:!mb-1 [&_h3]:!text-[12px] [&_h3]:!font-semibold [&_h3]:!m-0 [&_h3]:!mb-1 [&_code]:!text-[11px] [&_ul]:!pl-5 [&_ul]:!my-1 [&_ol]:!pl-6 [&_ol]:!my-1 [&_li]:!my-0.5 [&_li]:!py-0 font-season text-[12px] text-[var(--text-muted)]'
|
||||
className='[&_*]:!text-[var(--text-muted)] [&_*]:!text-[12px] [&_*]:!leading-[1.4] [&_p]:!m-0 [&_p]:!mb-1 [&_h1]:!text-[12px] [&_h1]:!font-semibold [&_h1]:!m-0 [&_h1]:!mb-1 [&_h2]:!text-[12px] [&_h2]:!font-semibold [&_h2]:!m-0 [&_h2]:!mb-1 [&_h3]:!text-[12px] [&_h3]:!font-semibold [&_h3]:!m-0 [&_h3]:!mb-1 [&_code]:!text-[11px] [&_ul]:!pl-5 [&_ul]:!my-1 [&_ol]:!pl-8 [&_ol]:!my-1 [&_li]:!my-0.5 [&_li]:!py-0 font-season text-[12px] text-[var(--text-muted)]'
|
||||
>
|
||||
<CopilotMarkdownRenderer content={displayedContent} />
|
||||
</div>
|
||||
@@ -355,7 +355,7 @@ export function ThinkingBlock({
|
||||
isExpanded ? 'mt-1.5 max-h-[150px] opacity-100' : 'max-h-0 opacity-0'
|
||||
)}
|
||||
>
|
||||
<div className='[&_*]:!text-[var(--text-muted)] [&_*]:!text-[12px] [&_*]:!leading-[1.4] [&_p]:!m-0 [&_p]:!mb-1 [&_h1]:!text-[12px] [&_h1]:!font-semibold [&_h1]:!m-0 [&_h1]:!mb-1 [&_h2]:!text-[12px] [&_h2]:!font-semibold [&_h2]:!m-0 [&_h2]:!mb-1 [&_h3]:!text-[12px] [&_h3]:!font-semibold [&_h3]:!m-0 [&_h3]:!mb-1 [&_code]:!text-[11px] [&_ul]:!pl-5 [&_ul]:!my-1 [&_ol]:!pl-6 [&_ol]:!my-1 [&_li]:!my-0.5 [&_li]:!py-0 font-season text-[12px] text-[var(--text-muted)]'>
|
||||
<div className='[&_*]:!text-[var(--text-muted)] [&_*]:!text-[12px] [&_*]:!leading-[1.4] [&_p]:!m-0 [&_p]:!mb-1 [&_h1]:!text-[12px] [&_h1]:!font-semibold [&_h1]:!m-0 [&_h1]:!mb-1 [&_h2]:!text-[12px] [&_h2]:!font-semibold [&_h2]:!m-0 [&_h2]:!mb-1 [&_h3]:!text-[12px] [&_h3]:!font-semibold [&_h3]:!m-0 [&_h3]:!mb-1 [&_code]:!text-[11px] [&_ul]:!pl-5 [&_ul]:!my-1 [&_ol]:!pl-8 [&_ol]:!my-1 [&_li]:!my-0.5 [&_li]:!py-0 font-season text-[12px] text-[var(--text-muted)]'>
|
||||
<CopilotMarkdownRenderer content={cleanContent} />
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -10,11 +10,9 @@ import {
|
||||
getReasoningEffortValuesForModel,
|
||||
getThinkingLevelsForModel,
|
||||
getVerbosityValuesForModel,
|
||||
MODELS_WITH_DEEP_RESEARCH,
|
||||
MODELS_WITH_REASONING_EFFORT,
|
||||
MODELS_WITH_THINKING,
|
||||
MODELS_WITH_VERBOSITY,
|
||||
MODELS_WITHOUT_MEMORY,
|
||||
providers,
|
||||
supportsTemperature,
|
||||
} from '@/providers/utils'
|
||||
@@ -414,22 +412,12 @@ Return ONLY the JSON array.`,
|
||||
title: 'Tools',
|
||||
type: 'tool-input',
|
||||
defaultValue: [],
|
||||
condition: {
|
||||
field: 'model',
|
||||
value: MODELS_WITH_DEEP_RESEARCH,
|
||||
not: true,
|
||||
},
|
||||
},
|
||||
{
|
||||
id: 'skills',
|
||||
title: 'Skills',
|
||||
type: 'skill-input',
|
||||
defaultValue: [],
|
||||
condition: {
|
||||
field: 'model',
|
||||
value: MODELS_WITH_DEEP_RESEARCH,
|
||||
not: true,
|
||||
},
|
||||
},
|
||||
{
|
||||
id: 'memoryType',
|
||||
@@ -443,11 +431,6 @@ Return ONLY the JSON array.`,
|
||||
{ label: 'Sliding window (tokens)', id: 'sliding_window_tokens' },
|
||||
],
|
||||
defaultValue: 'none',
|
||||
condition: {
|
||||
field: 'model',
|
||||
value: MODELS_WITHOUT_MEMORY,
|
||||
not: true,
|
||||
},
|
||||
},
|
||||
{
|
||||
id: 'conversationId',
|
||||
@@ -461,7 +444,6 @@ Return ONLY the JSON array.`,
|
||||
condition: {
|
||||
field: 'memoryType',
|
||||
value: ['conversation', 'sliding_window', 'sliding_window_tokens'],
|
||||
and: { field: 'model', value: MODELS_WITHOUT_MEMORY, not: true },
|
||||
},
|
||||
},
|
||||
{
|
||||
@@ -472,7 +454,6 @@ Return ONLY the JSON array.`,
|
||||
condition: {
|
||||
field: 'memoryType',
|
||||
value: ['sliding_window'],
|
||||
and: { field: 'model', value: MODELS_WITHOUT_MEMORY, not: true },
|
||||
},
|
||||
},
|
||||
{
|
||||
@@ -483,7 +464,6 @@ Return ONLY the JSON array.`,
|
||||
condition: {
|
||||
field: 'memoryType',
|
||||
value: ['sliding_window_tokens'],
|
||||
and: { field: 'model', value: MODELS_WITHOUT_MEMORY, not: true },
|
||||
},
|
||||
},
|
||||
{
|
||||
@@ -497,13 +477,9 @@ Return ONLY the JSON array.`,
|
||||
condition: () => ({
|
||||
field: 'model',
|
||||
value: (() => {
|
||||
const deepResearch = new Set(MODELS_WITH_DEEP_RESEARCH.map((m) => m.toLowerCase()))
|
||||
const allModels = Object.keys(getBaseModelProviders())
|
||||
return allModels.filter(
|
||||
(model) =>
|
||||
supportsTemperature(model) &&
|
||||
getMaxTemperature(model) === 1 &&
|
||||
!deepResearch.has(model.toLowerCase())
|
||||
(model) => supportsTemperature(model) && getMaxTemperature(model) === 1
|
||||
)
|
||||
})(),
|
||||
}),
|
||||
@@ -519,13 +495,9 @@ Return ONLY the JSON array.`,
|
||||
condition: () => ({
|
||||
field: 'model',
|
||||
value: (() => {
|
||||
const deepResearch = new Set(MODELS_WITH_DEEP_RESEARCH.map((m) => m.toLowerCase()))
|
||||
const allModels = Object.keys(getBaseModelProviders())
|
||||
return allModels.filter(
|
||||
(model) =>
|
||||
supportsTemperature(model) &&
|
||||
getMaxTemperature(model) === 2 &&
|
||||
!deepResearch.has(model.toLowerCase())
|
||||
(model) => supportsTemperature(model) && getMaxTemperature(model) === 2
|
||||
)
|
||||
})(),
|
||||
}),
|
||||
@@ -536,11 +508,6 @@ Return ONLY the JSON array.`,
|
||||
type: 'short-input',
|
||||
placeholder: 'Enter max tokens (e.g., 4096)...',
|
||||
mode: 'advanced',
|
||||
condition: {
|
||||
field: 'model',
|
||||
value: MODELS_WITH_DEEP_RESEARCH,
|
||||
not: true,
|
||||
},
|
||||
},
|
||||
{
|
||||
id: 'responseFormat',
|
||||
@@ -548,11 +515,6 @@ Return ONLY the JSON array.`,
|
||||
type: 'code',
|
||||
placeholder: 'Enter JSON schema...',
|
||||
language: 'json',
|
||||
condition: {
|
||||
field: 'model',
|
||||
value: MODELS_WITH_DEEP_RESEARCH,
|
||||
not: true,
|
||||
},
|
||||
wandConfig: {
|
||||
enabled: true,
|
||||
maintainHistory: true,
|
||||
@@ -645,16 +607,6 @@ Example 3 (Array Input):
|
||||
generationType: 'json-schema',
|
||||
},
|
||||
},
|
||||
{
|
||||
id: 'previousInteractionId',
|
||||
title: 'Previous Interaction ID',
|
||||
type: 'short-input',
|
||||
placeholder: 'e.g., {{agent_1.interactionId}}',
|
||||
condition: {
|
||||
field: 'model',
|
||||
value: MODELS_WITH_DEEP_RESEARCH,
|
||||
},
|
||||
},
|
||||
],
|
||||
tools: {
|
||||
access: [
|
||||
@@ -818,13 +770,5 @@ Example 3 (Array Input):
|
||||
description: 'Provider timing information',
|
||||
},
|
||||
cost: { type: 'json', description: 'Cost of the API call' },
|
||||
interactionId: {
|
||||
type: 'string',
|
||||
description: 'Interaction ID for multi-turn deep research follow-ups',
|
||||
condition: {
|
||||
field: 'model',
|
||||
value: MODELS_WITH_DEEP_RESEARCH,
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
@@ -2,8 +2,8 @@
|
||||
slug: enterprise
|
||||
title: 'Build with Sim for Enterprise'
|
||||
description: 'Access control, BYOK, self-hosted deployments, on-prem Copilot, SSO & SAML, whitelabeling, Admin API, and flexible data retention—enterprise features for teams with strict security and compliance requirements.'
|
||||
date: 2026-02-11
|
||||
updated: 2026-02-11
|
||||
date: 2026-01-23
|
||||
updated: 2026-01-23
|
||||
authors:
|
||||
- vik
|
||||
readingTime: 10
|
||||
@@ -13,8 +13,8 @@ ogAlt: 'Sim Enterprise features overview'
|
||||
about: ['Enterprise Software', 'Security', 'Compliance', 'Self-Hosting']
|
||||
timeRequired: PT10M
|
||||
canonical: https://sim.ai/studio/enterprise
|
||||
featured: true
|
||||
draft: false
|
||||
featured: false
|
||||
draft: true
|
||||
---
|
||||
|
||||
We've been working with security teams at larger organizations to bring Sim into environments with strict compliance and data handling requirements. This post covers the enterprise capabilities we've built: granular access control, bring-your-own-keys, self-hosted deployments, on-prem Copilot, SSO & SAML, whitelabeling, compliance, and programmatic management via the Admin API.
|
||||
|
||||
@@ -1,90 +0,0 @@
|
||||
---
|
||||
slug: workflow-bench
|
||||
title: 'Introducing Workflow Bench - Benchmarking Natural Language Workflow Building'
|
||||
description: 'How we built a benchmark to measure how well AI models translate natural language instructions into executable workflows, and what we learned along the way'
|
||||
date: 2026-02-11
|
||||
updated: 2026-02-11
|
||||
authors:
|
||||
- sid
|
||||
readingTime: 10
|
||||
tags: [Benchmark, Evaluation, Workflows, Natural Language]
|
||||
ogImage: /studio/workflow-bench/cover.png
|
||||
ogAlt: 'Workflow Bench benchmark overview'
|
||||
about: ['Benchmarking', 'Workflow Building', 'Natural Language']
|
||||
timeRequired: PT10M
|
||||
canonical: https://sim.ai/studio/workflow-bench
|
||||
featured: false
|
||||
draft: true
|
||||
---
|
||||
|
||||
Building workflows from natural language sounds straightforward until you try to measure it. When a user says "send me a Slack message every morning with a summary of my unread emails," how do you evaluate whether the resulting workflow is correct? Is partial credit fair? What about workflows that are functionally equivalent but structurally different?
|
||||
|
||||
We built Workflow Bench to answer these questions. This post covers why we needed a dedicated benchmark, how we designed it, and what the results tell us about the current state of natural language workflow building.
|
||||
|
||||
## Why a Workflow Benchmark?
|
||||
|
||||
<!-- TODO: Motivation for building Workflow Bench -->
|
||||
<!-- - Gap in existing benchmarks (code gen benchmarks don't capture workflow semantics) -->
|
||||
<!-- - Need to track progress as we iterate on the copilot / natural language builder -->
|
||||
<!-- - Workflows are structured artifacts, not just code — they have topology, block types, connections, configs -->
|
||||
|
||||
## What We're Measuring
|
||||
|
||||
<!-- TODO: Define the core evaluation dimensions -->
|
||||
<!-- - Structural correctness (right blocks, right connections) -->
|
||||
<!-- - Configuration accuracy (correct params, API mappings) -->
|
||||
<!-- - Functional equivalence (does it do the same thing even if shaped differently?) -->
|
||||
<!-- - Edge cases: loops, conditionals, parallel branches, error handling -->
|
||||
|
||||
## Benchmark Design
|
||||
|
||||
<!-- TODO: How the benchmark dataset is constructed -->
|
||||
<!-- - Task categories and complexity tiers -->
|
||||
<!-- - How ground truth workflows are defined -->
|
||||
<!-- - Natural language prompt variations (terse vs. detailed, ambiguous vs. precise) -->
|
||||
|
||||
### Task Categories
|
||||
|
||||
<!-- TODO: Break down the types of workflows in the benchmark -->
|
||||
<!-- - Simple linear (A → B → C) -->
|
||||
<!-- - Branching / conditional -->
|
||||
<!-- - Looping / iterative -->
|
||||
<!-- - Parallel fan-out / fan-in -->
|
||||
<!-- - Multi-trigger -->
|
||||
|
||||
### Scoring
|
||||
|
||||
<!-- TODO: Explain the scoring methodology -->
|
||||
<!-- - How partial credit works -->
|
||||
<!-- - Structural similarity metrics -->
|
||||
<!-- - Config-level accuracy -->
|
||||
<!-- - Overall composite score -->
|
||||
|
||||
## Evaluation Pipeline
|
||||
|
||||
<!-- TODO: How we run the benchmark end to end -->
|
||||
<!-- - Prompt → model → workflow JSON → evaluator → score -->
|
||||
<!-- - Automation and reproducibility -->
|
||||
<!-- - How we handle non-determinism across runs -->
|
||||
|
||||
## Results
|
||||
|
||||
<!-- TODO: Present the benchmark results -->
|
||||
<!-- - Model comparisons -->
|
||||
<!-- - Performance by task category -->
|
||||
<!-- - Where models struggle most -->
|
||||
<!-- - Trends over time as we iterate -->
|
||||
|
||||
## What We Learned
|
||||
|
||||
<!-- TODO: Key takeaways from running the benchmark -->
|
||||
<!-- - Surprising strengths and weaknesses -->
|
||||
<!-- - How benchmark results influenced product decisions -->
|
||||
<!-- - Common failure modes -->
|
||||
|
||||
## What's Next
|
||||
|
||||
<!-- TODO: Future directions -->
|
||||
<!-- - Expanding the benchmark (more tasks, more complexity) -->
|
||||
<!-- - Community contributions / open-sourcing -->
|
||||
<!-- - Using the benchmark to guide copilot improvements -->
|
||||
@@ -999,7 +999,6 @@ export class AgentBlockHandler implements BlockHandler {
|
||||
reasoningEffort: inputs.reasoningEffort,
|
||||
verbosity: inputs.verbosity,
|
||||
thinkingLevel: inputs.thinkingLevel,
|
||||
previousInteractionId: inputs.previousInteractionId,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1070,7 +1069,6 @@ export class AgentBlockHandler implements BlockHandler {
|
||||
reasoningEffort: providerRequest.reasoningEffort,
|
||||
verbosity: providerRequest.verbosity,
|
||||
thinkingLevel: providerRequest.thinkingLevel,
|
||||
previousInteractionId: providerRequest.previousInteractionId,
|
||||
})
|
||||
|
||||
return this.processProviderResponse(response, block, responseFormat)
|
||||
@@ -1271,7 +1269,6 @@ export class AgentBlockHandler implements BlockHandler {
|
||||
content: result.content,
|
||||
model: result.model,
|
||||
...this.createResponseMetadata(result),
|
||||
...(result.interactionId && { interactionId: result.interactionId }),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -20,8 +20,6 @@ export interface AgentInputs {
|
||||
conversationId?: string // Required for all non-none memory types
|
||||
slidingWindowSize?: string // For message-based sliding window
|
||||
slidingWindowTokens?: string // For token-based sliding window
|
||||
// Deep research multi-turn
|
||||
previousInteractionId?: string // Interactions API previous interaction reference
|
||||
// LLM parameters
|
||||
temperature?: string
|
||||
maxTokens?: string
|
||||
|
||||
@@ -5,7 +5,7 @@ import { serializeMessagesForDB } from './serialization'
|
||||
|
||||
const logger = createLogger('CopilotMessagePersistence')
|
||||
|
||||
export async function persistMessages(params: {
|
||||
interface PersistParams {
|
||||
chatId: string
|
||||
messages: CopilotMessage[]
|
||||
sensitiveCredentialIds?: Set<string>
|
||||
@@ -13,24 +13,29 @@ export async function persistMessages(params: {
|
||||
mode?: string
|
||||
model?: string
|
||||
conversationId?: string
|
||||
}): Promise<boolean> {
|
||||
}
|
||||
|
||||
/** Builds the JSON body used by both fetch and sendBeacon persistence paths. */
|
||||
function buildPersistBody(params: PersistParams): string {
|
||||
const dbMessages = serializeMessagesForDB(
|
||||
params.messages,
|
||||
params.sensitiveCredentialIds ?? new Set<string>()
|
||||
)
|
||||
return JSON.stringify({
|
||||
chatId: params.chatId,
|
||||
messages: dbMessages,
|
||||
...(params.planArtifact !== undefined ? { planArtifact: params.planArtifact } : {}),
|
||||
...(params.mode || params.model ? { config: { mode: params.mode, model: params.model } } : {}),
|
||||
...(params.conversationId ? { conversationId: params.conversationId } : {}),
|
||||
})
|
||||
}
|
||||
|
||||
export async function persistMessages(params: PersistParams): Promise<boolean> {
|
||||
try {
|
||||
const dbMessages = serializeMessagesForDB(
|
||||
params.messages,
|
||||
params.sensitiveCredentialIds ?? new Set<string>()
|
||||
)
|
||||
const response = await fetch(COPILOT_UPDATE_MESSAGES_API_PATH, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({
|
||||
chatId: params.chatId,
|
||||
messages: dbMessages,
|
||||
...(params.planArtifact !== undefined ? { planArtifact: params.planArtifact } : {}),
|
||||
...(params.mode || params.model
|
||||
? { config: { mode: params.mode, model: params.model } }
|
||||
: {}),
|
||||
...(params.conversationId ? { conversationId: params.conversationId } : {}),
|
||||
}),
|
||||
body: buildPersistBody(params),
|
||||
})
|
||||
return response.ok
|
||||
} catch (error) {
|
||||
@@ -41,3 +46,27 @@ export async function persistMessages(params: {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Persists messages using navigator.sendBeacon, which is reliable during page unload.
|
||||
* Unlike fetch, sendBeacon is guaranteed to be queued even when the page is being torn down.
|
||||
*/
|
||||
export function persistMessagesBeacon(params: PersistParams): boolean {
|
||||
try {
|
||||
const body = buildPersistBody(params)
|
||||
const blob = new Blob([body], { type: 'application/json' })
|
||||
const sent = navigator.sendBeacon(COPILOT_UPDATE_MESSAGES_API_PATH, blob)
|
||||
if (!sent) {
|
||||
logger.warn('sendBeacon returned false — browser may have rejected the request', {
|
||||
chatId: params.chatId,
|
||||
})
|
||||
}
|
||||
return sent
|
||||
} catch (error) {
|
||||
logger.warn('Failed to persist messages via sendBeacon', {
|
||||
chatId: params.chatId,
|
||||
error: error instanceof Error ? error.message : String(error),
|
||||
})
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,7 +5,6 @@ import {
|
||||
type GenerateContentConfig,
|
||||
type GenerateContentResponse,
|
||||
type GoogleGenAI,
|
||||
type Interactions,
|
||||
type Part,
|
||||
type Schema,
|
||||
type ThinkingConfig,
|
||||
@@ -28,7 +27,6 @@ import {
|
||||
import type { FunctionCallResponse, ProviderRequest, ProviderResponse } from '@/providers/types'
|
||||
import {
|
||||
calculateCost,
|
||||
isDeepResearchModel,
|
||||
prepareToolExecution,
|
||||
prepareToolsWithUsageControl,
|
||||
} from '@/providers/utils'
|
||||
@@ -383,468 +381,6 @@ export interface GeminiExecutionConfig {
|
||||
providerType: GeminiProviderType
|
||||
}
|
||||
|
||||
const DEEP_RESEARCH_POLL_INTERVAL_MS = 10_000
|
||||
const DEEP_RESEARCH_MAX_DURATION_MS = 60 * 60 * 1000
|
||||
|
||||
/**
|
||||
* Sleeps for the specified number of milliseconds
|
||||
*/
|
||||
function sleep(ms: number): Promise<void> {
|
||||
return new Promise((resolve) => setTimeout(resolve, ms))
|
||||
}
|
||||
|
||||
/**
|
||||
* Collapses a ProviderRequest into a single input string and optional system instruction
|
||||
* for the Interactions API, which takes a flat input rather than a messages array.
|
||||
*
|
||||
* Deep research is single-turn only — it takes one research query and returns a report.
|
||||
* Memory/conversation history is hidden in the UI for deep research models, so only
|
||||
* the last user message is used as input. System messages are passed via system_instruction.
|
||||
*/
|
||||
function collapseMessagesToInput(request: ProviderRequest): {
|
||||
input: string
|
||||
systemInstruction: string | undefined
|
||||
} {
|
||||
const systemParts: string[] = []
|
||||
const userParts: string[] = []
|
||||
|
||||
if (request.systemPrompt) {
|
||||
systemParts.push(request.systemPrompt)
|
||||
}
|
||||
|
||||
if (request.messages) {
|
||||
for (const msg of request.messages) {
|
||||
if (msg.role === 'system' && msg.content) {
|
||||
systemParts.push(msg.content)
|
||||
} else if (msg.role === 'user' && msg.content) {
|
||||
userParts.push(msg.content)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
input:
|
||||
userParts.length > 0
|
||||
? userParts[userParts.length - 1]
|
||||
: 'Please conduct research on the provided topic.',
|
||||
systemInstruction: systemParts.length > 0 ? systemParts.join('\n\n') : undefined,
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Extracts text content from a completed interaction's outputs array.
|
||||
* The outputs array can contain text, thought, google_search_result, and other types.
|
||||
* We concatenate all text outputs to get the full research report.
|
||||
*/
|
||||
function extractTextFromInteractionOutputs(outputs: Interactions.Interaction['outputs']): string {
|
||||
if (!outputs || outputs.length === 0) return ''
|
||||
|
||||
const textParts: string[] = []
|
||||
for (const output of outputs) {
|
||||
if (output.type === 'text') {
|
||||
const text = (output as Interactions.TextContent).text
|
||||
if (text) textParts.push(text)
|
||||
}
|
||||
}
|
||||
|
||||
return textParts.join('\n\n')
|
||||
}
|
||||
|
||||
/**
|
||||
* Extracts token usage from an Interaction's Usage object.
|
||||
* The Interactions API provides total_input_tokens, total_output_tokens, total_tokens,
|
||||
* and total_reasoning_tokens (for thinking models).
|
||||
*
|
||||
* Also handles the raw API field name total_thought_tokens which the SDK may
|
||||
* map to total_reasoning_tokens.
|
||||
*/
|
||||
function extractInteractionUsage(usage: Interactions.Usage | undefined): {
|
||||
inputTokens: number
|
||||
outputTokens: number
|
||||
reasoningTokens: number
|
||||
totalTokens: number
|
||||
} {
|
||||
if (!usage) {
|
||||
return { inputTokens: 0, outputTokens: 0, reasoningTokens: 0, totalTokens: 0 }
|
||||
}
|
||||
|
||||
const usageLogger = createLogger('DeepResearchUsage')
|
||||
usageLogger.info('Raw interaction usage', { usage: JSON.stringify(usage) })
|
||||
|
||||
const inputTokens = usage.total_input_tokens ?? 0
|
||||
const outputTokens = usage.total_output_tokens ?? 0
|
||||
const reasoningTokens =
|
||||
usage.total_reasoning_tokens ??
|
||||
((usage as Record<string, unknown>).total_thought_tokens as number) ??
|
||||
0
|
||||
const totalTokens = usage.total_tokens ?? inputTokens + outputTokens
|
||||
|
||||
return { inputTokens, outputTokens, reasoningTokens, totalTokens }
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds a standard ProviderResponse from a completed deep research interaction.
|
||||
*/
|
||||
function buildDeepResearchResponse(
|
||||
content: string,
|
||||
model: string,
|
||||
usage: {
|
||||
inputTokens: number
|
||||
outputTokens: number
|
||||
reasoningTokens: number
|
||||
totalTokens: number
|
||||
},
|
||||
providerStartTime: number,
|
||||
providerStartTimeISO: string,
|
||||
interactionId?: string
|
||||
): ProviderResponse {
|
||||
const providerEndTime = Date.now()
|
||||
const duration = providerEndTime - providerStartTime
|
||||
|
||||
return {
|
||||
content,
|
||||
model,
|
||||
tokens: {
|
||||
input: usage.inputTokens,
|
||||
output: usage.outputTokens,
|
||||
total: usage.totalTokens,
|
||||
},
|
||||
timing: {
|
||||
startTime: providerStartTimeISO,
|
||||
endTime: new Date(providerEndTime).toISOString(),
|
||||
duration,
|
||||
modelTime: duration,
|
||||
toolsTime: 0,
|
||||
firstResponseTime: duration,
|
||||
iterations: 1,
|
||||
timeSegments: [
|
||||
{
|
||||
type: 'model',
|
||||
name: 'Deep research',
|
||||
startTime: providerStartTime,
|
||||
endTime: providerEndTime,
|
||||
duration,
|
||||
},
|
||||
],
|
||||
},
|
||||
cost: calculateCost(model, usage.inputTokens, usage.outputTokens),
|
||||
interactionId,
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a ReadableStream from a deep research streaming interaction.
|
||||
*
|
||||
* Deep research streaming returns InteractionSSEEvent chunks including:
|
||||
* - interaction.start: initial interaction with ID
|
||||
* - content.delta: incremental text and thought_summary updates
|
||||
* - content.start / content.stop: output boundaries
|
||||
* - interaction.complete: final event (outputs is undefined in streaming; must reconstruct)
|
||||
* - error: error events
|
||||
*
|
||||
* We stream text deltas to the client and track usage from the interaction.complete event.
|
||||
*/
|
||||
function createDeepResearchStream(
|
||||
stream: AsyncIterable<Interactions.InteractionSSEEvent>,
|
||||
onComplete?: (
|
||||
content: string,
|
||||
usage: {
|
||||
inputTokens: number
|
||||
outputTokens: number
|
||||
reasoningTokens: number
|
||||
totalTokens: number
|
||||
},
|
||||
interactionId?: string
|
||||
) => void
|
||||
): ReadableStream<Uint8Array> {
|
||||
const streamLogger = createLogger('DeepResearchStream')
|
||||
let fullContent = ''
|
||||
let completionUsage = { inputTokens: 0, outputTokens: 0, reasoningTokens: 0, totalTokens: 0 }
|
||||
let completedInteractionId: string | undefined
|
||||
|
||||
return new ReadableStream({
|
||||
async start(controller) {
|
||||
try {
|
||||
for await (const event of stream) {
|
||||
if (event.event_type === 'content.delta') {
|
||||
const delta = (event as Interactions.ContentDelta).delta
|
||||
if (delta?.type === 'text' && 'text' in delta && delta.text) {
|
||||
fullContent += delta.text
|
||||
controller.enqueue(new TextEncoder().encode(delta.text))
|
||||
}
|
||||
} else if (event.event_type === 'interaction.complete') {
|
||||
const interaction = (event as Interactions.InteractionEvent).interaction
|
||||
if (interaction?.usage) {
|
||||
completionUsage = extractInteractionUsage(interaction.usage)
|
||||
}
|
||||
completedInteractionId = interaction?.id
|
||||
} else if (event.event_type === 'interaction.start') {
|
||||
const interaction = (event as Interactions.InteractionEvent).interaction
|
||||
if (interaction?.id) {
|
||||
completedInteractionId = interaction.id
|
||||
}
|
||||
} else if (event.event_type === 'error') {
|
||||
const errorEvent = event as { error?: { code?: string; message?: string } }
|
||||
const message = errorEvent.error?.message ?? 'Unknown deep research stream error'
|
||||
streamLogger.error('Deep research stream error', {
|
||||
code: errorEvent.error?.code,
|
||||
message,
|
||||
})
|
||||
controller.error(new Error(message))
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
onComplete?.(fullContent, completionUsage, completedInteractionId)
|
||||
controller.close()
|
||||
} catch (error) {
|
||||
streamLogger.error('Error reading deep research stream', {
|
||||
error: error instanceof Error ? error.message : String(error),
|
||||
})
|
||||
controller.error(error)
|
||||
}
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Executes a deep research request using the Interactions API.
|
||||
*
|
||||
* Deep research uses the Interactions API ({@link https://ai.google.dev/api/interactions-api}),
|
||||
* a completely different surface from generateContent. It creates a background interaction
|
||||
* that performs comprehensive research (up to 60 minutes).
|
||||
*
|
||||
* Supports both streaming and non-streaming modes:
|
||||
* - Streaming: returns a StreamingExecution with a ReadableStream of text deltas
|
||||
* - Non-streaming: polls until completion and returns a ProviderResponse
|
||||
*
|
||||
* Deep research does NOT support custom function calling tools, MCP servers,
|
||||
* or structured output (response_format). These are gracefully ignored.
|
||||
*/
|
||||
export async function executeDeepResearchRequest(
|
||||
config: GeminiExecutionConfig
|
||||
): Promise<ProviderResponse | StreamingExecution> {
|
||||
const { ai, model, request, providerType } = config
|
||||
const logger = createLogger(providerType === 'google' ? 'GoogleProvider' : 'VertexProvider')
|
||||
|
||||
logger.info('Preparing deep research request', {
|
||||
model,
|
||||
hasSystemPrompt: !!request.systemPrompt,
|
||||
hasMessages: !!request.messages?.length,
|
||||
streaming: !!request.stream,
|
||||
hasPreviousInteractionId: !!request.previousInteractionId,
|
||||
})
|
||||
|
||||
if (request.tools?.length) {
|
||||
logger.warn('Deep research does not support custom tools — ignoring tools parameter')
|
||||
}
|
||||
if (request.responseFormat) {
|
||||
logger.warn(
|
||||
'Deep research does not support structured output — ignoring responseFormat parameter'
|
||||
)
|
||||
}
|
||||
|
||||
const providerStartTime = Date.now()
|
||||
const providerStartTimeISO = new Date(providerStartTime).toISOString()
|
||||
|
||||
try {
|
||||
const { input, systemInstruction } = collapseMessagesToInput(request)
|
||||
|
||||
// Deep research requires background=true and store=true (store defaults to true,
|
||||
// but we set it explicitly per API requirements)
|
||||
const baseParams = {
|
||||
agent: model as Interactions.CreateAgentInteractionParamsNonStreaming['agent'],
|
||||
input,
|
||||
background: true,
|
||||
store: true,
|
||||
...(systemInstruction && { system_instruction: systemInstruction }),
|
||||
...(request.previousInteractionId && {
|
||||
previous_interaction_id: request.previousInteractionId,
|
||||
}),
|
||||
agent_config: {
|
||||
type: 'deep-research' as const,
|
||||
thinking_summaries: 'auto' as const,
|
||||
},
|
||||
}
|
||||
|
||||
logger.info('Creating deep research interaction', {
|
||||
inputLength: input.length,
|
||||
hasSystemInstruction: !!systemInstruction,
|
||||
streaming: !!request.stream,
|
||||
})
|
||||
|
||||
// Streaming mode: create a streaming interaction and return a StreamingExecution
|
||||
if (request.stream) {
|
||||
const streamParams: Interactions.CreateAgentInteractionParamsStreaming = {
|
||||
...baseParams,
|
||||
stream: true,
|
||||
}
|
||||
|
||||
const streamResponse = await ai.interactions.create(streamParams)
|
||||
const firstResponseTime = Date.now() - providerStartTime
|
||||
|
||||
const streamingResult: StreamingExecution = {
|
||||
stream: undefined as unknown as ReadableStream<Uint8Array>,
|
||||
execution: {
|
||||
success: true,
|
||||
output: {
|
||||
content: '',
|
||||
model,
|
||||
tokens: { input: 0, output: 0, total: 0 },
|
||||
providerTiming: {
|
||||
startTime: providerStartTimeISO,
|
||||
endTime: new Date().toISOString(),
|
||||
duration: Date.now() - providerStartTime,
|
||||
modelTime: firstResponseTime,
|
||||
toolsTime: 0,
|
||||
firstResponseTime,
|
||||
iterations: 1,
|
||||
timeSegments: [
|
||||
{
|
||||
type: 'model',
|
||||
name: 'Deep research (streaming)',
|
||||
startTime: providerStartTime,
|
||||
endTime: providerStartTime + firstResponseTime,
|
||||
duration: firstResponseTime,
|
||||
},
|
||||
],
|
||||
},
|
||||
cost: {
|
||||
input: 0,
|
||||
output: 0,
|
||||
total: 0,
|
||||
pricing: { input: 0, output: 0, updatedAt: new Date().toISOString() },
|
||||
},
|
||||
},
|
||||
logs: [],
|
||||
metadata: {
|
||||
startTime: providerStartTimeISO,
|
||||
endTime: new Date().toISOString(),
|
||||
duration: Date.now() - providerStartTime,
|
||||
},
|
||||
isStreaming: true,
|
||||
},
|
||||
}
|
||||
|
||||
streamingResult.stream = createDeepResearchStream(
|
||||
streamResponse,
|
||||
(content, usage, streamInteractionId) => {
|
||||
streamingResult.execution.output.content = content
|
||||
streamingResult.execution.output.tokens = {
|
||||
input: usage.inputTokens,
|
||||
output: usage.outputTokens,
|
||||
total: usage.totalTokens,
|
||||
}
|
||||
streamingResult.execution.output.interactionId = streamInteractionId
|
||||
|
||||
const cost = calculateCost(model, usage.inputTokens, usage.outputTokens)
|
||||
streamingResult.execution.output.cost = cost
|
||||
|
||||
const streamEndTime = Date.now()
|
||||
if (streamingResult.execution.output.providerTiming) {
|
||||
streamingResult.execution.output.providerTiming.endTime = new Date(
|
||||
streamEndTime
|
||||
).toISOString()
|
||||
streamingResult.execution.output.providerTiming.duration =
|
||||
streamEndTime - providerStartTime
|
||||
const segments = streamingResult.execution.output.providerTiming.timeSegments
|
||||
if (segments?.[0]) {
|
||||
segments[0].endTime = streamEndTime
|
||||
segments[0].duration = streamEndTime - providerStartTime
|
||||
}
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
return streamingResult
|
||||
}
|
||||
|
||||
// Non-streaming mode: create and poll
|
||||
const createParams: Interactions.CreateAgentInteractionParamsNonStreaming = {
|
||||
...baseParams,
|
||||
stream: false,
|
||||
}
|
||||
|
||||
const interaction = await ai.interactions.create(createParams)
|
||||
const interactionId = interaction.id
|
||||
|
||||
logger.info('Deep research interaction created', { interactionId, status: interaction.status })
|
||||
|
||||
// Poll until a terminal status
|
||||
const pollStartTime = Date.now()
|
||||
let result: Interactions.Interaction = interaction
|
||||
|
||||
while (Date.now() - pollStartTime < DEEP_RESEARCH_MAX_DURATION_MS) {
|
||||
if (result.status === 'completed') {
|
||||
break
|
||||
}
|
||||
|
||||
if (result.status === 'failed') {
|
||||
throw new Error(`Deep research interaction failed: ${interactionId}`)
|
||||
}
|
||||
|
||||
if (result.status === 'cancelled') {
|
||||
throw new Error(`Deep research interaction was cancelled: ${interactionId}`)
|
||||
}
|
||||
|
||||
logger.info('Deep research in progress, polling...', {
|
||||
interactionId,
|
||||
status: result.status,
|
||||
elapsedMs: Date.now() - pollStartTime,
|
||||
})
|
||||
|
||||
await sleep(DEEP_RESEARCH_POLL_INTERVAL_MS)
|
||||
result = await ai.interactions.get(interactionId)
|
||||
}
|
||||
|
||||
if (result.status !== 'completed') {
|
||||
throw new Error(
|
||||
`Deep research timed out after ${DEEP_RESEARCH_MAX_DURATION_MS / 1000}s (status: ${result.status})`
|
||||
)
|
||||
}
|
||||
|
||||
const content = extractTextFromInteractionOutputs(result.outputs)
|
||||
const usage = extractInteractionUsage(result.usage)
|
||||
|
||||
logger.info('Deep research completed', {
|
||||
interactionId,
|
||||
contentLength: content.length,
|
||||
inputTokens: usage.inputTokens,
|
||||
outputTokens: usage.outputTokens,
|
||||
reasoningTokens: usage.reasoningTokens,
|
||||
totalTokens: usage.totalTokens,
|
||||
durationMs: Date.now() - providerStartTime,
|
||||
})
|
||||
|
||||
return buildDeepResearchResponse(
|
||||
content,
|
||||
model,
|
||||
usage,
|
||||
providerStartTime,
|
||||
providerStartTimeISO,
|
||||
interactionId
|
||||
)
|
||||
} catch (error) {
|
||||
const providerEndTime = Date.now()
|
||||
const duration = providerEndTime - providerStartTime
|
||||
|
||||
logger.error('Error in deep research request:', {
|
||||
error: error instanceof Error ? error.message : String(error),
|
||||
stack: error instanceof Error ? error.stack : undefined,
|
||||
})
|
||||
|
||||
const enhancedError = error instanceof Error ? error : new Error(String(error))
|
||||
Object.assign(enhancedError, {
|
||||
timing: {
|
||||
startTime: providerStartTimeISO,
|
||||
endTime: new Date(providerEndTime).toISOString(),
|
||||
duration,
|
||||
},
|
||||
})
|
||||
throw enhancedError
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Executes a request using the Gemini API
|
||||
*
|
||||
@@ -855,12 +391,6 @@ export async function executeGeminiRequest(
|
||||
config: GeminiExecutionConfig
|
||||
): Promise<ProviderResponse | StreamingExecution> {
|
||||
const { ai, model, request, providerType } = config
|
||||
|
||||
// Route deep research models to the interactions API
|
||||
if (isDeepResearchModel(model)) {
|
||||
return executeDeepResearchRequest(config)
|
||||
}
|
||||
|
||||
const logger = createLogger(providerType === 'google' ? 'GoogleProvider' : 'VertexProvider')
|
||||
|
||||
logger.info(`Preparing ${providerType} Gemini request`, {
|
||||
|
||||
@@ -46,9 +46,6 @@ export interface ModelCapabilities {
|
||||
levels: string[]
|
||||
default?: string
|
||||
}
|
||||
deepResearch?: boolean
|
||||
/** Whether this model supports conversation memory. Defaults to true if omitted. */
|
||||
memory?: boolean
|
||||
}
|
||||
|
||||
export interface ModelDefinition {
|
||||
@@ -828,7 +825,7 @@ export const PROVIDER_DEFINITIONS: Record<string, ProviderDefinition> = {
|
||||
name: 'Google',
|
||||
description: "Google's Gemini models",
|
||||
defaultModel: 'gemini-2.5-pro',
|
||||
modelPatterns: [/^gemini/, /^deep-research/],
|
||||
modelPatterns: [/^gemini/],
|
||||
capabilities: {
|
||||
toolUsageControl: true,
|
||||
},
|
||||
@@ -931,19 +928,6 @@ export const PROVIDER_DEFINITIONS: Record<string, ProviderDefinition> = {
|
||||
},
|
||||
contextWindow: 1000000,
|
||||
},
|
||||
{
|
||||
id: 'deep-research-pro-preview-12-2025',
|
||||
pricing: {
|
||||
input: 2.0,
|
||||
output: 2.0,
|
||||
updatedAt: '2026-02-10',
|
||||
},
|
||||
capabilities: {
|
||||
deepResearch: true,
|
||||
memory: false,
|
||||
},
|
||||
contextWindow: 1000000,
|
||||
},
|
||||
],
|
||||
},
|
||||
vertex: {
|
||||
@@ -1054,19 +1038,6 @@ export const PROVIDER_DEFINITIONS: Record<string, ProviderDefinition> = {
|
||||
},
|
||||
contextWindow: 1000000,
|
||||
},
|
||||
{
|
||||
id: 'vertex/deep-research-pro-preview-12-2025',
|
||||
pricing: {
|
||||
input: 2.0,
|
||||
output: 2.0,
|
||||
updatedAt: '2026-02-10',
|
||||
},
|
||||
capabilities: {
|
||||
deepResearch: true,
|
||||
memory: false,
|
||||
},
|
||||
contextWindow: 1000000,
|
||||
},
|
||||
],
|
||||
},
|
||||
deepseek: {
|
||||
@@ -2509,37 +2480,6 @@ export function getThinkingLevelsForModel(modelId: string): string[] | null {
|
||||
return capability?.levels ?? null
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all models that support deep research capability
|
||||
*/
|
||||
export function getModelsWithDeepResearch(): string[] {
|
||||
const models: string[] = []
|
||||
for (const provider of Object.values(PROVIDER_DEFINITIONS)) {
|
||||
for (const model of provider.models) {
|
||||
if (model.capabilities.deepResearch) {
|
||||
models.push(model.id)
|
||||
}
|
||||
}
|
||||
}
|
||||
return models
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all models that explicitly disable memory support (memory: false).
|
||||
* Models without this capability default to supporting memory.
|
||||
*/
|
||||
export function getModelsWithoutMemory(): string[] {
|
||||
const models: string[] = []
|
||||
for (const provider of Object.values(PROVIDER_DEFINITIONS)) {
|
||||
for (const model of provider.models) {
|
||||
if (model.capabilities.memory === false) {
|
||||
models.push(model.id)
|
||||
}
|
||||
}
|
||||
}
|
||||
return models
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the max output tokens for a specific model.
|
||||
*
|
||||
|
||||
@@ -95,8 +95,6 @@ export interface ProviderResponse {
|
||||
total: number
|
||||
pricing: ModelPricing
|
||||
}
|
||||
/** Interaction ID returned by the Interactions API (used for multi-turn deep research) */
|
||||
interactionId?: string
|
||||
}
|
||||
|
||||
export type ToolUsageControl = 'auto' | 'force' | 'none'
|
||||
@@ -171,8 +169,6 @@ export interface ProviderRequest {
|
||||
verbosity?: string
|
||||
thinkingLevel?: string
|
||||
isDeployedContext?: boolean
|
||||
/** Previous interaction ID for multi-turn Interactions API requests (deep research follow-ups) */
|
||||
previousInteractionId?: string
|
||||
}
|
||||
|
||||
export const providers: Record<string, ProviderConfig> = {}
|
||||
|
||||
@@ -12,8 +12,6 @@ import {
|
||||
getMaxOutputTokensForModel as getMaxOutputTokensForModelFromDefinitions,
|
||||
getMaxTemperature as getMaxTempFromDefinitions,
|
||||
getModelPricing as getModelPricingFromDefinitions,
|
||||
getModelsWithDeepResearch,
|
||||
getModelsWithoutMemory,
|
||||
getModelsWithReasoningEffort,
|
||||
getModelsWithTemperatureSupport,
|
||||
getModelsWithTempRange01,
|
||||
@@ -955,8 +953,6 @@ export const MODELS_WITH_TEMPERATURE_SUPPORT = getModelsWithTemperatureSupport()
|
||||
export const MODELS_WITH_REASONING_EFFORT = getModelsWithReasoningEffort()
|
||||
export const MODELS_WITH_VERBOSITY = getModelsWithVerbosity()
|
||||
export const MODELS_WITH_THINKING = getModelsWithThinking()
|
||||
export const MODELS_WITH_DEEP_RESEARCH = getModelsWithDeepResearch()
|
||||
export const MODELS_WITHOUT_MEMORY = getModelsWithoutMemory()
|
||||
export const PROVIDERS_WITH_TOOL_USAGE_CONTROL = getProvidersWithToolUsageControl()
|
||||
|
||||
export function supportsTemperature(model: string): boolean {
|
||||
@@ -975,10 +971,6 @@ export function supportsThinking(model: string): boolean {
|
||||
return MODELS_WITH_THINKING.includes(model.toLowerCase())
|
||||
}
|
||||
|
||||
export function isDeepResearchModel(model: string): boolean {
|
||||
return MODELS_WITH_DEEP_RESEARCH.includes(model.toLowerCase())
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the maximum temperature value for a model
|
||||
* @returns Maximum temperature value (1 or 2) or undefined if temperature not supported
|
||||
|
||||
Binary file not shown.
|
Before Width: | Height: | Size: 78 KiB After Width: | Height: | Size: 45 KiB |
Binary file not shown.
|
Before Width: | Height: | Size: 58 KiB After Width: | Height: | Size: 58 KiB |
@@ -39,6 +39,7 @@ import {
|
||||
buildToolCallsById,
|
||||
normalizeMessagesForUI,
|
||||
persistMessages,
|
||||
persistMessagesBeacon,
|
||||
saveMessageCheckpoint,
|
||||
} from '@/lib/copilot/messages'
|
||||
import type { CopilotTransportMode } from '@/lib/copilot/models'
|
||||
@@ -78,6 +79,28 @@ let _isPageUnloading = false
|
||||
if (typeof window !== 'undefined') {
|
||||
window.addEventListener('beforeunload', () => {
|
||||
_isPageUnloading = true
|
||||
|
||||
// Emergency persistence: flush any pending streaming updates to the store and
|
||||
// persist via sendBeacon (which is guaranteed to be queued during page teardown).
|
||||
// Without this, thinking blocks and in-progress content are lost on refresh.
|
||||
try {
|
||||
const state = useCopilotStore.getState()
|
||||
if (state.isSendingMessage && state.currentChat) {
|
||||
// Flush batched streaming updates into the store messages
|
||||
flushStreamingUpdates(useCopilotStore.setState.bind(useCopilotStore))
|
||||
const flushedState = useCopilotStore.getState()
|
||||
persistMessagesBeacon({
|
||||
chatId: flushedState.currentChat!.id,
|
||||
messages: flushedState.messages,
|
||||
sensitiveCredentialIds: flushedState.sensitiveCredentialIds,
|
||||
planArtifact: flushedState.streamingPlanContent || null,
|
||||
mode: flushedState.mode,
|
||||
model: flushedState.selectedModel,
|
||||
})
|
||||
}
|
||||
} catch {
|
||||
// Best-effort — don't let errors prevent page unload
|
||||
}
|
||||
})
|
||||
}
|
||||
function isPageUnloading(): boolean {
|
||||
@@ -1461,19 +1484,26 @@ export const useCopilotStore = create<CopilotStore>()(
|
||||
// Immediately put all in-progress tools into aborted state
|
||||
abortAllInProgressTools(set, get)
|
||||
|
||||
// Persist whatever contentBlocks/text we have to keep ordering for reloads
|
||||
// Persist whatever contentBlocks/text we have to keep ordering for reloads.
|
||||
// During page unload, use sendBeacon which is guaranteed to be queued even
|
||||
// as the page tears down. Regular async fetch won't complete in time.
|
||||
const { currentChat, streamingPlanContent, mode, selectedModel } = get()
|
||||
if (currentChat) {
|
||||
try {
|
||||
const currentMessages = get().messages
|
||||
void persistMessages({
|
||||
const persistParams = {
|
||||
chatId: currentChat.id,
|
||||
messages: currentMessages,
|
||||
sensitiveCredentialIds: get().sensitiveCredentialIds,
|
||||
planArtifact: streamingPlanContent || null,
|
||||
mode,
|
||||
model: selectedModel,
|
||||
})
|
||||
}
|
||||
if (isPageUnloading()) {
|
||||
persistMessagesBeacon(persistParams)
|
||||
} else {
|
||||
void persistMessages(persistParams)
|
||||
}
|
||||
} catch (error) {
|
||||
logger.warn('[Copilot] Failed to queue abort snapshot persistence', {
|
||||
error: error instanceof Error ? error.message : String(error),
|
||||
|
||||
Reference in New Issue
Block a user