fix(rectification): bind default session model
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
begin;
|
||||
|
||||
create or replace function public.default_rectification_chat_session_model()
|
||||
returns trigger
|
||||
language plpgsql
|
||||
security definer
|
||||
set search_path = ''
|
||||
as $$
|
||||
begin
|
||||
if new.session_type = 'birth_time_rectification' and new.model_id is null then
|
||||
select c.model_id into new.model_id
|
||||
from public.model_config_versions v
|
||||
join public.model_configs c on c.id = v.config_id
|
||||
join public.model_providers p on p.id = v.provider_id
|
||||
where v.status = 'published'
|
||||
and v.enabled
|
||||
and v.is_default
|
||||
and p.enabled
|
||||
limit 1;
|
||||
end if;
|
||||
return new;
|
||||
end
|
||||
$$;
|
||||
|
||||
revoke all on function public.default_rectification_chat_session_model() from public, anon, authenticated;
|
||||
|
||||
drop trigger if exists chat_sessions_default_rectification_model on public.chat_sessions;
|
||||
create trigger chat_sessions_default_rectification_model
|
||||
before insert or update of session_type, model_id on public.chat_sessions
|
||||
for each row execute function public.default_rectification_chat_session_model();
|
||||
|
||||
with default_model as (
|
||||
select c.model_id
|
||||
from public.model_config_versions v
|
||||
join public.model_configs c on c.id = v.config_id
|
||||
join public.model_providers p on p.id = v.provider_id
|
||||
where v.status = 'published'
|
||||
and v.enabled
|
||||
and v.is_default
|
||||
and p.enabled
|
||||
limit 1
|
||||
)
|
||||
update public.chat_sessions s
|
||||
set model_id = default_model.model_id
|
||||
from default_model
|
||||
where s.session_type = 'birth_time_rectification'
|
||||
and s.model_id is null;
|
||||
|
||||
commit;
|
||||
@@ -101,6 +101,23 @@ test("v9 open is atomic, idempotent and resumes instead of duplicating", { skip:
|
||||
where id = '${userId}';
|
||||
`);
|
||||
|
||||
fixture.psql(`
|
||||
with provider as (
|
||||
insert into public.model_providers (code, name, provider_type, encrypted_api_key, enabled)
|
||||
values ('v9-test', 'V9 Test', 'openai', 'test-ciphertext', true)
|
||||
returning id
|
||||
), config as (
|
||||
insert into public.model_configs (model_id)
|
||||
values ('v9-default-model')
|
||||
returning id
|
||||
)
|
||||
insert into public.model_config_versions (
|
||||
config_id, version, provider_id, label, provider_model, enabled, is_default, status, published_at
|
||||
)
|
||||
select config.id, 1, provider.id, 'V9 Default', 'gpt-test', true, true, 'published', now()
|
||||
from config cross join provider;
|
||||
`);
|
||||
|
||||
const service = createLocalPostgresDataClient(
|
||||
fixture.connectionUrl("service_runtime", "service-runtime-test-password"),
|
||||
null,
|
||||
@@ -134,6 +151,10 @@ test("v9 open is atomic, idempotent and resumes instead of duplicating", { skip:
|
||||
fixture.psql(`select count(*) from public.chat_sessions where user_id = '${userId}'`),
|
||||
"1",
|
||||
);
|
||||
assert.equal(
|
||||
fixture.psql(`select model_id || ':' || model_config_version from public.chat_sessions where id = '${sessionId}'`),
|
||||
"v9-default-model:1",
|
||||
);
|
||||
assert.equal(
|
||||
fixture.psql(
|
||||
`select count(*) from public.agentic_rectification_open_ledger where user_id = '${userId}'`,
|
||||
|
||||
Reference in New Issue
Block a user