u
This commit is contained in:
@@ -5,15 +5,25 @@ export const MODELCONVERSATION_COLUMNS = `
|
||||
model,
|
||||
created_at,
|
||||
updated_at,
|
||||
state
|
||||
state,
|
||||
started_by
|
||||
`;
|
||||
|
||||
export const LIST_BY_COMPANYUSER = `
|
||||
SELECT
|
||||
${MODELCONVERSATION_COLUMNS}
|
||||
FROM modelconversation
|
||||
WHERE companyuser_id = @companyuserId
|
||||
ORDER BY updated_at DESC, id DESC
|
||||
${MODELCONVERSATION_COLUMNS},
|
||||
(
|
||||
SELECT m.message
|
||||
FROM modelmessage m
|
||||
WHERE m.id = (
|
||||
SELECT MIN(f.id)
|
||||
FROM modelmessage f
|
||||
WHERE f.conversation_id = c.id AND f.role = 'user'
|
||||
)
|
||||
) AS first_user_message
|
||||
FROM modelconversation c
|
||||
WHERE c.companyuser_id = @companyuserId
|
||||
ORDER BY c.updated_at DESC, c.id DESC
|
||||
`;
|
||||
|
||||
export const GET_BY_COMPANYUSER_AND_ID = `
|
||||
@@ -27,7 +37,8 @@ export const CREATE = `
|
||||
INSERT INTO modelconversation (
|
||||
companyuser_id,
|
||||
provider,
|
||||
model
|
||||
model,
|
||||
started_by
|
||||
)
|
||||
OUTPUT
|
||||
INSERTED.id,
|
||||
@@ -36,11 +47,13 @@ OUTPUT
|
||||
INSERTED.model,
|
||||
INSERTED.created_at,
|
||||
INSERTED.updated_at,
|
||||
INSERTED.state
|
||||
INSERTED.state,
|
||||
INSERTED.started_by
|
||||
VALUES (
|
||||
@companyuserId,
|
||||
@provider,
|
||||
@model
|
||||
@model,
|
||||
@startedBy
|
||||
)
|
||||
`;
|
||||
|
||||
@@ -69,11 +82,30 @@ OUTPUT
|
||||
INSERTED.model,
|
||||
INSERTED.created_at,
|
||||
INSERTED.updated_at,
|
||||
INSERTED.state
|
||||
INSERTED.state,
|
||||
INSERTED.started_by
|
||||
WHERE id = @id AND companyuser_id = @companyuserId
|
||||
`;
|
||||
|
||||
/**
|
||||
* Deletes a conversation and, because started_by is not a foreign key, the whole chain of
|
||||
* subagent conversations below it (started_by -> id, recursively) in a single statement.
|
||||
* Messages go with their conversations via ON DELETE CASCADE. rowsAffected is 0 only when
|
||||
* the root conversation does not exist for this user (a root always yields >= 1 row).
|
||||
*/
|
||||
export const DELETE_BY_COMPANYUSER_AND_ID = `
|
||||
WITH subtree AS (
|
||||
SELECT id
|
||||
FROM modelconversation
|
||||
WHERE id = @id AND companyuser_id = @companyuserId
|
||||
|
||||
UNION ALL
|
||||
|
||||
SELECT c.id
|
||||
FROM modelconversation c
|
||||
INNER JOIN subtree s ON c.started_by = s.id
|
||||
WHERE c.companyuser_id = @companyuserId
|
||||
)
|
||||
DELETE FROM modelconversation
|
||||
WHERE id = @id AND companyuser_id = @companyuserId
|
||||
WHERE id IN (SELECT id FROM subtree)
|
||||
`;
|
||||
|
||||
@@ -819,10 +819,12 @@ BEGIN
|
||||
created_at DATETIME2 NOT NULL CONSTRAINT DF_modelconversation_created_at DEFAULT SYSUTCDATETIME(),
|
||||
updated_at DATETIME2 NOT NULL CONSTRAINT DF_modelconversation_updated_at DEFAULT SYSUTCDATETIME(),
|
||||
state NVARCHAR(20) NOT NULL CONSTRAINT DF_modelconversation_state DEFAULT 'idle',
|
||||
started_by INT NULL, -- id of the conversation that started this one as a subagent; deliberately NOT a foreign key
|
||||
CONSTRAINT FK_modelconversation_companyuser FOREIGN KEY (companyuser_id) REFERENCES companyuser(id) ON DELETE CASCADE,
|
||||
CONSTRAINT CK_modelconversation_state CHECK (state IN ('idle', 'busy'))
|
||||
);
|
||||
CREATE INDEX IX_modelconversation_companyuser ON modelconversation (companyuser_id, updated_at);
|
||||
CREATE INDEX IX_modelconversation_started_by ON modelconversation (started_by);
|
||||
END
|
||||
`;
|
||||
|
||||
|
||||
@@ -511,9 +511,11 @@ CREATE TABLE IF NOT EXISTS modelconversation (
|
||||
created_at TEXT NOT NULL DEFAULT (${UTC_NOW}),
|
||||
updated_at TEXT NOT NULL DEFAULT (${UTC_NOW}),
|
||||
state TEXT NOT NULL DEFAULT 'idle' CHECK (state IN ('idle', 'busy')),
|
||||
started_by INTEGER, -- id of the conversation that started this one as a subagent; deliberately NOT a foreign key
|
||||
FOREIGN KEY (companyuser_id) REFERENCES companyuser(id) ON DELETE CASCADE
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS IX_modelconversation_companyuser ON modelconversation (companyuser_id, updated_at);
|
||||
CREATE INDEX IF NOT EXISTS IX_modelconversation_started_by ON modelconversation (started_by);
|
||||
`;
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user